Linux Audio

Check our new training course

Loading...
v6.2
  1// SPDX-License-Identifier: GPL-2.0+
  2/*
  3 * Surface System Aggregator Module bus and device integration.
  4 *
  5 * Copyright (C) 2019-2022 Maximilian Luz <luzmaximilian@gmail.com>
  6 */
  7
  8#include <linux/device.h>
  9#include <linux/property.h>
 10#include <linux/slab.h>
 11
 12#include <linux/surface_aggregator/controller.h>
 13#include <linux/surface_aggregator/device.h>
 14
 15#include "bus.h"
 16#include "controller.h"
 17
 18
 19/* -- Device and bus functions. --------------------------------------------- */
 20
 21static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
 22			     char *buf)
 23{
 24	struct ssam_device *sdev = to_ssam_device(dev);
 25
 26	return sysfs_emit(buf, "ssam:d%02Xc%02Xt%02Xi%02Xf%02X\n",
 27			sdev->uid.domain, sdev->uid.category, sdev->uid.target,
 28			sdev->uid.instance, sdev->uid.function);
 29}
 30static DEVICE_ATTR_RO(modalias);
 31
 32static struct attribute *ssam_device_attrs[] = {
 33	&dev_attr_modalias.attr,
 34	NULL,
 35};
 36ATTRIBUTE_GROUPS(ssam_device);
 37
 38static int ssam_device_uevent(struct device *dev, struct kobj_uevent_env *env)
 39{
 40	struct ssam_device *sdev = to_ssam_device(dev);
 41
 42	return add_uevent_var(env, "MODALIAS=ssam:d%02Xc%02Xt%02Xi%02Xf%02X",
 43			      sdev->uid.domain, sdev->uid.category,
 44			      sdev->uid.target, sdev->uid.instance,
 45			      sdev->uid.function);
 46}
 47
 48static void ssam_device_release(struct device *dev)
 49{
 50	struct ssam_device *sdev = to_ssam_device(dev);
 51
 52	ssam_controller_put(sdev->ctrl);
 53	fwnode_handle_put(sdev->dev.fwnode);
 54	kfree(sdev);
 55}
 56
 57const struct device_type ssam_device_type = {
 58	.name    = "surface_aggregator_device",
 59	.groups  = ssam_device_groups,
 60	.uevent  = ssam_device_uevent,
 61	.release = ssam_device_release,
 62};
 63EXPORT_SYMBOL_GPL(ssam_device_type);
 64
 65/**
 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.
 69 *
 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().
 73 * Refer to that function for more details.
 74 *
 75 * Return: Returns the newly allocated and initialized SSAM client device, or
 76 * %NULL if it could not be allocated.
 77 */
 78struct ssam_device *ssam_device_alloc(struct ssam_controller *ctrl,
 79				      struct ssam_device_uid uid)
 80{
 81	struct ssam_device *sdev;
 82
 83	sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
 84	if (!sdev)
 85		return NULL;
 86
 87	device_initialize(&sdev->dev);
 88	sdev->dev.bus = &ssam_bus_type;
 89	sdev->dev.type = &ssam_device_type;
 90	sdev->dev.parent = ssam_controller_device(ctrl);
 91	sdev->ctrl = ssam_controller_get(ctrl);
 92	sdev->uid = uid;
 93
 94	dev_set_name(&sdev->dev, "%02x:%02x:%02x:%02x:%02x",
 95		     sdev->uid.domain, sdev->uid.category, sdev->uid.target,
 96		     sdev->uid.instance, sdev->uid.function);
 97
 98	return sdev;
 99}
100EXPORT_SYMBOL_GPL(ssam_device_alloc);
101
102/**
103 * ssam_device_add() - Add a SSAM client device.
104 * @sdev: The SSAM client device to be added.
105 *
106 * Added client devices must be guaranteed to always have a valid and active
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
109 * shut down.
110 *
111 * The caller of this function should ensure that the corresponding call to
112 * ssam_device_remove() is issued before the controller is shut down. If the
113 * added device is a direct child of the controller device (default), it will
114 * be automatically removed when the controller is shut down.
115 *
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
118 * called, but care must be taken that a) the correct suspend/resume ordering
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.
121 * In case these guarantees have to be manually enforced, please refer to the
122 * ssam_client_link() and ssam_client_bind() functions, which are intended to
123 * set up device-links for this purpose.
124 *
125 * Return: Returns zero on success, a negative error code on failure.
126 */
127int ssam_device_add(struct ssam_device *sdev)
128{
129	int status;
130
131	/*
132	 * Ensure that we can only add new devices to a controller if it has
133	 * been started and is not going away soon. This works in combination
134	 * with ssam_controller_remove_clients to ensure driver presence for the
135	 * controller device, i.e. it ensures that the controller (sdev->ctrl)
136	 * is always valid and can be used for requests as long as the client
137	 * device we add here is registered as child under it. This essentially
138	 * guarantees that the client driver can always expect the preconditions
139	 * for functions like ssam_request_sync (controller has to be started
140	 * and is not suspended) to hold and thus does not have to check for
141	 * them.
142	 *
143	 * Note that for this to work, the controller has to be a parent device.
144	 * If it is not a direct parent, care has to be taken that the device is
145	 * removed via ssam_device_remove(), as device_unregister does not
146	 * remove child devices recursively.
147	 */
148	ssam_controller_statelock(sdev->ctrl);
149
150	if (sdev->ctrl->state != SSAM_CONTROLLER_STARTED) {
151		ssam_controller_stateunlock(sdev->ctrl);
152		return -ENODEV;
153	}
154
155	status = device_add(&sdev->dev);
156
157	ssam_controller_stateunlock(sdev->ctrl);
158	return status;
159}
160EXPORT_SYMBOL_GPL(ssam_device_add);
161
162/**
163 * ssam_device_remove() - Remove a SSAM client device.
164 * @sdev: The device to remove.
165 *
166 * Removes and unregisters the provided SSAM client device.
167 */
168void ssam_device_remove(struct ssam_device *sdev)
169{
170	device_unregister(&sdev->dev);
171}
172EXPORT_SYMBOL_GPL(ssam_device_remove);
173
174/**
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.
178 *
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
181 * described in its &ssam_device_id.match_flags member.
182 *
183 * Return: Returns %true if the given UID is compatible to the match rule
184 * described by the given ID, %false otherwise.
185 */
186static bool ssam_device_id_compatible(const struct ssam_device_id *id,
187				      struct ssam_device_uid uid)
188{
189	if (id->domain != uid.domain || id->category != uid.category)
190		return false;
191
192	if ((id->match_flags & SSAM_MATCH_TARGET) && id->target != uid.target)
193		return false;
194
195	if ((id->match_flags & SSAM_MATCH_INSTANCE) && id->instance != uid.instance)
196		return false;
197
198	if ((id->match_flags & SSAM_MATCH_FUNCTION) && id->function != uid.function)
199		return false;
200
201	return true;
202}
203
204/**
205 * ssam_device_id_is_null() - Check if a device ID is null.
206 * @id: The device ID to check.
207 *
208 * Check if a given device ID is null, i.e. all zeros. Used to check for the
209 * end of ``MODULE_DEVICE_TABLE(ssam, ...)`` or similar lists.
210 *
211 * Return: Returns %true if the given ID represents a null ID, %false
212 * otherwise.
213 */
214static bool ssam_device_id_is_null(const struct ssam_device_id *id)
215{
216	return id->match_flags == 0 &&
217		id->domain == 0 &&
218		id->category == 0 &&
219		id->target == 0 &&
220		id->instance == 0 &&
221		id->function == 0 &&
222		id->driver_data == 0;
223}
224
225/**
226 * ssam_device_id_match() - Find the matching ID table entry for the given UID.
227 * @table: The table to search in.
228 * @uid:   The UID to matched against the individual table entries.
229 *
230 * Find the first match for the provided device UID in the provided ID table
231 * and return it. Returns %NULL if no match could be found.
232 */
233const struct ssam_device_id *ssam_device_id_match(const struct ssam_device_id *table,
234						  const struct ssam_device_uid uid)
235{
236	const struct ssam_device_id *id;
237
238	for (id = table; !ssam_device_id_is_null(id); ++id)
239		if (ssam_device_id_compatible(id, uid))
240			return id;
241
242	return NULL;
243}
244EXPORT_SYMBOL_GPL(ssam_device_id_match);
245
246/**
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.
250 *
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
253 * have a driver bound to it, the driver does not have match_table (i.e. it is
254 * %NULL), or there is no match in the driver's match_table.
255 *
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.
258 *
259 * Return: Returns the first match for the UID of the device in the device
260 * driver's match table, or %NULL if no such match could be found.
261 */
262const struct ssam_device_id *ssam_device_get_match(const struct ssam_device *dev)
263{
264	const struct ssam_device_driver *sdrv;
265
266	sdrv = to_ssam_device_driver(dev->dev.driver);
267	if (!sdrv)
268		return NULL;
269
270	if (!sdrv->match_table)
271		return NULL;
272
273	return ssam_device_id_match(sdrv->match_table, dev->uid);
274}
275EXPORT_SYMBOL_GPL(ssam_device_get_match);
276
277/**
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.
281 *
282 * Find the fist match for the UID of the device in the ID table of the
283 * corresponding driver and return its driver_data. Returns %NULL if the
284 * device does not have a driver bound to it, the driver does not have
285 * match_table (i.e. it is %NULL), there is no match in the driver's
286 * match_table, or the match does not have any driver_data.
287 *
288 * This function essentially calls ssam_device_get_match() and, if any match
289 * could be found, returns its ``struct ssam_device_id.driver_data`` member.
290 *
291 * Return: Returns the driver data associated with the first match for the UID
292 * of the device in the device driver's match table, or %NULL if no such match
293 * could be found.
294 */
295const void *ssam_device_get_match_data(const struct ssam_device *dev)
296{
297	const struct ssam_device_id *id;
298
299	id = ssam_device_get_match(dev);
300	if (!id)
301		return NULL;
302
303	return (const void *)id->driver_data;
304}
305EXPORT_SYMBOL_GPL(ssam_device_get_match_data);
306
307static int ssam_bus_match(struct device *dev, struct device_driver *drv)
308{
309	struct ssam_device_driver *sdrv = to_ssam_device_driver(drv);
310	struct ssam_device *sdev = to_ssam_device(dev);
311
312	if (!is_ssam_device(dev))
313		return 0;
314
315	return !!ssam_device_id_match(sdrv->match_table, sdev->uid);
316}
317
318static int ssam_bus_probe(struct device *dev)
319{
320	return to_ssam_device_driver(dev->driver)
321		->probe(to_ssam_device(dev));
322}
323
324static void ssam_bus_remove(struct device *dev)
325{
326	struct ssam_device_driver *sdrv = to_ssam_device_driver(dev->driver);
327
328	if (sdrv->remove)
329		sdrv->remove(to_ssam_device(dev));
 
 
330}
331
332struct bus_type ssam_bus_type = {
333	.name   = "surface_aggregator",
334	.match  = ssam_bus_match,
335	.probe  = ssam_bus_probe,
336	.remove = ssam_bus_remove,
337};
338EXPORT_SYMBOL_GPL(ssam_bus_type);
339
340/**
341 * __ssam_device_driver_register() - Register a SSAM client device driver.
342 * @sdrv:  The driver to register.
343 * @owner: The module owning the provided driver.
344 *
345 * Please refer to the ssam_device_driver_register() macro for the normal way
346 * to register a driver from inside its owning module.
347 */
348int __ssam_device_driver_register(struct ssam_device_driver *sdrv,
349				  struct module *owner)
350{
351	sdrv->driver.owner = owner;
352	sdrv->driver.bus = &ssam_bus_type;
353
354	/* force drivers to async probe so I/O is possible in probe */
355	sdrv->driver.probe_type = PROBE_PREFER_ASYNCHRONOUS;
356
357	return driver_register(&sdrv->driver);
358}
359EXPORT_SYMBOL_GPL(__ssam_device_driver_register);
360
361/**
362 * ssam_device_driver_unregister - Unregister a SSAM device driver.
363 * @sdrv: The driver to unregister.
364 */
365void ssam_device_driver_unregister(struct ssam_device_driver *sdrv)
366{
367	driver_unregister(&sdrv->driver);
368}
369EXPORT_SYMBOL_GPL(ssam_device_driver_unregister);
370
371
372/* -- Bus registration. ----------------------------------------------------- */
373
374/**
375 * ssam_bus_register() - Register and set-up the SSAM client device bus.
376 */
377int ssam_bus_register(void)
378{
379	return bus_register(&ssam_bus_type);
380}
381
382/**
383 * ssam_bus_unregister() - Unregister the SSAM client device bus.
384 */
385void ssam_bus_unregister(void)
386{
387	return bus_unregister(&ssam_bus_type);
388}
389
390
391/* -- Helpers for controller and hub devices. ------------------------------- */
392
393static int ssam_device_uid_from_string(const char *str, struct ssam_device_uid *uid)
394{
395	u8 d, tc, tid, iid, fn;
396	int n;
397
398	n = sscanf(str, "%hhx:%hhx:%hhx:%hhx:%hhx", &d, &tc, &tid, &iid, &fn);
399	if (n != 5)
400		return -EINVAL;
401
402	uid->domain = d;
403	uid->category = tc;
404	uid->target = tid;
405	uid->instance = iid;
406	uid->function = fn;
407
408	return 0;
409}
410
411static int ssam_get_uid_for_node(struct fwnode_handle *node, struct ssam_device_uid *uid)
412{
413	const char *str = fwnode_get_name(node);
414
415	/*
416	 * To simplify definitions of firmware nodes, we set the device name
417	 * based on the UID of the device, prefixed with "ssam:".
418	 */
419	if (strncmp(str, "ssam:", strlen("ssam:")) != 0)
420		return -ENODEV;
421
422	str += strlen("ssam:");
423	return ssam_device_uid_from_string(str, uid);
424}
425
426static int ssam_add_client_device(struct device *parent, struct ssam_controller *ctrl,
427				  struct fwnode_handle *node)
428{
429	struct ssam_device_uid uid;
430	struct ssam_device *sdev;
431	int status;
432
433	status = ssam_get_uid_for_node(node, &uid);
434	if (status)
435		return status;
436
437	sdev = ssam_device_alloc(ctrl, uid);
438	if (!sdev)
439		return -ENOMEM;
440
441	sdev->dev.parent = parent;
442	sdev->dev.fwnode = fwnode_handle_get(node);
443
444	status = ssam_device_add(sdev);
445	if (status)
446		ssam_device_put(sdev);
447
448	return status;
449}
450
451/**
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.
455 * @ctrl: The controller with which client should be registered.
456 * @node: The firmware node holding definitions of the devices to be added.
457 *
458 * Register all clients that have been defined as children of the given root
459 * firmware node as children of the given parent device. The respective child
460 * firmware nodes will be associated with the correspondingly created child
461 * devices.
462 *
463 * The given controller will be used to instantiate the new devices. See
464 * ssam_device_add() for details.
465 *
466 * Note that, generally, the use of either ssam_device_register_clients() or
467 * ssam_register_clients() should be preferred as they directly use the
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.
470 * ACPI and firmware nodes) need to be combined (as is done in the platform hub
471 * of the device registry).
472 *
473 * Return: Returns zero on success, nonzero on failure.
 
 
 
 
 
 
 
 
474 */
475int __ssam_register_clients(struct device *parent, struct ssam_controller *ctrl,
476			    struct fwnode_handle *node)
477{
478	struct fwnode_handle *child;
479	int status;
480
481	fwnode_for_each_child_node(node, child) {
482		/*
483		 * Try to add the device specified in the firmware node. If
484		 * this fails with -ENODEV, the node does not specify any SSAM
485		 * device, so ignore it and continue with the next one.
486		 */
487		status = ssam_add_client_device(parent, ctrl, child);
488		if (status && status != -ENODEV)
489			goto err;
490	}
491
492	return 0;
493err:
494	ssam_remove_clients(parent);
495	return status;
496}
497EXPORT_SYMBOL_GPL(__ssam_register_clients);
498
499static int ssam_remove_device(struct device *dev, void *_data)
 
 
 
500{
501	struct ssam_device *sdev = to_ssam_device(dev);
502
503	if (is_ssam_device(dev))
504		ssam_device_remove(sdev);
505
506	return 0;
507}
508
509/**
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.
513 *
514 * Remove all SSAM client devices registered as direct children under the given
515 * device. Note that this only accounts for direct children of the device.
516 * Refer to ssam_device_add()/ssam_device_remove() for more details.
517 */
518void ssam_remove_clients(struct device *dev)
519{
520	device_for_each_child_reverse(dev, NULL, ssam_remove_device);
521}
522EXPORT_SYMBOL_GPL(ssam_remove_clients);
v5.14.15
  1// SPDX-License-Identifier: GPL-2.0+
  2/*
  3 * Surface System Aggregator Module bus and device integration.
  4 *
  5 * Copyright (C) 2019-2021 Maximilian Luz <luzmaximilian@gmail.com>
  6 */
  7
  8#include <linux/device.h>
 
  9#include <linux/slab.h>
 10
 11#include <linux/surface_aggregator/controller.h>
 12#include <linux/surface_aggregator/device.h>
 13
 14#include "bus.h"
 15#include "controller.h"
 16
 
 
 
 17static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
 18			     char *buf)
 19{
 20	struct ssam_device *sdev = to_ssam_device(dev);
 21
 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);
 25}
 26static DEVICE_ATTR_RO(modalias);
 27
 28static struct attribute *ssam_device_attrs[] = {
 29	&dev_attr_modalias.attr,
 30	NULL,
 31};
 32ATTRIBUTE_GROUPS(ssam_device);
 33
 34static int ssam_device_uevent(struct device *dev, struct kobj_uevent_env *env)
 35{
 36	struct ssam_device *sdev = to_ssam_device(dev);
 37
 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,
 41			      sdev->uid.function);
 42}
 43
 44static void ssam_device_release(struct device *dev)
 45{
 46	struct ssam_device *sdev = to_ssam_device(dev);
 47
 48	ssam_controller_put(sdev->ctrl);
 
 49	kfree(sdev);
 50}
 51
 52const 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,
 57};
 58EXPORT_SYMBOL_GPL(ssam_device_type);
 59
 60/**
 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.
 64 *
 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.
 69 *
 70 * Return: Returns the newly allocated and initialized SSAM client device, or
 71 * %NULL if it could not be allocated.
 72 */
 73struct ssam_device *ssam_device_alloc(struct ssam_controller *ctrl,
 74				      struct ssam_device_uid uid)
 75{
 76	struct ssam_device *sdev;
 77
 78	sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
 79	if (!sdev)
 80		return NULL;
 81
 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);
 87	sdev->uid = uid;
 88
 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);
 92
 93	return sdev;
 94}
 95EXPORT_SYMBOL_GPL(ssam_device_alloc);
 96
 97/**
 98 * ssam_device_add() - Add a SSAM client device.
 99 * @sdev: The SSAM client device to be added.
100 *
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
104 * shut down.
105 *
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.
110 *
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.
119 *
120 * Return: Returns zero on success, a negative error code on failure.
121 */
122int ssam_device_add(struct ssam_device *sdev)
123{
124	int status;
125
126	/*
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
136	 * them.
137	 *
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.
142	 */
143	ssam_controller_statelock(sdev->ctrl);
144
145	if (sdev->ctrl->state != SSAM_CONTROLLER_STARTED) {
146		ssam_controller_stateunlock(sdev->ctrl);
147		return -ENODEV;
148	}
149
150	status = device_add(&sdev->dev);
151
152	ssam_controller_stateunlock(sdev->ctrl);
153	return status;
154}
155EXPORT_SYMBOL_GPL(ssam_device_add);
156
157/**
158 * ssam_device_remove() - Remove a SSAM client device.
159 * @sdev: The device to remove.
160 *
161 * Removes and unregisters the provided SSAM client device.
162 */
163void ssam_device_remove(struct ssam_device *sdev)
164{
165	device_unregister(&sdev->dev);
166}
167EXPORT_SYMBOL_GPL(ssam_device_remove);
168
169/**
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.
173 *
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.
177 *
178 * Return: Returns %true if the given UID is compatible to the match rule
179 * described by the given ID, %false otherwise.
180 */
181static bool ssam_device_id_compatible(const struct ssam_device_id *id,
182				      struct ssam_device_uid uid)
183{
184	if (id->domain != uid.domain || id->category != uid.category)
185		return false;
186
187	if ((id->match_flags & SSAM_MATCH_TARGET) && id->target != uid.target)
188		return false;
189
190	if ((id->match_flags & SSAM_MATCH_INSTANCE) && id->instance != uid.instance)
191		return false;
192
193	if ((id->match_flags & SSAM_MATCH_FUNCTION) && id->function != uid.function)
194		return false;
195
196	return true;
197}
198
199/**
200 * ssam_device_id_is_null() - Check if a device ID is null.
201 * @id: The device ID to check.
202 *
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.
205 *
206 * Return: Returns %true if the given ID represents a null ID, %false
207 * otherwise.
208 */
209static bool ssam_device_id_is_null(const struct ssam_device_id *id)
210{
211	return id->match_flags == 0 &&
212		id->domain == 0 &&
213		id->category == 0 &&
214		id->target == 0 &&
215		id->instance == 0 &&
216		id->function == 0 &&
217		id->driver_data == 0;
218}
219
220/**
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.
224 *
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.
227 */
228const struct ssam_device_id *ssam_device_id_match(const struct ssam_device_id *table,
229						  const struct ssam_device_uid uid)
230{
231	const struct ssam_device_id *id;
232
233	for (id = table; !ssam_device_id_is_null(id); ++id)
234		if (ssam_device_id_compatible(id, uid))
235			return id;
236
237	return NULL;
238}
239EXPORT_SYMBOL_GPL(ssam_device_id_match);
240
241/**
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.
245 *
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.
250 *
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.
253 *
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.
256 */
257const struct ssam_device_id *ssam_device_get_match(const struct ssam_device *dev)
258{
259	const struct ssam_device_driver *sdrv;
260
261	sdrv = to_ssam_device_driver(dev->dev.driver);
262	if (!sdrv)
263		return NULL;
264
265	if (!sdrv->match_table)
266		return NULL;
267
268	return ssam_device_id_match(sdrv->match_table, dev->uid);
269}
270EXPORT_SYMBOL_GPL(ssam_device_get_match);
271
272/**
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.
276 *
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.
282 *
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.
285 *
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
288 * could be found.
289 */
290const void *ssam_device_get_match_data(const struct ssam_device *dev)
291{
292	const struct ssam_device_id *id;
293
294	id = ssam_device_get_match(dev);
295	if (!id)
296		return NULL;
297
298	return (const void *)id->driver_data;
299}
300EXPORT_SYMBOL_GPL(ssam_device_get_match_data);
301
302static int ssam_bus_match(struct device *dev, struct device_driver *drv)
303{
304	struct ssam_device_driver *sdrv = to_ssam_device_driver(drv);
305	struct ssam_device *sdev = to_ssam_device(dev);
306
307	if (!is_ssam_device(dev))
308		return 0;
309
310	return !!ssam_device_id_match(sdrv->match_table, sdev->uid);
311}
312
313static int ssam_bus_probe(struct device *dev)
314{
315	return to_ssam_device_driver(dev->driver)
316		->probe(to_ssam_device(dev));
317}
318
319static int ssam_bus_remove(struct device *dev)
320{
321	struct ssam_device_driver *sdrv = to_ssam_device_driver(dev->driver);
322
323	if (sdrv->remove)
324		sdrv->remove(to_ssam_device(dev));
325
326	return 0;
327}
328
329struct bus_type ssam_bus_type = {
330	.name   = "surface_aggregator",
331	.match  = ssam_bus_match,
332	.probe  = ssam_bus_probe,
333	.remove = ssam_bus_remove,
334};
335EXPORT_SYMBOL_GPL(ssam_bus_type);
336
337/**
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.
341 *
342 * Please refer to the ssam_device_driver_register() macro for the normal way
343 * to register a driver from inside its owning module.
344 */
345int __ssam_device_driver_register(struct ssam_device_driver *sdrv,
346				  struct module *owner)
347{
348	sdrv->driver.owner = owner;
349	sdrv->driver.bus = &ssam_bus_type;
350
351	/* force drivers to async probe so I/O is possible in probe */
352	sdrv->driver.probe_type = PROBE_PREFER_ASYNCHRONOUS;
353
354	return driver_register(&sdrv->driver);
355}
356EXPORT_SYMBOL_GPL(__ssam_device_driver_register);
357
358/**
359 * ssam_device_driver_unregister - Unregister a SSAM device driver.
360 * @sdrv: The driver to unregister.
361 */
362void ssam_device_driver_unregister(struct ssam_device_driver *sdrv)
363{
364	driver_unregister(&sdrv->driver);
365}
366EXPORT_SYMBOL_GPL(ssam_device_driver_unregister);
367
368static int ssam_remove_device(struct device *dev, void *_data)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
369{
370	struct ssam_device *sdev = to_ssam_device(dev);
 
371
372	if (is_ssam_device(dev))
373		ssam_device_remove(sdev);
 
 
 
 
 
 
 
374
375	return 0;
376}
377
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
378/**
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.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
382 *
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.
388 *
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.
392 */
393void ssam_controller_remove_clients(struct ssam_controller *ctrl)
 
394{
395	struct device *dev;
 
396
397	dev = ssam_controller_device(ctrl);
398	device_for_each_child_reverse(dev, NULL, ssam_remove_device);
 
 
 
 
 
 
 
 
 
 
 
 
 
399}
 
400
401/**
402 * ssam_bus_register() - Register and set-up the SSAM client device bus.
403 */
404int ssam_bus_register(void)
405{
406	return bus_register(&ssam_bus_type);
 
 
 
 
 
407}
408
409/**
410 * ssam_bus_unregister() - Unregister the SSAM client device bus.
 
 
 
 
 
 
411 */
412void ssam_bus_unregister(void)
413{
414	return bus_unregister(&ssam_bus_type);
415}