blob: 9aa1274602625fa6beb6c463838d77a770800919 [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
13#include <linux/config.h>
14#include <linux/device.h>
15#include <linux/module.h>
16#include <linux/init.h>
17#include <linux/string.h>
18#include <linux/kdev_t.h>
gregkh@suse.dee9ba6362005-03-15 11:54:21 -080019#include <linux/err.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080020#include <linux/slab.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)
24#define to_class(obj) container_of(obj, struct class, subsys.kset.kobj)
25
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
69static struct kobj_type ktype_class = {
70 .sysfs_ops = &class_sysfs_ops,
71 .release = class_release,
72};
73
74/* Hotplug events for classes go to the class_obj subsys */
75static decl_subsys(class, &ktype_class, NULL);
76
77
78int class_create_file(struct class * cls, const struct class_attribute * attr)
79{
80 int error;
81 if (cls) {
82 error = sysfs_create_file(&cls->subsys.kset.kobj, &attr->attr);
83 } 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)
91 sysfs_remove_file(&cls->subsys.kset.kobj, &attr->attr);
92}
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)
97 return container_of(subsys_get(&cls->subsys), struct class, subsys);
98 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)
104 subsys_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);
147 init_MUTEX(&cls->sem);
148 error = kobject_set_name(&cls->subsys.kset.kobj, "%s", cls->name);
149 if (error)
150 return error;
151
152 subsys_set_kset(cls, class_subsys);
153
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,
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700183 char **envp, int num_envp,
184 char *buffer, int buffer_size)
185{
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 */
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800201struct class *class_create(struct module *owner, char *name)
202{
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
230 * @cs: pointer to the struct class that is to be destroyed
231 *
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
261int 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
277static 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
Jesper Juhlfef6ec82005-08-17 22:06:34 +0200315 kfree(cd->devt_attr);
316 cd->devt_attr = NULL;
Maneesh Soni208f3d62005-08-16 15:15:48 -0700317
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700318 if (cd->release)
319 cd->release(cd);
320 else if (cls->release)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 cls->release(cd);
322 else {
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700323 printk(KERN_ERR "Class Device '%s' does not have a release() function, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 "it is broken and must be fixed.\n",
325 cd->class_id);
326 WARN_ON(1);
327 }
328}
329
330static struct kobj_type ktype_class_device = {
331 .sysfs_ops = &class_dev_sysfs_ops,
332 .release = class_dev_release,
333};
334
Kay Sievers312c0042005-11-16 09:00:00 +0100335static int class_uevent_filter(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
337 struct kobj_type *ktype = get_ktype(kobj);
338
339 if (ktype == &ktype_class_device) {
340 struct class_device *class_dev = to_class_dev(kobj);
341 if (class_dev->class)
342 return 1;
343 }
344 return 0;
345}
346
Kay Sievers312c0042005-11-16 09:00:00 +0100347static const char *class_uevent_name(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
349 struct class_device *class_dev = to_class_dev(kobj);
350
351 return class_dev->class->name;
352}
353
Kay Sievers312c0042005-11-16 09:00:00 +0100354static int class_uevent(struct kset *kset, struct kobject *kobj, char **envp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 int num_envp, char *buffer, int buffer_size)
356{
357 struct class_device *class_dev = to_class_dev(kobj);
358 int i = 0;
359 int length = 0;
360 int retval = 0;
361
362 pr_debug("%s - name = %s\n", __FUNCTION__, class_dev->class_id);
363
364 if (class_dev->dev) {
365 /* add physical device, backing this device */
366 struct device *dev = class_dev->dev;
367 char *path = kobject_get_path(&dev->kobj, GFP_KERNEL);
368
Kay Sievers312c0042005-11-16 09:00:00 +0100369 add_uevent_var(envp, num_envp, &i, buffer, buffer_size,
370 &length, "PHYSDEVPATH=%s", path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 kfree(path);
372
373 if (dev->bus)
Kay Sievers312c0042005-11-16 09:00:00 +0100374 add_uevent_var(envp, num_envp, &i,
375 buffer, buffer_size, &length,
376 "PHYSDEVBUS=%s", dev->bus->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
378 if (dev->driver)
Kay Sievers312c0042005-11-16 09:00:00 +0100379 add_uevent_var(envp, num_envp, &i,
380 buffer, buffer_size, &length,
381 "PHYSDEVDRIVER=%s", dev->driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 }
383
384 if (MAJOR(class_dev->devt)) {
Kay Sievers312c0042005-11-16 09:00:00 +0100385 add_uevent_var(envp, num_envp, &i,
386 buffer, buffer_size, &length,
387 "MAJOR=%u", MAJOR(class_dev->devt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Kay Sievers312c0042005-11-16 09:00:00 +0100389 add_uevent_var(envp, num_envp, &i,
390 buffer, buffer_size, &length,
391 "MINOR=%u", MINOR(class_dev->devt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 }
393
394 /* terminate, set to next free slot, shrink available space */
395 envp[i] = NULL;
396 envp = &envp[i];
397 num_envp -= i;
398 buffer = &buffer[length];
399 buffer_size -= length;
400
Kay Sievers312c0042005-11-16 09:00:00 +0100401 if (class_dev->uevent) {
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700402 /* have the class device specific function add its stuff */
Kay Sievers312c0042005-11-16 09:00:00 +0100403 retval = class_dev->uevent(class_dev, envp, num_envp,
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700404 buffer, buffer_size);
405 if (retval)
Kay Sievers312c0042005-11-16 09:00:00 +0100406 pr_debug("class_dev->uevent() returned %d\n", retval);
407 } else if (class_dev->class->uevent) {
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700408 /* have the class specific function add its stuff */
Kay Sievers312c0042005-11-16 09:00:00 +0100409 retval = class_dev->class->uevent(class_dev, envp, num_envp,
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700410 buffer, buffer_size);
411 if (retval)
Kay Sievers312c0042005-11-16 09:00:00 +0100412 pr_debug("class->uevent() returned %d\n", retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
414
415 return retval;
416}
417
Kay Sievers312c0042005-11-16 09:00:00 +0100418static struct kset_uevent_ops class_uevent_ops = {
419 .filter = class_uevent_filter,
420 .name = class_uevent_name,
421 .uevent = class_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422};
423
Kay Sievers312c0042005-11-16 09:00:00 +0100424static decl_subsys(class_obj, &ktype_class_device, &class_uevent_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426
427static int class_device_add_attrs(struct class_device * cd)
428{
429 int i;
430 int error = 0;
431 struct class * cls = cd->class;
432
433 if (cls->class_dev_attrs) {
434 for (i = 0; attr_name(cls->class_dev_attrs[i]); i++) {
435 error = class_device_create_file(cd,
436 &cls->class_dev_attrs[i]);
437 if (error)
438 goto Err;
439 }
440 }
441 Done:
442 return error;
443 Err:
444 while (--i >= 0)
445 class_device_remove_file(cd,&cls->class_dev_attrs[i]);
446 goto Done;
447}
448
449static void class_device_remove_attrs(struct class_device * cd)
450{
451 int i;
452 struct class * cls = cd->class;
453
454 if (cls->class_dev_attrs) {
455 for (i = 0; attr_name(cls->class_dev_attrs[i]); i++)
456 class_device_remove_file(cd,&cls->class_dev_attrs[i]);
457 }
458}
459
Stephen Hemminger14982212006-05-06 17:55:11 -0700460static int class_device_add_groups(struct class_device * cd)
461{
462 int i;
463 int error = 0;
464
465 if (cd->groups) {
466 for (i = 0; cd->groups[i]; i++) {
467 error = sysfs_create_group(&cd->kobj, cd->groups[i]);
468 if (error) {
469 while (--i >= 0)
470 sysfs_remove_group(&cd->kobj, cd->groups[i]);
471 goto out;
472 }
473 }
474 }
475out:
476 return error;
477}
478
479static void class_device_remove_groups(struct class_device * cd)
480{
481 int i;
482 if (cd->groups) {
483 for (i = 0; cd->groups[i]; i++) {
484 sysfs_remove_group(&cd->kobj, cd->groups[i]);
485 }
486 }
487}
488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489static ssize_t show_dev(struct class_device *class_dev, char *buf)
490{
491 return print_dev_t(buf, class_dev->devt);
492}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Kay Sieversa7fd6702005-10-01 14:49:43 +0200494static ssize_t store_uevent(struct class_device *class_dev,
495 const char *buf, size_t count)
496{
Kay Sievers312c0042005-11-16 09:00:00 +0100497 kobject_uevent(&class_dev->kobj, KOBJ_ADD);
Kay Sieversa7fd6702005-10-01 14:49:43 +0200498 return count;
499}
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501void class_device_initialize(struct class_device *class_dev)
502{
503 kobj_set_kset_s(class_dev, class_obj_subsys);
504 kobject_init(&class_dev->kobj);
505 INIT_LIST_HEAD(&class_dev->node);
506}
507
Greg Kroah-Hartmanaa49b912006-06-20 13:59:20 -0700508char *make_class_name(const char *name, struct kobject *kobj)
Dmitry Torokhov76d1ce02005-07-10 01:21:24 -0500509{
Greg Kroah-Hartmanaa49b912006-06-20 13:59:20 -0700510 char *class_name;
Dmitry Torokhov76d1ce02005-07-10 01:21:24 -0500511 int size;
512
Greg Kroah-Hartmanaa49b912006-06-20 13:59:20 -0700513 size = strlen(name) + strlen(kobject_name(kobj)) + 2;
Dmitry Torokhov76d1ce02005-07-10 01:21:24 -0500514
Greg Kroah-Hartmanaa49b912006-06-20 13:59:20 -0700515 class_name = kmalloc(size, GFP_KERNEL);
516 if (!class_name)
Dmitry Torokhov76d1ce02005-07-10 01:21:24 -0500517 return ERR_PTR(-ENOMEM);
518
Greg Kroah-Hartmanaa49b912006-06-20 13:59:20 -0700519 strcpy(class_name, name);
520 strcat(class_name, ":");
521 strcat(class_name, kobject_name(kobj));
522 return class_name;
Dmitry Torokhov76d1ce02005-07-10 01:21:24 -0500523}
524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525int class_device_add(struct class_device *class_dev)
526{
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700527 struct class *parent_class = NULL;
528 struct class_device *parent_class_dev = NULL;
529 struct class_interface *class_intf;
Dmitry Torokhov76d1ce02005-07-10 01:21:24 -0500530 char *class_name = NULL;
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700531 int error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533 class_dev = class_device_get(class_dev);
534 if (!class_dev)
535 return -EINVAL;
536
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700537 if (!strlen(class_dev->class_id))
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700538 goto out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700540 parent_class = class_get(class_dev->class);
541 if (!parent_class)
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700542 goto out1;
543
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700544 parent_class_dev = class_device_get(class_dev->parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546 pr_debug("CLASS: registering class device: ID = '%s'\n",
547 class_dev->class_id);
548
549 /* first, register with generic layer. */
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700550 error = kobject_set_name(&class_dev->kobj, "%s", class_dev->class_id);
551 if (error)
552 goto out2;
553
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700554 if (parent_class_dev)
555 class_dev->kobj.parent = &parent_class_dev->kobj;
556 else
557 class_dev->kobj.parent = &parent_class->subsys.kset.kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700559 error = kobject_add(&class_dev->kobj);
560 if (error)
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700561 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800563 /* add the needed attributes to this device */
Kay Sieversb9d9c822006-06-15 15:31:56 +0200564 sysfs_create_link(&class_dev->kobj, &parent_class->subsys.kset.kobj, "subsystem");
Kay Sieversa7fd6702005-10-01 14:49:43 +0200565 class_dev->uevent_attr.attr.name = "uevent";
566 class_dev->uevent_attr.attr.mode = S_IWUSR;
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700567 class_dev->uevent_attr.attr.owner = parent_class->owner;
Kay Sieversa7fd6702005-10-01 14:49:43 +0200568 class_dev->uevent_attr.store = store_uevent;
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700569 error = class_device_create_file(class_dev, &class_dev->uevent_attr);
570 if (error)
571 goto out3;
Kay Sieversa7fd6702005-10-01 14:49:43 +0200572
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800573 if (MAJOR(class_dev->devt)) {
574 struct class_device_attribute *attr;
Jiri Slaby4aed0642005-09-13 01:25:01 -0700575 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800576 if (!attr) {
577 error = -ENOMEM;
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700578 goto out4;
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800579 }
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800580 attr->attr.name = "dev";
581 attr->attr.mode = S_IRUGO;
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700582 attr->attr.owner = parent_class->owner;
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800583 attr->show = show_dev;
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700584 error = class_device_create_file(class_dev, attr);
585 if (error) {
586 kfree(attr);
587 goto out4;
588 }
589
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800590 class_dev->devt_attr = attr;
591 }
592
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700593 error = class_device_add_attrs(class_dev);
594 if (error)
595 goto out5;
596
Dmitry Torokhov76d1ce02005-07-10 01:21:24 -0500597 if (class_dev->dev) {
Greg Kroah-Hartmanaa49b912006-06-20 13:59:20 -0700598 class_name = make_class_name(class_dev->class->name,
599 &class_dev->kobj);
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700600 error = sysfs_create_link(&class_dev->kobj,
601 &class_dev->dev->kobj, "device");
602 if (error)
603 goto out6;
604 error = sysfs_create_link(&class_dev->dev->kobj, &class_dev->kobj,
605 class_name);
606 if (error)
607 goto out7;
Dmitry Torokhov76d1ce02005-07-10 01:21:24 -0500608 }
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800609
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700610 error = class_device_add_groups(class_dev);
611 if (error)
612 goto out8;
Stephen Hemminger14982212006-05-06 17:55:11 -0700613
Kay Sievers312c0042005-11-16 09:00:00 +0100614 kobject_uevent(&class_dev->kobj, KOBJ_ADD);
Dmitry Torokhovdbe90352005-09-15 02:01:37 -0500615
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800616 /* notify any interfaces this device is now here */
Jayachandran Ca1438892006-04-03 12:31:53 -0700617 down(&parent_class->sem);
618 list_add_tail(&class_dev->node, &parent_class->children);
619 list_for_each_entry(class_intf, &parent_class->interfaces, node) {
620 if (class_intf->add)
621 class_intf->add(class_dev, class_intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 }
Jayachandran Ca1438892006-04-03 12:31:53 -0700623 up(&parent_class->sem);
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800624
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700625 goto out1;
626
627 out8:
628 if (class_dev->dev)
629 sysfs_remove_link(&class_dev->kobj, class_name);
630 out7:
631 if (class_dev->dev)
632 sysfs_remove_link(&class_dev->kobj, "device");
633 out6:
634 class_device_remove_attrs(class_dev);
635 out5:
636 if (class_dev->devt_attr)
637 class_device_remove_file(class_dev, class_dev->devt_attr);
638 out4:
639 class_device_remove_file(class_dev, &class_dev->uevent_attr);
640 out3:
641 kobject_del(&class_dev->kobj);
642 out2:
643 if(parent_class_dev)
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700644 class_device_put(parent_class_dev);
Stephen Hemmingerb7fe4a62006-04-26 09:53:14 -0700645 class_put(parent_class);
646 out1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 class_device_put(class_dev);
Dmitry Torokhov76d1ce02005-07-10 01:21:24 -0500648 kfree(class_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 return error;
650}
651
652int class_device_register(struct class_device *class_dev)
653{
654 class_device_initialize(class_dev);
655 return class_device_add(class_dev);
656}
657
gregkh@suse.de2fc68442005-03-23 10:02:56 -0800658/**
659 * class_device_create - creates a class device and registers it with sysfs
660 * @cs: pointer to the struct class that this device should be registered to.
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700661 * @parent: pointer to the parent struct class_device of this new device, if any.
gregkh@suse.de2fc68442005-03-23 10:02:56 -0800662 * @dev: the dev_t for the char device to be added.
663 * @device: a pointer to a struct device that is assiociated with this class device.
664 * @fmt: string for the class device's name
665 *
666 * This function can be used by char device classes. A struct
667 * class_device will be created in sysfs, registered to the specified
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700668 * class.
669 * A "dev" file will be created, showing the dev_t for the device, if
670 * the dev_t is not 0,0.
671 * If a pointer to a parent struct class_device is passed in, the newly
672 * created struct class_device will be a child of that device in sysfs.
673 * The pointer to the struct class_device will be returned from the
674 * call. Any further sysfs files that might be required can be created
675 * using this pointer.
gregkh@suse.de2fc68442005-03-23 10:02:56 -0800676 *
677 * Note: the struct class passed to this function must have previously
678 * been created with a call to class_create().
679 */
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700680struct class_device *class_device_create(struct class *cls,
681 struct class_device *parent,
682 dev_t devt,
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800683 struct device *device, char *fmt, ...)
684{
685 va_list args;
686 struct class_device *class_dev = NULL;
687 int retval = -ENODEV;
688
689 if (cls == NULL || IS_ERR(cls))
690 goto error;
691
Jiri Slaby4aed0642005-09-13 01:25:01 -0700692 class_dev = kzalloc(sizeof(*class_dev), GFP_KERNEL);
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800693 if (!class_dev) {
694 retval = -ENOMEM;
695 goto error;
696 }
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800697
698 class_dev->devt = devt;
699 class_dev->dev = device;
700 class_dev->class = cls;
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700701 class_dev->parent = parent;
702 class_dev->release = class_device_create_release;
Kay Sievers312c0042005-11-16 09:00:00 +0100703 class_dev->uevent = class_device_create_uevent;
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800704
705 va_start(args, fmt);
706 vsnprintf(class_dev->class_id, BUS_ID_SIZE, fmt, args);
707 va_end(args);
708 retval = class_device_register(class_dev);
709 if (retval)
710 goto error;
711
712 return class_dev;
713
714error:
715 kfree(class_dev);
716 return ERR_PTR(retval);
717}
718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719void class_device_del(struct class_device *class_dev)
720{
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700721 struct class *parent_class = class_dev->class;
722 struct class_device *parent_device = class_dev->parent;
723 struct class_interface *class_intf;
Dmitry Torokhov76d1ce02005-07-10 01:21:24 -0500724 char *class_name = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700726 if (parent_class) {
727 down(&parent_class->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 list_del_init(&class_dev->node);
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700729 list_for_each_entry(class_intf, &parent_class->interfaces, node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 if (class_intf->remove)
Dmitry Torokhovd8539d82005-09-15 02:01:36 -0500731 class_intf->remove(class_dev, class_intf);
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700732 up(&parent_class->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 }
734
Dmitry Torokhov76d1ce02005-07-10 01:21:24 -0500735 if (class_dev->dev) {
Greg Kroah-Hartmanaa49b912006-06-20 13:59:20 -0700736 class_name = make_class_name(class_dev->class->name,
737 &class_dev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 sysfs_remove_link(&class_dev->kobj, "device");
Dmitry Torokhov76d1ce02005-07-10 01:21:24 -0500739 sysfs_remove_link(&class_dev->dev->kobj, class_name);
740 }
Kay Sieversb9d9c822006-06-15 15:31:56 +0200741 sysfs_remove_link(&class_dev->kobj, "subsystem");
Kay Sieversa7fd6702005-10-01 14:49:43 +0200742 class_device_remove_file(class_dev, &class_dev->uevent_attr);
Maneesh Soni208f3d62005-08-16 15:15:48 -0700743 if (class_dev->devt_attr)
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800744 class_device_remove_file(class_dev, class_dev->devt_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 class_device_remove_attrs(class_dev);
Stephen Hemminger14982212006-05-06 17:55:11 -0700746 class_device_remove_groups(class_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Kay Sievers312c0042005-11-16 09:00:00 +0100748 kobject_uevent(&class_dev->kobj, KOBJ_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 kobject_del(&class_dev->kobj);
750
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700751 class_device_put(parent_device);
752 class_put(parent_class);
Dmitry Torokhov76d1ce02005-07-10 01:21:24 -0500753 kfree(class_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754}
755
756void class_device_unregister(struct class_device *class_dev)
757{
758 pr_debug("CLASS: Unregistering class device. ID = '%s'\n",
759 class_dev->class_id);
760 class_device_del(class_dev);
761 class_device_put(class_dev);
762}
763
gregkh@suse.de2fc68442005-03-23 10:02:56 -0800764/**
765 * class_device_destroy - removes a class device that was created with class_device_create()
766 * @cls: the pointer to the struct class that this device was registered * with.
767 * @dev: the dev_t of the device that was previously registered.
768 *
769 * This call unregisters and cleans up a class device that was created with a
770 * call to class_device_create()
771 */
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800772void class_device_destroy(struct class *cls, dev_t devt)
773{
774 struct class_device *class_dev = NULL;
775 struct class_device *class_dev_tmp;
776
777 down(&cls->sem);
778 list_for_each_entry(class_dev_tmp, &cls->children, node) {
779 if (class_dev_tmp->devt == devt) {
780 class_dev = class_dev_tmp;
781 break;
782 }
783 }
784 up(&cls->sem);
785
786 if (class_dev)
787 class_device_unregister(class_dev);
788}
789
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790int class_device_rename(struct class_device *class_dev, char *new_name)
791{
792 int error = 0;
Bill Nottingham3e513772005-09-22 00:47:36 -0700793 char *old_class_name = NULL, *new_class_name = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
795 class_dev = class_device_get(class_dev);
796 if (!class_dev)
797 return -EINVAL;
798
799 pr_debug("CLASS: renaming '%s' to '%s'\n", class_dev->class_id,
800 new_name);
801
Bill Nottingham3e513772005-09-22 00:47:36 -0700802 if (class_dev->dev)
Greg Kroah-Hartmanaa49b912006-06-20 13:59:20 -0700803 old_class_name = make_class_name(class_dev->class->name,
804 &class_dev->kobj);
Bill Nottingham3e513772005-09-22 00:47:36 -0700805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 strlcpy(class_dev->class_id, new_name, KOBJ_NAME_LEN);
807
808 error = kobject_rename(&class_dev->kobj, new_name);
809
Bill Nottingham3e513772005-09-22 00:47:36 -0700810 if (class_dev->dev) {
Greg Kroah-Hartmanaa49b912006-06-20 13:59:20 -0700811 new_class_name = make_class_name(class_dev->class->name,
812 &class_dev->kobj);
Bill Nottingham3e513772005-09-22 00:47:36 -0700813 sysfs_create_link(&class_dev->dev->kobj, &class_dev->kobj,
814 new_class_name);
815 sysfs_remove_link(&class_dev->dev->kobj, old_class_name);
816 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 class_device_put(class_dev);
818
Bill Nottingham3e513772005-09-22 00:47:36 -0700819 kfree(old_class_name);
820 kfree(new_class_name);
821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 return error;
823}
824
825struct class_device * class_device_get(struct class_device *class_dev)
826{
827 if (class_dev)
828 return to_class_dev(kobject_get(&class_dev->kobj));
829 return NULL;
830}
831
832void class_device_put(struct class_device *class_dev)
833{
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700834 if (class_dev)
835 kobject_put(&class_dev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836}
837
838
839int class_interface_register(struct class_interface *class_intf)
840{
841 struct class *parent;
842 struct class_device *class_dev;
843
844 if (!class_intf || !class_intf->class)
845 return -ENODEV;
846
847 parent = class_get(class_intf->class);
848 if (!parent)
849 return -EINVAL;
850
851 down(&parent->sem);
852 list_add_tail(&class_intf->node, &parent->interfaces);
853 if (class_intf->add) {
854 list_for_each_entry(class_dev, &parent->children, node)
Dmitry Torokhovd8539d82005-09-15 02:01:36 -0500855 class_intf->add(class_dev, class_intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 }
857 up(&parent->sem);
858
859 return 0;
860}
861
862void class_interface_unregister(struct class_interface *class_intf)
863{
864 struct class * parent = class_intf->class;
865 struct class_device *class_dev;
866
867 if (!parent)
868 return;
869
870 down(&parent->sem);
871 list_del_init(&class_intf->node);
872 if (class_intf->remove) {
873 list_for_each_entry(class_dev, &parent->children, node)
Dmitry Torokhovd8539d82005-09-15 02:01:36 -0500874 class_intf->remove(class_dev, class_intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 }
876 up(&parent->sem);
877
878 class_put(parent);
879}
880
881
882
883int __init classes_init(void)
884{
885 int retval;
886
887 retval = subsystem_register(&class_subsys);
888 if (retval)
889 return retval;
890
891 /* ick, this is ugly, the things we go through to keep from showing up
892 * in sysfs... */
893 subsystem_init(&class_obj_subsys);
894 if (!class_obj_subsys.kset.subsys)
895 class_obj_subsys.kset.subsys = &class_obj_subsys;
896 return 0;
897}
898
899EXPORT_SYMBOL_GPL(class_create_file);
900EXPORT_SYMBOL_GPL(class_remove_file);
901EXPORT_SYMBOL_GPL(class_register);
902EXPORT_SYMBOL_GPL(class_unregister);
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800903EXPORT_SYMBOL_GPL(class_create);
904EXPORT_SYMBOL_GPL(class_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
906EXPORT_SYMBOL_GPL(class_device_register);
907EXPORT_SYMBOL_GPL(class_device_unregister);
908EXPORT_SYMBOL_GPL(class_device_initialize);
909EXPORT_SYMBOL_GPL(class_device_add);
910EXPORT_SYMBOL_GPL(class_device_del);
911EXPORT_SYMBOL_GPL(class_device_get);
912EXPORT_SYMBOL_GPL(class_device_put);
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800913EXPORT_SYMBOL_GPL(class_device_create);
914EXPORT_SYMBOL_GPL(class_device_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915EXPORT_SYMBOL_GPL(class_device_create_file);
916EXPORT_SYMBOL_GPL(class_device_remove_file);
917EXPORT_SYMBOL_GPL(class_device_create_bin_file);
918EXPORT_SYMBOL_GPL(class_device_remove_bin_file);
919
920EXPORT_SYMBOL_GPL(class_interface_register);
921EXPORT_SYMBOL_GPL(class_interface_unregister);