blob: e21a65fc043e9d20cb899a0f231db59cc0c761fe [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21#include <asm/semaphore.h>
22
23#include "base.h"
24#include "power/power.h"
25
26int (*platform_notify)(struct device * dev) = NULL;
27int (*platform_notify_remove)(struct device * dev) = NULL;
28
29/*
30 * sysfs bindings for devices.
31 */
32
Alan Stern3e956372006-06-16 17:10:48 -040033/**
34 * dev_driver_string - Return a device's driver name, if at all possible
35 * @dev: struct device to get the name of
36 *
37 * Will return the device's driver's name if it is bound to a device. If
38 * the device is not bound to a device, it will return the name of the bus
39 * it is attached to. If it is not attached to a bus either, an empty
40 * string will be returned.
41 */
42const char *dev_driver_string(struct device *dev)
43{
44 return dev->driver ? dev->driver->name :
45 (dev->bus ? dev->bus->name : "");
46}
47EXPORT_SYMBOL_GPL(dev_driver_string);
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#define to_dev(obj) container_of(obj, struct device, kobj)
50#define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052static ssize_t
53dev_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
54{
55 struct device_attribute * dev_attr = to_dev_attr(attr);
56 struct device * dev = to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -050057 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59 if (dev_attr->show)
Yani Ioannou54b6f352005-05-17 06:39:34 -040060 ret = dev_attr->show(dev, dev_attr, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 return ret;
62}
63
64static ssize_t
65dev_attr_store(struct kobject * kobj, struct attribute * attr,
66 const char * buf, size_t count)
67{
68 struct device_attribute * dev_attr = to_dev_attr(attr);
69 struct device * dev = to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -050070 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72 if (dev_attr->store)
Yani Ioannou54b6f352005-05-17 06:39:34 -040073 ret = dev_attr->store(dev, dev_attr, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 return ret;
75}
76
77static struct sysfs_ops dev_sysfs_ops = {
78 .show = dev_attr_show,
79 .store = dev_attr_store,
80};
81
82
83/**
84 * device_release - free device structure.
85 * @kobj: device's kobject.
86 *
87 * This is called once the reference count for the object
88 * reaches 0. We forward the call to the device's release
89 * method, which should handle actually freeing the structure.
90 */
91static void device_release(struct kobject * kobj)
92{
93 struct device * dev = to_dev(kobj);
94
95 if (dev->release)
96 dev->release(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -070097 else if (dev->class && dev->class->dev_release)
98 dev->class->dev_release(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 else {
100 printk(KERN_ERR "Device '%s' does not have a release() function, "
101 "it is broken and must be fixed.\n",
102 dev->bus_id);
103 WARN_ON(1);
104 }
105}
106
107static struct kobj_type ktype_device = {
108 .release = device_release,
109 .sysfs_ops = &dev_sysfs_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110};
111
112
Kay Sievers312c0042005-11-16 09:00:00 +0100113static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
115 struct kobj_type *ktype = get_ktype(kobj);
116
117 if (ktype == &ktype_device) {
118 struct device *dev = to_dev(kobj);
119 if (dev->bus)
120 return 1;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700121 if (dev->class)
122 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 }
124 return 0;
125}
126
Kay Sievers312c0042005-11-16 09:00:00 +0100127static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
129 struct device *dev = to_dev(kobj);
130
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700131 if (dev->bus)
132 return dev->bus->name;
133 if (dev->class)
134 return dev->class->name;
135 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136}
137
Kay Sievers312c0042005-11-16 09:00:00 +0100138static int dev_uevent(struct kset *kset, struct kobject *kobj, char **envp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 int num_envp, char *buffer, int buffer_size)
140{
141 struct device *dev = to_dev(kobj);
142 int i = 0;
143 int length = 0;
144 int retval = 0;
145
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700146 /* add the major/minor if present */
147 if (MAJOR(dev->devt)) {
148 add_uevent_var(envp, num_envp, &i,
149 buffer, buffer_size, &length,
150 "MAJOR=%u", MAJOR(dev->devt));
151 add_uevent_var(envp, num_envp, &i,
152 buffer, buffer_size, &length,
153 "MINOR=%u", MINOR(dev->devt));
154 }
155
Kay Sieversd81d9d62006-08-13 06:17:09 +0200156 /* add bus name (same as SUBSYSTEM, deprecated) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 if (dev->bus)
Kay Sievers312c0042005-11-16 09:00:00 +0100158 add_uevent_var(envp, num_envp, &i,
159 buffer, buffer_size, &length,
160 "PHYSDEVBUS=%s", dev->bus->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Kay Sieversd81d9d62006-08-13 06:17:09 +0200162 /* add driver name (PHYSDEV* values are deprecated)*/
163 if (dev->driver) {
164 add_uevent_var(envp, num_envp, &i,
165 buffer, buffer_size, &length,
166 "DRIVER=%s", dev->driver->name);
Kay Sievers312c0042005-11-16 09:00:00 +0100167 add_uevent_var(envp, num_envp, &i,
168 buffer, buffer_size, &length,
169 "PHYSDEVDRIVER=%s", dev->driver->name);
Kay Sieversd81d9d62006-08-13 06:17:09 +0200170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172 /* terminate, set to next free slot, shrink available space */
173 envp[i] = NULL;
174 envp = &envp[i];
175 num_envp -= i;
176 buffer = &buffer[length];
177 buffer_size -= length;
178
Kay Sievers312c0042005-11-16 09:00:00 +0100179 if (dev->bus && dev->bus->uevent) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 /* have the bus specific function add its stuff */
Kay Sievers312c0042005-11-16 09:00:00 +0100181 retval = dev->bus->uevent(dev, envp, num_envp, buffer, buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 if (retval) {
Kay Sievers312c0042005-11-16 09:00:00 +0100183 pr_debug ("%s - uevent() returned %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 __FUNCTION__, retval);
185 }
186 }
187
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700188 if (dev->class && dev->class->dev_uevent) {
189 /* have the class specific function add its stuff */
190 retval = dev->class->dev_uevent(dev, envp, num_envp, buffer, buffer_size);
191 if (retval) {
192 pr_debug("%s - dev_uevent() returned %d\n",
193 __FUNCTION__, retval);
194 }
195 }
196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 return retval;
198}
199
Kay Sievers312c0042005-11-16 09:00:00 +0100200static struct kset_uevent_ops device_uevent_ops = {
201 .filter = dev_uevent_filter,
202 .name = dev_uevent_name,
203 .uevent = dev_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204};
205
Kay Sieversa7fd6702005-10-01 14:49:43 +0200206static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
207 const char *buf, size_t count)
208{
Kay Sievers312c0042005-11-16 09:00:00 +0100209 kobject_uevent(&dev->kobj, KOBJ_ADD);
Kay Sieversa7fd6702005-10-01 14:49:43 +0200210 return count;
211}
212
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700213static int device_add_groups(struct device *dev)
214{
215 int i;
216 int error = 0;
217
218 if (dev->groups) {
219 for (i = 0; dev->groups[i]; i++) {
220 error = sysfs_create_group(&dev->kobj, dev->groups[i]);
221 if (error) {
222 while (--i >= 0)
223 sysfs_remove_group(&dev->kobj, dev->groups[i]);
224 goto out;
225 }
226 }
227 }
228out:
229 return error;
230}
231
232static void device_remove_groups(struct device *dev)
233{
234 int i;
235 if (dev->groups) {
236 for (i = 0; dev->groups[i]; i++) {
237 sysfs_remove_group(&dev->kobj, dev->groups[i]);
238 }
239 }
240}
241
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700242static int device_add_attrs(struct device *dev)
243{
244 struct class *class = dev->class;
245 int error = 0;
246 int i;
247
248 if (!class)
249 return 0;
250
251 if (class->dev_attrs) {
252 for (i = 0; attr_name(class->dev_attrs[i]); i++) {
253 error = device_create_file(dev, &class->dev_attrs[i]);
254 if (error)
255 break;
256 }
257 }
258 if (error)
259 while (--i >= 0)
260 device_remove_file(dev, &class->dev_attrs[i]);
261 return error;
262}
263
264static void device_remove_attrs(struct device *dev)
265{
266 struct class *class = dev->class;
267 int i;
268
269 if (!class)
270 return;
271
272 if (class->dev_attrs) {
273 for (i = 0; attr_name(class->dev_attrs[i]); i++)
274 device_remove_file(dev, &class->dev_attrs[i]);
275 }
276}
277
278
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700279static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
280 char *buf)
281{
282 return print_dev_t(buf, dev->devt);
283}
284
Martin Waitz0863afb2006-01-09 20:53:55 -0800285/*
286 * devices_subsys - structure to be registered with kobject core.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 */
288
Kay Sievers312c0042005-11-16 09:00:00 +0100289decl_subsys(devices, &ktype_device, &device_uevent_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291
292/**
293 * device_create_file - create sysfs attribute file for device.
294 * @dev: device.
295 * @attr: device attribute descriptor.
296 */
297
298int device_create_file(struct device * dev, struct device_attribute * attr)
299{
300 int error = 0;
301 if (get_device(dev)) {
302 error = sysfs_create_file(&dev->kobj, &attr->attr);
303 put_device(dev);
304 }
305 return error;
306}
307
308/**
309 * device_remove_file - remove sysfs attribute file.
310 * @dev: device.
311 * @attr: device attribute descriptor.
312 */
313
314void device_remove_file(struct device * dev, struct device_attribute * attr)
315{
316 if (get_device(dev)) {
317 sysfs_remove_file(&dev->kobj, &attr->attr);
318 put_device(dev);
319 }
320}
321
James Bottomley34bb61f2005-09-06 16:56:51 -0700322static void klist_children_get(struct klist_node *n)
323{
324 struct device *dev = container_of(n, struct device, knode_parent);
325
326 get_device(dev);
327}
328
329static void klist_children_put(struct klist_node *n)
330{
331 struct device *dev = container_of(n, struct device, knode_parent);
332
333 put_device(dev);
334}
335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337/**
338 * device_initialize - init device structure.
339 * @dev: device.
340 *
341 * This prepares the device for use by other layers,
342 * including adding it to the device hierarchy.
343 * It is the first half of device_register(), if called by
344 * that, though it can also be called separately, so one
345 * may use @dev's fields (e.g. the refcount).
346 */
347
348void device_initialize(struct device *dev)
349{
350 kobj_set_kset_s(dev, devices_subsys);
351 kobject_init(&dev->kobj);
James Bottomley34bb61f2005-09-06 16:56:51 -0700352 klist_init(&dev->klist_children, klist_children_get,
353 klist_children_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 INIT_LIST_HEAD(&dev->dma_pools);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700355 INIT_LIST_HEAD(&dev->node);
mochel@digitalimplant.orgaf703162005-03-21 10:41:04 -0800356 init_MUTEX(&dev->sem);
David Brownell0ac85242005-09-12 19:39:34 -0700357 device_init_wakeup(dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358}
359
360/**
361 * device_add - add device to device hierarchy.
362 * @dev: device.
363 *
364 * This is part 2 of device_register(), though may be called
365 * separately _iff_ device_initialize() has been called separately.
366 *
367 * This adds it to the kobject hierarchy via kobject_add(), adds it
368 * to the global and sibling lists for the device, then
369 * adds it to the other relevant subsystems of the driver model.
370 */
371int device_add(struct device *dev)
372{
373 struct device *parent = NULL;
Greg Kroah-Hartmane9a7d302006-06-20 13:59:20 -0700374 char *class_name = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 int error = -EINVAL;
376
377 dev = get_device(dev);
378 if (!dev || !strlen(dev->bus_id))
379 goto Error;
380
Greg Kroah-Hartmanc205ef42006-08-07 22:19:37 -0700381 /* if this is a class device, and has no parent, create one */
382 if ((dev->class) && (dev->parent == NULL)) {
383 error = virtual_device_parent(dev);
384 if (error)
385 goto Error;
386 }
387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 parent = get_device(dev->parent);
389
390 pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id);
391
392 /* first, register with generic layer. */
393 kobject_set_name(&dev->kobj, "%s", dev->bus_id);
394 if (parent)
395 dev->kobj.parent = &parent->kobj;
396
397 if ((error = kobject_add(&dev->kobj)))
398 goto Error;
Kay Sieversa7fd6702005-10-01 14:49:43 +0200399
400 dev->uevent_attr.attr.name = "uevent";
401 dev->uevent_attr.attr.mode = S_IWUSR;
402 if (dev->driver)
403 dev->uevent_attr.attr.owner = dev->driver->owner;
404 dev->uevent_attr.store = store_uevent;
405 device_create_file(dev, &dev->uevent_attr);
406
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700407 if (MAJOR(dev->devt)) {
408 struct device_attribute *attr;
409 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
410 if (!attr) {
411 error = -ENOMEM;
412 goto PMError;
413 }
414 attr->attr.name = "dev";
415 attr->attr.mode = S_IRUGO;
416 if (dev->driver)
417 attr->attr.owner = dev->driver->owner;
418 attr->show = show_dev;
419 error = device_create_file(dev, attr);
420 if (error) {
421 kfree(attr);
422 goto attrError;
423 }
424
425 dev->devt_attr = attr;
426 }
427
Kay Sieversb9d9c822006-06-15 15:31:56 +0200428 if (dev->class) {
429 sysfs_create_link(&dev->kobj, &dev->class->subsys.kset.kobj,
430 "subsystem");
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700431 sysfs_create_link(&dev->class->subsys.kset.kobj, &dev->kobj,
432 dev->bus_id);
Greg Kroah-Hartman64bb5d22006-06-28 16:19:58 -0700433 if (parent) {
434 sysfs_create_link(&dev->kobj, &dev->parent->kobj, "device");
435 class_name = make_class_name(dev->class->name, &dev->kobj);
436 sysfs_create_link(&dev->parent->kobj, &dev->kobj, class_name);
437 }
Kay Sieversb9d9c822006-06-15 15:31:56 +0200438 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700439
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700440 if ((error = device_add_attrs(dev)))
441 goto AttrsError;
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700442 if ((error = device_add_groups(dev)))
443 goto GroupError;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 if ((error = device_pm_add(dev)))
445 goto PMError;
446 if ((error = bus_add_device(dev)))
447 goto BusError;
Kay Sievers53877d02006-04-04 20:42:26 +0200448 kobject_uevent(&dev->kobj, KOBJ_ADD);
449 bus_attach_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 if (parent)
James Bottomleyd856f1e32005-08-19 09:14:01 -0400451 klist_add_tail(&dev->knode_parent, &parent->klist_children);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700453 if (dev->class) {
454 /* tie the class to the device */
455 down(&dev->class->sem);
456 list_add_tail(&dev->node, &dev->class->devices);
457 up(&dev->class->sem);
458 }
459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 /* notify platform of device entry */
461 if (platform_notify)
462 platform_notify(dev);
463 Done:
Greg Kroah-Hartmane9a7d302006-06-20 13:59:20 -0700464 kfree(class_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 put_device(dev);
466 return error;
467 BusError:
468 device_pm_remove(dev);
469 PMError:
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700470 device_remove_groups(dev);
471 GroupError:
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700472 device_remove_attrs(dev);
473 AttrsError:
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700474 if (dev->devt_attr) {
475 device_remove_file(dev, dev->devt_attr);
476 kfree(dev->devt_attr);
477 }
478 attrError:
Kay Sievers312c0042005-11-16 09:00:00 +0100479 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 kobject_del(&dev->kobj);
481 Error:
482 if (parent)
483 put_device(parent);
484 goto Done;
485}
486
487
488/**
489 * device_register - register a device with the system.
490 * @dev: pointer to the device structure
491 *
492 * This happens in two clean steps - initialize the device
493 * and add it to the system. The two steps can be called
494 * separately, but this is the easiest and most common.
495 * I.e. you should only call the two helpers separately if
496 * have a clearly defined need to use and refcount the device
497 * before it is added to the hierarchy.
498 */
499
500int device_register(struct device *dev)
501{
502 device_initialize(dev);
503 return device_add(dev);
504}
505
506
507/**
508 * get_device - increment reference count for device.
509 * @dev: device.
510 *
511 * This simply forwards the call to kobject_get(), though
512 * we do take care to provide for the case that we get a NULL
513 * pointer passed in.
514 */
515
516struct device * get_device(struct device * dev)
517{
518 return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
519}
520
521
522/**
523 * put_device - decrement reference count.
524 * @dev: device in question.
525 */
526void put_device(struct device * dev)
527{
528 if (dev)
529 kobject_put(&dev->kobj);
530}
531
532
533/**
534 * device_del - delete device from system.
535 * @dev: device.
536 *
537 * This is the first part of the device unregistration
538 * sequence. This removes the device from the lists we control
539 * from here, has it removed from the other driver model
540 * subsystems it was added to in device_add(), and removes it
541 * from the kobject hierarchy.
542 *
543 * NOTE: this should be called manually _iff_ device_add() was
544 * also called manually.
545 */
546
547void device_del(struct device * dev)
548{
549 struct device * parent = dev->parent;
Greg Kroah-Hartmane9a7d302006-06-20 13:59:20 -0700550 char *class_name = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 if (parent)
Patrick Mocheld62c0f92005-06-24 08:39:33 -0700553 klist_del(&dev->knode_parent);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700554 if (dev->devt_attr)
555 device_remove_file(dev, dev->devt_attr);
Kay Sieversb9d9c822006-06-15 15:31:56 +0200556 if (dev->class) {
557 sysfs_remove_link(&dev->kobj, "subsystem");
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700558 sysfs_remove_link(&dev->class->subsys.kset.kobj, dev->bus_id);
Greg Kroah-Hartmane9a7d302006-06-20 13:59:20 -0700559 class_name = make_class_name(dev->class->name, &dev->kobj);
Greg Kroah-Hartman64bb5d22006-06-28 16:19:58 -0700560 if (parent) {
561 sysfs_remove_link(&dev->kobj, "device");
562 sysfs_remove_link(&dev->parent->kobj, class_name);
563 }
Greg Kroah-Hartmane9a7d302006-06-20 13:59:20 -0700564 kfree(class_name);
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700565 down(&dev->class->sem);
566 list_del_init(&dev->node);
567 up(&dev->class->sem);
Kay Sieversb9d9c822006-06-15 15:31:56 +0200568 }
Kay Sieversa7fd6702005-10-01 14:49:43 +0200569 device_remove_file(dev, &dev->uevent_attr);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700570 device_remove_groups(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700571 device_remove_attrs(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573 /* Notify the platform of the removal, in case they
574 * need to do anything...
575 */
576 if (platform_notify_remove)
577 platform_notify_remove(dev);
578 bus_remove_device(dev);
579 device_pm_remove(dev);
Kay Sievers312c0042005-11-16 09:00:00 +0100580 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 kobject_del(&dev->kobj);
582 if (parent)
583 put_device(parent);
584}
585
586/**
587 * device_unregister - unregister device from system.
588 * @dev: device going away.
589 *
590 * We do this in two parts, like we do device_register(). First,
591 * we remove it from all the subsystems with device_del(), then
592 * we decrement the reference count via put_device(). If that
593 * is the final reference count, the device will be cleaned up
594 * via device_release() above. Otherwise, the structure will
595 * stick around until the final reference to the device is dropped.
596 */
597void device_unregister(struct device * dev)
598{
599 pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id);
600 device_del(dev);
601 put_device(dev);
602}
603
604
mochel@digitalimplant.org36239572005-03-24 19:08:30 -0800605static struct device * next_device(struct klist_iter * i)
606{
607 struct klist_node * n = klist_next(i);
608 return n ? container_of(n, struct device, knode_parent) : NULL;
609}
610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611/**
612 * device_for_each_child - device child iterator.
Randy Dunlapc41455f2005-10-23 11:59:14 -0700613 * @parent: parent struct device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 * @data: data for the callback.
615 * @fn: function to be called for each device.
616 *
Randy Dunlapc41455f2005-10-23 11:59:14 -0700617 * Iterate over @parent's child devices, and call @fn for each,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 * passing it @data.
619 *
620 * We check the return of @fn each time. If it returns anything
621 * other than 0, we break out and return that value.
622 */
mochel@digitalimplant.org36239572005-03-24 19:08:30 -0800623int device_for_each_child(struct device * parent, void * data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 int (*fn)(struct device *, void *))
625{
mochel@digitalimplant.org36239572005-03-24 19:08:30 -0800626 struct klist_iter i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 struct device * child;
628 int error = 0;
629
mochel@digitalimplant.org36239572005-03-24 19:08:30 -0800630 klist_iter_init(&parent->klist_children, &i);
631 while ((child = next_device(&i)) && !error)
632 error = fn(child, data);
633 klist_iter_exit(&i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 return error;
635}
636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637int __init devices_init(void)
638{
639 return subsystem_register(&devices_subsys);
640}
641
642EXPORT_SYMBOL_GPL(device_for_each_child);
643
644EXPORT_SYMBOL_GPL(device_initialize);
645EXPORT_SYMBOL_GPL(device_add);
646EXPORT_SYMBOL_GPL(device_register);
647
648EXPORT_SYMBOL_GPL(device_del);
649EXPORT_SYMBOL_GPL(device_unregister);
650EXPORT_SYMBOL_GPL(get_device);
651EXPORT_SYMBOL_GPL(put_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653EXPORT_SYMBOL_GPL(device_create_file);
654EXPORT_SYMBOL_GPL(device_remove_file);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700655
656
657static void device_create_release(struct device *dev)
658{
659 pr_debug("%s called for %s\n", __FUNCTION__, dev->bus_id);
660 kfree(dev);
661}
662
663/**
664 * device_create - creates a device and registers it with sysfs
Henrik Kretzschmar42734da2006-07-05 00:53:19 +0200665 * @class: pointer to the struct class that this device should be registered to
666 * @parent: pointer to the parent struct device of this new device, if any
667 * @devt: the dev_t for the char device to be added
668 * @fmt: string for the device's name
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700669 *
Henrik Kretzschmar42734da2006-07-05 00:53:19 +0200670 * This function can be used by char device classes. A struct device
671 * will be created in sysfs, registered to the specified class.
672 *
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700673 * A "dev" file will be created, showing the dev_t for the device, if
674 * the dev_t is not 0,0.
Henrik Kretzschmar42734da2006-07-05 00:53:19 +0200675 * If a pointer to a parent struct device is passed in, the newly created
676 * struct device will be a child of that device in sysfs.
677 * The pointer to the struct device will be returned from the call.
678 * Any further sysfs files that might be required can be created using this
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700679 * pointer.
680 *
681 * Note: the struct class passed to this function must have previously
682 * been created with a call to class_create().
683 */
684struct device *device_create(struct class *class, struct device *parent,
Greg Kroah-Hartman5cbe5f82006-08-10 01:19:19 -0400685 dev_t devt, const char *fmt, ...)
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700686{
687 va_list args;
688 struct device *dev = NULL;
689 int retval = -ENODEV;
690
691 if (class == NULL || IS_ERR(class))
692 goto error;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700693
694 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
695 if (!dev) {
696 retval = -ENOMEM;
697 goto error;
698 }
699
700 dev->devt = devt;
701 dev->class = class;
702 dev->parent = parent;
703 dev->release = device_create_release;
704
705 va_start(args, fmt);
706 vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args);
707 va_end(args);
708 retval = device_register(dev);
709 if (retval)
710 goto error;
711
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700712 return dev;
713
714error:
715 kfree(dev);
716 return ERR_PTR(retval);
717}
718EXPORT_SYMBOL_GPL(device_create);
719
720/**
721 * device_destroy - removes a device that was created with device_create()
Henrik Kretzschmar42734da2006-07-05 00:53:19 +0200722 * @class: pointer to the struct class that this device was registered with
723 * @devt: the dev_t of the device that was previously registered
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700724 *
Henrik Kretzschmar42734da2006-07-05 00:53:19 +0200725 * This call unregisters and cleans up a device that was created with a
726 * call to device_create().
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700727 */
728void device_destroy(struct class *class, dev_t devt)
729{
730 struct device *dev = NULL;
731 struct device *dev_tmp;
732
733 down(&class->sem);
734 list_for_each_entry(dev_tmp, &class->devices, node) {
735 if (dev_tmp->devt == devt) {
736 dev = dev_tmp;
737 break;
738 }
739 }
740 up(&class->sem);
741
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700742 if (dev)
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700743 device_unregister(dev);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700744}
745EXPORT_SYMBOL_GPL(device_destroy);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -0700746
747/**
748 * device_rename - renames a device
749 * @dev: the pointer to the struct device to be renamed
750 * @new_name: the new name of the device
751 */
752int device_rename(struct device *dev, char *new_name)
753{
754 char *old_class_name = NULL;
755 char *new_class_name = NULL;
756 char *old_symlink_name = NULL;
757 int error;
758
759 dev = get_device(dev);
760 if (!dev)
761 return -EINVAL;
762
763 pr_debug("DEVICE: renaming '%s' to '%s'\n", dev->bus_id, new_name);
764
765 if ((dev->class) && (dev->parent))
766 old_class_name = make_class_name(dev->class->name, &dev->kobj);
767
768 if (dev->class) {
769 old_symlink_name = kmalloc(BUS_ID_SIZE, GFP_KERNEL);
770 if (!old_symlink_name)
771 return -ENOMEM;
772 strlcpy(old_symlink_name, dev->bus_id, BUS_ID_SIZE);
773 }
774
775 strlcpy(dev->bus_id, new_name, BUS_ID_SIZE);
776
777 error = kobject_rename(&dev->kobj, new_name);
778
779 if (old_class_name) {
780 new_class_name = make_class_name(dev->class->name, &dev->kobj);
781 if (new_class_name) {
782 sysfs_create_link(&dev->parent->kobj, &dev->kobj,
783 new_class_name);
784 sysfs_remove_link(&dev->parent->kobj, old_class_name);
785 }
786 }
787 if (dev->class) {
788 sysfs_remove_link(&dev->class->subsys.kset.kobj,
789 old_symlink_name);
790 sysfs_create_link(&dev->class->subsys.kset.kobj, &dev->kobj,
791 dev->bus_id);
792 }
793 put_device(dev);
794
795 kfree(old_class_name);
796 kfree(new_class_name);
797 kfree(old_symlink_name);
798
799 return error;
800}