ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.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
24
25 extern struct subsystem devices_subsys;
26
27 #define to_sysdev(k) container_of(k,struct sys_device,kobj)
28 #define to_sysdev_attr(a) container_of(a,struct sysdev_attribute,attr)
29
30
31 static ssize_t 
32 sysdev_show(struct kobject * kobj, struct attribute * attr, char * buffer)
33 {
34         struct sys_device * sysdev = to_sysdev(kobj);
35         struct sysdev_attribute * sysdev_attr = to_sysdev_attr(attr);
36
37         if (sysdev_attr->show)
38                 return sysdev_attr->show(sysdev,buffer);
39         return 0;
40 }
41
42
43 static ssize_t
44 sysdev_store(struct kobject * kobj, struct attribute * attr, 
45              const char * buffer, size_t count)
46 {
47         struct sys_device * sysdev = to_sysdev(kobj);
48         struct sysdev_attribute * sysdev_attr = to_sysdev_attr(attr);
49
50         if (sysdev_attr->store)
51                 return sysdev_attr->store(sysdev,buffer,count);
52         return 0;
53 }
54
55 static struct sysfs_ops sysfs_ops = {
56         .show   = sysdev_show,
57         .store  = sysdev_store,
58 };
59
60 static struct kobj_type ktype_sysdev = {
61         .sysfs_ops      = &sysfs_ops,
62 };
63
64
65 int sysdev_create_file(struct sys_device * s, struct sysdev_attribute * a)
66 {
67         return sysfs_create_file(&s->kobj,&a->attr);
68 }
69
70
71 void sysdev_remove_file(struct sys_device * s, struct sysdev_attribute * a)
72 {
73         sysfs_remove_file(&s->kobj,&a->attr);
74 }
75
76 EXPORT_SYMBOL(sysdev_create_file);
77 EXPORT_SYMBOL(sysdev_remove_file);
78
79 /* 
80  * declare system_subsys 
81  */
82 decl_subsys(system,&ktype_sysdev,NULL);
83
84 int sysdev_class_register(struct sysdev_class * cls)
85 {
86         pr_debug("Registering sysdev class '%s'\n",
87                  kobject_name(&cls->kset.kobj));
88         INIT_LIST_HEAD(&cls->drivers);
89         cls->kset.subsys = &system_subsys;
90         kset_set_kset_s(cls,system_subsys);
91         return kset_register(&cls->kset);
92 }
93
94 void sysdev_class_unregister(struct sysdev_class * cls)
95 {
96         pr_debug("Unregistering sysdev class '%s'\n",
97                  kobject_name(&cls->kset.kobj));
98         kset_unregister(&cls->kset);
99 }
100
101 EXPORT_SYMBOL(sysdev_class_register);
102 EXPORT_SYMBOL(sysdev_class_unregister);
103
104
105 static LIST_HEAD(global_drivers);
106
107 /**
108  *      sysdev_driver_register - Register auxillary driver
109  *      @cls:   Device class driver belongs to.
110  *      @drv:   Driver.
111  *
112  *      If @cls is valid, then @drv is inserted into @cls->drivers to be 
113  *      called on each operation on devices of that class. The refcount
114  *      of @cls is incremented. 
115  *      Otherwise, @drv is inserted into global_drivers, and called for 
116  *      each device.
117  */
118
119 int sysdev_driver_register(struct sysdev_class * cls, 
120                            struct sysdev_driver * drv)
121 {
122         down_write(&system_subsys.rwsem);
123         if (cls && kset_get(&cls->kset)) {
124                 list_add_tail(&drv->entry,&cls->drivers);
125
126                 /* If devices of this class already exist, tell the driver */
127                 if (drv->add) {
128                         struct sys_device *dev;
129                         list_for_each_entry(dev, &cls->kset.list, kobj.entry)
130                                 drv->add(dev);
131                 }
132         } else
133                 list_add_tail(&drv->entry,&global_drivers);
134         up_write(&system_subsys.rwsem);
135         return 0;
136 }
137
138
139 /**
140  *      sysdev_driver_unregister - Remove an auxillary driver.
141  *      @cls:   Class driver belongs to.
142  *      @drv:   Driver.
143  */
144 void sysdev_driver_unregister(struct sysdev_class * cls,
145                               struct sysdev_driver * drv)
146 {
147         down_write(&system_subsys.rwsem);
148         list_del_init(&drv->entry);
149         if (cls) {
150                 if (drv->remove) {
151                         struct sys_device *dev;
152                         list_for_each_entry(dev, &cls->kset.list, kobj.entry)
153                                 drv->remove(dev);
154                 }
155                 kset_put(&cls->kset);
156         }
157         up_write(&system_subsys.rwsem);
158 }
159
160 EXPORT_SYMBOL(sysdev_driver_register);
161 EXPORT_SYMBOL(sysdev_driver_unregister);
162
163
164
165 /**
166  *      sysdev_register - add a system device to the tree
167  *      @sysdev:        device in question
168  *
169  */
170 int sysdev_register(struct sys_device * sysdev)
171 {
172         int error;
173         struct sysdev_class * cls = sysdev->cls;
174
175         if (!cls)
176                 return -EINVAL;
177
178         /* Make sure the kset is set */
179         sysdev->kobj.kset = &cls->kset;
180
181         /* But make sure we point to the right type for sysfs translation */
182         sysdev->kobj.ktype = &ktype_sysdev;
183         kobject_set_name(&sysdev->kobj,"%s%d",
184                          kobject_name(&cls->kset.kobj),sysdev->id);
185         pr_debug("Registering sys device '%s'\n",kobject_name(&sysdev->kobj));
186
187         /* Register the object */
188         error = kobject_register(&sysdev->kobj);
189
190         if (!error) {
191                 struct sysdev_driver * drv;
192
193                 down_write(&system_subsys.rwsem);
194                 /* Generic notification is implicit, because it's that 
195                  * code that should have called us. 
196                  */
197
198                 /* Notify global drivers */
199                 list_for_each_entry(drv,&global_drivers,entry) {
200                         if (drv->add)
201                                 drv->add(sysdev);
202                 }
203
204                 /* Notify class auxillary drivers */
205                 list_for_each_entry(drv,&cls->drivers,entry) {
206                         if (drv->add)
207                                 drv->add(sysdev);
208                 }
209                 up_write(&system_subsys.rwsem);
210         }
211         return error;
212 }
213
214 void sysdev_unregister(struct sys_device * sysdev)
215 {
216         struct sysdev_driver * drv;
217
218         down_write(&system_subsys.rwsem);
219         list_for_each_entry(drv,&global_drivers,entry) {
220                 if (drv->remove)
221                         drv->remove(sysdev);
222         }
223
224         list_for_each_entry(drv,&sysdev->cls->drivers,entry) {
225                 if (drv->remove)
226                         drv->remove(sysdev);
227         }
228         up_write(&system_subsys.rwsem);
229
230         kobject_unregister(&sysdev->kobj);
231 }
232
233
234
235 /**
236  *      sysdev_shutdown - Shut down all system devices.
237  *
238  *      Loop over each class of system devices, and the devices in each
239  *      of those classes. For each device, we call the shutdown method for
240  *      each driver registered for the device - the globals, the auxillaries,
241  *      and the class driver. 
242  *
243  *      Note: The list is iterated in reverse order, so that we shut down
244  *      child devices before we shut down thier parents. The list ordering
245  *      is guaranteed by virtue of the fact that child devices are registered
246  *      after their parents. 
247  */
248
249 void sysdev_shutdown(void)
250 {
251         struct sysdev_class * cls;
252
253         pr_debug("Shutting Down System Devices\n");
254
255         down_write(&system_subsys.rwsem);
256         list_for_each_entry_reverse(cls,&system_subsys.kset.list,
257                                     kset.kobj.entry) {
258                 struct sys_device * sysdev;
259
260                 pr_debug("Shutting down type '%s':\n",
261                          kobject_name(&cls->kset.kobj));
262
263                 list_for_each_entry(sysdev,&cls->kset.list,kobj.entry) {
264                         struct sysdev_driver * drv;
265                         pr_debug(" %s\n",kobject_name(&sysdev->kobj));
266
267                         /* Call global drivers first. */
268                         list_for_each_entry(drv,&global_drivers,entry) {
269                                 if (drv->shutdown)
270                                         drv->shutdown(sysdev);
271                         }
272
273                         /* Call auxillary drivers next. */
274                         list_for_each_entry(drv,&cls->drivers,entry) {
275                                 if (drv->shutdown)
276                                         drv->shutdown(sysdev);
277                         }
278
279                         /* Now call the generic one */
280                         if (cls->shutdown)
281                                 cls->shutdown(sysdev);
282                 }
283         }
284         up_write(&system_subsys.rwsem);
285 }
286
287
288 /**
289  *      sysdev_suspend - Suspend all system devices.
290  *      @state:         Power state to enter.
291  *
292  *      We perform an almost identical operation as sys_device_shutdown()
293  *      above, though calling ->suspend() instead. Interrupts are disabled
294  *      when this called. Devices are responsible for both saving state and
295  *      quiescing or powering down the device. 
296  *
297  *      This is only called by the device PM core, so we let them handle
298  *      all synchronization.
299  */
300
301 int sysdev_suspend(u32 state)
302 {
303         struct sysdev_class * cls;
304
305         pr_debug("Suspending System Devices\n");
306
307         list_for_each_entry_reverse(cls,&system_subsys.kset.list,
308                                     kset.kobj.entry) {
309                 struct sys_device * sysdev;
310
311                 pr_debug("Suspending type '%s':\n",
312                          kobject_name(&cls->kset.kobj));
313
314                 list_for_each_entry(sysdev,&cls->kset.list,kobj.entry) {
315                         struct sysdev_driver * drv;
316                         pr_debug(" %s\n",kobject_name(&sysdev->kobj));
317
318                         /* Call global drivers first. */
319                         list_for_each_entry(drv,&global_drivers,entry) {
320                                 if (drv->suspend)
321                                         drv->suspend(sysdev,state);
322                         }
323
324                         /* Call auxillary drivers next. */
325                         list_for_each_entry(drv,&cls->drivers,entry) {
326                                 if (drv->suspend)
327                                         drv->suspend(sysdev,state);
328                         }
329
330                         /* Now call the generic one */
331                         if (cls->suspend)
332                                 cls->suspend(sysdev,state);
333                 }
334         }
335         return 0;
336 }
337
338
339 /**
340  *      sysdev_resume - Bring system devices back to life.
341  *
342  *      Similar to sys_device_suspend(), but we iterate the list forwards
343  *      to guarantee that parent devices are resumed before their children.
344  *
345  *      Note: Interrupts are disabled when called. 
346  */
347
348 int sysdev_resume(void)
349 {
350         struct sysdev_class * cls;
351
352         pr_debug("Resuming System Devices\n");
353
354         list_for_each_entry(cls,&system_subsys.kset.list,kset.kobj.entry) {
355                 struct sys_device * sysdev;
356
357                 pr_debug("Resuming type '%s':\n",
358                          kobject_name(&cls->kset.kobj));
359
360                 list_for_each_entry(sysdev,&cls->kset.list,kobj.entry) {
361                         struct sysdev_driver * drv;
362                         pr_debug(" %s\n",kobject_name(&sysdev->kobj));
363
364                         /* First, call the class-specific one */
365                         if (cls->resume)
366                                 cls->resume(sysdev);
367
368                         /* Call auxillary drivers next. */
369                         list_for_each_entry(drv,&cls->drivers,entry) {
370                                 if (drv->resume)
371                                         drv->resume(sysdev);
372                         }
373
374                         /* Call global drivers. */
375                         list_for_each_entry(drv,&global_drivers,entry) {
376                                 if (drv->resume)
377                                         drv->resume(sysdev);
378                         }
379
380                 }
381         }
382         return 0;
383 }
384
385
386 int __init system_bus_init(void)
387 {
388         system_subsys.kset.kobj.parent = &devices_subsys.kset.kobj;
389         return subsystem_register(&system_subsys);
390 }
391
392 EXPORT_SYMBOL(sysdev_register);
393 EXPORT_SYMBOL(sysdev_unregister);