blob: 624b3316e938aebae6f4f27a0ea8a4366274d707 [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>
Kay Sieversedfaa7c2007-05-21 22:08:01 +020020#include <linux/genhd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "base.h"
22
23#define to_class_attr(_attr) container_of(_attr, struct class_attribute, attr)
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -070024#define to_class(obj) container_of(obj, struct class, subsys.kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26static ssize_t
27class_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
28{
29 struct class_attribute * class_attr = to_class_attr(attr);
30 struct class * dc = to_class(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -050031 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33 if (class_attr->show)
34 ret = class_attr->show(dc, buf);
35 return ret;
36}
37
38static ssize_t
39class_attr_store(struct kobject * kobj, struct attribute * attr,
40 const char * buf, size_t count)
41{
42 struct class_attribute * class_attr = to_class_attr(attr);
43 struct class * dc = to_class(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -050044 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46 if (class_attr->store)
47 ret = class_attr->store(dc, buf, count);
48 return ret;
49}
50
51static void class_release(struct kobject * kobj)
52{
53 struct class *class = to_class(kobj);
54
55 pr_debug("class '%s': release.\n", class->name);
56
57 if (class->class_release)
58 class->class_release(class);
59 else
60 pr_debug("class '%s' does not have a release() function, "
61 "be careful\n", class->name);
62}
63
64static struct sysfs_ops class_sysfs_ops = {
65 .show = class_attr_show,
66 .store = class_attr_store,
67};
68
Greg Kroah-Hartmanadc56802007-10-11 10:47:49 -060069static struct kobj_type class_ktype = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 .sysfs_ops = &class_sysfs_ops,
71 .release = class_release,
72};
73
74/* Hotplug events for classes go to the class_obj subsys */
Greg Kroah-Hartman443dbf92007-10-29 23:22:26 -050075static struct kset *class_kset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77
78int class_create_file(struct class * cls, const struct class_attribute * attr)
79{
80 int error;
81 if (cls) {
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -070082 error = sysfs_create_file(&cls->subsys.kobj, &attr->attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 } else
84 error = -EINVAL;
85 return error;
86}
87
88void class_remove_file(struct class * cls, const struct class_attribute * attr)
89{
90 if (cls)
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -070091 sysfs_remove_file(&cls->subsys.kobj, &attr->attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092}
93
Greg Kroah-Hartman17407572006-05-02 16:59:59 +020094static struct class *class_get(struct class *cls)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
96 if (cls)
Greg Kroah-Hartman1ef4cfa2007-09-12 15:06:57 -070097 return container_of(kset_get(&cls->subsys), struct class, subsys);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 return NULL;
99}
100
Greg Kroah-Hartman17407572006-05-02 16:59:59 +0200101static void class_put(struct class * cls)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700103 if (cls)
Greg Kroah-Hartman6e9d9302007-09-12 15:06:57 -0700104 kset_put(&cls->subsys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
106
107
108static int add_class_attrs(struct class * cls)
109{
110 int i;
111 int error = 0;
112
113 if (cls->class_attrs) {
114 for (i = 0; attr_name(cls->class_attrs[i]); i++) {
115 error = class_create_file(cls,&cls->class_attrs[i]);
116 if (error)
117 goto Err;
118 }
119 }
120 Done:
121 return error;
122 Err:
123 while (--i >= 0)
124 class_remove_file(cls,&cls->class_attrs[i]);
125 goto Done;
126}
127
128static void remove_class_attrs(struct class * cls)
129{
130 int i;
131
132 if (cls->class_attrs) {
133 for (i = 0; attr_name(cls->class_attrs[i]); i++)
134 class_remove_file(cls,&cls->class_attrs[i]);
135 }
136}
137
138int class_register(struct class * cls)
139{
140 int error;
141
142 pr_debug("device class '%s': registering\n", cls->name);
143
144 INIT_LIST_HEAD(&cls->children);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700145 INIT_LIST_HEAD(&cls->devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 INIT_LIST_HEAD(&cls->interfaces);
Kay Sievers86406242007-03-14 03:25:56 +0100147 kset_init(&cls->class_dirs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 init_MUTEX(&cls->sem);
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700149 error = kobject_set_name(&cls->subsys.kobj, "%s", cls->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 if (error)
151 return error;
152
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200153#ifdef CONFIG_SYSFS_DEPRECATED
154 /* let the block class directory show up in the root of sysfs */
155 if (cls != &block_class)
156 cls->subsys.kobj.kset = class_kset;
157#else
Greg Kroah-Hartman443dbf92007-10-29 23:22:26 -0500158 cls->subsys.kobj.kset = class_kset;
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200159#endif
Greg Kroah-Hartman3514fac2007-10-16 10:11:44 -0600160 cls->subsys.kobj.ktype = &class_ktype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Greg Kroah-Hartman2fb91132007-11-06 15:03:30 -0800162 error = kset_register(&cls->subsys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 if (!error) {
164 error = add_class_attrs(class_get(cls));
165 class_put(cls);
166 }
167 return error;
168}
169
170void class_unregister(struct class * cls)
171{
172 pr_debug("device class '%s': unregistering\n", cls->name);
173 remove_class_attrs(cls);
Greg Kroah-Hartman2fb91132007-11-06 15:03:30 -0800174 kset_unregister(&cls->subsys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
176
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800177static void class_create_release(struct class *cls)
178{
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700179 pr_debug("%s called for %s\n", __FUNCTION__, cls->name);
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800180 kfree(cls);
181}
182
183static void class_device_create_release(struct class_device *class_dev)
184{
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700185 pr_debug("%s called for %s\n", __FUNCTION__, class_dev->class_id);
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800186 kfree(class_dev);
187}
188
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700189/* needed to allow these devices to have parent class devices */
Kay Sievers312c0042005-11-16 09:00:00 +0100190static int class_device_create_uevent(struct class_device *class_dev,
Kay Sievers7eff2e72007-08-14 15:15:12 +0200191 struct kobj_uevent_env *env)
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700192{
193 pr_debug("%s called for %s\n", __FUNCTION__, class_dev->class_id);
194 return 0;
195}
196
gregkh@suse.de2fc68442005-03-23 10:02:56 -0800197/**
198 * class_create - create a struct class structure
199 * @owner: pointer to the module that is to "own" this struct class
200 * @name: pointer to a string for the name of this class.
201 *
202 * This is used to create a struct class pointer that can then be used
203 * in calls to class_device_create().
204 *
205 * Note, the pointer created here is to be destroyed when finished by
206 * making a call to class_destroy().
207 */
Miguel Ojeda Sandonisab7d7372006-09-13 15:34:05 +0200208struct class *class_create(struct module *owner, const char *name)
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800209{
210 struct class *cls;
211 int retval;
212
Jiri Slaby4aed0642005-09-13 01:25:01 -0700213 cls = kzalloc(sizeof(*cls), GFP_KERNEL);
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800214 if (!cls) {
215 retval = -ENOMEM;
216 goto error;
217 }
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800218
219 cls->name = name;
220 cls->owner = owner;
221 cls->class_release = class_create_release;
222 cls->release = class_device_create_release;
223
224 retval = class_register(cls);
225 if (retval)
226 goto error;
227
228 return cls;
229
230error:
231 kfree(cls);
232 return ERR_PTR(retval);
233}
234
gregkh@suse.de2fc68442005-03-23 10:02:56 -0800235/**
236 * class_destroy - destroys a struct class structure
Rolf Eike Beer92a0f862006-09-29 01:59:13 -0700237 * @cls: pointer to the struct class that is to be destroyed
gregkh@suse.de2fc68442005-03-23 10:02:56 -0800238 *
239 * Note, the pointer to be destroyed must have been created with a call
240 * to class_create().
241 */
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800242void class_destroy(struct class *cls)
243{
244 if ((cls == NULL) || (IS_ERR(cls)))
245 return;
246
247 class_unregister(cls);
248}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250/* Class Device Stuff */
251
252int class_device_create_file(struct class_device * class_dev,
253 const struct class_device_attribute * attr)
254{
255 int error = -EINVAL;
256 if (class_dev)
257 error = sysfs_create_file(&class_dev->kobj, &attr->attr);
258 return error;
259}
260
261void class_device_remove_file(struct class_device * class_dev,
262 const struct class_device_attribute * attr)
263{
264 if (class_dev)
265 sysfs_remove_file(&class_dev->kobj, &attr->attr);
266}
267
Greg Kroah-Hartmand919fd42007-10-31 12:51:29 -0700268int class_device_create_bin_file(struct class_device *class_dev,
269 struct bin_attribute *attr)
270{
271 int error = -EINVAL;
272 if (class_dev)
273 error = sysfs_create_bin_file(&class_dev->kobj, attr);
274 return error;
275}
276
277void class_device_remove_bin_file(struct class_device *class_dev,
278 struct bin_attribute *attr)
279{
280 if (class_dev)
281 sysfs_remove_bin_file(&class_dev->kobj, attr);
282}
283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284static ssize_t
285class_device_attr_show(struct kobject * kobj, struct attribute * attr,
286 char * buf)
287{
288 struct class_device_attribute * class_dev_attr = to_class_dev_attr(attr);
289 struct class_device * cd = to_class_dev(kobj);
290 ssize_t ret = 0;
291
292 if (class_dev_attr->show)
293 ret = class_dev_attr->show(cd, buf);
294 return ret;
295}
296
297static ssize_t
298class_device_attr_store(struct kobject * kobj, struct attribute * attr,
299 const char * buf, size_t count)
300{
301 struct class_device_attribute * class_dev_attr = to_class_dev_attr(attr);
302 struct class_device * cd = to_class_dev(kobj);
303 ssize_t ret = 0;
304
305 if (class_dev_attr->store)
306 ret = class_dev_attr->store(cd, buf, count);
307 return ret;
308}
309
310static struct sysfs_ops class_dev_sysfs_ops = {
311 .show = class_device_attr_show,
312 .store = class_device_attr_store,
313};
314
315static void class_dev_release(struct kobject * kobj)
316{
317 struct class_device *cd = to_class_dev(kobj);
318 struct class * cls = cd->class;
319
320 pr_debug("device class '%s': release.\n", cd->class_id);
321
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700322 if (cd->release)
323 cd->release(cd);
324 else if (cls->release)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 cls->release(cd);
326 else {
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700327 printk(KERN_ERR "Class Device '%s' does not have a release() function, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 "it is broken and must be fixed.\n",
329 cd->class_id);
330 WARN_ON(1);
331 }
332}
333
Greg Kroah-Hartmanadc56802007-10-11 10:47:49 -0600334static struct kobj_type class_device_ktype = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 .sysfs_ops = &class_dev_sysfs_ops,
336 .release = class_dev_release,
337};
338
Kay Sievers312c0042005-11-16 09:00:00 +0100339static int class_uevent_filter(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
341 struct kobj_type *ktype = get_ktype(kobj);
342
Greg Kroah-Hartmanadc56802007-10-11 10:47:49 -0600343 if (ktype == &class_device_ktype) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 struct class_device *class_dev = to_class_dev(kobj);
345 if (class_dev->class)
346 return 1;
347 }
348 return 0;
349}
350
Kay Sievers312c0042005-11-16 09:00:00 +0100351static const char *class_uevent_name(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
353 struct class_device *class_dev = to_class_dev(kobj);
354
355 return class_dev->class->name;
356}
357
Kay Sievers805fab42006-09-14 11:23:28 +0200358#ifdef CONFIG_SYSFS_DEPRECATED
359char *make_class_name(const char *name, struct kobject *kobj)
360{
361 char *class_name;
362 int size;
363
364 size = strlen(name) + strlen(kobject_name(kobj)) + 2;
365
366 class_name = kmalloc(size, GFP_KERNEL);
367 if (!class_name)
Cornelia Huckcb360bb2006-11-27 10:35:05 +0100368 return NULL;
Kay Sievers805fab42006-09-14 11:23:28 +0200369
370 strcpy(class_name, name);
371 strcat(class_name, ":");
372 strcat(class_name, kobject_name(kobj));
373 return class_name;
374}
375
Kay Sievers805fab42006-09-14 11:23:28 +0200376static int make_deprecated_class_device_links(struct class_device *class_dev)
377{
378 char *class_name;
379 int error;
380
381 if (!class_dev->dev)
382 return 0;
383
384 class_name = make_class_name(class_dev->class->name, &class_dev->kobj);
Cornelia Huckcb360bb2006-11-27 10:35:05 +0100385 if (class_name)
386 error = sysfs_create_link(&class_dev->dev->kobj,
387 &class_dev->kobj, class_name);
388 else
389 error = -ENOMEM;
Kay Sievers805fab42006-09-14 11:23:28 +0200390 kfree(class_name);
391 return error;
392}
393
394static void remove_deprecated_class_device_links(struct class_device *class_dev)
395{
396 char *class_name;
397
398 if (!class_dev->dev)
399 return;
400
401 class_name = make_class_name(class_dev->class->name, &class_dev->kobj);
Cornelia Huckcb360bb2006-11-27 10:35:05 +0100402 if (class_name)
403 sysfs_remove_link(&class_dev->dev->kobj, class_name);
Kay Sievers805fab42006-09-14 11:23:28 +0200404 kfree(class_name);
405}
406#else
Kay Sievers805fab42006-09-14 11:23:28 +0200407static inline int make_deprecated_class_device_links(struct class_device *cd)
408{ return 0; }
409static void remove_deprecated_class_device_links(struct class_device *cd)
410{ }
411#endif
412
Kay Sievers7eff2e72007-08-14 15:15:12 +0200413static int class_uevent(struct kset *kset, struct kobject *kobj,
414 struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
416 struct class_device *class_dev = to_class_dev(kobj);
Kay Sievers2c7afd12007-05-01 13:46:26 +0200417 struct device *dev = class_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 int retval = 0;
419
420 pr_debug("%s - name = %s\n", __FUNCTION__, class_dev->class_id);
421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 if (MAJOR(class_dev->devt)) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200423 add_uevent_var(env, "MAJOR=%u", MAJOR(class_dev->devt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Kay Sievers7eff2e72007-08-14 15:15:12 +0200425 add_uevent_var(env, "MINOR=%u", MINOR(class_dev->devt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 }
427
Kay Sievers2c7afd12007-05-01 13:46:26 +0200428 if (dev) {
429 const char *path = kobject_get_path(&dev->kobj, GFP_KERNEL);
430 if (path) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200431 add_uevent_var(env, "PHYSDEVPATH=%s", path);
Kay Sievers2c7afd12007-05-01 13:46:26 +0200432 kfree(path);
433 }
434
435 if (dev->bus)
Kay Sievers7eff2e72007-08-14 15:15:12 +0200436 add_uevent_var(env, "PHYSDEVBUS=%s", dev->bus->name);
Kay Sievers2c7afd12007-05-01 13:46:26 +0200437
438 if (dev->driver)
Kay Sievers7eff2e72007-08-14 15:15:12 +0200439 add_uevent_var(env, "PHYSDEVDRIVER=%s", dev->driver->name);
Kay Sievers2c7afd12007-05-01 13:46:26 +0200440 }
441
Kay Sievers312c0042005-11-16 09:00:00 +0100442 if (class_dev->uevent) {
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700443 /* have the class device specific function add its stuff */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200444 retval = class_dev->uevent(class_dev, env);
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700445 if (retval)
Kay Sievers312c0042005-11-16 09:00:00 +0100446 pr_debug("class_dev->uevent() returned %d\n", retval);
447 } else if (class_dev->class->uevent) {
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700448 /* have the class specific function add its stuff */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200449 retval = class_dev->class->uevent(class_dev, env);
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700450 if (retval)
Kay Sievers312c0042005-11-16 09:00:00 +0100451 pr_debug("class->uevent() returned %d\n", retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 }
453
454 return retval;
455}
456
Kay Sievers312c0042005-11-16 09:00:00 +0100457static struct kset_uevent_ops class_uevent_ops = {
458 .filter = class_uevent_filter,
459 .name = class_uevent_name,
460 .uevent = class_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461};
462
Greg Kroah-Hartman5c03c7a2007-11-02 16:19:59 -0700463/*
464 * DO NOT copy how this is created, kset_create_and_add() should be
465 * called, but this is a hold-over from the old-way and will be deleted
466 * entirely soon.
467 */
468static struct kset class_obj_subsys = {
469 .kobj = { .k_name = "class_obj", },
470 .uevent_ops = &class_uevent_ops,
471};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473static int class_device_add_attrs(struct class_device * cd)
474{
475 int i;
476 int error = 0;
477 struct class * cls = cd->class;
478
479 if (cls->class_dev_attrs) {
480 for (i = 0; attr_name(cls->class_dev_attrs[i]); i++) {
481 error = class_device_create_file(cd,
482 &cls->class_dev_attrs[i]);
483 if (error)
484 goto Err;
485 }
486 }
487 Done:
488 return error;
489 Err:
490 while (--i >= 0)
491 class_device_remove_file(cd,&cls->class_dev_attrs[i]);
492 goto Done;
493}
494
495static void class_device_remove_attrs(struct class_device * cd)
496{
497 int i;
498 struct class * cls = cd->class;
499
500 if (cls->class_dev_attrs) {
501 for (i = 0; attr_name(cls->class_dev_attrs[i]); i++)
502 class_device_remove_file(cd,&cls->class_dev_attrs[i]);
503 }
504}
505
Stephen Hemminger14982212006-05-06 17:55:11 -0700506static int class_device_add_groups(struct class_device * cd)
507{
508 int i;
509 int error = 0;
510
511 if (cd->groups) {
512 for (i = 0; cd->groups[i]; i++) {
513 error = sysfs_create_group(&cd->kobj, cd->groups[i]);
514 if (error) {
515 while (--i >= 0)
516 sysfs_remove_group(&cd->kobj, cd->groups[i]);
517 goto out;
518 }
519 }
520 }
521out:
522 return error;
523}
524
525static void class_device_remove_groups(struct class_device * cd)
526{
527 int i;
528 if (cd->groups) {
529 for (i = 0; cd->groups[i]; i++) {
530 sysfs_remove_group(&cd->kobj, cd->groups[i]);
531 }
532 }
533}
534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535static ssize_t show_dev(struct class_device *class_dev, char *buf)
536{
537 return print_dev_t(buf, class_dev->devt);
538}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Tejun Heoad6a1e12007-06-14 03:45:17 +0900540static struct class_device_attribute class_devt_attr =
541 __ATTR(dev, S_IRUGO, show_dev, NULL);
542
Kay Sieversa7fd6702005-10-01 14:49:43 +0200543static ssize_t store_uevent(struct class_device *class_dev,
544 const char *buf, size_t count)
545{
Kay Sievers312c0042005-11-16 09:00:00 +0100546 kobject_uevent(&class_dev->kobj, KOBJ_ADD);
Kay Sieversa7fd6702005-10-01 14:49:43 +0200547 return count;
548}
549
Tejun Heoad6a1e12007-06-14 03:45:17 +0900550static struct class_device_attribute class_uevent_attr =
551 __ATTR(uevent, S_IWUSR, NULL, store_uevent);
552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553void class_device_initialize(struct class_device *class_dev)
554{
Greg Kroah-Hartman3514fac2007-10-16 10:11:44 -0600555 class_dev->kobj.kset = &class_obj_subsys;
Greg Kroah-Hartman60728d62007-12-17 23:05:35 -0700556 kobject_init_ng(&class_dev->kobj, &class_device_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 INIT_LIST_HEAD(&class_dev->node);
558}
559
560int class_device_add(struct class_device *class_dev)
561{
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700562 struct class *parent_class = NULL;
563 struct class_device *parent_class_dev = NULL;
564 struct class_interface *class_intf;
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700565 int error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567 class_dev = class_device_get(class_dev);
568 if (!class_dev)
569 return -EINVAL;
570
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700571 if (!strlen(class_dev->class_id))
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700572 goto out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700574 parent_class = class_get(class_dev->class);
575 if (!parent_class)
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700576 goto out1;
577
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700578 parent_class_dev = class_device_get(class_dev->parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 pr_debug("CLASS: registering class device: ID = '%s'\n",
581 class_dev->class_id);
582
583 /* first, register with generic layer. */
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700584 if (parent_class_dev)
585 class_dev->kobj.parent = &parent_class_dev->kobj;
586 else
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700587 class_dev->kobj.parent = &parent_class->subsys.kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Greg Kroah-Hartman60728d62007-12-17 23:05:35 -0700589 error = kobject_add_ng(&class_dev->kobj, class_dev->kobj.parent,
590 "%s", class_dev->class_id);
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700591 if (error)
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700592 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800594 /* add the needed attributes to this device */
Cornelia Huckf0e17612006-09-22 11:37:00 +0200595 error = sysfs_create_link(&class_dev->kobj,
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700596 &parent_class->subsys.kobj, "subsystem");
Cornelia Huckf0e17612006-09-22 11:37:00 +0200597 if (error)
598 goto out3;
Tejun Heoad6a1e12007-06-14 03:45:17 +0900599
600 error = class_device_create_file(class_dev, &class_uevent_attr);
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700601 if (error)
602 goto out3;
Kay Sieversa7fd6702005-10-01 14:49:43 +0200603
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800604 if (MAJOR(class_dev->devt)) {
Tejun Heoad6a1e12007-06-14 03:45:17 +0900605 error = class_device_create_file(class_dev, &class_devt_attr);
606 if (error)
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700607 goto out4;
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800608 }
609
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700610 error = class_device_add_attrs(class_dev);
611 if (error)
612 goto out5;
613
Dmitry Torokhov76d1ce02005-07-10 01:21:24 -0500614 if (class_dev->dev) {
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700615 error = sysfs_create_link(&class_dev->kobj,
616 &class_dev->dev->kobj, "device");
617 if (error)
618 goto out6;
Dmitry Torokhov76d1ce02005-07-10 01:21:24 -0500619 }
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800620
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700621 error = class_device_add_groups(class_dev);
622 if (error)
Kay Sievers805fab42006-09-14 11:23:28 +0200623 goto out7;
624
625 error = make_deprecated_class_device_links(class_dev);
626 if (error)
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700627 goto out8;
Stephen Hemminger14982212006-05-06 17:55:11 -0700628
Kay Sievers312c0042005-11-16 09:00:00 +0100629 kobject_uevent(&class_dev->kobj, KOBJ_ADD);
Dmitry Torokhovdbe90352005-09-15 02:01:37 -0500630
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800631 /* notify any interfaces this device is now here */
Jayachandran Ca1438892006-04-03 12:31:53 -0700632 down(&parent_class->sem);
633 list_add_tail(&class_dev->node, &parent_class->children);
634 list_for_each_entry(class_intf, &parent_class->interfaces, node) {
635 if (class_intf->add)
636 class_intf->add(class_dev, class_intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 }
Jayachandran Ca1438892006-04-03 12:31:53 -0700638 up(&parent_class->sem);
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800639
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700640 goto out1;
641
642 out8:
Kay Sievers805fab42006-09-14 11:23:28 +0200643 class_device_remove_groups(class_dev);
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700644 out7:
645 if (class_dev->dev)
646 sysfs_remove_link(&class_dev->kobj, "device");
647 out6:
648 class_device_remove_attrs(class_dev);
649 out5:
Tejun Heoad6a1e12007-06-14 03:45:17 +0900650 if (MAJOR(class_dev->devt))
651 class_device_remove_file(class_dev, &class_devt_attr);
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700652 out4:
Tejun Heoad6a1e12007-06-14 03:45:17 +0900653 class_device_remove_file(class_dev, &class_uevent_attr);
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700654 out3:
655 kobject_del(&class_dev->kobj);
656 out2:
657 if(parent_class_dev)
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700658 class_device_put(parent_class_dev);
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700659 class_put(parent_class);
660 out1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 class_device_put(class_dev);
662 return error;
663}
664
665int class_device_register(struct class_device *class_dev)
666{
667 class_device_initialize(class_dev);
668 return class_device_add(class_dev);
669}
670
gregkh@suse.de2fc68442005-03-23 10:02:56 -0800671/**
672 * class_device_create - creates a class device and registers it with sysfs
Rolf Eike Beer92a0f862006-09-29 01:59:13 -0700673 * @cls: pointer to the struct class that this device should be registered to.
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700674 * @parent: pointer to the parent struct class_device of this new device, if any.
Rolf Eike Beer92a0f862006-09-29 01:59:13 -0700675 * @devt: the dev_t for the char device to be added.
gregkh@suse.de2fc68442005-03-23 10:02:56 -0800676 * @device: a pointer to a struct device that is assiociated with this class device.
677 * @fmt: string for the class device's name
678 *
679 * This function can be used by char device classes. A struct
680 * class_device will be created in sysfs, registered to the specified
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700681 * class.
682 * A "dev" file will be created, showing the dev_t for the device, if
683 * the dev_t is not 0,0.
684 * If a pointer to a parent struct class_device is passed in, the newly
685 * created struct class_device will be a child of that device in sysfs.
686 * The pointer to the struct class_device will be returned from the
687 * call. Any further sysfs files that might be required can be created
688 * using this pointer.
gregkh@suse.de2fc68442005-03-23 10:02:56 -0800689 *
690 * Note: the struct class passed to this function must have previously
691 * been created with a call to class_create().
692 */
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700693struct class_device *class_device_create(struct class *cls,
694 struct class_device *parent,
695 dev_t devt,
Dmitry Torokhovddd5d352006-08-10 01:18:18 -0400696 struct device *device,
697 const char *fmt, ...)
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800698{
699 va_list args;
700 struct class_device *class_dev = NULL;
701 int retval = -ENODEV;
702
703 if (cls == NULL || IS_ERR(cls))
704 goto error;
705
Jiri Slaby4aed0642005-09-13 01:25:01 -0700706 class_dev = kzalloc(sizeof(*class_dev), GFP_KERNEL);
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800707 if (!class_dev) {
708 retval = -ENOMEM;
709 goto error;
710 }
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800711
712 class_dev->devt = devt;
713 class_dev->dev = device;
714 class_dev->class = cls;
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700715 class_dev->parent = parent;
716 class_dev->release = class_device_create_release;
Kay Sievers312c0042005-11-16 09:00:00 +0100717 class_dev->uevent = class_device_create_uevent;
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800718
719 va_start(args, fmt);
720 vsnprintf(class_dev->class_id, BUS_ID_SIZE, fmt, args);
721 va_end(args);
722 retval = class_device_register(class_dev);
723 if (retval)
724 goto error;
725
726 return class_dev;
727
728error:
729 kfree(class_dev);
730 return ERR_PTR(retval);
731}
732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733void class_device_del(struct class_device *class_dev)
734{
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700735 struct class *parent_class = class_dev->class;
736 struct class_device *parent_device = class_dev->parent;
737 struct class_interface *class_intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700739 if (parent_class) {
740 down(&parent_class->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 list_del_init(&class_dev->node);
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700742 list_for_each_entry(class_intf, &parent_class->interfaces, node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 if (class_intf->remove)
Dmitry Torokhovd8539d82005-09-15 02:01:36 -0500744 class_intf->remove(class_dev, class_intf);
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700745 up(&parent_class->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 }
747
Dmitry Torokhov76d1ce02005-07-10 01:21:24 -0500748 if (class_dev->dev) {
Kay Sievers805fab42006-09-14 11:23:28 +0200749 remove_deprecated_class_device_links(class_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 sysfs_remove_link(&class_dev->kobj, "device");
Dmitry Torokhov76d1ce02005-07-10 01:21:24 -0500751 }
Kay Sieversb9d9c822006-06-15 15:31:56 +0200752 sysfs_remove_link(&class_dev->kobj, "subsystem");
Tejun Heoad6a1e12007-06-14 03:45:17 +0900753 class_device_remove_file(class_dev, &class_uevent_attr);
754 if (MAJOR(class_dev->devt))
755 class_device_remove_file(class_dev, &class_devt_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 class_device_remove_attrs(class_dev);
Stephen Hemminger14982212006-05-06 17:55:11 -0700757 class_device_remove_groups(class_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Kay Sievers312c0042005-11-16 09:00:00 +0100759 kobject_uevent(&class_dev->kobj, KOBJ_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 kobject_del(&class_dev->kobj);
761
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700762 class_device_put(parent_device);
763 class_put(parent_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764}
765
766void class_device_unregister(struct class_device *class_dev)
767{
768 pr_debug("CLASS: Unregistering class device. ID = '%s'\n",
769 class_dev->class_id);
770 class_device_del(class_dev);
771 class_device_put(class_dev);
772}
773
gregkh@suse.de2fc68442005-03-23 10:02:56 -0800774/**
775 * class_device_destroy - removes a class device that was created with class_device_create()
776 * @cls: the pointer to the struct class that this device was registered * with.
Rolf Eike Beer92a0f862006-09-29 01:59:13 -0700777 * @devt: the dev_t of the device that was previously registered.
gregkh@suse.de2fc68442005-03-23 10:02:56 -0800778 *
779 * This call unregisters and cleans up a class device that was created with a
780 * call to class_device_create()
781 */
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800782void class_device_destroy(struct class *cls, dev_t devt)
783{
784 struct class_device *class_dev = NULL;
785 struct class_device *class_dev_tmp;
786
787 down(&cls->sem);
788 list_for_each_entry(class_dev_tmp, &cls->children, node) {
789 if (class_dev_tmp->devt == devt) {
790 class_dev = class_dev_tmp;
791 break;
792 }
793 }
794 up(&cls->sem);
795
796 if (class_dev)
797 class_device_unregister(class_dev);
798}
799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800struct class_device * class_device_get(struct class_device *class_dev)
801{
802 if (class_dev)
803 return to_class_dev(kobject_get(&class_dev->kobj));
804 return NULL;
805}
806
807void class_device_put(struct class_device *class_dev)
808{
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700809 if (class_dev)
810 kobject_put(&class_dev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811}
812
813
814int class_interface_register(struct class_interface *class_intf)
815{
816 struct class *parent;
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 (!class_intf || !class_intf->class)
821 return -ENODEV;
822
823 parent = class_get(class_intf->class);
824 if (!parent)
825 return -EINVAL;
826
827 down(&parent->sem);
828 list_add_tail(&class_intf->node, &parent->interfaces);
829 if (class_intf->add) {
830 list_for_each_entry(class_dev, &parent->children, node)
Dmitry Torokhovd8539d82005-09-15 02:01:36 -0500831 class_intf->add(class_dev, class_intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 }
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200833 if (class_intf->add_dev) {
834 list_for_each_entry(dev, &parent->devices, node)
835 class_intf->add_dev(dev, class_intf);
836 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 up(&parent->sem);
838
839 return 0;
840}
841
842void class_interface_unregister(struct class_interface *class_intf)
843{
844 struct class * parent = class_intf->class;
845 struct class_device *class_dev;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200846 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
848 if (!parent)
849 return;
850
851 down(&parent->sem);
852 list_del_init(&class_intf->node);
853 if (class_intf->remove) {
854 list_for_each_entry(class_dev, &parent->children, node)
Dmitry Torokhovd8539d82005-09-15 02:01:36 -0500855 class_intf->remove(class_dev, class_intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 }
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200857 if (class_intf->remove_dev) {
858 list_for_each_entry(dev, &parent->devices, node)
859 class_intf->remove_dev(dev, class_intf);
860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 up(&parent->sem);
862
863 class_put(parent);
864}
865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866int __init classes_init(void)
867{
Greg Kroah-Hartman443dbf92007-10-29 23:22:26 -0500868 class_kset = kset_create_and_add("class", NULL, NULL);
869 if (!class_kset)
870 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
872 /* ick, this is ugly, the things we go through to keep from showing up
873 * in sysfs... */
Greg Kroah-Hartmane4bc1662007-09-26 11:12:00 -0700874 kset_init(&class_obj_subsys);
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700875 if (!class_obj_subsys.kobj.parent)
876 class_obj_subsys.kobj.parent = &class_obj_subsys.kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 return 0;
878}
879
880EXPORT_SYMBOL_GPL(class_create_file);
881EXPORT_SYMBOL_GPL(class_remove_file);
882EXPORT_SYMBOL_GPL(class_register);
883EXPORT_SYMBOL_GPL(class_unregister);
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800884EXPORT_SYMBOL_GPL(class_create);
885EXPORT_SYMBOL_GPL(class_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
887EXPORT_SYMBOL_GPL(class_device_register);
888EXPORT_SYMBOL_GPL(class_device_unregister);
889EXPORT_SYMBOL_GPL(class_device_initialize);
890EXPORT_SYMBOL_GPL(class_device_add);
891EXPORT_SYMBOL_GPL(class_device_del);
892EXPORT_SYMBOL_GPL(class_device_get);
893EXPORT_SYMBOL_GPL(class_device_put);
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800894EXPORT_SYMBOL_GPL(class_device_create);
895EXPORT_SYMBOL_GPL(class_device_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896EXPORT_SYMBOL_GPL(class_device_create_file);
897EXPORT_SYMBOL_GPL(class_device_remove_file);
Greg Kroah-Hartmand919fd42007-10-31 12:51:29 -0700898EXPORT_SYMBOL_GPL(class_device_create_bin_file);
899EXPORT_SYMBOL_GPL(class_device_remove_bin_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
901EXPORT_SYMBOL_GPL(class_interface_register);
902EXPORT_SYMBOL_GPL(class_interface_unregister);