12 struct list_head node;
13 struct list_head devices;
14 struct list_head drivers;
16 struct driver_dir_entry dir;
17 struct driver_dir_entry device_dir;
18 struct driver_dir_entry driver_dir;
20 int (*match) (struct device * dev, struct device_driver * drv);
21 struct device (*add) (struct device * parent, char * bus_id);
24 int bus_register(struct bus_type * bus);
30 Each bus type in the kernel (PCI, USB, etc) should declare one static
31 object of this type. They must initialize the name field, and may
32 optionally initialize the match callback.
34 struct bus_type pci_bus_type = {
36 .match = pci_bus_match,
39 The structure should be exported to drivers in a header file:
41 extern struct bus_type pci_bus_type;
47 When a bus driver is initialized, it calls bus_register. This
48 initializes the rest of the fields in the bus object and inserts it
49 into a global list of bus types. Once the bus object is registered,
50 the fields in it (e.g. the rwlock_t) are usable by the bus driver.
56 match(): Attaching Drivers to Devices
57 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
59 The format of device ID structures and the semantics for comparing
60 them are inherently bus-specific. Drivers typically declare an array
61 of device IDs of devices they support that reside in a bus-specific
64 The purpose of the match callback is provide the bus an opportunity to
65 determine if a particular driver supports a particular device by
66 comparing the device IDs the driver supports with the device ID of a
67 particular device, without sacrificing bus-specific functionality or
70 When a driver is registered with the bus, the bus's list of devices is
71 iterated over, and the match callback is called for each device that
72 does not have a driver associated with it.
74 add(): Adding a child device
75 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
77 The add callback is available to notify the bus about a child device
78 at a particular location.
80 The parent parameter is the parent device of the child to be added. If
81 parent == NULL, the bus should add the device as a child of a default
82 parent device or as a child of the root. This policy decision is up to
85 The format of the bus_id field should be consistent with the format of
86 the bus_id field of the rest of the devices on the bus. This requires
87 the caller to know the format.
89 On return, the bus driver should return a pointer to the device that
90 was created. If the device was not created, the bus driver should
91 return an appropriate error code. Refer to include/linux/err.h for
92 helper functions to encode errors. Some sample code:
94 struct device * pci_bus_add(struct device * parent, char * bus_id)
97 /* the device already exists */
98 return ERR_PTR(-EEXIST);
102 The caller can check the return value using IS_ERR():
104 struct device * newdev = pci_bus_type.add(parent,bus_id);
105 if (IS_ERR(newdev)) {
110 Device and Driver Lists
111 ~~~~~~~~~~~~~~~~~~~~~~~
113 The lists of devices and drivers are intended to replace the local
114 lists that many buses keep. They are lists of struct devices and
115 struct device_drivers, respectively. Bus drivers are free to use the
116 lists as they please, but conversion to the bus-specific type may be
119 The LDM core provides helper functions for iterating over each list.
121 int bus_for_each_dev(struct bus_type * bus, void * data,
122 int (*callback)(struct device * dev, void * data));
123 int bus_for_each_drv(struct bus_type * bus, void * data,
124 int (*callback)(struct device_driver * drv, void * data));
126 These helpers iterate over the respective list, and call the callback
127 for each device or driver in the list. All list accesses are
128 synchronized by taking the bus's lock (read currently). The reference
129 count on each object in the list is incremented before the callback is
130 called; it is decremented after the next object has been obtained. The
131 lock is not held when calling the callback.
136 There is a top-level directory named 'bus'.
138 Each bus gets a directory in the bus directory, along with two default
145 Drivers registered with the bus get a directory in the bus's drivers
152 |-- Intel ICH Joystick
156 Each device that is discovered on a bus of that type gets a symlink in
157 the bus's devices directory to the device's directory in the physical
162 | |-- 00:00.0 -> ../../../root/pci0/00:00.0
163 | |-- 00:01.0 -> ../../../root/pci0/00:01.0
164 | `-- 00:02.0 -> ../../../root/pci0/00:02.0
170 struct bus_attribute {
171 struct attribute attr;
172 ssize_t (*show)(struct bus_type *, char * buf, size_t count, loff_t off);
173 ssize_t (*store)(struct bus_type *, const char * buf, size_t count, loff_t off);
176 Bus drivers can export attributes using the BUS_ATTR macro that works
177 similarly to the DEVICE_ATTR macro for devices. For example, a definition
180 static BUS_ATTR(debug,0644,show_debug,store_debug);
182 is equivalent to declaring:
184 static bus_attribute bus_attr_debug;
186 This can then be used to add and remove the attribute from the bus's
187 sysfs directory using:
189 int bus_create_file(struct bus_type *, struct bus_attribute *);
190 void bus_remove_file(struct bus_type *, struct bus_attribute *);