blob: f88b498ee9da4adda892a0f9f5b351c235455a89 [file] [log] [blame]
Greg Kroah-Hartman989d42e2017-11-07 17:30:07 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * device.h - generic, centralized driver model
4 *
5 * Copyright (c) 2001-2003 Patrick Mochel <[email protected]>
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07006 * Copyright (c) 2004-2009 Greg Kroah-Hartman <[email protected]>
7 * Copyright (c) 2008-2009 Novell Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
Mauro Carvalho Chehabfe34c892019-06-18 12:34:59 -03009 * See Documentation/driver-api/driver-model/ for more information.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11
12#ifndef _DEVICE_H_
13#define _DEVICE_H_
14
Greg Kroah-Hartmanaf628aa2019-12-09 20:33:00 +010015#include <linux/dev_printk.h>
Lukasz Luba1bc138c2020-06-10 11:12:23 +010016#include <linux/energy_model.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/ioport.h>
18#include <linux/kobject.h>
[email protected]465c7a32005-03-21 11:49:14 -080019#include <linux/klist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/list.h>
Matthew Wilcoxd2a3b912008-05-28 09:28:39 -070021#include <linux/lockdep.h>
Andrew Morton4a7fb632006-08-14 22:43:17 -070022#include <linux/compiler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/types.h>
Paul Gortmakerde477252011-05-26 13:46:22 -040024#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/pm.h>
Arun Sharma600634972011-07-26 16:09:06 -070026#include <linux/atomic.h>
Kay Sievers3c2670e2013-04-06 09:56:00 -070027#include <linux/uidgid.h>
Joe Perches64c862a82013-10-11 13:11:38 -070028#include <linux/gfp.h>
Kees Cook2509b562018-05-08 22:29:52 -070029#include <linux/overflow.h>
Greg Kroah-Hartman5aee2bf2019-12-09 20:33:01 +010030#include <linux/device/bus.h>
Greg Kroah-Hartmana8ae6082019-12-09 20:33:02 +010031#include <linux/device/class.h>
Greg Kroah-Hartman4c002c92019-12-09 20:33:03 +010032#include <linux/device/driver.h>
Peter Zijlstra3c6cc62c2023-05-26 12:23:48 +020033#include <linux/cleanup.h>
Benjamin Herrenschmidtc6dbaef2006-11-11 17:18:39 +110034#include <asm/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Linus Torvalds1da177e2005-04-16 15:20:36 -070036struct device;
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -080037struct device_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038struct device_driver;
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -080039struct driver_private;
Paul Gortmakerde477252011-05-26 13:46:22 -040040struct module;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041struct class;
Kay Sievers6b6e39a2010-11-15 23:13:18 +010042struct subsys_private;
Grant Likelyd706c1b2010-04-13 16:12:28 -070043struct device_node;
Rafael J. Wysockice793482015-03-16 23:49:03 +010044struct fwnode_handle;
Joerg Roedelff217762011-08-26 16:48:26 +020045struct iommu_ops;
Alex Williamson74416e1e2012-05-30 14:18:41 -060046struct iommu_group;
Linus Torvalds23c35f42018-02-02 16:44:14 -080047struct dev_pin_info;
Joerg Roedel045a7042020-03-26 16:08:30 +010048struct dev_iommu;
Thomas Gleixner013bd8e2021-12-10 23:18:55 +010049struct msi_device_data;
Kay Sieversb8c5cec2007-02-16 17:33:36 +010050
Wanlong Gao880ffb52011-05-05 07:55:36 +080051/**
Kay Sieversca22e562011-12-14 14:29:38 -080052 * struct subsys_interface - interfaces to device functions
Randy Dunlap2eda0132012-01-21 11:02:51 -080053 * @name: name of the device function
Bhaskar Chowdhury5dd5f932021-03-21 01:42:40 +053054 * @subsys: subsystem of the devices to attach to
Randy Dunlap2eda0132012-01-21 11:02:51 -080055 * @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
Kay Sieversca22e562011-12-14 14:29:38 -080058 *
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);
Viresh Kumar71db87b2015-07-30 15:04:01 +053069 void (*remove_dev)(struct device *dev, struct subsys_interface *sif);
Kay Sieversca22e562011-12-14 14:29:38 -080070};
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);
Tejun Heod73ce002013-03-12 11:30:05 -070077int subsys_virtual_register(struct bus_type *subsys,
78 const struct attribute_group **groups);
Kay Sieversca22e562011-12-14 14:29:38 -080079
Kay Sievers414264f2007-03-12 21:08:57 +010080/*
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 */
Kay Sieversf9f852d2006-10-07 21:54:55 +020089struct device_type {
Kay Sievers414264f2007-03-12 21:08:57 +010090 const char *name;
David Brownella4dbd672009-06-24 10:06:31 -070091 const struct attribute_group **groups;
Kay Sievers7eff2e72007-08-14 15:15:12 +020092 int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
Kay Sievers3c2670e2013-04-06 09:56:00 -070093 char *(*devnode)(struct device *dev, umode_t *mode,
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -070094 kuid_t *uid, kgid_t *gid);
Kay Sieversf9f852d2006-10-07 21:54:55 +020095 void (*release)(struct device *dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +020096
Dmitry Torokhov8150f322009-07-24 22:11:32 -070097 const struct dev_pm_ops *pm;
Kay Sieversf9f852d2006-10-07 21:54:55 +020098};
99
Kay Sieversa7fd6702005-10-01 14:49:43 +0200100/* interface for exporting device attributes */
101struct device_attribute {
102 struct attribute attr;
103 ssize_t (*show)(struct device *dev, struct device_attribute *attr,
104 char *buf);
105 ssize_t (*store)(struct device *dev, struct device_attribute *attr,
106 const char *buf, size_t count);
107};
108
Kay Sieversca22e562011-12-14 14:29:38 -0800109struct dev_ext_attribute {
110 struct device_attribute attr;
111 void *var;
112};
Kay Sieversa7fd6702005-10-01 14:49:43 +0200113
Kay Sieversca22e562011-12-14 14:29:38 -0800114ssize_t device_show_ulong(struct device *dev, struct device_attribute *attr,
115 char *buf);
116ssize_t device_store_ulong(struct device *dev, struct device_attribute *attr,
117 const char *buf, size_t count);
118ssize_t device_show_int(struct device *dev, struct device_attribute *attr,
119 char *buf);
120ssize_t device_store_int(struct device *dev, struct device_attribute *attr,
121 const char *buf, size_t count);
Borislav Petkov91872392012-10-09 19:52:05 +0200122ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
123 char *buf);
124ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
125 const char *buf, size_t count);
Kay Sieversca22e562011-12-14 14:29:38 -0800126
Kay Sieversa7fd6702005-10-01 14:49:43 +0200127#define DEVICE_ATTR(_name, _mode, _show, _store) \
Kay Sieversca22e562011-12-14 14:29:38 -0800128 struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store)
Christophe Leroy7fda9102017-12-18 11:08:29 +0100129#define DEVICE_ATTR_PREALLOC(_name, _mode, _show, _store) \
130 struct device_attribute dev_attr_##_name = \
131 __ATTR_PREALLOC(_name, _mode, _show, _store)
Greg Kroah-Hartmanced321b2013-07-14 16:05:54 -0700132#define DEVICE_ATTR_RW(_name) \
133 struct device_attribute dev_attr_##_name = __ATTR_RW(_name)
Dan Williams3022c6a2020-06-25 16:51:03 -0700134#define DEVICE_ATTR_ADMIN_RW(_name) \
135 struct device_attribute dev_attr_##_name = __ATTR_RW_MODE(_name, 0600)
Greg Kroah-Hartmanced321b2013-07-14 16:05:54 -0700136#define DEVICE_ATTR_RO(_name) \
137 struct device_attribute dev_attr_##_name = __ATTR_RO(_name)
Dan Williams3022c6a2020-06-25 16:51:03 -0700138#define DEVICE_ATTR_ADMIN_RO(_name) \
139 struct device_attribute dev_attr_##_name = __ATTR_RO_MODE(_name, 0400)
Greg Kroah-Hartman1130c552013-08-23 15:02:56 -0700140#define DEVICE_ATTR_WO(_name) \
141 struct device_attribute dev_attr_##_name = __ATTR_WO(_name)
Kay Sieversca22e562011-12-14 14:29:38 -0800142#define DEVICE_ULONG_ATTR(_name, _mode, _var) \
143 struct dev_ext_attribute dev_attr_##_name = \
144 { __ATTR(_name, _mode, device_show_ulong, device_store_ulong), &(_var) }
145#define DEVICE_INT_ATTR(_name, _mode, _var) \
146 struct dev_ext_attribute dev_attr_##_name = \
Michael Davidson94758182012-05-03 16:19:02 -0700147 { __ATTR(_name, _mode, device_show_int, device_store_int), &(_var) }
Borislav Petkov91872392012-10-09 19:52:05 +0200148#define DEVICE_BOOL_ATTR(_name, _mode, _var) \
149 struct dev_ext_attribute dev_attr_##_name = \
150 { __ATTR(_name, _mode, device_show_bool, device_store_bool), &(_var) }
Alan Stern356c05d2012-05-14 13:30:03 -0400151#define DEVICE_ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store) \
152 struct device_attribute dev_attr_##_name = \
153 __ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store)
Kay Sieversa7fd6702005-10-01 14:49:43 +0200154
Bartosz Golaszewski67dd0772020-06-29 08:50:05 +0200155int device_create_file(struct device *device,
156 const struct device_attribute *entry);
157void device_remove_file(struct device *dev,
158 const struct device_attribute *attr);
159bool device_remove_file_self(struct device *dev,
160 const struct device_attribute *attr);
161int __must_check device_create_bin_file(struct device *dev,
Phil Carmody66ecb922009-12-18 15:34:20 +0200162 const struct bin_attribute *attr);
Bartosz Golaszewski67dd0772020-06-29 08:50:05 +0200163void device_remove_bin_file(struct device *dev,
164 const struct bin_attribute *attr);
Tejun Heo9ac78492007-01-20 16:00:26 +0900165
166/* device resource management */
167typedef void (*dr_release_t)(struct device *dev, void *res);
168typedef int (*dr_match_t)(struct device *dev, void *res, void *match_data);
169
Bartosz Golaszewski67dd0772020-06-29 08:50:05 +0200170void *__devres_alloc_node(dr_release_t release, size_t size, gfp_t gfp,
171 int nid, const char *name) __malloc;
Tejun Heo9ac78492007-01-20 16:00:26 +0900172#define devres_alloc(release, size, gfp) \
Dan Williams7c683942015-10-05 20:35:55 -0400173 __devres_alloc_node(release, size, gfp, NUMA_NO_NODE, #release)
174#define devres_alloc_node(release, size, gfp, nid) \
175 __devres_alloc_node(release, size, gfp, nid, #release)
Dan Williams7c683942015-10-05 20:35:55 -0400176
Bartosz Golaszewski67dd0772020-06-29 08:50:05 +0200177void devres_for_each_res(struct device *dev, dr_release_t release,
178 dr_match_t match, void *match_data,
179 void (*fn)(struct device *, void *, void *),
180 void *data);
181void devres_free(void *res);
182void devres_add(struct device *dev, void *res);
183void *devres_find(struct device *dev, dr_release_t release,
184 dr_match_t match, void *match_data);
185void *devres_get(struct device *dev, void *new_res,
186 dr_match_t match, void *match_data);
187void *devres_remove(struct device *dev, dr_release_t release,
188 dr_match_t match, void *match_data);
189int devres_destroy(struct device *dev, dr_release_t release,
190 dr_match_t match, void *match_data);
191int devres_release(struct device *dev, dr_release_t release,
192 dr_match_t match, void *match_data);
Tejun Heo9ac78492007-01-20 16:00:26 +0900193
194/* devres group */
Bartosz Golaszewski67dd0772020-06-29 08:50:05 +0200195void * __must_check devres_open_group(struct device *dev, void *id, gfp_t gfp);
196void devres_close_group(struct device *dev, void *id);
197void devres_remove_group(struct device *dev, void *id);
198int devres_release_group(struct device *dev, void *id);
Tejun Heo9ac78492007-01-20 16:00:26 +0900199
Joe Perches64c862a82013-10-11 13:11:38 -0700200/* managed devm_k.alloc/kfree for device drivers */
Bartosz Golaszewski67dd0772020-06-29 08:50:05 +0200201void *devm_kmalloc(struct device *dev, size_t size, gfp_t gfp) __malloc;
Bartosz Golaszewskif8248572020-08-24 19:38:57 +0200202void *devm_krealloc(struct device *dev, void *ptr, size_t size,
203 gfp_t gfp) __must_check;
Bartosz Golaszewski67dd0772020-06-29 08:50:05 +0200204__printf(3, 0) char *devm_kvasprintf(struct device *dev, gfp_t gfp,
205 const char *fmt, va_list ap) __malloc;
206__printf(3, 4) char *devm_kasprintf(struct device *dev, gfp_t gfp,
207 const char *fmt, ...) __malloc;
Joe Perches64c862a82013-10-11 13:11:38 -0700208static inline void *devm_kzalloc(struct device *dev, size_t size, gfp_t gfp)
209{
210 return devm_kmalloc(dev, size, gfp | __GFP_ZERO);
211}
212static inline void *devm_kmalloc_array(struct device *dev,
213 size_t n, size_t size, gfp_t flags)
214{
Kees Cook2509b562018-05-08 22:29:52 -0700215 size_t bytes;
216
217 if (unlikely(check_mul_overflow(n, size, &bytes)))
Joe Perches64c862a82013-10-11 13:11:38 -0700218 return NULL;
Kees Cook2509b562018-05-08 22:29:52 -0700219
220 return devm_kmalloc(dev, bytes, flags);
Joe Perches64c862a82013-10-11 13:11:38 -0700221}
222static inline void *devm_kcalloc(struct device *dev,
223 size_t n, size_t size, gfp_t flags)
224{
225 return devm_kmalloc_array(dev, n, size, flags | __GFP_ZERO);
226}
Bartosz Golaszewski67dd0772020-06-29 08:50:05 +0200227void devm_kfree(struct device *dev, const void *p);
228char *devm_kstrdup(struct device *dev, const char *s, gfp_t gfp) __malloc;
229const char *devm_kstrdup_const(struct device *dev, const char *s, gfp_t gfp);
230void *devm_kmemdup(struct device *dev, const void *src, size_t len, gfp_t gfp);
Tejun Heo9ac78492007-01-20 16:00:26 +0900231
Bartosz Golaszewski67dd0772020-06-29 08:50:05 +0200232unsigned long devm_get_free_pages(struct device *dev,
233 gfp_t gfp_mask, unsigned int order);
234void devm_free_pages(struct device *dev, unsigned long addr);
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -0700235
Arnd Bergmanneef778c2019-07-04 15:14:45 -0700236void __iomem *devm_ioremap_resource(struct device *dev,
237 const struct resource *res);
Bartosz Golaszewskib873af62019-10-22 10:43:13 +0200238void __iomem *devm_ioremap_resource_wc(struct device *dev,
239 const struct resource *res);
Wolfram Sang72f8c0b2011-10-25 15:16:47 +0200240
Benjamin Herrenschmidtd5e83822018-06-05 13:21:26 +1000241void __iomem *devm_of_iomap(struct device *dev,
242 struct device_node *node, int index,
243 resource_size_t *size);
244
Dmitry Torokhovd6b0c582013-02-23 13:11:14 -0800245/* allows to add/remove a custom action to devres stack */
246int devm_add_action(struct device *dev, void (*action)(void *), void *data);
247void devm_remove_action(struct device *dev, void (*action)(void *), void *data);
Dan Williams2374b682019-06-13 15:56:18 -0700248void devm_release_action(struct device *dev, void (*action)(void *), void *data);
Dmitry Torokhovd6b0c582013-02-23 13:11:14 -0800249
Sudip Mukherjeea3499e92015-12-23 17:57:19 +0530250static inline int devm_add_action_or_reset(struct device *dev,
251 void (*action)(void *), void *data)
252{
253 int ret;
254
255 ret = devm_add_action(dev, action, data);
256 if (ret)
257 action(data);
258
259 return ret;
260}
261
Madalin Bucurff86aae2016-11-15 10:41:01 +0200262/**
263 * devm_alloc_percpu - Resource-managed alloc_percpu
264 * @dev: Device to allocate per-cpu memory for
265 * @type: Type to allocate per-cpu memory for
266 *
267 * Managed alloc_percpu. Per-cpu memory allocated with this function is
268 * automatically freed on driver detach.
269 *
270 * RETURNS:
271 * Pointer to allocated memory on success, NULL on failure.
272 */
273#define devm_alloc_percpu(dev, type) \
274 ((typeof(type) __percpu *)__devm_alloc_percpu((dev), sizeof(type), \
275 __alignof__(type)))
276
277void __percpu *__devm_alloc_percpu(struct device *dev, size_t size,
278 size_t align);
279void devm_free_percpu(struct device *dev, void __percpu *pdata);
280
FUJITA Tomonori6b7b6512008-02-04 22:27:55 -0800281struct device_dma_parameters {
282 /*
283 * a low level driver may set these to teach IOMMU code about
284 * sg limitations.
285 */
286 unsigned int max_segment_size;
Jianxiong Gao36950f22021-02-01 10:30:15 -0800287 unsigned int min_align_mask;
FUJITA Tomonori6b7b6512008-02-04 22:27:55 -0800288 unsigned long segment_boundary_mask;
289};
290
Heikki Krogeruscd7753d2018-09-20 14:23:40 +0300291/**
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100292 * enum device_link_state - Device link states.
293 * @DL_STATE_NONE: The presence of the drivers is not being tracked.
294 * @DL_STATE_DORMANT: None of the supplier/consumer drivers is present.
295 * @DL_STATE_AVAILABLE: The supplier driver is present, but the consumer is not.
296 * @DL_STATE_CONSUMER_PROBE: The consumer is probing (supplier driver present).
297 * @DL_STATE_ACTIVE: Both the supplier and consumer drivers are present.
298 * @DL_STATE_SUPPLIER_UNBIND: The supplier driver is unbinding.
299 */
300enum device_link_state {
301 DL_STATE_NONE = -1,
302 DL_STATE_DORMANT = 0,
303 DL_STATE_AVAILABLE,
304 DL_STATE_CONSUMER_PROBE,
305 DL_STATE_ACTIVE,
306 DL_STATE_SUPPLIER_UNBIND,
307};
308
309/*
310 * Device link flags.
311 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200312 * STATELESS: The core will not remove this link automatically.
Vivek Gautame88728f2018-06-27 18:20:55 +0530313 * AUTOREMOVE_CONSUMER: Remove the link automatically on consumer driver unbind.
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100314 * PM_RUNTIME: If set, the runtime PM framework will use this link.
315 * RPM_ACTIVE: Run pm_runtime_get_sync() on the supplier during link creation.
Vivek Gautam1689cac2018-06-27 18:20:56 +0530316 * AUTOREMOVE_SUPPLIER: Remove the link automatically on supplier driver unbind.
Rafael J. Wysockie7dd4012019-02-01 01:59:42 +0100317 * AUTOPROBE_CONSUMER: Probe consumer driver automatically after supplier binds.
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200318 * MANAGED: The core tracks presence of supplier/consumer drivers (internal).
Saravana Kannan05ef9832019-10-28 15:00:22 -0700319 * SYNC_STATE_ONLY: Link only affects sync_state() behavior.
Saravana Kannan4b9bbb22020-12-17 19:17:00 -0800320 * INFERRED: Inferred from data (eg: firmware) and not from driver actions.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100321 */
Vivek Gautame88728f2018-06-27 18:20:55 +0530322#define DL_FLAG_STATELESS BIT(0)
323#define DL_FLAG_AUTOREMOVE_CONSUMER BIT(1)
324#define DL_FLAG_PM_RUNTIME BIT(2)
325#define DL_FLAG_RPM_ACTIVE BIT(3)
Vivek Gautam1689cac2018-06-27 18:20:56 +0530326#define DL_FLAG_AUTOREMOVE_SUPPLIER BIT(4)
Rafael J. Wysockie7dd4012019-02-01 01:59:42 +0100327#define DL_FLAG_AUTOPROBE_CONSUMER BIT(5)
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200328#define DL_FLAG_MANAGED BIT(6)
Saravana Kannan05ef9832019-10-28 15:00:22 -0700329#define DL_FLAG_SYNC_STATE_ONLY BIT(7)
Saravana Kannan4b9bbb22020-12-17 19:17:00 -0800330#define DL_FLAG_INFERRED BIT(8)
Saravana Kannan2455b812023-02-06 17:41:57 -0800331#define DL_FLAG_CYCLE BIT(9)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100332
333/**
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100334 * enum dl_dev_state - Device driver presence tracking information.
335 * @DL_DEV_NO_DRIVER: There is no driver attached to the device.
336 * @DL_DEV_PROBING: A driver is probing.
337 * @DL_DEV_DRIVER_BOUND: The driver has been bound to the device.
338 * @DL_DEV_UNBINDING: The driver is unbinding from the device.
339 */
340enum dl_dev_state {
341 DL_DEV_NO_DRIVER = 0,
342 DL_DEV_PROBING,
343 DL_DEV_DRIVER_BOUND,
344 DL_DEV_UNBINDING,
345};
346
347/**
Rajat Jain70f400d2021-05-24 10:18:11 -0700348 * enum device_removable - Whether the device is removable. The criteria for a
349 * device to be classified as removable is determined by its subsystem or bus.
350 * @DEVICE_REMOVABLE_NOT_SUPPORTED: This attribute is not supported for this
351 * device (default).
352 * @DEVICE_REMOVABLE_UNKNOWN: Device location is Unknown.
353 * @DEVICE_FIXED: Device is not removable by the user.
354 * @DEVICE_REMOVABLE: Device is removable by the user.
355 */
356enum device_removable {
357 DEVICE_REMOVABLE_NOT_SUPPORTED = 0, /* must be 0 */
358 DEVICE_REMOVABLE_UNKNOWN,
359 DEVICE_FIXED,
360 DEVICE_REMOVABLE,
361};
362
363/**
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100364 * struct dev_links_info - Device data related to device links.
365 * @suppliers: List of links to supplier devices.
366 * @consumers: List of links to consumer devices.
Saravana Kannan3b052a32020-11-20 18:02:17 -0800367 * @defer_sync: Hook to global list of devices that have deferred sync_state.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100368 * @status: Driver status information.
369 */
370struct dev_links_info {
371 struct list_head suppliers;
372 struct list_head consumers;
Saravana Kannan3b052a32020-11-20 18:02:17 -0800373 struct list_head defer_sync;
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100374 enum dl_dev_state status;
375};
376
377/**
Thomas Gleixner34fff6282021-12-10 23:18:54 +0100378 * struct dev_msi_info - Device data related to MSI
379 * @domain: The MSI interrupt domain associated to the device
Thomas Gleixner013bd8e2021-12-10 23:18:55 +0100380 * @data: Pointer to MSI device data
Thomas Gleixner34fff6282021-12-10 23:18:54 +0100381 */
382struct dev_msi_info {
383#ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN
384 struct irq_domain *domain;
385#endif
Thomas Gleixner013bd8e2021-12-10 23:18:55 +0100386#ifdef CONFIG_GENERIC_MSI_IRQ
387 struct msi_device_data *data;
388#endif
Thomas Gleixner34fff6282021-12-10 23:18:54 +0100389};
390
391/**
Won Chung6423d292022-03-14 19:54:58 +0000392 * enum device_physical_location_panel - Describes which panel surface of the
393 * system's housing the device connection point resides on.
394 * @DEVICE_PANEL_TOP: Device connection point is on the top panel.
395 * @DEVICE_PANEL_BOTTOM: Device connection point is on the bottom panel.
396 * @DEVICE_PANEL_LEFT: Device connection point is on the left panel.
397 * @DEVICE_PANEL_RIGHT: Device connection point is on the right panel.
398 * @DEVICE_PANEL_FRONT: Device connection point is on the front panel.
399 * @DEVICE_PANEL_BACK: Device connection point is on the back panel.
400 * @DEVICE_PANEL_UNKNOWN: The panel with device connection point is unknown.
401 */
402enum device_physical_location_panel {
403 DEVICE_PANEL_TOP,
404 DEVICE_PANEL_BOTTOM,
405 DEVICE_PANEL_LEFT,
406 DEVICE_PANEL_RIGHT,
407 DEVICE_PANEL_FRONT,
408 DEVICE_PANEL_BACK,
409 DEVICE_PANEL_UNKNOWN,
410};
411
412/**
413 * enum device_physical_location_vertical_position - Describes vertical
414 * position of the device connection point on the panel surface.
415 * @DEVICE_VERT_POS_UPPER: Device connection point is at upper part of panel.
416 * @DEVICE_VERT_POS_CENTER: Device connection point is at center part of panel.
417 * @DEVICE_VERT_POS_LOWER: Device connection point is at lower part of panel.
418 */
419enum device_physical_location_vertical_position {
420 DEVICE_VERT_POS_UPPER,
421 DEVICE_VERT_POS_CENTER,
422 DEVICE_VERT_POS_LOWER,
423};
424
425/**
426 * enum device_physical_location_horizontal_position - Describes horizontal
427 * position of the device connection point on the panel surface.
428 * @DEVICE_HORI_POS_LEFT: Device connection point is at left part of panel.
429 * @DEVICE_HORI_POS_CENTER: Device connection point is at center part of panel.
430 * @DEVICE_HORI_POS_RIGHT: Device connection point is at right part of panel.
431 */
432enum device_physical_location_horizontal_position {
433 DEVICE_HORI_POS_LEFT,
434 DEVICE_HORI_POS_CENTER,
435 DEVICE_HORI_POS_RIGHT,
436};
437
438/**
439 * struct device_physical_location - Device data related to physical location
440 * of the device connection point.
441 * @panel: Panel surface of the system's housing that the device connection
442 * point resides on.
443 * @vertical_position: Vertical position of the device connection point within
444 * the panel.
445 * @horizontal_position: Horizontal position of the device connection point
446 * within the panel.
447 * @dock: Set if the device connection point resides in a docking station or
448 * port replicator.
449 * @lid: Set if this device connection point resides on the lid of laptop
450 * system.
451 */
452struct device_physical_location {
453 enum device_physical_location_panel panel;
454 enum device_physical_location_vertical_position vertical_position;
455 enum device_physical_location_horizontal_position horizontal_position;
456 bool dock;
457 bool lid;
458};
459
460/**
Wanlong Gao880ffb52011-05-05 07:55:36 +0800461 * struct device - The basic device structure
462 * @parent: The device's "parent" device, the device to which it is attached.
463 * In most cases, a parent device is some sort of bus or host
464 * controller. If parent is NULL, the device, is a top-level device,
465 * which is not usually what you want.
466 * @p: Holds the private data of the driver core portions of the device.
467 * See the comment of the struct device_private for detail.
468 * @kobj: A top-level, abstract class from which other classes are derived.
469 * @init_name: Initial name of the device.
470 * @type: The type of device.
471 * This identifies the device type and carries type-specific
472 * information.
473 * @mutex: Mutex to synchronize calls to its driver.
Wanlong Gao880ffb52011-05-05 07:55:36 +0800474 * @bus: Type of bus device is on.
475 * @driver: Which driver has allocated this
476 * @platform_data: Platform data specific to the device.
477 * Example: For devices on custom boards, as typical of embedded
478 * and SOC based hardware, Linux often uses platform_data to point
479 * to board-specific structures describing devices and how they
480 * are wired. That can include what ports are available, chip
481 * variants, which GPIO pins act in what additional roles, and so
482 * on. This shrinks the "Board Support Packages" (BSPs) and
483 * minimizes board-specific #ifdefs in drivers.
Jean Delvare1bb6c082014-04-14 12:54:47 +0200484 * @driver_data: Private pointer for driver specific info.
Lukas Wunner64df1142016-12-04 13:10:04 +0100485 * @links: Links to suppliers and consumers of this device.
Wanlong Gao880ffb52011-05-05 07:55:36 +0800486 * @power: For device power management.
Geert Uytterhoeven74378c52017-09-05 20:16:27 +0200487 * See Documentation/driver-api/pm/devices.rst for details.
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200488 * @pm_domain: Provide callbacks that are executed during system suspend,
Wanlong Gao880ffb52011-05-05 07:55:36 +0800489 * hibernation, system resume and during runtime PM transitions
490 * along with subsystem-level and driver-level callbacks.
Randy Dunlap95035ea2020-09-06 20:42:52 -0700491 * @em_pd: device's energy model performance domain
Linus Walleijab780292013-01-22 10:56:14 -0700492 * @pins: For device pin management.
Mauro Carvalho Chehab4b0c9942021-05-19 10:51:42 +0200493 * See Documentation/driver-api/pin-control.rst for details.
Thomas Gleixner34fff6282021-12-10 23:18:54 +0100494 * @msi: MSI related data
Wanlong Gao880ffb52011-05-05 07:55:36 +0800495 * @numa_node: NUMA node this device is close to.
Jonathan Corbet6a7a8172017-08-24 16:09:10 -0600496 * @dma_ops: DMA mapping operations for this device.
Wanlong Gao880ffb52011-05-05 07:55:36 +0800497 * @dma_mask: Dma mask (if dma'ble device).
498 * @coherent_dma_mask: Like dma_mask, but for alloc_coherent mapping as not all
499 * hardware supports 64-bit addresses for consistent allocations
500 * such descriptors.
Nicolas Saenz Juliennea7ba70f2019-11-21 10:26:44 +0100501 * @bus_dma_limit: Limit of an upstream bridge or bus which imposes a smaller
502 * DMA limit than the device itself supports.
Jim Quinlane0d07272020-09-17 18:43:40 +0200503 * @dma_range_map: map for DMA memory ranges relative to that of RAM
Wanlong Gao880ffb52011-05-05 07:55:36 +0800504 * @dma_parms: A low level driver may set these to teach IOMMU code about
505 * segment limitations.
506 * @dma_pools: Dma pools (if dma'ble device).
507 * @dma_mem: Internal for coherent mem override.
Michael Opdenackerbfd63cd2013-06-26 05:57:06 +0200508 * @cma_area: Contiguous memory area for dma allocations
Claire Chang69031f52021-06-19 11:40:34 +0800509 * @dma_io_tlb_mem: Pointer to the swiotlb pool used. Not for driver use.
Wanlong Gao880ffb52011-05-05 07:55:36 +0800510 * @archdata: For arch-specific additions.
511 * @of_node: Associated device tree node.
Rafael J. Wysockice793482015-03-16 23:49:03 +0100512 * @fwnode: Associated device node supplied by platform firmware.
Wanlong Gao880ffb52011-05-05 07:55:36 +0800513 * @devt: For creating the sysfs "dev".
Randy Dunlap2eda0132012-01-21 11:02:51 -0800514 * @id: device instance
Wanlong Gao880ffb52011-05-05 07:55:36 +0800515 * @devres_lock: Spinlock to protect the resource of the device.
516 * @devres_head: The resources list of the device.
517 * @knode_class: The node used to add the device to the class list.
518 * @class: The class of the device.
519 * @groups: Optional attribute groups.
520 * @release: Callback to free the device after all references have
521 * gone away. This should be set by the allocator of the
522 * device (i.e. the bus driver that discovered the device).
Michael Opdenackerbfd63cd2013-06-26 05:57:06 +0200523 * @iommu_group: IOMMU group the device belongs to.
Joerg Roedel045a7042020-03-26 16:08:30 +0100524 * @iommu: Per device generic IOMMU runtime data
Won Chung6423d292022-03-14 19:54:58 +0000525 * @physical_location: Describes physical location of the device connection
526 * point in the system housing.
Rajat Jain70f400d2021-05-24 10:18:11 -0700527 * @removable: Whether the device can be removed from the system. This
528 * should be set by the subsystem / bus driver that discovered
529 * the device.
Wanlong Gao880ffb52011-05-05 07:55:36 +0800530 *
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +0200531 * @offline_disabled: If set, the device is permanently online.
532 * @offline: Set after successful invocation of bus type's .offline().
Johan Hovold4e75e1d2017-06-06 17:59:00 +0200533 * @of_node_reused: Set if the device-tree node is shared with an ancestor
534 * device.
Saravana Kannanfc5a2512019-09-04 14:11:23 -0700535 * @state_synced: The hardware state of this device has been synced to match
536 * the software state of this device by calling the driver/bus
537 * sync_state() callback.
Saravana Kannanf2db85b2021-03-02 13:11:30 -0800538 * @can_match: The device has matched with a driver at least once or it is in
539 * a bus (like AMBA) which can't check for matching drivers until
540 * other devices probe successfully.
Christoph Hellwigf3ecc0f2018-08-19 14:53:20 +0200541 * @dma_coherent: this particular device is dma coherent, even if the
542 * architecture supports non-coherent devices.
Christoph Hellwigd35834c2020-03-23 18:19:30 +0100543 * @dma_ops_bypass: If set to %true then the dma_ops are bypassed for the
544 * streaming DMA operations (->map_* / ->unmap_* / ->sync_*),
545 * and optionall (if the coherent mask is large enough) also
546 * for dma allocations. This flag is managed by the dma ops
547 * instance from ->dma_supported.
Wanlong Gao880ffb52011-05-05 07:55:36 +0800548 *
549 * At the lowest level, every device in a Linux system is represented by an
550 * instance of struct device. The device structure contains the information
551 * that the device model core needs to model the system. Most subsystems,
552 * however, track additional information about the devices they host. As a
553 * result, it is rare for devices to be represented by bare device structures;
554 * instead, that structure, like kobject structures, is usually embedded within
555 * a higher-level representation of the device.
556 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557struct device {
Greg Kroah-Hartman159ef31e2019-02-26 15:32:29 +0100558 struct kobject kobj;
David Brownell49a4ec12007-05-08 00:29:39 -0700559 struct device *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800561 struct device_private *p;
562
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -0700563 const char *init_name; /* initial name of the device */
Stephen Hemmingeraed65af2011-03-28 09:12:52 -0700564 const struct device_type *type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Greg Kroah-Hartmand4629432008-01-24 21:04:46 -0800566 struct bus_type *bus; /* type of bus device is on */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 struct device_driver *driver; /* which driver has allocated this
568 device */
Greg Kroah-Hartmane67c8562009-03-08 23:13:32 +0800569 void *platform_data; /* Platform specific data, device
570 core doesn't touch it */
Jean Delvare1bb6c082014-04-14 12:54:47 +0200571 void *driver_data; /* Driver data, set and get with
David Engraf2c6f4fc2019-02-05 13:19:52 +0100572 dev_set_drvdata/dev_get_drvdata */
Greg Kroah-Hartman159ef31e2019-02-26 15:32:29 +0100573 struct mutex mutex; /* mutex to synchronize calls to
574 * its driver.
575 */
576
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100577 struct dev_links_info links;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 struct dev_pm_info power;
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200579 struct dev_pm_domain *pm_domain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Lukasz Luba1bc138c2020-06-10 11:12:23 +0100581#ifdef CONFIG_ENERGY_MODEL
582 struct em_perf_domain *em_pd;
583#endif
584
Linus Walleijab780292013-01-22 10:56:14 -0700585#ifdef CONFIG_PINCTRL
586 struct dev_pin_info *pins;
587#endif
Thomas Gleixner34fff6282021-12-10 23:18:54 +0100588 struct dev_msi_info msi;
Christoph Hellwig2f9237d2020-07-08 09:30:00 +0200589#ifdef CONFIG_DMA_OPS
Bart Van Assche56579332017-01-20 13:04:02 -0800590 const struct dma_map_ops *dma_ops;
Christoph Hellwig2f9237d2020-07-08 09:30:00 +0200591#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 u64 *dma_mask; /* dma mask (if dma'able device) */
593 u64 coherent_dma_mask;/* Like dma_mask, but for
594 alloc_coherent mappings as
595 not all hardware supports
596 64 bit addresses for consistent
597 allocations such descriptors. */
Nicolas Saenz Juliennea7ba70f2019-11-21 10:26:44 +0100598 u64 bus_dma_limit; /* upstream dma constraint */
Jim Quinlane0d07272020-09-17 18:43:40 +0200599 const struct bus_dma_region *dma_range_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
FUJITA Tomonori6b7b6512008-02-04 22:27:55 -0800601 struct device_dma_parameters *dma_parms;
602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 struct list_head dma_pools; /* dma pools (if dma'ble) */
604
Christoph Hellwigff4c25f2019-02-03 20:12:02 +0100605#ifdef CONFIG_DMA_DECLARE_COHERENT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 struct dma_coherent_mem *dma_mem; /* internal for coherent mem
607 override */
Christoph Hellwig2b281292018-12-25 14:03:32 +0100608#endif
Marek Szyprowskia2547382013-07-29 14:31:45 +0200609#ifdef CONFIG_DMA_CMA
Marek Szyprowskic64be2b2011-12-29 13:09:51 +0100610 struct cma *cma_area; /* contiguous memory area for dma
611 allocations */
612#endif
Claire Chang69031f52021-06-19 11:40:34 +0800613#ifdef CONFIG_SWIOTLB
614 struct io_tlb_mem *dma_io_tlb_mem;
615#endif
Benjamin Herrenschmidtc6dbaef2006-11-11 17:18:39 +1100616 /* arch specific additions */
617 struct dev_archdata archdata;
Grant Likelyc9e358d2011-01-21 09:24:48 -0700618
619 struct device_node *of_node; /* associated device tree node */
Rafael J. Wysockice793482015-03-16 23:49:03 +0100620 struct fwnode_handle *fwnode; /* firmware device node */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Greg Kroah-Hartman159ef31e2019-02-26 15:32:29 +0100622#ifdef CONFIG_NUMA
623 int numa_node; /* NUMA node this device is close to */
624#endif
Matthew Wilcox929d2fa2008-10-16 15:51:35 -0600625 dev_t devt; /* dev_t, creates the sysfs "dev" */
Kay Sieversca22e562011-12-14 14:29:38 -0800626 u32 id; /* device instance */
Matthew Wilcox929d2fa2008-10-16 15:51:35 -0600627
Tejun Heo9ac78492007-01-20 16:00:26 +0900628 spinlock_t devres_lock;
629 struct list_head devres_head;
630
Kay Sieversb7a3e812006-10-07 21:54:55 +0200631 struct class *class;
David Brownella4dbd672009-06-24 10:06:31 -0700632 const struct attribute_group **groups; /* optional groups */
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700633
Greg Kroah-Hartmand4629432008-01-24 21:04:46 -0800634 void (*release)(struct device *dev);
Alex Williamson74416e1e2012-05-30 14:18:41 -0600635 struct iommu_group *iommu_group;
Joerg Roedel045a7042020-03-26 16:08:30 +0100636 struct dev_iommu *iommu;
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +0200637
Won Chung6423d292022-03-14 19:54:58 +0000638 struct device_physical_location *physical_location;
639
Rajat Jain70f400d2021-05-24 10:18:11 -0700640 enum device_removable removable;
641
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +0200642 bool offline_disabled:1;
643 bool offline:1;
Johan Hovold4e75e1d2017-06-06 17:59:00 +0200644 bool of_node_reused:1;
Saravana Kannanfc5a2512019-09-04 14:11:23 -0700645 bool state_synced:1;
Saravana Kannanf2db85b2021-03-02 13:11:30 -0800646 bool can_match:1;
Christoph Hellwigf3ecc0f2018-08-19 14:53:20 +0200647#if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE) || \
648 defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU) || \
649 defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL)
650 bool dma_coherent:1;
651#endif
Christoph Hellwigd35834c2020-03-23 18:19:30 +0100652#ifdef CONFIG_DMA_OPS_BYPASS
653 bool dma_ops_bypass : 1;
654#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655};
656
Saravana Kannan287905e2020-05-21 12:17:58 -0700657/**
658 * struct device_link - Device link representation.
659 * @supplier: The device on the supplier end of the link.
660 * @s_node: Hook to the supplier device's list of links to consumers.
661 * @consumer: The device on the consumer end of the link.
662 * @c_node: Hook to the consumer device's list of links to suppliers.
663 * @link_dev: device used to expose link details in sysfs
664 * @status: The state of the link (with respect to the presence of drivers).
665 * @flags: Link flags.
666 * @rpm_active: Whether or not the consumer device is runtime-PM-active.
667 * @kref: Count repeated addition of the same link.
Rafael J. Wysocki80dd33c2021-05-14 14:10:15 +0200668 * @rm_work: Work structure used for removing the link.
Saravana Kannan287905e2020-05-21 12:17:58 -0700669 * @supplier_preactivated: Supplier has been made active before consumer probe.
670 */
671struct device_link {
672 struct device *supplier;
673 struct list_head s_node;
674 struct device *consumer;
675 struct list_head c_node;
676 struct device link_dev;
677 enum device_link_state status;
678 u32 flags;
679 refcount_t rpm_active;
680 struct kref kref;
Rafael J. Wysocki80dd33c2021-05-14 14:10:15 +0200681 struct work_struct rm_work;
Saravana Kannan287905e2020-05-21 12:17:58 -0700682 bool supplier_preactivated; /* Owned by consumer probe. */
683};
684
Lars-Peter Clausena4232962012-07-03 18:49:35 +0200685static inline struct device *kobj_to_dev(struct kobject *kobj)
686{
687 return container_of(kobj, struct device, kobj);
688}
689
Joerg Roedeldbba1972018-11-30 12:51:52 +0100690/**
691 * device_iommu_mapped - Returns true when the device DMA is translated
692 * by an IOMMU
693 * @dev: Device to perform the check on
694 */
695static inline bool device_iommu_mapped(struct device *dev)
696{
697 return (dev->iommu_group != NULL);
698}
699
Alan Stern9a3df1f2008-03-19 22:39:13 +0100700/* Get the wakeup routines, which depend on struct device */
701#include <linux/pm_wakeup.h>
702
Jean Delvarebf9ca692008-07-30 12:29:21 -0700703static inline const char *dev_name(const struct device *dev)
Kay Sievers06916632008-05-02 06:02:41 +0200704{
Paul Mundta636ee7f2010-03-09 06:57:53 +0000705 /* Use the init name until the kobject becomes available */
706 if (dev->init_name)
707 return dev->init_name;
708
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100709 return kobject_name(&dev->kobj);
Kay Sievers06916632008-05-02 06:02:41 +0200710}
711
Saravana Kannane020ff62021-01-10 09:54:07 -0800712/**
713 * dev_bus_name - Return a device's bus/class name, if at all possible
714 * @dev: struct device to get the bus/class name of
715 *
716 * Will return the name of the bus/class the device is attached to. If it is
717 * not attached to a bus/class, an empty string will be returned.
718 */
719static inline const char *dev_bus_name(const struct device *dev)
720{
721 return dev->bus ? dev->bus->name : (dev->class ? dev->class->name : "");
722}
723
Bartosz Golaszewski67dd0772020-06-29 08:50:05 +0200724__printf(2, 3) int dev_set_name(struct device *dev, const char *name, ...);
Stephen Rothwell413c2392008-05-30 10:16:40 +1000725
Christoph Hellwig87348132006-12-06 20:32:33 -0800726#ifdef CONFIG_NUMA
727static inline int dev_to_node(struct device *dev)
728{
729 return dev->numa_node;
730}
731static inline void set_dev_node(struct device *dev, int node)
732{
733 dev->numa_node = node;
734}
735#else
736static inline int dev_to_node(struct device *dev)
737{
Anshuman Khandual98fa15f2019-03-05 15:42:58 -0800738 return NUMA_NO_NODE;
Christoph Hellwig87348132006-12-06 20:32:33 -0800739}
740static inline void set_dev_node(struct device *dev, int node)
741{
742}
743#endif
744
Marc Zyngierf1421db2015-07-28 14:46:10 +0100745static inline struct irq_domain *dev_get_msi_domain(const struct device *dev)
746{
747#ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN
Thomas Gleixner34fff6282021-12-10 23:18:54 +0100748 return dev->msi.domain;
Marc Zyngierf1421db2015-07-28 14:46:10 +0100749#else
750 return NULL;
751#endif
752}
753
754static inline void dev_set_msi_domain(struct device *dev, struct irq_domain *d)
755{
756#ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN
Thomas Gleixner34fff6282021-12-10 23:18:54 +0100757 dev->msi.domain = d;
Marc Zyngierf1421db2015-07-28 14:46:10 +0100758#endif
759}
760
Jean Delvarea996d012014-04-14 12:58:53 +0200761static inline void *dev_get_drvdata(const struct device *dev)
762{
763 return dev->driver_data;
764}
765
766static inline void dev_set_drvdata(struct device *dev, void *data)
767{
768 dev->driver_data = data;
769}
770
Rafael J. Wysocki5c095a02011-08-25 15:33:50 +0200771static inline struct pm_subsys_data *dev_to_psd(struct device *dev)
772{
773 return dev ? dev->power.subsys_data : NULL;
774}
775
Ming Leif67f1292009-03-01 21:10:49 +0800776static inline unsigned int dev_get_uevent_suppress(const struct device *dev)
777{
778 return dev->kobj.uevent_suppress;
779}
780
781static inline void dev_set_uevent_suppress(struct device *dev, int val)
782{
783 dev->kobj.uevent_suppress = val;
784}
785
Daniel Ritzd305ef52005-09-22 00:47:24 -0700786static inline int device_is_registered(struct device *dev)
787{
Greg Kroah-Hartman3f62e572008-03-13 17:07:03 -0400788 return dev->kobj.state_in_sysfs;
Daniel Ritzd305ef52005-09-22 00:47:24 -0700789}
790
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100791static inline void device_enable_async_suspend(struct device *dev)
792{
Alan Sternf76b168b2011-06-18 20:22:23 +0200793 if (!dev->power.is_prepared)
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100794 dev->power.async_suspend = true;
795}
796
Rafael J. Wysocki5a2eb852010-01-23 22:25:23 +0100797static inline void device_disable_async_suspend(struct device *dev)
798{
Alan Sternf76b168b2011-06-18 20:22:23 +0200799 if (!dev->power.is_prepared)
Rafael J. Wysocki5a2eb852010-01-23 22:25:23 +0100800 dev->power.async_suspend = false;
801}
802
803static inline bool device_async_suspend_enabled(struct device *dev)
804{
805 return !!dev->power.async_suspend;
806}
807
Sudeep Holla85945c282019-02-14 18:29:10 +0000808static inline bool device_pm_not_required(struct device *dev)
809{
810 return dev->power.no_pm;
811}
812
813static inline void device_set_pm_not_required(struct device *dev)
814{
815 dev->power.no_pm = true;
816}
817
Rafael J. Wysockifeb70af2012-08-13 14:00:25 +0200818static inline void dev_pm_syscore_device(struct device *dev, bool val)
819{
820#ifdef CONFIG_PM_SLEEP
821 dev->power.syscore = val;
822#endif
823}
824
Rafael J. Wysocki08810a412017-10-25 14:12:29 +0200825static inline void dev_pm_set_driver_flags(struct device *dev, u32 flags)
826{
827 dev->power.driver_flags = flags;
828}
829
830static inline bool dev_pm_test_driver_flags(struct device *dev, u32 flags)
831{
832 return !!(dev->power.driver_flags & flags);
833}
834
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800835static inline void device_lock(struct device *dev)
836{
Thomas Gleixner31427882010-01-29 20:39:02 +0000837 mutex_lock(&dev->mutex);
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800838}
839
Oliver Neukum7dd9cba2016-01-21 15:18:47 +0100840static inline int device_lock_interruptible(struct device *dev)
841{
842 return mutex_lock_interruptible(&dev->mutex);
843}
844
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800845static inline int device_trylock(struct device *dev)
846{
Thomas Gleixner31427882010-01-29 20:39:02 +0000847 return mutex_trylock(&dev->mutex);
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800848}
849
850static inline void device_unlock(struct device *dev)
851{
Thomas Gleixner31427882010-01-29 20:39:02 +0000852 mutex_unlock(&dev->mutex);
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800853}
854
Konrad Rzeszutek Wilkac801022014-12-03 16:40:27 -0500855static inline void device_lock_assert(struct device *dev)
856{
857 lockdep_assert_held(&dev->mutex);
858}
859
Benjamin Herrenschmidte8a51e12015-02-17 19:03:41 -0600860static inline struct device_node *dev_of_node(struct device *dev)
861{
Stephen Boyd1b833922019-04-12 11:31:45 -0700862 if (!IS_ENABLED(CONFIG_OF) || !dev)
Benjamin Herrenschmidte8a51e12015-02-17 19:03:41 -0600863 return NULL;
864 return dev->of_node;
865}
866
Saravana Kannanac338ac2020-02-21 00:05:09 -0800867static inline bool dev_has_sync_state(struct device *dev)
868{
869 if (!dev)
870 return false;
871 if (dev->driver && dev->driver->sync_state)
872 return true;
873 if (dev->bus && dev->bus->sync_state)
874 return true;
875 return false;
876}
877
Rajat Jain70f400d2021-05-24 10:18:11 -0700878static inline void dev_set_removable(struct device *dev,
879 enum device_removable removable)
880{
881 dev->removable = removable;
882}
883
884static inline bool dev_is_removable(struct device *dev)
885{
886 return dev->removable == DEVICE_REMOVABLE;
887}
888
889static inline bool dev_removable_is_valid(struct device *dev)
890{
891 return dev->removable != DEVICE_REMOVABLE_NOT_SUPPORTED;
892}
893
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894/*
895 * High level routines for use by the bus drivers
896 */
Bartosz Golaszewski67dd0772020-06-29 08:50:05 +0200897int __must_check device_register(struct device *dev);
898void device_unregister(struct device *dev);
899void device_initialize(struct device *dev);
900int __must_check device_add(struct device *dev);
901void device_del(struct device *dev);
Peter Zijlstra3c6cc62c2023-05-26 12:23:48 +0200902
903DEFINE_FREE(device_del, struct device *, if (_T) device_del(_T))
904
Bartosz Golaszewski67dd0772020-06-29 08:50:05 +0200905int device_for_each_child(struct device *dev, void *data,
906 int (*fn)(struct device *dev, void *data));
907int device_for_each_child_reverse(struct device *dev, void *data,
908 int (*fn)(struct device *dev, void *data));
909struct device *device_find_child(struct device *dev, void *data,
910 int (*match)(struct device *dev, void *data));
911struct device *device_find_child_by_name(struct device *parent,
912 const char *name);
Andy Shevchenko82b070b2022-06-10 15:02:18 +0300913struct device *device_find_any_child(struct device *parent);
914
Bartosz Golaszewski67dd0772020-06-29 08:50:05 +0200915int device_rename(struct device *dev, const char *new_name);
916int device_move(struct device *dev, struct device *new_parent,
917 enum dpm_order dpm_order);
918int device_change_owner(struct device *dev, kuid_t kuid, kgid_t kgid);
919const char *device_get_devnode(struct device *dev, umode_t *mode, kuid_t *uid,
920 kgid_t *gid, const char **tmp);
Linus Torvalds44197792020-08-05 13:02:45 -0700921int device_is_dependent(struct device *dev, void *target);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +0200923static inline bool device_supports_offline(struct device *dev)
924{
925 return dev->bus && dev->bus->offline && dev->bus->online;
926}
927
Dan Williamsd864b8e2022-04-26 12:22:44 -0700928#define __device_lock_set_class(dev, name, key) \
929do { \
930 struct device *__d2 __maybe_unused = dev; \
931 lock_set_class(&__d2->mutex.dep_map, name, key, 0, _THIS_IP_); \
932} while (0)
933
934/**
935 * device_lock_set_class - Specify a temporary lock class while a device
936 * is attached to a driver
937 * @dev: device to modify
938 * @key: lock class key data
939 *
940 * This must be called with the device_lock() already held, for example
941 * from driver ->probe(). Take care to only override the default
942 * lockdep_no_validate class.
943 */
944#ifdef CONFIG_LOCKDEP
945#define device_lock_set_class(dev, key) \
946do { \
947 struct device *__d = dev; \
948 dev_WARN_ONCE(__d, !lockdep_match_class(&__d->mutex, \
949 &__lockdep_no_validate__), \
950 "overriding existing custom lock class\n"); \
951 __device_lock_set_class(__d, #key, key); \
952} while (0)
953#else
954#define device_lock_set_class(dev, key) __device_lock_set_class(dev, #key, key)
955#endif
956
957/**
958 * device_lock_reset_class - Return a device to the default lockdep novalidate state
959 * @dev: device to modify
960 *
961 * This must be called with the device_lock() already held, for example
962 * from driver ->remove().
963 */
964#define device_lock_reset_class(dev) \
965do { \
966 struct device *__d __maybe_unused = dev; \
967 lock_set_novalidate_class(&__d->mutex.dep_map, "&dev->mutex", \
968 _THIS_IP_); \
969} while (0)
970
Bartosz Golaszewski67dd0772020-06-29 08:50:05 +0200971void lock_device_hotplug(void);
972void unlock_device_hotplug(void);
973int lock_device_hotplug_sysfs(void);
974int device_offline(struct device *dev);
975int device_online(struct device *dev);
976void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode);
977void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode);
Johan Hovold4e75e1d2017-06-06 17:59:00 +0200978void device_set_of_node_from_dev(struct device *dev, const struct device *dev2);
Ioana Ciornei43e76d42021-06-17 15:29:04 +0300979void device_set_node(struct device *dev, struct fwnode_handle *fwnode);
Rafael J. Wysocki97badf82015-04-03 23:23:37 +0200980
Phil Sutter9af15c32017-01-18 14:04:39 +0100981static inline int dev_num_vf(struct device *dev)
982{
983 if (dev->bus && dev->bus->num_vf)
984 return dev->bus->num_vf(dev);
985 return 0;
986}
987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988/*
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +0000989 * Root device objects for grouping under /sys/devices
990 */
Bartosz Golaszewski67dd0772020-06-29 08:50:05 +0200991struct device *__root_device_register(const char *name, struct module *owner);
Paul Gortmakereb5589a2011-05-27 09:02:11 -0400992
Tejun Heo33ac1252014-01-10 08:57:31 -0500993/* This is a macro to avoid include problems with THIS_MODULE */
Paul Gortmakereb5589a2011-05-27 09:02:11 -0400994#define root_device_register(name) \
995 __root_device_register(name, THIS_MODULE)
996
Bartosz Golaszewski67dd0772020-06-29 08:50:05 +0200997void root_device_unregister(struct device *root);
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +0000998
Mark Browna5b8b1a2009-07-17 15:06:08 +0100999static inline void *dev_get_platdata(const struct device *dev)
1000{
1001 return dev->platform_data;
1002}
1003
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001004/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 * Manual binding of a device to driver. See drivers/base/bus.c
1006 * for information on use.
1007 */
Jason Gunthorpe0d9f8372021-06-17 16:22:13 +02001008int __must_check device_driver_attach(struct device_driver *drv,
1009 struct device *dev);
Bartosz Golaszewski67dd0772020-06-29 08:50:05 +02001010int __must_check device_bind_driver(struct device *dev);
1011void device_release_driver(struct device *dev);
1012int __must_check device_attach(struct device *dev);
1013int __must_check driver_attach(struct device_driver *drv);
1014void device_initial_probe(struct device *dev);
1015int __must_check device_reprobe(struct device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
Bartosz Golaszewski67dd0772020-06-29 08:50:05 +02001017bool device_is_bound(struct device *dev);
Tomeu Vizoso6b9cb422016-01-07 16:46:12 +01001018
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001019/*
1020 * Easy functions for dynamically creating devices on the fly
1021 */
Bartosz Golaszewski67dd0772020-06-29 08:50:05 +02001022__printf(5, 6) struct device *
1023device_create(struct class *cls, struct device *parent, dev_t devt,
1024 void *drvdata, const char *fmt, ...);
1025__printf(6, 7) struct device *
1026device_create_with_groups(struct class *cls, struct device *parent, dev_t devt,
1027 void *drvdata, const struct attribute_group **groups,
1028 const char *fmt, ...);
1029void device_destroy(struct class *cls, dev_t devt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Bartosz Golaszewski67dd0772020-06-29 08:50:05 +02001031int __must_check device_add_groups(struct device *dev,
1032 const struct attribute_group **groups);
1033void device_remove_groups(struct device *dev,
1034 const struct attribute_group **groups);
Dmitry Torokhova7670d42017-07-19 17:24:31 -07001035
Dmitry Torokhove323b2d2017-07-19 17:24:32 -07001036static inline int __must_check device_add_group(struct device *dev,
1037 const struct attribute_group *grp)
1038{
1039 const struct attribute_group *groups[] = { grp, NULL };
1040
1041 return device_add_groups(dev, groups);
1042}
1043
1044static inline void device_remove_group(struct device *dev,
1045 const struct attribute_group *grp)
1046{
1047 const struct attribute_group *groups[] = { grp, NULL };
1048
1049 return device_remove_groups(dev, groups);
1050}
1051
Bartosz Golaszewski67dd0772020-06-29 08:50:05 +02001052int __must_check devm_device_add_groups(struct device *dev,
Dmitry Torokhov57b8ff02017-07-19 17:24:33 -07001053 const struct attribute_group **groups);
Bartosz Golaszewski67dd0772020-06-29 08:50:05 +02001054void devm_device_remove_groups(struct device *dev,
1055 const struct attribute_group **groups);
1056int __must_check devm_device_add_group(struct device *dev,
1057 const struct attribute_group *grp);
1058void devm_device_remove_group(struct device *dev,
1059 const struct attribute_group *grp);
Dmitry Torokhov57b8ff02017-07-19 17:24:33 -07001060
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061/*
1062 * Platform "fixup" functions - allow the platform to have their say
1063 * about devices and actions that the general device layer doesn't
1064 * know about.
1065 */
1066/* Notify platform of device discovery */
Greg Kroah-Hartmand4629432008-01-24 21:04:46 -08001067extern int (*platform_notify)(struct device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Greg Kroah-Hartmand4629432008-01-24 21:04:46 -08001069extern int (*platform_notify_remove)(struct device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
1071
Wanlong Gao880ffb52011-05-05 07:55:36 +08001072/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 * get_device - atomically increment the reference count for the device.
1074 *
1075 */
Bartosz Golaszewski67dd0772020-06-29 08:50:05 +02001076struct device *get_device(struct device *dev);
1077void put_device(struct device *dev);
Peter Zijlstra3c6cc62c2023-05-26 12:23:48 +02001078
1079DEFINE_FREE(put_device, struct device *, if (_T) put_device(_T))
1080
Bartosz Golaszewski67dd0772020-06-29 08:50:05 +02001081bool kill_device(struct device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
Kay Sievers2b2af542009-04-30 15:23:42 +02001083#ifdef CONFIG_DEVTMPFS
Bartosz Golaszewski67dd0772020-06-29 08:50:05 +02001084int devtmpfs_mount(void);
Kay Sievers2b2af542009-04-30 15:23:42 +02001085#else
Dominik Brodowski5e787dbf2018-10-23 22:10:35 +02001086static inline int devtmpfs_mount(void) { return 0; }
Kay Sievers2b2af542009-04-30 15:23:42 +02001087#endif
1088
Rytchkov Alexey116f232b2006-03-22 00:58:53 +01001089/* drivers/base/power/shutdown.c */
Bartosz Golaszewski67dd0772020-06-29 08:50:05 +02001090void device_shutdown(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092/* debugging and troubleshooting/diagnostic helpers. */
Bartosz Golaszewski67dd0772020-06-29 08:50:05 +02001093const char *dev_driver_string(const struct device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001095/* Device links interface. */
1096struct device_link *device_link_add(struct device *consumer,
1097 struct device *supplier, u32 flags);
1098void device_link_del(struct device_link *link);
pascal pailletd8842212018-07-05 14:25:56 +00001099void device_link_remove(void *consumer, struct device *supplier);
Saravana Kannanfc5a2512019-09-04 14:11:23 -07001100void device_links_supplier_sync_state_pause(void);
1101void device_links_supplier_sync_state_resume(void);
Herve Codina9406d592024-03-25 16:21:25 +01001102void device_link_wait_removal(void);
Joe Perches99bcf212010-06-27 01:02:34 +00001103
Andrzej Hajdaa787e542020-07-13 16:43:21 +02001104extern __printf(3, 4)
Greg Kroah-Hartmanf601e8f2020-09-09 09:37:40 +02001105int dev_err_probe(const struct device *dev, int err, const char *fmt, ...);
Andrzej Hajdaa787e542020-07-13 16:43:21 +02001106
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107/* Create alias, so I can be autoloaded. */
1108#define MODULE_ALIAS_CHARDEV(major,minor) \
1109 MODULE_ALIAS("char-major-" __stringify(major) "-" __stringify(minor))
1110#define MODULE_ALIAS_CHARDEV_MAJOR(major) \
1111 MODULE_ALIAS("char-major-" __stringify(major) "-*")
Andi Kleene52eec132010-09-08 16:54:17 +02001112
1113#ifdef CONFIG_SYSFS_DEPRECATED
1114extern long sysfs_deprecated;
1115#else
1116#define sysfs_deprecated 0
1117#endif
1118
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119#endif /* _DEVICE_H_ */