First Linux driver

Source: Internet
Author: User
In Linux, drivers are mapped to files. these files are called device files or driver files, which are stored in the dev directory. This design concept maps drivers to files in Linux drive systems. these files are called device files or driver files,
Are saved in the/dev/directory. This design concept makes interaction with the Linux driver like
It is as easy to interact with common files.

--- Compile the Linux driver

Like other types of Linux programs, Linux drivers also have their own rules. To
The following is a general step for compiling a basic Linux driver.

1) create a Linux driver skeleton (load and uninstall the Linux driver)
A program of any type has a basic structure, just as a C program requires a main function.
The same number. When using the driver, the kernel also needs to initialize and establish the device first.
File, memory allocation, etc. when the driver is detached, resources need to be released and files need to be deleted.
Work. In the Linux driver, two functions are required for initialization and exit.
Both module_init and module_exit are supported by the driver.

2) Register and destroy device files
All devices in Linux are files, so each driver needs a device file,
Creating a device file is usually done at the initialization location. Delete a device file
Where the driver exits.

3) specify driver information
The driver is self-described. You can use modinfo to obtain the driver author and
Information such as protocol and driver description. This information must be specified in the driver code.
MODULE_AUTHOR MODULE_LICENSE MODULE_ALIAS

4) specify the callback function
Linux specifies multiple actions and can also be events. For example, write data to a device file
The "write" event is triggered, and the Linux system calls the write callback function of the corresponding driver.
When reading data from a device file, a read event is triggered. the system calls the read callback function of the driver.
A driver does not need to specify all destroyed functions. The callback function uses the relevant mechanism.
To register.

5) write business logic
This is the core part of the driver. the above four parts are meaningless only because of the skeleton. Any
The complete Linux driver will do some work related to its functions.
6) compile the Makefile file
7) compile the driver
8) install and uninstall the Linux driver

--- Driver analysis
Unlike normal files, device files cannot be created using IO functions, but must be registered with dedicated devices.
Function and device destruction function. Create a device file when initializing the driver and delete it when detaching the driver.
Device file. The device also needs a struct to describe its related information. The device struct contains
An important member variable fops, used to describe the function pointer of a device file at various triggering times. Such
The process of operating device files is clear.
--- Code implementation
The following code references and organizes the book "deep exploration of HAL and driver development for Android" (-Li Ning.

# Include
# Include
# Include
# Include
# Include
# Include

# Define DEVICE_NAME "worldcount"

Static unsigned char mem [1000];
Static char read_flag = 'y ';
Static int written_count = 0;


Static ssize_t wordCountRead (struct file * file, char _ user * buf, size_t count, loff_t * ppos)
{
If (read_flag = 'n ')
{
Copy_to_user (buf, (void *) mem, written_count );
Printk ("read count: % d", written_count );
Read_flag = 'y ';
Return written_count;
}
Else
Return 0;
}

Static ssize_t wordCountWrite (struct file * file, const char _ user * buf, size_t count, loff_t * ppos)
{
Copy_from_user (mem, buf, count );
Read_flag = 'n ';
Written_count = count;
Printk ("written count: % d", count );
Return count;
}

Static struct file_operations dev_fops =
{. Owner = THIS_MODULE,. read = wordCountRead,. write = wordCountWrite };

Static struct miscdevice misc =
{. Minor = MISC_DYNAMIC_MINOR,. name = DEVICE_NAME,. fops = & dev_fops };


Static int wordCount_init (void)
{
Int ret;
Ret = misc_register (& misc );
Printk ("word count init success \ n ");
Return ret;
}

Static void wordCount_exit (void)
{
Misc_deregister (& misc );
Printk ("word count exit success \ n ");
}

Module_init (wordCount_init );
Module_exit (wordCount_exit );


MODULE_LICENSE ("GPL ");
MODULE_ALIAS ("word count module ");
MODULE_AUTHOR ("lining ");

--- Code analysis
1) the driver is a simple character device Drive MISC device, which is easy to register and not complex to operate
2) in addition to introducing necessary header files and defining related variables, the code also specifically implements read/write operations on device files.
3) all functions and variables are defined as static, which facilitates driver efficiency improvement.
4) the read/write callback function is specified in the code and the file operation structure is specified.
The device installation and removal work, so we will find the wordcount device file under/dev.
5) Finally, the device file information is specified.
--- Error summary
Summarize the errors encountered in writing the driver.
1) the system forgets to install and uninstall the specified device and mistakenly writes it as MODULE_INIT (wordCount_init );
MODULE_EXIT (wordCount_exit). This will cause the device file to fail to be generated under/dev/, but use the command
The driver can still be installed successfully, and modinfo can also be used to view information.
2) use sudo echo 'hello world'>/dev/wordCount to operate successfully, and use cat to view the characters that can be displayed
However, when the module is uninstalled, the system will prompt that the resource is busy and cannot be uninstalled.
PS --> the above test error was tested in Ubuntu. when I switched to win7 and used a virtual machine for testing

This problem does not occur. the above problem may be an accident.

--- Drive the test code

# Include
# Include
# Include
# Include
# Include

Void main ()
{
Int fd;
Char * buf;
Buf = (char *) malloc (10 );
Puts ("hello world \ n ");
Fd = open ("/dev/worldcount", O_RDWR );
If (fd> 0)
Printf ("open success % d \ n", fd );
Else
Printf ("open failed \ n ");

Write (fd, "test", sizeof ("test "));
Read (fd, buf, 1 );
Buf [1] = '\ 0 ';
Puts (buf );
}

--- Code effect
The result of the executed code is as follows:
Hello world

Open success 3
T

It turns out that the driver works, but we won't see it when we go to cat/dev/worldcout.
(I'm also curious why I don't see the string test .)
Basically, this simple MISC device driver is complete.
O (tips _ tips) O thank you for recording your learning experience.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.