Linux设备模型分析之device_driver(基于3.10.1内核)

一、device_driver定义
 

[cpp] view plain copy

  1. 181/** 
  2. 182 * struct device_driver - The basic device driver structure 
  3. 183 * @name:   Name of the device driver. 
  4. 184 * @bus:    The bus which the device of this driver belongs to. 
  5. 185 * @owner:  The module owner. 
  6. 186 * @mod_name:   Used for built-in modules. 
  7. 187 * @suppress_bind_attrs: Disables bind/unbind via sysfs. 
  8. 188 * @of_match_table: The open firmware table. 
  9. 189 * @acpi_match_table: The ACPI match table. 
  10. 190 * @probe:  Called to query the existence of a specific device, 
  11. 191 *      whether this driver can work with it, and bind the driver 
  12. 192 *      to a specific device. 
  13. 193 * @remove: Called when the device is removed from the system to 
  14. 194 *      unbind a device from this driver. 
  15. 195 * @shutdown:   Called at shut-down time to quiesce the device. 
  16. 196 * @suspend:    Called to put the device to sleep mode. Usually to a 
  17. 197 *      low power state. 
  18. 198 * @resume: Called to bring a device from sleep mode. 
  19. 199 * @groups: Default attributes that get created by the driver core 
  20. 200 *      automatically. 
  21. 201 * @pm:     Power management operations of the device which matched 
  22. 202 *      this driver. 
  23. 203 * @p:      Driver core's private data, no one other than the driver 
  24. 204 *      core can touch this. 
  25. 205 * 
  26. 206 * The device driver-model tracks all of the drivers known to the system. 
  27. 207 * The main reason for this tracking is to enable the driver core to match 
  28. 208 * up drivers with new devices. Once drivers are known objects within the 
  29. 209 * system, however, a number of other things become possible. Device drivers 
  30. 210 * can export information and configuration variables that are independent 
  31. 211 * of any specific device. 
  32. 212 */  
  33. 213struct device_driver {  
  34. 214    const char      *name;  
  35. 215    struct bus_type     *bus;  
  36. 216  
  37. 217    struct module       *owner;  
  38. 218    const char      *mod_name;  /* used for built-in modules */  
  39. 219  
  40. 220    bool suppress_bind_attrs;   /* disables bind/unbind via sysfs */  
  41. 221  
  42. 222    const struct of_device_id   *of_match_table;  
  43. 223    const struct acpi_device_id *acpi_match_table;  
  44. 224  
  45. 225    int (*probe) (struct device *dev);  
  46. 226    int (*remove) (struct device *dev);  
  47. 227    void (*shutdown) (struct device *dev);  
  48. 228    int (*suspend) (struct device *dev, pm_message_t state);  
  49. 229    int (*resume) (struct device *dev);  
  50. 230    const struct attribute_group **groups;  
  51. 231  
  52. 232    const struct dev_pm_ops *pm;  
  53. 233  
  54. 234    struct driver_private *p;  
  55. 235};  

 

 

name,device_driver的名字。

 

 

bus,device_driver支持的device所依附的bus。
probe,探测device_drvier是否支持参数指定的device。如果支持,则绑定该device_driver和该device。
remove,该device被移除时调用该函数,解除该device与device_driver的绑定。
shutdown,当关机时调用该函数,以关闭参数指定的device。
suspend,当device进入休眠状态时,调用该函数。
resume,当device从休眠状态被唤醒时,调用该函数。
p,device_driver私有数据,它是struct driver_private类型,该类型定义在drivers/base/base.h文件中,其内容如下:
 

[cpp] view plain copy

  1. 46struct driver_private {  
  2. 47    struct kobject kobj;  
  3. 48    struct klist klist_devices;  
  4. 49    struct klist_node knode_bus;  
  5. 50    struct module_kobject *mkobj;  
  6. 51    struct device_driver *driver;  
  7. 52};  

 

 

kobj,是其所属的device_driver对应的kobject。

 

 

klist_devices,其所属的device_driver支持的device链表。
driver,所属的device_driver。
 
二、device_driver的注册
device_driver的注册是通过调用driver_register函数完成的,该函数定义在drivers/base/driver.c文件中,其内容如下:
 

[cpp] view plain copy

  1. 156/** 
  2. 157 * driver_register - register driver with bus 
  3. 158 * @drv: driver to register 
  4. 159 * 
  5. 160 * We pass off most of the work to the bus_add_driver() call, 
  6. 161 * since most of the things we have to do deal with the bus 
  7. 162 * structures. 
  8. 163 */  
  9. 164int driver_register(struct device_driver *drv)  
  10. 165{  
  11. 166    int ret;  
  12. 167    struct device_driver *other;  
  13. 168  
  14. 169    BUG_ON(!drv->bus->p);  
  15. 170  
  16. 171    if ((drv->bus->probe && drv->probe) ||  
  17. 172        (drv->bus->remove && drv->remove) ||  
  18. 173        (drv->bus->shutdown && drv->shutdown))  
  19. 174        printk(KERN_WARNING "Driver '%s' needs updating - please use "  
  20. 175            "bus_type methods\n", drv->name);  
  21. 176  
  22. 177    other = driver_find(drv->name, drv->bus);  
  23. 178    if (other) {  
  24. 179        printk(KERN_ERR "Error: Driver '%s' is already registered, "  
  25. 180            "aborting...\n", drv->name);  
  26. 181        return -EBUSY;  
  27. 182    }  
  28. 183  
  29. 184    ret = bus_add_driver(drv);  
  30. 185    if (ret)  
  31. 186        return ret;  
  32. 187    ret = driver_add_groups(drv, drv->groups);  
  33. 188    if (ret) {  
  34. 189        bus_remove_driver(drv);  
  35. 190        return ret;  
  36. 191    }  
  37. 192    kobject_uevent(&drv->p->kobj, KOBJ_ADD);  
  38. 193  
  39. 194    return ret;  
  40. 195}  

 

 

171-175行,如果bus和device_driver定义了相同的函数,会优先调用bus的相应函数,这里会发出警告信息。

 

 

177行,调用driver_find在bus的drivers_kset中查找是否已经有同名device_driver已经注册过,如果已经注册过,则退出。
184行,调用bus_add_driver函数完成注册,该函数定义在drivers/base/bus.c文件中,其内容如下:
 

[cpp] view plain copy

  1. 673/** 
  2. 674 * bus_add_driver - Add a driver to the bus. 
  3. 675 * @drv: driver. 
  4. 676 */  
  5. 677int bus_add_driver(struct device_driver *drv)  
  6. 678{  
  7. 679    struct bus_type *bus;  
  8. 680    struct driver_private *priv;  
  9. 681    int error = 0;  
  10. 682  
  11. 683    bus = bus_get(drv->bus);  
  12. 684    if (!bus)  
  13. 685        return -EINVAL;  
  14. 686  
  15. 687    pr_debug("bus: '%s': add driver %s\n", bus->name, drv->name);  
  16. 688  
  17. 689    priv = kzalloc(sizeof(*priv), GFP_KERNEL);  
  18. 690    if (!priv) {  
  19. 691        error = -ENOMEM;  
  20. 692        goto out_put_bus;  
  21. 693    }  
  22. 694    klist_init(&priv->klist_devices, NULL, NULL);  
  23. 695    priv->driver = drv;  
  24. 696    drv->p = priv;  
  25. 697    priv->kobj.kset = bus->p->drivers_kset;  
  26. 698    error = kobject_init_and_add(&priv->kobj, &driver_ktype, NULL,  
  27. 699                     "%s", drv->name);  
  28. 700    if (error)  
  29. 701        goto out_unregister;  
  30. 702  
  31. 703    klist_add_tail(&priv->knode_bus, &bus->p->klist_drivers);  
  32. 704    if (drv->bus->p->drivers_autoprobe) {  
  33. 705        error = driver_attach(drv);  
  34. 706        if (error)  
  35. 707            goto out_unregister;  
  36. 708    }  
  37. 709    module_add_driver(drv->owner, drv);  
  38. 710  
  39. 711    error = driver_create_file(drv, &driver_attr_uevent);  
  40. 712    if (error) {  
  41. 713        printk(KERN_ERR "%s: uevent attr (%s) failed\n",  
  42. 714            __func__, drv->name);  
  43. 715    }  
  44. 716    error = driver_add_attrs(bus, drv);  
  45. 717    if (error) {  
  46. 718        /* How the hell do we get out of this pickle? Give up */  
  47. 719        printk(KERN_ERR "%s: driver_add_attrs(%s) failed\n",  
  48. 720            __func__, drv->name);  
  49. 721    }  
  50. 722  
  51. 723    if (!drv->suppress_bind_attrs) {  
  52. 724        error = add_bind_files(drv);  
  53. 725        if (error) {  
  54. 726            /* Ditto */  
  55. 727            printk(KERN_ERR "%s: add_bind_files(%s) failed\n",  
  56. 728                __func__, drv->name);  
  57. 729        }  
  58. 730    }  
  59. 731  
  60. 732    return 0;  
  61. 733  
  62. 734out_unregister:  
  63. 735    kobject_put(&priv->kobj);  
  64. 736    kfree(drv->p);  
  65. 737    drv->p = NULL;  
  66. 738out_put_bus:  
  67. 739    bus_put(bus);  
  68. 740    return error;  
  69. 741}  

 

 

698行,调用kobject_init_and_add函数将device_driver添加到sysfs文件系统中,因为指定了priv->kobj.kset为bus->p->drivers_kset,所以其对应的目录会出现在/sys/bus/bus_name/drivers目录下。

 

 

703行,调用klist_add_tail将device_driver加入到bus->p->klist_drivers中。
704-708行,如果drv->bus->p->drivers_autoprobe为1,则调用driver_attach(drv)函数将当前device_driver与相应device进行绑定。该函数与上一篇博客中分析device的注册过程中调用的device_attach类似。driver_attach函数定义在drivers/base/dd.c文件中,其内容如下:
 

[cpp] view plain copy

  1. 468/** 
  2. 469 * driver_attach - try to bind driver to devices. 
  3. 470 * @drv: driver. 
  4. 471 * 
  5. 472 * Walk the list of devices that the bus has on it and try to 
  6. 473 * match the driver with each one.  If driver_probe_device() 
  7. 474 * returns 0 and the @dev->driver is set, we've found a 
  8. 475 * compatible pair. 
  9. 476 */  
  10. 477int driver_attach(struct device_driver *drv)  
  11. 478{  
  12. 479    return bus_for_each_dev(drv->bus, NULL, drv, __driver_attach);  
  13. 480}  

 

 

bus_for_each_dev函数定义在drivers/base/bus.c文件中,其内容如下:

 

 

 

[cpp] view plain copy

  1. 267/** 
  2. 268 * bus_for_each_dev - device iterator. 
  3. 269 * @bus: bus type. 
  4. 270 * @start: device to start iterating from. 
  5. 271 * @data: data for the callback. 
  6. 272 * @fn: function to be called for each device. 
  7. 273 * 
  8. 274 * Iterate over @bus's list of devices, and call @fn for each, 
  9. 275 * passing it @data. If @start is not NULL, we use that device to 
  10. 276 * begin iterating from. 
  11. 277 * 
  12. 278 * We check the return of @fn each time. If it returns anything 
  13. 279 * other than 0, we break out and return that value. 
  14. 280 * 
  15. 281 * NOTE: The device that returns a non-zero value is not retained 
  16. 282 * in any way, nor is its refcount incremented. If the caller needs 
  17. 283 * to retain this data, it should do so, and increment the reference 
  18. 284 * count in the supplied callback. 
  19. 285 */  
  20. 286int bus_for_each_dev(struct bus_type *bus, struct device *start,  
  21. 287             void *data, int (*fn)(struct device *, void *))  
  22. 288{  
  23. 289    struct klist_iter i;  
  24. 290    struct device *dev;  
  25. 291    int error = 0;  
  26. 292  
  27. 293    if (!bus || !bus->p)  
  28. 294        return -EINVAL;  
  29. 295  
  30. 296    klist_iter_init_node(&bus->p->klist_devices, &i,  
  31. 297                 (start ? &start->p->knode_bus : NULL));  
  32. 298    while ((dev = next_device(&i)) && !error)  
  33. 299        error = fn(dev, data);  
  34. 300    klist_iter_exit(&i);  
  35. 301    return error;  
  36. 302}  

 

 

298-299行,这个while循环遍历bus->p->klist_devices链表,对注册在bus上的每个device调用fn函数,这里,fn函数是传递进来的__driver_attach函数,该函数定义在drivers/base/dd.c文件中,其内容如下:

 

 

 

[cpp] view plain copy

  1. 439static int __driver_attach(struct device *dev, void *data)  
  2. 440{  
  3. 441    struct device_driver *drv = data;  
  4. 442  
  5. 443    /* 
  6. 444     * Lock device and try to bind to it. We drop the error 
  7. 445     * here and always return 0, because we need to keep trying 
  8. 446     * to bind to devices and some drivers will return an error 
  9. 447     * simply if it didn't support the device. 
  10. 448     * 
  11. 449     * driver_probe_device() will spit a warning if there 
  12. 450     * is an error. 
  13. 451     */  
  14. 452  
  15. 453    if (!driver_match_device(drv, dev))  
  16. 454        return 0;  
  17. 455  
  18. 456    if (dev->parent)    /* Needed for USB */  
  19. 457        device_lock(dev->parent);  
  20. 458    device_lock(dev);  
  21. 459    if (!dev->driver)  
  22. 460        driver_probe_device(drv, dev);  
  23. 461    device_unlock(dev);  
  24. 462    if (dev->parent)  
  25. 463        device_unlock(dev->parent);  
  26. 464  
  27. 465    return 0;  
  28. 466}  

 

 

453行,调用driver_match_device函数,该函数定义在drivers/base/base.h文件中,其内容如下:

 

 

 

[cpp] view plain copy

  1. 116static inline int driver_match_device(struct device_driver *drv,  
  2. 117                      struct device *dev)  
  3. 118{  
  4. 119    return drv->bus->match ? drv->bus->match(dev, drv) : 1;  
  5. 120}  

 

 

如果定义了drv->bus->match函数,则调用之,否则直接返回1。

 

 

回到__driver_attach函数:
460行,调用driver_probe_device函数,该函数定义在drivers/base/dd.c文件中,其内容如下:
 

[cpp] view plain copy

  1. 360/** 
  2. 361 * driver_probe_device - attempt to bind device & driver together 
  3. 362 * @drv: driver to bind a device to 
  4. 363 * @dev: device to try to bind to the driver 
  5. 364 * 
  6. 365 * This function returns -ENODEV if the device is not registered, 
  7. 366 * 1 if the device is bound successfully and 0 otherwise. 
  8. 367 * 
  9. 368 * This function must be called with @dev lock held.  When called for a 
  10. 369 * USB interface, @dev->parent lock must be held as well. 
  11. 370 */  
  12. 371int driver_probe_device(struct device_driver *drv, struct device *dev)  
  13. 372{  
  14. 373    int ret = 0;  
  15. 374  
  16. 375    if (!device_is_registered(dev))  
  17. 376        return -ENODEV;  
  18. 377  
  19. 378    pr_debug("bus: '%s': %s: matched device %s with driver %s\n",  
  20. 379         drv->bus->name, __func__, dev_name(dev), drv->name);  
  21. 380  
  22. 381    pm_runtime_barrier(dev);  
  23. 382    ret = really_probe(dev, drv);  
  24. 383    pm_request_idle(dev);  
  25. 384  
  26. 385    return ret;  
  27. 386}  

 

 

375行,调用device_is_registered函数判断device是否已经在sysfs系统中注册过,如果还没有注册过,则返回ENODEV,退出。该函数定义在include/linux/device.h文件中,其内容如下:

 

 

 

[cpp] view plain copy

  1. 787static inline int device_is_registered(struct device *dev)  
  2. 788{  
  3. 789    return dev->kobj.state_in_sysfs;  
  4. 790}  

 

 

382行,调用really_probe函数,该函数定义在drivers/base/dd.c文件中,其内容如下:

 

 

 

[cpp] view plain copy

  1. 265static int really_probe(struct device *dev, struct device_driver *drv)  
  2. 266{  
  3. 267    int ret = 0;  
  4. 268  
  5. 269    atomic_inc(&probe_count);  
  6. 270    pr_debug("bus: '%s': %s: probing driver %s with device %s\n",  
  7. 271         drv->bus->name, __func__, drv->name, dev_name(dev));  
  8. 272    WARN_ON(!list_empty(&dev->devres_head));  
  9. 273  
  10. 274    dev->driver = drv;  
  11. 275  
  12. 276    /* If using pinctrl, bind pins now before probing */  
  13. 277    ret = pinctrl_bind_pins(dev);  
  14. 278    if (ret)  
  15. 279        goto probe_failed;  
  16. 280  
  17. 281    if (driver_sysfs_add(dev)) {  
  18. 282        printk(KERN_ERR "%s: driver_sysfs_add(%s) failed\n",  
  19. 283            __func__, dev_name(dev));  
  20. 284        goto probe_failed;  
  21. 285    }  
  22. 286  
  23. 287    if (dev->bus->probe) {  
  24. 288        ret = dev->bus->probe(dev);  
  25. 289        if (ret)  
  26. 290            goto probe_failed;  
  27. 291    } else if (drv->probe) {  
  28. 292        ret = drv->probe(dev);  
  29. 293        if (ret)  
  30. 294            goto probe_failed;  
  31. 295    }  
  32. 296  
  33. 297    driver_bound(dev);  
  34. 298    ret = 1;  
  35. 299    pr_debug("bus: '%s': %s: bound device %s to driver %s\n",  
  36. 300         drv->bus->name, __func__, dev_name(dev), drv->name);  
  37. 301    goto done;  
  38. 302  
  39. 303probe_failed:  
  40. 304    devres_release_all(dev);  
  41. 305    driver_sysfs_remove(dev);  
  42. 306    dev->driver = NULL;  
  43. 307    dev_set_drvdata(dev, NULL);  
  44. 308  
  45. 309    if (ret == -EPROBE_DEFER) {  
  46. 310        /* Driver requested deferred probing */  
  47. 311        dev_info(dev, "Driver %s requests probe deferral\n", drv->name);  
  48. 312        driver_deferred_probe_add(dev);  
  49. 313    } else if (ret != -ENODEV && ret != -ENXIO) {  
  50. 314        /* driver matched but the probe failed */  
  51. 315        printk(KERN_WARNING  
  52. 316               "%s: probe of %s failed with error %d\n",  
  53. 317               drv->name, dev_name(dev), ret);  
  54. 318    } else {  
  55. 319        pr_debug("%s: probe of %s rejects match %d\n",  
  56. 320               drv->name, dev_name(dev), ret);  
  57. 321    }  
  58. 322    /* 
  59. 323     * Ignore errors returned by ->probe so that the next driver can try 
  60. 324     * its luck. 
  61. 325     */  
  62. 326    ret = 0;  
  63. 327done:  
  64. 328    atomic_dec(&probe_count);  
  65. 329    wake_up(&probe_waitqueue);  
  66. 330    return ret;  
  67. 331}  

 

 

287-295行,如果定义了dev->bus->probe函数,则调用该函数;如果没有定义dev->bus->probe函数,但是定义了drv->probe函数,则调用drv->probe函数。这里,我们一般写Linux驱动程序时都要实现的probe函数就会被调用了。

 

 

297行,调用driver_bound(dev)函数,该函数定义在drivers/base/dd.c文件中,其内容如下:
 

[cpp] view plain copy

  1. 182static void driver_bound(struct device *dev)  
  2. 183{  
  3. 184    if (klist_node_attached(&dev->p->knode_driver)) {  
  4. 185        printk(KERN_WARNING "%s: device %s already bound\n",  
  5. 186            __func__, kobject_name(&dev->kobj));  
  6. 187        return;  
  7. 188    }  
  8. 189  
  9. 190    pr_debug("driver: '%s': %s: bound to device '%s'\n", dev_name(dev),  
  10. 191         __func__, dev->driver->name);  
  11. 192  
  12. 193    klist_add_tail(&dev->p->knode_driver, &dev->driver->p->klist_devices);  
  13. 194  
  14. 195    /* 
  15. 196     * Make sure the device is no longer in one of the deferred lists and 
  16. 197     * kick off retrying all pending devices 
  17. 198     */  
  18. 199    driver_deferred_probe_del(dev);  
  19. 200    driver_deferred_probe_trigger();  
  20. 201  
  21. 202    if (dev->bus)  
  22. 203        blocking_notifier_call_chain(&dev->bus->p->bus_notifier,  
  23. 204                         BUS_NOTIFY_BOUND_DRIVER, dev);  
  24. 205}  

 

 

193行,调用klist_add_tail函数将device加入到device_driver的driver->p->klist_devices链表中。

 

 

至此,我们一步一步回退driver_bound->really_probe-> driver_probe_device->__driver_attach->driver_attach->bus_add_driver。
回到bus_add_driver函数:
711行,调用driver_create_file(drv, &driver_attr_uevent)函数,创建属性文件,driver_attr_uevent定义在drivers/base/bus.c文件中:
 

[cpp] view plain copy

  1. 671static DRIVER_ATTR(uevent, S_IWUSR, NULL, driver_uevent_store);  

 

 

DRIVER_ATTR宏定义在include/linux/device.h文件中:

 

 

 

[cpp] view plain copy

  1. 256#define DRIVER_ATTR(_name, _mode, _show, _store)    \  
  2. 257struct driver_attribute driver_attr_##_name =       \  
  3. 258    __ATTR(_name, _mode, _show, _store)  

 

 

__ATTR宏定义在include/linux/sysfs.h文件中:

 

 

 

[cpp] view plain copy

  1. 71#define __ATTR(_name,_mode,_show,_store) { \  
  2. 72    .attr = {.name = __stringify(_name), .mode = _mode },   \  
  3. 73    .show   = _show,                    \  
  4. 74    .store  = _store,                   \  
  5. 75}  

 

 

回到bus_add_driver函数:

 

 

716行,调用driver_add_attrs(bus, drv)函数,为bus->drv_attrs创建属性文件。
724行,调用add_bind_files为driver_attr_unbind和driver_attr_bind创建属性文件。
回到driver_register函数:
187行,调用driver_add_groups,创建属性组。
192行,调用kobject_uevent(&drv->p->kobj, KOBJ_ADD),发送uenvnt事件通知用户空间。
至此,我们就清楚device_driver是怎样注册的了。
This is, on the surface, a book about writing device drivers for the Linux system. That is a worthy goal, of course; the flow of new hardware products is not likely to slow down anytime soon, and somebody is going to have to make all those new gadgets work with Linux. But this book is also about how the Linux kernel works and how to adapt its workings to your needs or interests. Linux is an open system; with this book, we hope, it is more open and accessible to a larger community of developers. This is the third edition of Linux Device Drivers. The kernel has changed greatly since this book was first published, and we have tried to evolve the text to match. This edition covers the 2.6.10 kernel as completely as we are able. We have, this time around, elected to omit the discussion of backward compatibility with previous kernel versions. The changes from 2.4 are simply too large, and the 2.4 interface remains well documented in the (freely available) second edition. This edition contains quite a bit of new material relevant to the 2.6 kernel. The discussion of locking and concurrency has been expanded and moved into its own chapter. The Linux device model, which is new in 2.6, is covered in detail. There are new chapters on the USB bus and the serial driver subsystem; the chapter on PCI has also been enhanced. While the organization of the rest of the book resembles that of the earlier editions, every chapter has been thoroughly updated. We hope you enjoy reading this book as much as we have enjoyed writing it.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值