nfc-hci.txt 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. HCI backend for NFC Core
  2. Author: Eric Lapuyade, Samuel Ortiz
  3. Contact: eric.lapuyade@intel.com, samuel.ortiz@intel.com
  4. General
  5. -------
  6. The HCI layer implements much of the ETSI TS 102 622 V10.2.0 specification. It
  7. enables easy writing of HCI-based NFC drivers. The HCI layer runs as an NFC Core
  8. backend, implementing an abstract nfc device and translating NFC Core API
  9. to HCI commands and events.
  10. HCI
  11. ---
  12. HCI registers as an nfc device with NFC Core. Requests coming from userspace are
  13. routed through netlink sockets to NFC Core and then to HCI. From this point,
  14. they are translated in a sequence of HCI commands sent to the HCI layer in the
  15. host controller (the chip). Commands can be executed synchronously (the sending
  16. context blocks waiting for response) or asynchronously (the response is returned
  17. from HCI Rx context).
  18. HCI events can also be received from the host controller. They will be handled
  19. and a translation will be forwarded to NFC Core as needed. There are hooks to
  20. let the HCI driver handle proprietary events or override standard behavior.
  21. HCI uses 2 execution contexts:
  22. - one for executing commands : nfc_hci_msg_tx_work(). Only one command
  23. can be executing at any given moment.
  24. - one for dispatching received events and commands : nfc_hci_msg_rx_work().
  25. HCI Session initialization:
  26. ---------------------------
  27. The Session initialization is an HCI standard which must unfortunately
  28. support proprietary gates. This is the reason why the driver will pass a list
  29. of proprietary gates that must be part of the session. HCI will ensure all
  30. those gates have pipes connected when the hci device is set up.
  31. In case the chip supports pre-opened gates and pseudo-static pipes, the driver
  32. can pass that information to HCI core.
  33. HCI Gates and Pipes
  34. -------------------
  35. A gate defines the 'port' where some service can be found. In order to access
  36. a service, one must create a pipe to that gate and open it. In this
  37. implementation, pipes are totally hidden. The public API only knows gates.
  38. This is consistent with the driver need to send commands to proprietary gates
  39. without knowing the pipe connected to it.
  40. Driver interface
  41. ----------------
  42. A driver is generally written in two parts : the physical link management and
  43. the HCI management. This makes it easier to maintain a driver for a chip that
  44. can be connected using various phy (i2c, spi, ...)
  45. HCI Management
  46. --------------
  47. A driver would normally register itself with HCI and provide the following
  48. entry points:
  49. struct nfc_hci_ops {
  50. int (*open)(struct nfc_hci_dev *hdev);
  51. void (*close)(struct nfc_hci_dev *hdev);
  52. int (*hci_ready) (struct nfc_hci_dev *hdev);
  53. int (*xmit) (struct nfc_hci_dev *hdev, struct sk_buff *skb);
  54. int (*start_poll) (struct nfc_hci_dev *hdev,
  55. u32 im_protocols, u32 tm_protocols);
  56. int (*dep_link_up)(struct nfc_hci_dev *hdev, struct nfc_target *target,
  57. u8 comm_mode, u8 *gb, size_t gb_len);
  58. int (*dep_link_down)(struct nfc_hci_dev *hdev);
  59. int (*target_from_gate) (struct nfc_hci_dev *hdev, u8 gate,
  60. struct nfc_target *target);
  61. int (*complete_target_discovered) (struct nfc_hci_dev *hdev, u8 gate,
  62. struct nfc_target *target);
  63. int (*im_transceive) (struct nfc_hci_dev *hdev,
  64. struct nfc_target *target, struct sk_buff *skb,
  65. data_exchange_cb_t cb, void *cb_context);
  66. int (*tm_send)(struct nfc_hci_dev *hdev, struct sk_buff *skb);
  67. int (*check_presence)(struct nfc_hci_dev *hdev,
  68. struct nfc_target *target);
  69. int (*event_received)(struct nfc_hci_dev *hdev, u8 gate, u8 event,
  70. struct sk_buff *skb);
  71. };
  72. - open() and close() shall turn the hardware on and off.
  73. - hci_ready() is an optional entry point that is called right after the hci
  74. session has been set up. The driver can use it to do additional initialization
  75. that must be performed using HCI commands.
  76. - xmit() shall simply write a frame to the physical link.
  77. - start_poll() is an optional entrypoint that shall set the hardware in polling
  78. mode. This must be implemented only if the hardware uses proprietary gates or a
  79. mechanism slightly different from the HCI standard.
  80. - dep_link_up() is called after a p2p target has been detected, to finish
  81. the p2p connection setup with hardware parameters that need to be passed back
  82. to nfc core.
  83. - dep_link_down() is called to bring the p2p link down.
  84. - target_from_gate() is an optional entrypoint to return the nfc protocols
  85. corresponding to a proprietary gate.
  86. - complete_target_discovered() is an optional entry point to let the driver
  87. perform additional proprietary processing necessary to auto activate the
  88. discovered target.
  89. - im_transceive() must be implemented by the driver if proprietary HCI commands
  90. are required to send data to the tag. Some tag types will require custom
  91. commands, others can be written to using the standard HCI commands. The driver
  92. can check the tag type and either do proprietary processing, or return 1 to ask
  93. for standard processing. The data exchange command itself must be sent
  94. asynchronously.
  95. - tm_send() is called to send data in the case of a p2p connection
  96. - check_presence() is an optional entry point that will be called regularly
  97. by the core to check that an activated tag is still in the field. If this is
  98. not implemented, the core will not be able to push tag_lost events to the user
  99. space
  100. - event_received() is called to handle an event coming from the chip. Driver
  101. can handle the event or return 1 to let HCI attempt standard processing.
  102. On the rx path, the driver is responsible to push incoming HCP frames to HCI
  103. using nfc_hci_recv_frame(). HCI will take care of re-aggregation and handling
  104. This must be done from a context that can sleep.
  105. PHY Management
  106. --------------
  107. The physical link (i2c, ...) management is defined by the following structure:
  108. struct nfc_phy_ops {
  109. int (*write)(void *dev_id, struct sk_buff *skb);
  110. int (*enable)(void *dev_id);
  111. void (*disable)(void *dev_id);
  112. };
  113. enable(): turn the phy on (power on), make it ready to transfer data
  114. disable(): turn the phy off
  115. write(): Send a data frame to the chip. Note that to enable higher
  116. layers such as an llc to store the frame for re-emission, this function must
  117. not alter the skb. It must also not return a positive result (return 0 for
  118. success, negative for failure).
  119. Data coming from the chip shall be sent directly to nfc_hci_recv_frame().
  120. LLC
  121. ---
  122. Communication between the CPU and the chip often requires some link layer
  123. protocol. Those are isolated as modules managed by the HCI layer. There are
  124. currently two modules : nop (raw transfert) and shdlc.
  125. A new llc must implement the following functions:
  126. struct nfc_llc_ops {
  127. void *(*init) (struct nfc_hci_dev *hdev, xmit_to_drv_t xmit_to_drv,
  128. rcv_to_hci_t rcv_to_hci, int tx_headroom,
  129. int tx_tailroom, int *rx_headroom, int *rx_tailroom,
  130. llc_failure_t llc_failure);
  131. void (*deinit) (struct nfc_llc *llc);
  132. int (*start) (struct nfc_llc *llc);
  133. int (*stop) (struct nfc_llc *llc);
  134. void (*rcv_from_drv) (struct nfc_llc *llc, struct sk_buff *skb);
  135. int (*xmit_from_hci) (struct nfc_llc *llc, struct sk_buff *skb);
  136. };
  137. - init() : allocate and init your private storage
  138. - deinit() : cleanup
  139. - start() : establish the logical connection
  140. - stop () : terminate the logical connection
  141. - rcv_from_drv() : handle data coming from the chip, going to HCI
  142. - xmit_from_hci() : handle data sent by HCI, going to the chip
  143. The llc must be registered with nfc before it can be used. Do that by
  144. calling nfc_llc_register(const char *name, struct nfc_llc_ops *ops);
  145. Again, note that the llc does not handle the physical link. It is thus very
  146. easy to mix any physical link with any llc for a given chip driver.
  147. Included Drivers
  148. ----------------
  149. An HCI based driver for an NXP PN544, connected through I2C bus, and using
  150. shdlc is included.
  151. Execution Contexts
  152. ------------------
  153. The execution contexts are the following:
  154. - IRQ handler (IRQH):
  155. fast, cannot sleep. sends incoming frames to HCI where they are passed to
  156. the current llc. In case of shdlc, the frame is queued in shdlc rx queue.
  157. - SHDLC State Machine worker (SMW)
  158. Only when llc_shdlc is used: handles shdlc rx & tx queues.
  159. Dispatches HCI cmd responses.
  160. - HCI Tx Cmd worker (MSGTXWQ)
  161. Serializes execution of HCI commands. Completes execution in case of response
  162. timeout.
  163. - HCI Rx worker (MSGRXWQ)
  164. Dispatches incoming HCI commands or events.
  165. - Syscall context from a userspace call (SYSCALL)
  166. Any entrypoint in HCI called from NFC Core
  167. Workflow executing an HCI command (using shdlc)
  168. -----------------------------------------------
  169. Executing an HCI command can easily be performed synchronously using the
  170. following API:
  171. int nfc_hci_send_cmd (struct nfc_hci_dev *hdev, u8 gate, u8 cmd,
  172. const u8 *param, size_t param_len, struct sk_buff **skb)
  173. The API must be invoked from a context that can sleep. Most of the time, this
  174. will be the syscall context. skb will return the result that was received in
  175. the response.
  176. Internally, execution is asynchronous. So all this API does is to enqueue the
  177. HCI command, setup a local wait queue on stack, and wait_event() for completion.
  178. The wait is not interruptible because it is guaranteed that the command will
  179. complete after some short timeout anyway.
  180. MSGTXWQ context will then be scheduled and invoke nfc_hci_msg_tx_work().
  181. This function will dequeue the next pending command and send its HCP fragments
  182. to the lower layer which happens to be shdlc. It will then start a timer to be
  183. able to complete the command with a timeout error if no response arrive.
  184. SMW context gets scheduled and invokes nfc_shdlc_sm_work(). This function
  185. handles shdlc framing in and out. It uses the driver xmit to send frames and
  186. receives incoming frames in an skb queue filled from the driver IRQ handler.
  187. SHDLC I(nformation) frames payload are HCP fragments. They are aggregated to
  188. form complete HCI frames, which can be a response, command, or event.
  189. HCI Responses are dispatched immediately from this context to unblock
  190. waiting command execution. Response processing involves invoking the completion
  191. callback that was provided by nfc_hci_msg_tx_work() when it sent the command.
  192. The completion callback will then wake the syscall context.
  193. It is also possible to execute the command asynchronously using this API:
  194. static int nfc_hci_execute_cmd_async(struct nfc_hci_dev *hdev, u8 pipe, u8 cmd,
  195. const u8 *param, size_t param_len,
  196. data_exchange_cb_t cb, void *cb_context)
  197. The workflow is the same, except that the API call returns immediately, and
  198. the callback will be called with the result from the SMW context.
  199. Workflow receiving an HCI event or command
  200. ------------------------------------------
  201. HCI commands or events are not dispatched from SMW context. Instead, they are
  202. queued to HCI rx_queue and will be dispatched from HCI rx worker
  203. context (MSGRXWQ). This is done this way to allow a cmd or event handler
  204. to also execute other commands (for example, handling the
  205. NFC_HCI_EVT_TARGET_DISCOVERED event from PN544 requires to issue an
  206. ANY_GET_PARAMETER to the reader A gate to get information on the target
  207. that was discovered).
  208. Typically, such an event will be propagated to NFC Core from MSGRXWQ context.
  209. Error management
  210. ----------------
  211. Errors that occur synchronously with the execution of an NFC Core request are
  212. simply returned as the execution result of the request. These are easy.
  213. Errors that occur asynchronously (e.g. in a background protocol handling thread)
  214. must be reported such that upper layers don't stay ignorant that something
  215. went wrong below and know that expected events will probably never happen.
  216. Handling of these errors is done as follows:
  217. - driver (pn544) fails to deliver an incoming frame: it stores the error such
  218. that any subsequent call to the driver will result in this error. Then it calls
  219. the standard nfc_shdlc_recv_frame() with a NULL argument to report the problem
  220. above. shdlc stores a EREMOTEIO sticky status, which will trigger SMW to
  221. report above in turn.
  222. - SMW is basically a background thread to handle incoming and outgoing shdlc
  223. frames. This thread will also check the shdlc sticky status and report to HCI
  224. when it discovers it is not able to run anymore because of an unrecoverable
  225. error that happened within shdlc or below. If the problem occurs during shdlc
  226. connection, the error is reported through the connect completion.
  227. - HCI: if an internal HCI error happens (frame is lost), or HCI is reported an
  228. error from a lower layer, HCI will either complete the currently executing
  229. command with that error, or notify NFC Core directly if no command is executing.
  230. - NFC Core: when NFC Core is notified of an error from below and polling is
  231. active, it will send a tag discovered event with an empty tag list to the user
  232. space to let it know that the poll operation will never be able to detect a tag.
  233. If polling is not active and the error was sticky, lower levels will return it
  234. at next invocation.