digital.h 7.7 KB

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