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