]>
Commit | Line | Data |
---|---|---|
989d42e8 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 LT |
2 | /* |
3 | * drivers/base/core.c - core driver model code (device registration, etc) | |
4 | * | |
5 | * Copyright (c) 2002-3 Patrick Mochel | |
6 | * Copyright (c) 2002-3 Open Source Development Labs | |
64bb5d2c GKH |
7 | * Copyright (c) 2006 Greg Kroah-Hartman <gregkh@suse.de> |
8 | * Copyright (c) 2006 Novell, Inc. | |
1da177e4 LT |
9 | */ |
10 | ||
7847a145 | 11 | #include <linux/acpi.h> |
65650b35 | 12 | #include <linux/cpufreq.h> |
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> |
f75b1c60 | 25 | #include <linux/mutex.h> |
af8db150 | 26 | #include <linux/pm_runtime.h> |
c4e00daa | 27 | #include <linux/netdevice.h> |
174cd4b1 | 28 | #include <linux/sched/signal.h> |
b8530017 | 29 | #include <linux/sched/mm.h> |
69031f50 | 30 | #include <linux/swiotlb.h> |
63967685 | 31 | #include <linux/sysfs.h> |
6d4e9a8e | 32 | #include <linux/dma-map-ops.h> /* for dma_default_coherent */ |
1da177e4 LT |
33 | |
34 | #include "base.h" | |
35 | #include "power/power.h" | |
36 | ||
e52eec13 AK |
37 | #ifdef CONFIG_SYSFS_DEPRECATED |
38 | #ifdef CONFIG_SYSFS_DEPRECATED_V2 | |
39 | long sysfs_deprecated = 1; | |
40 | #else | |
41 | long sysfs_deprecated = 0; | |
42 | #endif | |
3454bf96 | 43 | static int __init sysfs_deprecated_setup(char *arg) |
e52eec13 | 44 | { |
34da5e67 | 45 | return kstrtol(arg, 10, &sysfs_deprecated); |
e52eec13 AK |
46 | } |
47 | early_param("sysfs.deprecated", sysfs_deprecated_setup); | |
48 | #endif | |
49 | ||
9ed98953 | 50 | /* Device links support. */ |
fc5a251d SK |
51 | static LIST_HEAD(deferred_sync); |
52 | static unsigned int defer_sync_state_count = 1; | |
7b337cb3 | 53 | static DEFINE_MUTEX(fwnode_link_lock); |
25ac86c6 | 54 | static bool fw_devlink_is_permissive(void); |
d46f3e3e | 55 | static bool fw_devlink_drv_reg_done; |
7b337cb3 SK |
56 | |
57 | /** | |
58 | * fwnode_link_add - Create a link between two fwnode_handles. | |
59 | * @con: Consumer end of the link. | |
60 | * @sup: Supplier end of the link. | |
61 | * | |
62 | * Create a fwnode link between fwnode handles @con and @sup. The fwnode link | |
63 | * represents the detail that the firmware lists @sup fwnode as supplying a | |
64 | * resource to @con. | |
65 | * | |
66 | * The driver core will use the fwnode link to create a device link between the | |
67 | * two device objects corresponding to @con and @sup when they are created. The | |
68 | * driver core will automatically delete the fwnode link between @con and @sup | |
69 | * after doing that. | |
70 | * | |
71 | * Attempts to create duplicate links between the same pair of fwnode handles | |
72 | * are ignored and there is no reference counting. | |
73 | */ | |
74 | int fwnode_link_add(struct fwnode_handle *con, struct fwnode_handle *sup) | |
75 | { | |
76 | struct fwnode_link *link; | |
77 | int ret = 0; | |
78 | ||
79 | mutex_lock(&fwnode_link_lock); | |
80 | ||
81 | list_for_each_entry(link, &sup->consumers, s_hook) | |
82 | if (link->consumer == con) | |
83 | goto out; | |
84 | ||
85 | link = kzalloc(sizeof(*link), GFP_KERNEL); | |
86 | if (!link) { | |
87 | ret = -ENOMEM; | |
88 | goto out; | |
89 | } | |
90 | ||
91 | link->supplier = sup; | |
92 | INIT_LIST_HEAD(&link->s_hook); | |
93 | link->consumer = con; | |
94 | INIT_LIST_HEAD(&link->c_hook); | |
95 | ||
96 | list_add(&link->s_hook, &sup->consumers); | |
97 | list_add(&link->c_hook, &con->suppliers); | |
ebd6823a SK |
98 | pr_debug("%pfwP Linked as a fwnode consumer to %pfwP\n", |
99 | con, sup); | |
7b337cb3 SK |
100 | out: |
101 | mutex_unlock(&fwnode_link_lock); | |
102 | ||
103 | return ret; | |
104 | } | |
105 | ||
76f13081 SK |
106 | /** |
107 | * __fwnode_link_del - Delete a link between two fwnode_handles. | |
108 | * @link: the fwnode_link to be deleted | |
109 | * | |
110 | * The fwnode_link_lock needs to be held when this function is called. | |
111 | */ | |
112 | static void __fwnode_link_del(struct fwnode_link *link) | |
113 | { | |
ebd6823a SK |
114 | pr_debug("%pfwP Dropping the fwnode link to %pfwP\n", |
115 | link->consumer, link->supplier); | |
76f13081 SK |
116 | list_del(&link->s_hook); |
117 | list_del(&link->c_hook); | |
118 | kfree(link); | |
119 | } | |
120 | ||
7b337cb3 SK |
121 | /** |
122 | * fwnode_links_purge_suppliers - Delete all supplier links of fwnode_handle. | |
123 | * @fwnode: fwnode whose supplier links need to be deleted | |
124 | * | |
125 | * Deletes all supplier links connecting directly to @fwnode. | |
126 | */ | |
127 | static void fwnode_links_purge_suppliers(struct fwnode_handle *fwnode) | |
128 | { | |
129 | struct fwnode_link *link, *tmp; | |
130 | ||
131 | mutex_lock(&fwnode_link_lock); | |
76f13081 SK |
132 | list_for_each_entry_safe(link, tmp, &fwnode->suppliers, c_hook) |
133 | __fwnode_link_del(link); | |
7b337cb3 SK |
134 | mutex_unlock(&fwnode_link_lock); |
135 | } | |
136 | ||
137 | /** | |
138 | * fwnode_links_purge_consumers - Delete all consumer links of fwnode_handle. | |
139 | * @fwnode: fwnode whose consumer links need to be deleted | |
140 | * | |
141 | * Deletes all consumer links connecting directly to @fwnode. | |
142 | */ | |
143 | static void fwnode_links_purge_consumers(struct fwnode_handle *fwnode) | |
144 | { | |
145 | struct fwnode_link *link, *tmp; | |
146 | ||
147 | mutex_lock(&fwnode_link_lock); | |
76f13081 SK |
148 | list_for_each_entry_safe(link, tmp, &fwnode->consumers, s_hook) |
149 | __fwnode_link_del(link); | |
7b337cb3 SK |
150 | mutex_unlock(&fwnode_link_lock); |
151 | } | |
152 | ||
153 | /** | |
154 | * fwnode_links_purge - Delete all links connected to a fwnode_handle. | |
155 | * @fwnode: fwnode whose links needs to be deleted | |
156 | * | |
157 | * Deletes all links connecting directly to a fwnode. | |
158 | */ | |
159 | void fwnode_links_purge(struct fwnode_handle *fwnode) | |
160 | { | |
161 | fwnode_links_purge_suppliers(fwnode); | |
162 | fwnode_links_purge_consumers(fwnode); | |
163 | } | |
9ed98953 | 164 | |
28ec344b | 165 | void fw_devlink_purge_absent_suppliers(struct fwnode_handle *fwnode) |
9528e0d9 SK |
166 | { |
167 | struct fwnode_handle *child; | |
168 | ||
169 | /* Don't purge consumer links of an added child */ | |
170 | if (fwnode->dev) | |
171 | return; | |
172 | ||
173 | fwnode->flags |= FWNODE_FLAG_NOT_DEVICE; | |
174 | fwnode_links_purge_consumers(fwnode); | |
175 | ||
176 | fwnode_for_each_available_child_node(fwnode, child) | |
177 | fw_devlink_purge_absent_suppliers(child); | |
178 | } | |
28ec344b | 179 | EXPORT_SYMBOL_GPL(fw_devlink_purge_absent_suppliers); |
9528e0d9 | 180 | |
9ed98953 RW |
181 | #ifdef CONFIG_SRCU |
182 | static DEFINE_MUTEX(device_links_lock); | |
183 | DEFINE_STATIC_SRCU(device_links_srcu); | |
184 | ||
185 | static inline void device_links_write_lock(void) | |
186 | { | |
187 | mutex_lock(&device_links_lock); | |
188 | } | |
189 | ||
190 | static inline void device_links_write_unlock(void) | |
191 | { | |
192 | mutex_unlock(&device_links_lock); | |
193 | } | |
194 | ||
68464d79 | 195 | int device_links_read_lock(void) __acquires(&device_links_srcu) |
9ed98953 RW |
196 | { |
197 | return srcu_read_lock(&device_links_srcu); | |
198 | } | |
199 | ||
ab7789c5 | 200 | void device_links_read_unlock(int idx) __releases(&device_links_srcu) |
9ed98953 RW |
201 | { |
202 | srcu_read_unlock(&device_links_srcu, idx); | |
203 | } | |
c2fa1e1b JFG |
204 | |
205 | int device_links_read_lock_held(void) | |
206 | { | |
207 | return srcu_read_lock_held(&device_links_srcu); | |
208 | } | |
80dd33cf RW |
209 | |
210 | static void device_link_synchronize_removal(void) | |
211 | { | |
212 | synchronize_srcu(&device_links_srcu); | |
213 | } | |
0c871315 RW |
214 | |
215 | static void device_link_remove_from_lists(struct device_link *link) | |
216 | { | |
217 | list_del_rcu(&link->s_node); | |
218 | list_del_rcu(&link->c_node); | |
219 | } | |
9ed98953 RW |
220 | #else /* !CONFIG_SRCU */ |
221 | static DECLARE_RWSEM(device_links_lock); | |
222 | ||
223 | static inline void device_links_write_lock(void) | |
224 | { | |
225 | down_write(&device_links_lock); | |
226 | } | |
227 | ||
228 | static inline void device_links_write_unlock(void) | |
229 | { | |
230 | up_write(&device_links_lock); | |
231 | } | |
232 | ||
233 | int device_links_read_lock(void) | |
234 | { | |
235 | down_read(&device_links_lock); | |
236 | return 0; | |
237 | } | |
238 | ||
239 | void device_links_read_unlock(int not_used) | |
240 | { | |
241 | up_read(&device_links_lock); | |
242 | } | |
c2fa1e1b JFG |
243 | |
244 | #ifdef CONFIG_DEBUG_LOCK_ALLOC | |
245 | int device_links_read_lock_held(void) | |
246 | { | |
247 | return lockdep_is_held(&device_links_lock); | |
248 | } | |
249 | #endif | |
80dd33cf RW |
250 | |
251 | static inline void device_link_synchronize_removal(void) | |
252 | { | |
253 | } | |
0c871315 RW |
254 | |
255 | static void device_link_remove_from_lists(struct device_link *link) | |
256 | { | |
257 | list_del(&link->s_node); | |
258 | list_del(&link->c_node); | |
259 | } | |
9ed98953 RW |
260 | #endif /* !CONFIG_SRCU */ |
261 | ||
3d1cf435 RW |
262 | static bool device_is_ancestor(struct device *dev, struct device *target) |
263 | { | |
264 | while (target->parent) { | |
265 | target = target->parent; | |
266 | if (dev == target) | |
267 | return true; | |
268 | } | |
269 | return false; | |
270 | } | |
271 | ||
9ed98953 RW |
272 | /** |
273 | * device_is_dependent - Check if one device depends on another one | |
274 | * @dev: Device to check dependencies for. | |
275 | * @target: Device to check against. | |
276 | * | |
277 | * Check if @target depends on @dev or any device dependent on it (its child or | |
278 | * its consumer etc). Return 1 if that is the case or 0 otherwise. | |
279 | */ | |
7d34ca38 | 280 | int device_is_dependent(struct device *dev, void *target) |
9ed98953 RW |
281 | { |
282 | struct device_link *link; | |
283 | int ret; | |
284 | ||
3d1cf435 RW |
285 | /* |
286 | * The "ancestors" check is needed to catch the case when the target | |
287 | * device has not been completely initialized yet and it is still | |
288 | * missing from the list of children of its parent device. | |
289 | */ | |
290 | if (dev == target || device_is_ancestor(dev, target)) | |
9ed98953 RW |
291 | return 1; |
292 | ||
293 | ret = device_for_each_child(dev, target, device_is_dependent); | |
294 | if (ret) | |
295 | return ret; | |
296 | ||
297 | list_for_each_entry(link, &dev->links.consumers, s_node) { | |
4b9bbb29 SK |
298 | if ((link->flags & ~DL_FLAG_INFERRED) == |
299 | (DL_FLAG_SYNC_STATE_ONLY | DL_FLAG_MANAGED)) | |
05ef983e SK |
300 | continue; |
301 | ||
e16f4f3e | 302 | if (link->consumer == target) |
9ed98953 RW |
303 | return 1; |
304 | ||
305 | ret = device_is_dependent(link->consumer, target); | |
306 | if (ret) | |
307 | break; | |
308 | } | |
309 | return ret; | |
310 | } | |
311 | ||
515db266 RW |
312 | static void device_link_init_status(struct device_link *link, |
313 | struct device *consumer, | |
314 | struct device *supplier) | |
315 | { | |
316 | switch (supplier->links.status) { | |
317 | case DL_DEV_PROBING: | |
318 | switch (consumer->links.status) { | |
319 | case DL_DEV_PROBING: | |
320 | /* | |
321 | * A consumer driver can create a link to a supplier | |
322 | * that has not completed its probing yet as long as it | |
323 | * knows that the supplier is already functional (for | |
324 | * example, it has just acquired some resources from the | |
325 | * supplier). | |
326 | */ | |
327 | link->status = DL_STATE_CONSUMER_PROBE; | |
328 | break; | |
329 | default: | |
330 | link->status = DL_STATE_DORMANT; | |
331 | break; | |
332 | } | |
333 | break; | |
334 | case DL_DEV_DRIVER_BOUND: | |
335 | switch (consumer->links.status) { | |
336 | case DL_DEV_PROBING: | |
337 | link->status = DL_STATE_CONSUMER_PROBE; | |
338 | break; | |
339 | case DL_DEV_DRIVER_BOUND: | |
340 | link->status = DL_STATE_ACTIVE; | |
341 | break; | |
342 | default: | |
343 | link->status = DL_STATE_AVAILABLE; | |
344 | break; | |
345 | } | |
346 | break; | |
347 | case DL_DEV_UNBINDING: | |
348 | link->status = DL_STATE_SUPPLIER_UNBIND; | |
349 | break; | |
350 | default: | |
351 | link->status = DL_STATE_DORMANT; | |
352 | break; | |
353 | } | |
354 | } | |
355 | ||
9ed98953 RW |
356 | static int device_reorder_to_tail(struct device *dev, void *not_used) |
357 | { | |
358 | struct device_link *link; | |
359 | ||
360 | /* | |
361 | * Devices that have not been registered yet will be put to the ends | |
362 | * of the lists during the registration, so skip them here. | |
363 | */ | |
364 | if (device_is_registered(dev)) | |
365 | devices_kset_move_last(dev); | |
366 | ||
367 | if (device_pm_initialized(dev)) | |
368 | device_pm_move_last(dev); | |
369 | ||
370 | device_for_each_child(dev, NULL, device_reorder_to_tail); | |
05ef983e | 371 | list_for_each_entry(link, &dev->links.consumers, s_node) { |
4b9bbb29 SK |
372 | if ((link->flags & ~DL_FLAG_INFERRED) == |
373 | (DL_FLAG_SYNC_STATE_ONLY | DL_FLAG_MANAGED)) | |
05ef983e | 374 | continue; |
9ed98953 | 375 | device_reorder_to_tail(link->consumer, NULL); |
05ef983e | 376 | } |
9ed98953 RW |
377 | |
378 | return 0; | |
379 | } | |
380 | ||
494fd7b7 FK |
381 | /** |
382 | * device_pm_move_to_tail - Move set of devices to the end of device lists | |
383 | * @dev: Device to move | |
384 | * | |
385 | * This is a device_reorder_to_tail() wrapper taking the requisite locks. | |
386 | * | |
387 | * It moves the @dev along with all of its children and all of its consumers | |
388 | * to the ends of the device_kset and dpm_list, recursively. | |
389 | */ | |
390 | void device_pm_move_to_tail(struct device *dev) | |
391 | { | |
392 | int idx; | |
393 | ||
394 | idx = device_links_read_lock(); | |
395 | device_pm_lock(); | |
396 | device_reorder_to_tail(dev, NULL); | |
397 | device_pm_unlock(); | |
398 | device_links_read_unlock(idx); | |
399 | } | |
400 | ||
287905e6 SK |
401 | #define to_devlink(dev) container_of((dev), struct device_link, link_dev) |
402 | ||
403 | static ssize_t status_show(struct device *dev, | |
948b3edb | 404 | struct device_attribute *attr, char *buf) |
287905e6 | 405 | { |
948b3edb | 406 | const char *output; |
287905e6 SK |
407 | |
408 | switch (to_devlink(dev)->status) { | |
409 | case DL_STATE_NONE: | |
948b3edb JP |
410 | output = "not tracked"; |
411 | break; | |
287905e6 | 412 | case DL_STATE_DORMANT: |
948b3edb JP |
413 | output = "dormant"; |
414 | break; | |
287905e6 | 415 | case DL_STATE_AVAILABLE: |
948b3edb JP |
416 | output = "available"; |
417 | break; | |
287905e6 | 418 | case DL_STATE_CONSUMER_PROBE: |
948b3edb JP |
419 | output = "consumer probing"; |
420 | break; | |
287905e6 | 421 | case DL_STATE_ACTIVE: |
948b3edb JP |
422 | output = "active"; |
423 | break; | |
287905e6 | 424 | case DL_STATE_SUPPLIER_UNBIND: |
948b3edb JP |
425 | output = "supplier unbinding"; |
426 | break; | |
287905e6 | 427 | default: |
948b3edb JP |
428 | output = "unknown"; |
429 | break; | |
287905e6 | 430 | } |
948b3edb JP |
431 | |
432 | return sysfs_emit(buf, "%s\n", output); | |
287905e6 SK |
433 | } |
434 | static DEVICE_ATTR_RO(status); | |
435 | ||
436 | static ssize_t auto_remove_on_show(struct device *dev, | |
437 | struct device_attribute *attr, char *buf) | |
438 | { | |
439 | struct device_link *link = to_devlink(dev); | |
973c3911 | 440 | const char *output; |
287905e6 SK |
441 | |
442 | if (link->flags & DL_FLAG_AUTOREMOVE_SUPPLIER) | |
973c3911 | 443 | output = "supplier unbind"; |
287905e6 | 444 | else if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER) |
973c3911 | 445 | output = "consumer unbind"; |
287905e6 | 446 | else |
973c3911 | 447 | output = "never"; |
287905e6 | 448 | |
973c3911 | 449 | return sysfs_emit(buf, "%s\n", output); |
287905e6 SK |
450 | } |
451 | static DEVICE_ATTR_RO(auto_remove_on); | |
452 | ||
453 | static ssize_t runtime_pm_show(struct device *dev, | |
454 | struct device_attribute *attr, char *buf) | |
455 | { | |
456 | struct device_link *link = to_devlink(dev); | |
457 | ||
aa838896 | 458 | return sysfs_emit(buf, "%d\n", !!(link->flags & DL_FLAG_PM_RUNTIME)); |
287905e6 SK |
459 | } |
460 | static DEVICE_ATTR_RO(runtime_pm); | |
461 | ||
462 | static ssize_t sync_state_only_show(struct device *dev, | |
463 | struct device_attribute *attr, char *buf) | |
464 | { | |
465 | struct device_link *link = to_devlink(dev); | |
466 | ||
aa838896 JP |
467 | return sysfs_emit(buf, "%d\n", |
468 | !!(link->flags & DL_FLAG_SYNC_STATE_ONLY)); | |
287905e6 SK |
469 | } |
470 | static DEVICE_ATTR_RO(sync_state_only); | |
471 | ||
472 | static struct attribute *devlink_attrs[] = { | |
473 | &dev_attr_status.attr, | |
474 | &dev_attr_auto_remove_on.attr, | |
475 | &dev_attr_runtime_pm.attr, | |
476 | &dev_attr_sync_state_only.attr, | |
477 | NULL, | |
478 | }; | |
479 | ATTRIBUTE_GROUPS(devlink); | |
480 | ||
80dd33cf | 481 | static void device_link_release_fn(struct work_struct *work) |
843e600b | 482 | { |
80dd33cf RW |
483 | struct device_link *link = container_of(work, struct device_link, rm_work); |
484 | ||
485 | /* Ensure that all references to the link object have been dropped. */ | |
486 | device_link_synchronize_removal(); | |
487 | ||
88b786e5 | 488 | pm_runtime_release_supplier(link, true); |
843e600b SK |
489 | |
490 | put_device(link->consumer); | |
491 | put_device(link->supplier); | |
492 | kfree(link); | |
493 | } | |
494 | ||
287905e6 SK |
495 | static void devlink_dev_release(struct device *dev) |
496 | { | |
843e600b SK |
497 | struct device_link *link = to_devlink(dev); |
498 | ||
80dd33cf RW |
499 | INIT_WORK(&link->rm_work, device_link_release_fn); |
500 | /* | |
501 | * It may take a while to complete this work because of the SRCU | |
502 | * synchronization in device_link_release_fn() and if the consumer or | |
503 | * supplier devices get deleted when it runs, so put it into the "long" | |
504 | * workqueue. | |
505 | */ | |
506 | queue_work(system_long_wq, &link->rm_work); | |
843e600b | 507 | } |
287905e6 SK |
508 | |
509 | static struct class devlink_class = { | |
510 | .name = "devlink", | |
511 | .owner = THIS_MODULE, | |
512 | .dev_groups = devlink_groups, | |
513 | .dev_release = devlink_dev_release, | |
514 | }; | |
515 | ||
516 | static int devlink_add_symlinks(struct device *dev, | |
517 | struct class_interface *class_intf) | |
518 | { | |
519 | int ret; | |
520 | size_t len; | |
521 | struct device_link *link = to_devlink(dev); | |
522 | struct device *sup = link->supplier; | |
523 | struct device *con = link->consumer; | |
524 | char *buf; | |
525 | ||
e020ff61 SK |
526 | len = max(strlen(dev_bus_name(sup)) + strlen(dev_name(sup)), |
527 | strlen(dev_bus_name(con)) + strlen(dev_name(con))); | |
528 | len += strlen(":"); | |
287905e6 SK |
529 | len += strlen("supplier:") + 1; |
530 | buf = kzalloc(len, GFP_KERNEL); | |
531 | if (!buf) | |
532 | return -ENOMEM; | |
533 | ||
534 | ret = sysfs_create_link(&link->link_dev.kobj, &sup->kobj, "supplier"); | |
535 | if (ret) | |
536 | goto out; | |
537 | ||
538 | ret = sysfs_create_link(&link->link_dev.kobj, &con->kobj, "consumer"); | |
539 | if (ret) | |
540 | goto err_con; | |
541 | ||
e020ff61 | 542 | snprintf(buf, len, "consumer:%s:%s", dev_bus_name(con), dev_name(con)); |
287905e6 SK |
543 | ret = sysfs_create_link(&sup->kobj, &link->link_dev.kobj, buf); |
544 | if (ret) | |
545 | goto err_con_dev; | |
546 | ||
e020ff61 | 547 | snprintf(buf, len, "supplier:%s:%s", dev_bus_name(sup), dev_name(sup)); |
287905e6 SK |
548 | ret = sysfs_create_link(&con->kobj, &link->link_dev.kobj, buf); |
549 | if (ret) | |
550 | goto err_sup_dev; | |
551 | ||
552 | goto out; | |
553 | ||
554 | err_sup_dev: | |
e020ff61 | 555 | snprintf(buf, len, "consumer:%s:%s", dev_bus_name(con), dev_name(con)); |
287905e6 SK |
556 | sysfs_remove_link(&sup->kobj, buf); |
557 | err_con_dev: | |
558 | sysfs_remove_link(&link->link_dev.kobj, "consumer"); | |
559 | err_con: | |
560 | sysfs_remove_link(&link->link_dev.kobj, "supplier"); | |
561 | out: | |
562 | kfree(buf); | |
563 | return ret; | |
564 | } | |
565 | ||
566 | static void devlink_remove_symlinks(struct device *dev, | |
567 | struct class_interface *class_intf) | |
568 | { | |
569 | struct device_link *link = to_devlink(dev); | |
570 | size_t len; | |
571 | struct device *sup = link->supplier; | |
572 | struct device *con = link->consumer; | |
573 | char *buf; | |
574 | ||
575 | sysfs_remove_link(&link->link_dev.kobj, "consumer"); | |
576 | sysfs_remove_link(&link->link_dev.kobj, "supplier"); | |
577 | ||
e020ff61 SK |
578 | len = max(strlen(dev_bus_name(sup)) + strlen(dev_name(sup)), |
579 | strlen(dev_bus_name(con)) + strlen(dev_name(con))); | |
580 | len += strlen(":"); | |
287905e6 SK |
581 | len += strlen("supplier:") + 1; |
582 | buf = kzalloc(len, GFP_KERNEL); | |
583 | if (!buf) { | |
584 | WARN(1, "Unable to properly free device link symlinks!\n"); | |
585 | return; | |
586 | } | |
587 | ||
e64daad6 AH |
588 | if (device_is_registered(con)) { |
589 | snprintf(buf, len, "supplier:%s:%s", dev_bus_name(sup), dev_name(sup)); | |
590 | sysfs_remove_link(&con->kobj, buf); | |
591 | } | |
e020ff61 | 592 | snprintf(buf, len, "consumer:%s:%s", dev_bus_name(con), dev_name(con)); |
287905e6 SK |
593 | sysfs_remove_link(&sup->kobj, buf); |
594 | kfree(buf); | |
595 | } | |
596 | ||
597 | static struct class_interface devlink_class_intf = { | |
598 | .class = &devlink_class, | |
599 | .add_dev = devlink_add_symlinks, | |
600 | .remove_dev = devlink_remove_symlinks, | |
601 | }; | |
602 | ||
603 | static int __init devlink_class_init(void) | |
604 | { | |
605 | int ret; | |
606 | ||
607 | ret = class_register(&devlink_class); | |
608 | if (ret) | |
609 | return ret; | |
610 | ||
611 | ret = class_interface_register(&devlink_class_intf); | |
612 | if (ret) | |
613 | class_unregister(&devlink_class); | |
614 | ||
615 | return ret; | |
616 | } | |
617 | postcore_initcall(devlink_class_init); | |
618 | ||
515db266 RW |
619 | #define DL_MANAGED_LINK_FLAGS (DL_FLAG_AUTOREMOVE_CONSUMER | \ |
620 | DL_FLAG_AUTOREMOVE_SUPPLIER | \ | |
05ef983e | 621 | DL_FLAG_AUTOPROBE_CONSUMER | \ |
4b9bbb29 SK |
622 | DL_FLAG_SYNC_STATE_ONLY | \ |
623 | DL_FLAG_INFERRED) | |
515db266 | 624 | |
fb583c8e RW |
625 | #define DL_ADD_VALID_FLAGS (DL_MANAGED_LINK_FLAGS | DL_FLAG_STATELESS | \ |
626 | DL_FLAG_PM_RUNTIME | DL_FLAG_RPM_ACTIVE) | |
627 | ||
9ed98953 RW |
628 | /** |
629 | * device_link_add - Create a link between two devices. | |
630 | * @consumer: Consumer end of the link. | |
631 | * @supplier: Supplier end of the link. | |
632 | * @flags: Link flags. | |
633 | * | |
21d5c57b RW |
634 | * The caller is responsible for the proper synchronization of the link creation |
635 | * with runtime PM. First, setting the DL_FLAG_PM_RUNTIME flag will cause the | |
636 | * runtime PM framework to take the link into account. Second, if the | |
637 | * DL_FLAG_RPM_ACTIVE flag is set in addition to it, the supplier devices will | |
d475f8ea | 638 | * be forced into the active meta state and reference-counted upon the creation |
21d5c57b RW |
639 | * of the link. If DL_FLAG_PM_RUNTIME is not set, DL_FLAG_RPM_ACTIVE will be |
640 | * ignored. | |
641 | * | |
515db266 RW |
642 | * If DL_FLAG_STATELESS is set in @flags, the caller of this function is |
643 | * expected to release the link returned by it directly with the help of either | |
644 | * device_link_del() or device_link_remove(). | |
72175d4e RW |
645 | * |
646 | * If that flag is not set, however, the caller of this function is handing the | |
647 | * management of the link over to the driver core entirely and its return value | |
648 | * can only be used to check whether or not the link is present. In that case, | |
649 | * the DL_FLAG_AUTOREMOVE_CONSUMER and DL_FLAG_AUTOREMOVE_SUPPLIER device link | |
650 | * flags can be used to indicate to the driver core when the link can be safely | |
651 | * deleted. Namely, setting one of them in @flags indicates to the driver core | |
652 | * that the link is not going to be used (by the given caller of this function) | |
653 | * after unbinding the consumer or supplier driver, respectively, from its | |
654 | * device, so the link can be deleted at that point. If none of them is set, | |
655 | * the link will be maintained until one of the devices pointed to by it (either | |
656 | * the consumer or the supplier) is unregistered. | |
c8d50986 | 657 | * |
e7dd4010 RW |
658 | * Also, if DL_FLAG_STATELESS, DL_FLAG_AUTOREMOVE_CONSUMER and |
659 | * DL_FLAG_AUTOREMOVE_SUPPLIER are not set in @flags (that is, a persistent | |
660 | * managed device link is being added), the DL_FLAG_AUTOPROBE_CONSUMER flag can | |
d475f8ea | 661 | * be used to request the driver core to automatically probe for a consumer |
e7dd4010 RW |
662 | * driver after successfully binding a driver to the supplier device. |
663 | * | |
515db266 RW |
664 | * The combination of DL_FLAG_STATELESS and one of DL_FLAG_AUTOREMOVE_CONSUMER, |
665 | * DL_FLAG_AUTOREMOVE_SUPPLIER, or DL_FLAG_AUTOPROBE_CONSUMER set in @flags at | |
666 | * the same time is invalid and will cause NULL to be returned upfront. | |
667 | * However, if a device link between the given @consumer and @supplier pair | |
668 | * exists already when this function is called for them, the existing link will | |
669 | * be returned regardless of its current type and status (the link's flags may | |
670 | * be modified then). The caller of this function is then expected to treat | |
671 | * the link as though it has just been created, so (in particular) if | |
672 | * DL_FLAG_STATELESS was passed in @flags, the link needs to be released | |
673 | * explicitly when not needed any more (as stated above). | |
9ed98953 RW |
674 | * |
675 | * A side effect of the link creation is re-ordering of dpm_list and the | |
676 | * devices_kset list by moving the consumer device and all devices depending | |
677 | * on it to the ends of these lists (that does not happen to devices that have | |
678 | * not been registered when this function is called). | |
679 | * | |
680 | * The supplier device is required to be registered when this function is called | |
681 | * and NULL will be returned if that is not the case. The consumer device need | |
64df1148 | 682 | * not be registered, however. |
9ed98953 RW |
683 | */ |
684 | struct device_link *device_link_add(struct device *consumer, | |
685 | struct device *supplier, u32 flags) | |
686 | { | |
687 | struct device_link *link; | |
688 | ||
f729a592 SK |
689 | if (!consumer || !supplier || consumer == supplier || |
690 | flags & ~DL_ADD_VALID_FLAGS || | |
515db266 | 691 | (flags & DL_FLAG_STATELESS && flags & DL_MANAGED_LINK_FLAGS) || |
05ef983e | 692 | (flags & DL_FLAG_SYNC_STATE_ONLY && |
4b9bbb29 | 693 | (flags & ~DL_FLAG_INFERRED) != DL_FLAG_SYNC_STATE_ONLY) || |
e7dd4010 RW |
694 | (flags & DL_FLAG_AUTOPROBE_CONSUMER && |
695 | flags & (DL_FLAG_AUTOREMOVE_CONSUMER | | |
696 | DL_FLAG_AUTOREMOVE_SUPPLIER))) | |
9ed98953 RW |
697 | return NULL; |
698 | ||
5db25c9e RW |
699 | if (flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) { |
700 | if (pm_runtime_get_sync(supplier) < 0) { | |
701 | pm_runtime_put_noidle(supplier); | |
702 | return NULL; | |
703 | } | |
5db25c9e RW |
704 | } |
705 | ||
515db266 RW |
706 | if (!(flags & DL_FLAG_STATELESS)) |
707 | flags |= DL_FLAG_MANAGED; | |
708 | ||
9ed98953 RW |
709 | device_links_write_lock(); |
710 | device_pm_lock(); | |
711 | ||
712 | /* | |
713 | * If the supplier has not been fully registered yet or there is a | |
05ef983e SK |
714 | * reverse (non-SYNC_STATE_ONLY) dependency between the consumer and |
715 | * the supplier already in the graph, return NULL. If the link is a | |
716 | * SYNC_STATE_ONLY link, we don't check for reverse dependencies | |
717 | * because it only affects sync_state() callbacks. | |
9ed98953 RW |
718 | */ |
719 | if (!device_pm_initialized(supplier) | |
05ef983e SK |
720 | || (!(flags & DL_FLAG_SYNC_STATE_ONLY) && |
721 | device_is_dependent(consumer, supplier))) { | |
9ed98953 RW |
722 | link = NULL; |
723 | goto out; | |
724 | } | |
725 | ||
ac66c5bb SK |
726 | /* |
727 | * SYNC_STATE_ONLY links are useless once a consumer device has probed. | |
728 | * So, only create it if the consumer hasn't probed yet. | |
729 | */ | |
730 | if (flags & DL_FLAG_SYNC_STATE_ONLY && | |
731 | consumer->links.status != DL_DEV_NO_DRIVER && | |
732 | consumer->links.status != DL_DEV_PROBING) { | |
733 | link = NULL; | |
734 | goto out; | |
735 | } | |
736 | ||
72175d4e RW |
737 | /* |
738 | * DL_FLAG_AUTOREMOVE_SUPPLIER indicates that the link will be needed | |
739 | * longer than for DL_FLAG_AUTOREMOVE_CONSUMER and setting them both | |
740 | * together doesn't make sense, so prefer DL_FLAG_AUTOREMOVE_SUPPLIER. | |
741 | */ | |
742 | if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER) | |
743 | flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER; | |
744 | ||
f265df55 RW |
745 | list_for_each_entry(link, &supplier->links.consumers, s_node) { |
746 | if (link->consumer != consumer) | |
747 | continue; | |
748 | ||
4b9bbb29 SK |
749 | if (link->flags & DL_FLAG_INFERRED && |
750 | !(flags & DL_FLAG_INFERRED)) | |
751 | link->flags &= ~DL_FLAG_INFERRED; | |
752 | ||
e2f3cd83 RW |
753 | if (flags & DL_FLAG_PM_RUNTIME) { |
754 | if (!(link->flags & DL_FLAG_PM_RUNTIME)) { | |
4c06c4e6 | 755 | pm_runtime_new_link(consumer); |
e2f3cd83 RW |
756 | link->flags |= DL_FLAG_PM_RUNTIME; |
757 | } | |
758 | if (flags & DL_FLAG_RPM_ACTIVE) | |
36003d4c | 759 | refcount_inc(&link->rpm_active); |
e2f3cd83 RW |
760 | } |
761 | ||
72175d4e RW |
762 | if (flags & DL_FLAG_STATELESS) { |
763 | kref_get(&link->kref); | |
05ef983e | 764 | if (link->flags & DL_FLAG_SYNC_STATE_ONLY && |
44e96049 SK |
765 | !(link->flags & DL_FLAG_STATELESS)) { |
766 | link->flags |= DL_FLAG_STATELESS; | |
05ef983e | 767 | goto reorder; |
44e96049 SK |
768 | } else { |
769 | link->flags |= DL_FLAG_STATELESS; | |
05ef983e | 770 | goto out; |
44e96049 | 771 | } |
72175d4e RW |
772 | } |
773 | ||
774 | /* | |
775 | * If the life time of the link following from the new flags is | |
776 | * longer than indicated by the flags of the existing link, | |
777 | * update the existing link to stay around longer. | |
778 | */ | |
779 | if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER) { | |
780 | if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER) { | |
781 | link->flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER; | |
782 | link->flags |= DL_FLAG_AUTOREMOVE_SUPPLIER; | |
783 | } | |
784 | } else if (!(flags & DL_FLAG_AUTOREMOVE_CONSUMER)) { | |
785 | link->flags &= ~(DL_FLAG_AUTOREMOVE_CONSUMER | | |
786 | DL_FLAG_AUTOREMOVE_SUPPLIER); | |
787 | } | |
515db266 RW |
788 | if (!(link->flags & DL_FLAG_MANAGED)) { |
789 | kref_get(&link->kref); | |
790 | link->flags |= DL_FLAG_MANAGED; | |
791 | device_link_init_status(link, consumer, supplier); | |
792 | } | |
05ef983e SK |
793 | if (link->flags & DL_FLAG_SYNC_STATE_ONLY && |
794 | !(flags & DL_FLAG_SYNC_STATE_ONLY)) { | |
795 | link->flags &= ~DL_FLAG_SYNC_STATE_ONLY; | |
796 | goto reorder; | |
797 | } | |
798 | ||
f265df55 RW |
799 | goto out; |
800 | } | |
801 | ||
21d5c57b | 802 | link = kzalloc(sizeof(*link), GFP_KERNEL); |
9ed98953 RW |
803 | if (!link) |
804 | goto out; | |
805 | ||
e2f3cd83 RW |
806 | refcount_set(&link->rpm_active, 1); |
807 | ||
9ed98953 RW |
808 | get_device(supplier); |
809 | link->supplier = supplier; | |
810 | INIT_LIST_HEAD(&link->s_node); | |
811 | get_device(consumer); | |
812 | link->consumer = consumer; | |
813 | INIT_LIST_HEAD(&link->c_node); | |
814 | link->flags = flags; | |
ead18c23 | 815 | kref_init(&link->kref); |
9ed98953 | 816 | |
287905e6 SK |
817 | link->link_dev.class = &devlink_class; |
818 | device_set_pm_not_required(&link->link_dev); | |
e020ff61 SK |
819 | dev_set_name(&link->link_dev, "%s:%s--%s:%s", |
820 | dev_bus_name(supplier), dev_name(supplier), | |
821 | dev_bus_name(consumer), dev_name(consumer)); | |
287905e6 | 822 | if (device_register(&link->link_dev)) { |
988dadd1 | 823 | put_device(&link->link_dev); |
287905e6 SK |
824 | link = NULL; |
825 | goto out; | |
826 | } | |
827 | ||
828 | if (flags & DL_FLAG_PM_RUNTIME) { | |
829 | if (flags & DL_FLAG_RPM_ACTIVE) | |
830 | refcount_inc(&link->rpm_active); | |
831 | ||
832 | pm_runtime_new_link(consumer); | |
833 | } | |
834 | ||
64df1148 | 835 | /* Determine the initial link state. */ |
515db266 | 836 | if (flags & DL_FLAG_STATELESS) |
9ed98953 | 837 | link->status = DL_STATE_NONE; |
515db266 RW |
838 | else |
839 | device_link_init_status(link, consumer, supplier); | |
9ed98953 | 840 | |
15cfb094 RW |
841 | /* |
842 | * Some callers expect the link creation during consumer driver probe to | |
843 | * resume the supplier even without DL_FLAG_RPM_ACTIVE. | |
844 | */ | |
845 | if (link->status == DL_STATE_CONSUMER_PROBE && | |
846 | flags & DL_FLAG_PM_RUNTIME) | |
847 | pm_runtime_resume(supplier); | |
848 | ||
21c27f06 SK |
849 | list_add_tail_rcu(&link->s_node, &supplier->links.consumers); |
850 | list_add_tail_rcu(&link->c_node, &consumer->links.suppliers); | |
851 | ||
05ef983e SK |
852 | if (flags & DL_FLAG_SYNC_STATE_ONLY) { |
853 | dev_dbg(consumer, | |
854 | "Linked as a sync state only consumer to %s\n", | |
855 | dev_name(supplier)); | |
856 | goto out; | |
857 | } | |
21c27f06 | 858 | |
05ef983e | 859 | reorder: |
9ed98953 RW |
860 | /* |
861 | * Move the consumer and all of the devices depending on it to the end | |
862 | * of dpm_list and the devices_kset list. | |
863 | * | |
864 | * It is necessary to hold dpm_list locked throughout all that or else | |
865 | * we may end up suspending with a wrong ordering of it. | |
866 | */ | |
867 | device_reorder_to_tail(consumer, NULL); | |
868 | ||
8a4b3269 | 869 | dev_dbg(consumer, "Linked as a consumer to %s\n", dev_name(supplier)); |
9ed98953 | 870 | |
21c27f06 | 871 | out: |
9ed98953 RW |
872 | device_pm_unlock(); |
873 | device_links_write_unlock(); | |
5db25c9e | 874 | |
e2f3cd83 | 875 | if ((flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) && !link) |
5db25c9e RW |
876 | pm_runtime_put(supplier); |
877 | ||
9ed98953 RW |
878 | return link; |
879 | } | |
880 | EXPORT_SYMBOL_GPL(device_link_add); | |
881 | ||
ead18c23 | 882 | static void __device_link_del(struct kref *kref) |
9ed98953 | 883 | { |
ead18c23 LW |
884 | struct device_link *link = container_of(kref, struct device_link, kref); |
885 | ||
8a4b3269 JB |
886 | dev_dbg(link->consumer, "Dropping the link to %s\n", |
887 | dev_name(link->supplier)); | |
9ed98953 | 888 | |
e0e398e2 | 889 | pm_runtime_drop_link(link); |
baa8809f | 890 | |
0c871315 | 891 | device_link_remove_from_lists(link); |
843e600b | 892 | device_unregister(&link->link_dev); |
9ed98953 | 893 | } |
9ed98953 | 894 | |
72175d4e RW |
895 | static void device_link_put_kref(struct device_link *link) |
896 | { | |
897 | if (link->flags & DL_FLAG_STATELESS) | |
898 | kref_put(&link->kref, __device_link_del); | |
bf25967a AH |
899 | else if (!device_is_registered(link->consumer)) |
900 | __device_link_del(&link->kref); | |
72175d4e RW |
901 | else |
902 | WARN(1, "Unable to drop a managed device link reference\n"); | |
903 | } | |
904 | ||
9ed98953 | 905 | /** |
72175d4e | 906 | * device_link_del - Delete a stateless link between two devices. |
9ed98953 RW |
907 | * @link: Device link to delete. |
908 | * | |
909 | * The caller must ensure proper synchronization of this function with runtime | |
ead18c23 LW |
910 | * PM. If the link was added multiple times, it needs to be deleted as often. |
911 | * Care is required for hotplugged devices: Their links are purged on removal | |
912 | * and calling device_link_del() is then no longer allowed. | |
9ed98953 RW |
913 | */ |
914 | void device_link_del(struct device_link *link) | |
915 | { | |
916 | device_links_write_lock(); | |
72175d4e | 917 | device_link_put_kref(link); |
9ed98953 RW |
918 | device_links_write_unlock(); |
919 | } | |
920 | EXPORT_SYMBOL_GPL(device_link_del); | |
921 | ||
d8842211 | 922 | /** |
72175d4e | 923 | * device_link_remove - Delete a stateless link between two devices. |
d8842211 | 924 | * @consumer: Consumer end of the link. |
925 | * @supplier: Supplier end of the link. | |
926 | * | |
927 | * The caller must ensure proper synchronization of this function with runtime | |
928 | * PM. | |
929 | */ | |
930 | void device_link_remove(void *consumer, struct device *supplier) | |
931 | { | |
932 | struct device_link *link; | |
933 | ||
934 | if (WARN_ON(consumer == supplier)) | |
935 | return; | |
936 | ||
937 | device_links_write_lock(); | |
d8842211 | 938 | |
939 | list_for_each_entry(link, &supplier->links.consumers, s_node) { | |
940 | if (link->consumer == consumer) { | |
72175d4e | 941 | device_link_put_kref(link); |
d8842211 | 942 | break; |
943 | } | |
944 | } | |
945 | ||
d8842211 | 946 | device_links_write_unlock(); |
947 | } | |
948 | EXPORT_SYMBOL_GPL(device_link_remove); | |
949 | ||
9ed98953 RW |
950 | static void device_links_missing_supplier(struct device *dev) |
951 | { | |
952 | struct device_link *link; | |
953 | ||
8c3e315d SK |
954 | list_for_each_entry(link, &dev->links.suppliers, c_node) { |
955 | if (link->status != DL_STATE_CONSUMER_PROBE) | |
956 | continue; | |
957 | ||
958 | if (link->supplier->links.status == DL_DEV_DRIVER_BOUND) { | |
9ed98953 | 959 | WRITE_ONCE(link->status, DL_STATE_AVAILABLE); |
8c3e315d SK |
960 | } else { |
961 | WARN_ON(!(link->flags & DL_FLAG_SYNC_STATE_ONLY)); | |
962 | WRITE_ONCE(link->status, DL_STATE_DORMANT); | |
963 | } | |
964 | } | |
9ed98953 RW |
965 | } |
966 | ||
967 | /** | |
968 | * device_links_check_suppliers - Check presence of supplier drivers. | |
969 | * @dev: Consumer device. | |
970 | * | |
971 | * Check links from this device to any suppliers. Walk the list of the device's | |
972 | * links to suppliers and see if all of them are available. If not, simply | |
973 | * return -EPROBE_DEFER. | |
974 | * | |
975 | * We need to guarantee that the supplier will not go away after the check has | |
976 | * been positive here. It only can go away in __device_release_driver() and | |
977 | * that function checks the device's links to consumers. This means we need to | |
978 | * mark the link as "consumer probe in progress" to make the supplier removal | |
979 | * wait for us to complete (or bad things may happen). | |
980 | * | |
515db266 | 981 | * Links without the DL_FLAG_MANAGED flag set are ignored. |
9ed98953 RW |
982 | */ |
983 | int device_links_check_suppliers(struct device *dev) | |
984 | { | |
985 | struct device_link *link; | |
986 | int ret = 0; | |
68223eee | 987 | struct fwnode_handle *sup_fw; |
9ed98953 | 988 | |
e2ae9bcc SK |
989 | /* |
990 | * Device waiting for supplier to become available is not allowed to | |
991 | * probe. | |
992 | */ | |
25ac86c6 SK |
993 | mutex_lock(&fwnode_link_lock); |
994 | if (dev->fwnode && !list_empty(&dev->fwnode->suppliers) && | |
995 | !fw_devlink_is_permissive()) { | |
68223eee SK |
996 | sup_fw = list_first_entry(&dev->fwnode->suppliers, |
997 | struct fwnode_link, | |
998 | c_hook)->supplier; | |
999 | dev_err_probe(dev, -EPROBE_DEFER, "wait for supplier %pfwP\n", | |
1000 | sup_fw); | |
25ac86c6 | 1001 | mutex_unlock(&fwnode_link_lock); |
e2ae9bcc SK |
1002 | return -EPROBE_DEFER; |
1003 | } | |
25ac86c6 | 1004 | mutex_unlock(&fwnode_link_lock); |
e2ae9bcc | 1005 | |
9ed98953 RW |
1006 | device_links_write_lock(); |
1007 | ||
1008 | list_for_each_entry(link, &dev->links.suppliers, c_node) { | |
8c3e315d | 1009 | if (!(link->flags & DL_FLAG_MANAGED)) |
9ed98953 RW |
1010 | continue; |
1011 | ||
8c3e315d SK |
1012 | if (link->status != DL_STATE_AVAILABLE && |
1013 | !(link->flags & DL_FLAG_SYNC_STATE_ONLY)) { | |
9ed98953 | 1014 | device_links_missing_supplier(dev); |
68223eee SK |
1015 | dev_err_probe(dev, -EPROBE_DEFER, |
1016 | "supplier %s not ready\n", | |
1017 | dev_name(link->supplier)); | |
9ed98953 RW |
1018 | ret = -EPROBE_DEFER; |
1019 | break; | |
1020 | } | |
1021 | WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE); | |
1022 | } | |
1023 | dev->links.status = DL_DEV_PROBING; | |
1024 | ||
1025 | device_links_write_unlock(); | |
1026 | return ret; | |
1027 | } | |
1028 | ||
26e77708 SK |
1029 | /** |
1030 | * __device_links_queue_sync_state - Queue a device for sync_state() callback | |
1031 | * @dev: Device to call sync_state() on | |
1032 | * @list: List head to queue the @dev on | |
1033 | * | |
1034 | * Queues a device for a sync_state() callback when the device links write lock | |
1035 | * isn't held. This allows the sync_state() execution flow to use device links | |
1036 | * APIs. The caller must ensure this function is called with | |
1037 | * device_links_write_lock() held. | |
1038 | * | |
1039 | * This function does a get_device() to make sure the device is not freed while | |
1040 | * on this list. | |
1041 | * | |
1042 | * So the caller must also ensure that device_links_flush_sync_list() is called | |
1043 | * as soon as the caller releases device_links_write_lock(). This is necessary | |
1044 | * to make sure the sync_state() is called in a timely fashion and the | |
1045 | * put_device() is called on this device. | |
1046 | */ | |
1047 | static void __device_links_queue_sync_state(struct device *dev, | |
1048 | struct list_head *list) | |
fc5a251d SK |
1049 | { |
1050 | struct device_link *link; | |
1051 | ||
77036165 SK |
1052 | if (!dev_has_sync_state(dev)) |
1053 | return; | |
fc5a251d SK |
1054 | if (dev->state_synced) |
1055 | return; | |
1056 | ||
1057 | list_for_each_entry(link, &dev->links.consumers, s_node) { | |
1058 | if (!(link->flags & DL_FLAG_MANAGED)) | |
1059 | continue; | |
1060 | if (link->status != DL_STATE_ACTIVE) | |
1061 | return; | |
1062 | } | |
1063 | ||
26e77708 SK |
1064 | /* |
1065 | * Set the flag here to avoid adding the same device to a list more | |
1066 | * than once. This can happen if new consumers get added to the device | |
1067 | * and probed before the list is flushed. | |
1068 | */ | |
fc5a251d | 1069 | dev->state_synced = true; |
26e77708 | 1070 | |
3b052a3e | 1071 | if (WARN_ON(!list_empty(&dev->links.defer_sync))) |
26e77708 SK |
1072 | return; |
1073 | ||
1074 | get_device(dev); | |
3b052a3e | 1075 | list_add_tail(&dev->links.defer_sync, list); |
26e77708 SK |
1076 | } |
1077 | ||
1078 | /** | |
1079 | * device_links_flush_sync_list - Call sync_state() on a list of devices | |
1080 | * @list: List of devices to call sync_state() on | |
21eb93f4 | 1081 | * @dont_lock_dev: Device for which lock is already held by the caller |
26e77708 SK |
1082 | * |
1083 | * Calls sync_state() on all the devices that have been queued for it. This | |
21eb93f4 SK |
1084 | * function is used in conjunction with __device_links_queue_sync_state(). The |
1085 | * @dont_lock_dev parameter is useful when this function is called from a | |
1086 | * context where a device lock is already held. | |
26e77708 | 1087 | */ |
21eb93f4 SK |
1088 | static void device_links_flush_sync_list(struct list_head *list, |
1089 | struct device *dont_lock_dev) | |
26e77708 SK |
1090 | { |
1091 | struct device *dev, *tmp; | |
1092 | ||
3b052a3e SK |
1093 | list_for_each_entry_safe(dev, tmp, list, links.defer_sync) { |
1094 | list_del_init(&dev->links.defer_sync); | |
26e77708 | 1095 | |
21eb93f4 SK |
1096 | if (dev != dont_lock_dev) |
1097 | device_lock(dev); | |
26e77708 SK |
1098 | |
1099 | if (dev->bus->sync_state) | |
1100 | dev->bus->sync_state(dev); | |
1101 | else if (dev->driver && dev->driver->sync_state) | |
1102 | dev->driver->sync_state(dev); | |
1103 | ||
21eb93f4 SK |
1104 | if (dev != dont_lock_dev) |
1105 | device_unlock(dev); | |
26e77708 SK |
1106 | |
1107 | put_device(dev); | |
1108 | } | |
fc5a251d SK |
1109 | } |
1110 | ||
1111 | void device_links_supplier_sync_state_pause(void) | |
1112 | { | |
1113 | device_links_write_lock(); | |
1114 | defer_sync_state_count++; | |
1115 | device_links_write_unlock(); | |
1116 | } | |
1117 | ||
1118 | void device_links_supplier_sync_state_resume(void) | |
1119 | { | |
1120 | struct device *dev, *tmp; | |
26e77708 | 1121 | LIST_HEAD(sync_list); |
fc5a251d SK |
1122 | |
1123 | device_links_write_lock(); | |
1124 | if (!defer_sync_state_count) { | |
1125 | WARN(true, "Unmatched sync_state pause/resume!"); | |
1126 | goto out; | |
1127 | } | |
1128 | defer_sync_state_count--; | |
1129 | if (defer_sync_state_count) | |
1130 | goto out; | |
1131 | ||
3b052a3e | 1132 | list_for_each_entry_safe(dev, tmp, &deferred_sync, links.defer_sync) { |
26e77708 SK |
1133 | /* |
1134 | * Delete from deferred_sync list before queuing it to | |
3b052a3e | 1135 | * sync_list because defer_sync is used for both lists. |
26e77708 | 1136 | */ |
3b052a3e | 1137 | list_del_init(&dev->links.defer_sync); |
26e77708 | 1138 | __device_links_queue_sync_state(dev, &sync_list); |
fc5a251d SK |
1139 | } |
1140 | out: | |
1141 | device_links_write_unlock(); | |
26e77708 | 1142 | |
21eb93f4 | 1143 | device_links_flush_sync_list(&sync_list, NULL); |
fc5a251d SK |
1144 | } |
1145 | ||
1146 | static int sync_state_resume_initcall(void) | |
1147 | { | |
1148 | device_links_supplier_sync_state_resume(); | |
1149 | return 0; | |
1150 | } | |
1151 | late_initcall(sync_state_resume_initcall); | |
1152 | ||
1153 | static void __device_links_supplier_defer_sync(struct device *sup) | |
1154 | { | |
3b052a3e SK |
1155 | if (list_empty(&sup->links.defer_sync) && dev_has_sync_state(sup)) |
1156 | list_add_tail(&sup->links.defer_sync, &deferred_sync); | |
fc5a251d SK |
1157 | } |
1158 | ||
21c27f06 SK |
1159 | static void device_link_drop_managed(struct device_link *link) |
1160 | { | |
1161 | link->flags &= ~DL_FLAG_MANAGED; | |
1162 | WRITE_ONCE(link->status, DL_STATE_NONE); | |
1163 | kref_put(&link->kref, __device_link_del); | |
1164 | } | |
1165 | ||
da6d6475 SK |
1166 | static ssize_t waiting_for_supplier_show(struct device *dev, |
1167 | struct device_attribute *attr, | |
1168 | char *buf) | |
1169 | { | |
1170 | bool val; | |
1171 | ||
1172 | device_lock(dev); | |
25ac86c6 | 1173 | val = !list_empty(&dev->fwnode->suppliers); |
da6d6475 | 1174 | device_unlock(dev); |
aa838896 | 1175 | return sysfs_emit(buf, "%u\n", val); |
da6d6475 SK |
1176 | } |
1177 | static DEVICE_ATTR_RO(waiting_for_supplier); | |
1178 | ||
b6f617df SK |
1179 | /** |
1180 | * device_links_force_bind - Prepares device to be force bound | |
1181 | * @dev: Consumer device. | |
1182 | * | |
1183 | * device_bind_driver() force binds a device to a driver without calling any | |
1184 | * driver probe functions. So the consumer really isn't going to wait for any | |
1185 | * supplier before it's bound to the driver. We still want the device link | |
1186 | * states to be sensible when this happens. | |
1187 | * | |
1188 | * In preparation for device_bind_driver(), this function goes through each | |
1189 | * supplier device links and checks if the supplier is bound. If it is, then | |
1190 | * the device link status is set to CONSUMER_PROBE. Otherwise, the device link | |
1191 | * is dropped. Links without the DL_FLAG_MANAGED flag set are ignored. | |
1192 | */ | |
1193 | void device_links_force_bind(struct device *dev) | |
1194 | { | |
1195 | struct device_link *link, *ln; | |
1196 | ||
1197 | device_links_write_lock(); | |
1198 | ||
1199 | list_for_each_entry_safe(link, ln, &dev->links.suppliers, c_node) { | |
1200 | if (!(link->flags & DL_FLAG_MANAGED)) | |
1201 | continue; | |
1202 | ||
1203 | if (link->status != DL_STATE_AVAILABLE) { | |
1204 | device_link_drop_managed(link); | |
1205 | continue; | |
1206 | } | |
1207 | WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE); | |
1208 | } | |
1209 | dev->links.status = DL_DEV_PROBING; | |
1210 | ||
1211 | device_links_write_unlock(); | |
1212 | } | |
1213 | ||
9ed98953 RW |
1214 | /** |
1215 | * device_links_driver_bound - Update device links after probing its driver. | |
1216 | * @dev: Device to update the links for. | |
1217 | * | |
1218 | * The probe has been successful, so update links from this device to any | |
1219 | * consumers by changing their status to "available". | |
1220 | * | |
1221 | * Also change the status of @dev's links to suppliers to "active". | |
1222 | * | |
515db266 | 1223 | * Links without the DL_FLAG_MANAGED flag set are ignored. |
9ed98953 RW |
1224 | */ |
1225 | void device_links_driver_bound(struct device *dev) | |
1226 | { | |
21c27f06 | 1227 | struct device_link *link, *ln; |
26e77708 | 1228 | LIST_HEAD(sync_list); |
9ed98953 | 1229 | |
bcbbcfd5 | 1230 | /* |
9528e0d9 | 1231 | * If a device binds successfully, it's expected to have created all |
bcbbcfd5 | 1232 | * the device links it needs to or make new device links as it needs |
9528e0d9 SK |
1233 | * them. So, fw_devlink no longer needs to create device links to any |
1234 | * of the device's suppliers. | |
1235 | * | |
1236 | * Also, if a child firmware node of this bound device is not added as | |
1237 | * a device by now, assume it is never going to be added and make sure | |
1238 | * other devices don't defer probe indefinitely by waiting for such a | |
1239 | * child device. | |
bcbbcfd5 | 1240 | */ |
9528e0d9 SK |
1241 | if (dev->fwnode && dev->fwnode->dev == dev) { |
1242 | struct fwnode_handle *child; | |
f9aa4606 | 1243 | fwnode_links_purge_suppliers(dev->fwnode); |
9528e0d9 SK |
1244 | fwnode_for_each_available_child_node(dev->fwnode, child) |
1245 | fw_devlink_purge_absent_suppliers(child); | |
1246 | } | |
da6d6475 | 1247 | device_remove_file(dev, &dev_attr_waiting_for_supplier); |
bcbbcfd5 | 1248 | |
9ed98953 RW |
1249 | device_links_write_lock(); |
1250 | ||
1251 | list_for_each_entry(link, &dev->links.consumers, s_node) { | |
515db266 | 1252 | if (!(link->flags & DL_FLAG_MANAGED)) |
9ed98953 RW |
1253 | continue; |
1254 | ||
15cfb094 RW |
1255 | /* |
1256 | * Links created during consumer probe may be in the "consumer | |
1257 | * probe" state to start with if the supplier is still probing | |
1258 | * when they are created and they may become "active" if the | |
1259 | * consumer probe returns first. Skip them here. | |
1260 | */ | |
1261 | if (link->status == DL_STATE_CONSUMER_PROBE || | |
1262 | link->status == DL_STATE_ACTIVE) | |
1263 | continue; | |
1264 | ||
9ed98953 RW |
1265 | WARN_ON(link->status != DL_STATE_DORMANT); |
1266 | WRITE_ONCE(link->status, DL_STATE_AVAILABLE); | |
e7dd4010 RW |
1267 | |
1268 | if (link->flags & DL_FLAG_AUTOPROBE_CONSUMER) | |
1269 | driver_deferred_probe_add(link->consumer); | |
9ed98953 RW |
1270 | } |
1271 | ||
21eb93f4 SK |
1272 | if (defer_sync_state_count) |
1273 | __device_links_supplier_defer_sync(dev); | |
1274 | else | |
1275 | __device_links_queue_sync_state(dev, &sync_list); | |
1276 | ||
21c27f06 SK |
1277 | list_for_each_entry_safe(link, ln, &dev->links.suppliers, c_node) { |
1278 | struct device *supplier; | |
1279 | ||
515db266 | 1280 | if (!(link->flags & DL_FLAG_MANAGED)) |
9ed98953 RW |
1281 | continue; |
1282 | ||
21c27f06 SK |
1283 | supplier = link->supplier; |
1284 | if (link->flags & DL_FLAG_SYNC_STATE_ONLY) { | |
1285 | /* | |
1286 | * When DL_FLAG_SYNC_STATE_ONLY is set, it means no | |
1287 | * other DL_MANAGED_LINK_FLAGS have been set. So, it's | |
1288 | * save to drop the managed link completely. | |
1289 | */ | |
1290 | device_link_drop_managed(link); | |
1291 | } else { | |
1292 | WARN_ON(link->status != DL_STATE_CONSUMER_PROBE); | |
1293 | WRITE_ONCE(link->status, DL_STATE_ACTIVE); | |
1294 | } | |
fc5a251d | 1295 | |
21c27f06 SK |
1296 | /* |
1297 | * This needs to be done even for the deleted | |
1298 | * DL_FLAG_SYNC_STATE_ONLY device link in case it was the last | |
1299 | * device link that was preventing the supplier from getting a | |
1300 | * sync_state() call. | |
1301 | */ | |
fc5a251d | 1302 | if (defer_sync_state_count) |
21c27f06 | 1303 | __device_links_supplier_defer_sync(supplier); |
fc5a251d | 1304 | else |
21c27f06 | 1305 | __device_links_queue_sync_state(supplier, &sync_list); |
9ed98953 RW |
1306 | } |
1307 | ||
1308 | dev->links.status = DL_DEV_DRIVER_BOUND; | |
1309 | ||
1310 | device_links_write_unlock(); | |
26e77708 | 1311 | |
21eb93f4 | 1312 | device_links_flush_sync_list(&sync_list, dev); |
9ed98953 RW |
1313 | } |
1314 | ||
1315 | /** | |
1316 | * __device_links_no_driver - Update links of a device without a driver. | |
1317 | * @dev: Device without a drvier. | |
1318 | * | |
1319 | * Delete all non-persistent links from this device to any suppliers. | |
1320 | * | |
1321 | * Persistent links stay around, but their status is changed to "available", | |
1322 | * unless they already are in the "supplier unbind in progress" state in which | |
1323 | * case they need not be updated. | |
1324 | * | |
515db266 | 1325 | * Links without the DL_FLAG_MANAGED flag set are ignored. |
9ed98953 RW |
1326 | */ |
1327 | static void __device_links_no_driver(struct device *dev) | |
1328 | { | |
1329 | struct device_link *link, *ln; | |
1330 | ||
1331 | list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) { | |
515db266 | 1332 | if (!(link->flags & DL_FLAG_MANAGED)) |
9ed98953 RW |
1333 | continue; |
1334 | ||
8c3e315d | 1335 | if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER) { |
515db266 | 1336 | device_link_drop_managed(link); |
8c3e315d SK |
1337 | continue; |
1338 | } | |
1339 | ||
1340 | if (link->status != DL_STATE_CONSUMER_PROBE && | |
1341 | link->status != DL_STATE_ACTIVE) | |
1342 | continue; | |
1343 | ||
1344 | if (link->supplier->links.status == DL_DEV_DRIVER_BOUND) { | |
9ed98953 | 1345 | WRITE_ONCE(link->status, DL_STATE_AVAILABLE); |
8c3e315d SK |
1346 | } else { |
1347 | WARN_ON(!(link->flags & DL_FLAG_SYNC_STATE_ONLY)); | |
1348 | WRITE_ONCE(link->status, DL_STATE_DORMANT); | |
1349 | } | |
9ed98953 RW |
1350 | } |
1351 | ||
1352 | dev->links.status = DL_DEV_NO_DRIVER; | |
1353 | } | |
1354 | ||
15cfb094 RW |
1355 | /** |
1356 | * device_links_no_driver - Update links after failing driver probe. | |
1357 | * @dev: Device whose driver has just failed to probe. | |
1358 | * | |
1359 | * Clean up leftover links to consumers for @dev and invoke | |
1360 | * %__device_links_no_driver() to update links to suppliers for it as | |
1361 | * appropriate. | |
1362 | * | |
515db266 | 1363 | * Links without the DL_FLAG_MANAGED flag set are ignored. |
15cfb094 | 1364 | */ |
9ed98953 RW |
1365 | void device_links_no_driver(struct device *dev) |
1366 | { | |
15cfb094 RW |
1367 | struct device_link *link; |
1368 | ||
9ed98953 | 1369 | device_links_write_lock(); |
15cfb094 RW |
1370 | |
1371 | list_for_each_entry(link, &dev->links.consumers, s_node) { | |
515db266 | 1372 | if (!(link->flags & DL_FLAG_MANAGED)) |
15cfb094 RW |
1373 | continue; |
1374 | ||
1375 | /* | |
1376 | * The probe has failed, so if the status of the link is | |
1377 | * "consumer probe" or "active", it must have been added by | |
1378 | * a probing consumer while this device was still probing. | |
1379 | * Change its state to "dormant", as it represents a valid | |
1380 | * relationship, but it is not functionally meaningful. | |
1381 | */ | |
1382 | if (link->status == DL_STATE_CONSUMER_PROBE || | |
1383 | link->status == DL_STATE_ACTIVE) | |
1384 | WRITE_ONCE(link->status, DL_STATE_DORMANT); | |
1385 | } | |
1386 | ||
9ed98953 | 1387 | __device_links_no_driver(dev); |
15cfb094 | 1388 | |
9ed98953 RW |
1389 | device_links_write_unlock(); |
1390 | } | |
1391 | ||
1392 | /** | |
1393 | * device_links_driver_cleanup - Update links after driver removal. | |
1394 | * @dev: Device whose driver has just gone away. | |
1395 | * | |
1396 | * Update links to consumers for @dev by changing their status to "dormant" and | |
1397 | * invoke %__device_links_no_driver() to update links to suppliers for it as | |
1398 | * appropriate. | |
1399 | * | |
515db266 | 1400 | * Links without the DL_FLAG_MANAGED flag set are ignored. |
9ed98953 RW |
1401 | */ |
1402 | void device_links_driver_cleanup(struct device *dev) | |
1403 | { | |
c8d50986 | 1404 | struct device_link *link, *ln; |
9ed98953 RW |
1405 | |
1406 | device_links_write_lock(); | |
1407 | ||
c8d50986 | 1408 | list_for_each_entry_safe(link, ln, &dev->links.consumers, s_node) { |
515db266 | 1409 | if (!(link->flags & DL_FLAG_MANAGED)) |
9ed98953 RW |
1410 | continue; |
1411 | ||
e88728f4 | 1412 | WARN_ON(link->flags & DL_FLAG_AUTOREMOVE_CONSUMER); |
9ed98953 | 1413 | WARN_ON(link->status != DL_STATE_SUPPLIER_UNBIND); |
1689cac5 VG |
1414 | |
1415 | /* | |
1416 | * autoremove the links between this @dev and its consumer | |
1417 | * devices that are not active, i.e. where the link state | |
1418 | * has moved to DL_STATE_SUPPLIER_UNBIND. | |
1419 | */ | |
1420 | if (link->status == DL_STATE_SUPPLIER_UNBIND && | |
1421 | link->flags & DL_FLAG_AUTOREMOVE_SUPPLIER) | |
515db266 | 1422 | device_link_drop_managed(link); |
1689cac5 | 1423 | |
9ed98953 RW |
1424 | WRITE_ONCE(link->status, DL_STATE_DORMANT); |
1425 | } | |
1426 | ||
3b052a3e | 1427 | list_del_init(&dev->links.defer_sync); |
9ed98953 RW |
1428 | __device_links_no_driver(dev); |
1429 | ||
1430 | device_links_write_unlock(); | |
1431 | } | |
1432 | ||
1433 | /** | |
1434 | * device_links_busy - Check if there are any busy links to consumers. | |
1435 | * @dev: Device to check. | |
1436 | * | |
1437 | * Check each consumer of the device and return 'true' if its link's status | |
1438 | * is one of "consumer probe" or "active" (meaning that the given consumer is | |
1439 | * probing right now or its driver is present). Otherwise, change the link | |
1440 | * state to "supplier unbind" to prevent the consumer from being probed | |
1441 | * successfully going forward. | |
1442 | * | |
1443 | * Return 'false' if there are no probing or active consumers. | |
1444 | * | |
515db266 | 1445 | * Links without the DL_FLAG_MANAGED flag set are ignored. |
9ed98953 RW |
1446 | */ |
1447 | bool device_links_busy(struct device *dev) | |
1448 | { | |
1449 | struct device_link *link; | |
1450 | bool ret = false; | |
1451 | ||
1452 | device_links_write_lock(); | |
1453 | ||
1454 | list_for_each_entry(link, &dev->links.consumers, s_node) { | |
515db266 | 1455 | if (!(link->flags & DL_FLAG_MANAGED)) |
9ed98953 RW |
1456 | continue; |
1457 | ||
1458 | if (link->status == DL_STATE_CONSUMER_PROBE | |
1459 | || link->status == DL_STATE_ACTIVE) { | |
1460 | ret = true; | |
1461 | break; | |
1462 | } | |
1463 | WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND); | |
1464 | } | |
1465 | ||
1466 | dev->links.status = DL_DEV_UNBINDING; | |
1467 | ||
1468 | device_links_write_unlock(); | |
1469 | return ret; | |
1470 | } | |
1471 | ||
1472 | /** | |
1473 | * device_links_unbind_consumers - Force unbind consumers of the given device. | |
1474 | * @dev: Device to unbind the consumers of. | |
1475 | * | |
1476 | * Walk the list of links to consumers for @dev and if any of them is in the | |
1477 | * "consumer probe" state, wait for all device probes in progress to complete | |
1478 | * and start over. | |
1479 | * | |
1480 | * If that's not the case, change the status of the link to "supplier unbind" | |
1481 | * and check if the link was in the "active" state. If so, force the consumer | |
1482 | * driver to unbind and start over (the consumer will not re-probe as we have | |
1483 | * changed the state of the link already). | |
1484 | * | |
515db266 | 1485 | * Links without the DL_FLAG_MANAGED flag set are ignored. |
9ed98953 RW |
1486 | */ |
1487 | void device_links_unbind_consumers(struct device *dev) | |
1488 | { | |
1489 | struct device_link *link; | |
1490 | ||
1491 | start: | |
1492 | device_links_write_lock(); | |
1493 | ||
1494 | list_for_each_entry(link, &dev->links.consumers, s_node) { | |
1495 | enum device_link_state status; | |
1496 | ||
05ef983e SK |
1497 | if (!(link->flags & DL_FLAG_MANAGED) || |
1498 | link->flags & DL_FLAG_SYNC_STATE_ONLY) | |
9ed98953 RW |
1499 | continue; |
1500 | ||
1501 | status = link->status; | |
1502 | if (status == DL_STATE_CONSUMER_PROBE) { | |
1503 | device_links_write_unlock(); | |
1504 | ||
1505 | wait_for_device_probe(); | |
1506 | goto start; | |
1507 | } | |
1508 | WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND); | |
1509 | if (status == DL_STATE_ACTIVE) { | |
1510 | struct device *consumer = link->consumer; | |
1511 | ||
1512 | get_device(consumer); | |
1513 | ||
1514 | device_links_write_unlock(); | |
1515 | ||
1516 | device_release_driver_internal(consumer, NULL, | |
1517 | consumer->parent); | |
1518 | put_device(consumer); | |
1519 | goto start; | |
1520 | } | |
1521 | } | |
1522 | ||
1523 | device_links_write_unlock(); | |
1524 | } | |
1525 | ||
1526 | /** | |
1527 | * device_links_purge - Delete existing links to other devices. | |
1528 | * @dev: Target device. | |
1529 | */ | |
1530 | static void device_links_purge(struct device *dev) | |
1531 | { | |
1532 | struct device_link *link, *ln; | |
1533 | ||
287905e6 SK |
1534 | if (dev->class == &devlink_class) |
1535 | return; | |
1536 | ||
9ed98953 RW |
1537 | /* |
1538 | * Delete all of the remaining links from this device to any other | |
1539 | * devices (either consumers or suppliers). | |
1540 | */ | |
1541 | device_links_write_lock(); | |
1542 | ||
1543 | list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) { | |
1544 | WARN_ON(link->status == DL_STATE_ACTIVE); | |
ead18c23 | 1545 | __device_link_del(&link->kref); |
9ed98953 RW |
1546 | } |
1547 | ||
1548 | list_for_each_entry_safe_reverse(link, ln, &dev->links.consumers, s_node) { | |
1549 | WARN_ON(link->status != DL_STATE_DORMANT && | |
1550 | link->status != DL_STATE_NONE); | |
ead18c23 | 1551 | __device_link_del(&link->kref); |
9ed98953 RW |
1552 | } |
1553 | ||
1554 | device_links_write_unlock(); | |
1555 | } | |
1556 | ||
b90fb8f6 SK |
1557 | #define FW_DEVLINK_FLAGS_PERMISSIVE (DL_FLAG_INFERRED | \ |
1558 | DL_FLAG_SYNC_STATE_ONLY) | |
1559 | #define FW_DEVLINK_FLAGS_ON (DL_FLAG_INFERRED | \ | |
1560 | DL_FLAG_AUTOPROBE_CONSUMER) | |
1561 | #define FW_DEVLINK_FLAGS_RPM (FW_DEVLINK_FLAGS_ON | \ | |
1562 | DL_FLAG_PM_RUNTIME) | |
1563 | ||
ea718c69 | 1564 | static u32 fw_devlink_flags = FW_DEVLINK_FLAGS_ON; |
42926ac3 SK |
1565 | static int __init fw_devlink_setup(char *arg) |
1566 | { | |
1567 | if (!arg) | |
1568 | return -EINVAL; | |
1569 | ||
1570 | if (strcmp(arg, "off") == 0) { | |
1571 | fw_devlink_flags = 0; | |
1572 | } else if (strcmp(arg, "permissive") == 0) { | |
b90fb8f6 | 1573 | fw_devlink_flags = FW_DEVLINK_FLAGS_PERMISSIVE; |
42926ac3 | 1574 | } else if (strcmp(arg, "on") == 0) { |
b90fb8f6 | 1575 | fw_devlink_flags = FW_DEVLINK_FLAGS_ON; |
42926ac3 | 1576 | } else if (strcmp(arg, "rpm") == 0) { |
b90fb8f6 | 1577 | fw_devlink_flags = FW_DEVLINK_FLAGS_RPM; |
42926ac3 SK |
1578 | } |
1579 | return 0; | |
1580 | } | |
1581 | early_param("fw_devlink", fw_devlink_setup); | |
1582 | ||
19d0f5f6 SK |
1583 | static bool fw_devlink_strict; |
1584 | static int __init fw_devlink_strict_setup(char *arg) | |
1585 | { | |
1586 | return strtobool(arg, &fw_devlink_strict); | |
1587 | } | |
1588 | early_param("fw_devlink.strict", fw_devlink_strict_setup); | |
1589 | ||
42926ac3 SK |
1590 | u32 fw_devlink_get_flags(void) |
1591 | { | |
1592 | return fw_devlink_flags; | |
1593 | } | |
1594 | ||
1595 | static bool fw_devlink_is_permissive(void) | |
1596 | { | |
b90fb8f6 | 1597 | return fw_devlink_flags == FW_DEVLINK_FLAGS_PERMISSIVE; |
42926ac3 SK |
1598 | } |
1599 | ||
19d0f5f6 SK |
1600 | bool fw_devlink_is_strict(void) |
1601 | { | |
1602 | return fw_devlink_strict && !fw_devlink_is_permissive(); | |
42926ac3 SK |
1603 | } |
1604 | ||
c2c724c8 SK |
1605 | static void fw_devlink_parse_fwnode(struct fwnode_handle *fwnode) |
1606 | { | |
1607 | if (fwnode->flags & FWNODE_FLAG_LINKS_ADDED) | |
1608 | return; | |
1609 | ||
2d09e6eb | 1610 | fwnode_call_int_op(fwnode, add_links); |
c2c724c8 SK |
1611 | fwnode->flags |= FWNODE_FLAG_LINKS_ADDED; |
1612 | } | |
1613 | ||
1614 | static void fw_devlink_parse_fwtree(struct fwnode_handle *fwnode) | |
1615 | { | |
1616 | struct fwnode_handle *child = NULL; | |
1617 | ||
1618 | fw_devlink_parse_fwnode(fwnode); | |
1619 | ||
1620 | while ((child = fwnode_get_next_available_child_node(fwnode, child))) | |
1621 | fw_devlink_parse_fwtree(child); | |
1622 | } | |
1623 | ||
d46f3e3e SK |
1624 | static void fw_devlink_relax_link(struct device_link *link) |
1625 | { | |
1626 | if (!(link->flags & DL_FLAG_INFERRED)) | |
1627 | return; | |
1628 | ||
1629 | if (link->flags == (DL_FLAG_MANAGED | FW_DEVLINK_FLAGS_PERMISSIVE)) | |
1630 | return; | |
1631 | ||
1632 | pm_runtime_drop_link(link); | |
1633 | link->flags = DL_FLAG_MANAGED | FW_DEVLINK_FLAGS_PERMISSIVE; | |
1634 | dev_dbg(link->consumer, "Relaxing link with %s\n", | |
1635 | dev_name(link->supplier)); | |
1636 | } | |
1637 | ||
1638 | static int fw_devlink_no_driver(struct device *dev, void *data) | |
1639 | { | |
1640 | struct device_link *link = to_devlink(dev); | |
1641 | ||
1642 | if (!link->supplier->can_match) | |
1643 | fw_devlink_relax_link(link); | |
1644 | ||
1645 | return 0; | |
1646 | } | |
1647 | ||
1648 | void fw_devlink_drivers_done(void) | |
1649 | { | |
1650 | fw_devlink_drv_reg_done = true; | |
1651 | device_links_write_lock(); | |
1652 | class_for_each_device(&devlink_class, NULL, NULL, | |
1653 | fw_devlink_no_driver); | |
1654 | device_links_write_unlock(); | |
1655 | } | |
1656 | ||
1657 | static void fw_devlink_unblock_consumers(struct device *dev) | |
1658 | { | |
1659 | struct device_link *link; | |
1660 | ||
1661 | if (!fw_devlink_flags || fw_devlink_is_permissive()) | |
1662 | return; | |
1663 | ||
1664 | device_links_write_lock(); | |
1665 | list_for_each_entry(link, &dev->links.consumers, s_node) | |
1666 | fw_devlink_relax_link(link); | |
1667 | device_links_write_unlock(); | |
1668 | } | |
1669 | ||
b0e2fa4f SK |
1670 | /** |
1671 | * fw_devlink_relax_cycle - Convert cyclic links to SYNC_STATE_ONLY links | |
1672 | * @con: Device to check dependencies for. | |
1673 | * @sup: Device to check against. | |
1674 | * | |
1675 | * Check if @sup depends on @con or any device dependent on it (its child or | |
1676 | * its consumer etc). When such a cyclic dependency is found, convert all | |
1677 | * device links created solely by fw_devlink into SYNC_STATE_ONLY device links. | |
1678 | * This is the equivalent of doing fw_devlink=permissive just between the | |
1679 | * devices in the cycle. We need to do this because, at this point, fw_devlink | |
1680 | * can't tell which of these dependencies is not a real dependency. | |
1681 | * | |
1682 | * Return 1 if a cycle is found. Otherwise, return 0. | |
1683 | */ | |
c13b8279 | 1684 | static int fw_devlink_relax_cycle(struct device *con, void *sup) |
b0e2fa4f SK |
1685 | { |
1686 | struct device_link *link; | |
1687 | int ret; | |
1688 | ||
1689 | if (con == sup) | |
1690 | return 1; | |
1691 | ||
1692 | ret = device_for_each_child(con, sup, fw_devlink_relax_cycle); | |
1693 | if (ret) | |
1694 | return ret; | |
1695 | ||
1696 | list_for_each_entry(link, &con->links.consumers, s_node) { | |
1697 | if ((link->flags & ~DL_FLAG_INFERRED) == | |
1698 | (DL_FLAG_SYNC_STATE_ONLY | DL_FLAG_MANAGED)) | |
1699 | continue; | |
1700 | ||
1701 | if (!fw_devlink_relax_cycle(link->consumer, sup)) | |
1702 | continue; | |
1703 | ||
1704 | ret = 1; | |
1705 | ||
d46f3e3e | 1706 | fw_devlink_relax_link(link); |
b0e2fa4f SK |
1707 | } |
1708 | return ret; | |
1709 | } | |
1710 | ||
f9aa4606 SK |
1711 | /** |
1712 | * fw_devlink_create_devlink - Create a device link from a consumer to fwnode | |
37c52f74 PLB |
1713 | * @con: consumer device for the device link |
1714 | * @sup_handle: fwnode handle of supplier | |
1715 | * @flags: devlink flags | |
f9aa4606 SK |
1716 | * |
1717 | * This function will try to create a device link between the consumer device | |
1718 | * @con and the supplier device represented by @sup_handle. | |
1719 | * | |
1720 | * The supplier has to be provided as a fwnode because incorrect cycles in | |
1721 | * fwnode links can sometimes cause the supplier device to never be created. | |
1722 | * This function detects such cases and returns an error if it cannot create a | |
1723 | * device link from the consumer to a missing supplier. | |
1724 | * | |
1725 | * Returns, | |
1726 | * 0 on successfully creating a device link | |
1727 | * -EINVAL if the device link cannot be created as expected | |
1728 | * -EAGAIN if the device link cannot be created right now, but it may be | |
1729 | * possible to do that in the future | |
1730 | */ | |
1731 | static int fw_devlink_create_devlink(struct device *con, | |
1732 | struct fwnode_handle *sup_handle, u32 flags) | |
5f5377ea | 1733 | { |
f9aa4606 SK |
1734 | struct device *sup_dev; |
1735 | int ret = 0; | |
5f5377ea | 1736 | |
5501765a SK |
1737 | /* |
1738 | * In some cases, a device P might also be a supplier to its child node | |
1739 | * C. However, this would defer the probe of C until the probe of P | |
1740 | * completes successfully. This is perfectly fine in the device driver | |
1741 | * model. device_add() doesn't guarantee probe completion of the device | |
1742 | * by the time it returns. | |
1743 | * | |
1744 | * However, there are a few drivers that assume C will finish probing | |
1745 | * as soon as it's added and before P finishes probing. So, we provide | |
1746 | * a flag to let fw_devlink know not to delay the probe of C until the | |
1747 | * probe of P completes successfully. | |
1748 | * | |
1749 | * When such a flag is set, we can't create device links where P is the | |
1750 | * supplier of C as that would delay the probe of C. | |
1751 | */ | |
1752 | if (sup_handle->flags & FWNODE_FLAG_NEEDS_CHILD_BOUND_ON_ADD && | |
1753 | fwnode_is_ancestor_of(sup_handle, con->fwnode)) | |
1754 | return -EINVAL; | |
1755 | ||
f9aa4606 SK |
1756 | sup_dev = get_dev_from_fwnode(sup_handle); |
1757 | if (sup_dev) { | |
74c782cf SK |
1758 | /* |
1759 | * If it's one of those drivers that don't actually bind to | |
1760 | * their device using driver core, then don't wait on this | |
1761 | * supplier device indefinitely. | |
1762 | */ | |
1763 | if (sup_dev->links.status == DL_DEV_NO_DRIVER && | |
1764 | sup_handle->flags & FWNODE_FLAG_INITIALIZED) { | |
1765 | ret = -EINVAL; | |
1766 | goto out; | |
1767 | } | |
1768 | ||
f9aa4606 SK |
1769 | /* |
1770 | * If this fails, it is due to cycles in device links. Just | |
1771 | * give up on this link and treat it as invalid. | |
1772 | */ | |
b0e2fa4f SK |
1773 | if (!device_link_add(con, sup_dev, flags) && |
1774 | !(flags & DL_FLAG_SYNC_STATE_ONLY)) { | |
1775 | dev_info(con, "Fixing up cyclic dependency with %s\n", | |
1776 | dev_name(sup_dev)); | |
1777 | device_links_write_lock(); | |
1778 | fw_devlink_relax_cycle(con, sup_dev); | |
1779 | device_links_write_unlock(); | |
1780 | device_link_add(con, sup_dev, | |
1781 | FW_DEVLINK_FLAGS_PERMISSIVE); | |
f9aa4606 | 1782 | ret = -EINVAL; |
b0e2fa4f | 1783 | } |
716a7a25 | 1784 | |
f9aa4606 | 1785 | goto out; |
5f5377ea | 1786 | } |
f9aa4606 | 1787 | |
74c782cf SK |
1788 | /* Supplier that's already initialized without a struct device. */ |
1789 | if (sup_handle->flags & FWNODE_FLAG_INITIALIZED) | |
1790 | return -EINVAL; | |
1791 | ||
f9aa4606 SK |
1792 | /* |
1793 | * DL_FLAG_SYNC_STATE_ONLY doesn't block probing and supports | |
1794 | * cycles. So cycle detection isn't necessary and shouldn't be | |
1795 | * done. | |
1796 | */ | |
1797 | if (flags & DL_FLAG_SYNC_STATE_ONLY) | |
1798 | return -EAGAIN; | |
1799 | ||
1800 | /* | |
1801 | * If we can't find the supplier device from its fwnode, it might be | |
1802 | * due to a cyclic dependency between fwnodes. Some of these cycles can | |
1803 | * be broken by applying logic. Check for these types of cycles and | |
1804 | * break them so that devices in the cycle probe properly. | |
1805 | * | |
2de9d8e0 SK |
1806 | * If the supplier's parent is dependent on the consumer, then the |
1807 | * consumer and supplier have a cyclic dependency. Since fw_devlink | |
1808 | * can't tell which of the inferred dependencies are incorrect, don't | |
1809 | * enforce probe ordering between any of the devices in this cyclic | |
1810 | * dependency. Do this by relaxing all the fw_devlink device links in | |
1811 | * this cycle and by treating the fwnode link between the consumer and | |
1812 | * the supplier as an invalid dependency. | |
f9aa4606 SK |
1813 | */ |
1814 | sup_dev = fwnode_get_next_parent_dev(sup_handle); | |
1815 | if (sup_dev && device_is_dependent(con, sup_dev)) { | |
2de9d8e0 SK |
1816 | dev_info(con, "Fixing up cyclic dependency with %pfwP (%s)\n", |
1817 | sup_handle, dev_name(sup_dev)); | |
1818 | device_links_write_lock(); | |
1819 | fw_devlink_relax_cycle(con, sup_dev); | |
1820 | device_links_write_unlock(); | |
f9aa4606 SK |
1821 | ret = -EINVAL; |
1822 | } else { | |
1823 | /* | |
1824 | * Can't check for cycles or no cycles. So let's try | |
1825 | * again later. | |
1826 | */ | |
1827 | ret = -EAGAIN; | |
1828 | } | |
1829 | ||
1830 | out: | |
1831 | put_device(sup_dev); | |
1832 | return ret; | |
1833 | } | |
1834 | ||
1835 | /** | |
1836 | * __fw_devlink_link_to_consumers - Create device links to consumers of a device | |
37c52f74 | 1837 | * @dev: Device that needs to be linked to its consumers |
f9aa4606 SK |
1838 | * |
1839 | * This function looks at all the consumer fwnodes of @dev and creates device | |
1840 | * links between the consumer device and @dev (supplier). | |
1841 | * | |
1842 | * If the consumer device has not been added yet, then this function creates a | |
1843 | * SYNC_STATE_ONLY link between @dev (supplier) and the closest ancestor device | |
1844 | * of the consumer fwnode. This is necessary to make sure @dev doesn't get a | |
1845 | * sync_state() callback before the real consumer device gets to be added and | |
1846 | * then probed. | |
1847 | * | |
1848 | * Once device links are created from the real consumer to @dev (supplier), the | |
1849 | * fwnode links are deleted. | |
1850 | */ | |
1851 | static void __fw_devlink_link_to_consumers(struct device *dev) | |
1852 | { | |
1853 | struct fwnode_handle *fwnode = dev->fwnode; | |
1854 | struct fwnode_link *link, *tmp; | |
1855 | ||
1856 | list_for_each_entry_safe(link, tmp, &fwnode->consumers, s_hook) { | |
1857 | u32 dl_flags = fw_devlink_get_flags(); | |
1858 | struct device *con_dev; | |
1859 | bool own_link = true; | |
1860 | int ret; | |
1861 | ||
1862 | con_dev = get_dev_from_fwnode(link->consumer); | |
1863 | /* | |
1864 | * If consumer device is not available yet, make a "proxy" | |
1865 | * SYNC_STATE_ONLY link from the consumer's parent device to | |
1866 | * the supplier device. This is necessary to make sure the | |
1867 | * supplier doesn't get a sync_state() callback before the real | |
1868 | * consumer can create a device link to the supplier. | |
1869 | * | |
1870 | * This proxy link step is needed to handle the case where the | |
1871 | * consumer's parent device is added before the supplier. | |
1872 | */ | |
1873 | if (!con_dev) { | |
1874 | con_dev = fwnode_get_next_parent_dev(link->consumer); | |
1875 | /* | |
1876 | * However, if the consumer's parent device is also the | |
1877 | * parent of the supplier, don't create a | |
1878 | * consumer-supplier link from the parent to its child | |
1879 | * device. Such a dependency is impossible. | |
1880 | */ | |
1881 | if (con_dev && | |
1882 | fwnode_is_ancestor_of(con_dev->fwnode, fwnode)) { | |
1883 | put_device(con_dev); | |
1884 | con_dev = NULL; | |
1885 | } else { | |
1886 | own_link = false; | |
b90fb8f6 | 1887 | dl_flags = FW_DEVLINK_FLAGS_PERMISSIVE; |
f9aa4606 SK |
1888 | } |
1889 | } | |
1890 | ||
1891 | if (!con_dev) | |
1892 | continue; | |
1893 | ||
1894 | ret = fw_devlink_create_devlink(con_dev, fwnode, dl_flags); | |
1895 | put_device(con_dev); | |
1896 | if (!own_link || ret == -EAGAIN) | |
1897 | continue; | |
1898 | ||
76f13081 | 1899 | __fwnode_link_del(link); |
f9aa4606 SK |
1900 | } |
1901 | } | |
1902 | ||
1903 | /** | |
1904 | * __fw_devlink_link_to_suppliers - Create device links to suppliers of a device | |
37c52f74 PLB |
1905 | * @dev: The consumer device that needs to be linked to its suppliers |
1906 | * @fwnode: Root of the fwnode tree that is used to create device links | |
f9aa4606 SK |
1907 | * |
1908 | * This function looks at all the supplier fwnodes of fwnode tree rooted at | |
1909 | * @fwnode and creates device links between @dev (consumer) and all the | |
1910 | * supplier devices of the entire fwnode tree at @fwnode. | |
1911 | * | |
1912 | * The function creates normal (non-SYNC_STATE_ONLY) device links between @dev | |
1913 | * and the real suppliers of @dev. Once these device links are created, the | |
1914 | * fwnode links are deleted. When such device links are successfully created, | |
1915 | * this function is called recursively on those supplier devices. This is | |
1916 | * needed to detect and break some invalid cycles in fwnode links. See | |
1917 | * fw_devlink_create_devlink() for more details. | |
1918 | * | |
1919 | * In addition, it also looks at all the suppliers of the entire fwnode tree | |
1920 | * because some of the child devices of @dev that have not been added yet | |
1921 | * (because @dev hasn't probed) might already have their suppliers added to | |
1922 | * driver core. So, this function creates SYNC_STATE_ONLY device links between | |
1923 | * @dev (consumer) and these suppliers to make sure they don't execute their | |
1924 | * sync_state() callbacks before these child devices have a chance to create | |
1925 | * their device links. The fwnode links that correspond to the child devices | |
1926 | * aren't delete because they are needed later to create the device links | |
1927 | * between the real consumer and supplier devices. | |
1928 | */ | |
1929 | static void __fw_devlink_link_to_suppliers(struct device *dev, | |
1930 | struct fwnode_handle *fwnode) | |
1931 | { | |
1932 | bool own_link = (dev->fwnode == fwnode); | |
1933 | struct fwnode_link *link, *tmp; | |
1934 | struct fwnode_handle *child = NULL; | |
1935 | u32 dl_flags; | |
1936 | ||
1937 | if (own_link) | |
1938 | dl_flags = fw_devlink_get_flags(); | |
1939 | else | |
b90fb8f6 | 1940 | dl_flags = FW_DEVLINK_FLAGS_PERMISSIVE; |
f9aa4606 SK |
1941 | |
1942 | list_for_each_entry_safe(link, tmp, &fwnode->suppliers, c_hook) { | |
1943 | int ret; | |
1944 | struct device *sup_dev; | |
1945 | struct fwnode_handle *sup = link->supplier; | |
1946 | ||
1947 | ret = fw_devlink_create_devlink(dev, sup, dl_flags); | |
1948 | if (!own_link || ret == -EAGAIN) | |
1949 | continue; | |
1950 | ||
76f13081 | 1951 | __fwnode_link_del(link); |
f9aa4606 SK |
1952 | |
1953 | /* If no device link was created, nothing more to do. */ | |
1954 | if (ret) | |
1955 | continue; | |
1956 | ||
1957 | /* | |
1958 | * If a device link was successfully created to a supplier, we | |
1959 | * now need to try and link the supplier to all its suppliers. | |
1960 | * | |
1961 | * This is needed to detect and delete false dependencies in | |
1962 | * fwnode links that haven't been converted to a device link | |
1963 | * yet. See comments in fw_devlink_create_devlink() for more | |
1964 | * details on the false dependency. | |
1965 | * | |
1966 | * Without deleting these false dependencies, some devices will | |
1967 | * never probe because they'll keep waiting for their false | |
1968 | * dependency fwnode links to be converted to device links. | |
1969 | */ | |
1970 | sup_dev = get_dev_from_fwnode(sup); | |
1971 | __fw_devlink_link_to_suppliers(sup_dev, sup_dev->fwnode); | |
1972 | put_device(sup_dev); | |
1973 | } | |
1974 | ||
1975 | /* | |
1976 | * Make "proxy" SYNC_STATE_ONLY device links to represent the needs of | |
1977 | * all the descendants. This proxy link step is needed to handle the | |
1978 | * case where the supplier is added before the consumer's parent device | |
1979 | * (@dev). | |
1980 | */ | |
1981 | while ((child = fwnode_get_next_available_child_node(fwnode, child))) | |
1982 | __fw_devlink_link_to_suppliers(dev, child); | |
1983 | } | |
1984 | ||
1985 | static void fw_devlink_link_device(struct device *dev) | |
1986 | { | |
1987 | struct fwnode_handle *fwnode = dev->fwnode; | |
1988 | ||
1989 | if (!fw_devlink_flags) | |
1990 | return; | |
1991 | ||
1992 | fw_devlink_parse_fwtree(fwnode); | |
1993 | ||
1994 | mutex_lock(&fwnode_link_lock); | |
1995 | __fw_devlink_link_to_consumers(dev); | |
1996 | __fw_devlink_link_to_suppliers(dev, fwnode); | |
1997 | mutex_unlock(&fwnode_link_lock); | |
5f5377ea SK |
1998 | } |
1999 | ||
9ed98953 RW |
2000 | /* Device links support end. */ |
2001 | ||
4a3ad20c GKH |
2002 | int (*platform_notify)(struct device *dev) = NULL; |
2003 | int (*platform_notify_remove)(struct device *dev) = NULL; | |
e105b8bf DW |
2004 | static struct kobject *dev_kobj; |
2005 | struct kobject *sysfs_dev_char_kobj; | |
2006 | struct kobject *sysfs_dev_block_kobj; | |
1da177e4 | 2007 | |
5e33bc41 RW |
2008 | static DEFINE_MUTEX(device_hotplug_lock); |
2009 | ||
2010 | void lock_device_hotplug(void) | |
2011 | { | |
2012 | mutex_lock(&device_hotplug_lock); | |
2013 | } | |
2014 | ||
2015 | void unlock_device_hotplug(void) | |
2016 | { | |
2017 | mutex_unlock(&device_hotplug_lock); | |
2018 | } | |
2019 | ||
2020 | int lock_device_hotplug_sysfs(void) | |
2021 | { | |
2022 | if (mutex_trylock(&device_hotplug_lock)) | |
2023 | return 0; | |
2024 | ||
2025 | /* Avoid busy looping (5 ms of sleep should do). */ | |
2026 | msleep(5); | |
2027 | return restart_syscall(); | |
2028 | } | |
2029 | ||
4e886c29 GKH |
2030 | #ifdef CONFIG_BLOCK |
2031 | static inline int device_is_not_partition(struct device *dev) | |
2032 | { | |
2033 | return !(dev->type == &part_type); | |
2034 | } | |
2035 | #else | |
2036 | static inline int device_is_not_partition(struct device *dev) | |
2037 | { | |
2038 | return 1; | |
2039 | } | |
2040 | #endif | |
1da177e4 | 2041 | |
b2ebd9dd | 2042 | static void device_platform_notify(struct device *dev) |
07de0e86 | 2043 | { |
b2ebd9dd | 2044 | acpi_device_notify(dev); |
7847a145 | 2045 | |
b2ebd9dd | 2046 | software_node_notify(dev); |
7847a145 | 2047 | |
b2ebd9dd | 2048 | if (platform_notify) |
07de0e86 | 2049 | platform_notify(dev); |
b2ebd9dd RW |
2050 | } |
2051 | ||
2052 | static void device_platform_notify_remove(struct device *dev) | |
2053 | { | |
2054 | acpi_device_notify_remove(dev); | |
2055 | ||
2056 | software_node_notify_remove(dev); | |
2057 | ||
2058 | if (platform_notify_remove) | |
07de0e86 | 2059 | platform_notify_remove(dev); |
07de0e86 HK |
2060 | } |
2061 | ||
3e95637a AS |
2062 | /** |
2063 | * dev_driver_string - Return a device's driver name, if at all possible | |
2064 | * @dev: struct device to get the name of | |
2065 | * | |
2066 | * Will return the device's driver's name if it is bound to a device. If | |
9169c012 | 2067 | * the device is not bound to a driver, it will return the name of the bus |
3e95637a AS |
2068 | * it is attached to. If it is not attached to a bus either, an empty |
2069 | * string will be returned. | |
2070 | */ | |
bf9ca69f | 2071 | const char *dev_driver_string(const struct device *dev) |
3e95637a | 2072 | { |
3589972e AS |
2073 | struct device_driver *drv; |
2074 | ||
2075 | /* dev->driver can change to NULL underneath us because of unbinding, | |
2076 | * so be careful about accessing it. dev->bus and dev->class should | |
2077 | * never change once they are set, so they don't need special care. | |
2078 | */ | |
6aa7de05 | 2079 | drv = READ_ONCE(dev->driver); |
e020ff61 | 2080 | return drv ? drv->name : dev_bus_name(dev); |
3e95637a | 2081 | } |
310a922d | 2082 | EXPORT_SYMBOL(dev_driver_string); |
3e95637a | 2083 | |
1da177e4 LT |
2084 | #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr) |
2085 | ||
4a3ad20c GKH |
2086 | static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr, |
2087 | char *buf) | |
1da177e4 | 2088 | { |
4a3ad20c | 2089 | struct device_attribute *dev_attr = to_dev_attr(attr); |
b0d1f807 | 2090 | struct device *dev = kobj_to_dev(kobj); |
4a0c20bf | 2091 | ssize_t ret = -EIO; |
1da177e4 LT |
2092 | |
2093 | if (dev_attr->show) | |
54b6f35c | 2094 | ret = dev_attr->show(dev, dev_attr, buf); |
815d2d50 | 2095 | if (ret >= (ssize_t)PAGE_SIZE) { |
a52668c6 SS |
2096 | printk("dev_attr_show: %pS returned bad count\n", |
2097 | dev_attr->show); | |
815d2d50 | 2098 | } |
1da177e4 LT |
2099 | return ret; |
2100 | } | |
2101 | ||
4a3ad20c GKH |
2102 | static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr, |
2103 | const char *buf, size_t count) | |
1da177e4 | 2104 | { |
4a3ad20c | 2105 | struct device_attribute *dev_attr = to_dev_attr(attr); |
b0d1f807 | 2106 | struct device *dev = kobj_to_dev(kobj); |
4a0c20bf | 2107 | ssize_t ret = -EIO; |
1da177e4 LT |
2108 | |
2109 | if (dev_attr->store) | |
54b6f35c | 2110 | ret = dev_attr->store(dev, dev_attr, buf, count); |
1da177e4 LT |
2111 | return ret; |
2112 | } | |
2113 | ||
52cf25d0 | 2114 | static const struct sysfs_ops dev_sysfs_ops = { |
1da177e4 LT |
2115 | .show = dev_attr_show, |
2116 | .store = dev_attr_store, | |
2117 | }; | |
2118 | ||
ca22e56d KS |
2119 | #define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr) |
2120 | ||
2121 | ssize_t device_store_ulong(struct device *dev, | |
2122 | struct device_attribute *attr, | |
2123 | const char *buf, size_t size) | |
2124 | { | |
2125 | struct dev_ext_attribute *ea = to_ext_attr(attr); | |
f88184bf K |
2126 | int ret; |
2127 | unsigned long new; | |
2128 | ||
2129 | ret = kstrtoul(buf, 0, &new); | |
2130 | if (ret) | |
2131 | return ret; | |
ca22e56d KS |
2132 | *(unsigned long *)(ea->var) = new; |
2133 | /* Always return full write size even if we didn't consume all */ | |
2134 | return size; | |
2135 | } | |
2136 | EXPORT_SYMBOL_GPL(device_store_ulong); | |
2137 | ||
2138 | ssize_t device_show_ulong(struct device *dev, | |
2139 | struct device_attribute *attr, | |
2140 | char *buf) | |
2141 | { | |
2142 | struct dev_ext_attribute *ea = to_ext_attr(attr); | |
aa838896 | 2143 | return sysfs_emit(buf, "%lx\n", *(unsigned long *)(ea->var)); |
ca22e56d KS |
2144 | } |
2145 | EXPORT_SYMBOL_GPL(device_show_ulong); | |
2146 | ||
2147 | ssize_t device_store_int(struct device *dev, | |
2148 | struct device_attribute *attr, | |
2149 | const char *buf, size_t size) | |
2150 | { | |
2151 | struct dev_ext_attribute *ea = to_ext_attr(attr); | |
f88184bf K |
2152 | int ret; |
2153 | long new; | |
2154 | ||
2155 | ret = kstrtol(buf, 0, &new); | |
2156 | if (ret) | |
2157 | return ret; | |
2158 | ||
2159 | if (new > INT_MAX || new < INT_MIN) | |
ca22e56d KS |
2160 | return -EINVAL; |
2161 | *(int *)(ea->var) = new; | |
2162 | /* Always return full write size even if we didn't consume all */ | |
2163 | return size; | |
2164 | } | |
2165 | EXPORT_SYMBOL_GPL(device_store_int); | |
2166 | ||
2167 | ssize_t device_show_int(struct device *dev, | |
2168 | struct device_attribute *attr, | |
2169 | char *buf) | |
2170 | { | |
2171 | struct dev_ext_attribute *ea = to_ext_attr(attr); | |
2172 | ||
aa838896 | 2173 | return sysfs_emit(buf, "%d\n", *(int *)(ea->var)); |
ca22e56d KS |
2174 | } |
2175 | EXPORT_SYMBOL_GPL(device_show_int); | |
1da177e4 | 2176 | |
91872392 BP |
2177 | ssize_t device_store_bool(struct device *dev, struct device_attribute *attr, |
2178 | const char *buf, size_t size) | |
2179 | { | |
2180 | struct dev_ext_attribute *ea = to_ext_attr(attr); | |
2181 | ||
2182 | if (strtobool(buf, ea->var) < 0) | |
2183 | return -EINVAL; | |
2184 | ||
2185 | return size; | |
2186 | } | |
2187 | EXPORT_SYMBOL_GPL(device_store_bool); | |
2188 | ||
2189 | ssize_t device_show_bool(struct device *dev, struct device_attribute *attr, | |
2190 | char *buf) | |
2191 | { | |
2192 | struct dev_ext_attribute *ea = to_ext_attr(attr); | |
2193 | ||
aa838896 | 2194 | return sysfs_emit(buf, "%d\n", *(bool *)(ea->var)); |
91872392 BP |
2195 | } |
2196 | EXPORT_SYMBOL_GPL(device_show_bool); | |
2197 | ||
1da177e4 | 2198 | /** |
f8878dcb RD |
2199 | * device_release - free device structure. |
2200 | * @kobj: device's kobject. | |
1da177e4 | 2201 | * |
f8878dcb RD |
2202 | * This is called once the reference count for the object |
2203 | * reaches 0. We forward the call to the device's release | |
2204 | * method, which should handle actually freeing the structure. | |
1da177e4 | 2205 | */ |
4a3ad20c | 2206 | static void device_release(struct kobject *kobj) |
1da177e4 | 2207 | { |
b0d1f807 | 2208 | struct device *dev = kobj_to_dev(kobj); |
fb069a5d | 2209 | struct device_private *p = dev->p; |
1da177e4 | 2210 | |
a525a3dd ML |
2211 | /* |
2212 | * Some platform devices are driven without driver attached | |
2213 | * and managed resources may have been acquired. Make sure | |
2214 | * all resources are released. | |
2215 | * | |
2216 | * Drivers still can add resources into device after device | |
2217 | * is deleted but alive, so release devres here to avoid | |
2218 | * possible memory leak. | |
2219 | */ | |
2220 | devres_release_all(dev); | |
2221 | ||
e0d07278 JQ |
2222 | kfree(dev->dma_range_map); |
2223 | ||
1da177e4 LT |
2224 | if (dev->release) |
2225 | dev->release(dev); | |
f9f852df KS |
2226 | else if (dev->type && dev->type->release) |
2227 | dev->type->release(dev); | |
2620efef GKH |
2228 | else if (dev->class && dev->class->dev_release) |
2229 | dev->class->dev_release(dev); | |
f810a5cf | 2230 | else |
0c1bc6b8 | 2231 | WARN(1, KERN_ERR "Device '%s' does not have a release() function, it is broken and must be fixed. See Documentation/core-api/kobject.rst.\n", |
1e0b2cf9 | 2232 | dev_name(dev)); |
fb069a5d | 2233 | kfree(p); |
1da177e4 LT |
2234 | } |
2235 | ||
bc451f20 EB |
2236 | static const void *device_namespace(struct kobject *kobj) |
2237 | { | |
b0d1f807 | 2238 | struct device *dev = kobj_to_dev(kobj); |
bc451f20 EB |
2239 | const void *ns = NULL; |
2240 | ||
2241 | if (dev->class && dev->class->ns_type) | |
2242 | ns = dev->class->namespace(dev); | |
2243 | ||
2244 | return ns; | |
2245 | } | |
2246 | ||
9944e894 DT |
2247 | static void device_get_ownership(struct kobject *kobj, kuid_t *uid, kgid_t *gid) |
2248 | { | |
2249 | struct device *dev = kobj_to_dev(kobj); | |
2250 | ||
2251 | if (dev->class && dev->class->get_ownership) | |
2252 | dev->class->get_ownership(dev, uid, gid); | |
2253 | } | |
2254 | ||
8f4afc41 | 2255 | static struct kobj_type device_ktype = { |
1da177e4 LT |
2256 | .release = device_release, |
2257 | .sysfs_ops = &dev_sysfs_ops, | |
bc451f20 | 2258 | .namespace = device_namespace, |
9944e894 | 2259 | .get_ownership = device_get_ownership, |
1da177e4 LT |
2260 | }; |
2261 | ||
2262 | ||
312c004d | 2263 | static int dev_uevent_filter(struct kset *kset, struct kobject *kobj) |
1da177e4 LT |
2264 | { |
2265 | struct kobj_type *ktype = get_ktype(kobj); | |
2266 | ||
8f4afc41 | 2267 | if (ktype == &device_ktype) { |
b0d1f807 | 2268 | struct device *dev = kobj_to_dev(kobj); |
1da177e4 LT |
2269 | if (dev->bus) |
2270 | return 1; | |
23681e47 GKH |
2271 | if (dev->class) |
2272 | return 1; | |
1da177e4 LT |
2273 | } |
2274 | return 0; | |
2275 | } | |
2276 | ||
312c004d | 2277 | static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj) |
1da177e4 | 2278 | { |
b0d1f807 | 2279 | struct device *dev = kobj_to_dev(kobj); |
1da177e4 | 2280 | |
23681e47 GKH |
2281 | if (dev->bus) |
2282 | return dev->bus->name; | |
2283 | if (dev->class) | |
2284 | return dev->class->name; | |
2285 | return NULL; | |
1da177e4 LT |
2286 | } |
2287 | ||
7eff2e7a KS |
2288 | static int dev_uevent(struct kset *kset, struct kobject *kobj, |
2289 | struct kobj_uevent_env *env) | |
1da177e4 | 2290 | { |
b0d1f807 | 2291 | struct device *dev = kobj_to_dev(kobj); |
1da177e4 LT |
2292 | int retval = 0; |
2293 | ||
6fcf53ac | 2294 | /* add device node properties if present */ |
23681e47 | 2295 | if (MAJOR(dev->devt)) { |
6fcf53ac KS |
2296 | const char *tmp; |
2297 | const char *name; | |
2c9ede55 | 2298 | umode_t mode = 0; |
4e4098a3 GKH |
2299 | kuid_t uid = GLOBAL_ROOT_UID; |
2300 | kgid_t gid = GLOBAL_ROOT_GID; | |
6fcf53ac | 2301 | |
7eff2e7a KS |
2302 | add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt)); |
2303 | add_uevent_var(env, "MINOR=%u", MINOR(dev->devt)); | |
3c2670e6 | 2304 | name = device_get_devnode(dev, &mode, &uid, &gid, &tmp); |
6fcf53ac KS |
2305 | if (name) { |
2306 | add_uevent_var(env, "DEVNAME=%s", name); | |
e454cea2 KS |
2307 | if (mode) |
2308 | add_uevent_var(env, "DEVMODE=%#o", mode & 0777); | |
4e4098a3 GKH |
2309 | if (!uid_eq(uid, GLOBAL_ROOT_UID)) |
2310 | add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid)); | |
2311 | if (!gid_eq(gid, GLOBAL_ROOT_GID)) | |
2312 | add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid)); | |
3c2670e6 | 2313 | kfree(tmp); |
6fcf53ac | 2314 | } |
23681e47 GKH |
2315 | } |
2316 | ||
414264f9 | 2317 | if (dev->type && dev->type->name) |
7eff2e7a | 2318 | add_uevent_var(env, "DEVTYPE=%s", dev->type->name); |
414264f9 | 2319 | |
239378f1 | 2320 | if (dev->driver) |
7eff2e7a | 2321 | add_uevent_var(env, "DRIVER=%s", dev->driver->name); |
239378f1 | 2322 | |
07d57a32 GL |
2323 | /* Add common DT information about the device */ |
2324 | of_device_uevent(dev, env); | |
2325 | ||
7eff2e7a | 2326 | /* have the bus specific function add its stuff */ |
312c004d | 2327 | if (dev->bus && dev->bus->uevent) { |
7eff2e7a | 2328 | retval = dev->bus->uevent(dev, env); |
f9f852df | 2329 | if (retval) |
7dc72b28 | 2330 | pr_debug("device: '%s': %s: bus uevent() returned %d\n", |
1e0b2cf9 | 2331 | dev_name(dev), __func__, retval); |
1da177e4 LT |
2332 | } |
2333 | ||
7eff2e7a | 2334 | /* have the class specific function add its stuff */ |
2620efef | 2335 | if (dev->class && dev->class->dev_uevent) { |
7eff2e7a | 2336 | retval = dev->class->dev_uevent(dev, env); |
f9f852df | 2337 | if (retval) |
7dc72b28 | 2338 | pr_debug("device: '%s': %s: class uevent() " |
1e0b2cf9 | 2339 | "returned %d\n", dev_name(dev), |
2b3a302a | 2340 | __func__, retval); |
f9f852df KS |
2341 | } |
2342 | ||
eef35c2d | 2343 | /* have the device type specific function add its stuff */ |
f9f852df | 2344 | if (dev->type && dev->type->uevent) { |
7eff2e7a | 2345 | retval = dev->type->uevent(dev, env); |
f9f852df | 2346 | if (retval) |
7dc72b28 | 2347 | pr_debug("device: '%s': %s: dev_type uevent() " |
1e0b2cf9 | 2348 | "returned %d\n", dev_name(dev), |
2b3a302a | 2349 | __func__, retval); |
2620efef GKH |
2350 | } |
2351 | ||
1da177e4 LT |
2352 | return retval; |
2353 | } | |
2354 | ||
9cd43611 | 2355 | static const struct kset_uevent_ops device_uevent_ops = { |
312c004d KS |
2356 | .filter = dev_uevent_filter, |
2357 | .name = dev_uevent_name, | |
2358 | .uevent = dev_uevent, | |
1da177e4 LT |
2359 | }; |
2360 | ||
c5e064a6 | 2361 | static ssize_t uevent_show(struct device *dev, struct device_attribute *attr, |
16574dcc KS |
2362 | char *buf) |
2363 | { | |
2364 | struct kobject *top_kobj; | |
2365 | struct kset *kset; | |
7eff2e7a | 2366 | struct kobj_uevent_env *env = NULL; |
16574dcc | 2367 | int i; |
948b3edb | 2368 | int len = 0; |
16574dcc KS |
2369 | int retval; |
2370 | ||
2371 | /* search the kset, the device belongs to */ | |
2372 | top_kobj = &dev->kobj; | |
5c5daf65 KS |
2373 | while (!top_kobj->kset && top_kobj->parent) |
2374 | top_kobj = top_kobj->parent; | |
16574dcc KS |
2375 | if (!top_kobj->kset) |
2376 | goto out; | |
5c5daf65 | 2377 | |
16574dcc KS |
2378 | kset = top_kobj->kset; |
2379 | if (!kset->uevent_ops || !kset->uevent_ops->uevent) | |
2380 | goto out; | |
2381 | ||
2382 | /* respect filter */ | |
2383 | if (kset->uevent_ops && kset->uevent_ops->filter) | |
2384 | if (!kset->uevent_ops->filter(kset, &dev->kobj)) | |
2385 | goto out; | |
2386 | ||
7eff2e7a KS |
2387 | env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL); |
2388 | if (!env) | |
c7308c81 GKH |
2389 | return -ENOMEM; |
2390 | ||
16574dcc | 2391 | /* let the kset specific function add its keys */ |
7eff2e7a | 2392 | retval = kset->uevent_ops->uevent(kset, &dev->kobj, env); |
16574dcc KS |
2393 | if (retval) |
2394 | goto out; | |
2395 | ||
2396 | /* copy keys to file */ | |
7eff2e7a | 2397 | for (i = 0; i < env->envp_idx; i++) |
948b3edb | 2398 | len += sysfs_emit_at(buf, len, "%s\n", env->envp[i]); |
16574dcc | 2399 | out: |
7eff2e7a | 2400 | kfree(env); |
948b3edb | 2401 | return len; |
16574dcc KS |
2402 | } |
2403 | ||
c5e064a6 | 2404 | static ssize_t uevent_store(struct device *dev, struct device_attribute *attr, |
a7fd6706 KS |
2405 | const char *buf, size_t count) |
2406 | { | |
df44b479 PR |
2407 | int rc; |
2408 | ||
2409 | rc = kobject_synth_uevent(&dev->kobj, buf, count); | |
2410 | ||
2411 | if (rc) { | |
f36776fa | 2412 | dev_err(dev, "uevent: failed to send synthetic uevent\n"); |
df44b479 PR |
2413 | return rc; |
2414 | } | |
60a96a59 | 2415 | |
a7fd6706 KS |
2416 | return count; |
2417 | } | |
c5e064a6 | 2418 | static DEVICE_ATTR_RW(uevent); |
a7fd6706 | 2419 | |
c5e064a6 | 2420 | static ssize_t online_show(struct device *dev, struct device_attribute *attr, |
4f3549d7 RW |
2421 | char *buf) |
2422 | { | |
2423 | bool val; | |
2424 | ||
5e33bc41 | 2425 | device_lock(dev); |
4f3549d7 | 2426 | val = !dev->offline; |
5e33bc41 | 2427 | device_unlock(dev); |
aa838896 | 2428 | return sysfs_emit(buf, "%u\n", val); |
4f3549d7 RW |
2429 | } |
2430 | ||
c5e064a6 | 2431 | static ssize_t online_store(struct device *dev, struct device_attribute *attr, |
4f3549d7 RW |
2432 | const char *buf, size_t count) |
2433 | { | |
2434 | bool val; | |
2435 | int ret; | |
2436 | ||
2437 | ret = strtobool(buf, &val); | |
2438 | if (ret < 0) | |
2439 | return ret; | |
2440 | ||
5e33bc41 RW |
2441 | ret = lock_device_hotplug_sysfs(); |
2442 | if (ret) | |
2443 | return ret; | |
2444 | ||
4f3549d7 RW |
2445 | ret = val ? device_online(dev) : device_offline(dev); |
2446 | unlock_device_hotplug(); | |
2447 | return ret < 0 ? ret : count; | |
2448 | } | |
c5e064a6 | 2449 | static DEVICE_ATTR_RW(online); |
4f3549d7 | 2450 | |
70f400d4 RJ |
2451 | static ssize_t removable_show(struct device *dev, struct device_attribute *attr, |
2452 | char *buf) | |
2453 | { | |
2454 | const char *loc; | |
2455 | ||
2456 | switch (dev->removable) { | |
2457 | case DEVICE_REMOVABLE: | |
2458 | loc = "removable"; | |
2459 | break; | |
2460 | case DEVICE_FIXED: | |
2461 | loc = "fixed"; | |
2462 | break; | |
2463 | default: | |
2464 | loc = "unknown"; | |
2465 | } | |
2466 | return sysfs_emit(buf, "%s\n", loc); | |
2467 | } | |
2468 | static DEVICE_ATTR_RO(removable); | |
2469 | ||
fa6fdb33 | 2470 | int device_add_groups(struct device *dev, const struct attribute_group **groups) |
621a1672 | 2471 | { |
3e9b2bae | 2472 | return sysfs_create_groups(&dev->kobj, groups); |
de0ff00d | 2473 | } |
a7670d42 | 2474 | EXPORT_SYMBOL_GPL(device_add_groups); |
de0ff00d | 2475 | |
fa6fdb33 GKH |
2476 | void device_remove_groups(struct device *dev, |
2477 | const struct attribute_group **groups) | |
de0ff00d | 2478 | { |
3e9b2bae | 2479 | sysfs_remove_groups(&dev->kobj, groups); |
de0ff00d | 2480 | } |
a7670d42 | 2481 | EXPORT_SYMBOL_GPL(device_remove_groups); |
de0ff00d | 2482 | |
57b8ff07 DT |
2483 | union device_attr_group_devres { |
2484 | const struct attribute_group *group; | |
2485 | const struct attribute_group **groups; | |
2486 | }; | |
2487 | ||
2488 | static int devm_attr_group_match(struct device *dev, void *res, void *data) | |
2489 | { | |
2490 | return ((union device_attr_group_devres *)res)->group == data; | |
2491 | } | |
2492 | ||
2493 | static void devm_attr_group_remove(struct device *dev, void *res) | |
2494 | { | |
2495 | union device_attr_group_devres *devres = res; | |
2496 | const struct attribute_group *group = devres->group; | |
2497 | ||
2498 | dev_dbg(dev, "%s: removing group %p\n", __func__, group); | |
2499 | sysfs_remove_group(&dev->kobj, group); | |
2500 | } | |
2501 | ||
2502 | static void devm_attr_groups_remove(struct device *dev, void *res) | |
2503 | { | |
2504 | union device_attr_group_devres *devres = res; | |
2505 | const struct attribute_group **groups = devres->groups; | |
2506 | ||
2507 | dev_dbg(dev, "%s: removing groups %p\n", __func__, groups); | |
2508 | sysfs_remove_groups(&dev->kobj, groups); | |
2509 | } | |
2510 | ||
2511 | /** | |
2512 | * devm_device_add_group - given a device, create a managed attribute group | |
2513 | * @dev: The device to create the group for | |
2514 | * @grp: The attribute group to create | |
2515 | * | |
2516 | * This function creates a group for the first time. It will explicitly | |
2517 | * warn and error if any of the attribute files being created already exist. | |
2518 | * | |
2519 | * Returns 0 on success or error code on failure. | |
2520 | */ | |
2521 | int devm_device_add_group(struct device *dev, const struct attribute_group *grp) | |
2522 | { | |
2523 | union device_attr_group_devres *devres; | |
2524 | int error; | |
2525 | ||
2526 | devres = devres_alloc(devm_attr_group_remove, | |
2527 | sizeof(*devres), GFP_KERNEL); | |
2528 | if (!devres) | |
2529 | return -ENOMEM; | |
2530 | ||
2531 | error = sysfs_create_group(&dev->kobj, grp); | |
2532 | if (error) { | |
2533 | devres_free(devres); | |
2534 | return error; | |
2535 | } | |
2536 | ||
2537 | devres->group = grp; | |
2538 | devres_add(dev, devres); | |
2539 | return 0; | |
2540 | } | |
2541 | EXPORT_SYMBOL_GPL(devm_device_add_group); | |
2542 | ||
2543 | /** | |
2544 | * devm_device_remove_group: remove a managed group from a device | |
2545 | * @dev: device to remove the group from | |
2546 | * @grp: group to remove | |
2547 | * | |
2548 | * This function removes a group of attributes from a device. The attributes | |
2549 | * previously have to have been created for this group, otherwise it will fail. | |
2550 | */ | |
2551 | void devm_device_remove_group(struct device *dev, | |
2552 | const struct attribute_group *grp) | |
2553 | { | |
2554 | WARN_ON(devres_release(dev, devm_attr_group_remove, | |
2555 | devm_attr_group_match, | |
2556 | /* cast away const */ (void *)grp)); | |
2557 | } | |
2558 | EXPORT_SYMBOL_GPL(devm_device_remove_group); | |
2559 | ||
2560 | /** | |
2561 | * devm_device_add_groups - create a bunch of managed attribute groups | |
2562 | * @dev: The device to create the group for | |
2563 | * @groups: The attribute groups to create, NULL terminated | |
2564 | * | |
2565 | * This function creates a bunch of managed attribute groups. If an error | |
2566 | * occurs when creating a group, all previously created groups will be | |
2567 | * removed, unwinding everything back to the original state when this | |
2568 | * function was called. It will explicitly warn and error if any of the | |
2569 | * attribute files being created already exist. | |
2570 | * | |
2571 | * Returns 0 on success or error code from sysfs_create_group on failure. | |
2572 | */ | |
2573 | int devm_device_add_groups(struct device *dev, | |
2574 | const struct attribute_group **groups) | |
2575 | { | |
2576 | union device_attr_group_devres *devres; | |
2577 | int error; | |
2578 | ||
2579 | devres = devres_alloc(devm_attr_groups_remove, | |
2580 | sizeof(*devres), GFP_KERNEL); | |
2581 | if (!devres) | |
2582 | return -ENOMEM; | |
2583 | ||
2584 | error = sysfs_create_groups(&dev->kobj, groups); | |
2585 | if (error) { | |
2586 | devres_free(devres); | |
2587 | return error; | |
2588 | } | |
2589 | ||
2590 | devres->groups = groups; | |
2591 | devres_add(dev, devres); | |
2592 | return 0; | |
2593 | } | |
2594 | EXPORT_SYMBOL_GPL(devm_device_add_groups); | |
2595 | ||
2596 | /** | |
2597 | * devm_device_remove_groups - remove a list of managed groups | |
2598 | * | |
2599 | * @dev: The device for the groups to be removed from | |
2600 | * @groups: NULL terminated list of groups to be removed | |
2601 | * | |
2602 | * If groups is not NULL, remove the specified groups from the device. | |
2603 | */ | |
2604 | void devm_device_remove_groups(struct device *dev, | |
2605 | const struct attribute_group **groups) | |
2606 | { | |
2607 | WARN_ON(devres_release(dev, devm_attr_groups_remove, | |
2608 | devm_attr_group_match, | |
2609 | /* cast away const */ (void *)groups)); | |
2610 | } | |
2611 | EXPORT_SYMBOL_GPL(devm_device_remove_groups); | |
de0ff00d | 2612 | |
2620efef GKH |
2613 | static int device_add_attrs(struct device *dev) |
2614 | { | |
2615 | struct class *class = dev->class; | |
aed65af1 | 2616 | const struct device_type *type = dev->type; |
621a1672 | 2617 | int error; |
2620efef | 2618 | |
621a1672 | 2619 | if (class) { |
d05a6f96 | 2620 | error = device_add_groups(dev, class->dev_groups); |
f9f852df | 2621 | if (error) |
621a1672 | 2622 | return error; |
2620efef | 2623 | } |
f9f852df | 2624 | |
621a1672 DT |
2625 | if (type) { |
2626 | error = device_add_groups(dev, type->groups); | |
f9f852df | 2627 | if (error) |
a6b01ded | 2628 | goto err_remove_class_groups; |
f9f852df KS |
2629 | } |
2630 | ||
621a1672 DT |
2631 | error = device_add_groups(dev, dev->groups); |
2632 | if (error) | |
2633 | goto err_remove_type_groups; | |
2634 | ||
4f3549d7 | 2635 | if (device_supports_offline(dev) && !dev->offline_disabled) { |
c5e064a6 | 2636 | error = device_create_file(dev, &dev_attr_online); |
4f3549d7 | 2637 | if (error) |
ecfbf6fd | 2638 | goto err_remove_dev_groups; |
4f3549d7 RW |
2639 | } |
2640 | ||
25ac86c6 | 2641 | if (fw_devlink_flags && !fw_devlink_is_permissive() && dev->fwnode) { |
da6d6475 SK |
2642 | error = device_create_file(dev, &dev_attr_waiting_for_supplier); |
2643 | if (error) | |
2644 | goto err_remove_dev_online; | |
2645 | } | |
2646 | ||
70f400d4 RJ |
2647 | if (dev_removable_is_valid(dev)) { |
2648 | error = device_create_file(dev, &dev_attr_removable); | |
2649 | if (error) | |
2650 | goto err_remove_dev_waiting_for_supplier; | |
2651 | } | |
2652 | ||
621a1672 DT |
2653 | return 0; |
2654 | ||
70f400d4 RJ |
2655 | err_remove_dev_waiting_for_supplier: |
2656 | device_remove_file(dev, &dev_attr_waiting_for_supplier); | |
da6d6475 SK |
2657 | err_remove_dev_online: |
2658 | device_remove_file(dev, &dev_attr_online); | |
ecfbf6fd RW |
2659 | err_remove_dev_groups: |
2660 | device_remove_groups(dev, dev->groups); | |
621a1672 DT |
2661 | err_remove_type_groups: |
2662 | if (type) | |
2663 | device_remove_groups(dev, type->groups); | |
d05a6f96 GKH |
2664 | err_remove_class_groups: |
2665 | if (class) | |
2666 | device_remove_groups(dev, class->dev_groups); | |
621a1672 | 2667 | |
2620efef GKH |
2668 | return error; |
2669 | } | |
2670 | ||
2671 | static void device_remove_attrs(struct device *dev) | |
2672 | { | |
2673 | struct class *class = dev->class; | |
aed65af1 | 2674 | const struct device_type *type = dev->type; |
2620efef | 2675 | |
70f400d4 | 2676 | device_remove_file(dev, &dev_attr_removable); |
da6d6475 | 2677 | device_remove_file(dev, &dev_attr_waiting_for_supplier); |
c5e064a6 | 2678 | device_remove_file(dev, &dev_attr_online); |
621a1672 | 2679 | device_remove_groups(dev, dev->groups); |
f9f852df | 2680 | |
621a1672 DT |
2681 | if (type) |
2682 | device_remove_groups(dev, type->groups); | |
2683 | ||
a6b01ded | 2684 | if (class) |
d05a6f96 | 2685 | device_remove_groups(dev, class->dev_groups); |
2620efef GKH |
2686 | } |
2687 | ||
c5e064a6 | 2688 | static ssize_t dev_show(struct device *dev, struct device_attribute *attr, |
23681e47 GKH |
2689 | char *buf) |
2690 | { | |
2691 | return print_dev_t(buf, dev->devt); | |
2692 | } | |
c5e064a6 | 2693 | static DEVICE_ATTR_RO(dev); |
ad6a1e1c | 2694 | |
ca22e56d | 2695 | /* /sys/devices/ */ |
881c6cfd | 2696 | struct kset *devices_kset; |
1da177e4 | 2697 | |
52cdbdd4 GS |
2698 | /** |
2699 | * devices_kset_move_before - Move device in the devices_kset's list. | |
2700 | * @deva: Device to move. | |
2701 | * @devb: Device @deva should come before. | |
2702 | */ | |
2703 | static void devices_kset_move_before(struct device *deva, struct device *devb) | |
2704 | { | |
2705 | if (!devices_kset) | |
2706 | return; | |
2707 | pr_debug("devices_kset: Moving %s before %s\n", | |
2708 | dev_name(deva), dev_name(devb)); | |
2709 | spin_lock(&devices_kset->list_lock); | |
2710 | list_move_tail(&deva->kobj.entry, &devb->kobj.entry); | |
2711 | spin_unlock(&devices_kset->list_lock); | |
2712 | } | |
2713 | ||
2714 | /** | |
2715 | * devices_kset_move_after - Move device in the devices_kset's list. | |
2716 | * @deva: Device to move | |
2717 | * @devb: Device @deva should come after. | |
2718 | */ | |
2719 | static void devices_kset_move_after(struct device *deva, struct device *devb) | |
2720 | { | |
2721 | if (!devices_kset) | |
2722 | return; | |
2723 | pr_debug("devices_kset: Moving %s after %s\n", | |
2724 | dev_name(deva), dev_name(devb)); | |
2725 | spin_lock(&devices_kset->list_lock); | |
2726 | list_move(&deva->kobj.entry, &devb->kobj.entry); | |
2727 | spin_unlock(&devices_kset->list_lock); | |
2728 | } | |
2729 | ||
2730 | /** | |
2731 | * devices_kset_move_last - move the device to the end of devices_kset's list. | |
2732 | * @dev: device to move | |
2733 | */ | |
2734 | void devices_kset_move_last(struct device *dev) | |
2735 | { | |
2736 | if (!devices_kset) | |
2737 | return; | |
2738 | pr_debug("devices_kset: Moving %s to end of list\n", dev_name(dev)); | |
2739 | spin_lock(&devices_kset->list_lock); | |
2740 | list_move_tail(&dev->kobj.entry, &devices_kset->list); | |
2741 | spin_unlock(&devices_kset->list_lock); | |
2742 | } | |
2743 | ||
1da177e4 | 2744 | /** |
4a3ad20c GKH |
2745 | * device_create_file - create sysfs attribute file for device. |
2746 | * @dev: device. | |
2747 | * @attr: device attribute descriptor. | |
1da177e4 | 2748 | */ |
26579ab7 PC |
2749 | int device_create_file(struct device *dev, |
2750 | const struct device_attribute *attr) | |
1da177e4 LT |
2751 | { |
2752 | int error = 0; | |
8f46baaa FB |
2753 | |
2754 | if (dev) { | |
2755 | WARN(((attr->attr.mode & S_IWUGO) && !attr->store), | |
97521978 | 2756 | "Attribute %s: write permission without 'store'\n", |
2757 | attr->attr.name); | |
8f46baaa | 2758 | WARN(((attr->attr.mode & S_IRUGO) && !attr->show), |
97521978 | 2759 | "Attribute %s: read permission without 'show'\n", |
2760 | attr->attr.name); | |
1da177e4 | 2761 | error = sysfs_create_file(&dev->kobj, &attr->attr); |
8f46baaa FB |
2762 | } |
2763 | ||
1da177e4 LT |
2764 | return error; |
2765 | } | |
86df2687 | 2766 | EXPORT_SYMBOL_GPL(device_create_file); |
1da177e4 LT |
2767 | |
2768 | /** | |
4a3ad20c GKH |
2769 | * device_remove_file - remove sysfs attribute file. |
2770 | * @dev: device. | |
2771 | * @attr: device attribute descriptor. | |
1da177e4 | 2772 | */ |
26579ab7 PC |
2773 | void device_remove_file(struct device *dev, |
2774 | const struct device_attribute *attr) | |
1da177e4 | 2775 | { |
0c98b19f | 2776 | if (dev) |
1da177e4 | 2777 | sysfs_remove_file(&dev->kobj, &attr->attr); |
1da177e4 | 2778 | } |
86df2687 | 2779 | EXPORT_SYMBOL_GPL(device_remove_file); |
1da177e4 | 2780 | |
6b0afc2a TH |
2781 | /** |
2782 | * device_remove_file_self - remove sysfs attribute file from its own method. | |
2783 | * @dev: device. | |
2784 | * @attr: device attribute descriptor. | |
2785 | * | |
2786 | * See kernfs_remove_self() for details. | |
2787 | */ | |
2788 | bool device_remove_file_self(struct device *dev, | |
2789 | const struct device_attribute *attr) | |
2790 | { | |
2791 | if (dev) | |
2792 | return sysfs_remove_file_self(&dev->kobj, &attr->attr); | |
2793 | else | |
2794 | return false; | |
2795 | } | |
2796 | EXPORT_SYMBOL_GPL(device_remove_file_self); | |
2797 | ||
2589f188 GKH |
2798 | /** |
2799 | * device_create_bin_file - create sysfs binary attribute file for device. | |
2800 | * @dev: device. | |
2801 | * @attr: device binary attribute descriptor. | |
2802 | */ | |
66ecb92b PC |
2803 | int device_create_bin_file(struct device *dev, |
2804 | const struct bin_attribute *attr) | |
2589f188 GKH |
2805 | { |
2806 | int error = -EINVAL; | |
2807 | if (dev) | |
2808 | error = sysfs_create_bin_file(&dev->kobj, attr); | |
2809 | return error; | |
2810 | } | |
2811 | EXPORT_SYMBOL_GPL(device_create_bin_file); | |
2812 | ||
2813 | /** | |
2814 | * device_remove_bin_file - remove sysfs binary attribute file | |
2815 | * @dev: device. | |
2816 | * @attr: device binary attribute descriptor. | |
2817 | */ | |
66ecb92b PC |
2818 | void device_remove_bin_file(struct device *dev, |
2819 | const struct bin_attribute *attr) | |
2589f188 GKH |
2820 | { |
2821 | if (dev) | |
2822 | sysfs_remove_bin_file(&dev->kobj, attr); | |
2823 | } | |
2824 | EXPORT_SYMBOL_GPL(device_remove_bin_file); | |
2825 | ||
34bb61f9 JB |
2826 | static void klist_children_get(struct klist_node *n) |
2827 | { | |
f791b8c8 GKH |
2828 | struct device_private *p = to_device_private_parent(n); |
2829 | struct device *dev = p->device; | |
34bb61f9 JB |
2830 | |
2831 | get_device(dev); | |
2832 | } | |
2833 | ||
2834 | static void klist_children_put(struct klist_node *n) | |
2835 | { | |
f791b8c8 GKH |
2836 | struct device_private *p = to_device_private_parent(n); |
2837 | struct device *dev = p->device; | |
34bb61f9 JB |
2838 | |
2839 | put_device(dev); | |
2840 | } | |
2841 | ||
1da177e4 | 2842 | /** |
4a3ad20c GKH |
2843 | * device_initialize - init device structure. |
2844 | * @dev: device. | |
1da177e4 | 2845 | * |
5739411a CH |
2846 | * This prepares the device for use by other layers by initializing |
2847 | * its fields. | |
4a3ad20c | 2848 | * It is the first half of device_register(), if called by |
5739411a CH |
2849 | * that function, though it can also be called separately, so one |
2850 | * may use @dev's fields. In particular, get_device()/put_device() | |
2851 | * may be used for reference counting of @dev after calling this | |
2852 | * function. | |
2853 | * | |
b10d5efd AS |
2854 | * All fields in @dev must be initialized by the caller to 0, except |
2855 | * for those explicitly set to some other value. The simplest | |
2856 | * approach is to use kzalloc() to allocate the structure containing | |
2857 | * @dev. | |
2858 | * | |
5739411a CH |
2859 | * NOTE: Use put_device() to give up your reference instead of freeing |
2860 | * @dev directly once you have called this function. | |
1da177e4 | 2861 | */ |
1da177e4 LT |
2862 | void device_initialize(struct device *dev) |
2863 | { | |
881c6cfd | 2864 | dev->kobj.kset = devices_kset; |
f9cb074b | 2865 | kobject_init(&dev->kobj, &device_ktype); |
1da177e4 | 2866 | INIT_LIST_HEAD(&dev->dma_pools); |
3142788b | 2867 | mutex_init(&dev->mutex); |
87a30e1f DW |
2868 | #ifdef CONFIG_PROVE_LOCKING |
2869 | mutex_init(&dev->lockdep_mutex); | |
2870 | #endif | |
1704f47b | 2871 | lockdep_set_novalidate_class(&dev->mutex); |
9ac7849e TH |
2872 | spin_lock_init(&dev->devres_lock); |
2873 | INIT_LIST_HEAD(&dev->devres_head); | |
3b98aeaf | 2874 | device_pm_init(dev); |
87348136 | 2875 | set_dev_node(dev, -1); |
4a7cc831 | 2876 | #ifdef CONFIG_GENERIC_MSI_IRQ |
77e89afc | 2877 | raw_spin_lock_init(&dev->msi_lock); |
4a7cc831 JL |
2878 | INIT_LIST_HEAD(&dev->msi_list); |
2879 | #endif | |
9ed98953 RW |
2880 | INIT_LIST_HEAD(&dev->links.consumers); |
2881 | INIT_LIST_HEAD(&dev->links.suppliers); | |
3b052a3e | 2882 | INIT_LIST_HEAD(&dev->links.defer_sync); |
9ed98953 | 2883 | dev->links.status = DL_DEV_NO_DRIVER; |
6d4e9a8e CH |
2884 | #if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE) || \ |
2885 | defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU) || \ | |
2886 | defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL) | |
2887 | dev->dma_coherent = dma_default_coherent; | |
2888 | #endif | |
69031f50 | 2889 | #ifdef CONFIG_SWIOTLB |
463e862a | 2890 | dev->dma_io_tlb_mem = &io_tlb_default_mem; |
69031f50 | 2891 | #endif |
1da177e4 | 2892 | } |
86df2687 | 2893 | EXPORT_SYMBOL_GPL(device_initialize); |
1da177e4 | 2894 | |
d73ce004 | 2895 | struct kobject *virtual_device_parent(struct device *dev) |
f0ee61a6 | 2896 | { |
86406245 | 2897 | static struct kobject *virtual_dir = NULL; |
f0ee61a6 | 2898 | |
86406245 | 2899 | if (!virtual_dir) |
4ff6abff | 2900 | virtual_dir = kobject_create_and_add("virtual", |
881c6cfd | 2901 | &devices_kset->kobj); |
f0ee61a6 | 2902 | |
86406245 | 2903 | return virtual_dir; |
f0ee61a6 GKH |
2904 | } |
2905 | ||
bc451f20 EB |
2906 | struct class_dir { |
2907 | struct kobject kobj; | |
2908 | struct class *class; | |
2909 | }; | |
2910 | ||
2911 | #define to_class_dir(obj) container_of(obj, struct class_dir, kobj) | |
2912 | ||
2913 | static void class_dir_release(struct kobject *kobj) | |
2914 | { | |
2915 | struct class_dir *dir = to_class_dir(kobj); | |
2916 | kfree(dir); | |
2917 | } | |
2918 | ||
2919 | static const | |
2920 | struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj) | |
40fa5422 | 2921 | { |
bc451f20 EB |
2922 | struct class_dir *dir = to_class_dir(kobj); |
2923 | return dir->class->ns_type; | |
2924 | } | |
2925 | ||
2926 | static struct kobj_type class_dir_ktype = { | |
2927 | .release = class_dir_release, | |
2928 | .sysfs_ops = &kobj_sysfs_ops, | |
2929 | .child_ns_type = class_dir_child_ns_type | |
2930 | }; | |
2931 | ||
2932 | static struct kobject * | |
2933 | class_dir_create_and_add(struct class *class, struct kobject *parent_kobj) | |
2934 | { | |
2935 | struct class_dir *dir; | |
43968d2f GKH |
2936 | int retval; |
2937 | ||
bc451f20 EB |
2938 | dir = kzalloc(sizeof(*dir), GFP_KERNEL); |
2939 | if (!dir) | |
84d0c27d | 2940 | return ERR_PTR(-ENOMEM); |
bc451f20 EB |
2941 | |
2942 | dir->class = class; | |
2943 | kobject_init(&dir->kobj, &class_dir_ktype); | |
2944 | ||
6b6e39a6 | 2945 | dir->kobj.kset = &class->p->glue_dirs; |
bc451f20 EB |
2946 | |
2947 | retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name); | |
2948 | if (retval < 0) { | |
2949 | kobject_put(&dir->kobj); | |
84d0c27d | 2950 | return ERR_PTR(retval); |
bc451f20 EB |
2951 | } |
2952 | return &dir->kobj; | |
2953 | } | |
2954 | ||
e4a60d13 | 2955 | static DEFINE_MUTEX(gdp_mutex); |
bc451f20 EB |
2956 | |
2957 | static struct kobject *get_device_parent(struct device *dev, | |
2958 | struct device *parent) | |
2959 | { | |
86406245 KS |
2960 | if (dev->class) { |
2961 | struct kobject *kobj = NULL; | |
2962 | struct kobject *parent_kobj; | |
2963 | struct kobject *k; | |
2964 | ||
ead454fe | 2965 | #ifdef CONFIG_BLOCK |
39aba963 | 2966 | /* block disks show up in /sys/block */ |
e52eec13 | 2967 | if (sysfs_deprecated && dev->class == &block_class) { |
39aba963 KS |
2968 | if (parent && parent->class == &block_class) |
2969 | return &parent->kobj; | |
6b6e39a6 | 2970 | return &block_class.p->subsys.kobj; |
39aba963 | 2971 | } |
ead454fe | 2972 | #endif |
e52eec13 | 2973 | |
86406245 KS |
2974 | /* |
2975 | * If we have no parent, we live in "virtual". | |
0f4dafc0 KS |
2976 | * Class-devices with a non class-device as parent, live |
2977 | * in a "glue" directory to prevent namespace collisions. | |
86406245 KS |
2978 | */ |
2979 | if (parent == NULL) | |
2980 | parent_kobj = virtual_device_parent(dev); | |
24b1442d | 2981 | else if (parent->class && !dev->class->ns_type) |
86406245 KS |
2982 | return &parent->kobj; |
2983 | else | |
2984 | parent_kobj = &parent->kobj; | |
2985 | ||
77d3d7c1 TH |
2986 | mutex_lock(&gdp_mutex); |
2987 | ||
86406245 | 2988 | /* find our class-directory at the parent and reference it */ |
6b6e39a6 KS |
2989 | spin_lock(&dev->class->p->glue_dirs.list_lock); |
2990 | list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry) | |
86406245 KS |
2991 | if (k->parent == parent_kobj) { |
2992 | kobj = kobject_get(k); | |
2993 | break; | |
2994 | } | |
6b6e39a6 | 2995 | spin_unlock(&dev->class->p->glue_dirs.list_lock); |
77d3d7c1 TH |
2996 | if (kobj) { |
2997 | mutex_unlock(&gdp_mutex); | |
86406245 | 2998 | return kobj; |
77d3d7c1 | 2999 | } |
86406245 KS |
3000 | |
3001 | /* or create a new class-directory at the parent device */ | |
bc451f20 | 3002 | k = class_dir_create_and_add(dev->class, parent_kobj); |
0f4dafc0 | 3003 | /* do not emit an uevent for this simple "glue" directory */ |
77d3d7c1 | 3004 | mutex_unlock(&gdp_mutex); |
43968d2f | 3005 | return k; |
86406245 KS |
3006 | } |
3007 | ||
ca22e56d KS |
3008 | /* subsystems can specify a default root directory for their devices */ |
3009 | if (!parent && dev->bus && dev->bus->dev_root) | |
3010 | return &dev->bus->dev_root->kobj; | |
3011 | ||
86406245 | 3012 | if (parent) |
c744aeae CH |
3013 | return &parent->kobj; |
3014 | return NULL; | |
3015 | } | |
da231fd5 | 3016 | |
cebf8fd1 ML |
3017 | static inline bool live_in_glue_dir(struct kobject *kobj, |
3018 | struct device *dev) | |
3019 | { | |
3020 | if (!kobj || !dev->class || | |
3021 | kobj->kset != &dev->class->p->glue_dirs) | |
3022 | return false; | |
3023 | return true; | |
3024 | } | |
3025 | ||
3026 | static inline struct kobject *get_glue_dir(struct device *dev) | |
3027 | { | |
3028 | return dev->kobj.parent; | |
3029 | } | |
3030 | ||
3031 | /* | |
3032 | * make sure cleaning up dir as the last step, we need to make | |
3033 | * sure .release handler of kobject is run with holding the | |
3034 | * global lock | |
3035 | */ | |
63b6971a | 3036 | static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir) |
da231fd5 | 3037 | { |
ac43432c MS |
3038 | unsigned int ref; |
3039 | ||
0f4dafc0 | 3040 | /* see if we live in a "glue" directory */ |
cebf8fd1 | 3041 | if (!live_in_glue_dir(glue_dir, dev)) |
da231fd5 KS |
3042 | return; |
3043 | ||
e4a60d13 | 3044 | mutex_lock(&gdp_mutex); |
ac43432c MS |
3045 | /** |
3046 | * There is a race condition between removing glue directory | |
3047 | * and adding a new device under the glue directory. | |
3048 | * | |
3049 | * CPU1: CPU2: | |
3050 | * | |
3051 | * device_add() | |
3052 | * get_device_parent() | |
3053 | * class_dir_create_and_add() | |
3054 | * kobject_add_internal() | |
3055 | * create_dir() // create glue_dir | |
3056 | * | |
3057 | * device_add() | |
3058 | * get_device_parent() | |
3059 | * kobject_get() // get glue_dir | |
3060 | * | |
3061 | * device_del() | |
3062 | * cleanup_glue_dir() | |
3063 | * kobject_del(glue_dir) | |
3064 | * | |
3065 | * kobject_add() | |
3066 | * kobject_add_internal() | |
3067 | * create_dir() // in glue_dir | |
3068 | * sysfs_create_dir_ns() | |
3069 | * kernfs_create_dir_ns(sd) | |
3070 | * | |
3071 | * sysfs_remove_dir() // glue_dir->sd=NULL | |
3072 | * sysfs_put() // free glue_dir->sd | |
3073 | * | |
3074 | * // sd is freed | |
3075 | * kernfs_new_node(sd) | |
3076 | * kernfs_get(glue_dir) | |
3077 | * kernfs_add_one() | |
3078 | * kernfs_put() | |
3079 | * | |
3080 | * Before CPU1 remove last child device under glue dir, if CPU2 add | |
3081 | * a new device under glue dir, the glue_dir kobject reference count | |
3082 | * will be increase to 2 in kobject_get(k). And CPU2 has been called | |
3083 | * kernfs_create_dir_ns(). Meanwhile, CPU1 call sysfs_remove_dir() | |
3084 | * and sysfs_put(). This result in glue_dir->sd is freed. | |
3085 | * | |
3086 | * Then the CPU2 will see a stale "empty" but still potentially used | |
3087 | * glue dir around in kernfs_new_node(). | |
3088 | * | |
3089 | * In order to avoid this happening, we also should make sure that | |
3090 | * kernfs_node for glue_dir is released in CPU1 only when refcount | |
3091 | * for glue_dir kobj is 1. | |
3092 | */ | |
3093 | ref = kref_read(&glue_dir->kref); | |
3094 | if (!kobject_has_children(glue_dir) && !--ref) | |
726e4109 | 3095 | kobject_del(glue_dir); |
0f4dafc0 | 3096 | kobject_put(glue_dir); |
e4a60d13 | 3097 | mutex_unlock(&gdp_mutex); |
da231fd5 | 3098 | } |
63b6971a | 3099 | |
2ee97caf CH |
3100 | static int device_add_class_symlinks(struct device *dev) |
3101 | { | |
5590f319 | 3102 | struct device_node *of_node = dev_of_node(dev); |
2ee97caf CH |
3103 | int error; |
3104 | ||
5590f319 | 3105 | if (of_node) { |
0c3c234b | 3106 | error = sysfs_create_link(&dev->kobj, of_node_kobj(of_node), "of_node"); |
5590f319 BH |
3107 | if (error) |
3108 | dev_warn(dev, "Error %d creating of_node link\n",error); | |
3109 | /* An error here doesn't warrant bringing down the device */ | |
3110 | } | |
3111 | ||
2ee97caf CH |
3112 | if (!dev->class) |
3113 | return 0; | |
da231fd5 | 3114 | |
1fbfee6c | 3115 | error = sysfs_create_link(&dev->kobj, |
6b6e39a6 | 3116 | &dev->class->p->subsys.kobj, |
2ee97caf CH |
3117 | "subsystem"); |
3118 | if (error) | |
5590f319 | 3119 | goto out_devnode; |
da231fd5 | 3120 | |
4e886c29 | 3121 | if (dev->parent && device_is_not_partition(dev)) { |
39aba963 | 3122 | error = sysfs_create_link(&dev->kobj, &dev->parent->kobj, |
4f01a757 DT |
3123 | "device"); |
3124 | if (error) | |
39aba963 | 3125 | goto out_subsys; |
2ee97caf | 3126 | } |
2ee97caf | 3127 | |
ead454fe | 3128 | #ifdef CONFIG_BLOCK |
39aba963 | 3129 | /* /sys/block has directories and does not need symlinks */ |
e52eec13 | 3130 | if (sysfs_deprecated && dev->class == &block_class) |
39aba963 | 3131 | return 0; |
ead454fe | 3132 | #endif |
39aba963 | 3133 | |
da231fd5 | 3134 | /* link in the class directory pointing to the device */ |
6b6e39a6 | 3135 | error = sysfs_create_link(&dev->class->p->subsys.kobj, |
1e0b2cf9 | 3136 | &dev->kobj, dev_name(dev)); |
da231fd5 | 3137 | if (error) |
39aba963 | 3138 | goto out_device; |
da231fd5 | 3139 | |
da231fd5 KS |
3140 | return 0; |
3141 | ||
39aba963 KS |
3142 | out_device: |
3143 | sysfs_remove_link(&dev->kobj, "device"); | |
da231fd5 | 3144 | |
2ee97caf CH |
3145 | out_subsys: |
3146 | sysfs_remove_link(&dev->kobj, "subsystem"); | |
5590f319 BH |
3147 | out_devnode: |
3148 | sysfs_remove_link(&dev->kobj, "of_node"); | |
2ee97caf CH |
3149 | return error; |
3150 | } | |
3151 | ||
3152 | static void device_remove_class_symlinks(struct device *dev) | |
3153 | { | |
5590f319 BH |
3154 | if (dev_of_node(dev)) |
3155 | sysfs_remove_link(&dev->kobj, "of_node"); | |
3156 | ||
2ee97caf CH |
3157 | if (!dev->class) |
3158 | return; | |
da231fd5 | 3159 | |
4e886c29 | 3160 | if (dev->parent && device_is_not_partition(dev)) |
da231fd5 | 3161 | sysfs_remove_link(&dev->kobj, "device"); |
2ee97caf | 3162 | sysfs_remove_link(&dev->kobj, "subsystem"); |
ead454fe | 3163 | #ifdef CONFIG_BLOCK |
e52eec13 | 3164 | if (sysfs_deprecated && dev->class == &block_class) |
39aba963 | 3165 | return; |
ead454fe | 3166 | #endif |
6b6e39a6 | 3167 | sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev)); |
2ee97caf CH |
3168 | } |
3169 | ||
413c239f SR |
3170 | /** |
3171 | * dev_set_name - set a device name | |
3172 | * @dev: device | |
46232366 | 3173 | * @fmt: format string for the device's name |
413c239f SR |
3174 | */ |
3175 | int dev_set_name(struct device *dev, const char *fmt, ...) | |
3176 | { | |
3177 | va_list vargs; | |
1fa5ae85 | 3178 | int err; |
413c239f SR |
3179 | |
3180 | va_start(vargs, fmt); | |
1fa5ae85 | 3181 | err = kobject_set_name_vargs(&dev->kobj, fmt, vargs); |
413c239f | 3182 | va_end(vargs); |
1fa5ae85 | 3183 | return err; |
413c239f SR |
3184 | } |
3185 | EXPORT_SYMBOL_GPL(dev_set_name); | |
3186 | ||
e105b8bf DW |
3187 | /** |
3188 | * device_to_dev_kobj - select a /sys/dev/ directory for the device | |
3189 | * @dev: device | |
3190 | * | |
3191 | * By default we select char/ for new entries. Setting class->dev_obj | |
3192 | * to NULL prevents an entry from being created. class->dev_kobj must | |
3193 | * be set (or cleared) before any devices are registered to the class | |
3194 | * otherwise device_create_sys_dev_entry() and | |
0d4e293c PK |
3195 | * device_remove_sys_dev_entry() will disagree about the presence of |
3196 | * the link. | |
e105b8bf DW |
3197 | */ |
3198 | static struct kobject *device_to_dev_kobj(struct device *dev) | |
3199 | { | |
3200 | struct kobject *kobj; | |
3201 | ||
3202 | if (dev->class) | |
3203 | kobj = dev->class->dev_kobj; | |
3204 | else | |
3205 | kobj = sysfs_dev_char_kobj; | |
3206 | ||
3207 | return kobj; | |
3208 | } | |
3209 | ||
3210 | static int device_create_sys_dev_entry(struct device *dev) | |
3211 | { | |
3212 | struct kobject *kobj = device_to_dev_kobj(dev); | |
3213 | int error = 0; | |
3214 | char devt_str[15]; | |
3215 | ||
3216 | if (kobj) { | |
3217 | format_dev_t(devt_str, dev->devt); | |
3218 | error = sysfs_create_link(kobj, &dev->kobj, devt_str); | |
3219 | } | |
3220 | ||
3221 | return error; | |
3222 | } | |
3223 | ||
3224 | static void device_remove_sys_dev_entry(struct device *dev) | |
3225 | { | |
3226 | struct kobject *kobj = device_to_dev_kobj(dev); | |
3227 | char devt_str[15]; | |
3228 | ||
3229 | if (kobj) { | |
3230 | format_dev_t(devt_str, dev->devt); | |
3231 | sysfs_remove_link(kobj, devt_str); | |
3232 | } | |
3233 | } | |
3234 | ||
46d3a037 | 3235 | static int device_private_init(struct device *dev) |
b4028437 GKH |
3236 | { |
3237 | dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL); | |
3238 | if (!dev->p) | |
3239 | return -ENOMEM; | |
3240 | dev->p->device = dev; | |
3241 | klist_init(&dev->p->klist_children, klist_children_get, | |
3242 | klist_children_put); | |
ef8a3fd6 | 3243 | INIT_LIST_HEAD(&dev->p->deferred_probe); |
b4028437 GKH |
3244 | return 0; |
3245 | } | |
3246 | ||
1da177e4 | 3247 | /** |
4a3ad20c GKH |
3248 | * device_add - add device to device hierarchy. |
3249 | * @dev: device. | |
1da177e4 | 3250 | * |
4a3ad20c GKH |
3251 | * This is part 2 of device_register(), though may be called |
3252 | * separately _iff_ device_initialize() has been called separately. | |
1da177e4 | 3253 | * |
5739411a | 3254 | * This adds @dev to the kobject hierarchy via kobject_add(), adds it |
4a3ad20c GKH |
3255 | * to the global and sibling lists for the device, then |
3256 | * adds it to the other relevant subsystems of the driver model. | |
5739411a | 3257 | * |
b10d5efd AS |
3258 | * Do not call this routine or device_register() more than once for |
3259 | * any device structure. The driver model core is not designed to work | |
3260 | * with devices that get unregistered and then spring back to life. | |
3261 | * (Among other things, it's very hard to guarantee that all references | |
3262 | * to the previous incarnation of @dev have been dropped.) Allocate | |
3263 | * and register a fresh new struct device instead. | |
3264 | * | |
5739411a CH |
3265 | * NOTE: _Never_ directly free @dev after calling this function, even |
3266 | * if it returned an error! Always use put_device() to give up your | |
3267 | * reference instead. | |
affada72 BP |
3268 | * |
3269 | * Rule of thumb is: if device_add() succeeds, you should call | |
3270 | * device_del() when you want to get rid of it. If device_add() has | |
3271 | * *not* succeeded, use *only* put_device() to drop the reference | |
3272 | * count. | |
1da177e4 LT |
3273 | */ |
3274 | int device_add(struct device *dev) | |
3275 | { | |
35dbf4ef | 3276 | struct device *parent; |
ca22e56d | 3277 | struct kobject *kobj; |
c47ed219 | 3278 | struct class_interface *class_intf; |
5f5377ea | 3279 | int error = -EINVAL; |
cebf8fd1 | 3280 | struct kobject *glue_dir = NULL; |
775b64d2 | 3281 | |
1da177e4 | 3282 | dev = get_device(dev); |
c906a48a GKH |
3283 | if (!dev) |
3284 | goto done; | |
3285 | ||
fb069a5d | 3286 | if (!dev->p) { |
b4028437 GKH |
3287 | error = device_private_init(dev); |
3288 | if (error) | |
3289 | goto done; | |
fb069a5d | 3290 | } |
fb069a5d | 3291 | |
1fa5ae85 KS |
3292 | /* |
3293 | * for statically allocated devices, which should all be converted | |
3294 | * some day, we need to initialize the name. We prevent reading back | |
3295 | * the name, and force the use of dev_name() | |
3296 | */ | |
3297 | if (dev->init_name) { | |
acc0e90f | 3298 | dev_set_name(dev, "%s", dev->init_name); |
1fa5ae85 KS |
3299 | dev->init_name = NULL; |
3300 | } | |
c906a48a | 3301 | |
ca22e56d KS |
3302 | /* subsystems can specify simple device enumeration */ |
3303 | if (!dev_name(dev) && dev->bus && dev->bus->dev_name) | |
3304 | dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id); | |
3305 | ||
e6309e75 TG |
3306 | if (!dev_name(dev)) { |
3307 | error = -EINVAL; | |
5c8563d7 | 3308 | goto name_error; |
e6309e75 | 3309 | } |
1da177e4 | 3310 | |
1e0b2cf9 | 3311 | pr_debug("device: '%s': %s\n", dev_name(dev), __func__); |
c205ef48 | 3312 | |
1da177e4 | 3313 | parent = get_device(dev->parent); |
ca22e56d | 3314 | kobj = get_device_parent(dev, parent); |
84d0c27d TH |
3315 | if (IS_ERR(kobj)) { |
3316 | error = PTR_ERR(kobj); | |
3317 | goto parent_error; | |
3318 | } | |
ca22e56d KS |
3319 | if (kobj) |
3320 | dev->kobj.parent = kobj; | |
1da177e4 | 3321 | |
0d358f22 | 3322 | /* use parent numa_node */ |
56f2de81 | 3323 | if (parent && (dev_to_node(dev) == NUMA_NO_NODE)) |
0d358f22 YL |
3324 | set_dev_node(dev, dev_to_node(parent)); |
3325 | ||
1da177e4 | 3326 | /* first, register with generic layer. */ |
8a577ffc KS |
3327 | /* we require the name to be set before, and pass NULL */ |
3328 | error = kobject_add(&dev->kobj, dev->kobj.parent, NULL); | |
cebf8fd1 ML |
3329 | if (error) { |
3330 | glue_dir = get_glue_dir(dev); | |
1da177e4 | 3331 | goto Error; |
cebf8fd1 | 3332 | } |
a7fd6706 | 3333 | |
37022644 | 3334 | /* notify platform of device entry */ |
b2ebd9dd | 3335 | device_platform_notify(dev); |
37022644 | 3336 | |
c5e064a6 | 3337 | error = device_create_file(dev, &dev_attr_uevent); |
a306eea4 CH |
3338 | if (error) |
3339 | goto attrError; | |
a7fd6706 | 3340 | |
2ee97caf CH |
3341 | error = device_add_class_symlinks(dev); |
3342 | if (error) | |
3343 | goto SymlinkError; | |
dc0afa83 CH |
3344 | error = device_add_attrs(dev); |
3345 | if (error) | |
2620efef | 3346 | goto AttrsError; |
dc0afa83 CH |
3347 | error = bus_add_device(dev); |
3348 | if (error) | |
1da177e4 | 3349 | goto BusError; |
3b98aeaf | 3350 | error = dpm_sysfs_add(dev); |
57eee3d2 | 3351 | if (error) |
3b98aeaf AS |
3352 | goto DPMError; |
3353 | device_pm_add(dev); | |
ec0676ee | 3354 | |
0cd75047 SK |
3355 | if (MAJOR(dev->devt)) { |
3356 | error = device_create_file(dev, &dev_attr_dev); | |
3357 | if (error) | |
3358 | goto DevAttrError; | |
3359 | ||
3360 | error = device_create_sys_dev_entry(dev); | |
3361 | if (error) | |
3362 | goto SysEntryError; | |
3363 | ||
3364 | devtmpfs_create_node(dev); | |
3365 | } | |
3366 | ||
ec0676ee | 3367 | /* Notify clients of device addition. This call must come |
268863f4 | 3368 | * after dpm_sysfs_add() and before kobject_uevent(). |
ec0676ee AS |
3369 | */ |
3370 | if (dev->bus) | |
3371 | blocking_notifier_call_chain(&dev->bus->p->bus_notifier, | |
3372 | BUS_NOTIFY_ADD_DEVICE, dev); | |
3373 | ||
83b5fb4c | 3374 | kobject_uevent(&dev->kobj, KOBJ_ADD); |
372a67c0 | 3375 | |
e2ae9bcc SK |
3376 | /* |
3377 | * Check if any of the other devices (consumers) have been waiting for | |
3378 | * this device (supplier) to be added so that they can create a device | |
3379 | * link to it. | |
3380 | * | |
3381 | * This needs to happen after device_pm_add() because device_link_add() | |
3382 | * requires the supplier be registered before it's called. | |
3383 | * | |
2cd38fd1 | 3384 | * But this also needs to happen before bus_probe_device() to make sure |
e2ae9bcc SK |
3385 | * waiting consumers can link to it before the driver is bound to the |
3386 | * device and the driver sync_state callback is called for this device. | |
3387 | */ | |
2cd38fd1 SK |
3388 | if (dev->fwnode && !dev->fwnode->dev) { |
3389 | dev->fwnode->dev = dev; | |
5f5377ea | 3390 | fw_devlink_link_device(dev); |
03324507 | 3391 | } |
e2ae9bcc | 3392 | |
2023c610 | 3393 | bus_probe_device(dev); |
d46f3e3e SK |
3394 | |
3395 | /* | |
3396 | * If all driver registration is done and a newly added device doesn't | |
3397 | * match with any driver, don't block its consumers from probing in | |
3398 | * case the consumer device is able to operate without this supplier. | |
3399 | */ | |
3400 | if (dev->fwnode && fw_devlink_drv_reg_done && !dev->can_match) | |
3401 | fw_devlink_unblock_consumers(dev); | |
3402 | ||
1da177e4 | 3403 | if (parent) |
f791b8c8 GKH |
3404 | klist_add_tail(&dev->p->knode_parent, |
3405 | &parent->p->klist_children); | |
1da177e4 | 3406 | |
5d9fd169 | 3407 | if (dev->class) { |
ca22e56d | 3408 | mutex_lock(&dev->class->p->mutex); |
c47ed219 | 3409 | /* tie the class to the device */ |
570d0200 | 3410 | klist_add_tail(&dev->p->knode_class, |
6b6e39a6 | 3411 | &dev->class->p->klist_devices); |
c47ed219 GKH |
3412 | |
3413 | /* notify any interfaces that the device is here */ | |
184f1f77 | 3414 | list_for_each_entry(class_intf, |
ca22e56d | 3415 | &dev->class->p->interfaces, node) |
c47ed219 GKH |
3416 | if (class_intf->add_dev) |
3417 | class_intf->add_dev(dev, class_intf); | |
ca22e56d | 3418 | mutex_unlock(&dev->class->p->mutex); |
5d9fd169 | 3419 | } |
c906a48a | 3420 | done: |
1da177e4 LT |
3421 | put_device(dev); |
3422 | return error; | |
0cd75047 SK |
3423 | SysEntryError: |
3424 | if (MAJOR(dev->devt)) | |
3425 | device_remove_file(dev, &dev_attr_dev); | |
3426 | DevAttrError: | |
3427 | device_pm_remove(dev); | |
3428 | dpm_sysfs_remove(dev); | |
3b98aeaf | 3429 | DPMError: |
57eee3d2 RW |
3430 | bus_remove_device(dev); |
3431 | BusError: | |
82f0cf9b | 3432 | device_remove_attrs(dev); |
2620efef | 3433 | AttrsError: |
2ee97caf CH |
3434 | device_remove_class_symlinks(dev); |
3435 | SymlinkError: | |
c5e064a6 | 3436 | device_remove_file(dev, &dev_attr_uevent); |
23681e47 | 3437 | attrError: |
b2ebd9dd | 3438 | device_platform_notify_remove(dev); |
312c004d | 3439 | kobject_uevent(&dev->kobj, KOBJ_REMOVE); |
cebf8fd1 | 3440 | glue_dir = get_glue_dir(dev); |
1da177e4 LT |
3441 | kobject_del(&dev->kobj); |
3442 | Error: | |
cebf8fd1 | 3443 | cleanup_glue_dir(dev, glue_dir); |
84d0c27d | 3444 | parent_error: |
5f0163a5 | 3445 | put_device(parent); |
5c8563d7 KS |
3446 | name_error: |
3447 | kfree(dev->p); | |
3448 | dev->p = NULL; | |
c906a48a | 3449 | goto done; |
1da177e4 | 3450 | } |
86df2687 | 3451 | EXPORT_SYMBOL_GPL(device_add); |
1da177e4 | 3452 | |
1da177e4 | 3453 | /** |
4a3ad20c GKH |
3454 | * device_register - register a device with the system. |
3455 | * @dev: pointer to the device structure | |
1da177e4 | 3456 | * |
4a3ad20c GKH |
3457 | * This happens in two clean steps - initialize the device |
3458 | * and add it to the system. The two steps can be called | |
3459 | * separately, but this is the easiest and most common. | |
3460 | * I.e. you should only call the two helpers separately if | |
3461 | * have a clearly defined need to use and refcount the device | |
3462 | * before it is added to the hierarchy. | |
5739411a | 3463 | * |
b10d5efd AS |
3464 | * For more information, see the kerneldoc for device_initialize() |
3465 | * and device_add(). | |
3466 | * | |
5739411a CH |
3467 | * NOTE: _Never_ directly free @dev after calling this function, even |
3468 | * if it returned an error! Always use put_device() to give up the | |
3469 | * reference initialized in this function instead. | |
1da177e4 | 3470 | */ |
1da177e4 LT |
3471 | int device_register(struct device *dev) |
3472 | { | |
3473 | device_initialize(dev); | |
3474 | return device_add(dev); | |
3475 | } | |
86df2687 | 3476 | EXPORT_SYMBOL_GPL(device_register); |
1da177e4 | 3477 | |
1da177e4 | 3478 | /** |
4a3ad20c GKH |
3479 | * get_device - increment reference count for device. |
3480 | * @dev: device. | |
1da177e4 | 3481 | * |
4a3ad20c GKH |
3482 | * This simply forwards the call to kobject_get(), though |
3483 | * we do take care to provide for the case that we get a NULL | |
3484 | * pointer passed in. | |
1da177e4 | 3485 | */ |
4a3ad20c | 3486 | struct device *get_device(struct device *dev) |
1da177e4 | 3487 | { |
b0d1f807 | 3488 | return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL; |
1da177e4 | 3489 | } |
86df2687 | 3490 | EXPORT_SYMBOL_GPL(get_device); |
1da177e4 | 3491 | |
1da177e4 | 3492 | /** |
4a3ad20c GKH |
3493 | * put_device - decrement reference count. |
3494 | * @dev: device in question. | |
1da177e4 | 3495 | */ |
4a3ad20c | 3496 | void put_device(struct device *dev) |
1da177e4 | 3497 | { |
edfaa7c3 | 3498 | /* might_sleep(); */ |
1da177e4 LT |
3499 | if (dev) |
3500 | kobject_put(&dev->kobj); | |
3501 | } | |
86df2687 | 3502 | EXPORT_SYMBOL_GPL(put_device); |
1da177e4 | 3503 | |
00289cd8 DW |
3504 | bool kill_device(struct device *dev) |
3505 | { | |
3506 | /* | |
3507 | * Require the device lock and set the "dead" flag to guarantee that | |
3508 | * the update behavior is consistent with the other bitfields near | |
3509 | * it and that we cannot have an asynchronous probe routine trying | |
3510 | * to run while we are tearing out the bus/class/sysfs from | |
3511 | * underneath the device. | |
3512 | */ | |
8c60a141 | 3513 | device_lock_assert(dev); |
00289cd8 DW |
3514 | |
3515 | if (dev->p->dead) | |
3516 | return false; | |
3517 | dev->p->dead = true; | |
3518 | return true; | |
3519 | } | |
3520 | EXPORT_SYMBOL_GPL(kill_device); | |
3521 | ||
1da177e4 | 3522 | /** |
4a3ad20c GKH |
3523 | * device_del - delete device from system. |
3524 | * @dev: device. | |
1da177e4 | 3525 | * |
4a3ad20c GKH |
3526 | * This is the first part of the device unregistration |
3527 | * sequence. This removes the device from the lists we control | |
3528 | * from here, has it removed from the other driver model | |
3529 | * subsystems it was added to in device_add(), and removes it | |
3530 | * from the kobject hierarchy. | |
1da177e4 | 3531 | * |
4a3ad20c GKH |
3532 | * NOTE: this should be called manually _iff_ device_add() was |
3533 | * also called manually. | |
1da177e4 | 3534 | */ |
4a3ad20c | 3535 | void device_del(struct device *dev) |
1da177e4 | 3536 | { |
4a3ad20c | 3537 | struct device *parent = dev->parent; |
cebf8fd1 | 3538 | struct kobject *glue_dir = NULL; |
c47ed219 | 3539 | struct class_interface *class_intf; |
b8530017 | 3540 | unsigned int noio_flag; |
1da177e4 | 3541 | |
3451a495 | 3542 | device_lock(dev); |
00289cd8 | 3543 | kill_device(dev); |
3451a495 AD |
3544 | device_unlock(dev); |
3545 | ||
372a67c0 SK |
3546 | if (dev->fwnode && dev->fwnode->dev == dev) |
3547 | dev->fwnode->dev = NULL; | |
3548 | ||
ec0676ee AS |
3549 | /* Notify clients of device removal. This call must come |
3550 | * before dpm_sysfs_remove(). | |
3551 | */ | |
b8530017 | 3552 | noio_flag = memalloc_noio_save(); |
ec0676ee AS |
3553 | if (dev->bus) |
3554 | blocking_notifier_call_chain(&dev->bus->p->bus_notifier, | |
3555 | BUS_NOTIFY_DEL_DEVICE, dev); | |
9ed98953 | 3556 | |
3b98aeaf | 3557 | dpm_sysfs_remove(dev); |
1da177e4 | 3558 | if (parent) |
f791b8c8 | 3559 | klist_del(&dev->p->knode_parent); |
e105b8bf | 3560 | if (MAJOR(dev->devt)) { |
2b2af54a | 3561 | devtmpfs_delete_node(dev); |
e105b8bf | 3562 | device_remove_sys_dev_entry(dev); |
c5e064a6 | 3563 | device_remove_file(dev, &dev_attr_dev); |
e105b8bf | 3564 | } |
b9d9c82b | 3565 | if (dev->class) { |
da231fd5 | 3566 | device_remove_class_symlinks(dev); |
99ef3ef8 | 3567 | |
ca22e56d | 3568 | mutex_lock(&dev->class->p->mutex); |
c47ed219 | 3569 | /* notify any interfaces that the device is now gone */ |
184f1f77 | 3570 | list_for_each_entry(class_intf, |
ca22e56d | 3571 | &dev->class->p->interfaces, node) |
c47ed219 GKH |
3572 | if (class_intf->remove_dev) |
3573 | class_intf->remove_dev(dev, class_intf); | |
3574 | /* remove the device from the class list */ | |
570d0200 | 3575 | klist_del(&dev->p->knode_class); |
ca22e56d | 3576 | mutex_unlock(&dev->class->p->mutex); |
b9d9c82b | 3577 | } |
c5e064a6 | 3578 | device_remove_file(dev, &dev_attr_uevent); |
2620efef | 3579 | device_remove_attrs(dev); |
28953533 | 3580 | bus_remove_device(dev); |
4b6d1f12 | 3581 | device_pm_remove(dev); |
d1c3414c | 3582 | driver_deferred_probe_del(dev); |
b2ebd9dd | 3583 | device_platform_notify_remove(dev); |
478573c9 | 3584 | device_remove_properties(dev); |
2ec16150 | 3585 | device_links_purge(dev); |
1da177e4 | 3586 | |
599bad38 JR |
3587 | if (dev->bus) |
3588 | blocking_notifier_call_chain(&dev->bus->p->bus_notifier, | |
3589 | BUS_NOTIFY_REMOVED_DEVICE, dev); | |
312c004d | 3590 | kobject_uevent(&dev->kobj, KOBJ_REMOVE); |
cebf8fd1 | 3591 | glue_dir = get_glue_dir(dev); |
1da177e4 | 3592 | kobject_del(&dev->kobj); |
cebf8fd1 | 3593 | cleanup_glue_dir(dev, glue_dir); |
b8530017 | 3594 | memalloc_noio_restore(noio_flag); |
da231fd5 | 3595 | put_device(parent); |
1da177e4 | 3596 | } |
86df2687 | 3597 | EXPORT_SYMBOL_GPL(device_del); |
1da177e4 LT |
3598 | |
3599 | /** | |
4a3ad20c GKH |
3600 | * device_unregister - unregister device from system. |
3601 | * @dev: device going away. | |
1da177e4 | 3602 | * |
4a3ad20c GKH |
3603 | * We do this in two parts, like we do device_register(). First, |
3604 | * we remove it from all the subsystems with device_del(), then | |
3605 | * we decrement the reference count via put_device(). If that | |
3606 | * is the final reference count, the device will be cleaned up | |
3607 | * via device_release() above. Otherwise, the structure will | |
3608 | * stick around until the final reference to the device is dropped. | |
1da177e4 | 3609 | */ |
4a3ad20c | 3610 | void device_unregister(struct device *dev) |
1da177e4 | 3611 | { |
1e0b2cf9 | 3612 | pr_debug("device: '%s': %s\n", dev_name(dev), __func__); |
1da177e4 LT |
3613 | device_del(dev); |
3614 | put_device(dev); | |
3615 | } | |
86df2687 | 3616 | EXPORT_SYMBOL_GPL(device_unregister); |
1da177e4 | 3617 | |
3d060aeb AS |
3618 | static struct device *prev_device(struct klist_iter *i) |
3619 | { | |
3620 | struct klist_node *n = klist_prev(i); | |
3621 | struct device *dev = NULL; | |
3622 | struct device_private *p; | |
3623 | ||
3624 | if (n) { | |
3625 | p = to_device_private_parent(n); | |
3626 | dev = p->device; | |
3627 | } | |
3628 | return dev; | |
3629 | } | |
3630 | ||
4a3ad20c | 3631 | static struct device *next_device(struct klist_iter *i) |
36239577 | 3632 | { |
4a3ad20c | 3633 | struct klist_node *n = klist_next(i); |
f791b8c8 GKH |
3634 | struct device *dev = NULL; |
3635 | struct device_private *p; | |
3636 | ||
3637 | if (n) { | |
3638 | p = to_device_private_parent(n); | |
3639 | dev = p->device; | |
3640 | } | |
3641 | return dev; | |
36239577 PM |
3642 | } |
3643 | ||
6fcf53ac | 3644 | /** |
e454cea2 | 3645 | * device_get_devnode - path of device node file |
6fcf53ac | 3646 | * @dev: device |
e454cea2 | 3647 | * @mode: returned file access mode |
3c2670e6 KS |
3648 | * @uid: returned file owner |
3649 | * @gid: returned file group | |
6fcf53ac KS |
3650 | * @tmp: possibly allocated string |
3651 | * | |
3652 | * Return the relative path of a possible device node. | |
3653 | * Non-default names may need to allocate a memory to compose | |
3654 | * a name. This memory is returned in tmp and needs to be | |
3655 | * freed by the caller. | |
3656 | */ | |
e454cea2 | 3657 | const char *device_get_devnode(struct device *dev, |
4e4098a3 | 3658 | umode_t *mode, kuid_t *uid, kgid_t *gid, |
3c2670e6 | 3659 | const char **tmp) |
6fcf53ac KS |
3660 | { |
3661 | char *s; | |
3662 | ||
3663 | *tmp = NULL; | |
3664 | ||
3665 | /* the device type may provide a specific name */ | |
e454cea2 | 3666 | if (dev->type && dev->type->devnode) |
3c2670e6 | 3667 | *tmp = dev->type->devnode(dev, mode, uid, gid); |
6fcf53ac KS |
3668 | if (*tmp) |
3669 | return *tmp; | |
3670 | ||
3671 | /* the class may provide a specific name */ | |
e454cea2 KS |
3672 | if (dev->class && dev->class->devnode) |
3673 | *tmp = dev->class->devnode(dev, mode); | |
6fcf53ac KS |
3674 | if (*tmp) |
3675 | return *tmp; | |
3676 | ||
3677 | /* return name without allocation, tmp == NULL */ | |
3678 | if (strchr(dev_name(dev), '!') == NULL) | |
3679 | return dev_name(dev); | |
3680 | ||
3681 | /* replace '!' in the name with '/' */ | |
a29fd614 RV |
3682 | s = kstrdup(dev_name(dev), GFP_KERNEL); |
3683 | if (!s) | |
6fcf53ac | 3684 | return NULL; |
a29fd614 RV |
3685 | strreplace(s, '!', '/'); |
3686 | return *tmp = s; | |
6fcf53ac KS |
3687 | } |
3688 | ||
1da177e4 | 3689 | /** |
4a3ad20c GKH |
3690 | * device_for_each_child - device child iterator. |
3691 | * @parent: parent struct device. | |
4a3ad20c | 3692 | * @fn: function to be called for each device. |
f8878dcb | 3693 | * @data: data for the callback. |
1da177e4 | 3694 | * |
4a3ad20c GKH |
3695 | * Iterate over @parent's child devices, and call @fn for each, |
3696 | * passing it @data. | |
1da177e4 | 3697 | * |
4a3ad20c GKH |
3698 | * We check the return of @fn each time. If it returns anything |
3699 | * other than 0, we break out and return that value. | |
1da177e4 | 3700 | */ |
4a3ad20c GKH |
3701 | int device_for_each_child(struct device *parent, void *data, |
3702 | int (*fn)(struct device *dev, void *data)) | |
1da177e4 | 3703 | { |
36239577 | 3704 | struct klist_iter i; |
4a3ad20c | 3705 | struct device *child; |
1da177e4 LT |
3706 | int error = 0; |
3707 | ||
014c90db GKH |
3708 | if (!parent->p) |
3709 | return 0; | |
3710 | ||
f791b8c8 | 3711 | klist_iter_init(&parent->p->klist_children, &i); |
93ead7c9 | 3712 | while (!error && (child = next_device(&i))) |
36239577 PM |
3713 | error = fn(child, data); |
3714 | klist_iter_exit(&i); | |
1da177e4 LT |
3715 | return error; |
3716 | } | |
86df2687 | 3717 | EXPORT_SYMBOL_GPL(device_for_each_child); |
1da177e4 | 3718 | |
3d060aeb AS |
3719 | /** |
3720 | * device_for_each_child_reverse - device child iterator in reversed order. | |
3721 | * @parent: parent struct device. | |
3722 | * @fn: function to be called for each device. | |
3723 | * @data: data for the callback. | |
3724 | * | |
3725 | * Iterate over @parent's child devices, and call @fn for each, | |
3726 | * passing it @data. | |
3727 | * | |
3728 | * We check the return of @fn each time. If it returns anything | |
3729 | * other than 0, we break out and return that value. | |
3730 | */ | |
3731 | int device_for_each_child_reverse(struct device *parent, void *data, | |
3732 | int (*fn)(struct device *dev, void *data)) | |
3733 | { | |
3734 | struct klist_iter i; | |
3735 | struct device *child; | |
3736 | int error = 0; | |
3737 | ||
3738 | if (!parent->p) | |
3739 | return 0; | |
3740 | ||
3741 | klist_iter_init(&parent->p->klist_children, &i); | |
3742 | while ((child = prev_device(&i)) && !error) | |
3743 | error = fn(child, data); | |
3744 | klist_iter_exit(&i); | |
3745 | return error; | |
3746 | } | |
3747 | EXPORT_SYMBOL_GPL(device_for_each_child_reverse); | |
3748 | ||
5ab69981 CH |
3749 | /** |
3750 | * device_find_child - device iterator for locating a particular device. | |
3751 | * @parent: parent struct device | |
5ab69981 | 3752 | * @match: Callback function to check device |
f8878dcb | 3753 | * @data: Data to pass to match function |
5ab69981 CH |
3754 | * |
3755 | * This is similar to the device_for_each_child() function above, but it | |
3756 | * returns a reference to a device that is 'found' for later use, as | |
3757 | * determined by the @match callback. | |
3758 | * | |
3759 | * The callback should return 0 if the device doesn't match and non-zero | |
3760 | * if it does. If the callback returns non-zero and a reference to the | |
3761 | * current device can be obtained, this function will return to the caller | |
3762 | * and not iterate over any more devices. | |
a4e2400a FV |
3763 | * |
3764 | * NOTE: you will need to drop the reference with put_device() after use. | |
5ab69981 | 3765 | */ |
4a3ad20c GKH |
3766 | struct device *device_find_child(struct device *parent, void *data, |
3767 | int (*match)(struct device *dev, void *data)) | |
5ab69981 CH |
3768 | { |
3769 | struct klist_iter i; | |
3770 | struct device *child; | |
3771 | ||
3772 | if (!parent) | |
3773 | return NULL; | |
3774 | ||
f791b8c8 | 3775 | klist_iter_init(&parent->p->klist_children, &i); |
5ab69981 CH |
3776 | while ((child = next_device(&i))) |
3777 | if (match(child, data) && get_device(child)) | |
3778 | break; | |
3779 | klist_iter_exit(&i); | |
3780 | return child; | |
3781 | } | |
86df2687 | 3782 | EXPORT_SYMBOL_GPL(device_find_child); |
5ab69981 | 3783 | |
dad9bb01 HK |
3784 | /** |
3785 | * device_find_child_by_name - device iterator for locating a child device. | |
3786 | * @parent: parent struct device | |
3787 | * @name: name of the child device | |
3788 | * | |
3789 | * This is similar to the device_find_child() function above, but it | |
3790 | * returns a reference to a device that has the name @name. | |
3791 | * | |
3792 | * NOTE: you will need to drop the reference with put_device() after use. | |
3793 | */ | |
3794 | struct device *device_find_child_by_name(struct device *parent, | |
3795 | const char *name) | |
3796 | { | |
3797 | struct klist_iter i; | |
3798 | struct device *child; | |
3799 | ||
3800 | if (!parent) | |
3801 | return NULL; | |
3802 | ||
3803 | klist_iter_init(&parent->p->klist_children, &i); | |
3804 | while ((child = next_device(&i))) | |
c77f520d | 3805 | if (sysfs_streq(dev_name(child), name) && get_device(child)) |
dad9bb01 HK |
3806 | break; |
3807 | klist_iter_exit(&i); | |
3808 | return child; | |
3809 | } | |
3810 | EXPORT_SYMBOL_GPL(device_find_child_by_name); | |
3811 | ||
1da177e4 LT |
3812 | int __init devices_init(void) |
3813 | { | |
881c6cfd GKH |
3814 | devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL); |
3815 | if (!devices_kset) | |
3816 | return -ENOMEM; | |
e105b8bf DW |
3817 | dev_kobj = kobject_create_and_add("dev", NULL); |
3818 | if (!dev_kobj) | |
3819 | goto dev_kobj_err; | |
3820 | sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj); | |
3821 | if (!sysfs_dev_block_kobj) | |
3822 | goto block_kobj_err; | |
3823 | sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj); | |
3824 | if (!sysfs_dev_char_kobj) | |
3825 | goto char_kobj_err; | |
3826 | ||
881c6cfd | 3827 | return 0; |
e105b8bf DW |
3828 | |
3829 | char_kobj_err: | |
3830 | kobject_put(sysfs_dev_block_kobj); | |
3831 | block_kobj_err: | |
3832 | kobject_put(dev_kobj); | |
3833 | dev_kobj_err: | |
3834 | kset_unregister(devices_kset); | |
3835 | return -ENOMEM; | |
1da177e4 LT |
3836 | } |
3837 | ||
4f3549d7 RW |
3838 | static int device_check_offline(struct device *dev, void *not_used) |
3839 | { | |
3840 | int ret; | |
3841 | ||
3842 | ret = device_for_each_child(dev, NULL, device_check_offline); | |
3843 | if (ret) | |
3844 | return ret; | |
3845 | ||
3846 | return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0; | |
3847 | } | |
3848 | ||
3849 | /** | |
3850 | * device_offline - Prepare the device for hot-removal. | |
3851 | * @dev: Device to be put offline. | |
3852 | * | |
3853 | * Execute the device bus type's .offline() callback, if present, to prepare | |
3854 | * the device for a subsequent hot-removal. If that succeeds, the device must | |
3855 | * not be used until either it is removed or its bus type's .online() callback | |
3856 | * is executed. | |
3857 | * | |
3858 | * Call under device_hotplug_lock. | |
3859 | */ | |
3860 | int device_offline(struct device *dev) | |
3861 | { | |
3862 | int ret; | |
3863 | ||
3864 | if (dev->offline_disabled) | |
3865 | return -EPERM; | |
3866 | ||
3867 | ret = device_for_each_child(dev, NULL, device_check_offline); | |
3868 | if (ret) | |
3869 | return ret; | |
3870 | ||
3871 | device_lock(dev); | |
3872 | if (device_supports_offline(dev)) { | |
3873 | if (dev->offline) { | |
3874 | ret = 1; | |
3875 | } else { | |
3876 | ret = dev->bus->offline(dev); | |
3877 | if (!ret) { | |
3878 | kobject_uevent(&dev->kobj, KOBJ_OFFLINE); | |
3879 | dev->offline = true; | |
3880 | } | |
3881 | } | |
3882 | } | |
3883 | device_unlock(dev); | |
3884 | ||
3885 | return ret; | |
3886 | } | |
3887 | ||
3888 | /** | |
3889 | * device_online - Put the device back online after successful device_offline(). | |
3890 | * @dev: Device to be put back online. | |
3891 | * | |
3892 | * If device_offline() has been successfully executed for @dev, but the device | |
3893 | * has not been removed subsequently, execute its bus type's .online() callback | |
3894 | * to indicate that the device can be used again. | |
3895 | * | |
3896 | * Call under device_hotplug_lock. | |
3897 | */ | |
3898 | int device_online(struct device *dev) | |
3899 | { | |
3900 | int ret = 0; | |
3901 | ||
3902 | device_lock(dev); | |
3903 | if (device_supports_offline(dev)) { | |
3904 | if (dev->offline) { | |
3905 | ret = dev->bus->online(dev); | |
3906 | if (!ret) { | |
3907 | kobject_uevent(&dev->kobj, KOBJ_ONLINE); | |
3908 | dev->offline = false; | |
3909 | } | |
3910 | } else { | |
3911 | ret = 1; | |
3912 | } | |
3913 | } | |
3914 | device_unlock(dev); | |
3915 | ||
3916 | return ret; | |
3917 | } | |
3918 | ||
7f100d15 | 3919 | struct root_device { |
0aa0dc41 MM |
3920 | struct device dev; |
3921 | struct module *owner; | |
3922 | }; | |
3923 | ||
93058424 | 3924 | static inline struct root_device *to_root_device(struct device *d) |
481e2079 FW |
3925 | { |
3926 | return container_of(d, struct root_device, dev); | |
3927 | } | |
0aa0dc41 MM |
3928 | |
3929 | static void root_device_release(struct device *dev) | |
3930 | { | |
3931 | kfree(to_root_device(dev)); | |
3932 | } | |
3933 | ||
3934 | /** | |
3935 | * __root_device_register - allocate and register a root device | |
3936 | * @name: root device name | |
3937 | * @owner: owner module of the root device, usually THIS_MODULE | |
3938 | * | |
3939 | * This function allocates a root device and registers it | |
3940 | * using device_register(). In order to free the returned | |
3941 | * device, use root_device_unregister(). | |
3942 | * | |
3943 | * Root devices are dummy devices which allow other devices | |
3944 | * to be grouped under /sys/devices. Use this function to | |
3945 | * allocate a root device and then use it as the parent of | |
3946 | * any device which should appear under /sys/devices/{name} | |
3947 | * | |
3948 | * The /sys/devices/{name} directory will also contain a | |
3949 | * 'module' symlink which points to the @owner directory | |
3950 | * in sysfs. | |
3951 | * | |
f0eae0ed JN |
3952 | * Returns &struct device pointer on success, or ERR_PTR() on error. |
3953 | * | |
0aa0dc41 MM |
3954 | * Note: You probably want to use root_device_register(). |
3955 | */ | |
3956 | struct device *__root_device_register(const char *name, struct module *owner) | |
3957 | { | |
3958 | struct root_device *root; | |
3959 | int err = -ENOMEM; | |
3960 | ||
3961 | root = kzalloc(sizeof(struct root_device), GFP_KERNEL); | |
3962 | if (!root) | |
3963 | return ERR_PTR(err); | |
3964 | ||
acc0e90f | 3965 | err = dev_set_name(&root->dev, "%s", name); |
0aa0dc41 MM |
3966 | if (err) { |
3967 | kfree(root); | |
3968 | return ERR_PTR(err); | |
3969 | } | |
3970 | ||
3971 | root->dev.release = root_device_release; | |
3972 | ||
3973 | err = device_register(&root->dev); | |
3974 | if (err) { | |
3975 | put_device(&root->dev); | |
3976 | return ERR_PTR(err); | |
3977 | } | |
3978 | ||
1d9e882b | 3979 | #ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */ |
0aa0dc41 MM |
3980 | if (owner) { |
3981 | struct module_kobject *mk = &owner->mkobj; | |
3982 | ||
3983 | err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module"); | |
3984 | if (err) { | |
3985 | device_unregister(&root->dev); | |
3986 | return ERR_PTR(err); | |
3987 | } | |
3988 | root->owner = owner; | |
3989 | } | |
3990 | #endif | |
3991 | ||
3992 | return &root->dev; | |
3993 | } | |
3994 | EXPORT_SYMBOL_GPL(__root_device_register); | |
3995 | ||
3996 | /** | |
3997 | * root_device_unregister - unregister and free a root device | |
7cbcf225 | 3998 | * @dev: device going away |
0aa0dc41 MM |
3999 | * |
4000 | * This function unregisters and cleans up a device that was created by | |
4001 | * root_device_register(). | |
4002 | */ | |
4003 | void root_device_unregister(struct device *dev) | |
4004 | { | |
4005 | struct root_device *root = to_root_device(dev); | |
4006 | ||
4007 | if (root->owner) | |
4008 | sysfs_remove_link(&root->dev.kobj, "module"); | |
4009 | ||
4010 | device_unregister(dev); | |
4011 | } | |
4012 | EXPORT_SYMBOL_GPL(root_device_unregister); | |
4013 | ||
23681e47 GKH |
4014 | |
4015 | static void device_create_release(struct device *dev) | |
4016 | { | |
1e0b2cf9 | 4017 | pr_debug("device: '%s': %s\n", dev_name(dev), __func__); |
23681e47 GKH |
4018 | kfree(dev); |
4019 | } | |
4020 | ||
6a8b55d7 | 4021 | static __printf(6, 0) struct device * |
39ef3112 GR |
4022 | device_create_groups_vargs(struct class *class, struct device *parent, |
4023 | dev_t devt, void *drvdata, | |
4024 | const struct attribute_group **groups, | |
4025 | const char *fmt, va_list args) | |
23681e47 | 4026 | { |
23681e47 GKH |
4027 | struct device *dev = NULL; |
4028 | int retval = -ENODEV; | |
4029 | ||
4030 | if (class == NULL || IS_ERR(class)) | |
4031 | goto error; | |
23681e47 GKH |
4032 | |
4033 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); | |
4034 | if (!dev) { | |
4035 | retval = -ENOMEM; | |
4036 | goto error; | |
4037 | } | |
4038 | ||
bbc780f8 | 4039 | device_initialize(dev); |
23681e47 GKH |
4040 | dev->devt = devt; |
4041 | dev->class = class; | |
4042 | dev->parent = parent; | |
39ef3112 | 4043 | dev->groups = groups; |
23681e47 | 4044 | dev->release = device_create_release; |
8882b394 | 4045 | dev_set_drvdata(dev, drvdata); |
23681e47 | 4046 | |
1fa5ae85 KS |
4047 | retval = kobject_set_name_vargs(&dev->kobj, fmt, args); |
4048 | if (retval) | |
4049 | goto error; | |
4050 | ||
bbc780f8 | 4051 | retval = device_add(dev); |
23681e47 GKH |
4052 | if (retval) |
4053 | goto error; | |
4054 | ||
23681e47 GKH |
4055 | return dev; |
4056 | ||
4057 | error: | |
286661b3 | 4058 | put_device(dev); |
23681e47 GKH |
4059 | return ERR_PTR(retval); |
4060 | } | |
39ef3112 | 4061 | |
8882b394 | 4062 | /** |
4e106739 | 4063 | * device_create - creates a device and registers it with sysfs |
8882b394 GKH |
4064 | * @class: pointer to the struct class that this device should be registered to |
4065 | * @parent: pointer to the parent struct device of this new device, if any | |
4066 | * @devt: the dev_t for the char device to be added | |
4067 | * @drvdata: the data to be added to the device for callbacks | |
4068 | * @fmt: string for the device's name | |
4069 | * | |
4070 | * This function can be used by char device classes. A struct device | |
4071 | * will be created in sysfs, registered to the specified class. | |
4072 | * | |
4073 | * A "dev" file will be created, showing the dev_t for the device, if | |
4074 | * the dev_t is not 0,0. | |
4075 | * If a pointer to a parent struct device is passed in, the newly created | |
4076 | * struct device will be a child of that device in sysfs. | |
4077 | * The pointer to the struct device will be returned from the call. | |
4078 | * Any further sysfs files that might be required can be created using this | |
4079 | * pointer. | |
4080 | * | |
f0eae0ed JN |
4081 | * Returns &struct device pointer on success, or ERR_PTR() on error. |
4082 | * | |
8882b394 GKH |
4083 | * Note: the struct class passed to this function must have previously |
4084 | * been created with a call to class_create(). | |
4085 | */ | |
4e106739 GKH |
4086 | struct device *device_create(struct class *class, struct device *parent, |
4087 | dev_t devt, void *drvdata, const char *fmt, ...) | |
8882b394 GKH |
4088 | { |
4089 | va_list vargs; | |
4090 | struct device *dev; | |
4091 | ||
4092 | va_start(vargs, fmt); | |
4c747466 CH |
4093 | dev = device_create_groups_vargs(class, parent, devt, drvdata, NULL, |
4094 | fmt, vargs); | |
8882b394 GKH |
4095 | va_end(vargs); |
4096 | return dev; | |
4097 | } | |
4e106739 | 4098 | EXPORT_SYMBOL_GPL(device_create); |
8882b394 | 4099 | |
39ef3112 GR |
4100 | /** |
4101 | * device_create_with_groups - creates a device and registers it with sysfs | |
4102 | * @class: pointer to the struct class that this device should be registered to | |
4103 | * @parent: pointer to the parent struct device of this new device, if any | |
4104 | * @devt: the dev_t for the char device to be added | |
4105 | * @drvdata: the data to be added to the device for callbacks | |
4106 | * @groups: NULL-terminated list of attribute groups to be created | |
4107 | * @fmt: string for the device's name | |
4108 | * | |
4109 | * This function can be used by char device classes. A struct device | |
4110 | * will be created in sysfs, registered to the specified class. | |
4111 | * Additional attributes specified in the groups parameter will also | |
4112 | * be created automatically. | |
4113 | * | |
4114 | * A "dev" file will be created, showing the dev_t for the device, if | |
4115 | * the dev_t is not 0,0. | |
4116 | * If a pointer to a parent struct device is passed in, the newly created | |
4117 | * struct device will be a child of that device in sysfs. | |
4118 | * The pointer to the struct device will be returned from the call. | |
4119 | * Any further sysfs files that might be required can be created using this | |
4120 | * pointer. | |
4121 | * | |
4122 | * Returns &struct device pointer on success, or ERR_PTR() on error. | |
4123 | * | |
4124 | * Note: the struct class passed to this function must have previously | |
4125 | * been created with a call to class_create(). | |
4126 | */ | |
4127 | struct device *device_create_with_groups(struct class *class, | |
4128 | struct device *parent, dev_t devt, | |
4129 | void *drvdata, | |
4130 | const struct attribute_group **groups, | |
4131 | const char *fmt, ...) | |
4132 | { | |
4133 | va_list vargs; | |
4134 | struct device *dev; | |
4135 | ||
4136 | va_start(vargs, fmt); | |
4137 | dev = device_create_groups_vargs(class, parent, devt, drvdata, groups, | |
4138 | fmt, vargs); | |
4139 | va_end(vargs); | |
4140 | return dev; | |
4141 | } | |
4142 | EXPORT_SYMBOL_GPL(device_create_with_groups); | |
4143 | ||
775b64d2 RW |
4144 | /** |
4145 | * device_destroy - removes a device that was created with device_create() | |
4146 | * @class: pointer to the struct class that this device was registered with | |
4147 | * @devt: the dev_t of the device that was previously registered | |
4148 | * | |
4149 | * This call unregisters and cleans up a device that was created with a | |
4150 | * call to device_create(). | |
4151 | */ | |
4152 | void device_destroy(struct class *class, dev_t devt) | |
4153 | { | |
4154 | struct device *dev; | |
23681e47 | 4155 | |
4495dfdd | 4156 | dev = class_find_device_by_devt(class, devt); |
cd35449b DY |
4157 | if (dev) { |
4158 | put_device(dev); | |
23681e47 | 4159 | device_unregister(dev); |
cd35449b | 4160 | } |
23681e47 GKH |
4161 | } |
4162 | EXPORT_SYMBOL_GPL(device_destroy); | |
a2de48ca GKH |
4163 | |
4164 | /** | |
4165 | * device_rename - renames a device | |
4166 | * @dev: the pointer to the struct device to be renamed | |
4167 | * @new_name: the new name of the device | |
030c1d2b EB |
4168 | * |
4169 | * It is the responsibility of the caller to provide mutual | |
4170 | * exclusion between two different calls of device_rename | |
4171 | * on the same device to ensure that new_name is valid and | |
4172 | * won't conflict with other devices. | |
c6c0ac66 | 4173 | * |
a5462516 TT |
4174 | * Note: Don't call this function. Currently, the networking layer calls this |
4175 | * function, but that will change. The following text from Kay Sievers offers | |
4176 | * some insight: | |
4177 | * | |
4178 | * Renaming devices is racy at many levels, symlinks and other stuff are not | |
4179 | * replaced atomically, and you get a "move" uevent, but it's not easy to | |
4180 | * connect the event to the old and new device. Device nodes are not renamed at | |
4181 | * all, there isn't even support for that in the kernel now. | |
4182 | * | |
4183 | * In the meantime, during renaming, your target name might be taken by another | |
4184 | * driver, creating conflicts. Or the old name is taken directly after you | |
4185 | * renamed it -- then you get events for the same DEVPATH, before you even see | |
4186 | * the "move" event. It's just a mess, and nothing new should ever rely on | |
4187 | * kernel device renaming. Besides that, it's not even implemented now for | |
4188 | * other things than (driver-core wise very simple) network devices. | |
4189 | * | |
4190 | * We are currently about to change network renaming in udev to completely | |
4191 | * disallow renaming of devices in the same namespace as the kernel uses, | |
4192 | * because we can't solve the problems properly, that arise with swapping names | |
4193 | * of multiple interfaces without races. Means, renaming of eth[0-9]* will only | |
4194 | * be allowed to some other name than eth[0-9]*, for the aforementioned | |
4195 | * reasons. | |
4196 | * | |
4197 | * Make up a "real" name in the driver before you register anything, or add | |
4198 | * some other attributes for userspace to find the device, or use udev to add | |
4199 | * symlinks -- but never rename kernel devices later, it's a complete mess. We | |
4200 | * don't even want to get into that and try to implement the missing pieces in | |
4201 | * the core. We really have other pieces to fix in the driver core mess. :) | |
a2de48ca | 4202 | */ |
6937e8f8 | 4203 | int device_rename(struct device *dev, const char *new_name) |
a2de48ca | 4204 | { |
4b30ee58 | 4205 | struct kobject *kobj = &dev->kobj; |
2ee97caf | 4206 | char *old_device_name = NULL; |
a2de48ca GKH |
4207 | int error; |
4208 | ||
4209 | dev = get_device(dev); | |
4210 | if (!dev) | |
4211 | return -EINVAL; | |
4212 | ||
69df7533 | 4213 | dev_dbg(dev, "renaming to %s\n", new_name); |
a2de48ca | 4214 | |
1fa5ae85 | 4215 | old_device_name = kstrdup(dev_name(dev), GFP_KERNEL); |
2ee97caf CH |
4216 | if (!old_device_name) { |
4217 | error = -ENOMEM; | |
4218 | goto out; | |
a2de48ca | 4219 | } |
a2de48ca | 4220 | |
f349cf34 | 4221 | if (dev->class) { |
4b30ee58 TH |
4222 | error = sysfs_rename_link_ns(&dev->class->p->subsys.kobj, |
4223 | kobj, old_device_name, | |
4224 | new_name, kobject_namespace(kobj)); | |
f349cf34 EB |
4225 | if (error) |
4226 | goto out; | |
4227 | } | |
39aba963 | 4228 | |
4b30ee58 | 4229 | error = kobject_rename(kobj, new_name); |
1fa5ae85 | 4230 | if (error) |
2ee97caf | 4231 | goto out; |
a2de48ca | 4232 | |
2ee97caf | 4233 | out: |
a2de48ca GKH |
4234 | put_device(dev); |
4235 | ||
2ee97caf | 4236 | kfree(old_device_name); |
a2de48ca GKH |
4237 | |
4238 | return error; | |
4239 | } | |
a2807dbc | 4240 | EXPORT_SYMBOL_GPL(device_rename); |
8a82472f CH |
4241 | |
4242 | static int device_move_class_links(struct device *dev, | |
4243 | struct device *old_parent, | |
4244 | struct device *new_parent) | |
4245 | { | |
f7f3461d | 4246 | int error = 0; |
8a82472f | 4247 | |
f7f3461d GKH |
4248 | if (old_parent) |
4249 | sysfs_remove_link(&dev->kobj, "device"); | |
4250 | if (new_parent) | |
4251 | error = sysfs_create_link(&dev->kobj, &new_parent->kobj, | |
4252 | "device"); | |
4253 | return error; | |
8a82472f CH |
4254 | } |
4255 | ||
4256 | /** | |
4257 | * device_move - moves a device to a new parent | |
4258 | * @dev: the pointer to the struct device to be moved | |
13509860 | 4259 | * @new_parent: the new parent of the device (can be NULL) |
ffa6a705 | 4260 | * @dpm_order: how to reorder the dpm_list |
8a82472f | 4261 | */ |
ffa6a705 CH |
4262 | int device_move(struct device *dev, struct device *new_parent, |
4263 | enum dpm_order dpm_order) | |
8a82472f CH |
4264 | { |
4265 | int error; | |
4266 | struct device *old_parent; | |
c744aeae | 4267 | struct kobject *new_parent_kobj; |
8a82472f CH |
4268 | |
4269 | dev = get_device(dev); | |
4270 | if (!dev) | |
4271 | return -EINVAL; | |
4272 | ||
ffa6a705 | 4273 | device_pm_lock(); |
8a82472f | 4274 | new_parent = get_device(new_parent); |
4a3ad20c | 4275 | new_parent_kobj = get_device_parent(dev, new_parent); |
84d0c27d TH |
4276 | if (IS_ERR(new_parent_kobj)) { |
4277 | error = PTR_ERR(new_parent_kobj); | |
4278 | put_device(new_parent); | |
4279 | goto out; | |
4280 | } | |
63b6971a | 4281 | |
1e0b2cf9 KS |
4282 | pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev), |
4283 | __func__, new_parent ? dev_name(new_parent) : "<NULL>"); | |
c744aeae | 4284 | error = kobject_move(&dev->kobj, new_parent_kobj); |
8a82472f | 4285 | if (error) { |
63b6971a | 4286 | cleanup_glue_dir(dev, new_parent_kobj); |
8a82472f CH |
4287 | put_device(new_parent); |
4288 | goto out; | |
4289 | } | |
4290 | old_parent = dev->parent; | |
4291 | dev->parent = new_parent; | |
4292 | if (old_parent) | |
f791b8c8 | 4293 | klist_remove(&dev->p->knode_parent); |
0d358f22 | 4294 | if (new_parent) { |
f791b8c8 GKH |
4295 | klist_add_tail(&dev->p->knode_parent, |
4296 | &new_parent->p->klist_children); | |
0d358f22 YL |
4297 | set_dev_node(dev, dev_to_node(new_parent)); |
4298 | } | |
4299 | ||
bdd4034d RV |
4300 | if (dev->class) { |
4301 | error = device_move_class_links(dev, old_parent, new_parent); | |
4302 | if (error) { | |
4303 | /* We ignore errors on cleanup since we're hosed anyway... */ | |
4304 | device_move_class_links(dev, new_parent, old_parent); | |
4305 | if (!kobject_move(&dev->kobj, &old_parent->kobj)) { | |
4306 | if (new_parent) | |
4307 | klist_remove(&dev->p->knode_parent); | |
4308 | dev->parent = old_parent; | |
4309 | if (old_parent) { | |
4310 | klist_add_tail(&dev->p->knode_parent, | |
4311 | &old_parent->p->klist_children); | |
4312 | set_dev_node(dev, dev_to_node(old_parent)); | |
4313 | } | |
0d358f22 | 4314 | } |
bdd4034d RV |
4315 | cleanup_glue_dir(dev, new_parent_kobj); |
4316 | put_device(new_parent); | |
4317 | goto out; | |
8a82472f | 4318 | } |
8a82472f | 4319 | } |
ffa6a705 CH |
4320 | switch (dpm_order) { |
4321 | case DPM_ORDER_NONE: | |
4322 | break; | |
4323 | case DPM_ORDER_DEV_AFTER_PARENT: | |
4324 | device_pm_move_after(dev, new_parent); | |
52cdbdd4 | 4325 | devices_kset_move_after(dev, new_parent); |
ffa6a705 CH |
4326 | break; |
4327 | case DPM_ORDER_PARENT_BEFORE_DEV: | |
4328 | device_pm_move_before(new_parent, dev); | |
52cdbdd4 | 4329 | devices_kset_move_before(new_parent, dev); |
ffa6a705 CH |
4330 | break; |
4331 | case DPM_ORDER_DEV_LAST: | |
4332 | device_pm_move_last(dev); | |
52cdbdd4 | 4333 | devices_kset_move_last(dev); |
ffa6a705 CH |
4334 | break; |
4335 | } | |
bdd4034d | 4336 | |
8a82472f CH |
4337 | put_device(old_parent); |
4338 | out: | |
ffa6a705 | 4339 | device_pm_unlock(); |
8a82472f CH |
4340 | put_device(dev); |
4341 | return error; | |
4342 | } | |
8a82472f | 4343 | EXPORT_SYMBOL_GPL(device_move); |
37b0c020 | 4344 | |
b8f33e5d CB |
4345 | static int device_attrs_change_owner(struct device *dev, kuid_t kuid, |
4346 | kgid_t kgid) | |
4347 | { | |
4348 | struct kobject *kobj = &dev->kobj; | |
4349 | struct class *class = dev->class; | |
4350 | const struct device_type *type = dev->type; | |
4351 | int error; | |
4352 | ||
4353 | if (class) { | |
4354 | /* | |
4355 | * Change the device groups of the device class for @dev to | |
4356 | * @kuid/@kgid. | |
4357 | */ | |
4358 | error = sysfs_groups_change_owner(kobj, class->dev_groups, kuid, | |
4359 | kgid); | |
4360 | if (error) | |
4361 | return error; | |
4362 | } | |
4363 | ||
4364 | if (type) { | |
4365 | /* | |
4366 | * Change the device groups of the device type for @dev to | |
4367 | * @kuid/@kgid. | |
4368 | */ | |
4369 | error = sysfs_groups_change_owner(kobj, type->groups, kuid, | |
4370 | kgid); | |
4371 | if (error) | |
4372 | return error; | |
4373 | } | |
4374 | ||
4375 | /* Change the device groups of @dev to @kuid/@kgid. */ | |
4376 | error = sysfs_groups_change_owner(kobj, dev->groups, kuid, kgid); | |
4377 | if (error) | |
4378 | return error; | |
4379 | ||
4380 | if (device_supports_offline(dev) && !dev->offline_disabled) { | |
4381 | /* Change online device attributes of @dev to @kuid/@kgid. */ | |
4382 | error = sysfs_file_change_owner(kobj, dev_attr_online.attr.name, | |
4383 | kuid, kgid); | |
4384 | if (error) | |
4385 | return error; | |
4386 | } | |
4387 | ||
4388 | return 0; | |
4389 | } | |
4390 | ||
4391 | /** | |
4392 | * device_change_owner - change the owner of an existing device. | |
4393 | * @dev: device. | |
4394 | * @kuid: new owner's kuid | |
4395 | * @kgid: new owner's kgid | |
4396 | * | |
4397 | * This changes the owner of @dev and its corresponding sysfs entries to | |
4398 | * @kuid/@kgid. This function closely mirrors how @dev was added via driver | |
4399 | * core. | |
4400 | * | |
4401 | * Returns 0 on success or error code on failure. | |
4402 | */ | |
4403 | int device_change_owner(struct device *dev, kuid_t kuid, kgid_t kgid) | |
4404 | { | |
4405 | int error; | |
4406 | struct kobject *kobj = &dev->kobj; | |
4407 | ||
4408 | dev = get_device(dev); | |
4409 | if (!dev) | |
4410 | return -EINVAL; | |
4411 | ||
4412 | /* | |
4413 | * Change the kobject and the default attributes and groups of the | |
4414 | * ktype associated with it to @kuid/@kgid. | |
4415 | */ | |
4416 | error = sysfs_change_owner(kobj, kuid, kgid); | |
4417 | if (error) | |
4418 | goto out; | |
4419 | ||
4420 | /* | |
4421 | * Change the uevent file for @dev to the new owner. The uevent file | |
4422 | * was created in a separate step when @dev got added and we mirror | |
4423 | * that step here. | |
4424 | */ | |
4425 | error = sysfs_file_change_owner(kobj, dev_attr_uevent.attr.name, kuid, | |
4426 | kgid); | |
4427 | if (error) | |
4428 | goto out; | |
4429 | ||
4430 | /* | |
4431 | * Change the device groups, the device groups associated with the | |
4432 | * device class, and the groups associated with the device type of @dev | |
4433 | * to @kuid/@kgid. | |
4434 | */ | |
4435 | error = device_attrs_change_owner(dev, kuid, kgid); | |
4436 | if (error) | |
4437 | goto out; | |
4438 | ||
3b52fc5d CB |
4439 | error = dpm_sysfs_change_owner(dev, kuid, kgid); |
4440 | if (error) | |
4441 | goto out; | |
4442 | ||
b8f33e5d CB |
4443 | #ifdef CONFIG_BLOCK |
4444 | if (sysfs_deprecated && dev->class == &block_class) | |
4445 | goto out; | |
4446 | #endif | |
4447 | ||
4448 | /* | |
4449 | * Change the owner of the symlink located in the class directory of | |
4450 | * the device class associated with @dev which points to the actual | |
4451 | * directory entry for @dev to @kuid/@kgid. This ensures that the | |
4452 | * symlink shows the same permissions as its target. | |
4453 | */ | |
4454 | error = sysfs_link_change_owner(&dev->class->p->subsys.kobj, &dev->kobj, | |
4455 | dev_name(dev), kuid, kgid); | |
4456 | if (error) | |
4457 | goto out; | |
4458 | ||
4459 | out: | |
4460 | put_device(dev); | |
4461 | return error; | |
4462 | } | |
4463 | EXPORT_SYMBOL_GPL(device_change_owner); | |
4464 | ||
37b0c020 GKH |
4465 | /** |
4466 | * device_shutdown - call ->shutdown() on each device to shutdown. | |
4467 | */ | |
4468 | void device_shutdown(void) | |
4469 | { | |
f123db8e | 4470 | struct device *dev, *parent; |
6245838f | 4471 | |
3297c8fc PL |
4472 | wait_for_device_probe(); |
4473 | device_block_probing(); | |
4474 | ||
65650b35 RW |
4475 | cpufreq_suspend(); |
4476 | ||
6245838f HD |
4477 | spin_lock(&devices_kset->list_lock); |
4478 | /* | |
4479 | * Walk the devices list backward, shutting down each in turn. | |
4480 | * Beware that device unplug events may also start pulling | |
4481 | * devices offline, even as the system is shutting down. | |
4482 | */ | |
4483 | while (!list_empty(&devices_kset->list)) { | |
4484 | dev = list_entry(devices_kset->list.prev, struct device, | |
4485 | kobj.entry); | |
d1c6c030 ML |
4486 | |
4487 | /* | |
4488 | * hold reference count of device's parent to | |
4489 | * prevent it from being freed because parent's | |
4490 | * lock is to be held | |
4491 | */ | |
f123db8e | 4492 | parent = get_device(dev->parent); |
6245838f HD |
4493 | get_device(dev); |
4494 | /* | |
4495 | * Make sure the device is off the kset list, in the | |
4496 | * event that dev->*->shutdown() doesn't remove it. | |
4497 | */ | |
4498 | list_del_init(&dev->kobj.entry); | |
4499 | spin_unlock(&devices_kset->list_lock); | |
fe6b91f4 | 4500 | |
d1c6c030 | 4501 | /* hold lock to avoid race with probe/release */ |
f123db8e BL |
4502 | if (parent) |
4503 | device_lock(parent); | |
d1c6c030 ML |
4504 | device_lock(dev); |
4505 | ||
fe6b91f4 AS |
4506 | /* Don't allow any more runtime suspends */ |
4507 | pm_runtime_get_noresume(dev); | |
4508 | pm_runtime_barrier(dev); | |
37b0c020 | 4509 | |
7521621e | 4510 | if (dev->class && dev->class->shutdown_pre) { |
f77af151 | 4511 | if (initcall_debug) |
7521621e MS |
4512 | dev_info(dev, "shutdown_pre\n"); |
4513 | dev->class->shutdown_pre(dev); | |
4514 | } | |
4515 | if (dev->bus && dev->bus->shutdown) { | |
0246c4fa SL |
4516 | if (initcall_debug) |
4517 | dev_info(dev, "shutdown\n"); | |
37b0c020 GKH |
4518 | dev->bus->shutdown(dev); |
4519 | } else if (dev->driver && dev->driver->shutdown) { | |
0246c4fa SL |
4520 | if (initcall_debug) |
4521 | dev_info(dev, "shutdown\n"); | |
37b0c020 GKH |
4522 | dev->driver->shutdown(dev); |
4523 | } | |
d1c6c030 ML |
4524 | |
4525 | device_unlock(dev); | |
f123db8e BL |
4526 | if (parent) |
4527 | device_unlock(parent); | |
d1c6c030 | 4528 | |
6245838f | 4529 | put_device(dev); |
f123db8e | 4530 | put_device(parent); |
6245838f HD |
4531 | |
4532 | spin_lock(&devices_kset->list_lock); | |
37b0c020 | 4533 | } |
6245838f | 4534 | spin_unlock(&devices_kset->list_lock); |
37b0c020 | 4535 | } |
99bcf217 JP |
4536 | |
4537 | /* | |
4538 | * Device logging functions | |
4539 | */ | |
4540 | ||
4541 | #ifdef CONFIG_PRINTK | |
74caba7f JO |
4542 | static void |
4543 | set_dev_info(const struct device *dev, struct dev_printk_info *dev_info) | |
99bcf217 | 4544 | { |
c4e00daa | 4545 | const char *subsys; |
74caba7f JO |
4546 | |
4547 | memset(dev_info, 0, sizeof(*dev_info)); | |
99bcf217 | 4548 | |
c4e00daa KS |
4549 | if (dev->class) |
4550 | subsys = dev->class->name; | |
4551 | else if (dev->bus) | |
4552 | subsys = dev->bus->name; | |
4553 | else | |
74caba7f | 4554 | return; |
c4e00daa | 4555 | |
74caba7f | 4556 | strscpy(dev_info->subsystem, subsys, sizeof(dev_info->subsystem)); |
c4e00daa KS |
4557 | |
4558 | /* | |
4559 | * Add device identifier DEVICE=: | |
4560 | * b12:8 block dev_t | |
4561 | * c127:3 char dev_t | |
4562 | * n8 netdev ifindex | |
4563 | * +sound:card0 subsystem:devname | |
4564 | */ | |
4565 | if (MAJOR(dev->devt)) { | |
4566 | char c; | |
4567 | ||
4568 | if (strcmp(subsys, "block") == 0) | |
4569 | c = 'b'; | |
4570 | else | |
4571 | c = 'c'; | |
74caba7f JO |
4572 | |
4573 | snprintf(dev_info->device, sizeof(dev_info->device), | |
4574 | "%c%u:%u", c, MAJOR(dev->devt), MINOR(dev->devt)); | |
c4e00daa KS |
4575 | } else if (strcmp(subsys, "net") == 0) { |
4576 | struct net_device *net = to_net_dev(dev); | |
4577 | ||
74caba7f JO |
4578 | snprintf(dev_info->device, sizeof(dev_info->device), |
4579 | "n%u", net->ifindex); | |
c4e00daa | 4580 | } else { |
74caba7f JO |
4581 | snprintf(dev_info->device, sizeof(dev_info->device), |
4582 | "+%s:%s", subsys, dev_name(dev)); | |
c4e00daa | 4583 | } |
798efc60 | 4584 | } |
798efc60 | 4585 | |
05e4e5b8 JP |
4586 | int dev_vprintk_emit(int level, const struct device *dev, |
4587 | const char *fmt, va_list args) | |
4588 | { | |
74caba7f | 4589 | struct dev_printk_info dev_info; |
05e4e5b8 | 4590 | |
74caba7f | 4591 | set_dev_info(dev, &dev_info); |
05e4e5b8 | 4592 | |
74caba7f | 4593 | return vprintk_emit(0, level, &dev_info, fmt, args); |
05e4e5b8 JP |
4594 | } |
4595 | EXPORT_SYMBOL(dev_vprintk_emit); | |
4596 | ||
4597 | int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...) | |
4598 | { | |
4599 | va_list args; | |
4600 | int r; | |
4601 | ||
4602 | va_start(args, fmt); | |
4603 | ||
4604 | r = dev_vprintk_emit(level, dev, fmt, args); | |
4605 | ||
4606 | va_end(args); | |
4607 | ||
4608 | return r; | |
4609 | } | |
4610 | EXPORT_SYMBOL(dev_printk_emit); | |
4611 | ||
d1f1052c | 4612 | static void __dev_printk(const char *level, const struct device *dev, |
798efc60 JP |
4613 | struct va_format *vaf) |
4614 | { | |
d1f1052c JP |
4615 | if (dev) |
4616 | dev_printk_emit(level[1] - '0', dev, "%s %s: %pV", | |
4617 | dev_driver_string(dev), dev_name(dev), vaf); | |
4618 | else | |
4619 | printk("%s(NULL device *): %pV", level, vaf); | |
99bcf217 JP |
4620 | } |
4621 | ||
ad7d61f1 CD |
4622 | void _dev_printk(const char *level, const struct device *dev, |
4623 | const char *fmt, ...) | |
99bcf217 JP |
4624 | { |
4625 | struct va_format vaf; | |
4626 | va_list args; | |
99bcf217 JP |
4627 | |
4628 | va_start(args, fmt); | |
4629 | ||
4630 | vaf.fmt = fmt; | |
4631 | vaf.va = &args; | |
4632 | ||
d1f1052c | 4633 | __dev_printk(level, dev, &vaf); |
798efc60 | 4634 | |
99bcf217 | 4635 | va_end(args); |
99bcf217 | 4636 | } |
ad7d61f1 | 4637 | EXPORT_SYMBOL(_dev_printk); |
99bcf217 JP |
4638 | |
4639 | #define define_dev_printk_level(func, kern_level) \ | |
d1f1052c | 4640 | void func(const struct device *dev, const char *fmt, ...) \ |
99bcf217 JP |
4641 | { \ |
4642 | struct va_format vaf; \ | |
4643 | va_list args; \ | |
99bcf217 JP |
4644 | \ |
4645 | va_start(args, fmt); \ | |
4646 | \ | |
4647 | vaf.fmt = fmt; \ | |
4648 | vaf.va = &args; \ | |
4649 | \ | |
d1f1052c | 4650 | __dev_printk(kern_level, dev, &vaf); \ |
798efc60 | 4651 | \ |
99bcf217 | 4652 | va_end(args); \ |
99bcf217 JP |
4653 | } \ |
4654 | EXPORT_SYMBOL(func); | |
4655 | ||
663336ee JP |
4656 | define_dev_printk_level(_dev_emerg, KERN_EMERG); |
4657 | define_dev_printk_level(_dev_alert, KERN_ALERT); | |
4658 | define_dev_printk_level(_dev_crit, KERN_CRIT); | |
4659 | define_dev_printk_level(_dev_err, KERN_ERR); | |
4660 | define_dev_printk_level(_dev_warn, KERN_WARNING); | |
4661 | define_dev_printk_level(_dev_notice, KERN_NOTICE); | |
99bcf217 JP |
4662 | define_dev_printk_level(_dev_info, KERN_INFO); |
4663 | ||
4664 | #endif | |
97badf87 | 4665 | |
a787e540 AH |
4666 | /** |
4667 | * dev_err_probe - probe error check and log helper | |
4668 | * @dev: the pointer to the struct device | |
4669 | * @err: error value to test | |
4670 | * @fmt: printf-style format string | |
4671 | * @...: arguments as specified in the format string | |
4672 | * | |
4673 | * This helper implements common pattern present in probe functions for error | |
4674 | * checking: print debug or error message depending if the error value is | |
4675 | * -EPROBE_DEFER and propagate error upwards. | |
d090b70e AH |
4676 | * In case of -EPROBE_DEFER it sets also defer probe reason, which can be |
4677 | * checked later by reading devices_deferred debugfs attribute. | |
074b3aad MCC |
4678 | * It replaces code sequence:: |
4679 | * | |
a787e540 AH |
4680 | * if (err != -EPROBE_DEFER) |
4681 | * dev_err(dev, ...); | |
4682 | * else | |
4683 | * dev_dbg(dev, ...); | |
4684 | * return err; | |
074b3aad MCC |
4685 | * |
4686 | * with:: | |
4687 | * | |
a787e540 AH |
4688 | * return dev_err_probe(dev, err, ...); |
4689 | * | |
4690 | * Returns @err. | |
4691 | * | |
4692 | */ | |
4693 | int dev_err_probe(const struct device *dev, int err, const char *fmt, ...) | |
4694 | { | |
4695 | struct va_format vaf; | |
4696 | va_list args; | |
4697 | ||
4698 | va_start(args, fmt); | |
4699 | vaf.fmt = fmt; | |
4700 | vaf.va = &args; | |
4701 | ||
d090b70e | 4702 | if (err != -EPROBE_DEFER) { |
693a8e93 | 4703 | dev_err(dev, "error %pe: %pV", ERR_PTR(err), &vaf); |
d090b70e AH |
4704 | } else { |
4705 | device_set_deferred_probe_reason(dev, &vaf); | |
693a8e93 | 4706 | dev_dbg(dev, "error %pe: %pV", ERR_PTR(err), &vaf); |
d090b70e | 4707 | } |
a787e540 AH |
4708 | |
4709 | va_end(args); | |
4710 | ||
4711 | return err; | |
4712 | } | |
4713 | EXPORT_SYMBOL_GPL(dev_err_probe); | |
4714 | ||
97badf87 RW |
4715 | static inline bool fwnode_is_primary(struct fwnode_handle *fwnode) |
4716 | { | |
4717 | return fwnode && !IS_ERR(fwnode->secondary); | |
4718 | } | |
4719 | ||
4720 | /** | |
4721 | * set_primary_fwnode - Change the primary firmware node of a given device. | |
4722 | * @dev: Device to handle. | |
4723 | * @fwnode: New primary firmware node of the device. | |
4724 | * | |
4725 | * Set the device's firmware node pointer to @fwnode, but if a secondary | |
4726 | * firmware node of the device is present, preserve it. | |
3f7bddaf BL |
4727 | * |
4728 | * Valid fwnode cases are: | |
4729 | * - primary --> secondary --> -ENODEV | |
4730 | * - primary --> NULL | |
4731 | * - secondary --> -ENODEV | |
4732 | * - NULL | |
97badf87 RW |
4733 | */ |
4734 | void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode) | |
4735 | { | |
99aed922 | 4736 | struct device *parent = dev->parent; |
c15e1bdd | 4737 | struct fwnode_handle *fn = dev->fwnode; |
97badf87 | 4738 | |
c15e1bdd | 4739 | if (fwnode) { |
97badf87 RW |
4740 | if (fwnode_is_primary(fn)) |
4741 | fn = fn->secondary; | |
4742 | ||
55f89a8a MW |
4743 | if (fn) { |
4744 | WARN_ON(fwnode->secondary); | |
4745 | fwnode->secondary = fn; | |
4746 | } | |
97badf87 RW |
4747 | dev->fwnode = fwnode; |
4748 | } else { | |
c15e1bdd HK |
4749 | if (fwnode_is_primary(fn)) { |
4750 | dev->fwnode = fn->secondary; | |
3f7bddaf | 4751 | /* Set fn->secondary = NULL, so fn remains the primary fwnode */ |
99aed922 | 4752 | if (!(parent && fn == parent->fwnode)) |
47f44699 | 4753 | fn->secondary = NULL; |
c15e1bdd HK |
4754 | } else { |
4755 | dev->fwnode = NULL; | |
4756 | } | |
97badf87 RW |
4757 | } |
4758 | } | |
4759 | EXPORT_SYMBOL_GPL(set_primary_fwnode); | |
4760 | ||
4761 | /** | |
4762 | * set_secondary_fwnode - Change the secondary firmware node of a given device. | |
4763 | * @dev: Device to handle. | |
4764 | * @fwnode: New secondary firmware node of the device. | |
4765 | * | |
4766 | * If a primary firmware node of the device is present, set its secondary | |
4767 | * pointer to @fwnode. Otherwise, set the device's firmware node pointer to | |
4768 | * @fwnode. | |
4769 | */ | |
4770 | void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode) | |
4771 | { | |
4772 | if (fwnode) | |
4773 | fwnode->secondary = ERR_PTR(-ENODEV); | |
4774 | ||
4775 | if (fwnode_is_primary(dev->fwnode)) | |
4776 | dev->fwnode->secondary = fwnode; | |
4777 | else | |
4778 | dev->fwnode = fwnode; | |
4779 | } | |
96489ae1 | 4780 | EXPORT_SYMBOL_GPL(set_secondary_fwnode); |
4e75e1d7 JH |
4781 | |
4782 | /** | |
4783 | * device_set_of_node_from_dev - reuse device-tree node of another device | |
4784 | * @dev: device whose device-tree node is being set | |
4785 | * @dev2: device whose device-tree node is being reused | |
4786 | * | |
4787 | * Takes another reference to the new device-tree node after first dropping | |
4788 | * any reference held to the old node. | |
4789 | */ | |
4790 | void device_set_of_node_from_dev(struct device *dev, const struct device *dev2) | |
4791 | { | |
4792 | of_node_put(dev->of_node); | |
4793 | dev->of_node = of_node_get(dev2->of_node); | |
4794 | dev->of_node_reused = true; | |
4795 | } | |
4796 | EXPORT_SYMBOL_GPL(device_set_of_node_from_dev); | |
65b66682 | 4797 | |
43e76d46 IC |
4798 | void device_set_node(struct device *dev, struct fwnode_handle *fwnode) |
4799 | { | |
4800 | dev->fwnode = fwnode; | |
4801 | dev->of_node = to_of_node(fwnode); | |
4802 | } | |
4803 | EXPORT_SYMBOL_GPL(device_set_node); | |
4804 | ||
6cda08a2 SP |
4805 | int device_match_name(struct device *dev, const void *name) |
4806 | { | |
4807 | return sysfs_streq(dev_name(dev), name); | |
4808 | } | |
4809 | EXPORT_SYMBOL_GPL(device_match_name); | |
4810 | ||
65b66682 SP |
4811 | int device_match_of_node(struct device *dev, const void *np) |
4812 | { | |
4813 | return dev->of_node == np; | |
4814 | } | |
4815 | EXPORT_SYMBOL_GPL(device_match_of_node); | |
67843bba SP |
4816 | |
4817 | int device_match_fwnode(struct device *dev, const void *fwnode) | |
4818 | { | |
4819 | return dev_fwnode(dev) == fwnode; | |
4820 | } | |
4821 | EXPORT_SYMBOL_GPL(device_match_fwnode); | |
4495dfdd SP |
4822 | |
4823 | int device_match_devt(struct device *dev, const void *pdevt) | |
4824 | { | |
4825 | return dev->devt == *(dev_t *)pdevt; | |
4826 | } | |
4827 | EXPORT_SYMBOL_GPL(device_match_devt); | |
00500147 SP |
4828 | |
4829 | int device_match_acpi_dev(struct device *dev, const void *adev) | |
4830 | { | |
4831 | return ACPI_COMPANION(dev) == adev; | |
4832 | } | |
4833 | EXPORT_SYMBOL(device_match_acpi_dev); | |
6bf85ba9 SP |
4834 | |
4835 | int device_match_any(struct device *dev, const void *unused) | |
4836 | { | |
4837 | return 1; | |
4838 | } | |
4839 | EXPORT_SYMBOL_GPL(device_match_any); |