Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 Instituto Nokia de Tecnologia |
Waldemar Rymarkiewicz | e70b96e | 2013-04-03 08:02:01 +0200 | [diff] [blame] | 3 | * Copyright (C) 2012-2013 Tieto Poland |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
Jeff Kirsher | 98b32de | 2013-12-06 08:56:16 -0800 | [diff] [blame] | 16 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 17 | */ |
| 18 | |
| 19 | #include <linux/device.h> |
| 20 | #include <linux/kernel.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/slab.h> |
| 23 | #include <linux/usb.h> |
| 24 | #include <linux/nfc.h> |
| 25 | #include <linux/netdevice.h> |
Ilan Elias | 55eb94f | 2011-09-18 11:19:34 +0300 | [diff] [blame] | 26 | #include <net/nfc/nfc.h> |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 27 | |
Waldemar Rymarkiewicz | 495af72 | 2013-04-03 08:02:16 +0200 | [diff] [blame] | 28 | #define VERSION "0.2" |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 29 | |
| 30 | #define PN533_VENDOR_ID 0x4CC |
| 31 | #define PN533_PRODUCT_ID 0x2533 |
| 32 | |
| 33 | #define SCM_VENDOR_ID 0x4E6 |
| 34 | #define SCL3711_PRODUCT_ID 0x5591 |
| 35 | |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 36 | #define SONY_VENDOR_ID 0x054c |
| 37 | #define PASORI_PRODUCT_ID 0x02e1 |
| 38 | |
Waldemar Rymarkiewicz | 53cf483 | 2013-04-03 08:02:13 +0200 | [diff] [blame] | 39 | #define ACS_VENDOR_ID 0x072f |
| 40 | #define ACR122U_PRODUCT_ID 0x2200 |
| 41 | |
| 42 | #define PN533_DEVICE_STD 0x1 |
| 43 | #define PN533_DEVICE_PASORI 0x2 |
| 44 | #define PN533_DEVICE_ACR122U 0x3 |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 45 | |
Samuel Ortiz | 01d719a | 2012-07-04 00:14:04 +0200 | [diff] [blame] | 46 | #define PN533_ALL_PROTOCOLS (NFC_PROTO_JEWEL_MASK | NFC_PROTO_MIFARE_MASK |\ |
| 47 | NFC_PROTO_FELICA_MASK | NFC_PROTO_ISO14443_MASK |\ |
| 48 | NFC_PROTO_NFC_DEP_MASK |\ |
| 49 | NFC_PROTO_ISO14443_B_MASK) |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 50 | |
| 51 | #define PN533_NO_TYPE_B_PROTOCOLS (NFC_PROTO_JEWEL_MASK | \ |
| 52 | NFC_PROTO_MIFARE_MASK | \ |
| 53 | NFC_PROTO_FELICA_MASK | \ |
Samuel Ortiz | 01d719a | 2012-07-04 00:14:04 +0200 | [diff] [blame] | 54 | NFC_PROTO_ISO14443_MASK | \ |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 55 | NFC_PROTO_NFC_DEP_MASK) |
| 56 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 57 | static const struct usb_device_id pn533_table[] = { |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 58 | { .match_flags = USB_DEVICE_ID_MATCH_DEVICE, |
| 59 | .idVendor = PN533_VENDOR_ID, |
| 60 | .idProduct = PN533_PRODUCT_ID, |
| 61 | .driver_info = PN533_DEVICE_STD, |
| 62 | }, |
| 63 | { .match_flags = USB_DEVICE_ID_MATCH_DEVICE, |
| 64 | .idVendor = SCM_VENDOR_ID, |
| 65 | .idProduct = SCL3711_PRODUCT_ID, |
| 66 | .driver_info = PN533_DEVICE_STD, |
| 67 | }, |
| 68 | { .match_flags = USB_DEVICE_ID_MATCH_DEVICE, |
| 69 | .idVendor = SONY_VENDOR_ID, |
| 70 | .idProduct = PASORI_PRODUCT_ID, |
| 71 | .driver_info = PN533_DEVICE_PASORI, |
| 72 | }, |
Waldemar Rymarkiewicz | 53cf483 | 2013-04-03 08:02:13 +0200 | [diff] [blame] | 73 | { .match_flags = USB_DEVICE_ID_MATCH_DEVICE, |
| 74 | .idVendor = ACS_VENDOR_ID, |
| 75 | .idProduct = ACR122U_PRODUCT_ID, |
| 76 | .driver_info = PN533_DEVICE_ACR122U, |
| 77 | }, |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 78 | { } |
| 79 | }; |
| 80 | MODULE_DEVICE_TABLE(usb, pn533_table); |
| 81 | |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 82 | /* How much time we spend listening for initiators */ |
| 83 | #define PN533_LISTEN_TIME 2 |
Samuel Ortiz | 46f793b | 2013-07-03 14:50:36 +0200 | [diff] [blame] | 84 | /* Delay between each poll frame (ms) */ |
| 85 | #define PN533_POLL_INTERVAL 10 |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 86 | |
Olivier Guiter | 1575b9d | 2013-06-13 15:43:27 +0200 | [diff] [blame] | 87 | /* Standard pn533 frame definitions (standard and extended)*/ |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 88 | #define PN533_STD_FRAME_HEADER_LEN (sizeof(struct pn533_std_frame) \ |
Waldemar Rymarkiewicz | b1bb290 | 2012-11-26 14:18:32 +0100 | [diff] [blame] | 89 | + 2) /* data[0] TFI, data[1] CC */ |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 90 | #define PN533_STD_FRAME_TAIL_LEN 2 /* data[len] DCS, data[len + 1] postamble*/ |
Waldemar Rymarkiewicz | 82dec34 | 2012-10-11 14:03:58 +0200 | [diff] [blame] | 91 | |
Olivier Guiter | 1575b9d | 2013-06-13 15:43:27 +0200 | [diff] [blame] | 92 | #define PN533_EXT_FRAME_HEADER_LEN (sizeof(struct pn533_ext_frame) \ |
| 93 | + 2) /* data[0] TFI, data[1] CC */ |
| 94 | |
| 95 | #define PN533_CMD_DATAEXCH_DATA_MAXLEN 262 |
| 96 | #define PN533_CMD_DATAFRAME_MAXLEN 240 /* max data length (send) */ |
| 97 | |
Waldemar Rymarkiewicz | 15461ae | 2012-11-26 14:18:35 +0100 | [diff] [blame] | 98 | /* |
| 99 | * Max extended frame payload len, excluding TFI and CC |
| 100 | * which are already in PN533_FRAME_HEADER_LEN. |
| 101 | */ |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 102 | #define PN533_STD_FRAME_MAX_PAYLOAD_LEN 263 |
Waldemar Rymarkiewicz | 15461ae | 2012-11-26 14:18:35 +0100 | [diff] [blame] | 103 | |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 104 | #define PN533_STD_FRAME_ACK_SIZE 6 /* Preamble (1), SoPC (2), ACK Code (2), |
Waldemar Rymarkiewicz | 5b5a443 | 2012-12-10 14:42:56 +0100 | [diff] [blame] | 105 | Postamble (1) */ |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 106 | #define PN533_STD_FRAME_CHECKSUM(f) (f->data[f->datalen]) |
| 107 | #define PN533_STD_FRAME_POSTAMBLE(f) (f->data[f->datalen + 1]) |
Olivier Guiter | 1575b9d | 2013-06-13 15:43:27 +0200 | [diff] [blame] | 108 | /* Half start code (3), LEN (4) should be 0xffff for extended frame */ |
| 109 | #define PN533_STD_IS_EXTENDED(hdr) ((hdr)->datalen == 0xFF \ |
| 110 | && (hdr)->datalen_checksum == 0xFF) |
| 111 | #define PN533_EXT_FRAME_CHECKSUM(f) (f->data[be16_to_cpu(f->datalen)]) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 112 | |
| 113 | /* start of frame */ |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 114 | #define PN533_STD_FRAME_SOF 0x00FF |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 115 | |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 116 | /* standard frame identifier: in/out/error */ |
| 117 | #define PN533_STD_FRAME_IDENTIFIER(f) (f->data[0]) /* TFI */ |
| 118 | #define PN533_STD_FRAME_DIR_OUT 0xD4 |
| 119 | #define PN533_STD_FRAME_DIR_IN 0xD5 |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 120 | |
Waldemar Rymarkiewicz | 53cf483 | 2013-04-03 08:02:13 +0200 | [diff] [blame] | 121 | /* ACS ACR122 pn533 frame definitions */ |
| 122 | #define PN533_ACR122_TX_FRAME_HEADER_LEN (sizeof(struct pn533_acr122_tx_frame) \ |
| 123 | + 2) |
| 124 | #define PN533_ACR122_TX_FRAME_TAIL_LEN 0 |
| 125 | #define PN533_ACR122_RX_FRAME_HEADER_LEN (sizeof(struct pn533_acr122_rx_frame) \ |
| 126 | + 2) |
| 127 | #define PN533_ACR122_RX_FRAME_TAIL_LEN 2 |
| 128 | #define PN533_ACR122_FRAME_MAX_PAYLOAD_LEN PN533_STD_FRAME_MAX_PAYLOAD_LEN |
| 129 | |
| 130 | /* CCID messages types */ |
| 131 | #define PN533_ACR122_PC_TO_RDR_ICCPOWERON 0x62 |
| 132 | #define PN533_ACR122_PC_TO_RDR_ESCAPE 0x6B |
| 133 | |
| 134 | #define PN533_ACR122_RDR_TO_PC_ESCAPE 0x83 |
| 135 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 136 | /* PN533 Commands */ |
Olivier Guiter | 1575b9d | 2013-06-13 15:43:27 +0200 | [diff] [blame] | 137 | #define PN533_FRAME_CMD(f) (f->data[1]) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 138 | |
| 139 | #define PN533_CMD_GET_FIRMWARE_VERSION 0x02 |
| 140 | #define PN533_CMD_RF_CONFIGURATION 0x32 |
| 141 | #define PN533_CMD_IN_DATA_EXCHANGE 0x40 |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 142 | #define PN533_CMD_IN_COMM_THRU 0x42 |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 143 | #define PN533_CMD_IN_LIST_PASSIVE_TARGET 0x4A |
| 144 | #define PN533_CMD_IN_ATR 0x50 |
| 145 | #define PN533_CMD_IN_RELEASE 0x52 |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 146 | #define PN533_CMD_IN_JUMP_FOR_DEP 0x56 |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 147 | |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 148 | #define PN533_CMD_TG_INIT_AS_TARGET 0x8c |
Samuel Ortiz | 103b34c | 2012-05-31 00:07:51 +0200 | [diff] [blame] | 149 | #define PN533_CMD_TG_GET_DATA 0x86 |
Samuel Ortiz | dadb06f | 2012-05-31 00:09:11 +0200 | [diff] [blame] | 150 | #define PN533_CMD_TG_SET_DATA 0x8e |
Olivier Guiter | 93ad420 | 2013-09-23 12:24:39 +0200 | [diff] [blame] | 151 | #define PN533_CMD_TG_SET_META_DATA 0x94 |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 152 | #define PN533_CMD_UNDEF 0xff |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 153 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 154 | #define PN533_CMD_RESPONSE(cmd) (cmd + 1) |
| 155 | |
| 156 | /* PN533 Return codes */ |
| 157 | #define PN533_CMD_RET_MASK 0x3F |
| 158 | #define PN533_CMD_MI_MASK 0x40 |
| 159 | #define PN533_CMD_RET_SUCCESS 0x00 |
| 160 | |
| 161 | struct pn533; |
| 162 | |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 163 | typedef int (*pn533_send_async_complete_t) (struct pn533 *dev, void *arg, |
| 164 | struct sk_buff *resp); |
| 165 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 166 | /* structs for pn533 commands */ |
| 167 | |
| 168 | /* PN533_CMD_GET_FIRMWARE_VERSION */ |
| 169 | struct pn533_fw_version { |
| 170 | u8 ic; |
| 171 | u8 ver; |
| 172 | u8 rev; |
| 173 | u8 support; |
| 174 | }; |
| 175 | |
| 176 | /* PN533_CMD_RF_CONFIGURATION */ |
Samuel Ortiz | 60d9edd5 | 2013-04-11 11:45:41 +0200 | [diff] [blame] | 177 | #define PN533_CFGITEM_RF_FIELD 0x01 |
| 178 | #define PN533_CFGITEM_TIMING 0x02 |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 179 | #define PN533_CFGITEM_MAX_RETRIES 0x05 |
Samuel Ortiz | 60d9edd5 | 2013-04-11 11:45:41 +0200 | [diff] [blame] | 180 | #define PN533_CFGITEM_PASORI 0x82 |
| 181 | |
Samuel Ortiz | 3a8eab3 | 2013-07-01 17:26:58 +0200 | [diff] [blame] | 182 | #define PN533_CFGITEM_RF_FIELD_AUTO_RFCA 0x2 |
| 183 | #define PN533_CFGITEM_RF_FIELD_ON 0x1 |
| 184 | #define PN533_CFGITEM_RF_FIELD_OFF 0x0 |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 185 | |
Samuel Ortiz | 34a85bf | 2012-05-29 21:34:08 +0200 | [diff] [blame] | 186 | #define PN533_CONFIG_TIMING_102 0xb |
| 187 | #define PN533_CONFIG_TIMING_204 0xc |
| 188 | #define PN533_CONFIG_TIMING_409 0xd |
| 189 | #define PN533_CONFIG_TIMING_819 0xe |
| 190 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 191 | #define PN533_CONFIG_MAX_RETRIES_NO_RETRY 0x00 |
| 192 | #define PN533_CONFIG_MAX_RETRIES_ENDLESS 0xFF |
| 193 | |
| 194 | struct pn533_config_max_retries { |
| 195 | u8 mx_rty_atr; |
| 196 | u8 mx_rty_psl; |
| 197 | u8 mx_rty_passive_act; |
| 198 | } __packed; |
| 199 | |
Samuel Ortiz | 34a85bf | 2012-05-29 21:34:08 +0200 | [diff] [blame] | 200 | struct pn533_config_timing { |
| 201 | u8 rfu; |
| 202 | u8 atr_res_timeout; |
| 203 | u8 dep_timeout; |
| 204 | } __packed; |
| 205 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 206 | /* PN533_CMD_IN_LIST_PASSIVE_TARGET */ |
| 207 | |
| 208 | /* felica commands opcode */ |
| 209 | #define PN533_FELICA_OPC_SENSF_REQ 0 |
| 210 | #define PN533_FELICA_OPC_SENSF_RES 1 |
| 211 | /* felica SENSF_REQ parameters */ |
| 212 | #define PN533_FELICA_SENSF_SC_ALL 0xFFFF |
| 213 | #define PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE 0 |
| 214 | #define PN533_FELICA_SENSF_RC_SYSTEM_CODE 1 |
| 215 | #define PN533_FELICA_SENSF_RC_ADVANCED_PROTOCOL 2 |
| 216 | |
| 217 | /* type B initiator_data values */ |
| 218 | #define PN533_TYPE_B_AFI_ALL_FAMILIES 0 |
| 219 | #define PN533_TYPE_B_POLL_METHOD_TIMESLOT 0 |
| 220 | #define PN533_TYPE_B_POLL_METHOD_PROBABILISTIC 1 |
| 221 | |
| 222 | union pn533_cmd_poll_initdata { |
| 223 | struct { |
| 224 | u8 afi; |
| 225 | u8 polling_method; |
| 226 | } __packed type_b; |
| 227 | struct { |
| 228 | u8 opcode; |
| 229 | __be16 sc; |
| 230 | u8 rc; |
| 231 | u8 tsn; |
| 232 | } __packed felica; |
| 233 | }; |
| 234 | |
| 235 | /* Poll modulations */ |
| 236 | enum { |
| 237 | PN533_POLL_MOD_106KBPS_A, |
| 238 | PN533_POLL_MOD_212KBPS_FELICA, |
| 239 | PN533_POLL_MOD_424KBPS_FELICA, |
| 240 | PN533_POLL_MOD_106KBPS_JEWEL, |
| 241 | PN533_POLL_MOD_847KBPS_B, |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 242 | PN533_LISTEN_MOD, |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 243 | |
| 244 | __PN533_POLL_MOD_AFTER_LAST, |
| 245 | }; |
| 246 | #define PN533_POLL_MOD_MAX (__PN533_POLL_MOD_AFTER_LAST - 1) |
| 247 | |
| 248 | struct pn533_poll_modulations { |
| 249 | struct { |
| 250 | u8 maxtg; |
| 251 | u8 brty; |
| 252 | union pn533_cmd_poll_initdata initiator_data; |
| 253 | } __packed data; |
| 254 | u8 len; |
| 255 | }; |
| 256 | |
Waldemar Rymarkiewicz | ef3d56e | 2013-01-16 15:51:51 +0100 | [diff] [blame] | 257 | static const struct pn533_poll_modulations poll_mod[] = { |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 258 | [PN533_POLL_MOD_106KBPS_A] = { |
| 259 | .data = { |
| 260 | .maxtg = 1, |
| 261 | .brty = 0, |
| 262 | }, |
| 263 | .len = 2, |
| 264 | }, |
| 265 | [PN533_POLL_MOD_212KBPS_FELICA] = { |
| 266 | .data = { |
| 267 | .maxtg = 1, |
| 268 | .brty = 1, |
| 269 | .initiator_data.felica = { |
| 270 | .opcode = PN533_FELICA_OPC_SENSF_REQ, |
| 271 | .sc = PN533_FELICA_SENSF_SC_ALL, |
Samuel Ortiz | a94e10f | 2013-06-28 15:43:19 +0200 | [diff] [blame] | 272 | .rc = PN533_FELICA_SENSF_RC_SYSTEM_CODE, |
Samuel Ortiz | 31c4446 | 2013-05-27 14:59:40 +0200 | [diff] [blame] | 273 | .tsn = 0x03, |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 274 | }, |
| 275 | }, |
| 276 | .len = 7, |
| 277 | }, |
| 278 | [PN533_POLL_MOD_424KBPS_FELICA] = { |
| 279 | .data = { |
| 280 | .maxtg = 1, |
| 281 | .brty = 2, |
| 282 | .initiator_data.felica = { |
| 283 | .opcode = PN533_FELICA_OPC_SENSF_REQ, |
| 284 | .sc = PN533_FELICA_SENSF_SC_ALL, |
Samuel Ortiz | a94e10f | 2013-06-28 15:43:19 +0200 | [diff] [blame] | 285 | .rc = PN533_FELICA_SENSF_RC_SYSTEM_CODE, |
Samuel Ortiz | 31c4446 | 2013-05-27 14:59:40 +0200 | [diff] [blame] | 286 | .tsn = 0x03, |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 287 | }, |
| 288 | }, |
| 289 | .len = 7, |
| 290 | }, |
| 291 | [PN533_POLL_MOD_106KBPS_JEWEL] = { |
| 292 | .data = { |
| 293 | .maxtg = 1, |
| 294 | .brty = 4, |
| 295 | }, |
| 296 | .len = 2, |
| 297 | }, |
| 298 | [PN533_POLL_MOD_847KBPS_B] = { |
| 299 | .data = { |
| 300 | .maxtg = 1, |
| 301 | .brty = 8, |
| 302 | .initiator_data.type_b = { |
| 303 | .afi = PN533_TYPE_B_AFI_ALL_FAMILIES, |
| 304 | .polling_method = |
| 305 | PN533_TYPE_B_POLL_METHOD_TIMESLOT, |
| 306 | }, |
| 307 | }, |
| 308 | .len = 3, |
| 309 | }, |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 310 | [PN533_LISTEN_MOD] = { |
| 311 | .len = 0, |
| 312 | }, |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 313 | }; |
| 314 | |
| 315 | /* PN533_CMD_IN_ATR */ |
| 316 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 317 | struct pn533_cmd_activate_response { |
| 318 | u8 status; |
| 319 | u8 nfcid3t[10]; |
| 320 | u8 didt; |
| 321 | u8 bst; |
| 322 | u8 brt; |
| 323 | u8 to; |
| 324 | u8 ppt; |
| 325 | /* optional */ |
| 326 | u8 gt[]; |
| 327 | } __packed; |
| 328 | |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 329 | struct pn533_cmd_jump_dep_response { |
| 330 | u8 status; |
| 331 | u8 tg; |
| 332 | u8 nfcid3t[10]; |
| 333 | u8 didt; |
| 334 | u8 bst; |
| 335 | u8 brt; |
| 336 | u8 to; |
| 337 | u8 ppt; |
| 338 | /* optional */ |
| 339 | u8 gt[]; |
| 340 | } __packed; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 341 | |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 342 | |
| 343 | /* PN533_TG_INIT_AS_TARGET */ |
| 344 | #define PN533_INIT_TARGET_PASSIVE 0x1 |
| 345 | #define PN533_INIT_TARGET_DEP 0x2 |
| 346 | |
Samuel Ortiz | fc40a8c | 2012-06-01 13:21:13 +0200 | [diff] [blame] | 347 | #define PN533_INIT_TARGET_RESP_FRAME_MASK 0x3 |
| 348 | #define PN533_INIT_TARGET_RESP_ACTIVE 0x1 |
| 349 | #define PN533_INIT_TARGET_RESP_DEP 0x4 |
| 350 | |
Waldemar Rymarkiewicz | 5852037 | 2013-04-03 08:02:12 +0200 | [diff] [blame] | 351 | enum pn533_protocol_type { |
| 352 | PN533_PROTO_REQ_ACK_RESP = 0, |
| 353 | PN533_PROTO_REQ_RESP |
| 354 | }; |
| 355 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 356 | struct pn533 { |
| 357 | struct usb_device *udev; |
| 358 | struct usb_interface *interface; |
| 359 | struct nfc_dev *nfc_dev; |
Waldemar Rymarkiewicz | d5590bb | 2013-04-03 08:02:10 +0200 | [diff] [blame] | 360 | u32 device_type; |
Waldemar Rymarkiewicz | 5852037 | 2013-04-03 08:02:12 +0200 | [diff] [blame] | 361 | enum pn533_protocol_type protocol_type; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 362 | |
| 363 | struct urb *out_urb; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 364 | struct urb *in_urb; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 365 | |
Samuel Ortiz | 6ff73fd | 2012-04-10 19:43:18 +0200 | [diff] [blame] | 366 | struct sk_buff_head resp_q; |
Olivier Guiter | 963a82e | 2013-06-13 13:43:28 +0000 | [diff] [blame] | 367 | struct sk_buff_head fragment_skb; |
Samuel Ortiz | 6ff73fd | 2012-04-10 19:43:18 +0200 | [diff] [blame] | 368 | |
Samuel Ortiz | 4849f85 | 2012-04-10 19:43:17 +0200 | [diff] [blame] | 369 | struct workqueue_struct *wq; |
| 370 | struct work_struct cmd_work; |
Samuel Ortiz | 5d50b36 | 2012-08-17 23:47:54 +0200 | [diff] [blame] | 371 | struct work_struct cmd_complete_work; |
Samuel Ortiz | 46f793b | 2013-07-03 14:50:36 +0200 | [diff] [blame] | 372 | struct delayed_work poll_work; |
Olivier Guiter | 963a82e | 2013-06-13 13:43:28 +0000 | [diff] [blame] | 373 | struct work_struct mi_rx_work; |
| 374 | struct work_struct mi_tx_work; |
Olivier Guiter | 3c13b24 | 2013-09-23 12:24:38 +0200 | [diff] [blame] | 375 | struct work_struct mi_tm_rx_work; |
Olivier Guiter | 93ad420 | 2013-09-23 12:24:39 +0200 | [diff] [blame] | 376 | struct work_struct mi_tm_tx_work; |
Samuel Ortiz | 103b34c | 2012-05-31 00:07:51 +0200 | [diff] [blame] | 377 | struct work_struct tg_work; |
Samuel Ortiz | 17e9d9d | 2013-06-26 18:16:21 +0200 | [diff] [blame] | 378 | struct work_struct rf_work; |
Waldemar Rymarkiewicz | d5590bb | 2013-04-03 08:02:10 +0200 | [diff] [blame] | 379 | |
| 380 | struct list_head cmd_queue; |
| 381 | struct pn533_cmd *cmd; |
| 382 | u8 cmd_pending; |
Waldemar Rymarkiewicz | d5590bb | 2013-04-03 08:02:10 +0200 | [diff] [blame] | 383 | struct mutex cmd_lock; /* protects cmd queue */ |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 384 | |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 385 | void *cmd_complete_mi_arg; |
Olivier Guiter | 963a82e | 2013-06-13 13:43:28 +0000 | [diff] [blame] | 386 | void *cmd_complete_dep_arg; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 387 | |
| 388 | struct pn533_poll_modulations *poll_mod_active[PN533_POLL_MOD_MAX + 1]; |
| 389 | u8 poll_mod_count; |
| 390 | u8 poll_mod_curr; |
Samuel Ortiz | 673088f | 2013-08-21 15:06:55 +0200 | [diff] [blame] | 391 | u8 poll_dep; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 392 | u32 poll_protocols; |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 393 | u32 listen_protocols; |
Waldemar Rymarkiewicz | d5590bb | 2013-04-03 08:02:10 +0200 | [diff] [blame] | 394 | struct timer_list listen_timer; |
| 395 | int cancel_listen; |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 396 | |
| 397 | u8 *gb; |
| 398 | size_t gb_len; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 399 | |
| 400 | u8 tgt_available_prots; |
| 401 | u8 tgt_active_prot; |
Samuel Ortiz | 51ad304 | 2012-05-31 20:01:32 +0200 | [diff] [blame] | 402 | u8 tgt_mode; |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 403 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 404 | struct pn533_frame_ops *ops; |
Samuel Ortiz | 5d50b36 | 2012-08-17 23:47:54 +0200 | [diff] [blame] | 405 | }; |
| 406 | |
| 407 | struct pn533_cmd { |
| 408 | struct list_head queue; |
Waldemar Rymarkiewicz | 4b2a953 | 2013-04-03 08:02:06 +0200 | [diff] [blame] | 409 | u8 code; |
Waldemar Rymarkiewicz | f87bc9f | 2013-04-03 08:02:11 +0200 | [diff] [blame] | 410 | int status; |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 411 | struct sk_buff *req; |
| 412 | struct sk_buff *resp; |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 413 | int resp_len; |
Waldemar Rymarkiewicz | 4231604 | 2013-04-03 08:02:04 +0200 | [diff] [blame] | 414 | pn533_send_async_complete_t complete_cb; |
| 415 | void *complete_cb_context; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 416 | }; |
| 417 | |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 418 | struct pn533_std_frame { |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 419 | u8 preamble; |
| 420 | __be16 start_frame; |
| 421 | u8 datalen; |
| 422 | u8 datalen_checksum; |
| 423 | u8 data[]; |
| 424 | } __packed; |
| 425 | |
Olivier Guiter | 1575b9d | 2013-06-13 15:43:27 +0200 | [diff] [blame] | 426 | struct pn533_ext_frame { /* Extended Information frame */ |
| 427 | u8 preamble; |
| 428 | __be16 start_frame; |
| 429 | __be16 eif_flag; /* fixed to 0xFFFF */ |
| 430 | __be16 datalen; |
| 431 | u8 datalen_checksum; |
| 432 | u8 data[]; |
| 433 | } __packed; |
| 434 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 435 | struct pn533_frame_ops { |
| 436 | void (*tx_frame_init)(void *frame, u8 cmd_code); |
| 437 | void (*tx_frame_finish)(void *frame); |
| 438 | void (*tx_update_payload_len)(void *frame, int len); |
| 439 | int tx_header_len; |
| 440 | int tx_tail_len; |
| 441 | |
Olivier Guiter | 56a63c82 | 2013-06-13 13:43:29 +0000 | [diff] [blame] | 442 | bool (*rx_is_frame_valid)(void *frame, struct pn533 *dev); |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 443 | int (*rx_frame_size)(void *frame); |
| 444 | int rx_header_len; |
| 445 | int rx_tail_len; |
| 446 | |
| 447 | int max_payload_len; |
| 448 | u8 (*get_cmd_code)(void *frame); |
| 449 | }; |
| 450 | |
Waldemar Rymarkiewicz | 53cf483 | 2013-04-03 08:02:13 +0200 | [diff] [blame] | 451 | struct pn533_acr122_ccid_hdr { |
| 452 | u8 type; |
| 453 | u32 datalen; |
| 454 | u8 slot; |
| 455 | u8 seq; |
| 456 | u8 params[3]; /* 3 msg specific bytes or status, error and 1 specific |
| 457 | byte for reposnse msg */ |
| 458 | u8 data[]; /* payload */ |
| 459 | } __packed; |
| 460 | |
| 461 | struct pn533_acr122_apdu_hdr { |
| 462 | u8 class; |
| 463 | u8 ins; |
| 464 | u8 p1; |
| 465 | u8 p2; |
| 466 | } __packed; |
| 467 | |
| 468 | struct pn533_acr122_tx_frame { |
| 469 | struct pn533_acr122_ccid_hdr ccid; |
| 470 | struct pn533_acr122_apdu_hdr apdu; |
| 471 | u8 datalen; |
| 472 | u8 data[]; /* pn533 frame: TFI ... */ |
| 473 | } __packed; |
| 474 | |
| 475 | struct pn533_acr122_rx_frame { |
| 476 | struct pn533_acr122_ccid_hdr ccid; |
| 477 | u8 data[]; /* pn533 frame : TFI ... */ |
| 478 | } __packed; |
| 479 | |
| 480 | static void pn533_acr122_tx_frame_init(void *_frame, u8 cmd_code) |
| 481 | { |
| 482 | struct pn533_acr122_tx_frame *frame = _frame; |
| 483 | |
| 484 | frame->ccid.type = PN533_ACR122_PC_TO_RDR_ESCAPE; |
| 485 | frame->ccid.datalen = sizeof(frame->apdu) + 1; /* sizeof(apdu_hdr) + |
| 486 | sizeof(datalen) */ |
| 487 | frame->ccid.slot = 0; |
| 488 | frame->ccid.seq = 0; |
| 489 | frame->ccid.params[0] = 0; |
| 490 | frame->ccid.params[1] = 0; |
| 491 | frame->ccid.params[2] = 0; |
| 492 | |
| 493 | frame->data[0] = PN533_STD_FRAME_DIR_OUT; |
| 494 | frame->data[1] = cmd_code; |
| 495 | frame->datalen = 2; /* data[0] + data[1] */ |
| 496 | |
| 497 | frame->apdu.class = 0xFF; |
| 498 | frame->apdu.ins = 0; |
| 499 | frame->apdu.p1 = 0; |
| 500 | frame->apdu.p2 = 0; |
| 501 | } |
| 502 | |
| 503 | static void pn533_acr122_tx_frame_finish(void *_frame) |
| 504 | { |
| 505 | struct pn533_acr122_tx_frame *frame = _frame; |
| 506 | |
| 507 | frame->ccid.datalen += frame->datalen; |
| 508 | } |
| 509 | |
| 510 | static void pn533_acr122_tx_update_payload_len(void *_frame, int len) |
| 511 | { |
| 512 | struct pn533_acr122_tx_frame *frame = _frame; |
| 513 | |
| 514 | frame->datalen += len; |
| 515 | } |
| 516 | |
Olivier Guiter | 56a63c82 | 2013-06-13 13:43:29 +0000 | [diff] [blame] | 517 | static bool pn533_acr122_is_rx_frame_valid(void *_frame, struct pn533 *dev) |
Waldemar Rymarkiewicz | 53cf483 | 2013-04-03 08:02:13 +0200 | [diff] [blame] | 518 | { |
| 519 | struct pn533_acr122_rx_frame *frame = _frame; |
| 520 | |
| 521 | if (frame->ccid.type != 0x83) |
| 522 | return false; |
| 523 | |
Samuel Ortiz | ea87a5e | 2014-01-04 03:23:35 +0100 | [diff] [blame] | 524 | if (!frame->ccid.datalen) |
| 525 | return false; |
| 526 | |
Waldemar Rymarkiewicz | 53cf483 | 2013-04-03 08:02:13 +0200 | [diff] [blame] | 527 | if (frame->data[frame->ccid.datalen - 2] == 0x63) |
| 528 | return false; |
| 529 | |
| 530 | return true; |
| 531 | } |
| 532 | |
| 533 | static int pn533_acr122_rx_frame_size(void *frame) |
| 534 | { |
| 535 | struct pn533_acr122_rx_frame *f = frame; |
| 536 | |
| 537 | /* f->ccid.datalen already includes tail length */ |
| 538 | return sizeof(struct pn533_acr122_rx_frame) + f->ccid.datalen; |
| 539 | } |
| 540 | |
| 541 | static u8 pn533_acr122_get_cmd_code(void *frame) |
| 542 | { |
| 543 | struct pn533_acr122_rx_frame *f = frame; |
| 544 | |
Olivier Guiter | 1575b9d | 2013-06-13 15:43:27 +0200 | [diff] [blame] | 545 | return PN533_FRAME_CMD(f); |
Waldemar Rymarkiewicz | 53cf483 | 2013-04-03 08:02:13 +0200 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | static struct pn533_frame_ops pn533_acr122_frame_ops = { |
| 549 | .tx_frame_init = pn533_acr122_tx_frame_init, |
| 550 | .tx_frame_finish = pn533_acr122_tx_frame_finish, |
| 551 | .tx_update_payload_len = pn533_acr122_tx_update_payload_len, |
| 552 | .tx_header_len = PN533_ACR122_TX_FRAME_HEADER_LEN, |
| 553 | .tx_tail_len = PN533_ACR122_TX_FRAME_TAIL_LEN, |
| 554 | |
| 555 | .rx_is_frame_valid = pn533_acr122_is_rx_frame_valid, |
| 556 | .rx_header_len = PN533_ACR122_RX_FRAME_HEADER_LEN, |
| 557 | .rx_tail_len = PN533_ACR122_RX_FRAME_TAIL_LEN, |
| 558 | .rx_frame_size = pn533_acr122_rx_frame_size, |
| 559 | |
| 560 | .max_payload_len = PN533_ACR122_FRAME_MAX_PAYLOAD_LEN, |
| 561 | .get_cmd_code = pn533_acr122_get_cmd_code, |
| 562 | }; |
| 563 | |
Olivier Guiter | 1575b9d | 2013-06-13 15:43:27 +0200 | [diff] [blame] | 564 | /* The rule: value(high byte) + value(low byte) + checksum = 0 */ |
| 565 | static inline u8 pn533_ext_checksum(u16 value) |
| 566 | { |
| 567 | return ~(u8)(((value & 0xFF00) >> 8) + (u8)(value & 0xFF)) + 1; |
| 568 | } |
| 569 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 570 | /* The rule: value + checksum = 0 */ |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 571 | static inline u8 pn533_std_checksum(u8 value) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 572 | { |
| 573 | return ~value + 1; |
| 574 | } |
| 575 | |
| 576 | /* The rule: sum(data elements) + checksum = 0 */ |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 577 | static u8 pn533_std_data_checksum(u8 *data, int datalen) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 578 | { |
| 579 | u8 sum = 0; |
| 580 | int i; |
| 581 | |
| 582 | for (i = 0; i < datalen; i++) |
| 583 | sum += data[i]; |
| 584 | |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 585 | return pn533_std_checksum(sum); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 586 | } |
| 587 | |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 588 | 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] | 589 | { |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 590 | struct pn533_std_frame *frame = _frame; |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 591 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 592 | frame->preamble = 0; |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 593 | frame->start_frame = cpu_to_be16(PN533_STD_FRAME_SOF); |
| 594 | PN533_STD_FRAME_IDENTIFIER(frame) = PN533_STD_FRAME_DIR_OUT; |
Olivier Guiter | 1575b9d | 2013-06-13 15:43:27 +0200 | [diff] [blame] | 595 | PN533_FRAME_CMD(frame) = cmd_code; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 596 | frame->datalen = 2; |
| 597 | } |
| 598 | |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 599 | static void pn533_std_tx_frame_finish(void *_frame) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 600 | { |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 601 | struct pn533_std_frame *frame = _frame; |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 602 | |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 603 | frame->datalen_checksum = pn533_std_checksum(frame->datalen); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 604 | |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 605 | PN533_STD_FRAME_CHECKSUM(frame) = |
| 606 | pn533_std_data_checksum(frame->data, frame->datalen); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 607 | |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 608 | PN533_STD_FRAME_POSTAMBLE(frame) = 0; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 609 | } |
| 610 | |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 611 | static void pn533_std_tx_update_payload_len(void *_frame, int len) |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 612 | { |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 613 | struct pn533_std_frame *frame = _frame; |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 614 | |
| 615 | frame->datalen += len; |
| 616 | } |
| 617 | |
Olivier Guiter | 56a63c82 | 2013-06-13 13:43:29 +0000 | [diff] [blame] | 618 | 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] | 619 | { |
| 620 | u8 checksum; |
Olivier Guiter | 1575b9d | 2013-06-13 15:43:27 +0200 | [diff] [blame] | 621 | struct pn533_std_frame *stdf = _frame; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 622 | |
Olivier Guiter | 1575b9d | 2013-06-13 15:43:27 +0200 | [diff] [blame] | 623 | if (stdf->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF)) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 624 | return false; |
| 625 | |
Olivier Guiter | 1575b9d | 2013-06-13 15:43:27 +0200 | [diff] [blame] | 626 | if (likely(!PN533_STD_IS_EXTENDED(stdf))) { |
| 627 | /* Standard frame code */ |
Olivier Guiter | 56a63c82 | 2013-06-13 13:43:29 +0000 | [diff] [blame] | 628 | dev->ops->rx_header_len = PN533_STD_FRAME_HEADER_LEN; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 629 | |
Olivier Guiter | 1575b9d | 2013-06-13 15:43:27 +0200 | [diff] [blame] | 630 | checksum = pn533_std_checksum(stdf->datalen); |
| 631 | if (checksum != stdf->datalen_checksum) |
| 632 | return false; |
| 633 | |
| 634 | checksum = pn533_std_data_checksum(stdf->data, stdf->datalen); |
| 635 | if (checksum != PN533_STD_FRAME_CHECKSUM(stdf)) |
| 636 | return false; |
| 637 | } else { |
| 638 | /* Extended */ |
| 639 | struct pn533_ext_frame *eif = _frame; |
| 640 | |
Olivier Guiter | 56a63c82 | 2013-06-13 13:43:29 +0000 | [diff] [blame] | 641 | dev->ops->rx_header_len = PN533_EXT_FRAME_HEADER_LEN; |
| 642 | |
Olivier Guiter | 1575b9d | 2013-06-13 15:43:27 +0200 | [diff] [blame] | 643 | checksum = pn533_ext_checksum(be16_to_cpu(eif->datalen)); |
| 644 | if (checksum != eif->datalen_checksum) |
| 645 | return false; |
| 646 | |
| 647 | /* check data checksum */ |
| 648 | checksum = pn533_std_data_checksum(eif->data, |
| 649 | be16_to_cpu(eif->datalen)); |
| 650 | if (checksum != PN533_EXT_FRAME_CHECKSUM(eif)) |
| 651 | return false; |
| 652 | } |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 653 | |
| 654 | return true; |
| 655 | } |
| 656 | |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 657 | static bool pn533_std_rx_frame_is_ack(struct pn533_std_frame *frame) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 658 | { |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 659 | if (frame->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF)) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 660 | return false; |
| 661 | |
| 662 | if (frame->datalen != 0 || frame->datalen_checksum != 0xFF) |
| 663 | return false; |
| 664 | |
| 665 | return true; |
| 666 | } |
| 667 | |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 668 | static inline int pn533_std_rx_frame_size(void *frame) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 669 | { |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 670 | struct pn533_std_frame *f = frame; |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 671 | |
Olivier Guiter | 1575b9d | 2013-06-13 15:43:27 +0200 | [diff] [blame] | 672 | /* check for Extended Information frame */ |
| 673 | if (PN533_STD_IS_EXTENDED(f)) { |
| 674 | struct pn533_ext_frame *eif = frame; |
| 675 | |
| 676 | return sizeof(struct pn533_ext_frame) |
| 677 | + be16_to_cpu(eif->datalen) + PN533_STD_FRAME_TAIL_LEN; |
| 678 | } |
| 679 | |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 680 | return sizeof(struct pn533_std_frame) + f->datalen + |
| 681 | PN533_STD_FRAME_TAIL_LEN; |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 682 | } |
| 683 | |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 684 | static u8 pn533_std_get_cmd_code(void *frame) |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 685 | { |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 686 | struct pn533_std_frame *f = frame; |
Olivier Guiter | 1575b9d | 2013-06-13 15:43:27 +0200 | [diff] [blame] | 687 | struct pn533_ext_frame *eif = frame; |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 688 | |
Olivier Guiter | 1575b9d | 2013-06-13 15:43:27 +0200 | [diff] [blame] | 689 | if (PN533_STD_IS_EXTENDED(f)) |
| 690 | return PN533_FRAME_CMD(eif); |
| 691 | else |
| 692 | return PN533_FRAME_CMD(f); |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 693 | } |
| 694 | |
Waldemar Rymarkiewicz | ef3d56e | 2013-01-16 15:51:51 +0100 | [diff] [blame] | 695 | static struct pn533_frame_ops pn533_std_frame_ops = { |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 696 | .tx_frame_init = pn533_std_tx_frame_init, |
| 697 | .tx_frame_finish = pn533_std_tx_frame_finish, |
| 698 | .tx_update_payload_len = pn533_std_tx_update_payload_len, |
| 699 | .tx_header_len = PN533_STD_FRAME_HEADER_LEN, |
| 700 | .tx_tail_len = PN533_STD_FRAME_TAIL_LEN, |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 701 | |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 702 | .rx_is_frame_valid = pn533_std_rx_frame_is_valid, |
| 703 | .rx_frame_size = pn533_std_rx_frame_size, |
| 704 | .rx_header_len = PN533_STD_FRAME_HEADER_LEN, |
| 705 | .rx_tail_len = PN533_STD_FRAME_TAIL_LEN, |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 706 | |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 707 | .max_payload_len = PN533_STD_FRAME_MAX_PAYLOAD_LEN, |
| 708 | .get_cmd_code = pn533_std_get_cmd_code, |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 709 | }; |
| 710 | |
| 711 | static bool pn533_rx_frame_is_cmd_response(struct pn533 *dev, void *frame) |
| 712 | { |
Waldemar Rymarkiewicz | 2c206fb | 2013-04-03 08:02:05 +0200 | [diff] [blame] | 713 | return (dev->ops->get_cmd_code(frame) == |
Waldemar Rymarkiewicz | 4b2a953 | 2013-04-03 08:02:06 +0200 | [diff] [blame] | 714 | PN533_CMD_RESPONSE(dev->cmd->code)); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 715 | } |
| 716 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 717 | static void pn533_recv_response(struct urb *urb) |
| 718 | { |
| 719 | struct pn533 *dev = urb->context; |
Waldemar Rymarkiewicz | f87bc9f | 2013-04-03 08:02:11 +0200 | [diff] [blame] | 720 | struct pn533_cmd *cmd = dev->cmd; |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 721 | u8 *in_frame; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 722 | |
Waldemar Rymarkiewicz | f87bc9f | 2013-04-03 08:02:11 +0200 | [diff] [blame] | 723 | cmd->status = urb->status; |
| 724 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 725 | switch (urb->status) { |
| 726 | case 0: |
Waldemar Rymarkiewicz | f8f9917 | 2012-12-10 14:42:54 +0100 | [diff] [blame] | 727 | break; /* success */ |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 728 | case -ECONNRESET: |
| 729 | case -ENOENT: |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 730 | dev_dbg(&dev->interface->dev, |
| 731 | "The urb has been canceled (status %d)\n", |
| 732 | urb->status); |
Samuel Ortiz | 4849f85 | 2012-04-10 19:43:17 +0200 | [diff] [blame] | 733 | goto sched_wq; |
Waldemar Rymarkiewicz | f8f9917 | 2012-12-10 14:42:54 +0100 | [diff] [blame] | 734 | case -ESHUTDOWN: |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 735 | default: |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 736 | nfc_err(&dev->interface->dev, |
| 737 | "Urb failure (status %d)\n", urb->status); |
Samuel Ortiz | 4849f85 | 2012-04-10 19:43:17 +0200 | [diff] [blame] | 738 | goto sched_wq; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | in_frame = dev->in_urb->transfer_buffer; |
| 742 | |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 743 | dev_dbg(&dev->interface->dev, "Received a frame\n"); |
Samuel Ortiz | e279f84 | 2013-03-20 11:27:57 +0100 | [diff] [blame] | 744 | print_hex_dump_debug("PN533 RX: ", DUMP_PREFIX_NONE, 16, 1, in_frame, |
| 745 | dev->ops->rx_frame_size(in_frame), false); |
Waldemar Rymarkiewicz | 99e591b | 2012-12-10 14:42:47 +0100 | [diff] [blame] | 746 | |
Olivier Guiter | 56a63c82 | 2013-06-13 13:43:29 +0000 | [diff] [blame] | 747 | if (!dev->ops->rx_is_frame_valid(in_frame, dev)) { |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 748 | nfc_err(&dev->interface->dev, "Received an invalid frame\n"); |
Waldemar Rymarkiewicz | f87bc9f | 2013-04-03 08:02:11 +0200 | [diff] [blame] | 749 | cmd->status = -EIO; |
Samuel Ortiz | 4849f85 | 2012-04-10 19:43:17 +0200 | [diff] [blame] | 750 | goto sched_wq; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 751 | } |
| 752 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 753 | if (!pn533_rx_frame_is_cmd_response(dev, in_frame)) { |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 754 | nfc_err(&dev->interface->dev, |
| 755 | "It it not the response to the last command\n"); |
Waldemar Rymarkiewicz | f87bc9f | 2013-04-03 08:02:11 +0200 | [diff] [blame] | 756 | cmd->status = -EIO; |
Samuel Ortiz | 4849f85 | 2012-04-10 19:43:17 +0200 | [diff] [blame] | 757 | goto sched_wq; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 758 | } |
| 759 | |
Samuel Ortiz | 4849f85 | 2012-04-10 19:43:17 +0200 | [diff] [blame] | 760 | sched_wq: |
Samuel Ortiz | 5d50b36 | 2012-08-17 23:47:54 +0200 | [diff] [blame] | 761 | queue_work(dev->wq, &dev->cmd_complete_work); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | static int pn533_submit_urb_for_response(struct pn533 *dev, gfp_t flags) |
| 765 | { |
| 766 | dev->in_urb->complete = pn533_recv_response; |
| 767 | |
| 768 | return usb_submit_urb(dev->in_urb, flags); |
| 769 | } |
| 770 | |
| 771 | static void pn533_recv_ack(struct urb *urb) |
| 772 | { |
| 773 | struct pn533 *dev = urb->context; |
Waldemar Rymarkiewicz | f87bc9f | 2013-04-03 08:02:11 +0200 | [diff] [blame] | 774 | struct pn533_cmd *cmd = dev->cmd; |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 775 | struct pn533_std_frame *in_frame; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 776 | int rc; |
| 777 | |
Waldemar Rymarkiewicz | f87bc9f | 2013-04-03 08:02:11 +0200 | [diff] [blame] | 778 | cmd->status = urb->status; |
| 779 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 780 | switch (urb->status) { |
| 781 | case 0: |
Waldemar Rymarkiewicz | f8f9917 | 2012-12-10 14:42:54 +0100 | [diff] [blame] | 782 | break; /* success */ |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 783 | case -ECONNRESET: |
| 784 | case -ENOENT: |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 785 | dev_dbg(&dev->interface->dev, |
| 786 | "The urb has been stopped (status %d)\n", |
| 787 | urb->status); |
Samuel Ortiz | 4849f85 | 2012-04-10 19:43:17 +0200 | [diff] [blame] | 788 | goto sched_wq; |
Waldemar Rymarkiewicz | f8f9917 | 2012-12-10 14:42:54 +0100 | [diff] [blame] | 789 | case -ESHUTDOWN: |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 790 | default: |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 791 | nfc_err(&dev->interface->dev, |
| 792 | "Urb failure (status %d)\n", urb->status); |
Samuel Ortiz | 4849f85 | 2012-04-10 19:43:17 +0200 | [diff] [blame] | 793 | goto sched_wq; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 794 | } |
| 795 | |
| 796 | in_frame = dev->in_urb->transfer_buffer; |
| 797 | |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 798 | if (!pn533_std_rx_frame_is_ack(in_frame)) { |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 799 | nfc_err(&dev->interface->dev, "Received an invalid ack\n"); |
Waldemar Rymarkiewicz | f87bc9f | 2013-04-03 08:02:11 +0200 | [diff] [blame] | 800 | cmd->status = -EIO; |
Samuel Ortiz | 4849f85 | 2012-04-10 19:43:17 +0200 | [diff] [blame] | 801 | goto sched_wq; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 802 | } |
| 803 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 804 | rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC); |
| 805 | if (rc) { |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 806 | nfc_err(&dev->interface->dev, |
| 807 | "usb_submit_urb failed with result %d\n", rc); |
Waldemar Rymarkiewicz | f87bc9f | 2013-04-03 08:02:11 +0200 | [diff] [blame] | 808 | cmd->status = rc; |
Samuel Ortiz | 4849f85 | 2012-04-10 19:43:17 +0200 | [diff] [blame] | 809 | goto sched_wq; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | return; |
| 813 | |
Samuel Ortiz | 4849f85 | 2012-04-10 19:43:17 +0200 | [diff] [blame] | 814 | sched_wq: |
Samuel Ortiz | 5d50b36 | 2012-08-17 23:47:54 +0200 | [diff] [blame] | 815 | queue_work(dev->wq, &dev->cmd_complete_work); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | static int pn533_submit_urb_for_ack(struct pn533 *dev, gfp_t flags) |
| 819 | { |
| 820 | dev->in_urb->complete = pn533_recv_ack; |
| 821 | |
| 822 | return usb_submit_urb(dev->in_urb, flags); |
| 823 | } |
| 824 | |
| 825 | static int pn533_send_ack(struct pn533 *dev, gfp_t flags) |
| 826 | { |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 827 | u8 ack[PN533_STD_FRAME_ACK_SIZE] = {0x00, 0x00, 0xff, 0x00, 0xff, 0x00}; |
Waldemar Rymarkiewicz | 5b5a443 | 2012-12-10 14:42:56 +0100 | [diff] [blame] | 828 | /* spec 7.1.1.3: Preamble, SoPC (2), ACK Code (2), Postamble */ |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 829 | int rc; |
| 830 | |
Waldemar Rymarkiewicz | 5b5a443 | 2012-12-10 14:42:56 +0100 | [diff] [blame] | 831 | dev->out_urb->transfer_buffer = ack; |
| 832 | dev->out_urb->transfer_buffer_length = sizeof(ack); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 833 | rc = usb_submit_urb(dev->out_urb, flags); |
| 834 | |
| 835 | return rc; |
| 836 | } |
| 837 | |
Waldemar Rymarkiewicz | e8f4053 | 2012-12-10 14:42:57 +0100 | [diff] [blame] | 838 | static int __pn533_send_frame_async(struct pn533 *dev, |
| 839 | struct sk_buff *out, |
| 840 | struct sk_buff *in, |
Waldemar Rymarkiewicz | ddf19d2 | 2013-04-03 08:02:08 +0200 | [diff] [blame] | 841 | int in_len) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 842 | { |
| 843 | int rc; |
| 844 | |
Waldemar Rymarkiewicz | e8f4053 | 2012-12-10 14:42:57 +0100 | [diff] [blame] | 845 | dev->out_urb->transfer_buffer = out->data; |
| 846 | dev->out_urb->transfer_buffer_length = out->len; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 847 | |
Waldemar Rymarkiewicz | e8f4053 | 2012-12-10 14:42:57 +0100 | [diff] [blame] | 848 | dev->in_urb->transfer_buffer = in->data; |
| 849 | dev->in_urb->transfer_buffer_length = in_len; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 850 | |
Samuel Ortiz | e279f84 | 2013-03-20 11:27:57 +0100 | [diff] [blame] | 851 | print_hex_dump_debug("PN533 TX: ", DUMP_PREFIX_NONE, 16, 1, |
| 852 | out->data, out->len, false); |
Waldemar Rymarkiewicz | 99e591b | 2012-12-10 14:42:47 +0100 | [diff] [blame] | 853 | |
Waldemar Rymarkiewicz | d94ea4f | 2012-11-26 14:18:33 +0100 | [diff] [blame] | 854 | rc = usb_submit_urb(dev->out_urb, GFP_KERNEL); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 855 | if (rc) |
| 856 | return rc; |
| 857 | |
Waldemar Rymarkiewicz | 5852037 | 2013-04-03 08:02:12 +0200 | [diff] [blame] | 858 | if (dev->protocol_type == PN533_PROTO_REQ_RESP) { |
| 859 | /* request for response for sent packet directly */ |
| 860 | rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC); |
| 861 | if (rc) |
| 862 | goto error; |
| 863 | } else if (dev->protocol_type == PN533_PROTO_REQ_ACK_RESP) { |
| 864 | /* request for ACK if that's the case */ |
| 865 | rc = pn533_submit_urb_for_ack(dev, GFP_KERNEL); |
| 866 | if (rc) |
| 867 | goto error; |
| 868 | } |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 869 | |
| 870 | return 0; |
| 871 | |
| 872 | error: |
| 873 | usb_unlink_urb(dev->out_urb); |
| 874 | return rc; |
| 875 | } |
| 876 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 877 | static void pn533_build_cmd_frame(struct pn533 *dev, u8 cmd_code, |
| 878 | struct sk_buff *skb) |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 879 | { |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 880 | /* payload is already there, just update datalen */ |
| 881 | int payload_len = skb->len; |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 882 | struct pn533_frame_ops *ops = dev->ops; |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 883 | |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 884 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 885 | skb_push(skb, ops->tx_header_len); |
| 886 | skb_put(skb, ops->tx_tail_len); |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 887 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 888 | ops->tx_frame_init(skb->data, cmd_code); |
| 889 | ops->tx_update_payload_len(skb->data, payload_len); |
| 890 | ops->tx_frame_finish(skb->data); |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 891 | } |
| 892 | |
Waldemar Rymarkiewicz | ddf19d2 | 2013-04-03 08:02:08 +0200 | [diff] [blame] | 893 | static int pn533_send_async_complete(struct pn533 *dev) |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 894 | { |
Waldemar Rymarkiewicz | ddf19d2 | 2013-04-03 08:02:08 +0200 | [diff] [blame] | 895 | struct pn533_cmd *cmd = dev->cmd; |
Waldemar Rymarkiewicz | f87bc9f | 2013-04-03 08:02:11 +0200 | [diff] [blame] | 896 | int status = cmd->status; |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 897 | |
Waldemar Rymarkiewicz | 4231604 | 2013-04-03 08:02:04 +0200 | [diff] [blame] | 898 | struct sk_buff *req = cmd->req; |
| 899 | struct sk_buff *resp = cmd->resp; |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 900 | |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 901 | int rc; |
| 902 | |
| 903 | dev_kfree_skb(req); |
| 904 | |
Waldemar Rymarkiewicz | 0c33d26 | 2012-12-10 14:42:59 +0100 | [diff] [blame] | 905 | if (status < 0) { |
Waldemar Rymarkiewicz | 4231604 | 2013-04-03 08:02:04 +0200 | [diff] [blame] | 906 | rc = cmd->complete_cb(dev, cmd->complete_cb_context, |
| 907 | ERR_PTR(status)); |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 908 | dev_kfree_skb(resp); |
Waldemar Rymarkiewicz | 2c206fb | 2013-04-03 08:02:05 +0200 | [diff] [blame] | 909 | goto done; |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 910 | } |
| 911 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 912 | skb_put(resp, dev->ops->rx_frame_size(resp->data)); |
| 913 | skb_pull(resp, dev->ops->rx_header_len); |
| 914 | skb_trim(resp, resp->len - dev->ops->rx_tail_len); |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 915 | |
Waldemar Rymarkiewicz | 4231604 | 2013-04-03 08:02:04 +0200 | [diff] [blame] | 916 | rc = cmd->complete_cb(dev, cmd->complete_cb_context, resp); |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 917 | |
Waldemar Rymarkiewicz | 2c206fb | 2013-04-03 08:02:05 +0200 | [diff] [blame] | 918 | done: |
Waldemar Rymarkiewicz | 4231604 | 2013-04-03 08:02:04 +0200 | [diff] [blame] | 919 | kfree(cmd); |
Waldemar Rymarkiewicz | 2c206fb | 2013-04-03 08:02:05 +0200 | [diff] [blame] | 920 | dev->cmd = NULL; |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 921 | return rc; |
| 922 | } |
| 923 | |
| 924 | static int __pn533_send_async(struct pn533 *dev, u8 cmd_code, |
| 925 | struct sk_buff *req, struct sk_buff *resp, |
| 926 | int resp_len, |
| 927 | pn533_send_async_complete_t complete_cb, |
| 928 | void *complete_cb_context) |
| 929 | { |
| 930 | struct pn533_cmd *cmd; |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 931 | int rc = 0; |
| 932 | |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 933 | dev_dbg(&dev->interface->dev, "Sending command 0x%x\n", cmd_code); |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 934 | |
Waldemar Rymarkiewicz | 4231604 | 2013-04-03 08:02:04 +0200 | [diff] [blame] | 935 | cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); |
| 936 | if (!cmd) |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 937 | return -ENOMEM; |
| 938 | |
Waldemar Rymarkiewicz | 4b2a953 | 2013-04-03 08:02:06 +0200 | [diff] [blame] | 939 | cmd->code = cmd_code; |
Waldemar Rymarkiewicz | 4231604 | 2013-04-03 08:02:04 +0200 | [diff] [blame] | 940 | cmd->req = req; |
| 941 | cmd->resp = resp; |
| 942 | cmd->resp_len = resp_len; |
| 943 | cmd->complete_cb = complete_cb; |
| 944 | cmd->complete_cb_context = complete_cb_context; |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 945 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 946 | pn533_build_cmd_frame(dev, cmd_code, req); |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 947 | |
| 948 | mutex_lock(&dev->cmd_lock); |
| 949 | |
| 950 | if (!dev->cmd_pending) { |
Waldemar Rymarkiewicz | ddf19d2 | 2013-04-03 08:02:08 +0200 | [diff] [blame] | 951 | rc = __pn533_send_frame_async(dev, req, resp, resp_len); |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 952 | if (rc) |
| 953 | goto error; |
| 954 | |
| 955 | dev->cmd_pending = 1; |
Waldemar Rymarkiewicz | 2c206fb | 2013-04-03 08:02:05 +0200 | [diff] [blame] | 956 | dev->cmd = cmd; |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 957 | goto unlock; |
| 958 | } |
| 959 | |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 960 | dev_dbg(&dev->interface->dev, "%s Queueing command 0x%x\n", |
| 961 | __func__, cmd_code); |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 962 | |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 963 | INIT_LIST_HEAD(&cmd->queue); |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 964 | list_add_tail(&cmd->queue, &dev->cmd_queue); |
| 965 | |
| 966 | goto unlock; |
| 967 | |
| 968 | error: |
Waldemar Rymarkiewicz | 4231604 | 2013-04-03 08:02:04 +0200 | [diff] [blame] | 969 | kfree(cmd); |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 970 | unlock: |
| 971 | mutex_unlock(&dev->cmd_lock); |
| 972 | return rc; |
| 973 | } |
| 974 | |
Waldemar Rymarkiewicz | 15461ae | 2012-11-26 14:18:35 +0100 | [diff] [blame] | 975 | static int pn533_send_data_async(struct pn533 *dev, u8 cmd_code, |
| 976 | struct sk_buff *req, |
| 977 | pn533_send_async_complete_t complete_cb, |
| 978 | void *complete_cb_context) |
| 979 | { |
| 980 | struct sk_buff *resp; |
| 981 | int rc; |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 982 | int resp_len = dev->ops->rx_header_len + |
| 983 | dev->ops->max_payload_len + |
| 984 | dev->ops->rx_tail_len; |
Waldemar Rymarkiewicz | 15461ae | 2012-11-26 14:18:35 +0100 | [diff] [blame] | 985 | |
Waldemar Rymarkiewicz | 15461ae | 2012-11-26 14:18:35 +0100 | [diff] [blame] | 986 | resp = nfc_alloc_recv_skb(resp_len, GFP_KERNEL); |
| 987 | if (!resp) |
| 988 | return -ENOMEM; |
| 989 | |
| 990 | rc = __pn533_send_async(dev, cmd_code, req, resp, resp_len, complete_cb, |
| 991 | complete_cb_context); |
| 992 | if (rc) |
| 993 | dev_kfree_skb(resp); |
| 994 | |
| 995 | return rc; |
| 996 | } |
| 997 | |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 998 | static int pn533_send_cmd_async(struct pn533 *dev, u8 cmd_code, |
| 999 | struct sk_buff *req, |
| 1000 | pn533_send_async_complete_t complete_cb, |
| 1001 | void *complete_cb_context) |
| 1002 | { |
| 1003 | struct sk_buff *resp; |
| 1004 | int rc; |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 1005 | int resp_len = dev->ops->rx_header_len + |
| 1006 | dev->ops->max_payload_len + |
| 1007 | dev->ops->rx_tail_len; |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 1008 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 1009 | resp = alloc_skb(resp_len, GFP_KERNEL); |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 1010 | if (!resp) |
| 1011 | return -ENOMEM; |
| 1012 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 1013 | rc = __pn533_send_async(dev, cmd_code, req, resp, resp_len, complete_cb, |
| 1014 | complete_cb_context); |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 1015 | if (rc) |
| 1016 | dev_kfree_skb(resp); |
| 1017 | |
| 1018 | return rc; |
| 1019 | } |
| 1020 | |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 1021 | /* |
| 1022 | * pn533_send_cmd_direct_async |
| 1023 | * |
| 1024 | * The function sends a piority cmd directly to the chip omiting the cmd |
| 1025 | * queue. It's intended to be used by chaining mechanism of received responses |
| 1026 | * where the host has to request every single chunk of data before scheduling |
| 1027 | * next cmd from the queue. |
| 1028 | */ |
| 1029 | static int pn533_send_cmd_direct_async(struct pn533 *dev, u8 cmd_code, |
| 1030 | struct sk_buff *req, |
| 1031 | pn533_send_async_complete_t complete_cb, |
| 1032 | void *complete_cb_context) |
| 1033 | { |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 1034 | struct sk_buff *resp; |
Waldemar Rymarkiewicz | 4231604 | 2013-04-03 08:02:04 +0200 | [diff] [blame] | 1035 | struct pn533_cmd *cmd; |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 1036 | int rc; |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 1037 | int resp_len = dev->ops->rx_header_len + |
| 1038 | dev->ops->max_payload_len + |
| 1039 | dev->ops->rx_tail_len; |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 1040 | |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 1041 | resp = alloc_skb(resp_len, GFP_KERNEL); |
| 1042 | if (!resp) |
| 1043 | return -ENOMEM; |
| 1044 | |
Waldemar Rymarkiewicz | 4231604 | 2013-04-03 08:02:04 +0200 | [diff] [blame] | 1045 | cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); |
| 1046 | if (!cmd) { |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 1047 | dev_kfree_skb(resp); |
| 1048 | return -ENOMEM; |
| 1049 | } |
| 1050 | |
Waldemar Rymarkiewicz | 4b2a953 | 2013-04-03 08:02:06 +0200 | [diff] [blame] | 1051 | cmd->code = cmd_code; |
Waldemar Rymarkiewicz | 4231604 | 2013-04-03 08:02:04 +0200 | [diff] [blame] | 1052 | cmd->req = req; |
| 1053 | cmd->resp = resp; |
| 1054 | cmd->resp_len = resp_len; |
| 1055 | cmd->complete_cb = complete_cb; |
| 1056 | cmd->complete_cb_context = complete_cb_context; |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 1057 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 1058 | pn533_build_cmd_frame(dev, cmd_code, req); |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 1059 | |
Waldemar Rymarkiewicz | ddf19d2 | 2013-04-03 08:02:08 +0200 | [diff] [blame] | 1060 | rc = __pn533_send_frame_async(dev, req, resp, resp_len); |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 1061 | if (rc < 0) { |
| 1062 | dev_kfree_skb(resp); |
Waldemar Rymarkiewicz | 4231604 | 2013-04-03 08:02:04 +0200 | [diff] [blame] | 1063 | kfree(cmd); |
Waldemar Rymarkiewicz | 2c206fb | 2013-04-03 08:02:05 +0200 | [diff] [blame] | 1064 | } else { |
| 1065 | dev->cmd = cmd; |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 1066 | } |
| 1067 | |
| 1068 | return rc; |
| 1069 | } |
| 1070 | |
Waldemar Rymarkiewicz | c79490e | 2013-04-03 08:02:09 +0200 | [diff] [blame] | 1071 | static void pn533_wq_cmd_complete(struct work_struct *work) |
| 1072 | { |
| 1073 | struct pn533 *dev = container_of(work, struct pn533, cmd_complete_work); |
| 1074 | int rc; |
| 1075 | |
| 1076 | rc = pn533_send_async_complete(dev); |
| 1077 | if (rc != -EINPROGRESS) |
| 1078 | queue_work(dev->wq, &dev->cmd_work); |
| 1079 | } |
| 1080 | |
Samuel Ortiz | 5d50b36 | 2012-08-17 23:47:54 +0200 | [diff] [blame] | 1081 | static void pn533_wq_cmd(struct work_struct *work) |
| 1082 | { |
| 1083 | struct pn533 *dev = container_of(work, struct pn533, cmd_work); |
| 1084 | struct pn533_cmd *cmd; |
Waldemar Rymarkiewicz | 0ce1fbd | 2013-04-03 08:02:03 +0200 | [diff] [blame] | 1085 | int rc; |
Samuel Ortiz | 5d50b36 | 2012-08-17 23:47:54 +0200 | [diff] [blame] | 1086 | |
| 1087 | mutex_lock(&dev->cmd_lock); |
| 1088 | |
| 1089 | if (list_empty(&dev->cmd_queue)) { |
| 1090 | dev->cmd_pending = 0; |
| 1091 | mutex_unlock(&dev->cmd_lock); |
| 1092 | return; |
| 1093 | } |
| 1094 | |
| 1095 | cmd = list_first_entry(&dev->cmd_queue, struct pn533_cmd, queue); |
| 1096 | |
Szymon Janc | 60ad07a | 2012-10-25 17:29:45 +0200 | [diff] [blame] | 1097 | list_del(&cmd->queue); |
| 1098 | |
Samuel Ortiz | 5d50b36 | 2012-08-17 23:47:54 +0200 | [diff] [blame] | 1099 | mutex_unlock(&dev->cmd_lock); |
| 1100 | |
Waldemar Rymarkiewicz | ddf19d2 | 2013-04-03 08:02:08 +0200 | [diff] [blame] | 1101 | rc = __pn533_send_frame_async(dev, cmd->req, cmd->resp, cmd->resp_len); |
Waldemar Rymarkiewicz | 0ce1fbd | 2013-04-03 08:02:03 +0200 | [diff] [blame] | 1102 | if (rc < 0) { |
| 1103 | dev_kfree_skb(cmd->req); |
| 1104 | dev_kfree_skb(cmd->resp); |
Waldemar Rymarkiewicz | 4231604 | 2013-04-03 08:02:04 +0200 | [diff] [blame] | 1105 | kfree(cmd); |
Waldemar Rymarkiewicz | 2c206fb | 2013-04-03 08:02:05 +0200 | [diff] [blame] | 1106 | return; |
Waldemar Rymarkiewicz | 0ce1fbd | 2013-04-03 08:02:03 +0200 | [diff] [blame] | 1107 | } |
Waldemar Rymarkiewicz | 2c206fb | 2013-04-03 08:02:05 +0200 | [diff] [blame] | 1108 | |
| 1109 | dev->cmd = cmd; |
Samuel Ortiz | 5d50b36 | 2012-08-17 23:47:54 +0200 | [diff] [blame] | 1110 | } |
| 1111 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1112 | struct pn533_sync_cmd_response { |
Waldemar Rymarkiewicz | 94c5c15 | 2012-11-26 14:18:36 +0100 | [diff] [blame] | 1113 | struct sk_buff *resp; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1114 | struct completion done; |
| 1115 | }; |
| 1116 | |
Waldemar Rymarkiewicz | 94c5c15 | 2012-11-26 14:18:36 +0100 | [diff] [blame] | 1117 | static int pn533_send_sync_complete(struct pn533 *dev, void *_arg, |
| 1118 | struct sk_buff *resp) |
| 1119 | { |
| 1120 | struct pn533_sync_cmd_response *arg = _arg; |
| 1121 | |
Waldemar Rymarkiewicz | 94c5c15 | 2012-11-26 14:18:36 +0100 | [diff] [blame] | 1122 | arg->resp = resp; |
| 1123 | complete(&arg->done); |
| 1124 | |
| 1125 | return 0; |
| 1126 | } |
| 1127 | |
| 1128 | /* pn533_send_cmd_sync |
| 1129 | * |
| 1130 | * Please note the req parameter is freed inside the function to |
| 1131 | * limit a number of return value interpretations by the caller. |
| 1132 | * |
| 1133 | * 1. negative in case of error during TX path -> req should be freed |
| 1134 | * |
| 1135 | * 2. negative in case of error during RX path -> req should not be freed |
| 1136 | * as it's been already freed at the begining of RX path by |
| 1137 | * async_complete_cb. |
| 1138 | * |
| 1139 | * 3. valid pointer in case of succesfult RX path |
| 1140 | * |
| 1141 | * A caller has to check a return value with IS_ERR macro. If the test pass, |
| 1142 | * the returned pointer is valid. |
| 1143 | * |
| 1144 | * */ |
| 1145 | static struct sk_buff *pn533_send_cmd_sync(struct pn533 *dev, u8 cmd_code, |
| 1146 | struct sk_buff *req) |
| 1147 | { |
| 1148 | int rc; |
| 1149 | struct pn533_sync_cmd_response arg; |
| 1150 | |
Waldemar Rymarkiewicz | 94c5c15 | 2012-11-26 14:18:36 +0100 | [diff] [blame] | 1151 | init_completion(&arg.done); |
| 1152 | |
| 1153 | rc = pn533_send_cmd_async(dev, cmd_code, req, |
| 1154 | pn533_send_sync_complete, &arg); |
| 1155 | if (rc) { |
| 1156 | dev_kfree_skb(req); |
| 1157 | return ERR_PTR(rc); |
| 1158 | } |
| 1159 | |
| 1160 | wait_for_completion(&arg.done); |
| 1161 | |
| 1162 | return arg.resp; |
| 1163 | } |
| 1164 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1165 | static void pn533_send_complete(struct urb *urb) |
| 1166 | { |
| 1167 | struct pn533 *dev = urb->context; |
| 1168 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1169 | switch (urb->status) { |
| 1170 | case 0: |
Waldemar Rymarkiewicz | f8f9917 | 2012-12-10 14:42:54 +0100 | [diff] [blame] | 1171 | break; /* success */ |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1172 | case -ECONNRESET: |
| 1173 | case -ENOENT: |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 1174 | dev_dbg(&dev->interface->dev, |
| 1175 | "The urb has been stopped (status %d)\n", |
| 1176 | urb->status); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1177 | break; |
Waldemar Rymarkiewicz | f8f9917 | 2012-12-10 14:42:54 +0100 | [diff] [blame] | 1178 | case -ESHUTDOWN: |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1179 | default: |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 1180 | nfc_err(&dev->interface->dev, "Urb failure (status %d)\n", |
| 1181 | urb->status); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1182 | } |
| 1183 | } |
| 1184 | |
Waldemar Rymarkiewicz | 10cff29 | 2013-04-03 08:02:14 +0200 | [diff] [blame] | 1185 | static void pn533_abort_cmd(struct pn533 *dev, gfp_t flags) |
| 1186 | { |
| 1187 | /* ACR122U does not support any command which aborts last |
| 1188 | * issued command i.e. as ACK for standard PN533. Additionally, |
| 1189 | * it behaves stange, sending broken or incorrect responses, |
| 1190 | * when we cancel urb before the chip will send response. |
| 1191 | */ |
| 1192 | if (dev->device_type == PN533_DEVICE_ACR122U) |
| 1193 | return; |
| 1194 | |
| 1195 | /* An ack will cancel the last issued command */ |
| 1196 | pn533_send_ack(dev, flags); |
| 1197 | |
| 1198 | /* cancel the urb request */ |
| 1199 | usb_kill_urb(dev->in_urb); |
| 1200 | } |
| 1201 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 1202 | 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] | 1203 | { |
| 1204 | struct sk_buff *skb; |
| 1205 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 1206 | skb = alloc_skb(dev->ops->tx_header_len + |
Waldemar Rymarkiewicz | d22b2db | 2012-11-26 14:18:37 +0100 | [diff] [blame] | 1207 | size + |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 1208 | dev->ops->tx_tail_len, GFP_KERNEL); |
Waldemar Rymarkiewicz | d22b2db | 2012-11-26 14:18:37 +0100 | [diff] [blame] | 1209 | |
| 1210 | if (skb) |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 1211 | skb_reserve(skb, dev->ops->tx_header_len); |
Waldemar Rymarkiewicz | d22b2db | 2012-11-26 14:18:37 +0100 | [diff] [blame] | 1212 | |
| 1213 | return skb; |
| 1214 | } |
| 1215 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1216 | struct pn533_target_type_a { |
| 1217 | __be16 sens_res; |
| 1218 | u8 sel_res; |
| 1219 | u8 nfcid_len; |
| 1220 | u8 nfcid_data[]; |
| 1221 | } __packed; |
| 1222 | |
| 1223 | |
| 1224 | #define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6)) |
| 1225 | #define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0)) |
| 1226 | #define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8)) |
| 1227 | |
| 1228 | #define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00 |
| 1229 | #define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C |
| 1230 | |
| 1231 | #define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5) |
| 1232 | #define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2) |
| 1233 | |
| 1234 | #define PN533_TYPE_A_SEL_PROT_MIFARE 0 |
| 1235 | #define PN533_TYPE_A_SEL_PROT_ISO14443 1 |
| 1236 | #define PN533_TYPE_A_SEL_PROT_DEP 2 |
| 1237 | #define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3 |
| 1238 | |
| 1239 | static bool pn533_target_type_a_is_valid(struct pn533_target_type_a *type_a, |
| 1240 | int target_data_len) |
| 1241 | { |
| 1242 | u8 ssd; |
| 1243 | u8 platconf; |
| 1244 | |
| 1245 | if (target_data_len < sizeof(struct pn533_target_type_a)) |
| 1246 | return false; |
| 1247 | |
| 1248 | /* The lenght check of nfcid[] and ats[] are not being performed because |
| 1249 | the values are not being used */ |
| 1250 | |
| 1251 | /* Requirement 4.6.3.3 from NFC Forum Digital Spec */ |
| 1252 | ssd = PN533_TYPE_A_SENS_RES_SSD(type_a->sens_res); |
| 1253 | platconf = PN533_TYPE_A_SENS_RES_PLATCONF(type_a->sens_res); |
| 1254 | |
| 1255 | if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL && |
Waldemar Rymarkiewicz | 5d46774 | 2012-12-10 14:42:51 +0100 | [diff] [blame] | 1256 | platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) || |
| 1257 | (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL && |
| 1258 | platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL)) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1259 | return false; |
| 1260 | |
| 1261 | /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */ |
| 1262 | if (PN533_TYPE_A_SEL_CASCADE(type_a->sel_res) != 0) |
| 1263 | return false; |
| 1264 | |
| 1265 | return true; |
| 1266 | } |
| 1267 | |
| 1268 | static int pn533_target_found_type_a(struct nfc_target *nfc_tgt, u8 *tgt_data, |
| 1269 | int tgt_data_len) |
| 1270 | { |
| 1271 | struct pn533_target_type_a *tgt_type_a; |
| 1272 | |
Waldemar Rymarkiewicz | 37cf4fc | 2012-12-10 14:42:50 +0100 | [diff] [blame] | 1273 | tgt_type_a = (struct pn533_target_type_a *)tgt_data; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1274 | |
| 1275 | if (!pn533_target_type_a_is_valid(tgt_type_a, tgt_data_len)) |
| 1276 | return -EPROTO; |
| 1277 | |
| 1278 | switch (PN533_TYPE_A_SEL_PROT(tgt_type_a->sel_res)) { |
| 1279 | case PN533_TYPE_A_SEL_PROT_MIFARE: |
| 1280 | nfc_tgt->supported_protocols = NFC_PROTO_MIFARE_MASK; |
| 1281 | break; |
| 1282 | case PN533_TYPE_A_SEL_PROT_ISO14443: |
| 1283 | nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK; |
| 1284 | break; |
| 1285 | case PN533_TYPE_A_SEL_PROT_DEP: |
| 1286 | nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK; |
| 1287 | break; |
| 1288 | case PN533_TYPE_A_SEL_PROT_ISO14443_DEP: |
| 1289 | nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK | |
| 1290 | NFC_PROTO_NFC_DEP_MASK; |
| 1291 | break; |
| 1292 | } |
| 1293 | |
| 1294 | nfc_tgt->sens_res = be16_to_cpu(tgt_type_a->sens_res); |
| 1295 | nfc_tgt->sel_res = tgt_type_a->sel_res; |
Samuel Ortiz | c3b1e1e | 2012-03-05 01:03:33 +0100 | [diff] [blame] | 1296 | nfc_tgt->nfcid1_len = tgt_type_a->nfcid_len; |
| 1297 | 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] | 1298 | |
| 1299 | return 0; |
| 1300 | } |
| 1301 | |
| 1302 | struct pn533_target_felica { |
| 1303 | u8 pol_res; |
| 1304 | u8 opcode; |
Samuel Ortiz | 322bce9 | 2013-05-27 15:29:11 +0200 | [diff] [blame] | 1305 | u8 nfcid2[NFC_NFCID2_MAXSIZE]; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1306 | u8 pad[8]; |
| 1307 | /* optional */ |
| 1308 | u8 syst_code[]; |
| 1309 | } __packed; |
| 1310 | |
| 1311 | #define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01 |
| 1312 | #define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE |
| 1313 | |
| 1314 | static bool pn533_target_felica_is_valid(struct pn533_target_felica *felica, |
| 1315 | int target_data_len) |
| 1316 | { |
| 1317 | if (target_data_len < sizeof(struct pn533_target_felica)) |
| 1318 | return false; |
| 1319 | |
| 1320 | if (felica->opcode != PN533_FELICA_OPC_SENSF_RES) |
| 1321 | return false; |
| 1322 | |
| 1323 | return true; |
| 1324 | } |
| 1325 | |
| 1326 | static int pn533_target_found_felica(struct nfc_target *nfc_tgt, u8 *tgt_data, |
| 1327 | int tgt_data_len) |
| 1328 | { |
| 1329 | struct pn533_target_felica *tgt_felica; |
| 1330 | |
Waldemar Rymarkiewicz | 37cf4fc | 2012-12-10 14:42:50 +0100 | [diff] [blame] | 1331 | tgt_felica = (struct pn533_target_felica *)tgt_data; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1332 | |
| 1333 | if (!pn533_target_felica_is_valid(tgt_felica, tgt_data_len)) |
| 1334 | return -EPROTO; |
| 1335 | |
Waldemar Rymarkiewicz | 5d46774 | 2012-12-10 14:42:51 +0100 | [diff] [blame] | 1336 | if ((tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1) && |
| 1337 | (tgt_felica->nfcid2[1] == PN533_FELICA_SENSF_NFCID2_DEP_B2)) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1338 | nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK; |
| 1339 | else |
| 1340 | nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK; |
| 1341 | |
Samuel Ortiz | 7975754 | 2012-03-05 01:03:45 +0100 | [diff] [blame] | 1342 | memcpy(nfc_tgt->sensf_res, &tgt_felica->opcode, 9); |
| 1343 | nfc_tgt->sensf_res_len = 9; |
| 1344 | |
Samuel Ortiz | 322bce9 | 2013-05-27 15:29:11 +0200 | [diff] [blame] | 1345 | memcpy(nfc_tgt->nfcid2, tgt_felica->nfcid2, NFC_NFCID2_MAXSIZE); |
| 1346 | nfc_tgt->nfcid2_len = NFC_NFCID2_MAXSIZE; |
| 1347 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1348 | return 0; |
| 1349 | } |
| 1350 | |
| 1351 | struct pn533_target_jewel { |
| 1352 | __be16 sens_res; |
| 1353 | u8 jewelid[4]; |
| 1354 | } __packed; |
| 1355 | |
| 1356 | static bool pn533_target_jewel_is_valid(struct pn533_target_jewel *jewel, |
| 1357 | int target_data_len) |
| 1358 | { |
| 1359 | u8 ssd; |
| 1360 | u8 platconf; |
| 1361 | |
| 1362 | if (target_data_len < sizeof(struct pn533_target_jewel)) |
| 1363 | return false; |
| 1364 | |
| 1365 | /* Requirement 4.6.3.3 from NFC Forum Digital Spec */ |
| 1366 | ssd = PN533_TYPE_A_SENS_RES_SSD(jewel->sens_res); |
| 1367 | platconf = PN533_TYPE_A_SENS_RES_PLATCONF(jewel->sens_res); |
| 1368 | |
| 1369 | if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL && |
Waldemar Rymarkiewicz | 5d46774 | 2012-12-10 14:42:51 +0100 | [diff] [blame] | 1370 | platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) || |
| 1371 | (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL && |
| 1372 | platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL)) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1373 | return false; |
| 1374 | |
| 1375 | return true; |
| 1376 | } |
| 1377 | |
| 1378 | static int pn533_target_found_jewel(struct nfc_target *nfc_tgt, u8 *tgt_data, |
| 1379 | int tgt_data_len) |
| 1380 | { |
| 1381 | struct pn533_target_jewel *tgt_jewel; |
| 1382 | |
Waldemar Rymarkiewicz | 37cf4fc | 2012-12-10 14:42:50 +0100 | [diff] [blame] | 1383 | tgt_jewel = (struct pn533_target_jewel *)tgt_data; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1384 | |
| 1385 | if (!pn533_target_jewel_is_valid(tgt_jewel, tgt_data_len)) |
| 1386 | return -EPROTO; |
| 1387 | |
| 1388 | nfc_tgt->supported_protocols = NFC_PROTO_JEWEL_MASK; |
| 1389 | nfc_tgt->sens_res = be16_to_cpu(tgt_jewel->sens_res); |
Samuel Ortiz | d8dc107 | 2012-03-05 01:03:46 +0100 | [diff] [blame] | 1390 | nfc_tgt->nfcid1_len = 4; |
| 1391 | memcpy(nfc_tgt->nfcid1, tgt_jewel->jewelid, nfc_tgt->nfcid1_len); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1392 | |
| 1393 | return 0; |
| 1394 | } |
| 1395 | |
| 1396 | struct pn533_type_b_prot_info { |
| 1397 | u8 bitrate; |
| 1398 | u8 fsci_type; |
| 1399 | u8 fwi_adc_fo; |
| 1400 | } __packed; |
| 1401 | |
| 1402 | #define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4) |
| 1403 | #define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0) |
| 1404 | #define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8 |
| 1405 | |
| 1406 | struct pn533_type_b_sens_res { |
| 1407 | u8 opcode; |
| 1408 | u8 nfcid[4]; |
| 1409 | u8 appdata[4]; |
| 1410 | struct pn533_type_b_prot_info prot_info; |
| 1411 | } __packed; |
| 1412 | |
| 1413 | #define PN533_TYPE_B_OPC_SENSB_RES 0x50 |
| 1414 | |
| 1415 | struct pn533_target_type_b { |
| 1416 | struct pn533_type_b_sens_res sensb_res; |
| 1417 | u8 attrib_res_len; |
| 1418 | u8 attrib_res[]; |
| 1419 | } __packed; |
| 1420 | |
| 1421 | static bool pn533_target_type_b_is_valid(struct pn533_target_type_b *type_b, |
| 1422 | int target_data_len) |
| 1423 | { |
| 1424 | if (target_data_len < sizeof(struct pn533_target_type_b)) |
| 1425 | return false; |
| 1426 | |
| 1427 | if (type_b->sensb_res.opcode != PN533_TYPE_B_OPC_SENSB_RES) |
| 1428 | return false; |
| 1429 | |
| 1430 | if (PN533_TYPE_B_PROT_TYPE(type_b->sensb_res.prot_info.fsci_type) & |
| 1431 | PN533_TYPE_B_PROT_TYPE_RFU_MASK) |
| 1432 | return false; |
| 1433 | |
| 1434 | return true; |
| 1435 | } |
| 1436 | |
| 1437 | static int pn533_target_found_type_b(struct nfc_target *nfc_tgt, u8 *tgt_data, |
| 1438 | int tgt_data_len) |
| 1439 | { |
| 1440 | struct pn533_target_type_b *tgt_type_b; |
| 1441 | |
Waldemar Rymarkiewicz | 37cf4fc | 2012-12-10 14:42:50 +0100 | [diff] [blame] | 1442 | tgt_type_b = (struct pn533_target_type_b *)tgt_data; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1443 | |
| 1444 | if (!pn533_target_type_b_is_valid(tgt_type_b, tgt_data_len)) |
| 1445 | return -EPROTO; |
| 1446 | |
Samuel Ortiz | 01d719a | 2012-07-04 00:14:04 +0200 | [diff] [blame] | 1447 | nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_B_MASK; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1448 | |
| 1449 | return 0; |
| 1450 | } |
| 1451 | |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1452 | static int pn533_target_found(struct pn533 *dev, u8 tg, u8 *tgdata, |
| 1453 | int tgdata_len) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1454 | { |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1455 | struct nfc_target nfc_tgt; |
| 1456 | int rc; |
| 1457 | |
Joe Perches | 17936b4 | 2013-04-05 12:27:39 -0700 | [diff] [blame] | 1458 | dev_dbg(&dev->interface->dev, "%s: modulation=%d\n", |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 1459 | __func__, dev->poll_mod_curr); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1460 | |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1461 | if (tg != 1) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1462 | return -EPROTO; |
| 1463 | |
Samuel Ortiz | 98b3ac1 | 2012-03-05 01:03:39 +0100 | [diff] [blame] | 1464 | memset(&nfc_tgt, 0, sizeof(struct nfc_target)); |
| 1465 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1466 | switch (dev->poll_mod_curr) { |
| 1467 | case PN533_POLL_MOD_106KBPS_A: |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1468 | rc = pn533_target_found_type_a(&nfc_tgt, tgdata, tgdata_len); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1469 | break; |
| 1470 | case PN533_POLL_MOD_212KBPS_FELICA: |
| 1471 | case PN533_POLL_MOD_424KBPS_FELICA: |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1472 | rc = pn533_target_found_felica(&nfc_tgt, tgdata, tgdata_len); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1473 | break; |
| 1474 | case PN533_POLL_MOD_106KBPS_JEWEL: |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1475 | rc = pn533_target_found_jewel(&nfc_tgt, tgdata, tgdata_len); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1476 | break; |
| 1477 | case PN533_POLL_MOD_847KBPS_B: |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1478 | rc = pn533_target_found_type_b(&nfc_tgt, tgdata, tgdata_len); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1479 | break; |
| 1480 | default: |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 1481 | nfc_err(&dev->interface->dev, |
| 1482 | "Unknown current poll modulation\n"); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1483 | return -EPROTO; |
| 1484 | } |
| 1485 | |
| 1486 | if (rc) |
| 1487 | return rc; |
| 1488 | |
| 1489 | if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) { |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 1490 | dev_dbg(&dev->interface->dev, |
| 1491 | "The Tg found doesn't have the desired protocol\n"); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1492 | return -EAGAIN; |
| 1493 | } |
| 1494 | |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 1495 | dev_dbg(&dev->interface->dev, |
| 1496 | "Target found - supported protocols: 0x%x\n", |
| 1497 | nfc_tgt.supported_protocols); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1498 | |
| 1499 | dev->tgt_available_prots = nfc_tgt.supported_protocols; |
| 1500 | |
| 1501 | nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1); |
| 1502 | |
| 1503 | return 0; |
| 1504 | } |
| 1505 | |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1506 | static inline void pn533_poll_next_mod(struct pn533 *dev) |
| 1507 | { |
| 1508 | dev->poll_mod_curr = (dev->poll_mod_curr + 1) % dev->poll_mod_count; |
| 1509 | } |
| 1510 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1511 | static void pn533_poll_reset_mod_list(struct pn533 *dev) |
| 1512 | { |
| 1513 | dev->poll_mod_count = 0; |
| 1514 | } |
| 1515 | |
| 1516 | static void pn533_poll_add_mod(struct pn533 *dev, u8 mod_index) |
| 1517 | { |
| 1518 | dev->poll_mod_active[dev->poll_mod_count] = |
Waldemar Rymarkiewicz | 37cf4fc | 2012-12-10 14:42:50 +0100 | [diff] [blame] | 1519 | (struct pn533_poll_modulations *)&poll_mod[mod_index]; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1520 | dev->poll_mod_count++; |
| 1521 | } |
| 1522 | |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1523 | static void pn533_poll_create_mod_list(struct pn533 *dev, |
| 1524 | u32 im_protocols, u32 tm_protocols) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1525 | { |
| 1526 | pn533_poll_reset_mod_list(dev); |
| 1527 | |
Waldemar Rymarkiewicz | b08e860 | 2012-12-10 14:42:52 +0100 | [diff] [blame] | 1528 | if ((im_protocols & NFC_PROTO_MIFARE_MASK) || |
| 1529 | (im_protocols & NFC_PROTO_ISO14443_MASK) || |
| 1530 | (im_protocols & NFC_PROTO_NFC_DEP_MASK)) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1531 | pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A); |
| 1532 | |
Waldemar Rymarkiewicz | b08e860 | 2012-12-10 14:42:52 +0100 | [diff] [blame] | 1533 | if (im_protocols & NFC_PROTO_FELICA_MASK || |
| 1534 | im_protocols & NFC_PROTO_NFC_DEP_MASK) { |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1535 | pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA); |
| 1536 | pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA); |
| 1537 | } |
| 1538 | |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1539 | if (im_protocols & NFC_PROTO_JEWEL_MASK) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1540 | pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_JEWEL); |
| 1541 | |
Samuel Ortiz | 01d719a | 2012-07-04 00:14:04 +0200 | [diff] [blame] | 1542 | if (im_protocols & NFC_PROTO_ISO14443_B_MASK) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1543 | pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1544 | |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1545 | if (tm_protocols) |
| 1546 | pn533_poll_add_mod(dev, PN533_LISTEN_MOD); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1547 | } |
| 1548 | |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1549 | 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] | 1550 | { |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1551 | u8 nbtg, tg, *tgdata; |
| 1552 | int rc, tgdata_len; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1553 | |
Samuel Ortiz | 673088f | 2013-08-21 15:06:55 +0200 | [diff] [blame] | 1554 | /* Toggle the DEP polling */ |
| 1555 | dev->poll_dep = 1; |
| 1556 | |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1557 | nbtg = resp->data[0]; |
| 1558 | tg = resp->data[1]; |
| 1559 | tgdata = &resp->data[2]; |
| 1560 | tgdata_len = resp->len - 2; /* nbtg + tg */ |
| 1561 | |
| 1562 | if (nbtg) { |
| 1563 | rc = pn533_target_found(dev, tg, tgdata, tgdata_len); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1564 | |
| 1565 | /* We must stop the poll after a valid target found */ |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1566 | if (rc == 0) { |
| 1567 | pn533_poll_reset_mod_list(dev); |
| 1568 | return 0; |
| 1569 | } |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1570 | } |
| 1571 | |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1572 | return -EAGAIN; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1573 | } |
| 1574 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 1575 | static struct sk_buff *pn533_alloc_poll_tg_frame(struct pn533 *dev) |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 1576 | { |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1577 | struct sk_buff *skb; |
| 1578 | u8 *felica, *nfcid3, *gb; |
| 1579 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 1580 | u8 *gbytes = dev->gb; |
| 1581 | size_t gbytes_len = dev->gb_len; |
| 1582 | |
Samuel Ortiz | 51d9e80 | 2012-05-30 01:48:46 +0200 | [diff] [blame] | 1583 | u8 felica_params[18] = {0x1, 0xfe, /* DEP */ |
| 1584 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* random */ |
| 1585 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, |
| 1586 | 0xff, 0xff}; /* System code */ |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1587 | |
Samuel Ortiz | 51d9e80 | 2012-05-30 01:48:46 +0200 | [diff] [blame] | 1588 | u8 mifare_params[6] = {0x1, 0x1, /* SENS_RES */ |
| 1589 | 0x0, 0x0, 0x0, |
| 1590 | 0x40}; /* SEL_RES for DEP */ |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 1591 | |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1592 | unsigned int skb_len = 36 + /* mode (1), mifare (6), |
| 1593 | felica (18), nfcid3 (10), gb_len (1) */ |
| 1594 | gbytes_len + |
| 1595 | 1; /* len Tk*/ |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 1596 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 1597 | skb = pn533_alloc_skb(dev, skb_len); |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1598 | if (!skb) |
| 1599 | return NULL; |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 1600 | |
| 1601 | /* DEP support only */ |
Thierry Escande | 52f2eae | 2013-02-05 10:08:35 +0100 | [diff] [blame] | 1602 | *skb_put(skb, 1) = PN533_INIT_TARGET_DEP; |
Samuel Ortiz | 51d9e80 | 2012-05-30 01:48:46 +0200 | [diff] [blame] | 1603 | |
| 1604 | /* MIFARE params */ |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1605 | memcpy(skb_put(skb, 6), mifare_params, 6); |
| 1606 | |
| 1607 | /* Felica params */ |
| 1608 | felica = skb_put(skb, 18); |
| 1609 | memcpy(felica, felica_params, 18); |
| 1610 | get_random_bytes(felica + 2, 6); |
| 1611 | |
| 1612 | /* NFCID3 */ |
| 1613 | nfcid3 = skb_put(skb, 10); |
| 1614 | memset(nfcid3, 0, 10); |
| 1615 | memcpy(nfcid3, felica, 8); |
Samuel Ortiz | 51d9e80 | 2012-05-30 01:48:46 +0200 | [diff] [blame] | 1616 | |
| 1617 | /* General bytes */ |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1618 | *skb_put(skb, 1) = gbytes_len; |
| 1619 | |
| 1620 | gb = skb_put(skb, gbytes_len); |
| 1621 | memcpy(gb, gbytes, gbytes_len); |
Samuel Ortiz | 51d9e80 | 2012-05-30 01:48:46 +0200 | [diff] [blame] | 1622 | |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 1623 | /* Len Tk */ |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1624 | *skb_put(skb, 1) = 0; |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 1625 | |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1626 | return skb; |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 1627 | } |
| 1628 | |
Waldemar Rymarkiewicz | b1bb290 | 2012-11-26 14:18:32 +0100 | [diff] [blame] | 1629 | #define PN533_CMD_DATAEXCH_HEAD_LEN 1 |
Samuel Ortiz | 103b34c | 2012-05-31 00:07:51 +0200 | [diff] [blame] | 1630 | #define PN533_CMD_DATAEXCH_DATA_MAXLEN 262 |
Olivier Guiter | 3c13b24 | 2013-09-23 12:24:38 +0200 | [diff] [blame] | 1631 | static void pn533_wq_tm_mi_recv(struct work_struct *work); |
| 1632 | static struct sk_buff *pn533_build_response(struct pn533 *dev); |
| 1633 | |
Samuel Ortiz | 103b34c | 2012-05-31 00:07:51 +0200 | [diff] [blame] | 1634 | static int pn533_tm_get_data_complete(struct pn533 *dev, void *arg, |
Waldemar Rymarkiewicz | e487882 | 2012-12-10 14:42:44 +0100 | [diff] [blame] | 1635 | struct sk_buff *resp) |
Samuel Ortiz | 103b34c | 2012-05-31 00:07:51 +0200 | [diff] [blame] | 1636 | { |
Olivier Guiter | 3c13b24 | 2013-09-23 12:24:38 +0200 | [diff] [blame] | 1637 | struct sk_buff *skb; |
| 1638 | u8 status, ret, mi; |
| 1639 | int rc; |
Samuel Ortiz | 103b34c | 2012-05-31 00:07:51 +0200 | [diff] [blame] | 1640 | |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 1641 | dev_dbg(&dev->interface->dev, "%s\n", __func__); |
Samuel Ortiz | 103b34c | 2012-05-31 00:07:51 +0200 | [diff] [blame] | 1642 | |
Olivier Guiter | 3c13b24 | 2013-09-23 12:24:38 +0200 | [diff] [blame] | 1643 | if (IS_ERR(resp)) { |
| 1644 | skb_queue_purge(&dev->resp_q); |
Waldemar Rymarkiewicz | e487882 | 2012-12-10 14:42:44 +0100 | [diff] [blame] | 1645 | return PTR_ERR(resp); |
Samuel Ortiz | 103b34c | 2012-05-31 00:07:51 +0200 | [diff] [blame] | 1646 | } |
| 1647 | |
Olivier Guiter | 3c13b24 | 2013-09-23 12:24:38 +0200 | [diff] [blame] | 1648 | status = resp->data[0]; |
| 1649 | |
| 1650 | ret = status & PN533_CMD_RET_MASK; |
| 1651 | mi = status & PN533_CMD_MI_MASK; |
| 1652 | |
| 1653 | skb_pull(resp, sizeof(status)); |
| 1654 | |
| 1655 | if (ret != PN533_CMD_RET_SUCCESS) { |
| 1656 | rc = -EIO; |
| 1657 | goto error; |
| 1658 | } |
| 1659 | |
| 1660 | skb_queue_tail(&dev->resp_q, resp); |
| 1661 | |
| 1662 | if (mi) { |
| 1663 | queue_work(dev->wq, &dev->mi_tm_rx_work); |
| 1664 | return -EINPROGRESS; |
| 1665 | } |
| 1666 | |
| 1667 | skb = pn533_build_response(dev); |
| 1668 | if (!skb) { |
| 1669 | rc = -EIO; |
| 1670 | goto error; |
| 1671 | } |
| 1672 | |
| 1673 | return nfc_tm_data_received(dev->nfc_dev, skb); |
| 1674 | |
| 1675 | error: |
| 1676 | nfc_tm_deactivated(dev->nfc_dev); |
| 1677 | dev->tgt_mode = 0; |
| 1678 | skb_queue_purge(&dev->resp_q); |
| 1679 | dev_kfree_skb(resp); |
| 1680 | |
| 1681 | return rc; |
| 1682 | } |
| 1683 | |
| 1684 | static void pn533_wq_tm_mi_recv(struct work_struct *work) |
| 1685 | { |
| 1686 | struct pn533 *dev = container_of(work, struct pn533, mi_tm_rx_work); |
| 1687 | struct sk_buff *skb; |
| 1688 | int rc; |
| 1689 | |
| 1690 | dev_dbg(&dev->interface->dev, "%s\n", __func__); |
| 1691 | |
| 1692 | skb = pn533_alloc_skb(dev, 0); |
| 1693 | if (!skb) |
| 1694 | return; |
| 1695 | |
| 1696 | rc = pn533_send_cmd_direct_async(dev, |
| 1697 | PN533_CMD_TG_GET_DATA, |
| 1698 | skb, |
| 1699 | pn533_tm_get_data_complete, |
| 1700 | NULL); |
| 1701 | |
| 1702 | if (rc < 0) |
| 1703 | dev_kfree_skb(skb); |
| 1704 | |
| 1705 | return; |
Samuel Ortiz | 103b34c | 2012-05-31 00:07:51 +0200 | [diff] [blame] | 1706 | } |
| 1707 | |
Olivier Guiter | 93ad420 | 2013-09-23 12:24:39 +0200 | [diff] [blame] | 1708 | static int pn533_tm_send_complete(struct pn533 *dev, void *arg, |
| 1709 | struct sk_buff *resp); |
| 1710 | static void pn533_wq_tm_mi_send(struct work_struct *work) |
| 1711 | { |
| 1712 | struct pn533 *dev = container_of(work, struct pn533, mi_tm_tx_work); |
| 1713 | struct sk_buff *skb; |
| 1714 | int rc; |
| 1715 | |
| 1716 | dev_dbg(&dev->interface->dev, "%s\n", __func__); |
| 1717 | |
| 1718 | /* Grab the first skb in the queue */ |
| 1719 | skb = skb_dequeue(&dev->fragment_skb); |
| 1720 | if (skb == NULL) { /* No more data */ |
| 1721 | /* Reset the queue for future use */ |
| 1722 | skb_queue_head_init(&dev->fragment_skb); |
| 1723 | goto error; |
| 1724 | } |
| 1725 | |
| 1726 | /* last entry - remove MI bit */ |
| 1727 | if (skb_queue_len(&dev->fragment_skb) == 0) { |
| 1728 | rc = pn533_send_cmd_direct_async(dev, PN533_CMD_TG_SET_DATA, |
| 1729 | skb, pn533_tm_send_complete, NULL); |
| 1730 | } else |
| 1731 | rc = pn533_send_cmd_direct_async(dev, |
| 1732 | PN533_CMD_TG_SET_META_DATA, |
| 1733 | skb, pn533_tm_send_complete, NULL); |
| 1734 | |
| 1735 | if (rc == 0) /* success */ |
| 1736 | return; |
| 1737 | |
| 1738 | dev_err(&dev->interface->dev, |
| 1739 | "Error %d when trying to perform set meta data_exchange", rc); |
| 1740 | |
| 1741 | dev_kfree_skb(skb); |
| 1742 | |
| 1743 | error: |
| 1744 | pn533_send_ack(dev, GFP_KERNEL); |
| 1745 | queue_work(dev->wq, &dev->cmd_work); |
| 1746 | } |
| 1747 | |
Samuel Ortiz | 103b34c | 2012-05-31 00:07:51 +0200 | [diff] [blame] | 1748 | static void pn533_wq_tg_get_data(struct work_struct *work) |
| 1749 | { |
| 1750 | struct pn533 *dev = container_of(work, struct pn533, tg_work); |
Waldemar Rymarkiewicz | e487882 | 2012-12-10 14:42:44 +0100 | [diff] [blame] | 1751 | struct sk_buff *skb; |
| 1752 | int rc; |
Samuel Ortiz | 103b34c | 2012-05-31 00:07:51 +0200 | [diff] [blame] | 1753 | |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 1754 | dev_dbg(&dev->interface->dev, "%s\n", __func__); |
Samuel Ortiz | 103b34c | 2012-05-31 00:07:51 +0200 | [diff] [blame] | 1755 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 1756 | skb = pn533_alloc_skb(dev, 0); |
Waldemar Rymarkiewicz | e487882 | 2012-12-10 14:42:44 +0100 | [diff] [blame] | 1757 | if (!skb) |
Samuel Ortiz | 103b34c | 2012-05-31 00:07:51 +0200 | [diff] [blame] | 1758 | return; |
| 1759 | |
Waldemar Rymarkiewicz | e487882 | 2012-12-10 14:42:44 +0100 | [diff] [blame] | 1760 | rc = pn533_send_data_async(dev, PN533_CMD_TG_GET_DATA, skb, |
| 1761 | pn533_tm_get_data_complete, NULL); |
Samuel Ortiz | 103b34c | 2012-05-31 00:07:51 +0200 | [diff] [blame] | 1762 | |
Waldemar Rymarkiewicz | e487882 | 2012-12-10 14:42:44 +0100 | [diff] [blame] | 1763 | if (rc < 0) |
| 1764 | dev_kfree_skb(skb); |
Samuel Ortiz | 103b34c | 2012-05-31 00:07:51 +0200 | [diff] [blame] | 1765 | |
| 1766 | return; |
| 1767 | } |
| 1768 | |
Samuel Ortiz | fc40a8c | 2012-06-01 13:21:13 +0200 | [diff] [blame] | 1769 | #define ATR_REQ_GB_OFFSET 17 |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1770 | 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] | 1771 | { |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1772 | u8 mode, *cmd, comm_mode = NFC_COMM_PASSIVE, *gb; |
Samuel Ortiz | fc40a8c | 2012-06-01 13:21:13 +0200 | [diff] [blame] | 1773 | size_t gb_len; |
Samuel Ortiz | 103b34c | 2012-05-31 00:07:51 +0200 | [diff] [blame] | 1774 | int rc; |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 1775 | |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 1776 | dev_dbg(&dev->interface->dev, "%s\n", __func__); |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 1777 | |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1778 | if (resp->len < ATR_REQ_GB_OFFSET + 1) |
Samuel Ortiz | fc40a8c | 2012-06-01 13:21:13 +0200 | [diff] [blame] | 1779 | return -EINVAL; |
| 1780 | |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1781 | mode = resp->data[0]; |
| 1782 | cmd = &resp->data[1]; |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 1783 | |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 1784 | dev_dbg(&dev->interface->dev, "Target mode 0x%x len %d\n", |
| 1785 | mode, resp->len); |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 1786 | |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1787 | if ((mode & PN533_INIT_TARGET_RESP_FRAME_MASK) == |
| 1788 | PN533_INIT_TARGET_RESP_ACTIVE) |
Samuel Ortiz | fc40a8c | 2012-06-01 13:21:13 +0200 | [diff] [blame] | 1789 | comm_mode = NFC_COMM_ACTIVE; |
| 1790 | |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1791 | if ((mode & PN533_INIT_TARGET_RESP_DEP) == 0) /* Only DEP supported */ |
Samuel Ortiz | fc40a8c | 2012-06-01 13:21:13 +0200 | [diff] [blame] | 1792 | return -EOPNOTSUPP; |
| 1793 | |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1794 | gb = cmd + ATR_REQ_GB_OFFSET; |
| 1795 | gb_len = resp->len - (ATR_REQ_GB_OFFSET + 1); |
Samuel Ortiz | fc40a8c | 2012-06-01 13:21:13 +0200 | [diff] [blame] | 1796 | |
Samuel Ortiz | 103b34c | 2012-05-31 00:07:51 +0200 | [diff] [blame] | 1797 | rc = nfc_tm_activated(dev->nfc_dev, NFC_PROTO_NFC_DEP_MASK, |
| 1798 | comm_mode, gb, gb_len); |
| 1799 | if (rc < 0) { |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 1800 | nfc_err(&dev->interface->dev, |
| 1801 | "Error when signaling target activation\n"); |
Samuel Ortiz | 103b34c | 2012-05-31 00:07:51 +0200 | [diff] [blame] | 1802 | return rc; |
| 1803 | } |
| 1804 | |
Samuel Ortiz | 51ad304 | 2012-05-31 20:01:32 +0200 | [diff] [blame] | 1805 | dev->tgt_mode = 1; |
Samuel Ortiz | 103b34c | 2012-05-31 00:07:51 +0200 | [diff] [blame] | 1806 | queue_work(dev->wq, &dev->tg_work); |
| 1807 | |
| 1808 | return 0; |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 1809 | } |
| 1810 | |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1811 | static void pn533_listen_mode_timer(unsigned long data) |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 1812 | { |
Waldemar Rymarkiewicz | 37cf4fc | 2012-12-10 14:42:50 +0100 | [diff] [blame] | 1813 | struct pn533 *dev = (struct pn533 *)data; |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1814 | |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 1815 | dev_dbg(&dev->interface->dev, "Listen mode timeout\n"); |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1816 | |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1817 | dev->cancel_listen = 1; |
| 1818 | |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1819 | pn533_poll_next_mod(dev); |
| 1820 | |
Samuel Ortiz | 46f793b | 2013-07-03 14:50:36 +0200 | [diff] [blame] | 1821 | queue_delayed_work(dev->wq, &dev->poll_work, |
| 1822 | msecs_to_jiffies(PN533_POLL_INTERVAL)); |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1823 | } |
| 1824 | |
Samuel Ortiz | 17e9d9d | 2013-06-26 18:16:21 +0200 | [diff] [blame] | 1825 | static int pn533_rf_complete(struct pn533 *dev, void *arg, |
| 1826 | struct sk_buff *resp) |
| 1827 | { |
| 1828 | int rc = 0; |
| 1829 | |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 1830 | dev_dbg(&dev->interface->dev, "%s\n", __func__); |
Samuel Ortiz | 17e9d9d | 2013-06-26 18:16:21 +0200 | [diff] [blame] | 1831 | |
| 1832 | if (IS_ERR(resp)) { |
| 1833 | rc = PTR_ERR(resp); |
| 1834 | |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 1835 | nfc_err(&dev->interface->dev, "RF setting error %d", rc); |
Samuel Ortiz | 17e9d9d | 2013-06-26 18:16:21 +0200 | [diff] [blame] | 1836 | |
| 1837 | return rc; |
| 1838 | } |
| 1839 | |
Samuel Ortiz | 46f793b | 2013-07-03 14:50:36 +0200 | [diff] [blame] | 1840 | queue_delayed_work(dev->wq, &dev->poll_work, |
| 1841 | msecs_to_jiffies(PN533_POLL_INTERVAL)); |
Samuel Ortiz | 17e9d9d | 2013-06-26 18:16:21 +0200 | [diff] [blame] | 1842 | |
| 1843 | dev_kfree_skb(resp); |
| 1844 | return rc; |
| 1845 | } |
| 1846 | |
| 1847 | static void pn533_wq_rf(struct work_struct *work) |
| 1848 | { |
| 1849 | struct pn533 *dev = container_of(work, struct pn533, rf_work); |
| 1850 | struct sk_buff *skb; |
| 1851 | int rc; |
| 1852 | |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 1853 | dev_dbg(&dev->interface->dev, "%s\n", __func__); |
Samuel Ortiz | 17e9d9d | 2013-06-26 18:16:21 +0200 | [diff] [blame] | 1854 | |
| 1855 | skb = pn533_alloc_skb(dev, 2); |
| 1856 | if (!skb) |
| 1857 | return; |
| 1858 | |
| 1859 | *skb_put(skb, 1) = PN533_CFGITEM_RF_FIELD; |
Samuel Ortiz | 3a8eab3 | 2013-07-01 17:26:58 +0200 | [diff] [blame] | 1860 | *skb_put(skb, 1) = PN533_CFGITEM_RF_FIELD_AUTO_RFCA; |
Samuel Ortiz | 17e9d9d | 2013-06-26 18:16:21 +0200 | [diff] [blame] | 1861 | |
| 1862 | rc = pn533_send_cmd_async(dev, PN533_CMD_RF_CONFIGURATION, skb, |
| 1863 | pn533_rf_complete, NULL); |
| 1864 | if (rc < 0) { |
| 1865 | dev_kfree_skb(skb); |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 1866 | nfc_err(&dev->interface->dev, "RF setting error %d\n", rc); |
Samuel Ortiz | 17e9d9d | 2013-06-26 18:16:21 +0200 | [diff] [blame] | 1867 | } |
| 1868 | |
| 1869 | return; |
| 1870 | } |
| 1871 | |
Samuel Ortiz | 673088f | 2013-08-21 15:06:55 +0200 | [diff] [blame] | 1872 | static int pn533_poll_dep_complete(struct pn533 *dev, void *arg, |
| 1873 | struct sk_buff *resp) |
| 1874 | { |
| 1875 | struct pn533_cmd_jump_dep_response *rsp; |
| 1876 | struct nfc_target nfc_target; |
| 1877 | u8 target_gt_len; |
| 1878 | int rc; |
| 1879 | |
| 1880 | if (IS_ERR(resp)) |
| 1881 | return PTR_ERR(resp); |
| 1882 | |
| 1883 | rsp = (struct pn533_cmd_jump_dep_response *)resp->data; |
| 1884 | |
| 1885 | rc = rsp->status & PN533_CMD_RET_MASK; |
| 1886 | if (rc != PN533_CMD_RET_SUCCESS) { |
| 1887 | /* Not target found, turn radio off */ |
| 1888 | queue_work(dev->wq, &dev->rf_work); |
| 1889 | |
| 1890 | dev_kfree_skb(resp); |
| 1891 | return 0; |
| 1892 | } |
| 1893 | |
| 1894 | dev_dbg(&dev->interface->dev, "Creating new target"); |
| 1895 | |
| 1896 | nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK; |
| 1897 | nfc_target.nfcid1_len = 10; |
| 1898 | memcpy(nfc_target.nfcid1, rsp->nfcid3t, nfc_target.nfcid1_len); |
| 1899 | rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1); |
| 1900 | if (rc) |
| 1901 | goto error; |
| 1902 | |
| 1903 | dev->tgt_available_prots = 0; |
| 1904 | dev->tgt_active_prot = NFC_PROTO_NFC_DEP; |
| 1905 | |
| 1906 | /* ATR_RES general bytes are located at offset 17 */ |
| 1907 | target_gt_len = resp->len - 17; |
| 1908 | rc = nfc_set_remote_general_bytes(dev->nfc_dev, |
| 1909 | rsp->gt, target_gt_len); |
| 1910 | if (!rc) { |
| 1911 | rc = nfc_dep_link_is_up(dev->nfc_dev, |
| 1912 | dev->nfc_dev->targets[0].idx, |
| 1913 | 0, NFC_RF_INITIATOR); |
| 1914 | |
| 1915 | if (!rc) |
| 1916 | pn533_poll_reset_mod_list(dev); |
| 1917 | } |
| 1918 | error: |
| 1919 | dev_kfree_skb(resp); |
| 1920 | return rc; |
| 1921 | } |
| 1922 | |
| 1923 | #define PASSIVE_DATA_LEN 5 |
| 1924 | static int pn533_poll_dep(struct nfc_dev *nfc_dev) |
| 1925 | { |
| 1926 | struct pn533 *dev = nfc_get_drvdata(nfc_dev); |
| 1927 | struct sk_buff *skb; |
| 1928 | int rc, skb_len; |
| 1929 | u8 *next, nfcid3[NFC_NFCID3_MAXSIZE]; |
| 1930 | u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3}; |
| 1931 | |
| 1932 | dev_dbg(&dev->interface->dev, "%s", __func__); |
| 1933 | |
| 1934 | if (!dev->gb) { |
| 1935 | dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len); |
| 1936 | |
| 1937 | if (!dev->gb || !dev->gb_len) { |
| 1938 | dev->poll_dep = 0; |
| 1939 | queue_work(dev->wq, &dev->rf_work); |
| 1940 | } |
| 1941 | } |
| 1942 | |
| 1943 | skb_len = 3 + dev->gb_len; /* ActPass + BR + Next */ |
| 1944 | skb_len += PASSIVE_DATA_LEN; |
| 1945 | |
| 1946 | /* NFCID3 */ |
| 1947 | skb_len += NFC_NFCID3_MAXSIZE; |
| 1948 | nfcid3[0] = 0x1; |
| 1949 | nfcid3[1] = 0xfe; |
| 1950 | get_random_bytes(nfcid3 + 2, 6); |
| 1951 | |
| 1952 | skb = pn533_alloc_skb(dev, skb_len); |
| 1953 | if (!skb) |
| 1954 | return -ENOMEM; |
| 1955 | |
| 1956 | *skb_put(skb, 1) = 0x01; /* Active */ |
| 1957 | *skb_put(skb, 1) = 0x02; /* 424 kbps */ |
| 1958 | |
| 1959 | next = skb_put(skb, 1); /* Next */ |
| 1960 | *next = 0; |
| 1961 | |
| 1962 | /* Copy passive data */ |
| 1963 | memcpy(skb_put(skb, PASSIVE_DATA_LEN), passive_data, PASSIVE_DATA_LEN); |
| 1964 | *next |= 1; |
| 1965 | |
| 1966 | /* Copy NFCID3 (which is NFCID2 from SENSF_RES) */ |
| 1967 | memcpy(skb_put(skb, NFC_NFCID3_MAXSIZE), nfcid3, |
| 1968 | NFC_NFCID3_MAXSIZE); |
| 1969 | *next |= 2; |
| 1970 | |
| 1971 | memcpy(skb_put(skb, dev->gb_len), dev->gb, dev->gb_len); |
| 1972 | *next |= 4; /* We have some Gi */ |
| 1973 | |
| 1974 | rc = pn533_send_cmd_async(dev, PN533_CMD_IN_JUMP_FOR_DEP, skb, |
| 1975 | pn533_poll_dep_complete, NULL); |
| 1976 | |
| 1977 | if (rc < 0) |
| 1978 | dev_kfree_skb(skb); |
| 1979 | |
| 1980 | return rc; |
| 1981 | } |
| 1982 | |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1983 | static int pn533_poll_complete(struct pn533 *dev, void *arg, |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1984 | struct sk_buff *resp) |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1985 | { |
| 1986 | struct pn533_poll_modulations *cur_mod; |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 1987 | int rc; |
| 1988 | |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 1989 | dev_dbg(&dev->interface->dev, "%s\n", __func__); |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1990 | |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1991 | if (IS_ERR(resp)) { |
| 1992 | rc = PTR_ERR(resp); |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1993 | |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 1994 | nfc_err(&dev->interface->dev, "%s Poll complete error %d\n", |
| 1995 | __func__, rc); |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1996 | |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1997 | if (rc == -ENOENT) { |
| 1998 | if (dev->poll_mod_count != 0) |
| 1999 | return rc; |
| 2000 | else |
| 2001 | goto stop_poll; |
| 2002 | } else if (rc < 0) { |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 2003 | nfc_err(&dev->interface->dev, |
| 2004 | "Error %d when running poll\n", rc); |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 2005 | goto stop_poll; |
| 2006 | } |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 2007 | } |
| 2008 | |
| 2009 | cur_mod = dev->poll_mod_active[dev->poll_mod_curr]; |
| 2010 | |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 2011 | if (cur_mod->len == 0) { /* Target mode */ |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 2012 | del_timer(&dev->listen_timer); |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 2013 | rc = pn533_init_target_complete(dev, resp); |
| 2014 | goto done; |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 2015 | } |
| 2016 | |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 2017 | /* Initiator mode */ |
| 2018 | rc = pn533_start_poll_complete(dev, resp); |
| 2019 | if (!rc) |
| 2020 | goto done; |
| 2021 | |
Waldemar Rymarkiewicz | 95cb9e1 | 2013-04-03 08:02:00 +0200 | [diff] [blame] | 2022 | if (!dev->poll_mod_count) { |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 2023 | dev_dbg(&dev->interface->dev, "Polling has been stopped\n"); |
Waldemar Rymarkiewicz | 95cb9e1 | 2013-04-03 08:02:00 +0200 | [diff] [blame] | 2024 | goto done; |
| 2025 | } |
| 2026 | |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 2027 | pn533_poll_next_mod(dev); |
Samuel Ortiz | 17e9d9d | 2013-06-26 18:16:21 +0200 | [diff] [blame] | 2028 | /* Not target found, turn radio off */ |
| 2029 | queue_work(dev->wq, &dev->rf_work); |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 2030 | |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 2031 | done: |
| 2032 | dev_kfree_skb(resp); |
| 2033 | return rc; |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 2034 | |
| 2035 | stop_poll: |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 2036 | nfc_err(&dev->interface->dev, "Polling operation has been stopped\n"); |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 2037 | |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 2038 | pn533_poll_reset_mod_list(dev); |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 2039 | dev->poll_protocols = 0; |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 2040 | return rc; |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 2041 | } |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 2042 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 2043 | static struct sk_buff *pn533_alloc_poll_in_frame(struct pn533 *dev, |
| 2044 | struct pn533_poll_modulations *mod) |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 2045 | { |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 2046 | struct sk_buff *skb; |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 2047 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 2048 | skb = pn533_alloc_skb(dev, mod->len); |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 2049 | if (!skb) |
| 2050 | return NULL; |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 2051 | |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 2052 | memcpy(skb_put(skb, mod->len), &mod->data, mod->len); |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 2053 | |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 2054 | return skb; |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 2055 | } |
| 2056 | |
| 2057 | static int pn533_send_poll_frame(struct pn533 *dev) |
| 2058 | { |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 2059 | struct pn533_poll_modulations *mod; |
| 2060 | struct sk_buff *skb; |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 2061 | int rc; |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 2062 | u8 cmd_code; |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 2063 | |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 2064 | mod = dev->poll_mod_active[dev->poll_mod_curr]; |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 2065 | |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 2066 | dev_dbg(&dev->interface->dev, "%s mod len %d\n", |
| 2067 | __func__, mod->len); |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 2068 | |
Samuel Ortiz | 673088f | 2013-08-21 15:06:55 +0200 | [diff] [blame] | 2069 | if (dev->poll_dep) { |
| 2070 | dev->poll_dep = 0; |
| 2071 | return pn533_poll_dep(dev->nfc_dev); |
| 2072 | } |
| 2073 | |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 2074 | if (mod->len == 0) { /* Listen mode */ |
| 2075 | cmd_code = PN533_CMD_TG_INIT_AS_TARGET; |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 2076 | skb = pn533_alloc_poll_tg_frame(dev); |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 2077 | } else { /* Polling mode */ |
| 2078 | cmd_code = PN533_CMD_IN_LIST_PASSIVE_TARGET; |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 2079 | skb = pn533_alloc_poll_in_frame(dev, mod); |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 2080 | } |
| 2081 | |
| 2082 | if (!skb) { |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 2083 | nfc_err(&dev->interface->dev, "Failed to allocate skb\n"); |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 2084 | return -ENOMEM; |
| 2085 | } |
| 2086 | |
| 2087 | rc = pn533_send_cmd_async(dev, cmd_code, skb, pn533_poll_complete, |
| 2088 | NULL); |
| 2089 | if (rc < 0) { |
| 2090 | dev_kfree_skb(skb); |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 2091 | nfc_err(&dev->interface->dev, "Polling loop error %d\n", rc); |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 2092 | } |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 2093 | |
| 2094 | return rc; |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 2095 | } |
| 2096 | |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 2097 | static void pn533_wq_poll(struct work_struct *work) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2098 | { |
Samuel Ortiz | 46f793b | 2013-07-03 14:50:36 +0200 | [diff] [blame] | 2099 | struct pn533 *dev = container_of(work, struct pn533, poll_work.work); |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 2100 | struct pn533_poll_modulations *cur_mod; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2101 | int rc; |
| 2102 | |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 2103 | cur_mod = dev->poll_mod_active[dev->poll_mod_curr]; |
| 2104 | |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 2105 | dev_dbg(&dev->interface->dev, |
| 2106 | "%s cancel_listen %d modulation len %d\n", |
| 2107 | __func__, dev->cancel_listen, cur_mod->len); |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 2108 | |
| 2109 | if (dev->cancel_listen == 1) { |
| 2110 | dev->cancel_listen = 0; |
Waldemar Rymarkiewicz | 10cff29 | 2013-04-03 08:02:14 +0200 | [diff] [blame] | 2111 | pn533_abort_cmd(dev, GFP_ATOMIC); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2112 | } |
| 2113 | |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 2114 | rc = pn533_send_poll_frame(dev); |
| 2115 | if (rc) |
| 2116 | return; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2117 | |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 2118 | if (cur_mod->len == 0 && dev->poll_mod_count > 1) |
| 2119 | mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2120 | |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 2121 | return; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2122 | } |
| 2123 | |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 2124 | static int pn533_start_poll(struct nfc_dev *nfc_dev, |
| 2125 | u32 im_protocols, u32 tm_protocols) |
| 2126 | { |
| 2127 | struct pn533 *dev = nfc_get_drvdata(nfc_dev); |
Samuel Ortiz | cec4b8e | 2013-08-21 15:12:06 +0200 | [diff] [blame] | 2128 | struct pn533_poll_modulations *cur_mod; |
Samuel Ortiz | dfccd0f | 2013-07-03 15:14:17 +0200 | [diff] [blame] | 2129 | u8 rand_mod; |
Samuel Ortiz | cec4b8e | 2013-08-21 15:12:06 +0200 | [diff] [blame] | 2130 | int rc; |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 2131 | |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 2132 | dev_dbg(&dev->interface->dev, |
| 2133 | "%s: im protocols 0x%x tm protocols 0x%x\n", |
| 2134 | __func__, im_protocols, tm_protocols); |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 2135 | |
| 2136 | if (dev->tgt_active_prot) { |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 2137 | nfc_err(&dev->interface->dev, |
| 2138 | "Cannot poll with a target already activated\n"); |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 2139 | return -EBUSY; |
| 2140 | } |
| 2141 | |
Samuel Ortiz | 51ad304 | 2012-05-31 20:01:32 +0200 | [diff] [blame] | 2142 | if (dev->tgt_mode) { |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 2143 | nfc_err(&dev->interface->dev, |
| 2144 | "Cannot poll while already being activated\n"); |
Samuel Ortiz | 51ad304 | 2012-05-31 20:01:32 +0200 | [diff] [blame] | 2145 | return -EBUSY; |
| 2146 | } |
| 2147 | |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 2148 | if (tm_protocols) { |
| 2149 | dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len); |
| 2150 | if (dev->gb == NULL) |
| 2151 | tm_protocols = 0; |
| 2152 | } |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 2153 | |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 2154 | pn533_poll_create_mod_list(dev, im_protocols, tm_protocols); |
| 2155 | dev->poll_protocols = im_protocols; |
| 2156 | dev->listen_protocols = tm_protocols; |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 2157 | |
Samuel Ortiz | dfccd0f | 2013-07-03 15:14:17 +0200 | [diff] [blame] | 2158 | /* Do not always start polling from the same modulation */ |
| 2159 | get_random_bytes(&rand_mod, sizeof(rand_mod)); |
| 2160 | rand_mod %= dev->poll_mod_count; |
| 2161 | dev->poll_mod_curr = rand_mod; |
| 2162 | |
Samuel Ortiz | cec4b8e | 2013-08-21 15:12:06 +0200 | [diff] [blame] | 2163 | cur_mod = dev->poll_mod_active[dev->poll_mod_curr]; |
| 2164 | |
| 2165 | rc = pn533_send_poll_frame(dev); |
| 2166 | |
| 2167 | /* Start listen timer */ |
| 2168 | if (!rc && cur_mod->len == 0 && dev->poll_mod_count > 1) |
| 2169 | mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ); |
| 2170 | |
| 2171 | return rc; |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 2172 | } |
| 2173 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2174 | static void pn533_stop_poll(struct nfc_dev *nfc_dev) |
| 2175 | { |
| 2176 | struct pn533 *dev = nfc_get_drvdata(nfc_dev); |
| 2177 | |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 2178 | del_timer(&dev->listen_timer); |
| 2179 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2180 | if (!dev->poll_mod_count) { |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 2181 | dev_dbg(&dev->interface->dev, |
| 2182 | "Polling operation was not running\n"); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2183 | return; |
| 2184 | } |
| 2185 | |
Waldemar Rymarkiewicz | 10cff29 | 2013-04-03 08:02:14 +0200 | [diff] [blame] | 2186 | pn533_abort_cmd(dev, GFP_KERNEL); |
Samuel Ortiz | 46f793b | 2013-07-03 14:50:36 +0200 | [diff] [blame] | 2187 | flush_delayed_work(&dev->poll_work); |
Samuel Ortiz | 7c2a04a93 | 2012-05-21 16:20:01 +0200 | [diff] [blame] | 2188 | pn533_poll_reset_mod_list(dev); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2189 | } |
| 2190 | |
| 2191 | static int pn533_activate_target_nfcdep(struct pn533 *dev) |
| 2192 | { |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 2193 | struct pn533_cmd_activate_response *rsp; |
Samuel Ortiz | 541d920 | 2011-12-14 16:43:10 +0100 | [diff] [blame] | 2194 | u16 gt_len; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2195 | int rc; |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 2196 | struct sk_buff *skb; |
| 2197 | struct sk_buff *resp; |
| 2198 | |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 2199 | dev_dbg(&dev->interface->dev, "%s\n", __func__); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2200 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 2201 | skb = pn533_alloc_skb(dev, sizeof(u8) * 2); /*TG + Next*/ |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 2202 | if (!skb) |
| 2203 | return -ENOMEM; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2204 | |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 2205 | *skb_put(skb, sizeof(u8)) = 1; /* TG */ |
| 2206 | *skb_put(skb, sizeof(u8)) = 0; /* Next */ |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2207 | |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 2208 | resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_ATR, skb); |
| 2209 | if (IS_ERR(resp)) |
| 2210 | return PTR_ERR(resp); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2211 | |
Waldemar Rymarkiewicz | 37cf4fc | 2012-12-10 14:42:50 +0100 | [diff] [blame] | 2212 | rsp = (struct pn533_cmd_activate_response *)resp->data; |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 2213 | rc = rsp->status & PN533_CMD_RET_MASK; |
Waldemar Rymarkiewicz | 8a0ecfe | 2012-12-21 10:39:01 +0100 | [diff] [blame] | 2214 | if (rc != PN533_CMD_RET_SUCCESS) { |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 2215 | nfc_err(&dev->interface->dev, |
| 2216 | "Target activation failed (error 0x%x)\n", rc); |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 2217 | dev_kfree_skb(resp); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2218 | return -EIO; |
Waldemar Rymarkiewicz | 8a0ecfe | 2012-12-21 10:39:01 +0100 | [diff] [blame] | 2219 | } |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2220 | |
Samuel Ortiz | 541d920 | 2011-12-14 16:43:10 +0100 | [diff] [blame] | 2221 | /* ATR_RES general bytes are located at offset 16 */ |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 2222 | gt_len = resp->len - 16; |
| 2223 | 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] | 2224 | |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 2225 | dev_kfree_skb(resp); |
Samuel Ortiz | 541d920 | 2011-12-14 16:43:10 +0100 | [diff] [blame] | 2226 | return rc; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2227 | } |
| 2228 | |
Eric Lapuyade | 9009943 | 2012-05-07 12:31:13 +0200 | [diff] [blame] | 2229 | static int pn533_activate_target(struct nfc_dev *nfc_dev, |
| 2230 | struct nfc_target *target, u32 protocol) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2231 | { |
| 2232 | struct pn533 *dev = nfc_get_drvdata(nfc_dev); |
| 2233 | int rc; |
| 2234 | |
Joe Perches | 17936b4 | 2013-04-05 12:27:39 -0700 | [diff] [blame] | 2235 | dev_dbg(&dev->interface->dev, "%s: protocol=%u\n", __func__, protocol); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2236 | |
| 2237 | if (dev->poll_mod_count) { |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 2238 | nfc_err(&dev->interface->dev, |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 2239 | "Cannot activate while polling\n"); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2240 | return -EBUSY; |
| 2241 | } |
| 2242 | |
| 2243 | if (dev->tgt_active_prot) { |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 2244 | nfc_err(&dev->interface->dev, |
| 2245 | "There is already an active target\n"); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2246 | return -EBUSY; |
| 2247 | } |
| 2248 | |
| 2249 | if (!dev->tgt_available_prots) { |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 2250 | nfc_err(&dev->interface->dev, |
| 2251 | "There is no available target to activate\n"); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2252 | return -EINVAL; |
| 2253 | } |
| 2254 | |
| 2255 | if (!(dev->tgt_available_prots & (1 << protocol))) { |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 2256 | nfc_err(&dev->interface->dev, |
| 2257 | "Target doesn't support requested proto %u\n", |
| 2258 | protocol); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2259 | return -EINVAL; |
| 2260 | } |
| 2261 | |
| 2262 | if (protocol == NFC_PROTO_NFC_DEP) { |
| 2263 | rc = pn533_activate_target_nfcdep(dev); |
| 2264 | if (rc) { |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 2265 | nfc_err(&dev->interface->dev, |
| 2266 | "Activating target with DEP failed %d\n", rc); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2267 | return rc; |
| 2268 | } |
| 2269 | } |
| 2270 | |
| 2271 | dev->tgt_active_prot = protocol; |
| 2272 | dev->tgt_available_prots = 0; |
| 2273 | |
| 2274 | return 0; |
| 2275 | } |
| 2276 | |
Eric Lapuyade | 9009943 | 2012-05-07 12:31:13 +0200 | [diff] [blame] | 2277 | static void pn533_deactivate_target(struct nfc_dev *nfc_dev, |
| 2278 | struct nfc_target *target) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2279 | { |
| 2280 | struct pn533 *dev = nfc_get_drvdata(nfc_dev); |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 2281 | struct sk_buff *skb; |
| 2282 | struct sk_buff *resp; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2283 | int rc; |
| 2284 | |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 2285 | dev_dbg(&dev->interface->dev, "%s\n", __func__); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2286 | |
| 2287 | if (!dev->tgt_active_prot) { |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 2288 | nfc_err(&dev->interface->dev, "There is no active target\n"); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2289 | return; |
| 2290 | } |
| 2291 | |
| 2292 | dev->tgt_active_prot = 0; |
Samuel Ortiz | 6ff73fd | 2012-04-10 19:43:18 +0200 | [diff] [blame] | 2293 | skb_queue_purge(&dev->resp_q); |
| 2294 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 2295 | skb = pn533_alloc_skb(dev, sizeof(u8)); |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 2296 | if (!skb) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2297 | return; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2298 | |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 2299 | *skb_put(skb, 1) = 1; /* TG*/ |
| 2300 | |
| 2301 | resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_RELEASE, skb); |
| 2302 | if (IS_ERR(resp)) |
| 2303 | return; |
| 2304 | |
| 2305 | rc = resp->data[0] & PN533_CMD_RET_MASK; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2306 | if (rc != PN533_CMD_RET_SUCCESS) |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 2307 | nfc_err(&dev->interface->dev, |
| 2308 | "Error 0x%x when releasing the target\n", rc); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2309 | |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 2310 | dev_kfree_skb(resp); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2311 | return; |
| 2312 | } |
| 2313 | |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 2314 | |
| 2315 | 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] | 2316 | struct sk_buff *resp) |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 2317 | { |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 2318 | struct pn533_cmd_jump_dep_response *rsp; |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 2319 | u8 target_gt_len; |
| 2320 | int rc; |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 2321 | u8 active = *(u8 *)arg; |
Waldemar Rymarkiewicz | 70418e6 | 2012-10-11 14:04:00 +0200 | [diff] [blame] | 2322 | |
| 2323 | kfree(arg); |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 2324 | |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 2325 | if (IS_ERR(resp)) |
| 2326 | return PTR_ERR(resp); |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 2327 | |
| 2328 | if (dev->tgt_available_prots && |
| 2329 | !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) { |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 2330 | nfc_err(&dev->interface->dev, |
| 2331 | "The target does not support DEP\n"); |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 2332 | rc = -EINVAL; |
| 2333 | goto error; |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 2334 | } |
| 2335 | |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 2336 | rsp = (struct pn533_cmd_jump_dep_response *)resp->data; |
| 2337 | |
| 2338 | rc = rsp->status & PN533_CMD_RET_MASK; |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 2339 | if (rc != PN533_CMD_RET_SUCCESS) { |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 2340 | nfc_err(&dev->interface->dev, |
| 2341 | "Bringing DEP link up failed (error 0x%x)\n", rc); |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 2342 | goto error; |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 2343 | } |
| 2344 | |
| 2345 | if (!dev->tgt_available_prots) { |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 2346 | struct nfc_target nfc_target; |
| 2347 | |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 2348 | dev_dbg(&dev->interface->dev, "Creating new target\n"); |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 2349 | |
| 2350 | nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK; |
Samuel Ortiz | 2fbabfa | 2012-03-05 01:03:47 +0100 | [diff] [blame] | 2351 | nfc_target.nfcid1_len = 10; |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 2352 | memcpy(nfc_target.nfcid1, rsp->nfcid3t, nfc_target.nfcid1_len); |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 2353 | rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1); |
| 2354 | if (rc) |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 2355 | goto error; |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 2356 | |
| 2357 | dev->tgt_available_prots = 0; |
| 2358 | } |
| 2359 | |
| 2360 | dev->tgt_active_prot = NFC_PROTO_NFC_DEP; |
| 2361 | |
| 2362 | /* ATR_RES general bytes are located at offset 17 */ |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 2363 | target_gt_len = resp->len - 17; |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 2364 | rc = nfc_set_remote_general_bytes(dev->nfc_dev, |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 2365 | rsp->gt, target_gt_len); |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 2366 | if (rc == 0) |
| 2367 | rc = nfc_dep_link_is_up(dev->nfc_dev, |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 2368 | dev->nfc_dev->targets[0].idx, |
| 2369 | !active, NFC_RF_INITIATOR); |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 2370 | |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 2371 | error: |
| 2372 | dev_kfree_skb(resp); |
| 2373 | return rc; |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 2374 | } |
| 2375 | |
Samuel Ortiz | 17e9d9d | 2013-06-26 18:16:21 +0200 | [diff] [blame] | 2376 | static int pn533_rf_field(struct nfc_dev *nfc_dev, u8 rf); |
Eric Lapuyade | 9009943 | 2012-05-07 12:31:13 +0200 | [diff] [blame] | 2377 | 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] | 2378 | u8 comm_mode, u8 *gb, size_t gb_len) |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 2379 | { |
| 2380 | struct pn533 *dev = nfc_get_drvdata(nfc_dev); |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 2381 | struct sk_buff *skb; |
Samuel Ortiz | 5eef484 | 2013-07-01 10:58:12 +0200 | [diff] [blame] | 2382 | int rc, skb_len; |
| 2383 | u8 *next, *arg, nfcid3[NFC_NFCID3_MAXSIZE]; |
Samuel Ortiz | d7f3345 | 2012-05-29 21:45:21 +0200 | [diff] [blame] | 2384 | u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3}; |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 2385 | |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 2386 | dev_dbg(&dev->interface->dev, "%s\n", __func__); |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 2387 | |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 2388 | if (dev->poll_mod_count) { |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 2389 | nfc_err(&dev->interface->dev, |
| 2390 | "Cannot bring the DEP link up while polling\n"); |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 2391 | return -EBUSY; |
| 2392 | } |
| 2393 | |
| 2394 | if (dev->tgt_active_prot) { |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 2395 | nfc_err(&dev->interface->dev, |
| 2396 | "There is already an active target\n"); |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 2397 | return -EBUSY; |
| 2398 | } |
| 2399 | |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 2400 | skb_len = 3 + gb_len; /* ActPass + BR + Next */ |
Samuel Ortiz | 5eef484 | 2013-07-01 10:58:12 +0200 | [diff] [blame] | 2401 | skb_len += PASSIVE_DATA_LEN; |
Samuel Ortiz | d7f3345 | 2012-05-29 21:45:21 +0200 | [diff] [blame] | 2402 | |
Samuel Ortiz | 5eef484 | 2013-07-01 10:58:12 +0200 | [diff] [blame] | 2403 | /* NFCID3 */ |
| 2404 | skb_len += NFC_NFCID3_MAXSIZE; |
| 2405 | if (target && !target->nfcid2_len) { |
| 2406 | nfcid3[0] = 0x1; |
| 2407 | nfcid3[1] = 0xfe; |
| 2408 | get_random_bytes(nfcid3 + 2, 6); |
| 2409 | } |
Samuel Ortiz | 322bce9 | 2013-05-27 15:29:11 +0200 | [diff] [blame] | 2410 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 2411 | skb = pn533_alloc_skb(dev, skb_len); |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 2412 | if (!skb) |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 2413 | return -ENOMEM; |
| 2414 | |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 2415 | *skb_put(skb, 1) = !comm_mode; /* ActPass */ |
Samuel Ortiz | 5eef484 | 2013-07-01 10:58:12 +0200 | [diff] [blame] | 2416 | *skb_put(skb, 1) = 0x02; /* 424 kbps */ |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 2417 | |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 2418 | next = skb_put(skb, 1); /* Next */ |
| 2419 | *next = 0; |
| 2420 | |
Samuel Ortiz | 5eef484 | 2013-07-01 10:58:12 +0200 | [diff] [blame] | 2421 | /* Copy passive data */ |
| 2422 | memcpy(skb_put(skb, PASSIVE_DATA_LEN), passive_data, PASSIVE_DATA_LEN); |
| 2423 | *next |= 1; |
Samuel Ortiz | d7f3345 | 2012-05-29 21:45:21 +0200 | [diff] [blame] | 2424 | |
Samuel Ortiz | 5eef484 | 2013-07-01 10:58:12 +0200 | [diff] [blame] | 2425 | /* Copy NFCID3 (which is NFCID2 from SENSF_RES) */ |
| 2426 | if (target && target->nfcid2_len) |
Samuel Ortiz | 322bce9 | 2013-05-27 15:29:11 +0200 | [diff] [blame] | 2427 | memcpy(skb_put(skb, NFC_NFCID3_MAXSIZE), target->nfcid2, |
| 2428 | target->nfcid2_len); |
Samuel Ortiz | 5eef484 | 2013-07-01 10:58:12 +0200 | [diff] [blame] | 2429 | else |
| 2430 | memcpy(skb_put(skb, NFC_NFCID3_MAXSIZE), nfcid3, |
| 2431 | NFC_NFCID3_MAXSIZE); |
| 2432 | *next |= 2; |
Samuel Ortiz | 322bce9 | 2013-05-27 15:29:11 +0200 | [diff] [blame] | 2433 | |
Samuel Ortiz | 47807d3 | 2012-03-05 01:03:50 +0100 | [diff] [blame] | 2434 | if (gb != NULL && gb_len > 0) { |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 2435 | memcpy(skb_put(skb, gb_len), gb, gb_len); |
| 2436 | *next |= 4; /* We have some Gi */ |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 2437 | } else { |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 2438 | *next = 0; |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 2439 | } |
| 2440 | |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 2441 | arg = kmalloc(sizeof(*arg), GFP_KERNEL); |
| 2442 | if (!arg) { |
| 2443 | dev_kfree_skb(skb); |
| 2444 | return -ENOMEM; |
| 2445 | } |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 2446 | |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 2447 | *arg = !comm_mode; |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 2448 | |
Samuel Ortiz | 17e9d9d | 2013-06-26 18:16:21 +0200 | [diff] [blame] | 2449 | pn533_rf_field(dev->nfc_dev, 0); |
| 2450 | |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 2451 | rc = pn533_send_cmd_async(dev, PN533_CMD_IN_JUMP_FOR_DEP, skb, |
| 2452 | pn533_in_dep_link_up_complete, arg); |
| 2453 | |
| 2454 | if (rc < 0) { |
| 2455 | dev_kfree_skb(skb); |
| 2456 | kfree(arg); |
| 2457 | } |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 2458 | |
| 2459 | return rc; |
| 2460 | } |
| 2461 | |
| 2462 | static int pn533_dep_link_down(struct nfc_dev *nfc_dev) |
| 2463 | { |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 2464 | struct pn533 *dev = nfc_get_drvdata(nfc_dev); |
| 2465 | |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 2466 | dev_dbg(&dev->interface->dev, "%s\n", __func__); |
Waldemar Rymarkiewicz | fcfafc7 | 2012-12-10 14:42:55 +0100 | [diff] [blame] | 2467 | |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 2468 | pn533_poll_reset_mod_list(dev); |
| 2469 | |
Waldemar Rymarkiewicz | 10cff29 | 2013-04-03 08:02:14 +0200 | [diff] [blame] | 2470 | if (dev->tgt_mode || dev->tgt_active_prot) |
| 2471 | pn533_abort_cmd(dev, GFP_KERNEL); |
Samuel Ortiz | 51ad304 | 2012-05-31 20:01:32 +0200 | [diff] [blame] | 2472 | |
| 2473 | dev->tgt_active_prot = 0; |
| 2474 | dev->tgt_mode = 0; |
| 2475 | |
| 2476 | skb_queue_purge(&dev->resp_q); |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 2477 | |
| 2478 | return 0; |
| 2479 | } |
| 2480 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2481 | struct pn533_data_exchange_arg { |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2482 | data_exchange_cb_t cb; |
| 2483 | void *cb_context; |
| 2484 | }; |
| 2485 | |
Samuel Ortiz | 6ff73fd | 2012-04-10 19:43:18 +0200 | [diff] [blame] | 2486 | static struct sk_buff *pn533_build_response(struct pn533 *dev) |
| 2487 | { |
| 2488 | struct sk_buff *skb, *tmp, *t; |
| 2489 | unsigned int skb_len = 0, tmp_len = 0; |
| 2490 | |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 2491 | dev_dbg(&dev->interface->dev, "%s\n", __func__); |
Samuel Ortiz | 6ff73fd | 2012-04-10 19:43:18 +0200 | [diff] [blame] | 2492 | |
| 2493 | if (skb_queue_empty(&dev->resp_q)) |
| 2494 | return NULL; |
| 2495 | |
| 2496 | if (skb_queue_len(&dev->resp_q) == 1) { |
| 2497 | skb = skb_dequeue(&dev->resp_q); |
| 2498 | goto out; |
| 2499 | } |
| 2500 | |
| 2501 | skb_queue_walk_safe(&dev->resp_q, tmp, t) |
| 2502 | skb_len += tmp->len; |
| 2503 | |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 2504 | dev_dbg(&dev->interface->dev, "%s total length %d\n", |
| 2505 | __func__, skb_len); |
Samuel Ortiz | 6ff73fd | 2012-04-10 19:43:18 +0200 | [diff] [blame] | 2506 | |
| 2507 | skb = alloc_skb(skb_len, GFP_KERNEL); |
| 2508 | if (skb == NULL) |
| 2509 | goto out; |
| 2510 | |
| 2511 | skb_put(skb, skb_len); |
| 2512 | |
| 2513 | skb_queue_walk_safe(&dev->resp_q, tmp, t) { |
| 2514 | memcpy(skb->data + tmp_len, tmp->data, tmp->len); |
| 2515 | tmp_len += tmp->len; |
| 2516 | } |
| 2517 | |
| 2518 | out: |
| 2519 | skb_queue_purge(&dev->resp_q); |
| 2520 | |
| 2521 | return skb; |
| 2522 | } |
| 2523 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2524 | static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg, |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2525 | struct sk_buff *resp) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2526 | { |
| 2527 | struct pn533_data_exchange_arg *arg = _arg; |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2528 | struct sk_buff *skb; |
| 2529 | int rc = 0; |
| 2530 | u8 status, ret, mi; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2531 | |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 2532 | dev_dbg(&dev->interface->dev, "%s\n", __func__); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2533 | |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2534 | if (IS_ERR(resp)) { |
| 2535 | rc = PTR_ERR(resp); |
| 2536 | goto _error; |
| 2537 | } |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2538 | |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2539 | status = resp->data[0]; |
| 2540 | ret = status & PN533_CMD_RET_MASK; |
| 2541 | mi = status & PN533_CMD_MI_MASK; |
| 2542 | |
| 2543 | skb_pull(resp, sizeof(status)); |
| 2544 | |
| 2545 | if (ret != PN533_CMD_RET_SUCCESS) { |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 2546 | nfc_err(&dev->interface->dev, |
| 2547 | "Exchanging data failed (error 0x%x)\n", ret); |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2548 | rc = -EIO; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2549 | goto error; |
| 2550 | } |
| 2551 | |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2552 | skb_queue_tail(&dev->resp_q, resp); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2553 | |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2554 | if (mi) { |
| 2555 | dev->cmd_complete_mi_arg = arg; |
Olivier Guiter | 963a82e | 2013-06-13 13:43:28 +0000 | [diff] [blame] | 2556 | queue_work(dev->wq, &dev->mi_rx_work); |
| 2557 | return -EINPROGRESS; |
| 2558 | } |
| 2559 | |
| 2560 | /* Prepare for the next round */ |
| 2561 | if (skb_queue_len(&dev->fragment_skb) > 0) { |
| 2562 | dev->cmd_complete_dep_arg = arg; |
| 2563 | queue_work(dev->wq, &dev->mi_tx_work); |
| 2564 | |
Samuel Ortiz | 6ff73fd | 2012-04-10 19:43:18 +0200 | [diff] [blame] | 2565 | return -EINPROGRESS; |
| 2566 | } |
| 2567 | |
| 2568 | skb = pn533_build_response(dev); |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2569 | if (!skb) |
Samuel Ortiz | 6ff73fd | 2012-04-10 19:43:18 +0200 | [diff] [blame] | 2570 | goto error; |
| 2571 | |
| 2572 | arg->cb(arg->cb_context, skb, 0); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2573 | kfree(arg); |
| 2574 | return 0; |
| 2575 | |
| 2576 | error: |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2577 | dev_kfree_skb(resp); |
| 2578 | _error: |
Samuel Ortiz | 6ff73fd | 2012-04-10 19:43:18 +0200 | [diff] [blame] | 2579 | skb_queue_purge(&dev->resp_q); |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2580 | arg->cb(arg->cb_context, NULL, rc); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2581 | kfree(arg); |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2582 | return rc; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2583 | } |
| 2584 | |
Olivier Guiter | 963a82e | 2013-06-13 13:43:28 +0000 | [diff] [blame] | 2585 | /* Split the Tx skb into small chunks */ |
| 2586 | static int pn533_fill_fragment_skbs(struct pn533 *dev, struct sk_buff *skb) |
| 2587 | { |
| 2588 | struct sk_buff *frag; |
| 2589 | int frag_size; |
| 2590 | |
| 2591 | do { |
| 2592 | /* Remaining size */ |
| 2593 | if (skb->len > PN533_CMD_DATAFRAME_MAXLEN) |
| 2594 | frag_size = PN533_CMD_DATAFRAME_MAXLEN; |
| 2595 | else |
| 2596 | frag_size = skb->len; |
| 2597 | |
| 2598 | /* Allocate and reserve */ |
| 2599 | frag = pn533_alloc_skb(dev, frag_size); |
| 2600 | if (!frag) { |
| 2601 | skb_queue_purge(&dev->fragment_skb); |
| 2602 | break; |
| 2603 | } |
| 2604 | |
Olivier Guiter | 22953f9 | 2013-09-23 12:24:37 +0200 | [diff] [blame] | 2605 | if (!dev->tgt_mode) { |
| 2606 | /* Reserve the TG/MI byte */ |
| 2607 | skb_reserve(frag, 1); |
Olivier Guiter | 963a82e | 2013-06-13 13:43:28 +0000 | [diff] [blame] | 2608 | |
Olivier Guiter | 22953f9 | 2013-09-23 12:24:37 +0200 | [diff] [blame] | 2609 | /* MI + TG */ |
| 2610 | if (frag_size == PN533_CMD_DATAFRAME_MAXLEN) |
| 2611 | *skb_push(frag, sizeof(u8)) = |
| 2612 | (PN533_CMD_MI_MASK | 1); |
| 2613 | else |
| 2614 | *skb_push(frag, sizeof(u8)) = 1; /* TG */ |
| 2615 | } |
Olivier Guiter | 963a82e | 2013-06-13 13:43:28 +0000 | [diff] [blame] | 2616 | |
| 2617 | memcpy(skb_put(frag, frag_size), skb->data, frag_size); |
| 2618 | |
| 2619 | /* Reduce the size of incoming buffer */ |
| 2620 | skb_pull(skb, frag_size); |
| 2621 | |
| 2622 | /* Add this to skb_queue */ |
| 2623 | skb_queue_tail(&dev->fragment_skb, frag); |
| 2624 | |
| 2625 | } while (skb->len > 0); |
| 2626 | |
| 2627 | dev_kfree_skb(skb); |
| 2628 | |
| 2629 | return skb_queue_len(&dev->fragment_skb); |
| 2630 | } |
| 2631 | |
Samuel Ortiz | be9ae4c | 2012-05-16 15:55:48 +0200 | [diff] [blame] | 2632 | static int pn533_transceive(struct nfc_dev *nfc_dev, |
| 2633 | struct nfc_target *target, struct sk_buff *skb, |
| 2634 | data_exchange_cb_t cb, void *cb_context) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2635 | { |
| 2636 | struct pn533 *dev = nfc_get_drvdata(nfc_dev); |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2637 | struct pn533_data_exchange_arg *arg = NULL; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2638 | int rc; |
| 2639 | |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 2640 | dev_dbg(&dev->interface->dev, "%s\n", __func__); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2641 | |
| 2642 | if (!dev->tgt_active_prot) { |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 2643 | nfc_err(&dev->interface->dev, |
| 2644 | "Can't exchange data if there is no active target\n"); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2645 | rc = -EINVAL; |
| 2646 | goto error; |
| 2647 | } |
| 2648 | |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2649 | arg = kmalloc(sizeof(*arg), GFP_KERNEL); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2650 | if (!arg) { |
| 2651 | rc = -ENOMEM; |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2652 | goto error; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2653 | } |
| 2654 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2655 | arg->cb = cb; |
| 2656 | arg->cb_context = cb_context; |
| 2657 | |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2658 | switch (dev->device_type) { |
| 2659 | case PN533_DEVICE_PASORI: |
| 2660 | if (dev->tgt_active_prot == NFC_PROTO_FELICA) { |
| 2661 | rc = pn533_send_data_async(dev, PN533_CMD_IN_COMM_THRU, |
| 2662 | skb, |
| 2663 | pn533_data_exchange_complete, |
| 2664 | arg); |
| 2665 | |
| 2666 | break; |
| 2667 | } |
| 2668 | default: |
Olivier Guiter | 963a82e | 2013-06-13 13:43:28 +0000 | [diff] [blame] | 2669 | /* jumbo frame ? */ |
| 2670 | if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) { |
| 2671 | rc = pn533_fill_fragment_skbs(dev, skb); |
| 2672 | if (rc <= 0) |
| 2673 | goto error; |
| 2674 | |
| 2675 | skb = skb_dequeue(&dev->fragment_skb); |
| 2676 | if (!skb) { |
| 2677 | rc = -EIO; |
| 2678 | goto error; |
| 2679 | } |
| 2680 | } else { |
| 2681 | *skb_push(skb, sizeof(u8)) = 1; /* TG */ |
| 2682 | } |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2683 | |
| 2684 | rc = pn533_send_data_async(dev, PN533_CMD_IN_DATA_EXCHANGE, |
| 2685 | skb, pn533_data_exchange_complete, |
| 2686 | arg); |
| 2687 | |
| 2688 | break; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2689 | } |
| 2690 | |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2691 | if (rc < 0) /* rc from send_async */ |
| 2692 | goto error; |
| 2693 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2694 | return 0; |
| 2695 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2696 | error: |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2697 | kfree(arg); |
| 2698 | dev_kfree_skb(skb); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2699 | return rc; |
| 2700 | } |
| 2701 | |
Samuel Ortiz | dadb06f | 2012-05-31 00:09:11 +0200 | [diff] [blame] | 2702 | static int pn533_tm_send_complete(struct pn533 *dev, void *arg, |
Waldemar Rymarkiewicz | e487882 | 2012-12-10 14:42:44 +0100 | [diff] [blame] | 2703 | struct sk_buff *resp) |
Samuel Ortiz | dadb06f | 2012-05-31 00:09:11 +0200 | [diff] [blame] | 2704 | { |
Waldemar Rymarkiewicz | e487882 | 2012-12-10 14:42:44 +0100 | [diff] [blame] | 2705 | u8 status; |
Thierry Escande | 5b412fd | 2012-11-15 18:24:28 +0100 | [diff] [blame] | 2706 | |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 2707 | dev_dbg(&dev->interface->dev, "%s\n", __func__); |
Samuel Ortiz | dadb06f | 2012-05-31 00:09:11 +0200 | [diff] [blame] | 2708 | |
Waldemar Rymarkiewicz | e487882 | 2012-12-10 14:42:44 +0100 | [diff] [blame] | 2709 | if (IS_ERR(resp)) |
| 2710 | return PTR_ERR(resp); |
Thierry Escande | 5b412fd | 2012-11-15 18:24:28 +0100 | [diff] [blame] | 2711 | |
Waldemar Rymarkiewicz | e487882 | 2012-12-10 14:42:44 +0100 | [diff] [blame] | 2712 | status = resp->data[0]; |
Samuel Ortiz | dadb06f | 2012-05-31 00:09:11 +0200 | [diff] [blame] | 2713 | |
Olivier Guiter | 93ad420 | 2013-09-23 12:24:39 +0200 | [diff] [blame] | 2714 | /* Prepare for the next round */ |
| 2715 | if (skb_queue_len(&dev->fragment_skb) > 0) { |
| 2716 | queue_work(dev->wq, &dev->mi_tm_tx_work); |
| 2717 | return -EINPROGRESS; |
| 2718 | } |
Waldemar Rymarkiewicz | e487882 | 2012-12-10 14:42:44 +0100 | [diff] [blame] | 2719 | dev_kfree_skb(resp); |
Samuel Ortiz | dadb06f | 2012-05-31 00:09:11 +0200 | [diff] [blame] | 2720 | |
Waldemar Rymarkiewicz | e487882 | 2012-12-10 14:42:44 +0100 | [diff] [blame] | 2721 | if (status != 0) { |
Samuel Ortiz | dadb06f | 2012-05-31 00:09:11 +0200 | [diff] [blame] | 2722 | nfc_tm_deactivated(dev->nfc_dev); |
| 2723 | |
Samuel Ortiz | 51ad304 | 2012-05-31 20:01:32 +0200 | [diff] [blame] | 2724 | dev->tgt_mode = 0; |
| 2725 | |
Samuel Ortiz | dadb06f | 2012-05-31 00:09:11 +0200 | [diff] [blame] | 2726 | return 0; |
| 2727 | } |
| 2728 | |
| 2729 | queue_work(dev->wq, &dev->tg_work); |
| 2730 | |
| 2731 | return 0; |
| 2732 | } |
| 2733 | |
| 2734 | static int pn533_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb) |
| 2735 | { |
| 2736 | struct pn533 *dev = nfc_get_drvdata(nfc_dev); |
Samuel Ortiz | dadb06f | 2012-05-31 00:09:11 +0200 | [diff] [blame] | 2737 | int rc; |
| 2738 | |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 2739 | dev_dbg(&dev->interface->dev, "%s\n", __func__); |
Samuel Ortiz | dadb06f | 2012-05-31 00:09:11 +0200 | [diff] [blame] | 2740 | |
Olivier Guiter | 93ad420 | 2013-09-23 12:24:39 +0200 | [diff] [blame] | 2741 | /* let's split in multiple chunks if size's too big */ |
Waldemar Rymarkiewicz | e487882 | 2012-12-10 14:42:44 +0100 | [diff] [blame] | 2742 | if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) { |
Olivier Guiter | 93ad420 | 2013-09-23 12:24:39 +0200 | [diff] [blame] | 2743 | rc = pn533_fill_fragment_skbs(dev, skb); |
| 2744 | if (rc <= 0) |
| 2745 | goto error; |
| 2746 | |
| 2747 | /* get the first skb */ |
| 2748 | skb = skb_dequeue(&dev->fragment_skb); |
| 2749 | if (!skb) { |
| 2750 | rc = -EIO; |
| 2751 | goto error; |
| 2752 | } |
| 2753 | |
| 2754 | rc = pn533_send_data_async(dev, PN533_CMD_TG_SET_META_DATA, skb, |
| 2755 | pn533_tm_send_complete, NULL); |
| 2756 | } else { |
| 2757 | /* Send th skb */ |
| 2758 | rc = pn533_send_data_async(dev, PN533_CMD_TG_SET_DATA, skb, |
| 2759 | pn533_tm_send_complete, NULL); |
Samuel Ortiz | dadb06f | 2012-05-31 00:09:11 +0200 | [diff] [blame] | 2760 | } |
| 2761 | |
Olivier Guiter | 93ad420 | 2013-09-23 12:24:39 +0200 | [diff] [blame] | 2762 | error: |
| 2763 | if (rc < 0) { |
Waldemar Rymarkiewicz | e487882 | 2012-12-10 14:42:44 +0100 | [diff] [blame] | 2764 | dev_kfree_skb(skb); |
Olivier Guiter | 93ad420 | 2013-09-23 12:24:39 +0200 | [diff] [blame] | 2765 | skb_queue_purge(&dev->fragment_skb); |
| 2766 | } |
Samuel Ortiz | dadb06f | 2012-05-31 00:09:11 +0200 | [diff] [blame] | 2767 | |
| 2768 | return rc; |
| 2769 | } |
| 2770 | |
Samuel Ortiz | 6ff73fd | 2012-04-10 19:43:18 +0200 | [diff] [blame] | 2771 | static void pn533_wq_mi_recv(struct work_struct *work) |
| 2772 | { |
Olivier Guiter | 963a82e | 2013-06-13 13:43:28 +0000 | [diff] [blame] | 2773 | struct pn533 *dev = container_of(work, struct pn533, mi_rx_work); |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2774 | struct sk_buff *skb; |
Samuel Ortiz | 6ff73fd | 2012-04-10 19:43:18 +0200 | [diff] [blame] | 2775 | int rc; |
| 2776 | |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 2777 | dev_dbg(&dev->interface->dev, "%s\n", __func__); |
Samuel Ortiz | 6ff73fd | 2012-04-10 19:43:18 +0200 | [diff] [blame] | 2778 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 2779 | skb = pn533_alloc_skb(dev, PN533_CMD_DATAEXCH_HEAD_LEN); |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2780 | if (!skb) |
| 2781 | goto error; |
Samuel Ortiz | 6ff73fd | 2012-04-10 19:43:18 +0200 | [diff] [blame] | 2782 | |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2783 | switch (dev->device_type) { |
| 2784 | case PN533_DEVICE_PASORI: |
| 2785 | if (dev->tgt_active_prot == NFC_PROTO_FELICA) { |
| 2786 | rc = pn533_send_cmd_direct_async(dev, |
| 2787 | PN533_CMD_IN_COMM_THRU, |
| 2788 | skb, |
| 2789 | pn533_data_exchange_complete, |
| 2790 | dev->cmd_complete_mi_arg); |
Samuel Ortiz | 6ff73fd | 2012-04-10 19:43:18 +0200 | [diff] [blame] | 2791 | |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2792 | break; |
| 2793 | } |
| 2794 | default: |
| 2795 | *skb_put(skb, sizeof(u8)) = 1; /*TG*/ |
Samuel Ortiz | 6ff73fd | 2012-04-10 19:43:18 +0200 | [diff] [blame] | 2796 | |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2797 | rc = pn533_send_cmd_direct_async(dev, |
| 2798 | PN533_CMD_IN_DATA_EXCHANGE, |
| 2799 | skb, |
| 2800 | pn533_data_exchange_complete, |
| 2801 | dev->cmd_complete_mi_arg); |
Waldemar Rymarkiewicz | b1bb290 | 2012-11-26 14:18:32 +0100 | [diff] [blame] | 2802 | |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2803 | break; |
Samuel Ortiz | 6ff73fd | 2012-04-10 19:43:18 +0200 | [diff] [blame] | 2804 | } |
| 2805 | |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2806 | if (rc == 0) /* success */ |
Samuel Ortiz | 6ff73fd | 2012-04-10 19:43:18 +0200 | [diff] [blame] | 2807 | return; |
| 2808 | |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 2809 | nfc_err(&dev->interface->dev, |
| 2810 | "Error %d when trying to perform data_exchange\n", rc); |
Samuel Ortiz | 6ff73fd | 2012-04-10 19:43:18 +0200 | [diff] [blame] | 2811 | |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2812 | dev_kfree_skb(skb); |
Waldemar Rymarkiewicz | 140ef7f | 2013-04-03 08:02:07 +0200 | [diff] [blame] | 2813 | kfree(dev->cmd_complete_mi_arg); |
Samuel Ortiz | 6ff73fd | 2012-04-10 19:43:18 +0200 | [diff] [blame] | 2814 | |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2815 | error: |
Samuel Ortiz | 6ff73fd | 2012-04-10 19:43:18 +0200 | [diff] [blame] | 2816 | pn533_send_ack(dev, GFP_KERNEL); |
Samuel Ortiz | 5d50b36 | 2012-08-17 23:47:54 +0200 | [diff] [blame] | 2817 | queue_work(dev->wq, &dev->cmd_work); |
Samuel Ortiz | 6ff73fd | 2012-04-10 19:43:18 +0200 | [diff] [blame] | 2818 | } |
| 2819 | |
Olivier Guiter | 963a82e | 2013-06-13 13:43:28 +0000 | [diff] [blame] | 2820 | static void pn533_wq_mi_send(struct work_struct *work) |
| 2821 | { |
| 2822 | struct pn533 *dev = container_of(work, struct pn533, mi_tx_work); |
| 2823 | struct sk_buff *skb; |
| 2824 | int rc; |
| 2825 | |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 2826 | dev_dbg(&dev->interface->dev, "%s\n", __func__); |
Olivier Guiter | 963a82e | 2013-06-13 13:43:28 +0000 | [diff] [blame] | 2827 | |
| 2828 | /* Grab the first skb in the queue */ |
| 2829 | skb = skb_dequeue(&dev->fragment_skb); |
| 2830 | |
| 2831 | if (skb == NULL) { /* No more data */ |
| 2832 | /* Reset the queue for future use */ |
| 2833 | skb_queue_head_init(&dev->fragment_skb); |
| 2834 | goto error; |
| 2835 | } |
| 2836 | |
| 2837 | switch (dev->device_type) { |
| 2838 | case PN533_DEVICE_PASORI: |
| 2839 | if (dev->tgt_active_prot != NFC_PROTO_FELICA) { |
| 2840 | rc = -EIO; |
| 2841 | break; |
| 2842 | } |
| 2843 | |
| 2844 | rc = pn533_send_cmd_direct_async(dev, PN533_CMD_IN_COMM_THRU, |
| 2845 | skb, |
| 2846 | pn533_data_exchange_complete, |
| 2847 | dev->cmd_complete_dep_arg); |
| 2848 | |
| 2849 | break; |
| 2850 | |
| 2851 | default: |
| 2852 | /* Still some fragments? */ |
| 2853 | rc = pn533_send_cmd_direct_async(dev,PN533_CMD_IN_DATA_EXCHANGE, |
| 2854 | skb, |
| 2855 | pn533_data_exchange_complete, |
| 2856 | dev->cmd_complete_dep_arg); |
| 2857 | |
| 2858 | break; |
| 2859 | } |
| 2860 | |
| 2861 | if (rc == 0) /* success */ |
| 2862 | return; |
| 2863 | |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 2864 | nfc_err(&dev->interface->dev, |
| 2865 | "Error %d when trying to perform data_exchange\n", rc); |
Olivier Guiter | 963a82e | 2013-06-13 13:43:28 +0000 | [diff] [blame] | 2866 | |
| 2867 | dev_kfree_skb(skb); |
| 2868 | kfree(dev->cmd_complete_dep_arg); |
| 2869 | |
| 2870 | error: |
| 2871 | pn533_send_ack(dev, GFP_KERNEL); |
| 2872 | queue_work(dev->wq, &dev->cmd_work); |
| 2873 | } |
| 2874 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2875 | static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata, |
| 2876 | u8 cfgdata_len) |
| 2877 | { |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 2878 | struct sk_buff *skb; |
| 2879 | struct sk_buff *resp; |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 2880 | int skb_len; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2881 | |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 2882 | dev_dbg(&dev->interface->dev, "%s\n", __func__); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2883 | |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 2884 | skb_len = sizeof(cfgitem) + cfgdata_len; /* cfgitem + cfgdata */ |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2885 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 2886 | skb = pn533_alloc_skb(dev, skb_len); |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 2887 | if (!skb) |
| 2888 | return -ENOMEM; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2889 | |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 2890 | *skb_put(skb, sizeof(cfgitem)) = cfgitem; |
| 2891 | memcpy(skb_put(skb, cfgdata_len), cfgdata, cfgdata_len); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2892 | |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 2893 | resp = pn533_send_cmd_sync(dev, PN533_CMD_RF_CONFIGURATION, skb); |
| 2894 | if (IS_ERR(resp)) |
| 2895 | return PTR_ERR(resp); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2896 | |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 2897 | dev_kfree_skb(resp); |
| 2898 | return 0; |
| 2899 | } |
| 2900 | |
| 2901 | static int pn533_get_firmware_version(struct pn533 *dev, |
| 2902 | struct pn533_fw_version *fv) |
| 2903 | { |
| 2904 | struct sk_buff *skb; |
| 2905 | struct sk_buff *resp; |
| 2906 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 2907 | skb = pn533_alloc_skb(dev, 0); |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 2908 | if (!skb) |
| 2909 | return -ENOMEM; |
| 2910 | |
| 2911 | resp = pn533_send_cmd_sync(dev, PN533_CMD_GET_FIRMWARE_VERSION, skb); |
| 2912 | if (IS_ERR(resp)) |
| 2913 | return PTR_ERR(resp); |
| 2914 | |
| 2915 | fv->ic = resp->data[0]; |
| 2916 | fv->ver = resp->data[1]; |
| 2917 | fv->rev = resp->data[2]; |
| 2918 | fv->support = resp->data[3]; |
| 2919 | |
| 2920 | dev_kfree_skb(resp); |
| 2921 | return 0; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2922 | } |
| 2923 | |
Waldemar Rymarkiewicz | f75c291 | 2013-04-03 08:02:02 +0200 | [diff] [blame] | 2924 | static int pn533_pasori_fw_reset(struct pn533 *dev) |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 2925 | { |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 2926 | struct sk_buff *skb; |
| 2927 | struct sk_buff *resp; |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 2928 | |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 2929 | dev_dbg(&dev->interface->dev, "%s\n", __func__); |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 2930 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 2931 | skb = pn533_alloc_skb(dev, sizeof(u8)); |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 2932 | if (!skb) |
| 2933 | return -ENOMEM; |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 2934 | |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 2935 | *skb_put(skb, sizeof(u8)) = 0x1; |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 2936 | |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 2937 | resp = pn533_send_cmd_sync(dev, 0x18, skb); |
| 2938 | if (IS_ERR(resp)) |
| 2939 | return PTR_ERR(resp); |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 2940 | |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 2941 | dev_kfree_skb(resp); |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 2942 | |
Waldemar Rymarkiewicz | 94c5c15 | 2012-11-26 14:18:36 +0100 | [diff] [blame] | 2943 | return 0; |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 2944 | } |
| 2945 | |
Waldemar Rymarkiewicz | 53cf483 | 2013-04-03 08:02:13 +0200 | [diff] [blame] | 2946 | struct pn533_acr122_poweron_rdr_arg { |
| 2947 | int rc; |
| 2948 | struct completion done; |
| 2949 | }; |
| 2950 | |
| 2951 | static void pn533_acr122_poweron_rdr_resp(struct urb *urb) |
| 2952 | { |
| 2953 | struct pn533_acr122_poweron_rdr_arg *arg = urb->context; |
| 2954 | |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 2955 | dev_dbg(&urb->dev->dev, "%s\n", __func__); |
Waldemar Rymarkiewicz | 53cf483 | 2013-04-03 08:02:13 +0200 | [diff] [blame] | 2956 | |
Olivier Guiter | 86eca4e | 2013-06-03 12:02:29 +0200 | [diff] [blame] | 2957 | print_hex_dump_debug("ACR122 RX: ", DUMP_PREFIX_NONE, 16, 1, |
Waldemar Rymarkiewicz | 53cf483 | 2013-04-03 08:02:13 +0200 | [diff] [blame] | 2958 | urb->transfer_buffer, urb->transfer_buffer_length, |
| 2959 | false); |
| 2960 | |
| 2961 | arg->rc = urb->status; |
| 2962 | complete(&arg->done); |
| 2963 | } |
| 2964 | |
| 2965 | static int pn533_acr122_poweron_rdr(struct pn533 *dev) |
| 2966 | { |
| 2967 | /* Power on th reader (CCID cmd) */ |
| 2968 | u8 cmd[10] = {PN533_ACR122_PC_TO_RDR_ICCPOWERON, |
| 2969 | 0, 0, 0, 0, 0, 0, 3, 0, 0}; |
| 2970 | u8 buf[255]; |
| 2971 | int rc; |
| 2972 | void *cntx; |
| 2973 | struct pn533_acr122_poweron_rdr_arg arg; |
| 2974 | |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 2975 | dev_dbg(&dev->interface->dev, "%s\n", __func__); |
Waldemar Rymarkiewicz | 53cf483 | 2013-04-03 08:02:13 +0200 | [diff] [blame] | 2976 | |
| 2977 | init_completion(&arg.done); |
| 2978 | cntx = dev->in_urb->context; /* backup context */ |
| 2979 | |
| 2980 | dev->in_urb->transfer_buffer = buf; |
| 2981 | dev->in_urb->transfer_buffer_length = 255; |
| 2982 | dev->in_urb->complete = pn533_acr122_poweron_rdr_resp; |
| 2983 | dev->in_urb->context = &arg; |
| 2984 | |
| 2985 | dev->out_urb->transfer_buffer = cmd; |
| 2986 | dev->out_urb->transfer_buffer_length = sizeof(cmd); |
| 2987 | |
Olivier Guiter | 86eca4e | 2013-06-03 12:02:29 +0200 | [diff] [blame] | 2988 | print_hex_dump_debug("ACR122 TX: ", DUMP_PREFIX_NONE, 16, 1, |
Waldemar Rymarkiewicz | 53cf483 | 2013-04-03 08:02:13 +0200 | [diff] [blame] | 2989 | cmd, sizeof(cmd), false); |
| 2990 | |
| 2991 | rc = usb_submit_urb(dev->out_urb, GFP_KERNEL); |
| 2992 | if (rc) { |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 2993 | nfc_err(&dev->interface->dev, |
| 2994 | "Reader power on cmd error %d\n", rc); |
Waldemar Rymarkiewicz | 53cf483 | 2013-04-03 08:02:13 +0200 | [diff] [blame] | 2995 | return rc; |
| 2996 | } |
| 2997 | |
| 2998 | rc = usb_submit_urb(dev->in_urb, GFP_KERNEL); |
| 2999 | if (rc) { |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 3000 | nfc_err(&dev->interface->dev, |
| 3001 | "Can't submit reader poweron cmd response %d\n", rc); |
Waldemar Rymarkiewicz | 53cf483 | 2013-04-03 08:02:13 +0200 | [diff] [blame] | 3002 | return rc; |
| 3003 | } |
| 3004 | |
| 3005 | wait_for_completion(&arg.done); |
| 3006 | dev->in_urb->context = cntx; /* restore context */ |
| 3007 | |
| 3008 | return arg.rc; |
| 3009 | } |
| 3010 | |
Samuel Ortiz | 60d9edd5 | 2013-04-11 11:45:41 +0200 | [diff] [blame] | 3011 | static int pn533_rf_field(struct nfc_dev *nfc_dev, u8 rf) |
| 3012 | { |
| 3013 | struct pn533 *dev = nfc_get_drvdata(nfc_dev); |
| 3014 | u8 rf_field = !!rf; |
| 3015 | int rc; |
| 3016 | |
Samuel Ortiz | 3a8eab3 | 2013-07-01 17:26:58 +0200 | [diff] [blame] | 3017 | rf_field |= PN533_CFGITEM_RF_FIELD_AUTO_RFCA; |
| 3018 | |
Samuel Ortiz | 60d9edd5 | 2013-04-11 11:45:41 +0200 | [diff] [blame] | 3019 | rc = pn533_set_configuration(dev, PN533_CFGITEM_RF_FIELD, |
| 3020 | (u8 *)&rf_field, 1); |
| 3021 | if (rc) { |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 3022 | nfc_err(&dev->interface->dev, "Error on setting RF field\n"); |
Samuel Ortiz | 60d9edd5 | 2013-04-11 11:45:41 +0200 | [diff] [blame] | 3023 | return rc; |
| 3024 | } |
| 3025 | |
| 3026 | return rc; |
| 3027 | } |
| 3028 | |
Sachin Kamat | e44666b | 2013-09-20 14:30:41 +0530 | [diff] [blame] | 3029 | static int pn533_dev_up(struct nfc_dev *nfc_dev) |
Samuel Ortiz | 60d9edd5 | 2013-04-11 11:45:41 +0200 | [diff] [blame] | 3030 | { |
| 3031 | return pn533_rf_field(nfc_dev, 1); |
| 3032 | } |
| 3033 | |
Sachin Kamat | e44666b | 2013-09-20 14:30:41 +0530 | [diff] [blame] | 3034 | static int pn533_dev_down(struct nfc_dev *nfc_dev) |
Samuel Ortiz | 60d9edd5 | 2013-04-11 11:45:41 +0200 | [diff] [blame] | 3035 | { |
| 3036 | return pn533_rf_field(nfc_dev, 0); |
| 3037 | } |
| 3038 | |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 3039 | static struct nfc_ops pn533_nfc_ops = { |
Samuel Ortiz | 60d9edd5 | 2013-04-11 11:45:41 +0200 | [diff] [blame] | 3040 | .dev_up = pn533_dev_up, |
| 3041 | .dev_down = pn533_dev_down, |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 3042 | .dep_link_up = pn533_dep_link_up, |
| 3043 | .dep_link_down = pn533_dep_link_down, |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 3044 | .start_poll = pn533_start_poll, |
| 3045 | .stop_poll = pn533_stop_poll, |
| 3046 | .activate_target = pn533_activate_target, |
| 3047 | .deactivate_target = pn533_deactivate_target, |
Samuel Ortiz | be9ae4c | 2012-05-16 15:55:48 +0200 | [diff] [blame] | 3048 | .im_transceive = pn533_transceive, |
Samuel Ortiz | dadb06f | 2012-05-31 00:09:11 +0200 | [diff] [blame] | 3049 | .tm_send = pn533_tm_send, |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 3050 | }; |
| 3051 | |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 3052 | static int pn533_setup(struct pn533 *dev) |
| 3053 | { |
| 3054 | struct pn533_config_max_retries max_retries; |
| 3055 | struct pn533_config_timing timing; |
| 3056 | u8 pasori_cfg[3] = {0x08, 0x01, 0x08}; |
| 3057 | int rc; |
| 3058 | |
| 3059 | switch (dev->device_type) { |
| 3060 | case PN533_DEVICE_STD: |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 3061 | case PN533_DEVICE_PASORI: |
Waldemar Rymarkiewicz | 53cf483 | 2013-04-03 08:02:13 +0200 | [diff] [blame] | 3062 | case PN533_DEVICE_ACR122U: |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 3063 | max_retries.mx_rty_atr = 0x2; |
| 3064 | max_retries.mx_rty_psl = 0x1; |
| 3065 | max_retries.mx_rty_passive_act = |
| 3066 | PN533_CONFIG_MAX_RETRIES_NO_RETRY; |
| 3067 | |
| 3068 | timing.rfu = PN533_CONFIG_TIMING_102; |
| 3069 | timing.atr_res_timeout = PN533_CONFIG_TIMING_102; |
| 3070 | timing.dep_timeout = PN533_CONFIG_TIMING_204; |
| 3071 | |
| 3072 | break; |
| 3073 | |
| 3074 | default: |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 3075 | nfc_err(&dev->interface->dev, "Unknown device type %d\n", |
| 3076 | dev->device_type); |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 3077 | return -EINVAL; |
| 3078 | } |
| 3079 | |
| 3080 | rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES, |
| 3081 | (u8 *)&max_retries, sizeof(max_retries)); |
| 3082 | if (rc) { |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 3083 | nfc_err(&dev->interface->dev, |
| 3084 | "Error on setting MAX_RETRIES config\n"); |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 3085 | return rc; |
| 3086 | } |
| 3087 | |
| 3088 | |
| 3089 | rc = pn533_set_configuration(dev, PN533_CFGITEM_TIMING, |
| 3090 | (u8 *)&timing, sizeof(timing)); |
| 3091 | if (rc) { |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 3092 | nfc_err(&dev->interface->dev, "Error on setting RF timings\n"); |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 3093 | return rc; |
| 3094 | } |
| 3095 | |
| 3096 | switch (dev->device_type) { |
| 3097 | case PN533_DEVICE_STD: |
| 3098 | break; |
| 3099 | |
| 3100 | case PN533_DEVICE_PASORI: |
Waldemar Rymarkiewicz | f75c291 | 2013-04-03 08:02:02 +0200 | [diff] [blame] | 3101 | pn533_pasori_fw_reset(dev); |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 3102 | |
| 3103 | rc = pn533_set_configuration(dev, PN533_CFGITEM_PASORI, |
| 3104 | pasori_cfg, 3); |
| 3105 | if (rc) { |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 3106 | nfc_err(&dev->interface->dev, |
| 3107 | "Error while settings PASORI config\n"); |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 3108 | return rc; |
| 3109 | } |
| 3110 | |
Waldemar Rymarkiewicz | f75c291 | 2013-04-03 08:02:02 +0200 | [diff] [blame] | 3111 | pn533_pasori_fw_reset(dev); |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 3112 | |
| 3113 | break; |
| 3114 | } |
| 3115 | |
| 3116 | return 0; |
| 3117 | } |
| 3118 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 3119 | static int pn533_probe(struct usb_interface *interface, |
| 3120 | const struct usb_device_id *id) |
| 3121 | { |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 3122 | struct pn533_fw_version fw_ver; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 3123 | struct pn533 *dev; |
| 3124 | struct usb_host_interface *iface_desc; |
| 3125 | struct usb_endpoint_descriptor *endpoint; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 3126 | int in_endpoint = 0; |
| 3127 | int out_endpoint = 0; |
| 3128 | int rc = -ENOMEM; |
| 3129 | int i; |
| 3130 | u32 protocols; |
| 3131 | |
| 3132 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
| 3133 | if (!dev) |
| 3134 | return -ENOMEM; |
| 3135 | |
| 3136 | dev->udev = usb_get_dev(interface_to_usbdev(interface)); |
| 3137 | dev->interface = interface; |
Samuel Ortiz | 0201ed0 | 2012-05-31 17:56:46 +0200 | [diff] [blame] | 3138 | mutex_init(&dev->cmd_lock); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 3139 | |
| 3140 | iface_desc = interface->cur_altsetting; |
| 3141 | for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) { |
| 3142 | endpoint = &iface_desc->endpoint[i].desc; |
| 3143 | |
Waldemar Rymarkiewicz | 8d25ca7 | 2012-11-26 14:18:30 +0100 | [diff] [blame] | 3144 | if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint)) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 3145 | in_endpoint = endpoint->bEndpointAddress; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 3146 | |
Waldemar Rymarkiewicz | 8d25ca7 | 2012-11-26 14:18:30 +0100 | [diff] [blame] | 3147 | if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint)) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 3148 | out_endpoint = endpoint->bEndpointAddress; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 3149 | } |
| 3150 | |
| 3151 | if (!in_endpoint || !out_endpoint) { |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 3152 | nfc_err(&interface->dev, |
| 3153 | "Could not find bulk-in or bulk-out endpoint\n"); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 3154 | rc = -ENODEV; |
| 3155 | goto error; |
| 3156 | } |
| 3157 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 3158 | dev->in_urb = usb_alloc_urb(0, GFP_KERNEL); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 3159 | dev->out_urb = usb_alloc_urb(0, GFP_KERNEL); |
| 3160 | |
Waldemar Rymarkiewicz | a579809 | 2012-12-10 14:42:58 +0100 | [diff] [blame] | 3161 | if (!dev->in_urb || !dev->out_urb) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 3162 | goto error; |
| 3163 | |
| 3164 | usb_fill_bulk_urb(dev->in_urb, dev->udev, |
Waldemar Rymarkiewicz | 5d46774 | 2012-12-10 14:42:51 +0100 | [diff] [blame] | 3165 | usb_rcvbulkpipe(dev->udev, in_endpoint), |
| 3166 | NULL, 0, NULL, dev); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 3167 | usb_fill_bulk_urb(dev->out_urb, dev->udev, |
Waldemar Rymarkiewicz | 5d46774 | 2012-12-10 14:42:51 +0100 | [diff] [blame] | 3168 | usb_sndbulkpipe(dev->udev, out_endpoint), |
| 3169 | NULL, 0, pn533_send_complete, dev); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 3170 | |
Samuel Ortiz | 5d50b36 | 2012-08-17 23:47:54 +0200 | [diff] [blame] | 3171 | INIT_WORK(&dev->cmd_work, pn533_wq_cmd); |
| 3172 | INIT_WORK(&dev->cmd_complete_work, pn533_wq_cmd_complete); |
Olivier Guiter | 963a82e | 2013-06-13 13:43:28 +0000 | [diff] [blame] | 3173 | INIT_WORK(&dev->mi_rx_work, pn533_wq_mi_recv); |
| 3174 | INIT_WORK(&dev->mi_tx_work, pn533_wq_mi_send); |
Samuel Ortiz | 103b34c | 2012-05-31 00:07:51 +0200 | [diff] [blame] | 3175 | INIT_WORK(&dev->tg_work, pn533_wq_tg_get_data); |
Olivier Guiter | 3c13b24 | 2013-09-23 12:24:38 +0200 | [diff] [blame] | 3176 | INIT_WORK(&dev->mi_tm_rx_work, pn533_wq_tm_mi_recv); |
Olivier Guiter | 93ad420 | 2013-09-23 12:24:39 +0200 | [diff] [blame] | 3177 | INIT_WORK(&dev->mi_tm_tx_work, pn533_wq_tm_mi_send); |
Samuel Ortiz | 46f793b | 2013-07-03 14:50:36 +0200 | [diff] [blame] | 3178 | INIT_DELAYED_WORK(&dev->poll_work, pn533_wq_poll); |
Samuel Ortiz | 17e9d9d | 2013-06-26 18:16:21 +0200 | [diff] [blame] | 3179 | INIT_WORK(&dev->rf_work, pn533_wq_rf); |
Tejun Heo | 58637c9 | 2012-08-22 16:28:46 -0700 | [diff] [blame] | 3180 | dev->wq = alloc_ordered_workqueue("pn533", 0); |
Samuel Ortiz | 4849f85 | 2012-04-10 19:43:17 +0200 | [diff] [blame] | 3181 | if (dev->wq == NULL) |
| 3182 | goto error; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 3183 | |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 3184 | init_timer(&dev->listen_timer); |
| 3185 | dev->listen_timer.data = (unsigned long) dev; |
| 3186 | dev->listen_timer.function = pn533_listen_mode_timer; |
| 3187 | |
Samuel Ortiz | 6ff73fd | 2012-04-10 19:43:18 +0200 | [diff] [blame] | 3188 | skb_queue_head_init(&dev->resp_q); |
Olivier Guiter | 963a82e | 2013-06-13 13:43:28 +0000 | [diff] [blame] | 3189 | skb_queue_head_init(&dev->fragment_skb); |
Samuel Ortiz | 6ff73fd | 2012-04-10 19:43:18 +0200 | [diff] [blame] | 3190 | |
Samuel Ortiz | 5d50b36 | 2012-08-17 23:47:54 +0200 | [diff] [blame] | 3191 | INIT_LIST_HEAD(&dev->cmd_queue); |
| 3192 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 3193 | usb_set_intfdata(interface, dev); |
| 3194 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 3195 | dev->ops = &pn533_std_frame_ops; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 3196 | |
Waldemar Rymarkiewicz | 5852037 | 2013-04-03 08:02:12 +0200 | [diff] [blame] | 3197 | dev->protocol_type = PN533_PROTO_REQ_ACK_RESP; |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 3198 | dev->device_type = id->driver_info; |
| 3199 | switch (dev->device_type) { |
| 3200 | case PN533_DEVICE_STD: |
| 3201 | protocols = PN533_ALL_PROTOCOLS; |
| 3202 | break; |
| 3203 | |
| 3204 | case PN533_DEVICE_PASORI: |
| 3205 | protocols = PN533_NO_TYPE_B_PROTOCOLS; |
| 3206 | break; |
| 3207 | |
Waldemar Rymarkiewicz | 53cf483 | 2013-04-03 08:02:13 +0200 | [diff] [blame] | 3208 | case PN533_DEVICE_ACR122U: |
| 3209 | protocols = PN533_NO_TYPE_B_PROTOCOLS; |
| 3210 | dev->ops = &pn533_acr122_frame_ops; |
| 3211 | dev->protocol_type = PN533_PROTO_REQ_RESP, |
| 3212 | |
| 3213 | rc = pn533_acr122_poweron_rdr(dev); |
| 3214 | if (rc < 0) { |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 3215 | nfc_err(&dev->interface->dev, |
| 3216 | "Couldn't poweron the reader (error %d)\n", rc); |
Waldemar Rymarkiewicz | 53cf483 | 2013-04-03 08:02:13 +0200 | [diff] [blame] | 3217 | goto destroy_wq; |
| 3218 | } |
| 3219 | break; |
| 3220 | |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 3221 | default: |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 3222 | nfc_err(&dev->interface->dev, "Unknown device type %d\n", |
| 3223 | dev->device_type); |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 3224 | rc = -EINVAL; |
| 3225 | goto destroy_wq; |
| 3226 | } |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 3227 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 3228 | memset(&fw_ver, 0, sizeof(fw_ver)); |
| 3229 | rc = pn533_get_firmware_version(dev, &fw_ver); |
| 3230 | if (rc < 0) |
| 3231 | goto destroy_wq; |
| 3232 | |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 3233 | nfc_info(&dev->interface->dev, |
| 3234 | "NXP PN5%02X firmware ver %d.%d now attached\n", |
| 3235 | fw_ver.ic, fw_ver.ver, fw_ver.rev); |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 3236 | |
| 3237 | |
Samuel Ortiz | e875304 | 2011-08-19 15:47:11 +0200 | [diff] [blame] | 3238 | dev->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols, |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 3239 | dev->ops->tx_header_len + |
Samuel Ortiz | e875304 | 2011-08-19 15:47:11 +0200 | [diff] [blame] | 3240 | PN533_CMD_DATAEXCH_HEAD_LEN, |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 3241 | dev->ops->tx_tail_len); |
Wei Yongjun | 4674d0f | 2013-05-07 20:07:52 +0800 | [diff] [blame] | 3242 | if (!dev->nfc_dev) { |
| 3243 | rc = -ENOMEM; |
Samuel Ortiz | 4849f85 | 2012-04-10 19:43:17 +0200 | [diff] [blame] | 3244 | goto destroy_wq; |
Wei Yongjun | 4674d0f | 2013-05-07 20:07:52 +0800 | [diff] [blame] | 3245 | } |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 3246 | |
| 3247 | nfc_set_parent_dev(dev->nfc_dev, &interface->dev); |
| 3248 | nfc_set_drvdata(dev->nfc_dev, dev); |
| 3249 | |
| 3250 | rc = nfc_register_device(dev->nfc_dev); |
| 3251 | if (rc) |
| 3252 | goto free_nfc_dev; |
| 3253 | |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 3254 | rc = pn533_setup(dev); |
| 3255 | if (rc) |
Samuel Ortiz | 9f2f8ba | 2012-05-29 21:28:58 +0200 | [diff] [blame] | 3256 | goto unregister_nfc_dev; |
Samuel Ortiz | 34a85bf | 2012-05-29 21:34:08 +0200 | [diff] [blame] | 3257 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 3258 | return 0; |
| 3259 | |
Samuel Ortiz | 9f2f8ba | 2012-05-29 21:28:58 +0200 | [diff] [blame] | 3260 | unregister_nfc_dev: |
| 3261 | nfc_unregister_device(dev->nfc_dev); |
| 3262 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 3263 | free_nfc_dev: |
| 3264 | nfc_free_device(dev->nfc_dev); |
Samuel Ortiz | 9f2f8ba | 2012-05-29 21:28:58 +0200 | [diff] [blame] | 3265 | |
Samuel Ortiz | 4849f85 | 2012-04-10 19:43:17 +0200 | [diff] [blame] | 3266 | destroy_wq: |
| 3267 | destroy_workqueue(dev->wq); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 3268 | error: |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 3269 | usb_free_urb(dev->in_urb); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 3270 | usb_free_urb(dev->out_urb); |
Marina Makienko | 7c5a54f | 2013-02-26 11:41:18 +0400 | [diff] [blame] | 3271 | usb_put_dev(dev->udev); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 3272 | kfree(dev); |
| 3273 | return rc; |
| 3274 | } |
| 3275 | |
| 3276 | static void pn533_disconnect(struct usb_interface *interface) |
| 3277 | { |
| 3278 | struct pn533 *dev; |
Samuel Ortiz | 5d50b36 | 2012-08-17 23:47:54 +0200 | [diff] [blame] | 3279 | struct pn533_cmd *cmd, *n; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 3280 | |
| 3281 | dev = usb_get_intfdata(interface); |
| 3282 | usb_set_intfdata(interface, NULL); |
| 3283 | |
| 3284 | nfc_unregister_device(dev->nfc_dev); |
| 3285 | nfc_free_device(dev->nfc_dev); |
| 3286 | |
| 3287 | usb_kill_urb(dev->in_urb); |
| 3288 | usb_kill_urb(dev->out_urb); |
| 3289 | |
Samuel Ortiz | 46f793b | 2013-07-03 14:50:36 +0200 | [diff] [blame] | 3290 | flush_delayed_work(&dev->poll_work); |
Samuel Ortiz | 4849f85 | 2012-04-10 19:43:17 +0200 | [diff] [blame] | 3291 | destroy_workqueue(dev->wq); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 3292 | |
Samuel Ortiz | 6ff73fd | 2012-04-10 19:43:18 +0200 | [diff] [blame] | 3293 | skb_queue_purge(&dev->resp_q); |
| 3294 | |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 3295 | del_timer(&dev->listen_timer); |
| 3296 | |
Samuel Ortiz | 5d50b36 | 2012-08-17 23:47:54 +0200 | [diff] [blame] | 3297 | list_for_each_entry_safe(cmd, n, &dev->cmd_queue, queue) { |
| 3298 | list_del(&cmd->queue); |
| 3299 | kfree(cmd); |
| 3300 | } |
| 3301 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 3302 | usb_free_urb(dev->in_urb); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 3303 | usb_free_urb(dev->out_urb); |
| 3304 | kfree(dev); |
| 3305 | |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 3306 | nfc_info(&interface->dev, "NXP PN533 NFC device disconnected\n"); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 3307 | } |
| 3308 | |
| 3309 | static struct usb_driver pn533_driver = { |
| 3310 | .name = "pn533", |
| 3311 | .probe = pn533_probe, |
| 3312 | .disconnect = pn533_disconnect, |
| 3313 | .id_table = pn533_table, |
| 3314 | }; |
| 3315 | |
Greg Kroah-Hartman | fe74848 | 2011-11-18 09:52:10 -0800 | [diff] [blame] | 3316 | module_usb_driver(pn533_driver); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 3317 | |
Waldemar Rymarkiewicz | e70b96e | 2013-04-03 08:02:01 +0200 | [diff] [blame] | 3318 | MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>"); |
| 3319 | MODULE_AUTHOR("Aloisio Almeida Jr <aloisio.almeida@openbossa.org>"); |
| 3320 | MODULE_AUTHOR("Waldemar Rymarkiewicz <waldemar.rymarkiewicz@tieto.com>"); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 3321 | MODULE_DESCRIPTION("PN533 usb driver ver " VERSION); |
| 3322 | MODULE_VERSION(VERSION); |
| 3323 | MODULE_LICENSE("GPL"); |