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