nfc.rst 5.1 KB

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