digital.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * NFC Digital Protocol stack
  4. * Copyright (c) 2013, Intel Corporation.
  5. */
  6. #ifndef __NFC_DIGITAL_H
  7. #define __NFC_DIGITAL_H
  8. #include <linux/skbuff.h>
  9. #include <net/nfc/nfc.h>
  10. /**
  11. * Configuration types for in_configure_hw and tg_configure_hw.
  12. */
  13. enum {
  14. NFC_DIGITAL_CONFIG_RF_TECH = 0,
  15. NFC_DIGITAL_CONFIG_FRAMING,
  16. };
  17. /**
  18. * RF technology values passed as param argument to in_configure_hw and
  19. * tg_configure_hw for NFC_DIGITAL_CONFIG_RF_TECH configuration type.
  20. */
  21. enum {
  22. NFC_DIGITAL_RF_TECH_106A = 0,
  23. NFC_DIGITAL_RF_TECH_212F,
  24. NFC_DIGITAL_RF_TECH_424F,
  25. NFC_DIGITAL_RF_TECH_ISO15693,
  26. NFC_DIGITAL_RF_TECH_106B,
  27. NFC_DIGITAL_RF_TECH_LAST,
  28. };
  29. /**
  30. * Framing configuration passed as param argument to in_configure_hw and
  31. * tg_configure_hw for NFC_DIGITAL_CONFIG_FRAMING configuration type.
  32. */
  33. enum {
  34. NFC_DIGITAL_FRAMING_NFCA_SHORT = 0,
  35. NFC_DIGITAL_FRAMING_NFCA_STANDARD,
  36. NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A,
  37. NFC_DIGITAL_FRAMING_NFCA_ANTICOL_COMPLETE,
  38. NFC_DIGITAL_FRAMING_NFCA_T1T,
  39. NFC_DIGITAL_FRAMING_NFCA_T2T,
  40. NFC_DIGITAL_FRAMING_NFCA_T4T,
  41. NFC_DIGITAL_FRAMING_NFCA_NFC_DEP,
  42. NFC_DIGITAL_FRAMING_NFCF,
  43. NFC_DIGITAL_FRAMING_NFCF_T3T,
  44. NFC_DIGITAL_FRAMING_NFCF_NFC_DEP,
  45. NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED,
  46. NFC_DIGITAL_FRAMING_ISO15693_INVENTORY,
  47. NFC_DIGITAL_FRAMING_ISO15693_T5T,
  48. NFC_DIGITAL_FRAMING_NFCB,
  49. NFC_DIGITAL_FRAMING_NFCB_T4T,
  50. NFC_DIGITAL_FRAMING_LAST,
  51. };
  52. #define DIGITAL_MDAA_NFCID1_SIZE 3
  53. struct digital_tg_mdaa_params {
  54. u16 sens_res;
  55. u8 nfcid1[DIGITAL_MDAA_NFCID1_SIZE];
  56. u8 sel_res;
  57. u8 nfcid2[NFC_NFCID2_MAXSIZE];
  58. u16 sc;
  59. };
  60. struct nfc_digital_dev;
  61. /**
  62. * nfc_digital_cmd_complete_t - Definition of command result callback
  63. *
  64. * @ddev: nfc_digital_device ref
  65. * @arg: user data
  66. * @resp: response data
  67. *
  68. * resp pointer can be an error code and will be checked with IS_ERR() macro.
  69. * The callback is responsible for freeing resp sk_buff.
  70. */
  71. typedef void (*nfc_digital_cmd_complete_t)(struct nfc_digital_dev *ddev,
  72. void *arg, struct sk_buff *resp);
  73. /**
  74. * Device side NFC Digital operations
  75. *
  76. * Initiator mode:
  77. * @in_configure_hw: Hardware configuration for RF technology and communication
  78. * framing in initiator mode. This is a synchronous function.
  79. * @in_send_cmd: Initiator mode data exchange using RF technology and framing
  80. * previously set with in_configure_hw. The peer response is returned
  81. * through callback cb. If an io error occurs or the peer didn't reply
  82. * within the specified timeout (ms), the error code is passed back through
  83. * the resp pointer. This is an asynchronous function.
  84. *
  85. * Target mode: Only NFC-DEP protocol is supported in target mode.
  86. * @tg_configure_hw: Hardware configuration for RF technology and communication
  87. * framing in target mode. This is a synchronous function.
  88. * @tg_send_cmd: Target mode data exchange using RF technology and framing
  89. * previously set with tg_configure_hw. The peer next command is returned
  90. * through callback cb. If an io error occurs or the peer didn't reply
  91. * within the specified timeout (ms), the error code is passed back through
  92. * the resp pointer. This is an asynchronous function.
  93. * @tg_listen: Put the device in listen mode waiting for data from the peer
  94. * device. This is an asynchronous function.
  95. * @tg_listen_mdaa: If supported, put the device in automatic listen mode with
  96. * mode detection and automatic anti-collision. In this mode, the device
  97. * automatically detects the RF technology and executes the anti-collision
  98. * detection using the command responses specified in mdaa_params. The
  99. * mdaa_params structure contains SENS_RES, NFCID1, and SEL_RES for 106A RF
  100. * tech. NFCID2 and system code (sc) for 212F and 424F. The driver returns
  101. * the NFC-DEP ATR_REQ command through cb. The digital stack deducts the RF
  102. * tech by analyzing the SoD of the frame containing the ATR_REQ command.
  103. * This is an asynchronous function.
  104. * @tg_listen_md: If supported, put the device in automatic listen mode with
  105. * mode detection but without automatic anti-collision. In this mode, the
  106. * device automatically detects the RF technology. What the actual
  107. * RF technology is can be retrieved by calling @tg_get_rf_tech.
  108. * The digital stack will then perform the appropriate anti-collision
  109. * sequence. This is an asynchronous function.
  110. * @tg_get_rf_tech: Required when @tg_listen_md is supported, unused otherwise.
  111. * Return the RF Technology that was detected by the @tg_listen_md call.
  112. * This is a synchronous function.
  113. *
  114. * @switch_rf: Turns device radio on or off. The stack does not call explicitly
  115. * switch_rf to turn the radio on. A call to in|tg_configure_hw must turn
  116. * the device radio on.
  117. * @abort_cmd: Discard the last sent command.
  118. *
  119. * Notes: Asynchronous functions have a timeout parameter. It is the driver
  120. * responsibility to call the digital stack back through the
  121. * nfc_digital_cmd_complete_t callback when no RF respsonse has been
  122. * received within the specified time (in milliseconds). In that case the
  123. * driver must set the resp sk_buff to ERR_PTR(-ETIMEDOUT).
  124. * Since the digital stack serializes commands to be sent, it's mandatory
  125. * for the driver to handle the timeout correctly. Otherwise the stack
  126. * would not be able to send new commands, waiting for the reply of the
  127. * current one.
  128. */
  129. struct nfc_digital_ops {
  130. int (*in_configure_hw)(struct nfc_digital_dev *ddev, int type,
  131. int param);
  132. int (*in_send_cmd)(struct nfc_digital_dev *ddev, struct sk_buff *skb,
  133. u16 timeout, nfc_digital_cmd_complete_t cb,
  134. void *arg);
  135. int (*tg_configure_hw)(struct nfc_digital_dev *ddev, int type,
  136. int param);
  137. int (*tg_send_cmd)(struct nfc_digital_dev *ddev, struct sk_buff *skb,
  138. u16 timeout, nfc_digital_cmd_complete_t cb,
  139. void *arg);
  140. int (*tg_listen)(struct nfc_digital_dev *ddev, u16 timeout,
  141. nfc_digital_cmd_complete_t cb, void *arg);
  142. int (*tg_listen_mdaa)(struct nfc_digital_dev *ddev,
  143. struct digital_tg_mdaa_params *mdaa_params,
  144. u16 timeout, nfc_digital_cmd_complete_t cb,
  145. void *arg);
  146. int (*tg_listen_md)(struct nfc_digital_dev *ddev, u16 timeout,
  147. nfc_digital_cmd_complete_t cb, void *arg);
  148. int (*tg_get_rf_tech)(struct nfc_digital_dev *ddev, u8 *rf_tech);
  149. int (*switch_rf)(struct nfc_digital_dev *ddev, bool on);
  150. void (*abort_cmd)(struct nfc_digital_dev *ddev);
  151. };
  152. #define NFC_DIGITAL_POLL_MODE_COUNT_MAX 6 /* 106A, 212F, and 424F in & tg */
  153. typedef int (*digital_poll_t)(struct nfc_digital_dev *ddev, u8 rf_tech);
  154. struct digital_poll_tech {
  155. u8 rf_tech;
  156. digital_poll_t poll_func;
  157. };
  158. /**
  159. * Driver capabilities - bit mask made of the following values
  160. *
  161. * @NFC_DIGITAL_DRV_CAPS_IN_CRC: The driver handles CRC calculation in initiator
  162. * mode.
  163. * @NFC_DIGITAL_DRV_CAPS_TG_CRC: The driver handles CRC calculation in target
  164. * mode.
  165. */
  166. #define NFC_DIGITAL_DRV_CAPS_IN_CRC 0x0001
  167. #define NFC_DIGITAL_DRV_CAPS_TG_CRC 0x0002
  168. struct nfc_digital_dev {
  169. struct nfc_dev *nfc_dev;
  170. const struct nfc_digital_ops *ops;
  171. u32 protocols;
  172. int tx_headroom;
  173. int tx_tailroom;
  174. u32 driver_capabilities;
  175. void *driver_data;
  176. struct digital_poll_tech poll_techs[NFC_DIGITAL_POLL_MODE_COUNT_MAX];
  177. u8 poll_tech_count;
  178. u8 poll_tech_index;
  179. struct mutex poll_lock;
  180. struct work_struct cmd_work;
  181. struct work_struct cmd_complete_work;
  182. struct list_head cmd_queue;
  183. struct mutex cmd_lock;
  184. struct delayed_work poll_work;
  185. u8 curr_protocol;
  186. u8 curr_rf_tech;
  187. u8 curr_nfc_dep_pni;
  188. u8 did;
  189. u16 dep_rwt;
  190. u8 local_payload_max;
  191. u8 remote_payload_max;
  192. struct sk_buff *chaining_skb;
  193. struct digital_data_exch *data_exch;
  194. int atn_count;
  195. int nack_count;
  196. struct sk_buff *saved_skb;
  197. u16 target_fsc;
  198. int (*skb_check_crc)(struct sk_buff *skb);
  199. void (*skb_add_crc)(struct sk_buff *skb);
  200. };
  201. struct nfc_digital_dev *nfc_digital_allocate_device(const struct nfc_digital_ops *ops,
  202. __u32 supported_protocols,
  203. __u32 driver_capabilities,
  204. int tx_headroom,
  205. int tx_tailroom);
  206. void nfc_digital_free_device(struct nfc_digital_dev *ndev);
  207. int nfc_digital_register_device(struct nfc_digital_dev *ndev);
  208. void nfc_digital_unregister_device(struct nfc_digital_dev *ndev);
  209. static inline void nfc_digital_set_parent_dev(struct nfc_digital_dev *ndev,
  210. struct device *dev)
  211. {
  212. nfc_set_parent_dev(ndev->nfc_dev, dev);
  213. }
  214. static inline void nfc_digital_set_drvdata(struct nfc_digital_dev *dev,
  215. void *data)
  216. {
  217. dev->driver_data = data;
  218. }
  219. static inline void *nfc_digital_get_drvdata(struct nfc_digital_dev *dev)
  220. {
  221. return dev->driver_data;
  222. }
  223. #endif /* __NFC_DIGITAL_H */