Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Greg Kroah-Hartman | 5367601 | 2019-12-09 20:32:58 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2001-2003 Patrick Mochel <mochel@osdl.org> |
| 4 | * Copyright (c) 2004-2009 Greg Kroah-Hartman <gregkh@suse.de> |
| 5 | * Copyright (c) 2008-2012 Novell Inc. |
| 6 | * Copyright (c) 2012-2019 Greg Kroah-Hartman <gregkh@linuxfoundation.org> |
| 7 | * Copyright (c) 2012-2019 Linux Foundation |
| 8 | * |
| 9 | * Core driver model functions and structures that should not be |
| 10 | * shared outside of the drivers/base/ directory. |
| 11 | * |
| 12 | */ |
Paul Gortmaker | ba33162 | 2011-05-26 18:08:35 -0400 | [diff] [blame] | 13 | #include <linux/notifier.h> |
Ben Dooks | a1bdc7a | 2005-10-13 17:54:41 +0100 | [diff] [blame] | 14 | |
Greg Kroah-Hartman | c6f7e72 | 2007-11-01 19:41:16 -0700 | [diff] [blame] | 15 | /** |
Kay Sievers | 6b6e39a | 2010-11-15 23:13:18 +0100 | [diff] [blame] | 16 | * struct subsys_private - structure to hold the private to the driver core portions of the bus_type/class structure. |
Greg Kroah-Hartman | c6f7e72 | 2007-11-01 19:41:16 -0700 | [diff] [blame] | 17 | * |
Kay Sievers | 6b6e39a | 2010-11-15 23:13:18 +0100 | [diff] [blame] | 18 | * @subsys - the struct kset that defines this subsystem |
Kay Sievers | ca22e56 | 2011-12-14 14:29:38 -0800 | [diff] [blame] | 19 | * @devices_kset - the subsystem's 'devices' directory |
| 20 | * @interfaces - list of subsystem interfaces associated |
| 21 | * @mutex - protect the devices, and interfaces lists. |
Kay Sievers | 6b6e39a | 2010-11-15 23:13:18 +0100 | [diff] [blame] | 22 | * |
| 23 | * @drivers_kset - the list of drivers associated |
Greg Kroah-Hartman | c6f7e72 | 2007-11-01 19:41:16 -0700 | [diff] [blame] | 24 | * @klist_devices - the klist to iterate over the @devices_kset |
| 25 | * @klist_drivers - the klist to iterate over the @drivers_kset |
| 26 | * @bus_notifier - the bus notifier list for anything that cares about things |
Kay Sievers | 6b6e39a | 2010-11-15 23:13:18 +0100 | [diff] [blame] | 27 | * on this bus. |
Greg Kroah-Hartman | c6f7e72 | 2007-11-01 19:41:16 -0700 | [diff] [blame] | 28 | * @bus - pointer back to the struct bus_type that this structure is associated |
Kay Sievers | 6b6e39a | 2010-11-15 23:13:18 +0100 | [diff] [blame] | 29 | * with. |
| 30 | * |
Kay Sievers | 6b6e39a | 2010-11-15 23:13:18 +0100 | [diff] [blame] | 31 | * @glue_dirs - "glue" directory to put in-between the parent device to |
| 32 | * avoid namespace conflicts |
Kay Sievers | 6b6e39a | 2010-11-15 23:13:18 +0100 | [diff] [blame] | 33 | * @class - pointer back to the struct class that this structure is associated |
| 34 | * with. |
Greg Kroah-Hartman | c6f7e72 | 2007-11-01 19:41:16 -0700 | [diff] [blame] | 35 | * |
| 36 | * This structure is the one that is the actual kobject allowing struct |
Kay Sievers | 6b6e39a | 2010-11-15 23:13:18 +0100 | [diff] [blame] | 37 | * bus_type/class to be statically allocated safely. Nothing outside of the |
| 38 | * driver core should ever touch these fields. |
Greg Kroah-Hartman | c6f7e72 | 2007-11-01 19:41:16 -0700 | [diff] [blame] | 39 | */ |
Kay Sievers | 6b6e39a | 2010-11-15 23:13:18 +0100 | [diff] [blame] | 40 | struct subsys_private { |
Greg Kroah-Hartman | c6f7e72 | 2007-11-01 19:41:16 -0700 | [diff] [blame] | 41 | struct kset subsys; |
Greg Kroah-Hartman | c6f7e72 | 2007-11-01 19:41:16 -0700 | [diff] [blame] | 42 | struct kset *devices_kset; |
Kay Sievers | ca22e56 | 2011-12-14 14:29:38 -0800 | [diff] [blame] | 43 | struct list_head interfaces; |
| 44 | struct mutex mutex; |
Kay Sievers | 6b6e39a | 2010-11-15 23:13:18 +0100 | [diff] [blame] | 45 | |
| 46 | struct kset *drivers_kset; |
Greg Kroah-Hartman | c6f7e72 | 2007-11-01 19:41:16 -0700 | [diff] [blame] | 47 | struct klist klist_devices; |
| 48 | struct klist klist_drivers; |
| 49 | struct blocking_notifier_head bus_notifier; |
| 50 | unsigned int drivers_autoprobe:1; |
| 51 | struct bus_type *bus; |
Kay Sievers | 6b6e39a | 2010-11-15 23:13:18 +0100 | [diff] [blame] | 52 | |
Kay Sievers | 6b6e39a | 2010-11-15 23:13:18 +0100 | [diff] [blame] | 53 | struct kset glue_dirs; |
Kay Sievers | 6b6e39a | 2010-11-15 23:13:18 +0100 | [diff] [blame] | 54 | struct class *class; |
Greg Kroah-Hartman | c6f7e72 | 2007-11-01 19:41:16 -0700 | [diff] [blame] | 55 | }; |
Kay Sievers | 6b6e39a | 2010-11-15 23:13:18 +0100 | [diff] [blame] | 56 | #define to_subsys_private(obj) container_of(obj, struct subsys_private, subsys.kobj) |
Ben Dooks | a1bdc7a | 2005-10-13 17:54:41 +0100 | [diff] [blame] | 57 | |
Greg Kroah-Hartman | e5dd127 | 2007-11-28 15:59:15 -0800 | [diff] [blame] | 58 | struct driver_private { |
| 59 | struct kobject kobj; |
| 60 | struct klist klist_devices; |
| 61 | struct klist_node knode_bus; |
| 62 | struct module_kobject *mkobj; |
| 63 | struct device_driver *driver; |
| 64 | }; |
| 65 | #define to_driver(obj) container_of(obj, struct driver_private, kobj) |
Greg Kroah-Hartman | c6f7e72 | 2007-11-01 19:41:16 -0700 | [diff] [blame] | 66 | |
Greg Kroah-Hartman | fb069a5 | 2008-12-16 12:23:36 -0800 | [diff] [blame] | 67 | /** |
| 68 | * struct device_private - structure to hold the private to the driver core portions of the device structure. |
| 69 | * |
Greg Kroah-Hartman | f791b8c | 2008-12-16 12:24:56 -0800 | [diff] [blame] | 70 | * @klist_children - klist containing all children of this device |
| 71 | * @knode_parent - node in sibling list |
Greg Kroah-Hartman | 8940b4f | 2008-12-16 12:25:49 -0800 | [diff] [blame] | 72 | * @knode_driver - node in driver list |
Greg Kroah-Hartman | ae1b417 | 2008-12-16 12:26:21 -0800 | [diff] [blame] | 73 | * @knode_bus - node in bus list |
Wei Yang | 570d020 | 2019-01-18 10:34:59 +0800 | [diff] [blame] | 74 | * @knode_class - node in class list |
Greg Kroah-Hartman | ef8a3fd | 2012-03-08 12:17:22 -0800 | [diff] [blame] | 75 | * @deferred_probe - entry in deferred_probe_list which is used to retry the |
| 76 | * binding of drivers which were unable to get all the resources needed by |
| 77 | * the device; typically because it depends on another driver getting |
| 78 | * probed first. |
Alexander Duyck | ef0ff68 | 2019-01-22 10:39:21 -0800 | [diff] [blame] | 79 | * @async_driver - pointer to device driver awaiting probe via async_probe |
Tomeu Vizoso | 82b2c3c | 2015-06-29 16:59:02 +0200 | [diff] [blame] | 80 | * @device - pointer back to the struct device that this structure is |
Greg Kroah-Hartman | fb069a5 | 2008-12-16 12:23:36 -0800 | [diff] [blame] | 81 | * associated with. |
Alexander Duyck | 3451a49 | 2019-01-22 10:39:10 -0800 | [diff] [blame] | 82 | * @dead - This device is currently either in the process of or has been |
| 83 | * removed from the system. Any asynchronous events scheduled for this |
| 84 | * device should exit without taking any action. |
Greg Kroah-Hartman | fb069a5 | 2008-12-16 12:23:36 -0800 | [diff] [blame] | 85 | * |
| 86 | * Nothing outside of the driver core should ever touch these fields. |
| 87 | */ |
| 88 | struct device_private { |
Greg Kroah-Hartman | f791b8c | 2008-12-16 12:24:56 -0800 | [diff] [blame] | 89 | struct klist klist_children; |
| 90 | struct klist_node knode_parent; |
Greg Kroah-Hartman | 8940b4f | 2008-12-16 12:25:49 -0800 | [diff] [blame] | 91 | struct klist_node knode_driver; |
Greg Kroah-Hartman | ae1b417 | 2008-12-16 12:26:21 -0800 | [diff] [blame] | 92 | struct klist_node knode_bus; |
Wei Yang | 570d020 | 2019-01-18 10:34:59 +0800 | [diff] [blame] | 93 | struct klist_node knode_class; |
Greg Kroah-Hartman | ef8a3fd | 2012-03-08 12:17:22 -0800 | [diff] [blame] | 94 | struct list_head deferred_probe; |
Alexander Duyck | ef0ff68 | 2019-01-22 10:39:21 -0800 | [diff] [blame] | 95 | struct device_driver *async_driver; |
Andrzej Hajda | d090b70 | 2020-07-13 16:43:22 +0200 | [diff] [blame] | 96 | char *deferred_probe_reason; |
Greg Kroah-Hartman | fb069a5 | 2008-12-16 12:23:36 -0800 | [diff] [blame] | 97 | struct device *device; |
Alexander Duyck | 3451a49 | 2019-01-22 10:39:10 -0800 | [diff] [blame] | 98 | u8 dead:1; |
Greg Kroah-Hartman | fb069a5 | 2008-12-16 12:23:36 -0800 | [diff] [blame] | 99 | }; |
Greg Kroah-Hartman | f791b8c | 2008-12-16 12:24:56 -0800 | [diff] [blame] | 100 | #define to_device_private_parent(obj) \ |
| 101 | container_of(obj, struct device_private, knode_parent) |
Greg Kroah-Hartman | 8940b4f | 2008-12-16 12:25:49 -0800 | [diff] [blame] | 102 | #define to_device_private_driver(obj) \ |
| 103 | container_of(obj, struct device_private, knode_driver) |
Greg Kroah-Hartman | ae1b417 | 2008-12-16 12:26:21 -0800 | [diff] [blame] | 104 | #define to_device_private_bus(obj) \ |
| 105 | container_of(obj, struct device_private, knode_bus) |
Wei Yang | 570d020 | 2019-01-18 10:34:59 +0800 | [diff] [blame] | 106 | #define to_device_private_class(obj) \ |
| 107 | container_of(obj, struct device_private, knode_class) |
Greg Kroah-Hartman | fb069a5 | 2008-12-16 12:23:36 -0800 | [diff] [blame] | 108 | |
Greg Kroah-Hartman | c6f7e72 | 2007-11-01 19:41:16 -0700 | [diff] [blame] | 109 | /* initialisation functions */ |
Ben Dooks | a1bdc7a | 2005-10-13 17:54:41 +0100 | [diff] [blame] | 110 | extern int devices_init(void); |
| 111 | extern int buses_init(void); |
| 112 | extern int classes_init(void); |
| 113 | extern int firmware_init(void); |
Michael Holzheu | 4039483 | 2006-05-09 12:53:49 +0200 | [diff] [blame] | 114 | #ifdef CONFIG_SYS_HYPERVISOR |
| 115 | extern int hypervisor_init(void); |
| 116 | #else |
| 117 | static inline int hypervisor_init(void) { return 0; } |
| 118 | #endif |
Ben Dooks | a1bdc7a | 2005-10-13 17:54:41 +0100 | [diff] [blame] | 119 | extern int platform_bus_init(void); |
Ben Hutchings | 024f784 | 2012-01-10 02:59:49 +0000 | [diff] [blame] | 120 | extern void cpu_dev_init(void); |
Rafael J. Wysocki | caa73ea | 2013-12-29 15:25:48 +0100 | [diff] [blame] | 121 | extern void container_dev_init(void); |
Dave Jiang | 471b12c4 | 2021-02-10 13:16:11 -0700 | [diff] [blame] | 122 | #ifdef CONFIG_AUXILIARY_BUS |
| 123 | extern void auxiliary_bus_init(void); |
| 124 | #else |
| 125 | static inline void auxiliary_bus_init(void) { } |
| 126 | #endif |
Ben Dooks | a1bdc7a | 2005-10-13 17:54:41 +0100 | [diff] [blame] | 127 | |
Tejun Heo | d73ce00 | 2013-03-12 11:30:05 -0700 | [diff] [blame] | 128 | struct kobject *virtual_device_parent(struct device *dev); |
| 129 | |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 130 | extern int bus_add_device(struct device *dev); |
Alan Stern | 2023c61 | 2009-07-30 15:27:18 -0400 | [diff] [blame] | 131 | extern void bus_probe_device(struct device *dev); |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 132 | extern void bus_remove_device(struct device *dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 134 | extern int bus_add_driver(struct device_driver *drv); |
| 135 | extern void bus_remove_driver(struct device_driver *drv); |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 136 | extern void device_release_driver_internal(struct device *dev, |
| 137 | struct device_driver *drv, |
| 138 | struct device *parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 140 | extern void driver_detach(struct device_driver *drv); |
Grant Likely | d1c3414 | 2012-03-05 08:47:41 -0700 | [diff] [blame] | 141 | extern void driver_deferred_probe_del(struct device *dev); |
Andrzej Hajda | d090b70 | 2020-07-13 16:43:22 +0200 | [diff] [blame] | 142 | extern void device_set_deferred_probe_reason(const struct device *dev, |
| 143 | struct va_format *vaf); |
Ming Lei | 49b420a | 2009-01-21 23:27:47 +0800 | [diff] [blame] | 144 | static inline int driver_match_device(struct device_driver *drv, |
| 145 | struct device *dev) |
| 146 | { |
Ming Lei | 5247aec | 2009-03-27 21:50:00 +0800 | [diff] [blame] | 147 | return drv->bus->match ? drv->bus->match(dev, drv) : 1; |
Ming Lei | 49b420a | 2009-01-21 23:27:47 +0800 | [diff] [blame] | 148 | } |
Dmitry Torokhov | 765230b | 2015-03-30 16:20:04 -0700 | [diff] [blame] | 149 | extern bool driver_allows_async_probing(struct device_driver *drv); |
mochel@digitalimplant.org | 07e4a3e | 2005-03-21 10:52:54 -0800 | [diff] [blame] | 150 | |
Greg Kroah-Hartman | ed0617b | 2013-08-08 15:22:56 -0700 | [diff] [blame] | 151 | extern int driver_add_groups(struct device_driver *drv, |
| 152 | const struct attribute_group **groups); |
| 153 | extern void driver_remove_groups(struct device_driver *drv, |
| 154 | const struct attribute_group **groups); |
Alexander Duyck | ed88747 | 2019-01-22 10:39:16 -0800 | [diff] [blame] | 155 | void device_driver_detach(struct device *dev); |
Greg Kroah-Hartman | ed0617b | 2013-08-08 15:22:56 -0700 | [diff] [blame] | 156 | |
Greg Kroah-Hartman | aa49b91 | 2006-06-20 13:59:20 -0700 | [diff] [blame] | 157 | extern char *make_class_name(const char *name, struct kobject *kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | |
Adrian Bunk | 2a01345 | 2007-06-18 01:42:54 +0200 | [diff] [blame] | 159 | extern int devres_release_all(struct device *dev); |
Strashko, Grygorii | 013c074 | 2015-11-10 11:42:34 +0200 | [diff] [blame] | 160 | extern void device_block_probing(void); |
| 161 | extern void device_unblock_probing(void); |
Greg Kroah-Hartman | 823bccf | 2007-04-13 13:15:19 -0700 | [diff] [blame] | 162 | |
Kay Sievers | ca22e56 | 2011-12-14 14:29:38 -0800 | [diff] [blame] | 163 | /* /sys/devices directory */ |
Greg Kroah-Hartman | 881c6cfd | 2007-11-01 09:29:06 -0600 | [diff] [blame] | 164 | extern struct kset *devices_kset; |
Grygorii Strashko | 52cdbdd | 2015-07-27 20:43:01 +0300 | [diff] [blame] | 165 | extern void devices_kset_move_last(struct device *dev); |
Greg Kroah-Hartman | c63469a | 2007-11-28 12:23:18 -0800 | [diff] [blame] | 166 | |
Randy Dunlap | 92b4214 | 2007-12-31 10:05:43 -0800 | [diff] [blame] | 167 | #if defined(CONFIG_MODULES) && defined(CONFIG_SYSFS) |
Greg Kroah-Hartman | c63469a | 2007-11-28 12:23:18 -0800 | [diff] [blame] | 168 | extern void module_add_driver(struct module *mod, struct device_driver *drv); |
| 169 | extern void module_remove_driver(struct device_driver *drv); |
| 170 | #else |
| 171 | static inline void module_add_driver(struct module *mod, |
| 172 | struct device_driver *drv) { } |
| 173 | static inline void module_remove_driver(struct device_driver *drv) { } |
| 174 | #endif |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 175 | |
| 176 | #ifdef CONFIG_DEVTMPFS |
| 177 | extern int devtmpfs_init(void); |
| 178 | #else |
| 179 | static inline int devtmpfs_init(void) { return 0; } |
| 180 | #endif |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 181 | |
| 182 | /* Device links support */ |
| 183 | extern int device_links_read_lock(void); |
| 184 | extern void device_links_read_unlock(int idx); |
Joel Fernandes (Google) | c2fa1e1 | 2019-07-16 18:12:25 -0400 | [diff] [blame] | 185 | extern int device_links_read_lock_held(void); |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 186 | extern int device_links_check_suppliers(struct device *dev); |
Saravana Kannan | b6f617d | 2021-03-02 13:11:31 -0800 | [diff] [blame] | 187 | extern void device_links_force_bind(struct device *dev); |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 188 | extern void device_links_driver_bound(struct device *dev); |
| 189 | extern void device_links_driver_cleanup(struct device *dev); |
| 190 | extern void device_links_no_driver(struct device *dev); |
| 191 | extern bool device_links_busy(struct device *dev); |
| 192 | extern void device_links_unbind_consumers(struct device *dev); |
Saravana Kannan | d46f3e3 | 2021-04-01 21:03:41 -0700 | [diff] [blame] | 193 | extern void fw_devlink_drivers_done(void); |
Feng Kan | 494fd7b | 2018-04-10 16:57:06 -0700 | [diff] [blame] | 194 | |
| 195 | /* device pm support */ |
| 196 | void device_pm_move_to_tail(struct device *dev); |
Greg Kroah-Hartman | cf901a1c | 2019-12-09 20:32:59 +0100 | [diff] [blame] | 197 | |
| 198 | #ifdef CONFIG_DEVTMPFS |
| 199 | int devtmpfs_create_node(struct device *dev); |
| 200 | int devtmpfs_delete_node(struct device *dev); |
| 201 | #else |
| 202 | static inline int devtmpfs_create_node(struct device *dev) { return 0; } |
| 203 | static inline int devtmpfs_delete_node(struct device *dev) { return 0; } |
| 204 | #endif |
Rafael J. Wysocki | 384f5a8 | 2021-07-12 19:27:12 +0200 | [diff] [blame] | 205 | |
| 206 | void software_node_notify(struct device *dev); |
| 207 | void software_node_notify_remove(struct device *dev); |