Greg Kroah-Hartman | 989d42e | 2017-11-07 17:30:07 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * device.h - generic, centralized driver model |
| 4 | * |
| 5 | * Copyright (c) 2001-2003 Patrick Mochel <[email protected]> |
Greg Kroah-Hartman | b402843 | 2009-05-11 14:16:57 -0700 | [diff] [blame] | 6 | * Copyright (c) 2004-2009 Greg Kroah-Hartman <[email protected]> |
| 7 | * Copyright (c) 2008-2009 Novell Inc. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * |
Mauro Carvalho Chehab | fe34c89 | 2019-06-18 12:34:59 -0300 | [diff] [blame] | 9 | * See Documentation/driver-api/driver-model/ for more information. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #ifndef _DEVICE_H_ |
| 13 | #define _DEVICE_H_ |
| 14 | |
Greg Kroah-Hartman | af628aa | 2019-12-09 20:33:00 +0100 | [diff] [blame] | 15 | #include <linux/dev_printk.h> |
Lukasz Luba | 1bc138c | 2020-06-10 11:12:23 +0100 | [diff] [blame] | 16 | #include <linux/energy_model.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/ioport.h> |
| 18 | #include <linux/kobject.h> |
[email protected] | 465c7a3 | 2005-03-21 11:49:14 -0800 | [diff] [blame] | 19 | #include <linux/klist.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/list.h> |
Matthew Wilcox | d2a3b91 | 2008-05-28 09:28:39 -0700 | [diff] [blame] | 21 | #include <linux/lockdep.h> |
Andrew Morton | 4a7fb63 | 2006-08-14 22:43:17 -0700 | [diff] [blame] | 22 | #include <linux/compiler.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/types.h> |
Paul Gortmaker | de47725 | 2011-05-26 13:46:22 -0400 | [diff] [blame] | 24 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/pm.h> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 26 | #include <linux/atomic.h> |
Kay Sievers | 3c2670e | 2013-04-06 09:56:00 -0700 | [diff] [blame] | 27 | #include <linux/uidgid.h> |
Joe Perches | 64c862a8 | 2013-10-11 13:11:38 -0700 | [diff] [blame] | 28 | #include <linux/gfp.h> |
Kees Cook | 2509b56 | 2018-05-08 22:29:52 -0700 | [diff] [blame] | 29 | #include <linux/overflow.h> |
Greg Kroah-Hartman | 5aee2bf | 2019-12-09 20:33:01 +0100 | [diff] [blame] | 30 | #include <linux/device/bus.h> |
Greg Kroah-Hartman | a8ae608 | 2019-12-09 20:33:02 +0100 | [diff] [blame] | 31 | #include <linux/device/class.h> |
Greg Kroah-Hartman | 4c002c9 | 2019-12-09 20:33:03 +0100 | [diff] [blame] | 32 | #include <linux/device/driver.h> |
Peter Zijlstra | 3c6cc62c | 2023-05-26 12:23:48 +0200 | [diff] [blame] | 33 | #include <linux/cleanup.h> |
Benjamin Herrenschmidt | c6dbaef | 2006-11-11 17:18:39 +1100 | [diff] [blame] | 34 | #include <asm/device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | struct device; |
Greg Kroah-Hartman | fb069a5 | 2008-12-16 12:23:36 -0800 | [diff] [blame] | 37 | struct device_private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | struct device_driver; |
Greg Kroah-Hartman | e5dd127 | 2007-11-28 15:59:15 -0800 | [diff] [blame] | 39 | struct driver_private; |
Paul Gortmaker | de47725 | 2011-05-26 13:46:22 -0400 | [diff] [blame] | 40 | struct module; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | struct class; |
Kay Sievers | 6b6e39a | 2010-11-15 23:13:18 +0100 | [diff] [blame] | 42 | struct subsys_private; |
Grant Likely | d706c1b | 2010-04-13 16:12:28 -0700 | [diff] [blame] | 43 | struct device_node; |
Rafael J. Wysocki | ce79348 | 2015-03-16 23:49:03 +0100 | [diff] [blame] | 44 | struct fwnode_handle; |
Joerg Roedel | ff21776 | 2011-08-26 16:48:26 +0200 | [diff] [blame] | 45 | struct iommu_ops; |
Alex Williamson | 74416e1e | 2012-05-30 14:18:41 -0600 | [diff] [blame] | 46 | struct iommu_group; |
Linus Torvalds | 23c35f4 | 2018-02-02 16:44:14 -0800 | [diff] [blame] | 47 | struct dev_pin_info; |
Joerg Roedel | 045a704 | 2020-03-26 16:08:30 +0100 | [diff] [blame] | 48 | struct dev_iommu; |
Thomas Gleixner | 013bd8e | 2021-12-10 23:18:55 +0100 | [diff] [blame] | 49 | struct msi_device_data; |
Kay Sievers | b8c5cec | 2007-02-16 17:33:36 +0100 | [diff] [blame] | 50 | |
Wanlong Gao | 880ffb5 | 2011-05-05 07:55:36 +0800 | [diff] [blame] | 51 | /** |
Kay Sievers | ca22e56 | 2011-12-14 14:29:38 -0800 | [diff] [blame] | 52 | * struct subsys_interface - interfaces to device functions |
Randy Dunlap | 2eda013 | 2012-01-21 11:02:51 -0800 | [diff] [blame] | 53 | * @name: name of the device function |
Bhaskar Chowdhury | 5dd5f93 | 2021-03-21 01:42:40 +0530 | [diff] [blame] | 54 | * @subsys: subsystem of the devices to attach to |
Randy Dunlap | 2eda013 | 2012-01-21 11:02:51 -0800 | [diff] [blame] | 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 |
Kay Sievers | ca22e56 | 2011-12-14 14:29:38 -0800 | [diff] [blame] | 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 | */ |
| 64 | struct 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 Kumar | 71db87b | 2015-07-30 15:04:01 +0530 | [diff] [blame] | 69 | void (*remove_dev)(struct device *dev, struct subsys_interface *sif); |
Kay Sievers | ca22e56 | 2011-12-14 14:29:38 -0800 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | int subsys_interface_register(struct subsys_interface *sif); |
| 73 | void subsys_interface_unregister(struct subsys_interface *sif); |
| 74 | |
| 75 | int subsys_system_register(struct bus_type *subsys, |
| 76 | const struct attribute_group **groups); |
Tejun Heo | d73ce00 | 2013-03-12 11:30:05 -0700 | [diff] [blame] | 77 | int subsys_virtual_register(struct bus_type *subsys, |
| 78 | const struct attribute_group **groups); |
Kay Sievers | ca22e56 | 2011-12-14 14:29:38 -0800 | [diff] [blame] | 79 | |
Kay Sievers | 414264f | 2007-03-12 21:08:57 +0100 | [diff] [blame] | 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 | */ |
Kay Sievers | f9f852d | 2006-10-07 21:54:55 +0200 | [diff] [blame] | 89 | struct device_type { |
Kay Sievers | 414264f | 2007-03-12 21:08:57 +0100 | [diff] [blame] | 90 | const char *name; |
David Brownell | a4dbd67 | 2009-06-24 10:06:31 -0700 | [diff] [blame] | 91 | const struct attribute_group **groups; |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 92 | int (*uevent)(struct device *dev, struct kobj_uevent_env *env); |
Kay Sievers | 3c2670e | 2013-04-06 09:56:00 -0700 | [diff] [blame] | 93 | char *(*devnode)(struct device *dev, umode_t *mode, |
Greg Kroah-Hartman | 4e4098a | 2013-04-11 11:43:29 -0700 | [diff] [blame] | 94 | kuid_t *uid, kgid_t *gid); |
Kay Sievers | f9f852d | 2006-10-07 21:54:55 +0200 | [diff] [blame] | 95 | void (*release)(struct device *dev); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 96 | |
Dmitry Torokhov | 8150f32 | 2009-07-24 22:11:32 -0700 | [diff] [blame] | 97 | const struct dev_pm_ops *pm; |
Kay Sievers | f9f852d | 2006-10-07 21:54:55 +0200 | [diff] [blame] | 98 | }; |
| 99 | |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 100 | /* interface for exporting device attributes */ |
| 101 | struct 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 Sievers | ca22e56 | 2011-12-14 14:29:38 -0800 | [diff] [blame] | 109 | struct dev_ext_attribute { |
| 110 | struct device_attribute attr; |
| 111 | void *var; |
| 112 | }; |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 113 | |
Kay Sievers | ca22e56 | 2011-12-14 14:29:38 -0800 | [diff] [blame] | 114 | ssize_t device_show_ulong(struct device *dev, struct device_attribute *attr, |
| 115 | char *buf); |
| 116 | ssize_t device_store_ulong(struct device *dev, struct device_attribute *attr, |
| 117 | const char *buf, size_t count); |
| 118 | ssize_t device_show_int(struct device *dev, struct device_attribute *attr, |
| 119 | char *buf); |
| 120 | ssize_t device_store_int(struct device *dev, struct device_attribute *attr, |
| 121 | const char *buf, size_t count); |
Borislav Petkov | 9187239 | 2012-10-09 19:52:05 +0200 | [diff] [blame] | 122 | ssize_t device_show_bool(struct device *dev, struct device_attribute *attr, |
| 123 | char *buf); |
| 124 | ssize_t device_store_bool(struct device *dev, struct device_attribute *attr, |
| 125 | const char *buf, size_t count); |
Kay Sievers | ca22e56 | 2011-12-14 14:29:38 -0800 | [diff] [blame] | 126 | |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 127 | #define DEVICE_ATTR(_name, _mode, _show, _store) \ |
Kay Sievers | ca22e56 | 2011-12-14 14:29:38 -0800 | [diff] [blame] | 128 | struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store) |
Christophe Leroy | 7fda910 | 2017-12-18 11:08:29 +0100 | [diff] [blame] | 129 | #define DEVICE_ATTR_PREALLOC(_name, _mode, _show, _store) \ |
| 130 | struct device_attribute dev_attr_##_name = \ |
| 131 | __ATTR_PREALLOC(_name, _mode, _show, _store) |
Greg Kroah-Hartman | ced321b | 2013-07-14 16:05:54 -0700 | [diff] [blame] | 132 | #define DEVICE_ATTR_RW(_name) \ |
| 133 | struct device_attribute dev_attr_##_name = __ATTR_RW(_name) |
Dan Williams | 3022c6a | 2020-06-25 16:51:03 -0700 | [diff] [blame] | 134 | #define DEVICE_ATTR_ADMIN_RW(_name) \ |
| 135 | struct device_attribute dev_attr_##_name = __ATTR_RW_MODE(_name, 0600) |
Greg Kroah-Hartman | ced321b | 2013-07-14 16:05:54 -0700 | [diff] [blame] | 136 | #define DEVICE_ATTR_RO(_name) \ |
| 137 | struct device_attribute dev_attr_##_name = __ATTR_RO(_name) |
Dan Williams | 3022c6a | 2020-06-25 16:51:03 -0700 | [diff] [blame] | 138 | #define DEVICE_ATTR_ADMIN_RO(_name) \ |
| 139 | struct device_attribute dev_attr_##_name = __ATTR_RO_MODE(_name, 0400) |
Greg Kroah-Hartman | 1130c55 | 2013-08-23 15:02:56 -0700 | [diff] [blame] | 140 | #define DEVICE_ATTR_WO(_name) \ |
| 141 | struct device_attribute dev_attr_##_name = __ATTR_WO(_name) |
Kay Sievers | ca22e56 | 2011-12-14 14:29:38 -0800 | [diff] [blame] | 142 | #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 Davidson | 9475818 | 2012-05-03 16:19:02 -0700 | [diff] [blame] | 147 | { __ATTR(_name, _mode, device_show_int, device_store_int), &(_var) } |
Borislav Petkov | 9187239 | 2012-10-09 19:52:05 +0200 | [diff] [blame] | 148 | #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 Stern | 356c05d | 2012-05-14 13:30:03 -0400 | [diff] [blame] | 151 | #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 Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 154 | |
Bartosz Golaszewski | 67dd077 | 2020-06-29 08:50:05 +0200 | [diff] [blame] | 155 | int device_create_file(struct device *device, |
| 156 | const struct device_attribute *entry); |
| 157 | void device_remove_file(struct device *dev, |
| 158 | const struct device_attribute *attr); |
| 159 | bool device_remove_file_self(struct device *dev, |
| 160 | const struct device_attribute *attr); |
| 161 | int __must_check device_create_bin_file(struct device *dev, |
Phil Carmody | 66ecb92 | 2009-12-18 15:34:20 +0200 | [diff] [blame] | 162 | const struct bin_attribute *attr); |
Bartosz Golaszewski | 67dd077 | 2020-06-29 08:50:05 +0200 | [diff] [blame] | 163 | void device_remove_bin_file(struct device *dev, |
| 164 | const struct bin_attribute *attr); |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 165 | |
| 166 | /* device resource management */ |
| 167 | typedef void (*dr_release_t)(struct device *dev, void *res); |
| 168 | typedef int (*dr_match_t)(struct device *dev, void *res, void *match_data); |
| 169 | |
Bartosz Golaszewski | 67dd077 | 2020-06-29 08:50:05 +0200 | [diff] [blame] | 170 | void *__devres_alloc_node(dr_release_t release, size_t size, gfp_t gfp, |
| 171 | int nid, const char *name) __malloc; |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 172 | #define devres_alloc(release, size, gfp) \ |
Dan Williams | 7c68394 | 2015-10-05 20:35:55 -0400 | [diff] [blame] | 173 | __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 Williams | 7c68394 | 2015-10-05 20:35:55 -0400 | [diff] [blame] | 176 | |
Bartosz Golaszewski | 67dd077 | 2020-06-29 08:50:05 +0200 | [diff] [blame] | 177 | void 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); |
| 181 | void devres_free(void *res); |
| 182 | void devres_add(struct device *dev, void *res); |
| 183 | void *devres_find(struct device *dev, dr_release_t release, |
| 184 | dr_match_t match, void *match_data); |
| 185 | void *devres_get(struct device *dev, void *new_res, |
| 186 | dr_match_t match, void *match_data); |
| 187 | void *devres_remove(struct device *dev, dr_release_t release, |
| 188 | dr_match_t match, void *match_data); |
| 189 | int devres_destroy(struct device *dev, dr_release_t release, |
| 190 | dr_match_t match, void *match_data); |
| 191 | int devres_release(struct device *dev, dr_release_t release, |
| 192 | dr_match_t match, void *match_data); |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 193 | |
| 194 | /* devres group */ |
Bartosz Golaszewski | 67dd077 | 2020-06-29 08:50:05 +0200 | [diff] [blame] | 195 | void * __must_check devres_open_group(struct device *dev, void *id, gfp_t gfp); |
| 196 | void devres_close_group(struct device *dev, void *id); |
| 197 | void devres_remove_group(struct device *dev, void *id); |
| 198 | int devres_release_group(struct device *dev, void *id); |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 199 | |
Joe Perches | 64c862a8 | 2013-10-11 13:11:38 -0700 | [diff] [blame] | 200 | /* managed devm_k.alloc/kfree for device drivers */ |
Bartosz Golaszewski | 67dd077 | 2020-06-29 08:50:05 +0200 | [diff] [blame] | 201 | void *devm_kmalloc(struct device *dev, size_t size, gfp_t gfp) __malloc; |
Bartosz Golaszewski | f824857 | 2020-08-24 19:38:57 +0200 | [diff] [blame] | 202 | void *devm_krealloc(struct device *dev, void *ptr, size_t size, |
| 203 | gfp_t gfp) __must_check; |
Bartosz Golaszewski | 67dd077 | 2020-06-29 08:50:05 +0200 | [diff] [blame] | 204 | __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 Perches | 64c862a8 | 2013-10-11 13:11:38 -0700 | [diff] [blame] | 208 | static inline void *devm_kzalloc(struct device *dev, size_t size, gfp_t gfp) |
| 209 | { |
| 210 | return devm_kmalloc(dev, size, gfp | __GFP_ZERO); |
| 211 | } |
| 212 | static inline void *devm_kmalloc_array(struct device *dev, |
| 213 | size_t n, size_t size, gfp_t flags) |
| 214 | { |
Kees Cook | 2509b56 | 2018-05-08 22:29:52 -0700 | [diff] [blame] | 215 | size_t bytes; |
| 216 | |
| 217 | if (unlikely(check_mul_overflow(n, size, &bytes))) |
Joe Perches | 64c862a8 | 2013-10-11 13:11:38 -0700 | [diff] [blame] | 218 | return NULL; |
Kees Cook | 2509b56 | 2018-05-08 22:29:52 -0700 | [diff] [blame] | 219 | |
| 220 | return devm_kmalloc(dev, bytes, flags); |
Joe Perches | 64c862a8 | 2013-10-11 13:11:38 -0700 | [diff] [blame] | 221 | } |
| 222 | static 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 Golaszewski | 67dd077 | 2020-06-29 08:50:05 +0200 | [diff] [blame] | 227 | void devm_kfree(struct device *dev, const void *p); |
| 228 | char *devm_kstrdup(struct device *dev, const char *s, gfp_t gfp) __malloc; |
| 229 | const char *devm_kstrdup_const(struct device *dev, const char *s, gfp_t gfp); |
| 230 | void *devm_kmemdup(struct device *dev, const void *src, size_t len, gfp_t gfp); |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 231 | |
Bartosz Golaszewski | 67dd077 | 2020-06-29 08:50:05 +0200 | [diff] [blame] | 232 | unsigned long devm_get_free_pages(struct device *dev, |
| 233 | gfp_t gfp_mask, unsigned int order); |
| 234 | void devm_free_pages(struct device *dev, unsigned long addr); |
Greg Kroah-Hartman | 2589f188 | 2006-09-19 09:39:19 -0700 | [diff] [blame] | 235 | |
Arnd Bergmann | eef778c | 2019-07-04 15:14:45 -0700 | [diff] [blame] | 236 | void __iomem *devm_ioremap_resource(struct device *dev, |
| 237 | const struct resource *res); |
Bartosz Golaszewski | b873af6 | 2019-10-22 10:43:13 +0200 | [diff] [blame] | 238 | void __iomem *devm_ioremap_resource_wc(struct device *dev, |
| 239 | const struct resource *res); |
Wolfram Sang | 72f8c0b | 2011-10-25 15:16:47 +0200 | [diff] [blame] | 240 | |
Benjamin Herrenschmidt | d5e8382 | 2018-06-05 13:21:26 +1000 | [diff] [blame] | 241 | void __iomem *devm_of_iomap(struct device *dev, |
| 242 | struct device_node *node, int index, |
| 243 | resource_size_t *size); |
| 244 | |
Dmitry Torokhov | d6b0c58 | 2013-02-23 13:11:14 -0800 | [diff] [blame] | 245 | /* allows to add/remove a custom action to devres stack */ |
| 246 | int devm_add_action(struct device *dev, void (*action)(void *), void *data); |
| 247 | void devm_remove_action(struct device *dev, void (*action)(void *), void *data); |
Dan Williams | 2374b68 | 2019-06-13 15:56:18 -0700 | [diff] [blame] | 248 | void devm_release_action(struct device *dev, void (*action)(void *), void *data); |
Dmitry Torokhov | d6b0c58 | 2013-02-23 13:11:14 -0800 | [diff] [blame] | 249 | |
Sudip Mukherjee | a3499e9 | 2015-12-23 17:57:19 +0530 | [diff] [blame] | 250 | static 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 Bucur | ff86aae | 2016-11-15 10:41:01 +0200 | [diff] [blame] | 262 | /** |
| 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 | |
| 277 | void __percpu *__devm_alloc_percpu(struct device *dev, size_t size, |
| 278 | size_t align); |
| 279 | void devm_free_percpu(struct device *dev, void __percpu *pdata); |
| 280 | |
FUJITA Tomonori | 6b7b651 | 2008-02-04 22:27:55 -0800 | [diff] [blame] | 281 | struct 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 Gao | 36950f2 | 2021-02-01 10:30:15 -0800 | [diff] [blame] | 287 | unsigned int min_align_mask; |
FUJITA Tomonori | 6b7b651 | 2008-02-04 22:27:55 -0800 | [diff] [blame] | 288 | unsigned long segment_boundary_mask; |
| 289 | }; |
| 290 | |
Heikki Krogerus | cd7753d | 2018-09-20 14:23:40 +0300 | [diff] [blame] | 291 | /** |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 292 | * 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 | */ |
| 300 | enum 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. Wysocki | 515db26 | 2019-07-16 17:21:06 +0200 | [diff] [blame] | 312 | * STATELESS: The core will not remove this link automatically. |
Vivek Gautam | e88728f | 2018-06-27 18:20:55 +0530 | [diff] [blame] | 313 | * AUTOREMOVE_CONSUMER: Remove the link automatically on consumer driver unbind. |
Rafael J. Wysocki | 21d5c57 | 2016-10-30 17:32:31 +0100 | [diff] [blame] | 314 | * 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 Gautam | 1689cac | 2018-06-27 18:20:56 +0530 | [diff] [blame] | 316 | * AUTOREMOVE_SUPPLIER: Remove the link automatically on supplier driver unbind. |
Rafael J. Wysocki | e7dd401 | 2019-02-01 01:59:42 +0100 | [diff] [blame] | 317 | * AUTOPROBE_CONSUMER: Probe consumer driver automatically after supplier binds. |
Rafael J. Wysocki | 515db26 | 2019-07-16 17:21:06 +0200 | [diff] [blame] | 318 | * MANAGED: The core tracks presence of supplier/consumer drivers (internal). |
Saravana Kannan | 05ef983 | 2019-10-28 15:00:22 -0700 | [diff] [blame] | 319 | * SYNC_STATE_ONLY: Link only affects sync_state() behavior. |
Saravana Kannan | 4b9bbb2 | 2020-12-17 19:17:00 -0800 | [diff] [blame] | 320 | * INFERRED: Inferred from data (eg: firmware) and not from driver actions. |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 321 | */ |
Vivek Gautam | e88728f | 2018-06-27 18:20:55 +0530 | [diff] [blame] | 322 | #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 Gautam | 1689cac | 2018-06-27 18:20:56 +0530 | [diff] [blame] | 326 | #define DL_FLAG_AUTOREMOVE_SUPPLIER BIT(4) |
Rafael J. Wysocki | e7dd401 | 2019-02-01 01:59:42 +0100 | [diff] [blame] | 327 | #define DL_FLAG_AUTOPROBE_CONSUMER BIT(5) |
Rafael J. Wysocki | 515db26 | 2019-07-16 17:21:06 +0200 | [diff] [blame] | 328 | #define DL_FLAG_MANAGED BIT(6) |
Saravana Kannan | 05ef983 | 2019-10-28 15:00:22 -0700 | [diff] [blame] | 329 | #define DL_FLAG_SYNC_STATE_ONLY BIT(7) |
Saravana Kannan | 4b9bbb2 | 2020-12-17 19:17:00 -0800 | [diff] [blame] | 330 | #define DL_FLAG_INFERRED BIT(8) |
Saravana Kannan | 2455b81 | 2023-02-06 17:41:57 -0800 | [diff] [blame] | 331 | #define DL_FLAG_CYCLE BIT(9) |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 332 | |
| 333 | /** |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 334 | * 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 | */ |
| 340 | enum 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 Jain | 70f400d | 2021-05-24 10:18:11 -0700 | [diff] [blame] | 348 | * 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 | */ |
| 356 | enum 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. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 364 | * 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 Kannan | 3b052a3 | 2020-11-20 18:02:17 -0800 | [diff] [blame] | 367 | * @defer_sync: Hook to global list of devices that have deferred sync_state. |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 368 | * @status: Driver status information. |
| 369 | */ |
| 370 | struct dev_links_info { |
| 371 | struct list_head suppliers; |
| 372 | struct list_head consumers; |
Saravana Kannan | 3b052a3 | 2020-11-20 18:02:17 -0800 | [diff] [blame] | 373 | struct list_head defer_sync; |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 374 | enum dl_dev_state status; |
| 375 | }; |
| 376 | |
| 377 | /** |
Thomas Gleixner | 34fff628 | 2021-12-10 23:18:54 +0100 | [diff] [blame] | 378 | * struct dev_msi_info - Device data related to MSI |
| 379 | * @domain: The MSI interrupt domain associated to the device |
Thomas Gleixner | 013bd8e | 2021-12-10 23:18:55 +0100 | [diff] [blame] | 380 | * @data: Pointer to MSI device data |
Thomas Gleixner | 34fff628 | 2021-12-10 23:18:54 +0100 | [diff] [blame] | 381 | */ |
| 382 | struct dev_msi_info { |
| 383 | #ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN |
| 384 | struct irq_domain *domain; |
| 385 | #endif |
Thomas Gleixner | 013bd8e | 2021-12-10 23:18:55 +0100 | [diff] [blame] | 386 | #ifdef CONFIG_GENERIC_MSI_IRQ |
| 387 | struct msi_device_data *data; |
| 388 | #endif |
Thomas Gleixner | 34fff628 | 2021-12-10 23:18:54 +0100 | [diff] [blame] | 389 | }; |
| 390 | |
| 391 | /** |
Won Chung | 6423d29 | 2022-03-14 19:54:58 +0000 | [diff] [blame] | 392 | * 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 | */ |
| 402 | enum 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 | */ |
| 419 | enum 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 | */ |
| 432 | enum 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 | */ |
| 452 | struct 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 Gao | 880ffb5 | 2011-05-05 07:55:36 +0800 | [diff] [blame] | 461 | * 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 Gao | 880ffb5 | 2011-05-05 07:55:36 +0800 | [diff] [blame] | 474 | * @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 Delvare | 1bb6c08 | 2014-04-14 12:54:47 +0200 | [diff] [blame] | 484 | * @driver_data: Private pointer for driver specific info. |
Lukas Wunner | 64df114 | 2016-12-04 13:10:04 +0100 | [diff] [blame] | 485 | * @links: Links to suppliers and consumers of this device. |
Wanlong Gao | 880ffb5 | 2011-05-05 07:55:36 +0800 | [diff] [blame] | 486 | * @power: For device power management. |
Geert Uytterhoeven | 74378c5 | 2017-09-05 20:16:27 +0200 | [diff] [blame] | 487 | * See Documentation/driver-api/pm/devices.rst for details. |
Rafael J. Wysocki | 564b905 | 2011-06-23 01:52:55 +0200 | [diff] [blame] | 488 | * @pm_domain: Provide callbacks that are executed during system suspend, |
Wanlong Gao | 880ffb5 | 2011-05-05 07:55:36 +0800 | [diff] [blame] | 489 | * hibernation, system resume and during runtime PM transitions |
| 490 | * along with subsystem-level and driver-level callbacks. |
Randy Dunlap | 95035ea | 2020-09-06 20:42:52 -0700 | [diff] [blame] | 491 | * @em_pd: device's energy model performance domain |
Linus Walleij | ab78029 | 2013-01-22 10:56:14 -0700 | [diff] [blame] | 492 | * @pins: For device pin management. |
Mauro Carvalho Chehab | 4b0c994 | 2021-05-19 10:51:42 +0200 | [diff] [blame] | 493 | * See Documentation/driver-api/pin-control.rst for details. |
Thomas Gleixner | 34fff628 | 2021-12-10 23:18:54 +0100 | [diff] [blame] | 494 | * @msi: MSI related data |
Wanlong Gao | 880ffb5 | 2011-05-05 07:55:36 +0800 | [diff] [blame] | 495 | * @numa_node: NUMA node this device is close to. |
Jonathan Corbet | 6a7a817 | 2017-08-24 16:09:10 -0600 | [diff] [blame] | 496 | * @dma_ops: DMA mapping operations for this device. |
Wanlong Gao | 880ffb5 | 2011-05-05 07:55:36 +0800 | [diff] [blame] | 497 | * @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 Julienne | a7ba70f | 2019-11-21 10:26:44 +0100 | [diff] [blame] | 501 | * @bus_dma_limit: Limit of an upstream bridge or bus which imposes a smaller |
| 502 | * DMA limit than the device itself supports. |
Jim Quinlan | e0d0727 | 2020-09-17 18:43:40 +0200 | [diff] [blame] | 503 | * @dma_range_map: map for DMA memory ranges relative to that of RAM |
Wanlong Gao | 880ffb5 | 2011-05-05 07:55:36 +0800 | [diff] [blame] | 504 | * @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 Opdenacker | bfd63cd | 2013-06-26 05:57:06 +0200 | [diff] [blame] | 508 | * @cma_area: Contiguous memory area for dma allocations |
Claire Chang | 69031f5 | 2021-06-19 11:40:34 +0800 | [diff] [blame] | 509 | * @dma_io_tlb_mem: Pointer to the swiotlb pool used. Not for driver use. |
Wanlong Gao | 880ffb5 | 2011-05-05 07:55:36 +0800 | [diff] [blame] | 510 | * @archdata: For arch-specific additions. |
| 511 | * @of_node: Associated device tree node. |
Rafael J. Wysocki | ce79348 | 2015-03-16 23:49:03 +0100 | [diff] [blame] | 512 | * @fwnode: Associated device node supplied by platform firmware. |
Wanlong Gao | 880ffb5 | 2011-05-05 07:55:36 +0800 | [diff] [blame] | 513 | * @devt: For creating the sysfs "dev". |
Randy Dunlap | 2eda013 | 2012-01-21 11:02:51 -0800 | [diff] [blame] | 514 | * @id: device instance |
Wanlong Gao | 880ffb5 | 2011-05-05 07:55:36 +0800 | [diff] [blame] | 515 | * @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 Opdenacker | bfd63cd | 2013-06-26 05:57:06 +0200 | [diff] [blame] | 523 | * @iommu_group: IOMMU group the device belongs to. |
Joerg Roedel | 045a704 | 2020-03-26 16:08:30 +0100 | [diff] [blame] | 524 | * @iommu: Per device generic IOMMU runtime data |
Won Chung | 6423d29 | 2022-03-14 19:54:58 +0000 | [diff] [blame] | 525 | * @physical_location: Describes physical location of the device connection |
| 526 | * point in the system housing. |
Rajat Jain | 70f400d | 2021-05-24 10:18:11 -0700 | [diff] [blame] | 527 | * @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 Gao | 880ffb5 | 2011-05-05 07:55:36 +0800 | [diff] [blame] | 530 | * |
Rafael J. Wysocki | 4f3549d | 2013-05-02 22:15:29 +0200 | [diff] [blame] | 531 | * @offline_disabled: If set, the device is permanently online. |
| 532 | * @offline: Set after successful invocation of bus type's .offline(). |
Johan Hovold | 4e75e1d | 2017-06-06 17:59:00 +0200 | [diff] [blame] | 533 | * @of_node_reused: Set if the device-tree node is shared with an ancestor |
| 534 | * device. |
Saravana Kannan | fc5a251 | 2019-09-04 14:11:23 -0700 | [diff] [blame] | 535 | * @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 Kannan | f2db85b | 2021-03-02 13:11:30 -0800 | [diff] [blame] | 538 | * @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 Hellwig | f3ecc0f | 2018-08-19 14:53:20 +0200 | [diff] [blame] | 541 | * @dma_coherent: this particular device is dma coherent, even if the |
| 542 | * architecture supports non-coherent devices. |
Christoph Hellwig | d35834c | 2020-03-23 18:19:30 +0100 | [diff] [blame] | 543 | * @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 Gao | 880ffb5 | 2011-05-05 07:55:36 +0800 | [diff] [blame] | 548 | * |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | struct device { |
Greg Kroah-Hartman | 159ef31e | 2019-02-26 15:32:29 +0100 | [diff] [blame] | 558 | struct kobject kobj; |
David Brownell | 49a4ec1 | 2007-05-08 00:29:39 -0700 | [diff] [blame] | 559 | struct device *parent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | |
Greg Kroah-Hartman | fb069a5 | 2008-12-16 12:23:36 -0800 | [diff] [blame] | 561 | struct device_private *p; |
| 562 | |
Greg Kroah-Hartman | c906a48 | 2008-05-30 10:45:12 -0700 | [diff] [blame] | 563 | const char *init_name; /* initial name of the device */ |
Stephen Hemminger | aed65af | 2011-03-28 09:12:52 -0700 | [diff] [blame] | 564 | const struct device_type *type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | |
Greg Kroah-Hartman | d462943 | 2008-01-24 21:04:46 -0800 | [diff] [blame] | 566 | struct bus_type *bus; /* type of bus device is on */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | struct device_driver *driver; /* which driver has allocated this |
| 568 | device */ |
Greg Kroah-Hartman | e67c856 | 2009-03-08 23:13:32 +0800 | [diff] [blame] | 569 | void *platform_data; /* Platform specific data, device |
| 570 | core doesn't touch it */ |
Jean Delvare | 1bb6c08 | 2014-04-14 12:54:47 +0200 | [diff] [blame] | 571 | void *driver_data; /* Driver data, set and get with |
David Engraf | 2c6f4fc | 2019-02-05 13:19:52 +0100 | [diff] [blame] | 572 | dev_set_drvdata/dev_get_drvdata */ |
Greg Kroah-Hartman | 159ef31e | 2019-02-26 15:32:29 +0100 | [diff] [blame] | 573 | struct mutex mutex; /* mutex to synchronize calls to |
| 574 | * its driver. |
| 575 | */ |
| 576 | |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 577 | struct dev_links_info links; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | struct dev_pm_info power; |
Rafael J. Wysocki | 564b905 | 2011-06-23 01:52:55 +0200 | [diff] [blame] | 579 | struct dev_pm_domain *pm_domain; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | |
Lukasz Luba | 1bc138c | 2020-06-10 11:12:23 +0100 | [diff] [blame] | 581 | #ifdef CONFIG_ENERGY_MODEL |
| 582 | struct em_perf_domain *em_pd; |
| 583 | #endif |
| 584 | |
Linus Walleij | ab78029 | 2013-01-22 10:56:14 -0700 | [diff] [blame] | 585 | #ifdef CONFIG_PINCTRL |
| 586 | struct dev_pin_info *pins; |
| 587 | #endif |
Thomas Gleixner | 34fff628 | 2021-12-10 23:18:54 +0100 | [diff] [blame] | 588 | struct dev_msi_info msi; |
Christoph Hellwig | 2f9237d | 2020-07-08 09:30:00 +0200 | [diff] [blame] | 589 | #ifdef CONFIG_DMA_OPS |
Bart Van Assche | 5657933 | 2017-01-20 13:04:02 -0800 | [diff] [blame] | 590 | const struct dma_map_ops *dma_ops; |
Christoph Hellwig | 2f9237d | 2020-07-08 09:30:00 +0200 | [diff] [blame] | 591 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | 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 Julienne | a7ba70f | 2019-11-21 10:26:44 +0100 | [diff] [blame] | 598 | u64 bus_dma_limit; /* upstream dma constraint */ |
Jim Quinlan | e0d0727 | 2020-09-17 18:43:40 +0200 | [diff] [blame] | 599 | const struct bus_dma_region *dma_range_map; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | |
FUJITA Tomonori | 6b7b651 | 2008-02-04 22:27:55 -0800 | [diff] [blame] | 601 | struct device_dma_parameters *dma_parms; |
| 602 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | struct list_head dma_pools; /* dma pools (if dma'ble) */ |
| 604 | |
Christoph Hellwig | ff4c25f | 2019-02-03 20:12:02 +0100 | [diff] [blame] | 605 | #ifdef CONFIG_DMA_DECLARE_COHERENT |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | struct dma_coherent_mem *dma_mem; /* internal for coherent mem |
| 607 | override */ |
Christoph Hellwig | 2b28129 | 2018-12-25 14:03:32 +0100 | [diff] [blame] | 608 | #endif |
Marek Szyprowski | a254738 | 2013-07-29 14:31:45 +0200 | [diff] [blame] | 609 | #ifdef CONFIG_DMA_CMA |
Marek Szyprowski | c64be2b | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 610 | struct cma *cma_area; /* contiguous memory area for dma |
| 611 | allocations */ |
| 612 | #endif |
Claire Chang | 69031f5 | 2021-06-19 11:40:34 +0800 | [diff] [blame] | 613 | #ifdef CONFIG_SWIOTLB |
| 614 | struct io_tlb_mem *dma_io_tlb_mem; |
| 615 | #endif |
Benjamin Herrenschmidt | c6dbaef | 2006-11-11 17:18:39 +1100 | [diff] [blame] | 616 | /* arch specific additions */ |
| 617 | struct dev_archdata archdata; |
Grant Likely | c9e358d | 2011-01-21 09:24:48 -0700 | [diff] [blame] | 618 | |
| 619 | struct device_node *of_node; /* associated device tree node */ |
Rafael J. Wysocki | ce79348 | 2015-03-16 23:49:03 +0100 | [diff] [blame] | 620 | struct fwnode_handle *fwnode; /* firmware device node */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | |
Greg Kroah-Hartman | 159ef31e | 2019-02-26 15:32:29 +0100 | [diff] [blame] | 622 | #ifdef CONFIG_NUMA |
| 623 | int numa_node; /* NUMA node this device is close to */ |
| 624 | #endif |
Matthew Wilcox | 929d2fa | 2008-10-16 15:51:35 -0600 | [diff] [blame] | 625 | dev_t devt; /* dev_t, creates the sysfs "dev" */ |
Kay Sievers | ca22e56 | 2011-12-14 14:29:38 -0800 | [diff] [blame] | 626 | u32 id; /* device instance */ |
Matthew Wilcox | 929d2fa | 2008-10-16 15:51:35 -0600 | [diff] [blame] | 627 | |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 628 | spinlock_t devres_lock; |
| 629 | struct list_head devres_head; |
| 630 | |
Kay Sievers | b7a3e81 | 2006-10-07 21:54:55 +0200 | [diff] [blame] | 631 | struct class *class; |
David Brownell | a4dbd67 | 2009-06-24 10:06:31 -0700 | [diff] [blame] | 632 | const struct attribute_group **groups; /* optional groups */ |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 633 | |
Greg Kroah-Hartman | d462943 | 2008-01-24 21:04:46 -0800 | [diff] [blame] | 634 | void (*release)(struct device *dev); |
Alex Williamson | 74416e1e | 2012-05-30 14:18:41 -0600 | [diff] [blame] | 635 | struct iommu_group *iommu_group; |
Joerg Roedel | 045a704 | 2020-03-26 16:08:30 +0100 | [diff] [blame] | 636 | struct dev_iommu *iommu; |
Rafael J. Wysocki | 4f3549d | 2013-05-02 22:15:29 +0200 | [diff] [blame] | 637 | |
Won Chung | 6423d29 | 2022-03-14 19:54:58 +0000 | [diff] [blame] | 638 | struct device_physical_location *physical_location; |
| 639 | |
Rajat Jain | 70f400d | 2021-05-24 10:18:11 -0700 | [diff] [blame] | 640 | enum device_removable removable; |
| 641 | |
Rafael J. Wysocki | 4f3549d | 2013-05-02 22:15:29 +0200 | [diff] [blame] | 642 | bool offline_disabled:1; |
| 643 | bool offline:1; |
Johan Hovold | 4e75e1d | 2017-06-06 17:59:00 +0200 | [diff] [blame] | 644 | bool of_node_reused:1; |
Saravana Kannan | fc5a251 | 2019-09-04 14:11:23 -0700 | [diff] [blame] | 645 | bool state_synced:1; |
Saravana Kannan | f2db85b | 2021-03-02 13:11:30 -0800 | [diff] [blame] | 646 | bool can_match:1; |
Christoph Hellwig | f3ecc0f | 2018-08-19 14:53:20 +0200 | [diff] [blame] | 647 | #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 Hellwig | d35834c | 2020-03-23 18:19:30 +0100 | [diff] [blame] | 652 | #ifdef CONFIG_DMA_OPS_BYPASS |
| 653 | bool dma_ops_bypass : 1; |
| 654 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | }; |
| 656 | |
Saravana Kannan | 287905e | 2020-05-21 12:17:58 -0700 | [diff] [blame] | 657 | /** |
| 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. Wysocki | 80dd33c | 2021-05-14 14:10:15 +0200 | [diff] [blame] | 668 | * @rm_work: Work structure used for removing the link. |
Saravana Kannan | 287905e | 2020-05-21 12:17:58 -0700 | [diff] [blame] | 669 | * @supplier_preactivated: Supplier has been made active before consumer probe. |
| 670 | */ |
| 671 | struct 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. Wysocki | 80dd33c | 2021-05-14 14:10:15 +0200 | [diff] [blame] | 681 | struct work_struct rm_work; |
Saravana Kannan | 287905e | 2020-05-21 12:17:58 -0700 | [diff] [blame] | 682 | bool supplier_preactivated; /* Owned by consumer probe. */ |
| 683 | }; |
| 684 | |
Lars-Peter Clausen | a423296 | 2012-07-03 18:49:35 +0200 | [diff] [blame] | 685 | static inline struct device *kobj_to_dev(struct kobject *kobj) |
| 686 | { |
| 687 | return container_of(kobj, struct device, kobj); |
| 688 | } |
| 689 | |
Joerg Roedel | dbba197 | 2018-11-30 12:51:52 +0100 | [diff] [blame] | 690 | /** |
| 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 | */ |
| 695 | static inline bool device_iommu_mapped(struct device *dev) |
| 696 | { |
| 697 | return (dev->iommu_group != NULL); |
| 698 | } |
| 699 | |
Alan Stern | 9a3df1f | 2008-03-19 22:39:13 +0100 | [diff] [blame] | 700 | /* Get the wakeup routines, which depend on struct device */ |
| 701 | #include <linux/pm_wakeup.h> |
| 702 | |
Jean Delvare | bf9ca69 | 2008-07-30 12:29:21 -0700 | [diff] [blame] | 703 | static inline const char *dev_name(const struct device *dev) |
Kay Sievers | 0691663 | 2008-05-02 06:02:41 +0200 | [diff] [blame] | 704 | { |
Paul Mundt | a636ee7f | 2010-03-09 06:57:53 +0000 | [diff] [blame] | 705 | /* Use the init name until the kobject becomes available */ |
| 706 | if (dev->init_name) |
| 707 | return dev->init_name; |
| 708 | |
Kay Sievers | 1fa5ae8 | 2009-01-25 15:17:37 +0100 | [diff] [blame] | 709 | return kobject_name(&dev->kobj); |
Kay Sievers | 0691663 | 2008-05-02 06:02:41 +0200 | [diff] [blame] | 710 | } |
| 711 | |
Saravana Kannan | e020ff6 | 2021-01-10 09:54:07 -0800 | [diff] [blame] | 712 | /** |
| 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 | */ |
| 719 | static 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 Golaszewski | 67dd077 | 2020-06-29 08:50:05 +0200 | [diff] [blame] | 724 | __printf(2, 3) int dev_set_name(struct device *dev, const char *name, ...); |
Stephen Rothwell | 413c239 | 2008-05-30 10:16:40 +1000 | [diff] [blame] | 725 | |
Christoph Hellwig | 8734813 | 2006-12-06 20:32:33 -0800 | [diff] [blame] | 726 | #ifdef CONFIG_NUMA |
| 727 | static inline int dev_to_node(struct device *dev) |
| 728 | { |
| 729 | return dev->numa_node; |
| 730 | } |
| 731 | static inline void set_dev_node(struct device *dev, int node) |
| 732 | { |
| 733 | dev->numa_node = node; |
| 734 | } |
| 735 | #else |
| 736 | static inline int dev_to_node(struct device *dev) |
| 737 | { |
Anshuman Khandual | 98fa15f | 2019-03-05 15:42:58 -0800 | [diff] [blame] | 738 | return NUMA_NO_NODE; |
Christoph Hellwig | 8734813 | 2006-12-06 20:32:33 -0800 | [diff] [blame] | 739 | } |
| 740 | static inline void set_dev_node(struct device *dev, int node) |
| 741 | { |
| 742 | } |
| 743 | #endif |
| 744 | |
Marc Zyngier | f1421db | 2015-07-28 14:46:10 +0100 | [diff] [blame] | 745 | static inline struct irq_domain *dev_get_msi_domain(const struct device *dev) |
| 746 | { |
| 747 | #ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN |
Thomas Gleixner | 34fff628 | 2021-12-10 23:18:54 +0100 | [diff] [blame] | 748 | return dev->msi.domain; |
Marc Zyngier | f1421db | 2015-07-28 14:46:10 +0100 | [diff] [blame] | 749 | #else |
| 750 | return NULL; |
| 751 | #endif |
| 752 | } |
| 753 | |
| 754 | static inline void dev_set_msi_domain(struct device *dev, struct irq_domain *d) |
| 755 | { |
| 756 | #ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN |
Thomas Gleixner | 34fff628 | 2021-12-10 23:18:54 +0100 | [diff] [blame] | 757 | dev->msi.domain = d; |
Marc Zyngier | f1421db | 2015-07-28 14:46:10 +0100 | [diff] [blame] | 758 | #endif |
| 759 | } |
| 760 | |
Jean Delvare | a996d01 | 2014-04-14 12:58:53 +0200 | [diff] [blame] | 761 | static inline void *dev_get_drvdata(const struct device *dev) |
| 762 | { |
| 763 | return dev->driver_data; |
| 764 | } |
| 765 | |
| 766 | static inline void dev_set_drvdata(struct device *dev, void *data) |
| 767 | { |
| 768 | dev->driver_data = data; |
| 769 | } |
| 770 | |
Rafael J. Wysocki | 5c095a0 | 2011-08-25 15:33:50 +0200 | [diff] [blame] | 771 | static inline struct pm_subsys_data *dev_to_psd(struct device *dev) |
| 772 | { |
| 773 | return dev ? dev->power.subsys_data : NULL; |
| 774 | } |
| 775 | |
Ming Lei | f67f129 | 2009-03-01 21:10:49 +0800 | [diff] [blame] | 776 | static inline unsigned int dev_get_uevent_suppress(const struct device *dev) |
| 777 | { |
| 778 | return dev->kobj.uevent_suppress; |
| 779 | } |
| 780 | |
| 781 | static inline void dev_set_uevent_suppress(struct device *dev, int val) |
| 782 | { |
| 783 | dev->kobj.uevent_suppress = val; |
| 784 | } |
| 785 | |
Daniel Ritz | d305ef5 | 2005-09-22 00:47:24 -0700 | [diff] [blame] | 786 | static inline int device_is_registered(struct device *dev) |
| 787 | { |
Greg Kroah-Hartman | 3f62e57 | 2008-03-13 17:07:03 -0400 | [diff] [blame] | 788 | return dev->kobj.state_in_sysfs; |
Daniel Ritz | d305ef5 | 2005-09-22 00:47:24 -0700 | [diff] [blame] | 789 | } |
| 790 | |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 791 | static inline void device_enable_async_suspend(struct device *dev) |
| 792 | { |
Alan Stern | f76b168b | 2011-06-18 20:22:23 +0200 | [diff] [blame] | 793 | if (!dev->power.is_prepared) |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 794 | dev->power.async_suspend = true; |
| 795 | } |
| 796 | |
Rafael J. Wysocki | 5a2eb85 | 2010-01-23 22:25:23 +0100 | [diff] [blame] | 797 | static inline void device_disable_async_suspend(struct device *dev) |
| 798 | { |
Alan Stern | f76b168b | 2011-06-18 20:22:23 +0200 | [diff] [blame] | 799 | if (!dev->power.is_prepared) |
Rafael J. Wysocki | 5a2eb85 | 2010-01-23 22:25:23 +0100 | [diff] [blame] | 800 | dev->power.async_suspend = false; |
| 801 | } |
| 802 | |
| 803 | static inline bool device_async_suspend_enabled(struct device *dev) |
| 804 | { |
| 805 | return !!dev->power.async_suspend; |
| 806 | } |
| 807 | |
Sudeep Holla | 85945c28 | 2019-02-14 18:29:10 +0000 | [diff] [blame] | 808 | static inline bool device_pm_not_required(struct device *dev) |
| 809 | { |
| 810 | return dev->power.no_pm; |
| 811 | } |
| 812 | |
| 813 | static inline void device_set_pm_not_required(struct device *dev) |
| 814 | { |
| 815 | dev->power.no_pm = true; |
| 816 | } |
| 817 | |
Rafael J. Wysocki | feb70af | 2012-08-13 14:00:25 +0200 | [diff] [blame] | 818 | static 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. Wysocki | 08810a41 | 2017-10-25 14:12:29 +0200 | [diff] [blame] | 825 | static inline void dev_pm_set_driver_flags(struct device *dev, u32 flags) |
| 826 | { |
| 827 | dev->power.driver_flags = flags; |
| 828 | } |
| 829 | |
| 830 | static inline bool dev_pm_test_driver_flags(struct device *dev, u32 flags) |
| 831 | { |
| 832 | return !!(dev->power.driver_flags & flags); |
| 833 | } |
| 834 | |
Greg Kroah-Hartman | 8e9394c | 2010-02-17 10:57:05 -0800 | [diff] [blame] | 835 | static inline void device_lock(struct device *dev) |
| 836 | { |
Thomas Gleixner | 3142788 | 2010-01-29 20:39:02 +0000 | [diff] [blame] | 837 | mutex_lock(&dev->mutex); |
Greg Kroah-Hartman | 8e9394c | 2010-02-17 10:57:05 -0800 | [diff] [blame] | 838 | } |
| 839 | |
Oliver Neukum | 7dd9cba | 2016-01-21 15:18:47 +0100 | [diff] [blame] | 840 | static inline int device_lock_interruptible(struct device *dev) |
| 841 | { |
| 842 | return mutex_lock_interruptible(&dev->mutex); |
| 843 | } |
| 844 | |
Greg Kroah-Hartman | 8e9394c | 2010-02-17 10:57:05 -0800 | [diff] [blame] | 845 | static inline int device_trylock(struct device *dev) |
| 846 | { |
Thomas Gleixner | 3142788 | 2010-01-29 20:39:02 +0000 | [diff] [blame] | 847 | return mutex_trylock(&dev->mutex); |
Greg Kroah-Hartman | 8e9394c | 2010-02-17 10:57:05 -0800 | [diff] [blame] | 848 | } |
| 849 | |
| 850 | static inline void device_unlock(struct device *dev) |
| 851 | { |
Thomas Gleixner | 3142788 | 2010-01-29 20:39:02 +0000 | [diff] [blame] | 852 | mutex_unlock(&dev->mutex); |
Greg Kroah-Hartman | 8e9394c | 2010-02-17 10:57:05 -0800 | [diff] [blame] | 853 | } |
| 854 | |
Konrad Rzeszutek Wilk | ac80102 | 2014-12-03 16:40:27 -0500 | [diff] [blame] | 855 | static inline void device_lock_assert(struct device *dev) |
| 856 | { |
| 857 | lockdep_assert_held(&dev->mutex); |
| 858 | } |
| 859 | |
Benjamin Herrenschmidt | e8a51e1 | 2015-02-17 19:03:41 -0600 | [diff] [blame] | 860 | static inline struct device_node *dev_of_node(struct device *dev) |
| 861 | { |
Stephen Boyd | 1b83392 | 2019-04-12 11:31:45 -0700 | [diff] [blame] | 862 | if (!IS_ENABLED(CONFIG_OF) || !dev) |
Benjamin Herrenschmidt | e8a51e1 | 2015-02-17 19:03:41 -0600 | [diff] [blame] | 863 | return NULL; |
| 864 | return dev->of_node; |
| 865 | } |
| 866 | |
Saravana Kannan | ac338ac | 2020-02-21 00:05:09 -0800 | [diff] [blame] | 867 | static 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 Jain | 70f400d | 2021-05-24 10:18:11 -0700 | [diff] [blame] | 878 | static inline void dev_set_removable(struct device *dev, |
| 879 | enum device_removable removable) |
| 880 | { |
| 881 | dev->removable = removable; |
| 882 | } |
| 883 | |
| 884 | static inline bool dev_is_removable(struct device *dev) |
| 885 | { |
| 886 | return dev->removable == DEVICE_REMOVABLE; |
| 887 | } |
| 888 | |
| 889 | static inline bool dev_removable_is_valid(struct device *dev) |
| 890 | { |
| 891 | return dev->removable != DEVICE_REMOVABLE_NOT_SUPPORTED; |
| 892 | } |
| 893 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | /* |
| 895 | * High level routines for use by the bus drivers |
| 896 | */ |
Bartosz Golaszewski | 67dd077 | 2020-06-29 08:50:05 +0200 | [diff] [blame] | 897 | int __must_check device_register(struct device *dev); |
| 898 | void device_unregister(struct device *dev); |
| 899 | void device_initialize(struct device *dev); |
| 900 | int __must_check device_add(struct device *dev); |
| 901 | void device_del(struct device *dev); |
Peter Zijlstra | 3c6cc62c | 2023-05-26 12:23:48 +0200 | [diff] [blame] | 902 | |
| 903 | DEFINE_FREE(device_del, struct device *, if (_T) device_del(_T)) |
| 904 | |
Bartosz Golaszewski | 67dd077 | 2020-06-29 08:50:05 +0200 | [diff] [blame] | 905 | int device_for_each_child(struct device *dev, void *data, |
| 906 | int (*fn)(struct device *dev, void *data)); |
| 907 | int device_for_each_child_reverse(struct device *dev, void *data, |
| 908 | int (*fn)(struct device *dev, void *data)); |
| 909 | struct device *device_find_child(struct device *dev, void *data, |
| 910 | int (*match)(struct device *dev, void *data)); |
| 911 | struct device *device_find_child_by_name(struct device *parent, |
| 912 | const char *name); |
Andy Shevchenko | 82b070b | 2022-06-10 15:02:18 +0300 | [diff] [blame] | 913 | struct device *device_find_any_child(struct device *parent); |
| 914 | |
Bartosz Golaszewski | 67dd077 | 2020-06-29 08:50:05 +0200 | [diff] [blame] | 915 | int device_rename(struct device *dev, const char *new_name); |
| 916 | int device_move(struct device *dev, struct device *new_parent, |
| 917 | enum dpm_order dpm_order); |
| 918 | int device_change_owner(struct device *dev, kuid_t kuid, kgid_t kgid); |
| 919 | const char *device_get_devnode(struct device *dev, umode_t *mode, kuid_t *uid, |
| 920 | kgid_t *gid, const char **tmp); |
Linus Torvalds | 4419779 | 2020-08-05 13:02:45 -0700 | [diff] [blame] | 921 | int device_is_dependent(struct device *dev, void *target); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | |
Rafael J. Wysocki | 4f3549d | 2013-05-02 22:15:29 +0200 | [diff] [blame] | 923 | static inline bool device_supports_offline(struct device *dev) |
| 924 | { |
| 925 | return dev->bus && dev->bus->offline && dev->bus->online; |
| 926 | } |
| 927 | |
Dan Williams | d864b8e | 2022-04-26 12:22:44 -0700 | [diff] [blame] | 928 | #define __device_lock_set_class(dev, name, key) \ |
| 929 | do { \ |
| 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) \ |
| 946 | do { \ |
| 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) \ |
| 965 | do { \ |
| 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 Golaszewski | 67dd077 | 2020-06-29 08:50:05 +0200 | [diff] [blame] | 971 | void lock_device_hotplug(void); |
| 972 | void unlock_device_hotplug(void); |
| 973 | int lock_device_hotplug_sysfs(void); |
| 974 | int device_offline(struct device *dev); |
| 975 | int device_online(struct device *dev); |
| 976 | void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode); |
| 977 | void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode); |
Johan Hovold | 4e75e1d | 2017-06-06 17:59:00 +0200 | [diff] [blame] | 978 | void device_set_of_node_from_dev(struct device *dev, const struct device *dev2); |
Ioana Ciornei | 43e76d4 | 2021-06-17 15:29:04 +0300 | [diff] [blame] | 979 | void device_set_node(struct device *dev, struct fwnode_handle *fwnode); |
Rafael J. Wysocki | 97badf8 | 2015-04-03 23:23:37 +0200 | [diff] [blame] | 980 | |
Phil Sutter | 9af15c3 | 2017-01-18 14:04:39 +0100 | [diff] [blame] | 981 | static 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | /* |
Mark McLoughlin | 0aa0dc4 | 2008-12-15 12:58:26 +0000 | [diff] [blame] | 989 | * Root device objects for grouping under /sys/devices |
| 990 | */ |
Bartosz Golaszewski | 67dd077 | 2020-06-29 08:50:05 +0200 | [diff] [blame] | 991 | struct device *__root_device_register(const char *name, struct module *owner); |
Paul Gortmaker | eb5589a | 2011-05-27 09:02:11 -0400 | [diff] [blame] | 992 | |
Tejun Heo | 33ac125 | 2014-01-10 08:57:31 -0500 | [diff] [blame] | 993 | /* This is a macro to avoid include problems with THIS_MODULE */ |
Paul Gortmaker | eb5589a | 2011-05-27 09:02:11 -0400 | [diff] [blame] | 994 | #define root_device_register(name) \ |
| 995 | __root_device_register(name, THIS_MODULE) |
| 996 | |
Bartosz Golaszewski | 67dd077 | 2020-06-29 08:50:05 +0200 | [diff] [blame] | 997 | void root_device_unregister(struct device *root); |
Mark McLoughlin | 0aa0dc4 | 2008-12-15 12:58:26 +0000 | [diff] [blame] | 998 | |
Mark Brown | a5b8b1a | 2009-07-17 15:06:08 +0100 | [diff] [blame] | 999 | static inline void *dev_get_platdata(const struct device *dev) |
| 1000 | { |
| 1001 | return dev->platform_data; |
| 1002 | } |
| 1003 | |
Mark McLoughlin | 0aa0dc4 | 2008-12-15 12:58:26 +0000 | [diff] [blame] | 1004 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | * Manual binding of a device to driver. See drivers/base/bus.c |
| 1006 | * for information on use. |
| 1007 | */ |
Jason Gunthorpe | 0d9f837 | 2021-06-17 16:22:13 +0200 | [diff] [blame] | 1008 | int __must_check device_driver_attach(struct device_driver *drv, |
| 1009 | struct device *dev); |
Bartosz Golaszewski | 67dd077 | 2020-06-29 08:50:05 +0200 | [diff] [blame] | 1010 | int __must_check device_bind_driver(struct device *dev); |
| 1011 | void device_release_driver(struct device *dev); |
| 1012 | int __must_check device_attach(struct device *dev); |
| 1013 | int __must_check driver_attach(struct device_driver *drv); |
| 1014 | void device_initial_probe(struct device *dev); |
| 1015 | int __must_check device_reprobe(struct device *dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1016 | |
Bartosz Golaszewski | 67dd077 | 2020-06-29 08:50:05 +0200 | [diff] [blame] | 1017 | bool device_is_bound(struct device *dev); |
Tomeu Vizoso | 6b9cb42 | 2016-01-07 16:46:12 +0100 | [diff] [blame] | 1018 | |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 1019 | /* |
| 1020 | * Easy functions for dynamically creating devices on the fly |
| 1021 | */ |
Bartosz Golaszewski | 67dd077 | 2020-06-29 08:50:05 +0200 | [diff] [blame] | 1022 | __printf(5, 6) struct device * |
| 1023 | device_create(struct class *cls, struct device *parent, dev_t devt, |
| 1024 | void *drvdata, const char *fmt, ...); |
| 1025 | __printf(6, 7) struct device * |
| 1026 | device_create_with_groups(struct class *cls, struct device *parent, dev_t devt, |
| 1027 | void *drvdata, const struct attribute_group **groups, |
| 1028 | const char *fmt, ...); |
| 1029 | void device_destroy(struct class *cls, dev_t devt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | |
Bartosz Golaszewski | 67dd077 | 2020-06-29 08:50:05 +0200 | [diff] [blame] | 1031 | int __must_check device_add_groups(struct device *dev, |
| 1032 | const struct attribute_group **groups); |
| 1033 | void device_remove_groups(struct device *dev, |
| 1034 | const struct attribute_group **groups); |
Dmitry Torokhov | a7670d4 | 2017-07-19 17:24:31 -0700 | [diff] [blame] | 1035 | |
Dmitry Torokhov | e323b2d | 2017-07-19 17:24:32 -0700 | [diff] [blame] | 1036 | static 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 | |
| 1044 | static 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 Golaszewski | 67dd077 | 2020-06-29 08:50:05 +0200 | [diff] [blame] | 1052 | int __must_check devm_device_add_groups(struct device *dev, |
Dmitry Torokhov | 57b8ff0 | 2017-07-19 17:24:33 -0700 | [diff] [blame] | 1053 | const struct attribute_group **groups); |
Bartosz Golaszewski | 67dd077 | 2020-06-29 08:50:05 +0200 | [diff] [blame] | 1054 | void devm_device_remove_groups(struct device *dev, |
| 1055 | const struct attribute_group **groups); |
| 1056 | int __must_check devm_device_add_group(struct device *dev, |
| 1057 | const struct attribute_group *grp); |
| 1058 | void devm_device_remove_group(struct device *dev, |
| 1059 | const struct attribute_group *grp); |
Dmitry Torokhov | 57b8ff0 | 2017-07-19 17:24:33 -0700 | [diff] [blame] | 1060 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1061 | /* |
| 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-Hartman | d462943 | 2008-01-24 21:04:46 -0800 | [diff] [blame] | 1067 | extern int (*platform_notify)(struct device *dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | |
Greg Kroah-Hartman | d462943 | 2008-01-24 21:04:46 -0800 | [diff] [blame] | 1069 | extern int (*platform_notify_remove)(struct device *dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 | |
| 1071 | |
Wanlong Gao | 880ffb5 | 2011-05-05 07:55:36 +0800 | [diff] [blame] | 1072 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | * get_device - atomically increment the reference count for the device. |
| 1074 | * |
| 1075 | */ |
Bartosz Golaszewski | 67dd077 | 2020-06-29 08:50:05 +0200 | [diff] [blame] | 1076 | struct device *get_device(struct device *dev); |
| 1077 | void put_device(struct device *dev); |
Peter Zijlstra | 3c6cc62c | 2023-05-26 12:23:48 +0200 | [diff] [blame] | 1078 | |
| 1079 | DEFINE_FREE(put_device, struct device *, if (_T) put_device(_T)) |
| 1080 | |
Bartosz Golaszewski | 67dd077 | 2020-06-29 08:50:05 +0200 | [diff] [blame] | 1081 | bool kill_device(struct device *dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 1083 | #ifdef CONFIG_DEVTMPFS |
Bartosz Golaszewski | 67dd077 | 2020-06-29 08:50:05 +0200 | [diff] [blame] | 1084 | int devtmpfs_mount(void); |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 1085 | #else |
Dominik Brodowski | 5e787dbf | 2018-10-23 22:10:35 +0200 | [diff] [blame] | 1086 | static inline int devtmpfs_mount(void) { return 0; } |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 1087 | #endif |
| 1088 | |
Rytchkov Alexey | 116f232b | 2006-03-22 00:58:53 +0100 | [diff] [blame] | 1089 | /* drivers/base/power/shutdown.c */ |
Bartosz Golaszewski | 67dd077 | 2020-06-29 08:50:05 +0200 | [diff] [blame] | 1090 | void device_shutdown(void); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1092 | /* debugging and troubleshooting/diagnostic helpers. */ |
Bartosz Golaszewski | 67dd077 | 2020-06-29 08:50:05 +0200 | [diff] [blame] | 1093 | const char *dev_driver_string(const struct device *dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1094 | |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 1095 | /* Device links interface. */ |
| 1096 | struct device_link *device_link_add(struct device *consumer, |
| 1097 | struct device *supplier, u32 flags); |
| 1098 | void device_link_del(struct device_link *link); |
pascal paillet | d884221 | 2018-07-05 14:25:56 +0000 | [diff] [blame] | 1099 | void device_link_remove(void *consumer, struct device *supplier); |
Saravana Kannan | fc5a251 | 2019-09-04 14:11:23 -0700 | [diff] [blame] | 1100 | void device_links_supplier_sync_state_pause(void); |
| 1101 | void device_links_supplier_sync_state_resume(void); |
Herve Codina | 9406d59 | 2024-03-25 16:21:25 +0100 | [diff] [blame] | 1102 | void device_link_wait_removal(void); |
Joe Perches | 99bcf21 | 2010-06-27 01:02:34 +0000 | [diff] [blame] | 1103 | |
Andrzej Hajda | a787e54 | 2020-07-13 16:43:21 +0200 | [diff] [blame] | 1104 | extern __printf(3, 4) |
Greg Kroah-Hartman | f601e8f | 2020-09-09 09:37:40 +0200 | [diff] [blame] | 1105 | int dev_err_probe(const struct device *dev, int err, const char *fmt, ...); |
Andrzej Hajda | a787e54 | 2020-07-13 16:43:21 +0200 | [diff] [blame] | 1106 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1107 | /* 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 Kleen | e52eec13 | 2010-09-08 16:54:17 +0200 | [diff] [blame] | 1112 | |
| 1113 | #ifdef CONFIG_SYSFS_DEPRECATED |
| 1114 | extern long sysfs_deprecated; |
| 1115 | #else |
| 1116 | #define sysfs_deprecated 0 |
| 1117 | #endif |
| 1118 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1119 | #endif /* _DEVICE_H_ */ |