Device driver development manual simplified version, manualsimplified
Device driver development manual simplified version
Author: Eliot shao
Blog URL: blog.csdn.net/eliot_shao
Date: 2015.03.26
Description: linux device driver model is regular, Stand in the angle of the device driver users, just makes nodes in/dev (udev) or/sys (sysfs ). so just operate file system to build a bridge from user space file nodes to kernel data structures and device driver functions. each device has a device number, each device number connects to a device operations (file operations ).
Unused device driver framework
** Malloc master and slave device number
Int alloc_chrdev_region (dev_t * dev, unsigned baseminor, unsigned count,
Const char * name)
Initialize a cdev structure
Void cdev_init (struct cdev * cdev, const struct file_operations * fops)
NOTE: Fill file_operations!
Add a char device to the system
Int cdev_add (struct cdev * p, dev_t dev, unsigned count)
Now cdev has been create and add to system, but there is NO file node under the/dev connected device number. There are two ways to do:
1, Created under the/dev directory statically .~ #: Sudo mknod/dev/xxx c 234 0;
2. Kernel sysfs support, which is an important part of the Linux device model .**
2. Sysfs operations
** Create device type directory xxx on/sys/class, create a struct class structure
Struct class * class_create (struct module * owner, const char * name)
Creates a class device and registers it with sysfs, Send uevents to udev, so it'll create/dev nodes
Struct class_device * class_device_create (struct class * cls,
Struct class_device * parent,
Dev_t devt,
Struct device * device,
Const char * fmt ,...)
Create sysfs attribute file for device
Int device_create_file (struct device * dev, struct device_attribute * attr )**
Use device driver framework
If you use device driver framework, It's easier!
Input devices, for example:
1, Allocate an input device data structure
Input_allocate_device ();
2, Register with the input subsystem
Input_register_device (xxx_input_dev );
That is!
Stay tuned for more share ......