[PATCH] remove protection of LANANA-reserved majors
[linux-2.6/mini2440.git] / drivers / base / core.c
blobd7fcf823a42a71e08af9da4e62d83b0dae5129fd
1 /*
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
6 * Copyright (c) 2006 Greg Kroah-Hartman <gregkh@suse.de>
7 * Copyright (c) 2006 Novell, Inc.
9 * This file is released under the GPLv2
13 #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>
19 #include <linux/kdev_t.h>
20 #include <linux/notifier.h>
22 #include <asm/semaphore.h>
24 #include "base.h"
25 #include "power/power.h"
27 int (*platform_notify)(struct device * dev) = NULL;
28 int (*platform_notify_remove)(struct device * dev) = NULL;
31 * sysfs bindings for devices.
34 /**
35 * dev_driver_string - Return a device's driver name, if at all possible
36 * @dev: struct device to get the name of
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.
43 const char *dev_driver_string(struct device *dev)
45 return dev->driver ? dev->driver->name :
46 (dev->bus ? dev->bus->name : "");
48 EXPORT_SYMBOL(dev_driver_string);
50 #define to_dev(obj) container_of(obj, struct device, kobj)
51 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
53 static ssize_t
54 dev_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
56 struct device_attribute * dev_attr = to_dev_attr(attr);
57 struct device * dev = to_dev(kobj);
58 ssize_t ret = -EIO;
60 if (dev_attr->show)
61 ret = dev_attr->show(dev, dev_attr, buf);
62 return ret;
65 static ssize_t
66 dev_attr_store(struct kobject * kobj, struct attribute * attr,
67 const char * buf, size_t count)
69 struct device_attribute * dev_attr = to_dev_attr(attr);
70 struct device * dev = to_dev(kobj);
71 ssize_t ret = -EIO;
73 if (dev_attr->store)
74 ret = dev_attr->store(dev, dev_attr, buf, count);
75 return ret;
78 static struct sysfs_ops dev_sysfs_ops = {
79 .show = dev_attr_show,
80 .store = dev_attr_store,
84 /**
85 * device_release - free device structure.
86 * @kobj: device's kobject.
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.
92 static void device_release(struct kobject * kobj)
94 struct device * dev = to_dev(kobj);
96 if (dev->release)
97 dev->release(dev);
98 else if (dev->type && dev->type->release)
99 dev->type->release(dev);
100 else if (dev->class && dev->class->dev_release)
101 dev->class->dev_release(dev);
102 else {
103 printk(KERN_ERR "Device '%s' does not have a release() function, "
104 "it is broken and must be fixed.\n",
105 dev->bus_id);
106 WARN_ON(1);
110 static struct kobj_type ktype_device = {
111 .release = device_release,
112 .sysfs_ops = &dev_sysfs_ops,
116 static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
118 struct kobj_type *ktype = get_ktype(kobj);
120 if (ktype == &ktype_device) {
121 struct device *dev = to_dev(kobj);
122 if (dev->bus)
123 return 1;
124 if (dev->class)
125 return 1;
127 return 0;
130 static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
132 struct device *dev = to_dev(kobj);
134 if (dev->bus)
135 return dev->bus->name;
136 if (dev->class)
137 return dev->class->name;
138 return NULL;
141 static int dev_uevent(struct kset *kset, struct kobject *kobj, char **envp,
142 int num_envp, char *buffer, int buffer_size)
144 struct device *dev = to_dev(kobj);
145 int i = 0;
146 int length = 0;
147 int retval = 0;
149 /* add the major/minor if present */
150 if (MAJOR(dev->devt)) {
151 add_uevent_var(envp, num_envp, &i,
152 buffer, buffer_size, &length,
153 "MAJOR=%u", MAJOR(dev->devt));
154 add_uevent_var(envp, num_envp, &i,
155 buffer, buffer_size, &length,
156 "MINOR=%u", MINOR(dev->devt));
159 if (dev->driver)
160 add_uevent_var(envp, num_envp, &i,
161 buffer, buffer_size, &length,
162 "DRIVER=%s", dev->driver->name);
164 #ifdef CONFIG_SYSFS_DEPRECATED
165 if (dev->class) {
166 struct device *parent = dev->parent;
168 /* find first bus device in parent chain */
169 while (parent && !parent->bus)
170 parent = parent->parent;
171 if (parent && parent->bus) {
172 const char *path;
174 path = kobject_get_path(&parent->kobj, GFP_KERNEL);
175 add_uevent_var(envp, num_envp, &i,
176 buffer, buffer_size, &length,
177 "PHYSDEVPATH=%s", path);
178 kfree(path);
180 add_uevent_var(envp, num_envp, &i,
181 buffer, buffer_size, &length,
182 "PHYSDEVBUS=%s", parent->bus->name);
184 if (parent->driver)
185 add_uevent_var(envp, num_envp, &i,
186 buffer, buffer_size, &length,
187 "PHYSDEVDRIVER=%s", parent->driver->name);
189 } else if (dev->bus) {
190 add_uevent_var(envp, num_envp, &i,
191 buffer, buffer_size, &length,
192 "PHYSDEVBUS=%s", dev->bus->name);
194 if (dev->driver)
195 add_uevent_var(envp, num_envp, &i,
196 buffer, buffer_size, &length,
197 "PHYSDEVDRIVER=%s", dev->driver->name);
199 #endif
201 /* terminate, set to next free slot, shrink available space */
202 envp[i] = NULL;
203 envp = &envp[i];
204 num_envp -= i;
205 buffer = &buffer[length];
206 buffer_size -= length;
208 if (dev->bus && dev->bus->uevent) {
209 /* have the bus specific function add its stuff */
210 retval = dev->bus->uevent(dev, envp, num_envp, buffer, buffer_size);
211 if (retval)
212 pr_debug ("%s: bus uevent() returned %d\n",
213 __FUNCTION__, retval);
216 if (dev->class && dev->class->dev_uevent) {
217 /* have the class specific function add its stuff */
218 retval = dev->class->dev_uevent(dev, envp, num_envp, buffer, buffer_size);
219 if (retval)
220 pr_debug("%s: class uevent() returned %d\n",
221 __FUNCTION__, retval);
224 if (dev->type && dev->type->uevent) {
225 /* have the device type specific fuction add its stuff */
226 retval = dev->type->uevent(dev, envp, num_envp, buffer, buffer_size);
227 if (retval)
228 pr_debug("%s: dev_type uevent() returned %d\n",
229 __FUNCTION__, retval);
232 return retval;
235 static struct kset_uevent_ops device_uevent_ops = {
236 .filter = dev_uevent_filter,
237 .name = dev_uevent_name,
238 .uevent = dev_uevent,
241 static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
242 const char *buf, size_t count)
244 kobject_uevent(&dev->kobj, KOBJ_ADD);
245 return count;
248 static int device_add_groups(struct device *dev)
250 int i;
251 int error = 0;
253 if (dev->groups) {
254 for (i = 0; dev->groups[i]; i++) {
255 error = sysfs_create_group(&dev->kobj, dev->groups[i]);
256 if (error) {
257 while (--i >= 0)
258 sysfs_remove_group(&dev->kobj, dev->groups[i]);
259 goto out;
263 out:
264 return error;
267 static void device_remove_groups(struct device *dev)
269 int i;
270 if (dev->groups) {
271 for (i = 0; dev->groups[i]; i++) {
272 sysfs_remove_group(&dev->kobj, dev->groups[i]);
277 static int device_add_attrs(struct device *dev)
279 struct class *class = dev->class;
280 struct device_type *type = dev->type;
281 int error = 0;
282 int i;
284 if (class && class->dev_attrs) {
285 for (i = 0; attr_name(class->dev_attrs[i]); i++) {
286 error = device_create_file(dev, &class->dev_attrs[i]);
287 if (error)
288 break;
290 if (error)
291 while (--i >= 0)
292 device_remove_file(dev, &class->dev_attrs[i]);
295 if (type && type->attrs) {
296 for (i = 0; attr_name(type->attrs[i]); i++) {
297 error = device_create_file(dev, &type->attrs[i]);
298 if (error)
299 break;
301 if (error)
302 while (--i >= 0)
303 device_remove_file(dev, &type->attrs[i]);
306 return error;
309 static void device_remove_attrs(struct device *dev)
311 struct class *class = dev->class;
312 struct device_type *type = dev->type;
313 int i;
315 if (class && class->dev_attrs) {
316 for (i = 0; attr_name(class->dev_attrs[i]); i++)
317 device_remove_file(dev, &class->dev_attrs[i]);
320 if (type && type->attrs) {
321 for (i = 0; attr_name(type->attrs[i]); i++)
322 device_remove_file(dev, &type->attrs[i]);
327 static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
328 char *buf)
330 return print_dev_t(buf, dev->devt);
334 * devices_subsys - structure to be registered with kobject core.
337 decl_subsys(devices, &ktype_device, &device_uevent_ops);
341 * device_create_file - create sysfs attribute file for device.
342 * @dev: device.
343 * @attr: device attribute descriptor.
346 int device_create_file(struct device * dev, struct device_attribute * attr)
348 int error = 0;
349 if (get_device(dev)) {
350 error = sysfs_create_file(&dev->kobj, &attr->attr);
351 put_device(dev);
353 return error;
357 * device_remove_file - remove sysfs attribute file.
358 * @dev: device.
359 * @attr: device attribute descriptor.
362 void device_remove_file(struct device * dev, struct device_attribute * attr)
364 if (get_device(dev)) {
365 sysfs_remove_file(&dev->kobj, &attr->attr);
366 put_device(dev);
371 * device_create_bin_file - create sysfs binary attribute file for device.
372 * @dev: device.
373 * @attr: device binary attribute descriptor.
375 int device_create_bin_file(struct device *dev, struct bin_attribute *attr)
377 int error = -EINVAL;
378 if (dev)
379 error = sysfs_create_bin_file(&dev->kobj, attr);
380 return error;
382 EXPORT_SYMBOL_GPL(device_create_bin_file);
385 * device_remove_bin_file - remove sysfs binary attribute file
386 * @dev: device.
387 * @attr: device binary attribute descriptor.
389 void device_remove_bin_file(struct device *dev, struct bin_attribute *attr)
391 if (dev)
392 sysfs_remove_bin_file(&dev->kobj, attr);
394 EXPORT_SYMBOL_GPL(device_remove_bin_file);
397 * device_schedule_callback - helper to schedule a callback for a device
398 * @dev: device.
399 * @func: callback function to invoke later.
401 * Attribute methods must not unregister themselves or their parent device
402 * (which would amount to the same thing). Attempts to do so will deadlock,
403 * since unregistration is mutually exclusive with driver callbacks.
405 * Instead methods can call this routine, which will attempt to allocate
406 * and schedule a workqueue request to call back @func with @dev as its
407 * argument in the workqueue's process context. @dev will be pinned until
408 * @func returns.
410 * Returns 0 if the request was submitted, -ENOMEM if storage could not
411 * be allocated.
413 * NOTE: This routine won't work if CONFIG_SYSFS isn't set! It uses an
414 * underlying sysfs routine (since it is intended for use by attribute
415 * methods), and if sysfs isn't available you'll get nothing but -ENOSYS.
417 int device_schedule_callback(struct device *dev,
418 void (*func)(struct device *))
420 return sysfs_schedule_callback(&dev->kobj,
421 (void (*)(void *)) func, dev);
423 EXPORT_SYMBOL_GPL(device_schedule_callback);
425 static void klist_children_get(struct klist_node *n)
427 struct device *dev = container_of(n, struct device, knode_parent);
429 get_device(dev);
432 static void klist_children_put(struct klist_node *n)
434 struct device *dev = container_of(n, struct device, knode_parent);
436 put_device(dev);
441 * device_initialize - init device structure.
442 * @dev: device.
444 * This prepares the device for use by other layers,
445 * including adding it to the device hierarchy.
446 * It is the first half of device_register(), if called by
447 * that, though it can also be called separately, so one
448 * may use @dev's fields (e.g. the refcount).
451 void device_initialize(struct device *dev)
453 kobj_set_kset_s(dev, devices_subsys);
454 kobject_init(&dev->kobj);
455 klist_init(&dev->klist_children, klist_children_get,
456 klist_children_put);
457 INIT_LIST_HEAD(&dev->dma_pools);
458 INIT_LIST_HEAD(&dev->node);
459 init_MUTEX(&dev->sem);
460 spin_lock_init(&dev->devres_lock);
461 INIT_LIST_HEAD(&dev->devres_head);
462 device_init_wakeup(dev, 0);
463 set_dev_node(dev, -1);
466 #ifdef CONFIG_SYSFS_DEPRECATED
467 static struct kobject * get_device_parent(struct device *dev,
468 struct device *parent)
470 /* Set the parent to the class, not the parent device */
471 /* this keeps sysfs from having a symlink to make old udevs happy */
472 if (dev->class)
473 return &dev->class->subsys.kset.kobj;
474 else if (parent)
475 return &parent->kobj;
477 return NULL;
479 #else
480 static struct kobject * virtual_device_parent(struct device *dev)
482 if (!dev->class)
483 return ERR_PTR(-ENODEV);
485 if (!dev->class->virtual_dir) {
486 static struct kobject *virtual_dir = NULL;
488 if (!virtual_dir)
489 virtual_dir = kobject_add_dir(&devices_subsys.kset.kobj, "virtual");
490 dev->class->virtual_dir = kobject_add_dir(virtual_dir, dev->class->name);
493 return dev->class->virtual_dir;
496 static struct kobject * get_device_parent(struct device *dev,
497 struct device *parent)
499 /* if this is a class device, and has no parent, create one */
500 if ((dev->class) && (parent == NULL)) {
501 return virtual_device_parent(dev);
502 } else if (parent)
503 return &parent->kobj;
504 return NULL;
507 #endif
508 static int setup_parent(struct device *dev, struct device *parent)
510 struct kobject *kobj;
511 kobj = get_device_parent(dev, parent);
512 if (IS_ERR(kobj))
513 return PTR_ERR(kobj);
514 if (kobj)
515 dev->kobj.parent = kobj;
516 return 0;
520 * device_add - add device to device hierarchy.
521 * @dev: device.
523 * This is part 2 of device_register(), though may be called
524 * separately _iff_ device_initialize() has been called separately.
526 * This adds it to the kobject hierarchy via kobject_add(), adds it
527 * to the global and sibling lists for the device, then
528 * adds it to the other relevant subsystems of the driver model.
530 int device_add(struct device *dev)
532 struct device *parent = NULL;
533 char *class_name = NULL;
534 struct class_interface *class_intf;
535 int error = -EINVAL;
537 dev = get_device(dev);
538 if (!dev || !strlen(dev->bus_id))
539 goto Error;
541 pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id);
543 parent = get_device(dev->parent);
545 error = setup_parent(dev, parent);
546 if (error)
547 goto Error;
549 /* first, register with generic layer. */
550 kobject_set_name(&dev->kobj, "%s", dev->bus_id);
551 error = kobject_add(&dev->kobj);
552 if (error)
553 goto Error;
555 /* notify platform of device entry */
556 if (platform_notify)
557 platform_notify(dev);
559 /* notify clients of device entry (new way) */
560 if (dev->bus)
561 blocking_notifier_call_chain(&dev->bus->bus_notifier,
562 BUS_NOTIFY_ADD_DEVICE, dev);
564 dev->uevent_attr.attr.name = "uevent";
565 dev->uevent_attr.attr.mode = S_IWUSR;
566 if (dev->driver)
567 dev->uevent_attr.attr.owner = dev->driver->owner;
568 dev->uevent_attr.store = store_uevent;
569 error = device_create_file(dev, &dev->uevent_attr);
570 if (error)
571 goto attrError;
573 if (MAJOR(dev->devt)) {
574 struct device_attribute *attr;
575 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
576 if (!attr) {
577 error = -ENOMEM;
578 goto ueventattrError;
580 attr->attr.name = "dev";
581 attr->attr.mode = S_IRUGO;
582 if (dev->driver)
583 attr->attr.owner = dev->driver->owner;
584 attr->show = show_dev;
585 error = device_create_file(dev, attr);
586 if (error) {
587 kfree(attr);
588 goto ueventattrError;
591 dev->devt_attr = attr;
594 if (dev->class) {
595 sysfs_create_link(&dev->kobj, &dev->class->subsys.kset.kobj,
596 "subsystem");
597 /* If this is not a "fake" compatible device, then create the
598 * symlink from the class to the device. */
599 if (dev->kobj.parent != &dev->class->subsys.kset.kobj)
600 sysfs_create_link(&dev->class->subsys.kset.kobj,
601 &dev->kobj, dev->bus_id);
602 if (parent) {
603 sysfs_create_link(&dev->kobj, &dev->parent->kobj,
604 "device");
605 #ifdef CONFIG_SYSFS_DEPRECATED
606 class_name = make_class_name(dev->class->name,
607 &dev->kobj);
608 if (class_name)
609 sysfs_create_link(&dev->parent->kobj,
610 &dev->kobj, class_name);
611 #endif
615 if ((error = device_add_attrs(dev)))
616 goto AttrsError;
617 if ((error = device_add_groups(dev)))
618 goto GroupError;
619 if ((error = device_pm_add(dev)))
620 goto PMError;
621 if ((error = bus_add_device(dev)))
622 goto BusError;
623 if (!dev->uevent_suppress)
624 kobject_uevent(&dev->kobj, KOBJ_ADD);
625 if ((error = bus_attach_device(dev)))
626 goto AttachError;
627 if (parent)
628 klist_add_tail(&dev->knode_parent, &parent->klist_children);
630 if (dev->class) {
631 down(&dev->class->sem);
632 /* tie the class to the device */
633 list_add_tail(&dev->node, &dev->class->devices);
635 /* notify any interfaces that the device is here */
636 list_for_each_entry(class_intf, &dev->class->interfaces, node)
637 if (class_intf->add_dev)
638 class_intf->add_dev(dev, class_intf);
639 up(&dev->class->sem);
641 Done:
642 kfree(class_name);
643 put_device(dev);
644 return error;
645 AttachError:
646 bus_remove_device(dev);
647 BusError:
648 device_pm_remove(dev);
649 PMError:
650 if (dev->bus)
651 blocking_notifier_call_chain(&dev->bus->bus_notifier,
652 BUS_NOTIFY_DEL_DEVICE, dev);
653 device_remove_groups(dev);
654 GroupError:
655 device_remove_attrs(dev);
656 AttrsError:
657 if (dev->devt_attr) {
658 device_remove_file(dev, dev->devt_attr);
659 kfree(dev->devt_attr);
662 if (dev->class) {
663 sysfs_remove_link(&dev->kobj, "subsystem");
664 /* If this is not a "fake" compatible device, remove the
665 * symlink from the class to the device. */
666 if (dev->kobj.parent != &dev->class->subsys.kset.kobj)
667 sysfs_remove_link(&dev->class->subsys.kset.kobj,
668 dev->bus_id);
669 if (parent) {
670 #ifdef CONFIG_SYSFS_DEPRECATED
671 char *class_name = make_class_name(dev->class->name,
672 &dev->kobj);
673 if (class_name)
674 sysfs_remove_link(&dev->parent->kobj,
675 class_name);
676 kfree(class_name);
677 #endif
678 sysfs_remove_link(&dev->kobj, "device");
681 down(&dev->class->sem);
682 /* notify any interfaces that the device is now gone */
683 list_for_each_entry(class_intf, &dev->class->interfaces, node)
684 if (class_intf->remove_dev)
685 class_intf->remove_dev(dev, class_intf);
686 /* remove the device from the class list */
687 list_del_init(&dev->node);
688 up(&dev->class->sem);
690 ueventattrError:
691 device_remove_file(dev, &dev->uevent_attr);
692 attrError:
693 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
694 kobject_del(&dev->kobj);
695 Error:
696 if (parent)
697 put_device(parent);
698 goto Done;
703 * device_register - register a device with the system.
704 * @dev: pointer to the device structure
706 * This happens in two clean steps - initialize the device
707 * and add it to the system. The two steps can be called
708 * separately, but this is the easiest and most common.
709 * I.e. you should only call the two helpers separately if
710 * have a clearly defined need to use and refcount the device
711 * before it is added to the hierarchy.
714 int device_register(struct device *dev)
716 device_initialize(dev);
717 return device_add(dev);
722 * get_device - increment reference count for device.
723 * @dev: device.
725 * This simply forwards the call to kobject_get(), though
726 * we do take care to provide for the case that we get a NULL
727 * pointer passed in.
730 struct device * get_device(struct device * dev)
732 return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
737 * put_device - decrement reference count.
738 * @dev: device in question.
740 void put_device(struct device * dev)
742 if (dev)
743 kobject_put(&dev->kobj);
748 * device_del - delete device from system.
749 * @dev: device.
751 * This is the first part of the device unregistration
752 * sequence. This removes the device from the lists we control
753 * from here, has it removed from the other driver model
754 * subsystems it was added to in device_add(), and removes it
755 * from the kobject hierarchy.
757 * NOTE: this should be called manually _iff_ device_add() was
758 * also called manually.
761 void device_del(struct device * dev)
763 struct device * parent = dev->parent;
764 struct class_interface *class_intf;
766 if (parent)
767 klist_del(&dev->knode_parent);
768 if (dev->devt_attr) {
769 device_remove_file(dev, dev->devt_attr);
770 kfree(dev->devt_attr);
772 if (dev->class) {
773 sysfs_remove_link(&dev->kobj, "subsystem");
774 /* If this is not a "fake" compatible device, remove the
775 * symlink from the class to the device. */
776 if (dev->kobj.parent != &dev->class->subsys.kset.kobj)
777 sysfs_remove_link(&dev->class->subsys.kset.kobj,
778 dev->bus_id);
779 if (parent) {
780 #ifdef CONFIG_SYSFS_DEPRECATED
781 char *class_name = make_class_name(dev->class->name,
782 &dev->kobj);
783 if (class_name)
784 sysfs_remove_link(&dev->parent->kobj,
785 class_name);
786 kfree(class_name);
787 #endif
788 sysfs_remove_link(&dev->kobj, "device");
791 down(&dev->class->sem);
792 /* notify any interfaces that the device is now gone */
793 list_for_each_entry(class_intf, &dev->class->interfaces, node)
794 if (class_intf->remove_dev)
795 class_intf->remove_dev(dev, class_intf);
796 /* remove the device from the class list */
797 list_del_init(&dev->node);
798 up(&dev->class->sem);
800 device_remove_file(dev, &dev->uevent_attr);
801 device_remove_groups(dev);
802 device_remove_attrs(dev);
803 bus_remove_device(dev);
806 * Some platform devices are driven without driver attached
807 * and managed resources may have been acquired. Make sure
808 * all resources are released.
810 devres_release_all(dev);
812 /* Notify the platform of the removal, in case they
813 * need to do anything...
815 if (platform_notify_remove)
816 platform_notify_remove(dev);
817 if (dev->bus)
818 blocking_notifier_call_chain(&dev->bus->bus_notifier,
819 BUS_NOTIFY_DEL_DEVICE, dev);
820 device_pm_remove(dev);
821 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
822 kobject_del(&dev->kobj);
823 if (parent)
824 put_device(parent);
828 * device_unregister - unregister device from system.
829 * @dev: device going away.
831 * We do this in two parts, like we do device_register(). First,
832 * we remove it from all the subsystems with device_del(), then
833 * we decrement the reference count via put_device(). If that
834 * is the final reference count, the device will be cleaned up
835 * via device_release() above. Otherwise, the structure will
836 * stick around until the final reference to the device is dropped.
838 void device_unregister(struct device * dev)
840 pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id);
841 device_del(dev);
842 put_device(dev);
846 static struct device * next_device(struct klist_iter * i)
848 struct klist_node * n = klist_next(i);
849 return n ? container_of(n, struct device, knode_parent) : NULL;
853 * device_for_each_child - device child iterator.
854 * @parent: parent struct device.
855 * @data: data for the callback.
856 * @fn: function to be called for each device.
858 * Iterate over @parent's child devices, and call @fn for each,
859 * passing it @data.
861 * We check the return of @fn each time. If it returns anything
862 * other than 0, we break out and return that value.
864 int device_for_each_child(struct device * parent, void * data,
865 int (*fn)(struct device *, void *))
867 struct klist_iter i;
868 struct device * child;
869 int error = 0;
871 klist_iter_init(&parent->klist_children, &i);
872 while ((child = next_device(&i)) && !error)
873 error = fn(child, data);
874 klist_iter_exit(&i);
875 return error;
879 * device_find_child - device iterator for locating a particular device.
880 * @parent: parent struct device
881 * @data: Data to pass to match function
882 * @match: Callback function to check device
884 * This is similar to the device_for_each_child() function above, but it
885 * returns a reference to a device that is 'found' for later use, as
886 * determined by the @match callback.
888 * The callback should return 0 if the device doesn't match and non-zero
889 * if it does. If the callback returns non-zero and a reference to the
890 * current device can be obtained, this function will return to the caller
891 * and not iterate over any more devices.
893 struct device * device_find_child(struct device *parent, void *data,
894 int (*match)(struct device *, void *))
896 struct klist_iter i;
897 struct device *child;
899 if (!parent)
900 return NULL;
902 klist_iter_init(&parent->klist_children, &i);
903 while ((child = next_device(&i)))
904 if (match(child, data) && get_device(child))
905 break;
906 klist_iter_exit(&i);
907 return child;
910 int __init devices_init(void)
912 return subsystem_register(&devices_subsys);
915 EXPORT_SYMBOL_GPL(device_for_each_child);
916 EXPORT_SYMBOL_GPL(device_find_child);
918 EXPORT_SYMBOL_GPL(device_initialize);
919 EXPORT_SYMBOL_GPL(device_add);
920 EXPORT_SYMBOL_GPL(device_register);
922 EXPORT_SYMBOL_GPL(device_del);
923 EXPORT_SYMBOL_GPL(device_unregister);
924 EXPORT_SYMBOL_GPL(get_device);
925 EXPORT_SYMBOL_GPL(put_device);
927 EXPORT_SYMBOL_GPL(device_create_file);
928 EXPORT_SYMBOL_GPL(device_remove_file);
931 static void device_create_release(struct device *dev)
933 pr_debug("%s called for %s\n", __FUNCTION__, dev->bus_id);
934 kfree(dev);
938 * device_create - creates a device and registers it with sysfs
939 * @class: pointer to the struct class that this device should be registered to
940 * @parent: pointer to the parent struct device of this new device, if any
941 * @devt: the dev_t for the char device to be added
942 * @fmt: string for the device's name
944 * This function can be used by char device classes. A struct device
945 * will be created in sysfs, registered to the specified class.
947 * A "dev" file will be created, showing the dev_t for the device, if
948 * the dev_t is not 0,0.
949 * If a pointer to a parent struct device is passed in, the newly created
950 * struct device will be a child of that device in sysfs.
951 * The pointer to the struct device will be returned from the call.
952 * Any further sysfs files that might be required can be created using this
953 * pointer.
955 * Note: the struct class passed to this function must have previously
956 * been created with a call to class_create().
958 struct device *device_create(struct class *class, struct device *parent,
959 dev_t devt, const char *fmt, ...)
961 va_list args;
962 struct device *dev = NULL;
963 int retval = -ENODEV;
965 if (class == NULL || IS_ERR(class))
966 goto error;
968 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
969 if (!dev) {
970 retval = -ENOMEM;
971 goto error;
974 dev->devt = devt;
975 dev->class = class;
976 dev->parent = parent;
977 dev->release = device_create_release;
979 va_start(args, fmt);
980 vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args);
981 va_end(args);
982 retval = device_register(dev);
983 if (retval)
984 goto error;
986 return dev;
988 error:
989 kfree(dev);
990 return ERR_PTR(retval);
992 EXPORT_SYMBOL_GPL(device_create);
995 * device_destroy - removes a device that was created with device_create()
996 * @class: pointer to the struct class that this device was registered with
997 * @devt: the dev_t of the device that was previously registered
999 * This call unregisters and cleans up a device that was created with a
1000 * call to device_create().
1002 void device_destroy(struct class *class, dev_t devt)
1004 struct device *dev = NULL;
1005 struct device *dev_tmp;
1007 down(&class->sem);
1008 list_for_each_entry(dev_tmp, &class->devices, node) {
1009 if (dev_tmp->devt == devt) {
1010 dev = dev_tmp;
1011 break;
1014 up(&class->sem);
1016 if (dev)
1017 device_unregister(dev);
1019 EXPORT_SYMBOL_GPL(device_destroy);
1022 * device_rename - renames a device
1023 * @dev: the pointer to the struct device to be renamed
1024 * @new_name: the new name of the device
1026 int device_rename(struct device *dev, char *new_name)
1028 char *old_class_name = NULL;
1029 char *new_class_name = NULL;
1030 char *old_symlink_name = NULL;
1031 int error;
1033 dev = get_device(dev);
1034 if (!dev)
1035 return -EINVAL;
1037 pr_debug("DEVICE: renaming '%s' to '%s'\n", dev->bus_id, new_name);
1039 #ifdef CONFIG_SYSFS_DEPRECATED
1040 if ((dev->class) && (dev->parent))
1041 old_class_name = make_class_name(dev->class->name, &dev->kobj);
1042 #endif
1044 if (dev->class) {
1045 old_symlink_name = kmalloc(BUS_ID_SIZE, GFP_KERNEL);
1046 if (!old_symlink_name) {
1047 error = -ENOMEM;
1048 goto out_free_old_class;
1050 strlcpy(old_symlink_name, dev->bus_id, BUS_ID_SIZE);
1053 strlcpy(dev->bus_id, new_name, BUS_ID_SIZE);
1055 error = kobject_rename(&dev->kobj, new_name);
1057 #ifdef CONFIG_SYSFS_DEPRECATED
1058 if (old_class_name) {
1059 new_class_name = make_class_name(dev->class->name, &dev->kobj);
1060 if (new_class_name) {
1061 sysfs_create_link(&dev->parent->kobj, &dev->kobj,
1062 new_class_name);
1063 sysfs_remove_link(&dev->parent->kobj, old_class_name);
1066 #endif
1068 if (dev->class) {
1069 sysfs_remove_link(&dev->class->subsys.kset.kobj,
1070 old_symlink_name);
1071 sysfs_create_link(&dev->class->subsys.kset.kobj, &dev->kobj,
1072 dev->bus_id);
1074 put_device(dev);
1076 kfree(new_class_name);
1077 kfree(old_symlink_name);
1078 out_free_old_class:
1079 kfree(old_class_name);
1081 return error;
1083 EXPORT_SYMBOL_GPL(device_rename);
1085 static int device_move_class_links(struct device *dev,
1086 struct device *old_parent,
1087 struct device *new_parent)
1089 int error = 0;
1090 #ifdef CONFIG_SYSFS_DEPRECATED
1091 char *class_name;
1093 class_name = make_class_name(dev->class->name, &dev->kobj);
1094 if (!class_name) {
1095 error = -ENOMEM;
1096 goto out;
1098 if (old_parent) {
1099 sysfs_remove_link(&dev->kobj, "device");
1100 sysfs_remove_link(&old_parent->kobj, class_name);
1102 if (new_parent) {
1103 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1104 "device");
1105 if (error)
1106 goto out;
1107 error = sysfs_create_link(&new_parent->kobj, &dev->kobj,
1108 class_name);
1109 if (error)
1110 sysfs_remove_link(&dev->kobj, "device");
1112 else
1113 error = 0;
1114 out:
1115 kfree(class_name);
1116 return error;
1117 #else
1118 if (old_parent)
1119 sysfs_remove_link(&dev->kobj, "device");
1120 if (new_parent)
1121 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1122 "device");
1123 return error;
1124 #endif
1128 * device_move - moves a device to a new parent
1129 * @dev: the pointer to the struct device to be moved
1130 * @new_parent: the new parent of the device (can by NULL)
1132 int device_move(struct device *dev, struct device *new_parent)
1134 int error;
1135 struct device *old_parent;
1136 struct kobject *new_parent_kobj;
1138 dev = get_device(dev);
1139 if (!dev)
1140 return -EINVAL;
1142 new_parent = get_device(new_parent);
1143 new_parent_kobj = get_device_parent (dev, new_parent);
1144 if (IS_ERR(new_parent_kobj)) {
1145 error = PTR_ERR(new_parent_kobj);
1146 put_device(new_parent);
1147 goto out;
1149 pr_debug("DEVICE: moving '%s' to '%s'\n", dev->bus_id,
1150 new_parent ? new_parent->bus_id : "<NULL>");
1151 error = kobject_move(&dev->kobj, new_parent_kobj);
1152 if (error) {
1153 put_device(new_parent);
1154 goto out;
1156 old_parent = dev->parent;
1157 dev->parent = new_parent;
1158 if (old_parent)
1159 klist_remove(&dev->knode_parent);
1160 if (new_parent)
1161 klist_add_tail(&dev->knode_parent, &new_parent->klist_children);
1162 if (!dev->class)
1163 goto out_put;
1164 error = device_move_class_links(dev, old_parent, new_parent);
1165 if (error) {
1166 /* We ignore errors on cleanup since we're hosed anyway... */
1167 device_move_class_links(dev, new_parent, old_parent);
1168 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
1169 if (new_parent)
1170 klist_remove(&dev->knode_parent);
1171 if (old_parent)
1172 klist_add_tail(&dev->knode_parent,
1173 &old_parent->klist_children);
1175 put_device(new_parent);
1176 goto out;
1178 out_put:
1179 put_device(old_parent);
1180 out:
1181 put_device(dev);
1182 return error;
1185 EXPORT_SYMBOL_GPL(device_move);