Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
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> | |
e9ba6365 | 19 | #include <linux/err.h> |
1da177e4 LT |
20 | #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 | ||
25 | static ssize_t | |
26 | class_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); | |
4a0c20bf | 30 | ssize_t ret = -EIO; |
1da177e4 LT |
31 | |
32 | if (class_attr->show) | |
33 | ret = class_attr->show(dc, buf); | |
34 | return ret; | |
35 | } | |
36 | ||
37 | static ssize_t | |
38 | class_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); | |
4a0c20bf | 43 | ssize_t ret = -EIO; |
1da177e4 LT |
44 | |
45 | if (class_attr->store) | |
46 | ret = class_attr->store(dc, buf, count); | |
47 | return ret; | |
48 | } | |
49 | ||
50 | static 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 | ||
63 | static struct sysfs_ops class_sysfs_ops = { | |
64 | .show = class_attr_show, | |
65 | .store = class_attr_store, | |
66 | }; | |
67 | ||
68 | static 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 */ | |
74 | static decl_subsys(class, &ktype_class, NULL); | |
75 | ||
76 | ||
77 | int 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 | ||
87 | void 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 | ||
93 | struct 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 | ||
100 | void class_put(struct class * cls) | |
101 | { | |
102 | subsys_put(&cls->subsys); | |
103 | } | |
104 | ||
105 | ||
106 | static int add_class_attrs(struct class * cls) | |
107 | { | |
108 | int i; | |
109 | int error = 0; | |
110 | ||
111 | if (cls->class_attrs) { | |
112 | for (i = 0; attr_name(cls->class_attrs[i]); i++) { | |
113 | error = class_create_file(cls,&cls->class_attrs[i]); | |
114 | if (error) | |
115 | goto Err; | |
116 | } | |
117 | } | |
118 | Done: | |
119 | return error; | |
120 | Err: | |
121 | while (--i >= 0) | |
122 | class_remove_file(cls,&cls->class_attrs[i]); | |
123 | goto Done; | |
124 | } | |
125 | ||
126 | static void remove_class_attrs(struct class * cls) | |
127 | { | |
128 | int i; | |
129 | ||
130 | if (cls->class_attrs) { | |
131 | for (i = 0; attr_name(cls->class_attrs[i]); i++) | |
132 | class_remove_file(cls,&cls->class_attrs[i]); | |
133 | } | |
134 | } | |
135 | ||
136 | int class_register(struct class * cls) | |
137 | { | |
138 | int error; | |
139 | ||
140 | pr_debug("device class '%s': registering\n", cls->name); | |
141 | ||
142 | INIT_LIST_HEAD(&cls->children); | |
143 | INIT_LIST_HEAD(&cls->interfaces); | |
144 | init_MUTEX(&cls->sem); | |
145 | error = kobject_set_name(&cls->subsys.kset.kobj, "%s", cls->name); | |
146 | if (error) | |
147 | return error; | |
148 | ||
149 | subsys_set_kset(cls, class_subsys); | |
150 | ||
151 | error = subsystem_register(&cls->subsys); | |
152 | if (!error) { | |
153 | error = add_class_attrs(class_get(cls)); | |
154 | class_put(cls); | |
155 | } | |
156 | return error; | |
157 | } | |
158 | ||
159 | void class_unregister(struct class * cls) | |
160 | { | |
161 | pr_debug("device class '%s': unregistering\n", cls->name); | |
162 | remove_class_attrs(cls); | |
163 | subsystem_unregister(&cls->subsys); | |
164 | } | |
165 | ||
e9ba6365 | 166 | static void class_create_release(struct class *cls) |
167 | { | |
168 | kfree(cls); | |
169 | } | |
170 | ||
171 | static void class_device_create_release(struct class_device *class_dev) | |
172 | { | |
173 | kfree(class_dev); | |
174 | } | |
175 | ||
2fc68447 | 176 | /** |
177 | * class_create - create a struct class structure | |
178 | * @owner: pointer to the module that is to "own" this struct class | |
179 | * @name: pointer to a string for the name of this class. | |
180 | * | |
181 | * This is used to create a struct class pointer that can then be used | |
182 | * in calls to class_device_create(). | |
183 | * | |
184 | * Note, the pointer created here is to be destroyed when finished by | |
185 | * making a call to class_destroy(). | |
186 | */ | |
e9ba6365 | 187 | struct class *class_create(struct module *owner, char *name) |
188 | { | |
189 | struct class *cls; | |
190 | int retval; | |
191 | ||
192 | cls = kmalloc(sizeof(struct class), GFP_KERNEL); | |
193 | if (!cls) { | |
194 | retval = -ENOMEM; | |
195 | goto error; | |
196 | } | |
197 | memset(cls, 0x00, sizeof(struct class)); | |
198 | ||
199 | cls->name = name; | |
200 | cls->owner = owner; | |
201 | cls->class_release = class_create_release; | |
202 | cls->release = class_device_create_release; | |
203 | ||
204 | retval = class_register(cls); | |
205 | if (retval) | |
206 | goto error; | |
207 | ||
208 | return cls; | |
209 | ||
210 | error: | |
211 | kfree(cls); | |
212 | return ERR_PTR(retval); | |
213 | } | |
214 | ||
2fc68447 | 215 | /** |
216 | * class_destroy - destroys a struct class structure | |
217 | * @cs: pointer to the struct class that is to be destroyed | |
218 | * | |
219 | * Note, the pointer to be destroyed must have been created with a call | |
220 | * to class_create(). | |
221 | */ | |
e9ba6365 | 222 | void class_destroy(struct class *cls) |
223 | { | |
224 | if ((cls == NULL) || (IS_ERR(cls))) | |
225 | return; | |
226 | ||
227 | class_unregister(cls); | |
228 | } | |
1da177e4 LT |
229 | |
230 | /* Class Device Stuff */ | |
231 | ||
232 | int class_device_create_file(struct class_device * class_dev, | |
233 | const struct class_device_attribute * attr) | |
234 | { | |
235 | int error = -EINVAL; | |
236 | if (class_dev) | |
237 | error = sysfs_create_file(&class_dev->kobj, &attr->attr); | |
238 | return error; | |
239 | } | |
240 | ||
241 | void class_device_remove_file(struct class_device * class_dev, | |
242 | const struct class_device_attribute * attr) | |
243 | { | |
244 | if (class_dev) | |
245 | sysfs_remove_file(&class_dev->kobj, &attr->attr); | |
246 | } | |
247 | ||
248 | int class_device_create_bin_file(struct class_device *class_dev, | |
249 | struct bin_attribute *attr) | |
250 | { | |
251 | int error = -EINVAL; | |
252 | if (class_dev) | |
253 | error = sysfs_create_bin_file(&class_dev->kobj, attr); | |
254 | return error; | |
255 | } | |
256 | ||
257 | void class_device_remove_bin_file(struct class_device *class_dev, | |
258 | struct bin_attribute *attr) | |
259 | { | |
260 | if (class_dev) | |
261 | sysfs_remove_bin_file(&class_dev->kobj, attr); | |
262 | } | |
263 | ||
264 | static ssize_t | |
265 | class_device_attr_show(struct kobject * kobj, struct attribute * attr, | |
266 | char * buf) | |
267 | { | |
268 | struct class_device_attribute * class_dev_attr = to_class_dev_attr(attr); | |
269 | struct class_device * cd = to_class_dev(kobj); | |
270 | ssize_t ret = 0; | |
271 | ||
272 | if (class_dev_attr->show) | |
273 | ret = class_dev_attr->show(cd, buf); | |
274 | return ret; | |
275 | } | |
276 | ||
277 | static ssize_t | |
278 | class_device_attr_store(struct kobject * kobj, struct attribute * attr, | |
279 | const char * buf, size_t count) | |
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->store) | |
286 | ret = class_dev_attr->store(cd, buf, count); | |
287 | return ret; | |
288 | } | |
289 | ||
290 | static struct sysfs_ops class_dev_sysfs_ops = { | |
291 | .show = class_device_attr_show, | |
292 | .store = class_device_attr_store, | |
293 | }; | |
294 | ||
295 | static void class_dev_release(struct kobject * kobj) | |
296 | { | |
297 | struct class_device *cd = to_class_dev(kobj); | |
298 | struct class * cls = cd->class; | |
299 | ||
300 | pr_debug("device class '%s': release.\n", cd->class_id); | |
301 | ||
302 | if (cls->release) | |
303 | cls->release(cd); | |
304 | else { | |
305 | printk(KERN_ERR "Device class '%s' does not have a release() function, " | |
306 | "it is broken and must be fixed.\n", | |
307 | cd->class_id); | |
308 | WARN_ON(1); | |
309 | } | |
310 | } | |
311 | ||
312 | static struct kobj_type ktype_class_device = { | |
313 | .sysfs_ops = &class_dev_sysfs_ops, | |
314 | .release = class_dev_release, | |
315 | }; | |
316 | ||
317 | static int class_hotplug_filter(struct kset *kset, struct kobject *kobj) | |
318 | { | |
319 | struct kobj_type *ktype = get_ktype(kobj); | |
320 | ||
321 | if (ktype == &ktype_class_device) { | |
322 | struct class_device *class_dev = to_class_dev(kobj); | |
323 | if (class_dev->class) | |
324 | return 1; | |
325 | } | |
326 | return 0; | |
327 | } | |
328 | ||
419cab3f | 329 | static const char *class_hotplug_name(struct kset *kset, struct kobject *kobj) |
1da177e4 LT |
330 | { |
331 | struct class_device *class_dev = to_class_dev(kobj); | |
332 | ||
333 | return class_dev->class->name; | |
334 | } | |
335 | ||
336 | static int class_hotplug(struct kset *kset, struct kobject *kobj, char **envp, | |
337 | int num_envp, char *buffer, int buffer_size) | |
338 | { | |
339 | struct class_device *class_dev = to_class_dev(kobj); | |
340 | int i = 0; | |
341 | int length = 0; | |
342 | int retval = 0; | |
343 | ||
344 | pr_debug("%s - name = %s\n", __FUNCTION__, class_dev->class_id); | |
345 | ||
346 | if (class_dev->dev) { | |
347 | /* add physical device, backing this device */ | |
348 | struct device *dev = class_dev->dev; | |
349 | char *path = kobject_get_path(&dev->kobj, GFP_KERNEL); | |
350 | ||
351 | add_hotplug_env_var(envp, num_envp, &i, buffer, buffer_size, | |
352 | &length, "PHYSDEVPATH=%s", path); | |
353 | kfree(path); | |
354 | ||
355 | if (dev->bus) | |
356 | add_hotplug_env_var(envp, num_envp, &i, | |
357 | buffer, buffer_size, &length, | |
358 | "PHYSDEVBUS=%s", dev->bus->name); | |
359 | ||
360 | if (dev->driver) | |
361 | add_hotplug_env_var(envp, num_envp, &i, | |
362 | buffer, buffer_size, &length, | |
363 | "PHYSDEVDRIVER=%s", dev->driver->name); | |
364 | } | |
365 | ||
366 | if (MAJOR(class_dev->devt)) { | |
367 | add_hotplug_env_var(envp, num_envp, &i, | |
368 | buffer, buffer_size, &length, | |
369 | "MAJOR=%u", MAJOR(class_dev->devt)); | |
370 | ||
371 | add_hotplug_env_var(envp, num_envp, &i, | |
372 | buffer, buffer_size, &length, | |
373 | "MINOR=%u", MINOR(class_dev->devt)); | |
374 | } | |
375 | ||
376 | /* terminate, set to next free slot, shrink available space */ | |
377 | envp[i] = NULL; | |
378 | envp = &envp[i]; | |
379 | num_envp -= i; | |
380 | buffer = &buffer[length]; | |
381 | buffer_size -= length; | |
382 | ||
383 | if (class_dev->class->hotplug) { | |
384 | /* have the bus specific function add its stuff */ | |
385 | retval = class_dev->class->hotplug (class_dev, envp, num_envp, | |
386 | buffer, buffer_size); | |
387 | if (retval) { | |
388 | pr_debug ("%s - hotplug() returned %d\n", | |
389 | __FUNCTION__, retval); | |
390 | } | |
391 | } | |
392 | ||
393 | return retval; | |
394 | } | |
395 | ||
396 | static struct kset_hotplug_ops class_hotplug_ops = { | |
397 | .filter = class_hotplug_filter, | |
398 | .name = class_hotplug_name, | |
399 | .hotplug = class_hotplug, | |
400 | }; | |
401 | ||
402 | static decl_subsys(class_obj, &ktype_class_device, &class_hotplug_ops); | |
403 | ||
404 | ||
405 | static int class_device_add_attrs(struct class_device * cd) | |
406 | { | |
407 | int i; | |
408 | int error = 0; | |
409 | struct class * cls = cd->class; | |
410 | ||
411 | if (cls->class_dev_attrs) { | |
412 | for (i = 0; attr_name(cls->class_dev_attrs[i]); i++) { | |
413 | error = class_device_create_file(cd, | |
414 | &cls->class_dev_attrs[i]); | |
415 | if (error) | |
416 | goto Err; | |
417 | } | |
418 | } | |
419 | Done: | |
420 | return error; | |
421 | Err: | |
422 | while (--i >= 0) | |
423 | class_device_remove_file(cd,&cls->class_dev_attrs[i]); | |
424 | goto Done; | |
425 | } | |
426 | ||
427 | static void class_device_remove_attrs(struct class_device * cd) | |
428 | { | |
429 | int i; | |
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 | class_device_remove_file(cd,&cls->class_dev_attrs[i]); | |
435 | } | |
436 | } | |
437 | ||
438 | static ssize_t show_dev(struct class_device *class_dev, char *buf) | |
439 | { | |
440 | return print_dev_t(buf, class_dev->devt); | |
441 | } | |
1da177e4 LT |
442 | |
443 | void class_device_initialize(struct class_device *class_dev) | |
444 | { | |
445 | kobj_set_kset_s(class_dev, class_obj_subsys); | |
446 | kobject_init(&class_dev->kobj); | |
447 | INIT_LIST_HEAD(&class_dev->node); | |
448 | } | |
449 | ||
450 | int class_device_add(struct class_device *class_dev) | |
451 | { | |
452 | struct class * parent = NULL; | |
453 | struct class_interface * class_intf; | |
454 | int error; | |
455 | ||
456 | class_dev = class_device_get(class_dev); | |
457 | if (!class_dev) | |
458 | return -EINVAL; | |
459 | ||
460 | if (!strlen(class_dev->class_id)) { | |
461 | error = -EINVAL; | |
462 | goto register_done; | |
463 | } | |
464 | ||
465 | parent = class_get(class_dev->class); | |
466 | ||
467 | pr_debug("CLASS: registering class device: ID = '%s'\n", | |
468 | class_dev->class_id); | |
469 | ||
470 | /* first, register with generic layer. */ | |
471 | kobject_set_name(&class_dev->kobj, "%s", class_dev->class_id); | |
472 | if (parent) | |
473 | class_dev->kobj.parent = &parent->subsys.kset.kobj; | |
474 | ||
475 | if ((error = kobject_add(&class_dev->kobj))) | |
476 | goto register_done; | |
477 | ||
e9ba6365 | 478 | /* add the needed attributes to this device */ |
479 | if (MAJOR(class_dev->devt)) { | |
480 | struct class_device_attribute *attr; | |
481 | attr = kmalloc(sizeof(*attr), GFP_KERNEL); | |
482 | if (!attr) { | |
483 | error = -ENOMEM; | |
484 | kobject_del(&class_dev->kobj); | |
485 | goto register_done; | |
486 | } | |
487 | memset(attr, sizeof(*attr), 0x00); | |
488 | attr->attr.name = "dev"; | |
489 | attr->attr.mode = S_IRUGO; | |
490 | attr->attr.owner = parent->owner; | |
491 | attr->show = show_dev; | |
492 | attr->store = NULL; | |
493 | class_device_create_file(class_dev, attr); | |
494 | class_dev->devt_attr = attr; | |
495 | } | |
496 | ||
497 | class_device_add_attrs(class_dev); | |
498 | if (class_dev->dev) | |
499 | sysfs_create_link(&class_dev->kobj, | |
500 | &class_dev->dev->kobj, "device"); | |
501 | ||
502 | /* notify any interfaces this device is now here */ | |
1da177e4 LT |
503 | if (parent) { |
504 | down(&parent->sem); | |
505 | list_add_tail(&class_dev->node, &parent->children); | |
506 | list_for_each_entry(class_intf, &parent->interfaces, node) | |
507 | if (class_intf->add) | |
508 | class_intf->add(class_dev); | |
509 | up(&parent->sem); | |
510 | } | |
0700f56b | 511 | kobject_hotplug(&class_dev->kobj, KOBJ_ADD); |
e9ba6365 | 512 | |
1da177e4 LT |
513 | register_done: |
514 | if (error && parent) | |
515 | class_put(parent); | |
516 | class_device_put(class_dev); | |
517 | return error; | |
518 | } | |
519 | ||
520 | int class_device_register(struct class_device *class_dev) | |
521 | { | |
522 | class_device_initialize(class_dev); | |
523 | return class_device_add(class_dev); | |
524 | } | |
525 | ||
2fc68447 | 526 | /** |
527 | * class_device_create - creates a class device and registers it with sysfs | |
528 | * @cs: pointer to the struct class that this device should be registered to. | |
529 | * @dev: the dev_t for the char device to be added. | |
530 | * @device: a pointer to a struct device that is assiociated with this class device. | |
531 | * @fmt: string for the class device's name | |
532 | * | |
533 | * This function can be used by char device classes. A struct | |
534 | * class_device will be created in sysfs, registered to the specified | |
535 | * class. A "dev" file will be created, showing the dev_t for the | |
536 | * device. The pointer to the struct class_device will be returned from | |
537 | * the call. Any further sysfs files that might be required can be | |
538 | * created using this pointer. | |
539 | * | |
540 | * Note: the struct class passed to this function must have previously | |
541 | * been created with a call to class_create(). | |
542 | */ | |
e9ba6365 | 543 | struct class_device *class_device_create(struct class *cls, dev_t devt, |
544 | struct device *device, char *fmt, ...) | |
545 | { | |
546 | va_list args; | |
547 | struct class_device *class_dev = NULL; | |
548 | int retval = -ENODEV; | |
549 | ||
550 | if (cls == NULL || IS_ERR(cls)) | |
551 | goto error; | |
552 | ||
553 | class_dev = kmalloc(sizeof(struct class_device), GFP_KERNEL); | |
554 | if (!class_dev) { | |
555 | retval = -ENOMEM; | |
556 | goto error; | |
557 | } | |
558 | memset(class_dev, 0x00, sizeof(struct class_device)); | |
559 | ||
560 | class_dev->devt = devt; | |
561 | class_dev->dev = device; | |
562 | class_dev->class = cls; | |
563 | ||
564 | va_start(args, fmt); | |
565 | vsnprintf(class_dev->class_id, BUS_ID_SIZE, fmt, args); | |
566 | va_end(args); | |
567 | retval = class_device_register(class_dev); | |
568 | if (retval) | |
569 | goto error; | |
570 | ||
571 | return class_dev; | |
572 | ||
573 | error: | |
574 | kfree(class_dev); | |
575 | return ERR_PTR(retval); | |
576 | } | |
577 | ||
1da177e4 LT |
578 | void class_device_del(struct class_device *class_dev) |
579 | { | |
580 | struct class * parent = class_dev->class; | |
581 | struct class_interface * class_intf; | |
582 | ||
583 | if (parent) { | |
584 | down(&parent->sem); | |
585 | list_del_init(&class_dev->node); | |
586 | list_for_each_entry(class_intf, &parent->interfaces, node) | |
587 | if (class_intf->remove) | |
588 | class_intf->remove(class_dev); | |
589 | up(&parent->sem); | |
590 | } | |
591 | ||
592 | if (class_dev->dev) | |
593 | sysfs_remove_link(&class_dev->kobj, "device"); | |
e9ba6365 | 594 | if (class_dev->devt_attr) { |
595 | class_device_remove_file(class_dev, class_dev->devt_attr); | |
596 | kfree(class_dev->devt_attr); | |
597 | class_dev->devt_attr = NULL; | |
598 | } | |
1da177e4 LT |
599 | class_device_remove_attrs(class_dev); |
600 | ||
0700f56b | 601 | kobject_hotplug(&class_dev->kobj, KOBJ_REMOVE); |
1da177e4 LT |
602 | kobject_del(&class_dev->kobj); |
603 | ||
604 | if (parent) | |
605 | class_put(parent); | |
606 | } | |
607 | ||
608 | void class_device_unregister(struct class_device *class_dev) | |
609 | { | |
610 | pr_debug("CLASS: Unregistering class device. ID = '%s'\n", | |
611 | class_dev->class_id); | |
612 | class_device_del(class_dev); | |
613 | class_device_put(class_dev); | |
614 | } | |
615 | ||
2fc68447 | 616 | /** |
617 | * class_device_destroy - removes a class device that was created with class_device_create() | |
618 | * @cls: the pointer to the struct class that this device was registered * with. | |
619 | * @dev: the dev_t of the device that was previously registered. | |
620 | * | |
621 | * This call unregisters and cleans up a class device that was created with a | |
622 | * call to class_device_create() | |
623 | */ | |
e9ba6365 | 624 | void class_device_destroy(struct class *cls, dev_t devt) |
625 | { | |
626 | struct class_device *class_dev = NULL; | |
627 | struct class_device *class_dev_tmp; | |
628 | ||
629 | down(&cls->sem); | |
630 | list_for_each_entry(class_dev_tmp, &cls->children, node) { | |
631 | if (class_dev_tmp->devt == devt) { | |
632 | class_dev = class_dev_tmp; | |
633 | break; | |
634 | } | |
635 | } | |
636 | up(&cls->sem); | |
637 | ||
638 | if (class_dev) | |
639 | class_device_unregister(class_dev); | |
640 | } | |
641 | ||
1da177e4 LT |
642 | int class_device_rename(struct class_device *class_dev, char *new_name) |
643 | { | |
644 | int error = 0; | |
645 | ||
646 | class_dev = class_device_get(class_dev); | |
647 | if (!class_dev) | |
648 | return -EINVAL; | |
649 | ||
650 | pr_debug("CLASS: renaming '%s' to '%s'\n", class_dev->class_id, | |
651 | new_name); | |
652 | ||
653 | strlcpy(class_dev->class_id, new_name, KOBJ_NAME_LEN); | |
654 | ||
655 | error = kobject_rename(&class_dev->kobj, new_name); | |
656 | ||
657 | class_device_put(class_dev); | |
658 | ||
659 | return error; | |
660 | } | |
661 | ||
662 | struct class_device * class_device_get(struct class_device *class_dev) | |
663 | { | |
664 | if (class_dev) | |
665 | return to_class_dev(kobject_get(&class_dev->kobj)); | |
666 | return NULL; | |
667 | } | |
668 | ||
669 | void class_device_put(struct class_device *class_dev) | |
670 | { | |
671 | kobject_put(&class_dev->kobj); | |
672 | } | |
673 | ||
674 | ||
675 | int class_interface_register(struct class_interface *class_intf) | |
676 | { | |
677 | struct class *parent; | |
678 | struct class_device *class_dev; | |
679 | ||
680 | if (!class_intf || !class_intf->class) | |
681 | return -ENODEV; | |
682 | ||
683 | parent = class_get(class_intf->class); | |
684 | if (!parent) | |
685 | return -EINVAL; | |
686 | ||
687 | down(&parent->sem); | |
688 | list_add_tail(&class_intf->node, &parent->interfaces); | |
689 | if (class_intf->add) { | |
690 | list_for_each_entry(class_dev, &parent->children, node) | |
691 | class_intf->add(class_dev); | |
692 | } | |
693 | up(&parent->sem); | |
694 | ||
695 | return 0; | |
696 | } | |
697 | ||
698 | void class_interface_unregister(struct class_interface *class_intf) | |
699 | { | |
700 | struct class * parent = class_intf->class; | |
701 | struct class_device *class_dev; | |
702 | ||
703 | if (!parent) | |
704 | return; | |
705 | ||
706 | down(&parent->sem); | |
707 | list_del_init(&class_intf->node); | |
708 | if (class_intf->remove) { | |
709 | list_for_each_entry(class_dev, &parent->children, node) | |
710 | class_intf->remove(class_dev); | |
711 | } | |
712 | up(&parent->sem); | |
713 | ||
714 | class_put(parent); | |
715 | } | |
716 | ||
717 | ||
718 | ||
719 | int __init classes_init(void) | |
720 | { | |
721 | int retval; | |
722 | ||
723 | retval = subsystem_register(&class_subsys); | |
724 | if (retval) | |
725 | return retval; | |
726 | ||
727 | /* ick, this is ugly, the things we go through to keep from showing up | |
728 | * in sysfs... */ | |
729 | subsystem_init(&class_obj_subsys); | |
730 | if (!class_obj_subsys.kset.subsys) | |
731 | class_obj_subsys.kset.subsys = &class_obj_subsys; | |
732 | return 0; | |
733 | } | |
734 | ||
735 | EXPORT_SYMBOL_GPL(class_create_file); | |
736 | EXPORT_SYMBOL_GPL(class_remove_file); | |
737 | EXPORT_SYMBOL_GPL(class_register); | |
738 | EXPORT_SYMBOL_GPL(class_unregister); | |
739 | EXPORT_SYMBOL_GPL(class_get); | |
740 | EXPORT_SYMBOL_GPL(class_put); | |
e9ba6365 | 741 | EXPORT_SYMBOL_GPL(class_create); |
742 | EXPORT_SYMBOL_GPL(class_destroy); | |
1da177e4 LT |
743 | |
744 | EXPORT_SYMBOL_GPL(class_device_register); | |
745 | EXPORT_SYMBOL_GPL(class_device_unregister); | |
746 | EXPORT_SYMBOL_GPL(class_device_initialize); | |
747 | EXPORT_SYMBOL_GPL(class_device_add); | |
748 | EXPORT_SYMBOL_GPL(class_device_del); | |
749 | EXPORT_SYMBOL_GPL(class_device_get); | |
750 | EXPORT_SYMBOL_GPL(class_device_put); | |
e9ba6365 | 751 | EXPORT_SYMBOL_GPL(class_device_create); |
752 | EXPORT_SYMBOL_GPL(class_device_destroy); | |
1da177e4 LT |
753 | EXPORT_SYMBOL_GPL(class_device_create_file); |
754 | EXPORT_SYMBOL_GPL(class_device_remove_file); | |
755 | EXPORT_SYMBOL_GPL(class_device_create_bin_file); | |
756 | EXPORT_SYMBOL_GPL(class_device_remove_bin_file); | |
757 | ||
758 | EXPORT_SYMBOL_GPL(class_interface_register); | |
759 | EXPORT_SYMBOL_GPL(class_interface_unregister); |