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