Table of Contents
One Linux environment variable, device driver model 1. The origin of the Linux device driver model
First review the usual process of device driver compilation
【1】Implement the entry function module_init() and module unloading function module_exit();
【2】Apply for device number, register_chrdev();-----> (related to kernel)
【3】Use udev/mdev mechanism to create device file nodes class_create(), device_create();------>(related to the kernel)
【4】Hardware initialization: 1.io resource mapping ioremap(), the kernel provides gpio library function; 2. Register interrupt. ------->(Hardware related)
【5】Construct the file_operation structure--------> (related to the kernel)
[6] Implement methods of operating hardware xxx_open(), xxx_write()...
Inference: Not only the fourth part of the entire process is related to hardware, but the rest are similar operations. In order to compile device drivers conveniently (without reinventing the wheel) and save manpower, the device driver model is proposed to simplify device driver compilation. process.
2. Device driver model framework
You can view the bus object through the sysfs virtual file system (taking USB as an example)
二Linux driver programming, BUS bus model programming
Concept map
1.Bus object
【1】structbus_type: Bus object linux training institution, describes a bus, manages device and driverlinux driver programming, perfectly matched.
struct bus_type {//只列举重要成员 const char *name; //总线名字 int (*match)(struct device *dev, struct device_driver *drv);//匹配方法 };
【2】Register and logout bus
int bus_register(struct bus_type *bus); void bus_unregister(struct bus_type *bus);
【3】Build a bus
#include #include #include //实例化一个bus对象 struct bus_type mybus = { .name = "mybus", .match = mybus_match, }; EXPORT_SYMBOL(mybus); //导出总线对象,让device对象和driver对象使用 static int __init mybus_init(void) { printk("------%s---------n",__FUNCTION__); int ret; //构建一个总线结果:/sys/bus/mybus ret = bus_register(&mybus); if(ret != 0) { printk("bus_register errorn"); return ret; } return 0; } static void __exit mybus_exit(void) { printk("------%s---------n",__FUNCTION__); bus_unregister(&mybus); } module_init(mybus_init); module_exit(mybus_exit); MODULE_LICENSE("GPL");
2.device object
【1】device object: device object, describing device information, including address, interrupt and other data
struct device {//只列举重要成员 struct kobject kobj; //所有对象的父类 const char *init_name;//在/sys/bus/mybus/device的名字,用于在总线中匹配 struct bus_type *bus; //指向该device对象依附的总线对象 void *platform_data; //自定义的数据,指向任何类型的数据 };
【2】Register and logout bus
int device_register(struct device *dev); void device_unregister(struct device *dev);
【3】Write device object
定义一个描述设备的信息的结构体,匹配成功以后让driver对象在总线中领到device对象的信息,实现分离。
#include #include #include extern struct bus_type mybus; struct mydev_desc{ char* name; int irqno; unsigned long addr; }; struct mydev_desc deviofo = { .name = "hqs", .irqno = 8877, .addr = 0x12345678, }; void mydev_release(struct device* dev) { printk("------%s---------n",__FUNCTION__); } //构建一个device对象 struct device mydev = { .init_name = "fsdev_drv", .bus = &mybus, .release = mydev_release, .platform_data = &deviofo, }; static int __init mydev_init(void) { printk("------%s---------n",__FUNCTION__); int ret; //将device注册到总线中 ret = device_register(&mydev); if(ret < 0) { printk("device_register errorn"); return ret; } return 0; } static void __exit mydev_exit(void) { printk("------%s---------n",__FUNCTION__); device_unregister(&mydev); } module_init(mydev_init); module_exit(mydev_exit); MODULE_LICENSE("GPL");
3.driver对象
【1】driver对象:描述设备驱动发的方式
struct device_driver {//只列举重要成员 const char *name;//在/sys/bus/mybus/driver的名字,用于在总线中匹配 struct bus_type *bus;//指向该device对象依附的总线对象 int (*probe) (struct device *dev); //device和driver匹配之后要做的事情 int (*remove) (struct device *dev); //device和driver分离之后要做的事情 };
【2】注册和注销总线
int driver_register(struct device_driver *drv); void driver_unregister(struct device_driver *drv);
【3】编写driver对象
匹配成功以后可以在总线中领到device对象的数据,具体实现在probe函数里。
#include #include #include #include extern struct bus_type mybus; struct mydev_desc{ char* name; int irqno; unsigned long addr; }; struct mydev_desc* pdesc; int mydrv_probe(struct device *dev) { printk("------%s---------n",__FUNCTION__); pdesc = (struct mydev_desc*)dev->platform_data; printk("name =%sn",pdesc->name); printk("irqno = %dn",pdesc->irqno); unsigned long * paddr = ioremap(pdesc->addr, 8); return 0; } int mydrv_remove (struct device *dev) { printk("------%s---------n",__FUNCTION__); return 0; } //构建一个driver对象 struct device_driver mydrv = { .name = "fsdev_drv", .bus = &mybus, .probe = mydrv_probe, .remove = mydrv_remove, }; static int __init mydrv_init(void) { printk("------%s---------n",__FUNCTION__); int ret; //将device注册到总线中 ret = driver_register(&mydrv); if(ret < 0) { printk("driver_register errorn"); return ret; } return 0; } static void __exit mydrv_exit(void) { printk("------%s---------n",__FUNCTION__); driver_unregister(&mydrv); } module_init(mydrv_init); module_exit(mydrv_exit); MODULE_LICENSE("GPL");
4.device对象和driver对象匹配
【1】实现BUS对象中的match方式(按device对象的名子和driver对象中的名子相匹配)
要注意的是不能直接用device对象中的init_name,而要用device对象中继承的structkobjectkobj;上面的成员name不然会报错
int mybus_match(struct device *dev, struct device_driver *drv) { //如果匹配成功,match方法一定要返回一个1,失败返回0 if(! strncmp(drv->name,dev->kobj.name,strlen(drv->name))) { printk("match okn"); return 1; } else { printk("match failedn"); return 0; } return 0; } //实例化一个bus对象 struct bus_type mybus = { .name = "mybus", .match = mybus_match, };
【2】保证device对象和driver对象的名子一样:例如这儿都使用"fsdev_drv"才能保证能匹配成功
//构建一个device对象 struct device mydev = { .init_name = "fsdev_drv", .bus = &mybus, .release = mydev_release, .platform_data = &deviofo, };
//构建一个driver对象 struct device_driver mydrv = { .name = "fsdev_drv", .bus = &mybus, .probe = mydrv_probe, .remove = mydrv_remove, };
The above is the detailed content of Linux device driver model. For more information, please follow other related articles on the PHP Chinese website!