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