]>
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> | |
97badf87 | 15 | #include <linux/fwnode.h> |
1da177e4 LT |
16 | #include <linux/init.h> |
17 | #include <linux/module.h> | |
18 | #include <linux/slab.h> | |
19 | #include <linux/string.h> | |
23681e47 | 20 | #include <linux/kdev_t.h> |
116af378 | 21 | #include <linux/notifier.h> |
07d57a32 GL |
22 | #include <linux/of.h> |
23 | #include <linux/of_device.h> | |
da231fd5 | 24 | #include <linux/genhd.h> |
815d2d50 | 25 | #include <linux/kallsyms.h> |
f75b1c60 | 26 | #include <linux/mutex.h> |
af8db150 | 27 | #include <linux/pm_runtime.h> |
c4e00daa | 28 | #include <linux/netdevice.h> |
174cd4b1 | 29 | #include <linux/sched/signal.h> |
63967685 | 30 | #include <linux/sysfs.h> |
1da177e4 LT |
31 | |
32 | #include "base.h" | |
33 | #include "power/power.h" | |
34 | ||
e52eec13 AK |
35 | #ifdef CONFIG_SYSFS_DEPRECATED |
36 | #ifdef CONFIG_SYSFS_DEPRECATED_V2 | |
37 | long sysfs_deprecated = 1; | |
38 | #else | |
39 | long sysfs_deprecated = 0; | |
40 | #endif | |
3454bf96 | 41 | static int __init sysfs_deprecated_setup(char *arg) |
e52eec13 | 42 | { |
34da5e67 | 43 | return kstrtol(arg, 10, &sysfs_deprecated); |
e52eec13 AK |
44 | } |
45 | early_param("sysfs.deprecated", sysfs_deprecated_setup); | |
46 | #endif | |
47 | ||
9ed98953 RW |
48 | /* Device links support. */ |
49 | ||
50 | #ifdef CONFIG_SRCU | |
51 | static DEFINE_MUTEX(device_links_lock); | |
52 | DEFINE_STATIC_SRCU(device_links_srcu); | |
53 | ||
54 | static inline void device_links_write_lock(void) | |
55 | { | |
56 | mutex_lock(&device_links_lock); | |
57 | } | |
58 | ||
59 | static inline void device_links_write_unlock(void) | |
60 | { | |
61 | mutex_unlock(&device_links_lock); | |
62 | } | |
63 | ||
64 | int device_links_read_lock(void) | |
65 | { | |
66 | return srcu_read_lock(&device_links_srcu); | |
67 | } | |
68 | ||
69 | void device_links_read_unlock(int idx) | |
70 | { | |
71 | srcu_read_unlock(&device_links_srcu, idx); | |
72 | } | |
73 | #else /* !CONFIG_SRCU */ | |
74 | static DECLARE_RWSEM(device_links_lock); | |
75 | ||
76 | static inline void device_links_write_lock(void) | |
77 | { | |
78 | down_write(&device_links_lock); | |
79 | } | |
80 | ||
81 | static inline void device_links_write_unlock(void) | |
82 | { | |
83 | up_write(&device_links_lock); | |
84 | } | |
85 | ||
86 | int device_links_read_lock(void) | |
87 | { | |
88 | down_read(&device_links_lock); | |
89 | return 0; | |
90 | } | |
91 | ||
92 | void device_links_read_unlock(int not_used) | |
93 | { | |
94 | up_read(&device_links_lock); | |
95 | } | |
96 | #endif /* !CONFIG_SRCU */ | |
97 | ||
98 | /** | |
99 | * device_is_dependent - Check if one device depends on another one | |
100 | * @dev: Device to check dependencies for. | |
101 | * @target: Device to check against. | |
102 | * | |
103 | * Check if @target depends on @dev or any device dependent on it (its child or | |
104 | * its consumer etc). Return 1 if that is the case or 0 otherwise. | |
105 | */ | |
106 | static int device_is_dependent(struct device *dev, void *target) | |
107 | { | |
108 | struct device_link *link; | |
109 | int ret; | |
110 | ||
111 | if (WARN_ON(dev == target)) | |
112 | return 1; | |
113 | ||
114 | ret = device_for_each_child(dev, target, device_is_dependent); | |
115 | if (ret) | |
116 | return ret; | |
117 | ||
118 | list_for_each_entry(link, &dev->links.consumers, s_node) { | |
119 | if (WARN_ON(link->consumer == target)) | |
120 | return 1; | |
121 | ||
122 | ret = device_is_dependent(link->consumer, target); | |
123 | if (ret) | |
124 | break; | |
125 | } | |
126 | return ret; | |
127 | } | |
128 | ||
129 | static int device_reorder_to_tail(struct device *dev, void *not_used) | |
130 | { | |
131 | struct device_link *link; | |
132 | ||
133 | /* | |
134 | * Devices that have not been registered yet will be put to the ends | |
135 | * of the lists during the registration, so skip them here. | |
136 | */ | |
137 | if (device_is_registered(dev)) | |
138 | devices_kset_move_last(dev); | |
139 | ||
140 | if (device_pm_initialized(dev)) | |
141 | device_pm_move_last(dev); | |
142 | ||
143 | device_for_each_child(dev, NULL, device_reorder_to_tail); | |
144 | list_for_each_entry(link, &dev->links.consumers, s_node) | |
145 | device_reorder_to_tail(link->consumer, NULL); | |
146 | ||
147 | return 0; | |
148 | } | |
149 | ||
150 | /** | |
151 | * device_link_add - Create a link between two devices. | |
152 | * @consumer: Consumer end of the link. | |
153 | * @supplier: Supplier end of the link. | |
154 | * @flags: Link flags. | |
155 | * | |
21d5c57b RW |
156 | * The caller is responsible for the proper synchronization of the link creation |
157 | * with runtime PM. First, setting the DL_FLAG_PM_RUNTIME flag will cause the | |
158 | * runtime PM framework to take the link into account. Second, if the | |
159 | * DL_FLAG_RPM_ACTIVE flag is set in addition to it, the supplier devices will | |
160 | * be forced into the active metastate and reference-counted upon the creation | |
161 | * of the link. If DL_FLAG_PM_RUNTIME is not set, DL_FLAG_RPM_ACTIVE will be | |
162 | * ignored. | |
163 | * | |
9ed98953 RW |
164 | * If the DL_FLAG_AUTOREMOVE is set, the link will be removed automatically |
165 | * when the consumer device driver unbinds from it. The combination of both | |
166 | * DL_FLAG_AUTOREMOVE and DL_FLAG_STATELESS set is invalid and will cause NULL | |
167 | * to be returned. | |
168 | * | |
169 | * A side effect of the link creation is re-ordering of dpm_list and the | |
170 | * devices_kset list by moving the consumer device and all devices depending | |
171 | * on it to the ends of these lists (that does not happen to devices that have | |
172 | * not been registered when this function is called). | |
173 | * | |
174 | * The supplier device is required to be registered when this function is called | |
175 | * and NULL will be returned if that is not the case. The consumer device need | |
64df1148 | 176 | * not be registered, however. |
9ed98953 RW |
177 | */ |
178 | struct device_link *device_link_add(struct device *consumer, | |
179 | struct device *supplier, u32 flags) | |
180 | { | |
181 | struct device_link *link; | |
182 | ||
183 | if (!consumer || !supplier || | |
184 | ((flags & DL_FLAG_STATELESS) && (flags & DL_FLAG_AUTOREMOVE))) | |
185 | return NULL; | |
186 | ||
187 | device_links_write_lock(); | |
188 | device_pm_lock(); | |
189 | ||
190 | /* | |
191 | * If the supplier has not been fully registered yet or there is a | |
192 | * reverse dependency between the consumer and the supplier already in | |
193 | * the graph, return NULL. | |
194 | */ | |
195 | if (!device_pm_initialized(supplier) | |
196 | || device_is_dependent(consumer, supplier)) { | |
197 | link = NULL; | |
198 | goto out; | |
199 | } | |
200 | ||
201 | list_for_each_entry(link, &supplier->links.consumers, s_node) | |
202 | if (link->consumer == consumer) | |
203 | goto out; | |
204 | ||
21d5c57b | 205 | link = kzalloc(sizeof(*link), GFP_KERNEL); |
9ed98953 RW |
206 | if (!link) |
207 | goto out; | |
208 | ||
baa8809f RW |
209 | if (flags & DL_FLAG_PM_RUNTIME) { |
210 | if (flags & DL_FLAG_RPM_ACTIVE) { | |
211 | if (pm_runtime_get_sync(supplier) < 0) { | |
212 | pm_runtime_put_noidle(supplier); | |
213 | kfree(link); | |
214 | link = NULL; | |
215 | goto out; | |
216 | } | |
217 | link->rpm_active = true; | |
21d5c57b | 218 | } |
baa8809f | 219 | pm_runtime_new_link(consumer); |
21d5c57b | 220 | } |
9ed98953 RW |
221 | get_device(supplier); |
222 | link->supplier = supplier; | |
223 | INIT_LIST_HEAD(&link->s_node); | |
224 | get_device(consumer); | |
225 | link->consumer = consumer; | |
226 | INIT_LIST_HEAD(&link->c_node); | |
227 | link->flags = flags; | |
228 | ||
64df1148 | 229 | /* Determine the initial link state. */ |
9ed98953 RW |
230 | if (flags & DL_FLAG_STATELESS) { |
231 | link->status = DL_STATE_NONE; | |
232 | } else { | |
233 | switch (supplier->links.status) { | |
234 | case DL_DEV_DRIVER_BOUND: | |
235 | switch (consumer->links.status) { | |
236 | case DL_DEV_PROBING: | |
21d5c57b RW |
237 | /* |
238 | * Balance the decrementation of the supplier's | |
239 | * runtime PM usage counter after consumer probe | |
240 | * in driver_probe_device(). | |
241 | */ | |
242 | if (flags & DL_FLAG_PM_RUNTIME) | |
243 | pm_runtime_get_sync(supplier); | |
244 | ||
9ed98953 RW |
245 | link->status = DL_STATE_CONSUMER_PROBE; |
246 | break; | |
247 | case DL_DEV_DRIVER_BOUND: | |
248 | link->status = DL_STATE_ACTIVE; | |
249 | break; | |
250 | default: | |
251 | link->status = DL_STATE_AVAILABLE; | |
252 | break; | |
253 | } | |
254 | break; | |
255 | case DL_DEV_UNBINDING: | |
256 | link->status = DL_STATE_SUPPLIER_UNBIND; | |
257 | break; | |
258 | default: | |
259 | link->status = DL_STATE_DORMANT; | |
260 | break; | |
261 | } | |
262 | } | |
263 | ||
264 | /* | |
265 | * Move the consumer and all of the devices depending on it to the end | |
266 | * of dpm_list and the devices_kset list. | |
267 | * | |
268 | * It is necessary to hold dpm_list locked throughout all that or else | |
269 | * we may end up suspending with a wrong ordering of it. | |
270 | */ | |
271 | device_reorder_to_tail(consumer, NULL); | |
272 | ||
273 | list_add_tail_rcu(&link->s_node, &supplier->links.consumers); | |
274 | list_add_tail_rcu(&link->c_node, &consumer->links.suppliers); | |
275 | ||
276 | dev_info(consumer, "Linked as a consumer to %s\n", dev_name(supplier)); | |
277 | ||
278 | out: | |
279 | device_pm_unlock(); | |
280 | device_links_write_unlock(); | |
281 | return link; | |
282 | } | |
283 | EXPORT_SYMBOL_GPL(device_link_add); | |
284 | ||
285 | static void device_link_free(struct device_link *link) | |
286 | { | |
287 | put_device(link->consumer); | |
288 | put_device(link->supplier); | |
289 | kfree(link); | |
290 | } | |
291 | ||
292 | #ifdef CONFIG_SRCU | |
293 | static void __device_link_free_srcu(struct rcu_head *rhead) | |
294 | { | |
295 | device_link_free(container_of(rhead, struct device_link, rcu_head)); | |
296 | } | |
297 | ||
298 | static void __device_link_del(struct device_link *link) | |
299 | { | |
300 | dev_info(link->consumer, "Dropping the link to %s\n", | |
301 | dev_name(link->supplier)); | |
302 | ||
baa8809f RW |
303 | if (link->flags & DL_FLAG_PM_RUNTIME) |
304 | pm_runtime_drop_link(link->consumer); | |
305 | ||
9ed98953 RW |
306 | list_del_rcu(&link->s_node); |
307 | list_del_rcu(&link->c_node); | |
308 | call_srcu(&device_links_srcu, &link->rcu_head, __device_link_free_srcu); | |
309 | } | |
310 | #else /* !CONFIG_SRCU */ | |
311 | static void __device_link_del(struct device_link *link) | |
312 | { | |
313 | dev_info(link->consumer, "Dropping the link to %s\n", | |
314 | dev_name(link->supplier)); | |
315 | ||
03d0bf0f LW |
316 | if (link->flags & DL_FLAG_PM_RUNTIME) |
317 | pm_runtime_drop_link(link->consumer); | |
318 | ||
9ed98953 RW |
319 | list_del(&link->s_node); |
320 | list_del(&link->c_node); | |
321 | device_link_free(link); | |
322 | } | |
323 | #endif /* !CONFIG_SRCU */ | |
324 | ||
325 | /** | |
326 | * device_link_del - Delete a link between two devices. | |
327 | * @link: Device link to delete. | |
328 | * | |
329 | * The caller must ensure proper synchronization of this function with runtime | |
330 | * PM. | |
331 | */ | |
332 | void device_link_del(struct device_link *link) | |
333 | { | |
334 | device_links_write_lock(); | |
335 | device_pm_lock(); | |
336 | __device_link_del(link); | |
337 | device_pm_unlock(); | |
338 | device_links_write_unlock(); | |
339 | } | |
340 | EXPORT_SYMBOL_GPL(device_link_del); | |
341 | ||
342 | static void device_links_missing_supplier(struct device *dev) | |
343 | { | |
344 | struct device_link *link; | |
345 | ||
346 | list_for_each_entry(link, &dev->links.suppliers, c_node) | |
347 | if (link->status == DL_STATE_CONSUMER_PROBE) | |
348 | WRITE_ONCE(link->status, DL_STATE_AVAILABLE); | |
349 | } | |
350 | ||
351 | /** | |
352 | * device_links_check_suppliers - Check presence of supplier drivers. | |
353 | * @dev: Consumer device. | |
354 | * | |
355 | * Check links from this device to any suppliers. Walk the list of the device's | |
356 | * links to suppliers and see if all of them are available. If not, simply | |
357 | * return -EPROBE_DEFER. | |
358 | * | |
359 | * We need to guarantee that the supplier will not go away after the check has | |
360 | * been positive here. It only can go away in __device_release_driver() and | |
361 | * that function checks the device's links to consumers. This means we need to | |
362 | * mark the link as "consumer probe in progress" to make the supplier removal | |
363 | * wait for us to complete (or bad things may happen). | |
364 | * | |
365 | * Links with the DL_FLAG_STATELESS flag set are ignored. | |
366 | */ | |
367 | int device_links_check_suppliers(struct device *dev) | |
368 | { | |
369 | struct device_link *link; | |
370 | int ret = 0; | |
371 | ||
372 | device_links_write_lock(); | |
373 | ||
374 | list_for_each_entry(link, &dev->links.suppliers, c_node) { | |
375 | if (link->flags & DL_FLAG_STATELESS) | |
376 | continue; | |
377 | ||
378 | if (link->status != DL_STATE_AVAILABLE) { | |
379 | device_links_missing_supplier(dev); | |
380 | ret = -EPROBE_DEFER; | |
381 | break; | |
382 | } | |
383 | WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE); | |
384 | } | |
385 | dev->links.status = DL_DEV_PROBING; | |
386 | ||
387 | device_links_write_unlock(); | |
388 | return ret; | |
389 | } | |
390 | ||
391 | /** | |
392 | * device_links_driver_bound - Update device links after probing its driver. | |
393 | * @dev: Device to update the links for. | |
394 | * | |
395 | * The probe has been successful, so update links from this device to any | |
396 | * consumers by changing their status to "available". | |
397 | * | |
398 | * Also change the status of @dev's links to suppliers to "active". | |
399 | * | |
400 | * Links with the DL_FLAG_STATELESS flag set are ignored. | |
401 | */ | |
402 | void device_links_driver_bound(struct device *dev) | |
403 | { | |
404 | struct device_link *link; | |
405 | ||
406 | device_links_write_lock(); | |
407 | ||
408 | list_for_each_entry(link, &dev->links.consumers, s_node) { | |
409 | if (link->flags & DL_FLAG_STATELESS) | |
410 | continue; | |
411 | ||
412 | WARN_ON(link->status != DL_STATE_DORMANT); | |
413 | WRITE_ONCE(link->status, DL_STATE_AVAILABLE); | |
414 | } | |
415 | ||
416 | list_for_each_entry(link, &dev->links.suppliers, c_node) { | |
417 | if (link->flags & DL_FLAG_STATELESS) | |
418 | continue; | |
419 | ||
420 | WARN_ON(link->status != DL_STATE_CONSUMER_PROBE); | |
421 | WRITE_ONCE(link->status, DL_STATE_ACTIVE); | |
422 | } | |
423 | ||
424 | dev->links.status = DL_DEV_DRIVER_BOUND; | |
425 | ||
426 | device_links_write_unlock(); | |
427 | } | |
428 | ||
429 | /** | |
430 | * __device_links_no_driver - Update links of a device without a driver. | |
431 | * @dev: Device without a drvier. | |
432 | * | |
433 | * Delete all non-persistent links from this device to any suppliers. | |
434 | * | |
435 | * Persistent links stay around, but their status is changed to "available", | |
436 | * unless they already are in the "supplier unbind in progress" state in which | |
437 | * case they need not be updated. | |
438 | * | |
439 | * Links with the DL_FLAG_STATELESS flag set are ignored. | |
440 | */ | |
441 | static void __device_links_no_driver(struct device *dev) | |
442 | { | |
443 | struct device_link *link, *ln; | |
444 | ||
445 | list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) { | |
446 | if (link->flags & DL_FLAG_STATELESS) | |
447 | continue; | |
448 | ||
449 | if (link->flags & DL_FLAG_AUTOREMOVE) | |
450 | __device_link_del(link); | |
451 | else if (link->status != DL_STATE_SUPPLIER_UNBIND) | |
452 | WRITE_ONCE(link->status, DL_STATE_AVAILABLE); | |
453 | } | |
454 | ||
455 | dev->links.status = DL_DEV_NO_DRIVER; | |
456 | } | |
457 | ||
458 | void device_links_no_driver(struct device *dev) | |
459 | { | |
460 | device_links_write_lock(); | |
461 | __device_links_no_driver(dev); | |
462 | device_links_write_unlock(); | |
463 | } | |
464 | ||
465 | /** | |
466 | * device_links_driver_cleanup - Update links after driver removal. | |
467 | * @dev: Device whose driver has just gone away. | |
468 | * | |
469 | * Update links to consumers for @dev by changing their status to "dormant" and | |
470 | * invoke %__device_links_no_driver() to update links to suppliers for it as | |
471 | * appropriate. | |
472 | * | |
473 | * Links with the DL_FLAG_STATELESS flag set are ignored. | |
474 | */ | |
475 | void device_links_driver_cleanup(struct device *dev) | |
476 | { | |
477 | struct device_link *link; | |
478 | ||
479 | device_links_write_lock(); | |
480 | ||
481 | list_for_each_entry(link, &dev->links.consumers, s_node) { | |
482 | if (link->flags & DL_FLAG_STATELESS) | |
483 | continue; | |
484 | ||
485 | WARN_ON(link->flags & DL_FLAG_AUTOREMOVE); | |
486 | WARN_ON(link->status != DL_STATE_SUPPLIER_UNBIND); | |
487 | WRITE_ONCE(link->status, DL_STATE_DORMANT); | |
488 | } | |
489 | ||
490 | __device_links_no_driver(dev); | |
491 | ||
492 | device_links_write_unlock(); | |
493 | } | |
494 | ||
495 | /** | |
496 | * device_links_busy - Check if there are any busy links to consumers. | |
497 | * @dev: Device to check. | |
498 | * | |
499 | * Check each consumer of the device and return 'true' if its link's status | |
500 | * is one of "consumer probe" or "active" (meaning that the given consumer is | |
501 | * probing right now or its driver is present). Otherwise, change the link | |
502 | * state to "supplier unbind" to prevent the consumer from being probed | |
503 | * successfully going forward. | |
504 | * | |
505 | * Return 'false' if there are no probing or active consumers. | |
506 | * | |
507 | * Links with the DL_FLAG_STATELESS flag set are ignored. | |
508 | */ | |
509 | bool device_links_busy(struct device *dev) | |
510 | { | |
511 | struct device_link *link; | |
512 | bool ret = false; | |
513 | ||
514 | device_links_write_lock(); | |
515 | ||
516 | list_for_each_entry(link, &dev->links.consumers, s_node) { | |
517 | if (link->flags & DL_FLAG_STATELESS) | |
518 | continue; | |
519 | ||
520 | if (link->status == DL_STATE_CONSUMER_PROBE | |
521 | || link->status == DL_STATE_ACTIVE) { | |
522 | ret = true; | |
523 | break; | |
524 | } | |
525 | WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND); | |
526 | } | |
527 | ||
528 | dev->links.status = DL_DEV_UNBINDING; | |
529 | ||
530 | device_links_write_unlock(); | |
531 | return ret; | |
532 | } | |
533 | ||
534 | /** | |
535 | * device_links_unbind_consumers - Force unbind consumers of the given device. | |
536 | * @dev: Device to unbind the consumers of. | |
537 | * | |
538 | * Walk the list of links to consumers for @dev and if any of them is in the | |
539 | * "consumer probe" state, wait for all device probes in progress to complete | |
540 | * and start over. | |
541 | * | |
542 | * If that's not the case, change the status of the link to "supplier unbind" | |
543 | * and check if the link was in the "active" state. If so, force the consumer | |
544 | * driver to unbind and start over (the consumer will not re-probe as we have | |
545 | * changed the state of the link already). | |
546 | * | |
547 | * Links with the DL_FLAG_STATELESS flag set are ignored. | |
548 | */ | |
549 | void device_links_unbind_consumers(struct device *dev) | |
550 | { | |
551 | struct device_link *link; | |
552 | ||
553 | start: | |
554 | device_links_write_lock(); | |
555 | ||
556 | list_for_each_entry(link, &dev->links.consumers, s_node) { | |
557 | enum device_link_state status; | |
558 | ||
559 | if (link->flags & DL_FLAG_STATELESS) | |
560 | continue; | |
561 | ||
562 | status = link->status; | |
563 | if (status == DL_STATE_CONSUMER_PROBE) { | |
564 | device_links_write_unlock(); | |
565 | ||
566 | wait_for_device_probe(); | |
567 | goto start; | |
568 | } | |
569 | WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND); | |
570 | if (status == DL_STATE_ACTIVE) { | |
571 | struct device *consumer = link->consumer; | |
572 | ||
573 | get_device(consumer); | |
574 | ||
575 | device_links_write_unlock(); | |
576 | ||
577 | device_release_driver_internal(consumer, NULL, | |
578 | consumer->parent); | |
579 | put_device(consumer); | |
580 | goto start; | |
581 | } | |
582 | } | |
583 | ||
584 | device_links_write_unlock(); | |
585 | } | |
586 | ||
587 | /** | |
588 | * device_links_purge - Delete existing links to other devices. | |
589 | * @dev: Target device. | |
590 | */ | |
591 | static void device_links_purge(struct device *dev) | |
592 | { | |
593 | struct device_link *link, *ln; | |
594 | ||
595 | /* | |
596 | * Delete all of the remaining links from this device to any other | |
597 | * devices (either consumers or suppliers). | |
598 | */ | |
599 | device_links_write_lock(); | |
600 | ||
601 | list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) { | |
602 | WARN_ON(link->status == DL_STATE_ACTIVE); | |
603 | __device_link_del(link); | |
604 | } | |
605 | ||
606 | list_for_each_entry_safe_reverse(link, ln, &dev->links.consumers, s_node) { | |
607 | WARN_ON(link->status != DL_STATE_DORMANT && | |
608 | link->status != DL_STATE_NONE); | |
609 | __device_link_del(link); | |
610 | } | |
611 | ||
612 | device_links_write_unlock(); | |
613 | } | |
614 | ||
615 | /* Device links support end. */ | |
616 | ||
4a3ad20c GKH |
617 | int (*platform_notify)(struct device *dev) = NULL; |
618 | int (*platform_notify_remove)(struct device *dev) = NULL; | |
e105b8bf DW |
619 | static struct kobject *dev_kobj; |
620 | struct kobject *sysfs_dev_char_kobj; | |
621 | struct kobject *sysfs_dev_block_kobj; | |
1da177e4 | 622 | |
5e33bc41 RW |
623 | static DEFINE_MUTEX(device_hotplug_lock); |
624 | ||
625 | void lock_device_hotplug(void) | |
626 | { | |
627 | mutex_lock(&device_hotplug_lock); | |
628 | } | |
629 | ||
630 | void unlock_device_hotplug(void) | |
631 | { | |
632 | mutex_unlock(&device_hotplug_lock); | |
633 | } | |
634 | ||
635 | int lock_device_hotplug_sysfs(void) | |
636 | { | |
637 | if (mutex_trylock(&device_hotplug_lock)) | |
638 | return 0; | |
639 | ||
640 | /* Avoid busy looping (5 ms of sleep should do). */ | |
641 | msleep(5); | |
642 | return restart_syscall(); | |
643 | } | |
644 | ||
4e886c29 GKH |
645 | #ifdef CONFIG_BLOCK |
646 | static inline int device_is_not_partition(struct device *dev) | |
647 | { | |
648 | return !(dev->type == &part_type); | |
649 | } | |
650 | #else | |
651 | static inline int device_is_not_partition(struct device *dev) | |
652 | { | |
653 | return 1; | |
654 | } | |
655 | #endif | |
1da177e4 | 656 | |
3e95637a AS |
657 | /** |
658 | * dev_driver_string - Return a device's driver name, if at all possible | |
659 | * @dev: struct device to get the name of | |
660 | * | |
661 | * Will return the device's driver's name if it is bound to a device. If | |
9169c012 | 662 | * the device is not bound to a driver, it will return the name of the bus |
3e95637a AS |
663 | * it is attached to. If it is not attached to a bus either, an empty |
664 | * string will be returned. | |
665 | */ | |
bf9ca69f | 666 | const char *dev_driver_string(const struct device *dev) |
3e95637a | 667 | { |
3589972e AS |
668 | struct device_driver *drv; |
669 | ||
670 | /* dev->driver can change to NULL underneath us because of unbinding, | |
671 | * so be careful about accessing it. dev->bus and dev->class should | |
672 | * never change once they are set, so they don't need special care. | |
673 | */ | |
6aa7de05 | 674 | drv = READ_ONCE(dev->driver); |
3589972e | 675 | return drv ? drv->name : |
a456b702 JD |
676 | (dev->bus ? dev->bus->name : |
677 | (dev->class ? dev->class->name : "")); | |
3e95637a | 678 | } |
310a922d | 679 | EXPORT_SYMBOL(dev_driver_string); |
3e95637a | 680 | |
1da177e4 LT |
681 | #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr) |
682 | ||
4a3ad20c GKH |
683 | static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr, |
684 | char *buf) | |
1da177e4 | 685 | { |
4a3ad20c | 686 | struct device_attribute *dev_attr = to_dev_attr(attr); |
b0d1f807 | 687 | struct device *dev = kobj_to_dev(kobj); |
4a0c20bf | 688 | ssize_t ret = -EIO; |
1da177e4 LT |
689 | |
690 | if (dev_attr->show) | |
54b6f35c | 691 | ret = dev_attr->show(dev, dev_attr, buf); |
815d2d50 | 692 | if (ret >= (ssize_t)PAGE_SIZE) { |
53a9c87e GKH |
693 | print_symbol("dev_attr_show: %s returned bad count\n", |
694 | (unsigned long)dev_attr->show); | |
815d2d50 | 695 | } |
1da177e4 LT |
696 | return ret; |
697 | } | |
698 | ||
4a3ad20c GKH |
699 | static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr, |
700 | const char *buf, size_t count) | |
1da177e4 | 701 | { |
4a3ad20c | 702 | struct device_attribute *dev_attr = to_dev_attr(attr); |
b0d1f807 | 703 | struct device *dev = kobj_to_dev(kobj); |
4a0c20bf | 704 | ssize_t ret = -EIO; |
1da177e4 LT |
705 | |
706 | if (dev_attr->store) | |
54b6f35c | 707 | ret = dev_attr->store(dev, dev_attr, buf, count); |
1da177e4 LT |
708 | return ret; |
709 | } | |
710 | ||
52cf25d0 | 711 | static const struct sysfs_ops dev_sysfs_ops = { |
1da177e4 LT |
712 | .show = dev_attr_show, |
713 | .store = dev_attr_store, | |
714 | }; | |
715 | ||
ca22e56d KS |
716 | #define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr) |
717 | ||
718 | ssize_t device_store_ulong(struct device *dev, | |
719 | struct device_attribute *attr, | |
720 | const char *buf, size_t size) | |
721 | { | |
722 | struct dev_ext_attribute *ea = to_ext_attr(attr); | |
723 | char *end; | |
724 | unsigned long new = simple_strtoul(buf, &end, 0); | |
725 | if (end == buf) | |
726 | return -EINVAL; | |
727 | *(unsigned long *)(ea->var) = new; | |
728 | /* Always return full write size even if we didn't consume all */ | |
729 | return size; | |
730 | } | |
731 | EXPORT_SYMBOL_GPL(device_store_ulong); | |
732 | ||
733 | ssize_t device_show_ulong(struct device *dev, | |
734 | struct device_attribute *attr, | |
735 | char *buf) | |
736 | { | |
737 | struct dev_ext_attribute *ea = to_ext_attr(attr); | |
738 | return snprintf(buf, PAGE_SIZE, "%lx\n", *(unsigned long *)(ea->var)); | |
739 | } | |
740 | EXPORT_SYMBOL_GPL(device_show_ulong); | |
741 | ||
742 | ssize_t device_store_int(struct device *dev, | |
743 | struct device_attribute *attr, | |
744 | const char *buf, size_t size) | |
745 | { | |
746 | struct dev_ext_attribute *ea = to_ext_attr(attr); | |
747 | char *end; | |
748 | long new = simple_strtol(buf, &end, 0); | |
749 | if (end == buf || new > INT_MAX || new < INT_MIN) | |
750 | return -EINVAL; | |
751 | *(int *)(ea->var) = new; | |
752 | /* Always return full write size even if we didn't consume all */ | |
753 | return size; | |
754 | } | |
755 | EXPORT_SYMBOL_GPL(device_store_int); | |
756 | ||
757 | ssize_t device_show_int(struct device *dev, | |
758 | struct device_attribute *attr, | |
759 | char *buf) | |
760 | { | |
761 | struct dev_ext_attribute *ea = to_ext_attr(attr); | |
762 | ||
763 | return snprintf(buf, PAGE_SIZE, "%d\n", *(int *)(ea->var)); | |
764 | } | |
765 | EXPORT_SYMBOL_GPL(device_show_int); | |
1da177e4 | 766 | |
91872392 BP |
767 | ssize_t device_store_bool(struct device *dev, struct device_attribute *attr, |
768 | const char *buf, size_t size) | |
769 | { | |
770 | struct dev_ext_attribute *ea = to_ext_attr(attr); | |
771 | ||
772 | if (strtobool(buf, ea->var) < 0) | |
773 | return -EINVAL; | |
774 | ||
775 | return size; | |
776 | } | |
777 | EXPORT_SYMBOL_GPL(device_store_bool); | |
778 | ||
779 | ssize_t device_show_bool(struct device *dev, struct device_attribute *attr, | |
780 | char *buf) | |
781 | { | |
782 | struct dev_ext_attribute *ea = to_ext_attr(attr); | |
783 | ||
784 | return snprintf(buf, PAGE_SIZE, "%d\n", *(bool *)(ea->var)); | |
785 | } | |
786 | EXPORT_SYMBOL_GPL(device_show_bool); | |
787 | ||
1da177e4 | 788 | /** |
f8878dcb RD |
789 | * device_release - free device structure. |
790 | * @kobj: device's kobject. | |
1da177e4 | 791 | * |
f8878dcb RD |
792 | * This is called once the reference count for the object |
793 | * reaches 0. We forward the call to the device's release | |
794 | * method, which should handle actually freeing the structure. | |
1da177e4 | 795 | */ |
4a3ad20c | 796 | static void device_release(struct kobject *kobj) |
1da177e4 | 797 | { |
b0d1f807 | 798 | struct device *dev = kobj_to_dev(kobj); |
fb069a5d | 799 | struct device_private *p = dev->p; |
1da177e4 | 800 | |
a525a3dd ML |
801 | /* |
802 | * Some platform devices are driven without driver attached | |
803 | * and managed resources may have been acquired. Make sure | |
804 | * all resources are released. | |
805 | * | |
806 | * Drivers still can add resources into device after device | |
807 | * is deleted but alive, so release devres here to avoid | |
808 | * possible memory leak. | |
809 | */ | |
810 | devres_release_all(dev); | |
811 | ||
1da177e4 LT |
812 | if (dev->release) |
813 | dev->release(dev); | |
f9f852df KS |
814 | else if (dev->type && dev->type->release) |
815 | dev->type->release(dev); | |
2620efef GKH |
816 | else if (dev->class && dev->class->dev_release) |
817 | dev->class->dev_release(dev); | |
f810a5cf AV |
818 | else |
819 | WARN(1, KERN_ERR "Device '%s' does not have a release() " | |
4a3ad20c | 820 | "function, it is broken and must be fixed.\n", |
1e0b2cf9 | 821 | dev_name(dev)); |
fb069a5d | 822 | kfree(p); |
1da177e4 LT |
823 | } |
824 | ||
bc451f20 EB |
825 | static const void *device_namespace(struct kobject *kobj) |
826 | { | |
b0d1f807 | 827 | struct device *dev = kobj_to_dev(kobj); |
bc451f20 EB |
828 | const void *ns = NULL; |
829 | ||
830 | if (dev->class && dev->class->ns_type) | |
831 | ns = dev->class->namespace(dev); | |
832 | ||
833 | return ns; | |
834 | } | |
835 | ||
1fd3d7b2 DT |
836 | static void device_get_ownership(struct kobject *kobj, kuid_t *uid, kgid_t *gid) |
837 | { | |
838 | struct device *dev = kobj_to_dev(kobj); | |
839 | ||
840 | if (dev->class && dev->class->get_ownership) | |
841 | dev->class->get_ownership(dev, uid, gid); | |
842 | } | |
843 | ||
8f4afc41 | 844 | static struct kobj_type device_ktype = { |
1da177e4 LT |
845 | .release = device_release, |
846 | .sysfs_ops = &dev_sysfs_ops, | |
bc451f20 | 847 | .namespace = device_namespace, |
1fd3d7b2 | 848 | .get_ownership = device_get_ownership, |
1da177e4 LT |
849 | }; |
850 | ||
851 | ||
312c004d | 852 | static int dev_uevent_filter(struct kset *kset, struct kobject *kobj) |
1da177e4 LT |
853 | { |
854 | struct kobj_type *ktype = get_ktype(kobj); | |
855 | ||
8f4afc41 | 856 | if (ktype == &device_ktype) { |
b0d1f807 | 857 | struct device *dev = kobj_to_dev(kobj); |
1da177e4 LT |
858 | if (dev->bus) |
859 | return 1; | |
23681e47 GKH |
860 | if (dev->class) |
861 | return 1; | |
1da177e4 LT |
862 | } |
863 | return 0; | |
864 | } | |
865 | ||
312c004d | 866 | static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj) |
1da177e4 | 867 | { |
b0d1f807 | 868 | struct device *dev = kobj_to_dev(kobj); |
1da177e4 | 869 | |
23681e47 GKH |
870 | if (dev->bus) |
871 | return dev->bus->name; | |
872 | if (dev->class) | |
873 | return dev->class->name; | |
874 | return NULL; | |
1da177e4 LT |
875 | } |
876 | ||
7eff2e7a KS |
877 | static int dev_uevent(struct kset *kset, struct kobject *kobj, |
878 | struct kobj_uevent_env *env) | |
1da177e4 | 879 | { |
b0d1f807 | 880 | struct device *dev = kobj_to_dev(kobj); |
1da177e4 LT |
881 | int retval = 0; |
882 | ||
6fcf53ac | 883 | /* add device node properties if present */ |
23681e47 | 884 | if (MAJOR(dev->devt)) { |
6fcf53ac KS |
885 | const char *tmp; |
886 | const char *name; | |
2c9ede55 | 887 | umode_t mode = 0; |
4e4098a3 GKH |
888 | kuid_t uid = GLOBAL_ROOT_UID; |
889 | kgid_t gid = GLOBAL_ROOT_GID; | |
6fcf53ac | 890 | |
7eff2e7a KS |
891 | add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt)); |
892 | add_uevent_var(env, "MINOR=%u", MINOR(dev->devt)); | |
3c2670e6 | 893 | name = device_get_devnode(dev, &mode, &uid, &gid, &tmp); |
6fcf53ac KS |
894 | if (name) { |
895 | add_uevent_var(env, "DEVNAME=%s", name); | |
e454cea2 KS |
896 | if (mode) |
897 | add_uevent_var(env, "DEVMODE=%#o", mode & 0777); | |
4e4098a3 GKH |
898 | if (!uid_eq(uid, GLOBAL_ROOT_UID)) |
899 | add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid)); | |
900 | if (!gid_eq(gid, GLOBAL_ROOT_GID)) | |
901 | add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid)); | |
3c2670e6 | 902 | kfree(tmp); |
6fcf53ac | 903 | } |
23681e47 GKH |
904 | } |
905 | ||
414264f9 | 906 | if (dev->type && dev->type->name) |
7eff2e7a | 907 | add_uevent_var(env, "DEVTYPE=%s", dev->type->name); |
414264f9 | 908 | |
239378f1 | 909 | if (dev->driver) |
7eff2e7a | 910 | add_uevent_var(env, "DRIVER=%s", dev->driver->name); |
239378f1 | 911 | |
07d57a32 GL |
912 | /* Add common DT information about the device */ |
913 | of_device_uevent(dev, env); | |
914 | ||
7eff2e7a | 915 | /* have the bus specific function add its stuff */ |
312c004d | 916 | if (dev->bus && dev->bus->uevent) { |
7eff2e7a | 917 | retval = dev->bus->uevent(dev, env); |
f9f852df | 918 | if (retval) |
7dc72b28 | 919 | pr_debug("device: '%s': %s: bus uevent() returned %d\n", |
1e0b2cf9 | 920 | dev_name(dev), __func__, retval); |
1da177e4 LT |
921 | } |
922 | ||
7eff2e7a | 923 | /* have the class specific function add its stuff */ |
2620efef | 924 | if (dev->class && dev->class->dev_uevent) { |
7eff2e7a | 925 | retval = dev->class->dev_uevent(dev, env); |
f9f852df | 926 | if (retval) |
7dc72b28 | 927 | pr_debug("device: '%s': %s: class uevent() " |
1e0b2cf9 | 928 | "returned %d\n", dev_name(dev), |
2b3a302a | 929 | __func__, retval); |
f9f852df KS |
930 | } |
931 | ||
eef35c2d | 932 | /* have the device type specific function add its stuff */ |
f9f852df | 933 | if (dev->type && dev->type->uevent) { |
7eff2e7a | 934 | retval = dev->type->uevent(dev, env); |
f9f852df | 935 | if (retval) |
7dc72b28 | 936 | pr_debug("device: '%s': %s: dev_type uevent() " |
1e0b2cf9 | 937 | "returned %d\n", dev_name(dev), |
2b3a302a | 938 | __func__, retval); |
2620efef GKH |
939 | } |
940 | ||
1da177e4 LT |
941 | return retval; |
942 | } | |
943 | ||
9cd43611 | 944 | static const struct kset_uevent_ops device_uevent_ops = { |
312c004d KS |
945 | .filter = dev_uevent_filter, |
946 | .name = dev_uevent_name, | |
947 | .uevent = dev_uevent, | |
1da177e4 LT |
948 | }; |
949 | ||
c5e064a6 | 950 | static ssize_t uevent_show(struct device *dev, struct device_attribute *attr, |
16574dcc KS |
951 | char *buf) |
952 | { | |
953 | struct kobject *top_kobj; | |
954 | struct kset *kset; | |
7eff2e7a | 955 | struct kobj_uevent_env *env = NULL; |
16574dcc KS |
956 | int i; |
957 | size_t count = 0; | |
958 | int retval; | |
959 | ||
960 | /* search the kset, the device belongs to */ | |
961 | top_kobj = &dev->kobj; | |
5c5daf65 KS |
962 | while (!top_kobj->kset && top_kobj->parent) |
963 | top_kobj = top_kobj->parent; | |
16574dcc KS |
964 | if (!top_kobj->kset) |
965 | goto out; | |
5c5daf65 | 966 | |
16574dcc KS |
967 | kset = top_kobj->kset; |
968 | if (!kset->uevent_ops || !kset->uevent_ops->uevent) | |
969 | goto out; | |
970 | ||
971 | /* respect filter */ | |
972 | if (kset->uevent_ops && kset->uevent_ops->filter) | |
973 | if (!kset->uevent_ops->filter(kset, &dev->kobj)) | |
974 | goto out; | |
975 | ||
7eff2e7a KS |
976 | env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL); |
977 | if (!env) | |
c7308c81 GKH |
978 | return -ENOMEM; |
979 | ||
16574dcc | 980 | /* let the kset specific function add its keys */ |
7eff2e7a | 981 | retval = kset->uevent_ops->uevent(kset, &dev->kobj, env); |
16574dcc KS |
982 | if (retval) |
983 | goto out; | |
984 | ||
985 | /* copy keys to file */ | |
7eff2e7a KS |
986 | for (i = 0; i < env->envp_idx; i++) |
987 | count += sprintf(&buf[count], "%s\n", env->envp[i]); | |
16574dcc | 988 | out: |
7eff2e7a | 989 | kfree(env); |
16574dcc KS |
990 | return count; |
991 | } | |
992 | ||
c5e064a6 | 993 | static ssize_t uevent_store(struct device *dev, struct device_attribute *attr, |
a7fd6706 KS |
994 | const char *buf, size_t count) |
995 | { | |
f36776fa PR |
996 | if (kobject_synth_uevent(&dev->kobj, buf, count)) |
997 | dev_err(dev, "uevent: failed to send synthetic uevent\n"); | |
60a96a59 | 998 | |
a7fd6706 KS |
999 | return count; |
1000 | } | |
c5e064a6 | 1001 | static DEVICE_ATTR_RW(uevent); |
a7fd6706 | 1002 | |
c5e064a6 | 1003 | static ssize_t online_show(struct device *dev, struct device_attribute *attr, |
4f3549d7 RW |
1004 | char *buf) |
1005 | { | |
1006 | bool val; | |
1007 | ||
5e33bc41 | 1008 | device_lock(dev); |
4f3549d7 | 1009 | val = !dev->offline; |
5e33bc41 | 1010 | device_unlock(dev); |
4f3549d7 RW |
1011 | return sprintf(buf, "%u\n", val); |
1012 | } | |
1013 | ||
c5e064a6 | 1014 | static ssize_t online_store(struct device *dev, struct device_attribute *attr, |
4f3549d7 RW |
1015 | const char *buf, size_t count) |
1016 | { | |
1017 | bool val; | |
1018 | int ret; | |
1019 | ||
1020 | ret = strtobool(buf, &val); | |
1021 | if (ret < 0) | |
1022 | return ret; | |
1023 | ||
5e33bc41 RW |
1024 | ret = lock_device_hotplug_sysfs(); |
1025 | if (ret) | |
1026 | return ret; | |
1027 | ||
4f3549d7 RW |
1028 | ret = val ? device_online(dev) : device_offline(dev); |
1029 | unlock_device_hotplug(); | |
1030 | return ret < 0 ? ret : count; | |
1031 | } | |
c5e064a6 | 1032 | static DEVICE_ATTR_RW(online); |
4f3549d7 | 1033 | |
fa6fdb33 | 1034 | int device_add_groups(struct device *dev, const struct attribute_group **groups) |
621a1672 | 1035 | { |
3e9b2bae | 1036 | return sysfs_create_groups(&dev->kobj, groups); |
de0ff00d | 1037 | } |
a7670d42 | 1038 | EXPORT_SYMBOL_GPL(device_add_groups); |
de0ff00d | 1039 | |
fa6fdb33 GKH |
1040 | void device_remove_groups(struct device *dev, |
1041 | const struct attribute_group **groups) | |
de0ff00d | 1042 | { |
3e9b2bae | 1043 | sysfs_remove_groups(&dev->kobj, groups); |
de0ff00d | 1044 | } |
a7670d42 | 1045 | EXPORT_SYMBOL_GPL(device_remove_groups); |
de0ff00d | 1046 | |
57b8ff07 DT |
1047 | union device_attr_group_devres { |
1048 | const struct attribute_group *group; | |
1049 | const struct attribute_group **groups; | |
1050 | }; | |
1051 | ||
1052 | static int devm_attr_group_match(struct device *dev, void *res, void *data) | |
1053 | { | |
1054 | return ((union device_attr_group_devres *)res)->group == data; | |
1055 | } | |
1056 | ||
1057 | static void devm_attr_group_remove(struct device *dev, void *res) | |
1058 | { | |
1059 | union device_attr_group_devres *devres = res; | |
1060 | const struct attribute_group *group = devres->group; | |
1061 | ||
1062 | dev_dbg(dev, "%s: removing group %p\n", __func__, group); | |
1063 | sysfs_remove_group(&dev->kobj, group); | |
1064 | } | |
1065 | ||
1066 | static void devm_attr_groups_remove(struct device *dev, void *res) | |
1067 | { | |
1068 | union device_attr_group_devres *devres = res; | |
1069 | const struct attribute_group **groups = devres->groups; | |
1070 | ||
1071 | dev_dbg(dev, "%s: removing groups %p\n", __func__, groups); | |
1072 | sysfs_remove_groups(&dev->kobj, groups); | |
1073 | } | |
1074 | ||
1075 | /** | |
1076 | * devm_device_add_group - given a device, create a managed attribute group | |
1077 | * @dev: The device to create the group for | |
1078 | * @grp: The attribute group to create | |
1079 | * | |
1080 | * This function creates a group for the first time. It will explicitly | |
1081 | * warn and error if any of the attribute files being created already exist. | |
1082 | * | |
1083 | * Returns 0 on success or error code on failure. | |
1084 | */ | |
1085 | int devm_device_add_group(struct device *dev, const struct attribute_group *grp) | |
1086 | { | |
1087 | union device_attr_group_devres *devres; | |
1088 | int error; | |
1089 | ||
1090 | devres = devres_alloc(devm_attr_group_remove, | |
1091 | sizeof(*devres), GFP_KERNEL); | |
1092 | if (!devres) | |
1093 | return -ENOMEM; | |
1094 | ||
1095 | error = sysfs_create_group(&dev->kobj, grp); | |
1096 | if (error) { | |
1097 | devres_free(devres); | |
1098 | return error; | |
1099 | } | |
1100 | ||
1101 | devres->group = grp; | |
1102 | devres_add(dev, devres); | |
1103 | return 0; | |
1104 | } | |
1105 | EXPORT_SYMBOL_GPL(devm_device_add_group); | |
1106 | ||
1107 | /** | |
1108 | * devm_device_remove_group: remove a managed group from a device | |
1109 | * @dev: device to remove the group from | |
1110 | * @grp: group to remove | |
1111 | * | |
1112 | * This function removes a group of attributes from a device. The attributes | |
1113 | * previously have to have been created for this group, otherwise it will fail. | |
1114 | */ | |
1115 | void devm_device_remove_group(struct device *dev, | |
1116 | const struct attribute_group *grp) | |
1117 | { | |
1118 | WARN_ON(devres_release(dev, devm_attr_group_remove, | |
1119 | devm_attr_group_match, | |
1120 | /* cast away const */ (void *)grp)); | |
1121 | } | |
1122 | EXPORT_SYMBOL_GPL(devm_device_remove_group); | |
1123 | ||
1124 | /** | |
1125 | * devm_device_add_groups - create a bunch of managed attribute groups | |
1126 | * @dev: The device to create the group for | |
1127 | * @groups: The attribute groups to create, NULL terminated | |
1128 | * | |
1129 | * This function creates a bunch of managed attribute groups. If an error | |
1130 | * occurs when creating a group, all previously created groups will be | |
1131 | * removed, unwinding everything back to the original state when this | |
1132 | * function was called. It will explicitly warn and error if any of the | |
1133 | * attribute files being created already exist. | |
1134 | * | |
1135 | * Returns 0 on success or error code from sysfs_create_group on failure. | |
1136 | */ | |
1137 | int devm_device_add_groups(struct device *dev, | |
1138 | const struct attribute_group **groups) | |
1139 | { | |
1140 | union device_attr_group_devres *devres; | |
1141 | int error; | |
1142 | ||
1143 | devres = devres_alloc(devm_attr_groups_remove, | |
1144 | sizeof(*devres), GFP_KERNEL); | |
1145 | if (!devres) | |
1146 | return -ENOMEM; | |
1147 | ||
1148 | error = sysfs_create_groups(&dev->kobj, groups); | |
1149 | if (error) { | |
1150 | devres_free(devres); | |
1151 | return error; | |
1152 | } | |
1153 | ||
1154 | devres->groups = groups; | |
1155 | devres_add(dev, devres); | |
1156 | return 0; | |
1157 | } | |
1158 | EXPORT_SYMBOL_GPL(devm_device_add_groups); | |
1159 | ||
1160 | /** | |
1161 | * devm_device_remove_groups - remove a list of managed groups | |
1162 | * | |
1163 | * @dev: The device for the groups to be removed from | |
1164 | * @groups: NULL terminated list of groups to be removed | |
1165 | * | |
1166 | * If groups is not NULL, remove the specified groups from the device. | |
1167 | */ | |
1168 | void devm_device_remove_groups(struct device *dev, | |
1169 | const struct attribute_group **groups) | |
1170 | { | |
1171 | WARN_ON(devres_release(dev, devm_attr_groups_remove, | |
1172 | devm_attr_group_match, | |
1173 | /* cast away const */ (void *)groups)); | |
1174 | } | |
1175 | EXPORT_SYMBOL_GPL(devm_device_remove_groups); | |
de0ff00d | 1176 | |
2620efef GKH |
1177 | static int device_add_attrs(struct device *dev) |
1178 | { | |
1179 | struct class *class = dev->class; | |
aed65af1 | 1180 | const struct device_type *type = dev->type; |
621a1672 | 1181 | int error; |
2620efef | 1182 | |
621a1672 | 1183 | if (class) { |
d05a6f96 | 1184 | error = device_add_groups(dev, class->dev_groups); |
f9f852df | 1185 | if (error) |
621a1672 | 1186 | return error; |
2620efef | 1187 | } |
f9f852df | 1188 | |
621a1672 DT |
1189 | if (type) { |
1190 | error = device_add_groups(dev, type->groups); | |
f9f852df | 1191 | if (error) |
a6b01ded | 1192 | goto err_remove_class_groups; |
f9f852df KS |
1193 | } |
1194 | ||
621a1672 DT |
1195 | error = device_add_groups(dev, dev->groups); |
1196 | if (error) | |
1197 | goto err_remove_type_groups; | |
1198 | ||
4f3549d7 | 1199 | if (device_supports_offline(dev) && !dev->offline_disabled) { |
c5e064a6 | 1200 | error = device_create_file(dev, &dev_attr_online); |
4f3549d7 | 1201 | if (error) |
ecfbf6fd | 1202 | goto err_remove_dev_groups; |
4f3549d7 RW |
1203 | } |
1204 | ||
621a1672 DT |
1205 | return 0; |
1206 | ||
ecfbf6fd RW |
1207 | err_remove_dev_groups: |
1208 | device_remove_groups(dev, dev->groups); | |
621a1672 DT |
1209 | err_remove_type_groups: |
1210 | if (type) | |
1211 | device_remove_groups(dev, type->groups); | |
d05a6f96 GKH |
1212 | err_remove_class_groups: |
1213 | if (class) | |
1214 | device_remove_groups(dev, class->dev_groups); | |
621a1672 | 1215 | |
2620efef GKH |
1216 | return error; |
1217 | } | |
1218 | ||
1219 | static void device_remove_attrs(struct device *dev) | |
1220 | { | |
1221 | struct class *class = dev->class; | |
aed65af1 | 1222 | const struct device_type *type = dev->type; |
2620efef | 1223 | |
c5e064a6 | 1224 | device_remove_file(dev, &dev_attr_online); |
621a1672 | 1225 | device_remove_groups(dev, dev->groups); |
f9f852df | 1226 | |
621a1672 DT |
1227 | if (type) |
1228 | device_remove_groups(dev, type->groups); | |
1229 | ||
a6b01ded | 1230 | if (class) |
d05a6f96 | 1231 | device_remove_groups(dev, class->dev_groups); |
2620efef GKH |
1232 | } |
1233 | ||
c5e064a6 | 1234 | static ssize_t dev_show(struct device *dev, struct device_attribute *attr, |
23681e47 GKH |
1235 | char *buf) |
1236 | { | |
1237 | return print_dev_t(buf, dev->devt); | |
1238 | } | |
c5e064a6 | 1239 | static DEVICE_ATTR_RO(dev); |
ad6a1e1c | 1240 | |
ca22e56d | 1241 | /* /sys/devices/ */ |
881c6cfd | 1242 | struct kset *devices_kset; |
1da177e4 | 1243 | |
52cdbdd4 GS |
1244 | /** |
1245 | * devices_kset_move_before - Move device in the devices_kset's list. | |
1246 | * @deva: Device to move. | |
1247 | * @devb: Device @deva should come before. | |
1248 | */ | |
1249 | static void devices_kset_move_before(struct device *deva, struct device *devb) | |
1250 | { | |
1251 | if (!devices_kset) | |
1252 | return; | |
1253 | pr_debug("devices_kset: Moving %s before %s\n", | |
1254 | dev_name(deva), dev_name(devb)); | |
1255 | spin_lock(&devices_kset->list_lock); | |
1256 | list_move_tail(&deva->kobj.entry, &devb->kobj.entry); | |
1257 | spin_unlock(&devices_kset->list_lock); | |
1258 | } | |
1259 | ||
1260 | /** | |
1261 | * devices_kset_move_after - Move device in the devices_kset's list. | |
1262 | * @deva: Device to move | |
1263 | * @devb: Device @deva should come after. | |
1264 | */ | |
1265 | static void devices_kset_move_after(struct device *deva, struct device *devb) | |
1266 | { | |
1267 | if (!devices_kset) | |
1268 | return; | |
1269 | pr_debug("devices_kset: Moving %s after %s\n", | |
1270 | dev_name(deva), dev_name(devb)); | |
1271 | spin_lock(&devices_kset->list_lock); | |
1272 | list_move(&deva->kobj.entry, &devb->kobj.entry); | |
1273 | spin_unlock(&devices_kset->list_lock); | |
1274 | } | |
1275 | ||
1276 | /** | |
1277 | * devices_kset_move_last - move the device to the end of devices_kset's list. | |
1278 | * @dev: device to move | |
1279 | */ | |
1280 | void devices_kset_move_last(struct device *dev) | |
1281 | { | |
1282 | if (!devices_kset) | |
1283 | return; | |
1284 | pr_debug("devices_kset: Moving %s to end of list\n", dev_name(dev)); | |
1285 | spin_lock(&devices_kset->list_lock); | |
1286 | list_move_tail(&dev->kobj.entry, &devices_kset->list); | |
1287 | spin_unlock(&devices_kset->list_lock); | |
1288 | } | |
1289 | ||
1da177e4 | 1290 | /** |
4a3ad20c GKH |
1291 | * device_create_file - create sysfs attribute file for device. |
1292 | * @dev: device. | |
1293 | * @attr: device attribute descriptor. | |
1da177e4 | 1294 | */ |
26579ab7 PC |
1295 | int device_create_file(struct device *dev, |
1296 | const struct device_attribute *attr) | |
1da177e4 LT |
1297 | { |
1298 | int error = 0; | |
8f46baaa FB |
1299 | |
1300 | if (dev) { | |
1301 | WARN(((attr->attr.mode & S_IWUGO) && !attr->store), | |
97521978 | 1302 | "Attribute %s: write permission without 'store'\n", |
1303 | attr->attr.name); | |
8f46baaa | 1304 | WARN(((attr->attr.mode & S_IRUGO) && !attr->show), |
97521978 | 1305 | "Attribute %s: read permission without 'show'\n", |
1306 | attr->attr.name); | |
1da177e4 | 1307 | error = sysfs_create_file(&dev->kobj, &attr->attr); |
8f46baaa FB |
1308 | } |
1309 | ||
1da177e4 LT |
1310 | return error; |
1311 | } | |
86df2687 | 1312 | EXPORT_SYMBOL_GPL(device_create_file); |
1da177e4 LT |
1313 | |
1314 | /** | |
4a3ad20c GKH |
1315 | * device_remove_file - remove sysfs attribute file. |
1316 | * @dev: device. | |
1317 | * @attr: device attribute descriptor. | |
1da177e4 | 1318 | */ |
26579ab7 PC |
1319 | void device_remove_file(struct device *dev, |
1320 | const struct device_attribute *attr) | |
1da177e4 | 1321 | { |
0c98b19f | 1322 | if (dev) |
1da177e4 | 1323 | sysfs_remove_file(&dev->kobj, &attr->attr); |
1da177e4 | 1324 | } |
86df2687 | 1325 | EXPORT_SYMBOL_GPL(device_remove_file); |
1da177e4 | 1326 | |
6b0afc2a TH |
1327 | /** |
1328 | * device_remove_file_self - remove sysfs attribute file from its own method. | |
1329 | * @dev: device. | |
1330 | * @attr: device attribute descriptor. | |
1331 | * | |
1332 | * See kernfs_remove_self() for details. | |
1333 | */ | |
1334 | bool device_remove_file_self(struct device *dev, | |
1335 | const struct device_attribute *attr) | |
1336 | { | |
1337 | if (dev) | |
1338 | return sysfs_remove_file_self(&dev->kobj, &attr->attr); | |
1339 | else | |
1340 | return false; | |
1341 | } | |
1342 | EXPORT_SYMBOL_GPL(device_remove_file_self); | |
1343 | ||
2589f188 GKH |
1344 | /** |
1345 | * device_create_bin_file - create sysfs binary attribute file for device. | |
1346 | * @dev: device. | |
1347 | * @attr: device binary attribute descriptor. | |
1348 | */ | |
66ecb92b PC |
1349 | int device_create_bin_file(struct device *dev, |
1350 | const struct bin_attribute *attr) | |
2589f188 GKH |
1351 | { |
1352 | int error = -EINVAL; | |
1353 | if (dev) | |
1354 | error = sysfs_create_bin_file(&dev->kobj, attr); | |
1355 | return error; | |
1356 | } | |
1357 | EXPORT_SYMBOL_GPL(device_create_bin_file); | |
1358 | ||
1359 | /** | |
1360 | * device_remove_bin_file - remove sysfs binary attribute file | |
1361 | * @dev: device. | |
1362 | * @attr: device binary attribute descriptor. | |
1363 | */ | |
66ecb92b PC |
1364 | void device_remove_bin_file(struct device *dev, |
1365 | const struct bin_attribute *attr) | |
2589f188 GKH |
1366 | { |
1367 | if (dev) | |
1368 | sysfs_remove_bin_file(&dev->kobj, attr); | |
1369 | } | |
1370 | EXPORT_SYMBOL_GPL(device_remove_bin_file); | |
1371 | ||
34bb61f9 JB |
1372 | static void klist_children_get(struct klist_node *n) |
1373 | { | |
f791b8c8 GKH |
1374 | struct device_private *p = to_device_private_parent(n); |
1375 | struct device *dev = p->device; | |
34bb61f9 JB |
1376 | |
1377 | get_device(dev); | |
1378 | } | |
1379 | ||
1380 | static void klist_children_put(struct klist_node *n) | |
1381 | { | |
f791b8c8 GKH |
1382 | struct device_private *p = to_device_private_parent(n); |
1383 | struct device *dev = p->device; | |
34bb61f9 JB |
1384 | |
1385 | put_device(dev); | |
1386 | } | |
1387 | ||
1da177e4 | 1388 | /** |
4a3ad20c GKH |
1389 | * device_initialize - init device structure. |
1390 | * @dev: device. | |
1da177e4 | 1391 | * |
5739411a CH |
1392 | * This prepares the device for use by other layers by initializing |
1393 | * its fields. | |
4a3ad20c | 1394 | * It is the first half of device_register(), if called by |
5739411a CH |
1395 | * that function, though it can also be called separately, so one |
1396 | * may use @dev's fields. In particular, get_device()/put_device() | |
1397 | * may be used for reference counting of @dev after calling this | |
1398 | * function. | |
1399 | * | |
b10d5efd AS |
1400 | * All fields in @dev must be initialized by the caller to 0, except |
1401 | * for those explicitly set to some other value. The simplest | |
1402 | * approach is to use kzalloc() to allocate the structure containing | |
1403 | * @dev. | |
1404 | * | |
5739411a CH |
1405 | * NOTE: Use put_device() to give up your reference instead of freeing |
1406 | * @dev directly once you have called this function. | |
1da177e4 | 1407 | */ |
1da177e4 LT |
1408 | void device_initialize(struct device *dev) |
1409 | { | |
881c6cfd | 1410 | dev->kobj.kset = devices_kset; |
f9cb074b | 1411 | kobject_init(&dev->kobj, &device_ktype); |
1da177e4 | 1412 | INIT_LIST_HEAD(&dev->dma_pools); |
3142788b | 1413 | mutex_init(&dev->mutex); |
1704f47b | 1414 | lockdep_set_novalidate_class(&dev->mutex); |
9ac7849e TH |
1415 | spin_lock_init(&dev->devres_lock); |
1416 | INIT_LIST_HEAD(&dev->devres_head); | |
3b98aeaf | 1417 | device_pm_init(dev); |
87348136 | 1418 | set_dev_node(dev, -1); |
4a7cc831 JL |
1419 | #ifdef CONFIG_GENERIC_MSI_IRQ |
1420 | INIT_LIST_HEAD(&dev->msi_list); | |
1421 | #endif | |
9ed98953 RW |
1422 | INIT_LIST_HEAD(&dev->links.consumers); |
1423 | INIT_LIST_HEAD(&dev->links.suppliers); | |
1424 | dev->links.status = DL_DEV_NO_DRIVER; | |
1da177e4 | 1425 | } |
86df2687 | 1426 | EXPORT_SYMBOL_GPL(device_initialize); |
1da177e4 | 1427 | |
d73ce004 | 1428 | struct kobject *virtual_device_parent(struct device *dev) |
f0ee61a6 | 1429 | { |
86406245 | 1430 | static struct kobject *virtual_dir = NULL; |
f0ee61a6 | 1431 | |
86406245 | 1432 | if (!virtual_dir) |
4ff6abff | 1433 | virtual_dir = kobject_create_and_add("virtual", |
881c6cfd | 1434 | &devices_kset->kobj); |
f0ee61a6 | 1435 | |
86406245 | 1436 | return virtual_dir; |
f0ee61a6 GKH |
1437 | } |
1438 | ||
bc451f20 EB |
1439 | struct class_dir { |
1440 | struct kobject kobj; | |
1441 | struct class *class; | |
1442 | }; | |
1443 | ||
1444 | #define to_class_dir(obj) container_of(obj, struct class_dir, kobj) | |
1445 | ||
1446 | static void class_dir_release(struct kobject *kobj) | |
1447 | { | |
1448 | struct class_dir *dir = to_class_dir(kobj); | |
1449 | kfree(dir); | |
1450 | } | |
1451 | ||
1452 | static const | |
1453 | struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj) | |
40fa5422 | 1454 | { |
bc451f20 EB |
1455 | struct class_dir *dir = to_class_dir(kobj); |
1456 | return dir->class->ns_type; | |
1457 | } | |
1458 | ||
1459 | static struct kobj_type class_dir_ktype = { | |
1460 | .release = class_dir_release, | |
1461 | .sysfs_ops = &kobj_sysfs_ops, | |
1462 | .child_ns_type = class_dir_child_ns_type | |
1463 | }; | |
1464 | ||
1465 | static struct kobject * | |
1466 | class_dir_create_and_add(struct class *class, struct kobject *parent_kobj) | |
1467 | { | |
1468 | struct class_dir *dir; | |
43968d2f GKH |
1469 | int retval; |
1470 | ||
bc451f20 EB |
1471 | dir = kzalloc(sizeof(*dir), GFP_KERNEL); |
1472 | if (!dir) | |
9de48bc4 | 1473 | return ERR_PTR(-ENOMEM); |
bc451f20 EB |
1474 | |
1475 | dir->class = class; | |
1476 | kobject_init(&dir->kobj, &class_dir_ktype); | |
1477 | ||
6b6e39a6 | 1478 | dir->kobj.kset = &class->p->glue_dirs; |
bc451f20 EB |
1479 | |
1480 | retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name); | |
1481 | if (retval < 0) { | |
1482 | kobject_put(&dir->kobj); | |
9de48bc4 | 1483 | return ERR_PTR(retval); |
bc451f20 EB |
1484 | } |
1485 | return &dir->kobj; | |
1486 | } | |
1487 | ||
e4a60d13 | 1488 | static DEFINE_MUTEX(gdp_mutex); |
bc451f20 EB |
1489 | |
1490 | static struct kobject *get_device_parent(struct device *dev, | |
1491 | struct device *parent) | |
1492 | { | |
86406245 KS |
1493 | if (dev->class) { |
1494 | struct kobject *kobj = NULL; | |
1495 | struct kobject *parent_kobj; | |
1496 | struct kobject *k; | |
1497 | ||
ead454fe | 1498 | #ifdef CONFIG_BLOCK |
39aba963 | 1499 | /* block disks show up in /sys/block */ |
e52eec13 | 1500 | if (sysfs_deprecated && dev->class == &block_class) { |
39aba963 KS |
1501 | if (parent && parent->class == &block_class) |
1502 | return &parent->kobj; | |
6b6e39a6 | 1503 | return &block_class.p->subsys.kobj; |
39aba963 | 1504 | } |
ead454fe | 1505 | #endif |
e52eec13 | 1506 | |
86406245 KS |
1507 | /* |
1508 | * If we have no parent, we live in "virtual". | |
0f4dafc0 KS |
1509 | * Class-devices with a non class-device as parent, live |
1510 | * in a "glue" directory to prevent namespace collisions. | |
86406245 KS |
1511 | */ |
1512 | if (parent == NULL) | |
1513 | parent_kobj = virtual_device_parent(dev); | |
24b1442d | 1514 | else if (parent->class && !dev->class->ns_type) |
86406245 KS |
1515 | return &parent->kobj; |
1516 | else | |
1517 | parent_kobj = &parent->kobj; | |
1518 | ||
77d3d7c1 TH |
1519 | mutex_lock(&gdp_mutex); |
1520 | ||
86406245 | 1521 | /* find our class-directory at the parent and reference it */ |
6b6e39a6 KS |
1522 | spin_lock(&dev->class->p->glue_dirs.list_lock); |
1523 | list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry) | |
86406245 KS |
1524 | if (k->parent == parent_kobj) { |
1525 | kobj = kobject_get(k); | |
1526 | break; | |
1527 | } | |
6b6e39a6 | 1528 | spin_unlock(&dev->class->p->glue_dirs.list_lock); |
77d3d7c1 TH |
1529 | if (kobj) { |
1530 | mutex_unlock(&gdp_mutex); | |
86406245 | 1531 | return kobj; |
77d3d7c1 | 1532 | } |
86406245 KS |
1533 | |
1534 | /* or create a new class-directory at the parent device */ | |
bc451f20 | 1535 | k = class_dir_create_and_add(dev->class, parent_kobj); |
0f4dafc0 | 1536 | /* do not emit an uevent for this simple "glue" directory */ |
77d3d7c1 | 1537 | mutex_unlock(&gdp_mutex); |
43968d2f | 1538 | return k; |
86406245 KS |
1539 | } |
1540 | ||
ca22e56d KS |
1541 | /* subsystems can specify a default root directory for their devices */ |
1542 | if (!parent && dev->bus && dev->bus->dev_root) | |
1543 | return &dev->bus->dev_root->kobj; | |
1544 | ||
86406245 | 1545 | if (parent) |
c744aeae CH |
1546 | return &parent->kobj; |
1547 | return NULL; | |
1548 | } | |
da231fd5 | 1549 | |
cebf8fd1 ML |
1550 | static inline bool live_in_glue_dir(struct kobject *kobj, |
1551 | struct device *dev) | |
1552 | { | |
1553 | if (!kobj || !dev->class || | |
1554 | kobj->kset != &dev->class->p->glue_dirs) | |
1555 | return false; | |
1556 | return true; | |
1557 | } | |
1558 | ||
1559 | static inline struct kobject *get_glue_dir(struct device *dev) | |
1560 | { | |
1561 | return dev->kobj.parent; | |
1562 | } | |
1563 | ||
1564 | /* | |
1565 | * make sure cleaning up dir as the last step, we need to make | |
1566 | * sure .release handler of kobject is run with holding the | |
1567 | * global lock | |
1568 | */ | |
63b6971a | 1569 | static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir) |
da231fd5 | 1570 | { |
0f4dafc0 | 1571 | /* see if we live in a "glue" directory */ |
cebf8fd1 | 1572 | if (!live_in_glue_dir(glue_dir, dev)) |
da231fd5 KS |
1573 | return; |
1574 | ||
e4a60d13 | 1575 | mutex_lock(&gdp_mutex); |
0f4dafc0 | 1576 | kobject_put(glue_dir); |
e4a60d13 | 1577 | mutex_unlock(&gdp_mutex); |
da231fd5 | 1578 | } |
63b6971a | 1579 | |
2ee97caf CH |
1580 | static int device_add_class_symlinks(struct device *dev) |
1581 | { | |
5590f319 | 1582 | struct device_node *of_node = dev_of_node(dev); |
2ee97caf CH |
1583 | int error; |
1584 | ||
5590f319 | 1585 | if (of_node) { |
0c3c234b | 1586 | error = sysfs_create_link(&dev->kobj, of_node_kobj(of_node), "of_node"); |
5590f319 BH |
1587 | if (error) |
1588 | dev_warn(dev, "Error %d creating of_node link\n",error); | |
1589 | /* An error here doesn't warrant bringing down the device */ | |
1590 | } | |
1591 | ||
2ee97caf CH |
1592 | if (!dev->class) |
1593 | return 0; | |
da231fd5 | 1594 | |
1fbfee6c | 1595 | error = sysfs_create_link(&dev->kobj, |
6b6e39a6 | 1596 | &dev->class->p->subsys.kobj, |
2ee97caf CH |
1597 | "subsystem"); |
1598 | if (error) | |
5590f319 | 1599 | goto out_devnode; |
da231fd5 | 1600 | |
4e886c29 | 1601 | if (dev->parent && device_is_not_partition(dev)) { |
39aba963 | 1602 | error = sysfs_create_link(&dev->kobj, &dev->parent->kobj, |
4f01a757 DT |
1603 | "device"); |
1604 | if (error) | |
39aba963 | 1605 | goto out_subsys; |
2ee97caf | 1606 | } |
2ee97caf | 1607 | |
ead454fe | 1608 | #ifdef CONFIG_BLOCK |
39aba963 | 1609 | /* /sys/block has directories and does not need symlinks */ |
e52eec13 | 1610 | if (sysfs_deprecated && dev->class == &block_class) |
39aba963 | 1611 | return 0; |
ead454fe | 1612 | #endif |
39aba963 | 1613 | |
da231fd5 | 1614 | /* link in the class directory pointing to the device */ |
6b6e39a6 | 1615 | error = sysfs_create_link(&dev->class->p->subsys.kobj, |
1e0b2cf9 | 1616 | &dev->kobj, dev_name(dev)); |
da231fd5 | 1617 | if (error) |
39aba963 | 1618 | goto out_device; |
da231fd5 | 1619 | |
da231fd5 KS |
1620 | return 0; |
1621 | ||
39aba963 KS |
1622 | out_device: |
1623 | sysfs_remove_link(&dev->kobj, "device"); | |
da231fd5 | 1624 | |
2ee97caf CH |
1625 | out_subsys: |
1626 | sysfs_remove_link(&dev->kobj, "subsystem"); | |
5590f319 BH |
1627 | out_devnode: |
1628 | sysfs_remove_link(&dev->kobj, "of_node"); | |
2ee97caf CH |
1629 | return error; |
1630 | } | |
1631 | ||
1632 | static void device_remove_class_symlinks(struct device *dev) | |
1633 | { | |
5590f319 BH |
1634 | if (dev_of_node(dev)) |
1635 | sysfs_remove_link(&dev->kobj, "of_node"); | |
1636 | ||
2ee97caf CH |
1637 | if (!dev->class) |
1638 | return; | |
da231fd5 | 1639 | |
4e886c29 | 1640 | if (dev->parent && device_is_not_partition(dev)) |
da231fd5 | 1641 | sysfs_remove_link(&dev->kobj, "device"); |
2ee97caf | 1642 | sysfs_remove_link(&dev->kobj, "subsystem"); |
ead454fe | 1643 | #ifdef CONFIG_BLOCK |
e52eec13 | 1644 | if (sysfs_deprecated && dev->class == &block_class) |
39aba963 | 1645 | return; |
ead454fe | 1646 | #endif |
6b6e39a6 | 1647 | sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev)); |
2ee97caf CH |
1648 | } |
1649 | ||
413c239f SR |
1650 | /** |
1651 | * dev_set_name - set a device name | |
1652 | * @dev: device | |
46232366 | 1653 | * @fmt: format string for the device's name |
413c239f SR |
1654 | */ |
1655 | int dev_set_name(struct device *dev, const char *fmt, ...) | |
1656 | { | |
1657 | va_list vargs; | |
1fa5ae85 | 1658 | int err; |
413c239f SR |
1659 | |
1660 | va_start(vargs, fmt); | |
1fa5ae85 | 1661 | err = kobject_set_name_vargs(&dev->kobj, fmt, vargs); |
413c239f | 1662 | va_end(vargs); |
1fa5ae85 | 1663 | return err; |
413c239f SR |
1664 | } |
1665 | EXPORT_SYMBOL_GPL(dev_set_name); | |
1666 | ||
e105b8bf DW |
1667 | /** |
1668 | * device_to_dev_kobj - select a /sys/dev/ directory for the device | |
1669 | * @dev: device | |
1670 | * | |
1671 | * By default we select char/ for new entries. Setting class->dev_obj | |
1672 | * to NULL prevents an entry from being created. class->dev_kobj must | |
1673 | * be set (or cleared) before any devices are registered to the class | |
1674 | * otherwise device_create_sys_dev_entry() and | |
0d4e293c PK |
1675 | * device_remove_sys_dev_entry() will disagree about the presence of |
1676 | * the link. | |
e105b8bf DW |
1677 | */ |
1678 | static struct kobject *device_to_dev_kobj(struct device *dev) | |
1679 | { | |
1680 | struct kobject *kobj; | |
1681 | ||
1682 | if (dev->class) | |
1683 | kobj = dev->class->dev_kobj; | |
1684 | else | |
1685 | kobj = sysfs_dev_char_kobj; | |
1686 | ||
1687 | return kobj; | |
1688 | } | |
1689 | ||
1690 | static int device_create_sys_dev_entry(struct device *dev) | |
1691 | { | |
1692 | struct kobject *kobj = device_to_dev_kobj(dev); | |
1693 | int error = 0; | |
1694 | char devt_str[15]; | |
1695 | ||
1696 | if (kobj) { | |
1697 | format_dev_t(devt_str, dev->devt); | |
1698 | error = sysfs_create_link(kobj, &dev->kobj, devt_str); | |
1699 | } | |
1700 | ||
1701 | return error; | |
1702 | } | |
1703 | ||
1704 | static void device_remove_sys_dev_entry(struct device *dev) | |
1705 | { | |
1706 | struct kobject *kobj = device_to_dev_kobj(dev); | |
1707 | char devt_str[15]; | |
1708 | ||
1709 | if (kobj) { | |
1710 | format_dev_t(devt_str, dev->devt); | |
1711 | sysfs_remove_link(kobj, devt_str); | |
1712 | } | |
1713 | } | |
1714 | ||
b4028437 GKH |
1715 | int device_private_init(struct device *dev) |
1716 | { | |
1717 | dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL); | |
1718 | if (!dev->p) | |
1719 | return -ENOMEM; | |
1720 | dev->p->device = dev; | |
1721 | klist_init(&dev->p->klist_children, klist_children_get, | |
1722 | klist_children_put); | |
ef8a3fd6 | 1723 | INIT_LIST_HEAD(&dev->p->deferred_probe); |
b4028437 GKH |
1724 | return 0; |
1725 | } | |
1726 | ||
1da177e4 | 1727 | /** |
4a3ad20c GKH |
1728 | * device_add - add device to device hierarchy. |
1729 | * @dev: device. | |
1da177e4 | 1730 | * |
4a3ad20c GKH |
1731 | * This is part 2 of device_register(), though may be called |
1732 | * separately _iff_ device_initialize() has been called separately. | |
1da177e4 | 1733 | * |
5739411a | 1734 | * This adds @dev to the kobject hierarchy via kobject_add(), adds it |
4a3ad20c GKH |
1735 | * to the global and sibling lists for the device, then |
1736 | * adds it to the other relevant subsystems of the driver model. | |
5739411a | 1737 | * |
b10d5efd AS |
1738 | * Do not call this routine or device_register() more than once for |
1739 | * any device structure. The driver model core is not designed to work | |
1740 | * with devices that get unregistered and then spring back to life. | |
1741 | * (Among other things, it's very hard to guarantee that all references | |
1742 | * to the previous incarnation of @dev have been dropped.) Allocate | |
1743 | * and register a fresh new struct device instead. | |
1744 | * | |
5739411a CH |
1745 | * NOTE: _Never_ directly free @dev after calling this function, even |
1746 | * if it returned an error! Always use put_device() to give up your | |
1747 | * reference instead. | |
1da177e4 LT |
1748 | */ |
1749 | int device_add(struct device *dev) | |
1750 | { | |
35dbf4ef | 1751 | struct device *parent; |
ca22e56d | 1752 | struct kobject *kobj; |
c47ed219 | 1753 | struct class_interface *class_intf; |
c906a48a | 1754 | int error = -EINVAL; |
cebf8fd1 | 1755 | struct kobject *glue_dir = NULL; |
775b64d2 | 1756 | |
1da177e4 | 1757 | dev = get_device(dev); |
c906a48a GKH |
1758 | if (!dev) |
1759 | goto done; | |
1760 | ||
fb069a5d | 1761 | if (!dev->p) { |
b4028437 GKH |
1762 | error = device_private_init(dev); |
1763 | if (error) | |
1764 | goto done; | |
fb069a5d | 1765 | } |
fb069a5d | 1766 | |
1fa5ae85 KS |
1767 | /* |
1768 | * for statically allocated devices, which should all be converted | |
1769 | * some day, we need to initialize the name. We prevent reading back | |
1770 | * the name, and force the use of dev_name() | |
1771 | */ | |
1772 | if (dev->init_name) { | |
acc0e90f | 1773 | dev_set_name(dev, "%s", dev->init_name); |
1fa5ae85 KS |
1774 | dev->init_name = NULL; |
1775 | } | |
c906a48a | 1776 | |
ca22e56d KS |
1777 | /* subsystems can specify simple device enumeration */ |
1778 | if (!dev_name(dev) && dev->bus && dev->bus->dev_name) | |
1779 | dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id); | |
1780 | ||
e6309e75 TG |
1781 | if (!dev_name(dev)) { |
1782 | error = -EINVAL; | |
5c8563d7 | 1783 | goto name_error; |
e6309e75 | 1784 | } |
1da177e4 | 1785 | |
1e0b2cf9 | 1786 | pr_debug("device: '%s': %s\n", dev_name(dev), __func__); |
c205ef48 | 1787 | |
1da177e4 | 1788 | parent = get_device(dev->parent); |
ca22e56d | 1789 | kobj = get_device_parent(dev, parent); |
9de48bc4 TH |
1790 | if (IS_ERR(kobj)) { |
1791 | error = PTR_ERR(kobj); | |
1792 | goto parent_error; | |
1793 | } | |
ca22e56d KS |
1794 | if (kobj) |
1795 | dev->kobj.parent = kobj; | |
1da177e4 | 1796 | |
0d358f22 | 1797 | /* use parent numa_node */ |
56f2de81 | 1798 | if (parent && (dev_to_node(dev) == NUMA_NO_NODE)) |
0d358f22 YL |
1799 | set_dev_node(dev, dev_to_node(parent)); |
1800 | ||
1da177e4 | 1801 | /* first, register with generic layer. */ |
8a577ffc KS |
1802 | /* we require the name to be set before, and pass NULL */ |
1803 | error = kobject_add(&dev->kobj, dev->kobj.parent, NULL); | |
cebf8fd1 ML |
1804 | if (error) { |
1805 | glue_dir = get_glue_dir(dev); | |
1da177e4 | 1806 | goto Error; |
cebf8fd1 | 1807 | } |
a7fd6706 | 1808 | |
37022644 BW |
1809 | /* notify platform of device entry */ |
1810 | if (platform_notify) | |
1811 | platform_notify(dev); | |
1812 | ||
c5e064a6 | 1813 | error = device_create_file(dev, &dev_attr_uevent); |
a306eea4 CH |
1814 | if (error) |
1815 | goto attrError; | |
a7fd6706 | 1816 | |
2ee97caf CH |
1817 | error = device_add_class_symlinks(dev); |
1818 | if (error) | |
1819 | goto SymlinkError; | |
dc0afa83 CH |
1820 | error = device_add_attrs(dev); |
1821 | if (error) | |
2620efef | 1822 | goto AttrsError; |
dc0afa83 CH |
1823 | error = bus_add_device(dev); |
1824 | if (error) | |
1da177e4 | 1825 | goto BusError; |
3b98aeaf | 1826 | error = dpm_sysfs_add(dev); |
57eee3d2 | 1827 | if (error) |
3b98aeaf AS |
1828 | goto DPMError; |
1829 | device_pm_add(dev); | |
ec0676ee | 1830 | |
0cd75047 SK |
1831 | if (MAJOR(dev->devt)) { |
1832 | error = device_create_file(dev, &dev_attr_dev); | |
1833 | if (error) | |
1834 | goto DevAttrError; | |
1835 | ||
1836 | error = device_create_sys_dev_entry(dev); | |
1837 | if (error) | |
1838 | goto SysEntryError; | |
1839 | ||
1840 | devtmpfs_create_node(dev); | |
1841 | } | |
1842 | ||
ec0676ee | 1843 | /* Notify clients of device addition. This call must come |
268863f4 | 1844 | * after dpm_sysfs_add() and before kobject_uevent(). |
ec0676ee AS |
1845 | */ |
1846 | if (dev->bus) | |
1847 | blocking_notifier_call_chain(&dev->bus->p->bus_notifier, | |
1848 | BUS_NOTIFY_ADD_DEVICE, dev); | |
1849 | ||
83b5fb4c | 1850 | kobject_uevent(&dev->kobj, KOBJ_ADD); |
2023c610 | 1851 | bus_probe_device(dev); |
1da177e4 | 1852 | if (parent) |
f791b8c8 GKH |
1853 | klist_add_tail(&dev->p->knode_parent, |
1854 | &parent->p->klist_children); | |
1da177e4 | 1855 | |
5d9fd169 | 1856 | if (dev->class) { |
ca22e56d | 1857 | mutex_lock(&dev->class->p->mutex); |
c47ed219 | 1858 | /* tie the class to the device */ |
5a3ceb86 | 1859 | klist_add_tail(&dev->knode_class, |
6b6e39a6 | 1860 | &dev->class->p->klist_devices); |
c47ed219 GKH |
1861 | |
1862 | /* notify any interfaces that the device is here */ | |
184f1f77 | 1863 | list_for_each_entry(class_intf, |
ca22e56d | 1864 | &dev->class->p->interfaces, node) |
c47ed219 GKH |
1865 | if (class_intf->add_dev) |
1866 | class_intf->add_dev(dev, class_intf); | |
ca22e56d | 1867 | mutex_unlock(&dev->class->p->mutex); |
5d9fd169 | 1868 | } |
c906a48a | 1869 | done: |
1da177e4 LT |
1870 | put_device(dev); |
1871 | return error; | |
0cd75047 SK |
1872 | SysEntryError: |
1873 | if (MAJOR(dev->devt)) | |
1874 | device_remove_file(dev, &dev_attr_dev); | |
1875 | DevAttrError: | |
1876 | device_pm_remove(dev); | |
1877 | dpm_sysfs_remove(dev); | |
3b98aeaf | 1878 | DPMError: |
57eee3d2 RW |
1879 | bus_remove_device(dev); |
1880 | BusError: | |
82f0cf9b | 1881 | device_remove_attrs(dev); |
2620efef | 1882 | AttrsError: |
2ee97caf CH |
1883 | device_remove_class_symlinks(dev); |
1884 | SymlinkError: | |
c5e064a6 | 1885 | device_remove_file(dev, &dev_attr_uevent); |
23681e47 | 1886 | attrError: |
312c004d | 1887 | kobject_uevent(&dev->kobj, KOBJ_REMOVE); |
cebf8fd1 | 1888 | glue_dir = get_glue_dir(dev); |
1da177e4 LT |
1889 | kobject_del(&dev->kobj); |
1890 | Error: | |
cebf8fd1 | 1891 | cleanup_glue_dir(dev, glue_dir); |
9de48bc4 | 1892 | parent_error: |
5f0163a5 | 1893 | put_device(parent); |
5c8563d7 KS |
1894 | name_error: |
1895 | kfree(dev->p); | |
1896 | dev->p = NULL; | |
c906a48a | 1897 | goto done; |
1da177e4 | 1898 | } |
86df2687 | 1899 | EXPORT_SYMBOL_GPL(device_add); |
1da177e4 | 1900 | |
1da177e4 | 1901 | /** |
4a3ad20c GKH |
1902 | * device_register - register a device with the system. |
1903 | * @dev: pointer to the device structure | |
1da177e4 | 1904 | * |
4a3ad20c GKH |
1905 | * This happens in two clean steps - initialize the device |
1906 | * and add it to the system. The two steps can be called | |
1907 | * separately, but this is the easiest and most common. | |
1908 | * I.e. you should only call the two helpers separately if | |
1909 | * have a clearly defined need to use and refcount the device | |
1910 | * before it is added to the hierarchy. | |
5739411a | 1911 | * |
b10d5efd AS |
1912 | * For more information, see the kerneldoc for device_initialize() |
1913 | * and device_add(). | |
1914 | * | |
5739411a CH |
1915 | * NOTE: _Never_ directly free @dev after calling this function, even |
1916 | * if it returned an error! Always use put_device() to give up the | |
1917 | * reference initialized in this function instead. | |
1da177e4 | 1918 | */ |
1da177e4 LT |
1919 | int device_register(struct device *dev) |
1920 | { | |
1921 | device_initialize(dev); | |
1922 | return device_add(dev); | |
1923 | } | |
86df2687 | 1924 | EXPORT_SYMBOL_GPL(device_register); |
1da177e4 | 1925 | |
1da177e4 | 1926 | /** |
4a3ad20c GKH |
1927 | * get_device - increment reference count for device. |
1928 | * @dev: device. | |
1da177e4 | 1929 | * |
4a3ad20c GKH |
1930 | * This simply forwards the call to kobject_get(), though |
1931 | * we do take care to provide for the case that we get a NULL | |
1932 | * pointer passed in. | |
1da177e4 | 1933 | */ |
4a3ad20c | 1934 | struct device *get_device(struct device *dev) |
1da177e4 | 1935 | { |
b0d1f807 | 1936 | return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL; |
1da177e4 | 1937 | } |
86df2687 | 1938 | EXPORT_SYMBOL_GPL(get_device); |
1da177e4 | 1939 | |
1da177e4 | 1940 | /** |
4a3ad20c GKH |
1941 | * put_device - decrement reference count. |
1942 | * @dev: device in question. | |
1da177e4 | 1943 | */ |
4a3ad20c | 1944 | void put_device(struct device *dev) |
1da177e4 | 1945 | { |
edfaa7c3 | 1946 | /* might_sleep(); */ |
1da177e4 LT |
1947 | if (dev) |
1948 | kobject_put(&dev->kobj); | |
1949 | } | |
86df2687 | 1950 | EXPORT_SYMBOL_GPL(put_device); |
1da177e4 | 1951 | |
1da177e4 | 1952 | /** |
4a3ad20c GKH |
1953 | * device_del - delete device from system. |
1954 | * @dev: device. | |
1da177e4 | 1955 | * |
4a3ad20c GKH |
1956 | * This is the first part of the device unregistration |
1957 | * sequence. This removes the device from the lists we control | |
1958 | * from here, has it removed from the other driver model | |
1959 | * subsystems it was added to in device_add(), and removes it | |
1960 | * from the kobject hierarchy. | |
1da177e4 | 1961 | * |
4a3ad20c GKH |
1962 | * NOTE: this should be called manually _iff_ device_add() was |
1963 | * also called manually. | |
1da177e4 | 1964 | */ |
4a3ad20c | 1965 | void device_del(struct device *dev) |
1da177e4 | 1966 | { |
4a3ad20c | 1967 | struct device *parent = dev->parent; |
cebf8fd1 | 1968 | struct kobject *glue_dir = NULL; |
c47ed219 | 1969 | struct class_interface *class_intf; |
1da177e4 | 1970 | |
ec0676ee AS |
1971 | /* Notify clients of device removal. This call must come |
1972 | * before dpm_sysfs_remove(). | |
1973 | */ | |
1974 | if (dev->bus) | |
1975 | blocking_notifier_call_chain(&dev->bus->p->bus_notifier, | |
1976 | BUS_NOTIFY_DEL_DEVICE, dev); | |
9ed98953 | 1977 | |
3b98aeaf | 1978 | dpm_sysfs_remove(dev); |
1da177e4 | 1979 | if (parent) |
f791b8c8 | 1980 | klist_del(&dev->p->knode_parent); |
e105b8bf | 1981 | if (MAJOR(dev->devt)) { |
2b2af54a | 1982 | devtmpfs_delete_node(dev); |
e105b8bf | 1983 | device_remove_sys_dev_entry(dev); |
c5e064a6 | 1984 | device_remove_file(dev, &dev_attr_dev); |
e105b8bf | 1985 | } |
b9d9c82b | 1986 | if (dev->class) { |
da231fd5 | 1987 | device_remove_class_symlinks(dev); |
99ef3ef8 | 1988 | |
ca22e56d | 1989 | mutex_lock(&dev->class->p->mutex); |
c47ed219 | 1990 | /* notify any interfaces that the device is now gone */ |
184f1f77 | 1991 | list_for_each_entry(class_intf, |
ca22e56d | 1992 | &dev->class->p->interfaces, node) |
c47ed219 GKH |
1993 | if (class_intf->remove_dev) |
1994 | class_intf->remove_dev(dev, class_intf); | |
1995 | /* remove the device from the class list */ | |
5a3ceb86 | 1996 | klist_del(&dev->knode_class); |
ca22e56d | 1997 | mutex_unlock(&dev->class->p->mutex); |
b9d9c82b | 1998 | } |
c5e064a6 | 1999 | device_remove_file(dev, &dev_attr_uevent); |
2620efef | 2000 | device_remove_attrs(dev); |
28953533 | 2001 | bus_remove_device(dev); |
4b6d1f12 | 2002 | device_pm_remove(dev); |
d1c3414c | 2003 | driver_deferred_probe_del(dev); |
478573c9 | 2004 | device_remove_properties(dev); |
2ec16150 | 2005 | device_links_purge(dev); |
1da177e4 LT |
2006 | |
2007 | /* Notify the platform of the removal, in case they | |
2008 | * need to do anything... | |
2009 | */ | |
2010 | if (platform_notify_remove) | |
2011 | platform_notify_remove(dev); | |
599bad38 JR |
2012 | if (dev->bus) |
2013 | blocking_notifier_call_chain(&dev->bus->p->bus_notifier, | |
2014 | BUS_NOTIFY_REMOVED_DEVICE, dev); | |
312c004d | 2015 | kobject_uevent(&dev->kobj, KOBJ_REMOVE); |
cebf8fd1 | 2016 | glue_dir = get_glue_dir(dev); |
1da177e4 | 2017 | kobject_del(&dev->kobj); |
cebf8fd1 | 2018 | cleanup_glue_dir(dev, glue_dir); |
da231fd5 | 2019 | put_device(parent); |
1da177e4 | 2020 | } |
86df2687 | 2021 | EXPORT_SYMBOL_GPL(device_del); |
1da177e4 LT |
2022 | |
2023 | /** | |
4a3ad20c GKH |
2024 | * device_unregister - unregister device from system. |
2025 | * @dev: device going away. | |
1da177e4 | 2026 | * |
4a3ad20c GKH |
2027 | * We do this in two parts, like we do device_register(). First, |
2028 | * we remove it from all the subsystems with device_del(), then | |
2029 | * we decrement the reference count via put_device(). If that | |
2030 | * is the final reference count, the device will be cleaned up | |
2031 | * via device_release() above. Otherwise, the structure will | |
2032 | * stick around until the final reference to the device is dropped. | |
1da177e4 | 2033 | */ |
4a3ad20c | 2034 | void device_unregister(struct device *dev) |
1da177e4 | 2035 | { |
1e0b2cf9 | 2036 | pr_debug("device: '%s': %s\n", dev_name(dev), __func__); |
1da177e4 LT |
2037 | device_del(dev); |
2038 | put_device(dev); | |
2039 | } | |
86df2687 | 2040 | EXPORT_SYMBOL_GPL(device_unregister); |
1da177e4 | 2041 | |
3d060aeb AS |
2042 | static struct device *prev_device(struct klist_iter *i) |
2043 | { | |
2044 | struct klist_node *n = klist_prev(i); | |
2045 | struct device *dev = NULL; | |
2046 | struct device_private *p; | |
2047 | ||
2048 | if (n) { | |
2049 | p = to_device_private_parent(n); | |
2050 | dev = p->device; | |
2051 | } | |
2052 | return dev; | |
2053 | } | |
2054 | ||
4a3ad20c | 2055 | static struct device *next_device(struct klist_iter *i) |
36239577 | 2056 | { |
4a3ad20c | 2057 | struct klist_node *n = klist_next(i); |
f791b8c8 GKH |
2058 | struct device *dev = NULL; |
2059 | struct device_private *p; | |
2060 | ||
2061 | if (n) { | |
2062 | p = to_device_private_parent(n); | |
2063 | dev = p->device; | |
2064 | } | |
2065 | return dev; | |
36239577 PM |
2066 | } |
2067 | ||
6fcf53ac | 2068 | /** |
e454cea2 | 2069 | * device_get_devnode - path of device node file |
6fcf53ac | 2070 | * @dev: device |
e454cea2 | 2071 | * @mode: returned file access mode |
3c2670e6 KS |
2072 | * @uid: returned file owner |
2073 | * @gid: returned file group | |
6fcf53ac KS |
2074 | * @tmp: possibly allocated string |
2075 | * | |
2076 | * Return the relative path of a possible device node. | |
2077 | * Non-default names may need to allocate a memory to compose | |
2078 | * a name. This memory is returned in tmp and needs to be | |
2079 | * freed by the caller. | |
2080 | */ | |
e454cea2 | 2081 | const char *device_get_devnode(struct device *dev, |
4e4098a3 | 2082 | umode_t *mode, kuid_t *uid, kgid_t *gid, |
3c2670e6 | 2083 | const char **tmp) |
6fcf53ac KS |
2084 | { |
2085 | char *s; | |
2086 | ||
2087 | *tmp = NULL; | |
2088 | ||
2089 | /* the device type may provide a specific name */ | |
e454cea2 | 2090 | if (dev->type && dev->type->devnode) |
3c2670e6 | 2091 | *tmp = dev->type->devnode(dev, mode, uid, gid); |
6fcf53ac KS |
2092 | if (*tmp) |
2093 | return *tmp; | |
2094 | ||
2095 | /* the class may provide a specific name */ | |
e454cea2 KS |
2096 | if (dev->class && dev->class->devnode) |
2097 | *tmp = dev->class->devnode(dev, mode); | |
6fcf53ac KS |
2098 | if (*tmp) |
2099 | return *tmp; | |
2100 | ||
2101 | /* return name without allocation, tmp == NULL */ | |
2102 | if (strchr(dev_name(dev), '!') == NULL) | |
2103 | return dev_name(dev); | |
2104 | ||
2105 | /* replace '!' in the name with '/' */ | |
a29fd614 RV |
2106 | s = kstrdup(dev_name(dev), GFP_KERNEL); |
2107 | if (!s) | |
6fcf53ac | 2108 | return NULL; |
a29fd614 RV |
2109 | strreplace(s, '!', '/'); |
2110 | return *tmp = s; | |
6fcf53ac KS |
2111 | } |
2112 | ||
1da177e4 | 2113 | /** |
4a3ad20c GKH |
2114 | * device_for_each_child - device child iterator. |
2115 | * @parent: parent struct device. | |
4a3ad20c | 2116 | * @fn: function to be called for each device. |
f8878dcb | 2117 | * @data: data for the callback. |
1da177e4 | 2118 | * |
4a3ad20c GKH |
2119 | * Iterate over @parent's child devices, and call @fn for each, |
2120 | * passing it @data. | |
1da177e4 | 2121 | * |
4a3ad20c GKH |
2122 | * We check the return of @fn each time. If it returns anything |
2123 | * other than 0, we break out and return that value. | |
1da177e4 | 2124 | */ |
4a3ad20c GKH |
2125 | int device_for_each_child(struct device *parent, void *data, |
2126 | int (*fn)(struct device *dev, void *data)) | |
1da177e4 | 2127 | { |
36239577 | 2128 | struct klist_iter i; |
4a3ad20c | 2129 | struct device *child; |
1da177e4 LT |
2130 | int error = 0; |
2131 | ||
014c90db GKH |
2132 | if (!parent->p) |
2133 | return 0; | |
2134 | ||
f791b8c8 | 2135 | klist_iter_init(&parent->p->klist_children, &i); |
36239577 PM |
2136 | while ((child = next_device(&i)) && !error) |
2137 | error = fn(child, data); | |
2138 | klist_iter_exit(&i); | |
1da177e4 LT |
2139 | return error; |
2140 | } | |
86df2687 | 2141 | EXPORT_SYMBOL_GPL(device_for_each_child); |
1da177e4 | 2142 | |
3d060aeb AS |
2143 | /** |
2144 | * device_for_each_child_reverse - device child iterator in reversed order. | |
2145 | * @parent: parent struct device. | |
2146 | * @fn: function to be called for each device. | |
2147 | * @data: data for the callback. | |
2148 | * | |
2149 | * Iterate over @parent's child devices, and call @fn for each, | |
2150 | * passing it @data. | |
2151 | * | |
2152 | * We check the return of @fn each time. If it returns anything | |
2153 | * other than 0, we break out and return that value. | |
2154 | */ | |
2155 | int device_for_each_child_reverse(struct device *parent, void *data, | |
2156 | int (*fn)(struct device *dev, void *data)) | |
2157 | { | |
2158 | struct klist_iter i; | |
2159 | struct device *child; | |
2160 | int error = 0; | |
2161 | ||
2162 | if (!parent->p) | |
2163 | return 0; | |
2164 | ||
2165 | klist_iter_init(&parent->p->klist_children, &i); | |
2166 | while ((child = prev_device(&i)) && !error) | |
2167 | error = fn(child, data); | |
2168 | klist_iter_exit(&i); | |
2169 | return error; | |
2170 | } | |
2171 | EXPORT_SYMBOL_GPL(device_for_each_child_reverse); | |
2172 | ||
5ab69981 CH |
2173 | /** |
2174 | * device_find_child - device iterator for locating a particular device. | |
2175 | * @parent: parent struct device | |
5ab69981 | 2176 | * @match: Callback function to check device |
f8878dcb | 2177 | * @data: Data to pass to match function |
5ab69981 CH |
2178 | * |
2179 | * This is similar to the device_for_each_child() function above, but it | |
2180 | * returns a reference to a device that is 'found' for later use, as | |
2181 | * determined by the @match callback. | |
2182 | * | |
2183 | * The callback should return 0 if the device doesn't match and non-zero | |
2184 | * if it does. If the callback returns non-zero and a reference to the | |
2185 | * current device can be obtained, this function will return to the caller | |
2186 | * and not iterate over any more devices. | |
a4e2400a FV |
2187 | * |
2188 | * NOTE: you will need to drop the reference with put_device() after use. | |
5ab69981 | 2189 | */ |
4a3ad20c GKH |
2190 | struct device *device_find_child(struct device *parent, void *data, |
2191 | int (*match)(struct device *dev, void *data)) | |
5ab69981 CH |
2192 | { |
2193 | struct klist_iter i; | |
2194 | struct device *child; | |
2195 | ||
2196 | if (!parent) | |
2197 | return NULL; | |
2198 | ||
f791b8c8 | 2199 | klist_iter_init(&parent->p->klist_children, &i); |
5ab69981 CH |
2200 | while ((child = next_device(&i))) |
2201 | if (match(child, data) && get_device(child)) | |
2202 | break; | |
2203 | klist_iter_exit(&i); | |
2204 | return child; | |
2205 | } | |
86df2687 | 2206 | EXPORT_SYMBOL_GPL(device_find_child); |
5ab69981 | 2207 | |
1da177e4 LT |
2208 | int __init devices_init(void) |
2209 | { | |
881c6cfd GKH |
2210 | devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL); |
2211 | if (!devices_kset) | |
2212 | return -ENOMEM; | |
e105b8bf DW |
2213 | dev_kobj = kobject_create_and_add("dev", NULL); |
2214 | if (!dev_kobj) | |
2215 | goto dev_kobj_err; | |
2216 | sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj); | |
2217 | if (!sysfs_dev_block_kobj) | |
2218 | goto block_kobj_err; | |
2219 | sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj); | |
2220 | if (!sysfs_dev_char_kobj) | |
2221 | goto char_kobj_err; | |
2222 | ||
881c6cfd | 2223 | return 0; |
e105b8bf DW |
2224 | |
2225 | char_kobj_err: | |
2226 | kobject_put(sysfs_dev_block_kobj); | |
2227 | block_kobj_err: | |
2228 | kobject_put(dev_kobj); | |
2229 | dev_kobj_err: | |
2230 | kset_unregister(devices_kset); | |
2231 | return -ENOMEM; | |
1da177e4 LT |
2232 | } |
2233 | ||
4f3549d7 RW |
2234 | static int device_check_offline(struct device *dev, void *not_used) |
2235 | { | |
2236 | int ret; | |
2237 | ||
2238 | ret = device_for_each_child(dev, NULL, device_check_offline); | |
2239 | if (ret) | |
2240 | return ret; | |
2241 | ||
2242 | return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0; | |
2243 | } | |
2244 | ||
2245 | /** | |
2246 | * device_offline - Prepare the device for hot-removal. | |
2247 | * @dev: Device to be put offline. | |
2248 | * | |
2249 | * Execute the device bus type's .offline() callback, if present, to prepare | |
2250 | * the device for a subsequent hot-removal. If that succeeds, the device must | |
2251 | * not be used until either it is removed or its bus type's .online() callback | |
2252 | * is executed. | |
2253 | * | |
2254 | * Call under device_hotplug_lock. | |
2255 | */ | |
2256 | int device_offline(struct device *dev) | |
2257 | { | |
2258 | int ret; | |
2259 | ||
2260 | if (dev->offline_disabled) | |
2261 | return -EPERM; | |
2262 | ||
2263 | ret = device_for_each_child(dev, NULL, device_check_offline); | |
2264 | if (ret) | |
2265 | return ret; | |
2266 | ||
2267 | device_lock(dev); | |
2268 | if (device_supports_offline(dev)) { | |
2269 | if (dev->offline) { | |
2270 | ret = 1; | |
2271 | } else { | |
2272 | ret = dev->bus->offline(dev); | |
2273 | if (!ret) { | |
2274 | kobject_uevent(&dev->kobj, KOBJ_OFFLINE); | |
2275 | dev->offline = true; | |
2276 | } | |
2277 | } | |
2278 | } | |
2279 | device_unlock(dev); | |
2280 | ||
2281 | return ret; | |
2282 | } | |
2283 | ||
2284 | /** | |
2285 | * device_online - Put the device back online after successful device_offline(). | |
2286 | * @dev: Device to be put back online. | |
2287 | * | |
2288 | * If device_offline() has been successfully executed for @dev, but the device | |
2289 | * has not been removed subsequently, execute its bus type's .online() callback | |
2290 | * to indicate that the device can be used again. | |
2291 | * | |
2292 | * Call under device_hotplug_lock. | |
2293 | */ | |
2294 | int device_online(struct device *dev) | |
2295 | { | |
2296 | int ret = 0; | |
2297 | ||
2298 | device_lock(dev); | |
2299 | if (device_supports_offline(dev)) { | |
2300 | if (dev->offline) { | |
2301 | ret = dev->bus->online(dev); | |
2302 | if (!ret) { | |
2303 | kobject_uevent(&dev->kobj, KOBJ_ONLINE); | |
2304 | dev->offline = false; | |
2305 | } | |
2306 | } else { | |
2307 | ret = 1; | |
2308 | } | |
2309 | } | |
2310 | device_unlock(dev); | |
2311 | ||
2312 | return ret; | |
2313 | } | |
2314 | ||
7f100d15 | 2315 | struct root_device { |
0aa0dc41 MM |
2316 | struct device dev; |
2317 | struct module *owner; | |
2318 | }; | |
2319 | ||
93058424 | 2320 | static inline struct root_device *to_root_device(struct device *d) |
481e2079 FW |
2321 | { |
2322 | return container_of(d, struct root_device, dev); | |
2323 | } | |
0aa0dc41 MM |
2324 | |
2325 | static void root_device_release(struct device *dev) | |
2326 | { | |
2327 | kfree(to_root_device(dev)); | |
2328 | } | |
2329 | ||
2330 | /** | |
2331 | * __root_device_register - allocate and register a root device | |
2332 | * @name: root device name | |
2333 | * @owner: owner module of the root device, usually THIS_MODULE | |
2334 | * | |
2335 | * This function allocates a root device and registers it | |
2336 | * using device_register(). In order to free the returned | |
2337 | * device, use root_device_unregister(). | |
2338 | * | |
2339 | * Root devices are dummy devices which allow other devices | |
2340 | * to be grouped under /sys/devices. Use this function to | |
2341 | * allocate a root device and then use it as the parent of | |
2342 | * any device which should appear under /sys/devices/{name} | |
2343 | * | |
2344 | * The /sys/devices/{name} directory will also contain a | |
2345 | * 'module' symlink which points to the @owner directory | |
2346 | * in sysfs. | |
2347 | * | |
f0eae0ed JN |
2348 | * Returns &struct device pointer on success, or ERR_PTR() on error. |
2349 | * | |
0aa0dc41 MM |
2350 | * Note: You probably want to use root_device_register(). |
2351 | */ | |
2352 | struct device *__root_device_register(const char *name, struct module *owner) | |
2353 | { | |
2354 | struct root_device *root; | |
2355 | int err = -ENOMEM; | |
2356 | ||
2357 | root = kzalloc(sizeof(struct root_device), GFP_KERNEL); | |
2358 | if (!root) | |
2359 | return ERR_PTR(err); | |
2360 | ||
acc0e90f | 2361 | err = dev_set_name(&root->dev, "%s", name); |
0aa0dc41 MM |
2362 | if (err) { |
2363 | kfree(root); | |
2364 | return ERR_PTR(err); | |
2365 | } | |
2366 | ||
2367 | root->dev.release = root_device_release; | |
2368 | ||
2369 | err = device_register(&root->dev); | |
2370 | if (err) { | |
2371 | put_device(&root->dev); | |
2372 | return ERR_PTR(err); | |
2373 | } | |
2374 | ||
1d9e882b | 2375 | #ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */ |
0aa0dc41 MM |
2376 | if (owner) { |
2377 | struct module_kobject *mk = &owner->mkobj; | |
2378 | ||
2379 | err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module"); | |
2380 | if (err) { | |
2381 | device_unregister(&root->dev); | |
2382 | return ERR_PTR(err); | |
2383 | } | |
2384 | root->owner = owner; | |
2385 | } | |
2386 | #endif | |
2387 | ||
2388 | return &root->dev; | |
2389 | } | |
2390 | EXPORT_SYMBOL_GPL(__root_device_register); | |
2391 | ||
2392 | /** | |
2393 | * root_device_unregister - unregister and free a root device | |
7cbcf225 | 2394 | * @dev: device going away |
0aa0dc41 MM |
2395 | * |
2396 | * This function unregisters and cleans up a device that was created by | |
2397 | * root_device_register(). | |
2398 | */ | |
2399 | void root_device_unregister(struct device *dev) | |
2400 | { | |
2401 | struct root_device *root = to_root_device(dev); | |
2402 | ||
2403 | if (root->owner) | |
2404 | sysfs_remove_link(&root->dev.kobj, "module"); | |
2405 | ||
2406 | device_unregister(dev); | |
2407 | } | |
2408 | EXPORT_SYMBOL_GPL(root_device_unregister); | |
2409 | ||
23681e47 GKH |
2410 | |
2411 | static void device_create_release(struct device *dev) | |
2412 | { | |
1e0b2cf9 | 2413 | pr_debug("device: '%s': %s\n", dev_name(dev), __func__); |
23681e47 GKH |
2414 | kfree(dev); |
2415 | } | |
2416 | ||
39ef3112 GR |
2417 | static struct device * |
2418 | device_create_groups_vargs(struct class *class, struct device *parent, | |
2419 | dev_t devt, void *drvdata, | |
2420 | const struct attribute_group **groups, | |
2421 | const char *fmt, va_list args) | |
23681e47 | 2422 | { |
23681e47 GKH |
2423 | struct device *dev = NULL; |
2424 | int retval = -ENODEV; | |
2425 | ||
2426 | if (class == NULL || IS_ERR(class)) | |
2427 | goto error; | |
23681e47 GKH |
2428 | |
2429 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); | |
2430 | if (!dev) { | |
2431 | retval = -ENOMEM; | |
2432 | goto error; | |
2433 | } | |
2434 | ||
bbc780f8 | 2435 | device_initialize(dev); |
23681e47 GKH |
2436 | dev->devt = devt; |
2437 | dev->class = class; | |
2438 | dev->parent = parent; | |
39ef3112 | 2439 | dev->groups = groups; |
23681e47 | 2440 | dev->release = device_create_release; |
8882b394 | 2441 | dev_set_drvdata(dev, drvdata); |
23681e47 | 2442 | |
1fa5ae85 KS |
2443 | retval = kobject_set_name_vargs(&dev->kobj, fmt, args); |
2444 | if (retval) | |
2445 | goto error; | |
2446 | ||
bbc780f8 | 2447 | retval = device_add(dev); |
23681e47 GKH |
2448 | if (retval) |
2449 | goto error; | |
2450 | ||
23681e47 GKH |
2451 | return dev; |
2452 | ||
2453 | error: | |
286661b3 | 2454 | put_device(dev); |
23681e47 GKH |
2455 | return ERR_PTR(retval); |
2456 | } | |
39ef3112 GR |
2457 | |
2458 | /** | |
2459 | * device_create_vargs - creates a device and registers it with sysfs | |
2460 | * @class: pointer to the struct class that this device should be registered to | |
2461 | * @parent: pointer to the parent struct device of this new device, if any | |
2462 | * @devt: the dev_t for the char device to be added | |
2463 | * @drvdata: the data to be added to the device for callbacks | |
2464 | * @fmt: string for the device's name | |
2465 | * @args: va_list for the device's name | |
2466 | * | |
2467 | * This function can be used by char device classes. A struct device | |
2468 | * will be created in sysfs, registered to the specified class. | |
2469 | * | |
2470 | * A "dev" file will be created, showing the dev_t for the device, if | |
2471 | * the dev_t is not 0,0. | |
2472 | * If a pointer to a parent struct device is passed in, the newly created | |
2473 | * struct device will be a child of that device in sysfs. | |
2474 | * The pointer to the struct device will be returned from the call. | |
2475 | * Any further sysfs files that might be required can be created using this | |
2476 | * pointer. | |
2477 | * | |
2478 | * Returns &struct device pointer on success, or ERR_PTR() on error. | |
2479 | * | |
2480 | * Note: the struct class passed to this function must have previously | |
2481 | * been created with a call to class_create(). | |
2482 | */ | |
2483 | struct device *device_create_vargs(struct class *class, struct device *parent, | |
2484 | dev_t devt, void *drvdata, const char *fmt, | |
2485 | va_list args) | |
2486 | { | |
2487 | return device_create_groups_vargs(class, parent, devt, drvdata, NULL, | |
2488 | fmt, args); | |
2489 | } | |
8882b394 GKH |
2490 | EXPORT_SYMBOL_GPL(device_create_vargs); |
2491 | ||
2492 | /** | |
4e106739 | 2493 | * device_create - creates a device and registers it with sysfs |
8882b394 GKH |
2494 | * @class: pointer to the struct class that this device should be registered to |
2495 | * @parent: pointer to the parent struct device of this new device, if any | |
2496 | * @devt: the dev_t for the char device to be added | |
2497 | * @drvdata: the data to be added to the device for callbacks | |
2498 | * @fmt: string for the device's name | |
2499 | * | |
2500 | * This function can be used by char device classes. A struct device | |
2501 | * will be created in sysfs, registered to the specified class. | |
2502 | * | |
2503 | * A "dev" file will be created, showing the dev_t for the device, if | |
2504 | * the dev_t is not 0,0. | |
2505 | * If a pointer to a parent struct device is passed in, the newly created | |
2506 | * struct device will be a child of that device in sysfs. | |
2507 | * The pointer to the struct device will be returned from the call. | |
2508 | * Any further sysfs files that might be required can be created using this | |
2509 | * pointer. | |
2510 | * | |
f0eae0ed JN |
2511 | * Returns &struct device pointer on success, or ERR_PTR() on error. |
2512 | * | |
8882b394 GKH |
2513 | * Note: the struct class passed to this function must have previously |
2514 | * been created with a call to class_create(). | |
2515 | */ | |
4e106739 GKH |
2516 | struct device *device_create(struct class *class, struct device *parent, |
2517 | dev_t devt, void *drvdata, const char *fmt, ...) | |
8882b394 GKH |
2518 | { |
2519 | va_list vargs; | |
2520 | struct device *dev; | |
2521 | ||
2522 | va_start(vargs, fmt); | |
2523 | dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs); | |
2524 | va_end(vargs); | |
2525 | return dev; | |
2526 | } | |
4e106739 | 2527 | EXPORT_SYMBOL_GPL(device_create); |
8882b394 | 2528 | |
39ef3112 GR |
2529 | /** |
2530 | * device_create_with_groups - creates a device and registers it with sysfs | |
2531 | * @class: pointer to the struct class that this device should be registered to | |
2532 | * @parent: pointer to the parent struct device of this new device, if any | |
2533 | * @devt: the dev_t for the char device to be added | |
2534 | * @drvdata: the data to be added to the device for callbacks | |
2535 | * @groups: NULL-terminated list of attribute groups to be created | |
2536 | * @fmt: string for the device's name | |
2537 | * | |
2538 | * This function can be used by char device classes. A struct device | |
2539 | * will be created in sysfs, registered to the specified class. | |
2540 | * Additional attributes specified in the groups parameter will also | |
2541 | * be created automatically. | |
2542 | * | |
2543 | * A "dev" file will be created, showing the dev_t for the device, if | |
2544 | * the dev_t is not 0,0. | |
2545 | * If a pointer to a parent struct device is passed in, the newly created | |
2546 | * struct device will be a child of that device in sysfs. | |
2547 | * The pointer to the struct device will be returned from the call. | |
2548 | * Any further sysfs files that might be required can be created using this | |
2549 | * pointer. | |
2550 | * | |
2551 | * Returns &struct device pointer on success, or ERR_PTR() on error. | |
2552 | * | |
2553 | * Note: the struct class passed to this function must have previously | |
2554 | * been created with a call to class_create(). | |
2555 | */ | |
2556 | struct device *device_create_with_groups(struct class *class, | |
2557 | struct device *parent, dev_t devt, | |
2558 | void *drvdata, | |
2559 | const struct attribute_group **groups, | |
2560 | const char *fmt, ...) | |
2561 | { | |
2562 | va_list vargs; | |
2563 | struct device *dev; | |
2564 | ||
2565 | va_start(vargs, fmt); | |
2566 | dev = device_create_groups_vargs(class, parent, devt, drvdata, groups, | |
2567 | fmt, vargs); | |
2568 | va_end(vargs); | |
2569 | return dev; | |
2570 | } | |
2571 | EXPORT_SYMBOL_GPL(device_create_with_groups); | |
2572 | ||
9f3b795a | 2573 | static int __match_devt(struct device *dev, const void *data) |
23681e47 | 2574 | { |
9f3b795a | 2575 | const dev_t *devt = data; |
23681e47 | 2576 | |
cd35449b | 2577 | return dev->devt == *devt; |
775b64d2 RW |
2578 | } |
2579 | ||
2580 | /** | |
2581 | * device_destroy - removes a device that was created with device_create() | |
2582 | * @class: pointer to the struct class that this device was registered with | |
2583 | * @devt: the dev_t of the device that was previously registered | |
2584 | * | |
2585 | * This call unregisters and cleans up a device that was created with a | |
2586 | * call to device_create(). | |
2587 | */ | |
2588 | void device_destroy(struct class *class, dev_t devt) | |
2589 | { | |
2590 | struct device *dev; | |
23681e47 | 2591 | |
695794ae | 2592 | dev = class_find_device(class, NULL, &devt, __match_devt); |
cd35449b DY |
2593 | if (dev) { |
2594 | put_device(dev); | |
23681e47 | 2595 | device_unregister(dev); |
cd35449b | 2596 | } |
23681e47 GKH |
2597 | } |
2598 | EXPORT_SYMBOL_GPL(device_destroy); | |
a2de48ca GKH |
2599 | |
2600 | /** | |
2601 | * device_rename - renames a device | |
2602 | * @dev: the pointer to the struct device to be renamed | |
2603 | * @new_name: the new name of the device | |
030c1d2b EB |
2604 | * |
2605 | * It is the responsibility of the caller to provide mutual | |
2606 | * exclusion between two different calls of device_rename | |
2607 | * on the same device to ensure that new_name is valid and | |
2608 | * won't conflict with other devices. | |
c6c0ac66 | 2609 | * |
a5462516 TT |
2610 | * Note: Don't call this function. Currently, the networking layer calls this |
2611 | * function, but that will change. The following text from Kay Sievers offers | |
2612 | * some insight: | |
2613 | * | |
2614 | * Renaming devices is racy at many levels, symlinks and other stuff are not | |
2615 | * replaced atomically, and you get a "move" uevent, but it's not easy to | |
2616 | * connect the event to the old and new device. Device nodes are not renamed at | |
2617 | * all, there isn't even support for that in the kernel now. | |
2618 | * | |
2619 | * In the meantime, during renaming, your target name might be taken by another | |
2620 | * driver, creating conflicts. Or the old name is taken directly after you | |
2621 | * renamed it -- then you get events for the same DEVPATH, before you even see | |
2622 | * the "move" event. It's just a mess, and nothing new should ever rely on | |
2623 | * kernel device renaming. Besides that, it's not even implemented now for | |
2624 | * other things than (driver-core wise very simple) network devices. | |
2625 | * | |
2626 | * We are currently about to change network renaming in udev to completely | |
2627 | * disallow renaming of devices in the same namespace as the kernel uses, | |
2628 | * because we can't solve the problems properly, that arise with swapping names | |
2629 | * of multiple interfaces without races. Means, renaming of eth[0-9]* will only | |
2630 | * be allowed to some other name than eth[0-9]*, for the aforementioned | |
2631 | * reasons. | |
2632 | * | |
2633 | * Make up a "real" name in the driver before you register anything, or add | |
2634 | * some other attributes for userspace to find the device, or use udev to add | |
2635 | * symlinks -- but never rename kernel devices later, it's a complete mess. We | |
2636 | * don't even want to get into that and try to implement the missing pieces in | |
2637 | * the core. We really have other pieces to fix in the driver core mess. :) | |
a2de48ca | 2638 | */ |
6937e8f8 | 2639 | int device_rename(struct device *dev, const char *new_name) |
a2de48ca | 2640 | { |
4b30ee58 | 2641 | struct kobject *kobj = &dev->kobj; |
2ee97caf | 2642 | char *old_device_name = NULL; |
a2de48ca GKH |
2643 | int error; |
2644 | ||
2645 | dev = get_device(dev); | |
2646 | if (!dev) | |
2647 | return -EINVAL; | |
2648 | ||
69df7533 | 2649 | dev_dbg(dev, "renaming to %s\n", new_name); |
a2de48ca | 2650 | |
1fa5ae85 | 2651 | old_device_name = kstrdup(dev_name(dev), GFP_KERNEL); |
2ee97caf CH |
2652 | if (!old_device_name) { |
2653 | error = -ENOMEM; | |
2654 | goto out; | |
a2de48ca | 2655 | } |
a2de48ca | 2656 | |
f349cf34 | 2657 | if (dev->class) { |
4b30ee58 TH |
2658 | error = sysfs_rename_link_ns(&dev->class->p->subsys.kobj, |
2659 | kobj, old_device_name, | |
2660 | new_name, kobject_namespace(kobj)); | |
f349cf34 EB |
2661 | if (error) |
2662 | goto out; | |
2663 | } | |
39aba963 | 2664 | |
4b30ee58 | 2665 | error = kobject_rename(kobj, new_name); |
1fa5ae85 | 2666 | if (error) |
2ee97caf | 2667 | goto out; |
a2de48ca | 2668 | |
2ee97caf | 2669 | out: |
a2de48ca GKH |
2670 | put_device(dev); |
2671 | ||
2ee97caf | 2672 | kfree(old_device_name); |
a2de48ca GKH |
2673 | |
2674 | return error; | |
2675 | } | |
a2807dbc | 2676 | EXPORT_SYMBOL_GPL(device_rename); |
8a82472f CH |
2677 | |
2678 | static int device_move_class_links(struct device *dev, | |
2679 | struct device *old_parent, | |
2680 | struct device *new_parent) | |
2681 | { | |
f7f3461d | 2682 | int error = 0; |
8a82472f | 2683 | |
f7f3461d GKH |
2684 | if (old_parent) |
2685 | sysfs_remove_link(&dev->kobj, "device"); | |
2686 | if (new_parent) | |
2687 | error = sysfs_create_link(&dev->kobj, &new_parent->kobj, | |
2688 | "device"); | |
2689 | return error; | |
8a82472f CH |
2690 | } |
2691 | ||
2692 | /** | |
2693 | * device_move - moves a device to a new parent | |
2694 | * @dev: the pointer to the struct device to be moved | |
c744aeae | 2695 | * @new_parent: the new parent of the device (can by NULL) |
ffa6a705 | 2696 | * @dpm_order: how to reorder the dpm_list |
8a82472f | 2697 | */ |
ffa6a705 CH |
2698 | int device_move(struct device *dev, struct device *new_parent, |
2699 | enum dpm_order dpm_order) | |
8a82472f CH |
2700 | { |
2701 | int error; | |
2702 | struct device *old_parent; | |
c744aeae | 2703 | struct kobject *new_parent_kobj; |
8a82472f CH |
2704 | |
2705 | dev = get_device(dev); | |
2706 | if (!dev) | |
2707 | return -EINVAL; | |
2708 | ||
ffa6a705 | 2709 | device_pm_lock(); |
8a82472f | 2710 | new_parent = get_device(new_parent); |
4a3ad20c | 2711 | new_parent_kobj = get_device_parent(dev, new_parent); |
9de48bc4 TH |
2712 | if (IS_ERR(new_parent_kobj)) { |
2713 | error = PTR_ERR(new_parent_kobj); | |
2714 | put_device(new_parent); | |
2715 | goto out; | |
2716 | } | |
63b6971a | 2717 | |
1e0b2cf9 KS |
2718 | pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev), |
2719 | __func__, new_parent ? dev_name(new_parent) : "<NULL>"); | |
c744aeae | 2720 | error = kobject_move(&dev->kobj, new_parent_kobj); |
8a82472f | 2721 | if (error) { |
63b6971a | 2722 | cleanup_glue_dir(dev, new_parent_kobj); |
8a82472f CH |
2723 | put_device(new_parent); |
2724 | goto out; | |
2725 | } | |
2726 | old_parent = dev->parent; | |
2727 | dev->parent = new_parent; | |
2728 | if (old_parent) | |
f791b8c8 | 2729 | klist_remove(&dev->p->knode_parent); |
0d358f22 | 2730 | if (new_parent) { |
f791b8c8 GKH |
2731 | klist_add_tail(&dev->p->knode_parent, |
2732 | &new_parent->p->klist_children); | |
0d358f22 YL |
2733 | set_dev_node(dev, dev_to_node(new_parent)); |
2734 | } | |
2735 | ||
bdd4034d RV |
2736 | if (dev->class) { |
2737 | error = device_move_class_links(dev, old_parent, new_parent); | |
2738 | if (error) { | |
2739 | /* We ignore errors on cleanup since we're hosed anyway... */ | |
2740 | device_move_class_links(dev, new_parent, old_parent); | |
2741 | if (!kobject_move(&dev->kobj, &old_parent->kobj)) { | |
2742 | if (new_parent) | |
2743 | klist_remove(&dev->p->knode_parent); | |
2744 | dev->parent = old_parent; | |
2745 | if (old_parent) { | |
2746 | klist_add_tail(&dev->p->knode_parent, | |
2747 | &old_parent->p->klist_children); | |
2748 | set_dev_node(dev, dev_to_node(old_parent)); | |
2749 | } | |
0d358f22 | 2750 | } |
bdd4034d RV |
2751 | cleanup_glue_dir(dev, new_parent_kobj); |
2752 | put_device(new_parent); | |
2753 | goto out; | |
8a82472f | 2754 | } |
8a82472f | 2755 | } |
ffa6a705 CH |
2756 | switch (dpm_order) { |
2757 | case DPM_ORDER_NONE: | |
2758 | break; | |
2759 | case DPM_ORDER_DEV_AFTER_PARENT: | |
2760 | device_pm_move_after(dev, new_parent); | |
52cdbdd4 | 2761 | devices_kset_move_after(dev, new_parent); |
ffa6a705 CH |
2762 | break; |
2763 | case DPM_ORDER_PARENT_BEFORE_DEV: | |
2764 | device_pm_move_before(new_parent, dev); | |
52cdbdd4 | 2765 | devices_kset_move_before(new_parent, dev); |
ffa6a705 CH |
2766 | break; |
2767 | case DPM_ORDER_DEV_LAST: | |
2768 | device_pm_move_last(dev); | |
52cdbdd4 | 2769 | devices_kset_move_last(dev); |
ffa6a705 CH |
2770 | break; |
2771 | } | |
bdd4034d | 2772 | |
8a82472f CH |
2773 | put_device(old_parent); |
2774 | out: | |
ffa6a705 | 2775 | device_pm_unlock(); |
8a82472f CH |
2776 | put_device(dev); |
2777 | return error; | |
2778 | } | |
8a82472f | 2779 | EXPORT_SYMBOL_GPL(device_move); |
37b0c020 GKH |
2780 | |
2781 | /** | |
2782 | * device_shutdown - call ->shutdown() on each device to shutdown. | |
2783 | */ | |
2784 | void device_shutdown(void) | |
2785 | { | |
f123db8e | 2786 | struct device *dev, *parent; |
6245838f HD |
2787 | |
2788 | spin_lock(&devices_kset->list_lock); | |
2789 | /* | |
2790 | * Walk the devices list backward, shutting down each in turn. | |
2791 | * Beware that device unplug events may also start pulling | |
2792 | * devices offline, even as the system is shutting down. | |
2793 | */ | |
2794 | while (!list_empty(&devices_kset->list)) { | |
2795 | dev = list_entry(devices_kset->list.prev, struct device, | |
2796 | kobj.entry); | |
d1c6c030 ML |
2797 | |
2798 | /* | |
2799 | * hold reference count of device's parent to | |
2800 | * prevent it from being freed because parent's | |
2801 | * lock is to be held | |
2802 | */ | |
f123db8e | 2803 | parent = get_device(dev->parent); |
6245838f HD |
2804 | get_device(dev); |
2805 | /* | |
2806 | * Make sure the device is off the kset list, in the | |
2807 | * event that dev->*->shutdown() doesn't remove it. | |
2808 | */ | |
2809 | list_del_init(&dev->kobj.entry); | |
2810 | spin_unlock(&devices_kset->list_lock); | |
fe6b91f4 | 2811 | |
d1c6c030 | 2812 | /* hold lock to avoid race with probe/release */ |
f123db8e BL |
2813 | if (parent) |
2814 | device_lock(parent); | |
d1c6c030 ML |
2815 | device_lock(dev); |
2816 | ||
fe6b91f4 AS |
2817 | /* Don't allow any more runtime suspends */ |
2818 | pm_runtime_get_noresume(dev); | |
2819 | pm_runtime_barrier(dev); | |
37b0c020 | 2820 | |
7521621e | 2821 | if (dev->class && dev->class->shutdown_pre) { |
f77af151 | 2822 | if (initcall_debug) |
7521621e MS |
2823 | dev_info(dev, "shutdown_pre\n"); |
2824 | dev->class->shutdown_pre(dev); | |
2825 | } | |
2826 | if (dev->bus && dev->bus->shutdown) { | |
0246c4fa SL |
2827 | if (initcall_debug) |
2828 | dev_info(dev, "shutdown\n"); | |
37b0c020 GKH |
2829 | dev->bus->shutdown(dev); |
2830 | } else if (dev->driver && dev->driver->shutdown) { | |
0246c4fa SL |
2831 | if (initcall_debug) |
2832 | dev_info(dev, "shutdown\n"); | |
37b0c020 GKH |
2833 | dev->driver->shutdown(dev); |
2834 | } | |
d1c6c030 ML |
2835 | |
2836 | device_unlock(dev); | |
f123db8e BL |
2837 | if (parent) |
2838 | device_unlock(parent); | |
d1c6c030 | 2839 | |
6245838f | 2840 | put_device(dev); |
f123db8e | 2841 | put_device(parent); |
6245838f HD |
2842 | |
2843 | spin_lock(&devices_kset->list_lock); | |
37b0c020 | 2844 | } |
6245838f | 2845 | spin_unlock(&devices_kset->list_lock); |
37b0c020 | 2846 | } |
99bcf217 JP |
2847 | |
2848 | /* | |
2849 | * Device logging functions | |
2850 | */ | |
2851 | ||
2852 | #ifdef CONFIG_PRINTK | |
666f355f JP |
2853 | static int |
2854 | create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen) | |
99bcf217 | 2855 | { |
c4e00daa | 2856 | const char *subsys; |
798efc60 | 2857 | size_t pos = 0; |
99bcf217 | 2858 | |
c4e00daa KS |
2859 | if (dev->class) |
2860 | subsys = dev->class->name; | |
2861 | else if (dev->bus) | |
2862 | subsys = dev->bus->name; | |
2863 | else | |
798efc60 | 2864 | return 0; |
c4e00daa | 2865 | |
798efc60 | 2866 | pos += snprintf(hdr + pos, hdrlen - pos, "SUBSYSTEM=%s", subsys); |
655e5b7c BH |
2867 | if (pos >= hdrlen) |
2868 | goto overflow; | |
c4e00daa KS |
2869 | |
2870 | /* | |
2871 | * Add device identifier DEVICE=: | |
2872 | * b12:8 block dev_t | |
2873 | * c127:3 char dev_t | |
2874 | * n8 netdev ifindex | |
2875 | * +sound:card0 subsystem:devname | |
2876 | */ | |
2877 | if (MAJOR(dev->devt)) { | |
2878 | char c; | |
2879 | ||
2880 | if (strcmp(subsys, "block") == 0) | |
2881 | c = 'b'; | |
2882 | else | |
2883 | c = 'c'; | |
798efc60 JP |
2884 | pos++; |
2885 | pos += snprintf(hdr + pos, hdrlen - pos, | |
2886 | "DEVICE=%c%u:%u", | |
2887 | c, MAJOR(dev->devt), MINOR(dev->devt)); | |
c4e00daa KS |
2888 | } else if (strcmp(subsys, "net") == 0) { |
2889 | struct net_device *net = to_net_dev(dev); | |
2890 | ||
798efc60 JP |
2891 | pos++; |
2892 | pos += snprintf(hdr + pos, hdrlen - pos, | |
2893 | "DEVICE=n%u", net->ifindex); | |
c4e00daa | 2894 | } else { |
798efc60 JP |
2895 | pos++; |
2896 | pos += snprintf(hdr + pos, hdrlen - pos, | |
2897 | "DEVICE=+%s:%s", subsys, dev_name(dev)); | |
c4e00daa | 2898 | } |
af7f2158 | 2899 | |
655e5b7c BH |
2900 | if (pos >= hdrlen) |
2901 | goto overflow; | |
2902 | ||
798efc60 | 2903 | return pos; |
655e5b7c BH |
2904 | |
2905 | overflow: | |
2906 | dev_WARN(dev, "device/subsystem name too long"); | |
2907 | return 0; | |
798efc60 | 2908 | } |
798efc60 | 2909 | |
05e4e5b8 JP |
2910 | int dev_vprintk_emit(int level, const struct device *dev, |
2911 | const char *fmt, va_list args) | |
2912 | { | |
2913 | char hdr[128]; | |
2914 | size_t hdrlen; | |
2915 | ||
2916 | hdrlen = create_syslog_header(dev, hdr, sizeof(hdr)); | |
2917 | ||
2918 | return vprintk_emit(0, level, hdrlen ? hdr : NULL, hdrlen, fmt, args); | |
2919 | } | |
2920 | EXPORT_SYMBOL(dev_vprintk_emit); | |
2921 | ||
2922 | int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...) | |
2923 | { | |
2924 | va_list args; | |
2925 | int r; | |
2926 | ||
2927 | va_start(args, fmt); | |
2928 | ||
2929 | r = dev_vprintk_emit(level, dev, fmt, args); | |
2930 | ||
2931 | va_end(args); | |
2932 | ||
2933 | return r; | |
2934 | } | |
2935 | EXPORT_SYMBOL(dev_printk_emit); | |
2936 | ||
d1f1052c | 2937 | static void __dev_printk(const char *level, const struct device *dev, |
798efc60 JP |
2938 | struct va_format *vaf) |
2939 | { | |
d1f1052c JP |
2940 | if (dev) |
2941 | dev_printk_emit(level[1] - '0', dev, "%s %s: %pV", | |
2942 | dev_driver_string(dev), dev_name(dev), vaf); | |
2943 | else | |
2944 | printk("%s(NULL device *): %pV", level, vaf); | |
99bcf217 JP |
2945 | } |
2946 | ||
d1f1052c JP |
2947 | void dev_printk(const char *level, const struct device *dev, |
2948 | const char *fmt, ...) | |
99bcf217 JP |
2949 | { |
2950 | struct va_format vaf; | |
2951 | va_list args; | |
99bcf217 JP |
2952 | |
2953 | va_start(args, fmt); | |
2954 | ||
2955 | vaf.fmt = fmt; | |
2956 | vaf.va = &args; | |
2957 | ||
d1f1052c | 2958 | __dev_printk(level, dev, &vaf); |
798efc60 | 2959 | |
99bcf217 | 2960 | va_end(args); |
99bcf217 JP |
2961 | } |
2962 | EXPORT_SYMBOL(dev_printk); | |
2963 | ||
2964 | #define define_dev_printk_level(func, kern_level) \ | |
d1f1052c | 2965 | void func(const struct device *dev, const char *fmt, ...) \ |
99bcf217 JP |
2966 | { \ |
2967 | struct va_format vaf; \ | |
2968 | va_list args; \ | |
99bcf217 JP |
2969 | \ |
2970 | va_start(args, fmt); \ | |
2971 | \ | |
2972 | vaf.fmt = fmt; \ | |
2973 | vaf.va = &args; \ | |
2974 | \ | |
d1f1052c | 2975 | __dev_printk(kern_level, dev, &vaf); \ |
798efc60 | 2976 | \ |
99bcf217 | 2977 | va_end(args); \ |
99bcf217 JP |
2978 | } \ |
2979 | EXPORT_SYMBOL(func); | |
2980 | ||
2981 | define_dev_printk_level(dev_emerg, KERN_EMERG); | |
2982 | define_dev_printk_level(dev_alert, KERN_ALERT); | |
2983 | define_dev_printk_level(dev_crit, KERN_CRIT); | |
2984 | define_dev_printk_level(dev_err, KERN_ERR); | |
2985 | define_dev_printk_level(dev_warn, KERN_WARNING); | |
2986 | define_dev_printk_level(dev_notice, KERN_NOTICE); | |
2987 | define_dev_printk_level(_dev_info, KERN_INFO); | |
2988 | ||
2989 | #endif | |
97badf87 RW |
2990 | |
2991 | static inline bool fwnode_is_primary(struct fwnode_handle *fwnode) | |
2992 | { | |
2993 | return fwnode && !IS_ERR(fwnode->secondary); | |
2994 | } | |
2995 | ||
2996 | /** | |
2997 | * set_primary_fwnode - Change the primary firmware node of a given device. | |
2998 | * @dev: Device to handle. | |
2999 | * @fwnode: New primary firmware node of the device. | |
3000 | * | |
3001 | * Set the device's firmware node pointer to @fwnode, but if a secondary | |
3002 | * firmware node of the device is present, preserve it. | |
3003 | */ | |
3004 | void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode) | |
3005 | { | |
3006 | if (fwnode) { | |
3007 | struct fwnode_handle *fn = dev->fwnode; | |
3008 | ||
3009 | if (fwnode_is_primary(fn)) | |
3010 | fn = fn->secondary; | |
3011 | ||
55f89a8a MW |
3012 | if (fn) { |
3013 | WARN_ON(fwnode->secondary); | |
3014 | fwnode->secondary = fn; | |
3015 | } | |
97badf87 RW |
3016 | dev->fwnode = fwnode; |
3017 | } else { | |
3018 | dev->fwnode = fwnode_is_primary(dev->fwnode) ? | |
3019 | dev->fwnode->secondary : NULL; | |
3020 | } | |
3021 | } | |
3022 | EXPORT_SYMBOL_GPL(set_primary_fwnode); | |
3023 | ||
3024 | /** | |
3025 | * set_secondary_fwnode - Change the secondary firmware node of a given device. | |
3026 | * @dev: Device to handle. | |
3027 | * @fwnode: New secondary firmware node of the device. | |
3028 | * | |
3029 | * If a primary firmware node of the device is present, set its secondary | |
3030 | * pointer to @fwnode. Otherwise, set the device's firmware node pointer to | |
3031 | * @fwnode. | |
3032 | */ | |
3033 | void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode) | |
3034 | { | |
3035 | if (fwnode) | |
3036 | fwnode->secondary = ERR_PTR(-ENODEV); | |
3037 | ||
3038 | if (fwnode_is_primary(dev->fwnode)) | |
3039 | dev->fwnode->secondary = fwnode; | |
3040 | else | |
3041 | dev->fwnode = fwnode; | |
3042 | } | |
4e75e1d7 JH |
3043 | |
3044 | /** | |
3045 | * device_set_of_node_from_dev - reuse device-tree node of another device | |
3046 | * @dev: device whose device-tree node is being set | |
3047 | * @dev2: device whose device-tree node is being reused | |
3048 | * | |
3049 | * Takes another reference to the new device-tree node after first dropping | |
3050 | * any reference held to the old node. | |
3051 | */ | |
3052 | void device_set_of_node_from_dev(struct device *dev, const struct device *dev2) | |
3053 | { | |
3054 | of_node_put(dev->of_node); | |
3055 | dev->of_node = of_node_get(dev2->of_node); | |
3056 | dev->of_node_reused = true; | |
3057 | } | |
3058 | EXPORT_SYMBOL_GPL(device_set_of_node_from_dev); |