st21nfca.h 5.8 KB

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