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