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