Lines Matching +full:device +full:- +full:id
1 // SPDX-License-Identifier: GPL-2.0+
3 * Surface System Aggregator Module bus and device integration.
5 * Copyright (C) 2019-2022 Maximilian Luz <luzmaximilian@gmail.com>
8 #include <linux/device.h>
13 #include <linux/surface_aggregator/device.h>
19 /* -- Device and bus functions. --------------------------------------------- */
21 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, in modalias_show()
27 sdev->uid.domain, sdev->uid.category, sdev->uid.target, in modalias_show()
28 sdev->uid.instance, sdev->uid.function); in modalias_show()
38 static int ssam_device_uevent(struct device *dev, struct kobj_uevent_env *env) in ssam_device_uevent()
43 sdev->uid.domain, sdev->uid.category, in ssam_device_uevent()
44 sdev->uid.target, sdev->uid.instance, in ssam_device_uevent()
45 sdev->uid.function); in ssam_device_uevent()
48 static void ssam_device_release(struct device *dev) in ssam_device_release()
52 ssam_controller_put(sdev->ctrl); in ssam_device_release()
53 fwnode_handle_put(sdev->dev.fwnode); in ssam_device_release()
66 * ssam_device_alloc() - Allocate and initialize a SSAM client device.
67 * @ctrl: The controller under which the device should be added.
68 * @uid: The UID of the device to be added.
70 * Allocates and initializes a new client device. The parent of the device
71 * will be set to the controller device and the name will be set based on the
72 * UID. Note that the device still has to be added via ssam_device_add().
75 * Return: Returns the newly allocated and initialized SSAM client device, or
87 device_initialize(&sdev->dev); in ssam_device_alloc()
88 sdev->dev.bus = &ssam_bus_type; in ssam_device_alloc()
89 sdev->dev.type = &ssam_device_type; in ssam_device_alloc()
90 sdev->dev.parent = ssam_controller_device(ctrl); in ssam_device_alloc()
91 sdev->ctrl = ssam_controller_get(ctrl); in ssam_device_alloc()
92 sdev->uid = uid; in ssam_device_alloc()
94 dev_set_name(&sdev->dev, "%02x:%02x:%02x:%02x:%02x", in ssam_device_alloc()
95 sdev->uid.domain, sdev->uid.category, sdev->uid.target, in ssam_device_alloc()
96 sdev->uid.instance, sdev->uid.function); in ssam_device_alloc()
103 * ssam_device_add() - Add a SSAM client device.
104 * @sdev: The SSAM client device to be added.
107 * controller. Thus, this function will fail with %-ENODEV if the controller
108 * of the device has not been initialized yet, has been suspended, or has been
113 * added device is a direct child of the controller device (default), it will
116 * By default, the controller device will become the parent of the newly
117 * created client device. The parent may be changed before ssam_device_add is
119 * is guaranteed and b) the client device does not outlive the controller,
120 * i.e. that the device is removed before the controller is being shut down.
123 * set up device-links for this purpose.
135 * controller device, i.e. it ensures that the controller (sdev->ctrl) in ssam_device_add()
137 * device we add here is registered as child under it. This essentially in ssam_device_add()
143 * Note that for this to work, the controller has to be a parent device. in ssam_device_add()
144 * If it is not a direct parent, care has to be taken that the device is in ssam_device_add()
148 ssam_controller_statelock(sdev->ctrl); in ssam_device_add()
150 if (sdev->ctrl->state != SSAM_CONTROLLER_STARTED) { in ssam_device_add()
151 ssam_controller_stateunlock(sdev->ctrl); in ssam_device_add()
152 return -ENODEV; in ssam_device_add()
155 status = device_add(&sdev->dev); in ssam_device_add()
157 ssam_controller_stateunlock(sdev->ctrl); in ssam_device_add()
163 * ssam_device_remove() - Remove a SSAM client device.
164 * @sdev: The device to remove.
166 * Removes and unregisters the provided SSAM client device.
170 device_unregister(&sdev->dev); in ssam_device_remove()
175 * ssam_device_id_compatible() - Check if a device ID matches a UID.
176 * @id: The device ID as potential match.
177 * @uid: The device UID matching against.
179 * Check if the given ID is a match for the given UID, i.e. if a device with
180 * the provided UID is compatible to the given ID following the match rules
184 * described by the given ID, %false otherwise.
186 static bool ssam_device_id_compatible(const struct ssam_device_id *id, in ssam_device_id_compatible() argument
189 if (id->domain != uid.domain || id->category != uid.category) in ssam_device_id_compatible()
192 if ((id->match_flags & SSAM_MATCH_TARGET) && id->target != uid.target) in ssam_device_id_compatible()
195 if ((id->match_flags & SSAM_MATCH_INSTANCE) && id->instance != uid.instance) in ssam_device_id_compatible()
198 if ((id->match_flags & SSAM_MATCH_FUNCTION) && id->function != uid.function) in ssam_device_id_compatible()
205 * ssam_device_id_is_null() - Check if a device ID is null.
206 * @id: The device ID to check.
208 * Check if a given device ID is null, i.e. all zeros. Used to check for the
211 * Return: Returns %true if the given ID represents a null ID, %false
214 static bool ssam_device_id_is_null(const struct ssam_device_id *id) in ssam_device_id_is_null() argument
216 return id->match_flags == 0 && in ssam_device_id_is_null()
217 id->domain == 0 && in ssam_device_id_is_null()
218 id->category == 0 && in ssam_device_id_is_null()
219 id->target == 0 && in ssam_device_id_is_null()
220 id->instance == 0 && in ssam_device_id_is_null()
221 id->function == 0 && in ssam_device_id_is_null()
222 id->driver_data == 0; in ssam_device_id_is_null()
226 * ssam_device_id_match() - Find the matching ID table entry for the given UID.
230 * Find the first match for the provided device UID in the provided ID table
236 const struct ssam_device_id *id; in ssam_device_id_match() local
238 for (id = table; !ssam_device_id_is_null(id); ++id) in ssam_device_id_match()
239 if (ssam_device_id_compatible(id, uid)) in ssam_device_id_match()
240 return id; in ssam_device_id_match()
247 * ssam_device_get_match() - Find and return the ID matching the device in the
248 * ID table of the bound driver.
249 * @dev: The device for which to get the matching ID table entry.
251 * Find the fist match for the UID of the device in the ID table of the
252 * currently bound driver and return it. Returns %NULL if the device does not
256 * This function essentially calls ssam_device_id_match() with the ID table of
257 * the bound device driver and the UID of the device.
259 * Return: Returns the first match for the UID of the device in the device
266 sdrv = to_ssam_device_driver(dev->dev.driver); in ssam_device_get_match()
270 if (!sdrv->match_table) in ssam_device_get_match()
273 return ssam_device_id_match(sdrv->match_table, dev->uid); in ssam_device_get_match()
278 * ssam_device_get_match_data() - Find the ID matching the device in the
279 * ID table of the bound driver and return its ``driver_data`` member.
280 * @dev: The device for which to get the match data.
282 * Find the fist match for the UID of the device in the ID table of the
284 * device does not have a driver bound to it, the driver does not have
292 * of the device in the device driver's match table, or %NULL if no such match
297 const struct ssam_device_id *id; in ssam_device_get_match_data() local
299 id = ssam_device_get_match(dev); in ssam_device_get_match_data()
300 if (!id) in ssam_device_get_match_data()
303 return (const void *)id->driver_data; in ssam_device_get_match_data()
307 static int ssam_bus_match(struct device *dev, struct device_driver *drv) in ssam_bus_match()
315 return !!ssam_device_id_match(sdrv->match_table, sdev->uid); in ssam_bus_match()
318 static int ssam_bus_probe(struct device *dev) in ssam_bus_probe()
320 return to_ssam_device_driver(dev->driver) in ssam_bus_probe()
321 ->probe(to_ssam_device(dev)); in ssam_bus_probe()
324 static void ssam_bus_remove(struct device *dev) in ssam_bus_remove()
326 struct ssam_device_driver *sdrv = to_ssam_device_driver(dev->driver); in ssam_bus_remove()
328 if (sdrv->remove) in ssam_bus_remove()
329 sdrv->remove(to_ssam_device(dev)); in ssam_bus_remove()
341 * __ssam_device_driver_register() - Register a SSAM client device driver.
351 sdrv->driver.owner = owner; in __ssam_device_driver_register()
352 sdrv->driver.bus = &ssam_bus_type; in __ssam_device_driver_register()
355 sdrv->driver.probe_type = PROBE_PREFER_ASYNCHRONOUS; in __ssam_device_driver_register()
357 return driver_register(&sdrv->driver); in __ssam_device_driver_register()
362 * ssam_device_driver_unregister - Unregister a SSAM device driver.
367 driver_unregister(&sdrv->driver); in ssam_device_driver_unregister()
372 /* -- Bus registration. ----------------------------------------------------- */
375 * ssam_bus_register() - Register and set-up the SSAM client device bus.
383 * ssam_bus_unregister() - Unregister the SSAM client device bus.
391 /* -- Helpers for controller and hub devices. ------------------------------- */
400 return -EINVAL; in ssam_device_uid_from_string()
402 uid->domain = d; in ssam_device_uid_from_string()
403 uid->category = tc; in ssam_device_uid_from_string()
404 uid->target = tid; in ssam_device_uid_from_string()
405 uid->instance = iid; in ssam_device_uid_from_string()
406 uid->function = fn; in ssam_device_uid_from_string()
416 * To simplify definitions of firmware nodes, we set the device name in ssam_get_uid_for_node()
417 * based on the UID of the device, prefixed with "ssam:". in ssam_get_uid_for_node()
420 return -ENODEV; in ssam_get_uid_for_node()
426 static int ssam_add_client_device(struct device *parent, struct ssam_controller *ctrl, in ssam_add_client_device()
439 return -ENOMEM; in ssam_add_client_device()
441 sdev->dev.parent = parent; in ssam_add_client_device()
442 sdev->dev.fwnode = fwnode_handle_get(node); in ssam_add_client_device()
452 * __ssam_register_clients() - Register client devices defined under the
453 * given firmware node as children of the given device.
454 * @parent: The parent device under which clients should be registered.
459 * firmware node as children of the given parent device. The respective child
468 * firmware node and/or controller associated with the given device. This
469 * function is only intended for use when different device specifications (e.g.
471 * of the device registry).
475 int __ssam_register_clients(struct device *parent, struct ssam_controller *ctrl, in __ssam_register_clients()
483 * Try to add the device specified in the firmware node. If in __ssam_register_clients()
484 * this fails with -ENODEV, the node does not specify any SSAM in __ssam_register_clients()
485 * device, so ignore it and continue with the next one. in __ssam_register_clients()
488 if (status && status != -ENODEV) in __ssam_register_clients()
499 static int ssam_remove_device(struct device *dev, void *_data) in ssam_remove_device()
510 * ssam_remove_clients() - Remove SSAM client devices registered as direct
511 * children under the given parent device.
512 * @dev: The (parent) device to remove all direct clients for.
515 * device. Note that this only accounts for direct children of the device.
518 void ssam_remove_clients(struct device *dev) in ssam_remove_clients()