# BRCM_VERSION=3
[bcm963xx.git] / kernel / linux / Documentation / driver-model / bus.txt
1
2 Bus Types 
3
4 Definition
5 ~~~~~~~~~~
6
7 struct bus_type {
8         char                    * name;
9         rwlock_t                lock;
10         atomic_t                refcount;
11
12         struct list_head        node;
13         struct list_head        devices;
14         struct list_head        drivers;
15
16         struct driver_dir_entry dir;
17         struct driver_dir_entry device_dir;
18         struct driver_dir_entry driver_dir;
19
20         int     (*match)        (struct device * dev, struct device_driver * drv);
21         struct device (*add)    (struct device * parent, char * bus_id);
22 };
23
24 int bus_register(struct bus_type * bus);
25
26
27 Declaration
28 ~~~~~~~~~~~
29
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.
33
34 struct bus_type pci_bus_type = {
35        .name    = "pci",
36        .match   = pci_bus_match,
37 };
38
39 The structure should be exported to drivers in a header file:
40
41 extern struct bus_type pci_bus_type;
42
43
44 Registration
45 ~~~~~~~~~~~~
46
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. 
51
52
53 Callbacks
54 ~~~~~~~~~
55
56 match(): Attaching Drivers to Devices
57 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
58
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
62 driver structure. 
63
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
68 type-safety. 
69
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. 
73
74 add(): Adding a child device
75 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
76
77 The add callback is available to notify the bus about a child device
78 at a particular location. 
79
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
83 the bus driver.
84
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.
88
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:
93
94 struct device * pci_bus_add(struct device * parent, char * bus_id)
95 {
96         ...
97         /* the device already exists */
98         return ERR_PTR(-EEXIST);
99         ...
100 }
101
102 The caller can check the return value using IS_ERR():
103
104     struct device * newdev = pci_bus_type.add(parent,bus_id);
105     if (IS_ERR(newdev)) {
106         ...
107     }
108
109
110 Device and Driver Lists
111 ~~~~~~~~~~~~~~~~~~~~~~~
112
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
117 necessary. 
118
119 The LDM core provides helper functions for iterating over each list.
120
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));
125
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. 
132
133
134 sysfs
135 ~~~~~~~~
136 There is a top-level directory named 'bus'.
137
138 Each bus gets a directory in the bus directory, along with two default
139 directories:
140
141         /sys/bus/pci/
142         |-- devices
143         `-- drivers
144
145 Drivers registered with the bus get a directory in the bus's drivers
146 directory:
147
148         /sys/bus/pci/
149         |-- devices
150         `-- drivers
151             |-- Intel ICH
152             |-- Intel ICH Joystick
153             |-- agpgart
154             `-- e100
155
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
158 hierarchy:
159
160         /sys/bus/pci/
161         |-- devices
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
165         `-- drivers
166
167
168 Exporting Attributes
169 ~~~~~~~~~~~~~~~~~~~~
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);
174 };
175
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 
178 like this:
179
180 static BUS_ATTR(debug,0644,show_debug,store_debug);
181
182 is equivalent to declaring:
183
184 static bus_attribute bus_attr_debug;
185
186 This can then be used to add and remove the attribute from the bus's
187 sysfs directory using:
188
189 int bus_create_file(struct bus_type *, struct bus_attribute *);
190 void bus_remove_file(struct bus_type *, struct bus_attribute *);
191
192