Merge tag 'trace-v6.13-rc7-2' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[linux.git] / drivers / platform / surface / aggregator / bus.c
blobd68d231e716ee4404764e320c4e82fa304743dbe
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Surface System Aggregator Module bus and device integration.
5 * Copyright (C) 2019-2022 Maximilian Luz <luzmaximilian@gmail.com>
6 */
8 #include <linux/device.h>
9 #include <linux/of.h>
10 #include <linux/property.h>
11 #include <linux/slab.h>
13 #include <linux/surface_aggregator/controller.h>
14 #include <linux/surface_aggregator/device.h>
16 #include "bus.h"
17 #include "controller.h"
20 /* -- Device and bus functions. --------------------------------------------- */
22 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
23 char *buf)
25 struct ssam_device *sdev = to_ssam_device(dev);
27 return sysfs_emit(buf, "ssam:d%02Xc%02Xt%02Xi%02Xf%02X\n",
28 sdev->uid.domain, sdev->uid.category, sdev->uid.target,
29 sdev->uid.instance, sdev->uid.function);
31 static DEVICE_ATTR_RO(modalias);
33 static struct attribute *ssam_device_attrs[] = {
34 &dev_attr_modalias.attr,
35 NULL,
37 ATTRIBUTE_GROUPS(ssam_device);
39 static const struct bus_type ssam_bus_type;
41 static int ssam_device_uevent(const struct device *dev, struct kobj_uevent_env *env)
43 const struct ssam_device *sdev = to_ssam_device(dev);
45 return add_uevent_var(env, "MODALIAS=ssam:d%02Xc%02Xt%02Xi%02Xf%02X",
46 sdev->uid.domain, sdev->uid.category,
47 sdev->uid.target, sdev->uid.instance,
48 sdev->uid.function);
51 static void ssam_device_release(struct device *dev)
53 struct ssam_device *sdev = to_ssam_device(dev);
55 ssam_controller_put(sdev->ctrl);
56 fwnode_handle_put(sdev->dev.fwnode);
57 kfree(sdev);
60 const struct device_type ssam_device_type = {
61 .name = "surface_aggregator_device",
62 .groups = ssam_device_groups,
63 .uevent = ssam_device_uevent,
64 .release = ssam_device_release,
66 EXPORT_SYMBOL_GPL(ssam_device_type);
68 /**
69 * ssam_device_alloc() - Allocate and initialize a SSAM client device.
70 * @ctrl: The controller under which the device should be added.
71 * @uid: The UID of the device to be added.
73 * Allocates and initializes a new client device. The parent of the device
74 * will be set to the controller device and the name will be set based on the
75 * UID. Note that the device still has to be added via ssam_device_add().
76 * Refer to that function for more details.
78 * Return: Returns the newly allocated and initialized SSAM client device, or
79 * %NULL if it could not be allocated.
81 struct ssam_device *ssam_device_alloc(struct ssam_controller *ctrl,
82 struct ssam_device_uid uid)
84 struct ssam_device *sdev;
86 sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
87 if (!sdev)
88 return NULL;
90 device_initialize(&sdev->dev);
91 sdev->dev.bus = &ssam_bus_type;
92 sdev->dev.type = &ssam_device_type;
93 sdev->dev.parent = ssam_controller_device(ctrl);
94 sdev->ctrl = ssam_controller_get(ctrl);
95 sdev->uid = uid;
97 dev_set_name(&sdev->dev, "%02x:%02x:%02x:%02x:%02x",
98 sdev->uid.domain, sdev->uid.category, sdev->uid.target,
99 sdev->uid.instance, sdev->uid.function);
101 return sdev;
103 EXPORT_SYMBOL_GPL(ssam_device_alloc);
106 * ssam_device_add() - Add a SSAM client device.
107 * @sdev: The SSAM client device to be added.
109 * Added client devices must be guaranteed to always have a valid and active
110 * controller. Thus, this function will fail with %-ENODEV if the controller
111 * of the device has not been initialized yet, has been suspended, or has been
112 * shut down.
114 * The caller of this function should ensure that the corresponding call to
115 * ssam_device_remove() is issued before the controller is shut down. If the
116 * added device is a direct child of the controller device (default), it will
117 * be automatically removed when the controller is shut down.
119 * By default, the controller device will become the parent of the newly
120 * created client device. The parent may be changed before ssam_device_add is
121 * called, but care must be taken that a) the correct suspend/resume ordering
122 * is guaranteed and b) the client device does not outlive the controller,
123 * i.e. that the device is removed before the controller is being shut down.
124 * In case these guarantees have to be manually enforced, please refer to the
125 * ssam_client_link() and ssam_client_bind() functions, which are intended to
126 * set up device-links for this purpose.
128 * Return: Returns zero on success, a negative error code on failure.
130 int ssam_device_add(struct ssam_device *sdev)
132 int status;
135 * Ensure that we can only add new devices to a controller if it has
136 * been started and is not going away soon. This works in combination
137 * with ssam_controller_remove_clients to ensure driver presence for the
138 * controller device, i.e. it ensures that the controller (sdev->ctrl)
139 * is always valid and can be used for requests as long as the client
140 * device we add here is registered as child under it. This essentially
141 * guarantees that the client driver can always expect the preconditions
142 * for functions like ssam_request_do_sync() (controller has to be
143 * started and is not suspended) to hold and thus does not have to check
144 * for them.
146 * Note that for this to work, the controller has to be a parent device.
147 * If it is not a direct parent, care has to be taken that the device is
148 * removed via ssam_device_remove(), as device_unregister does not
149 * remove child devices recursively.
151 ssam_controller_statelock(sdev->ctrl);
153 if (sdev->ctrl->state != SSAM_CONTROLLER_STARTED) {
154 ssam_controller_stateunlock(sdev->ctrl);
155 return -ENODEV;
158 status = device_add(&sdev->dev);
160 ssam_controller_stateunlock(sdev->ctrl);
161 return status;
163 EXPORT_SYMBOL_GPL(ssam_device_add);
166 * ssam_device_remove() - Remove a SSAM client device.
167 * @sdev: The device to remove.
169 * Removes and unregisters the provided SSAM client device.
171 void ssam_device_remove(struct ssam_device *sdev)
173 device_unregister(&sdev->dev);
175 EXPORT_SYMBOL_GPL(ssam_device_remove);
178 * ssam_device_id_compatible() - Check if a device ID matches a UID.
179 * @id: The device ID as potential match.
180 * @uid: The device UID matching against.
182 * Check if the given ID is a match for the given UID, i.e. if a device with
183 * the provided UID is compatible to the given ID following the match rules
184 * described in its &ssam_device_id.match_flags member.
186 * Return: Returns %true if the given UID is compatible to the match rule
187 * described by the given ID, %false otherwise.
189 static bool ssam_device_id_compatible(const struct ssam_device_id *id,
190 struct ssam_device_uid uid)
192 if (id->domain != uid.domain || id->category != uid.category)
193 return false;
195 if ((id->match_flags & SSAM_MATCH_TARGET) && id->target != uid.target)
196 return false;
198 if ((id->match_flags & SSAM_MATCH_INSTANCE) && id->instance != uid.instance)
199 return false;
201 if ((id->match_flags & SSAM_MATCH_FUNCTION) && id->function != uid.function)
202 return false;
204 return true;
208 * ssam_device_id_is_null() - Check if a device ID is null.
209 * @id: The device ID to check.
211 * Check if a given device ID is null, i.e. all zeros. Used to check for the
212 * end of ``MODULE_DEVICE_TABLE(ssam, ...)`` or similar lists.
214 * Return: Returns %true if the given ID represents a null ID, %false
215 * otherwise.
217 static bool ssam_device_id_is_null(const struct ssam_device_id *id)
219 return id->match_flags == 0 &&
220 id->domain == 0 &&
221 id->category == 0 &&
222 id->target == 0 &&
223 id->instance == 0 &&
224 id->function == 0 &&
225 id->driver_data == 0;
229 * ssam_device_id_match() - Find the matching ID table entry for the given UID.
230 * @table: The table to search in.
231 * @uid: The UID to matched against the individual table entries.
233 * Find the first match for the provided device UID in the provided ID table
234 * and return it. Returns %NULL if no match could be found.
236 const struct ssam_device_id *ssam_device_id_match(const struct ssam_device_id *table,
237 const struct ssam_device_uid uid)
239 const struct ssam_device_id *id;
241 for (id = table; !ssam_device_id_is_null(id); ++id)
242 if (ssam_device_id_compatible(id, uid))
243 return id;
245 return NULL;
247 EXPORT_SYMBOL_GPL(ssam_device_id_match);
250 * ssam_device_get_match() - Find and return the ID matching the device in the
251 * ID table of the bound driver.
252 * @dev: The device for which to get the matching ID table entry.
254 * Find the fist match for the UID of the device in the ID table of the
255 * currently bound driver and return it. Returns %NULL if the device does not
256 * have a driver bound to it, the driver does not have match_table (i.e. it is
257 * %NULL), or there is no match in the driver's match_table.
259 * This function essentially calls ssam_device_id_match() with the ID table of
260 * the bound device driver and the UID of the device.
262 * Return: Returns the first match for the UID of the device in the device
263 * driver's match table, or %NULL if no such match could be found.
265 const struct ssam_device_id *ssam_device_get_match(const struct ssam_device *dev)
267 const struct ssam_device_driver *sdrv;
269 sdrv = to_ssam_device_driver(dev->dev.driver);
270 if (!sdrv)
271 return NULL;
273 if (!sdrv->match_table)
274 return NULL;
276 return ssam_device_id_match(sdrv->match_table, dev->uid);
278 EXPORT_SYMBOL_GPL(ssam_device_get_match);
281 * ssam_device_get_match_data() - Find the ID matching the device in the
282 * ID table of the bound driver and return its ``driver_data`` member.
283 * @dev: The device for which to get the match data.
285 * Find the fist match for the UID of the device in the ID table of the
286 * corresponding driver and return its driver_data. Returns %NULL if the
287 * device does not have a driver bound to it, the driver does not have
288 * match_table (i.e. it is %NULL), there is no match in the driver's
289 * match_table, or the match does not have any driver_data.
291 * This function essentially calls ssam_device_get_match() and, if any match
292 * could be found, returns its ``struct ssam_device_id.driver_data`` member.
294 * Return: Returns the driver data associated with the first match for the UID
295 * of the device in the device driver's match table, or %NULL if no such match
296 * could be found.
298 const void *ssam_device_get_match_data(const struct ssam_device *dev)
300 const struct ssam_device_id *id;
302 id = ssam_device_get_match(dev);
303 if (!id)
304 return NULL;
306 return (const void *)id->driver_data;
308 EXPORT_SYMBOL_GPL(ssam_device_get_match_data);
310 static int ssam_bus_match(struct device *dev, const struct device_driver *drv)
312 const struct ssam_device_driver *sdrv = to_ssam_device_driver(drv);
313 struct ssam_device *sdev = to_ssam_device(dev);
315 if (!is_ssam_device(dev))
316 return 0;
318 return !!ssam_device_id_match(sdrv->match_table, sdev->uid);
321 static int ssam_bus_probe(struct device *dev)
323 return to_ssam_device_driver(dev->driver)
324 ->probe(to_ssam_device(dev));
327 static void ssam_bus_remove(struct device *dev)
329 struct ssam_device_driver *sdrv = to_ssam_device_driver(dev->driver);
331 if (sdrv->remove)
332 sdrv->remove(to_ssam_device(dev));
335 static const struct bus_type ssam_bus_type = {
336 .name = "surface_aggregator",
337 .match = ssam_bus_match,
338 .probe = ssam_bus_probe,
339 .remove = ssam_bus_remove,
343 * __ssam_device_driver_register() - Register a SSAM client device driver.
344 * @sdrv: The driver to register.
345 * @owner: The module owning the provided driver.
347 * Please refer to the ssam_device_driver_register() macro for the normal way
348 * to register a driver from inside its owning module.
350 int __ssam_device_driver_register(struct ssam_device_driver *sdrv,
351 struct module *owner)
353 sdrv->driver.owner = owner;
354 sdrv->driver.bus = &ssam_bus_type;
356 /* force drivers to async probe so I/O is possible in probe */
357 sdrv->driver.probe_type = PROBE_PREFER_ASYNCHRONOUS;
359 return driver_register(&sdrv->driver);
361 EXPORT_SYMBOL_GPL(__ssam_device_driver_register);
364 * ssam_device_driver_unregister - Unregister a SSAM device driver.
365 * @sdrv: The driver to unregister.
367 void ssam_device_driver_unregister(struct ssam_device_driver *sdrv)
369 driver_unregister(&sdrv->driver);
371 EXPORT_SYMBOL_GPL(ssam_device_driver_unregister);
374 /* -- Bus registration. ----------------------------------------------------- */
377 * ssam_bus_register() - Register and set-up the SSAM client device bus.
379 int ssam_bus_register(void)
381 return bus_register(&ssam_bus_type);
385 * ssam_bus_unregister() - Unregister the SSAM client device bus.
387 void ssam_bus_unregister(void)
389 return bus_unregister(&ssam_bus_type);
393 /* -- Helpers for controller and hub devices. ------------------------------- */
395 static int ssam_device_uid_from_string(const char *str, struct ssam_device_uid *uid)
397 u8 d, tc, tid, iid, fn;
398 int n;
400 n = sscanf(str, "%hhx:%hhx:%hhx:%hhx:%hhx", &d, &tc, &tid, &iid, &fn);
401 if (n != 5)
402 return -EINVAL;
404 uid->domain = d;
405 uid->category = tc;
406 uid->target = tid;
407 uid->instance = iid;
408 uid->function = fn;
410 return 0;
413 static int ssam_get_uid_for_node(struct fwnode_handle *node, struct ssam_device_uid *uid)
415 const char *str = fwnode_get_name(node);
418 * To simplify definitions of firmware nodes, we set the device name
419 * based on the UID of the device, prefixed with "ssam:".
421 if (strncmp(str, "ssam:", strlen("ssam:")) != 0)
422 return -ENODEV;
424 str += strlen("ssam:");
425 return ssam_device_uid_from_string(str, uid);
428 static int ssam_add_client_device(struct device *parent, struct ssam_controller *ctrl,
429 struct fwnode_handle *node)
431 struct ssam_device_uid uid;
432 struct ssam_device *sdev;
433 int status;
435 status = ssam_get_uid_for_node(node, &uid);
436 if (status)
437 return status;
439 sdev = ssam_device_alloc(ctrl, uid);
440 if (!sdev)
441 return -ENOMEM;
443 sdev->dev.parent = parent;
444 sdev->dev.fwnode = fwnode_handle_get(node);
445 sdev->dev.of_node = to_of_node(node);
447 status = ssam_device_add(sdev);
448 if (status)
449 ssam_device_put(sdev);
451 return status;
455 * __ssam_register_clients() - Register client devices defined under the
456 * given firmware node as children of the given device.
457 * @parent: The parent device under which clients should be registered.
458 * @ctrl: The controller with which client should be registered.
459 * @node: The firmware node holding definitions of the devices to be added.
461 * Register all clients that have been defined as children of the given root
462 * firmware node as children of the given parent device. The respective child
463 * firmware nodes will be associated with the correspondingly created child
464 * devices.
466 * The given controller will be used to instantiate the new devices. See
467 * ssam_device_add() for details.
469 * Note that, generally, the use of either ssam_device_register_clients() or
470 * ssam_register_clients() should be preferred as they directly use the
471 * firmware node and/or controller associated with the given device. This
472 * function is only intended for use when different device specifications (e.g.
473 * ACPI and firmware nodes) need to be combined (as is done in the platform hub
474 * of the device registry).
476 * Return: Returns zero on success, nonzero on failure.
478 int __ssam_register_clients(struct device *parent, struct ssam_controller *ctrl,
479 struct fwnode_handle *node)
481 struct fwnode_handle *child;
482 int status;
484 fwnode_for_each_child_node(node, child) {
486 * Try to add the device specified in the firmware node. If
487 * this fails with -ENODEV, the node does not specify any SSAM
488 * device, so ignore it and continue with the next one.
490 status = ssam_add_client_device(parent, ctrl, child);
491 if (status && status != -ENODEV) {
492 fwnode_handle_put(child);
493 goto err;
497 return 0;
498 err:
499 ssam_remove_clients(parent);
500 return status;
502 EXPORT_SYMBOL_GPL(__ssam_register_clients);
504 static int ssam_remove_device(struct device *dev, void *_data)
506 struct ssam_device *sdev = to_ssam_device(dev);
508 if (is_ssam_device(dev))
509 ssam_device_remove(sdev);
511 return 0;
515 * ssam_remove_clients() - Remove SSAM client devices registered as direct
516 * children under the given parent device.
517 * @dev: The (parent) device to remove all direct clients for.
519 * Remove all SSAM client devices registered as direct children under the given
520 * device. Note that this only accounts for direct children of the device.
521 * Refer to ssam_device_add()/ssam_device_remove() for more details.
523 void ssam_remove_clients(struct device *dev)
525 device_for_each_child_reverse(dev, NULL, ssam_remove_device);
527 EXPORT_SYMBOL_GPL(ssam_remove_clients);