[PATCH] sysctl: s390: remove unnecessary use of insert_at_head
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / base / core.c
bloba8ac34ba610716a4fc5c4193920a3d44a4697ccd
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);
396 static void klist_children_get(struct klist_node *n)
398 struct device *dev = container_of(n, struct device, knode_parent);
400 get_device(dev);
403 static void klist_children_put(struct klist_node *n)
405 struct device *dev = container_of(n, struct device, knode_parent);
407 put_device(dev);
412 * device_initialize - init device structure.
413 * @dev: device.
415 * This prepares the device for use by other layers,
416 * including adding it to the device hierarchy.
417 * It is the first half of device_register(), if called by
418 * that, though it can also be called separately, so one
419 * may use @dev's fields (e.g. the refcount).
422 void device_initialize(struct device *dev)
424 kobj_set_kset_s(dev, devices_subsys);
425 kobject_init(&dev->kobj);
426 klist_init(&dev->klist_children, klist_children_get,
427 klist_children_put);
428 INIT_LIST_HEAD(&dev->dma_pools);
429 INIT_LIST_HEAD(&dev->node);
430 init_MUTEX(&dev->sem);
431 spin_lock_init(&dev->devres_lock);
432 INIT_LIST_HEAD(&dev->devres_head);
433 device_init_wakeup(dev, 0);
434 set_dev_node(dev, -1);
437 #ifdef CONFIG_SYSFS_DEPRECATED
438 static struct kobject * get_device_parent(struct device *dev,
439 struct device *parent)
441 /* Set the parent to the class, not the parent device */
442 /* this keeps sysfs from having a symlink to make old udevs happy */
443 if (dev->class)
444 return &dev->class->subsys.kset.kobj;
445 else if (parent)
446 return &parent->kobj;
448 return NULL;
450 #else
451 static struct kobject * virtual_device_parent(struct device *dev)
453 if (!dev->class)
454 return ERR_PTR(-ENODEV);
456 if (!dev->class->virtual_dir) {
457 static struct kobject *virtual_dir = NULL;
459 if (!virtual_dir)
460 virtual_dir = kobject_add_dir(&devices_subsys.kset.kobj, "virtual");
461 dev->class->virtual_dir = kobject_add_dir(virtual_dir, dev->class->name);
464 return dev->class->virtual_dir;
467 static struct kobject * get_device_parent(struct device *dev,
468 struct device *parent)
470 /* if this is a class device, and has no parent, create one */
471 if ((dev->class) && (parent == NULL)) {
472 return virtual_device_parent(dev);
473 } else if (parent)
474 return &parent->kobj;
475 return NULL;
478 #endif
479 static int setup_parent(struct device *dev, struct device *parent)
481 struct kobject *kobj;
482 kobj = get_device_parent(dev, parent);
483 if (IS_ERR(kobj))
484 return PTR_ERR(kobj);
485 if (kobj)
486 dev->kobj.parent = kobj;
487 return 0;
491 * device_add - add device to device hierarchy.
492 * @dev: device.
494 * This is part 2 of device_register(), though may be called
495 * separately _iff_ device_initialize() has been called separately.
497 * This adds it to the kobject hierarchy via kobject_add(), adds it
498 * to the global and sibling lists for the device, then
499 * adds it to the other relevant subsystems of the driver model.
501 int device_add(struct device *dev)
503 struct device *parent = NULL;
504 char *class_name = NULL;
505 struct class_interface *class_intf;
506 int error = -EINVAL;
508 dev = get_device(dev);
509 if (!dev || !strlen(dev->bus_id))
510 goto Error;
512 pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id);
514 parent = get_device(dev->parent);
516 error = setup_parent(dev, parent);
517 if (error)
518 goto Error;
520 /* first, register with generic layer. */
521 kobject_set_name(&dev->kobj, "%s", dev->bus_id);
522 error = kobject_add(&dev->kobj);
523 if (error)
524 goto Error;
526 /* notify platform of device entry */
527 if (platform_notify)
528 platform_notify(dev);
530 /* notify clients of device entry (new way) */
531 if (dev->bus)
532 blocking_notifier_call_chain(&dev->bus->bus_notifier,
533 BUS_NOTIFY_ADD_DEVICE, dev);
535 dev->uevent_attr.attr.name = "uevent";
536 dev->uevent_attr.attr.mode = S_IWUSR;
537 if (dev->driver)
538 dev->uevent_attr.attr.owner = dev->driver->owner;
539 dev->uevent_attr.store = store_uevent;
540 error = device_create_file(dev, &dev->uevent_attr);
541 if (error)
542 goto attrError;
544 if (MAJOR(dev->devt)) {
545 struct device_attribute *attr;
546 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
547 if (!attr) {
548 error = -ENOMEM;
549 goto ueventattrError;
551 attr->attr.name = "dev";
552 attr->attr.mode = S_IRUGO;
553 if (dev->driver)
554 attr->attr.owner = dev->driver->owner;
555 attr->show = show_dev;
556 error = device_create_file(dev, attr);
557 if (error) {
558 kfree(attr);
559 goto ueventattrError;
562 dev->devt_attr = attr;
565 if (dev->class) {
566 sysfs_create_link(&dev->kobj, &dev->class->subsys.kset.kobj,
567 "subsystem");
568 /* If this is not a "fake" compatible device, then create the
569 * symlink from the class to the device. */
570 if (dev->kobj.parent != &dev->class->subsys.kset.kobj)
571 sysfs_create_link(&dev->class->subsys.kset.kobj,
572 &dev->kobj, dev->bus_id);
573 #ifdef CONFIG_SYSFS_DEPRECATED
574 if (parent) {
575 sysfs_create_link(&dev->kobj, &dev->parent->kobj,
576 "device");
577 class_name = make_class_name(dev->class->name,
578 &dev->kobj);
579 if (class_name)
580 sysfs_create_link(&dev->parent->kobj,
581 &dev->kobj, class_name);
583 #endif
586 if ((error = device_add_attrs(dev)))
587 goto AttrsError;
588 if ((error = device_add_groups(dev)))
589 goto GroupError;
590 if ((error = device_pm_add(dev)))
591 goto PMError;
592 if ((error = bus_add_device(dev)))
593 goto BusError;
594 if (!dev->uevent_suppress)
595 kobject_uevent(&dev->kobj, KOBJ_ADD);
596 if ((error = bus_attach_device(dev)))
597 goto AttachError;
598 if (parent)
599 klist_add_tail(&dev->knode_parent, &parent->klist_children);
601 if (dev->class) {
602 down(&dev->class->sem);
603 /* tie the class to the device */
604 list_add_tail(&dev->node, &dev->class->devices);
606 /* notify any interfaces that the device is here */
607 list_for_each_entry(class_intf, &dev->class->interfaces, node)
608 if (class_intf->add_dev)
609 class_intf->add_dev(dev, class_intf);
610 up(&dev->class->sem);
612 Done:
613 kfree(class_name);
614 put_device(dev);
615 return error;
616 AttachError:
617 bus_remove_device(dev);
618 BusError:
619 device_pm_remove(dev);
620 PMError:
621 if (dev->bus)
622 blocking_notifier_call_chain(&dev->bus->bus_notifier,
623 BUS_NOTIFY_DEL_DEVICE, dev);
624 device_remove_groups(dev);
625 GroupError:
626 device_remove_attrs(dev);
627 AttrsError:
628 if (dev->devt_attr) {
629 device_remove_file(dev, dev->devt_attr);
630 kfree(dev->devt_attr);
632 ueventattrError:
633 device_remove_file(dev, &dev->uevent_attr);
634 attrError:
635 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
636 kobject_del(&dev->kobj);
637 Error:
638 if (parent)
639 put_device(parent);
640 goto Done;
645 * device_register - register a device with the system.
646 * @dev: pointer to the device structure
648 * This happens in two clean steps - initialize the device
649 * and add it to the system. The two steps can be called
650 * separately, but this is the easiest and most common.
651 * I.e. you should only call the two helpers separately if
652 * have a clearly defined need to use and refcount the device
653 * before it is added to the hierarchy.
656 int device_register(struct device *dev)
658 device_initialize(dev);
659 return device_add(dev);
664 * get_device - increment reference count for device.
665 * @dev: device.
667 * This simply forwards the call to kobject_get(), though
668 * we do take care to provide for the case that we get a NULL
669 * pointer passed in.
672 struct device * get_device(struct device * dev)
674 return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
679 * put_device - decrement reference count.
680 * @dev: device in question.
682 void put_device(struct device * dev)
684 if (dev)
685 kobject_put(&dev->kobj);
690 * device_del - delete device from system.
691 * @dev: device.
693 * This is the first part of the device unregistration
694 * sequence. This removes the device from the lists we control
695 * from here, has it removed from the other driver model
696 * subsystems it was added to in device_add(), and removes it
697 * from the kobject hierarchy.
699 * NOTE: this should be called manually _iff_ device_add() was
700 * also called manually.
703 void device_del(struct device * dev)
705 struct device * parent = dev->parent;
706 struct class_interface *class_intf;
708 if (parent)
709 klist_del(&dev->knode_parent);
710 if (dev->devt_attr) {
711 device_remove_file(dev, dev->devt_attr);
712 kfree(dev->devt_attr);
714 if (dev->class) {
715 sysfs_remove_link(&dev->kobj, "subsystem");
716 /* If this is not a "fake" compatible device, remove the
717 * symlink from the class to the device. */
718 if (dev->kobj.parent != &dev->class->subsys.kset.kobj)
719 sysfs_remove_link(&dev->class->subsys.kset.kobj,
720 dev->bus_id);
721 #ifdef CONFIG_SYSFS_DEPRECATED
722 if (parent) {
723 char *class_name = make_class_name(dev->class->name,
724 &dev->kobj);
725 if (class_name)
726 sysfs_remove_link(&dev->parent->kobj,
727 class_name);
728 kfree(class_name);
729 sysfs_remove_link(&dev->kobj, "device");
731 #endif
733 down(&dev->class->sem);
734 /* notify any interfaces that the device is now gone */
735 list_for_each_entry(class_intf, &dev->class->interfaces, node)
736 if (class_intf->remove_dev)
737 class_intf->remove_dev(dev, class_intf);
738 /* remove the device from the class list */
739 list_del_init(&dev->node);
740 up(&dev->class->sem);
742 device_remove_file(dev, &dev->uevent_attr);
743 device_remove_groups(dev);
744 device_remove_attrs(dev);
745 bus_remove_device(dev);
747 /* Notify the platform of the removal, in case they
748 * need to do anything...
750 if (platform_notify_remove)
751 platform_notify_remove(dev);
752 if (dev->bus)
753 blocking_notifier_call_chain(&dev->bus->bus_notifier,
754 BUS_NOTIFY_DEL_DEVICE, dev);
755 device_pm_remove(dev);
756 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
757 kobject_del(&dev->kobj);
758 if (parent)
759 put_device(parent);
763 * device_unregister - unregister device from system.
764 * @dev: device going away.
766 * We do this in two parts, like we do device_register(). First,
767 * we remove it from all the subsystems with device_del(), then
768 * we decrement the reference count via put_device(). If that
769 * is the final reference count, the device will be cleaned up
770 * via device_release() above. Otherwise, the structure will
771 * stick around until the final reference to the device is dropped.
773 void device_unregister(struct device * dev)
775 pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id);
776 device_del(dev);
777 put_device(dev);
781 static struct device * next_device(struct klist_iter * i)
783 struct klist_node * n = klist_next(i);
784 return n ? container_of(n, struct device, knode_parent) : NULL;
788 * device_for_each_child - device child iterator.
789 * @parent: parent struct device.
790 * @data: data for the callback.
791 * @fn: function to be called for each device.
793 * Iterate over @parent's child devices, and call @fn for each,
794 * passing it @data.
796 * We check the return of @fn each time. If it returns anything
797 * other than 0, we break out and return that value.
799 int device_for_each_child(struct device * parent, void * data,
800 int (*fn)(struct device *, void *))
802 struct klist_iter i;
803 struct device * child;
804 int error = 0;
806 klist_iter_init(&parent->klist_children, &i);
807 while ((child = next_device(&i)) && !error)
808 error = fn(child, data);
809 klist_iter_exit(&i);
810 return error;
814 * device_find_child - device iterator for locating a particular device.
815 * @parent: parent struct device
816 * @data: Data to pass to match function
817 * @match: Callback function to check device
819 * This is similar to the device_for_each_child() function above, but it
820 * returns a reference to a device that is 'found' for later use, as
821 * determined by the @match callback.
823 * The callback should return 0 if the device doesn't match and non-zero
824 * if it does. If the callback returns non-zero and a reference to the
825 * current device can be obtained, this function will return to the caller
826 * and not iterate over any more devices.
828 struct device * device_find_child(struct device *parent, void *data,
829 int (*match)(struct device *, void *))
831 struct klist_iter i;
832 struct device *child;
834 if (!parent)
835 return NULL;
837 klist_iter_init(&parent->klist_children, &i);
838 while ((child = next_device(&i)))
839 if (match(child, data) && get_device(child))
840 break;
841 klist_iter_exit(&i);
842 return child;
845 int __init devices_init(void)
847 return subsystem_register(&devices_subsys);
850 EXPORT_SYMBOL_GPL(device_for_each_child);
851 EXPORT_SYMBOL_GPL(device_find_child);
853 EXPORT_SYMBOL_GPL(device_initialize);
854 EXPORT_SYMBOL_GPL(device_add);
855 EXPORT_SYMBOL_GPL(device_register);
857 EXPORT_SYMBOL_GPL(device_del);
858 EXPORT_SYMBOL_GPL(device_unregister);
859 EXPORT_SYMBOL_GPL(get_device);
860 EXPORT_SYMBOL_GPL(put_device);
862 EXPORT_SYMBOL_GPL(device_create_file);
863 EXPORT_SYMBOL_GPL(device_remove_file);
866 static void device_create_release(struct device *dev)
868 pr_debug("%s called for %s\n", __FUNCTION__, dev->bus_id);
869 kfree(dev);
873 * device_create - creates a device and registers it with sysfs
874 * @class: pointer to the struct class that this device should be registered to
875 * @parent: pointer to the parent struct device of this new device, if any
876 * @devt: the dev_t for the char device to be added
877 * @fmt: string for the device's name
879 * This function can be used by char device classes. A struct device
880 * will be created in sysfs, registered to the specified class.
882 * A "dev" file will be created, showing the dev_t for the device, if
883 * the dev_t is not 0,0.
884 * If a pointer to a parent struct device is passed in, the newly created
885 * struct device will be a child of that device in sysfs.
886 * The pointer to the struct device will be returned from the call.
887 * Any further sysfs files that might be required can be created using this
888 * pointer.
890 * Note: the struct class passed to this function must have previously
891 * been created with a call to class_create().
893 struct device *device_create(struct class *class, struct device *parent,
894 dev_t devt, const char *fmt, ...)
896 va_list args;
897 struct device *dev = NULL;
898 int retval = -ENODEV;
900 if (class == NULL || IS_ERR(class))
901 goto error;
903 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
904 if (!dev) {
905 retval = -ENOMEM;
906 goto error;
909 dev->devt = devt;
910 dev->class = class;
911 dev->parent = parent;
912 dev->release = device_create_release;
914 va_start(args, fmt);
915 vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args);
916 va_end(args);
917 retval = device_register(dev);
918 if (retval)
919 goto error;
921 return dev;
923 error:
924 kfree(dev);
925 return ERR_PTR(retval);
927 EXPORT_SYMBOL_GPL(device_create);
930 * device_destroy - removes a device that was created with device_create()
931 * @class: pointer to the struct class that this device was registered with
932 * @devt: the dev_t of the device that was previously registered
934 * This call unregisters and cleans up a device that was created with a
935 * call to device_create().
937 void device_destroy(struct class *class, dev_t devt)
939 struct device *dev = NULL;
940 struct device *dev_tmp;
942 down(&class->sem);
943 list_for_each_entry(dev_tmp, &class->devices, node) {
944 if (dev_tmp->devt == devt) {
945 dev = dev_tmp;
946 break;
949 up(&class->sem);
951 if (dev)
952 device_unregister(dev);
954 EXPORT_SYMBOL_GPL(device_destroy);
957 * device_rename - renames a device
958 * @dev: the pointer to the struct device to be renamed
959 * @new_name: the new name of the device
961 int device_rename(struct device *dev, char *new_name)
963 char *old_class_name = NULL;
964 char *new_class_name = NULL;
965 char *old_symlink_name = NULL;
966 int error;
968 dev = get_device(dev);
969 if (!dev)
970 return -EINVAL;
972 pr_debug("DEVICE: renaming '%s' to '%s'\n", dev->bus_id, new_name);
974 #ifdef CONFIG_SYSFS_DEPRECATED
975 if ((dev->class) && (dev->parent))
976 old_class_name = make_class_name(dev->class->name, &dev->kobj);
977 #endif
979 if (dev->class) {
980 old_symlink_name = kmalloc(BUS_ID_SIZE, GFP_KERNEL);
981 if (!old_symlink_name) {
982 error = -ENOMEM;
983 goto out_free_old_class;
985 strlcpy(old_symlink_name, dev->bus_id, BUS_ID_SIZE);
988 strlcpy(dev->bus_id, new_name, BUS_ID_SIZE);
990 error = kobject_rename(&dev->kobj, new_name);
992 #ifdef CONFIG_SYSFS_DEPRECATED
993 if (old_class_name) {
994 new_class_name = make_class_name(dev->class->name, &dev->kobj);
995 if (new_class_name) {
996 sysfs_create_link(&dev->parent->kobj, &dev->kobj,
997 new_class_name);
998 sysfs_remove_link(&dev->parent->kobj, old_class_name);
1001 #endif
1003 if (dev->class) {
1004 sysfs_remove_link(&dev->class->subsys.kset.kobj,
1005 old_symlink_name);
1006 sysfs_create_link(&dev->class->subsys.kset.kobj, &dev->kobj,
1007 dev->bus_id);
1009 put_device(dev);
1011 kfree(new_class_name);
1012 kfree(old_symlink_name);
1013 out_free_old_class:
1014 kfree(old_class_name);
1016 return error;
1020 static int device_move_class_links(struct device *dev,
1021 struct device *old_parent,
1022 struct device *new_parent)
1024 #ifdef CONFIG_SYSFS_DEPRECATED
1025 int error;
1026 char *class_name;
1028 class_name = make_class_name(dev->class->name, &dev->kobj);
1029 if (!class_name) {
1030 error = -ENOMEM;
1031 goto out;
1033 if (old_parent) {
1034 sysfs_remove_link(&dev->kobj, "device");
1035 sysfs_remove_link(&old_parent->kobj, class_name);
1037 if (new_parent) {
1038 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1039 "device");
1040 if (error)
1041 goto out;
1042 error = sysfs_create_link(&new_parent->kobj, &dev->kobj,
1043 class_name);
1044 if (error)
1045 sysfs_remove_link(&dev->kobj, "device");
1047 else
1048 error = 0;
1049 out:
1050 kfree(class_name);
1051 return error;
1052 #else
1053 return 0;
1054 #endif
1058 * device_move - moves a device to a new parent
1059 * @dev: the pointer to the struct device to be moved
1060 * @new_parent: the new parent of the device (can by NULL)
1062 int device_move(struct device *dev, struct device *new_parent)
1064 int error;
1065 struct device *old_parent;
1066 struct kobject *new_parent_kobj;
1068 dev = get_device(dev);
1069 if (!dev)
1070 return -EINVAL;
1072 new_parent = get_device(new_parent);
1073 new_parent_kobj = get_device_parent (dev, new_parent);
1074 if (IS_ERR(new_parent_kobj)) {
1075 error = PTR_ERR(new_parent_kobj);
1076 put_device(new_parent);
1077 goto out;
1079 pr_debug("DEVICE: moving '%s' to '%s'\n", dev->bus_id,
1080 new_parent ? new_parent->bus_id : "<NULL>");
1081 error = kobject_move(&dev->kobj, new_parent_kobj);
1082 if (error) {
1083 put_device(new_parent);
1084 goto out;
1086 old_parent = dev->parent;
1087 dev->parent = new_parent;
1088 if (old_parent)
1089 klist_remove(&dev->knode_parent);
1090 if (new_parent)
1091 klist_add_tail(&dev->knode_parent, &new_parent->klist_children);
1092 if (!dev->class)
1093 goto out_put;
1094 error = device_move_class_links(dev, old_parent, new_parent);
1095 if (error) {
1096 /* We ignore errors on cleanup since we're hosed anyway... */
1097 device_move_class_links(dev, new_parent, old_parent);
1098 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
1099 if (new_parent)
1100 klist_remove(&dev->knode_parent);
1101 if (old_parent)
1102 klist_add_tail(&dev->knode_parent,
1103 &old_parent->klist_children);
1105 put_device(new_parent);
1106 goto out;
1108 out_put:
1109 put_device(old_parent);
1110 out:
1111 put_device(dev);
1112 return error;
1115 EXPORT_SYMBOL_GPL(device_move);