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