Driver core: add ability for classes to handle devices properly
[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
21 #include <asm/semaphore.h>
22
23 #include "base.h"
24 #include "power/power.h"
25
26 int (*platform_notify)(struct device * dev) = NULL;
27 int (*platform_notify_remove)(struct device * dev) = NULL;
28
29 /*
30  * sysfs bindings for devices.
31  */
32
33 /**
34  * dev_driver_string - Return a device's driver name, if at all possible
35  * @dev: struct device to get the name of
36  *
37  * Will return the device's driver's name if it is bound to a device.  If
38  * the device is not bound to a device, it will return the name of the bus
39  * it is attached to.  If it is not attached to a bus either, an empty
40  * string will be returned.
41  */
42 const char *dev_driver_string(struct device *dev)
43 {
44         return dev->driver ? dev->driver->name :
45                         (dev->bus ? dev->bus->name : "");
46 }
47 EXPORT_SYMBOL_GPL(dev_driver_string);
48
49 #define to_dev(obj) container_of(obj, struct device, kobj)
50 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
51
52 static ssize_t
53 dev_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
54 {
55         struct device_attribute * dev_attr = to_dev_attr(attr);
56         struct device * dev = to_dev(kobj);
57         ssize_t ret = -EIO;
58
59         if (dev_attr->show)
60                 ret = dev_attr->show(dev, dev_attr, buf);
61         return ret;
62 }
63
64 static ssize_t
65 dev_attr_store(struct kobject * kobj, struct attribute * attr,
66                const char * buf, size_t count)
67 {
68         struct device_attribute * dev_attr = to_dev_attr(attr);
69         struct device * dev = to_dev(kobj);
70         ssize_t ret = -EIO;
71
72         if (dev_attr->store)
73                 ret = dev_attr->store(dev, dev_attr, buf, count);
74         return ret;
75 }
76
77 static struct sysfs_ops dev_sysfs_ops = {
78         .show   = dev_attr_show,
79         .store  = dev_attr_store,
80 };
81
82
83 /**
84  *      device_release - free device structure.
85  *      @kobj:  device's kobject.
86  *
87  *      This is called once the reference count for the object
88  *      reaches 0. We forward the call to the device's release
89  *      method, which should handle actually freeing the structure.
90  */
91 static void device_release(struct kobject * kobj)
92 {
93         struct device * dev = to_dev(kobj);
94
95         if (dev->release)
96                 dev->release(dev);
97         else if (dev->class && dev->class->dev_release)
98                 dev->class->dev_release(dev);
99         else {
100                 printk(KERN_ERR "Device '%s' does not have a release() function, "
101                         "it is broken and must be fixed.\n",
102                         dev->bus_id);
103                 WARN_ON(1);
104         }
105 }
106
107 static struct kobj_type ktype_device = {
108         .release        = device_release,
109         .sysfs_ops      = &dev_sysfs_ops,
110 };
111
112
113 static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
114 {
115         struct kobj_type *ktype = get_ktype(kobj);
116
117         if (ktype == &ktype_device) {
118                 struct device *dev = to_dev(kobj);
119                 if (dev->bus)
120                         return 1;
121                 if (dev->class)
122                         return 1;
123         }
124         return 0;
125 }
126
127 static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
128 {
129         struct device *dev = to_dev(kobj);
130
131         if (dev->bus)
132                 return dev->bus->name;
133         if (dev->class)
134                 return dev->class->name;
135         return NULL;
136 }
137
138 static int dev_uevent(struct kset *kset, struct kobject *kobj, char **envp,
139                         int num_envp, char *buffer, int buffer_size)
140 {
141         struct device *dev = to_dev(kobj);
142         int i = 0;
143         int length = 0;
144         int retval = 0;
145
146         /* add the major/minor if present */
147         if (MAJOR(dev->devt)) {
148                 add_uevent_var(envp, num_envp, &i,
149                                buffer, buffer_size, &length,
150                                "MAJOR=%u", MAJOR(dev->devt));
151                 add_uevent_var(envp, num_envp, &i,
152                                buffer, buffer_size, &length,
153                                "MINOR=%u", MINOR(dev->devt));
154         }
155
156         /* add bus name (same as SUBSYSTEM, deprecated) */
157         if (dev->bus)
158                 add_uevent_var(envp, num_envp, &i,
159                                buffer, buffer_size, &length,
160                                "PHYSDEVBUS=%s", dev->bus->name);
161
162         /* add driver name (PHYSDEV* values are deprecated)*/
163         if (dev->driver) {
164                 add_uevent_var(envp, num_envp, &i,
165                                buffer, buffer_size, &length,
166                                "DRIVER=%s", dev->driver->name);
167                 add_uevent_var(envp, num_envp, &i,
168                                buffer, buffer_size, &length,
169                                "PHYSDEVDRIVER=%s", dev->driver->name);
170         }
171
172         /* terminate, set to next free slot, shrink available space */
173         envp[i] = NULL;
174         envp = &envp[i];
175         num_envp -= i;
176         buffer = &buffer[length];
177         buffer_size -= length;
178
179         if (dev->bus && dev->bus->uevent) {
180                 /* have the bus specific function add its stuff */
181                 retval = dev->bus->uevent(dev, envp, num_envp, buffer, buffer_size);
182                         if (retval) {
183                         pr_debug ("%s - uevent() returned %d\n",
184                                   __FUNCTION__, retval);
185                 }
186         }
187
188         if (dev->class && dev->class->dev_uevent) {
189                 /* have the class specific function add its stuff */
190                 retval = dev->class->dev_uevent(dev, envp, num_envp, buffer, buffer_size);
191                         if (retval) {
192                                 pr_debug("%s - dev_uevent() returned %d\n",
193                                          __FUNCTION__, retval);
194                 }
195         }
196
197         return retval;
198 }
199
200 static struct kset_uevent_ops device_uevent_ops = {
201         .filter =       dev_uevent_filter,
202         .name =         dev_uevent_name,
203         .uevent =       dev_uevent,
204 };
205
206 static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
207                             const char *buf, size_t count)
208 {
209         kobject_uevent(&dev->kobj, KOBJ_ADD);
210         return count;
211 }
212
213 static int device_add_groups(struct device *dev)
214 {
215         int i;
216         int error = 0;
217
218         if (dev->groups) {
219                 for (i = 0; dev->groups[i]; i++) {
220                         error = sysfs_create_group(&dev->kobj, dev->groups[i]);
221                         if (error) {
222                                 while (--i >= 0)
223                                         sysfs_remove_group(&dev->kobj, dev->groups[i]);
224                                 goto out;
225                         }
226                 }
227         }
228 out:
229         return error;
230 }
231
232 static void device_remove_groups(struct device *dev)
233 {
234         int i;
235         if (dev->groups) {
236                 for (i = 0; dev->groups[i]; i++) {
237                         sysfs_remove_group(&dev->kobj, dev->groups[i]);
238                 }
239         }
240 }
241
242 static int device_add_attrs(struct device *dev)
243 {
244         struct class *class = dev->class;
245         int error = 0;
246         int i;
247
248         if (!class)
249                 return 0;
250
251         if (class->dev_attrs) {
252                 for (i = 0; attr_name(class->dev_attrs[i]); i++) {
253                         error = device_create_file(dev, &class->dev_attrs[i]);
254                         if (error)
255                                 break;
256                 }
257         }
258         if (error)
259                 while (--i >= 0)
260                         device_remove_file(dev, &class->dev_attrs[i]);
261         return error;
262 }
263
264 static void device_remove_attrs(struct device *dev)
265 {
266         struct class *class = dev->class;
267         int i;
268
269         if (!class)
270                 return;
271
272         if (class->dev_attrs) {
273                 for (i = 0; attr_name(class->dev_attrs[i]); i++)
274                         device_remove_file(dev, &class->dev_attrs[i]);
275         }
276 }
277
278
279 static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
280                         char *buf)
281 {
282         return print_dev_t(buf, dev->devt);
283 }
284
285 /*
286  *      devices_subsys - structure to be registered with kobject core.
287  */
288
289 decl_subsys(devices, &ktype_device, &device_uevent_ops);
290
291
292 /**
293  *      device_create_file - create sysfs attribute file for device.
294  *      @dev:   device.
295  *      @attr:  device attribute descriptor.
296  */
297
298 int device_create_file(struct device * dev, struct device_attribute * attr)
299 {
300         int error = 0;
301         if (get_device(dev)) {
302                 error = sysfs_create_file(&dev->kobj, &attr->attr);
303                 put_device(dev);
304         }
305         return error;
306 }
307
308 /**
309  *      device_remove_file - remove sysfs attribute file.
310  *      @dev:   device.
311  *      @attr:  device attribute descriptor.
312  */
313
314 void device_remove_file(struct device * dev, struct device_attribute * attr)
315 {
316         if (get_device(dev)) {
317                 sysfs_remove_file(&dev->kobj, &attr->attr);
318                 put_device(dev);
319         }
320 }
321
322 static void klist_children_get(struct klist_node *n)
323 {
324         struct device *dev = container_of(n, struct device, knode_parent);
325
326         get_device(dev);
327 }
328
329 static void klist_children_put(struct klist_node *n)
330 {
331         struct device *dev = container_of(n, struct device, knode_parent);
332
333         put_device(dev);
334 }
335
336
337 /**
338  *      device_initialize - init device structure.
339  *      @dev:   device.
340  *
341  *      This prepares the device for use by other layers,
342  *      including adding it to the device hierarchy.
343  *      It is the first half of device_register(), if called by
344  *      that, though it can also be called separately, so one
345  *      may use @dev's fields (e.g. the refcount).
346  */
347
348 void device_initialize(struct device *dev)
349 {
350         kobj_set_kset_s(dev, devices_subsys);
351         kobject_init(&dev->kobj);
352         klist_init(&dev->klist_children, klist_children_get,
353                    klist_children_put);
354         INIT_LIST_HEAD(&dev->dma_pools);
355         INIT_LIST_HEAD(&dev->node);
356         init_MUTEX(&dev->sem);
357         device_init_wakeup(dev, 0);
358 }
359
360 /**
361  *      device_add - add device to device hierarchy.
362  *      @dev:   device.
363  *
364  *      This is part 2 of device_register(), though may be called
365  *      separately _iff_ device_initialize() has been called separately.
366  *
367  *      This adds it to the kobject hierarchy via kobject_add(), adds it
368  *      to the global and sibling lists for the device, then
369  *      adds it to the other relevant subsystems of the driver model.
370  */
371 int device_add(struct device *dev)
372 {
373         struct device *parent = NULL;
374         char *class_name = NULL;
375         int error = -EINVAL;
376
377         dev = get_device(dev);
378         if (!dev || !strlen(dev->bus_id))
379                 goto Error;
380
381         parent = get_device(dev->parent);
382
383         pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id);
384
385         /* first, register with generic layer. */
386         kobject_set_name(&dev->kobj, "%s", dev->bus_id);
387         if (parent)
388                 dev->kobj.parent = &parent->kobj;
389
390         if ((error = kobject_add(&dev->kobj)))
391                 goto Error;
392
393         dev->uevent_attr.attr.name = "uevent";
394         dev->uevent_attr.attr.mode = S_IWUSR;
395         if (dev->driver)
396                 dev->uevent_attr.attr.owner = dev->driver->owner;
397         dev->uevent_attr.store = store_uevent;
398         device_create_file(dev, &dev->uevent_attr);
399
400         if (MAJOR(dev->devt)) {
401                 struct device_attribute *attr;
402                 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
403                 if (!attr) {
404                         error = -ENOMEM;
405                         goto PMError;
406                 }
407                 attr->attr.name = "dev";
408                 attr->attr.mode = S_IRUGO;
409                 if (dev->driver)
410                         attr->attr.owner = dev->driver->owner;
411                 attr->show = show_dev;
412                 error = device_create_file(dev, attr);
413                 if (error) {
414                         kfree(attr);
415                         goto attrError;
416                 }
417
418                 dev->devt_attr = attr;
419         }
420
421         if (dev->class) {
422                 sysfs_create_link(&dev->kobj, &dev->class->subsys.kset.kobj,
423                                   "subsystem");
424                 sysfs_create_link(&dev->class->subsys.kset.kobj, &dev->kobj,
425                                   dev->bus_id);
426                 if (parent) {
427                         sysfs_create_link(&dev->kobj, &dev->parent->kobj, "device");
428                         class_name = make_class_name(dev->class->name, &dev->kobj);
429                         sysfs_create_link(&dev->parent->kobj, &dev->kobj, class_name);
430                 }
431         }
432
433         if ((error = device_add_attrs(dev)))
434                 goto AttrsError;
435         if ((error = device_add_groups(dev)))
436                 goto GroupError;
437         if ((error = device_pm_add(dev)))
438                 goto PMError;
439         if ((error = bus_add_device(dev)))
440                 goto BusError;
441         kobject_uevent(&dev->kobj, KOBJ_ADD);
442         bus_attach_device(dev);
443         if (parent)
444                 klist_add_tail(&dev->knode_parent, &parent->klist_children);
445
446         if (dev->class) {
447                 /* tie the class to the device */
448                 down(&dev->class->sem);
449                 list_add_tail(&dev->node, &dev->class->devices);
450                 up(&dev->class->sem);
451         }
452
453         /* notify platform of device entry */
454         if (platform_notify)
455                 platform_notify(dev);
456  Done:
457         kfree(class_name);
458         put_device(dev);
459         return error;
460  BusError:
461         device_pm_remove(dev);
462  PMError:
463         device_remove_groups(dev);
464  GroupError:
465         device_remove_attrs(dev);
466  AttrsError:
467         if (dev->devt_attr) {
468                 device_remove_file(dev, dev->devt_attr);
469                 kfree(dev->devt_attr);
470         }
471  attrError:
472         kobject_uevent(&dev->kobj, KOBJ_REMOVE);
473         kobject_del(&dev->kobj);
474  Error:
475         if (parent)
476                 put_device(parent);
477         goto Done;
478 }
479
480
481 /**
482  *      device_register - register a device with the system.
483  *      @dev:   pointer to the device structure
484  *
485  *      This happens in two clean steps - initialize the device
486  *      and add it to the system. The two steps can be called
487  *      separately, but this is the easiest and most common.
488  *      I.e. you should only call the two helpers separately if
489  *      have a clearly defined need to use and refcount the device
490  *      before it is added to the hierarchy.
491  */
492
493 int device_register(struct device *dev)
494 {
495         device_initialize(dev);
496         return device_add(dev);
497 }
498
499
500 /**
501  *      get_device - increment reference count for device.
502  *      @dev:   device.
503  *
504  *      This simply forwards the call to kobject_get(), though
505  *      we do take care to provide for the case that we get a NULL
506  *      pointer passed in.
507  */
508
509 struct device * get_device(struct device * dev)
510 {
511         return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
512 }
513
514
515 /**
516  *      put_device - decrement reference count.
517  *      @dev:   device in question.
518  */
519 void put_device(struct device * dev)
520 {
521         if (dev)
522                 kobject_put(&dev->kobj);
523 }
524
525
526 /**
527  *      device_del - delete device from system.
528  *      @dev:   device.
529  *
530  *      This is the first part of the device unregistration
531  *      sequence. This removes the device from the lists we control
532  *      from here, has it removed from the other driver model
533  *      subsystems it was added to in device_add(), and removes it
534  *      from the kobject hierarchy.
535  *
536  *      NOTE: this should be called manually _iff_ device_add() was
537  *      also called manually.
538  */
539
540 void device_del(struct device * dev)
541 {
542         struct device * parent = dev->parent;
543         char *class_name = NULL;
544
545         if (parent)
546                 klist_del(&dev->knode_parent);
547         if (dev->devt_attr)
548                 device_remove_file(dev, dev->devt_attr);
549         if (dev->class) {
550                 sysfs_remove_link(&dev->kobj, "subsystem");
551                 sysfs_remove_link(&dev->class->subsys.kset.kobj, dev->bus_id);
552                 class_name = make_class_name(dev->class->name, &dev->kobj);
553                 if (parent) {
554                         sysfs_remove_link(&dev->kobj, "device");
555                         sysfs_remove_link(&dev->parent->kobj, class_name);
556                 }
557                 kfree(class_name);
558                 down(&dev->class->sem);
559                 list_del_init(&dev->node);
560                 up(&dev->class->sem);
561         }
562         device_remove_file(dev, &dev->uevent_attr);
563         device_remove_groups(dev);
564         device_remove_attrs(dev);
565
566         /* Notify the platform of the removal, in case they
567          * need to do anything...
568          */
569         if (platform_notify_remove)
570                 platform_notify_remove(dev);
571         bus_remove_device(dev);
572         device_pm_remove(dev);
573         kobject_uevent(&dev->kobj, KOBJ_REMOVE);
574         kobject_del(&dev->kobj);
575         if (parent)
576                 put_device(parent);
577 }
578
579 /**
580  *      device_unregister - unregister device from system.
581  *      @dev:   device going away.
582  *
583  *      We do this in two parts, like we do device_register(). First,
584  *      we remove it from all the subsystems with device_del(), then
585  *      we decrement the reference count via put_device(). If that
586  *      is the final reference count, the device will be cleaned up
587  *      via device_release() above. Otherwise, the structure will
588  *      stick around until the final reference to the device is dropped.
589  */
590 void device_unregister(struct device * dev)
591 {
592         pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id);
593         device_del(dev);
594         put_device(dev);
595 }
596
597
598 static struct device * next_device(struct klist_iter * i)
599 {
600         struct klist_node * n = klist_next(i);
601         return n ? container_of(n, struct device, knode_parent) : NULL;
602 }
603
604 /**
605  *      device_for_each_child - device child iterator.
606  *      @parent: parent struct device.
607  *      @data:  data for the callback.
608  *      @fn:    function to be called for each device.
609  *
610  *      Iterate over @parent's child devices, and call @fn for each,
611  *      passing it @data.
612  *
613  *      We check the return of @fn each time. If it returns anything
614  *      other than 0, we break out and return that value.
615  */
616 int device_for_each_child(struct device * parent, void * data,
617                      int (*fn)(struct device *, void *))
618 {
619         struct klist_iter i;
620         struct device * child;
621         int error = 0;
622
623         klist_iter_init(&parent->klist_children, &i);
624         while ((child = next_device(&i)) && !error)
625                 error = fn(child, data);
626         klist_iter_exit(&i);
627         return error;
628 }
629
630 int __init devices_init(void)
631 {
632         return subsystem_register(&devices_subsys);
633 }
634
635 EXPORT_SYMBOL_GPL(device_for_each_child);
636
637 EXPORT_SYMBOL_GPL(device_initialize);
638 EXPORT_SYMBOL_GPL(device_add);
639 EXPORT_SYMBOL_GPL(device_register);
640
641 EXPORT_SYMBOL_GPL(device_del);
642 EXPORT_SYMBOL_GPL(device_unregister);
643 EXPORT_SYMBOL_GPL(get_device);
644 EXPORT_SYMBOL_GPL(put_device);
645
646 EXPORT_SYMBOL_GPL(device_create_file);
647 EXPORT_SYMBOL_GPL(device_remove_file);
648
649
650 static void device_create_release(struct device *dev)
651 {
652         pr_debug("%s called for %s\n", __FUNCTION__, dev->bus_id);
653         kfree(dev);
654 }
655
656 /**
657  * device_create - creates a device and registers it with sysfs
658  * @class: pointer to the struct class that this device should be registered to
659  * @parent: pointer to the parent struct device of this new device, if any
660  * @devt: the dev_t for the char device to be added
661  * @fmt: string for the device's name
662  *
663  * This function can be used by char device classes.  A struct device
664  * will be created in sysfs, registered to the specified class.
665  *
666  * A "dev" file will be created, showing the dev_t for the device, if
667  * the dev_t is not 0,0.
668  * If a pointer to a parent struct device is passed in, the newly created
669  * struct device will be a child of that device in sysfs.
670  * The pointer to the struct device will be returned from the call.
671  * Any further sysfs files that might be required can be created using this
672  * pointer.
673  *
674  * Note: the struct class passed to this function must have previously
675  * been created with a call to class_create().
676  */
677 struct device *device_create(struct class *class, struct device *parent,
678                              dev_t devt, const char *fmt, ...)
679 {
680         va_list args;
681         struct device *dev = NULL;
682         int retval = -ENODEV;
683
684         if (class == NULL || IS_ERR(class))
685                 goto error;
686
687         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
688         if (!dev) {
689                 retval = -ENOMEM;
690                 goto error;
691         }
692
693         dev->devt = devt;
694         dev->class = class;
695         dev->parent = parent;
696         dev->release = device_create_release;
697
698         va_start(args, fmt);
699         vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args);
700         va_end(args);
701         retval = device_register(dev);
702         if (retval)
703                 goto error;
704
705         return dev;
706
707 error:
708         kfree(dev);
709         return ERR_PTR(retval);
710 }
711 EXPORT_SYMBOL_GPL(device_create);
712
713 /**
714  * device_destroy - removes a device that was created with device_create()
715  * @class: pointer to the struct class that this device was registered with
716  * @devt: the dev_t of the device that was previously registered
717  *
718  * This call unregisters and cleans up a device that was created with a
719  * call to device_create().
720  */
721 void device_destroy(struct class *class, dev_t devt)
722 {
723         struct device *dev = NULL;
724         struct device *dev_tmp;
725
726         down(&class->sem);
727         list_for_each_entry(dev_tmp, &class->devices, node) {
728                 if (dev_tmp->devt == devt) {
729                         dev = dev_tmp;
730                         break;
731                 }
732         }
733         up(&class->sem);
734
735         if (dev)
736                 device_unregister(dev);
737 }
738 EXPORT_SYMBOL_GPL(device_destroy);