digital.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. struct nfc_digital_ops {
  112. int (*in_configure_hw)(struct nfc_digital_dev *ddev, int type,
  113. int param);
  114. int (*in_send_cmd)(struct nfc_digital_dev *ddev, struct sk_buff *skb,
  115. u16 timeout, nfc_digital_cmd_complete_t cb,
  116. void *arg);
  117. int (*tg_configure_hw)(struct nfc_digital_dev *ddev, int type,
  118. int param);
  119. int (*tg_send_cmd)(struct nfc_digital_dev *ddev, struct sk_buff *skb,
  120. u16 timeout, nfc_digital_cmd_complete_t cb,
  121. void *arg);
  122. int (*tg_listen)(struct nfc_digital_dev *ddev, u16 timeout,
  123. nfc_digital_cmd_complete_t cb, void *arg);
  124. int (*tg_listen_mdaa)(struct nfc_digital_dev *ddev,
  125. struct digital_tg_mdaa_params *mdaa_params,
  126. u16 timeout, nfc_digital_cmd_complete_t cb,
  127. void *arg);
  128. int (*switch_rf)(struct nfc_digital_dev *ddev, bool on);
  129. void (*abort_cmd)(struct nfc_digital_dev *ddev);
  130. };
  131. #define NFC_DIGITAL_POLL_MODE_COUNT_MAX 6 /* 106A, 212F, and 424F in & tg */
  132. typedef int (*digital_poll_t)(struct nfc_digital_dev *ddev, u8 rf_tech);
  133. struct digital_poll_tech {
  134. u8 rf_tech;
  135. digital_poll_t poll_func;
  136. };
  137. /**
  138. * Driver capabilities - bit mask made of the following values
  139. *
  140. * @NFC_DIGITAL_DRV_CAPS_IN_CRC: The driver handles CRC calculation in initiator
  141. * mode.
  142. * @NFC_DIGITAL_DRV_CAPS_TG_CRC: The driver handles CRC calculation in target
  143. * mode.
  144. */
  145. #define NFC_DIGITAL_DRV_CAPS_IN_CRC 0x0001
  146. #define NFC_DIGITAL_DRV_CAPS_TG_CRC 0x0002
  147. struct nfc_digital_dev {
  148. struct nfc_dev *nfc_dev;
  149. struct nfc_digital_ops *ops;
  150. u32 protocols;
  151. int tx_headroom;
  152. int tx_tailroom;
  153. u32 driver_capabilities;
  154. void *driver_data;
  155. struct digital_poll_tech poll_techs[NFC_DIGITAL_POLL_MODE_COUNT_MAX];
  156. u8 poll_tech_count;
  157. u8 poll_tech_index;
  158. struct mutex poll_lock;
  159. struct work_struct cmd_work;
  160. struct work_struct cmd_complete_work;
  161. struct list_head cmd_queue;
  162. struct mutex cmd_lock;
  163. struct work_struct poll_work;
  164. u8 curr_protocol;
  165. u8 curr_rf_tech;
  166. u8 curr_nfc_dep_pni;
  167. int (*skb_check_crc)(struct sk_buff *skb);
  168. void (*skb_add_crc)(struct sk_buff *skb);
  169. };
  170. struct nfc_digital_dev *nfc_digital_allocate_device(struct nfc_digital_ops *ops,
  171. __u32 supported_protocols,
  172. __u32 driver_capabilities,
  173. int tx_headroom,
  174. int tx_tailroom);
  175. void nfc_digital_free_device(struct nfc_digital_dev *ndev);
  176. int nfc_digital_register_device(struct nfc_digital_dev *ndev);
  177. void nfc_digital_unregister_device(struct nfc_digital_dev *ndev);
  178. static inline void nfc_digital_set_parent_dev(struct nfc_digital_dev *ndev,
  179. struct device *dev)
  180. {
  181. nfc_set_parent_dev(ndev->nfc_dev, dev);
  182. }
  183. static inline void nfc_digital_set_drvdata(struct nfc_digital_dev *dev,
  184. void *data)
  185. {
  186. dev->driver_data = data;
  187. }
  188. static inline void *nfc_digital_get_drvdata(struct nfc_digital_dev *dev)
  189. {
  190. return dev->driver_data;
  191. }
  192. #endif /* __NFC_DIGITAL_H */