162306a36Sopenharmony_ci===================
262306a36Sopenharmony_ciLinux NFC subsystem
362306a36Sopenharmony_ci===================
462306a36Sopenharmony_ci
562306a36Sopenharmony_ciThe Near Field Communication (NFC) subsystem is required to standardize the
662306a36Sopenharmony_ciNFC device drivers development and to create an unified userspace interface.
762306a36Sopenharmony_ci
862306a36Sopenharmony_ciThis document covers the architecture overview, the device driver interface
962306a36Sopenharmony_cidescription and the userspace interface description.
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ciArchitecture overview
1262306a36Sopenharmony_ci=====================
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ciThe NFC subsystem is responsible for:
1562306a36Sopenharmony_ci      - NFC adapters management;
1662306a36Sopenharmony_ci      - Polling for targets;
1762306a36Sopenharmony_ci      - Low-level data exchange;
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_ciThe subsystem is divided in some parts. The 'core' is responsible for
2062306a36Sopenharmony_ciproviding the device driver interface. On the other side, it is also
2162306a36Sopenharmony_ciresponsible for providing an interface to control operations and low-level
2262306a36Sopenharmony_cidata exchange.
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ciThe control operations are available to userspace via generic netlink.
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ciThe low-level data exchange interface is provided by the new socket family
2762306a36Sopenharmony_ciPF_NFC. The NFC_SOCKPROTO_RAW performs raw communication with NFC targets.
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ci.. code-block:: none
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ci        +--------------------------------------+
3262306a36Sopenharmony_ci        |              USER SPACE              |
3362306a36Sopenharmony_ci        +--------------------------------------+
3462306a36Sopenharmony_ci            ^                       ^
3562306a36Sopenharmony_ci            | low-level             | control
3662306a36Sopenharmony_ci            | data exchange         | operations
3762306a36Sopenharmony_ci            |                       |
3862306a36Sopenharmony_ci            |                       v
3962306a36Sopenharmony_ci            |                  +-----------+
4062306a36Sopenharmony_ci            | AF_NFC           |  netlink  |
4162306a36Sopenharmony_ci            | socket           +-----------+
4262306a36Sopenharmony_ci            | raw                   ^
4362306a36Sopenharmony_ci            |                       |
4462306a36Sopenharmony_ci            v                       v
4562306a36Sopenharmony_ci        +---------+            +-----------+
4662306a36Sopenharmony_ci        | rawsock | <--------> |   core    |
4762306a36Sopenharmony_ci        +---------+            +-----------+
4862306a36Sopenharmony_ci                                    ^
4962306a36Sopenharmony_ci                                    |
5062306a36Sopenharmony_ci                                    v
5162306a36Sopenharmony_ci                               +-----------+
5262306a36Sopenharmony_ci                               |  driver   |
5362306a36Sopenharmony_ci                               +-----------+
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ciDevice Driver Interface
5662306a36Sopenharmony_ci=======================
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_ciWhen registering on the NFC subsystem, the device driver must inform the core
5962306a36Sopenharmony_ciof the set of supported NFC protocols and the set of ops callbacks. The ops
6062306a36Sopenharmony_cicallbacks that must be implemented are the following:
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_ci* start_poll - setup the device to poll for targets
6362306a36Sopenharmony_ci* stop_poll - stop on progress polling operation
6462306a36Sopenharmony_ci* activate_target - select and initialize one of the targets found
6562306a36Sopenharmony_ci* deactivate_target - deselect and deinitialize the selected target
6662306a36Sopenharmony_ci* data_exchange - send data and receive the response (transceive operation)
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ciUserspace interface
6962306a36Sopenharmony_ci===================
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ciThe userspace interface is divided in control operations and low-level data
7262306a36Sopenharmony_ciexchange operation.
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ciCONTROL OPERATIONS:
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_ciGeneric netlink is used to implement the interface to the control operations.
7762306a36Sopenharmony_ciThe operations are composed by commands and events, all listed below:
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ci* NFC_CMD_GET_DEVICE - get specific device info or dump the device list
8062306a36Sopenharmony_ci* NFC_CMD_START_POLL - setup a specific device to polling for targets
8162306a36Sopenharmony_ci* NFC_CMD_STOP_POLL - stop the polling operation in a specific device
8262306a36Sopenharmony_ci* NFC_CMD_GET_TARGET - dump the list of targets found by a specific device
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ci* NFC_EVENT_DEVICE_ADDED - reports an NFC device addition
8562306a36Sopenharmony_ci* NFC_EVENT_DEVICE_REMOVED - reports an NFC device removal
8662306a36Sopenharmony_ci* NFC_EVENT_TARGETS_FOUND - reports START_POLL results when 1 or more targets
8762306a36Sopenharmony_ci  are found
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_ciThe user must call START_POLL to poll for NFC targets, passing the desired NFC
9062306a36Sopenharmony_ciprotocols through NFC_ATTR_PROTOCOLS attribute. The device remains in polling
9162306a36Sopenharmony_cistate until it finds any target. However, the user can stop the polling
9262306a36Sopenharmony_cioperation by calling STOP_POLL command. In this case, it will be checked if
9362306a36Sopenharmony_cithe requester of STOP_POLL is the same of START_POLL.
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_ciIf the polling operation finds one or more targets, the event TARGETS_FOUND is
9662306a36Sopenharmony_cisent (including the device id). The user must call GET_TARGET to get the list of
9762306a36Sopenharmony_ciall targets found by such device. Each reply message has target attributes with
9862306a36Sopenharmony_cirelevant information such as the supported NFC protocols.
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_ciAll polling operations requested through one netlink socket are stopped when
10162306a36Sopenharmony_ciit's closed.
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_ciLOW-LEVEL DATA EXCHANGE:
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ciThe userspace must use PF_NFC sockets to perform any data communication with
10662306a36Sopenharmony_citargets. All NFC sockets use AF_NFC::
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_ci        struct sockaddr_nfc {
10962306a36Sopenharmony_ci               sa_family_t sa_family;
11062306a36Sopenharmony_ci               __u32 dev_idx;
11162306a36Sopenharmony_ci               __u32 target_idx;
11262306a36Sopenharmony_ci               __u32 nfc_protocol;
11362306a36Sopenharmony_ci        };
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ciTo establish a connection with one target, the user must create an
11662306a36Sopenharmony_ciNFC_SOCKPROTO_RAW socket and call the 'connect' syscall with the sockaddr_nfc
11762306a36Sopenharmony_cistruct correctly filled. All information comes from NFC_EVENT_TARGETS_FOUND
11862306a36Sopenharmony_cinetlink event. As a target can support more than one NFC protocol, the user
11962306a36Sopenharmony_cimust inform which protocol it wants to use.
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_ciInternally, 'connect' will result in an activate_target call to the driver.
12262306a36Sopenharmony_ciWhen the socket is closed, the target is deactivated.
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_ciThe data format exchanged through the sockets is NFC protocol dependent. For
12562306a36Sopenharmony_ciinstance, when communicating with MIFARE tags, the data exchanged are MIFARE
12662306a36Sopenharmony_cicommands and their responses.
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_ciThe first received package is the response to the first sent package and so
12962306a36Sopenharmony_cion. In order to allow valid "empty" responses, every data received has a NULL
13062306a36Sopenharmony_ciheader of 1 byte.
131