Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * drivers/base/core.c - core driver model code (device registration, etc) | |
3 | * | |
4 | * Copyright (c) 2002-3 Patrick Mochel | |
5 | * Copyright (c) 2002-3 Open Source Development Labs | |
64bb5d2c GKH |
6 | * Copyright (c) 2006 Greg Kroah-Hartman <gregkh@suse.de> |
7 | * Copyright (c) 2006 Novell, Inc. | |
1da177e4 LT |
8 | * |
9 | * This file is released under the GPLv2 | |
10 | * | |
11 | */ | |
12 | ||
1da177e4 LT |
13 | #include <linux/device.h> |
14 | #include <linux/err.h> | |
15 | #include <linux/init.h> | |
16 | #include <linux/module.h> | |
17 | #include <linux/slab.h> | |
18 | #include <linux/string.h> | |
23681e47 | 19 | #include <linux/kdev_t.h> |
116af378 | 20 | #include <linux/notifier.h> |
1da177e4 LT |
21 | |
22 | #include <asm/semaphore.h> | |
23 | ||
24 | #include "base.h" | |
25 | #include "power/power.h" | |
26 | ||
27 | int (*platform_notify)(struct device * dev) = NULL; | |
28 | int (*platform_notify_remove)(struct device * dev) = NULL; | |
29 | ||
30 | /* | |
31 | * sysfs bindings for devices. | |
32 | */ | |
33 | ||
3e95637a AS |
34 | /** |
35 | * dev_driver_string - Return a device's driver name, if at all possible | |
36 | * @dev: struct device to get the name of | |
37 | * | |
38 | * Will return the device's driver's name if it is bound to a device. If | |
39 | * the device is not bound to a device, it will return the name of the bus | |
40 | * it is attached to. If it is not attached to a bus either, an empty | |
41 | * string will be returned. | |
42 | */ | |
43 | const char *dev_driver_string(struct device *dev) | |
44 | { | |
45 | return dev->driver ? dev->driver->name : | |
46 | (dev->bus ? dev->bus->name : ""); | |
47 | } | |
310a922d | 48 | EXPORT_SYMBOL(dev_driver_string); |
3e95637a | 49 | |
1da177e4 LT |
50 | #define to_dev(obj) container_of(obj, struct device, kobj) |
51 | #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr) | |
52 | ||
1da177e4 LT |
53 | static ssize_t |
54 | dev_attr_show(struct kobject * kobj, struct attribute * attr, char * buf) | |
55 | { | |
56 | struct device_attribute * dev_attr = to_dev_attr(attr); | |
57 | struct device * dev = to_dev(kobj); | |
4a0c20bf | 58 | ssize_t ret = -EIO; |
1da177e4 LT |
59 | |
60 | if (dev_attr->show) | |
54b6f35c | 61 | ret = dev_attr->show(dev, dev_attr, buf); |
1da177e4 LT |
62 | return ret; |
63 | } | |
64 | ||
65 | static ssize_t | |
66 | dev_attr_store(struct kobject * kobj, struct attribute * attr, | |
67 | const char * buf, size_t count) | |
68 | { | |
69 | struct device_attribute * dev_attr = to_dev_attr(attr); | |
70 | struct device * dev = to_dev(kobj); | |
4a0c20bf | 71 | ssize_t ret = -EIO; |
1da177e4 LT |
72 | |
73 | if (dev_attr->store) | |
54b6f35c | 74 | ret = dev_attr->store(dev, dev_attr, buf, count); |
1da177e4 LT |
75 | return ret; |
76 | } | |
77 | ||
78 | static struct sysfs_ops dev_sysfs_ops = { | |
79 | .show = dev_attr_show, | |
80 | .store = dev_attr_store, | |
81 | }; | |
82 | ||
83 | ||
84 | /** | |
85 | * device_release - free device structure. | |
86 | * @kobj: device's kobject. | |
87 | * | |
88 | * This is called once the reference count for the object | |
89 | * reaches 0. We forward the call to the device's release | |
90 | * method, which should handle actually freeing the structure. | |
91 | */ | |
92 | static void device_release(struct kobject * kobj) | |
93 | { | |
94 | struct device * dev = to_dev(kobj); | |
95 | ||
96 | if (dev->release) | |
97 | dev->release(dev); | |
2620efef GKH |
98 | else if (dev->class && dev->class->dev_release) |
99 | dev->class->dev_release(dev); | |
1da177e4 LT |
100 | else { |
101 | printk(KERN_ERR "Device '%s' does not have a release() function, " | |
102 | "it is broken and must be fixed.\n", | |
103 | dev->bus_id); | |
104 | WARN_ON(1); | |
105 | } | |
106 | } | |
107 | ||
108 | static struct kobj_type ktype_device = { | |
109 | .release = device_release, | |
110 | .sysfs_ops = &dev_sysfs_ops, | |
1da177e4 LT |
111 | }; |
112 | ||
113 | ||
312c004d | 114 | static int dev_uevent_filter(struct kset *kset, struct kobject *kobj) |
1da177e4 LT |
115 | { |
116 | struct kobj_type *ktype = get_ktype(kobj); | |
117 | ||
118 | if (ktype == &ktype_device) { | |
119 | struct device *dev = to_dev(kobj); | |
120 | if (dev->bus) | |
121 | return 1; | |
23681e47 GKH |
122 | if (dev->class) |
123 | return 1; | |
1da177e4 LT |
124 | } |
125 | return 0; | |
126 | } | |
127 | ||
312c004d | 128 | static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj) |
1da177e4 LT |
129 | { |
130 | struct device *dev = to_dev(kobj); | |
131 | ||
23681e47 GKH |
132 | if (dev->bus) |
133 | return dev->bus->name; | |
134 | if (dev->class) | |
135 | return dev->class->name; | |
136 | return NULL; | |
1da177e4 LT |
137 | } |
138 | ||
312c004d | 139 | static int dev_uevent(struct kset *kset, struct kobject *kobj, char **envp, |
1da177e4 LT |
140 | int num_envp, char *buffer, int buffer_size) |
141 | { | |
142 | struct device *dev = to_dev(kobj); | |
143 | int i = 0; | |
144 | int length = 0; | |
145 | int retval = 0; | |
146 | ||
23681e47 GKH |
147 | /* add the major/minor if present */ |
148 | if (MAJOR(dev->devt)) { | |
149 | add_uevent_var(envp, num_envp, &i, | |
150 | buffer, buffer_size, &length, | |
151 | "MAJOR=%u", MAJOR(dev->devt)); | |
152 | add_uevent_var(envp, num_envp, &i, | |
153 | buffer, buffer_size, &length, | |
154 | "MINOR=%u", MINOR(dev->devt)); | |
155 | } | |
156 | ||
d81d9d6b | 157 | /* add bus name (same as SUBSYSTEM, deprecated) */ |
1da177e4 | 158 | if (dev->bus) |
312c004d KS |
159 | add_uevent_var(envp, num_envp, &i, |
160 | buffer, buffer_size, &length, | |
161 | "PHYSDEVBUS=%s", dev->bus->name); | |
1da177e4 | 162 | |
d81d9d6b KS |
163 | /* add driver name (PHYSDEV* values are deprecated)*/ |
164 | if (dev->driver) { | |
165 | add_uevent_var(envp, num_envp, &i, | |
166 | buffer, buffer_size, &length, | |
167 | "DRIVER=%s", dev->driver->name); | |
312c004d KS |
168 | add_uevent_var(envp, num_envp, &i, |
169 | buffer, buffer_size, &length, | |
170 | "PHYSDEVDRIVER=%s", dev->driver->name); | |
d81d9d6b | 171 | } |
1da177e4 LT |
172 | |
173 | /* terminate, set to next free slot, shrink available space */ | |
174 | envp[i] = NULL; | |
175 | envp = &envp[i]; | |
176 | num_envp -= i; | |
177 | buffer = &buffer[length]; | |
178 | buffer_size -= length; | |
179 | ||
312c004d | 180 | if (dev->bus && dev->bus->uevent) { |
1da177e4 | 181 | /* have the bus specific function add its stuff */ |
312c004d | 182 | retval = dev->bus->uevent(dev, envp, num_envp, buffer, buffer_size); |
1da177e4 | 183 | if (retval) { |
312c004d | 184 | pr_debug ("%s - uevent() returned %d\n", |
1da177e4 LT |
185 | __FUNCTION__, retval); |
186 | } | |
187 | } | |
188 | ||
2620efef GKH |
189 | if (dev->class && dev->class->dev_uevent) { |
190 | /* have the class specific function add its stuff */ | |
191 | retval = dev->class->dev_uevent(dev, envp, num_envp, buffer, buffer_size); | |
192 | if (retval) { | |
193 | pr_debug("%s - dev_uevent() returned %d\n", | |
194 | __FUNCTION__, retval); | |
195 | } | |
196 | } | |
197 | ||
1da177e4 LT |
198 | return retval; |
199 | } | |
200 | ||
312c004d KS |
201 | static struct kset_uevent_ops device_uevent_ops = { |
202 | .filter = dev_uevent_filter, | |
203 | .name = dev_uevent_name, | |
204 | .uevent = dev_uevent, | |
1da177e4 LT |
205 | }; |
206 | ||
a7fd6706 KS |
207 | static ssize_t store_uevent(struct device *dev, struct device_attribute *attr, |
208 | const char *buf, size_t count) | |
209 | { | |
312c004d | 210 | kobject_uevent(&dev->kobj, KOBJ_ADD); |
a7fd6706 KS |
211 | return count; |
212 | } | |
213 | ||
de0ff00d GKH |
214 | static int device_add_groups(struct device *dev) |
215 | { | |
216 | int i; | |
217 | int error = 0; | |
218 | ||
219 | if (dev->groups) { | |
220 | for (i = 0; dev->groups[i]; i++) { | |
221 | error = sysfs_create_group(&dev->kobj, dev->groups[i]); | |
222 | if (error) { | |
223 | while (--i >= 0) | |
224 | sysfs_remove_group(&dev->kobj, dev->groups[i]); | |
225 | goto out; | |
226 | } | |
227 | } | |
228 | } | |
229 | out: | |
230 | return error; | |
231 | } | |
232 | ||
233 | static void device_remove_groups(struct device *dev) | |
234 | { | |
235 | int i; | |
236 | if (dev->groups) { | |
237 | for (i = 0; dev->groups[i]; i++) { | |
238 | sysfs_remove_group(&dev->kobj, dev->groups[i]); | |
239 | } | |
240 | } | |
241 | } | |
242 | ||
2620efef GKH |
243 | static int device_add_attrs(struct device *dev) |
244 | { | |
245 | struct class *class = dev->class; | |
246 | int error = 0; | |
247 | int i; | |
248 | ||
249 | if (!class) | |
250 | return 0; | |
251 | ||
252 | if (class->dev_attrs) { | |
253 | for (i = 0; attr_name(class->dev_attrs[i]); i++) { | |
254 | error = device_create_file(dev, &class->dev_attrs[i]); | |
255 | if (error) | |
256 | break; | |
257 | } | |
258 | } | |
259 | if (error) | |
260 | while (--i >= 0) | |
261 | device_remove_file(dev, &class->dev_attrs[i]); | |
262 | return error; | |
263 | } | |
264 | ||
265 | static void device_remove_attrs(struct device *dev) | |
266 | { | |
267 | struct class *class = dev->class; | |
268 | int i; | |
269 | ||
270 | if (!class) | |
271 | return; | |
272 | ||
273 | if (class->dev_attrs) { | |
274 | for (i = 0; attr_name(class->dev_attrs[i]); i++) | |
275 | device_remove_file(dev, &class->dev_attrs[i]); | |
276 | } | |
277 | } | |
278 | ||
279 | ||
23681e47 GKH |
280 | static ssize_t show_dev(struct device *dev, struct device_attribute *attr, |
281 | char *buf) | |
282 | { | |
283 | return print_dev_t(buf, dev->devt); | |
284 | } | |
285 | ||
0863afb3 MW |
286 | /* |
287 | * devices_subsys - structure to be registered with kobject core. | |
1da177e4 LT |
288 | */ |
289 | ||
312c004d | 290 | decl_subsys(devices, &ktype_device, &device_uevent_ops); |
1da177e4 LT |
291 | |
292 | ||
293 | /** | |
294 | * device_create_file - create sysfs attribute file for device. | |
295 | * @dev: device. | |
296 | * @attr: device attribute descriptor. | |
297 | */ | |
298 | ||
299 | int device_create_file(struct device * dev, struct device_attribute * attr) | |
300 | { | |
301 | int error = 0; | |
302 | if (get_device(dev)) { | |
303 | error = sysfs_create_file(&dev->kobj, &attr->attr); | |
304 | put_device(dev); | |
305 | } | |
306 | return error; | |
307 | } | |
308 | ||
309 | /** | |
310 | * device_remove_file - remove sysfs attribute file. | |
311 | * @dev: device. | |
312 | * @attr: device attribute descriptor. | |
313 | */ | |
314 | ||
315 | void device_remove_file(struct device * dev, struct device_attribute * attr) | |
316 | { | |
317 | if (get_device(dev)) { | |
318 | sysfs_remove_file(&dev->kobj, &attr->attr); | |
319 | put_device(dev); | |
320 | } | |
321 | } | |
322 | ||
2589f188 GKH |
323 | /** |
324 | * device_create_bin_file - create sysfs binary attribute file for device. | |
325 | * @dev: device. | |
326 | * @attr: device binary attribute descriptor. | |
327 | */ | |
328 | int device_create_bin_file(struct device *dev, struct bin_attribute *attr) | |
329 | { | |
330 | int error = -EINVAL; | |
331 | if (dev) | |
332 | error = sysfs_create_bin_file(&dev->kobj, attr); | |
333 | return error; | |
334 | } | |
335 | EXPORT_SYMBOL_GPL(device_create_bin_file); | |
336 | ||
337 | /** | |
338 | * device_remove_bin_file - remove sysfs binary attribute file | |
339 | * @dev: device. | |
340 | * @attr: device binary attribute descriptor. | |
341 | */ | |
342 | void device_remove_bin_file(struct device *dev, struct bin_attribute *attr) | |
343 | { | |
344 | if (dev) | |
345 | sysfs_remove_bin_file(&dev->kobj, attr); | |
346 | } | |
347 | EXPORT_SYMBOL_GPL(device_remove_bin_file); | |
348 | ||
34bb61f9 JB |
349 | static void klist_children_get(struct klist_node *n) |
350 | { | |
351 | struct device *dev = container_of(n, struct device, knode_parent); | |
352 | ||
353 | get_device(dev); | |
354 | } | |
355 | ||
356 | static void klist_children_put(struct klist_node *n) | |
357 | { | |
358 | struct device *dev = container_of(n, struct device, knode_parent); | |
359 | ||
360 | put_device(dev); | |
361 | } | |
362 | ||
1da177e4 LT |
363 | |
364 | /** | |
365 | * device_initialize - init device structure. | |
366 | * @dev: device. | |
367 | * | |
368 | * This prepares the device for use by other layers, | |
369 | * including adding it to the device hierarchy. | |
370 | * It is the first half of device_register(), if called by | |
371 | * that, though it can also be called separately, so one | |
372 | * may use @dev's fields (e.g. the refcount). | |
373 | */ | |
374 | ||
375 | void device_initialize(struct device *dev) | |
376 | { | |
377 | kobj_set_kset_s(dev, devices_subsys); | |
378 | kobject_init(&dev->kobj); | |
34bb61f9 JB |
379 | klist_init(&dev->klist_children, klist_children_get, |
380 | klist_children_put); | |
1da177e4 | 381 | INIT_LIST_HEAD(&dev->dma_pools); |
23681e47 | 382 | INIT_LIST_HEAD(&dev->node); |
af70316a | 383 | init_MUTEX(&dev->sem); |
0ac85241 | 384 | device_init_wakeup(dev, 0); |
1da177e4 LT |
385 | } |
386 | ||
387 | /** | |
388 | * device_add - add device to device hierarchy. | |
389 | * @dev: device. | |
390 | * | |
391 | * This is part 2 of device_register(), though may be called | |
392 | * separately _iff_ device_initialize() has been called separately. | |
393 | * | |
394 | * This adds it to the kobject hierarchy via kobject_add(), adds it | |
395 | * to the global and sibling lists for the device, then | |
396 | * adds it to the other relevant subsystems of the driver model. | |
397 | */ | |
398 | int device_add(struct device *dev) | |
399 | { | |
400 | struct device *parent = NULL; | |
e9a7d305 | 401 | char *class_name = NULL; |
c47ed219 | 402 | struct class_interface *class_intf; |
1da177e4 LT |
403 | int error = -EINVAL; |
404 | ||
405 | dev = get_device(dev); | |
406 | if (!dev || !strlen(dev->bus_id)) | |
407 | goto Error; | |
408 | ||
c205ef48 GKH |
409 | /* if this is a class device, and has no parent, create one */ |
410 | if ((dev->class) && (dev->parent == NULL)) { | |
411 | error = virtual_device_parent(dev); | |
412 | if (error) | |
413 | goto Error; | |
414 | } | |
415 | ||
1da177e4 LT |
416 | parent = get_device(dev->parent); |
417 | ||
418 | pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id); | |
419 | ||
420 | /* first, register with generic layer. */ | |
421 | kobject_set_name(&dev->kobj, "%s", dev->bus_id); | |
422 | if (parent) | |
423 | dev->kobj.parent = &parent->kobj; | |
424 | ||
425 | if ((error = kobject_add(&dev->kobj))) | |
426 | goto Error; | |
a7fd6706 | 427 | |
37022644 BW |
428 | /* notify platform of device entry */ |
429 | if (platform_notify) | |
430 | platform_notify(dev); | |
431 | ||
116af378 BH |
432 | /* notify clients of device entry (new way) */ |
433 | if (dev->bus) | |
434 | blocking_notifier_call_chain(&dev->bus->bus_notifier, | |
435 | BUS_NOTIFY_ADD_DEVICE, dev); | |
436 | ||
a7fd6706 KS |
437 | dev->uevent_attr.attr.name = "uevent"; |
438 | dev->uevent_attr.attr.mode = S_IWUSR; | |
439 | if (dev->driver) | |
440 | dev->uevent_attr.attr.owner = dev->driver->owner; | |
441 | dev->uevent_attr.store = store_uevent; | |
a306eea4 CH |
442 | error = device_create_file(dev, &dev->uevent_attr); |
443 | if (error) | |
444 | goto attrError; | |
a7fd6706 | 445 | |
23681e47 GKH |
446 | if (MAJOR(dev->devt)) { |
447 | struct device_attribute *attr; | |
448 | attr = kzalloc(sizeof(*attr), GFP_KERNEL); | |
449 | if (!attr) { | |
450 | error = -ENOMEM; | |
a306eea4 | 451 | goto ueventattrError; |
23681e47 GKH |
452 | } |
453 | attr->attr.name = "dev"; | |
454 | attr->attr.mode = S_IRUGO; | |
455 | if (dev->driver) | |
456 | attr->attr.owner = dev->driver->owner; | |
457 | attr->show = show_dev; | |
458 | error = device_create_file(dev, attr); | |
459 | if (error) { | |
460 | kfree(attr); | |
a306eea4 | 461 | goto ueventattrError; |
23681e47 GKH |
462 | } |
463 | ||
464 | dev->devt_attr = attr; | |
465 | } | |
466 | ||
b9d9c82b KS |
467 | if (dev->class) { |
468 | sysfs_create_link(&dev->kobj, &dev->class->subsys.kset.kobj, | |
469 | "subsystem"); | |
23681e47 GKH |
470 | sysfs_create_link(&dev->class->subsys.kset.kobj, &dev->kobj, |
471 | dev->bus_id); | |
64bb5d2c GKH |
472 | if (parent) { |
473 | sysfs_create_link(&dev->kobj, &dev->parent->kobj, "device"); | |
474 | class_name = make_class_name(dev->class->name, &dev->kobj); | |
475 | sysfs_create_link(&dev->parent->kobj, &dev->kobj, class_name); | |
476 | } | |
b9d9c82b | 477 | } |
23681e47 | 478 | |
2620efef GKH |
479 | if ((error = device_add_attrs(dev))) |
480 | goto AttrsError; | |
de0ff00d GKH |
481 | if ((error = device_add_groups(dev))) |
482 | goto GroupError; | |
1da177e4 LT |
483 | if ((error = device_pm_add(dev))) |
484 | goto PMError; | |
485 | if ((error = bus_add_device(dev))) | |
486 | goto BusError; | |
53877d06 | 487 | kobject_uevent(&dev->kobj, KOBJ_ADD); |
f70fa629 AS |
488 | if ((error = bus_attach_device(dev))) |
489 | goto AttachError; | |
1da177e4 | 490 | if (parent) |
d856f1e3 | 491 | klist_add_tail(&dev->knode_parent, &parent->klist_children); |
1da177e4 | 492 | |
5d9fd169 | 493 | if (dev->class) { |
5d9fd169 | 494 | down(&dev->class->sem); |
c47ed219 | 495 | /* tie the class to the device */ |
5d9fd169 | 496 | list_add_tail(&dev->node, &dev->class->devices); |
c47ed219 GKH |
497 | |
498 | /* notify any interfaces that the device is here */ | |
499 | list_for_each_entry(class_intf, &dev->class->interfaces, node) | |
500 | if (class_intf->add_dev) | |
501 | class_intf->add_dev(dev, class_intf); | |
5d9fd169 GKH |
502 | up(&dev->class->sem); |
503 | } | |
1da177e4 | 504 | Done: |
e9a7d305 | 505 | kfree(class_name); |
1da177e4 LT |
506 | put_device(dev); |
507 | return error; | |
f70fa629 AS |
508 | AttachError: |
509 | bus_remove_device(dev); | |
1da177e4 LT |
510 | BusError: |
511 | device_pm_remove(dev); | |
512 | PMError: | |
116af378 BH |
513 | if (dev->bus) |
514 | blocking_notifier_call_chain(&dev->bus->bus_notifier, | |
515 | BUS_NOTIFY_DEL_DEVICE, dev); | |
de0ff00d GKH |
516 | device_remove_groups(dev); |
517 | GroupError: | |
2620efef GKH |
518 | device_remove_attrs(dev); |
519 | AttrsError: | |
23681e47 GKH |
520 | if (dev->devt_attr) { |
521 | device_remove_file(dev, dev->devt_attr); | |
522 | kfree(dev->devt_attr); | |
523 | } | |
a306eea4 CH |
524 | ueventattrError: |
525 | device_remove_file(dev, &dev->uevent_attr); | |
23681e47 | 526 | attrError: |
312c004d | 527 | kobject_uevent(&dev->kobj, KOBJ_REMOVE); |
1da177e4 LT |
528 | kobject_del(&dev->kobj); |
529 | Error: | |
530 | if (parent) | |
531 | put_device(parent); | |
532 | goto Done; | |
533 | } | |
534 | ||
535 | ||
536 | /** | |
537 | * device_register - register a device with the system. | |
538 | * @dev: pointer to the device structure | |
539 | * | |
540 | * This happens in two clean steps - initialize the device | |
541 | * and add it to the system. The two steps can be called | |
542 | * separately, but this is the easiest and most common. | |
543 | * I.e. you should only call the two helpers separately if | |
544 | * have a clearly defined need to use and refcount the device | |
545 | * before it is added to the hierarchy. | |
546 | */ | |
547 | ||
548 | int device_register(struct device *dev) | |
549 | { | |
550 | device_initialize(dev); | |
551 | return device_add(dev); | |
552 | } | |
553 | ||
554 | ||
555 | /** | |
556 | * get_device - increment reference count for device. | |
557 | * @dev: device. | |
558 | * | |
559 | * This simply forwards the call to kobject_get(), though | |
560 | * we do take care to provide for the case that we get a NULL | |
561 | * pointer passed in. | |
562 | */ | |
563 | ||
564 | struct device * get_device(struct device * dev) | |
565 | { | |
566 | return dev ? to_dev(kobject_get(&dev->kobj)) : NULL; | |
567 | } | |
568 | ||
569 | ||
570 | /** | |
571 | * put_device - decrement reference count. | |
572 | * @dev: device in question. | |
573 | */ | |
574 | void put_device(struct device * dev) | |
575 | { | |
576 | if (dev) | |
577 | kobject_put(&dev->kobj); | |
578 | } | |
579 | ||
580 | ||
581 | /** | |
582 | * device_del - delete device from system. | |
583 | * @dev: device. | |
584 | * | |
585 | * This is the first part of the device unregistration | |
586 | * sequence. This removes the device from the lists we control | |
587 | * from here, has it removed from the other driver model | |
588 | * subsystems it was added to in device_add(), and removes it | |
589 | * from the kobject hierarchy. | |
590 | * | |
591 | * NOTE: this should be called manually _iff_ device_add() was | |
592 | * also called manually. | |
593 | */ | |
594 | ||
595 | void device_del(struct device * dev) | |
596 | { | |
597 | struct device * parent = dev->parent; | |
e9a7d305 | 598 | char *class_name = NULL; |
c47ed219 | 599 | struct class_interface *class_intf; |
1da177e4 | 600 | |
1da177e4 | 601 | if (parent) |
d62c0f9f | 602 | klist_del(&dev->knode_parent); |
82189b98 | 603 | if (dev->devt_attr) { |
23681e47 | 604 | device_remove_file(dev, dev->devt_attr); |
82189b98 CM |
605 | kfree(dev->devt_attr); |
606 | } | |
b9d9c82b KS |
607 | if (dev->class) { |
608 | sysfs_remove_link(&dev->kobj, "subsystem"); | |
23681e47 | 609 | sysfs_remove_link(&dev->class->subsys.kset.kobj, dev->bus_id); |
e9a7d305 | 610 | class_name = make_class_name(dev->class->name, &dev->kobj); |
64bb5d2c GKH |
611 | if (parent) { |
612 | sysfs_remove_link(&dev->kobj, "device"); | |
613 | sysfs_remove_link(&dev->parent->kobj, class_name); | |
614 | } | |
e9a7d305 | 615 | kfree(class_name); |
5d9fd169 | 616 | down(&dev->class->sem); |
c47ed219 GKH |
617 | /* notify any interfaces that the device is now gone */ |
618 | list_for_each_entry(class_intf, &dev->class->interfaces, node) | |
619 | if (class_intf->remove_dev) | |
620 | class_intf->remove_dev(dev, class_intf); | |
621 | /* remove the device from the class list */ | |
5d9fd169 GKH |
622 | list_del_init(&dev->node); |
623 | up(&dev->class->sem); | |
b9d9c82b | 624 | } |
a7fd6706 | 625 | device_remove_file(dev, &dev->uevent_attr); |
de0ff00d | 626 | device_remove_groups(dev); |
2620efef | 627 | device_remove_attrs(dev); |
1da177e4 LT |
628 | |
629 | /* Notify the platform of the removal, in case they | |
630 | * need to do anything... | |
631 | */ | |
632 | if (platform_notify_remove) | |
633 | platform_notify_remove(dev); | |
116af378 BH |
634 | if (dev->bus) |
635 | blocking_notifier_call_chain(&dev->bus->bus_notifier, | |
636 | BUS_NOTIFY_DEL_DEVICE, dev); | |
1da177e4 LT |
637 | bus_remove_device(dev); |
638 | device_pm_remove(dev); | |
312c004d | 639 | kobject_uevent(&dev->kobj, KOBJ_REMOVE); |
1da177e4 LT |
640 | kobject_del(&dev->kobj); |
641 | if (parent) | |
642 | put_device(parent); | |
643 | } | |
644 | ||
645 | /** | |
646 | * device_unregister - unregister device from system. | |
647 | * @dev: device going away. | |
648 | * | |
649 | * We do this in two parts, like we do device_register(). First, | |
650 | * we remove it from all the subsystems with device_del(), then | |
651 | * we decrement the reference count via put_device(). If that | |
652 | * is the final reference count, the device will be cleaned up | |
653 | * via device_release() above. Otherwise, the structure will | |
654 | * stick around until the final reference to the device is dropped. | |
655 | */ | |
656 | void device_unregister(struct device * dev) | |
657 | { | |
658 | pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id); | |
659 | device_del(dev); | |
660 | put_device(dev); | |
661 | } | |
662 | ||
663 | ||
36239577 PM |
664 | static struct device * next_device(struct klist_iter * i) |
665 | { | |
666 | struct klist_node * n = klist_next(i); | |
667 | return n ? container_of(n, struct device, knode_parent) : NULL; | |
668 | } | |
669 | ||
1da177e4 LT |
670 | /** |
671 | * device_for_each_child - device child iterator. | |
c41455fb | 672 | * @parent: parent struct device. |
1da177e4 LT |
673 | * @data: data for the callback. |
674 | * @fn: function to be called for each device. | |
675 | * | |
c41455fb | 676 | * Iterate over @parent's child devices, and call @fn for each, |
1da177e4 LT |
677 | * passing it @data. |
678 | * | |
679 | * We check the return of @fn each time. If it returns anything | |
680 | * other than 0, we break out and return that value. | |
681 | */ | |
36239577 | 682 | int device_for_each_child(struct device * parent, void * data, |
1da177e4 LT |
683 | int (*fn)(struct device *, void *)) |
684 | { | |
36239577 | 685 | struct klist_iter i; |
1da177e4 LT |
686 | struct device * child; |
687 | int error = 0; | |
688 | ||
36239577 PM |
689 | klist_iter_init(&parent->klist_children, &i); |
690 | while ((child = next_device(&i)) && !error) | |
691 | error = fn(child, data); | |
692 | klist_iter_exit(&i); | |
1da177e4 LT |
693 | return error; |
694 | } | |
695 | ||
1da177e4 LT |
696 | int __init devices_init(void) |
697 | { | |
698 | return subsystem_register(&devices_subsys); | |
699 | } | |
700 | ||
701 | EXPORT_SYMBOL_GPL(device_for_each_child); | |
702 | ||
703 | EXPORT_SYMBOL_GPL(device_initialize); | |
704 | EXPORT_SYMBOL_GPL(device_add); | |
705 | EXPORT_SYMBOL_GPL(device_register); | |
706 | ||
707 | EXPORT_SYMBOL_GPL(device_del); | |
708 | EXPORT_SYMBOL_GPL(device_unregister); | |
709 | EXPORT_SYMBOL_GPL(get_device); | |
710 | EXPORT_SYMBOL_GPL(put_device); | |
1da177e4 LT |
711 | |
712 | EXPORT_SYMBOL_GPL(device_create_file); | |
713 | EXPORT_SYMBOL_GPL(device_remove_file); | |
23681e47 GKH |
714 | |
715 | ||
716 | static void device_create_release(struct device *dev) | |
717 | { | |
718 | pr_debug("%s called for %s\n", __FUNCTION__, dev->bus_id); | |
719 | kfree(dev); | |
720 | } | |
721 | ||
722 | /** | |
723 | * device_create - creates a device and registers it with sysfs | |
42734daf HK |
724 | * @class: pointer to the struct class that this device should be registered to |
725 | * @parent: pointer to the parent struct device of this new device, if any | |
726 | * @devt: the dev_t for the char device to be added | |
727 | * @fmt: string for the device's name | |
728 | * | |
729 | * This function can be used by char device classes. A struct device | |
730 | * will be created in sysfs, registered to the specified class. | |
23681e47 | 731 | * |
23681e47 GKH |
732 | * A "dev" file will be created, showing the dev_t for the device, if |
733 | * the dev_t is not 0,0. | |
42734daf HK |
734 | * If a pointer to a parent struct device is passed in, the newly created |
735 | * struct device will be a child of that device in sysfs. | |
736 | * The pointer to the struct device will be returned from the call. | |
737 | * Any further sysfs files that might be required can be created using this | |
23681e47 GKH |
738 | * pointer. |
739 | * | |
740 | * Note: the struct class passed to this function must have previously | |
741 | * been created with a call to class_create(). | |
742 | */ | |
743 | struct device *device_create(struct class *class, struct device *parent, | |
5cbe5f8a | 744 | dev_t devt, const char *fmt, ...) |
23681e47 GKH |
745 | { |
746 | va_list args; | |
747 | struct device *dev = NULL; | |
748 | int retval = -ENODEV; | |
749 | ||
750 | if (class == NULL || IS_ERR(class)) | |
751 | goto error; | |
23681e47 GKH |
752 | |
753 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); | |
754 | if (!dev) { | |
755 | retval = -ENOMEM; | |
756 | goto error; | |
757 | } | |
758 | ||
759 | dev->devt = devt; | |
760 | dev->class = class; | |
761 | dev->parent = parent; | |
762 | dev->release = device_create_release; | |
763 | ||
764 | va_start(args, fmt); | |
765 | vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args); | |
766 | va_end(args); | |
767 | retval = device_register(dev); | |
768 | if (retval) | |
769 | goto error; | |
770 | ||
23681e47 GKH |
771 | return dev; |
772 | ||
773 | error: | |
774 | kfree(dev); | |
775 | return ERR_PTR(retval); | |
776 | } | |
777 | EXPORT_SYMBOL_GPL(device_create); | |
778 | ||
779 | /** | |
780 | * device_destroy - removes a device that was created with device_create() | |
42734daf HK |
781 | * @class: pointer to the struct class that this device was registered with |
782 | * @devt: the dev_t of the device that was previously registered | |
23681e47 | 783 | * |
42734daf HK |
784 | * This call unregisters and cleans up a device that was created with a |
785 | * call to device_create(). | |
23681e47 GKH |
786 | */ |
787 | void device_destroy(struct class *class, dev_t devt) | |
788 | { | |
789 | struct device *dev = NULL; | |
790 | struct device *dev_tmp; | |
791 | ||
792 | down(&class->sem); | |
793 | list_for_each_entry(dev_tmp, &class->devices, node) { | |
794 | if (dev_tmp->devt == devt) { | |
795 | dev = dev_tmp; | |
796 | break; | |
797 | } | |
798 | } | |
799 | up(&class->sem); | |
800 | ||
5d9fd169 | 801 | if (dev) |
23681e47 | 802 | device_unregister(dev); |
23681e47 GKH |
803 | } |
804 | EXPORT_SYMBOL_GPL(device_destroy); | |
a2de48ca GKH |
805 | |
806 | /** | |
807 | * device_rename - renames a device | |
808 | * @dev: the pointer to the struct device to be renamed | |
809 | * @new_name: the new name of the device | |
810 | */ | |
811 | int device_rename(struct device *dev, char *new_name) | |
812 | { | |
813 | char *old_class_name = NULL; | |
814 | char *new_class_name = NULL; | |
815 | char *old_symlink_name = NULL; | |
816 | int error; | |
817 | ||
818 | dev = get_device(dev); | |
819 | if (!dev) | |
820 | return -EINVAL; | |
821 | ||
822 | pr_debug("DEVICE: renaming '%s' to '%s'\n", dev->bus_id, new_name); | |
823 | ||
824 | if ((dev->class) && (dev->parent)) | |
825 | old_class_name = make_class_name(dev->class->name, &dev->kobj); | |
826 | ||
827 | if (dev->class) { | |
828 | old_symlink_name = kmalloc(BUS_ID_SIZE, GFP_KERNEL); | |
952ab431 JJ |
829 | if (!old_symlink_name) { |
830 | error = -ENOMEM; | |
831 | goto out_free_old_class; | |
832 | } | |
a2de48ca GKH |
833 | strlcpy(old_symlink_name, dev->bus_id, BUS_ID_SIZE); |
834 | } | |
835 | ||
836 | strlcpy(dev->bus_id, new_name, BUS_ID_SIZE); | |
837 | ||
838 | error = kobject_rename(&dev->kobj, new_name); | |
839 | ||
840 | if (old_class_name) { | |
841 | new_class_name = make_class_name(dev->class->name, &dev->kobj); | |
842 | if (new_class_name) { | |
843 | sysfs_create_link(&dev->parent->kobj, &dev->kobj, | |
844 | new_class_name); | |
845 | sysfs_remove_link(&dev->parent->kobj, old_class_name); | |
846 | } | |
847 | } | |
848 | if (dev->class) { | |
849 | sysfs_remove_link(&dev->class->subsys.kset.kobj, | |
850 | old_symlink_name); | |
851 | sysfs_create_link(&dev->class->subsys.kset.kobj, &dev->kobj, | |
852 | dev->bus_id); | |
853 | } | |
854 | put_device(dev); | |
855 | ||
a2de48ca GKH |
856 | kfree(new_class_name); |
857 | kfree(old_symlink_name); | |
952ab431 JJ |
858 | out_free_old_class: |
859 | kfree(old_class_name); | |
a2de48ca GKH |
860 | |
861 | return error; | |
862 | } |