blob: b1ea4df85c7dde7fc3289e27529bf1b85e785dda [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
94struct class * class_get(struct class * cls)
95{
96 if (cls)
97 return container_of(subsys_get(&cls->subsys), struct class, subsys);
98 return NULL;
99}
100
101void class_put(struct class * cls)
102{
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);
145 INIT_LIST_HEAD(&cls->interfaces);
146 init_MUTEX(&cls->sem);
147 error = kobject_set_name(&cls->subsys.kset.kobj, "%s", cls->name);
148 if (error)
149 return error;
150
151 subsys_set_kset(cls, class_subsys);
152
153 error = subsystem_register(&cls->subsys);
154 if (!error) {
155 error = add_class_attrs(class_get(cls));
156 class_put(cls);
157 }
158 return error;
159}
160
161void class_unregister(struct class * cls)
162{
163 pr_debug("device class '%s': unregistering\n", cls->name);
164 remove_class_attrs(cls);
165 subsystem_unregister(&cls->subsys);
166}
167
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800168static void class_create_release(struct class *cls)
169{
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700170 pr_debug("%s called for %s\n", __FUNCTION__, cls->name);
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800171 kfree(cls);
172}
173
174static void class_device_create_release(struct class_device *class_dev)
175{
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700176 pr_debug("%s called for %s\n", __FUNCTION__, class_dev->class_id);
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800177 kfree(class_dev);
178}
179
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700180/* needed to allow these devices to have parent class devices */
Kay Sievers312c0042005-11-16 09:00:00 +0100181static int class_device_create_uevent(struct class_device *class_dev,
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700182 char **envp, int num_envp,
183 char *buffer, int buffer_size)
184{
185 pr_debug("%s called for %s\n", __FUNCTION__, class_dev->class_id);
186 return 0;
187}
188
gregkh@suse.de2fc68442005-03-23 10:02:56 -0800189/**
190 * class_create - create a struct class structure
191 * @owner: pointer to the module that is to "own" this struct class
192 * @name: pointer to a string for the name of this class.
193 *
194 * This is used to create a struct class pointer that can then be used
195 * in calls to class_device_create().
196 *
197 * Note, the pointer created here is to be destroyed when finished by
198 * making a call to class_destroy().
199 */
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800200struct class *class_create(struct module *owner, char *name)
201{
202 struct class *cls;
203 int retval;
204
Jiri Slaby4aed0642005-09-13 01:25:01 -0700205 cls = kzalloc(sizeof(*cls), GFP_KERNEL);
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800206 if (!cls) {
207 retval = -ENOMEM;
208 goto error;
209 }
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800210
211 cls->name = name;
212 cls->owner = owner;
213 cls->class_release = class_create_release;
214 cls->release = class_device_create_release;
215
216 retval = class_register(cls);
217 if (retval)
218 goto error;
219
220 return cls;
221
222error:
223 kfree(cls);
224 return ERR_PTR(retval);
225}
226
gregkh@suse.de2fc68442005-03-23 10:02:56 -0800227/**
228 * class_destroy - destroys a struct class structure
229 * @cs: pointer to the struct class that is to be destroyed
230 *
231 * Note, the pointer to be destroyed must have been created with a call
232 * to class_create().
233 */
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800234void class_destroy(struct class *cls)
235{
236 if ((cls == NULL) || (IS_ERR(cls)))
237 return;
238
239 class_unregister(cls);
240}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242/* Class Device Stuff */
243
244int class_device_create_file(struct class_device * class_dev,
245 const struct class_device_attribute * attr)
246{
247 int error = -EINVAL;
248 if (class_dev)
249 error = sysfs_create_file(&class_dev->kobj, &attr->attr);
250 return error;
251}
252
253void class_device_remove_file(struct class_device * class_dev,
254 const struct class_device_attribute * attr)
255{
256 if (class_dev)
257 sysfs_remove_file(&class_dev->kobj, &attr->attr);
258}
259
260int class_device_create_bin_file(struct class_device *class_dev,
261 struct bin_attribute *attr)
262{
263 int error = -EINVAL;
264 if (class_dev)
265 error = sysfs_create_bin_file(&class_dev->kobj, attr);
266 return error;
267}
268
269void class_device_remove_bin_file(struct class_device *class_dev,
270 struct bin_attribute *attr)
271{
272 if (class_dev)
273 sysfs_remove_bin_file(&class_dev->kobj, attr);
274}
275
276static ssize_t
277class_device_attr_show(struct kobject * kobj, struct attribute * attr,
278 char * buf)
279{
280 struct class_device_attribute * class_dev_attr = to_class_dev_attr(attr);
281 struct class_device * cd = to_class_dev(kobj);
282 ssize_t ret = 0;
283
284 if (class_dev_attr->show)
285 ret = class_dev_attr->show(cd, buf);
286 return ret;
287}
288
289static ssize_t
290class_device_attr_store(struct kobject * kobj, struct attribute * attr,
291 const char * buf, size_t count)
292{
293 struct class_device_attribute * class_dev_attr = to_class_dev_attr(attr);
294 struct class_device * cd = to_class_dev(kobj);
295 ssize_t ret = 0;
296
297 if (class_dev_attr->store)
298 ret = class_dev_attr->store(cd, buf, count);
299 return ret;
300}
301
302static struct sysfs_ops class_dev_sysfs_ops = {
303 .show = class_device_attr_show,
304 .store = class_device_attr_store,
305};
306
307static void class_dev_release(struct kobject * kobj)
308{
309 struct class_device *cd = to_class_dev(kobj);
310 struct class * cls = cd->class;
311
312 pr_debug("device class '%s': release.\n", cd->class_id);
313
Jesper Juhlfef6ec82005-08-17 22:06:34 +0200314 kfree(cd->devt_attr);
315 cd->devt_attr = NULL;
Maneesh Soni208f3d62005-08-16 15:15:48 -0700316
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700317 if (cd->release)
318 cd->release(cd);
319 else if (cls->release)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 cls->release(cd);
321 else {
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700322 printk(KERN_ERR "Class Device '%s' does not have a release() function, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 "it is broken and must be fixed.\n",
324 cd->class_id);
325 WARN_ON(1);
326 }
327}
328
329static struct kobj_type ktype_class_device = {
330 .sysfs_ops = &class_dev_sysfs_ops,
331 .release = class_dev_release,
332};
333
Kay Sievers312c0042005-11-16 09:00:00 +0100334static int class_uevent_filter(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
336 struct kobj_type *ktype = get_ktype(kobj);
337
338 if (ktype == &ktype_class_device) {
339 struct class_device *class_dev = to_class_dev(kobj);
340 if (class_dev->class)
341 return 1;
342 }
343 return 0;
344}
345
Kay Sievers312c0042005-11-16 09:00:00 +0100346static const char *class_uevent_name(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
348 struct class_device *class_dev = to_class_dev(kobj);
349
350 return class_dev->class->name;
351}
352
Kay Sievers312c0042005-11-16 09:00:00 +0100353static int class_uevent(struct kset *kset, struct kobject *kobj, char **envp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 int num_envp, char *buffer, int buffer_size)
355{
356 struct class_device *class_dev = to_class_dev(kobj);
357 int i = 0;
358 int length = 0;
359 int retval = 0;
360
361 pr_debug("%s - name = %s\n", __FUNCTION__, class_dev->class_id);
362
363 if (class_dev->dev) {
364 /* add physical device, backing this device */
365 struct device *dev = class_dev->dev;
366 char *path = kobject_get_path(&dev->kobj, GFP_KERNEL);
367
Kay Sievers312c0042005-11-16 09:00:00 +0100368 add_uevent_var(envp, num_envp, &i, buffer, buffer_size,
369 &length, "PHYSDEVPATH=%s", path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 kfree(path);
371
372 if (dev->bus)
Kay Sievers312c0042005-11-16 09:00:00 +0100373 add_uevent_var(envp, num_envp, &i,
374 buffer, buffer_size, &length,
375 "PHYSDEVBUS=%s", dev->bus->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377 if (dev->driver)
Kay Sievers312c0042005-11-16 09:00:00 +0100378 add_uevent_var(envp, num_envp, &i,
379 buffer, buffer_size, &length,
380 "PHYSDEVDRIVER=%s", dev->driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 }
382
383 if (MAJOR(class_dev->devt)) {
Kay Sievers312c0042005-11-16 09:00:00 +0100384 add_uevent_var(envp, num_envp, &i,
385 buffer, buffer_size, &length,
386 "MAJOR=%u", MAJOR(class_dev->devt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Kay Sievers312c0042005-11-16 09:00:00 +0100388 add_uevent_var(envp, num_envp, &i,
389 buffer, buffer_size, &length,
390 "MINOR=%u", MINOR(class_dev->devt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 }
392
393 /* terminate, set to next free slot, shrink available space */
394 envp[i] = NULL;
395 envp = &envp[i];
396 num_envp -= i;
397 buffer = &buffer[length];
398 buffer_size -= length;
399
Kay Sievers312c0042005-11-16 09:00:00 +0100400 if (class_dev->uevent) {
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700401 /* have the class device specific function add its stuff */
Kay Sievers312c0042005-11-16 09:00:00 +0100402 retval = class_dev->uevent(class_dev, envp, num_envp,
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700403 buffer, buffer_size);
404 if (retval)
Kay Sievers312c0042005-11-16 09:00:00 +0100405 pr_debug("class_dev->uevent() returned %d\n", retval);
406 } else if (class_dev->class->uevent) {
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700407 /* have the class specific function add its stuff */
Kay Sievers312c0042005-11-16 09:00:00 +0100408 retval = class_dev->class->uevent(class_dev, envp, num_envp,
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700409 buffer, buffer_size);
410 if (retval)
Kay Sievers312c0042005-11-16 09:00:00 +0100411 pr_debug("class->uevent() returned %d\n", retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 }
413
414 return retval;
415}
416
Kay Sievers312c0042005-11-16 09:00:00 +0100417static struct kset_uevent_ops class_uevent_ops = {
418 .filter = class_uevent_filter,
419 .name = class_uevent_name,
420 .uevent = class_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421};
422
Kay Sievers312c0042005-11-16 09:00:00 +0100423static decl_subsys(class_obj, &ktype_class_device, &class_uevent_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425
426static int class_device_add_attrs(struct class_device * cd)
427{
428 int i;
429 int error = 0;
430 struct class * cls = cd->class;
431
432 if (cls->class_dev_attrs) {
433 for (i = 0; attr_name(cls->class_dev_attrs[i]); i++) {
434 error = class_device_create_file(cd,
435 &cls->class_dev_attrs[i]);
436 if (error)
437 goto Err;
438 }
439 }
440 Done:
441 return error;
442 Err:
443 while (--i >= 0)
444 class_device_remove_file(cd,&cls->class_dev_attrs[i]);
445 goto Done;
446}
447
448static void class_device_remove_attrs(struct class_device * cd)
449{
450 int i;
451 struct class * cls = cd->class;
452
453 if (cls->class_dev_attrs) {
454 for (i = 0; attr_name(cls->class_dev_attrs[i]); i++)
455 class_device_remove_file(cd,&cls->class_dev_attrs[i]);
456 }
457}
458
Stephen Hemminger14982212006-05-06 17:55:11 -0700459static int class_device_add_groups(struct class_device * cd)
460{
461 int i;
462 int error = 0;
463
464 if (cd->groups) {
465 for (i = 0; cd->groups[i]; i++) {
466 error = sysfs_create_group(&cd->kobj, cd->groups[i]);
467 if (error) {
468 while (--i >= 0)
469 sysfs_remove_group(&cd->kobj, cd->groups[i]);
470 goto out;
471 }
472 }
473 }
474out:
475 return error;
476}
477
478static void class_device_remove_groups(struct class_device * cd)
479{
480 int i;
481 if (cd->groups) {
482 for (i = 0; cd->groups[i]; i++) {
483 sysfs_remove_group(&cd->kobj, cd->groups[i]);
484 }
485 }
486}
487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488static ssize_t show_dev(struct class_device *class_dev, char *buf)
489{
490 return print_dev_t(buf, class_dev->devt);
491}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Kay Sieversa7fd6702005-10-01 14:49:43 +0200493static ssize_t store_uevent(struct class_device *class_dev,
494 const char *buf, size_t count)
495{
Kay Sievers312c0042005-11-16 09:00:00 +0100496 kobject_uevent(&class_dev->kobj, KOBJ_ADD);
Kay Sieversa7fd6702005-10-01 14:49:43 +0200497 return count;
498}
499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500void class_device_initialize(struct class_device *class_dev)
501{
502 kobj_set_kset_s(class_dev, class_obj_subsys);
503 kobject_init(&class_dev->kobj);
504 INIT_LIST_HEAD(&class_dev->node);
505}
506
Dmitry Torokhov76d1ce02005-07-10 01:21:24 -0500507static char *make_class_name(struct class_device *class_dev)
508{
509 char *name;
510 int size;
511
512 size = strlen(class_dev->class->name) +
513 strlen(kobject_name(&class_dev->kobj)) + 2;
514
515 name = kmalloc(size, GFP_KERNEL);
516 if (!name)
517 return ERR_PTR(-ENOMEM);
518
519 strcpy(name, class_dev->class->name);
520 strcat(name, ":");
521 strcat(name, kobject_name(&class_dev->kobj));
522 return name;
523}
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))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 goto register_done;
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)
542 goto register_done;
543 parent_class_dev = class_device_get(class_dev->parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 pr_debug("CLASS: registering class device: ID = '%s'\n",
546 class_dev->class_id);
547
548 /* first, register with generic layer. */
549 kobject_set_name(&class_dev->kobj, "%s", class_dev->class_id);
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700550 if (parent_class_dev)
551 class_dev->kobj.parent = &parent_class_dev->kobj;
552 else
553 class_dev->kobj.parent = &parent_class->subsys.kset.kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700555 error = kobject_add(&class_dev->kobj);
556 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 goto register_done;
558
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800559 /* add the needed attributes to this device */
Kay Sieversa7fd6702005-10-01 14:49:43 +0200560 class_dev->uevent_attr.attr.name = "uevent";
561 class_dev->uevent_attr.attr.mode = S_IWUSR;
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700562 class_dev->uevent_attr.attr.owner = parent_class->owner;
Kay Sieversa7fd6702005-10-01 14:49:43 +0200563 class_dev->uevent_attr.store = store_uevent;
564 class_device_create_file(class_dev, &class_dev->uevent_attr);
565
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800566 if (MAJOR(class_dev->devt)) {
567 struct class_device_attribute *attr;
Jiri Slaby4aed0642005-09-13 01:25:01 -0700568 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800569 if (!attr) {
570 error = -ENOMEM;
571 kobject_del(&class_dev->kobj);
572 goto register_done;
573 }
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800574 attr->attr.name = "dev";
575 attr->attr.mode = S_IRUGO;
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700576 attr->attr.owner = parent_class->owner;
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800577 attr->show = show_dev;
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800578 class_device_create_file(class_dev, attr);
579 class_dev->devt_attr = attr;
580 }
581
582 class_device_add_attrs(class_dev);
Dmitry Torokhov76d1ce02005-07-10 01:21:24 -0500583 if (class_dev->dev) {
584 class_name = make_class_name(class_dev);
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800585 sysfs_create_link(&class_dev->kobj,
586 &class_dev->dev->kobj, "device");
Dmitry Torokhov76d1ce02005-07-10 01:21:24 -0500587 sysfs_create_link(&class_dev->dev->kobj, &class_dev->kobj,
588 class_name);
589 }
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800590
Stephen Hemminger14982212006-05-06 17:55:11 -0700591 class_device_add_groups(class_dev);
592
Kay Sievers312c0042005-11-16 09:00:00 +0100593 kobject_uevent(&class_dev->kobj, KOBJ_ADD);
Dmitry Torokhovdbe90352005-09-15 02:01:37 -0500594
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800595 /* notify any interfaces this device is now here */
Jayachandran Ca1438892006-04-03 12:31:53 -0700596 down(&parent_class->sem);
597 list_add_tail(&class_dev->node, &parent_class->children);
598 list_for_each_entry(class_intf, &parent_class->interfaces, node) {
599 if (class_intf->add)
600 class_intf->add(class_dev, class_intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 }
Jayachandran Ca1438892006-04-03 12:31:53 -0700602 up(&parent_class->sem);
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 register_done:
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700605 if (error) {
606 class_put(parent_class);
607 class_device_put(parent_class_dev);
608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 class_device_put(class_dev);
Dmitry Torokhov76d1ce02005-07-10 01:21:24 -0500610 kfree(class_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 return error;
612}
613
614int class_device_register(struct class_device *class_dev)
615{
616 class_device_initialize(class_dev);
617 return class_device_add(class_dev);
618}
619
gregkh@suse.de2fc68442005-03-23 10:02:56 -0800620/**
621 * class_device_create - creates a class device and registers it with sysfs
622 * @cs: pointer to the struct class that this device should be registered to.
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700623 * @parent: pointer to the parent struct class_device of this new device, if any.
gregkh@suse.de2fc68442005-03-23 10:02:56 -0800624 * @dev: the dev_t for the char device to be added.
625 * @device: a pointer to a struct device that is assiociated with this class device.
626 * @fmt: string for the class device's name
627 *
628 * This function can be used by char device classes. A struct
629 * class_device will be created in sysfs, registered to the specified
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700630 * class.
631 * A "dev" file will be created, showing the dev_t for the device, if
632 * the dev_t is not 0,0.
633 * If a pointer to a parent struct class_device is passed in, the newly
634 * created struct class_device will be a child of that device in sysfs.
635 * The pointer to the struct class_device will be returned from the
636 * call. Any further sysfs files that might be required can be created
637 * using this pointer.
gregkh@suse.de2fc68442005-03-23 10:02:56 -0800638 *
639 * Note: the struct class passed to this function must have previously
640 * been created with a call to class_create().
641 */
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700642struct class_device *class_device_create(struct class *cls,
643 struct class_device *parent,
644 dev_t devt,
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800645 struct device *device, char *fmt, ...)
646{
647 va_list args;
648 struct class_device *class_dev = NULL;
649 int retval = -ENODEV;
650
651 if (cls == NULL || IS_ERR(cls))
652 goto error;
653
Jiri Slaby4aed0642005-09-13 01:25:01 -0700654 class_dev = kzalloc(sizeof(*class_dev), GFP_KERNEL);
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800655 if (!class_dev) {
656 retval = -ENOMEM;
657 goto error;
658 }
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800659
660 class_dev->devt = devt;
661 class_dev->dev = device;
662 class_dev->class = cls;
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700663 class_dev->parent = parent;
664 class_dev->release = class_device_create_release;
Kay Sievers312c0042005-11-16 09:00:00 +0100665 class_dev->uevent = class_device_create_uevent;
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800666
667 va_start(args, fmt);
668 vsnprintf(class_dev->class_id, BUS_ID_SIZE, fmt, args);
669 va_end(args);
670 retval = class_device_register(class_dev);
671 if (retval)
672 goto error;
673
674 return class_dev;
675
676error:
677 kfree(class_dev);
678 return ERR_PTR(retval);
679}
680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681void class_device_del(struct class_device *class_dev)
682{
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700683 struct class *parent_class = class_dev->class;
684 struct class_device *parent_device = class_dev->parent;
685 struct class_interface *class_intf;
Dmitry Torokhov76d1ce02005-07-10 01:21:24 -0500686 char *class_name = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700688 if (parent_class) {
689 down(&parent_class->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 list_del_init(&class_dev->node);
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700691 list_for_each_entry(class_intf, &parent_class->interfaces, node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 if (class_intf->remove)
Dmitry Torokhovd8539d82005-09-15 02:01:36 -0500693 class_intf->remove(class_dev, class_intf);
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700694 up(&parent_class->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 }
696
Dmitry Torokhov76d1ce02005-07-10 01:21:24 -0500697 if (class_dev->dev) {
698 class_name = make_class_name(class_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 sysfs_remove_link(&class_dev->kobj, "device");
Dmitry Torokhov76d1ce02005-07-10 01:21:24 -0500700 sysfs_remove_link(&class_dev->dev->kobj, class_name);
701 }
Kay Sieversa7fd6702005-10-01 14:49:43 +0200702 class_device_remove_file(class_dev, &class_dev->uevent_attr);
Maneesh Soni208f3d62005-08-16 15:15:48 -0700703 if (class_dev->devt_attr)
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800704 class_device_remove_file(class_dev, class_dev->devt_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 class_device_remove_attrs(class_dev);
Stephen Hemminger14982212006-05-06 17:55:11 -0700706 class_device_remove_groups(class_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Kay Sievers312c0042005-11-16 09:00:00 +0100708 kobject_uevent(&class_dev->kobj, KOBJ_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 kobject_del(&class_dev->kobj);
710
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700711 class_device_put(parent_device);
712 class_put(parent_class);
Dmitry Torokhov76d1ce02005-07-10 01:21:24 -0500713 kfree(class_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714}
715
716void class_device_unregister(struct class_device *class_dev)
717{
718 pr_debug("CLASS: Unregistering class device. ID = '%s'\n",
719 class_dev->class_id);
720 class_device_del(class_dev);
721 class_device_put(class_dev);
722}
723
gregkh@suse.de2fc68442005-03-23 10:02:56 -0800724/**
725 * class_device_destroy - removes a class device that was created with class_device_create()
726 * @cls: the pointer to the struct class that this device was registered * with.
727 * @dev: the dev_t of the device that was previously registered.
728 *
729 * This call unregisters and cleans up a class device that was created with a
730 * call to class_device_create()
731 */
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800732void class_device_destroy(struct class *cls, dev_t devt)
733{
734 struct class_device *class_dev = NULL;
735 struct class_device *class_dev_tmp;
736
737 down(&cls->sem);
738 list_for_each_entry(class_dev_tmp, &cls->children, node) {
739 if (class_dev_tmp->devt == devt) {
740 class_dev = class_dev_tmp;
741 break;
742 }
743 }
744 up(&cls->sem);
745
746 if (class_dev)
747 class_device_unregister(class_dev);
748}
749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750int class_device_rename(struct class_device *class_dev, char *new_name)
751{
752 int error = 0;
Bill Nottingham3e513772005-09-22 00:47:36 -0700753 char *old_class_name = NULL, *new_class_name = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
755 class_dev = class_device_get(class_dev);
756 if (!class_dev)
757 return -EINVAL;
758
759 pr_debug("CLASS: renaming '%s' to '%s'\n", class_dev->class_id,
760 new_name);
761
Bill Nottingham3e513772005-09-22 00:47:36 -0700762 if (class_dev->dev)
763 old_class_name = make_class_name(class_dev);
764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 strlcpy(class_dev->class_id, new_name, KOBJ_NAME_LEN);
766
767 error = kobject_rename(&class_dev->kobj, new_name);
768
Bill Nottingham3e513772005-09-22 00:47:36 -0700769 if (class_dev->dev) {
770 new_class_name = make_class_name(class_dev);
771 sysfs_create_link(&class_dev->dev->kobj, &class_dev->kobj,
772 new_class_name);
773 sysfs_remove_link(&class_dev->dev->kobj, old_class_name);
774 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 class_device_put(class_dev);
776
Bill Nottingham3e513772005-09-22 00:47:36 -0700777 kfree(old_class_name);
778 kfree(new_class_name);
779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 return error;
781}
782
783struct class_device * class_device_get(struct class_device *class_dev)
784{
785 if (class_dev)
786 return to_class_dev(kobject_get(&class_dev->kobj));
787 return NULL;
788}
789
790void class_device_put(struct class_device *class_dev)
791{
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700792 if (class_dev)
793 kobject_put(&class_dev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794}
795
796
797int class_interface_register(struct class_interface *class_intf)
798{
799 struct class *parent;
800 struct class_device *class_dev;
801
802 if (!class_intf || !class_intf->class)
803 return -ENODEV;
804
805 parent = class_get(class_intf->class);
806 if (!parent)
807 return -EINVAL;
808
809 down(&parent->sem);
810 list_add_tail(&class_intf->node, &parent->interfaces);
811 if (class_intf->add) {
812 list_for_each_entry(class_dev, &parent->children, node)
Dmitry Torokhovd8539d82005-09-15 02:01:36 -0500813 class_intf->add(class_dev, class_intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 }
815 up(&parent->sem);
816
817 return 0;
818}
819
820void class_interface_unregister(struct class_interface *class_intf)
821{
822 struct class * parent = class_intf->class;
823 struct class_device *class_dev;
824
825 if (!parent)
826 return;
827
828 down(&parent->sem);
829 list_del_init(&class_intf->node);
830 if (class_intf->remove) {
831 list_for_each_entry(class_dev, &parent->children, node)
Dmitry Torokhovd8539d82005-09-15 02:01:36 -0500832 class_intf->remove(class_dev, class_intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 }
834 up(&parent->sem);
835
836 class_put(parent);
837}
838
839
840
841int __init classes_init(void)
842{
843 int retval;
844
845 retval = subsystem_register(&class_subsys);
846 if (retval)
847 return retval;
848
849 /* ick, this is ugly, the things we go through to keep from showing up
850 * in sysfs... */
851 subsystem_init(&class_obj_subsys);
852 if (!class_obj_subsys.kset.subsys)
853 class_obj_subsys.kset.subsys = &class_obj_subsys;
854 return 0;
855}
856
857EXPORT_SYMBOL_GPL(class_create_file);
858EXPORT_SYMBOL_GPL(class_remove_file);
859EXPORT_SYMBOL_GPL(class_register);
860EXPORT_SYMBOL_GPL(class_unregister);
861EXPORT_SYMBOL_GPL(class_get);
862EXPORT_SYMBOL_GPL(class_put);
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800863EXPORT_SYMBOL_GPL(class_create);
864EXPORT_SYMBOL_GPL(class_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
866EXPORT_SYMBOL_GPL(class_device_register);
867EXPORT_SYMBOL_GPL(class_device_unregister);
868EXPORT_SYMBOL_GPL(class_device_initialize);
869EXPORT_SYMBOL_GPL(class_device_add);
870EXPORT_SYMBOL_GPL(class_device_del);
871EXPORT_SYMBOL_GPL(class_device_get);
872EXPORT_SYMBOL_GPL(class_device_put);
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800873EXPORT_SYMBOL_GPL(class_device_create);
874EXPORT_SYMBOL_GPL(class_device_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875EXPORT_SYMBOL_GPL(class_device_create_file);
876EXPORT_SYMBOL_GPL(class_device_remove_file);
877EXPORT_SYMBOL_GPL(class_device_create_bin_file);
878EXPORT_SYMBOL_GPL(class_device_remove_bin_file);
879
880EXPORT_SYMBOL_GPL(class_interface_register);
881EXPORT_SYMBOL_GPL(class_interface_unregister);