Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * Surface System Aggregator Module bus and device integration.
0004  *
0005  * Copyright (C) 2019-2022 Maximilian Luz <luzmaximilian@gmail.com>
0006  */
0007 
0008 #include <linux/device.h>
0009 #include <linux/property.h>
0010 #include <linux/slab.h>
0011 
0012 #include <linux/surface_aggregator/controller.h>
0013 #include <linux/surface_aggregator/device.h>
0014 
0015 #include "bus.h"
0016 #include "controller.h"
0017 
0018 
0019 /* -- Device and bus functions. --------------------------------------------- */
0020 
0021 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
0022                  char *buf)
0023 {
0024     struct ssam_device *sdev = to_ssam_device(dev);
0025 
0026     return sysfs_emit(buf, "ssam:d%02Xc%02Xt%02Xi%02Xf%02X\n",
0027             sdev->uid.domain, sdev->uid.category, sdev->uid.target,
0028             sdev->uid.instance, sdev->uid.function);
0029 }
0030 static DEVICE_ATTR_RO(modalias);
0031 
0032 static struct attribute *ssam_device_attrs[] = {
0033     &dev_attr_modalias.attr,
0034     NULL,
0035 };
0036 ATTRIBUTE_GROUPS(ssam_device);
0037 
0038 static int ssam_device_uevent(struct device *dev, struct kobj_uevent_env *env)
0039 {
0040     struct ssam_device *sdev = to_ssam_device(dev);
0041 
0042     return add_uevent_var(env, "MODALIAS=ssam:d%02Xc%02Xt%02Xi%02Xf%02X",
0043                   sdev->uid.domain, sdev->uid.category,
0044                   sdev->uid.target, sdev->uid.instance,
0045                   sdev->uid.function);
0046 }
0047 
0048 static void ssam_device_release(struct device *dev)
0049 {
0050     struct ssam_device *sdev = to_ssam_device(dev);
0051 
0052     ssam_controller_put(sdev->ctrl);
0053     fwnode_handle_put(sdev->dev.fwnode);
0054     kfree(sdev);
0055 }
0056 
0057 const struct device_type ssam_device_type = {
0058     .name    = "surface_aggregator_device",
0059     .groups  = ssam_device_groups,
0060     .uevent  = ssam_device_uevent,
0061     .release = ssam_device_release,
0062 };
0063 EXPORT_SYMBOL_GPL(ssam_device_type);
0064 
0065 /**
0066  * ssam_device_alloc() - Allocate and initialize a SSAM client device.
0067  * @ctrl: The controller under which the device should be added.
0068  * @uid:  The UID of the device to be added.
0069  *
0070  * Allocates and initializes a new client device. The parent of the device
0071  * will be set to the controller device and the name will be set based on the
0072  * UID. Note that the device still has to be added via ssam_device_add().
0073  * Refer to that function for more details.
0074  *
0075  * Return: Returns the newly allocated and initialized SSAM client device, or
0076  * %NULL if it could not be allocated.
0077  */
0078 struct ssam_device *ssam_device_alloc(struct ssam_controller *ctrl,
0079                       struct ssam_device_uid uid)
0080 {
0081     struct ssam_device *sdev;
0082 
0083     sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
0084     if (!sdev)
0085         return NULL;
0086 
0087     device_initialize(&sdev->dev);
0088     sdev->dev.bus = &ssam_bus_type;
0089     sdev->dev.type = &ssam_device_type;
0090     sdev->dev.parent = ssam_controller_device(ctrl);
0091     sdev->ctrl = ssam_controller_get(ctrl);
0092     sdev->uid = uid;
0093 
0094     dev_set_name(&sdev->dev, "%02x:%02x:%02x:%02x:%02x",
0095              sdev->uid.domain, sdev->uid.category, sdev->uid.target,
0096              sdev->uid.instance, sdev->uid.function);
0097 
0098     return sdev;
0099 }
0100 EXPORT_SYMBOL_GPL(ssam_device_alloc);
0101 
0102 /**
0103  * ssam_device_add() - Add a SSAM client device.
0104  * @sdev: The SSAM client device to be added.
0105  *
0106  * Added client devices must be guaranteed to always have a valid and active
0107  * controller. Thus, this function will fail with %-ENODEV if the controller
0108  * of the device has not been initialized yet, has been suspended, or has been
0109  * shut down.
0110  *
0111  * The caller of this function should ensure that the corresponding call to
0112  * ssam_device_remove() is issued before the controller is shut down. If the
0113  * added device is a direct child of the controller device (default), it will
0114  * be automatically removed when the controller is shut down.
0115  *
0116  * By default, the controller device will become the parent of the newly
0117  * created client device. The parent may be changed before ssam_device_add is
0118  * called, but care must be taken that a) the correct suspend/resume ordering
0119  * is guaranteed and b) the client device does not outlive the controller,
0120  * i.e. that the device is removed before the controller is being shut down.
0121  * In case these guarantees have to be manually enforced, please refer to the
0122  * ssam_client_link() and ssam_client_bind() functions, which are intended to
0123  * set up device-links for this purpose.
0124  *
0125  * Return: Returns zero on success, a negative error code on failure.
0126  */
0127 int ssam_device_add(struct ssam_device *sdev)
0128 {
0129     int status;
0130 
0131     /*
0132      * Ensure that we can only add new devices to a controller if it has
0133      * been started and is not going away soon. This works in combination
0134      * with ssam_controller_remove_clients to ensure driver presence for the
0135      * controller device, i.e. it ensures that the controller (sdev->ctrl)
0136      * is always valid and can be used for requests as long as the client
0137      * device we add here is registered as child under it. This essentially
0138      * guarantees that the client driver can always expect the preconditions
0139      * for functions like ssam_request_sync (controller has to be started
0140      * and is not suspended) to hold and thus does not have to check for
0141      * them.
0142      *
0143      * Note that for this to work, the controller has to be a parent device.
0144      * If it is not a direct parent, care has to be taken that the device is
0145      * removed via ssam_device_remove(), as device_unregister does not
0146      * remove child devices recursively.
0147      */
0148     ssam_controller_statelock(sdev->ctrl);
0149 
0150     if (sdev->ctrl->state != SSAM_CONTROLLER_STARTED) {
0151         ssam_controller_stateunlock(sdev->ctrl);
0152         return -ENODEV;
0153     }
0154 
0155     status = device_add(&sdev->dev);
0156 
0157     ssam_controller_stateunlock(sdev->ctrl);
0158     return status;
0159 }
0160 EXPORT_SYMBOL_GPL(ssam_device_add);
0161 
0162 /**
0163  * ssam_device_remove() - Remove a SSAM client device.
0164  * @sdev: The device to remove.
0165  *
0166  * Removes and unregisters the provided SSAM client device.
0167  */
0168 void ssam_device_remove(struct ssam_device *sdev)
0169 {
0170     device_unregister(&sdev->dev);
0171 }
0172 EXPORT_SYMBOL_GPL(ssam_device_remove);
0173 
0174 /**
0175  * ssam_device_id_compatible() - Check if a device ID matches a UID.
0176  * @id:  The device ID as potential match.
0177  * @uid: The device UID matching against.
0178  *
0179  * Check if the given ID is a match for the given UID, i.e. if a device with
0180  * the provided UID is compatible to the given ID following the match rules
0181  * described in its &ssam_device_id.match_flags member.
0182  *
0183  * Return: Returns %true if the given UID is compatible to the match rule
0184  * described by the given ID, %false otherwise.
0185  */
0186 static bool ssam_device_id_compatible(const struct ssam_device_id *id,
0187                       struct ssam_device_uid uid)
0188 {
0189     if (id->domain != uid.domain || id->category != uid.category)
0190         return false;
0191 
0192     if ((id->match_flags & SSAM_MATCH_TARGET) && id->target != uid.target)
0193         return false;
0194 
0195     if ((id->match_flags & SSAM_MATCH_INSTANCE) && id->instance != uid.instance)
0196         return false;
0197 
0198     if ((id->match_flags & SSAM_MATCH_FUNCTION) && id->function != uid.function)
0199         return false;
0200 
0201     return true;
0202 }
0203 
0204 /**
0205  * ssam_device_id_is_null() - Check if a device ID is null.
0206  * @id: The device ID to check.
0207  *
0208  * Check if a given device ID is null, i.e. all zeros. Used to check for the
0209  * end of ``MODULE_DEVICE_TABLE(ssam, ...)`` or similar lists.
0210  *
0211  * Return: Returns %true if the given ID represents a null ID, %false
0212  * otherwise.
0213  */
0214 static bool ssam_device_id_is_null(const struct ssam_device_id *id)
0215 {
0216     return id->match_flags == 0 &&
0217         id->domain == 0 &&
0218         id->category == 0 &&
0219         id->target == 0 &&
0220         id->instance == 0 &&
0221         id->function == 0 &&
0222         id->driver_data == 0;
0223 }
0224 
0225 /**
0226  * ssam_device_id_match() - Find the matching ID table entry for the given UID.
0227  * @table: The table to search in.
0228  * @uid:   The UID to matched against the individual table entries.
0229  *
0230  * Find the first match for the provided device UID in the provided ID table
0231  * and return it. Returns %NULL if no match could be found.
0232  */
0233 const struct ssam_device_id *ssam_device_id_match(const struct ssam_device_id *table,
0234                           const struct ssam_device_uid uid)
0235 {
0236     const struct ssam_device_id *id;
0237 
0238     for (id = table; !ssam_device_id_is_null(id); ++id)
0239         if (ssam_device_id_compatible(id, uid))
0240             return id;
0241 
0242     return NULL;
0243 }
0244 EXPORT_SYMBOL_GPL(ssam_device_id_match);
0245 
0246 /**
0247  * ssam_device_get_match() - Find and return the ID matching the device in the
0248  * ID table of the bound driver.
0249  * @dev: The device for which to get the matching ID table entry.
0250  *
0251  * Find the fist match for the UID of the device in the ID table of the
0252  * currently bound driver and return it. Returns %NULL if the device does not
0253  * have a driver bound to it, the driver does not have match_table (i.e. it is
0254  * %NULL), or there is no match in the driver's match_table.
0255  *
0256  * This function essentially calls ssam_device_id_match() with the ID table of
0257  * the bound device driver and the UID of the device.
0258  *
0259  * Return: Returns the first match for the UID of the device in the device
0260  * driver's match table, or %NULL if no such match could be found.
0261  */
0262 const struct ssam_device_id *ssam_device_get_match(const struct ssam_device *dev)
0263 {
0264     const struct ssam_device_driver *sdrv;
0265 
0266     sdrv = to_ssam_device_driver(dev->dev.driver);
0267     if (!sdrv)
0268         return NULL;
0269 
0270     if (!sdrv->match_table)
0271         return NULL;
0272 
0273     return ssam_device_id_match(sdrv->match_table, dev->uid);
0274 }
0275 EXPORT_SYMBOL_GPL(ssam_device_get_match);
0276 
0277 /**
0278  * ssam_device_get_match_data() - Find the ID matching the device in the
0279  * ID table of the bound driver and return its ``driver_data`` member.
0280  * @dev: The device for which to get the match data.
0281  *
0282  * Find the fist match for the UID of the device in the ID table of the
0283  * corresponding driver and return its driver_data. Returns %NULL if the
0284  * device does not have a driver bound to it, the driver does not have
0285  * match_table (i.e. it is %NULL), there is no match in the driver's
0286  * match_table, or the match does not have any driver_data.
0287  *
0288  * This function essentially calls ssam_device_get_match() and, if any match
0289  * could be found, returns its ``struct ssam_device_id.driver_data`` member.
0290  *
0291  * Return: Returns the driver data associated with the first match for the UID
0292  * of the device in the device driver's match table, or %NULL if no such match
0293  * could be found.
0294  */
0295 const void *ssam_device_get_match_data(const struct ssam_device *dev)
0296 {
0297     const struct ssam_device_id *id;
0298 
0299     id = ssam_device_get_match(dev);
0300     if (!id)
0301         return NULL;
0302 
0303     return (const void *)id->driver_data;
0304 }
0305 EXPORT_SYMBOL_GPL(ssam_device_get_match_data);
0306 
0307 static int ssam_bus_match(struct device *dev, struct device_driver *drv)
0308 {
0309     struct ssam_device_driver *sdrv = to_ssam_device_driver(drv);
0310     struct ssam_device *sdev = to_ssam_device(dev);
0311 
0312     if (!is_ssam_device(dev))
0313         return 0;
0314 
0315     return !!ssam_device_id_match(sdrv->match_table, sdev->uid);
0316 }
0317 
0318 static int ssam_bus_probe(struct device *dev)
0319 {
0320     return to_ssam_device_driver(dev->driver)
0321         ->probe(to_ssam_device(dev));
0322 }
0323 
0324 static void ssam_bus_remove(struct device *dev)
0325 {
0326     struct ssam_device_driver *sdrv = to_ssam_device_driver(dev->driver);
0327 
0328     if (sdrv->remove)
0329         sdrv->remove(to_ssam_device(dev));
0330 }
0331 
0332 struct bus_type ssam_bus_type = {
0333     .name   = "surface_aggregator",
0334     .match  = ssam_bus_match,
0335     .probe  = ssam_bus_probe,
0336     .remove = ssam_bus_remove,
0337 };
0338 EXPORT_SYMBOL_GPL(ssam_bus_type);
0339 
0340 /**
0341  * __ssam_device_driver_register() - Register a SSAM client device driver.
0342  * @sdrv:  The driver to register.
0343  * @owner: The module owning the provided driver.
0344  *
0345  * Please refer to the ssam_device_driver_register() macro for the normal way
0346  * to register a driver from inside its owning module.
0347  */
0348 int __ssam_device_driver_register(struct ssam_device_driver *sdrv,
0349                   struct module *owner)
0350 {
0351     sdrv->driver.owner = owner;
0352     sdrv->driver.bus = &ssam_bus_type;
0353 
0354     /* force drivers to async probe so I/O is possible in probe */
0355     sdrv->driver.probe_type = PROBE_PREFER_ASYNCHRONOUS;
0356 
0357     return driver_register(&sdrv->driver);
0358 }
0359 EXPORT_SYMBOL_GPL(__ssam_device_driver_register);
0360 
0361 /**
0362  * ssam_device_driver_unregister - Unregister a SSAM device driver.
0363  * @sdrv: The driver to unregister.
0364  */
0365 void ssam_device_driver_unregister(struct ssam_device_driver *sdrv)
0366 {
0367     driver_unregister(&sdrv->driver);
0368 }
0369 EXPORT_SYMBOL_GPL(ssam_device_driver_unregister);
0370 
0371 
0372 /* -- Bus registration. ----------------------------------------------------- */
0373 
0374 /**
0375  * ssam_bus_register() - Register and set-up the SSAM client device bus.
0376  */
0377 int ssam_bus_register(void)
0378 {
0379     return bus_register(&ssam_bus_type);
0380 }
0381 
0382 /**
0383  * ssam_bus_unregister() - Unregister the SSAM client device bus.
0384  */
0385 void ssam_bus_unregister(void)
0386 {
0387     return bus_unregister(&ssam_bus_type);
0388 }
0389 
0390 
0391 /* -- Helpers for controller and hub devices. ------------------------------- */
0392 
0393 static int ssam_device_uid_from_string(const char *str, struct ssam_device_uid *uid)
0394 {
0395     u8 d, tc, tid, iid, fn;
0396     int n;
0397 
0398     n = sscanf(str, "%hhx:%hhx:%hhx:%hhx:%hhx", &d, &tc, &tid, &iid, &fn);
0399     if (n != 5)
0400         return -EINVAL;
0401 
0402     uid->domain = d;
0403     uid->category = tc;
0404     uid->target = tid;
0405     uid->instance = iid;
0406     uid->function = fn;
0407 
0408     return 0;
0409 }
0410 
0411 static int ssam_get_uid_for_node(struct fwnode_handle *node, struct ssam_device_uid *uid)
0412 {
0413     const char *str = fwnode_get_name(node);
0414 
0415     /*
0416      * To simplify definitions of firmware nodes, we set the device name
0417      * based on the UID of the device, prefixed with "ssam:".
0418      */
0419     if (strncmp(str, "ssam:", strlen("ssam:")) != 0)
0420         return -ENODEV;
0421 
0422     str += strlen("ssam:");
0423     return ssam_device_uid_from_string(str, uid);
0424 }
0425 
0426 static int ssam_add_client_device(struct device *parent, struct ssam_controller *ctrl,
0427                   struct fwnode_handle *node)
0428 {
0429     struct ssam_device_uid uid;
0430     struct ssam_device *sdev;
0431     int status;
0432 
0433     status = ssam_get_uid_for_node(node, &uid);
0434     if (status)
0435         return status;
0436 
0437     sdev = ssam_device_alloc(ctrl, uid);
0438     if (!sdev)
0439         return -ENOMEM;
0440 
0441     sdev->dev.parent = parent;
0442     sdev->dev.fwnode = fwnode_handle_get(node);
0443 
0444     status = ssam_device_add(sdev);
0445     if (status)
0446         ssam_device_put(sdev);
0447 
0448     return status;
0449 }
0450 
0451 /**
0452  * __ssam_register_clients() - Register client devices defined under the
0453  * given firmware node as children of the given device.
0454  * @parent: The parent device under which clients should be registered.
0455  * @ctrl: The controller with which client should be registered.
0456  * @node: The firmware node holding definitions of the devices to be added.
0457  *
0458  * Register all clients that have been defined as children of the given root
0459  * firmware node as children of the given parent device. The respective child
0460  * firmware nodes will be associated with the correspondingly created child
0461  * devices.
0462  *
0463  * The given controller will be used to instantiate the new devices. See
0464  * ssam_device_add() for details.
0465  *
0466  * Note that, generally, the use of either ssam_device_register_clients() or
0467  * ssam_register_clients() should be preferred as they directly use the
0468  * firmware node and/or controller associated with the given device. This
0469  * function is only intended for use when different device specifications (e.g.
0470  * ACPI and firmware nodes) need to be combined (as is done in the platform hub
0471  * of the device registry).
0472  *
0473  * Return: Returns zero on success, nonzero on failure.
0474  */
0475 int __ssam_register_clients(struct device *parent, struct ssam_controller *ctrl,
0476                 struct fwnode_handle *node)
0477 {
0478     struct fwnode_handle *child;
0479     int status;
0480 
0481     fwnode_for_each_child_node(node, child) {
0482         /*
0483          * Try to add the device specified in the firmware node. If
0484          * this fails with -ENODEV, the node does not specify any SSAM
0485          * device, so ignore it and continue with the next one.
0486          */
0487         status = ssam_add_client_device(parent, ctrl, child);
0488         if (status && status != -ENODEV)
0489             goto err;
0490     }
0491 
0492     return 0;
0493 err:
0494     ssam_remove_clients(parent);
0495     return status;
0496 }
0497 EXPORT_SYMBOL_GPL(__ssam_register_clients);
0498 
0499 static int ssam_remove_device(struct device *dev, void *_data)
0500 {
0501     struct ssam_device *sdev = to_ssam_device(dev);
0502 
0503     if (is_ssam_device(dev))
0504         ssam_device_remove(sdev);
0505 
0506     return 0;
0507 }
0508 
0509 /**
0510  * ssam_remove_clients() - Remove SSAM client devices registered as direct
0511  * children under the given parent device.
0512  * @dev: The (parent) device to remove all direct clients for.
0513  *
0514  * Remove all SSAM client devices registered as direct children under the given
0515  * device. Note that this only accounts for direct children of the device.
0516  * Refer to ssam_device_add()/ssam_device_remove() for more details.
0517  */
0518 void ssam_remove_clients(struct device *dev)
0519 {
0520     device_for_each_child_reverse(dev, NULL, ssam_remove_device);
0521 }
0522 EXPORT_SYMBOL_GPL(ssam_remove_clients);