2 * class.c - basic device class management
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.
9 * This file is released under the GPLv2
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>
20 #define to_class_attr(_attr) container_of(_attr, struct class_attribute, attr)
21 #define to_class(obj) container_of(obj, struct class, subsys.kset.kobj)
24 class_attr_show(struct kobject
* kobj
, struct attribute
* attr
, char * buf
)
26 struct class_attribute
* class_attr
= to_class_attr(attr
);
27 struct class * dc
= to_class(kobj
);
31 ret
= class_attr
->show(dc
, buf
);
36 class_attr_store(struct kobject
* kobj
, struct attribute
* attr
,
37 const char * buf
, size_t count
)
39 struct class_attribute
* class_attr
= to_class_attr(attr
);
40 struct class * dc
= to_class(kobj
);
43 if (class_attr
->store
)
44 ret
= class_attr
->store(dc
, buf
, count
);
48 static void class_release(struct kobject
* kobj
)
50 struct class *class = to_class(kobj
);
52 pr_debug("class '%s': release.\n", class->name
);
54 if (class->class_release
)
55 class->class_release(class);
57 pr_debug("class '%s' does not have a release() function, "
58 "be careful\n", class->name
);
61 static struct sysfs_ops class_sysfs_ops
= {
62 .show
= class_attr_show
,
63 .store
= class_attr_store
,
66 static struct kobj_type ktype_class
= {
67 .sysfs_ops
= &class_sysfs_ops
,
68 .release
= class_release
,
71 /* Hotplug events for classes go to the class_obj subsys */
72 static decl_subsys(class, &ktype_class
, NULL
);
75 int class_create_file(struct class * cls
, const struct class_attribute
* attr
)
79 error
= sysfs_create_file(&cls
->subsys
.kset
.kobj
, &attr
->attr
);
85 void class_remove_file(struct class * cls
, const struct class_attribute
* attr
)
88 sysfs_remove_file(&cls
->subsys
.kset
.kobj
, &attr
->attr
);
91 struct class * class_get(struct class * cls
)
94 return container_of(subsys_get(&cls
->subsys
), struct class, subsys
);
98 void class_put(struct class * cls
)
100 subsys_put(&cls
->subsys
);
104 static int add_class_attrs(struct class * cls
)
109 if (cls
->class_attrs
) {
110 for (i
= 0; attr_name(cls
->class_attrs
[i
]); i
++) {
111 error
= class_create_file(cls
,&cls
->class_attrs
[i
]);
120 class_remove_file(cls
,&cls
->class_attrs
[i
]);
124 static void remove_class_attrs(struct class * cls
)
128 if (cls
->class_attrs
) {
129 for (i
= 0; attr_name(cls
->class_attrs
[i
]); i
++)
130 class_remove_file(cls
,&cls
->class_attrs
[i
]);
134 int class_register(struct class * cls
)
138 pr_debug("device class '%s': registering\n", cls
->name
);
140 INIT_LIST_HEAD(&cls
->children
);
141 INIT_LIST_HEAD(&cls
->interfaces
);
142 error
= kobject_set_name(&cls
->subsys
.kset
.kobj
, cls
->name
);
146 subsys_set_kset(cls
, class_subsys
);
148 error
= subsystem_register(&cls
->subsys
);
150 error
= add_class_attrs(class_get(cls
));
156 void class_unregister(struct class * cls
)
158 pr_debug("device class '%s': unregistering\n", cls
->name
);
159 remove_class_attrs(cls
);
160 subsystem_unregister(&cls
->subsys
);
164 /* Class Device Stuff */
166 int class_device_create_file(struct class_device
* class_dev
,
167 const struct class_device_attribute
* attr
)
171 error
= sysfs_create_file(&class_dev
->kobj
, &attr
->attr
);
175 void class_device_remove_file(struct class_device
* class_dev
,
176 const struct class_device_attribute
* attr
)
179 sysfs_remove_file(&class_dev
->kobj
, &attr
->attr
);
182 static int class_device_dev_link(struct class_device
* class_dev
)
185 return sysfs_create_link(&class_dev
->kobj
,
186 &class_dev
->dev
->kobj
, "device");
190 static void class_device_dev_unlink(struct class_device
* class_dev
)
192 sysfs_remove_link(&class_dev
->kobj
, "device");
195 static int class_device_driver_link(struct class_device
* class_dev
)
197 if ((class_dev
->dev
) && (class_dev
->dev
->driver
))
198 return sysfs_create_link(&class_dev
->kobj
,
199 &class_dev
->dev
->driver
->kobj
, "driver");
203 static void class_device_driver_unlink(struct class_device
* class_dev
)
205 sysfs_remove_link(&class_dev
->kobj
, "driver");
210 class_device_attr_show(struct kobject
* kobj
, struct attribute
* attr
,
213 struct class_device_attribute
* class_dev_attr
= to_class_dev_attr(attr
);
214 struct class_device
* cd
= to_class_dev(kobj
);
217 if (class_dev_attr
->show
)
218 ret
= class_dev_attr
->show(cd
, buf
);
223 class_device_attr_store(struct kobject
* kobj
, struct attribute
* attr
,
224 const char * buf
, size_t count
)
226 struct class_device_attribute
* class_dev_attr
= to_class_dev_attr(attr
);
227 struct class_device
* cd
= to_class_dev(kobj
);
230 if (class_dev_attr
->store
)
231 ret
= class_dev_attr
->store(cd
, buf
, count
);
235 static struct sysfs_ops class_dev_sysfs_ops
= {
236 .show
= class_device_attr_show
,
237 .store
= class_device_attr_store
,
240 static void class_dev_release(struct kobject
* kobj
)
242 struct class_device
*cd
= to_class_dev(kobj
);
243 struct class * cls
= cd
->class;
245 pr_debug("device class '%s': release.\n", cd
->class_id
);
250 printk(KERN_ERR
"Device class '%s' does not have a release() function, "
251 "it is broken and must be fixed.\n",
257 static struct kobj_type ktype_class_device
= {
258 .sysfs_ops
= &class_dev_sysfs_ops
,
259 .release
= class_dev_release
,
262 static int class_hotplug_filter(struct kset
*kset
, struct kobject
*kobj
)
264 struct kobj_type
*ktype
= get_ktype(kobj
);
266 if (ktype
== &ktype_class_device
) {
267 struct class_device
*class_dev
= to_class_dev(kobj
);
268 if (class_dev
->class)
274 static char *class_hotplug_name(struct kset
*kset
, struct kobject
*kobj
)
276 struct class_device
*class_dev
= to_class_dev(kobj
);
278 return class_dev
->class->name
;
281 static int class_hotplug(struct kset
*kset
, struct kobject
*kobj
, char **envp
,
282 int num_envp
, char *buffer
, int buffer_size
)
284 struct class_device
*class_dev
= to_class_dev(kobj
);
287 pr_debug("%s - name = %s\n", __FUNCTION__
, class_dev
->class_id
);
288 if (class_dev
->class->hotplug
) {
289 /* have the bus specific function add its stuff */
290 retval
= class_dev
->class->hotplug (class_dev
, envp
, num_envp
,
291 buffer
, buffer_size
);
293 pr_debug ("%s - hotplug() returned %d\n",
294 __FUNCTION__
, retval
);
301 static struct kset_hotplug_ops class_hotplug_ops
= {
302 .filter
= class_hotplug_filter
,
303 .name
= class_hotplug_name
,
304 .hotplug
= class_hotplug
,
307 static decl_subsys(class_obj
, &ktype_class_device
, &class_hotplug_ops
);
310 static int class_device_add_attrs(struct class_device
* cd
)
314 struct class * cls
= cd
->class;
316 if (cls
->class_dev_attrs
) {
317 for (i
= 0; attr_name(cls
->class_dev_attrs
[i
]); i
++) {
318 error
= class_device_create_file(cd
,
319 &cls
->class_dev_attrs
[i
]);
328 class_device_remove_file(cd
,&cls
->class_dev_attrs
[i
]);
332 static void class_device_remove_attrs(struct class_device
* cd
)
335 struct class * cls
= cd
->class;
337 if (cls
->class_dev_attrs
) {
338 for (i
= 0; attr_name(cls
->class_dev_attrs
[i
]); i
++)
339 class_device_remove_file(cd
,&cls
->class_dev_attrs
[i
]);
343 void class_device_initialize(struct class_device
*class_dev
)
345 kobj_set_kset_s(class_dev
, class_obj_subsys
);
346 kobject_init(&class_dev
->kobj
);
347 INIT_LIST_HEAD(&class_dev
->node
);
350 int class_device_add(struct class_device
*class_dev
)
352 struct class * parent
= NULL
;
353 struct class_interface
* class_intf
;
356 class_dev
= class_device_get(class_dev
);
360 if (!strlen(class_dev
->class_id
)) {
365 parent
= class_get(class_dev
->class);
367 pr_debug("CLASS: registering class device: ID = '%s'\n",
368 class_dev
->class_id
);
370 /* first, register with generic layer. */
371 kobject_set_name(&class_dev
->kobj
, class_dev
->class_id
);
373 class_dev
->kobj
.parent
= &parent
->subsys
.kset
.kobj
;
375 if ((error
= kobject_add(&class_dev
->kobj
)))
378 /* now take care of our own registration */
380 down_write(&parent
->subsys
.rwsem
);
381 list_add_tail(&class_dev
->node
, &parent
->children
);
382 list_for_each_entry(class_intf
, &parent
->interfaces
, node
)
384 class_intf
->add(class_dev
);
385 up_write(&parent
->subsys
.rwsem
);
387 class_device_add_attrs(class_dev
);
388 class_device_dev_link(class_dev
);
389 class_device_driver_link(class_dev
);
394 class_device_put(class_dev
);
398 int class_device_register(struct class_device
*class_dev
)
400 class_device_initialize(class_dev
);
401 return class_device_add(class_dev
);
404 void class_device_del(struct class_device
*class_dev
)
406 struct class * parent
= class_dev
->class;
407 struct class_interface
* class_intf
;
410 down_write(&parent
->subsys
.rwsem
);
411 list_del_init(&class_dev
->node
);
412 list_for_each_entry(class_intf
, &parent
->interfaces
, node
)
413 if (class_intf
->remove
)
414 class_intf
->remove(class_dev
);
415 up_write(&parent
->subsys
.rwsem
);
418 class_device_dev_unlink(class_dev
);
419 class_device_driver_unlink(class_dev
);
420 class_device_remove_attrs(class_dev
);
422 kobject_del(&class_dev
->kobj
);
428 void class_device_unregister(struct class_device
*class_dev
)
430 pr_debug("CLASS: Unregistering class device. ID = '%s'\n",
431 class_dev
->class_id
);
432 class_device_del(class_dev
);
433 class_device_put(class_dev
);
436 int class_device_rename(struct class_device
*class_dev
, char *new_name
)
440 class_dev
= class_device_get(class_dev
);
444 pr_debug("CLASS: renaming '%s' to '%s'\n", class_dev
->class_id
,
447 strlcpy(class_dev
->class_id
, new_name
, KOBJ_NAME_LEN
);
449 error
= kobject_rename(&class_dev
->kobj
, new_name
);
451 class_device_put(class_dev
);
456 struct class_device
* class_device_get(struct class_device
*class_dev
)
459 return to_class_dev(kobject_get(&class_dev
->kobj
));
463 void class_device_put(struct class_device
*class_dev
)
465 kobject_put(&class_dev
->kobj
);
469 int class_interface_register(struct class_interface
*class_intf
)
471 struct class * parent
;
472 struct class_device
* class_dev
;
474 if (!class_intf
|| !class_intf
->class)
477 parent
= class_get(class_intf
->class);
481 down_write(&parent
->subsys
.rwsem
);
482 list_add_tail(&class_intf
->node
, &parent
->interfaces
);
484 if (class_intf
->add
) {
485 list_for_each_entry(class_dev
, &parent
->children
, node
)
486 class_intf
->add(class_dev
);
488 up_write(&parent
->subsys
.rwsem
);
493 void class_interface_unregister(struct class_interface
*class_intf
)
495 struct class * parent
= class_intf
->class;
496 struct class_device
*class_dev
;
501 down_write(&parent
->subsys
.rwsem
);
502 list_del_init(&class_intf
->node
);
504 if (class_intf
->remove
) {
505 list_for_each_entry(class_dev
, &parent
->children
, node
)
506 class_intf
->remove(class_dev
);
508 up_write(&parent
->subsys
.rwsem
);
515 int __init
classes_init(void)
519 retval
= subsystem_register(&class_subsys
);
523 /* ick, this is ugly, the things we go through to keep from showing up
525 subsystem_init(&class_obj_subsys
);
526 if (!class_obj_subsys
.kset
.subsys
)
527 class_obj_subsys
.kset
.subsys
= &class_obj_subsys
;
531 EXPORT_SYMBOL(class_create_file
);
532 EXPORT_SYMBOL(class_remove_file
);
533 EXPORT_SYMBOL(class_register
);
534 EXPORT_SYMBOL(class_unregister
);
535 EXPORT_SYMBOL(class_get
);
536 EXPORT_SYMBOL(class_put
);
538 EXPORT_SYMBOL(class_device_register
);
539 EXPORT_SYMBOL(class_device_unregister
);
540 EXPORT_SYMBOL(class_device_initialize
);
541 EXPORT_SYMBOL(class_device_add
);
542 EXPORT_SYMBOL(class_device_del
);
543 EXPORT_SYMBOL(class_device_get
);
544 EXPORT_SYMBOL(class_device_put
);
545 EXPORT_SYMBOL(class_device_create_file
);
546 EXPORT_SYMBOL(class_device_remove_file
);
548 EXPORT_SYMBOL(class_interface_register
);
549 EXPORT_SYMBOL(class_interface_unregister
);