]>
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> |
1da177e4 LT |
21 | |
22 | #include <asm/semaphore.h> | |
23 | ||
24 | #include "base.h" | |
25 | #include "power/power.h" | |
26 | ||
27 | int (*platform_notify)(struct device * dev) = NULL; | |
28 | int (*platform_notify_remove)(struct device * dev) = NULL; | |
29 | ||
30 | /* | |
31 | * sysfs bindings for devices. | |
32 | */ | |
33 | ||
3e95637a AS |
34 | /** |
35 | * dev_driver_string - Return a device's driver name, if at all possible | |
36 | * @dev: struct device to get the name of | |
37 | * | |
38 | * Will return the device's driver's name if it is bound to a device. If | |
39 | * the device is not bound to a device, it will return the name of the bus | |
40 | * it is attached to. If it is not attached to a bus either, an empty | |
41 | * string will be returned. | |
42 | */ | |
43 | const char *dev_driver_string(struct device *dev) | |
44 | { | |
45 | return dev->driver ? dev->driver->name : | |
a456b702 JD |
46 | (dev->bus ? dev->bus->name : |
47 | (dev->class ? dev->class->name : "")); | |
3e95637a | 48 | } |
310a922d | 49 | EXPORT_SYMBOL(dev_driver_string); |
3e95637a | 50 | |
1da177e4 LT |
51 | #define to_dev(obj) container_of(obj, struct device, kobj) |
52 | #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr) | |
53 | ||
1da177e4 LT |
54 | static ssize_t |
55 | dev_attr_show(struct kobject * kobj, struct attribute * attr, char * buf) | |
56 | { | |
57 | struct device_attribute * dev_attr = to_dev_attr(attr); | |
58 | struct device * dev = to_dev(kobj); | |
4a0c20bf | 59 | ssize_t ret = -EIO; |
1da177e4 LT |
60 | |
61 | if (dev_attr->show) | |
54b6f35c | 62 | ret = dev_attr->show(dev, dev_attr, buf); |
1da177e4 LT |
63 | return ret; |
64 | } | |
65 | ||
66 | static ssize_t | |
67 | dev_attr_store(struct kobject * kobj, struct attribute * attr, | |
68 | const char * buf, size_t count) | |
69 | { | |
70 | struct device_attribute * dev_attr = to_dev_attr(attr); | |
71 | struct device * dev = to_dev(kobj); | |
4a0c20bf | 72 | ssize_t ret = -EIO; |
1da177e4 LT |
73 | |
74 | if (dev_attr->store) | |
54b6f35c | 75 | ret = dev_attr->store(dev, dev_attr, buf, count); |
1da177e4 LT |
76 | return ret; |
77 | } | |
78 | ||
79 | static struct sysfs_ops dev_sysfs_ops = { | |
80 | .show = dev_attr_show, | |
81 | .store = dev_attr_store, | |
82 | }; | |
83 | ||
84 | ||
85 | /** | |
86 | * device_release - free device structure. | |
87 | * @kobj: device's kobject. | |
88 | * | |
89 | * This is called once the reference count for the object | |
90 | * reaches 0. We forward the call to the device's release | |
91 | * method, which should handle actually freeing the structure. | |
92 | */ | |
93 | static void device_release(struct kobject * kobj) | |
94 | { | |
95 | struct device * dev = to_dev(kobj); | |
96 | ||
97 | if (dev->release) | |
98 | dev->release(dev); | |
f9f852df KS |
99 | else if (dev->type && dev->type->release) |
100 | dev->type->release(dev); | |
2620efef GKH |
101 | else if (dev->class && dev->class->dev_release) |
102 | dev->class->dev_release(dev); | |
1da177e4 LT |
103 | else { |
104 | printk(KERN_ERR "Device '%s' does not have a release() function, " | |
105 | "it is broken and must be fixed.\n", | |
106 | dev->bus_id); | |
107 | WARN_ON(1); | |
108 | } | |
109 | } | |
110 | ||
111 | static struct kobj_type ktype_device = { | |
112 | .release = device_release, | |
113 | .sysfs_ops = &dev_sysfs_ops, | |
1da177e4 LT |
114 | }; |
115 | ||
116 | ||
312c004d | 117 | static int dev_uevent_filter(struct kset *kset, struct kobject *kobj) |
1da177e4 LT |
118 | { |
119 | struct kobj_type *ktype = get_ktype(kobj); | |
120 | ||
121 | if (ktype == &ktype_device) { | |
122 | struct device *dev = to_dev(kobj); | |
123 | if (dev->bus) | |
124 | return 1; | |
23681e47 GKH |
125 | if (dev->class) |
126 | return 1; | |
1da177e4 LT |
127 | } |
128 | return 0; | |
129 | } | |
130 | ||
312c004d | 131 | static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj) |
1da177e4 LT |
132 | { |
133 | struct device *dev = to_dev(kobj); | |
134 | ||
23681e47 GKH |
135 | if (dev->bus) |
136 | return dev->bus->name; | |
137 | if (dev->class) | |
138 | return dev->class->name; | |
139 | return NULL; | |
1da177e4 LT |
140 | } |
141 | ||
312c004d | 142 | static int dev_uevent(struct kset *kset, struct kobject *kobj, char **envp, |
1da177e4 LT |
143 | int num_envp, char *buffer, int buffer_size) |
144 | { | |
145 | struct device *dev = to_dev(kobj); | |
146 | int i = 0; | |
147 | int length = 0; | |
148 | int retval = 0; | |
149 | ||
23681e47 GKH |
150 | /* add the major/minor if present */ |
151 | if (MAJOR(dev->devt)) { | |
152 | add_uevent_var(envp, num_envp, &i, | |
153 | buffer, buffer_size, &length, | |
154 | "MAJOR=%u", MAJOR(dev->devt)); | |
155 | add_uevent_var(envp, num_envp, &i, | |
156 | buffer, buffer_size, &length, | |
157 | "MINOR=%u", MINOR(dev->devt)); | |
158 | } | |
159 | ||
239378f1 | 160 | if (dev->driver) |
d81d9d6b KS |
161 | add_uevent_var(envp, num_envp, &i, |
162 | buffer, buffer_size, &length, | |
163 | "DRIVER=%s", dev->driver->name); | |
239378f1 | 164 | |
a87cb2ac | 165 | #ifdef CONFIG_SYSFS_DEPRECATED |
239378f1 KS |
166 | if (dev->class) { |
167 | struct device *parent = dev->parent; | |
168 | ||
169 | /* find first bus device in parent chain */ | |
170 | while (parent && !parent->bus) | |
171 | parent = parent->parent; | |
172 | if (parent && parent->bus) { | |
173 | const char *path; | |
174 | ||
175 | path = kobject_get_path(&parent->kobj, GFP_KERNEL); | |
176 | add_uevent_var(envp, num_envp, &i, | |
177 | buffer, buffer_size, &length, | |
178 | "PHYSDEVPATH=%s", path); | |
179 | kfree(path); | |
180 | ||
181 | add_uevent_var(envp, num_envp, &i, | |
182 | buffer, buffer_size, &length, | |
183 | "PHYSDEVBUS=%s", parent->bus->name); | |
184 | ||
185 | if (parent->driver) | |
186 | add_uevent_var(envp, num_envp, &i, | |
187 | buffer, buffer_size, &length, | |
188 | "PHYSDEVDRIVER=%s", parent->driver->name); | |
189 | } | |
190 | } else if (dev->bus) { | |
312c004d KS |
191 | add_uevent_var(envp, num_envp, &i, |
192 | buffer, buffer_size, &length, | |
239378f1 KS |
193 | "PHYSDEVBUS=%s", dev->bus->name); |
194 | ||
195 | if (dev->driver) | |
196 | add_uevent_var(envp, num_envp, &i, | |
197 | buffer, buffer_size, &length, | |
198 | "PHYSDEVDRIVER=%s", dev->driver->name); | |
d81d9d6b | 199 | } |
239378f1 | 200 | #endif |
1da177e4 LT |
201 | |
202 | /* terminate, set to next free slot, shrink available space */ | |
203 | envp[i] = NULL; | |
204 | envp = &envp[i]; | |
205 | num_envp -= i; | |
206 | buffer = &buffer[length]; | |
207 | buffer_size -= length; | |
208 | ||
312c004d | 209 | if (dev->bus && dev->bus->uevent) { |
1da177e4 | 210 | /* have the bus specific function add its stuff */ |
312c004d | 211 | retval = dev->bus->uevent(dev, envp, num_envp, buffer, buffer_size); |
f9f852df KS |
212 | if (retval) |
213 | pr_debug ("%s: bus uevent() returned %d\n", | |
1da177e4 | 214 | __FUNCTION__, retval); |
1da177e4 LT |
215 | } |
216 | ||
2620efef GKH |
217 | if (dev->class && dev->class->dev_uevent) { |
218 | /* have the class specific function add its stuff */ | |
219 | retval = dev->class->dev_uevent(dev, envp, num_envp, buffer, buffer_size); | |
f9f852df KS |
220 | if (retval) |
221 | pr_debug("%s: class uevent() returned %d\n", | |
222 | __FUNCTION__, retval); | |
223 | } | |
224 | ||
225 | if (dev->type && dev->type->uevent) { | |
226 | /* have the device type specific fuction add its stuff */ | |
227 | retval = dev->type->uevent(dev, envp, num_envp, buffer, buffer_size); | |
228 | if (retval) | |
229 | pr_debug("%s: dev_type uevent() returned %d\n", | |
230 | __FUNCTION__, retval); | |
2620efef GKH |
231 | } |
232 | ||
1da177e4 LT |
233 | return retval; |
234 | } | |
235 | ||
312c004d KS |
236 | static struct kset_uevent_ops device_uevent_ops = { |
237 | .filter = dev_uevent_filter, | |
238 | .name = dev_uevent_name, | |
239 | .uevent = dev_uevent, | |
1da177e4 LT |
240 | }; |
241 | ||
a7fd6706 KS |
242 | static ssize_t store_uevent(struct device *dev, struct device_attribute *attr, |
243 | const char *buf, size_t count) | |
244 | { | |
312c004d | 245 | kobject_uevent(&dev->kobj, KOBJ_ADD); |
a7fd6706 KS |
246 | return count; |
247 | } | |
248 | ||
621a1672 DT |
249 | static int device_add_attributes(struct device *dev, |
250 | struct device_attribute *attrs) | |
de0ff00d | 251 | { |
621a1672 | 252 | int error = 0; |
de0ff00d | 253 | int i; |
621a1672 DT |
254 | |
255 | if (attrs) { | |
256 | for (i = 0; attr_name(attrs[i]); i++) { | |
257 | error = device_create_file(dev, &attrs[i]); | |
258 | if (error) | |
259 | break; | |
260 | } | |
261 | if (error) | |
262 | while (--i >= 0) | |
263 | device_remove_file(dev, &attrs[i]); | |
264 | } | |
265 | return error; | |
266 | } | |
267 | ||
268 | static void device_remove_attributes(struct device *dev, | |
269 | struct device_attribute *attrs) | |
270 | { | |
271 | int i; | |
272 | ||
273 | if (attrs) | |
274 | for (i = 0; attr_name(attrs[i]); i++) | |
275 | device_remove_file(dev, &attrs[i]); | |
276 | } | |
277 | ||
278 | static int device_add_groups(struct device *dev, | |
279 | struct attribute_group **groups) | |
280 | { | |
de0ff00d | 281 | int error = 0; |
621a1672 | 282 | int i; |
de0ff00d | 283 | |
621a1672 DT |
284 | if (groups) { |
285 | for (i = 0; groups[i]; i++) { | |
286 | error = sysfs_create_group(&dev->kobj, groups[i]); | |
de0ff00d GKH |
287 | if (error) { |
288 | while (--i >= 0) | |
621a1672 DT |
289 | sysfs_remove_group(&dev->kobj, groups[i]); |
290 | break; | |
de0ff00d GKH |
291 | } |
292 | } | |
293 | } | |
de0ff00d GKH |
294 | return error; |
295 | } | |
296 | ||
621a1672 DT |
297 | static void device_remove_groups(struct device *dev, |
298 | struct attribute_group **groups) | |
de0ff00d GKH |
299 | { |
300 | int i; | |
621a1672 DT |
301 | |
302 | if (groups) | |
303 | for (i = 0; groups[i]; i++) | |
304 | sysfs_remove_group(&dev->kobj, groups[i]); | |
de0ff00d GKH |
305 | } |
306 | ||
2620efef GKH |
307 | static int device_add_attrs(struct device *dev) |
308 | { | |
309 | struct class *class = dev->class; | |
f9f852df | 310 | struct device_type *type = dev->type; |
621a1672 | 311 | int error; |
2620efef | 312 | |
621a1672 DT |
313 | if (class) { |
314 | error = device_add_attributes(dev, class->dev_attrs); | |
f9f852df | 315 | if (error) |
621a1672 | 316 | return error; |
2620efef | 317 | } |
f9f852df | 318 | |
621a1672 DT |
319 | if (type) { |
320 | error = device_add_groups(dev, type->groups); | |
f9f852df | 321 | if (error) |
621a1672 | 322 | goto err_remove_class_attrs; |
f9f852df KS |
323 | } |
324 | ||
621a1672 DT |
325 | error = device_add_groups(dev, dev->groups); |
326 | if (error) | |
327 | goto err_remove_type_groups; | |
328 | ||
329 | return 0; | |
330 | ||
331 | err_remove_type_groups: | |
332 | if (type) | |
333 | device_remove_groups(dev, type->groups); | |
334 | err_remove_class_attrs: | |
335 | if (class) | |
336 | device_remove_attributes(dev, class->dev_attrs); | |
337 | ||
2620efef GKH |
338 | return error; |
339 | } | |
340 | ||
341 | static void device_remove_attrs(struct device *dev) | |
342 | { | |
343 | struct class *class = dev->class; | |
f9f852df | 344 | struct device_type *type = dev->type; |
2620efef | 345 | |
621a1672 | 346 | device_remove_groups(dev, dev->groups); |
f9f852df | 347 | |
621a1672 DT |
348 | if (type) |
349 | device_remove_groups(dev, type->groups); | |
350 | ||
351 | if (class) | |
352 | device_remove_attributes(dev, class->dev_attrs); | |
2620efef GKH |
353 | } |
354 | ||
355 | ||
23681e47 GKH |
356 | static ssize_t show_dev(struct device *dev, struct device_attribute *attr, |
357 | char *buf) | |
358 | { | |
359 | return print_dev_t(buf, dev->devt); | |
360 | } | |
361 | ||
0863afb3 MW |
362 | /* |
363 | * devices_subsys - structure to be registered with kobject core. | |
1da177e4 LT |
364 | */ |
365 | ||
312c004d | 366 | decl_subsys(devices, &ktype_device, &device_uevent_ops); |
1da177e4 LT |
367 | |
368 | ||
369 | /** | |
370 | * device_create_file - create sysfs attribute file for device. | |
371 | * @dev: device. | |
372 | * @attr: device attribute descriptor. | |
373 | */ | |
374 | ||
375 | int device_create_file(struct device * dev, struct device_attribute * attr) | |
376 | { | |
377 | int error = 0; | |
378 | if (get_device(dev)) { | |
379 | error = sysfs_create_file(&dev->kobj, &attr->attr); | |
380 | put_device(dev); | |
381 | } | |
382 | return error; | |
383 | } | |
384 | ||
385 | /** | |
386 | * device_remove_file - remove sysfs attribute file. | |
387 | * @dev: device. | |
388 | * @attr: device attribute descriptor. | |
389 | */ | |
390 | ||
391 | void device_remove_file(struct device * dev, struct device_attribute * attr) | |
392 | { | |
393 | if (get_device(dev)) { | |
394 | sysfs_remove_file(&dev->kobj, &attr->attr); | |
395 | put_device(dev); | |
396 | } | |
397 | } | |
398 | ||
2589f188 GKH |
399 | /** |
400 | * device_create_bin_file - create sysfs binary attribute file for device. | |
401 | * @dev: device. | |
402 | * @attr: device binary attribute descriptor. | |
403 | */ | |
404 | int device_create_bin_file(struct device *dev, struct bin_attribute *attr) | |
405 | { | |
406 | int error = -EINVAL; | |
407 | if (dev) | |
408 | error = sysfs_create_bin_file(&dev->kobj, attr); | |
409 | return error; | |
410 | } | |
411 | EXPORT_SYMBOL_GPL(device_create_bin_file); | |
412 | ||
413 | /** | |
414 | * device_remove_bin_file - remove sysfs binary attribute file | |
415 | * @dev: device. | |
416 | * @attr: device binary attribute descriptor. | |
417 | */ | |
418 | void device_remove_bin_file(struct device *dev, struct bin_attribute *attr) | |
419 | { | |
420 | if (dev) | |
421 | sysfs_remove_bin_file(&dev->kobj, attr); | |
422 | } | |
423 | EXPORT_SYMBOL_GPL(device_remove_bin_file); | |
424 | ||
d9a9cdfb AS |
425 | /** |
426 | * device_schedule_callback - helper to schedule a callback for a device | |
427 | * @dev: device. | |
428 | * @func: callback function to invoke later. | |
429 | * | |
430 | * Attribute methods must not unregister themselves or their parent device | |
431 | * (which would amount to the same thing). Attempts to do so will deadlock, | |
432 | * since unregistration is mutually exclusive with driver callbacks. | |
433 | * | |
434 | * Instead methods can call this routine, which will attempt to allocate | |
435 | * and schedule a workqueue request to call back @func with @dev as its | |
436 | * argument in the workqueue's process context. @dev will be pinned until | |
437 | * @func returns. | |
438 | * | |
439 | * Returns 0 if the request was submitted, -ENOMEM if storage could not | |
440 | * be allocated. | |
441 | * | |
442 | * NOTE: This routine won't work if CONFIG_SYSFS isn't set! It uses an | |
443 | * underlying sysfs routine (since it is intended for use by attribute | |
444 | * methods), and if sysfs isn't available you'll get nothing but -ENOSYS. | |
445 | */ | |
446 | int device_schedule_callback(struct device *dev, | |
447 | void (*func)(struct device *)) | |
448 | { | |
449 | return sysfs_schedule_callback(&dev->kobj, | |
450 | (void (*)(void *)) func, dev); | |
451 | } | |
452 | EXPORT_SYMBOL_GPL(device_schedule_callback); | |
453 | ||
34bb61f9 JB |
454 | static void klist_children_get(struct klist_node *n) |
455 | { | |
456 | struct device *dev = container_of(n, struct device, knode_parent); | |
457 | ||
458 | get_device(dev); | |
459 | } | |
460 | ||
461 | static void klist_children_put(struct klist_node *n) | |
462 | { | |
463 | struct device *dev = container_of(n, struct device, knode_parent); | |
464 | ||
465 | put_device(dev); | |
466 | } | |
467 | ||
1da177e4 LT |
468 | |
469 | /** | |
470 | * device_initialize - init device structure. | |
471 | * @dev: device. | |
472 | * | |
473 | * This prepares the device for use by other layers, | |
474 | * including adding it to the device hierarchy. | |
475 | * It is the first half of device_register(), if called by | |
476 | * that, though it can also be called separately, so one | |
477 | * may use @dev's fields (e.g. the refcount). | |
478 | */ | |
479 | ||
480 | void device_initialize(struct device *dev) | |
481 | { | |
482 | kobj_set_kset_s(dev, devices_subsys); | |
483 | kobject_init(&dev->kobj); | |
34bb61f9 JB |
484 | klist_init(&dev->klist_children, klist_children_get, |
485 | klist_children_put); | |
1da177e4 | 486 | INIT_LIST_HEAD(&dev->dma_pools); |
23681e47 | 487 | INIT_LIST_HEAD(&dev->node); |
af70316a | 488 | init_MUTEX(&dev->sem); |
9ac7849e TH |
489 | spin_lock_init(&dev->devres_lock); |
490 | INIT_LIST_HEAD(&dev->devres_head); | |
0ac85241 | 491 | device_init_wakeup(dev, 0); |
87348136 | 492 | set_dev_node(dev, -1); |
1da177e4 LT |
493 | } |
494 | ||
40fa5422 | 495 | #ifdef CONFIG_SYSFS_DEPRECATED |
c744aeae CH |
496 | static struct kobject * get_device_parent(struct device *dev, |
497 | struct device *parent) | |
40fa5422 GKH |
498 | { |
499 | /* Set the parent to the class, not the parent device */ | |
500 | /* this keeps sysfs from having a symlink to make old udevs happy */ | |
501 | if (dev->class) | |
c744aeae | 502 | return &dev->class->subsys.kset.kobj; |
40fa5422 | 503 | else if (parent) |
c744aeae | 504 | return &parent->kobj; |
40fa5422 | 505 | |
c744aeae | 506 | return NULL; |
40fa5422 GKH |
507 | } |
508 | #else | |
86406245 | 509 | static struct kobject *virtual_device_parent(struct device *dev) |
f0ee61a6 | 510 | { |
86406245 | 511 | static struct kobject *virtual_dir = NULL; |
f0ee61a6 | 512 | |
86406245 KS |
513 | if (!virtual_dir) |
514 | virtual_dir = kobject_add_dir(&devices_subsys.kset.kobj, "virtual"); | |
f0ee61a6 | 515 | |
86406245 | 516 | return virtual_dir; |
f0ee61a6 GKH |
517 | } |
518 | ||
c744aeae CH |
519 | static struct kobject * get_device_parent(struct device *dev, |
520 | struct device *parent) | |
40fa5422 | 521 | { |
86406245 KS |
522 | if (dev->class) { |
523 | struct kobject *kobj = NULL; | |
524 | struct kobject *parent_kobj; | |
525 | struct kobject *k; | |
526 | ||
527 | /* | |
528 | * If we have no parent, we live in "virtual". | |
529 | * Class-devices with a bus-device as parent, live | |
530 | * in a class-directory to prevent namespace collisions. | |
531 | */ | |
532 | if (parent == NULL) | |
533 | parent_kobj = virtual_device_parent(dev); | |
534 | else if (parent->class) | |
535 | return &parent->kobj; | |
536 | else | |
537 | parent_kobj = &parent->kobj; | |
538 | ||
539 | /* find our class-directory at the parent and reference it */ | |
540 | spin_lock(&dev->class->class_dirs.list_lock); | |
541 | list_for_each_entry(k, &dev->class->class_dirs.list, entry) | |
542 | if (k->parent == parent_kobj) { | |
543 | kobj = kobject_get(k); | |
544 | break; | |
545 | } | |
546 | spin_unlock(&dev->class->class_dirs.list_lock); | |
547 | if (kobj) | |
548 | return kobj; | |
549 | ||
550 | /* or create a new class-directory at the parent device */ | |
551 | return kobject_kset_add_dir(&dev->class->class_dirs, | |
552 | parent_kobj, dev->class->name); | |
553 | } | |
554 | ||
555 | if (parent) | |
c744aeae CH |
556 | return &parent->kobj; |
557 | return NULL; | |
558 | } | |
c744aeae | 559 | #endif |
86406245 | 560 | |
c744aeae CH |
561 | static int setup_parent(struct device *dev, struct device *parent) |
562 | { | |
563 | struct kobject *kobj; | |
564 | kobj = get_device_parent(dev, parent); | |
565 | if (IS_ERR(kobj)) | |
566 | return PTR_ERR(kobj); | |
567 | if (kobj) | |
568 | dev->kobj.parent = kobj; | |
40fa5422 GKH |
569 | return 0; |
570 | } | |
40fa5422 | 571 | |
1da177e4 LT |
572 | /** |
573 | * device_add - add device to device hierarchy. | |
574 | * @dev: device. | |
575 | * | |
576 | * This is part 2 of device_register(), though may be called | |
577 | * separately _iff_ device_initialize() has been called separately. | |
578 | * | |
579 | * This adds it to the kobject hierarchy via kobject_add(), adds it | |
580 | * to the global and sibling lists for the device, then | |
581 | * adds it to the other relevant subsystems of the driver model. | |
582 | */ | |
583 | int device_add(struct device *dev) | |
584 | { | |
585 | struct device *parent = NULL; | |
e9a7d305 | 586 | char *class_name = NULL; |
c47ed219 | 587 | struct class_interface *class_intf; |
1da177e4 LT |
588 | int error = -EINVAL; |
589 | ||
590 | dev = get_device(dev); | |
591 | if (!dev || !strlen(dev->bus_id)) | |
592 | goto Error; | |
593 | ||
40fa5422 | 594 | pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id); |
c205ef48 | 595 | |
1da177e4 | 596 | parent = get_device(dev->parent); |
40fa5422 GKH |
597 | error = setup_parent(dev, parent); |
598 | if (error) | |
599 | goto Error; | |
1da177e4 LT |
600 | |
601 | /* first, register with generic layer. */ | |
602 | kobject_set_name(&dev->kobj, "%s", dev->bus_id); | |
40fa5422 GKH |
603 | error = kobject_add(&dev->kobj); |
604 | if (error) | |
1da177e4 | 605 | goto Error; |
a7fd6706 | 606 | |
37022644 BW |
607 | /* notify platform of device entry */ |
608 | if (platform_notify) | |
609 | platform_notify(dev); | |
610 | ||
116af378 BH |
611 | /* notify clients of device entry (new way) */ |
612 | if (dev->bus) | |
613 | blocking_notifier_call_chain(&dev->bus->bus_notifier, | |
614 | BUS_NOTIFY_ADD_DEVICE, dev); | |
615 | ||
a7fd6706 KS |
616 | dev->uevent_attr.attr.name = "uevent"; |
617 | dev->uevent_attr.attr.mode = S_IWUSR; | |
618 | if (dev->driver) | |
619 | dev->uevent_attr.attr.owner = dev->driver->owner; | |
620 | dev->uevent_attr.store = store_uevent; | |
a306eea4 CH |
621 | error = device_create_file(dev, &dev->uevent_attr); |
622 | if (error) | |
623 | goto attrError; | |
a7fd6706 | 624 | |
23681e47 GKH |
625 | if (MAJOR(dev->devt)) { |
626 | struct device_attribute *attr; | |
627 | attr = kzalloc(sizeof(*attr), GFP_KERNEL); | |
628 | if (!attr) { | |
629 | error = -ENOMEM; | |
a306eea4 | 630 | goto ueventattrError; |
23681e47 GKH |
631 | } |
632 | attr->attr.name = "dev"; | |
633 | attr->attr.mode = S_IRUGO; | |
634 | if (dev->driver) | |
635 | attr->attr.owner = dev->driver->owner; | |
636 | attr->show = show_dev; | |
637 | error = device_create_file(dev, attr); | |
638 | if (error) { | |
639 | kfree(attr); | |
a306eea4 | 640 | goto ueventattrError; |
23681e47 GKH |
641 | } |
642 | ||
643 | dev->devt_attr = attr; | |
644 | } | |
645 | ||
b9d9c82b KS |
646 | if (dev->class) { |
647 | sysfs_create_link(&dev->kobj, &dev->class->subsys.kset.kobj, | |
648 | "subsystem"); | |
40fa5422 GKH |
649 | /* If this is not a "fake" compatible device, then create the |
650 | * symlink from the class to the device. */ | |
651 | if (dev->kobj.parent != &dev->class->subsys.kset.kobj) | |
652 | sysfs_create_link(&dev->class->subsys.kset.kobj, | |
653 | &dev->kobj, dev->bus_id); | |
64bb5d2c | 654 | if (parent) { |
cb360bbf CH |
655 | sysfs_create_link(&dev->kobj, &dev->parent->kobj, |
656 | "device"); | |
f7f3461d | 657 | #ifdef CONFIG_SYSFS_DEPRECATED |
cb360bbf CH |
658 | class_name = make_class_name(dev->class->name, |
659 | &dev->kobj); | |
660 | if (class_name) | |
661 | sysfs_create_link(&dev->parent->kobj, | |
662 | &dev->kobj, class_name); | |
99ef3ef8 | 663 | #endif |
f7f3461d | 664 | } |
b9d9c82b | 665 | } |
23681e47 | 666 | |
2620efef GKH |
667 | if ((error = device_add_attrs(dev))) |
668 | goto AttrsError; | |
1da177e4 LT |
669 | if ((error = device_pm_add(dev))) |
670 | goto PMError; | |
671 | if ((error = bus_add_device(dev))) | |
672 | goto BusError; | |
b7a3e813 KS |
673 | if (!dev->uevent_suppress) |
674 | kobject_uevent(&dev->kobj, KOBJ_ADD); | |
f70fa629 AS |
675 | if ((error = bus_attach_device(dev))) |
676 | goto AttachError; | |
1da177e4 | 677 | if (parent) |
d856f1e3 | 678 | klist_add_tail(&dev->knode_parent, &parent->klist_children); |
1da177e4 | 679 | |
5d9fd169 | 680 | if (dev->class) { |
5d9fd169 | 681 | down(&dev->class->sem); |
c47ed219 | 682 | /* tie the class to the device */ |
5d9fd169 | 683 | list_add_tail(&dev->node, &dev->class->devices); |
c47ed219 GKH |
684 | |
685 | /* notify any interfaces that the device is here */ | |
686 | list_for_each_entry(class_intf, &dev->class->interfaces, node) | |
687 | if (class_intf->add_dev) | |
688 | class_intf->add_dev(dev, class_intf); | |
5d9fd169 GKH |
689 | up(&dev->class->sem); |
690 | } | |
1da177e4 | 691 | Done: |
621a1672 | 692 | kfree(class_name); |
1da177e4 LT |
693 | put_device(dev); |
694 | return error; | |
f70fa629 AS |
695 | AttachError: |
696 | bus_remove_device(dev); | |
1da177e4 LT |
697 | BusError: |
698 | device_pm_remove(dev); | |
699 | PMError: | |
116af378 BH |
700 | if (dev->bus) |
701 | blocking_notifier_call_chain(&dev->bus->bus_notifier, | |
702 | BUS_NOTIFY_DEL_DEVICE, dev); | |
82f0cf9b | 703 | device_remove_attrs(dev); |
2620efef | 704 | AttrsError: |
23681e47 GKH |
705 | if (dev->devt_attr) { |
706 | device_remove_file(dev, dev->devt_attr); | |
707 | kfree(dev->devt_attr); | |
708 | } | |
82f0cf9b JS |
709 | |
710 | if (dev->class) { | |
711 | sysfs_remove_link(&dev->kobj, "subsystem"); | |
712 | /* If this is not a "fake" compatible device, remove the | |
713 | * symlink from the class to the device. */ | |
714 | if (dev->kobj.parent != &dev->class->subsys.kset.kobj) | |
715 | sysfs_remove_link(&dev->class->subsys.kset.kobj, | |
716 | dev->bus_id); | |
82f0cf9b | 717 | if (parent) { |
f7f3461d | 718 | #ifdef CONFIG_SYSFS_DEPRECATED |
82f0cf9b JS |
719 | char *class_name = make_class_name(dev->class->name, |
720 | &dev->kobj); | |
721 | if (class_name) | |
722 | sysfs_remove_link(&dev->parent->kobj, | |
723 | class_name); | |
724 | kfree(class_name); | |
f7f3461d | 725 | #endif |
82f0cf9b JS |
726 | sysfs_remove_link(&dev->kobj, "device"); |
727 | } | |
82f0cf9b | 728 | } |
a306eea4 CH |
729 | ueventattrError: |
730 | device_remove_file(dev, &dev->uevent_attr); | |
23681e47 | 731 | attrError: |
312c004d | 732 | kobject_uevent(&dev->kobj, KOBJ_REMOVE); |
1da177e4 LT |
733 | kobject_del(&dev->kobj); |
734 | Error: | |
735 | if (parent) | |
736 | put_device(parent); | |
737 | goto Done; | |
738 | } | |
739 | ||
740 | ||
741 | /** | |
742 | * device_register - register a device with the system. | |
743 | * @dev: pointer to the device structure | |
744 | * | |
745 | * This happens in two clean steps - initialize the device | |
746 | * and add it to the system. The two steps can be called | |
747 | * separately, but this is the easiest and most common. | |
748 | * I.e. you should only call the two helpers separately if | |
749 | * have a clearly defined need to use and refcount the device | |
750 | * before it is added to the hierarchy. | |
751 | */ | |
752 | ||
753 | int device_register(struct device *dev) | |
754 | { | |
755 | device_initialize(dev); | |
756 | return device_add(dev); | |
757 | } | |
758 | ||
759 | ||
760 | /** | |
761 | * get_device - increment reference count for device. | |
762 | * @dev: device. | |
763 | * | |
764 | * This simply forwards the call to kobject_get(), though | |
765 | * we do take care to provide for the case that we get a NULL | |
766 | * pointer passed in. | |
767 | */ | |
768 | ||
769 | struct device * get_device(struct device * dev) | |
770 | { | |
771 | return dev ? to_dev(kobject_get(&dev->kobj)) : NULL; | |
772 | } | |
773 | ||
774 | ||
775 | /** | |
776 | * put_device - decrement reference count. | |
777 | * @dev: device in question. | |
778 | */ | |
779 | void put_device(struct device * dev) | |
780 | { | |
781 | if (dev) | |
782 | kobject_put(&dev->kobj); | |
783 | } | |
784 | ||
785 | ||
786 | /** | |
787 | * device_del - delete device from system. | |
788 | * @dev: device. | |
789 | * | |
790 | * This is the first part of the device unregistration | |
791 | * sequence. This removes the device from the lists we control | |
792 | * from here, has it removed from the other driver model | |
793 | * subsystems it was added to in device_add(), and removes it | |
794 | * from the kobject hierarchy. | |
795 | * | |
796 | * NOTE: this should be called manually _iff_ device_add() was | |
797 | * also called manually. | |
798 | */ | |
799 | ||
800 | void device_del(struct device * dev) | |
801 | { | |
802 | struct device * parent = dev->parent; | |
c47ed219 | 803 | struct class_interface *class_intf; |
1da177e4 | 804 | |
1da177e4 | 805 | if (parent) |
d62c0f9f | 806 | klist_del(&dev->knode_parent); |
82189b98 | 807 | if (dev->devt_attr) { |
23681e47 | 808 | device_remove_file(dev, dev->devt_attr); |
82189b98 CM |
809 | kfree(dev->devt_attr); |
810 | } | |
b9d9c82b KS |
811 | if (dev->class) { |
812 | sysfs_remove_link(&dev->kobj, "subsystem"); | |
40fa5422 GKH |
813 | /* If this is not a "fake" compatible device, remove the |
814 | * symlink from the class to the device. */ | |
815 | if (dev->kobj.parent != &dev->class->subsys.kset.kobj) | |
816 | sysfs_remove_link(&dev->class->subsys.kset.kobj, | |
817 | dev->bus_id); | |
64bb5d2c | 818 | if (parent) { |
f7f3461d | 819 | #ifdef CONFIG_SYSFS_DEPRECATED |
99ef3ef8 KS |
820 | char *class_name = make_class_name(dev->class->name, |
821 | &dev->kobj); | |
cb360bbf CH |
822 | if (class_name) |
823 | sysfs_remove_link(&dev->parent->kobj, | |
824 | class_name); | |
99ef3ef8 | 825 | kfree(class_name); |
f7f3461d | 826 | #endif |
99ef3ef8 | 827 | sysfs_remove_link(&dev->kobj, "device"); |
64bb5d2c | 828 | } |
99ef3ef8 | 829 | |
5d9fd169 | 830 | down(&dev->class->sem); |
c47ed219 GKH |
831 | /* notify any interfaces that the device is now gone */ |
832 | list_for_each_entry(class_intf, &dev->class->interfaces, node) | |
833 | if (class_intf->remove_dev) | |
834 | class_intf->remove_dev(dev, class_intf); | |
835 | /* remove the device from the class list */ | |
5d9fd169 GKH |
836 | list_del_init(&dev->node); |
837 | up(&dev->class->sem); | |
86406245 KS |
838 | |
839 | /* If we live in a parent class-directory, unreference it */ | |
840 | if (dev->kobj.parent->kset == &dev->class->class_dirs) { | |
841 | struct device *d; | |
842 | int other = 0; | |
843 | ||
844 | /* | |
845 | * if we are the last child of our class, delete | |
846 | * our class-directory at this parent | |
847 | */ | |
848 | down(&dev->class->sem); | |
849 | list_for_each_entry(d, &dev->class->devices, node) { | |
850 | if (d == dev) | |
851 | continue; | |
852 | if (d->kobj.parent == dev->kobj.parent) { | |
853 | other = 1; | |
854 | break; | |
855 | } | |
856 | } | |
857 | if (!other) | |
858 | kobject_del(dev->kobj.parent); | |
859 | ||
860 | kobject_put(dev->kobj.parent); | |
861 | up(&dev->class->sem); | |
862 | } | |
b9d9c82b | 863 | } |
a7fd6706 | 864 | device_remove_file(dev, &dev->uevent_attr); |
2620efef | 865 | device_remove_attrs(dev); |
28953533 | 866 | bus_remove_device(dev); |
1da177e4 | 867 | |
2f8d16a9 TH |
868 | /* |
869 | * Some platform devices are driven without driver attached | |
870 | * and managed resources may have been acquired. Make sure | |
871 | * all resources are released. | |
872 | */ | |
873 | devres_release_all(dev); | |
874 | ||
1da177e4 LT |
875 | /* Notify the platform of the removal, in case they |
876 | * need to do anything... | |
877 | */ | |
878 | if (platform_notify_remove) | |
879 | platform_notify_remove(dev); | |
116af378 BH |
880 | if (dev->bus) |
881 | blocking_notifier_call_chain(&dev->bus->bus_notifier, | |
882 | BUS_NOTIFY_DEL_DEVICE, dev); | |
1da177e4 | 883 | device_pm_remove(dev); |
312c004d | 884 | kobject_uevent(&dev->kobj, KOBJ_REMOVE); |
1da177e4 LT |
885 | kobject_del(&dev->kobj); |
886 | if (parent) | |
887 | put_device(parent); | |
888 | } | |
889 | ||
890 | /** | |
891 | * device_unregister - unregister device from system. | |
892 | * @dev: device going away. | |
893 | * | |
894 | * We do this in two parts, like we do device_register(). First, | |
895 | * we remove it from all the subsystems with device_del(), then | |
896 | * we decrement the reference count via put_device(). If that | |
897 | * is the final reference count, the device will be cleaned up | |
898 | * via device_release() above. Otherwise, the structure will | |
899 | * stick around until the final reference to the device is dropped. | |
900 | */ | |
901 | void device_unregister(struct device * dev) | |
902 | { | |
903 | pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id); | |
904 | device_del(dev); | |
905 | put_device(dev); | |
906 | } | |
907 | ||
908 | ||
36239577 PM |
909 | static struct device * next_device(struct klist_iter * i) |
910 | { | |
911 | struct klist_node * n = klist_next(i); | |
912 | return n ? container_of(n, struct device, knode_parent) : NULL; | |
913 | } | |
914 | ||
1da177e4 LT |
915 | /** |
916 | * device_for_each_child - device child iterator. | |
c41455fb | 917 | * @parent: parent struct device. |
1da177e4 LT |
918 | * @data: data for the callback. |
919 | * @fn: function to be called for each device. | |
920 | * | |
c41455fb | 921 | * Iterate over @parent's child devices, and call @fn for each, |
1da177e4 LT |
922 | * passing it @data. |
923 | * | |
924 | * We check the return of @fn each time. If it returns anything | |
925 | * other than 0, we break out and return that value. | |
926 | */ | |
36239577 | 927 | int device_for_each_child(struct device * parent, void * data, |
1da177e4 LT |
928 | int (*fn)(struct device *, void *)) |
929 | { | |
36239577 | 930 | struct klist_iter i; |
1da177e4 LT |
931 | struct device * child; |
932 | int error = 0; | |
933 | ||
36239577 PM |
934 | klist_iter_init(&parent->klist_children, &i); |
935 | while ((child = next_device(&i)) && !error) | |
936 | error = fn(child, data); | |
937 | klist_iter_exit(&i); | |
1da177e4 LT |
938 | return error; |
939 | } | |
940 | ||
5ab69981 CH |
941 | /** |
942 | * device_find_child - device iterator for locating a particular device. | |
943 | * @parent: parent struct device | |
944 | * @data: Data to pass to match function | |
945 | * @match: Callback function to check device | |
946 | * | |
947 | * This is similar to the device_for_each_child() function above, but it | |
948 | * returns a reference to a device that is 'found' for later use, as | |
949 | * determined by the @match callback. | |
950 | * | |
951 | * The callback should return 0 if the device doesn't match and non-zero | |
952 | * if it does. If the callback returns non-zero and a reference to the | |
953 | * current device can be obtained, this function will return to the caller | |
954 | * and not iterate over any more devices. | |
955 | */ | |
956 | struct device * device_find_child(struct device *parent, void *data, | |
957 | int (*match)(struct device *, void *)) | |
958 | { | |
959 | struct klist_iter i; | |
960 | struct device *child; | |
961 | ||
962 | if (!parent) | |
963 | return NULL; | |
964 | ||
965 | klist_iter_init(&parent->klist_children, &i); | |
966 | while ((child = next_device(&i))) | |
967 | if (match(child, data) && get_device(child)) | |
968 | break; | |
969 | klist_iter_exit(&i); | |
970 | return child; | |
971 | } | |
972 | ||
1da177e4 LT |
973 | int __init devices_init(void) |
974 | { | |
975 | return subsystem_register(&devices_subsys); | |
976 | } | |
977 | ||
978 | EXPORT_SYMBOL_GPL(device_for_each_child); | |
5ab69981 | 979 | EXPORT_SYMBOL_GPL(device_find_child); |
1da177e4 LT |
980 | |
981 | EXPORT_SYMBOL_GPL(device_initialize); | |
982 | EXPORT_SYMBOL_GPL(device_add); | |
983 | EXPORT_SYMBOL_GPL(device_register); | |
984 | ||
985 | EXPORT_SYMBOL_GPL(device_del); | |
986 | EXPORT_SYMBOL_GPL(device_unregister); | |
987 | EXPORT_SYMBOL_GPL(get_device); | |
988 | EXPORT_SYMBOL_GPL(put_device); | |
1da177e4 LT |
989 | |
990 | EXPORT_SYMBOL_GPL(device_create_file); | |
991 | EXPORT_SYMBOL_GPL(device_remove_file); | |
23681e47 GKH |
992 | |
993 | ||
994 | static void device_create_release(struct device *dev) | |
995 | { | |
996 | pr_debug("%s called for %s\n", __FUNCTION__, dev->bus_id); | |
997 | kfree(dev); | |
998 | } | |
999 | ||
1000 | /** | |
1001 | * device_create - creates a device and registers it with sysfs | |
42734daf HK |
1002 | * @class: pointer to the struct class that this device should be registered to |
1003 | * @parent: pointer to the parent struct device of this new device, if any | |
1004 | * @devt: the dev_t for the char device to be added | |
1005 | * @fmt: string for the device's name | |
1006 | * | |
1007 | * This function can be used by char device classes. A struct device | |
1008 | * will be created in sysfs, registered to the specified class. | |
23681e47 | 1009 | * |
23681e47 GKH |
1010 | * A "dev" file will be created, showing the dev_t for the device, if |
1011 | * the dev_t is not 0,0. | |
42734daf HK |
1012 | * If a pointer to a parent struct device is passed in, the newly created |
1013 | * struct device will be a child of that device in sysfs. | |
1014 | * The pointer to the struct device will be returned from the call. | |
1015 | * Any further sysfs files that might be required can be created using this | |
23681e47 GKH |
1016 | * pointer. |
1017 | * | |
1018 | * Note: the struct class passed to this function must have previously | |
1019 | * been created with a call to class_create(). | |
1020 | */ | |
1021 | struct device *device_create(struct class *class, struct device *parent, | |
5cbe5f8a | 1022 | dev_t devt, const char *fmt, ...) |
23681e47 GKH |
1023 | { |
1024 | va_list args; | |
1025 | struct device *dev = NULL; | |
1026 | int retval = -ENODEV; | |
1027 | ||
1028 | if (class == NULL || IS_ERR(class)) | |
1029 | goto error; | |
23681e47 GKH |
1030 | |
1031 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); | |
1032 | if (!dev) { | |
1033 | retval = -ENOMEM; | |
1034 | goto error; | |
1035 | } | |
1036 | ||
1037 | dev->devt = devt; | |
1038 | dev->class = class; | |
1039 | dev->parent = parent; | |
1040 | dev->release = device_create_release; | |
1041 | ||
1042 | va_start(args, fmt); | |
1043 | vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args); | |
1044 | va_end(args); | |
1045 | retval = device_register(dev); | |
1046 | if (retval) | |
1047 | goto error; | |
1048 | ||
23681e47 GKH |
1049 | return dev; |
1050 | ||
1051 | error: | |
1052 | kfree(dev); | |
1053 | return ERR_PTR(retval); | |
1054 | } | |
1055 | EXPORT_SYMBOL_GPL(device_create); | |
1056 | ||
1057 | /** | |
1058 | * device_destroy - removes a device that was created with device_create() | |
42734daf HK |
1059 | * @class: pointer to the struct class that this device was registered with |
1060 | * @devt: the dev_t of the device that was previously registered | |
23681e47 | 1061 | * |
42734daf HK |
1062 | * This call unregisters and cleans up a device that was created with a |
1063 | * call to device_create(). | |
23681e47 GKH |
1064 | */ |
1065 | void device_destroy(struct class *class, dev_t devt) | |
1066 | { | |
1067 | struct device *dev = NULL; | |
1068 | struct device *dev_tmp; | |
1069 | ||
1070 | down(&class->sem); | |
1071 | list_for_each_entry(dev_tmp, &class->devices, node) { | |
1072 | if (dev_tmp->devt == devt) { | |
1073 | dev = dev_tmp; | |
1074 | break; | |
1075 | } | |
1076 | } | |
1077 | up(&class->sem); | |
1078 | ||
5d9fd169 | 1079 | if (dev) |
23681e47 | 1080 | device_unregister(dev); |
23681e47 GKH |
1081 | } |
1082 | EXPORT_SYMBOL_GPL(device_destroy); | |
a2de48ca GKH |
1083 | |
1084 | /** | |
1085 | * device_rename - renames a device | |
1086 | * @dev: the pointer to the struct device to be renamed | |
1087 | * @new_name: the new name of the device | |
1088 | */ | |
1089 | int device_rename(struct device *dev, char *new_name) | |
1090 | { | |
1091 | char *old_class_name = NULL; | |
1092 | char *new_class_name = NULL; | |
1093 | char *old_symlink_name = NULL; | |
1094 | int error; | |
1095 | ||
1096 | dev = get_device(dev); | |
1097 | if (!dev) | |
1098 | return -EINVAL; | |
1099 | ||
1100 | pr_debug("DEVICE: renaming '%s' to '%s'\n", dev->bus_id, new_name); | |
1101 | ||
99ef3ef8 | 1102 | #ifdef CONFIG_SYSFS_DEPRECATED |
a2de48ca GKH |
1103 | if ((dev->class) && (dev->parent)) |
1104 | old_class_name = make_class_name(dev->class->name, &dev->kobj); | |
99ef3ef8 | 1105 | #endif |
a2de48ca GKH |
1106 | |
1107 | if (dev->class) { | |
1108 | old_symlink_name = kmalloc(BUS_ID_SIZE, GFP_KERNEL); | |
952ab431 JJ |
1109 | if (!old_symlink_name) { |
1110 | error = -ENOMEM; | |
1111 | goto out_free_old_class; | |
1112 | } | |
a2de48ca GKH |
1113 | strlcpy(old_symlink_name, dev->bus_id, BUS_ID_SIZE); |
1114 | } | |
1115 | ||
1116 | strlcpy(dev->bus_id, new_name, BUS_ID_SIZE); | |
1117 | ||
1118 | error = kobject_rename(&dev->kobj, new_name); | |
1119 | ||
99ef3ef8 | 1120 | #ifdef CONFIG_SYSFS_DEPRECATED |
a2de48ca GKH |
1121 | if (old_class_name) { |
1122 | new_class_name = make_class_name(dev->class->name, &dev->kobj); | |
1123 | if (new_class_name) { | |
1124 | sysfs_create_link(&dev->parent->kobj, &dev->kobj, | |
1125 | new_class_name); | |
1126 | sysfs_remove_link(&dev->parent->kobj, old_class_name); | |
1127 | } | |
1128 | } | |
99ef3ef8 KS |
1129 | #endif |
1130 | ||
a2de48ca GKH |
1131 | if (dev->class) { |
1132 | sysfs_remove_link(&dev->class->subsys.kset.kobj, | |
1133 | old_symlink_name); | |
1134 | sysfs_create_link(&dev->class->subsys.kset.kobj, &dev->kobj, | |
1135 | dev->bus_id); | |
1136 | } | |
1137 | put_device(dev); | |
1138 | ||
a2de48ca GKH |
1139 | kfree(new_class_name); |
1140 | kfree(old_symlink_name); | |
952ab431 JJ |
1141 | out_free_old_class: |
1142 | kfree(old_class_name); | |
a2de48ca GKH |
1143 | |
1144 | return error; | |
1145 | } | |
a2807dbc | 1146 | EXPORT_SYMBOL_GPL(device_rename); |
8a82472f CH |
1147 | |
1148 | static int device_move_class_links(struct device *dev, | |
1149 | struct device *old_parent, | |
1150 | struct device *new_parent) | |
1151 | { | |
f7f3461d | 1152 | int error = 0; |
8a82472f | 1153 | #ifdef CONFIG_SYSFS_DEPRECATED |
8a82472f CH |
1154 | char *class_name; |
1155 | ||
1156 | class_name = make_class_name(dev->class->name, &dev->kobj); | |
1157 | if (!class_name) { | |
cb360bbf | 1158 | error = -ENOMEM; |
8a82472f CH |
1159 | goto out; |
1160 | } | |
1161 | if (old_parent) { | |
1162 | sysfs_remove_link(&dev->kobj, "device"); | |
1163 | sysfs_remove_link(&old_parent->kobj, class_name); | |
1164 | } | |
c744aeae CH |
1165 | if (new_parent) { |
1166 | error = sysfs_create_link(&dev->kobj, &new_parent->kobj, | |
1167 | "device"); | |
1168 | if (error) | |
1169 | goto out; | |
1170 | error = sysfs_create_link(&new_parent->kobj, &dev->kobj, | |
1171 | class_name); | |
1172 | if (error) | |
1173 | sysfs_remove_link(&dev->kobj, "device"); | |
1174 | } | |
1175 | else | |
1176 | error = 0; | |
8a82472f CH |
1177 | out: |
1178 | kfree(class_name); | |
1179 | return error; | |
1180 | #else | |
f7f3461d GKH |
1181 | if (old_parent) |
1182 | sysfs_remove_link(&dev->kobj, "device"); | |
1183 | if (new_parent) | |
1184 | error = sysfs_create_link(&dev->kobj, &new_parent->kobj, | |
1185 | "device"); | |
1186 | return error; | |
8a82472f CH |
1187 | #endif |
1188 | } | |
1189 | ||
1190 | /** | |
1191 | * device_move - moves a device to a new parent | |
1192 | * @dev: the pointer to the struct device to be moved | |
c744aeae | 1193 | * @new_parent: the new parent of the device (can by NULL) |
8a82472f CH |
1194 | */ |
1195 | int device_move(struct device *dev, struct device *new_parent) | |
1196 | { | |
1197 | int error; | |
1198 | struct device *old_parent; | |
c744aeae | 1199 | struct kobject *new_parent_kobj; |
8a82472f CH |
1200 | |
1201 | dev = get_device(dev); | |
1202 | if (!dev) | |
1203 | return -EINVAL; | |
1204 | ||
8a82472f | 1205 | new_parent = get_device(new_parent); |
c744aeae CH |
1206 | new_parent_kobj = get_device_parent (dev, new_parent); |
1207 | if (IS_ERR(new_parent_kobj)) { | |
1208 | error = PTR_ERR(new_parent_kobj); | |
1209 | put_device(new_parent); | |
8a82472f CH |
1210 | goto out; |
1211 | } | |
1212 | pr_debug("DEVICE: moving '%s' to '%s'\n", dev->bus_id, | |
c744aeae CH |
1213 | new_parent ? new_parent->bus_id : "<NULL>"); |
1214 | error = kobject_move(&dev->kobj, new_parent_kobj); | |
8a82472f CH |
1215 | if (error) { |
1216 | put_device(new_parent); | |
1217 | goto out; | |
1218 | } | |
1219 | old_parent = dev->parent; | |
1220 | dev->parent = new_parent; | |
1221 | if (old_parent) | |
acf02d23 | 1222 | klist_remove(&dev->knode_parent); |
c744aeae CH |
1223 | if (new_parent) |
1224 | klist_add_tail(&dev->knode_parent, &new_parent->klist_children); | |
8a82472f CH |
1225 | if (!dev->class) |
1226 | goto out_put; | |
1227 | error = device_move_class_links(dev, old_parent, new_parent); | |
1228 | if (error) { | |
1229 | /* We ignore errors on cleanup since we're hosed anyway... */ | |
1230 | device_move_class_links(dev, new_parent, old_parent); | |
1231 | if (!kobject_move(&dev->kobj, &old_parent->kobj)) { | |
c744aeae CH |
1232 | if (new_parent) |
1233 | klist_remove(&dev->knode_parent); | |
8a82472f CH |
1234 | if (old_parent) |
1235 | klist_add_tail(&dev->knode_parent, | |
1236 | &old_parent->klist_children); | |
1237 | } | |
1238 | put_device(new_parent); | |
1239 | goto out; | |
1240 | } | |
1241 | out_put: | |
1242 | put_device(old_parent); | |
1243 | out: | |
1244 | put_device(dev); | |
1245 | return error; | |
1246 | } | |
1247 | ||
1248 | EXPORT_SYMBOL_GPL(device_move); |