sysdev: fix problem with sysdev_class being re-registered
[safe/jmp/linux-2.6] / drivers / base / sys.c
1 /*
2  * sys.c - pseudo-bus for system 'devices' (cpus, PICs, timers, etc)
3  *
4  * Copyright (c) 2002-3 Patrick Mochel
5  *               2002-3 Open Source Development Lab
6  *
7  * This file is released under the GPLv2
8  *
9  * This exports a 'system' bus type.
10  * By default, a 'sys' bus gets added to the root of the system. There will
11  * always be core system devices. Devices can use sysdev_register() to
12  * add themselves as children of the system bus.
13  */
14
15 #include <linux/sysdev.h>
16 #include <linux/err.h>
17 #include <linux/module.h>
18 #include <linux/kernel.h>
19 #include <linux/init.h>
20 #include <linux/slab.h>
21 #include <linux/string.h>
22 #include <linux/pm.h>
23 #include <linux/device.h>
24 #include <linux/mutex.h>
25
26 #include "base.h"
27
28 #define to_sysdev(k) container_of(k, struct sys_device, kobj)
29 #define to_sysdev_attr(a) container_of(a, struct sysdev_attribute, attr)
30
31
32 static ssize_t
33 sysdev_show(struct kobject * kobj, struct attribute * attr, char * buffer)
34 {
35         struct sys_device * sysdev = to_sysdev(kobj);
36         struct sysdev_attribute * sysdev_attr = to_sysdev_attr(attr);
37
38         if (sysdev_attr->show)
39                 return sysdev_attr->show(sysdev, buffer);
40         return -EIO;
41 }
42
43
44 static ssize_t
45 sysdev_store(struct kobject * kobj, struct attribute * attr,
46              const char * buffer, size_t count)
47 {
48         struct sys_device * sysdev = to_sysdev(kobj);
49         struct sysdev_attribute * sysdev_attr = to_sysdev_attr(attr);
50
51         if (sysdev_attr->store)
52                 return sysdev_attr->store(sysdev, buffer, count);
53         return -EIO;
54 }
55
56 static struct sysfs_ops sysfs_ops = {
57         .show   = sysdev_show,
58         .store  = sysdev_store,
59 };
60
61 static struct kobj_type ktype_sysdev = {
62         .sysfs_ops      = &sysfs_ops,
63 };
64
65
66 int sysdev_create_file(struct sys_device * s, struct sysdev_attribute * a)
67 {
68         return sysfs_create_file(&s->kobj, &a->attr);
69 }
70
71
72 void sysdev_remove_file(struct sys_device * s, struct sysdev_attribute * a)
73 {
74         sysfs_remove_file(&s->kobj, &a->attr);
75 }
76
77 EXPORT_SYMBOL_GPL(sysdev_create_file);
78 EXPORT_SYMBOL_GPL(sysdev_remove_file);
79
80 #define to_sysdev_class(k) container_of(k, struct sysdev_class, kset.kobj)
81 #define to_sysdev_class_attr(a) container_of(a, \
82         struct sysdev_class_attribute, attr)
83
84 static ssize_t sysdev_class_show(struct kobject *kobj, struct attribute *attr,
85                                  char *buffer)
86 {
87         struct sysdev_class * class = to_sysdev_class(kobj);
88         struct sysdev_class_attribute *class_attr = to_sysdev_class_attr(attr);
89
90         if (class_attr->show)
91                 return class_attr->show(class, buffer);
92         return -EIO;
93 }
94
95 static ssize_t sysdev_class_store(struct kobject *kobj, struct attribute *attr,
96                                   const char *buffer, size_t count)
97 {
98         struct sysdev_class * class = to_sysdev_class(kobj);
99         struct sysdev_class_attribute * class_attr = to_sysdev_class_attr(attr);
100
101         if (class_attr->store)
102                 return class_attr->store(class, buffer, count);
103         return -EIO;
104 }
105
106 static struct sysfs_ops sysfs_class_ops = {
107         .show   = sysdev_class_show,
108         .store  = sysdev_class_store,
109 };
110
111 static struct kobj_type ktype_sysdev_class = {
112         .sysfs_ops      = &sysfs_class_ops,
113 };
114
115 int sysdev_class_create_file(struct sysdev_class *c,
116                              struct sysdev_class_attribute *a)
117 {
118         return sysfs_create_file(&c->kset.kobj, &a->attr);
119 }
120 EXPORT_SYMBOL_GPL(sysdev_class_create_file);
121
122 void sysdev_class_remove_file(struct sysdev_class *c,
123                               struct sysdev_class_attribute *a)
124 {
125         sysfs_remove_file(&c->kset.kobj, &a->attr);
126 }
127 EXPORT_SYMBOL_GPL(sysdev_class_remove_file);
128
129 static struct kset *system_kset;
130
131 int sysdev_class_register(struct sysdev_class * cls)
132 {
133         pr_debug("Registering sysdev class '%s'\n",
134                  kobject_name(&cls->kset.kobj));
135         INIT_LIST_HEAD(&cls->drivers);
136         memset(&cls->kset.kobj, 0x00, sizeof(struct kobject));
137         cls->kset.kobj.parent = &system_kset->kobj;
138         cls->kset.kobj.ktype = &ktype_sysdev_class;
139         cls->kset.kobj.kset = system_kset;
140         kobject_set_name(&cls->kset.kobj, cls->name);
141         return kset_register(&cls->kset);
142 }
143
144 void sysdev_class_unregister(struct sysdev_class * cls)
145 {
146         pr_debug("Unregistering sysdev class '%s'\n",
147                  kobject_name(&cls->kset.kobj));
148         kset_unregister(&cls->kset);
149 }
150
151 EXPORT_SYMBOL_GPL(sysdev_class_register);
152 EXPORT_SYMBOL_GPL(sysdev_class_unregister);
153
154 static DEFINE_MUTEX(sysdev_drivers_lock);
155
156 /**
157  *      sysdev_driver_register - Register auxillary driver
158  *      @cls:   Device class driver belongs to.
159  *      @drv:   Driver.
160  *
161  *      @drv is inserted into @cls->drivers to be
162  *      called on each operation on devices of that class. The refcount
163  *      of @cls is incremented.
164  */
165
166 int sysdev_driver_register(struct sysdev_class *cls, struct sysdev_driver *drv)
167 {
168         int err = 0;
169
170         mutex_lock(&sysdev_drivers_lock);
171         if (cls && kset_get(&cls->kset)) {
172                 list_add_tail(&drv->entry, &cls->drivers);
173
174                 /* If devices of this class already exist, tell the driver */
175                 if (drv->add) {
176                         struct sys_device *dev;
177                         list_for_each_entry(dev, &cls->kset.list, kobj.entry)
178                                 drv->add(dev);
179                 }
180         } else {
181                 err = -EINVAL;
182                 printk(KERN_ERR "%s: invalid device class\n", __FUNCTION__);
183                 WARN_ON(1);
184         }
185         mutex_unlock(&sysdev_drivers_lock);
186         return err;
187 }
188
189
190 /**
191  *      sysdev_driver_unregister - Remove an auxillary driver.
192  *      @cls:   Class driver belongs to.
193  *      @drv:   Driver.
194  */
195 void sysdev_driver_unregister(struct sysdev_class * cls,
196                               struct sysdev_driver * drv)
197 {
198         mutex_lock(&sysdev_drivers_lock);
199         list_del_init(&drv->entry);
200         if (cls) {
201                 if (drv->remove) {
202                         struct sys_device *dev;
203                         list_for_each_entry(dev, &cls->kset.list, kobj.entry)
204                                 drv->remove(dev);
205                 }
206                 kset_put(&cls->kset);
207         }
208         mutex_unlock(&sysdev_drivers_lock);
209 }
210
211 EXPORT_SYMBOL_GPL(sysdev_driver_register);
212 EXPORT_SYMBOL_GPL(sysdev_driver_unregister);
213
214
215
216 /**
217  *      sysdev_register - add a system device to the tree
218  *      @sysdev:        device in question
219  *
220  */
221 int sysdev_register(struct sys_device * sysdev)
222 {
223         int error;
224         struct sysdev_class * cls = sysdev->cls;
225
226         if (!cls)
227                 return -EINVAL;
228
229         pr_debug("Registering sys device '%s'\n", kobject_name(&sysdev->kobj));
230
231         /* initialize the kobject to 0, in case it had previously been used */
232         memset(&sysdev->kobj, 0x00, sizeof(struct kobject));
233
234         /* Make sure the kset is set */
235         sysdev->kobj.kset = &cls->kset;
236
237         /* Register the object */
238         error = kobject_init_and_add(&sysdev->kobj, &ktype_sysdev, NULL,
239                                      "%s%d", kobject_name(&cls->kset.kobj),
240                                      sysdev->id);
241
242         if (!error) {
243                 struct sysdev_driver * drv;
244
245                 mutex_lock(&sysdev_drivers_lock);
246                 /* Generic notification is implicit, because it's that
247                  * code that should have called us.
248                  */
249
250                 /* Notify class auxillary drivers */
251                 list_for_each_entry(drv, &cls->drivers, entry) {
252                         if (drv->add)
253                                 drv->add(sysdev);
254                 }
255                 mutex_unlock(&sysdev_drivers_lock);
256         }
257         kobject_uevent(&sysdev->kobj, KOBJ_ADD);
258         return error;
259 }
260
261 void sysdev_unregister(struct sys_device * sysdev)
262 {
263         struct sysdev_driver * drv;
264
265         mutex_lock(&sysdev_drivers_lock);
266         list_for_each_entry(drv, &sysdev->cls->drivers, entry) {
267                 if (drv->remove)
268                         drv->remove(sysdev);
269         }
270         mutex_unlock(&sysdev_drivers_lock);
271
272         kobject_put(&sysdev->kobj);
273 }
274
275
276
277 /**
278  *      sysdev_shutdown - Shut down all system devices.
279  *
280  *      Loop over each class of system devices, and the devices in each
281  *      of those classes. For each device, we call the shutdown method for
282  *      each driver registered for the device - the auxillaries,
283  *      and the class driver.
284  *
285  *      Note: The list is iterated in reverse order, so that we shut down
286  *      child devices before we shut down thier parents. The list ordering
287  *      is guaranteed by virtue of the fact that child devices are registered
288  *      after their parents.
289  */
290
291 void sysdev_shutdown(void)
292 {
293         struct sysdev_class * cls;
294
295         pr_debug("Shutting Down System Devices\n");
296
297         mutex_lock(&sysdev_drivers_lock);
298         list_for_each_entry_reverse(cls, &system_kset->list, kset.kobj.entry) {
299                 struct sys_device * sysdev;
300
301                 pr_debug("Shutting down type '%s':\n",
302                          kobject_name(&cls->kset.kobj));
303
304                 list_for_each_entry(sysdev, &cls->kset.list, kobj.entry) {
305                         struct sysdev_driver * drv;
306                         pr_debug(" %s\n", kobject_name(&sysdev->kobj));
307
308                         /* Call auxillary drivers first */
309                         list_for_each_entry(drv, &cls->drivers, entry) {
310                                 if (drv->shutdown)
311                                         drv->shutdown(sysdev);
312                         }
313
314                         /* Now call the generic one */
315                         if (cls->shutdown)
316                                 cls->shutdown(sysdev);
317                 }
318         }
319         mutex_unlock(&sysdev_drivers_lock);
320 }
321
322 static void __sysdev_resume(struct sys_device *dev)
323 {
324         struct sysdev_class *cls = dev->cls;
325         struct sysdev_driver *drv;
326
327         /* First, call the class-specific one */
328         if (cls->resume)
329                 cls->resume(dev);
330
331         /* Call auxillary drivers next. */
332         list_for_each_entry(drv, &cls->drivers, entry) {
333                 if (drv->resume)
334                         drv->resume(dev);
335         }
336 }
337
338 /**
339  *      sysdev_suspend - Suspend all system devices.
340  *      @state:         Power state to enter.
341  *
342  *      We perform an almost identical operation as sys_device_shutdown()
343  *      above, though calling ->suspend() instead. Interrupts are disabled
344  *      when this called. Devices are responsible for both saving state and
345  *      quiescing or powering down the device.
346  *
347  *      This is only called by the device PM core, so we let them handle
348  *      all synchronization.
349  */
350
351 int sysdev_suspend(pm_message_t state)
352 {
353         struct sysdev_class * cls;
354         struct sys_device *sysdev, *err_dev;
355         struct sysdev_driver *drv, *err_drv;
356         int ret;
357
358         pr_debug("Suspending System Devices\n");
359
360         list_for_each_entry_reverse(cls, &system_kset->list, kset.kobj.entry) {
361                 pr_debug("Suspending type '%s':\n",
362                          kobject_name(&cls->kset.kobj));
363
364                 list_for_each_entry(sysdev, &cls->kset.list, kobj.entry) {
365                         pr_debug(" %s\n", kobject_name(&sysdev->kobj));
366
367                         /* Call auxillary drivers first */
368                         list_for_each_entry(drv, &cls->drivers, entry) {
369                                 if (drv->suspend) {
370                                         ret = drv->suspend(sysdev, state);
371                                         if (ret)
372                                                 goto aux_driver;
373                                 }
374                         }
375
376                         /* Now call the generic one */
377                         if (cls->suspend) {
378                                 ret = cls->suspend(sysdev, state);
379                                 if (ret)
380                                         goto cls_driver;
381                         }
382                 }
383         }
384         return 0;
385         /* resume current sysdev */
386 cls_driver:
387         drv = NULL;
388         printk(KERN_ERR "Class suspend failed for %s\n",
389                 kobject_name(&sysdev->kobj));
390
391 aux_driver:
392         if (drv)
393                 printk(KERN_ERR "Class driver suspend failed for %s\n",
394                                 kobject_name(&sysdev->kobj));
395         list_for_each_entry(err_drv, &cls->drivers, entry) {
396                 if (err_drv == drv)
397                         break;
398                 if (err_drv->resume)
399                         err_drv->resume(sysdev);
400         }
401
402         /* resume other sysdevs in current class */
403         list_for_each_entry(err_dev, &cls->kset.list, kobj.entry) {
404                 if (err_dev == sysdev)
405                         break;
406                 pr_debug(" %s\n", kobject_name(&err_dev->kobj));
407                 __sysdev_resume(err_dev);
408         }
409
410         /* resume other classes */
411         list_for_each_entry_continue(cls, &system_kset->list, kset.kobj.entry) {
412                 list_for_each_entry(err_dev, &cls->kset.list, kobj.entry) {
413                         pr_debug(" %s\n", kobject_name(&err_dev->kobj));
414                         __sysdev_resume(err_dev);
415                 }
416         }
417         return ret;
418 }
419
420
421 /**
422  *      sysdev_resume - Bring system devices back to life.
423  *
424  *      Similar to sys_device_suspend(), but we iterate the list forwards
425  *      to guarantee that parent devices are resumed before their children.
426  *
427  *      Note: Interrupts are disabled when called.
428  */
429
430 int sysdev_resume(void)
431 {
432         struct sysdev_class * cls;
433
434         pr_debug("Resuming System Devices\n");
435
436         list_for_each_entry(cls, &system_kset->list, kset.kobj.entry) {
437                 struct sys_device * sysdev;
438
439                 pr_debug("Resuming type '%s':\n",
440                          kobject_name(&cls->kset.kobj));
441
442                 list_for_each_entry(sysdev, &cls->kset.list, kobj.entry) {
443                         pr_debug(" %s\n", kobject_name(&sysdev->kobj));
444
445                         __sysdev_resume(sysdev);
446                 }
447         }
448         return 0;
449 }
450
451
452 int __init system_bus_init(void)
453 {
454         system_kset = kset_create_and_add("system", NULL, &devices_kset->kobj);
455         if (!system_kset)
456                 return -ENOMEM;
457         return 0;
458 }
459
460 EXPORT_SYMBOL_GPL(sysdev_register);
461 EXPORT_SYMBOL_GPL(sysdev_unregister);