blob: f6ebe6af3ef20229aa7a62a9083a35d2e988355d [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
22#define to_class_attr(_attr) container_of(_attr, struct class_attribute, attr)
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -070023#define to_class(obj) container_of(obj, struct class, subsys.kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25static ssize_t
26class_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
27{
28 struct class_attribute * class_attr = to_class_attr(attr);
29 struct class * dc = to_class(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -050030 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32 if (class_attr->show)
33 ret = class_attr->show(dc, buf);
34 return ret;
35}
36
37static ssize_t
38class_attr_store(struct kobject * kobj, struct attribute * attr,
39 const char * buf, size_t count)
40{
41 struct class_attribute * class_attr = to_class_attr(attr);
42 struct class * dc = to_class(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -050043 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45 if (class_attr->store)
46 ret = class_attr->store(dc, buf, count);
47 return ret;
48}
49
50static void class_release(struct kobject * kobj)
51{
52 struct class *class = to_class(kobj);
53
54 pr_debug("class '%s': release.\n", class->name);
55
56 if (class->class_release)
57 class->class_release(class);
58 else
59 pr_debug("class '%s' does not have a release() function, "
60 "be careful\n", class->name);
61}
62
63static struct sysfs_ops class_sysfs_ops = {
64 .show = class_attr_show,
65 .store = class_attr_store,
66};
67
Greg Kroah-Hartmanadc56802007-10-11 10:47:49 -060068static struct kobj_type class_ktype = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 .sysfs_ops = &class_sysfs_ops,
70 .release = class_release,
71};
72
73/* Hotplug events for classes go to the class_obj subsys */
Greg Kroah-Hartmanadc56802007-10-11 10:47:49 -060074static decl_subsys(class, &class_ktype, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76
77int class_create_file(struct class * cls, const struct class_attribute * attr)
78{
79 int error;
80 if (cls) {
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -070081 error = sysfs_create_file(&cls->subsys.kobj, &attr->attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 } else
83 error = -EINVAL;
84 return error;
85}
86
87void class_remove_file(struct class * cls, const struct class_attribute * attr)
88{
89 if (cls)
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -070090 sysfs_remove_file(&cls->subsys.kobj, &attr->attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
92
Greg Kroah-Hartman17407572006-05-02 16:59:59 +020093static struct class *class_get(struct class *cls)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
95 if (cls)
Greg Kroah-Hartman1ef4cfa2007-09-12 15:06:57 -070096 return container_of(kset_get(&cls->subsys), struct class, subsys);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 return NULL;
98}
99
Greg Kroah-Hartman17407572006-05-02 16:59:59 +0200100static void class_put(struct class * cls)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700102 if (cls)
Greg Kroah-Hartman6e9d9302007-09-12 15:06:57 -0700103 kset_put(&cls->subsys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
106
107static int add_class_attrs(struct class * cls)
108{
109 int i;
110 int error = 0;
111
112 if (cls->class_attrs) {
113 for (i = 0; attr_name(cls->class_attrs[i]); i++) {
114 error = class_create_file(cls,&cls->class_attrs[i]);
115 if (error)
116 goto Err;
117 }
118 }
119 Done:
120 return error;
121 Err:
122 while (--i >= 0)
123 class_remove_file(cls,&cls->class_attrs[i]);
124 goto Done;
125}
126
127static void remove_class_attrs(struct class * cls)
128{
129 int i;
130
131 if (cls->class_attrs) {
132 for (i = 0; attr_name(cls->class_attrs[i]); i++)
133 class_remove_file(cls,&cls->class_attrs[i]);
134 }
135}
136
137int class_register(struct class * cls)
138{
139 int error;
140
141 pr_debug("device class '%s': registering\n", cls->name);
142
143 INIT_LIST_HEAD(&cls->children);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700144 INIT_LIST_HEAD(&cls->devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 INIT_LIST_HEAD(&cls->interfaces);
Kay Sievers86406242007-03-14 03:25:56 +0100146 kset_init(&cls->class_dirs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 init_MUTEX(&cls->sem);
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700148 error = kobject_set_name(&cls->subsys.kobj, "%s", cls->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 if (error)
150 return error;
151
Greg Kroah-Hartmand6b05b82007-09-12 15:06:57 -0700152 cls->subsys.kobj.kset = &class_subsys;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154 error = subsystem_register(&cls->subsys);
155 if (!error) {
156 error = add_class_attrs(class_get(cls));
157 class_put(cls);
158 }
159 return error;
160}
161
162void class_unregister(struct class * cls)
163{
164 pr_debug("device class '%s': unregistering\n", cls->name);
165 remove_class_attrs(cls);
166 subsystem_unregister(&cls->subsys);
167}
168
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800169static void class_create_release(struct class *cls)
170{
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700171 pr_debug("%s called for %s\n", __FUNCTION__, cls->name);
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800172 kfree(cls);
173}
174
175static void class_device_create_release(struct class_device *class_dev)
176{
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700177 pr_debug("%s called for %s\n", __FUNCTION__, class_dev->class_id);
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800178 kfree(class_dev);
179}
180
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700181/* needed to allow these devices to have parent class devices */
Kay Sievers312c0042005-11-16 09:00:00 +0100182static int class_device_create_uevent(struct class_device *class_dev,
Kay Sievers7eff2e72007-08-14 15:15:12 +0200183 struct kobj_uevent_env *env)
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700184{
185 pr_debug("%s called for %s\n", __FUNCTION__, class_dev->class_id);
186 return 0;
187}
188
gregkh@suse.de2fc68442005-03-23 10:02:56 -0800189/**
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 */
Miguel Ojeda Sandonisab7d7372006-09-13 15:34:05 +0200200struct class *class_create(struct module *owner, const char *name)
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800201{
202 struct class *cls;
203 int retval;
204
Jiri Slaby4aed0642005-09-13 01:25:01 -0700205 cls = kzalloc(sizeof(*cls), GFP_KERNEL);
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800206 if (!cls) {
207 retval = -ENOMEM;
208 goto error;
209 }
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800210
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
gregkh@suse.de2fc68442005-03-23 10:02:56 -0800227/**
228 * class_destroy - destroys a struct class structure
Rolf Eike Beer92a0f862006-09-29 01:59:13 -0700229 * @cls: pointer to the struct class that is to be destroyed
gregkh@suse.de2fc68442005-03-23 10:02:56 -0800230 *
231 * Note, the pointer to be destroyed must have been created with a call
232 * to class_create().
233 */
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800234void class_destroy(struct class *cls)
235{
236 if ((cls == NULL) || (IS_ERR(cls)))
237 return;
238
239 class_unregister(cls);
240}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260static ssize_t
261class_device_attr_show(struct kobject * kobj, struct attribute * attr,
262 char * buf)
263{
264 struct class_device_attribute * class_dev_attr = to_class_dev_attr(attr);
265 struct class_device * cd = to_class_dev(kobj);
266 ssize_t ret = 0;
267
268 if (class_dev_attr->show)
269 ret = class_dev_attr->show(cd, buf);
270 return ret;
271}
272
273static ssize_t
274class_device_attr_store(struct kobject * kobj, struct attribute * attr,
275 const char * buf, size_t count)
276{
277 struct class_device_attribute * class_dev_attr = to_class_dev_attr(attr);
278 struct class_device * cd = to_class_dev(kobj);
279 ssize_t ret = 0;
280
281 if (class_dev_attr->store)
282 ret = class_dev_attr->store(cd, buf, count);
283 return ret;
284}
285
286static struct sysfs_ops class_dev_sysfs_ops = {
287 .show = class_device_attr_show,
288 .store = class_device_attr_store,
289};
290
291static void class_dev_release(struct kobject * kobj)
292{
293 struct class_device *cd = to_class_dev(kobj);
294 struct class * cls = cd->class;
295
296 pr_debug("device class '%s': release.\n", cd->class_id);
297
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700298 if (cd->release)
299 cd->release(cd);
300 else if (cls->release)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 cls->release(cd);
302 else {
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700303 printk(KERN_ERR "Class Device '%s' does not have a release() function, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 "it is broken and must be fixed.\n",
305 cd->class_id);
306 WARN_ON(1);
307 }
308}
309
Greg Kroah-Hartmanadc56802007-10-11 10:47:49 -0600310static struct kobj_type class_device_ktype = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 .sysfs_ops = &class_dev_sysfs_ops,
312 .release = class_dev_release,
313};
314
Kay Sievers312c0042005-11-16 09:00:00 +0100315static int class_uevent_filter(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
317 struct kobj_type *ktype = get_ktype(kobj);
318
Greg Kroah-Hartmanadc56802007-10-11 10:47:49 -0600319 if (ktype == &class_device_ktype) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 struct class_device *class_dev = to_class_dev(kobj);
321 if (class_dev->class)
322 return 1;
323 }
324 return 0;
325}
326
Kay Sievers312c0042005-11-16 09:00:00 +0100327static const char *class_uevent_name(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
329 struct class_device *class_dev = to_class_dev(kobj);
330
331 return class_dev->class->name;
332}
333
Kay Sievers805fab42006-09-14 11:23:28 +0200334#ifdef CONFIG_SYSFS_DEPRECATED
335char *make_class_name(const char *name, struct kobject *kobj)
336{
337 char *class_name;
338 int size;
339
340 size = strlen(name) + strlen(kobject_name(kobj)) + 2;
341
342 class_name = kmalloc(size, GFP_KERNEL);
343 if (!class_name)
Cornelia Huckcb360bb2006-11-27 10:35:05 +0100344 return NULL;
Kay Sievers805fab42006-09-14 11:23:28 +0200345
346 strcpy(class_name, name);
347 strcat(class_name, ":");
348 strcat(class_name, kobject_name(kobj));
349 return class_name;
350}
351
Kay Sievers805fab42006-09-14 11:23:28 +0200352static int make_deprecated_class_device_links(struct class_device *class_dev)
353{
354 char *class_name;
355 int error;
356
357 if (!class_dev->dev)
358 return 0;
359
360 class_name = make_class_name(class_dev->class->name, &class_dev->kobj);
Cornelia Huckcb360bb2006-11-27 10:35:05 +0100361 if (class_name)
362 error = sysfs_create_link(&class_dev->dev->kobj,
363 &class_dev->kobj, class_name);
364 else
365 error = -ENOMEM;
Kay Sievers805fab42006-09-14 11:23:28 +0200366 kfree(class_name);
367 return error;
368}
369
370static void remove_deprecated_class_device_links(struct class_device *class_dev)
371{
372 char *class_name;
373
374 if (!class_dev->dev)
375 return;
376
377 class_name = make_class_name(class_dev->class->name, &class_dev->kobj);
Cornelia Huckcb360bb2006-11-27 10:35:05 +0100378 if (class_name)
379 sysfs_remove_link(&class_dev->dev->kobj, class_name);
Kay Sievers805fab42006-09-14 11:23:28 +0200380 kfree(class_name);
381}
382#else
Kay Sievers805fab42006-09-14 11:23:28 +0200383static inline int make_deprecated_class_device_links(struct class_device *cd)
384{ return 0; }
385static void remove_deprecated_class_device_links(struct class_device *cd)
386{ }
387#endif
388
Kay Sievers7eff2e72007-08-14 15:15:12 +0200389static int class_uevent(struct kset *kset, struct kobject *kobj,
390 struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
392 struct class_device *class_dev = to_class_dev(kobj);
Kay Sievers2c7afd12007-05-01 13:46:26 +0200393 struct device *dev = class_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 int retval = 0;
395
396 pr_debug("%s - name = %s\n", __FUNCTION__, class_dev->class_id);
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 if (MAJOR(class_dev->devt)) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200399 add_uevent_var(env, "MAJOR=%u", MAJOR(class_dev->devt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Kay Sievers7eff2e72007-08-14 15:15:12 +0200401 add_uevent_var(env, "MINOR=%u", MINOR(class_dev->devt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 }
403
Kay Sievers2c7afd12007-05-01 13:46:26 +0200404 if (dev) {
405 const char *path = kobject_get_path(&dev->kobj, GFP_KERNEL);
406 if (path) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200407 add_uevent_var(env, "PHYSDEVPATH=%s", path);
Kay Sievers2c7afd12007-05-01 13:46:26 +0200408 kfree(path);
409 }
410
411 if (dev->bus)
Kay Sievers7eff2e72007-08-14 15:15:12 +0200412 add_uevent_var(env, "PHYSDEVBUS=%s", dev->bus->name);
Kay Sievers2c7afd12007-05-01 13:46:26 +0200413
414 if (dev->driver)
Kay Sievers7eff2e72007-08-14 15:15:12 +0200415 add_uevent_var(env, "PHYSDEVDRIVER=%s", dev->driver->name);
Kay Sievers2c7afd12007-05-01 13:46:26 +0200416 }
417
Kay Sievers312c0042005-11-16 09:00:00 +0100418 if (class_dev->uevent) {
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700419 /* have the class device specific function add its stuff */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200420 retval = class_dev->uevent(class_dev, env);
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700421 if (retval)
Kay Sievers312c0042005-11-16 09:00:00 +0100422 pr_debug("class_dev->uevent() returned %d\n", retval);
423 } else if (class_dev->class->uevent) {
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700424 /* have the class specific function add its stuff */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200425 retval = class_dev->class->uevent(class_dev, env);
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700426 if (retval)
Kay Sievers312c0042005-11-16 09:00:00 +0100427 pr_debug("class->uevent() returned %d\n", retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 }
429
430 return retval;
431}
432
Kay Sievers312c0042005-11-16 09:00:00 +0100433static struct kset_uevent_ops class_uevent_ops = {
434 .filter = class_uevent_filter,
435 .name = class_uevent_name,
436 .uevent = class_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437};
438
Greg Kroah-Hartmanadc56802007-10-11 10:47:49 -0600439static decl_subsys(class_obj, &class_device_ktype, &class_uevent_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441
442static int class_device_add_attrs(struct class_device * cd)
443{
444 int i;
445 int error = 0;
446 struct class * cls = cd->class;
447
448 if (cls->class_dev_attrs) {
449 for (i = 0; attr_name(cls->class_dev_attrs[i]); i++) {
450 error = class_device_create_file(cd,
451 &cls->class_dev_attrs[i]);
452 if (error)
453 goto Err;
454 }
455 }
456 Done:
457 return error;
458 Err:
459 while (--i >= 0)
460 class_device_remove_file(cd,&cls->class_dev_attrs[i]);
461 goto Done;
462}
463
464static void class_device_remove_attrs(struct class_device * cd)
465{
466 int i;
467 struct class * cls = cd->class;
468
469 if (cls->class_dev_attrs) {
470 for (i = 0; attr_name(cls->class_dev_attrs[i]); i++)
471 class_device_remove_file(cd,&cls->class_dev_attrs[i]);
472 }
473}
474
Stephen Hemminger14982212006-05-06 17:55:11 -0700475static int class_device_add_groups(struct class_device * cd)
476{
477 int i;
478 int error = 0;
479
480 if (cd->groups) {
481 for (i = 0; cd->groups[i]; i++) {
482 error = sysfs_create_group(&cd->kobj, cd->groups[i]);
483 if (error) {
484 while (--i >= 0)
485 sysfs_remove_group(&cd->kobj, cd->groups[i]);
486 goto out;
487 }
488 }
489 }
490out:
491 return error;
492}
493
494static void class_device_remove_groups(struct class_device * cd)
495{
496 int i;
497 if (cd->groups) {
498 for (i = 0; cd->groups[i]; i++) {
499 sysfs_remove_group(&cd->kobj, cd->groups[i]);
500 }
501 }
502}
503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504static ssize_t show_dev(struct class_device *class_dev, char *buf)
505{
506 return print_dev_t(buf, class_dev->devt);
507}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Tejun Heoad6a1e12007-06-14 03:45:17 +0900509static struct class_device_attribute class_devt_attr =
510 __ATTR(dev, S_IRUGO, show_dev, NULL);
511
Kay Sieversa7fd6702005-10-01 14:49:43 +0200512static ssize_t store_uevent(struct class_device *class_dev,
513 const char *buf, size_t count)
514{
Kay Sievers312c0042005-11-16 09:00:00 +0100515 kobject_uevent(&class_dev->kobj, KOBJ_ADD);
Kay Sieversa7fd6702005-10-01 14:49:43 +0200516 return count;
517}
518
Tejun Heoad6a1e12007-06-14 03:45:17 +0900519static struct class_device_attribute class_uevent_attr =
520 __ATTR(uevent, S_IWUSR, NULL, store_uevent);
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522void class_device_initialize(struct class_device *class_dev)
523{
524 kobj_set_kset_s(class_dev, class_obj_subsys);
525 kobject_init(&class_dev->kobj);
526 INIT_LIST_HEAD(&class_dev->node);
527}
528
529int class_device_add(struct class_device *class_dev)
530{
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700531 struct class *parent_class = NULL;
532 struct class_device *parent_class_dev = NULL;
533 struct class_interface *class_intf;
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700534 int error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536 class_dev = class_device_get(class_dev);
537 if (!class_dev)
538 return -EINVAL;
539
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700540 if (!strlen(class_dev->class_id))
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700541 goto out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700543 parent_class = class_get(class_dev->class);
544 if (!parent_class)
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700545 goto out1;
546
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700547 parent_class_dev = class_device_get(class_dev->parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
549 pr_debug("CLASS: registering class device: ID = '%s'\n",
550 class_dev->class_id);
551
552 /* first, register with generic layer. */
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700553 error = kobject_set_name(&class_dev->kobj, "%s", class_dev->class_id);
554 if (error)
555 goto out2;
556
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700557 if (parent_class_dev)
558 class_dev->kobj.parent = &parent_class_dev->kobj;
559 else
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700560 class_dev->kobj.parent = &parent_class->subsys.kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700562 error = kobject_add(&class_dev->kobj);
563 if (error)
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700564 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800566 /* add the needed attributes to this device */
Cornelia Huckf0e17612006-09-22 11:37:00 +0200567 error = sysfs_create_link(&class_dev->kobj,
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700568 &parent_class->subsys.kobj, "subsystem");
Cornelia Huckf0e17612006-09-22 11:37:00 +0200569 if (error)
570 goto out3;
Tejun Heoad6a1e12007-06-14 03:45:17 +0900571
572 error = class_device_create_file(class_dev, &class_uevent_attr);
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700573 if (error)
574 goto out3;
Kay Sieversa7fd6702005-10-01 14:49:43 +0200575
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800576 if (MAJOR(class_dev->devt)) {
Tejun Heoad6a1e12007-06-14 03:45:17 +0900577 error = class_device_create_file(class_dev, &class_devt_attr);
578 if (error)
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700579 goto out4;
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800580 }
581
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700582 error = class_device_add_attrs(class_dev);
583 if (error)
584 goto out5;
585
Dmitry Torokhov76d1ce02005-07-10 01:21:24 -0500586 if (class_dev->dev) {
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700587 error = sysfs_create_link(&class_dev->kobj,
588 &class_dev->dev->kobj, "device");
589 if (error)
590 goto out6;
Dmitry Torokhov76d1ce02005-07-10 01:21:24 -0500591 }
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800592
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700593 error = class_device_add_groups(class_dev);
594 if (error)
Kay Sievers805fab42006-09-14 11:23:28 +0200595 goto out7;
596
597 error = make_deprecated_class_device_links(class_dev);
598 if (error)
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700599 goto out8;
Stephen Hemminger14982212006-05-06 17:55:11 -0700600
Kay Sievers312c0042005-11-16 09:00:00 +0100601 kobject_uevent(&class_dev->kobj, KOBJ_ADD);
Dmitry Torokhovdbe90352005-09-15 02:01:37 -0500602
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800603 /* notify any interfaces this device is now here */
Jayachandran Ca1438892006-04-03 12:31:53 -0700604 down(&parent_class->sem);
605 list_add_tail(&class_dev->node, &parent_class->children);
606 list_for_each_entry(class_intf, &parent_class->interfaces, node) {
607 if (class_intf->add)
608 class_intf->add(class_dev, class_intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 }
Jayachandran Ca1438892006-04-03 12:31:53 -0700610 up(&parent_class->sem);
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800611
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700612 goto out1;
613
614 out8:
Kay Sievers805fab42006-09-14 11:23:28 +0200615 class_device_remove_groups(class_dev);
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700616 out7:
617 if (class_dev->dev)
618 sysfs_remove_link(&class_dev->kobj, "device");
619 out6:
620 class_device_remove_attrs(class_dev);
621 out5:
Tejun Heoad6a1e12007-06-14 03:45:17 +0900622 if (MAJOR(class_dev->devt))
623 class_device_remove_file(class_dev, &class_devt_attr);
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700624 out4:
Tejun Heoad6a1e12007-06-14 03:45:17 +0900625 class_device_remove_file(class_dev, &class_uevent_attr);
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700626 out3:
627 kobject_del(&class_dev->kobj);
628 out2:
629 if(parent_class_dev)
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700630 class_device_put(parent_class_dev);
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700631 class_put(parent_class);
632 out1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 class_device_put(class_dev);
634 return error;
635}
636
637int class_device_register(struct class_device *class_dev)
638{
639 class_device_initialize(class_dev);
640 return class_device_add(class_dev);
641}
642
gregkh@suse.de2fc68442005-03-23 10:02:56 -0800643/**
644 * class_device_create - creates a class device and registers it with sysfs
Rolf Eike Beer92a0f862006-09-29 01:59:13 -0700645 * @cls: pointer to the struct class that this device should be registered to.
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700646 * @parent: pointer to the parent struct class_device of this new device, if any.
Rolf Eike Beer92a0f862006-09-29 01:59:13 -0700647 * @devt: the dev_t for the char device to be added.
gregkh@suse.de2fc68442005-03-23 10:02:56 -0800648 * @device: a pointer to a struct device that is assiociated with this class device.
649 * @fmt: string for the class device's name
650 *
651 * This function can be used by char device classes. A struct
652 * class_device will be created in sysfs, registered to the specified
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700653 * class.
654 * A "dev" file will be created, showing the dev_t for the device, if
655 * the dev_t is not 0,0.
656 * If a pointer to a parent struct class_device is passed in, the newly
657 * created struct class_device will be a child of that device in sysfs.
658 * The pointer to the struct class_device will be returned from the
659 * call. Any further sysfs files that might be required can be created
660 * using this pointer.
gregkh@suse.de2fc68442005-03-23 10:02:56 -0800661 *
662 * Note: the struct class passed to this function must have previously
663 * been created with a call to class_create().
664 */
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700665struct class_device *class_device_create(struct class *cls,
666 struct class_device *parent,
667 dev_t devt,
Dmitry Torokhovddd5d352006-08-10 01:18:18 -0400668 struct device *device,
669 const char *fmt, ...)
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800670{
671 va_list args;
672 struct class_device *class_dev = NULL;
673 int retval = -ENODEV;
674
675 if (cls == NULL || IS_ERR(cls))
676 goto error;
677
Jiri Slaby4aed0642005-09-13 01:25:01 -0700678 class_dev = kzalloc(sizeof(*class_dev), GFP_KERNEL);
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800679 if (!class_dev) {
680 retval = -ENOMEM;
681 goto error;
682 }
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800683
684 class_dev->devt = devt;
685 class_dev->dev = device;
686 class_dev->class = cls;
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700687 class_dev->parent = parent;
688 class_dev->release = class_device_create_release;
Kay Sievers312c0042005-11-16 09:00:00 +0100689 class_dev->uevent = class_device_create_uevent;
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800690
691 va_start(args, fmt);
692 vsnprintf(class_dev->class_id, BUS_ID_SIZE, fmt, args);
693 va_end(args);
694 retval = class_device_register(class_dev);
695 if (retval)
696 goto error;
697
698 return class_dev;
699
700error:
701 kfree(class_dev);
702 return ERR_PTR(retval);
703}
704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705void class_device_del(struct class_device *class_dev)
706{
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700707 struct class *parent_class = class_dev->class;
708 struct class_device *parent_device = class_dev->parent;
709 struct class_interface *class_intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700711 if (parent_class) {
712 down(&parent_class->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 list_del_init(&class_dev->node);
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700714 list_for_each_entry(class_intf, &parent_class->interfaces, node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 if (class_intf->remove)
Dmitry Torokhovd8539d82005-09-15 02:01:36 -0500716 class_intf->remove(class_dev, class_intf);
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700717 up(&parent_class->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 }
719
Dmitry Torokhov76d1ce02005-07-10 01:21:24 -0500720 if (class_dev->dev) {
Kay Sievers805fab42006-09-14 11:23:28 +0200721 remove_deprecated_class_device_links(class_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 sysfs_remove_link(&class_dev->kobj, "device");
Dmitry Torokhov76d1ce02005-07-10 01:21:24 -0500723 }
Kay Sieversb9d9c822006-06-15 15:31:56 +0200724 sysfs_remove_link(&class_dev->kobj, "subsystem");
Tejun Heoad6a1e12007-06-14 03:45:17 +0900725 class_device_remove_file(class_dev, &class_uevent_attr);
726 if (MAJOR(class_dev->devt))
727 class_device_remove_file(class_dev, &class_devt_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 class_device_remove_attrs(class_dev);
Stephen Hemminger14982212006-05-06 17:55:11 -0700729 class_device_remove_groups(class_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Kay Sievers312c0042005-11-16 09:00:00 +0100731 kobject_uevent(&class_dev->kobj, KOBJ_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 kobject_del(&class_dev->kobj);
733
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700734 class_device_put(parent_device);
735 class_put(parent_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736}
737
738void class_device_unregister(struct class_device *class_dev)
739{
740 pr_debug("CLASS: Unregistering class device. ID = '%s'\n",
741 class_dev->class_id);
742 class_device_del(class_dev);
743 class_device_put(class_dev);
744}
745
gregkh@suse.de2fc68442005-03-23 10:02:56 -0800746/**
747 * class_device_destroy - removes a class device that was created with class_device_create()
748 * @cls: the pointer to the struct class that this device was registered * with.
Rolf Eike Beer92a0f862006-09-29 01:59:13 -0700749 * @devt: the dev_t of the device that was previously registered.
gregkh@suse.de2fc68442005-03-23 10:02:56 -0800750 *
751 * This call unregisters and cleans up a class device that was created with a
752 * call to class_device_create()
753 */
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800754void class_device_destroy(struct class *cls, dev_t devt)
755{
756 struct class_device *class_dev = NULL;
757 struct class_device *class_dev_tmp;
758
759 down(&cls->sem);
760 list_for_each_entry(class_dev_tmp, &cls->children, node) {
761 if (class_dev_tmp->devt == devt) {
762 class_dev = class_dev_tmp;
763 break;
764 }
765 }
766 up(&cls->sem);
767
768 if (class_dev)
769 class_device_unregister(class_dev);
770}
771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772struct class_device * class_device_get(struct class_device *class_dev)
773{
774 if (class_dev)
775 return to_class_dev(kobject_get(&class_dev->kobj));
776 return NULL;
777}
778
779void class_device_put(struct class_device *class_dev)
780{
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700781 if (class_dev)
782 kobject_put(&class_dev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783}
784
785
786int class_interface_register(struct class_interface *class_intf)
787{
788 struct class *parent;
789 struct class_device *class_dev;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200790 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792 if (!class_intf || !class_intf->class)
793 return -ENODEV;
794
795 parent = class_get(class_intf->class);
796 if (!parent)
797 return -EINVAL;
798
799 down(&parent->sem);
800 list_add_tail(&class_intf->node, &parent->interfaces);
801 if (class_intf->add) {
802 list_for_each_entry(class_dev, &parent->children, node)
Dmitry Torokhovd8539d82005-09-15 02:01:36 -0500803 class_intf->add(class_dev, class_intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 }
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200805 if (class_intf->add_dev) {
806 list_for_each_entry(dev, &parent->devices, node)
807 class_intf->add_dev(dev, class_intf);
808 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 up(&parent->sem);
810
811 return 0;
812}
813
814void class_interface_unregister(struct class_interface *class_intf)
815{
816 struct class * parent = class_intf->class;
817 struct class_device *class_dev;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200818 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820 if (!parent)
821 return;
822
823 down(&parent->sem);
824 list_del_init(&class_intf->node);
825 if (class_intf->remove) {
826 list_for_each_entry(class_dev, &parent->children, node)
Dmitry Torokhovd8539d82005-09-15 02:01:36 -0500827 class_intf->remove(class_dev, class_intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 }
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200829 if (class_intf->remove_dev) {
830 list_for_each_entry(dev, &parent->devices, node)
831 class_intf->remove_dev(dev, class_intf);
832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 up(&parent->sem);
834
835 class_put(parent);
836}
837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838int __init classes_init(void)
839{
840 int retval;
841
842 retval = subsystem_register(&class_subsys);
843 if (retval)
844 return retval;
845
846 /* ick, this is ugly, the things we go through to keep from showing up
847 * in sysfs... */
Greg Kroah-Hartmane4bc1662007-09-26 11:12:00 -0700848 kset_init(&class_obj_subsys);
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700849 if (!class_obj_subsys.kobj.parent)
850 class_obj_subsys.kobj.parent = &class_obj_subsys.kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 return 0;
852}
853
854EXPORT_SYMBOL_GPL(class_create_file);
855EXPORT_SYMBOL_GPL(class_remove_file);
856EXPORT_SYMBOL_GPL(class_register);
857EXPORT_SYMBOL_GPL(class_unregister);
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800858EXPORT_SYMBOL_GPL(class_create);
859EXPORT_SYMBOL_GPL(class_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
861EXPORT_SYMBOL_GPL(class_device_register);
862EXPORT_SYMBOL_GPL(class_device_unregister);
863EXPORT_SYMBOL_GPL(class_device_initialize);
864EXPORT_SYMBOL_GPL(class_device_add);
865EXPORT_SYMBOL_GPL(class_device_del);
866EXPORT_SYMBOL_GPL(class_device_get);
867EXPORT_SYMBOL_GPL(class_device_put);
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800868EXPORT_SYMBOL_GPL(class_device_create);
869EXPORT_SYMBOL_GPL(class_device_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870EXPORT_SYMBOL_GPL(class_device_create_file);
871EXPORT_SYMBOL_GPL(class_device_remove_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
873EXPORT_SYMBOL_GPL(class_interface_register);
874EXPORT_SYMBOL_GPL(class_interface_unregister);