blob: 7a5336f7df897605f576f6bcd28f1e0f153fff7b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Greg Kroah-Hartman64bb5d22006-06-28 16:19:58 -07006 * Copyright (c) 2006 Greg Kroah-Hartman <gregkh@suse.de>
7 * Copyright (c) 2006 Novell, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * This file is released under the GPLv2
10 *
11 */
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#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>
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -070019#include <linux/kdev_t.h>
Benjamin Herrenschmidt116af372006-10-25 13:44:59 +100020#include <linux/notifier.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22#include <asm/semaphore.h>
23
24#include "base.h"
25#include "power/power.h"
26
27int (*platform_notify)(struct device * dev) = NULL;
28int (*platform_notify_remove)(struct device * dev) = NULL;
29
30/*
31 * sysfs bindings for devices.
32 */
33
Alan Stern3e956372006-06-16 17:10:48 -040034/**
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 */
43const char *dev_driver_string(struct device *dev)
44{
45 return dev->driver ? dev->driver->name :
46 (dev->bus ? dev->bus->name : "");
47}
Matthew Wilcox310a9222006-09-23 23:35:04 -060048EXPORT_SYMBOL(dev_driver_string);
Alan Stern3e956372006-06-16 17:10:48 -040049
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#define to_dev(obj) container_of(obj, struct device, kobj)
51#define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053static ssize_t
54dev_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);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -050058 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60 if (dev_attr->show)
Yani Ioannou54b6f352005-05-17 06:39:34 -040061 ret = dev_attr->show(dev, dev_attr, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 return ret;
63}
64
65static ssize_t
66dev_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);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -050071 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73 if (dev_attr->store)
Yani Ioannou54b6f352005-05-17 06:39:34 -040074 ret = dev_attr->store(dev, dev_attr, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 return ret;
76}
77
78static 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 */
92static void device_release(struct kobject * kobj)
93{
94 struct device * dev = to_dev(kobj);
95
96 if (dev->release)
97 dev->release(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -070098 else if (dev->class && dev->class->dev_release)
99 dev->class->dev_release(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 else {
101 printk(KERN_ERR "Device '%s' does not have a release() function, "
102 "it is broken and must be fixed.\n",
103 dev->bus_id);
104 WARN_ON(1);
105 }
106}
107
108static struct kobj_type ktype_device = {
109 .release = device_release,
110 .sysfs_ops = &dev_sysfs_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111};
112
113
Kay Sievers312c0042005-11-16 09:00:00 +0100114static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
116 struct kobj_type *ktype = get_ktype(kobj);
117
118 if (ktype == &ktype_device) {
119 struct device *dev = to_dev(kobj);
120 if (dev->bus)
121 return 1;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700122 if (dev->class)
123 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 }
125 return 0;
126}
127
Kay Sievers312c0042005-11-16 09:00:00 +0100128static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
130 struct device *dev = to_dev(kobj);
131
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700132 if (dev->bus)
133 return dev->bus->name;
134 if (dev->class)
135 return dev->class->name;
136 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
Kay Sievers312c0042005-11-16 09:00:00 +0100139static int dev_uevent(struct kset *kset, struct kobject *kobj, char **envp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 int num_envp, char *buffer, int buffer_size)
141{
142 struct device *dev = to_dev(kobj);
143 int i = 0;
144 int length = 0;
145 int retval = 0;
146
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700147 /* add the major/minor if present */
148 if (MAJOR(dev->devt)) {
149 add_uevent_var(envp, num_envp, &i,
150 buffer, buffer_size, &length,
151 "MAJOR=%u", MAJOR(dev->devt));
152 add_uevent_var(envp, num_envp, &i,
153 buffer, buffer_size, &length,
154 "MINOR=%u", MINOR(dev->devt));
155 }
156
Kay Sievers239378f2006-10-07 21:54:55 +0200157 if (dev->driver)
Kay Sieversd81d9d62006-08-13 06:17:09 +0200158 add_uevent_var(envp, num_envp, &i,
159 buffer, buffer_size, &length,
160 "DRIVER=%s", dev->driver->name);
Kay Sievers239378f2006-10-07 21:54:55 +0200161
Kay Sieversa87cb2a2006-09-14 11:23:28 +0200162#ifdef CONFIG_SYSFS_DEPRECATED
Kay Sievers239378f2006-10-07 21:54:55 +0200163 if (dev->class) {
164 struct device *parent = dev->parent;
165
166 /* find first bus device in parent chain */
167 while (parent && !parent->bus)
168 parent = parent->parent;
169 if (parent && parent->bus) {
170 const char *path;
171
172 path = kobject_get_path(&parent->kobj, GFP_KERNEL);
173 add_uevent_var(envp, num_envp, &i,
174 buffer, buffer_size, &length,
175 "PHYSDEVPATH=%s", path);
176 kfree(path);
177
178 add_uevent_var(envp, num_envp, &i,
179 buffer, buffer_size, &length,
180 "PHYSDEVBUS=%s", parent->bus->name);
181
182 if (parent->driver)
183 add_uevent_var(envp, num_envp, &i,
184 buffer, buffer_size, &length,
185 "PHYSDEVDRIVER=%s", parent->driver->name);
186 }
187 } else if (dev->bus) {
Kay Sievers312c0042005-11-16 09:00:00 +0100188 add_uevent_var(envp, num_envp, &i,
189 buffer, buffer_size, &length,
Kay Sievers239378f2006-10-07 21:54:55 +0200190 "PHYSDEVBUS=%s", dev->bus->name);
191
192 if (dev->driver)
193 add_uevent_var(envp, num_envp, &i,
194 buffer, buffer_size, &length,
195 "PHYSDEVDRIVER=%s", dev->driver->name);
Kay Sieversd81d9d62006-08-13 06:17:09 +0200196 }
Kay Sievers239378f2006-10-07 21:54:55 +0200197#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 /* terminate, set to next free slot, shrink available space */
200 envp[i] = NULL;
201 envp = &envp[i];
202 num_envp -= i;
203 buffer = &buffer[length];
204 buffer_size -= length;
205
Kay Sievers312c0042005-11-16 09:00:00 +0100206 if (dev->bus && dev->bus->uevent) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 /* have the bus specific function add its stuff */
Kay Sievers312c0042005-11-16 09:00:00 +0100208 retval = dev->bus->uevent(dev, envp, num_envp, buffer, buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 if (retval) {
Kay Sievers312c0042005-11-16 09:00:00 +0100210 pr_debug ("%s - uevent() returned %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 __FUNCTION__, retval);
212 }
213 }
214
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700215 if (dev->class && dev->class->dev_uevent) {
216 /* have the class specific function add its stuff */
217 retval = dev->class->dev_uevent(dev, envp, num_envp, buffer, buffer_size);
218 if (retval) {
219 pr_debug("%s - dev_uevent() returned %d\n",
220 __FUNCTION__, retval);
221 }
222 }
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 return retval;
225}
226
Kay Sievers312c0042005-11-16 09:00:00 +0100227static struct kset_uevent_ops device_uevent_ops = {
228 .filter = dev_uevent_filter,
229 .name = dev_uevent_name,
230 .uevent = dev_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231};
232
Kay Sieversa7fd6702005-10-01 14:49:43 +0200233static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
234 const char *buf, size_t count)
235{
Kay Sievers312c0042005-11-16 09:00:00 +0100236 kobject_uevent(&dev->kobj, KOBJ_ADD);
Kay Sieversa7fd6702005-10-01 14:49:43 +0200237 return count;
238}
239
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700240static int device_add_groups(struct device *dev)
241{
242 int i;
243 int error = 0;
244
245 if (dev->groups) {
246 for (i = 0; dev->groups[i]; i++) {
247 error = sysfs_create_group(&dev->kobj, dev->groups[i]);
248 if (error) {
249 while (--i >= 0)
250 sysfs_remove_group(&dev->kobj, dev->groups[i]);
251 goto out;
252 }
253 }
254 }
255out:
256 return error;
257}
258
259static void device_remove_groups(struct device *dev)
260{
261 int i;
262 if (dev->groups) {
263 for (i = 0; dev->groups[i]; i++) {
264 sysfs_remove_group(&dev->kobj, dev->groups[i]);
265 }
266 }
267}
268
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700269static int device_add_attrs(struct device *dev)
270{
271 struct class *class = dev->class;
272 int error = 0;
273 int i;
274
275 if (!class)
276 return 0;
277
278 if (class->dev_attrs) {
279 for (i = 0; attr_name(class->dev_attrs[i]); i++) {
280 error = device_create_file(dev, &class->dev_attrs[i]);
281 if (error)
282 break;
283 }
284 }
285 if (error)
286 while (--i >= 0)
287 device_remove_file(dev, &class->dev_attrs[i]);
288 return error;
289}
290
291static void device_remove_attrs(struct device *dev)
292{
293 struct class *class = dev->class;
294 int i;
295
296 if (!class)
297 return;
298
299 if (class->dev_attrs) {
300 for (i = 0; attr_name(class->dev_attrs[i]); i++)
301 device_remove_file(dev, &class->dev_attrs[i]);
302 }
303}
304
305
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700306static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
307 char *buf)
308{
309 return print_dev_t(buf, dev->devt);
310}
311
Martin Waitz0863afb2006-01-09 20:53:55 -0800312/*
313 * devices_subsys - structure to be registered with kobject core.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 */
315
Kay Sievers312c0042005-11-16 09:00:00 +0100316decl_subsys(devices, &ktype_device, &device_uevent_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
318
319/**
320 * device_create_file - create sysfs attribute file for device.
321 * @dev: device.
322 * @attr: device attribute descriptor.
323 */
324
325int device_create_file(struct device * dev, struct device_attribute * attr)
326{
327 int error = 0;
328 if (get_device(dev)) {
329 error = sysfs_create_file(&dev->kobj, &attr->attr);
330 put_device(dev);
331 }
332 return error;
333}
334
335/**
336 * device_remove_file - remove sysfs attribute file.
337 * @dev: device.
338 * @attr: device attribute descriptor.
339 */
340
341void device_remove_file(struct device * dev, struct device_attribute * attr)
342{
343 if (get_device(dev)) {
344 sysfs_remove_file(&dev->kobj, &attr->attr);
345 put_device(dev);
346 }
347}
348
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -0700349/**
350 * device_create_bin_file - create sysfs binary attribute file for device.
351 * @dev: device.
352 * @attr: device binary attribute descriptor.
353 */
354int device_create_bin_file(struct device *dev, struct bin_attribute *attr)
355{
356 int error = -EINVAL;
357 if (dev)
358 error = sysfs_create_bin_file(&dev->kobj, attr);
359 return error;
360}
361EXPORT_SYMBOL_GPL(device_create_bin_file);
362
363/**
364 * device_remove_bin_file - remove sysfs binary attribute file
365 * @dev: device.
366 * @attr: device binary attribute descriptor.
367 */
368void device_remove_bin_file(struct device *dev, struct bin_attribute *attr)
369{
370 if (dev)
371 sysfs_remove_bin_file(&dev->kobj, attr);
372}
373EXPORT_SYMBOL_GPL(device_remove_bin_file);
374
James Bottomley34bb61f2005-09-06 16:56:51 -0700375static void klist_children_get(struct klist_node *n)
376{
377 struct device *dev = container_of(n, struct device, knode_parent);
378
379 get_device(dev);
380}
381
382static void klist_children_put(struct klist_node *n)
383{
384 struct device *dev = container_of(n, struct device, knode_parent);
385
386 put_device(dev);
387}
388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390/**
391 * device_initialize - init device structure.
392 * @dev: device.
393 *
394 * This prepares the device for use by other layers,
395 * including adding it to the device hierarchy.
396 * It is the first half of device_register(), if called by
397 * that, though it can also be called separately, so one
398 * may use @dev's fields (e.g. the refcount).
399 */
400
401void device_initialize(struct device *dev)
402{
403 kobj_set_kset_s(dev, devices_subsys);
404 kobject_init(&dev->kobj);
James Bottomley34bb61f2005-09-06 16:56:51 -0700405 klist_init(&dev->klist_children, klist_children_get,
406 klist_children_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 INIT_LIST_HEAD(&dev->dma_pools);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700408 INIT_LIST_HEAD(&dev->node);
mochel@digitalimplant.orgaf703162005-03-21 10:41:04 -0800409 init_MUTEX(&dev->sem);
David Brownell0ac85242005-09-12 19:39:34 -0700410 device_init_wakeup(dev, 0);
Christoph Hellwig87348132006-12-06 20:32:33 -0800411 set_dev_node(dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412}
413
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100414#ifdef CONFIG_SYSFS_DEPRECATED
Cornelia Huckc744aea2007-01-08 20:16:44 +0100415static struct kobject * get_device_parent(struct device *dev,
416 struct device *parent)
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100417{
418 /* Set the parent to the class, not the parent device */
419 /* this keeps sysfs from having a symlink to make old udevs happy */
420 if (dev->class)
Cornelia Huckc744aea2007-01-08 20:16:44 +0100421 return &dev->class->subsys.kset.kobj;
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100422 else if (parent)
Cornelia Huckc744aea2007-01-08 20:16:44 +0100423 return &parent->kobj;
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100424
Cornelia Huckc744aea2007-01-08 20:16:44 +0100425 return NULL;
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100426}
427#else
Cornelia Huckc744aea2007-01-08 20:16:44 +0100428static struct kobject * virtual_device_parent(struct device *dev)
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700429{
430 if (!dev->class)
Cornelia Huckc744aea2007-01-08 20:16:44 +0100431 return ERR_PTR(-ENODEV);
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700432
433 if (!dev->class->virtual_dir) {
434 static struct kobject *virtual_dir = NULL;
435
436 if (!virtual_dir)
437 virtual_dir = kobject_add_dir(&devices_subsys.kset.kobj, "virtual");
438 dev->class->virtual_dir = kobject_add_dir(virtual_dir, dev->class->name);
439 }
440
Cornelia Huckc744aea2007-01-08 20:16:44 +0100441 return dev->class->virtual_dir;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700442}
443
Cornelia Huckc744aea2007-01-08 20:16:44 +0100444static struct kobject * get_device_parent(struct device *dev,
445 struct device *parent)
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100446{
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100447 /* if this is a class device, and has no parent, create one */
448 if ((dev->class) && (parent == NULL)) {
Cornelia Huckc744aea2007-01-08 20:16:44 +0100449 return virtual_device_parent(dev);
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100450 } else if (parent)
Cornelia Huckc744aea2007-01-08 20:16:44 +0100451 return &parent->kobj;
452 return NULL;
453}
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100454
Cornelia Huckc744aea2007-01-08 20:16:44 +0100455#endif
456static int setup_parent(struct device *dev, struct device *parent)
457{
458 struct kobject *kobj;
459 kobj = get_device_parent(dev, parent);
460 if (IS_ERR(kobj))
461 return PTR_ERR(kobj);
462 if (kobj)
463 dev->kobj.parent = kobj;
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100464 return 0;
465}
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467/**
468 * device_add - add device to device hierarchy.
469 * @dev: device.
470 *
471 * This is part 2 of device_register(), though may be called
472 * separately _iff_ device_initialize() has been called separately.
473 *
474 * This adds it to the kobject hierarchy via kobject_add(), adds it
475 * to the global and sibling lists for the device, then
476 * adds it to the other relevant subsystems of the driver model.
477 */
478int device_add(struct device *dev)
479{
480 struct device *parent = NULL;
Greg Kroah-Hartmane9a7d302006-06-20 13:59:20 -0700481 char *class_name = NULL;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200482 struct class_interface *class_intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 int error = -EINVAL;
484
485 dev = get_device(dev);
486 if (!dev || !strlen(dev->bus_id))
487 goto Error;
488
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100489 pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id);
Greg Kroah-Hartmanc205ef42006-08-07 22:19:37 -0700490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 parent = get_device(dev->parent);
492
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100493 error = setup_parent(dev, parent);
494 if (error)
495 goto Error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497 /* first, register with generic layer. */
498 kobject_set_name(&dev->kobj, "%s", dev->bus_id);
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100499 error = kobject_add(&dev->kobj);
500 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 goto Error;
Kay Sieversa7fd6702005-10-01 14:49:43 +0200502
Brian Walsh37022642006-08-14 22:43:19 -0700503 /* notify platform of device entry */
504 if (platform_notify)
505 platform_notify(dev);
506
Benjamin Herrenschmidt116af372006-10-25 13:44:59 +1000507 /* notify clients of device entry (new way) */
508 if (dev->bus)
509 blocking_notifier_call_chain(&dev->bus->bus_notifier,
510 BUS_NOTIFY_ADD_DEVICE, dev);
511
Kay Sieversa7fd6702005-10-01 14:49:43 +0200512 dev->uevent_attr.attr.name = "uevent";
513 dev->uevent_attr.attr.mode = S_IWUSR;
514 if (dev->driver)
515 dev->uevent_attr.attr.owner = dev->driver->owner;
516 dev->uevent_attr.store = store_uevent;
Cornelia Hucka306eea2006-09-22 11:37:13 +0200517 error = device_create_file(dev, &dev->uevent_attr);
518 if (error)
519 goto attrError;
Kay Sieversa7fd6702005-10-01 14:49:43 +0200520
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700521 if (MAJOR(dev->devt)) {
522 struct device_attribute *attr;
523 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
524 if (!attr) {
525 error = -ENOMEM;
Cornelia Hucka306eea2006-09-22 11:37:13 +0200526 goto ueventattrError;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700527 }
528 attr->attr.name = "dev";
529 attr->attr.mode = S_IRUGO;
530 if (dev->driver)
531 attr->attr.owner = dev->driver->owner;
532 attr->show = show_dev;
533 error = device_create_file(dev, attr);
534 if (error) {
535 kfree(attr);
Cornelia Hucka306eea2006-09-22 11:37:13 +0200536 goto ueventattrError;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700537 }
538
539 dev->devt_attr = attr;
540 }
541
Kay Sieversb9d9c822006-06-15 15:31:56 +0200542 if (dev->class) {
543 sysfs_create_link(&dev->kobj, &dev->class->subsys.kset.kobj,
544 "subsystem");
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100545 /* If this is not a "fake" compatible device, then create the
546 * symlink from the class to the device. */
547 if (dev->kobj.parent != &dev->class->subsys.kset.kobj)
548 sysfs_create_link(&dev->class->subsys.kset.kobj,
549 &dev->kobj, dev->bus_id);
Kay Sievers99ef3ef2006-09-14 11:23:28 +0200550#ifdef CONFIG_SYSFS_DEPRECATED
Greg Kroah-Hartman64bb5d22006-06-28 16:19:58 -0700551 if (parent) {
Cornelia Huckcb360bb2006-11-27 10:35:05 +0100552 sysfs_create_link(&dev->kobj, &dev->parent->kobj,
553 "device");
554 class_name = make_class_name(dev->class->name,
555 &dev->kobj);
556 if (class_name)
557 sysfs_create_link(&dev->parent->kobj,
558 &dev->kobj, class_name);
Greg Kroah-Hartman64bb5d22006-06-28 16:19:58 -0700559 }
Kay Sievers99ef3ef2006-09-14 11:23:28 +0200560#endif
Kay Sieversb9d9c822006-06-15 15:31:56 +0200561 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700562
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700563 if ((error = device_add_attrs(dev)))
564 goto AttrsError;
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700565 if ((error = device_add_groups(dev)))
566 goto GroupError;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 if ((error = device_pm_add(dev)))
568 goto PMError;
569 if ((error = bus_add_device(dev)))
570 goto BusError;
Kay Sievers53877d02006-04-04 20:42:26 +0200571 kobject_uevent(&dev->kobj, KOBJ_ADD);
Alan Sternf70fa622006-10-05 17:03:24 -0400572 if ((error = bus_attach_device(dev)))
573 goto AttachError;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 if (parent)
James Bottomleyd856f1e2005-08-19 09:14:01 -0400575 klist_add_tail(&dev->knode_parent, &parent->klist_children);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700577 if (dev->class) {
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700578 down(&dev->class->sem);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200579 /* tie the class to the device */
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700580 list_add_tail(&dev->node, &dev->class->devices);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200581
582 /* notify any interfaces that the device is here */
583 list_for_each_entry(class_intf, &dev->class->interfaces, node)
584 if (class_intf->add_dev)
585 class_intf->add_dev(dev, class_intf);
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700586 up(&dev->class->sem);
587 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 Done:
Greg Kroah-Hartmane9a7d302006-06-20 13:59:20 -0700589 kfree(class_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 put_device(dev);
591 return error;
Alan Sternf70fa622006-10-05 17:03:24 -0400592 AttachError:
593 bus_remove_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 BusError:
595 device_pm_remove(dev);
596 PMError:
Benjamin Herrenschmidt116af372006-10-25 13:44:59 +1000597 if (dev->bus)
598 blocking_notifier_call_chain(&dev->bus->bus_notifier,
599 BUS_NOTIFY_DEL_DEVICE, dev);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700600 device_remove_groups(dev);
601 GroupError:
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700602 device_remove_attrs(dev);
603 AttrsError:
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700604 if (dev->devt_attr) {
605 device_remove_file(dev, dev->devt_attr);
606 kfree(dev->devt_attr);
607 }
Cornelia Hucka306eea2006-09-22 11:37:13 +0200608 ueventattrError:
609 device_remove_file(dev, &dev->uevent_attr);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700610 attrError:
Kay Sievers312c0042005-11-16 09:00:00 +0100611 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 kobject_del(&dev->kobj);
613 Error:
614 if (parent)
615 put_device(parent);
616 goto Done;
617}
618
619
620/**
621 * device_register - register a device with the system.
622 * @dev: pointer to the device structure
623 *
624 * This happens in two clean steps - initialize the device
625 * and add it to the system. The two steps can be called
626 * separately, but this is the easiest and most common.
627 * I.e. you should only call the two helpers separately if
628 * have a clearly defined need to use and refcount the device
629 * before it is added to the hierarchy.
630 */
631
632int device_register(struct device *dev)
633{
634 device_initialize(dev);
635 return device_add(dev);
636}
637
638
639/**
640 * get_device - increment reference count for device.
641 * @dev: device.
642 *
643 * This simply forwards the call to kobject_get(), though
644 * we do take care to provide for the case that we get a NULL
645 * pointer passed in.
646 */
647
648struct device * get_device(struct device * dev)
649{
650 return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
651}
652
653
654/**
655 * put_device - decrement reference count.
656 * @dev: device in question.
657 */
658void put_device(struct device * dev)
659{
660 if (dev)
661 kobject_put(&dev->kobj);
662}
663
664
665/**
666 * device_del - delete device from system.
667 * @dev: device.
668 *
669 * This is the first part of the device unregistration
670 * sequence. This removes the device from the lists we control
671 * from here, has it removed from the other driver model
672 * subsystems it was added to in device_add(), and removes it
673 * from the kobject hierarchy.
674 *
675 * NOTE: this should be called manually _iff_ device_add() was
676 * also called manually.
677 */
678
679void device_del(struct device * dev)
680{
681 struct device * parent = dev->parent;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200682 struct class_interface *class_intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 if (parent)
Patrick Mocheld62c0f92005-06-24 08:39:33 -0700685 klist_del(&dev->knode_parent);
Catalin Marinas82189b92006-11-25 11:09:30 -0800686 if (dev->devt_attr) {
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700687 device_remove_file(dev, dev->devt_attr);
Catalin Marinas82189b92006-11-25 11:09:30 -0800688 kfree(dev->devt_attr);
689 }
Kay Sieversb9d9c822006-06-15 15:31:56 +0200690 if (dev->class) {
691 sysfs_remove_link(&dev->kobj, "subsystem");
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100692 /* If this is not a "fake" compatible device, remove the
693 * symlink from the class to the device. */
694 if (dev->kobj.parent != &dev->class->subsys.kset.kobj)
695 sysfs_remove_link(&dev->class->subsys.kset.kobj,
696 dev->bus_id);
Kay Sievers99ef3ef2006-09-14 11:23:28 +0200697#ifdef CONFIG_SYSFS_DEPRECATED
Greg Kroah-Hartman64bb5d22006-06-28 16:19:58 -0700698 if (parent) {
Kay Sievers99ef3ef2006-09-14 11:23:28 +0200699 char *class_name = make_class_name(dev->class->name,
700 &dev->kobj);
Cornelia Huckcb360bb2006-11-27 10:35:05 +0100701 if (class_name)
702 sysfs_remove_link(&dev->parent->kobj,
703 class_name);
Kay Sievers99ef3ef2006-09-14 11:23:28 +0200704 kfree(class_name);
705 sysfs_remove_link(&dev->kobj, "device");
Greg Kroah-Hartman64bb5d22006-06-28 16:19:58 -0700706 }
Kay Sievers99ef3ef2006-09-14 11:23:28 +0200707#endif
708
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700709 down(&dev->class->sem);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200710 /* notify any interfaces that the device is now gone */
711 list_for_each_entry(class_intf, &dev->class->interfaces, node)
712 if (class_intf->remove_dev)
713 class_intf->remove_dev(dev, class_intf);
714 /* remove the device from the class list */
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700715 list_del_init(&dev->node);
716 up(&dev->class->sem);
Kay Sieversb9d9c822006-06-15 15:31:56 +0200717 }
Kay Sieversa7fd6702005-10-01 14:49:43 +0200718 device_remove_file(dev, &dev->uevent_attr);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700719 device_remove_groups(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700720 device_remove_attrs(dev);
Benjamin Herrenschmidt28953532006-11-08 19:46:14 -0800721 bus_remove_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 /* Notify the platform of the removal, in case they
724 * need to do anything...
725 */
726 if (platform_notify_remove)
727 platform_notify_remove(dev);
Benjamin Herrenschmidt116af372006-10-25 13:44:59 +1000728 if (dev->bus)
729 blocking_notifier_call_chain(&dev->bus->bus_notifier,
730 BUS_NOTIFY_DEL_DEVICE, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 device_pm_remove(dev);
Kay Sievers312c0042005-11-16 09:00:00 +0100732 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 kobject_del(&dev->kobj);
734 if (parent)
735 put_device(parent);
736}
737
738/**
739 * device_unregister - unregister device from system.
740 * @dev: device going away.
741 *
742 * We do this in two parts, like we do device_register(). First,
743 * we remove it from all the subsystems with device_del(), then
744 * we decrement the reference count via put_device(). If that
745 * is the final reference count, the device will be cleaned up
746 * via device_release() above. Otherwise, the structure will
747 * stick around until the final reference to the device is dropped.
748 */
749void device_unregister(struct device * dev)
750{
751 pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id);
752 device_del(dev);
753 put_device(dev);
754}
755
756
mochel@digitalimplant.org36239572005-03-24 19:08:30 -0800757static struct device * next_device(struct klist_iter * i)
758{
759 struct klist_node * n = klist_next(i);
760 return n ? container_of(n, struct device, knode_parent) : NULL;
761}
762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763/**
764 * device_for_each_child - device child iterator.
Randy Dunlapc41455f2005-10-23 11:59:14 -0700765 * @parent: parent struct device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 * @data: data for the callback.
767 * @fn: function to be called for each device.
768 *
Randy Dunlapc41455f2005-10-23 11:59:14 -0700769 * Iterate over @parent's child devices, and call @fn for each,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 * passing it @data.
771 *
772 * We check the return of @fn each time. If it returns anything
773 * other than 0, we break out and return that value.
774 */
mochel@digitalimplant.org36239572005-03-24 19:08:30 -0800775int device_for_each_child(struct device * parent, void * data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 int (*fn)(struct device *, void *))
777{
mochel@digitalimplant.org36239572005-03-24 19:08:30 -0800778 struct klist_iter i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 struct device * child;
780 int error = 0;
781
mochel@digitalimplant.org36239572005-03-24 19:08:30 -0800782 klist_iter_init(&parent->klist_children, &i);
783 while ((child = next_device(&i)) && !error)
784 error = fn(child, data);
785 klist_iter_exit(&i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 return error;
787}
788
Cornelia Huck5ab69982006-11-16 15:42:07 +0100789/**
790 * device_find_child - device iterator for locating a particular device.
791 * @parent: parent struct device
792 * @data: Data to pass to match function
793 * @match: Callback function to check device
794 *
795 * This is similar to the device_for_each_child() function above, but it
796 * returns a reference to a device that is 'found' for later use, as
797 * determined by the @match callback.
798 *
799 * The callback should return 0 if the device doesn't match and non-zero
800 * if it does. If the callback returns non-zero and a reference to the
801 * current device can be obtained, this function will return to the caller
802 * and not iterate over any more devices.
803 */
804struct device * device_find_child(struct device *parent, void *data,
805 int (*match)(struct device *, void *))
806{
807 struct klist_iter i;
808 struct device *child;
809
810 if (!parent)
811 return NULL;
812
813 klist_iter_init(&parent->klist_children, &i);
814 while ((child = next_device(&i)))
815 if (match(child, data) && get_device(child))
816 break;
817 klist_iter_exit(&i);
818 return child;
819}
820
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821int __init devices_init(void)
822{
823 return subsystem_register(&devices_subsys);
824}
825
826EXPORT_SYMBOL_GPL(device_for_each_child);
Cornelia Huck5ab69982006-11-16 15:42:07 +0100827EXPORT_SYMBOL_GPL(device_find_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829EXPORT_SYMBOL_GPL(device_initialize);
830EXPORT_SYMBOL_GPL(device_add);
831EXPORT_SYMBOL_GPL(device_register);
832
833EXPORT_SYMBOL_GPL(device_del);
834EXPORT_SYMBOL_GPL(device_unregister);
835EXPORT_SYMBOL_GPL(get_device);
836EXPORT_SYMBOL_GPL(put_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
838EXPORT_SYMBOL_GPL(device_create_file);
839EXPORT_SYMBOL_GPL(device_remove_file);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700840
841
842static void device_create_release(struct device *dev)
843{
844 pr_debug("%s called for %s\n", __FUNCTION__, dev->bus_id);
845 kfree(dev);
846}
847
848/**
849 * device_create - creates a device and registers it with sysfs
Henrik Kretzschmar42734da2006-07-05 00:53:19 +0200850 * @class: pointer to the struct class that this device should be registered to
851 * @parent: pointer to the parent struct device of this new device, if any
852 * @devt: the dev_t for the char device to be added
853 * @fmt: string for the device's name
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700854 *
Henrik Kretzschmar42734da2006-07-05 00:53:19 +0200855 * This function can be used by char device classes. A struct device
856 * will be created in sysfs, registered to the specified class.
857 *
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700858 * A "dev" file will be created, showing the dev_t for the device, if
859 * the dev_t is not 0,0.
Henrik Kretzschmar42734da2006-07-05 00:53:19 +0200860 * If a pointer to a parent struct device is passed in, the newly created
861 * struct device will be a child of that device in sysfs.
862 * The pointer to the struct device will be returned from the call.
863 * Any further sysfs files that might be required can be created using this
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700864 * pointer.
865 *
866 * Note: the struct class passed to this function must have previously
867 * been created with a call to class_create().
868 */
869struct device *device_create(struct class *class, struct device *parent,
Greg Kroah-Hartman5cbe5f82006-08-10 01:19:19 -0400870 dev_t devt, const char *fmt, ...)
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700871{
872 va_list args;
873 struct device *dev = NULL;
874 int retval = -ENODEV;
875
876 if (class == NULL || IS_ERR(class))
877 goto error;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700878
879 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
880 if (!dev) {
881 retval = -ENOMEM;
882 goto error;
883 }
884
885 dev->devt = devt;
886 dev->class = class;
887 dev->parent = parent;
888 dev->release = device_create_release;
889
890 va_start(args, fmt);
891 vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args);
892 va_end(args);
893 retval = device_register(dev);
894 if (retval)
895 goto error;
896
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700897 return dev;
898
899error:
900 kfree(dev);
901 return ERR_PTR(retval);
902}
903EXPORT_SYMBOL_GPL(device_create);
904
905/**
906 * device_destroy - removes a device that was created with device_create()
Henrik Kretzschmar42734da2006-07-05 00:53:19 +0200907 * @class: pointer to the struct class that this device was registered with
908 * @devt: the dev_t of the device that was previously registered
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700909 *
Henrik Kretzschmar42734da2006-07-05 00:53:19 +0200910 * This call unregisters and cleans up a device that was created with a
911 * call to device_create().
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700912 */
913void device_destroy(struct class *class, dev_t devt)
914{
915 struct device *dev = NULL;
916 struct device *dev_tmp;
917
918 down(&class->sem);
919 list_for_each_entry(dev_tmp, &class->devices, node) {
920 if (dev_tmp->devt == devt) {
921 dev = dev_tmp;
922 break;
923 }
924 }
925 up(&class->sem);
926
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700927 if (dev)
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700928 device_unregister(dev);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700929}
930EXPORT_SYMBOL_GPL(device_destroy);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -0700931
932/**
933 * device_rename - renames a device
934 * @dev: the pointer to the struct device to be renamed
935 * @new_name: the new name of the device
936 */
937int device_rename(struct device *dev, char *new_name)
938{
939 char *old_class_name = NULL;
940 char *new_class_name = NULL;
941 char *old_symlink_name = NULL;
942 int error;
943
944 dev = get_device(dev);
945 if (!dev)
946 return -EINVAL;
947
948 pr_debug("DEVICE: renaming '%s' to '%s'\n", dev->bus_id, new_name);
949
Kay Sievers99ef3ef2006-09-14 11:23:28 +0200950#ifdef CONFIG_SYSFS_DEPRECATED
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -0700951 if ((dev->class) && (dev->parent))
952 old_class_name = make_class_name(dev->class->name, &dev->kobj);
Kay Sievers99ef3ef2006-09-14 11:23:28 +0200953#endif
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -0700954
955 if (dev->class) {
956 old_symlink_name = kmalloc(BUS_ID_SIZE, GFP_KERNEL);
Jesper Juhl952ab432006-09-28 23:56:01 +0200957 if (!old_symlink_name) {
958 error = -ENOMEM;
959 goto out_free_old_class;
960 }
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -0700961 strlcpy(old_symlink_name, dev->bus_id, BUS_ID_SIZE);
962 }
963
964 strlcpy(dev->bus_id, new_name, BUS_ID_SIZE);
965
966 error = kobject_rename(&dev->kobj, new_name);
967
Kay Sievers99ef3ef2006-09-14 11:23:28 +0200968#ifdef CONFIG_SYSFS_DEPRECATED
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -0700969 if (old_class_name) {
970 new_class_name = make_class_name(dev->class->name, &dev->kobj);
971 if (new_class_name) {
972 sysfs_create_link(&dev->parent->kobj, &dev->kobj,
973 new_class_name);
974 sysfs_remove_link(&dev->parent->kobj, old_class_name);
975 }
976 }
Kay Sievers99ef3ef2006-09-14 11:23:28 +0200977#endif
978
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -0700979 if (dev->class) {
980 sysfs_remove_link(&dev->class->subsys.kset.kobj,
981 old_symlink_name);
982 sysfs_create_link(&dev->class->subsys.kset.kobj, &dev->kobj,
983 dev->bus_id);
984 }
985 put_device(dev);
986
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -0700987 kfree(new_class_name);
988 kfree(old_symlink_name);
Jesper Juhl952ab432006-09-28 23:56:01 +0200989 out_free_old_class:
990 kfree(old_class_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -0700991
992 return error;
993}
Cornelia Huck8a824722006-11-20 17:07:51 +0100994
995
996static int device_move_class_links(struct device *dev,
997 struct device *old_parent,
998 struct device *new_parent)
999{
1000#ifdef CONFIG_SYSFS_DEPRECATED
1001 int error;
1002 char *class_name;
1003
1004 class_name = make_class_name(dev->class->name, &dev->kobj);
1005 if (!class_name) {
Cornelia Huckcb360bb2006-11-27 10:35:05 +01001006 error = -ENOMEM;
Cornelia Huck8a824722006-11-20 17:07:51 +01001007 goto out;
1008 }
1009 if (old_parent) {
1010 sysfs_remove_link(&dev->kobj, "device");
1011 sysfs_remove_link(&old_parent->kobj, class_name);
1012 }
Cornelia Huckc744aea2007-01-08 20:16:44 +01001013 if (new_parent) {
1014 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1015 "device");
1016 if (error)
1017 goto out;
1018 error = sysfs_create_link(&new_parent->kobj, &dev->kobj,
1019 class_name);
1020 if (error)
1021 sysfs_remove_link(&dev->kobj, "device");
1022 }
1023 else
1024 error = 0;
Cornelia Huck8a824722006-11-20 17:07:51 +01001025out:
1026 kfree(class_name);
1027 return error;
1028#else
1029 return 0;
1030#endif
1031}
1032
1033/**
1034 * device_move - moves a device to a new parent
1035 * @dev: the pointer to the struct device to be moved
Cornelia Huckc744aea2007-01-08 20:16:44 +01001036 * @new_parent: the new parent of the device (can by NULL)
Cornelia Huck8a824722006-11-20 17:07:51 +01001037 */
1038int device_move(struct device *dev, struct device *new_parent)
1039{
1040 int error;
1041 struct device *old_parent;
Cornelia Huckc744aea2007-01-08 20:16:44 +01001042 struct kobject *new_parent_kobj;
Cornelia Huck8a824722006-11-20 17:07:51 +01001043
1044 dev = get_device(dev);
1045 if (!dev)
1046 return -EINVAL;
1047
Cornelia Huck8a824722006-11-20 17:07:51 +01001048 new_parent = get_device(new_parent);
Cornelia Huckc744aea2007-01-08 20:16:44 +01001049 new_parent_kobj = get_device_parent (dev, new_parent);
1050 if (IS_ERR(new_parent_kobj)) {
1051 error = PTR_ERR(new_parent_kobj);
1052 put_device(new_parent);
Cornelia Huck8a824722006-11-20 17:07:51 +01001053 goto out;
1054 }
1055 pr_debug("DEVICE: moving '%s' to '%s'\n", dev->bus_id,
Cornelia Huckc744aea2007-01-08 20:16:44 +01001056 new_parent ? new_parent->bus_id : "<NULL>");
1057 error = kobject_move(&dev->kobj, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01001058 if (error) {
1059 put_device(new_parent);
1060 goto out;
1061 }
1062 old_parent = dev->parent;
1063 dev->parent = new_parent;
1064 if (old_parent)
Cornelia Huckacf02d22006-11-22 17:49:39 +01001065 klist_remove(&dev->knode_parent);
Cornelia Huckc744aea2007-01-08 20:16:44 +01001066 if (new_parent)
1067 klist_add_tail(&dev->knode_parent, &new_parent->klist_children);
Cornelia Huck8a824722006-11-20 17:07:51 +01001068 if (!dev->class)
1069 goto out_put;
1070 error = device_move_class_links(dev, old_parent, new_parent);
1071 if (error) {
1072 /* We ignore errors on cleanup since we're hosed anyway... */
1073 device_move_class_links(dev, new_parent, old_parent);
1074 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
Cornelia Huckc744aea2007-01-08 20:16:44 +01001075 if (new_parent)
1076 klist_remove(&dev->knode_parent);
Cornelia Huck8a824722006-11-20 17:07:51 +01001077 if (old_parent)
1078 klist_add_tail(&dev->knode_parent,
1079 &old_parent->klist_children);
1080 }
1081 put_device(new_parent);
1082 goto out;
1083 }
1084out_put:
1085 put_device(old_parent);
1086out:
1087 put_device(dev);
1088 return error;
1089}
1090
1091EXPORT_SYMBOL_GPL(device_move);