]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/base/core.c
Driver core: add device_type to struct device
[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->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);
107         }
108 }
109
110 static struct kobj_type ktype_device = {
111         .release        = device_release,
112         .sysfs_ops      = &dev_sysfs_ops,
113 };
114
115
116 static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
117 {
118         struct kobj_type *ktype = get_ktype(kobj);
119
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;
126         }
127         return 0;
128 }
129
130 static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
131 {
132         struct device *dev = to_dev(kobj);
133
134         if (dev->bus)
135                 return dev->bus->name;
136         if (dev->class)
137                 return dev->class->name;
138         return NULL;
139 }
140
141 static int dev_uevent(struct kset *kset, struct kobject *kobj, char **envp,
142                         int num_envp, char *buffer, int buffer_size)
143 {
144         struct device *dev = to_dev(kobj);
145         int i = 0;
146         int length = 0;
147         int retval = 0;
148
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));
157         }
158
159         if (dev->driver)
160                 add_uevent_var(envp, num_envp, &i,
161                                buffer, buffer_size, &length,
162                                "DRIVER=%s", dev->driver->name);
163
164 #ifdef CONFIG_SYSFS_DEPRECATED
165         if (dev->class) {
166                 struct device *parent = dev->parent;
167
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;
173
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);
179
180                         add_uevent_var(envp, num_envp, &i,
181                                        buffer, buffer_size, &length,
182                                        "PHYSDEVBUS=%s", parent->bus->name);
183
184                         if (parent->driver)
185                                 add_uevent_var(envp, num_envp, &i,
186                                                buffer, buffer_size, &length,
187                                                "PHYSDEVDRIVER=%s", parent->driver->name);
188                 }
189         } else if (dev->bus) {
190                 add_uevent_var(envp, num_envp, &i,
191                                buffer, buffer_size, &length,
192                                "PHYSDEVBUS=%s", dev->bus->name);
193
194                 if (dev->driver)
195                         add_uevent_var(envp, num_envp, &i,
196                                        buffer, buffer_size, &length,
197                                        "PHYSDEVDRIVER=%s", dev->driver->name);
198         }
199 #endif
200
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;
207
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);
214         }
215
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);
222         }
223
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);
230         }
231
232         return retval;
233 }
234
235 static struct kset_uevent_ops device_uevent_ops = {
236         .filter =       dev_uevent_filter,
237         .name =         dev_uevent_name,
238         .uevent =       dev_uevent,
239 };
240
241 static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
242                             const char *buf, size_t count)
243 {
244         kobject_uevent(&dev->kobj, KOBJ_ADD);
245         return count;
246 }
247
248 static int device_add_groups(struct device *dev)
249 {
250         int i;
251         int error = 0;
252
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;
260                         }
261                 }
262         }
263 out:
264         return error;
265 }
266
267 static void device_remove_groups(struct device *dev)
268 {
269         int i;
270         if (dev->groups) {
271                 for (i = 0; dev->groups[i]; i++) {
272                         sysfs_remove_group(&dev->kobj, dev->groups[i]);
273                 }
274         }
275 }
276
277 static int device_add_attrs(struct device *dev)
278 {
279         struct class *class = dev->class;
280         struct device_type *type = dev->type;
281         int error = 0;
282         int i;
283
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;
289                 }
290                 if (error)
291                         while (--i >= 0)
292                                 device_remove_file(dev, &class->dev_attrs[i]);
293         }
294
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;
300                 }
301                 if (error)
302                         while (--i >= 0)
303                                 device_remove_file(dev, &type->attrs[i]);
304         }
305
306         return error;
307 }
308
309 static void device_remove_attrs(struct device *dev)
310 {
311         struct class *class = dev->class;
312         struct device_type *type = dev->type;
313         int i;
314
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]);
318         }
319
320         if (type && type->attrs) {
321                 for (i = 0; attr_name(type->attrs[i]); i++)
322                         device_remove_file(dev, &type->attrs[i]);
323         }
324 }
325
326
327 static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
328                         char *buf)
329 {
330         return print_dev_t(buf, dev->devt);
331 }
332
333 /*
334  *      devices_subsys - structure to be registered with kobject core.
335  */
336
337 decl_subsys(devices, &ktype_device, &device_uevent_ops);
338
339
340 /**
341  *      device_create_file - create sysfs attribute file for device.
342  *      @dev:   device.
343  *      @attr:  device attribute descriptor.
344  */
345
346 int device_create_file(struct device * dev, struct device_attribute * attr)
347 {
348         int error = 0;
349         if (get_device(dev)) {
350                 error = sysfs_create_file(&dev->kobj, &attr->attr);
351                 put_device(dev);
352         }
353         return error;
354 }
355
356 /**
357  *      device_remove_file - remove sysfs attribute file.
358  *      @dev:   device.
359  *      @attr:  device attribute descriptor.
360  */
361
362 void device_remove_file(struct device * dev, struct device_attribute * attr)
363 {
364         if (get_device(dev)) {
365                 sysfs_remove_file(&dev->kobj, &attr->attr);
366                 put_device(dev);
367         }
368 }
369
370 /**
371  * device_create_bin_file - create sysfs binary attribute file for device.
372  * @dev: device.
373  * @attr: device binary attribute descriptor.
374  */
375 int device_create_bin_file(struct device *dev, struct bin_attribute *attr)
376 {
377         int error = -EINVAL;
378         if (dev)
379                 error = sysfs_create_bin_file(&dev->kobj, attr);
380         return error;
381 }
382 EXPORT_SYMBOL_GPL(device_create_bin_file);
383
384 /**
385  * device_remove_bin_file - remove sysfs binary attribute file
386  * @dev: device.
387  * @attr: device binary attribute descriptor.
388  */
389 void device_remove_bin_file(struct device *dev, struct bin_attribute *attr)
390 {
391         if (dev)
392                 sysfs_remove_bin_file(&dev->kobj, attr);
393 }
394 EXPORT_SYMBOL_GPL(device_remove_bin_file);
395
396 static void klist_children_get(struct klist_node *n)
397 {
398         struct device *dev = container_of(n, struct device, knode_parent);
399
400         get_device(dev);
401 }
402
403 static void klist_children_put(struct klist_node *n)
404 {
405         struct device *dev = container_of(n, struct device, knode_parent);
406
407         put_device(dev);
408 }
409
410
411 /**
412  *      device_initialize - init device structure.
413  *      @dev:   device.
414  *
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).
420  */
421
422 void device_initialize(struct device *dev)
423 {
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         device_init_wakeup(dev, 0);
432         set_dev_node(dev, -1);
433 }
434
435 #ifdef CONFIG_SYSFS_DEPRECATED
436 static struct kobject * get_device_parent(struct device *dev,
437                                           struct device *parent)
438 {
439         /* Set the parent to the class, not the parent device */
440         /* this keeps sysfs from having a symlink to make old udevs happy */
441         if (dev->class)
442                 return &dev->class->subsys.kset.kobj;
443         else if (parent)
444                 return &parent->kobj;
445
446         return NULL;
447 }
448 #else
449 static struct kobject * virtual_device_parent(struct device *dev)
450 {
451         if (!dev->class)
452                 return ERR_PTR(-ENODEV);
453
454         if (!dev->class->virtual_dir) {
455                 static struct kobject *virtual_dir = NULL;
456
457                 if (!virtual_dir)
458                         virtual_dir = kobject_add_dir(&devices_subsys.kset.kobj, "virtual");
459                 dev->class->virtual_dir = kobject_add_dir(virtual_dir, dev->class->name);
460         }
461
462         return dev->class->virtual_dir;
463 }
464
465 static struct kobject * get_device_parent(struct device *dev,
466                                           struct device *parent)
467 {
468         /* if this is a class device, and has no parent, create one */
469         if ((dev->class) && (parent == NULL)) {
470                 return virtual_device_parent(dev);
471         } else if (parent)
472                 return &parent->kobj;
473         return NULL;
474 }
475
476 #endif
477 static int setup_parent(struct device *dev, struct device *parent)
478 {
479         struct kobject *kobj;
480         kobj = get_device_parent(dev, parent);
481         if (IS_ERR(kobj))
482                 return PTR_ERR(kobj);
483         if (kobj)
484                 dev->kobj.parent = kobj;
485         return 0;
486 }
487
488 /**
489  *      device_add - add device to device hierarchy.
490  *      @dev:   device.
491  *
492  *      This is part 2 of device_register(), though may be called
493  *      separately _iff_ device_initialize() has been called separately.
494  *
495  *      This adds it to the kobject hierarchy via kobject_add(), adds it
496  *      to the global and sibling lists for the device, then
497  *      adds it to the other relevant subsystems of the driver model.
498  */
499 int device_add(struct device *dev)
500 {
501         struct device *parent = NULL;
502         char *class_name = NULL;
503         struct class_interface *class_intf;
504         int error = -EINVAL;
505
506         dev = get_device(dev);
507         if (!dev || !strlen(dev->bus_id))
508                 goto Error;
509
510         pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id);
511
512         parent = get_device(dev->parent);
513
514         error = setup_parent(dev, parent);
515         if (error)
516                 goto Error;
517
518         /* first, register with generic layer. */
519         kobject_set_name(&dev->kobj, "%s", dev->bus_id);
520         error = kobject_add(&dev->kobj);
521         if (error)
522                 goto Error;
523
524         /* notify platform of device entry */
525         if (platform_notify)
526                 platform_notify(dev);
527
528         /* notify clients of device entry (new way) */
529         if (dev->bus)
530                 blocking_notifier_call_chain(&dev->bus->bus_notifier,
531                                              BUS_NOTIFY_ADD_DEVICE, dev);
532
533         dev->uevent_attr.attr.name = "uevent";
534         dev->uevent_attr.attr.mode = S_IWUSR;
535         if (dev->driver)
536                 dev->uevent_attr.attr.owner = dev->driver->owner;
537         dev->uevent_attr.store = store_uevent;
538         error = device_create_file(dev, &dev->uevent_attr);
539         if (error)
540                 goto attrError;
541
542         if (MAJOR(dev->devt)) {
543                 struct device_attribute *attr;
544                 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
545                 if (!attr) {
546                         error = -ENOMEM;
547                         goto ueventattrError;
548                 }
549                 attr->attr.name = "dev";
550                 attr->attr.mode = S_IRUGO;
551                 if (dev->driver)
552                         attr->attr.owner = dev->driver->owner;
553                 attr->show = show_dev;
554                 error = device_create_file(dev, attr);
555                 if (error) {
556                         kfree(attr);
557                         goto ueventattrError;
558                 }
559
560                 dev->devt_attr = attr;
561         }
562
563         if (dev->class) {
564                 sysfs_create_link(&dev->kobj, &dev->class->subsys.kset.kobj,
565                                   "subsystem");
566                 /* If this is not a "fake" compatible device, then create the
567                  * symlink from the class to the device. */
568                 if (dev->kobj.parent != &dev->class->subsys.kset.kobj)
569                         sysfs_create_link(&dev->class->subsys.kset.kobj,
570                                           &dev->kobj, dev->bus_id);
571 #ifdef CONFIG_SYSFS_DEPRECATED
572                 if (parent) {
573                         sysfs_create_link(&dev->kobj, &dev->parent->kobj,
574                                                         "device");
575                         class_name = make_class_name(dev->class->name,
576                                                         &dev->kobj);
577                         if (class_name)
578                                 sysfs_create_link(&dev->parent->kobj,
579                                                   &dev->kobj, class_name);
580                 }
581 #endif
582         }
583
584         if ((error = device_add_attrs(dev)))
585                 goto AttrsError;
586         if ((error = device_add_groups(dev)))
587                 goto GroupError;
588         if ((error = device_pm_add(dev)))
589                 goto PMError;
590         if ((error = bus_add_device(dev)))
591                 goto BusError;
592         kobject_uevent(&dev->kobj, KOBJ_ADD);
593         if ((error = bus_attach_device(dev)))
594                 goto AttachError;
595         if (parent)
596                 klist_add_tail(&dev->knode_parent, &parent->klist_children);
597
598         if (dev->class) {
599                 down(&dev->class->sem);
600                 /* tie the class to the device */
601                 list_add_tail(&dev->node, &dev->class->devices);
602
603                 /* notify any interfaces that the device is here */
604                 list_for_each_entry(class_intf, &dev->class->interfaces, node)
605                         if (class_intf->add_dev)
606                                 class_intf->add_dev(dev, class_intf);
607                 up(&dev->class->sem);
608         }
609  Done:
610         kfree(class_name);
611         put_device(dev);
612         return error;
613  AttachError:
614         bus_remove_device(dev);
615  BusError:
616         device_pm_remove(dev);
617  PMError:
618         if (dev->bus)
619                 blocking_notifier_call_chain(&dev->bus->bus_notifier,
620                                              BUS_NOTIFY_DEL_DEVICE, dev);
621         device_remove_groups(dev);
622  GroupError:
623         device_remove_attrs(dev);
624  AttrsError:
625         if (dev->devt_attr) {
626                 device_remove_file(dev, dev->devt_attr);
627                 kfree(dev->devt_attr);
628         }
629  ueventattrError:
630         device_remove_file(dev, &dev->uevent_attr);
631  attrError:
632         kobject_uevent(&dev->kobj, KOBJ_REMOVE);
633         kobject_del(&dev->kobj);
634  Error:
635         if (parent)
636                 put_device(parent);
637         goto Done;
638 }
639
640
641 /**
642  *      device_register - register a device with the system.
643  *      @dev:   pointer to the device structure
644  *
645  *      This happens in two clean steps - initialize the device
646  *      and add it to the system. The two steps can be called
647  *      separately, but this is the easiest and most common.
648  *      I.e. you should only call the two helpers separately if
649  *      have a clearly defined need to use and refcount the device
650  *      before it is added to the hierarchy.
651  */
652
653 int device_register(struct device *dev)
654 {
655         device_initialize(dev);
656         return device_add(dev);
657 }
658
659
660 /**
661  *      get_device - increment reference count for device.
662  *      @dev:   device.
663  *
664  *      This simply forwards the call to kobject_get(), though
665  *      we do take care to provide for the case that we get a NULL
666  *      pointer passed in.
667  */
668
669 struct device * get_device(struct device * dev)
670 {
671         return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
672 }
673
674
675 /**
676  *      put_device - decrement reference count.
677  *      @dev:   device in question.
678  */
679 void put_device(struct device * dev)
680 {
681         if (dev)
682                 kobject_put(&dev->kobj);
683 }
684
685
686 /**
687  *      device_del - delete device from system.
688  *      @dev:   device.
689  *
690  *      This is the first part of the device unregistration
691  *      sequence. This removes the device from the lists we control
692  *      from here, has it removed from the other driver model
693  *      subsystems it was added to in device_add(), and removes it
694  *      from the kobject hierarchy.
695  *
696  *      NOTE: this should be called manually _iff_ device_add() was
697  *      also called manually.
698  */
699
700 void device_del(struct device * dev)
701 {
702         struct device * parent = dev->parent;
703         struct class_interface *class_intf;
704
705         if (parent)
706                 klist_del(&dev->knode_parent);
707         if (dev->devt_attr) {
708                 device_remove_file(dev, dev->devt_attr);
709                 kfree(dev->devt_attr);
710         }
711         if (dev->class) {
712                 sysfs_remove_link(&dev->kobj, "subsystem");
713                 /* If this is not a "fake" compatible device, remove the
714                  * symlink from the class to the device. */
715                 if (dev->kobj.parent != &dev->class->subsys.kset.kobj)
716                         sysfs_remove_link(&dev->class->subsys.kset.kobj,
717                                           dev->bus_id);
718 #ifdef CONFIG_SYSFS_DEPRECATED
719                 if (parent) {
720                         char *class_name = make_class_name(dev->class->name,
721                                                            &dev->kobj);
722                         if (class_name)
723                                 sysfs_remove_link(&dev->parent->kobj,
724                                                   class_name);
725                         kfree(class_name);
726                         sysfs_remove_link(&dev->kobj, "device");
727                 }
728 #endif
729
730                 down(&dev->class->sem);
731                 /* notify any interfaces that the device is now gone */
732                 list_for_each_entry(class_intf, &dev->class->interfaces, node)
733                         if (class_intf->remove_dev)
734                                 class_intf->remove_dev(dev, class_intf);
735                 /* remove the device from the class list */
736                 list_del_init(&dev->node);
737                 up(&dev->class->sem);
738         }
739         device_remove_file(dev, &dev->uevent_attr);
740         device_remove_groups(dev);
741         device_remove_attrs(dev);
742         bus_remove_device(dev);
743
744         /* Notify the platform of the removal, in case they
745          * need to do anything...
746          */
747         if (platform_notify_remove)
748                 platform_notify_remove(dev);
749         if (dev->bus)
750                 blocking_notifier_call_chain(&dev->bus->bus_notifier,
751                                              BUS_NOTIFY_DEL_DEVICE, dev);
752         device_pm_remove(dev);
753         kobject_uevent(&dev->kobj, KOBJ_REMOVE);
754         kobject_del(&dev->kobj);
755         if (parent)
756                 put_device(parent);
757 }
758
759 /**
760  *      device_unregister - unregister device from system.
761  *      @dev:   device going away.
762  *
763  *      We do this in two parts, like we do device_register(). First,
764  *      we remove it from all the subsystems with device_del(), then
765  *      we decrement the reference count via put_device(). If that
766  *      is the final reference count, the device will be cleaned up
767  *      via device_release() above. Otherwise, the structure will
768  *      stick around until the final reference to the device is dropped.
769  */
770 void device_unregister(struct device * dev)
771 {
772         pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id);
773         device_del(dev);
774         put_device(dev);
775 }
776
777
778 static struct device * next_device(struct klist_iter * i)
779 {
780         struct klist_node * n = klist_next(i);
781         return n ? container_of(n, struct device, knode_parent) : NULL;
782 }
783
784 /**
785  *      device_for_each_child - device child iterator.
786  *      @parent: parent struct device.
787  *      @data:  data for the callback.
788  *      @fn:    function to be called for each device.
789  *
790  *      Iterate over @parent's child devices, and call @fn for each,
791  *      passing it @data.
792  *
793  *      We check the return of @fn each time. If it returns anything
794  *      other than 0, we break out and return that value.
795  */
796 int device_for_each_child(struct device * parent, void * data,
797                      int (*fn)(struct device *, void *))
798 {
799         struct klist_iter i;
800         struct device * child;
801         int error = 0;
802
803         klist_iter_init(&parent->klist_children, &i);
804         while ((child = next_device(&i)) && !error)
805                 error = fn(child, data);
806         klist_iter_exit(&i);
807         return error;
808 }
809
810 /**
811  * device_find_child - device iterator for locating a particular device.
812  * @parent: parent struct device
813  * @data: Data to pass to match function
814  * @match: Callback function to check device
815  *
816  * This is similar to the device_for_each_child() function above, but it
817  * returns a reference to a device that is 'found' for later use, as
818  * determined by the @match callback.
819  *
820  * The callback should return 0 if the device doesn't match and non-zero
821  * if it does.  If the callback returns non-zero and a reference to the
822  * current device can be obtained, this function will return to the caller
823  * and not iterate over any more devices.
824  */
825 struct device * device_find_child(struct device *parent, void *data,
826                                   int (*match)(struct device *, void *))
827 {
828         struct klist_iter i;
829         struct device *child;
830
831         if (!parent)
832                 return NULL;
833
834         klist_iter_init(&parent->klist_children, &i);
835         while ((child = next_device(&i)))
836                 if (match(child, data) && get_device(child))
837                         break;
838         klist_iter_exit(&i);
839         return child;
840 }
841
842 int __init devices_init(void)
843 {
844         return subsystem_register(&devices_subsys);
845 }
846
847 EXPORT_SYMBOL_GPL(device_for_each_child);
848 EXPORT_SYMBOL_GPL(device_find_child);
849
850 EXPORT_SYMBOL_GPL(device_initialize);
851 EXPORT_SYMBOL_GPL(device_add);
852 EXPORT_SYMBOL_GPL(device_register);
853
854 EXPORT_SYMBOL_GPL(device_del);
855 EXPORT_SYMBOL_GPL(device_unregister);
856 EXPORT_SYMBOL_GPL(get_device);
857 EXPORT_SYMBOL_GPL(put_device);
858
859 EXPORT_SYMBOL_GPL(device_create_file);
860 EXPORT_SYMBOL_GPL(device_remove_file);
861
862
863 static void device_create_release(struct device *dev)
864 {
865         pr_debug("%s called for %s\n", __FUNCTION__, dev->bus_id);
866         kfree(dev);
867 }
868
869 /**
870  * device_create - creates a device and registers it with sysfs
871  * @class: pointer to the struct class that this device should be registered to
872  * @parent: pointer to the parent struct device of this new device, if any
873  * @devt: the dev_t for the char device to be added
874  * @fmt: string for the device's name
875  *
876  * This function can be used by char device classes.  A struct device
877  * will be created in sysfs, registered to the specified class.
878  *
879  * A "dev" file will be created, showing the dev_t for the device, if
880  * the dev_t is not 0,0.
881  * If a pointer to a parent struct device is passed in, the newly created
882  * struct device will be a child of that device in sysfs.
883  * The pointer to the struct device will be returned from the call.
884  * Any further sysfs files that might be required can be created using this
885  * pointer.
886  *
887  * Note: the struct class passed to this function must have previously
888  * been created with a call to class_create().
889  */
890 struct device *device_create(struct class *class, struct device *parent,
891                              dev_t devt, const char *fmt, ...)
892 {
893         va_list args;
894         struct device *dev = NULL;
895         int retval = -ENODEV;
896
897         if (class == NULL || IS_ERR(class))
898                 goto error;
899
900         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
901         if (!dev) {
902                 retval = -ENOMEM;
903                 goto error;
904         }
905
906         dev->devt = devt;
907         dev->class = class;
908         dev->parent = parent;
909         dev->release = device_create_release;
910
911         va_start(args, fmt);
912         vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args);
913         va_end(args);
914         retval = device_register(dev);
915         if (retval)
916                 goto error;
917
918         return dev;
919
920 error:
921         kfree(dev);
922         return ERR_PTR(retval);
923 }
924 EXPORT_SYMBOL_GPL(device_create);
925
926 /**
927  * device_destroy - removes a device that was created with device_create()
928  * @class: pointer to the struct class that this device was registered with
929  * @devt: the dev_t of the device that was previously registered
930  *
931  * This call unregisters and cleans up a device that was created with a
932  * call to device_create().
933  */
934 void device_destroy(struct class *class, dev_t devt)
935 {
936         struct device *dev = NULL;
937         struct device *dev_tmp;
938
939         down(&class->sem);
940         list_for_each_entry(dev_tmp, &class->devices, node) {
941                 if (dev_tmp->devt == devt) {
942                         dev = dev_tmp;
943                         break;
944                 }
945         }
946         up(&class->sem);
947
948         if (dev)
949                 device_unregister(dev);
950 }
951 EXPORT_SYMBOL_GPL(device_destroy);
952
953 /**
954  * device_rename - renames a device
955  * @dev: the pointer to the struct device to be renamed
956  * @new_name: the new name of the device
957  */
958 int device_rename(struct device *dev, char *new_name)
959 {
960         char *old_class_name = NULL;
961         char *new_class_name = NULL;
962         char *old_symlink_name = NULL;
963         int error;
964
965         dev = get_device(dev);
966         if (!dev)
967                 return -EINVAL;
968
969         pr_debug("DEVICE: renaming '%s' to '%s'\n", dev->bus_id, new_name);
970
971 #ifdef CONFIG_SYSFS_DEPRECATED
972         if ((dev->class) && (dev->parent))
973                 old_class_name = make_class_name(dev->class->name, &dev->kobj);
974 #endif
975
976         if (dev->class) {
977                 old_symlink_name = kmalloc(BUS_ID_SIZE, GFP_KERNEL);
978                 if (!old_symlink_name) {
979                         error = -ENOMEM;
980                         goto out_free_old_class;
981                 }
982                 strlcpy(old_symlink_name, dev->bus_id, BUS_ID_SIZE);
983         }
984
985         strlcpy(dev->bus_id, new_name, BUS_ID_SIZE);
986
987         error = kobject_rename(&dev->kobj, new_name);
988
989 #ifdef CONFIG_SYSFS_DEPRECATED
990         if (old_class_name) {
991                 new_class_name = make_class_name(dev->class->name, &dev->kobj);
992                 if (new_class_name) {
993                         sysfs_create_link(&dev->parent->kobj, &dev->kobj,
994                                           new_class_name);
995                         sysfs_remove_link(&dev->parent->kobj, old_class_name);
996                 }
997         }
998 #endif
999
1000         if (dev->class) {
1001                 sysfs_remove_link(&dev->class->subsys.kset.kobj,
1002                                   old_symlink_name);
1003                 sysfs_create_link(&dev->class->subsys.kset.kobj, &dev->kobj,
1004                                   dev->bus_id);
1005         }
1006         put_device(dev);
1007
1008         kfree(new_class_name);
1009         kfree(old_symlink_name);
1010  out_free_old_class:
1011         kfree(old_class_name);
1012
1013         return error;
1014 }
1015
1016
1017 static int device_move_class_links(struct device *dev,
1018                                    struct device *old_parent,
1019                                    struct device *new_parent)
1020 {
1021 #ifdef CONFIG_SYSFS_DEPRECATED
1022         int error;
1023         char *class_name;
1024
1025         class_name = make_class_name(dev->class->name, &dev->kobj);
1026         if (!class_name) {
1027                 error = -ENOMEM;
1028                 goto out;
1029         }
1030         if (old_parent) {
1031                 sysfs_remove_link(&dev->kobj, "device");
1032                 sysfs_remove_link(&old_parent->kobj, class_name);
1033         }
1034         if (new_parent) {
1035                 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1036                                           "device");
1037                 if (error)
1038                         goto out;
1039                 error = sysfs_create_link(&new_parent->kobj, &dev->kobj,
1040                                           class_name);
1041                 if (error)
1042                         sysfs_remove_link(&dev->kobj, "device");
1043         }
1044         else
1045                 error = 0;
1046 out:
1047         kfree(class_name);
1048         return error;
1049 #else
1050         return 0;
1051 #endif
1052 }
1053
1054 /**
1055  * device_move - moves a device to a new parent
1056  * @dev: the pointer to the struct device to be moved
1057  * @new_parent: the new parent of the device (can by NULL)
1058  */
1059 int device_move(struct device *dev, struct device *new_parent)
1060 {
1061         int error;
1062         struct device *old_parent;
1063         struct kobject *new_parent_kobj;
1064
1065         dev = get_device(dev);
1066         if (!dev)
1067                 return -EINVAL;
1068
1069         new_parent = get_device(new_parent);
1070         new_parent_kobj = get_device_parent (dev, new_parent);
1071         if (IS_ERR(new_parent_kobj)) {
1072                 error = PTR_ERR(new_parent_kobj);
1073                 put_device(new_parent);
1074                 goto out;
1075         }
1076         pr_debug("DEVICE: moving '%s' to '%s'\n", dev->bus_id,
1077                  new_parent ? new_parent->bus_id : "<NULL>");
1078         error = kobject_move(&dev->kobj, new_parent_kobj);
1079         if (error) {
1080                 put_device(new_parent);
1081                 goto out;
1082         }
1083         old_parent = dev->parent;
1084         dev->parent = new_parent;
1085         if (old_parent)
1086                 klist_remove(&dev->knode_parent);
1087         if (new_parent)
1088                 klist_add_tail(&dev->knode_parent, &new_parent->klist_children);
1089         if (!dev->class)
1090                 goto out_put;
1091         error = device_move_class_links(dev, old_parent, new_parent);
1092         if (error) {
1093                 /* We ignore errors on cleanup since we're hosed anyway... */
1094                 device_move_class_links(dev, new_parent, old_parent);
1095                 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
1096                         if (new_parent)
1097                                 klist_remove(&dev->knode_parent);
1098                         if (old_parent)
1099                                 klist_add_tail(&dev->knode_parent,
1100                                                &old_parent->klist_children);
1101                 }
1102                 put_device(new_parent);
1103                 goto out;
1104         }
1105 out_put:
1106         put_device(old_parent);
1107 out:
1108         put_device(dev);
1109         return error;
1110 }
1111
1112 EXPORT_SYMBOL_GPL(device_move);