digital.h 8.8 KB

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