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