blob: e13614241c9eeed951141d285bb5d8e041ded23c [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);
Kay Sieversf9f852d2006-10-07 21:54:55 +020098 else if (dev->type && dev->type->release)
99 dev->type->release(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700100 else if (dev->class && dev->class->dev_release)
101 dev->class->dev_release(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 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
110static struct kobj_type ktype_device = {
111 .release = device_release,
112 .sysfs_ops = &dev_sysfs_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113};
114
115
Kay Sievers312c0042005-11-16 09:00:00 +0100116static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
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;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700124 if (dev->class)
125 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 }
127 return 0;
128}
129
Kay Sievers312c0042005-11-16 09:00:00 +0100130static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
132 struct device *dev = to_dev(kobj);
133
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700134 if (dev->bus)
135 return dev->bus->name;
136 if (dev->class)
137 return dev->class->name;
138 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139}
140
Kay Sievers312c0042005-11-16 09:00:00 +0100141static int dev_uevent(struct kset *kset, struct kobject *kobj, char **envp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 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
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700149 /* 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
Kay Sievers239378f2006-10-07 21:54:55 +0200159 if (dev->driver)
Kay Sieversd81d9d62006-08-13 06:17:09 +0200160 add_uevent_var(envp, num_envp, &i,
161 buffer, buffer_size, &length,
162 "DRIVER=%s", dev->driver->name);
Kay Sievers239378f2006-10-07 21:54:55 +0200163
Kay Sieversa87cb2a2006-09-14 11:23:28 +0200164#ifdef CONFIG_SYSFS_DEPRECATED
Kay Sievers239378f2006-10-07 21:54:55 +0200165 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) {
Kay Sievers312c0042005-11-16 09:00:00 +0100190 add_uevent_var(envp, num_envp, &i,
191 buffer, buffer_size, &length,
Kay Sievers239378f2006-10-07 21:54:55 +0200192 "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);
Kay Sieversd81d9d62006-08-13 06:17:09 +0200198 }
Kay Sievers239378f2006-10-07 21:54:55 +0200199#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
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
Kay Sievers312c0042005-11-16 09:00:00 +0100208 if (dev->bus && dev->bus->uevent) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 /* have the bus specific function add its stuff */
Kay Sievers312c0042005-11-16 09:00:00 +0100210 retval = dev->bus->uevent(dev, envp, num_envp, buffer, buffer_size);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200211 if (retval)
212 pr_debug ("%s: bus uevent() returned %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 __FUNCTION__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 }
215
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700216 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);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200219 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);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700230 }
231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 return retval;
233}
234
Kay Sievers312c0042005-11-16 09:00:00 +0100235static struct kset_uevent_ops device_uevent_ops = {
236 .filter = dev_uevent_filter,
237 .name = dev_uevent_name,
238 .uevent = dev_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239};
240
Kay Sieversa7fd6702005-10-01 14:49:43 +0200241static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
242 const char *buf, size_t count)
243{
Kay Sievers312c0042005-11-16 09:00:00 +0100244 kobject_uevent(&dev->kobj, KOBJ_ADD);
Kay Sieversa7fd6702005-10-01 14:49:43 +0200245 return count;
246}
247
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700248static 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 }
263out:
264 return error;
265}
266
267static 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
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700277static int device_add_attrs(struct device *dev)
278{
279 struct class *class = dev->class;
Kay Sieversf9f852d2006-10-07 21:54:55 +0200280 struct device_type *type = dev->type;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700281 int error = 0;
282 int i;
283
Kay Sieversf9f852d2006-10-07 21:54:55 +0200284 if (class && class->dev_attrs) {
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700285 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 }
Kay Sieversf9f852d2006-10-07 21:54:55 +0200290 if (error)
291 while (--i >= 0)
292 device_remove_file(dev, &class->dev_attrs[i]);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700293 }
Kay Sieversf9f852d2006-10-07 21:54:55 +0200294
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
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700306 return error;
307}
308
309static void device_remove_attrs(struct device *dev)
310{
311 struct class *class = dev->class;
Kay Sieversf9f852d2006-10-07 21:54:55 +0200312 struct device_type *type = dev->type;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700313 int i;
314
Kay Sieversf9f852d2006-10-07 21:54:55 +0200315 if (class && class->dev_attrs) {
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700316 for (i = 0; attr_name(class->dev_attrs[i]); i++)
317 device_remove_file(dev, &class->dev_attrs[i]);
318 }
Kay Sieversf9f852d2006-10-07 21:54:55 +0200319
320 if (type && type->attrs) {
321 for (i = 0; attr_name(type->attrs[i]); i++)
322 device_remove_file(dev, &type->attrs[i]);
323 }
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700324}
325
326
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700327static 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
Martin Waitz0863afb2006-01-09 20:53:55 -0800333/*
334 * devices_subsys - structure to be registered with kobject core.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 */
336
Kay Sievers312c0042005-11-16 09:00:00 +0100337decl_subsys(devices, &ktype_device, &device_uevent_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339
340/**
341 * device_create_file - create sysfs attribute file for device.
342 * @dev: device.
343 * @attr: device attribute descriptor.
344 */
345
346int 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
362void 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
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -0700370/**
371 * device_create_bin_file - create sysfs binary attribute file for device.
372 * @dev: device.
373 * @attr: device binary attribute descriptor.
374 */
375int 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}
382EXPORT_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 */
389void device_remove_bin_file(struct device *dev, struct bin_attribute *attr)
390{
391 if (dev)
392 sysfs_remove_bin_file(&dev->kobj, attr);
393}
394EXPORT_SYMBOL_GPL(device_remove_bin_file);
395
James Bottomley34bb61f2005-09-06 16:56:51 -0700396static void klist_children_get(struct klist_node *n)
397{
398 struct device *dev = container_of(n, struct device, knode_parent);
399
400 get_device(dev);
401}
402
403static void klist_children_put(struct klist_node *n)
404{
405 struct device *dev = container_of(n, struct device, knode_parent);
406
407 put_device(dev);
408}
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411/**
412 * device_initialize - init device structure.
413 * @dev: device.
414 *
415 * This prepares the device for use by other layers,
416 * including adding it to the device hierarchy.
417 * It is the first half of device_register(), if called by
418 * that, though it can also be called separately, so one
419 * may use @dev's fields (e.g. the refcount).
420 */
421
422void device_initialize(struct device *dev)
423{
424 kobj_set_kset_s(dev, devices_subsys);
425 kobject_init(&dev->kobj);
James Bottomley34bb61f2005-09-06 16:56:51 -0700426 klist_init(&dev->klist_children, klist_children_get,
427 klist_children_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 INIT_LIST_HEAD(&dev->dma_pools);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700429 INIT_LIST_HEAD(&dev->node);
mochel@digitalimplant.orgaf703162005-03-21 10:41:04 -0800430 init_MUTEX(&dev->sem);
David Brownell0ac85242005-09-12 19:39:34 -0700431 device_init_wakeup(dev, 0);
Christoph Hellwig87348132006-12-06 20:32:33 -0800432 set_dev_node(dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433}
434
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100435#ifdef CONFIG_SYSFS_DEPRECATED
Cornelia Huckc744aeae2007-01-08 20:16:44 +0100436static struct kobject * get_device_parent(struct device *dev,
437 struct device *parent)
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100438{
439 /* Set the parent to the class, not the parent device */
440 /* this keeps sysfs from having a symlink to make old udevs happy */
441 if (dev->class)
Cornelia Huckc744aeae2007-01-08 20:16:44 +0100442 return &dev->class->subsys.kset.kobj;
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100443 else if (parent)
Cornelia Huckc744aeae2007-01-08 20:16:44 +0100444 return &parent->kobj;
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100445
Cornelia Huckc744aeae2007-01-08 20:16:44 +0100446 return NULL;
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100447}
448#else
Cornelia Huckc744aeae2007-01-08 20:16:44 +0100449static struct kobject * virtual_device_parent(struct device *dev)
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700450{
451 if (!dev->class)
Cornelia Huckc744aeae2007-01-08 20:16:44 +0100452 return ERR_PTR(-ENODEV);
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700453
454 if (!dev->class->virtual_dir) {
455 static struct kobject *virtual_dir = NULL;
456
457 if (!virtual_dir)
458 virtual_dir = kobject_add_dir(&devices_subsys.kset.kobj, "virtual");
459 dev->class->virtual_dir = kobject_add_dir(virtual_dir, dev->class->name);
460 }
461
Cornelia Huckc744aeae2007-01-08 20:16:44 +0100462 return dev->class->virtual_dir;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700463}
464
Cornelia Huckc744aeae2007-01-08 20:16:44 +0100465static struct kobject * get_device_parent(struct device *dev,
466 struct device *parent)
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100467{
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100468 /* if this is a class device, and has no parent, create one */
469 if ((dev->class) && (parent == NULL)) {
Cornelia Huckc744aeae2007-01-08 20:16:44 +0100470 return virtual_device_parent(dev);
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100471 } else if (parent)
Cornelia Huckc744aeae2007-01-08 20:16:44 +0100472 return &parent->kobj;
473 return NULL;
474}
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100475
Cornelia Huckc744aeae2007-01-08 20:16:44 +0100476#endif
477static int setup_parent(struct device *dev, struct device *parent)
478{
479 struct kobject *kobj;
480 kobj = get_device_parent(dev, parent);
481 if (IS_ERR(kobj))
482 return PTR_ERR(kobj);
483 if (kobj)
484 dev->kobj.parent = kobj;
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100485 return 0;
486}
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488/**
489 * device_add - add device to device hierarchy.
490 * @dev: device.
491 *
492 * This is part 2 of device_register(), though may be called
493 * separately _iff_ device_initialize() has been called separately.
494 *
495 * This adds it to the kobject hierarchy via kobject_add(), adds it
496 * to the global and sibling lists for the device, then
497 * adds it to the other relevant subsystems of the driver model.
498 */
499int device_add(struct device *dev)
500{
501 struct device *parent = NULL;
Greg Kroah-Hartmane9a7d302006-06-20 13:59:20 -0700502 char *class_name = NULL;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200503 struct class_interface *class_intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 int error = -EINVAL;
505
506 dev = get_device(dev);
507 if (!dev || !strlen(dev->bus_id))
508 goto Error;
509
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100510 pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id);
Greg Kroah-Hartmanc205ef42006-08-07 22:19:37 -0700511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 parent = get_device(dev->parent);
513
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100514 error = setup_parent(dev, parent);
515 if (error)
516 goto Error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518 /* first, register with generic layer. */
519 kobject_set_name(&dev->kobj, "%s", dev->bus_id);
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100520 error = kobject_add(&dev->kobj);
521 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 goto Error;
Kay Sieversa7fd6702005-10-01 14:49:43 +0200523
Brian Walsh37022642006-08-14 22:43:19 -0700524 /* notify platform of device entry */
525 if (platform_notify)
526 platform_notify(dev);
527
Benjamin Herrenschmidt116af372006-10-25 13:44:59 +1000528 /* notify clients of device entry (new way) */
529 if (dev->bus)
530 blocking_notifier_call_chain(&dev->bus->bus_notifier,
531 BUS_NOTIFY_ADD_DEVICE, dev);
532
Kay Sieversa7fd6702005-10-01 14:49:43 +0200533 dev->uevent_attr.attr.name = "uevent";
534 dev->uevent_attr.attr.mode = S_IWUSR;
535 if (dev->driver)
536 dev->uevent_attr.attr.owner = dev->driver->owner;
537 dev->uevent_attr.store = store_uevent;
Cornelia Hucka306eea2006-09-22 11:37:13 +0200538 error = device_create_file(dev, &dev->uevent_attr);
539 if (error)
540 goto attrError;
Kay Sieversa7fd6702005-10-01 14:49:43 +0200541
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700542 if (MAJOR(dev->devt)) {
543 struct device_attribute *attr;
544 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
545 if (!attr) {
546 error = -ENOMEM;
Cornelia Hucka306eea2006-09-22 11:37:13 +0200547 goto ueventattrError;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700548 }
549 attr->attr.name = "dev";
550 attr->attr.mode = S_IRUGO;
551 if (dev->driver)
552 attr->attr.owner = dev->driver->owner;
553 attr->show = show_dev;
554 error = device_create_file(dev, attr);
555 if (error) {
556 kfree(attr);
Cornelia Hucka306eea2006-09-22 11:37:13 +0200557 goto ueventattrError;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700558 }
559
560 dev->devt_attr = attr;
561 }
562
Kay Sieversb9d9c822006-06-15 15:31:56 +0200563 if (dev->class) {
564 sysfs_create_link(&dev->kobj, &dev->class->subsys.kset.kobj,
565 "subsystem");
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100566 /* If this is not a "fake" compatible device, then create the
567 * symlink from the class to the device. */
568 if (dev->kobj.parent != &dev->class->subsys.kset.kobj)
569 sysfs_create_link(&dev->class->subsys.kset.kobj,
570 &dev->kobj, dev->bus_id);
Kay Sievers99ef3ef2006-09-14 11:23:28 +0200571#ifdef CONFIG_SYSFS_DEPRECATED
Greg Kroah-Hartman64bb5d22006-06-28 16:19:58 -0700572 if (parent) {
Cornelia Huckcb360bb2006-11-27 10:35:05 +0100573 sysfs_create_link(&dev->kobj, &dev->parent->kobj,
574 "device");
575 class_name = make_class_name(dev->class->name,
576 &dev->kobj);
577 if (class_name)
578 sysfs_create_link(&dev->parent->kobj,
579 &dev->kobj, class_name);
Greg Kroah-Hartman64bb5d22006-06-28 16:19:58 -0700580 }
Kay Sievers99ef3ef2006-09-14 11:23:28 +0200581#endif
Kay Sieversb9d9c822006-06-15 15:31:56 +0200582 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700583
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700584 if ((error = device_add_attrs(dev)))
585 goto AttrsError;
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700586 if ((error = device_add_groups(dev)))
587 goto GroupError;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 if ((error = device_pm_add(dev)))
589 goto PMError;
590 if ((error = bus_add_device(dev)))
591 goto BusError;
Kay Sieversb7a3e812006-10-07 21:54:55 +0200592 if (!dev->uevent_suppress)
593 kobject_uevent(&dev->kobj, KOBJ_ADD);
Alan Sternf70fa622006-10-05 17:03:24 -0400594 if ((error = bus_attach_device(dev)))
595 goto AttachError;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 if (parent)
James Bottomleyd856f1e32005-08-19 09:14:01 -0400597 klist_add_tail(&dev->knode_parent, &parent->klist_children);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700599 if (dev->class) {
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700600 down(&dev->class->sem);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200601 /* tie the class to the device */
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700602 list_add_tail(&dev->node, &dev->class->devices);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200603
604 /* notify any interfaces that the device is here */
605 list_for_each_entry(class_intf, &dev->class->interfaces, node)
606 if (class_intf->add_dev)
607 class_intf->add_dev(dev, class_intf);
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700608 up(&dev->class->sem);
609 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 Done:
Greg Kroah-Hartmane9a7d302006-06-20 13:59:20 -0700611 kfree(class_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 put_device(dev);
613 return error;
Alan Sternf70fa622006-10-05 17:03:24 -0400614 AttachError:
615 bus_remove_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 BusError:
617 device_pm_remove(dev);
618 PMError:
Benjamin Herrenschmidt116af372006-10-25 13:44:59 +1000619 if (dev->bus)
620 blocking_notifier_call_chain(&dev->bus->bus_notifier,
621 BUS_NOTIFY_DEL_DEVICE, dev);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700622 device_remove_groups(dev);
623 GroupError:
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700624 device_remove_attrs(dev);
625 AttrsError:
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700626 if (dev->devt_attr) {
627 device_remove_file(dev, dev->devt_attr);
628 kfree(dev->devt_attr);
629 }
Cornelia Hucka306eea2006-09-22 11:37:13 +0200630 ueventattrError:
631 device_remove_file(dev, &dev->uevent_attr);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700632 attrError:
Kay Sievers312c0042005-11-16 09:00:00 +0100633 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 kobject_del(&dev->kobj);
635 Error:
636 if (parent)
637 put_device(parent);
638 goto Done;
639}
640
641
642/**
643 * device_register - register a device with the system.
644 * @dev: pointer to the device structure
645 *
646 * This happens in two clean steps - initialize the device
647 * and add it to the system. The two steps can be called
648 * separately, but this is the easiest and most common.
649 * I.e. you should only call the two helpers separately if
650 * have a clearly defined need to use and refcount the device
651 * before it is added to the hierarchy.
652 */
653
654int device_register(struct device *dev)
655{
656 device_initialize(dev);
657 return device_add(dev);
658}
659
660
661/**
662 * get_device - increment reference count for device.
663 * @dev: device.
664 *
665 * This simply forwards the call to kobject_get(), though
666 * we do take care to provide for the case that we get a NULL
667 * pointer passed in.
668 */
669
670struct device * get_device(struct device * dev)
671{
672 return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
673}
674
675
676/**
677 * put_device - decrement reference count.
678 * @dev: device in question.
679 */
680void put_device(struct device * dev)
681{
682 if (dev)
683 kobject_put(&dev->kobj);
684}
685
686
687/**
688 * device_del - delete device from system.
689 * @dev: device.
690 *
691 * This is the first part of the device unregistration
692 * sequence. This removes the device from the lists we control
693 * from here, has it removed from the other driver model
694 * subsystems it was added to in device_add(), and removes it
695 * from the kobject hierarchy.
696 *
697 * NOTE: this should be called manually _iff_ device_add() was
698 * also called manually.
699 */
700
701void device_del(struct device * dev)
702{
703 struct device * parent = dev->parent;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200704 struct class_interface *class_intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 if (parent)
Patrick Mocheld62c0f92005-06-24 08:39:33 -0700707 klist_del(&dev->knode_parent);
Catalin Marinas82189b92006-11-25 11:09:30 -0800708 if (dev->devt_attr) {
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700709 device_remove_file(dev, dev->devt_attr);
Catalin Marinas82189b92006-11-25 11:09:30 -0800710 kfree(dev->devt_attr);
711 }
Kay Sieversb9d9c822006-06-15 15:31:56 +0200712 if (dev->class) {
713 sysfs_remove_link(&dev->kobj, "subsystem");
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100714 /* If this is not a "fake" compatible device, remove the
715 * symlink from the class to the device. */
716 if (dev->kobj.parent != &dev->class->subsys.kset.kobj)
717 sysfs_remove_link(&dev->class->subsys.kset.kobj,
718 dev->bus_id);
Kay Sievers99ef3ef2006-09-14 11:23:28 +0200719#ifdef CONFIG_SYSFS_DEPRECATED
Greg Kroah-Hartman64bb5d22006-06-28 16:19:58 -0700720 if (parent) {
Kay Sievers99ef3ef2006-09-14 11:23:28 +0200721 char *class_name = make_class_name(dev->class->name,
722 &dev->kobj);
Cornelia Huckcb360bb2006-11-27 10:35:05 +0100723 if (class_name)
724 sysfs_remove_link(&dev->parent->kobj,
725 class_name);
Kay Sievers99ef3ef2006-09-14 11:23:28 +0200726 kfree(class_name);
727 sysfs_remove_link(&dev->kobj, "device");
Greg Kroah-Hartman64bb5d22006-06-28 16:19:58 -0700728 }
Kay Sievers99ef3ef2006-09-14 11:23:28 +0200729#endif
730
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700731 down(&dev->class->sem);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200732 /* notify any interfaces that the device is now gone */
733 list_for_each_entry(class_intf, &dev->class->interfaces, node)
734 if (class_intf->remove_dev)
735 class_intf->remove_dev(dev, class_intf);
736 /* remove the device from the class list */
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700737 list_del_init(&dev->node);
738 up(&dev->class->sem);
Kay Sieversb9d9c822006-06-15 15:31:56 +0200739 }
Kay Sieversa7fd6702005-10-01 14:49:43 +0200740 device_remove_file(dev, &dev->uevent_attr);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700741 device_remove_groups(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700742 device_remove_attrs(dev);
Benjamin Herrenschmidt28953532006-11-08 19:46:14 -0800743 bus_remove_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
745 /* Notify the platform of the removal, in case they
746 * need to do anything...
747 */
748 if (platform_notify_remove)
749 platform_notify_remove(dev);
Benjamin Herrenschmidt116af372006-10-25 13:44:59 +1000750 if (dev->bus)
751 blocking_notifier_call_chain(&dev->bus->bus_notifier,
752 BUS_NOTIFY_DEL_DEVICE, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 device_pm_remove(dev);
Kay Sievers312c0042005-11-16 09:00:00 +0100754 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 kobject_del(&dev->kobj);
756 if (parent)
757 put_device(parent);
758}
759
760/**
761 * device_unregister - unregister device from system.
762 * @dev: device going away.
763 *
764 * We do this in two parts, like we do device_register(). First,
765 * we remove it from all the subsystems with device_del(), then
766 * we decrement the reference count via put_device(). If that
767 * is the final reference count, the device will be cleaned up
768 * via device_release() above. Otherwise, the structure will
769 * stick around until the final reference to the device is dropped.
770 */
771void device_unregister(struct device * dev)
772{
773 pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id);
774 device_del(dev);
775 put_device(dev);
776}
777
778
mochel@digitalimplant.org36239572005-03-24 19:08:30 -0800779static struct device * next_device(struct klist_iter * i)
780{
781 struct klist_node * n = klist_next(i);
782 return n ? container_of(n, struct device, knode_parent) : NULL;
783}
784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785/**
786 * device_for_each_child - device child iterator.
Randy Dunlapc41455f2005-10-23 11:59:14 -0700787 * @parent: parent struct device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 * @data: data for the callback.
789 * @fn: function to be called for each device.
790 *
Randy Dunlapc41455f2005-10-23 11:59:14 -0700791 * Iterate over @parent's child devices, and call @fn for each,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 * passing it @data.
793 *
794 * We check the return of @fn each time. If it returns anything
795 * other than 0, we break out and return that value.
796 */
mochel@digitalimplant.org36239572005-03-24 19:08:30 -0800797int device_for_each_child(struct device * parent, void * data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 int (*fn)(struct device *, void *))
799{
mochel@digitalimplant.org36239572005-03-24 19:08:30 -0800800 struct klist_iter i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 struct device * child;
802 int error = 0;
803
mochel@digitalimplant.org36239572005-03-24 19:08:30 -0800804 klist_iter_init(&parent->klist_children, &i);
805 while ((child = next_device(&i)) && !error)
806 error = fn(child, data);
807 klist_iter_exit(&i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 return error;
809}
810
Cornelia Huck5ab69982006-11-16 15:42:07 +0100811/**
812 * device_find_child - device iterator for locating a particular device.
813 * @parent: parent struct device
814 * @data: Data to pass to match function
815 * @match: Callback function to check device
816 *
817 * This is similar to the device_for_each_child() function above, but it
818 * returns a reference to a device that is 'found' for later use, as
819 * determined by the @match callback.
820 *
821 * The callback should return 0 if the device doesn't match and non-zero
822 * if it does. If the callback returns non-zero and a reference to the
823 * current device can be obtained, this function will return to the caller
824 * and not iterate over any more devices.
825 */
826struct device * device_find_child(struct device *parent, void *data,
827 int (*match)(struct device *, void *))
828{
829 struct klist_iter i;
830 struct device *child;
831
832 if (!parent)
833 return NULL;
834
835 klist_iter_init(&parent->klist_children, &i);
836 while ((child = next_device(&i)))
837 if (match(child, data) && get_device(child))
838 break;
839 klist_iter_exit(&i);
840 return child;
841}
842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843int __init devices_init(void)
844{
845 return subsystem_register(&devices_subsys);
846}
847
848EXPORT_SYMBOL_GPL(device_for_each_child);
Cornelia Huck5ab69982006-11-16 15:42:07 +0100849EXPORT_SYMBOL_GPL(device_find_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
851EXPORT_SYMBOL_GPL(device_initialize);
852EXPORT_SYMBOL_GPL(device_add);
853EXPORT_SYMBOL_GPL(device_register);
854
855EXPORT_SYMBOL_GPL(device_del);
856EXPORT_SYMBOL_GPL(device_unregister);
857EXPORT_SYMBOL_GPL(get_device);
858EXPORT_SYMBOL_GPL(put_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
860EXPORT_SYMBOL_GPL(device_create_file);
861EXPORT_SYMBOL_GPL(device_remove_file);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700862
863
864static void device_create_release(struct device *dev)
865{
866 pr_debug("%s called for %s\n", __FUNCTION__, dev->bus_id);
867 kfree(dev);
868}
869
870/**
871 * device_create - creates a device and registers it with sysfs
Henrik Kretzschmar42734da2006-07-05 00:53:19 +0200872 * @class: pointer to the struct class that this device should be registered to
873 * @parent: pointer to the parent struct device of this new device, if any
874 * @devt: the dev_t for the char device to be added
875 * @fmt: string for the device's name
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700876 *
Henrik Kretzschmar42734da2006-07-05 00:53:19 +0200877 * This function can be used by char device classes. A struct device
878 * will be created in sysfs, registered to the specified class.
879 *
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700880 * A "dev" file will be created, showing the dev_t for the device, if
881 * the dev_t is not 0,0.
Henrik Kretzschmar42734da2006-07-05 00:53:19 +0200882 * If a pointer to a parent struct device is passed in, the newly created
883 * struct device will be a child of that device in sysfs.
884 * The pointer to the struct device will be returned from the call.
885 * Any further sysfs files that might be required can be created using this
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700886 * pointer.
887 *
888 * Note: the struct class passed to this function must have previously
889 * been created with a call to class_create().
890 */
891struct device *device_create(struct class *class, struct device *parent,
Greg Kroah-Hartman5cbe5f82006-08-10 01:19:19 -0400892 dev_t devt, const char *fmt, ...)
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700893{
894 va_list args;
895 struct device *dev = NULL;
896 int retval = -ENODEV;
897
898 if (class == NULL || IS_ERR(class))
899 goto error;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700900
901 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
902 if (!dev) {
903 retval = -ENOMEM;
904 goto error;
905 }
906
907 dev->devt = devt;
908 dev->class = class;
909 dev->parent = parent;
910 dev->release = device_create_release;
911
912 va_start(args, fmt);
913 vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args);
914 va_end(args);
915 retval = device_register(dev);
916 if (retval)
917 goto error;
918
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700919 return dev;
920
921error:
922 kfree(dev);
923 return ERR_PTR(retval);
924}
925EXPORT_SYMBOL_GPL(device_create);
926
927/**
928 * device_destroy - removes a device that was created with device_create()
Henrik Kretzschmar42734da2006-07-05 00:53:19 +0200929 * @class: pointer to the struct class that this device was registered with
930 * @devt: the dev_t of the device that was previously registered
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700931 *
Henrik Kretzschmar42734da2006-07-05 00:53:19 +0200932 * This call unregisters and cleans up a device that was created with a
933 * call to device_create().
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700934 */
935void device_destroy(struct class *class, dev_t devt)
936{
937 struct device *dev = NULL;
938 struct device *dev_tmp;
939
940 down(&class->sem);
941 list_for_each_entry(dev_tmp, &class->devices, node) {
942 if (dev_tmp->devt == devt) {
943 dev = dev_tmp;
944 break;
945 }
946 }
947 up(&class->sem);
948
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700949 if (dev)
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700950 device_unregister(dev);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700951}
952EXPORT_SYMBOL_GPL(device_destroy);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -0700953
954/**
955 * device_rename - renames a device
956 * @dev: the pointer to the struct device to be renamed
957 * @new_name: the new name of the device
958 */
959int device_rename(struct device *dev, char *new_name)
960{
961 char *old_class_name = NULL;
962 char *new_class_name = NULL;
963 char *old_symlink_name = NULL;
964 int error;
965
966 dev = get_device(dev);
967 if (!dev)
968 return -EINVAL;
969
970 pr_debug("DEVICE: renaming '%s' to '%s'\n", dev->bus_id, new_name);
971
Kay Sievers99ef3ef2006-09-14 11:23:28 +0200972#ifdef CONFIG_SYSFS_DEPRECATED
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -0700973 if ((dev->class) && (dev->parent))
974 old_class_name = make_class_name(dev->class->name, &dev->kobj);
Kay Sievers99ef3ef2006-09-14 11:23:28 +0200975#endif
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -0700976
977 if (dev->class) {
978 old_symlink_name = kmalloc(BUS_ID_SIZE, GFP_KERNEL);
Jesper Juhl952ab432006-09-28 23:56:01 +0200979 if (!old_symlink_name) {
980 error = -ENOMEM;
981 goto out_free_old_class;
982 }
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -0700983 strlcpy(old_symlink_name, dev->bus_id, BUS_ID_SIZE);
984 }
985
986 strlcpy(dev->bus_id, new_name, BUS_ID_SIZE);
987
988 error = kobject_rename(&dev->kobj, new_name);
989
Kay Sievers99ef3ef2006-09-14 11:23:28 +0200990#ifdef CONFIG_SYSFS_DEPRECATED
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -0700991 if (old_class_name) {
992 new_class_name = make_class_name(dev->class->name, &dev->kobj);
993 if (new_class_name) {
994 sysfs_create_link(&dev->parent->kobj, &dev->kobj,
995 new_class_name);
996 sysfs_remove_link(&dev->parent->kobj, old_class_name);
997 }
998 }
Kay Sievers99ef3ef2006-09-14 11:23:28 +0200999#endif
1000
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001001 if (dev->class) {
1002 sysfs_remove_link(&dev->class->subsys.kset.kobj,
1003 old_symlink_name);
1004 sysfs_create_link(&dev->class->subsys.kset.kobj, &dev->kobj,
1005 dev->bus_id);
1006 }
1007 put_device(dev);
1008
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001009 kfree(new_class_name);
1010 kfree(old_symlink_name);
Jesper Juhl952ab432006-09-28 23:56:01 +02001011 out_free_old_class:
1012 kfree(old_class_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001013
1014 return error;
1015}
Cornelia Huck8a824722006-11-20 17:07:51 +01001016
1017
1018static int device_move_class_links(struct device *dev,
1019 struct device *old_parent,
1020 struct device *new_parent)
1021{
1022#ifdef CONFIG_SYSFS_DEPRECATED
1023 int error;
1024 char *class_name;
1025
1026 class_name = make_class_name(dev->class->name, &dev->kobj);
1027 if (!class_name) {
Cornelia Huckcb360bb2006-11-27 10:35:05 +01001028 error = -ENOMEM;
Cornelia Huck8a824722006-11-20 17:07:51 +01001029 goto out;
1030 }
1031 if (old_parent) {
1032 sysfs_remove_link(&dev->kobj, "device");
1033 sysfs_remove_link(&old_parent->kobj, class_name);
1034 }
Cornelia Huckc744aeae2007-01-08 20:16:44 +01001035 if (new_parent) {
1036 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1037 "device");
1038 if (error)
1039 goto out;
1040 error = sysfs_create_link(&new_parent->kobj, &dev->kobj,
1041 class_name);
1042 if (error)
1043 sysfs_remove_link(&dev->kobj, "device");
1044 }
1045 else
1046 error = 0;
Cornelia Huck8a824722006-11-20 17:07:51 +01001047out:
1048 kfree(class_name);
1049 return error;
1050#else
1051 return 0;
1052#endif
1053}
1054
1055/**
1056 * device_move - moves a device to a new parent
1057 * @dev: the pointer to the struct device to be moved
Cornelia Huckc744aeae2007-01-08 20:16:44 +01001058 * @new_parent: the new parent of the device (can by NULL)
Cornelia Huck8a824722006-11-20 17:07:51 +01001059 */
1060int device_move(struct device *dev, struct device *new_parent)
1061{
1062 int error;
1063 struct device *old_parent;
Cornelia Huckc744aeae2007-01-08 20:16:44 +01001064 struct kobject *new_parent_kobj;
Cornelia Huck8a824722006-11-20 17:07:51 +01001065
1066 dev = get_device(dev);
1067 if (!dev)
1068 return -EINVAL;
1069
Cornelia Huck8a824722006-11-20 17:07:51 +01001070 new_parent = get_device(new_parent);
Cornelia Huckc744aeae2007-01-08 20:16:44 +01001071 new_parent_kobj = get_device_parent (dev, new_parent);
1072 if (IS_ERR(new_parent_kobj)) {
1073 error = PTR_ERR(new_parent_kobj);
1074 put_device(new_parent);
Cornelia Huck8a824722006-11-20 17:07:51 +01001075 goto out;
1076 }
1077 pr_debug("DEVICE: moving '%s' to '%s'\n", dev->bus_id,
Cornelia Huckc744aeae2007-01-08 20:16:44 +01001078 new_parent ? new_parent->bus_id : "<NULL>");
1079 error = kobject_move(&dev->kobj, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01001080 if (error) {
1081 put_device(new_parent);
1082 goto out;
1083 }
1084 old_parent = dev->parent;
1085 dev->parent = new_parent;
1086 if (old_parent)
Cornelia Huckacf02d22006-11-22 17:49:39 +01001087 klist_remove(&dev->knode_parent);
Cornelia Huckc744aeae2007-01-08 20:16:44 +01001088 if (new_parent)
1089 klist_add_tail(&dev->knode_parent, &new_parent->klist_children);
Cornelia Huck8a824722006-11-20 17:07:51 +01001090 if (!dev->class)
1091 goto out_put;
1092 error = device_move_class_links(dev, old_parent, new_parent);
1093 if (error) {
1094 /* We ignore errors on cleanup since we're hosed anyway... */
1095 device_move_class_links(dev, new_parent, old_parent);
1096 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
Cornelia Huckc744aeae2007-01-08 20:16:44 +01001097 if (new_parent)
1098 klist_remove(&dev->knode_parent);
Cornelia Huck8a824722006-11-20 17:07:51 +01001099 if (old_parent)
1100 klist_add_tail(&dev->knode_parent,
1101 &old_parent->klist_children);
1102 }
1103 put_device(new_parent);
1104 goto out;
1105 }
1106out_put:
1107 put_device(old_parent);
1108out:
1109 put_device(dev);
1110 return error;
1111}
1112
1113EXPORT_SYMBOL_GPL(device_move);