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