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