blob: ba6745b0fd2fe186505f582b2bb8cb6a0a0a04cf [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-Hartman443dbf92007-10-29 23:22:26 -050074static struct kset *class_kset;
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-Hartman443dbf92007-10-29 23:22:26 -0500152 cls->subsys.kobj.kset = class_kset;
Greg Kroah-Hartman3514fac2007-10-16 10:11:44 -0600153 cls->subsys.kobj.ktype = &class_ktype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Greg Kroah-Hartman2fb91132007-11-06 15:03:30 -0800155 error = kset_register(&cls->subsys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 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);
Greg Kroah-Hartman2fb91132007-11-06 15:03:30 -0800167 kset_unregister(&cls->subsys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168}
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,
Kay Sievers7eff2e72007-08-14 15:15:12 +0200184 struct kobj_uevent_env *env)
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700185{
186 pr_debug("%s called for %s\n", __FUNCTION__, class_dev->class_id);
187 return 0;
188}
189
gregkh@suse.de2fc68442005-03-23 10:02:56 -0800190/**
191 * class_create - create a struct class structure
192 * @owner: pointer to the module that is to "own" this struct class
193 * @name: pointer to a string for the name of this class.
194 *
195 * This is used to create a struct class pointer that can then be used
196 * in calls to class_device_create().
197 *
198 * Note, the pointer created here is to be destroyed when finished by
199 * making a call to class_destroy().
200 */
Miguel Ojeda Sandonisab7d7372006-09-13 15:34:05 +0200201struct class *class_create(struct module *owner, const char *name)
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800202{
203 struct class *cls;
204 int retval;
205
Jiri Slaby4aed0642005-09-13 01:25:01 -0700206 cls = kzalloc(sizeof(*cls), GFP_KERNEL);
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800207 if (!cls) {
208 retval = -ENOMEM;
209 goto error;
210 }
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800211
212 cls->name = name;
213 cls->owner = owner;
214 cls->class_release = class_create_release;
215 cls->release = class_device_create_release;
216
217 retval = class_register(cls);
218 if (retval)
219 goto error;
220
221 return cls;
222
223error:
224 kfree(cls);
225 return ERR_PTR(retval);
226}
227
gregkh@suse.de2fc68442005-03-23 10:02:56 -0800228/**
229 * class_destroy - destroys a struct class structure
Rolf Eike Beer92a0f862006-09-29 01:59:13 -0700230 * @cls: pointer to the struct class that is to be destroyed
gregkh@suse.de2fc68442005-03-23 10:02:56 -0800231 *
232 * Note, the pointer to be destroyed must have been created with a call
233 * to class_create().
234 */
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800235void class_destroy(struct class *cls)
236{
237 if ((cls == NULL) || (IS_ERR(cls)))
238 return;
239
240 class_unregister(cls);
241}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243/* Class Device Stuff */
244
245int class_device_create_file(struct class_device * class_dev,
246 const struct class_device_attribute * attr)
247{
248 int error = -EINVAL;
249 if (class_dev)
250 error = sysfs_create_file(&class_dev->kobj, &attr->attr);
251 return error;
252}
253
254void class_device_remove_file(struct class_device * class_dev,
255 const struct class_device_attribute * attr)
256{
257 if (class_dev)
258 sysfs_remove_file(&class_dev->kobj, &attr->attr);
259}
260
Greg Kroah-Hartmand919fd42007-10-31 12:51:29 -0700261int class_device_create_bin_file(struct class_device *class_dev,
262 struct bin_attribute *attr)
263{
264 int error = -EINVAL;
265 if (class_dev)
266 error = sysfs_create_bin_file(&class_dev->kobj, attr);
267 return error;
268}
269
270void class_device_remove_bin_file(struct class_device *class_dev,
271 struct bin_attribute *attr)
272{
273 if (class_dev)
274 sysfs_remove_bin_file(&class_dev->kobj, attr);
275}
276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277static ssize_t
278class_device_attr_show(struct kobject * kobj, struct attribute * attr,
279 char * buf)
280{
281 struct class_device_attribute * class_dev_attr = to_class_dev_attr(attr);
282 struct class_device * cd = to_class_dev(kobj);
283 ssize_t ret = 0;
284
285 if (class_dev_attr->show)
286 ret = class_dev_attr->show(cd, buf);
287 return ret;
288}
289
290static ssize_t
291class_device_attr_store(struct kobject * kobj, struct attribute * attr,
292 const char * buf, size_t count)
293{
294 struct class_device_attribute * class_dev_attr = to_class_dev_attr(attr);
295 struct class_device * cd = to_class_dev(kobj);
296 ssize_t ret = 0;
297
298 if (class_dev_attr->store)
299 ret = class_dev_attr->store(cd, buf, count);
300 return ret;
301}
302
303static struct sysfs_ops class_dev_sysfs_ops = {
304 .show = class_device_attr_show,
305 .store = class_device_attr_store,
306};
307
308static void class_dev_release(struct kobject * kobj)
309{
310 struct class_device *cd = to_class_dev(kobj);
311 struct class * cls = cd->class;
312
313 pr_debug("device class '%s': release.\n", cd->class_id);
314
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700315 if (cd->release)
316 cd->release(cd);
317 else if (cls->release)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 cls->release(cd);
319 else {
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700320 printk(KERN_ERR "Class Device '%s' does not have a release() function, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 "it is broken and must be fixed.\n",
322 cd->class_id);
323 WARN_ON(1);
324 }
325}
326
Greg Kroah-Hartmanadc56802007-10-11 10:47:49 -0600327static struct kobj_type class_device_ktype = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 .sysfs_ops = &class_dev_sysfs_ops,
329 .release = class_dev_release,
330};
331
Kay Sievers312c0042005-11-16 09:00:00 +0100332static int class_uevent_filter(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
334 struct kobj_type *ktype = get_ktype(kobj);
335
Greg Kroah-Hartmanadc56802007-10-11 10:47:49 -0600336 if (ktype == &class_device_ktype) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 struct class_device *class_dev = to_class_dev(kobj);
338 if (class_dev->class)
339 return 1;
340 }
341 return 0;
342}
343
Kay Sievers312c0042005-11-16 09:00:00 +0100344static const char *class_uevent_name(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
346 struct class_device *class_dev = to_class_dev(kobj);
347
348 return class_dev->class->name;
349}
350
Kay Sievers805fab42006-09-14 11:23:28 +0200351#ifdef CONFIG_SYSFS_DEPRECATED
352char *make_class_name(const char *name, struct kobject *kobj)
353{
354 char *class_name;
355 int size;
356
357 size = strlen(name) + strlen(kobject_name(kobj)) + 2;
358
359 class_name = kmalloc(size, GFP_KERNEL);
360 if (!class_name)
Cornelia Huckcb360bb2006-11-27 10:35:05 +0100361 return NULL;
Kay Sievers805fab42006-09-14 11:23:28 +0200362
363 strcpy(class_name, name);
364 strcat(class_name, ":");
365 strcat(class_name, kobject_name(kobj));
366 return class_name;
367}
368
Kay Sievers805fab42006-09-14 11:23:28 +0200369static int make_deprecated_class_device_links(struct class_device *class_dev)
370{
371 char *class_name;
372 int error;
373
374 if (!class_dev->dev)
375 return 0;
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 error = sysfs_create_link(&class_dev->dev->kobj,
380 &class_dev->kobj, class_name);
381 else
382 error = -ENOMEM;
Kay Sievers805fab42006-09-14 11:23:28 +0200383 kfree(class_name);
384 return error;
385}
386
387static void remove_deprecated_class_device_links(struct class_device *class_dev)
388{
389 char *class_name;
390
391 if (!class_dev->dev)
392 return;
393
394 class_name = make_class_name(class_dev->class->name, &class_dev->kobj);
Cornelia Huckcb360bb2006-11-27 10:35:05 +0100395 if (class_name)
396 sysfs_remove_link(&class_dev->dev->kobj, class_name);
Kay Sievers805fab42006-09-14 11:23:28 +0200397 kfree(class_name);
398}
399#else
Kay Sievers805fab42006-09-14 11:23:28 +0200400static inline int make_deprecated_class_device_links(struct class_device *cd)
401{ return 0; }
402static void remove_deprecated_class_device_links(struct class_device *cd)
403{ }
404#endif
405
Kay Sievers7eff2e72007-08-14 15:15:12 +0200406static int class_uevent(struct kset *kset, struct kobject *kobj,
407 struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
409 struct class_device *class_dev = to_class_dev(kobj);
Kay Sievers2c7afd12007-05-01 13:46:26 +0200410 struct device *dev = class_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 int retval = 0;
412
413 pr_debug("%s - name = %s\n", __FUNCTION__, class_dev->class_id);
414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 if (MAJOR(class_dev->devt)) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200416 add_uevent_var(env, "MAJOR=%u", MAJOR(class_dev->devt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Kay Sievers7eff2e72007-08-14 15:15:12 +0200418 add_uevent_var(env, "MINOR=%u", MINOR(class_dev->devt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 }
420
Kay Sievers2c7afd12007-05-01 13:46:26 +0200421 if (dev) {
422 const char *path = kobject_get_path(&dev->kobj, GFP_KERNEL);
423 if (path) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200424 add_uevent_var(env, "PHYSDEVPATH=%s", path);
Kay Sievers2c7afd12007-05-01 13:46:26 +0200425 kfree(path);
426 }
427
428 if (dev->bus)
Kay Sievers7eff2e72007-08-14 15:15:12 +0200429 add_uevent_var(env, "PHYSDEVBUS=%s", dev->bus->name);
Kay Sievers2c7afd12007-05-01 13:46:26 +0200430
431 if (dev->driver)
Kay Sievers7eff2e72007-08-14 15:15:12 +0200432 add_uevent_var(env, "PHYSDEVDRIVER=%s", dev->driver->name);
Kay Sievers2c7afd12007-05-01 13:46:26 +0200433 }
434
Kay Sievers312c0042005-11-16 09:00:00 +0100435 if (class_dev->uevent) {
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700436 /* have the class device specific function add its stuff */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200437 retval = class_dev->uevent(class_dev, env);
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700438 if (retval)
Kay Sievers312c0042005-11-16 09:00:00 +0100439 pr_debug("class_dev->uevent() returned %d\n", retval);
440 } else if (class_dev->class->uevent) {
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700441 /* have the class specific function add its stuff */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200442 retval = class_dev->class->uevent(class_dev, env);
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700443 if (retval)
Kay Sievers312c0042005-11-16 09:00:00 +0100444 pr_debug("class->uevent() returned %d\n", retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 }
446
447 return retval;
448}
449
Kay Sievers312c0042005-11-16 09:00:00 +0100450static struct kset_uevent_ops class_uevent_ops = {
451 .filter = class_uevent_filter,
452 .name = class_uevent_name,
453 .uevent = class_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454};
455
Greg Kroah-Hartman5c03c7a2007-11-02 16:19:59 -0700456/*
457 * DO NOT copy how this is created, kset_create_and_add() should be
458 * called, but this is a hold-over from the old-way and will be deleted
459 * entirely soon.
460 */
461static struct kset class_obj_subsys = {
462 .kobj = { .k_name = "class_obj", },
463 .uevent_ops = &class_uevent_ops,
464};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466static int class_device_add_attrs(struct class_device * cd)
467{
468 int i;
469 int error = 0;
470 struct class * cls = cd->class;
471
472 if (cls->class_dev_attrs) {
473 for (i = 0; attr_name(cls->class_dev_attrs[i]); i++) {
474 error = class_device_create_file(cd,
475 &cls->class_dev_attrs[i]);
476 if (error)
477 goto Err;
478 }
479 }
480 Done:
481 return error;
482 Err:
483 while (--i >= 0)
484 class_device_remove_file(cd,&cls->class_dev_attrs[i]);
485 goto Done;
486}
487
488static void class_device_remove_attrs(struct class_device * cd)
489{
490 int i;
491 struct class * cls = cd->class;
492
493 if (cls->class_dev_attrs) {
494 for (i = 0; attr_name(cls->class_dev_attrs[i]); i++)
495 class_device_remove_file(cd,&cls->class_dev_attrs[i]);
496 }
497}
498
Stephen Hemminger14982212006-05-06 17:55:11 -0700499static int class_device_add_groups(struct class_device * cd)
500{
501 int i;
502 int error = 0;
503
504 if (cd->groups) {
505 for (i = 0; cd->groups[i]; i++) {
506 error = sysfs_create_group(&cd->kobj, cd->groups[i]);
507 if (error) {
508 while (--i >= 0)
509 sysfs_remove_group(&cd->kobj, cd->groups[i]);
510 goto out;
511 }
512 }
513 }
514out:
515 return error;
516}
517
518static void class_device_remove_groups(struct class_device * cd)
519{
520 int i;
521 if (cd->groups) {
522 for (i = 0; cd->groups[i]; i++) {
523 sysfs_remove_group(&cd->kobj, cd->groups[i]);
524 }
525 }
526}
527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528static ssize_t show_dev(struct class_device *class_dev, char *buf)
529{
530 return print_dev_t(buf, class_dev->devt);
531}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Tejun Heoad6a1e12007-06-14 03:45:17 +0900533static struct class_device_attribute class_devt_attr =
534 __ATTR(dev, S_IRUGO, show_dev, NULL);
535
Kay Sieversa7fd6702005-10-01 14:49:43 +0200536static ssize_t store_uevent(struct class_device *class_dev,
537 const char *buf, size_t count)
538{
Kay Sievers312c0042005-11-16 09:00:00 +0100539 kobject_uevent(&class_dev->kobj, KOBJ_ADD);
Kay Sieversa7fd6702005-10-01 14:49:43 +0200540 return count;
541}
542
Tejun Heoad6a1e12007-06-14 03:45:17 +0900543static struct class_device_attribute class_uevent_attr =
544 __ATTR(uevent, S_IWUSR, NULL, store_uevent);
545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546void class_device_initialize(struct class_device *class_dev)
547{
Greg Kroah-Hartman3514fac2007-10-16 10:11:44 -0600548 class_dev->kobj.kset = &class_obj_subsys;
Greg Kroah-Hartman60728d62007-12-17 23:05:35 -0700549 kobject_init_ng(&class_dev->kobj, &class_device_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 INIT_LIST_HEAD(&class_dev->node);
551}
552
553int class_device_add(struct class_device *class_dev)
554{
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700555 struct class *parent_class = NULL;
556 struct class_device *parent_class_dev = NULL;
557 struct class_interface *class_intf;
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700558 int error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 class_dev = class_device_get(class_dev);
561 if (!class_dev)
562 return -EINVAL;
563
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700564 if (!strlen(class_dev->class_id))
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700565 goto out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700567 parent_class = class_get(class_dev->class);
568 if (!parent_class)
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700569 goto out1;
570
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700571 parent_class_dev = class_device_get(class_dev->parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573 pr_debug("CLASS: registering class device: ID = '%s'\n",
574 class_dev->class_id);
575
576 /* first, register with generic layer. */
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700577 if (parent_class_dev)
578 class_dev->kobj.parent = &parent_class_dev->kobj;
579 else
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700580 class_dev->kobj.parent = &parent_class->subsys.kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Greg Kroah-Hartman60728d62007-12-17 23:05:35 -0700582 error = kobject_add_ng(&class_dev->kobj, class_dev->kobj.parent,
583 "%s", class_dev->class_id);
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700584 if (error)
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700585 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800587 /* add the needed attributes to this device */
Cornelia Huckf0e17612006-09-22 11:37:00 +0200588 error = sysfs_create_link(&class_dev->kobj,
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700589 &parent_class->subsys.kobj, "subsystem");
Cornelia Huckf0e17612006-09-22 11:37:00 +0200590 if (error)
591 goto out3;
Tejun Heoad6a1e12007-06-14 03:45:17 +0900592
593 error = class_device_create_file(class_dev, &class_uevent_attr);
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700594 if (error)
595 goto out3;
Kay Sieversa7fd6702005-10-01 14:49:43 +0200596
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800597 if (MAJOR(class_dev->devt)) {
Tejun Heoad6a1e12007-06-14 03:45:17 +0900598 error = class_device_create_file(class_dev, &class_devt_attr);
599 if (error)
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700600 goto out4;
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800601 }
602
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700603 error = class_device_add_attrs(class_dev);
604 if (error)
605 goto out5;
606
Dmitry Torokhov76d1ce02005-07-10 01:21:24 -0500607 if (class_dev->dev) {
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700608 error = sysfs_create_link(&class_dev->kobj,
609 &class_dev->dev->kobj, "device");
610 if (error)
611 goto out6;
Dmitry Torokhov76d1ce02005-07-10 01:21:24 -0500612 }
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800613
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700614 error = class_device_add_groups(class_dev);
615 if (error)
Kay Sievers805fab42006-09-14 11:23:28 +0200616 goto out7;
617
618 error = make_deprecated_class_device_links(class_dev);
619 if (error)
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700620 goto out8;
Stephen Hemminger14982212006-05-06 17:55:11 -0700621
Kay Sievers312c0042005-11-16 09:00:00 +0100622 kobject_uevent(&class_dev->kobj, KOBJ_ADD);
Dmitry Torokhovdbe90352005-09-15 02:01:37 -0500623
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800624 /* notify any interfaces this device is now here */
Jayachandran Ca1438892006-04-03 12:31:53 -0700625 down(&parent_class->sem);
626 list_add_tail(&class_dev->node, &parent_class->children);
627 list_for_each_entry(class_intf, &parent_class->interfaces, node) {
628 if (class_intf->add)
629 class_intf->add(class_dev, class_intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 }
Jayachandran Ca1438892006-04-03 12:31:53 -0700631 up(&parent_class->sem);
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800632
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700633 goto out1;
634
635 out8:
Kay Sievers805fab42006-09-14 11:23:28 +0200636 class_device_remove_groups(class_dev);
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700637 out7:
638 if (class_dev->dev)
639 sysfs_remove_link(&class_dev->kobj, "device");
640 out6:
641 class_device_remove_attrs(class_dev);
642 out5:
Tejun Heoad6a1e12007-06-14 03:45:17 +0900643 if (MAJOR(class_dev->devt))
644 class_device_remove_file(class_dev, &class_devt_attr);
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700645 out4:
Tejun Heoad6a1e12007-06-14 03:45:17 +0900646 class_device_remove_file(class_dev, &class_uevent_attr);
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700647 out3:
648 kobject_del(&class_dev->kobj);
649 out2:
650 if(parent_class_dev)
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700651 class_device_put(parent_class_dev);
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700652 class_put(parent_class);
653 out1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 class_device_put(class_dev);
655 return error;
656}
657
658int class_device_register(struct class_device *class_dev)
659{
660 class_device_initialize(class_dev);
661 return class_device_add(class_dev);
662}
663
gregkh@suse.de2fc68442005-03-23 10:02:56 -0800664/**
665 * class_device_create - creates a class device and registers it with sysfs
Rolf Eike Beer92a0f862006-09-29 01:59:13 -0700666 * @cls: pointer to the struct class that this device should be registered to.
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700667 * @parent: pointer to the parent struct class_device of this new device, if any.
Rolf Eike Beer92a0f862006-09-29 01:59:13 -0700668 * @devt: the dev_t for the char device to be added.
gregkh@suse.de2fc68442005-03-23 10:02:56 -0800669 * @device: a pointer to a struct device that is assiociated with this class device.
670 * @fmt: string for the class device's name
671 *
672 * This function can be used by char device classes. A struct
673 * class_device will be created in sysfs, registered to the specified
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700674 * class.
675 * A "dev" file will be created, showing the dev_t for the device, if
676 * the dev_t is not 0,0.
677 * If a pointer to a parent struct class_device is passed in, the newly
678 * created struct class_device will be a child of that device in sysfs.
679 * The pointer to the struct class_device will be returned from the
680 * call. Any further sysfs files that might be required can be created
681 * using this pointer.
gregkh@suse.de2fc68442005-03-23 10:02:56 -0800682 *
683 * Note: the struct class passed to this function must have previously
684 * been created with a call to class_create().
685 */
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700686struct class_device *class_device_create(struct class *cls,
687 struct class_device *parent,
688 dev_t devt,
Dmitry Torokhovddd5d352006-08-10 01:18:18 -0400689 struct device *device,
690 const char *fmt, ...)
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800691{
692 va_list args;
693 struct class_device *class_dev = NULL;
694 int retval = -ENODEV;
695
696 if (cls == NULL || IS_ERR(cls))
697 goto error;
698
Jiri Slaby4aed0642005-09-13 01:25:01 -0700699 class_dev = kzalloc(sizeof(*class_dev), GFP_KERNEL);
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800700 if (!class_dev) {
701 retval = -ENOMEM;
702 goto error;
703 }
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800704
705 class_dev->devt = devt;
706 class_dev->dev = device;
707 class_dev->class = cls;
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700708 class_dev->parent = parent;
709 class_dev->release = class_device_create_release;
Kay Sievers312c0042005-11-16 09:00:00 +0100710 class_dev->uevent = class_device_create_uevent;
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800711
712 va_start(args, fmt);
713 vsnprintf(class_dev->class_id, BUS_ID_SIZE, fmt, args);
714 va_end(args);
715 retval = class_device_register(class_dev);
716 if (retval)
717 goto error;
718
719 return class_dev;
720
721error:
722 kfree(class_dev);
723 return ERR_PTR(retval);
724}
725
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726void class_device_del(struct class_device *class_dev)
727{
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700728 struct class *parent_class = class_dev->class;
729 struct class_device *parent_device = class_dev->parent;
730 struct class_interface *class_intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700732 if (parent_class) {
733 down(&parent_class->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 list_del_init(&class_dev->node);
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700735 list_for_each_entry(class_intf, &parent_class->interfaces, node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 if (class_intf->remove)
Dmitry Torokhovd8539d82005-09-15 02:01:36 -0500737 class_intf->remove(class_dev, class_intf);
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700738 up(&parent_class->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 }
740
Dmitry Torokhov76d1ce02005-07-10 01:21:24 -0500741 if (class_dev->dev) {
Kay Sievers805fab42006-09-14 11:23:28 +0200742 remove_deprecated_class_device_links(class_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 sysfs_remove_link(&class_dev->kobj, "device");
Dmitry Torokhov76d1ce02005-07-10 01:21:24 -0500744 }
Kay Sieversb9d9c822006-06-15 15:31:56 +0200745 sysfs_remove_link(&class_dev->kobj, "subsystem");
Tejun Heoad6a1e12007-06-14 03:45:17 +0900746 class_device_remove_file(class_dev, &class_uevent_attr);
747 if (MAJOR(class_dev->devt))
748 class_device_remove_file(class_dev, &class_devt_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 class_device_remove_attrs(class_dev);
Stephen Hemminger14982212006-05-06 17:55:11 -0700750 class_device_remove_groups(class_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Kay Sievers312c0042005-11-16 09:00:00 +0100752 kobject_uevent(&class_dev->kobj, KOBJ_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 kobject_del(&class_dev->kobj);
754
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700755 class_device_put(parent_device);
756 class_put(parent_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757}
758
759void class_device_unregister(struct class_device *class_dev)
760{
761 pr_debug("CLASS: Unregistering class device. ID = '%s'\n",
762 class_dev->class_id);
763 class_device_del(class_dev);
764 class_device_put(class_dev);
765}
766
gregkh@suse.de2fc68442005-03-23 10:02:56 -0800767/**
768 * class_device_destroy - removes a class device that was created with class_device_create()
769 * @cls: the pointer to the struct class that this device was registered * with.
Rolf Eike Beer92a0f862006-09-29 01:59:13 -0700770 * @devt: the dev_t of the device that was previously registered.
gregkh@suse.de2fc68442005-03-23 10:02:56 -0800771 *
772 * This call unregisters and cleans up a class device that was created with a
773 * call to class_device_create()
774 */
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800775void class_device_destroy(struct class *cls, dev_t devt)
776{
777 struct class_device *class_dev = NULL;
778 struct class_device *class_dev_tmp;
779
780 down(&cls->sem);
781 list_for_each_entry(class_dev_tmp, &cls->children, node) {
782 if (class_dev_tmp->devt == devt) {
783 class_dev = class_dev_tmp;
784 break;
785 }
786 }
787 up(&cls->sem);
788
789 if (class_dev)
790 class_device_unregister(class_dev);
791}
792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793struct class_device * class_device_get(struct class_device *class_dev)
794{
795 if (class_dev)
796 return to_class_dev(kobject_get(&class_dev->kobj));
797 return NULL;
798}
799
800void class_device_put(struct class_device *class_dev)
801{
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700802 if (class_dev)
803 kobject_put(&class_dev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804}
805
806
807int class_interface_register(struct class_interface *class_intf)
808{
809 struct class *parent;
810 struct class_device *class_dev;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200811 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
813 if (!class_intf || !class_intf->class)
814 return -ENODEV;
815
816 parent = class_get(class_intf->class);
817 if (!parent)
818 return -EINVAL;
819
820 down(&parent->sem);
821 list_add_tail(&class_intf->node, &parent->interfaces);
822 if (class_intf->add) {
823 list_for_each_entry(class_dev, &parent->children, node)
Dmitry Torokhovd8539d82005-09-15 02:01:36 -0500824 class_intf->add(class_dev, class_intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 }
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200826 if (class_intf->add_dev) {
827 list_for_each_entry(dev, &parent->devices, node)
828 class_intf->add_dev(dev, class_intf);
829 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 up(&parent->sem);
831
832 return 0;
833}
834
835void class_interface_unregister(struct class_interface *class_intf)
836{
837 struct class * parent = class_intf->class;
838 struct class_device *class_dev;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200839 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
841 if (!parent)
842 return;
843
844 down(&parent->sem);
845 list_del_init(&class_intf->node);
846 if (class_intf->remove) {
847 list_for_each_entry(class_dev, &parent->children, node)
Dmitry Torokhovd8539d82005-09-15 02:01:36 -0500848 class_intf->remove(class_dev, class_intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 }
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200850 if (class_intf->remove_dev) {
851 list_for_each_entry(dev, &parent->devices, node)
852 class_intf->remove_dev(dev, class_intf);
853 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 up(&parent->sem);
855
856 class_put(parent);
857}
858
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859int __init classes_init(void)
860{
Greg Kroah-Hartman443dbf92007-10-29 23:22:26 -0500861 class_kset = kset_create_and_add("class", NULL, NULL);
862 if (!class_kset)
863 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
865 /* ick, this is ugly, the things we go through to keep from showing up
866 * in sysfs... */
Greg Kroah-Hartmane4bc1662007-09-26 11:12:00 -0700867 kset_init(&class_obj_subsys);
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700868 if (!class_obj_subsys.kobj.parent)
869 class_obj_subsys.kobj.parent = &class_obj_subsys.kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 return 0;
871}
872
873EXPORT_SYMBOL_GPL(class_create_file);
874EXPORT_SYMBOL_GPL(class_remove_file);
875EXPORT_SYMBOL_GPL(class_register);
876EXPORT_SYMBOL_GPL(class_unregister);
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800877EXPORT_SYMBOL_GPL(class_create);
878EXPORT_SYMBOL_GPL(class_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
880EXPORT_SYMBOL_GPL(class_device_register);
881EXPORT_SYMBOL_GPL(class_device_unregister);
882EXPORT_SYMBOL_GPL(class_device_initialize);
883EXPORT_SYMBOL_GPL(class_device_add);
884EXPORT_SYMBOL_GPL(class_device_del);
885EXPORT_SYMBOL_GPL(class_device_get);
886EXPORT_SYMBOL_GPL(class_device_put);
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800887EXPORT_SYMBOL_GPL(class_device_create);
888EXPORT_SYMBOL_GPL(class_device_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889EXPORT_SYMBOL_GPL(class_device_create_file);
890EXPORT_SYMBOL_GPL(class_device_remove_file);
Greg Kroah-Hartmand919fd42007-10-31 12:51:29 -0700891EXPORT_SYMBOL_GPL(class_device_create_bin_file);
892EXPORT_SYMBOL_GPL(class_device_remove_bin_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
894EXPORT_SYMBOL_GPL(class_interface_register);
895EXPORT_SYMBOL_GPL(class_interface_unregister);