blob: c3e569730afe709e46973fbc456b9038cc76d8aa [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>
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)
23#define to_class(obj) container_of(obj, struct class, subsys.kset.kobj)
24
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
68static struct kobj_type ktype_class = {
69 .sysfs_ops = &class_sysfs_ops,
70 .release = class_release,
71};
72
73/* Hotplug events for classes go to the class_obj subsys */
74static decl_subsys(class, &ktype_class, NULL);
75
76
77int class_create_file(struct class * cls, const struct class_attribute * attr)
78{
79 int error;
80 if (cls) {
81 error = sysfs_create_file(&cls->subsys.kset.kobj, &attr->attr);
82 } 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)
90 sysfs_remove_file(&cls->subsys.kset.kobj, &attr->attr);
91}
92
93struct class * class_get(struct class * cls)
94{
95 if (cls)
96 return container_of(subsys_get(&cls->subsys), struct class, subsys);
97 return NULL;
98}
99
100void class_put(struct class * cls)
101{
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700102 if (cls)
103 subsys_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);
144 INIT_LIST_HEAD(&cls->interfaces);
145 init_MUTEX(&cls->sem);
146 error = kobject_set_name(&cls->subsys.kset.kobj, "%s", cls->name);
147 if (error)
148 return error;
149
150 subsys_set_kset(cls, class_subsys);
151
152 error = subsystem_register(&cls->subsys);
153 if (!error) {
154 error = add_class_attrs(class_get(cls));
155 class_put(cls);
156 }
157 return error;
158}
159
160void class_unregister(struct class * cls)
161{
162 pr_debug("device class '%s': unregistering\n", cls->name);
163 remove_class_attrs(cls);
164 subsystem_unregister(&cls->subsys);
165}
166
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800167static void class_create_release(struct class *cls)
168{
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700169 pr_debug("%s called for %s\n", __FUNCTION__, cls->name);
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800170 kfree(cls);
171}
172
173static void class_device_create_release(struct class_device *class_dev)
174{
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700175 pr_debug("%s called for %s\n", __FUNCTION__, class_dev->class_id);
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800176 kfree(class_dev);
177}
178
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700179/* needed to allow these devices to have parent class devices */
180static int class_device_create_hotplug(struct class_device *class_dev,
181 char **envp, int num_envp,
182 char *buffer, int buffer_size)
183{
184 pr_debug("%s called for %s\n", __FUNCTION__, class_dev->class_id);
185 return 0;
186}
187
gregkh@suse.de2fc68442005-03-23 10:02:56 -0800188/**
189 * class_create - create a struct class structure
190 * @owner: pointer to the module that is to "own" this struct class
191 * @name: pointer to a string for the name of this class.
192 *
193 * This is used to create a struct class pointer that can then be used
194 * in calls to class_device_create().
195 *
196 * Note, the pointer created here is to be destroyed when finished by
197 * making a call to class_destroy().
198 */
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800199struct class *class_create(struct module *owner, char *name)
200{
201 struct class *cls;
202 int retval;
203
Jiri Slaby4aed0642005-09-13 01:25:01 -0700204 cls = kzalloc(sizeof(*cls), GFP_KERNEL);
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800205 if (!cls) {
206 retval = -ENOMEM;
207 goto error;
208 }
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800209
210 cls->name = name;
211 cls->owner = owner;
212 cls->class_release = class_create_release;
213 cls->release = class_device_create_release;
214
215 retval = class_register(cls);
216 if (retval)
217 goto error;
218
219 return cls;
220
221error:
222 kfree(cls);
223 return ERR_PTR(retval);
224}
225
gregkh@suse.de2fc68442005-03-23 10:02:56 -0800226/**
227 * class_destroy - destroys a struct class structure
228 * @cs: pointer to the struct class that is to be destroyed
229 *
230 * Note, the pointer to be destroyed must have been created with a call
231 * to class_create().
232 */
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800233void class_destroy(struct class *cls)
234{
235 if ((cls == NULL) || (IS_ERR(cls)))
236 return;
237
238 class_unregister(cls);
239}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241/* Class Device Stuff */
242
243int class_device_create_file(struct class_device * class_dev,
244 const struct class_device_attribute * attr)
245{
246 int error = -EINVAL;
247 if (class_dev)
248 error = sysfs_create_file(&class_dev->kobj, &attr->attr);
249 return error;
250}
251
252void class_device_remove_file(struct class_device * class_dev,
253 const struct class_device_attribute * attr)
254{
255 if (class_dev)
256 sysfs_remove_file(&class_dev->kobj, &attr->attr);
257}
258
259int class_device_create_bin_file(struct class_device *class_dev,
260 struct bin_attribute *attr)
261{
262 int error = -EINVAL;
263 if (class_dev)
264 error = sysfs_create_bin_file(&class_dev->kobj, attr);
265 return error;
266}
267
268void class_device_remove_bin_file(struct class_device *class_dev,
269 struct bin_attribute *attr)
270{
271 if (class_dev)
272 sysfs_remove_bin_file(&class_dev->kobj, attr);
273}
274
275static ssize_t
276class_device_attr_show(struct kobject * kobj, struct attribute * attr,
277 char * buf)
278{
279 struct class_device_attribute * class_dev_attr = to_class_dev_attr(attr);
280 struct class_device * cd = to_class_dev(kobj);
281 ssize_t ret = 0;
282
283 if (class_dev_attr->show)
284 ret = class_dev_attr->show(cd, buf);
285 return ret;
286}
287
288static ssize_t
289class_device_attr_store(struct kobject * kobj, struct attribute * attr,
290 const char * buf, size_t count)
291{
292 struct class_device_attribute * class_dev_attr = to_class_dev_attr(attr);
293 struct class_device * cd = to_class_dev(kobj);
294 ssize_t ret = 0;
295
296 if (class_dev_attr->store)
297 ret = class_dev_attr->store(cd, buf, count);
298 return ret;
299}
300
301static struct sysfs_ops class_dev_sysfs_ops = {
302 .show = class_device_attr_show,
303 .store = class_device_attr_store,
304};
305
306static void class_dev_release(struct kobject * kobj)
307{
308 struct class_device *cd = to_class_dev(kobj);
309 struct class * cls = cd->class;
310
311 pr_debug("device class '%s': release.\n", cd->class_id);
312
Jesper Juhlfef6ec82005-08-17 22:06:34 +0200313 kfree(cd->devt_attr);
314 cd->devt_attr = NULL;
Maneesh Soni208f3d62005-08-16 15:15:48 -0700315
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700316 if (cd->release)
317 cd->release(cd);
318 else if (cls->release)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 cls->release(cd);
320 else {
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700321 printk(KERN_ERR "Class Device '%s' does not have a release() function, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 "it is broken and must be fixed.\n",
323 cd->class_id);
324 WARN_ON(1);
325 }
326}
327
328static struct kobj_type ktype_class_device = {
329 .sysfs_ops = &class_dev_sysfs_ops,
330 .release = class_dev_release,
331};
332
333static int class_hotplug_filter(struct kset *kset, struct kobject *kobj)
334{
335 struct kobj_type *ktype = get_ktype(kobj);
336
337 if (ktype == &ktype_class_device) {
338 struct class_device *class_dev = to_class_dev(kobj);
339 if (class_dev->class)
340 return 1;
341 }
342 return 0;
343}
344
Dmitry Torokhov419cab32005-04-26 02:32:54 -0500345static const char *class_hotplug_name(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
347 struct class_device *class_dev = to_class_dev(kobj);
348
349 return class_dev->class->name;
350}
351
352static int class_hotplug(struct kset *kset, struct kobject *kobj, char **envp,
353 int num_envp, char *buffer, int buffer_size)
354{
355 struct class_device *class_dev = to_class_dev(kobj);
356 int i = 0;
357 int length = 0;
358 int retval = 0;
359
360 pr_debug("%s - name = %s\n", __FUNCTION__, class_dev->class_id);
361
362 if (class_dev->dev) {
363 /* add physical device, backing this device */
364 struct device *dev = class_dev->dev;
365 char *path = kobject_get_path(&dev->kobj, GFP_KERNEL);
366
367 add_hotplug_env_var(envp, num_envp, &i, buffer, buffer_size,
368 &length, "PHYSDEVPATH=%s", path);
369 kfree(path);
370
371 if (dev->bus)
372 add_hotplug_env_var(envp, num_envp, &i,
373 buffer, buffer_size, &length,
374 "PHYSDEVBUS=%s", dev->bus->name);
375
376 if (dev->driver)
377 add_hotplug_env_var(envp, num_envp, &i,
378 buffer, buffer_size, &length,
379 "PHYSDEVDRIVER=%s", dev->driver->name);
380 }
381
382 if (MAJOR(class_dev->devt)) {
383 add_hotplug_env_var(envp, num_envp, &i,
384 buffer, buffer_size, &length,
385 "MAJOR=%u", MAJOR(class_dev->devt));
386
387 add_hotplug_env_var(envp, num_envp, &i,
388 buffer, buffer_size, &length,
389 "MINOR=%u", MINOR(class_dev->devt));
390 }
391
392 /* terminate, set to next free slot, shrink available space */
393 envp[i] = NULL;
394 envp = &envp[i];
395 num_envp -= i;
396 buffer = &buffer[length];
397 buffer_size -= length;
398
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700399 if (class_dev->hotplug) {
400 /* have the class device specific function add its stuff */
401 retval = class_dev->hotplug(class_dev, envp, num_envp,
402 buffer, buffer_size);
403 if (retval)
404 pr_debug("class_dev->hotplug() returned %d\n", retval);
405 } else if (class_dev->class->hotplug) {
406 /* have the class specific function add its stuff */
407 retval = class_dev->class->hotplug(class_dev, envp, num_envp,
408 buffer, buffer_size);
409 if (retval)
410 pr_debug("class->hotplug() returned %d\n", retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
412
413 return retval;
414}
415
416static struct kset_hotplug_ops class_hotplug_ops = {
417 .filter = class_hotplug_filter,
418 .name = class_hotplug_name,
419 .hotplug = class_hotplug,
420};
421
422static decl_subsys(class_obj, &ktype_class_device, &class_hotplug_ops);
423
424
425static int class_device_add_attrs(struct class_device * cd)
426{
427 int i;
428 int error = 0;
429 struct class * cls = cd->class;
430
431 if (cls->class_dev_attrs) {
432 for (i = 0; attr_name(cls->class_dev_attrs[i]); i++) {
433 error = class_device_create_file(cd,
434 &cls->class_dev_attrs[i]);
435 if (error)
436 goto Err;
437 }
438 }
439 Done:
440 return error;
441 Err:
442 while (--i >= 0)
443 class_device_remove_file(cd,&cls->class_dev_attrs[i]);
444 goto Done;
445}
446
447static void class_device_remove_attrs(struct class_device * cd)
448{
449 int i;
450 struct class * cls = cd->class;
451
452 if (cls->class_dev_attrs) {
453 for (i = 0; attr_name(cls->class_dev_attrs[i]); i++)
454 class_device_remove_file(cd,&cls->class_dev_attrs[i]);
455 }
456}
457
458static ssize_t show_dev(struct class_device *class_dev, char *buf)
459{
460 return print_dev_t(buf, class_dev->devt);
461}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Kay Sieversa7fd6702005-10-01 14:49:43 +0200463static ssize_t store_uevent(struct class_device *class_dev,
464 const char *buf, size_t count)
465{
466 kobject_hotplug(&class_dev->kobj, KOBJ_ADD);
467 return count;
468}
469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470void class_device_initialize(struct class_device *class_dev)
471{
472 kobj_set_kset_s(class_dev, class_obj_subsys);
473 kobject_init(&class_dev->kobj);
474 INIT_LIST_HEAD(&class_dev->node);
475}
476
Dmitry Torokhov76d1ce02005-07-10 01:21:24 -0500477static char *make_class_name(struct class_device *class_dev)
478{
479 char *name;
480 int size;
481
482 size = strlen(class_dev->class->name) +
483 strlen(kobject_name(&class_dev->kobj)) + 2;
484
485 name = kmalloc(size, GFP_KERNEL);
486 if (!name)
487 return ERR_PTR(-ENOMEM);
488
489 strcpy(name, class_dev->class->name);
490 strcat(name, ":");
491 strcat(name, kobject_name(&class_dev->kobj));
492 return name;
493}
494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495int class_device_add(struct class_device *class_dev)
496{
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700497 struct class *parent_class = NULL;
498 struct class_device *parent_class_dev = NULL;
499 struct class_interface *class_intf;
Dmitry Torokhov76d1ce02005-07-10 01:21:24 -0500500 char *class_name = NULL;
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700501 int error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
503 class_dev = class_device_get(class_dev);
504 if (!class_dev)
505 return -EINVAL;
506
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700507 if (!strlen(class_dev->class_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 goto register_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700510 parent_class = class_get(class_dev->class);
511 if (!parent_class)
512 goto register_done;
513 parent_class_dev = class_device_get(class_dev->parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515 pr_debug("CLASS: registering class device: ID = '%s'\n",
516 class_dev->class_id);
517
518 /* first, register with generic layer. */
519 kobject_set_name(&class_dev->kobj, "%s", class_dev->class_id);
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700520 if (parent_class_dev)
521 class_dev->kobj.parent = &parent_class_dev->kobj;
522 else
523 class_dev->kobj.parent = &parent_class->subsys.kset.kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700525 error = kobject_add(&class_dev->kobj);
526 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 goto register_done;
528
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800529 /* add the needed attributes to this device */
Kay Sieversa7fd6702005-10-01 14:49:43 +0200530 class_dev->uevent_attr.attr.name = "uevent";
531 class_dev->uevent_attr.attr.mode = S_IWUSR;
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700532 class_dev->uevent_attr.attr.owner = parent_class->owner;
Kay Sieversa7fd6702005-10-01 14:49:43 +0200533 class_dev->uevent_attr.store = store_uevent;
534 class_device_create_file(class_dev, &class_dev->uevent_attr);
535
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800536 if (MAJOR(class_dev->devt)) {
537 struct class_device_attribute *attr;
Jiri Slaby4aed0642005-09-13 01:25:01 -0700538 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800539 if (!attr) {
540 error = -ENOMEM;
541 kobject_del(&class_dev->kobj);
542 goto register_done;
543 }
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800544 attr->attr.name = "dev";
545 attr->attr.mode = S_IRUGO;
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700546 attr->attr.owner = parent_class->owner;
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800547 attr->show = show_dev;
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800548 class_device_create_file(class_dev, attr);
549 class_dev->devt_attr = attr;
550 }
551
552 class_device_add_attrs(class_dev);
Dmitry Torokhov76d1ce02005-07-10 01:21:24 -0500553 if (class_dev->dev) {
554 class_name = make_class_name(class_dev);
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800555 sysfs_create_link(&class_dev->kobj,
556 &class_dev->dev->kobj, "device");
Dmitry Torokhov76d1ce02005-07-10 01:21:24 -0500557 sysfs_create_link(&class_dev->dev->kobj, &class_dev->kobj,
558 class_name);
559 }
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800560
Dmitry Torokhovdbe90352005-09-15 02:01:37 -0500561 kobject_hotplug(&class_dev->kobj, KOBJ_ADD);
562
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800563 /* notify any interfaces this device is now here */
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700564 if (parent_class) {
565 down(&parent_class->sem);
566 list_add_tail(&class_dev->node, &parent_class->children);
567 list_for_each_entry(class_intf, &parent_class->interfaces, node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 if (class_intf->add)
Dmitry Torokhovd8539d82005-09-15 02:01:36 -0500569 class_intf->add(class_dev, class_intf);
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700570 up(&parent_class->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 }
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 register_done:
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700574 if (error) {
575 class_put(parent_class);
576 class_device_put(parent_class_dev);
577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 class_device_put(class_dev);
Dmitry Torokhov76d1ce02005-07-10 01:21:24 -0500579 kfree(class_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 return error;
581}
582
583int class_device_register(struct class_device *class_dev)
584{
585 class_device_initialize(class_dev);
586 return class_device_add(class_dev);
587}
588
gregkh@suse.de2fc68442005-03-23 10:02:56 -0800589/**
590 * class_device_create - creates a class device and registers it with sysfs
591 * @cs: pointer to the struct class that this device should be registered to.
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700592 * @parent: pointer to the parent struct class_device of this new device, if any.
gregkh@suse.de2fc68442005-03-23 10:02:56 -0800593 * @dev: the dev_t for the char device to be added.
594 * @device: a pointer to a struct device that is assiociated with this class device.
595 * @fmt: string for the class device's name
596 *
597 * This function can be used by char device classes. A struct
598 * class_device will be created in sysfs, registered to the specified
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700599 * class.
600 * A "dev" file will be created, showing the dev_t for the device, if
601 * the dev_t is not 0,0.
602 * If a pointer to a parent struct class_device is passed in, the newly
603 * created struct class_device will be a child of that device in sysfs.
604 * The pointer to the struct class_device will be returned from the
605 * call. Any further sysfs files that might be required can be created
606 * using this pointer.
gregkh@suse.de2fc68442005-03-23 10:02:56 -0800607 *
608 * Note: the struct class passed to this function must have previously
609 * been created with a call to class_create().
610 */
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700611struct class_device *class_device_create(struct class *cls,
612 struct class_device *parent,
613 dev_t devt,
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800614 struct device *device, char *fmt, ...)
615{
616 va_list args;
617 struct class_device *class_dev = NULL;
618 int retval = -ENODEV;
619
620 if (cls == NULL || IS_ERR(cls))
621 goto error;
622
Jiri Slaby4aed0642005-09-13 01:25:01 -0700623 class_dev = kzalloc(sizeof(*class_dev), GFP_KERNEL);
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800624 if (!class_dev) {
625 retval = -ENOMEM;
626 goto error;
627 }
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800628
629 class_dev->devt = devt;
630 class_dev->dev = device;
631 class_dev->class = cls;
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700632 class_dev->parent = parent;
633 class_dev->release = class_device_create_release;
634 class_dev->hotplug = class_device_create_hotplug;
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800635
636 va_start(args, fmt);
637 vsnprintf(class_dev->class_id, BUS_ID_SIZE, fmt, args);
638 va_end(args);
639 retval = class_device_register(class_dev);
640 if (retval)
641 goto error;
642
643 return class_dev;
644
645error:
646 kfree(class_dev);
647 return ERR_PTR(retval);
648}
649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650void class_device_del(struct class_device *class_dev)
651{
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700652 struct class *parent_class = class_dev->class;
653 struct class_device *parent_device = class_dev->parent;
654 struct class_interface *class_intf;
Dmitry Torokhov76d1ce02005-07-10 01:21:24 -0500655 char *class_name = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700657 if (parent_class) {
658 down(&parent_class->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 list_del_init(&class_dev->node);
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700660 list_for_each_entry(class_intf, &parent_class->interfaces, node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 if (class_intf->remove)
Dmitry Torokhovd8539d82005-09-15 02:01:36 -0500662 class_intf->remove(class_dev, class_intf);
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700663 up(&parent_class->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 }
665
Dmitry Torokhov76d1ce02005-07-10 01:21:24 -0500666 if (class_dev->dev) {
667 class_name = make_class_name(class_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 sysfs_remove_link(&class_dev->kobj, "device");
Dmitry Torokhov76d1ce02005-07-10 01:21:24 -0500669 sysfs_remove_link(&class_dev->dev->kobj, class_name);
670 }
Kay Sieversa7fd6702005-10-01 14:49:43 +0200671 class_device_remove_file(class_dev, &class_dev->uevent_attr);
Maneesh Soni208f3d62005-08-16 15:15:48 -0700672 if (class_dev->devt_attr)
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800673 class_device_remove_file(class_dev, class_dev->devt_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 class_device_remove_attrs(class_dev);
675
kay.sievers@vrfy.org0700f562005-04-18 21:57:35 -0700676 kobject_hotplug(&class_dev->kobj, KOBJ_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 kobject_del(&class_dev->kobj);
678
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700679 class_device_put(parent_device);
680 class_put(parent_class);
Dmitry Torokhov76d1ce02005-07-10 01:21:24 -0500681 kfree(class_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682}
683
684void class_device_unregister(struct class_device *class_dev)
685{
686 pr_debug("CLASS: Unregistering class device. ID = '%s'\n",
687 class_dev->class_id);
688 class_device_del(class_dev);
689 class_device_put(class_dev);
690}
691
gregkh@suse.de2fc68442005-03-23 10:02:56 -0800692/**
693 * class_device_destroy - removes a class device that was created with class_device_create()
694 * @cls: the pointer to the struct class that this device was registered * with.
695 * @dev: the dev_t of the device that was previously registered.
696 *
697 * This call unregisters and cleans up a class device that was created with a
698 * call to class_device_create()
699 */
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800700void class_device_destroy(struct class *cls, dev_t devt)
701{
702 struct class_device *class_dev = NULL;
703 struct class_device *class_dev_tmp;
704
705 down(&cls->sem);
706 list_for_each_entry(class_dev_tmp, &cls->children, node) {
707 if (class_dev_tmp->devt == devt) {
708 class_dev = class_dev_tmp;
709 break;
710 }
711 }
712 up(&cls->sem);
713
714 if (class_dev)
715 class_device_unregister(class_dev);
716}
717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718int class_device_rename(struct class_device *class_dev, char *new_name)
719{
720 int error = 0;
Bill Nottingham3e513772005-09-22 00:47:36 -0700721 char *old_class_name = NULL, *new_class_name = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 class_dev = class_device_get(class_dev);
724 if (!class_dev)
725 return -EINVAL;
726
727 pr_debug("CLASS: renaming '%s' to '%s'\n", class_dev->class_id,
728 new_name);
729
Bill Nottingham3e513772005-09-22 00:47:36 -0700730 if (class_dev->dev)
731 old_class_name = make_class_name(class_dev);
732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 strlcpy(class_dev->class_id, new_name, KOBJ_NAME_LEN);
734
735 error = kobject_rename(&class_dev->kobj, new_name);
736
Bill Nottingham3e513772005-09-22 00:47:36 -0700737 if (class_dev->dev) {
738 new_class_name = make_class_name(class_dev);
739 sysfs_create_link(&class_dev->dev->kobj, &class_dev->kobj,
740 new_class_name);
741 sysfs_remove_link(&class_dev->dev->kobj, old_class_name);
742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 class_device_put(class_dev);
744
Bill Nottingham3e513772005-09-22 00:47:36 -0700745 kfree(old_class_name);
746 kfree(new_class_name);
747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 return error;
749}
750
751struct class_device * class_device_get(struct class_device *class_dev)
752{
753 if (class_dev)
754 return to_class_dev(kobject_get(&class_dev->kobj));
755 return NULL;
756}
757
758void class_device_put(struct class_device *class_dev)
759{
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700760 if (class_dev)
761 kobject_put(&class_dev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762}
763
764
765int class_interface_register(struct class_interface *class_intf)
766{
767 struct class *parent;
768 struct class_device *class_dev;
769
770 if (!class_intf || !class_intf->class)
771 return -ENODEV;
772
773 parent = class_get(class_intf->class);
774 if (!parent)
775 return -EINVAL;
776
777 down(&parent->sem);
778 list_add_tail(&class_intf->node, &parent->interfaces);
779 if (class_intf->add) {
780 list_for_each_entry(class_dev, &parent->children, node)
Dmitry Torokhovd8539d82005-09-15 02:01:36 -0500781 class_intf->add(class_dev, class_intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 }
783 up(&parent->sem);
784
785 return 0;
786}
787
788void class_interface_unregister(struct class_interface *class_intf)
789{
790 struct class * parent = class_intf->class;
791 struct class_device *class_dev;
792
793 if (!parent)
794 return;
795
796 down(&parent->sem);
797 list_del_init(&class_intf->node);
798 if (class_intf->remove) {
799 list_for_each_entry(class_dev, &parent->children, node)
Dmitry Torokhovd8539d82005-09-15 02:01:36 -0500800 class_intf->remove(class_dev, class_intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 }
802 up(&parent->sem);
803
804 class_put(parent);
805}
806
807
808
809int __init classes_init(void)
810{
811 int retval;
812
813 retval = subsystem_register(&class_subsys);
814 if (retval)
815 return retval;
816
817 /* ick, this is ugly, the things we go through to keep from showing up
818 * in sysfs... */
819 subsystem_init(&class_obj_subsys);
820 if (!class_obj_subsys.kset.subsys)
821 class_obj_subsys.kset.subsys = &class_obj_subsys;
822 return 0;
823}
824
825EXPORT_SYMBOL_GPL(class_create_file);
826EXPORT_SYMBOL_GPL(class_remove_file);
827EXPORT_SYMBOL_GPL(class_register);
828EXPORT_SYMBOL_GPL(class_unregister);
829EXPORT_SYMBOL_GPL(class_get);
830EXPORT_SYMBOL_GPL(class_put);
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800831EXPORT_SYMBOL_GPL(class_create);
832EXPORT_SYMBOL_GPL(class_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
834EXPORT_SYMBOL_GPL(class_device_register);
835EXPORT_SYMBOL_GPL(class_device_unregister);
836EXPORT_SYMBOL_GPL(class_device_initialize);
837EXPORT_SYMBOL_GPL(class_device_add);
838EXPORT_SYMBOL_GPL(class_device_del);
839EXPORT_SYMBOL_GPL(class_device_get);
840EXPORT_SYMBOL_GPL(class_device_put);
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800841EXPORT_SYMBOL_GPL(class_device_create);
842EXPORT_SYMBOL_GPL(class_device_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843EXPORT_SYMBOL_GPL(class_device_create_file);
844EXPORT_SYMBOL_GPL(class_device_remove_file);
845EXPORT_SYMBOL_GPL(class_device_create_bin_file);
846EXPORT_SYMBOL_GPL(class_device_remove_bin_file);
847
848EXPORT_SYMBOL_GPL(class_interface_register);
849EXPORT_SYMBOL_GPL(class_interface_unregister);