nfc-hci.txt 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 if for executing commands : nfc_hci_msg_tx_work(). Only one command
  21. can be executing at any given moment.
  22. - one if for dispatching received events and responses : 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 (*xmit)(struct nfc_hci_dev *hdev, struct sk_buff *skb);
  44. int (*start_poll)(struct nfc_hci_dev *hdev, u32 protocols);
  45. int (*target_from_gate)(struct nfc_hci_dev *hdev, u8 gate,
  46. struct nfc_target *target);
  47. };
  48. open() and close() shall turn the hardware on and off. xmit() shall simply
  49. write a frame to the chip. start_poll() is an optional entrypoint that shall
  50. set the hardware in polling mode. This must be implemented only if the hardware
  51. uses proprietary gates or a mechanism slightly different from the HCI standard.
  52. target_from_gate() is another optional entrypoint to return the protocols
  53. corresponding to a proprietary gate.
  54. On the rx path, the driver is responsible to push incoming HCP frames to HCI
  55. using nfc_hci_recv_frame(). HCI will take care of re-aggregation and handling
  56. This must be done from a context that can sleep.
  57. SHDLC
  58. -----
  59. Most chips use shdlc to ensure integrity and delivery ordering of the HCP
  60. frames between the host controller (the chip) and hosts (entities connected
  61. to the chip, like the cpu). In order to simplify writing the driver, an shdlc
  62. layer is available for use by the driver.
  63. When used, the driver actually registers with shdlc, and shdlc will register
  64. with HCI. HCI sees shdlc as the driver and thus send its HCP frames
  65. through shdlc->xmit.
  66. SHDLC adds a new execution context (nfc_shdlc_sm_work()) to run its state
  67. machine and handle both its rx and tx path.
  68. Included Drivers
  69. ----------------
  70. An HCI based driver for an NXP PN544, connected through I2C bus, and using
  71. shdlc is included.
  72. Execution Contexts
  73. ------------------
  74. The execution contexts are the following:
  75. - IRQ handler (IRQH):
  76. fast, cannot sleep. stores incoming frames into an shdlc rx queue
  77. - SHDLC State Machine worker (SMW)
  78. handles shdlc rx & tx queues. Dispatches HCI cmd responses.
  79. - HCI Tx Cmd worker (MSGTXWQ)
  80. Serialize execution of HCI commands. Complete execution in case of resp timeout.
  81. - HCI Rx worker (MSGRXWQ)
  82. Dispatches incoming HCI commands or events.
  83. - Syscall context from a userspace call (SYSCALL)
  84. Any entrypoint in HCI called from NFC Core
  85. Workflow executing an HCI command (using shdlc)
  86. -----------------------------------------------
  87. Executing an HCI command can easily be performed synchronously using the
  88. following API:
  89. int nfc_hci_send_cmd (struct nfc_hci_dev *hdev, u8 gate, u8 cmd,
  90. const u8 *param, size_t param_len, struct sk_buff **skb)
  91. The API must be invoked from a context that can sleep. Most of the time, this
  92. will be the syscall context. skb will return the result that was received in
  93. the response.
  94. Internally, execution is asynchronous. So all this API does is to enqueue the
  95. HCI command, setup a local wait queue on stack, and wait_event() for completion.
  96. The wait is not interruptible because it is guaranteed that the command will
  97. complete after some short timeout anyway.
  98. MSGTXWQ context will then be scheduled and invoke nfc_hci_msg_tx_work().
  99. This function will dequeue the next pending command and send its HCP fragments
  100. to the lower layer which happens to be shdlc. It will then start a timer to be
  101. able to complete the command with a timeout error if no response arrive.
  102. SMW context gets scheduled and invokes nfc_shdlc_sm_work(). This function
  103. handles shdlc framing in and out. It uses the driver xmit to send frames and
  104. receives incoming frames in an skb queue filled from the driver IRQ handler.
  105. SHDLC I(nformation) frames payload are HCP fragments. They are agregated to
  106. form complete HCI frames, which can be a response, command, or event.
  107. HCI Responses are dispatched immediately from this context to unblock
  108. waiting command execution. Reponse processing involves invoking the completion
  109. callback that was provided by nfc_hci_msg_tx_work() when it sent the command.
  110. The completion callback will then wake the syscall context.
  111. Workflow receiving an HCI event or command
  112. ------------------------------------------
  113. HCI commands or events are not dispatched from SMW context. Instead, they are
  114. queued to HCI rx_queue and will be dispatched from HCI rx worker
  115. context (MSGRXWQ). This is done this way to allow a cmd or event handler
  116. to also execute other commands (for example, handling the
  117. NFC_HCI_EVT_TARGET_DISCOVERED event from PN544 requires to issue an
  118. ANY_GET_PARAMETER to the reader A gate to get information on the target
  119. that was discovered).
  120. Typically, such an event will be propagated to NFC Core from MSGRXWQ context.