digital.h 7.6 KB

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