blob: f098881f45b28129ec58aeae78e761fc4768e9bd [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);
166 remove_class_attrs(cls);
167 subsystem_unregister(&cls->subsys);
168}
169
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800170static void class_create_release(struct class *cls)
171{
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700172 pr_debug("%s called for %s\n", __FUNCTION__, cls->name);
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800173 kfree(cls);
174}
175
176static void class_device_create_release(struct class_device *class_dev)
177{
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700178 pr_debug("%s called for %s\n", __FUNCTION__, class_dev->class_id);
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800179 kfree(class_dev);
180}
181
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700182/* needed to allow these devices to have parent class devices */
Kay Sievers312c0042005-11-16 09:00:00 +0100183static int class_device_create_uevent(struct class_device *class_dev,
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700184 char **envp, int num_envp,
185 char *buffer, int buffer_size)
186{
187 pr_debug("%s called for %s\n", __FUNCTION__, class_dev->class_id);
188 return 0;
189}
190
gregkh@suse.de2fc68442005-03-23 10:02:56 -0800191/**
192 * class_create - create a struct class structure
193 * @owner: pointer to the module that is to "own" this struct class
194 * @name: pointer to a string for the name of this class.
195 *
196 * This is used to create a struct class pointer that can then be used
197 * in calls to class_device_create().
198 *
199 * Note, the pointer created here is to be destroyed when finished by
200 * making a call to class_destroy().
201 */
Miguel Ojeda Sandonisab7d7372006-09-13 15:34:05 +0200202struct class *class_create(struct module *owner, const char *name)
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800203{
204 struct class *cls;
205 int retval;
206
Jiri Slaby4aed0642005-09-13 01:25:01 -0700207 cls = kzalloc(sizeof(*cls), GFP_KERNEL);
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800208 if (!cls) {
209 retval = -ENOMEM;
210 goto error;
211 }
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800212
213 cls->name = name;
214 cls->owner = owner;
215 cls->class_release = class_create_release;
216 cls->release = class_device_create_release;
217
218 retval = class_register(cls);
219 if (retval)
220 goto error;
221
222 return cls;
223
224error:
225 kfree(cls);
226 return ERR_PTR(retval);
227}
228
gregkh@suse.de2fc68442005-03-23 10:02:56 -0800229/**
230 * class_destroy - destroys a struct class structure
Rolf Eike Beer92a0f862006-09-29 01:59:13 -0700231 * @cls: pointer to the struct class that is to be destroyed
gregkh@suse.de2fc68442005-03-23 10:02:56 -0800232 *
233 * Note, the pointer to be destroyed must have been created with a call
234 * to class_create().
235 */
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800236void class_destroy(struct class *cls)
237{
238 if ((cls == NULL) || (IS_ERR(cls)))
239 return;
240
241 class_unregister(cls);
242}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244/* Class Device Stuff */
245
246int class_device_create_file(struct class_device * class_dev,
247 const struct class_device_attribute * attr)
248{
249 int error = -EINVAL;
250 if (class_dev)
251 error = sysfs_create_file(&class_dev->kobj, &attr->attr);
252 return error;
253}
254
255void class_device_remove_file(struct class_device * class_dev,
256 const struct class_device_attribute * attr)
257{
258 if (class_dev)
259 sysfs_remove_file(&class_dev->kobj, &attr->attr);
260}
261
262int class_device_create_bin_file(struct class_device *class_dev,
263 struct bin_attribute *attr)
264{
265 int error = -EINVAL;
266 if (class_dev)
267 error = sysfs_create_bin_file(&class_dev->kobj, attr);
268 return error;
269}
270
271void class_device_remove_bin_file(struct class_device *class_dev,
272 struct bin_attribute *attr)
273{
274 if (class_dev)
275 sysfs_remove_bin_file(&class_dev->kobj, attr);
276}
277
278static ssize_t
279class_device_attr_show(struct kobject * kobj, struct attribute * attr,
280 char * buf)
281{
282 struct class_device_attribute * class_dev_attr = to_class_dev_attr(attr);
283 struct class_device * cd = to_class_dev(kobj);
284 ssize_t ret = 0;
285
286 if (class_dev_attr->show)
287 ret = class_dev_attr->show(cd, buf);
288 return ret;
289}
290
291static ssize_t
292class_device_attr_store(struct kobject * kobj, struct attribute * attr,
293 const char * buf, size_t count)
294{
295 struct class_device_attribute * class_dev_attr = to_class_dev_attr(attr);
296 struct class_device * cd = to_class_dev(kobj);
297 ssize_t ret = 0;
298
299 if (class_dev_attr->store)
300 ret = class_dev_attr->store(cd, buf, count);
301 return ret;
302}
303
304static struct sysfs_ops class_dev_sysfs_ops = {
305 .show = class_device_attr_show,
306 .store = class_device_attr_store,
307};
308
309static void class_dev_release(struct kobject * kobj)
310{
311 struct class_device *cd = to_class_dev(kobj);
312 struct class * cls = cd->class;
313
314 pr_debug("device class '%s': release.\n", cd->class_id);
315
Jesper Juhlfef6ec82005-08-17 22:06:34 +0200316 kfree(cd->devt_attr);
317 cd->devt_attr = NULL;
Maneesh Soni208f3d62005-08-16 15:15:48 -0700318
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700319 if (cd->release)
320 cd->release(cd);
321 else if (cls->release)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 cls->release(cd);
323 else {
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700324 printk(KERN_ERR "Class Device '%s' does not have a release() function, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 "it is broken and must be fixed.\n",
326 cd->class_id);
327 WARN_ON(1);
328 }
329}
330
331static struct kobj_type ktype_class_device = {
332 .sysfs_ops = &class_dev_sysfs_ops,
333 .release = class_dev_release,
334};
335
Kay Sievers312c0042005-11-16 09:00:00 +0100336static int class_uevent_filter(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
338 struct kobj_type *ktype = get_ktype(kobj);
339
340 if (ktype == &ktype_class_device) {
341 struct class_device *class_dev = to_class_dev(kobj);
342 if (class_dev->class)
343 return 1;
344 }
345 return 0;
346}
347
Kay Sievers312c0042005-11-16 09:00:00 +0100348static const char *class_uevent_name(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
350 struct class_device *class_dev = to_class_dev(kobj);
351
352 return class_dev->class->name;
353}
354
Kay Sievers805fab42006-09-14 11:23:28 +0200355#ifdef CONFIG_SYSFS_DEPRECATED
356char *make_class_name(const char *name, struct kobject *kobj)
357{
358 char *class_name;
359 int size;
360
361 size = strlen(name) + strlen(kobject_name(kobj)) + 2;
362
363 class_name = kmalloc(size, GFP_KERNEL);
364 if (!class_name)
365 return ERR_PTR(-ENOMEM);
366
367 strcpy(class_name, name);
368 strcat(class_name, ":");
369 strcat(class_name, kobject_name(kobj));
370 return class_name;
371}
372
373static int deprecated_class_uevent(char **envp, int num_envp, int *cur_index,
374 char *buffer, int buffer_size,
375 int *cur_len,
376 struct class_device *class_dev)
377{
378 struct device *dev = class_dev->dev;
379 char *path;
380
381 if (!dev)
382 return 0;
383
384 /* add device, backing this class device (deprecated) */
385 path = kobject_get_path(&dev->kobj, GFP_KERNEL);
386
387 add_uevent_var(envp, num_envp, cur_index, buffer, buffer_size,
388 cur_len, "PHYSDEVPATH=%s", path);
389 kfree(path);
390
391 if (dev->bus)
392 add_uevent_var(envp, num_envp, cur_index,
393 buffer, buffer_size, cur_len,
394 "PHYSDEVBUS=%s", dev->bus->name);
395
396 if (dev->driver)
397 add_uevent_var(envp, num_envp, cur_index,
398 buffer, buffer_size, cur_len,
399 "PHYSDEVDRIVER=%s", dev->driver->name);
400 return 0;
401}
402
403static int make_deprecated_class_device_links(struct class_device *class_dev)
404{
405 char *class_name;
406 int error;
407
408 if (!class_dev->dev)
409 return 0;
410
411 class_name = make_class_name(class_dev->class->name, &class_dev->kobj);
412 error = sysfs_create_link(&class_dev->dev->kobj, &class_dev->kobj,
413 class_name);
414 kfree(class_name);
415 return error;
416}
417
418static void remove_deprecated_class_device_links(struct class_device *class_dev)
419{
420 char *class_name;
421
422 if (!class_dev->dev)
423 return;
424
425 class_name = make_class_name(class_dev->class->name, &class_dev->kobj);
426 sysfs_remove_link(&class_dev->dev->kobj, class_name);
427 kfree(class_name);
428}
429#else
430static inline int deprecated_class_uevent(char **envp, int num_envp,
431 int *cur_index, char *buffer,
432 int buffer_size, int *cur_len,
433 struct class_device *class_dev)
434{ return 0; }
435static inline int make_deprecated_class_device_links(struct class_device *cd)
436{ return 0; }
437static void remove_deprecated_class_device_links(struct class_device *cd)
438{ }
439#endif
440
Kay Sievers312c0042005-11-16 09:00:00 +0100441static int class_uevent(struct kset *kset, struct kobject *kobj, char **envp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 int num_envp, char *buffer, int buffer_size)
443{
444 struct class_device *class_dev = to_class_dev(kobj);
445 int i = 0;
446 int length = 0;
447 int retval = 0;
448
449 pr_debug("%s - name = %s\n", __FUNCTION__, class_dev->class_id);
450
Kay Sievers805fab42006-09-14 11:23:28 +0200451 deprecated_class_uevent(envp, num_envp, &i, buffer, buffer_size,
452 &length, class_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454 if (MAJOR(class_dev->devt)) {
Kay Sievers312c0042005-11-16 09:00:00 +0100455 add_uevent_var(envp, num_envp, &i,
456 buffer, buffer_size, &length,
457 "MAJOR=%u", MAJOR(class_dev->devt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Kay Sievers312c0042005-11-16 09:00:00 +0100459 add_uevent_var(envp, num_envp, &i,
460 buffer, buffer_size, &length,
461 "MINOR=%u", MINOR(class_dev->devt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 }
463
464 /* terminate, set to next free slot, shrink available space */
465 envp[i] = NULL;
466 envp = &envp[i];
467 num_envp -= i;
468 buffer = &buffer[length];
469 buffer_size -= length;
470
Kay Sievers312c0042005-11-16 09:00:00 +0100471 if (class_dev->uevent) {
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700472 /* have the class device specific function add its stuff */
Kay Sievers312c0042005-11-16 09:00:00 +0100473 retval = class_dev->uevent(class_dev, envp, num_envp,
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700474 buffer, buffer_size);
475 if (retval)
Kay Sievers312c0042005-11-16 09:00:00 +0100476 pr_debug("class_dev->uevent() returned %d\n", retval);
477 } else if (class_dev->class->uevent) {
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700478 /* have the class specific function add its stuff */
Kay Sievers312c0042005-11-16 09:00:00 +0100479 retval = class_dev->class->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->uevent() returned %d\n", retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 }
484
485 return retval;
486}
487
Kay Sievers312c0042005-11-16 09:00:00 +0100488static struct kset_uevent_ops class_uevent_ops = {
489 .filter = class_uevent_filter,
490 .name = class_uevent_name,
491 .uevent = class_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492};
493
Kay Sievers312c0042005-11-16 09:00:00 +0100494static decl_subsys(class_obj, &ktype_class_device, &class_uevent_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
496
497static int class_device_add_attrs(struct class_device * cd)
498{
499 int i;
500 int error = 0;
501 struct class * cls = cd->class;
502
503 if (cls->class_dev_attrs) {
504 for (i = 0; attr_name(cls->class_dev_attrs[i]); i++) {
505 error = class_device_create_file(cd,
506 &cls->class_dev_attrs[i]);
507 if (error)
508 goto Err;
509 }
510 }
511 Done:
512 return error;
513 Err:
514 while (--i >= 0)
515 class_device_remove_file(cd,&cls->class_dev_attrs[i]);
516 goto Done;
517}
518
519static void class_device_remove_attrs(struct class_device * cd)
520{
521 int i;
522 struct class * cls = cd->class;
523
524 if (cls->class_dev_attrs) {
525 for (i = 0; attr_name(cls->class_dev_attrs[i]); i++)
526 class_device_remove_file(cd,&cls->class_dev_attrs[i]);
527 }
528}
529
Stephen Hemminger14982212006-05-06 17:55:11 -0700530static int class_device_add_groups(struct class_device * cd)
531{
532 int i;
533 int error = 0;
534
535 if (cd->groups) {
536 for (i = 0; cd->groups[i]; i++) {
537 error = sysfs_create_group(&cd->kobj, cd->groups[i]);
538 if (error) {
539 while (--i >= 0)
540 sysfs_remove_group(&cd->kobj, cd->groups[i]);
541 goto out;
542 }
543 }
544 }
545out:
546 return error;
547}
548
549static void class_device_remove_groups(struct class_device * cd)
550{
551 int i;
552 if (cd->groups) {
553 for (i = 0; cd->groups[i]; i++) {
554 sysfs_remove_group(&cd->kobj, cd->groups[i]);
555 }
556 }
557}
558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559static ssize_t show_dev(struct class_device *class_dev, char *buf)
560{
561 return print_dev_t(buf, class_dev->devt);
562}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Kay Sieversa7fd6702005-10-01 14:49:43 +0200564static ssize_t store_uevent(struct class_device *class_dev,
565 const char *buf, size_t count)
566{
Kay Sievers312c0042005-11-16 09:00:00 +0100567 kobject_uevent(&class_dev->kobj, KOBJ_ADD);
Kay Sieversa7fd6702005-10-01 14:49:43 +0200568 return count;
569}
570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571void class_device_initialize(struct class_device *class_dev)
572{
573 kobj_set_kset_s(class_dev, class_obj_subsys);
574 kobject_init(&class_dev->kobj);
575 INIT_LIST_HEAD(&class_dev->node);
576}
577
578int class_device_add(struct class_device *class_dev)
579{
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700580 struct class *parent_class = NULL;
581 struct class_device *parent_class_dev = NULL;
582 struct class_interface *class_intf;
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700583 int error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585 class_dev = class_device_get(class_dev);
586 if (!class_dev)
587 return -EINVAL;
588
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700589 if (!strlen(class_dev->class_id))
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700590 goto out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700592 parent_class = class_get(class_dev->class);
593 if (!parent_class)
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700594 goto out1;
595
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700596 parent_class_dev = class_device_get(class_dev->parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598 pr_debug("CLASS: registering class device: ID = '%s'\n",
599 class_dev->class_id);
600
601 /* first, register with generic layer. */
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700602 error = kobject_set_name(&class_dev->kobj, "%s", class_dev->class_id);
603 if (error)
604 goto out2;
605
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700606 if (parent_class_dev)
607 class_dev->kobj.parent = &parent_class_dev->kobj;
608 else
609 class_dev->kobj.parent = &parent_class->subsys.kset.kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700611 error = kobject_add(&class_dev->kobj);
612 if (error)
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700613 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800615 /* add the needed attributes to this device */
Cornelia Huckf0e17612006-09-22 11:37:00 +0200616 error = sysfs_create_link(&class_dev->kobj,
617 &parent_class->subsys.kset.kobj, "subsystem");
618 if (error)
619 goto out3;
Kay Sieversa7fd6702005-10-01 14:49:43 +0200620 class_dev->uevent_attr.attr.name = "uevent";
621 class_dev->uevent_attr.attr.mode = S_IWUSR;
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700622 class_dev->uevent_attr.attr.owner = parent_class->owner;
Kay Sieversa7fd6702005-10-01 14:49:43 +0200623 class_dev->uevent_attr.store = store_uevent;
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700624 error = class_device_create_file(class_dev, &class_dev->uevent_attr);
625 if (error)
626 goto out3;
Kay Sieversa7fd6702005-10-01 14:49:43 +0200627
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800628 if (MAJOR(class_dev->devt)) {
629 struct class_device_attribute *attr;
Jiri Slaby4aed0642005-09-13 01:25:01 -0700630 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800631 if (!attr) {
632 error = -ENOMEM;
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700633 goto out4;
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800634 }
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800635 attr->attr.name = "dev";
636 attr->attr.mode = S_IRUGO;
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700637 attr->attr.owner = parent_class->owner;
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800638 attr->show = show_dev;
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700639 error = class_device_create_file(class_dev, attr);
640 if (error) {
641 kfree(attr);
642 goto out4;
643 }
644
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800645 class_dev->devt_attr = attr;
646 }
647
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700648 error = class_device_add_attrs(class_dev);
649 if (error)
650 goto out5;
651
Dmitry Torokhov76d1ce02005-07-10 01:21:24 -0500652 if (class_dev->dev) {
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700653 error = sysfs_create_link(&class_dev->kobj,
654 &class_dev->dev->kobj, "device");
655 if (error)
656 goto out6;
Dmitry Torokhov76d1ce02005-07-10 01:21:24 -0500657 }
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800658
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700659 error = class_device_add_groups(class_dev);
660 if (error)
Kay Sievers805fab42006-09-14 11:23:28 +0200661 goto out7;
662
663 error = make_deprecated_class_device_links(class_dev);
664 if (error)
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700665 goto out8;
Stephen Hemminger14982212006-05-06 17:55:11 -0700666
Kay Sievers312c0042005-11-16 09:00:00 +0100667 kobject_uevent(&class_dev->kobj, KOBJ_ADD);
Dmitry Torokhovdbe90352005-09-15 02:01:37 -0500668
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800669 /* notify any interfaces this device is now here */
Jayachandran Ca1438892006-04-03 12:31:53 -0700670 down(&parent_class->sem);
671 list_add_tail(&class_dev->node, &parent_class->children);
672 list_for_each_entry(class_intf, &parent_class->interfaces, node) {
673 if (class_intf->add)
674 class_intf->add(class_dev, class_intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 }
Jayachandran Ca1438892006-04-03 12:31:53 -0700676 up(&parent_class->sem);
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800677
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700678 goto out1;
679
680 out8:
Kay Sievers805fab42006-09-14 11:23:28 +0200681 class_device_remove_groups(class_dev);
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700682 out7:
683 if (class_dev->dev)
684 sysfs_remove_link(&class_dev->kobj, "device");
685 out6:
686 class_device_remove_attrs(class_dev);
687 out5:
688 if (class_dev->devt_attr)
689 class_device_remove_file(class_dev, class_dev->devt_attr);
690 out4:
691 class_device_remove_file(class_dev, &class_dev->uevent_attr);
692 out3:
693 kobject_del(&class_dev->kobj);
694 out2:
695 if(parent_class_dev)
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700696 class_device_put(parent_class_dev);
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700697 class_put(parent_class);
698 out1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 class_device_put(class_dev);
700 return error;
701}
702
703int class_device_register(struct class_device *class_dev)
704{
705 class_device_initialize(class_dev);
706 return class_device_add(class_dev);
707}
708
gregkh@suse.de2fc68442005-03-23 10:02:56 -0800709/**
710 * class_device_create - creates a class device and registers it with sysfs
Rolf Eike Beer92a0f862006-09-29 01:59:13 -0700711 * @cls: pointer to the struct class that this device should be registered to.
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700712 * @parent: pointer to the parent struct class_device of this new device, if any.
Rolf Eike Beer92a0f862006-09-29 01:59:13 -0700713 * @devt: the dev_t for the char device to be added.
gregkh@suse.de2fc68442005-03-23 10:02:56 -0800714 * @device: a pointer to a struct device that is assiociated with this class device.
715 * @fmt: string for the class device's name
716 *
717 * This function can be used by char device classes. A struct
718 * class_device will be created in sysfs, registered to the specified
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700719 * class.
720 * A "dev" file will be created, showing the dev_t for the device, if
721 * the dev_t is not 0,0.
722 * If a pointer to a parent struct class_device is passed in, the newly
723 * created struct class_device will be a child of that device in sysfs.
724 * The pointer to the struct class_device will be returned from the
725 * call. Any further sysfs files that might be required can be created
726 * using this pointer.
gregkh@suse.de2fc68442005-03-23 10:02:56 -0800727 *
728 * Note: the struct class passed to this function must have previously
729 * been created with a call to class_create().
730 */
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700731struct class_device *class_device_create(struct class *cls,
732 struct class_device *parent,
733 dev_t devt,
Dmitry Torokhovddd5d352006-08-10 01:18:18 -0400734 struct device *device,
735 const char *fmt, ...)
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800736{
737 va_list args;
738 struct class_device *class_dev = NULL;
739 int retval = -ENODEV;
740
741 if (cls == NULL || IS_ERR(cls))
742 goto error;
743
Jiri Slaby4aed0642005-09-13 01:25:01 -0700744 class_dev = kzalloc(sizeof(*class_dev), GFP_KERNEL);
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800745 if (!class_dev) {
746 retval = -ENOMEM;
747 goto error;
748 }
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800749
750 class_dev->devt = devt;
751 class_dev->dev = device;
752 class_dev->class = cls;
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700753 class_dev->parent = parent;
754 class_dev->release = class_device_create_release;
Kay Sievers312c0042005-11-16 09:00:00 +0100755 class_dev->uevent = class_device_create_uevent;
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800756
757 va_start(args, fmt);
758 vsnprintf(class_dev->class_id, BUS_ID_SIZE, fmt, args);
759 va_end(args);
760 retval = class_device_register(class_dev);
761 if (retval)
762 goto error;
763
764 return class_dev;
765
766error:
767 kfree(class_dev);
768 return ERR_PTR(retval);
769}
770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771void class_device_del(struct class_device *class_dev)
772{
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700773 struct class *parent_class = class_dev->class;
774 struct class_device *parent_device = class_dev->parent;
775 struct class_interface *class_intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700777 if (parent_class) {
778 down(&parent_class->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 list_del_init(&class_dev->node);
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700780 list_for_each_entry(class_intf, &parent_class->interfaces, node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 if (class_intf->remove)
Dmitry Torokhovd8539d82005-09-15 02:01:36 -0500782 class_intf->remove(class_dev, class_intf);
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700783 up(&parent_class->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 }
785
Dmitry Torokhov76d1ce02005-07-10 01:21:24 -0500786 if (class_dev->dev) {
Kay Sievers805fab42006-09-14 11:23:28 +0200787 remove_deprecated_class_device_links(class_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 sysfs_remove_link(&class_dev->kobj, "device");
Dmitry Torokhov76d1ce02005-07-10 01:21:24 -0500789 }
Kay Sieversb9d9c822006-06-15 15:31:56 +0200790 sysfs_remove_link(&class_dev->kobj, "subsystem");
Kay Sieversa7fd6702005-10-01 14:49:43 +0200791 class_device_remove_file(class_dev, &class_dev->uevent_attr);
Maneesh Soni208f3d62005-08-16 15:15:48 -0700792 if (class_dev->devt_attr)
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800793 class_device_remove_file(class_dev, class_dev->devt_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 class_device_remove_attrs(class_dev);
Stephen Hemminger14982212006-05-06 17:55:11 -0700795 class_device_remove_groups(class_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Kay Sievers312c0042005-11-16 09:00:00 +0100797 kobject_uevent(&class_dev->kobj, KOBJ_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 kobject_del(&class_dev->kobj);
799
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700800 class_device_put(parent_device);
801 class_put(parent_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802}
803
804void class_device_unregister(struct class_device *class_dev)
805{
806 pr_debug("CLASS: Unregistering class device. ID = '%s'\n",
807 class_dev->class_id);
808 class_device_del(class_dev);
809 class_device_put(class_dev);
810}
811
gregkh@suse.de2fc68442005-03-23 10:02:56 -0800812/**
813 * class_device_destroy - removes a class device that was created with class_device_create()
814 * @cls: the pointer to the struct class that this device was registered * with.
Rolf Eike Beer92a0f862006-09-29 01:59:13 -0700815 * @devt: the dev_t of the device that was previously registered.
gregkh@suse.de2fc68442005-03-23 10:02:56 -0800816 *
817 * This call unregisters and cleans up a class device that was created with a
818 * call to class_device_create()
819 */
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800820void class_device_destroy(struct class *cls, dev_t devt)
821{
822 struct class_device *class_dev = NULL;
823 struct class_device *class_dev_tmp;
824
825 down(&cls->sem);
826 list_for_each_entry(class_dev_tmp, &cls->children, node) {
827 if (class_dev_tmp->devt == devt) {
828 class_dev = class_dev_tmp;
829 break;
830 }
831 }
832 up(&cls->sem);
833
834 if (class_dev)
835 class_device_unregister(class_dev);
836}
837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838int class_device_rename(struct class_device *class_dev, char *new_name)
839{
840 int error = 0;
Bill Nottingham3e513772005-09-22 00:47:36 -0700841 char *old_class_name = NULL, *new_class_name = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
843 class_dev = class_device_get(class_dev);
844 if (!class_dev)
845 return -EINVAL;
846
847 pr_debug("CLASS: renaming '%s' to '%s'\n", class_dev->class_id,
848 new_name);
849
Kay Sievers805fab42006-09-14 11:23:28 +0200850#ifdef CONFIG_SYSFS_DEPRECATED
Bill Nottingham3e513772005-09-22 00:47:36 -0700851 if (class_dev->dev)
Greg Kroah-Hartmanaa49b912006-06-20 13:59:20 -0700852 old_class_name = make_class_name(class_dev->class->name,
853 &class_dev->kobj);
Kay Sievers805fab42006-09-14 11:23:28 +0200854#endif
Bill Nottingham3e513772005-09-22 00:47:36 -0700855
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 strlcpy(class_dev->class_id, new_name, KOBJ_NAME_LEN);
857
858 error = kobject_rename(&class_dev->kobj, new_name);
859
Kay Sievers805fab42006-09-14 11:23:28 +0200860#ifdef CONFIG_SYSFS_DEPRECATED
Bill Nottingham3e513772005-09-22 00:47:36 -0700861 if (class_dev->dev) {
Greg Kroah-Hartmanaa49b912006-06-20 13:59:20 -0700862 new_class_name = make_class_name(class_dev->class->name,
863 &class_dev->kobj);
Bill Nottingham3e513772005-09-22 00:47:36 -0700864 sysfs_create_link(&class_dev->dev->kobj, &class_dev->kobj,
865 new_class_name);
866 sysfs_remove_link(&class_dev->dev->kobj, old_class_name);
867 }
Kay Sievers805fab42006-09-14 11:23:28 +0200868#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 class_device_put(class_dev);
870
Bill Nottingham3e513772005-09-22 00:47:36 -0700871 kfree(old_class_name);
872 kfree(new_class_name);
873
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 return error;
875}
876
877struct class_device * class_device_get(struct class_device *class_dev)
878{
879 if (class_dev)
880 return to_class_dev(kobject_get(&class_dev->kobj));
881 return NULL;
882}
883
884void class_device_put(struct class_device *class_dev)
885{
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700886 if (class_dev)
887 kobject_put(&class_dev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888}
889
890
891int class_interface_register(struct class_interface *class_intf)
892{
893 struct class *parent;
894 struct class_device *class_dev;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200895 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
897 if (!class_intf || !class_intf->class)
898 return -ENODEV;
899
900 parent = class_get(class_intf->class);
901 if (!parent)
902 return -EINVAL;
903
904 down(&parent->sem);
905 list_add_tail(&class_intf->node, &parent->interfaces);
906 if (class_intf->add) {
907 list_for_each_entry(class_dev, &parent->children, node)
Dmitry Torokhovd8539d82005-09-15 02:01:36 -0500908 class_intf->add(class_dev, class_intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 }
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200910 if (class_intf->add_dev) {
911 list_for_each_entry(dev, &parent->devices, node)
912 class_intf->add_dev(dev, class_intf);
913 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 up(&parent->sem);
915
916 return 0;
917}
918
919void class_interface_unregister(struct class_interface *class_intf)
920{
921 struct class * parent = class_intf->class;
922 struct class_device *class_dev;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200923 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
925 if (!parent)
926 return;
927
928 down(&parent->sem);
929 list_del_init(&class_intf->node);
930 if (class_intf->remove) {
931 list_for_each_entry(class_dev, &parent->children, node)
Dmitry Torokhovd8539d82005-09-15 02:01:36 -0500932 class_intf->remove(class_dev, class_intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 }
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200934 if (class_intf->remove_dev) {
935 list_for_each_entry(dev, &parent->devices, node)
936 class_intf->remove_dev(dev, class_intf);
937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 up(&parent->sem);
939
940 class_put(parent);
941}
942
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943int __init classes_init(void)
944{
945 int retval;
946
947 retval = subsystem_register(&class_subsys);
948 if (retval)
949 return retval;
950
951 /* ick, this is ugly, the things we go through to keep from showing up
952 * in sysfs... */
953 subsystem_init(&class_obj_subsys);
954 if (!class_obj_subsys.kset.subsys)
955 class_obj_subsys.kset.subsys = &class_obj_subsys;
956 return 0;
957}
958
959EXPORT_SYMBOL_GPL(class_create_file);
960EXPORT_SYMBOL_GPL(class_remove_file);
961EXPORT_SYMBOL_GPL(class_register);
962EXPORT_SYMBOL_GPL(class_unregister);
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800963EXPORT_SYMBOL_GPL(class_create);
964EXPORT_SYMBOL_GPL(class_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
966EXPORT_SYMBOL_GPL(class_device_register);
967EXPORT_SYMBOL_GPL(class_device_unregister);
968EXPORT_SYMBOL_GPL(class_device_initialize);
969EXPORT_SYMBOL_GPL(class_device_add);
970EXPORT_SYMBOL_GPL(class_device_del);
971EXPORT_SYMBOL_GPL(class_device_get);
972EXPORT_SYMBOL_GPL(class_device_put);
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800973EXPORT_SYMBOL_GPL(class_device_create);
974EXPORT_SYMBOL_GPL(class_device_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975EXPORT_SYMBOL_GPL(class_device_create_file);
976EXPORT_SYMBOL_GPL(class_device_remove_file);
977EXPORT_SYMBOL_GPL(class_device_create_bin_file);
978EXPORT_SYMBOL_GPL(class_device_remove_bin_file);
979
980EXPORT_SYMBOL_GPL(class_interface_register);
981EXPORT_SYMBOL_GPL(class_interface_unregister);