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