nfc-hci.txt 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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). The sending context blocks while waiting for the
  16. response to arrive.
  17. HCI events can also be received from the host controller. They will be handled
  18. and a translation will be forwarded to NFC Core as needed.
  19. HCI uses 2 execution contexts:
  20. - one for executing commands : nfc_hci_msg_tx_work(). Only one command
  21. can be executing at any given moment.
  22. - one for dispatching received events and commands : nfc_hci_msg_rx_work().
  23. HCI Session initialization:
  24. ---------------------------
  25. The Session initialization is an HCI standard which must unfortunately
  26. support proprietary gates. This is the reason why the driver will pass a list
  27. of proprietary gates that must be part of the session. HCI will ensure all
  28. those gates have pipes connected when the hci device is set up.
  29. HCI Gates and Pipes
  30. -------------------
  31. A gate defines the 'port' where some service can be found. In order to access
  32. a service, one must create a pipe to that gate and open it. In this
  33. implementation, pipes are totally hidden. The public API only knows gates.
  34. This is consistent with the driver need to send commands to proprietary gates
  35. without knowing the pipe connected to it.
  36. Driver interface
  37. ----------------
  38. A driver would normally register itself with HCI and provide the following
  39. entry points:
  40. struct nfc_hci_ops {
  41. int (*open)(struct nfc_hci_dev *hdev);
  42. void (*close)(struct nfc_hci_dev *hdev);
  43. int (*hci_ready) (struct nfc_hci_dev *hdev);
  44. int (*xmit)(struct nfc_hci_dev *hdev, struct sk_buff *skb);
  45. int (*start_poll)(struct nfc_hci_dev *hdev, u32 protocols);
  46. int (*target_from_gate)(struct nfc_hci_dev *hdev, u8 gate,
  47. struct nfc_target *target);
  48. int (*complete_target_discovered) (struct nfc_hci_dev *hdev, u8 gate,
  49. struct nfc_target *target);
  50. int (*data_exchange) (struct nfc_hci_dev *hdev,
  51. struct nfc_target *target,
  52. struct sk_buff *skb, struct sk_buff **res_skb);
  53. int (*check_presence)(struct nfc_hci_dev *hdev,
  54. struct nfc_target *target);
  55. };
  56. - open() and close() shall turn the hardware on and off.
  57. - hci_ready() is an optional entry point that is called right after the hci
  58. session has been set up. The driver can use it to do additional initialization
  59. that must be performed using HCI commands.
  60. - xmit() shall simply write a frame to the chip.
  61. - start_poll() is an optional entrypoint that shall set the hardware in polling
  62. mode. This must be implemented only if the hardware uses proprietary gates or a
  63. mechanism slightly different from the HCI standard.
  64. - target_from_gate() is an optional entrypoint to return the nfc protocols
  65. corresponding to a proprietary gate.
  66. - complete_target_discovered() is an optional entry point to let the driver
  67. perform additional proprietary processing necessary to auto activate the
  68. discovered target.
  69. - data_exchange() must be implemented by the driver if proprietary HCI commands
  70. are required to send data to the tag. Some tag types will require custom
  71. commands, others can be written to using the standard HCI commands. The driver
  72. can check the tag type and either do proprietary processing, or return 1 to ask
  73. for standard processing.
  74. - check_presence() is an optional entry point that will be called regularly
  75. by the core to check that an activated tag is still in the field. If this is
  76. not implemented, the core will not be able to push tag_lost events to the user
  77. space
  78. On the rx path, the driver is responsible to push incoming HCP frames to HCI
  79. using nfc_hci_recv_frame(). HCI will take care of re-aggregation and handling
  80. This must be done from a context that can sleep.
  81. SHDLC
  82. -----
  83. Most chips use shdlc to ensure integrity and delivery ordering of the HCP
  84. frames between the host controller (the chip) and hosts (entities connected
  85. to the chip, like the cpu). In order to simplify writing the driver, an shdlc
  86. layer is available for use by the driver.
  87. When used, the driver actually registers with shdlc, and shdlc will register
  88. with HCI. HCI sees shdlc as the driver and thus send its HCP frames
  89. through shdlc->xmit.
  90. SHDLC adds a new execution context (nfc_shdlc_sm_work()) to run its state
  91. machine and handle both its rx and tx path.
  92. Included Drivers
  93. ----------------
  94. An HCI based driver for an NXP PN544, connected through I2C bus, and using
  95. shdlc is included.
  96. Execution Contexts
  97. ------------------
  98. The execution contexts are the following:
  99. - IRQ handler (IRQH):
  100. fast, cannot sleep. stores incoming frames into an shdlc rx queue
  101. - SHDLC State Machine worker (SMW)
  102. handles shdlc rx & tx queues. Dispatches HCI cmd responses.
  103. - HCI Tx Cmd worker (MSGTXWQ)
  104. Serializes execution of HCI commands. Completes execution in case of response
  105. timeout.
  106. - HCI Rx worker (MSGRXWQ)
  107. Dispatches incoming HCI commands or events.
  108. - Syscall context from a userspace call (SYSCALL)
  109. Any entrypoint in HCI called from NFC Core
  110. Workflow executing an HCI command (using shdlc)
  111. -----------------------------------------------
  112. Executing an HCI command can easily be performed synchronously using the
  113. following API:
  114. int nfc_hci_send_cmd (struct nfc_hci_dev *hdev, u8 gate, u8 cmd,
  115. const u8 *param, size_t param_len, struct sk_buff **skb)
  116. The API must be invoked from a context that can sleep. Most of the time, this
  117. will be the syscall context. skb will return the result that was received in
  118. the response.
  119. Internally, execution is asynchronous. So all this API does is to enqueue the
  120. HCI command, setup a local wait queue on stack, and wait_event() for completion.
  121. The wait is not interruptible because it is guaranteed that the command will
  122. complete after some short timeout anyway.
  123. MSGTXWQ context will then be scheduled and invoke nfc_hci_msg_tx_work().
  124. This function will dequeue the next pending command and send its HCP fragments
  125. to the lower layer which happens to be shdlc. It will then start a timer to be
  126. able to complete the command with a timeout error if no response arrive.
  127. SMW context gets scheduled and invokes nfc_shdlc_sm_work(). This function
  128. handles shdlc framing in and out. It uses the driver xmit to send frames and
  129. receives incoming frames in an skb queue filled from the driver IRQ handler.
  130. SHDLC I(nformation) frames payload are HCP fragments. They are aggregated to
  131. form complete HCI frames, which can be a response, command, or event.
  132. HCI Responses are dispatched immediately from this context to unblock
  133. waiting command execution. Response processing involves invoking the completion
  134. callback that was provided by nfc_hci_msg_tx_work() when it sent the command.
  135. The completion callback will then wake the syscall context.
  136. Workflow receiving an HCI event or command
  137. ------------------------------------------
  138. HCI commands or events are not dispatched from SMW context. Instead, they are
  139. queued to HCI rx_queue and will be dispatched from HCI rx worker
  140. context (MSGRXWQ). This is done this way to allow a cmd or event handler
  141. to also execute other commands (for example, handling the
  142. NFC_HCI_EVT_TARGET_DISCOVERED event from PN544 requires to issue an
  143. ANY_GET_PARAMETER to the reader A gate to get information on the target
  144. that was discovered).
  145. Typically, such an event will be propagated to NFC Core from MSGRXWQ context.
  146. Error management
  147. ----------------
  148. Errors that occur synchronously with the execution of an NFC Core request are
  149. simply returned as the execution result of the request. These are easy.
  150. Errors that occur asynchronously (e.g. in a background protocol handling thread)
  151. must be reported such that upper layers don't stay ignorant that something
  152. went wrong below and know that expected events will probably never happen.
  153. Handling of these errors is done as follows:
  154. - driver (pn544) fails to deliver an incoming frame: it stores the error such
  155. that any subsequent call to the driver will result in this error. Then it calls
  156. the standard nfc_shdlc_recv_frame() with a NULL argument to report the problem
  157. above. shdlc stores a EREMOTEIO sticky status, which will trigger SMW to
  158. report above in turn.
  159. - SMW is basically a background thread to handle incoming and outgoing shdlc
  160. frames. This thread will also check the shdlc sticky status and report to HCI
  161. when it discovers it is not able to run anymore because of an unrecoverable
  162. error that happened within shdlc or below. If the problem occurs during shdlc
  163. connection, the error is reported through the connect completion.
  164. - HCI: if an internal HCI error happens (frame is lost), or HCI is reported an
  165. error from a lower layer, HCI will either complete the currently executing
  166. command with that error, or notify NFC Core directly if no command is executing.
  167. - NFC Core: when NFC Core is notified of an error from below and polling is
  168. active, it will send a tag discovered event with an empty tag list to the user
  169. space to let it know that the poll operation will never be able to detect a tag.
  170. If polling is not active and the error was sticky, lower levels will return it
  171. at next invocation.