xref: /openbmc/linux/include/linux/auxiliary_bus.h (revision 365481e4)
17de3697eSDave Ertman /* SPDX-License-Identifier: GPL-2.0-only */
27de3697eSDave Ertman /*
37de3697eSDave Ertman  * Copyright (c) 2019-2020 Intel Corporation
47de3697eSDave Ertman  *
57de3697eSDave Ertman  * Please see Documentation/driver-api/auxiliary_bus.rst for more information.
67de3697eSDave Ertman  */
77de3697eSDave Ertman 
87de3697eSDave Ertman #ifndef _AUXILIARY_BUS_H_
97de3697eSDave Ertman #define _AUXILIARY_BUS_H_
107de3697eSDave Ertman 
117de3697eSDave Ertman #include <linux/device.h>
127de3697eSDave Ertman #include <linux/mod_devicetable.h>
137de3697eSDave Ertman 
14e1b51868SIra Weiny /**
15e1b51868SIra Weiny  * DOC: DEVICE_LIFESPAN
16e1b51868SIra Weiny  *
17e1b51868SIra Weiny  * The registering driver is the entity that allocates memory for the
18e1b51868SIra Weiny  * auxiliary_device and registers it on the auxiliary bus.  It is important to
19e1b51868SIra Weiny  * note that, as opposed to the platform bus, the registering driver is wholly
20e1b51868SIra Weiny  * responsible for the management of the memory used for the device object.
21e1b51868SIra Weiny  *
22e1b51868SIra Weiny  * To be clear the memory for the auxiliary_device is freed in the release()
23e1b51868SIra Weiny  * callback defined by the registering driver.  The registering driver should
24e1b51868SIra Weiny  * only call auxiliary_device_delete() and then auxiliary_device_uninit() when
25e1b51868SIra Weiny  * it is done with the device.  The release() function is then automatically
26e1b51868SIra Weiny  * called if and when other code releases their reference to the devices.
27e1b51868SIra Weiny  *
28e1b51868SIra Weiny  * A parent object, defined in the shared header file, contains the
29e1b51868SIra Weiny  * auxiliary_device.  It also contains a pointer to the shared object(s), which
30e1b51868SIra Weiny  * also is defined in the shared header.  Both the parent object and the shared
31e1b51868SIra Weiny  * object(s) are allocated by the registering driver.  This layout allows the
32e1b51868SIra Weiny  * auxiliary_driver's registering module to perform a container_of() call to go
33e1b51868SIra Weiny  * from the pointer to the auxiliary_device, that is passed during the call to
34e1b51868SIra Weiny  * the auxiliary_driver's probe function, up to the parent object, and then
35e1b51868SIra Weiny  * have access to the shared object(s).
36e1b51868SIra Weiny  *
37e1b51868SIra Weiny  * The memory for the shared object(s) must have a lifespan equal to, or
38e1b51868SIra Weiny  * greater than, the lifespan of the memory for the auxiliary_device.  The
39e1b51868SIra Weiny  * auxiliary_driver should only consider that the shared object is valid as
40e1b51868SIra Weiny  * long as the auxiliary_device is still registered on the auxiliary bus.  It
41e1b51868SIra Weiny  * is up to the registering driver to manage (e.g. free or keep available) the
42e1b51868SIra Weiny  * memory for the shared object beyond the life of the auxiliary_device.
43e1b51868SIra Weiny  *
44e1b51868SIra Weiny  * The registering driver must unregister all auxiliary devices before its own
45e1b51868SIra Weiny  * driver.remove() is completed.  An easy way to ensure this is to use the
46e1b51868SIra Weiny  * devm_add_action_or_reset() call to register a function against the parent
47e1b51868SIra Weiny  * device which unregisters the auxiliary device object(s).
48e1b51868SIra Weiny  *
49e1b51868SIra Weiny  * Finally, any operations which operate on the auxiliary devices must continue
50e1b51868SIra Weiny  * to function (if only to return an error) after the registering driver
51e1b51868SIra Weiny  * unregisters the auxiliary device.
52e1b51868SIra Weiny  */
53e1b51868SIra Weiny 
54e1b51868SIra Weiny /**
55e1b51868SIra Weiny  * struct auxiliary_device - auxiliary device object.
56e1b51868SIra Weiny  * @dev: Device,
57e1b51868SIra Weiny  *       The release and parent fields of the device structure must be filled
58e1b51868SIra Weiny  *       in
59e1b51868SIra Weiny  * @name: Match name found by the auxiliary device driver,
60e1b51868SIra Weiny  * @id: unique identitier if multiple devices of the same name are exported,
61e1b51868SIra Weiny  *
62e1b51868SIra Weiny  * An auxiliary_device represents a part of its parent device's functionality.
63e1b51868SIra Weiny  * It is given a name that, combined with the registering drivers
64e1b51868SIra Weiny  * KBUILD_MODNAME, creates a match_name that is used for driver binding, and an
65e1b51868SIra Weiny  * id that combined with the match_name provide a unique name to register with
66e1b51868SIra Weiny  * the bus subsystem.  For example, a driver registering an auxiliary device is
67e1b51868SIra Weiny  * named 'foo_mod.ko' and the subdevice is named 'foo_dev'.  The match name is
68e1b51868SIra Weiny  * therefore 'foo_mod.foo_dev'.
69e1b51868SIra Weiny  *
70e1b51868SIra Weiny  * Registering an auxiliary_device is a three-step process.
71e1b51868SIra Weiny  *
72e1b51868SIra Weiny  * First, a 'struct auxiliary_device' needs to be defined or allocated for each
73e1b51868SIra Weiny  * sub-device desired.  The name, id, dev.release, and dev.parent fields of
74e1b51868SIra Weiny  * this structure must be filled in as follows.
75e1b51868SIra Weiny  *
76e1b51868SIra Weiny  * The 'name' field is to be given a name that is recognized by the auxiliary
77e1b51868SIra Weiny  * driver.  If two auxiliary_devices with the same match_name, eg
78e1b51868SIra Weiny  * "foo_mod.foo_dev", are registered onto the bus, they must have unique id
79e1b51868SIra Weiny  * values (e.g. "x" and "y") so that the registered devices names are
80e1b51868SIra Weiny  * "foo_mod.foo_dev.x" and "foo_mod.foo_dev.y".  If match_name + id are not
81e1b51868SIra Weiny  * unique, then the device_add fails and generates an error message.
82e1b51868SIra Weiny  *
83e1b51868SIra Weiny  * The auxiliary_device.dev.type.release or auxiliary_device.dev.release must
84e1b51868SIra Weiny  * be populated with a non-NULL pointer to successfully register the
85e1b51868SIra Weiny  * auxiliary_device.  This release call is where resources associated with the
86e1b51868SIra Weiny  * auxiliary device must be free'ed.  Because once the device is placed on the
87e1b51868SIra Weiny  * bus the parent driver can not tell what other code may have a reference to
88e1b51868SIra Weiny  * this data.
89e1b51868SIra Weiny  *
90e1b51868SIra Weiny  * The auxiliary_device.dev.parent should be set.  Typically to the registering
91e1b51868SIra Weiny  * drivers device.
92e1b51868SIra Weiny  *
93e1b51868SIra Weiny  * Second, call auxiliary_device_init(), which checks several aspects of the
94e1b51868SIra Weiny  * auxiliary_device struct and performs a device_initialize().  After this step
95e1b51868SIra Weiny  * completes, any error state must have a call to auxiliary_device_uninit() in
96e1b51868SIra Weiny  * its resolution path.
97e1b51868SIra Weiny  *
98e1b51868SIra Weiny  * The third and final step in registering an auxiliary_device is to perform a
99e1b51868SIra Weiny  * call to auxiliary_device_add(), which sets the name of the device and adds
100e1b51868SIra Weiny  * the device to the bus.
101e1b51868SIra Weiny  *
102e1b51868SIra Weiny  * .. code-block:: c
103e1b51868SIra Weiny  *
104e1b51868SIra Weiny  *      #define MY_DEVICE_NAME "foo_dev"
105e1b51868SIra Weiny  *
106e1b51868SIra Weiny  *      ...
107e1b51868SIra Weiny  *
108e1b51868SIra Weiny  *	struct auxiliary_device *my_aux_dev = my_aux_dev_alloc(xxx);
109e1b51868SIra Weiny  *
110e1b51868SIra Weiny  *	// Step 1:
111e1b51868SIra Weiny  *	my_aux_dev->name = MY_DEVICE_NAME;
112e1b51868SIra Weiny  *	my_aux_dev->id = my_unique_id_alloc(xxx);
113e1b51868SIra Weiny  *	my_aux_dev->dev.release = my_aux_dev_release;
114e1b51868SIra Weiny  *	my_aux_dev->dev.parent = my_dev;
115e1b51868SIra Weiny  *
116e1b51868SIra Weiny  *	// Step 2:
117e1b51868SIra Weiny  *	if (auxiliary_device_init(my_aux_dev))
118e1b51868SIra Weiny  *		goto fail;
119e1b51868SIra Weiny  *
120e1b51868SIra Weiny  *	// Step 3:
121e1b51868SIra Weiny  *	if (auxiliary_device_add(my_aux_dev)) {
122e1b51868SIra Weiny  *		auxiliary_device_uninit(my_aux_dev);
123e1b51868SIra Weiny  *		goto fail;
124e1b51868SIra Weiny  *	}
125e1b51868SIra Weiny  *
126e1b51868SIra Weiny  *	...
127e1b51868SIra Weiny  *
128e1b51868SIra Weiny  *
129e1b51868SIra Weiny  * Unregistering an auxiliary_device is a two-step process to mirror the
130e1b51868SIra Weiny  * register process.  First call auxiliary_device_delete(), then call
131e1b51868SIra Weiny  * auxiliary_device_uninit().
132e1b51868SIra Weiny  *
133e1b51868SIra Weiny  * .. code-block:: c
134e1b51868SIra Weiny  *
135e1b51868SIra Weiny  *         auxiliary_device_delete(my_dev->my_aux_dev);
136e1b51868SIra Weiny  *         auxiliary_device_uninit(my_dev->my_aux_dev);
137e1b51868SIra Weiny  */
1387de3697eSDave Ertman struct auxiliary_device {
1397de3697eSDave Ertman 	struct device dev;
1407de3697eSDave Ertman 	const char *name;
1417de3697eSDave Ertman 	u32 id;
1427de3697eSDave Ertman };
1437de3697eSDave Ertman 
144e1b51868SIra Weiny /**
145e1b51868SIra Weiny  * struct auxiliary_driver - Definition of an auxiliary bus driver
146e1b51868SIra Weiny  * @probe: Called when a matching device is added to the bus.
147e1b51868SIra Weiny  * @remove: Called when device is removed from the bus.
148e1b51868SIra Weiny  * @shutdown: Called at shut-down time to quiesce the device.
149e1b51868SIra Weiny  * @suspend: Called to put the device to sleep mode. Usually to a power state.
150e1b51868SIra Weiny  * @resume: Called to bring a device from sleep mode.
151e1b51868SIra Weiny  * @name: Driver name.
152e1b51868SIra Weiny  * @driver: Core driver structure.
153e1b51868SIra Weiny  * @id_table: Table of devices this driver should match on the bus.
154e1b51868SIra Weiny  *
155e1b51868SIra Weiny  * Auxiliary drivers follow the standard driver model convention, where
156e1b51868SIra Weiny  * discovery/enumeration is handled by the core, and drivers provide probe()
157e1b51868SIra Weiny  * and remove() methods. They support power management and shutdown
158e1b51868SIra Weiny  * notifications using the standard conventions.
159e1b51868SIra Weiny  *
160e1b51868SIra Weiny  * Auxiliary drivers register themselves with the bus by calling
161e1b51868SIra Weiny  * auxiliary_driver_register(). The id_table contains the match_names of
162e1b51868SIra Weiny  * auxiliary devices that a driver can bind with.
163e1b51868SIra Weiny  *
164e1b51868SIra Weiny  * .. code-block:: c
165e1b51868SIra Weiny  *
166e1b51868SIra Weiny  *         static const struct auxiliary_device_id my_auxiliary_id_table[] = {
167e1b51868SIra Weiny  *		   { .name = "foo_mod.foo_dev" },
168e1b51868SIra Weiny  *                 {},
169e1b51868SIra Weiny  *         };
170e1b51868SIra Weiny  *
171e1b51868SIra Weiny  *         MODULE_DEVICE_TABLE(auxiliary, my_auxiliary_id_table);
172e1b51868SIra Weiny  *
173e1b51868SIra Weiny  *         struct auxiliary_driver my_drv = {
174e1b51868SIra Weiny  *                 .name = "myauxiliarydrv",
175e1b51868SIra Weiny  *                 .id_table = my_auxiliary_id_table,
176e1b51868SIra Weiny  *                 .probe = my_drv_probe,
177e1b51868SIra Weiny  *                 .remove = my_drv_remove
178e1b51868SIra Weiny  *         };
179e1b51868SIra Weiny  */
1807de3697eSDave Ertman struct auxiliary_driver {
1817de3697eSDave Ertman 	int (*probe)(struct auxiliary_device *auxdev, const struct auxiliary_device_id *id);
1828142a46cSGreg Kroah-Hartman 	void (*remove)(struct auxiliary_device *auxdev);
1837de3697eSDave Ertman 	void (*shutdown)(struct auxiliary_device *auxdev);
1847de3697eSDave Ertman 	int (*suspend)(struct auxiliary_device *auxdev, pm_message_t state);
1857de3697eSDave Ertman 	int (*resume)(struct auxiliary_device *auxdev);
1867de3697eSDave Ertman 	const char *name;
1877de3697eSDave Ertman 	struct device_driver driver;
1887de3697eSDave Ertman 	const struct auxiliary_device_id *id_table;
1897de3697eSDave Ertman };
1907de3697eSDave Ertman 
auxiliary_get_drvdata(struct auxiliary_device * auxdev)191*365481e4SDavid E. Box static inline void *auxiliary_get_drvdata(struct auxiliary_device *auxdev)
192*365481e4SDavid E. Box {
193*365481e4SDavid E. Box 	return dev_get_drvdata(&auxdev->dev);
194*365481e4SDavid E. Box }
195*365481e4SDavid E. Box 
auxiliary_set_drvdata(struct auxiliary_device * auxdev,void * data)196*365481e4SDavid E. Box static inline void auxiliary_set_drvdata(struct auxiliary_device *auxdev, void *data)
197*365481e4SDavid E. Box {
198*365481e4SDavid E. Box 	dev_set_drvdata(&auxdev->dev, data);
199*365481e4SDavid E. Box }
200*365481e4SDavid E. Box 
to_auxiliary_dev(struct device * dev)2017de3697eSDave Ertman static inline struct auxiliary_device *to_auxiliary_dev(struct device *dev)
2027de3697eSDave Ertman {
2037de3697eSDave Ertman 	return container_of(dev, struct auxiliary_device, dev);
2047de3697eSDave Ertman }
2057de3697eSDave Ertman 
to_auxiliary_drv(struct device_driver * drv)2067de3697eSDave Ertman static inline struct auxiliary_driver *to_auxiliary_drv(struct device_driver *drv)
2077de3697eSDave Ertman {
2087de3697eSDave Ertman 	return container_of(drv, struct auxiliary_driver, driver);
2097de3697eSDave Ertman }
2107de3697eSDave Ertman 
2117de3697eSDave Ertman int auxiliary_device_init(struct auxiliary_device *auxdev);
2127de3697eSDave Ertman int __auxiliary_device_add(struct auxiliary_device *auxdev, const char *modname);
2137de3697eSDave Ertman #define auxiliary_device_add(auxdev) __auxiliary_device_add(auxdev, KBUILD_MODNAME)
2147de3697eSDave Ertman 
auxiliary_device_uninit(struct auxiliary_device * auxdev)2157de3697eSDave Ertman static inline void auxiliary_device_uninit(struct auxiliary_device *auxdev)
2167de3697eSDave Ertman {
2177de3697eSDave Ertman 	put_device(&auxdev->dev);
2187de3697eSDave Ertman }
2197de3697eSDave Ertman 
auxiliary_device_delete(struct auxiliary_device * auxdev)2207de3697eSDave Ertman static inline void auxiliary_device_delete(struct auxiliary_device *auxdev)
2217de3697eSDave Ertman {
2227de3697eSDave Ertman 	device_del(&auxdev->dev);
2237de3697eSDave Ertman }
2247de3697eSDave Ertman 
2257de3697eSDave Ertman int __auxiliary_driver_register(struct auxiliary_driver *auxdrv, struct module *owner,
2267de3697eSDave Ertman 				const char *modname);
2277de3697eSDave Ertman #define auxiliary_driver_register(auxdrv) \
2287de3697eSDave Ertman 	__auxiliary_driver_register(auxdrv, THIS_MODULE, KBUILD_MODNAME)
2297de3697eSDave Ertman 
2307de3697eSDave Ertman void auxiliary_driver_unregister(struct auxiliary_driver *auxdrv);
2317de3697eSDave Ertman 
2327de3697eSDave Ertman /**
2337de3697eSDave Ertman  * module_auxiliary_driver() - Helper macro for registering an auxiliary driver
2347de3697eSDave Ertman  * @__auxiliary_driver: auxiliary driver struct
2357de3697eSDave Ertman  *
2367de3697eSDave Ertman  * Helper macro for auxiliary drivers which do not do anything special in
2377de3697eSDave Ertman  * module init/exit. This eliminates a lot of boilerplate. Each module may only
2387de3697eSDave Ertman  * use this macro once, and calling it replaces module_init() and module_exit()
23914866a7dSIra Weiny  *
24014866a7dSIra Weiny  * .. code-block:: c
24114866a7dSIra Weiny  *
24214866a7dSIra Weiny  *	module_auxiliary_driver(my_drv);
2437de3697eSDave Ertman  */
2447de3697eSDave Ertman #define module_auxiliary_driver(__auxiliary_driver) \
2457de3697eSDave Ertman 	module_driver(__auxiliary_driver, auxiliary_driver_register, auxiliary_driver_unregister)
2467de3697eSDave Ertman 
2470d2bf11aSGreg Kroah-Hartman struct auxiliary_device *auxiliary_find_device(struct device *start,
2480d2bf11aSGreg Kroah-Hartman 					       const void *data,
2497de3697eSDave Ertman 					       int (*match)(struct device *dev, const void *data));
2507de3697eSDave Ertman 
2517de3697eSDave Ertman #endif /* _AUXILIARY_BUS_H_ */
252