pn533.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * Driver for NXP PN533 NFC Chip
  3. *
  4. * Copyright (C) 2011 Instituto Nokia de Tecnologia
  5. * Copyright (C) 2012-2013 Tieto Poland
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #define PN533_DEVICE_STD 0x1
  21. #define PN533_DEVICE_PASORI 0x2
  22. #define PN533_DEVICE_ACR122U 0x3
  23. #define PN533_DEVICE_PN532 0x4
  24. #define PN533_ALL_PROTOCOLS (NFC_PROTO_JEWEL_MASK | NFC_PROTO_MIFARE_MASK |\
  25. NFC_PROTO_FELICA_MASK | NFC_PROTO_ISO14443_MASK |\
  26. NFC_PROTO_NFC_DEP_MASK |\
  27. NFC_PROTO_ISO14443_B_MASK)
  28. #define PN533_NO_TYPE_B_PROTOCOLS (NFC_PROTO_JEWEL_MASK | \
  29. NFC_PROTO_MIFARE_MASK | \
  30. NFC_PROTO_FELICA_MASK | \
  31. NFC_PROTO_ISO14443_MASK | \
  32. NFC_PROTO_NFC_DEP_MASK)
  33. /* Standard pn533 frame definitions (standard and extended)*/
  34. #define PN533_STD_FRAME_HEADER_LEN (sizeof(struct pn533_std_frame) \
  35. + 2) /* data[0] TFI, data[1] CC */
  36. #define PN533_STD_FRAME_TAIL_LEN 2 /* data[len] DCS, data[len + 1] postamble*/
  37. #define PN533_EXT_FRAME_HEADER_LEN (sizeof(struct pn533_ext_frame) \
  38. + 2) /* data[0] TFI, data[1] CC */
  39. #define PN533_CMD_DATAEXCH_HEAD_LEN 1
  40. #define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
  41. #define PN533_CMD_DATAFRAME_MAXLEN 240 /* max data length (send) */
  42. /*
  43. * Max extended frame payload len, excluding TFI and CC
  44. * which are already in PN533_FRAME_HEADER_LEN.
  45. */
  46. #define PN533_STD_FRAME_MAX_PAYLOAD_LEN 263
  47. /* Preamble (1), SoPC (2), ACK Code (2), Postamble (1) */
  48. #define PN533_STD_FRAME_ACK_SIZE 6
  49. #define PN533_STD_FRAME_CHECKSUM(f) (f->data[f->datalen])
  50. #define PN533_STD_FRAME_POSTAMBLE(f) (f->data[f->datalen + 1])
  51. /* Half start code (3), LEN (4) should be 0xffff for extended frame */
  52. #define PN533_STD_IS_EXTENDED(hdr) ((hdr)->datalen == 0xFF \
  53. && (hdr)->datalen_checksum == 0xFF)
  54. #define PN533_EXT_FRAME_CHECKSUM(f) (f->data[be16_to_cpu(f->datalen)])
  55. /* start of frame */
  56. #define PN533_STD_FRAME_SOF 0x00FF
  57. /* standard frame identifier: in/out/error */
  58. #define PN533_STD_FRAME_IDENTIFIER(f) (f->data[0]) /* TFI */
  59. #define PN533_STD_FRAME_DIR_OUT 0xD4
  60. #define PN533_STD_FRAME_DIR_IN 0xD5
  61. /* PN533 Commands */
  62. #define PN533_FRAME_CMD(f) (f->data[1])
  63. #define PN533_CMD_GET_FIRMWARE_VERSION 0x02
  64. #define PN533_CMD_SAM_CONFIGURATION 0x14
  65. #define PN533_CMD_RF_CONFIGURATION 0x32
  66. #define PN533_CMD_IN_DATA_EXCHANGE 0x40
  67. #define PN533_CMD_IN_COMM_THRU 0x42
  68. #define PN533_CMD_IN_LIST_PASSIVE_TARGET 0x4A
  69. #define PN533_CMD_IN_ATR 0x50
  70. #define PN533_CMD_IN_RELEASE 0x52
  71. #define PN533_CMD_IN_JUMP_FOR_DEP 0x56
  72. #define PN533_CMD_TG_INIT_AS_TARGET 0x8c
  73. #define PN533_CMD_TG_GET_DATA 0x86
  74. #define PN533_CMD_TG_SET_DATA 0x8e
  75. #define PN533_CMD_TG_SET_META_DATA 0x94
  76. #define PN533_CMD_UNDEF 0xff
  77. #define PN533_CMD_RESPONSE(cmd) (cmd + 1)
  78. /* PN533 Return codes */
  79. #define PN533_CMD_RET_MASK 0x3F
  80. #define PN533_CMD_MI_MASK 0x40
  81. #define PN533_CMD_RET_SUCCESS 0x00
  82. enum pn533_protocol_type {
  83. PN533_PROTO_REQ_ACK_RESP = 0,
  84. PN533_PROTO_REQ_RESP
  85. };
  86. /* Poll modulations */
  87. enum {
  88. PN533_POLL_MOD_106KBPS_A,
  89. PN533_POLL_MOD_212KBPS_FELICA,
  90. PN533_POLL_MOD_424KBPS_FELICA,
  91. PN533_POLL_MOD_106KBPS_JEWEL,
  92. PN533_POLL_MOD_847KBPS_B,
  93. PN533_LISTEN_MOD,
  94. __PN533_POLL_MOD_AFTER_LAST,
  95. };
  96. #define PN533_POLL_MOD_MAX (__PN533_POLL_MOD_AFTER_LAST - 1)
  97. struct pn533_std_frame {
  98. u8 preamble;
  99. __be16 start_frame;
  100. u8 datalen;
  101. u8 datalen_checksum;
  102. u8 data[];
  103. } __packed;
  104. struct pn533_ext_frame { /* Extended Information frame */
  105. u8 preamble;
  106. __be16 start_frame;
  107. __be16 eif_flag; /* fixed to 0xFFFF */
  108. __be16 datalen;
  109. u8 datalen_checksum;
  110. u8 data[];
  111. } __packed;
  112. struct pn533 {
  113. struct nfc_dev *nfc_dev;
  114. u32 device_type;
  115. enum pn533_protocol_type protocol_type;
  116. struct sk_buff_head resp_q;
  117. struct sk_buff_head fragment_skb;
  118. struct workqueue_struct *wq;
  119. struct work_struct cmd_work;
  120. struct work_struct cmd_complete_work;
  121. struct delayed_work poll_work;
  122. struct work_struct mi_rx_work;
  123. struct work_struct mi_tx_work;
  124. struct work_struct mi_tm_rx_work;
  125. struct work_struct mi_tm_tx_work;
  126. struct work_struct tg_work;
  127. struct work_struct rf_work;
  128. struct list_head cmd_queue;
  129. struct pn533_cmd *cmd;
  130. u8 cmd_pending;
  131. struct mutex cmd_lock; /* protects cmd queue */
  132. void *cmd_complete_mi_arg;
  133. void *cmd_complete_dep_arg;
  134. struct pn533_poll_modulations *poll_mod_active[PN533_POLL_MOD_MAX + 1];
  135. u8 poll_mod_count;
  136. u8 poll_mod_curr;
  137. u8 poll_dep;
  138. u32 poll_protocols;
  139. u32 listen_protocols;
  140. struct timer_list listen_timer;
  141. int cancel_listen;
  142. u8 *gb;
  143. size_t gb_len;
  144. u8 tgt_available_prots;
  145. u8 tgt_active_prot;
  146. u8 tgt_mode;
  147. struct pn533_frame_ops *ops;
  148. struct device *dev;
  149. void *phy;
  150. struct pn533_phy_ops *phy_ops;
  151. };
  152. typedef int (*pn533_send_async_complete_t) (struct pn533 *dev, void *arg,
  153. struct sk_buff *resp);
  154. struct pn533_cmd {
  155. struct list_head queue;
  156. u8 code;
  157. int status;
  158. struct sk_buff *req;
  159. struct sk_buff *resp;
  160. pn533_send_async_complete_t complete_cb;
  161. void *complete_cb_context;
  162. };
  163. struct pn533_frame_ops {
  164. void (*tx_frame_init)(void *frame, u8 cmd_code);
  165. void (*tx_frame_finish)(void *frame);
  166. void (*tx_update_payload_len)(void *frame, int len);
  167. int tx_header_len;
  168. int tx_tail_len;
  169. bool (*rx_is_frame_valid)(void *frame, struct pn533 *dev);
  170. bool (*rx_frame_is_ack)(void *frame);
  171. int (*rx_frame_size)(void *frame);
  172. int rx_header_len;
  173. int rx_tail_len;
  174. int max_payload_len;
  175. u8 (*get_cmd_code)(void *frame);
  176. };
  177. struct pn533_phy_ops {
  178. int (*send_frame)(struct pn533 *priv,
  179. struct sk_buff *out);
  180. int (*send_ack)(struct pn533 *dev, gfp_t flags);
  181. void (*abort_cmd)(struct pn533 *priv, gfp_t flags);
  182. };
  183. struct pn533 *pn533_register_device(u32 device_type,
  184. u32 protocols,
  185. enum pn533_protocol_type protocol_type,
  186. void *phy,
  187. struct pn533_phy_ops *phy_ops,
  188. struct pn533_frame_ops *fops,
  189. struct device *dev,
  190. struct device *parent);
  191. void pn533_unregister_device(struct pn533 *priv);
  192. void pn533_recv_frame(struct pn533 *dev, struct sk_buff *skb, int status);
  193. bool pn533_rx_frame_is_cmd_response(struct pn533 *dev, void *frame);
  194. bool pn533_rx_frame_is_ack(void *_frame);