设备
2.设备
379struct device {
380 struct device *parent;
381
382 struct device_private *p;
383
384 struct kobject kobj;
385 const char *init_name; /* initial name of thedevice */
386 struct device_type *type;
387
388 struct semaphore sem; /* semaphore to synchronizecalls to
389 * its driver.
390 */
391
392 struct bus_type *bus; /* type of bus device is on*/
393 struct device_driver *driver; /* which driver has allocatedthis
394 device */
395 void *platform_data; /* Platform specific data,device
396 core doesn't touch it */
397 struct dev_pm_info power;
398
399#ifdef CONFIG_NUMA
400 int numa_node; /* NUMA node this device isclose to */
401#endif
402 u64 *dma_mask; /* dma mask (if dma'abledevice) */
403 u64 coherent_dma_mask;/* Like dma_mask, but for
404 alloc_coherent mappingsas
405 not all hardwaresupports
406 64 bit addresses forconsistent
407 allocations suchdescriptors. */
408
409 struct device_dma_parameters *dma_parms;
410
411 struct list_head dma_pools; /* dma pools (ifdma'ble) */
412
413 struct dma_coherent_mem *dma_mem; /* internal for coherentmem
414 override */
415 /* arch specific additions */
416 struct dev_archdata archdata;
417
418 dev_t devt; /* dev_t, creates the sysfs"dev" */
419
420 spinlock_t devres_lock;
421 struct list_head devres_head;
422
423 struct klist_node knode_class;
424 struct class *class;
425 const struct attribute_group **groups; /* optional groups */
426
427 void (*release)(struct device *dev);
428};
parent代表设备的父设备,bus一看就知道代表设备挂载哪条总线上,driver代表设备的驱动。设备也有个私有数据结构。
66/**
67 * struct device_private - structure to hold the private to thedriver core portions of the device structure.
68 *
69 * @klist_children - klist containing all children of this device
70 * @knode_parent - node in sibling list
71 * @knode_driver - node in driver list
72 * @knode_bus - node in bus list
73 * @driver_data - private pointer for driver specific info. Willturn into a
74 * list soon.
75 * @device - pointer back to the struct class that this structure is
76 * associated with.
77 *
78 * Nothing outside of the driver core should ever touch these fields.
79 */
80struct device_private {
81 struct klist klist_children;
82 struct klist_node knode_parent;
83 struct klist_node knode_driver;
84 struct klist_node knode_bus;
85 void *driver_data;
86 struct device *device;
87};
klist_children代表子设备链表,然后是三个链表节点,分别用于父设备的子设备链表、驱动的设备链表和总线的设备链表。来看设备的注册函数
1016/**
1017 * device_register - register a device with the system.
1018 * @dev: pointer to the device structure
1019 *
1020 * This happens in two clean steps - initialize the device
1021 * and add it to the system. The two steps can be called
1022 * separately, but this is the easiest and most common.
1023 * I.e. you should only call the two helpers separately if
1024 * have a clearly defined need to use and refcount the device
1025 * before it is added to the hierarchy.
1026 *
1027 * NOTE: _Never_ directly free @dev after calling this function, even
1028 * if it returned an error! Always use put_device() to give up the
1029 * reference initialized in this function instead.
1030 */
1031int device_register(struct device *dev)
1032{
1033 device_initialize(dev);
1034 return device_add(dev);
1035}
一个一个的看,先看device_initialize
543/**
544 * device_initialize - init device structure.
545 * @dev: device.
546 *
547 * This prepares the device for use by other layers by initializing
548 * its fields.
549 * It is the first half of device_register(), if called by
550 * that function, though it can also be called separately, so one
551 * may use @dev's fields. In particular, get_device()/put_device()
552 * may be used for reference counting of @dev after calling this
553 * function.
554 *
555 * NOTE: Use put_device() to give up your reference instead offreeing
556 * @dev directly once you have called this function.
557 */
558void device_initialize(struct device *dev)
559{
560 dev->kobj.kset = devices_kset;
561 kobject_init(&dev->kobj, &device_ktype);
562 INIT_LIST_HEAD(&dev->dma_pools);
563 init_MUTEX(&dev->sem);
564 spin_lock_init(&dev->devres_lock);
565 INIT_LIST_HEAD(&dev->devres_head);
566 device_init_wakeup(dev, 0);
567 device_pm_init(dev);
568 set_dev_node(dev, -1);
569}
就是对设备做了一些初始化,至于初始化了什么东西,这里就不细说了。
867/**
868 * device_add - add device to device hierarchy.
869 * @dev: device.
870 *
871 * This is part 2 of device_register(), though may be called
872 * separately _iff_ device_initialize() has been called separately.
873 *
874 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
875 * to the global and sibling lists for the device, then
876 * adds it to the other relevant subsystems of the driver model.
877 *
878 * NOTE: _Never_ directly free @dev after calling this function, even
879 * if it returned an error! Always use put_device() to give up your
880 * reference instead.
881 */
882int device_add(struct device *dev)
883{
884 struct device *parent = NULL;
885 struct class_interface *class_intf;
886 int error = -EINVAL;
887
888 dev = get_device(dev);
889 if (!dev)
890 goto done;
891
892 if (!dev->p) {
893 error = device_private_init(dev);
894 if (error)
895 goto done;
896 }
897
898 /*
899 * for statically allocated devices, which should all beconverted
900 * some day, we need to initialize the name. We preventreading back
901 * the name, and force the use of dev_name()
902 */
903 if (dev->init_name) {
904 dev_set_name(dev, "%s", dev->init_name);
905 dev->init_name = NULL;
906 }
907
908 if (!dev_name(dev))
909 goto name_error;
910
911 pr_debug("device: '%s': %s\n", dev_name(dev),__func__);
912
913 parent = get_device(dev->parent);
914 setup_parent(dev, parent);
915
916 /* use parent numa_node */
917 if (parent)
918 set_dev_node(dev, dev_to_node(parent));
919
920 /* first, register with generic layer. */
921 /* we require the name to be set before, and pass NULL */
922 error = kobject_add(&dev->kobj, dev->kobj.parent,NULL);
923 if (error)
924 goto Error;
925
926 /* notify platform of device entry */
927 if (platform_notify)
928 platform_notify(dev);
929
930 error = device_create_file(dev, &uevent_attr);
931 if (error)
932 goto attrError;
933
934 if (MAJOR(dev->devt)) {
935 error = device_create_file(dev, &devt_attr);
936 if (error)
937 goto ueventattrError;
938
939 error = device_create_sys_dev_entry(dev);
940 if (error)
941 goto devtattrError;
942
943 devtmpfs_create_node(dev);
944 }
945
946 error = device_add_class_symlinks(dev);
947 if (error)
948 goto SymlinkError;
949 error = device_add_attrs(dev);
950 if (error)
951 goto AttrsError;
952 error = bus_add_device(dev);
953 if (error)
954 goto BusError;
955 error = dpm_sysfs_add(dev);
956 if (error)
957 goto DPMError;
958 device_pm_add(dev);
959
960 /* Notify clients of device addition. This call must come
961 * after dpm_sysf_add() and before kobject_uevent().
962 */
963 if (dev->bus)
964 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
965 BUS_NOTIFY_ADD_DEVICE,dev);
966
967 kobject_uevent(&dev->kobj, KOBJ_ADD);
968 bus_probe_device(dev);
969 if (parent)
970 klist_add_tail(&dev->p->knode_parent,
971 &parent->p->klist_children);
972
973 if (dev->class) {
974 mutex_lock(&dev->class->p->class_mutex);
975 /* tie the class to the device */
976 klist_add_tail(&dev->knode_class,
977 &dev->class->p->class_devices);
978
979 /* notify any interfaces that the device is here */
980 list_for_each_entry(class_intf,
981 &dev->class->p->class_interfaces,node)
982 if (class_intf->add_dev)
983 class_intf->add_dev(dev,class_intf);
984 mutex_unlock(&dev->class->p->class_mutex);
985 }
986done:
987 put_device(dev);
988 return error;
989 DPMError:
990 bus_remove_device(dev);
991 BusError:
992 device_remove_attrs(dev);
993 AttrsError:
994 device_remove_class_symlinks(dev);
995 SymlinkError:
996 if (MAJOR(dev->devt))
997 device_remove_sys_dev_entry(dev);
998 devtattrError:
999 if (MAJOR(dev->devt))
1000 device_remove_file(dev, &devt_attr);
1001 ueventattrError:
1002 device_remove_file(dev, &uevent_attr);
1003 attrError:
1004 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
1005 kobject_del(&dev->kobj);
1006 Error:
1007 cleanup_device_parent(dev);
1008 if (parent)
1009 put_device(parent);
1010name_error:
1011 kfree(dev->p);
1012 dev->p = NULL;
1013 goto done;
1014}
这个函数有点吓人,还是同以前一样,抓住主要的看就行了,来看两个函数,bus_add_device和bus_probe_device
458/**
459 * bus_add_device - add device to bus
460 * @dev: device being added
461 *
462 * - Add device's bus attributes.
463 * - Create links to device's bus.
464 * - Add the device to its bus's list of devices.
465 */
466int bus_add_device(struct device *dev)
467{
468 struct bus_type *bus = bus_get(dev->bus);
469 int error = 0;
470
471 if (bus) {
472 pr_debug("bus: '%s': add device %s\n",bus->name, dev_name(dev));
473 error = device_add_attrs(bus, dev);
474 if (error)
475 goto out_put;
476 error =sysfs_create_link(&bus->p->devices_kset->kobj,
477 &dev->kobj,dev_name(dev));
478 if (error)
479 goto out_id;
480 error = sysfs_create_link(&dev->kobj,
481 &dev->bus->p->subsys.kobj,"subsystem");
482 if (error)
483 goto out_subsys;
484 error = make_deprecated_bus_links(dev);
485 if (error)
486 goto out_deprecated;
487 klist_add_tail(&dev->p->knode_bus,&bus->p->klist_devices);
488 }
489 return 0;
490
491out_deprecated:
492 sysfs_remove_link(&dev->kobj, "subsystem");
493out_subsys:
494 sysfs_remove_link(&bus->p->devices_kset->kobj,dev_name(dev));
495out_id:
496 device_remove_attrs(bus, dev);
497out_put:
498 bus_put(dev->bus);
499 return error;
500}
我们看这一句
487 klist_add_tail(&dev->p->knode_bus,&bus->p->klist_devices);
就是将设备添加到总线的设备链表里面。
502/**
503 * bus_probe_device - probe drivers for a new device
504 * @dev: device to probe
505 *
506 * - Automatically probe for a driver if the bus allows it.
507 */
508void bus_probe_device(struct device *dev)
509{
510 struct bus_type *bus = dev->bus;
511 int ret;
512
513 if (bus && bus->p->drivers_autoprobe) {
514 ret = device_attach(dev);
515 WARN_ON(ret < 0);
516 }
517}
224/**
225 * device_attach - try to attach device to a driver.
226 * @dev: device.
227 *
228 * Walk the list of drivers that the bus has and call
229 * driver_probe_device() for each pair. If a compatible
230 * pair is found, break out and return.
231 *
232 * Returns 1 if the device was bound to a driver;
233 * 0 if no matching driver was found;
234 * -ENODEV if the device is not registered.
235 *
236 * When called for a USB interface, @dev->parent->sem must beheld.
237 */
238int device_attach(struct device *dev)
239{
240 int ret = 0;
241
242 down(&dev->sem);
243 if (dev->driver) {
244 ret = device_bind_driver(dev);
245 if (ret == 0)
246 ret = 1;
247 else {
248 dev->driver = NULL;
249 ret = 0;
250 }
251 } else {
252 pm_runtime_get_noresume(dev);
253 ret = bus_for_each_drv(dev->bus, NULL, dev,__device_attach);
254 pm_runtime_put_sync(dev);
255 }
256 up(&dev->sem);
257 return ret;
258}
259EXPORT_SYMBOL_GPL(device_attach);
因为驱动可以支持多个设备,所以驱动有一个设备链表。这里先判断设备是否已经指定了驱动,如果设备已经指定了驱动,那就调用device_bind_driver函数将设备添加到设备指定驱动的设备链表中。
76/**
77 * device_bind_driver - bind a driver to one device.
78 * @dev: device.
79 *
80 * Allow manual attachment of a driver to a device.
81 * Caller must have already set @dev->driver.
82 *
83 * Note that this does not modify the bus reference count
84 * nor take the bus's rwsem. Please verify those are accounted
85 * for before calling this. (It is ok to call with no other effort
86 * from a driver's probe() method.)
87 *
88 * This function must be called with @dev->sem held.
89 */
90int device_bind_driver(struct device *dev)
91{
92 int ret;
93
94 ret = driver_sysfs_add(dev);
95 if (!ret)
96 driver_bound(dev);
97 return ret;
98}
99EXPORT_SYMBOL_GPL(device_bind_driver);
32static void driver_bound(struct device *dev)
33{
34 if (klist_node_attached(&dev->p->knode_driver)) {
35 printk(KERN_WARNING "%s: device %s alreadybound\n",
36 __func__, kobject_name(&dev->kobj));
37 return;
38 }
39
40 pr_debug("driver: '%s': %s: bound to device '%s'\n",dev_name(dev),
41 __func__, dev->driver->name);
42
43 if (dev->bus)
44 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
45 BUS_NOTIFY_BOUND_DRIVER,dev);
46
47 klist_add_tail(&dev->p->knode_driver,&dev->driver->p->klist_devices);
48}
如果设备还没有指定驱动,那就调用bus_for_each_drv函数去遍历总线的驱动链表,找到一个驱动,就调用__device_attach函数。
375/**
376 * bus_for_each_drv - driver iterator
377 * @bus: bus we're dealing with.
378 * @start: driver to start iterating on.
379 * @data: data to pass to the callback.
380 * @fn: function to call for each driver.
381 *
382 * This is nearly identical to the device iterator above.
383 * We iterate over each driver that belongs to @bus, and call
384 * @fn for each. If @fn returns anything but 0, we break out
385 * and return it. If @start is not NULL, we use it as the head
386 * of the list.
387 *
388 * NOTE: we don't return the driver that returns a non-zero
389 * value, nor do we leave the reference count incremented for that
390 * driver. If the caller needs to know that info, it must set it
391 * in the callback. It must also be sure to increment the refcount
392 * so it doesn't disappear before returning to the caller.
393 */
394int bus_for_each_drv(struct bus_type *bus, struct device_driver*start,
395 void *data, int (*fn)(struct device_driver *,void *))
396{
397 struct klist_iter i;
398 struct device_driver *drv;
399 int error = 0;
400
401 if (!bus)
402 return -EINVAL;
403
404 klist_iter_init_node(&bus->p->klist_drivers, &i,
405 start ? &start->p->knode_bus :NULL);
406 while ((drv = next_driver(&i)) && !error)
407 error = fn(drv, data);
408 klist_iter_exit(&i);
409 return error;
410}
411EXPORT_SYMBOL_GPL(bus_for_each_drv);
214static int __device_attach(struct device_driver *drv, void *data)
215{
216 struct device *dev = data;
217
218 if (!driver_match_device(drv, dev))
219 return 0;
220
221 return driver_probe_device(drv, dev);
222}
120static inline int driver_match_device(struct device_driver *drv,
121 struct device *dev)
122{
123 return drv->bus->match ? drv->bus->match(dev,drv) : 1;
124}
我们看先是调用driver_match_device函数去匹配一个设备和驱动,怎样去匹配的呢,就是调用总线的match函数。如果匹配未成功,返回0,如果匹配成功,调用接下来的函数driver_probe_device。
185/**
186 * driver_probe_device - attempt to bind device & driver together
187 * @drv: driver to bind a device to
188 * @dev: device to try to bind to the driver
189 *
190 * This function returns -ENODEV if the device is not registered,
191 * 1 if the device is bound sucessfully and 0 otherwise.
192 *
193 * This function must be called with @dev->sem held. When calledfor a
194 * USB interface, @dev->parent->sem must be held as well.
195 */
196int driver_probe_device(struct device_driver *drv, struct device*dev)
197{
198 int ret = 0;
199
200 if (!device_is_registered(dev))
201 return -ENODEV;
202
203 pr_debug("bus: '%s': %s: matched device %s with driver%s\n",
204 drv->bus->name, __func__, dev_name(dev),drv->name);
205
206 pm_runtime_get_noresume(dev);
207 pm_runtime_barrier(dev);
208 ret = really_probe(dev, drv);
209 pm_runtime_put_sync(dev);
210
211 return ret;
212}
104static int really_probe(struct device *dev, struct device_driver*drv)
105{
106 int ret = 0;
107
108 atomic_inc(&probe_count);
109 pr_debug("bus: '%s': %s: probing driver %s with device%s\n",
110 drv->bus->name, __func__, drv->name,dev_name(dev));
111 WARN_ON(!list_empty(&dev->devres_head));
112
113 dev->driver = drv;
114 if (driver_sysfs_add(dev)) {
115 printk(KERN_ERR "%s: driver_sysfs_add(%s)failed\n",
116 __func__, dev_name(dev));
117 goto probe_failed;
118 }
119
120 if (dev->bus->probe) {
121 ret = dev->bus->probe(dev);
122 if (ret)
123 goto probe_failed;
124 } else if (drv->probe) {
125 ret = drv->probe(dev);
126 if (ret)
127 goto probe_failed;
128 }
129
130 driver_bound(dev);
131 ret = 1;
132 pr_debug("bus: '%s': %s: bound device %s to driver%s\n",
133 drv->bus->name, __func__, dev_name(dev),drv->name);
134 goto done;
135
136probe_failed:
137 devres_release_all(dev);
138 driver_sysfs_remove(dev);
139 dev->driver = NULL;
140
141 if (ret != -ENODEV && ret != -ENXIO) {
142 /* driver matched but the probe failed */
143 printk(KERN_WARNING
144 "%s: probe of %s failed with error %d\n",
145 drv->name, dev_name(dev), ret);
146 }
147 /*
148 * Ignore errors returned by ->probe so that the nextdriver can try
149 * its luck.
150 */
151 ret = 0;
152done:
153 atomic_dec(&probe_count);
154 wake_up(&probe_waitqueue);
155 return ret;
156}
因为前面已经匹配成功了,总线已经为设备找到了一个驱动,所以说先指定设备的驱动就是该驱动,如果总线定义了probe函数,就先调用总线的probe函数,如果驱动定义了probe函数,就调用驱动的probe函数,最后还是会调用driver_bound函数将这个设备添加到设备指定驱动的设备链表中。至此,设备注册过程完成。再来看设备的注销函数device_unregister
1129/**
1130 * device_unregister - unregister device from system.
1131 * @dev: device going away.
1132 *
1133 * We do this in two parts, like we do device_register(). First,
1134 * we remove it from all the subsystems with device_del(), then
1135 * we decrement the reference count via put_device(). If that
1136 * is the final reference count, the device will be cleaned up
1137 * via device_release() above. Otherwise, the structure will
1138 * stick around until the final reference to the device is dropped.
1139 */
1140void device_unregister(struct device *dev)
1141{
1142 pr_debug("device: '%s': %s\n", dev_name(dev),__func__);
1143 device_del(dev);
1144 put_device(dev);
1145}
1061/**
1062 * device_del - delete device from system.
1063 * @dev: device.
1064 *
1065 * This is the first part of the device unregistration
1066 * sequence. This removes the device from the lists we control
1067 * from here, has it removed from the other driver model
1068 * subsystems it was added to in device_add(), and removes it
1069 * from the kobject hierarchy.
1070 *
1071 * NOTE: this should be called manually _iff_ device_add() was
1072 * also called manually.
1073 */
1074void device_del(struct device *dev)
1075{
1076 struct device *parent = dev->parent;
1077 struct class_interface *class_intf;
1078
1079 /* Notify clients of device removal. This call must come
1080 * before dpm_sysfs_remove().
1081 */
1082 if (dev->bus)
1083 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1084 BUS_NOTIFY_DEL_DEVICE,dev);
1085 device_pm_remove(dev);
1086 dpm_sysfs_remove(dev);
1087 if (parent)
1088 klist_del(&dev->p->knode_parent);
1089 if (MAJOR(dev->devt)) {
1090 devtmpfs_delete_node(dev);
1091 device_remove_sys_dev_entry(dev);
1092 device_remove_file(dev, &devt_attr);
1093 }
1094 if (dev->class) {
1095 device_remove_class_symlinks(dev);
1096
1097 mutex_lock(&dev->class->p->class_mutex);
1098 /* notify any interfaces that the device is now gone*/
1099 list_for_each_entry(class_intf,
1100 &dev->class->p->class_interfaces,node)
1101 if (class_intf->remove_dev)
1102 class_intf->remove_dev(dev,class_intf);
1103 /* remove the device from the class list */
1104 klist_del(&dev->knode_class);
1105 mutex_unlock(&dev->class->p->class_mutex);
1106 }
1107 device_remove_file(dev, &uevent_attr);
1108 device_remove_attrs(dev);
1109 bus_remove_device(dev);
1110
1111 /*
1112 * Some platform devices are driven without driver attached
1113 * and managed resources may have been acquired. Make sure
1114 * all resources are released.
1115 */
1116 devres_release_all(dev);
1117
1118 /* Notify the platform of the removal, in case they
1119 * need to do anything...
1120 */
1121 if (platform_notify_remove)
1122 platform_notify_remove(dev);
1123 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
1124 cleanup_device_parent(dev);
1125 kobject_del(&dev->kobj);
1126 put_device(parent);
1127}
只看bus_remove_device函数
519/**
520 * bus_remove_device - remove device from bus
521 * @dev: device to be removed
522 *
523 * - Remove symlink from bus's directory.
524 * - Delete device from bus's list.
525 * - Detach from its driver.
526 * - Drop reference taken in bus_add_device().
527 */
528void bus_remove_device(struct device *dev)
529{
530 if (dev->bus) {
531 sysfs_remove_link(&dev->kobj, "subsystem");
532 remove_deprecated_bus_links(dev);
533 sysfs_remove_link(&dev->bus->p->devices_kset->kobj,
534 dev_name(dev));
535 device_remove_attrs(dev->bus, dev);
536 if (klist_node_attached(&dev->p->knode_bus))
537 klist_del(&dev->p->knode_bus);
538
539 pr_debug("bus: '%s': remove device %s\n",
540 dev->bus->name, dev_name(dev));
541 device_release_driver(dev);
542 bus_put(dev->bus);
543 }
544}
将设备从总线的设备链表中删除掉,然后是device_release_driver。
341/**
342 * device_release_driver - manually detach device from driver.
343 * @dev: device.
344 *
345 * Manually detach device from driver.
346 * When called for a USB interface, @dev->parent->sem must beheld.
347 */
348void device_release_driver(struct device *dev)
349{
350 /*
351 * If anyone calls device_release_driver() recursively from
352 * within their ->remove callback for the same device,they
353 * will deadlock right here.
354 */
355 down(&dev->sem);
356 __device_release_driver(dev);
357 up(&dev->sem);
358}
359EXPORT_SYMBOL_GPL(device_release_driver);
305/*
306 * __device_release_driver() must be called with @dev->sem held.
307 * When called for a USB interface, @dev->parent->sem must beheld as well.
308 */
309static void __device_release_driver(struct device *dev)
310{
311 struct device_driver *drv;
312
313 drv = dev->driver;
314 if (drv) {
315 pm_runtime_get_noresume(dev);
316 pm_runtime_barrier(dev);
317
318 driver_sysfs_remove(dev);
319
320 if (dev->bus)
321 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
322 BUS_NOTIFY_UNBIND_DRIVER,
323 dev);
324
325 if (dev->bus && dev->bus->remove)
326 dev->bus->remove(dev);
327 else if (drv->remove)
328 drv->remove(dev);
329 devres_release_all(dev);
330 dev->driver = NULL;
331 klist_remove(&dev->p->knode_driver);
332 if (dev->bus)
333 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
334 BUS_NOTIFY_UNBOUND_DRIVER,
335 dev);
336
337 pm_runtime_put_sync(dev);
338 }
339}
我们看,如果总线定义了remove函数,就调用总线的remove函数,如果驱动定义了remove函数,就调用驱动的remove函数,然后将设备从驱动的设备链表中删除掉。
我总结了有三个除名,一是父设备中除名,二是总线中除名,三是驱动中除名。