Merge tag 'phy-for-6.6' of git://git.kernel.org/pub/scm/linux/kernel/git/phy/linux-phy
[linux-block.git] / include / linux / device.h
CommitLineData
989d42e8 1// SPDX-License-Identifier: GPL-2.0
1da177e4
LT
2/*
3 * device.h - generic, centralized driver model
4 *
5 * Copyright (c) 2001-2003 Patrick Mochel <mochel@osdl.org>
b4028437
GKH
6 * Copyright (c) 2004-2009 Greg Kroah-Hartman <gregkh@suse.de>
7 * Copyright (c) 2008-2009 Novell Inc.
1da177e4 8 *
fe34c89d 9 * See Documentation/driver-api/driver-model/ for more information.
1da177e4
LT
10 */
11
12#ifndef _DEVICE_H_
13#define _DEVICE_H_
14
af628aae 15#include <linux/dev_printk.h>
1bc138c6 16#include <linux/energy_model.h>
1da177e4
LT
17#include <linux/ioport.h>
18#include <linux/kobject.h>
465c7a3a 19#include <linux/klist.h>
1da177e4 20#include <linux/list.h>
d2a3b914 21#include <linux/lockdep.h>
4a7fb636 22#include <linux/compiler.h>
1da177e4 23#include <linux/types.h>
de477254 24#include <linux/mutex.h>
1da177e4 25#include <linux/pm.h>
60063497 26#include <linux/atomic.h>
3c2670e6 27#include <linux/uidgid.h>
64c862a8 28#include <linux/gfp.h>
2509b561 29#include <linux/overflow.h>
5aee2bf2 30#include <linux/device/bus.h>
a8ae6085 31#include <linux/device/class.h>
4c002c97 32#include <linux/device/driver.h>
54da6a09 33#include <linux/cleanup.h>
c6dbaef2 34#include <asm/device.h>
1da177e4 35
1da177e4 36struct device;
fb069a5d 37struct device_private;
1da177e4 38struct device_driver;
e5dd1278 39struct driver_private;
de477254 40struct module;
1da177e4 41struct class;
6b6e39a6 42struct subsys_private;
d706c1b0 43struct device_node;
ce793486 44struct fwnode_handle;
ff21776d 45struct iommu_ops;
74416e1e 46struct iommu_group;
23c35f48 47struct dev_pin_info;
045a7042 48struct dev_iommu;
013bd8e5 49struct msi_device_data;
b8c5cec2 50
ca22e56d
KS
51/**
52 * struct subsys_interface - interfaces to device functions
2eda013f 53 * @name: name of the device function
5dd5f934 54 * @subsys: subsystem of the devices to attach to
2eda013f
RD
55 * @node: the list of functions registered at the subsystem
56 * @add_dev: device hookup to device function handler
57 * @remove_dev: device hookup to device function handler
ca22e56d
KS
58 *
59 * Simple interfaces attached to a subsystem. Multiple interfaces can
60 * attach to a subsystem and its devices. Unlike drivers, they do not
61 * exclusively claim or control devices. Interfaces usually represent
62 * a specific functionality of a subsystem/class of devices.
63 */
64struct subsys_interface {
65 const char *name;
66 struct bus_type *subsys;
67 struct list_head node;
68 int (*add_dev)(struct device *dev, struct subsys_interface *sif);
71db87ba 69 void (*remove_dev)(struct device *dev, struct subsys_interface *sif);
ca22e56d
KS
70};
71
72int subsys_interface_register(struct subsys_interface *sif);
73void subsys_interface_unregister(struct subsys_interface *sif);
74
75int subsys_system_register(struct bus_type *subsys,
76 const struct attribute_group **groups);
d73ce004
TH
77int subsys_virtual_register(struct bus_type *subsys,
78 const struct attribute_group **groups);
ca22e56d 79
414264f9
KS
80/*
81 * The type of device, "struct device" is embedded in. A class
82 * or bus can contain devices of different types
83 * like "partitions" and "disks", "mouse" and "event".
84 * This identifies the device type and carries type-specific
85 * information, equivalent to the kobj_type of a kobject.
86 * If "name" is specified, the uevent will contain it in
87 * the DEVTYPE variable.
88 */
f9f852df 89struct device_type {
414264f9 90 const char *name;
a4dbd674 91 const struct attribute_group **groups;
162736b0 92 int (*uevent)(const struct device *dev, struct kobj_uevent_env *env);
a9b12f8b 93 char *(*devnode)(const struct device *dev, umode_t *mode,
4e4098a3 94 kuid_t *uid, kgid_t *gid);
f9f852df 95 void (*release)(struct device *dev);
1eede070 96
8150f32b 97 const struct dev_pm_ops *pm;
f9f852df
KS
98};
99
cd00bc2c
JS
100/**
101 * struct device_attribute - Interface for exporting device attributes.
102 * @attr: sysfs attribute definition.
103 * @show: Show handler.
104 * @store: Store handler.
105 */
a7fd6706
KS
106struct device_attribute {
107 struct attribute attr;
108 ssize_t (*show)(struct device *dev, struct device_attribute *attr,
109 char *buf);
110 ssize_t (*store)(struct device *dev, struct device_attribute *attr,
111 const char *buf, size_t count);
112};
113
cd00bc2c
JS
114/**
115 * struct dev_ext_attribute - Exported device attribute with extra context.
116 * @attr: Exported device attribute.
117 * @var: Pointer to context.
118 */
ca22e56d
KS
119struct dev_ext_attribute {
120 struct device_attribute attr;
121 void *var;
122};
123
124ssize_t device_show_ulong(struct device *dev, struct device_attribute *attr,
125 char *buf);
126ssize_t device_store_ulong(struct device *dev, struct device_attribute *attr,
127 const char *buf, size_t count);
128ssize_t device_show_int(struct device *dev, struct device_attribute *attr,
129 char *buf);
130ssize_t device_store_int(struct device *dev, struct device_attribute *attr,
131 const char *buf, size_t count);
91872392
BP
132ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
133 char *buf);
134ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
135 const char *buf, size_t count);
a7fd6706 136
cd00bc2c
JS
137/**
138 * DEVICE_ATTR - Define a device attribute.
139 * @_name: Attribute name.
140 * @_mode: File mode.
141 * @_show: Show handler. Optional, but mandatory if attribute is readable.
142 * @_store: Store handler. Optional, but mandatory if attribute is writable.
143 *
144 * Convenience macro for defining a struct device_attribute.
145 *
146 * For example, ``DEVICE_ATTR(foo, 0644, foo_show, foo_store);`` expands to:
147 *
148 * .. code-block:: c
149 *
150 * struct device_attribute dev_attr_foo = {
151 * .attr = { .name = "foo", .mode = 0644 },
152 * .show = foo_show,
153 * .store = foo_store,
154 * };
155 */
d462943a 156#define DEVICE_ATTR(_name, _mode, _show, _store) \
ca22e56d 157 struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store)
cd00bc2c
JS
158
159/**
160 * DEVICE_ATTR_PREALLOC - Define a preallocated device attribute.
161 * @_name: Attribute name.
162 * @_mode: File mode.
163 * @_show: Show handler. Optional, but mandatory if attribute is readable.
164 * @_store: Store handler. Optional, but mandatory if attribute is writable.
165 *
166 * Like DEVICE_ATTR(), but ``SYSFS_PREALLOC`` is set on @_mode.
167 */
7fda9100
CL
168#define DEVICE_ATTR_PREALLOC(_name, _mode, _show, _store) \
169 struct device_attribute dev_attr_##_name = \
170 __ATTR_PREALLOC(_name, _mode, _show, _store)
cd00bc2c
JS
171
172/**
173 * DEVICE_ATTR_RW - Define a read-write device attribute.
174 * @_name: Attribute name.
175 *
176 * Like DEVICE_ATTR(), but @_mode is 0644, @_show is <_name>_show,
177 * and @_store is <_name>_store.
178 */
ced321bf
GKH
179#define DEVICE_ATTR_RW(_name) \
180 struct device_attribute dev_attr_##_name = __ATTR_RW(_name)
cd00bc2c
JS
181
182/**
183 * DEVICE_ATTR_ADMIN_RW - Define an admin-only read-write device attribute.
184 * @_name: Attribute name.
185 *
186 * Like DEVICE_ATTR_RW(), but @_mode is 0600.
187 */
3022c6a1
DW
188#define DEVICE_ATTR_ADMIN_RW(_name) \
189 struct device_attribute dev_attr_##_name = __ATTR_RW_MODE(_name, 0600)
cd00bc2c
JS
190
191/**
192 * DEVICE_ATTR_RO - Define a readable device attribute.
193 * @_name: Attribute name.
194 *
195 * Like DEVICE_ATTR(), but @_mode is 0444 and @_show is <_name>_show.
196 */
ced321bf
GKH
197#define DEVICE_ATTR_RO(_name) \
198 struct device_attribute dev_attr_##_name = __ATTR_RO(_name)
cd00bc2c
JS
199
200/**
201 * DEVICE_ATTR_ADMIN_RO - Define an admin-only readable device attribute.
202 * @_name: Attribute name.
203 *
204 * Like DEVICE_ATTR_RO(), but @_mode is 0400.
205 */
3022c6a1
DW
206#define DEVICE_ATTR_ADMIN_RO(_name) \
207 struct device_attribute dev_attr_##_name = __ATTR_RO_MODE(_name, 0400)
cd00bc2c
JS
208
209/**
210 * DEVICE_ATTR_WO - Define an admin-only writable device attribute.
211 * @_name: Attribute name.
212 *
213 * Like DEVICE_ATTR(), but @_mode is 0200 and @_store is <_name>_store.
214 */
1130c55c
GKH
215#define DEVICE_ATTR_WO(_name) \
216 struct device_attribute dev_attr_##_name = __ATTR_WO(_name)
cd00bc2c
JS
217
218/**
219 * DEVICE_ULONG_ATTR - Define a device attribute backed by an unsigned long.
220 * @_name: Attribute name.
221 * @_mode: File mode.
222 * @_var: Identifier of unsigned long.
223 *
224 * Like DEVICE_ATTR(), but @_show and @_store are automatically provided
225 * such that reads and writes to the attribute from userspace affect @_var.
226 */
ca22e56d
KS
227#define DEVICE_ULONG_ATTR(_name, _mode, _var) \
228 struct dev_ext_attribute dev_attr_##_name = \
229 { __ATTR(_name, _mode, device_show_ulong, device_store_ulong), &(_var) }
cd00bc2c
JS
230
231/**
232 * DEVICE_INT_ATTR - Define a device attribute backed by an int.
233 * @_name: Attribute name.
234 * @_mode: File mode.
235 * @_var: Identifier of int.
236 *
237 * Like DEVICE_ULONG_ATTR(), but @_var is an int.
238 */
ca22e56d
KS
239#define DEVICE_INT_ATTR(_name, _mode, _var) \
240 struct dev_ext_attribute dev_attr_##_name = \
94758185 241 { __ATTR(_name, _mode, device_show_int, device_store_int), &(_var) }
cd00bc2c
JS
242
243/**
244 * DEVICE_BOOL_ATTR - Define a device attribute backed by a bool.
245 * @_name: Attribute name.
246 * @_mode: File mode.
247 * @_var: Identifier of bool.
248 *
249 * Like DEVICE_ULONG_ATTR(), but @_var is a bool.
250 */
91872392
BP
251#define DEVICE_BOOL_ATTR(_name, _mode, _var) \
252 struct dev_ext_attribute dev_attr_##_name = \
253 { __ATTR(_name, _mode, device_show_bool, device_store_bool), &(_var) }
cd00bc2c 254
356c05d5
AS
255#define DEVICE_ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store) \
256 struct device_attribute dev_attr_##_name = \
257 __ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store)
a7fd6706 258
67dd0772
BG
259int device_create_file(struct device *device,
260 const struct device_attribute *entry);
261void device_remove_file(struct device *dev,
262 const struct device_attribute *attr);
263bool device_remove_file_self(struct device *dev,
264 const struct device_attribute *attr);
265int __must_check device_create_bin_file(struct device *dev,
66ecb92b 266 const struct bin_attribute *attr);
67dd0772
BG
267void device_remove_bin_file(struct device *dev,
268 const struct bin_attribute *attr);
9ac7849e
TH
269
270/* device resource management */
271typedef void (*dr_release_t)(struct device *dev, void *res);
272typedef int (*dr_match_t)(struct device *dev, void *res, void *match_data);
273
67dd0772
BG
274void *__devres_alloc_node(dr_release_t release, size_t size, gfp_t gfp,
275 int nid, const char *name) __malloc;
9ac7849e 276#define devres_alloc(release, size, gfp) \
7c683941
DW
277 __devres_alloc_node(release, size, gfp, NUMA_NO_NODE, #release)
278#define devres_alloc_node(release, size, gfp, nid) \
279 __devres_alloc_node(release, size, gfp, nid, #release)
7c683941 280
67dd0772
BG
281void devres_for_each_res(struct device *dev, dr_release_t release,
282 dr_match_t match, void *match_data,
283 void (*fn)(struct device *, void *, void *),
284 void *data);
285void devres_free(void *res);
286void devres_add(struct device *dev, void *res);
287void *devres_find(struct device *dev, dr_release_t release,
288 dr_match_t match, void *match_data);
289void *devres_get(struct device *dev, void *new_res,
290 dr_match_t match, void *match_data);
291void *devres_remove(struct device *dev, dr_release_t release,
292 dr_match_t match, void *match_data);
293int devres_destroy(struct device *dev, dr_release_t release,
294 dr_match_t match, void *match_data);
295int devres_release(struct device *dev, dr_release_t release,
296 dr_match_t match, void *match_data);
9ac7849e
TH
297
298/* devres group */
67dd0772
BG
299void * __must_check devres_open_group(struct device *dev, void *id, gfp_t gfp);
300void devres_close_group(struct device *dev, void *id);
301void devres_remove_group(struct device *dev, void *id);
302int devres_release_group(struct device *dev, void *id);
9ac7849e 303
64c862a8 304/* managed devm_k.alloc/kfree for device drivers */
74c8e6bf 305void *devm_kmalloc(struct device *dev, size_t size, gfp_t gfp) __alloc_size(2);
f8248572 306void *devm_krealloc(struct device *dev, void *ptr, size_t size,
74c8e6bf 307 gfp_t gfp) __must_check __realloc_size(3);
67dd0772
BG
308__printf(3, 0) char *devm_kvasprintf(struct device *dev, gfp_t gfp,
309 const char *fmt, va_list ap) __malloc;
310__printf(3, 4) char *devm_kasprintf(struct device *dev, gfp_t gfp,
311 const char *fmt, ...) __malloc;
64c862a8
JP
312static inline void *devm_kzalloc(struct device *dev, size_t size, gfp_t gfp)
313{
314 return devm_kmalloc(dev, size, gfp | __GFP_ZERO);
315}
316static inline void *devm_kmalloc_array(struct device *dev,
317 size_t n, size_t size, gfp_t flags)
318{
2509b561
KC
319 size_t bytes;
320
321 if (unlikely(check_mul_overflow(n, size, &bytes)))
64c862a8 322 return NULL;
2509b561
KC
323
324 return devm_kmalloc(dev, bytes, flags);
64c862a8
JP
325}
326static inline void *devm_kcalloc(struct device *dev,
327 size_t n, size_t size, gfp_t flags)
328{
329 return devm_kmalloc_array(dev, n, size, flags | __GFP_ZERO);
330}
d388f06a
JC
331static inline __realloc_size(3, 4) void * __must_check
332devm_krealloc_array(struct device *dev, void *p, size_t new_n, size_t new_size, gfp_t flags)
333{
334 size_t bytes;
335
336 if (unlikely(check_mul_overflow(new_n, new_size, &bytes)))
337 return NULL;
338
339 return devm_krealloc(dev, p, bytes, flags);
340}
341
67dd0772
BG
342void devm_kfree(struct device *dev, const void *p);
343char *devm_kstrdup(struct device *dev, const char *s, gfp_t gfp) __malloc;
344const char *devm_kstrdup_const(struct device *dev, const char *s, gfp_t gfp);
74c8e6bf
KC
345void *devm_kmemdup(struct device *dev, const void *src, size_t len, gfp_t gfp)
346 __realloc_size(3);
9ac7849e 347
67dd0772
BG
348unsigned long devm_get_free_pages(struct device *dev,
349 gfp_t gfp_mask, unsigned int order);
350void devm_free_pages(struct device *dev, unsigned long addr);
9ac7849e 351
da7c07b1 352#ifdef CONFIG_HAS_IOMEM
eef778c9
AB
353void __iomem *devm_ioremap_resource(struct device *dev,
354 const struct resource *res);
b873af62
BG
355void __iomem *devm_ioremap_resource_wc(struct device *dev,
356 const struct resource *res);
72f8c0bf 357
d5e83827
BH
358void __iomem *devm_of_iomap(struct device *dev,
359 struct device_node *node, int index,
360 resource_size_t *size);
da7c07b1
MB
361#else
362
363static inline
364void __iomem *devm_ioremap_resource(struct device *dev,
365 const struct resource *res)
366{
367 return ERR_PTR(-EINVAL);
368}
369
370static inline
371void __iomem *devm_ioremap_resource_wc(struct device *dev,
372 const struct resource *res)
373{
374 return ERR_PTR(-EINVAL);
375}
376
377static inline
378void __iomem *devm_of_iomap(struct device *dev,
379 struct device_node *node, int index,
380 resource_size_t *size)
381{
382 return ERR_PTR(-EINVAL);
383}
384
385#endif
d5e83827 386
d6b0c580 387/* allows to add/remove a custom action to devres stack */
d6b0c580 388void devm_remove_action(struct device *dev, void (*action)(void *), void *data);
2374b682 389void devm_release_action(struct device *dev, void (*action)(void *), void *data);
d6b0c580 390
0433686c
AS
391int __devm_add_action(struct device *dev, void (*action)(void *), void *data, const char *name);
392#define devm_add_action(release, action, data) \
393 __devm_add_action(release, action, data, #action)
394
410e7088
AS
395static inline int __devm_add_action_or_reset(struct device *dev, void (*action)(void *),
396 void *data, const char *name)
a3499e9b
SM
397{
398 int ret;
399
410e7088 400 ret = __devm_add_action(dev, action, data, name);
a3499e9b
SM
401 if (ret)
402 action(data);
403
404 return ret;
405}
410e7088
AS
406#define devm_add_action_or_reset(release, action, data) \
407 __devm_add_action_or_reset(release, action, data, #action)
a3499e9b 408
ff86aae3
MB
409/**
410 * devm_alloc_percpu - Resource-managed alloc_percpu
411 * @dev: Device to allocate per-cpu memory for
412 * @type: Type to allocate per-cpu memory for
413 *
414 * Managed alloc_percpu. Per-cpu memory allocated with this function is
415 * automatically freed on driver detach.
416 *
417 * RETURNS:
418 * Pointer to allocated memory on success, NULL on failure.
419 */
420#define devm_alloc_percpu(dev, type) \
421 ((typeof(type) __percpu *)__devm_alloc_percpu((dev), sizeof(type), \
422 __alignof__(type)))
423
424void __percpu *__devm_alloc_percpu(struct device *dev, size_t size,
425 size_t align);
426void devm_free_percpu(struct device *dev, void __percpu *pdata);
427
6b7b6510
FT
428struct device_dma_parameters {
429 /*
430 * a low level driver may set these to teach IOMMU code about
431 * sg limitations.
432 */
433 unsigned int max_segment_size;
36950f2d 434 unsigned int min_align_mask;
6b7b6510
FT
435 unsigned long segment_boundary_mask;
436};
437
9ed98953
RW
438/**
439 * enum device_link_state - Device link states.
440 * @DL_STATE_NONE: The presence of the drivers is not being tracked.
441 * @DL_STATE_DORMANT: None of the supplier/consumer drivers is present.
442 * @DL_STATE_AVAILABLE: The supplier driver is present, but the consumer is not.
443 * @DL_STATE_CONSUMER_PROBE: The consumer is probing (supplier driver present).
444 * @DL_STATE_ACTIVE: Both the supplier and consumer drivers are present.
445 * @DL_STATE_SUPPLIER_UNBIND: The supplier driver is unbinding.
446 */
447enum device_link_state {
448 DL_STATE_NONE = -1,
449 DL_STATE_DORMANT = 0,
450 DL_STATE_AVAILABLE,
451 DL_STATE_CONSUMER_PROBE,
452 DL_STATE_ACTIVE,
453 DL_STATE_SUPPLIER_UNBIND,
454};
455
456/*
457 * Device link flags.
458 *
515db266 459 * STATELESS: The core will not remove this link automatically.
e88728f4 460 * AUTOREMOVE_CONSUMER: Remove the link automatically on consumer driver unbind.
21d5c57b
RW
461 * PM_RUNTIME: If set, the runtime PM framework will use this link.
462 * RPM_ACTIVE: Run pm_runtime_get_sync() on the supplier during link creation.
1689cac5 463 * AUTOREMOVE_SUPPLIER: Remove the link automatically on supplier driver unbind.
e7dd4010 464 * AUTOPROBE_CONSUMER: Probe consumer driver automatically after supplier binds.
515db266 465 * MANAGED: The core tracks presence of supplier/consumer drivers (internal).
05ef983e 466 * SYNC_STATE_ONLY: Link only affects sync_state() behavior.
4b9bbb29 467 * INFERRED: Inferred from data (eg: firmware) and not from driver actions.
9ed98953 468 */
e88728f4
VG
469#define DL_FLAG_STATELESS BIT(0)
470#define DL_FLAG_AUTOREMOVE_CONSUMER BIT(1)
471#define DL_FLAG_PM_RUNTIME BIT(2)
472#define DL_FLAG_RPM_ACTIVE BIT(3)
1689cac5 473#define DL_FLAG_AUTOREMOVE_SUPPLIER BIT(4)
e7dd4010 474#define DL_FLAG_AUTOPROBE_CONSUMER BIT(5)
515db266 475#define DL_FLAG_MANAGED BIT(6)
05ef983e 476#define DL_FLAG_SYNC_STATE_ONLY BIT(7)
4b9bbb29 477#define DL_FLAG_INFERRED BIT(8)
67cad5c6 478#define DL_FLAG_CYCLE BIT(9)
9ed98953 479
9ed98953
RW
480/**
481 * enum dl_dev_state - Device driver presence tracking information.
482 * @DL_DEV_NO_DRIVER: There is no driver attached to the device.
483 * @DL_DEV_PROBING: A driver is probing.
484 * @DL_DEV_DRIVER_BOUND: The driver has been bound to the device.
485 * @DL_DEV_UNBINDING: The driver is unbinding from the device.
486 */
487enum dl_dev_state {
488 DL_DEV_NO_DRIVER = 0,
489 DL_DEV_PROBING,
490 DL_DEV_DRIVER_BOUND,
491 DL_DEV_UNBINDING,
492};
493
70f400d4
RJ
494/**
495 * enum device_removable - Whether the device is removable. The criteria for a
496 * device to be classified as removable is determined by its subsystem or bus.
497 * @DEVICE_REMOVABLE_NOT_SUPPORTED: This attribute is not supported for this
498 * device (default).
499 * @DEVICE_REMOVABLE_UNKNOWN: Device location is Unknown.
500 * @DEVICE_FIXED: Device is not removable by the user.
501 * @DEVICE_REMOVABLE: Device is removable by the user.
502 */
503enum device_removable {
504 DEVICE_REMOVABLE_NOT_SUPPORTED = 0, /* must be 0 */
505 DEVICE_REMOVABLE_UNKNOWN,
506 DEVICE_FIXED,
507 DEVICE_REMOVABLE,
508};
509
9ed98953
RW
510/**
511 * struct dev_links_info - Device data related to device links.
512 * @suppliers: List of links to supplier devices.
513 * @consumers: List of links to consumer devices.
3b052a3e 514 * @defer_sync: Hook to global list of devices that have deferred sync_state.
9ed98953
RW
515 * @status: Driver status information.
516 */
517struct dev_links_info {
518 struct list_head suppliers;
519 struct list_head consumers;
3b052a3e 520 struct list_head defer_sync;
9ed98953
RW
521 enum dl_dev_state status;
522};
523
34fff628
TG
524/**
525 * struct dev_msi_info - Device data related to MSI
526 * @domain: The MSI interrupt domain associated to the device
013bd8e5 527 * @data: Pointer to MSI device data
34fff628
TG
528 */
529struct dev_msi_info {
013bd8e5 530#ifdef CONFIG_GENERIC_MSI_IRQ
13e7accb 531 struct irq_domain *domain;
013bd8e5
TG
532 struct msi_device_data *data;
533#endif
34fff628
TG
534};
535
6423d295
WC
536/**
537 * enum device_physical_location_panel - Describes which panel surface of the
538 * system's housing the device connection point resides on.
539 * @DEVICE_PANEL_TOP: Device connection point is on the top panel.
540 * @DEVICE_PANEL_BOTTOM: Device connection point is on the bottom panel.
541 * @DEVICE_PANEL_LEFT: Device connection point is on the left panel.
542 * @DEVICE_PANEL_RIGHT: Device connection point is on the right panel.
543 * @DEVICE_PANEL_FRONT: Device connection point is on the front panel.
544 * @DEVICE_PANEL_BACK: Device connection point is on the back panel.
545 * @DEVICE_PANEL_UNKNOWN: The panel with device connection point is unknown.
546 */
547enum device_physical_location_panel {
548 DEVICE_PANEL_TOP,
549 DEVICE_PANEL_BOTTOM,
550 DEVICE_PANEL_LEFT,
551 DEVICE_PANEL_RIGHT,
552 DEVICE_PANEL_FRONT,
553 DEVICE_PANEL_BACK,
554 DEVICE_PANEL_UNKNOWN,
555};
556
557/**
558 * enum device_physical_location_vertical_position - Describes vertical
559 * position of the device connection point on the panel surface.
560 * @DEVICE_VERT_POS_UPPER: Device connection point is at upper part of panel.
561 * @DEVICE_VERT_POS_CENTER: Device connection point is at center part of panel.
562 * @DEVICE_VERT_POS_LOWER: Device connection point is at lower part of panel.
563 */
564enum device_physical_location_vertical_position {
565 DEVICE_VERT_POS_UPPER,
566 DEVICE_VERT_POS_CENTER,
567 DEVICE_VERT_POS_LOWER,
568};
569
570/**
571 * enum device_physical_location_horizontal_position - Describes horizontal
572 * position of the device connection point on the panel surface.
573 * @DEVICE_HORI_POS_LEFT: Device connection point is at left part of panel.
574 * @DEVICE_HORI_POS_CENTER: Device connection point is at center part of panel.
575 * @DEVICE_HORI_POS_RIGHT: Device connection point is at right part of panel.
576 */
577enum device_physical_location_horizontal_position {
578 DEVICE_HORI_POS_LEFT,
579 DEVICE_HORI_POS_CENTER,
580 DEVICE_HORI_POS_RIGHT,
581};
582
583/**
584 * struct device_physical_location - Device data related to physical location
585 * of the device connection point.
586 * @panel: Panel surface of the system's housing that the device connection
587 * point resides on.
588 * @vertical_position: Vertical position of the device connection point within
589 * the panel.
590 * @horizontal_position: Horizontal position of the device connection point
591 * within the panel.
592 * @dock: Set if the device connection point resides in a docking station or
593 * port replicator.
594 * @lid: Set if this device connection point resides on the lid of laptop
595 * system.
596 */
597struct device_physical_location {
598 enum device_physical_location_panel panel;
599 enum device_physical_location_vertical_position vertical_position;
600 enum device_physical_location_horizontal_position horizontal_position;
601 bool dock;
602 bool lid;
603};
604
880ffb5c
WG
605/**
606 * struct device - The basic device structure
607 * @parent: The device's "parent" device, the device to which it is attached.
608 * In most cases, a parent device is some sort of bus or host
609 * controller. If parent is NULL, the device, is a top-level device,
610 * which is not usually what you want.
611 * @p: Holds the private data of the driver core portions of the device.
612 * See the comment of the struct device_private for detail.
613 * @kobj: A top-level, abstract class from which other classes are derived.
614 * @init_name: Initial name of the device.
615 * @type: The type of device.
616 * This identifies the device type and carries type-specific
617 * information.
618 * @mutex: Mutex to synchronize calls to its driver.
619 * @bus: Type of bus device is on.
620 * @driver: Which driver has allocated this
621 * @platform_data: Platform data specific to the device.
622 * Example: For devices on custom boards, as typical of embedded
623 * and SOC based hardware, Linux often uses platform_data to point
624 * to board-specific structures describing devices and how they
625 * are wired. That can include what ports are available, chip
626 * variants, which GPIO pins act in what additional roles, and so
627 * on. This shrinks the "Board Support Packages" (BSPs) and
628 * minimizes board-specific #ifdefs in drivers.
1bb6c08a 629 * @driver_data: Private pointer for driver specific info.
64df1148 630 * @links: Links to suppliers and consumers of this device.
880ffb5c 631 * @power: For device power management.
74378c5c 632 * See Documentation/driver-api/pm/devices.rst for details.
564b905a 633 * @pm_domain: Provide callbacks that are executed during system suspend,
880ffb5c
WG
634 * hibernation, system resume and during runtime PM transitions
635 * along with subsystem-level and driver-level callbacks.
95035eac 636 * @em_pd: device's energy model performance domain
ab78029e 637 * @pins: For device pin management.
4b0c9948 638 * See Documentation/driver-api/pin-control.rst for details.
34fff628 639 * @msi: MSI related data
880ffb5c 640 * @numa_node: NUMA node this device is close to.
6a7a8176 641 * @dma_ops: DMA mapping operations for this device.
880ffb5c
WG
642 * @dma_mask: Dma mask (if dma'ble device).
643 * @coherent_dma_mask: Like dma_mask, but for alloc_coherent mapping as not all
644 * hardware supports 64-bit addresses for consistent allocations
645 * such descriptors.
a7ba70f1
NSJ
646 * @bus_dma_limit: Limit of an upstream bridge or bus which imposes a smaller
647 * DMA limit than the device itself supports.
e0d07278 648 * @dma_range_map: map for DMA memory ranges relative to that of RAM
880ffb5c
WG
649 * @dma_parms: A low level driver may set these to teach IOMMU code about
650 * segment limitations.
651 * @dma_pools: Dma pools (if dma'ble device).
652 * @dma_mem: Internal for coherent mem override.
bfd63cd2 653 * @cma_area: Contiguous memory area for dma allocations
158dbe9c 654 * @dma_io_tlb_mem: Software IO TLB allocator. Not for driver use.
79636caa
PT
655 * @dma_io_tlb_pools: List of transient swiotlb memory pools.
656 * @dma_io_tlb_lock: Protects changes to the list of active pools.
1395706a 657 * @dma_uses_io_tlb: %true if device has used the software IO TLB.
880ffb5c
WG
658 * @archdata: For arch-specific additions.
659 * @of_node: Associated device tree node.
ce793486 660 * @fwnode: Associated device node supplied by platform firmware.
880ffb5c 661 * @devt: For creating the sysfs "dev".
2eda013f 662 * @id: device instance
880ffb5c
WG
663 * @devres_lock: Spinlock to protect the resource of the device.
664 * @devres_head: The resources list of the device.
665 * @knode_class: The node used to add the device to the class list.
666 * @class: The class of the device.
667 * @groups: Optional attribute groups.
668 * @release: Callback to free the device after all references have
669 * gone away. This should be set by the allocator of the
670 * device (i.e. the bus driver that discovered the device).
bfd63cd2 671 * @iommu_group: IOMMU group the device belongs to.
045a7042 672 * @iommu: Per device generic IOMMU runtime data
6423d295
WC
673 * @physical_location: Describes physical location of the device connection
674 * point in the system housing.
70f400d4
RJ
675 * @removable: Whether the device can be removed from the system. This
676 * should be set by the subsystem / bus driver that discovered
677 * the device.
880ffb5c 678 *
4f3549d7
RW
679 * @offline_disabled: If set, the device is permanently online.
680 * @offline: Set after successful invocation of bus type's .offline().
4e75e1d7
JH
681 * @of_node_reused: Set if the device-tree node is shared with an ancestor
682 * device.
fc5a251d
SK
683 * @state_synced: The hardware state of this device has been synced to match
684 * the software state of this device by calling the driver/bus
685 * sync_state() callback.
f2db85b6
SK
686 * @can_match: The device has matched with a driver at least once or it is in
687 * a bus (like AMBA) which can't check for matching drivers until
688 * other devices probe successfully.
f3ecc0ff
CH
689 * @dma_coherent: this particular device is dma coherent, even if the
690 * architecture supports non-coherent devices.
d35834c6
CH
691 * @dma_ops_bypass: If set to %true then the dma_ops are bypassed for the
692 * streaming DMA operations (->map_* / ->unmap_* / ->sync_*),
693 * and optionall (if the coherent mask is large enough) also
694 * for dma allocations. This flag is managed by the dma ops
695 * instance from ->dma_supported.
880ffb5c
WG
696 *
697 * At the lowest level, every device in a Linux system is represented by an
698 * instance of struct device. The device structure contains the information
699 * that the device model core needs to model the system. Most subsystems,
700 * however, track additional information about the devices they host. As a
701 * result, it is rare for devices to be represented by bare device structures;
702 * instead, that structure, like kobject structures, is usually embedded within
703 * a higher-level representation of the device.
704 */
1da177e4 705struct device {
159ef31e 706 struct kobject kobj;
49a4ec18 707 struct device *parent;
1da177e4 708
fb069a5d
GKH
709 struct device_private *p;
710
c906a48a 711 const char *init_name; /* initial name of the device */
aed65af1 712 const struct device_type *type;
1da177e4 713
d492cc25 714 const struct bus_type *bus; /* type of bus device is on */
1da177e4
LT
715 struct device_driver *driver; /* which driver has allocated this
716 device */
e67c8562
GKH
717 void *platform_data; /* Platform specific data, device
718 core doesn't touch it */
1bb6c08a 719 void *driver_data; /* Driver data, set and get with
2c6f4fc8 720 dev_set_drvdata/dev_get_drvdata */
159ef31e
GKH
721 struct mutex mutex; /* mutex to synchronize calls to
722 * its driver.
723 */
724
9ed98953 725 struct dev_links_info links;
1da177e4 726 struct dev_pm_info power;
564b905a 727 struct dev_pm_domain *pm_domain;
1da177e4 728
1bc138c6
LL
729#ifdef CONFIG_ENERGY_MODEL
730 struct em_perf_domain *em_pd;
731#endif
732
ab78029e
LW
733#ifdef CONFIG_PINCTRL
734 struct dev_pin_info *pins;
735#endif
34fff628 736 struct dev_msi_info msi;
2f9237d4 737#ifdef CONFIG_DMA_OPS
5657933d 738 const struct dma_map_ops *dma_ops;
2f9237d4 739#endif
1da177e4
LT
740 u64 *dma_mask; /* dma mask (if dma'able device) */
741 u64 coherent_dma_mask;/* Like dma_mask, but for
742 alloc_coherent mappings as
743 not all hardware supports
744 64 bit addresses for consistent
745 allocations such descriptors. */
a7ba70f1 746 u64 bus_dma_limit; /* upstream dma constraint */
e0d07278 747 const struct bus_dma_region *dma_range_map;
1da177e4 748
6b7b6510
FT
749 struct device_dma_parameters *dma_parms;
750
1da177e4
LT
751 struct list_head dma_pools; /* dma pools (if dma'ble) */
752
ff4c25f2 753#ifdef CONFIG_DMA_DECLARE_COHERENT
1da177e4
LT
754 struct dma_coherent_mem *dma_mem; /* internal for coherent mem
755 override */
2b281296 756#endif
a2547380 757#ifdef CONFIG_DMA_CMA
c64be2bb
MS
758 struct cma *cma_area; /* contiguous memory area for dma
759 allocations */
69031f50
CC
760#endif
761#ifdef CONFIG_SWIOTLB
762 struct io_tlb_mem *dma_io_tlb_mem;
79636caa
PT
763#endif
764#ifdef CONFIG_SWIOTLB_DYNAMIC
765 struct list_head dma_io_tlb_pools;
766 spinlock_t dma_io_tlb_lock;
1395706a 767 bool dma_uses_io_tlb;
c64be2bb 768#endif
c6dbaef2
BH
769 /* arch specific additions */
770 struct dev_archdata archdata;
c9e358df
GL
771
772 struct device_node *of_node; /* associated device tree node */
ce793486 773 struct fwnode_handle *fwnode; /* firmware device node */
1da177e4 774
159ef31e
GKH
775#ifdef CONFIG_NUMA
776 int numa_node; /* NUMA node this device is close to */
777#endif
929d2fa5 778 dev_t devt; /* dev_t, creates the sysfs "dev" */
ca22e56d 779 u32 id; /* device instance */
929d2fa5 780
9ac7849e
TH
781 spinlock_t devres_lock;
782 struct list_head devres_head;
783
9fa120fb 784 const struct class *class;
a4dbd674 785 const struct attribute_group **groups; /* optional groups */
23681e47 786
d462943a 787 void (*release)(struct device *dev);
74416e1e 788 struct iommu_group *iommu_group;
045a7042 789 struct dev_iommu *iommu;
4f3549d7 790
6423d295
WC
791 struct device_physical_location *physical_location;
792
70f400d4
RJ
793 enum device_removable removable;
794
4f3549d7
RW
795 bool offline_disabled:1;
796 bool offline:1;
4e75e1d7 797 bool of_node_reused:1;
fc5a251d 798 bool state_synced:1;
f2db85b6 799 bool can_match:1;
f3ecc0ff
CH
800#if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE) || \
801 defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU) || \
802 defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL)
803 bool dma_coherent:1;
804#endif
d35834c6
CH
805#ifdef CONFIG_DMA_OPS_BYPASS
806 bool dma_ops_bypass : 1;
807#endif
1da177e4
LT
808};
809
287905e6
SK
810/**
811 * struct device_link - Device link representation.
812 * @supplier: The device on the supplier end of the link.
813 * @s_node: Hook to the supplier device's list of links to consumers.
814 * @consumer: The device on the consumer end of the link.
815 * @c_node: Hook to the consumer device's list of links to suppliers.
816 * @link_dev: device used to expose link details in sysfs
817 * @status: The state of the link (with respect to the presence of drivers).
818 * @flags: Link flags.
819 * @rpm_active: Whether or not the consumer device is runtime-PM-active.
820 * @kref: Count repeated addition of the same link.
80dd33cf 821 * @rm_work: Work structure used for removing the link.
287905e6
SK
822 * @supplier_preactivated: Supplier has been made active before consumer probe.
823 */
824struct device_link {
825 struct device *supplier;
826 struct list_head s_node;
827 struct device *consumer;
828 struct list_head c_node;
829 struct device link_dev;
830 enum device_link_state status;
831 u32 flags;
832 refcount_t rpm_active;
833 struct kref kref;
80dd33cf 834 struct work_struct rm_work;
287905e6
SK
835 bool supplier_preactivated; /* Owned by consumer probe. */
836};
837
6149f83b 838#define kobj_to_dev(__kobj) container_of_const(__kobj, struct device, kobj)
a4232963 839
dbba197e
JR
840/**
841 * device_iommu_mapped - Returns true when the device DMA is translated
842 * by an IOMMU
843 * @dev: Device to perform the check on
844 */
845static inline bool device_iommu_mapped(struct device *dev)
846{
847 return (dev->iommu_group != NULL);
848}
849
9a3df1f7
AS
850/* Get the wakeup routines, which depend on struct device */
851#include <linux/pm_wakeup.h>
852
cd00bc2c
JS
853/**
854 * dev_name - Return a device's name.
855 * @dev: Device with name to get.
856 * Return: The kobject name of the device, or its initial name if unavailable.
857 */
bf9ca69f 858static inline const char *dev_name(const struct device *dev)
06916639 859{
a636ee7f
PM
860 /* Use the init name until the kobject becomes available */
861 if (dev->init_name)
862 return dev->init_name;
863
1fa5ae85 864 return kobject_name(&dev->kobj);
06916639
KS
865}
866
e020ff61
SK
867/**
868 * dev_bus_name - Return a device's bus/class name, if at all possible
869 * @dev: struct device to get the bus/class name of
870 *
871 * Will return the name of the bus/class the device is attached to. If it is
872 * not attached to a bus/class, an empty string will be returned.
873 */
874static inline const char *dev_bus_name(const struct device *dev)
875{
876 return dev->bus ? dev->bus->name : (dev->class ? dev->class->name : "");
877}
878
67dd0772 879__printf(2, 3) int dev_set_name(struct device *dev, const char *name, ...);
413c239f 880
87348136
CH
881#ifdef CONFIG_NUMA
882static inline int dev_to_node(struct device *dev)
883{
884 return dev->numa_node;
885}
886static inline void set_dev_node(struct device *dev, int node)
887{
888 dev->numa_node = node;
889}
890#else
891static inline int dev_to_node(struct device *dev)
892{
98fa15f3 893 return NUMA_NO_NODE;
87348136
CH
894}
895static inline void set_dev_node(struct device *dev, int node)
896{
897}
898#endif
899
f1421db8
MZ
900static inline struct irq_domain *dev_get_msi_domain(const struct device *dev)
901{
13e7accb 902#ifdef CONFIG_GENERIC_MSI_IRQ
34fff628 903 return dev->msi.domain;
f1421db8
MZ
904#else
905 return NULL;
906#endif
907}
908
909static inline void dev_set_msi_domain(struct device *dev, struct irq_domain *d)
910{
13e7accb 911#ifdef CONFIG_GENERIC_MSI_IRQ
34fff628 912 dev->msi.domain = d;
f1421db8
MZ
913#endif
914}
915
a996d010
JD
916static inline void *dev_get_drvdata(const struct device *dev)
917{
918 return dev->driver_data;
919}
920
921static inline void dev_set_drvdata(struct device *dev, void *data)
922{
923 dev->driver_data = data;
924}
925
5c095a0e
RW
926static inline struct pm_subsys_data *dev_to_psd(struct device *dev)
927{
928 return dev ? dev->power.subsys_data : NULL;
929}
930
f67f129e
ML
931static inline unsigned int dev_get_uevent_suppress(const struct device *dev)
932{
933 return dev->kobj.uevent_suppress;
934}
935
936static inline void dev_set_uevent_suppress(struct device *dev, int val)
937{
938 dev->kobj.uevent_suppress = val;
939}
940
d305ef5d
DR
941static inline int device_is_registered(struct device *dev)
942{
3f62e570 943 return dev->kobj.state_in_sysfs;
d305ef5d
DR
944}
945
5af84b82
RW
946static inline void device_enable_async_suspend(struct device *dev)
947{
f76b168b 948 if (!dev->power.is_prepared)
5af84b82
RW
949 dev->power.async_suspend = true;
950}
951
5a2eb858
RW
952static inline void device_disable_async_suspend(struct device *dev)
953{
f76b168b 954 if (!dev->power.is_prepared)
5a2eb858
RW
955 dev->power.async_suspend = false;
956}
957
958static inline bool device_async_suspend_enabled(struct device *dev)
959{
960 return !!dev->power.async_suspend;
961}
962
85945c28
SH
963static inline bool device_pm_not_required(struct device *dev)
964{
965 return dev->power.no_pm;
966}
967
968static inline void device_set_pm_not_required(struct device *dev)
969{
970 dev->power.no_pm = true;
971}
972
feb70af0
RW
973static inline void dev_pm_syscore_device(struct device *dev, bool val)
974{
975#ifdef CONFIG_PM_SLEEP
976 dev->power.syscore = val;
977#endif
978}
979
08810a41
RW
980static inline void dev_pm_set_driver_flags(struct device *dev, u32 flags)
981{
982 dev->power.driver_flags = flags;
983}
984
985static inline bool dev_pm_test_driver_flags(struct device *dev, u32 flags)
986{
987 return !!(dev->power.driver_flags & flags);
988}
989
8e9394ce
GKH
990static inline void device_lock(struct device *dev)
991{
3142788b 992 mutex_lock(&dev->mutex);
8e9394ce
GKH
993}
994
7dd9cba5
ON
995static inline int device_lock_interruptible(struct device *dev)
996{
997 return mutex_lock_interruptible(&dev->mutex);
998}
999
8e9394ce
GKH
1000static inline int device_trylock(struct device *dev)
1001{
3142788b 1002 return mutex_trylock(&dev->mutex);
8e9394ce
GKH
1003}
1004
1005static inline void device_unlock(struct device *dev)
1006{
3142788b 1007 mutex_unlock(&dev->mutex);
8e9394ce
GKH
1008}
1009
ac801022
KRW
1010static inline void device_lock_assert(struct device *dev)
1011{
1012 lockdep_assert_held(&dev->mutex);
1013}
1014
e8a51e1b
BH
1015static inline struct device_node *dev_of_node(struct device *dev)
1016{
1b833924 1017 if (!IS_ENABLED(CONFIG_OF) || !dev)
e8a51e1b
BH
1018 return NULL;
1019 return dev->of_node;
1020}
1021
ac338acf
SK
1022static inline bool dev_has_sync_state(struct device *dev)
1023{
1024 if (!dev)
1025 return false;
1026 if (dev->driver && dev->driver->sync_state)
1027 return true;
1028 if (dev->bus && dev->bus->sync_state)
1029 return true;
1030 return false;
1031}
1032
70f400d4
RJ
1033static inline void dev_set_removable(struct device *dev,
1034 enum device_removable removable)
1035{
1036 dev->removable = removable;
1037}
1038
1039static inline bool dev_is_removable(struct device *dev)
1040{
1041 return dev->removable == DEVICE_REMOVABLE;
1042}
1043
1044static inline bool dev_removable_is_valid(struct device *dev)
1045{
1046 return dev->removable != DEVICE_REMOVABLE_NOT_SUPPORTED;
1047}
1048
1da177e4
LT
1049/*
1050 * High level routines for use by the bus drivers
1051 */
67dd0772
BG
1052int __must_check device_register(struct device *dev);
1053void device_unregister(struct device *dev);
1054void device_initialize(struct device *dev);
1055int __must_check device_add(struct device *dev);
1056void device_del(struct device *dev);
54da6a09
PZ
1057
1058DEFINE_FREE(device_del, struct device *, if (_T) device_del(_T))
1059
67dd0772
BG
1060int device_for_each_child(struct device *dev, void *data,
1061 int (*fn)(struct device *dev, void *data));
1062int device_for_each_child_reverse(struct device *dev, void *data,
1063 int (*fn)(struct device *dev, void *data));
1064struct device *device_find_child(struct device *dev, void *data,
1065 int (*match)(struct device *dev, void *data));
1066struct device *device_find_child_by_name(struct device *parent,
1067 const char *name);
82b070be
AS
1068struct device *device_find_any_child(struct device *parent);
1069
67dd0772
BG
1070int device_rename(struct device *dev, const char *new_name);
1071int device_move(struct device *dev, struct device *new_parent,
1072 enum dpm_order dpm_order);
1073int device_change_owner(struct device *dev, kuid_t kuid, kgid_t kgid);
44197797 1074int device_is_dependent(struct device *dev, void *target);
1da177e4 1075
4f3549d7
RW
1076static inline bool device_supports_offline(struct device *dev)
1077{
1078 return dev->bus && dev->bus->offline && dev->bus->online;
1079}
1080
d864b8ea
DW
1081#define __device_lock_set_class(dev, name, key) \
1082do { \
1083 struct device *__d2 __maybe_unused = dev; \
1084 lock_set_class(&__d2->mutex.dep_map, name, key, 0, _THIS_IP_); \
1085} while (0)
1086
1087/**
1088 * device_lock_set_class - Specify a temporary lock class while a device
1089 * is attached to a driver
1090 * @dev: device to modify
1091 * @key: lock class key data
1092 *
1093 * This must be called with the device_lock() already held, for example
1094 * from driver ->probe(). Take care to only override the default
1095 * lockdep_no_validate class.
1096 */
1097#ifdef CONFIG_LOCKDEP
1098#define device_lock_set_class(dev, key) \
1099do { \
1100 struct device *__d = dev; \
1101 dev_WARN_ONCE(__d, !lockdep_match_class(&__d->mutex, \
1102 &__lockdep_no_validate__), \
1103 "overriding existing custom lock class\n"); \
1104 __device_lock_set_class(__d, #key, key); \
1105} while (0)
1106#else
1107#define device_lock_set_class(dev, key) __device_lock_set_class(dev, #key, key)
1108#endif
1109
1110/**
1111 * device_lock_reset_class - Return a device to the default lockdep novalidate state
1112 * @dev: device to modify
1113 *
1114 * This must be called with the device_lock() already held, for example
1115 * from driver ->remove().
1116 */
1117#define device_lock_reset_class(dev) \
1118do { \
1119 struct device *__d __maybe_unused = dev; \
1120 lock_set_novalidate_class(&__d->mutex.dep_map, "&dev->mutex", \
1121 _THIS_IP_); \
1122} while (0)
1123
67dd0772
BG
1124void lock_device_hotplug(void);
1125void unlock_device_hotplug(void);
1126int lock_device_hotplug_sysfs(void);
1127int device_offline(struct device *dev);
1128int device_online(struct device *dev);
1129void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode);
1130void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode);
4e75e1d7 1131void device_set_of_node_from_dev(struct device *dev, const struct device *dev2);
43e76d46 1132void device_set_node(struct device *dev, struct fwnode_handle *fwnode);
97badf87 1133
9af15c38
PS
1134static inline int dev_num_vf(struct device *dev)
1135{
1136 if (dev->bus && dev->bus->num_vf)
1137 return dev->bus->num_vf(dev);
1138 return 0;
1139}
1140
0aa0dc41
MM
1141/*
1142 * Root device objects for grouping under /sys/devices
1143 */
67dd0772 1144struct device *__root_device_register(const char *name, struct module *owner);
eb5589a8 1145
33ac1257 1146/* This is a macro to avoid include problems with THIS_MODULE */
eb5589a8
PG
1147#define root_device_register(name) \
1148 __root_device_register(name, THIS_MODULE)
1149
67dd0772 1150void root_device_unregister(struct device *root);
0aa0dc41 1151
a5b8b1ad
MB
1152static inline void *dev_get_platdata(const struct device *dev)
1153{
1154 return dev->platform_data;
1155}
1156
1da177e4
LT
1157/*
1158 * Manual binding of a device to driver. See drivers/base/bus.c
1159 * for information on use.
1160 */
0d9f837c
JG
1161int __must_check device_driver_attach(struct device_driver *drv,
1162 struct device *dev);
67dd0772
BG
1163int __must_check device_bind_driver(struct device *dev);
1164void device_release_driver(struct device *dev);
1165int __must_check device_attach(struct device *dev);
1166int __must_check driver_attach(struct device_driver *drv);
1167void device_initial_probe(struct device *dev);
1168int __must_check device_reprobe(struct device *dev);
1da177e4 1169
67dd0772 1170bool device_is_bound(struct device *dev);
6b9cb427 1171
23681e47
GKH
1172/*
1173 * Easy functions for dynamically creating devices on the fly
1174 */
67dd0772 1175__printf(5, 6) struct device *
2bd5c639 1176device_create(const struct class *cls, struct device *parent, dev_t devt,
67dd0772
BG
1177 void *drvdata, const char *fmt, ...);
1178__printf(6, 7) struct device *
2bd5c639 1179device_create_with_groups(const struct class *cls, struct device *parent, dev_t devt,
67dd0772
BG
1180 void *drvdata, const struct attribute_group **groups,
1181 const char *fmt, ...);
d2fff096 1182void device_destroy(const struct class *cls, dev_t devt);
67dd0772
BG
1183
1184int __must_check device_add_groups(struct device *dev,
1185 const struct attribute_group **groups);
1186void device_remove_groups(struct device *dev,
1187 const struct attribute_group **groups);
a7670d42 1188
e323b2dd
DT
1189static inline int __must_check device_add_group(struct device *dev,
1190 const struct attribute_group *grp)
1191{
1192 const struct attribute_group *groups[] = { grp, NULL };
1193
1194 return device_add_groups(dev, groups);
1195}
1196
1197static inline void device_remove_group(struct device *dev,
1198 const struct attribute_group *grp)
1199{
1200 const struct attribute_group *groups[] = { grp, NULL };
1201
1202 return device_remove_groups(dev, groups);
1203}
1204
67dd0772 1205int __must_check devm_device_add_groups(struct device *dev,
57b8ff07 1206 const struct attribute_group **groups);
67dd0772
BG
1207int __must_check devm_device_add_group(struct device *dev,
1208 const struct attribute_group *grp);
57b8ff07 1209
1da177e4
LT
1210/*
1211 * Platform "fixup" functions - allow the platform to have their say
1212 * about devices and actions that the general device layer doesn't
1213 * know about.
1214 */
1215/* Notify platform of device discovery */
d462943a 1216extern int (*platform_notify)(struct device *dev);
1da177e4 1217
d462943a 1218extern int (*platform_notify_remove)(struct device *dev);
1da177e4
LT
1219
1220
880ffb5c 1221/*
1da177e4
LT
1222 * get_device - atomically increment the reference count for the device.
1223 *
1224 */
67dd0772
BG
1225struct device *get_device(struct device *dev);
1226void put_device(struct device *dev);
54da6a09
PZ
1227
1228DEFINE_FREE(put_device, struct device *, if (_T) put_device(_T))
1229
67dd0772 1230bool kill_device(struct device *dev);
1da177e4 1231
2b2af54a 1232#ifdef CONFIG_DEVTMPFS
67dd0772 1233int devtmpfs_mount(void);
2b2af54a 1234#else
5e787dbf 1235static inline int devtmpfs_mount(void) { return 0; }
2b2af54a
KS
1236#endif
1237
116f232b 1238/* drivers/base/power/shutdown.c */
67dd0772 1239void device_shutdown(void);
1da177e4 1240
1da177e4 1241/* debugging and troubleshooting/diagnostic helpers. */
67dd0772 1242const char *dev_driver_string(const struct device *dev);
99bcf217 1243
9ed98953
RW
1244/* Device links interface. */
1245struct device_link *device_link_add(struct device *consumer,
1246 struct device *supplier, u32 flags);
1247void device_link_del(struct device_link *link);
d8842211 1248void device_link_remove(void *consumer, struct device *supplier);
fc5a251d
SK
1249void device_links_supplier_sync_state_pause(void);
1250void device_links_supplier_sync_state_resume(void);
99bcf217 1251
1da177e4
LT
1252/* Create alias, so I can be autoloaded. */
1253#define MODULE_ALIAS_CHARDEV(major,minor) \
1254 MODULE_ALIAS("char-major-" __stringify(major) "-" __stringify(minor))
1255#define MODULE_ALIAS_CHARDEV_MAJOR(major) \
1256 MODULE_ALIAS("char-major-" __stringify(major) "-*")
e52eec13 1257
1da177e4 1258#endif /* _DEVICE_H_ */