blob: b565b7e9d40bde58c385be1e07f96699851e47a3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/base/core.c - core driver model code (device registration, etc)
3 *
4 * Copyright (c) 2002-3 Patrick Mochel
5 * Copyright (c) 2002-3 Open Source Development Labs
Greg Kroah-Hartman64bb5d22006-06-28 16:19:58 -07006 * Copyright (c) 2006 Greg Kroah-Hartman <gregkh@suse.de>
7 * Copyright (c) 2006 Novell, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * This file is released under the GPLv2
10 *
11 */
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/device.h>
14#include <linux/err.h>
15#include <linux/init.h>
16#include <linux/module.h>
17#include <linux/slab.h>
18#include <linux/string.h>
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -070019#include <linux/kdev_t.h>
Benjamin Herrenschmidt116af372006-10-25 13:44:59 +100020#include <linux/notifier.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22#include <asm/semaphore.h>
23
24#include "base.h"
25#include "power/power.h"
26
27int (*platform_notify)(struct device * dev) = NULL;
28int (*platform_notify_remove)(struct device * dev) = NULL;
29
30/*
31 * sysfs bindings for devices.
32 */
33
Alan Stern3e956372006-06-16 17:10:48 -040034/**
35 * dev_driver_string - Return a device's driver name, if at all possible
36 * @dev: struct device to get the name of
37 *
38 * Will return the device's driver's name if it is bound to a device. If
39 * the device is not bound to a device, it will return the name of the bus
40 * it is attached to. If it is not attached to a bus either, an empty
41 * string will be returned.
42 */
43const char *dev_driver_string(struct device *dev)
44{
45 return dev->driver ? dev->driver->name :
46 (dev->bus ? dev->bus->name : "");
47}
Matthew Wilcox310a9222006-09-23 23:35:04 -060048EXPORT_SYMBOL(dev_driver_string);
Alan Stern3e956372006-06-16 17:10:48 -040049
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#define to_dev(obj) container_of(obj, struct device, kobj)
51#define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053static ssize_t
54dev_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
55{
56 struct device_attribute * dev_attr = to_dev_attr(attr);
57 struct device * dev = to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -050058 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60 if (dev_attr->show)
Yani Ioannou54b6f352005-05-17 06:39:34 -040061 ret = dev_attr->show(dev, dev_attr, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 return ret;
63}
64
65static ssize_t
66dev_attr_store(struct kobject * kobj, struct attribute * attr,
67 const char * buf, size_t count)
68{
69 struct device_attribute * dev_attr = to_dev_attr(attr);
70 struct device * dev = to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -050071 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73 if (dev_attr->store)
Yani Ioannou54b6f352005-05-17 06:39:34 -040074 ret = dev_attr->store(dev, dev_attr, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 return ret;
76}
77
78static struct sysfs_ops dev_sysfs_ops = {
79 .show = dev_attr_show,
80 .store = dev_attr_store,
81};
82
83
84/**
85 * device_release - free device structure.
86 * @kobj: device's kobject.
87 *
88 * This is called once the reference count for the object
89 * reaches 0. We forward the call to the device's release
90 * method, which should handle actually freeing the structure.
91 */
92static void device_release(struct kobject * kobj)
93{
94 struct device * dev = to_dev(kobj);
95
96 if (dev->release)
97 dev->release(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -070098 else if (dev->class && dev->class->dev_release)
99 dev->class->dev_release(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 else {
101 printk(KERN_ERR "Device '%s' does not have a release() function, "
102 "it is broken and must be fixed.\n",
103 dev->bus_id);
104 WARN_ON(1);
105 }
106}
107
108static struct kobj_type ktype_device = {
109 .release = device_release,
110 .sysfs_ops = &dev_sysfs_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111};
112
113
Kay Sievers312c0042005-11-16 09:00:00 +0100114static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
116 struct kobj_type *ktype = get_ktype(kobj);
117
118 if (ktype == &ktype_device) {
119 struct device *dev = to_dev(kobj);
120 if (dev->bus)
121 return 1;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700122 if (dev->class)
123 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 }
125 return 0;
126}
127
Kay Sievers312c0042005-11-16 09:00:00 +0100128static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
130 struct device *dev = to_dev(kobj);
131
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700132 if (dev->bus)
133 return dev->bus->name;
134 if (dev->class)
135 return dev->class->name;
136 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
Kay Sievers312c0042005-11-16 09:00:00 +0100139static int dev_uevent(struct kset *kset, struct kobject *kobj, char **envp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 int num_envp, char *buffer, int buffer_size)
141{
142 struct device *dev = to_dev(kobj);
143 int i = 0;
144 int length = 0;
145 int retval = 0;
146
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700147 /* add the major/minor if present */
148 if (MAJOR(dev->devt)) {
149 add_uevent_var(envp, num_envp, &i,
150 buffer, buffer_size, &length,
151 "MAJOR=%u", MAJOR(dev->devt));
152 add_uevent_var(envp, num_envp, &i,
153 buffer, buffer_size, &length,
154 "MINOR=%u", MINOR(dev->devt));
155 }
156
Kay Sieversd81d9d62006-08-13 06:17:09 +0200157 /* add bus name (same as SUBSYSTEM, deprecated) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 if (dev->bus)
Kay Sievers312c0042005-11-16 09:00:00 +0100159 add_uevent_var(envp, num_envp, &i,
160 buffer, buffer_size, &length,
161 "PHYSDEVBUS=%s", dev->bus->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Kay Sieversd81d9d62006-08-13 06:17:09 +0200163 /* add driver name (PHYSDEV* values are deprecated)*/
164 if (dev->driver) {
165 add_uevent_var(envp, num_envp, &i,
166 buffer, buffer_size, &length,
167 "DRIVER=%s", dev->driver->name);
Kay Sievers312c0042005-11-16 09:00:00 +0100168 add_uevent_var(envp, num_envp, &i,
169 buffer, buffer_size, &length,
170 "PHYSDEVDRIVER=%s", dev->driver->name);
Kay Sieversd81d9d62006-08-13 06:17:09 +0200171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 /* terminate, set to next free slot, shrink available space */
174 envp[i] = NULL;
175 envp = &envp[i];
176 num_envp -= i;
177 buffer = &buffer[length];
178 buffer_size -= length;
179
Kay Sievers312c0042005-11-16 09:00:00 +0100180 if (dev->bus && dev->bus->uevent) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 /* have the bus specific function add its stuff */
Kay Sievers312c0042005-11-16 09:00:00 +0100182 retval = dev->bus->uevent(dev, envp, num_envp, buffer, buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 if (retval) {
Kay Sievers312c0042005-11-16 09:00:00 +0100184 pr_debug ("%s - uevent() returned %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 __FUNCTION__, retval);
186 }
187 }
188
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700189 if (dev->class && dev->class->dev_uevent) {
190 /* have the class specific function add its stuff */
191 retval = dev->class->dev_uevent(dev, envp, num_envp, buffer, buffer_size);
192 if (retval) {
193 pr_debug("%s - dev_uevent() returned %d\n",
194 __FUNCTION__, retval);
195 }
196 }
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 return retval;
199}
200
Kay Sievers312c0042005-11-16 09:00:00 +0100201static struct kset_uevent_ops device_uevent_ops = {
202 .filter = dev_uevent_filter,
203 .name = dev_uevent_name,
204 .uevent = dev_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205};
206
Kay Sieversa7fd6702005-10-01 14:49:43 +0200207static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
208 const char *buf, size_t count)
209{
Kay Sievers312c0042005-11-16 09:00:00 +0100210 kobject_uevent(&dev->kobj, KOBJ_ADD);
Kay Sieversa7fd6702005-10-01 14:49:43 +0200211 return count;
212}
213
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700214static int device_add_groups(struct device *dev)
215{
216 int i;
217 int error = 0;
218
219 if (dev->groups) {
220 for (i = 0; dev->groups[i]; i++) {
221 error = sysfs_create_group(&dev->kobj, dev->groups[i]);
222 if (error) {
223 while (--i >= 0)
224 sysfs_remove_group(&dev->kobj, dev->groups[i]);
225 goto out;
226 }
227 }
228 }
229out:
230 return error;
231}
232
233static void device_remove_groups(struct device *dev)
234{
235 int i;
236 if (dev->groups) {
237 for (i = 0; dev->groups[i]; i++) {
238 sysfs_remove_group(&dev->kobj, dev->groups[i]);
239 }
240 }
241}
242
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700243static int device_add_attrs(struct device *dev)
244{
245 struct class *class = dev->class;
246 int error = 0;
247 int i;
248
249 if (!class)
250 return 0;
251
252 if (class->dev_attrs) {
253 for (i = 0; attr_name(class->dev_attrs[i]); i++) {
254 error = device_create_file(dev, &class->dev_attrs[i]);
255 if (error)
256 break;
257 }
258 }
259 if (error)
260 while (--i >= 0)
261 device_remove_file(dev, &class->dev_attrs[i]);
262 return error;
263}
264
265static void device_remove_attrs(struct device *dev)
266{
267 struct class *class = dev->class;
268 int i;
269
270 if (!class)
271 return;
272
273 if (class->dev_attrs) {
274 for (i = 0; attr_name(class->dev_attrs[i]); i++)
275 device_remove_file(dev, &class->dev_attrs[i]);
276 }
277}
278
279
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700280static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
281 char *buf)
282{
283 return print_dev_t(buf, dev->devt);
284}
285
Martin Waitz0863afb2006-01-09 20:53:55 -0800286/*
287 * devices_subsys - structure to be registered with kobject core.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 */
289
Kay Sievers312c0042005-11-16 09:00:00 +0100290decl_subsys(devices, &ktype_device, &device_uevent_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292
293/**
294 * device_create_file - create sysfs attribute file for device.
295 * @dev: device.
296 * @attr: device attribute descriptor.
297 */
298
299int device_create_file(struct device * dev, struct device_attribute * attr)
300{
301 int error = 0;
302 if (get_device(dev)) {
303 error = sysfs_create_file(&dev->kobj, &attr->attr);
304 put_device(dev);
305 }
306 return error;
307}
308
309/**
310 * device_remove_file - remove sysfs attribute file.
311 * @dev: device.
312 * @attr: device attribute descriptor.
313 */
314
315void device_remove_file(struct device * dev, struct device_attribute * attr)
316{
317 if (get_device(dev)) {
318 sysfs_remove_file(&dev->kobj, &attr->attr);
319 put_device(dev);
320 }
321}
322
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -0700323/**
324 * device_create_bin_file - create sysfs binary attribute file for device.
325 * @dev: device.
326 * @attr: device binary attribute descriptor.
327 */
328int device_create_bin_file(struct device *dev, struct bin_attribute *attr)
329{
330 int error = -EINVAL;
331 if (dev)
332 error = sysfs_create_bin_file(&dev->kobj, attr);
333 return error;
334}
335EXPORT_SYMBOL_GPL(device_create_bin_file);
336
337/**
338 * device_remove_bin_file - remove sysfs binary attribute file
339 * @dev: device.
340 * @attr: device binary attribute descriptor.
341 */
342void device_remove_bin_file(struct device *dev, struct bin_attribute *attr)
343{
344 if (dev)
345 sysfs_remove_bin_file(&dev->kobj, attr);
346}
347EXPORT_SYMBOL_GPL(device_remove_bin_file);
348
James Bottomley34bb61f2005-09-06 16:56:51 -0700349static void klist_children_get(struct klist_node *n)
350{
351 struct device *dev = container_of(n, struct device, knode_parent);
352
353 get_device(dev);
354}
355
356static void klist_children_put(struct klist_node *n)
357{
358 struct device *dev = container_of(n, struct device, knode_parent);
359
360 put_device(dev);
361}
362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364/**
365 * device_initialize - init device structure.
366 * @dev: device.
367 *
368 * This prepares the device for use by other layers,
369 * including adding it to the device hierarchy.
370 * It is the first half of device_register(), if called by
371 * that, though it can also be called separately, so one
372 * may use @dev's fields (e.g. the refcount).
373 */
374
375void device_initialize(struct device *dev)
376{
377 kobj_set_kset_s(dev, devices_subsys);
378 kobject_init(&dev->kobj);
James Bottomley34bb61f2005-09-06 16:56:51 -0700379 klist_init(&dev->klist_children, klist_children_get,
380 klist_children_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 INIT_LIST_HEAD(&dev->dma_pools);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700382 INIT_LIST_HEAD(&dev->node);
mochel@digitalimplant.orgaf703162005-03-21 10:41:04 -0800383 init_MUTEX(&dev->sem);
David Brownell0ac85242005-09-12 19:39:34 -0700384 device_init_wakeup(dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385}
386
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100387#ifdef CONFIG_SYSFS_DEPRECATED
388int setup_parent(struct device *dev, struct device *parent)
389{
390 /* Set the parent to the class, not the parent device */
391 /* this keeps sysfs from having a symlink to make old udevs happy */
392 if (dev->class)
393 dev->kobj.parent = &dev->class->subsys.kset.kobj;
394 else if (parent)
395 dev->kobj.parent = &parent->kobj;
396
397 return 0;
398}
399#else
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700400static int virtual_device_parent(struct device *dev)
401{
402 if (!dev->class)
403 return -ENODEV;
404
405 if (!dev->class->virtual_dir) {
406 static struct kobject *virtual_dir = NULL;
407
408 if (!virtual_dir)
409 virtual_dir = kobject_add_dir(&devices_subsys.kset.kobj, "virtual");
410 dev->class->virtual_dir = kobject_add_dir(virtual_dir, dev->class->name);
411 }
412
413 dev->kobj.parent = dev->class->virtual_dir;
414 return 0;
415}
416
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100417int setup_parent(struct device *dev, struct device *parent)
418{
419 int error;
420
421 /* if this is a class device, and has no parent, create one */
422 if ((dev->class) && (parent == NULL)) {
423 error = virtual_device_parent(dev);
424 if (error)
425 return error;
426 } else if (parent)
427 dev->kobj.parent = &parent->kobj;
428
429 return 0;
430}
431#endif
432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433/**
434 * device_add - add device to device hierarchy.
435 * @dev: device.
436 *
437 * This is part 2 of device_register(), though may be called
438 * separately _iff_ device_initialize() has been called separately.
439 *
440 * This adds it to the kobject hierarchy via kobject_add(), adds it
441 * to the global and sibling lists for the device, then
442 * adds it to the other relevant subsystems of the driver model.
443 */
444int device_add(struct device *dev)
445{
446 struct device *parent = NULL;
Greg Kroah-Hartmane9a7d302006-06-20 13:59:20 -0700447 char *class_name = NULL;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200448 struct class_interface *class_intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 int error = -EINVAL;
450
451 dev = get_device(dev);
452 if (!dev || !strlen(dev->bus_id))
453 goto Error;
454
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100455 pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id);
Greg Kroah-Hartmanc205ef42006-08-07 22:19:37 -0700456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 parent = get_device(dev->parent);
458
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100459 error = setup_parent(dev, parent);
460 if (error)
461 goto Error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463 /* first, register with generic layer. */
464 kobject_set_name(&dev->kobj, "%s", dev->bus_id);
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100465 error = kobject_add(&dev->kobj);
466 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 goto Error;
Kay Sieversa7fd6702005-10-01 14:49:43 +0200468
Brian Walsh37022642006-08-14 22:43:19 -0700469 /* notify platform of device entry */
470 if (platform_notify)
471 platform_notify(dev);
472
Benjamin Herrenschmidt116af372006-10-25 13:44:59 +1000473 /* notify clients of device entry (new way) */
474 if (dev->bus)
475 blocking_notifier_call_chain(&dev->bus->bus_notifier,
476 BUS_NOTIFY_ADD_DEVICE, dev);
477
Kay Sieversa7fd6702005-10-01 14:49:43 +0200478 dev->uevent_attr.attr.name = "uevent";
479 dev->uevent_attr.attr.mode = S_IWUSR;
480 if (dev->driver)
481 dev->uevent_attr.attr.owner = dev->driver->owner;
482 dev->uevent_attr.store = store_uevent;
Cornelia Hucka306eea2006-09-22 11:37:13 +0200483 error = device_create_file(dev, &dev->uevent_attr);
484 if (error)
485 goto attrError;
Kay Sieversa7fd6702005-10-01 14:49:43 +0200486
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700487 if (MAJOR(dev->devt)) {
488 struct device_attribute *attr;
489 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
490 if (!attr) {
491 error = -ENOMEM;
Cornelia Hucka306eea2006-09-22 11:37:13 +0200492 goto ueventattrError;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700493 }
494 attr->attr.name = "dev";
495 attr->attr.mode = S_IRUGO;
496 if (dev->driver)
497 attr->attr.owner = dev->driver->owner;
498 attr->show = show_dev;
499 error = device_create_file(dev, attr);
500 if (error) {
501 kfree(attr);
Cornelia Hucka306eea2006-09-22 11:37:13 +0200502 goto ueventattrError;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700503 }
504
505 dev->devt_attr = attr;
506 }
507
Kay Sieversb9d9c822006-06-15 15:31:56 +0200508 if (dev->class) {
509 sysfs_create_link(&dev->kobj, &dev->class->subsys.kset.kobj,
510 "subsystem");
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100511 /* If this is not a "fake" compatible device, then create the
512 * symlink from the class to the device. */
513 if (dev->kobj.parent != &dev->class->subsys.kset.kobj)
514 sysfs_create_link(&dev->class->subsys.kset.kobj,
515 &dev->kobj, dev->bus_id);
Kay Sievers99ef3ef2006-09-14 11:23:28 +0200516#ifdef CONFIG_SYSFS_DEPRECATED
Greg Kroah-Hartman64bb5d22006-06-28 16:19:58 -0700517 if (parent) {
518 sysfs_create_link(&dev->kobj, &dev->parent->kobj, "device");
519 class_name = make_class_name(dev->class->name, &dev->kobj);
520 sysfs_create_link(&dev->parent->kobj, &dev->kobj, class_name);
521 }
Kay Sievers99ef3ef2006-09-14 11:23:28 +0200522#endif
Kay Sieversb9d9c822006-06-15 15:31:56 +0200523 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700524
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700525 if ((error = device_add_attrs(dev)))
526 goto AttrsError;
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700527 if ((error = device_add_groups(dev)))
528 goto GroupError;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 if ((error = device_pm_add(dev)))
530 goto PMError;
531 if ((error = bus_add_device(dev)))
532 goto BusError;
Kay Sievers53877d02006-04-04 20:42:26 +0200533 kobject_uevent(&dev->kobj, KOBJ_ADD);
Alan Sternf70fa622006-10-05 17:03:24 -0400534 if ((error = bus_attach_device(dev)))
535 goto AttachError;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 if (parent)
James Bottomleyd856f1e2005-08-19 09:14:01 -0400537 klist_add_tail(&dev->knode_parent, &parent->klist_children);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700539 if (dev->class) {
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700540 down(&dev->class->sem);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200541 /* tie the class to the device */
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700542 list_add_tail(&dev->node, &dev->class->devices);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200543
544 /* notify any interfaces that the device is here */
545 list_for_each_entry(class_intf, &dev->class->interfaces, node)
546 if (class_intf->add_dev)
547 class_intf->add_dev(dev, class_intf);
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700548 up(&dev->class->sem);
549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 Done:
Greg Kroah-Hartmane9a7d302006-06-20 13:59:20 -0700551 kfree(class_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 put_device(dev);
553 return error;
Alan Sternf70fa622006-10-05 17:03:24 -0400554 AttachError:
555 bus_remove_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 BusError:
557 device_pm_remove(dev);
558 PMError:
Benjamin Herrenschmidt116af372006-10-25 13:44:59 +1000559 if (dev->bus)
560 blocking_notifier_call_chain(&dev->bus->bus_notifier,
561 BUS_NOTIFY_DEL_DEVICE, dev);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700562 device_remove_groups(dev);
563 GroupError:
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700564 device_remove_attrs(dev);
565 AttrsError:
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700566 if (dev->devt_attr) {
567 device_remove_file(dev, dev->devt_attr);
568 kfree(dev->devt_attr);
569 }
Cornelia Hucka306eea2006-09-22 11:37:13 +0200570 ueventattrError:
571 device_remove_file(dev, &dev->uevent_attr);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700572 attrError:
Kay Sievers312c0042005-11-16 09:00:00 +0100573 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 kobject_del(&dev->kobj);
575 Error:
576 if (parent)
577 put_device(parent);
578 goto Done;
579}
580
581
582/**
583 * device_register - register a device with the system.
584 * @dev: pointer to the device structure
585 *
586 * This happens in two clean steps - initialize the device
587 * and add it to the system. The two steps can be called
588 * separately, but this is the easiest and most common.
589 * I.e. you should only call the two helpers separately if
590 * have a clearly defined need to use and refcount the device
591 * before it is added to the hierarchy.
592 */
593
594int device_register(struct device *dev)
595{
596 device_initialize(dev);
597 return device_add(dev);
598}
599
600
601/**
602 * get_device - increment reference count for device.
603 * @dev: device.
604 *
605 * This simply forwards the call to kobject_get(), though
606 * we do take care to provide for the case that we get a NULL
607 * pointer passed in.
608 */
609
610struct device * get_device(struct device * dev)
611{
612 return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
613}
614
615
616/**
617 * put_device - decrement reference count.
618 * @dev: device in question.
619 */
620void put_device(struct device * dev)
621{
622 if (dev)
623 kobject_put(&dev->kobj);
624}
625
626
627/**
628 * device_del - delete device from system.
629 * @dev: device.
630 *
631 * This is the first part of the device unregistration
632 * sequence. This removes the device from the lists we control
633 * from here, has it removed from the other driver model
634 * subsystems it was added to in device_add(), and removes it
635 * from the kobject hierarchy.
636 *
637 * NOTE: this should be called manually _iff_ device_add() was
638 * also called manually.
639 */
640
641void device_del(struct device * dev)
642{
643 struct device * parent = dev->parent;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200644 struct class_interface *class_intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 if (parent)
Patrick Mocheld62c0f92005-06-24 08:39:33 -0700647 klist_del(&dev->knode_parent);
Catalin Marinas82189b92006-11-25 11:09:30 -0800648 if (dev->devt_attr) {
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700649 device_remove_file(dev, dev->devt_attr);
Catalin Marinas82189b92006-11-25 11:09:30 -0800650 kfree(dev->devt_attr);
651 }
Kay Sieversb9d9c822006-06-15 15:31:56 +0200652 if (dev->class) {
653 sysfs_remove_link(&dev->kobj, "subsystem");
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100654 /* If this is not a "fake" compatible device, remove the
655 * symlink from the class to the device. */
656 if (dev->kobj.parent != &dev->class->subsys.kset.kobj)
657 sysfs_remove_link(&dev->class->subsys.kset.kobj,
658 dev->bus_id);
Kay Sievers99ef3ef2006-09-14 11:23:28 +0200659#ifdef CONFIG_SYSFS_DEPRECATED
Greg Kroah-Hartman64bb5d22006-06-28 16:19:58 -0700660 if (parent) {
Kay Sievers99ef3ef2006-09-14 11:23:28 +0200661 char *class_name = make_class_name(dev->class->name,
662 &dev->kobj);
Greg Kroah-Hartman64bb5d22006-06-28 16:19:58 -0700663 sysfs_remove_link(&dev->parent->kobj, class_name);
Kay Sievers99ef3ef2006-09-14 11:23:28 +0200664 kfree(class_name);
665 sysfs_remove_link(&dev->kobj, "device");
Greg Kroah-Hartman64bb5d22006-06-28 16:19:58 -0700666 }
Kay Sievers99ef3ef2006-09-14 11:23:28 +0200667#endif
668
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700669 down(&dev->class->sem);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200670 /* notify any interfaces that the device is now gone */
671 list_for_each_entry(class_intf, &dev->class->interfaces, node)
672 if (class_intf->remove_dev)
673 class_intf->remove_dev(dev, class_intf);
674 /* remove the device from the class list */
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700675 list_del_init(&dev->node);
676 up(&dev->class->sem);
Kay Sieversb9d9c822006-06-15 15:31:56 +0200677 }
Kay Sieversa7fd6702005-10-01 14:49:43 +0200678 device_remove_file(dev, &dev->uevent_attr);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700679 device_remove_groups(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700680 device_remove_attrs(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
682 /* Notify the platform of the removal, in case they
683 * need to do anything...
684 */
685 if (platform_notify_remove)
686 platform_notify_remove(dev);
Benjamin Herrenschmidt116af372006-10-25 13:44:59 +1000687 if (dev->bus)
688 blocking_notifier_call_chain(&dev->bus->bus_notifier,
689 BUS_NOTIFY_DEL_DEVICE, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 bus_remove_device(dev);
691 device_pm_remove(dev);
Kay Sievers312c0042005-11-16 09:00:00 +0100692 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 kobject_del(&dev->kobj);
694 if (parent)
695 put_device(parent);
696}
697
698/**
699 * device_unregister - unregister device from system.
700 * @dev: device going away.
701 *
702 * We do this in two parts, like we do device_register(). First,
703 * we remove it from all the subsystems with device_del(), then
704 * we decrement the reference count via put_device(). If that
705 * is the final reference count, the device will be cleaned up
706 * via device_release() above. Otherwise, the structure will
707 * stick around until the final reference to the device is dropped.
708 */
709void device_unregister(struct device * dev)
710{
711 pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id);
712 device_del(dev);
713 put_device(dev);
714}
715
716
mochel@digitalimplant.org36239572005-03-24 19:08:30 -0800717static struct device * next_device(struct klist_iter * i)
718{
719 struct klist_node * n = klist_next(i);
720 return n ? container_of(n, struct device, knode_parent) : NULL;
721}
722
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723/**
724 * device_for_each_child - device child iterator.
Randy Dunlapc41455f2005-10-23 11:59:14 -0700725 * @parent: parent struct device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 * @data: data for the callback.
727 * @fn: function to be called for each device.
728 *
Randy Dunlapc41455f2005-10-23 11:59:14 -0700729 * Iterate over @parent's child devices, and call @fn for each,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 * passing it @data.
731 *
732 * We check the return of @fn each time. If it returns anything
733 * other than 0, we break out and return that value.
734 */
mochel@digitalimplant.org36239572005-03-24 19:08:30 -0800735int device_for_each_child(struct device * parent, void * data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 int (*fn)(struct device *, void *))
737{
mochel@digitalimplant.org36239572005-03-24 19:08:30 -0800738 struct klist_iter i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 struct device * child;
740 int error = 0;
741
mochel@digitalimplant.org36239572005-03-24 19:08:30 -0800742 klist_iter_init(&parent->klist_children, &i);
743 while ((child = next_device(&i)) && !error)
744 error = fn(child, data);
745 klist_iter_exit(&i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 return error;
747}
748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749int __init devices_init(void)
750{
751 return subsystem_register(&devices_subsys);
752}
753
754EXPORT_SYMBOL_GPL(device_for_each_child);
755
756EXPORT_SYMBOL_GPL(device_initialize);
757EXPORT_SYMBOL_GPL(device_add);
758EXPORT_SYMBOL_GPL(device_register);
759
760EXPORT_SYMBOL_GPL(device_del);
761EXPORT_SYMBOL_GPL(device_unregister);
762EXPORT_SYMBOL_GPL(get_device);
763EXPORT_SYMBOL_GPL(put_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
765EXPORT_SYMBOL_GPL(device_create_file);
766EXPORT_SYMBOL_GPL(device_remove_file);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700767
768
769static void device_create_release(struct device *dev)
770{
771 pr_debug("%s called for %s\n", __FUNCTION__, dev->bus_id);
772 kfree(dev);
773}
774
775/**
776 * device_create - creates a device and registers it with sysfs
Henrik Kretzschmar42734da2006-07-05 00:53:19 +0200777 * @class: pointer to the struct class that this device should be registered to
778 * @parent: pointer to the parent struct device of this new device, if any
779 * @devt: the dev_t for the char device to be added
780 * @fmt: string for the device's name
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700781 *
Henrik Kretzschmar42734da2006-07-05 00:53:19 +0200782 * This function can be used by char device classes. A struct device
783 * will be created in sysfs, registered to the specified class.
784 *
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700785 * A "dev" file will be created, showing the dev_t for the device, if
786 * the dev_t is not 0,0.
Henrik Kretzschmar42734da2006-07-05 00:53:19 +0200787 * If a pointer to a parent struct device is passed in, the newly created
788 * struct device will be a child of that device in sysfs.
789 * The pointer to the struct device will be returned from the call.
790 * Any further sysfs files that might be required can be created using this
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700791 * pointer.
792 *
793 * Note: the struct class passed to this function must have previously
794 * been created with a call to class_create().
795 */
796struct device *device_create(struct class *class, struct device *parent,
Greg Kroah-Hartman5cbe5f82006-08-10 01:19:19 -0400797 dev_t devt, const char *fmt, ...)
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700798{
799 va_list args;
800 struct device *dev = NULL;
801 int retval = -ENODEV;
802
803 if (class == NULL || IS_ERR(class))
804 goto error;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700805
806 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
807 if (!dev) {
808 retval = -ENOMEM;
809 goto error;
810 }
811
812 dev->devt = devt;
813 dev->class = class;
814 dev->parent = parent;
815 dev->release = device_create_release;
816
817 va_start(args, fmt);
818 vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args);
819 va_end(args);
820 retval = device_register(dev);
821 if (retval)
822 goto error;
823
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700824 return dev;
825
826error:
827 kfree(dev);
828 return ERR_PTR(retval);
829}
830EXPORT_SYMBOL_GPL(device_create);
831
832/**
833 * device_destroy - removes a device that was created with device_create()
Henrik Kretzschmar42734da2006-07-05 00:53:19 +0200834 * @class: pointer to the struct class that this device was registered with
835 * @devt: the dev_t of the device that was previously registered
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700836 *
Henrik Kretzschmar42734da2006-07-05 00:53:19 +0200837 * This call unregisters and cleans up a device that was created with a
838 * call to device_create().
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700839 */
840void device_destroy(struct class *class, dev_t devt)
841{
842 struct device *dev = NULL;
843 struct device *dev_tmp;
844
845 down(&class->sem);
846 list_for_each_entry(dev_tmp, &class->devices, node) {
847 if (dev_tmp->devt == devt) {
848 dev = dev_tmp;
849 break;
850 }
851 }
852 up(&class->sem);
853
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700854 if (dev)
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700855 device_unregister(dev);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700856}
857EXPORT_SYMBOL_GPL(device_destroy);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -0700858
859/**
860 * device_rename - renames a device
861 * @dev: the pointer to the struct device to be renamed
862 * @new_name: the new name of the device
863 */
864int device_rename(struct device *dev, char *new_name)
865{
866 char *old_class_name = NULL;
867 char *new_class_name = NULL;
868 char *old_symlink_name = NULL;
869 int error;
870
871 dev = get_device(dev);
872 if (!dev)
873 return -EINVAL;
874
875 pr_debug("DEVICE: renaming '%s' to '%s'\n", dev->bus_id, new_name);
876
Kay Sievers99ef3ef2006-09-14 11:23:28 +0200877#ifdef CONFIG_SYSFS_DEPRECATED
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -0700878 if ((dev->class) && (dev->parent))
879 old_class_name = make_class_name(dev->class->name, &dev->kobj);
Kay Sievers99ef3ef2006-09-14 11:23:28 +0200880#endif
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -0700881
882 if (dev->class) {
883 old_symlink_name = kmalloc(BUS_ID_SIZE, GFP_KERNEL);
Jesper Juhl952ab432006-09-28 23:56:01 +0200884 if (!old_symlink_name) {
885 error = -ENOMEM;
886 goto out_free_old_class;
887 }
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -0700888 strlcpy(old_symlink_name, dev->bus_id, BUS_ID_SIZE);
889 }
890
891 strlcpy(dev->bus_id, new_name, BUS_ID_SIZE);
892
893 error = kobject_rename(&dev->kobj, new_name);
894
Kay Sievers99ef3ef2006-09-14 11:23:28 +0200895#ifdef CONFIG_SYSFS_DEPRECATED
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -0700896 if (old_class_name) {
897 new_class_name = make_class_name(dev->class->name, &dev->kobj);
898 if (new_class_name) {
899 sysfs_create_link(&dev->parent->kobj, &dev->kobj,
900 new_class_name);
901 sysfs_remove_link(&dev->parent->kobj, old_class_name);
902 }
903 }
Kay Sievers99ef3ef2006-09-14 11:23:28 +0200904#endif
905
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -0700906 if (dev->class) {
907 sysfs_remove_link(&dev->class->subsys.kset.kobj,
908 old_symlink_name);
909 sysfs_create_link(&dev->class->subsys.kset.kobj, &dev->kobj,
910 dev->bus_id);
911 }
912 put_device(dev);
913
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -0700914 kfree(new_class_name);
915 kfree(old_symlink_name);
Jesper Juhl952ab432006-09-28 23:56:01 +0200916 out_free_old_class:
917 kfree(old_class_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -0700918
919 return error;
920}