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