blob: 5c91d0d81e7807476318b44e1e8cbaa78dd0b26f [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);
97 else {
98 printk(KERN_ERR "Device '%s' does not have a release() function, "
99 "it is broken and must be fixed.\n",
100 dev->bus_id);
101 WARN_ON(1);
102 }
103}
104
105static struct kobj_type ktype_device = {
106 .release = device_release,
107 .sysfs_ops = &dev_sysfs_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108};
109
110
Kay Sievers312c0042005-11-16 09:00:00 +0100111static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
113 struct kobj_type *ktype = get_ktype(kobj);
114
115 if (ktype == &ktype_device) {
116 struct device *dev = to_dev(kobj);
117 if (dev->bus)
118 return 1;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700119 if (dev->class)
120 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 }
122 return 0;
123}
124
Kay Sievers312c0042005-11-16 09:00:00 +0100125static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
127 struct device *dev = to_dev(kobj);
128
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700129 if (dev->bus)
130 return dev->bus->name;
131 if (dev->class)
132 return dev->class->name;
133 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134}
135
Kay Sievers312c0042005-11-16 09:00:00 +0100136static int dev_uevent(struct kset *kset, struct kobject *kobj, char **envp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 int num_envp, char *buffer, int buffer_size)
138{
139 struct device *dev = to_dev(kobj);
140 int i = 0;
141 int length = 0;
142 int retval = 0;
143
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700144 /* add the major/minor if present */
145 if (MAJOR(dev->devt)) {
146 add_uevent_var(envp, num_envp, &i,
147 buffer, buffer_size, &length,
148 "MAJOR=%u", MAJOR(dev->devt));
149 add_uevent_var(envp, num_envp, &i,
150 buffer, buffer_size, &length,
151 "MINOR=%u", MINOR(dev->devt));
152 }
153
Kay Sieversd81d9d62006-08-13 06:17:09 +0200154 /* add bus name (same as SUBSYSTEM, deprecated) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 if (dev->bus)
Kay Sievers312c0042005-11-16 09:00:00 +0100156 add_uevent_var(envp, num_envp, &i,
157 buffer, buffer_size, &length,
158 "PHYSDEVBUS=%s", dev->bus->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Kay Sieversd81d9d62006-08-13 06:17:09 +0200160 /* add driver name (PHYSDEV* values are deprecated)*/
161 if (dev->driver) {
162 add_uevent_var(envp, num_envp, &i,
163 buffer, buffer_size, &length,
164 "DRIVER=%s", dev->driver->name);
Kay Sievers312c0042005-11-16 09:00:00 +0100165 add_uevent_var(envp, num_envp, &i,
166 buffer, buffer_size, &length,
167 "PHYSDEVDRIVER=%s", dev->driver->name);
Kay Sieversd81d9d62006-08-13 06:17:09 +0200168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170 /* terminate, set to next free slot, shrink available space */
171 envp[i] = NULL;
172 envp = &envp[i];
173 num_envp -= i;
174 buffer = &buffer[length];
175 buffer_size -= length;
176
Kay Sievers312c0042005-11-16 09:00:00 +0100177 if (dev->bus && dev->bus->uevent) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 /* have the bus specific function add its stuff */
Kay Sievers312c0042005-11-16 09:00:00 +0100179 retval = dev->bus->uevent(dev, envp, num_envp, buffer, buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 if (retval) {
Kay Sievers312c0042005-11-16 09:00:00 +0100181 pr_debug ("%s - uevent() returned %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 __FUNCTION__, retval);
183 }
184 }
185
186 return retval;
187}
188
Kay Sievers312c0042005-11-16 09:00:00 +0100189static struct kset_uevent_ops device_uevent_ops = {
190 .filter = dev_uevent_filter,
191 .name = dev_uevent_name,
192 .uevent = dev_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193};
194
Kay Sieversa7fd6702005-10-01 14:49:43 +0200195static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
196 const char *buf, size_t count)
197{
Kay Sievers312c0042005-11-16 09:00:00 +0100198 kobject_uevent(&dev->kobj, KOBJ_ADD);
Kay Sieversa7fd6702005-10-01 14:49:43 +0200199 return count;
200}
201
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700202static int device_add_groups(struct device *dev)
203{
204 int i;
205 int error = 0;
206
207 if (dev->groups) {
208 for (i = 0; dev->groups[i]; i++) {
209 error = sysfs_create_group(&dev->kobj, dev->groups[i]);
210 if (error) {
211 while (--i >= 0)
212 sysfs_remove_group(&dev->kobj, dev->groups[i]);
213 goto out;
214 }
215 }
216 }
217out:
218 return error;
219}
220
221static void device_remove_groups(struct device *dev)
222{
223 int i;
224 if (dev->groups) {
225 for (i = 0; dev->groups[i]; i++) {
226 sysfs_remove_group(&dev->kobj, dev->groups[i]);
227 }
228 }
229}
230
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700231static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
232 char *buf)
233{
234 return print_dev_t(buf, dev->devt);
235}
236
Martin Waitz0863afb2006-01-09 20:53:55 -0800237/*
238 * devices_subsys - structure to be registered with kobject core.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 */
240
Kay Sievers312c0042005-11-16 09:00:00 +0100241decl_subsys(devices, &ktype_device, &device_uevent_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243
244/**
245 * device_create_file - create sysfs attribute file for device.
246 * @dev: device.
247 * @attr: device attribute descriptor.
248 */
249
250int device_create_file(struct device * dev, struct device_attribute * attr)
251{
252 int error = 0;
253 if (get_device(dev)) {
254 error = sysfs_create_file(&dev->kobj, &attr->attr);
255 put_device(dev);
256 }
257 return error;
258}
259
260/**
261 * device_remove_file - remove sysfs attribute file.
262 * @dev: device.
263 * @attr: device attribute descriptor.
264 */
265
266void device_remove_file(struct device * dev, struct device_attribute * attr)
267{
268 if (get_device(dev)) {
269 sysfs_remove_file(&dev->kobj, &attr->attr);
270 put_device(dev);
271 }
272}
273
James Bottomley34bb61f2005-09-06 16:56:51 -0700274static void klist_children_get(struct klist_node *n)
275{
276 struct device *dev = container_of(n, struct device, knode_parent);
277
278 get_device(dev);
279}
280
281static void klist_children_put(struct klist_node *n)
282{
283 struct device *dev = container_of(n, struct device, knode_parent);
284
285 put_device(dev);
286}
287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289/**
290 * device_initialize - init device structure.
291 * @dev: device.
292 *
293 * This prepares the device for use by other layers,
294 * including adding it to the device hierarchy.
295 * It is the first half of device_register(), if called by
296 * that, though it can also be called separately, so one
297 * may use @dev's fields (e.g. the refcount).
298 */
299
300void device_initialize(struct device *dev)
301{
302 kobj_set_kset_s(dev, devices_subsys);
303 kobject_init(&dev->kobj);
James Bottomley34bb61f2005-09-06 16:56:51 -0700304 klist_init(&dev->klist_children, klist_children_get,
305 klist_children_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 INIT_LIST_HEAD(&dev->dma_pools);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700307 INIT_LIST_HEAD(&dev->node);
mochel@digitalimplant.orgaf703162005-03-21 10:41:04 -0800308 init_MUTEX(&dev->sem);
David Brownell0ac85242005-09-12 19:39:34 -0700309 device_init_wakeup(dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310}
311
312/**
313 * device_add - add device to device hierarchy.
314 * @dev: device.
315 *
316 * This is part 2 of device_register(), though may be called
317 * separately _iff_ device_initialize() has been called separately.
318 *
319 * This adds it to the kobject hierarchy via kobject_add(), adds it
320 * to the global and sibling lists for the device, then
321 * adds it to the other relevant subsystems of the driver model.
322 */
323int device_add(struct device *dev)
324{
325 struct device *parent = NULL;
Greg Kroah-Hartmane9a7d302006-06-20 13:59:20 -0700326 char *class_name = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 int error = -EINVAL;
328
329 dev = get_device(dev);
330 if (!dev || !strlen(dev->bus_id))
331 goto Error;
332
333 parent = get_device(dev->parent);
334
335 pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id);
336
337 /* first, register with generic layer. */
338 kobject_set_name(&dev->kobj, "%s", dev->bus_id);
339 if (parent)
340 dev->kobj.parent = &parent->kobj;
341
342 if ((error = kobject_add(&dev->kobj)))
343 goto Error;
Kay Sieversa7fd6702005-10-01 14:49:43 +0200344
345 dev->uevent_attr.attr.name = "uevent";
346 dev->uevent_attr.attr.mode = S_IWUSR;
347 if (dev->driver)
348 dev->uevent_attr.attr.owner = dev->driver->owner;
349 dev->uevent_attr.store = store_uevent;
350 device_create_file(dev, &dev->uevent_attr);
351
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700352 if (MAJOR(dev->devt)) {
353 struct device_attribute *attr;
354 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
355 if (!attr) {
356 error = -ENOMEM;
357 goto PMError;
358 }
359 attr->attr.name = "dev";
360 attr->attr.mode = S_IRUGO;
361 if (dev->driver)
362 attr->attr.owner = dev->driver->owner;
363 attr->show = show_dev;
364 error = device_create_file(dev, attr);
365 if (error) {
366 kfree(attr);
367 goto attrError;
368 }
369
370 dev->devt_attr = attr;
371 }
372
Kay Sieversb9d9c822006-06-15 15:31:56 +0200373 if (dev->class) {
374 sysfs_create_link(&dev->kobj, &dev->class->subsys.kset.kobj,
375 "subsystem");
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700376 sysfs_create_link(&dev->class->subsys.kset.kobj, &dev->kobj,
377 dev->bus_id);
Greg Kroah-Hartman64bb5d22006-06-28 16:19:58 -0700378 if (parent) {
379 sysfs_create_link(&dev->kobj, &dev->parent->kobj, "device");
380 class_name = make_class_name(dev->class->name, &dev->kobj);
381 sysfs_create_link(&dev->parent->kobj, &dev->kobj, class_name);
382 }
Kay Sieversb9d9c822006-06-15 15:31:56 +0200383 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700384
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700385 if ((error = device_add_groups(dev)))
386 goto GroupError;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 if ((error = device_pm_add(dev)))
388 goto PMError;
389 if ((error = bus_add_device(dev)))
390 goto BusError;
Kay Sievers53877d02006-04-04 20:42:26 +0200391 kobject_uevent(&dev->kobj, KOBJ_ADD);
392 bus_attach_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 if (parent)
James Bottomleyd856f1e2005-08-19 09:14:01 -0400394 klist_add_tail(&dev->knode_parent, &parent->klist_children);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700396 if (dev->class) {
397 /* tie the class to the device */
398 down(&dev->class->sem);
399 list_add_tail(&dev->node, &dev->class->devices);
400 up(&dev->class->sem);
401 }
402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 /* notify platform of device entry */
404 if (platform_notify)
405 platform_notify(dev);
406 Done:
Greg Kroah-Hartmane9a7d302006-06-20 13:59:20 -0700407 kfree(class_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 put_device(dev);
409 return error;
410 BusError:
411 device_pm_remove(dev);
412 PMError:
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700413 device_remove_groups(dev);
414 GroupError:
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700415 if (dev->devt_attr) {
416 device_remove_file(dev, dev->devt_attr);
417 kfree(dev->devt_attr);
418 }
419 attrError:
Kay Sievers312c0042005-11-16 09:00:00 +0100420 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 kobject_del(&dev->kobj);
422 Error:
423 if (parent)
424 put_device(parent);
425 goto Done;
426}
427
428
429/**
430 * device_register - register a device with the system.
431 * @dev: pointer to the device structure
432 *
433 * This happens in two clean steps - initialize the device
434 * and add it to the system. The two steps can be called
435 * separately, but this is the easiest and most common.
436 * I.e. you should only call the two helpers separately if
437 * have a clearly defined need to use and refcount the device
438 * before it is added to the hierarchy.
439 */
440
441int device_register(struct device *dev)
442{
443 device_initialize(dev);
444 return device_add(dev);
445}
446
447
448/**
449 * get_device - increment reference count for device.
450 * @dev: device.
451 *
452 * This simply forwards the call to kobject_get(), though
453 * we do take care to provide for the case that we get a NULL
454 * pointer passed in.
455 */
456
457struct device * get_device(struct device * dev)
458{
459 return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
460}
461
462
463/**
464 * put_device - decrement reference count.
465 * @dev: device in question.
466 */
467void put_device(struct device * dev)
468{
469 if (dev)
470 kobject_put(&dev->kobj);
471}
472
473
474/**
475 * device_del - delete device from system.
476 * @dev: device.
477 *
478 * This is the first part of the device unregistration
479 * sequence. This removes the device from the lists we control
480 * from here, has it removed from the other driver model
481 * subsystems it was added to in device_add(), and removes it
482 * from the kobject hierarchy.
483 *
484 * NOTE: this should be called manually _iff_ device_add() was
485 * also called manually.
486 */
487
488void device_del(struct device * dev)
489{
490 struct device * parent = dev->parent;
Greg Kroah-Hartmane9a7d302006-06-20 13:59:20 -0700491 char *class_name = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 if (parent)
Patrick Mocheld62c0f92005-06-24 08:39:33 -0700494 klist_del(&dev->knode_parent);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700495 if (dev->devt_attr)
496 device_remove_file(dev, dev->devt_attr);
Kay Sieversb9d9c822006-06-15 15:31:56 +0200497 if (dev->class) {
498 sysfs_remove_link(&dev->kobj, "subsystem");
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700499 sysfs_remove_link(&dev->class->subsys.kset.kobj, dev->bus_id);
Greg Kroah-Hartmane9a7d302006-06-20 13:59:20 -0700500 class_name = make_class_name(dev->class->name, &dev->kobj);
Greg Kroah-Hartman64bb5d22006-06-28 16:19:58 -0700501 if (parent) {
502 sysfs_remove_link(&dev->kobj, "device");
503 sysfs_remove_link(&dev->parent->kobj, class_name);
504 }
Greg Kroah-Hartmane9a7d302006-06-20 13:59:20 -0700505 kfree(class_name);
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700506 down(&dev->class->sem);
507 list_del_init(&dev->node);
508 up(&dev->class->sem);
Kay Sieversb9d9c822006-06-15 15:31:56 +0200509 }
Kay Sieversa7fd6702005-10-01 14:49:43 +0200510 device_remove_file(dev, &dev->uevent_attr);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700511 device_remove_groups(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513 /* Notify the platform of the removal, in case they
514 * need to do anything...
515 */
516 if (platform_notify_remove)
517 platform_notify_remove(dev);
518 bus_remove_device(dev);
519 device_pm_remove(dev);
Kay Sievers312c0042005-11-16 09:00:00 +0100520 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 kobject_del(&dev->kobj);
522 if (parent)
523 put_device(parent);
524}
525
526/**
527 * device_unregister - unregister device from system.
528 * @dev: device going away.
529 *
530 * We do this in two parts, like we do device_register(). First,
531 * we remove it from all the subsystems with device_del(), then
532 * we decrement the reference count via put_device(). If that
533 * is the final reference count, the device will be cleaned up
534 * via device_release() above. Otherwise, the structure will
535 * stick around until the final reference to the device is dropped.
536 */
537void device_unregister(struct device * dev)
538{
539 pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id);
540 device_del(dev);
541 put_device(dev);
542}
543
544
mochel@digitalimplant.org36239572005-03-24 19:08:30 -0800545static struct device * next_device(struct klist_iter * i)
546{
547 struct klist_node * n = klist_next(i);
548 return n ? container_of(n, struct device, knode_parent) : NULL;
549}
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551/**
552 * device_for_each_child - device child iterator.
Randy Dunlapc41455f2005-10-23 11:59:14 -0700553 * @parent: parent struct device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 * @data: data for the callback.
555 * @fn: function to be called for each device.
556 *
Randy Dunlapc41455f2005-10-23 11:59:14 -0700557 * Iterate over @parent's child devices, and call @fn for each,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 * passing it @data.
559 *
560 * We check the return of @fn each time. If it returns anything
561 * other than 0, we break out and return that value.
562 */
mochel@digitalimplant.org36239572005-03-24 19:08:30 -0800563int device_for_each_child(struct device * parent, void * data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 int (*fn)(struct device *, void *))
565{
mochel@digitalimplant.org36239572005-03-24 19:08:30 -0800566 struct klist_iter i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 struct device * child;
568 int error = 0;
569
mochel@digitalimplant.org36239572005-03-24 19:08:30 -0800570 klist_iter_init(&parent->klist_children, &i);
571 while ((child = next_device(&i)) && !error)
572 error = fn(child, data);
573 klist_iter_exit(&i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 return error;
575}
576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577int __init devices_init(void)
578{
579 return subsystem_register(&devices_subsys);
580}
581
582EXPORT_SYMBOL_GPL(device_for_each_child);
583
584EXPORT_SYMBOL_GPL(device_initialize);
585EXPORT_SYMBOL_GPL(device_add);
586EXPORT_SYMBOL_GPL(device_register);
587
588EXPORT_SYMBOL_GPL(device_del);
589EXPORT_SYMBOL_GPL(device_unregister);
590EXPORT_SYMBOL_GPL(get_device);
591EXPORT_SYMBOL_GPL(put_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
593EXPORT_SYMBOL_GPL(device_create_file);
594EXPORT_SYMBOL_GPL(device_remove_file);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700595
596
597static void device_create_release(struct device *dev)
598{
599 pr_debug("%s called for %s\n", __FUNCTION__, dev->bus_id);
600 kfree(dev);
601}
602
603/**
604 * device_create - creates a device and registers it with sysfs
Henrik Kretzschmar42734da2006-07-05 00:53:19 +0200605 * @class: pointer to the struct class that this device should be registered to
606 * @parent: pointer to the parent struct device of this new device, if any
607 * @devt: the dev_t for the char device to be added
608 * @fmt: string for the device's name
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700609 *
Henrik Kretzschmar42734da2006-07-05 00:53:19 +0200610 * This function can be used by char device classes. A struct device
611 * will be created in sysfs, registered to the specified class.
612 *
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700613 * A "dev" file will be created, showing the dev_t for the device, if
614 * the dev_t is not 0,0.
Henrik Kretzschmar42734da2006-07-05 00:53:19 +0200615 * If a pointer to a parent struct device is passed in, the newly created
616 * struct device will be a child of that device in sysfs.
617 * The pointer to the struct device will be returned from the call.
618 * Any further sysfs files that might be required can be created using this
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700619 * pointer.
620 *
621 * Note: the struct class passed to this function must have previously
622 * been created with a call to class_create().
623 */
624struct device *device_create(struct class *class, struct device *parent,
Greg Kroah-Hartman5cbe5f82006-08-10 01:19:19 -0400625 dev_t devt, const char *fmt, ...)
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700626{
627 va_list args;
628 struct device *dev = NULL;
629 int retval = -ENODEV;
630
631 if (class == NULL || IS_ERR(class))
632 goto error;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700633
634 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
635 if (!dev) {
636 retval = -ENOMEM;
637 goto error;
638 }
639
640 dev->devt = devt;
641 dev->class = class;
642 dev->parent = parent;
643 dev->release = device_create_release;
644
645 va_start(args, fmt);
646 vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args);
647 va_end(args);
648 retval = device_register(dev);
649 if (retval)
650 goto error;
651
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700652 return dev;
653
654error:
655 kfree(dev);
656 return ERR_PTR(retval);
657}
658EXPORT_SYMBOL_GPL(device_create);
659
660/**
661 * device_destroy - removes a device that was created with device_create()
Henrik Kretzschmar42734da2006-07-05 00:53:19 +0200662 * @class: pointer to the struct class that this device was registered with
663 * @devt: the dev_t of the device that was previously registered
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700664 *
Henrik Kretzschmar42734da2006-07-05 00:53:19 +0200665 * This call unregisters and cleans up a device that was created with a
666 * call to device_create().
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700667 */
668void device_destroy(struct class *class, dev_t devt)
669{
670 struct device *dev = NULL;
671 struct device *dev_tmp;
672
673 down(&class->sem);
674 list_for_each_entry(dev_tmp, &class->devices, node) {
675 if (dev_tmp->devt == devt) {
676 dev = dev_tmp;
677 break;
678 }
679 }
680 up(&class->sem);
681
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700682 if (dev)
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700683 device_unregister(dev);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700684}
685EXPORT_SYMBOL_GPL(device_destroy);