class.txt 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. Device Classes
  2. Introduction
  3. ~~~~~~~~~~~~
  4. A device class describes a type of device, like an audio or network
  5. device. The following device classes have been identified:
  6. <Insert List of Device Classes Here>
  7. Each device class defines a set of semantics and a programming interface
  8. that devices of that class adhere to. Device drivers are the
  9. implementation of that programming interface for a particular device on
  10. a particular bus.
  11. Device classes are agnostic with respect to what bus a device resides
  12. on.
  13. Programming Interface
  14. ~~~~~~~~~~~~~~~~~~~~~
  15. The device class structure looks like:
  16. typedef int (*devclass_add)(struct device *);
  17. typedef void (*devclass_remove)(struct device *);
  18. struct device_class {
  19. char * name;
  20. rwlock_t lock;
  21. u32 devnum;
  22. struct list_head node;
  23. struct list_head drivers;
  24. struct list_head intf_list;
  25. struct driver_dir_entry dir;
  26. struct driver_dir_entry device_dir;
  27. struct driver_dir_entry driver_dir;
  28. devclass_add add_device;
  29. devclass_remove remove_device;
  30. };
  31. A typical device class definition would look like:
  32. struct device_class input_devclass = {
  33. .name = "input",
  34. .add_device = input_add_device,
  35. .remove_device = input_remove_device,
  36. };
  37. Each device class structure should be exported in a header file so it
  38. can be used by drivers, extensions and interfaces.
  39. Device classes are registered and unregistered with the core using:
  40. int devclass_register(struct device_class * cls);
  41. void devclass_unregister(struct device_class * cls);
  42. Devices
  43. ~~~~~~~
  44. As devices are bound to drivers, they are added to the device class
  45. that the driver belongs to. Before the driver model core, this would
  46. typically happen during the driver's probe() callback, once the device
  47. has been initialized. It now happens after the probe() callback
  48. finishes from the core.
  49. The device is enumerated in the class. Each time a device is added to
  50. the class, the class's devnum field is incremented and assigned to the
  51. device. The field is never decremented, so if the device is removed
  52. from the class and re-added, it will receive a different enumerated
  53. value.
  54. The class is allowed to create a class-specific structure for the
  55. device and store it in the device's class_data pointer.
  56. There is no list of devices in the device class. Each driver has a
  57. list of devices that it supports. The device class has a list of
  58. drivers of that particular class. To access all of the devices in the
  59. class, iterate over the device lists of each driver in the class.
  60. Device Drivers
  61. ~~~~~~~~~~~~~~
  62. Device drivers are added to device classes when they are registered
  63. with the core. A driver specifies the class it belongs to by setting
  64. the struct device_driver::devclass field.
  65. sysfs directory structure
  66. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  67. There is a top-level sysfs directory named 'class'.
  68. Each class gets a directory in the class directory, along with two
  69. default subdirectories:
  70. class/
  71. `-- input
  72. |-- devices
  73. `-- drivers
  74. Drivers registered with the class get a symlink in the drivers/ directory
  75. that points to the driver's directory (under its bus directory):
  76. class/
  77. `-- input
  78. |-- devices
  79. `-- drivers
  80. `-- usb:usb_mouse -> ../../../bus/drivers/usb_mouse/
  81. Each device gets a symlink in the devices/ directory that points to the
  82. device's directory in the physical hierarchy:
  83. class/
  84. `-- input
  85. |-- devices
  86. | `-- 1 -> ../../../root/pci0/00:1f.0/usb_bus/00:1f.2-1:0/
  87. `-- drivers
  88. Exporting Attributes
  89. ~~~~~~~~~~~~~~~~~~~~
  90. struct devclass_attribute {
  91. struct attribute attr;
  92. ssize_t (*show)(struct device_class *, char * buf, size_t count, loff_t off);
  93. ssize_t (*store)(struct device_class *, const char * buf, size_t count, loff_t off);
  94. };
  95. Class drivers can export attributes using the DEVCLASS_ATTR macro that works
  96. similarly to the DEVICE_ATTR macro for devices. For example, a definition
  97. like this:
  98. static DEVCLASS_ATTR(debug,0644,show_debug,store_debug);
  99. is equivalent to declaring:
  100. static devclass_attribute devclass_attr_debug;
  101. The bus driver can add and remove the attribute from the class's
  102. sysfs directory using:
  103. int devclass_create_file(struct device_class *, struct devclass_attribute *);
  104. void devclass_remove_file(struct device_class *, struct devclass_attribute *);
  105. In the example above, the file will be named 'debug' in placed in the
  106. class's directory in sysfs.
  107. Interfaces
  108. ~~~~~~~~~~
  109. There may exist multiple mechanisms for accessing the same device of a
  110. particular class type. Device interfaces describe these mechanisms.
  111. When a device is added to a device class, the core attempts to add it
  112. to every interface that is registered with the device class.