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