1 // SPDX-License-Identifier: GPL-2.0+
3 * Surface System Aggregator Module bus and device integration.
5 * Copyright (C) 2019-2021 Maximilian Luz <luzmaximilian@gmail.com>
8 #include <linux/device.h>
9 #include <linux/slab.h>
11 #include <linux/surface_aggregator/controller.h>
12 #include <linux/surface_aggregator/device.h>
15 #include "controller.h"
17 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
20 struct ssam_device *sdev = to_ssam_device(dev);
22 return sysfs_emit(buf, "ssam:d%02Xc%02Xt%02Xi%02Xf%02X\n",
23 sdev->uid.domain, sdev->uid.category, sdev->uid.target,
24 sdev->uid.instance, sdev->uid.function);
26 static DEVICE_ATTR_RO(modalias);
28 static struct attribute *ssam_device_attrs[] = {
29 &dev_attr_modalias.attr,
32 ATTRIBUTE_GROUPS(ssam_device);
34 static int ssam_device_uevent(struct device *dev, struct kobj_uevent_env *env)
36 struct ssam_device *sdev = to_ssam_device(dev);
38 return add_uevent_var(env, "MODALIAS=ssam:d%02Xc%02Xt%02Xi%02Xf%02X",
39 sdev->uid.domain, sdev->uid.category,
40 sdev->uid.target, sdev->uid.instance,
44 static void ssam_device_release(struct device *dev)
46 struct ssam_device *sdev = to_ssam_device(dev);
48 ssam_controller_put(sdev->ctrl);
52 const struct device_type ssam_device_type = {
53 .name = "surface_aggregator_device",
54 .groups = ssam_device_groups,
55 .uevent = ssam_device_uevent,
56 .release = ssam_device_release,
58 EXPORT_SYMBOL_GPL(ssam_device_type);
61 * ssam_device_alloc() - Allocate and initialize a SSAM client device.
62 * @ctrl: The controller under which the device should be added.
63 * @uid: The UID of the device to be added.
65 * Allocates and initializes a new client device. The parent of the device
66 * will be set to the controller device and the name will be set based on the
67 * UID. Note that the device still has to be added via ssam_device_add().
68 * Refer to that function for more details.
70 * Return: Returns the newly allocated and initialized SSAM client device, or
71 * %NULL if it could not be allocated.
73 struct ssam_device *ssam_device_alloc(struct ssam_controller *ctrl,
74 struct ssam_device_uid uid)
76 struct ssam_device *sdev;
78 sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
82 device_initialize(&sdev->dev);
83 sdev->dev.bus = &ssam_bus_type;
84 sdev->dev.type = &ssam_device_type;
85 sdev->dev.parent = ssam_controller_device(ctrl);
86 sdev->ctrl = ssam_controller_get(ctrl);
89 dev_set_name(&sdev->dev, "%02x:%02x:%02x:%02x:%02x",
90 sdev->uid.domain, sdev->uid.category, sdev->uid.target,
91 sdev->uid.instance, sdev->uid.function);
95 EXPORT_SYMBOL_GPL(ssam_device_alloc);
98 * ssam_device_add() - Add a SSAM client device.
99 * @sdev: The SSAM client device to be added.
101 * Added client devices must be guaranteed to always have a valid and active
102 * controller. Thus, this function will fail with %-ENODEV if the controller
103 * of the device has not been initialized yet, has been suspended, or has been
106 * The caller of this function should ensure that the corresponding call to
107 * ssam_device_remove() is issued before the controller is shut down. If the
108 * added device is a direct child of the controller device (default), it will
109 * be automatically removed when the controller is shut down.
111 * By default, the controller device will become the parent of the newly
112 * created client device. The parent may be changed before ssam_device_add is
113 * called, but care must be taken that a) the correct suspend/resume ordering
114 * is guaranteed and b) the client device does not outlive the controller,
115 * i.e. that the device is removed before the controller is being shut down.
116 * In case these guarantees have to be manually enforced, please refer to the
117 * ssam_client_link() and ssam_client_bind() functions, which are intended to
118 * set up device-links for this purpose.
120 * Return: Returns zero on success, a negative error code on failure.
122 int ssam_device_add(struct ssam_device *sdev)
127 * Ensure that we can only add new devices to a controller if it has
128 * been started and is not going away soon. This works in combination
129 * with ssam_controller_remove_clients to ensure driver presence for the
130 * controller device, i.e. it ensures that the controller (sdev->ctrl)
131 * is always valid and can be used for requests as long as the client
132 * device we add here is registered as child under it. This essentially
133 * guarantees that the client driver can always expect the preconditions
134 * for functions like ssam_request_sync (controller has to be started
135 * and is not suspended) to hold and thus does not have to check for
138 * Note that for this to work, the controller has to be a parent device.
139 * If it is not a direct parent, care has to be taken that the device is
140 * removed via ssam_device_remove(), as device_unregister does not
141 * remove child devices recursively.
143 ssam_controller_statelock(sdev->ctrl);
145 if (sdev->ctrl->state != SSAM_CONTROLLER_STARTED) {
146 ssam_controller_stateunlock(sdev->ctrl);
150 status = device_add(&sdev->dev);
152 ssam_controller_stateunlock(sdev->ctrl);
155 EXPORT_SYMBOL_GPL(ssam_device_add);
158 * ssam_device_remove() - Remove a SSAM client device.
159 * @sdev: The device to remove.
161 * Removes and unregisters the provided SSAM client device.
163 void ssam_device_remove(struct ssam_device *sdev)
165 device_unregister(&sdev->dev);
167 EXPORT_SYMBOL_GPL(ssam_device_remove);
170 * ssam_device_id_compatible() - Check if a device ID matches a UID.
171 * @id: The device ID as potential match.
172 * @uid: The device UID matching against.
174 * Check if the given ID is a match for the given UID, i.e. if a device with
175 * the provided UID is compatible to the given ID following the match rules
176 * described in its &ssam_device_id.match_flags member.
178 * Return: Returns %true if the given UID is compatible to the match rule
179 * described by the given ID, %false otherwise.
181 static bool ssam_device_id_compatible(const struct ssam_device_id *id,
182 struct ssam_device_uid uid)
184 if (id->domain != uid.domain || id->category != uid.category)
187 if ((id->match_flags & SSAM_MATCH_TARGET) && id->target != uid.target)
190 if ((id->match_flags & SSAM_MATCH_INSTANCE) && id->instance != uid.instance)
193 if ((id->match_flags & SSAM_MATCH_FUNCTION) && id->function != uid.function)
200 * ssam_device_id_is_null() - Check if a device ID is null.
201 * @id: The device ID to check.
203 * Check if a given device ID is null, i.e. all zeros. Used to check for the
204 * end of ``MODULE_DEVICE_TABLE(ssam, ...)`` or similar lists.
206 * Return: Returns %true if the given ID represents a null ID, %false
209 static bool ssam_device_id_is_null(const struct ssam_device_id *id)
211 return id->match_flags == 0 &&
217 id->driver_data == 0;
221 * ssam_device_id_match() - Find the matching ID table entry for the given UID.
222 * @table: The table to search in.
223 * @uid: The UID to matched against the individual table entries.
225 * Find the first match for the provided device UID in the provided ID table
226 * and return it. Returns %NULL if no match could be found.
228 const struct ssam_device_id *ssam_device_id_match(const struct ssam_device_id *table,
229 const struct ssam_device_uid uid)
231 const struct ssam_device_id *id;
233 for (id = table; !ssam_device_id_is_null(id); ++id)
234 if (ssam_device_id_compatible(id, uid))
239 EXPORT_SYMBOL_GPL(ssam_device_id_match);
242 * ssam_device_get_match() - Find and return the ID matching the device in the
243 * ID table of the bound driver.
244 * @dev: The device for which to get the matching ID table entry.
246 * Find the fist match for the UID of the device in the ID table of the
247 * currently bound driver and return it. Returns %NULL if the device does not
248 * have a driver bound to it, the driver does not have match_table (i.e. it is
249 * %NULL), or there is no match in the driver's match_table.
251 * This function essentially calls ssam_device_id_match() with the ID table of
252 * the bound device driver and the UID of the device.
254 * Return: Returns the first match for the UID of the device in the device
255 * driver's match table, or %NULL if no such match could be found.
257 const struct ssam_device_id *ssam_device_get_match(const struct ssam_device *dev)
259 const struct ssam_device_driver *sdrv;
261 sdrv = to_ssam_device_driver(dev->dev.driver);
265 if (!sdrv->match_table)
268 return ssam_device_id_match(sdrv->match_table, dev->uid);
270 EXPORT_SYMBOL_GPL(ssam_device_get_match);
273 * ssam_device_get_match_data() - Find the ID matching the device in the
274 * ID table of the bound driver and return its ``driver_data`` member.
275 * @dev: The device for which to get the match data.
277 * Find the fist match for the UID of the device in the ID table of the
278 * corresponding driver and return its driver_data. Returns %NULL if the
279 * device does not have a driver bound to it, the driver does not have
280 * match_table (i.e. it is %NULL), there is no match in the driver's
281 * match_table, or the match does not have any driver_data.
283 * This function essentially calls ssam_device_get_match() and, if any match
284 * could be found, returns its ``struct ssam_device_id.driver_data`` member.
286 * Return: Returns the driver data associated with the first match for the UID
287 * of the device in the device driver's match table, or %NULL if no such match
290 const void *ssam_device_get_match_data(const struct ssam_device *dev)
292 const struct ssam_device_id *id;
294 id = ssam_device_get_match(dev);
298 return (const void *)id->driver_data;
300 EXPORT_SYMBOL_GPL(ssam_device_get_match_data);
302 static int ssam_bus_match(struct device *dev, struct device_driver *drv)
304 struct ssam_device_driver *sdrv = to_ssam_device_driver(drv);
305 struct ssam_device *sdev = to_ssam_device(dev);
307 if (!is_ssam_device(dev))
310 return !!ssam_device_id_match(sdrv->match_table, sdev->uid);
313 static int ssam_bus_probe(struct device *dev)
315 return to_ssam_device_driver(dev->driver)
316 ->probe(to_ssam_device(dev));
319 static int ssam_bus_remove(struct device *dev)
321 struct ssam_device_driver *sdrv = to_ssam_device_driver(dev->driver);
324 sdrv->remove(to_ssam_device(dev));
329 struct bus_type ssam_bus_type = {
330 .name = "surface_aggregator",
331 .match = ssam_bus_match,
332 .probe = ssam_bus_probe,
333 .remove = ssam_bus_remove,
335 EXPORT_SYMBOL_GPL(ssam_bus_type);
338 * __ssam_device_driver_register() - Register a SSAM client device driver.
339 * @sdrv: The driver to register.
340 * @owner: The module owning the provided driver.
342 * Please refer to the ssam_device_driver_register() macro for the normal way
343 * to register a driver from inside its owning module.
345 int __ssam_device_driver_register(struct ssam_device_driver *sdrv,
346 struct module *owner)
348 sdrv->driver.owner = owner;
349 sdrv->driver.bus = &ssam_bus_type;
351 /* force drivers to async probe so I/O is possible in probe */
352 sdrv->driver.probe_type = PROBE_PREFER_ASYNCHRONOUS;
354 return driver_register(&sdrv->driver);
356 EXPORT_SYMBOL_GPL(__ssam_device_driver_register);
359 * ssam_device_driver_unregister - Unregister a SSAM device driver.
360 * @sdrv: The driver to unregister.
362 void ssam_device_driver_unregister(struct ssam_device_driver *sdrv)
364 driver_unregister(&sdrv->driver);
366 EXPORT_SYMBOL_GPL(ssam_device_driver_unregister);
368 static int ssam_remove_device(struct device *dev, void *_data)
370 struct ssam_device *sdev = to_ssam_device(dev);
372 if (is_ssam_device(dev))
373 ssam_device_remove(sdev);
379 * ssam_controller_remove_clients() - Remove SSAM client devices registered as
380 * direct children under the given controller.
381 * @ctrl: The controller to remove all direct clients for.
383 * Remove all SSAM client devices registered as direct children under the
384 * given controller. Note that this only accounts for direct children of the
385 * controller device. This does not take care of any client devices where the
386 * parent device has been manually set before calling ssam_device_add. Refer
387 * to ssam_device_add()/ssam_device_remove() for more details on those cases.
389 * To avoid new devices being added in parallel to this call, the main
390 * controller lock (not statelock) must be held during this (and if
391 * necessary, any subsequent deinitialization) call.
393 void ssam_controller_remove_clients(struct ssam_controller *ctrl)
397 dev = ssam_controller_device(ctrl);
398 device_for_each_child_reverse(dev, NULL, ssam_remove_device);
402 * ssam_bus_register() - Register and set-up the SSAM client device bus.
404 int ssam_bus_register(void)
406 return bus_register(&ssam_bus_type);
410 * ssam_bus_unregister() - Unregister the SSAM client device bus.
412 void ssam_bus_unregister(void)
414 return bus_unregister(&ssam_bus_type);