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