]>
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> |
07d57a32 GL |
21 | #include <linux/of.h> |
22 | #include <linux/of_device.h> | |
da231fd5 | 23 | #include <linux/genhd.h> |
815d2d50 | 24 | #include <linux/kallsyms.h> |
f75b1c60 | 25 | #include <linux/mutex.h> |
401097ea | 26 | #include <linux/async.h> |
af8db150 | 27 | #include <linux/pm_runtime.h> |
c4e00daa | 28 | #include <linux/netdevice.h> |
1da177e4 LT |
29 | |
30 | #include "base.h" | |
31 | #include "power/power.h" | |
32 | ||
e52eec13 AK |
33 | #ifdef CONFIG_SYSFS_DEPRECATED |
34 | #ifdef CONFIG_SYSFS_DEPRECATED_V2 | |
35 | long sysfs_deprecated = 1; | |
36 | #else | |
37 | long sysfs_deprecated = 0; | |
38 | #endif | |
3454bf96 | 39 | static int __init sysfs_deprecated_setup(char *arg) |
e52eec13 | 40 | { |
34da5e67 | 41 | return kstrtol(arg, 10, &sysfs_deprecated); |
e52eec13 AK |
42 | } |
43 | early_param("sysfs.deprecated", sysfs_deprecated_setup); | |
44 | #endif | |
45 | ||
4a3ad20c GKH |
46 | int (*platform_notify)(struct device *dev) = NULL; |
47 | int (*platform_notify_remove)(struct device *dev) = NULL; | |
e105b8bf DW |
48 | static struct kobject *dev_kobj; |
49 | struct kobject *sysfs_dev_char_kobj; | |
50 | struct kobject *sysfs_dev_block_kobj; | |
1da177e4 | 51 | |
4e886c29 GKH |
52 | #ifdef CONFIG_BLOCK |
53 | static inline int device_is_not_partition(struct device *dev) | |
54 | { | |
55 | return !(dev->type == &part_type); | |
56 | } | |
57 | #else | |
58 | static inline int device_is_not_partition(struct device *dev) | |
59 | { | |
60 | return 1; | |
61 | } | |
62 | #endif | |
1da177e4 | 63 | |
3e95637a AS |
64 | /** |
65 | * dev_driver_string - Return a device's driver name, if at all possible | |
66 | * @dev: struct device to get the name of | |
67 | * | |
68 | * Will return the device's driver's name if it is bound to a device. If | |
9169c012 | 69 | * the device is not bound to a driver, it will return the name of the bus |
3e95637a AS |
70 | * it is attached to. If it is not attached to a bus either, an empty |
71 | * string will be returned. | |
72 | */ | |
bf9ca69f | 73 | const char *dev_driver_string(const struct device *dev) |
3e95637a | 74 | { |
3589972e AS |
75 | struct device_driver *drv; |
76 | ||
77 | /* dev->driver can change to NULL underneath us because of unbinding, | |
78 | * so be careful about accessing it. dev->bus and dev->class should | |
79 | * never change once they are set, so they don't need special care. | |
80 | */ | |
81 | drv = ACCESS_ONCE(dev->driver); | |
82 | return drv ? drv->name : | |
a456b702 JD |
83 | (dev->bus ? dev->bus->name : |
84 | (dev->class ? dev->class->name : "")); | |
3e95637a | 85 | } |
310a922d | 86 | EXPORT_SYMBOL(dev_driver_string); |
3e95637a | 87 | |
1da177e4 LT |
88 | #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr) |
89 | ||
4a3ad20c GKH |
90 | static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr, |
91 | char *buf) | |
1da177e4 | 92 | { |
4a3ad20c | 93 | struct device_attribute *dev_attr = to_dev_attr(attr); |
b0d1f807 | 94 | struct device *dev = kobj_to_dev(kobj); |
4a0c20bf | 95 | ssize_t ret = -EIO; |
1da177e4 LT |
96 | |
97 | if (dev_attr->show) | |
54b6f35c | 98 | ret = dev_attr->show(dev, dev_attr, buf); |
815d2d50 | 99 | if (ret >= (ssize_t)PAGE_SIZE) { |
53a9c87e GKH |
100 | print_symbol("dev_attr_show: %s returned bad count\n", |
101 | (unsigned long)dev_attr->show); | |
815d2d50 | 102 | } |
1da177e4 LT |
103 | return ret; |
104 | } | |
105 | ||
4a3ad20c GKH |
106 | static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr, |
107 | const char *buf, size_t count) | |
1da177e4 | 108 | { |
4a3ad20c | 109 | struct device_attribute *dev_attr = to_dev_attr(attr); |
b0d1f807 | 110 | struct device *dev = kobj_to_dev(kobj); |
4a0c20bf | 111 | ssize_t ret = -EIO; |
1da177e4 LT |
112 | |
113 | if (dev_attr->store) | |
54b6f35c | 114 | ret = dev_attr->store(dev, dev_attr, buf, count); |
1da177e4 LT |
115 | return ret; |
116 | } | |
117 | ||
52cf25d0 | 118 | static const struct sysfs_ops dev_sysfs_ops = { |
1da177e4 LT |
119 | .show = dev_attr_show, |
120 | .store = dev_attr_store, | |
121 | }; | |
122 | ||
ca22e56d KS |
123 | #define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr) |
124 | ||
125 | ssize_t device_store_ulong(struct device *dev, | |
126 | struct device_attribute *attr, | |
127 | const char *buf, size_t size) | |
128 | { | |
129 | struct dev_ext_attribute *ea = to_ext_attr(attr); | |
130 | char *end; | |
131 | unsigned long new = simple_strtoul(buf, &end, 0); | |
132 | if (end == buf) | |
133 | return -EINVAL; | |
134 | *(unsigned long *)(ea->var) = new; | |
135 | /* Always return full write size even if we didn't consume all */ | |
136 | return size; | |
137 | } | |
138 | EXPORT_SYMBOL_GPL(device_store_ulong); | |
139 | ||
140 | ssize_t device_show_ulong(struct device *dev, | |
141 | struct device_attribute *attr, | |
142 | char *buf) | |
143 | { | |
144 | struct dev_ext_attribute *ea = to_ext_attr(attr); | |
145 | return snprintf(buf, PAGE_SIZE, "%lx\n", *(unsigned long *)(ea->var)); | |
146 | } | |
147 | EXPORT_SYMBOL_GPL(device_show_ulong); | |
148 | ||
149 | ssize_t device_store_int(struct device *dev, | |
150 | struct device_attribute *attr, | |
151 | const char *buf, size_t size) | |
152 | { | |
153 | struct dev_ext_attribute *ea = to_ext_attr(attr); | |
154 | char *end; | |
155 | long new = simple_strtol(buf, &end, 0); | |
156 | if (end == buf || new > INT_MAX || new < INT_MIN) | |
157 | return -EINVAL; | |
158 | *(int *)(ea->var) = new; | |
159 | /* Always return full write size even if we didn't consume all */ | |
160 | return size; | |
161 | } | |
162 | EXPORT_SYMBOL_GPL(device_store_int); | |
163 | ||
164 | ssize_t device_show_int(struct device *dev, | |
165 | struct device_attribute *attr, | |
166 | char *buf) | |
167 | { | |
168 | struct dev_ext_attribute *ea = to_ext_attr(attr); | |
169 | ||
170 | return snprintf(buf, PAGE_SIZE, "%d\n", *(int *)(ea->var)); | |
171 | } | |
172 | EXPORT_SYMBOL_GPL(device_show_int); | |
1da177e4 | 173 | |
91872392 BP |
174 | ssize_t device_store_bool(struct device *dev, struct device_attribute *attr, |
175 | const char *buf, size_t size) | |
176 | { | |
177 | struct dev_ext_attribute *ea = to_ext_attr(attr); | |
178 | ||
179 | if (strtobool(buf, ea->var) < 0) | |
180 | return -EINVAL; | |
181 | ||
182 | return size; | |
183 | } | |
184 | EXPORT_SYMBOL_GPL(device_store_bool); | |
185 | ||
186 | ssize_t device_show_bool(struct device *dev, struct device_attribute *attr, | |
187 | char *buf) | |
188 | { | |
189 | struct dev_ext_attribute *ea = to_ext_attr(attr); | |
190 | ||
191 | return snprintf(buf, PAGE_SIZE, "%d\n", *(bool *)(ea->var)); | |
192 | } | |
193 | EXPORT_SYMBOL_GPL(device_show_bool); | |
194 | ||
1da177e4 | 195 | /** |
f8878dcb RD |
196 | * device_release - free device structure. |
197 | * @kobj: device's kobject. | |
1da177e4 | 198 | * |
f8878dcb RD |
199 | * This is called once the reference count for the object |
200 | * reaches 0. We forward the call to the device's release | |
201 | * method, which should handle actually freeing the structure. | |
1da177e4 | 202 | */ |
4a3ad20c | 203 | static void device_release(struct kobject *kobj) |
1da177e4 | 204 | { |
b0d1f807 | 205 | struct device *dev = kobj_to_dev(kobj); |
fb069a5d | 206 | struct device_private *p = dev->p; |
1da177e4 | 207 | |
a525a3dd ML |
208 | /* |
209 | * Some platform devices are driven without driver attached | |
210 | * and managed resources may have been acquired. Make sure | |
211 | * all resources are released. | |
212 | * | |
213 | * Drivers still can add resources into device after device | |
214 | * is deleted but alive, so release devres here to avoid | |
215 | * possible memory leak. | |
216 | */ | |
217 | devres_release_all(dev); | |
218 | ||
1da177e4 LT |
219 | if (dev->release) |
220 | dev->release(dev); | |
f9f852df KS |
221 | else if (dev->type && dev->type->release) |
222 | dev->type->release(dev); | |
2620efef GKH |
223 | else if (dev->class && dev->class->dev_release) |
224 | dev->class->dev_release(dev); | |
f810a5cf AV |
225 | else |
226 | WARN(1, KERN_ERR "Device '%s' does not have a release() " | |
4a3ad20c | 227 | "function, it is broken and must be fixed.\n", |
1e0b2cf9 | 228 | dev_name(dev)); |
fb069a5d | 229 | kfree(p); |
1da177e4 LT |
230 | } |
231 | ||
bc451f20 EB |
232 | static const void *device_namespace(struct kobject *kobj) |
233 | { | |
b0d1f807 | 234 | struct device *dev = kobj_to_dev(kobj); |
bc451f20 EB |
235 | const void *ns = NULL; |
236 | ||
237 | if (dev->class && dev->class->ns_type) | |
238 | ns = dev->class->namespace(dev); | |
239 | ||
240 | return ns; | |
241 | } | |
242 | ||
8f4afc41 | 243 | static struct kobj_type device_ktype = { |
1da177e4 LT |
244 | .release = device_release, |
245 | .sysfs_ops = &dev_sysfs_ops, | |
bc451f20 | 246 | .namespace = device_namespace, |
1da177e4 LT |
247 | }; |
248 | ||
249 | ||
312c004d | 250 | static int dev_uevent_filter(struct kset *kset, struct kobject *kobj) |
1da177e4 LT |
251 | { |
252 | struct kobj_type *ktype = get_ktype(kobj); | |
253 | ||
8f4afc41 | 254 | if (ktype == &device_ktype) { |
b0d1f807 | 255 | struct device *dev = kobj_to_dev(kobj); |
1da177e4 LT |
256 | if (dev->bus) |
257 | return 1; | |
23681e47 GKH |
258 | if (dev->class) |
259 | return 1; | |
1da177e4 LT |
260 | } |
261 | return 0; | |
262 | } | |
263 | ||
312c004d | 264 | static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj) |
1da177e4 | 265 | { |
b0d1f807 | 266 | struct device *dev = kobj_to_dev(kobj); |
1da177e4 | 267 | |
23681e47 GKH |
268 | if (dev->bus) |
269 | return dev->bus->name; | |
270 | if (dev->class) | |
271 | return dev->class->name; | |
272 | return NULL; | |
1da177e4 LT |
273 | } |
274 | ||
7eff2e7a KS |
275 | static int dev_uevent(struct kset *kset, struct kobject *kobj, |
276 | struct kobj_uevent_env *env) | |
1da177e4 | 277 | { |
b0d1f807 | 278 | struct device *dev = kobj_to_dev(kobj); |
1da177e4 LT |
279 | int retval = 0; |
280 | ||
6fcf53ac | 281 | /* add device node properties if present */ |
23681e47 | 282 | if (MAJOR(dev->devt)) { |
6fcf53ac KS |
283 | const char *tmp; |
284 | const char *name; | |
2c9ede55 | 285 | umode_t mode = 0; |
4e4098a3 GKH |
286 | kuid_t uid = GLOBAL_ROOT_UID; |
287 | kgid_t gid = GLOBAL_ROOT_GID; | |
6fcf53ac | 288 | |
7eff2e7a KS |
289 | add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt)); |
290 | add_uevent_var(env, "MINOR=%u", MINOR(dev->devt)); | |
3c2670e6 | 291 | name = device_get_devnode(dev, &mode, &uid, &gid, &tmp); |
6fcf53ac KS |
292 | if (name) { |
293 | add_uevent_var(env, "DEVNAME=%s", name); | |
e454cea2 KS |
294 | if (mode) |
295 | add_uevent_var(env, "DEVMODE=%#o", mode & 0777); | |
4e4098a3 GKH |
296 | if (!uid_eq(uid, GLOBAL_ROOT_UID)) |
297 | add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid)); | |
298 | if (!gid_eq(gid, GLOBAL_ROOT_GID)) | |
299 | add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid)); | |
3c2670e6 | 300 | kfree(tmp); |
6fcf53ac | 301 | } |
23681e47 GKH |
302 | } |
303 | ||
414264f9 | 304 | if (dev->type && dev->type->name) |
7eff2e7a | 305 | add_uevent_var(env, "DEVTYPE=%s", dev->type->name); |
414264f9 | 306 | |
239378f1 | 307 | if (dev->driver) |
7eff2e7a | 308 | add_uevent_var(env, "DRIVER=%s", dev->driver->name); |
239378f1 | 309 | |
07d57a32 GL |
310 | /* Add common DT information about the device */ |
311 | of_device_uevent(dev, env); | |
312 | ||
7eff2e7a | 313 | /* have the bus specific function add its stuff */ |
312c004d | 314 | if (dev->bus && dev->bus->uevent) { |
7eff2e7a | 315 | retval = dev->bus->uevent(dev, env); |
f9f852df | 316 | if (retval) |
7dc72b28 | 317 | pr_debug("device: '%s': %s: bus uevent() returned %d\n", |
1e0b2cf9 | 318 | dev_name(dev), __func__, retval); |
1da177e4 LT |
319 | } |
320 | ||
7eff2e7a | 321 | /* have the class specific function add its stuff */ |
2620efef | 322 | if (dev->class && dev->class->dev_uevent) { |
7eff2e7a | 323 | retval = dev->class->dev_uevent(dev, env); |
f9f852df | 324 | if (retval) |
7dc72b28 | 325 | pr_debug("device: '%s': %s: class uevent() " |
1e0b2cf9 | 326 | "returned %d\n", dev_name(dev), |
2b3a302a | 327 | __func__, retval); |
f9f852df KS |
328 | } |
329 | ||
eef35c2d | 330 | /* have the device type specific function add its stuff */ |
f9f852df | 331 | if (dev->type && dev->type->uevent) { |
7eff2e7a | 332 | retval = dev->type->uevent(dev, env); |
f9f852df | 333 | if (retval) |
7dc72b28 | 334 | pr_debug("device: '%s': %s: dev_type uevent() " |
1e0b2cf9 | 335 | "returned %d\n", dev_name(dev), |
2b3a302a | 336 | __func__, retval); |
2620efef GKH |
337 | } |
338 | ||
1da177e4 LT |
339 | return retval; |
340 | } | |
341 | ||
9cd43611 | 342 | static const struct kset_uevent_ops device_uevent_ops = { |
312c004d KS |
343 | .filter = dev_uevent_filter, |
344 | .name = dev_uevent_name, | |
345 | .uevent = dev_uevent, | |
1da177e4 LT |
346 | }; |
347 | ||
16574dcc KS |
348 | static ssize_t show_uevent(struct device *dev, struct device_attribute *attr, |
349 | char *buf) | |
350 | { | |
351 | struct kobject *top_kobj; | |
352 | struct kset *kset; | |
7eff2e7a | 353 | struct kobj_uevent_env *env = NULL; |
16574dcc KS |
354 | int i; |
355 | size_t count = 0; | |
356 | int retval; | |
357 | ||
358 | /* search the kset, the device belongs to */ | |
359 | top_kobj = &dev->kobj; | |
5c5daf65 KS |
360 | while (!top_kobj->kset && top_kobj->parent) |
361 | top_kobj = top_kobj->parent; | |
16574dcc KS |
362 | if (!top_kobj->kset) |
363 | goto out; | |
5c5daf65 | 364 | |
16574dcc KS |
365 | kset = top_kobj->kset; |
366 | if (!kset->uevent_ops || !kset->uevent_ops->uevent) | |
367 | goto out; | |
368 | ||
369 | /* respect filter */ | |
370 | if (kset->uevent_ops && kset->uevent_ops->filter) | |
371 | if (!kset->uevent_ops->filter(kset, &dev->kobj)) | |
372 | goto out; | |
373 | ||
7eff2e7a KS |
374 | env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL); |
375 | if (!env) | |
c7308c81 GKH |
376 | return -ENOMEM; |
377 | ||
16574dcc | 378 | /* let the kset specific function add its keys */ |
7eff2e7a | 379 | retval = kset->uevent_ops->uevent(kset, &dev->kobj, env); |
16574dcc KS |
380 | if (retval) |
381 | goto out; | |
382 | ||
383 | /* copy keys to file */ | |
7eff2e7a KS |
384 | for (i = 0; i < env->envp_idx; i++) |
385 | count += sprintf(&buf[count], "%s\n", env->envp[i]); | |
16574dcc | 386 | out: |
7eff2e7a | 387 | kfree(env); |
16574dcc KS |
388 | return count; |
389 | } | |
390 | ||
a7fd6706 KS |
391 | static ssize_t store_uevent(struct device *dev, struct device_attribute *attr, |
392 | const char *buf, size_t count) | |
393 | { | |
60a96a59 KS |
394 | enum kobject_action action; |
395 | ||
3f5468c9 | 396 | if (kobject_action_type(buf, count, &action) == 0) |
60a96a59 | 397 | kobject_uevent(&dev->kobj, action); |
3f5468c9 KS |
398 | else |
399 | dev_err(dev, "uevent: unknown action-string\n"); | |
a7fd6706 KS |
400 | return count; |
401 | } | |
402 | ||
ad6a1e1c TH |
403 | static struct device_attribute uevent_attr = |
404 | __ATTR(uevent, S_IRUGO | S_IWUSR, show_uevent, store_uevent); | |
405 | ||
4f3549d7 RW |
406 | static ssize_t show_online(struct device *dev, struct device_attribute *attr, |
407 | char *buf) | |
408 | { | |
409 | bool val; | |
410 | ||
411 | lock_device_hotplug(); | |
412 | val = !dev->offline; | |
413 | unlock_device_hotplug(); | |
414 | return sprintf(buf, "%u\n", val); | |
415 | } | |
416 | ||
417 | static ssize_t store_online(struct device *dev, struct device_attribute *attr, | |
418 | const char *buf, size_t count) | |
419 | { | |
420 | bool val; | |
421 | int ret; | |
422 | ||
423 | ret = strtobool(buf, &val); | |
424 | if (ret < 0) | |
425 | return ret; | |
426 | ||
427 | lock_device_hotplug(); | |
428 | ret = val ? device_online(dev) : device_offline(dev); | |
429 | unlock_device_hotplug(); | |
430 | return ret < 0 ? ret : count; | |
431 | } | |
432 | ||
433 | static struct device_attribute online_attr = | |
434 | __ATTR(online, S_IRUGO | S_IWUSR, show_online, store_online); | |
435 | ||
621a1672 DT |
436 | static int device_add_attributes(struct device *dev, |
437 | struct device_attribute *attrs) | |
de0ff00d | 438 | { |
621a1672 | 439 | int error = 0; |
de0ff00d | 440 | int i; |
621a1672 DT |
441 | |
442 | if (attrs) { | |
3e1026b3 | 443 | for (i = 0; attrs[i].attr.name; i++) { |
621a1672 DT |
444 | error = device_create_file(dev, &attrs[i]); |
445 | if (error) | |
446 | break; | |
447 | } | |
448 | if (error) | |
449 | while (--i >= 0) | |
450 | device_remove_file(dev, &attrs[i]); | |
451 | } | |
452 | return error; | |
453 | } | |
454 | ||
455 | static void device_remove_attributes(struct device *dev, | |
456 | struct device_attribute *attrs) | |
457 | { | |
458 | int i; | |
459 | ||
460 | if (attrs) | |
3e1026b3 | 461 | for (i = 0; attrs[i].attr.name; i++) |
621a1672 DT |
462 | device_remove_file(dev, &attrs[i]); |
463 | } | |
464 | ||
c97415a7 SA |
465 | static int device_add_bin_attributes(struct device *dev, |
466 | struct bin_attribute *attrs) | |
467 | { | |
468 | int error = 0; | |
469 | int i; | |
470 | ||
471 | if (attrs) { | |
3e1026b3 | 472 | for (i = 0; attrs[i].attr.name; i++) { |
c97415a7 SA |
473 | error = device_create_bin_file(dev, &attrs[i]); |
474 | if (error) | |
475 | break; | |
476 | } | |
477 | if (error) | |
478 | while (--i >= 0) | |
479 | device_remove_bin_file(dev, &attrs[i]); | |
480 | } | |
481 | return error; | |
482 | } | |
483 | ||
484 | static void device_remove_bin_attributes(struct device *dev, | |
485 | struct bin_attribute *attrs) | |
486 | { | |
487 | int i; | |
488 | ||
489 | if (attrs) | |
3e1026b3 | 490 | for (i = 0; attrs[i].attr.name; i++) |
c97415a7 SA |
491 | device_remove_bin_file(dev, &attrs[i]); |
492 | } | |
493 | ||
fa6fdb33 | 494 | int device_add_groups(struct device *dev, const struct attribute_group **groups) |
621a1672 | 495 | { |
3e9b2bae | 496 | return sysfs_create_groups(&dev->kobj, groups); |
de0ff00d GKH |
497 | } |
498 | ||
fa6fdb33 GKH |
499 | void device_remove_groups(struct device *dev, |
500 | const struct attribute_group **groups) | |
de0ff00d | 501 | { |
3e9b2bae | 502 | sysfs_remove_groups(&dev->kobj, groups); |
de0ff00d GKH |
503 | } |
504 | ||
2620efef GKH |
505 | static int device_add_attrs(struct device *dev) |
506 | { | |
507 | struct class *class = dev->class; | |
aed65af1 | 508 | const struct device_type *type = dev->type; |
621a1672 | 509 | int error; |
2620efef | 510 | |
621a1672 | 511 | if (class) { |
d05a6f96 | 512 | error = device_add_groups(dev, class->dev_groups); |
f9f852df | 513 | if (error) |
621a1672 | 514 | return error; |
d05a6f96 GKH |
515 | error = device_add_attributes(dev, class->dev_attrs); |
516 | if (error) | |
517 | goto err_remove_class_groups; | |
c97415a7 SA |
518 | error = device_add_bin_attributes(dev, class->dev_bin_attrs); |
519 | if (error) | |
520 | goto err_remove_class_attrs; | |
2620efef | 521 | } |
f9f852df | 522 | |
621a1672 DT |
523 | if (type) { |
524 | error = device_add_groups(dev, type->groups); | |
f9f852df | 525 | if (error) |
c97415a7 | 526 | goto err_remove_class_bin_attrs; |
f9f852df KS |
527 | } |
528 | ||
621a1672 DT |
529 | error = device_add_groups(dev, dev->groups); |
530 | if (error) | |
531 | goto err_remove_type_groups; | |
532 | ||
4f3549d7 RW |
533 | if (device_supports_offline(dev) && !dev->offline_disabled) { |
534 | error = device_create_file(dev, &online_attr); | |
535 | if (error) | |
536 | goto err_remove_type_groups; | |
537 | } | |
538 | ||
621a1672 DT |
539 | return 0; |
540 | ||
541 | err_remove_type_groups: | |
542 | if (type) | |
543 | device_remove_groups(dev, type->groups); | |
c97415a7 SA |
544 | err_remove_class_bin_attrs: |
545 | if (class) | |
546 | device_remove_bin_attributes(dev, class->dev_bin_attrs); | |
621a1672 DT |
547 | err_remove_class_attrs: |
548 | if (class) | |
549 | device_remove_attributes(dev, class->dev_attrs); | |
d05a6f96 GKH |
550 | err_remove_class_groups: |
551 | if (class) | |
552 | device_remove_groups(dev, class->dev_groups); | |
621a1672 | 553 | |
2620efef GKH |
554 | return error; |
555 | } | |
556 | ||
557 | static void device_remove_attrs(struct device *dev) | |
558 | { | |
559 | struct class *class = dev->class; | |
aed65af1 | 560 | const struct device_type *type = dev->type; |
2620efef | 561 | |
4f3549d7 | 562 | device_remove_file(dev, &online_attr); |
621a1672 | 563 | device_remove_groups(dev, dev->groups); |
f9f852df | 564 | |
621a1672 DT |
565 | if (type) |
566 | device_remove_groups(dev, type->groups); | |
567 | ||
c97415a7 | 568 | if (class) { |
621a1672 | 569 | device_remove_attributes(dev, class->dev_attrs); |
c97415a7 | 570 | device_remove_bin_attributes(dev, class->dev_bin_attrs); |
d05a6f96 | 571 | device_remove_groups(dev, class->dev_groups); |
c97415a7 | 572 | } |
2620efef GKH |
573 | } |
574 | ||
575 | ||
23681e47 GKH |
576 | static ssize_t show_dev(struct device *dev, struct device_attribute *attr, |
577 | char *buf) | |
578 | { | |
579 | return print_dev_t(buf, dev->devt); | |
580 | } | |
581 | ||
ad6a1e1c TH |
582 | static struct device_attribute devt_attr = |
583 | __ATTR(dev, S_IRUGO, show_dev, NULL); | |
584 | ||
ca22e56d | 585 | /* /sys/devices/ */ |
881c6cfd | 586 | struct kset *devices_kset; |
1da177e4 | 587 | |
1da177e4 | 588 | /** |
4a3ad20c GKH |
589 | * device_create_file - create sysfs attribute file for device. |
590 | * @dev: device. | |
591 | * @attr: device attribute descriptor. | |
1da177e4 | 592 | */ |
26579ab7 PC |
593 | int device_create_file(struct device *dev, |
594 | const struct device_attribute *attr) | |
1da177e4 LT |
595 | { |
596 | int error = 0; | |
8f46baaa FB |
597 | |
598 | if (dev) { | |
599 | WARN(((attr->attr.mode & S_IWUGO) && !attr->store), | |
97521978 | 600 | "Attribute %s: write permission without 'store'\n", |
601 | attr->attr.name); | |
8f46baaa | 602 | WARN(((attr->attr.mode & S_IRUGO) && !attr->show), |
97521978 | 603 | "Attribute %s: read permission without 'show'\n", |
604 | attr->attr.name); | |
1da177e4 | 605 | error = sysfs_create_file(&dev->kobj, &attr->attr); |
8f46baaa FB |
606 | } |
607 | ||
1da177e4 LT |
608 | return error; |
609 | } | |
86df2687 | 610 | EXPORT_SYMBOL_GPL(device_create_file); |
1da177e4 LT |
611 | |
612 | /** | |
4a3ad20c GKH |
613 | * device_remove_file - remove sysfs attribute file. |
614 | * @dev: device. | |
615 | * @attr: device attribute descriptor. | |
1da177e4 | 616 | */ |
26579ab7 PC |
617 | void device_remove_file(struct device *dev, |
618 | const struct device_attribute *attr) | |
1da177e4 | 619 | { |
0c98b19f | 620 | if (dev) |
1da177e4 | 621 | sysfs_remove_file(&dev->kobj, &attr->attr); |
1da177e4 | 622 | } |
86df2687 | 623 | EXPORT_SYMBOL_GPL(device_remove_file); |
1da177e4 | 624 | |
2589f188 GKH |
625 | /** |
626 | * device_create_bin_file - create sysfs binary attribute file for device. | |
627 | * @dev: device. | |
628 | * @attr: device binary attribute descriptor. | |
629 | */ | |
66ecb92b PC |
630 | int device_create_bin_file(struct device *dev, |
631 | const struct bin_attribute *attr) | |
2589f188 GKH |
632 | { |
633 | int error = -EINVAL; | |
634 | if (dev) | |
635 | error = sysfs_create_bin_file(&dev->kobj, attr); | |
636 | return error; | |
637 | } | |
638 | EXPORT_SYMBOL_GPL(device_create_bin_file); | |
639 | ||
640 | /** | |
641 | * device_remove_bin_file - remove sysfs binary attribute file | |
642 | * @dev: device. | |
643 | * @attr: device binary attribute descriptor. | |
644 | */ | |
66ecb92b PC |
645 | void device_remove_bin_file(struct device *dev, |
646 | const struct bin_attribute *attr) | |
2589f188 GKH |
647 | { |
648 | if (dev) | |
649 | sysfs_remove_bin_file(&dev->kobj, attr); | |
650 | } | |
651 | EXPORT_SYMBOL_GPL(device_remove_bin_file); | |
652 | ||
d9a9cdfb | 653 | /** |
523ded71 | 654 | * device_schedule_callback_owner - helper to schedule a callback for a device |
d9a9cdfb AS |
655 | * @dev: device. |
656 | * @func: callback function to invoke later. | |
523ded71 | 657 | * @owner: module owning the callback routine |
d9a9cdfb AS |
658 | * |
659 | * Attribute methods must not unregister themselves or their parent device | |
660 | * (which would amount to the same thing). Attempts to do so will deadlock, | |
661 | * since unregistration is mutually exclusive with driver callbacks. | |
662 | * | |
663 | * Instead methods can call this routine, which will attempt to allocate | |
664 | * and schedule a workqueue request to call back @func with @dev as its | |
665 | * argument in the workqueue's process context. @dev will be pinned until | |
666 | * @func returns. | |
667 | * | |
523ded71 AS |
668 | * This routine is usually called via the inline device_schedule_callback(), |
669 | * which automatically sets @owner to THIS_MODULE. | |
670 | * | |
d9a9cdfb | 671 | * Returns 0 if the request was submitted, -ENOMEM if storage could not |
523ded71 | 672 | * be allocated, -ENODEV if a reference to @owner isn't available. |
d9a9cdfb AS |
673 | * |
674 | * NOTE: This routine won't work if CONFIG_SYSFS isn't set! It uses an | |
675 | * underlying sysfs routine (since it is intended for use by attribute | |
676 | * methods), and if sysfs isn't available you'll get nothing but -ENOSYS. | |
677 | */ | |
523ded71 AS |
678 | int device_schedule_callback_owner(struct device *dev, |
679 | void (*func)(struct device *), struct module *owner) | |
d9a9cdfb AS |
680 | { |
681 | return sysfs_schedule_callback(&dev->kobj, | |
523ded71 | 682 | (void (*)(void *)) func, dev, owner); |
d9a9cdfb | 683 | } |
523ded71 | 684 | EXPORT_SYMBOL_GPL(device_schedule_callback_owner); |
d9a9cdfb | 685 | |
34bb61f9 JB |
686 | static void klist_children_get(struct klist_node *n) |
687 | { | |
f791b8c8 GKH |
688 | struct device_private *p = to_device_private_parent(n); |
689 | struct device *dev = p->device; | |
34bb61f9 JB |
690 | |
691 | get_device(dev); | |
692 | } | |
693 | ||
694 | static void klist_children_put(struct klist_node *n) | |
695 | { | |
f791b8c8 GKH |
696 | struct device_private *p = to_device_private_parent(n); |
697 | struct device *dev = p->device; | |
34bb61f9 JB |
698 | |
699 | put_device(dev); | |
700 | } | |
701 | ||
1da177e4 | 702 | /** |
4a3ad20c GKH |
703 | * device_initialize - init device structure. |
704 | * @dev: device. | |
1da177e4 | 705 | * |
5739411a CH |
706 | * This prepares the device for use by other layers by initializing |
707 | * its fields. | |
4a3ad20c | 708 | * It is the first half of device_register(), if called by |
5739411a CH |
709 | * that function, though it can also be called separately, so one |
710 | * may use @dev's fields. In particular, get_device()/put_device() | |
711 | * may be used for reference counting of @dev after calling this | |
712 | * function. | |
713 | * | |
b10d5efd AS |
714 | * All fields in @dev must be initialized by the caller to 0, except |
715 | * for those explicitly set to some other value. The simplest | |
716 | * approach is to use kzalloc() to allocate the structure containing | |
717 | * @dev. | |
718 | * | |
5739411a CH |
719 | * NOTE: Use put_device() to give up your reference instead of freeing |
720 | * @dev directly once you have called this function. | |
1da177e4 | 721 | */ |
1da177e4 LT |
722 | void device_initialize(struct device *dev) |
723 | { | |
881c6cfd | 724 | dev->kobj.kset = devices_kset; |
f9cb074b | 725 | kobject_init(&dev->kobj, &device_ktype); |
1da177e4 | 726 | INIT_LIST_HEAD(&dev->dma_pools); |
3142788b | 727 | mutex_init(&dev->mutex); |
1704f47b | 728 | lockdep_set_novalidate_class(&dev->mutex); |
9ac7849e TH |
729 | spin_lock_init(&dev->devres_lock); |
730 | INIT_LIST_HEAD(&dev->devres_head); | |
3b98aeaf | 731 | device_pm_init(dev); |
87348136 | 732 | set_dev_node(dev, -1); |
1da177e4 | 733 | } |
86df2687 | 734 | EXPORT_SYMBOL_GPL(device_initialize); |
1da177e4 | 735 | |
d73ce004 | 736 | struct kobject *virtual_device_parent(struct device *dev) |
f0ee61a6 | 737 | { |
86406245 | 738 | static struct kobject *virtual_dir = NULL; |
f0ee61a6 | 739 | |
86406245 | 740 | if (!virtual_dir) |
4ff6abff | 741 | virtual_dir = kobject_create_and_add("virtual", |
881c6cfd | 742 | &devices_kset->kobj); |
f0ee61a6 | 743 | |
86406245 | 744 | return virtual_dir; |
f0ee61a6 GKH |
745 | } |
746 | ||
bc451f20 EB |
747 | struct class_dir { |
748 | struct kobject kobj; | |
749 | struct class *class; | |
750 | }; | |
751 | ||
752 | #define to_class_dir(obj) container_of(obj, struct class_dir, kobj) | |
753 | ||
754 | static void class_dir_release(struct kobject *kobj) | |
755 | { | |
756 | struct class_dir *dir = to_class_dir(kobj); | |
757 | kfree(dir); | |
758 | } | |
759 | ||
760 | static const | |
761 | struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj) | |
40fa5422 | 762 | { |
bc451f20 EB |
763 | struct class_dir *dir = to_class_dir(kobj); |
764 | return dir->class->ns_type; | |
765 | } | |
766 | ||
767 | static struct kobj_type class_dir_ktype = { | |
768 | .release = class_dir_release, | |
769 | .sysfs_ops = &kobj_sysfs_ops, | |
770 | .child_ns_type = class_dir_child_ns_type | |
771 | }; | |
772 | ||
773 | static struct kobject * | |
774 | class_dir_create_and_add(struct class *class, struct kobject *parent_kobj) | |
775 | { | |
776 | struct class_dir *dir; | |
43968d2f GKH |
777 | int retval; |
778 | ||
bc451f20 EB |
779 | dir = kzalloc(sizeof(*dir), GFP_KERNEL); |
780 | if (!dir) | |
781 | return NULL; | |
782 | ||
783 | dir->class = class; | |
784 | kobject_init(&dir->kobj, &class_dir_ktype); | |
785 | ||
6b6e39a6 | 786 | dir->kobj.kset = &class->p->glue_dirs; |
bc451f20 EB |
787 | |
788 | retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name); | |
789 | if (retval < 0) { | |
790 | kobject_put(&dir->kobj); | |
791 | return NULL; | |
792 | } | |
793 | return &dir->kobj; | |
794 | } | |
795 | ||
796 | ||
797 | static struct kobject *get_device_parent(struct device *dev, | |
798 | struct device *parent) | |
799 | { | |
86406245 | 800 | if (dev->class) { |
77d3d7c1 | 801 | static DEFINE_MUTEX(gdp_mutex); |
86406245 KS |
802 | struct kobject *kobj = NULL; |
803 | struct kobject *parent_kobj; | |
804 | struct kobject *k; | |
805 | ||
ead454fe | 806 | #ifdef CONFIG_BLOCK |
39aba963 | 807 | /* block disks show up in /sys/block */ |
e52eec13 | 808 | if (sysfs_deprecated && dev->class == &block_class) { |
39aba963 KS |
809 | if (parent && parent->class == &block_class) |
810 | return &parent->kobj; | |
6b6e39a6 | 811 | return &block_class.p->subsys.kobj; |
39aba963 | 812 | } |
ead454fe | 813 | #endif |
e52eec13 | 814 | |
86406245 KS |
815 | /* |
816 | * If we have no parent, we live in "virtual". | |
0f4dafc0 KS |
817 | * Class-devices with a non class-device as parent, live |
818 | * in a "glue" directory to prevent namespace collisions. | |
86406245 KS |
819 | */ |
820 | if (parent == NULL) | |
821 | parent_kobj = virtual_device_parent(dev); | |
24b1442d | 822 | else if (parent->class && !dev->class->ns_type) |
86406245 KS |
823 | return &parent->kobj; |
824 | else | |
825 | parent_kobj = &parent->kobj; | |
826 | ||
77d3d7c1 TH |
827 | mutex_lock(&gdp_mutex); |
828 | ||
86406245 | 829 | /* find our class-directory at the parent and reference it */ |
6b6e39a6 KS |
830 | spin_lock(&dev->class->p->glue_dirs.list_lock); |
831 | list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry) | |
86406245 KS |
832 | if (k->parent == parent_kobj) { |
833 | kobj = kobject_get(k); | |
834 | break; | |
835 | } | |
6b6e39a6 | 836 | spin_unlock(&dev->class->p->glue_dirs.list_lock); |
77d3d7c1 TH |
837 | if (kobj) { |
838 | mutex_unlock(&gdp_mutex); | |
86406245 | 839 | return kobj; |
77d3d7c1 | 840 | } |
86406245 KS |
841 | |
842 | /* or create a new class-directory at the parent device */ | |
bc451f20 | 843 | k = class_dir_create_and_add(dev->class, parent_kobj); |
0f4dafc0 | 844 | /* do not emit an uevent for this simple "glue" directory */ |
77d3d7c1 | 845 | mutex_unlock(&gdp_mutex); |
43968d2f | 846 | return k; |
86406245 KS |
847 | } |
848 | ||
ca22e56d KS |
849 | /* subsystems can specify a default root directory for their devices */ |
850 | if (!parent && dev->bus && dev->bus->dev_root) | |
851 | return &dev->bus->dev_root->kobj; | |
852 | ||
86406245 | 853 | if (parent) |
c744aeae CH |
854 | return &parent->kobj; |
855 | return NULL; | |
856 | } | |
da231fd5 | 857 | |
63b6971a | 858 | static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir) |
da231fd5 | 859 | { |
0f4dafc0 | 860 | /* see if we live in a "glue" directory */ |
c1fe539a | 861 | if (!glue_dir || !dev->class || |
6b6e39a6 | 862 | glue_dir->kset != &dev->class->p->glue_dirs) |
da231fd5 KS |
863 | return; |
864 | ||
0f4dafc0 | 865 | kobject_put(glue_dir); |
da231fd5 | 866 | } |
63b6971a CH |
867 | |
868 | static void cleanup_device_parent(struct device *dev) | |
869 | { | |
870 | cleanup_glue_dir(dev, dev->kobj.parent); | |
871 | } | |
86406245 | 872 | |
2ee97caf CH |
873 | static int device_add_class_symlinks(struct device *dev) |
874 | { | |
875 | int error; | |
876 | ||
877 | if (!dev->class) | |
878 | return 0; | |
da231fd5 | 879 | |
1fbfee6c | 880 | error = sysfs_create_link(&dev->kobj, |
6b6e39a6 | 881 | &dev->class->p->subsys.kobj, |
2ee97caf CH |
882 | "subsystem"); |
883 | if (error) | |
884 | goto out; | |
da231fd5 | 885 | |
4e886c29 | 886 | if (dev->parent && device_is_not_partition(dev)) { |
39aba963 | 887 | error = sysfs_create_link(&dev->kobj, &dev->parent->kobj, |
4f01a757 DT |
888 | "device"); |
889 | if (error) | |
39aba963 | 890 | goto out_subsys; |
2ee97caf | 891 | } |
2ee97caf | 892 | |
ead454fe | 893 | #ifdef CONFIG_BLOCK |
39aba963 | 894 | /* /sys/block has directories and does not need symlinks */ |
e52eec13 | 895 | if (sysfs_deprecated && dev->class == &block_class) |
39aba963 | 896 | return 0; |
ead454fe | 897 | #endif |
39aba963 | 898 | |
da231fd5 | 899 | /* link in the class directory pointing to the device */ |
6b6e39a6 | 900 | error = sysfs_create_link(&dev->class->p->subsys.kobj, |
1e0b2cf9 | 901 | &dev->kobj, dev_name(dev)); |
da231fd5 | 902 | if (error) |
39aba963 | 903 | goto out_device; |
da231fd5 | 904 | |
da231fd5 KS |
905 | return 0; |
906 | ||
39aba963 KS |
907 | out_device: |
908 | sysfs_remove_link(&dev->kobj, "device"); | |
da231fd5 | 909 | |
2ee97caf CH |
910 | out_subsys: |
911 | sysfs_remove_link(&dev->kobj, "subsystem"); | |
912 | out: | |
913 | return error; | |
914 | } | |
915 | ||
916 | static void device_remove_class_symlinks(struct device *dev) | |
917 | { | |
918 | if (!dev->class) | |
919 | return; | |
da231fd5 | 920 | |
4e886c29 | 921 | if (dev->parent && device_is_not_partition(dev)) |
da231fd5 | 922 | sysfs_remove_link(&dev->kobj, "device"); |
2ee97caf | 923 | sysfs_remove_link(&dev->kobj, "subsystem"); |
ead454fe | 924 | #ifdef CONFIG_BLOCK |
e52eec13 | 925 | if (sysfs_deprecated && dev->class == &block_class) |
39aba963 | 926 | return; |
ead454fe | 927 | #endif |
6b6e39a6 | 928 | sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev)); |
2ee97caf CH |
929 | } |
930 | ||
413c239f SR |
931 | /** |
932 | * dev_set_name - set a device name | |
933 | * @dev: device | |
46232366 | 934 | * @fmt: format string for the device's name |
413c239f SR |
935 | */ |
936 | int dev_set_name(struct device *dev, const char *fmt, ...) | |
937 | { | |
938 | va_list vargs; | |
1fa5ae85 | 939 | int err; |
413c239f SR |
940 | |
941 | va_start(vargs, fmt); | |
1fa5ae85 | 942 | err = kobject_set_name_vargs(&dev->kobj, fmt, vargs); |
413c239f | 943 | va_end(vargs); |
1fa5ae85 | 944 | return err; |
413c239f SR |
945 | } |
946 | EXPORT_SYMBOL_GPL(dev_set_name); | |
947 | ||
e105b8bf DW |
948 | /** |
949 | * device_to_dev_kobj - select a /sys/dev/ directory for the device | |
950 | * @dev: device | |
951 | * | |
952 | * By default we select char/ for new entries. Setting class->dev_obj | |
953 | * to NULL prevents an entry from being created. class->dev_kobj must | |
954 | * be set (or cleared) before any devices are registered to the class | |
955 | * otherwise device_create_sys_dev_entry() and | |
0d4e293c PK |
956 | * device_remove_sys_dev_entry() will disagree about the presence of |
957 | * the link. | |
e105b8bf DW |
958 | */ |
959 | static struct kobject *device_to_dev_kobj(struct device *dev) | |
960 | { | |
961 | struct kobject *kobj; | |
962 | ||
963 | if (dev->class) | |
964 | kobj = dev->class->dev_kobj; | |
965 | else | |
966 | kobj = sysfs_dev_char_kobj; | |
967 | ||
968 | return kobj; | |
969 | } | |
970 | ||
971 | static int device_create_sys_dev_entry(struct device *dev) | |
972 | { | |
973 | struct kobject *kobj = device_to_dev_kobj(dev); | |
974 | int error = 0; | |
975 | char devt_str[15]; | |
976 | ||
977 | if (kobj) { | |
978 | format_dev_t(devt_str, dev->devt); | |
979 | error = sysfs_create_link(kobj, &dev->kobj, devt_str); | |
980 | } | |
981 | ||
982 | return error; | |
983 | } | |
984 | ||
985 | static void device_remove_sys_dev_entry(struct device *dev) | |
986 | { | |
987 | struct kobject *kobj = device_to_dev_kobj(dev); | |
988 | char devt_str[15]; | |
989 | ||
990 | if (kobj) { | |
991 | format_dev_t(devt_str, dev->devt); | |
992 | sysfs_remove_link(kobj, devt_str); | |
993 | } | |
994 | } | |
995 | ||
b4028437 GKH |
996 | int device_private_init(struct device *dev) |
997 | { | |
998 | dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL); | |
999 | if (!dev->p) | |
1000 | return -ENOMEM; | |
1001 | dev->p->device = dev; | |
1002 | klist_init(&dev->p->klist_children, klist_children_get, | |
1003 | klist_children_put); | |
ef8a3fd6 | 1004 | INIT_LIST_HEAD(&dev->p->deferred_probe); |
b4028437 GKH |
1005 | return 0; |
1006 | } | |
1007 | ||
1da177e4 | 1008 | /** |
4a3ad20c GKH |
1009 | * device_add - add device to device hierarchy. |
1010 | * @dev: device. | |
1da177e4 | 1011 | * |
4a3ad20c GKH |
1012 | * This is part 2 of device_register(), though may be called |
1013 | * separately _iff_ device_initialize() has been called separately. | |
1da177e4 | 1014 | * |
5739411a | 1015 | * This adds @dev to the kobject hierarchy via kobject_add(), adds it |
4a3ad20c GKH |
1016 | * to the global and sibling lists for the device, then |
1017 | * adds it to the other relevant subsystems of the driver model. | |
5739411a | 1018 | * |
b10d5efd AS |
1019 | * Do not call this routine or device_register() more than once for |
1020 | * any device structure. The driver model core is not designed to work | |
1021 | * with devices that get unregistered and then spring back to life. | |
1022 | * (Among other things, it's very hard to guarantee that all references | |
1023 | * to the previous incarnation of @dev have been dropped.) Allocate | |
1024 | * and register a fresh new struct device instead. | |
1025 | * | |
5739411a CH |
1026 | * NOTE: _Never_ directly free @dev after calling this function, even |
1027 | * if it returned an error! Always use put_device() to give up your | |
1028 | * reference instead. | |
1da177e4 LT |
1029 | */ |
1030 | int device_add(struct device *dev) | |
1031 | { | |
1032 | struct device *parent = NULL; | |
ca22e56d | 1033 | struct kobject *kobj; |
c47ed219 | 1034 | struct class_interface *class_intf; |
c906a48a | 1035 | int error = -EINVAL; |
775b64d2 | 1036 | |
1da177e4 | 1037 | dev = get_device(dev); |
c906a48a GKH |
1038 | if (!dev) |
1039 | goto done; | |
1040 | ||
fb069a5d | 1041 | if (!dev->p) { |
b4028437 GKH |
1042 | error = device_private_init(dev); |
1043 | if (error) | |
1044 | goto done; | |
fb069a5d | 1045 | } |
fb069a5d | 1046 | |
1fa5ae85 KS |
1047 | /* |
1048 | * for statically allocated devices, which should all be converted | |
1049 | * some day, we need to initialize the name. We prevent reading back | |
1050 | * the name, and force the use of dev_name() | |
1051 | */ | |
1052 | if (dev->init_name) { | |
acc0e90f | 1053 | dev_set_name(dev, "%s", dev->init_name); |
1fa5ae85 KS |
1054 | dev->init_name = NULL; |
1055 | } | |
c906a48a | 1056 | |
ca22e56d KS |
1057 | /* subsystems can specify simple device enumeration */ |
1058 | if (!dev_name(dev) && dev->bus && dev->bus->dev_name) | |
1059 | dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id); | |
1060 | ||
e6309e75 TG |
1061 | if (!dev_name(dev)) { |
1062 | error = -EINVAL; | |
5c8563d7 | 1063 | goto name_error; |
e6309e75 | 1064 | } |
1da177e4 | 1065 | |
1e0b2cf9 | 1066 | pr_debug("device: '%s': %s\n", dev_name(dev), __func__); |
c205ef48 | 1067 | |
1da177e4 | 1068 | parent = get_device(dev->parent); |
ca22e56d KS |
1069 | kobj = get_device_parent(dev, parent); |
1070 | if (kobj) | |
1071 | dev->kobj.parent = kobj; | |
1da177e4 | 1072 | |
0d358f22 YL |
1073 | /* use parent numa_node */ |
1074 | if (parent) | |
1075 | set_dev_node(dev, dev_to_node(parent)); | |
1076 | ||
1da177e4 | 1077 | /* first, register with generic layer. */ |
8a577ffc KS |
1078 | /* we require the name to be set before, and pass NULL */ |
1079 | error = kobject_add(&dev->kobj, dev->kobj.parent, NULL); | |
40fa5422 | 1080 | if (error) |
1da177e4 | 1081 | goto Error; |
a7fd6706 | 1082 | |
37022644 BW |
1083 | /* notify platform of device entry */ |
1084 | if (platform_notify) | |
1085 | platform_notify(dev); | |
1086 | ||
ad6a1e1c | 1087 | error = device_create_file(dev, &uevent_attr); |
a306eea4 CH |
1088 | if (error) |
1089 | goto attrError; | |
a7fd6706 | 1090 | |
23681e47 | 1091 | if (MAJOR(dev->devt)) { |
ad6a1e1c TH |
1092 | error = device_create_file(dev, &devt_attr); |
1093 | if (error) | |
a306eea4 | 1094 | goto ueventattrError; |
e105b8bf DW |
1095 | |
1096 | error = device_create_sys_dev_entry(dev); | |
1097 | if (error) | |
1098 | goto devtattrError; | |
2b2af54a KS |
1099 | |
1100 | devtmpfs_create_node(dev); | |
23681e47 GKH |
1101 | } |
1102 | ||
2ee97caf CH |
1103 | error = device_add_class_symlinks(dev); |
1104 | if (error) | |
1105 | goto SymlinkError; | |
dc0afa83 CH |
1106 | error = device_add_attrs(dev); |
1107 | if (error) | |
2620efef | 1108 | goto AttrsError; |
dc0afa83 CH |
1109 | error = bus_add_device(dev); |
1110 | if (error) | |
1da177e4 | 1111 | goto BusError; |
3b98aeaf | 1112 | error = dpm_sysfs_add(dev); |
57eee3d2 | 1113 | if (error) |
3b98aeaf AS |
1114 | goto DPMError; |
1115 | device_pm_add(dev); | |
ec0676ee AS |
1116 | |
1117 | /* Notify clients of device addition. This call must come | |
268863f4 | 1118 | * after dpm_sysfs_add() and before kobject_uevent(). |
ec0676ee AS |
1119 | */ |
1120 | if (dev->bus) | |
1121 | blocking_notifier_call_chain(&dev->bus->p->bus_notifier, | |
1122 | BUS_NOTIFY_ADD_DEVICE, dev); | |
1123 | ||
83b5fb4c | 1124 | kobject_uevent(&dev->kobj, KOBJ_ADD); |
2023c610 | 1125 | bus_probe_device(dev); |
1da177e4 | 1126 | if (parent) |
f791b8c8 GKH |
1127 | klist_add_tail(&dev->p->knode_parent, |
1128 | &parent->p->klist_children); | |
1da177e4 | 1129 | |
5d9fd169 | 1130 | if (dev->class) { |
ca22e56d | 1131 | mutex_lock(&dev->class->p->mutex); |
c47ed219 | 1132 | /* tie the class to the device */ |
5a3ceb86 | 1133 | klist_add_tail(&dev->knode_class, |
6b6e39a6 | 1134 | &dev->class->p->klist_devices); |
c47ed219 GKH |
1135 | |
1136 | /* notify any interfaces that the device is here */ | |
184f1f77 | 1137 | list_for_each_entry(class_intf, |
ca22e56d | 1138 | &dev->class->p->interfaces, node) |
c47ed219 GKH |
1139 | if (class_intf->add_dev) |
1140 | class_intf->add_dev(dev, class_intf); | |
ca22e56d | 1141 | mutex_unlock(&dev->class->p->mutex); |
5d9fd169 | 1142 | } |
c906a48a | 1143 | done: |
1da177e4 LT |
1144 | put_device(dev); |
1145 | return error; | |
3b98aeaf | 1146 | DPMError: |
57eee3d2 RW |
1147 | bus_remove_device(dev); |
1148 | BusError: | |
82f0cf9b | 1149 | device_remove_attrs(dev); |
2620efef | 1150 | AttrsError: |
2ee97caf CH |
1151 | device_remove_class_symlinks(dev); |
1152 | SymlinkError: | |
ad72956d KS |
1153 | if (MAJOR(dev->devt)) |
1154 | devtmpfs_delete_node(dev); | |
e105b8bf DW |
1155 | if (MAJOR(dev->devt)) |
1156 | device_remove_sys_dev_entry(dev); | |
1157 | devtattrError: | |
ad6a1e1c TH |
1158 | if (MAJOR(dev->devt)) |
1159 | device_remove_file(dev, &devt_attr); | |
a306eea4 | 1160 | ueventattrError: |
ad6a1e1c | 1161 | device_remove_file(dev, &uevent_attr); |
23681e47 | 1162 | attrError: |
312c004d | 1163 | kobject_uevent(&dev->kobj, KOBJ_REMOVE); |
1da177e4 LT |
1164 | kobject_del(&dev->kobj); |
1165 | Error: | |
63b6971a | 1166 | cleanup_device_parent(dev); |
1da177e4 LT |
1167 | if (parent) |
1168 | put_device(parent); | |
5c8563d7 KS |
1169 | name_error: |
1170 | kfree(dev->p); | |
1171 | dev->p = NULL; | |
c906a48a | 1172 | goto done; |
1da177e4 | 1173 | } |
86df2687 | 1174 | EXPORT_SYMBOL_GPL(device_add); |
1da177e4 | 1175 | |
1da177e4 | 1176 | /** |
4a3ad20c GKH |
1177 | * device_register - register a device with the system. |
1178 | * @dev: pointer to the device structure | |
1da177e4 | 1179 | * |
4a3ad20c GKH |
1180 | * This happens in two clean steps - initialize the device |
1181 | * and add it to the system. The two steps can be called | |
1182 | * separately, but this is the easiest and most common. | |
1183 | * I.e. you should only call the two helpers separately if | |
1184 | * have a clearly defined need to use and refcount the device | |
1185 | * before it is added to the hierarchy. | |
5739411a | 1186 | * |
b10d5efd AS |
1187 | * For more information, see the kerneldoc for device_initialize() |
1188 | * and device_add(). | |
1189 | * | |
5739411a CH |
1190 | * NOTE: _Never_ directly free @dev after calling this function, even |
1191 | * if it returned an error! Always use put_device() to give up the | |
1192 | * reference initialized in this function instead. | |
1da177e4 | 1193 | */ |
1da177e4 LT |
1194 | int device_register(struct device *dev) |
1195 | { | |
1196 | device_initialize(dev); | |
1197 | return device_add(dev); | |
1198 | } | |
86df2687 | 1199 | EXPORT_SYMBOL_GPL(device_register); |
1da177e4 | 1200 | |
1da177e4 | 1201 | /** |
4a3ad20c GKH |
1202 | * get_device - increment reference count for device. |
1203 | * @dev: device. | |
1da177e4 | 1204 | * |
4a3ad20c GKH |
1205 | * This simply forwards the call to kobject_get(), though |
1206 | * we do take care to provide for the case that we get a NULL | |
1207 | * pointer passed in. | |
1da177e4 | 1208 | */ |
4a3ad20c | 1209 | struct device *get_device(struct device *dev) |
1da177e4 | 1210 | { |
b0d1f807 | 1211 | return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL; |
1da177e4 | 1212 | } |
86df2687 | 1213 | EXPORT_SYMBOL_GPL(get_device); |
1da177e4 | 1214 | |
1da177e4 | 1215 | /** |
4a3ad20c GKH |
1216 | * put_device - decrement reference count. |
1217 | * @dev: device in question. | |
1da177e4 | 1218 | */ |
4a3ad20c | 1219 | void put_device(struct device *dev) |
1da177e4 | 1220 | { |
edfaa7c3 | 1221 | /* might_sleep(); */ |
1da177e4 LT |
1222 | if (dev) |
1223 | kobject_put(&dev->kobj); | |
1224 | } | |
86df2687 | 1225 | EXPORT_SYMBOL_GPL(put_device); |
1da177e4 | 1226 | |
1da177e4 | 1227 | /** |
4a3ad20c GKH |
1228 | * device_del - delete device from system. |
1229 | * @dev: device. | |
1da177e4 | 1230 | * |
4a3ad20c GKH |
1231 | * This is the first part of the device unregistration |
1232 | * sequence. This removes the device from the lists we control | |
1233 | * from here, has it removed from the other driver model | |
1234 | * subsystems it was added to in device_add(), and removes it | |
1235 | * from the kobject hierarchy. | |
1da177e4 | 1236 | * |
4a3ad20c GKH |
1237 | * NOTE: this should be called manually _iff_ device_add() was |
1238 | * also called manually. | |
1da177e4 | 1239 | */ |
4a3ad20c | 1240 | void device_del(struct device *dev) |
1da177e4 | 1241 | { |
4a3ad20c | 1242 | struct device *parent = dev->parent; |
c47ed219 | 1243 | struct class_interface *class_intf; |
1da177e4 | 1244 | |
ec0676ee AS |
1245 | /* Notify clients of device removal. This call must come |
1246 | * before dpm_sysfs_remove(). | |
1247 | */ | |
1248 | if (dev->bus) | |
1249 | blocking_notifier_call_chain(&dev->bus->p->bus_notifier, | |
1250 | BUS_NOTIFY_DEL_DEVICE, dev); | |
3b98aeaf | 1251 | dpm_sysfs_remove(dev); |
1da177e4 | 1252 | if (parent) |
f791b8c8 | 1253 | klist_del(&dev->p->knode_parent); |
e105b8bf | 1254 | if (MAJOR(dev->devt)) { |
2b2af54a | 1255 | devtmpfs_delete_node(dev); |
e105b8bf | 1256 | device_remove_sys_dev_entry(dev); |
ad6a1e1c | 1257 | device_remove_file(dev, &devt_attr); |
e105b8bf | 1258 | } |
b9d9c82b | 1259 | if (dev->class) { |
da231fd5 | 1260 | device_remove_class_symlinks(dev); |
99ef3ef8 | 1261 | |
ca22e56d | 1262 | mutex_lock(&dev->class->p->mutex); |
c47ed219 | 1263 | /* notify any interfaces that the device is now gone */ |
184f1f77 | 1264 | list_for_each_entry(class_intf, |
ca22e56d | 1265 | &dev->class->p->interfaces, node) |
c47ed219 GKH |
1266 | if (class_intf->remove_dev) |
1267 | class_intf->remove_dev(dev, class_intf); | |
1268 | /* remove the device from the class list */ | |
5a3ceb86 | 1269 | klist_del(&dev->knode_class); |
ca22e56d | 1270 | mutex_unlock(&dev->class->p->mutex); |
b9d9c82b | 1271 | } |
ad6a1e1c | 1272 | device_remove_file(dev, &uevent_attr); |
2620efef | 1273 | device_remove_attrs(dev); |
28953533 | 1274 | bus_remove_device(dev); |
4b6d1f12 | 1275 | device_pm_remove(dev); |
d1c3414c | 1276 | driver_deferred_probe_del(dev); |
1da177e4 LT |
1277 | |
1278 | /* Notify the platform of the removal, in case they | |
1279 | * need to do anything... | |
1280 | */ | |
1281 | if (platform_notify_remove) | |
1282 | platform_notify_remove(dev); | |
312c004d | 1283 | kobject_uevent(&dev->kobj, KOBJ_REMOVE); |
da231fd5 | 1284 | cleanup_device_parent(dev); |
1da177e4 | 1285 | kobject_del(&dev->kobj); |
da231fd5 | 1286 | put_device(parent); |
1da177e4 | 1287 | } |
86df2687 | 1288 | EXPORT_SYMBOL_GPL(device_del); |
1da177e4 LT |
1289 | |
1290 | /** | |
4a3ad20c GKH |
1291 | * device_unregister - unregister device from system. |
1292 | * @dev: device going away. | |
1da177e4 | 1293 | * |
4a3ad20c GKH |
1294 | * We do this in two parts, like we do device_register(). First, |
1295 | * we remove it from all the subsystems with device_del(), then | |
1296 | * we decrement the reference count via put_device(). If that | |
1297 | * is the final reference count, the device will be cleaned up | |
1298 | * via device_release() above. Otherwise, the structure will | |
1299 | * stick around until the final reference to the device is dropped. | |
1da177e4 | 1300 | */ |
4a3ad20c | 1301 | void device_unregister(struct device *dev) |
1da177e4 | 1302 | { |
1e0b2cf9 | 1303 | pr_debug("device: '%s': %s\n", dev_name(dev), __func__); |
1da177e4 LT |
1304 | device_del(dev); |
1305 | put_device(dev); | |
1306 | } | |
86df2687 | 1307 | EXPORT_SYMBOL_GPL(device_unregister); |
1da177e4 | 1308 | |
4a3ad20c | 1309 | static struct device *next_device(struct klist_iter *i) |
36239577 | 1310 | { |
4a3ad20c | 1311 | struct klist_node *n = klist_next(i); |
f791b8c8 GKH |
1312 | struct device *dev = NULL; |
1313 | struct device_private *p; | |
1314 | ||
1315 | if (n) { | |
1316 | p = to_device_private_parent(n); | |
1317 | dev = p->device; | |
1318 | } | |
1319 | return dev; | |
36239577 PM |
1320 | } |
1321 | ||
6fcf53ac | 1322 | /** |
e454cea2 | 1323 | * device_get_devnode - path of device node file |
6fcf53ac | 1324 | * @dev: device |
e454cea2 | 1325 | * @mode: returned file access mode |
3c2670e6 KS |
1326 | * @uid: returned file owner |
1327 | * @gid: returned file group | |
6fcf53ac KS |
1328 | * @tmp: possibly allocated string |
1329 | * | |
1330 | * Return the relative path of a possible device node. | |
1331 | * Non-default names may need to allocate a memory to compose | |
1332 | * a name. This memory is returned in tmp and needs to be | |
1333 | * freed by the caller. | |
1334 | */ | |
e454cea2 | 1335 | const char *device_get_devnode(struct device *dev, |
4e4098a3 | 1336 | umode_t *mode, kuid_t *uid, kgid_t *gid, |
3c2670e6 | 1337 | const char **tmp) |
6fcf53ac KS |
1338 | { |
1339 | char *s; | |
1340 | ||
1341 | *tmp = NULL; | |
1342 | ||
1343 | /* the device type may provide a specific name */ | |
e454cea2 | 1344 | if (dev->type && dev->type->devnode) |
3c2670e6 | 1345 | *tmp = dev->type->devnode(dev, mode, uid, gid); |
6fcf53ac KS |
1346 | if (*tmp) |
1347 | return *tmp; | |
1348 | ||
1349 | /* the class may provide a specific name */ | |
e454cea2 KS |
1350 | if (dev->class && dev->class->devnode) |
1351 | *tmp = dev->class->devnode(dev, mode); | |
6fcf53ac KS |
1352 | if (*tmp) |
1353 | return *tmp; | |
1354 | ||
1355 | /* return name without allocation, tmp == NULL */ | |
1356 | if (strchr(dev_name(dev), '!') == NULL) | |
1357 | return dev_name(dev); | |
1358 | ||
1359 | /* replace '!' in the name with '/' */ | |
1360 | *tmp = kstrdup(dev_name(dev), GFP_KERNEL); | |
1361 | if (!*tmp) | |
1362 | return NULL; | |
1363 | while ((s = strchr(*tmp, '!'))) | |
1364 | s[0] = '/'; | |
1365 | return *tmp; | |
1366 | } | |
1367 | ||
1da177e4 | 1368 | /** |
4a3ad20c GKH |
1369 | * device_for_each_child - device child iterator. |
1370 | * @parent: parent struct device. | |
4a3ad20c | 1371 | * @fn: function to be called for each device. |
f8878dcb | 1372 | * @data: data for the callback. |
1da177e4 | 1373 | * |
4a3ad20c GKH |
1374 | * Iterate over @parent's child devices, and call @fn for each, |
1375 | * passing it @data. | |
1da177e4 | 1376 | * |
4a3ad20c GKH |
1377 | * We check the return of @fn each time. If it returns anything |
1378 | * other than 0, we break out and return that value. | |
1da177e4 | 1379 | */ |
4a3ad20c GKH |
1380 | int device_for_each_child(struct device *parent, void *data, |
1381 | int (*fn)(struct device *dev, void *data)) | |
1da177e4 | 1382 | { |
36239577 | 1383 | struct klist_iter i; |
4a3ad20c | 1384 | struct device *child; |
1da177e4 LT |
1385 | int error = 0; |
1386 | ||
014c90db GKH |
1387 | if (!parent->p) |
1388 | return 0; | |
1389 | ||
f791b8c8 | 1390 | klist_iter_init(&parent->p->klist_children, &i); |
36239577 PM |
1391 | while ((child = next_device(&i)) && !error) |
1392 | error = fn(child, data); | |
1393 | klist_iter_exit(&i); | |
1da177e4 LT |
1394 | return error; |
1395 | } | |
86df2687 | 1396 | EXPORT_SYMBOL_GPL(device_for_each_child); |
1da177e4 | 1397 | |
5ab69981 CH |
1398 | /** |
1399 | * device_find_child - device iterator for locating a particular device. | |
1400 | * @parent: parent struct device | |
5ab69981 | 1401 | * @match: Callback function to check device |
f8878dcb | 1402 | * @data: Data to pass to match function |
5ab69981 CH |
1403 | * |
1404 | * This is similar to the device_for_each_child() function above, but it | |
1405 | * returns a reference to a device that is 'found' for later use, as | |
1406 | * determined by the @match callback. | |
1407 | * | |
1408 | * The callback should return 0 if the device doesn't match and non-zero | |
1409 | * if it does. If the callback returns non-zero and a reference to the | |
1410 | * current device can be obtained, this function will return to the caller | |
1411 | * and not iterate over any more devices. | |
a4e2400a FV |
1412 | * |
1413 | * NOTE: you will need to drop the reference with put_device() after use. | |
5ab69981 | 1414 | */ |
4a3ad20c GKH |
1415 | struct device *device_find_child(struct device *parent, void *data, |
1416 | int (*match)(struct device *dev, void *data)) | |
5ab69981 CH |
1417 | { |
1418 | struct klist_iter i; | |
1419 | struct device *child; | |
1420 | ||
1421 | if (!parent) | |
1422 | return NULL; | |
1423 | ||
f791b8c8 | 1424 | klist_iter_init(&parent->p->klist_children, &i); |
5ab69981 CH |
1425 | while ((child = next_device(&i))) |
1426 | if (match(child, data) && get_device(child)) | |
1427 | break; | |
1428 | klist_iter_exit(&i); | |
1429 | return child; | |
1430 | } | |
86df2687 | 1431 | EXPORT_SYMBOL_GPL(device_find_child); |
5ab69981 | 1432 | |
1da177e4 LT |
1433 | int __init devices_init(void) |
1434 | { | |
881c6cfd GKH |
1435 | devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL); |
1436 | if (!devices_kset) | |
1437 | return -ENOMEM; | |
e105b8bf DW |
1438 | dev_kobj = kobject_create_and_add("dev", NULL); |
1439 | if (!dev_kobj) | |
1440 | goto dev_kobj_err; | |
1441 | sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj); | |
1442 | if (!sysfs_dev_block_kobj) | |
1443 | goto block_kobj_err; | |
1444 | sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj); | |
1445 | if (!sysfs_dev_char_kobj) | |
1446 | goto char_kobj_err; | |
1447 | ||
881c6cfd | 1448 | return 0; |
e105b8bf DW |
1449 | |
1450 | char_kobj_err: | |
1451 | kobject_put(sysfs_dev_block_kobj); | |
1452 | block_kobj_err: | |
1453 | kobject_put(dev_kobj); | |
1454 | dev_kobj_err: | |
1455 | kset_unregister(devices_kset); | |
1456 | return -ENOMEM; | |
1da177e4 LT |
1457 | } |
1458 | ||
4f3549d7 RW |
1459 | static DEFINE_MUTEX(device_hotplug_lock); |
1460 | ||
1461 | void lock_device_hotplug(void) | |
1462 | { | |
1463 | mutex_lock(&device_hotplug_lock); | |
1464 | } | |
1465 | ||
1466 | void unlock_device_hotplug(void) | |
1467 | { | |
1468 | mutex_unlock(&device_hotplug_lock); | |
1469 | } | |
1470 | ||
1471 | static int device_check_offline(struct device *dev, void *not_used) | |
1472 | { | |
1473 | int ret; | |
1474 | ||
1475 | ret = device_for_each_child(dev, NULL, device_check_offline); | |
1476 | if (ret) | |
1477 | return ret; | |
1478 | ||
1479 | return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0; | |
1480 | } | |
1481 | ||
1482 | /** | |
1483 | * device_offline - Prepare the device for hot-removal. | |
1484 | * @dev: Device to be put offline. | |
1485 | * | |
1486 | * Execute the device bus type's .offline() callback, if present, to prepare | |
1487 | * the device for a subsequent hot-removal. If that succeeds, the device must | |
1488 | * not be used until either it is removed or its bus type's .online() callback | |
1489 | * is executed. | |
1490 | * | |
1491 | * Call under device_hotplug_lock. | |
1492 | */ | |
1493 | int device_offline(struct device *dev) | |
1494 | { | |
1495 | int ret; | |
1496 | ||
1497 | if (dev->offline_disabled) | |
1498 | return -EPERM; | |
1499 | ||
1500 | ret = device_for_each_child(dev, NULL, device_check_offline); | |
1501 | if (ret) | |
1502 | return ret; | |
1503 | ||
1504 | device_lock(dev); | |
1505 | if (device_supports_offline(dev)) { | |
1506 | if (dev->offline) { | |
1507 | ret = 1; | |
1508 | } else { | |
1509 | ret = dev->bus->offline(dev); | |
1510 | if (!ret) { | |
1511 | kobject_uevent(&dev->kobj, KOBJ_OFFLINE); | |
1512 | dev->offline = true; | |
1513 | } | |
1514 | } | |
1515 | } | |
1516 | device_unlock(dev); | |
1517 | ||
1518 | return ret; | |
1519 | } | |
1520 | ||
1521 | /** | |
1522 | * device_online - Put the device back online after successful device_offline(). | |
1523 | * @dev: Device to be put back online. | |
1524 | * | |
1525 | * If device_offline() has been successfully executed for @dev, but the device | |
1526 | * has not been removed subsequently, execute its bus type's .online() callback | |
1527 | * to indicate that the device can be used again. | |
1528 | * | |
1529 | * Call under device_hotplug_lock. | |
1530 | */ | |
1531 | int device_online(struct device *dev) | |
1532 | { | |
1533 | int ret = 0; | |
1534 | ||
1535 | device_lock(dev); | |
1536 | if (device_supports_offline(dev)) { | |
1537 | if (dev->offline) { | |
1538 | ret = dev->bus->online(dev); | |
1539 | if (!ret) { | |
1540 | kobject_uevent(&dev->kobj, KOBJ_ONLINE); | |
1541 | dev->offline = false; | |
1542 | } | |
1543 | } else { | |
1544 | ret = 1; | |
1545 | } | |
1546 | } | |
1547 | device_unlock(dev); | |
1548 | ||
1549 | return ret; | |
1550 | } | |
1551 | ||
7f100d15 | 1552 | struct root_device { |
0aa0dc41 MM |
1553 | struct device dev; |
1554 | struct module *owner; | |
1555 | }; | |
1556 | ||
93058424 | 1557 | static inline struct root_device *to_root_device(struct device *d) |
481e2079 FW |
1558 | { |
1559 | return container_of(d, struct root_device, dev); | |
1560 | } | |
0aa0dc41 MM |
1561 | |
1562 | static void root_device_release(struct device *dev) | |
1563 | { | |
1564 | kfree(to_root_device(dev)); | |
1565 | } | |
1566 | ||
1567 | /** | |
1568 | * __root_device_register - allocate and register a root device | |
1569 | * @name: root device name | |
1570 | * @owner: owner module of the root device, usually THIS_MODULE | |
1571 | * | |
1572 | * This function allocates a root device and registers it | |
1573 | * using device_register(). In order to free the returned | |
1574 | * device, use root_device_unregister(). | |
1575 | * | |
1576 | * Root devices are dummy devices which allow other devices | |
1577 | * to be grouped under /sys/devices. Use this function to | |
1578 | * allocate a root device and then use it as the parent of | |
1579 | * any device which should appear under /sys/devices/{name} | |
1580 | * | |
1581 | * The /sys/devices/{name} directory will also contain a | |
1582 | * 'module' symlink which points to the @owner directory | |
1583 | * in sysfs. | |
1584 | * | |
f0eae0ed JN |
1585 | * Returns &struct device pointer on success, or ERR_PTR() on error. |
1586 | * | |
0aa0dc41 MM |
1587 | * Note: You probably want to use root_device_register(). |
1588 | */ | |
1589 | struct device *__root_device_register(const char *name, struct module *owner) | |
1590 | { | |
1591 | struct root_device *root; | |
1592 | int err = -ENOMEM; | |
1593 | ||
1594 | root = kzalloc(sizeof(struct root_device), GFP_KERNEL); | |
1595 | if (!root) | |
1596 | return ERR_PTR(err); | |
1597 | ||
acc0e90f | 1598 | err = dev_set_name(&root->dev, "%s", name); |
0aa0dc41 MM |
1599 | if (err) { |
1600 | kfree(root); | |
1601 | return ERR_PTR(err); | |
1602 | } | |
1603 | ||
1604 | root->dev.release = root_device_release; | |
1605 | ||
1606 | err = device_register(&root->dev); | |
1607 | if (err) { | |
1608 | put_device(&root->dev); | |
1609 | return ERR_PTR(err); | |
1610 | } | |
1611 | ||
1d9e882b | 1612 | #ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */ |
0aa0dc41 MM |
1613 | if (owner) { |
1614 | struct module_kobject *mk = &owner->mkobj; | |
1615 | ||
1616 | err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module"); | |
1617 | if (err) { | |
1618 | device_unregister(&root->dev); | |
1619 | return ERR_PTR(err); | |
1620 | } | |
1621 | root->owner = owner; | |
1622 | } | |
1623 | #endif | |
1624 | ||
1625 | return &root->dev; | |
1626 | } | |
1627 | EXPORT_SYMBOL_GPL(__root_device_register); | |
1628 | ||
1629 | /** | |
1630 | * root_device_unregister - unregister and free a root device | |
7cbcf225 | 1631 | * @dev: device going away |
0aa0dc41 MM |
1632 | * |
1633 | * This function unregisters and cleans up a device that was created by | |
1634 | * root_device_register(). | |
1635 | */ | |
1636 | void root_device_unregister(struct device *dev) | |
1637 | { | |
1638 | struct root_device *root = to_root_device(dev); | |
1639 | ||
1640 | if (root->owner) | |
1641 | sysfs_remove_link(&root->dev.kobj, "module"); | |
1642 | ||
1643 | device_unregister(dev); | |
1644 | } | |
1645 | EXPORT_SYMBOL_GPL(root_device_unregister); | |
1646 | ||
23681e47 GKH |
1647 | |
1648 | static void device_create_release(struct device *dev) | |
1649 | { | |
1e0b2cf9 | 1650 | pr_debug("device: '%s': %s\n", dev_name(dev), __func__); |
23681e47 GKH |
1651 | kfree(dev); |
1652 | } | |
1653 | ||
39ef3112 GR |
1654 | static struct device * |
1655 | device_create_groups_vargs(struct class *class, struct device *parent, | |
1656 | dev_t devt, void *drvdata, | |
1657 | const struct attribute_group **groups, | |
1658 | const char *fmt, va_list args) | |
23681e47 | 1659 | { |
23681e47 GKH |
1660 | struct device *dev = NULL; |
1661 | int retval = -ENODEV; | |
1662 | ||
1663 | if (class == NULL || IS_ERR(class)) | |
1664 | goto error; | |
23681e47 GKH |
1665 | |
1666 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); | |
1667 | if (!dev) { | |
1668 | retval = -ENOMEM; | |
1669 | goto error; | |
1670 | } | |
1671 | ||
1672 | dev->devt = devt; | |
1673 | dev->class = class; | |
1674 | dev->parent = parent; | |
39ef3112 | 1675 | dev->groups = groups; |
23681e47 | 1676 | dev->release = device_create_release; |
8882b394 | 1677 | dev_set_drvdata(dev, drvdata); |
23681e47 | 1678 | |
1fa5ae85 KS |
1679 | retval = kobject_set_name_vargs(&dev->kobj, fmt, args); |
1680 | if (retval) | |
1681 | goto error; | |
1682 | ||
23681e47 GKH |
1683 | retval = device_register(dev); |
1684 | if (retval) | |
1685 | goto error; | |
1686 | ||
23681e47 GKH |
1687 | return dev; |
1688 | ||
1689 | error: | |
286661b3 | 1690 | put_device(dev); |
23681e47 GKH |
1691 | return ERR_PTR(retval); |
1692 | } | |
39ef3112 GR |
1693 | |
1694 | /** | |
1695 | * device_create_vargs - creates a device and registers it with sysfs | |
1696 | * @class: pointer to the struct class that this device should be registered to | |
1697 | * @parent: pointer to the parent struct device of this new device, if any | |
1698 | * @devt: the dev_t for the char device to be added | |
1699 | * @drvdata: the data to be added to the device for callbacks | |
1700 | * @fmt: string for the device's name | |
1701 | * @args: va_list for the device's name | |
1702 | * | |
1703 | * This function can be used by char device classes. A struct device | |
1704 | * will be created in sysfs, registered to the specified class. | |
1705 | * | |
1706 | * A "dev" file will be created, showing the dev_t for the device, if | |
1707 | * the dev_t is not 0,0. | |
1708 | * If a pointer to a parent struct device is passed in, the newly created | |
1709 | * struct device will be a child of that device in sysfs. | |
1710 | * The pointer to the struct device will be returned from the call. | |
1711 | * Any further sysfs files that might be required can be created using this | |
1712 | * pointer. | |
1713 | * | |
1714 | * Returns &struct device pointer on success, or ERR_PTR() on error. | |
1715 | * | |
1716 | * Note: the struct class passed to this function must have previously | |
1717 | * been created with a call to class_create(). | |
1718 | */ | |
1719 | struct device *device_create_vargs(struct class *class, struct device *parent, | |
1720 | dev_t devt, void *drvdata, const char *fmt, | |
1721 | va_list args) | |
1722 | { | |
1723 | return device_create_groups_vargs(class, parent, devt, drvdata, NULL, | |
1724 | fmt, args); | |
1725 | } | |
8882b394 GKH |
1726 | EXPORT_SYMBOL_GPL(device_create_vargs); |
1727 | ||
1728 | /** | |
4e106739 | 1729 | * device_create - creates a device and registers it with sysfs |
8882b394 GKH |
1730 | * @class: pointer to the struct class that this device should be registered to |
1731 | * @parent: pointer to the parent struct device of this new device, if any | |
1732 | * @devt: the dev_t for the char device to be added | |
1733 | * @drvdata: the data to be added to the device for callbacks | |
1734 | * @fmt: string for the device's name | |
1735 | * | |
1736 | * This function can be used by char device classes. A struct device | |
1737 | * will be created in sysfs, registered to the specified class. | |
1738 | * | |
1739 | * A "dev" file will be created, showing the dev_t for the device, if | |
1740 | * the dev_t is not 0,0. | |
1741 | * If a pointer to a parent struct device is passed in, the newly created | |
1742 | * struct device will be a child of that device in sysfs. | |
1743 | * The pointer to the struct device will be returned from the call. | |
1744 | * Any further sysfs files that might be required can be created using this | |
1745 | * pointer. | |
1746 | * | |
f0eae0ed JN |
1747 | * Returns &struct device pointer on success, or ERR_PTR() on error. |
1748 | * | |
8882b394 GKH |
1749 | * Note: the struct class passed to this function must have previously |
1750 | * been created with a call to class_create(). | |
1751 | */ | |
4e106739 GKH |
1752 | struct device *device_create(struct class *class, struct device *parent, |
1753 | dev_t devt, void *drvdata, const char *fmt, ...) | |
8882b394 GKH |
1754 | { |
1755 | va_list vargs; | |
1756 | struct device *dev; | |
1757 | ||
1758 | va_start(vargs, fmt); | |
1759 | dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs); | |
1760 | va_end(vargs); | |
1761 | return dev; | |
1762 | } | |
4e106739 | 1763 | EXPORT_SYMBOL_GPL(device_create); |
8882b394 | 1764 | |
39ef3112 GR |
1765 | /** |
1766 | * device_create_with_groups - creates a device and registers it with sysfs | |
1767 | * @class: pointer to the struct class that this device should be registered to | |
1768 | * @parent: pointer to the parent struct device of this new device, if any | |
1769 | * @devt: the dev_t for the char device to be added | |
1770 | * @drvdata: the data to be added to the device for callbacks | |
1771 | * @groups: NULL-terminated list of attribute groups to be created | |
1772 | * @fmt: string for the device's name | |
1773 | * | |
1774 | * This function can be used by char device classes. A struct device | |
1775 | * will be created in sysfs, registered to the specified class. | |
1776 | * Additional attributes specified in the groups parameter will also | |
1777 | * be created automatically. | |
1778 | * | |
1779 | * A "dev" file will be created, showing the dev_t for the device, if | |
1780 | * the dev_t is not 0,0. | |
1781 | * If a pointer to a parent struct device is passed in, the newly created | |
1782 | * struct device will be a child of that device in sysfs. | |
1783 | * The pointer to the struct device will be returned from the call. | |
1784 | * Any further sysfs files that might be required can be created using this | |
1785 | * pointer. | |
1786 | * | |
1787 | * Returns &struct device pointer on success, or ERR_PTR() on error. | |
1788 | * | |
1789 | * Note: the struct class passed to this function must have previously | |
1790 | * been created with a call to class_create(). | |
1791 | */ | |
1792 | struct device *device_create_with_groups(struct class *class, | |
1793 | struct device *parent, dev_t devt, | |
1794 | void *drvdata, | |
1795 | const struct attribute_group **groups, | |
1796 | const char *fmt, ...) | |
1797 | { | |
1798 | va_list vargs; | |
1799 | struct device *dev; | |
1800 | ||
1801 | va_start(vargs, fmt); | |
1802 | dev = device_create_groups_vargs(class, parent, devt, drvdata, groups, | |
1803 | fmt, vargs); | |
1804 | va_end(vargs); | |
1805 | return dev; | |
1806 | } | |
1807 | EXPORT_SYMBOL_GPL(device_create_with_groups); | |
1808 | ||
9f3b795a | 1809 | static int __match_devt(struct device *dev, const void *data) |
23681e47 | 1810 | { |
9f3b795a | 1811 | const dev_t *devt = data; |
23681e47 | 1812 | |
cd35449b | 1813 | return dev->devt == *devt; |
775b64d2 RW |
1814 | } |
1815 | ||
1816 | /** | |
1817 | * device_destroy - removes a device that was created with device_create() | |
1818 | * @class: pointer to the struct class that this device was registered with | |
1819 | * @devt: the dev_t of the device that was previously registered | |
1820 | * | |
1821 | * This call unregisters and cleans up a device that was created with a | |
1822 | * call to device_create(). | |
1823 | */ | |
1824 | void device_destroy(struct class *class, dev_t devt) | |
1825 | { | |
1826 | struct device *dev; | |
23681e47 | 1827 | |
695794ae | 1828 | dev = class_find_device(class, NULL, &devt, __match_devt); |
cd35449b DY |
1829 | if (dev) { |
1830 | put_device(dev); | |
23681e47 | 1831 | device_unregister(dev); |
cd35449b | 1832 | } |
23681e47 GKH |
1833 | } |
1834 | EXPORT_SYMBOL_GPL(device_destroy); | |
a2de48ca GKH |
1835 | |
1836 | /** | |
1837 | * device_rename - renames a device | |
1838 | * @dev: the pointer to the struct device to be renamed | |
1839 | * @new_name: the new name of the device | |
030c1d2b EB |
1840 | * |
1841 | * It is the responsibility of the caller to provide mutual | |
1842 | * exclusion between two different calls of device_rename | |
1843 | * on the same device to ensure that new_name is valid and | |
1844 | * won't conflict with other devices. | |
c6c0ac66 | 1845 | * |
a5462516 TT |
1846 | * Note: Don't call this function. Currently, the networking layer calls this |
1847 | * function, but that will change. The following text from Kay Sievers offers | |
1848 | * some insight: | |
1849 | * | |
1850 | * Renaming devices is racy at many levels, symlinks and other stuff are not | |
1851 | * replaced atomically, and you get a "move" uevent, but it's not easy to | |
1852 | * connect the event to the old and new device. Device nodes are not renamed at | |
1853 | * all, there isn't even support for that in the kernel now. | |
1854 | * | |
1855 | * In the meantime, during renaming, your target name might be taken by another | |
1856 | * driver, creating conflicts. Or the old name is taken directly after you | |
1857 | * renamed it -- then you get events for the same DEVPATH, before you even see | |
1858 | * the "move" event. It's just a mess, and nothing new should ever rely on | |
1859 | * kernel device renaming. Besides that, it's not even implemented now for | |
1860 | * other things than (driver-core wise very simple) network devices. | |
1861 | * | |
1862 | * We are currently about to change network renaming in udev to completely | |
1863 | * disallow renaming of devices in the same namespace as the kernel uses, | |
1864 | * because we can't solve the problems properly, that arise with swapping names | |
1865 | * of multiple interfaces without races. Means, renaming of eth[0-9]* will only | |
1866 | * be allowed to some other name than eth[0-9]*, for the aforementioned | |
1867 | * reasons. | |
1868 | * | |
1869 | * Make up a "real" name in the driver before you register anything, or add | |
1870 | * some other attributes for userspace to find the device, or use udev to add | |
1871 | * symlinks -- but never rename kernel devices later, it's a complete mess. We | |
1872 | * don't even want to get into that and try to implement the missing pieces in | |
1873 | * the core. We really have other pieces to fix in the driver core mess. :) | |
a2de48ca | 1874 | */ |
6937e8f8 | 1875 | int device_rename(struct device *dev, const char *new_name) |
a2de48ca | 1876 | { |
2ee97caf | 1877 | char *old_device_name = NULL; |
a2de48ca GKH |
1878 | int error; |
1879 | ||
1880 | dev = get_device(dev); | |
1881 | if (!dev) | |
1882 | return -EINVAL; | |
1883 | ||
1e0b2cf9 | 1884 | pr_debug("device: '%s': %s: renaming to '%s'\n", dev_name(dev), |
2b3a302a | 1885 | __func__, new_name); |
a2de48ca | 1886 | |
1fa5ae85 | 1887 | old_device_name = kstrdup(dev_name(dev), GFP_KERNEL); |
2ee97caf CH |
1888 | if (!old_device_name) { |
1889 | error = -ENOMEM; | |
1890 | goto out; | |
a2de48ca | 1891 | } |
a2de48ca | 1892 | |
f349cf34 | 1893 | if (dev->class) { |
6b6e39a6 | 1894 | error = sysfs_rename_link(&dev->class->p->subsys.kobj, |
f349cf34 EB |
1895 | &dev->kobj, old_device_name, new_name); |
1896 | if (error) | |
1897 | goto out; | |
1898 | } | |
39aba963 | 1899 | |
a2de48ca | 1900 | error = kobject_rename(&dev->kobj, new_name); |
1fa5ae85 | 1901 | if (error) |
2ee97caf | 1902 | goto out; |
a2de48ca | 1903 | |
2ee97caf | 1904 | out: |
a2de48ca GKH |
1905 | put_device(dev); |
1906 | ||
2ee97caf | 1907 | kfree(old_device_name); |
a2de48ca GKH |
1908 | |
1909 | return error; | |
1910 | } | |
a2807dbc | 1911 | EXPORT_SYMBOL_GPL(device_rename); |
8a82472f CH |
1912 | |
1913 | static int device_move_class_links(struct device *dev, | |
1914 | struct device *old_parent, | |
1915 | struct device *new_parent) | |
1916 | { | |
f7f3461d | 1917 | int error = 0; |
8a82472f | 1918 | |
f7f3461d GKH |
1919 | if (old_parent) |
1920 | sysfs_remove_link(&dev->kobj, "device"); | |
1921 | if (new_parent) | |
1922 | error = sysfs_create_link(&dev->kobj, &new_parent->kobj, | |
1923 | "device"); | |
1924 | return error; | |
8a82472f CH |
1925 | } |
1926 | ||
1927 | /** | |
1928 | * device_move - moves a device to a new parent | |
1929 | * @dev: the pointer to the struct device to be moved | |
c744aeae | 1930 | * @new_parent: the new parent of the device (can by NULL) |
ffa6a705 | 1931 | * @dpm_order: how to reorder the dpm_list |
8a82472f | 1932 | */ |
ffa6a705 CH |
1933 | int device_move(struct device *dev, struct device *new_parent, |
1934 | enum dpm_order dpm_order) | |
8a82472f CH |
1935 | { |
1936 | int error; | |
1937 | struct device *old_parent; | |
c744aeae | 1938 | struct kobject *new_parent_kobj; |
8a82472f CH |
1939 | |
1940 | dev = get_device(dev); | |
1941 | if (!dev) | |
1942 | return -EINVAL; | |
1943 | ||
ffa6a705 | 1944 | device_pm_lock(); |
8a82472f | 1945 | new_parent = get_device(new_parent); |
4a3ad20c | 1946 | new_parent_kobj = get_device_parent(dev, new_parent); |
63b6971a | 1947 | |
1e0b2cf9 KS |
1948 | pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev), |
1949 | __func__, new_parent ? dev_name(new_parent) : "<NULL>"); | |
c744aeae | 1950 | error = kobject_move(&dev->kobj, new_parent_kobj); |
8a82472f | 1951 | if (error) { |
63b6971a | 1952 | cleanup_glue_dir(dev, new_parent_kobj); |
8a82472f CH |
1953 | put_device(new_parent); |
1954 | goto out; | |
1955 | } | |
1956 | old_parent = dev->parent; | |
1957 | dev->parent = new_parent; | |
1958 | if (old_parent) | |
f791b8c8 | 1959 | klist_remove(&dev->p->knode_parent); |
0d358f22 | 1960 | if (new_parent) { |
f791b8c8 GKH |
1961 | klist_add_tail(&dev->p->knode_parent, |
1962 | &new_parent->p->klist_children); | |
0d358f22 YL |
1963 | set_dev_node(dev, dev_to_node(new_parent)); |
1964 | } | |
1965 | ||
bdd4034d RV |
1966 | if (dev->class) { |
1967 | error = device_move_class_links(dev, old_parent, new_parent); | |
1968 | if (error) { | |
1969 | /* We ignore errors on cleanup since we're hosed anyway... */ | |
1970 | device_move_class_links(dev, new_parent, old_parent); | |
1971 | if (!kobject_move(&dev->kobj, &old_parent->kobj)) { | |
1972 | if (new_parent) | |
1973 | klist_remove(&dev->p->knode_parent); | |
1974 | dev->parent = old_parent; | |
1975 | if (old_parent) { | |
1976 | klist_add_tail(&dev->p->knode_parent, | |
1977 | &old_parent->p->klist_children); | |
1978 | set_dev_node(dev, dev_to_node(old_parent)); | |
1979 | } | |
0d358f22 | 1980 | } |
bdd4034d RV |
1981 | cleanup_glue_dir(dev, new_parent_kobj); |
1982 | put_device(new_parent); | |
1983 | goto out; | |
8a82472f | 1984 | } |
8a82472f | 1985 | } |
ffa6a705 CH |
1986 | switch (dpm_order) { |
1987 | case DPM_ORDER_NONE: | |
1988 | break; | |
1989 | case DPM_ORDER_DEV_AFTER_PARENT: | |
1990 | device_pm_move_after(dev, new_parent); | |
1991 | break; | |
1992 | case DPM_ORDER_PARENT_BEFORE_DEV: | |
1993 | device_pm_move_before(new_parent, dev); | |
1994 | break; | |
1995 | case DPM_ORDER_DEV_LAST: | |
1996 | device_pm_move_last(dev); | |
1997 | break; | |
1998 | } | |
bdd4034d | 1999 | |
8a82472f CH |
2000 | put_device(old_parent); |
2001 | out: | |
ffa6a705 | 2002 | device_pm_unlock(); |
8a82472f CH |
2003 | put_device(dev); |
2004 | return error; | |
2005 | } | |
8a82472f | 2006 | EXPORT_SYMBOL_GPL(device_move); |
37b0c020 GKH |
2007 | |
2008 | /** | |
2009 | * device_shutdown - call ->shutdown() on each device to shutdown. | |
2010 | */ | |
2011 | void device_shutdown(void) | |
2012 | { | |
6245838f HD |
2013 | struct device *dev; |
2014 | ||
2015 | spin_lock(&devices_kset->list_lock); | |
2016 | /* | |
2017 | * Walk the devices list backward, shutting down each in turn. | |
2018 | * Beware that device unplug events may also start pulling | |
2019 | * devices offline, even as the system is shutting down. | |
2020 | */ | |
2021 | while (!list_empty(&devices_kset->list)) { | |
2022 | dev = list_entry(devices_kset->list.prev, struct device, | |
2023 | kobj.entry); | |
d1c6c030 ML |
2024 | |
2025 | /* | |
2026 | * hold reference count of device's parent to | |
2027 | * prevent it from being freed because parent's | |
2028 | * lock is to be held | |
2029 | */ | |
2030 | get_device(dev->parent); | |
6245838f HD |
2031 | get_device(dev); |
2032 | /* | |
2033 | * Make sure the device is off the kset list, in the | |
2034 | * event that dev->*->shutdown() doesn't remove it. | |
2035 | */ | |
2036 | list_del_init(&dev->kobj.entry); | |
2037 | spin_unlock(&devices_kset->list_lock); | |
fe6b91f4 | 2038 | |
d1c6c030 ML |
2039 | /* hold lock to avoid race with probe/release */ |
2040 | if (dev->parent) | |
2041 | device_lock(dev->parent); | |
2042 | device_lock(dev); | |
2043 | ||
fe6b91f4 AS |
2044 | /* Don't allow any more runtime suspends */ |
2045 | pm_runtime_get_noresume(dev); | |
2046 | pm_runtime_barrier(dev); | |
37b0c020 | 2047 | |
37b0c020 | 2048 | if (dev->bus && dev->bus->shutdown) { |
0246c4fa SL |
2049 | if (initcall_debug) |
2050 | dev_info(dev, "shutdown\n"); | |
37b0c020 GKH |
2051 | dev->bus->shutdown(dev); |
2052 | } else if (dev->driver && dev->driver->shutdown) { | |
0246c4fa SL |
2053 | if (initcall_debug) |
2054 | dev_info(dev, "shutdown\n"); | |
37b0c020 GKH |
2055 | dev->driver->shutdown(dev); |
2056 | } | |
d1c6c030 ML |
2057 | |
2058 | device_unlock(dev); | |
2059 | if (dev->parent) | |
2060 | device_unlock(dev->parent); | |
2061 | ||
6245838f | 2062 | put_device(dev); |
d1c6c030 | 2063 | put_device(dev->parent); |
6245838f HD |
2064 | |
2065 | spin_lock(&devices_kset->list_lock); | |
37b0c020 | 2066 | } |
6245838f | 2067 | spin_unlock(&devices_kset->list_lock); |
401097ea | 2068 | async_synchronize_full(); |
37b0c020 | 2069 | } |
99bcf217 JP |
2070 | |
2071 | /* | |
2072 | * Device logging functions | |
2073 | */ | |
2074 | ||
2075 | #ifdef CONFIG_PRINTK | |
666f355f JP |
2076 | static int |
2077 | create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen) | |
99bcf217 | 2078 | { |
c4e00daa | 2079 | const char *subsys; |
798efc60 | 2080 | size_t pos = 0; |
99bcf217 | 2081 | |
c4e00daa KS |
2082 | if (dev->class) |
2083 | subsys = dev->class->name; | |
2084 | else if (dev->bus) | |
2085 | subsys = dev->bus->name; | |
2086 | else | |
798efc60 | 2087 | return 0; |
c4e00daa | 2088 | |
798efc60 | 2089 | pos += snprintf(hdr + pos, hdrlen - pos, "SUBSYSTEM=%s", subsys); |
c4e00daa KS |
2090 | |
2091 | /* | |
2092 | * Add device identifier DEVICE=: | |
2093 | * b12:8 block dev_t | |
2094 | * c127:3 char dev_t | |
2095 | * n8 netdev ifindex | |
2096 | * +sound:card0 subsystem:devname | |
2097 | */ | |
2098 | if (MAJOR(dev->devt)) { | |
2099 | char c; | |
2100 | ||
2101 | if (strcmp(subsys, "block") == 0) | |
2102 | c = 'b'; | |
2103 | else | |
2104 | c = 'c'; | |
798efc60 JP |
2105 | pos++; |
2106 | pos += snprintf(hdr + pos, hdrlen - pos, | |
2107 | "DEVICE=%c%u:%u", | |
2108 | c, MAJOR(dev->devt), MINOR(dev->devt)); | |
c4e00daa KS |
2109 | } else if (strcmp(subsys, "net") == 0) { |
2110 | struct net_device *net = to_net_dev(dev); | |
2111 | ||
798efc60 JP |
2112 | pos++; |
2113 | pos += snprintf(hdr + pos, hdrlen - pos, | |
2114 | "DEVICE=n%u", net->ifindex); | |
c4e00daa | 2115 | } else { |
798efc60 JP |
2116 | pos++; |
2117 | pos += snprintf(hdr + pos, hdrlen - pos, | |
2118 | "DEVICE=+%s:%s", subsys, dev_name(dev)); | |
c4e00daa | 2119 | } |
af7f2158 | 2120 | |
798efc60 JP |
2121 | return pos; |
2122 | } | |
2123 | EXPORT_SYMBOL(create_syslog_header); | |
2124 | ||
05e4e5b8 JP |
2125 | int dev_vprintk_emit(int level, const struct device *dev, |
2126 | const char *fmt, va_list args) | |
2127 | { | |
2128 | char hdr[128]; | |
2129 | size_t hdrlen; | |
2130 | ||
2131 | hdrlen = create_syslog_header(dev, hdr, sizeof(hdr)); | |
2132 | ||
2133 | return vprintk_emit(0, level, hdrlen ? hdr : NULL, hdrlen, fmt, args); | |
2134 | } | |
2135 | EXPORT_SYMBOL(dev_vprintk_emit); | |
2136 | ||
2137 | int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...) | |
2138 | { | |
2139 | va_list args; | |
2140 | int r; | |
2141 | ||
2142 | va_start(args, fmt); | |
2143 | ||
2144 | r = dev_vprintk_emit(level, dev, fmt, args); | |
2145 | ||
2146 | va_end(args); | |
2147 | ||
2148 | return r; | |
2149 | } | |
2150 | EXPORT_SYMBOL(dev_printk_emit); | |
2151 | ||
798efc60 JP |
2152 | static int __dev_printk(const char *level, const struct device *dev, |
2153 | struct va_format *vaf) | |
2154 | { | |
798efc60 JP |
2155 | if (!dev) |
2156 | return printk("%s(NULL device *): %pV", level, vaf); | |
2157 | ||
666f355f JP |
2158 | return dev_printk_emit(level[1] - '0', dev, |
2159 | "%s %s: %pV", | |
2160 | dev_driver_string(dev), dev_name(dev), vaf); | |
99bcf217 JP |
2161 | } |
2162 | ||
2163 | int dev_printk(const char *level, const struct device *dev, | |
2164 | const char *fmt, ...) | |
2165 | { | |
2166 | struct va_format vaf; | |
2167 | va_list args; | |
2168 | int r; | |
2169 | ||
2170 | va_start(args, fmt); | |
2171 | ||
2172 | vaf.fmt = fmt; | |
2173 | vaf.va = &args; | |
2174 | ||
2175 | r = __dev_printk(level, dev, &vaf); | |
798efc60 | 2176 | |
99bcf217 JP |
2177 | va_end(args); |
2178 | ||
2179 | return r; | |
2180 | } | |
2181 | EXPORT_SYMBOL(dev_printk); | |
2182 | ||
2183 | #define define_dev_printk_level(func, kern_level) \ | |
2184 | int func(const struct device *dev, const char *fmt, ...) \ | |
2185 | { \ | |
2186 | struct va_format vaf; \ | |
2187 | va_list args; \ | |
2188 | int r; \ | |
2189 | \ | |
2190 | va_start(args, fmt); \ | |
2191 | \ | |
2192 | vaf.fmt = fmt; \ | |
2193 | vaf.va = &args; \ | |
2194 | \ | |
2195 | r = __dev_printk(kern_level, dev, &vaf); \ | |
798efc60 | 2196 | \ |
99bcf217 JP |
2197 | va_end(args); \ |
2198 | \ | |
2199 | return r; \ | |
2200 | } \ | |
2201 | EXPORT_SYMBOL(func); | |
2202 | ||
2203 | define_dev_printk_level(dev_emerg, KERN_EMERG); | |
2204 | define_dev_printk_level(dev_alert, KERN_ALERT); | |
2205 | define_dev_printk_level(dev_crit, KERN_CRIT); | |
2206 | define_dev_printk_level(dev_err, KERN_ERR); | |
2207 | define_dev_printk_level(dev_warn, KERN_WARNING); | |
2208 | define_dev_printk_level(dev_notice, KERN_NOTICE); | |
2209 | define_dev_printk_level(_dev_info, KERN_INFO); | |
2210 | ||
2211 | #endif |