hci.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * Copyright (C) 2011 Intel Corporation. All rights reserved.
  4. */
  5. #ifndef __NET_HCI_H
  6. #define __NET_HCI_H
  7. #include <linux/skbuff.h>
  8. #include <net/nfc/nfc.h>
  9. struct nfc_hci_dev;
  10. struct nfc_hci_ops {
  11. int (*open) (struct nfc_hci_dev *hdev);
  12. void (*close) (struct nfc_hci_dev *hdev);
  13. int (*load_session) (struct nfc_hci_dev *hdev);
  14. int (*hci_ready) (struct nfc_hci_dev *hdev);
  15. /*
  16. * xmit must always send the complete buffer before
  17. * returning. Returned result must be 0 for success
  18. * or negative for failure.
  19. */
  20. int (*xmit) (struct nfc_hci_dev *hdev, struct sk_buff *skb);
  21. int (*start_poll) (struct nfc_hci_dev *hdev,
  22. u32 im_protocols, u32 tm_protocols);
  23. void (*stop_poll) (struct nfc_hci_dev *hdev);
  24. int (*dep_link_up)(struct nfc_hci_dev *hdev, struct nfc_target *target,
  25. u8 comm_mode, u8 *gb, size_t gb_len);
  26. int (*dep_link_down)(struct nfc_hci_dev *hdev);
  27. int (*target_from_gate) (struct nfc_hci_dev *hdev, u8 gate,
  28. struct nfc_target *target);
  29. int (*complete_target_discovered) (struct nfc_hci_dev *hdev, u8 gate,
  30. struct nfc_target *target);
  31. int (*im_transceive) (struct nfc_hci_dev *hdev,
  32. struct nfc_target *target, struct sk_buff *skb,
  33. data_exchange_cb_t cb, void *cb_context);
  34. int (*tm_send)(struct nfc_hci_dev *hdev, struct sk_buff *skb);
  35. int (*check_presence)(struct nfc_hci_dev *hdev,
  36. struct nfc_target *target);
  37. int (*event_received)(struct nfc_hci_dev *hdev, u8 pipe, u8 event,
  38. struct sk_buff *skb);
  39. void (*cmd_received)(struct nfc_hci_dev *hdev, u8 pipe, u8 cmd,
  40. struct sk_buff *skb);
  41. int (*fw_download)(struct nfc_hci_dev *hdev, const char *firmware_name);
  42. int (*discover_se)(struct nfc_hci_dev *dev);
  43. int (*enable_se)(struct nfc_hci_dev *dev, u32 se_idx);
  44. int (*disable_se)(struct nfc_hci_dev *dev, u32 se_idx);
  45. int (*se_io)(struct nfc_hci_dev *dev, u32 se_idx,
  46. u8 *apdu, size_t apdu_length,
  47. se_io_cb_t cb, void *cb_context);
  48. };
  49. /* Pipes */
  50. #define NFC_HCI_DO_NOT_CREATE_PIPE 0x81
  51. #define NFC_HCI_INVALID_PIPE 0x80
  52. #define NFC_HCI_INVALID_GATE 0xFF
  53. #define NFC_HCI_INVALID_HOST 0x80
  54. #define NFC_HCI_LINK_MGMT_PIPE 0x00
  55. #define NFC_HCI_ADMIN_PIPE 0x01
  56. struct nfc_hci_gate {
  57. u8 gate;
  58. u8 pipe;
  59. };
  60. struct nfc_hci_pipe {
  61. u8 gate;
  62. u8 dest_host;
  63. };
  64. #define NFC_HCI_MAX_CUSTOM_GATES 50
  65. /*
  66. * According to specification 102 622 chapter 4.4 Pipes,
  67. * the pipe identifier is 7 bits long.
  68. */
  69. #define NFC_HCI_MAX_PIPES 128
  70. struct nfc_hci_init_data {
  71. u8 gate_count;
  72. struct nfc_hci_gate gates[NFC_HCI_MAX_CUSTOM_GATES];
  73. char session_id[9];
  74. };
  75. typedef int (*xmit) (struct sk_buff *skb, void *cb_data);
  76. #define NFC_HCI_MAX_GATES 256
  77. /*
  78. * These values can be specified by a driver to indicate it requires some
  79. * adaptation of the HCI standard.
  80. *
  81. * NFC_HCI_QUIRK_SHORT_CLEAR - send HCI_ADM_CLEAR_ALL_PIPE cmd with no params
  82. */
  83. enum {
  84. NFC_HCI_QUIRK_SHORT_CLEAR = 0,
  85. };
  86. struct nfc_hci_dev {
  87. struct nfc_dev *ndev;
  88. u32 max_data_link_payload;
  89. bool shutting_down;
  90. struct mutex msg_tx_mutex;
  91. struct list_head msg_tx_queue;
  92. struct work_struct msg_tx_work;
  93. struct timer_list cmd_timer;
  94. struct hci_msg *cmd_pending_msg;
  95. struct sk_buff_head rx_hcp_frags;
  96. struct work_struct msg_rx_work;
  97. struct sk_buff_head msg_rx_queue;
  98. const struct nfc_hci_ops *ops;
  99. struct nfc_llc *llc;
  100. struct nfc_hci_init_data init_data;
  101. void *clientdata;
  102. u8 gate2pipe[NFC_HCI_MAX_GATES];
  103. struct nfc_hci_pipe pipes[NFC_HCI_MAX_PIPES];
  104. u8 sw_romlib;
  105. u8 sw_patch;
  106. u8 sw_flashlib_major;
  107. u8 sw_flashlib_minor;
  108. u8 hw_derivative;
  109. u8 hw_version;
  110. u8 hw_mpw;
  111. u8 hw_software;
  112. u8 hw_bsid;
  113. int async_cb_type;
  114. data_exchange_cb_t async_cb;
  115. void *async_cb_context;
  116. u8 *gb;
  117. size_t gb_len;
  118. unsigned long quirks;
  119. };
  120. /* hci device allocation */
  121. struct nfc_hci_dev *nfc_hci_allocate_device(const struct nfc_hci_ops *ops,
  122. struct nfc_hci_init_data *init_data,
  123. unsigned long quirks,
  124. u32 protocols,
  125. const char *llc_name,
  126. int tx_headroom,
  127. int tx_tailroom,
  128. int max_link_payload);
  129. void nfc_hci_free_device(struct nfc_hci_dev *hdev);
  130. int nfc_hci_register_device(struct nfc_hci_dev *hdev);
  131. void nfc_hci_unregister_device(struct nfc_hci_dev *hdev);
  132. void nfc_hci_set_clientdata(struct nfc_hci_dev *hdev, void *clientdata);
  133. void *nfc_hci_get_clientdata(struct nfc_hci_dev *hdev);
  134. static inline int nfc_hci_set_vendor_cmds(struct nfc_hci_dev *hdev,
  135. const struct nfc_vendor_cmd *cmds,
  136. int n_cmds)
  137. {
  138. return nfc_set_vendor_cmds(hdev->ndev, cmds, n_cmds);
  139. }
  140. void nfc_hci_driver_failure(struct nfc_hci_dev *hdev, int err);
  141. int nfc_hci_result_to_errno(u8 result);
  142. void nfc_hci_reset_pipes(struct nfc_hci_dev *dev);
  143. void nfc_hci_reset_pipes_per_host(struct nfc_hci_dev *hdev, u8 host);
  144. /* Host IDs */
  145. #define NFC_HCI_HOST_CONTROLLER_ID 0x00
  146. #define NFC_HCI_TERMINAL_HOST_ID 0x01
  147. #define NFC_HCI_UICC_HOST_ID 0x02
  148. /* Host Controller Gates and registry indexes */
  149. #define NFC_HCI_ADMIN_GATE 0x00
  150. #define NFC_HCI_ADMIN_SESSION_IDENTITY 0x01
  151. #define NFC_HCI_ADMIN_MAX_PIPE 0x02
  152. #define NFC_HCI_ADMIN_WHITELIST 0x03
  153. #define NFC_HCI_ADMIN_HOST_LIST 0x04
  154. #define NFC_HCI_LOOPBACK_GATE 0x04
  155. #define NFC_HCI_ID_MGMT_GATE 0x05
  156. #define NFC_HCI_ID_MGMT_VERSION_SW 0x01
  157. #define NFC_HCI_ID_MGMT_VERSION_HW 0x03
  158. #define NFC_HCI_ID_MGMT_VENDOR_NAME 0x04
  159. #define NFC_HCI_ID_MGMT_MODEL_ID 0x05
  160. #define NFC_HCI_ID_MGMT_HCI_VERSION 0x02
  161. #define NFC_HCI_ID_MGMT_GATES_LIST 0x06
  162. #define NFC_HCI_LINK_MGMT_GATE 0x06
  163. #define NFC_HCI_LINK_MGMT_REC_ERROR 0x01
  164. #define NFC_HCI_RF_READER_B_GATE 0x11
  165. #define NFC_HCI_RF_READER_B_PUPI 0x03
  166. #define NFC_HCI_RF_READER_B_APPLICATION_DATA 0x04
  167. #define NFC_HCI_RF_READER_B_AFI 0x02
  168. #define NFC_HCI_RF_READER_B_HIGHER_LAYER_RESPONSE 0x01
  169. #define NFC_HCI_RF_READER_B_HIGHER_LAYER_DATA 0x05
  170. #define NFC_HCI_RF_READER_A_GATE 0x13
  171. #define NFC_HCI_RF_READER_A_UID 0x02
  172. #define NFC_HCI_RF_READER_A_ATQA 0x04
  173. #define NFC_HCI_RF_READER_A_APPLICATION_DATA 0x05
  174. #define NFC_HCI_RF_READER_A_SAK 0x03
  175. #define NFC_HCI_RF_READER_A_FWI_SFGT 0x06
  176. #define NFC_HCI_RF_READER_A_DATARATE_MAX 0x01
  177. #define NFC_HCI_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
  178. #define NFC_HCI_TYPE_A_SEL_PROT_MIFARE 0
  179. #define NFC_HCI_TYPE_A_SEL_PROT_ISO14443 1
  180. #define NFC_HCI_TYPE_A_SEL_PROT_DEP 2
  181. #define NFC_HCI_TYPE_A_SEL_PROT_ISO14443_DEP 3
  182. /* Generic events */
  183. #define NFC_HCI_EVT_HCI_END_OF_OPERATION 0x01
  184. #define NFC_HCI_EVT_POST_DATA 0x02
  185. #define NFC_HCI_EVT_HOT_PLUG 0x03
  186. /* Generic commands */
  187. #define NFC_HCI_ANY_SET_PARAMETER 0x01
  188. #define NFC_HCI_ANY_GET_PARAMETER 0x02
  189. #define NFC_HCI_ANY_OPEN_PIPE 0x03
  190. #define NFC_HCI_ANY_CLOSE_PIPE 0x04
  191. /* Reader RF gates events */
  192. #define NFC_HCI_EVT_READER_REQUESTED 0x10
  193. #define NFC_HCI_EVT_END_OPERATION 0x11
  194. /* Reader Application gate events */
  195. #define NFC_HCI_EVT_TARGET_DISCOVERED 0x10
  196. /* receiving messages from lower layer */
  197. void nfc_hci_resp_received(struct nfc_hci_dev *hdev, u8 result,
  198. struct sk_buff *skb);
  199. void nfc_hci_cmd_received(struct nfc_hci_dev *hdev, u8 pipe, u8 cmd,
  200. struct sk_buff *skb);
  201. void nfc_hci_event_received(struct nfc_hci_dev *hdev, u8 pipe, u8 event,
  202. struct sk_buff *skb);
  203. void nfc_hci_recv_frame(struct nfc_hci_dev *hdev, struct sk_buff *skb);
  204. /* connecting to gates and sending hci instructions */
  205. int nfc_hci_connect_gate(struct nfc_hci_dev *hdev, u8 dest_host, u8 dest_gate,
  206. u8 pipe);
  207. int nfc_hci_disconnect_gate(struct nfc_hci_dev *hdev, u8 gate);
  208. int nfc_hci_disconnect_all_gates(struct nfc_hci_dev *hdev);
  209. int nfc_hci_get_param(struct nfc_hci_dev *hdev, u8 gate, u8 idx,
  210. struct sk_buff **skb);
  211. int nfc_hci_set_param(struct nfc_hci_dev *hdev, u8 gate, u8 idx,
  212. const u8 *param, size_t param_len);
  213. int nfc_hci_send_cmd(struct nfc_hci_dev *hdev, u8 gate, u8 cmd,
  214. const u8 *param, size_t param_len, struct sk_buff **skb);
  215. int nfc_hci_send_cmd_async(struct nfc_hci_dev *hdev, u8 gate, u8 cmd,
  216. const u8 *param, size_t param_len,
  217. data_exchange_cb_t cb, void *cb_context);
  218. int nfc_hci_send_event(struct nfc_hci_dev *hdev, u8 gate, u8 event,
  219. const u8 *param, size_t param_len);
  220. int nfc_hci_target_discovered(struct nfc_hci_dev *hdev, u8 gate);
  221. u32 nfc_hci_sak_to_protocol(u8 sak);
  222. #endif /* __NET_HCI_H */