]>
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 | ||
60a96a59 KS |
27 | extern const char *kobject_actions[]; |
28 | ||
1da177e4 LT |
29 | int (*platform_notify)(struct device * dev) = NULL; |
30 | int (*platform_notify_remove)(struct device * dev) = NULL; | |
31 | ||
32 | /* | |
33 | * sysfs bindings for devices. | |
34 | */ | |
35 | ||
3e95637a AS |
36 | /** |
37 | * dev_driver_string - Return a device's driver name, if at all possible | |
38 | * @dev: struct device to get the name of | |
39 | * | |
40 | * Will return the device's driver's name if it is bound to a device. If | |
41 | * the device is not bound to a device, it will return the name of the bus | |
42 | * it is attached to. If it is not attached to a bus either, an empty | |
43 | * string will be returned. | |
44 | */ | |
45 | const char *dev_driver_string(struct device *dev) | |
46 | { | |
47 | return dev->driver ? dev->driver->name : | |
a456b702 JD |
48 | (dev->bus ? dev->bus->name : |
49 | (dev->class ? dev->class->name : "")); | |
3e95637a | 50 | } |
310a922d | 51 | EXPORT_SYMBOL(dev_driver_string); |
3e95637a | 52 | |
1da177e4 LT |
53 | #define to_dev(obj) container_of(obj, struct device, kobj) |
54 | #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr) | |
55 | ||
1da177e4 LT |
56 | static ssize_t |
57 | dev_attr_show(struct kobject * kobj, struct attribute * attr, char * buf) | |
58 | { | |
59 | struct device_attribute * dev_attr = to_dev_attr(attr); | |
60 | struct device * dev = to_dev(kobj); | |
4a0c20bf | 61 | ssize_t ret = -EIO; |
1da177e4 LT |
62 | |
63 | if (dev_attr->show) | |
54b6f35c | 64 | ret = dev_attr->show(dev, dev_attr, buf); |
1da177e4 LT |
65 | return ret; |
66 | } | |
67 | ||
68 | static ssize_t | |
69 | dev_attr_store(struct kobject * kobj, struct attribute * attr, | |
70 | const char * buf, size_t count) | |
71 | { | |
72 | struct device_attribute * dev_attr = to_dev_attr(attr); | |
73 | struct device * dev = to_dev(kobj); | |
4a0c20bf | 74 | ssize_t ret = -EIO; |
1da177e4 LT |
75 | |
76 | if (dev_attr->store) | |
54b6f35c | 77 | ret = dev_attr->store(dev, dev_attr, buf, count); |
1da177e4 LT |
78 | return ret; |
79 | } | |
80 | ||
81 | static struct sysfs_ops dev_sysfs_ops = { | |
82 | .show = dev_attr_show, | |
83 | .store = dev_attr_store, | |
84 | }; | |
85 | ||
86 | ||
87 | /** | |
88 | * device_release - free device structure. | |
89 | * @kobj: device's kobject. | |
90 | * | |
91 | * This is called once the reference count for the object | |
92 | * reaches 0. We forward the call to the device's release | |
93 | * method, which should handle actually freeing the structure. | |
94 | */ | |
95 | static void device_release(struct kobject * kobj) | |
96 | { | |
97 | struct device * dev = to_dev(kobj); | |
98 | ||
99 | if (dev->release) | |
100 | dev->release(dev); | |
f9f852df KS |
101 | else if (dev->type && dev->type->release) |
102 | dev->type->release(dev); | |
2620efef GKH |
103 | else if (dev->class && dev->class->dev_release) |
104 | dev->class->dev_release(dev); | |
1da177e4 LT |
105 | else { |
106 | printk(KERN_ERR "Device '%s' does not have a release() function, " | |
107 | "it is broken and must be fixed.\n", | |
108 | dev->bus_id); | |
109 | WARN_ON(1); | |
110 | } | |
111 | } | |
112 | ||
113 | static struct kobj_type ktype_device = { | |
114 | .release = device_release, | |
115 | .sysfs_ops = &dev_sysfs_ops, | |
1da177e4 LT |
116 | }; |
117 | ||
118 | ||
312c004d | 119 | static int dev_uevent_filter(struct kset *kset, struct kobject *kobj) |
1da177e4 LT |
120 | { |
121 | struct kobj_type *ktype = get_ktype(kobj); | |
122 | ||
123 | if (ktype == &ktype_device) { | |
124 | struct device *dev = to_dev(kobj); | |
83b5fb4c CH |
125 | if (dev->uevent_suppress) |
126 | return 0; | |
1da177e4 LT |
127 | if (dev->bus) |
128 | return 1; | |
23681e47 GKH |
129 | if (dev->class) |
130 | return 1; | |
1da177e4 LT |
131 | } |
132 | return 0; | |
133 | } | |
134 | ||
312c004d | 135 | static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj) |
1da177e4 LT |
136 | { |
137 | struct device *dev = to_dev(kobj); | |
138 | ||
23681e47 GKH |
139 | if (dev->bus) |
140 | return dev->bus->name; | |
141 | if (dev->class) | |
142 | return dev->class->name; | |
143 | return NULL; | |
1da177e4 LT |
144 | } |
145 | ||
312c004d | 146 | static int dev_uevent(struct kset *kset, struct kobject *kobj, char **envp, |
1da177e4 LT |
147 | int num_envp, char *buffer, int buffer_size) |
148 | { | |
149 | struct device *dev = to_dev(kobj); | |
150 | int i = 0; | |
151 | int length = 0; | |
152 | int retval = 0; | |
153 | ||
23681e47 GKH |
154 | /* add the major/minor if present */ |
155 | if (MAJOR(dev->devt)) { | |
156 | add_uevent_var(envp, num_envp, &i, | |
157 | buffer, buffer_size, &length, | |
158 | "MAJOR=%u", MAJOR(dev->devt)); | |
159 | add_uevent_var(envp, num_envp, &i, | |
160 | buffer, buffer_size, &length, | |
161 | "MINOR=%u", MINOR(dev->devt)); | |
162 | } | |
163 | ||
414264f9 KS |
164 | if (dev->type && dev->type->name) |
165 | add_uevent_var(envp, num_envp, &i, | |
166 | buffer, buffer_size, &length, | |
167 | "DEVTYPE=%s", dev->type->name); | |
168 | ||
239378f1 | 169 | if (dev->driver) |
d81d9d6b KS |
170 | add_uevent_var(envp, num_envp, &i, |
171 | buffer, buffer_size, &length, | |
172 | "DRIVER=%s", dev->driver->name); | |
239378f1 | 173 | |
a87cb2ac | 174 | #ifdef CONFIG_SYSFS_DEPRECATED |
239378f1 KS |
175 | if (dev->class) { |
176 | struct device *parent = dev->parent; | |
177 | ||
178 | /* find first bus device in parent chain */ | |
179 | while (parent && !parent->bus) | |
180 | parent = parent->parent; | |
181 | if (parent && parent->bus) { | |
182 | const char *path; | |
183 | ||
184 | path = kobject_get_path(&parent->kobj, GFP_KERNEL); | |
2c7afd12 KS |
185 | if (path) { |
186 | add_uevent_var(envp, num_envp, &i, | |
187 | buffer, buffer_size, &length, | |
188 | "PHYSDEVPATH=%s", path); | |
189 | kfree(path); | |
190 | } | |
239378f1 KS |
191 | |
192 | add_uevent_var(envp, num_envp, &i, | |
193 | buffer, buffer_size, &length, | |
194 | "PHYSDEVBUS=%s", parent->bus->name); | |
195 | ||
196 | if (parent->driver) | |
197 | add_uevent_var(envp, num_envp, &i, | |
198 | buffer, buffer_size, &length, | |
199 | "PHYSDEVDRIVER=%s", parent->driver->name); | |
200 | } | |
201 | } else if (dev->bus) { | |
312c004d KS |
202 | add_uevent_var(envp, num_envp, &i, |
203 | buffer, buffer_size, &length, | |
239378f1 KS |
204 | "PHYSDEVBUS=%s", dev->bus->name); |
205 | ||
206 | if (dev->driver) | |
207 | add_uevent_var(envp, num_envp, &i, | |
208 | buffer, buffer_size, &length, | |
209 | "PHYSDEVDRIVER=%s", dev->driver->name); | |
d81d9d6b | 210 | } |
239378f1 | 211 | #endif |
1da177e4 LT |
212 | |
213 | /* terminate, set to next free slot, shrink available space */ | |
214 | envp[i] = NULL; | |
215 | envp = &envp[i]; | |
216 | num_envp -= i; | |
217 | buffer = &buffer[length]; | |
218 | buffer_size -= length; | |
219 | ||
312c004d | 220 | if (dev->bus && dev->bus->uevent) { |
1da177e4 | 221 | /* have the bus specific function add its stuff */ |
312c004d | 222 | retval = dev->bus->uevent(dev, envp, num_envp, buffer, buffer_size); |
f9f852df KS |
223 | if (retval) |
224 | pr_debug ("%s: bus uevent() returned %d\n", | |
1da177e4 | 225 | __FUNCTION__, retval); |
1da177e4 LT |
226 | } |
227 | ||
2620efef GKH |
228 | if (dev->class && dev->class->dev_uevent) { |
229 | /* have the class specific function add its stuff */ | |
230 | retval = dev->class->dev_uevent(dev, envp, num_envp, buffer, buffer_size); | |
f9f852df KS |
231 | if (retval) |
232 | pr_debug("%s: class uevent() returned %d\n", | |
233 | __FUNCTION__, retval); | |
234 | } | |
235 | ||
236 | if (dev->type && dev->type->uevent) { | |
237 | /* have the device type specific fuction add its stuff */ | |
238 | retval = dev->type->uevent(dev, envp, num_envp, buffer, buffer_size); | |
239 | if (retval) | |
240 | pr_debug("%s: dev_type uevent() returned %d\n", | |
241 | __FUNCTION__, retval); | |
2620efef GKH |
242 | } |
243 | ||
1da177e4 LT |
244 | return retval; |
245 | } | |
246 | ||
312c004d KS |
247 | static struct kset_uevent_ops device_uevent_ops = { |
248 | .filter = dev_uevent_filter, | |
249 | .name = dev_uevent_name, | |
250 | .uevent = dev_uevent, | |
1da177e4 LT |
251 | }; |
252 | ||
16574dcc KS |
253 | static ssize_t show_uevent(struct device *dev, struct device_attribute *attr, |
254 | char *buf) | |
255 | { | |
256 | struct kobject *top_kobj; | |
257 | struct kset *kset; | |
258 | char *envp[32]; | |
c7308c81 | 259 | char *data = NULL; |
16574dcc KS |
260 | char *pos; |
261 | int i; | |
262 | size_t count = 0; | |
263 | int retval; | |
264 | ||
265 | /* search the kset, the device belongs to */ | |
266 | top_kobj = &dev->kobj; | |
267 | if (!top_kobj->kset && top_kobj->parent) { | |
268 | do { | |
269 | top_kobj = top_kobj->parent; | |
270 | } while (!top_kobj->kset && top_kobj->parent); | |
271 | } | |
272 | if (!top_kobj->kset) | |
273 | goto out; | |
274 | kset = top_kobj->kset; | |
275 | if (!kset->uevent_ops || !kset->uevent_ops->uevent) | |
276 | goto out; | |
277 | ||
278 | /* respect filter */ | |
279 | if (kset->uevent_ops && kset->uevent_ops->filter) | |
280 | if (!kset->uevent_ops->filter(kset, &dev->kobj)) | |
281 | goto out; | |
282 | ||
c7308c81 GKH |
283 | data = (char *)get_zeroed_page(GFP_KERNEL); |
284 | if (!data) | |
285 | return -ENOMEM; | |
286 | ||
16574dcc KS |
287 | /* let the kset specific function add its keys */ |
288 | pos = data; | |
289 | retval = kset->uevent_ops->uevent(kset, &dev->kobj, | |
290 | envp, ARRAY_SIZE(envp), | |
291 | pos, PAGE_SIZE); | |
292 | if (retval) | |
293 | goto out; | |
294 | ||
295 | /* copy keys to file */ | |
296 | for (i = 0; envp[i]; i++) { | |
297 | pos = &buf[count]; | |
298 | count += sprintf(pos, "%s\n", envp[i]); | |
299 | } | |
300 | out: | |
c7308c81 | 301 | free_page((unsigned long)data); |
16574dcc KS |
302 | return count; |
303 | } | |
304 | ||
a7fd6706 KS |
305 | static ssize_t store_uevent(struct device *dev, struct device_attribute *attr, |
306 | const char *buf, size_t count) | |
307 | { | |
60a96a59 KS |
308 | size_t len = count; |
309 | enum kobject_action action; | |
310 | ||
311 | if (len && buf[len-1] == '\n') | |
312 | len--; | |
313 | ||
314 | for (action = 0; action < KOBJ_MAX; action++) { | |
315 | if (strncmp(kobject_actions[action], buf, len) != 0) | |
316 | continue; | |
317 | if (kobject_actions[action][len] != '\0') | |
318 | continue; | |
319 | kobject_uevent(&dev->kobj, action); | |
320 | goto out; | |
321 | } | |
322 | ||
323 | dev_err(dev, "uevent: unsupported action-string; this will " | |
324 | "be ignored in a future kernel version\n"); | |
312c004d | 325 | kobject_uevent(&dev->kobj, KOBJ_ADD); |
60a96a59 | 326 | out: |
a7fd6706 KS |
327 | return count; |
328 | } | |
329 | ||
ad6a1e1c TH |
330 | static struct device_attribute uevent_attr = |
331 | __ATTR(uevent, S_IRUGO | S_IWUSR, show_uevent, store_uevent); | |
332 | ||
621a1672 DT |
333 | static int device_add_attributes(struct device *dev, |
334 | struct device_attribute *attrs) | |
de0ff00d | 335 | { |
621a1672 | 336 | int error = 0; |
de0ff00d | 337 | int i; |
621a1672 DT |
338 | |
339 | if (attrs) { | |
340 | for (i = 0; attr_name(attrs[i]); i++) { | |
341 | error = device_create_file(dev, &attrs[i]); | |
342 | if (error) | |
343 | break; | |
344 | } | |
345 | if (error) | |
346 | while (--i >= 0) | |
347 | device_remove_file(dev, &attrs[i]); | |
348 | } | |
349 | return error; | |
350 | } | |
351 | ||
352 | static void device_remove_attributes(struct device *dev, | |
353 | struct device_attribute *attrs) | |
354 | { | |
355 | int i; | |
356 | ||
357 | if (attrs) | |
358 | for (i = 0; attr_name(attrs[i]); i++) | |
359 | device_remove_file(dev, &attrs[i]); | |
360 | } | |
361 | ||
362 | static int device_add_groups(struct device *dev, | |
363 | struct attribute_group **groups) | |
364 | { | |
de0ff00d | 365 | int error = 0; |
621a1672 | 366 | int i; |
de0ff00d | 367 | |
621a1672 DT |
368 | if (groups) { |
369 | for (i = 0; groups[i]; i++) { | |
370 | error = sysfs_create_group(&dev->kobj, groups[i]); | |
de0ff00d GKH |
371 | if (error) { |
372 | while (--i >= 0) | |
621a1672 DT |
373 | sysfs_remove_group(&dev->kobj, groups[i]); |
374 | break; | |
de0ff00d GKH |
375 | } |
376 | } | |
377 | } | |
de0ff00d GKH |
378 | return error; |
379 | } | |
380 | ||
621a1672 DT |
381 | static void device_remove_groups(struct device *dev, |
382 | struct attribute_group **groups) | |
de0ff00d GKH |
383 | { |
384 | int i; | |
621a1672 DT |
385 | |
386 | if (groups) | |
387 | for (i = 0; groups[i]; i++) | |
388 | sysfs_remove_group(&dev->kobj, groups[i]); | |
de0ff00d GKH |
389 | } |
390 | ||
2620efef GKH |
391 | static int device_add_attrs(struct device *dev) |
392 | { | |
393 | struct class *class = dev->class; | |
f9f852df | 394 | struct device_type *type = dev->type; |
621a1672 | 395 | int error; |
2620efef | 396 | |
621a1672 DT |
397 | if (class) { |
398 | error = device_add_attributes(dev, class->dev_attrs); | |
f9f852df | 399 | if (error) |
621a1672 | 400 | return error; |
2620efef | 401 | } |
f9f852df | 402 | |
621a1672 DT |
403 | if (type) { |
404 | error = device_add_groups(dev, type->groups); | |
f9f852df | 405 | if (error) |
621a1672 | 406 | goto err_remove_class_attrs; |
f9f852df KS |
407 | } |
408 | ||
621a1672 DT |
409 | error = device_add_groups(dev, dev->groups); |
410 | if (error) | |
411 | goto err_remove_type_groups; | |
412 | ||
413 | return 0; | |
414 | ||
415 | err_remove_type_groups: | |
416 | if (type) | |
417 | device_remove_groups(dev, type->groups); | |
418 | err_remove_class_attrs: | |
419 | if (class) | |
420 | device_remove_attributes(dev, class->dev_attrs); | |
421 | ||
2620efef GKH |
422 | return error; |
423 | } | |
424 | ||
425 | static void device_remove_attrs(struct device *dev) | |
426 | { | |
427 | struct class *class = dev->class; | |
f9f852df | 428 | struct device_type *type = dev->type; |
2620efef | 429 | |
621a1672 | 430 | device_remove_groups(dev, dev->groups); |
f9f852df | 431 | |
621a1672 DT |
432 | if (type) |
433 | device_remove_groups(dev, type->groups); | |
434 | ||
435 | if (class) | |
436 | device_remove_attributes(dev, class->dev_attrs); | |
2620efef GKH |
437 | } |
438 | ||
439 | ||
23681e47 GKH |
440 | static ssize_t show_dev(struct device *dev, struct device_attribute *attr, |
441 | char *buf) | |
442 | { | |
443 | return print_dev_t(buf, dev->devt); | |
444 | } | |
445 | ||
ad6a1e1c TH |
446 | static struct device_attribute devt_attr = |
447 | __ATTR(dev, S_IRUGO, show_dev, NULL); | |
448 | ||
0863afb3 MW |
449 | /* |
450 | * devices_subsys - structure to be registered with kobject core. | |
1da177e4 LT |
451 | */ |
452 | ||
312c004d | 453 | decl_subsys(devices, &ktype_device, &device_uevent_ops); |
1da177e4 LT |
454 | |
455 | ||
456 | /** | |
457 | * device_create_file - create sysfs attribute file for device. | |
458 | * @dev: device. | |
459 | * @attr: device attribute descriptor. | |
460 | */ | |
461 | ||
462 | int device_create_file(struct device * dev, struct device_attribute * attr) | |
463 | { | |
464 | int error = 0; | |
465 | if (get_device(dev)) { | |
466 | error = sysfs_create_file(&dev->kobj, &attr->attr); | |
467 | put_device(dev); | |
468 | } | |
469 | return error; | |
470 | } | |
471 | ||
472 | /** | |
473 | * device_remove_file - remove sysfs attribute file. | |
474 | * @dev: device. | |
475 | * @attr: device attribute descriptor. | |
476 | */ | |
477 | ||
478 | void device_remove_file(struct device * dev, struct device_attribute * attr) | |
479 | { | |
480 | if (get_device(dev)) { | |
481 | sysfs_remove_file(&dev->kobj, &attr->attr); | |
482 | put_device(dev); | |
483 | } | |
484 | } | |
485 | ||
2589f188 GKH |
486 | /** |
487 | * device_create_bin_file - create sysfs binary attribute file for device. | |
488 | * @dev: device. | |
489 | * @attr: device binary attribute descriptor. | |
490 | */ | |
491 | int device_create_bin_file(struct device *dev, struct bin_attribute *attr) | |
492 | { | |
493 | int error = -EINVAL; | |
494 | if (dev) | |
495 | error = sysfs_create_bin_file(&dev->kobj, attr); | |
496 | return error; | |
497 | } | |
498 | EXPORT_SYMBOL_GPL(device_create_bin_file); | |
499 | ||
500 | /** | |
501 | * device_remove_bin_file - remove sysfs binary attribute file | |
502 | * @dev: device. | |
503 | * @attr: device binary attribute descriptor. | |
504 | */ | |
505 | void device_remove_bin_file(struct device *dev, struct bin_attribute *attr) | |
506 | { | |
507 | if (dev) | |
508 | sysfs_remove_bin_file(&dev->kobj, attr); | |
509 | } | |
510 | EXPORT_SYMBOL_GPL(device_remove_bin_file); | |
511 | ||
d9a9cdfb | 512 | /** |
523ded71 | 513 | * device_schedule_callback_owner - helper to schedule a callback for a device |
d9a9cdfb AS |
514 | * @dev: device. |
515 | * @func: callback function to invoke later. | |
523ded71 | 516 | * @owner: module owning the callback routine |
d9a9cdfb AS |
517 | * |
518 | * Attribute methods must not unregister themselves or their parent device | |
519 | * (which would amount to the same thing). Attempts to do so will deadlock, | |
520 | * since unregistration is mutually exclusive with driver callbacks. | |
521 | * | |
522 | * Instead methods can call this routine, which will attempt to allocate | |
523 | * and schedule a workqueue request to call back @func with @dev as its | |
524 | * argument in the workqueue's process context. @dev will be pinned until | |
525 | * @func returns. | |
526 | * | |
523ded71 AS |
527 | * This routine is usually called via the inline device_schedule_callback(), |
528 | * which automatically sets @owner to THIS_MODULE. | |
529 | * | |
d9a9cdfb | 530 | * Returns 0 if the request was submitted, -ENOMEM if storage could not |
523ded71 | 531 | * be allocated, -ENODEV if a reference to @owner isn't available. |
d9a9cdfb AS |
532 | * |
533 | * NOTE: This routine won't work if CONFIG_SYSFS isn't set! It uses an | |
534 | * underlying sysfs routine (since it is intended for use by attribute | |
535 | * methods), and if sysfs isn't available you'll get nothing but -ENOSYS. | |
536 | */ | |
523ded71 AS |
537 | int device_schedule_callback_owner(struct device *dev, |
538 | void (*func)(struct device *), struct module *owner) | |
d9a9cdfb AS |
539 | { |
540 | return sysfs_schedule_callback(&dev->kobj, | |
523ded71 | 541 | (void (*)(void *)) func, dev, owner); |
d9a9cdfb | 542 | } |
523ded71 | 543 | EXPORT_SYMBOL_GPL(device_schedule_callback_owner); |
d9a9cdfb | 544 | |
34bb61f9 JB |
545 | static void klist_children_get(struct klist_node *n) |
546 | { | |
547 | struct device *dev = container_of(n, struct device, knode_parent); | |
548 | ||
549 | get_device(dev); | |
550 | } | |
551 | ||
552 | static void klist_children_put(struct klist_node *n) | |
553 | { | |
554 | struct device *dev = container_of(n, struct device, knode_parent); | |
555 | ||
556 | put_device(dev); | |
557 | } | |
558 | ||
1da177e4 LT |
559 | |
560 | /** | |
561 | * device_initialize - init device structure. | |
562 | * @dev: device. | |
563 | * | |
564 | * This prepares the device for use by other layers, | |
565 | * including adding it to the device hierarchy. | |
566 | * It is the first half of device_register(), if called by | |
567 | * that, though it can also be called separately, so one | |
568 | * may use @dev's fields (e.g. the refcount). | |
569 | */ | |
570 | ||
571 | void device_initialize(struct device *dev) | |
572 | { | |
573 | kobj_set_kset_s(dev, devices_subsys); | |
574 | kobject_init(&dev->kobj); | |
34bb61f9 JB |
575 | klist_init(&dev->klist_children, klist_children_get, |
576 | klist_children_put); | |
1da177e4 | 577 | INIT_LIST_HEAD(&dev->dma_pools); |
23681e47 | 578 | INIT_LIST_HEAD(&dev->node); |
af70316a | 579 | init_MUTEX(&dev->sem); |
9ac7849e TH |
580 | spin_lock_init(&dev->devres_lock); |
581 | INIT_LIST_HEAD(&dev->devres_head); | |
0ac85241 | 582 | device_init_wakeup(dev, 0); |
87348136 | 583 | set_dev_node(dev, -1); |
1da177e4 LT |
584 | } |
585 | ||
40fa5422 | 586 | #ifdef CONFIG_SYSFS_DEPRECATED |
c744aeae CH |
587 | static struct kobject * get_device_parent(struct device *dev, |
588 | struct device *parent) | |
40fa5422 GKH |
589 | { |
590 | /* Set the parent to the class, not the parent device */ | |
591 | /* this keeps sysfs from having a symlink to make old udevs happy */ | |
592 | if (dev->class) | |
823bccfc | 593 | return &dev->class->subsys.kobj; |
40fa5422 | 594 | else if (parent) |
c744aeae | 595 | return &parent->kobj; |
40fa5422 | 596 | |
c744aeae | 597 | return NULL; |
40fa5422 GKH |
598 | } |
599 | #else | |
86406245 | 600 | static struct kobject *virtual_device_parent(struct device *dev) |
f0ee61a6 | 601 | { |
86406245 | 602 | static struct kobject *virtual_dir = NULL; |
f0ee61a6 | 603 | |
86406245 | 604 | if (!virtual_dir) |
823bccfc | 605 | virtual_dir = kobject_add_dir(&devices_subsys.kobj, "virtual"); |
f0ee61a6 | 606 | |
86406245 | 607 | return virtual_dir; |
f0ee61a6 GKH |
608 | } |
609 | ||
c744aeae CH |
610 | static struct kobject * get_device_parent(struct device *dev, |
611 | struct device *parent) | |
40fa5422 | 612 | { |
86406245 KS |
613 | if (dev->class) { |
614 | struct kobject *kobj = NULL; | |
615 | struct kobject *parent_kobj; | |
616 | struct kobject *k; | |
617 | ||
618 | /* | |
619 | * If we have no parent, we live in "virtual". | |
620 | * Class-devices with a bus-device as parent, live | |
621 | * in a class-directory to prevent namespace collisions. | |
622 | */ | |
623 | if (parent == NULL) | |
624 | parent_kobj = virtual_device_parent(dev); | |
625 | else if (parent->class) | |
626 | return &parent->kobj; | |
627 | else | |
628 | parent_kobj = &parent->kobj; | |
629 | ||
630 | /* find our class-directory at the parent and reference it */ | |
631 | spin_lock(&dev->class->class_dirs.list_lock); | |
632 | list_for_each_entry(k, &dev->class->class_dirs.list, entry) | |
633 | if (k->parent == parent_kobj) { | |
634 | kobj = kobject_get(k); | |
635 | break; | |
636 | } | |
637 | spin_unlock(&dev->class->class_dirs.list_lock); | |
638 | if (kobj) | |
639 | return kobj; | |
640 | ||
641 | /* or create a new class-directory at the parent device */ | |
642 | return kobject_kset_add_dir(&dev->class->class_dirs, | |
643 | parent_kobj, dev->class->name); | |
644 | } | |
645 | ||
646 | if (parent) | |
c744aeae CH |
647 | return &parent->kobj; |
648 | return NULL; | |
649 | } | |
c744aeae | 650 | #endif |
86406245 | 651 | |
c744aeae CH |
652 | static int setup_parent(struct device *dev, struct device *parent) |
653 | { | |
654 | struct kobject *kobj; | |
655 | kobj = get_device_parent(dev, parent); | |
656 | if (IS_ERR(kobj)) | |
657 | return PTR_ERR(kobj); | |
658 | if (kobj) | |
659 | dev->kobj.parent = kobj; | |
40fa5422 GKH |
660 | return 0; |
661 | } | |
40fa5422 | 662 | |
1da177e4 LT |
663 | /** |
664 | * device_add - add device to device hierarchy. | |
665 | * @dev: device. | |
666 | * | |
667 | * This is part 2 of device_register(), though may be called | |
668 | * separately _iff_ device_initialize() has been called separately. | |
669 | * | |
670 | * This adds it to the kobject hierarchy via kobject_add(), adds it | |
671 | * to the global and sibling lists for the device, then | |
672 | * adds it to the other relevant subsystems of the driver model. | |
673 | */ | |
674 | int device_add(struct device *dev) | |
675 | { | |
676 | struct device *parent = NULL; | |
e9a7d305 | 677 | char *class_name = NULL; |
c47ed219 | 678 | struct class_interface *class_intf; |
1da177e4 LT |
679 | int error = -EINVAL; |
680 | ||
681 | dev = get_device(dev); | |
682 | if (!dev || !strlen(dev->bus_id)) | |
683 | goto Error; | |
684 | ||
40fa5422 | 685 | pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id); |
c205ef48 | 686 | |
1da177e4 | 687 | parent = get_device(dev->parent); |
40fa5422 GKH |
688 | error = setup_parent(dev, parent); |
689 | if (error) | |
690 | goto Error; | |
1da177e4 LT |
691 | |
692 | /* first, register with generic layer. */ | |
693 | kobject_set_name(&dev->kobj, "%s", dev->bus_id); | |
40fa5422 GKH |
694 | error = kobject_add(&dev->kobj); |
695 | if (error) | |
1da177e4 | 696 | goto Error; |
a7fd6706 | 697 | |
37022644 BW |
698 | /* notify platform of device entry */ |
699 | if (platform_notify) | |
700 | platform_notify(dev); | |
701 | ||
116af378 BH |
702 | /* notify clients of device entry (new way) */ |
703 | if (dev->bus) | |
704 | blocking_notifier_call_chain(&dev->bus->bus_notifier, | |
705 | BUS_NOTIFY_ADD_DEVICE, dev); | |
706 | ||
ad6a1e1c | 707 | error = device_create_file(dev, &uevent_attr); |
a306eea4 CH |
708 | if (error) |
709 | goto attrError; | |
a7fd6706 | 710 | |
23681e47 | 711 | if (MAJOR(dev->devt)) { |
ad6a1e1c TH |
712 | error = device_create_file(dev, &devt_attr); |
713 | if (error) | |
a306eea4 | 714 | goto ueventattrError; |
23681e47 GKH |
715 | } |
716 | ||
b9d9c82b | 717 | if (dev->class) { |
823bccfc | 718 | sysfs_create_link(&dev->kobj, &dev->class->subsys.kobj, |
b9d9c82b | 719 | "subsystem"); |
40fa5422 GKH |
720 | /* If this is not a "fake" compatible device, then create the |
721 | * symlink from the class to the device. */ | |
823bccfc GKH |
722 | if (dev->kobj.parent != &dev->class->subsys.kobj) |
723 | sysfs_create_link(&dev->class->subsys.kobj, | |
40fa5422 | 724 | &dev->kobj, dev->bus_id); |
64bb5d2c | 725 | if (parent) { |
cb360bbf CH |
726 | sysfs_create_link(&dev->kobj, &dev->parent->kobj, |
727 | "device"); | |
f7f3461d | 728 | #ifdef CONFIG_SYSFS_DEPRECATED |
cb360bbf CH |
729 | class_name = make_class_name(dev->class->name, |
730 | &dev->kobj); | |
731 | if (class_name) | |
732 | sysfs_create_link(&dev->parent->kobj, | |
733 | &dev->kobj, class_name); | |
99ef3ef8 | 734 | #endif |
f7f3461d | 735 | } |
b9d9c82b | 736 | } |
23681e47 | 737 | |
dc0afa83 CH |
738 | error = device_add_attrs(dev); |
739 | if (error) | |
2620efef | 740 | goto AttrsError; |
dc0afa83 CH |
741 | error = device_pm_add(dev); |
742 | if (error) | |
1da177e4 | 743 | goto PMError; |
dc0afa83 CH |
744 | error = bus_add_device(dev); |
745 | if (error) | |
1da177e4 | 746 | goto BusError; |
83b5fb4c | 747 | kobject_uevent(&dev->kobj, KOBJ_ADD); |
c6a46696 | 748 | bus_attach_device(dev); |
1da177e4 | 749 | if (parent) |
d856f1e3 | 750 | klist_add_tail(&dev->knode_parent, &parent->klist_children); |
1da177e4 | 751 | |
5d9fd169 | 752 | if (dev->class) { |
5d9fd169 | 753 | down(&dev->class->sem); |
c47ed219 | 754 | /* tie the class to the device */ |
5d9fd169 | 755 | list_add_tail(&dev->node, &dev->class->devices); |
c47ed219 GKH |
756 | |
757 | /* notify any interfaces that the device is here */ | |
758 | list_for_each_entry(class_intf, &dev->class->interfaces, node) | |
759 | if (class_intf->add_dev) | |
760 | class_intf->add_dev(dev, class_intf); | |
5d9fd169 GKH |
761 | up(&dev->class->sem); |
762 | } | |
1da177e4 | 763 | Done: |
621a1672 | 764 | kfree(class_name); |
1da177e4 LT |
765 | put_device(dev); |
766 | return error; | |
767 | BusError: | |
768 | device_pm_remove(dev); | |
769 | PMError: | |
116af378 BH |
770 | if (dev->bus) |
771 | blocking_notifier_call_chain(&dev->bus->bus_notifier, | |
772 | BUS_NOTIFY_DEL_DEVICE, dev); | |
82f0cf9b | 773 | device_remove_attrs(dev); |
2620efef | 774 | AttrsError: |
ad6a1e1c TH |
775 | if (MAJOR(dev->devt)) |
776 | device_remove_file(dev, &devt_attr); | |
82f0cf9b JS |
777 | |
778 | if (dev->class) { | |
779 | sysfs_remove_link(&dev->kobj, "subsystem"); | |
780 | /* If this is not a "fake" compatible device, remove the | |
781 | * symlink from the class to the device. */ | |
823bccfc GKH |
782 | if (dev->kobj.parent != &dev->class->subsys.kobj) |
783 | sysfs_remove_link(&dev->class->subsys.kobj, | |
82f0cf9b | 784 | dev->bus_id); |
82f0cf9b | 785 | if (parent) { |
f7f3461d | 786 | #ifdef CONFIG_SYSFS_DEPRECATED |
82f0cf9b JS |
787 | char *class_name = make_class_name(dev->class->name, |
788 | &dev->kobj); | |
789 | if (class_name) | |
790 | sysfs_remove_link(&dev->parent->kobj, | |
791 | class_name); | |
792 | kfree(class_name); | |
f7f3461d | 793 | #endif |
82f0cf9b JS |
794 | sysfs_remove_link(&dev->kobj, "device"); |
795 | } | |
82f0cf9b | 796 | } |
a306eea4 | 797 | ueventattrError: |
ad6a1e1c | 798 | device_remove_file(dev, &uevent_attr); |
23681e47 | 799 | attrError: |
312c004d | 800 | kobject_uevent(&dev->kobj, KOBJ_REMOVE); |
1da177e4 LT |
801 | kobject_del(&dev->kobj); |
802 | Error: | |
803 | if (parent) | |
804 | put_device(parent); | |
805 | goto Done; | |
806 | } | |
807 | ||
808 | ||
809 | /** | |
810 | * device_register - register a device with the system. | |
811 | * @dev: pointer to the device structure | |
812 | * | |
813 | * This happens in two clean steps - initialize the device | |
814 | * and add it to the system. The two steps can be called | |
815 | * separately, but this is the easiest and most common. | |
816 | * I.e. you should only call the two helpers separately if | |
817 | * have a clearly defined need to use and refcount the device | |
818 | * before it is added to the hierarchy. | |
819 | */ | |
820 | ||
821 | int device_register(struct device *dev) | |
822 | { | |
823 | device_initialize(dev); | |
824 | return device_add(dev); | |
825 | } | |
826 | ||
827 | ||
828 | /** | |
829 | * get_device - increment reference count for device. | |
830 | * @dev: device. | |
831 | * | |
832 | * This simply forwards the call to kobject_get(), though | |
833 | * we do take care to provide for the case that we get a NULL | |
834 | * pointer passed in. | |
835 | */ | |
836 | ||
837 | struct device * get_device(struct device * dev) | |
838 | { | |
839 | return dev ? to_dev(kobject_get(&dev->kobj)) : NULL; | |
840 | } | |
841 | ||
842 | ||
843 | /** | |
844 | * put_device - decrement reference count. | |
845 | * @dev: device in question. | |
846 | */ | |
847 | void put_device(struct device * dev) | |
848 | { | |
849 | if (dev) | |
850 | kobject_put(&dev->kobj); | |
851 | } | |
852 | ||
853 | ||
854 | /** | |
855 | * device_del - delete device from system. | |
856 | * @dev: device. | |
857 | * | |
858 | * This is the first part of the device unregistration | |
859 | * sequence. This removes the device from the lists we control | |
860 | * from here, has it removed from the other driver model | |
861 | * subsystems it was added to in device_add(), and removes it | |
862 | * from the kobject hierarchy. | |
863 | * | |
864 | * NOTE: this should be called manually _iff_ device_add() was | |
865 | * also called manually. | |
866 | */ | |
867 | ||
868 | void device_del(struct device * dev) | |
869 | { | |
870 | struct device * parent = dev->parent; | |
c47ed219 | 871 | struct class_interface *class_intf; |
1da177e4 | 872 | |
1da177e4 | 873 | if (parent) |
d62c0f9f | 874 | klist_del(&dev->knode_parent); |
ad6a1e1c TH |
875 | if (MAJOR(dev->devt)) |
876 | device_remove_file(dev, &devt_attr); | |
b9d9c82b KS |
877 | if (dev->class) { |
878 | sysfs_remove_link(&dev->kobj, "subsystem"); | |
40fa5422 GKH |
879 | /* If this is not a "fake" compatible device, remove the |
880 | * symlink from the class to the device. */ | |
823bccfc GKH |
881 | if (dev->kobj.parent != &dev->class->subsys.kobj) |
882 | sysfs_remove_link(&dev->class->subsys.kobj, | |
40fa5422 | 883 | dev->bus_id); |
64bb5d2c | 884 | if (parent) { |
f7f3461d | 885 | #ifdef CONFIG_SYSFS_DEPRECATED |
99ef3ef8 KS |
886 | char *class_name = make_class_name(dev->class->name, |
887 | &dev->kobj); | |
cb360bbf CH |
888 | if (class_name) |
889 | sysfs_remove_link(&dev->parent->kobj, | |
890 | class_name); | |
99ef3ef8 | 891 | kfree(class_name); |
f7f3461d | 892 | #endif |
99ef3ef8 | 893 | sysfs_remove_link(&dev->kobj, "device"); |
64bb5d2c | 894 | } |
99ef3ef8 | 895 | |
5d9fd169 | 896 | down(&dev->class->sem); |
c47ed219 GKH |
897 | /* notify any interfaces that the device is now gone */ |
898 | list_for_each_entry(class_intf, &dev->class->interfaces, node) | |
899 | if (class_intf->remove_dev) | |
900 | class_intf->remove_dev(dev, class_intf); | |
901 | /* remove the device from the class list */ | |
5d9fd169 GKH |
902 | list_del_init(&dev->node); |
903 | up(&dev->class->sem); | |
86406245 KS |
904 | |
905 | /* If we live in a parent class-directory, unreference it */ | |
906 | if (dev->kobj.parent->kset == &dev->class->class_dirs) { | |
907 | struct device *d; | |
908 | int other = 0; | |
909 | ||
910 | /* | |
911 | * if we are the last child of our class, delete | |
912 | * our class-directory at this parent | |
913 | */ | |
914 | down(&dev->class->sem); | |
915 | list_for_each_entry(d, &dev->class->devices, node) { | |
916 | if (d == dev) | |
917 | continue; | |
918 | if (d->kobj.parent == dev->kobj.parent) { | |
919 | other = 1; | |
920 | break; | |
921 | } | |
922 | } | |
923 | if (!other) | |
924 | kobject_del(dev->kobj.parent); | |
925 | ||
926 | kobject_put(dev->kobj.parent); | |
927 | up(&dev->class->sem); | |
928 | } | |
b9d9c82b | 929 | } |
ad6a1e1c | 930 | device_remove_file(dev, &uevent_attr); |
2620efef | 931 | device_remove_attrs(dev); |
28953533 | 932 | bus_remove_device(dev); |
1da177e4 | 933 | |
2f8d16a9 TH |
934 | /* |
935 | * Some platform devices are driven without driver attached | |
936 | * and managed resources may have been acquired. Make sure | |
937 | * all resources are released. | |
938 | */ | |
939 | devres_release_all(dev); | |
940 | ||
1da177e4 LT |
941 | /* Notify the platform of the removal, in case they |
942 | * need to do anything... | |
943 | */ | |
944 | if (platform_notify_remove) | |
945 | platform_notify_remove(dev); | |
116af378 BH |
946 | if (dev->bus) |
947 | blocking_notifier_call_chain(&dev->bus->bus_notifier, | |
948 | BUS_NOTIFY_DEL_DEVICE, dev); | |
1da177e4 | 949 | device_pm_remove(dev); |
312c004d | 950 | kobject_uevent(&dev->kobj, KOBJ_REMOVE); |
1da177e4 LT |
951 | kobject_del(&dev->kobj); |
952 | if (parent) | |
953 | put_device(parent); | |
954 | } | |
955 | ||
956 | /** | |
957 | * device_unregister - unregister device from system. | |
958 | * @dev: device going away. | |
959 | * | |
960 | * We do this in two parts, like we do device_register(). First, | |
961 | * we remove it from all the subsystems with device_del(), then | |
962 | * we decrement the reference count via put_device(). If that | |
963 | * is the final reference count, the device will be cleaned up | |
964 | * via device_release() above. Otherwise, the structure will | |
965 | * stick around until the final reference to the device is dropped. | |
966 | */ | |
967 | void device_unregister(struct device * dev) | |
968 | { | |
969 | pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id); | |
970 | device_del(dev); | |
971 | put_device(dev); | |
972 | } | |
973 | ||
974 | ||
36239577 PM |
975 | static struct device * next_device(struct klist_iter * i) |
976 | { | |
977 | struct klist_node * n = klist_next(i); | |
978 | return n ? container_of(n, struct device, knode_parent) : NULL; | |
979 | } | |
980 | ||
1da177e4 LT |
981 | /** |
982 | * device_for_each_child - device child iterator. | |
c41455fb | 983 | * @parent: parent struct device. |
1da177e4 LT |
984 | * @data: data for the callback. |
985 | * @fn: function to be called for each device. | |
986 | * | |
c41455fb | 987 | * Iterate over @parent's child devices, and call @fn for each, |
1da177e4 LT |
988 | * passing it @data. |
989 | * | |
990 | * We check the return of @fn each time. If it returns anything | |
991 | * other than 0, we break out and return that value. | |
992 | */ | |
36239577 | 993 | int device_for_each_child(struct device * parent, void * data, |
1da177e4 LT |
994 | int (*fn)(struct device *, void *)) |
995 | { | |
36239577 | 996 | struct klist_iter i; |
1da177e4 LT |
997 | struct device * child; |
998 | int error = 0; | |
999 | ||
36239577 PM |
1000 | klist_iter_init(&parent->klist_children, &i); |
1001 | while ((child = next_device(&i)) && !error) | |
1002 | error = fn(child, data); | |
1003 | klist_iter_exit(&i); | |
1da177e4 LT |
1004 | return error; |
1005 | } | |
1006 | ||
5ab69981 CH |
1007 | /** |
1008 | * device_find_child - device iterator for locating a particular device. | |
1009 | * @parent: parent struct device | |
1010 | * @data: Data to pass to match function | |
1011 | * @match: Callback function to check device | |
1012 | * | |
1013 | * This is similar to the device_for_each_child() function above, but it | |
1014 | * returns a reference to a device that is 'found' for later use, as | |
1015 | * determined by the @match callback. | |
1016 | * | |
1017 | * The callback should return 0 if the device doesn't match and non-zero | |
1018 | * if it does. If the callback returns non-zero and a reference to the | |
1019 | * current device can be obtained, this function will return to the caller | |
1020 | * and not iterate over any more devices. | |
1021 | */ | |
1022 | struct device * device_find_child(struct device *parent, void *data, | |
1023 | int (*match)(struct device *, void *)) | |
1024 | { | |
1025 | struct klist_iter i; | |
1026 | struct device *child; | |
1027 | ||
1028 | if (!parent) | |
1029 | return NULL; | |
1030 | ||
1031 | klist_iter_init(&parent->klist_children, &i); | |
1032 | while ((child = next_device(&i))) | |
1033 | if (match(child, data) && get_device(child)) | |
1034 | break; | |
1035 | klist_iter_exit(&i); | |
1036 | return child; | |
1037 | } | |
1038 | ||
1da177e4 LT |
1039 | int __init devices_init(void) |
1040 | { | |
1041 | return subsystem_register(&devices_subsys); | |
1042 | } | |
1043 | ||
1044 | EXPORT_SYMBOL_GPL(device_for_each_child); | |
5ab69981 | 1045 | EXPORT_SYMBOL_GPL(device_find_child); |
1da177e4 LT |
1046 | |
1047 | EXPORT_SYMBOL_GPL(device_initialize); | |
1048 | EXPORT_SYMBOL_GPL(device_add); | |
1049 | EXPORT_SYMBOL_GPL(device_register); | |
1050 | ||
1051 | EXPORT_SYMBOL_GPL(device_del); | |
1052 | EXPORT_SYMBOL_GPL(device_unregister); | |
1053 | EXPORT_SYMBOL_GPL(get_device); | |
1054 | EXPORT_SYMBOL_GPL(put_device); | |
1da177e4 LT |
1055 | |
1056 | EXPORT_SYMBOL_GPL(device_create_file); | |
1057 | EXPORT_SYMBOL_GPL(device_remove_file); | |
23681e47 GKH |
1058 | |
1059 | ||
1060 | static void device_create_release(struct device *dev) | |
1061 | { | |
1062 | pr_debug("%s called for %s\n", __FUNCTION__, dev->bus_id); | |
1063 | kfree(dev); | |
1064 | } | |
1065 | ||
1066 | /** | |
1067 | * device_create - creates a device and registers it with sysfs | |
42734daf HK |
1068 | * @class: pointer to the struct class that this device should be registered to |
1069 | * @parent: pointer to the parent struct device of this new device, if any | |
1070 | * @devt: the dev_t for the char device to be added | |
1071 | * @fmt: string for the device's name | |
1072 | * | |
1073 | * This function can be used by char device classes. A struct device | |
1074 | * will be created in sysfs, registered to the specified class. | |
23681e47 | 1075 | * |
23681e47 GKH |
1076 | * A "dev" file will be created, showing the dev_t for the device, if |
1077 | * the dev_t is not 0,0. | |
42734daf HK |
1078 | * If a pointer to a parent struct device is passed in, the newly created |
1079 | * struct device will be a child of that device in sysfs. | |
1080 | * The pointer to the struct device will be returned from the call. | |
1081 | * Any further sysfs files that might be required can be created using this | |
23681e47 GKH |
1082 | * pointer. |
1083 | * | |
1084 | * Note: the struct class passed to this function must have previously | |
1085 | * been created with a call to class_create(). | |
1086 | */ | |
1087 | struct device *device_create(struct class *class, struct device *parent, | |
5cbe5f8a | 1088 | dev_t devt, const char *fmt, ...) |
23681e47 GKH |
1089 | { |
1090 | va_list args; | |
1091 | struct device *dev = NULL; | |
1092 | int retval = -ENODEV; | |
1093 | ||
1094 | if (class == NULL || IS_ERR(class)) | |
1095 | goto error; | |
23681e47 GKH |
1096 | |
1097 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); | |
1098 | if (!dev) { | |
1099 | retval = -ENOMEM; | |
1100 | goto error; | |
1101 | } | |
1102 | ||
1103 | dev->devt = devt; | |
1104 | dev->class = class; | |
1105 | dev->parent = parent; | |
1106 | dev->release = device_create_release; | |
1107 | ||
1108 | va_start(args, fmt); | |
1109 | vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args); | |
1110 | va_end(args); | |
1111 | retval = device_register(dev); | |
1112 | if (retval) | |
1113 | goto error; | |
1114 | ||
23681e47 GKH |
1115 | return dev; |
1116 | ||
1117 | error: | |
1118 | kfree(dev); | |
1119 | return ERR_PTR(retval); | |
1120 | } | |
1121 | EXPORT_SYMBOL_GPL(device_create); | |
1122 | ||
1123 | /** | |
1124 | * device_destroy - removes a device that was created with device_create() | |
42734daf HK |
1125 | * @class: pointer to the struct class that this device was registered with |
1126 | * @devt: the dev_t of the device that was previously registered | |
23681e47 | 1127 | * |
42734daf HK |
1128 | * This call unregisters and cleans up a device that was created with a |
1129 | * call to device_create(). | |
23681e47 GKH |
1130 | */ |
1131 | void device_destroy(struct class *class, dev_t devt) | |
1132 | { | |
1133 | struct device *dev = NULL; | |
1134 | struct device *dev_tmp; | |
1135 | ||
1136 | down(&class->sem); | |
1137 | list_for_each_entry(dev_tmp, &class->devices, node) { | |
1138 | if (dev_tmp->devt == devt) { | |
1139 | dev = dev_tmp; | |
1140 | break; | |
1141 | } | |
1142 | } | |
1143 | up(&class->sem); | |
1144 | ||
5d9fd169 | 1145 | if (dev) |
23681e47 | 1146 | device_unregister(dev); |
23681e47 GKH |
1147 | } |
1148 | EXPORT_SYMBOL_GPL(device_destroy); | |
a2de48ca GKH |
1149 | |
1150 | /** | |
1151 | * device_rename - renames a device | |
1152 | * @dev: the pointer to the struct device to be renamed | |
1153 | * @new_name: the new name of the device | |
1154 | */ | |
1155 | int device_rename(struct device *dev, char *new_name) | |
1156 | { | |
1157 | char *old_class_name = NULL; | |
1158 | char *new_class_name = NULL; | |
1159 | char *old_symlink_name = NULL; | |
1160 | int error; | |
1161 | ||
1162 | dev = get_device(dev); | |
1163 | if (!dev) | |
1164 | return -EINVAL; | |
1165 | ||
1166 | pr_debug("DEVICE: renaming '%s' to '%s'\n", dev->bus_id, new_name); | |
1167 | ||
99ef3ef8 | 1168 | #ifdef CONFIG_SYSFS_DEPRECATED |
a2de48ca GKH |
1169 | if ((dev->class) && (dev->parent)) |
1170 | old_class_name = make_class_name(dev->class->name, &dev->kobj); | |
99ef3ef8 | 1171 | #endif |
a2de48ca GKH |
1172 | |
1173 | if (dev->class) { | |
1174 | old_symlink_name = kmalloc(BUS_ID_SIZE, GFP_KERNEL); | |
952ab431 JJ |
1175 | if (!old_symlink_name) { |
1176 | error = -ENOMEM; | |
1177 | goto out_free_old_class; | |
1178 | } | |
a2de48ca GKH |
1179 | strlcpy(old_symlink_name, dev->bus_id, BUS_ID_SIZE); |
1180 | } | |
1181 | ||
1182 | strlcpy(dev->bus_id, new_name, BUS_ID_SIZE); | |
1183 | ||
1184 | error = kobject_rename(&dev->kobj, new_name); | |
1185 | ||
99ef3ef8 | 1186 | #ifdef CONFIG_SYSFS_DEPRECATED |
a2de48ca GKH |
1187 | if (old_class_name) { |
1188 | new_class_name = make_class_name(dev->class->name, &dev->kobj); | |
1189 | if (new_class_name) { | |
1190 | sysfs_create_link(&dev->parent->kobj, &dev->kobj, | |
1191 | new_class_name); | |
1192 | sysfs_remove_link(&dev->parent->kobj, old_class_name); | |
1193 | } | |
1194 | } | |
99ef3ef8 KS |
1195 | #endif |
1196 | ||
a2de48ca | 1197 | if (dev->class) { |
823bccfc | 1198 | sysfs_remove_link(&dev->class->subsys.kobj, |
a2de48ca | 1199 | old_symlink_name); |
823bccfc | 1200 | sysfs_create_link(&dev->class->subsys.kobj, &dev->kobj, |
a2de48ca GKH |
1201 | dev->bus_id); |
1202 | } | |
1203 | put_device(dev); | |
1204 | ||
a2de48ca GKH |
1205 | kfree(new_class_name); |
1206 | kfree(old_symlink_name); | |
952ab431 JJ |
1207 | out_free_old_class: |
1208 | kfree(old_class_name); | |
a2de48ca GKH |
1209 | |
1210 | return error; | |
1211 | } | |
a2807dbc | 1212 | EXPORT_SYMBOL_GPL(device_rename); |
8a82472f CH |
1213 | |
1214 | static int device_move_class_links(struct device *dev, | |
1215 | struct device *old_parent, | |
1216 | struct device *new_parent) | |
1217 | { | |
f7f3461d | 1218 | int error = 0; |
8a82472f | 1219 | #ifdef CONFIG_SYSFS_DEPRECATED |
8a82472f CH |
1220 | char *class_name; |
1221 | ||
1222 | class_name = make_class_name(dev->class->name, &dev->kobj); | |
1223 | if (!class_name) { | |
cb360bbf | 1224 | error = -ENOMEM; |
8a82472f CH |
1225 | goto out; |
1226 | } | |
1227 | if (old_parent) { | |
1228 | sysfs_remove_link(&dev->kobj, "device"); | |
1229 | sysfs_remove_link(&old_parent->kobj, class_name); | |
1230 | } | |
c744aeae CH |
1231 | if (new_parent) { |
1232 | error = sysfs_create_link(&dev->kobj, &new_parent->kobj, | |
1233 | "device"); | |
1234 | if (error) | |
1235 | goto out; | |
1236 | error = sysfs_create_link(&new_parent->kobj, &dev->kobj, | |
1237 | class_name); | |
1238 | if (error) | |
1239 | sysfs_remove_link(&dev->kobj, "device"); | |
1240 | } | |
1241 | else | |
1242 | error = 0; | |
8a82472f CH |
1243 | out: |
1244 | kfree(class_name); | |
1245 | return error; | |
1246 | #else | |
f7f3461d GKH |
1247 | if (old_parent) |
1248 | sysfs_remove_link(&dev->kobj, "device"); | |
1249 | if (new_parent) | |
1250 | error = sysfs_create_link(&dev->kobj, &new_parent->kobj, | |
1251 | "device"); | |
1252 | return error; | |
8a82472f CH |
1253 | #endif |
1254 | } | |
1255 | ||
1256 | /** | |
1257 | * device_move - moves a device to a new parent | |
1258 | * @dev: the pointer to the struct device to be moved | |
c744aeae | 1259 | * @new_parent: the new parent of the device (can by NULL) |
8a82472f CH |
1260 | */ |
1261 | int device_move(struct device *dev, struct device *new_parent) | |
1262 | { | |
1263 | int error; | |
1264 | struct device *old_parent; | |
c744aeae | 1265 | struct kobject *new_parent_kobj; |
8a82472f CH |
1266 | |
1267 | dev = get_device(dev); | |
1268 | if (!dev) | |
1269 | return -EINVAL; | |
1270 | ||
8a82472f | 1271 | new_parent = get_device(new_parent); |
c744aeae CH |
1272 | new_parent_kobj = get_device_parent (dev, new_parent); |
1273 | if (IS_ERR(new_parent_kobj)) { | |
1274 | error = PTR_ERR(new_parent_kobj); | |
1275 | put_device(new_parent); | |
8a82472f CH |
1276 | goto out; |
1277 | } | |
1278 | pr_debug("DEVICE: moving '%s' to '%s'\n", dev->bus_id, | |
c744aeae CH |
1279 | new_parent ? new_parent->bus_id : "<NULL>"); |
1280 | error = kobject_move(&dev->kobj, new_parent_kobj); | |
8a82472f CH |
1281 | if (error) { |
1282 | put_device(new_parent); | |
1283 | goto out; | |
1284 | } | |
1285 | old_parent = dev->parent; | |
1286 | dev->parent = new_parent; | |
1287 | if (old_parent) | |
acf02d23 | 1288 | klist_remove(&dev->knode_parent); |
c744aeae CH |
1289 | if (new_parent) |
1290 | klist_add_tail(&dev->knode_parent, &new_parent->klist_children); | |
8a82472f CH |
1291 | if (!dev->class) |
1292 | goto out_put; | |
1293 | error = device_move_class_links(dev, old_parent, new_parent); | |
1294 | if (error) { | |
1295 | /* We ignore errors on cleanup since we're hosed anyway... */ | |
1296 | device_move_class_links(dev, new_parent, old_parent); | |
1297 | if (!kobject_move(&dev->kobj, &old_parent->kobj)) { | |
c744aeae CH |
1298 | if (new_parent) |
1299 | klist_remove(&dev->knode_parent); | |
8a82472f CH |
1300 | if (old_parent) |
1301 | klist_add_tail(&dev->knode_parent, | |
1302 | &old_parent->klist_children); | |
1303 | } | |
1304 | put_device(new_parent); | |
1305 | goto out; | |
1306 | } | |
1307 | out_put: | |
1308 | put_device(old_parent); | |
1309 | out: | |
1310 | put_device(dev); | |
1311 | return error; | |
1312 | } | |
1313 | ||
1314 | EXPORT_SYMBOL_GPL(device_move); |