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