]>
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> |
da231fd5 | 21 | #include <linux/genhd.h> |
815d2d50 | 22 | #include <linux/kallsyms.h> |
f75b1c60 | 23 | #include <linux/mutex.h> |
401097ea | 24 | #include <linux/async.h> |
1da177e4 LT |
25 | |
26 | #include "base.h" | |
27 | #include "power/power.h" | |
28 | ||
4a3ad20c GKH |
29 | int (*platform_notify)(struct device *dev) = NULL; |
30 | int (*platform_notify_remove)(struct device *dev) = NULL; | |
e105b8bf DW |
31 | static struct kobject *dev_kobj; |
32 | struct kobject *sysfs_dev_char_kobj; | |
33 | struct kobject *sysfs_dev_block_kobj; | |
1da177e4 | 34 | |
4e886c29 GKH |
35 | #ifdef CONFIG_BLOCK |
36 | static inline int device_is_not_partition(struct device *dev) | |
37 | { | |
38 | return !(dev->type == &part_type); | |
39 | } | |
40 | #else | |
41 | static inline int device_is_not_partition(struct device *dev) | |
42 | { | |
43 | return 1; | |
44 | } | |
45 | #endif | |
1da177e4 | 46 | |
3e95637a AS |
47 | /** |
48 | * dev_driver_string - Return a device's driver name, if at all possible | |
49 | * @dev: struct device to get the name of | |
50 | * | |
51 | * Will return the device's driver's name if it is bound to a device. If | |
52 | * the device is not bound to a device, it will return the name of the bus | |
53 | * it is attached to. If it is not attached to a bus either, an empty | |
54 | * string will be returned. | |
55 | */ | |
bf9ca69f | 56 | const char *dev_driver_string(const struct device *dev) |
3e95637a | 57 | { |
3589972e AS |
58 | struct device_driver *drv; |
59 | ||
60 | /* dev->driver can change to NULL underneath us because of unbinding, | |
61 | * so be careful about accessing it. dev->bus and dev->class should | |
62 | * never change once they are set, so they don't need special care. | |
63 | */ | |
64 | drv = ACCESS_ONCE(dev->driver); | |
65 | return drv ? drv->name : | |
a456b702 JD |
66 | (dev->bus ? dev->bus->name : |
67 | (dev->class ? dev->class->name : "")); | |
3e95637a | 68 | } |
310a922d | 69 | EXPORT_SYMBOL(dev_driver_string); |
3e95637a | 70 | |
1da177e4 LT |
71 | #define to_dev(obj) container_of(obj, struct device, kobj) |
72 | #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr) | |
73 | ||
4a3ad20c GKH |
74 | static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr, |
75 | char *buf) | |
1da177e4 | 76 | { |
4a3ad20c GKH |
77 | struct device_attribute *dev_attr = to_dev_attr(attr); |
78 | struct device *dev = to_dev(kobj); | |
4a0c20bf | 79 | ssize_t ret = -EIO; |
1da177e4 LT |
80 | |
81 | if (dev_attr->show) | |
54b6f35c | 82 | ret = dev_attr->show(dev, dev_attr, buf); |
815d2d50 AM |
83 | if (ret >= (ssize_t)PAGE_SIZE) { |
84 | print_symbol("dev_attr_show: %s returned bad count\n", | |
85 | (unsigned long)dev_attr->show); | |
86 | } | |
1da177e4 LT |
87 | return ret; |
88 | } | |
89 | ||
4a3ad20c GKH |
90 | static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr, |
91 | const char *buf, size_t count) | |
1da177e4 | 92 | { |
4a3ad20c GKH |
93 | struct device_attribute *dev_attr = to_dev_attr(attr); |
94 | struct device *dev = to_dev(kobj); | |
4a0c20bf | 95 | ssize_t ret = -EIO; |
1da177e4 LT |
96 | |
97 | if (dev_attr->store) | |
54b6f35c | 98 | ret = dev_attr->store(dev, dev_attr, buf, count); |
1da177e4 LT |
99 | return ret; |
100 | } | |
101 | ||
52cf25d0 | 102 | static const struct sysfs_ops dev_sysfs_ops = { |
1da177e4 LT |
103 | .show = dev_attr_show, |
104 | .store = dev_attr_store, | |
105 | }; | |
106 | ||
107 | ||
108 | /** | |
109 | * device_release - free device structure. | |
110 | * @kobj: device's kobject. | |
111 | * | |
112 | * This is called once the reference count for the object | |
113 | * reaches 0. We forward the call to the device's release | |
114 | * method, which should handle actually freeing the structure. | |
115 | */ | |
4a3ad20c | 116 | static void device_release(struct kobject *kobj) |
1da177e4 | 117 | { |
4a3ad20c | 118 | struct device *dev = to_dev(kobj); |
fb069a5d | 119 | struct device_private *p = dev->p; |
1da177e4 LT |
120 | |
121 | if (dev->release) | |
122 | dev->release(dev); | |
f9f852df KS |
123 | else if (dev->type && dev->type->release) |
124 | dev->type->release(dev); | |
2620efef GKH |
125 | else if (dev->class && dev->class->dev_release) |
126 | dev->class->dev_release(dev); | |
f810a5cf AV |
127 | else |
128 | WARN(1, KERN_ERR "Device '%s' does not have a release() " | |
4a3ad20c | 129 | "function, it is broken and must be fixed.\n", |
1e0b2cf9 | 130 | dev_name(dev)); |
fb069a5d | 131 | kfree(p); |
1da177e4 LT |
132 | } |
133 | ||
8f4afc41 | 134 | static struct kobj_type device_ktype = { |
1da177e4 LT |
135 | .release = device_release, |
136 | .sysfs_ops = &dev_sysfs_ops, | |
1da177e4 LT |
137 | }; |
138 | ||
139 | ||
312c004d | 140 | static int dev_uevent_filter(struct kset *kset, struct kobject *kobj) |
1da177e4 LT |
141 | { |
142 | struct kobj_type *ktype = get_ktype(kobj); | |
143 | ||
8f4afc41 | 144 | if (ktype == &device_ktype) { |
1da177e4 LT |
145 | struct device *dev = to_dev(kobj); |
146 | if (dev->bus) | |
147 | return 1; | |
23681e47 GKH |
148 | if (dev->class) |
149 | return 1; | |
1da177e4 LT |
150 | } |
151 | return 0; | |
152 | } | |
153 | ||
312c004d | 154 | static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj) |
1da177e4 LT |
155 | { |
156 | struct device *dev = to_dev(kobj); | |
157 | ||
23681e47 GKH |
158 | if (dev->bus) |
159 | return dev->bus->name; | |
160 | if (dev->class) | |
161 | return dev->class->name; | |
162 | return NULL; | |
1da177e4 LT |
163 | } |
164 | ||
7eff2e7a KS |
165 | static int dev_uevent(struct kset *kset, struct kobject *kobj, |
166 | struct kobj_uevent_env *env) | |
1da177e4 LT |
167 | { |
168 | struct device *dev = to_dev(kobj); | |
1da177e4 LT |
169 | int retval = 0; |
170 | ||
6fcf53ac | 171 | /* add device node properties if present */ |
23681e47 | 172 | if (MAJOR(dev->devt)) { |
6fcf53ac KS |
173 | const char *tmp; |
174 | const char *name; | |
e454cea2 | 175 | mode_t mode = 0; |
6fcf53ac | 176 | |
7eff2e7a KS |
177 | add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt)); |
178 | add_uevent_var(env, "MINOR=%u", MINOR(dev->devt)); | |
e454cea2 | 179 | name = device_get_devnode(dev, &mode, &tmp); |
6fcf53ac KS |
180 | if (name) { |
181 | add_uevent_var(env, "DEVNAME=%s", name); | |
182 | kfree(tmp); | |
e454cea2 KS |
183 | if (mode) |
184 | add_uevent_var(env, "DEVMODE=%#o", mode & 0777); | |
6fcf53ac | 185 | } |
23681e47 GKH |
186 | } |
187 | ||
414264f9 | 188 | if (dev->type && dev->type->name) |
7eff2e7a | 189 | add_uevent_var(env, "DEVTYPE=%s", dev->type->name); |
414264f9 | 190 | |
239378f1 | 191 | if (dev->driver) |
7eff2e7a | 192 | add_uevent_var(env, "DRIVER=%s", dev->driver->name); |
239378f1 | 193 | |
a87cb2ac | 194 | #ifdef CONFIG_SYSFS_DEPRECATED |
239378f1 KS |
195 | if (dev->class) { |
196 | struct device *parent = dev->parent; | |
197 | ||
198 | /* find first bus device in parent chain */ | |
199 | while (parent && !parent->bus) | |
200 | parent = parent->parent; | |
201 | if (parent && parent->bus) { | |
202 | const char *path; | |
203 | ||
204 | path = kobject_get_path(&parent->kobj, GFP_KERNEL); | |
2c7afd12 | 205 | if (path) { |
7eff2e7a | 206 | add_uevent_var(env, "PHYSDEVPATH=%s", path); |
2c7afd12 KS |
207 | kfree(path); |
208 | } | |
239378f1 | 209 | |
7eff2e7a | 210 | add_uevent_var(env, "PHYSDEVBUS=%s", parent->bus->name); |
239378f1 KS |
211 | |
212 | if (parent->driver) | |
7eff2e7a KS |
213 | add_uevent_var(env, "PHYSDEVDRIVER=%s", |
214 | parent->driver->name); | |
239378f1 KS |
215 | } |
216 | } else if (dev->bus) { | |
7eff2e7a | 217 | add_uevent_var(env, "PHYSDEVBUS=%s", dev->bus->name); |
239378f1 KS |
218 | |
219 | if (dev->driver) | |
4a3ad20c GKH |
220 | add_uevent_var(env, "PHYSDEVDRIVER=%s", |
221 | dev->driver->name); | |
d81d9d6b | 222 | } |
239378f1 | 223 | #endif |
1da177e4 | 224 | |
7eff2e7a | 225 | /* have the bus specific function add its stuff */ |
312c004d | 226 | if (dev->bus && dev->bus->uevent) { |
7eff2e7a | 227 | retval = dev->bus->uevent(dev, env); |
f9f852df | 228 | if (retval) |
7dc72b28 | 229 | pr_debug("device: '%s': %s: bus uevent() returned %d\n", |
1e0b2cf9 | 230 | dev_name(dev), __func__, retval); |
1da177e4 LT |
231 | } |
232 | ||
7eff2e7a | 233 | /* have the class specific function add its stuff */ |
2620efef | 234 | if (dev->class && dev->class->dev_uevent) { |
7eff2e7a | 235 | retval = dev->class->dev_uevent(dev, env); |
f9f852df | 236 | if (retval) |
7dc72b28 | 237 | pr_debug("device: '%s': %s: class uevent() " |
1e0b2cf9 | 238 | "returned %d\n", dev_name(dev), |
2b3a302a | 239 | __func__, retval); |
f9f852df KS |
240 | } |
241 | ||
7eff2e7a | 242 | /* have the device type specific fuction add its stuff */ |
f9f852df | 243 | if (dev->type && dev->type->uevent) { |
7eff2e7a | 244 | retval = dev->type->uevent(dev, env); |
f9f852df | 245 | if (retval) |
7dc72b28 | 246 | pr_debug("device: '%s': %s: dev_type uevent() " |
1e0b2cf9 | 247 | "returned %d\n", dev_name(dev), |
2b3a302a | 248 | __func__, retval); |
2620efef GKH |
249 | } |
250 | ||
1da177e4 LT |
251 | return retval; |
252 | } | |
253 | ||
9cd43611 | 254 | static const struct kset_uevent_ops device_uevent_ops = { |
312c004d KS |
255 | .filter = dev_uevent_filter, |
256 | .name = dev_uevent_name, | |
257 | .uevent = dev_uevent, | |
1da177e4 LT |
258 | }; |
259 | ||
16574dcc KS |
260 | static ssize_t show_uevent(struct device *dev, struct device_attribute *attr, |
261 | char *buf) | |
262 | { | |
263 | struct kobject *top_kobj; | |
264 | struct kset *kset; | |
7eff2e7a | 265 | struct kobj_uevent_env *env = NULL; |
16574dcc KS |
266 | int i; |
267 | size_t count = 0; | |
268 | int retval; | |
269 | ||
270 | /* search the kset, the device belongs to */ | |
271 | top_kobj = &dev->kobj; | |
5c5daf65 KS |
272 | while (!top_kobj->kset && top_kobj->parent) |
273 | top_kobj = top_kobj->parent; | |
16574dcc KS |
274 | if (!top_kobj->kset) |
275 | goto out; | |
5c5daf65 | 276 | |
16574dcc KS |
277 | kset = top_kobj->kset; |
278 | if (!kset->uevent_ops || !kset->uevent_ops->uevent) | |
279 | goto out; | |
280 | ||
281 | /* respect filter */ | |
282 | if (kset->uevent_ops && kset->uevent_ops->filter) | |
283 | if (!kset->uevent_ops->filter(kset, &dev->kobj)) | |
284 | goto out; | |
285 | ||
7eff2e7a KS |
286 | env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL); |
287 | if (!env) | |
c7308c81 GKH |
288 | return -ENOMEM; |
289 | ||
16574dcc | 290 | /* let the kset specific function add its keys */ |
7eff2e7a | 291 | retval = kset->uevent_ops->uevent(kset, &dev->kobj, env); |
16574dcc KS |
292 | if (retval) |
293 | goto out; | |
294 | ||
295 | /* copy keys to file */ | |
7eff2e7a KS |
296 | for (i = 0; i < env->envp_idx; i++) |
297 | count += sprintf(&buf[count], "%s\n", env->envp[i]); | |
16574dcc | 298 | out: |
7eff2e7a | 299 | kfree(env); |
16574dcc KS |
300 | return count; |
301 | } | |
302 | ||
a7fd6706 KS |
303 | static ssize_t store_uevent(struct device *dev, struct device_attribute *attr, |
304 | const char *buf, size_t count) | |
305 | { | |
60a96a59 KS |
306 | enum kobject_action action; |
307 | ||
3f5468c9 | 308 | if (kobject_action_type(buf, count, &action) == 0) |
60a96a59 | 309 | kobject_uevent(&dev->kobj, action); |
3f5468c9 KS |
310 | else |
311 | dev_err(dev, "uevent: unknown action-string\n"); | |
a7fd6706 KS |
312 | return count; |
313 | } | |
314 | ||
ad6a1e1c TH |
315 | static struct device_attribute uevent_attr = |
316 | __ATTR(uevent, S_IRUGO | S_IWUSR, show_uevent, store_uevent); | |
317 | ||
621a1672 DT |
318 | static int device_add_attributes(struct device *dev, |
319 | struct device_attribute *attrs) | |
de0ff00d | 320 | { |
621a1672 | 321 | int error = 0; |
de0ff00d | 322 | int i; |
621a1672 DT |
323 | |
324 | if (attrs) { | |
325 | for (i = 0; attr_name(attrs[i]); i++) { | |
326 | error = device_create_file(dev, &attrs[i]); | |
327 | if (error) | |
328 | break; | |
329 | } | |
330 | if (error) | |
331 | while (--i >= 0) | |
332 | device_remove_file(dev, &attrs[i]); | |
333 | } | |
334 | return error; | |
335 | } | |
336 | ||
337 | static void device_remove_attributes(struct device *dev, | |
338 | struct device_attribute *attrs) | |
339 | { | |
340 | int i; | |
341 | ||
342 | if (attrs) | |
343 | for (i = 0; attr_name(attrs[i]); i++) | |
344 | device_remove_file(dev, &attrs[i]); | |
345 | } | |
346 | ||
347 | static int device_add_groups(struct device *dev, | |
a4dbd674 | 348 | const struct attribute_group **groups) |
621a1672 | 349 | { |
de0ff00d | 350 | int error = 0; |
621a1672 | 351 | int i; |
de0ff00d | 352 | |
621a1672 DT |
353 | if (groups) { |
354 | for (i = 0; groups[i]; i++) { | |
355 | error = sysfs_create_group(&dev->kobj, groups[i]); | |
de0ff00d GKH |
356 | if (error) { |
357 | while (--i >= 0) | |
4a3ad20c GKH |
358 | sysfs_remove_group(&dev->kobj, |
359 | groups[i]); | |
621a1672 | 360 | break; |
de0ff00d GKH |
361 | } |
362 | } | |
363 | } | |
de0ff00d GKH |
364 | return error; |
365 | } | |
366 | ||
621a1672 | 367 | static void device_remove_groups(struct device *dev, |
a4dbd674 | 368 | const struct attribute_group **groups) |
de0ff00d GKH |
369 | { |
370 | int i; | |
621a1672 DT |
371 | |
372 | if (groups) | |
373 | for (i = 0; groups[i]; i++) | |
374 | sysfs_remove_group(&dev->kobj, groups[i]); | |
de0ff00d GKH |
375 | } |
376 | ||
2620efef GKH |
377 | static int device_add_attrs(struct device *dev) |
378 | { | |
379 | struct class *class = dev->class; | |
f9f852df | 380 | struct device_type *type = dev->type; |
621a1672 | 381 | int error; |
2620efef | 382 | |
621a1672 DT |
383 | if (class) { |
384 | error = device_add_attributes(dev, class->dev_attrs); | |
f9f852df | 385 | if (error) |
621a1672 | 386 | return error; |
2620efef | 387 | } |
f9f852df | 388 | |
621a1672 DT |
389 | if (type) { |
390 | error = device_add_groups(dev, type->groups); | |
f9f852df | 391 | if (error) |
621a1672 | 392 | goto err_remove_class_attrs; |
f9f852df KS |
393 | } |
394 | ||
621a1672 DT |
395 | error = device_add_groups(dev, dev->groups); |
396 | if (error) | |
397 | goto err_remove_type_groups; | |
398 | ||
399 | return 0; | |
400 | ||
401 | err_remove_type_groups: | |
402 | if (type) | |
403 | device_remove_groups(dev, type->groups); | |
404 | err_remove_class_attrs: | |
405 | if (class) | |
406 | device_remove_attributes(dev, class->dev_attrs); | |
407 | ||
2620efef GKH |
408 | return error; |
409 | } | |
410 | ||
411 | static void device_remove_attrs(struct device *dev) | |
412 | { | |
413 | struct class *class = dev->class; | |
f9f852df | 414 | struct device_type *type = dev->type; |
2620efef | 415 | |
621a1672 | 416 | device_remove_groups(dev, dev->groups); |
f9f852df | 417 | |
621a1672 DT |
418 | if (type) |
419 | device_remove_groups(dev, type->groups); | |
420 | ||
421 | if (class) | |
422 | device_remove_attributes(dev, class->dev_attrs); | |
2620efef GKH |
423 | } |
424 | ||
425 | ||
23681e47 GKH |
426 | static ssize_t show_dev(struct device *dev, struct device_attribute *attr, |
427 | char *buf) | |
428 | { | |
429 | return print_dev_t(buf, dev->devt); | |
430 | } | |
431 | ||
ad6a1e1c TH |
432 | static struct device_attribute devt_attr = |
433 | __ATTR(dev, S_IRUGO, show_dev, NULL); | |
434 | ||
881c6cfd GKH |
435 | /* kset to create /sys/devices/ */ |
436 | struct kset *devices_kset; | |
1da177e4 | 437 | |
1da177e4 | 438 | /** |
4a3ad20c GKH |
439 | * device_create_file - create sysfs attribute file for device. |
440 | * @dev: device. | |
441 | * @attr: device attribute descriptor. | |
1da177e4 | 442 | */ |
26579ab7 PC |
443 | int device_create_file(struct device *dev, |
444 | const struct device_attribute *attr) | |
1da177e4 LT |
445 | { |
446 | int error = 0; | |
0c98b19f | 447 | if (dev) |
1da177e4 | 448 | error = sysfs_create_file(&dev->kobj, &attr->attr); |
1da177e4 LT |
449 | return error; |
450 | } | |
451 | ||
452 | /** | |
4a3ad20c GKH |
453 | * device_remove_file - remove sysfs attribute file. |
454 | * @dev: device. | |
455 | * @attr: device attribute descriptor. | |
1da177e4 | 456 | */ |
26579ab7 PC |
457 | void device_remove_file(struct device *dev, |
458 | const struct device_attribute *attr) | |
1da177e4 | 459 | { |
0c98b19f | 460 | if (dev) |
1da177e4 | 461 | sysfs_remove_file(&dev->kobj, &attr->attr); |
1da177e4 LT |
462 | } |
463 | ||
2589f188 GKH |
464 | /** |
465 | * device_create_bin_file - create sysfs binary attribute file for device. | |
466 | * @dev: device. | |
467 | * @attr: device binary attribute descriptor. | |
468 | */ | |
66ecb92b PC |
469 | int device_create_bin_file(struct device *dev, |
470 | const struct bin_attribute *attr) | |
2589f188 GKH |
471 | { |
472 | int error = -EINVAL; | |
473 | if (dev) | |
474 | error = sysfs_create_bin_file(&dev->kobj, attr); | |
475 | return error; | |
476 | } | |
477 | EXPORT_SYMBOL_GPL(device_create_bin_file); | |
478 | ||
479 | /** | |
480 | * device_remove_bin_file - remove sysfs binary attribute file | |
481 | * @dev: device. | |
482 | * @attr: device binary attribute descriptor. | |
483 | */ | |
66ecb92b PC |
484 | void device_remove_bin_file(struct device *dev, |
485 | const struct bin_attribute *attr) | |
2589f188 GKH |
486 | { |
487 | if (dev) | |
488 | sysfs_remove_bin_file(&dev->kobj, attr); | |
489 | } | |
490 | EXPORT_SYMBOL_GPL(device_remove_bin_file); | |
491 | ||
d9a9cdfb | 492 | /** |
523ded71 | 493 | * device_schedule_callback_owner - helper to schedule a callback for a device |
d9a9cdfb AS |
494 | * @dev: device. |
495 | * @func: callback function to invoke later. | |
523ded71 | 496 | * @owner: module owning the callback routine |
d9a9cdfb AS |
497 | * |
498 | * Attribute methods must not unregister themselves or their parent device | |
499 | * (which would amount to the same thing). Attempts to do so will deadlock, | |
500 | * since unregistration is mutually exclusive with driver callbacks. | |
501 | * | |
502 | * Instead methods can call this routine, which will attempt to allocate | |
503 | * and schedule a workqueue request to call back @func with @dev as its | |
504 | * argument in the workqueue's process context. @dev will be pinned until | |
505 | * @func returns. | |
506 | * | |
523ded71 AS |
507 | * This routine is usually called via the inline device_schedule_callback(), |
508 | * which automatically sets @owner to THIS_MODULE. | |
509 | * | |
d9a9cdfb | 510 | * Returns 0 if the request was submitted, -ENOMEM if storage could not |
523ded71 | 511 | * be allocated, -ENODEV if a reference to @owner isn't available. |
d9a9cdfb AS |
512 | * |
513 | * NOTE: This routine won't work if CONFIG_SYSFS isn't set! It uses an | |
514 | * underlying sysfs routine (since it is intended for use by attribute | |
515 | * methods), and if sysfs isn't available you'll get nothing but -ENOSYS. | |
516 | */ | |
523ded71 AS |
517 | int device_schedule_callback_owner(struct device *dev, |
518 | void (*func)(struct device *), struct module *owner) | |
d9a9cdfb AS |
519 | { |
520 | return sysfs_schedule_callback(&dev->kobj, | |
523ded71 | 521 | (void (*)(void *)) func, dev, owner); |
d9a9cdfb | 522 | } |
523ded71 | 523 | EXPORT_SYMBOL_GPL(device_schedule_callback_owner); |
d9a9cdfb | 524 | |
34bb61f9 JB |
525 | static void klist_children_get(struct klist_node *n) |
526 | { | |
f791b8c8 GKH |
527 | struct device_private *p = to_device_private_parent(n); |
528 | struct device *dev = p->device; | |
34bb61f9 JB |
529 | |
530 | get_device(dev); | |
531 | } | |
532 | ||
533 | static void klist_children_put(struct klist_node *n) | |
534 | { | |
f791b8c8 GKH |
535 | struct device_private *p = to_device_private_parent(n); |
536 | struct device *dev = p->device; | |
34bb61f9 JB |
537 | |
538 | put_device(dev); | |
539 | } | |
540 | ||
1da177e4 | 541 | /** |
4a3ad20c GKH |
542 | * device_initialize - init device structure. |
543 | * @dev: device. | |
1da177e4 | 544 | * |
5739411a CH |
545 | * This prepares the device for use by other layers by initializing |
546 | * its fields. | |
4a3ad20c | 547 | * It is the first half of device_register(), if called by |
5739411a CH |
548 | * that function, though it can also be called separately, so one |
549 | * may use @dev's fields. In particular, get_device()/put_device() | |
550 | * may be used for reference counting of @dev after calling this | |
551 | * function. | |
552 | * | |
553 | * NOTE: Use put_device() to give up your reference instead of freeing | |
554 | * @dev directly once you have called this function. | |
1da177e4 | 555 | */ |
1da177e4 LT |
556 | void device_initialize(struct device *dev) |
557 | { | |
881c6cfd | 558 | dev->kobj.kset = devices_kset; |
f9cb074b | 559 | kobject_init(&dev->kobj, &device_ktype); |
1da177e4 | 560 | INIT_LIST_HEAD(&dev->dma_pools); |
3142788b | 561 | mutex_init(&dev->mutex); |
1704f47b | 562 | lockdep_set_novalidate_class(&dev->mutex); |
9ac7849e TH |
563 | spin_lock_init(&dev->devres_lock); |
564 | INIT_LIST_HEAD(&dev->devres_head); | |
3b98aeaf | 565 | device_pm_init(dev); |
87348136 | 566 | set_dev_node(dev, -1); |
1da177e4 LT |
567 | } |
568 | ||
40fa5422 | 569 | #ifdef CONFIG_SYSFS_DEPRECATED |
da231fd5 KS |
570 | static struct kobject *get_device_parent(struct device *dev, |
571 | struct device *parent) | |
40fa5422 | 572 | { |
da231fd5 | 573 | /* class devices without a parent live in /sys/class/<classname>/ */ |
3eb215de | 574 | if (dev->class && (!parent || parent->class != dev->class)) |
1fbfee6c | 575 | return &dev->class->p->class_subsys.kobj; |
da231fd5 | 576 | /* all other devices keep their parent */ |
40fa5422 | 577 | else if (parent) |
c744aeae | 578 | return &parent->kobj; |
40fa5422 | 579 | |
c744aeae | 580 | return NULL; |
40fa5422 | 581 | } |
da231fd5 KS |
582 | |
583 | static inline void cleanup_device_parent(struct device *dev) {} | |
63b6971a CH |
584 | static inline void cleanup_glue_dir(struct device *dev, |
585 | struct kobject *glue_dir) {} | |
40fa5422 | 586 | #else |
86406245 | 587 | static struct kobject *virtual_device_parent(struct device *dev) |
f0ee61a6 | 588 | { |
86406245 | 589 | static struct kobject *virtual_dir = NULL; |
f0ee61a6 | 590 | |
86406245 | 591 | if (!virtual_dir) |
4ff6abff | 592 | virtual_dir = kobject_create_and_add("virtual", |
881c6cfd | 593 | &devices_kset->kobj); |
f0ee61a6 | 594 | |
86406245 | 595 | return virtual_dir; |
f0ee61a6 GKH |
596 | } |
597 | ||
da231fd5 KS |
598 | static struct kobject *get_device_parent(struct device *dev, |
599 | struct device *parent) | |
40fa5422 | 600 | { |
43968d2f GKH |
601 | int retval; |
602 | ||
86406245 | 603 | if (dev->class) { |
77d3d7c1 | 604 | static DEFINE_MUTEX(gdp_mutex); |
86406245 KS |
605 | struct kobject *kobj = NULL; |
606 | struct kobject *parent_kobj; | |
607 | struct kobject *k; | |
608 | ||
609 | /* | |
610 | * If we have no parent, we live in "virtual". | |
0f4dafc0 KS |
611 | * Class-devices with a non class-device as parent, live |
612 | * in a "glue" directory to prevent namespace collisions. | |
86406245 KS |
613 | */ |
614 | if (parent == NULL) | |
615 | parent_kobj = virtual_device_parent(dev); | |
616 | else if (parent->class) | |
617 | return &parent->kobj; | |
618 | else | |
619 | parent_kobj = &parent->kobj; | |
620 | ||
77d3d7c1 TH |
621 | mutex_lock(&gdp_mutex); |
622 | ||
86406245 | 623 | /* find our class-directory at the parent and reference it */ |
7c71448b GKH |
624 | spin_lock(&dev->class->p->class_dirs.list_lock); |
625 | list_for_each_entry(k, &dev->class->p->class_dirs.list, entry) | |
86406245 KS |
626 | if (k->parent == parent_kobj) { |
627 | kobj = kobject_get(k); | |
628 | break; | |
629 | } | |
7c71448b | 630 | spin_unlock(&dev->class->p->class_dirs.list_lock); |
77d3d7c1 TH |
631 | if (kobj) { |
632 | mutex_unlock(&gdp_mutex); | |
86406245 | 633 | return kobj; |
77d3d7c1 | 634 | } |
86406245 KS |
635 | |
636 | /* or create a new class-directory at the parent device */ | |
43968d2f | 637 | k = kobject_create(); |
77d3d7c1 TH |
638 | if (!k) { |
639 | mutex_unlock(&gdp_mutex); | |
43968d2f | 640 | return NULL; |
77d3d7c1 | 641 | } |
7c71448b | 642 | k->kset = &dev->class->p->class_dirs; |
b2d6db58 | 643 | retval = kobject_add(k, parent_kobj, "%s", dev->class->name); |
43968d2f | 644 | if (retval < 0) { |
77d3d7c1 | 645 | mutex_unlock(&gdp_mutex); |
43968d2f GKH |
646 | kobject_put(k); |
647 | return NULL; | |
648 | } | |
0f4dafc0 | 649 | /* do not emit an uevent for this simple "glue" directory */ |
77d3d7c1 | 650 | mutex_unlock(&gdp_mutex); |
43968d2f | 651 | return k; |
86406245 KS |
652 | } |
653 | ||
654 | if (parent) | |
c744aeae CH |
655 | return &parent->kobj; |
656 | return NULL; | |
657 | } | |
da231fd5 | 658 | |
63b6971a | 659 | static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir) |
da231fd5 | 660 | { |
0f4dafc0 | 661 | /* see if we live in a "glue" directory */ |
c1fe539a | 662 | if (!glue_dir || !dev->class || |
7c71448b | 663 | glue_dir->kset != &dev->class->p->class_dirs) |
da231fd5 KS |
664 | return; |
665 | ||
0f4dafc0 | 666 | kobject_put(glue_dir); |
da231fd5 | 667 | } |
63b6971a CH |
668 | |
669 | static void cleanup_device_parent(struct device *dev) | |
670 | { | |
671 | cleanup_glue_dir(dev, dev->kobj.parent); | |
672 | } | |
c744aeae | 673 | #endif |
86406245 | 674 | |
63b6971a | 675 | static void setup_parent(struct device *dev, struct device *parent) |
c744aeae CH |
676 | { |
677 | struct kobject *kobj; | |
678 | kobj = get_device_parent(dev, parent); | |
c744aeae CH |
679 | if (kobj) |
680 | dev->kobj.parent = kobj; | |
40fa5422 | 681 | } |
40fa5422 | 682 | |
2ee97caf CH |
683 | static int device_add_class_symlinks(struct device *dev) |
684 | { | |
685 | int error; | |
686 | ||
687 | if (!dev->class) | |
688 | return 0; | |
da231fd5 | 689 | |
1fbfee6c GKH |
690 | error = sysfs_create_link(&dev->kobj, |
691 | &dev->class->p->class_subsys.kobj, | |
2ee97caf CH |
692 | "subsystem"); |
693 | if (error) | |
694 | goto out; | |
da231fd5 KS |
695 | |
696 | #ifdef CONFIG_SYSFS_DEPRECATED | |
697 | /* stacked class devices need a symlink in the class directory */ | |
1fbfee6c | 698 | if (dev->kobj.parent != &dev->class->p->class_subsys.kobj && |
4e886c29 | 699 | device_is_not_partition(dev)) { |
1fbfee6c | 700 | error = sysfs_create_link(&dev->class->p->class_subsys.kobj, |
1e0b2cf9 | 701 | &dev->kobj, dev_name(dev)); |
2ee97caf CH |
702 | if (error) |
703 | goto out_subsys; | |
704 | } | |
da231fd5 | 705 | |
4e886c29 | 706 | if (dev->parent && device_is_not_partition(dev)) { |
da231fd5 KS |
707 | struct device *parent = dev->parent; |
708 | char *class_name; | |
4f01a757 | 709 | |
da231fd5 KS |
710 | /* |
711 | * stacked class devices have the 'device' link | |
712 | * pointing to the bus device instead of the parent | |
713 | */ | |
714 | while (parent->class && !parent->bus && parent->parent) | |
715 | parent = parent->parent; | |
716 | ||
717 | error = sysfs_create_link(&dev->kobj, | |
718 | &parent->kobj, | |
4f01a757 DT |
719 | "device"); |
720 | if (error) | |
721 | goto out_busid; | |
da231fd5 KS |
722 | |
723 | class_name = make_class_name(dev->class->name, | |
724 | &dev->kobj); | |
725 | if (class_name) | |
726 | error = sysfs_create_link(&dev->parent->kobj, | |
727 | &dev->kobj, class_name); | |
728 | kfree(class_name); | |
729 | if (error) | |
730 | goto out_device; | |
2ee97caf CH |
731 | } |
732 | return 0; | |
733 | ||
2ee97caf | 734 | out_device: |
4e886c29 | 735 | if (dev->parent && device_is_not_partition(dev)) |
2ee97caf | 736 | sysfs_remove_link(&dev->kobj, "device"); |
2ee97caf | 737 | out_busid: |
1fbfee6c | 738 | if (dev->kobj.parent != &dev->class->p->class_subsys.kobj && |
4e886c29 | 739 | device_is_not_partition(dev)) |
1fbfee6c | 740 | sysfs_remove_link(&dev->class->p->class_subsys.kobj, |
1e0b2cf9 | 741 | dev_name(dev)); |
da231fd5 KS |
742 | #else |
743 | /* link in the class directory pointing to the device */ | |
1fbfee6c | 744 | error = sysfs_create_link(&dev->class->p->class_subsys.kobj, |
1e0b2cf9 | 745 | &dev->kobj, dev_name(dev)); |
da231fd5 KS |
746 | if (error) |
747 | goto out_subsys; | |
748 | ||
4e886c29 | 749 | if (dev->parent && device_is_not_partition(dev)) { |
da231fd5 KS |
750 | error = sysfs_create_link(&dev->kobj, &dev->parent->kobj, |
751 | "device"); | |
752 | if (error) | |
753 | goto out_busid; | |
754 | } | |
755 | return 0; | |
756 | ||
757 | out_busid: | |
1e0b2cf9 | 758 | sysfs_remove_link(&dev->class->p->class_subsys.kobj, dev_name(dev)); |
da231fd5 KS |
759 | #endif |
760 | ||
2ee97caf CH |
761 | out_subsys: |
762 | sysfs_remove_link(&dev->kobj, "subsystem"); | |
763 | out: | |
764 | return error; | |
765 | } | |
766 | ||
767 | static void device_remove_class_symlinks(struct device *dev) | |
768 | { | |
769 | if (!dev->class) | |
770 | return; | |
da231fd5 | 771 | |
2ee97caf | 772 | #ifdef CONFIG_SYSFS_DEPRECATED |
4e886c29 | 773 | if (dev->parent && device_is_not_partition(dev)) { |
2ee97caf CH |
774 | char *class_name; |
775 | ||
776 | class_name = make_class_name(dev->class->name, &dev->kobj); | |
777 | if (class_name) { | |
778 | sysfs_remove_link(&dev->parent->kobj, class_name); | |
779 | kfree(class_name); | |
780 | } | |
2ee97caf CH |
781 | sysfs_remove_link(&dev->kobj, "device"); |
782 | } | |
da231fd5 | 783 | |
1fbfee6c | 784 | if (dev->kobj.parent != &dev->class->p->class_subsys.kobj && |
4e886c29 | 785 | device_is_not_partition(dev)) |
1fbfee6c | 786 | sysfs_remove_link(&dev->class->p->class_subsys.kobj, |
1e0b2cf9 | 787 | dev_name(dev)); |
da231fd5 | 788 | #else |
4e886c29 | 789 | if (dev->parent && device_is_not_partition(dev)) |
da231fd5 KS |
790 | sysfs_remove_link(&dev->kobj, "device"); |
791 | ||
1e0b2cf9 | 792 | sysfs_remove_link(&dev->class->p->class_subsys.kobj, dev_name(dev)); |
da231fd5 KS |
793 | #endif |
794 | ||
2ee97caf CH |
795 | sysfs_remove_link(&dev->kobj, "subsystem"); |
796 | } | |
797 | ||
413c239f SR |
798 | /** |
799 | * dev_set_name - set a device name | |
800 | * @dev: device | |
46232366 | 801 | * @fmt: format string for the device's name |
413c239f SR |
802 | */ |
803 | int dev_set_name(struct device *dev, const char *fmt, ...) | |
804 | { | |
805 | va_list vargs; | |
1fa5ae85 | 806 | int err; |
413c239f SR |
807 | |
808 | va_start(vargs, fmt); | |
1fa5ae85 | 809 | err = kobject_set_name_vargs(&dev->kobj, fmt, vargs); |
413c239f | 810 | va_end(vargs); |
1fa5ae85 | 811 | return err; |
413c239f SR |
812 | } |
813 | EXPORT_SYMBOL_GPL(dev_set_name); | |
814 | ||
e105b8bf DW |
815 | /** |
816 | * device_to_dev_kobj - select a /sys/dev/ directory for the device | |
817 | * @dev: device | |
818 | * | |
819 | * By default we select char/ for new entries. Setting class->dev_obj | |
820 | * to NULL prevents an entry from being created. class->dev_kobj must | |
821 | * be set (or cleared) before any devices are registered to the class | |
822 | * otherwise device_create_sys_dev_entry() and | |
823 | * device_remove_sys_dev_entry() will disagree about the the presence | |
824 | * of the link. | |
825 | */ | |
826 | static struct kobject *device_to_dev_kobj(struct device *dev) | |
827 | { | |
828 | struct kobject *kobj; | |
829 | ||
830 | if (dev->class) | |
831 | kobj = dev->class->dev_kobj; | |
832 | else | |
833 | kobj = sysfs_dev_char_kobj; | |
834 | ||
835 | return kobj; | |
836 | } | |
837 | ||
838 | static int device_create_sys_dev_entry(struct device *dev) | |
839 | { | |
840 | struct kobject *kobj = device_to_dev_kobj(dev); | |
841 | int error = 0; | |
842 | char devt_str[15]; | |
843 | ||
844 | if (kobj) { | |
845 | format_dev_t(devt_str, dev->devt); | |
846 | error = sysfs_create_link(kobj, &dev->kobj, devt_str); | |
847 | } | |
848 | ||
849 | return error; | |
850 | } | |
851 | ||
852 | static void device_remove_sys_dev_entry(struct device *dev) | |
853 | { | |
854 | struct kobject *kobj = device_to_dev_kobj(dev); | |
855 | char devt_str[15]; | |
856 | ||
857 | if (kobj) { | |
858 | format_dev_t(devt_str, dev->devt); | |
859 | sysfs_remove_link(kobj, devt_str); | |
860 | } | |
861 | } | |
862 | ||
b4028437 GKH |
863 | int device_private_init(struct device *dev) |
864 | { | |
865 | dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL); | |
866 | if (!dev->p) | |
867 | return -ENOMEM; | |
868 | dev->p->device = dev; | |
869 | klist_init(&dev->p->klist_children, klist_children_get, | |
870 | klist_children_put); | |
871 | return 0; | |
872 | } | |
873 | ||
1da177e4 | 874 | /** |
4a3ad20c GKH |
875 | * device_add - add device to device hierarchy. |
876 | * @dev: device. | |
1da177e4 | 877 | * |
4a3ad20c GKH |
878 | * This is part 2 of device_register(), though may be called |
879 | * separately _iff_ device_initialize() has been called separately. | |
1da177e4 | 880 | * |
5739411a | 881 | * This adds @dev to the kobject hierarchy via kobject_add(), adds it |
4a3ad20c GKH |
882 | * to the global and sibling lists for the device, then |
883 | * adds it to the other relevant subsystems of the driver model. | |
5739411a CH |
884 | * |
885 | * NOTE: _Never_ directly free @dev after calling this function, even | |
886 | * if it returned an error! Always use put_device() to give up your | |
887 | * reference instead. | |
1da177e4 LT |
888 | */ |
889 | int device_add(struct device *dev) | |
890 | { | |
891 | struct device *parent = NULL; | |
c47ed219 | 892 | struct class_interface *class_intf; |
c906a48a | 893 | int error = -EINVAL; |
775b64d2 | 894 | |
1da177e4 | 895 | dev = get_device(dev); |
c906a48a GKH |
896 | if (!dev) |
897 | goto done; | |
898 | ||
fb069a5d | 899 | if (!dev->p) { |
b4028437 GKH |
900 | error = device_private_init(dev); |
901 | if (error) | |
902 | goto done; | |
fb069a5d | 903 | } |
fb069a5d | 904 | |
1fa5ae85 KS |
905 | /* |
906 | * for statically allocated devices, which should all be converted | |
907 | * some day, we need to initialize the name. We prevent reading back | |
908 | * the name, and force the use of dev_name() | |
909 | */ | |
910 | if (dev->init_name) { | |
acc0e90f | 911 | dev_set_name(dev, "%s", dev->init_name); |
1fa5ae85 KS |
912 | dev->init_name = NULL; |
913 | } | |
c906a48a | 914 | |
e6309e75 TG |
915 | if (!dev_name(dev)) { |
916 | error = -EINVAL; | |
5c8563d7 | 917 | goto name_error; |
e6309e75 | 918 | } |
1da177e4 | 919 | |
1e0b2cf9 | 920 | pr_debug("device: '%s': %s\n", dev_name(dev), __func__); |
c205ef48 | 921 | |
1da177e4 | 922 | parent = get_device(dev->parent); |
63b6971a | 923 | setup_parent(dev, parent); |
1da177e4 | 924 | |
0d358f22 YL |
925 | /* use parent numa_node */ |
926 | if (parent) | |
927 | set_dev_node(dev, dev_to_node(parent)); | |
928 | ||
1da177e4 | 929 | /* first, register with generic layer. */ |
8a577ffc KS |
930 | /* we require the name to be set before, and pass NULL */ |
931 | error = kobject_add(&dev->kobj, dev->kobj.parent, NULL); | |
40fa5422 | 932 | if (error) |
1da177e4 | 933 | goto Error; |
a7fd6706 | 934 | |
37022644 BW |
935 | /* notify platform of device entry */ |
936 | if (platform_notify) | |
937 | platform_notify(dev); | |
938 | ||
ad6a1e1c | 939 | error = device_create_file(dev, &uevent_attr); |
a306eea4 CH |
940 | if (error) |
941 | goto attrError; | |
a7fd6706 | 942 | |
23681e47 | 943 | if (MAJOR(dev->devt)) { |
ad6a1e1c TH |
944 | error = device_create_file(dev, &devt_attr); |
945 | if (error) | |
a306eea4 | 946 | goto ueventattrError; |
e105b8bf DW |
947 | |
948 | error = device_create_sys_dev_entry(dev); | |
949 | if (error) | |
950 | goto devtattrError; | |
2b2af54a KS |
951 | |
952 | devtmpfs_create_node(dev); | |
23681e47 GKH |
953 | } |
954 | ||
2ee97caf CH |
955 | error = device_add_class_symlinks(dev); |
956 | if (error) | |
957 | goto SymlinkError; | |
dc0afa83 CH |
958 | error = device_add_attrs(dev); |
959 | if (error) | |
2620efef | 960 | goto AttrsError; |
dc0afa83 CH |
961 | error = bus_add_device(dev); |
962 | if (error) | |
1da177e4 | 963 | goto BusError; |
3b98aeaf | 964 | error = dpm_sysfs_add(dev); |
57eee3d2 | 965 | if (error) |
3b98aeaf AS |
966 | goto DPMError; |
967 | device_pm_add(dev); | |
ec0676ee AS |
968 | |
969 | /* Notify clients of device addition. This call must come | |
970 | * after dpm_sysf_add() and before kobject_uevent(). | |
971 | */ | |
972 | if (dev->bus) | |
973 | blocking_notifier_call_chain(&dev->bus->p->bus_notifier, | |
974 | BUS_NOTIFY_ADD_DEVICE, dev); | |
975 | ||
83b5fb4c | 976 | kobject_uevent(&dev->kobj, KOBJ_ADD); |
2023c610 | 977 | bus_probe_device(dev); |
1da177e4 | 978 | if (parent) |
f791b8c8 GKH |
979 | klist_add_tail(&dev->p->knode_parent, |
980 | &parent->p->klist_children); | |
1da177e4 | 981 | |
5d9fd169 | 982 | if (dev->class) { |
f75b1c60 | 983 | mutex_lock(&dev->class->p->class_mutex); |
c47ed219 | 984 | /* tie the class to the device */ |
5a3ceb86 TH |
985 | klist_add_tail(&dev->knode_class, |
986 | &dev->class->p->class_devices); | |
c47ed219 GKH |
987 | |
988 | /* notify any interfaces that the device is here */ | |
184f1f77 GKH |
989 | list_for_each_entry(class_intf, |
990 | &dev->class->p->class_interfaces, node) | |
c47ed219 GKH |
991 | if (class_intf->add_dev) |
992 | class_intf->add_dev(dev, class_intf); | |
f75b1c60 | 993 | mutex_unlock(&dev->class->p->class_mutex); |
5d9fd169 | 994 | } |
c906a48a | 995 | done: |
1da177e4 LT |
996 | put_device(dev); |
997 | return error; | |
3b98aeaf | 998 | DPMError: |
57eee3d2 RW |
999 | bus_remove_device(dev); |
1000 | BusError: | |
82f0cf9b | 1001 | device_remove_attrs(dev); |
2620efef | 1002 | AttrsError: |
2ee97caf CH |
1003 | device_remove_class_symlinks(dev); |
1004 | SymlinkError: | |
ad72956d KS |
1005 | if (MAJOR(dev->devt)) |
1006 | devtmpfs_delete_node(dev); | |
e105b8bf DW |
1007 | if (MAJOR(dev->devt)) |
1008 | device_remove_sys_dev_entry(dev); | |
1009 | devtattrError: | |
ad6a1e1c TH |
1010 | if (MAJOR(dev->devt)) |
1011 | device_remove_file(dev, &devt_attr); | |
a306eea4 | 1012 | ueventattrError: |
ad6a1e1c | 1013 | device_remove_file(dev, &uevent_attr); |
23681e47 | 1014 | attrError: |
312c004d | 1015 | kobject_uevent(&dev->kobj, KOBJ_REMOVE); |
1da177e4 LT |
1016 | kobject_del(&dev->kobj); |
1017 | Error: | |
63b6971a | 1018 | cleanup_device_parent(dev); |
1da177e4 LT |
1019 | if (parent) |
1020 | put_device(parent); | |
5c8563d7 KS |
1021 | name_error: |
1022 | kfree(dev->p); | |
1023 | dev->p = NULL; | |
c906a48a | 1024 | goto done; |
1da177e4 LT |
1025 | } |
1026 | ||
1da177e4 | 1027 | /** |
4a3ad20c GKH |
1028 | * device_register - register a device with the system. |
1029 | * @dev: pointer to the device structure | |
1da177e4 | 1030 | * |
4a3ad20c GKH |
1031 | * This happens in two clean steps - initialize the device |
1032 | * and add it to the system. The two steps can be called | |
1033 | * separately, but this is the easiest and most common. | |
1034 | * I.e. you should only call the two helpers separately if | |
1035 | * have a clearly defined need to use and refcount the device | |
1036 | * before it is added to the hierarchy. | |
5739411a CH |
1037 | * |
1038 | * NOTE: _Never_ directly free @dev after calling this function, even | |
1039 | * if it returned an error! Always use put_device() to give up the | |
1040 | * reference initialized in this function instead. | |
1da177e4 | 1041 | */ |
1da177e4 LT |
1042 | int device_register(struct device *dev) |
1043 | { | |
1044 | device_initialize(dev); | |
1045 | return device_add(dev); | |
1046 | } | |
1047 | ||
1da177e4 | 1048 | /** |
4a3ad20c GKH |
1049 | * get_device - increment reference count for device. |
1050 | * @dev: device. | |
1da177e4 | 1051 | * |
4a3ad20c GKH |
1052 | * This simply forwards the call to kobject_get(), though |
1053 | * we do take care to provide for the case that we get a NULL | |
1054 | * pointer passed in. | |
1da177e4 | 1055 | */ |
4a3ad20c | 1056 | struct device *get_device(struct device *dev) |
1da177e4 LT |
1057 | { |
1058 | return dev ? to_dev(kobject_get(&dev->kobj)) : NULL; | |
1059 | } | |
1060 | ||
1da177e4 | 1061 | /** |
4a3ad20c GKH |
1062 | * put_device - decrement reference count. |
1063 | * @dev: device in question. | |
1da177e4 | 1064 | */ |
4a3ad20c | 1065 | void put_device(struct device *dev) |
1da177e4 | 1066 | { |
edfaa7c3 | 1067 | /* might_sleep(); */ |
1da177e4 LT |
1068 | if (dev) |
1069 | kobject_put(&dev->kobj); | |
1070 | } | |
1071 | ||
1da177e4 | 1072 | /** |
4a3ad20c GKH |
1073 | * device_del - delete device from system. |
1074 | * @dev: device. | |
1da177e4 | 1075 | * |
4a3ad20c GKH |
1076 | * This is the first part of the device unregistration |
1077 | * sequence. This removes the device from the lists we control | |
1078 | * from here, has it removed from the other driver model | |
1079 | * subsystems it was added to in device_add(), and removes it | |
1080 | * from the kobject hierarchy. | |
1da177e4 | 1081 | * |
4a3ad20c GKH |
1082 | * NOTE: this should be called manually _iff_ device_add() was |
1083 | * also called manually. | |
1da177e4 | 1084 | */ |
4a3ad20c | 1085 | void device_del(struct device *dev) |
1da177e4 | 1086 | { |
4a3ad20c | 1087 | struct device *parent = dev->parent; |
c47ed219 | 1088 | struct class_interface *class_intf; |
1da177e4 | 1089 | |
ec0676ee AS |
1090 | /* Notify clients of device removal. This call must come |
1091 | * before dpm_sysfs_remove(). | |
1092 | */ | |
1093 | if (dev->bus) | |
1094 | blocking_notifier_call_chain(&dev->bus->p->bus_notifier, | |
1095 | BUS_NOTIFY_DEL_DEVICE, dev); | |
775b64d2 | 1096 | device_pm_remove(dev); |
3b98aeaf | 1097 | dpm_sysfs_remove(dev); |
1da177e4 | 1098 | if (parent) |
f791b8c8 | 1099 | klist_del(&dev->p->knode_parent); |
e105b8bf | 1100 | if (MAJOR(dev->devt)) { |
2b2af54a | 1101 | devtmpfs_delete_node(dev); |
e105b8bf | 1102 | device_remove_sys_dev_entry(dev); |
ad6a1e1c | 1103 | device_remove_file(dev, &devt_attr); |
e105b8bf | 1104 | } |
b9d9c82b | 1105 | if (dev->class) { |
da231fd5 | 1106 | device_remove_class_symlinks(dev); |
99ef3ef8 | 1107 | |
f75b1c60 | 1108 | mutex_lock(&dev->class->p->class_mutex); |
c47ed219 | 1109 | /* notify any interfaces that the device is now gone */ |
184f1f77 GKH |
1110 | list_for_each_entry(class_intf, |
1111 | &dev->class->p->class_interfaces, node) | |
c47ed219 GKH |
1112 | if (class_intf->remove_dev) |
1113 | class_intf->remove_dev(dev, class_intf); | |
1114 | /* remove the device from the class list */ | |
5a3ceb86 | 1115 | klist_del(&dev->knode_class); |
f75b1c60 | 1116 | mutex_unlock(&dev->class->p->class_mutex); |
b9d9c82b | 1117 | } |
ad6a1e1c | 1118 | device_remove_file(dev, &uevent_attr); |
2620efef | 1119 | device_remove_attrs(dev); |
28953533 | 1120 | bus_remove_device(dev); |
1da177e4 | 1121 | |
2f8d16a9 TH |
1122 | /* |
1123 | * Some platform devices are driven without driver attached | |
1124 | * and managed resources may have been acquired. Make sure | |
1125 | * all resources are released. | |
1126 | */ | |
1127 | devres_release_all(dev); | |
1128 | ||
1da177e4 LT |
1129 | /* Notify the platform of the removal, in case they |
1130 | * need to do anything... | |
1131 | */ | |
1132 | if (platform_notify_remove) | |
1133 | platform_notify_remove(dev); | |
312c004d | 1134 | kobject_uevent(&dev->kobj, KOBJ_REMOVE); |
da231fd5 | 1135 | cleanup_device_parent(dev); |
1da177e4 | 1136 | kobject_del(&dev->kobj); |
da231fd5 | 1137 | put_device(parent); |
1da177e4 LT |
1138 | } |
1139 | ||
1140 | /** | |
4a3ad20c GKH |
1141 | * device_unregister - unregister device from system. |
1142 | * @dev: device going away. | |
1da177e4 | 1143 | * |
4a3ad20c GKH |
1144 | * We do this in two parts, like we do device_register(). First, |
1145 | * we remove it from all the subsystems with device_del(), then | |
1146 | * we decrement the reference count via put_device(). If that | |
1147 | * is the final reference count, the device will be cleaned up | |
1148 | * via device_release() above. Otherwise, the structure will | |
1149 | * stick around until the final reference to the device is dropped. | |
1da177e4 | 1150 | */ |
4a3ad20c | 1151 | void device_unregister(struct device *dev) |
1da177e4 | 1152 | { |
1e0b2cf9 | 1153 | pr_debug("device: '%s': %s\n", dev_name(dev), __func__); |
1da177e4 LT |
1154 | device_del(dev); |
1155 | put_device(dev); | |
1156 | } | |
1157 | ||
4a3ad20c | 1158 | static struct device *next_device(struct klist_iter *i) |
36239577 | 1159 | { |
4a3ad20c | 1160 | struct klist_node *n = klist_next(i); |
f791b8c8 GKH |
1161 | struct device *dev = NULL; |
1162 | struct device_private *p; | |
1163 | ||
1164 | if (n) { | |
1165 | p = to_device_private_parent(n); | |
1166 | dev = p->device; | |
1167 | } | |
1168 | return dev; | |
36239577 PM |
1169 | } |
1170 | ||
6fcf53ac | 1171 | /** |
e454cea2 | 1172 | * device_get_devnode - path of device node file |
6fcf53ac | 1173 | * @dev: device |
e454cea2 | 1174 | * @mode: returned file access mode |
6fcf53ac KS |
1175 | * @tmp: possibly allocated string |
1176 | * | |
1177 | * Return the relative path of a possible device node. | |
1178 | * Non-default names may need to allocate a memory to compose | |
1179 | * a name. This memory is returned in tmp and needs to be | |
1180 | * freed by the caller. | |
1181 | */ | |
e454cea2 KS |
1182 | const char *device_get_devnode(struct device *dev, |
1183 | mode_t *mode, const char **tmp) | |
6fcf53ac KS |
1184 | { |
1185 | char *s; | |
1186 | ||
1187 | *tmp = NULL; | |
1188 | ||
1189 | /* the device type may provide a specific name */ | |
e454cea2 KS |
1190 | if (dev->type && dev->type->devnode) |
1191 | *tmp = dev->type->devnode(dev, mode); | |
6fcf53ac KS |
1192 | if (*tmp) |
1193 | return *tmp; | |
1194 | ||
1195 | /* the class may provide a specific name */ | |
e454cea2 KS |
1196 | if (dev->class && dev->class->devnode) |
1197 | *tmp = dev->class->devnode(dev, mode); | |
6fcf53ac KS |
1198 | if (*tmp) |
1199 | return *tmp; | |
1200 | ||
1201 | /* return name without allocation, tmp == NULL */ | |
1202 | if (strchr(dev_name(dev), '!') == NULL) | |
1203 | return dev_name(dev); | |
1204 | ||
1205 | /* replace '!' in the name with '/' */ | |
1206 | *tmp = kstrdup(dev_name(dev), GFP_KERNEL); | |
1207 | if (!*tmp) | |
1208 | return NULL; | |
1209 | while ((s = strchr(*tmp, '!'))) | |
1210 | s[0] = '/'; | |
1211 | return *tmp; | |
1212 | } | |
1213 | ||
1da177e4 | 1214 | /** |
4a3ad20c GKH |
1215 | * device_for_each_child - device child iterator. |
1216 | * @parent: parent struct device. | |
1217 | * @data: data for the callback. | |
1218 | * @fn: function to be called for each device. | |
1da177e4 | 1219 | * |
4a3ad20c GKH |
1220 | * Iterate over @parent's child devices, and call @fn for each, |
1221 | * passing it @data. | |
1da177e4 | 1222 | * |
4a3ad20c GKH |
1223 | * We check the return of @fn each time. If it returns anything |
1224 | * other than 0, we break out and return that value. | |
1da177e4 | 1225 | */ |
4a3ad20c GKH |
1226 | int device_for_each_child(struct device *parent, void *data, |
1227 | int (*fn)(struct device *dev, void *data)) | |
1da177e4 | 1228 | { |
36239577 | 1229 | struct klist_iter i; |
4a3ad20c | 1230 | struct device *child; |
1da177e4 LT |
1231 | int error = 0; |
1232 | ||
014c90db GKH |
1233 | if (!parent->p) |
1234 | return 0; | |
1235 | ||
f791b8c8 | 1236 | klist_iter_init(&parent->p->klist_children, &i); |
36239577 PM |
1237 | while ((child = next_device(&i)) && !error) |
1238 | error = fn(child, data); | |
1239 | klist_iter_exit(&i); | |
1da177e4 LT |
1240 | return error; |
1241 | } | |
1242 | ||
5ab69981 CH |
1243 | /** |
1244 | * device_find_child - device iterator for locating a particular device. | |
1245 | * @parent: parent struct device | |
1246 | * @data: Data to pass to match function | |
1247 | * @match: Callback function to check device | |
1248 | * | |
1249 | * This is similar to the device_for_each_child() function above, but it | |
1250 | * returns a reference to a device that is 'found' for later use, as | |
1251 | * determined by the @match callback. | |
1252 | * | |
1253 | * The callback should return 0 if the device doesn't match and non-zero | |
1254 | * if it does. If the callback returns non-zero and a reference to the | |
1255 | * current device can be obtained, this function will return to the caller | |
1256 | * and not iterate over any more devices. | |
1257 | */ | |
4a3ad20c GKH |
1258 | struct device *device_find_child(struct device *parent, void *data, |
1259 | int (*match)(struct device *dev, void *data)) | |
5ab69981 CH |
1260 | { |
1261 | struct klist_iter i; | |
1262 | struct device *child; | |
1263 | ||
1264 | if (!parent) | |
1265 | return NULL; | |
1266 | ||
f791b8c8 | 1267 | klist_iter_init(&parent->p->klist_children, &i); |
5ab69981 CH |
1268 | while ((child = next_device(&i))) |
1269 | if (match(child, data) && get_device(child)) | |
1270 | break; | |
1271 | klist_iter_exit(&i); | |
1272 | return child; | |
1273 | } | |
1274 | ||
1da177e4 LT |
1275 | int __init devices_init(void) |
1276 | { | |
881c6cfd GKH |
1277 | devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL); |
1278 | if (!devices_kset) | |
1279 | return -ENOMEM; | |
e105b8bf DW |
1280 | dev_kobj = kobject_create_and_add("dev", NULL); |
1281 | if (!dev_kobj) | |
1282 | goto dev_kobj_err; | |
1283 | sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj); | |
1284 | if (!sysfs_dev_block_kobj) | |
1285 | goto block_kobj_err; | |
1286 | sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj); | |
1287 | if (!sysfs_dev_char_kobj) | |
1288 | goto char_kobj_err; | |
1289 | ||
881c6cfd | 1290 | return 0; |
e105b8bf DW |
1291 | |
1292 | char_kobj_err: | |
1293 | kobject_put(sysfs_dev_block_kobj); | |
1294 | block_kobj_err: | |
1295 | kobject_put(dev_kobj); | |
1296 | dev_kobj_err: | |
1297 | kset_unregister(devices_kset); | |
1298 | return -ENOMEM; | |
1da177e4 LT |
1299 | } |
1300 | ||
1301 | EXPORT_SYMBOL_GPL(device_for_each_child); | |
5ab69981 | 1302 | EXPORT_SYMBOL_GPL(device_find_child); |
1da177e4 LT |
1303 | |
1304 | EXPORT_SYMBOL_GPL(device_initialize); | |
1305 | EXPORT_SYMBOL_GPL(device_add); | |
1306 | EXPORT_SYMBOL_GPL(device_register); | |
1307 | ||
1308 | EXPORT_SYMBOL_GPL(device_del); | |
1309 | EXPORT_SYMBOL_GPL(device_unregister); | |
1310 | EXPORT_SYMBOL_GPL(get_device); | |
1311 | EXPORT_SYMBOL_GPL(put_device); | |
1da177e4 LT |
1312 | |
1313 | EXPORT_SYMBOL_GPL(device_create_file); | |
1314 | EXPORT_SYMBOL_GPL(device_remove_file); | |
23681e47 | 1315 | |
0aa0dc41 MM |
1316 | struct root_device |
1317 | { | |
1318 | struct device dev; | |
1319 | struct module *owner; | |
1320 | }; | |
1321 | ||
1322 | #define to_root_device(dev) container_of(dev, struct root_device, dev) | |
1323 | ||
1324 | static void root_device_release(struct device *dev) | |
1325 | { | |
1326 | kfree(to_root_device(dev)); | |
1327 | } | |
1328 | ||
1329 | /** | |
1330 | * __root_device_register - allocate and register a root device | |
1331 | * @name: root device name | |
1332 | * @owner: owner module of the root device, usually THIS_MODULE | |
1333 | * | |
1334 | * This function allocates a root device and registers it | |
1335 | * using device_register(). In order to free the returned | |
1336 | * device, use root_device_unregister(). | |
1337 | * | |
1338 | * Root devices are dummy devices which allow other devices | |
1339 | * to be grouped under /sys/devices. Use this function to | |
1340 | * allocate a root device and then use it as the parent of | |
1341 | * any device which should appear under /sys/devices/{name} | |
1342 | * | |
1343 | * The /sys/devices/{name} directory will also contain a | |
1344 | * 'module' symlink which points to the @owner directory | |
1345 | * in sysfs. | |
1346 | * | |
f0eae0ed JN |
1347 | * Returns &struct device pointer on success, or ERR_PTR() on error. |
1348 | * | |
0aa0dc41 MM |
1349 | * Note: You probably want to use root_device_register(). |
1350 | */ | |
1351 | struct device *__root_device_register(const char *name, struct module *owner) | |
1352 | { | |
1353 | struct root_device *root; | |
1354 | int err = -ENOMEM; | |
1355 | ||
1356 | root = kzalloc(sizeof(struct root_device), GFP_KERNEL); | |
1357 | if (!root) | |
1358 | return ERR_PTR(err); | |
1359 | ||
acc0e90f | 1360 | err = dev_set_name(&root->dev, "%s", name); |
0aa0dc41 MM |
1361 | if (err) { |
1362 | kfree(root); | |
1363 | return ERR_PTR(err); | |
1364 | } | |
1365 | ||
1366 | root->dev.release = root_device_release; | |
1367 | ||
1368 | err = device_register(&root->dev); | |
1369 | if (err) { | |
1370 | put_device(&root->dev); | |
1371 | return ERR_PTR(err); | |
1372 | } | |
1373 | ||
1374 | #ifdef CONFIG_MODULE /* gotta find a "cleaner" way to do this */ | |
1375 | if (owner) { | |
1376 | struct module_kobject *mk = &owner->mkobj; | |
1377 | ||
1378 | err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module"); | |
1379 | if (err) { | |
1380 | device_unregister(&root->dev); | |
1381 | return ERR_PTR(err); | |
1382 | } | |
1383 | root->owner = owner; | |
1384 | } | |
1385 | #endif | |
1386 | ||
1387 | return &root->dev; | |
1388 | } | |
1389 | EXPORT_SYMBOL_GPL(__root_device_register); | |
1390 | ||
1391 | /** | |
1392 | * root_device_unregister - unregister and free a root device | |
7cbcf225 | 1393 | * @dev: device going away |
0aa0dc41 MM |
1394 | * |
1395 | * This function unregisters and cleans up a device that was created by | |
1396 | * root_device_register(). | |
1397 | */ | |
1398 | void root_device_unregister(struct device *dev) | |
1399 | { | |
1400 | struct root_device *root = to_root_device(dev); | |
1401 | ||
1402 | if (root->owner) | |
1403 | sysfs_remove_link(&root->dev.kobj, "module"); | |
1404 | ||
1405 | device_unregister(dev); | |
1406 | } | |
1407 | EXPORT_SYMBOL_GPL(root_device_unregister); | |
1408 | ||
23681e47 GKH |
1409 | |
1410 | static void device_create_release(struct device *dev) | |
1411 | { | |
1e0b2cf9 | 1412 | pr_debug("device: '%s': %s\n", dev_name(dev), __func__); |
23681e47 GKH |
1413 | kfree(dev); |
1414 | } | |
1415 | ||
1416 | /** | |
8882b394 | 1417 | * device_create_vargs - creates a device and registers it with sysfs |
42734daf HK |
1418 | * @class: pointer to the struct class that this device should be registered to |
1419 | * @parent: pointer to the parent struct device of this new device, if any | |
1420 | * @devt: the dev_t for the char device to be added | |
8882b394 | 1421 | * @drvdata: the data to be added to the device for callbacks |
42734daf | 1422 | * @fmt: string for the device's name |
8882b394 | 1423 | * @args: va_list for the device's name |
42734daf HK |
1424 | * |
1425 | * This function can be used by char device classes. A struct device | |
1426 | * will be created in sysfs, registered to the specified class. | |
23681e47 | 1427 | * |
23681e47 GKH |
1428 | * A "dev" file will be created, showing the dev_t for the device, if |
1429 | * the dev_t is not 0,0. | |
42734daf HK |
1430 | * If a pointer to a parent struct device is passed in, the newly created |
1431 | * struct device will be a child of that device in sysfs. | |
1432 | * The pointer to the struct device will be returned from the call. | |
1433 | * Any further sysfs files that might be required can be created using this | |
23681e47 GKH |
1434 | * pointer. |
1435 | * | |
f0eae0ed JN |
1436 | * Returns &struct device pointer on success, or ERR_PTR() on error. |
1437 | * | |
23681e47 GKH |
1438 | * Note: the struct class passed to this function must have previously |
1439 | * been created with a call to class_create(). | |
1440 | */ | |
8882b394 GKH |
1441 | struct device *device_create_vargs(struct class *class, struct device *parent, |
1442 | dev_t devt, void *drvdata, const char *fmt, | |
1443 | va_list args) | |
23681e47 | 1444 | { |
23681e47 GKH |
1445 | struct device *dev = NULL; |
1446 | int retval = -ENODEV; | |
1447 | ||
1448 | if (class == NULL || IS_ERR(class)) | |
1449 | goto error; | |
23681e47 GKH |
1450 | |
1451 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); | |
1452 | if (!dev) { | |
1453 | retval = -ENOMEM; | |
1454 | goto error; | |
1455 | } | |
1456 | ||
1457 | dev->devt = devt; | |
1458 | dev->class = class; | |
1459 | dev->parent = parent; | |
1460 | dev->release = device_create_release; | |
8882b394 | 1461 | dev_set_drvdata(dev, drvdata); |
23681e47 | 1462 | |
1fa5ae85 KS |
1463 | retval = kobject_set_name_vargs(&dev->kobj, fmt, args); |
1464 | if (retval) | |
1465 | goto error; | |
1466 | ||
23681e47 GKH |
1467 | retval = device_register(dev); |
1468 | if (retval) | |
1469 | goto error; | |
1470 | ||
23681e47 GKH |
1471 | return dev; |
1472 | ||
1473 | error: | |
286661b3 | 1474 | put_device(dev); |
23681e47 GKH |
1475 | return ERR_PTR(retval); |
1476 | } | |
8882b394 GKH |
1477 | EXPORT_SYMBOL_GPL(device_create_vargs); |
1478 | ||
1479 | /** | |
4e106739 | 1480 | * device_create - creates a device and registers it with sysfs |
8882b394 GKH |
1481 | * @class: pointer to the struct class that this device should be registered to |
1482 | * @parent: pointer to the parent struct device of this new device, if any | |
1483 | * @devt: the dev_t for the char device to be added | |
1484 | * @drvdata: the data to be added to the device for callbacks | |
1485 | * @fmt: string for the device's name | |
1486 | * | |
1487 | * This function can be used by char device classes. A struct device | |
1488 | * will be created in sysfs, registered to the specified class. | |
1489 | * | |
1490 | * A "dev" file will be created, showing the dev_t for the device, if | |
1491 | * the dev_t is not 0,0. | |
1492 | * If a pointer to a parent struct device is passed in, the newly created | |
1493 | * struct device will be a child of that device in sysfs. | |
1494 | * The pointer to the struct device will be returned from the call. | |
1495 | * Any further sysfs files that might be required can be created using this | |
1496 | * pointer. | |
1497 | * | |
f0eae0ed JN |
1498 | * Returns &struct device pointer on success, or ERR_PTR() on error. |
1499 | * | |
8882b394 GKH |
1500 | * Note: the struct class passed to this function must have previously |
1501 | * been created with a call to class_create(). | |
1502 | */ | |
4e106739 GKH |
1503 | struct device *device_create(struct class *class, struct device *parent, |
1504 | dev_t devt, void *drvdata, const char *fmt, ...) | |
8882b394 GKH |
1505 | { |
1506 | va_list vargs; | |
1507 | struct device *dev; | |
1508 | ||
1509 | va_start(vargs, fmt); | |
1510 | dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs); | |
1511 | va_end(vargs); | |
1512 | return dev; | |
1513 | } | |
4e106739 | 1514 | EXPORT_SYMBOL_GPL(device_create); |
8882b394 | 1515 | |
cd35449b | 1516 | static int __match_devt(struct device *dev, void *data) |
23681e47 | 1517 | { |
cd35449b | 1518 | dev_t *devt = data; |
23681e47 | 1519 | |
cd35449b | 1520 | return dev->devt == *devt; |
775b64d2 RW |
1521 | } |
1522 | ||
1523 | /** | |
1524 | * device_destroy - removes a device that was created with device_create() | |
1525 | * @class: pointer to the struct class that this device was registered with | |
1526 | * @devt: the dev_t of the device that was previously registered | |
1527 | * | |
1528 | * This call unregisters and cleans up a device that was created with a | |
1529 | * call to device_create(). | |
1530 | */ | |
1531 | void device_destroy(struct class *class, dev_t devt) | |
1532 | { | |
1533 | struct device *dev; | |
23681e47 | 1534 | |
695794ae | 1535 | dev = class_find_device(class, NULL, &devt, __match_devt); |
cd35449b DY |
1536 | if (dev) { |
1537 | put_device(dev); | |
23681e47 | 1538 | device_unregister(dev); |
cd35449b | 1539 | } |
23681e47 GKH |
1540 | } |
1541 | EXPORT_SYMBOL_GPL(device_destroy); | |
a2de48ca GKH |
1542 | |
1543 | /** | |
1544 | * device_rename - renames a device | |
1545 | * @dev: the pointer to the struct device to be renamed | |
1546 | * @new_name: the new name of the device | |
030c1d2b EB |
1547 | * |
1548 | * It is the responsibility of the caller to provide mutual | |
1549 | * exclusion between two different calls of device_rename | |
1550 | * on the same device to ensure that new_name is valid and | |
1551 | * won't conflict with other devices. | |
a2de48ca GKH |
1552 | */ |
1553 | int device_rename(struct device *dev, char *new_name) | |
1554 | { | |
1555 | char *old_class_name = NULL; | |
1556 | char *new_class_name = NULL; | |
2ee97caf | 1557 | char *old_device_name = NULL; |
a2de48ca GKH |
1558 | int error; |
1559 | ||
1560 | dev = get_device(dev); | |
1561 | if (!dev) | |
1562 | return -EINVAL; | |
1563 | ||
1e0b2cf9 | 1564 | pr_debug("device: '%s': %s: renaming to '%s'\n", dev_name(dev), |
2b3a302a | 1565 | __func__, new_name); |
a2de48ca | 1566 | |
99ef3ef8 | 1567 | #ifdef CONFIG_SYSFS_DEPRECATED |
a2de48ca GKH |
1568 | if ((dev->class) && (dev->parent)) |
1569 | old_class_name = make_class_name(dev->class->name, &dev->kobj); | |
99ef3ef8 | 1570 | #endif |
a2de48ca | 1571 | |
1fa5ae85 | 1572 | old_device_name = kstrdup(dev_name(dev), GFP_KERNEL); |
2ee97caf CH |
1573 | if (!old_device_name) { |
1574 | error = -ENOMEM; | |
1575 | goto out; | |
a2de48ca | 1576 | } |
a2de48ca GKH |
1577 | |
1578 | error = kobject_rename(&dev->kobj, new_name); | |
1fa5ae85 | 1579 | if (error) |
2ee97caf | 1580 | goto out; |
a2de48ca | 1581 | |
99ef3ef8 | 1582 | #ifdef CONFIG_SYSFS_DEPRECATED |
a2de48ca GKH |
1583 | if (old_class_name) { |
1584 | new_class_name = make_class_name(dev->class->name, &dev->kobj); | |
1585 | if (new_class_name) { | |
2354dcc7 EB |
1586 | error = sysfs_rename_link(&dev->parent->kobj, |
1587 | &dev->kobj, | |
1588 | old_class_name, | |
1589 | new_class_name); | |
a2de48ca GKH |
1590 | } |
1591 | } | |
60b8cabd | 1592 | #else |
a2de48ca | 1593 | if (dev->class) { |
2354dcc7 EB |
1594 | error = sysfs_rename_link(&dev->class->p->class_subsys.kobj, |
1595 | &dev->kobj, old_device_name, new_name); | |
a2de48ca | 1596 | } |
60b8cabd KS |
1597 | #endif |
1598 | ||
2ee97caf | 1599 | out: |
a2de48ca GKH |
1600 | put_device(dev); |
1601 | ||
a2de48ca | 1602 | kfree(new_class_name); |
952ab431 | 1603 | kfree(old_class_name); |
2ee97caf | 1604 | kfree(old_device_name); |
a2de48ca GKH |
1605 | |
1606 | return error; | |
1607 | } | |
a2807dbc | 1608 | EXPORT_SYMBOL_GPL(device_rename); |
8a82472f CH |
1609 | |
1610 | static int device_move_class_links(struct device *dev, | |
1611 | struct device *old_parent, | |
1612 | struct device *new_parent) | |
1613 | { | |
f7f3461d | 1614 | int error = 0; |
8a82472f | 1615 | #ifdef CONFIG_SYSFS_DEPRECATED |
8a82472f CH |
1616 | char *class_name; |
1617 | ||
1618 | class_name = make_class_name(dev->class->name, &dev->kobj); | |
1619 | if (!class_name) { | |
cb360bbf | 1620 | error = -ENOMEM; |
8a82472f CH |
1621 | goto out; |
1622 | } | |
1623 | if (old_parent) { | |
1624 | sysfs_remove_link(&dev->kobj, "device"); | |
1625 | sysfs_remove_link(&old_parent->kobj, class_name); | |
1626 | } | |
c744aeae CH |
1627 | if (new_parent) { |
1628 | error = sysfs_create_link(&dev->kobj, &new_parent->kobj, | |
1629 | "device"); | |
1630 | if (error) | |
1631 | goto out; | |
1632 | error = sysfs_create_link(&new_parent->kobj, &dev->kobj, | |
1633 | class_name); | |
1634 | if (error) | |
1635 | sysfs_remove_link(&dev->kobj, "device"); | |
4a3ad20c | 1636 | } else |
c744aeae | 1637 | error = 0; |
8a82472f CH |
1638 | out: |
1639 | kfree(class_name); | |
1640 | return error; | |
1641 | #else | |
f7f3461d GKH |
1642 | if (old_parent) |
1643 | sysfs_remove_link(&dev->kobj, "device"); | |
1644 | if (new_parent) | |
1645 | error = sysfs_create_link(&dev->kobj, &new_parent->kobj, | |
1646 | "device"); | |
1647 | return error; | |
8a82472f CH |
1648 | #endif |
1649 | } | |
1650 | ||
1651 | /** | |
1652 | * device_move - moves a device to a new parent | |
1653 | * @dev: the pointer to the struct device to be moved | |
c744aeae | 1654 | * @new_parent: the new parent of the device (can by NULL) |
ffa6a705 | 1655 | * @dpm_order: how to reorder the dpm_list |
8a82472f | 1656 | */ |
ffa6a705 CH |
1657 | int device_move(struct device *dev, struct device *new_parent, |
1658 | enum dpm_order dpm_order) | |
8a82472f CH |
1659 | { |
1660 | int error; | |
1661 | struct device *old_parent; | |
c744aeae | 1662 | struct kobject *new_parent_kobj; |
8a82472f CH |
1663 | |
1664 | dev = get_device(dev); | |
1665 | if (!dev) | |
1666 | return -EINVAL; | |
1667 | ||
ffa6a705 | 1668 | device_pm_lock(); |
8a82472f | 1669 | new_parent = get_device(new_parent); |
4a3ad20c | 1670 | new_parent_kobj = get_device_parent(dev, new_parent); |
63b6971a | 1671 | |
1e0b2cf9 KS |
1672 | pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev), |
1673 | __func__, new_parent ? dev_name(new_parent) : "<NULL>"); | |
c744aeae | 1674 | error = kobject_move(&dev->kobj, new_parent_kobj); |
8a82472f | 1675 | if (error) { |
63b6971a | 1676 | cleanup_glue_dir(dev, new_parent_kobj); |
8a82472f CH |
1677 | put_device(new_parent); |
1678 | goto out; | |
1679 | } | |
1680 | old_parent = dev->parent; | |
1681 | dev->parent = new_parent; | |
1682 | if (old_parent) | |
f791b8c8 | 1683 | klist_remove(&dev->p->knode_parent); |
0d358f22 | 1684 | if (new_parent) { |
f791b8c8 GKH |
1685 | klist_add_tail(&dev->p->knode_parent, |
1686 | &new_parent->p->klist_children); | |
0d358f22 YL |
1687 | set_dev_node(dev, dev_to_node(new_parent)); |
1688 | } | |
1689 | ||
8a82472f CH |
1690 | if (!dev->class) |
1691 | goto out_put; | |
1692 | error = device_move_class_links(dev, old_parent, new_parent); | |
1693 | if (error) { | |
1694 | /* We ignore errors on cleanup since we're hosed anyway... */ | |
1695 | device_move_class_links(dev, new_parent, old_parent); | |
1696 | if (!kobject_move(&dev->kobj, &old_parent->kobj)) { | |
c744aeae | 1697 | if (new_parent) |
f791b8c8 | 1698 | klist_remove(&dev->p->knode_parent); |
0d358f22 YL |
1699 | dev->parent = old_parent; |
1700 | if (old_parent) { | |
f791b8c8 GKH |
1701 | klist_add_tail(&dev->p->knode_parent, |
1702 | &old_parent->p->klist_children); | |
0d358f22 YL |
1703 | set_dev_node(dev, dev_to_node(old_parent)); |
1704 | } | |
8a82472f | 1705 | } |
63b6971a | 1706 | cleanup_glue_dir(dev, new_parent_kobj); |
8a82472f CH |
1707 | put_device(new_parent); |
1708 | goto out; | |
1709 | } | |
ffa6a705 CH |
1710 | switch (dpm_order) { |
1711 | case DPM_ORDER_NONE: | |
1712 | break; | |
1713 | case DPM_ORDER_DEV_AFTER_PARENT: | |
1714 | device_pm_move_after(dev, new_parent); | |
1715 | break; | |
1716 | case DPM_ORDER_PARENT_BEFORE_DEV: | |
1717 | device_pm_move_before(new_parent, dev); | |
1718 | break; | |
1719 | case DPM_ORDER_DEV_LAST: | |
1720 | device_pm_move_last(dev); | |
1721 | break; | |
1722 | } | |
8a82472f CH |
1723 | out_put: |
1724 | put_device(old_parent); | |
1725 | out: | |
ffa6a705 | 1726 | device_pm_unlock(); |
8a82472f CH |
1727 | put_device(dev); |
1728 | return error; | |
1729 | } | |
8a82472f | 1730 | EXPORT_SYMBOL_GPL(device_move); |
37b0c020 GKH |
1731 | |
1732 | /** | |
1733 | * device_shutdown - call ->shutdown() on each device to shutdown. | |
1734 | */ | |
1735 | void device_shutdown(void) | |
1736 | { | |
6245838f HD |
1737 | struct device *dev; |
1738 | ||
1739 | spin_lock(&devices_kset->list_lock); | |
1740 | /* | |
1741 | * Walk the devices list backward, shutting down each in turn. | |
1742 | * Beware that device unplug events may also start pulling | |
1743 | * devices offline, even as the system is shutting down. | |
1744 | */ | |
1745 | while (!list_empty(&devices_kset->list)) { | |
1746 | dev = list_entry(devices_kset->list.prev, struct device, | |
1747 | kobj.entry); | |
1748 | get_device(dev); | |
1749 | /* | |
1750 | * Make sure the device is off the kset list, in the | |
1751 | * event that dev->*->shutdown() doesn't remove it. | |
1752 | */ | |
1753 | list_del_init(&dev->kobj.entry); | |
1754 | spin_unlock(&devices_kset->list_lock); | |
37b0c020 | 1755 | |
37b0c020 GKH |
1756 | if (dev->bus && dev->bus->shutdown) { |
1757 | dev_dbg(dev, "shutdown\n"); | |
1758 | dev->bus->shutdown(dev); | |
1759 | } else if (dev->driver && dev->driver->shutdown) { | |
1760 | dev_dbg(dev, "shutdown\n"); | |
1761 | dev->driver->shutdown(dev); | |
1762 | } | |
6245838f HD |
1763 | put_device(dev); |
1764 | ||
1765 | spin_lock(&devices_kset->list_lock); | |
37b0c020 | 1766 | } |
6245838f | 1767 | spin_unlock(&devices_kset->list_lock); |
401097ea | 1768 | async_synchronize_full(); |
37b0c020 | 1769 | } |