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()
40 static int ssam_device_uevent(const struct device *dev, struct kobj_uevent_env *env) in ssam_device_uevent()
45 sdev->uid.domain, sdev->uid.category, in ssam_device_uevent()
46 sdev->uid.target, sdev->uid.instance, in ssam_device_uevent()
47 sdev->uid.function); in ssam_device_uevent()
50 static void ssam_device_release(struct device *dev) in ssam_device_release()
54 ssam_controller_put(sdev->ctrl); in ssam_device_release()
55 fwnode_handle_put(sdev->dev.fwnode); in ssam_device_release()
68 * ssam_device_alloc() - Allocate and initialize a SSAM client device.
69 * @ctrl: The controller under which the device should be added.
70 * @uid: The UID of the device to be added.
72 * Allocates and initializes a new client device. The parent of the device
73 * will be set to the controller device and the name will be set based on the
74 * UID. Note that the device still has to be added via ssam_device_add().
77 * Return: Returns the newly allocated and initialized SSAM client device, or
89 device_initialize(&sdev->dev); in ssam_device_alloc()
90 sdev->dev.bus = &ssam_bus_type; in ssam_device_alloc()
91 sdev->dev.type = &ssam_device_type; in ssam_device_alloc()
92 sdev->dev.parent = ssam_controller_device(ctrl); in ssam_device_alloc()
93 sdev->ctrl = ssam_controller_get(ctrl); in ssam_device_alloc()
94 sdev->uid = uid; in ssam_device_alloc()
96 dev_set_name(&sdev->dev, "%02x:%02x:%02x:%02x:%02x", in ssam_device_alloc()
97 sdev->uid.domain, sdev->uid.category, sdev->uid.target, in ssam_device_alloc()
98 sdev->uid.instance, sdev->uid.function); in ssam_device_alloc()
105 * ssam_device_add() - Add a SSAM client device.
106 * @sdev: The SSAM client device to be added.
109 * controller. Thus, this function will fail with %-ENODEV if the controller
110 * of the device has not been initialized yet, has been suspended, or has been
115 * added device is a direct child of the controller device (default), it will
118 * By default, the controller device will become the parent of the newly
119 * created client device. The parent may be changed before ssam_device_add is
121 * is guaranteed and b) the client device does not outlive the controller,
122 * i.e. that the device is removed before the controller is being shut down.
125 * set up device-links for this purpose.
137 * controller device, i.e. it ensures that the controller (sdev->ctrl) in ssam_device_add()
139 * device we add here is registered as child under it. This essentially in ssam_device_add()
145 * Note that for this to work, the controller has to be a parent device. in ssam_device_add()
146 * If it is not a direct parent, care has to be taken that the device is in ssam_device_add()
150 ssam_controller_statelock(sdev->ctrl); in ssam_device_add()
152 if (sdev->ctrl->state != SSAM_CONTROLLER_STARTED) { in ssam_device_add()
153 ssam_controller_stateunlock(sdev->ctrl); in ssam_device_add()
154 return -ENODEV; in ssam_device_add()
157 status = device_add(&sdev->dev); in ssam_device_add()
159 ssam_controller_stateunlock(sdev->ctrl); in ssam_device_add()
165 * ssam_device_remove() - Remove a SSAM client device.
166 * @sdev: The device to remove.
168 * Removes and unregisters the provided SSAM client device.
172 device_unregister(&sdev->dev); in ssam_device_remove()
177 * ssam_device_id_compatible() - Check if a device ID matches a UID.
178 * @id: The device ID as potential match.
179 * @uid: The device UID matching against.
181 * Check if the given ID is a match for the given UID, i.e. if a device with
182 * the provided UID is compatible to the given ID following the match rules
186 * described by the given ID, %false otherwise.
188 static bool ssam_device_id_compatible(const struct ssam_device_id *id, in ssam_device_id_compatible() argument
191 if (id->domain != uid.domain || id->category != uid.category) in ssam_device_id_compatible()
194 if ((id->match_flags & SSAM_MATCH_TARGET) && id->target != uid.target) in ssam_device_id_compatible()
197 if ((id->match_flags & SSAM_MATCH_INSTANCE) && id->instance != uid.instance) in ssam_device_id_compatible()
200 if ((id->match_flags & SSAM_MATCH_FUNCTION) && id->function != uid.function) in ssam_device_id_compatible()
207 * ssam_device_id_is_null() - Check if a device ID is null.
208 * @id: The device ID to check.
210 * Check if a given device ID is null, i.e. all zeros. Used to check for the
213 * Return: Returns %true if the given ID represents a null ID, %false
216 static bool ssam_device_id_is_null(const struct ssam_device_id *id) in ssam_device_id_is_null() argument
218 return id->match_flags == 0 && in ssam_device_id_is_null()
219 id->domain == 0 && in ssam_device_id_is_null()
220 id->category == 0 && in ssam_device_id_is_null()
221 id->target == 0 && in ssam_device_id_is_null()
222 id->instance == 0 && in ssam_device_id_is_null()
223 id->function == 0 && in ssam_device_id_is_null()
224 id->driver_data == 0; in ssam_device_id_is_null()
228 * ssam_device_id_match() - Find the matching ID table entry for the given UID.
232 * Find the first match for the provided device UID in the provided ID table
238 const struct ssam_device_id *id; in ssam_device_id_match() local
240 for (id = table; !ssam_device_id_is_null(id); ++id) in ssam_device_id_match()
241 if (ssam_device_id_compatible(id, uid)) in ssam_device_id_match()
242 return id; in ssam_device_id_match()
249 * ssam_device_get_match() - Find and return the ID matching the device in the
250 * ID table of the bound driver.
251 * @dev: The device for which to get the matching ID table entry.
253 * Find the fist match for the UID of the device in the ID table of the
254 * currently bound driver and return it. Returns %NULL if the device does not
258 * This function essentially calls ssam_device_id_match() with the ID table of
259 * the bound device driver and the UID of the device.
261 * Return: Returns the first match for the UID of the device in the device
268 sdrv = to_ssam_device_driver(dev->dev.driver); in ssam_device_get_match()
272 if (!sdrv->match_table) in ssam_device_get_match()
275 return ssam_device_id_match(sdrv->match_table, dev->uid); in ssam_device_get_match()
280 * ssam_device_get_match_data() - Find the ID matching the device in the
281 * ID table of the bound driver and return its ``driver_data`` member.
282 * @dev: The device for which to get the match data.
284 * Find the fist match for the UID of the device in the ID table of the
286 * device does not have a driver bound to it, the driver does not have
294 * of the device in the device driver's match table, or %NULL if no such match
299 const struct ssam_device_id *id; in ssam_device_get_match_data() local
301 id = ssam_device_get_match(dev); in ssam_device_get_match_data()
302 if (!id) in ssam_device_get_match_data()
305 return (const void *)id->driver_data; in ssam_device_get_match_data()
309 static int ssam_bus_match(struct device *dev, struct device_driver *drv) in ssam_bus_match()
317 return !!ssam_device_id_match(sdrv->match_table, sdev->uid); in ssam_bus_match()
320 static int ssam_bus_probe(struct device *dev) in ssam_bus_probe()
322 return to_ssam_device_driver(dev->driver) in ssam_bus_probe()
323 ->probe(to_ssam_device(dev)); in ssam_bus_probe()
326 static void ssam_bus_remove(struct device *dev) in ssam_bus_remove()
328 struct ssam_device_driver *sdrv = to_ssam_device_driver(dev->driver); in ssam_bus_remove()
330 if (sdrv->remove) in ssam_bus_remove()
331 sdrv->remove(to_ssam_device(dev)); in ssam_bus_remove()
342 * __ssam_device_driver_register() - Register a SSAM client device driver.
352 sdrv->driver.owner = owner; in __ssam_device_driver_register()
353 sdrv->driver.bus = &ssam_bus_type; in __ssam_device_driver_register()
356 sdrv->driver.probe_type = PROBE_PREFER_ASYNCHRONOUS; in __ssam_device_driver_register()
358 return driver_register(&sdrv->driver); in __ssam_device_driver_register()
363 * ssam_device_driver_unregister - Unregister a SSAM device driver.
368 driver_unregister(&sdrv->driver); in ssam_device_driver_unregister()
373 /* -- Bus registration. ----------------------------------------------------- */
376 * ssam_bus_register() - Register and set-up the SSAM client device bus.
384 * ssam_bus_unregister() - Unregister the SSAM client device bus.
392 /* -- Helpers for controller and hub devices. ------------------------------- */
401 return -EINVAL; in ssam_device_uid_from_string()
403 uid->domain = d; in ssam_device_uid_from_string()
404 uid->category = tc; in ssam_device_uid_from_string()
405 uid->target = tid; in ssam_device_uid_from_string()
406 uid->instance = iid; in ssam_device_uid_from_string()
407 uid->function = fn; in ssam_device_uid_from_string()
417 * To simplify definitions of firmware nodes, we set the device name in ssam_get_uid_for_node()
418 * based on the UID of the device, prefixed with "ssam:". in ssam_get_uid_for_node()
421 return -ENODEV; in ssam_get_uid_for_node()
427 static int ssam_add_client_device(struct device *parent, struct ssam_controller *ctrl, in ssam_add_client_device()
440 return -ENOMEM; in ssam_add_client_device()
442 sdev->dev.parent = parent; in ssam_add_client_device()
443 sdev->dev.fwnode = fwnode_handle_get(node); in ssam_add_client_device()
453 * __ssam_register_clients() - Register client devices defined under the
454 * given firmware node as children of the given device.
455 * @parent: The parent device under which clients should be registered.
460 * firmware node as children of the given parent device. The respective child
469 * firmware node and/or controller associated with the given device. This
470 * function is only intended for use when different device specifications (e.g.
472 * of the device registry).
476 int __ssam_register_clients(struct device *parent, struct ssam_controller *ctrl, in __ssam_register_clients()
484 * Try to add the device specified in the firmware node. If in __ssam_register_clients()
485 * this fails with -ENODEV, the node does not specify any SSAM in __ssam_register_clients()
486 * device, so ignore it and continue with the next one. in __ssam_register_clients()
489 if (status && status != -ENODEV) { in __ssam_register_clients()
502 static int ssam_remove_device(struct device *dev, void *_data) in ssam_remove_device()
513 * ssam_remove_clients() - Remove SSAM client devices registered as direct
514 * children under the given parent device.
515 * @dev: The (parent) device to remove all direct clients for.
518 * device. Note that this only accounts for direct children of the device.
521 void ssam_remove_clients(struct device *dev) in ssam_remove_clients()