class.txt 4.4 KB

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