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