Thomas Gleixner | 1ccea77 | 2019-05-19 15:51:43 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2 | /* |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 3 | * Driver for NXP PN533 NFC Chip - core functions |
| 4 | * |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 5 | * Copyright (C) 2011 Instituto Nokia de Tecnologia |
Waldemar Rymarkiewicz | e70b96e | 2013-04-03 08:02:01 +0200 | [diff] [blame] | 6 | * Copyright (C) 2012-2013 Tieto Poland |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/device.h> |
| 10 | #include <linux/kernel.h> |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/slab.h> |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 13 | #include <linux/nfc.h> |
| 14 | #include <linux/netdevice.h> |
Ilan Elias | 55eb94f | 2011-09-18 11:19:34 +0300 | [diff] [blame] | 15 | #include <net/nfc/nfc.h> |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 16 | #include "pn533.h" |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 17 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 18 | #define VERSION "0.3" |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 19 | |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 20 | /* How much time we spend listening for initiators */ |
| 21 | #define PN533_LISTEN_TIME 2 |
Samuel Ortiz | 46f793b | 2013-07-03 14:50:36 +0200 | [diff] [blame] | 22 | /* Delay between each poll frame (ms) */ |
| 23 | #define PN533_POLL_INTERVAL 10 |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 24 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 25 | /* structs for pn533 commands */ |
| 26 | |
| 27 | /* PN533_CMD_GET_FIRMWARE_VERSION */ |
| 28 | struct pn533_fw_version { |
| 29 | u8 ic; |
| 30 | u8 ver; |
| 31 | u8 rev; |
| 32 | u8 support; |
| 33 | }; |
| 34 | |
| 35 | /* PN533_CMD_RF_CONFIGURATION */ |
Samuel Ortiz | 60d9edd5 | 2013-04-11 11:45:41 +0200 | [diff] [blame] | 36 | #define PN533_CFGITEM_RF_FIELD 0x01 |
| 37 | #define PN533_CFGITEM_TIMING 0x02 |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 38 | #define PN533_CFGITEM_MAX_RETRIES 0x05 |
Samuel Ortiz | 60d9edd5 | 2013-04-11 11:45:41 +0200 | [diff] [blame] | 39 | #define PN533_CFGITEM_PASORI 0x82 |
| 40 | |
Samuel Ortiz | 3a8eab3 | 2013-07-01 17:26:58 +0200 | [diff] [blame] | 41 | #define PN533_CFGITEM_RF_FIELD_AUTO_RFCA 0x2 |
| 42 | #define PN533_CFGITEM_RF_FIELD_ON 0x1 |
| 43 | #define PN533_CFGITEM_RF_FIELD_OFF 0x0 |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 44 | |
Samuel Ortiz | 34a85bf | 2012-05-29 21:34:08 +0200 | [diff] [blame] | 45 | #define PN533_CONFIG_TIMING_102 0xb |
| 46 | #define PN533_CONFIG_TIMING_204 0xc |
| 47 | #define PN533_CONFIG_TIMING_409 0xd |
| 48 | #define PN533_CONFIG_TIMING_819 0xe |
| 49 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 50 | #define PN533_CONFIG_MAX_RETRIES_NO_RETRY 0x00 |
| 51 | #define PN533_CONFIG_MAX_RETRIES_ENDLESS 0xFF |
| 52 | |
| 53 | struct pn533_config_max_retries { |
| 54 | u8 mx_rty_atr; |
| 55 | u8 mx_rty_psl; |
| 56 | u8 mx_rty_passive_act; |
| 57 | } __packed; |
| 58 | |
Samuel Ortiz | 34a85bf | 2012-05-29 21:34:08 +0200 | [diff] [blame] | 59 | struct pn533_config_timing { |
| 60 | u8 rfu; |
| 61 | u8 atr_res_timeout; |
| 62 | u8 dep_timeout; |
| 63 | } __packed; |
| 64 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 65 | /* PN533_CMD_IN_LIST_PASSIVE_TARGET */ |
| 66 | |
| 67 | /* felica commands opcode */ |
| 68 | #define PN533_FELICA_OPC_SENSF_REQ 0 |
| 69 | #define PN533_FELICA_OPC_SENSF_RES 1 |
| 70 | /* felica SENSF_REQ parameters */ |
| 71 | #define PN533_FELICA_SENSF_SC_ALL 0xFFFF |
| 72 | #define PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE 0 |
| 73 | #define PN533_FELICA_SENSF_RC_SYSTEM_CODE 1 |
| 74 | #define PN533_FELICA_SENSF_RC_ADVANCED_PROTOCOL 2 |
| 75 | |
| 76 | /* type B initiator_data values */ |
| 77 | #define PN533_TYPE_B_AFI_ALL_FAMILIES 0 |
| 78 | #define PN533_TYPE_B_POLL_METHOD_TIMESLOT 0 |
| 79 | #define PN533_TYPE_B_POLL_METHOD_PROBABILISTIC 1 |
| 80 | |
| 81 | union pn533_cmd_poll_initdata { |
| 82 | struct { |
| 83 | u8 afi; |
| 84 | u8 polling_method; |
| 85 | } __packed type_b; |
| 86 | struct { |
| 87 | u8 opcode; |
| 88 | __be16 sc; |
| 89 | u8 rc; |
| 90 | u8 tsn; |
| 91 | } __packed felica; |
| 92 | }; |
| 93 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 94 | struct pn533_poll_modulations { |
| 95 | struct { |
| 96 | u8 maxtg; |
| 97 | u8 brty; |
| 98 | union pn533_cmd_poll_initdata initiator_data; |
| 99 | } __packed data; |
| 100 | u8 len; |
| 101 | }; |
| 102 | |
Waldemar Rymarkiewicz | ef3d56e | 2013-01-16 15:51:51 +0100 | [diff] [blame] | 103 | static const struct pn533_poll_modulations poll_mod[] = { |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 104 | [PN533_POLL_MOD_106KBPS_A] = { |
| 105 | .data = { |
| 106 | .maxtg = 1, |
| 107 | .brty = 0, |
| 108 | }, |
| 109 | .len = 2, |
| 110 | }, |
| 111 | [PN533_POLL_MOD_212KBPS_FELICA] = { |
| 112 | .data = { |
| 113 | .maxtg = 1, |
| 114 | .brty = 1, |
| 115 | .initiator_data.felica = { |
| 116 | .opcode = PN533_FELICA_OPC_SENSF_REQ, |
| 117 | .sc = PN533_FELICA_SENSF_SC_ALL, |
Samuel Ortiz | a94e10f | 2013-06-28 15:43:19 +0200 | [diff] [blame] | 118 | .rc = PN533_FELICA_SENSF_RC_SYSTEM_CODE, |
Samuel Ortiz | 31c4446 | 2013-05-27 14:59:40 +0200 | [diff] [blame] | 119 | .tsn = 0x03, |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 120 | }, |
| 121 | }, |
| 122 | .len = 7, |
| 123 | }, |
| 124 | [PN533_POLL_MOD_424KBPS_FELICA] = { |
| 125 | .data = { |
| 126 | .maxtg = 1, |
| 127 | .brty = 2, |
| 128 | .initiator_data.felica = { |
| 129 | .opcode = PN533_FELICA_OPC_SENSF_REQ, |
| 130 | .sc = PN533_FELICA_SENSF_SC_ALL, |
Samuel Ortiz | a94e10f | 2013-06-28 15:43:19 +0200 | [diff] [blame] | 131 | .rc = PN533_FELICA_SENSF_RC_SYSTEM_CODE, |
Samuel Ortiz | 31c4446 | 2013-05-27 14:59:40 +0200 | [diff] [blame] | 132 | .tsn = 0x03, |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 133 | }, |
| 134 | }, |
| 135 | .len = 7, |
| 136 | }, |
| 137 | [PN533_POLL_MOD_106KBPS_JEWEL] = { |
| 138 | .data = { |
| 139 | .maxtg = 1, |
| 140 | .brty = 4, |
| 141 | }, |
| 142 | .len = 2, |
| 143 | }, |
| 144 | [PN533_POLL_MOD_847KBPS_B] = { |
| 145 | .data = { |
| 146 | .maxtg = 1, |
| 147 | .brty = 8, |
| 148 | .initiator_data.type_b = { |
| 149 | .afi = PN533_TYPE_B_AFI_ALL_FAMILIES, |
| 150 | .polling_method = |
| 151 | PN533_TYPE_B_POLL_METHOD_TIMESLOT, |
| 152 | }, |
| 153 | }, |
| 154 | .len = 3, |
| 155 | }, |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 156 | [PN533_LISTEN_MOD] = { |
| 157 | .len = 0, |
| 158 | }, |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 159 | }; |
| 160 | |
| 161 | /* PN533_CMD_IN_ATR */ |
| 162 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 163 | struct pn533_cmd_activate_response { |
| 164 | u8 status; |
| 165 | u8 nfcid3t[10]; |
| 166 | u8 didt; |
| 167 | u8 bst; |
| 168 | u8 brt; |
| 169 | u8 to; |
| 170 | u8 ppt; |
| 171 | /* optional */ |
| 172 | u8 gt[]; |
| 173 | } __packed; |
| 174 | |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 175 | struct pn533_cmd_jump_dep_response { |
| 176 | u8 status; |
| 177 | u8 tg; |
| 178 | u8 nfcid3t[10]; |
| 179 | u8 didt; |
| 180 | u8 bst; |
| 181 | u8 brt; |
| 182 | u8 to; |
| 183 | u8 ppt; |
| 184 | /* optional */ |
| 185 | u8 gt[]; |
| 186 | } __packed; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 187 | |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 188 | |
| 189 | /* PN533_TG_INIT_AS_TARGET */ |
| 190 | #define PN533_INIT_TARGET_PASSIVE 0x1 |
| 191 | #define PN533_INIT_TARGET_DEP 0x2 |
| 192 | |
Samuel Ortiz | fc40a8c | 2012-06-01 13:21:13 +0200 | [diff] [blame] | 193 | #define PN533_INIT_TARGET_RESP_FRAME_MASK 0x3 |
| 194 | #define PN533_INIT_TARGET_RESP_ACTIVE 0x1 |
| 195 | #define PN533_INIT_TARGET_RESP_DEP 0x4 |
| 196 | |
Olivier Guiter | 1575b9d | 2013-06-13 15:43:27 +0200 | [diff] [blame] | 197 | /* The rule: value(high byte) + value(low byte) + checksum = 0 */ |
| 198 | static inline u8 pn533_ext_checksum(u16 value) |
| 199 | { |
| 200 | return ~(u8)(((value & 0xFF00) >> 8) + (u8)(value & 0xFF)) + 1; |
| 201 | } |
| 202 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 203 | /* The rule: value + checksum = 0 */ |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 204 | static inline u8 pn533_std_checksum(u8 value) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 205 | { |
| 206 | return ~value + 1; |
| 207 | } |
| 208 | |
| 209 | /* The rule: sum(data elements) + checksum = 0 */ |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 210 | static u8 pn533_std_data_checksum(u8 *data, int datalen) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 211 | { |
| 212 | u8 sum = 0; |
| 213 | int i; |
| 214 | |
| 215 | for (i = 0; i < datalen; i++) |
| 216 | sum += data[i]; |
| 217 | |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 218 | return pn533_std_checksum(sum); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 219 | } |
| 220 | |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 221 | static void pn533_std_tx_frame_init(void *_frame, u8 cmd_code) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 222 | { |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 223 | struct pn533_std_frame *frame = _frame; |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 224 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 225 | frame->preamble = 0; |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 226 | frame->start_frame = cpu_to_be16(PN533_STD_FRAME_SOF); |
| 227 | PN533_STD_FRAME_IDENTIFIER(frame) = PN533_STD_FRAME_DIR_OUT; |
Olivier Guiter | 1575b9d | 2013-06-13 15:43:27 +0200 | [diff] [blame] | 228 | PN533_FRAME_CMD(frame) = cmd_code; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 229 | frame->datalen = 2; |
| 230 | } |
| 231 | |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 232 | static void pn533_std_tx_frame_finish(void *_frame) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 233 | { |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 234 | struct pn533_std_frame *frame = _frame; |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 235 | |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 236 | frame->datalen_checksum = pn533_std_checksum(frame->datalen); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 237 | |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 238 | PN533_STD_FRAME_CHECKSUM(frame) = |
| 239 | pn533_std_data_checksum(frame->data, frame->datalen); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 240 | |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 241 | PN533_STD_FRAME_POSTAMBLE(frame) = 0; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 242 | } |
| 243 | |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 244 | static void pn533_std_tx_update_payload_len(void *_frame, int len) |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 245 | { |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 246 | struct pn533_std_frame *frame = _frame; |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 247 | |
| 248 | frame->datalen += len; |
| 249 | } |
| 250 | |
Olivier Guiter | 56a63c82 | 2013-06-13 13:43:29 +0000 | [diff] [blame] | 251 | static bool pn533_std_rx_frame_is_valid(void *_frame, struct pn533 *dev) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 252 | { |
| 253 | u8 checksum; |
Olivier Guiter | 1575b9d | 2013-06-13 15:43:27 +0200 | [diff] [blame] | 254 | struct pn533_std_frame *stdf = _frame; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 255 | |
Olivier Guiter | 1575b9d | 2013-06-13 15:43:27 +0200 | [diff] [blame] | 256 | if (stdf->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF)) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 257 | return false; |
| 258 | |
Olivier Guiter | 1575b9d | 2013-06-13 15:43:27 +0200 | [diff] [blame] | 259 | if (likely(!PN533_STD_IS_EXTENDED(stdf))) { |
| 260 | /* Standard frame code */ |
Olivier Guiter | 56a63c82 | 2013-06-13 13:43:29 +0000 | [diff] [blame] | 261 | dev->ops->rx_header_len = PN533_STD_FRAME_HEADER_LEN; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 262 | |
Olivier Guiter | 1575b9d | 2013-06-13 15:43:27 +0200 | [diff] [blame] | 263 | checksum = pn533_std_checksum(stdf->datalen); |
| 264 | if (checksum != stdf->datalen_checksum) |
| 265 | return false; |
| 266 | |
| 267 | checksum = pn533_std_data_checksum(stdf->data, stdf->datalen); |
| 268 | if (checksum != PN533_STD_FRAME_CHECKSUM(stdf)) |
| 269 | return false; |
| 270 | } else { |
| 271 | /* Extended */ |
| 272 | struct pn533_ext_frame *eif = _frame; |
| 273 | |
Olivier Guiter | 56a63c82 | 2013-06-13 13:43:29 +0000 | [diff] [blame] | 274 | dev->ops->rx_header_len = PN533_EXT_FRAME_HEADER_LEN; |
| 275 | |
Olivier Guiter | 1575b9d | 2013-06-13 15:43:27 +0200 | [diff] [blame] | 276 | checksum = pn533_ext_checksum(be16_to_cpu(eif->datalen)); |
| 277 | if (checksum != eif->datalen_checksum) |
| 278 | return false; |
| 279 | |
| 280 | /* check data checksum */ |
| 281 | checksum = pn533_std_data_checksum(eif->data, |
| 282 | be16_to_cpu(eif->datalen)); |
| 283 | if (checksum != PN533_EXT_FRAME_CHECKSUM(eif)) |
| 284 | return false; |
| 285 | } |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 286 | |
| 287 | return true; |
| 288 | } |
| 289 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 290 | bool pn533_rx_frame_is_ack(void *_frame) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 291 | { |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 292 | struct pn533_std_frame *frame = _frame; |
| 293 | |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 294 | if (frame->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF)) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 295 | return false; |
| 296 | |
| 297 | if (frame->datalen != 0 || frame->datalen_checksum != 0xFF) |
| 298 | return false; |
| 299 | |
| 300 | return true; |
| 301 | } |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 302 | EXPORT_SYMBOL_GPL(pn533_rx_frame_is_ack); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 303 | |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 304 | static inline int pn533_std_rx_frame_size(void *frame) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 305 | { |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 306 | struct pn533_std_frame *f = frame; |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 307 | |
Olivier Guiter | 1575b9d | 2013-06-13 15:43:27 +0200 | [diff] [blame] | 308 | /* check for Extended Information frame */ |
| 309 | if (PN533_STD_IS_EXTENDED(f)) { |
| 310 | struct pn533_ext_frame *eif = frame; |
| 311 | |
| 312 | return sizeof(struct pn533_ext_frame) |
| 313 | + be16_to_cpu(eif->datalen) + PN533_STD_FRAME_TAIL_LEN; |
| 314 | } |
| 315 | |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 316 | return sizeof(struct pn533_std_frame) + f->datalen + |
| 317 | PN533_STD_FRAME_TAIL_LEN; |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 318 | } |
| 319 | |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 320 | static u8 pn533_std_get_cmd_code(void *frame) |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 321 | { |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 322 | struct pn533_std_frame *f = frame; |
Olivier Guiter | 1575b9d | 2013-06-13 15:43:27 +0200 | [diff] [blame] | 323 | struct pn533_ext_frame *eif = frame; |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 324 | |
Olivier Guiter | 1575b9d | 2013-06-13 15:43:27 +0200 | [diff] [blame] | 325 | if (PN533_STD_IS_EXTENDED(f)) |
| 326 | return PN533_FRAME_CMD(eif); |
| 327 | else |
| 328 | return PN533_FRAME_CMD(f); |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 329 | } |
| 330 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 331 | bool pn533_rx_frame_is_cmd_response(struct pn533 *dev, void *frame) |
| 332 | { |
| 333 | return (dev->ops->get_cmd_code(frame) == |
| 334 | PN533_CMD_RESPONSE(dev->cmd->code)); |
| 335 | } |
| 336 | EXPORT_SYMBOL_GPL(pn533_rx_frame_is_cmd_response); |
| 337 | |
| 338 | |
Waldemar Rymarkiewicz | ef3d56e | 2013-01-16 15:51:51 +0100 | [diff] [blame] | 339 | static struct pn533_frame_ops pn533_std_frame_ops = { |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 340 | .tx_frame_init = pn533_std_tx_frame_init, |
| 341 | .tx_frame_finish = pn533_std_tx_frame_finish, |
| 342 | .tx_update_payload_len = pn533_std_tx_update_payload_len, |
| 343 | .tx_header_len = PN533_STD_FRAME_HEADER_LEN, |
| 344 | .tx_tail_len = PN533_STD_FRAME_TAIL_LEN, |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 345 | |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 346 | .rx_is_frame_valid = pn533_std_rx_frame_is_valid, |
| 347 | .rx_frame_size = pn533_std_rx_frame_size, |
| 348 | .rx_header_len = PN533_STD_FRAME_HEADER_LEN, |
| 349 | .rx_tail_len = PN533_STD_FRAME_TAIL_LEN, |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 350 | |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 351 | .max_payload_len = PN533_STD_FRAME_MAX_PAYLOAD_LEN, |
| 352 | .get_cmd_code = pn533_std_get_cmd_code, |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 353 | }; |
| 354 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 355 | static void pn533_build_cmd_frame(struct pn533 *dev, u8 cmd_code, |
| 356 | struct sk_buff *skb) |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 357 | { |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 358 | /* payload is already there, just update datalen */ |
| 359 | int payload_len = skb->len; |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 360 | struct pn533_frame_ops *ops = dev->ops; |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 361 | |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 362 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 363 | skb_push(skb, ops->tx_header_len); |
| 364 | skb_put(skb, ops->tx_tail_len); |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 365 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 366 | ops->tx_frame_init(skb->data, cmd_code); |
| 367 | ops->tx_update_payload_len(skb->data, payload_len); |
| 368 | ops->tx_frame_finish(skb->data); |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 369 | } |
| 370 | |
Waldemar Rymarkiewicz | ddf19d2 | 2013-04-03 08:02:08 +0200 | [diff] [blame] | 371 | static int pn533_send_async_complete(struct pn533 *dev) |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 372 | { |
Waldemar Rymarkiewicz | ddf19d2 | 2013-04-03 08:02:08 +0200 | [diff] [blame] | 373 | struct pn533_cmd *cmd = dev->cmd; |
Andrey Rusalin | 5dd9c1b | 2016-12-28 20:10:58 +0300 | [diff] [blame] | 374 | struct sk_buff *resp; |
| 375 | int status, rc = 0; |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 376 | |
Andrey Rusalin | 5dd9c1b | 2016-12-28 20:10:58 +0300 | [diff] [blame] | 377 | if (!cmd) { |
| 378 | dev_dbg(dev->dev, "%s: cmd not set\n", __func__); |
| 379 | goto done; |
| 380 | } |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 381 | |
Andrey Rusalin | 5dd9c1b | 2016-12-28 20:10:58 +0300 | [diff] [blame] | 382 | dev_kfree_skb(cmd->req); |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 383 | |
Andrey Rusalin | 5dd9c1b | 2016-12-28 20:10:58 +0300 | [diff] [blame] | 384 | status = cmd->status; |
| 385 | resp = cmd->resp; |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 386 | |
Waldemar Rymarkiewicz | 0c33d26 | 2012-12-10 14:42:59 +0100 | [diff] [blame] | 387 | if (status < 0) { |
Waldemar Rymarkiewicz | 4231604 | 2013-04-03 08:02:04 +0200 | [diff] [blame] | 388 | rc = cmd->complete_cb(dev, cmd->complete_cb_context, |
| 389 | ERR_PTR(status)); |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 390 | dev_kfree_skb(resp); |
Waldemar Rymarkiewicz | 2c206fb | 2013-04-03 08:02:05 +0200 | [diff] [blame] | 391 | goto done; |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 392 | } |
| 393 | |
Andrey Rusalin | 5dd9c1b | 2016-12-28 20:10:58 +0300 | [diff] [blame] | 394 | /* when no response is set we got interrupted */ |
| 395 | if (!resp) |
| 396 | resp = ERR_PTR(-EINTR); |
| 397 | |
| 398 | if (!IS_ERR(resp)) { |
| 399 | skb_pull(resp, dev->ops->rx_header_len); |
| 400 | skb_trim(resp, resp->len - dev->ops->rx_tail_len); |
| 401 | } |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 402 | |
Waldemar Rymarkiewicz | 4231604 | 2013-04-03 08:02:04 +0200 | [diff] [blame] | 403 | rc = cmd->complete_cb(dev, cmd->complete_cb_context, resp); |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 404 | |
Waldemar Rymarkiewicz | 2c206fb | 2013-04-03 08:02:05 +0200 | [diff] [blame] | 405 | done: |
Waldemar Rymarkiewicz | 4231604 | 2013-04-03 08:02:04 +0200 | [diff] [blame] | 406 | kfree(cmd); |
Waldemar Rymarkiewicz | 2c206fb | 2013-04-03 08:02:05 +0200 | [diff] [blame] | 407 | dev->cmd = NULL; |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 408 | return rc; |
| 409 | } |
| 410 | |
| 411 | static int __pn533_send_async(struct pn533 *dev, u8 cmd_code, |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 412 | struct sk_buff *req, |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 413 | pn533_send_async_complete_t complete_cb, |
| 414 | void *complete_cb_context) |
| 415 | { |
| 416 | struct pn533_cmd *cmd; |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 417 | int rc = 0; |
| 418 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 419 | dev_dbg(dev->dev, "Sending command 0x%x\n", cmd_code); |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 420 | |
Waldemar Rymarkiewicz | 4231604 | 2013-04-03 08:02:04 +0200 | [diff] [blame] | 421 | cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); |
| 422 | if (!cmd) |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 423 | return -ENOMEM; |
| 424 | |
Waldemar Rymarkiewicz | 4b2a953 | 2013-04-03 08:02:06 +0200 | [diff] [blame] | 425 | cmd->code = cmd_code; |
Waldemar Rymarkiewicz | 4231604 | 2013-04-03 08:02:04 +0200 | [diff] [blame] | 426 | cmd->req = req; |
Waldemar Rymarkiewicz | 4231604 | 2013-04-03 08:02:04 +0200 | [diff] [blame] | 427 | cmd->complete_cb = complete_cb; |
| 428 | cmd->complete_cb_context = complete_cb_context; |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 429 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 430 | pn533_build_cmd_frame(dev, cmd_code, req); |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 431 | |
| 432 | mutex_lock(&dev->cmd_lock); |
| 433 | |
| 434 | if (!dev->cmd_pending) { |
Andrey Rusalin | 5dd9c1b | 2016-12-28 20:10:58 +0300 | [diff] [blame] | 435 | dev->cmd = cmd; |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 436 | rc = dev->phy_ops->send_frame(dev, req); |
Andrey Rusalin | 5dd9c1b | 2016-12-28 20:10:58 +0300 | [diff] [blame] | 437 | if (rc) { |
| 438 | dev->cmd = NULL; |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 439 | goto error; |
Andrey Rusalin | 5dd9c1b | 2016-12-28 20:10:58 +0300 | [diff] [blame] | 440 | } |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 441 | |
| 442 | dev->cmd_pending = 1; |
| 443 | goto unlock; |
| 444 | } |
| 445 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 446 | dev_dbg(dev->dev, "%s Queueing command 0x%x\n", |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 447 | __func__, cmd_code); |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 448 | |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 449 | INIT_LIST_HEAD(&cmd->queue); |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 450 | list_add_tail(&cmd->queue, &dev->cmd_queue); |
| 451 | |
| 452 | goto unlock; |
| 453 | |
| 454 | error: |
Waldemar Rymarkiewicz | 4231604 | 2013-04-03 08:02:04 +0200 | [diff] [blame] | 455 | kfree(cmd); |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 456 | unlock: |
| 457 | mutex_unlock(&dev->cmd_lock); |
| 458 | return rc; |
| 459 | } |
| 460 | |
Waldemar Rymarkiewicz | 15461ae | 2012-11-26 14:18:35 +0100 | [diff] [blame] | 461 | static int pn533_send_data_async(struct pn533 *dev, u8 cmd_code, |
| 462 | struct sk_buff *req, |
| 463 | pn533_send_async_complete_t complete_cb, |
| 464 | void *complete_cb_context) |
| 465 | { |
Waldemar Rymarkiewicz | 15461ae | 2012-11-26 14:18:35 +0100 | [diff] [blame] | 466 | int rc; |
Waldemar Rymarkiewicz | 15461ae | 2012-11-26 14:18:35 +0100 | [diff] [blame] | 467 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 468 | rc = __pn533_send_async(dev, cmd_code, req, complete_cb, |
Waldemar Rymarkiewicz | 15461ae | 2012-11-26 14:18:35 +0100 | [diff] [blame] | 469 | complete_cb_context); |
Waldemar Rymarkiewicz | 15461ae | 2012-11-26 14:18:35 +0100 | [diff] [blame] | 470 | |
| 471 | return rc; |
| 472 | } |
| 473 | |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 474 | static int pn533_send_cmd_async(struct pn533 *dev, u8 cmd_code, |
| 475 | struct sk_buff *req, |
| 476 | pn533_send_async_complete_t complete_cb, |
| 477 | void *complete_cb_context) |
| 478 | { |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 479 | int rc; |
| 480 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 481 | rc = __pn533_send_async(dev, cmd_code, req, complete_cb, |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 482 | complete_cb_context); |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 483 | |
| 484 | return rc; |
| 485 | } |
| 486 | |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 487 | /* |
| 488 | * pn533_send_cmd_direct_async |
| 489 | * |
Masahiro Yamada | 0cf2a84 | 2017-02-27 14:29:39 -0800 | [diff] [blame] | 490 | * The function sends a piority cmd directly to the chip omitting the cmd |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 491 | * queue. It's intended to be used by chaining mechanism of received responses |
| 492 | * where the host has to request every single chunk of data before scheduling |
| 493 | * next cmd from the queue. |
| 494 | */ |
| 495 | static int pn533_send_cmd_direct_async(struct pn533 *dev, u8 cmd_code, |
| 496 | struct sk_buff *req, |
| 497 | pn533_send_async_complete_t complete_cb, |
| 498 | void *complete_cb_context) |
| 499 | { |
Waldemar Rymarkiewicz | 4231604 | 2013-04-03 08:02:04 +0200 | [diff] [blame] | 500 | struct pn533_cmd *cmd; |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 501 | int rc; |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 502 | |
Waldemar Rymarkiewicz | 4231604 | 2013-04-03 08:02:04 +0200 | [diff] [blame] | 503 | cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 504 | if (!cmd) |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 505 | return -ENOMEM; |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 506 | |
Waldemar Rymarkiewicz | 4b2a953 | 2013-04-03 08:02:06 +0200 | [diff] [blame] | 507 | cmd->code = cmd_code; |
Waldemar Rymarkiewicz | 4231604 | 2013-04-03 08:02:04 +0200 | [diff] [blame] | 508 | cmd->req = req; |
Waldemar Rymarkiewicz | 4231604 | 2013-04-03 08:02:04 +0200 | [diff] [blame] | 509 | cmd->complete_cb = complete_cb; |
| 510 | cmd->complete_cb_context = complete_cb_context; |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 511 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 512 | pn533_build_cmd_frame(dev, cmd_code, req); |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 513 | |
Andrey Rusalin | 5dd9c1b | 2016-12-28 20:10:58 +0300 | [diff] [blame] | 514 | dev->cmd = cmd; |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 515 | rc = dev->phy_ops->send_frame(dev, req); |
Andrey Rusalin | 5dd9c1b | 2016-12-28 20:10:58 +0300 | [diff] [blame] | 516 | if (rc < 0) { |
| 517 | dev->cmd = NULL; |
Waldemar Rymarkiewicz | 4231604 | 2013-04-03 08:02:04 +0200 | [diff] [blame] | 518 | kfree(cmd); |
Andrey Rusalin | 5dd9c1b | 2016-12-28 20:10:58 +0300 | [diff] [blame] | 519 | } |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 520 | |
| 521 | return rc; |
| 522 | } |
| 523 | |
Waldemar Rymarkiewicz | c79490e | 2013-04-03 08:02:09 +0200 | [diff] [blame] | 524 | static void pn533_wq_cmd_complete(struct work_struct *work) |
| 525 | { |
| 526 | struct pn533 *dev = container_of(work, struct pn533, cmd_complete_work); |
| 527 | int rc; |
| 528 | |
| 529 | rc = pn533_send_async_complete(dev); |
| 530 | if (rc != -EINPROGRESS) |
| 531 | queue_work(dev->wq, &dev->cmd_work); |
| 532 | } |
| 533 | |
Samuel Ortiz | 5d50b36 | 2012-08-17 23:47:54 +0200 | [diff] [blame] | 534 | static void pn533_wq_cmd(struct work_struct *work) |
| 535 | { |
| 536 | struct pn533 *dev = container_of(work, struct pn533, cmd_work); |
| 537 | struct pn533_cmd *cmd; |
Waldemar Rymarkiewicz | 0ce1fbd | 2013-04-03 08:02:03 +0200 | [diff] [blame] | 538 | int rc; |
Samuel Ortiz | 5d50b36 | 2012-08-17 23:47:54 +0200 | [diff] [blame] | 539 | |
| 540 | mutex_lock(&dev->cmd_lock); |
| 541 | |
| 542 | if (list_empty(&dev->cmd_queue)) { |
| 543 | dev->cmd_pending = 0; |
| 544 | mutex_unlock(&dev->cmd_lock); |
| 545 | return; |
| 546 | } |
| 547 | |
| 548 | cmd = list_first_entry(&dev->cmd_queue, struct pn533_cmd, queue); |
| 549 | |
Szymon Janc | 60ad07a | 2012-10-25 17:29:45 +0200 | [diff] [blame] | 550 | list_del(&cmd->queue); |
| 551 | |
Samuel Ortiz | 5d50b36 | 2012-08-17 23:47:54 +0200 | [diff] [blame] | 552 | mutex_unlock(&dev->cmd_lock); |
| 553 | |
Andrey Rusalin | 5dd9c1b | 2016-12-28 20:10:58 +0300 | [diff] [blame] | 554 | dev->cmd = cmd; |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 555 | rc = dev->phy_ops->send_frame(dev, cmd->req); |
Waldemar Rymarkiewicz | 0ce1fbd | 2013-04-03 08:02:03 +0200 | [diff] [blame] | 556 | if (rc < 0) { |
Andrey Rusalin | 5dd9c1b | 2016-12-28 20:10:58 +0300 | [diff] [blame] | 557 | dev->cmd = NULL; |
Waldemar Rymarkiewicz | 0ce1fbd | 2013-04-03 08:02:03 +0200 | [diff] [blame] | 558 | dev_kfree_skb(cmd->req); |
Waldemar Rymarkiewicz | 4231604 | 2013-04-03 08:02:04 +0200 | [diff] [blame] | 559 | kfree(cmd); |
Waldemar Rymarkiewicz | 2c206fb | 2013-04-03 08:02:05 +0200 | [diff] [blame] | 560 | return; |
Waldemar Rymarkiewicz | 0ce1fbd | 2013-04-03 08:02:03 +0200 | [diff] [blame] | 561 | } |
Waldemar Rymarkiewicz | 2c206fb | 2013-04-03 08:02:05 +0200 | [diff] [blame] | 562 | |
Samuel Ortiz | 5d50b36 | 2012-08-17 23:47:54 +0200 | [diff] [blame] | 563 | } |
| 564 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 565 | struct pn533_sync_cmd_response { |
Waldemar Rymarkiewicz | 94c5c15 | 2012-11-26 14:18:36 +0100 | [diff] [blame] | 566 | struct sk_buff *resp; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 567 | struct completion done; |
| 568 | }; |
| 569 | |
Waldemar Rymarkiewicz | 94c5c15 | 2012-11-26 14:18:36 +0100 | [diff] [blame] | 570 | static int pn533_send_sync_complete(struct pn533 *dev, void *_arg, |
| 571 | struct sk_buff *resp) |
| 572 | { |
| 573 | struct pn533_sync_cmd_response *arg = _arg; |
| 574 | |
Waldemar Rymarkiewicz | 94c5c15 | 2012-11-26 14:18:36 +0100 | [diff] [blame] | 575 | arg->resp = resp; |
| 576 | complete(&arg->done); |
| 577 | |
| 578 | return 0; |
| 579 | } |
| 580 | |
| 581 | /* pn533_send_cmd_sync |
| 582 | * |
| 583 | * Please note the req parameter is freed inside the function to |
| 584 | * limit a number of return value interpretations by the caller. |
| 585 | * |
| 586 | * 1. negative in case of error during TX path -> req should be freed |
| 587 | * |
| 588 | * 2. negative in case of error during RX path -> req should not be freed |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 589 | * as it's been already freed at the beginning of RX path by |
Waldemar Rymarkiewicz | 94c5c15 | 2012-11-26 14:18:36 +0100 | [diff] [blame] | 590 | * async_complete_cb. |
| 591 | * |
| 592 | * 3. valid pointer in case of succesfult RX path |
| 593 | * |
| 594 | * A caller has to check a return value with IS_ERR macro. If the test pass, |
| 595 | * the returned pointer is valid. |
| 596 | * |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 597 | */ |
Waldemar Rymarkiewicz | 94c5c15 | 2012-11-26 14:18:36 +0100 | [diff] [blame] | 598 | static struct sk_buff *pn533_send_cmd_sync(struct pn533 *dev, u8 cmd_code, |
| 599 | struct sk_buff *req) |
| 600 | { |
| 601 | int rc; |
| 602 | struct pn533_sync_cmd_response arg; |
| 603 | |
Waldemar Rymarkiewicz | 94c5c15 | 2012-11-26 14:18:36 +0100 | [diff] [blame] | 604 | init_completion(&arg.done); |
| 605 | |
| 606 | rc = pn533_send_cmd_async(dev, cmd_code, req, |
| 607 | pn533_send_sync_complete, &arg); |
| 608 | if (rc) { |
| 609 | dev_kfree_skb(req); |
| 610 | return ERR_PTR(rc); |
| 611 | } |
| 612 | |
| 613 | wait_for_completion(&arg.done); |
| 614 | |
| 615 | return arg.resp; |
| 616 | } |
| 617 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 618 | static struct sk_buff *pn533_alloc_skb(struct pn533 *dev, unsigned int size) |
Waldemar Rymarkiewicz | d22b2db | 2012-11-26 14:18:37 +0100 | [diff] [blame] | 619 | { |
| 620 | struct sk_buff *skb; |
| 621 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 622 | skb = alloc_skb(dev->ops->tx_header_len + |
Waldemar Rymarkiewicz | d22b2db | 2012-11-26 14:18:37 +0100 | [diff] [blame] | 623 | size + |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 624 | dev->ops->tx_tail_len, GFP_KERNEL); |
Waldemar Rymarkiewicz | d22b2db | 2012-11-26 14:18:37 +0100 | [diff] [blame] | 625 | |
| 626 | if (skb) |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 627 | skb_reserve(skb, dev->ops->tx_header_len); |
Waldemar Rymarkiewicz | d22b2db | 2012-11-26 14:18:37 +0100 | [diff] [blame] | 628 | |
| 629 | return skb; |
| 630 | } |
| 631 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 632 | struct pn533_target_type_a { |
| 633 | __be16 sens_res; |
| 634 | u8 sel_res; |
| 635 | u8 nfcid_len; |
| 636 | u8 nfcid_data[]; |
| 637 | } __packed; |
| 638 | |
| 639 | |
| 640 | #define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6)) |
| 641 | #define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0)) |
| 642 | #define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8)) |
| 643 | |
| 644 | #define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00 |
| 645 | #define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C |
| 646 | |
| 647 | #define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5) |
| 648 | #define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2) |
| 649 | |
| 650 | #define PN533_TYPE_A_SEL_PROT_MIFARE 0 |
| 651 | #define PN533_TYPE_A_SEL_PROT_ISO14443 1 |
| 652 | #define PN533_TYPE_A_SEL_PROT_DEP 2 |
| 653 | #define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3 |
| 654 | |
| 655 | static bool pn533_target_type_a_is_valid(struct pn533_target_type_a *type_a, |
| 656 | int target_data_len) |
| 657 | { |
| 658 | u8 ssd; |
| 659 | u8 platconf; |
| 660 | |
| 661 | if (target_data_len < sizeof(struct pn533_target_type_a)) |
| 662 | return false; |
| 663 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 664 | /* |
| 665 | * The length check of nfcid[] and ats[] are not being performed because |
| 666 | * the values are not being used |
| 667 | */ |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 668 | |
| 669 | /* Requirement 4.6.3.3 from NFC Forum Digital Spec */ |
| 670 | ssd = PN533_TYPE_A_SENS_RES_SSD(type_a->sens_res); |
| 671 | platconf = PN533_TYPE_A_SENS_RES_PLATCONF(type_a->sens_res); |
| 672 | |
| 673 | if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL && |
Waldemar Rymarkiewicz | 5d46774 | 2012-12-10 14:42:51 +0100 | [diff] [blame] | 674 | platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) || |
| 675 | (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL && |
| 676 | platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL)) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 677 | return false; |
| 678 | |
| 679 | /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */ |
| 680 | if (PN533_TYPE_A_SEL_CASCADE(type_a->sel_res) != 0) |
| 681 | return false; |
| 682 | |
| 683 | return true; |
| 684 | } |
| 685 | |
| 686 | static int pn533_target_found_type_a(struct nfc_target *nfc_tgt, u8 *tgt_data, |
| 687 | int tgt_data_len) |
| 688 | { |
| 689 | struct pn533_target_type_a *tgt_type_a; |
| 690 | |
Waldemar Rymarkiewicz | 37cf4fc | 2012-12-10 14:42:50 +0100 | [diff] [blame] | 691 | tgt_type_a = (struct pn533_target_type_a *)tgt_data; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 692 | |
| 693 | if (!pn533_target_type_a_is_valid(tgt_type_a, tgt_data_len)) |
| 694 | return -EPROTO; |
| 695 | |
| 696 | switch (PN533_TYPE_A_SEL_PROT(tgt_type_a->sel_res)) { |
| 697 | case PN533_TYPE_A_SEL_PROT_MIFARE: |
| 698 | nfc_tgt->supported_protocols = NFC_PROTO_MIFARE_MASK; |
| 699 | break; |
| 700 | case PN533_TYPE_A_SEL_PROT_ISO14443: |
| 701 | nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK; |
| 702 | break; |
| 703 | case PN533_TYPE_A_SEL_PROT_DEP: |
| 704 | nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK; |
| 705 | break; |
| 706 | case PN533_TYPE_A_SEL_PROT_ISO14443_DEP: |
| 707 | nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK | |
| 708 | NFC_PROTO_NFC_DEP_MASK; |
| 709 | break; |
| 710 | } |
| 711 | |
| 712 | nfc_tgt->sens_res = be16_to_cpu(tgt_type_a->sens_res); |
| 713 | nfc_tgt->sel_res = tgt_type_a->sel_res; |
Samuel Ortiz | c3b1e1e | 2012-03-05 01:03:33 +0100 | [diff] [blame] | 714 | nfc_tgt->nfcid1_len = tgt_type_a->nfcid_len; |
| 715 | memcpy(nfc_tgt->nfcid1, tgt_type_a->nfcid_data, nfc_tgt->nfcid1_len); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 716 | |
| 717 | return 0; |
| 718 | } |
| 719 | |
| 720 | struct pn533_target_felica { |
| 721 | u8 pol_res; |
| 722 | u8 opcode; |
Samuel Ortiz | 322bce9 | 2013-05-27 15:29:11 +0200 | [diff] [blame] | 723 | u8 nfcid2[NFC_NFCID2_MAXSIZE]; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 724 | u8 pad[8]; |
| 725 | /* optional */ |
| 726 | u8 syst_code[]; |
| 727 | } __packed; |
| 728 | |
| 729 | #define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01 |
| 730 | #define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE |
| 731 | |
| 732 | static bool pn533_target_felica_is_valid(struct pn533_target_felica *felica, |
| 733 | int target_data_len) |
| 734 | { |
| 735 | if (target_data_len < sizeof(struct pn533_target_felica)) |
| 736 | return false; |
| 737 | |
| 738 | if (felica->opcode != PN533_FELICA_OPC_SENSF_RES) |
| 739 | return false; |
| 740 | |
| 741 | return true; |
| 742 | } |
| 743 | |
| 744 | static int pn533_target_found_felica(struct nfc_target *nfc_tgt, u8 *tgt_data, |
| 745 | int tgt_data_len) |
| 746 | { |
| 747 | struct pn533_target_felica *tgt_felica; |
| 748 | |
Waldemar Rymarkiewicz | 37cf4fc | 2012-12-10 14:42:50 +0100 | [diff] [blame] | 749 | tgt_felica = (struct pn533_target_felica *)tgt_data; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 750 | |
| 751 | if (!pn533_target_felica_is_valid(tgt_felica, tgt_data_len)) |
| 752 | return -EPROTO; |
| 753 | |
Waldemar Rymarkiewicz | 5d46774 | 2012-12-10 14:42:51 +0100 | [diff] [blame] | 754 | if ((tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1) && |
| 755 | (tgt_felica->nfcid2[1] == PN533_FELICA_SENSF_NFCID2_DEP_B2)) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 756 | nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK; |
| 757 | else |
| 758 | nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK; |
| 759 | |
Samuel Ortiz | 7975754 | 2012-03-05 01:03:45 +0100 | [diff] [blame] | 760 | memcpy(nfc_tgt->sensf_res, &tgt_felica->opcode, 9); |
| 761 | nfc_tgt->sensf_res_len = 9; |
| 762 | |
Samuel Ortiz | 322bce9 | 2013-05-27 15:29:11 +0200 | [diff] [blame] | 763 | memcpy(nfc_tgt->nfcid2, tgt_felica->nfcid2, NFC_NFCID2_MAXSIZE); |
| 764 | nfc_tgt->nfcid2_len = NFC_NFCID2_MAXSIZE; |
| 765 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 766 | return 0; |
| 767 | } |
| 768 | |
| 769 | struct pn533_target_jewel { |
| 770 | __be16 sens_res; |
| 771 | u8 jewelid[4]; |
| 772 | } __packed; |
| 773 | |
| 774 | static bool pn533_target_jewel_is_valid(struct pn533_target_jewel *jewel, |
| 775 | int target_data_len) |
| 776 | { |
| 777 | u8 ssd; |
| 778 | u8 platconf; |
| 779 | |
| 780 | if (target_data_len < sizeof(struct pn533_target_jewel)) |
| 781 | return false; |
| 782 | |
| 783 | /* Requirement 4.6.3.3 from NFC Forum Digital Spec */ |
| 784 | ssd = PN533_TYPE_A_SENS_RES_SSD(jewel->sens_res); |
| 785 | platconf = PN533_TYPE_A_SENS_RES_PLATCONF(jewel->sens_res); |
| 786 | |
| 787 | if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL && |
Waldemar Rymarkiewicz | 5d46774 | 2012-12-10 14:42:51 +0100 | [diff] [blame] | 788 | platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) || |
| 789 | (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL && |
| 790 | platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL)) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 791 | return false; |
| 792 | |
| 793 | return true; |
| 794 | } |
| 795 | |
| 796 | static int pn533_target_found_jewel(struct nfc_target *nfc_tgt, u8 *tgt_data, |
| 797 | int tgt_data_len) |
| 798 | { |
| 799 | struct pn533_target_jewel *tgt_jewel; |
| 800 | |
Waldemar Rymarkiewicz | 37cf4fc | 2012-12-10 14:42:50 +0100 | [diff] [blame] | 801 | tgt_jewel = (struct pn533_target_jewel *)tgt_data; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 802 | |
| 803 | if (!pn533_target_jewel_is_valid(tgt_jewel, tgt_data_len)) |
| 804 | return -EPROTO; |
| 805 | |
| 806 | nfc_tgt->supported_protocols = NFC_PROTO_JEWEL_MASK; |
| 807 | nfc_tgt->sens_res = be16_to_cpu(tgt_jewel->sens_res); |
Samuel Ortiz | d8dc107 | 2012-03-05 01:03:46 +0100 | [diff] [blame] | 808 | nfc_tgt->nfcid1_len = 4; |
| 809 | memcpy(nfc_tgt->nfcid1, tgt_jewel->jewelid, nfc_tgt->nfcid1_len); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 810 | |
| 811 | return 0; |
| 812 | } |
| 813 | |
| 814 | struct pn533_type_b_prot_info { |
| 815 | u8 bitrate; |
| 816 | u8 fsci_type; |
| 817 | u8 fwi_adc_fo; |
| 818 | } __packed; |
| 819 | |
| 820 | #define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4) |
| 821 | #define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0) |
| 822 | #define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8 |
| 823 | |
| 824 | struct pn533_type_b_sens_res { |
| 825 | u8 opcode; |
| 826 | u8 nfcid[4]; |
| 827 | u8 appdata[4]; |
| 828 | struct pn533_type_b_prot_info prot_info; |
| 829 | } __packed; |
| 830 | |
| 831 | #define PN533_TYPE_B_OPC_SENSB_RES 0x50 |
| 832 | |
| 833 | struct pn533_target_type_b { |
| 834 | struct pn533_type_b_sens_res sensb_res; |
| 835 | u8 attrib_res_len; |
| 836 | u8 attrib_res[]; |
| 837 | } __packed; |
| 838 | |
| 839 | static bool pn533_target_type_b_is_valid(struct pn533_target_type_b *type_b, |
| 840 | int target_data_len) |
| 841 | { |
| 842 | if (target_data_len < sizeof(struct pn533_target_type_b)) |
| 843 | return false; |
| 844 | |
| 845 | if (type_b->sensb_res.opcode != PN533_TYPE_B_OPC_SENSB_RES) |
| 846 | return false; |
| 847 | |
| 848 | if (PN533_TYPE_B_PROT_TYPE(type_b->sensb_res.prot_info.fsci_type) & |
| 849 | PN533_TYPE_B_PROT_TYPE_RFU_MASK) |
| 850 | return false; |
| 851 | |
| 852 | return true; |
| 853 | } |
| 854 | |
| 855 | static int pn533_target_found_type_b(struct nfc_target *nfc_tgt, u8 *tgt_data, |
| 856 | int tgt_data_len) |
| 857 | { |
| 858 | struct pn533_target_type_b *tgt_type_b; |
| 859 | |
Waldemar Rymarkiewicz | 37cf4fc | 2012-12-10 14:42:50 +0100 | [diff] [blame] | 860 | tgt_type_b = (struct pn533_target_type_b *)tgt_data; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 861 | |
| 862 | if (!pn533_target_type_b_is_valid(tgt_type_b, tgt_data_len)) |
| 863 | return -EPROTO; |
| 864 | |
Samuel Ortiz | 01d719a | 2012-07-04 00:14:04 +0200 | [diff] [blame] | 865 | nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_B_MASK; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 866 | |
| 867 | return 0; |
| 868 | } |
| 869 | |
Michael Thalmeier | c952f91 | 2016-04-21 16:43:52 +0200 | [diff] [blame] | 870 | static void pn533_poll_reset_mod_list(struct pn533 *dev); |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 871 | static int pn533_target_found(struct pn533 *dev, u8 tg, u8 *tgdata, |
| 872 | int tgdata_len) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 873 | { |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 874 | struct nfc_target nfc_tgt; |
| 875 | int rc; |
| 876 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 877 | dev_dbg(dev->dev, "%s: modulation=%d\n", |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 878 | __func__, dev->poll_mod_curr); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 879 | |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 880 | if (tg != 1) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 881 | return -EPROTO; |
| 882 | |
Samuel Ortiz | 98b3ac1 | 2012-03-05 01:03:39 +0100 | [diff] [blame] | 883 | memset(&nfc_tgt, 0, sizeof(struct nfc_target)); |
| 884 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 885 | switch (dev->poll_mod_curr) { |
| 886 | case PN533_POLL_MOD_106KBPS_A: |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 887 | rc = pn533_target_found_type_a(&nfc_tgt, tgdata, tgdata_len); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 888 | break; |
| 889 | case PN533_POLL_MOD_212KBPS_FELICA: |
| 890 | case PN533_POLL_MOD_424KBPS_FELICA: |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 891 | rc = pn533_target_found_felica(&nfc_tgt, tgdata, tgdata_len); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 892 | break; |
| 893 | case PN533_POLL_MOD_106KBPS_JEWEL: |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 894 | rc = pn533_target_found_jewel(&nfc_tgt, tgdata, tgdata_len); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 895 | break; |
| 896 | case PN533_POLL_MOD_847KBPS_B: |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 897 | rc = pn533_target_found_type_b(&nfc_tgt, tgdata, tgdata_len); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 898 | break; |
| 899 | default: |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 900 | nfc_err(dev->dev, |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 901 | "Unknown current poll modulation\n"); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 902 | return -EPROTO; |
| 903 | } |
| 904 | |
| 905 | if (rc) |
| 906 | return rc; |
| 907 | |
| 908 | if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) { |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 909 | dev_dbg(dev->dev, |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 910 | "The Tg found doesn't have the desired protocol\n"); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 911 | return -EAGAIN; |
| 912 | } |
| 913 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 914 | dev_dbg(dev->dev, |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 915 | "Target found - supported protocols: 0x%x\n", |
| 916 | nfc_tgt.supported_protocols); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 917 | |
| 918 | dev->tgt_available_prots = nfc_tgt.supported_protocols; |
| 919 | |
Michael Thalmeier | c952f91 | 2016-04-21 16:43:52 +0200 | [diff] [blame] | 920 | pn533_poll_reset_mod_list(dev); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 921 | nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1); |
| 922 | |
| 923 | return 0; |
| 924 | } |
| 925 | |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 926 | static inline void pn533_poll_next_mod(struct pn533 *dev) |
| 927 | { |
| 928 | dev->poll_mod_curr = (dev->poll_mod_curr + 1) % dev->poll_mod_count; |
| 929 | } |
| 930 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 931 | static void pn533_poll_reset_mod_list(struct pn533 *dev) |
| 932 | { |
| 933 | dev->poll_mod_count = 0; |
| 934 | } |
| 935 | |
| 936 | static void pn533_poll_add_mod(struct pn533 *dev, u8 mod_index) |
| 937 | { |
| 938 | dev->poll_mod_active[dev->poll_mod_count] = |
Waldemar Rymarkiewicz | 37cf4fc | 2012-12-10 14:42:50 +0100 | [diff] [blame] | 939 | (struct pn533_poll_modulations *)&poll_mod[mod_index]; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 940 | dev->poll_mod_count++; |
| 941 | } |
| 942 | |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 943 | static void pn533_poll_create_mod_list(struct pn533 *dev, |
| 944 | u32 im_protocols, u32 tm_protocols) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 945 | { |
| 946 | pn533_poll_reset_mod_list(dev); |
| 947 | |
Waldemar Rymarkiewicz | b08e860 | 2012-12-10 14:42:52 +0100 | [diff] [blame] | 948 | if ((im_protocols & NFC_PROTO_MIFARE_MASK) || |
| 949 | (im_protocols & NFC_PROTO_ISO14443_MASK) || |
| 950 | (im_protocols & NFC_PROTO_NFC_DEP_MASK)) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 951 | pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A); |
| 952 | |
Waldemar Rymarkiewicz | b08e860 | 2012-12-10 14:42:52 +0100 | [diff] [blame] | 953 | if (im_protocols & NFC_PROTO_FELICA_MASK || |
| 954 | im_protocols & NFC_PROTO_NFC_DEP_MASK) { |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 955 | pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA); |
| 956 | pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA); |
| 957 | } |
| 958 | |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 959 | if (im_protocols & NFC_PROTO_JEWEL_MASK) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 960 | pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_JEWEL); |
| 961 | |
Samuel Ortiz | 01d719a | 2012-07-04 00:14:04 +0200 | [diff] [blame] | 962 | if (im_protocols & NFC_PROTO_ISO14443_B_MASK) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 963 | pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 964 | |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 965 | if (tm_protocols) |
| 966 | pn533_poll_add_mod(dev, PN533_LISTEN_MOD); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 967 | } |
| 968 | |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 969 | static int pn533_start_poll_complete(struct pn533 *dev, struct sk_buff *resp) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 970 | { |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 971 | u8 nbtg, tg, *tgdata; |
| 972 | int rc, tgdata_len; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 973 | |
Samuel Ortiz | 673088f | 2013-08-21 15:06:55 +0200 | [diff] [blame] | 974 | /* Toggle the DEP polling */ |
Michael Thalmeier | e997ebbe46 | 2016-03-25 15:46:51 +0100 | [diff] [blame] | 975 | if (dev->poll_protocols & NFC_PROTO_NFC_DEP_MASK) |
| 976 | dev->poll_dep = 1; |
Samuel Ortiz | 673088f | 2013-08-21 15:06:55 +0200 | [diff] [blame] | 977 | |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 978 | nbtg = resp->data[0]; |
| 979 | tg = resp->data[1]; |
| 980 | tgdata = &resp->data[2]; |
| 981 | tgdata_len = resp->len - 2; /* nbtg + tg */ |
| 982 | |
| 983 | if (nbtg) { |
| 984 | rc = pn533_target_found(dev, tg, tgdata, tgdata_len); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 985 | |
| 986 | /* We must stop the poll after a valid target found */ |
Michael Thalmeier | c952f91 | 2016-04-21 16:43:52 +0200 | [diff] [blame] | 987 | if (rc == 0) |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 988 | return 0; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 989 | } |
| 990 | |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 991 | return -EAGAIN; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 992 | } |
| 993 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 994 | static struct sk_buff *pn533_alloc_poll_tg_frame(struct pn533 *dev) |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 995 | { |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 996 | struct sk_buff *skb; |
yuan linyu | b952f4d | 2017-06-18 22:52:04 +0800 | [diff] [blame] | 997 | u8 *felica, *nfcid3; |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 998 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 999 | u8 *gbytes = dev->gb; |
| 1000 | size_t gbytes_len = dev->gb_len; |
| 1001 | |
Samuel Ortiz | 51d9e80 | 2012-05-30 01:48:46 +0200 | [diff] [blame] | 1002 | u8 felica_params[18] = {0x1, 0xfe, /* DEP */ |
| 1003 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* random */ |
| 1004 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, |
| 1005 | 0xff, 0xff}; /* System code */ |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1006 | |
Samuel Ortiz | 51d9e80 | 2012-05-30 01:48:46 +0200 | [diff] [blame] | 1007 | u8 mifare_params[6] = {0x1, 0x1, /* SENS_RES */ |
| 1008 | 0x0, 0x0, 0x0, |
| 1009 | 0x40}; /* SEL_RES for DEP */ |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 1010 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 1011 | unsigned int skb_len = 36 + /* |
| 1012 | * mode (1), mifare (6), |
| 1013 | * felica (18), nfcid3 (10), gb_len (1) |
| 1014 | */ |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1015 | gbytes_len + |
| 1016 | 1; /* len Tk*/ |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 1017 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 1018 | skb = pn533_alloc_skb(dev, skb_len); |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1019 | if (!skb) |
| 1020 | return NULL; |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 1021 | |
| 1022 | /* DEP support only */ |
Johannes Berg | 634fef6 | 2017-06-16 14:29:24 +0200 | [diff] [blame] | 1023 | skb_put_u8(skb, PN533_INIT_TARGET_DEP); |
Samuel Ortiz | 51d9e80 | 2012-05-30 01:48:46 +0200 | [diff] [blame] | 1024 | |
| 1025 | /* MIFARE params */ |
Johannes Berg | 59ae1d1 | 2017-06-16 14:29:20 +0200 | [diff] [blame] | 1026 | skb_put_data(skb, mifare_params, 6); |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1027 | |
| 1028 | /* Felica params */ |
Johannes Berg | 59ae1d1 | 2017-06-16 14:29:20 +0200 | [diff] [blame] | 1029 | felica = skb_put_data(skb, felica_params, 18); |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1030 | get_random_bytes(felica + 2, 6); |
| 1031 | |
| 1032 | /* NFCID3 */ |
Johannes Berg | aa9f979 | 2017-06-13 14:28:18 +0200 | [diff] [blame] | 1033 | nfcid3 = skb_put_zero(skb, 10); |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1034 | memcpy(nfcid3, felica, 8); |
Samuel Ortiz | 51d9e80 | 2012-05-30 01:48:46 +0200 | [diff] [blame] | 1035 | |
| 1036 | /* General bytes */ |
Johannes Berg | 634fef6 | 2017-06-16 14:29:24 +0200 | [diff] [blame] | 1037 | skb_put_u8(skb, gbytes_len); |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1038 | |
yuan linyu | b952f4d | 2017-06-18 22:52:04 +0800 | [diff] [blame] | 1039 | skb_put_data(skb, gbytes, gbytes_len); |
Samuel Ortiz | 51d9e80 | 2012-05-30 01:48:46 +0200 | [diff] [blame] | 1040 | |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 1041 | /* Len Tk */ |
Johannes Berg | 634fef6 | 2017-06-16 14:29:24 +0200 | [diff] [blame] | 1042 | skb_put_u8(skb, 0); |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 1043 | |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1044 | return skb; |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 1045 | } |
| 1046 | |
Olivier Guiter | 3c13b24 | 2013-09-23 12:24:38 +0200 | [diff] [blame] | 1047 | static void pn533_wq_tm_mi_recv(struct work_struct *work); |
| 1048 | static struct sk_buff *pn533_build_response(struct pn533 *dev); |
| 1049 | |
Samuel Ortiz | 103b34c | 2012-05-31 00:07:51 +0200 | [diff] [blame] | 1050 | static int pn533_tm_get_data_complete(struct pn533 *dev, void *arg, |
Waldemar Rymarkiewicz | e487882 | 2012-12-10 14:42:44 +0100 | [diff] [blame] | 1051 | struct sk_buff *resp) |
Samuel Ortiz | 103b34c | 2012-05-31 00:07:51 +0200 | [diff] [blame] | 1052 | { |
Olivier Guiter | 3c13b24 | 2013-09-23 12:24:38 +0200 | [diff] [blame] | 1053 | struct sk_buff *skb; |
| 1054 | u8 status, ret, mi; |
| 1055 | int rc; |
Samuel Ortiz | 103b34c | 2012-05-31 00:07:51 +0200 | [diff] [blame] | 1056 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 1057 | dev_dbg(dev->dev, "%s\n", __func__); |
Samuel Ortiz | 103b34c | 2012-05-31 00:07:51 +0200 | [diff] [blame] | 1058 | |
Olivier Guiter | 3c13b24 | 2013-09-23 12:24:38 +0200 | [diff] [blame] | 1059 | if (IS_ERR(resp)) { |
| 1060 | skb_queue_purge(&dev->resp_q); |
Waldemar Rymarkiewicz | e487882 | 2012-12-10 14:42:44 +0100 | [diff] [blame] | 1061 | return PTR_ERR(resp); |
Samuel Ortiz | 103b34c | 2012-05-31 00:07:51 +0200 | [diff] [blame] | 1062 | } |
| 1063 | |
Olivier Guiter | 3c13b24 | 2013-09-23 12:24:38 +0200 | [diff] [blame] | 1064 | status = resp->data[0]; |
| 1065 | |
| 1066 | ret = status & PN533_CMD_RET_MASK; |
| 1067 | mi = status & PN533_CMD_MI_MASK; |
| 1068 | |
| 1069 | skb_pull(resp, sizeof(status)); |
| 1070 | |
| 1071 | if (ret != PN533_CMD_RET_SUCCESS) { |
| 1072 | rc = -EIO; |
| 1073 | goto error; |
| 1074 | } |
| 1075 | |
| 1076 | skb_queue_tail(&dev->resp_q, resp); |
| 1077 | |
| 1078 | if (mi) { |
| 1079 | queue_work(dev->wq, &dev->mi_tm_rx_work); |
| 1080 | return -EINPROGRESS; |
| 1081 | } |
| 1082 | |
| 1083 | skb = pn533_build_response(dev); |
| 1084 | if (!skb) { |
| 1085 | rc = -EIO; |
| 1086 | goto error; |
| 1087 | } |
| 1088 | |
| 1089 | return nfc_tm_data_received(dev->nfc_dev, skb); |
| 1090 | |
| 1091 | error: |
| 1092 | nfc_tm_deactivated(dev->nfc_dev); |
| 1093 | dev->tgt_mode = 0; |
| 1094 | skb_queue_purge(&dev->resp_q); |
| 1095 | dev_kfree_skb(resp); |
| 1096 | |
| 1097 | return rc; |
| 1098 | } |
| 1099 | |
| 1100 | static void pn533_wq_tm_mi_recv(struct work_struct *work) |
| 1101 | { |
| 1102 | struct pn533 *dev = container_of(work, struct pn533, mi_tm_rx_work); |
| 1103 | struct sk_buff *skb; |
| 1104 | int rc; |
| 1105 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 1106 | dev_dbg(dev->dev, "%s\n", __func__); |
Olivier Guiter | 3c13b24 | 2013-09-23 12:24:38 +0200 | [diff] [blame] | 1107 | |
| 1108 | skb = pn533_alloc_skb(dev, 0); |
| 1109 | if (!skb) |
| 1110 | return; |
| 1111 | |
| 1112 | rc = pn533_send_cmd_direct_async(dev, |
| 1113 | PN533_CMD_TG_GET_DATA, |
| 1114 | skb, |
| 1115 | pn533_tm_get_data_complete, |
| 1116 | NULL); |
| 1117 | |
| 1118 | if (rc < 0) |
| 1119 | dev_kfree_skb(skb); |
Samuel Ortiz | 103b34c | 2012-05-31 00:07:51 +0200 | [diff] [blame] | 1120 | } |
| 1121 | |
Olivier Guiter | 93ad420 | 2013-09-23 12:24:39 +0200 | [diff] [blame] | 1122 | static int pn533_tm_send_complete(struct pn533 *dev, void *arg, |
| 1123 | struct sk_buff *resp); |
| 1124 | static void pn533_wq_tm_mi_send(struct work_struct *work) |
| 1125 | { |
| 1126 | struct pn533 *dev = container_of(work, struct pn533, mi_tm_tx_work); |
| 1127 | struct sk_buff *skb; |
| 1128 | int rc; |
| 1129 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 1130 | dev_dbg(dev->dev, "%s\n", __func__); |
Olivier Guiter | 93ad420 | 2013-09-23 12:24:39 +0200 | [diff] [blame] | 1131 | |
| 1132 | /* Grab the first skb in the queue */ |
| 1133 | skb = skb_dequeue(&dev->fragment_skb); |
| 1134 | if (skb == NULL) { /* No more data */ |
| 1135 | /* Reset the queue for future use */ |
| 1136 | skb_queue_head_init(&dev->fragment_skb); |
| 1137 | goto error; |
| 1138 | } |
| 1139 | |
| 1140 | /* last entry - remove MI bit */ |
| 1141 | if (skb_queue_len(&dev->fragment_skb) == 0) { |
| 1142 | rc = pn533_send_cmd_direct_async(dev, PN533_CMD_TG_SET_DATA, |
| 1143 | skb, pn533_tm_send_complete, NULL); |
| 1144 | } else |
| 1145 | rc = pn533_send_cmd_direct_async(dev, |
| 1146 | PN533_CMD_TG_SET_META_DATA, |
| 1147 | skb, pn533_tm_send_complete, NULL); |
| 1148 | |
| 1149 | if (rc == 0) /* success */ |
| 1150 | return; |
| 1151 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 1152 | dev_err(dev->dev, |
Olivier Guiter | 93ad420 | 2013-09-23 12:24:39 +0200 | [diff] [blame] | 1153 | "Error %d when trying to perform set meta data_exchange", rc); |
| 1154 | |
| 1155 | dev_kfree_skb(skb); |
| 1156 | |
| 1157 | error: |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 1158 | dev->phy_ops->send_ack(dev, GFP_KERNEL); |
Olivier Guiter | 93ad420 | 2013-09-23 12:24:39 +0200 | [diff] [blame] | 1159 | queue_work(dev->wq, &dev->cmd_work); |
| 1160 | } |
| 1161 | |
Samuel Ortiz | 103b34c | 2012-05-31 00:07:51 +0200 | [diff] [blame] | 1162 | static void pn533_wq_tg_get_data(struct work_struct *work) |
| 1163 | { |
| 1164 | struct pn533 *dev = container_of(work, struct pn533, tg_work); |
Waldemar Rymarkiewicz | e487882 | 2012-12-10 14:42:44 +0100 | [diff] [blame] | 1165 | struct sk_buff *skb; |
| 1166 | int rc; |
Samuel Ortiz | 103b34c | 2012-05-31 00:07:51 +0200 | [diff] [blame] | 1167 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 1168 | dev_dbg(dev->dev, "%s\n", __func__); |
Samuel Ortiz | 103b34c | 2012-05-31 00:07:51 +0200 | [diff] [blame] | 1169 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 1170 | skb = pn533_alloc_skb(dev, 0); |
Waldemar Rymarkiewicz | e487882 | 2012-12-10 14:42:44 +0100 | [diff] [blame] | 1171 | if (!skb) |
Samuel Ortiz | 103b34c | 2012-05-31 00:07:51 +0200 | [diff] [blame] | 1172 | return; |
| 1173 | |
Waldemar Rymarkiewicz | e487882 | 2012-12-10 14:42:44 +0100 | [diff] [blame] | 1174 | rc = pn533_send_data_async(dev, PN533_CMD_TG_GET_DATA, skb, |
| 1175 | pn533_tm_get_data_complete, NULL); |
Samuel Ortiz | 103b34c | 2012-05-31 00:07:51 +0200 | [diff] [blame] | 1176 | |
Waldemar Rymarkiewicz | e487882 | 2012-12-10 14:42:44 +0100 | [diff] [blame] | 1177 | if (rc < 0) |
| 1178 | dev_kfree_skb(skb); |
Samuel Ortiz | 103b34c | 2012-05-31 00:07:51 +0200 | [diff] [blame] | 1179 | } |
| 1180 | |
Samuel Ortiz | fc40a8c | 2012-06-01 13:21:13 +0200 | [diff] [blame] | 1181 | #define ATR_REQ_GB_OFFSET 17 |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1182 | static int pn533_init_target_complete(struct pn533 *dev, struct sk_buff *resp) |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 1183 | { |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1184 | u8 mode, *cmd, comm_mode = NFC_COMM_PASSIVE, *gb; |
Samuel Ortiz | fc40a8c | 2012-06-01 13:21:13 +0200 | [diff] [blame] | 1185 | size_t gb_len; |
Samuel Ortiz | 103b34c | 2012-05-31 00:07:51 +0200 | [diff] [blame] | 1186 | int rc; |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 1187 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 1188 | dev_dbg(dev->dev, "%s\n", __func__); |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 1189 | |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1190 | if (resp->len < ATR_REQ_GB_OFFSET + 1) |
Samuel Ortiz | fc40a8c | 2012-06-01 13:21:13 +0200 | [diff] [blame] | 1191 | return -EINVAL; |
| 1192 | |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1193 | mode = resp->data[0]; |
| 1194 | cmd = &resp->data[1]; |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 1195 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 1196 | dev_dbg(dev->dev, "Target mode 0x%x len %d\n", |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 1197 | mode, resp->len); |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 1198 | |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1199 | if ((mode & PN533_INIT_TARGET_RESP_FRAME_MASK) == |
| 1200 | PN533_INIT_TARGET_RESP_ACTIVE) |
Samuel Ortiz | fc40a8c | 2012-06-01 13:21:13 +0200 | [diff] [blame] | 1201 | comm_mode = NFC_COMM_ACTIVE; |
| 1202 | |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1203 | if ((mode & PN533_INIT_TARGET_RESP_DEP) == 0) /* Only DEP supported */ |
Samuel Ortiz | fc40a8c | 2012-06-01 13:21:13 +0200 | [diff] [blame] | 1204 | return -EOPNOTSUPP; |
| 1205 | |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1206 | gb = cmd + ATR_REQ_GB_OFFSET; |
| 1207 | gb_len = resp->len - (ATR_REQ_GB_OFFSET + 1); |
Samuel Ortiz | fc40a8c | 2012-06-01 13:21:13 +0200 | [diff] [blame] | 1208 | |
Samuel Ortiz | 103b34c | 2012-05-31 00:07:51 +0200 | [diff] [blame] | 1209 | rc = nfc_tm_activated(dev->nfc_dev, NFC_PROTO_NFC_DEP_MASK, |
| 1210 | comm_mode, gb, gb_len); |
| 1211 | if (rc < 0) { |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 1212 | nfc_err(dev->dev, |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 1213 | "Error when signaling target activation\n"); |
Samuel Ortiz | 103b34c | 2012-05-31 00:07:51 +0200 | [diff] [blame] | 1214 | return rc; |
| 1215 | } |
| 1216 | |
Samuel Ortiz | 51ad304 | 2012-05-31 20:01:32 +0200 | [diff] [blame] | 1217 | dev->tgt_mode = 1; |
Samuel Ortiz | 103b34c | 2012-05-31 00:07:51 +0200 | [diff] [blame] | 1218 | queue_work(dev->wq, &dev->tg_work); |
| 1219 | |
| 1220 | return 0; |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 1221 | } |
| 1222 | |
Kees Cook | e99e88a | 2017-10-16 14:43:17 -0700 | [diff] [blame] | 1223 | static void pn533_listen_mode_timer(struct timer_list *t) |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 1224 | { |
Kees Cook | e99e88a | 2017-10-16 14:43:17 -0700 | [diff] [blame] | 1225 | struct pn533 *dev = from_timer(dev, t, listen_timer); |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1226 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 1227 | dev_dbg(dev->dev, "Listen mode timeout\n"); |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1228 | |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1229 | dev->cancel_listen = 1; |
| 1230 | |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1231 | pn533_poll_next_mod(dev); |
| 1232 | |
Samuel Ortiz | 46f793b | 2013-07-03 14:50:36 +0200 | [diff] [blame] | 1233 | queue_delayed_work(dev->wq, &dev->poll_work, |
| 1234 | msecs_to_jiffies(PN533_POLL_INTERVAL)); |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1235 | } |
| 1236 | |
Samuel Ortiz | 17e9d9d | 2013-06-26 18:16:21 +0200 | [diff] [blame] | 1237 | static int pn533_rf_complete(struct pn533 *dev, void *arg, |
| 1238 | struct sk_buff *resp) |
| 1239 | { |
| 1240 | int rc = 0; |
| 1241 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 1242 | dev_dbg(dev->dev, "%s\n", __func__); |
Samuel Ortiz | 17e9d9d | 2013-06-26 18:16:21 +0200 | [diff] [blame] | 1243 | |
| 1244 | if (IS_ERR(resp)) { |
| 1245 | rc = PTR_ERR(resp); |
| 1246 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 1247 | nfc_err(dev->dev, "RF setting error %d\n", rc); |
Samuel Ortiz | 17e9d9d | 2013-06-26 18:16:21 +0200 | [diff] [blame] | 1248 | |
| 1249 | return rc; |
| 1250 | } |
| 1251 | |
Samuel Ortiz | 46f793b | 2013-07-03 14:50:36 +0200 | [diff] [blame] | 1252 | queue_delayed_work(dev->wq, &dev->poll_work, |
| 1253 | msecs_to_jiffies(PN533_POLL_INTERVAL)); |
Samuel Ortiz | 17e9d9d | 2013-06-26 18:16:21 +0200 | [diff] [blame] | 1254 | |
| 1255 | dev_kfree_skb(resp); |
| 1256 | return rc; |
| 1257 | } |
| 1258 | |
| 1259 | static void pn533_wq_rf(struct work_struct *work) |
| 1260 | { |
| 1261 | struct pn533 *dev = container_of(work, struct pn533, rf_work); |
| 1262 | struct sk_buff *skb; |
| 1263 | int rc; |
| 1264 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 1265 | dev_dbg(dev->dev, "%s\n", __func__); |
Samuel Ortiz | 17e9d9d | 2013-06-26 18:16:21 +0200 | [diff] [blame] | 1266 | |
| 1267 | skb = pn533_alloc_skb(dev, 2); |
| 1268 | if (!skb) |
| 1269 | return; |
| 1270 | |
Johannes Berg | 634fef6 | 2017-06-16 14:29:24 +0200 | [diff] [blame] | 1271 | skb_put_u8(skb, PN533_CFGITEM_RF_FIELD); |
| 1272 | skb_put_u8(skb, PN533_CFGITEM_RF_FIELD_AUTO_RFCA); |
Samuel Ortiz | 17e9d9d | 2013-06-26 18:16:21 +0200 | [diff] [blame] | 1273 | |
| 1274 | rc = pn533_send_cmd_async(dev, PN533_CMD_RF_CONFIGURATION, skb, |
| 1275 | pn533_rf_complete, NULL); |
| 1276 | if (rc < 0) { |
| 1277 | dev_kfree_skb(skb); |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 1278 | nfc_err(dev->dev, "RF setting error %d\n", rc); |
Samuel Ortiz | 17e9d9d | 2013-06-26 18:16:21 +0200 | [diff] [blame] | 1279 | } |
Samuel Ortiz | 17e9d9d | 2013-06-26 18:16:21 +0200 | [diff] [blame] | 1280 | } |
| 1281 | |
Samuel Ortiz | 673088f | 2013-08-21 15:06:55 +0200 | [diff] [blame] | 1282 | static int pn533_poll_dep_complete(struct pn533 *dev, void *arg, |
| 1283 | struct sk_buff *resp) |
| 1284 | { |
| 1285 | struct pn533_cmd_jump_dep_response *rsp; |
| 1286 | struct nfc_target nfc_target; |
| 1287 | u8 target_gt_len; |
| 1288 | int rc; |
| 1289 | |
| 1290 | if (IS_ERR(resp)) |
| 1291 | return PTR_ERR(resp); |
| 1292 | |
| 1293 | rsp = (struct pn533_cmd_jump_dep_response *)resp->data; |
| 1294 | |
| 1295 | rc = rsp->status & PN533_CMD_RET_MASK; |
| 1296 | if (rc != PN533_CMD_RET_SUCCESS) { |
| 1297 | /* Not target found, turn radio off */ |
| 1298 | queue_work(dev->wq, &dev->rf_work); |
| 1299 | |
| 1300 | dev_kfree_skb(resp); |
| 1301 | return 0; |
| 1302 | } |
| 1303 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 1304 | dev_dbg(dev->dev, "Creating new target"); |
Samuel Ortiz | 673088f | 2013-08-21 15:06:55 +0200 | [diff] [blame] | 1305 | |
| 1306 | nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK; |
| 1307 | nfc_target.nfcid1_len = 10; |
| 1308 | memcpy(nfc_target.nfcid1, rsp->nfcid3t, nfc_target.nfcid1_len); |
| 1309 | rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1); |
| 1310 | if (rc) |
| 1311 | goto error; |
| 1312 | |
| 1313 | dev->tgt_available_prots = 0; |
| 1314 | dev->tgt_active_prot = NFC_PROTO_NFC_DEP; |
| 1315 | |
| 1316 | /* ATR_RES general bytes are located at offset 17 */ |
| 1317 | target_gt_len = resp->len - 17; |
| 1318 | rc = nfc_set_remote_general_bytes(dev->nfc_dev, |
| 1319 | rsp->gt, target_gt_len); |
| 1320 | if (!rc) { |
| 1321 | rc = nfc_dep_link_is_up(dev->nfc_dev, |
| 1322 | dev->nfc_dev->targets[0].idx, |
| 1323 | 0, NFC_RF_INITIATOR); |
| 1324 | |
| 1325 | if (!rc) |
| 1326 | pn533_poll_reset_mod_list(dev); |
| 1327 | } |
| 1328 | error: |
| 1329 | dev_kfree_skb(resp); |
| 1330 | return rc; |
| 1331 | } |
| 1332 | |
| 1333 | #define PASSIVE_DATA_LEN 5 |
| 1334 | static int pn533_poll_dep(struct nfc_dev *nfc_dev) |
| 1335 | { |
| 1336 | struct pn533 *dev = nfc_get_drvdata(nfc_dev); |
| 1337 | struct sk_buff *skb; |
| 1338 | int rc, skb_len; |
| 1339 | u8 *next, nfcid3[NFC_NFCID3_MAXSIZE]; |
| 1340 | u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3}; |
| 1341 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 1342 | dev_dbg(dev->dev, "%s", __func__); |
Samuel Ortiz | 673088f | 2013-08-21 15:06:55 +0200 | [diff] [blame] | 1343 | |
| 1344 | if (!dev->gb) { |
| 1345 | dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len); |
| 1346 | |
| 1347 | if (!dev->gb || !dev->gb_len) { |
| 1348 | dev->poll_dep = 0; |
| 1349 | queue_work(dev->wq, &dev->rf_work); |
| 1350 | } |
| 1351 | } |
| 1352 | |
| 1353 | skb_len = 3 + dev->gb_len; /* ActPass + BR + Next */ |
| 1354 | skb_len += PASSIVE_DATA_LEN; |
| 1355 | |
| 1356 | /* NFCID3 */ |
| 1357 | skb_len += NFC_NFCID3_MAXSIZE; |
| 1358 | nfcid3[0] = 0x1; |
| 1359 | nfcid3[1] = 0xfe; |
| 1360 | get_random_bytes(nfcid3 + 2, 6); |
| 1361 | |
| 1362 | skb = pn533_alloc_skb(dev, skb_len); |
| 1363 | if (!skb) |
| 1364 | return -ENOMEM; |
| 1365 | |
Johannes Berg | 634fef6 | 2017-06-16 14:29:24 +0200 | [diff] [blame] | 1366 | skb_put_u8(skb, 0x01); /* Active */ |
| 1367 | skb_put_u8(skb, 0x02); /* 424 kbps */ |
Samuel Ortiz | 673088f | 2013-08-21 15:06:55 +0200 | [diff] [blame] | 1368 | |
| 1369 | next = skb_put(skb, 1); /* Next */ |
| 1370 | *next = 0; |
| 1371 | |
| 1372 | /* Copy passive data */ |
Johannes Berg | 59ae1d1 | 2017-06-16 14:29:20 +0200 | [diff] [blame] | 1373 | skb_put_data(skb, passive_data, PASSIVE_DATA_LEN); |
Samuel Ortiz | 673088f | 2013-08-21 15:06:55 +0200 | [diff] [blame] | 1374 | *next |= 1; |
| 1375 | |
| 1376 | /* Copy NFCID3 (which is NFCID2 from SENSF_RES) */ |
Johannes Berg | 59ae1d1 | 2017-06-16 14:29:20 +0200 | [diff] [blame] | 1377 | skb_put_data(skb, nfcid3, NFC_NFCID3_MAXSIZE); |
Samuel Ortiz | 673088f | 2013-08-21 15:06:55 +0200 | [diff] [blame] | 1378 | *next |= 2; |
| 1379 | |
Johannes Berg | 59ae1d1 | 2017-06-16 14:29:20 +0200 | [diff] [blame] | 1380 | skb_put_data(skb, dev->gb, dev->gb_len); |
Samuel Ortiz | 673088f | 2013-08-21 15:06:55 +0200 | [diff] [blame] | 1381 | *next |= 4; /* We have some Gi */ |
| 1382 | |
| 1383 | rc = pn533_send_cmd_async(dev, PN533_CMD_IN_JUMP_FOR_DEP, skb, |
| 1384 | pn533_poll_dep_complete, NULL); |
| 1385 | |
| 1386 | if (rc < 0) |
| 1387 | dev_kfree_skb(skb); |
| 1388 | |
| 1389 | return rc; |
| 1390 | } |
| 1391 | |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1392 | static int pn533_poll_complete(struct pn533 *dev, void *arg, |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1393 | struct sk_buff *resp) |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1394 | { |
| 1395 | struct pn533_poll_modulations *cur_mod; |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 1396 | int rc; |
| 1397 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 1398 | dev_dbg(dev->dev, "%s\n", __func__); |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1399 | |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1400 | if (IS_ERR(resp)) { |
| 1401 | rc = PTR_ERR(resp); |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1402 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 1403 | nfc_err(dev->dev, "%s Poll complete error %d\n", |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 1404 | __func__, rc); |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1405 | |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1406 | if (rc == -ENOENT) { |
| 1407 | if (dev->poll_mod_count != 0) |
| 1408 | return rc; |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 1409 | goto stop_poll; |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1410 | } else if (rc < 0) { |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 1411 | nfc_err(dev->dev, |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 1412 | "Error %d when running poll\n", rc); |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1413 | goto stop_poll; |
| 1414 | } |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1415 | } |
| 1416 | |
| 1417 | cur_mod = dev->poll_mod_active[dev->poll_mod_curr]; |
| 1418 | |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1419 | if (cur_mod->len == 0) { /* Target mode */ |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1420 | del_timer(&dev->listen_timer); |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1421 | rc = pn533_init_target_complete(dev, resp); |
| 1422 | goto done; |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1423 | } |
| 1424 | |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1425 | /* Initiator mode */ |
| 1426 | rc = pn533_start_poll_complete(dev, resp); |
| 1427 | if (!rc) |
| 1428 | goto done; |
| 1429 | |
Waldemar Rymarkiewicz | 95cb9e1 | 2013-04-03 08:02:00 +0200 | [diff] [blame] | 1430 | if (!dev->poll_mod_count) { |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 1431 | dev_dbg(dev->dev, "Polling has been stopped\n"); |
Waldemar Rymarkiewicz | 95cb9e1 | 2013-04-03 08:02:00 +0200 | [diff] [blame] | 1432 | goto done; |
| 1433 | } |
| 1434 | |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1435 | pn533_poll_next_mod(dev); |
Samuel Ortiz | 17e9d9d | 2013-06-26 18:16:21 +0200 | [diff] [blame] | 1436 | /* Not target found, turn radio off */ |
| 1437 | queue_work(dev->wq, &dev->rf_work); |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1438 | |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1439 | done: |
| 1440 | dev_kfree_skb(resp); |
| 1441 | return rc; |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1442 | |
| 1443 | stop_poll: |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 1444 | nfc_err(dev->dev, "Polling operation has been stopped\n"); |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1445 | |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 1446 | pn533_poll_reset_mod_list(dev); |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1447 | dev->poll_protocols = 0; |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1448 | return rc; |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1449 | } |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 1450 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 1451 | static struct sk_buff *pn533_alloc_poll_in_frame(struct pn533 *dev, |
| 1452 | struct pn533_poll_modulations *mod) |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1453 | { |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1454 | struct sk_buff *skb; |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 1455 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 1456 | skb = pn533_alloc_skb(dev, mod->len); |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1457 | if (!skb) |
| 1458 | return NULL; |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1459 | |
Johannes Berg | 59ae1d1 | 2017-06-16 14:29:20 +0200 | [diff] [blame] | 1460 | skb_put_data(skb, &mod->data, mod->len); |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1461 | |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1462 | return skb; |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1463 | } |
| 1464 | |
| 1465 | static int pn533_send_poll_frame(struct pn533 *dev) |
| 1466 | { |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1467 | struct pn533_poll_modulations *mod; |
| 1468 | struct sk_buff *skb; |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1469 | int rc; |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1470 | u8 cmd_code; |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1471 | |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1472 | mod = dev->poll_mod_active[dev->poll_mod_curr]; |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1473 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 1474 | dev_dbg(dev->dev, "%s mod len %d\n", |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 1475 | __func__, mod->len); |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 1476 | |
Michael Thalmeier | e997ebbe46 | 2016-03-25 15:46:51 +0100 | [diff] [blame] | 1477 | if ((dev->poll_protocols & NFC_PROTO_NFC_DEP_MASK) && dev->poll_dep) { |
Samuel Ortiz | 673088f | 2013-08-21 15:06:55 +0200 | [diff] [blame] | 1478 | dev->poll_dep = 0; |
| 1479 | return pn533_poll_dep(dev->nfc_dev); |
| 1480 | } |
| 1481 | |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1482 | if (mod->len == 0) { /* Listen mode */ |
| 1483 | cmd_code = PN533_CMD_TG_INIT_AS_TARGET; |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 1484 | skb = pn533_alloc_poll_tg_frame(dev); |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1485 | } else { /* Polling mode */ |
| 1486 | cmd_code = PN533_CMD_IN_LIST_PASSIVE_TARGET; |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 1487 | skb = pn533_alloc_poll_in_frame(dev, mod); |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1488 | } |
| 1489 | |
| 1490 | if (!skb) { |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 1491 | nfc_err(dev->dev, "Failed to allocate skb\n"); |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1492 | return -ENOMEM; |
| 1493 | } |
| 1494 | |
| 1495 | rc = pn533_send_cmd_async(dev, cmd_code, skb, pn533_poll_complete, |
| 1496 | NULL); |
| 1497 | if (rc < 0) { |
| 1498 | dev_kfree_skb(skb); |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 1499 | nfc_err(dev->dev, "Polling loop error %d\n", rc); |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1500 | } |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 1501 | |
| 1502 | return rc; |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 1503 | } |
| 1504 | |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1505 | static void pn533_wq_poll(struct work_struct *work) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1506 | { |
Samuel Ortiz | 46f793b | 2013-07-03 14:50:36 +0200 | [diff] [blame] | 1507 | struct pn533 *dev = container_of(work, struct pn533, poll_work.work); |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1508 | struct pn533_poll_modulations *cur_mod; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1509 | int rc; |
| 1510 | |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1511 | cur_mod = dev->poll_mod_active[dev->poll_mod_curr]; |
| 1512 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 1513 | dev_dbg(dev->dev, |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 1514 | "%s cancel_listen %d modulation len %d\n", |
| 1515 | __func__, dev->cancel_listen, cur_mod->len); |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1516 | |
| 1517 | if (dev->cancel_listen == 1) { |
| 1518 | dev->cancel_listen = 0; |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 1519 | dev->phy_ops->abort_cmd(dev, GFP_ATOMIC); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1520 | } |
| 1521 | |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1522 | rc = pn533_send_poll_frame(dev); |
| 1523 | if (rc) |
| 1524 | return; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1525 | |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1526 | if (cur_mod->len == 0 && dev->poll_mod_count > 1) |
| 1527 | mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1528 | } |
| 1529 | |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 1530 | static int pn533_start_poll(struct nfc_dev *nfc_dev, |
| 1531 | u32 im_protocols, u32 tm_protocols) |
| 1532 | { |
| 1533 | struct pn533 *dev = nfc_get_drvdata(nfc_dev); |
Samuel Ortiz | cec4b8e | 2013-08-21 15:12:06 +0200 | [diff] [blame] | 1534 | struct pn533_poll_modulations *cur_mod; |
Samuel Ortiz | dfccd0f | 2013-07-03 15:14:17 +0200 | [diff] [blame] | 1535 | u8 rand_mod; |
Samuel Ortiz | cec4b8e | 2013-08-21 15:12:06 +0200 | [diff] [blame] | 1536 | int rc; |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 1537 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 1538 | dev_dbg(dev->dev, |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 1539 | "%s: im protocols 0x%x tm protocols 0x%x\n", |
| 1540 | __func__, im_protocols, tm_protocols); |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 1541 | |
| 1542 | if (dev->tgt_active_prot) { |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 1543 | nfc_err(dev->dev, |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 1544 | "Cannot poll with a target already activated\n"); |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 1545 | return -EBUSY; |
| 1546 | } |
| 1547 | |
Samuel Ortiz | 51ad304 | 2012-05-31 20:01:32 +0200 | [diff] [blame] | 1548 | if (dev->tgt_mode) { |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 1549 | nfc_err(dev->dev, |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 1550 | "Cannot poll while already being activated\n"); |
Samuel Ortiz | 51ad304 | 2012-05-31 20:01:32 +0200 | [diff] [blame] | 1551 | return -EBUSY; |
| 1552 | } |
| 1553 | |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1554 | if (tm_protocols) { |
| 1555 | dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len); |
| 1556 | if (dev->gb == NULL) |
| 1557 | tm_protocols = 0; |
| 1558 | } |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 1559 | |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1560 | pn533_poll_create_mod_list(dev, im_protocols, tm_protocols); |
| 1561 | dev->poll_protocols = im_protocols; |
| 1562 | dev->listen_protocols = tm_protocols; |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 1563 | |
Samuel Ortiz | dfccd0f | 2013-07-03 15:14:17 +0200 | [diff] [blame] | 1564 | /* Do not always start polling from the same modulation */ |
| 1565 | get_random_bytes(&rand_mod, sizeof(rand_mod)); |
| 1566 | rand_mod %= dev->poll_mod_count; |
| 1567 | dev->poll_mod_curr = rand_mod; |
| 1568 | |
Samuel Ortiz | cec4b8e | 2013-08-21 15:12:06 +0200 | [diff] [blame] | 1569 | cur_mod = dev->poll_mod_active[dev->poll_mod_curr]; |
| 1570 | |
| 1571 | rc = pn533_send_poll_frame(dev); |
| 1572 | |
| 1573 | /* Start listen timer */ |
| 1574 | if (!rc && cur_mod->len == 0 && dev->poll_mod_count > 1) |
| 1575 | mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ); |
| 1576 | |
| 1577 | return rc; |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 1578 | } |
| 1579 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1580 | static void pn533_stop_poll(struct nfc_dev *nfc_dev) |
| 1581 | { |
| 1582 | struct pn533 *dev = nfc_get_drvdata(nfc_dev); |
| 1583 | |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1584 | del_timer(&dev->listen_timer); |
| 1585 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1586 | if (!dev->poll_mod_count) { |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 1587 | dev_dbg(dev->dev, |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 1588 | "Polling operation was not running\n"); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1589 | return; |
| 1590 | } |
| 1591 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 1592 | dev->phy_ops->abort_cmd(dev, GFP_KERNEL); |
Samuel Ortiz | 46f793b | 2013-07-03 14:50:36 +0200 | [diff] [blame] | 1593 | flush_delayed_work(&dev->poll_work); |
Samuel Ortiz | 7c2a04a93 | 2012-05-21 16:20:01 +0200 | [diff] [blame] | 1594 | pn533_poll_reset_mod_list(dev); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1595 | } |
| 1596 | |
| 1597 | static int pn533_activate_target_nfcdep(struct pn533 *dev) |
| 1598 | { |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 1599 | struct pn533_cmd_activate_response *rsp; |
Samuel Ortiz | 541d920 | 2011-12-14 16:43:10 +0100 | [diff] [blame] | 1600 | u16 gt_len; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1601 | int rc; |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 1602 | struct sk_buff *skb; |
| 1603 | struct sk_buff *resp; |
| 1604 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 1605 | dev_dbg(dev->dev, "%s\n", __func__); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1606 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 1607 | skb = pn533_alloc_skb(dev, sizeof(u8) * 2); /*TG + Next*/ |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 1608 | if (!skb) |
| 1609 | return -ENOMEM; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1610 | |
Johannes Berg | 634fef6 | 2017-06-16 14:29:24 +0200 | [diff] [blame] | 1611 | skb_put_u8(skb, 1); /* TG */ |
| 1612 | skb_put_u8(skb, 0); /* Next */ |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1613 | |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 1614 | resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_ATR, skb); |
| 1615 | if (IS_ERR(resp)) |
| 1616 | return PTR_ERR(resp); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1617 | |
Waldemar Rymarkiewicz | 37cf4fc | 2012-12-10 14:42:50 +0100 | [diff] [blame] | 1618 | rsp = (struct pn533_cmd_activate_response *)resp->data; |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 1619 | rc = rsp->status & PN533_CMD_RET_MASK; |
Waldemar Rymarkiewicz | 8a0ecfe | 2012-12-21 10:39:01 +0100 | [diff] [blame] | 1620 | if (rc != PN533_CMD_RET_SUCCESS) { |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 1621 | nfc_err(dev->dev, |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 1622 | "Target activation failed (error 0x%x)\n", rc); |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 1623 | dev_kfree_skb(resp); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1624 | return -EIO; |
Waldemar Rymarkiewicz | 8a0ecfe | 2012-12-21 10:39:01 +0100 | [diff] [blame] | 1625 | } |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1626 | |
Samuel Ortiz | 541d920 | 2011-12-14 16:43:10 +0100 | [diff] [blame] | 1627 | /* ATR_RES general bytes are located at offset 16 */ |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 1628 | gt_len = resp->len - 16; |
| 1629 | rc = nfc_set_remote_general_bytes(dev->nfc_dev, rsp->gt, gt_len); |
Samuel Ortiz | 541d920 | 2011-12-14 16:43:10 +0100 | [diff] [blame] | 1630 | |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 1631 | dev_kfree_skb(resp); |
Samuel Ortiz | 541d920 | 2011-12-14 16:43:10 +0100 | [diff] [blame] | 1632 | return rc; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1633 | } |
| 1634 | |
Eric Lapuyade | 9009943 | 2012-05-07 12:31:13 +0200 | [diff] [blame] | 1635 | static int pn533_activate_target(struct nfc_dev *nfc_dev, |
| 1636 | struct nfc_target *target, u32 protocol) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1637 | { |
| 1638 | struct pn533 *dev = nfc_get_drvdata(nfc_dev); |
| 1639 | int rc; |
| 1640 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 1641 | dev_dbg(dev->dev, "%s: protocol=%u\n", __func__, protocol); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1642 | |
| 1643 | if (dev->poll_mod_count) { |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 1644 | nfc_err(dev->dev, |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 1645 | "Cannot activate while polling\n"); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1646 | return -EBUSY; |
| 1647 | } |
| 1648 | |
| 1649 | if (dev->tgt_active_prot) { |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 1650 | nfc_err(dev->dev, |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 1651 | "There is already an active target\n"); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1652 | return -EBUSY; |
| 1653 | } |
| 1654 | |
| 1655 | if (!dev->tgt_available_prots) { |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 1656 | nfc_err(dev->dev, |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 1657 | "There is no available target to activate\n"); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1658 | return -EINVAL; |
| 1659 | } |
| 1660 | |
| 1661 | if (!(dev->tgt_available_prots & (1 << protocol))) { |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 1662 | nfc_err(dev->dev, |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 1663 | "Target doesn't support requested proto %u\n", |
| 1664 | protocol); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1665 | return -EINVAL; |
| 1666 | } |
| 1667 | |
| 1668 | if (protocol == NFC_PROTO_NFC_DEP) { |
| 1669 | rc = pn533_activate_target_nfcdep(dev); |
| 1670 | if (rc) { |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 1671 | nfc_err(dev->dev, |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 1672 | "Activating target with DEP failed %d\n", rc); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1673 | return rc; |
| 1674 | } |
| 1675 | } |
| 1676 | |
| 1677 | dev->tgt_active_prot = protocol; |
| 1678 | dev->tgt_available_prots = 0; |
| 1679 | |
| 1680 | return 0; |
| 1681 | } |
| 1682 | |
Michael Thalmeier | 37f895d7 | 2016-03-25 15:46:52 +0100 | [diff] [blame] | 1683 | static int pn533_deactivate_target_complete(struct pn533 *dev, void *arg, |
| 1684 | struct sk_buff *resp) |
| 1685 | { |
| 1686 | int rc = 0; |
| 1687 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 1688 | dev_dbg(dev->dev, "%s\n", __func__); |
Michael Thalmeier | 37f895d7 | 2016-03-25 15:46:52 +0100 | [diff] [blame] | 1689 | |
| 1690 | if (IS_ERR(resp)) { |
| 1691 | rc = PTR_ERR(resp); |
| 1692 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 1693 | nfc_err(dev->dev, "Target release error %d\n", rc); |
Michael Thalmeier | 37f895d7 | 2016-03-25 15:46:52 +0100 | [diff] [blame] | 1694 | |
| 1695 | return rc; |
| 1696 | } |
| 1697 | |
| 1698 | rc = resp->data[0] & PN533_CMD_RET_MASK; |
| 1699 | if (rc != PN533_CMD_RET_SUCCESS) |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 1700 | nfc_err(dev->dev, |
Michael Thalmeier | 37f895d7 | 2016-03-25 15:46:52 +0100 | [diff] [blame] | 1701 | "Error 0x%x when releasing the target\n", rc); |
| 1702 | |
| 1703 | dev_kfree_skb(resp); |
| 1704 | return rc; |
| 1705 | } |
| 1706 | |
Eric Lapuyade | 9009943 | 2012-05-07 12:31:13 +0200 | [diff] [blame] | 1707 | static void pn533_deactivate_target(struct nfc_dev *nfc_dev, |
Christophe Ricard | 96d4581 | 2015-10-25 22:54:43 +0100 | [diff] [blame] | 1708 | struct nfc_target *target, u8 mode) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1709 | { |
| 1710 | struct pn533 *dev = nfc_get_drvdata(nfc_dev); |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 1711 | struct sk_buff *skb; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1712 | int rc; |
| 1713 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 1714 | dev_dbg(dev->dev, "%s\n", __func__); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1715 | |
| 1716 | if (!dev->tgt_active_prot) { |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 1717 | nfc_err(dev->dev, "There is no active target\n"); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1718 | return; |
| 1719 | } |
| 1720 | |
| 1721 | dev->tgt_active_prot = 0; |
Samuel Ortiz | 6ff73fd | 2012-04-10 19:43:18 +0200 | [diff] [blame] | 1722 | skb_queue_purge(&dev->resp_q); |
| 1723 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 1724 | skb = pn533_alloc_skb(dev, sizeof(u8)); |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 1725 | if (!skb) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1726 | return; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1727 | |
Johannes Berg | 634fef6 | 2017-06-16 14:29:24 +0200 | [diff] [blame] | 1728 | skb_put_u8(skb, 1); /* TG*/ |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 1729 | |
Michael Thalmeier | 37f895d7 | 2016-03-25 15:46:52 +0100 | [diff] [blame] | 1730 | rc = pn533_send_cmd_async(dev, PN533_CMD_IN_RELEASE, skb, |
| 1731 | pn533_deactivate_target_complete, NULL); |
| 1732 | if (rc < 0) { |
| 1733 | dev_kfree_skb(skb); |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 1734 | nfc_err(dev->dev, "Target release error %d\n", rc); |
Michael Thalmeier | 37f895d7 | 2016-03-25 15:46:52 +0100 | [diff] [blame] | 1735 | } |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1736 | } |
| 1737 | |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 1738 | |
| 1739 | static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg, |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 1740 | struct sk_buff *resp) |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 1741 | { |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 1742 | struct pn533_cmd_jump_dep_response *rsp; |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 1743 | u8 target_gt_len; |
| 1744 | int rc; |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 1745 | u8 active = *(u8 *)arg; |
Waldemar Rymarkiewicz | 70418e6 | 2012-10-11 14:04:00 +0200 | [diff] [blame] | 1746 | |
| 1747 | kfree(arg); |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 1748 | |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 1749 | if (IS_ERR(resp)) |
| 1750 | return PTR_ERR(resp); |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 1751 | |
| 1752 | if (dev->tgt_available_prots && |
| 1753 | !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) { |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 1754 | nfc_err(dev->dev, |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 1755 | "The target does not support DEP\n"); |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 1756 | rc = -EINVAL; |
| 1757 | goto error; |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 1758 | } |
| 1759 | |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 1760 | rsp = (struct pn533_cmd_jump_dep_response *)resp->data; |
| 1761 | |
| 1762 | rc = rsp->status & PN533_CMD_RET_MASK; |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 1763 | if (rc != PN533_CMD_RET_SUCCESS) { |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 1764 | nfc_err(dev->dev, |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 1765 | "Bringing DEP link up failed (error 0x%x)\n", rc); |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 1766 | goto error; |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 1767 | } |
| 1768 | |
| 1769 | if (!dev->tgt_available_prots) { |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 1770 | struct nfc_target nfc_target; |
| 1771 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 1772 | dev_dbg(dev->dev, "Creating new target\n"); |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 1773 | |
| 1774 | nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK; |
Samuel Ortiz | 2fbabfa | 2012-03-05 01:03:47 +0100 | [diff] [blame] | 1775 | nfc_target.nfcid1_len = 10; |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 1776 | memcpy(nfc_target.nfcid1, rsp->nfcid3t, nfc_target.nfcid1_len); |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 1777 | rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1); |
| 1778 | if (rc) |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 1779 | goto error; |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 1780 | |
| 1781 | dev->tgt_available_prots = 0; |
| 1782 | } |
| 1783 | |
| 1784 | dev->tgt_active_prot = NFC_PROTO_NFC_DEP; |
| 1785 | |
| 1786 | /* ATR_RES general bytes are located at offset 17 */ |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 1787 | target_gt_len = resp->len - 17; |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 1788 | rc = nfc_set_remote_general_bytes(dev->nfc_dev, |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 1789 | rsp->gt, target_gt_len); |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 1790 | if (rc == 0) |
| 1791 | rc = nfc_dep_link_is_up(dev->nfc_dev, |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 1792 | dev->nfc_dev->targets[0].idx, |
| 1793 | !active, NFC_RF_INITIATOR); |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 1794 | |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 1795 | error: |
| 1796 | dev_kfree_skb(resp); |
| 1797 | return rc; |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 1798 | } |
| 1799 | |
Samuel Ortiz | 17e9d9d | 2013-06-26 18:16:21 +0200 | [diff] [blame] | 1800 | static int pn533_rf_field(struct nfc_dev *nfc_dev, u8 rf); |
Eric Lapuyade | 9009943 | 2012-05-07 12:31:13 +0200 | [diff] [blame] | 1801 | static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target, |
Waldemar Rymarkiewicz | 37cf4fc | 2012-12-10 14:42:50 +0100 | [diff] [blame] | 1802 | u8 comm_mode, u8 *gb, size_t gb_len) |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 1803 | { |
| 1804 | struct pn533 *dev = nfc_get_drvdata(nfc_dev); |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 1805 | struct sk_buff *skb; |
Samuel Ortiz | 5eef484 | 2013-07-01 10:58:12 +0200 | [diff] [blame] | 1806 | int rc, skb_len; |
| 1807 | u8 *next, *arg, nfcid3[NFC_NFCID3_MAXSIZE]; |
Samuel Ortiz | d7f3345 | 2012-05-29 21:45:21 +0200 | [diff] [blame] | 1808 | u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3}; |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 1809 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 1810 | dev_dbg(dev->dev, "%s\n", __func__); |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 1811 | |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 1812 | if (dev->poll_mod_count) { |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 1813 | nfc_err(dev->dev, |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 1814 | "Cannot bring the DEP link up while polling\n"); |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 1815 | return -EBUSY; |
| 1816 | } |
| 1817 | |
| 1818 | if (dev->tgt_active_prot) { |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 1819 | nfc_err(dev->dev, |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 1820 | "There is already an active target\n"); |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 1821 | return -EBUSY; |
| 1822 | } |
| 1823 | |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 1824 | skb_len = 3 + gb_len; /* ActPass + BR + Next */ |
Samuel Ortiz | 5eef484 | 2013-07-01 10:58:12 +0200 | [diff] [blame] | 1825 | skb_len += PASSIVE_DATA_LEN; |
Samuel Ortiz | d7f3345 | 2012-05-29 21:45:21 +0200 | [diff] [blame] | 1826 | |
Samuel Ortiz | 5eef484 | 2013-07-01 10:58:12 +0200 | [diff] [blame] | 1827 | /* NFCID3 */ |
| 1828 | skb_len += NFC_NFCID3_MAXSIZE; |
| 1829 | if (target && !target->nfcid2_len) { |
| 1830 | nfcid3[0] = 0x1; |
| 1831 | nfcid3[1] = 0xfe; |
| 1832 | get_random_bytes(nfcid3 + 2, 6); |
| 1833 | } |
Samuel Ortiz | 322bce9 | 2013-05-27 15:29:11 +0200 | [diff] [blame] | 1834 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 1835 | skb = pn533_alloc_skb(dev, skb_len); |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 1836 | if (!skb) |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 1837 | return -ENOMEM; |
| 1838 | |
Johannes Berg | 634fef6 | 2017-06-16 14:29:24 +0200 | [diff] [blame] | 1839 | skb_put_u8(skb, !comm_mode); /* ActPass */ |
| 1840 | skb_put_u8(skb, 0x02); /* 424 kbps */ |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 1841 | |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 1842 | next = skb_put(skb, 1); /* Next */ |
| 1843 | *next = 0; |
| 1844 | |
Samuel Ortiz | 5eef484 | 2013-07-01 10:58:12 +0200 | [diff] [blame] | 1845 | /* Copy passive data */ |
Johannes Berg | 59ae1d1 | 2017-06-16 14:29:20 +0200 | [diff] [blame] | 1846 | skb_put_data(skb, passive_data, PASSIVE_DATA_LEN); |
Samuel Ortiz | 5eef484 | 2013-07-01 10:58:12 +0200 | [diff] [blame] | 1847 | *next |= 1; |
Samuel Ortiz | d7f3345 | 2012-05-29 21:45:21 +0200 | [diff] [blame] | 1848 | |
Samuel Ortiz | 5eef484 | 2013-07-01 10:58:12 +0200 | [diff] [blame] | 1849 | /* Copy NFCID3 (which is NFCID2 from SENSF_RES) */ |
| 1850 | if (target && target->nfcid2_len) |
Samuel Ortiz | 322bce9 | 2013-05-27 15:29:11 +0200 | [diff] [blame] | 1851 | memcpy(skb_put(skb, NFC_NFCID3_MAXSIZE), target->nfcid2, |
| 1852 | target->nfcid2_len); |
Samuel Ortiz | 5eef484 | 2013-07-01 10:58:12 +0200 | [diff] [blame] | 1853 | else |
Johannes Berg | 59ae1d1 | 2017-06-16 14:29:20 +0200 | [diff] [blame] | 1854 | skb_put_data(skb, nfcid3, NFC_NFCID3_MAXSIZE); |
Samuel Ortiz | 5eef484 | 2013-07-01 10:58:12 +0200 | [diff] [blame] | 1855 | *next |= 2; |
Samuel Ortiz | 322bce9 | 2013-05-27 15:29:11 +0200 | [diff] [blame] | 1856 | |
Samuel Ortiz | 47807d3 | 2012-03-05 01:03:50 +0100 | [diff] [blame] | 1857 | if (gb != NULL && gb_len > 0) { |
Johannes Berg | 59ae1d1 | 2017-06-16 14:29:20 +0200 | [diff] [blame] | 1858 | skb_put_data(skb, gb, gb_len); |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 1859 | *next |= 4; /* We have some Gi */ |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 1860 | } else { |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 1861 | *next = 0; |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 1862 | } |
| 1863 | |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 1864 | arg = kmalloc(sizeof(*arg), GFP_KERNEL); |
| 1865 | if (!arg) { |
| 1866 | dev_kfree_skb(skb); |
| 1867 | return -ENOMEM; |
| 1868 | } |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 1869 | |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 1870 | *arg = !comm_mode; |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 1871 | |
Samuel Ortiz | 17e9d9d | 2013-06-26 18:16:21 +0200 | [diff] [blame] | 1872 | pn533_rf_field(dev->nfc_dev, 0); |
| 1873 | |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 1874 | rc = pn533_send_cmd_async(dev, PN533_CMD_IN_JUMP_FOR_DEP, skb, |
| 1875 | pn533_in_dep_link_up_complete, arg); |
| 1876 | |
| 1877 | if (rc < 0) { |
| 1878 | dev_kfree_skb(skb); |
| 1879 | kfree(arg); |
| 1880 | } |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 1881 | |
| 1882 | return rc; |
| 1883 | } |
| 1884 | |
| 1885 | static int pn533_dep_link_down(struct nfc_dev *nfc_dev) |
| 1886 | { |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1887 | struct pn533 *dev = nfc_get_drvdata(nfc_dev); |
| 1888 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 1889 | dev_dbg(dev->dev, "%s\n", __func__); |
Waldemar Rymarkiewicz | fcfafc7 | 2012-12-10 14:42:55 +0100 | [diff] [blame] | 1890 | |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1891 | pn533_poll_reset_mod_list(dev); |
| 1892 | |
Waldemar Rymarkiewicz | 10cff29 | 2013-04-03 08:02:14 +0200 | [diff] [blame] | 1893 | if (dev->tgt_mode || dev->tgt_active_prot) |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 1894 | dev->phy_ops->abort_cmd(dev, GFP_KERNEL); |
Samuel Ortiz | 51ad304 | 2012-05-31 20:01:32 +0200 | [diff] [blame] | 1895 | |
| 1896 | dev->tgt_active_prot = 0; |
| 1897 | dev->tgt_mode = 0; |
| 1898 | |
| 1899 | skb_queue_purge(&dev->resp_q); |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 1900 | |
| 1901 | return 0; |
| 1902 | } |
| 1903 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1904 | struct pn533_data_exchange_arg { |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1905 | data_exchange_cb_t cb; |
| 1906 | void *cb_context; |
| 1907 | }; |
| 1908 | |
Samuel Ortiz | 6ff73fd | 2012-04-10 19:43:18 +0200 | [diff] [blame] | 1909 | static struct sk_buff *pn533_build_response(struct pn533 *dev) |
| 1910 | { |
| 1911 | struct sk_buff *skb, *tmp, *t; |
| 1912 | unsigned int skb_len = 0, tmp_len = 0; |
| 1913 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 1914 | dev_dbg(dev->dev, "%s\n", __func__); |
Samuel Ortiz | 6ff73fd | 2012-04-10 19:43:18 +0200 | [diff] [blame] | 1915 | |
| 1916 | if (skb_queue_empty(&dev->resp_q)) |
| 1917 | return NULL; |
| 1918 | |
| 1919 | if (skb_queue_len(&dev->resp_q) == 1) { |
| 1920 | skb = skb_dequeue(&dev->resp_q); |
| 1921 | goto out; |
| 1922 | } |
| 1923 | |
| 1924 | skb_queue_walk_safe(&dev->resp_q, tmp, t) |
| 1925 | skb_len += tmp->len; |
| 1926 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 1927 | dev_dbg(dev->dev, "%s total length %d\n", |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 1928 | __func__, skb_len); |
Samuel Ortiz | 6ff73fd | 2012-04-10 19:43:18 +0200 | [diff] [blame] | 1929 | |
| 1930 | skb = alloc_skb(skb_len, GFP_KERNEL); |
| 1931 | if (skb == NULL) |
| 1932 | goto out; |
| 1933 | |
| 1934 | skb_put(skb, skb_len); |
| 1935 | |
| 1936 | skb_queue_walk_safe(&dev->resp_q, tmp, t) { |
| 1937 | memcpy(skb->data + tmp_len, tmp->data, tmp->len); |
| 1938 | tmp_len += tmp->len; |
| 1939 | } |
| 1940 | |
| 1941 | out: |
| 1942 | skb_queue_purge(&dev->resp_q); |
| 1943 | |
| 1944 | return skb; |
| 1945 | } |
| 1946 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1947 | static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg, |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 1948 | struct sk_buff *resp) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1949 | { |
| 1950 | struct pn533_data_exchange_arg *arg = _arg; |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 1951 | struct sk_buff *skb; |
| 1952 | int rc = 0; |
| 1953 | u8 status, ret, mi; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1954 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 1955 | dev_dbg(dev->dev, "%s\n", __func__); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1956 | |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 1957 | if (IS_ERR(resp)) { |
| 1958 | rc = PTR_ERR(resp); |
| 1959 | goto _error; |
| 1960 | } |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1961 | |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 1962 | status = resp->data[0]; |
| 1963 | ret = status & PN533_CMD_RET_MASK; |
| 1964 | mi = status & PN533_CMD_MI_MASK; |
| 1965 | |
| 1966 | skb_pull(resp, sizeof(status)); |
| 1967 | |
| 1968 | if (ret != PN533_CMD_RET_SUCCESS) { |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 1969 | nfc_err(dev->dev, |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 1970 | "Exchanging data failed (error 0x%x)\n", ret); |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 1971 | rc = -EIO; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1972 | goto error; |
| 1973 | } |
| 1974 | |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 1975 | skb_queue_tail(&dev->resp_q, resp); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1976 | |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 1977 | if (mi) { |
| 1978 | dev->cmd_complete_mi_arg = arg; |
Olivier Guiter | 963a82e | 2013-06-13 13:43:28 +0000 | [diff] [blame] | 1979 | queue_work(dev->wq, &dev->mi_rx_work); |
| 1980 | return -EINPROGRESS; |
| 1981 | } |
| 1982 | |
| 1983 | /* Prepare for the next round */ |
| 1984 | if (skb_queue_len(&dev->fragment_skb) > 0) { |
| 1985 | dev->cmd_complete_dep_arg = arg; |
| 1986 | queue_work(dev->wq, &dev->mi_tx_work); |
| 1987 | |
Samuel Ortiz | 6ff73fd | 2012-04-10 19:43:18 +0200 | [diff] [blame] | 1988 | return -EINPROGRESS; |
| 1989 | } |
| 1990 | |
| 1991 | skb = pn533_build_response(dev); |
Julia Lawall | 5df848f | 2015-04-05 14:06:36 +0200 | [diff] [blame] | 1992 | if (!skb) { |
| 1993 | rc = -ENOMEM; |
Samuel Ortiz | 6ff73fd | 2012-04-10 19:43:18 +0200 | [diff] [blame] | 1994 | goto error; |
Julia Lawall | 5df848f | 2015-04-05 14:06:36 +0200 | [diff] [blame] | 1995 | } |
Samuel Ortiz | 6ff73fd | 2012-04-10 19:43:18 +0200 | [diff] [blame] | 1996 | |
| 1997 | arg->cb(arg->cb_context, skb, 0); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1998 | kfree(arg); |
| 1999 | return 0; |
| 2000 | |
| 2001 | error: |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2002 | dev_kfree_skb(resp); |
| 2003 | _error: |
Samuel Ortiz | 6ff73fd | 2012-04-10 19:43:18 +0200 | [diff] [blame] | 2004 | skb_queue_purge(&dev->resp_q); |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2005 | arg->cb(arg->cb_context, NULL, rc); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2006 | kfree(arg); |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2007 | return rc; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2008 | } |
| 2009 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 2010 | /* |
| 2011 | * Receive an incoming pn533 frame. skb contains only header and payload. |
| 2012 | * If skb == NULL, it is a notification that the link below is dead. |
| 2013 | */ |
| 2014 | void pn533_recv_frame(struct pn533 *dev, struct sk_buff *skb, int status) |
| 2015 | { |
Michael Thalmeier | b31d510 | 2016-04-21 16:43:53 +0200 | [diff] [blame] | 2016 | if (!dev->cmd) |
| 2017 | goto sched_wq; |
| 2018 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 2019 | dev->cmd->status = status; |
| 2020 | |
Michael Thalmeier | b31d510 | 2016-04-21 16:43:53 +0200 | [diff] [blame] | 2021 | if (status != 0) { |
| 2022 | dev_dbg(dev->dev, "%s: Error received: %d\n", __func__, status); |
| 2023 | goto sched_wq; |
| 2024 | } |
| 2025 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 2026 | if (skb == NULL) { |
| 2027 | pr_err("NULL Frame -> link is dead\n"); |
| 2028 | goto sched_wq; |
| 2029 | } |
| 2030 | |
| 2031 | if (pn533_rx_frame_is_ack(skb->data)) { |
| 2032 | dev_dbg(dev->dev, "%s: Received ACK frame\n", __func__); |
| 2033 | dev_kfree_skb(skb); |
| 2034 | return; |
| 2035 | } |
| 2036 | |
| 2037 | print_hex_dump_debug("PN533 RX: ", DUMP_PREFIX_NONE, 16, 1, skb->data, |
| 2038 | dev->ops->rx_frame_size(skb->data), false); |
| 2039 | |
| 2040 | if (!dev->ops->rx_is_frame_valid(skb->data, dev)) { |
| 2041 | nfc_err(dev->dev, "Received an invalid frame\n"); |
| 2042 | dev->cmd->status = -EIO; |
| 2043 | } else if (!pn533_rx_frame_is_cmd_response(dev, skb->data)) { |
| 2044 | nfc_err(dev->dev, "It it not the response to the last command\n"); |
| 2045 | dev->cmd->status = -EIO; |
| 2046 | } |
| 2047 | |
| 2048 | dev->cmd->resp = skb; |
| 2049 | |
| 2050 | sched_wq: |
| 2051 | queue_work(dev->wq, &dev->cmd_complete_work); |
| 2052 | } |
| 2053 | EXPORT_SYMBOL(pn533_recv_frame); |
| 2054 | |
Olivier Guiter | 963a82e | 2013-06-13 13:43:28 +0000 | [diff] [blame] | 2055 | /* Split the Tx skb into small chunks */ |
| 2056 | static int pn533_fill_fragment_skbs(struct pn533 *dev, struct sk_buff *skb) |
| 2057 | { |
| 2058 | struct sk_buff *frag; |
| 2059 | int frag_size; |
| 2060 | |
| 2061 | do { |
| 2062 | /* Remaining size */ |
| 2063 | if (skb->len > PN533_CMD_DATAFRAME_MAXLEN) |
| 2064 | frag_size = PN533_CMD_DATAFRAME_MAXLEN; |
| 2065 | else |
| 2066 | frag_size = skb->len; |
| 2067 | |
| 2068 | /* Allocate and reserve */ |
| 2069 | frag = pn533_alloc_skb(dev, frag_size); |
| 2070 | if (!frag) { |
| 2071 | skb_queue_purge(&dev->fragment_skb); |
| 2072 | break; |
| 2073 | } |
| 2074 | |
Olivier Guiter | 22953f9 | 2013-09-23 12:24:37 +0200 | [diff] [blame] | 2075 | if (!dev->tgt_mode) { |
| 2076 | /* Reserve the TG/MI byte */ |
| 2077 | skb_reserve(frag, 1); |
Olivier Guiter | 963a82e | 2013-06-13 13:43:28 +0000 | [diff] [blame] | 2078 | |
Olivier Guiter | 22953f9 | 2013-09-23 12:24:37 +0200 | [diff] [blame] | 2079 | /* MI + TG */ |
| 2080 | if (frag_size == PN533_CMD_DATAFRAME_MAXLEN) |
Johannes Berg | d58ff35 | 2017-06-16 14:29:23 +0200 | [diff] [blame] | 2081 | *(u8 *)skb_push(frag, sizeof(u8)) = |
| 2082 | (PN533_CMD_MI_MASK | 1); |
Olivier Guiter | 22953f9 | 2013-09-23 12:24:37 +0200 | [diff] [blame] | 2083 | else |
Johannes Berg | d58ff35 | 2017-06-16 14:29:23 +0200 | [diff] [blame] | 2084 | *(u8 *)skb_push(frag, sizeof(u8)) = 1; /* TG */ |
Olivier Guiter | 22953f9 | 2013-09-23 12:24:37 +0200 | [diff] [blame] | 2085 | } |
Olivier Guiter | 963a82e | 2013-06-13 13:43:28 +0000 | [diff] [blame] | 2086 | |
Johannes Berg | 59ae1d1 | 2017-06-16 14:29:20 +0200 | [diff] [blame] | 2087 | skb_put_data(frag, skb->data, frag_size); |
Olivier Guiter | 963a82e | 2013-06-13 13:43:28 +0000 | [diff] [blame] | 2088 | |
| 2089 | /* Reduce the size of incoming buffer */ |
| 2090 | skb_pull(skb, frag_size); |
| 2091 | |
| 2092 | /* Add this to skb_queue */ |
| 2093 | skb_queue_tail(&dev->fragment_skb, frag); |
| 2094 | |
| 2095 | } while (skb->len > 0); |
| 2096 | |
| 2097 | dev_kfree_skb(skb); |
| 2098 | |
| 2099 | return skb_queue_len(&dev->fragment_skb); |
| 2100 | } |
| 2101 | |
Samuel Ortiz | be9ae4c | 2012-05-16 15:55:48 +0200 | [diff] [blame] | 2102 | static int pn533_transceive(struct nfc_dev *nfc_dev, |
| 2103 | struct nfc_target *target, struct sk_buff *skb, |
| 2104 | data_exchange_cb_t cb, void *cb_context) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2105 | { |
| 2106 | struct pn533 *dev = nfc_get_drvdata(nfc_dev); |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2107 | struct pn533_data_exchange_arg *arg = NULL; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2108 | int rc; |
| 2109 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 2110 | dev_dbg(dev->dev, "%s\n", __func__); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2111 | |
| 2112 | if (!dev->tgt_active_prot) { |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 2113 | nfc_err(dev->dev, |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 2114 | "Can't exchange data if there is no active target\n"); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2115 | rc = -EINVAL; |
| 2116 | goto error; |
| 2117 | } |
| 2118 | |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2119 | arg = kmalloc(sizeof(*arg), GFP_KERNEL); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2120 | if (!arg) { |
| 2121 | rc = -ENOMEM; |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2122 | goto error; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2123 | } |
| 2124 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2125 | arg->cb = cb; |
| 2126 | arg->cb_context = cb_context; |
| 2127 | |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2128 | switch (dev->device_type) { |
| 2129 | case PN533_DEVICE_PASORI: |
| 2130 | if (dev->tgt_active_prot == NFC_PROTO_FELICA) { |
| 2131 | rc = pn533_send_data_async(dev, PN533_CMD_IN_COMM_THRU, |
| 2132 | skb, |
| 2133 | pn533_data_exchange_complete, |
| 2134 | arg); |
| 2135 | |
| 2136 | break; |
| 2137 | } |
Gustavo A. R. Silva | 9fe0a75 | 2019-02-13 12:54:03 -0600 | [diff] [blame] | 2138 | /* fall through */ |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2139 | default: |
Olivier Guiter | 963a82e | 2013-06-13 13:43:28 +0000 | [diff] [blame] | 2140 | /* jumbo frame ? */ |
| 2141 | if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) { |
| 2142 | rc = pn533_fill_fragment_skbs(dev, skb); |
| 2143 | if (rc <= 0) |
| 2144 | goto error; |
| 2145 | |
| 2146 | skb = skb_dequeue(&dev->fragment_skb); |
| 2147 | if (!skb) { |
| 2148 | rc = -EIO; |
| 2149 | goto error; |
| 2150 | } |
| 2151 | } else { |
Johannes Berg | d58ff35 | 2017-06-16 14:29:23 +0200 | [diff] [blame] | 2152 | *(u8 *)skb_push(skb, sizeof(u8)) = 1; /* TG */ |
Olivier Guiter | 963a82e | 2013-06-13 13:43:28 +0000 | [diff] [blame] | 2153 | } |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2154 | |
| 2155 | rc = pn533_send_data_async(dev, PN533_CMD_IN_DATA_EXCHANGE, |
| 2156 | skb, pn533_data_exchange_complete, |
| 2157 | arg); |
| 2158 | |
| 2159 | break; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2160 | } |
| 2161 | |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2162 | if (rc < 0) /* rc from send_async */ |
| 2163 | goto error; |
| 2164 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2165 | return 0; |
| 2166 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2167 | error: |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2168 | kfree(arg); |
| 2169 | dev_kfree_skb(skb); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2170 | return rc; |
| 2171 | } |
| 2172 | |
Samuel Ortiz | dadb06f | 2012-05-31 00:09:11 +0200 | [diff] [blame] | 2173 | static int pn533_tm_send_complete(struct pn533 *dev, void *arg, |
Waldemar Rymarkiewicz | e487882 | 2012-12-10 14:42:44 +0100 | [diff] [blame] | 2174 | struct sk_buff *resp) |
Samuel Ortiz | dadb06f | 2012-05-31 00:09:11 +0200 | [diff] [blame] | 2175 | { |
Waldemar Rymarkiewicz | e487882 | 2012-12-10 14:42:44 +0100 | [diff] [blame] | 2176 | u8 status; |
Thierry Escande | 5b412fd | 2012-11-15 18:24:28 +0100 | [diff] [blame] | 2177 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 2178 | dev_dbg(dev->dev, "%s\n", __func__); |
Samuel Ortiz | dadb06f | 2012-05-31 00:09:11 +0200 | [diff] [blame] | 2179 | |
Waldemar Rymarkiewicz | e487882 | 2012-12-10 14:42:44 +0100 | [diff] [blame] | 2180 | if (IS_ERR(resp)) |
| 2181 | return PTR_ERR(resp); |
Thierry Escande | 5b412fd | 2012-11-15 18:24:28 +0100 | [diff] [blame] | 2182 | |
Waldemar Rymarkiewicz | e487882 | 2012-12-10 14:42:44 +0100 | [diff] [blame] | 2183 | status = resp->data[0]; |
Samuel Ortiz | dadb06f | 2012-05-31 00:09:11 +0200 | [diff] [blame] | 2184 | |
Olivier Guiter | 93ad420 | 2013-09-23 12:24:39 +0200 | [diff] [blame] | 2185 | /* Prepare for the next round */ |
| 2186 | if (skb_queue_len(&dev->fragment_skb) > 0) { |
| 2187 | queue_work(dev->wq, &dev->mi_tm_tx_work); |
| 2188 | return -EINPROGRESS; |
| 2189 | } |
Waldemar Rymarkiewicz | e487882 | 2012-12-10 14:42:44 +0100 | [diff] [blame] | 2190 | dev_kfree_skb(resp); |
Samuel Ortiz | dadb06f | 2012-05-31 00:09:11 +0200 | [diff] [blame] | 2191 | |
Waldemar Rymarkiewicz | e487882 | 2012-12-10 14:42:44 +0100 | [diff] [blame] | 2192 | if (status != 0) { |
Samuel Ortiz | dadb06f | 2012-05-31 00:09:11 +0200 | [diff] [blame] | 2193 | nfc_tm_deactivated(dev->nfc_dev); |
| 2194 | |
Samuel Ortiz | 51ad304 | 2012-05-31 20:01:32 +0200 | [diff] [blame] | 2195 | dev->tgt_mode = 0; |
| 2196 | |
Samuel Ortiz | dadb06f | 2012-05-31 00:09:11 +0200 | [diff] [blame] | 2197 | return 0; |
| 2198 | } |
| 2199 | |
| 2200 | queue_work(dev->wq, &dev->tg_work); |
| 2201 | |
| 2202 | return 0; |
| 2203 | } |
| 2204 | |
| 2205 | static int pn533_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb) |
| 2206 | { |
| 2207 | struct pn533 *dev = nfc_get_drvdata(nfc_dev); |
Samuel Ortiz | dadb06f | 2012-05-31 00:09:11 +0200 | [diff] [blame] | 2208 | int rc; |
| 2209 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 2210 | dev_dbg(dev->dev, "%s\n", __func__); |
Samuel Ortiz | dadb06f | 2012-05-31 00:09:11 +0200 | [diff] [blame] | 2211 | |
Olivier Guiter | 93ad420 | 2013-09-23 12:24:39 +0200 | [diff] [blame] | 2212 | /* let's split in multiple chunks if size's too big */ |
Waldemar Rymarkiewicz | e487882 | 2012-12-10 14:42:44 +0100 | [diff] [blame] | 2213 | if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) { |
Olivier Guiter | 93ad420 | 2013-09-23 12:24:39 +0200 | [diff] [blame] | 2214 | rc = pn533_fill_fragment_skbs(dev, skb); |
| 2215 | if (rc <= 0) |
| 2216 | goto error; |
| 2217 | |
| 2218 | /* get the first skb */ |
| 2219 | skb = skb_dequeue(&dev->fragment_skb); |
| 2220 | if (!skb) { |
| 2221 | rc = -EIO; |
| 2222 | goto error; |
| 2223 | } |
| 2224 | |
| 2225 | rc = pn533_send_data_async(dev, PN533_CMD_TG_SET_META_DATA, skb, |
| 2226 | pn533_tm_send_complete, NULL); |
| 2227 | } else { |
| 2228 | /* Send th skb */ |
| 2229 | rc = pn533_send_data_async(dev, PN533_CMD_TG_SET_DATA, skb, |
| 2230 | pn533_tm_send_complete, NULL); |
Samuel Ortiz | dadb06f | 2012-05-31 00:09:11 +0200 | [diff] [blame] | 2231 | } |
| 2232 | |
Olivier Guiter | 93ad420 | 2013-09-23 12:24:39 +0200 | [diff] [blame] | 2233 | error: |
| 2234 | if (rc < 0) { |
Waldemar Rymarkiewicz | e487882 | 2012-12-10 14:42:44 +0100 | [diff] [blame] | 2235 | dev_kfree_skb(skb); |
Olivier Guiter | 93ad420 | 2013-09-23 12:24:39 +0200 | [diff] [blame] | 2236 | skb_queue_purge(&dev->fragment_skb); |
| 2237 | } |
Samuel Ortiz | dadb06f | 2012-05-31 00:09:11 +0200 | [diff] [blame] | 2238 | |
| 2239 | return rc; |
| 2240 | } |
| 2241 | |
Samuel Ortiz | 6ff73fd | 2012-04-10 19:43:18 +0200 | [diff] [blame] | 2242 | static void pn533_wq_mi_recv(struct work_struct *work) |
| 2243 | { |
Olivier Guiter | 963a82e | 2013-06-13 13:43:28 +0000 | [diff] [blame] | 2244 | struct pn533 *dev = container_of(work, struct pn533, mi_rx_work); |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2245 | struct sk_buff *skb; |
Samuel Ortiz | 6ff73fd | 2012-04-10 19:43:18 +0200 | [diff] [blame] | 2246 | int rc; |
| 2247 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 2248 | dev_dbg(dev->dev, "%s\n", __func__); |
Samuel Ortiz | 6ff73fd | 2012-04-10 19:43:18 +0200 | [diff] [blame] | 2249 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 2250 | skb = pn533_alloc_skb(dev, PN533_CMD_DATAEXCH_HEAD_LEN); |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2251 | if (!skb) |
| 2252 | goto error; |
Samuel Ortiz | 6ff73fd | 2012-04-10 19:43:18 +0200 | [diff] [blame] | 2253 | |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2254 | switch (dev->device_type) { |
| 2255 | case PN533_DEVICE_PASORI: |
| 2256 | if (dev->tgt_active_prot == NFC_PROTO_FELICA) { |
| 2257 | rc = pn533_send_cmd_direct_async(dev, |
| 2258 | PN533_CMD_IN_COMM_THRU, |
| 2259 | skb, |
| 2260 | pn533_data_exchange_complete, |
| 2261 | dev->cmd_complete_mi_arg); |
Samuel Ortiz | 6ff73fd | 2012-04-10 19:43:18 +0200 | [diff] [blame] | 2262 | |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2263 | break; |
| 2264 | } |
Gustavo A. R. Silva | 9fe0a75 | 2019-02-13 12:54:03 -0600 | [diff] [blame] | 2265 | /* fall through */ |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2266 | default: |
Johannes Berg | 634fef6 | 2017-06-16 14:29:24 +0200 | [diff] [blame] | 2267 | skb_put_u8(skb, 1); /*TG*/ |
Samuel Ortiz | 6ff73fd | 2012-04-10 19:43:18 +0200 | [diff] [blame] | 2268 | |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2269 | rc = pn533_send_cmd_direct_async(dev, |
| 2270 | PN533_CMD_IN_DATA_EXCHANGE, |
| 2271 | skb, |
| 2272 | pn533_data_exchange_complete, |
| 2273 | dev->cmd_complete_mi_arg); |
Waldemar Rymarkiewicz | b1bb290 | 2012-11-26 14:18:32 +0100 | [diff] [blame] | 2274 | |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2275 | break; |
Samuel Ortiz | 6ff73fd | 2012-04-10 19:43:18 +0200 | [diff] [blame] | 2276 | } |
| 2277 | |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2278 | if (rc == 0) /* success */ |
Samuel Ortiz | 6ff73fd | 2012-04-10 19:43:18 +0200 | [diff] [blame] | 2279 | return; |
| 2280 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 2281 | nfc_err(dev->dev, |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 2282 | "Error %d when trying to perform data_exchange\n", rc); |
Samuel Ortiz | 6ff73fd | 2012-04-10 19:43:18 +0200 | [diff] [blame] | 2283 | |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2284 | dev_kfree_skb(skb); |
Waldemar Rymarkiewicz | 140ef7f | 2013-04-03 08:02:07 +0200 | [diff] [blame] | 2285 | kfree(dev->cmd_complete_mi_arg); |
Samuel Ortiz | 6ff73fd | 2012-04-10 19:43:18 +0200 | [diff] [blame] | 2286 | |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2287 | error: |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 2288 | dev->phy_ops->send_ack(dev, GFP_KERNEL); |
Samuel Ortiz | 5d50b36 | 2012-08-17 23:47:54 +0200 | [diff] [blame] | 2289 | queue_work(dev->wq, &dev->cmd_work); |
Samuel Ortiz | 6ff73fd | 2012-04-10 19:43:18 +0200 | [diff] [blame] | 2290 | } |
| 2291 | |
Olivier Guiter | 963a82e | 2013-06-13 13:43:28 +0000 | [diff] [blame] | 2292 | static void pn533_wq_mi_send(struct work_struct *work) |
| 2293 | { |
| 2294 | struct pn533 *dev = container_of(work, struct pn533, mi_tx_work); |
| 2295 | struct sk_buff *skb; |
| 2296 | int rc; |
| 2297 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 2298 | dev_dbg(dev->dev, "%s\n", __func__); |
Olivier Guiter | 963a82e | 2013-06-13 13:43:28 +0000 | [diff] [blame] | 2299 | |
| 2300 | /* Grab the first skb in the queue */ |
| 2301 | skb = skb_dequeue(&dev->fragment_skb); |
| 2302 | |
| 2303 | if (skb == NULL) { /* No more data */ |
| 2304 | /* Reset the queue for future use */ |
| 2305 | skb_queue_head_init(&dev->fragment_skb); |
| 2306 | goto error; |
| 2307 | } |
| 2308 | |
| 2309 | switch (dev->device_type) { |
| 2310 | case PN533_DEVICE_PASORI: |
| 2311 | if (dev->tgt_active_prot != NFC_PROTO_FELICA) { |
| 2312 | rc = -EIO; |
| 2313 | break; |
| 2314 | } |
| 2315 | |
| 2316 | rc = pn533_send_cmd_direct_async(dev, PN533_CMD_IN_COMM_THRU, |
| 2317 | skb, |
| 2318 | pn533_data_exchange_complete, |
| 2319 | dev->cmd_complete_dep_arg); |
| 2320 | |
| 2321 | break; |
| 2322 | |
| 2323 | default: |
| 2324 | /* Still some fragments? */ |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 2325 | rc = pn533_send_cmd_direct_async(dev, |
| 2326 | PN533_CMD_IN_DATA_EXCHANGE, |
Olivier Guiter | 963a82e | 2013-06-13 13:43:28 +0000 | [diff] [blame] | 2327 | skb, |
| 2328 | pn533_data_exchange_complete, |
| 2329 | dev->cmd_complete_dep_arg); |
| 2330 | |
| 2331 | break; |
| 2332 | } |
| 2333 | |
| 2334 | if (rc == 0) /* success */ |
| 2335 | return; |
| 2336 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 2337 | nfc_err(dev->dev, |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 2338 | "Error %d when trying to perform data_exchange\n", rc); |
Olivier Guiter | 963a82e | 2013-06-13 13:43:28 +0000 | [diff] [blame] | 2339 | |
| 2340 | dev_kfree_skb(skb); |
| 2341 | kfree(dev->cmd_complete_dep_arg); |
| 2342 | |
| 2343 | error: |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 2344 | dev->phy_ops->send_ack(dev, GFP_KERNEL); |
Olivier Guiter | 963a82e | 2013-06-13 13:43:28 +0000 | [diff] [blame] | 2345 | queue_work(dev->wq, &dev->cmd_work); |
| 2346 | } |
| 2347 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2348 | static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata, |
| 2349 | u8 cfgdata_len) |
| 2350 | { |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 2351 | struct sk_buff *skb; |
| 2352 | struct sk_buff *resp; |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 2353 | int skb_len; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2354 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 2355 | dev_dbg(dev->dev, "%s\n", __func__); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2356 | |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 2357 | skb_len = sizeof(cfgitem) + cfgdata_len; /* cfgitem + cfgdata */ |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2358 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 2359 | skb = pn533_alloc_skb(dev, skb_len); |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 2360 | if (!skb) |
| 2361 | return -ENOMEM; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2362 | |
Johannes Berg | 634fef6 | 2017-06-16 14:29:24 +0200 | [diff] [blame] | 2363 | skb_put_u8(skb, cfgitem); |
Johannes Berg | 59ae1d1 | 2017-06-16 14:29:20 +0200 | [diff] [blame] | 2364 | skb_put_data(skb, cfgdata, cfgdata_len); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2365 | |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 2366 | resp = pn533_send_cmd_sync(dev, PN533_CMD_RF_CONFIGURATION, skb); |
| 2367 | if (IS_ERR(resp)) |
| 2368 | return PTR_ERR(resp); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2369 | |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 2370 | dev_kfree_skb(resp); |
| 2371 | return 0; |
| 2372 | } |
| 2373 | |
| 2374 | static int pn533_get_firmware_version(struct pn533 *dev, |
| 2375 | struct pn533_fw_version *fv) |
| 2376 | { |
| 2377 | struct sk_buff *skb; |
| 2378 | struct sk_buff *resp; |
| 2379 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 2380 | skb = pn533_alloc_skb(dev, 0); |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 2381 | if (!skb) |
| 2382 | return -ENOMEM; |
| 2383 | |
| 2384 | resp = pn533_send_cmd_sync(dev, PN533_CMD_GET_FIRMWARE_VERSION, skb); |
| 2385 | if (IS_ERR(resp)) |
| 2386 | return PTR_ERR(resp); |
| 2387 | |
| 2388 | fv->ic = resp->data[0]; |
| 2389 | fv->ver = resp->data[1]; |
| 2390 | fv->rev = resp->data[2]; |
| 2391 | fv->support = resp->data[3]; |
| 2392 | |
| 2393 | dev_kfree_skb(resp); |
| 2394 | return 0; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2395 | } |
| 2396 | |
Waldemar Rymarkiewicz | f75c291 | 2013-04-03 08:02:02 +0200 | [diff] [blame] | 2397 | static int pn533_pasori_fw_reset(struct pn533 *dev) |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 2398 | { |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 2399 | struct sk_buff *skb; |
| 2400 | struct sk_buff *resp; |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 2401 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 2402 | dev_dbg(dev->dev, "%s\n", __func__); |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 2403 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 2404 | skb = pn533_alloc_skb(dev, sizeof(u8)); |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 2405 | if (!skb) |
| 2406 | return -ENOMEM; |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 2407 | |
Johannes Berg | 634fef6 | 2017-06-16 14:29:24 +0200 | [diff] [blame] | 2408 | skb_put_u8(skb, 0x1); |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 2409 | |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 2410 | resp = pn533_send_cmd_sync(dev, 0x18, skb); |
| 2411 | if (IS_ERR(resp)) |
| 2412 | return PTR_ERR(resp); |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 2413 | |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 2414 | dev_kfree_skb(resp); |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 2415 | |
Waldemar Rymarkiewicz | 94c5c15 | 2012-11-26 14:18:36 +0100 | [diff] [blame] | 2416 | return 0; |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 2417 | } |
| 2418 | |
Samuel Ortiz | 60d9edd5 | 2013-04-11 11:45:41 +0200 | [diff] [blame] | 2419 | static int pn533_rf_field(struct nfc_dev *nfc_dev, u8 rf) |
| 2420 | { |
| 2421 | struct pn533 *dev = nfc_get_drvdata(nfc_dev); |
| 2422 | u8 rf_field = !!rf; |
| 2423 | int rc; |
| 2424 | |
Samuel Ortiz | 3a8eab3 | 2013-07-01 17:26:58 +0200 | [diff] [blame] | 2425 | rf_field |= PN533_CFGITEM_RF_FIELD_AUTO_RFCA; |
| 2426 | |
Samuel Ortiz | 60d9edd5 | 2013-04-11 11:45:41 +0200 | [diff] [blame] | 2427 | rc = pn533_set_configuration(dev, PN533_CFGITEM_RF_FIELD, |
| 2428 | (u8 *)&rf_field, 1); |
| 2429 | if (rc) { |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 2430 | nfc_err(dev->dev, "Error on setting RF field\n"); |
Samuel Ortiz | 60d9edd5 | 2013-04-11 11:45:41 +0200 | [diff] [blame] | 2431 | return rc; |
| 2432 | } |
| 2433 | |
| 2434 | return rc; |
| 2435 | } |
| 2436 | |
Michael Thalmeier | dd7bedc | 2016-03-25 15:46:54 +0100 | [diff] [blame] | 2437 | static int pn532_sam_configuration(struct nfc_dev *nfc_dev) |
| 2438 | { |
| 2439 | struct pn533 *dev = nfc_get_drvdata(nfc_dev); |
| 2440 | struct sk_buff *skb; |
| 2441 | struct sk_buff *resp; |
| 2442 | |
| 2443 | skb = pn533_alloc_skb(dev, 1); |
| 2444 | if (!skb) |
| 2445 | return -ENOMEM; |
| 2446 | |
Johannes Berg | 634fef6 | 2017-06-16 14:29:24 +0200 | [diff] [blame] | 2447 | skb_put_u8(skb, 0x01); |
Michael Thalmeier | dd7bedc | 2016-03-25 15:46:54 +0100 | [diff] [blame] | 2448 | |
| 2449 | resp = pn533_send_cmd_sync(dev, PN533_CMD_SAM_CONFIGURATION, skb); |
| 2450 | if (IS_ERR(resp)) |
| 2451 | return PTR_ERR(resp); |
| 2452 | |
| 2453 | dev_kfree_skb(resp); |
| 2454 | return 0; |
| 2455 | } |
| 2456 | |
Sachin Kamat | e44666b | 2013-09-20 14:30:41 +0530 | [diff] [blame] | 2457 | static int pn533_dev_up(struct nfc_dev *nfc_dev) |
Samuel Ortiz | 60d9edd5 | 2013-04-11 11:45:41 +0200 | [diff] [blame] | 2458 | { |
Michael Thalmeier | dd7bedc | 2016-03-25 15:46:54 +0100 | [diff] [blame] | 2459 | struct pn533 *dev = nfc_get_drvdata(nfc_dev); |
| 2460 | |
| 2461 | if (dev->device_type == PN533_DEVICE_PN532) { |
| 2462 | int rc = pn532_sam_configuration(nfc_dev); |
| 2463 | |
| 2464 | if (rc) |
| 2465 | return rc; |
| 2466 | } |
| 2467 | |
Samuel Ortiz | 60d9edd5 | 2013-04-11 11:45:41 +0200 | [diff] [blame] | 2468 | return pn533_rf_field(nfc_dev, 1); |
| 2469 | } |
| 2470 | |
Sachin Kamat | e44666b | 2013-09-20 14:30:41 +0530 | [diff] [blame] | 2471 | static int pn533_dev_down(struct nfc_dev *nfc_dev) |
Samuel Ortiz | 60d9edd5 | 2013-04-11 11:45:41 +0200 | [diff] [blame] | 2472 | { |
| 2473 | return pn533_rf_field(nfc_dev, 0); |
| 2474 | } |
| 2475 | |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 2476 | static struct nfc_ops pn533_nfc_ops = { |
Samuel Ortiz | 60d9edd5 | 2013-04-11 11:45:41 +0200 | [diff] [blame] | 2477 | .dev_up = pn533_dev_up, |
| 2478 | .dev_down = pn533_dev_down, |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 2479 | .dep_link_up = pn533_dep_link_up, |
| 2480 | .dep_link_down = pn533_dep_link_down, |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2481 | .start_poll = pn533_start_poll, |
| 2482 | .stop_poll = pn533_stop_poll, |
| 2483 | .activate_target = pn533_activate_target, |
| 2484 | .deactivate_target = pn533_deactivate_target, |
Samuel Ortiz | be9ae4c | 2012-05-16 15:55:48 +0200 | [diff] [blame] | 2485 | .im_transceive = pn533_transceive, |
Samuel Ortiz | dadb06f | 2012-05-31 00:09:11 +0200 | [diff] [blame] | 2486 | .tm_send = pn533_tm_send, |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2487 | }; |
| 2488 | |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 2489 | static int pn533_setup(struct pn533 *dev) |
| 2490 | { |
| 2491 | struct pn533_config_max_retries max_retries; |
| 2492 | struct pn533_config_timing timing; |
| 2493 | u8 pasori_cfg[3] = {0x08, 0x01, 0x08}; |
| 2494 | int rc; |
| 2495 | |
| 2496 | switch (dev->device_type) { |
| 2497 | case PN533_DEVICE_STD: |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 2498 | case PN533_DEVICE_PASORI: |
Waldemar Rymarkiewicz | 53cf483 | 2013-04-03 08:02:13 +0200 | [diff] [blame] | 2499 | case PN533_DEVICE_ACR122U: |
Michael Thalmeier | dd7bedc | 2016-03-25 15:46:54 +0100 | [diff] [blame] | 2500 | case PN533_DEVICE_PN532: |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 2501 | max_retries.mx_rty_atr = 0x2; |
| 2502 | max_retries.mx_rty_psl = 0x1; |
| 2503 | max_retries.mx_rty_passive_act = |
| 2504 | PN533_CONFIG_MAX_RETRIES_NO_RETRY; |
| 2505 | |
| 2506 | timing.rfu = PN533_CONFIG_TIMING_102; |
| 2507 | timing.atr_res_timeout = PN533_CONFIG_TIMING_102; |
| 2508 | timing.dep_timeout = PN533_CONFIG_TIMING_204; |
| 2509 | |
| 2510 | break; |
| 2511 | |
| 2512 | default: |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 2513 | nfc_err(dev->dev, "Unknown device type %d\n", |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 2514 | dev->device_type); |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 2515 | return -EINVAL; |
| 2516 | } |
| 2517 | |
| 2518 | rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES, |
| 2519 | (u8 *)&max_retries, sizeof(max_retries)); |
| 2520 | if (rc) { |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 2521 | nfc_err(dev->dev, |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 2522 | "Error on setting MAX_RETRIES config\n"); |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 2523 | return rc; |
| 2524 | } |
| 2525 | |
| 2526 | |
| 2527 | rc = pn533_set_configuration(dev, PN533_CFGITEM_TIMING, |
| 2528 | (u8 *)&timing, sizeof(timing)); |
| 2529 | if (rc) { |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 2530 | nfc_err(dev->dev, "Error on setting RF timings\n"); |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 2531 | return rc; |
| 2532 | } |
| 2533 | |
| 2534 | switch (dev->device_type) { |
| 2535 | case PN533_DEVICE_STD: |
Michael Thalmeier | dd7bedc | 2016-03-25 15:46:54 +0100 | [diff] [blame] | 2536 | case PN533_DEVICE_PN532: |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 2537 | break; |
| 2538 | |
| 2539 | case PN533_DEVICE_PASORI: |
Waldemar Rymarkiewicz | f75c291 | 2013-04-03 08:02:02 +0200 | [diff] [blame] | 2540 | pn533_pasori_fw_reset(dev); |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 2541 | |
| 2542 | rc = pn533_set_configuration(dev, PN533_CFGITEM_PASORI, |
| 2543 | pasori_cfg, 3); |
| 2544 | if (rc) { |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 2545 | nfc_err(dev->dev, |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 2546 | "Error while settings PASORI config\n"); |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 2547 | return rc; |
| 2548 | } |
| 2549 | |
Waldemar Rymarkiewicz | f75c291 | 2013-04-03 08:02:02 +0200 | [diff] [blame] | 2550 | pn533_pasori_fw_reset(dev); |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 2551 | |
| 2552 | break; |
| 2553 | } |
| 2554 | |
| 2555 | return 0; |
| 2556 | } |
| 2557 | |
Andrey Rusalin | 32ecc75 | 2016-12-28 20:10:59 +0300 | [diff] [blame] | 2558 | int pn533_finalize_setup(struct pn533 *dev) |
| 2559 | { |
| 2560 | |
| 2561 | struct pn533_fw_version fw_ver; |
| 2562 | int rc; |
| 2563 | |
| 2564 | memset(&fw_ver, 0, sizeof(fw_ver)); |
| 2565 | |
| 2566 | rc = pn533_get_firmware_version(dev, &fw_ver); |
| 2567 | if (rc) { |
| 2568 | nfc_err(dev->dev, "Unable to get FW version\n"); |
| 2569 | return rc; |
| 2570 | } |
| 2571 | |
| 2572 | nfc_info(dev->dev, "NXP PN5%02X firmware ver %d.%d now attached\n", |
| 2573 | fw_ver.ic, fw_ver.ver, fw_ver.rev); |
| 2574 | |
| 2575 | rc = pn533_setup(dev); |
| 2576 | if (rc) |
| 2577 | return rc; |
| 2578 | |
| 2579 | return 0; |
| 2580 | } |
| 2581 | EXPORT_SYMBOL_GPL(pn533_finalize_setup); |
| 2582 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 2583 | struct pn533 *pn533_register_device(u32 device_type, |
| 2584 | u32 protocols, |
| 2585 | enum pn533_protocol_type protocol_type, |
| 2586 | void *phy, |
| 2587 | struct pn533_phy_ops *phy_ops, |
| 2588 | struct pn533_frame_ops *fops, |
Michael Thalmeier | b16931b | 2016-04-21 16:43:50 +0200 | [diff] [blame] | 2589 | struct device *dev, |
| 2590 | struct device *parent) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2591 | { |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 2592 | struct pn533 *priv; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2593 | int rc = -ENOMEM; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2594 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 2595 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
| 2596 | if (!priv) |
| 2597 | return ERR_PTR(-ENOMEM); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2598 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 2599 | priv->phy = phy; |
| 2600 | priv->phy_ops = phy_ops; |
| 2601 | priv->dev = dev; |
| 2602 | if (fops != NULL) |
| 2603 | priv->ops = fops; |
| 2604 | else |
| 2605 | priv->ops = &pn533_std_frame_ops; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2606 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 2607 | priv->protocol_type = protocol_type; |
| 2608 | priv->device_type = device_type; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2609 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 2610 | mutex_init(&priv->cmd_lock); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2611 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 2612 | INIT_WORK(&priv->cmd_work, pn533_wq_cmd); |
| 2613 | INIT_WORK(&priv->cmd_complete_work, pn533_wq_cmd_complete); |
| 2614 | INIT_WORK(&priv->mi_rx_work, pn533_wq_mi_recv); |
| 2615 | INIT_WORK(&priv->mi_tx_work, pn533_wq_mi_send); |
| 2616 | INIT_WORK(&priv->tg_work, pn533_wq_tg_get_data); |
| 2617 | INIT_WORK(&priv->mi_tm_rx_work, pn533_wq_tm_mi_recv); |
| 2618 | INIT_WORK(&priv->mi_tm_tx_work, pn533_wq_tm_mi_send); |
| 2619 | INIT_DELAYED_WORK(&priv->poll_work, pn533_wq_poll); |
| 2620 | INIT_WORK(&priv->rf_work, pn533_wq_rf); |
| 2621 | priv->wq = alloc_ordered_workqueue("pn533", 0); |
| 2622 | if (priv->wq == NULL) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2623 | goto error; |
| 2624 | |
Kees Cook | e99e88a | 2017-10-16 14:43:17 -0700 | [diff] [blame] | 2625 | timer_setup(&priv->listen_timer, pn533_listen_mode_timer, 0); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2626 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 2627 | skb_queue_head_init(&priv->resp_q); |
| 2628 | skb_queue_head_init(&priv->fragment_skb); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2629 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 2630 | INIT_LIST_HEAD(&priv->cmd_queue); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2631 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 2632 | priv->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols, |
| 2633 | priv->ops->tx_header_len + |
Samuel Ortiz | e875304 | 2011-08-19 15:47:11 +0200 | [diff] [blame] | 2634 | PN533_CMD_DATAEXCH_HEAD_LEN, |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 2635 | priv->ops->tx_tail_len); |
| 2636 | if (!priv->nfc_dev) { |
Wei Yongjun | 4674d0f | 2013-05-07 20:07:52 +0800 | [diff] [blame] | 2637 | rc = -ENOMEM; |
Samuel Ortiz | 4849f85 | 2012-04-10 19:43:17 +0200 | [diff] [blame] | 2638 | goto destroy_wq; |
Wei Yongjun | 4674d0f | 2013-05-07 20:07:52 +0800 | [diff] [blame] | 2639 | } |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2640 | |
Michael Thalmeier | b16931b | 2016-04-21 16:43:50 +0200 | [diff] [blame] | 2641 | nfc_set_parent_dev(priv->nfc_dev, parent); |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 2642 | nfc_set_drvdata(priv->nfc_dev, priv); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2643 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 2644 | rc = nfc_register_device(priv->nfc_dev); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2645 | if (rc) |
| 2646 | goto free_nfc_dev; |
| 2647 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 2648 | return priv; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2649 | |
| 2650 | free_nfc_dev: |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 2651 | nfc_free_device(priv->nfc_dev); |
Samuel Ortiz | 9f2f8ba | 2012-05-29 21:28:58 +0200 | [diff] [blame] | 2652 | |
Samuel Ortiz | 4849f85 | 2012-04-10 19:43:17 +0200 | [diff] [blame] | 2653 | destroy_wq: |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 2654 | destroy_workqueue(priv->wq); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2655 | error: |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 2656 | kfree(priv); |
| 2657 | return ERR_PTR(rc); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2658 | } |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 2659 | EXPORT_SYMBOL_GPL(pn533_register_device); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2660 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 2661 | void pn533_unregister_device(struct pn533 *priv) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2662 | { |
Samuel Ortiz | 5d50b36 | 2012-08-17 23:47:54 +0200 | [diff] [blame] | 2663 | struct pn533_cmd *cmd, *n; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2664 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 2665 | nfc_unregister_device(priv->nfc_dev); |
| 2666 | nfc_free_device(priv->nfc_dev); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2667 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 2668 | flush_delayed_work(&priv->poll_work); |
| 2669 | destroy_workqueue(priv->wq); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2670 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 2671 | skb_queue_purge(&priv->resp_q); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2672 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 2673 | del_timer(&priv->listen_timer); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2674 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 2675 | list_for_each_entry_safe(cmd, n, &priv->cmd_queue, queue) { |
Samuel Ortiz | 5d50b36 | 2012-08-17 23:47:54 +0200 | [diff] [blame] | 2676 | list_del(&cmd->queue); |
| 2677 | kfree(cmd); |
| 2678 | } |
| 2679 | |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 2680 | kfree(priv); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2681 | } |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 2682 | EXPORT_SYMBOL_GPL(pn533_unregister_device); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2683 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2684 | |
Waldemar Rymarkiewicz | e70b96e | 2013-04-03 08:02:01 +0200 | [diff] [blame] | 2685 | MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>"); |
| 2686 | MODULE_AUTHOR("Aloisio Almeida Jr <aloisio.almeida@openbossa.org>"); |
| 2687 | MODULE_AUTHOR("Waldemar Rymarkiewicz <waldemar.rymarkiewicz@tieto.com>"); |
Michael Thalmeier | 9815c7c | 2016-03-25 15:46:53 +0100 | [diff] [blame] | 2688 | MODULE_DESCRIPTION("PN533 driver ver " VERSION); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2689 | MODULE_VERSION(VERSION); |
| 2690 | MODULE_LICENSE("GPL"); |