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