upgrade to linux 2.6.10-1.12_FC2
[linux-2.6.git] / drivers / base / class.c
1 /*
2  * class.c - basic device class management
3  *
4  * Copyright (c) 2002-3 Patrick Mochel
5  * Copyright (c) 2002-3 Open Source Development Labs
6  * Copyright (c) 2003-2004 Greg Kroah-Hartman
7  * Copyright (c) 2003-2004 IBM Corp.
8  *
9  * This file is released under the GPLv2
10  *
11  */
12
13 #include <linux/config.h>
14 #include <linux/device.h>
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/string.h>
18 #include "base.h"
19
20 #define to_class_attr(_attr) container_of(_attr, struct class_attribute, attr)
21 #define to_class(obj) container_of(obj, struct class, subsys.kset.kobj)
22
23 static ssize_t
24 class_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
25 {
26         struct class_attribute * class_attr = to_class_attr(attr);
27         struct class * dc = to_class(kobj);
28         ssize_t ret = 0;
29
30         if (class_attr->show)
31                 ret = class_attr->show(dc, buf);
32         return ret;
33 }
34
35 static ssize_t
36 class_attr_store(struct kobject * kobj, struct attribute * attr,
37                  const char * buf, size_t count)
38 {
39         struct class_attribute * class_attr = to_class_attr(attr);
40         struct class * dc = to_class(kobj);
41         ssize_t ret = 0;
42
43         if (class_attr->store)
44                 ret = class_attr->store(dc, buf, count);
45         return ret;
46 }
47
48 static void class_release(struct kobject * kobj)
49 {
50         struct class *class = to_class(kobj);
51
52         pr_debug("class '%s': release.\n", class->name);
53
54         if (class->class_release)
55                 class->class_release(class);
56         else
57                 pr_debug("class '%s' does not have a release() function, "
58                          "be careful\n", class->name);
59 }
60
61 static struct sysfs_ops class_sysfs_ops = {
62         .show   = class_attr_show,
63         .store  = class_attr_store,
64 };
65
66 static struct kobj_type ktype_class = {
67         .sysfs_ops      = &class_sysfs_ops,
68         .release        = class_release,
69 };
70
71 /* Hotplug events for classes go to the class_obj subsys */
72 static decl_subsys(class, &ktype_class, NULL);
73
74
75 int class_create_file(struct class * cls, const struct class_attribute * attr)
76 {
77         int error;
78         if (cls) {
79                 error = sysfs_create_file(&cls->subsys.kset.kobj, &attr->attr);
80         } else
81                 error = -EINVAL;
82         return error;
83 }
84
85 void class_remove_file(struct class * cls, const struct class_attribute * attr)
86 {
87         if (cls)
88                 sysfs_remove_file(&cls->subsys.kset.kobj, &attr->attr);
89 }
90
91 struct class * class_get(struct class * cls)
92 {
93         if (cls)
94                 return container_of(subsys_get(&cls->subsys), struct class, subsys);
95         return NULL;
96 }
97
98 void class_put(struct class * cls)
99 {
100         subsys_put(&cls->subsys);
101 }
102
103
104 static int add_class_attrs(struct class * cls)
105 {
106         int i;
107         int error = 0;
108
109         if (cls->class_attrs) {
110                 for (i = 0; attr_name(cls->class_attrs[i]); i++) {
111                         error = class_create_file(cls,&cls->class_attrs[i]);
112                         if (error)
113                                 goto Err;
114                 }
115         }
116  Done:
117         return error;
118  Err:
119         while (--i >= 0)
120                 class_remove_file(cls,&cls->class_attrs[i]);
121         goto Done;
122 }
123
124 static void remove_class_attrs(struct class * cls)
125 {
126         int i;
127
128         if (cls->class_attrs) {
129                 for (i = 0; attr_name(cls->class_attrs[i]); i++)
130                         class_remove_file(cls,&cls->class_attrs[i]);
131         }
132 }
133
134 int class_register(struct class * cls)
135 {
136         int error;
137
138         pr_debug("device class '%s': registering\n", cls->name);
139
140         INIT_LIST_HEAD(&cls->children);
141         INIT_LIST_HEAD(&cls->interfaces);
142         error = kobject_set_name(&cls->subsys.kset.kobj, "%s", cls->name);
143         if (error)
144                 return error;
145
146         subsys_set_kset(cls, class_subsys);
147
148         error = subsystem_register(&cls->subsys);
149         if (!error) {
150                 error = add_class_attrs(class_get(cls));
151                 class_put(cls);
152         }
153         return error;
154 }
155
156 void class_unregister(struct class * cls)
157 {
158         pr_debug("device class '%s': unregistering\n", cls->name);
159         remove_class_attrs(cls);
160         subsystem_unregister(&cls->subsys);
161 }
162
163
164 /* Class Device Stuff */
165
166 int class_device_create_file(struct class_device * class_dev,
167                              const struct class_device_attribute * attr)
168 {
169         int error = -EINVAL;
170         if (class_dev)
171                 error = sysfs_create_file(&class_dev->kobj, &attr->attr);
172         return error;
173 }
174
175 void class_device_remove_file(struct class_device * class_dev,
176                               const struct class_device_attribute * attr)
177 {
178         if (class_dev)
179                 sysfs_remove_file(&class_dev->kobj, &attr->attr);
180 }
181
182 static int class_device_dev_link(struct class_device * class_dev)
183 {
184         if (class_dev->dev)
185                 return sysfs_create_link(&class_dev->kobj,
186                                          &class_dev->dev->kobj, "device");
187         return 0;
188 }
189
190 static void class_device_dev_unlink(struct class_device * class_dev)
191 {
192         sysfs_remove_link(&class_dev->kobj, "device");
193 }
194
195 static int class_device_driver_link(struct class_device * class_dev)
196 {
197         if ((class_dev->dev) && (class_dev->dev->driver))
198                 return sysfs_create_link(&class_dev->kobj,
199                                          &class_dev->dev->driver->kobj, "driver");
200         return 0;
201 }
202
203 static void class_device_driver_unlink(struct class_device * class_dev)
204 {
205         sysfs_remove_link(&class_dev->kobj, "driver");
206 }
207
208
209 static ssize_t
210 class_device_attr_show(struct kobject * kobj, struct attribute * attr,
211                        char * buf)
212 {
213         struct class_device_attribute * class_dev_attr = to_class_dev_attr(attr);
214         struct class_device * cd = to_class_dev(kobj);
215         ssize_t ret = 0;
216
217         if (class_dev_attr->show)
218                 ret = class_dev_attr->show(cd, buf);
219         return ret;
220 }
221
222 static ssize_t
223 class_device_attr_store(struct kobject * kobj, struct attribute * attr,
224                         const char * buf, size_t count)
225 {
226         struct class_device_attribute * class_dev_attr = to_class_dev_attr(attr);
227         struct class_device * cd = to_class_dev(kobj);
228         ssize_t ret = 0;
229
230         if (class_dev_attr->store)
231                 ret = class_dev_attr->store(cd, buf, count);
232         return ret;
233 }
234
235 static struct sysfs_ops class_dev_sysfs_ops = {
236         .show   = class_device_attr_show,
237         .store  = class_device_attr_store,
238 };
239
240 static void class_dev_release(struct kobject * kobj)
241 {
242         struct class_device *cd = to_class_dev(kobj);
243         struct class * cls = cd->class;
244
245         pr_debug("device class '%s': release.\n", cd->class_id);
246
247         if (cls->release)
248                 cls->release(cd);
249         else {
250                 printk(KERN_ERR "Device class '%s' does not have a release() function, "
251                         "it is broken and must be fixed.\n",
252                         cd->class_id);
253                 WARN_ON(1);
254         }
255 }
256
257 static struct kobj_type ktype_class_device = {
258         .sysfs_ops      = &class_dev_sysfs_ops,
259         .release        = class_dev_release,
260 };
261
262 static int class_hotplug_filter(struct kset *kset, struct kobject *kobj)
263 {
264         struct kobj_type *ktype = get_ktype(kobj);
265
266         if (ktype == &ktype_class_device) {
267                 struct class_device *class_dev = to_class_dev(kobj);
268                 if (class_dev->class)
269                         return 1;
270         }
271         return 0;
272 }
273
274 static char *class_hotplug_name(struct kset *kset, struct kobject *kobj)
275 {
276         struct class_device *class_dev = to_class_dev(kobj);
277
278         return class_dev->class->name;
279 }
280
281 static int class_hotplug(struct kset *kset, struct kobject *kobj, char **envp,
282                          int num_envp, char *buffer, int buffer_size)
283 {
284         struct class_device *class_dev = to_class_dev(kobj);
285         int retval = 0;
286         int i = 0;
287         int length = 0;
288
289         pr_debug("%s - name = %s\n", __FUNCTION__, class_dev->class_id);
290
291         if (class_dev->dev) {
292                 /* add physical device, backing this device  */
293                 struct device *dev = class_dev->dev;
294                 char *path = kobject_get_path(&dev->kobj, GFP_KERNEL);
295
296                 add_hotplug_env_var(envp, num_envp, &i, buffer, buffer_size,
297                                     &length, "PHYSDEVPATH=%s", path);
298                 kfree(path);
299
300                 /* add bus name of physical device */
301                 if (dev->bus)
302                         add_hotplug_env_var(envp, num_envp, &i,
303                                             buffer, buffer_size, &length,
304                                             "PHYSDEVBUS=%s", dev->bus->name);
305
306                 /* add driver name of physical device */
307                 if (dev->driver)
308                         add_hotplug_env_var(envp, num_envp, &i,
309                                             buffer, buffer_size, &length,
310                                             "PHYSDEVDRIVER=%s", dev->driver->name);
311
312                 /* terminate, set to next free slot, shrink available space */
313                 envp[i] = NULL;
314                 envp = &envp[i];
315                 num_envp -= i;
316                 buffer = &buffer[length];
317                 buffer_size -= length;
318         }
319
320         if (class_dev->class->hotplug) {
321                 /* have the bus specific function add its stuff */
322                 retval = class_dev->class->hotplug (class_dev, envp, num_envp,
323                                                     buffer, buffer_size);
324                         if (retval) {
325                         pr_debug ("%s - hotplug() returned %d\n",
326                                   __FUNCTION__, retval);
327                 }
328         }
329
330         return retval;
331 }
332
333 static struct kset_hotplug_ops class_hotplug_ops = {
334         .filter =       class_hotplug_filter,
335         .name =         class_hotplug_name,
336         .hotplug =      class_hotplug,
337 };
338
339 static decl_subsys(class_obj, &ktype_class_device, &class_hotplug_ops);
340
341
342 static int class_device_add_attrs(struct class_device * cd)
343 {
344         int i;
345         int error = 0;
346         struct class * cls = cd->class;
347
348         if (cls->class_dev_attrs) {
349                 for (i = 0; attr_name(cls->class_dev_attrs[i]); i++) {
350                         error = class_device_create_file(cd,
351                                                          &cls->class_dev_attrs[i]);
352                         if (error)
353                                 goto Err;
354                 }
355         }
356  Done:
357         return error;
358  Err:
359         while (--i >= 0)
360                 class_device_remove_file(cd,&cls->class_dev_attrs[i]);
361         goto Done;
362 }
363
364 static void class_device_remove_attrs(struct class_device * cd)
365 {
366         int i;
367         struct class * cls = cd->class;
368
369         if (cls->class_dev_attrs) {
370                 for (i = 0; attr_name(cls->class_dev_attrs[i]); i++)
371                         class_device_remove_file(cd,&cls->class_dev_attrs[i]);
372         }
373 }
374
375 void class_device_initialize(struct class_device *class_dev)
376 {
377         kobj_set_kset_s(class_dev, class_obj_subsys);
378         kobject_init(&class_dev->kobj);
379         INIT_LIST_HEAD(&class_dev->node);
380 }
381
382 int class_device_add(struct class_device *class_dev)
383 {
384         struct class * parent = NULL;
385         struct class_interface * class_intf;
386         int error;
387
388         class_dev = class_device_get(class_dev);
389         if (!class_dev)
390                 return -EINVAL;
391
392         if (!strlen(class_dev->class_id)) {
393                 error = -EINVAL;
394                 goto register_done;
395         }
396
397         parent = class_get(class_dev->class);
398
399         pr_debug("CLASS: registering class device: ID = '%s'\n",
400                  class_dev->class_id);
401
402         /* first, register with generic layer. */
403         kobject_set_name(&class_dev->kobj, "%s", class_dev->class_id);
404         if (parent)
405                 class_dev->kobj.parent = &parent->subsys.kset.kobj;
406
407         if ((error = kobject_add(&class_dev->kobj)))
408                 goto register_done;
409
410         /* now take care of our own registration */
411         if (parent) {
412                 down_write(&parent->subsys.rwsem);
413                 list_add_tail(&class_dev->node, &parent->children);
414                 list_for_each_entry(class_intf, &parent->interfaces, node)
415                         if (class_intf->add)
416                                 class_intf->add(class_dev);
417                 up_write(&parent->subsys.rwsem);
418         }
419         class_device_add_attrs(class_dev);
420         class_device_dev_link(class_dev);
421         class_device_driver_link(class_dev);
422
423  register_done:
424         if (error && parent)
425                 class_put(parent);
426         class_device_put(class_dev);
427         return error;
428 }
429
430 int class_device_register(struct class_device *class_dev)
431 {
432         class_device_initialize(class_dev);
433         return class_device_add(class_dev);
434 }
435
436 void class_device_del(struct class_device *class_dev)
437 {
438         struct class * parent = class_dev->class;
439         struct class_interface * class_intf;
440
441         if (parent) {
442                 down_write(&parent->subsys.rwsem);
443                 list_del_init(&class_dev->node);
444                 list_for_each_entry(class_intf, &parent->interfaces, node)
445                         if (class_intf->remove)
446                                 class_intf->remove(class_dev);
447                 up_write(&parent->subsys.rwsem);
448         }
449
450         class_device_dev_unlink(class_dev);
451         class_device_driver_unlink(class_dev);
452         class_device_remove_attrs(class_dev);
453
454         kobject_del(&class_dev->kobj);
455
456         if (parent)
457                 class_put(parent);
458 }
459
460 void class_device_unregister(struct class_device *class_dev)
461 {
462         pr_debug("CLASS: Unregistering class device. ID = '%s'\n",
463                  class_dev->class_id);
464         class_device_del(class_dev);
465         class_device_put(class_dev);
466 }
467
468 int class_device_rename(struct class_device *class_dev, char *new_name)
469 {
470         int error = 0;
471
472         class_dev = class_device_get(class_dev);
473         if (!class_dev)
474                 return -EINVAL;
475
476         pr_debug("CLASS: renaming '%s' to '%s'\n", class_dev->class_id,
477                  new_name);
478
479         strlcpy(class_dev->class_id, new_name, KOBJ_NAME_LEN);
480
481         error = kobject_rename(&class_dev->kobj, new_name);
482
483         class_device_put(class_dev);
484
485         return error;
486 }
487
488 struct class_device * class_device_get(struct class_device *class_dev)
489 {
490         if (class_dev)
491                 return to_class_dev(kobject_get(&class_dev->kobj));
492         return NULL;
493 }
494
495 void class_device_put(struct class_device *class_dev)
496 {
497         kobject_put(&class_dev->kobj);
498 }
499
500
501 int class_interface_register(struct class_interface *class_intf)
502 {
503         struct class * parent;
504         struct class_device * class_dev;
505
506         if (!class_intf || !class_intf->class)
507                 return -ENODEV;
508
509         parent = class_get(class_intf->class);
510         if (!parent)
511                 return -EINVAL;
512
513         down_write(&parent->subsys.rwsem);
514         list_add_tail(&class_intf->node, &parent->interfaces);
515
516         if (class_intf->add) {
517                 list_for_each_entry(class_dev, &parent->children, node)
518                         class_intf->add(class_dev);
519         }
520         up_write(&parent->subsys.rwsem);
521
522         return 0;
523 }
524
525 void class_interface_unregister(struct class_interface *class_intf)
526 {
527         struct class * parent = class_intf->class;
528         struct class_device *class_dev;
529
530         if (!parent)
531                 return;
532
533         down_write(&parent->subsys.rwsem);
534         list_del_init(&class_intf->node);
535
536         if (class_intf->remove) {
537                 list_for_each_entry(class_dev, &parent->children, node)
538                         class_intf->remove(class_dev);
539         }
540         up_write(&parent->subsys.rwsem);
541
542         class_put(parent);
543 }
544
545
546
547 int __init classes_init(void)
548 {
549         int retval;
550
551         retval = subsystem_register(&class_subsys);
552         if (retval)
553                 return retval;
554
555         /* ick, this is ugly, the things we go through to keep from showing up
556          * in sysfs... */
557         subsystem_init(&class_obj_subsys);
558         if (!class_obj_subsys.kset.subsys)
559                         class_obj_subsys.kset.subsys = &class_obj_subsys;
560         return 0;
561 }
562
563 EXPORT_SYMBOL_GPL(class_create_file);
564 EXPORT_SYMBOL_GPL(class_remove_file);
565 EXPORT_SYMBOL_GPL(class_register);
566 EXPORT_SYMBOL_GPL(class_unregister);
567 EXPORT_SYMBOL_GPL(class_get);
568 EXPORT_SYMBOL_GPL(class_put);
569
570 EXPORT_SYMBOL_GPL(class_device_register);
571 EXPORT_SYMBOL_GPL(class_device_unregister);
572 EXPORT_SYMBOL_GPL(class_device_initialize);
573 EXPORT_SYMBOL_GPL(class_device_add);
574 EXPORT_SYMBOL_GPL(class_device_del);
575 EXPORT_SYMBOL_GPL(class_device_get);
576 EXPORT_SYMBOL_GPL(class_device_put);
577 EXPORT_SYMBOL_GPL(class_device_create_file);
578 EXPORT_SYMBOL_GPL(class_device_remove_file);
579
580 EXPORT_SYMBOL_GPL(class_interface_register);
581 EXPORT_SYMBOL_GPL(class_interface_unregister);