blob: ad0f4a2f25c466079c6c471f8b0f77a83b2845f0 [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/*
Andrew Mortonb446b602007-02-20 13:57:48 -080031 * Detect the LANANA-assigned LOCAL/EXPERIMENTAL majors
32 */
33bool is_lanana_major(unsigned int major)
34{
35 if (major >= 60 && major <= 63)
36 return 1;
37 if (major >= 120 && major <= 127)
38 return 1;
39 if (major >= 240 && major <= 254)
40 return 1;
41 return 0;
42}
43
44/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 * sysfs bindings for devices.
46 */
47
Alan Stern3e956372006-06-16 17:10:48 -040048/**
49 * dev_driver_string - Return a device's driver name, if at all possible
50 * @dev: struct device to get the name of
51 *
52 * Will return the device's driver's name if it is bound to a device. If
53 * the device is not bound to a device, it will return the name of the bus
54 * it is attached to. If it is not attached to a bus either, an empty
55 * string will be returned.
56 */
57const char *dev_driver_string(struct device *dev)
58{
59 return dev->driver ? dev->driver->name :
60 (dev->bus ? dev->bus->name : "");
61}
Matthew Wilcox310a9222006-09-23 23:35:04 -060062EXPORT_SYMBOL(dev_driver_string);
Alan Stern3e956372006-06-16 17:10:48 -040063
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#define to_dev(obj) container_of(obj, struct device, kobj)
65#define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static ssize_t
68dev_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
69{
70 struct device_attribute * dev_attr = to_dev_attr(attr);
71 struct device * dev = to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -050072 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74 if (dev_attr->show)
Yani Ioannou54b6f352005-05-17 06:39:34 -040075 ret = dev_attr->show(dev, dev_attr, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 return ret;
77}
78
79static ssize_t
80dev_attr_store(struct kobject * kobj, struct attribute * attr,
81 const char * buf, size_t count)
82{
83 struct device_attribute * dev_attr = to_dev_attr(attr);
84 struct device * dev = to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -050085 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87 if (dev_attr->store)
Yani Ioannou54b6f352005-05-17 06:39:34 -040088 ret = dev_attr->store(dev, dev_attr, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 return ret;
90}
91
92static struct sysfs_ops dev_sysfs_ops = {
93 .show = dev_attr_show,
94 .store = dev_attr_store,
95};
96
97
98/**
99 * device_release - free device structure.
100 * @kobj: device's kobject.
101 *
102 * This is called once the reference count for the object
103 * reaches 0. We forward the call to the device's release
104 * method, which should handle actually freeing the structure.
105 */
106static void device_release(struct kobject * kobj)
107{
108 struct device * dev = to_dev(kobj);
109
110 if (dev->release)
111 dev->release(dev);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200112 else if (dev->type && dev->type->release)
113 dev->type->release(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700114 else if (dev->class && dev->class->dev_release)
115 dev->class->dev_release(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 else {
117 printk(KERN_ERR "Device '%s' does not have a release() function, "
118 "it is broken and must be fixed.\n",
119 dev->bus_id);
120 WARN_ON(1);
121 }
122}
123
124static struct kobj_type ktype_device = {
125 .release = device_release,
126 .sysfs_ops = &dev_sysfs_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127};
128
129
Kay Sievers312c0042005-11-16 09:00:00 +0100130static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
132 struct kobj_type *ktype = get_ktype(kobj);
133
134 if (ktype == &ktype_device) {
135 struct device *dev = to_dev(kobj);
136 if (dev->bus)
137 return 1;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700138 if (dev->class)
139 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 }
141 return 0;
142}
143
Kay Sievers312c0042005-11-16 09:00:00 +0100144static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
146 struct device *dev = to_dev(kobj);
147
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700148 if (dev->bus)
149 return dev->bus->name;
150 if (dev->class)
151 return dev->class->name;
152 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
154
Kay Sievers312c0042005-11-16 09:00:00 +0100155static int dev_uevent(struct kset *kset, struct kobject *kobj, char **envp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 int num_envp, char *buffer, int buffer_size)
157{
158 struct device *dev = to_dev(kobj);
159 int i = 0;
160 int length = 0;
161 int retval = 0;
162
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700163 /* add the major/minor if present */
164 if (MAJOR(dev->devt)) {
165 add_uevent_var(envp, num_envp, &i,
166 buffer, buffer_size, &length,
167 "MAJOR=%u", MAJOR(dev->devt));
168 add_uevent_var(envp, num_envp, &i,
169 buffer, buffer_size, &length,
170 "MINOR=%u", MINOR(dev->devt));
171 }
172
Kay Sievers239378f2006-10-07 21:54:55 +0200173 if (dev->driver)
Kay Sieversd81d9d62006-08-13 06:17:09 +0200174 add_uevent_var(envp, num_envp, &i,
175 buffer, buffer_size, &length,
176 "DRIVER=%s", dev->driver->name);
Kay Sievers239378f2006-10-07 21:54:55 +0200177
Kay Sieversa87cb2a2006-09-14 11:23:28 +0200178#ifdef CONFIG_SYSFS_DEPRECATED
Kay Sievers239378f2006-10-07 21:54:55 +0200179 if (dev->class) {
180 struct device *parent = dev->parent;
181
182 /* find first bus device in parent chain */
183 while (parent && !parent->bus)
184 parent = parent->parent;
185 if (parent && parent->bus) {
186 const char *path;
187
188 path = kobject_get_path(&parent->kobj, GFP_KERNEL);
189 add_uevent_var(envp, num_envp, &i,
190 buffer, buffer_size, &length,
191 "PHYSDEVPATH=%s", path);
192 kfree(path);
193
194 add_uevent_var(envp, num_envp, &i,
195 buffer, buffer_size, &length,
196 "PHYSDEVBUS=%s", parent->bus->name);
197
198 if (parent->driver)
199 add_uevent_var(envp, num_envp, &i,
200 buffer, buffer_size, &length,
201 "PHYSDEVDRIVER=%s", parent->driver->name);
202 }
203 } else if (dev->bus) {
Kay Sievers312c0042005-11-16 09:00:00 +0100204 add_uevent_var(envp, num_envp, &i,
205 buffer, buffer_size, &length,
Kay Sievers239378f2006-10-07 21:54:55 +0200206 "PHYSDEVBUS=%s", dev->bus->name);
207
208 if (dev->driver)
209 add_uevent_var(envp, num_envp, &i,
210 buffer, buffer_size, &length,
211 "PHYSDEVDRIVER=%s", dev->driver->name);
Kay Sieversd81d9d62006-08-13 06:17:09 +0200212 }
Kay Sievers239378f2006-10-07 21:54:55 +0200213#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215 /* terminate, set to next free slot, shrink available space */
216 envp[i] = NULL;
217 envp = &envp[i];
218 num_envp -= i;
219 buffer = &buffer[length];
220 buffer_size -= length;
221
Kay Sievers312c0042005-11-16 09:00:00 +0100222 if (dev->bus && dev->bus->uevent) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 /* have the bus specific function add its stuff */
Kay Sievers312c0042005-11-16 09:00:00 +0100224 retval = dev->bus->uevent(dev, envp, num_envp, buffer, buffer_size);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200225 if (retval)
226 pr_debug ("%s: bus uevent() returned %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 __FUNCTION__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 }
229
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700230 if (dev->class && dev->class->dev_uevent) {
231 /* have the class specific function add its stuff */
232 retval = dev->class->dev_uevent(dev, envp, num_envp, buffer, buffer_size);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200233 if (retval)
234 pr_debug("%s: class uevent() returned %d\n",
235 __FUNCTION__, retval);
236 }
237
238 if (dev->type && dev->type->uevent) {
239 /* have the device type specific fuction add its stuff */
240 retval = dev->type->uevent(dev, envp, num_envp, buffer, buffer_size);
241 if (retval)
242 pr_debug("%s: dev_type uevent() returned %d\n",
243 __FUNCTION__, retval);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700244 }
245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 return retval;
247}
248
Kay Sievers312c0042005-11-16 09:00:00 +0100249static struct kset_uevent_ops device_uevent_ops = {
250 .filter = dev_uevent_filter,
251 .name = dev_uevent_name,
252 .uevent = dev_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253};
254
Kay Sieversa7fd6702005-10-01 14:49:43 +0200255static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
256 const char *buf, size_t count)
257{
Kay Sievers312c0042005-11-16 09:00:00 +0100258 kobject_uevent(&dev->kobj, KOBJ_ADD);
Kay Sieversa7fd6702005-10-01 14:49:43 +0200259 return count;
260}
261
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700262static int device_add_groups(struct device *dev)
263{
264 int i;
265 int error = 0;
266
267 if (dev->groups) {
268 for (i = 0; dev->groups[i]; i++) {
269 error = sysfs_create_group(&dev->kobj, dev->groups[i]);
270 if (error) {
271 while (--i >= 0)
272 sysfs_remove_group(&dev->kobj, dev->groups[i]);
273 goto out;
274 }
275 }
276 }
277out:
278 return error;
279}
280
281static void device_remove_groups(struct device *dev)
282{
283 int i;
284 if (dev->groups) {
285 for (i = 0; dev->groups[i]; i++) {
286 sysfs_remove_group(&dev->kobj, dev->groups[i]);
287 }
288 }
289}
290
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700291static int device_add_attrs(struct device *dev)
292{
293 struct class *class = dev->class;
Kay Sieversf9f852d2006-10-07 21:54:55 +0200294 struct device_type *type = dev->type;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700295 int error = 0;
296 int i;
297
Kay Sieversf9f852d2006-10-07 21:54:55 +0200298 if (class && class->dev_attrs) {
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700299 for (i = 0; attr_name(class->dev_attrs[i]); i++) {
300 error = device_create_file(dev, &class->dev_attrs[i]);
301 if (error)
302 break;
303 }
Kay Sieversf9f852d2006-10-07 21:54:55 +0200304 if (error)
305 while (--i >= 0)
306 device_remove_file(dev, &class->dev_attrs[i]);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700307 }
Kay Sieversf9f852d2006-10-07 21:54:55 +0200308
309 if (type && type->attrs) {
310 for (i = 0; attr_name(type->attrs[i]); i++) {
311 error = device_create_file(dev, &type->attrs[i]);
312 if (error)
313 break;
314 }
315 if (error)
316 while (--i >= 0)
317 device_remove_file(dev, &type->attrs[i]);
318 }
319
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700320 return error;
321}
322
323static void device_remove_attrs(struct device *dev)
324{
325 struct class *class = dev->class;
Kay Sieversf9f852d2006-10-07 21:54:55 +0200326 struct device_type *type = dev->type;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700327 int i;
328
Kay Sieversf9f852d2006-10-07 21:54:55 +0200329 if (class && class->dev_attrs) {
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700330 for (i = 0; attr_name(class->dev_attrs[i]); i++)
331 device_remove_file(dev, &class->dev_attrs[i]);
332 }
Kay Sieversf9f852d2006-10-07 21:54:55 +0200333
334 if (type && type->attrs) {
335 for (i = 0; attr_name(type->attrs[i]); i++)
336 device_remove_file(dev, &type->attrs[i]);
337 }
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700338}
339
340
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700341static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
342 char *buf)
343{
344 return print_dev_t(buf, dev->devt);
345}
346
Martin Waitz0863afb2006-01-09 20:53:55 -0800347/*
348 * devices_subsys - structure to be registered with kobject core.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 */
350
Kay Sievers312c0042005-11-16 09:00:00 +0100351decl_subsys(devices, &ktype_device, &device_uevent_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353
354/**
355 * device_create_file - create sysfs attribute file for device.
356 * @dev: device.
357 * @attr: device attribute descriptor.
358 */
359
360int device_create_file(struct device * dev, struct device_attribute * attr)
361{
362 int error = 0;
363 if (get_device(dev)) {
364 error = sysfs_create_file(&dev->kobj, &attr->attr);
365 put_device(dev);
366 }
367 return error;
368}
369
370/**
371 * device_remove_file - remove sysfs attribute file.
372 * @dev: device.
373 * @attr: device attribute descriptor.
374 */
375
376void device_remove_file(struct device * dev, struct device_attribute * attr)
377{
378 if (get_device(dev)) {
379 sysfs_remove_file(&dev->kobj, &attr->attr);
380 put_device(dev);
381 }
382}
383
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -0700384/**
385 * device_create_bin_file - create sysfs binary attribute file for device.
386 * @dev: device.
387 * @attr: device binary attribute descriptor.
388 */
389int device_create_bin_file(struct device *dev, struct bin_attribute *attr)
390{
391 int error = -EINVAL;
392 if (dev)
393 error = sysfs_create_bin_file(&dev->kobj, attr);
394 return error;
395}
396EXPORT_SYMBOL_GPL(device_create_bin_file);
397
398/**
399 * device_remove_bin_file - remove sysfs binary attribute file
400 * @dev: device.
401 * @attr: device binary attribute descriptor.
402 */
403void device_remove_bin_file(struct device *dev, struct bin_attribute *attr)
404{
405 if (dev)
406 sysfs_remove_bin_file(&dev->kobj, attr);
407}
408EXPORT_SYMBOL_GPL(device_remove_bin_file);
409
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400410/**
411 * device_schedule_callback - helper to schedule a callback for a device
412 * @dev: device.
413 * @func: callback function to invoke later.
414 *
415 * Attribute methods must not unregister themselves or their parent device
416 * (which would amount to the same thing). Attempts to do so will deadlock,
417 * since unregistration is mutually exclusive with driver callbacks.
418 *
419 * Instead methods can call this routine, which will attempt to allocate
420 * and schedule a workqueue request to call back @func with @dev as its
421 * argument in the workqueue's process context. @dev will be pinned until
422 * @func returns.
423 *
424 * Returns 0 if the request was submitted, -ENOMEM if storage could not
425 * be allocated.
426 *
427 * NOTE: This routine won't work if CONFIG_SYSFS isn't set! It uses an
428 * underlying sysfs routine (since it is intended for use by attribute
429 * methods), and if sysfs isn't available you'll get nothing but -ENOSYS.
430 */
431int device_schedule_callback(struct device *dev,
432 void (*func)(struct device *))
433{
434 return sysfs_schedule_callback(&dev->kobj,
435 (void (*)(void *)) func, dev);
436}
437EXPORT_SYMBOL_GPL(device_schedule_callback);
438
James Bottomley34bb61f2005-09-06 16:56:51 -0700439static void klist_children_get(struct klist_node *n)
440{
441 struct device *dev = container_of(n, struct device, knode_parent);
442
443 get_device(dev);
444}
445
446static void klist_children_put(struct klist_node *n)
447{
448 struct device *dev = container_of(n, struct device, knode_parent);
449
450 put_device(dev);
451}
452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454/**
455 * device_initialize - init device structure.
456 * @dev: device.
457 *
458 * This prepares the device for use by other layers,
459 * including adding it to the device hierarchy.
460 * It is the first half of device_register(), if called by
461 * that, though it can also be called separately, so one
462 * may use @dev's fields (e.g. the refcount).
463 */
464
465void device_initialize(struct device *dev)
466{
467 kobj_set_kset_s(dev, devices_subsys);
468 kobject_init(&dev->kobj);
James Bottomley34bb61f2005-09-06 16:56:51 -0700469 klist_init(&dev->klist_children, klist_children_get,
470 klist_children_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 INIT_LIST_HEAD(&dev->dma_pools);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700472 INIT_LIST_HEAD(&dev->node);
mochel@digitalimplant.orgaf703162005-03-21 10:41:04 -0800473 init_MUTEX(&dev->sem);
Tejun Heo9ac78492007-01-20 16:00:26 +0900474 spin_lock_init(&dev->devres_lock);
475 INIT_LIST_HEAD(&dev->devres_head);
David Brownell0ac85242005-09-12 19:39:34 -0700476 device_init_wakeup(dev, 0);
Christoph Hellwig87348132006-12-06 20:32:33 -0800477 set_dev_node(dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478}
479
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100480#ifdef CONFIG_SYSFS_DEPRECATED
Cornelia Huckc744aea2007-01-08 20:16:44 +0100481static struct kobject * get_device_parent(struct device *dev,
482 struct device *parent)
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100483{
484 /* Set the parent to the class, not the parent device */
485 /* this keeps sysfs from having a symlink to make old udevs happy */
486 if (dev->class)
Cornelia Huckc744aea2007-01-08 20:16:44 +0100487 return &dev->class->subsys.kset.kobj;
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100488 else if (parent)
Cornelia Huckc744aea2007-01-08 20:16:44 +0100489 return &parent->kobj;
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100490
Cornelia Huckc744aea2007-01-08 20:16:44 +0100491 return NULL;
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100492}
493#else
Cornelia Huckc744aea2007-01-08 20:16:44 +0100494static struct kobject * virtual_device_parent(struct device *dev)
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700495{
496 if (!dev->class)
Cornelia Huckc744aea2007-01-08 20:16:44 +0100497 return ERR_PTR(-ENODEV);
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700498
499 if (!dev->class->virtual_dir) {
500 static struct kobject *virtual_dir = NULL;
501
502 if (!virtual_dir)
503 virtual_dir = kobject_add_dir(&devices_subsys.kset.kobj, "virtual");
504 dev->class->virtual_dir = kobject_add_dir(virtual_dir, dev->class->name);
505 }
506
Cornelia Huckc744aea2007-01-08 20:16:44 +0100507 return dev->class->virtual_dir;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700508}
509
Cornelia Huckc744aea2007-01-08 20:16:44 +0100510static struct kobject * get_device_parent(struct device *dev,
511 struct device *parent)
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100512{
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100513 /* if this is a class device, and has no parent, create one */
514 if ((dev->class) && (parent == NULL)) {
Cornelia Huckc744aea2007-01-08 20:16:44 +0100515 return virtual_device_parent(dev);
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100516 } else if (parent)
Cornelia Huckc744aea2007-01-08 20:16:44 +0100517 return &parent->kobj;
518 return NULL;
519}
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100520
Cornelia Huckc744aea2007-01-08 20:16:44 +0100521#endif
522static int setup_parent(struct device *dev, struct device *parent)
523{
524 struct kobject *kobj;
525 kobj = get_device_parent(dev, parent);
526 if (IS_ERR(kobj))
527 return PTR_ERR(kobj);
528 if (kobj)
529 dev->kobj.parent = kobj;
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100530 return 0;
531}
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533/**
534 * device_add - add device to device hierarchy.
535 * @dev: device.
536 *
537 * This is part 2 of device_register(), though may be called
538 * separately _iff_ device_initialize() has been called separately.
539 *
540 * This adds it to the kobject hierarchy via kobject_add(), adds it
541 * to the global and sibling lists for the device, then
542 * adds it to the other relevant subsystems of the driver model.
543 */
544int device_add(struct device *dev)
545{
546 struct device *parent = NULL;
Greg Kroah-Hartmane9a7d302006-06-20 13:59:20 -0700547 char *class_name = NULL;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200548 struct class_interface *class_intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 int error = -EINVAL;
550
551 dev = get_device(dev);
552 if (!dev || !strlen(dev->bus_id))
553 goto Error;
554
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100555 pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id);
Greg Kroah-Hartmanc205ef42006-08-07 22:19:37 -0700556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 parent = get_device(dev->parent);
558
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100559 error = setup_parent(dev, parent);
560 if (error)
561 goto Error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
563 /* first, register with generic layer. */
564 kobject_set_name(&dev->kobj, "%s", dev->bus_id);
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100565 error = kobject_add(&dev->kobj);
566 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 goto Error;
Kay Sieversa7fd6702005-10-01 14:49:43 +0200568
Brian Walsh37022642006-08-14 22:43:19 -0700569 /* notify platform of device entry */
570 if (platform_notify)
571 platform_notify(dev);
572
Benjamin Herrenschmidt116af372006-10-25 13:44:59 +1000573 /* notify clients of device entry (new way) */
574 if (dev->bus)
575 blocking_notifier_call_chain(&dev->bus->bus_notifier,
576 BUS_NOTIFY_ADD_DEVICE, dev);
577
Kay Sieversa7fd6702005-10-01 14:49:43 +0200578 dev->uevent_attr.attr.name = "uevent";
579 dev->uevent_attr.attr.mode = S_IWUSR;
580 if (dev->driver)
581 dev->uevent_attr.attr.owner = dev->driver->owner;
582 dev->uevent_attr.store = store_uevent;
Cornelia Hucka306eea2006-09-22 11:37:13 +0200583 error = device_create_file(dev, &dev->uevent_attr);
584 if (error)
585 goto attrError;
Kay Sieversa7fd6702005-10-01 14:49:43 +0200586
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700587 if (MAJOR(dev->devt)) {
588 struct device_attribute *attr;
589 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
590 if (!attr) {
591 error = -ENOMEM;
Cornelia Hucka306eea2006-09-22 11:37:13 +0200592 goto ueventattrError;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700593 }
594 attr->attr.name = "dev";
595 attr->attr.mode = S_IRUGO;
596 if (dev->driver)
597 attr->attr.owner = dev->driver->owner;
598 attr->show = show_dev;
599 error = device_create_file(dev, attr);
600 if (error) {
601 kfree(attr);
Cornelia Hucka306eea2006-09-22 11:37:13 +0200602 goto ueventattrError;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700603 }
604
605 dev->devt_attr = attr;
606 }
607
Kay Sieversb9d9c822006-06-15 15:31:56 +0200608 if (dev->class) {
609 sysfs_create_link(&dev->kobj, &dev->class->subsys.kset.kobj,
610 "subsystem");
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100611 /* If this is not a "fake" compatible device, then create the
612 * symlink from the class to the device. */
613 if (dev->kobj.parent != &dev->class->subsys.kset.kobj)
614 sysfs_create_link(&dev->class->subsys.kset.kobj,
615 &dev->kobj, dev->bus_id);
Greg Kroah-Hartman64bb5d22006-06-28 16:19:58 -0700616 if (parent) {
Cornelia Huckcb360bb2006-11-27 10:35:05 +0100617 sysfs_create_link(&dev->kobj, &dev->parent->kobj,
618 "device");
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -0800619#ifdef CONFIG_SYSFS_DEPRECATED
Cornelia Huckcb360bb2006-11-27 10:35:05 +0100620 class_name = make_class_name(dev->class->name,
621 &dev->kobj);
622 if (class_name)
623 sysfs_create_link(&dev->parent->kobj,
624 &dev->kobj, class_name);
Kay Sievers99ef3ef2006-09-14 11:23:28 +0200625#endif
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -0800626 }
Kay Sieversb9d9c822006-06-15 15:31:56 +0200627 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700628
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700629 if ((error = device_add_attrs(dev)))
630 goto AttrsError;
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700631 if ((error = device_add_groups(dev)))
632 goto GroupError;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 if ((error = device_pm_add(dev)))
634 goto PMError;
635 if ((error = bus_add_device(dev)))
636 goto BusError;
Kay Sieversb7a3e812006-10-07 21:54:55 +0200637 if (!dev->uevent_suppress)
638 kobject_uevent(&dev->kobj, KOBJ_ADD);
Alan Sternf70fa622006-10-05 17:03:24 -0400639 if ((error = bus_attach_device(dev)))
640 goto AttachError;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 if (parent)
James Bottomleyd856f1e2005-08-19 09:14:01 -0400642 klist_add_tail(&dev->knode_parent, &parent->klist_children);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700644 if (dev->class) {
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700645 down(&dev->class->sem);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200646 /* tie the class to the device */
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700647 list_add_tail(&dev->node, &dev->class->devices);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200648
649 /* notify any interfaces that the device is here */
650 list_for_each_entry(class_intf, &dev->class->interfaces, node)
651 if (class_intf->add_dev)
652 class_intf->add_dev(dev, class_intf);
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700653 up(&dev->class->sem);
654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 Done:
Greg Kroah-Hartmane9a7d302006-06-20 13:59:20 -0700656 kfree(class_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 put_device(dev);
658 return error;
Alan Sternf70fa622006-10-05 17:03:24 -0400659 AttachError:
660 bus_remove_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 BusError:
662 device_pm_remove(dev);
663 PMError:
Benjamin Herrenschmidt116af372006-10-25 13:44:59 +1000664 if (dev->bus)
665 blocking_notifier_call_chain(&dev->bus->bus_notifier,
666 BUS_NOTIFY_DEL_DEVICE, dev);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700667 device_remove_groups(dev);
668 GroupError:
James Simmons82f0cf92007-02-21 17:44:51 +0000669 device_remove_attrs(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700670 AttrsError:
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700671 if (dev->devt_attr) {
672 device_remove_file(dev, dev->devt_attr);
673 kfree(dev->devt_attr);
674 }
James Simmons82f0cf92007-02-21 17:44:51 +0000675
676 if (dev->class) {
677 sysfs_remove_link(&dev->kobj, "subsystem");
678 /* If this is not a "fake" compatible device, remove the
679 * symlink from the class to the device. */
680 if (dev->kobj.parent != &dev->class->subsys.kset.kobj)
681 sysfs_remove_link(&dev->class->subsys.kset.kobj,
682 dev->bus_id);
James Simmons82f0cf92007-02-21 17:44:51 +0000683 if (parent) {
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -0800684#ifdef CONFIG_SYSFS_DEPRECATED
James Simmons82f0cf92007-02-21 17:44:51 +0000685 char *class_name = make_class_name(dev->class->name,
686 &dev->kobj);
687 if (class_name)
688 sysfs_remove_link(&dev->parent->kobj,
689 class_name);
690 kfree(class_name);
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -0800691#endif
James Simmons82f0cf92007-02-21 17:44:51 +0000692 sysfs_remove_link(&dev->kobj, "device");
693 }
James Simmons82f0cf92007-02-21 17:44:51 +0000694
695 down(&dev->class->sem);
696 /* notify any interfaces that the device is now gone */
697 list_for_each_entry(class_intf, &dev->class->interfaces, node)
698 if (class_intf->remove_dev)
699 class_intf->remove_dev(dev, class_intf);
700 /* remove the device from the class list */
701 list_del_init(&dev->node);
702 up(&dev->class->sem);
703 }
Cornelia Hucka306eea2006-09-22 11:37:13 +0200704 ueventattrError:
705 device_remove_file(dev, &dev->uevent_attr);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700706 attrError:
Kay Sievers312c0042005-11-16 09:00:00 +0100707 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 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
728int 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
744struct 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 */
754void 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
775void device_del(struct device * dev)
776{
777 struct device * parent = dev->parent;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200778 struct class_interface *class_intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 if (parent)
Patrick Mocheld62c0f92005-06-24 08:39:33 -0700781 klist_del(&dev->knode_parent);
Catalin Marinas82189b92006-11-25 11:09:30 -0800782 if (dev->devt_attr) {
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700783 device_remove_file(dev, dev->devt_attr);
Catalin Marinas82189b92006-11-25 11:09:30 -0800784 kfree(dev->devt_attr);
785 }
Kay Sieversb9d9c822006-06-15 15:31:56 +0200786 if (dev->class) {
787 sysfs_remove_link(&dev->kobj, "subsystem");
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100788 /* 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);
Greg Kroah-Hartman64bb5d22006-06-28 16:19:58 -0700793 if (parent) {
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -0800794#ifdef CONFIG_SYSFS_DEPRECATED
Kay Sievers99ef3ef2006-09-14 11:23:28 +0200795 char *class_name = make_class_name(dev->class->name,
796 &dev->kobj);
Cornelia Huckcb360bb2006-11-27 10:35:05 +0100797 if (class_name)
798 sysfs_remove_link(&dev->parent->kobj,
799 class_name);
Kay Sievers99ef3ef2006-09-14 11:23:28 +0200800 kfree(class_name);
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -0800801#endif
Kay Sievers99ef3ef2006-09-14 11:23:28 +0200802 sysfs_remove_link(&dev->kobj, "device");
Greg Kroah-Hartman64bb5d22006-06-28 16:19:58 -0700803 }
Kay Sievers99ef3ef2006-09-14 11:23:28 +0200804
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700805 down(&dev->class->sem);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200806 /* 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 */
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700811 list_del_init(&dev->node);
812 up(&dev->class->sem);
Kay Sieversb9d9c822006-06-15 15:31:56 +0200813 }
Kay Sieversa7fd6702005-10-01 14:49:43 +0200814 device_remove_file(dev, &dev->uevent_attr);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700815 device_remove_groups(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700816 device_remove_attrs(dev);
Benjamin Herrenschmidt28953532006-11-08 19:46:14 -0800817 bus_remove_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Tejun Heo2f8d16a2007-03-09 19:34:19 +0900819 /*
820 * Some platform devices are driven without driver attached
821 * and managed resources may have been acquired. Make sure
822 * all resources are released.
823 */
824 devres_release_all(dev);
825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 /* Notify the platform of the removal, in case they
827 * need to do anything...
828 */
829 if (platform_notify_remove)
830 platform_notify_remove(dev);
Benjamin Herrenschmidt116af372006-10-25 13:44:59 +1000831 if (dev->bus)
832 blocking_notifier_call_chain(&dev->bus->bus_notifier,
833 BUS_NOTIFY_DEL_DEVICE, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 device_pm_remove(dev);
Kay Sievers312c0042005-11-16 09:00:00 +0100835 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 kobject_del(&dev->kobj);
837 if (parent)
838 put_device(parent);
839}
840
841/**
842 * device_unregister - unregister device from system.
843 * @dev: device going away.
844 *
845 * We do this in two parts, like we do device_register(). First,
846 * we remove it from all the subsystems with device_del(), then
847 * we decrement the reference count via put_device(). If that
848 * is the final reference count, the device will be cleaned up
849 * via device_release() above. Otherwise, the structure will
850 * stick around until the final reference to the device is dropped.
851 */
852void device_unregister(struct device * dev)
853{
854 pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id);
855 device_del(dev);
856 put_device(dev);
857}
858
859
mochel@digitalimplant.org36239572005-03-24 19:08:30 -0800860static struct device * next_device(struct klist_iter * i)
861{
862 struct klist_node * n = klist_next(i);
863 return n ? container_of(n, struct device, knode_parent) : NULL;
864}
865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866/**
867 * device_for_each_child - device child iterator.
Randy Dunlapc41455f2005-10-23 11:59:14 -0700868 * @parent: parent struct device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 * @data: data for the callback.
870 * @fn: function to be called for each device.
871 *
Randy Dunlapc41455f2005-10-23 11:59:14 -0700872 * Iterate over @parent's child devices, and call @fn for each,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 * passing it @data.
874 *
875 * We check the return of @fn each time. If it returns anything
876 * other than 0, we break out and return that value.
877 */
mochel@digitalimplant.org36239572005-03-24 19:08:30 -0800878int device_for_each_child(struct device * parent, void * data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 int (*fn)(struct device *, void *))
880{
mochel@digitalimplant.org36239572005-03-24 19:08:30 -0800881 struct klist_iter i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 struct device * child;
883 int error = 0;
884
mochel@digitalimplant.org36239572005-03-24 19:08:30 -0800885 klist_iter_init(&parent->klist_children, &i);
886 while ((child = next_device(&i)) && !error)
887 error = fn(child, data);
888 klist_iter_exit(&i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 return error;
890}
891
Cornelia Huck5ab69982006-11-16 15:42:07 +0100892/**
893 * device_find_child - device iterator for locating a particular device.
894 * @parent: parent struct device
895 * @data: Data to pass to match function
896 * @match: Callback function to check device
897 *
898 * This is similar to the device_for_each_child() function above, but it
899 * returns a reference to a device that is 'found' for later use, as
900 * determined by the @match callback.
901 *
902 * The callback should return 0 if the device doesn't match and non-zero
903 * if it does. If the callback returns non-zero and a reference to the
904 * current device can be obtained, this function will return to the caller
905 * and not iterate over any more devices.
906 */
907struct device * device_find_child(struct device *parent, void *data,
908 int (*match)(struct device *, void *))
909{
910 struct klist_iter i;
911 struct device *child;
912
913 if (!parent)
914 return NULL;
915
916 klist_iter_init(&parent->klist_children, &i);
917 while ((child = next_device(&i)))
918 if (match(child, data) && get_device(child))
919 break;
920 klist_iter_exit(&i);
921 return child;
922}
923
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924int __init devices_init(void)
925{
926 return subsystem_register(&devices_subsys);
927}
928
929EXPORT_SYMBOL_GPL(device_for_each_child);
Cornelia Huck5ab69982006-11-16 15:42:07 +0100930EXPORT_SYMBOL_GPL(device_find_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
932EXPORT_SYMBOL_GPL(device_initialize);
933EXPORT_SYMBOL_GPL(device_add);
934EXPORT_SYMBOL_GPL(device_register);
935
936EXPORT_SYMBOL_GPL(device_del);
937EXPORT_SYMBOL_GPL(device_unregister);
938EXPORT_SYMBOL_GPL(get_device);
939EXPORT_SYMBOL_GPL(put_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
941EXPORT_SYMBOL_GPL(device_create_file);
942EXPORT_SYMBOL_GPL(device_remove_file);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700943
944
945static void device_create_release(struct device *dev)
946{
947 pr_debug("%s called for %s\n", __FUNCTION__, dev->bus_id);
948 kfree(dev);
949}
950
951/**
952 * device_create - creates a device and registers it with sysfs
Henrik Kretzschmar42734da2006-07-05 00:53:19 +0200953 * @class: pointer to the struct class that this device should be registered to
954 * @parent: pointer to the parent struct device of this new device, if any
955 * @devt: the dev_t for the char device to be added
956 * @fmt: string for the device's name
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700957 *
Henrik Kretzschmar42734da2006-07-05 00:53:19 +0200958 * This function can be used by char device classes. A struct device
959 * will be created in sysfs, registered to the specified class.
960 *
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700961 * A "dev" file will be created, showing the dev_t for the device, if
962 * the dev_t is not 0,0.
Henrik Kretzschmar42734da2006-07-05 00:53:19 +0200963 * If a pointer to a parent struct device is passed in, the newly created
964 * struct device will be a child of that device in sysfs.
965 * The pointer to the struct device will be returned from the call.
966 * Any further sysfs files that might be required can be created using this
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700967 * pointer.
968 *
969 * Note: the struct class passed to this function must have previously
970 * been created with a call to class_create().
971 */
972struct device *device_create(struct class *class, struct device *parent,
Greg Kroah-Hartman5cbe5f82006-08-10 01:19:19 -0400973 dev_t devt, const char *fmt, ...)
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700974{
975 va_list args;
976 struct device *dev = NULL;
977 int retval = -ENODEV;
978
979 if (class == NULL || IS_ERR(class))
980 goto error;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700981
982 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
983 if (!dev) {
984 retval = -ENOMEM;
985 goto error;
986 }
987
988 dev->devt = devt;
989 dev->class = class;
990 dev->parent = parent;
991 dev->release = device_create_release;
992
993 va_start(args, fmt);
994 vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args);
995 va_end(args);
996 retval = device_register(dev);
997 if (retval)
998 goto error;
999
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001000 return dev;
1001
1002error:
1003 kfree(dev);
1004 return ERR_PTR(retval);
1005}
1006EXPORT_SYMBOL_GPL(device_create);
1007
1008/**
1009 * device_destroy - removes a device that was created with device_create()
Henrik Kretzschmar42734da2006-07-05 00:53:19 +02001010 * @class: pointer to the struct class that this device was registered with
1011 * @devt: the dev_t of the device that was previously registered
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001012 *
Henrik Kretzschmar42734da2006-07-05 00:53:19 +02001013 * This call unregisters and cleans up a device that was created with a
1014 * call to device_create().
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001015 */
1016void device_destroy(struct class *class, dev_t devt)
1017{
1018 struct device *dev = NULL;
1019 struct device *dev_tmp;
1020
1021 down(&class->sem);
1022 list_for_each_entry(dev_tmp, &class->devices, node) {
1023 if (dev_tmp->devt == devt) {
1024 dev = dev_tmp;
1025 break;
1026 }
1027 }
1028 up(&class->sem);
1029
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -07001030 if (dev)
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001031 device_unregister(dev);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001032}
1033EXPORT_SYMBOL_GPL(device_destroy);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001034
1035/**
1036 * device_rename - renames a device
1037 * @dev: the pointer to the struct device to be renamed
1038 * @new_name: the new name of the device
1039 */
1040int device_rename(struct device *dev, char *new_name)
1041{
1042 char *old_class_name = NULL;
1043 char *new_class_name = NULL;
1044 char *old_symlink_name = NULL;
1045 int error;
1046
1047 dev = get_device(dev);
1048 if (!dev)
1049 return -EINVAL;
1050
1051 pr_debug("DEVICE: renaming '%s' to '%s'\n", dev->bus_id, new_name);
1052
Kay Sievers99ef3ef2006-09-14 11:23:28 +02001053#ifdef CONFIG_SYSFS_DEPRECATED
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001054 if ((dev->class) && (dev->parent))
1055 old_class_name = make_class_name(dev->class->name, &dev->kobj);
Kay Sievers99ef3ef2006-09-14 11:23:28 +02001056#endif
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001057
1058 if (dev->class) {
1059 old_symlink_name = kmalloc(BUS_ID_SIZE, GFP_KERNEL);
Jesper Juhl952ab432006-09-28 23:56:01 +02001060 if (!old_symlink_name) {
1061 error = -ENOMEM;
1062 goto out_free_old_class;
1063 }
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001064 strlcpy(old_symlink_name, dev->bus_id, BUS_ID_SIZE);
1065 }
1066
1067 strlcpy(dev->bus_id, new_name, BUS_ID_SIZE);
1068
1069 error = kobject_rename(&dev->kobj, new_name);
1070
Kay Sievers99ef3ef2006-09-14 11:23:28 +02001071#ifdef CONFIG_SYSFS_DEPRECATED
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001072 if (old_class_name) {
1073 new_class_name = make_class_name(dev->class->name, &dev->kobj);
1074 if (new_class_name) {
1075 sysfs_create_link(&dev->parent->kobj, &dev->kobj,
1076 new_class_name);
1077 sysfs_remove_link(&dev->parent->kobj, old_class_name);
1078 }
1079 }
Kay Sievers99ef3ef2006-09-14 11:23:28 +02001080#endif
1081
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001082 if (dev->class) {
1083 sysfs_remove_link(&dev->class->subsys.kset.kobj,
1084 old_symlink_name);
1085 sysfs_create_link(&dev->class->subsys.kset.kobj, &dev->kobj,
1086 dev->bus_id);
1087 }
1088 put_device(dev);
1089
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001090 kfree(new_class_name);
1091 kfree(old_symlink_name);
Jesper Juhl952ab432006-09-28 23:56:01 +02001092 out_free_old_class:
1093 kfree(old_class_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001094
1095 return error;
1096}
Johannes Berga2807db2007-02-28 12:38:31 +01001097EXPORT_SYMBOL_GPL(device_rename);
Cornelia Huck8a824722006-11-20 17:07:51 +01001098
1099static int device_move_class_links(struct device *dev,
1100 struct device *old_parent,
1101 struct device *new_parent)
1102{
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08001103 int error = 0;
Cornelia Huck8a824722006-11-20 17:07:51 +01001104#ifdef CONFIG_SYSFS_DEPRECATED
Cornelia Huck8a824722006-11-20 17:07:51 +01001105 char *class_name;
1106
1107 class_name = make_class_name(dev->class->name, &dev->kobj);
1108 if (!class_name) {
Cornelia Huckcb360bb2006-11-27 10:35:05 +01001109 error = -ENOMEM;
Cornelia Huck8a824722006-11-20 17:07:51 +01001110 goto out;
1111 }
1112 if (old_parent) {
1113 sysfs_remove_link(&dev->kobj, "device");
1114 sysfs_remove_link(&old_parent->kobj, class_name);
1115 }
Cornelia Huckc744aea2007-01-08 20:16:44 +01001116 if (new_parent) {
1117 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1118 "device");
1119 if (error)
1120 goto out;
1121 error = sysfs_create_link(&new_parent->kobj, &dev->kobj,
1122 class_name);
1123 if (error)
1124 sysfs_remove_link(&dev->kobj, "device");
1125 }
1126 else
1127 error = 0;
Cornelia Huck8a824722006-11-20 17:07:51 +01001128out:
1129 kfree(class_name);
1130 return error;
1131#else
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08001132 if (old_parent)
1133 sysfs_remove_link(&dev->kobj, "device");
1134 if (new_parent)
1135 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1136 "device");
1137 return error;
Cornelia Huck8a824722006-11-20 17:07:51 +01001138#endif
1139}
1140
1141/**
1142 * device_move - moves a device to a new parent
1143 * @dev: the pointer to the struct device to be moved
Cornelia Huckc744aea2007-01-08 20:16:44 +01001144 * @new_parent: the new parent of the device (can by NULL)
Cornelia Huck8a824722006-11-20 17:07:51 +01001145 */
1146int device_move(struct device *dev, struct device *new_parent)
1147{
1148 int error;
1149 struct device *old_parent;
Cornelia Huckc744aea2007-01-08 20:16:44 +01001150 struct kobject *new_parent_kobj;
Cornelia Huck8a824722006-11-20 17:07:51 +01001151
1152 dev = get_device(dev);
1153 if (!dev)
1154 return -EINVAL;
1155
Cornelia Huck8a824722006-11-20 17:07:51 +01001156 new_parent = get_device(new_parent);
Cornelia Huckc744aea2007-01-08 20:16:44 +01001157 new_parent_kobj = get_device_parent (dev, new_parent);
1158 if (IS_ERR(new_parent_kobj)) {
1159 error = PTR_ERR(new_parent_kobj);
1160 put_device(new_parent);
Cornelia Huck8a824722006-11-20 17:07:51 +01001161 goto out;
1162 }
1163 pr_debug("DEVICE: moving '%s' to '%s'\n", dev->bus_id,
Cornelia Huckc744aea2007-01-08 20:16:44 +01001164 new_parent ? new_parent->bus_id : "<NULL>");
1165 error = kobject_move(&dev->kobj, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01001166 if (error) {
1167 put_device(new_parent);
1168 goto out;
1169 }
1170 old_parent = dev->parent;
1171 dev->parent = new_parent;
1172 if (old_parent)
Cornelia Huckacf02d22006-11-22 17:49:39 +01001173 klist_remove(&dev->knode_parent);
Cornelia Huckc744aea2007-01-08 20:16:44 +01001174 if (new_parent)
1175 klist_add_tail(&dev->knode_parent, &new_parent->klist_children);
Cornelia Huck8a824722006-11-20 17:07:51 +01001176 if (!dev->class)
1177 goto out_put;
1178 error = device_move_class_links(dev, old_parent, new_parent);
1179 if (error) {
1180 /* We ignore errors on cleanup since we're hosed anyway... */
1181 device_move_class_links(dev, new_parent, old_parent);
1182 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
Cornelia Huckc744aea2007-01-08 20:16:44 +01001183 if (new_parent)
1184 klist_remove(&dev->knode_parent);
Cornelia Huck8a824722006-11-20 17:07:51 +01001185 if (old_parent)
1186 klist_add_tail(&dev->knode_parent,
1187 &old_parent->klist_children);
1188 }
1189 put_device(new_parent);
1190 goto out;
1191 }
1192out_put:
1193 put_device(old_parent);
1194out:
1195 put_device(dev);
1196 return error;
1197}
1198
1199EXPORT_SYMBOL_GPL(device_move);