st21nfca.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * Copyright (C) 2014 STMicroelectronics SAS. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #ifndef __LOCAL_ST21NFCA_H_
  17. #define __LOCAL_ST21NFCA_H_
  18. #include <net/nfc/hci.h>
  19. #include <linux/skbuff.h>
  20. #include <linux/workqueue.h>
  21. #define HCI_MODE 0
  22. /* framing in HCI mode */
  23. #define ST21NFCA_SOF_EOF_LEN 2
  24. /* Almost every time value is 0 */
  25. #define ST21NFCA_HCI_LLC_LEN 1
  26. /* Size in worst case :
  27. * In normal case CRC len = 2 but byte stuffing
  28. * may appear in case one CRC byte = ST21NFCA_SOF_EOF
  29. */
  30. #define ST21NFCA_HCI_LLC_CRC 4
  31. #define ST21NFCA_HCI_LLC_LEN_CRC (ST21NFCA_SOF_EOF_LEN + \
  32. ST21NFCA_HCI_LLC_LEN + \
  33. ST21NFCA_HCI_LLC_CRC)
  34. #define ST21NFCA_HCI_LLC_MIN_SIZE (1 + ST21NFCA_HCI_LLC_LEN_CRC)
  35. /* Worst case when adding byte stuffing between each byte */
  36. #define ST21NFCA_HCI_LLC_MAX_PAYLOAD 29
  37. #define ST21NFCA_HCI_LLC_MAX_SIZE (ST21NFCA_HCI_LLC_LEN_CRC + 1 + \
  38. ST21NFCA_HCI_LLC_MAX_PAYLOAD)
  39. /* Reader RF commands */
  40. #define ST21NFCA_WR_XCHG_DATA 0x10
  41. #define ST21NFCA_DEVICE_MGNT_GATE 0x01
  42. #define ST21NFCA_RF_READER_F_GATE 0x14
  43. #define ST21NFCA_RF_CARD_F_GATE 0x24
  44. #define ST21NFCA_APDU_READER_GATE 0xf0
  45. #define ST21NFCA_CONNECTIVITY_GATE 0x41
  46. /*
  47. * ref ISO7816-3 chap 8.1. the initial character TS is followed by a
  48. * sequence of at most 32 characters.
  49. */
  50. #define ST21NFCA_ESE_MAX_LENGTH 33
  51. #define ST21NFCA_ESE_HOST_ID 0xc0
  52. #define DRIVER_DESC "HCI NFC driver for ST21NFCA"
  53. #define ST21NFCA_HCI_MODE 0
  54. #define ST21NFCA_NUM_DEVICES 256
  55. #define ST21NFCA_VENDOR_OUI 0x0080E1 /* STMicroelectronics */
  56. #define ST21NFCA_FACTORY_MODE 2
  57. struct st21nfca_se_status {
  58. bool is_ese_present;
  59. bool is_uicc_present;
  60. };
  61. enum st21nfca_state {
  62. ST21NFCA_ST_COLD,
  63. ST21NFCA_ST_READY,
  64. };
  65. /**
  66. * enum nfc_vendor_cmds - supported nfc vendor commands
  67. *
  68. * @FACTORY_MODE: Allow to set the driver into a mode where no secure element
  69. * are activated. It does not consider any NFC_ATTR_VENDOR_DATA.
  70. * @HCI_CLEAR_ALL_PIPES: Allow to execute a HCI clear all pipes command.
  71. * It does not consider any NFC_ATTR_VENDOR_DATA.
  72. * @HCI_DM_PUT_DATA: Allow to configure specific CLF registry as for example
  73. * RF trimmings or low level drivers configurations (I2C, SPI, SWP).
  74. * @HCI_DM_UPDATE_AID: Allow to configure an AID routing into the CLF routing
  75. * table following RF technology, CLF mode or protocol.
  76. * @HCI_DM_GET_INFO: Allow to retrieve CLF information.
  77. * @HCI_DM_GET_DATA: Allow to retrieve CLF configurable data such as low
  78. * level drivers configurations or RF trimmings.
  79. * @HCI_DM_LOAD: Allow to load a firmware into the CLF. A complete
  80. * packet can be more than 8KB.
  81. * @HCI_DM_RESET: Allow to run a CLF reset in order to "commit" CLF
  82. * configuration changes without CLF power off.
  83. * @HCI_GET_PARAM: Allow to retrieve an HCI CLF parameter (for example the
  84. * white list).
  85. * @HCI_DM_FIELD_GENERATOR: Allow to generate different kind of RF
  86. * technology. When using this command to anti-collision is done.
  87. * @HCI_LOOPBACK: Allow to echo a command and test the Dh to CLF
  88. * connectivity.
  89. */
  90. enum nfc_vendor_cmds {
  91. FACTORY_MODE,
  92. HCI_CLEAR_ALL_PIPES,
  93. HCI_DM_PUT_DATA,
  94. HCI_DM_UPDATE_AID,
  95. HCI_DM_GET_INFO,
  96. HCI_DM_GET_DATA,
  97. HCI_DM_LOAD,
  98. HCI_DM_RESET,
  99. HCI_GET_PARAM,
  100. HCI_DM_FIELD_GENERATOR,
  101. HCI_LOOPBACK,
  102. };
  103. struct st21nfca_vendor_info {
  104. struct completion req_completion;
  105. struct sk_buff *rx_skb;
  106. };
  107. struct st21nfca_dep_info {
  108. struct sk_buff *tx_pending;
  109. struct work_struct tx_work;
  110. u8 curr_nfc_dep_pni;
  111. u32 idx;
  112. u8 to;
  113. u8 did;
  114. u8 bsi;
  115. u8 bri;
  116. u8 lri;
  117. } __packed;
  118. struct st21nfca_se_info {
  119. u8 atr[ST21NFCA_ESE_MAX_LENGTH];
  120. struct completion req_completion;
  121. struct timer_list bwi_timer;
  122. int wt_timeout; /* in msecs */
  123. bool bwi_active;
  124. struct timer_list se_active_timer;
  125. bool se_active;
  126. int expected_pipes;
  127. int count_pipes;
  128. bool xch_error;
  129. se_io_cb_t cb;
  130. void *cb_context;
  131. };
  132. struct st21nfca_hci_info {
  133. struct nfc_phy_ops *phy_ops;
  134. void *phy_id;
  135. struct nfc_hci_dev *hdev;
  136. struct st21nfca_se_status *se_status;
  137. enum st21nfca_state state;
  138. struct mutex info_lock;
  139. int async_cb_type;
  140. data_exchange_cb_t async_cb;
  141. void *async_cb_context;
  142. struct st21nfca_dep_info dep_info;
  143. struct st21nfca_se_info se_info;
  144. struct st21nfca_vendor_info vendor_info;
  145. };
  146. int st21nfca_hci_probe(void *phy_id, struct nfc_phy_ops *phy_ops,
  147. char *llc_name, int phy_headroom, int phy_tailroom,
  148. int phy_payload, struct nfc_hci_dev **hdev,
  149. struct st21nfca_se_status *se_status);
  150. void st21nfca_hci_remove(struct nfc_hci_dev *hdev);
  151. int st21nfca_dep_event_received(struct nfc_hci_dev *hdev,
  152. u8 event, struct sk_buff *skb);
  153. int st21nfca_tm_send_dep_res(struct nfc_hci_dev *hdev, struct sk_buff *skb);
  154. int st21nfca_im_send_atr_req(struct nfc_hci_dev *hdev, u8 *gb, size_t gb_len);
  155. int st21nfca_im_send_dep_req(struct nfc_hci_dev *hdev, struct sk_buff *skb);
  156. void st21nfca_dep_init(struct nfc_hci_dev *hdev);
  157. void st21nfca_dep_deinit(struct nfc_hci_dev *hdev);
  158. int st21nfca_connectivity_event_received(struct nfc_hci_dev *hdev, u8 host,
  159. u8 event, struct sk_buff *skb);
  160. int st21nfca_apdu_reader_event_received(struct nfc_hci_dev *hdev,
  161. u8 event, struct sk_buff *skb);
  162. int st21nfca_hci_discover_se(struct nfc_hci_dev *hdev);
  163. int st21nfca_hci_enable_se(struct nfc_hci_dev *hdev, u32 se_idx);
  164. int st21nfca_hci_disable_se(struct nfc_hci_dev *hdev, u32 se_idx);
  165. int st21nfca_hci_se_io(struct nfc_hci_dev *hdev, u32 se_idx,
  166. u8 *apdu, size_t apdu_length,
  167. se_io_cb_t cb, void *cb_context);
  168. void st21nfca_se_init(struct nfc_hci_dev *hdev);
  169. void st21nfca_se_deinit(struct nfc_hci_dev *hdev);
  170. int st21nfca_hci_loopback_event_received(struct nfc_hci_dev *ndev, u8 event,
  171. struct sk_buff *skb);
  172. int st21nfca_vendor_cmds_init(struct nfc_hci_dev *ndev);
  173. #endif /* __LOCAL_ST21NFCA_H_ */