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