1*4791d77aSRobert Schwebel=================== 2*4791d77aSRobert SchwebelLinux NFC subsystem 3*4791d77aSRobert Schwebel=================== 4*4791d77aSRobert Schwebel 5*4791d77aSRobert SchwebelThe Near Field Communication (NFC) subsystem is required to standardize the 6*4791d77aSRobert SchwebelNFC device drivers development and to create an unified userspace interface. 7*4791d77aSRobert Schwebel 8*4791d77aSRobert SchwebelThis document covers the architecture overview, the device driver interface 9*4791d77aSRobert Schwebeldescription and the userspace interface description. 10*4791d77aSRobert Schwebel 11*4791d77aSRobert SchwebelArchitecture overview 12*4791d77aSRobert Schwebel===================== 13*4791d77aSRobert Schwebel 14*4791d77aSRobert SchwebelThe NFC subsystem is responsible for: 15*4791d77aSRobert Schwebel - NFC adapters management; 16*4791d77aSRobert Schwebel - Polling for targets; 17*4791d77aSRobert Schwebel - Low-level data exchange; 18*4791d77aSRobert Schwebel 19*4791d77aSRobert SchwebelThe subsystem is divided in some parts. The 'core' is responsible for 20*4791d77aSRobert Schwebelproviding the device driver interface. On the other side, it is also 21*4791d77aSRobert Schwebelresponsible for providing an interface to control operations and low-level 22*4791d77aSRobert Schwebeldata exchange. 23*4791d77aSRobert Schwebel 24*4791d77aSRobert SchwebelThe control operations are available to userspace via generic netlink. 25*4791d77aSRobert Schwebel 26*4791d77aSRobert SchwebelThe low-level data exchange interface is provided by the new socket family 27*4791d77aSRobert SchwebelPF_NFC. The NFC_SOCKPROTO_RAW performs raw communication with NFC targets. 28*4791d77aSRobert Schwebel 29*4791d77aSRobert Schwebel.. code-block:: none 30*4791d77aSRobert Schwebel 31*4791d77aSRobert Schwebel +--------------------------------------+ 32*4791d77aSRobert Schwebel | USER SPACE | 33*4791d77aSRobert Schwebel +--------------------------------------+ 34*4791d77aSRobert Schwebel ^ ^ 35*4791d77aSRobert Schwebel | low-level | control 36*4791d77aSRobert Schwebel | data exchange | operations 37*4791d77aSRobert Schwebel | | 38*4791d77aSRobert Schwebel | v 39*4791d77aSRobert Schwebel | +-----------+ 40*4791d77aSRobert Schwebel | AF_NFC | netlink | 41*4791d77aSRobert Schwebel | socket +-----------+ 42*4791d77aSRobert Schwebel | raw ^ 43*4791d77aSRobert Schwebel | | 44*4791d77aSRobert Schwebel v v 45*4791d77aSRobert Schwebel +---------+ +-----------+ 46*4791d77aSRobert Schwebel | rawsock | <--------> | core | 47*4791d77aSRobert Schwebel +---------+ +-----------+ 48*4791d77aSRobert Schwebel ^ 49*4791d77aSRobert Schwebel | 50*4791d77aSRobert Schwebel v 51*4791d77aSRobert Schwebel +-----------+ 52*4791d77aSRobert Schwebel | driver | 53*4791d77aSRobert Schwebel +-----------+ 54*4791d77aSRobert Schwebel 55*4791d77aSRobert SchwebelDevice Driver Interface 56*4791d77aSRobert Schwebel======================= 57*4791d77aSRobert Schwebel 58*4791d77aSRobert SchwebelWhen registering on the NFC subsystem, the device driver must inform the core 59*4791d77aSRobert Schwebelof the set of supported NFC protocols and the set of ops callbacks. The ops 60*4791d77aSRobert Schwebelcallbacks that must be implemented are the following: 61*4791d77aSRobert Schwebel 62*4791d77aSRobert Schwebel* start_poll - setup the device to poll for targets 63*4791d77aSRobert Schwebel* stop_poll - stop on progress polling operation 64*4791d77aSRobert Schwebel* activate_target - select and initialize one of the targets found 65*4791d77aSRobert Schwebel* deactivate_target - deselect and deinitialize the selected target 66*4791d77aSRobert Schwebel* data_exchange - send data and receive the response (transceive operation) 67*4791d77aSRobert Schwebel 68*4791d77aSRobert SchwebelUserspace interface 69*4791d77aSRobert Schwebel=================== 70*4791d77aSRobert Schwebel 71*4791d77aSRobert SchwebelThe userspace interface is divided in control operations and low-level data 72*4791d77aSRobert Schwebelexchange operation. 73*4791d77aSRobert Schwebel 74*4791d77aSRobert SchwebelCONTROL OPERATIONS: 75*4791d77aSRobert Schwebel 76*4791d77aSRobert SchwebelGeneric netlink is used to implement the interface to the control operations. 77*4791d77aSRobert SchwebelThe operations are composed by commands and events, all listed below: 78*4791d77aSRobert Schwebel 79*4791d77aSRobert Schwebel* NFC_CMD_GET_DEVICE - get specific device info or dump the device list 80*4791d77aSRobert Schwebel* NFC_CMD_START_POLL - setup a specific device to polling for targets 81*4791d77aSRobert Schwebel* NFC_CMD_STOP_POLL - stop the polling operation in a specific device 82*4791d77aSRobert Schwebel* NFC_CMD_GET_TARGET - dump the list of targets found by a specific device 83*4791d77aSRobert Schwebel 84*4791d77aSRobert Schwebel* NFC_EVENT_DEVICE_ADDED - reports an NFC device addition 85*4791d77aSRobert Schwebel* NFC_EVENT_DEVICE_REMOVED - reports an NFC device removal 86*4791d77aSRobert Schwebel* NFC_EVENT_TARGETS_FOUND - reports START_POLL results when 1 or more targets 87*4791d77aSRobert Schwebel are found 88*4791d77aSRobert Schwebel 89*4791d77aSRobert SchwebelThe user must call START_POLL to poll for NFC targets, passing the desired NFC 90*4791d77aSRobert Schwebelprotocols through NFC_ATTR_PROTOCOLS attribute. The device remains in polling 91*4791d77aSRobert Schwebelstate until it finds any target. However, the user can stop the polling 92*4791d77aSRobert Schwebeloperation by calling STOP_POLL command. In this case, it will be checked if 93*4791d77aSRobert Schwebelthe requester of STOP_POLL is the same of START_POLL. 94*4791d77aSRobert Schwebel 95*4791d77aSRobert SchwebelIf the polling operation finds one or more targets, the event TARGETS_FOUND is 96*4791d77aSRobert Schwebelsent (including the device id). The user must call GET_TARGET to get the list of 97*4791d77aSRobert Schwebelall targets found by such device. Each reply message has target attributes with 98*4791d77aSRobert Schwebelrelevant information such as the supported NFC protocols. 99*4791d77aSRobert Schwebel 100*4791d77aSRobert SchwebelAll polling operations requested through one netlink socket are stopped when 101*4791d77aSRobert Schwebelit's closed. 102*4791d77aSRobert Schwebel 103*4791d77aSRobert SchwebelLOW-LEVEL DATA EXCHANGE: 104*4791d77aSRobert Schwebel 105*4791d77aSRobert SchwebelThe userspace must use PF_NFC sockets to perform any data communication with 106*4791d77aSRobert Schwebeltargets. All NFC sockets use AF_NFC:: 107*4791d77aSRobert Schwebel 108*4791d77aSRobert Schwebel struct sockaddr_nfc { 109*4791d77aSRobert Schwebel sa_family_t sa_family; 110*4791d77aSRobert Schwebel __u32 dev_idx; 111*4791d77aSRobert Schwebel __u32 target_idx; 112*4791d77aSRobert Schwebel __u32 nfc_protocol; 113*4791d77aSRobert Schwebel }; 114*4791d77aSRobert Schwebel 115*4791d77aSRobert SchwebelTo establish a connection with one target, the user must create an 116*4791d77aSRobert SchwebelNFC_SOCKPROTO_RAW socket and call the 'connect' syscall with the sockaddr_nfc 117*4791d77aSRobert Schwebelstruct correctly filled. All information comes from NFC_EVENT_TARGETS_FOUND 118*4791d77aSRobert Schwebelnetlink event. As a target can support more than one NFC protocol, the user 119*4791d77aSRobert Schwebelmust inform which protocol it wants to use. 120*4791d77aSRobert Schwebel 121*4791d77aSRobert SchwebelInternally, 'connect' will result in an activate_target call to the driver. 122*4791d77aSRobert SchwebelWhen the socket is closed, the target is deactivated. 123*4791d77aSRobert Schwebel 124*4791d77aSRobert SchwebelThe data format exchanged through the sockets is NFC protocol dependent. For 125*4791d77aSRobert Schwebelinstance, when communicating with MIFARE tags, the data exchanged are MIFARE 126*4791d77aSRobert Schwebelcommands and their responses. 127*4791d77aSRobert Schwebel 128*4791d77aSRobert SchwebelThe first received package is the response to the first sent package and so 129*4791d77aSRobert Schwebelon. In order to allow valid "empty" responses, every data received has a NULL 130*4791d77aSRobert Schwebelheader of 1 byte. 131