← Documents Documentation/networking/nfc.rst GitHub 원문 ↗

Linux 6.18.37 · Networking

Linux NFC subsystem

Generic Netlink 제어와 PF_NFC raw socket을 제공하는 NFC core·driver architecture입니다.

Source pathDocumentation/networking/nfc.rst
Source versionLinux v6.18.37
TranslationDUJINLABS 전문 번역 + 해설

요약·해설과 원문, 전문 번역을 서로 분리했습니다. API 이름, symbol, source path는 원문 표기를 사용합니다.

1. 요약·해설

원문의 핵심 논리와 kernel programming 관점의 보충 설명입니다. 아래의 전문 번역과는 별도로 작성했습니다.

요약·해설

nfc.rst:1-130

Control plane은 Generic Netlink, target data plane은 AF_NFC raw socket을 사용하며 core가 두 경로를 driver callback에 연결합니다.

2. 영어 원문 전체

번역 기준이 된 Linux v6.18.37 원문입니다. 줄 번호는 이 버전의 파일 좌표입니다.

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

3. 한국어 전문 번역

영어 원문의 문단 순서와 의미를 유지한 전체 번역입니다. 코드, 함수명, symbol과 URL은 원문 표기를 유지합니다.

NFC driver와 userspace 표준화

1-11

Near Field Communication subsystem은 NFC 장치 driver 개발을 표준화하고 통합 userspace interface를 제공하기 위해 존재합니다. 문서는 architecture, driver interface, userspace interface를 설명합니다.

===================
Linux NFC subsystem
===================

The Near Field Communication (NFC) subsystem is required to standardize the
NFC device drivers development and to create an unified userspace interface.

This document covers the architecture overview, the device driver interface
description and the userspace interface description.

Architecture overview

Core, Generic Netlink와 PF_NFC

12-55

NFC subsystem은 adapter 관리, target polling, low-level data exchange를 담당합니다. Core는 한쪽으로 device driver interface를 제공하고 다른 쪽으로 control operation과 low-level data exchange interface를 제공합니다.

Control operation은 generic netlink로 userspace에 노출됩니다. Low-level data exchange는 새 socket family `PF_NFC`가 담당하며 `NFC_SOCKPROTO_RAW`가 NFC target과 raw communication을 수행합니다.

NFC subsystem architecture
Userspace controlGeneric NetlinkNFC coreDevice driver
Userspace low-level dataAF_NFC raw socketrawsockNFC coreDevice driver

원문의 계층 ASCII 그림을 control plane과 data plane으로 나눴습니다.

=====================

The NFC subsystem is responsible for:
      - NFC adapters management;
      - Polling for targets;
      - Low-level data exchange;

The subsystem is divided in some parts. The 'core' is responsible for
providing the device driver interface. On the other side, it is also
responsible for providing an interface to control operations and low-level
data exchange.

The control operations are available to userspace via generic netlink.

The low-level data exchange interface is provided by the new socket family
PF_NFC. The NFC_SOCKPROTO_RAW performs raw communication with NFC targets.

.. code-block:: none

        +--------------------------------------+
        |              USER SPACE              |
        +--------------------------------------+
            ^                       ^
            | low-level             | control
            | data exchange         | operations
            |                       |
            |                       v
            |                  +-----------+
            | AF_NFC           |  netlink  |
            | socket           +-----------+
            | raw                   ^
            |                       |
            v                       v
        +---------+            +-----------+
        | rawsock | <--------> |   core    |
        +---------+            +-----------+
                                    ^
                                    |
                                    v
                               +-----------+
                               |  driver   |
                               +-----------+

Device Driver Interface

Device driver callback

56-68

NFC subsystem에 등록할 때 driver는 지원하는 NFC protocol 집합과 ops callback 집합을 core에 알려야 합니다. `start_poll`은 target polling을 설정하고 `stop_poll`은 진행 중 polling을 멈춥니다. `activate_target`과 `deactivate_target`은 발견한 target 하나를 선택·초기화하거나 해제하며, `data_exchange`는 데이터를 보내고 응답을 받는 transceive를 수행합니다.

NFC driver ops
Callback역할
start_polltarget polling 시작
stop_pollpolling 중지
activate_targettarget 선택·초기화
deactivate_targettarget 선택 해제·정리
data_exchange송신 후 응답 수신

필수 callback과 역할입니다.

=======================

When registering on the NFC subsystem, the device driver must inform the core
of the set of supported NFC protocols and the set of ops callbacks. The ops
callbacks that must be implemented are the following:

* start_poll - setup the device to poll for targets
* stop_poll - stop on progress polling operation
* activate_target - select and initialize one of the targets found
* deactivate_target - deselect and deinitialize the selected target
* data_exchange - send data and receive the response (transceive operation)

Userspace interface

Userspace control과 raw data exchange

69-130

Userspace interface는 control operation과 low-level data exchange로 나뉩니다. Generic netlink command는 `NFC_CMD_GET_DEVICE`, `NFC_CMD_START_POLL`, `NFC_CMD_STOP_POLL`, `NFC_CMD_GET_TARGET`이며 각각 장치 조회, polling 시작·중지, 발견한 target 목록 조회를 수행합니다.

Event는 장치 추가·제거를 알리는 `NFC_EVENT_DEVICE_ADDED`, `NFC_EVENT_DEVICE_REMOVED`와 하나 이상의 target을 찾았을 때 결과를 알리는 `NFC_EVENT_TARGETS_FOUND`입니다.

사용자는 `NFC_ATTR_PROTOCOLS`로 원하는 protocol을 전달해 START_POLL을 호출합니다. Target을 찾을 때까지 polling하며 STOP_POLL로 중지할 수 있습니다. 이때 중지 요청자가 시작 요청자와 같은지 검사합니다. Target을 찾으면 device ID를 포함한 TARGETS_FOUND event가 오고, GET_TARGET으로 전체 목록과 지원 protocol 같은 속성을 얻습니다. 한 netlink socket에서 요청한 polling은 socket을 닫을 때 모두 중지됩니다.

실제 target data communication에는 `PF_NFC` socket을 사용하며 모든 NFC socket의 address family는 `AF_NFC`입니다. `sockaddr_nfc`는 `sa_family`, `dev_idx`, `target_idx`, `nfc_protocol`을 담습니다.

Target에 연결하려면 `NFC_SOCKPROTO_RAW` socket을 만들고 TARGETS_FOUND event에서 얻은 정보로 `sockaddr_nfc`를 채워 `connect`합니다. Target이 여러 protocol을 지원할 수 있으므로 사용할 하나를 지정해야 합니다. 내부적으로 connect는 driver의 `activate_target`을 호출하고 socket close는 target을 deactivate합니다.

Socket을 통해 교환하는 data 형식은 NFC protocol에 따라 다릅니다. MIFARE tag라면 MIFARE command와 response가 오갑니다. 수신 순서는 송신 순서와 일대일로 대응하며, 유효한 빈 응답도 표현할 수 있도록 모든 수신 data 앞에 1바이트 NULL header가 붙습니다.

NFC userspace command와 event
종류이름의미
CommandGET_DEVICE장치 정보 또는 목록
CommandSTART_POLL / STOP_POLLtarget polling 시작·중지
CommandGET_TARGET발견 target 목록
EventDEVICE_ADDED / REMOVED장치 변화
EventTARGETS_FOUNDpolling 결과와 device ID

제어 흐름의 주요 Netlink 항목입니다.

===================

The userspace interface is divided in control operations and low-level data
exchange operation.

CONTROL OPERATIONS:

Generic netlink is used to implement the interface to the control operations.
The operations are composed by commands and events, all listed below:

* NFC_CMD_GET_DEVICE - get specific device info or dump the device list
* NFC_CMD_START_POLL - setup a specific device to polling for targets
* NFC_CMD_STOP_POLL - stop the polling operation in a specific device
* NFC_CMD_GET_TARGET - dump the list of targets found by a specific device

* NFC_EVENT_DEVICE_ADDED - reports an NFC device addition
* NFC_EVENT_DEVICE_REMOVED - reports an NFC device removal
* NFC_EVENT_TARGETS_FOUND - reports START_POLL results when 1 or more targets
  are found

The user must call START_POLL to poll for NFC targets, passing the desired NFC
protocols through NFC_ATTR_PROTOCOLS attribute. The device remains in polling
state until it finds any target. However, the user can stop the polling
operation by calling STOP_POLL command. In this case, it will be checked if
the requester of STOP_POLL is the same of START_POLL.

If the polling operation finds one or more targets, the event TARGETS_FOUND is
sent (including the device id). The user must call GET_TARGET to get the list of
all targets found by such device. Each reply message has target attributes with
relevant information such as the supported NFC protocols.

All polling operations requested through one netlink socket are stopped when
it's closed.

LOW-LEVEL DATA EXCHANGE:

The userspace must use PF_NFC sockets to perform any data communication with
targets. All NFC sockets use AF_NFC::

        struct sockaddr_nfc {
               sa_family_t sa_family;
               __u32 dev_idx;
               __u32 target_idx;
               __u32 nfc_protocol;
        };

To establish a connection with one target, the user must create an
NFC_SOCKPROTO_RAW socket and call the 'connect' syscall with the sockaddr_nfc
struct correctly filled. All information comes from NFC_EVENT_TARGETS_FOUND
netlink event. As a target can support more than one NFC protocol, the user
must inform which protocol it wants to use.

Internally, 'connect' will result in an activate_target call to the driver.
When the socket is closed, the target is deactivated.

The data format exchanged through the sockets is NFC protocol dependent. For
instance, when communicating with MIFARE tags, the data exchanged are MIFARE
commands and their responses.

The first received package is the response to the first sent package and so
on. In order to allow valid "empty" responses, every data received has a NULL
header of 1 byte.