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