Commit a15d49fd3094cff90e5410ca454a870e0a722fe1

Authored by Hannes Reinecke
Committed by Greg Kroah-Hartman
1 parent 97ec448aea

driver core: check start node in klist_iter_init_node

klist_iter_init_node() takes a node as a start argument.
However, this node might not be valid anymore.
This patch updates the klist_iter_init_node() and
dependent functions to return an error if so.
All calling functions have been audited to check
for a return code here.

Signed-off-by: Hannes Reinecke <hare@suse.de>
Cc: Greg Kroah-Hartmann <gregkh@linuxfoundation.org>
Cc: Kay Sievers <kay@vrfy.org>
Cc: Stable Kernel <stable@kernel.org>
Cc: Linux Kernel <linux-kernel@vger.kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Showing 6 changed files with 76 additions and 46 deletions Inline Diff

1 /* 1 /*
2 * bus.c - bus driver management 2 * bus.c - bus driver management
3 * 3 *
4 * Copyright (c) 2002-3 Patrick Mochel 4 * Copyright (c) 2002-3 Patrick Mochel
5 * Copyright (c) 2002-3 Open Source Development Labs 5 * Copyright (c) 2002-3 Open Source Development Labs
6 * Copyright (c) 2007 Greg Kroah-Hartman <gregkh@suse.de> 6 * Copyright (c) 2007 Greg Kroah-Hartman <gregkh@suse.de>
7 * Copyright (c) 2007 Novell Inc. 7 * Copyright (c) 2007 Novell Inc.
8 * 8 *
9 * This file is released under the GPLv2 9 * This file is released under the GPLv2
10 * 10 *
11 */ 11 */
12 12
13 #include <linux/device.h> 13 #include <linux/device.h>
14 #include <linux/module.h> 14 #include <linux/module.h>
15 #include <linux/errno.h> 15 #include <linux/errno.h>
16 #include <linux/slab.h> 16 #include <linux/slab.h>
17 #include <linux/init.h> 17 #include <linux/init.h>
18 #include <linux/string.h> 18 #include <linux/string.h>
19 #include <linux/mutex.h> 19 #include <linux/mutex.h>
20 #include "base.h" 20 #include "base.h"
21 #include "power/power.h" 21 #include "power/power.h"
22 22
23 /* /sys/devices/system */ 23 /* /sys/devices/system */
24 static struct kset *system_kset; 24 static struct kset *system_kset;
25 25
26 #define to_bus_attr(_attr) container_of(_attr, struct bus_attribute, attr) 26 #define to_bus_attr(_attr) container_of(_attr, struct bus_attribute, attr)
27 27
28 /* 28 /*
29 * sysfs bindings for drivers 29 * sysfs bindings for drivers
30 */ 30 */
31 31
32 #define to_drv_attr(_attr) container_of(_attr, struct driver_attribute, attr) 32 #define to_drv_attr(_attr) container_of(_attr, struct driver_attribute, attr)
33 33
34 34
35 static int __must_check bus_rescan_devices_helper(struct device *dev, 35 static int __must_check bus_rescan_devices_helper(struct device *dev,
36 void *data); 36 void *data);
37 37
38 static struct bus_type *bus_get(struct bus_type *bus) 38 static struct bus_type *bus_get(struct bus_type *bus)
39 { 39 {
40 if (bus) { 40 if (bus) {
41 kset_get(&bus->p->subsys); 41 kset_get(&bus->p->subsys);
42 return bus; 42 return bus;
43 } 43 }
44 return NULL; 44 return NULL;
45 } 45 }
46 46
47 static void bus_put(struct bus_type *bus) 47 static void bus_put(struct bus_type *bus)
48 { 48 {
49 if (bus) 49 if (bus)
50 kset_put(&bus->p->subsys); 50 kset_put(&bus->p->subsys);
51 } 51 }
52 52
53 static ssize_t drv_attr_show(struct kobject *kobj, struct attribute *attr, 53 static ssize_t drv_attr_show(struct kobject *kobj, struct attribute *attr,
54 char *buf) 54 char *buf)
55 { 55 {
56 struct driver_attribute *drv_attr = to_drv_attr(attr); 56 struct driver_attribute *drv_attr = to_drv_attr(attr);
57 struct driver_private *drv_priv = to_driver(kobj); 57 struct driver_private *drv_priv = to_driver(kobj);
58 ssize_t ret = -EIO; 58 ssize_t ret = -EIO;
59 59
60 if (drv_attr->show) 60 if (drv_attr->show)
61 ret = drv_attr->show(drv_priv->driver, buf); 61 ret = drv_attr->show(drv_priv->driver, buf);
62 return ret; 62 return ret;
63 } 63 }
64 64
65 static ssize_t drv_attr_store(struct kobject *kobj, struct attribute *attr, 65 static ssize_t drv_attr_store(struct kobject *kobj, struct attribute *attr,
66 const char *buf, size_t count) 66 const char *buf, size_t count)
67 { 67 {
68 struct driver_attribute *drv_attr = to_drv_attr(attr); 68 struct driver_attribute *drv_attr = to_drv_attr(attr);
69 struct driver_private *drv_priv = to_driver(kobj); 69 struct driver_private *drv_priv = to_driver(kobj);
70 ssize_t ret = -EIO; 70 ssize_t ret = -EIO;
71 71
72 if (drv_attr->store) 72 if (drv_attr->store)
73 ret = drv_attr->store(drv_priv->driver, buf, count); 73 ret = drv_attr->store(drv_priv->driver, buf, count);
74 return ret; 74 return ret;
75 } 75 }
76 76
77 static const struct sysfs_ops driver_sysfs_ops = { 77 static const struct sysfs_ops driver_sysfs_ops = {
78 .show = drv_attr_show, 78 .show = drv_attr_show,
79 .store = drv_attr_store, 79 .store = drv_attr_store,
80 }; 80 };
81 81
82 static void driver_release(struct kobject *kobj) 82 static void driver_release(struct kobject *kobj)
83 { 83 {
84 struct driver_private *drv_priv = to_driver(kobj); 84 struct driver_private *drv_priv = to_driver(kobj);
85 85
86 pr_debug("driver: '%s': %s\n", kobject_name(kobj), __func__); 86 pr_debug("driver: '%s': %s\n", kobject_name(kobj), __func__);
87 kfree(drv_priv); 87 kfree(drv_priv);
88 } 88 }
89 89
90 static struct kobj_type driver_ktype = { 90 static struct kobj_type driver_ktype = {
91 .sysfs_ops = &driver_sysfs_ops, 91 .sysfs_ops = &driver_sysfs_ops,
92 .release = driver_release, 92 .release = driver_release,
93 }; 93 };
94 94
95 /* 95 /*
96 * sysfs bindings for buses 96 * sysfs bindings for buses
97 */ 97 */
98 static ssize_t bus_attr_show(struct kobject *kobj, struct attribute *attr, 98 static ssize_t bus_attr_show(struct kobject *kobj, struct attribute *attr,
99 char *buf) 99 char *buf)
100 { 100 {
101 struct bus_attribute *bus_attr = to_bus_attr(attr); 101 struct bus_attribute *bus_attr = to_bus_attr(attr);
102 struct subsys_private *subsys_priv = to_subsys_private(kobj); 102 struct subsys_private *subsys_priv = to_subsys_private(kobj);
103 ssize_t ret = 0; 103 ssize_t ret = 0;
104 104
105 if (bus_attr->show) 105 if (bus_attr->show)
106 ret = bus_attr->show(subsys_priv->bus, buf); 106 ret = bus_attr->show(subsys_priv->bus, buf);
107 return ret; 107 return ret;
108 } 108 }
109 109
110 static ssize_t bus_attr_store(struct kobject *kobj, struct attribute *attr, 110 static ssize_t bus_attr_store(struct kobject *kobj, struct attribute *attr,
111 const char *buf, size_t count) 111 const char *buf, size_t count)
112 { 112 {
113 struct bus_attribute *bus_attr = to_bus_attr(attr); 113 struct bus_attribute *bus_attr = to_bus_attr(attr);
114 struct subsys_private *subsys_priv = to_subsys_private(kobj); 114 struct subsys_private *subsys_priv = to_subsys_private(kobj);
115 ssize_t ret = 0; 115 ssize_t ret = 0;
116 116
117 if (bus_attr->store) 117 if (bus_attr->store)
118 ret = bus_attr->store(subsys_priv->bus, buf, count); 118 ret = bus_attr->store(subsys_priv->bus, buf, count);
119 return ret; 119 return ret;
120 } 120 }
121 121
122 static const struct sysfs_ops bus_sysfs_ops = { 122 static const struct sysfs_ops bus_sysfs_ops = {
123 .show = bus_attr_show, 123 .show = bus_attr_show,
124 .store = bus_attr_store, 124 .store = bus_attr_store,
125 }; 125 };
126 126
127 int bus_create_file(struct bus_type *bus, struct bus_attribute *attr) 127 int bus_create_file(struct bus_type *bus, struct bus_attribute *attr)
128 { 128 {
129 int error; 129 int error;
130 if (bus_get(bus)) { 130 if (bus_get(bus)) {
131 error = sysfs_create_file(&bus->p->subsys.kobj, &attr->attr); 131 error = sysfs_create_file(&bus->p->subsys.kobj, &attr->attr);
132 bus_put(bus); 132 bus_put(bus);
133 } else 133 } else
134 error = -EINVAL; 134 error = -EINVAL;
135 return error; 135 return error;
136 } 136 }
137 EXPORT_SYMBOL_GPL(bus_create_file); 137 EXPORT_SYMBOL_GPL(bus_create_file);
138 138
139 void bus_remove_file(struct bus_type *bus, struct bus_attribute *attr) 139 void bus_remove_file(struct bus_type *bus, struct bus_attribute *attr)
140 { 140 {
141 if (bus_get(bus)) { 141 if (bus_get(bus)) {
142 sysfs_remove_file(&bus->p->subsys.kobj, &attr->attr); 142 sysfs_remove_file(&bus->p->subsys.kobj, &attr->attr);
143 bus_put(bus); 143 bus_put(bus);
144 } 144 }
145 } 145 }
146 EXPORT_SYMBOL_GPL(bus_remove_file); 146 EXPORT_SYMBOL_GPL(bus_remove_file);
147 147
148 static struct kobj_type bus_ktype = { 148 static struct kobj_type bus_ktype = {
149 .sysfs_ops = &bus_sysfs_ops, 149 .sysfs_ops = &bus_sysfs_ops,
150 }; 150 };
151 151
152 static int bus_uevent_filter(struct kset *kset, struct kobject *kobj) 152 static int bus_uevent_filter(struct kset *kset, struct kobject *kobj)
153 { 153 {
154 struct kobj_type *ktype = get_ktype(kobj); 154 struct kobj_type *ktype = get_ktype(kobj);
155 155
156 if (ktype == &bus_ktype) 156 if (ktype == &bus_ktype)
157 return 1; 157 return 1;
158 return 0; 158 return 0;
159 } 159 }
160 160
161 static const struct kset_uevent_ops bus_uevent_ops = { 161 static const struct kset_uevent_ops bus_uevent_ops = {
162 .filter = bus_uevent_filter, 162 .filter = bus_uevent_filter,
163 }; 163 };
164 164
165 static struct kset *bus_kset; 165 static struct kset *bus_kset;
166 166
167 167
168 #ifdef CONFIG_HOTPLUG 168 #ifdef CONFIG_HOTPLUG
169 /* Manually detach a device from its associated driver. */ 169 /* Manually detach a device from its associated driver. */
170 static ssize_t driver_unbind(struct device_driver *drv, 170 static ssize_t driver_unbind(struct device_driver *drv,
171 const char *buf, size_t count) 171 const char *buf, size_t count)
172 { 172 {
173 struct bus_type *bus = bus_get(drv->bus); 173 struct bus_type *bus = bus_get(drv->bus);
174 struct device *dev; 174 struct device *dev;
175 int err = -ENODEV; 175 int err = -ENODEV;
176 176
177 dev = bus_find_device_by_name(bus, NULL, buf); 177 dev = bus_find_device_by_name(bus, NULL, buf);
178 if (dev && dev->driver == drv) { 178 if (dev && dev->driver == drv) {
179 if (dev->parent) /* Needed for USB */ 179 if (dev->parent) /* Needed for USB */
180 device_lock(dev->parent); 180 device_lock(dev->parent);
181 device_release_driver(dev); 181 device_release_driver(dev);
182 if (dev->parent) 182 if (dev->parent)
183 device_unlock(dev->parent); 183 device_unlock(dev->parent);
184 err = count; 184 err = count;
185 } 185 }
186 put_device(dev); 186 put_device(dev);
187 bus_put(bus); 187 bus_put(bus);
188 return err; 188 return err;
189 } 189 }
190 static DRIVER_ATTR(unbind, S_IWUSR, NULL, driver_unbind); 190 static DRIVER_ATTR(unbind, S_IWUSR, NULL, driver_unbind);
191 191
192 /* 192 /*
193 * Manually attach a device to a driver. 193 * Manually attach a device to a driver.
194 * Note: the driver must want to bind to the device, 194 * Note: the driver must want to bind to the device,
195 * it is not possible to override the driver's id table. 195 * it is not possible to override the driver's id table.
196 */ 196 */
197 static ssize_t driver_bind(struct device_driver *drv, 197 static ssize_t driver_bind(struct device_driver *drv,
198 const char *buf, size_t count) 198 const char *buf, size_t count)
199 { 199 {
200 struct bus_type *bus = bus_get(drv->bus); 200 struct bus_type *bus = bus_get(drv->bus);
201 struct device *dev; 201 struct device *dev;
202 int err = -ENODEV; 202 int err = -ENODEV;
203 203
204 dev = bus_find_device_by_name(bus, NULL, buf); 204 dev = bus_find_device_by_name(bus, NULL, buf);
205 if (dev && dev->driver == NULL && driver_match_device(drv, dev)) { 205 if (dev && dev->driver == NULL && driver_match_device(drv, dev)) {
206 if (dev->parent) /* Needed for USB */ 206 if (dev->parent) /* Needed for USB */
207 device_lock(dev->parent); 207 device_lock(dev->parent);
208 device_lock(dev); 208 device_lock(dev);
209 err = driver_probe_device(drv, dev); 209 err = driver_probe_device(drv, dev);
210 device_unlock(dev); 210 device_unlock(dev);
211 if (dev->parent) 211 if (dev->parent)
212 device_unlock(dev->parent); 212 device_unlock(dev->parent);
213 213
214 if (err > 0) { 214 if (err > 0) {
215 /* success */ 215 /* success */
216 err = count; 216 err = count;
217 } else if (err == 0) { 217 } else if (err == 0) {
218 /* driver didn't accept device */ 218 /* driver didn't accept device */
219 err = -ENODEV; 219 err = -ENODEV;
220 } 220 }
221 } 221 }
222 put_device(dev); 222 put_device(dev);
223 bus_put(bus); 223 bus_put(bus);
224 return err; 224 return err;
225 } 225 }
226 static DRIVER_ATTR(bind, S_IWUSR, NULL, driver_bind); 226 static DRIVER_ATTR(bind, S_IWUSR, NULL, driver_bind);
227 227
228 static ssize_t show_drivers_autoprobe(struct bus_type *bus, char *buf) 228 static ssize_t show_drivers_autoprobe(struct bus_type *bus, char *buf)
229 { 229 {
230 return sprintf(buf, "%d\n", bus->p->drivers_autoprobe); 230 return sprintf(buf, "%d\n", bus->p->drivers_autoprobe);
231 } 231 }
232 232
233 static ssize_t store_drivers_autoprobe(struct bus_type *bus, 233 static ssize_t store_drivers_autoprobe(struct bus_type *bus,
234 const char *buf, size_t count) 234 const char *buf, size_t count)
235 { 235 {
236 if (buf[0] == '0') 236 if (buf[0] == '0')
237 bus->p->drivers_autoprobe = 0; 237 bus->p->drivers_autoprobe = 0;
238 else 238 else
239 bus->p->drivers_autoprobe = 1; 239 bus->p->drivers_autoprobe = 1;
240 return count; 240 return count;
241 } 241 }
242 242
243 static ssize_t store_drivers_probe(struct bus_type *bus, 243 static ssize_t store_drivers_probe(struct bus_type *bus,
244 const char *buf, size_t count) 244 const char *buf, size_t count)
245 { 245 {
246 struct device *dev; 246 struct device *dev;
247 247
248 dev = bus_find_device_by_name(bus, NULL, buf); 248 dev = bus_find_device_by_name(bus, NULL, buf);
249 if (!dev) 249 if (!dev)
250 return -ENODEV; 250 return -ENODEV;
251 if (bus_rescan_devices_helper(dev, NULL) != 0) 251 if (bus_rescan_devices_helper(dev, NULL) != 0)
252 return -EINVAL; 252 return -EINVAL;
253 return count; 253 return count;
254 } 254 }
255 #endif 255 #endif
256 256
257 static struct device *next_device(struct klist_iter *i) 257 static struct device *next_device(struct klist_iter *i)
258 { 258 {
259 struct klist_node *n = klist_next(i); 259 struct klist_node *n = klist_next(i);
260 struct device *dev = NULL; 260 struct device *dev = NULL;
261 struct device_private *dev_prv; 261 struct device_private *dev_prv;
262 262
263 if (n) { 263 if (n) {
264 dev_prv = to_device_private_bus(n); 264 dev_prv = to_device_private_bus(n);
265 dev = dev_prv->device; 265 dev = dev_prv->device;
266 } 266 }
267 return dev; 267 return dev;
268 } 268 }
269 269
270 /** 270 /**
271 * bus_for_each_dev - device iterator. 271 * bus_for_each_dev - device iterator.
272 * @bus: bus type. 272 * @bus: bus type.
273 * @start: device to start iterating from. 273 * @start: device to start iterating from.
274 * @data: data for the callback. 274 * @data: data for the callback.
275 * @fn: function to be called for each device. 275 * @fn: function to be called for each device.
276 * 276 *
277 * Iterate over @bus's list of devices, and call @fn for each, 277 * Iterate over @bus's list of devices, and call @fn for each,
278 * passing it @data. If @start is not NULL, we use that device to 278 * passing it @data. If @start is not NULL, we use that device to
279 * begin iterating from. 279 * begin iterating from.
280 * 280 *
281 * We check the return of @fn each time. If it returns anything 281 * We check the return of @fn each time. If it returns anything
282 * other than 0, we break out and return that value. 282 * other than 0, we break out and return that value.
283 * 283 *
284 * NOTE: The device that returns a non-zero value is not retained 284 * NOTE: The device that returns a non-zero value is not retained
285 * in any way, nor is its refcount incremented. If the caller needs 285 * in any way, nor is its refcount incremented. If the caller needs
286 * to retain this data, it should do so, and increment the reference 286 * to retain this data, it should do so, and increment the reference
287 * count in the supplied callback. 287 * count in the supplied callback.
288 */ 288 */
289 int bus_for_each_dev(struct bus_type *bus, struct device *start, 289 int bus_for_each_dev(struct bus_type *bus, struct device *start,
290 void *data, int (*fn)(struct device *, void *)) 290 void *data, int (*fn)(struct device *, void *))
291 { 291 {
292 struct klist_iter i; 292 struct klist_iter i;
293 struct device *dev; 293 struct device *dev;
294 int error = 0; 294 int error = 0;
295 295
296 if (!bus) 296 if (!bus)
297 return -EINVAL; 297 return -EINVAL;
298 298
299 klist_iter_init_node(&bus->p->klist_devices, &i, 299 error = klist_iter_init_node(&bus->p->klist_devices, &i,
300 (start ? &start->p->knode_bus : NULL)); 300 (start ? &start->p->knode_bus : NULL));
301 while ((dev = next_device(&i)) && !error) 301 if (!error) {
302 error = fn(dev, data); 302 while ((dev = next_device(&i)) && !error)
303 klist_iter_exit(&i); 303 error = fn(dev, data);
304 klist_iter_exit(&i);
305 }
304 return error; 306 return error;
305 } 307 }
306 EXPORT_SYMBOL_GPL(bus_for_each_dev); 308 EXPORT_SYMBOL_GPL(bus_for_each_dev);
307 309
308 /** 310 /**
309 * bus_find_device - device iterator for locating a particular device. 311 * bus_find_device - device iterator for locating a particular device.
310 * @bus: bus type 312 * @bus: bus type
311 * @start: Device to begin with 313 * @start: Device to begin with
312 * @data: Data to pass to match function 314 * @data: Data to pass to match function
313 * @match: Callback function to check device 315 * @match: Callback function to check device
314 * 316 *
315 * This is similar to the bus_for_each_dev() function above, but it 317 * This is similar to the bus_for_each_dev() function above, but it
316 * returns a reference to a device that is 'found' for later use, as 318 * returns a reference to a device that is 'found' for later use, as
317 * determined by the @match callback. 319 * determined by the @match callback.
318 * 320 *
319 * The callback should return 0 if the device doesn't match and non-zero 321 * The callback should return 0 if the device doesn't match and non-zero
320 * if it does. If the callback returns non-zero, this function will 322 * if it does. If the callback returns non-zero, this function will
321 * return to the caller and not iterate over any more devices. 323 * return to the caller and not iterate over any more devices.
322 */ 324 */
323 struct device *bus_find_device(struct bus_type *bus, 325 struct device *bus_find_device(struct bus_type *bus,
324 struct device *start, void *data, 326 struct device *start, void *data,
325 int (*match)(struct device *dev, void *data)) 327 int (*match)(struct device *dev, void *data))
326 { 328 {
327 struct klist_iter i; 329 struct klist_iter i;
328 struct device *dev; 330 struct device *dev;
329 331
330 if (!bus) 332 if (!bus)
331 return NULL; 333 return NULL;
332 334
333 klist_iter_init_node(&bus->p->klist_devices, &i, 335 if (klist_iter_init_node(&bus->p->klist_devices, &i,
334 (start ? &start->p->knode_bus : NULL)); 336 (start ? &start->p->knode_bus : NULL)) < 0)
337 return NULL;
338
335 while ((dev = next_device(&i))) 339 while ((dev = next_device(&i)))
336 if (match(dev, data) && get_device(dev)) 340 if (match(dev, data) && get_device(dev))
337 break; 341 break;
338 klist_iter_exit(&i); 342 klist_iter_exit(&i);
339 return dev; 343 return dev;
340 } 344 }
341 EXPORT_SYMBOL_GPL(bus_find_device); 345 EXPORT_SYMBOL_GPL(bus_find_device);
342 346
343 static int match_name(struct device *dev, void *data) 347 static int match_name(struct device *dev, void *data)
344 { 348 {
345 const char *name = data; 349 const char *name = data;
346 350
347 return sysfs_streq(name, dev_name(dev)); 351 return sysfs_streq(name, dev_name(dev));
348 } 352 }
349 353
350 /** 354 /**
351 * bus_find_device_by_name - device iterator for locating a particular device of a specific name 355 * bus_find_device_by_name - device iterator for locating a particular device of a specific name
352 * @bus: bus type 356 * @bus: bus type
353 * @start: Device to begin with 357 * @start: Device to begin with
354 * @name: name of the device to match 358 * @name: name of the device to match
355 * 359 *
356 * This is similar to the bus_find_device() function above, but it handles 360 * This is similar to the bus_find_device() function above, but it handles
357 * searching by a name automatically, no need to write another strcmp matching 361 * searching by a name automatically, no need to write another strcmp matching
358 * function. 362 * function.
359 */ 363 */
360 struct device *bus_find_device_by_name(struct bus_type *bus, 364 struct device *bus_find_device_by_name(struct bus_type *bus,
361 struct device *start, const char *name) 365 struct device *start, const char *name)
362 { 366 {
363 return bus_find_device(bus, start, (void *)name, match_name); 367 return bus_find_device(bus, start, (void *)name, match_name);
364 } 368 }
365 EXPORT_SYMBOL_GPL(bus_find_device_by_name); 369 EXPORT_SYMBOL_GPL(bus_find_device_by_name);
366 370
367 /** 371 /**
368 * subsys_find_device_by_id - find a device with a specific enumeration number 372 * subsys_find_device_by_id - find a device with a specific enumeration number
369 * @subsys: subsystem 373 * @subsys: subsystem
370 * @id: index 'id' in struct device 374 * @id: index 'id' in struct device
371 * @hint: device to check first 375 * @hint: device to check first
372 * 376 *
373 * Check the hint's next object and if it is a match return it directly, 377 * Check the hint's next object and if it is a match return it directly,
374 * otherwise, fall back to a full list search. Either way a reference for 378 * otherwise, fall back to a full list search. Either way a reference for
375 * the returned object is taken. 379 * the returned object is taken.
376 */ 380 */
377 struct device *subsys_find_device_by_id(struct bus_type *subsys, unsigned int id, 381 struct device *subsys_find_device_by_id(struct bus_type *subsys, unsigned int id,
378 struct device *hint) 382 struct device *hint)
379 { 383 {
380 struct klist_iter i; 384 struct klist_iter i;
381 struct device *dev; 385 struct device *dev;
382 386
383 if (!subsys) 387 if (!subsys)
384 return NULL; 388 return NULL;
385 389
386 if (hint) { 390 if (hint) {
387 klist_iter_init_node(&subsys->p->klist_devices, &i, &hint->p->knode_bus); 391 if (klist_iter_init_node(&subsys->p->klist_devices, &i,
392 &hint->p->knode_bus) < 0)
393 return NULL;
388 dev = next_device(&i); 394 dev = next_device(&i);
389 if (dev && dev->id == id && get_device(dev)) { 395 if (dev && dev->id == id && get_device(dev)) {
390 klist_iter_exit(&i); 396 klist_iter_exit(&i);
391 return dev; 397 return dev;
392 } 398 }
393 klist_iter_exit(&i); 399 klist_iter_exit(&i);
394 } 400 }
395 401
396 klist_iter_init_node(&subsys->p->klist_devices, &i, NULL); 402 klist_iter_init_node(&subsys->p->klist_devices, &i, NULL);
397 while ((dev = next_device(&i))) { 403 while ((dev = next_device(&i))) {
398 if (dev->id == id && get_device(dev)) { 404 if (dev->id == id && get_device(dev)) {
399 klist_iter_exit(&i); 405 klist_iter_exit(&i);
400 return dev; 406 return dev;
401 } 407 }
402 } 408 }
403 klist_iter_exit(&i); 409 klist_iter_exit(&i);
404 return NULL; 410 return NULL;
405 } 411 }
406 EXPORT_SYMBOL_GPL(subsys_find_device_by_id); 412 EXPORT_SYMBOL_GPL(subsys_find_device_by_id);
407 413
408 static struct device_driver *next_driver(struct klist_iter *i) 414 static struct device_driver *next_driver(struct klist_iter *i)
409 { 415 {
410 struct klist_node *n = klist_next(i); 416 struct klist_node *n = klist_next(i);
411 struct driver_private *drv_priv; 417 struct driver_private *drv_priv;
412 418
413 if (n) { 419 if (n) {
414 drv_priv = container_of(n, struct driver_private, knode_bus); 420 drv_priv = container_of(n, struct driver_private, knode_bus);
415 return drv_priv->driver; 421 return drv_priv->driver;
416 } 422 }
417 return NULL; 423 return NULL;
418 } 424 }
419 425
420 /** 426 /**
421 * bus_for_each_drv - driver iterator 427 * bus_for_each_drv - driver iterator
422 * @bus: bus we're dealing with. 428 * @bus: bus we're dealing with.
423 * @start: driver to start iterating on. 429 * @start: driver to start iterating on.
424 * @data: data to pass to the callback. 430 * @data: data to pass to the callback.
425 * @fn: function to call for each driver. 431 * @fn: function to call for each driver.
426 * 432 *
427 * This is nearly identical to the device iterator above. 433 * This is nearly identical to the device iterator above.
428 * We iterate over each driver that belongs to @bus, and call 434 * We iterate over each driver that belongs to @bus, and call
429 * @fn for each. If @fn returns anything but 0, we break out 435 * @fn for each. If @fn returns anything but 0, we break out
430 * and return it. If @start is not NULL, we use it as the head 436 * and return it. If @start is not NULL, we use it as the head
431 * of the list. 437 * of the list.
432 * 438 *
433 * NOTE: we don't return the driver that returns a non-zero 439 * NOTE: we don't return the driver that returns a non-zero
434 * value, nor do we leave the reference count incremented for that 440 * value, nor do we leave the reference count incremented for that
435 * driver. If the caller needs to know that info, it must set it 441 * driver. If the caller needs to know that info, it must set it
436 * in the callback. It must also be sure to increment the refcount 442 * in the callback. It must also be sure to increment the refcount
437 * so it doesn't disappear before returning to the caller. 443 * so it doesn't disappear before returning to the caller.
438 */ 444 */
439 int bus_for_each_drv(struct bus_type *bus, struct device_driver *start, 445 int bus_for_each_drv(struct bus_type *bus, struct device_driver *start,
440 void *data, int (*fn)(struct device_driver *, void *)) 446 void *data, int (*fn)(struct device_driver *, void *))
441 { 447 {
442 struct klist_iter i; 448 struct klist_iter i;
443 struct device_driver *drv; 449 struct device_driver *drv;
444 int error = 0; 450 int error = 0;
445 451
446 if (!bus) 452 if (!bus)
447 return -EINVAL; 453 return -EINVAL;
448 454
449 klist_iter_init_node(&bus->p->klist_drivers, &i, 455 error = klist_iter_init_node(&bus->p->klist_drivers, &i,
450 start ? &start->p->knode_bus : NULL); 456 start ? &start->p->knode_bus : NULL);
451 while ((drv = next_driver(&i)) && !error) 457 if (!error) {
452 error = fn(drv, data); 458 while ((drv = next_driver(&i)) && !error)
453 klist_iter_exit(&i); 459 error = fn(drv, data);
460 klist_iter_exit(&i);
461 }
454 return error; 462 return error;
455 } 463 }
456 EXPORT_SYMBOL_GPL(bus_for_each_drv); 464 EXPORT_SYMBOL_GPL(bus_for_each_drv);
457 465
458 static int device_add_attrs(struct bus_type *bus, struct device *dev) 466 static int device_add_attrs(struct bus_type *bus, struct device *dev)
459 { 467 {
460 int error = 0; 468 int error = 0;
461 int i; 469 int i;
462 470
463 if (!bus->dev_attrs) 471 if (!bus->dev_attrs)
464 return 0; 472 return 0;
465 473
466 for (i = 0; attr_name(bus->dev_attrs[i]); i++) { 474 for (i = 0; attr_name(bus->dev_attrs[i]); i++) {
467 error = device_create_file(dev, &bus->dev_attrs[i]); 475 error = device_create_file(dev, &bus->dev_attrs[i]);
468 if (error) { 476 if (error) {
469 while (--i >= 0) 477 while (--i >= 0)
470 device_remove_file(dev, &bus->dev_attrs[i]); 478 device_remove_file(dev, &bus->dev_attrs[i]);
471 break; 479 break;
472 } 480 }
473 } 481 }
474 return error; 482 return error;
475 } 483 }
476 484
477 static void device_remove_attrs(struct bus_type *bus, struct device *dev) 485 static void device_remove_attrs(struct bus_type *bus, struct device *dev)
478 { 486 {
479 int i; 487 int i;
480 488
481 if (bus->dev_attrs) { 489 if (bus->dev_attrs) {
482 for (i = 0; attr_name(bus->dev_attrs[i]); i++) 490 for (i = 0; attr_name(bus->dev_attrs[i]); i++)
483 device_remove_file(dev, &bus->dev_attrs[i]); 491 device_remove_file(dev, &bus->dev_attrs[i]);
484 } 492 }
485 } 493 }
486 494
487 /** 495 /**
488 * bus_add_device - add device to bus 496 * bus_add_device - add device to bus
489 * @dev: device being added 497 * @dev: device being added
490 * 498 *
491 * - Add device's bus attributes. 499 * - Add device's bus attributes.
492 * - Create links to device's bus. 500 * - Create links to device's bus.
493 * - Add the device to its bus's list of devices. 501 * - Add the device to its bus's list of devices.
494 */ 502 */
495 int bus_add_device(struct device *dev) 503 int bus_add_device(struct device *dev)
496 { 504 {
497 struct bus_type *bus = bus_get(dev->bus); 505 struct bus_type *bus = bus_get(dev->bus);
498 int error = 0; 506 int error = 0;
499 507
500 if (bus) { 508 if (bus) {
501 pr_debug("bus: '%s': add device %s\n", bus->name, dev_name(dev)); 509 pr_debug("bus: '%s': add device %s\n", bus->name, dev_name(dev));
502 error = device_add_attrs(bus, dev); 510 error = device_add_attrs(bus, dev);
503 if (error) 511 if (error)
504 goto out_put; 512 goto out_put;
505 error = sysfs_create_link(&bus->p->devices_kset->kobj, 513 error = sysfs_create_link(&bus->p->devices_kset->kobj,
506 &dev->kobj, dev_name(dev)); 514 &dev->kobj, dev_name(dev));
507 if (error) 515 if (error)
508 goto out_id; 516 goto out_id;
509 error = sysfs_create_link(&dev->kobj, 517 error = sysfs_create_link(&dev->kobj,
510 &dev->bus->p->subsys.kobj, "subsystem"); 518 &dev->bus->p->subsys.kobj, "subsystem");
511 if (error) 519 if (error)
512 goto out_subsys; 520 goto out_subsys;
513 klist_add_tail(&dev->p->knode_bus, &bus->p->klist_devices); 521 klist_add_tail(&dev->p->knode_bus, &bus->p->klist_devices);
514 } 522 }
515 return 0; 523 return 0;
516 524
517 out_subsys: 525 out_subsys:
518 sysfs_remove_link(&bus->p->devices_kset->kobj, dev_name(dev)); 526 sysfs_remove_link(&bus->p->devices_kset->kobj, dev_name(dev));
519 out_id: 527 out_id:
520 device_remove_attrs(bus, dev); 528 device_remove_attrs(bus, dev);
521 out_put: 529 out_put:
522 bus_put(dev->bus); 530 bus_put(dev->bus);
523 return error; 531 return error;
524 } 532 }
525 533
526 /** 534 /**
527 * bus_probe_device - probe drivers for a new device 535 * bus_probe_device - probe drivers for a new device
528 * @dev: device to probe 536 * @dev: device to probe
529 * 537 *
530 * - Automatically probe for a driver if the bus allows it. 538 * - Automatically probe for a driver if the bus allows it.
531 */ 539 */
532 void bus_probe_device(struct device *dev) 540 void bus_probe_device(struct device *dev)
533 { 541 {
534 struct bus_type *bus = dev->bus; 542 struct bus_type *bus = dev->bus;
535 struct subsys_interface *sif; 543 struct subsys_interface *sif;
536 int ret; 544 int ret;
537 545
538 if (!bus) 546 if (!bus)
539 return; 547 return;
540 548
541 if (bus->p->drivers_autoprobe) { 549 if (bus->p->drivers_autoprobe) {
542 ret = device_attach(dev); 550 ret = device_attach(dev);
543 WARN_ON(ret < 0); 551 WARN_ON(ret < 0);
544 } 552 }
545 553
546 mutex_lock(&bus->p->mutex); 554 mutex_lock(&bus->p->mutex);
547 list_for_each_entry(sif, &bus->p->interfaces, node) 555 list_for_each_entry(sif, &bus->p->interfaces, node)
548 if (sif->add_dev) 556 if (sif->add_dev)
549 sif->add_dev(dev, sif); 557 sif->add_dev(dev, sif);
550 mutex_unlock(&bus->p->mutex); 558 mutex_unlock(&bus->p->mutex);
551 } 559 }
552 560
553 /** 561 /**
554 * bus_remove_device - remove device from bus 562 * bus_remove_device - remove device from bus
555 * @dev: device to be removed 563 * @dev: device to be removed
556 * 564 *
557 * - Remove device from all interfaces. 565 * - Remove device from all interfaces.
558 * - Remove symlink from bus' directory. 566 * - Remove symlink from bus' directory.
559 * - Delete device from bus's list. 567 * - Delete device from bus's list.
560 * - Detach from its driver. 568 * - Detach from its driver.
561 * - Drop reference taken in bus_add_device(). 569 * - Drop reference taken in bus_add_device().
562 */ 570 */
563 void bus_remove_device(struct device *dev) 571 void bus_remove_device(struct device *dev)
564 { 572 {
565 struct bus_type *bus = dev->bus; 573 struct bus_type *bus = dev->bus;
566 struct subsys_interface *sif; 574 struct subsys_interface *sif;
567 575
568 if (!bus) 576 if (!bus)
569 return; 577 return;
570 578
571 mutex_lock(&bus->p->mutex); 579 mutex_lock(&bus->p->mutex);
572 list_for_each_entry(sif, &bus->p->interfaces, node) 580 list_for_each_entry(sif, &bus->p->interfaces, node)
573 if (sif->remove_dev) 581 if (sif->remove_dev)
574 sif->remove_dev(dev, sif); 582 sif->remove_dev(dev, sif);
575 mutex_unlock(&bus->p->mutex); 583 mutex_unlock(&bus->p->mutex);
576 584
577 sysfs_remove_link(&dev->kobj, "subsystem"); 585 sysfs_remove_link(&dev->kobj, "subsystem");
578 sysfs_remove_link(&dev->bus->p->devices_kset->kobj, 586 sysfs_remove_link(&dev->bus->p->devices_kset->kobj,
579 dev_name(dev)); 587 dev_name(dev));
580 device_remove_attrs(dev->bus, dev); 588 device_remove_attrs(dev->bus, dev);
581 if (klist_node_attached(&dev->p->knode_bus)) 589 if (klist_node_attached(&dev->p->knode_bus))
582 klist_del(&dev->p->knode_bus); 590 klist_del(&dev->p->knode_bus);
583 591
584 pr_debug("bus: '%s': remove device %s\n", 592 pr_debug("bus: '%s': remove device %s\n",
585 dev->bus->name, dev_name(dev)); 593 dev->bus->name, dev_name(dev));
586 device_release_driver(dev); 594 device_release_driver(dev);
587 bus_put(dev->bus); 595 bus_put(dev->bus);
588 } 596 }
589 597
590 static int driver_add_attrs(struct bus_type *bus, struct device_driver *drv) 598 static int driver_add_attrs(struct bus_type *bus, struct device_driver *drv)
591 { 599 {
592 int error = 0; 600 int error = 0;
593 int i; 601 int i;
594 602
595 if (bus->drv_attrs) { 603 if (bus->drv_attrs) {
596 for (i = 0; attr_name(bus->drv_attrs[i]); i++) { 604 for (i = 0; attr_name(bus->drv_attrs[i]); i++) {
597 error = driver_create_file(drv, &bus->drv_attrs[i]); 605 error = driver_create_file(drv, &bus->drv_attrs[i]);
598 if (error) 606 if (error)
599 goto err; 607 goto err;
600 } 608 }
601 } 609 }
602 done: 610 done:
603 return error; 611 return error;
604 err: 612 err:
605 while (--i >= 0) 613 while (--i >= 0)
606 driver_remove_file(drv, &bus->drv_attrs[i]); 614 driver_remove_file(drv, &bus->drv_attrs[i]);
607 goto done; 615 goto done;
608 } 616 }
609 617
610 static void driver_remove_attrs(struct bus_type *bus, 618 static void driver_remove_attrs(struct bus_type *bus,
611 struct device_driver *drv) 619 struct device_driver *drv)
612 { 620 {
613 int i; 621 int i;
614 622
615 if (bus->drv_attrs) { 623 if (bus->drv_attrs) {
616 for (i = 0; attr_name(bus->drv_attrs[i]); i++) 624 for (i = 0; attr_name(bus->drv_attrs[i]); i++)
617 driver_remove_file(drv, &bus->drv_attrs[i]); 625 driver_remove_file(drv, &bus->drv_attrs[i]);
618 } 626 }
619 } 627 }
620 628
621 #ifdef CONFIG_HOTPLUG 629 #ifdef CONFIG_HOTPLUG
622 /* 630 /*
623 * Thanks to drivers making their tables __devinit, we can't allow manual 631 * Thanks to drivers making their tables __devinit, we can't allow manual
624 * bind and unbind from userspace unless CONFIG_HOTPLUG is enabled. 632 * bind and unbind from userspace unless CONFIG_HOTPLUG is enabled.
625 */ 633 */
626 static int __must_check add_bind_files(struct device_driver *drv) 634 static int __must_check add_bind_files(struct device_driver *drv)
627 { 635 {
628 int ret; 636 int ret;
629 637
630 ret = driver_create_file(drv, &driver_attr_unbind); 638 ret = driver_create_file(drv, &driver_attr_unbind);
631 if (ret == 0) { 639 if (ret == 0) {
632 ret = driver_create_file(drv, &driver_attr_bind); 640 ret = driver_create_file(drv, &driver_attr_bind);
633 if (ret) 641 if (ret)
634 driver_remove_file(drv, &driver_attr_unbind); 642 driver_remove_file(drv, &driver_attr_unbind);
635 } 643 }
636 return ret; 644 return ret;
637 } 645 }
638 646
639 static void remove_bind_files(struct device_driver *drv) 647 static void remove_bind_files(struct device_driver *drv)
640 { 648 {
641 driver_remove_file(drv, &driver_attr_bind); 649 driver_remove_file(drv, &driver_attr_bind);
642 driver_remove_file(drv, &driver_attr_unbind); 650 driver_remove_file(drv, &driver_attr_unbind);
643 } 651 }
644 652
645 static BUS_ATTR(drivers_probe, S_IWUSR, NULL, store_drivers_probe); 653 static BUS_ATTR(drivers_probe, S_IWUSR, NULL, store_drivers_probe);
646 static BUS_ATTR(drivers_autoprobe, S_IWUSR | S_IRUGO, 654 static BUS_ATTR(drivers_autoprobe, S_IWUSR | S_IRUGO,
647 show_drivers_autoprobe, store_drivers_autoprobe); 655 show_drivers_autoprobe, store_drivers_autoprobe);
648 656
649 static int add_probe_files(struct bus_type *bus) 657 static int add_probe_files(struct bus_type *bus)
650 { 658 {
651 int retval; 659 int retval;
652 660
653 retval = bus_create_file(bus, &bus_attr_drivers_probe); 661 retval = bus_create_file(bus, &bus_attr_drivers_probe);
654 if (retval) 662 if (retval)
655 goto out; 663 goto out;
656 664
657 retval = bus_create_file(bus, &bus_attr_drivers_autoprobe); 665 retval = bus_create_file(bus, &bus_attr_drivers_autoprobe);
658 if (retval) 666 if (retval)
659 bus_remove_file(bus, &bus_attr_drivers_probe); 667 bus_remove_file(bus, &bus_attr_drivers_probe);
660 out: 668 out:
661 return retval; 669 return retval;
662 } 670 }
663 671
664 static void remove_probe_files(struct bus_type *bus) 672 static void remove_probe_files(struct bus_type *bus)
665 { 673 {
666 bus_remove_file(bus, &bus_attr_drivers_autoprobe); 674 bus_remove_file(bus, &bus_attr_drivers_autoprobe);
667 bus_remove_file(bus, &bus_attr_drivers_probe); 675 bus_remove_file(bus, &bus_attr_drivers_probe);
668 } 676 }
669 #else 677 #else
670 static inline int add_bind_files(struct device_driver *drv) { return 0; } 678 static inline int add_bind_files(struct device_driver *drv) { return 0; }
671 static inline void remove_bind_files(struct device_driver *drv) {} 679 static inline void remove_bind_files(struct device_driver *drv) {}
672 static inline int add_probe_files(struct bus_type *bus) { return 0; } 680 static inline int add_probe_files(struct bus_type *bus) { return 0; }
673 static inline void remove_probe_files(struct bus_type *bus) {} 681 static inline void remove_probe_files(struct bus_type *bus) {}
674 #endif 682 #endif
675 683
676 static ssize_t driver_uevent_store(struct device_driver *drv, 684 static ssize_t driver_uevent_store(struct device_driver *drv,
677 const char *buf, size_t count) 685 const char *buf, size_t count)
678 { 686 {
679 enum kobject_action action; 687 enum kobject_action action;
680 688
681 if (kobject_action_type(buf, count, &action) == 0) 689 if (kobject_action_type(buf, count, &action) == 0)
682 kobject_uevent(&drv->p->kobj, action); 690 kobject_uevent(&drv->p->kobj, action);
683 return count; 691 return count;
684 } 692 }
685 static DRIVER_ATTR(uevent, S_IWUSR, NULL, driver_uevent_store); 693 static DRIVER_ATTR(uevent, S_IWUSR, NULL, driver_uevent_store);
686 694
687 /** 695 /**
688 * bus_add_driver - Add a driver to the bus. 696 * bus_add_driver - Add a driver to the bus.
689 * @drv: driver. 697 * @drv: driver.
690 */ 698 */
691 int bus_add_driver(struct device_driver *drv) 699 int bus_add_driver(struct device_driver *drv)
692 { 700 {
693 struct bus_type *bus; 701 struct bus_type *bus;
694 struct driver_private *priv; 702 struct driver_private *priv;
695 int error = 0; 703 int error = 0;
696 704
697 bus = bus_get(drv->bus); 705 bus = bus_get(drv->bus);
698 if (!bus) 706 if (!bus)
699 return -EINVAL; 707 return -EINVAL;
700 708
701 pr_debug("bus: '%s': add driver %s\n", bus->name, drv->name); 709 pr_debug("bus: '%s': add driver %s\n", bus->name, drv->name);
702 710
703 priv = kzalloc(sizeof(*priv), GFP_KERNEL); 711 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
704 if (!priv) { 712 if (!priv) {
705 error = -ENOMEM; 713 error = -ENOMEM;
706 goto out_put_bus; 714 goto out_put_bus;
707 } 715 }
708 klist_init(&priv->klist_devices, NULL, NULL); 716 klist_init(&priv->klist_devices, NULL, NULL);
709 priv->driver = drv; 717 priv->driver = drv;
710 drv->p = priv; 718 drv->p = priv;
711 priv->kobj.kset = bus->p->drivers_kset; 719 priv->kobj.kset = bus->p->drivers_kset;
712 error = kobject_init_and_add(&priv->kobj, &driver_ktype, NULL, 720 error = kobject_init_and_add(&priv->kobj, &driver_ktype, NULL,
713 "%s", drv->name); 721 "%s", drv->name);
714 if (error) 722 if (error)
715 goto out_unregister; 723 goto out_unregister;
716 724
717 if (drv->bus->p->drivers_autoprobe) { 725 if (drv->bus->p->drivers_autoprobe) {
718 error = driver_attach(drv); 726 error = driver_attach(drv);
719 if (error) 727 if (error)
720 goto out_unregister; 728 goto out_unregister;
721 } 729 }
722 klist_add_tail(&priv->knode_bus, &bus->p->klist_drivers); 730 klist_add_tail(&priv->knode_bus, &bus->p->klist_drivers);
723 module_add_driver(drv->owner, drv); 731 module_add_driver(drv->owner, drv);
724 732
725 error = driver_create_file(drv, &driver_attr_uevent); 733 error = driver_create_file(drv, &driver_attr_uevent);
726 if (error) { 734 if (error) {
727 printk(KERN_ERR "%s: uevent attr (%s) failed\n", 735 printk(KERN_ERR "%s: uevent attr (%s) failed\n",
728 __func__, drv->name); 736 __func__, drv->name);
729 } 737 }
730 error = driver_add_attrs(bus, drv); 738 error = driver_add_attrs(bus, drv);
731 if (error) { 739 if (error) {
732 /* How the hell do we get out of this pickle? Give up */ 740 /* How the hell do we get out of this pickle? Give up */
733 printk(KERN_ERR "%s: driver_add_attrs(%s) failed\n", 741 printk(KERN_ERR "%s: driver_add_attrs(%s) failed\n",
734 __func__, drv->name); 742 __func__, drv->name);
735 } 743 }
736 744
737 if (!drv->suppress_bind_attrs) { 745 if (!drv->suppress_bind_attrs) {
738 error = add_bind_files(drv); 746 error = add_bind_files(drv);
739 if (error) { 747 if (error) {
740 /* Ditto */ 748 /* Ditto */
741 printk(KERN_ERR "%s: add_bind_files(%s) failed\n", 749 printk(KERN_ERR "%s: add_bind_files(%s) failed\n",
742 __func__, drv->name); 750 __func__, drv->name);
743 } 751 }
744 } 752 }
745 753
746 kobject_uevent(&priv->kobj, KOBJ_ADD); 754 kobject_uevent(&priv->kobj, KOBJ_ADD);
747 return 0; 755 return 0;
748 756
749 out_unregister: 757 out_unregister:
750 kobject_put(&priv->kobj); 758 kobject_put(&priv->kobj);
751 kfree(drv->p); 759 kfree(drv->p);
752 drv->p = NULL; 760 drv->p = NULL;
753 out_put_bus: 761 out_put_bus:
754 bus_put(bus); 762 bus_put(bus);
755 return error; 763 return error;
756 } 764 }
757 765
758 /** 766 /**
759 * bus_remove_driver - delete driver from bus's knowledge. 767 * bus_remove_driver - delete driver from bus's knowledge.
760 * @drv: driver. 768 * @drv: driver.
761 * 769 *
762 * Detach the driver from the devices it controls, and remove 770 * Detach the driver from the devices it controls, and remove
763 * it from its bus's list of drivers. Finally, we drop the reference 771 * it from its bus's list of drivers. Finally, we drop the reference
764 * to the bus we took in bus_add_driver(). 772 * to the bus we took in bus_add_driver().
765 */ 773 */
766 void bus_remove_driver(struct device_driver *drv) 774 void bus_remove_driver(struct device_driver *drv)
767 { 775 {
768 if (!drv->bus) 776 if (!drv->bus)
769 return; 777 return;
770 778
771 if (!drv->suppress_bind_attrs) 779 if (!drv->suppress_bind_attrs)
772 remove_bind_files(drv); 780 remove_bind_files(drv);
773 driver_remove_attrs(drv->bus, drv); 781 driver_remove_attrs(drv->bus, drv);
774 driver_remove_file(drv, &driver_attr_uevent); 782 driver_remove_file(drv, &driver_attr_uevent);
775 klist_remove(&drv->p->knode_bus); 783 klist_remove(&drv->p->knode_bus);
776 pr_debug("bus: '%s': remove driver %s\n", drv->bus->name, drv->name); 784 pr_debug("bus: '%s': remove driver %s\n", drv->bus->name, drv->name);
777 driver_detach(drv); 785 driver_detach(drv);
778 module_remove_driver(drv); 786 module_remove_driver(drv);
779 kobject_put(&drv->p->kobj); 787 kobject_put(&drv->p->kobj);
780 bus_put(drv->bus); 788 bus_put(drv->bus);
781 } 789 }
782 790
783 /* Helper for bus_rescan_devices's iter */ 791 /* Helper for bus_rescan_devices's iter */
784 static int __must_check bus_rescan_devices_helper(struct device *dev, 792 static int __must_check bus_rescan_devices_helper(struct device *dev,
785 void *data) 793 void *data)
786 { 794 {
787 int ret = 0; 795 int ret = 0;
788 796
789 if (!dev->driver) { 797 if (!dev->driver) {
790 if (dev->parent) /* Needed for USB */ 798 if (dev->parent) /* Needed for USB */
791 device_lock(dev->parent); 799 device_lock(dev->parent);
792 ret = device_attach(dev); 800 ret = device_attach(dev);
793 if (dev->parent) 801 if (dev->parent)
794 device_unlock(dev->parent); 802 device_unlock(dev->parent);
795 } 803 }
796 return ret < 0 ? ret : 0; 804 return ret < 0 ? ret : 0;
797 } 805 }
798 806
799 /** 807 /**
800 * bus_rescan_devices - rescan devices on the bus for possible drivers 808 * bus_rescan_devices - rescan devices on the bus for possible drivers
801 * @bus: the bus to scan. 809 * @bus: the bus to scan.
802 * 810 *
803 * This function will look for devices on the bus with no driver 811 * This function will look for devices on the bus with no driver
804 * attached and rescan it against existing drivers to see if it matches 812 * attached and rescan it against existing drivers to see if it matches
805 * any by calling device_attach() for the unbound devices. 813 * any by calling device_attach() for the unbound devices.
806 */ 814 */
807 int bus_rescan_devices(struct bus_type *bus) 815 int bus_rescan_devices(struct bus_type *bus)
808 { 816 {
809 return bus_for_each_dev(bus, NULL, NULL, bus_rescan_devices_helper); 817 return bus_for_each_dev(bus, NULL, NULL, bus_rescan_devices_helper);
810 } 818 }
811 EXPORT_SYMBOL_GPL(bus_rescan_devices); 819 EXPORT_SYMBOL_GPL(bus_rescan_devices);
812 820
813 /** 821 /**
814 * device_reprobe - remove driver for a device and probe for a new driver 822 * device_reprobe - remove driver for a device and probe for a new driver
815 * @dev: the device to reprobe 823 * @dev: the device to reprobe
816 * 824 *
817 * This function detaches the attached driver (if any) for the given 825 * This function detaches the attached driver (if any) for the given
818 * device and restarts the driver probing process. It is intended 826 * device and restarts the driver probing process. It is intended
819 * to use if probing criteria changed during a devices lifetime and 827 * to use if probing criteria changed during a devices lifetime and
820 * driver attachment should change accordingly. 828 * driver attachment should change accordingly.
821 */ 829 */
822 int device_reprobe(struct device *dev) 830 int device_reprobe(struct device *dev)
823 { 831 {
824 if (dev->driver) { 832 if (dev->driver) {
825 if (dev->parent) /* Needed for USB */ 833 if (dev->parent) /* Needed for USB */
826 device_lock(dev->parent); 834 device_lock(dev->parent);
827 device_release_driver(dev); 835 device_release_driver(dev);
828 if (dev->parent) 836 if (dev->parent)
829 device_unlock(dev->parent); 837 device_unlock(dev->parent);
830 } 838 }
831 return bus_rescan_devices_helper(dev, NULL); 839 return bus_rescan_devices_helper(dev, NULL);
832 } 840 }
833 EXPORT_SYMBOL_GPL(device_reprobe); 841 EXPORT_SYMBOL_GPL(device_reprobe);
834 842
835 /** 843 /**
836 * find_bus - locate bus by name. 844 * find_bus - locate bus by name.
837 * @name: name of bus. 845 * @name: name of bus.
838 * 846 *
839 * Call kset_find_obj() to iterate over list of buses to 847 * Call kset_find_obj() to iterate over list of buses to
840 * find a bus by name. Return bus if found. 848 * find a bus by name. Return bus if found.
841 * 849 *
842 * Note that kset_find_obj increments bus' reference count. 850 * Note that kset_find_obj increments bus' reference count.
843 */ 851 */
844 #if 0 852 #if 0
845 struct bus_type *find_bus(char *name) 853 struct bus_type *find_bus(char *name)
846 { 854 {
847 struct kobject *k = kset_find_obj(bus_kset, name); 855 struct kobject *k = kset_find_obj(bus_kset, name);
848 return k ? to_bus(k) : NULL; 856 return k ? to_bus(k) : NULL;
849 } 857 }
850 #endif /* 0 */ 858 #endif /* 0 */
851 859
852 860
853 /** 861 /**
854 * bus_add_attrs - Add default attributes for this bus. 862 * bus_add_attrs - Add default attributes for this bus.
855 * @bus: Bus that has just been registered. 863 * @bus: Bus that has just been registered.
856 */ 864 */
857 865
858 static int bus_add_attrs(struct bus_type *bus) 866 static int bus_add_attrs(struct bus_type *bus)
859 { 867 {
860 int error = 0; 868 int error = 0;
861 int i; 869 int i;
862 870
863 if (bus->bus_attrs) { 871 if (bus->bus_attrs) {
864 for (i = 0; attr_name(bus->bus_attrs[i]); i++) { 872 for (i = 0; attr_name(bus->bus_attrs[i]); i++) {
865 error = bus_create_file(bus, &bus->bus_attrs[i]); 873 error = bus_create_file(bus, &bus->bus_attrs[i]);
866 if (error) 874 if (error)
867 goto err; 875 goto err;
868 } 876 }
869 } 877 }
870 done: 878 done:
871 return error; 879 return error;
872 err: 880 err:
873 while (--i >= 0) 881 while (--i >= 0)
874 bus_remove_file(bus, &bus->bus_attrs[i]); 882 bus_remove_file(bus, &bus->bus_attrs[i]);
875 goto done; 883 goto done;
876 } 884 }
877 885
878 static void bus_remove_attrs(struct bus_type *bus) 886 static void bus_remove_attrs(struct bus_type *bus)
879 { 887 {
880 int i; 888 int i;
881 889
882 if (bus->bus_attrs) { 890 if (bus->bus_attrs) {
883 for (i = 0; attr_name(bus->bus_attrs[i]); i++) 891 for (i = 0; attr_name(bus->bus_attrs[i]); i++)
884 bus_remove_file(bus, &bus->bus_attrs[i]); 892 bus_remove_file(bus, &bus->bus_attrs[i]);
885 } 893 }
886 } 894 }
887 895
888 static void klist_devices_get(struct klist_node *n) 896 static void klist_devices_get(struct klist_node *n)
889 { 897 {
890 struct device_private *dev_prv = to_device_private_bus(n); 898 struct device_private *dev_prv = to_device_private_bus(n);
891 struct device *dev = dev_prv->device; 899 struct device *dev = dev_prv->device;
892 900
893 get_device(dev); 901 get_device(dev);
894 } 902 }
895 903
896 static void klist_devices_put(struct klist_node *n) 904 static void klist_devices_put(struct klist_node *n)
897 { 905 {
898 struct device_private *dev_prv = to_device_private_bus(n); 906 struct device_private *dev_prv = to_device_private_bus(n);
899 struct device *dev = dev_prv->device; 907 struct device *dev = dev_prv->device;
900 908
901 put_device(dev); 909 put_device(dev);
902 } 910 }
903 911
904 static ssize_t bus_uevent_store(struct bus_type *bus, 912 static ssize_t bus_uevent_store(struct bus_type *bus,
905 const char *buf, size_t count) 913 const char *buf, size_t count)
906 { 914 {
907 enum kobject_action action; 915 enum kobject_action action;
908 916
909 if (kobject_action_type(buf, count, &action) == 0) 917 if (kobject_action_type(buf, count, &action) == 0)
910 kobject_uevent(&bus->p->subsys.kobj, action); 918 kobject_uevent(&bus->p->subsys.kobj, action);
911 return count; 919 return count;
912 } 920 }
913 static BUS_ATTR(uevent, S_IWUSR, NULL, bus_uevent_store); 921 static BUS_ATTR(uevent, S_IWUSR, NULL, bus_uevent_store);
914 922
915 /** 923 /**
916 * __bus_register - register a driver-core subsystem 924 * __bus_register - register a driver-core subsystem
917 * @bus: bus to register 925 * @bus: bus to register
918 * @key: lockdep class key 926 * @key: lockdep class key
919 * 927 *
920 * Once we have that, we register the bus with the kobject 928 * Once we have that, we register the bus with the kobject
921 * infrastructure, then register the children subsystems it has: 929 * infrastructure, then register the children subsystems it has:
922 * the devices and drivers that belong to the subsystem. 930 * the devices and drivers that belong to the subsystem.
923 */ 931 */
924 int __bus_register(struct bus_type *bus, struct lock_class_key *key) 932 int __bus_register(struct bus_type *bus, struct lock_class_key *key)
925 { 933 {
926 int retval; 934 int retval;
927 struct subsys_private *priv; 935 struct subsys_private *priv;
928 936
929 priv = kzalloc(sizeof(struct subsys_private), GFP_KERNEL); 937 priv = kzalloc(sizeof(struct subsys_private), GFP_KERNEL);
930 if (!priv) 938 if (!priv)
931 return -ENOMEM; 939 return -ENOMEM;
932 940
933 priv->bus = bus; 941 priv->bus = bus;
934 bus->p = priv; 942 bus->p = priv;
935 943
936 BLOCKING_INIT_NOTIFIER_HEAD(&priv->bus_notifier); 944 BLOCKING_INIT_NOTIFIER_HEAD(&priv->bus_notifier);
937 945
938 retval = kobject_set_name(&priv->subsys.kobj, "%s", bus->name); 946 retval = kobject_set_name(&priv->subsys.kobj, "%s", bus->name);
939 if (retval) 947 if (retval)
940 goto out; 948 goto out;
941 949
942 priv->subsys.kobj.kset = bus_kset; 950 priv->subsys.kobj.kset = bus_kset;
943 priv->subsys.kobj.ktype = &bus_ktype; 951 priv->subsys.kobj.ktype = &bus_ktype;
944 priv->drivers_autoprobe = 1; 952 priv->drivers_autoprobe = 1;
945 953
946 retval = kset_register(&priv->subsys); 954 retval = kset_register(&priv->subsys);
947 if (retval) 955 if (retval)
948 goto out; 956 goto out;
949 957
950 retval = bus_create_file(bus, &bus_attr_uevent); 958 retval = bus_create_file(bus, &bus_attr_uevent);
951 if (retval) 959 if (retval)
952 goto bus_uevent_fail; 960 goto bus_uevent_fail;
953 961
954 priv->devices_kset = kset_create_and_add("devices", NULL, 962 priv->devices_kset = kset_create_and_add("devices", NULL,
955 &priv->subsys.kobj); 963 &priv->subsys.kobj);
956 if (!priv->devices_kset) { 964 if (!priv->devices_kset) {
957 retval = -ENOMEM; 965 retval = -ENOMEM;
958 goto bus_devices_fail; 966 goto bus_devices_fail;
959 } 967 }
960 968
961 priv->drivers_kset = kset_create_and_add("drivers", NULL, 969 priv->drivers_kset = kset_create_and_add("drivers", NULL,
962 &priv->subsys.kobj); 970 &priv->subsys.kobj);
963 if (!priv->drivers_kset) { 971 if (!priv->drivers_kset) {
964 retval = -ENOMEM; 972 retval = -ENOMEM;
965 goto bus_drivers_fail; 973 goto bus_drivers_fail;
966 } 974 }
967 975
968 INIT_LIST_HEAD(&priv->interfaces); 976 INIT_LIST_HEAD(&priv->interfaces);
969 __mutex_init(&priv->mutex, "subsys mutex", key); 977 __mutex_init(&priv->mutex, "subsys mutex", key);
970 klist_init(&priv->klist_devices, klist_devices_get, klist_devices_put); 978 klist_init(&priv->klist_devices, klist_devices_get, klist_devices_put);
971 klist_init(&priv->klist_drivers, NULL, NULL); 979 klist_init(&priv->klist_drivers, NULL, NULL);
972 980
973 retval = add_probe_files(bus); 981 retval = add_probe_files(bus);
974 if (retval) 982 if (retval)
975 goto bus_probe_files_fail; 983 goto bus_probe_files_fail;
976 984
977 retval = bus_add_attrs(bus); 985 retval = bus_add_attrs(bus);
978 if (retval) 986 if (retval)
979 goto bus_attrs_fail; 987 goto bus_attrs_fail;
980 988
981 pr_debug("bus: '%s': registered\n", bus->name); 989 pr_debug("bus: '%s': registered\n", bus->name);
982 return 0; 990 return 0;
983 991
984 bus_attrs_fail: 992 bus_attrs_fail:
985 remove_probe_files(bus); 993 remove_probe_files(bus);
986 bus_probe_files_fail: 994 bus_probe_files_fail:
987 kset_unregister(bus->p->drivers_kset); 995 kset_unregister(bus->p->drivers_kset);
988 bus_drivers_fail: 996 bus_drivers_fail:
989 kset_unregister(bus->p->devices_kset); 997 kset_unregister(bus->p->devices_kset);
990 bus_devices_fail: 998 bus_devices_fail:
991 bus_remove_file(bus, &bus_attr_uevent); 999 bus_remove_file(bus, &bus_attr_uevent);
992 bus_uevent_fail: 1000 bus_uevent_fail:
993 kset_unregister(&bus->p->subsys); 1001 kset_unregister(&bus->p->subsys);
994 out: 1002 out:
995 kfree(bus->p); 1003 kfree(bus->p);
996 bus->p = NULL; 1004 bus->p = NULL;
997 return retval; 1005 return retval;
998 } 1006 }
999 EXPORT_SYMBOL_GPL(__bus_register); 1007 EXPORT_SYMBOL_GPL(__bus_register);
1000 1008
1001 /** 1009 /**
1002 * bus_unregister - remove a bus from the system 1010 * bus_unregister - remove a bus from the system
1003 * @bus: bus. 1011 * @bus: bus.
1004 * 1012 *
1005 * Unregister the child subsystems and the bus itself. 1013 * Unregister the child subsystems and the bus itself.
1006 * Finally, we call bus_put() to release the refcount 1014 * Finally, we call bus_put() to release the refcount
1007 */ 1015 */
1008 void bus_unregister(struct bus_type *bus) 1016 void bus_unregister(struct bus_type *bus)
1009 { 1017 {
1010 pr_debug("bus: '%s': unregistering\n", bus->name); 1018 pr_debug("bus: '%s': unregistering\n", bus->name);
1011 if (bus->dev_root) 1019 if (bus->dev_root)
1012 device_unregister(bus->dev_root); 1020 device_unregister(bus->dev_root);
1013 bus_remove_attrs(bus); 1021 bus_remove_attrs(bus);
1014 remove_probe_files(bus); 1022 remove_probe_files(bus);
1015 kset_unregister(bus->p->drivers_kset); 1023 kset_unregister(bus->p->drivers_kset);
1016 kset_unregister(bus->p->devices_kset); 1024 kset_unregister(bus->p->devices_kset);
1017 bus_remove_file(bus, &bus_attr_uevent); 1025 bus_remove_file(bus, &bus_attr_uevent);
1018 kset_unregister(&bus->p->subsys); 1026 kset_unregister(&bus->p->subsys);
1019 kfree(bus->p); 1027 kfree(bus->p);
1020 bus->p = NULL; 1028 bus->p = NULL;
1021 } 1029 }
1022 EXPORT_SYMBOL_GPL(bus_unregister); 1030 EXPORT_SYMBOL_GPL(bus_unregister);
1023 1031
1024 int bus_register_notifier(struct bus_type *bus, struct notifier_block *nb) 1032 int bus_register_notifier(struct bus_type *bus, struct notifier_block *nb)
1025 { 1033 {
1026 return blocking_notifier_chain_register(&bus->p->bus_notifier, nb); 1034 return blocking_notifier_chain_register(&bus->p->bus_notifier, nb);
1027 } 1035 }
1028 EXPORT_SYMBOL_GPL(bus_register_notifier); 1036 EXPORT_SYMBOL_GPL(bus_register_notifier);
1029 1037
1030 int bus_unregister_notifier(struct bus_type *bus, struct notifier_block *nb) 1038 int bus_unregister_notifier(struct bus_type *bus, struct notifier_block *nb)
1031 { 1039 {
1032 return blocking_notifier_chain_unregister(&bus->p->bus_notifier, nb); 1040 return blocking_notifier_chain_unregister(&bus->p->bus_notifier, nb);
1033 } 1041 }
1034 EXPORT_SYMBOL_GPL(bus_unregister_notifier); 1042 EXPORT_SYMBOL_GPL(bus_unregister_notifier);
1035 1043
1036 struct kset *bus_get_kset(struct bus_type *bus) 1044 struct kset *bus_get_kset(struct bus_type *bus)
1037 { 1045 {
1038 return &bus->p->subsys; 1046 return &bus->p->subsys;
1039 } 1047 }
1040 EXPORT_SYMBOL_GPL(bus_get_kset); 1048 EXPORT_SYMBOL_GPL(bus_get_kset);
1041 1049
1042 struct klist *bus_get_device_klist(struct bus_type *bus) 1050 struct klist *bus_get_device_klist(struct bus_type *bus)
1043 { 1051 {
1044 return &bus->p->klist_devices; 1052 return &bus->p->klist_devices;
1045 } 1053 }
1046 EXPORT_SYMBOL_GPL(bus_get_device_klist); 1054 EXPORT_SYMBOL_GPL(bus_get_device_klist);
1047 1055
1048 /* 1056 /*
1049 * Yes, this forcibly breaks the klist abstraction temporarily. It 1057 * Yes, this forcibly breaks the klist abstraction temporarily. It
1050 * just wants to sort the klist, not change reference counts and 1058 * just wants to sort the klist, not change reference counts and
1051 * take/drop locks rapidly in the process. It does all this while 1059 * take/drop locks rapidly in the process. It does all this while
1052 * holding the lock for the list, so objects can't otherwise be 1060 * holding the lock for the list, so objects can't otherwise be
1053 * added/removed while we're swizzling. 1061 * added/removed while we're swizzling.
1054 */ 1062 */
1055 static void device_insertion_sort_klist(struct device *a, struct list_head *list, 1063 static void device_insertion_sort_klist(struct device *a, struct list_head *list,
1056 int (*compare)(const struct device *a, 1064 int (*compare)(const struct device *a,
1057 const struct device *b)) 1065 const struct device *b))
1058 { 1066 {
1059 struct list_head *pos; 1067 struct list_head *pos;
1060 struct klist_node *n; 1068 struct klist_node *n;
1061 struct device_private *dev_prv; 1069 struct device_private *dev_prv;
1062 struct device *b; 1070 struct device *b;
1063 1071
1064 list_for_each(pos, list) { 1072 list_for_each(pos, list) {
1065 n = container_of(pos, struct klist_node, n_node); 1073 n = container_of(pos, struct klist_node, n_node);
1066 dev_prv = to_device_private_bus(n); 1074 dev_prv = to_device_private_bus(n);
1067 b = dev_prv->device; 1075 b = dev_prv->device;
1068 if (compare(a, b) <= 0) { 1076 if (compare(a, b) <= 0) {
1069 list_move_tail(&a->p->knode_bus.n_node, 1077 list_move_tail(&a->p->knode_bus.n_node,
1070 &b->p->knode_bus.n_node); 1078 &b->p->knode_bus.n_node);
1071 return; 1079 return;
1072 } 1080 }
1073 } 1081 }
1074 list_move_tail(&a->p->knode_bus.n_node, list); 1082 list_move_tail(&a->p->knode_bus.n_node, list);
1075 } 1083 }
1076 1084
1077 void bus_sort_breadthfirst(struct bus_type *bus, 1085 void bus_sort_breadthfirst(struct bus_type *bus,
1078 int (*compare)(const struct device *a, 1086 int (*compare)(const struct device *a,
1079 const struct device *b)) 1087 const struct device *b))
1080 { 1088 {
1081 LIST_HEAD(sorted_devices); 1089 LIST_HEAD(sorted_devices);
1082 struct list_head *pos, *tmp; 1090 struct list_head *pos, *tmp;
1083 struct klist_node *n; 1091 struct klist_node *n;
1084 struct device_private *dev_prv; 1092 struct device_private *dev_prv;
1085 struct device *dev; 1093 struct device *dev;
1086 struct klist *device_klist; 1094 struct klist *device_klist;
1087 1095
1088 device_klist = bus_get_device_klist(bus); 1096 device_klist = bus_get_device_klist(bus);
1089 1097
1090 spin_lock(&device_klist->k_lock); 1098 spin_lock(&device_klist->k_lock);
1091 list_for_each_safe(pos, tmp, &device_klist->k_list) { 1099 list_for_each_safe(pos, tmp, &device_klist->k_list) {
1092 n = container_of(pos, struct klist_node, n_node); 1100 n = container_of(pos, struct klist_node, n_node);
1093 dev_prv = to_device_private_bus(n); 1101 dev_prv = to_device_private_bus(n);
1094 dev = dev_prv->device; 1102 dev = dev_prv->device;
1095 device_insertion_sort_klist(dev, &sorted_devices, compare); 1103 device_insertion_sort_klist(dev, &sorted_devices, compare);
1096 } 1104 }
1097 list_splice(&sorted_devices, &device_klist->k_list); 1105 list_splice(&sorted_devices, &device_klist->k_list);
1098 spin_unlock(&device_klist->k_lock); 1106 spin_unlock(&device_klist->k_lock);
1099 } 1107 }
1100 EXPORT_SYMBOL_GPL(bus_sort_breadthfirst); 1108 EXPORT_SYMBOL_GPL(bus_sort_breadthfirst);
1101 1109
1102 /** 1110 /**
1103 * subsys_dev_iter_init - initialize subsys device iterator 1111 * subsys_dev_iter_init - initialize subsys device iterator
1104 * @iter: subsys iterator to initialize 1112 * @iter: subsys iterator to initialize
1105 * @subsys: the subsys we wanna iterate over 1113 * @subsys: the subsys we wanna iterate over
1106 * @start: the device to start iterating from, if any 1114 * @start: the device to start iterating from, if any
1107 * @type: device_type of the devices to iterate over, NULL for all 1115 * @type: device_type of the devices to iterate over, NULL for all
1108 * 1116 *
1109 * Initialize subsys iterator @iter such that it iterates over devices 1117 * Initialize subsys iterator @iter such that it iterates over devices
1110 * of @subsys. If @start is set, the list iteration will start there, 1118 * of @subsys. If @start is set, the list iteration will start there,
1111 * otherwise if it is NULL, the iteration starts at the beginning of 1119 * otherwise if it is NULL, the iteration starts at the beginning of
1112 * the list. 1120 * the list.
1113 */ 1121 */
1114 void subsys_dev_iter_init(struct subsys_dev_iter *iter, struct bus_type *subsys, 1122 int subsys_dev_iter_init(struct subsys_dev_iter *iter, struct bus_type *subsys,
1115 struct device *start, const struct device_type *type) 1123 struct device *start, const struct device_type *type)
1116 { 1124 {
1117 struct klist_node *start_knode = NULL; 1125 struct klist_node *start_knode = NULL;
1126 int error;
1118 1127
1119 if (start) 1128 if (start)
1120 start_knode = &start->p->knode_bus; 1129 start_knode = &start->p->knode_bus;
1121 klist_iter_init_node(&subsys->p->klist_devices, &iter->ki, start_knode); 1130 error = klist_iter_init_node(&subsys->p->klist_devices, &iter->ki,
1122 iter->type = type; 1131 start_knode);
1132 if (!error)
1133 iter->type = type;
1134 return error;
1123 } 1135 }
1124 EXPORT_SYMBOL_GPL(subsys_dev_iter_init); 1136 EXPORT_SYMBOL_GPL(subsys_dev_iter_init);
1125 1137
1126 /** 1138 /**
1127 * subsys_dev_iter_next - iterate to the next device 1139 * subsys_dev_iter_next - iterate to the next device
1128 * @iter: subsys iterator to proceed 1140 * @iter: subsys iterator to proceed
1129 * 1141 *
1130 * Proceed @iter to the next device and return it. Returns NULL if 1142 * Proceed @iter to the next device and return it. Returns NULL if
1131 * iteration is complete. 1143 * iteration is complete.
1132 * 1144 *
1133 * The returned device is referenced and won't be released till 1145 * The returned device is referenced and won't be released till
1134 * iterator is proceed to the next device or exited. The caller is 1146 * iterator is proceed to the next device or exited. The caller is
1135 * free to do whatever it wants to do with the device including 1147 * free to do whatever it wants to do with the device including
1136 * calling back into subsys code. 1148 * calling back into subsys code.
1137 */ 1149 */
1138 struct device *subsys_dev_iter_next(struct subsys_dev_iter *iter) 1150 struct device *subsys_dev_iter_next(struct subsys_dev_iter *iter)
1139 { 1151 {
1140 struct klist_node *knode; 1152 struct klist_node *knode;
1141 struct device *dev; 1153 struct device *dev;
1142 1154
1143 for (;;) { 1155 for (;;) {
1144 knode = klist_next(&iter->ki); 1156 knode = klist_next(&iter->ki);
1145 if (!knode) 1157 if (!knode)
1146 return NULL; 1158 return NULL;
1147 dev = container_of(knode, struct device_private, knode_bus)->device; 1159 dev = container_of(knode, struct device_private, knode_bus)->device;
1148 if (!iter->type || iter->type == dev->type) 1160 if (!iter->type || iter->type == dev->type)
1149 return dev; 1161 return dev;
1150 } 1162 }
1151 } 1163 }
1152 EXPORT_SYMBOL_GPL(subsys_dev_iter_next); 1164 EXPORT_SYMBOL_GPL(subsys_dev_iter_next);
1153 1165
1154 /** 1166 /**
1155 * subsys_dev_iter_exit - finish iteration 1167 * subsys_dev_iter_exit - finish iteration
1156 * @iter: subsys iterator to finish 1168 * @iter: subsys iterator to finish
1157 * 1169 *
1158 * Finish an iteration. Always call this function after iteration is 1170 * Finish an iteration. Always call this function after iteration is
1159 * complete whether the iteration ran till the end or not. 1171 * complete whether the iteration ran till the end or not.
1160 */ 1172 */
1161 void subsys_dev_iter_exit(struct subsys_dev_iter *iter) 1173 void subsys_dev_iter_exit(struct subsys_dev_iter *iter)
1162 { 1174 {
1163 klist_iter_exit(&iter->ki); 1175 klist_iter_exit(&iter->ki);
1164 } 1176 }
1165 EXPORT_SYMBOL_GPL(subsys_dev_iter_exit); 1177 EXPORT_SYMBOL_GPL(subsys_dev_iter_exit);
1166 1178
1167 int subsys_interface_register(struct subsys_interface *sif) 1179 int subsys_interface_register(struct subsys_interface *sif)
1168 { 1180 {
1169 struct bus_type *subsys; 1181 struct bus_type *subsys;
1170 struct subsys_dev_iter iter; 1182 struct subsys_dev_iter iter;
1171 struct device *dev; 1183 struct device *dev;
1172 1184
1173 if (!sif || !sif->subsys) 1185 if (!sif || !sif->subsys)
1174 return -ENODEV; 1186 return -ENODEV;
1175 1187
1176 subsys = bus_get(sif->subsys); 1188 subsys = bus_get(sif->subsys);
1177 if (!subsys) 1189 if (!subsys)
1178 return -EINVAL; 1190 return -EINVAL;
1179 1191
1180 mutex_lock(&subsys->p->mutex); 1192 mutex_lock(&subsys->p->mutex);
1181 list_add_tail(&sif->node, &subsys->p->interfaces); 1193 list_add_tail(&sif->node, &subsys->p->interfaces);
1182 if (sif->add_dev) { 1194 if (sif->add_dev) {
1183 subsys_dev_iter_init(&iter, subsys, NULL, NULL); 1195 subsys_dev_iter_init(&iter, subsys, NULL, NULL);
1184 while ((dev = subsys_dev_iter_next(&iter))) 1196 while ((dev = subsys_dev_iter_next(&iter)))
1185 sif->add_dev(dev, sif); 1197 sif->add_dev(dev, sif);
1186 subsys_dev_iter_exit(&iter); 1198 subsys_dev_iter_exit(&iter);
1187 } 1199 }
1188 mutex_unlock(&subsys->p->mutex); 1200 mutex_unlock(&subsys->p->mutex);
1189 1201
1190 return 0; 1202 return 0;
1191 } 1203 }
1192 EXPORT_SYMBOL_GPL(subsys_interface_register); 1204 EXPORT_SYMBOL_GPL(subsys_interface_register);
1193 1205
1194 void subsys_interface_unregister(struct subsys_interface *sif) 1206 void subsys_interface_unregister(struct subsys_interface *sif)
1195 { 1207 {
1196 struct bus_type *subsys; 1208 struct bus_type *subsys;
1197 struct subsys_dev_iter iter; 1209 struct subsys_dev_iter iter;
1198 struct device *dev; 1210 struct device *dev;
1199 1211
1200 if (!sif || !sif->subsys) 1212 if (!sif || !sif->subsys)
1201 return; 1213 return;
1202 1214
1203 subsys = sif->subsys; 1215 subsys = sif->subsys;
1204 1216
1205 mutex_lock(&subsys->p->mutex); 1217 mutex_lock(&subsys->p->mutex);
1206 list_del_init(&sif->node); 1218 list_del_init(&sif->node);
1207 if (sif->remove_dev) { 1219 if (sif->remove_dev) {
1208 subsys_dev_iter_init(&iter, subsys, NULL, NULL); 1220 subsys_dev_iter_init(&iter, subsys, NULL, NULL);
1209 while ((dev = subsys_dev_iter_next(&iter))) 1221 while ((dev = subsys_dev_iter_next(&iter)))
1210 sif->remove_dev(dev, sif); 1222 sif->remove_dev(dev, sif);
1211 subsys_dev_iter_exit(&iter); 1223 subsys_dev_iter_exit(&iter);
1212 } 1224 }
1213 mutex_unlock(&subsys->p->mutex); 1225 mutex_unlock(&subsys->p->mutex);
1214 1226
1215 bus_put(subsys); 1227 bus_put(subsys);
1216 } 1228 }
1217 EXPORT_SYMBOL_GPL(subsys_interface_unregister); 1229 EXPORT_SYMBOL_GPL(subsys_interface_unregister);
1218 1230
1219 static void system_root_device_release(struct device *dev) 1231 static void system_root_device_release(struct device *dev)
1220 { 1232 {
1221 kfree(dev); 1233 kfree(dev);
1222 } 1234 }
1223 /** 1235 /**
1224 * subsys_system_register - register a subsystem at /sys/devices/system/ 1236 * subsys_system_register - register a subsystem at /sys/devices/system/
1225 * @subsys: system subsystem 1237 * @subsys: system subsystem
1226 * @groups: default attributes for the root device 1238 * @groups: default attributes for the root device
1227 * 1239 *
1228 * All 'system' subsystems have a /sys/devices/system/<name> root device 1240 * All 'system' subsystems have a /sys/devices/system/<name> root device
1229 * with the name of the subsystem. The root device can carry subsystem- 1241 * with the name of the subsystem. The root device can carry subsystem-
1230 * wide attributes. All registered devices are below this single root 1242 * wide attributes. All registered devices are below this single root
1231 * device and are named after the subsystem with a simple enumeration 1243 * device and are named after the subsystem with a simple enumeration
1232 * number appended. The registered devices are not explicitely named; 1244 * number appended. The registered devices are not explicitely named;
1233 * only 'id' in the device needs to be set. 1245 * only 'id' in the device needs to be set.
1234 * 1246 *
1235 * Do not use this interface for anything new, it exists for compatibility 1247 * Do not use this interface for anything new, it exists for compatibility
1236 * with bad ideas only. New subsystems should use plain subsystems; and 1248 * with bad ideas only. New subsystems should use plain subsystems; and
1237 * add the subsystem-wide attributes should be added to the subsystem 1249 * add the subsystem-wide attributes should be added to the subsystem
1238 * directory itself and not some create fake root-device placed in 1250 * directory itself and not some create fake root-device placed in
1239 * /sys/devices/system/<name>. 1251 * /sys/devices/system/<name>.
1240 */ 1252 */
1241 int subsys_system_register(struct bus_type *subsys, 1253 int subsys_system_register(struct bus_type *subsys,
1242 const struct attribute_group **groups) 1254 const struct attribute_group **groups)
1243 { 1255 {
1244 struct device *dev; 1256 struct device *dev;
1245 int err; 1257 int err;
1246 1258
1247 err = bus_register(subsys); 1259 err = bus_register(subsys);
1248 if (err < 0) 1260 if (err < 0)
1249 return err; 1261 return err;
1250 1262
1251 dev = kzalloc(sizeof(struct device), GFP_KERNEL); 1263 dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1252 if (!dev) { 1264 if (!dev) {
1253 err = -ENOMEM; 1265 err = -ENOMEM;
1254 goto err_dev; 1266 goto err_dev;
1255 } 1267 }
1256 1268
1257 err = dev_set_name(dev, "%s", subsys->name); 1269 err = dev_set_name(dev, "%s", subsys->name);
1258 if (err < 0) 1270 if (err < 0)
1259 goto err_name; 1271 goto err_name;
1260 1272
1261 dev->kobj.parent = &system_kset->kobj; 1273 dev->kobj.parent = &system_kset->kobj;
1262 dev->groups = groups; 1274 dev->groups = groups;
1263 dev->release = system_root_device_release; 1275 dev->release = system_root_device_release;
1264 1276
1265 err = device_register(dev); 1277 err = device_register(dev);
1266 if (err < 0) 1278 if (err < 0)
1267 goto err_dev_reg; 1279 goto err_dev_reg;
1268 1280
1269 subsys->dev_root = dev; 1281 subsys->dev_root = dev;
1270 return 0; 1282 return 0;
1271 1283
1272 err_dev_reg: 1284 err_dev_reg:
1273 put_device(dev); 1285 put_device(dev);
1274 dev = NULL; 1286 dev = NULL;
1275 err_name: 1287 err_name:
1276 kfree(dev); 1288 kfree(dev);
1277 err_dev: 1289 err_dev:
1278 bus_unregister(subsys); 1290 bus_unregister(subsys);
1279 return err; 1291 return err;
1280 } 1292 }
1281 EXPORT_SYMBOL_GPL(subsys_system_register); 1293 EXPORT_SYMBOL_GPL(subsys_system_register);
1282 1294
1283 int __init buses_init(void) 1295 int __init buses_init(void)
1284 { 1296 {
1285 bus_kset = kset_create_and_add("bus", &bus_uevent_ops, NULL); 1297 bus_kset = kset_create_and_add("bus", &bus_uevent_ops, NULL);
1286 if (!bus_kset) 1298 if (!bus_kset)
1287 return -ENOMEM; 1299 return -ENOMEM;
1288 1300
1289 system_kset = kset_create_and_add("system", NULL, &devices_kset->kobj); 1301 system_kset = kset_create_and_add("system", NULL, &devices_kset->kobj);
1290 if (!system_kset) 1302 if (!system_kset)
1291 return -ENOMEM; 1303 return -ENOMEM;
1292 1304
1293 return 0; 1305 return 0;
1294 } 1306 }
1295 1307
drivers/base/class.c
1 /* 1 /*
2 * class.c - basic device class management 2 * class.c - basic device class management
3 * 3 *
4 * Copyright (c) 2002-3 Patrick Mochel 4 * Copyright (c) 2002-3 Patrick Mochel
5 * Copyright (c) 2002-3 Open Source Development Labs 5 * Copyright (c) 2002-3 Open Source Development Labs
6 * Copyright (c) 2003-2004 Greg Kroah-Hartman 6 * Copyright (c) 2003-2004 Greg Kroah-Hartman
7 * Copyright (c) 2003-2004 IBM Corp. 7 * Copyright (c) 2003-2004 IBM Corp.
8 * 8 *
9 * This file is released under the GPLv2 9 * This file is released under the GPLv2
10 * 10 *
11 */ 11 */
12 12
13 #include <linux/device.h> 13 #include <linux/device.h>
14 #include <linux/module.h> 14 #include <linux/module.h>
15 #include <linux/init.h> 15 #include <linux/init.h>
16 #include <linux/string.h> 16 #include <linux/string.h>
17 #include <linux/kdev_t.h> 17 #include <linux/kdev_t.h>
18 #include <linux/err.h> 18 #include <linux/err.h>
19 #include <linux/slab.h> 19 #include <linux/slab.h>
20 #include <linux/genhd.h> 20 #include <linux/genhd.h>
21 #include <linux/mutex.h> 21 #include <linux/mutex.h>
22 #include "base.h" 22 #include "base.h"
23 23
24 #define to_class_attr(_attr) container_of(_attr, struct class_attribute, attr) 24 #define to_class_attr(_attr) container_of(_attr, struct class_attribute, attr)
25 25
26 static ssize_t class_attr_show(struct kobject *kobj, struct attribute *attr, 26 static ssize_t class_attr_show(struct kobject *kobj, struct attribute *attr,
27 char *buf) 27 char *buf)
28 { 28 {
29 struct class_attribute *class_attr = to_class_attr(attr); 29 struct class_attribute *class_attr = to_class_attr(attr);
30 struct subsys_private *cp = to_subsys_private(kobj); 30 struct subsys_private *cp = to_subsys_private(kobj);
31 ssize_t ret = -EIO; 31 ssize_t ret = -EIO;
32 32
33 if (class_attr->show) 33 if (class_attr->show)
34 ret = class_attr->show(cp->class, class_attr, buf); 34 ret = class_attr->show(cp->class, class_attr, buf);
35 return ret; 35 return ret;
36 } 36 }
37 37
38 static ssize_t class_attr_store(struct kobject *kobj, struct attribute *attr, 38 static ssize_t class_attr_store(struct kobject *kobj, struct attribute *attr,
39 const char *buf, size_t count) 39 const char *buf, size_t count)
40 { 40 {
41 struct class_attribute *class_attr = to_class_attr(attr); 41 struct class_attribute *class_attr = to_class_attr(attr);
42 struct subsys_private *cp = to_subsys_private(kobj); 42 struct subsys_private *cp = to_subsys_private(kobj);
43 ssize_t ret = -EIO; 43 ssize_t ret = -EIO;
44 44
45 if (class_attr->store) 45 if (class_attr->store)
46 ret = class_attr->store(cp->class, class_attr, buf, count); 46 ret = class_attr->store(cp->class, class_attr, buf, count);
47 return ret; 47 return ret;
48 } 48 }
49 49
50 static const void *class_attr_namespace(struct kobject *kobj, 50 static const void *class_attr_namespace(struct kobject *kobj,
51 const struct attribute *attr) 51 const struct attribute *attr)
52 { 52 {
53 struct class_attribute *class_attr = to_class_attr(attr); 53 struct class_attribute *class_attr = to_class_attr(attr);
54 struct subsys_private *cp = to_subsys_private(kobj); 54 struct subsys_private *cp = to_subsys_private(kobj);
55 const void *ns = NULL; 55 const void *ns = NULL;
56 56
57 if (class_attr->namespace) 57 if (class_attr->namespace)
58 ns = class_attr->namespace(cp->class, class_attr); 58 ns = class_attr->namespace(cp->class, class_attr);
59 return ns; 59 return ns;
60 } 60 }
61 61
62 static void class_release(struct kobject *kobj) 62 static void class_release(struct kobject *kobj)
63 { 63 {
64 struct subsys_private *cp = to_subsys_private(kobj); 64 struct subsys_private *cp = to_subsys_private(kobj);
65 struct class *class = cp->class; 65 struct class *class = cp->class;
66 66
67 pr_debug("class '%s': release.\n", class->name); 67 pr_debug("class '%s': release.\n", class->name);
68 68
69 if (class->class_release) 69 if (class->class_release)
70 class->class_release(class); 70 class->class_release(class);
71 else 71 else
72 pr_debug("class '%s' does not have a release() function, " 72 pr_debug("class '%s' does not have a release() function, "
73 "be careful\n", class->name); 73 "be careful\n", class->name);
74 74
75 kfree(cp); 75 kfree(cp);
76 } 76 }
77 77
78 static const struct kobj_ns_type_operations *class_child_ns_type(struct kobject *kobj) 78 static const struct kobj_ns_type_operations *class_child_ns_type(struct kobject *kobj)
79 { 79 {
80 struct subsys_private *cp = to_subsys_private(kobj); 80 struct subsys_private *cp = to_subsys_private(kobj);
81 struct class *class = cp->class; 81 struct class *class = cp->class;
82 82
83 return class->ns_type; 83 return class->ns_type;
84 } 84 }
85 85
86 static const struct sysfs_ops class_sysfs_ops = { 86 static const struct sysfs_ops class_sysfs_ops = {
87 .show = class_attr_show, 87 .show = class_attr_show,
88 .store = class_attr_store, 88 .store = class_attr_store,
89 .namespace = class_attr_namespace, 89 .namespace = class_attr_namespace,
90 }; 90 };
91 91
92 static struct kobj_type class_ktype = { 92 static struct kobj_type class_ktype = {
93 .sysfs_ops = &class_sysfs_ops, 93 .sysfs_ops = &class_sysfs_ops,
94 .release = class_release, 94 .release = class_release,
95 .child_ns_type = class_child_ns_type, 95 .child_ns_type = class_child_ns_type,
96 }; 96 };
97 97
98 /* Hotplug events for classes go to the class subsys */ 98 /* Hotplug events for classes go to the class subsys */
99 static struct kset *class_kset; 99 static struct kset *class_kset;
100 100
101 101
102 int class_create_file(struct class *cls, const struct class_attribute *attr) 102 int class_create_file(struct class *cls, const struct class_attribute *attr)
103 { 103 {
104 int error; 104 int error;
105 if (cls) 105 if (cls)
106 error = sysfs_create_file(&cls->p->subsys.kobj, 106 error = sysfs_create_file(&cls->p->subsys.kobj,
107 &attr->attr); 107 &attr->attr);
108 else 108 else
109 error = -EINVAL; 109 error = -EINVAL;
110 return error; 110 return error;
111 } 111 }
112 112
113 void class_remove_file(struct class *cls, const struct class_attribute *attr) 113 void class_remove_file(struct class *cls, const struct class_attribute *attr)
114 { 114 {
115 if (cls) 115 if (cls)
116 sysfs_remove_file(&cls->p->subsys.kobj, &attr->attr); 116 sysfs_remove_file(&cls->p->subsys.kobj, &attr->attr);
117 } 117 }
118 118
119 static struct class *class_get(struct class *cls) 119 static struct class *class_get(struct class *cls)
120 { 120 {
121 if (cls) 121 if (cls)
122 kset_get(&cls->p->subsys); 122 kset_get(&cls->p->subsys);
123 return cls; 123 return cls;
124 } 124 }
125 125
126 static void class_put(struct class *cls) 126 static void class_put(struct class *cls)
127 { 127 {
128 if (cls) 128 if (cls)
129 kset_put(&cls->p->subsys); 129 kset_put(&cls->p->subsys);
130 } 130 }
131 131
132 static int add_class_attrs(struct class *cls) 132 static int add_class_attrs(struct class *cls)
133 { 133 {
134 int i; 134 int i;
135 int error = 0; 135 int error = 0;
136 136
137 if (cls->class_attrs) { 137 if (cls->class_attrs) {
138 for (i = 0; attr_name(cls->class_attrs[i]); i++) { 138 for (i = 0; attr_name(cls->class_attrs[i]); i++) {
139 error = class_create_file(cls, &cls->class_attrs[i]); 139 error = class_create_file(cls, &cls->class_attrs[i]);
140 if (error) 140 if (error)
141 goto error; 141 goto error;
142 } 142 }
143 } 143 }
144 done: 144 done:
145 return error; 145 return error;
146 error: 146 error:
147 while (--i >= 0) 147 while (--i >= 0)
148 class_remove_file(cls, &cls->class_attrs[i]); 148 class_remove_file(cls, &cls->class_attrs[i]);
149 goto done; 149 goto done;
150 } 150 }
151 151
152 static void remove_class_attrs(struct class *cls) 152 static void remove_class_attrs(struct class *cls)
153 { 153 {
154 int i; 154 int i;
155 155
156 if (cls->class_attrs) { 156 if (cls->class_attrs) {
157 for (i = 0; attr_name(cls->class_attrs[i]); i++) 157 for (i = 0; attr_name(cls->class_attrs[i]); i++)
158 class_remove_file(cls, &cls->class_attrs[i]); 158 class_remove_file(cls, &cls->class_attrs[i]);
159 } 159 }
160 } 160 }
161 161
162 static void klist_class_dev_get(struct klist_node *n) 162 static void klist_class_dev_get(struct klist_node *n)
163 { 163 {
164 struct device *dev = container_of(n, struct device, knode_class); 164 struct device *dev = container_of(n, struct device, knode_class);
165 165
166 get_device(dev); 166 get_device(dev);
167 } 167 }
168 168
169 static void klist_class_dev_put(struct klist_node *n) 169 static void klist_class_dev_put(struct klist_node *n)
170 { 170 {
171 struct device *dev = container_of(n, struct device, knode_class); 171 struct device *dev = container_of(n, struct device, knode_class);
172 172
173 put_device(dev); 173 put_device(dev);
174 } 174 }
175 175
176 int __class_register(struct class *cls, struct lock_class_key *key) 176 int __class_register(struct class *cls, struct lock_class_key *key)
177 { 177 {
178 struct subsys_private *cp; 178 struct subsys_private *cp;
179 int error; 179 int error;
180 180
181 pr_debug("device class '%s': registering\n", cls->name); 181 pr_debug("device class '%s': registering\n", cls->name);
182 182
183 cp = kzalloc(sizeof(*cp), GFP_KERNEL); 183 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
184 if (!cp) 184 if (!cp)
185 return -ENOMEM; 185 return -ENOMEM;
186 klist_init(&cp->klist_devices, klist_class_dev_get, klist_class_dev_put); 186 klist_init(&cp->klist_devices, klist_class_dev_get, klist_class_dev_put);
187 INIT_LIST_HEAD(&cp->interfaces); 187 INIT_LIST_HEAD(&cp->interfaces);
188 kset_init(&cp->glue_dirs); 188 kset_init(&cp->glue_dirs);
189 __mutex_init(&cp->mutex, "subsys mutex", key); 189 __mutex_init(&cp->mutex, "subsys mutex", key);
190 error = kobject_set_name(&cp->subsys.kobj, "%s", cls->name); 190 error = kobject_set_name(&cp->subsys.kobj, "%s", cls->name);
191 if (error) { 191 if (error) {
192 kfree(cp); 192 kfree(cp);
193 return error; 193 return error;
194 } 194 }
195 195
196 /* set the default /sys/dev directory for devices of this class */ 196 /* set the default /sys/dev directory for devices of this class */
197 if (!cls->dev_kobj) 197 if (!cls->dev_kobj)
198 cls->dev_kobj = sysfs_dev_char_kobj; 198 cls->dev_kobj = sysfs_dev_char_kobj;
199 199
200 #if defined(CONFIG_BLOCK) 200 #if defined(CONFIG_BLOCK)
201 /* let the block class directory show up in the root of sysfs */ 201 /* let the block class directory show up in the root of sysfs */
202 if (!sysfs_deprecated || cls != &block_class) 202 if (!sysfs_deprecated || cls != &block_class)
203 cp->subsys.kobj.kset = class_kset; 203 cp->subsys.kobj.kset = class_kset;
204 #else 204 #else
205 cp->subsys.kobj.kset = class_kset; 205 cp->subsys.kobj.kset = class_kset;
206 #endif 206 #endif
207 cp->subsys.kobj.ktype = &class_ktype; 207 cp->subsys.kobj.ktype = &class_ktype;
208 cp->class = cls; 208 cp->class = cls;
209 cls->p = cp; 209 cls->p = cp;
210 210
211 error = kset_register(&cp->subsys); 211 error = kset_register(&cp->subsys);
212 if (error) { 212 if (error) {
213 kfree(cp); 213 kfree(cp);
214 return error; 214 return error;
215 } 215 }
216 error = add_class_attrs(class_get(cls)); 216 error = add_class_attrs(class_get(cls));
217 class_put(cls); 217 class_put(cls);
218 return error; 218 return error;
219 } 219 }
220 EXPORT_SYMBOL_GPL(__class_register); 220 EXPORT_SYMBOL_GPL(__class_register);
221 221
222 void class_unregister(struct class *cls) 222 void class_unregister(struct class *cls)
223 { 223 {
224 pr_debug("device class '%s': unregistering\n", cls->name); 224 pr_debug("device class '%s': unregistering\n", cls->name);
225 remove_class_attrs(cls); 225 remove_class_attrs(cls);
226 kset_unregister(&cls->p->subsys); 226 kset_unregister(&cls->p->subsys);
227 } 227 }
228 228
229 static void class_create_release(struct class *cls) 229 static void class_create_release(struct class *cls)
230 { 230 {
231 pr_debug("%s called for %s\n", __func__, cls->name); 231 pr_debug("%s called for %s\n", __func__, cls->name);
232 kfree(cls); 232 kfree(cls);
233 } 233 }
234 234
235 /** 235 /**
236 * class_create - create a struct class structure 236 * class_create - create a struct class structure
237 * @owner: pointer to the module that is to "own" this struct class 237 * @owner: pointer to the module that is to "own" this struct class
238 * @name: pointer to a string for the name of this class. 238 * @name: pointer to a string for the name of this class.
239 * @key: the lock_class_key for this class; used by mutex lock debugging 239 * @key: the lock_class_key for this class; used by mutex lock debugging
240 * 240 *
241 * This is used to create a struct class pointer that can then be used 241 * This is used to create a struct class pointer that can then be used
242 * in calls to device_create(). 242 * in calls to device_create().
243 * 243 *
244 * Returns &struct class pointer on success, or ERR_PTR() on error. 244 * Returns &struct class pointer on success, or ERR_PTR() on error.
245 * 245 *
246 * Note, the pointer created here is to be destroyed when finished by 246 * Note, the pointer created here is to be destroyed when finished by
247 * making a call to class_destroy(). 247 * making a call to class_destroy().
248 */ 248 */
249 struct class *__class_create(struct module *owner, const char *name, 249 struct class *__class_create(struct module *owner, const char *name,
250 struct lock_class_key *key) 250 struct lock_class_key *key)
251 { 251 {
252 struct class *cls; 252 struct class *cls;
253 int retval; 253 int retval;
254 254
255 cls = kzalloc(sizeof(*cls), GFP_KERNEL); 255 cls = kzalloc(sizeof(*cls), GFP_KERNEL);
256 if (!cls) { 256 if (!cls) {
257 retval = -ENOMEM; 257 retval = -ENOMEM;
258 goto error; 258 goto error;
259 } 259 }
260 260
261 cls->name = name; 261 cls->name = name;
262 cls->owner = owner; 262 cls->owner = owner;
263 cls->class_release = class_create_release; 263 cls->class_release = class_create_release;
264 264
265 retval = __class_register(cls, key); 265 retval = __class_register(cls, key);
266 if (retval) 266 if (retval)
267 goto error; 267 goto error;
268 268
269 return cls; 269 return cls;
270 270
271 error: 271 error:
272 kfree(cls); 272 kfree(cls);
273 return ERR_PTR(retval); 273 return ERR_PTR(retval);
274 } 274 }
275 EXPORT_SYMBOL_GPL(__class_create); 275 EXPORT_SYMBOL_GPL(__class_create);
276 276
277 /** 277 /**
278 * class_destroy - destroys a struct class structure 278 * class_destroy - destroys a struct class structure
279 * @cls: pointer to the struct class that is to be destroyed 279 * @cls: pointer to the struct class that is to be destroyed
280 * 280 *
281 * Note, the pointer to be destroyed must have been created with a call 281 * Note, the pointer to be destroyed must have been created with a call
282 * to class_create(). 282 * to class_create().
283 */ 283 */
284 void class_destroy(struct class *cls) 284 void class_destroy(struct class *cls)
285 { 285 {
286 if ((cls == NULL) || (IS_ERR(cls))) 286 if ((cls == NULL) || (IS_ERR(cls)))
287 return; 287 return;
288 288
289 class_unregister(cls); 289 class_unregister(cls);
290 } 290 }
291 291
292 /** 292 /**
293 * class_dev_iter_init - initialize class device iterator 293 * class_dev_iter_init - initialize class device iterator
294 * @iter: class iterator to initialize 294 * @iter: class iterator to initialize
295 * @class: the class we wanna iterate over 295 * @class: the class we wanna iterate over
296 * @start: the device to start iterating from, if any 296 * @start: the device to start iterating from, if any
297 * @type: device_type of the devices to iterate over, NULL for all 297 * @type: device_type of the devices to iterate over, NULL for all
298 * 298 *
299 * Initialize class iterator @iter such that it iterates over devices 299 * Initialize class iterator @iter such that it iterates over devices
300 * of @class. If @start is set, the list iteration will start there, 300 * of @class. If @start is set, the list iteration will start there,
301 * otherwise if it is NULL, the iteration starts at the beginning of 301 * otherwise if it is NULL, the iteration starts at the beginning of
302 * the list. 302 * the list.
303 */ 303 */
304 void class_dev_iter_init(struct class_dev_iter *iter, struct class *class, 304 int class_dev_iter_init(struct class_dev_iter *iter, struct class *class,
305 struct device *start, const struct device_type *type) 305 struct device *start, const struct device_type *type)
306 { 306 {
307 struct klist_node *start_knode = NULL; 307 struct klist_node *start_knode = NULL;
308 int error;
308 309
309 if (start) 310 if (start)
310 start_knode = &start->knode_class; 311 start_knode = &start->knode_class;
311 klist_iter_init_node(&class->p->klist_devices, &iter->ki, start_knode); 312 error = klist_iter_init_node(&class->p->klist_devices, &iter->ki,
312 iter->type = type; 313 start_knode);
314 if (!error)
315 iter->type = type;
316
317 return error;
313 } 318 }
314 EXPORT_SYMBOL_GPL(class_dev_iter_init); 319 EXPORT_SYMBOL_GPL(class_dev_iter_init);
315 320
316 /** 321 /**
317 * class_dev_iter_next - iterate to the next device 322 * class_dev_iter_next - iterate to the next device
318 * @iter: class iterator to proceed 323 * @iter: class iterator to proceed
319 * 324 *
320 * Proceed @iter to the next device and return it. Returns NULL if 325 * Proceed @iter to the next device and return it. Returns NULL if
321 * iteration is complete. 326 * iteration is complete.
322 * 327 *
323 * The returned device is referenced and won't be released till 328 * The returned device is referenced and won't be released till
324 * iterator is proceed to the next device or exited. The caller is 329 * iterator is proceed to the next device or exited. The caller is
325 * free to do whatever it wants to do with the device including 330 * free to do whatever it wants to do with the device including
326 * calling back into class code. 331 * calling back into class code.
327 */ 332 */
328 struct device *class_dev_iter_next(struct class_dev_iter *iter) 333 struct device *class_dev_iter_next(struct class_dev_iter *iter)
329 { 334 {
330 struct klist_node *knode; 335 struct klist_node *knode;
331 struct device *dev; 336 struct device *dev;
332 337
333 while (1) { 338 while (1) {
334 knode = klist_next(&iter->ki); 339 knode = klist_next(&iter->ki);
335 if (!knode) 340 if (!knode)
336 return NULL; 341 return NULL;
337 dev = container_of(knode, struct device, knode_class); 342 dev = container_of(knode, struct device, knode_class);
338 if (!iter->type || iter->type == dev->type) 343 if (!iter->type || iter->type == dev->type)
339 return dev; 344 return dev;
340 } 345 }
341 } 346 }
342 EXPORT_SYMBOL_GPL(class_dev_iter_next); 347 EXPORT_SYMBOL_GPL(class_dev_iter_next);
343 348
344 /** 349 /**
345 * class_dev_iter_exit - finish iteration 350 * class_dev_iter_exit - finish iteration
346 * @iter: class iterator to finish 351 * @iter: class iterator to finish
347 * 352 *
348 * Finish an iteration. Always call this function after iteration is 353 * Finish an iteration. Always call this function after iteration is
349 * complete whether the iteration ran till the end or not. 354 * complete whether the iteration ran till the end or not.
350 */ 355 */
351 void class_dev_iter_exit(struct class_dev_iter *iter) 356 void class_dev_iter_exit(struct class_dev_iter *iter)
352 { 357 {
353 klist_iter_exit(&iter->ki); 358 klist_iter_exit(&iter->ki);
354 } 359 }
355 EXPORT_SYMBOL_GPL(class_dev_iter_exit); 360 EXPORT_SYMBOL_GPL(class_dev_iter_exit);
356 361
357 /** 362 /**
358 * class_for_each_device - device iterator 363 * class_for_each_device - device iterator
359 * @class: the class we're iterating 364 * @class: the class we're iterating
360 * @start: the device to start with in the list, if any. 365 * @start: the device to start with in the list, if any.
361 * @data: data for the callback 366 * @data: data for the callback
362 * @fn: function to be called for each device 367 * @fn: function to be called for each device
363 * 368 *
364 * Iterate over @class's list of devices, and call @fn for each, 369 * Iterate over @class's list of devices, and call @fn for each,
365 * passing it @data. If @start is set, the list iteration will start 370 * passing it @data. If @start is set, the list iteration will start
366 * there, otherwise if it is NULL, the iteration starts at the 371 * there, otherwise if it is NULL, the iteration starts at the
367 * beginning of the list. 372 * beginning of the list.
368 * 373 *
369 * We check the return of @fn each time. If it returns anything 374 * We check the return of @fn each time. If it returns anything
370 * other than 0, we break out and return that value. 375 * other than 0, we break out and return that value.
371 * 376 *
372 * @fn is allowed to do anything including calling back into class 377 * @fn is allowed to do anything including calling back into class
373 * code. There's no locking restriction. 378 * code. There's no locking restriction.
374 */ 379 */
375 int class_for_each_device(struct class *class, struct device *start, 380 int class_for_each_device(struct class *class, struct device *start,
376 void *data, int (*fn)(struct device *, void *)) 381 void *data, int (*fn)(struct device *, void *))
377 { 382 {
378 struct class_dev_iter iter; 383 struct class_dev_iter iter;
379 struct device *dev; 384 struct device *dev;
380 int error = 0; 385 int error = 0;
381 386
382 if (!class) 387 if (!class)
383 return -EINVAL; 388 return -EINVAL;
384 if (!class->p) { 389 if (!class->p) {
385 WARN(1, "%s called for class '%s' before it was initialized", 390 WARN(1, "%s called for class '%s' before it was initialized",
386 __func__, class->name); 391 __func__, class->name);
387 return -EINVAL; 392 return -EINVAL;
388 } 393 }
389 394
390 class_dev_iter_init(&iter, class, start, NULL); 395 error = class_dev_iter_init(&iter, class, start, NULL);
391 while ((dev = class_dev_iter_next(&iter))) { 396 if (!error) {
392 error = fn(dev, data); 397 while ((dev = class_dev_iter_next(&iter))) {
393 if (error) 398 error = fn(dev, data);
394 break; 399 if (error)
400 break;
401 }
402 class_dev_iter_exit(&iter);
395 } 403 }
396 class_dev_iter_exit(&iter);
397
398 return error; 404 return error;
399 } 405 }
400 EXPORT_SYMBOL_GPL(class_for_each_device); 406 EXPORT_SYMBOL_GPL(class_for_each_device);
401 407
402 /** 408 /**
403 * class_find_device - device iterator for locating a particular device 409 * class_find_device - device iterator for locating a particular device
404 * @class: the class we're iterating 410 * @class: the class we're iterating
405 * @start: Device to begin with 411 * @start: Device to begin with
406 * @data: data for the match function 412 * @data: data for the match function
407 * @match: function to check device 413 * @match: function to check device
408 * 414 *
409 * This is similar to the class_for_each_dev() function above, but it 415 * This is similar to the class_for_each_dev() function above, but it
410 * returns a reference to a device that is 'found' for later use, as 416 * returns a reference to a device that is 'found' for later use, as
411 * determined by the @match callback. 417 * determined by the @match callback.
412 * 418 *
413 * The callback should return 0 if the device doesn't match and non-zero 419 * The callback should return 0 if the device doesn't match and non-zero
414 * if it does. If the callback returns non-zero, this function will 420 * if it does. If the callback returns non-zero, this function will
415 * return to the caller and not iterate over any more devices. 421 * return to the caller and not iterate over any more devices.
416 * 422 *
417 * Note, you will need to drop the reference with put_device() after use. 423 * Note, you will need to drop the reference with put_device() after use.
418 * 424 *
419 * @fn is allowed to do anything including calling back into class 425 * @fn is allowed to do anything including calling back into class
420 * code. There's no locking restriction. 426 * code. There's no locking restriction.
421 */ 427 */
422 struct device *class_find_device(struct class *class, struct device *start, 428 struct device *class_find_device(struct class *class, struct device *start,
423 void *data, 429 void *data,
424 int (*match)(struct device *, void *)) 430 int (*match)(struct device *, void *))
425 { 431 {
426 struct class_dev_iter iter; 432 struct class_dev_iter iter;
427 struct device *dev; 433 struct device *dev;
428 434
429 if (!class) 435 if (!class)
430 return NULL; 436 return NULL;
431 if (!class->p) { 437 if (!class->p) {
432 WARN(1, "%s called for class '%s' before it was initialized", 438 WARN(1, "%s called for class '%s' before it was initialized",
433 __func__, class->name); 439 __func__, class->name);
434 return NULL; 440 return NULL;
435 } 441 }
436 442
437 class_dev_iter_init(&iter, class, start, NULL); 443 if (class_dev_iter_init(&iter, class, start, NULL) < 0)
444 return NULL;
445
438 while ((dev = class_dev_iter_next(&iter))) { 446 while ((dev = class_dev_iter_next(&iter))) {
439 if (match(dev, data)) { 447 if (match(dev, data)) {
440 get_device(dev); 448 get_device(dev);
441 break; 449 break;
442 } 450 }
443 } 451 }
444 class_dev_iter_exit(&iter); 452 class_dev_iter_exit(&iter);
445 453
446 return dev; 454 return dev;
447 } 455 }
448 EXPORT_SYMBOL_GPL(class_find_device); 456 EXPORT_SYMBOL_GPL(class_find_device);
449 457
450 int class_interface_register(struct class_interface *class_intf) 458 int class_interface_register(struct class_interface *class_intf)
451 { 459 {
452 struct class *parent; 460 struct class *parent;
453 struct class_dev_iter iter; 461 struct class_dev_iter iter;
454 struct device *dev; 462 struct device *dev;
455 463
456 if (!class_intf || !class_intf->class) 464 if (!class_intf || !class_intf->class)
457 return -ENODEV; 465 return -ENODEV;
458 466
459 parent = class_get(class_intf->class); 467 parent = class_get(class_intf->class);
460 if (!parent) 468 if (!parent)
461 return -EINVAL; 469 return -EINVAL;
462 470
463 mutex_lock(&parent->p->mutex); 471 mutex_lock(&parent->p->mutex);
464 list_add_tail(&class_intf->node, &parent->p->interfaces); 472 list_add_tail(&class_intf->node, &parent->p->interfaces);
465 if (class_intf->add_dev) { 473 if (class_intf->add_dev) {
466 class_dev_iter_init(&iter, parent, NULL, NULL); 474 class_dev_iter_init(&iter, parent, NULL, NULL);
467 while ((dev = class_dev_iter_next(&iter))) 475 while ((dev = class_dev_iter_next(&iter)))
468 class_intf->add_dev(dev, class_intf); 476 class_intf->add_dev(dev, class_intf);
469 class_dev_iter_exit(&iter); 477 class_dev_iter_exit(&iter);
470 } 478 }
471 mutex_unlock(&parent->p->mutex); 479 mutex_unlock(&parent->p->mutex);
472 480
473 return 0; 481 return 0;
474 } 482 }
475 483
476 void class_interface_unregister(struct class_interface *class_intf) 484 void class_interface_unregister(struct class_interface *class_intf)
477 { 485 {
478 struct class *parent = class_intf->class; 486 struct class *parent = class_intf->class;
479 struct class_dev_iter iter; 487 struct class_dev_iter iter;
480 struct device *dev; 488 struct device *dev;
481 489
482 if (!parent) 490 if (!parent)
483 return; 491 return;
484 492
485 mutex_lock(&parent->p->mutex); 493 mutex_lock(&parent->p->mutex);
486 list_del_init(&class_intf->node); 494 list_del_init(&class_intf->node);
487 if (class_intf->remove_dev) { 495 if (class_intf->remove_dev) {
488 class_dev_iter_init(&iter, parent, NULL, NULL); 496 class_dev_iter_init(&iter, parent, NULL, NULL);
489 while ((dev = class_dev_iter_next(&iter))) 497 while ((dev = class_dev_iter_next(&iter)))
490 class_intf->remove_dev(dev, class_intf); 498 class_intf->remove_dev(dev, class_intf);
491 class_dev_iter_exit(&iter); 499 class_dev_iter_exit(&iter);
492 } 500 }
493 mutex_unlock(&parent->p->mutex); 501 mutex_unlock(&parent->p->mutex);
494 502
495 class_put(parent); 503 class_put(parent);
496 } 504 }
497 505
498 ssize_t show_class_attr_string(struct class *class, 506 ssize_t show_class_attr_string(struct class *class,
499 struct class_attribute *attr, char *buf) 507 struct class_attribute *attr, char *buf)
500 { 508 {
501 struct class_attribute_string *cs; 509 struct class_attribute_string *cs;
502 cs = container_of(attr, struct class_attribute_string, attr); 510 cs = container_of(attr, struct class_attribute_string, attr);
503 return snprintf(buf, PAGE_SIZE, "%s\n", cs->str); 511 return snprintf(buf, PAGE_SIZE, "%s\n", cs->str);
504 } 512 }
505 513
506 EXPORT_SYMBOL_GPL(show_class_attr_string); 514 EXPORT_SYMBOL_GPL(show_class_attr_string);
507 515
508 struct class_compat { 516 struct class_compat {
509 struct kobject *kobj; 517 struct kobject *kobj;
510 }; 518 };
511 519
512 /** 520 /**
513 * class_compat_register - register a compatibility class 521 * class_compat_register - register a compatibility class
514 * @name: the name of the class 522 * @name: the name of the class
515 * 523 *
516 * Compatibility class are meant as a temporary user-space compatibility 524 * Compatibility class are meant as a temporary user-space compatibility
517 * workaround when converting a family of class devices to a bus devices. 525 * workaround when converting a family of class devices to a bus devices.
518 */ 526 */
519 struct class_compat *class_compat_register(const char *name) 527 struct class_compat *class_compat_register(const char *name)
520 { 528 {
521 struct class_compat *cls; 529 struct class_compat *cls;
522 530
523 cls = kmalloc(sizeof(struct class_compat), GFP_KERNEL); 531 cls = kmalloc(sizeof(struct class_compat), GFP_KERNEL);
524 if (!cls) 532 if (!cls)
525 return NULL; 533 return NULL;
526 cls->kobj = kobject_create_and_add(name, &class_kset->kobj); 534 cls->kobj = kobject_create_and_add(name, &class_kset->kobj);
527 if (!cls->kobj) { 535 if (!cls->kobj) {
528 kfree(cls); 536 kfree(cls);
529 return NULL; 537 return NULL;
530 } 538 }
531 return cls; 539 return cls;
532 } 540 }
533 EXPORT_SYMBOL_GPL(class_compat_register); 541 EXPORT_SYMBOL_GPL(class_compat_register);
534 542
535 /** 543 /**
536 * class_compat_unregister - unregister a compatibility class 544 * class_compat_unregister - unregister a compatibility class
537 * @cls: the class to unregister 545 * @cls: the class to unregister
538 */ 546 */
539 void class_compat_unregister(struct class_compat *cls) 547 void class_compat_unregister(struct class_compat *cls)
540 { 548 {
541 kobject_put(cls->kobj); 549 kobject_put(cls->kobj);
542 kfree(cls); 550 kfree(cls);
543 } 551 }
544 EXPORT_SYMBOL_GPL(class_compat_unregister); 552 EXPORT_SYMBOL_GPL(class_compat_unregister);
545 553
546 /** 554 /**
547 * class_compat_create_link - create a compatibility class device link to 555 * class_compat_create_link - create a compatibility class device link to
548 * a bus device 556 * a bus device
549 * @cls: the compatibility class 557 * @cls: the compatibility class
550 * @dev: the target bus device 558 * @dev: the target bus device
551 * @device_link: an optional device to which a "device" link should be created 559 * @device_link: an optional device to which a "device" link should be created
552 */ 560 */
553 int class_compat_create_link(struct class_compat *cls, struct device *dev, 561 int class_compat_create_link(struct class_compat *cls, struct device *dev,
554 struct device *device_link) 562 struct device *device_link)
555 { 563 {
556 int error; 564 int error;
557 565
558 error = sysfs_create_link(cls->kobj, &dev->kobj, dev_name(dev)); 566 error = sysfs_create_link(cls->kobj, &dev->kobj, dev_name(dev));
559 if (error) 567 if (error)
560 return error; 568 return error;
561 569
562 /* 570 /*
563 * Optionally add a "device" link (typically to the parent), as a 571 * Optionally add a "device" link (typically to the parent), as a
564 * class device would have one and we want to provide as much 572 * class device would have one and we want to provide as much
565 * backwards compatibility as possible. 573 * backwards compatibility as possible.
566 */ 574 */
567 if (device_link) { 575 if (device_link) {
568 error = sysfs_create_link(&dev->kobj, &device_link->kobj, 576 error = sysfs_create_link(&dev->kobj, &device_link->kobj,
569 "device"); 577 "device");
570 if (error) 578 if (error)
571 sysfs_remove_link(cls->kobj, dev_name(dev)); 579 sysfs_remove_link(cls->kobj, dev_name(dev));
572 } 580 }
573 581
574 return error; 582 return error;
575 } 583 }
576 EXPORT_SYMBOL_GPL(class_compat_create_link); 584 EXPORT_SYMBOL_GPL(class_compat_create_link);
577 585
578 /** 586 /**
579 * class_compat_remove_link - remove a compatibility class device link to 587 * class_compat_remove_link - remove a compatibility class device link to
580 * a bus device 588 * a bus device
581 * @cls: the compatibility class 589 * @cls: the compatibility class
582 * @dev: the target bus device 590 * @dev: the target bus device
583 * @device_link: an optional device to which a "device" link was previously 591 * @device_link: an optional device to which a "device" link was previously
584 * created 592 * created
585 */ 593 */
586 void class_compat_remove_link(struct class_compat *cls, struct device *dev, 594 void class_compat_remove_link(struct class_compat *cls, struct device *dev,
587 struct device *device_link) 595 struct device *device_link)
588 { 596 {
589 if (device_link) 597 if (device_link)
590 sysfs_remove_link(&dev->kobj, "device"); 598 sysfs_remove_link(&dev->kobj, "device");
591 sysfs_remove_link(cls->kobj, dev_name(dev)); 599 sysfs_remove_link(cls->kobj, dev_name(dev));
592 } 600 }
593 EXPORT_SYMBOL_GPL(class_compat_remove_link); 601 EXPORT_SYMBOL_GPL(class_compat_remove_link);
594 602
595 int __init classes_init(void) 603 int __init classes_init(void)
596 { 604 {
597 class_kset = kset_create_and_add("class", NULL, NULL); 605 class_kset = kset_create_and_add("class", NULL, NULL);
598 if (!class_kset) 606 if (!class_kset)
599 return -ENOMEM; 607 return -ENOMEM;
600 return 0; 608 return 0;
601 } 609 }
602 610
603 EXPORT_SYMBOL_GPL(class_create_file); 611 EXPORT_SYMBOL_GPL(class_create_file);
604 EXPORT_SYMBOL_GPL(class_remove_file); 612 EXPORT_SYMBOL_GPL(class_remove_file);
605 EXPORT_SYMBOL_GPL(class_unregister); 613 EXPORT_SYMBOL_GPL(class_unregister);
606 EXPORT_SYMBOL_GPL(class_destroy); 614 EXPORT_SYMBOL_GPL(class_destroy);
607 615
608 EXPORT_SYMBOL_GPL(class_interface_register); 616 EXPORT_SYMBOL_GPL(class_interface_register);
drivers/base/driver.c
1 /* 1 /*
2 * driver.c - centralized device driver management 2 * driver.c - centralized device driver management
3 * 3 *
4 * Copyright (c) 2002-3 Patrick Mochel 4 * Copyright (c) 2002-3 Patrick Mochel
5 * Copyright (c) 2002-3 Open Source Development Labs 5 * Copyright (c) 2002-3 Open Source Development Labs
6 * Copyright (c) 2007 Greg Kroah-Hartman <gregkh@suse.de> 6 * Copyright (c) 2007 Greg Kroah-Hartman <gregkh@suse.de>
7 * Copyright (c) 2007 Novell Inc. 7 * Copyright (c) 2007 Novell Inc.
8 * 8 *
9 * This file is released under the GPLv2 9 * This file is released under the GPLv2
10 * 10 *
11 */ 11 */
12 12
13 #include <linux/device.h> 13 #include <linux/device.h>
14 #include <linux/module.h> 14 #include <linux/module.h>
15 #include <linux/errno.h> 15 #include <linux/errno.h>
16 #include <linux/slab.h> 16 #include <linux/slab.h>
17 #include <linux/string.h> 17 #include <linux/string.h>
18 #include "base.h" 18 #include "base.h"
19 19
20 static struct device *next_device(struct klist_iter *i) 20 static struct device *next_device(struct klist_iter *i)
21 { 21 {
22 struct klist_node *n = klist_next(i); 22 struct klist_node *n = klist_next(i);
23 struct device *dev = NULL; 23 struct device *dev = NULL;
24 struct device_private *dev_prv; 24 struct device_private *dev_prv;
25 25
26 if (n) { 26 if (n) {
27 dev_prv = to_device_private_driver(n); 27 dev_prv = to_device_private_driver(n);
28 dev = dev_prv->device; 28 dev = dev_prv->device;
29 } 29 }
30 return dev; 30 return dev;
31 } 31 }
32 32
33 /** 33 /**
34 * driver_for_each_device - Iterator for devices bound to a driver. 34 * driver_for_each_device - Iterator for devices bound to a driver.
35 * @drv: Driver we're iterating. 35 * @drv: Driver we're iterating.
36 * @start: Device to begin with 36 * @start: Device to begin with
37 * @data: Data to pass to the callback. 37 * @data: Data to pass to the callback.
38 * @fn: Function to call for each device. 38 * @fn: Function to call for each device.
39 * 39 *
40 * Iterate over the @drv's list of devices calling @fn for each one. 40 * Iterate over the @drv's list of devices calling @fn for each one.
41 */ 41 */
42 int driver_for_each_device(struct device_driver *drv, struct device *start, 42 int driver_for_each_device(struct device_driver *drv, struct device *start,
43 void *data, int (*fn)(struct device *, void *)) 43 void *data, int (*fn)(struct device *, void *))
44 { 44 {
45 struct klist_iter i; 45 struct klist_iter i;
46 struct device *dev; 46 struct device *dev;
47 int error = 0; 47 int error = 0;
48 48
49 if (!drv) 49 if (!drv)
50 return -EINVAL; 50 return -EINVAL;
51 51
52 klist_iter_init_node(&drv->p->klist_devices, &i, 52 error = klist_iter_init_node(&drv->p->klist_devices, &i,
53 start ? &start->p->knode_driver : NULL); 53 start ? &start->p->knode_driver : NULL);
54 while ((dev = next_device(&i)) && !error) 54 if (!error) {
55 error = fn(dev, data); 55 while ((dev = next_device(&i)) && !error)
56 klist_iter_exit(&i); 56 error = fn(dev, data);
57 klist_iter_exit(&i);
58 }
57 return error; 59 return error;
58 } 60 }
59 EXPORT_SYMBOL_GPL(driver_for_each_device); 61 EXPORT_SYMBOL_GPL(driver_for_each_device);
60 62
61 /** 63 /**
62 * driver_find_device - device iterator for locating a particular device. 64 * driver_find_device - device iterator for locating a particular device.
63 * @drv: The device's driver 65 * @drv: The device's driver
64 * @start: Device to begin with 66 * @start: Device to begin with
65 * @data: Data to pass to match function 67 * @data: Data to pass to match function
66 * @match: Callback function to check device 68 * @match: Callback function to check device
67 * 69 *
68 * This is similar to the driver_for_each_device() function above, but 70 * This is similar to the driver_for_each_device() function above, but
69 * it returns a reference to a device that is 'found' for later use, as 71 * it returns a reference to a device that is 'found' for later use, as
70 * determined by the @match callback. 72 * determined by the @match callback.
71 * 73 *
72 * The callback should return 0 if the device doesn't match and non-zero 74 * The callback should return 0 if the device doesn't match and non-zero
73 * if it does. If the callback returns non-zero, this function will 75 * if it does. If the callback returns non-zero, this function will
74 * return to the caller and not iterate over any more devices. 76 * return to the caller and not iterate over any more devices.
75 */ 77 */
76 struct device *driver_find_device(struct device_driver *drv, 78 struct device *driver_find_device(struct device_driver *drv,
77 struct device *start, void *data, 79 struct device *start, void *data,
78 int (*match)(struct device *dev, void *data)) 80 int (*match)(struct device *dev, void *data))
79 { 81 {
80 struct klist_iter i; 82 struct klist_iter i;
81 struct device *dev; 83 struct device *dev;
82 84
83 if (!drv) 85 if (!drv)
84 return NULL; 86 return NULL;
85 87
86 klist_iter_init_node(&drv->p->klist_devices, &i, 88 if (klist_iter_init_node(&drv->p->klist_devices, &i,
87 (start ? &start->p->knode_driver : NULL)); 89 (start ? &start->p->knode_driver : NULL)) < 0)
90 return NULL;
91
88 while ((dev = next_device(&i))) 92 while ((dev = next_device(&i)))
89 if (match(dev, data) && get_device(dev)) 93 if (match(dev, data) && get_device(dev))
90 break; 94 break;
91 klist_iter_exit(&i); 95 klist_iter_exit(&i);
92 return dev; 96 return dev;
93 } 97 }
94 EXPORT_SYMBOL_GPL(driver_find_device); 98 EXPORT_SYMBOL_GPL(driver_find_device);
95 99
96 /** 100 /**
97 * driver_create_file - create sysfs file for driver. 101 * driver_create_file - create sysfs file for driver.
98 * @drv: driver. 102 * @drv: driver.
99 * @attr: driver attribute descriptor. 103 * @attr: driver attribute descriptor.
100 */ 104 */
101 int driver_create_file(struct device_driver *drv, 105 int driver_create_file(struct device_driver *drv,
102 const struct driver_attribute *attr) 106 const struct driver_attribute *attr)
103 { 107 {
104 int error; 108 int error;
105 if (drv) 109 if (drv)
106 error = sysfs_create_file(&drv->p->kobj, &attr->attr); 110 error = sysfs_create_file(&drv->p->kobj, &attr->attr);
107 else 111 else
108 error = -EINVAL; 112 error = -EINVAL;
109 return error; 113 return error;
110 } 114 }
111 EXPORT_SYMBOL_GPL(driver_create_file); 115 EXPORT_SYMBOL_GPL(driver_create_file);
112 116
113 /** 117 /**
114 * driver_remove_file - remove sysfs file for driver. 118 * driver_remove_file - remove sysfs file for driver.
115 * @drv: driver. 119 * @drv: driver.
116 * @attr: driver attribute descriptor. 120 * @attr: driver attribute descriptor.
117 */ 121 */
118 void driver_remove_file(struct device_driver *drv, 122 void driver_remove_file(struct device_driver *drv,
119 const struct driver_attribute *attr) 123 const struct driver_attribute *attr)
120 { 124 {
121 if (drv) 125 if (drv)
122 sysfs_remove_file(&drv->p->kobj, &attr->attr); 126 sysfs_remove_file(&drv->p->kobj, &attr->attr);
123 } 127 }
124 EXPORT_SYMBOL_GPL(driver_remove_file); 128 EXPORT_SYMBOL_GPL(driver_remove_file);
125 129
126 static int driver_add_groups(struct device_driver *drv, 130 static int driver_add_groups(struct device_driver *drv,
127 const struct attribute_group **groups) 131 const struct attribute_group **groups)
128 { 132 {
129 int error = 0; 133 int error = 0;
130 int i; 134 int i;
131 135
132 if (groups) { 136 if (groups) {
133 for (i = 0; groups[i]; i++) { 137 for (i = 0; groups[i]; i++) {
134 error = sysfs_create_group(&drv->p->kobj, groups[i]); 138 error = sysfs_create_group(&drv->p->kobj, groups[i]);
135 if (error) { 139 if (error) {
136 while (--i >= 0) 140 while (--i >= 0)
137 sysfs_remove_group(&drv->p->kobj, 141 sysfs_remove_group(&drv->p->kobj,
138 groups[i]); 142 groups[i]);
139 break; 143 break;
140 } 144 }
141 } 145 }
142 } 146 }
143 return error; 147 return error;
144 } 148 }
145 149
146 static void driver_remove_groups(struct device_driver *drv, 150 static void driver_remove_groups(struct device_driver *drv,
147 const struct attribute_group **groups) 151 const struct attribute_group **groups)
148 { 152 {
149 int i; 153 int i;
150 154
151 if (groups) 155 if (groups)
152 for (i = 0; groups[i]; i++) 156 for (i = 0; groups[i]; i++)
153 sysfs_remove_group(&drv->p->kobj, groups[i]); 157 sysfs_remove_group(&drv->p->kobj, groups[i]);
154 } 158 }
155 159
156 /** 160 /**
157 * driver_register - register driver with bus 161 * driver_register - register driver with bus
158 * @drv: driver to register 162 * @drv: driver to register
159 * 163 *
160 * We pass off most of the work to the bus_add_driver() call, 164 * We pass off most of the work to the bus_add_driver() call,
161 * since most of the things we have to do deal with the bus 165 * since most of the things we have to do deal with the bus
162 * structures. 166 * structures.
163 */ 167 */
164 int driver_register(struct device_driver *drv) 168 int driver_register(struct device_driver *drv)
165 { 169 {
166 int ret; 170 int ret;
167 struct device_driver *other; 171 struct device_driver *other;
168 172
169 BUG_ON(!drv->bus->p); 173 BUG_ON(!drv->bus->p);
170 174
171 if ((drv->bus->probe && drv->probe) || 175 if ((drv->bus->probe && drv->probe) ||
172 (drv->bus->remove && drv->remove) || 176 (drv->bus->remove && drv->remove) ||
173 (drv->bus->shutdown && drv->shutdown)) 177 (drv->bus->shutdown && drv->shutdown))
174 printk(KERN_WARNING "Driver '%s' needs updating - please use " 178 printk(KERN_WARNING "Driver '%s' needs updating - please use "
175 "bus_type methods\n", drv->name); 179 "bus_type methods\n", drv->name);
176 180
177 other = driver_find(drv->name, drv->bus); 181 other = driver_find(drv->name, drv->bus);
178 if (other) { 182 if (other) {
179 printk(KERN_ERR "Error: Driver '%s' is already registered, " 183 printk(KERN_ERR "Error: Driver '%s' is already registered, "
180 "aborting...\n", drv->name); 184 "aborting...\n", drv->name);
181 return -EBUSY; 185 return -EBUSY;
182 } 186 }
183 187
184 ret = bus_add_driver(drv); 188 ret = bus_add_driver(drv);
185 if (ret) 189 if (ret)
186 return ret; 190 return ret;
187 ret = driver_add_groups(drv, drv->groups); 191 ret = driver_add_groups(drv, drv->groups);
188 if (ret) 192 if (ret)
189 bus_remove_driver(drv); 193 bus_remove_driver(drv);
190 return ret; 194 return ret;
191 } 195 }
192 EXPORT_SYMBOL_GPL(driver_register); 196 EXPORT_SYMBOL_GPL(driver_register);
193 197
194 /** 198 /**
195 * driver_unregister - remove driver from system. 199 * driver_unregister - remove driver from system.
196 * @drv: driver. 200 * @drv: driver.
197 * 201 *
198 * Again, we pass off most of the work to the bus-level call. 202 * Again, we pass off most of the work to the bus-level call.
199 */ 203 */
200 void driver_unregister(struct device_driver *drv) 204 void driver_unregister(struct device_driver *drv)
201 { 205 {
202 if (!drv || !drv->p) { 206 if (!drv || !drv->p) {
203 WARN(1, "Unexpected driver unregister!\n"); 207 WARN(1, "Unexpected driver unregister!\n");
204 return; 208 return;
205 } 209 }
206 driver_remove_groups(drv, drv->groups); 210 driver_remove_groups(drv, drv->groups);
207 bus_remove_driver(drv); 211 bus_remove_driver(drv);
208 } 212 }
209 EXPORT_SYMBOL_GPL(driver_unregister); 213 EXPORT_SYMBOL_GPL(driver_unregister);
210 214
211 /** 215 /**
212 * driver_find - locate driver on a bus by its name. 216 * driver_find - locate driver on a bus by its name.
213 * @name: name of the driver. 217 * @name: name of the driver.
214 * @bus: bus to scan for the driver. 218 * @bus: bus to scan for the driver.
215 * 219 *
216 * Call kset_find_obj() to iterate over list of drivers on 220 * Call kset_find_obj() to iterate over list of drivers on
217 * a bus to find driver by name. Return driver if found. 221 * a bus to find driver by name. Return driver if found.
218 * 222 *
219 * This routine provides no locking to prevent the driver it returns 223 * This routine provides no locking to prevent the driver it returns
220 * from being unregistered or unloaded while the caller is using it. 224 * from being unregistered or unloaded while the caller is using it.
221 * The caller is responsible for preventing this. 225 * The caller is responsible for preventing this.
222 */ 226 */
223 struct device_driver *driver_find(const char *name, struct bus_type *bus) 227 struct device_driver *driver_find(const char *name, struct bus_type *bus)
224 { 228 {
225 struct kobject *k = kset_find_obj(bus->p->drivers_kset, name); 229 struct kobject *k = kset_find_obj(bus->p->drivers_kset, name);
226 struct driver_private *priv; 230 struct driver_private *priv;
227 231
228 if (k) { 232 if (k) {
229 /* Drop reference added by kset_find_obj() */ 233 /* Drop reference added by kset_find_obj() */
230 kobject_put(k); 234 kobject_put(k);
231 priv = to_driver(k); 235 priv = to_driver(k);
232 return priv->driver; 236 return priv->driver;
233 } 237 }
234 return NULL; 238 return NULL;
235 } 239 }
236 EXPORT_SYMBOL_GPL(driver_find); 240 EXPORT_SYMBOL_GPL(driver_find);
237 241
include/linux/device.h
1 /* 1 /*
2 * device.h - generic, centralized driver model 2 * device.h - generic, centralized driver model
3 * 3 *
4 * Copyright (c) 2001-2003 Patrick Mochel <mochel@osdl.org> 4 * Copyright (c) 2001-2003 Patrick Mochel <mochel@osdl.org>
5 * Copyright (c) 2004-2009 Greg Kroah-Hartman <gregkh@suse.de> 5 * Copyright (c) 2004-2009 Greg Kroah-Hartman <gregkh@suse.de>
6 * Copyright (c) 2008-2009 Novell Inc. 6 * Copyright (c) 2008-2009 Novell Inc.
7 * 7 *
8 * This file is released under the GPLv2 8 * This file is released under the GPLv2
9 * 9 *
10 * See Documentation/driver-model/ for more information. 10 * See Documentation/driver-model/ for more information.
11 */ 11 */
12 12
13 #ifndef _DEVICE_H_ 13 #ifndef _DEVICE_H_
14 #define _DEVICE_H_ 14 #define _DEVICE_H_
15 15
16 #include <linux/ioport.h> 16 #include <linux/ioport.h>
17 #include <linux/kobject.h> 17 #include <linux/kobject.h>
18 #include <linux/klist.h> 18 #include <linux/klist.h>
19 #include <linux/list.h> 19 #include <linux/list.h>
20 #include <linux/lockdep.h> 20 #include <linux/lockdep.h>
21 #include <linux/compiler.h> 21 #include <linux/compiler.h>
22 #include <linux/types.h> 22 #include <linux/types.h>
23 #include <linux/mutex.h> 23 #include <linux/mutex.h>
24 #include <linux/pm.h> 24 #include <linux/pm.h>
25 #include <linux/atomic.h> 25 #include <linux/atomic.h>
26 #include <asm/device.h> 26 #include <asm/device.h>
27 27
28 struct device; 28 struct device;
29 struct device_private; 29 struct device_private;
30 struct device_driver; 30 struct device_driver;
31 struct driver_private; 31 struct driver_private;
32 struct module; 32 struct module;
33 struct class; 33 struct class;
34 struct subsys_private; 34 struct subsys_private;
35 struct bus_type; 35 struct bus_type;
36 struct device_node; 36 struct device_node;
37 struct iommu_ops; 37 struct iommu_ops;
38 38
39 struct bus_attribute { 39 struct bus_attribute {
40 struct attribute attr; 40 struct attribute attr;
41 ssize_t (*show)(struct bus_type *bus, char *buf); 41 ssize_t (*show)(struct bus_type *bus, char *buf);
42 ssize_t (*store)(struct bus_type *bus, const char *buf, size_t count); 42 ssize_t (*store)(struct bus_type *bus, const char *buf, size_t count);
43 }; 43 };
44 44
45 #define BUS_ATTR(_name, _mode, _show, _store) \ 45 #define BUS_ATTR(_name, _mode, _show, _store) \
46 struct bus_attribute bus_attr_##_name = __ATTR(_name, _mode, _show, _store) 46 struct bus_attribute bus_attr_##_name = __ATTR(_name, _mode, _show, _store)
47 47
48 extern int __must_check bus_create_file(struct bus_type *, 48 extern int __must_check bus_create_file(struct bus_type *,
49 struct bus_attribute *); 49 struct bus_attribute *);
50 extern void bus_remove_file(struct bus_type *, struct bus_attribute *); 50 extern void bus_remove_file(struct bus_type *, struct bus_attribute *);
51 51
52 /** 52 /**
53 * struct bus_type - The bus type of the device 53 * struct bus_type - The bus type of the device
54 * 54 *
55 * @name: The name of the bus. 55 * @name: The name of the bus.
56 * @dev_name: Used for subsystems to enumerate devices like ("foo%u", dev->id). 56 * @dev_name: Used for subsystems to enumerate devices like ("foo%u", dev->id).
57 * @dev_root: Default device to use as the parent. 57 * @dev_root: Default device to use as the parent.
58 * @bus_attrs: Default attributes of the bus. 58 * @bus_attrs: Default attributes of the bus.
59 * @dev_attrs: Default attributes of the devices on the bus. 59 * @dev_attrs: Default attributes of the devices on the bus.
60 * @drv_attrs: Default attributes of the device drivers on the bus. 60 * @drv_attrs: Default attributes of the device drivers on the bus.
61 * @match: Called, perhaps multiple times, whenever a new device or driver 61 * @match: Called, perhaps multiple times, whenever a new device or driver
62 * is added for this bus. It should return a nonzero value if the 62 * is added for this bus. It should return a nonzero value if the
63 * given device can be handled by the given driver. 63 * given device can be handled by the given driver.
64 * @uevent: Called when a device is added, removed, or a few other things 64 * @uevent: Called when a device is added, removed, or a few other things
65 * that generate uevents to add the environment variables. 65 * that generate uevents to add the environment variables.
66 * @probe: Called when a new device or driver add to this bus, and callback 66 * @probe: Called when a new device or driver add to this bus, and callback
67 * the specific driver's probe to initial the matched device. 67 * the specific driver's probe to initial the matched device.
68 * @remove: Called when a device removed from this bus. 68 * @remove: Called when a device removed from this bus.
69 * @shutdown: Called at shut-down time to quiesce the device. 69 * @shutdown: Called at shut-down time to quiesce the device.
70 * @suspend: Called when a device on this bus wants to go to sleep mode. 70 * @suspend: Called when a device on this bus wants to go to sleep mode.
71 * @resume: Called to bring a device on this bus out of sleep mode. 71 * @resume: Called to bring a device on this bus out of sleep mode.
72 * @pm: Power management operations of this bus, callback the specific 72 * @pm: Power management operations of this bus, callback the specific
73 * device driver's pm-ops. 73 * device driver's pm-ops.
74 * @iommu_ops: IOMMU specific operations for this bus, used to attach IOMMU 74 * @iommu_ops: IOMMU specific operations for this bus, used to attach IOMMU
75 * driver implementations to a bus and allow the driver to do 75 * driver implementations to a bus and allow the driver to do
76 * bus-specific setup 76 * bus-specific setup
77 * @p: The private data of the driver core, only the driver core can 77 * @p: The private data of the driver core, only the driver core can
78 * touch this. 78 * touch this.
79 * 79 *
80 * A bus is a channel between the processor and one or more devices. For the 80 * A bus is a channel between the processor and one or more devices. For the
81 * purposes of the device model, all devices are connected via a bus, even if 81 * purposes of the device model, all devices are connected via a bus, even if
82 * it is an internal, virtual, "platform" bus. Buses can plug into each other. 82 * it is an internal, virtual, "platform" bus. Buses can plug into each other.
83 * A USB controller is usually a PCI device, for example. The device model 83 * A USB controller is usually a PCI device, for example. The device model
84 * represents the actual connections between buses and the devices they control. 84 * represents the actual connections between buses and the devices they control.
85 * A bus is represented by the bus_type structure. It contains the name, the 85 * A bus is represented by the bus_type structure. It contains the name, the
86 * default attributes, the bus' methods, PM operations, and the driver core's 86 * default attributes, the bus' methods, PM operations, and the driver core's
87 * private data. 87 * private data.
88 */ 88 */
89 struct bus_type { 89 struct bus_type {
90 const char *name; 90 const char *name;
91 const char *dev_name; 91 const char *dev_name;
92 struct device *dev_root; 92 struct device *dev_root;
93 struct bus_attribute *bus_attrs; 93 struct bus_attribute *bus_attrs;
94 struct device_attribute *dev_attrs; 94 struct device_attribute *dev_attrs;
95 struct driver_attribute *drv_attrs; 95 struct driver_attribute *drv_attrs;
96 96
97 int (*match)(struct device *dev, struct device_driver *drv); 97 int (*match)(struct device *dev, struct device_driver *drv);
98 int (*uevent)(struct device *dev, struct kobj_uevent_env *env); 98 int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
99 int (*probe)(struct device *dev); 99 int (*probe)(struct device *dev);
100 int (*remove)(struct device *dev); 100 int (*remove)(struct device *dev);
101 void (*shutdown)(struct device *dev); 101 void (*shutdown)(struct device *dev);
102 102
103 int (*suspend)(struct device *dev, pm_message_t state); 103 int (*suspend)(struct device *dev, pm_message_t state);
104 int (*resume)(struct device *dev); 104 int (*resume)(struct device *dev);
105 105
106 const struct dev_pm_ops *pm; 106 const struct dev_pm_ops *pm;
107 107
108 struct iommu_ops *iommu_ops; 108 struct iommu_ops *iommu_ops;
109 109
110 struct subsys_private *p; 110 struct subsys_private *p;
111 }; 111 };
112 112
113 /* This is a #define to keep the compiler from merging different 113 /* This is a #define to keep the compiler from merging different
114 * instances of the __key variable */ 114 * instances of the __key variable */
115 #define bus_register(subsys) \ 115 #define bus_register(subsys) \
116 ({ \ 116 ({ \
117 static struct lock_class_key __key; \ 117 static struct lock_class_key __key; \
118 __bus_register(subsys, &__key); \ 118 __bus_register(subsys, &__key); \
119 }) 119 })
120 extern int __must_check __bus_register(struct bus_type *bus, 120 extern int __must_check __bus_register(struct bus_type *bus,
121 struct lock_class_key *key); 121 struct lock_class_key *key);
122 extern void bus_unregister(struct bus_type *bus); 122 extern void bus_unregister(struct bus_type *bus);
123 123
124 extern int __must_check bus_rescan_devices(struct bus_type *bus); 124 extern int __must_check bus_rescan_devices(struct bus_type *bus);
125 125
126 /* iterator helpers for buses */ 126 /* iterator helpers for buses */
127 struct subsys_dev_iter { 127 struct subsys_dev_iter {
128 struct klist_iter ki; 128 struct klist_iter ki;
129 const struct device_type *type; 129 const struct device_type *type;
130 }; 130 };
131 void subsys_dev_iter_init(struct subsys_dev_iter *iter, 131 int subsys_dev_iter_init(struct subsys_dev_iter *iter,
132 struct bus_type *subsys, 132 struct bus_type *subsys,
133 struct device *start, 133 struct device *start,
134 const struct device_type *type); 134 const struct device_type *type);
135 struct device *subsys_dev_iter_next(struct subsys_dev_iter *iter); 135 struct device *subsys_dev_iter_next(struct subsys_dev_iter *iter);
136 void subsys_dev_iter_exit(struct subsys_dev_iter *iter); 136 void subsys_dev_iter_exit(struct subsys_dev_iter *iter);
137 137
138 int bus_for_each_dev(struct bus_type *bus, struct device *start, void *data, 138 int bus_for_each_dev(struct bus_type *bus, struct device *start, void *data,
139 int (*fn)(struct device *dev, void *data)); 139 int (*fn)(struct device *dev, void *data));
140 struct device *bus_find_device(struct bus_type *bus, struct device *start, 140 struct device *bus_find_device(struct bus_type *bus, struct device *start,
141 void *data, 141 void *data,
142 int (*match)(struct device *dev, void *data)); 142 int (*match)(struct device *dev, void *data));
143 struct device *bus_find_device_by_name(struct bus_type *bus, 143 struct device *bus_find_device_by_name(struct bus_type *bus,
144 struct device *start, 144 struct device *start,
145 const char *name); 145 const char *name);
146 struct device *subsys_find_device_by_id(struct bus_type *bus, unsigned int id, 146 struct device *subsys_find_device_by_id(struct bus_type *bus, unsigned int id,
147 struct device *hint); 147 struct device *hint);
148 int bus_for_each_drv(struct bus_type *bus, struct device_driver *start, 148 int bus_for_each_drv(struct bus_type *bus, struct device_driver *start,
149 void *data, int (*fn)(struct device_driver *, void *)); 149 void *data, int (*fn)(struct device_driver *, void *));
150 void bus_sort_breadthfirst(struct bus_type *bus, 150 void bus_sort_breadthfirst(struct bus_type *bus,
151 int (*compare)(const struct device *a, 151 int (*compare)(const struct device *a,
152 const struct device *b)); 152 const struct device *b));
153 /* 153 /*
154 * Bus notifiers: Get notified of addition/removal of devices 154 * Bus notifiers: Get notified of addition/removal of devices
155 * and binding/unbinding of drivers to devices. 155 * and binding/unbinding of drivers to devices.
156 * In the long run, it should be a replacement for the platform 156 * In the long run, it should be a replacement for the platform
157 * notify hooks. 157 * notify hooks.
158 */ 158 */
159 struct notifier_block; 159 struct notifier_block;
160 160
161 extern int bus_register_notifier(struct bus_type *bus, 161 extern int bus_register_notifier(struct bus_type *bus,
162 struct notifier_block *nb); 162 struct notifier_block *nb);
163 extern int bus_unregister_notifier(struct bus_type *bus, 163 extern int bus_unregister_notifier(struct bus_type *bus,
164 struct notifier_block *nb); 164 struct notifier_block *nb);
165 165
166 /* All 4 notifers below get called with the target struct device * 166 /* All 4 notifers below get called with the target struct device *
167 * as an argument. Note that those functions are likely to be called 167 * as an argument. Note that those functions are likely to be called
168 * with the device lock held in the core, so be careful. 168 * with the device lock held in the core, so be careful.
169 */ 169 */
170 #define BUS_NOTIFY_ADD_DEVICE 0x00000001 /* device added */ 170 #define BUS_NOTIFY_ADD_DEVICE 0x00000001 /* device added */
171 #define BUS_NOTIFY_DEL_DEVICE 0x00000002 /* device removed */ 171 #define BUS_NOTIFY_DEL_DEVICE 0x00000002 /* device removed */
172 #define BUS_NOTIFY_BIND_DRIVER 0x00000003 /* driver about to be 172 #define BUS_NOTIFY_BIND_DRIVER 0x00000003 /* driver about to be
173 bound */ 173 bound */
174 #define BUS_NOTIFY_BOUND_DRIVER 0x00000004 /* driver bound to device */ 174 #define BUS_NOTIFY_BOUND_DRIVER 0x00000004 /* driver bound to device */
175 #define BUS_NOTIFY_UNBIND_DRIVER 0x00000005 /* driver about to be 175 #define BUS_NOTIFY_UNBIND_DRIVER 0x00000005 /* driver about to be
176 unbound */ 176 unbound */
177 #define BUS_NOTIFY_UNBOUND_DRIVER 0x00000006 /* driver is unbound 177 #define BUS_NOTIFY_UNBOUND_DRIVER 0x00000006 /* driver is unbound
178 from the device */ 178 from the device */
179 179
180 extern struct kset *bus_get_kset(struct bus_type *bus); 180 extern struct kset *bus_get_kset(struct bus_type *bus);
181 extern struct klist *bus_get_device_klist(struct bus_type *bus); 181 extern struct klist *bus_get_device_klist(struct bus_type *bus);
182 182
183 /** 183 /**
184 * struct device_driver - The basic device driver structure 184 * struct device_driver - The basic device driver structure
185 * @name: Name of the device driver. 185 * @name: Name of the device driver.
186 * @bus: The bus which the device of this driver belongs to. 186 * @bus: The bus which the device of this driver belongs to.
187 * @owner: The module owner. 187 * @owner: The module owner.
188 * @mod_name: Used for built-in modules. 188 * @mod_name: Used for built-in modules.
189 * @suppress_bind_attrs: Disables bind/unbind via sysfs. 189 * @suppress_bind_attrs: Disables bind/unbind via sysfs.
190 * @of_match_table: The open firmware table. 190 * @of_match_table: The open firmware table.
191 * @probe: Called to query the existence of a specific device, 191 * @probe: Called to query the existence of a specific device,
192 * whether this driver can work with it, and bind the driver 192 * whether this driver can work with it, and bind the driver
193 * to a specific device. 193 * to a specific device.
194 * @remove: Called when the device is removed from the system to 194 * @remove: Called when the device is removed from the system to
195 * unbind a device from this driver. 195 * unbind a device from this driver.
196 * @shutdown: Called at shut-down time to quiesce the device. 196 * @shutdown: Called at shut-down time to quiesce the device.
197 * @suspend: Called to put the device to sleep mode. Usually to a 197 * @suspend: Called to put the device to sleep mode. Usually to a
198 * low power state. 198 * low power state.
199 * @resume: Called to bring a device from sleep mode. 199 * @resume: Called to bring a device from sleep mode.
200 * @groups: Default attributes that get created by the driver core 200 * @groups: Default attributes that get created by the driver core
201 * automatically. 201 * automatically.
202 * @pm: Power management operations of the device which matched 202 * @pm: Power management operations of the device which matched
203 * this driver. 203 * this driver.
204 * @p: Driver core's private data, no one other than the driver 204 * @p: Driver core's private data, no one other than the driver
205 * core can touch this. 205 * core can touch this.
206 * 206 *
207 * The device driver-model tracks all of the drivers known to the system. 207 * The device driver-model tracks all of the drivers known to the system.
208 * The main reason for this tracking is to enable the driver core to match 208 * The main reason for this tracking is to enable the driver core to match
209 * up drivers with new devices. Once drivers are known objects within the 209 * up drivers with new devices. Once drivers are known objects within the
210 * system, however, a number of other things become possible. Device drivers 210 * system, however, a number of other things become possible. Device drivers
211 * can export information and configuration variables that are independent 211 * can export information and configuration variables that are independent
212 * of any specific device. 212 * of any specific device.
213 */ 213 */
214 struct device_driver { 214 struct device_driver {
215 const char *name; 215 const char *name;
216 struct bus_type *bus; 216 struct bus_type *bus;
217 217
218 struct module *owner; 218 struct module *owner;
219 const char *mod_name; /* used for built-in modules */ 219 const char *mod_name; /* used for built-in modules */
220 220
221 bool suppress_bind_attrs; /* disables bind/unbind via sysfs */ 221 bool suppress_bind_attrs; /* disables bind/unbind via sysfs */
222 222
223 const struct of_device_id *of_match_table; 223 const struct of_device_id *of_match_table;
224 224
225 int (*probe) (struct device *dev); 225 int (*probe) (struct device *dev);
226 int (*remove) (struct device *dev); 226 int (*remove) (struct device *dev);
227 void (*shutdown) (struct device *dev); 227 void (*shutdown) (struct device *dev);
228 int (*suspend) (struct device *dev, pm_message_t state); 228 int (*suspend) (struct device *dev, pm_message_t state);
229 int (*resume) (struct device *dev); 229 int (*resume) (struct device *dev);
230 const struct attribute_group **groups; 230 const struct attribute_group **groups;
231 231
232 const struct dev_pm_ops *pm; 232 const struct dev_pm_ops *pm;
233 233
234 struct driver_private *p; 234 struct driver_private *p;
235 }; 235 };
236 236
237 237
238 extern int __must_check driver_register(struct device_driver *drv); 238 extern int __must_check driver_register(struct device_driver *drv);
239 extern void driver_unregister(struct device_driver *drv); 239 extern void driver_unregister(struct device_driver *drv);
240 240
241 extern struct device_driver *driver_find(const char *name, 241 extern struct device_driver *driver_find(const char *name,
242 struct bus_type *bus); 242 struct bus_type *bus);
243 extern int driver_probe_done(void); 243 extern int driver_probe_done(void);
244 extern void wait_for_device_probe(void); 244 extern void wait_for_device_probe(void);
245 245
246 246
247 /* sysfs interface for exporting driver attributes */ 247 /* sysfs interface for exporting driver attributes */
248 248
249 struct driver_attribute { 249 struct driver_attribute {
250 struct attribute attr; 250 struct attribute attr;
251 ssize_t (*show)(struct device_driver *driver, char *buf); 251 ssize_t (*show)(struct device_driver *driver, char *buf);
252 ssize_t (*store)(struct device_driver *driver, const char *buf, 252 ssize_t (*store)(struct device_driver *driver, const char *buf,
253 size_t count); 253 size_t count);
254 }; 254 };
255 255
256 #define DRIVER_ATTR(_name, _mode, _show, _store) \ 256 #define DRIVER_ATTR(_name, _mode, _show, _store) \
257 struct driver_attribute driver_attr_##_name = \ 257 struct driver_attribute driver_attr_##_name = \
258 __ATTR(_name, _mode, _show, _store) 258 __ATTR(_name, _mode, _show, _store)
259 259
260 extern int __must_check driver_create_file(struct device_driver *driver, 260 extern int __must_check driver_create_file(struct device_driver *driver,
261 const struct driver_attribute *attr); 261 const struct driver_attribute *attr);
262 extern void driver_remove_file(struct device_driver *driver, 262 extern void driver_remove_file(struct device_driver *driver,
263 const struct driver_attribute *attr); 263 const struct driver_attribute *attr);
264 264
265 extern int __must_check driver_for_each_device(struct device_driver *drv, 265 extern int __must_check driver_for_each_device(struct device_driver *drv,
266 struct device *start, 266 struct device *start,
267 void *data, 267 void *data,
268 int (*fn)(struct device *dev, 268 int (*fn)(struct device *dev,
269 void *)); 269 void *));
270 struct device *driver_find_device(struct device_driver *drv, 270 struct device *driver_find_device(struct device_driver *drv,
271 struct device *start, void *data, 271 struct device *start, void *data,
272 int (*match)(struct device *dev, void *data)); 272 int (*match)(struct device *dev, void *data));
273 273
274 /** 274 /**
275 * struct subsys_interface - interfaces to device functions 275 * struct subsys_interface - interfaces to device functions
276 * @name: name of the device function 276 * @name: name of the device function
277 * @subsys: subsytem of the devices to attach to 277 * @subsys: subsytem of the devices to attach to
278 * @node: the list of functions registered at the subsystem 278 * @node: the list of functions registered at the subsystem
279 * @add_dev: device hookup to device function handler 279 * @add_dev: device hookup to device function handler
280 * @remove_dev: device hookup to device function handler 280 * @remove_dev: device hookup to device function handler
281 * 281 *
282 * Simple interfaces attached to a subsystem. Multiple interfaces can 282 * Simple interfaces attached to a subsystem. Multiple interfaces can
283 * attach to a subsystem and its devices. Unlike drivers, they do not 283 * attach to a subsystem and its devices. Unlike drivers, they do not
284 * exclusively claim or control devices. Interfaces usually represent 284 * exclusively claim or control devices. Interfaces usually represent
285 * a specific functionality of a subsystem/class of devices. 285 * a specific functionality of a subsystem/class of devices.
286 */ 286 */
287 struct subsys_interface { 287 struct subsys_interface {
288 const char *name; 288 const char *name;
289 struct bus_type *subsys; 289 struct bus_type *subsys;
290 struct list_head node; 290 struct list_head node;
291 int (*add_dev)(struct device *dev, struct subsys_interface *sif); 291 int (*add_dev)(struct device *dev, struct subsys_interface *sif);
292 int (*remove_dev)(struct device *dev, struct subsys_interface *sif); 292 int (*remove_dev)(struct device *dev, struct subsys_interface *sif);
293 }; 293 };
294 294
295 int subsys_interface_register(struct subsys_interface *sif); 295 int subsys_interface_register(struct subsys_interface *sif);
296 void subsys_interface_unregister(struct subsys_interface *sif); 296 void subsys_interface_unregister(struct subsys_interface *sif);
297 297
298 int subsys_system_register(struct bus_type *subsys, 298 int subsys_system_register(struct bus_type *subsys,
299 const struct attribute_group **groups); 299 const struct attribute_group **groups);
300 300
301 /** 301 /**
302 * struct class - device classes 302 * struct class - device classes
303 * @name: Name of the class. 303 * @name: Name of the class.
304 * @owner: The module owner. 304 * @owner: The module owner.
305 * @class_attrs: Default attributes of this class. 305 * @class_attrs: Default attributes of this class.
306 * @dev_attrs: Default attributes of the devices belong to the class. 306 * @dev_attrs: Default attributes of the devices belong to the class.
307 * @dev_bin_attrs: Default binary attributes of the devices belong to the class. 307 * @dev_bin_attrs: Default binary attributes of the devices belong to the class.
308 * @dev_kobj: The kobject that represents this class and links it into the hierarchy. 308 * @dev_kobj: The kobject that represents this class and links it into the hierarchy.
309 * @dev_uevent: Called when a device is added, removed from this class, or a 309 * @dev_uevent: Called when a device is added, removed from this class, or a
310 * few other things that generate uevents to add the environment 310 * few other things that generate uevents to add the environment
311 * variables. 311 * variables.
312 * @devnode: Callback to provide the devtmpfs. 312 * @devnode: Callback to provide the devtmpfs.
313 * @class_release: Called to release this class. 313 * @class_release: Called to release this class.
314 * @dev_release: Called to release the device. 314 * @dev_release: Called to release the device.
315 * @suspend: Used to put the device to sleep mode, usually to a low power 315 * @suspend: Used to put the device to sleep mode, usually to a low power
316 * state. 316 * state.
317 * @resume: Used to bring the device from the sleep mode. 317 * @resume: Used to bring the device from the sleep mode.
318 * @ns_type: Callbacks so sysfs can detemine namespaces. 318 * @ns_type: Callbacks so sysfs can detemine namespaces.
319 * @namespace: Namespace of the device belongs to this class. 319 * @namespace: Namespace of the device belongs to this class.
320 * @pm: The default device power management operations of this class. 320 * @pm: The default device power management operations of this class.
321 * @p: The private data of the driver core, no one other than the 321 * @p: The private data of the driver core, no one other than the
322 * driver core can touch this. 322 * driver core can touch this.
323 * 323 *
324 * A class is a higher-level view of a device that abstracts out low-level 324 * A class is a higher-level view of a device that abstracts out low-level
325 * implementation details. Drivers may see a SCSI disk or an ATA disk, but, 325 * implementation details. Drivers may see a SCSI disk or an ATA disk, but,
326 * at the class level, they are all simply disks. Classes allow user space 326 * at the class level, they are all simply disks. Classes allow user space
327 * to work with devices based on what they do, rather than how they are 327 * to work with devices based on what they do, rather than how they are
328 * connected or how they work. 328 * connected or how they work.
329 */ 329 */
330 struct class { 330 struct class {
331 const char *name; 331 const char *name;
332 struct module *owner; 332 struct module *owner;
333 333
334 struct class_attribute *class_attrs; 334 struct class_attribute *class_attrs;
335 struct device_attribute *dev_attrs; 335 struct device_attribute *dev_attrs;
336 struct bin_attribute *dev_bin_attrs; 336 struct bin_attribute *dev_bin_attrs;
337 struct kobject *dev_kobj; 337 struct kobject *dev_kobj;
338 338
339 int (*dev_uevent)(struct device *dev, struct kobj_uevent_env *env); 339 int (*dev_uevent)(struct device *dev, struct kobj_uevent_env *env);
340 char *(*devnode)(struct device *dev, umode_t *mode); 340 char *(*devnode)(struct device *dev, umode_t *mode);
341 341
342 void (*class_release)(struct class *class); 342 void (*class_release)(struct class *class);
343 void (*dev_release)(struct device *dev); 343 void (*dev_release)(struct device *dev);
344 344
345 int (*suspend)(struct device *dev, pm_message_t state); 345 int (*suspend)(struct device *dev, pm_message_t state);
346 int (*resume)(struct device *dev); 346 int (*resume)(struct device *dev);
347 347
348 const struct kobj_ns_type_operations *ns_type; 348 const struct kobj_ns_type_operations *ns_type;
349 const void *(*namespace)(struct device *dev); 349 const void *(*namespace)(struct device *dev);
350 350
351 const struct dev_pm_ops *pm; 351 const struct dev_pm_ops *pm;
352 352
353 struct subsys_private *p; 353 struct subsys_private *p;
354 }; 354 };
355 355
356 struct class_dev_iter { 356 struct class_dev_iter {
357 struct klist_iter ki; 357 struct klist_iter ki;
358 const struct device_type *type; 358 const struct device_type *type;
359 }; 359 };
360 360
361 extern struct kobject *sysfs_dev_block_kobj; 361 extern struct kobject *sysfs_dev_block_kobj;
362 extern struct kobject *sysfs_dev_char_kobj; 362 extern struct kobject *sysfs_dev_char_kobj;
363 extern int __must_check __class_register(struct class *class, 363 extern int __must_check __class_register(struct class *class,
364 struct lock_class_key *key); 364 struct lock_class_key *key);
365 extern void class_unregister(struct class *class); 365 extern void class_unregister(struct class *class);
366 366
367 /* This is a #define to keep the compiler from merging different 367 /* This is a #define to keep the compiler from merging different
368 * instances of the __key variable */ 368 * instances of the __key variable */
369 #define class_register(class) \ 369 #define class_register(class) \
370 ({ \ 370 ({ \
371 static struct lock_class_key __key; \ 371 static struct lock_class_key __key; \
372 __class_register(class, &__key); \ 372 __class_register(class, &__key); \
373 }) 373 })
374 374
375 struct class_compat; 375 struct class_compat;
376 struct class_compat *class_compat_register(const char *name); 376 struct class_compat *class_compat_register(const char *name);
377 void class_compat_unregister(struct class_compat *cls); 377 void class_compat_unregister(struct class_compat *cls);
378 int class_compat_create_link(struct class_compat *cls, struct device *dev, 378 int class_compat_create_link(struct class_compat *cls, struct device *dev,
379 struct device *device_link); 379 struct device *device_link);
380 void class_compat_remove_link(struct class_compat *cls, struct device *dev, 380 void class_compat_remove_link(struct class_compat *cls, struct device *dev,
381 struct device *device_link); 381 struct device *device_link);
382 382
383 extern void class_dev_iter_init(struct class_dev_iter *iter, 383 extern int class_dev_iter_init(struct class_dev_iter *iter,
384 struct class *class, 384 struct class *class,
385 struct device *start, 385 struct device *start,
386 const struct device_type *type); 386 const struct device_type *type);
387 extern struct device *class_dev_iter_next(struct class_dev_iter *iter); 387 extern struct device *class_dev_iter_next(struct class_dev_iter *iter);
388 extern void class_dev_iter_exit(struct class_dev_iter *iter); 388 extern void class_dev_iter_exit(struct class_dev_iter *iter);
389 389
390 extern int class_for_each_device(struct class *class, struct device *start, 390 extern int class_for_each_device(struct class *class, struct device *start,
391 void *data, 391 void *data,
392 int (*fn)(struct device *dev, void *data)); 392 int (*fn)(struct device *dev, void *data));
393 extern struct device *class_find_device(struct class *class, 393 extern struct device *class_find_device(struct class *class,
394 struct device *start, void *data, 394 struct device *start, void *data,
395 int (*match)(struct device *, void *)); 395 int (*match)(struct device *, void *));
396 396
397 struct class_attribute { 397 struct class_attribute {
398 struct attribute attr; 398 struct attribute attr;
399 ssize_t (*show)(struct class *class, struct class_attribute *attr, 399 ssize_t (*show)(struct class *class, struct class_attribute *attr,
400 char *buf); 400 char *buf);
401 ssize_t (*store)(struct class *class, struct class_attribute *attr, 401 ssize_t (*store)(struct class *class, struct class_attribute *attr,
402 const char *buf, size_t count); 402 const char *buf, size_t count);
403 const void *(*namespace)(struct class *class, 403 const void *(*namespace)(struct class *class,
404 const struct class_attribute *attr); 404 const struct class_attribute *attr);
405 }; 405 };
406 406
407 #define CLASS_ATTR(_name, _mode, _show, _store) \ 407 #define CLASS_ATTR(_name, _mode, _show, _store) \
408 struct class_attribute class_attr_##_name = __ATTR(_name, _mode, _show, _store) 408 struct class_attribute class_attr_##_name = __ATTR(_name, _mode, _show, _store)
409 409
410 extern int __must_check class_create_file(struct class *class, 410 extern int __must_check class_create_file(struct class *class,
411 const struct class_attribute *attr); 411 const struct class_attribute *attr);
412 extern void class_remove_file(struct class *class, 412 extern void class_remove_file(struct class *class,
413 const struct class_attribute *attr); 413 const struct class_attribute *attr);
414 414
415 /* Simple class attribute that is just a static string */ 415 /* Simple class attribute that is just a static string */
416 416
417 struct class_attribute_string { 417 struct class_attribute_string {
418 struct class_attribute attr; 418 struct class_attribute attr;
419 char *str; 419 char *str;
420 }; 420 };
421 421
422 /* Currently read-only only */ 422 /* Currently read-only only */
423 #define _CLASS_ATTR_STRING(_name, _mode, _str) \ 423 #define _CLASS_ATTR_STRING(_name, _mode, _str) \
424 { __ATTR(_name, _mode, show_class_attr_string, NULL), _str } 424 { __ATTR(_name, _mode, show_class_attr_string, NULL), _str }
425 #define CLASS_ATTR_STRING(_name, _mode, _str) \ 425 #define CLASS_ATTR_STRING(_name, _mode, _str) \
426 struct class_attribute_string class_attr_##_name = \ 426 struct class_attribute_string class_attr_##_name = \
427 _CLASS_ATTR_STRING(_name, _mode, _str) 427 _CLASS_ATTR_STRING(_name, _mode, _str)
428 428
429 extern ssize_t show_class_attr_string(struct class *class, struct class_attribute *attr, 429 extern ssize_t show_class_attr_string(struct class *class, struct class_attribute *attr,
430 char *buf); 430 char *buf);
431 431
432 struct class_interface { 432 struct class_interface {
433 struct list_head node; 433 struct list_head node;
434 struct class *class; 434 struct class *class;
435 435
436 int (*add_dev) (struct device *, struct class_interface *); 436 int (*add_dev) (struct device *, struct class_interface *);
437 void (*remove_dev) (struct device *, struct class_interface *); 437 void (*remove_dev) (struct device *, struct class_interface *);
438 }; 438 };
439 439
440 extern int __must_check class_interface_register(struct class_interface *); 440 extern int __must_check class_interface_register(struct class_interface *);
441 extern void class_interface_unregister(struct class_interface *); 441 extern void class_interface_unregister(struct class_interface *);
442 442
443 extern struct class * __must_check __class_create(struct module *owner, 443 extern struct class * __must_check __class_create(struct module *owner,
444 const char *name, 444 const char *name,
445 struct lock_class_key *key); 445 struct lock_class_key *key);
446 extern void class_destroy(struct class *cls); 446 extern void class_destroy(struct class *cls);
447 447
448 /* This is a #define to keep the compiler from merging different 448 /* This is a #define to keep the compiler from merging different
449 * instances of the __key variable */ 449 * instances of the __key variable */
450 #define class_create(owner, name) \ 450 #define class_create(owner, name) \
451 ({ \ 451 ({ \
452 static struct lock_class_key __key; \ 452 static struct lock_class_key __key; \
453 __class_create(owner, name, &__key); \ 453 __class_create(owner, name, &__key); \
454 }) 454 })
455 455
456 /* 456 /*
457 * The type of device, "struct device" is embedded in. A class 457 * The type of device, "struct device" is embedded in. A class
458 * or bus can contain devices of different types 458 * or bus can contain devices of different types
459 * like "partitions" and "disks", "mouse" and "event". 459 * like "partitions" and "disks", "mouse" and "event".
460 * This identifies the device type and carries type-specific 460 * This identifies the device type and carries type-specific
461 * information, equivalent to the kobj_type of a kobject. 461 * information, equivalent to the kobj_type of a kobject.
462 * If "name" is specified, the uevent will contain it in 462 * If "name" is specified, the uevent will contain it in
463 * the DEVTYPE variable. 463 * the DEVTYPE variable.
464 */ 464 */
465 struct device_type { 465 struct device_type {
466 const char *name; 466 const char *name;
467 const struct attribute_group **groups; 467 const struct attribute_group **groups;
468 int (*uevent)(struct device *dev, struct kobj_uevent_env *env); 468 int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
469 char *(*devnode)(struct device *dev, umode_t *mode); 469 char *(*devnode)(struct device *dev, umode_t *mode);
470 void (*release)(struct device *dev); 470 void (*release)(struct device *dev);
471 471
472 const struct dev_pm_ops *pm; 472 const struct dev_pm_ops *pm;
473 }; 473 };
474 474
475 /* interface for exporting device attributes */ 475 /* interface for exporting device attributes */
476 struct device_attribute { 476 struct device_attribute {
477 struct attribute attr; 477 struct attribute attr;
478 ssize_t (*show)(struct device *dev, struct device_attribute *attr, 478 ssize_t (*show)(struct device *dev, struct device_attribute *attr,
479 char *buf); 479 char *buf);
480 ssize_t (*store)(struct device *dev, struct device_attribute *attr, 480 ssize_t (*store)(struct device *dev, struct device_attribute *attr,
481 const char *buf, size_t count); 481 const char *buf, size_t count);
482 }; 482 };
483 483
484 struct dev_ext_attribute { 484 struct dev_ext_attribute {
485 struct device_attribute attr; 485 struct device_attribute attr;
486 void *var; 486 void *var;
487 }; 487 };
488 488
489 ssize_t device_show_ulong(struct device *dev, struct device_attribute *attr, 489 ssize_t device_show_ulong(struct device *dev, struct device_attribute *attr,
490 char *buf); 490 char *buf);
491 ssize_t device_store_ulong(struct device *dev, struct device_attribute *attr, 491 ssize_t device_store_ulong(struct device *dev, struct device_attribute *attr,
492 const char *buf, size_t count); 492 const char *buf, size_t count);
493 ssize_t device_show_int(struct device *dev, struct device_attribute *attr, 493 ssize_t device_show_int(struct device *dev, struct device_attribute *attr,
494 char *buf); 494 char *buf);
495 ssize_t device_store_int(struct device *dev, struct device_attribute *attr, 495 ssize_t device_store_int(struct device *dev, struct device_attribute *attr,
496 const char *buf, size_t count); 496 const char *buf, size_t count);
497 497
498 #define DEVICE_ATTR(_name, _mode, _show, _store) \ 498 #define DEVICE_ATTR(_name, _mode, _show, _store) \
499 struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store) 499 struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store)
500 #define DEVICE_ULONG_ATTR(_name, _mode, _var) \ 500 #define DEVICE_ULONG_ATTR(_name, _mode, _var) \
501 struct dev_ext_attribute dev_attr_##_name = \ 501 struct dev_ext_attribute dev_attr_##_name = \
502 { __ATTR(_name, _mode, device_show_ulong, device_store_ulong), &(_var) } 502 { __ATTR(_name, _mode, device_show_ulong, device_store_ulong), &(_var) }
503 #define DEVICE_INT_ATTR(_name, _mode, _var) \ 503 #define DEVICE_INT_ATTR(_name, _mode, _var) \
504 struct dev_ext_attribute dev_attr_##_name = \ 504 struct dev_ext_attribute dev_attr_##_name = \
505 { __ATTR(_name, _mode, device_show_ulong, device_store_ulong), &(_var) } 505 { __ATTR(_name, _mode, device_show_ulong, device_store_ulong), &(_var) }
506 506
507 extern int device_create_file(struct device *device, 507 extern int device_create_file(struct device *device,
508 const struct device_attribute *entry); 508 const struct device_attribute *entry);
509 extern void device_remove_file(struct device *dev, 509 extern void device_remove_file(struct device *dev,
510 const struct device_attribute *attr); 510 const struct device_attribute *attr);
511 extern int __must_check device_create_bin_file(struct device *dev, 511 extern int __must_check device_create_bin_file(struct device *dev,
512 const struct bin_attribute *attr); 512 const struct bin_attribute *attr);
513 extern void device_remove_bin_file(struct device *dev, 513 extern void device_remove_bin_file(struct device *dev,
514 const struct bin_attribute *attr); 514 const struct bin_attribute *attr);
515 extern int device_schedule_callback_owner(struct device *dev, 515 extern int device_schedule_callback_owner(struct device *dev,
516 void (*func)(struct device *dev), struct module *owner); 516 void (*func)(struct device *dev), struct module *owner);
517 517
518 /* This is a macro to avoid include problems with THIS_MODULE */ 518 /* This is a macro to avoid include problems with THIS_MODULE */
519 #define device_schedule_callback(dev, func) \ 519 #define device_schedule_callback(dev, func) \
520 device_schedule_callback_owner(dev, func, THIS_MODULE) 520 device_schedule_callback_owner(dev, func, THIS_MODULE)
521 521
522 /* device resource management */ 522 /* device resource management */
523 typedef void (*dr_release_t)(struct device *dev, void *res); 523 typedef void (*dr_release_t)(struct device *dev, void *res);
524 typedef int (*dr_match_t)(struct device *dev, void *res, void *match_data); 524 typedef int (*dr_match_t)(struct device *dev, void *res, void *match_data);
525 525
526 #ifdef CONFIG_DEBUG_DEVRES 526 #ifdef CONFIG_DEBUG_DEVRES
527 extern void *__devres_alloc(dr_release_t release, size_t size, gfp_t gfp, 527 extern void *__devres_alloc(dr_release_t release, size_t size, gfp_t gfp,
528 const char *name); 528 const char *name);
529 #define devres_alloc(release, size, gfp) \ 529 #define devres_alloc(release, size, gfp) \
530 __devres_alloc(release, size, gfp, #release) 530 __devres_alloc(release, size, gfp, #release)
531 #else 531 #else
532 extern void *devres_alloc(dr_release_t release, size_t size, gfp_t gfp); 532 extern void *devres_alloc(dr_release_t release, size_t size, gfp_t gfp);
533 #endif 533 #endif
534 extern void devres_free(void *res); 534 extern void devres_free(void *res);
535 extern void devres_add(struct device *dev, void *res); 535 extern void devres_add(struct device *dev, void *res);
536 extern void *devres_find(struct device *dev, dr_release_t release, 536 extern void *devres_find(struct device *dev, dr_release_t release,
537 dr_match_t match, void *match_data); 537 dr_match_t match, void *match_data);
538 extern void *devres_get(struct device *dev, void *new_res, 538 extern void *devres_get(struct device *dev, void *new_res,
539 dr_match_t match, void *match_data); 539 dr_match_t match, void *match_data);
540 extern void *devres_remove(struct device *dev, dr_release_t release, 540 extern void *devres_remove(struct device *dev, dr_release_t release,
541 dr_match_t match, void *match_data); 541 dr_match_t match, void *match_data);
542 extern int devres_destroy(struct device *dev, dr_release_t release, 542 extern int devres_destroy(struct device *dev, dr_release_t release,
543 dr_match_t match, void *match_data); 543 dr_match_t match, void *match_data);
544 544
545 /* devres group */ 545 /* devres group */
546 extern void * __must_check devres_open_group(struct device *dev, void *id, 546 extern void * __must_check devres_open_group(struct device *dev, void *id,
547 gfp_t gfp); 547 gfp_t gfp);
548 extern void devres_close_group(struct device *dev, void *id); 548 extern void devres_close_group(struct device *dev, void *id);
549 extern void devres_remove_group(struct device *dev, void *id); 549 extern void devres_remove_group(struct device *dev, void *id);
550 extern int devres_release_group(struct device *dev, void *id); 550 extern int devres_release_group(struct device *dev, void *id);
551 551
552 /* managed kzalloc/kfree for device drivers, no kmalloc, always use kzalloc */ 552 /* managed kzalloc/kfree for device drivers, no kmalloc, always use kzalloc */
553 extern void *devm_kzalloc(struct device *dev, size_t size, gfp_t gfp); 553 extern void *devm_kzalloc(struct device *dev, size_t size, gfp_t gfp);
554 extern void devm_kfree(struct device *dev, void *p); 554 extern void devm_kfree(struct device *dev, void *p);
555 555
556 void __iomem *devm_request_and_ioremap(struct device *dev, 556 void __iomem *devm_request_and_ioremap(struct device *dev,
557 struct resource *res); 557 struct resource *res);
558 558
559 struct device_dma_parameters { 559 struct device_dma_parameters {
560 /* 560 /*
561 * a low level driver may set these to teach IOMMU code about 561 * a low level driver may set these to teach IOMMU code about
562 * sg limitations. 562 * sg limitations.
563 */ 563 */
564 unsigned int max_segment_size; 564 unsigned int max_segment_size;
565 unsigned long segment_boundary_mask; 565 unsigned long segment_boundary_mask;
566 }; 566 };
567 567
568 /** 568 /**
569 * struct device - The basic device structure 569 * struct device - The basic device structure
570 * @parent: The device's "parent" device, the device to which it is attached. 570 * @parent: The device's "parent" device, the device to which it is attached.
571 * In most cases, a parent device is some sort of bus or host 571 * In most cases, a parent device is some sort of bus or host
572 * controller. If parent is NULL, the device, is a top-level device, 572 * controller. If parent is NULL, the device, is a top-level device,
573 * which is not usually what you want. 573 * which is not usually what you want.
574 * @p: Holds the private data of the driver core portions of the device. 574 * @p: Holds the private data of the driver core portions of the device.
575 * See the comment of the struct device_private for detail. 575 * See the comment of the struct device_private for detail.
576 * @kobj: A top-level, abstract class from which other classes are derived. 576 * @kobj: A top-level, abstract class from which other classes are derived.
577 * @init_name: Initial name of the device. 577 * @init_name: Initial name of the device.
578 * @type: The type of device. 578 * @type: The type of device.
579 * This identifies the device type and carries type-specific 579 * This identifies the device type and carries type-specific
580 * information. 580 * information.
581 * @mutex: Mutex to synchronize calls to its driver. 581 * @mutex: Mutex to synchronize calls to its driver.
582 * @bus: Type of bus device is on. 582 * @bus: Type of bus device is on.
583 * @driver: Which driver has allocated this 583 * @driver: Which driver has allocated this
584 * @platform_data: Platform data specific to the device. 584 * @platform_data: Platform data specific to the device.
585 * Example: For devices on custom boards, as typical of embedded 585 * Example: For devices on custom boards, as typical of embedded
586 * and SOC based hardware, Linux often uses platform_data to point 586 * and SOC based hardware, Linux often uses platform_data to point
587 * to board-specific structures describing devices and how they 587 * to board-specific structures describing devices and how they
588 * are wired. That can include what ports are available, chip 588 * are wired. That can include what ports are available, chip
589 * variants, which GPIO pins act in what additional roles, and so 589 * variants, which GPIO pins act in what additional roles, and so
590 * on. This shrinks the "Board Support Packages" (BSPs) and 590 * on. This shrinks the "Board Support Packages" (BSPs) and
591 * minimizes board-specific #ifdefs in drivers. 591 * minimizes board-specific #ifdefs in drivers.
592 * @power: For device power management. 592 * @power: For device power management.
593 * See Documentation/power/devices.txt for details. 593 * See Documentation/power/devices.txt for details.
594 * @pm_domain: Provide callbacks that are executed during system suspend, 594 * @pm_domain: Provide callbacks that are executed during system suspend,
595 * hibernation, system resume and during runtime PM transitions 595 * hibernation, system resume and during runtime PM transitions
596 * along with subsystem-level and driver-level callbacks. 596 * along with subsystem-level and driver-level callbacks.
597 * @numa_node: NUMA node this device is close to. 597 * @numa_node: NUMA node this device is close to.
598 * @dma_mask: Dma mask (if dma'ble device). 598 * @dma_mask: Dma mask (if dma'ble device).
599 * @coherent_dma_mask: Like dma_mask, but for alloc_coherent mapping as not all 599 * @coherent_dma_mask: Like dma_mask, but for alloc_coherent mapping as not all
600 * hardware supports 64-bit addresses for consistent allocations 600 * hardware supports 64-bit addresses for consistent allocations
601 * such descriptors. 601 * such descriptors.
602 * @dma_parms: A low level driver may set these to teach IOMMU code about 602 * @dma_parms: A low level driver may set these to teach IOMMU code about
603 * segment limitations. 603 * segment limitations.
604 * @dma_pools: Dma pools (if dma'ble device). 604 * @dma_pools: Dma pools (if dma'ble device).
605 * @dma_mem: Internal for coherent mem override. 605 * @dma_mem: Internal for coherent mem override.
606 * @archdata: For arch-specific additions. 606 * @archdata: For arch-specific additions.
607 * @of_node: Associated device tree node. 607 * @of_node: Associated device tree node.
608 * @devt: For creating the sysfs "dev". 608 * @devt: For creating the sysfs "dev".
609 * @id: device instance 609 * @id: device instance
610 * @devres_lock: Spinlock to protect the resource of the device. 610 * @devres_lock: Spinlock to protect the resource of the device.
611 * @devres_head: The resources list of the device. 611 * @devres_head: The resources list of the device.
612 * @knode_class: The node used to add the device to the class list. 612 * @knode_class: The node used to add the device to the class list.
613 * @class: The class of the device. 613 * @class: The class of the device.
614 * @groups: Optional attribute groups. 614 * @groups: Optional attribute groups.
615 * @release: Callback to free the device after all references have 615 * @release: Callback to free the device after all references have
616 * gone away. This should be set by the allocator of the 616 * gone away. This should be set by the allocator of the
617 * device (i.e. the bus driver that discovered the device). 617 * device (i.e. the bus driver that discovered the device).
618 * 618 *
619 * At the lowest level, every device in a Linux system is represented by an 619 * At the lowest level, every device in a Linux system is represented by an
620 * instance of struct device. The device structure contains the information 620 * instance of struct device. The device structure contains the information
621 * that the device model core needs to model the system. Most subsystems, 621 * that the device model core needs to model the system. Most subsystems,
622 * however, track additional information about the devices they host. As a 622 * however, track additional information about the devices they host. As a
623 * result, it is rare for devices to be represented by bare device structures; 623 * result, it is rare for devices to be represented by bare device structures;
624 * instead, that structure, like kobject structures, is usually embedded within 624 * instead, that structure, like kobject structures, is usually embedded within
625 * a higher-level representation of the device. 625 * a higher-level representation of the device.
626 */ 626 */
627 struct device { 627 struct device {
628 struct device *parent; 628 struct device *parent;
629 629
630 struct device_private *p; 630 struct device_private *p;
631 631
632 struct kobject kobj; 632 struct kobject kobj;
633 const char *init_name; /* initial name of the device */ 633 const char *init_name; /* initial name of the device */
634 const struct device_type *type; 634 const struct device_type *type;
635 635
636 struct mutex mutex; /* mutex to synchronize calls to 636 struct mutex mutex; /* mutex to synchronize calls to
637 * its driver. 637 * its driver.
638 */ 638 */
639 639
640 struct bus_type *bus; /* type of bus device is on */ 640 struct bus_type *bus; /* type of bus device is on */
641 struct device_driver *driver; /* which driver has allocated this 641 struct device_driver *driver; /* which driver has allocated this
642 device */ 642 device */
643 void *platform_data; /* Platform specific data, device 643 void *platform_data; /* Platform specific data, device
644 core doesn't touch it */ 644 core doesn't touch it */
645 struct dev_pm_info power; 645 struct dev_pm_info power;
646 struct dev_pm_domain *pm_domain; 646 struct dev_pm_domain *pm_domain;
647 647
648 #ifdef CONFIG_NUMA 648 #ifdef CONFIG_NUMA
649 int numa_node; /* NUMA node this device is close to */ 649 int numa_node; /* NUMA node this device is close to */
650 #endif 650 #endif
651 u64 *dma_mask; /* dma mask (if dma'able device) */ 651 u64 *dma_mask; /* dma mask (if dma'able device) */
652 u64 coherent_dma_mask;/* Like dma_mask, but for 652 u64 coherent_dma_mask;/* Like dma_mask, but for
653 alloc_coherent mappings as 653 alloc_coherent mappings as
654 not all hardware supports 654 not all hardware supports
655 64 bit addresses for consistent 655 64 bit addresses for consistent
656 allocations such descriptors. */ 656 allocations such descriptors. */
657 657
658 struct device_dma_parameters *dma_parms; 658 struct device_dma_parameters *dma_parms;
659 659
660 struct list_head dma_pools; /* dma pools (if dma'ble) */ 660 struct list_head dma_pools; /* dma pools (if dma'ble) */
661 661
662 struct dma_coherent_mem *dma_mem; /* internal for coherent mem 662 struct dma_coherent_mem *dma_mem; /* internal for coherent mem
663 override */ 663 override */
664 /* arch specific additions */ 664 /* arch specific additions */
665 struct dev_archdata archdata; 665 struct dev_archdata archdata;
666 666
667 struct device_node *of_node; /* associated device tree node */ 667 struct device_node *of_node; /* associated device tree node */
668 668
669 dev_t devt; /* dev_t, creates the sysfs "dev" */ 669 dev_t devt; /* dev_t, creates the sysfs "dev" */
670 u32 id; /* device instance */ 670 u32 id; /* device instance */
671 671
672 spinlock_t devres_lock; 672 spinlock_t devres_lock;
673 struct list_head devres_head; 673 struct list_head devres_head;
674 674
675 struct klist_node knode_class; 675 struct klist_node knode_class;
676 struct class *class; 676 struct class *class;
677 const struct attribute_group **groups; /* optional groups */ 677 const struct attribute_group **groups; /* optional groups */
678 678
679 void (*release)(struct device *dev); 679 void (*release)(struct device *dev);
680 }; 680 };
681 681
682 /* Get the wakeup routines, which depend on struct device */ 682 /* Get the wakeup routines, which depend on struct device */
683 #include <linux/pm_wakeup.h> 683 #include <linux/pm_wakeup.h>
684 684
685 static inline const char *dev_name(const struct device *dev) 685 static inline const char *dev_name(const struct device *dev)
686 { 686 {
687 /* Use the init name until the kobject becomes available */ 687 /* Use the init name until the kobject becomes available */
688 if (dev->init_name) 688 if (dev->init_name)
689 return dev->init_name; 689 return dev->init_name;
690 690
691 return kobject_name(&dev->kobj); 691 return kobject_name(&dev->kobj);
692 } 692 }
693 693
694 extern __printf(2, 3) 694 extern __printf(2, 3)
695 int dev_set_name(struct device *dev, const char *name, ...); 695 int dev_set_name(struct device *dev, const char *name, ...);
696 696
697 #ifdef CONFIG_NUMA 697 #ifdef CONFIG_NUMA
698 static inline int dev_to_node(struct device *dev) 698 static inline int dev_to_node(struct device *dev)
699 { 699 {
700 return dev->numa_node; 700 return dev->numa_node;
701 } 701 }
702 static inline void set_dev_node(struct device *dev, int node) 702 static inline void set_dev_node(struct device *dev, int node)
703 { 703 {
704 dev->numa_node = node; 704 dev->numa_node = node;
705 } 705 }
706 #else 706 #else
707 static inline int dev_to_node(struct device *dev) 707 static inline int dev_to_node(struct device *dev)
708 { 708 {
709 return -1; 709 return -1;
710 } 710 }
711 static inline void set_dev_node(struct device *dev, int node) 711 static inline void set_dev_node(struct device *dev, int node)
712 { 712 {
713 } 713 }
714 #endif 714 #endif
715 715
716 static inline struct pm_subsys_data *dev_to_psd(struct device *dev) 716 static inline struct pm_subsys_data *dev_to_psd(struct device *dev)
717 { 717 {
718 return dev ? dev->power.subsys_data : NULL; 718 return dev ? dev->power.subsys_data : NULL;
719 } 719 }
720 720
721 static inline unsigned int dev_get_uevent_suppress(const struct device *dev) 721 static inline unsigned int dev_get_uevent_suppress(const struct device *dev)
722 { 722 {
723 return dev->kobj.uevent_suppress; 723 return dev->kobj.uevent_suppress;
724 } 724 }
725 725
726 static inline void dev_set_uevent_suppress(struct device *dev, int val) 726 static inline void dev_set_uevent_suppress(struct device *dev, int val)
727 { 727 {
728 dev->kobj.uevent_suppress = val; 728 dev->kobj.uevent_suppress = val;
729 } 729 }
730 730
731 static inline int device_is_registered(struct device *dev) 731 static inline int device_is_registered(struct device *dev)
732 { 732 {
733 return dev->kobj.state_in_sysfs; 733 return dev->kobj.state_in_sysfs;
734 } 734 }
735 735
736 static inline void device_enable_async_suspend(struct device *dev) 736 static inline void device_enable_async_suspend(struct device *dev)
737 { 737 {
738 if (!dev->power.is_prepared) 738 if (!dev->power.is_prepared)
739 dev->power.async_suspend = true; 739 dev->power.async_suspend = true;
740 } 740 }
741 741
742 static inline void device_disable_async_suspend(struct device *dev) 742 static inline void device_disable_async_suspend(struct device *dev)
743 { 743 {
744 if (!dev->power.is_prepared) 744 if (!dev->power.is_prepared)
745 dev->power.async_suspend = false; 745 dev->power.async_suspend = false;
746 } 746 }
747 747
748 static inline bool device_async_suspend_enabled(struct device *dev) 748 static inline bool device_async_suspend_enabled(struct device *dev)
749 { 749 {
750 return !!dev->power.async_suspend; 750 return !!dev->power.async_suspend;
751 } 751 }
752 752
753 static inline void pm_suspend_ignore_children(struct device *dev, bool enable) 753 static inline void pm_suspend_ignore_children(struct device *dev, bool enable)
754 { 754 {
755 dev->power.ignore_children = enable; 755 dev->power.ignore_children = enable;
756 } 756 }
757 757
758 static inline void device_lock(struct device *dev) 758 static inline void device_lock(struct device *dev)
759 { 759 {
760 mutex_lock(&dev->mutex); 760 mutex_lock(&dev->mutex);
761 } 761 }
762 762
763 static inline int device_trylock(struct device *dev) 763 static inline int device_trylock(struct device *dev)
764 { 764 {
765 return mutex_trylock(&dev->mutex); 765 return mutex_trylock(&dev->mutex);
766 } 766 }
767 767
768 static inline void device_unlock(struct device *dev) 768 static inline void device_unlock(struct device *dev)
769 { 769 {
770 mutex_unlock(&dev->mutex); 770 mutex_unlock(&dev->mutex);
771 } 771 }
772 772
773 void driver_init(void); 773 void driver_init(void);
774 774
775 /* 775 /*
776 * High level routines for use by the bus drivers 776 * High level routines for use by the bus drivers
777 */ 777 */
778 extern int __must_check device_register(struct device *dev); 778 extern int __must_check device_register(struct device *dev);
779 extern void device_unregister(struct device *dev); 779 extern void device_unregister(struct device *dev);
780 extern void device_initialize(struct device *dev); 780 extern void device_initialize(struct device *dev);
781 extern int __must_check device_add(struct device *dev); 781 extern int __must_check device_add(struct device *dev);
782 extern void device_del(struct device *dev); 782 extern void device_del(struct device *dev);
783 extern int device_for_each_child(struct device *dev, void *data, 783 extern int device_for_each_child(struct device *dev, void *data,
784 int (*fn)(struct device *dev, void *data)); 784 int (*fn)(struct device *dev, void *data));
785 extern struct device *device_find_child(struct device *dev, void *data, 785 extern struct device *device_find_child(struct device *dev, void *data,
786 int (*match)(struct device *dev, void *data)); 786 int (*match)(struct device *dev, void *data));
787 extern int device_rename(struct device *dev, const char *new_name); 787 extern int device_rename(struct device *dev, const char *new_name);
788 extern int device_move(struct device *dev, struct device *new_parent, 788 extern int device_move(struct device *dev, struct device *new_parent,
789 enum dpm_order dpm_order); 789 enum dpm_order dpm_order);
790 extern const char *device_get_devnode(struct device *dev, 790 extern const char *device_get_devnode(struct device *dev,
791 umode_t *mode, const char **tmp); 791 umode_t *mode, const char **tmp);
792 extern void *dev_get_drvdata(const struct device *dev); 792 extern void *dev_get_drvdata(const struct device *dev);
793 extern int dev_set_drvdata(struct device *dev, void *data); 793 extern int dev_set_drvdata(struct device *dev, void *data);
794 794
795 /* 795 /*
796 * Root device objects for grouping under /sys/devices 796 * Root device objects for grouping under /sys/devices
797 */ 797 */
798 extern struct device *__root_device_register(const char *name, 798 extern struct device *__root_device_register(const char *name,
799 struct module *owner); 799 struct module *owner);
800 800
801 /* 801 /*
802 * This is a macro to avoid include problems with THIS_MODULE, 802 * This is a macro to avoid include problems with THIS_MODULE,
803 * just as per what is done for device_schedule_callback() above. 803 * just as per what is done for device_schedule_callback() above.
804 */ 804 */
805 #define root_device_register(name) \ 805 #define root_device_register(name) \
806 __root_device_register(name, THIS_MODULE) 806 __root_device_register(name, THIS_MODULE)
807 807
808 extern void root_device_unregister(struct device *root); 808 extern void root_device_unregister(struct device *root);
809 809
810 static inline void *dev_get_platdata(const struct device *dev) 810 static inline void *dev_get_platdata(const struct device *dev)
811 { 811 {
812 return dev->platform_data; 812 return dev->platform_data;
813 } 813 }
814 814
815 /* 815 /*
816 * Manual binding of a device to driver. See drivers/base/bus.c 816 * Manual binding of a device to driver. See drivers/base/bus.c
817 * for information on use. 817 * for information on use.
818 */ 818 */
819 extern int __must_check device_bind_driver(struct device *dev); 819 extern int __must_check device_bind_driver(struct device *dev);
820 extern void device_release_driver(struct device *dev); 820 extern void device_release_driver(struct device *dev);
821 extern int __must_check device_attach(struct device *dev); 821 extern int __must_check device_attach(struct device *dev);
822 extern int __must_check driver_attach(struct device_driver *drv); 822 extern int __must_check driver_attach(struct device_driver *drv);
823 extern int __must_check device_reprobe(struct device *dev); 823 extern int __must_check device_reprobe(struct device *dev);
824 824
825 /* 825 /*
826 * Easy functions for dynamically creating devices on the fly 826 * Easy functions for dynamically creating devices on the fly
827 */ 827 */
828 extern struct device *device_create_vargs(struct class *cls, 828 extern struct device *device_create_vargs(struct class *cls,
829 struct device *parent, 829 struct device *parent,
830 dev_t devt, 830 dev_t devt,
831 void *drvdata, 831 void *drvdata,
832 const char *fmt, 832 const char *fmt,
833 va_list vargs); 833 va_list vargs);
834 extern __printf(5, 6) 834 extern __printf(5, 6)
835 struct device *device_create(struct class *cls, struct device *parent, 835 struct device *device_create(struct class *cls, struct device *parent,
836 dev_t devt, void *drvdata, 836 dev_t devt, void *drvdata,
837 const char *fmt, ...); 837 const char *fmt, ...);
838 extern void device_destroy(struct class *cls, dev_t devt); 838 extern void device_destroy(struct class *cls, dev_t devt);
839 839
840 /* 840 /*
841 * Platform "fixup" functions - allow the platform to have their say 841 * Platform "fixup" functions - allow the platform to have their say
842 * about devices and actions that the general device layer doesn't 842 * about devices and actions that the general device layer doesn't
843 * know about. 843 * know about.
844 */ 844 */
845 /* Notify platform of device discovery */ 845 /* Notify platform of device discovery */
846 extern int (*platform_notify)(struct device *dev); 846 extern int (*platform_notify)(struct device *dev);
847 847
848 extern int (*platform_notify_remove)(struct device *dev); 848 extern int (*platform_notify_remove)(struct device *dev);
849 849
850 850
851 /* 851 /*
852 * get_device - atomically increment the reference count for the device. 852 * get_device - atomically increment the reference count for the device.
853 * 853 *
854 */ 854 */
855 extern struct device *get_device(struct device *dev); 855 extern struct device *get_device(struct device *dev);
856 extern void put_device(struct device *dev); 856 extern void put_device(struct device *dev);
857 857
858 extern void wait_for_device_probe(void); 858 extern void wait_for_device_probe(void);
859 859
860 #ifdef CONFIG_DEVTMPFS 860 #ifdef CONFIG_DEVTMPFS
861 extern int devtmpfs_create_node(struct device *dev); 861 extern int devtmpfs_create_node(struct device *dev);
862 extern int devtmpfs_delete_node(struct device *dev); 862 extern int devtmpfs_delete_node(struct device *dev);
863 extern int devtmpfs_mount(const char *mntdir); 863 extern int devtmpfs_mount(const char *mntdir);
864 #else 864 #else
865 static inline int devtmpfs_create_node(struct device *dev) { return 0; } 865 static inline int devtmpfs_create_node(struct device *dev) { return 0; }
866 static inline int devtmpfs_delete_node(struct device *dev) { return 0; } 866 static inline int devtmpfs_delete_node(struct device *dev) { return 0; }
867 static inline int devtmpfs_mount(const char *mountpoint) { return 0; } 867 static inline int devtmpfs_mount(const char *mountpoint) { return 0; }
868 #endif 868 #endif
869 869
870 /* drivers/base/power/shutdown.c */ 870 /* drivers/base/power/shutdown.c */
871 extern void device_shutdown(void); 871 extern void device_shutdown(void);
872 872
873 /* debugging and troubleshooting/diagnostic helpers. */ 873 /* debugging and troubleshooting/diagnostic helpers. */
874 extern const char *dev_driver_string(const struct device *dev); 874 extern const char *dev_driver_string(const struct device *dev);
875 875
876 876
877 #ifdef CONFIG_PRINTK 877 #ifdef CONFIG_PRINTK
878 878
879 extern int __dev_printk(const char *level, const struct device *dev, 879 extern int __dev_printk(const char *level, const struct device *dev,
880 struct va_format *vaf); 880 struct va_format *vaf);
881 extern __printf(3, 4) 881 extern __printf(3, 4)
882 int dev_printk(const char *level, const struct device *dev, 882 int dev_printk(const char *level, const struct device *dev,
883 const char *fmt, ...) 883 const char *fmt, ...)
884 ; 884 ;
885 extern __printf(2, 3) 885 extern __printf(2, 3)
886 int dev_emerg(const struct device *dev, const char *fmt, ...); 886 int dev_emerg(const struct device *dev, const char *fmt, ...);
887 extern __printf(2, 3) 887 extern __printf(2, 3)
888 int dev_alert(const struct device *dev, const char *fmt, ...); 888 int dev_alert(const struct device *dev, const char *fmt, ...);
889 extern __printf(2, 3) 889 extern __printf(2, 3)
890 int dev_crit(const struct device *dev, const char *fmt, ...); 890 int dev_crit(const struct device *dev, const char *fmt, ...);
891 extern __printf(2, 3) 891 extern __printf(2, 3)
892 int dev_err(const struct device *dev, const char *fmt, ...); 892 int dev_err(const struct device *dev, const char *fmt, ...);
893 extern __printf(2, 3) 893 extern __printf(2, 3)
894 int dev_warn(const struct device *dev, const char *fmt, ...); 894 int dev_warn(const struct device *dev, const char *fmt, ...);
895 extern __printf(2, 3) 895 extern __printf(2, 3)
896 int dev_notice(const struct device *dev, const char *fmt, ...); 896 int dev_notice(const struct device *dev, const char *fmt, ...);
897 extern __printf(2, 3) 897 extern __printf(2, 3)
898 int _dev_info(const struct device *dev, const char *fmt, ...); 898 int _dev_info(const struct device *dev, const char *fmt, ...);
899 899
900 #else 900 #else
901 901
902 static inline int __dev_printk(const char *level, const struct device *dev, 902 static inline int __dev_printk(const char *level, const struct device *dev,
903 struct va_format *vaf) 903 struct va_format *vaf)
904 { return 0; } 904 { return 0; }
905 static inline __printf(3, 4) 905 static inline __printf(3, 4)
906 int dev_printk(const char *level, const struct device *dev, 906 int dev_printk(const char *level, const struct device *dev,
907 const char *fmt, ...) 907 const char *fmt, ...)
908 { return 0; } 908 { return 0; }
909 909
910 static inline __printf(2, 3) 910 static inline __printf(2, 3)
911 int dev_emerg(const struct device *dev, const char *fmt, ...) 911 int dev_emerg(const struct device *dev, const char *fmt, ...)
912 { return 0; } 912 { return 0; }
913 static inline __printf(2, 3) 913 static inline __printf(2, 3)
914 int dev_crit(const struct device *dev, const char *fmt, ...) 914 int dev_crit(const struct device *dev, const char *fmt, ...)
915 { return 0; } 915 { return 0; }
916 static inline __printf(2, 3) 916 static inline __printf(2, 3)
917 int dev_alert(const struct device *dev, const char *fmt, ...) 917 int dev_alert(const struct device *dev, const char *fmt, ...)
918 { return 0; } 918 { return 0; }
919 static inline __printf(2, 3) 919 static inline __printf(2, 3)
920 int dev_err(const struct device *dev, const char *fmt, ...) 920 int dev_err(const struct device *dev, const char *fmt, ...)
921 { return 0; } 921 { return 0; }
922 static inline __printf(2, 3) 922 static inline __printf(2, 3)
923 int dev_warn(const struct device *dev, const char *fmt, ...) 923 int dev_warn(const struct device *dev, const char *fmt, ...)
924 { return 0; } 924 { return 0; }
925 static inline __printf(2, 3) 925 static inline __printf(2, 3)
926 int dev_notice(const struct device *dev, const char *fmt, ...) 926 int dev_notice(const struct device *dev, const char *fmt, ...)
927 { return 0; } 927 { return 0; }
928 static inline __printf(2, 3) 928 static inline __printf(2, 3)
929 int _dev_info(const struct device *dev, const char *fmt, ...) 929 int _dev_info(const struct device *dev, const char *fmt, ...)
930 { return 0; } 930 { return 0; }
931 931
932 #endif 932 #endif
933 933
934 /* 934 /*
935 * Stupid hackaround for existing uses of non-printk uses dev_info 935 * Stupid hackaround for existing uses of non-printk uses dev_info
936 * 936 *
937 * Note that the definition of dev_info below is actually _dev_info 937 * Note that the definition of dev_info below is actually _dev_info
938 * and a macro is used to avoid redefining dev_info 938 * and a macro is used to avoid redefining dev_info
939 */ 939 */
940 940
941 #define dev_info(dev, fmt, arg...) _dev_info(dev, fmt, ##arg) 941 #define dev_info(dev, fmt, arg...) _dev_info(dev, fmt, ##arg)
942 942
943 #if defined(CONFIG_DYNAMIC_DEBUG) 943 #if defined(CONFIG_DYNAMIC_DEBUG)
944 #define dev_dbg(dev, format, ...) \ 944 #define dev_dbg(dev, format, ...) \
945 do { \ 945 do { \
946 dynamic_dev_dbg(dev, format, ##__VA_ARGS__); \ 946 dynamic_dev_dbg(dev, format, ##__VA_ARGS__); \
947 } while (0) 947 } while (0)
948 #elif defined(DEBUG) 948 #elif defined(DEBUG)
949 #define dev_dbg(dev, format, arg...) \ 949 #define dev_dbg(dev, format, arg...) \
950 dev_printk(KERN_DEBUG, dev, format, ##arg) 950 dev_printk(KERN_DEBUG, dev, format, ##arg)
951 #else 951 #else
952 #define dev_dbg(dev, format, arg...) \ 952 #define dev_dbg(dev, format, arg...) \
953 ({ \ 953 ({ \
954 if (0) \ 954 if (0) \
955 dev_printk(KERN_DEBUG, dev, format, ##arg); \ 955 dev_printk(KERN_DEBUG, dev, format, ##arg); \
956 0; \ 956 0; \
957 }) 957 })
958 #endif 958 #endif
959 959
960 #ifdef VERBOSE_DEBUG 960 #ifdef VERBOSE_DEBUG
961 #define dev_vdbg dev_dbg 961 #define dev_vdbg dev_dbg
962 #else 962 #else
963 #define dev_vdbg(dev, format, arg...) \ 963 #define dev_vdbg(dev, format, arg...) \
964 ({ \ 964 ({ \
965 if (0) \ 965 if (0) \
966 dev_printk(KERN_DEBUG, dev, format, ##arg); \ 966 dev_printk(KERN_DEBUG, dev, format, ##arg); \
967 0; \ 967 0; \
968 }) 968 })
969 #endif 969 #endif
970 970
971 /* 971 /*
972 * dev_WARN*() acts like dev_printk(), but with the key difference 972 * dev_WARN*() acts like dev_printk(), but with the key difference
973 * of using a WARN/WARN_ON to get the message out, including the 973 * of using a WARN/WARN_ON to get the message out, including the
974 * file/line information and a backtrace. 974 * file/line information and a backtrace.
975 */ 975 */
976 #define dev_WARN(dev, format, arg...) \ 976 #define dev_WARN(dev, format, arg...) \
977 WARN(1, "Device: %s\n" format, dev_driver_string(dev), ## arg); 977 WARN(1, "Device: %s\n" format, dev_driver_string(dev), ## arg);
978 978
979 #define dev_WARN_ONCE(dev, condition, format, arg...) \ 979 #define dev_WARN_ONCE(dev, condition, format, arg...) \
980 WARN_ONCE(condition, "Device %s\n" format, \ 980 WARN_ONCE(condition, "Device %s\n" format, \
981 dev_driver_string(dev), ## arg) 981 dev_driver_string(dev), ## arg)
982 982
983 /* Create alias, so I can be autoloaded. */ 983 /* Create alias, so I can be autoloaded. */
984 #define MODULE_ALIAS_CHARDEV(major,minor) \ 984 #define MODULE_ALIAS_CHARDEV(major,minor) \
985 MODULE_ALIAS("char-major-" __stringify(major) "-" __stringify(minor)) 985 MODULE_ALIAS("char-major-" __stringify(major) "-" __stringify(minor))
986 #define MODULE_ALIAS_CHARDEV_MAJOR(major) \ 986 #define MODULE_ALIAS_CHARDEV_MAJOR(major) \
987 MODULE_ALIAS("char-major-" __stringify(major) "-*") 987 MODULE_ALIAS("char-major-" __stringify(major) "-*")
988 988
989 #ifdef CONFIG_SYSFS_DEPRECATED 989 #ifdef CONFIG_SYSFS_DEPRECATED
990 extern long sysfs_deprecated; 990 extern long sysfs_deprecated;
991 #else 991 #else
992 #define sysfs_deprecated 0 992 #define sysfs_deprecated 0
993 #endif 993 #endif
994 994
995 /** 995 /**
996 * module_driver() - Helper macro for drivers that don't do anything 996 * module_driver() - Helper macro for drivers that don't do anything
997 * special in module init/exit. This eliminates a lot of boilerplate. 997 * special in module init/exit. This eliminates a lot of boilerplate.
998 * Each module may only use this macro once, and calling it replaces 998 * Each module may only use this macro once, and calling it replaces
999 * module_init() and module_exit(). 999 * module_init() and module_exit().
1000 * 1000 *
1001 * @__driver: driver name 1001 * @__driver: driver name
1002 * @__register: register function for this driver type 1002 * @__register: register function for this driver type
1003 * @__unregister: unregister function for this driver type 1003 * @__unregister: unregister function for this driver type
1004 * @...: Additional arguments to be passed to __register and __unregister. 1004 * @...: Additional arguments to be passed to __register and __unregister.
1005 * 1005 *
1006 * Use this macro to construct bus specific macros for registering 1006 * Use this macro to construct bus specific macros for registering
1007 * drivers, and do not use it on its own. 1007 * drivers, and do not use it on its own.
1008 */ 1008 */
1009 #define module_driver(__driver, __register, __unregister, ...) \ 1009 #define module_driver(__driver, __register, __unregister, ...) \
1010 static int __init __driver##_init(void) \ 1010 static int __init __driver##_init(void) \
1011 { \ 1011 { \
1012 return __register(&(__driver) , ##__VA_ARGS__); \ 1012 return __register(&(__driver) , ##__VA_ARGS__); \
1013 } \ 1013 } \
1014 module_init(__driver##_init); \ 1014 module_init(__driver##_init); \
1015 static void __exit __driver##_exit(void) \ 1015 static void __exit __driver##_exit(void) \
1016 { \ 1016 { \
1017 __unregister(&(__driver) , ##__VA_ARGS__); \ 1017 __unregister(&(__driver) , ##__VA_ARGS__); \
1018 } \ 1018 } \
1019 module_exit(__driver##_exit); 1019 module_exit(__driver##_exit);
1020 1020
1021 #endif /* _DEVICE_H_ */ 1021 #endif /* _DEVICE_H_ */
1022 1022
include/linux/klist.h
1 /* 1 /*
2 * klist.h - Some generic list helpers, extending struct list_head a bit. 2 * klist.h - Some generic list helpers, extending struct list_head a bit.
3 * 3 *
4 * Implementations are found in lib/klist.c 4 * Implementations are found in lib/klist.c
5 * 5 *
6 * 6 *
7 * Copyright (C) 2005 Patrick Mochel 7 * Copyright (C) 2005 Patrick Mochel
8 * 8 *
9 * This file is rleased under the GPL v2. 9 * This file is rleased under the GPL v2.
10 */ 10 */
11 11
12 #ifndef _LINUX_KLIST_H 12 #ifndef _LINUX_KLIST_H
13 #define _LINUX_KLIST_H 13 #define _LINUX_KLIST_H
14 14
15 #include <linux/spinlock.h> 15 #include <linux/spinlock.h>
16 #include <linux/kref.h> 16 #include <linux/kref.h>
17 #include <linux/list.h> 17 #include <linux/list.h>
18 18
19 struct klist_node; 19 struct klist_node;
20 struct klist { 20 struct klist {
21 spinlock_t k_lock; 21 spinlock_t k_lock;
22 struct list_head k_list; 22 struct list_head k_list;
23 void (*get)(struct klist_node *); 23 void (*get)(struct klist_node *);
24 void (*put)(struct klist_node *); 24 void (*put)(struct klist_node *);
25 } __attribute__ ((aligned (sizeof(void *)))); 25 } __attribute__ ((aligned (sizeof(void *))));
26 26
27 #define KLIST_INIT(_name, _get, _put) \ 27 #define KLIST_INIT(_name, _get, _put) \
28 { .k_lock = __SPIN_LOCK_UNLOCKED(_name.k_lock), \ 28 { .k_lock = __SPIN_LOCK_UNLOCKED(_name.k_lock), \
29 .k_list = LIST_HEAD_INIT(_name.k_list), \ 29 .k_list = LIST_HEAD_INIT(_name.k_list), \
30 .get = _get, \ 30 .get = _get, \
31 .put = _put, } 31 .put = _put, }
32 32
33 #define DEFINE_KLIST(_name, _get, _put) \ 33 #define DEFINE_KLIST(_name, _get, _put) \
34 struct klist _name = KLIST_INIT(_name, _get, _put) 34 struct klist _name = KLIST_INIT(_name, _get, _put)
35 35
36 extern void klist_init(struct klist *k, void (*get)(struct klist_node *), 36 extern void klist_init(struct klist *k, void (*get)(struct klist_node *),
37 void (*put)(struct klist_node *)); 37 void (*put)(struct klist_node *));
38 38
39 struct klist_node { 39 struct klist_node {
40 void *n_klist; /* never access directly */ 40 void *n_klist; /* never access directly */
41 struct list_head n_node; 41 struct list_head n_node;
42 struct kref n_ref; 42 struct kref n_ref;
43 }; 43 };
44 44
45 extern void klist_add_tail(struct klist_node *n, struct klist *k); 45 extern void klist_add_tail(struct klist_node *n, struct klist *k);
46 extern void klist_add_head(struct klist_node *n, struct klist *k); 46 extern void klist_add_head(struct klist_node *n, struct klist *k);
47 extern void klist_add_after(struct klist_node *n, struct klist_node *pos); 47 extern void klist_add_after(struct klist_node *n, struct klist_node *pos);
48 extern void klist_add_before(struct klist_node *n, struct klist_node *pos); 48 extern void klist_add_before(struct klist_node *n, struct klist_node *pos);
49 49
50 extern void klist_del(struct klist_node *n); 50 extern void klist_del(struct klist_node *n);
51 extern void klist_remove(struct klist_node *n); 51 extern void klist_remove(struct klist_node *n);
52 52
53 extern int klist_node_attached(struct klist_node *n); 53 extern int klist_node_attached(struct klist_node *n);
54 54
55 55
56 struct klist_iter { 56 struct klist_iter {
57 struct klist *i_klist; 57 struct klist *i_klist;
58 struct klist_node *i_cur; 58 struct klist_node *i_cur;
59 }; 59 };
60 60
61 61
62 extern void klist_iter_init(struct klist *k, struct klist_iter *i); 62 extern void klist_iter_init(struct klist *k, struct klist_iter *i);
63 extern void klist_iter_init_node(struct klist *k, struct klist_iter *i, 63 extern int klist_iter_init_node(struct klist *k, struct klist_iter *i,
64 struct klist_node *n); 64 struct klist_node *n);
65 extern void klist_iter_exit(struct klist_iter *i); 65 extern void klist_iter_exit(struct klist_iter *i);
66 extern struct klist_node *klist_next(struct klist_iter *i); 66 extern struct klist_node *klist_next(struct klist_iter *i);
67 67
68 #endif 68 #endif
69 69
1 /* 1 /*
2 * klist.c - Routines for manipulating klists. 2 * klist.c - Routines for manipulating klists.
3 * 3 *
4 * Copyright (C) 2005 Patrick Mochel 4 * Copyright (C) 2005 Patrick Mochel
5 * 5 *
6 * This file is released under the GPL v2. 6 * This file is released under the GPL v2.
7 * 7 *
8 * This klist interface provides a couple of structures that wrap around 8 * This klist interface provides a couple of structures that wrap around
9 * struct list_head to provide explicit list "head" (struct klist) and list 9 * struct list_head to provide explicit list "head" (struct klist) and list
10 * "node" (struct klist_node) objects. For struct klist, a spinlock is 10 * "node" (struct klist_node) objects. For struct klist, a spinlock is
11 * included that protects access to the actual list itself. struct 11 * included that protects access to the actual list itself. struct
12 * klist_node provides a pointer to the klist that owns it and a kref 12 * klist_node provides a pointer to the klist that owns it and a kref
13 * reference count that indicates the number of current users of that node 13 * reference count that indicates the number of current users of that node
14 * in the list. 14 * in the list.
15 * 15 *
16 * The entire point is to provide an interface for iterating over a list 16 * The entire point is to provide an interface for iterating over a list
17 * that is safe and allows for modification of the list during the 17 * that is safe and allows for modification of the list during the
18 * iteration (e.g. insertion and removal), including modification of the 18 * iteration (e.g. insertion and removal), including modification of the
19 * current node on the list. 19 * current node on the list.
20 * 20 *
21 * It works using a 3rd object type - struct klist_iter - that is declared 21 * It works using a 3rd object type - struct klist_iter - that is declared
22 * and initialized before an iteration. klist_next() is used to acquire the 22 * and initialized before an iteration. klist_next() is used to acquire the
23 * next element in the list. It returns NULL if there are no more items. 23 * next element in the list. It returns NULL if there are no more items.
24 * Internally, that routine takes the klist's lock, decrements the 24 * Internally, that routine takes the klist's lock, decrements the
25 * reference count of the previous klist_node and increments the count of 25 * reference count of the previous klist_node and increments the count of
26 * the next klist_node. It then drops the lock and returns. 26 * the next klist_node. It then drops the lock and returns.
27 * 27 *
28 * There are primitives for adding and removing nodes to/from a klist. 28 * There are primitives for adding and removing nodes to/from a klist.
29 * When deleting, klist_del() will simply decrement the reference count. 29 * When deleting, klist_del() will simply decrement the reference count.
30 * Only when the count goes to 0 is the node removed from the list. 30 * Only when the count goes to 0 is the node removed from the list.
31 * klist_remove() will try to delete the node from the list and block until 31 * klist_remove() will try to delete the node from the list and block until
32 * it is actually removed. This is useful for objects (like devices) that 32 * it is actually removed. This is useful for objects (like devices) that
33 * have been removed from the system and must be freed (but must wait until 33 * have been removed from the system and must be freed (but must wait until
34 * all accessors have finished). 34 * all accessors have finished).
35 */ 35 */
36 36
37 #include <linux/klist.h> 37 #include <linux/klist.h>
38 #include <linux/export.h> 38 #include <linux/export.h>
39 #include <linux/sched.h> 39 #include <linux/sched.h>
40 40
41 /* 41 /*
42 * Use the lowest bit of n_klist to mark deleted nodes and exclude 42 * Use the lowest bit of n_klist to mark deleted nodes and exclude
43 * dead ones from iteration. 43 * dead ones from iteration.
44 */ 44 */
45 #define KNODE_DEAD 1LU 45 #define KNODE_DEAD 1LU
46 #define KNODE_KLIST_MASK ~KNODE_DEAD 46 #define KNODE_KLIST_MASK ~KNODE_DEAD
47 47
48 static struct klist *knode_klist(struct klist_node *knode) 48 static struct klist *knode_klist(struct klist_node *knode)
49 { 49 {
50 return (struct klist *) 50 return (struct klist *)
51 ((unsigned long)knode->n_klist & KNODE_KLIST_MASK); 51 ((unsigned long)knode->n_klist & KNODE_KLIST_MASK);
52 } 52 }
53 53
54 static bool knode_dead(struct klist_node *knode) 54 static bool knode_dead(struct klist_node *knode)
55 { 55 {
56 return (unsigned long)knode->n_klist & KNODE_DEAD; 56 return (unsigned long)knode->n_klist & KNODE_DEAD;
57 } 57 }
58 58
59 static void knode_set_klist(struct klist_node *knode, struct klist *klist) 59 static void knode_set_klist(struct klist_node *knode, struct klist *klist)
60 { 60 {
61 knode->n_klist = klist; 61 knode->n_klist = klist;
62 /* no knode deserves to start its life dead */ 62 /* no knode deserves to start its life dead */
63 WARN_ON(knode_dead(knode)); 63 WARN_ON(knode_dead(knode));
64 } 64 }
65 65
66 static void knode_kill(struct klist_node *knode) 66 static void knode_kill(struct klist_node *knode)
67 { 67 {
68 /* and no knode should die twice ever either, see we're very humane */ 68 /* and no knode should die twice ever either, see we're very humane */
69 WARN_ON(knode_dead(knode)); 69 WARN_ON(knode_dead(knode));
70 *(unsigned long *)&knode->n_klist |= KNODE_DEAD; 70 *(unsigned long *)&knode->n_klist |= KNODE_DEAD;
71 } 71 }
72 72
73 /** 73 /**
74 * klist_init - Initialize a klist structure. 74 * klist_init - Initialize a klist structure.
75 * @k: The klist we're initializing. 75 * @k: The klist we're initializing.
76 * @get: The get function for the embedding object (NULL if none) 76 * @get: The get function for the embedding object (NULL if none)
77 * @put: The put function for the embedding object (NULL if none) 77 * @put: The put function for the embedding object (NULL if none)
78 * 78 *
79 * Initialises the klist structure. If the klist_node structures are 79 * Initialises the klist structure. If the klist_node structures are
80 * going to be embedded in refcounted objects (necessary for safe 80 * going to be embedded in refcounted objects (necessary for safe
81 * deletion) then the get/put arguments are used to initialise 81 * deletion) then the get/put arguments are used to initialise
82 * functions that take and release references on the embedding 82 * functions that take and release references on the embedding
83 * objects. 83 * objects.
84 */ 84 */
85 void klist_init(struct klist *k, void (*get)(struct klist_node *), 85 void klist_init(struct klist *k, void (*get)(struct klist_node *),
86 void (*put)(struct klist_node *)) 86 void (*put)(struct klist_node *))
87 { 87 {
88 INIT_LIST_HEAD(&k->k_list); 88 INIT_LIST_HEAD(&k->k_list);
89 spin_lock_init(&k->k_lock); 89 spin_lock_init(&k->k_lock);
90 k->get = get; 90 k->get = get;
91 k->put = put; 91 k->put = put;
92 } 92 }
93 EXPORT_SYMBOL_GPL(klist_init); 93 EXPORT_SYMBOL_GPL(klist_init);
94 94
95 static void add_head(struct klist *k, struct klist_node *n) 95 static void add_head(struct klist *k, struct klist_node *n)
96 { 96 {
97 spin_lock(&k->k_lock); 97 spin_lock(&k->k_lock);
98 list_add(&n->n_node, &k->k_list); 98 list_add(&n->n_node, &k->k_list);
99 spin_unlock(&k->k_lock); 99 spin_unlock(&k->k_lock);
100 } 100 }
101 101
102 static void add_tail(struct klist *k, struct klist_node *n) 102 static void add_tail(struct klist *k, struct klist_node *n)
103 { 103 {
104 spin_lock(&k->k_lock); 104 spin_lock(&k->k_lock);
105 list_add_tail(&n->n_node, &k->k_list); 105 list_add_tail(&n->n_node, &k->k_list);
106 spin_unlock(&k->k_lock); 106 spin_unlock(&k->k_lock);
107 } 107 }
108 108
109 static void klist_node_init(struct klist *k, struct klist_node *n) 109 static void klist_node_init(struct klist *k, struct klist_node *n)
110 { 110 {
111 INIT_LIST_HEAD(&n->n_node); 111 INIT_LIST_HEAD(&n->n_node);
112 kref_init(&n->n_ref); 112 kref_init(&n->n_ref);
113 knode_set_klist(n, k); 113 knode_set_klist(n, k);
114 if (k->get) 114 if (k->get)
115 k->get(n); 115 k->get(n);
116 } 116 }
117 117
118 /** 118 /**
119 * klist_add_head - Initialize a klist_node and add it to front. 119 * klist_add_head - Initialize a klist_node and add it to front.
120 * @n: node we're adding. 120 * @n: node we're adding.
121 * @k: klist it's going on. 121 * @k: klist it's going on.
122 */ 122 */
123 void klist_add_head(struct klist_node *n, struct klist *k) 123 void klist_add_head(struct klist_node *n, struct klist *k)
124 { 124 {
125 klist_node_init(k, n); 125 klist_node_init(k, n);
126 add_head(k, n); 126 add_head(k, n);
127 } 127 }
128 EXPORT_SYMBOL_GPL(klist_add_head); 128 EXPORT_SYMBOL_GPL(klist_add_head);
129 129
130 /** 130 /**
131 * klist_add_tail - Initialize a klist_node and add it to back. 131 * klist_add_tail - Initialize a klist_node and add it to back.
132 * @n: node we're adding. 132 * @n: node we're adding.
133 * @k: klist it's going on. 133 * @k: klist it's going on.
134 */ 134 */
135 void klist_add_tail(struct klist_node *n, struct klist *k) 135 void klist_add_tail(struct klist_node *n, struct klist *k)
136 { 136 {
137 klist_node_init(k, n); 137 klist_node_init(k, n);
138 add_tail(k, n); 138 add_tail(k, n);
139 } 139 }
140 EXPORT_SYMBOL_GPL(klist_add_tail); 140 EXPORT_SYMBOL_GPL(klist_add_tail);
141 141
142 /** 142 /**
143 * klist_add_after - Init a klist_node and add it after an existing node 143 * klist_add_after - Init a klist_node and add it after an existing node
144 * @n: node we're adding. 144 * @n: node we're adding.
145 * @pos: node to put @n after 145 * @pos: node to put @n after
146 */ 146 */
147 void klist_add_after(struct klist_node *n, struct klist_node *pos) 147 void klist_add_after(struct klist_node *n, struct klist_node *pos)
148 { 148 {
149 struct klist *k = knode_klist(pos); 149 struct klist *k = knode_klist(pos);
150 150
151 klist_node_init(k, n); 151 klist_node_init(k, n);
152 spin_lock(&k->k_lock); 152 spin_lock(&k->k_lock);
153 list_add(&n->n_node, &pos->n_node); 153 list_add(&n->n_node, &pos->n_node);
154 spin_unlock(&k->k_lock); 154 spin_unlock(&k->k_lock);
155 } 155 }
156 EXPORT_SYMBOL_GPL(klist_add_after); 156 EXPORT_SYMBOL_GPL(klist_add_after);
157 157
158 /** 158 /**
159 * klist_add_before - Init a klist_node and add it before an existing node 159 * klist_add_before - Init a klist_node and add it before an existing node
160 * @n: node we're adding. 160 * @n: node we're adding.
161 * @pos: node to put @n after 161 * @pos: node to put @n after
162 */ 162 */
163 void klist_add_before(struct klist_node *n, struct klist_node *pos) 163 void klist_add_before(struct klist_node *n, struct klist_node *pos)
164 { 164 {
165 struct klist *k = knode_klist(pos); 165 struct klist *k = knode_klist(pos);
166 166
167 klist_node_init(k, n); 167 klist_node_init(k, n);
168 spin_lock(&k->k_lock); 168 spin_lock(&k->k_lock);
169 list_add_tail(&n->n_node, &pos->n_node); 169 list_add_tail(&n->n_node, &pos->n_node);
170 spin_unlock(&k->k_lock); 170 spin_unlock(&k->k_lock);
171 } 171 }
172 EXPORT_SYMBOL_GPL(klist_add_before); 172 EXPORT_SYMBOL_GPL(klist_add_before);
173 173
174 struct klist_waiter { 174 struct klist_waiter {
175 struct list_head list; 175 struct list_head list;
176 struct klist_node *node; 176 struct klist_node *node;
177 struct task_struct *process; 177 struct task_struct *process;
178 int woken; 178 int woken;
179 }; 179 };
180 180
181 static DEFINE_SPINLOCK(klist_remove_lock); 181 static DEFINE_SPINLOCK(klist_remove_lock);
182 static LIST_HEAD(klist_remove_waiters); 182 static LIST_HEAD(klist_remove_waiters);
183 183
184 static void klist_release(struct kref *kref) 184 static void klist_release(struct kref *kref)
185 { 185 {
186 struct klist_waiter *waiter, *tmp; 186 struct klist_waiter *waiter, *tmp;
187 struct klist_node *n = container_of(kref, struct klist_node, n_ref); 187 struct klist_node *n = container_of(kref, struct klist_node, n_ref);
188 188
189 WARN_ON(!knode_dead(n)); 189 WARN_ON(!knode_dead(n));
190 list_del(&n->n_node); 190 list_del(&n->n_node);
191 spin_lock(&klist_remove_lock); 191 spin_lock(&klist_remove_lock);
192 list_for_each_entry_safe(waiter, tmp, &klist_remove_waiters, list) { 192 list_for_each_entry_safe(waiter, tmp, &klist_remove_waiters, list) {
193 if (waiter->node != n) 193 if (waiter->node != n)
194 continue; 194 continue;
195 195
196 waiter->woken = 1; 196 waiter->woken = 1;
197 mb(); 197 mb();
198 wake_up_process(waiter->process); 198 wake_up_process(waiter->process);
199 list_del(&waiter->list); 199 list_del(&waiter->list);
200 } 200 }
201 spin_unlock(&klist_remove_lock); 201 spin_unlock(&klist_remove_lock);
202 knode_set_klist(n, NULL); 202 knode_set_klist(n, NULL);
203 } 203 }
204 204
205 static int klist_dec_and_del(struct klist_node *n) 205 static int klist_dec_and_del(struct klist_node *n)
206 { 206 {
207 return kref_put(&n->n_ref, klist_release); 207 return kref_put(&n->n_ref, klist_release);
208 } 208 }
209 209
210 static void klist_put(struct klist_node *n, bool kill) 210 static void klist_put(struct klist_node *n, bool kill)
211 { 211 {
212 struct klist *k = knode_klist(n); 212 struct klist *k = knode_klist(n);
213 void (*put)(struct klist_node *) = k->put; 213 void (*put)(struct klist_node *) = k->put;
214 214
215 spin_lock(&k->k_lock); 215 spin_lock(&k->k_lock);
216 if (kill) 216 if (kill)
217 knode_kill(n); 217 knode_kill(n);
218 if (!klist_dec_and_del(n)) 218 if (!klist_dec_and_del(n))
219 put = NULL; 219 put = NULL;
220 spin_unlock(&k->k_lock); 220 spin_unlock(&k->k_lock);
221 if (put) 221 if (put)
222 put(n); 222 put(n);
223 } 223 }
224 224
225 /** 225 /**
226 * klist_del - Decrement the reference count of node and try to remove. 226 * klist_del - Decrement the reference count of node and try to remove.
227 * @n: node we're deleting. 227 * @n: node we're deleting.
228 */ 228 */
229 void klist_del(struct klist_node *n) 229 void klist_del(struct klist_node *n)
230 { 230 {
231 klist_put(n, true); 231 klist_put(n, true);
232 } 232 }
233 EXPORT_SYMBOL_GPL(klist_del); 233 EXPORT_SYMBOL_GPL(klist_del);
234 234
235 /** 235 /**
236 * klist_remove - Decrement the refcount of node and wait for it to go away. 236 * klist_remove - Decrement the refcount of node and wait for it to go away.
237 * @n: node we're removing. 237 * @n: node we're removing.
238 */ 238 */
239 void klist_remove(struct klist_node *n) 239 void klist_remove(struct klist_node *n)
240 { 240 {
241 struct klist_waiter waiter; 241 struct klist_waiter waiter;
242 242
243 waiter.node = n; 243 waiter.node = n;
244 waiter.process = current; 244 waiter.process = current;
245 waiter.woken = 0; 245 waiter.woken = 0;
246 spin_lock(&klist_remove_lock); 246 spin_lock(&klist_remove_lock);
247 list_add(&waiter.list, &klist_remove_waiters); 247 list_add(&waiter.list, &klist_remove_waiters);
248 spin_unlock(&klist_remove_lock); 248 spin_unlock(&klist_remove_lock);
249 249
250 klist_del(n); 250 klist_del(n);
251 251
252 for (;;) { 252 for (;;) {
253 set_current_state(TASK_UNINTERRUPTIBLE); 253 set_current_state(TASK_UNINTERRUPTIBLE);
254 if (waiter.woken) 254 if (waiter.woken)
255 break; 255 break;
256 schedule(); 256 schedule();
257 } 257 }
258 __set_current_state(TASK_RUNNING); 258 __set_current_state(TASK_RUNNING);
259 } 259 }
260 EXPORT_SYMBOL_GPL(klist_remove); 260 EXPORT_SYMBOL_GPL(klist_remove);
261 261
262 /** 262 /**
263 * klist_node_attached - Say whether a node is bound to a list or not. 263 * klist_node_attached - Say whether a node is bound to a list or not.
264 * @n: Node that we're testing. 264 * @n: Node that we're testing.
265 */ 265 */
266 int klist_node_attached(struct klist_node *n) 266 int klist_node_attached(struct klist_node *n)
267 { 267 {
268 return (n->n_klist != NULL); 268 return (n->n_klist != NULL);
269 } 269 }
270 EXPORT_SYMBOL_GPL(klist_node_attached); 270 EXPORT_SYMBOL_GPL(klist_node_attached);
271 271
272 /** 272 /**
273 * klist_iter_init_node - Initialize a klist_iter structure. 273 * klist_iter_init_node - Initialize a klist_iter structure.
274 * @k: klist we're iterating. 274 * @k: klist we're iterating.
275 * @i: klist_iter we're filling. 275 * @i: klist_iter we're filling.
276 * @n: node to start with. 276 * @n: node to start with.
277 * 277 *
278 * Similar to klist_iter_init(), but starts the action off with @n, 278 * Similar to klist_iter_init(), but starts the action off with @n,
279 * instead of with the list head. 279 * instead of with the list head.
280 */ 280 */
281 void klist_iter_init_node(struct klist *k, struct klist_iter *i, 281 int klist_iter_init_node(struct klist *k, struct klist_iter *i,
282 struct klist_node *n) 282 struct klist_node *n)
283 { 283 {
284 if (n) {
285 kref_get(&n->n_ref);
286 if (!n->n_klist) {
287 kref_put(&n->n_ref);
288 return -ENODEV;
289 }
290 }
284 i->i_klist = k; 291 i->i_klist = k;
285 i->i_cur = n; 292 i->i_cur = n;
286 if (n) 293 return 0;
287 kref_get(&n->n_ref);
288 } 294 }
289 EXPORT_SYMBOL_GPL(klist_iter_init_node); 295 EXPORT_SYMBOL_GPL(klist_iter_init_node);
290 296
291 /** 297 /**
292 * klist_iter_init - Iniitalize a klist_iter structure. 298 * klist_iter_init - Iniitalize a klist_iter structure.
293 * @k: klist we're iterating. 299 * @k: klist we're iterating.
294 * @i: klist_iter structure we're filling. 300 * @i: klist_iter structure we're filling.
295 * 301 *
296 * Similar to klist_iter_init_node(), but start with the list head. 302 * Similar to klist_iter_init_node(), but start with the list head.
297 */ 303 */
298 void klist_iter_init(struct klist *k, struct klist_iter *i) 304 void klist_iter_init(struct klist *k, struct klist_iter *i)
299 { 305 {
300 klist_iter_init_node(k, i, NULL); 306 klist_iter_init_node(k, i, NULL);
301 } 307 }
302 EXPORT_SYMBOL_GPL(klist_iter_init); 308 EXPORT_SYMBOL_GPL(klist_iter_init);
303 309
304 /** 310 /**
305 * klist_iter_exit - Finish a list iteration. 311 * klist_iter_exit - Finish a list iteration.
306 * @i: Iterator structure. 312 * @i: Iterator structure.
307 * 313 *
308 * Must be called when done iterating over list, as it decrements the 314 * Must be called when done iterating over list, as it decrements the
309 * refcount of the current node. Necessary in case iteration exited before 315 * refcount of the current node. Necessary in case iteration exited before
310 * the end of the list was reached, and always good form. 316 * the end of the list was reached, and always good form.
311 */ 317 */
312 void klist_iter_exit(struct klist_iter *i) 318 void klist_iter_exit(struct klist_iter *i)
313 { 319 {
314 if (i->i_cur) { 320 if (i->i_cur) {
315 klist_put(i->i_cur, false); 321 klist_put(i->i_cur, false);
316 i->i_cur = NULL; 322 i->i_cur = NULL;
317 } 323 }
318 } 324 }
319 EXPORT_SYMBOL_GPL(klist_iter_exit); 325 EXPORT_SYMBOL_GPL(klist_iter_exit);
320 326
321 static struct klist_node *to_klist_node(struct list_head *n) 327 static struct klist_node *to_klist_node(struct list_head *n)
322 { 328 {
323 return container_of(n, struct klist_node, n_node); 329 return container_of(n, struct klist_node, n_node);
324 } 330 }
325 331
326 /** 332 /**
327 * klist_next - Ante up next node in list. 333 * klist_next - Ante up next node in list.
328 * @i: Iterator structure. 334 * @i: Iterator structure.
329 * 335 *
330 * First grab list lock. Decrement the reference count of the previous 336 * First grab list lock. Decrement the reference count of the previous
331 * node, if there was one. Grab the next node, increment its reference 337 * node, if there was one. Grab the next node, increment its reference
332 * count, drop the lock, and return that next node. 338 * count, drop the lock, and return that next node.
333 */ 339 */
334 struct klist_node *klist_next(struct klist_iter *i) 340 struct klist_node *klist_next(struct klist_iter *i)
335 { 341 {
336 void (*put)(struct klist_node *) = i->i_klist->put; 342 void (*put)(struct klist_node *) = i->i_klist->put;
337 struct klist_node *last = i->i_cur; 343 struct klist_node *last = i->i_cur;
338 struct klist_node *next; 344 struct klist_node *next;
339 345
340 spin_lock(&i->i_klist->k_lock); 346 spin_lock(&i->i_klist->k_lock);
341 347
342 if (last) { 348 if (last) {
343 next = to_klist_node(last->n_node.next); 349 next = to_klist_node(last->n_node.next);
344 if (!klist_dec_and_del(last)) 350 if (!klist_dec_and_del(last))
345 put = NULL; 351 put = NULL;
346 } else 352 } else
347 next = to_klist_node(i->i_klist->k_list.next); 353 next = to_klist_node(i->i_klist->k_list.next);
348 354
349 i->i_cur = NULL; 355 i->i_cur = NULL;
350 while (next != to_klist_node(&i->i_klist->k_list)) { 356 while (next != to_klist_node(&i->i_klist->k_list)) {
351 if (likely(!knode_dead(next))) { 357 if (likely(!knode_dead(next))) {
352 kref_get(&next->n_ref); 358 kref_get(&next->n_ref);
353 i->i_cur = next; 359 i->i_cur = next;
354 break; 360 break;
355 } 361 }
356 next = to_klist_node(next->n_node.next); 362 next = to_klist_node(next->n_node.next);
357 } 363 }
358 364
359 spin_unlock(&i->i_klist->k_lock); 365 spin_unlock(&i->i_klist->k_lock);
360 366
361 if (put && last) 367 if (put && last)
362 put(last); 368 put(last);
363 return i->i_cur; 369 return i->i_cur;
364 } 370 }
365 EXPORT_SYMBOL_GPL(klist_next); 371 EXPORT_SYMBOL_GPL(klist_next);