// SPDX-License-Identifier: GPL-2.0-only1/*2* Copyright (c) 2019-2020 Intel Corporation3*4* Please see Documentation/driver-api/auxiliary_bus.rst for more information.5*/67#define pr_fmt(fmt) "%s:%s: " fmt, KBUILD_MODNAME, __func__89#include <linux/device.h>10#include <linux/init.h>11#include <linux/slab.h>12#include <linux/module.h>13#include <linux/pm_domain.h>14#include <linux/pm_runtime.h>15#include <linux/string.h>16#include <linux/auxiliary_bus.h>17#include "base.h"1819/**20* DOC: PURPOSE21*22* In some subsystems, the functionality of the core device (PCI/ACPI/other) is23* too complex for a single device to be managed by a monolithic driver (e.g.24* Sound Open Firmware), multiple devices might implement a common intersection25* of functionality (e.g. NICs + RDMA), or a driver may want to export an26* interface for another subsystem to drive (e.g. SIOV Physical Function export27* Virtual Function management). A split of the functionality into child-28* devices representing sub-domains of functionality makes it possible to29* compartmentalize, layer, and distribute domain-specific concerns via a Linux30* device-driver model.31*32* An example for this kind of requirement is the audio subsystem where a33* single IP is handling multiple entities such as HDMI, Soundwire, local34* devices such as mics/speakers etc. The split for the core's functionality35* can be arbitrary or be defined by the DSP firmware topology and include36* hooks for test/debug. This allows for the audio core device to be minimal37* and focused on hardware-specific control and communication.38*39* Each auxiliary_device represents a part of its parent functionality. The40* generic behavior can be extended and specialized as needed by encapsulating41* an auxiliary_device within other domain-specific structures and the use of42* .ops callbacks. Devices on the auxiliary bus do not share any structures and43* the use of a communication channel with the parent is domain-specific.44*45* Note that ops are intended as a way to augment instance behavior within a46* class of auxiliary devices, it is not the mechanism for exporting common47* infrastructure from the parent. Consider EXPORT_SYMBOL_NS() to convey48* infrastructure from the parent module to the auxiliary module(s).49*/5051/**52* DOC: USAGE53*54* The auxiliary bus is to be used when a driver and one or more kernel55* modules, who share a common header file with the driver, need a mechanism to56* connect and provide access to a shared object allocated by the57* auxiliary_device's registering driver. The registering driver for the58* auxiliary_device(s) and the kernel module(s) registering auxiliary_drivers59* can be from the same subsystem, or from multiple subsystems.60*61* The emphasis here is on a common generic interface that keeps subsystem62* customization out of the bus infrastructure.63*64* One example is a PCI network device that is RDMA-capable and exports a child65* device to be driven by an auxiliary_driver in the RDMA subsystem. The PCI66* driver allocates and registers an auxiliary_device for each physical67* function on the NIC. The RDMA driver registers an auxiliary_driver that68* claims each of these auxiliary_devices. This conveys data/ops published by69* the parent PCI device/driver to the RDMA auxiliary_driver.70*71* Another use case is for the PCI device to be split out into multiple sub72* functions. For each sub function an auxiliary_device is created. A PCI sub73* function driver binds to such devices that creates its own one or more class74* devices. A PCI sub function auxiliary device is likely to be contained in a75* struct with additional attributes such as user defined sub function number76* and optional attributes such as resources and a link to the parent device.77* These attributes could be used by systemd/udev; and hence should be78* initialized before a driver binds to an auxiliary_device.79*80* A key requirement for utilizing the auxiliary bus is that there is no81* dependency on a physical bus, device, register accesses or regmap support.82* These individual devices split from the core cannot live on the platform bus83* as they are not physical devices that are controlled by DT/ACPI. The same84* argument applies for not using MFD in this scenario as MFD relies on85* individual function devices being physical devices.86*/8788/**89* DOC: EXAMPLE90*91* Auxiliary devices are created and registered by a subsystem-level core92* device that needs to break up its functionality into smaller fragments. One93* way to extend the scope of an auxiliary_device is to encapsulate it within a94* domain-specific structure defined by the parent device. This structure95* contains the auxiliary_device and any associated shared data/callbacks96* needed to establish the connection with the parent.97*98* An example is:99*100* .. code-block:: c101*102* struct foo {103* struct auxiliary_device auxdev;104* void (*connect)(struct auxiliary_device *auxdev);105* void (*disconnect)(struct auxiliary_device *auxdev);106* void *data;107* };108*109* The parent device then registers the auxiliary_device by calling110* auxiliary_device_init(), and then auxiliary_device_add(), with the pointer111* to the auxdev member of the above structure. The parent provides a name for112* the auxiliary_device that, combined with the parent's KBUILD_MODNAME,113* creates a match_name that is be used for matching and binding with a driver.114*115* Whenever an auxiliary_driver is registered, based on the match_name, the116* auxiliary_driver's probe() is invoked for the matching devices. The117* auxiliary_driver can also be encapsulated inside custom drivers that make118* the core device's functionality extensible by adding additional119* domain-specific ops as follows:120*121* .. code-block:: c122*123* struct my_ops {124* void (*send)(struct auxiliary_device *auxdev);125* void (*receive)(struct auxiliary_device *auxdev);126* };127*128*129* struct my_driver {130* struct auxiliary_driver auxiliary_drv;131* const struct my_ops ops;132* };133*134* An example of this type of usage is:135*136* .. code-block:: c137*138* const struct auxiliary_device_id my_auxiliary_id_table[] = {139* { .name = "foo_mod.foo_dev" },140* { },141* };142*143* const struct my_ops my_custom_ops = {144* .send = my_tx,145* .receive = my_rx,146* };147*148* const struct my_driver my_drv = {149* .auxiliary_drv = {150* .name = "myauxiliarydrv",151* .id_table = my_auxiliary_id_table,152* .probe = my_probe,153* .remove = my_remove,154* .shutdown = my_shutdown,155* },156* .ops = my_custom_ops,157* };158*159* Please note that such custom ops approach is valid, but it is hard to implement160* it right without global locks per-device to protect from auxiliary_drv removal161* during call to that ops. In addition, this implementation lacks proper module162* dependency, which causes to load/unload races between auxiliary parent and devices163* modules.164*165* The most easiest way to provide these ops reliably without needing to166* have a lock is to EXPORT_SYMBOL*() them and rely on already existing167* modules infrastructure for validity and correct dependencies chains.168*/169170static const struct auxiliary_device_id *auxiliary_match_id(const struct auxiliary_device_id *id,171const struct auxiliary_device *auxdev)172{173for (; id->name[0]; id++) {174const char *p = strrchr(dev_name(&auxdev->dev), '.');175int match_size;176177if (!p)178continue;179match_size = p - dev_name(&auxdev->dev);180181/* use dev_name(&auxdev->dev) prefix before last '.' char to match to */182if (strlen(id->name) == match_size &&183!strncmp(dev_name(&auxdev->dev), id->name, match_size))184return id;185}186return NULL;187}188189static int auxiliary_match(struct device *dev, const struct device_driver *drv)190{191struct auxiliary_device *auxdev = to_auxiliary_dev(dev);192const struct auxiliary_driver *auxdrv = to_auxiliary_drv(drv);193194return !!auxiliary_match_id(auxdrv->id_table, auxdev);195}196197static int auxiliary_uevent(const struct device *dev, struct kobj_uevent_env *env)198{199const char *name, *p;200201name = dev_name(dev);202p = strrchr(name, '.');203204return add_uevent_var(env, "MODALIAS=%s%.*s", AUXILIARY_MODULE_PREFIX,205(int)(p - name), name);206}207208static const struct dev_pm_ops auxiliary_dev_pm_ops = {209SET_RUNTIME_PM_OPS(pm_generic_runtime_suspend, pm_generic_runtime_resume, NULL)210SET_SYSTEM_SLEEP_PM_OPS(pm_generic_suspend, pm_generic_resume)211};212213static int auxiliary_bus_probe(struct device *dev)214{215const struct auxiliary_driver *auxdrv = to_auxiliary_drv(dev->driver);216struct auxiliary_device *auxdev = to_auxiliary_dev(dev);217int ret;218219ret = dev_pm_domain_attach(dev, PD_FLAG_ATTACH_POWER_ON);220if (ret) {221dev_warn(dev, "Failed to attach to PM Domain : %d\n", ret);222return ret;223}224225ret = auxdrv->probe(auxdev, auxiliary_match_id(auxdrv->id_table, auxdev));226if (ret)227dev_pm_domain_detach(dev, true);228229return ret;230}231232static void auxiliary_bus_remove(struct device *dev)233{234const struct auxiliary_driver *auxdrv = to_auxiliary_drv(dev->driver);235struct auxiliary_device *auxdev = to_auxiliary_dev(dev);236237if (auxdrv->remove)238auxdrv->remove(auxdev);239dev_pm_domain_detach(dev, true);240}241242static void auxiliary_bus_shutdown(struct device *dev)243{244const struct auxiliary_driver *auxdrv = NULL;245struct auxiliary_device *auxdev;246247if (dev->driver) {248auxdrv = to_auxiliary_drv(dev->driver);249auxdev = to_auxiliary_dev(dev);250}251252if (auxdrv && auxdrv->shutdown)253auxdrv->shutdown(auxdev);254}255256static const struct bus_type auxiliary_bus_type = {257.name = "auxiliary",258.probe = auxiliary_bus_probe,259.remove = auxiliary_bus_remove,260.shutdown = auxiliary_bus_shutdown,261.match = auxiliary_match,262.uevent = auxiliary_uevent,263.pm = &auxiliary_dev_pm_ops,264};265266/**267* auxiliary_device_init - check auxiliary_device and initialize268* @auxdev: auxiliary device struct269*270* This is the second step in the three-step process to register an271* auxiliary_device.272*273* When this function returns an error code, then the device_initialize will274* *not* have been performed, and the caller will be responsible to free any275* memory allocated for the auxiliary_device in the error path directly.276*277* It returns 0 on success. On success, the device_initialize has been278* performed. After this point any error unwinding will need to include a call279* to auxiliary_device_uninit(). In this post-initialize error scenario, a call280* to the device's .release callback will be triggered, and all memory clean-up281* is expected to be handled there.282*/283int auxiliary_device_init(struct auxiliary_device *auxdev)284{285struct device *dev = &auxdev->dev;286287if (!dev->parent) {288pr_err("auxiliary_device has a NULL dev->parent\n");289return -EINVAL;290}291292if (!auxdev->name) {293pr_err("auxiliary_device has a NULL name\n");294return -EINVAL;295}296297dev->bus = &auxiliary_bus_type;298device_initialize(&auxdev->dev);299mutex_init(&auxdev->sysfs.lock);300return 0;301}302EXPORT_SYMBOL_GPL(auxiliary_device_init);303304/**305* __auxiliary_device_add - add an auxiliary bus device306* @auxdev: auxiliary bus device to add to the bus307* @modname: name of the parent device's driver module308*309* This is the third step in the three-step process to register an310* auxiliary_device.311*312* This function must be called after a successful call to313* auxiliary_device_init(), which will perform the device_initialize. This314* means that if this returns an error code, then a call to315* auxiliary_device_uninit() must be performed so that the .release callback316* will be triggered to free the memory associated with the auxiliary_device.317*318* The expectation is that users will call the "auxiliary_device_add" macro so319* that the caller's KBUILD_MODNAME is automatically inserted for the modname320* parameter. Only if a user requires a custom name would this version be321* called directly.322*/323int __auxiliary_device_add(struct auxiliary_device *auxdev, const char *modname)324{325struct device *dev = &auxdev->dev;326int ret;327328if (!modname) {329dev_err(dev, "auxiliary device modname is NULL\n");330return -EINVAL;331}332333ret = dev_set_name(dev, "%s.%s.%d", modname, auxdev->name, auxdev->id);334if (ret) {335dev_err(dev, "auxiliary device dev_set_name failed: %d\n", ret);336return ret;337}338339ret = device_add(dev);340if (ret)341dev_err(dev, "adding auxiliary device failed!: %d\n", ret);342343return ret;344}345EXPORT_SYMBOL_GPL(__auxiliary_device_add);346347/**348* __auxiliary_driver_register - register a driver for auxiliary bus devices349* @auxdrv: auxiliary_driver structure350* @owner: owning module/driver351* @modname: KBUILD_MODNAME for parent driver352*353* The expectation is that users will call the "auxiliary_driver_register"354* macro so that the caller's KBUILD_MODNAME is automatically inserted for the355* modname parameter. Only if a user requires a custom name would this version356* be called directly.357*/358int __auxiliary_driver_register(struct auxiliary_driver *auxdrv,359struct module *owner, const char *modname)360{361int ret;362363if (WARN_ON(!auxdrv->probe) || WARN_ON(!auxdrv->id_table))364return -EINVAL;365366if (auxdrv->name)367auxdrv->driver.name = kasprintf(GFP_KERNEL, "%s.%s", modname,368auxdrv->name);369else370auxdrv->driver.name = kasprintf(GFP_KERNEL, "%s", modname);371if (!auxdrv->driver.name)372return -ENOMEM;373374auxdrv->driver.owner = owner;375auxdrv->driver.bus = &auxiliary_bus_type;376auxdrv->driver.mod_name = modname;377378ret = driver_register(&auxdrv->driver);379if (ret)380kfree(auxdrv->driver.name);381382return ret;383}384EXPORT_SYMBOL_GPL(__auxiliary_driver_register);385386/**387* auxiliary_driver_unregister - unregister a driver388* @auxdrv: auxiliary_driver structure389*/390void auxiliary_driver_unregister(struct auxiliary_driver *auxdrv)391{392driver_unregister(&auxdrv->driver);393kfree(auxdrv->driver.name);394}395EXPORT_SYMBOL_GPL(auxiliary_driver_unregister);396397static void auxiliary_device_release(struct device *dev)398{399struct auxiliary_device *auxdev = to_auxiliary_dev(dev);400401of_node_put(dev->of_node);402kfree(auxdev);403}404405/**406* auxiliary_device_create - create a device on the auxiliary bus407* @dev: parent device408* @modname: module name used to create the auxiliary driver name.409* @devname: auxiliary bus device name410* @platform_data: auxiliary bus device platform data411* @id: auxiliary bus device id412*413* Helper to create an auxiliary bus device.414* The device created matches driver 'modname.devname' on the auxiliary bus.415*/416struct auxiliary_device *auxiliary_device_create(struct device *dev,417const char *modname,418const char *devname,419void *platform_data,420int id)421{422struct auxiliary_device *auxdev;423int ret;424425auxdev = kzalloc(sizeof(*auxdev), GFP_KERNEL);426if (!auxdev)427return NULL;428429auxdev->id = id;430auxdev->name = devname;431auxdev->dev.parent = dev;432auxdev->dev.platform_data = platform_data;433auxdev->dev.release = auxiliary_device_release;434device_set_of_node_from_dev(&auxdev->dev, dev);435436ret = auxiliary_device_init(auxdev);437if (ret) {438of_node_put(auxdev->dev.of_node);439kfree(auxdev);440return NULL;441}442443ret = __auxiliary_device_add(auxdev, modname);444if (ret) {445/*446* It may look odd but auxdev should not be freed here.447* auxiliary_device_uninit() calls device_put() which call448* the device release function, freeing auxdev.449*/450auxiliary_device_uninit(auxdev);451return NULL;452}453454return auxdev;455}456EXPORT_SYMBOL_GPL(auxiliary_device_create);457458/**459* auxiliary_device_destroy - remove an auxiliary device460* @auxdev: pointer to the auxdev to be removed461*462* Helper to remove an auxiliary device created with463* auxiliary_device_create()464*/465void auxiliary_device_destroy(void *auxdev)466{467struct auxiliary_device *_auxdev = auxdev;468469auxiliary_device_delete(_auxdev);470auxiliary_device_uninit(_auxdev);471}472EXPORT_SYMBOL_GPL(auxiliary_device_destroy);473474/**475* __devm_auxiliary_device_create - create a managed device on the auxiliary bus476* @dev: parent device477* @modname: module name used to create the auxiliary driver name.478* @devname: auxiliary bus device name479* @platform_data: auxiliary bus device platform data480* @id: auxiliary bus device id481*482* Device managed helper to create an auxiliary bus device.483* The device created matches driver 'modname.devname' on the auxiliary bus.484*/485struct auxiliary_device *__devm_auxiliary_device_create(struct device *dev,486const char *modname,487const char *devname,488void *platform_data,489int id)490{491struct auxiliary_device *auxdev;492int ret;493494auxdev = auxiliary_device_create(dev, modname, devname, platform_data, id);495if (!auxdev)496return NULL;497498ret = devm_add_action_or_reset(dev, auxiliary_device_destroy,499auxdev);500if (ret)501return NULL;502503return auxdev;504}505EXPORT_SYMBOL_GPL(__devm_auxiliary_device_create);506507void __init auxiliary_bus_init(void)508{509WARN_ON(bus_register(&auxiliary_bus_type));510}511512513