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