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