[PATCH] class: convert the remaining class_simple users in the kernel to usee the...
[GitHub/exynos8895/android_kernel_samsung_universal8895.git] / drivers / base / class.c
CommitLineData
1da177e4
LT
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>
e9ba6365 19#include <linux/err.h>
1da177e4
LT
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
25static ssize_t
26class_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);
4a0c20bf 30 ssize_t ret = -EIO;
1da177e4
LT
31
32 if (class_attr->show)
33 ret = class_attr->show(dc, buf);
34 return ret;
35}
36
37static ssize_t
38class_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);
4a0c20bf 43 ssize_t ret = -EIO;
1da177e4
LT
44
45 if (class_attr->store)
46 ret = class_attr->store(dc, buf, count);
47 return ret;
48}
49
50static 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
63static struct sysfs_ops class_sysfs_ops = {
64 .show = class_attr_show,
65 .store = class_attr_store,
66};
67
68static 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 */
74static decl_subsys(class, &ktype_class, NULL);
75
76
77int 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
87void 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
93struct 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
100void class_put(struct class * cls)
101{
102 subsys_put(&cls->subsys);
103}
104
105
106static 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
126static 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
136int 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
159void 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
e9ba6365
GKH
166static void class_create_release(struct class *cls)
167{
168 kfree(cls);
169}
170
171static void class_device_create_release(struct class_device *class_dev)
172{
173 kfree(class_dev);
174}
175
176struct class *class_create(struct module *owner, char *name)
177{
178 struct class *cls;
179 int retval;
180
181 cls = kmalloc(sizeof(struct class), GFP_KERNEL);
182 if (!cls) {
183 retval = -ENOMEM;
184 goto error;
185 }
186 memset(cls, 0x00, sizeof(struct class));
187
188 cls->name = name;
189 cls->owner = owner;
190 cls->class_release = class_create_release;
191 cls->release = class_device_create_release;
192
193 retval = class_register(cls);
194 if (retval)
195 goto error;
196
197 return cls;
198
199error:
200 kfree(cls);
201 return ERR_PTR(retval);
202}
203
204void class_destroy(struct class *cls)
205{
206 if ((cls == NULL) || (IS_ERR(cls)))
207 return;
208
209 class_unregister(cls);
210}
1da177e4
LT
211
212/* Class Device Stuff */
213
214int class_device_create_file(struct class_device * class_dev,
215 const struct class_device_attribute * attr)
216{
217 int error = -EINVAL;
218 if (class_dev)
219 error = sysfs_create_file(&class_dev->kobj, &attr->attr);
220 return error;
221}
222
223void class_device_remove_file(struct class_device * class_dev,
224 const struct class_device_attribute * attr)
225{
226 if (class_dev)
227 sysfs_remove_file(&class_dev->kobj, &attr->attr);
228}
229
230int class_device_create_bin_file(struct class_device *class_dev,
231 struct bin_attribute *attr)
232{
233 int error = -EINVAL;
234 if (class_dev)
235 error = sysfs_create_bin_file(&class_dev->kobj, attr);
236 return error;
237}
238
239void class_device_remove_bin_file(struct class_device *class_dev,
240 struct bin_attribute *attr)
241{
242 if (class_dev)
243 sysfs_remove_bin_file(&class_dev->kobj, attr);
244}
245
246static ssize_t
247class_device_attr_show(struct kobject * kobj, struct attribute * attr,
248 char * buf)
249{
250 struct class_device_attribute * class_dev_attr = to_class_dev_attr(attr);
251 struct class_device * cd = to_class_dev(kobj);
252 ssize_t ret = 0;
253
254 if (class_dev_attr->show)
255 ret = class_dev_attr->show(cd, buf);
256 return ret;
257}
258
259static ssize_t
260class_device_attr_store(struct kobject * kobj, struct attribute * attr,
261 const char * buf, size_t count)
262{
263 struct class_device_attribute * class_dev_attr = to_class_dev_attr(attr);
264 struct class_device * cd = to_class_dev(kobj);
265 ssize_t ret = 0;
266
267 if (class_dev_attr->store)
268 ret = class_dev_attr->store(cd, buf, count);
269 return ret;
270}
271
272static struct sysfs_ops class_dev_sysfs_ops = {
273 .show = class_device_attr_show,
274 .store = class_device_attr_store,
275};
276
277static void class_dev_release(struct kobject * kobj)
278{
279 struct class_device *cd = to_class_dev(kobj);
280 struct class * cls = cd->class;
281
282 pr_debug("device class '%s': release.\n", cd->class_id);
283
284 if (cls->release)
285 cls->release(cd);
286 else {
287 printk(KERN_ERR "Device class '%s' does not have a release() function, "
288 "it is broken and must be fixed.\n",
289 cd->class_id);
290 WARN_ON(1);
291 }
292}
293
294static struct kobj_type ktype_class_device = {
295 .sysfs_ops = &class_dev_sysfs_ops,
296 .release = class_dev_release,
297};
298
299static int class_hotplug_filter(struct kset *kset, struct kobject *kobj)
300{
301 struct kobj_type *ktype = get_ktype(kobj);
302
303 if (ktype == &ktype_class_device) {
304 struct class_device *class_dev = to_class_dev(kobj);
305 if (class_dev->class)
306 return 1;
307 }
308 return 0;
309}
310
419cab3f 311static const char *class_hotplug_name(struct kset *kset, struct kobject *kobj)
1da177e4
LT
312{
313 struct class_device *class_dev = to_class_dev(kobj);
314
315 return class_dev->class->name;
316}
317
318static int class_hotplug(struct kset *kset, struct kobject *kobj, char **envp,
319 int num_envp, char *buffer, int buffer_size)
320{
321 struct class_device *class_dev = to_class_dev(kobj);
322 int i = 0;
323 int length = 0;
324 int retval = 0;
325
326 pr_debug("%s - name = %s\n", __FUNCTION__, class_dev->class_id);
327
328 if (class_dev->dev) {
329 /* add physical device, backing this device */
330 struct device *dev = class_dev->dev;
331 char *path = kobject_get_path(&dev->kobj, GFP_KERNEL);
332
333 add_hotplug_env_var(envp, num_envp, &i, buffer, buffer_size,
334 &length, "PHYSDEVPATH=%s", path);
335 kfree(path);
336
337 if (dev->bus)
338 add_hotplug_env_var(envp, num_envp, &i,
339 buffer, buffer_size, &length,
340 "PHYSDEVBUS=%s", dev->bus->name);
341
342 if (dev->driver)
343 add_hotplug_env_var(envp, num_envp, &i,
344 buffer, buffer_size, &length,
345 "PHYSDEVDRIVER=%s", dev->driver->name);
346 }
347
348 if (MAJOR(class_dev->devt)) {
349 add_hotplug_env_var(envp, num_envp, &i,
350 buffer, buffer_size, &length,
351 "MAJOR=%u", MAJOR(class_dev->devt));
352
353 add_hotplug_env_var(envp, num_envp, &i,
354 buffer, buffer_size, &length,
355 "MINOR=%u", MINOR(class_dev->devt));
356 }
357
358 /* terminate, set to next free slot, shrink available space */
359 envp[i] = NULL;
360 envp = &envp[i];
361 num_envp -= i;
362 buffer = &buffer[length];
363 buffer_size -= length;
364
365 if (class_dev->class->hotplug) {
366 /* have the bus specific function add its stuff */
367 retval = class_dev->class->hotplug (class_dev, envp, num_envp,
368 buffer, buffer_size);
369 if (retval) {
370 pr_debug ("%s - hotplug() returned %d\n",
371 __FUNCTION__, retval);
372 }
373 }
374
375 return retval;
376}
377
378static struct kset_hotplug_ops class_hotplug_ops = {
379 .filter = class_hotplug_filter,
380 .name = class_hotplug_name,
381 .hotplug = class_hotplug,
382};
383
384static decl_subsys(class_obj, &ktype_class_device, &class_hotplug_ops);
385
386
387static int class_device_add_attrs(struct class_device * cd)
388{
389 int i;
390 int error = 0;
391 struct class * cls = cd->class;
392
393 if (cls->class_dev_attrs) {
394 for (i = 0; attr_name(cls->class_dev_attrs[i]); i++) {
395 error = class_device_create_file(cd,
396 &cls->class_dev_attrs[i]);
397 if (error)
398 goto Err;
399 }
400 }
401 Done:
402 return error;
403 Err:
404 while (--i >= 0)
405 class_device_remove_file(cd,&cls->class_dev_attrs[i]);
406 goto Done;
407}
408
409static void class_device_remove_attrs(struct class_device * cd)
410{
411 int i;
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 class_device_remove_file(cd,&cls->class_dev_attrs[i]);
417 }
418}
419
420static ssize_t show_dev(struct class_device *class_dev, char *buf)
421{
422 return print_dev_t(buf, class_dev->devt);
423}
1da177e4
LT
424
425void class_device_initialize(struct class_device *class_dev)
426{
427 kobj_set_kset_s(class_dev, class_obj_subsys);
428 kobject_init(&class_dev->kobj);
429 INIT_LIST_HEAD(&class_dev->node);
430}
431
432int class_device_add(struct class_device *class_dev)
433{
434 struct class * parent = NULL;
435 struct class_interface * class_intf;
436 int error;
437
438 class_dev = class_device_get(class_dev);
439 if (!class_dev)
440 return -EINVAL;
441
442 if (!strlen(class_dev->class_id)) {
443 error = -EINVAL;
444 goto register_done;
445 }
446
447 parent = class_get(class_dev->class);
448
449 pr_debug("CLASS: registering class device: ID = '%s'\n",
450 class_dev->class_id);
451
452 /* first, register with generic layer. */
453 kobject_set_name(&class_dev->kobj, "%s", class_dev->class_id);
454 if (parent)
455 class_dev->kobj.parent = &parent->subsys.kset.kobj;
456
457 if ((error = kobject_add(&class_dev->kobj)))
458 goto register_done;
459
e9ba6365
GKH
460 /* add the needed attributes to this device */
461 if (MAJOR(class_dev->devt)) {
462 struct class_device_attribute *attr;
463 attr = kmalloc(sizeof(*attr), GFP_KERNEL);
464 if (!attr) {
465 error = -ENOMEM;
466 kobject_del(&class_dev->kobj);
467 goto register_done;
468 }
469 memset(attr, sizeof(*attr), 0x00);
470 attr->attr.name = "dev";
471 attr->attr.mode = S_IRUGO;
472 attr->attr.owner = parent->owner;
473 attr->show = show_dev;
474 attr->store = NULL;
475 class_device_create_file(class_dev, attr);
476 class_dev->devt_attr = attr;
477 }
478
479 class_device_add_attrs(class_dev);
480 if (class_dev->dev)
481 sysfs_create_link(&class_dev->kobj,
482 &class_dev->dev->kobj, "device");
483
484 /* notify any interfaces this device is now here */
1da177e4
LT
485 if (parent) {
486 down(&parent->sem);
487 list_add_tail(&class_dev->node, &parent->children);
488 list_for_each_entry(class_intf, &parent->interfaces, node)
489 if (class_intf->add)
490 class_intf->add(class_dev);
491 up(&parent->sem);
492 }
0700f56b 493 kobject_hotplug(&class_dev->kobj, KOBJ_ADD);
e9ba6365 494
1da177e4
LT
495 register_done:
496 if (error && parent)
497 class_put(parent);
498 class_device_put(class_dev);
499 return error;
500}
501
502int class_device_register(struct class_device *class_dev)
503{
504 class_device_initialize(class_dev);
505 return class_device_add(class_dev);
506}
507
e9ba6365
GKH
508struct class_device *class_device_create(struct class *cls, dev_t devt,
509 struct device *device, char *fmt, ...)
510{
511 va_list args;
512 struct class_device *class_dev = NULL;
513 int retval = -ENODEV;
514
515 if (cls == NULL || IS_ERR(cls))
516 goto error;
517
518 class_dev = kmalloc(sizeof(struct class_device), GFP_KERNEL);
519 if (!class_dev) {
520 retval = -ENOMEM;
521 goto error;
522 }
523 memset(class_dev, 0x00, sizeof(struct class_device));
524
525 class_dev->devt = devt;
526 class_dev->dev = device;
527 class_dev->class = cls;
528
529 va_start(args, fmt);
530 vsnprintf(class_dev->class_id, BUS_ID_SIZE, fmt, args);
531 va_end(args);
532 retval = class_device_register(class_dev);
533 if (retval)
534 goto error;
535
536 return class_dev;
537
538error:
539 kfree(class_dev);
540 return ERR_PTR(retval);
541}
542
1da177e4
LT
543void class_device_del(struct class_device *class_dev)
544{
545 struct class * parent = class_dev->class;
546 struct class_interface * class_intf;
547
548 if (parent) {
549 down(&parent->sem);
550 list_del_init(&class_dev->node);
551 list_for_each_entry(class_intf, &parent->interfaces, node)
552 if (class_intf->remove)
553 class_intf->remove(class_dev);
554 up(&parent->sem);
555 }
556
557 if (class_dev->dev)
558 sysfs_remove_link(&class_dev->kobj, "device");
e9ba6365
GKH
559 if (class_dev->devt_attr) {
560 class_device_remove_file(class_dev, class_dev->devt_attr);
561 kfree(class_dev->devt_attr);
562 class_dev->devt_attr = NULL;
563 }
1da177e4
LT
564 class_device_remove_attrs(class_dev);
565
0700f56b 566 kobject_hotplug(&class_dev->kobj, KOBJ_REMOVE);
1da177e4
LT
567 kobject_del(&class_dev->kobj);
568
569 if (parent)
570 class_put(parent);
571}
572
573void class_device_unregister(struct class_device *class_dev)
574{
575 pr_debug("CLASS: Unregistering class device. ID = '%s'\n",
576 class_dev->class_id);
577 class_device_del(class_dev);
578 class_device_put(class_dev);
579}
580
e9ba6365
GKH
581void class_device_destroy(struct class *cls, dev_t devt)
582{
583 struct class_device *class_dev = NULL;
584 struct class_device *class_dev_tmp;
585
586 down(&cls->sem);
587 list_for_each_entry(class_dev_tmp, &cls->children, node) {
588 if (class_dev_tmp->devt == devt) {
589 class_dev = class_dev_tmp;
590 break;
591 }
592 }
593 up(&cls->sem);
594
595 if (class_dev)
596 class_device_unregister(class_dev);
597}
598
1da177e4
LT
599int class_device_rename(struct class_device *class_dev, char *new_name)
600{
601 int error = 0;
602
603 class_dev = class_device_get(class_dev);
604 if (!class_dev)
605 return -EINVAL;
606
607 pr_debug("CLASS: renaming '%s' to '%s'\n", class_dev->class_id,
608 new_name);
609
610 strlcpy(class_dev->class_id, new_name, KOBJ_NAME_LEN);
611
612 error = kobject_rename(&class_dev->kobj, new_name);
613
614 class_device_put(class_dev);
615
616 return error;
617}
618
619struct class_device * class_device_get(struct class_device *class_dev)
620{
621 if (class_dev)
622 return to_class_dev(kobject_get(&class_dev->kobj));
623 return NULL;
624}
625
626void class_device_put(struct class_device *class_dev)
627{
628 kobject_put(&class_dev->kobj);
629}
630
631
632int class_interface_register(struct class_interface *class_intf)
633{
634 struct class *parent;
635 struct class_device *class_dev;
636
637 if (!class_intf || !class_intf->class)
638 return -ENODEV;
639
640 parent = class_get(class_intf->class);
641 if (!parent)
642 return -EINVAL;
643
644 down(&parent->sem);
645 list_add_tail(&class_intf->node, &parent->interfaces);
646 if (class_intf->add) {
647 list_for_each_entry(class_dev, &parent->children, node)
648 class_intf->add(class_dev);
649 }
650 up(&parent->sem);
651
652 return 0;
653}
654
655void class_interface_unregister(struct class_interface *class_intf)
656{
657 struct class * parent = class_intf->class;
658 struct class_device *class_dev;
659
660 if (!parent)
661 return;
662
663 down(&parent->sem);
664 list_del_init(&class_intf->node);
665 if (class_intf->remove) {
666 list_for_each_entry(class_dev, &parent->children, node)
667 class_intf->remove(class_dev);
668 }
669 up(&parent->sem);
670
671 class_put(parent);
672}
673
674
675
676int __init classes_init(void)
677{
678 int retval;
679
680 retval = subsystem_register(&class_subsys);
681 if (retval)
682 return retval;
683
684 /* ick, this is ugly, the things we go through to keep from showing up
685 * in sysfs... */
686 subsystem_init(&class_obj_subsys);
687 if (!class_obj_subsys.kset.subsys)
688 class_obj_subsys.kset.subsys = &class_obj_subsys;
689 return 0;
690}
691
692EXPORT_SYMBOL_GPL(class_create_file);
693EXPORT_SYMBOL_GPL(class_remove_file);
694EXPORT_SYMBOL_GPL(class_register);
695EXPORT_SYMBOL_GPL(class_unregister);
696EXPORT_SYMBOL_GPL(class_get);
697EXPORT_SYMBOL_GPL(class_put);
e9ba6365
GKH
698EXPORT_SYMBOL_GPL(class_create);
699EXPORT_SYMBOL_GPL(class_destroy);
1da177e4
LT
700
701EXPORT_SYMBOL_GPL(class_device_register);
702EXPORT_SYMBOL_GPL(class_device_unregister);
703EXPORT_SYMBOL_GPL(class_device_initialize);
704EXPORT_SYMBOL_GPL(class_device_add);
705EXPORT_SYMBOL_GPL(class_device_del);
706EXPORT_SYMBOL_GPL(class_device_get);
707EXPORT_SYMBOL_GPL(class_device_put);
e9ba6365
GKH
708EXPORT_SYMBOL_GPL(class_device_create);
709EXPORT_SYMBOL_GPL(class_device_destroy);
1da177e4
LT
710EXPORT_SYMBOL_GPL(class_device_create_file);
711EXPORT_SYMBOL_GPL(class_device_remove_file);
712EXPORT_SYMBOL_GPL(class_device_create_bin_file);
713EXPORT_SYMBOL_GPL(class_device_remove_bin_file);
714
715EXPORT_SYMBOL_GPL(class_interface_register);
716EXPORT_SYMBOL_GPL(class_interface_unregister);