bus.c 16 KB

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