1. 设备驱动,在Linux设备模型中,设备驱动用device_driver结构来表示
- struct device_driver {
- const char *name; /*名字*/
- struct bus_type *bus; /* 附属的总线 */
- struct module *owner;
- const struct of_device_id *of_match_table;
- int (*probe) (struct device *dev); /* 设备与驱动匹配时调用 */
- int (*remove) (struct device *dev); /* 设备移除时调用 */
- void (*shutdown) (struct device *dev);
- int (*suspend) (struct device *dev,pm_message_t state);
- int (*resume) (struct device *dev);
- const struct attribute_group **groups; /* 属性组 */
- /*电源管理操作*/
- const struct dev_pm_ops *pm;
- struct driver_private *p; /* 私有数据:内嵌的kobject,设备链表,总线链表节点*/
- };
2. 声明和初始化驱动结构体
驱动结构体一般被静态的声明,至少需要初始化name和bus成员,一般情况下,不会直接 使用device_driver结构体,而是使用一个封装了它的结构体。以下是eepro100的驱动结构体声明例子:
- /* 封装的新驱动结构体 */
- struct pci_driver {
- const struct pci_device_id *id_table;
- struct device_driver driver;
- };
- /* 声明驱动结构体 */
- static struct pci_driver eepro100_driver = {
- .id_table = eepro100_pci_tbl,
- .driver = {
- .name = "eepro100",
- .bus = &pci_bus_type,
- .probe = eepro100_probe,
- .remove = eepro100_remove,
- .suspend = eepro100_suspend,
- .resume = eepro100_resume,
- },
- };
注册驱动
int driver_register(struct device_driver *drv);
注销驱动
void driver_unregister(struct device_driver *drv);
注册驱动函数分析:
- int driver_register(struct device_driver *drv)
- {
- int ret;
- struct device_driver *other;
- /* 如果私有数据p指针为NULL,会产生oops */
- BUG_ON(!drv->bus->p);
- /* 检查是否实现了probe,remove,shutdown callback */
- if ((drv->bus->probe && drv->probe) ||
- (drv->bus->remove && drv->remove) ||
- (drv->bus->shutdown && drv->shutdown))
- printk(KERN_WARNING "Driver '%s' needs updating - please use "
- "bus_type methods\n", drv->name);
- /* 查找总线上是否已经注册此驱动 */
- other = driver_find(drv->name, drv->bus);
- if (other) {
- put_driver(other);
- printk(KERN_ERR "Error: Driver '%s' is already registered, "
- "aborting...\n", drv->name);
- return -EBUSY;
- }
- /* 向总线注册驱动 */
- ret = bus_add_driver(drv);
- if (ret)
- return ret;
- /* 创建属性文件 */
- ret = driver_add_groups(drv, drv->groups);
- if (ret)
- bus_remove_driver(drv);
- return ret;
- }
- int bus_add_driver(struct device_driver *drv)
- {
- struct bus_type *bus;
- struct driver_private *priv;
- int error = 0;
- /* bus的引用计数加1 */
- bus = bus_get(drv->bus);
- if (!bus)
- return -EINVAL;
- priv = kzalloc(sizeof(*priv), GFP_KERNEL);
- if (!priv) {
- error = -ENOMEM;
- goto out_put_bus;
- }
- klist_init(&priv->klist_devices, NULL, NULL);
- priv->driver = drv;
- drv->p = priv;
- priv->kobj.kset = bus->p->drivers_kset;
- /* 初始化和添加kobject */
- error = kobject_init_and_add(&priv->kobj, &driver_ktype, NULL,
- "%s", drv->name);
- if (error)
- goto out_unregister;
- /* 如果设置了自动probe,会调用driver_attach */
- if (drv->bus->p->drivers_autoprobe) {
- error = driver_attach(drv);
- if (error)
- goto out_unregister;
- }
- /* 将驱动添加到总线上的驱动链表 */
- klist_add_tail(&priv->knode_bus, &bus->p->klist_drivers);
- module_add_driver(drv->owner, drv);
- /* 创建驱动属性文件 */
- error = driver_create_file(drv, &driver_attr_uevent);
- if (error) {
- printk(KERN_ERR "%s: uevent attr (%s) failed\n",
- __func__, drv->name);
- }
- error = driver_add_attrs(bus, drv);
- if (error) {
- /* How the hell do we get out of this pickle? Give up */
- printk(KERN_ERR "%s: driver_add_attrs(%s) failed\n",
- __func__, drv->name);
- }
- if (!drv->suppress_bind_attrs) {
- error = add_bind_files(drv);
- if (error) {
- /* Ditto */
- printk(KERN_ERR "%s: add_bind_files(%s) failed\n",
- __func__, drv->name);
- }
- }
- /* 向用户空间发送KOBJ_ADD uevent */
- kobject_uevent(&priv->kobj, KOBJ_ADD);
- return 0;
- }
driver_attach()->bus_for_each_dev()遍历bus上的所有设备,并对每个device调用__driver_attach()来完成驱动和设备的绑定
- static int __driver_attach(struct device *dev, void *data)
- {
- struct device_driver *drv = data;
- /* 驱动和设备匹配 */
- if (!driver_match_device(drv, dev))
- return 0;
- if (dev->parent) /* Needed for USB */
- device_lock(dev->parent);
- device_lock(dev);
- if (!dev->driver)
- /* 调用probe */
- driver_probe_device(drv, dev);
- device_unlock(dev);
- if (dev->parent)
- device_unlock(dev->parent);
- return 0;
- }
当有驱动支持的设备时,调用__driver_attach函数完成相应的工作__driver_attach()->driver_probe_device()->driver_probe_device()->bus->probe()->drv->probe () 优先调用总线中定义的probe函数,如果bus中未定义probe,则再调用驱动中定义的probe。
4. 驱动属性,驱动属性由driver_attribute来描述
- struct driver_attribute {
- struct attribute attr;
- ssize_t (*show)(struct device_driver*driver, char *buf);
- ssize_t (*store)(struct device_driver*driver, const char *buf,
- size_t count);
- };
- DRIVER_ATTR(_name,_mode, _show, _store)
创建属性
intdriver_create_file(struct device_driver *driver,
const structdriver_attribute *attr);
删除属性
voiddriver_remove_file(struct device_driver *driver,
const struct driver_attribute *attr);
7.4 实例分析
- /*
- * for learn driver
- */
- #include <linux/kernel.h>
- #include <linux/module.h>
- #include <linux/init.h>
- #include <linux/device.h>
- #include <linux/string.h>
- extern struct bus_type scbus_type;
- static char *Version = "revision 1.0,scdriver";
- static int sc_probe(struct device *dev)
- {
- printk("driverfound device\n");
- return0;
- }
- static int sc_remove(struct device *dev)
- {
- printk("deviceremove\n");
- return0;
- }
- struct device_driver scdriver = {
- .name = "scdevice0", /* 驱动名字,用来匹配支持的设备 */
- .bus = &scbus_type, /* 依附的总线类型 */
- .probe = sc_probe, /* 设备和驱动匹配时调用 */
- .remove = sc_remove, /* 设备移除时调用 */
- };
- /*
- * export driver attribute
- */
- static ssize_t driver_show_version(structdevice_driver *driver, char *buf)
- {
- returnsprintf(buf, "%s\n", Version);
- }
- static DRIVER_ATTR(version, S_IRUGO,driver_show_version, NULL);
- static int __init scdriver_init(void)
- {
- intret;
- /* 注册驱动 */
- ret= driver_register(&scdriver);
- if(ret)
- returnret;
- /*创建属性 */
- ret= driver_create_file(&scdriver, &driver_attr_version);
- if(ret)
- gotoerr_create;
- printk("drvierregistered\n");
- return0;
- err_create:
- driver_unregister(&scdriver);
- returnret;
- }
- static void __exit scdriver_exit(void)
- {
- driver_remove_file(&scdriver,&driver_attr_version);
- driver_unregister(&scdriver);
- }
- module_init(scdriver_init);
- module_exit(scdriver_exit);
- MODULE_LICENSE("Dual BSD/GPL");
- MODULE_AUTHOR("CJOK<cjok.liao@gmail.com>");
试验结果: