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