blob: 2882af26392ab0b0d3d88f29ece960185dffefb4 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Greg Kroah-Hartman53676012019-12-09 20:32:58 +01002/*
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 Gortmakerba331622011-05-26 18:08:35 -040013#include <linux/notifier.h>
Ben Dooksa1bdc7a2005-10-13 17:54:41 +010014
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -070015/**
Kay Sievers6b6e39a2010-11-15 23:13:18 +010016 * struct subsys_private - structure to hold the private to the driver core portions of the bus_type/class structure.
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -070017 *
Kay Sievers6b6e39a2010-11-15 23:13:18 +010018 * @subsys - the struct kset that defines this subsystem
Kay Sieversca22e562011-12-14 14:29:38 -080019 * @devices_kset - the subsystem's 'devices' directory
20 * @interfaces - list of subsystem interfaces associated
21 * @mutex - protect the devices, and interfaces lists.
Kay Sievers6b6e39a2010-11-15 23:13:18 +010022 *
23 * @drivers_kset - the list of drivers associated
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -070024 * @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 Sievers6b6e39a2010-11-15 23:13:18 +010027 * on this bus.
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -070028 * @bus - pointer back to the struct bus_type that this structure is associated
Kay Sievers6b6e39a2010-11-15 23:13:18 +010029 * with.
30 *
Kay Sievers6b6e39a2010-11-15 23:13:18 +010031 * @glue_dirs - "glue" directory to put in-between the parent device to
32 * avoid namespace conflicts
Kay Sievers6b6e39a2010-11-15 23:13:18 +010033 * @class - pointer back to the struct class that this structure is associated
34 * with.
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -070035 *
36 * This structure is the one that is the actual kobject allowing struct
Kay Sievers6b6e39a2010-11-15 23:13:18 +010037 * bus_type/class to be statically allocated safely. Nothing outside of the
38 * driver core should ever touch these fields.
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -070039 */
Kay Sievers6b6e39a2010-11-15 23:13:18 +010040struct subsys_private {
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -070041 struct kset subsys;
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -070042 struct kset *devices_kset;
Kay Sieversca22e562011-12-14 14:29:38 -080043 struct list_head interfaces;
44 struct mutex mutex;
Kay Sievers6b6e39a2010-11-15 23:13:18 +010045
46 struct kset *drivers_kset;
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -070047 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 Sievers6b6e39a2010-11-15 23:13:18 +010052
Kay Sievers6b6e39a2010-11-15 23:13:18 +010053 struct kset glue_dirs;
Kay Sievers6b6e39a2010-11-15 23:13:18 +010054 struct class *class;
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -070055};
Kay Sievers6b6e39a2010-11-15 23:13:18 +010056#define to_subsys_private(obj) container_of(obj, struct subsys_private, subsys.kobj)
Ben Dooksa1bdc7a2005-10-13 17:54:41 +010057
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -080058struct 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-Hartmanc6f7e722007-11-01 19:41:16 -070066
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -080067/**
68 * struct device_private - structure to hold the private to the driver core portions of the device structure.
69 *
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -080070 * @klist_children - klist containing all children of this device
71 * @knode_parent - node in sibling list
Greg Kroah-Hartman8940b4f2008-12-16 12:25:49 -080072 * @knode_driver - node in driver list
Greg Kroah-Hartmanae1b4172008-12-16 12:26:21 -080073 * @knode_bus - node in bus list
Wei Yang570d0202019-01-18 10:34:59 +080074 * @knode_class - node in class list
Greg Kroah-Hartmanef8a3fd2012-03-08 12:17:22 -080075 * @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 Duyckef0ff682019-01-22 10:39:21 -080079 * @async_driver - pointer to device driver awaiting probe via async_probe
Tomeu Vizoso82b2c3c2015-06-29 16:59:02 +020080 * @device - pointer back to the struct device that this structure is
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -080081 * associated with.
Alexander Duyck3451a492019-01-22 10:39:10 -080082 * @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-Hartmanfb069a52008-12-16 12:23:36 -080085 *
86 * Nothing outside of the driver core should ever touch these fields.
87 */
88struct device_private {
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -080089 struct klist klist_children;
90 struct klist_node knode_parent;
Greg Kroah-Hartman8940b4f2008-12-16 12:25:49 -080091 struct klist_node knode_driver;
Greg Kroah-Hartmanae1b4172008-12-16 12:26:21 -080092 struct klist_node knode_bus;
Wei Yang570d0202019-01-18 10:34:59 +080093 struct klist_node knode_class;
Greg Kroah-Hartmanef8a3fd2012-03-08 12:17:22 -080094 struct list_head deferred_probe;
Alexander Duyckef0ff682019-01-22 10:39:21 -080095 struct device_driver *async_driver;
Andrzej Hajdad090b702020-07-13 16:43:22 +020096 char *deferred_probe_reason;
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -080097 struct device *device;
Alexander Duyck3451a492019-01-22 10:39:10 -080098 u8 dead:1;
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -080099};
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -0800100#define to_device_private_parent(obj) \
101 container_of(obj, struct device_private, knode_parent)
Greg Kroah-Hartman8940b4f2008-12-16 12:25:49 -0800102#define to_device_private_driver(obj) \
103 container_of(obj, struct device_private, knode_driver)
Greg Kroah-Hartmanae1b4172008-12-16 12:26:21 -0800104#define to_device_private_bus(obj) \
105 container_of(obj, struct device_private, knode_bus)
Wei Yang570d0202019-01-18 10:34:59 +0800106#define to_device_private_class(obj) \
107 container_of(obj, struct device_private, knode_class)
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800108
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700109/* initialisation functions */
Ben Dooksa1bdc7a2005-10-13 17:54:41 +0100110extern int devices_init(void);
111extern int buses_init(void);
112extern int classes_init(void);
113extern int firmware_init(void);
Michael Holzheu40394832006-05-09 12:53:49 +0200114#ifdef CONFIG_SYS_HYPERVISOR
115extern int hypervisor_init(void);
116#else
117static inline int hypervisor_init(void) { return 0; }
118#endif
Ben Dooksa1bdc7a2005-10-13 17:54:41 +0100119extern int platform_bus_init(void);
Ben Hutchings024f7842012-01-10 02:59:49 +0000120extern void cpu_dev_init(void);
Rafael J. Wysockicaa73ea2013-12-29 15:25:48 +0100121extern void container_dev_init(void);
Dave Jiang471b12c42021-02-10 13:16:11 -0700122#ifdef CONFIG_AUXILIARY_BUS
123extern void auxiliary_bus_init(void);
124#else
125static inline void auxiliary_bus_init(void) { }
126#endif
Ben Dooksa1bdc7a2005-10-13 17:54:41 +0100127
Tejun Heod73ce002013-03-12 11:30:05 -0700128struct kobject *virtual_device_parent(struct device *dev);
129
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800130extern int bus_add_device(struct device *dev);
Alan Stern2023c612009-07-30 15:27:18 -0400131extern void bus_probe_device(struct device *dev);
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800132extern void bus_remove_device(struct device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800134extern int bus_add_driver(struct device_driver *drv);
135extern void bus_remove_driver(struct device_driver *drv);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100136extern void device_release_driver_internal(struct device *dev,
137 struct device_driver *drv,
138 struct device *parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800140extern void driver_detach(struct device_driver *drv);
Grant Likelyd1c34142012-03-05 08:47:41 -0700141extern void driver_deferred_probe_del(struct device *dev);
Andrzej Hajdad090b702020-07-13 16:43:22 +0200142extern void device_set_deferred_probe_reason(const struct device *dev,
143 struct va_format *vaf);
Ming Lei49b420a2009-01-21 23:27:47 +0800144static inline int driver_match_device(struct device_driver *drv,
145 struct device *dev)
146{
Ming Lei5247aec2009-03-27 21:50:00 +0800147 return drv->bus->match ? drv->bus->match(dev, drv) : 1;
Ming Lei49b420a2009-01-21 23:27:47 +0800148}
Dmitry Torokhov765230b2015-03-30 16:20:04 -0700149extern bool driver_allows_async_probing(struct device_driver *drv);
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -0800150
Greg Kroah-Hartmaned0617b2013-08-08 15:22:56 -0700151extern int driver_add_groups(struct device_driver *drv,
152 const struct attribute_group **groups);
153extern void driver_remove_groups(struct device_driver *drv,
154 const struct attribute_group **groups);
Alexander Duycked887472019-01-22 10:39:16 -0800155void device_driver_detach(struct device *dev);
Greg Kroah-Hartmaned0617b2013-08-08 15:22:56 -0700156
Greg Kroah-Hartmanaa49b912006-06-20 13:59:20 -0700157extern char *make_class_name(const char *name, struct kobject *kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Adrian Bunk2a013452007-06-18 01:42:54 +0200159extern int devres_release_all(struct device *dev);
Strashko, Grygorii013c0742015-11-10 11:42:34 +0200160extern void device_block_probing(void);
161extern void device_unblock_probing(void);
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700162
Kay Sieversca22e562011-12-14 14:29:38 -0800163/* /sys/devices directory */
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -0600164extern struct kset *devices_kset;
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +0300165extern void devices_kset_move_last(struct device *dev);
Greg Kroah-Hartmanc63469a2007-11-28 12:23:18 -0800166
Randy Dunlap92b42142007-12-31 10:05:43 -0800167#if defined(CONFIG_MODULES) && defined(CONFIG_SYSFS)
Greg Kroah-Hartmanc63469a2007-11-28 12:23:18 -0800168extern void module_add_driver(struct module *mod, struct device_driver *drv);
169extern void module_remove_driver(struct device_driver *drv);
170#else
171static inline void module_add_driver(struct module *mod,
172 struct device_driver *drv) { }
173static inline void module_remove_driver(struct device_driver *drv) { }
174#endif
Kay Sievers2b2af542009-04-30 15:23:42 +0200175
176#ifdef CONFIG_DEVTMPFS
177extern int devtmpfs_init(void);
178#else
179static inline int devtmpfs_init(void) { return 0; }
180#endif
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100181
182/* Device links support */
183extern int device_links_read_lock(void);
184extern void device_links_read_unlock(int idx);
Joel Fernandes (Google)c2fa1e12019-07-16 18:12:25 -0400185extern int device_links_read_lock_held(void);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100186extern int device_links_check_suppliers(struct device *dev);
Saravana Kannanb6f617d2021-03-02 13:11:31 -0800187extern void device_links_force_bind(struct device *dev);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100188extern void device_links_driver_bound(struct device *dev);
189extern void device_links_driver_cleanup(struct device *dev);
190extern void device_links_no_driver(struct device *dev);
191extern bool device_links_busy(struct device *dev);
192extern void device_links_unbind_consumers(struct device *dev);
Saravana Kannand46f3e32021-04-01 21:03:41 -0700193extern void fw_devlink_drivers_done(void);
Feng Kan494fd7b2018-04-10 16:57:06 -0700194
195/* device pm support */
196void device_pm_move_to_tail(struct device *dev);
Greg Kroah-Hartmancf901a1c2019-12-09 20:32:59 +0100197
198#ifdef CONFIG_DEVTMPFS
199int devtmpfs_create_node(struct device *dev);
200int devtmpfs_delete_node(struct device *dev);
201#else
202static inline int devtmpfs_create_node(struct device *dev) { return 0; }
203static inline int devtmpfs_delete_node(struct device *dev) { return 0; }
204#endif
Rafael J. Wysocki384f5a82021-07-12 19:27:12 +0200205
206void software_node_notify(struct device *dev);
207void software_node_notify_remove(struct device *dev);