![]() |
|
|||
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 #define pr_fmt(fmt) "%s:%s: " fmt, KBUILD_MODNAME, __func__ 0009 0010 #include <linux/device.h> 0011 #include <linux/init.h> 0012 #include <linux/slab.h> 0013 #include <linux/module.h> 0014 #include <linux/pm_domain.h> 0015 #include <linux/pm_runtime.h> 0016 #include <linux/string.h> 0017 #include <linux/auxiliary_bus.h> 0018 #include "base.h" 0019 0020 /** 0021 * DOC: PURPOSE 0022 * 0023 * In some subsystems, the functionality of the core device (PCI/ACPI/other) is 0024 * too complex for a single device to be managed by a monolithic driver (e.g. 0025 * Sound Open Firmware), multiple devices might implement a common intersection 0026 * of functionality (e.g. NICs + RDMA), or a driver may want to export an 0027 * interface for another subsystem to drive (e.g. SIOV Physical Function export 0028 * Virtual Function management). A split of the functionality into child- 0029 * devices representing sub-domains of functionality makes it possible to 0030 * compartmentalize, layer, and distribute domain-specific concerns via a Linux 0031 * device-driver model. 0032 * 0033 * An example for this kind of requirement is the audio subsystem where a 0034 * single IP is handling multiple entities such as HDMI, Soundwire, local 0035 * devices such as mics/speakers etc. The split for the core's functionality 0036 * can be arbitrary or be defined by the DSP firmware topology and include 0037 * hooks for test/debug. This allows for the audio core device to be minimal 0038 * and focused on hardware-specific control and communication. 0039 * 0040 * Each auxiliary_device represents a part of its parent functionality. The 0041 * generic behavior can be extended and specialized as needed by encapsulating 0042 * an auxiliary_device within other domain-specific structures and the use of 0043 * .ops callbacks. Devices on the auxiliary bus do not share any structures and 0044 * the use of a communication channel with the parent is domain-specific. 0045 * 0046 * Note that ops are intended as a way to augment instance behavior within a 0047 * class of auxiliary devices, it is not the mechanism for exporting common 0048 * infrastructure from the parent. Consider EXPORT_SYMBOL_NS() to convey 0049 * infrastructure from the parent module to the auxiliary module(s). 0050 */ 0051 0052 /** 0053 * DOC: USAGE 0054 * 0055 * The auxiliary bus is to be used when a driver and one or more kernel 0056 * modules, who share a common header file with the driver, need a mechanism to 0057 * connect and provide access to a shared object allocated by the 0058 * auxiliary_device's registering driver. The registering driver for the 0059 * auxiliary_device(s) and the kernel module(s) registering auxiliary_drivers 0060 * can be from the same subsystem, or from multiple subsystems. 0061 * 0062 * The emphasis here is on a common generic interface that keeps subsystem 0063 * customization out of the bus infrastructure. 0064 * 0065 * One example is a PCI network device that is RDMA-capable and exports a child 0066 * device to be driven by an auxiliary_driver in the RDMA subsystem. The PCI 0067 * driver allocates and registers an auxiliary_device for each physical 0068 * function on the NIC. The RDMA driver registers an auxiliary_driver that 0069 * claims each of these auxiliary_devices. This conveys data/ops published by 0070 * the parent PCI device/driver to the RDMA auxiliary_driver. 0071 * 0072 * Another use case is for the PCI device to be split out into multiple sub 0073 * functions. For each sub function an auxiliary_device is created. A PCI sub 0074 * function driver binds to such devices that creates its own one or more class 0075 * devices. A PCI sub function auxiliary device is likely to be contained in a 0076 * struct with additional attributes such as user defined sub function number 0077 * and optional attributes such as resources and a link to the parent device. 0078 * These attributes could be used by systemd/udev; and hence should be 0079 * initialized before a driver binds to an auxiliary_device. 0080 * 0081 * A key requirement for utilizing the auxiliary bus is that there is no 0082 * dependency on a physical bus, device, register accesses or regmap support. 0083 * These individual devices split from the core cannot live on the platform bus 0084 * as they are not physical devices that are controlled by DT/ACPI. The same 0085 * argument applies for not using MFD in this scenario as MFD relies on 0086 * individual function devices being physical devices. 0087 */ 0088 0089 /** 0090 * DOC: EXAMPLE 0091 * 0092 * Auxiliary devices are created and registered by a subsystem-level core 0093 * device that needs to break up its functionality into smaller fragments. One 0094 * way to extend the scope of an auxiliary_device is to encapsulate it within a 0095 * domain- pecific structure defined by the parent device. This structure 0096 * contains the auxiliary_device and any associated shared data/callbacks 0097 * needed to establish the connection with the parent. 0098 * 0099 * An example is: 0100 * 0101 * .. code-block:: c 0102 * 0103 * struct foo { 0104 * struct auxiliary_device auxdev; 0105 * void (*connect)(struct auxiliary_device *auxdev); 0106 * void (*disconnect)(struct auxiliary_device *auxdev); 0107 * void *data; 0108 * }; 0109 * 0110 * The parent device then registers the auxiliary_device by calling 0111 * auxiliary_device_init(), and then auxiliary_device_add(), with the pointer 0112 * to the auxdev member of the above structure. The parent provides a name for 0113 * the auxiliary_device that, combined with the parent's KBUILD_MODNAME, 0114 * creates a match_name that is be used for matching and binding with a driver. 0115 * 0116 * Whenever an auxiliary_driver is registered, based on the match_name, the 0117 * auxiliary_driver's probe() is invoked for the matching devices. The 0118 * auxiliary_driver can also be encapsulated inside custom drivers that make 0119 * the core device's functionality extensible by adding additional 0120 * domain-specific ops as follows: 0121 * 0122 * .. code-block:: c 0123 * 0124 * struct my_ops { 0125 * void (*send)(struct auxiliary_device *auxdev); 0126 * void (*receive)(struct auxiliary_device *auxdev); 0127 * }; 0128 * 0129 * 0130 * struct my_driver { 0131 * struct auxiliary_driver auxiliary_drv; 0132 * const struct my_ops ops; 0133 * }; 0134 * 0135 * An example of this type of usage is: 0136 * 0137 * .. code-block:: c 0138 * 0139 * const struct auxiliary_device_id my_auxiliary_id_table[] = { 0140 * { .name = "foo_mod.foo_dev" }, 0141 * { }, 0142 * }; 0143 * 0144 * const struct my_ops my_custom_ops = { 0145 * .send = my_tx, 0146 * .receive = my_rx, 0147 * }; 0148 * 0149 * const struct my_driver my_drv = { 0150 * .auxiliary_drv = { 0151 * .name = "myauxiliarydrv", 0152 * .id_table = my_auxiliary_id_table, 0153 * .probe = my_probe, 0154 * .remove = my_remove, 0155 * .shutdown = my_shutdown, 0156 * }, 0157 * .ops = my_custom_ops, 0158 * }; 0159 */ 0160 0161 static const struct auxiliary_device_id *auxiliary_match_id(const struct auxiliary_device_id *id, 0162 const struct auxiliary_device *auxdev) 0163 { 0164 for (; id->name[0]; id++) { 0165 const char *p = strrchr(dev_name(&auxdev->dev), '.'); 0166 int match_size; 0167 0168 if (!p) 0169 continue; 0170 match_size = p - dev_name(&auxdev->dev); 0171 0172 /* use dev_name(&auxdev->dev) prefix before last '.' char to match to */ 0173 if (strlen(id->name) == match_size && 0174 !strncmp(dev_name(&auxdev->dev), id->name, match_size)) 0175 return id; 0176 } 0177 return NULL; 0178 } 0179 0180 static int auxiliary_match(struct device *dev, struct device_driver *drv) 0181 { 0182 struct auxiliary_device *auxdev = to_auxiliary_dev(dev); 0183 struct auxiliary_driver *auxdrv = to_auxiliary_drv(drv); 0184 0185 return !!auxiliary_match_id(auxdrv->id_table, auxdev); 0186 } 0187 0188 static int auxiliary_uevent(struct device *dev, struct kobj_uevent_env *env) 0189 { 0190 const char *name, *p; 0191 0192 name = dev_name(dev); 0193 p = strrchr(name, '.'); 0194 0195 return add_uevent_var(env, "MODALIAS=%s%.*s", AUXILIARY_MODULE_PREFIX, 0196 (int)(p - name), name); 0197 } 0198 0199 static const struct dev_pm_ops auxiliary_dev_pm_ops = { 0200 SET_RUNTIME_PM_OPS(pm_generic_runtime_suspend, pm_generic_runtime_resume, NULL) 0201 SET_SYSTEM_SLEEP_PM_OPS(pm_generic_suspend, pm_generic_resume) 0202 }; 0203 0204 static int auxiliary_bus_probe(struct device *dev) 0205 { 0206 struct auxiliary_driver *auxdrv = to_auxiliary_drv(dev->driver); 0207 struct auxiliary_device *auxdev = to_auxiliary_dev(dev); 0208 int ret; 0209 0210 ret = dev_pm_domain_attach(dev, true); 0211 if (ret) { 0212 dev_warn(dev, "Failed to attach to PM Domain : %d\n", ret); 0213 return ret; 0214 } 0215 0216 ret = auxdrv->probe(auxdev, auxiliary_match_id(auxdrv->id_table, auxdev)); 0217 if (ret) 0218 dev_pm_domain_detach(dev, true); 0219 0220 return ret; 0221 } 0222 0223 static void auxiliary_bus_remove(struct device *dev) 0224 { 0225 struct auxiliary_driver *auxdrv = to_auxiliary_drv(dev->driver); 0226 struct auxiliary_device *auxdev = to_auxiliary_dev(dev); 0227 0228 if (auxdrv->remove) 0229 auxdrv->remove(auxdev); 0230 dev_pm_domain_detach(dev, true); 0231 } 0232 0233 static void auxiliary_bus_shutdown(struct device *dev) 0234 { 0235 struct auxiliary_driver *auxdrv = NULL; 0236 struct auxiliary_device *auxdev; 0237 0238 if (dev->driver) { 0239 auxdrv = to_auxiliary_drv(dev->driver); 0240 auxdev = to_auxiliary_dev(dev); 0241 } 0242 0243 if (auxdrv && auxdrv->shutdown) 0244 auxdrv->shutdown(auxdev); 0245 } 0246 0247 static struct bus_type auxiliary_bus_type = { 0248 .name = "auxiliary", 0249 .probe = auxiliary_bus_probe, 0250 .remove = auxiliary_bus_remove, 0251 .shutdown = auxiliary_bus_shutdown, 0252 .match = auxiliary_match, 0253 .uevent = auxiliary_uevent, 0254 .pm = &auxiliary_dev_pm_ops, 0255 }; 0256 0257 /** 0258 * auxiliary_device_init - check auxiliary_device and initialize 0259 * @auxdev: auxiliary device struct 0260 * 0261 * This is the second step in the three-step process to register an 0262 * auxiliary_device. 0263 * 0264 * When this function returns an error code, then the device_initialize will 0265 * *not* have been performed, and the caller will be responsible to free any 0266 * memory allocated for the auxiliary_device in the error path directly. 0267 * 0268 * It returns 0 on success. On success, the device_initialize has been 0269 * performed. After this point any error unwinding will need to include a call 0270 * to auxiliary_device_uninit(). In this post-initialize error scenario, a call 0271 * to the device's .release callback will be triggered, and all memory clean-up 0272 * is expected to be handled there. 0273 */ 0274 int auxiliary_device_init(struct auxiliary_device *auxdev) 0275 { 0276 struct device *dev = &auxdev->dev; 0277 0278 if (!dev->parent) { 0279 pr_err("auxiliary_device has a NULL dev->parent\n"); 0280 return -EINVAL; 0281 } 0282 0283 if (!auxdev->name) { 0284 pr_err("auxiliary_device has a NULL name\n"); 0285 return -EINVAL; 0286 } 0287 0288 dev->bus = &auxiliary_bus_type; 0289 device_initialize(&auxdev->dev); 0290 return 0; 0291 } 0292 EXPORT_SYMBOL_GPL(auxiliary_device_init); 0293 0294 /** 0295 * __auxiliary_device_add - add an auxiliary bus device 0296 * @auxdev: auxiliary bus device to add to the bus 0297 * @modname: name of the parent device's driver module 0298 * 0299 * This is the third step in the three-step process to register an 0300 * auxiliary_device. 0301 * 0302 * This function must be called after a successful call to 0303 * auxiliary_device_init(), which will perform the device_initialize. This 0304 * means that if this returns an error code, then a call to 0305 * auxiliary_device_uninit() must be performed so that the .release callback 0306 * will be triggered to free the memory associated with the auxiliary_device. 0307 * 0308 * The expectation is that users will call the "auxiliary_device_add" macro so 0309 * that the caller's KBUILD_MODNAME is automatically inserted for the modname 0310 * parameter. Only if a user requires a custom name would this version be 0311 * called directly. 0312 */ 0313 int __auxiliary_device_add(struct auxiliary_device *auxdev, const char *modname) 0314 { 0315 struct device *dev = &auxdev->dev; 0316 int ret; 0317 0318 if (!modname) { 0319 dev_err(dev, "auxiliary device modname is NULL\n"); 0320 return -EINVAL; 0321 } 0322 0323 ret = dev_set_name(dev, "%s.%s.%d", modname, auxdev->name, auxdev->id); 0324 if (ret) { 0325 dev_err(dev, "auxiliary device dev_set_name failed: %d\n", ret); 0326 return ret; 0327 } 0328 0329 ret = device_add(dev); 0330 if (ret) 0331 dev_err(dev, "adding auxiliary device failed!: %d\n", ret); 0332 0333 return ret; 0334 } 0335 EXPORT_SYMBOL_GPL(__auxiliary_device_add); 0336 0337 /** 0338 * auxiliary_find_device - auxiliary device iterator for locating a particular device. 0339 * @start: Device to begin with 0340 * @data: Data to pass to match function 0341 * @match: Callback function to check device 0342 * 0343 * This function returns a reference to a device that is 'found' 0344 * for later use, as determined by the @match callback. 0345 * 0346 * The reference returned should be released with put_device(). 0347 * 0348 * The callback should return 0 if the device doesn't match and non-zero 0349 * if it does. If the callback returns non-zero, this function will 0350 * return to the caller and not iterate over any more devices. 0351 */ 0352 struct auxiliary_device *auxiliary_find_device(struct device *start, 0353 const void *data, 0354 int (*match)(struct device *dev, const void *data)) 0355 { 0356 struct device *dev; 0357 0358 dev = bus_find_device(&auxiliary_bus_type, start, data, match); 0359 if (!dev) 0360 return NULL; 0361 0362 return to_auxiliary_dev(dev); 0363 } 0364 EXPORT_SYMBOL_GPL(auxiliary_find_device); 0365 0366 /** 0367 * __auxiliary_driver_register - register a driver for auxiliary bus devices 0368 * @auxdrv: auxiliary_driver structure 0369 * @owner: owning module/driver 0370 * @modname: KBUILD_MODNAME for parent driver 0371 * 0372 * The expectation is that users will call the "auxiliary_driver_register" 0373 * macro so that the caller's KBUILD_MODNAME is automatically inserted for the 0374 * modname parameter. Only if a user requires a custom name would this version 0375 * be called directly. 0376 */ 0377 int __auxiliary_driver_register(struct auxiliary_driver *auxdrv, 0378 struct module *owner, const char *modname) 0379 { 0380 int ret; 0381 0382 if (WARN_ON(!auxdrv->probe) || WARN_ON(!auxdrv->id_table)) 0383 return -EINVAL; 0384 0385 if (auxdrv->name) 0386 auxdrv->driver.name = kasprintf(GFP_KERNEL, "%s.%s", modname, 0387 auxdrv->name); 0388 else 0389 auxdrv->driver.name = kasprintf(GFP_KERNEL, "%s", modname); 0390 if (!auxdrv->driver.name) 0391 return -ENOMEM; 0392 0393 auxdrv->driver.owner = owner; 0394 auxdrv->driver.bus = &auxiliary_bus_type; 0395 auxdrv->driver.mod_name = modname; 0396 0397 ret = driver_register(&auxdrv->driver); 0398 if (ret) 0399 kfree(auxdrv->driver.name); 0400 0401 return ret; 0402 } 0403 EXPORT_SYMBOL_GPL(__auxiliary_driver_register); 0404 0405 /** 0406 * auxiliary_driver_unregister - unregister a driver 0407 * @auxdrv: auxiliary_driver structure 0408 */ 0409 void auxiliary_driver_unregister(struct auxiliary_driver *auxdrv) 0410 { 0411 driver_unregister(&auxdrv->driver); 0412 kfree(auxdrv->driver.name); 0413 } 0414 EXPORT_SYMBOL_GPL(auxiliary_driver_unregister); 0415 0416 void __init auxiliary_bus_init(void) 0417 { 0418 WARN_ON(bus_register(&auxiliary_bus_type)); 0419 }
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.1.0 LXR engine. The LXR team |
![]() ![]() |