Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * drivers/base/core.c - core driver model code (device registration, etc) | |
3 | * | |
4 | * Copyright (c) 2002-3 Patrick Mochel | |
5 | * Copyright (c) 2002-3 Open Source Development Labs | |
64bb5d2c GKH |
6 | * Copyright (c) 2006 Greg Kroah-Hartman <gregkh@suse.de> |
7 | * Copyright (c) 2006 Novell, Inc. | |
1da177e4 LT |
8 | * |
9 | * This file is released under the GPLv2 | |
10 | * | |
11 | */ | |
12 | ||
1da177e4 LT |
13 | #include <linux/device.h> |
14 | #include <linux/err.h> | |
15 | #include <linux/init.h> | |
16 | #include <linux/module.h> | |
17 | #include <linux/slab.h> | |
18 | #include <linux/string.h> | |
23681e47 | 19 | #include <linux/kdev_t.h> |
116af378 | 20 | #include <linux/notifier.h> |
da231fd5 | 21 | #include <linux/genhd.h> |
815d2d50 | 22 | #include <linux/kallsyms.h> |
6188e10d | 23 | #include <linux/semaphore.h> |
f75b1c60 | 24 | #include <linux/mutex.h> |
401097ea | 25 | #include <linux/async.h> |
1da177e4 LT |
26 | |
27 | #include "base.h" | |
28 | #include "power/power.h" | |
29 | ||
4a3ad20c GKH |
30 | int (*platform_notify)(struct device *dev) = NULL; |
31 | int (*platform_notify_remove)(struct device *dev) = NULL; | |
e105b8bf DW |
32 | static struct kobject *dev_kobj; |
33 | struct kobject *sysfs_dev_char_kobj; | |
34 | struct kobject *sysfs_dev_block_kobj; | |
1da177e4 | 35 | |
4e886c29 GKH |
36 | #ifdef CONFIG_BLOCK |
37 | static inline int device_is_not_partition(struct device *dev) | |
38 | { | |
39 | return !(dev->type == &part_type); | |
40 | } | |
41 | #else | |
42 | static inline int device_is_not_partition(struct device *dev) | |
43 | { | |
44 | return 1; | |
45 | } | |
46 | #endif | |
1da177e4 | 47 | |
3e95637a AS |
48 | /** |
49 | * dev_driver_string - Return a device's driver name, if at all possible | |
50 | * @dev: struct device to get the name of | |
51 | * | |
52 | * Will return the device's driver's name if it is bound to a device. If | |
53 | * the device is not bound to a device, it will return the name of the bus | |
54 | * it is attached to. If it is not attached to a bus either, an empty | |
55 | * string will be returned. | |
56 | */ | |
bf9ca69f | 57 | const char *dev_driver_string(const struct device *dev) |
3e95637a AS |
58 | { |
59 | return dev->driver ? dev->driver->name : | |
a456b702 JD |
60 | (dev->bus ? dev->bus->name : |
61 | (dev->class ? dev->class->name : "")); | |
3e95637a | 62 | } |
310a922d | 63 | EXPORT_SYMBOL(dev_driver_string); |
3e95637a | 64 | |
1da177e4 LT |
65 | #define to_dev(obj) container_of(obj, struct device, kobj) |
66 | #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr) | |
67 | ||
4a3ad20c GKH |
68 | static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr, |
69 | char *buf) | |
1da177e4 | 70 | { |
4a3ad20c GKH |
71 | struct device_attribute *dev_attr = to_dev_attr(attr); |
72 | struct device *dev = to_dev(kobj); | |
4a0c20bf | 73 | ssize_t ret = -EIO; |
1da177e4 LT |
74 | |
75 | if (dev_attr->show) | |
54b6f35c | 76 | ret = dev_attr->show(dev, dev_attr, buf); |
815d2d50 AM |
77 | if (ret >= (ssize_t)PAGE_SIZE) { |
78 | print_symbol("dev_attr_show: %s returned bad count\n", | |
79 | (unsigned long)dev_attr->show); | |
80 | } | |
1da177e4 LT |
81 | return ret; |
82 | } | |
83 | ||
4a3ad20c GKH |
84 | static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr, |
85 | const char *buf, size_t count) | |
1da177e4 | 86 | { |
4a3ad20c GKH |
87 | struct device_attribute *dev_attr = to_dev_attr(attr); |
88 | struct device *dev = to_dev(kobj); | |
4a0c20bf | 89 | ssize_t ret = -EIO; |
1da177e4 LT |
90 | |
91 | if (dev_attr->store) | |
54b6f35c | 92 | ret = dev_attr->store(dev, dev_attr, buf, count); |
1da177e4 LT |
93 | return ret; |
94 | } | |
95 | ||
96 | static struct sysfs_ops dev_sysfs_ops = { | |
97 | .show = dev_attr_show, | |
98 | .store = dev_attr_store, | |
99 | }; | |
100 | ||
101 | ||
102 | /** | |
103 | * device_release - free device structure. | |
104 | * @kobj: device's kobject. | |
105 | * | |
106 | * This is called once the reference count for the object | |
107 | * reaches 0. We forward the call to the device's release | |
108 | * method, which should handle actually freeing the structure. | |
109 | */ | |
4a3ad20c | 110 | static void device_release(struct kobject *kobj) |
1da177e4 | 111 | { |
4a3ad20c | 112 | struct device *dev = to_dev(kobj); |
fb069a5d | 113 | struct device_private *p = dev->p; |
1da177e4 LT |
114 | |
115 | if (dev->release) | |
116 | dev->release(dev); | |
f9f852df KS |
117 | else if (dev->type && dev->type->release) |
118 | dev->type->release(dev); | |
2620efef GKH |
119 | else if (dev->class && dev->class->dev_release) |
120 | dev->class->dev_release(dev); | |
f810a5cf AV |
121 | else |
122 | WARN(1, KERN_ERR "Device '%s' does not have a release() " | |
4a3ad20c | 123 | "function, it is broken and must be fixed.\n", |
1e0b2cf9 | 124 | dev_name(dev)); |
fb069a5d | 125 | kfree(p); |
1da177e4 LT |
126 | } |
127 | ||
8f4afc41 | 128 | static struct kobj_type device_ktype = { |
1da177e4 LT |
129 | .release = device_release, |
130 | .sysfs_ops = &dev_sysfs_ops, | |
1da177e4 LT |
131 | }; |
132 | ||
133 | ||
312c004d | 134 | static int dev_uevent_filter(struct kset *kset, struct kobject *kobj) |
1da177e4 LT |
135 | { |
136 | struct kobj_type *ktype = get_ktype(kobj); | |
137 | ||
8f4afc41 | 138 | if (ktype == &device_ktype) { |
1da177e4 LT |
139 | struct device *dev = to_dev(kobj); |
140 | if (dev->bus) | |
141 | return 1; | |
23681e47 GKH |
142 | if (dev->class) |
143 | return 1; | |
1da177e4 LT |
144 | } |
145 | return 0; | |
146 | } | |
147 | ||
312c004d | 148 | static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj) |
1da177e4 LT |
149 | { |
150 | struct device *dev = to_dev(kobj); | |
151 | ||
23681e47 GKH |
152 | if (dev->bus) |
153 | return dev->bus->name; | |
154 | if (dev->class) | |
155 | return dev->class->name; | |
156 | return NULL; | |
1da177e4 LT |
157 | } |
158 | ||
7eff2e7a KS |
159 | static int dev_uevent(struct kset *kset, struct kobject *kobj, |
160 | struct kobj_uevent_env *env) | |
1da177e4 LT |
161 | { |
162 | struct device *dev = to_dev(kobj); | |
1da177e4 LT |
163 | int retval = 0; |
164 | ||
23681e47 GKH |
165 | /* add the major/minor if present */ |
166 | if (MAJOR(dev->devt)) { | |
7eff2e7a KS |
167 | add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt)); |
168 | add_uevent_var(env, "MINOR=%u", MINOR(dev->devt)); | |
23681e47 GKH |
169 | } |
170 | ||
414264f9 | 171 | if (dev->type && dev->type->name) |
7eff2e7a | 172 | add_uevent_var(env, "DEVTYPE=%s", dev->type->name); |
414264f9 | 173 | |
239378f1 | 174 | if (dev->driver) |
7eff2e7a | 175 | add_uevent_var(env, "DRIVER=%s", dev->driver->name); |
239378f1 | 176 | |
a87cb2ac | 177 | #ifdef CONFIG_SYSFS_DEPRECATED |
239378f1 KS |
178 | if (dev->class) { |
179 | struct device *parent = dev->parent; | |
180 | ||
181 | /* find first bus device in parent chain */ | |
182 | while (parent && !parent->bus) | |
183 | parent = parent->parent; | |
184 | if (parent && parent->bus) { | |
185 | const char *path; | |
186 | ||
187 | path = kobject_get_path(&parent->kobj, GFP_KERNEL); | |
2c7afd12 | 188 | if (path) { |
7eff2e7a | 189 | add_uevent_var(env, "PHYSDEVPATH=%s", path); |
2c7afd12 KS |
190 | kfree(path); |
191 | } | |
239378f1 | 192 | |
7eff2e7a | 193 | add_uevent_var(env, "PHYSDEVBUS=%s", parent->bus->name); |
239378f1 KS |
194 | |
195 | if (parent->driver) | |
7eff2e7a KS |
196 | add_uevent_var(env, "PHYSDEVDRIVER=%s", |
197 | parent->driver->name); | |
239378f1 KS |
198 | } |
199 | } else if (dev->bus) { | |
7eff2e7a | 200 | add_uevent_var(env, "PHYSDEVBUS=%s", dev->bus->name); |
239378f1 KS |
201 | |
202 | if (dev->driver) | |
4a3ad20c GKH |
203 | add_uevent_var(env, "PHYSDEVDRIVER=%s", |
204 | dev->driver->name); | |
d81d9d6b | 205 | } |
239378f1 | 206 | #endif |
1da177e4 | 207 | |
7eff2e7a | 208 | /* have the bus specific function add its stuff */ |
312c004d | 209 | if (dev->bus && dev->bus->uevent) { |
7eff2e7a | 210 | retval = dev->bus->uevent(dev, env); |
f9f852df | 211 | if (retval) |
7dc72b28 | 212 | pr_debug("device: '%s': %s: bus uevent() returned %d\n", |
1e0b2cf9 | 213 | dev_name(dev), __func__, retval); |
1da177e4 LT |
214 | } |
215 | ||
7eff2e7a | 216 | /* have the class specific function add its stuff */ |
2620efef | 217 | if (dev->class && dev->class->dev_uevent) { |
7eff2e7a | 218 | retval = dev->class->dev_uevent(dev, env); |
f9f852df | 219 | if (retval) |
7dc72b28 | 220 | pr_debug("device: '%s': %s: class uevent() " |
1e0b2cf9 | 221 | "returned %d\n", dev_name(dev), |
2b3a302a | 222 | __func__, retval); |
f9f852df KS |
223 | } |
224 | ||
7eff2e7a | 225 | /* have the device type specific fuction add its stuff */ |
f9f852df | 226 | if (dev->type && dev->type->uevent) { |
7eff2e7a | 227 | retval = dev->type->uevent(dev, env); |
f9f852df | 228 | if (retval) |
7dc72b28 | 229 | pr_debug("device: '%s': %s: dev_type uevent() " |
1e0b2cf9 | 230 | "returned %d\n", dev_name(dev), |
2b3a302a | 231 | __func__, retval); |
2620efef GKH |
232 | } |
233 | ||
1da177e4 LT |
234 | return retval; |
235 | } | |
236 | ||
312c004d KS |
237 | static struct kset_uevent_ops device_uevent_ops = { |
238 | .filter = dev_uevent_filter, | |
239 | .name = dev_uevent_name, | |
240 | .uevent = dev_uevent, | |
1da177e4 LT |
241 | }; |
242 | ||
16574dcc KS |
243 | static ssize_t show_uevent(struct device *dev, struct device_attribute *attr, |
244 | char *buf) | |
245 | { | |
246 | struct kobject *top_kobj; | |
247 | struct kset *kset; | |
7eff2e7a | 248 | struct kobj_uevent_env *env = NULL; |
16574dcc KS |
249 | int i; |
250 | size_t count = 0; | |
251 | int retval; | |
252 | ||
253 | /* search the kset, the device belongs to */ | |
254 | top_kobj = &dev->kobj; | |
5c5daf65 KS |
255 | while (!top_kobj->kset && top_kobj->parent) |
256 | top_kobj = top_kobj->parent; | |
16574dcc KS |
257 | if (!top_kobj->kset) |
258 | goto out; | |
5c5daf65 | 259 | |
16574dcc KS |
260 | kset = top_kobj->kset; |
261 | if (!kset->uevent_ops || !kset->uevent_ops->uevent) | |
262 | goto out; | |
263 | ||
264 | /* respect filter */ | |
265 | if (kset->uevent_ops && kset->uevent_ops->filter) | |
266 | if (!kset->uevent_ops->filter(kset, &dev->kobj)) | |
267 | goto out; | |
268 | ||
7eff2e7a KS |
269 | env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL); |
270 | if (!env) | |
c7308c81 GKH |
271 | return -ENOMEM; |
272 | ||
16574dcc | 273 | /* let the kset specific function add its keys */ |
7eff2e7a | 274 | retval = kset->uevent_ops->uevent(kset, &dev->kobj, env); |
16574dcc KS |
275 | if (retval) |
276 | goto out; | |
277 | ||
278 | /* copy keys to file */ | |
7eff2e7a KS |
279 | for (i = 0; i < env->envp_idx; i++) |
280 | count += sprintf(&buf[count], "%s\n", env->envp[i]); | |
16574dcc | 281 | out: |
7eff2e7a | 282 | kfree(env); |
16574dcc KS |
283 | return count; |
284 | } | |
285 | ||
a7fd6706 KS |
286 | static ssize_t store_uevent(struct device *dev, struct device_attribute *attr, |
287 | const char *buf, size_t count) | |
288 | { | |
60a96a59 KS |
289 | enum kobject_action action; |
290 | ||
5c5daf65 | 291 | if (kobject_action_type(buf, count, &action) == 0) { |
60a96a59 KS |
292 | kobject_uevent(&dev->kobj, action); |
293 | goto out; | |
294 | } | |
295 | ||
296 | dev_err(dev, "uevent: unsupported action-string; this will " | |
297 | "be ignored in a future kernel version\n"); | |
312c004d | 298 | kobject_uevent(&dev->kobj, KOBJ_ADD); |
60a96a59 | 299 | out: |
a7fd6706 KS |
300 | return count; |
301 | } | |
302 | ||
ad6a1e1c TH |
303 | static struct device_attribute uevent_attr = |
304 | __ATTR(uevent, S_IRUGO | S_IWUSR, show_uevent, store_uevent); | |
305 | ||
621a1672 DT |
306 | static int device_add_attributes(struct device *dev, |
307 | struct device_attribute *attrs) | |
de0ff00d | 308 | { |
621a1672 | 309 | int error = 0; |
de0ff00d | 310 | int i; |
621a1672 DT |
311 | |
312 | if (attrs) { | |
313 | for (i = 0; attr_name(attrs[i]); i++) { | |
314 | error = device_create_file(dev, &attrs[i]); | |
315 | if (error) | |
316 | break; | |
317 | } | |
318 | if (error) | |
319 | while (--i >= 0) | |
320 | device_remove_file(dev, &attrs[i]); | |
321 | } | |
322 | return error; | |
323 | } | |
324 | ||
325 | static void device_remove_attributes(struct device *dev, | |
326 | struct device_attribute *attrs) | |
327 | { | |
328 | int i; | |
329 | ||
330 | if (attrs) | |
331 | for (i = 0; attr_name(attrs[i]); i++) | |
332 | device_remove_file(dev, &attrs[i]); | |
333 | } | |
334 | ||
335 | static int device_add_groups(struct device *dev, | |
336 | struct attribute_group **groups) | |
337 | { | |
de0ff00d | 338 | int error = 0; |
621a1672 | 339 | int i; |
de0ff00d | 340 | |
621a1672 DT |
341 | if (groups) { |
342 | for (i = 0; groups[i]; i++) { | |
343 | error = sysfs_create_group(&dev->kobj, groups[i]); | |
de0ff00d GKH |
344 | if (error) { |
345 | while (--i >= 0) | |
4a3ad20c GKH |
346 | sysfs_remove_group(&dev->kobj, |
347 | groups[i]); | |
621a1672 | 348 | break; |
de0ff00d GKH |
349 | } |
350 | } | |
351 | } | |
de0ff00d GKH |
352 | return error; |
353 | } | |
354 | ||
621a1672 DT |
355 | static void device_remove_groups(struct device *dev, |
356 | struct attribute_group **groups) | |
de0ff00d GKH |
357 | { |
358 | int i; | |
621a1672 DT |
359 | |
360 | if (groups) | |
361 | for (i = 0; groups[i]; i++) | |
362 | sysfs_remove_group(&dev->kobj, groups[i]); | |
de0ff00d GKH |
363 | } |
364 | ||
2620efef GKH |
365 | static int device_add_attrs(struct device *dev) |
366 | { | |
367 | struct class *class = dev->class; | |
f9f852df | 368 | struct device_type *type = dev->type; |
621a1672 | 369 | int error; |
2620efef | 370 | |
621a1672 DT |
371 | if (class) { |
372 | error = device_add_attributes(dev, class->dev_attrs); | |
f9f852df | 373 | if (error) |
621a1672 | 374 | return error; |
2620efef | 375 | } |
f9f852df | 376 | |
621a1672 DT |
377 | if (type) { |
378 | error = device_add_groups(dev, type->groups); | |
f9f852df | 379 | if (error) |
621a1672 | 380 | goto err_remove_class_attrs; |
f9f852df KS |
381 | } |
382 | ||
621a1672 DT |
383 | error = device_add_groups(dev, dev->groups); |
384 | if (error) | |
385 | goto err_remove_type_groups; | |
386 | ||
387 | return 0; | |
388 | ||
389 | err_remove_type_groups: | |
390 | if (type) | |
391 | device_remove_groups(dev, type->groups); | |
392 | err_remove_class_attrs: | |
393 | if (class) | |
394 | device_remove_attributes(dev, class->dev_attrs); | |
395 | ||
2620efef GKH |
396 | return error; |
397 | } | |
398 | ||
399 | static void device_remove_attrs(struct device *dev) | |
400 | { | |
401 | struct class *class = dev->class; | |
f9f852df | 402 | struct device_type *type = dev->type; |
2620efef | 403 | |
621a1672 | 404 | device_remove_groups(dev, dev->groups); |
f9f852df | 405 | |
621a1672 DT |
406 | if (type) |
407 | device_remove_groups(dev, type->groups); | |
408 | ||
409 | if (class) | |
410 | device_remove_attributes(dev, class->dev_attrs); | |
2620efef GKH |
411 | } |
412 | ||
413 | ||
23681e47 GKH |
414 | static ssize_t show_dev(struct device *dev, struct device_attribute *attr, |
415 | char *buf) | |
416 | { | |
417 | return print_dev_t(buf, dev->devt); | |
418 | } | |
419 | ||
ad6a1e1c TH |
420 | static struct device_attribute devt_attr = |
421 | __ATTR(dev, S_IRUGO, show_dev, NULL); | |
422 | ||
881c6cfd GKH |
423 | /* kset to create /sys/devices/ */ |
424 | struct kset *devices_kset; | |
1da177e4 | 425 | |
1da177e4 | 426 | /** |
4a3ad20c GKH |
427 | * device_create_file - create sysfs attribute file for device. |
428 | * @dev: device. | |
429 | * @attr: device attribute descriptor. | |
1da177e4 | 430 | */ |
4a3ad20c | 431 | int device_create_file(struct device *dev, struct device_attribute *attr) |
1da177e4 LT |
432 | { |
433 | int error = 0; | |
0c98b19f | 434 | if (dev) |
1da177e4 | 435 | error = sysfs_create_file(&dev->kobj, &attr->attr); |
1da177e4 LT |
436 | return error; |
437 | } | |
438 | ||
439 | /** | |
4a3ad20c GKH |
440 | * device_remove_file - remove sysfs attribute file. |
441 | * @dev: device. | |
442 | * @attr: device attribute descriptor. | |
1da177e4 | 443 | */ |
4a3ad20c | 444 | void device_remove_file(struct device *dev, struct device_attribute *attr) |
1da177e4 | 445 | { |
0c98b19f | 446 | if (dev) |
1da177e4 | 447 | sysfs_remove_file(&dev->kobj, &attr->attr); |
1da177e4 LT |
448 | } |
449 | ||
2589f188 GKH |
450 | /** |
451 | * device_create_bin_file - create sysfs binary attribute file for device. | |
452 | * @dev: device. | |
453 | * @attr: device binary attribute descriptor. | |
454 | */ | |
455 | int device_create_bin_file(struct device *dev, struct bin_attribute *attr) | |
456 | { | |
457 | int error = -EINVAL; | |
458 | if (dev) | |
459 | error = sysfs_create_bin_file(&dev->kobj, attr); | |
460 | return error; | |
461 | } | |
462 | EXPORT_SYMBOL_GPL(device_create_bin_file); | |
463 | ||
464 | /** | |
465 | * device_remove_bin_file - remove sysfs binary attribute file | |
466 | * @dev: device. | |
467 | * @attr: device binary attribute descriptor. | |
468 | */ | |
469 | void device_remove_bin_file(struct device *dev, struct bin_attribute *attr) | |
470 | { | |
471 | if (dev) | |
472 | sysfs_remove_bin_file(&dev->kobj, attr); | |
473 | } | |
474 | EXPORT_SYMBOL_GPL(device_remove_bin_file); | |
475 | ||
d9a9cdfb | 476 | /** |
523ded71 | 477 | * device_schedule_callback_owner - helper to schedule a callback for a device |
d9a9cdfb AS |
478 | * @dev: device. |
479 | * @func: callback function to invoke later. | |
523ded71 | 480 | * @owner: module owning the callback routine |
d9a9cdfb AS |
481 | * |
482 | * Attribute methods must not unregister themselves or their parent device | |
483 | * (which would amount to the same thing). Attempts to do so will deadlock, | |
484 | * since unregistration is mutually exclusive with driver callbacks. | |
485 | * | |
486 | * Instead methods can call this routine, which will attempt to allocate | |
487 | * and schedule a workqueue request to call back @func with @dev as its | |
488 | * argument in the workqueue's process context. @dev will be pinned until | |
489 | * @func returns. | |
490 | * | |
523ded71 AS |
491 | * This routine is usually called via the inline device_schedule_callback(), |
492 | * which automatically sets @owner to THIS_MODULE. | |
493 | * | |
d9a9cdfb | 494 | * Returns 0 if the request was submitted, -ENOMEM if storage could not |
523ded71 | 495 | * be allocated, -ENODEV if a reference to @owner isn't available. |
d9a9cdfb AS |
496 | * |
497 | * NOTE: This routine won't work if CONFIG_SYSFS isn't set! It uses an | |
498 | * underlying sysfs routine (since it is intended for use by attribute | |
499 | * methods), and if sysfs isn't available you'll get nothing but -ENOSYS. | |
500 | */ | |
523ded71 AS |
501 | int device_schedule_callback_owner(struct device *dev, |
502 | void (*func)(struct device *), struct module *owner) | |
d9a9cdfb AS |
503 | { |
504 | return sysfs_schedule_callback(&dev->kobj, | |
523ded71 | 505 | (void (*)(void *)) func, dev, owner); |
d9a9cdfb | 506 | } |
523ded71 | 507 | EXPORT_SYMBOL_GPL(device_schedule_callback_owner); |
d9a9cdfb | 508 | |
34bb61f9 JB |
509 | static void klist_children_get(struct klist_node *n) |
510 | { | |
f791b8c8 GKH |
511 | struct device_private *p = to_device_private_parent(n); |
512 | struct device *dev = p->device; | |
34bb61f9 JB |
513 | |
514 | get_device(dev); | |
515 | } | |
516 | ||
517 | static void klist_children_put(struct klist_node *n) | |
518 | { | |
f791b8c8 GKH |
519 | struct device_private *p = to_device_private_parent(n); |
520 | struct device *dev = p->device; | |
34bb61f9 JB |
521 | |
522 | put_device(dev); | |
523 | } | |
524 | ||
1da177e4 | 525 | /** |
4a3ad20c GKH |
526 | * device_initialize - init device structure. |
527 | * @dev: device. | |
1da177e4 | 528 | * |
5739411a CH |
529 | * This prepares the device for use by other layers by initializing |
530 | * its fields. | |
4a3ad20c | 531 | * It is the first half of device_register(), if called by |
5739411a CH |
532 | * that function, though it can also be called separately, so one |
533 | * may use @dev's fields. In particular, get_device()/put_device() | |
534 | * may be used for reference counting of @dev after calling this | |
535 | * function. | |
536 | * | |
537 | * NOTE: Use put_device() to give up your reference instead of freeing | |
538 | * @dev directly once you have called this function. | |
1da177e4 | 539 | */ |
1da177e4 LT |
540 | void device_initialize(struct device *dev) |
541 | { | |
881c6cfd | 542 | dev->kobj.kset = devices_kset; |
f9cb074b | 543 | kobject_init(&dev->kobj, &device_ktype); |
1da177e4 | 544 | INIT_LIST_HEAD(&dev->dma_pools); |
af70316a | 545 | init_MUTEX(&dev->sem); |
9ac7849e TH |
546 | spin_lock_init(&dev->devres_lock); |
547 | INIT_LIST_HEAD(&dev->devres_head); | |
0ac85241 | 548 | device_init_wakeup(dev, 0); |
3b98aeaf | 549 | device_pm_init(dev); |
87348136 | 550 | set_dev_node(dev, -1); |
1da177e4 LT |
551 | } |
552 | ||
40fa5422 | 553 | #ifdef CONFIG_SYSFS_DEPRECATED |
da231fd5 KS |
554 | static struct kobject *get_device_parent(struct device *dev, |
555 | struct device *parent) | |
40fa5422 | 556 | { |
da231fd5 | 557 | /* class devices without a parent live in /sys/class/<classname>/ */ |
3eb215de | 558 | if (dev->class && (!parent || parent->class != dev->class)) |
1fbfee6c | 559 | return &dev->class->p->class_subsys.kobj; |
da231fd5 | 560 | /* all other devices keep their parent */ |
40fa5422 | 561 | else if (parent) |
c744aeae | 562 | return &parent->kobj; |
40fa5422 | 563 | |
c744aeae | 564 | return NULL; |
40fa5422 | 565 | } |
da231fd5 KS |
566 | |
567 | static inline void cleanup_device_parent(struct device *dev) {} | |
63b6971a CH |
568 | static inline void cleanup_glue_dir(struct device *dev, |
569 | struct kobject *glue_dir) {} | |
40fa5422 | 570 | #else |
86406245 | 571 | static struct kobject *virtual_device_parent(struct device *dev) |
f0ee61a6 | 572 | { |
86406245 | 573 | static struct kobject *virtual_dir = NULL; |
f0ee61a6 | 574 | |
86406245 | 575 | if (!virtual_dir) |
4ff6abff | 576 | virtual_dir = kobject_create_and_add("virtual", |
881c6cfd | 577 | &devices_kset->kobj); |
f0ee61a6 | 578 | |
86406245 | 579 | return virtual_dir; |
f0ee61a6 GKH |
580 | } |
581 | ||
da231fd5 KS |
582 | static struct kobject *get_device_parent(struct device *dev, |
583 | struct device *parent) | |
40fa5422 | 584 | { |
43968d2f GKH |
585 | int retval; |
586 | ||
86406245 KS |
587 | if (dev->class) { |
588 | struct kobject *kobj = NULL; | |
589 | struct kobject *parent_kobj; | |
590 | struct kobject *k; | |
591 | ||
592 | /* | |
593 | * If we have no parent, we live in "virtual". | |
0f4dafc0 KS |
594 | * Class-devices with a non class-device as parent, live |
595 | * in a "glue" directory to prevent namespace collisions. | |
86406245 KS |
596 | */ |
597 | if (parent == NULL) | |
598 | parent_kobj = virtual_device_parent(dev); | |
599 | else if (parent->class) | |
600 | return &parent->kobj; | |
601 | else | |
602 | parent_kobj = &parent->kobj; | |
603 | ||
604 | /* find our class-directory at the parent and reference it */ | |
7c71448b GKH |
605 | spin_lock(&dev->class->p->class_dirs.list_lock); |
606 | list_for_each_entry(k, &dev->class->p->class_dirs.list, entry) | |
86406245 KS |
607 | if (k->parent == parent_kobj) { |
608 | kobj = kobject_get(k); | |
609 | break; | |
610 | } | |
7c71448b | 611 | spin_unlock(&dev->class->p->class_dirs.list_lock); |
86406245 KS |
612 | if (kobj) |
613 | return kobj; | |
614 | ||
615 | /* or create a new class-directory at the parent device */ | |
43968d2f GKH |
616 | k = kobject_create(); |
617 | if (!k) | |
618 | return NULL; | |
7c71448b | 619 | k->kset = &dev->class->p->class_dirs; |
b2d6db58 | 620 | retval = kobject_add(k, parent_kobj, "%s", dev->class->name); |
43968d2f GKH |
621 | if (retval < 0) { |
622 | kobject_put(k); | |
623 | return NULL; | |
624 | } | |
0f4dafc0 | 625 | /* do not emit an uevent for this simple "glue" directory */ |
43968d2f | 626 | return k; |
86406245 KS |
627 | } |
628 | ||
629 | if (parent) | |
c744aeae CH |
630 | return &parent->kobj; |
631 | return NULL; | |
632 | } | |
da231fd5 | 633 | |
63b6971a | 634 | static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir) |
da231fd5 | 635 | { |
0f4dafc0 | 636 | /* see if we live in a "glue" directory */ |
c1fe539a | 637 | if (!glue_dir || !dev->class || |
7c71448b | 638 | glue_dir->kset != &dev->class->p->class_dirs) |
da231fd5 KS |
639 | return; |
640 | ||
0f4dafc0 | 641 | kobject_put(glue_dir); |
da231fd5 | 642 | } |
63b6971a CH |
643 | |
644 | static void cleanup_device_parent(struct device *dev) | |
645 | { | |
646 | cleanup_glue_dir(dev, dev->kobj.parent); | |
647 | } | |
c744aeae | 648 | #endif |
86406245 | 649 | |
63b6971a | 650 | static void setup_parent(struct device *dev, struct device *parent) |
c744aeae CH |
651 | { |
652 | struct kobject *kobj; | |
653 | kobj = get_device_parent(dev, parent); | |
c744aeae CH |
654 | if (kobj) |
655 | dev->kobj.parent = kobj; | |
40fa5422 | 656 | } |
40fa5422 | 657 | |
2ee97caf CH |
658 | static int device_add_class_symlinks(struct device *dev) |
659 | { | |
660 | int error; | |
661 | ||
662 | if (!dev->class) | |
663 | return 0; | |
da231fd5 | 664 | |
1fbfee6c GKH |
665 | error = sysfs_create_link(&dev->kobj, |
666 | &dev->class->p->class_subsys.kobj, | |
2ee97caf CH |
667 | "subsystem"); |
668 | if (error) | |
669 | goto out; | |
da231fd5 KS |
670 | |
671 | #ifdef CONFIG_SYSFS_DEPRECATED | |
672 | /* stacked class devices need a symlink in the class directory */ | |
1fbfee6c | 673 | if (dev->kobj.parent != &dev->class->p->class_subsys.kobj && |
4e886c29 | 674 | device_is_not_partition(dev)) { |
1fbfee6c | 675 | error = sysfs_create_link(&dev->class->p->class_subsys.kobj, |
1e0b2cf9 | 676 | &dev->kobj, dev_name(dev)); |
2ee97caf CH |
677 | if (error) |
678 | goto out_subsys; | |
679 | } | |
da231fd5 | 680 | |
4e886c29 | 681 | if (dev->parent && device_is_not_partition(dev)) { |
da231fd5 KS |
682 | struct device *parent = dev->parent; |
683 | char *class_name; | |
4f01a757 | 684 | |
da231fd5 KS |
685 | /* |
686 | * stacked class devices have the 'device' link | |
687 | * pointing to the bus device instead of the parent | |
688 | */ | |
689 | while (parent->class && !parent->bus && parent->parent) | |
690 | parent = parent->parent; | |
691 | ||
692 | error = sysfs_create_link(&dev->kobj, | |
693 | &parent->kobj, | |
4f01a757 DT |
694 | "device"); |
695 | if (error) | |
696 | goto out_busid; | |
da231fd5 KS |
697 | |
698 | class_name = make_class_name(dev->class->name, | |
699 | &dev->kobj); | |
700 | if (class_name) | |
701 | error = sysfs_create_link(&dev->parent->kobj, | |
702 | &dev->kobj, class_name); | |
703 | kfree(class_name); | |
704 | if (error) | |
705 | goto out_device; | |
2ee97caf CH |
706 | } |
707 | return 0; | |
708 | ||
2ee97caf | 709 | out_device: |
4e886c29 | 710 | if (dev->parent && device_is_not_partition(dev)) |
2ee97caf | 711 | sysfs_remove_link(&dev->kobj, "device"); |
2ee97caf | 712 | out_busid: |
1fbfee6c | 713 | if (dev->kobj.parent != &dev->class->p->class_subsys.kobj && |
4e886c29 | 714 | device_is_not_partition(dev)) |
1fbfee6c | 715 | sysfs_remove_link(&dev->class->p->class_subsys.kobj, |
1e0b2cf9 | 716 | dev_name(dev)); |
da231fd5 KS |
717 | #else |
718 | /* link in the class directory pointing to the device */ | |
1fbfee6c | 719 | error = sysfs_create_link(&dev->class->p->class_subsys.kobj, |
1e0b2cf9 | 720 | &dev->kobj, dev_name(dev)); |
da231fd5 KS |
721 | if (error) |
722 | goto out_subsys; | |
723 | ||
4e886c29 | 724 | if (dev->parent && device_is_not_partition(dev)) { |
da231fd5 KS |
725 | error = sysfs_create_link(&dev->kobj, &dev->parent->kobj, |
726 | "device"); | |
727 | if (error) | |
728 | goto out_busid; | |
729 | } | |
730 | return 0; | |
731 | ||
732 | out_busid: | |
1e0b2cf9 | 733 | sysfs_remove_link(&dev->class->p->class_subsys.kobj, dev_name(dev)); |
da231fd5 KS |
734 | #endif |
735 | ||
2ee97caf CH |
736 | out_subsys: |
737 | sysfs_remove_link(&dev->kobj, "subsystem"); | |
738 | out: | |
739 | return error; | |
740 | } | |
741 | ||
742 | static void device_remove_class_symlinks(struct device *dev) | |
743 | { | |
744 | if (!dev->class) | |
745 | return; | |
da231fd5 | 746 | |
2ee97caf | 747 | #ifdef CONFIG_SYSFS_DEPRECATED |
4e886c29 | 748 | if (dev->parent && device_is_not_partition(dev)) { |
2ee97caf CH |
749 | char *class_name; |
750 | ||
751 | class_name = make_class_name(dev->class->name, &dev->kobj); | |
752 | if (class_name) { | |
753 | sysfs_remove_link(&dev->parent->kobj, class_name); | |
754 | kfree(class_name); | |
755 | } | |
2ee97caf CH |
756 | sysfs_remove_link(&dev->kobj, "device"); |
757 | } | |
da231fd5 | 758 | |
1fbfee6c | 759 | if (dev->kobj.parent != &dev->class->p->class_subsys.kobj && |
4e886c29 | 760 | device_is_not_partition(dev)) |
1fbfee6c | 761 | sysfs_remove_link(&dev->class->p->class_subsys.kobj, |
1e0b2cf9 | 762 | dev_name(dev)); |
da231fd5 | 763 | #else |
4e886c29 | 764 | if (dev->parent && device_is_not_partition(dev)) |
da231fd5 KS |
765 | sysfs_remove_link(&dev->kobj, "device"); |
766 | ||
1e0b2cf9 | 767 | sysfs_remove_link(&dev->class->p->class_subsys.kobj, dev_name(dev)); |
da231fd5 KS |
768 | #endif |
769 | ||
2ee97caf CH |
770 | sysfs_remove_link(&dev->kobj, "subsystem"); |
771 | } | |
772 | ||
413c239f SR |
773 | /** |
774 | * dev_set_name - set a device name | |
775 | * @dev: device | |
46232366 | 776 | * @fmt: format string for the device's name |
413c239f SR |
777 | */ |
778 | int dev_set_name(struct device *dev, const char *fmt, ...) | |
779 | { | |
780 | va_list vargs; | |
1fa5ae85 | 781 | int err; |
413c239f SR |
782 | |
783 | va_start(vargs, fmt); | |
1fa5ae85 | 784 | err = kobject_set_name_vargs(&dev->kobj, fmt, vargs); |
413c239f | 785 | va_end(vargs); |
1fa5ae85 | 786 | return err; |
413c239f SR |
787 | } |
788 | EXPORT_SYMBOL_GPL(dev_set_name); | |
789 | ||
e105b8bf DW |
790 | /** |
791 | * device_to_dev_kobj - select a /sys/dev/ directory for the device | |
792 | * @dev: device | |
793 | * | |
794 | * By default we select char/ for new entries. Setting class->dev_obj | |
795 | * to NULL prevents an entry from being created. class->dev_kobj must | |
796 | * be set (or cleared) before any devices are registered to the class | |
797 | * otherwise device_create_sys_dev_entry() and | |
798 | * device_remove_sys_dev_entry() will disagree about the the presence | |
799 | * of the link. | |
800 | */ | |
801 | static struct kobject *device_to_dev_kobj(struct device *dev) | |
802 | { | |
803 | struct kobject *kobj; | |
804 | ||
805 | if (dev->class) | |
806 | kobj = dev->class->dev_kobj; | |
807 | else | |
808 | kobj = sysfs_dev_char_kobj; | |
809 | ||
810 | return kobj; | |
811 | } | |
812 | ||
813 | static int device_create_sys_dev_entry(struct device *dev) | |
814 | { | |
815 | struct kobject *kobj = device_to_dev_kobj(dev); | |
816 | int error = 0; | |
817 | char devt_str[15]; | |
818 | ||
819 | if (kobj) { | |
820 | format_dev_t(devt_str, dev->devt); | |
821 | error = sysfs_create_link(kobj, &dev->kobj, devt_str); | |
822 | } | |
823 | ||
824 | return error; | |
825 | } | |
826 | ||
827 | static void device_remove_sys_dev_entry(struct device *dev) | |
828 | { | |
829 | struct kobject *kobj = device_to_dev_kobj(dev); | |
830 | char devt_str[15]; | |
831 | ||
832 | if (kobj) { | |
833 | format_dev_t(devt_str, dev->devt); | |
834 | sysfs_remove_link(kobj, devt_str); | |
835 | } | |
836 | } | |
837 | ||
1da177e4 | 838 | /** |
4a3ad20c GKH |
839 | * device_add - add device to device hierarchy. |
840 | * @dev: device. | |
1da177e4 | 841 | * |
4a3ad20c GKH |
842 | * This is part 2 of device_register(), though may be called |
843 | * separately _iff_ device_initialize() has been called separately. | |
1da177e4 | 844 | * |
5739411a | 845 | * This adds @dev to the kobject hierarchy via kobject_add(), adds it |
4a3ad20c GKH |
846 | * to the global and sibling lists for the device, then |
847 | * adds it to the other relevant subsystems of the driver model. | |
5739411a CH |
848 | * |
849 | * NOTE: _Never_ directly free @dev after calling this function, even | |
850 | * if it returned an error! Always use put_device() to give up your | |
851 | * reference instead. | |
1da177e4 LT |
852 | */ |
853 | int device_add(struct device *dev) | |
854 | { | |
855 | struct device *parent = NULL; | |
c47ed219 | 856 | struct class_interface *class_intf; |
c906a48a | 857 | int error = -EINVAL; |
775b64d2 | 858 | |
1da177e4 | 859 | dev = get_device(dev); |
c906a48a GKH |
860 | if (!dev) |
861 | goto done; | |
862 | ||
fb069a5d GKH |
863 | dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL); |
864 | if (!dev->p) { | |
865 | error = -ENOMEM; | |
866 | goto done; | |
867 | } | |
868 | dev->p->device = dev; | |
f791b8c8 GKH |
869 | klist_init(&dev->p->klist_children, klist_children_get, |
870 | klist_children_put); | |
fb069a5d | 871 | |
1fa5ae85 KS |
872 | /* |
873 | * for statically allocated devices, which should all be converted | |
874 | * some day, we need to initialize the name. We prevent reading back | |
875 | * the name, and force the use of dev_name() | |
876 | */ | |
877 | if (dev->init_name) { | |
878 | dev_set_name(dev, dev->init_name); | |
879 | dev->init_name = NULL; | |
880 | } | |
c906a48a | 881 | |
1fa5ae85 | 882 | if (!dev_name(dev)) |
5c8563d7 | 883 | goto name_error; |
1da177e4 | 884 | |
1e0b2cf9 | 885 | pr_debug("device: '%s': %s\n", dev_name(dev), __func__); |
c205ef48 | 886 | |
1da177e4 | 887 | parent = get_device(dev->parent); |
63b6971a | 888 | setup_parent(dev, parent); |
1da177e4 | 889 | |
0d358f22 YL |
890 | /* use parent numa_node */ |
891 | if (parent) | |
892 | set_dev_node(dev, dev_to_node(parent)); | |
893 | ||
1da177e4 | 894 | /* first, register with generic layer. */ |
8a577ffc KS |
895 | /* we require the name to be set before, and pass NULL */ |
896 | error = kobject_add(&dev->kobj, dev->kobj.parent, NULL); | |
40fa5422 | 897 | if (error) |
1da177e4 | 898 | goto Error; |
a7fd6706 | 899 | |
37022644 BW |
900 | /* notify platform of device entry */ |
901 | if (platform_notify) | |
902 | platform_notify(dev); | |
903 | ||
ad6a1e1c | 904 | error = device_create_file(dev, &uevent_attr); |
a306eea4 CH |
905 | if (error) |
906 | goto attrError; | |
a7fd6706 | 907 | |
23681e47 | 908 | if (MAJOR(dev->devt)) { |
ad6a1e1c TH |
909 | error = device_create_file(dev, &devt_attr); |
910 | if (error) | |
a306eea4 | 911 | goto ueventattrError; |
e105b8bf DW |
912 | |
913 | error = device_create_sys_dev_entry(dev); | |
914 | if (error) | |
915 | goto devtattrError; | |
23681e47 GKH |
916 | } |
917 | ||
2ee97caf CH |
918 | error = device_add_class_symlinks(dev); |
919 | if (error) | |
920 | goto SymlinkError; | |
dc0afa83 CH |
921 | error = device_add_attrs(dev); |
922 | if (error) | |
2620efef | 923 | goto AttrsError; |
dc0afa83 CH |
924 | error = bus_add_device(dev); |
925 | if (error) | |
1da177e4 | 926 | goto BusError; |
3b98aeaf | 927 | error = dpm_sysfs_add(dev); |
57eee3d2 | 928 | if (error) |
3b98aeaf AS |
929 | goto DPMError; |
930 | device_pm_add(dev); | |
ec0676ee AS |
931 | |
932 | /* Notify clients of device addition. This call must come | |
933 | * after dpm_sysf_add() and before kobject_uevent(). | |
934 | */ | |
935 | if (dev->bus) | |
936 | blocking_notifier_call_chain(&dev->bus->p->bus_notifier, | |
937 | BUS_NOTIFY_ADD_DEVICE, dev); | |
938 | ||
83b5fb4c | 939 | kobject_uevent(&dev->kobj, KOBJ_ADD); |
c6a46696 | 940 | bus_attach_device(dev); |
1da177e4 | 941 | if (parent) |
f791b8c8 GKH |
942 | klist_add_tail(&dev->p->knode_parent, |
943 | &parent->p->klist_children); | |
1da177e4 | 944 | |
5d9fd169 | 945 | if (dev->class) { |
f75b1c60 | 946 | mutex_lock(&dev->class->p->class_mutex); |
c47ed219 | 947 | /* tie the class to the device */ |
5a3ceb86 TH |
948 | klist_add_tail(&dev->knode_class, |
949 | &dev->class->p->class_devices); | |
c47ed219 GKH |
950 | |
951 | /* notify any interfaces that the device is here */ | |
184f1f77 GKH |
952 | list_for_each_entry(class_intf, |
953 | &dev->class->p->class_interfaces, node) | |
c47ed219 GKH |
954 | if (class_intf->add_dev) |
955 | class_intf->add_dev(dev, class_intf); | |
f75b1c60 | 956 | mutex_unlock(&dev->class->p->class_mutex); |
5d9fd169 | 957 | } |
c906a48a | 958 | done: |
1da177e4 LT |
959 | put_device(dev); |
960 | return error; | |
3b98aeaf | 961 | DPMError: |
57eee3d2 RW |
962 | bus_remove_device(dev); |
963 | BusError: | |
82f0cf9b | 964 | device_remove_attrs(dev); |
2620efef | 965 | AttrsError: |
2ee97caf CH |
966 | device_remove_class_symlinks(dev); |
967 | SymlinkError: | |
e105b8bf DW |
968 | if (MAJOR(dev->devt)) |
969 | device_remove_sys_dev_entry(dev); | |
970 | devtattrError: | |
ad6a1e1c TH |
971 | if (MAJOR(dev->devt)) |
972 | device_remove_file(dev, &devt_attr); | |
a306eea4 | 973 | ueventattrError: |
ad6a1e1c | 974 | device_remove_file(dev, &uevent_attr); |
23681e47 | 975 | attrError: |
312c004d | 976 | kobject_uevent(&dev->kobj, KOBJ_REMOVE); |
1da177e4 LT |
977 | kobject_del(&dev->kobj); |
978 | Error: | |
63b6971a | 979 | cleanup_device_parent(dev); |
1da177e4 LT |
980 | if (parent) |
981 | put_device(parent); | |
5c8563d7 KS |
982 | name_error: |
983 | kfree(dev->p); | |
984 | dev->p = NULL; | |
c906a48a | 985 | goto done; |
1da177e4 LT |
986 | } |
987 | ||
1da177e4 | 988 | /** |
4a3ad20c GKH |
989 | * device_register - register a device with the system. |
990 | * @dev: pointer to the device structure | |
1da177e4 | 991 | * |
4a3ad20c GKH |
992 | * This happens in two clean steps - initialize the device |
993 | * and add it to the system. The two steps can be called | |
994 | * separately, but this is the easiest and most common. | |
995 | * I.e. you should only call the two helpers separately if | |
996 | * have a clearly defined need to use and refcount the device | |
997 | * before it is added to the hierarchy. | |
5739411a CH |
998 | * |
999 | * NOTE: _Never_ directly free @dev after calling this function, even | |
1000 | * if it returned an error! Always use put_device() to give up the | |
1001 | * reference initialized in this function instead. | |
1da177e4 | 1002 | */ |
1da177e4 LT |
1003 | int device_register(struct device *dev) |
1004 | { | |
1005 | device_initialize(dev); | |
1006 | return device_add(dev); | |
1007 | } | |
1008 | ||
1da177e4 | 1009 | /** |
4a3ad20c GKH |
1010 | * get_device - increment reference count for device. |
1011 | * @dev: device. | |
1da177e4 | 1012 | * |
4a3ad20c GKH |
1013 | * This simply forwards the call to kobject_get(), though |
1014 | * we do take care to provide for the case that we get a NULL | |
1015 | * pointer passed in. | |
1da177e4 | 1016 | */ |
4a3ad20c | 1017 | struct device *get_device(struct device *dev) |
1da177e4 LT |
1018 | { |
1019 | return dev ? to_dev(kobject_get(&dev->kobj)) : NULL; | |
1020 | } | |
1021 | ||
1da177e4 | 1022 | /** |
4a3ad20c GKH |
1023 | * put_device - decrement reference count. |
1024 | * @dev: device in question. | |
1da177e4 | 1025 | */ |
4a3ad20c | 1026 | void put_device(struct device *dev) |
1da177e4 | 1027 | { |
edfaa7c3 | 1028 | /* might_sleep(); */ |
1da177e4 LT |
1029 | if (dev) |
1030 | kobject_put(&dev->kobj); | |
1031 | } | |
1032 | ||
1da177e4 | 1033 | /** |
4a3ad20c GKH |
1034 | * device_del - delete device from system. |
1035 | * @dev: device. | |
1da177e4 | 1036 | * |
4a3ad20c GKH |
1037 | * This is the first part of the device unregistration |
1038 | * sequence. This removes the device from the lists we control | |
1039 | * from here, has it removed from the other driver model | |
1040 | * subsystems it was added to in device_add(), and removes it | |
1041 | * from the kobject hierarchy. | |
1da177e4 | 1042 | * |
4a3ad20c GKH |
1043 | * NOTE: this should be called manually _iff_ device_add() was |
1044 | * also called manually. | |
1da177e4 | 1045 | */ |
4a3ad20c | 1046 | void device_del(struct device *dev) |
1da177e4 | 1047 | { |
4a3ad20c | 1048 | struct device *parent = dev->parent; |
c47ed219 | 1049 | struct class_interface *class_intf; |
1da177e4 | 1050 | |
ec0676ee AS |
1051 | /* Notify clients of device removal. This call must come |
1052 | * before dpm_sysfs_remove(). | |
1053 | */ | |
1054 | if (dev->bus) | |
1055 | blocking_notifier_call_chain(&dev->bus->p->bus_notifier, | |
1056 | BUS_NOTIFY_DEL_DEVICE, dev); | |
775b64d2 | 1057 | device_pm_remove(dev); |
3b98aeaf | 1058 | dpm_sysfs_remove(dev); |
1da177e4 | 1059 | if (parent) |
f791b8c8 | 1060 | klist_del(&dev->p->knode_parent); |
e105b8bf DW |
1061 | if (MAJOR(dev->devt)) { |
1062 | device_remove_sys_dev_entry(dev); | |
ad6a1e1c | 1063 | device_remove_file(dev, &devt_attr); |
e105b8bf | 1064 | } |
b9d9c82b | 1065 | if (dev->class) { |
da231fd5 | 1066 | device_remove_class_symlinks(dev); |
99ef3ef8 | 1067 | |
f75b1c60 | 1068 | mutex_lock(&dev->class->p->class_mutex); |
c47ed219 | 1069 | /* notify any interfaces that the device is now gone */ |
184f1f77 GKH |
1070 | list_for_each_entry(class_intf, |
1071 | &dev->class->p->class_interfaces, node) | |
c47ed219 GKH |
1072 | if (class_intf->remove_dev) |
1073 | class_intf->remove_dev(dev, class_intf); | |
1074 | /* remove the device from the class list */ | |
5a3ceb86 | 1075 | klist_del(&dev->knode_class); |
f75b1c60 | 1076 | mutex_unlock(&dev->class->p->class_mutex); |
b9d9c82b | 1077 | } |
ad6a1e1c | 1078 | device_remove_file(dev, &uevent_attr); |
2620efef | 1079 | device_remove_attrs(dev); |
28953533 | 1080 | bus_remove_device(dev); |
1da177e4 | 1081 | |
2f8d16a9 TH |
1082 | /* |
1083 | * Some platform devices are driven without driver attached | |
1084 | * and managed resources may have been acquired. Make sure | |
1085 | * all resources are released. | |
1086 | */ | |
1087 | devres_release_all(dev); | |
1088 | ||
1da177e4 LT |
1089 | /* Notify the platform of the removal, in case they |
1090 | * need to do anything... | |
1091 | */ | |
1092 | if (platform_notify_remove) | |
1093 | platform_notify_remove(dev); | |
312c004d | 1094 | kobject_uevent(&dev->kobj, KOBJ_REMOVE); |
da231fd5 | 1095 | cleanup_device_parent(dev); |
1da177e4 | 1096 | kobject_del(&dev->kobj); |
da231fd5 | 1097 | put_device(parent); |
1da177e4 LT |
1098 | } |
1099 | ||
1100 | /** | |
4a3ad20c GKH |
1101 | * device_unregister - unregister device from system. |
1102 | * @dev: device going away. | |
1da177e4 | 1103 | * |
4a3ad20c GKH |
1104 | * We do this in two parts, like we do device_register(). First, |
1105 | * we remove it from all the subsystems with device_del(), then | |
1106 | * we decrement the reference count via put_device(). If that | |
1107 | * is the final reference count, the device will be cleaned up | |
1108 | * via device_release() above. Otherwise, the structure will | |
1109 | * stick around until the final reference to the device is dropped. | |
1da177e4 | 1110 | */ |
4a3ad20c | 1111 | void device_unregister(struct device *dev) |
1da177e4 | 1112 | { |
1e0b2cf9 | 1113 | pr_debug("device: '%s': %s\n", dev_name(dev), __func__); |
1da177e4 LT |
1114 | device_del(dev); |
1115 | put_device(dev); | |
1116 | } | |
1117 | ||
4a3ad20c | 1118 | static struct device *next_device(struct klist_iter *i) |
36239577 | 1119 | { |
4a3ad20c | 1120 | struct klist_node *n = klist_next(i); |
f791b8c8 GKH |
1121 | struct device *dev = NULL; |
1122 | struct device_private *p; | |
1123 | ||
1124 | if (n) { | |
1125 | p = to_device_private_parent(n); | |
1126 | dev = p->device; | |
1127 | } | |
1128 | return dev; | |
36239577 | 1129 | } |
1130 | ||
1da177e4 | 1131 | /** |
4a3ad20c GKH |
1132 | * device_for_each_child - device child iterator. |
1133 | * @parent: parent struct device. | |
1134 | * @data: data for the callback. | |
1135 | * @fn: function to be called for each device. | |
1da177e4 | 1136 | * |
4a3ad20c GKH |
1137 | * Iterate over @parent's child devices, and call @fn for each, |
1138 | * passing it @data. | |
1da177e4 | 1139 | * |
4a3ad20c GKH |
1140 | * We check the return of @fn each time. If it returns anything |
1141 | * other than 0, we break out and return that value. | |
1da177e4 | 1142 | */ |
4a3ad20c GKH |
1143 | int device_for_each_child(struct device *parent, void *data, |
1144 | int (*fn)(struct device *dev, void *data)) | |
1da177e4 | 1145 | { |
36239577 | 1146 | struct klist_iter i; |
4a3ad20c | 1147 | struct device *child; |
1da177e4 LT |
1148 | int error = 0; |
1149 | ||
014c90db GKH |
1150 | if (!parent->p) |
1151 | return 0; | |
1152 | ||
f791b8c8 | 1153 | klist_iter_init(&parent->p->klist_children, &i); |
36239577 | 1154 | while ((child = next_device(&i)) && !error) |
1155 | error = fn(child, data); | |
1156 | klist_iter_exit(&i); | |
1da177e4 LT |
1157 | return error; |
1158 | } | |
1159 | ||
5ab69981 CH |
1160 | /** |
1161 | * device_find_child - device iterator for locating a particular device. | |
1162 | * @parent: parent struct device | |
1163 | * @data: Data to pass to match function | |
1164 | * @match: Callback function to check device | |
1165 | * | |
1166 | * This is similar to the device_for_each_child() function above, but it | |
1167 | * returns a reference to a device that is 'found' for later use, as | |
1168 | * determined by the @match callback. | |
1169 | * | |
1170 | * The callback should return 0 if the device doesn't match and non-zero | |
1171 | * if it does. If the callback returns non-zero and a reference to the | |
1172 | * current device can be obtained, this function will return to the caller | |
1173 | * and not iterate over any more devices. | |
1174 | */ | |
4a3ad20c GKH |
1175 | struct device *device_find_child(struct device *parent, void *data, |
1176 | int (*match)(struct device *dev, void *data)) | |
5ab69981 CH |
1177 | { |
1178 | struct klist_iter i; | |
1179 | struct device *child; | |
1180 | ||
1181 | if (!parent) | |
1182 | return NULL; | |
1183 | ||
f791b8c8 | 1184 | klist_iter_init(&parent->p->klist_children, &i); |
5ab69981 CH |
1185 | while ((child = next_device(&i))) |
1186 | if (match(child, data) && get_device(child)) | |
1187 | break; | |
1188 | klist_iter_exit(&i); | |
1189 | return child; | |
1190 | } | |
1191 | ||
1da177e4 LT |
1192 | int __init devices_init(void) |
1193 | { | |
881c6cfd GKH |
1194 | devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL); |
1195 | if (!devices_kset) | |
1196 | return -ENOMEM; | |
e105b8bf DW |
1197 | dev_kobj = kobject_create_and_add("dev", NULL); |
1198 | if (!dev_kobj) | |
1199 | goto dev_kobj_err; | |
1200 | sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj); | |
1201 | if (!sysfs_dev_block_kobj) | |
1202 | goto block_kobj_err; | |
1203 | sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj); | |
1204 | if (!sysfs_dev_char_kobj) | |
1205 | goto char_kobj_err; | |
1206 | ||
881c6cfd | 1207 | return 0; |
e105b8bf DW |
1208 | |
1209 | char_kobj_err: | |
1210 | kobject_put(sysfs_dev_block_kobj); | |
1211 | block_kobj_err: | |
1212 | kobject_put(dev_kobj); | |
1213 | dev_kobj_err: | |
1214 | kset_unregister(devices_kset); | |
1215 | return -ENOMEM; | |
1da177e4 LT |
1216 | } |
1217 | ||
1218 | EXPORT_SYMBOL_GPL(device_for_each_child); | |
5ab69981 | 1219 | EXPORT_SYMBOL_GPL(device_find_child); |
1da177e4 LT |
1220 | |
1221 | EXPORT_SYMBOL_GPL(device_initialize); | |
1222 | EXPORT_SYMBOL_GPL(device_add); | |
1223 | EXPORT_SYMBOL_GPL(device_register); | |
1224 | ||
1225 | EXPORT_SYMBOL_GPL(device_del); | |
1226 | EXPORT_SYMBOL_GPL(device_unregister); | |
1227 | EXPORT_SYMBOL_GPL(get_device); | |
1228 | EXPORT_SYMBOL_GPL(put_device); | |
1da177e4 LT |
1229 | |
1230 | EXPORT_SYMBOL_GPL(device_create_file); | |
1231 | EXPORT_SYMBOL_GPL(device_remove_file); | |
23681e47 | 1232 | |
0aa0dc41 MM |
1233 | struct root_device |
1234 | { | |
1235 | struct device dev; | |
1236 | struct module *owner; | |
1237 | }; | |
1238 | ||
1239 | #define to_root_device(dev) container_of(dev, struct root_device, dev) | |
1240 | ||
1241 | static void root_device_release(struct device *dev) | |
1242 | { | |
1243 | kfree(to_root_device(dev)); | |
1244 | } | |
1245 | ||
1246 | /** | |
1247 | * __root_device_register - allocate and register a root device | |
1248 | * @name: root device name | |
1249 | * @owner: owner module of the root device, usually THIS_MODULE | |
1250 | * | |
1251 | * This function allocates a root device and registers it | |
1252 | * using device_register(). In order to free the returned | |
1253 | * device, use root_device_unregister(). | |
1254 | * | |
1255 | * Root devices are dummy devices which allow other devices | |
1256 | * to be grouped under /sys/devices. Use this function to | |
1257 | * allocate a root device and then use it as the parent of | |
1258 | * any device which should appear under /sys/devices/{name} | |
1259 | * | |
1260 | * The /sys/devices/{name} directory will also contain a | |
1261 | * 'module' symlink which points to the @owner directory | |
1262 | * in sysfs. | |
1263 | * | |
1264 | * Note: You probably want to use root_device_register(). | |
1265 | */ | |
1266 | struct device *__root_device_register(const char *name, struct module *owner) | |
1267 | { | |
1268 | struct root_device *root; | |
1269 | int err = -ENOMEM; | |
1270 | ||
1271 | root = kzalloc(sizeof(struct root_device), GFP_KERNEL); | |
1272 | if (!root) | |
1273 | return ERR_PTR(err); | |
1274 | ||
1275 | err = dev_set_name(&root->dev, name); | |
1276 | if (err) { | |
1277 | kfree(root); | |
1278 | return ERR_PTR(err); | |
1279 | } | |
1280 | ||
1281 | root->dev.release = root_device_release; | |
1282 | ||
1283 | err = device_register(&root->dev); | |
1284 | if (err) { | |
1285 | put_device(&root->dev); | |
1286 | return ERR_PTR(err); | |
1287 | } | |
1288 | ||
1289 | #ifdef CONFIG_MODULE /* gotta find a "cleaner" way to do this */ | |
1290 | if (owner) { | |
1291 | struct module_kobject *mk = &owner->mkobj; | |
1292 | ||
1293 | err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module"); | |
1294 | if (err) { | |
1295 | device_unregister(&root->dev); | |
1296 | return ERR_PTR(err); | |
1297 | } | |
1298 | root->owner = owner; | |
1299 | } | |
1300 | #endif | |
1301 | ||
1302 | return &root->dev; | |
1303 | } | |
1304 | EXPORT_SYMBOL_GPL(__root_device_register); | |
1305 | ||
1306 | /** | |
1307 | * root_device_unregister - unregister and free a root device | |
7cbcf225 | 1308 | * @dev: device going away |
0aa0dc41 MM |
1309 | * |
1310 | * This function unregisters and cleans up a device that was created by | |
1311 | * root_device_register(). | |
1312 | */ | |
1313 | void root_device_unregister(struct device *dev) | |
1314 | { | |
1315 | struct root_device *root = to_root_device(dev); | |
1316 | ||
1317 | if (root->owner) | |
1318 | sysfs_remove_link(&root->dev.kobj, "module"); | |
1319 | ||
1320 | device_unregister(dev); | |
1321 | } | |
1322 | EXPORT_SYMBOL_GPL(root_device_unregister); | |
1323 | ||
23681e47 GKH |
1324 | |
1325 | static void device_create_release(struct device *dev) | |
1326 | { | |
1e0b2cf9 | 1327 | pr_debug("device: '%s': %s\n", dev_name(dev), __func__); |
23681e47 GKH |
1328 | kfree(dev); |
1329 | } | |
1330 | ||
1331 | /** | |
8882b394 | 1332 | * device_create_vargs - creates a device and registers it with sysfs |
42734daf HK |
1333 | * @class: pointer to the struct class that this device should be registered to |
1334 | * @parent: pointer to the parent struct device of this new device, if any | |
1335 | * @devt: the dev_t for the char device to be added | |
8882b394 | 1336 | * @drvdata: the data to be added to the device for callbacks |
42734daf | 1337 | * @fmt: string for the device's name |
8882b394 | 1338 | * @args: va_list for the device's name |
42734daf HK |
1339 | * |
1340 | * This function can be used by char device classes. A struct device | |
1341 | * will be created in sysfs, registered to the specified class. | |
23681e47 | 1342 | * |
23681e47 GKH |
1343 | * A "dev" file will be created, showing the dev_t for the device, if |
1344 | * the dev_t is not 0,0. | |
42734daf HK |
1345 | * If a pointer to a parent struct device is passed in, the newly created |
1346 | * struct device will be a child of that device in sysfs. | |
1347 | * The pointer to the struct device will be returned from the call. | |
1348 | * Any further sysfs files that might be required can be created using this | |
23681e47 GKH |
1349 | * pointer. |
1350 | * | |
1351 | * Note: the struct class passed to this function must have previously | |
1352 | * been created with a call to class_create(). | |
1353 | */ | |
8882b394 GKH |
1354 | struct device *device_create_vargs(struct class *class, struct device *parent, |
1355 | dev_t devt, void *drvdata, const char *fmt, | |
1356 | va_list args) | |
23681e47 | 1357 | { |
23681e47 GKH |
1358 | struct device *dev = NULL; |
1359 | int retval = -ENODEV; | |
1360 | ||
1361 | if (class == NULL || IS_ERR(class)) | |
1362 | goto error; | |
23681e47 GKH |
1363 | |
1364 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); | |
1365 | if (!dev) { | |
1366 | retval = -ENOMEM; | |
1367 | goto error; | |
1368 | } | |
1369 | ||
1370 | dev->devt = devt; | |
1371 | dev->class = class; | |
1372 | dev->parent = parent; | |
1373 | dev->release = device_create_release; | |
8882b394 | 1374 | dev_set_drvdata(dev, drvdata); |
23681e47 | 1375 | |
1fa5ae85 KS |
1376 | retval = kobject_set_name_vargs(&dev->kobj, fmt, args); |
1377 | if (retval) | |
1378 | goto error; | |
1379 | ||
23681e47 GKH |
1380 | retval = device_register(dev); |
1381 | if (retval) | |
1382 | goto error; | |
1383 | ||
23681e47 GKH |
1384 | return dev; |
1385 | ||
1386 | error: | |
286661b3 | 1387 | put_device(dev); |
23681e47 GKH |
1388 | return ERR_PTR(retval); |
1389 | } | |
8882b394 GKH |
1390 | EXPORT_SYMBOL_GPL(device_create_vargs); |
1391 | ||
1392 | /** | |
4e106739 | 1393 | * device_create - creates a device and registers it with sysfs |
8882b394 GKH |
1394 | * @class: pointer to the struct class that this device should be registered to |
1395 | * @parent: pointer to the parent struct device of this new device, if any | |
1396 | * @devt: the dev_t for the char device to be added | |
1397 | * @drvdata: the data to be added to the device for callbacks | |
1398 | * @fmt: string for the device's name | |
1399 | * | |
1400 | * This function can be used by char device classes. A struct device | |
1401 | * will be created in sysfs, registered to the specified class. | |
1402 | * | |
1403 | * A "dev" file will be created, showing the dev_t for the device, if | |
1404 | * the dev_t is not 0,0. | |
1405 | * If a pointer to a parent struct device is passed in, the newly created | |
1406 | * struct device will be a child of that device in sysfs. | |
1407 | * The pointer to the struct device will be returned from the call. | |
1408 | * Any further sysfs files that might be required can be created using this | |
1409 | * pointer. | |
1410 | * | |
1411 | * Note: the struct class passed to this function must have previously | |
1412 | * been created with a call to class_create(). | |
1413 | */ | |
4e106739 GKH |
1414 | struct device *device_create(struct class *class, struct device *parent, |
1415 | dev_t devt, void *drvdata, const char *fmt, ...) | |
8882b394 GKH |
1416 | { |
1417 | va_list vargs; | |
1418 | struct device *dev; | |
1419 | ||
1420 | va_start(vargs, fmt); | |
1421 | dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs); | |
1422 | va_end(vargs); | |
1423 | return dev; | |
1424 | } | |
4e106739 | 1425 | EXPORT_SYMBOL_GPL(device_create); |
8882b394 | 1426 | |
cd35449b | 1427 | static int __match_devt(struct device *dev, void *data) |
23681e47 | 1428 | { |
cd35449b | 1429 | dev_t *devt = data; |
23681e47 | 1430 | |
cd35449b | 1431 | return dev->devt == *devt; |
775b64d2 RW |
1432 | } |
1433 | ||
1434 | /** | |
1435 | * device_destroy - removes a device that was created with device_create() | |
1436 | * @class: pointer to the struct class that this device was registered with | |
1437 | * @devt: the dev_t of the device that was previously registered | |
1438 | * | |
1439 | * This call unregisters and cleans up a device that was created with a | |
1440 | * call to device_create(). | |
1441 | */ | |
1442 | void device_destroy(struct class *class, dev_t devt) | |
1443 | { | |
1444 | struct device *dev; | |
23681e47 | 1445 | |
695794ae | 1446 | dev = class_find_device(class, NULL, &devt, __match_devt); |
cd35449b DY |
1447 | if (dev) { |
1448 | put_device(dev); | |
23681e47 | 1449 | device_unregister(dev); |
cd35449b | 1450 | } |
23681e47 GKH |
1451 | } |
1452 | EXPORT_SYMBOL_GPL(device_destroy); | |
a2de48ca GKH |
1453 | |
1454 | /** | |
1455 | * device_rename - renames a device | |
1456 | * @dev: the pointer to the struct device to be renamed | |
1457 | * @new_name: the new name of the device | |
030c1d2b EB |
1458 | * |
1459 | * It is the responsibility of the caller to provide mutual | |
1460 | * exclusion between two different calls of device_rename | |
1461 | * on the same device to ensure that new_name is valid and | |
1462 | * won't conflict with other devices. | |
a2de48ca GKH |
1463 | */ |
1464 | int device_rename(struct device *dev, char *new_name) | |
1465 | { | |
1466 | char *old_class_name = NULL; | |
1467 | char *new_class_name = NULL; | |
2ee97caf | 1468 | char *old_device_name = NULL; |
a2de48ca GKH |
1469 | int error; |
1470 | ||
1471 | dev = get_device(dev); | |
1472 | if (!dev) | |
1473 | return -EINVAL; | |
1474 | ||
1e0b2cf9 | 1475 | pr_debug("device: '%s': %s: renaming to '%s'\n", dev_name(dev), |
2b3a302a | 1476 | __func__, new_name); |
a2de48ca | 1477 | |
99ef3ef8 | 1478 | #ifdef CONFIG_SYSFS_DEPRECATED |
a2de48ca GKH |
1479 | if ((dev->class) && (dev->parent)) |
1480 | old_class_name = make_class_name(dev->class->name, &dev->kobj); | |
99ef3ef8 | 1481 | #endif |
a2de48ca | 1482 | |
1fa5ae85 | 1483 | old_device_name = kstrdup(dev_name(dev), GFP_KERNEL); |
2ee97caf CH |
1484 | if (!old_device_name) { |
1485 | error = -ENOMEM; | |
1486 | goto out; | |
a2de48ca | 1487 | } |
a2de48ca GKH |
1488 | |
1489 | error = kobject_rename(&dev->kobj, new_name); | |
1fa5ae85 | 1490 | if (error) |
2ee97caf | 1491 | goto out; |
a2de48ca | 1492 | |
99ef3ef8 | 1493 | #ifdef CONFIG_SYSFS_DEPRECATED |
a2de48ca GKH |
1494 | if (old_class_name) { |
1495 | new_class_name = make_class_name(dev->class->name, &dev->kobj); | |
1496 | if (new_class_name) { | |
36ce6dad CH |
1497 | error = sysfs_create_link_nowarn(&dev->parent->kobj, |
1498 | &dev->kobj, | |
1499 | new_class_name); | |
2ee97caf CH |
1500 | if (error) |
1501 | goto out; | |
a2de48ca GKH |
1502 | sysfs_remove_link(&dev->parent->kobj, old_class_name); |
1503 | } | |
1504 | } | |
60b8cabd | 1505 | #else |
a2de48ca | 1506 | if (dev->class) { |
36ce6dad | 1507 | error = sysfs_create_link_nowarn(&dev->class->p->class_subsys.kobj, |
1e0b2cf9 | 1508 | &dev->kobj, dev_name(dev)); |
0599ad53 SH |
1509 | if (error) |
1510 | goto out; | |
1fbfee6c | 1511 | sysfs_remove_link(&dev->class->p->class_subsys.kobj, |
7c71448b | 1512 | old_device_name); |
a2de48ca | 1513 | } |
60b8cabd KS |
1514 | #endif |
1515 | ||
2ee97caf | 1516 | out: |
a2de48ca GKH |
1517 | put_device(dev); |
1518 | ||
a2de48ca | 1519 | kfree(new_class_name); |
952ab431 | 1520 | kfree(old_class_name); |
2ee97caf | 1521 | kfree(old_device_name); |
a2de48ca GKH |
1522 | |
1523 | return error; | |
1524 | } | |
a2807dbc | 1525 | EXPORT_SYMBOL_GPL(device_rename); |
8a82472f CH |
1526 | |
1527 | static int device_move_class_links(struct device *dev, | |
1528 | struct device *old_parent, | |
1529 | struct device *new_parent) | |
1530 | { | |
f7f3461d | 1531 | int error = 0; |
8a82472f | 1532 | #ifdef CONFIG_SYSFS_DEPRECATED |
8a82472f CH |
1533 | char *class_name; |
1534 | ||
1535 | class_name = make_class_name(dev->class->name, &dev->kobj); | |
1536 | if (!class_name) { | |
cb360bbf | 1537 | error = -ENOMEM; |
8a82472f CH |
1538 | goto out; |
1539 | } | |
1540 | if (old_parent) { | |
1541 | sysfs_remove_link(&dev->kobj, "device"); | |
1542 | sysfs_remove_link(&old_parent->kobj, class_name); | |
1543 | } | |
c744aeae CH |
1544 | if (new_parent) { |
1545 | error = sysfs_create_link(&dev->kobj, &new_parent->kobj, | |
1546 | "device"); | |
1547 | if (error) | |
1548 | goto out; | |
1549 | error = sysfs_create_link(&new_parent->kobj, &dev->kobj, | |
1550 | class_name); | |
1551 | if (error) | |
1552 | sysfs_remove_link(&dev->kobj, "device"); | |
4a3ad20c | 1553 | } else |
c744aeae | 1554 | error = 0; |
8a82472f CH |
1555 | out: |
1556 | kfree(class_name); | |
1557 | return error; | |
1558 | #else | |
f7f3461d GKH |
1559 | if (old_parent) |
1560 | sysfs_remove_link(&dev->kobj, "device"); | |
1561 | if (new_parent) | |
1562 | error = sysfs_create_link(&dev->kobj, &new_parent->kobj, | |
1563 | "device"); | |
1564 | return error; | |
8a82472f CH |
1565 | #endif |
1566 | } | |
1567 | ||
1568 | /** | |
1569 | * device_move - moves a device to a new parent | |
1570 | * @dev: the pointer to the struct device to be moved | |
c744aeae | 1571 | * @new_parent: the new parent of the device (can by NULL) |
ffa6a705 | 1572 | * @dpm_order: how to reorder the dpm_list |
8a82472f | 1573 | */ |
ffa6a705 CH |
1574 | int device_move(struct device *dev, struct device *new_parent, |
1575 | enum dpm_order dpm_order) | |
8a82472f CH |
1576 | { |
1577 | int error; | |
1578 | struct device *old_parent; | |
c744aeae | 1579 | struct kobject *new_parent_kobj; |
8a82472f CH |
1580 | |
1581 | dev = get_device(dev); | |
1582 | if (!dev) | |
1583 | return -EINVAL; | |
1584 | ||
ffa6a705 | 1585 | device_pm_lock(); |
8a82472f | 1586 | new_parent = get_device(new_parent); |
4a3ad20c | 1587 | new_parent_kobj = get_device_parent(dev, new_parent); |
63b6971a | 1588 | |
1e0b2cf9 KS |
1589 | pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev), |
1590 | __func__, new_parent ? dev_name(new_parent) : "<NULL>"); | |
c744aeae | 1591 | error = kobject_move(&dev->kobj, new_parent_kobj); |
8a82472f | 1592 | if (error) { |
63b6971a | 1593 | cleanup_glue_dir(dev, new_parent_kobj); |
8a82472f CH |
1594 | put_device(new_parent); |
1595 | goto out; | |
1596 | } | |
1597 | old_parent = dev->parent; | |
1598 | dev->parent = new_parent; | |
1599 | if (old_parent) | |
f791b8c8 | 1600 | klist_remove(&dev->p->knode_parent); |
0d358f22 | 1601 | if (new_parent) { |
f791b8c8 GKH |
1602 | klist_add_tail(&dev->p->knode_parent, |
1603 | &new_parent->p->klist_children); | |
0d358f22 YL |
1604 | set_dev_node(dev, dev_to_node(new_parent)); |
1605 | } | |
1606 | ||
8a82472f CH |
1607 | if (!dev->class) |
1608 | goto out_put; | |
1609 | error = device_move_class_links(dev, old_parent, new_parent); | |
1610 | if (error) { | |
1611 | /* We ignore errors on cleanup since we're hosed anyway... */ | |
1612 | device_move_class_links(dev, new_parent, old_parent); | |
1613 | if (!kobject_move(&dev->kobj, &old_parent->kobj)) { | |
c744aeae | 1614 | if (new_parent) |
f791b8c8 | 1615 | klist_remove(&dev->p->knode_parent); |
0d358f22 YL |
1616 | dev->parent = old_parent; |
1617 | if (old_parent) { | |
f791b8c8 GKH |
1618 | klist_add_tail(&dev->p->knode_parent, |
1619 | &old_parent->p->klist_children); | |
0d358f22 YL |
1620 | set_dev_node(dev, dev_to_node(old_parent)); |
1621 | } | |
8a82472f | 1622 | } |
63b6971a | 1623 | cleanup_glue_dir(dev, new_parent_kobj); |
8a82472f CH |
1624 | put_device(new_parent); |
1625 | goto out; | |
1626 | } | |
ffa6a705 CH |
1627 | switch (dpm_order) { |
1628 | case DPM_ORDER_NONE: | |
1629 | break; | |
1630 | case DPM_ORDER_DEV_AFTER_PARENT: | |
1631 | device_pm_move_after(dev, new_parent); | |
1632 | break; | |
1633 | case DPM_ORDER_PARENT_BEFORE_DEV: | |
1634 | device_pm_move_before(new_parent, dev); | |
1635 | break; | |
1636 | case DPM_ORDER_DEV_LAST: | |
1637 | device_pm_move_last(dev); | |
1638 | break; | |
1639 | } | |
8a82472f CH |
1640 | out_put: |
1641 | put_device(old_parent); | |
1642 | out: | |
ffa6a705 | 1643 | device_pm_unlock(); |
8a82472f CH |
1644 | put_device(dev); |
1645 | return error; | |
1646 | } | |
8a82472f | 1647 | EXPORT_SYMBOL_GPL(device_move); |
37b0c020 GKH |
1648 | |
1649 | /** | |
1650 | * device_shutdown - call ->shutdown() on each device to shutdown. | |
1651 | */ | |
1652 | void device_shutdown(void) | |
1653 | { | |
4a3ad20c | 1654 | struct device *dev, *devn; |
37b0c020 GKH |
1655 | |
1656 | list_for_each_entry_safe_reverse(dev, devn, &devices_kset->list, | |
1657 | kobj.entry) { | |
1658 | if (dev->bus && dev->bus->shutdown) { | |
1659 | dev_dbg(dev, "shutdown\n"); | |
1660 | dev->bus->shutdown(dev); | |
1661 | } else if (dev->driver && dev->driver->shutdown) { | |
1662 | dev_dbg(dev, "shutdown\n"); | |
1663 | dev->driver->shutdown(dev); | |
1664 | } | |
1665 | } | |
e105b8bf DW |
1666 | kobject_put(sysfs_dev_char_kobj); |
1667 | kobject_put(sysfs_dev_block_kobj); | |
1668 | kobject_put(dev_kobj); | |
401097ea | 1669 | async_synchronize_full(); |
37b0c020 | 1670 | } |