Merge master.kernel.org:/pub/scm/linux/kernel/git/davej/agpgart
[sfrench/cifs-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 <linux/kdev_t.h>
19 #include <linux/err.h>
20 #include "base.h"
21
22 #define to_class_attr(_attr) container_of(_attr, struct class_attribute, attr)
23 #define to_class(obj) container_of(obj, struct class, subsys.kset.kobj)
24
25 static ssize_t
26 class_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
27 {
28         struct class_attribute * class_attr = to_class_attr(attr);
29         struct class * dc = to_class(kobj);
30         ssize_t ret = -EIO;
31
32         if (class_attr->show)
33                 ret = class_attr->show(dc, buf);
34         return ret;
35 }
36
37 static ssize_t
38 class_attr_store(struct kobject * kobj, struct attribute * attr,
39                  const char * buf, size_t count)
40 {
41         struct class_attribute * class_attr = to_class_attr(attr);
42         struct class * dc = to_class(kobj);
43         ssize_t ret = -EIO;
44
45         if (class_attr->store)
46                 ret = class_attr->store(dc, buf, count);
47         return ret;
48 }
49
50 static void class_release(struct kobject * kobj)
51 {
52         struct class *class = to_class(kobj);
53
54         pr_debug("class '%s': release.\n", class->name);
55
56         if (class->class_release)
57                 class->class_release(class);
58         else
59                 pr_debug("class '%s' does not have a release() function, "
60                          "be careful\n", class->name);
61 }
62
63 static struct sysfs_ops class_sysfs_ops = {
64         .show   = class_attr_show,
65         .store  = class_attr_store,
66 };
67
68 static struct kobj_type ktype_class = {
69         .sysfs_ops      = &class_sysfs_ops,
70         .release        = class_release,
71 };
72
73 /* Hotplug events for classes go to the class_obj subsys */
74 static decl_subsys(class, &ktype_class, NULL);
75
76
77 int class_create_file(struct class * cls, const struct class_attribute * attr)
78 {
79         int error;
80         if (cls) {
81                 error = sysfs_create_file(&cls->subsys.kset.kobj, &attr->attr);
82         } else
83                 error = -EINVAL;
84         return error;
85 }
86
87 void class_remove_file(struct class * cls, const struct class_attribute * attr)
88 {
89         if (cls)
90                 sysfs_remove_file(&cls->subsys.kset.kobj, &attr->attr);
91 }
92
93 struct class * class_get(struct class * cls)
94 {
95         if (cls)
96                 return container_of(subsys_get(&cls->subsys), struct class, subsys);
97         return NULL;
98 }
99
100 void class_put(struct class * cls)
101 {
102         subsys_put(&cls->subsys);
103 }
104
105
106 static int add_class_attrs(struct class * cls)
107 {
108         int i;
109         int error = 0;
110
111         if (cls->class_attrs) {
112                 for (i = 0; attr_name(cls->class_attrs[i]); i++) {
113                         error = class_create_file(cls,&cls->class_attrs[i]);
114                         if (error)
115                                 goto Err;
116                 }
117         }
118  Done:
119         return error;
120  Err:
121         while (--i >= 0)
122                 class_remove_file(cls,&cls->class_attrs[i]);
123         goto Done;
124 }
125
126 static void remove_class_attrs(struct class * cls)
127 {
128         int i;
129
130         if (cls->class_attrs) {
131                 for (i = 0; attr_name(cls->class_attrs[i]); i++)
132                         class_remove_file(cls,&cls->class_attrs[i]);
133         }
134 }
135
136 int class_register(struct class * cls)
137 {
138         int error;
139
140         pr_debug("device class '%s': registering\n", cls->name);
141
142         INIT_LIST_HEAD(&cls->children);
143         INIT_LIST_HEAD(&cls->interfaces);
144         init_MUTEX(&cls->sem);
145         error = kobject_set_name(&cls->subsys.kset.kobj, "%s", cls->name);
146         if (error)
147                 return error;
148
149         subsys_set_kset(cls, class_subsys);
150
151         error = subsystem_register(&cls->subsys);
152         if (!error) {
153                 error = add_class_attrs(class_get(cls));
154                 class_put(cls);
155         }
156         return error;
157 }
158
159 void class_unregister(struct class * cls)
160 {
161         pr_debug("device class '%s': unregistering\n", cls->name);
162         remove_class_attrs(cls);
163         subsystem_unregister(&cls->subsys);
164 }
165
166 static void class_create_release(struct class *cls)
167 {
168         kfree(cls);
169 }
170
171 static void class_device_create_release(struct class_device *class_dev)
172 {
173         kfree(class_dev);
174 }
175
176 /**
177  * class_create - create a struct class structure
178  * @owner: pointer to the module that is to "own" this struct class
179  * @name: pointer to a string for the name of this class.
180  *
181  * This is used to create a struct class pointer that can then be used
182  * in calls to class_device_create().
183  *
184  * Note, the pointer created here is to be destroyed when finished by
185  * making a call to class_destroy().
186  */
187 struct class *class_create(struct module *owner, char *name)
188 {
189         struct class *cls;
190         int retval;
191
192         cls = kmalloc(sizeof(struct class), GFP_KERNEL);
193         if (!cls) {
194                 retval = -ENOMEM;
195                 goto error;
196         }
197         memset(cls, 0x00, sizeof(struct class));
198
199         cls->name = name;
200         cls->owner = owner;
201         cls->class_release = class_create_release;
202         cls->release = class_device_create_release;
203
204         retval = class_register(cls);
205         if (retval)
206                 goto error;
207
208         return cls;
209
210 error:
211         kfree(cls);
212         return ERR_PTR(retval);
213 }
214
215 /**
216  * class_destroy - destroys a struct class structure
217  * @cs: pointer to the struct class that is to be destroyed
218  *
219  * Note, the pointer to be destroyed must have been created with a call
220  * to class_create().
221  */
222 void class_destroy(struct class *cls)
223 {
224         if ((cls == NULL) || (IS_ERR(cls)))
225                 return;
226
227         class_unregister(cls);
228 }
229
230 /* Class Device Stuff */
231
232 int class_device_create_file(struct class_device * class_dev,
233                              const struct class_device_attribute * attr)
234 {
235         int error = -EINVAL;
236         if (class_dev)
237                 error = sysfs_create_file(&class_dev->kobj, &attr->attr);
238         return error;
239 }
240
241 void class_device_remove_file(struct class_device * class_dev,
242                               const struct class_device_attribute * attr)
243 {
244         if (class_dev)
245                 sysfs_remove_file(&class_dev->kobj, &attr->attr);
246 }
247
248 int class_device_create_bin_file(struct class_device *class_dev,
249                                  struct bin_attribute *attr)
250 {
251         int error = -EINVAL;
252         if (class_dev)
253                 error = sysfs_create_bin_file(&class_dev->kobj, attr);
254         return error;
255 }
256
257 void class_device_remove_bin_file(struct class_device *class_dev,
258                                   struct bin_attribute *attr)
259 {
260         if (class_dev)
261                 sysfs_remove_bin_file(&class_dev->kobj, attr);
262 }
263
264 static ssize_t
265 class_device_attr_show(struct kobject * kobj, struct attribute * attr,
266                        char * buf)
267 {
268         struct class_device_attribute * class_dev_attr = to_class_dev_attr(attr);
269         struct class_device * cd = to_class_dev(kobj);
270         ssize_t ret = 0;
271
272         if (class_dev_attr->show)
273                 ret = class_dev_attr->show(cd, buf);
274         return ret;
275 }
276
277 static ssize_t
278 class_device_attr_store(struct kobject * kobj, struct attribute * attr,
279                         const char * buf, size_t count)
280 {
281         struct class_device_attribute * class_dev_attr = to_class_dev_attr(attr);
282         struct class_device * cd = to_class_dev(kobj);
283         ssize_t ret = 0;
284
285         if (class_dev_attr->store)
286                 ret = class_dev_attr->store(cd, buf, count);
287         return ret;
288 }
289
290 static struct sysfs_ops class_dev_sysfs_ops = {
291         .show   = class_device_attr_show,
292         .store  = class_device_attr_store,
293 };
294
295 static void class_dev_release(struct kobject * kobj)
296 {
297         struct class_device *cd = to_class_dev(kobj);
298         struct class * cls = cd->class;
299
300         pr_debug("device class '%s': release.\n", cd->class_id);
301
302         kfree(cd->devt_attr);
303         cd->devt_attr = NULL;
304
305         if (cls->release)
306                 cls->release(cd);
307         else {
308                 printk(KERN_ERR "Device class '%s' does not have a release() function, "
309                         "it is broken and must be fixed.\n",
310                         cd->class_id);
311                 WARN_ON(1);
312         }
313 }
314
315 static struct kobj_type ktype_class_device = {
316         .sysfs_ops      = &class_dev_sysfs_ops,
317         .release        = class_dev_release,
318 };
319
320 static int class_hotplug_filter(struct kset *kset, struct kobject *kobj)
321 {
322         struct kobj_type *ktype = get_ktype(kobj);
323
324         if (ktype == &ktype_class_device) {
325                 struct class_device *class_dev = to_class_dev(kobj);
326                 if (class_dev->class)
327                         return 1;
328         }
329         return 0;
330 }
331
332 static const char *class_hotplug_name(struct kset *kset, struct kobject *kobj)
333 {
334         struct class_device *class_dev = to_class_dev(kobj);
335
336         return class_dev->class->name;
337 }
338
339 static int class_hotplug(struct kset *kset, struct kobject *kobj, char **envp,
340                          int num_envp, char *buffer, int buffer_size)
341 {
342         struct class_device *class_dev = to_class_dev(kobj);
343         int i = 0;
344         int length = 0;
345         int retval = 0;
346
347         pr_debug("%s - name = %s\n", __FUNCTION__, class_dev->class_id);
348
349         if (class_dev->dev) {
350                 /* add physical device, backing this device  */
351                 struct device *dev = class_dev->dev;
352                 char *path = kobject_get_path(&dev->kobj, GFP_KERNEL);
353
354                 add_hotplug_env_var(envp, num_envp, &i, buffer, buffer_size,
355                                     &length, "PHYSDEVPATH=%s", path);
356                 kfree(path);
357
358                 if (dev->bus)
359                         add_hotplug_env_var(envp, num_envp, &i,
360                                             buffer, buffer_size, &length,
361                                             "PHYSDEVBUS=%s", dev->bus->name);
362
363                 if (dev->driver)
364                         add_hotplug_env_var(envp, num_envp, &i,
365                                             buffer, buffer_size, &length,
366                                             "PHYSDEVDRIVER=%s", dev->driver->name);
367         }
368
369         if (MAJOR(class_dev->devt)) {
370                 add_hotplug_env_var(envp, num_envp, &i,
371                                     buffer, buffer_size, &length,
372                                     "MAJOR=%u", MAJOR(class_dev->devt));
373
374                 add_hotplug_env_var(envp, num_envp, &i,
375                                     buffer, buffer_size, &length,
376                                     "MINOR=%u", MINOR(class_dev->devt));
377         }
378
379         /* terminate, set to next free slot, shrink available space */
380         envp[i] = NULL;
381         envp = &envp[i];
382         num_envp -= i;
383         buffer = &buffer[length];
384         buffer_size -= length;
385
386         if (class_dev->class->hotplug) {
387                 /* have the bus specific function add its stuff */
388                 retval = class_dev->class->hotplug (class_dev, envp, num_envp,
389                                                     buffer, buffer_size);
390                         if (retval) {
391                         pr_debug ("%s - hotplug() returned %d\n",
392                                   __FUNCTION__, retval);
393                 }
394         }
395
396         return retval;
397 }
398
399 static struct kset_hotplug_ops class_hotplug_ops = {
400         .filter =       class_hotplug_filter,
401         .name =         class_hotplug_name,
402         .hotplug =      class_hotplug,
403 };
404
405 static decl_subsys(class_obj, &ktype_class_device, &class_hotplug_ops);
406
407
408 static int class_device_add_attrs(struct class_device * cd)
409 {
410         int i;
411         int error = 0;
412         struct class * cls = cd->class;
413
414         if (cls->class_dev_attrs) {
415                 for (i = 0; attr_name(cls->class_dev_attrs[i]); i++) {
416                         error = class_device_create_file(cd,
417                                                          &cls->class_dev_attrs[i]);
418                         if (error)
419                                 goto Err;
420                 }
421         }
422  Done:
423         return error;
424  Err:
425         while (--i >= 0)
426                 class_device_remove_file(cd,&cls->class_dev_attrs[i]);
427         goto Done;
428 }
429
430 static void class_device_remove_attrs(struct class_device * cd)
431 {
432         int i;
433         struct class * cls = cd->class;
434
435         if (cls->class_dev_attrs) {
436                 for (i = 0; attr_name(cls->class_dev_attrs[i]); i++)
437                         class_device_remove_file(cd,&cls->class_dev_attrs[i]);
438         }
439 }
440
441 static ssize_t show_dev(struct class_device *class_dev, char *buf)
442 {
443         return print_dev_t(buf, class_dev->devt);
444 }
445
446 void class_device_initialize(struct class_device *class_dev)
447 {
448         kobj_set_kset_s(class_dev, class_obj_subsys);
449         kobject_init(&class_dev->kobj);
450         INIT_LIST_HEAD(&class_dev->node);
451 }
452
453 static char *make_class_name(struct class_device *class_dev)
454 {
455         char *name;
456         int size;
457
458         size = strlen(class_dev->class->name) +
459                 strlen(kobject_name(&class_dev->kobj)) + 2;
460
461         name = kmalloc(size, GFP_KERNEL);
462         if (!name)
463                 return ERR_PTR(-ENOMEM);
464
465         strcpy(name, class_dev->class->name);
466         strcat(name, ":");
467         strcat(name, kobject_name(&class_dev->kobj));
468         return name;
469 }
470
471 int class_device_add(struct class_device *class_dev)
472 {
473         struct class * parent = NULL;
474         struct class_interface * class_intf;
475         char *class_name = NULL;
476         int error;
477
478         class_dev = class_device_get(class_dev);
479         if (!class_dev)
480                 return -EINVAL;
481
482         if (!strlen(class_dev->class_id)) {
483                 error = -EINVAL;
484                 goto register_done;
485         }
486
487         parent = class_get(class_dev->class);
488
489         pr_debug("CLASS: registering class device: ID = '%s'\n",
490                  class_dev->class_id);
491
492         /* first, register with generic layer. */
493         kobject_set_name(&class_dev->kobj, "%s", class_dev->class_id);
494         if (parent)
495                 class_dev->kobj.parent = &parent->subsys.kset.kobj;
496
497         if ((error = kobject_add(&class_dev->kobj)))
498                 goto register_done;
499
500         /* add the needed attributes to this device */
501         if (MAJOR(class_dev->devt)) {
502                 struct class_device_attribute *attr;
503                 attr = kmalloc(sizeof(*attr), GFP_KERNEL);
504                 if (!attr) {
505                         error = -ENOMEM;
506                         kobject_del(&class_dev->kobj);
507                         goto register_done;
508                 }
509                 memset(attr, sizeof(*attr), 0x00);
510                 attr->attr.name = "dev";
511                 attr->attr.mode = S_IRUGO;
512                 attr->attr.owner = parent->owner;
513                 attr->show = show_dev;
514                 attr->store = NULL;
515                 class_device_create_file(class_dev, attr);
516                 class_dev->devt_attr = attr;
517         }
518
519         class_device_add_attrs(class_dev);
520         if (class_dev->dev) {
521                 class_name = make_class_name(class_dev);
522                 sysfs_create_link(&class_dev->kobj,
523                                   &class_dev->dev->kobj, "device");
524                 sysfs_create_link(&class_dev->dev->kobj, &class_dev->kobj,
525                                   class_name);
526         }
527
528         /* notify any interfaces this device is now here */
529         if (parent) {
530                 down(&parent->sem);
531                 list_add_tail(&class_dev->node, &parent->children);
532                 list_for_each_entry(class_intf, &parent->interfaces, node)
533                         if (class_intf->add)
534                                 class_intf->add(class_dev);
535                 up(&parent->sem);
536         }
537         kobject_hotplug(&class_dev->kobj, KOBJ_ADD);
538
539  register_done:
540         if (error && parent)
541                 class_put(parent);
542         class_device_put(class_dev);
543         kfree(class_name);
544         return error;
545 }
546
547 int class_device_register(struct class_device *class_dev)
548 {
549         class_device_initialize(class_dev);
550         return class_device_add(class_dev);
551 }
552
553 /**
554  * class_device_create - creates a class device and registers it with sysfs
555  * @cs: pointer to the struct class that this device should be registered to.
556  * @dev: the dev_t for the char device to be added.
557  * @device: a pointer to a struct device that is assiociated with this class device.
558  * @fmt: string for the class device's name
559  *
560  * This function can be used by char device classes.  A struct
561  * class_device will be created in sysfs, registered to the specified
562  * class.  A "dev" file will be created, showing the dev_t for the
563  * device.  The pointer to the struct class_device will be returned from
564  * the call.  Any further sysfs files that might be required can be
565  * created using this pointer.
566  *
567  * Note: the struct class passed to this function must have previously
568  * been created with a call to class_create().
569  */
570 struct class_device *class_device_create(struct class *cls, dev_t devt,
571                                          struct device *device, char *fmt, ...)
572 {
573         va_list args;
574         struct class_device *class_dev = NULL;
575         int retval = -ENODEV;
576
577         if (cls == NULL || IS_ERR(cls))
578                 goto error;
579
580         class_dev = kmalloc(sizeof(struct class_device), GFP_KERNEL);
581         if (!class_dev) {
582                 retval = -ENOMEM;
583                 goto error;
584         }
585         memset(class_dev, 0x00, sizeof(struct class_device));
586
587         class_dev->devt = devt;
588         class_dev->dev = device;
589         class_dev->class = cls;
590
591         va_start(args, fmt);
592         vsnprintf(class_dev->class_id, BUS_ID_SIZE, fmt, args);
593         va_end(args);
594         retval = class_device_register(class_dev);
595         if (retval)
596                 goto error;
597
598         return class_dev;
599
600 error:
601         kfree(class_dev);
602         return ERR_PTR(retval);
603 }
604
605 void class_device_del(struct class_device *class_dev)
606 {
607         struct class * parent = class_dev->class;
608         struct class_interface * class_intf;
609         char *class_name = NULL;
610
611         if (parent) {
612                 down(&parent->sem);
613                 list_del_init(&class_dev->node);
614                 list_for_each_entry(class_intf, &parent->interfaces, node)
615                         if (class_intf->remove)
616                                 class_intf->remove(class_dev);
617                 up(&parent->sem);
618         }
619
620         if (class_dev->dev) {
621                 class_name = make_class_name(class_dev);
622                 sysfs_remove_link(&class_dev->kobj, "device");
623                 sysfs_remove_link(&class_dev->dev->kobj, class_name);
624         }
625         if (class_dev->devt_attr)
626                 class_device_remove_file(class_dev, class_dev->devt_attr);
627         class_device_remove_attrs(class_dev);
628
629         kobject_hotplug(&class_dev->kobj, KOBJ_REMOVE);
630         kobject_del(&class_dev->kobj);
631
632         if (parent)
633                 class_put(parent);
634         kfree(class_name);
635 }
636
637 void class_device_unregister(struct class_device *class_dev)
638 {
639         pr_debug("CLASS: Unregistering class device. ID = '%s'\n",
640                  class_dev->class_id);
641         class_device_del(class_dev);
642         class_device_put(class_dev);
643 }
644
645 /**
646  * class_device_destroy - removes a class device that was created with class_device_create()
647  * @cls: the pointer to the struct class that this device was registered * with.
648  * @dev: the dev_t of the device that was previously registered.
649  *
650  * This call unregisters and cleans up a class device that was created with a
651  * call to class_device_create()
652  */
653 void class_device_destroy(struct class *cls, dev_t devt)
654 {
655         struct class_device *class_dev = NULL;
656         struct class_device *class_dev_tmp;
657
658         down(&cls->sem);
659         list_for_each_entry(class_dev_tmp, &cls->children, node) {
660                 if (class_dev_tmp->devt == devt) {
661                         class_dev = class_dev_tmp;
662                         break;
663                 }
664         }
665         up(&cls->sem);
666
667         if (class_dev)
668                 class_device_unregister(class_dev);
669 }
670
671 int class_device_rename(struct class_device *class_dev, char *new_name)
672 {
673         int error = 0;
674
675         class_dev = class_device_get(class_dev);
676         if (!class_dev)
677                 return -EINVAL;
678
679         pr_debug("CLASS: renaming '%s' to '%s'\n", class_dev->class_id,
680                  new_name);
681
682         strlcpy(class_dev->class_id, new_name, KOBJ_NAME_LEN);
683
684         error = kobject_rename(&class_dev->kobj, new_name);
685
686         class_device_put(class_dev);
687
688         return error;
689 }
690
691 struct class_device * class_device_get(struct class_device *class_dev)
692 {
693         if (class_dev)
694                 return to_class_dev(kobject_get(&class_dev->kobj));
695         return NULL;
696 }
697
698 void class_device_put(struct class_device *class_dev)
699 {
700         kobject_put(&class_dev->kobj);
701 }
702
703
704 int class_interface_register(struct class_interface *class_intf)
705 {
706         struct class *parent;
707         struct class_device *class_dev;
708
709         if (!class_intf || !class_intf->class)
710                 return -ENODEV;
711
712         parent = class_get(class_intf->class);
713         if (!parent)
714                 return -EINVAL;
715
716         down(&parent->sem);
717         list_add_tail(&class_intf->node, &parent->interfaces);
718         if (class_intf->add) {
719                 list_for_each_entry(class_dev, &parent->children, node)
720                         class_intf->add(class_dev);
721         }
722         up(&parent->sem);
723
724         return 0;
725 }
726
727 void class_interface_unregister(struct class_interface *class_intf)
728 {
729         struct class * parent = class_intf->class;
730         struct class_device *class_dev;
731
732         if (!parent)
733                 return;
734
735         down(&parent->sem);
736         list_del_init(&class_intf->node);
737         if (class_intf->remove) {
738                 list_for_each_entry(class_dev, &parent->children, node)
739                         class_intf->remove(class_dev);
740         }
741         up(&parent->sem);
742
743         class_put(parent);
744 }
745
746
747
748 int __init classes_init(void)
749 {
750         int retval;
751
752         retval = subsystem_register(&class_subsys);
753         if (retval)
754                 return retval;
755
756         /* ick, this is ugly, the things we go through to keep from showing up
757          * in sysfs... */
758         subsystem_init(&class_obj_subsys);
759         if (!class_obj_subsys.kset.subsys)
760                         class_obj_subsys.kset.subsys = &class_obj_subsys;
761         return 0;
762 }
763
764 EXPORT_SYMBOL_GPL(class_create_file);
765 EXPORT_SYMBOL_GPL(class_remove_file);
766 EXPORT_SYMBOL_GPL(class_register);
767 EXPORT_SYMBOL_GPL(class_unregister);
768 EXPORT_SYMBOL_GPL(class_get);
769 EXPORT_SYMBOL_GPL(class_put);
770 EXPORT_SYMBOL_GPL(class_create);
771 EXPORT_SYMBOL_GPL(class_destroy);
772
773 EXPORT_SYMBOL_GPL(class_device_register);
774 EXPORT_SYMBOL_GPL(class_device_unregister);
775 EXPORT_SYMBOL_GPL(class_device_initialize);
776 EXPORT_SYMBOL_GPL(class_device_add);
777 EXPORT_SYMBOL_GPL(class_device_del);
778 EXPORT_SYMBOL_GPL(class_device_get);
779 EXPORT_SYMBOL_GPL(class_device_put);
780 EXPORT_SYMBOL_GPL(class_device_create);
781 EXPORT_SYMBOL_GPL(class_device_destroy);
782 EXPORT_SYMBOL_GPL(class_device_create_file);
783 EXPORT_SYMBOL_GPL(class_device_remove_file);
784 EXPORT_SYMBOL_GPL(class_device_create_bin_file);
785 EXPORT_SYMBOL_GPL(class_device_remove_bin_file);
786
787 EXPORT_SYMBOL_GPL(class_interface_register);
788 EXPORT_SYMBOL_GPL(class_interface_unregister);