2 * drivers/base/core.c - core driver model code (device registration, etc)
4 * Copyright (c) 2002-3 Patrick Mochel
5 * Copyright (c) 2002-3 Open Source Development Labs
7 * This file is released under the GPLv2
11 #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>
17 #include <linux/kdev_t.h>
19 #include <asm/semaphore.h>
22 #include "power/power.h"
24 int (*platform_notify)(struct device * dev) = NULL;
25 int (*platform_notify_remove)(struct device * dev) = NULL;
28 * sysfs bindings for devices.
32 * dev_driver_string - Return a device's driver name, if at all possible
33 * @dev: struct device to get the name of
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.
40 const char *dev_driver_string(struct device *dev)
42 return dev->driver ? dev->driver->name :
43 (dev->bus ? dev->bus->name : "");
45 EXPORT_SYMBOL_GPL(dev_driver_string);
47 #define to_dev(obj) container_of(obj, struct device, kobj)
48 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
51 dev_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
53 struct device_attribute * dev_attr = to_dev_attr(attr);
54 struct device * dev = to_dev(kobj);
58 ret = dev_attr->show(dev, dev_attr, buf);
63 dev_attr_store(struct kobject * kobj, struct attribute * attr,
64 const char * buf, size_t count)
66 struct device_attribute * dev_attr = to_dev_attr(attr);
67 struct device * dev = to_dev(kobj);
71 ret = dev_attr->store(dev, dev_attr, buf, count);
75 static struct sysfs_ops dev_sysfs_ops = {
76 .show = dev_attr_show,
77 .store = dev_attr_store,
82 * device_release - free device structure.
83 * @kobj: device's kobject.
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.
89 static void device_release(struct kobject * kobj)
91 struct device * dev = to_dev(kobj);
96 printk(KERN_ERR "Device '%s' does not have a release() function, "
97 "it is broken and must be fixed.\n",
103 static struct kobj_type ktype_device = {
104 .release = device_release,
105 .sysfs_ops = &dev_sysfs_ops,
109 static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
111 struct kobj_type *ktype = get_ktype(kobj);
113 if (ktype == &ktype_device) {
114 struct device *dev = to_dev(kobj);
123 static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
125 struct device *dev = to_dev(kobj);
128 return dev->bus->name;
130 return dev->class->name;
134 static int dev_uevent(struct kset *kset, struct kobject *kobj, char **envp,
135 int num_envp, char *buffer, int buffer_size)
137 struct device *dev = to_dev(kobj);
142 /* 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));
152 /* add bus name (same as SUBSYSTEM, deprecated) */
154 add_uevent_var(envp, num_envp, &i,
155 buffer, buffer_size, &length,
156 "PHYSDEVBUS=%s", dev->bus->name);
158 /* add driver name (PHYSDEV* values are deprecated)*/
160 add_uevent_var(envp, num_envp, &i,
161 buffer, buffer_size, &length,
162 "DRIVER=%s", dev->driver->name);
163 add_uevent_var(envp, num_envp, &i,
164 buffer, buffer_size, &length,
165 "PHYSDEVDRIVER=%s", dev->driver->name);
168 /* terminate, set to next free slot, shrink available space */
172 buffer = &buffer[length];
173 buffer_size -= length;
175 if (dev->bus && dev->bus->uevent) {
176 /* have the bus specific function add its stuff */
177 retval = dev->bus->uevent(dev, envp, num_envp, buffer, buffer_size);
179 pr_debug ("%s - uevent() returned %d\n",
180 __FUNCTION__, retval);
187 static struct kset_uevent_ops device_uevent_ops = {
188 .filter = dev_uevent_filter,
189 .name = dev_uevent_name,
190 .uevent = dev_uevent,
193 static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
194 const char *buf, size_t count)
196 kobject_uevent(&dev->kobj, KOBJ_ADD);
200 static int device_add_groups(struct device *dev)
206 for (i = 0; dev->groups[i]; i++) {
207 error = sysfs_create_group(&dev->kobj, dev->groups[i]);
210 sysfs_remove_group(&dev->kobj, dev->groups[i]);
219 static void device_remove_groups(struct device *dev)
223 for (i = 0; dev->groups[i]; i++) {
224 sysfs_remove_group(&dev->kobj, dev->groups[i]);
229 static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
232 return print_dev_t(buf, dev->devt);
236 * devices_subsys - structure to be registered with kobject core.
239 decl_subsys(devices, &ktype_device, &device_uevent_ops);
243 * device_create_file - create sysfs attribute file for device.
245 * @attr: device attribute descriptor.
248 int device_create_file(struct device * dev, struct device_attribute * attr)
251 if (get_device(dev)) {
252 error = sysfs_create_file(&dev->kobj, &attr->attr);
259 * device_remove_file - remove sysfs attribute file.
261 * @attr: device attribute descriptor.
264 void device_remove_file(struct device * dev, struct device_attribute * attr)
266 if (get_device(dev)) {
267 sysfs_remove_file(&dev->kobj, &attr->attr);
272 static void klist_children_get(struct klist_node *n)
274 struct device *dev = container_of(n, struct device, knode_parent);
279 static void klist_children_put(struct klist_node *n)
281 struct device *dev = container_of(n, struct device, knode_parent);
288 * device_initialize - init device structure.
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).
298 void device_initialize(struct device *dev)
300 kobj_set_kset_s(dev, devices_subsys);
301 kobject_init(&dev->kobj);
302 klist_init(&dev->klist_children, klist_children_get,
304 INIT_LIST_HEAD(&dev->dma_pools);
305 INIT_LIST_HEAD(&dev->node);
306 init_MUTEX(&dev->sem);
307 device_init_wakeup(dev, 0);
311 * device_add - add device to device hierarchy.
314 * This is part 2 of device_register(), though may be called
315 * separately _iff_ device_initialize() has been called separately.
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.
321 int device_add(struct device *dev)
323 struct device *parent = NULL;
324 char *class_name = NULL;
327 dev = get_device(dev);
328 if (!dev || !strlen(dev->bus_id))
331 parent = get_device(dev->parent);
333 pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id);
335 /* first, register with generic layer. */
336 kobject_set_name(&dev->kobj, "%s", dev->bus_id);
338 dev->kobj.parent = &parent->kobj;
340 if ((error = kobject_add(&dev->kobj)))
343 dev->uevent_attr.attr.name = "uevent";
344 dev->uevent_attr.attr.mode = S_IWUSR;
346 dev->uevent_attr.attr.owner = dev->driver->owner;
347 dev->uevent_attr.store = store_uevent;
348 device_create_file(dev, &dev->uevent_attr);
350 if (MAJOR(dev->devt)) {
351 struct device_attribute *attr;
352 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
357 attr->attr.name = "dev";
358 attr->attr.mode = S_IRUGO;
360 attr->attr.owner = dev->driver->owner;
361 attr->show = show_dev;
362 error = device_create_file(dev, attr);
368 dev->devt_attr = attr;
372 sysfs_create_link(&dev->kobj, &dev->class->subsys.kset.kobj,
374 sysfs_create_link(&dev->class->subsys.kset.kobj, &dev->kobj,
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);
382 if ((error = device_add_groups(dev)))
384 if ((error = device_pm_add(dev)))
386 if ((error = bus_add_device(dev)))
388 kobject_uevent(&dev->kobj, KOBJ_ADD);
389 bus_attach_device(dev);
391 klist_add_tail(&dev->knode_parent, &parent->klist_children);
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);
400 /* notify platform of device entry */
402 platform_notify(dev);
408 device_pm_remove(dev);
410 device_remove_groups(dev);
412 if (dev->devt_attr) {
413 device_remove_file(dev, dev->devt_attr);
414 kfree(dev->devt_attr);
417 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
418 kobject_del(&dev->kobj);
427 * device_register - register a device with the system.
428 * @dev: pointer to the device structure
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.
438 int device_register(struct device *dev)
440 device_initialize(dev);
441 return device_add(dev);
446 * get_device - increment reference count for device.
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
454 struct device * get_device(struct device * dev)
456 return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
461 * put_device - decrement reference count.
462 * @dev: device in question.
464 void put_device(struct device * dev)
467 kobject_put(&dev->kobj);
472 * device_del - delete device from system.
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.
481 * NOTE: this should be called manually _iff_ device_add() was
482 * also called manually.
485 void device_del(struct device * dev)
487 struct device * parent = dev->parent;
488 char *class_name = NULL;
491 klist_del(&dev->knode_parent);
493 device_remove_file(dev, dev->devt_attr);
495 sysfs_remove_link(&dev->kobj, "subsystem");
496 sysfs_remove_link(&dev->class->subsys.kset.kobj, dev->bus_id);
497 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);
501 down(&dev->class->sem);
502 list_del_init(&dev->node);
503 up(&dev->class->sem);
505 device_remove_file(dev, &dev->uevent_attr);
506 device_remove_groups(dev);
508 /* Notify the platform of the removal, in case they
509 * need to do anything...
511 if (platform_notify_remove)
512 platform_notify_remove(dev);
513 bus_remove_device(dev);
514 device_pm_remove(dev);
515 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
516 kobject_del(&dev->kobj);
522 * device_unregister - unregister device from system.
523 * @dev: device going away.
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.
532 void device_unregister(struct device * dev)
534 pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id);
540 static struct device * next_device(struct klist_iter * i)
542 struct klist_node * n = klist_next(i);
543 return n ? container_of(n, struct device, knode_parent) : NULL;
547 * device_for_each_child - device child iterator.
548 * @parent: parent struct device.
549 * @data: data for the callback.
550 * @fn: function to be called for each device.
552 * Iterate over @parent's child devices, and call @fn for each,
555 * We check the return of @fn each time. If it returns anything
556 * other than 0, we break out and return that value.
558 int device_for_each_child(struct device * parent, void * data,
559 int (*fn)(struct device *, void *))
562 struct device * child;
565 klist_iter_init(&parent->klist_children, &i);
566 while ((child = next_device(&i)) && !error)
567 error = fn(child, data);
572 int __init devices_init(void)
574 return subsystem_register(&devices_subsys);
577 EXPORT_SYMBOL_GPL(device_for_each_child);
579 EXPORT_SYMBOL_GPL(device_initialize);
580 EXPORT_SYMBOL_GPL(device_add);
581 EXPORT_SYMBOL_GPL(device_register);
583 EXPORT_SYMBOL_GPL(device_del);
584 EXPORT_SYMBOL_GPL(device_unregister);
585 EXPORT_SYMBOL_GPL(get_device);
586 EXPORT_SYMBOL_GPL(put_device);
588 EXPORT_SYMBOL_GPL(device_create_file);
589 EXPORT_SYMBOL_GPL(device_remove_file);
592 static void device_create_release(struct device *dev)
594 pr_debug("%s called for %s\n", __FUNCTION__, dev->bus_id);
599 * device_create - creates a device and registers it with sysfs
600 * @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
605 * This function can be used by char device classes. A struct device
606 * will be created in sysfs, registered to the specified class.
608 * A "dev" file will be created, showing the dev_t for the device, if
609 * the dev_t is not 0,0.
610 * 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
616 * Note: the struct class passed to this function must have previously
617 * been created with a call to class_create().
619 struct device *device_create(struct class *class, struct device *parent,
620 dev_t devt, const char *fmt, ...)
623 struct device *dev = NULL;
624 int retval = -ENODEV;
626 if (class == NULL || IS_ERR(class))
628 if (parent == NULL) {
629 printk(KERN_WARNING "%s does not work yet for NULL parents\n", __FUNCTION__);
633 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
641 dev->parent = parent;
642 dev->release = device_create_release;
645 vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args);
647 retval = device_register(dev);
655 return ERR_PTR(retval);
657 EXPORT_SYMBOL_GPL(device_create);
660 * device_destroy - removes a device that was created with device_create()
661 * @class: pointer to the struct class that this device was registered with
662 * @devt: the dev_t of the device that was previously registered
664 * This call unregisters and cleans up a device that was created with a
665 * call to device_create().
667 void device_destroy(struct class *class, dev_t devt)
669 struct device *dev = NULL;
670 struct device *dev_tmp;
673 list_for_each_entry(dev_tmp, &class->devices, node) {
674 if (dev_tmp->devt == devt) {
682 device_unregister(dev);
684 EXPORT_SYMBOL_GPL(device_destroy);