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