client.rst 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. .. SPDX-License-Identifier: GPL-2.0+
  2. .. |ssam_controller| replace:: :c:type:`struct ssam_controller <ssam_controller>`
  3. .. |ssam_device| replace:: :c:type:`struct ssam_device <ssam_device>`
  4. .. |ssam_device_driver| replace:: :c:type:`struct ssam_device_driver <ssam_device_driver>`
  5. .. |ssam_client_bind| replace:: :c:func:`ssam_client_bind`
  6. .. |ssam_client_link| replace:: :c:func:`ssam_client_link`
  7. .. |ssam_get_controller| replace:: :c:func:`ssam_get_controller`
  8. .. |ssam_controller_get| replace:: :c:func:`ssam_controller_get`
  9. .. |ssam_controller_put| replace:: :c:func:`ssam_controller_put`
  10. .. |ssam_device_alloc| replace:: :c:func:`ssam_device_alloc`
  11. .. |ssam_device_add| replace:: :c:func:`ssam_device_add`
  12. .. |ssam_device_remove| replace:: :c:func:`ssam_device_remove`
  13. .. |ssam_device_driver_register| replace:: :c:func:`ssam_device_driver_register`
  14. .. |ssam_device_driver_unregister| replace:: :c:func:`ssam_device_driver_unregister`
  15. .. |module_ssam_device_driver| replace:: :c:func:`module_ssam_device_driver`
  16. .. |SSAM_DEVICE| replace:: :c:func:`SSAM_DEVICE`
  17. .. |ssam_notifier_register| replace:: :c:func:`ssam_notifier_register`
  18. .. |ssam_notifier_unregister| replace:: :c:func:`ssam_notifier_unregister`
  19. .. |ssam_device_notifier_register| replace:: :c:func:`ssam_device_notifier_register`
  20. .. |ssam_device_notifier_unregister| replace:: :c:func:`ssam_device_notifier_unregister`
  21. .. |ssam_request_sync| replace:: :c:func:`ssam_request_sync`
  22. .. |ssam_event_mask| replace:: :c:type:`enum ssam_event_mask <ssam_event_mask>`
  23. ======================
  24. Writing Client Drivers
  25. ======================
  26. For the API documentation, refer to:
  27. .. toctree::
  28. :maxdepth: 2
  29. client-api
  30. Overview
  31. ========
  32. Client drivers can be set up in two main ways, depending on how the
  33. corresponding device is made available to the system. We specifically
  34. differentiate between devices that are presented to the system via one of
  35. the conventional ways, e.g. as platform devices via ACPI, and devices that
  36. are non-discoverable and instead need to be explicitly provided by some
  37. other mechanism, as discussed further below.
  38. Non-SSAM Client Drivers
  39. =======================
  40. All communication with the SAM EC is handled via the |ssam_controller|
  41. representing that EC to the kernel. Drivers targeting a non-SSAM device (and
  42. thus not being a |ssam_device_driver|) need to explicitly establish a
  43. connection/relation to that controller. This can be done via the
  44. |ssam_client_bind| function. Said function returns a reference to the SSAM
  45. controller, but, more importantly, also establishes a device link between
  46. client device and controller (this can also be done separate via
  47. |ssam_client_link|). It is important to do this, as it, first, guarantees
  48. that the returned controller is valid for use in the client driver for as
  49. long as this driver is bound to its device, i.e. that the driver gets
  50. unbound before the controller ever becomes invalid, and, second, as it
  51. ensures correct suspend/resume ordering. This setup should be done in the
  52. driver's probe function, and may be used to defer probing in case the SSAM
  53. subsystem is not ready yet, for example:
  54. .. code-block:: c
  55. static int client_driver_probe(struct platform_device *pdev)
  56. {
  57. struct ssam_controller *ctrl;
  58. ctrl = ssam_client_bind(&pdev->dev);
  59. if (IS_ERR(ctrl))
  60. return PTR_ERR(ctrl) == -ENODEV ? -EPROBE_DEFER : PTR_ERR(ctrl);
  61. // ...
  62. return 0;
  63. }
  64. The controller may be separately obtained via |ssam_get_controller| and its
  65. lifetime be guaranteed via |ssam_controller_get| and |ssam_controller_put|.
  66. Note that none of these functions, however, guarantee that the controller
  67. will not be shut down or suspended. These functions essentially only operate
  68. on the reference, i.e. only guarantee a bare minimum of accessibility
  69. without any guarantees at all on practical operability.
  70. Adding SSAM Devices
  71. ===================
  72. If a device does not already exist/is not already provided via conventional
  73. means, it should be provided as |ssam_device| via the SSAM client device
  74. hub. New devices can be added to this hub by entering their UID into the
  75. corresponding registry. SSAM devices can also be manually allocated via
  76. |ssam_device_alloc|, subsequently to which they have to be added via
  77. |ssam_device_add| and eventually removed via |ssam_device_remove|. By
  78. default, the parent of the device is set to the controller device provided
  79. for allocation, however this may be changed before the device is added. Note
  80. that, when changing the parent device, care must be taken to ensure that the
  81. controller lifetime and suspend/resume ordering guarantees, in the default
  82. setup provided through the parent-child relation, are preserved. If
  83. necessary, by use of |ssam_client_link| as is done for non-SSAM client
  84. drivers and described in more detail above.
  85. A client device must always be removed by the party which added the
  86. respective device before the controller shuts down. Such removal can be
  87. guaranteed by linking the driver providing the SSAM device to the controller
  88. via |ssam_client_link|, causing it to unbind before the controller driver
  89. unbinds. Client devices registered with the controller as parent are
  90. automatically removed when the controller shuts down, but this should not be
  91. relied upon, especially as this does not extend to client devices with a
  92. different parent.
  93. SSAM Client Drivers
  94. ===================
  95. SSAM client device drivers are, in essence, no different than other device
  96. driver types. They are represented via |ssam_device_driver| and bind to a
  97. |ssam_device| via its UID (:c:type:`struct ssam_device.uid <ssam_device>`)
  98. member and the match table
  99. (:c:type:`struct ssam_device_driver.match_table <ssam_device_driver>`),
  100. which should be set when declaring the driver struct instance. Refer to the
  101. |SSAM_DEVICE| macro documentation for more details on how to define members
  102. of the driver's match table.
  103. The UID for SSAM client devices consists of a ``domain``, a ``category``,
  104. a ``target``, an ``instance``, and a ``function``. The ``domain`` is used
  105. differentiate between physical SAM devices
  106. (:c:type:`SSAM_DOMAIN_SERIALHUB <ssam_device_domain>`), i.e. devices that can
  107. be accessed via the Surface Serial Hub, and virtual ones
  108. (:c:type:`SSAM_DOMAIN_VIRTUAL <ssam_device_domain>`), such as client-device
  109. hubs, that have no real representation on the SAM EC and are solely used on
  110. the kernel/driver-side. For physical devices, ``category`` represents the
  111. target category, ``target`` the target ID, and ``instance`` the instance ID
  112. used to access the physical SAM device. In addition, ``function`` references
  113. a specific device functionality, but has no meaning to the SAM EC. The
  114. (default) name of a client device is generated based on its UID.
  115. A driver instance can be registered via |ssam_device_driver_register| and
  116. unregistered via |ssam_device_driver_unregister|. For convenience, the
  117. |module_ssam_device_driver| macro may be used to define module init- and
  118. exit-functions registering the driver.
  119. The controller associated with a SSAM client device can be found in its
  120. :c:type:`struct ssam_device.ctrl <ssam_device>` member. This reference is
  121. guaranteed to be valid for at least as long as the client driver is bound,
  122. but should also be valid for as long as the client device exists. Note,
  123. however, that access outside of the bound client driver must ensure that the
  124. controller device is not suspended while making any requests or
  125. (un-)registering event notifiers (and thus should generally be avoided). This
  126. is guaranteed when the controller is accessed from inside the bound client
  127. driver.
  128. Making Synchronous Requests
  129. ===========================
  130. Synchronous requests are (currently) the main form of host-initiated
  131. communication with the EC. There are a couple of ways to define and execute
  132. such requests, however, most of them boil down to something similar as shown
  133. in the example below. This example defines a write-read request, meaning
  134. that the caller provides an argument to the SAM EC and receives a response.
  135. The caller needs to know the (maximum) length of the response payload and
  136. provide a buffer for it.
  137. Care must be taken to ensure that any command payload data passed to the SAM
  138. EC is provided in little-endian format and, similarly, any response payload
  139. data received from it is converted from little-endian to host endianness.
  140. .. code-block:: c
  141. int perform_request(struct ssam_controller *ctrl, u32 arg, u32 *ret)
  142. {
  143. struct ssam_request rqst;
  144. struct ssam_response resp;
  145. int status;
  146. /* Convert request argument to little-endian. */
  147. __le32 arg_le = cpu_to_le32(arg);
  148. __le32 ret_le = cpu_to_le32(0);
  149. /*
  150. * Initialize request specification. Replace this with your values.
  151. * The rqst.payload field may be NULL if rqst.length is zero,
  152. * indicating that the request does not have any argument.
  153. *
  154. * Note: The request parameters used here are not valid, i.e.
  155. * they do not correspond to an actual SAM/EC request.
  156. */
  157. rqst.target_category = SSAM_SSH_TC_SAM;
  158. rqst.target_id = 0x01;
  159. rqst.command_id = 0x02;
  160. rqst.instance_id = 0x03;
  161. rqst.flags = SSAM_REQUEST_HAS_RESPONSE;
  162. rqst.length = sizeof(arg_le);
  163. rqst.payload = (u8 *)&arg_le;
  164. /* Initialize request response. */
  165. resp.capacity = sizeof(ret_le);
  166. resp.length = 0;
  167. resp.pointer = (u8 *)&ret_le;
  168. /*
  169. * Perform actual request. The response pointer may be null in case
  170. * the request does not have any response. This must be consistent
  171. * with the SSAM_REQUEST_HAS_RESPONSE flag set in the specification
  172. * above.
  173. */
  174. status = ssam_request_sync(ctrl, &rqst, &resp);
  175. /*
  176. * Alternatively use
  177. *
  178. * ssam_request_sync_onstack(ctrl, &rqst, &resp, sizeof(arg_le));
  179. *
  180. * to perform the request, allocating the message buffer directly
  181. * on the stack as opposed to allocation via kzalloc().
  182. */
  183. /*
  184. * Convert request response back to native format. Note that in the
  185. * error case, this value is not touched by the SSAM core, i.e.
  186. * 'ret_le' will be zero as specified in its initialization.
  187. */
  188. *ret = le32_to_cpu(ret_le);
  189. return status;
  190. }
  191. Note that |ssam_request_sync| in its essence is a wrapper over lower-level
  192. request primitives, which may also be used to perform requests. Refer to its
  193. implementation and documentation for more details.
  194. An arguably more user-friendly way of defining such functions is by using
  195. one of the generator macros, for example via:
  196. .. code-block:: c
  197. SSAM_DEFINE_SYNC_REQUEST_W(__ssam_tmp_perf_mode_set, __le32, {
  198. .target_category = SSAM_SSH_TC_TMP,
  199. .target_id = 0x01,
  200. .command_id = 0x03,
  201. .instance_id = 0x00,
  202. });
  203. This example defines a function
  204. .. code-block:: c
  205. static int __ssam_tmp_perf_mode_set(struct ssam_controller *ctrl, const __le32 *arg);
  206. executing the specified request, with the controller passed in when calling
  207. said function. In this example, the argument is provided via the ``arg``
  208. pointer. Note that the generated function allocates the message buffer on
  209. the stack. Thus, if the argument provided via the request is large, these
  210. kinds of macros should be avoided. Also note that, in contrast to the
  211. previous non-macro example, this function does not do any endianness
  212. conversion, which has to be handled by the caller. Apart from those
  213. differences the function generated by the macro is similar to the one
  214. provided in the non-macro example above.
  215. The full list of such function-generating macros is
  216. - :c:func:`SSAM_DEFINE_SYNC_REQUEST_N` for requests without return value and
  217. without argument.
  218. - :c:func:`SSAM_DEFINE_SYNC_REQUEST_R` for requests with return value but no
  219. argument.
  220. - :c:func:`SSAM_DEFINE_SYNC_REQUEST_W` for requests without return value but
  221. with argument.
  222. Refer to their respective documentation for more details. For each one of
  223. these macros, a special variant is provided, which targets request types
  224. applicable to multiple instances of the same device type:
  225. - :c:func:`SSAM_DEFINE_SYNC_REQUEST_MD_N`
  226. - :c:func:`SSAM_DEFINE_SYNC_REQUEST_MD_R`
  227. - :c:func:`SSAM_DEFINE_SYNC_REQUEST_MD_W`
  228. The difference of those macros to the previously mentioned versions is, that
  229. the device target and instance IDs are not fixed for the generated function,
  230. but instead have to be provided by the caller of said function.
  231. Additionally, variants for direct use with client devices, i.e.
  232. |ssam_device|, are also provided. These can, for example, be used as
  233. follows:
  234. .. code-block:: c
  235. SSAM_DEFINE_SYNC_REQUEST_CL_R(ssam_bat_get_sta, __le32, {
  236. .target_category = SSAM_SSH_TC_BAT,
  237. .command_id = 0x01,
  238. });
  239. This invocation of the macro defines a function
  240. .. code-block:: c
  241. static int ssam_bat_get_sta(struct ssam_device *sdev, __le32 *ret);
  242. executing the specified request, using the device IDs and controller given
  243. in the client device. The full list of such macros for client devices is:
  244. - :c:func:`SSAM_DEFINE_SYNC_REQUEST_CL_N`
  245. - :c:func:`SSAM_DEFINE_SYNC_REQUEST_CL_R`
  246. - :c:func:`SSAM_DEFINE_SYNC_REQUEST_CL_W`
  247. Handling Events
  248. ===============
  249. To receive events from the SAM EC, an event notifier must be registered for
  250. the desired event via |ssam_notifier_register|. The notifier must be
  251. unregistered via |ssam_notifier_unregister| once it is not required any
  252. more. For |ssam_device| type clients, the |ssam_device_notifier_register| and
  253. |ssam_device_notifier_unregister| wrappers should be preferred as they properly
  254. handle hot-removal of client devices.
  255. Event notifiers are registered by providing (at minimum) a callback to call
  256. in case an event has been received, the registry specifying how the event
  257. should be enabled, an event ID specifying for which target category and,
  258. optionally and depending on the registry used, for which instance ID events
  259. should be enabled, and finally, flags describing how the EC will send these
  260. events. If the specific registry does not enable events by instance ID, the
  261. instance ID must be set to zero. Additionally, a priority for the respective
  262. notifier may be specified, which determines its order in relation to any
  263. other notifier registered for the same target category.
  264. By default, event notifiers will receive all events for the specific target
  265. category, regardless of the instance ID specified when registering the
  266. notifier. The core may be instructed to only call a notifier if the target
  267. ID or instance ID (or both) of the event match the ones implied by the
  268. notifier IDs (in case of target ID, the target ID of the registry), by
  269. providing an event mask (see |ssam_event_mask|).
  270. In general, the target ID of the registry is also the target ID of the
  271. enabled event (with the notable exception being keyboard input events on the
  272. Surface Laptop 1 and 2, which are enabled via a registry with target ID 1,
  273. but provide events with target ID 2).
  274. A full example for registering an event notifier and handling received
  275. events is provided below:
  276. .. code-block:: c
  277. u32 notifier_callback(struct ssam_event_notifier *nf,
  278. const struct ssam_event *event)
  279. {
  280. int status = ...
  281. /* Handle the event here ... */
  282. /* Convert return value and indicate that we handled the event. */
  283. return ssam_notifier_from_errno(status) | SSAM_NOTIF_HANDLED;
  284. }
  285. int setup_notifier(struct ssam_device *sdev,
  286. struct ssam_event_notifier *nf)
  287. {
  288. /* Set priority wrt. other handlers of same target category. */
  289. nf->base.priority = 1;
  290. /* Set event/notifier callback. */
  291. nf->base.fn = notifier_callback;
  292. /* Specify event registry, i.e. how events get enabled/disabled. */
  293. nf->event.reg = SSAM_EVENT_REGISTRY_KIP;
  294. /* Specify which event to enable/disable */
  295. nf->event.id.target_category = sdev->uid.category;
  296. nf->event.id.instance = sdev->uid.instance;
  297. /*
  298. * Specify for which events the notifier callback gets executed.
  299. * This essentially tells the core if it can skip notifiers that
  300. * don't have target or instance IDs matching those of the event.
  301. */
  302. nf->event.mask = SSAM_EVENT_MASK_STRICT;
  303. /* Specify event flags. */
  304. nf->event.flags = SSAM_EVENT_SEQUENCED;
  305. return ssam_notifier_register(sdev->ctrl, nf);
  306. }
  307. Multiple event notifiers can be registered for the same event. The event
  308. handler core takes care of enabling and disabling events when notifiers are
  309. registered and unregistered, by keeping track of how many notifiers for a
  310. specific event (combination of registry, event target category, and event
  311. instance ID) are currently registered. This means that a specific event will
  312. be enabled when the first notifier for it is being registered and disabled
  313. when the last notifier for it is being unregistered. Note that the event
  314. flags are therefore only used on the first registered notifier, however, one
  315. should take care that notifiers for a specific event are always registered
  316. with the same flag and it is considered a bug to do otherwise.