GNU Linux-libre 4.19.314-gnu1
[releases.git] / drivers / nfc / pn533 / usb.c
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 struct pn533_out_arg {
169         struct pn533_usb_phy *phy;
170         struct completion done;
171 };
172
173 static int pn533_usb_send_frame(struct pn533 *dev,
174                                 struct sk_buff *out)
175 {
176         struct pn533_usb_phy *phy = dev->phy;
177         struct pn533_out_arg arg;
178         void *cntx;
179         int rc;
180
181         if (phy->priv == NULL)
182                 phy->priv = dev;
183
184         phy->out_urb->transfer_buffer = out->data;
185         phy->out_urb->transfer_buffer_length = out->len;
186
187         print_hex_dump_debug("PN533 TX: ", DUMP_PREFIX_NONE, 16, 1,
188                              out->data, out->len, false);
189
190         arg.phy = phy;
191         init_completion(&arg.done);
192         cntx = phy->out_urb->context;
193         phy->out_urb->context = &arg;
194
195         rc = usb_submit_urb(phy->out_urb, GFP_KERNEL);
196         if (rc)
197                 return rc;
198
199         wait_for_completion(&arg.done);
200         phy->out_urb->context = cntx;
201
202         if (dev->protocol_type == PN533_PROTO_REQ_RESP) {
203                 /* request for response for sent packet directly */
204                 rc = pn533_submit_urb_for_response(phy, GFP_KERNEL);
205                 if (rc)
206                         goto error;
207         } else if (dev->protocol_type == PN533_PROTO_REQ_ACK_RESP) {
208                 /* request for ACK if that's the case */
209                 rc = pn533_submit_urb_for_ack(phy, GFP_KERNEL);
210                 if (rc)
211                         goto error;
212         }
213
214         return 0;
215
216 error:
217         usb_unlink_urb(phy->out_urb);
218         return rc;
219 }
220
221 static void pn533_usb_abort_cmd(struct pn533 *dev, gfp_t flags)
222 {
223         struct pn533_usb_phy *phy = dev->phy;
224
225         /* ACR122U does not support any command which aborts last
226          * issued command i.e. as ACK for standard PN533. Additionally,
227          * it behaves stange, sending broken or incorrect responses,
228          * when we cancel urb before the chip will send response.
229          */
230         if (dev->device_type == PN533_DEVICE_ACR122U)
231                 return;
232
233         /* An ack will cancel the last issued command */
234         pn533_usb_send_ack(dev, flags);
235
236         /* cancel the urb request */
237         usb_kill_urb(phy->in_urb);
238 }
239
240 /* ACR122 specific structs and fucntions */
241
242 /* ACS ACR122 pn533 frame definitions */
243 #define PN533_ACR122_TX_FRAME_HEADER_LEN (sizeof(struct pn533_acr122_tx_frame) \
244                                           + 2)
245 #define PN533_ACR122_TX_FRAME_TAIL_LEN 0
246 #define PN533_ACR122_RX_FRAME_HEADER_LEN (sizeof(struct pn533_acr122_rx_frame) \
247                                           + 2)
248 #define PN533_ACR122_RX_FRAME_TAIL_LEN 2
249 #define PN533_ACR122_FRAME_MAX_PAYLOAD_LEN PN533_STD_FRAME_MAX_PAYLOAD_LEN
250
251 /* CCID messages types */
252 #define PN533_ACR122_PC_TO_RDR_ICCPOWERON 0x62
253 #define PN533_ACR122_PC_TO_RDR_ESCAPE 0x6B
254
255 #define PN533_ACR122_RDR_TO_PC_ESCAPE 0x83
256
257
258 struct pn533_acr122_ccid_hdr {
259         u8 type;
260         u32 datalen;
261         u8 slot;
262         u8 seq;
263
264         /*
265          * 3 msg specific bytes or status, error and 1 specific
266          * byte for reposnse msg
267          */
268         u8 params[3];
269         u8 data[]; /* payload */
270 } __packed;
271
272 struct pn533_acr122_apdu_hdr {
273         u8 class;
274         u8 ins;
275         u8 p1;
276         u8 p2;
277 } __packed;
278
279 struct pn533_acr122_tx_frame {
280         struct pn533_acr122_ccid_hdr ccid;
281         struct pn533_acr122_apdu_hdr apdu;
282         u8 datalen;
283         u8 data[]; /* pn533 frame: TFI ... */
284 } __packed;
285
286 struct pn533_acr122_rx_frame {
287         struct pn533_acr122_ccid_hdr ccid;
288         u8 data[]; /* pn533 frame : TFI ... */
289 } __packed;
290
291 static void pn533_acr122_tx_frame_init(void *_frame, u8 cmd_code)
292 {
293         struct pn533_acr122_tx_frame *frame = _frame;
294
295         frame->ccid.type = PN533_ACR122_PC_TO_RDR_ESCAPE;
296         /* sizeof(apdu_hdr) + sizeof(datalen) */
297         frame->ccid.datalen = sizeof(frame->apdu) + 1;
298         frame->ccid.slot = 0;
299         frame->ccid.seq = 0;
300         frame->ccid.params[0] = 0;
301         frame->ccid.params[1] = 0;
302         frame->ccid.params[2] = 0;
303
304         frame->data[0] = PN533_STD_FRAME_DIR_OUT;
305         frame->data[1] = cmd_code;
306         frame->datalen = 2;  /* data[0] + data[1] */
307
308         frame->apdu.class = 0xFF;
309         frame->apdu.ins = 0;
310         frame->apdu.p1 = 0;
311         frame->apdu.p2 = 0;
312 }
313
314 static void pn533_acr122_tx_frame_finish(void *_frame)
315 {
316         struct pn533_acr122_tx_frame *frame = _frame;
317
318         frame->ccid.datalen += frame->datalen;
319 }
320
321 static void pn533_acr122_tx_update_payload_len(void *_frame, int len)
322 {
323         struct pn533_acr122_tx_frame *frame = _frame;
324
325         frame->datalen += len;
326 }
327
328 static bool pn533_acr122_is_rx_frame_valid(void *_frame, struct pn533 *dev)
329 {
330         struct pn533_acr122_rx_frame *frame = _frame;
331
332         if (frame->ccid.type != 0x83)
333                 return false;
334
335         if (!frame->ccid.datalen)
336                 return false;
337
338         if (frame->data[frame->ccid.datalen - 2] == 0x63)
339                 return false;
340
341         return true;
342 }
343
344 static int pn533_acr122_rx_frame_size(void *frame)
345 {
346         struct pn533_acr122_rx_frame *f = frame;
347
348         /* f->ccid.datalen already includes tail length */
349         return sizeof(struct pn533_acr122_rx_frame) + f->ccid.datalen;
350 }
351
352 static u8 pn533_acr122_get_cmd_code(void *frame)
353 {
354         struct pn533_acr122_rx_frame *f = frame;
355
356         return PN533_FRAME_CMD(f);
357 }
358
359 static struct pn533_frame_ops pn533_acr122_frame_ops = {
360         .tx_frame_init = pn533_acr122_tx_frame_init,
361         .tx_frame_finish = pn533_acr122_tx_frame_finish,
362         .tx_update_payload_len = pn533_acr122_tx_update_payload_len,
363         .tx_header_len = PN533_ACR122_TX_FRAME_HEADER_LEN,
364         .tx_tail_len = PN533_ACR122_TX_FRAME_TAIL_LEN,
365
366         .rx_is_frame_valid = pn533_acr122_is_rx_frame_valid,
367         .rx_header_len = PN533_ACR122_RX_FRAME_HEADER_LEN,
368         .rx_tail_len = PN533_ACR122_RX_FRAME_TAIL_LEN,
369         .rx_frame_size = pn533_acr122_rx_frame_size,
370
371         .max_payload_len = PN533_ACR122_FRAME_MAX_PAYLOAD_LEN,
372         .get_cmd_code = pn533_acr122_get_cmd_code,
373 };
374
375 struct pn533_acr122_poweron_rdr_arg {
376         int rc;
377         struct completion done;
378 };
379
380 static void pn533_acr122_poweron_rdr_resp(struct urb *urb)
381 {
382         struct pn533_acr122_poweron_rdr_arg *arg = urb->context;
383
384         dev_dbg(&urb->dev->dev, "%s\n", __func__);
385
386         print_hex_dump_debug("ACR122 RX: ", DUMP_PREFIX_NONE, 16, 1,
387                        urb->transfer_buffer, urb->transfer_buffer_length,
388                        false);
389
390         arg->rc = urb->status;
391         complete(&arg->done);
392 }
393
394 static int pn533_acr122_poweron_rdr(struct pn533_usb_phy *phy)
395 {
396         /* Power on th reader (CCID cmd) */
397         u8 cmd[10] = {PN533_ACR122_PC_TO_RDR_ICCPOWERON,
398                       0, 0, 0, 0, 0, 0, 3, 0, 0};
399         char *buffer;
400         int transferred;
401         int rc;
402         void *cntx;
403         struct pn533_acr122_poweron_rdr_arg arg;
404
405         dev_dbg(&phy->udev->dev, "%s\n", __func__);
406
407         buffer = kmemdup(cmd, sizeof(cmd), GFP_KERNEL);
408         if (!buffer)
409                 return -ENOMEM;
410
411         init_completion(&arg.done);
412         cntx = phy->in_urb->context;  /* backup context */
413
414         phy->in_urb->complete = pn533_acr122_poweron_rdr_resp;
415         phy->in_urb->context = &arg;
416
417         print_hex_dump_debug("ACR122 TX: ", DUMP_PREFIX_NONE, 16, 1,
418                        cmd, sizeof(cmd), false);
419
420         rc = usb_bulk_msg(phy->udev, phy->out_urb->pipe, buffer, sizeof(cmd),
421                           &transferred, 5000);
422         kfree(buffer);
423         if (rc || (transferred != sizeof(cmd))) {
424                 nfc_err(&phy->udev->dev,
425                         "Reader power on cmd error %d\n", rc);
426                 return rc;
427         }
428
429         rc =  usb_submit_urb(phy->in_urb, GFP_KERNEL);
430         if (rc) {
431                 nfc_err(&phy->udev->dev,
432                         "Can't submit reader poweron cmd response %d\n", rc);
433                 return rc;
434         }
435
436         wait_for_completion(&arg.done);
437         phy->in_urb->context = cntx; /* restore context */
438
439         return arg.rc;
440 }
441
442 static void pn533_out_complete(struct urb *urb)
443 {
444         struct pn533_out_arg *arg = urb->context;
445         struct pn533_usb_phy *phy = arg->phy;
446
447         switch (urb->status) {
448         case 0:
449                 break; /* success */
450         case -ECONNRESET:
451         case -ENOENT:
452                 dev_dbg(&phy->udev->dev,
453                         "The urb has been stopped (status %d)\n",
454                         urb->status);
455                 break;
456         case -ESHUTDOWN:
457         default:
458                 nfc_err(&phy->udev->dev,
459                         "Urb failure (status %d)\n",
460                         urb->status);
461         }
462
463         complete(&arg->done);
464 }
465
466 static void pn533_ack_complete(struct urb *urb)
467 {
468         struct pn533_usb_phy *phy = urb->context;
469
470         switch (urb->status) {
471         case 0:
472                 break; /* success */
473         case -ECONNRESET:
474         case -ENOENT:
475                 dev_dbg(&phy->udev->dev,
476                         "The urb has been stopped (status %d)\n",
477                         urb->status);
478                 break;
479         case -ESHUTDOWN:
480         default:
481                 nfc_err(&phy->udev->dev,
482                         "Urb failure (status %d)\n",
483                         urb->status);
484         }
485 }
486
487 static struct pn533_phy_ops usb_phy_ops = {
488         .send_frame = pn533_usb_send_frame,
489         .send_ack = pn533_usb_send_ack,
490         .abort_cmd = pn533_usb_abort_cmd,
491 };
492
493 static int pn533_usb_probe(struct usb_interface *interface,
494                         const struct usb_device_id *id)
495 {
496         struct pn533 *priv;
497         struct pn533_usb_phy *phy;
498         struct usb_host_interface *iface_desc;
499         struct usb_endpoint_descriptor *endpoint;
500         int in_endpoint = 0;
501         int out_endpoint = 0;
502         int rc = -ENOMEM;
503         int i;
504         u32 protocols;
505         enum pn533_protocol_type protocol_type = PN533_PROTO_REQ_ACK_RESP;
506         struct pn533_frame_ops *fops = NULL;
507         unsigned char *in_buf;
508         int in_buf_len = PN533_EXT_FRAME_HEADER_LEN +
509                          PN533_STD_FRAME_MAX_PAYLOAD_LEN +
510                          PN533_STD_FRAME_TAIL_LEN;
511
512         phy = devm_kzalloc(&interface->dev, sizeof(*phy), GFP_KERNEL);
513         if (!phy)
514                 return -ENOMEM;
515
516         in_buf = kzalloc(in_buf_len, GFP_KERNEL);
517         if (!in_buf)
518                 return -ENOMEM;
519
520         phy->udev = usb_get_dev(interface_to_usbdev(interface));
521         phy->interface = interface;
522
523         iface_desc = interface->cur_altsetting;
524         for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
525                 endpoint = &iface_desc->endpoint[i].desc;
526
527                 if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint))
528                         in_endpoint = endpoint->bEndpointAddress;
529
530                 if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint))
531                         out_endpoint = endpoint->bEndpointAddress;
532         }
533
534         if (!in_endpoint || !out_endpoint) {
535                 nfc_err(&interface->dev,
536                         "Could not find bulk-in or bulk-out endpoint\n");
537                 rc = -ENODEV;
538                 goto error;
539         }
540
541         phy->in_urb = usb_alloc_urb(0, GFP_KERNEL);
542         phy->out_urb = usb_alloc_urb(0, GFP_KERNEL);
543         phy->ack_urb = usb_alloc_urb(0, GFP_KERNEL);
544
545         if (!phy->in_urb || !phy->out_urb || !phy->ack_urb)
546                 goto error;
547
548         usb_fill_bulk_urb(phy->in_urb, phy->udev,
549                           usb_rcvbulkpipe(phy->udev, in_endpoint),
550                           in_buf, in_buf_len, NULL, phy);
551
552         usb_fill_bulk_urb(phy->out_urb, phy->udev,
553                           usb_sndbulkpipe(phy->udev, out_endpoint),
554                           NULL, 0, pn533_out_complete, phy);
555         usb_fill_bulk_urb(phy->ack_urb, phy->udev,
556                           usb_sndbulkpipe(phy->udev, out_endpoint),
557                           NULL, 0, pn533_ack_complete, phy);
558
559         switch (id->driver_info) {
560         case PN533_DEVICE_STD:
561                 protocols = PN533_ALL_PROTOCOLS;
562                 break;
563
564         case PN533_DEVICE_PASORI:
565                 protocols = PN533_NO_TYPE_B_PROTOCOLS;
566                 break;
567
568         case PN533_DEVICE_ACR122U:
569                 protocols = PN533_NO_TYPE_B_PROTOCOLS;
570                 fops = &pn533_acr122_frame_ops;
571                 protocol_type = PN533_PROTO_REQ_RESP,
572
573                 rc = pn533_acr122_poweron_rdr(phy);
574                 if (rc < 0) {
575                         nfc_err(&interface->dev,
576                                 "Couldn't poweron the reader (error %d)\n", rc);
577                         goto error;
578                 }
579                 break;
580
581         default:
582                 nfc_err(&interface->dev, "Unknown device type %lu\n",
583                         id->driver_info);
584                 rc = -EINVAL;
585                 goto error;
586         }
587
588         priv = pn533_register_device(id->driver_info, protocols, protocol_type,
589                                         phy, &usb_phy_ops, fops,
590                                         &phy->udev->dev, &interface->dev);
591
592         if (IS_ERR(priv)) {
593                 rc = PTR_ERR(priv);
594                 goto error;
595         }
596
597         phy->priv = priv;
598
599         rc = pn533_finalize_setup(priv);
600         if (rc)
601                 goto err_deregister;
602
603         usb_set_intfdata(interface, phy);
604
605         return 0;
606
607 err_deregister:
608         pn533_unregister_device(phy->priv);
609 error:
610         usb_kill_urb(phy->in_urb);
611         usb_kill_urb(phy->out_urb);
612         usb_kill_urb(phy->ack_urb);
613
614         usb_free_urb(phy->in_urb);
615         usb_free_urb(phy->out_urb);
616         usb_free_urb(phy->ack_urb);
617         usb_put_dev(phy->udev);
618         kfree(in_buf);
619         kfree(phy->ack_buffer);
620
621         return rc;
622 }
623
624 static void pn533_usb_disconnect(struct usb_interface *interface)
625 {
626         struct pn533_usb_phy *phy = usb_get_intfdata(interface);
627
628         if (!phy)
629                 return;
630
631         pn533_unregister_device(phy->priv);
632
633         usb_set_intfdata(interface, NULL);
634
635         usb_kill_urb(phy->in_urb);
636         usb_kill_urb(phy->out_urb);
637         usb_kill_urb(phy->ack_urb);
638
639         kfree(phy->in_urb->transfer_buffer);
640         usb_free_urb(phy->in_urb);
641         usb_free_urb(phy->out_urb);
642         usb_free_urb(phy->ack_urb);
643         kfree(phy->ack_buffer);
644
645         nfc_info(&interface->dev, "NXP PN533 NFC device disconnected\n");
646 }
647
648 static struct usb_driver pn533_usb_driver = {
649         .name =         "pn533_usb",
650         .probe =        pn533_usb_probe,
651         .disconnect =   pn533_usb_disconnect,
652         .id_table =     pn533_usb_table,
653 };
654
655 module_usb_driver(pn533_usb_driver);
656
657 MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
658 MODULE_AUTHOR("Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
659 MODULE_AUTHOR("Waldemar Rymarkiewicz <waldemar.rymarkiewicz@tieto.com>");
660 MODULE_DESCRIPTION("PN533 USB driver ver " VERSION);
661 MODULE_VERSION(VERSION);
662 MODULE_LICENSE("GPL");