Thomas Gleixner | 2025cf9 | 2019-05-29 07:18:02 -0700 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Thierry Escande | 4b10884eb | 2013-09-19 17:55:25 +0200 | [diff] [blame] | 2 | /* |
| 3 | * NFC Digital Protocol stack |
| 4 | * Copyright (c) 2013, Intel Corporation. |
Thierry Escande | 4b10884eb | 2013-09-19 17:55:25 +0200 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #ifndef __DIGITAL_H |
| 8 | #define __DIGITAL_H |
| 9 | |
| 10 | #include <net/nfc/nfc.h> |
| 11 | #include <net/nfc/digital.h> |
| 12 | |
Thierry Escande | 2c66dae | 2013-09-19 17:55:27 +0200 | [diff] [blame] | 13 | #include <linux/crc-ccitt.h> |
Thierry Escande | 8c0695e4 | 2013-09-19 17:55:28 +0200 | [diff] [blame] | 14 | #include <linux/crc-itu-t.h> |
Thierry Escande | 2c66dae | 2013-09-19 17:55:27 +0200 | [diff] [blame] | 15 | |
Samuel Ortiz | c5da0e4 | 2013-09-20 09:05:48 +0200 | [diff] [blame] | 16 | #define PROTOCOL_ERR(req) pr_err("%d: NFC Digital Protocol error: %s\n", \ |
| 17 | __LINE__, req) |
Thierry Escande | 4b10884eb | 2013-09-19 17:55:25 +0200 | [diff] [blame] | 18 | |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 19 | #define DIGITAL_CMD_IN_SEND 0 |
| 20 | #define DIGITAL_CMD_TG_SEND 1 |
| 21 | #define DIGITAL_CMD_TG_LISTEN 2 |
| 22 | #define DIGITAL_CMD_TG_LISTEN_MDAA 3 |
Mark A. Greer | bf30a67 | 2014-07-21 21:24:39 -0700 | [diff] [blame] | 23 | #define DIGITAL_CMD_TG_LISTEN_MD 4 |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 24 | |
| 25 | #define DIGITAL_MAX_HEADER_LEN 7 |
| 26 | #define DIGITAL_CRC_LEN 2 |
| 27 | |
Thierry Escande | 7d0911c | 2013-09-19 17:55:29 +0200 | [diff] [blame] | 28 | #define DIGITAL_SENSF_NFCID2_NFC_DEP_B1 0x01 |
| 29 | #define DIGITAL_SENSF_NFCID2_NFC_DEP_B2 0xFE |
| 30 | |
| 31 | #define DIGITAL_SENS_RES_NFC_DEP 0x0100 |
| 32 | #define DIGITAL_SEL_RES_NFC_DEP 0x40 |
| 33 | #define DIGITAL_SENSF_FELICA_SC 0xFFFF |
| 34 | |
Thierry Escande | 2c66dae | 2013-09-19 17:55:27 +0200 | [diff] [blame] | 35 | #define DIGITAL_DRV_CAPS_IN_CRC(ddev) \ |
| 36 | ((ddev)->driver_capabilities & NFC_DIGITAL_DRV_CAPS_IN_CRC) |
| 37 | #define DIGITAL_DRV_CAPS_TG_CRC(ddev) \ |
| 38 | ((ddev)->driver_capabilities & NFC_DIGITAL_DRV_CAPS_TG_CRC) |
| 39 | |
| 40 | struct digital_data_exch { |
| 41 | data_exchange_cb_t cb; |
| 42 | void *cb_context; |
| 43 | }; |
| 44 | |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 45 | struct sk_buff *digital_skb_alloc(struct nfc_digital_dev *ddev, |
| 46 | unsigned int len); |
| 47 | |
| 48 | int digital_send_cmd(struct nfc_digital_dev *ddev, u8 cmd_type, |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 49 | struct sk_buff *skb, struct digital_tg_mdaa_params *params, |
| 50 | u16 timeout, nfc_digital_cmd_complete_t cmd_cb, |
| 51 | void *cb_context); |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 52 | |
| 53 | int digital_in_configure_hw(struct nfc_digital_dev *ddev, int type, int param); |
| 54 | static inline int digital_in_send_cmd(struct nfc_digital_dev *ddev, |
| 55 | struct sk_buff *skb, u16 timeout, |
| 56 | nfc_digital_cmd_complete_t cmd_cb, |
| 57 | void *cb_context) |
| 58 | { |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 59 | return digital_send_cmd(ddev, DIGITAL_CMD_IN_SEND, skb, NULL, timeout, |
| 60 | cmd_cb, cb_context); |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | void digital_poll_next_tech(struct nfc_digital_dev *ddev); |
| 64 | |
| 65 | int digital_in_send_sens_req(struct nfc_digital_dev *ddev, u8 rf_tech); |
Mark A. Greer | 2473460 | 2014-03-31 17:36:38 -0700 | [diff] [blame] | 66 | int digital_in_send_sensb_req(struct nfc_digital_dev *ddev, u8 rf_tech); |
Thierry Escande | 8c0695e4 | 2013-09-19 17:55:28 +0200 | [diff] [blame] | 67 | int digital_in_send_sensf_req(struct nfc_digital_dev *ddev, u8 rf_tech); |
Mark A. Greer | a381d48 | 2014-01-21 16:23:59 -0700 | [diff] [blame] | 68 | int digital_in_send_iso15693_inv_req(struct nfc_digital_dev *ddev, u8 rf_tech); |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 69 | |
Thierry Escande | c813007 | 2014-01-27 00:31:32 +0100 | [diff] [blame] | 70 | int digital_in_iso_dep_pull_sod(struct nfc_digital_dev *ddev, |
| 71 | struct sk_buff *skb); |
| 72 | int digital_in_iso_dep_push_sod(struct nfc_digital_dev *ddev, |
| 73 | struct sk_buff *skb); |
| 74 | |
Thierry Escande | 2c66dae | 2013-09-19 17:55:27 +0200 | [diff] [blame] | 75 | int digital_target_found(struct nfc_digital_dev *ddev, |
| 76 | struct nfc_target *target, u8 protocol); |
| 77 | |
| 78 | int digital_in_recv_mifare_res(struct sk_buff *resp); |
| 79 | |
Thierry Escande | 7d0911c | 2013-09-19 17:55:29 +0200 | [diff] [blame] | 80 | int digital_in_send_atr_req(struct nfc_digital_dev *ddev, |
| 81 | struct nfc_target *target, __u8 comm_mode, __u8 *gb, |
| 82 | size_t gb_len); |
| 83 | int digital_in_send_dep_req(struct nfc_digital_dev *ddev, |
| 84 | struct nfc_target *target, struct sk_buff *skb, |
| 85 | struct digital_data_exch *data_exch); |
| 86 | |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 87 | int digital_tg_configure_hw(struct nfc_digital_dev *ddev, int type, int param); |
| 88 | static inline int digital_tg_send_cmd(struct nfc_digital_dev *ddev, |
| 89 | struct sk_buff *skb, u16 timeout, |
| 90 | nfc_digital_cmd_complete_t cmd_cb, void *cb_context) |
| 91 | { |
| 92 | return digital_send_cmd(ddev, DIGITAL_CMD_TG_SEND, skb, NULL, timeout, |
| 93 | cmd_cb, cb_context); |
| 94 | } |
| 95 | |
| 96 | void digital_tg_recv_sens_req(struct nfc_digital_dev *ddev, void *arg, |
| 97 | struct sk_buff *resp); |
| 98 | |
| 99 | void digital_tg_recv_sensf_req(struct nfc_digital_dev *ddev, void *arg, |
| 100 | struct sk_buff *resp); |
| 101 | |
| 102 | static inline int digital_tg_listen(struct nfc_digital_dev *ddev, u16 timeout, |
| 103 | nfc_digital_cmd_complete_t cb, void *arg) |
| 104 | { |
| 105 | return digital_send_cmd(ddev, DIGITAL_CMD_TG_LISTEN, NULL, NULL, |
| 106 | timeout, cb, arg); |
| 107 | } |
| 108 | |
| 109 | void digital_tg_recv_atr_req(struct nfc_digital_dev *ddev, void *arg, |
| 110 | struct sk_buff *resp); |
| 111 | |
| 112 | int digital_tg_send_dep_res(struct nfc_digital_dev *ddev, struct sk_buff *skb); |
| 113 | |
| 114 | int digital_tg_listen_nfca(struct nfc_digital_dev *ddev, u8 rf_tech); |
| 115 | int digital_tg_listen_nfcf(struct nfc_digital_dev *ddev, u8 rf_tech); |
Mark A. Greer | bf30a67 | 2014-07-21 21:24:39 -0700 | [diff] [blame] | 116 | void digital_tg_recv_md_req(struct nfc_digital_dev *ddev, void *arg, |
| 117 | struct sk_buff *resp); |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 118 | |
Thierry Escande | 2c66dae | 2013-09-19 17:55:27 +0200 | [diff] [blame] | 119 | typedef u16 (*crc_func_t)(u16, const u8 *, size_t); |
| 120 | |
| 121 | #define CRC_A_INIT 0x6363 |
| 122 | #define CRC_B_INIT 0xFFFF |
Thierry Escande | 8c0695e4 | 2013-09-19 17:55:28 +0200 | [diff] [blame] | 123 | #define CRC_F_INIT 0x0000 |
Thierry Escande | 2c66dae | 2013-09-19 17:55:27 +0200 | [diff] [blame] | 124 | |
| 125 | void digital_skb_add_crc(struct sk_buff *skb, crc_func_t crc_func, u16 init, |
| 126 | u8 bitwise_inv, u8 msb_first); |
| 127 | |
| 128 | static inline void digital_skb_add_crc_a(struct sk_buff *skb) |
| 129 | { |
| 130 | digital_skb_add_crc(skb, crc_ccitt, CRC_A_INIT, 0, 0); |
| 131 | } |
| 132 | |
| 133 | static inline void digital_skb_add_crc_b(struct sk_buff *skb) |
| 134 | { |
| 135 | digital_skb_add_crc(skb, crc_ccitt, CRC_B_INIT, 1, 0); |
| 136 | } |
| 137 | |
Thierry Escande | 8c0695e4 | 2013-09-19 17:55:28 +0200 | [diff] [blame] | 138 | static inline void digital_skb_add_crc_f(struct sk_buff *skb) |
| 139 | { |
| 140 | digital_skb_add_crc(skb, crc_itu_t, CRC_F_INIT, 0, 1); |
| 141 | } |
| 142 | |
Thierry Escande | 2c66dae | 2013-09-19 17:55:27 +0200 | [diff] [blame] | 143 | static inline void digital_skb_add_crc_none(struct sk_buff *skb) |
| 144 | { |
| 145 | return; |
| 146 | } |
| 147 | |
| 148 | int digital_skb_check_crc(struct sk_buff *skb, crc_func_t crc_func, |
| 149 | u16 crc_init, u8 bitwise_inv, u8 msb_first); |
| 150 | |
| 151 | static inline int digital_skb_check_crc_a(struct sk_buff *skb) |
| 152 | { |
| 153 | return digital_skb_check_crc(skb, crc_ccitt, CRC_A_INIT, 0, 0); |
| 154 | } |
| 155 | |
| 156 | static inline int digital_skb_check_crc_b(struct sk_buff *skb) |
| 157 | { |
| 158 | return digital_skb_check_crc(skb, crc_ccitt, CRC_B_INIT, 1, 0); |
| 159 | } |
| 160 | |
Thierry Escande | 8c0695e4 | 2013-09-19 17:55:28 +0200 | [diff] [blame] | 161 | static inline int digital_skb_check_crc_f(struct sk_buff *skb) |
| 162 | { |
| 163 | return digital_skb_check_crc(skb, crc_itu_t, CRC_F_INIT, 0, 1); |
| 164 | } |
| 165 | |
Thierry Escande | 2c66dae | 2013-09-19 17:55:27 +0200 | [diff] [blame] | 166 | static inline int digital_skb_check_crc_none(struct sk_buff *skb) |
| 167 | { |
| 168 | return 0; |
| 169 | } |
| 170 | |
Thierry Escande | 4b10884eb | 2013-09-19 17:55:25 +0200 | [diff] [blame] | 171 | #endif /* __DIGITAL_H */ |