···1717HCI registers as an nfc device with NFC Core. Requests coming from userspace are1818routed through netlink sockets to NFC Core and then to HCI. From this point,1919they are translated in a sequence of HCI commands sent to the HCI layer in the2020-host controller (the chip). The sending context blocks while waiting for the2121-response to arrive.2020+host controller (the chip). Commands can be executed synchronously (the sending2121+context blocks waiting for response) or asynchronously (the response is returned2222+from HCI Rx context).2223HCI events can also be received from the host controller. They will be handled2323-and a translation will be forwarded to NFC Core as needed.2424+and a translation will be forwarded to NFC Core as needed. There are hooks to2525+let the HCI driver handle proprietary events or override standard behavior.2426HCI uses 2 execution contexts:2527- one for executing commands : nfc_hci_msg_tx_work(). Only one command2628can be executing at any given moment.···3533support proprietary gates. This is the reason why the driver will pass a list3634of proprietary gates that must be part of the session. HCI will ensure all3735those gates have pipes connected when the hci device is set up.3636+In case the chip supports pre-opened gates and pseudo-static pipes, the driver3737+can pass that information to HCI core.38383939HCI Gates and Pipes4040-------------------···5046Driver interface5147----------------52484949+A driver is generally written in two parts : the physical link management and5050+the HCI management. This makes it easier to maintain a driver for a chip that5151+can be connected using various phy (i2c, spi, ...)5252+5353+HCI Management5454+--------------5555+5356A driver would normally register itself with HCI and provide the following5457entry points:5558···6453 int (*open)(struct nfc_hci_dev *hdev);6554 void (*close)(struct nfc_hci_dev *hdev);6655 int (*hci_ready) (struct nfc_hci_dev *hdev);6767- int (*xmit)(struct nfc_hci_dev *hdev, struct sk_buff *skb);6868- int (*start_poll)(struct nfc_hci_dev *hdev, u32 protocols);6969- int (*target_from_gate)(struct nfc_hci_dev *hdev, u8 gate,7070- struct nfc_target *target);5656+ int (*xmit) (struct nfc_hci_dev *hdev, struct sk_buff *skb);5757+ int (*start_poll) (struct nfc_hci_dev *hdev,5858+ u32 im_protocols, u32 tm_protocols);5959+ int (*dep_link_up)(struct nfc_hci_dev *hdev, struct nfc_target *target,6060+ u8 comm_mode, u8 *gb, size_t gb_len);6161+ int (*dep_link_down)(struct nfc_hci_dev *hdev);6262+ int (*target_from_gate) (struct nfc_hci_dev *hdev, u8 gate,6363+ struct nfc_target *target);7164 int (*complete_target_discovered) (struct nfc_hci_dev *hdev, u8 gate,7265 struct nfc_target *target);7373- int (*data_exchange) (struct nfc_hci_dev *hdev,7474- struct nfc_target *target,7575- struct sk_buff *skb, struct sk_buff **res_skb);6666+ int (*im_transceive) (struct nfc_hci_dev *hdev,6767+ struct nfc_target *target, struct sk_buff *skb,6868+ data_exchange_cb_t cb, void *cb_context);6969+ int (*tm_send)(struct nfc_hci_dev *hdev, struct sk_buff *skb);7670 int (*check_presence)(struct nfc_hci_dev *hdev,7771 struct nfc_target *target);7272+ int (*event_received)(struct nfc_hci_dev *hdev, u8 gate, u8 event,7373+ struct sk_buff *skb);7874};79758076- open() and close() shall turn the hardware on and off.8177- hci_ready() is an optional entry point that is called right after the hci8278session has been set up. The driver can use it to do additional initialization8379that must be performed using HCI commands.8484-- xmit() shall simply write a frame to the chip.8080+- xmit() shall simply write a frame to the physical link.8581- start_poll() is an optional entrypoint that shall set the hardware in polling8682mode. This must be implemented only if the hardware uses proprietary gates or a8783mechanism slightly different from the HCI standard.8484+- dep_link_up() is called after a p2p target has been detected, to finish8585+the p2p connection setup with hardware parameters that need to be passed back8686+to nfc core.8787+- dep_link_down() is called to bring the p2p link down.8888- target_from_gate() is an optional entrypoint to return the nfc protocols8989corresponding to a proprietary gate.9090- complete_target_discovered() is an optional entry point to let the driver9191perform additional proprietary processing necessary to auto activate the9292discovered target.9393-- data_exchange() must be implemented by the driver if proprietary HCI commands9393+- im_transceive() must be implemented by the driver if proprietary HCI commands9494are required to send data to the tag. Some tag types will require custom9595commands, others can be written to using the standard HCI commands. The driver9696can check the tag type and either do proprietary processing, or return 1 to ask9797-for standard processing.9797+for standard processing. The data exchange command itself must be sent9898+asynchronously.9999+- tm_send() is called to send data in the case of a p2p connection98100- check_presence() is an optional entry point that will be called regularly99101by the core to check that an activated tag is still in the field. If this is100102not implemented, the core will not be able to push tag_lost events to the user101103space104104+- event_received() is called to handle an event coming from the chip. Driver105105+can handle the event or return 1 to let HCI attempt standard processing.102106103107On the rx path, the driver is responsible to push incoming HCP frames to HCI104108using nfc_hci_recv_frame(). HCI will take care of re-aggregation and handling105109This must be done from a context that can sleep.106110107107-SHDLC108108------111111+PHY Management112112+--------------109113110110-Most chips use shdlc to ensure integrity and delivery ordering of the HCP111111-frames between the host controller (the chip) and hosts (entities connected112112-to the chip, like the cpu). In order to simplify writing the driver, an shdlc113113-layer is available for use by the driver.114114-When used, the driver actually registers with shdlc, and shdlc will register115115-with HCI. HCI sees shdlc as the driver and thus send its HCP frames116116-through shdlc->xmit.117117-SHDLC adds a new execution context (nfc_shdlc_sm_work()) to run its state118118-machine and handle both its rx and tx path.114114+The physical link (i2c, ...) management is defined by the following struture:115115+116116+struct nfc_phy_ops {117117+ int (*write)(void *dev_id, struct sk_buff *skb);118118+ int (*enable)(void *dev_id);119119+ void (*disable)(void *dev_id);120120+};121121+122122+enable(): turn the phy on (power on), make it ready to transfer data123123+disable(): turn the phy off124124+write(): Send a data frame to the chip. Note that to enable higher125125+layers such as an llc to store the frame for re-emission, this function must126126+not alter the skb. It must also not return a positive result (return 0 for127127+success, negative for failure).128128+129129+Data coming from the chip shall be sent directly to nfc_hci_recv_frame().130130+131131+LLC132132+---133133+134134+Communication between the CPU and the chip often requires some link layer135135+protocol. Those are isolated as modules managed by the HCI layer. There are136136+currently two modules : nop (raw transfert) and shdlc.137137+A new llc must implement the following functions:138138+139139+struct nfc_llc_ops {140140+ void *(*init) (struct nfc_hci_dev *hdev, xmit_to_drv_t xmit_to_drv,141141+ rcv_to_hci_t rcv_to_hci, int tx_headroom,142142+ int tx_tailroom, int *rx_headroom, int *rx_tailroom,143143+ llc_failure_t llc_failure);144144+ void (*deinit) (struct nfc_llc *llc);145145+ int (*start) (struct nfc_llc *llc);146146+ int (*stop) (struct nfc_llc *llc);147147+ void (*rcv_from_drv) (struct nfc_llc *llc, struct sk_buff *skb);148148+ int (*xmit_from_hci) (struct nfc_llc *llc, struct sk_buff *skb);149149+};150150+151151+- init() : allocate and init your private storage152152+- deinit() : cleanup153153+- start() : establish the logical connection154154+- stop () : terminate the logical connection155155+- rcv_from_drv() : handle data coming from the chip, going to HCI156156+- xmit_from_hci() : handle data sent by HCI, going to the chip157157+158158+The llc must be registered with nfc before it can be used. Do that by159159+calling nfc_llc_register(const char *name, struct nfc_llc_ops *ops);160160+161161+Again, note that the llc does not handle the physical link. It is thus very162162+easy to mix any physical link with any llc for a given chip driver.119163120164Included Drivers121165----------------···183117184118The execution contexts are the following:185119- IRQ handler (IRQH):186186-fast, cannot sleep. stores incoming frames into an shdlc rx queue120120+fast, cannot sleep. sends incoming frames to HCI where they are passed to121121+the current llc. In case of shdlc, the frame is queued in shdlc rx queue.187122188123- SHDLC State Machine worker (SMW)189189-handles shdlc rx & tx queues. Dispatches HCI cmd responses.124124+Only when llc_shdlc is used: handles shdlc rx & tx queues.125125+Dispatches HCI cmd responses.190126191127- HCI Tx Cmd worker (MSGTXWQ)192128Serializes execution of HCI commands. Completes execution in case of response···233165waiting command execution. Response processing involves invoking the completion234166callback that was provided by nfc_hci_msg_tx_work() when it sent the command.235167The completion callback will then wake the syscall context.168168+169169+It is also possible to execute the command asynchronously using this API:170170+171171+static int nfc_hci_execute_cmd_async(struct nfc_hci_dev *hdev, u8 pipe, u8 cmd,172172+ const u8 *param, size_t param_len,173173+ data_exchange_cb_t cb, void *cb_context)174174+175175+The workflow is the same, except that the API call returns immediately, and176176+the callback will be called with the result from the SMW context.236177237178Workflow receiving an HCI event or command238179------------------------------------------