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