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