digital.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * NFC Digital Protocol stack
  3. * Copyright (c) 2013, Intel Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. */
  15. #ifndef __DIGITAL_H
  16. #define __DIGITAL_H
  17. #include <net/nfc/nfc.h>
  18. #include <net/nfc/digital.h>
  19. #include <linux/crc-ccitt.h>
  20. #include <linux/crc-itu-t.h>
  21. #define PR_DBG(fmt, ...) pr_debug("%s: " fmt "\n", __func__, ##__VA_ARGS__)
  22. #define PR_ERR(fmt, ...) pr_err("%s: " fmt "\n", __func__, ##__VA_ARGS__)
  23. #define PROTOCOL_ERR(req) pr_err("%s:%d: NFC Digital Protocol error: %s\n", \
  24. __func__, __LINE__, req)
  25. #define DIGITAL_CMD_IN_SEND 0
  26. #define DIGITAL_CMD_TG_SEND 1
  27. #define DIGITAL_CMD_TG_LISTEN 2
  28. #define DIGITAL_CMD_TG_LISTEN_MDAA 3
  29. #define DIGITAL_MAX_HEADER_LEN 7
  30. #define DIGITAL_CRC_LEN 2
  31. #define DIGITAL_SENSF_NFCID2_NFC_DEP_B1 0x01
  32. #define DIGITAL_SENSF_NFCID2_NFC_DEP_B2 0xFE
  33. #define DIGITAL_SENS_RES_NFC_DEP 0x0100
  34. #define DIGITAL_SEL_RES_NFC_DEP 0x40
  35. #define DIGITAL_SENSF_FELICA_SC 0xFFFF
  36. #define DIGITAL_DRV_CAPS_IN_CRC(ddev) \
  37. ((ddev)->driver_capabilities & NFC_DIGITAL_DRV_CAPS_IN_CRC)
  38. #define DIGITAL_DRV_CAPS_TG_CRC(ddev) \
  39. ((ddev)->driver_capabilities & NFC_DIGITAL_DRV_CAPS_TG_CRC)
  40. struct digital_data_exch {
  41. data_exchange_cb_t cb;
  42. void *cb_context;
  43. };
  44. struct sk_buff *digital_skb_alloc(struct nfc_digital_dev *ddev,
  45. unsigned int len);
  46. int digital_send_cmd(struct nfc_digital_dev *ddev, u8 cmd_type,
  47. struct sk_buff *skb, struct digital_tg_mdaa_params *params,
  48. u16 timeout, nfc_digital_cmd_complete_t cmd_cb,
  49. void *cb_context);
  50. int digital_in_configure_hw(struct nfc_digital_dev *ddev, int type, int param);
  51. static inline int digital_in_send_cmd(struct nfc_digital_dev *ddev,
  52. struct sk_buff *skb, u16 timeout,
  53. nfc_digital_cmd_complete_t cmd_cb,
  54. void *cb_context)
  55. {
  56. return digital_send_cmd(ddev, DIGITAL_CMD_IN_SEND, skb, NULL, timeout,
  57. cmd_cb, cb_context);
  58. }
  59. void digital_poll_next_tech(struct nfc_digital_dev *ddev);
  60. int digital_in_send_sens_req(struct nfc_digital_dev *ddev, u8 rf_tech);
  61. int digital_in_send_sensf_req(struct nfc_digital_dev *ddev, u8 rf_tech);
  62. int digital_target_found(struct nfc_digital_dev *ddev,
  63. struct nfc_target *target, u8 protocol);
  64. int digital_in_recv_mifare_res(struct sk_buff *resp);
  65. int digital_in_send_atr_req(struct nfc_digital_dev *ddev,
  66. struct nfc_target *target, __u8 comm_mode, __u8 *gb,
  67. size_t gb_len);
  68. int digital_in_send_dep_req(struct nfc_digital_dev *ddev,
  69. struct nfc_target *target, struct sk_buff *skb,
  70. struct digital_data_exch *data_exch);
  71. int digital_tg_configure_hw(struct nfc_digital_dev *ddev, int type, int param);
  72. static inline int digital_tg_send_cmd(struct nfc_digital_dev *ddev,
  73. struct sk_buff *skb, u16 timeout,
  74. nfc_digital_cmd_complete_t cmd_cb, void *cb_context)
  75. {
  76. return digital_send_cmd(ddev, DIGITAL_CMD_TG_SEND, skb, NULL, timeout,
  77. cmd_cb, cb_context);
  78. }
  79. void digital_tg_recv_sens_req(struct nfc_digital_dev *ddev, void *arg,
  80. struct sk_buff *resp);
  81. void digital_tg_recv_sensf_req(struct nfc_digital_dev *ddev, void *arg,
  82. struct sk_buff *resp);
  83. static inline int digital_tg_listen(struct nfc_digital_dev *ddev, u16 timeout,
  84. nfc_digital_cmd_complete_t cb, void *arg)
  85. {
  86. return digital_send_cmd(ddev, DIGITAL_CMD_TG_LISTEN, NULL, NULL,
  87. timeout, cb, arg);
  88. }
  89. void digital_tg_recv_atr_req(struct nfc_digital_dev *ddev, void *arg,
  90. struct sk_buff *resp);
  91. int digital_tg_send_dep_res(struct nfc_digital_dev *ddev, struct sk_buff *skb);
  92. int digital_tg_listen_nfca(struct nfc_digital_dev *ddev, u8 rf_tech);
  93. int digital_tg_listen_nfcf(struct nfc_digital_dev *ddev, u8 rf_tech);
  94. typedef u16 (*crc_func_t)(u16, const u8 *, size_t);
  95. #define CRC_A_INIT 0x6363
  96. #define CRC_B_INIT 0xFFFF
  97. #define CRC_F_INIT 0x0000
  98. void digital_skb_add_crc(struct sk_buff *skb, crc_func_t crc_func, u16 init,
  99. u8 bitwise_inv, u8 msb_first);
  100. static inline void digital_skb_add_crc_a(struct sk_buff *skb)
  101. {
  102. digital_skb_add_crc(skb, crc_ccitt, CRC_A_INIT, 0, 0);
  103. }
  104. static inline void digital_skb_add_crc_b(struct sk_buff *skb)
  105. {
  106. digital_skb_add_crc(skb, crc_ccitt, CRC_B_INIT, 1, 0);
  107. }
  108. static inline void digital_skb_add_crc_f(struct sk_buff *skb)
  109. {
  110. digital_skb_add_crc(skb, crc_itu_t, CRC_F_INIT, 0, 1);
  111. }
  112. static inline void digital_skb_add_crc_none(struct sk_buff *skb)
  113. {
  114. return;
  115. }
  116. int digital_skb_check_crc(struct sk_buff *skb, crc_func_t crc_func,
  117. u16 crc_init, u8 bitwise_inv, u8 msb_first);
  118. static inline int digital_skb_check_crc_a(struct sk_buff *skb)
  119. {
  120. return digital_skb_check_crc(skb, crc_ccitt, CRC_A_INIT, 0, 0);
  121. }
  122. static inline int digital_skb_check_crc_b(struct sk_buff *skb)
  123. {
  124. return digital_skb_check_crc(skb, crc_ccitt, CRC_B_INIT, 1, 0);
  125. }
  126. static inline int digital_skb_check_crc_f(struct sk_buff *skb)
  127. {
  128. return digital_skb_check_crc(skb, crc_itu_t, CRC_F_INIT, 0, 1);
  129. }
  130. static inline int digital_skb_check_crc_none(struct sk_buff *skb)
  131. {
  132. return 0;
  133. }
  134. #endif /* __DIGITAL_H */