2019-06-18 16:53:27 -04:00
|
|
|
==============
|
2005-04-16 18:20:36 -04:00
|
|
|
Device Classes
|
2019-06-18 16:53:27 -04:00
|
|
|
==============
|
2005-04-16 18:20:36 -04:00
|
|
|
|
|
|
|
Introduction
|
|
|
|
~~~~~~~~~~~~
|
|
|
|
A device class describes a type of device, like an audio or network
|
|
|
|
device. The following device classes have been identified:
|
|
|
|
|
|
|
|
<Insert List of Device Classes Here>
|
|
|
|
|
|
|
|
|
|
|
|
Each device class defines a set of semantics and a programming interface
|
|
|
|
that devices of that class adhere to. Device drivers are the
|
2006-10-03 16:50:39 -04:00
|
|
|
implementation of that programming interface for a particular device on
|
2019-06-18 16:53:27 -04:00
|
|
|
a particular bus.
|
2005-04-16 18:20:36 -04:00
|
|
|
|
|
|
|
Device classes are agnostic with respect to what bus a device resides
|
2019-06-18 16:53:27 -04:00
|
|
|
on.
|
2005-04-16 18:20:36 -04:00
|
|
|
|
|
|
|
|
|
|
|
Programming Interface
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~
|
2019-06-18 16:53:27 -04:00
|
|
|
The device class structure looks like::
|
2005-04-16 18:20:36 -04:00
|
|
|
|
|
|
|
|
2019-06-18 16:53:27 -04:00
|
|
|
typedef int (*devclass_add)(struct device *);
|
|
|
|
typedef void (*devclass_remove)(struct device *);
|
2005-04-16 18:20:36 -04:00
|
|
|
|
2011-05-04 19:55:37 -04:00
|
|
|
See the kerneldoc for the struct class.
|
2005-04-16 18:20:36 -04:00
|
|
|
|
2019-06-18 16:53:27 -04:00
|
|
|
A typical device class definition would look like::
|
2005-04-16 18:20:36 -04:00
|
|
|
|
2019-06-18 16:53:27 -04:00
|
|
|
struct device_class input_devclass = {
|
2005-04-16 18:20:36 -04:00
|
|
|
.name = "input",
|
|
|
|
.add_device = input_add_device,
|
|
|
|
.remove_device = input_remove_device,
|
2019-06-18 16:53:27 -04:00
|
|
|
};
|
2005-04-16 18:20:36 -04:00
|
|
|
|
|
|
|
Each device class structure should be exported in a header file so it
|
|
|
|
can be used by drivers, extensions and interfaces.
|
|
|
|
|
2019-06-18 16:53:27 -04:00
|
|
|
Device classes are registered and unregistered with the core using::
|
2005-04-16 18:20:36 -04:00
|
|
|
|
2019-06-18 16:53:27 -04:00
|
|
|
int devclass_register(struct device_class * cls);
|
|
|
|
void devclass_unregister(struct device_class * cls);
|
2005-04-16 18:20:36 -04:00
|
|
|
|
|
|
|
|
|
|
|
Devices
|
|
|
|
~~~~~~~
|
|
|
|
As devices are bound to drivers, they are added to the device class
|
|
|
|
that the driver belongs to. Before the driver model core, this would
|
|
|
|
typically happen during the driver's probe() callback, once the device
|
|
|
|
has been initialized. It now happens after the probe() callback
|
2019-06-18 16:53:27 -04:00
|
|
|
finishes from the core.
|
2005-04-16 18:20:36 -04:00
|
|
|
|
|
|
|
The device is enumerated in the class. Each time a device is added to
|
|
|
|
the class, the class's devnum field is incremented and assigned to the
|
|
|
|
device. The field is never decremented, so if the device is removed
|
|
|
|
from the class and re-added, it will receive a different enumerated
|
2019-06-18 16:53:27 -04:00
|
|
|
value.
|
2005-04-16 18:20:36 -04:00
|
|
|
|
|
|
|
The class is allowed to create a class-specific structure for the
|
2019-06-18 16:53:27 -04:00
|
|
|
device and store it in the device's class_data pointer.
|
2005-04-16 18:20:36 -04:00
|
|
|
|
|
|
|
There is no list of devices in the device class. Each driver has a
|
|
|
|
list of devices that it supports. The device class has a list of
|
|
|
|
drivers of that particular class. To access all of the devices in the
|
|
|
|
class, iterate over the device lists of each driver in the class.
|
|
|
|
|
|
|
|
|
|
|
|
Device Drivers
|
|
|
|
~~~~~~~~~~~~~~
|
|
|
|
Device drivers are added to device classes when they are registered
|
|
|
|
with the core. A driver specifies the class it belongs to by setting
|
2019-06-18 16:53:27 -04:00
|
|
|
the struct device_driver::devclass field.
|
2005-04-16 18:20:36 -04:00
|
|
|
|
|
|
|
|
|
|
|
sysfs directory structure
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2019-06-18 16:53:27 -04:00
|
|
|
There is a top-level sysfs directory named 'class'.
|
2005-04-16 18:20:36 -04:00
|
|
|
|
|
|
|
Each class gets a directory in the class directory, along with two
|
2019-06-18 16:53:27 -04:00
|
|
|
default subdirectories::
|
2005-04-16 18:20:36 -04:00
|
|
|
|
|
|
|
class/
|
|
|
|
`-- input
|
|
|
|
|-- devices
|
|
|
|
`-- drivers
|
|
|
|
|
|
|
|
|
2019-06-18 16:53:27 -04:00
|
|
|
Drivers registered with the class get a symlink in the drivers/ directory
|
|
|
|
that points to the driver's directory (under its bus directory)::
|
2005-04-16 18:20:36 -04:00
|
|
|
|
|
|
|
class/
|
|
|
|
`-- input
|
|
|
|
|-- devices
|
|
|
|
`-- drivers
|
|
|
|
`-- usb:usb_mouse -> ../../../bus/drivers/usb_mouse/
|
|
|
|
|
|
|
|
|
2019-06-18 16:53:27 -04:00
|
|
|
Each device gets a symlink in the devices/ directory that points to the
|
|
|
|
device's directory in the physical hierarchy::
|
2005-04-16 18:20:36 -04:00
|
|
|
|
|
|
|
class/
|
|
|
|
`-- input
|
|
|
|
|-- devices
|
|
|
|
| `-- 1 -> ../../../root/pci0/00:1f.0/usb_bus/00:1f.2-1:0/
|
|
|
|
`-- drivers
|
|
|
|
|
|
|
|
|
|
|
|
Exporting Attributes
|
|
|
|
~~~~~~~~~~~~~~~~~~~~
|
2019-06-18 16:53:27 -04:00
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
struct devclass_attribute {
|
2005-04-16 18:20:36 -04:00
|
|
|
struct attribute attr;
|
|
|
|
ssize_t (*show)(struct device_class *, char * buf, size_t count, loff_t off);
|
|
|
|
ssize_t (*store)(struct device_class *, const char * buf, size_t count, loff_t off);
|
2019-06-18 16:53:27 -04:00
|
|
|
};
|
2005-04-16 18:20:36 -04:00
|
|
|
|
|
|
|
Class drivers can export attributes using the DEVCLASS_ATTR macro that works
|
2019-06-18 16:53:27 -04:00
|
|
|
similarly to the DEVICE_ATTR macro for devices. For example, a definition
|
|
|
|
like this::
|
2005-04-16 18:20:36 -04:00
|
|
|
|
2019-06-18 16:53:27 -04:00
|
|
|
static DEVCLASS_ATTR(debug,0644,show_debug,store_debug);
|
2005-04-16 18:20:36 -04:00
|
|
|
|
2019-06-18 16:53:27 -04:00
|
|
|
is equivalent to declaring::
|
2005-04-16 18:20:36 -04:00
|
|
|
|
2019-06-18 16:53:27 -04:00
|
|
|
static devclass_attribute devclass_attr_debug;
|
2005-04-16 18:20:36 -04:00
|
|
|
|
|
|
|
The bus driver can add and remove the attribute from the class's
|
2019-06-18 16:53:27 -04:00
|
|
|
sysfs directory using::
|
2005-04-16 18:20:36 -04:00
|
|
|
|
2019-06-18 16:53:27 -04:00
|
|
|
int devclass_create_file(struct device_class *, struct devclass_attribute *);
|
|
|
|
void devclass_remove_file(struct device_class *, struct devclass_attribute *);
|
2005-04-16 18:20:36 -04:00
|
|
|
|
|
|
|
In the example above, the file will be named 'debug' in placed in the
|
2019-06-18 16:53:27 -04:00
|
|
|
class's directory in sysfs.
|
2005-04-16 18:20:36 -04:00
|
|
|
|
|
|
|
|
|
|
|
Interfaces
|
|
|
|
~~~~~~~~~~
|
|
|
|
There may exist multiple mechanisms for accessing the same device of a
|
2019-06-18 16:53:27 -04:00
|
|
|
particular class type. Device interfaces describe these mechanisms.
|
2005-04-16 18:20:36 -04:00
|
|
|
|
|
|
|
When a device is added to a device class, the core attempts to add it
|
|
|
|
to every interface that is registered with the device class.
|