Teach you how to implement Linux Misc device driver one from zero (based on friendly arm 4412 Development Board)

Source: Internet
Author: User

about how to write a misc device, in the previous article has introduced the approximate process, now let us implement a simplest Misc device driver. http://blog.csdn.net/morixinguan/article/details/52700146
The following four articles about the preceding character device can be used as a reference:http://blog.csdn.net/morixinguan/article/details/55002774
http://blog.csdn.net/morixinguan/article/details/55003176
http://blog.csdn.net/morixinguan/article/details/55004646
http://blog.csdn.net/morixinguan/article/details/55006654

why learn to program misc devices? because, if each of our drive equipment to write a character device driver, like the original, to allocate the main device number, the second device number, the implementation of the corresponding file operation function and so on the steps, it is a bit more, and it is not good to remember it, for this, the Linux kernel provides a series of lazy tricks, That is the realization of the Misc device, in fact, misc equipment, is also a character device, but the character device is encapsulated, see the following introduction to know. Let's first look at the structure of the misc device:
miscdevice struct struct Miscdevice  {//Secondary device number is generally assigned to misc_dynamic_minor----> The kernel automatically assigns the secondary device number int minor;// The name of the misc device const char *name;//file operations struct const struct File_operations *fops;struct list_head list;struct device *parent; struct device *this_device;const char *nodename;umode_t mode;};
Here, we write the simplest misc device, we only need to pay attention to minor (secondary device number), name (device name), FoPs (file operation function).See here we can imagine that the misc device is a character device to do a re-encapsulation, and, in the Misc device, the main device number is the same, are 10, only the second device number is different, when we do not know the kernel should be allocated that this device number, Minor can be assigned to misc_dynamic_minor this macro, meaning that the kernel to help us assign the secondary device number. name does not have to say, if the device registration succeeds, under the root file system/dev/will have the name after registering the device. FoPs is a series of file operation functions, what open, read, write, ioctl and so on, a lot, and the same as the character device. So, next, let's look at an example, here I omit my makefile and kcofig, can refer to the previous article, easy to implement.
implement a simple Misc device driver (written on the TINY4412 Development Board)cdev_test.c
#include <linux/kernel.h> #include <linux/module.h> #include <linux/miscdevice.h> #include < linux/fs.h> #include <linux/types.h> #include <linux/moduleparam.h> #include <linux/slab.h># Include <linux/ioctl.h> #include <linux/cdev.h> #include <linux/delay.h> #include <linux/gpio.h > #include <mach/gpio.h> #include <plat/gpio-cfg.h>//misc device Name---> is the character device # define DEVICE_NAME "Misc_ Dev "//implement Open function int tiny4412_misc_dev_open (struct inode *inode, struct file *filp) {PRINTK (" tiny4412 misc dev open!\n "); return 0;} Implement the Close function int tiny4412_misc_dev_close (struct inode *inode, struct file *filp) {PRINTK ("tiny4412 misc dev close!\n"); return 0;} Initialization file operation struct struct file_operations tiny4412_file_ops = {. Owner = This_module,. open = Tiny4412_misc_dev_open,.release = t iny4412_misc_dev_close,};//Initialize misc device struct struct Miscdevice Tiny4412_misc_dev = {//The secondary device number is automatically assigned by the kernel. Minor = Misc_dynamic_ MINOR,//Initialize the device name. Name = device_name,//Initialize the file operation structure. FoPs = &tiny4412_file_ops,};static int __init tiny4412_misc_dev_init (void) {int ret_error;//Register misc device INT ret = Misc_register (   &tiny4412_misc_dev); if (ret! = 0) {ret_error = ret;   PRINTK ("Misc Register fair!\n"); Goto Fair;} PRINTK ("Misc init success!\n"); return ret; Fair:return Ret_error;} static void __exit tiny4412_misc_dev_exit (void) {//Logoff misc device Misc_deregister (&tiny4412_misc_dev);} Module_init (Tiny4412_misc_dev_init); Module_exit (Tiny4412_misc_dev_exit); Module_license ("GPL"); Module_author ("Yyx Add Misc Driver");
then, after compiling the driver, say zimage download to the board. We can see that at boot time, misc Init succeeded in printing, proving that the Misc device registration was successful.Next, to the root filesystem of Android, we open/dev/, we can see the device name is Misc_dev device:we can see that the main device number of the Misc_dev is 10, the secondary device number isNext, Cat/proc/misc:
Note that misc has been registered to the proc file system. In the next section, we will implement a real misc device driver.



Teach you how to implement Linux Misc device driver one from zero (based on friendly arm 4412 Development Board)

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.