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