blob: 641c0c42bb4808050ce8e836dc4d73b2dd5ce25d [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
6 *
7 * This file is released under the GPLv2
8 *
9 */
10
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/device.h>
12#include <linux/err.h>
13#include <linux/init.h>
14#include <linux/module.h>
15#include <linux/slab.h>
16#include <linux/string.h>
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -070017#include <linux/kdev_t.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19#include <asm/semaphore.h>
20
21#include "base.h"
22#include "power/power.h"
23
24int (*platform_notify)(struct device * dev) = NULL;
25int (*platform_notify_remove)(struct device * dev) = NULL;
26
27/*
28 * sysfs bindings for devices.
29 */
30
Alan Stern3e956372006-06-16 17:10:48 -040031/**
32 * dev_driver_string - Return a device's driver name, if at all possible
33 * @dev: struct device to get the name of
34 *
35 * Will return the device's driver's name if it is bound to a device. If
36 * the device is not bound to a device, it will return the name of the bus
37 * it is attached to. If it is not attached to a bus either, an empty
38 * string will be returned.
39 */
40const char *dev_driver_string(struct device *dev)
41{
42 return dev->driver ? dev->driver->name :
43 (dev->bus ? dev->bus->name : "");
44}
45EXPORT_SYMBOL_GPL(dev_driver_string);
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#define to_dev(obj) container_of(obj, struct device, kobj)
48#define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050static ssize_t
51dev_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
52{
53 struct device_attribute * dev_attr = to_dev_attr(attr);
54 struct device * dev = to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -050055 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57 if (dev_attr->show)
Yani Ioannou54b6f352005-05-17 06:39:34 -040058 ret = dev_attr->show(dev, dev_attr, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 return ret;
60}
61
62static ssize_t
63dev_attr_store(struct kobject * kobj, struct attribute * attr,
64 const char * buf, size_t count)
65{
66 struct device_attribute * dev_attr = to_dev_attr(attr);
67 struct device * dev = to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -050068 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70 if (dev_attr->store)
Yani Ioannou54b6f352005-05-17 06:39:34 -040071 ret = dev_attr->store(dev, dev_attr, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 return ret;
73}
74
75static struct sysfs_ops dev_sysfs_ops = {
76 .show = dev_attr_show,
77 .store = dev_attr_store,
78};
79
80
81/**
82 * device_release - free device structure.
83 * @kobj: device's kobject.
84 *
85 * This is called once the reference count for the object
86 * reaches 0. We forward the call to the device's release
87 * method, which should handle actually freeing the structure.
88 */
89static void device_release(struct kobject * kobj)
90{
91 struct device * dev = to_dev(kobj);
92
93 if (dev->release)
94 dev->release(dev);
95 else {
96 printk(KERN_ERR "Device '%s' does not have a release() function, "
97 "it is broken and must be fixed.\n",
98 dev->bus_id);
99 WARN_ON(1);
100 }
101}
102
103static struct kobj_type ktype_device = {
104 .release = device_release,
105 .sysfs_ops = &dev_sysfs_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106};
107
108
Kay Sievers312c0042005-11-16 09:00:00 +0100109static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
111 struct kobj_type *ktype = get_ktype(kobj);
112
113 if (ktype == &ktype_device) {
114 struct device *dev = to_dev(kobj);
115 if (dev->bus)
116 return 1;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700117 if (dev->class)
118 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 }
120 return 0;
121}
122
Kay Sievers312c0042005-11-16 09:00:00 +0100123static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
125 struct device *dev = to_dev(kobj);
126
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700127 if (dev->bus)
128 return dev->bus->name;
129 if (dev->class)
130 return dev->class->name;
131 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
133
Kay Sievers312c0042005-11-16 09:00:00 +0100134static int dev_uevent(struct kset *kset, struct kobject *kobj, char **envp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 int num_envp, char *buffer, int buffer_size)
136{
137 struct device *dev = to_dev(kobj);
138 int i = 0;
139 int length = 0;
140 int retval = 0;
141
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700142 /* add the major/minor if present */
143 if (MAJOR(dev->devt)) {
144 add_uevent_var(envp, num_envp, &i,
145 buffer, buffer_size, &length,
146 "MAJOR=%u", MAJOR(dev->devt));
147 add_uevent_var(envp, num_envp, &i,
148 buffer, buffer_size, &length,
149 "MINOR=%u", MINOR(dev->devt));
150 }
151
Kay Sieversd81d9d62006-08-13 06:17:09 +0200152 /* add bus name (same as SUBSYSTEM, deprecated) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 if (dev->bus)
Kay Sievers312c0042005-11-16 09:00:00 +0100154 add_uevent_var(envp, num_envp, &i,
155 buffer, buffer_size, &length,
156 "PHYSDEVBUS=%s", dev->bus->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Kay Sieversd81d9d62006-08-13 06:17:09 +0200158 /* add driver name (PHYSDEV* values are deprecated)*/
159 if (dev->driver) {
160 add_uevent_var(envp, num_envp, &i,
161 buffer, buffer_size, &length,
162 "DRIVER=%s", dev->driver->name);
Kay Sievers312c0042005-11-16 09:00:00 +0100163 add_uevent_var(envp, num_envp, &i,
164 buffer, buffer_size, &length,
165 "PHYSDEVDRIVER=%s", dev->driver->name);
Kay Sieversd81d9d62006-08-13 06:17:09 +0200166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168 /* terminate, set to next free slot, shrink available space */
169 envp[i] = NULL;
170 envp = &envp[i];
171 num_envp -= i;
172 buffer = &buffer[length];
173 buffer_size -= length;
174
Kay Sievers312c0042005-11-16 09:00:00 +0100175 if (dev->bus && dev->bus->uevent) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 /* have the bus specific function add its stuff */
Kay Sievers312c0042005-11-16 09:00:00 +0100177 retval = dev->bus->uevent(dev, envp, num_envp, buffer, buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 if (retval) {
Kay Sievers312c0042005-11-16 09:00:00 +0100179 pr_debug ("%s - uevent() returned %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 __FUNCTION__, retval);
181 }
182 }
183
184 return retval;
185}
186
Kay Sievers312c0042005-11-16 09:00:00 +0100187static struct kset_uevent_ops device_uevent_ops = {
188 .filter = dev_uevent_filter,
189 .name = dev_uevent_name,
190 .uevent = dev_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191};
192
Kay Sieversa7fd6702005-10-01 14:49:43 +0200193static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
194 const char *buf, size_t count)
195{
Kay Sievers312c0042005-11-16 09:00:00 +0100196 kobject_uevent(&dev->kobj, KOBJ_ADD);
Kay Sieversa7fd6702005-10-01 14:49:43 +0200197 return count;
198}
199
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700200static int device_add_groups(struct device *dev)
201{
202 int i;
203 int error = 0;
204
205 if (dev->groups) {
206 for (i = 0; dev->groups[i]; i++) {
207 error = sysfs_create_group(&dev->kobj, dev->groups[i]);
208 if (error) {
209 while (--i >= 0)
210 sysfs_remove_group(&dev->kobj, dev->groups[i]);
211 goto out;
212 }
213 }
214 }
215out:
216 return error;
217}
218
219static void device_remove_groups(struct device *dev)
220{
221 int i;
222 if (dev->groups) {
223 for (i = 0; dev->groups[i]; i++) {
224 sysfs_remove_group(&dev->kobj, dev->groups[i]);
225 }
226 }
227}
228
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700229static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
230 char *buf)
231{
232 return print_dev_t(buf, dev->devt);
233}
234
Martin Waitz0863afb2006-01-09 20:53:55 -0800235/*
236 * devices_subsys - structure to be registered with kobject core.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 */
238
Kay Sievers312c0042005-11-16 09:00:00 +0100239decl_subsys(devices, &ktype_device, &device_uevent_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241
242/**
243 * device_create_file - create sysfs attribute file for device.
244 * @dev: device.
245 * @attr: device attribute descriptor.
246 */
247
248int device_create_file(struct device * dev, struct device_attribute * attr)
249{
250 int error = 0;
251 if (get_device(dev)) {
252 error = sysfs_create_file(&dev->kobj, &attr->attr);
253 put_device(dev);
254 }
255 return error;
256}
257
258/**
259 * device_remove_file - remove sysfs attribute file.
260 * @dev: device.
261 * @attr: device attribute descriptor.
262 */
263
264void device_remove_file(struct device * dev, struct device_attribute * attr)
265{
266 if (get_device(dev)) {
267 sysfs_remove_file(&dev->kobj, &attr->attr);
268 put_device(dev);
269 }
270}
271
James Bottomley34bb61f2005-09-06 16:56:51 -0700272static void klist_children_get(struct klist_node *n)
273{
274 struct device *dev = container_of(n, struct device, knode_parent);
275
276 get_device(dev);
277}
278
279static void klist_children_put(struct klist_node *n)
280{
281 struct device *dev = container_of(n, struct device, knode_parent);
282
283 put_device(dev);
284}
285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287/**
288 * device_initialize - init device structure.
289 * @dev: device.
290 *
291 * This prepares the device for use by other layers,
292 * including adding it to the device hierarchy.
293 * It is the first half of device_register(), if called by
294 * that, though it can also be called separately, so one
295 * may use @dev's fields (e.g. the refcount).
296 */
297
298void device_initialize(struct device *dev)
299{
300 kobj_set_kset_s(dev, devices_subsys);
301 kobject_init(&dev->kobj);
James Bottomley34bb61f2005-09-06 16:56:51 -0700302 klist_init(&dev->klist_children, klist_children_get,
303 klist_children_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 INIT_LIST_HEAD(&dev->dma_pools);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700305 INIT_LIST_HEAD(&dev->node);
mochel@digitalimplant.orgaf703162005-03-21 10:41:04 -0800306 init_MUTEX(&dev->sem);
David Brownell0ac85242005-09-12 19:39:34 -0700307 device_init_wakeup(dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308}
309
310/**
311 * device_add - add device to device hierarchy.
312 * @dev: device.
313 *
314 * This is part 2 of device_register(), though may be called
315 * separately _iff_ device_initialize() has been called separately.
316 *
317 * This adds it to the kobject hierarchy via kobject_add(), adds it
318 * to the global and sibling lists for the device, then
319 * adds it to the other relevant subsystems of the driver model.
320 */
321int device_add(struct device *dev)
322{
323 struct device *parent = NULL;
Greg Kroah-Hartmane9a7d302006-06-20 13:59:20 -0700324 char *class_name = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 int error = -EINVAL;
326
327 dev = get_device(dev);
328 if (!dev || !strlen(dev->bus_id))
329 goto Error;
330
331 parent = get_device(dev->parent);
332
333 pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id);
334
335 /* first, register with generic layer. */
336 kobject_set_name(&dev->kobj, "%s", dev->bus_id);
337 if (parent)
338 dev->kobj.parent = &parent->kobj;
339
340 if ((error = kobject_add(&dev->kobj)))
341 goto Error;
Kay Sieversa7fd6702005-10-01 14:49:43 +0200342
343 dev->uevent_attr.attr.name = "uevent";
344 dev->uevent_attr.attr.mode = S_IWUSR;
345 if (dev->driver)
346 dev->uevent_attr.attr.owner = dev->driver->owner;
347 dev->uevent_attr.store = store_uevent;
348 device_create_file(dev, &dev->uevent_attr);
349
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700350 if (MAJOR(dev->devt)) {
351 struct device_attribute *attr;
352 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
353 if (!attr) {
354 error = -ENOMEM;
355 goto PMError;
356 }
357 attr->attr.name = "dev";
358 attr->attr.mode = S_IRUGO;
359 if (dev->driver)
360 attr->attr.owner = dev->driver->owner;
361 attr->show = show_dev;
362 error = device_create_file(dev, attr);
363 if (error) {
364 kfree(attr);
365 goto attrError;
366 }
367
368 dev->devt_attr = attr;
369 }
370
Kay Sieversb9d9c822006-06-15 15:31:56 +0200371 if (dev->class) {
372 sysfs_create_link(&dev->kobj, &dev->class->subsys.kset.kobj,
373 "subsystem");
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700374 sysfs_create_link(&dev->class->subsys.kset.kobj, &dev->kobj,
375 dev->bus_id);
Greg Kroah-Hartmane9a7d302006-06-20 13:59:20 -0700376
377 sysfs_create_link(&dev->kobj, &dev->parent->kobj, "device");
378 class_name = make_class_name(dev->class->name, &dev->kobj);
379 sysfs_create_link(&dev->parent->kobj, &dev->kobj, class_name);
Kay Sieversb9d9c822006-06-15 15:31:56 +0200380 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700381
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700382 if ((error = device_add_groups(dev)))
383 goto GroupError;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 if ((error = device_pm_add(dev)))
385 goto PMError;
386 if ((error = bus_add_device(dev)))
387 goto BusError;
Kay Sievers53877d02006-04-04 20:42:26 +0200388 kobject_uevent(&dev->kobj, KOBJ_ADD);
389 bus_attach_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 if (parent)
James Bottomleyd856f1e2005-08-19 09:14:01 -0400391 klist_add_tail(&dev->knode_parent, &parent->klist_children);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700393 if (dev->class) {
394 /* tie the class to the device */
395 down(&dev->class->sem);
396 list_add_tail(&dev->node, &dev->class->devices);
397 up(&dev->class->sem);
398 }
399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 /* notify platform of device entry */
401 if (platform_notify)
402 platform_notify(dev);
403 Done:
Greg Kroah-Hartmane9a7d302006-06-20 13:59:20 -0700404 kfree(class_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 put_device(dev);
406 return error;
407 BusError:
408 device_pm_remove(dev);
409 PMError:
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700410 device_remove_groups(dev);
411 GroupError:
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700412 if (dev->devt_attr) {
413 device_remove_file(dev, dev->devt_attr);
414 kfree(dev->devt_attr);
415 }
416 attrError:
Kay Sievers312c0042005-11-16 09:00:00 +0100417 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 kobject_del(&dev->kobj);
419 Error:
420 if (parent)
421 put_device(parent);
422 goto Done;
423}
424
425
426/**
427 * device_register - register a device with the system.
428 * @dev: pointer to the device structure
429 *
430 * This happens in two clean steps - initialize the device
431 * and add it to the system. The two steps can be called
432 * separately, but this is the easiest and most common.
433 * I.e. you should only call the two helpers separately if
434 * have a clearly defined need to use and refcount the device
435 * before it is added to the hierarchy.
436 */
437
438int device_register(struct device *dev)
439{
440 device_initialize(dev);
441 return device_add(dev);
442}
443
444
445/**
446 * get_device - increment reference count for device.
447 * @dev: device.
448 *
449 * This simply forwards the call to kobject_get(), though
450 * we do take care to provide for the case that we get a NULL
451 * pointer passed in.
452 */
453
454struct device * get_device(struct device * dev)
455{
456 return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
457}
458
459
460/**
461 * put_device - decrement reference count.
462 * @dev: device in question.
463 */
464void put_device(struct device * dev)
465{
466 if (dev)
467 kobject_put(&dev->kobj);
468}
469
470
471/**
472 * device_del - delete device from system.
473 * @dev: device.
474 *
475 * This is the first part of the device unregistration
476 * sequence. This removes the device from the lists we control
477 * from here, has it removed from the other driver model
478 * subsystems it was added to in device_add(), and removes it
479 * from the kobject hierarchy.
480 *
481 * NOTE: this should be called manually _iff_ device_add() was
482 * also called manually.
483 */
484
485void device_del(struct device * dev)
486{
487 struct device * parent = dev->parent;
Greg Kroah-Hartmane9a7d302006-06-20 13:59:20 -0700488 char *class_name = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 if (parent)
Patrick Mocheld62c0f92005-06-24 08:39:33 -0700491 klist_del(&dev->knode_parent);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700492 if (dev->devt_attr)
493 device_remove_file(dev, dev->devt_attr);
Kay Sieversb9d9c822006-06-15 15:31:56 +0200494 if (dev->class) {
495 sysfs_remove_link(&dev->kobj, "subsystem");
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700496 sysfs_remove_link(&dev->class->subsys.kset.kobj, dev->bus_id);
Greg Kroah-Hartmane9a7d302006-06-20 13:59:20 -0700497 class_name = make_class_name(dev->class->name, &dev->kobj);
498 sysfs_remove_link(&dev->kobj, "device");
499 sysfs_remove_link(&dev->parent->kobj, class_name);
500 kfree(class_name);
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700501 down(&dev->class->sem);
502 list_del_init(&dev->node);
503 up(&dev->class->sem);
Kay Sieversb9d9c822006-06-15 15:31:56 +0200504 }
Kay Sieversa7fd6702005-10-01 14:49:43 +0200505 device_remove_file(dev, &dev->uevent_attr);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700506 device_remove_groups(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508 /* Notify the platform of the removal, in case they
509 * need to do anything...
510 */
511 if (platform_notify_remove)
512 platform_notify_remove(dev);
513 bus_remove_device(dev);
514 device_pm_remove(dev);
Kay Sievers312c0042005-11-16 09:00:00 +0100515 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 kobject_del(&dev->kobj);
517 if (parent)
518 put_device(parent);
519}
520
521/**
522 * device_unregister - unregister device from system.
523 * @dev: device going away.
524 *
525 * We do this in two parts, like we do device_register(). First,
526 * we remove it from all the subsystems with device_del(), then
527 * we decrement the reference count via put_device(). If that
528 * is the final reference count, the device will be cleaned up
529 * via device_release() above. Otherwise, the structure will
530 * stick around until the final reference to the device is dropped.
531 */
532void device_unregister(struct device * dev)
533{
534 pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id);
535 device_del(dev);
536 put_device(dev);
537}
538
539
mochel@digitalimplant.org36239572005-03-24 19:08:30 -0800540static struct device * next_device(struct klist_iter * i)
541{
542 struct klist_node * n = klist_next(i);
543 return n ? container_of(n, struct device, knode_parent) : NULL;
544}
545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546/**
547 * device_for_each_child - device child iterator.
Randy Dunlapc41455f2005-10-23 11:59:14 -0700548 * @parent: parent struct device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 * @data: data for the callback.
550 * @fn: function to be called for each device.
551 *
Randy Dunlapc41455f2005-10-23 11:59:14 -0700552 * Iterate over @parent's child devices, and call @fn for each,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 * passing it @data.
554 *
555 * We check the return of @fn each time. If it returns anything
556 * other than 0, we break out and return that value.
557 */
mochel@digitalimplant.org36239572005-03-24 19:08:30 -0800558int device_for_each_child(struct device * parent, void * data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 int (*fn)(struct device *, void *))
560{
mochel@digitalimplant.org36239572005-03-24 19:08:30 -0800561 struct klist_iter i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 struct device * child;
563 int error = 0;
564
mochel@digitalimplant.org36239572005-03-24 19:08:30 -0800565 klist_iter_init(&parent->klist_children, &i);
566 while ((child = next_device(&i)) && !error)
567 error = fn(child, data);
568 klist_iter_exit(&i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 return error;
570}
571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572int __init devices_init(void)
573{
574 return subsystem_register(&devices_subsys);
575}
576
577EXPORT_SYMBOL_GPL(device_for_each_child);
578
579EXPORT_SYMBOL_GPL(device_initialize);
580EXPORT_SYMBOL_GPL(device_add);
581EXPORT_SYMBOL_GPL(device_register);
582
583EXPORT_SYMBOL_GPL(device_del);
584EXPORT_SYMBOL_GPL(device_unregister);
585EXPORT_SYMBOL_GPL(get_device);
586EXPORT_SYMBOL_GPL(put_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588EXPORT_SYMBOL_GPL(device_create_file);
589EXPORT_SYMBOL_GPL(device_remove_file);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700590
591
592static void device_create_release(struct device *dev)
593{
594 pr_debug("%s called for %s\n", __FUNCTION__, dev->bus_id);
595 kfree(dev);
596}
597
598/**
599 * device_create - creates a device and registers it with sysfs
Henrik Kretzschmar42734da2006-07-05 00:53:19 +0200600 * @class: pointer to the struct class that this device should be registered to
601 * @parent: pointer to the parent struct device of this new device, if any
602 * @devt: the dev_t for the char device to be added
603 * @fmt: string for the device's name
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700604 *
Henrik Kretzschmar42734da2006-07-05 00:53:19 +0200605 * This function can be used by char device classes. A struct device
606 * will be created in sysfs, registered to the specified class.
607 *
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700608 * A "dev" file will be created, showing the dev_t for the device, if
609 * the dev_t is not 0,0.
Henrik Kretzschmar42734da2006-07-05 00:53:19 +0200610 * If a pointer to a parent struct device is passed in, the newly created
611 * struct device will be a child of that device in sysfs.
612 * The pointer to the struct device will be returned from the call.
613 * Any further sysfs files that might be required can be created using this
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700614 * pointer.
615 *
616 * Note: the struct class passed to this function must have previously
617 * been created with a call to class_create().
618 */
619struct device *device_create(struct class *class, struct device *parent,
Greg Kroah-Hartman5cbe5f82006-08-10 01:19:19 -0400620 dev_t devt, const char *fmt, ...)
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700621{
622 va_list args;
623 struct device *dev = NULL;
624 int retval = -ENODEV;
625
626 if (class == NULL || IS_ERR(class))
627 goto error;
628 if (parent == NULL) {
629 printk(KERN_WARNING "%s does not work yet for NULL parents\n", __FUNCTION__);
630 goto error;
631 }
632
633 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
634 if (!dev) {
635 retval = -ENOMEM;
636 goto error;
637 }
638
639 dev->devt = devt;
640 dev->class = class;
641 dev->parent = parent;
642 dev->release = device_create_release;
643
644 va_start(args, fmt);
645 vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args);
646 va_end(args);
647 retval = device_register(dev);
648 if (retval)
649 goto error;
650
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700651 return dev;
652
653error:
654 kfree(dev);
655 return ERR_PTR(retval);
656}
657EXPORT_SYMBOL_GPL(device_create);
658
659/**
660 * device_destroy - removes a device that was created with device_create()
Henrik Kretzschmar42734da2006-07-05 00:53:19 +0200661 * @class: pointer to the struct class that this device was registered with
662 * @devt: the dev_t of the device that was previously registered
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700663 *
Henrik Kretzschmar42734da2006-07-05 00:53:19 +0200664 * This call unregisters and cleans up a device that was created with a
665 * call to device_create().
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700666 */
667void device_destroy(struct class *class, dev_t devt)
668{
669 struct device *dev = NULL;
670 struct device *dev_tmp;
671
672 down(&class->sem);
673 list_for_each_entry(dev_tmp, &class->devices, node) {
674 if (dev_tmp->devt == devt) {
675 dev = dev_tmp;
676 break;
677 }
678 }
679 up(&class->sem);
680
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700681 if (dev)
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700682 device_unregister(dev);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700683}
684EXPORT_SYMBOL_GPL(device_destroy);