blob: 96def1ddba194b50347b171f221fbdfadbd896f5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/device.h>
14#include <linux/module.h>
15#include <linux/init.h>
16#include <linux/string.h>
17#include <linux/kdev_t.h>
gregkh@suse.dee9ba6362005-03-15 11:54:21 -080018#include <linux/err.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080019#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "base.h"
21
Greg Kroah-Hartmanc205ef42006-08-07 22:19:37 -070022extern struct subsystem devices_subsys;
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#define to_class_attr(_attr) container_of(_attr, struct class_attribute, attr)
25#define to_class(obj) container_of(obj, struct class, subsys.kset.kobj)
26
27static ssize_t
28class_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
29{
30 struct class_attribute * class_attr = to_class_attr(attr);
31 struct class * dc = to_class(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -050032 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34 if (class_attr->show)
35 ret = class_attr->show(dc, buf);
36 return ret;
37}
38
39static ssize_t
40class_attr_store(struct kobject * kobj, struct attribute * attr,
41 const char * buf, size_t count)
42{
43 struct class_attribute * class_attr = to_class_attr(attr);
44 struct class * dc = to_class(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -050045 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47 if (class_attr->store)
48 ret = class_attr->store(dc, buf, count);
49 return ret;
50}
51
52static void class_release(struct kobject * kobj)
53{
54 struct class *class = to_class(kobj);
55
56 pr_debug("class '%s': release.\n", class->name);
57
58 if (class->class_release)
59 class->class_release(class);
60 else
61 pr_debug("class '%s' does not have a release() function, "
62 "be careful\n", class->name);
63}
64
65static struct sysfs_ops class_sysfs_ops = {
66 .show = class_attr_show,
67 .store = class_attr_store,
68};
69
70static struct kobj_type ktype_class = {
71 .sysfs_ops = &class_sysfs_ops,
72 .release = class_release,
73};
74
75/* Hotplug events for classes go to the class_obj subsys */
76static decl_subsys(class, &ktype_class, NULL);
77
78
79int class_create_file(struct class * cls, const struct class_attribute * attr)
80{
81 int error;
82 if (cls) {
83 error = sysfs_create_file(&cls->subsys.kset.kobj, &attr->attr);
84 } else
85 error = -EINVAL;
86 return error;
87}
88
89void class_remove_file(struct class * cls, const struct class_attribute * attr)
90{
91 if (cls)
92 sysfs_remove_file(&cls->subsys.kset.kobj, &attr->attr);
93}
94
Greg Kroah-Hartman17407572006-05-02 16:59:59 +020095static struct class *class_get(struct class *cls)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
97 if (cls)
98 return container_of(subsys_get(&cls->subsys), struct class, subsys);
99 return NULL;
100}
101
Greg Kroah-Hartman17407572006-05-02 16:59:59 +0200102static void class_put(struct class * cls)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700104 if (cls)
105 subsys_put(&cls->subsys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
107
108
109static int add_class_attrs(struct class * cls)
110{
111 int i;
112 int error = 0;
113
114 if (cls->class_attrs) {
115 for (i = 0; attr_name(cls->class_attrs[i]); i++) {
116 error = class_create_file(cls,&cls->class_attrs[i]);
117 if (error)
118 goto Err;
119 }
120 }
121 Done:
122 return error;
123 Err:
124 while (--i >= 0)
125 class_remove_file(cls,&cls->class_attrs[i]);
126 goto Done;
127}
128
129static void remove_class_attrs(struct class * cls)
130{
131 int i;
132
133 if (cls->class_attrs) {
134 for (i = 0; attr_name(cls->class_attrs[i]); i++)
135 class_remove_file(cls,&cls->class_attrs[i]);
136 }
137}
138
139int class_register(struct class * cls)
140{
141 int error;
142
143 pr_debug("device class '%s': registering\n", cls->name);
144
145 INIT_LIST_HEAD(&cls->children);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700146 INIT_LIST_HEAD(&cls->devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 INIT_LIST_HEAD(&cls->interfaces);
148 init_MUTEX(&cls->sem);
149 error = kobject_set_name(&cls->subsys.kset.kobj, "%s", cls->name);
150 if (error)
151 return error;
152
153 subsys_set_kset(cls, class_subsys);
154
155 error = subsystem_register(&cls->subsys);
156 if (!error) {
157 error = add_class_attrs(class_get(cls));
158 class_put(cls);
159 }
160 return error;
161}
162
163void class_unregister(struct class * cls)
164{
165 pr_debug("device class '%s': unregistering\n", cls->name);
Akinobu Mita44c53c42006-11-21 04:53:18 +0900166 if (cls->virtual_dir)
167 kobject_unregister(cls->virtual_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 remove_class_attrs(cls);
169 subsystem_unregister(&cls->subsys);
170}
171
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800172static void class_create_release(struct class *cls)
173{
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700174 pr_debug("%s called for %s\n", __FUNCTION__, cls->name);
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800175 kfree(cls);
176}
177
178static void class_device_create_release(struct class_device *class_dev)
179{
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700180 pr_debug("%s called for %s\n", __FUNCTION__, class_dev->class_id);
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800181 kfree(class_dev);
182}
183
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700184/* needed to allow these devices to have parent class devices */
Kay Sievers312c0042005-11-16 09:00:00 +0100185static int class_device_create_uevent(struct class_device *class_dev,
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700186 char **envp, int num_envp,
187 char *buffer, int buffer_size)
188{
189 pr_debug("%s called for %s\n", __FUNCTION__, class_dev->class_id);
190 return 0;
191}
192
gregkh@suse.de2fc68442005-03-23 10:02:56 -0800193/**
194 * class_create - create a struct class structure
195 * @owner: pointer to the module that is to "own" this struct class
196 * @name: pointer to a string for the name of this class.
197 *
198 * This is used to create a struct class pointer that can then be used
199 * in calls to class_device_create().
200 *
201 * Note, the pointer created here is to be destroyed when finished by
202 * making a call to class_destroy().
203 */
Miguel Ojeda Sandonisab7d7372006-09-13 15:34:05 +0200204struct class *class_create(struct module *owner, const char *name)
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800205{
206 struct class *cls;
207 int retval;
208
Jiri Slaby4aed0642005-09-13 01:25:01 -0700209 cls = kzalloc(sizeof(*cls), GFP_KERNEL);
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800210 if (!cls) {
211 retval = -ENOMEM;
212 goto error;
213 }
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800214
215 cls->name = name;
216 cls->owner = owner;
217 cls->class_release = class_create_release;
218 cls->release = class_device_create_release;
219
220 retval = class_register(cls);
221 if (retval)
222 goto error;
223
224 return cls;
225
226error:
227 kfree(cls);
228 return ERR_PTR(retval);
229}
230
gregkh@suse.de2fc68442005-03-23 10:02:56 -0800231/**
232 * class_destroy - destroys a struct class structure
Rolf Eike Beer92a0f862006-09-29 01:59:13 -0700233 * @cls: pointer to the struct class that is to be destroyed
gregkh@suse.de2fc68442005-03-23 10:02:56 -0800234 *
235 * Note, the pointer to be destroyed must have been created with a call
236 * to class_create().
237 */
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800238void class_destroy(struct class *cls)
239{
240 if ((cls == NULL) || (IS_ERR(cls)))
241 return;
242
243 class_unregister(cls);
244}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246/* Class Device Stuff */
247
248int class_device_create_file(struct class_device * class_dev,
249 const struct class_device_attribute * attr)
250{
251 int error = -EINVAL;
252 if (class_dev)
253 error = sysfs_create_file(&class_dev->kobj, &attr->attr);
254 return error;
255}
256
257void class_device_remove_file(struct class_device * class_dev,
258 const struct class_device_attribute * attr)
259{
260 if (class_dev)
261 sysfs_remove_file(&class_dev->kobj, &attr->attr);
262}
263
264int class_device_create_bin_file(struct class_device *class_dev,
265 struct bin_attribute *attr)
266{
267 int error = -EINVAL;
268 if (class_dev)
269 error = sysfs_create_bin_file(&class_dev->kobj, attr);
270 return error;
271}
272
273void class_device_remove_bin_file(struct class_device *class_dev,
274 struct bin_attribute *attr)
275{
276 if (class_dev)
277 sysfs_remove_bin_file(&class_dev->kobj, attr);
278}
279
280static ssize_t
281class_device_attr_show(struct kobject * kobj, struct attribute * attr,
282 char * buf)
283{
284 struct class_device_attribute * class_dev_attr = to_class_dev_attr(attr);
285 struct class_device * cd = to_class_dev(kobj);
286 ssize_t ret = 0;
287
288 if (class_dev_attr->show)
289 ret = class_dev_attr->show(cd, buf);
290 return ret;
291}
292
293static ssize_t
294class_device_attr_store(struct kobject * kobj, struct attribute * attr,
295 const char * buf, size_t count)
296{
297 struct class_device_attribute * class_dev_attr = to_class_dev_attr(attr);
298 struct class_device * cd = to_class_dev(kobj);
299 ssize_t ret = 0;
300
301 if (class_dev_attr->store)
302 ret = class_dev_attr->store(cd, buf, count);
303 return ret;
304}
305
306static struct sysfs_ops class_dev_sysfs_ops = {
307 .show = class_device_attr_show,
308 .store = class_device_attr_store,
309};
310
311static void class_dev_release(struct kobject * kobj)
312{
313 struct class_device *cd = to_class_dev(kobj);
314 struct class * cls = cd->class;
315
316 pr_debug("device class '%s': release.\n", cd->class_id);
317
Jesper Juhlfef6ec82005-08-17 22:06:34 +0200318 kfree(cd->devt_attr);
319 cd->devt_attr = NULL;
Maneesh Soni208f3d62005-08-16 15:15:48 -0700320
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700321 if (cd->release)
322 cd->release(cd);
323 else if (cls->release)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 cls->release(cd);
325 else {
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700326 printk(KERN_ERR "Class Device '%s' does not have a release() function, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 "it is broken and must be fixed.\n",
328 cd->class_id);
329 WARN_ON(1);
330 }
331}
332
333static struct kobj_type ktype_class_device = {
334 .sysfs_ops = &class_dev_sysfs_ops,
335 .release = class_dev_release,
336};
337
Kay Sievers312c0042005-11-16 09:00:00 +0100338static int class_uevent_filter(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
340 struct kobj_type *ktype = get_ktype(kobj);
341
342 if (ktype == &ktype_class_device) {
343 struct class_device *class_dev = to_class_dev(kobj);
344 if (class_dev->class)
345 return 1;
346 }
347 return 0;
348}
349
Kay Sievers312c0042005-11-16 09:00:00 +0100350static const char *class_uevent_name(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
352 struct class_device *class_dev = to_class_dev(kobj);
353
354 return class_dev->class->name;
355}
356
Kay Sievers805fab42006-09-14 11:23:28 +0200357#ifdef CONFIG_SYSFS_DEPRECATED
358char *make_class_name(const char *name, struct kobject *kobj)
359{
360 char *class_name;
361 int size;
362
363 size = strlen(name) + strlen(kobject_name(kobj)) + 2;
364
365 class_name = kmalloc(size, GFP_KERNEL);
366 if (!class_name)
Cornelia Huckcb360bb2006-11-27 10:35:05 +0100367 return NULL;
Kay Sievers805fab42006-09-14 11:23:28 +0200368
369 strcpy(class_name, name);
370 strcat(class_name, ":");
371 strcat(class_name, kobject_name(kobj));
372 return class_name;
373}
374
375static int deprecated_class_uevent(char **envp, int num_envp, int *cur_index,
376 char *buffer, int buffer_size,
377 int *cur_len,
378 struct class_device *class_dev)
379{
380 struct device *dev = class_dev->dev;
381 char *path;
382
383 if (!dev)
384 return 0;
385
386 /* add device, backing this class device (deprecated) */
387 path = kobject_get_path(&dev->kobj, GFP_KERNEL);
388
389 add_uevent_var(envp, num_envp, cur_index, buffer, buffer_size,
390 cur_len, "PHYSDEVPATH=%s", path);
391 kfree(path);
392
393 if (dev->bus)
394 add_uevent_var(envp, num_envp, cur_index,
395 buffer, buffer_size, cur_len,
396 "PHYSDEVBUS=%s", dev->bus->name);
397
398 if (dev->driver)
399 add_uevent_var(envp, num_envp, cur_index,
400 buffer, buffer_size, cur_len,
401 "PHYSDEVDRIVER=%s", dev->driver->name);
402 return 0;
403}
404
405static int make_deprecated_class_device_links(struct class_device *class_dev)
406{
407 char *class_name;
408 int error;
409
410 if (!class_dev->dev)
411 return 0;
412
413 class_name = make_class_name(class_dev->class->name, &class_dev->kobj);
Cornelia Huckcb360bb2006-11-27 10:35:05 +0100414 if (class_name)
415 error = sysfs_create_link(&class_dev->dev->kobj,
416 &class_dev->kobj, class_name);
417 else
418 error = -ENOMEM;
Kay Sievers805fab42006-09-14 11:23:28 +0200419 kfree(class_name);
420 return error;
421}
422
423static void remove_deprecated_class_device_links(struct class_device *class_dev)
424{
425 char *class_name;
426
427 if (!class_dev->dev)
428 return;
429
430 class_name = make_class_name(class_dev->class->name, &class_dev->kobj);
Cornelia Huckcb360bb2006-11-27 10:35:05 +0100431 if (class_name)
432 sysfs_remove_link(&class_dev->dev->kobj, class_name);
Kay Sievers805fab42006-09-14 11:23:28 +0200433 kfree(class_name);
434}
435#else
436static inline int deprecated_class_uevent(char **envp, int num_envp,
437 int *cur_index, char *buffer,
438 int buffer_size, int *cur_len,
439 struct class_device *class_dev)
440{ return 0; }
441static inline int make_deprecated_class_device_links(struct class_device *cd)
442{ return 0; }
443static void remove_deprecated_class_device_links(struct class_device *cd)
444{ }
445#endif
446
Kay Sievers312c0042005-11-16 09:00:00 +0100447static int class_uevent(struct kset *kset, struct kobject *kobj, char **envp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 int num_envp, char *buffer, int buffer_size)
449{
450 struct class_device *class_dev = to_class_dev(kobj);
451 int i = 0;
452 int length = 0;
453 int retval = 0;
454
455 pr_debug("%s - name = %s\n", __FUNCTION__, class_dev->class_id);
456
Kay Sievers805fab42006-09-14 11:23:28 +0200457 deprecated_class_uevent(envp, num_envp, &i, buffer, buffer_size,
458 &length, class_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460 if (MAJOR(class_dev->devt)) {
Kay Sievers312c0042005-11-16 09:00:00 +0100461 add_uevent_var(envp, num_envp, &i,
462 buffer, buffer_size, &length,
463 "MAJOR=%u", MAJOR(class_dev->devt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Kay Sievers312c0042005-11-16 09:00:00 +0100465 add_uevent_var(envp, num_envp, &i,
466 buffer, buffer_size, &length,
467 "MINOR=%u", MINOR(class_dev->devt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 }
469
470 /* terminate, set to next free slot, shrink available space */
471 envp[i] = NULL;
472 envp = &envp[i];
473 num_envp -= i;
474 buffer = &buffer[length];
475 buffer_size -= length;
476
Kay Sievers312c0042005-11-16 09:00:00 +0100477 if (class_dev->uevent) {
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700478 /* have the class device specific function add its stuff */
Kay Sievers312c0042005-11-16 09:00:00 +0100479 retval = class_dev->uevent(class_dev, envp, num_envp,
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700480 buffer, buffer_size);
481 if (retval)
Kay Sievers312c0042005-11-16 09:00:00 +0100482 pr_debug("class_dev->uevent() returned %d\n", retval);
483 } else if (class_dev->class->uevent) {
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700484 /* have the class specific function add its stuff */
Kay Sievers312c0042005-11-16 09:00:00 +0100485 retval = class_dev->class->uevent(class_dev, envp, num_envp,
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700486 buffer, buffer_size);
487 if (retval)
Kay Sievers312c0042005-11-16 09:00:00 +0100488 pr_debug("class->uevent() returned %d\n", retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 }
490
491 return retval;
492}
493
Kay Sievers312c0042005-11-16 09:00:00 +0100494static struct kset_uevent_ops class_uevent_ops = {
495 .filter = class_uevent_filter,
496 .name = class_uevent_name,
497 .uevent = class_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498};
499
Kay Sievers312c0042005-11-16 09:00:00 +0100500static decl_subsys(class_obj, &ktype_class_device, &class_uevent_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502
503static int class_device_add_attrs(struct class_device * cd)
504{
505 int i;
506 int error = 0;
507 struct class * cls = cd->class;
508
509 if (cls->class_dev_attrs) {
510 for (i = 0; attr_name(cls->class_dev_attrs[i]); i++) {
511 error = class_device_create_file(cd,
512 &cls->class_dev_attrs[i]);
513 if (error)
514 goto Err;
515 }
516 }
517 Done:
518 return error;
519 Err:
520 while (--i >= 0)
521 class_device_remove_file(cd,&cls->class_dev_attrs[i]);
522 goto Done;
523}
524
525static void class_device_remove_attrs(struct class_device * cd)
526{
527 int i;
528 struct class * cls = cd->class;
529
530 if (cls->class_dev_attrs) {
531 for (i = 0; attr_name(cls->class_dev_attrs[i]); i++)
532 class_device_remove_file(cd,&cls->class_dev_attrs[i]);
533 }
534}
535
Stephen Hemminger14982212006-05-06 17:55:11 -0700536static int class_device_add_groups(struct class_device * cd)
537{
538 int i;
539 int error = 0;
540
541 if (cd->groups) {
542 for (i = 0; cd->groups[i]; i++) {
543 error = sysfs_create_group(&cd->kobj, cd->groups[i]);
544 if (error) {
545 while (--i >= 0)
546 sysfs_remove_group(&cd->kobj, cd->groups[i]);
547 goto out;
548 }
549 }
550 }
551out:
552 return error;
553}
554
555static void class_device_remove_groups(struct class_device * cd)
556{
557 int i;
558 if (cd->groups) {
559 for (i = 0; cd->groups[i]; i++) {
560 sysfs_remove_group(&cd->kobj, cd->groups[i]);
561 }
562 }
563}
564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565static ssize_t show_dev(struct class_device *class_dev, char *buf)
566{
567 return print_dev_t(buf, class_dev->devt);
568}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Kay Sieversa7fd6702005-10-01 14:49:43 +0200570static ssize_t store_uevent(struct class_device *class_dev,
571 const char *buf, size_t count)
572{
Kay Sievers312c0042005-11-16 09:00:00 +0100573 kobject_uevent(&class_dev->kobj, KOBJ_ADD);
Kay Sieversa7fd6702005-10-01 14:49:43 +0200574 return count;
575}
576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577void class_device_initialize(struct class_device *class_dev)
578{
579 kobj_set_kset_s(class_dev, class_obj_subsys);
580 kobject_init(&class_dev->kobj);
581 INIT_LIST_HEAD(&class_dev->node);
582}
583
584int class_device_add(struct class_device *class_dev)
585{
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700586 struct class *parent_class = NULL;
587 struct class_device *parent_class_dev = NULL;
588 struct class_interface *class_intf;
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700589 int error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
591 class_dev = class_device_get(class_dev);
592 if (!class_dev)
593 return -EINVAL;
594
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700595 if (!strlen(class_dev->class_id))
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700596 goto out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700598 parent_class = class_get(class_dev->class);
599 if (!parent_class)
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700600 goto out1;
601
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700602 parent_class_dev = class_device_get(class_dev->parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 pr_debug("CLASS: registering class device: ID = '%s'\n",
605 class_dev->class_id);
606
607 /* first, register with generic layer. */
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700608 error = kobject_set_name(&class_dev->kobj, "%s", class_dev->class_id);
609 if (error)
610 goto out2;
611
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700612 if (parent_class_dev)
613 class_dev->kobj.parent = &parent_class_dev->kobj;
614 else
615 class_dev->kobj.parent = &parent_class->subsys.kset.kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700617 error = kobject_add(&class_dev->kobj);
618 if (error)
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700619 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800621 /* add the needed attributes to this device */
Cornelia Huckf0e17612006-09-22 11:37:00 +0200622 error = sysfs_create_link(&class_dev->kobj,
623 &parent_class->subsys.kset.kobj, "subsystem");
624 if (error)
625 goto out3;
Kay Sieversa7fd6702005-10-01 14:49:43 +0200626 class_dev->uevent_attr.attr.name = "uevent";
627 class_dev->uevent_attr.attr.mode = S_IWUSR;
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700628 class_dev->uevent_attr.attr.owner = parent_class->owner;
Kay Sieversa7fd6702005-10-01 14:49:43 +0200629 class_dev->uevent_attr.store = store_uevent;
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700630 error = class_device_create_file(class_dev, &class_dev->uevent_attr);
631 if (error)
632 goto out3;
Kay Sieversa7fd6702005-10-01 14:49:43 +0200633
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800634 if (MAJOR(class_dev->devt)) {
635 struct class_device_attribute *attr;
Jiri Slaby4aed0642005-09-13 01:25:01 -0700636 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800637 if (!attr) {
638 error = -ENOMEM;
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700639 goto out4;
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800640 }
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800641 attr->attr.name = "dev";
642 attr->attr.mode = S_IRUGO;
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700643 attr->attr.owner = parent_class->owner;
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800644 attr->show = show_dev;
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700645 error = class_device_create_file(class_dev, attr);
646 if (error) {
647 kfree(attr);
648 goto out4;
649 }
650
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800651 class_dev->devt_attr = attr;
652 }
653
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700654 error = class_device_add_attrs(class_dev);
655 if (error)
656 goto out5;
657
Dmitry Torokhov76d1ce02005-07-10 01:21:24 -0500658 if (class_dev->dev) {
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700659 error = sysfs_create_link(&class_dev->kobj,
660 &class_dev->dev->kobj, "device");
661 if (error)
662 goto out6;
Dmitry Torokhov76d1ce02005-07-10 01:21:24 -0500663 }
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800664
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700665 error = class_device_add_groups(class_dev);
666 if (error)
Kay Sievers805fab42006-09-14 11:23:28 +0200667 goto out7;
668
669 error = make_deprecated_class_device_links(class_dev);
670 if (error)
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700671 goto out8;
Stephen Hemminger14982212006-05-06 17:55:11 -0700672
Kay Sievers312c0042005-11-16 09:00:00 +0100673 kobject_uevent(&class_dev->kobj, KOBJ_ADD);
Dmitry Torokhovdbe90352005-09-15 02:01:37 -0500674
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800675 /* notify any interfaces this device is now here */
Jayachandran Ca1438892006-04-03 12:31:53 -0700676 down(&parent_class->sem);
677 list_add_tail(&class_dev->node, &parent_class->children);
678 list_for_each_entry(class_intf, &parent_class->interfaces, node) {
679 if (class_intf->add)
680 class_intf->add(class_dev, class_intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 }
Jayachandran Ca1438892006-04-03 12:31:53 -0700682 up(&parent_class->sem);
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800683
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700684 goto out1;
685
686 out8:
Kay Sievers805fab42006-09-14 11:23:28 +0200687 class_device_remove_groups(class_dev);
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700688 out7:
689 if (class_dev->dev)
690 sysfs_remove_link(&class_dev->kobj, "device");
691 out6:
692 class_device_remove_attrs(class_dev);
693 out5:
694 if (class_dev->devt_attr)
695 class_device_remove_file(class_dev, class_dev->devt_attr);
696 out4:
697 class_device_remove_file(class_dev, &class_dev->uevent_attr);
698 out3:
699 kobject_del(&class_dev->kobj);
700 out2:
701 if(parent_class_dev)
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700702 class_device_put(parent_class_dev);
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700703 class_put(parent_class);
704 out1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 class_device_put(class_dev);
706 return error;
707}
708
709int class_device_register(struct class_device *class_dev)
710{
711 class_device_initialize(class_dev);
712 return class_device_add(class_dev);
713}
714
gregkh@suse.de2fc68442005-03-23 10:02:56 -0800715/**
716 * class_device_create - creates a class device and registers it with sysfs
Rolf Eike Beer92a0f862006-09-29 01:59:13 -0700717 * @cls: pointer to the struct class that this device should be registered to.
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700718 * @parent: pointer to the parent struct class_device of this new device, if any.
Rolf Eike Beer92a0f862006-09-29 01:59:13 -0700719 * @devt: the dev_t for the char device to be added.
gregkh@suse.de2fc68442005-03-23 10:02:56 -0800720 * @device: a pointer to a struct device that is assiociated with this class device.
721 * @fmt: string for the class device's name
722 *
723 * This function can be used by char device classes. A struct
724 * class_device will be created in sysfs, registered to the specified
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700725 * class.
726 * A "dev" file will be created, showing the dev_t for the device, if
727 * the dev_t is not 0,0.
728 * If a pointer to a parent struct class_device is passed in, the newly
729 * created struct class_device will be a child of that device in sysfs.
730 * The pointer to the struct class_device will be returned from the
731 * call. Any further sysfs files that might be required can be created
732 * using this pointer.
gregkh@suse.de2fc68442005-03-23 10:02:56 -0800733 *
734 * Note: the struct class passed to this function must have previously
735 * been created with a call to class_create().
736 */
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700737struct class_device *class_device_create(struct class *cls,
738 struct class_device *parent,
739 dev_t devt,
Dmitry Torokhovddd5d352006-08-10 01:18:18 -0400740 struct device *device,
741 const char *fmt, ...)
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800742{
743 va_list args;
744 struct class_device *class_dev = NULL;
745 int retval = -ENODEV;
746
747 if (cls == NULL || IS_ERR(cls))
748 goto error;
749
Jiri Slaby4aed0642005-09-13 01:25:01 -0700750 class_dev = kzalloc(sizeof(*class_dev), GFP_KERNEL);
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800751 if (!class_dev) {
752 retval = -ENOMEM;
753 goto error;
754 }
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800755
756 class_dev->devt = devt;
757 class_dev->dev = device;
758 class_dev->class = cls;
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700759 class_dev->parent = parent;
760 class_dev->release = class_device_create_release;
Kay Sievers312c0042005-11-16 09:00:00 +0100761 class_dev->uevent = class_device_create_uevent;
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800762
763 va_start(args, fmt);
764 vsnprintf(class_dev->class_id, BUS_ID_SIZE, fmt, args);
765 va_end(args);
766 retval = class_device_register(class_dev);
767 if (retval)
768 goto error;
769
770 return class_dev;
771
772error:
773 kfree(class_dev);
774 return ERR_PTR(retval);
775}
776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777void class_device_del(struct class_device *class_dev)
778{
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700779 struct class *parent_class = class_dev->class;
780 struct class_device *parent_device = class_dev->parent;
781 struct class_interface *class_intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700783 if (parent_class) {
784 down(&parent_class->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 list_del_init(&class_dev->node);
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700786 list_for_each_entry(class_intf, &parent_class->interfaces, node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 if (class_intf->remove)
Dmitry Torokhovd8539d82005-09-15 02:01:36 -0500788 class_intf->remove(class_dev, class_intf);
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700789 up(&parent_class->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 }
791
Dmitry Torokhov76d1ce02005-07-10 01:21:24 -0500792 if (class_dev->dev) {
Kay Sievers805fab42006-09-14 11:23:28 +0200793 remove_deprecated_class_device_links(class_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 sysfs_remove_link(&class_dev->kobj, "device");
Dmitry Torokhov76d1ce02005-07-10 01:21:24 -0500795 }
Kay Sieversb9d9c822006-06-15 15:31:56 +0200796 sysfs_remove_link(&class_dev->kobj, "subsystem");
Kay Sieversa7fd6702005-10-01 14:49:43 +0200797 class_device_remove_file(class_dev, &class_dev->uevent_attr);
Maneesh Soni208f3d62005-08-16 15:15:48 -0700798 if (class_dev->devt_attr)
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800799 class_device_remove_file(class_dev, class_dev->devt_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 class_device_remove_attrs(class_dev);
Stephen Hemminger14982212006-05-06 17:55:11 -0700801 class_device_remove_groups(class_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Kay Sievers312c0042005-11-16 09:00:00 +0100803 kobject_uevent(&class_dev->kobj, KOBJ_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 kobject_del(&class_dev->kobj);
805
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700806 class_device_put(parent_device);
807 class_put(parent_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808}
809
810void class_device_unregister(struct class_device *class_dev)
811{
812 pr_debug("CLASS: Unregistering class device. ID = '%s'\n",
813 class_dev->class_id);
814 class_device_del(class_dev);
815 class_device_put(class_dev);
816}
817
gregkh@suse.de2fc68442005-03-23 10:02:56 -0800818/**
819 * class_device_destroy - removes a class device that was created with class_device_create()
820 * @cls: the pointer to the struct class that this device was registered * with.
Rolf Eike Beer92a0f862006-09-29 01:59:13 -0700821 * @devt: the dev_t of the device that was previously registered.
gregkh@suse.de2fc68442005-03-23 10:02:56 -0800822 *
823 * This call unregisters and cleans up a class device that was created with a
824 * call to class_device_create()
825 */
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800826void class_device_destroy(struct class *cls, dev_t devt)
827{
828 struct class_device *class_dev = NULL;
829 struct class_device *class_dev_tmp;
830
831 down(&cls->sem);
832 list_for_each_entry(class_dev_tmp, &cls->children, node) {
833 if (class_dev_tmp->devt == devt) {
834 class_dev = class_dev_tmp;
835 break;
836 }
837 }
838 up(&cls->sem);
839
840 if (class_dev)
841 class_device_unregister(class_dev);
842}
843
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844int class_device_rename(struct class_device *class_dev, char *new_name)
845{
846 int error = 0;
Bill Nottingham3e513772005-09-22 00:47:36 -0700847 char *old_class_name = NULL, *new_class_name = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 class_dev = class_device_get(class_dev);
850 if (!class_dev)
851 return -EINVAL;
852
853 pr_debug("CLASS: renaming '%s' to '%s'\n", class_dev->class_id,
854 new_name);
855
Kay Sievers805fab42006-09-14 11:23:28 +0200856#ifdef CONFIG_SYSFS_DEPRECATED
Bill Nottingham3e513772005-09-22 00:47:36 -0700857 if (class_dev->dev)
Greg Kroah-Hartmanaa49b912006-06-20 13:59:20 -0700858 old_class_name = make_class_name(class_dev->class->name,
859 &class_dev->kobj);
Kay Sievers805fab42006-09-14 11:23:28 +0200860#endif
Bill Nottingham3e513772005-09-22 00:47:36 -0700861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 strlcpy(class_dev->class_id, new_name, KOBJ_NAME_LEN);
863
864 error = kobject_rename(&class_dev->kobj, new_name);
865
Kay Sievers805fab42006-09-14 11:23:28 +0200866#ifdef CONFIG_SYSFS_DEPRECATED
Bill Nottingham3e513772005-09-22 00:47:36 -0700867 if (class_dev->dev) {
Greg Kroah-Hartmanaa49b912006-06-20 13:59:20 -0700868 new_class_name = make_class_name(class_dev->class->name,
869 &class_dev->kobj);
Cornelia Huckcb360bb2006-11-27 10:35:05 +0100870 if (new_class_name)
871 sysfs_create_link(&class_dev->dev->kobj,
872 &class_dev->kobj, new_class_name);
873 if (old_class_name)
874 sysfs_remove_link(&class_dev->dev->kobj,
875 old_class_name);
Bill Nottingham3e513772005-09-22 00:47:36 -0700876 }
Kay Sievers805fab42006-09-14 11:23:28 +0200877#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 class_device_put(class_dev);
879
Bill Nottingham3e513772005-09-22 00:47:36 -0700880 kfree(old_class_name);
881 kfree(new_class_name);
882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 return error;
884}
885
886struct class_device * class_device_get(struct class_device *class_dev)
887{
888 if (class_dev)
889 return to_class_dev(kobject_get(&class_dev->kobj));
890 return NULL;
891}
892
893void class_device_put(struct class_device *class_dev)
894{
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700895 if (class_dev)
896 kobject_put(&class_dev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897}
898
899
900int class_interface_register(struct class_interface *class_intf)
901{
902 struct class *parent;
903 struct class_device *class_dev;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200904 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
906 if (!class_intf || !class_intf->class)
907 return -ENODEV;
908
909 parent = class_get(class_intf->class);
910 if (!parent)
911 return -EINVAL;
912
913 down(&parent->sem);
914 list_add_tail(&class_intf->node, &parent->interfaces);
915 if (class_intf->add) {
916 list_for_each_entry(class_dev, &parent->children, node)
Dmitry Torokhovd8539d82005-09-15 02:01:36 -0500917 class_intf->add(class_dev, class_intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 }
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200919 if (class_intf->add_dev) {
920 list_for_each_entry(dev, &parent->devices, node)
921 class_intf->add_dev(dev, class_intf);
922 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 up(&parent->sem);
924
925 return 0;
926}
927
928void class_interface_unregister(struct class_interface *class_intf)
929{
930 struct class * parent = class_intf->class;
931 struct class_device *class_dev;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200932 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
934 if (!parent)
935 return;
936
937 down(&parent->sem);
938 list_del_init(&class_intf->node);
939 if (class_intf->remove) {
940 list_for_each_entry(class_dev, &parent->children, node)
Dmitry Torokhovd8539d82005-09-15 02:01:36 -0500941 class_intf->remove(class_dev, class_intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 }
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200943 if (class_intf->remove_dev) {
944 list_for_each_entry(dev, &parent->devices, node)
945 class_intf->remove_dev(dev, class_intf);
946 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 up(&parent->sem);
948
949 class_put(parent);
950}
951
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952int __init classes_init(void)
953{
954 int retval;
955
956 retval = subsystem_register(&class_subsys);
957 if (retval)
958 return retval;
959
960 /* ick, this is ugly, the things we go through to keep from showing up
961 * in sysfs... */
962 subsystem_init(&class_obj_subsys);
963 if (!class_obj_subsys.kset.subsys)
964 class_obj_subsys.kset.subsys = &class_obj_subsys;
965 return 0;
966}
967
968EXPORT_SYMBOL_GPL(class_create_file);
969EXPORT_SYMBOL_GPL(class_remove_file);
970EXPORT_SYMBOL_GPL(class_register);
971EXPORT_SYMBOL_GPL(class_unregister);
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800972EXPORT_SYMBOL_GPL(class_create);
973EXPORT_SYMBOL_GPL(class_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
975EXPORT_SYMBOL_GPL(class_device_register);
976EXPORT_SYMBOL_GPL(class_device_unregister);
977EXPORT_SYMBOL_GPL(class_device_initialize);
978EXPORT_SYMBOL_GPL(class_device_add);
979EXPORT_SYMBOL_GPL(class_device_del);
980EXPORT_SYMBOL_GPL(class_device_get);
981EXPORT_SYMBOL_GPL(class_device_put);
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800982EXPORT_SYMBOL_GPL(class_device_create);
983EXPORT_SYMBOL_GPL(class_device_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984EXPORT_SYMBOL_GPL(class_device_create_file);
985EXPORT_SYMBOL_GPL(class_device_remove_file);
986EXPORT_SYMBOL_GPL(class_device_create_bin_file);
987EXPORT_SYMBOL_GPL(class_device_remove_bin_file);
988
989EXPORT_SYMBOL_GPL(class_interface_register);
990EXPORT_SYMBOL_GPL(class_interface_unregister);