xref: /openbmc/linux/drivers/nfc/pn533/usb.c (revision 6d99a79c)
1 /*
2  * Driver for NXP PN533 NFC Chip - USB transport layer
3  *
4  * Copyright (C) 2011 Instituto Nokia de Tecnologia
5  * Copyright (C) 2012-2013 Tieto Poland
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include <linux/device.h>
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/slab.h>
25 #include <linux/usb.h>
26 #include <linux/nfc.h>
27 #include <linux/netdevice.h>
28 #include <net/nfc/nfc.h>
29 #include "pn533.h"
30 
31 #define VERSION "0.1"
32 
33 #define PN533_VENDOR_ID 0x4CC
34 #define PN533_PRODUCT_ID 0x2533
35 
36 #define SCM_VENDOR_ID 0x4E6
37 #define SCL3711_PRODUCT_ID 0x5591
38 
39 #define SONY_VENDOR_ID         0x054c
40 #define PASORI_PRODUCT_ID      0x02e1
41 
42 #define ACS_VENDOR_ID 0x072f
43 #define ACR122U_PRODUCT_ID 0x2200
44 
45 static const struct usb_device_id pn533_usb_table[] = {
46 	{ USB_DEVICE(PN533_VENDOR_ID, PN533_PRODUCT_ID),
47 	  .driver_info = PN533_DEVICE_STD },
48 	{ USB_DEVICE(SCM_VENDOR_ID, SCL3711_PRODUCT_ID),
49 	  .driver_info = PN533_DEVICE_STD },
50 	{ USB_DEVICE(SONY_VENDOR_ID, PASORI_PRODUCT_ID),
51 	  .driver_info = PN533_DEVICE_PASORI },
52 	{ USB_DEVICE(ACS_VENDOR_ID, ACR122U_PRODUCT_ID),
53 	  .driver_info = PN533_DEVICE_ACR122U },
54 	{ }
55 };
56 MODULE_DEVICE_TABLE(usb, pn533_usb_table);
57 
58 struct pn533_usb_phy {
59 	struct usb_device *udev;
60 	struct usb_interface *interface;
61 
62 	struct urb *out_urb;
63 	struct urb *in_urb;
64 
65 	struct urb *ack_urb;
66 	u8 *ack_buffer;
67 
68 	struct pn533 *priv;
69 };
70 
71 static void pn533_recv_response(struct urb *urb)
72 {
73 	struct pn533_usb_phy *phy = urb->context;
74 	struct sk_buff *skb = NULL;
75 
76 	if (!urb->status) {
77 		skb = alloc_skb(urb->actual_length, GFP_ATOMIC);
78 		if (!skb) {
79 			nfc_err(&phy->udev->dev, "failed to alloc memory\n");
80 		} else {
81 			skb_put_data(skb, urb->transfer_buffer,
82 				     urb->actual_length);
83 		}
84 	}
85 
86 	pn533_recv_frame(phy->priv, skb, urb->status);
87 }
88 
89 static int pn533_submit_urb_for_response(struct pn533_usb_phy *phy, gfp_t flags)
90 {
91 	phy->in_urb->complete = pn533_recv_response;
92 
93 	return usb_submit_urb(phy->in_urb, flags);
94 }
95 
96 static void pn533_recv_ack(struct urb *urb)
97 {
98 	struct pn533_usb_phy *phy = urb->context;
99 	struct pn533 *priv = phy->priv;
100 	struct pn533_cmd *cmd = priv->cmd;
101 	struct pn533_std_frame *in_frame;
102 	int rc;
103 
104 	cmd->status = urb->status;
105 
106 	switch (urb->status) {
107 	case 0:
108 		break; /* success */
109 	case -ECONNRESET:
110 	case -ENOENT:
111 		dev_dbg(&phy->udev->dev,
112 			"The urb has been stopped (status %d)\n",
113 			urb->status);
114 		goto sched_wq;
115 	case -ESHUTDOWN:
116 	default:
117 		nfc_err(&phy->udev->dev,
118 			"Urb failure (status %d)\n", urb->status);
119 		goto sched_wq;
120 	}
121 
122 	in_frame = phy->in_urb->transfer_buffer;
123 
124 	if (!pn533_rx_frame_is_ack(in_frame)) {
125 		nfc_err(&phy->udev->dev, "Received an invalid ack\n");
126 		cmd->status = -EIO;
127 		goto sched_wq;
128 	}
129 
130 	rc = pn533_submit_urb_for_response(phy, GFP_ATOMIC);
131 	if (rc) {
132 		nfc_err(&phy->udev->dev,
133 			"usb_submit_urb failed with result %d\n", rc);
134 		cmd->status = rc;
135 		goto sched_wq;
136 	}
137 
138 	return;
139 
140 sched_wq:
141 	queue_work(priv->wq, &priv->cmd_complete_work);
142 }
143 
144 static int pn533_submit_urb_for_ack(struct pn533_usb_phy *phy, gfp_t flags)
145 {
146 	phy->in_urb->complete = pn533_recv_ack;
147 
148 	return usb_submit_urb(phy->in_urb, flags);
149 }
150 
151 static int pn533_usb_send_ack(struct pn533 *dev, gfp_t flags)
152 {
153 	struct pn533_usb_phy *phy = dev->phy;
154 	static const u8 ack[6] = {0x00, 0x00, 0xff, 0x00, 0xff, 0x00};
155 	/* spec 7.1.1.3:  Preamble, SoPC (2), ACK Code (2), Postamble */
156 
157 	if (!phy->ack_buffer) {
158 		phy->ack_buffer = kmemdup(ack, sizeof(ack), flags);
159 		if (!phy->ack_buffer)
160 			return -ENOMEM;
161 	}
162 
163 	phy->ack_urb->transfer_buffer = phy->ack_buffer;
164 	phy->ack_urb->transfer_buffer_length = sizeof(ack);
165 	return usb_submit_urb(phy->ack_urb, flags);
166 }
167 
168 static int pn533_usb_send_frame(struct pn533 *dev,
169 				struct sk_buff *out)
170 {
171 	struct pn533_usb_phy *phy = dev->phy;
172 	int rc;
173 
174 	if (phy->priv == NULL)
175 		phy->priv = dev;
176 
177 	phy->out_urb->transfer_buffer = out->data;
178 	phy->out_urb->transfer_buffer_length = out->len;
179 
180 	print_hex_dump_debug("PN533 TX: ", DUMP_PREFIX_NONE, 16, 1,
181 			     out->data, out->len, false);
182 
183 	rc = usb_submit_urb(phy->out_urb, GFP_KERNEL);
184 	if (rc)
185 		return rc;
186 
187 	if (dev->protocol_type == PN533_PROTO_REQ_RESP) {
188 		/* request for response for sent packet directly */
189 		rc = pn533_submit_urb_for_response(phy, GFP_KERNEL);
190 		if (rc)
191 			goto error;
192 	} else if (dev->protocol_type == PN533_PROTO_REQ_ACK_RESP) {
193 		/* request for ACK if that's the case */
194 		rc = pn533_submit_urb_for_ack(phy, GFP_KERNEL);
195 		if (rc)
196 			goto error;
197 	}
198 
199 	return 0;
200 
201 error:
202 	usb_unlink_urb(phy->out_urb);
203 	return rc;
204 }
205 
206 static void pn533_usb_abort_cmd(struct pn533 *dev, gfp_t flags)
207 {
208 	struct pn533_usb_phy *phy = dev->phy;
209 
210 	/* ACR122U does not support any command which aborts last
211 	 * issued command i.e. as ACK for standard PN533. Additionally,
212 	 * it behaves stange, sending broken or incorrect responses,
213 	 * when we cancel urb before the chip will send response.
214 	 */
215 	if (dev->device_type == PN533_DEVICE_ACR122U)
216 		return;
217 
218 	/* An ack will cancel the last issued command */
219 	pn533_usb_send_ack(dev, flags);
220 
221 	/* cancel the urb request */
222 	usb_kill_urb(phy->in_urb);
223 }
224 
225 /* ACR122 specific structs and fucntions */
226 
227 /* ACS ACR122 pn533 frame definitions */
228 #define PN533_ACR122_TX_FRAME_HEADER_LEN (sizeof(struct pn533_acr122_tx_frame) \
229 					  + 2)
230 #define PN533_ACR122_TX_FRAME_TAIL_LEN 0
231 #define PN533_ACR122_RX_FRAME_HEADER_LEN (sizeof(struct pn533_acr122_rx_frame) \
232 					  + 2)
233 #define PN533_ACR122_RX_FRAME_TAIL_LEN 2
234 #define PN533_ACR122_FRAME_MAX_PAYLOAD_LEN PN533_STD_FRAME_MAX_PAYLOAD_LEN
235 
236 /* CCID messages types */
237 #define PN533_ACR122_PC_TO_RDR_ICCPOWERON 0x62
238 #define PN533_ACR122_PC_TO_RDR_ESCAPE 0x6B
239 
240 #define PN533_ACR122_RDR_TO_PC_ESCAPE 0x83
241 
242 
243 struct pn533_acr122_ccid_hdr {
244 	u8 type;
245 	u32 datalen;
246 	u8 slot;
247 	u8 seq;
248 
249 	/*
250 	 * 3 msg specific bytes or status, error and 1 specific
251 	 * byte for reposnse msg
252 	 */
253 	u8 params[3];
254 	u8 data[]; /* payload */
255 } __packed;
256 
257 struct pn533_acr122_apdu_hdr {
258 	u8 class;
259 	u8 ins;
260 	u8 p1;
261 	u8 p2;
262 } __packed;
263 
264 struct pn533_acr122_tx_frame {
265 	struct pn533_acr122_ccid_hdr ccid;
266 	struct pn533_acr122_apdu_hdr apdu;
267 	u8 datalen;
268 	u8 data[]; /* pn533 frame: TFI ... */
269 } __packed;
270 
271 struct pn533_acr122_rx_frame {
272 	struct pn533_acr122_ccid_hdr ccid;
273 	u8 data[]; /* pn533 frame : TFI ... */
274 } __packed;
275 
276 static void pn533_acr122_tx_frame_init(void *_frame, u8 cmd_code)
277 {
278 	struct pn533_acr122_tx_frame *frame = _frame;
279 
280 	frame->ccid.type = PN533_ACR122_PC_TO_RDR_ESCAPE;
281 	/* sizeof(apdu_hdr) + sizeof(datalen) */
282 	frame->ccid.datalen = sizeof(frame->apdu) + 1;
283 	frame->ccid.slot = 0;
284 	frame->ccid.seq = 0;
285 	frame->ccid.params[0] = 0;
286 	frame->ccid.params[1] = 0;
287 	frame->ccid.params[2] = 0;
288 
289 	frame->data[0] = PN533_STD_FRAME_DIR_OUT;
290 	frame->data[1] = cmd_code;
291 	frame->datalen = 2;  /* data[0] + data[1] */
292 
293 	frame->apdu.class = 0xFF;
294 	frame->apdu.ins = 0;
295 	frame->apdu.p1 = 0;
296 	frame->apdu.p2 = 0;
297 }
298 
299 static void pn533_acr122_tx_frame_finish(void *_frame)
300 {
301 	struct pn533_acr122_tx_frame *frame = _frame;
302 
303 	frame->ccid.datalen += frame->datalen;
304 }
305 
306 static void pn533_acr122_tx_update_payload_len(void *_frame, int len)
307 {
308 	struct pn533_acr122_tx_frame *frame = _frame;
309 
310 	frame->datalen += len;
311 }
312 
313 static bool pn533_acr122_is_rx_frame_valid(void *_frame, struct pn533 *dev)
314 {
315 	struct pn533_acr122_rx_frame *frame = _frame;
316 
317 	if (frame->ccid.type != 0x83)
318 		return false;
319 
320 	if (!frame->ccid.datalen)
321 		return false;
322 
323 	if (frame->data[frame->ccid.datalen - 2] == 0x63)
324 		return false;
325 
326 	return true;
327 }
328 
329 static int pn533_acr122_rx_frame_size(void *frame)
330 {
331 	struct pn533_acr122_rx_frame *f = frame;
332 
333 	/* f->ccid.datalen already includes tail length */
334 	return sizeof(struct pn533_acr122_rx_frame) + f->ccid.datalen;
335 }
336 
337 static u8 pn533_acr122_get_cmd_code(void *frame)
338 {
339 	struct pn533_acr122_rx_frame *f = frame;
340 
341 	return PN533_FRAME_CMD(f);
342 }
343 
344 static struct pn533_frame_ops pn533_acr122_frame_ops = {
345 	.tx_frame_init = pn533_acr122_tx_frame_init,
346 	.tx_frame_finish = pn533_acr122_tx_frame_finish,
347 	.tx_update_payload_len = pn533_acr122_tx_update_payload_len,
348 	.tx_header_len = PN533_ACR122_TX_FRAME_HEADER_LEN,
349 	.tx_tail_len = PN533_ACR122_TX_FRAME_TAIL_LEN,
350 
351 	.rx_is_frame_valid = pn533_acr122_is_rx_frame_valid,
352 	.rx_header_len = PN533_ACR122_RX_FRAME_HEADER_LEN,
353 	.rx_tail_len = PN533_ACR122_RX_FRAME_TAIL_LEN,
354 	.rx_frame_size = pn533_acr122_rx_frame_size,
355 
356 	.max_payload_len = PN533_ACR122_FRAME_MAX_PAYLOAD_LEN,
357 	.get_cmd_code = pn533_acr122_get_cmd_code,
358 };
359 
360 struct pn533_acr122_poweron_rdr_arg {
361 	int rc;
362 	struct completion done;
363 };
364 
365 static void pn533_acr122_poweron_rdr_resp(struct urb *urb)
366 {
367 	struct pn533_acr122_poweron_rdr_arg *arg = urb->context;
368 
369 	dev_dbg(&urb->dev->dev, "%s\n", __func__);
370 
371 	print_hex_dump_debug("ACR122 RX: ", DUMP_PREFIX_NONE, 16, 1,
372 		       urb->transfer_buffer, urb->transfer_buffer_length,
373 		       false);
374 
375 	arg->rc = urb->status;
376 	complete(&arg->done);
377 }
378 
379 static int pn533_acr122_poweron_rdr(struct pn533_usb_phy *phy)
380 {
381 	/* Power on th reader (CCID cmd) */
382 	u8 cmd[10] = {PN533_ACR122_PC_TO_RDR_ICCPOWERON,
383 		      0, 0, 0, 0, 0, 0, 3, 0, 0};
384 	char *buffer;
385 	int transferred;
386 	int rc;
387 	void *cntx;
388 	struct pn533_acr122_poweron_rdr_arg arg;
389 
390 	dev_dbg(&phy->udev->dev, "%s\n", __func__);
391 
392 	buffer = kmemdup(cmd, sizeof(cmd), GFP_KERNEL);
393 	if (!buffer)
394 		return -ENOMEM;
395 
396 	init_completion(&arg.done);
397 	cntx = phy->in_urb->context;  /* backup context */
398 
399 	phy->in_urb->complete = pn533_acr122_poweron_rdr_resp;
400 	phy->in_urb->context = &arg;
401 
402 	print_hex_dump_debug("ACR122 TX: ", DUMP_PREFIX_NONE, 16, 1,
403 		       cmd, sizeof(cmd), false);
404 
405 	rc = usb_bulk_msg(phy->udev, phy->out_urb->pipe, buffer, sizeof(cmd),
406 			  &transferred, 0);
407 	kfree(buffer);
408 	if (rc || (transferred != sizeof(cmd))) {
409 		nfc_err(&phy->udev->dev,
410 			"Reader power on cmd error %d\n", rc);
411 		return rc;
412 	}
413 
414 	rc =  usb_submit_urb(phy->in_urb, GFP_KERNEL);
415 	if (rc) {
416 		nfc_err(&phy->udev->dev,
417 			"Can't submit reader poweron cmd response %d\n", rc);
418 		return rc;
419 	}
420 
421 	wait_for_completion(&arg.done);
422 	phy->in_urb->context = cntx; /* restore context */
423 
424 	return arg.rc;
425 }
426 
427 static void pn533_send_complete(struct urb *urb)
428 {
429 	struct pn533_usb_phy *phy = urb->context;
430 
431 	switch (urb->status) {
432 	case 0:
433 		break; /* success */
434 	case -ECONNRESET:
435 	case -ENOENT:
436 		dev_dbg(&phy->udev->dev,
437 			"The urb has been stopped (status %d)\n",
438 			urb->status);
439 		break;
440 	case -ESHUTDOWN:
441 	default:
442 		nfc_err(&phy->udev->dev,
443 			"Urb failure (status %d)\n",
444 			urb->status);
445 	}
446 }
447 
448 static struct pn533_phy_ops usb_phy_ops = {
449 	.send_frame = pn533_usb_send_frame,
450 	.send_ack = pn533_usb_send_ack,
451 	.abort_cmd = pn533_usb_abort_cmd,
452 };
453 
454 static int pn533_usb_probe(struct usb_interface *interface,
455 			const struct usb_device_id *id)
456 {
457 	struct pn533 *priv;
458 	struct pn533_usb_phy *phy;
459 	struct usb_host_interface *iface_desc;
460 	struct usb_endpoint_descriptor *endpoint;
461 	int in_endpoint = 0;
462 	int out_endpoint = 0;
463 	int rc = -ENOMEM;
464 	int i;
465 	u32 protocols;
466 	enum pn533_protocol_type protocol_type = PN533_PROTO_REQ_ACK_RESP;
467 	struct pn533_frame_ops *fops = NULL;
468 	unsigned char *in_buf;
469 	int in_buf_len = PN533_EXT_FRAME_HEADER_LEN +
470 			 PN533_STD_FRAME_MAX_PAYLOAD_LEN +
471 			 PN533_STD_FRAME_TAIL_LEN;
472 
473 	phy = devm_kzalloc(&interface->dev, sizeof(*phy), GFP_KERNEL);
474 	if (!phy)
475 		return -ENOMEM;
476 
477 	in_buf = kzalloc(in_buf_len, GFP_KERNEL);
478 	if (!in_buf)
479 		return -ENOMEM;
480 
481 	phy->udev = usb_get_dev(interface_to_usbdev(interface));
482 	phy->interface = interface;
483 
484 	iface_desc = interface->cur_altsetting;
485 	for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
486 		endpoint = &iface_desc->endpoint[i].desc;
487 
488 		if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint))
489 			in_endpoint = endpoint->bEndpointAddress;
490 
491 		if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint))
492 			out_endpoint = endpoint->bEndpointAddress;
493 	}
494 
495 	if (!in_endpoint || !out_endpoint) {
496 		nfc_err(&interface->dev,
497 			"Could not find bulk-in or bulk-out endpoint\n");
498 		rc = -ENODEV;
499 		goto error;
500 	}
501 
502 	phy->in_urb = usb_alloc_urb(0, GFP_KERNEL);
503 	phy->out_urb = usb_alloc_urb(0, GFP_KERNEL);
504 	phy->ack_urb = usb_alloc_urb(0, GFP_KERNEL);
505 
506 	if (!phy->in_urb || !phy->out_urb || !phy->ack_urb)
507 		goto error;
508 
509 	usb_fill_bulk_urb(phy->in_urb, phy->udev,
510 			  usb_rcvbulkpipe(phy->udev, in_endpoint),
511 			  in_buf, in_buf_len, NULL, phy);
512 
513 	usb_fill_bulk_urb(phy->out_urb, phy->udev,
514 			  usb_sndbulkpipe(phy->udev, out_endpoint),
515 			  NULL, 0, pn533_send_complete, phy);
516 	usb_fill_bulk_urb(phy->ack_urb, phy->udev,
517 			  usb_sndbulkpipe(phy->udev, out_endpoint),
518 			  NULL, 0, pn533_send_complete, phy);
519 
520 	switch (id->driver_info) {
521 	case PN533_DEVICE_STD:
522 		protocols = PN533_ALL_PROTOCOLS;
523 		break;
524 
525 	case PN533_DEVICE_PASORI:
526 		protocols = PN533_NO_TYPE_B_PROTOCOLS;
527 		break;
528 
529 	case PN533_DEVICE_ACR122U:
530 		protocols = PN533_NO_TYPE_B_PROTOCOLS;
531 		fops = &pn533_acr122_frame_ops;
532 		protocol_type = PN533_PROTO_REQ_RESP,
533 
534 		rc = pn533_acr122_poweron_rdr(phy);
535 		if (rc < 0) {
536 			nfc_err(&interface->dev,
537 				"Couldn't poweron the reader (error %d)\n", rc);
538 			goto error;
539 		}
540 		break;
541 
542 	default:
543 		nfc_err(&interface->dev, "Unknown device type %lu\n",
544 			id->driver_info);
545 		rc = -EINVAL;
546 		goto error;
547 	}
548 
549 	priv = pn533_register_device(id->driver_info, protocols, protocol_type,
550 					phy, &usb_phy_ops, fops,
551 					&phy->udev->dev, &interface->dev);
552 
553 	if (IS_ERR(priv)) {
554 		rc = PTR_ERR(priv);
555 		goto error;
556 	}
557 
558 	phy->priv = priv;
559 
560 	rc = pn533_finalize_setup(priv);
561 	if (rc)
562 		goto error;
563 
564 	usb_set_intfdata(interface, phy);
565 
566 	return 0;
567 
568 error:
569 	usb_free_urb(phy->in_urb);
570 	usb_free_urb(phy->out_urb);
571 	usb_free_urb(phy->ack_urb);
572 	usb_put_dev(phy->udev);
573 	kfree(in_buf);
574 
575 	return rc;
576 }
577 
578 static void pn533_usb_disconnect(struct usb_interface *interface)
579 {
580 	struct pn533_usb_phy *phy = usb_get_intfdata(interface);
581 
582 	if (!phy)
583 		return;
584 
585 	pn533_unregister_device(phy->priv);
586 
587 	usb_set_intfdata(interface, NULL);
588 
589 	usb_kill_urb(phy->in_urb);
590 	usb_kill_urb(phy->out_urb);
591 	usb_kill_urb(phy->ack_urb);
592 
593 	kfree(phy->in_urb->transfer_buffer);
594 	usb_free_urb(phy->in_urb);
595 	usb_free_urb(phy->out_urb);
596 	usb_free_urb(phy->ack_urb);
597 	kfree(phy->ack_buffer);
598 
599 	nfc_info(&interface->dev, "NXP PN533 NFC device disconnected\n");
600 }
601 
602 static struct usb_driver pn533_usb_driver = {
603 	.name =		"pn533_usb",
604 	.probe =	pn533_usb_probe,
605 	.disconnect =	pn533_usb_disconnect,
606 	.id_table =	pn533_usb_table,
607 };
608 
609 module_usb_driver(pn533_usb_driver);
610 
611 MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
612 MODULE_AUTHOR("Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
613 MODULE_AUTHOR("Waldemar Rymarkiewicz <waldemar.rymarkiewicz@tieto.com>");
614 MODULE_DESCRIPTION("PN533 USB driver ver " VERSION);
615 MODULE_VERSION(VERSION);
616 MODULE_LICENSE("GPL");
617