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 <[email protected]>
8 #include <linux/device.h>
14 #include <linux/surface_aggregator/device.h>
20 /* -- Device and bus functions. --------------------------------------------- */
22 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, in modalias_show()
28 sdev->uid.domain, sdev->uid.category, sdev->uid.target, in modalias_show()
29 sdev->uid.instance, sdev->uid.function); in modalias_show()
41 static int ssam_device_uevent(const struct device *dev, struct kobj_uevent_env *env) in ssam_device_uevent()
46 sdev->uid.domain, sdev->uid.category, in ssam_device_uevent()
47 sdev->uid.target, sdev->uid.instance, in ssam_device_uevent()
48 sdev->uid.function); in ssam_device_uevent()
51 static void ssam_device_release(struct device *dev) in ssam_device_release()
55 ssam_controller_put(sdev->ctrl); in ssam_device_release()
56 fwnode_handle_put(sdev->dev.fwnode); in ssam_device_release()
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().
78 * Return: Returns the newly allocated and initialized SSAM client device, or
90 device_initialize(&sdev->dev); in ssam_device_alloc()
91 sdev->dev.bus = &ssam_bus_type; in ssam_device_alloc()
92 sdev->dev.type = &ssam_device_type; in ssam_device_alloc()
93 sdev->dev.parent = ssam_controller_device(ctrl); in ssam_device_alloc()
94 sdev->ctrl = ssam_controller_get(ctrl); in ssam_device_alloc()
95 sdev->uid = uid; in ssam_device_alloc()
97 dev_set_name(&sdev->dev, "%02x:%02x:%02x:%02x:%02x", in ssam_device_alloc()
98 sdev->uid.domain, sdev->uid.category, sdev->uid.target, in ssam_device_alloc()
99 sdev->uid.instance, sdev->uid.function); in ssam_device_alloc()
106 * ssam_device_add() - Add a SSAM client device.
107 * @sdev: The SSAM client device to be added.
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
116 * added device is a direct child of the controller device (default), it will
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
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.
126 * set up device-links for this purpose.
138 * controller device, i.e. it ensures that the controller (sdev->ctrl) in ssam_device_add()
140 * device we add here is registered as child under it. This essentially in ssam_device_add()
146 * Note that for this to work, the controller has to be a parent device. in ssam_device_add()
147 * If it is not a direct parent, care has to be taken that the device is in ssam_device_add()
151 ssam_controller_statelock(sdev->ctrl); in ssam_device_add()
153 if (sdev->ctrl->state != SSAM_CONTROLLER_STARTED) { in ssam_device_add()
154 ssam_controller_stateunlock(sdev->ctrl); in ssam_device_add()
155 return -ENODEV; in ssam_device_add()
158 status = device_add(&sdev->dev); in ssam_device_add()
160 ssam_controller_stateunlock(sdev->ctrl); in 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.
173 device_unregister(&sdev->dev); in 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
187 * described by the given ID, %false otherwise.
189 static bool ssam_device_id_compatible(const struct ssam_device_id *id, in ssam_device_id_compatible() argument
192 if (id->domain != uid.domain || id->category != uid.category) in ssam_device_id_compatible()
195 if ((id->match_flags & SSAM_MATCH_TARGET) && id->target != uid.target) in ssam_device_id_compatible()
198 if ((id->match_flags & SSAM_MATCH_INSTANCE) && id->instance != uid.instance) in ssam_device_id_compatible()
201 if ((id->match_flags & SSAM_MATCH_FUNCTION) && id->function != uid.function) in ssam_device_id_compatible()
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
214 * Return: Returns %true if the given ID represents a null ID, %false
217 static bool ssam_device_id_is_null(const struct ssam_device_id *id) in ssam_device_id_is_null() argument
219 return id->match_flags == 0 && in ssam_device_id_is_null()
220 id->domain == 0 && in ssam_device_id_is_null()
221 id->category == 0 && in ssam_device_id_is_null()
222 id->target == 0 && in ssam_device_id_is_null()
223 id->instance == 0 && in ssam_device_id_is_null()
224 id->function == 0 && in ssam_device_id_is_null()
225 id->driver_data == 0; in ssam_device_id_is_null()
229 * ssam_device_id_match() - Find the matching ID table entry for the given UID.
233 * Find the first match for the provided device UID in the provided ID table
239 const struct ssam_device_id *id; in ssam_device_id_match() local
241 for (id = table; !ssam_device_id_is_null(id); ++id) in ssam_device_id_match()
242 if (ssam_device_id_compatible(id, uid)) in ssam_device_id_match()
243 return id; in 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
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
269 sdrv = to_ssam_device_driver(dev->dev.driver); in ssam_device_get_match()
273 if (!sdrv->match_table) in ssam_device_get_match()
276 return ssam_device_id_match(sdrv->match_table, dev->uid); in 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
287 * device does not have a driver bound to it, the driver does not have
295 * of the device in the device driver's match table, or %NULL if no such match
300 const struct ssam_device_id *id; in ssam_device_get_match_data() local
302 id = ssam_device_get_match(dev); in ssam_device_get_match_data()
303 if (!id) in ssam_device_get_match_data()
306 return (const void *)id->driver_data; in ssam_device_get_match_data()
310 static int ssam_bus_match(struct device *dev, const struct device_driver *drv) in ssam_bus_match()
318 return !!ssam_device_id_match(sdrv->match_table, sdev->uid); in ssam_bus_match()
321 static int ssam_bus_probe(struct device *dev) in ssam_bus_probe()
323 return to_ssam_device_driver(dev->driver) in ssam_bus_probe()
324 ->probe(to_ssam_device(dev)); in ssam_bus_probe()
327 static void ssam_bus_remove(struct device *dev) in ssam_bus_remove()
329 struct ssam_device_driver *sdrv = to_ssam_device_driver(dev->driver); in ssam_bus_remove()
331 if (sdrv->remove) in ssam_bus_remove()
332 sdrv->remove(to_ssam_device(dev)); in ssam_bus_remove()
343 * __ssam_device_driver_register() - Register a SSAM client device driver.
353 sdrv->driver.owner = owner; in __ssam_device_driver_register()
354 sdrv->driver.bus = &ssam_bus_type; in __ssam_device_driver_register()
357 sdrv->driver.probe_type = PROBE_PREFER_ASYNCHRONOUS; in __ssam_device_driver_register()
359 return driver_register(&sdrv->driver); in __ssam_device_driver_register()
364 * ssam_device_driver_unregister - Unregister a SSAM device driver.
369 driver_unregister(&sdrv->driver); in ssam_device_driver_unregister()
374 /* -- Bus registration. ----------------------------------------------------- */
377 * ssam_bus_register() - Register and set-up the SSAM client device bus.
385 * ssam_bus_unregister() - Unregister the SSAM client device bus.
393 /* -- Helpers for controller and hub devices. ------------------------------- */
402 return -EINVAL; in ssam_device_uid_from_string()
404 uid->domain = d; in ssam_device_uid_from_string()
405 uid->category = tc; in ssam_device_uid_from_string()
406 uid->target = tid; in ssam_device_uid_from_string()
407 uid->instance = iid; in ssam_device_uid_from_string()
408 uid->function = fn; in ssam_device_uid_from_string()
418 * To simplify definitions of firmware nodes, we set the device name in ssam_get_uid_for_node()
419 * based on the UID of the device, prefixed with "ssam:". in ssam_get_uid_for_node()
422 return -ENODEV; in ssam_get_uid_for_node()
428 static int ssam_add_client_device(struct device *parent, struct ssam_controller *ctrl, in ssam_add_client_device()
441 return -ENOMEM; in ssam_add_client_device()
443 sdev->dev.parent = parent; in ssam_add_client_device()
444 sdev->dev.fwnode = fwnode_handle_get(node); in ssam_add_client_device()
445 sdev->dev.of_node = to_of_node(node); in ssam_add_client_device()
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.
462 * firmware node as children of the given parent device. The respective child
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.
474 * of the device registry).
478 int __ssam_register_clients(struct device *parent, struct ssam_controller *ctrl, in __ssam_register_clients()
486 * Try to add the device specified in the firmware node. If in __ssam_register_clients()
487 * this fails with -ENODEV, the node does not specify any SSAM in __ssam_register_clients()
488 * device, so ignore it and continue with the next one. in __ssam_register_clients()
491 if (status && status != -ENODEV) { in __ssam_register_clients()
504 static int ssam_remove_device(struct device *dev, void *_data) in ssam_remove_device()
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.
520 * device. Note that this only accounts for direct children of the device.
523 void ssam_remove_clients(struct device *dev) in ssam_remove_clients()