CONFIG_SYSFS_DEPRECATED - PHYSDEV* uevent variables
[deliverable/linux.git] / drivers / base / core.c
1 /*
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 * Copyright (c) 2006 Greg Kroah-Hartman <gregkh@suse.de>
7 * Copyright (c) 2006 Novell, Inc.
8 *
9 * This file is released under the GPLv2
10 *
11 */
12
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>
21
22 #include <asm/semaphore.h>
23
24 #include "base.h"
25 #include "power/power.h"
26
27 int (*platform_notify)(struct device * dev) = NULL;
28 int (*platform_notify_remove)(struct device * dev) = NULL;
29
30 /*
31 * sysfs bindings for devices.
32 */
33
34 /**
35 * dev_driver_string - Return a device's driver name, if at all possible
36 * @dev: struct device to get the name of
37 *
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.
42 */
43 const char *dev_driver_string(struct device *dev)
44 {
45 return dev->driver ? dev->driver->name :
46 (dev->bus ? dev->bus->name : "");
47 }
48 EXPORT_SYMBOL(dev_driver_string);
49
50 #define to_dev(obj) container_of(obj, struct device, kobj)
51 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
52
53 static ssize_t
54 dev_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
55 {
56 struct device_attribute * dev_attr = to_dev_attr(attr);
57 struct device * dev = to_dev(kobj);
58 ssize_t ret = -EIO;
59
60 if (dev_attr->show)
61 ret = dev_attr->show(dev, dev_attr, buf);
62 return ret;
63 }
64
65 static ssize_t
66 dev_attr_store(struct kobject * kobj, struct attribute * attr,
67 const char * buf, size_t count)
68 {
69 struct device_attribute * dev_attr = to_dev_attr(attr);
70 struct device * dev = to_dev(kobj);
71 ssize_t ret = -EIO;
72
73 if (dev_attr->store)
74 ret = dev_attr->store(dev, dev_attr, buf, count);
75 return ret;
76 }
77
78 static struct sysfs_ops dev_sysfs_ops = {
79 .show = dev_attr_show,
80 .store = dev_attr_store,
81 };
82
83
84 /**
85 * device_release - free device structure.
86 * @kobj: device's kobject.
87 *
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.
91 */
92 static void device_release(struct kobject * kobj)
93 {
94 struct device * dev = to_dev(kobj);
95
96 if (dev->release)
97 dev->release(dev);
98 else if (dev->class && dev->class->dev_release)
99 dev->class->dev_release(dev);
100 else {
101 printk(KERN_ERR "Device '%s' does not have a release() function, "
102 "it is broken and must be fixed.\n",
103 dev->bus_id);
104 WARN_ON(1);
105 }
106 }
107
108 static struct kobj_type ktype_device = {
109 .release = device_release,
110 .sysfs_ops = &dev_sysfs_ops,
111 };
112
113
114 static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
115 {
116 struct kobj_type *ktype = get_ktype(kobj);
117
118 if (ktype == &ktype_device) {
119 struct device *dev = to_dev(kobj);
120 if (dev->bus)
121 return 1;
122 if (dev->class)
123 return 1;
124 }
125 return 0;
126 }
127
128 static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
129 {
130 struct device *dev = to_dev(kobj);
131
132 if (dev->bus)
133 return dev->bus->name;
134 if (dev->class)
135 return dev->class->name;
136 return NULL;
137 }
138
139 static int dev_uevent(struct kset *kset, struct kobject *kobj, char **envp,
140 int num_envp, char *buffer, int buffer_size)
141 {
142 struct device *dev = to_dev(kobj);
143 int i = 0;
144 int length = 0;
145 int retval = 0;
146
147 /* add the major/minor if present */
148 if (MAJOR(dev->devt)) {
149 add_uevent_var(envp, num_envp, &i,
150 buffer, buffer_size, &length,
151 "MAJOR=%u", MAJOR(dev->devt));
152 add_uevent_var(envp, num_envp, &i,
153 buffer, buffer_size, &length,
154 "MINOR=%u", MINOR(dev->devt));
155 }
156
157 #ifdef CONFIG_SYSFS_DEPRECATED
158 /* add bus name (same as SUBSYSTEM, deprecated) */
159 if (dev->bus)
160 add_uevent_var(envp, num_envp, &i,
161 buffer, buffer_size, &length,
162 "PHYSDEVBUS=%s", dev->bus->name);
163 #endif
164
165 /* add driver name (PHYSDEV* values are deprecated)*/
166 if (dev->driver) {
167 add_uevent_var(envp, num_envp, &i,
168 buffer, buffer_size, &length,
169 "DRIVER=%s", dev->driver->name);
170 #ifdef CONFIG_SYSFS_DEPRECATED
171 add_uevent_var(envp, num_envp, &i,
172 buffer, buffer_size, &length,
173 "PHYSDEVDRIVER=%s", dev->driver->name);
174 #endif
175 }
176
177 /* terminate, set to next free slot, shrink available space */
178 envp[i] = NULL;
179 envp = &envp[i];
180 num_envp -= i;
181 buffer = &buffer[length];
182 buffer_size -= length;
183
184 if (dev->bus && dev->bus->uevent) {
185 /* have the bus specific function add its stuff */
186 retval = dev->bus->uevent(dev, envp, num_envp, buffer, buffer_size);
187 if (retval) {
188 pr_debug ("%s - uevent() returned %d\n",
189 __FUNCTION__, retval);
190 }
191 }
192
193 if (dev->class && dev->class->dev_uevent) {
194 /* have the class specific function add its stuff */
195 retval = dev->class->dev_uevent(dev, envp, num_envp, buffer, buffer_size);
196 if (retval) {
197 pr_debug("%s - dev_uevent() returned %d\n",
198 __FUNCTION__, retval);
199 }
200 }
201
202 return retval;
203 }
204
205 static struct kset_uevent_ops device_uevent_ops = {
206 .filter = dev_uevent_filter,
207 .name = dev_uevent_name,
208 .uevent = dev_uevent,
209 };
210
211 static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
212 const char *buf, size_t count)
213 {
214 kobject_uevent(&dev->kobj, KOBJ_ADD);
215 return count;
216 }
217
218 static int device_add_groups(struct device *dev)
219 {
220 int i;
221 int error = 0;
222
223 if (dev->groups) {
224 for (i = 0; dev->groups[i]; i++) {
225 error = sysfs_create_group(&dev->kobj, dev->groups[i]);
226 if (error) {
227 while (--i >= 0)
228 sysfs_remove_group(&dev->kobj, dev->groups[i]);
229 goto out;
230 }
231 }
232 }
233 out:
234 return error;
235 }
236
237 static void device_remove_groups(struct device *dev)
238 {
239 int i;
240 if (dev->groups) {
241 for (i = 0; dev->groups[i]; i++) {
242 sysfs_remove_group(&dev->kobj, dev->groups[i]);
243 }
244 }
245 }
246
247 static int device_add_attrs(struct device *dev)
248 {
249 struct class *class = dev->class;
250 int error = 0;
251 int i;
252
253 if (!class)
254 return 0;
255
256 if (class->dev_attrs) {
257 for (i = 0; attr_name(class->dev_attrs[i]); i++) {
258 error = device_create_file(dev, &class->dev_attrs[i]);
259 if (error)
260 break;
261 }
262 }
263 if (error)
264 while (--i >= 0)
265 device_remove_file(dev, &class->dev_attrs[i]);
266 return error;
267 }
268
269 static void device_remove_attrs(struct device *dev)
270 {
271 struct class *class = dev->class;
272 int i;
273
274 if (!class)
275 return;
276
277 if (class->dev_attrs) {
278 for (i = 0; attr_name(class->dev_attrs[i]); i++)
279 device_remove_file(dev, &class->dev_attrs[i]);
280 }
281 }
282
283
284 static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
285 char *buf)
286 {
287 return print_dev_t(buf, dev->devt);
288 }
289
290 /*
291 * devices_subsys - structure to be registered with kobject core.
292 */
293
294 decl_subsys(devices, &ktype_device, &device_uevent_ops);
295
296
297 /**
298 * device_create_file - create sysfs attribute file for device.
299 * @dev: device.
300 * @attr: device attribute descriptor.
301 */
302
303 int device_create_file(struct device * dev, struct device_attribute * attr)
304 {
305 int error = 0;
306 if (get_device(dev)) {
307 error = sysfs_create_file(&dev->kobj, &attr->attr);
308 put_device(dev);
309 }
310 return error;
311 }
312
313 /**
314 * device_remove_file - remove sysfs attribute file.
315 * @dev: device.
316 * @attr: device attribute descriptor.
317 */
318
319 void device_remove_file(struct device * dev, struct device_attribute * attr)
320 {
321 if (get_device(dev)) {
322 sysfs_remove_file(&dev->kobj, &attr->attr);
323 put_device(dev);
324 }
325 }
326
327 /**
328 * device_create_bin_file - create sysfs binary attribute file for device.
329 * @dev: device.
330 * @attr: device binary attribute descriptor.
331 */
332 int device_create_bin_file(struct device *dev, struct bin_attribute *attr)
333 {
334 int error = -EINVAL;
335 if (dev)
336 error = sysfs_create_bin_file(&dev->kobj, attr);
337 return error;
338 }
339 EXPORT_SYMBOL_GPL(device_create_bin_file);
340
341 /**
342 * device_remove_bin_file - remove sysfs binary attribute file
343 * @dev: device.
344 * @attr: device binary attribute descriptor.
345 */
346 void device_remove_bin_file(struct device *dev, struct bin_attribute *attr)
347 {
348 if (dev)
349 sysfs_remove_bin_file(&dev->kobj, attr);
350 }
351 EXPORT_SYMBOL_GPL(device_remove_bin_file);
352
353 static void klist_children_get(struct klist_node *n)
354 {
355 struct device *dev = container_of(n, struct device, knode_parent);
356
357 get_device(dev);
358 }
359
360 static void klist_children_put(struct klist_node *n)
361 {
362 struct device *dev = container_of(n, struct device, knode_parent);
363
364 put_device(dev);
365 }
366
367
368 /**
369 * device_initialize - init device structure.
370 * @dev: device.
371 *
372 * This prepares the device for use by other layers,
373 * including adding it to the device hierarchy.
374 * It is the first half of device_register(), if called by
375 * that, though it can also be called separately, so one
376 * may use @dev's fields (e.g. the refcount).
377 */
378
379 void device_initialize(struct device *dev)
380 {
381 kobj_set_kset_s(dev, devices_subsys);
382 kobject_init(&dev->kobj);
383 klist_init(&dev->klist_children, klist_children_get,
384 klist_children_put);
385 INIT_LIST_HEAD(&dev->dma_pools);
386 INIT_LIST_HEAD(&dev->node);
387 init_MUTEX(&dev->sem);
388 device_init_wakeup(dev, 0);
389 }
390
391 #ifdef CONFIG_SYSFS_DEPRECATED
392 int setup_parent(struct device *dev, struct device *parent)
393 {
394 /* Set the parent to the class, not the parent device */
395 /* this keeps sysfs from having a symlink to make old udevs happy */
396 if (dev->class)
397 dev->kobj.parent = &dev->class->subsys.kset.kobj;
398 else if (parent)
399 dev->kobj.parent = &parent->kobj;
400
401 return 0;
402 }
403 #else
404 static int virtual_device_parent(struct device *dev)
405 {
406 if (!dev->class)
407 return -ENODEV;
408
409 if (!dev->class->virtual_dir) {
410 static struct kobject *virtual_dir = NULL;
411
412 if (!virtual_dir)
413 virtual_dir = kobject_add_dir(&devices_subsys.kset.kobj, "virtual");
414 dev->class->virtual_dir = kobject_add_dir(virtual_dir, dev->class->name);
415 }
416
417 dev->kobj.parent = dev->class->virtual_dir;
418 return 0;
419 }
420
421 int setup_parent(struct device *dev, struct device *parent)
422 {
423 int error;
424
425 /* if this is a class device, and has no parent, create one */
426 if ((dev->class) && (parent == NULL)) {
427 error = virtual_device_parent(dev);
428 if (error)
429 return error;
430 } else if (parent)
431 dev->kobj.parent = &parent->kobj;
432
433 return 0;
434 }
435 #endif
436
437 /**
438 * device_add - add device to device hierarchy.
439 * @dev: device.
440 *
441 * This is part 2 of device_register(), though may be called
442 * separately _iff_ device_initialize() has been called separately.
443 *
444 * This adds it to the kobject hierarchy via kobject_add(), adds it
445 * to the global and sibling lists for the device, then
446 * adds it to the other relevant subsystems of the driver model.
447 */
448 int device_add(struct device *dev)
449 {
450 struct device *parent = NULL;
451 char *class_name = NULL;
452 struct class_interface *class_intf;
453 int error = -EINVAL;
454
455 dev = get_device(dev);
456 if (!dev || !strlen(dev->bus_id))
457 goto Error;
458
459 pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id);
460
461 parent = get_device(dev->parent);
462
463 error = setup_parent(dev, parent);
464 if (error)
465 goto Error;
466
467 /* first, register with generic layer. */
468 kobject_set_name(&dev->kobj, "%s", dev->bus_id);
469 error = kobject_add(&dev->kobj);
470 if (error)
471 goto Error;
472
473 /* notify platform of device entry */
474 if (platform_notify)
475 platform_notify(dev);
476
477 /* notify clients of device entry (new way) */
478 if (dev->bus)
479 blocking_notifier_call_chain(&dev->bus->bus_notifier,
480 BUS_NOTIFY_ADD_DEVICE, dev);
481
482 dev->uevent_attr.attr.name = "uevent";
483 dev->uevent_attr.attr.mode = S_IWUSR;
484 if (dev->driver)
485 dev->uevent_attr.attr.owner = dev->driver->owner;
486 dev->uevent_attr.store = store_uevent;
487 error = device_create_file(dev, &dev->uevent_attr);
488 if (error)
489 goto attrError;
490
491 if (MAJOR(dev->devt)) {
492 struct device_attribute *attr;
493 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
494 if (!attr) {
495 error = -ENOMEM;
496 goto ueventattrError;
497 }
498 attr->attr.name = "dev";
499 attr->attr.mode = S_IRUGO;
500 if (dev->driver)
501 attr->attr.owner = dev->driver->owner;
502 attr->show = show_dev;
503 error = device_create_file(dev, attr);
504 if (error) {
505 kfree(attr);
506 goto ueventattrError;
507 }
508
509 dev->devt_attr = attr;
510 }
511
512 if (dev->class) {
513 sysfs_create_link(&dev->kobj, &dev->class->subsys.kset.kobj,
514 "subsystem");
515 /* If this is not a "fake" compatible device, then create the
516 * symlink from the class to the device. */
517 if (dev->kobj.parent != &dev->class->subsys.kset.kobj)
518 sysfs_create_link(&dev->class->subsys.kset.kobj,
519 &dev->kobj, dev->bus_id);
520 #ifdef CONFIG_SYSFS_DEPRECATED
521 if (parent) {
522 sysfs_create_link(&dev->kobj, &dev->parent->kobj, "device");
523 class_name = make_class_name(dev->class->name, &dev->kobj);
524 sysfs_create_link(&dev->parent->kobj, &dev->kobj, class_name);
525 }
526 #endif
527 }
528
529 if ((error = device_add_attrs(dev)))
530 goto AttrsError;
531 if ((error = device_add_groups(dev)))
532 goto GroupError;
533 if ((error = device_pm_add(dev)))
534 goto PMError;
535 if ((error = bus_add_device(dev)))
536 goto BusError;
537 kobject_uevent(&dev->kobj, KOBJ_ADD);
538 if ((error = bus_attach_device(dev)))
539 goto AttachError;
540 if (parent)
541 klist_add_tail(&dev->knode_parent, &parent->klist_children);
542
543 if (dev->class) {
544 down(&dev->class->sem);
545 /* tie the class to the device */
546 list_add_tail(&dev->node, &dev->class->devices);
547
548 /* notify any interfaces that the device is here */
549 list_for_each_entry(class_intf, &dev->class->interfaces, node)
550 if (class_intf->add_dev)
551 class_intf->add_dev(dev, class_intf);
552 up(&dev->class->sem);
553 }
554 Done:
555 kfree(class_name);
556 put_device(dev);
557 return error;
558 AttachError:
559 bus_remove_device(dev);
560 BusError:
561 device_pm_remove(dev);
562 PMError:
563 if (dev->bus)
564 blocking_notifier_call_chain(&dev->bus->bus_notifier,
565 BUS_NOTIFY_DEL_DEVICE, dev);
566 device_remove_groups(dev);
567 GroupError:
568 device_remove_attrs(dev);
569 AttrsError:
570 if (dev->devt_attr) {
571 device_remove_file(dev, dev->devt_attr);
572 kfree(dev->devt_attr);
573 }
574 ueventattrError:
575 device_remove_file(dev, &dev->uevent_attr);
576 attrError:
577 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
578 kobject_del(&dev->kobj);
579 Error:
580 if (parent)
581 put_device(parent);
582 goto Done;
583 }
584
585
586 /**
587 * device_register - register a device with the system.
588 * @dev: pointer to the device structure
589 *
590 * This happens in two clean steps - initialize the device
591 * and add it to the system. The two steps can be called
592 * separately, but this is the easiest and most common.
593 * I.e. you should only call the two helpers separately if
594 * have a clearly defined need to use and refcount the device
595 * before it is added to the hierarchy.
596 */
597
598 int device_register(struct device *dev)
599 {
600 device_initialize(dev);
601 return device_add(dev);
602 }
603
604
605 /**
606 * get_device - increment reference count for device.
607 * @dev: device.
608 *
609 * This simply forwards the call to kobject_get(), though
610 * we do take care to provide for the case that we get a NULL
611 * pointer passed in.
612 */
613
614 struct device * get_device(struct device * dev)
615 {
616 return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
617 }
618
619
620 /**
621 * put_device - decrement reference count.
622 * @dev: device in question.
623 */
624 void put_device(struct device * dev)
625 {
626 if (dev)
627 kobject_put(&dev->kobj);
628 }
629
630
631 /**
632 * device_del - delete device from system.
633 * @dev: device.
634 *
635 * This is the first part of the device unregistration
636 * sequence. This removes the device from the lists we control
637 * from here, has it removed from the other driver model
638 * subsystems it was added to in device_add(), and removes it
639 * from the kobject hierarchy.
640 *
641 * NOTE: this should be called manually _iff_ device_add() was
642 * also called manually.
643 */
644
645 void device_del(struct device * dev)
646 {
647 struct device * parent = dev->parent;
648 struct class_interface *class_intf;
649
650 if (parent)
651 klist_del(&dev->knode_parent);
652 if (dev->devt_attr) {
653 device_remove_file(dev, dev->devt_attr);
654 kfree(dev->devt_attr);
655 }
656 if (dev->class) {
657 sysfs_remove_link(&dev->kobj, "subsystem");
658 /* If this is not a "fake" compatible device, remove the
659 * symlink from the class to the device. */
660 if (dev->kobj.parent != &dev->class->subsys.kset.kobj)
661 sysfs_remove_link(&dev->class->subsys.kset.kobj,
662 dev->bus_id);
663 #ifdef CONFIG_SYSFS_DEPRECATED
664 if (parent) {
665 char *class_name = make_class_name(dev->class->name,
666 &dev->kobj);
667 sysfs_remove_link(&dev->parent->kobj, class_name);
668 kfree(class_name);
669 sysfs_remove_link(&dev->kobj, "device");
670 }
671 #endif
672
673 down(&dev->class->sem);
674 /* notify any interfaces that the device is now gone */
675 list_for_each_entry(class_intf, &dev->class->interfaces, node)
676 if (class_intf->remove_dev)
677 class_intf->remove_dev(dev, class_intf);
678 /* remove the device from the class list */
679 list_del_init(&dev->node);
680 up(&dev->class->sem);
681 }
682 device_remove_file(dev, &dev->uevent_attr);
683 device_remove_groups(dev);
684 device_remove_attrs(dev);
685
686 /* Notify the platform of the removal, in case they
687 * need to do anything...
688 */
689 if (platform_notify_remove)
690 platform_notify_remove(dev);
691 if (dev->bus)
692 blocking_notifier_call_chain(&dev->bus->bus_notifier,
693 BUS_NOTIFY_DEL_DEVICE, dev);
694 bus_remove_device(dev);
695 device_pm_remove(dev);
696 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
697 kobject_del(&dev->kobj);
698 if (parent)
699 put_device(parent);
700 }
701
702 /**
703 * device_unregister - unregister device from system.
704 * @dev: device going away.
705 *
706 * We do this in two parts, like we do device_register(). First,
707 * we remove it from all the subsystems with device_del(), then
708 * we decrement the reference count via put_device(). If that
709 * is the final reference count, the device will be cleaned up
710 * via device_release() above. Otherwise, the structure will
711 * stick around until the final reference to the device is dropped.
712 */
713 void device_unregister(struct device * dev)
714 {
715 pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id);
716 device_del(dev);
717 put_device(dev);
718 }
719
720
721 static struct device * next_device(struct klist_iter * i)
722 {
723 struct klist_node * n = klist_next(i);
724 return n ? container_of(n, struct device, knode_parent) : NULL;
725 }
726
727 /**
728 * device_for_each_child - device child iterator.
729 * @parent: parent struct device.
730 * @data: data for the callback.
731 * @fn: function to be called for each device.
732 *
733 * Iterate over @parent's child devices, and call @fn for each,
734 * passing it @data.
735 *
736 * We check the return of @fn each time. If it returns anything
737 * other than 0, we break out and return that value.
738 */
739 int device_for_each_child(struct device * parent, void * data,
740 int (*fn)(struct device *, void *))
741 {
742 struct klist_iter i;
743 struct device * child;
744 int error = 0;
745
746 klist_iter_init(&parent->klist_children, &i);
747 while ((child = next_device(&i)) && !error)
748 error = fn(child, data);
749 klist_iter_exit(&i);
750 return error;
751 }
752
753 int __init devices_init(void)
754 {
755 return subsystem_register(&devices_subsys);
756 }
757
758 EXPORT_SYMBOL_GPL(device_for_each_child);
759
760 EXPORT_SYMBOL_GPL(device_initialize);
761 EXPORT_SYMBOL_GPL(device_add);
762 EXPORT_SYMBOL_GPL(device_register);
763
764 EXPORT_SYMBOL_GPL(device_del);
765 EXPORT_SYMBOL_GPL(device_unregister);
766 EXPORT_SYMBOL_GPL(get_device);
767 EXPORT_SYMBOL_GPL(put_device);
768
769 EXPORT_SYMBOL_GPL(device_create_file);
770 EXPORT_SYMBOL_GPL(device_remove_file);
771
772
773 static void device_create_release(struct device *dev)
774 {
775 pr_debug("%s called for %s\n", __FUNCTION__, dev->bus_id);
776 kfree(dev);
777 }
778
779 /**
780 * device_create - creates a device and registers it with sysfs
781 * @class: pointer to the struct class that this device should be registered to
782 * @parent: pointer to the parent struct device of this new device, if any
783 * @devt: the dev_t for the char device to be added
784 * @fmt: string for the device's name
785 *
786 * This function can be used by char device classes. A struct device
787 * will be created in sysfs, registered to the specified class.
788 *
789 * A "dev" file will be created, showing the dev_t for the device, if
790 * the dev_t is not 0,0.
791 * If a pointer to a parent struct device is passed in, the newly created
792 * struct device will be a child of that device in sysfs.
793 * The pointer to the struct device will be returned from the call.
794 * Any further sysfs files that might be required can be created using this
795 * pointer.
796 *
797 * Note: the struct class passed to this function must have previously
798 * been created with a call to class_create().
799 */
800 struct device *device_create(struct class *class, struct device *parent,
801 dev_t devt, const char *fmt, ...)
802 {
803 va_list args;
804 struct device *dev = NULL;
805 int retval = -ENODEV;
806
807 if (class == NULL || IS_ERR(class))
808 goto error;
809
810 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
811 if (!dev) {
812 retval = -ENOMEM;
813 goto error;
814 }
815
816 dev->devt = devt;
817 dev->class = class;
818 dev->parent = parent;
819 dev->release = device_create_release;
820
821 va_start(args, fmt);
822 vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args);
823 va_end(args);
824 retval = device_register(dev);
825 if (retval)
826 goto error;
827
828 return dev;
829
830 error:
831 kfree(dev);
832 return ERR_PTR(retval);
833 }
834 EXPORT_SYMBOL_GPL(device_create);
835
836 /**
837 * device_destroy - removes a device that was created with device_create()
838 * @class: pointer to the struct class that this device was registered with
839 * @devt: the dev_t of the device that was previously registered
840 *
841 * This call unregisters and cleans up a device that was created with a
842 * call to device_create().
843 */
844 void device_destroy(struct class *class, dev_t devt)
845 {
846 struct device *dev = NULL;
847 struct device *dev_tmp;
848
849 down(&class->sem);
850 list_for_each_entry(dev_tmp, &class->devices, node) {
851 if (dev_tmp->devt == devt) {
852 dev = dev_tmp;
853 break;
854 }
855 }
856 up(&class->sem);
857
858 if (dev)
859 device_unregister(dev);
860 }
861 EXPORT_SYMBOL_GPL(device_destroy);
862
863 /**
864 * device_rename - renames a device
865 * @dev: the pointer to the struct device to be renamed
866 * @new_name: the new name of the device
867 */
868 int device_rename(struct device *dev, char *new_name)
869 {
870 char *old_class_name = NULL;
871 char *new_class_name = NULL;
872 char *old_symlink_name = NULL;
873 int error;
874
875 dev = get_device(dev);
876 if (!dev)
877 return -EINVAL;
878
879 pr_debug("DEVICE: renaming '%s' to '%s'\n", dev->bus_id, new_name);
880
881 #ifdef CONFIG_SYSFS_DEPRECATED
882 if ((dev->class) && (dev->parent))
883 old_class_name = make_class_name(dev->class->name, &dev->kobj);
884 #endif
885
886 if (dev->class) {
887 old_symlink_name = kmalloc(BUS_ID_SIZE, GFP_KERNEL);
888 if (!old_symlink_name) {
889 error = -ENOMEM;
890 goto out_free_old_class;
891 }
892 strlcpy(old_symlink_name, dev->bus_id, BUS_ID_SIZE);
893 }
894
895 strlcpy(dev->bus_id, new_name, BUS_ID_SIZE);
896
897 error = kobject_rename(&dev->kobj, new_name);
898
899 #ifdef CONFIG_SYSFS_DEPRECATED
900 if (old_class_name) {
901 new_class_name = make_class_name(dev->class->name, &dev->kobj);
902 if (new_class_name) {
903 sysfs_create_link(&dev->parent->kobj, &dev->kobj,
904 new_class_name);
905 sysfs_remove_link(&dev->parent->kobj, old_class_name);
906 }
907 }
908 #endif
909
910 if (dev->class) {
911 sysfs_remove_link(&dev->class->subsys.kset.kobj,
912 old_symlink_name);
913 sysfs_create_link(&dev->class->subsys.kset.kobj, &dev->kobj,
914 dev->bus_id);
915 }
916 put_device(dev);
917
918 kfree(new_class_name);
919 kfree(old_symlink_name);
920 out_free_old_class:
921 kfree(old_class_name);
922
923 return error;
924 }
This page took 0.061594 seconds and 5 git commands to generate.