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