Device driver development manual simplified version

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

  1. Create cdev

** 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……

发布了129 篇原创文章 · 获赞 322 · 访问量 49万+

猜你喜欢

转载自blog.csdn.net/seek_0380/article/details/44646949
今日推荐