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> |
4e57b681 | 20 | #include <linux/slab.h> |
1da177e4 LT |
21 | #include "base.h" |
22 | ||
23 | #define to_class_attr(_attr) container_of(_attr, struct class_attribute, attr) | |
24 | #define to_class(obj) container_of(obj, struct class, subsys.kset.kobj) | |
25 | ||
26 | static ssize_t | |
27 | class_attr_show(struct kobject * kobj, struct attribute * attr, char * buf) | |
28 | { | |
29 | struct class_attribute * class_attr = to_class_attr(attr); | |
30 | struct class * dc = to_class(kobj); | |
4a0c20bf | 31 | ssize_t ret = -EIO; |
1da177e4 LT |
32 | |
33 | if (class_attr->show) | |
34 | ret = class_attr->show(dc, buf); | |
35 | return ret; | |
36 | } | |
37 | ||
38 | static ssize_t | |
39 | class_attr_store(struct kobject * kobj, struct attribute * attr, | |
40 | const char * buf, size_t count) | |
41 | { | |
42 | struct class_attribute * class_attr = to_class_attr(attr); | |
43 | struct class * dc = to_class(kobj); | |
4a0c20bf | 44 | ssize_t ret = -EIO; |
1da177e4 LT |
45 | |
46 | if (class_attr->store) | |
47 | ret = class_attr->store(dc, buf, count); | |
48 | return ret; | |
49 | } | |
50 | ||
51 | static void class_release(struct kobject * kobj) | |
52 | { | |
53 | struct class *class = to_class(kobj); | |
54 | ||
55 | pr_debug("class '%s': release.\n", class->name); | |
56 | ||
57 | if (class->class_release) | |
58 | class->class_release(class); | |
59 | else | |
60 | pr_debug("class '%s' does not have a release() function, " | |
61 | "be careful\n", class->name); | |
62 | } | |
63 | ||
64 | static struct sysfs_ops class_sysfs_ops = { | |
65 | .show = class_attr_show, | |
66 | .store = class_attr_store, | |
67 | }; | |
68 | ||
69 | static struct kobj_type ktype_class = { | |
70 | .sysfs_ops = &class_sysfs_ops, | |
71 | .release = class_release, | |
72 | }; | |
73 | ||
74 | /* Hotplug events for classes go to the class_obj subsys */ | |
75 | static decl_subsys(class, &ktype_class, NULL); | |
76 | ||
77 | ||
78 | int class_create_file(struct class * cls, const struct class_attribute * attr) | |
79 | { | |
80 | int error; | |
81 | if (cls) { | |
82 | error = sysfs_create_file(&cls->subsys.kset.kobj, &attr->attr); | |
83 | } else | |
84 | error = -EINVAL; | |
85 | return error; | |
86 | } | |
87 | ||
88 | void class_remove_file(struct class * cls, const struct class_attribute * attr) | |
89 | { | |
90 | if (cls) | |
91 | sysfs_remove_file(&cls->subsys.kset.kobj, &attr->attr); | |
92 | } | |
93 | ||
1740757e | 94 | static struct class *class_get(struct class *cls) |
1da177e4 LT |
95 | { |
96 | if (cls) | |
97 | return container_of(subsys_get(&cls->subsys), struct class, subsys); | |
98 | return NULL; | |
99 | } | |
100 | ||
1740757e | 101 | static void class_put(struct class * cls) |
1da177e4 | 102 | { |
51d172d5 GKH |
103 | if (cls) |
104 | subsys_put(&cls->subsys); | |
1da177e4 LT |
105 | } |
106 | ||
107 | ||
108 | static int add_class_attrs(struct class * cls) | |
109 | { | |
110 | int i; | |
111 | int error = 0; | |
112 | ||
113 | if (cls->class_attrs) { | |
114 | for (i = 0; attr_name(cls->class_attrs[i]); i++) { | |
115 | error = class_create_file(cls,&cls->class_attrs[i]); | |
116 | if (error) | |
117 | goto Err; | |
118 | } | |
119 | } | |
120 | Done: | |
121 | return error; | |
122 | Err: | |
123 | while (--i >= 0) | |
124 | class_remove_file(cls,&cls->class_attrs[i]); | |
125 | goto Done; | |
126 | } | |
127 | ||
128 | static void remove_class_attrs(struct class * cls) | |
129 | { | |
130 | int i; | |
131 | ||
132 | if (cls->class_attrs) { | |
133 | for (i = 0; attr_name(cls->class_attrs[i]); i++) | |
134 | class_remove_file(cls,&cls->class_attrs[i]); | |
135 | } | |
136 | } | |
137 | ||
138 | int class_register(struct class * cls) | |
139 | { | |
140 | int error; | |
141 | ||
142 | pr_debug("device class '%s': registering\n", cls->name); | |
143 | ||
144 | INIT_LIST_HEAD(&cls->children); | |
145 | INIT_LIST_HEAD(&cls->interfaces); | |
146 | init_MUTEX(&cls->sem); | |
147 | error = kobject_set_name(&cls->subsys.kset.kobj, "%s", cls->name); | |
148 | if (error) | |
149 | return error; | |
150 | ||
151 | subsys_set_kset(cls, class_subsys); | |
152 | ||
153 | error = subsystem_register(&cls->subsys); | |
154 | if (!error) { | |
155 | error = add_class_attrs(class_get(cls)); | |
156 | class_put(cls); | |
157 | } | |
158 | return error; | |
159 | } | |
160 | ||
161 | void class_unregister(struct class * cls) | |
162 | { | |
163 | pr_debug("device class '%s': unregistering\n", cls->name); | |
164 | remove_class_attrs(cls); | |
165 | subsystem_unregister(&cls->subsys); | |
166 | } | |
167 | ||
e9ba6365 | 168 | static void class_create_release(struct class *cls) |
169 | { | |
51d172d5 | 170 | pr_debug("%s called for %s\n", __FUNCTION__, cls->name); |
e9ba6365 | 171 | kfree(cls); |
172 | } | |
173 | ||
174 | static void class_device_create_release(struct class_device *class_dev) | |
175 | { | |
51d172d5 | 176 | pr_debug("%s called for %s\n", __FUNCTION__, class_dev->class_id); |
e9ba6365 | 177 | kfree(class_dev); |
178 | } | |
179 | ||
51d172d5 | 180 | /* needed to allow these devices to have parent class devices */ |
312c004d | 181 | static int class_device_create_uevent(struct class_device *class_dev, |
51d172d5 GKH |
182 | char **envp, int num_envp, |
183 | char *buffer, int buffer_size) | |
184 | { | |
185 | pr_debug("%s called for %s\n", __FUNCTION__, class_dev->class_id); | |
186 | return 0; | |
187 | } | |
188 | ||
2fc68447 | 189 | /** |
190 | * class_create - create a struct class structure | |
191 | * @owner: pointer to the module that is to "own" this struct class | |
192 | * @name: pointer to a string for the name of this class. | |
193 | * | |
194 | * This is used to create a struct class pointer that can then be used | |
195 | * in calls to class_device_create(). | |
196 | * | |
197 | * Note, the pointer created here is to be destroyed when finished by | |
198 | * making a call to class_destroy(). | |
199 | */ | |
e9ba6365 | 200 | struct class *class_create(struct module *owner, char *name) |
201 | { | |
202 | struct class *cls; | |
203 | int retval; | |
204 | ||
4aed0644 | 205 | cls = kzalloc(sizeof(*cls), GFP_KERNEL); |
e9ba6365 | 206 | if (!cls) { |
207 | retval = -ENOMEM; | |
208 | goto error; | |
209 | } | |
e9ba6365 | 210 | |
211 | cls->name = name; | |
212 | cls->owner = owner; | |
213 | cls->class_release = class_create_release; | |
214 | cls->release = class_device_create_release; | |
215 | ||
216 | retval = class_register(cls); | |
217 | if (retval) | |
218 | goto error; | |
219 | ||
220 | return cls; | |
221 | ||
222 | error: | |
223 | kfree(cls); | |
224 | return ERR_PTR(retval); | |
225 | } | |
226 | ||
2fc68447 | 227 | /** |
228 | * class_destroy - destroys a struct class structure | |
229 | * @cs: pointer to the struct class that is to be destroyed | |
230 | * | |
231 | * Note, the pointer to be destroyed must have been created with a call | |
232 | * to class_create(). | |
233 | */ | |
e9ba6365 | 234 | void class_destroy(struct class *cls) |
235 | { | |
236 | if ((cls == NULL) || (IS_ERR(cls))) | |
237 | return; | |
238 | ||
239 | class_unregister(cls); | |
240 | } | |
1da177e4 LT |
241 | |
242 | /* Class Device Stuff */ | |
243 | ||
244 | int class_device_create_file(struct class_device * class_dev, | |
245 | const struct class_device_attribute * attr) | |
246 | { | |
247 | int error = -EINVAL; | |
248 | if (class_dev) | |
249 | error = sysfs_create_file(&class_dev->kobj, &attr->attr); | |
250 | return error; | |
251 | } | |
252 | ||
253 | void class_device_remove_file(struct class_device * class_dev, | |
254 | const struct class_device_attribute * attr) | |
255 | { | |
256 | if (class_dev) | |
257 | sysfs_remove_file(&class_dev->kobj, &attr->attr); | |
258 | } | |
259 | ||
260 | int class_device_create_bin_file(struct class_device *class_dev, | |
261 | struct bin_attribute *attr) | |
262 | { | |
263 | int error = -EINVAL; | |
264 | if (class_dev) | |
265 | error = sysfs_create_bin_file(&class_dev->kobj, attr); | |
266 | return error; | |
267 | } | |
268 | ||
269 | void class_device_remove_bin_file(struct class_device *class_dev, | |
270 | struct bin_attribute *attr) | |
271 | { | |
272 | if (class_dev) | |
273 | sysfs_remove_bin_file(&class_dev->kobj, attr); | |
274 | } | |
275 | ||
276 | static ssize_t | |
277 | class_device_attr_show(struct kobject * kobj, struct attribute * attr, | |
278 | char * buf) | |
279 | { | |
280 | struct class_device_attribute * class_dev_attr = to_class_dev_attr(attr); | |
281 | struct class_device * cd = to_class_dev(kobj); | |
282 | ssize_t ret = 0; | |
283 | ||
284 | if (class_dev_attr->show) | |
285 | ret = class_dev_attr->show(cd, buf); | |
286 | return ret; | |
287 | } | |
288 | ||
289 | static ssize_t | |
290 | class_device_attr_store(struct kobject * kobj, struct attribute * attr, | |
291 | const char * buf, size_t count) | |
292 | { | |
293 | struct class_device_attribute * class_dev_attr = to_class_dev_attr(attr); | |
294 | struct class_device * cd = to_class_dev(kobj); | |
295 | ssize_t ret = 0; | |
296 | ||
297 | if (class_dev_attr->store) | |
298 | ret = class_dev_attr->store(cd, buf, count); | |
299 | return ret; | |
300 | } | |
301 | ||
302 | static struct sysfs_ops class_dev_sysfs_ops = { | |
303 | .show = class_device_attr_show, | |
304 | .store = class_device_attr_store, | |
305 | }; | |
306 | ||
307 | static void class_dev_release(struct kobject * kobj) | |
308 | { | |
309 | struct class_device *cd = to_class_dev(kobj); | |
310 | struct class * cls = cd->class; | |
311 | ||
312 | pr_debug("device class '%s': release.\n", cd->class_id); | |
313 | ||
fef6ec8d JJ |
314 | kfree(cd->devt_attr); |
315 | cd->devt_attr = NULL; | |
208f3d61 | 316 | |
51d172d5 GKH |
317 | if (cd->release) |
318 | cd->release(cd); | |
319 | else if (cls->release) | |
1da177e4 LT |
320 | cls->release(cd); |
321 | else { | |
51d172d5 | 322 | printk(KERN_ERR "Class Device '%s' does not have a release() function, " |
1da177e4 LT |
323 | "it is broken and must be fixed.\n", |
324 | cd->class_id); | |
325 | WARN_ON(1); | |
326 | } | |
327 | } | |
328 | ||
329 | static struct kobj_type ktype_class_device = { | |
330 | .sysfs_ops = &class_dev_sysfs_ops, | |
331 | .release = class_dev_release, | |
332 | }; | |
333 | ||
312c004d | 334 | static int class_uevent_filter(struct kset *kset, struct kobject *kobj) |
1da177e4 LT |
335 | { |
336 | struct kobj_type *ktype = get_ktype(kobj); | |
337 | ||
338 | if (ktype == &ktype_class_device) { | |
339 | struct class_device *class_dev = to_class_dev(kobj); | |
340 | if (class_dev->class) | |
341 | return 1; | |
342 | } | |
343 | return 0; | |
344 | } | |
345 | ||
312c004d | 346 | static const char *class_uevent_name(struct kset *kset, struct kobject *kobj) |
1da177e4 LT |
347 | { |
348 | struct class_device *class_dev = to_class_dev(kobj); | |
349 | ||
350 | return class_dev->class->name; | |
351 | } | |
352 | ||
312c004d | 353 | static int class_uevent(struct kset *kset, struct kobject *kobj, char **envp, |
1da177e4 LT |
354 | int num_envp, char *buffer, int buffer_size) |
355 | { | |
356 | struct class_device *class_dev = to_class_dev(kobj); | |
357 | int i = 0; | |
358 | int length = 0; | |
359 | int retval = 0; | |
360 | ||
361 | pr_debug("%s - name = %s\n", __FUNCTION__, class_dev->class_id); | |
362 | ||
363 | if (class_dev->dev) { | |
364 | /* add physical device, backing this device */ | |
365 | struct device *dev = class_dev->dev; | |
366 | char *path = kobject_get_path(&dev->kobj, GFP_KERNEL); | |
367 | ||
312c004d KS |
368 | add_uevent_var(envp, num_envp, &i, buffer, buffer_size, |
369 | &length, "PHYSDEVPATH=%s", path); | |
1da177e4 LT |
370 | kfree(path); |
371 | ||
372 | if (dev->bus) | |
312c004d KS |
373 | add_uevent_var(envp, num_envp, &i, |
374 | buffer, buffer_size, &length, | |
375 | "PHYSDEVBUS=%s", dev->bus->name); | |
1da177e4 LT |
376 | |
377 | if (dev->driver) | |
312c004d KS |
378 | add_uevent_var(envp, num_envp, &i, |
379 | buffer, buffer_size, &length, | |
380 | "PHYSDEVDRIVER=%s", dev->driver->name); | |
1da177e4 LT |
381 | } |
382 | ||
383 | if (MAJOR(class_dev->devt)) { | |
312c004d KS |
384 | add_uevent_var(envp, num_envp, &i, |
385 | buffer, buffer_size, &length, | |
386 | "MAJOR=%u", MAJOR(class_dev->devt)); | |
1da177e4 | 387 | |
312c004d KS |
388 | add_uevent_var(envp, num_envp, &i, |
389 | buffer, buffer_size, &length, | |
390 | "MINOR=%u", MINOR(class_dev->devt)); | |
1da177e4 LT |
391 | } |
392 | ||
393 | /* terminate, set to next free slot, shrink available space */ | |
394 | envp[i] = NULL; | |
395 | envp = &envp[i]; | |
396 | num_envp -= i; | |
397 | buffer = &buffer[length]; | |
398 | buffer_size -= length; | |
399 | ||
312c004d | 400 | if (class_dev->uevent) { |
51d172d5 | 401 | /* have the class device specific function add its stuff */ |
312c004d | 402 | retval = class_dev->uevent(class_dev, envp, num_envp, |
51d172d5 GKH |
403 | buffer, buffer_size); |
404 | if (retval) | |
312c004d KS |
405 | pr_debug("class_dev->uevent() returned %d\n", retval); |
406 | } else if (class_dev->class->uevent) { | |
51d172d5 | 407 | /* have the class specific function add its stuff */ |
312c004d | 408 | retval = class_dev->class->uevent(class_dev, envp, num_envp, |
51d172d5 GKH |
409 | buffer, buffer_size); |
410 | if (retval) | |
312c004d | 411 | pr_debug("class->uevent() returned %d\n", retval); |
1da177e4 LT |
412 | } |
413 | ||
414 | return retval; | |
415 | } | |
416 | ||
312c004d KS |
417 | static struct kset_uevent_ops class_uevent_ops = { |
418 | .filter = class_uevent_filter, | |
419 | .name = class_uevent_name, | |
420 | .uevent = class_uevent, | |
1da177e4 LT |
421 | }; |
422 | ||
312c004d | 423 | static decl_subsys(class_obj, &ktype_class_device, &class_uevent_ops); |
1da177e4 LT |
424 | |
425 | ||
426 | static int class_device_add_attrs(struct class_device * cd) | |
427 | { | |
428 | int i; | |
429 | int error = 0; | |
430 | struct class * cls = cd->class; | |
431 | ||
432 | if (cls->class_dev_attrs) { | |
433 | for (i = 0; attr_name(cls->class_dev_attrs[i]); i++) { | |
434 | error = class_device_create_file(cd, | |
435 | &cls->class_dev_attrs[i]); | |
436 | if (error) | |
437 | goto Err; | |
438 | } | |
439 | } | |
440 | Done: | |
441 | return error; | |
442 | Err: | |
443 | while (--i >= 0) | |
444 | class_device_remove_file(cd,&cls->class_dev_attrs[i]); | |
445 | goto Done; | |
446 | } | |
447 | ||
448 | static void class_device_remove_attrs(struct class_device * cd) | |
449 | { | |
450 | int i; | |
451 | struct class * cls = cd->class; | |
452 | ||
453 | if (cls->class_dev_attrs) { | |
454 | for (i = 0; attr_name(cls->class_dev_attrs[i]); i++) | |
455 | class_device_remove_file(cd,&cls->class_dev_attrs[i]); | |
456 | } | |
457 | } | |
458 | ||
1498221d SH |
459 | static int class_device_add_groups(struct class_device * cd) |
460 | { | |
461 | int i; | |
462 | int error = 0; | |
463 | ||
464 | if (cd->groups) { | |
465 | for (i = 0; cd->groups[i]; i++) { | |
466 | error = sysfs_create_group(&cd->kobj, cd->groups[i]); | |
467 | if (error) { | |
468 | while (--i >= 0) | |
469 | sysfs_remove_group(&cd->kobj, cd->groups[i]); | |
470 | goto out; | |
471 | } | |
472 | } | |
473 | } | |
474 | out: | |
475 | return error; | |
476 | } | |
477 | ||
478 | static void class_device_remove_groups(struct class_device * cd) | |
479 | { | |
480 | int i; | |
481 | if (cd->groups) { | |
482 | for (i = 0; cd->groups[i]; i++) { | |
483 | sysfs_remove_group(&cd->kobj, cd->groups[i]); | |
484 | } | |
485 | } | |
486 | } | |
487 | ||
1da177e4 LT |
488 | static ssize_t show_dev(struct class_device *class_dev, char *buf) |
489 | { | |
490 | return print_dev_t(buf, class_dev->devt); | |
491 | } | |
1da177e4 | 492 | |
a7fd6706 KS |
493 | static ssize_t store_uevent(struct class_device *class_dev, |
494 | const char *buf, size_t count) | |
495 | { | |
312c004d | 496 | kobject_uevent(&class_dev->kobj, KOBJ_ADD); |
a7fd6706 KS |
497 | return count; |
498 | } | |
499 | ||
1da177e4 LT |
500 | void class_device_initialize(struct class_device *class_dev) |
501 | { | |
502 | kobj_set_kset_s(class_dev, class_obj_subsys); | |
503 | kobject_init(&class_dev->kobj); | |
504 | INIT_LIST_HEAD(&class_dev->node); | |
505 | } | |
506 | ||
76d1ce00 DT |
507 | static char *make_class_name(struct class_device *class_dev) |
508 | { | |
509 | char *name; | |
510 | int size; | |
511 | ||
512 | size = strlen(class_dev->class->name) + | |
513 | strlen(kobject_name(&class_dev->kobj)) + 2; | |
514 | ||
515 | name = kmalloc(size, GFP_KERNEL); | |
516 | if (!name) | |
517 | return ERR_PTR(-ENOMEM); | |
518 | ||
519 | strcpy(name, class_dev->class->name); | |
520 | strcat(name, ":"); | |
521 | strcat(name, kobject_name(&class_dev->kobj)); | |
522 | return name; | |
523 | } | |
524 | ||
1da177e4 LT |
525 | int class_device_add(struct class_device *class_dev) |
526 | { | |
51d172d5 GKH |
527 | struct class *parent_class = NULL; |
528 | struct class_device *parent_class_dev = NULL; | |
529 | struct class_interface *class_intf; | |
76d1ce00 | 530 | char *class_name = NULL; |
51d172d5 | 531 | int error = -EINVAL; |
1da177e4 LT |
532 | |
533 | class_dev = class_device_get(class_dev); | |
534 | if (!class_dev) | |
535 | return -EINVAL; | |
536 | ||
51d172d5 | 537 | if (!strlen(class_dev->class_id)) |
b7fe4a60 | 538 | goto out1; |
1da177e4 | 539 | |
51d172d5 GKH |
540 | parent_class = class_get(class_dev->class); |
541 | if (!parent_class) | |
b7fe4a60 SH |
542 | goto out1; |
543 | ||
51d172d5 | 544 | parent_class_dev = class_device_get(class_dev->parent); |
1da177e4 LT |
545 | |
546 | pr_debug("CLASS: registering class device: ID = '%s'\n", | |
547 | class_dev->class_id); | |
548 | ||
549 | /* first, register with generic layer. */ | |
b7fe4a60 SH |
550 | error = kobject_set_name(&class_dev->kobj, "%s", class_dev->class_id); |
551 | if (error) | |
552 | goto out2; | |
553 | ||
51d172d5 GKH |
554 | if (parent_class_dev) |
555 | class_dev->kobj.parent = &parent_class_dev->kobj; | |
556 | else | |
557 | class_dev->kobj.parent = &parent_class->subsys.kset.kobj; | |
1da177e4 | 558 | |
51d172d5 GKH |
559 | error = kobject_add(&class_dev->kobj); |
560 | if (error) | |
b7fe4a60 | 561 | goto out2; |
1da177e4 | 562 | |
e9ba6365 | 563 | /* add the needed attributes to this device */ |
a7fd6706 KS |
564 | class_dev->uevent_attr.attr.name = "uevent"; |
565 | class_dev->uevent_attr.attr.mode = S_IWUSR; | |
51d172d5 | 566 | class_dev->uevent_attr.attr.owner = parent_class->owner; |
a7fd6706 | 567 | class_dev->uevent_attr.store = store_uevent; |
b7fe4a60 SH |
568 | error = class_device_create_file(class_dev, &class_dev->uevent_attr); |
569 | if (error) | |
570 | goto out3; | |
a7fd6706 | 571 | |
e9ba6365 | 572 | if (MAJOR(class_dev->devt)) { |
573 | struct class_device_attribute *attr; | |
4aed0644 | 574 | attr = kzalloc(sizeof(*attr), GFP_KERNEL); |
e9ba6365 | 575 | if (!attr) { |
576 | error = -ENOMEM; | |
b7fe4a60 | 577 | goto out4; |
e9ba6365 | 578 | } |
e9ba6365 | 579 | attr->attr.name = "dev"; |
580 | attr->attr.mode = S_IRUGO; | |
51d172d5 | 581 | attr->attr.owner = parent_class->owner; |
e9ba6365 | 582 | attr->show = show_dev; |
b7fe4a60 SH |
583 | error = class_device_create_file(class_dev, attr); |
584 | if (error) { | |
585 | kfree(attr); | |
586 | goto out4; | |
587 | } | |
588 | ||
e9ba6365 | 589 | class_dev->devt_attr = attr; |
590 | } | |
591 | ||
b7fe4a60 SH |
592 | error = class_device_add_attrs(class_dev); |
593 | if (error) | |
594 | goto out5; | |
595 | ||
76d1ce00 DT |
596 | if (class_dev->dev) { |
597 | class_name = make_class_name(class_dev); | |
b7fe4a60 SH |
598 | error = sysfs_create_link(&class_dev->kobj, |
599 | &class_dev->dev->kobj, "device"); | |
600 | if (error) | |
601 | goto out6; | |
602 | error = sysfs_create_link(&class_dev->dev->kobj, &class_dev->kobj, | |
603 | class_name); | |
604 | if (error) | |
605 | goto out7; | |
76d1ce00 | 606 | } |
e9ba6365 | 607 | |
b7fe4a60 SH |
608 | error = class_device_add_groups(class_dev); |
609 | if (error) | |
610 | goto out8; | |
1498221d | 611 | |
312c004d | 612 | kobject_uevent(&class_dev->kobj, KOBJ_ADD); |
dbe9035d | 613 | |
e9ba6365 | 614 | /* notify any interfaces this device is now here */ |
a1438890 J |
615 | down(&parent_class->sem); |
616 | list_add_tail(&class_dev->node, &parent_class->children); | |
617 | list_for_each_entry(class_intf, &parent_class->interfaces, node) { | |
618 | if (class_intf->add) | |
619 | class_intf->add(class_dev, class_intf); | |
1da177e4 | 620 | } |
a1438890 | 621 | up(&parent_class->sem); |
e9ba6365 | 622 | |
b7fe4a60 SH |
623 | goto out1; |
624 | ||
625 | out8: | |
626 | if (class_dev->dev) | |
627 | sysfs_remove_link(&class_dev->kobj, class_name); | |
628 | out7: | |
629 | if (class_dev->dev) | |
630 | sysfs_remove_link(&class_dev->kobj, "device"); | |
631 | out6: | |
632 | class_device_remove_attrs(class_dev); | |
633 | out5: | |
634 | if (class_dev->devt_attr) | |
635 | class_device_remove_file(class_dev, class_dev->devt_attr); | |
636 | out4: | |
637 | class_device_remove_file(class_dev, &class_dev->uevent_attr); | |
638 | out3: | |
639 | kobject_del(&class_dev->kobj); | |
640 | out2: | |
641 | if(parent_class_dev) | |
51d172d5 | 642 | class_device_put(parent_class_dev); |
b7fe4a60 SH |
643 | class_put(parent_class); |
644 | out1: | |
1da177e4 | 645 | class_device_put(class_dev); |
76d1ce00 | 646 | kfree(class_name); |
1da177e4 LT |
647 | return error; |
648 | } | |
649 | ||
650 | int class_device_register(struct class_device *class_dev) | |
651 | { | |
652 | class_device_initialize(class_dev); | |
653 | return class_device_add(class_dev); | |
654 | } | |
655 | ||
2fc68447 | 656 | /** |
657 | * class_device_create - creates a class device and registers it with sysfs | |
658 | * @cs: pointer to the struct class that this device should be registered to. | |
51d172d5 | 659 | * @parent: pointer to the parent struct class_device of this new device, if any. |
2fc68447 | 660 | * @dev: the dev_t for the char device to be added. |
661 | * @device: a pointer to a struct device that is assiociated with this class device. | |
662 | * @fmt: string for the class device's name | |
663 | * | |
664 | * This function can be used by char device classes. A struct | |
665 | * class_device will be created in sysfs, registered to the specified | |
51d172d5 GKH |
666 | * class. |
667 | * A "dev" file will be created, showing the dev_t for the device, if | |
668 | * the dev_t is not 0,0. | |
669 | * If a pointer to a parent struct class_device is passed in, the newly | |
670 | * created struct class_device will be a child of that device in sysfs. | |
671 | * The pointer to the struct class_device will be returned from the | |
672 | * call. Any further sysfs files that might be required can be created | |
673 | * using this pointer. | |
2fc68447 | 674 | * |
675 | * Note: the struct class passed to this function must have previously | |
676 | * been created with a call to class_create(). | |
677 | */ | |
51d172d5 GKH |
678 | struct class_device *class_device_create(struct class *cls, |
679 | struct class_device *parent, | |
680 | dev_t devt, | |
e9ba6365 | 681 | struct device *device, char *fmt, ...) |
682 | { | |
683 | va_list args; | |
684 | struct class_device *class_dev = NULL; | |
685 | int retval = -ENODEV; | |
686 | ||
687 | if (cls == NULL || IS_ERR(cls)) | |
688 | goto error; | |
689 | ||
4aed0644 | 690 | class_dev = kzalloc(sizeof(*class_dev), GFP_KERNEL); |
e9ba6365 | 691 | if (!class_dev) { |
692 | retval = -ENOMEM; | |
693 | goto error; | |
694 | } | |
e9ba6365 | 695 | |
696 | class_dev->devt = devt; | |
697 | class_dev->dev = device; | |
698 | class_dev->class = cls; | |
51d172d5 GKH |
699 | class_dev->parent = parent; |
700 | class_dev->release = class_device_create_release; | |
312c004d | 701 | class_dev->uevent = class_device_create_uevent; |
e9ba6365 | 702 | |
703 | va_start(args, fmt); | |
704 | vsnprintf(class_dev->class_id, BUS_ID_SIZE, fmt, args); | |
705 | va_end(args); | |
706 | retval = class_device_register(class_dev); | |
707 | if (retval) | |
708 | goto error; | |
709 | ||
710 | return class_dev; | |
711 | ||
712 | error: | |
713 | kfree(class_dev); | |
714 | return ERR_PTR(retval); | |
715 | } | |
716 | ||
1da177e4 LT |
717 | void class_device_del(struct class_device *class_dev) |
718 | { | |
51d172d5 GKH |
719 | struct class *parent_class = class_dev->class; |
720 | struct class_device *parent_device = class_dev->parent; | |
721 | struct class_interface *class_intf; | |
76d1ce00 | 722 | char *class_name = NULL; |
1da177e4 | 723 | |
51d172d5 GKH |
724 | if (parent_class) { |
725 | down(&parent_class->sem); | |
1da177e4 | 726 | list_del_init(&class_dev->node); |
51d172d5 | 727 | list_for_each_entry(class_intf, &parent_class->interfaces, node) |
1da177e4 | 728 | if (class_intf->remove) |
d8539d81 | 729 | class_intf->remove(class_dev, class_intf); |
51d172d5 | 730 | up(&parent_class->sem); |
1da177e4 LT |
731 | } |
732 | ||
76d1ce00 DT |
733 | if (class_dev->dev) { |
734 | class_name = make_class_name(class_dev); | |
1da177e4 | 735 | sysfs_remove_link(&class_dev->kobj, "device"); |
76d1ce00 DT |
736 | sysfs_remove_link(&class_dev->dev->kobj, class_name); |
737 | } | |
a7fd6706 | 738 | class_device_remove_file(class_dev, &class_dev->uevent_attr); |
208f3d61 | 739 | if (class_dev->devt_attr) |
e9ba6365 | 740 | class_device_remove_file(class_dev, class_dev->devt_attr); |
1da177e4 | 741 | class_device_remove_attrs(class_dev); |
1498221d | 742 | class_device_remove_groups(class_dev); |
1da177e4 | 743 | |
312c004d | 744 | kobject_uevent(&class_dev->kobj, KOBJ_REMOVE); |
1da177e4 LT |
745 | kobject_del(&class_dev->kobj); |
746 | ||
51d172d5 GKH |
747 | class_device_put(parent_device); |
748 | class_put(parent_class); | |
76d1ce00 | 749 | kfree(class_name); |
1da177e4 LT |
750 | } |
751 | ||
752 | void class_device_unregister(struct class_device *class_dev) | |
753 | { | |
754 | pr_debug("CLASS: Unregistering class device. ID = '%s'\n", | |
755 | class_dev->class_id); | |
756 | class_device_del(class_dev); | |
757 | class_device_put(class_dev); | |
758 | } | |
759 | ||
2fc68447 | 760 | /** |
761 | * class_device_destroy - removes a class device that was created with class_device_create() | |
762 | * @cls: the pointer to the struct class that this device was registered * with. | |
763 | * @dev: the dev_t of the device that was previously registered. | |
764 | * | |
765 | * This call unregisters and cleans up a class device that was created with a | |
766 | * call to class_device_create() | |
767 | */ | |
e9ba6365 | 768 | void class_device_destroy(struct class *cls, dev_t devt) |
769 | { | |
770 | struct class_device *class_dev = NULL; | |
771 | struct class_device *class_dev_tmp; | |
772 | ||
773 | down(&cls->sem); | |
774 | list_for_each_entry(class_dev_tmp, &cls->children, node) { | |
775 | if (class_dev_tmp->devt == devt) { | |
776 | class_dev = class_dev_tmp; | |
777 | break; | |
778 | } | |
779 | } | |
780 | up(&cls->sem); | |
781 | ||
782 | if (class_dev) | |
783 | class_device_unregister(class_dev); | |
784 | } | |
785 | ||
1da177e4 LT |
786 | int class_device_rename(struct class_device *class_dev, char *new_name) |
787 | { | |
788 | int error = 0; | |
3e51377d | 789 | char *old_class_name = NULL, *new_class_name = NULL; |
1da177e4 LT |
790 | |
791 | class_dev = class_device_get(class_dev); | |
792 | if (!class_dev) | |
793 | return -EINVAL; | |
794 | ||
795 | pr_debug("CLASS: renaming '%s' to '%s'\n", class_dev->class_id, | |
796 | new_name); | |
797 | ||
3e51377d BN |
798 | if (class_dev->dev) |
799 | old_class_name = make_class_name(class_dev); | |
800 | ||
1da177e4 LT |
801 | strlcpy(class_dev->class_id, new_name, KOBJ_NAME_LEN); |
802 | ||
803 | error = kobject_rename(&class_dev->kobj, new_name); | |
804 | ||
3e51377d BN |
805 | if (class_dev->dev) { |
806 | new_class_name = make_class_name(class_dev); | |
807 | sysfs_create_link(&class_dev->dev->kobj, &class_dev->kobj, | |
808 | new_class_name); | |
809 | sysfs_remove_link(&class_dev->dev->kobj, old_class_name); | |
810 | } | |
1da177e4 LT |
811 | class_device_put(class_dev); |
812 | ||
3e51377d BN |
813 | kfree(old_class_name); |
814 | kfree(new_class_name); | |
815 | ||
1da177e4 LT |
816 | return error; |
817 | } | |
818 | ||
819 | struct class_device * class_device_get(struct class_device *class_dev) | |
820 | { | |
821 | if (class_dev) | |
822 | return to_class_dev(kobject_get(&class_dev->kobj)); | |
823 | return NULL; | |
824 | } | |
825 | ||
826 | void class_device_put(struct class_device *class_dev) | |
827 | { | |
51d172d5 GKH |
828 | if (class_dev) |
829 | kobject_put(&class_dev->kobj); | |
1da177e4 LT |
830 | } |
831 | ||
832 | ||
833 | int class_interface_register(struct class_interface *class_intf) | |
834 | { | |
835 | struct class *parent; | |
836 | struct class_device *class_dev; | |
837 | ||
838 | if (!class_intf || !class_intf->class) | |
839 | return -ENODEV; | |
840 | ||
841 | parent = class_get(class_intf->class); | |
842 | if (!parent) | |
843 | return -EINVAL; | |
844 | ||
845 | down(&parent->sem); | |
846 | list_add_tail(&class_intf->node, &parent->interfaces); | |
847 | if (class_intf->add) { | |
848 | list_for_each_entry(class_dev, &parent->children, node) | |
d8539d81 | 849 | class_intf->add(class_dev, class_intf); |
1da177e4 LT |
850 | } |
851 | up(&parent->sem); | |
852 | ||
853 | return 0; | |
854 | } | |
855 | ||
856 | void class_interface_unregister(struct class_interface *class_intf) | |
857 | { | |
858 | struct class * parent = class_intf->class; | |
859 | struct class_device *class_dev; | |
860 | ||
861 | if (!parent) | |
862 | return; | |
863 | ||
864 | down(&parent->sem); | |
865 | list_del_init(&class_intf->node); | |
866 | if (class_intf->remove) { | |
867 | list_for_each_entry(class_dev, &parent->children, node) | |
d8539d81 | 868 | class_intf->remove(class_dev, class_intf); |
1da177e4 LT |
869 | } |
870 | up(&parent->sem); | |
871 | ||
872 | class_put(parent); | |
873 | } | |
874 | ||
875 | ||
876 | ||
877 | int __init classes_init(void) | |
878 | { | |
879 | int retval; | |
880 | ||
881 | retval = subsystem_register(&class_subsys); | |
882 | if (retval) | |
883 | return retval; | |
884 | ||
885 | /* ick, this is ugly, the things we go through to keep from showing up | |
886 | * in sysfs... */ | |
887 | subsystem_init(&class_obj_subsys); | |
888 | if (!class_obj_subsys.kset.subsys) | |
889 | class_obj_subsys.kset.subsys = &class_obj_subsys; | |
890 | return 0; | |
891 | } | |
892 | ||
893 | EXPORT_SYMBOL_GPL(class_create_file); | |
894 | EXPORT_SYMBOL_GPL(class_remove_file); | |
895 | EXPORT_SYMBOL_GPL(class_register); | |
896 | EXPORT_SYMBOL_GPL(class_unregister); | |
e9ba6365 | 897 | EXPORT_SYMBOL_GPL(class_create); |
898 | EXPORT_SYMBOL_GPL(class_destroy); | |
1da177e4 LT |
899 | |
900 | EXPORT_SYMBOL_GPL(class_device_register); | |
901 | EXPORT_SYMBOL_GPL(class_device_unregister); | |
902 | EXPORT_SYMBOL_GPL(class_device_initialize); | |
903 | EXPORT_SYMBOL_GPL(class_device_add); | |
904 | EXPORT_SYMBOL_GPL(class_device_del); | |
905 | EXPORT_SYMBOL_GPL(class_device_get); | |
906 | EXPORT_SYMBOL_GPL(class_device_put); | |
e9ba6365 | 907 | EXPORT_SYMBOL_GPL(class_device_create); |
908 | EXPORT_SYMBOL_GPL(class_device_destroy); | |
1da177e4 LT |
909 | EXPORT_SYMBOL_GPL(class_device_create_file); |
910 | EXPORT_SYMBOL_GPL(class_device_remove_file); | |
911 | EXPORT_SYMBOL_GPL(class_device_create_bin_file); | |
912 | EXPORT_SYMBOL_GPL(class_device_remove_bin_file); | |
913 | ||
914 | EXPORT_SYMBOL_GPL(class_interface_register); | |
915 | EXPORT_SYMBOL_GPL(class_interface_unregister); |