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