nci_core.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * The NFC Controller Interface is the communication protocol between an
  3. * NFC Controller (NFCC) and a Device Host (DH).
  4. *
  5. * Copyright (C) 2011 Texas Instruments, Inc.
  6. * Copyright (C) 2013 Intel Corporation. All rights reserved.
  7. *
  8. * Written by Ilan Elias <ilane@ti.com>
  9. *
  10. * Acknowledgements:
  11. * This file is based on hci_core.h, which was written
  12. * by Maxim Krasnyansky.
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License version 2
  16. * as published by the Free Software Foundation
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. *
  27. */
  28. #ifndef __NCI_CORE_H
  29. #define __NCI_CORE_H
  30. #include <linux/interrupt.h>
  31. #include <linux/skbuff.h>
  32. #include <net/nfc/nfc.h>
  33. #include <net/nfc/nci.h>
  34. /* NCI device flags */
  35. enum nci_flag {
  36. NCI_INIT,
  37. NCI_UP,
  38. NCI_DATA_EXCHANGE,
  39. NCI_DATA_EXCHANGE_TO,
  40. };
  41. /* NCI device states */
  42. enum nci_state {
  43. NCI_IDLE,
  44. NCI_DISCOVERY,
  45. NCI_W4_ALL_DISCOVERIES,
  46. NCI_W4_HOST_SELECT,
  47. NCI_POLL_ACTIVE,
  48. };
  49. /* NCI timeouts */
  50. #define NCI_RESET_TIMEOUT 5000
  51. #define NCI_INIT_TIMEOUT 5000
  52. #define NCI_SET_CONFIG_TIMEOUT 5000
  53. #define NCI_RF_DISC_TIMEOUT 5000
  54. #define NCI_RF_DISC_SELECT_TIMEOUT 5000
  55. #define NCI_RF_DEACTIVATE_TIMEOUT 30000
  56. #define NCI_CMD_TIMEOUT 5000
  57. #define NCI_DATA_TIMEOUT 700
  58. struct nci_dev;
  59. struct nci_ops {
  60. int (*open)(struct nci_dev *ndev);
  61. int (*close)(struct nci_dev *ndev);
  62. int (*send)(struct nci_dev *ndev, struct sk_buff *skb);
  63. };
  64. #define NCI_MAX_SUPPORTED_RF_INTERFACES 4
  65. #define NCI_MAX_DISCOVERED_TARGETS 10
  66. /* NCI Core structures */
  67. struct nci_dev {
  68. struct nfc_dev *nfc_dev;
  69. struct nci_ops *ops;
  70. int tx_headroom;
  71. int tx_tailroom;
  72. atomic_t state;
  73. unsigned long flags;
  74. atomic_t cmd_cnt;
  75. atomic_t credits_cnt;
  76. struct timer_list cmd_timer;
  77. struct timer_list data_timer;
  78. struct workqueue_struct *cmd_wq;
  79. struct work_struct cmd_work;
  80. struct workqueue_struct *rx_wq;
  81. struct work_struct rx_work;
  82. struct workqueue_struct *tx_wq;
  83. struct work_struct tx_work;
  84. struct sk_buff_head cmd_q;
  85. struct sk_buff_head rx_q;
  86. struct sk_buff_head tx_q;
  87. struct mutex req_lock;
  88. struct completion req_completion;
  89. __u32 req_status;
  90. __u32 req_result;
  91. void *driver_data;
  92. __u32 poll_prots;
  93. __u32 target_active_prot;
  94. struct nfc_target targets[NCI_MAX_DISCOVERED_TARGETS];
  95. int n_targets;
  96. /* received during NCI_OP_CORE_RESET_RSP */
  97. __u8 nci_ver;
  98. /* received during NCI_OP_CORE_INIT_RSP */
  99. __u32 nfcc_features;
  100. __u8 num_supported_rf_interfaces;
  101. __u8 supported_rf_interfaces
  102. [NCI_MAX_SUPPORTED_RF_INTERFACES];
  103. __u8 max_logical_connections;
  104. __u16 max_routing_table_size;
  105. __u8 max_ctrl_pkt_payload_len;
  106. __u16 max_size_for_large_params;
  107. __u8 manufact_id;
  108. __u32 manufact_specific_info;
  109. /* received during NCI_OP_RF_INTF_ACTIVATED_NTF */
  110. __u8 max_data_pkt_payload_size;
  111. __u8 initial_num_credits;
  112. /* stored during nci_data_exchange */
  113. data_exchange_cb_t data_exchange_cb;
  114. void *data_exchange_cb_context;
  115. struct sk_buff *rx_data_reassembly;
  116. /* stored during intf_activated_ntf */
  117. __u8 remote_gb[NFC_MAX_GT_LEN];
  118. __u8 remote_gb_len;
  119. };
  120. /* ----- NCI Devices ----- */
  121. struct nci_dev *nci_allocate_device(struct nci_ops *ops,
  122. __u32 supported_protocols,
  123. int tx_headroom,
  124. int tx_tailroom);
  125. void nci_free_device(struct nci_dev *ndev);
  126. int nci_register_device(struct nci_dev *ndev);
  127. void nci_unregister_device(struct nci_dev *ndev);
  128. int nci_recv_frame(struct nci_dev *ndev, struct sk_buff *skb);
  129. static inline struct sk_buff *nci_skb_alloc(struct nci_dev *ndev,
  130. unsigned int len,
  131. gfp_t how)
  132. {
  133. struct sk_buff *skb;
  134. skb = alloc_skb(len + ndev->tx_headroom + ndev->tx_tailroom, how);
  135. if (skb)
  136. skb_reserve(skb, ndev->tx_headroom);
  137. return skb;
  138. }
  139. static inline void nci_set_parent_dev(struct nci_dev *ndev, struct device *dev)
  140. {
  141. nfc_set_parent_dev(ndev->nfc_dev, dev);
  142. }
  143. static inline void nci_set_drvdata(struct nci_dev *ndev, void *data)
  144. {
  145. ndev->driver_data = data;
  146. }
  147. static inline void *nci_get_drvdata(struct nci_dev *ndev)
  148. {
  149. return ndev->driver_data;
  150. }
  151. void nci_rsp_packet(struct nci_dev *ndev, struct sk_buff *skb);
  152. void nci_ntf_packet(struct nci_dev *ndev, struct sk_buff *skb);
  153. void nci_rx_data_packet(struct nci_dev *ndev, struct sk_buff *skb);
  154. int nci_send_cmd(struct nci_dev *ndev, __u16 opcode, __u8 plen, void *payload);
  155. int nci_send_data(struct nci_dev *ndev, __u8 conn_id, struct sk_buff *skb);
  156. void nci_data_exchange_complete(struct nci_dev *ndev, struct sk_buff *skb,
  157. int err);
  158. void nci_clear_target_list(struct nci_dev *ndev);
  159. /* ----- NCI requests ----- */
  160. #define NCI_REQ_DONE 0
  161. #define NCI_REQ_PEND 1
  162. #define NCI_REQ_CANCELED 2
  163. void nci_req_complete(struct nci_dev *ndev, int result);
  164. /* ----- NCI status code ----- */
  165. int nci_to_errno(__u8 code);
  166. /* ----- NCI over SPI acknowledge modes ----- */
  167. #define NCI_SPI_CRC_DISABLED 0x00
  168. #define NCI_SPI_CRC_ENABLED 0x01
  169. /* ----- NCI SPI structures ----- */
  170. struct nci_spi_dev;
  171. struct nci_spi_ops {
  172. int (*open)(struct nci_spi_dev *ndev);
  173. int (*close)(struct nci_spi_dev *ndev);
  174. void (*assert_int)(struct nci_spi_dev *ndev);
  175. void (*deassert_int)(struct nci_spi_dev *ndev);
  176. };
  177. struct nci_spi_dev {
  178. struct nci_dev *nci_dev;
  179. struct spi_device *spi;
  180. struct nci_spi_ops *ops;
  181. unsigned int xfer_udelay; /* microseconds delay between
  182. transactions */
  183. u8 acknowledge_mode;
  184. struct completion req_completion;
  185. u8 req_result;
  186. void *driver_data;
  187. };
  188. /* ----- NCI SPI Devices ----- */
  189. struct nci_spi_dev *nci_spi_allocate_device(struct spi_device *spi,
  190. struct nci_spi_ops *ops,
  191. u32 supported_protocols,
  192. u32 supported_se,
  193. u8 acknowledge_mode,
  194. unsigned int delay);
  195. void nci_spi_free_device(struct nci_spi_dev *ndev);
  196. int nci_spi_register_device(struct nci_spi_dev *ndev);
  197. void nci_spi_unregister_device(struct nci_spi_dev *ndev);
  198. int nci_spi_recv_frame(struct nci_spi_dev *ndev);
  199. static inline void nci_spi_set_drvdata(struct nci_spi_dev *ndev,
  200. void *data)
  201. {
  202. ndev->driver_data = data;
  203. }
  204. static inline void *nci_spi_get_drvdata(struct nci_spi_dev *ndev)
  205. {
  206. return ndev->driver_data;
  207. }
  208. #endif /* __NCI_CORE_H */