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