Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-only */
0002 /*
0003  * Copyright (c) 2019-2020 Intel Corporation
0004  *
0005  * Please see Documentation/driver-api/auxiliary_bus.rst for more information.
0006  */
0007 
0008 #ifndef _AUXILIARY_BUS_H_
0009 #define _AUXILIARY_BUS_H_
0010 
0011 #include <linux/device.h>
0012 #include <linux/mod_devicetable.h>
0013 
0014 /**
0015  * DOC: DEVICE_LIFESPAN
0016  *
0017  * The registering driver is the entity that allocates memory for the
0018  * auxiliary_device and registers it on the auxiliary bus.  It is important to
0019  * note that, as opposed to the platform bus, the registering driver is wholly
0020  * responsible for the management of the memory used for the device object.
0021  *
0022  * To be clear the memory for the auxiliary_device is freed in the release()
0023  * callback defined by the registering driver.  The registering driver should
0024  * only call auxiliary_device_delete() and then auxiliary_device_uninit() when
0025  * it is done with the device.  The release() function is then automatically
0026  * called if and when other code releases their reference to the devices.
0027  *
0028  * A parent object, defined in the shared header file, contains the
0029  * auxiliary_device.  It also contains a pointer to the shared object(s), which
0030  * also is defined in the shared header.  Both the parent object and the shared
0031  * object(s) are allocated by the registering driver.  This layout allows the
0032  * auxiliary_driver's registering module to perform a container_of() call to go
0033  * from the pointer to the auxiliary_device, that is passed during the call to
0034  * the auxiliary_driver's probe function, up to the parent object, and then
0035  * have access to the shared object(s).
0036  *
0037  * The memory for the shared object(s) must have a lifespan equal to, or
0038  * greater than, the lifespan of the memory for the auxiliary_device.  The
0039  * auxiliary_driver should only consider that the shared object is valid as
0040  * long as the auxiliary_device is still registered on the auxiliary bus.  It
0041  * is up to the registering driver to manage (e.g. free or keep available) the
0042  * memory for the shared object beyond the life of the auxiliary_device.
0043  *
0044  * The registering driver must unregister all auxiliary devices before its own
0045  * driver.remove() is completed.  An easy way to ensure this is to use the
0046  * devm_add_action_or_reset() call to register a function against the parent
0047  * device which unregisters the auxiliary device object(s).
0048  *
0049  * Finally, any operations which operate on the auxiliary devices must continue
0050  * to function (if only to return an error) after the registering driver
0051  * unregisters the auxiliary device.
0052  */
0053 
0054 /**
0055  * struct auxiliary_device - auxiliary device object.
0056  * @dev: Device,
0057  *       The release and parent fields of the device structure must be filled
0058  *       in
0059  * @name: Match name found by the auxiliary device driver,
0060  * @id: unique identitier if multiple devices of the same name are exported,
0061  *
0062  * An auxiliary_device represents a part of its parent device's functionality.
0063  * It is given a name that, combined with the registering drivers
0064  * KBUILD_MODNAME, creates a match_name that is used for driver binding, and an
0065  * id that combined with the match_name provide a unique name to register with
0066  * the bus subsystem.  For example, a driver registering an auxiliary device is
0067  * named 'foo_mod.ko' and the subdevice is named 'foo_dev'.  The match name is
0068  * therefore 'foo_mod.foo_dev'.
0069  *
0070  * Registering an auxiliary_device is a three-step process.
0071  *
0072  * First, a 'struct auxiliary_device' needs to be defined or allocated for each
0073  * sub-device desired.  The name, id, dev.release, and dev.parent fields of
0074  * this structure must be filled in as follows.
0075  *
0076  * The 'name' field is to be given a name that is recognized by the auxiliary
0077  * driver.  If two auxiliary_devices with the same match_name, eg
0078  * "foo_mod.foo_dev", are registered onto the bus, they must have unique id
0079  * values (e.g. "x" and "y") so that the registered devices names are
0080  * "foo_mod.foo_dev.x" and "foo_mod.foo_dev.y".  If match_name + id are not
0081  * unique, then the device_add fails and generates an error message.
0082  *
0083  * The auxiliary_device.dev.type.release or auxiliary_device.dev.release must
0084  * be populated with a non-NULL pointer to successfully register the
0085  * auxiliary_device.  This release call is where resources associated with the
0086  * auxiliary device must be free'ed.  Because once the device is placed on the
0087  * bus the parent driver can not tell what other code may have a reference to
0088  * this data.
0089  *
0090  * The auxiliary_device.dev.parent should be set.  Typically to the registering
0091  * drivers device.
0092  *
0093  * Second, call auxiliary_device_init(), which checks several aspects of the
0094  * auxiliary_device struct and performs a device_initialize().  After this step
0095  * completes, any error state must have a call to auxiliary_device_uninit() in
0096  * its resolution path.
0097  *
0098  * The third and final step in registering an auxiliary_device is to perform a
0099  * call to auxiliary_device_add(), which sets the name of the device and adds
0100  * the device to the bus.
0101  *
0102  * .. code-block:: c
0103  *
0104  *      #define MY_DEVICE_NAME "foo_dev"
0105  *
0106  *      ...
0107  *
0108  *  struct auxiliary_device *my_aux_dev = my_aux_dev_alloc(xxx);
0109  *
0110  *  // Step 1:
0111  *  my_aux_dev->name = MY_DEVICE_NAME;
0112  *  my_aux_dev->id = my_unique_id_alloc(xxx);
0113  *  my_aux_dev->dev.release = my_aux_dev_release;
0114  *  my_aux_dev->dev.parent = my_dev;
0115  *
0116  *  // Step 2:
0117  *  if (auxiliary_device_init(my_aux_dev))
0118  *      goto fail;
0119  *
0120  *  // Step 3:
0121  *  if (auxiliary_device_add(my_aux_dev)) {
0122  *      auxiliary_device_uninit(my_aux_dev);
0123  *      goto fail;
0124  *  }
0125  *
0126  *  ...
0127  *
0128  *
0129  * Unregistering an auxiliary_device is a two-step process to mirror the
0130  * register process.  First call auxiliary_device_delete(), then call
0131  * auxiliary_device_uninit().
0132  *
0133  * .. code-block:: c
0134  *
0135  *         auxiliary_device_delete(my_dev->my_aux_dev);
0136  *         auxiliary_device_uninit(my_dev->my_aux_dev);
0137  */
0138 struct auxiliary_device {
0139     struct device dev;
0140     const char *name;
0141     u32 id;
0142 };
0143 
0144 /**
0145  * struct auxiliary_driver - Definition of an auxiliary bus driver
0146  * @probe: Called when a matching device is added to the bus.
0147  * @remove: Called when device is removed from the bus.
0148  * @shutdown: Called at shut-down time to quiesce the device.
0149  * @suspend: Called to put the device to sleep mode. Usually to a power state.
0150  * @resume: Called to bring a device from sleep mode.
0151  * @name: Driver name.
0152  * @driver: Core driver structure.
0153  * @id_table: Table of devices this driver should match on the bus.
0154  *
0155  * Auxiliary drivers follow the standard driver model convention, where
0156  * discovery/enumeration is handled by the core, and drivers provide probe()
0157  * and remove() methods. They support power management and shutdown
0158  * notifications using the standard conventions.
0159  *
0160  * Auxiliary drivers register themselves with the bus by calling
0161  * auxiliary_driver_register(). The id_table contains the match_names of
0162  * auxiliary devices that a driver can bind with.
0163  *
0164  * .. code-block:: c
0165  *
0166  *         static const struct auxiliary_device_id my_auxiliary_id_table[] = {
0167  *         { .name = "foo_mod.foo_dev" },
0168  *                 {},
0169  *         };
0170  *
0171  *         MODULE_DEVICE_TABLE(auxiliary, my_auxiliary_id_table);
0172  *
0173  *         struct auxiliary_driver my_drv = {
0174  *                 .name = "myauxiliarydrv",
0175  *                 .id_table = my_auxiliary_id_table,
0176  *                 .probe = my_drv_probe,
0177  *                 .remove = my_drv_remove
0178  *         };
0179  */
0180 struct auxiliary_driver {
0181     int (*probe)(struct auxiliary_device *auxdev, const struct auxiliary_device_id *id);
0182     void (*remove)(struct auxiliary_device *auxdev);
0183     void (*shutdown)(struct auxiliary_device *auxdev);
0184     int (*suspend)(struct auxiliary_device *auxdev, pm_message_t state);
0185     int (*resume)(struct auxiliary_device *auxdev);
0186     const char *name;
0187     struct device_driver driver;
0188     const struct auxiliary_device_id *id_table;
0189 };
0190 
0191 static inline void *auxiliary_get_drvdata(struct auxiliary_device *auxdev)
0192 {
0193     return dev_get_drvdata(&auxdev->dev);
0194 }
0195 
0196 static inline void auxiliary_set_drvdata(struct auxiliary_device *auxdev, void *data)
0197 {
0198     dev_set_drvdata(&auxdev->dev, data);
0199 }
0200 
0201 static inline struct auxiliary_device *to_auxiliary_dev(struct device *dev)
0202 {
0203     return container_of(dev, struct auxiliary_device, dev);
0204 }
0205 
0206 static inline struct auxiliary_driver *to_auxiliary_drv(struct device_driver *drv)
0207 {
0208     return container_of(drv, struct auxiliary_driver, driver);
0209 }
0210 
0211 int auxiliary_device_init(struct auxiliary_device *auxdev);
0212 int __auxiliary_device_add(struct auxiliary_device *auxdev, const char *modname);
0213 #define auxiliary_device_add(auxdev) __auxiliary_device_add(auxdev, KBUILD_MODNAME)
0214 
0215 static inline void auxiliary_device_uninit(struct auxiliary_device *auxdev)
0216 {
0217     put_device(&auxdev->dev);
0218 }
0219 
0220 static inline void auxiliary_device_delete(struct auxiliary_device *auxdev)
0221 {
0222     device_del(&auxdev->dev);
0223 }
0224 
0225 int __auxiliary_driver_register(struct auxiliary_driver *auxdrv, struct module *owner,
0226                 const char *modname);
0227 #define auxiliary_driver_register(auxdrv) \
0228     __auxiliary_driver_register(auxdrv, THIS_MODULE, KBUILD_MODNAME)
0229 
0230 void auxiliary_driver_unregister(struct auxiliary_driver *auxdrv);
0231 
0232 /**
0233  * module_auxiliary_driver() - Helper macro for registering an auxiliary driver
0234  * @__auxiliary_driver: auxiliary driver struct
0235  *
0236  * Helper macro for auxiliary drivers which do not do anything special in
0237  * module init/exit. This eliminates a lot of boilerplate. Each module may only
0238  * use this macro once, and calling it replaces module_init() and module_exit()
0239  *
0240  * .. code-block:: c
0241  *
0242  *  module_auxiliary_driver(my_drv);
0243  */
0244 #define module_auxiliary_driver(__auxiliary_driver) \
0245     module_driver(__auxiliary_driver, auxiliary_driver_register, auxiliary_driver_unregister)
0246 
0247 struct auxiliary_device *auxiliary_find_device(struct device *start,
0248                            const void *data,
0249                            int (*match)(struct device *dev, const void *data));
0250 
0251 #endif /* _AUXILIARY_BUS_H_ */