Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

NFC: pn533: Separate physical layer from the core implementation

The driver now has all core stuff isolated in one file, and all
the hardware link specifics in another. Writing a pn533 driver
on top of another hardware link is now just a matter of adding a
new file for that new hardware specifics.

The first user of this separation will be the i2c based pn532
driver that reuses pn533 core implementation on top of an i2c
layer.

Signed-off-by: Michael Thalmeier <michael.thalmeier@hale.at>
Signed-off-by: Samuel Ortiz <sameo@linux.intel.com>

authored by

Michael Thalmeier and committed by
Samuel Ortiz
9815c7cf 37f895d7

+1078 -921
+1 -10
drivers/nfc/Kconfig
··· 5 5 menu "Near Field Communication (NFC) devices" 6 6 depends on NFC 7 7 8 - config NFC_PN533 9 - tristate "NXP PN533 USB driver" 10 - depends on USB 11 - help 12 - NXP PN533 USB driver. 13 - This driver provides support for NFC NXP PN533 devices. 14 - 15 - Say Y here to compile support for PN533 devices into the 16 - kernel or say M to compile it as module (pn533). 17 - 18 8 config NFC_WILINK 19 9 tristate "Texas Instruments NFC WiLink driver" 20 10 depends on TI_ST && NFC_NCI ··· 60 70 61 71 source "drivers/nfc/fdp/Kconfig" 62 72 source "drivers/nfc/pn544/Kconfig" 73 + source "drivers/nfc/pn533/Kconfig" 63 74 source "drivers/nfc/microread/Kconfig" 64 75 source "drivers/nfc/nfcmrvl/Kconfig" 65 76 source "drivers/nfc/st21nfca/Kconfig"
+1 -1
drivers/nfc/Makefile
··· 5 5 obj-$(CONFIG_NFC_FDP) += fdp/ 6 6 obj-$(CONFIG_NFC_PN544) += pn544/ 7 7 obj-$(CONFIG_NFC_MICROREAD) += microread/ 8 - obj-$(CONFIG_NFC_PN533) += pn533.o 8 + obj-$(CONFIG_NFC_PN533) += pn533/ 9 9 obj-$(CONFIG_NFC_WILINK) += nfcwilink.o 10 10 obj-$(CONFIG_NFC_MEI_PHY) += mei_phy.o 11 11 obj-$(CONFIG_NFC_SIM) += nfcsim.o
+220 -910
drivers/nfc/pn533.c drivers/nfc/pn533/pn533.c
··· 1 1 /* 2 + * Driver for NXP PN533 NFC Chip - core functions 3 + * 2 4 * Copyright (C) 2011 Instituto Nokia de Tecnologia 3 5 * Copyright (C) 2012-2013 Tieto Poland 4 6 * ··· 22 20 #include <linux/kernel.h> 23 21 #include <linux/module.h> 24 22 #include <linux/slab.h> 25 - #include <linux/usb.h> 26 23 #include <linux/nfc.h> 27 24 #include <linux/netdevice.h> 28 25 #include <net/nfc/nfc.h> 26 + #include "pn533.h" 29 27 30 - #define VERSION "0.2" 31 - 32 - #define PN533_VENDOR_ID 0x4CC 33 - #define PN533_PRODUCT_ID 0x2533 34 - 35 - #define SCM_VENDOR_ID 0x4E6 36 - #define SCL3711_PRODUCT_ID 0x5591 37 - 38 - #define SONY_VENDOR_ID 0x054c 39 - #define PASORI_PRODUCT_ID 0x02e1 40 - 41 - #define ACS_VENDOR_ID 0x072f 42 - #define ACR122U_PRODUCT_ID 0x2200 43 - 44 - #define PN533_DEVICE_STD 0x1 45 - #define PN533_DEVICE_PASORI 0x2 46 - #define PN533_DEVICE_ACR122U 0x3 47 - 48 - #define PN533_ALL_PROTOCOLS (NFC_PROTO_JEWEL_MASK | NFC_PROTO_MIFARE_MASK |\ 49 - NFC_PROTO_FELICA_MASK | NFC_PROTO_ISO14443_MASK |\ 50 - NFC_PROTO_NFC_DEP_MASK |\ 51 - NFC_PROTO_ISO14443_B_MASK) 52 - 53 - #define PN533_NO_TYPE_B_PROTOCOLS (NFC_PROTO_JEWEL_MASK | \ 54 - NFC_PROTO_MIFARE_MASK | \ 55 - NFC_PROTO_FELICA_MASK | \ 56 - NFC_PROTO_ISO14443_MASK | \ 57 - NFC_PROTO_NFC_DEP_MASK) 58 - 59 - static const struct usb_device_id pn533_table[] = { 60 - { USB_DEVICE(PN533_VENDOR_ID, PN533_PRODUCT_ID), 61 - .driver_info = PN533_DEVICE_STD }, 62 - { USB_DEVICE(SCM_VENDOR_ID, SCL3711_PRODUCT_ID), 63 - .driver_info = PN533_DEVICE_STD }, 64 - { USB_DEVICE(SONY_VENDOR_ID, PASORI_PRODUCT_ID), 65 - .driver_info = PN533_DEVICE_PASORI }, 66 - { USB_DEVICE(ACS_VENDOR_ID, ACR122U_PRODUCT_ID), 67 - .driver_info = PN533_DEVICE_ACR122U }, 68 - { } 69 - }; 70 - MODULE_DEVICE_TABLE(usb, pn533_table); 28 + #define VERSION "0.3" 71 29 72 30 /* How much time we spend listening for initiators */ 73 31 #define PN533_LISTEN_TIME 2 74 32 /* Delay between each poll frame (ms) */ 75 33 #define PN533_POLL_INTERVAL 10 76 - 77 - /* Standard pn533 frame definitions (standard and extended)*/ 78 - #define PN533_STD_FRAME_HEADER_LEN (sizeof(struct pn533_std_frame) \ 79 - + 2) /* data[0] TFI, data[1] CC */ 80 - #define PN533_STD_FRAME_TAIL_LEN 2 /* data[len] DCS, data[len + 1] postamble*/ 81 - 82 - #define PN533_EXT_FRAME_HEADER_LEN (sizeof(struct pn533_ext_frame) \ 83 - + 2) /* data[0] TFI, data[1] CC */ 84 - 85 - #define PN533_CMD_DATAEXCH_DATA_MAXLEN 262 86 - #define PN533_CMD_DATAFRAME_MAXLEN 240 /* max data length (send) */ 87 - 88 - /* 89 - * Max extended frame payload len, excluding TFI and CC 90 - * which are already in PN533_FRAME_HEADER_LEN. 91 - */ 92 - #define PN533_STD_FRAME_MAX_PAYLOAD_LEN 263 93 - 94 - #define PN533_STD_FRAME_ACK_SIZE 6 /* Preamble (1), SoPC (2), ACK Code (2), 95 - Postamble (1) */ 96 - #define PN533_STD_FRAME_CHECKSUM(f) (f->data[f->datalen]) 97 - #define PN533_STD_FRAME_POSTAMBLE(f) (f->data[f->datalen + 1]) 98 - /* Half start code (3), LEN (4) should be 0xffff for extended frame */ 99 - #define PN533_STD_IS_EXTENDED(hdr) ((hdr)->datalen == 0xFF \ 100 - && (hdr)->datalen_checksum == 0xFF) 101 - #define PN533_EXT_FRAME_CHECKSUM(f) (f->data[be16_to_cpu(f->datalen)]) 102 - 103 - /* start of frame */ 104 - #define PN533_STD_FRAME_SOF 0x00FF 105 - 106 - /* standard frame identifier: in/out/error */ 107 - #define PN533_STD_FRAME_IDENTIFIER(f) (f->data[0]) /* TFI */ 108 - #define PN533_STD_FRAME_DIR_OUT 0xD4 109 - #define PN533_STD_FRAME_DIR_IN 0xD5 110 - 111 - /* ACS ACR122 pn533 frame definitions */ 112 - #define PN533_ACR122_TX_FRAME_HEADER_LEN (sizeof(struct pn533_acr122_tx_frame) \ 113 - + 2) 114 - #define PN533_ACR122_TX_FRAME_TAIL_LEN 0 115 - #define PN533_ACR122_RX_FRAME_HEADER_LEN (sizeof(struct pn533_acr122_rx_frame) \ 116 - + 2) 117 - #define PN533_ACR122_RX_FRAME_TAIL_LEN 2 118 - #define PN533_ACR122_FRAME_MAX_PAYLOAD_LEN PN533_STD_FRAME_MAX_PAYLOAD_LEN 119 - 120 - /* CCID messages types */ 121 - #define PN533_ACR122_PC_TO_RDR_ICCPOWERON 0x62 122 - #define PN533_ACR122_PC_TO_RDR_ESCAPE 0x6B 123 - 124 - #define PN533_ACR122_RDR_TO_PC_ESCAPE 0x83 125 - 126 - /* PN533 Commands */ 127 - #define PN533_FRAME_CMD(f) (f->data[1]) 128 - 129 - #define PN533_CMD_GET_FIRMWARE_VERSION 0x02 130 - #define PN533_CMD_RF_CONFIGURATION 0x32 131 - #define PN533_CMD_IN_DATA_EXCHANGE 0x40 132 - #define PN533_CMD_IN_COMM_THRU 0x42 133 - #define PN533_CMD_IN_LIST_PASSIVE_TARGET 0x4A 134 - #define PN533_CMD_IN_ATR 0x50 135 - #define PN533_CMD_IN_RELEASE 0x52 136 - #define PN533_CMD_IN_JUMP_FOR_DEP 0x56 137 - 138 - #define PN533_CMD_TG_INIT_AS_TARGET 0x8c 139 - #define PN533_CMD_TG_GET_DATA 0x86 140 - #define PN533_CMD_TG_SET_DATA 0x8e 141 - #define PN533_CMD_TG_SET_META_DATA 0x94 142 - #define PN533_CMD_UNDEF 0xff 143 - 144 - #define PN533_CMD_RESPONSE(cmd) (cmd + 1) 145 - 146 - /* PN533 Return codes */ 147 - #define PN533_CMD_RET_MASK 0x3F 148 - #define PN533_CMD_MI_MASK 0x40 149 - #define PN533_CMD_RET_SUCCESS 0x00 150 - 151 - struct pn533; 152 - 153 - typedef int (*pn533_send_async_complete_t) (struct pn533 *dev, void *arg, 154 - struct sk_buff *resp); 155 34 156 35 /* structs for pn533 commands */ 157 36 ··· 102 219 u8 tsn; 103 220 } __packed felica; 104 221 }; 105 - 106 - /* Poll modulations */ 107 - enum { 108 - PN533_POLL_MOD_106KBPS_A, 109 - PN533_POLL_MOD_212KBPS_FELICA, 110 - PN533_POLL_MOD_424KBPS_FELICA, 111 - PN533_POLL_MOD_106KBPS_JEWEL, 112 - PN533_POLL_MOD_847KBPS_B, 113 - PN533_LISTEN_MOD, 114 - 115 - __PN533_POLL_MOD_AFTER_LAST, 116 - }; 117 - #define PN533_POLL_MOD_MAX (__PN533_POLL_MOD_AFTER_LAST - 1) 118 222 119 223 struct pn533_poll_modulations { 120 224 struct { ··· 206 336 #define PN533_INIT_TARGET_RESP_ACTIVE 0x1 207 337 #define PN533_INIT_TARGET_RESP_DEP 0x4 208 338 209 - enum pn533_protocol_type { 210 - PN533_PROTO_REQ_ACK_RESP = 0, 211 - PN533_PROTO_REQ_RESP 212 - }; 213 - 214 - struct pn533 { 215 - struct usb_device *udev; 216 - struct usb_interface *interface; 217 - struct nfc_dev *nfc_dev; 218 - u32 device_type; 219 - enum pn533_protocol_type protocol_type; 220 - 221 - struct urb *out_urb; 222 - struct urb *in_urb; 223 - 224 - struct sk_buff_head resp_q; 225 - struct sk_buff_head fragment_skb; 226 - 227 - struct workqueue_struct *wq; 228 - struct work_struct cmd_work; 229 - struct work_struct cmd_complete_work; 230 - struct delayed_work poll_work; 231 - struct work_struct mi_rx_work; 232 - struct work_struct mi_tx_work; 233 - struct work_struct mi_tm_rx_work; 234 - struct work_struct mi_tm_tx_work; 235 - struct work_struct tg_work; 236 - struct work_struct rf_work; 237 - 238 - struct list_head cmd_queue; 239 - struct pn533_cmd *cmd; 240 - u8 cmd_pending; 241 - struct mutex cmd_lock; /* protects cmd queue */ 242 - 243 - void *cmd_complete_mi_arg; 244 - void *cmd_complete_dep_arg; 245 - 246 - struct pn533_poll_modulations *poll_mod_active[PN533_POLL_MOD_MAX + 1]; 247 - u8 poll_mod_count; 248 - u8 poll_mod_curr; 249 - u8 poll_dep; 250 - u32 poll_protocols; 251 - u32 listen_protocols; 252 - struct timer_list listen_timer; 253 - int cancel_listen; 254 - 255 - u8 *gb; 256 - size_t gb_len; 257 - 258 - u8 tgt_available_prots; 259 - u8 tgt_active_prot; 260 - u8 tgt_mode; 261 - 262 - struct pn533_frame_ops *ops; 263 - }; 264 - 265 - struct pn533_cmd { 266 - struct list_head queue; 267 - u8 code; 268 - int status; 269 - struct sk_buff *req; 270 - struct sk_buff *resp; 271 - int resp_len; 272 - pn533_send_async_complete_t complete_cb; 273 - void *complete_cb_context; 274 - }; 275 - 276 - struct pn533_std_frame { 277 - u8 preamble; 278 - __be16 start_frame; 279 - u8 datalen; 280 - u8 datalen_checksum; 281 - u8 data[]; 282 - } __packed; 283 - 284 - struct pn533_ext_frame { /* Extended Information frame */ 285 - u8 preamble; 286 - __be16 start_frame; 287 - __be16 eif_flag; /* fixed to 0xFFFF */ 288 - __be16 datalen; 289 - u8 datalen_checksum; 290 - u8 data[]; 291 - } __packed; 292 - 293 - struct pn533_frame_ops { 294 - void (*tx_frame_init)(void *frame, u8 cmd_code); 295 - void (*tx_frame_finish)(void *frame); 296 - void (*tx_update_payload_len)(void *frame, int len); 297 - int tx_header_len; 298 - int tx_tail_len; 299 - 300 - bool (*rx_is_frame_valid)(void *frame, struct pn533 *dev); 301 - int (*rx_frame_size)(void *frame); 302 - int rx_header_len; 303 - int rx_tail_len; 304 - 305 - int max_payload_len; 306 - u8 (*get_cmd_code)(void *frame); 307 - }; 308 - 309 - struct pn533_acr122_ccid_hdr { 310 - u8 type; 311 - u32 datalen; 312 - u8 slot; 313 - u8 seq; 314 - u8 params[3]; /* 3 msg specific bytes or status, error and 1 specific 315 - byte for reposnse msg */ 316 - u8 data[]; /* payload */ 317 - } __packed; 318 - 319 - struct pn533_acr122_apdu_hdr { 320 - u8 class; 321 - u8 ins; 322 - u8 p1; 323 - u8 p2; 324 - } __packed; 325 - 326 - struct pn533_acr122_tx_frame { 327 - struct pn533_acr122_ccid_hdr ccid; 328 - struct pn533_acr122_apdu_hdr apdu; 329 - u8 datalen; 330 - u8 data[]; /* pn533 frame: TFI ... */ 331 - } __packed; 332 - 333 - struct pn533_acr122_rx_frame { 334 - struct pn533_acr122_ccid_hdr ccid; 335 - u8 data[]; /* pn533 frame : TFI ... */ 336 - } __packed; 337 - 338 - static void pn533_acr122_tx_frame_init(void *_frame, u8 cmd_code) 339 - { 340 - struct pn533_acr122_tx_frame *frame = _frame; 341 - 342 - frame->ccid.type = PN533_ACR122_PC_TO_RDR_ESCAPE; 343 - frame->ccid.datalen = sizeof(frame->apdu) + 1; /* sizeof(apdu_hdr) + 344 - sizeof(datalen) */ 345 - frame->ccid.slot = 0; 346 - frame->ccid.seq = 0; 347 - frame->ccid.params[0] = 0; 348 - frame->ccid.params[1] = 0; 349 - frame->ccid.params[2] = 0; 350 - 351 - frame->data[0] = PN533_STD_FRAME_DIR_OUT; 352 - frame->data[1] = cmd_code; 353 - frame->datalen = 2; /* data[0] + data[1] */ 354 - 355 - frame->apdu.class = 0xFF; 356 - frame->apdu.ins = 0; 357 - frame->apdu.p1 = 0; 358 - frame->apdu.p2 = 0; 359 - } 360 - 361 - static void pn533_acr122_tx_frame_finish(void *_frame) 362 - { 363 - struct pn533_acr122_tx_frame *frame = _frame; 364 - 365 - frame->ccid.datalen += frame->datalen; 366 - } 367 - 368 - static void pn533_acr122_tx_update_payload_len(void *_frame, int len) 369 - { 370 - struct pn533_acr122_tx_frame *frame = _frame; 371 - 372 - frame->datalen += len; 373 - } 374 - 375 - static bool pn533_acr122_is_rx_frame_valid(void *_frame, struct pn533 *dev) 376 - { 377 - struct pn533_acr122_rx_frame *frame = _frame; 378 - 379 - if (frame->ccid.type != 0x83) 380 - return false; 381 - 382 - if (!frame->ccid.datalen) 383 - return false; 384 - 385 - if (frame->data[frame->ccid.datalen - 2] == 0x63) 386 - return false; 387 - 388 - return true; 389 - } 390 - 391 - static int pn533_acr122_rx_frame_size(void *frame) 392 - { 393 - struct pn533_acr122_rx_frame *f = frame; 394 - 395 - /* f->ccid.datalen already includes tail length */ 396 - return sizeof(struct pn533_acr122_rx_frame) + f->ccid.datalen; 397 - } 398 - 399 - static u8 pn533_acr122_get_cmd_code(void *frame) 400 - { 401 - struct pn533_acr122_rx_frame *f = frame; 402 - 403 - return PN533_FRAME_CMD(f); 404 - } 405 - 406 - static struct pn533_frame_ops pn533_acr122_frame_ops = { 407 - .tx_frame_init = pn533_acr122_tx_frame_init, 408 - .tx_frame_finish = pn533_acr122_tx_frame_finish, 409 - .tx_update_payload_len = pn533_acr122_tx_update_payload_len, 410 - .tx_header_len = PN533_ACR122_TX_FRAME_HEADER_LEN, 411 - .tx_tail_len = PN533_ACR122_TX_FRAME_TAIL_LEN, 412 - 413 - .rx_is_frame_valid = pn533_acr122_is_rx_frame_valid, 414 - .rx_header_len = PN533_ACR122_RX_FRAME_HEADER_LEN, 415 - .rx_tail_len = PN533_ACR122_RX_FRAME_TAIL_LEN, 416 - .rx_frame_size = pn533_acr122_rx_frame_size, 417 - 418 - .max_payload_len = PN533_ACR122_FRAME_MAX_PAYLOAD_LEN, 419 - .get_cmd_code = pn533_acr122_get_cmd_code, 420 - }; 421 - 422 339 /* The rule: value(high byte) + value(low byte) + checksum = 0 */ 423 340 static inline u8 pn533_ext_checksum(u16 value) 424 341 { ··· 299 642 return true; 300 643 } 301 644 302 - static bool pn533_std_rx_frame_is_ack(struct pn533_std_frame *frame) 645 + bool pn533_rx_frame_is_ack(void *_frame) 303 646 { 647 + struct pn533_std_frame *frame = _frame; 648 + 304 649 if (frame->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF)) 305 650 return false; 306 651 ··· 311 652 312 653 return true; 313 654 } 655 + EXPORT_SYMBOL_GPL(pn533_rx_frame_is_ack); 314 656 315 657 static inline int pn533_std_rx_frame_size(void *frame) 316 658 { ··· 340 680 return PN533_FRAME_CMD(f); 341 681 } 342 682 683 + bool pn533_rx_frame_is_cmd_response(struct pn533 *dev, void *frame) 684 + { 685 + return (dev->ops->get_cmd_code(frame) == 686 + PN533_CMD_RESPONSE(dev->cmd->code)); 687 + } 688 + EXPORT_SYMBOL_GPL(pn533_rx_frame_is_cmd_response); 689 + 690 + 343 691 static struct pn533_frame_ops pn533_std_frame_ops = { 344 692 .tx_frame_init = pn533_std_tx_frame_init, 345 693 .tx_frame_finish = pn533_std_tx_frame_finish, ··· 363 695 .max_payload_len = PN533_STD_FRAME_MAX_PAYLOAD_LEN, 364 696 .get_cmd_code = pn533_std_get_cmd_code, 365 697 }; 366 - 367 - static bool pn533_rx_frame_is_cmd_response(struct pn533 *dev, void *frame) 368 - { 369 - return (dev->ops->get_cmd_code(frame) == 370 - PN533_CMD_RESPONSE(dev->cmd->code)); 371 - } 372 - 373 - static void pn533_recv_response(struct urb *urb) 374 - { 375 - struct pn533 *dev = urb->context; 376 - struct pn533_cmd *cmd = dev->cmd; 377 - u8 *in_frame; 378 - 379 - cmd->status = urb->status; 380 - 381 - switch (urb->status) { 382 - case 0: 383 - break; /* success */ 384 - case -ECONNRESET: 385 - case -ENOENT: 386 - dev_dbg(&dev->interface->dev, 387 - "The urb has been canceled (status %d)\n", 388 - urb->status); 389 - goto sched_wq; 390 - case -ESHUTDOWN: 391 - default: 392 - nfc_err(&dev->interface->dev, 393 - "Urb failure (status %d)\n", urb->status); 394 - goto sched_wq; 395 - } 396 - 397 - in_frame = dev->in_urb->transfer_buffer; 398 - 399 - dev_dbg(&dev->interface->dev, "Received a frame\n"); 400 - print_hex_dump_debug("PN533 RX: ", DUMP_PREFIX_NONE, 16, 1, in_frame, 401 - dev->ops->rx_frame_size(in_frame), false); 402 - 403 - if (!dev->ops->rx_is_frame_valid(in_frame, dev)) { 404 - nfc_err(&dev->interface->dev, "Received an invalid frame\n"); 405 - cmd->status = -EIO; 406 - goto sched_wq; 407 - } 408 - 409 - if (!pn533_rx_frame_is_cmd_response(dev, in_frame)) { 410 - nfc_err(&dev->interface->dev, 411 - "It it not the response to the last command\n"); 412 - cmd->status = -EIO; 413 - goto sched_wq; 414 - } 415 - 416 - sched_wq: 417 - queue_work(dev->wq, &dev->cmd_complete_work); 418 - } 419 - 420 - static int pn533_submit_urb_for_response(struct pn533 *dev, gfp_t flags) 421 - { 422 - dev->in_urb->complete = pn533_recv_response; 423 - 424 - return usb_submit_urb(dev->in_urb, flags); 425 - } 426 - 427 - static void pn533_recv_ack(struct urb *urb) 428 - { 429 - struct pn533 *dev = urb->context; 430 - struct pn533_cmd *cmd = dev->cmd; 431 - struct pn533_std_frame *in_frame; 432 - int rc; 433 - 434 - cmd->status = urb->status; 435 - 436 - switch (urb->status) { 437 - case 0: 438 - break; /* success */ 439 - case -ECONNRESET: 440 - case -ENOENT: 441 - dev_dbg(&dev->interface->dev, 442 - "The urb has been stopped (status %d)\n", 443 - urb->status); 444 - goto sched_wq; 445 - case -ESHUTDOWN: 446 - default: 447 - nfc_err(&dev->interface->dev, 448 - "Urb failure (status %d)\n", urb->status); 449 - goto sched_wq; 450 - } 451 - 452 - in_frame = dev->in_urb->transfer_buffer; 453 - 454 - if (!pn533_std_rx_frame_is_ack(in_frame)) { 455 - nfc_err(&dev->interface->dev, "Received an invalid ack\n"); 456 - cmd->status = -EIO; 457 - goto sched_wq; 458 - } 459 - 460 - rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC); 461 - if (rc) { 462 - nfc_err(&dev->interface->dev, 463 - "usb_submit_urb failed with result %d\n", rc); 464 - cmd->status = rc; 465 - goto sched_wq; 466 - } 467 - 468 - return; 469 - 470 - sched_wq: 471 - queue_work(dev->wq, &dev->cmd_complete_work); 472 - } 473 - 474 - static int pn533_submit_urb_for_ack(struct pn533 *dev, gfp_t flags) 475 - { 476 - dev->in_urb->complete = pn533_recv_ack; 477 - 478 - return usb_submit_urb(dev->in_urb, flags); 479 - } 480 - 481 - static int pn533_send_ack(struct pn533 *dev, gfp_t flags) 482 - { 483 - u8 ack[PN533_STD_FRAME_ACK_SIZE] = {0x00, 0x00, 0xff, 0x00, 0xff, 0x00}; 484 - /* spec 7.1.1.3: Preamble, SoPC (2), ACK Code (2), Postamble */ 485 - int rc; 486 - 487 - dev->out_urb->transfer_buffer = ack; 488 - dev->out_urb->transfer_buffer_length = sizeof(ack); 489 - rc = usb_submit_urb(dev->out_urb, flags); 490 - 491 - return rc; 492 - } 493 - 494 - static int __pn533_send_frame_async(struct pn533 *dev, 495 - struct sk_buff *out, 496 - struct sk_buff *in, 497 - int in_len) 498 - { 499 - int rc; 500 - 501 - dev->out_urb->transfer_buffer = out->data; 502 - dev->out_urb->transfer_buffer_length = out->len; 503 - 504 - dev->in_urb->transfer_buffer = in->data; 505 - dev->in_urb->transfer_buffer_length = in_len; 506 - 507 - print_hex_dump_debug("PN533 TX: ", DUMP_PREFIX_NONE, 16, 1, 508 - out->data, out->len, false); 509 - 510 - rc = usb_submit_urb(dev->out_urb, GFP_KERNEL); 511 - if (rc) 512 - return rc; 513 - 514 - if (dev->protocol_type == PN533_PROTO_REQ_RESP) { 515 - /* request for response for sent packet directly */ 516 - rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC); 517 - if (rc) 518 - goto error; 519 - } else if (dev->protocol_type == PN533_PROTO_REQ_ACK_RESP) { 520 - /* request for ACK if that's the case */ 521 - rc = pn533_submit_urb_for_ack(dev, GFP_KERNEL); 522 - if (rc) 523 - goto error; 524 - } 525 - 526 - return 0; 527 - 528 - error: 529 - usb_unlink_urb(dev->out_urb); 530 - return rc; 531 - } 532 698 533 699 static void pn533_build_cmd_frame(struct pn533 *dev, u8 cmd_code, 534 700 struct sk_buff *skb) ··· 399 897 goto done; 400 898 } 401 899 402 - skb_put(resp, dev->ops->rx_frame_size(resp->data)); 403 900 skb_pull(resp, dev->ops->rx_header_len); 404 901 skb_trim(resp, resp->len - dev->ops->rx_tail_len); 405 902 ··· 411 910 } 412 911 413 912 static int __pn533_send_async(struct pn533 *dev, u8 cmd_code, 414 - struct sk_buff *req, struct sk_buff *resp, 415 - int resp_len, 913 + struct sk_buff *req, 416 914 pn533_send_async_complete_t complete_cb, 417 915 void *complete_cb_context) 418 916 { 419 917 struct pn533_cmd *cmd; 420 918 int rc = 0; 421 919 422 - dev_dbg(&dev->interface->dev, "Sending command 0x%x\n", cmd_code); 920 + dev_dbg(dev->dev, "Sending command 0x%x\n", cmd_code); 423 921 424 922 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); 425 923 if (!cmd) ··· 426 926 427 927 cmd->code = cmd_code; 428 928 cmd->req = req; 429 - cmd->resp = resp; 430 - cmd->resp_len = resp_len; 431 929 cmd->complete_cb = complete_cb; 432 930 cmd->complete_cb_context = complete_cb_context; 433 931 ··· 434 936 mutex_lock(&dev->cmd_lock); 435 937 436 938 if (!dev->cmd_pending) { 437 - rc = __pn533_send_frame_async(dev, req, resp, resp_len); 939 + rc = dev->phy_ops->send_frame(dev, req); 438 940 if (rc) 439 941 goto error; 440 942 ··· 443 945 goto unlock; 444 946 } 445 947 446 - dev_dbg(&dev->interface->dev, "%s Queueing command 0x%x\n", 948 + dev_dbg(dev->dev, "%s Queueing command 0x%x\n", 447 949 __func__, cmd_code); 448 950 449 951 INIT_LIST_HEAD(&cmd->queue); ··· 463 965 pn533_send_async_complete_t complete_cb, 464 966 void *complete_cb_context) 465 967 { 466 - struct sk_buff *resp; 467 968 int rc; 468 - int resp_len = dev->ops->rx_header_len + 469 - dev->ops->max_payload_len + 470 - dev->ops->rx_tail_len; 471 969 472 - resp = nfc_alloc_recv_skb(resp_len, GFP_KERNEL); 473 - if (!resp) 474 - return -ENOMEM; 475 - 476 - rc = __pn533_send_async(dev, cmd_code, req, resp, resp_len, complete_cb, 970 + rc = __pn533_send_async(dev, cmd_code, req, complete_cb, 477 971 complete_cb_context); 478 - if (rc) 479 - dev_kfree_skb(resp); 480 972 481 973 return rc; 482 974 } ··· 476 988 pn533_send_async_complete_t complete_cb, 477 989 void *complete_cb_context) 478 990 { 479 - struct sk_buff *resp; 480 991 int rc; 481 - int resp_len = dev->ops->rx_header_len + 482 - dev->ops->max_payload_len + 483 - dev->ops->rx_tail_len; 484 992 485 - resp = alloc_skb(resp_len, GFP_KERNEL); 486 - if (!resp) 487 - return -ENOMEM; 488 - 489 - rc = __pn533_send_async(dev, cmd_code, req, resp, resp_len, complete_cb, 993 + rc = __pn533_send_async(dev, cmd_code, req, complete_cb, 490 994 complete_cb_context); 491 - if (rc) 492 - dev_kfree_skb(resp); 493 995 494 996 return rc; 495 997 } ··· 497 1019 pn533_send_async_complete_t complete_cb, 498 1020 void *complete_cb_context) 499 1021 { 500 - struct sk_buff *resp; 501 1022 struct pn533_cmd *cmd; 502 1023 int rc; 503 - int resp_len = dev->ops->rx_header_len + 504 - dev->ops->max_payload_len + 505 - dev->ops->rx_tail_len; 506 - 507 - resp = alloc_skb(resp_len, GFP_KERNEL); 508 - if (!resp) 509 - return -ENOMEM; 510 1024 511 1025 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); 512 - if (!cmd) { 513 - dev_kfree_skb(resp); 1026 + if (!cmd) 514 1027 return -ENOMEM; 515 - } 516 1028 517 1029 cmd->code = cmd_code; 518 1030 cmd->req = req; 519 - cmd->resp = resp; 520 - cmd->resp_len = resp_len; 521 1031 cmd->complete_cb = complete_cb; 522 1032 cmd->complete_cb_context = complete_cb_context; 523 1033 524 1034 pn533_build_cmd_frame(dev, cmd_code, req); 525 1035 526 - rc = __pn533_send_frame_async(dev, req, resp, resp_len); 527 - if (rc < 0) { 528 - dev_kfree_skb(resp); 1036 + rc = dev->phy_ops->send_frame(dev, req); 1037 + if (rc < 0) 529 1038 kfree(cmd); 530 - } else { 1039 + else 531 1040 dev->cmd = cmd; 532 - } 533 1041 534 1042 return rc; 535 1043 } ··· 550 1086 551 1087 mutex_unlock(&dev->cmd_lock); 552 1088 553 - rc = __pn533_send_frame_async(dev, cmd->req, cmd->resp, cmd->resp_len); 1089 + rc = dev->phy_ops->send_frame(dev, cmd->req); 554 1090 if (rc < 0) { 555 1091 dev_kfree_skb(cmd->req); 556 - dev_kfree_skb(cmd->resp); 557 1092 kfree(cmd); 558 1093 return; 559 1094 } ··· 584 1121 * 1. negative in case of error during TX path -> req should be freed 585 1122 * 586 1123 * 2. negative in case of error during RX path -> req should not be freed 587 - * as it's been already freed at the begining of RX path by 1124 + * as it's been already freed at the beginning of RX path by 588 1125 * async_complete_cb. 589 1126 * 590 1127 * 3. valid pointer in case of succesfult RX path ··· 592 1129 * A caller has to check a return value with IS_ERR macro. If the test pass, 593 1130 * the returned pointer is valid. 594 1131 * 595 - * */ 1132 + */ 596 1133 static struct sk_buff *pn533_send_cmd_sync(struct pn533 *dev, u8 cmd_code, 597 1134 struct sk_buff *req) 598 1135 { ··· 611 1148 wait_for_completion(&arg.done); 612 1149 613 1150 return arg.resp; 614 - } 615 - 616 - static void pn533_send_complete(struct urb *urb) 617 - { 618 - struct pn533 *dev = urb->context; 619 - 620 - switch (urb->status) { 621 - case 0: 622 - break; /* success */ 623 - case -ECONNRESET: 624 - case -ENOENT: 625 - dev_dbg(&dev->interface->dev, 626 - "The urb has been stopped (status %d)\n", 627 - urb->status); 628 - break; 629 - case -ESHUTDOWN: 630 - default: 631 - nfc_err(&dev->interface->dev, "Urb failure (status %d)\n", 632 - urb->status); 633 - } 634 - } 635 - 636 - static void pn533_abort_cmd(struct pn533 *dev, gfp_t flags) 637 - { 638 - /* ACR122U does not support any command which aborts last 639 - * issued command i.e. as ACK for standard PN533. Additionally, 640 - * it behaves stange, sending broken or incorrect responses, 641 - * when we cancel urb before the chip will send response. 642 - */ 643 - if (dev->device_type == PN533_DEVICE_ACR122U) 644 - return; 645 - 646 - /* An ack will cancel the last issued command */ 647 - pn533_send_ack(dev, flags); 648 - 649 - /* cancel the urb request */ 650 - usb_kill_urb(dev->in_urb); 651 1151 } 652 1152 653 1153 static struct sk_buff *pn533_alloc_skb(struct pn533 *dev, unsigned int size) ··· 659 1233 if (target_data_len < sizeof(struct pn533_target_type_a)) 660 1234 return false; 661 1235 662 - /* The lenght check of nfcid[] and ats[] are not being performed because 663 - the values are not being used */ 1236 + /* 1237 + * The length check of nfcid[] and ats[] are not being performed because 1238 + * the values are not being used 1239 + */ 664 1240 665 1241 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */ 666 1242 ssd = PN533_TYPE_A_SENS_RES_SSD(type_a->sens_res); ··· 871 1443 struct nfc_target nfc_tgt; 872 1444 int rc; 873 1445 874 - dev_dbg(&dev->interface->dev, "%s: modulation=%d\n", 1446 + dev_dbg(dev->dev, "%s: modulation=%d\n", 875 1447 __func__, dev->poll_mod_curr); 876 1448 877 1449 if (tg != 1) ··· 894 1466 rc = pn533_target_found_type_b(&nfc_tgt, tgdata, tgdata_len); 895 1467 break; 896 1468 default: 897 - nfc_err(&dev->interface->dev, 1469 + nfc_err(dev->dev, 898 1470 "Unknown current poll modulation\n"); 899 1471 return -EPROTO; 900 1472 } ··· 903 1475 return rc; 904 1476 905 1477 if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) { 906 - dev_dbg(&dev->interface->dev, 1478 + dev_dbg(dev->dev, 907 1479 "The Tg found doesn't have the desired protocol\n"); 908 1480 return -EAGAIN; 909 1481 } 910 1482 911 - dev_dbg(&dev->interface->dev, 1483 + dev_dbg(dev->dev, 912 1484 "Target found - supported protocols: 0x%x\n", 913 1485 nfc_tgt.supported_protocols); 914 1486 ··· 1006 1578 0x0, 0x0, 0x0, 1007 1579 0x40}; /* SEL_RES for DEP */ 1008 1580 1009 - unsigned int skb_len = 36 + /* mode (1), mifare (6), 1010 - felica (18), nfcid3 (10), gb_len (1) */ 1581 + unsigned int skb_len = 36 + /* 1582 + * mode (1), mifare (6), 1583 + * felica (18), nfcid3 (10), gb_len (1) 1584 + */ 1011 1585 gbytes_len + 1012 1586 1; /* len Tk*/ 1013 1587 ··· 1045 1615 return skb; 1046 1616 } 1047 1617 1048 - #define PN533_CMD_DATAEXCH_HEAD_LEN 1 1049 - #define PN533_CMD_DATAEXCH_DATA_MAXLEN 262 1050 1618 static void pn533_wq_tm_mi_recv(struct work_struct *work); 1051 1619 static struct sk_buff *pn533_build_response(struct pn533 *dev); 1052 1620 ··· 1055 1627 u8 status, ret, mi; 1056 1628 int rc; 1057 1629 1058 - dev_dbg(&dev->interface->dev, "%s\n", __func__); 1630 + dev_dbg(dev->dev, "%s\n", __func__); 1059 1631 1060 1632 if (IS_ERR(resp)) { 1061 1633 skb_queue_purge(&dev->resp_q); ··· 1104 1676 struct sk_buff *skb; 1105 1677 int rc; 1106 1678 1107 - dev_dbg(&dev->interface->dev, "%s\n", __func__); 1679 + dev_dbg(dev->dev, "%s\n", __func__); 1108 1680 1109 1681 skb = pn533_alloc_skb(dev, 0); 1110 1682 if (!skb) ··· 1118 1690 1119 1691 if (rc < 0) 1120 1692 dev_kfree_skb(skb); 1121 - 1122 - return; 1123 1693 } 1124 1694 1125 1695 static int pn533_tm_send_complete(struct pn533 *dev, void *arg, ··· 1128 1702 struct sk_buff *skb; 1129 1703 int rc; 1130 1704 1131 - dev_dbg(&dev->interface->dev, "%s\n", __func__); 1705 + dev_dbg(dev->dev, "%s\n", __func__); 1132 1706 1133 1707 /* Grab the first skb in the queue */ 1134 1708 skb = skb_dequeue(&dev->fragment_skb); ··· 1150 1724 if (rc == 0) /* success */ 1151 1725 return; 1152 1726 1153 - dev_err(&dev->interface->dev, 1727 + dev_err(dev->dev, 1154 1728 "Error %d when trying to perform set meta data_exchange", rc); 1155 1729 1156 1730 dev_kfree_skb(skb); 1157 1731 1158 1732 error: 1159 - pn533_send_ack(dev, GFP_KERNEL); 1733 + dev->phy_ops->send_ack(dev, GFP_KERNEL); 1160 1734 queue_work(dev->wq, &dev->cmd_work); 1161 1735 } 1162 1736 ··· 1166 1740 struct sk_buff *skb; 1167 1741 int rc; 1168 1742 1169 - dev_dbg(&dev->interface->dev, "%s\n", __func__); 1743 + dev_dbg(dev->dev, "%s\n", __func__); 1170 1744 1171 1745 skb = pn533_alloc_skb(dev, 0); 1172 1746 if (!skb) ··· 1177 1751 1178 1752 if (rc < 0) 1179 1753 dev_kfree_skb(skb); 1180 - 1181 - return; 1182 1754 } 1183 1755 1184 1756 #define ATR_REQ_GB_OFFSET 17 ··· 1186 1762 size_t gb_len; 1187 1763 int rc; 1188 1764 1189 - dev_dbg(&dev->interface->dev, "%s\n", __func__); 1765 + dev_dbg(dev->dev, "%s\n", __func__); 1190 1766 1191 1767 if (resp->len < ATR_REQ_GB_OFFSET + 1) 1192 1768 return -EINVAL; ··· 1194 1770 mode = resp->data[0]; 1195 1771 cmd = &resp->data[1]; 1196 1772 1197 - dev_dbg(&dev->interface->dev, "Target mode 0x%x len %d\n", 1773 + dev_dbg(dev->dev, "Target mode 0x%x len %d\n", 1198 1774 mode, resp->len); 1199 1775 1200 1776 if ((mode & PN533_INIT_TARGET_RESP_FRAME_MASK) == ··· 1210 1786 rc = nfc_tm_activated(dev->nfc_dev, NFC_PROTO_NFC_DEP_MASK, 1211 1787 comm_mode, gb, gb_len); 1212 1788 if (rc < 0) { 1213 - nfc_err(&dev->interface->dev, 1789 + nfc_err(dev->dev, 1214 1790 "Error when signaling target activation\n"); 1215 1791 return rc; 1216 1792 } ··· 1225 1801 { 1226 1802 struct pn533 *dev = (struct pn533 *)data; 1227 1803 1228 - dev_dbg(&dev->interface->dev, "Listen mode timeout\n"); 1804 + dev_dbg(dev->dev, "Listen mode timeout\n"); 1229 1805 1230 1806 dev->cancel_listen = 1; 1231 1807 ··· 1240 1816 { 1241 1817 int rc = 0; 1242 1818 1243 - dev_dbg(&dev->interface->dev, "%s\n", __func__); 1819 + dev_dbg(dev->dev, "%s\n", __func__); 1244 1820 1245 1821 if (IS_ERR(resp)) { 1246 1822 rc = PTR_ERR(resp); 1247 1823 1248 - nfc_err(&dev->interface->dev, "RF setting error %d\n", rc); 1824 + nfc_err(dev->dev, "RF setting error %d\n", rc); 1249 1825 1250 1826 return rc; 1251 1827 } ··· 1263 1839 struct sk_buff *skb; 1264 1840 int rc; 1265 1841 1266 - dev_dbg(&dev->interface->dev, "%s\n", __func__); 1842 + dev_dbg(dev->dev, "%s\n", __func__); 1267 1843 1268 1844 skb = pn533_alloc_skb(dev, 2); 1269 1845 if (!skb) ··· 1276 1852 pn533_rf_complete, NULL); 1277 1853 if (rc < 0) { 1278 1854 dev_kfree_skb(skb); 1279 - nfc_err(&dev->interface->dev, "RF setting error %d\n", rc); 1855 + nfc_err(dev->dev, "RF setting error %d\n", rc); 1280 1856 } 1281 - 1282 - return; 1283 1857 } 1284 1858 1285 1859 static int pn533_poll_dep_complete(struct pn533 *dev, void *arg, ··· 1302 1880 return 0; 1303 1881 } 1304 1882 1305 - dev_dbg(&dev->interface->dev, "Creating new target"); 1883 + dev_dbg(dev->dev, "Creating new target"); 1306 1884 1307 1885 nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK; 1308 1886 nfc_target.nfcid1_len = 10; ··· 1340 1918 u8 *next, nfcid3[NFC_NFCID3_MAXSIZE]; 1341 1919 u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3}; 1342 1920 1343 - dev_dbg(&dev->interface->dev, "%s", __func__); 1921 + dev_dbg(dev->dev, "%s", __func__); 1344 1922 1345 1923 if (!dev->gb) { 1346 1924 dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len); ··· 1397 1975 struct pn533_poll_modulations *cur_mod; 1398 1976 int rc; 1399 1977 1400 - dev_dbg(&dev->interface->dev, "%s\n", __func__); 1978 + dev_dbg(dev->dev, "%s\n", __func__); 1401 1979 1402 1980 if (IS_ERR(resp)) { 1403 1981 rc = PTR_ERR(resp); 1404 1982 1405 - nfc_err(&dev->interface->dev, "%s Poll complete error %d\n", 1983 + nfc_err(dev->dev, "%s Poll complete error %d\n", 1406 1984 __func__, rc); 1407 1985 1408 1986 if (rc == -ENOENT) { 1409 1987 if (dev->poll_mod_count != 0) 1410 1988 return rc; 1411 - else 1412 - goto stop_poll; 1989 + goto stop_poll; 1413 1990 } else if (rc < 0) { 1414 - nfc_err(&dev->interface->dev, 1991 + nfc_err(dev->dev, 1415 1992 "Error %d when running poll\n", rc); 1416 1993 goto stop_poll; 1417 1994 } ··· 1430 2009 goto done; 1431 2010 1432 2011 if (!dev->poll_mod_count) { 1433 - dev_dbg(&dev->interface->dev, "Polling has been stopped\n"); 2012 + dev_dbg(dev->dev, "Polling has been stopped\n"); 1434 2013 goto done; 1435 2014 } 1436 2015 ··· 1443 2022 return rc; 1444 2023 1445 2024 stop_poll: 1446 - nfc_err(&dev->interface->dev, "Polling operation has been stopped\n"); 2025 + nfc_err(dev->dev, "Polling operation has been stopped\n"); 1447 2026 1448 2027 pn533_poll_reset_mod_list(dev); 1449 2028 dev->poll_protocols = 0; ··· 1473 2052 1474 2053 mod = dev->poll_mod_active[dev->poll_mod_curr]; 1475 2054 1476 - dev_dbg(&dev->interface->dev, "%s mod len %d\n", 2055 + dev_dbg(dev->dev, "%s mod len %d\n", 1477 2056 __func__, mod->len); 1478 2057 1479 2058 if ((dev->poll_protocols & NFC_PROTO_NFC_DEP_MASK) && dev->poll_dep) { ··· 1490 2069 } 1491 2070 1492 2071 if (!skb) { 1493 - nfc_err(&dev->interface->dev, "Failed to allocate skb\n"); 2072 + nfc_err(dev->dev, "Failed to allocate skb\n"); 1494 2073 return -ENOMEM; 1495 2074 } 1496 2075 ··· 1498 2077 NULL); 1499 2078 if (rc < 0) { 1500 2079 dev_kfree_skb(skb); 1501 - nfc_err(&dev->interface->dev, "Polling loop error %d\n", rc); 2080 + nfc_err(dev->dev, "Polling loop error %d\n", rc); 1502 2081 } 1503 2082 1504 2083 return rc; ··· 1512 2091 1513 2092 cur_mod = dev->poll_mod_active[dev->poll_mod_curr]; 1514 2093 1515 - dev_dbg(&dev->interface->dev, 2094 + dev_dbg(dev->dev, 1516 2095 "%s cancel_listen %d modulation len %d\n", 1517 2096 __func__, dev->cancel_listen, cur_mod->len); 1518 2097 1519 2098 if (dev->cancel_listen == 1) { 1520 2099 dev->cancel_listen = 0; 1521 - pn533_abort_cmd(dev, GFP_ATOMIC); 2100 + dev->phy_ops->abort_cmd(dev, GFP_ATOMIC); 1522 2101 } 1523 2102 1524 2103 rc = pn533_send_poll_frame(dev); ··· 1527 2106 1528 2107 if (cur_mod->len == 0 && dev->poll_mod_count > 1) 1529 2108 mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ); 1530 - 1531 - return; 1532 2109 } 1533 2110 1534 2111 static int pn533_start_poll(struct nfc_dev *nfc_dev, ··· 1537 2118 u8 rand_mod; 1538 2119 int rc; 1539 2120 1540 - dev_dbg(&dev->interface->dev, 2121 + dev_dbg(dev->dev, 1541 2122 "%s: im protocols 0x%x tm protocols 0x%x\n", 1542 2123 __func__, im_protocols, tm_protocols); 1543 2124 1544 2125 if (dev->tgt_active_prot) { 1545 - nfc_err(&dev->interface->dev, 2126 + nfc_err(dev->dev, 1546 2127 "Cannot poll with a target already activated\n"); 1547 2128 return -EBUSY; 1548 2129 } 1549 2130 1550 2131 if (dev->tgt_mode) { 1551 - nfc_err(&dev->interface->dev, 2132 + nfc_err(dev->dev, 1552 2133 "Cannot poll while already being activated\n"); 1553 2134 return -EBUSY; 1554 2135 } ··· 1586 2167 del_timer(&dev->listen_timer); 1587 2168 1588 2169 if (!dev->poll_mod_count) { 1589 - dev_dbg(&dev->interface->dev, 2170 + dev_dbg(dev->dev, 1590 2171 "Polling operation was not running\n"); 1591 2172 return; 1592 2173 } 1593 2174 1594 - pn533_abort_cmd(dev, GFP_KERNEL); 2175 + dev->phy_ops->abort_cmd(dev, GFP_KERNEL); 1595 2176 flush_delayed_work(&dev->poll_work); 1596 2177 pn533_poll_reset_mod_list(dev); 1597 2178 } ··· 1604 2185 struct sk_buff *skb; 1605 2186 struct sk_buff *resp; 1606 2187 1607 - dev_dbg(&dev->interface->dev, "%s\n", __func__); 2188 + dev_dbg(dev->dev, "%s\n", __func__); 1608 2189 1609 2190 skb = pn533_alloc_skb(dev, sizeof(u8) * 2); /*TG + Next*/ 1610 2191 if (!skb) ··· 1620 2201 rsp = (struct pn533_cmd_activate_response *)resp->data; 1621 2202 rc = rsp->status & PN533_CMD_RET_MASK; 1622 2203 if (rc != PN533_CMD_RET_SUCCESS) { 1623 - nfc_err(&dev->interface->dev, 2204 + nfc_err(dev->dev, 1624 2205 "Target activation failed (error 0x%x)\n", rc); 1625 2206 dev_kfree_skb(resp); 1626 2207 return -EIO; ··· 1640 2221 struct pn533 *dev = nfc_get_drvdata(nfc_dev); 1641 2222 int rc; 1642 2223 1643 - dev_dbg(&dev->interface->dev, "%s: protocol=%u\n", __func__, protocol); 2224 + dev_dbg(dev->dev, "%s: protocol=%u\n", __func__, protocol); 1644 2225 1645 2226 if (dev->poll_mod_count) { 1646 - nfc_err(&dev->interface->dev, 2227 + nfc_err(dev->dev, 1647 2228 "Cannot activate while polling\n"); 1648 2229 return -EBUSY; 1649 2230 } 1650 2231 1651 2232 if (dev->tgt_active_prot) { 1652 - nfc_err(&dev->interface->dev, 2233 + nfc_err(dev->dev, 1653 2234 "There is already an active target\n"); 1654 2235 return -EBUSY; 1655 2236 } 1656 2237 1657 2238 if (!dev->tgt_available_prots) { 1658 - nfc_err(&dev->interface->dev, 2239 + nfc_err(dev->dev, 1659 2240 "There is no available target to activate\n"); 1660 2241 return -EINVAL; 1661 2242 } 1662 2243 1663 2244 if (!(dev->tgt_available_prots & (1 << protocol))) { 1664 - nfc_err(&dev->interface->dev, 2245 + nfc_err(dev->dev, 1665 2246 "Target doesn't support requested proto %u\n", 1666 2247 protocol); 1667 2248 return -EINVAL; ··· 1670 2251 if (protocol == NFC_PROTO_NFC_DEP) { 1671 2252 rc = pn533_activate_target_nfcdep(dev); 1672 2253 if (rc) { 1673 - nfc_err(&dev->interface->dev, 2254 + nfc_err(dev->dev, 1674 2255 "Activating target with DEP failed %d\n", rc); 1675 2256 return rc; 1676 2257 } ··· 1687 2268 { 1688 2269 int rc = 0; 1689 2270 1690 - dev_dbg(&dev->interface->dev, "%s\n", __func__); 2271 + dev_dbg(dev->dev, "%s\n", __func__); 1691 2272 1692 2273 if (IS_ERR(resp)) { 1693 2274 rc = PTR_ERR(resp); 1694 2275 1695 - nfc_err(&dev->interface->dev, "Target release error %d\n", rc); 2276 + nfc_err(dev->dev, "Target release error %d\n", rc); 1696 2277 1697 2278 return rc; 1698 2279 } 1699 2280 1700 2281 rc = resp->data[0] & PN533_CMD_RET_MASK; 1701 2282 if (rc != PN533_CMD_RET_SUCCESS) 1702 - nfc_err(&dev->interface->dev, 2283 + nfc_err(dev->dev, 1703 2284 "Error 0x%x when releasing the target\n", rc); 1704 2285 1705 2286 dev_kfree_skb(resp); ··· 1713 2294 struct sk_buff *skb; 1714 2295 int rc; 1715 2296 1716 - dev_dbg(&dev->interface->dev, "%s\n", __func__); 2297 + dev_dbg(dev->dev, "%s\n", __func__); 1717 2298 1718 2299 if (!dev->tgt_active_prot) { 1719 - nfc_err(&dev->interface->dev, "There is no active target\n"); 2300 + nfc_err(dev->dev, "There is no active target\n"); 1720 2301 return; 1721 2302 } 1722 2303 ··· 1733 2314 pn533_deactivate_target_complete, NULL); 1734 2315 if (rc < 0) { 1735 2316 dev_kfree_skb(skb); 1736 - nfc_err(&dev->interface->dev, "Target release error %d\n", rc); 2317 + nfc_err(dev->dev, "Target release error %d\n", rc); 1737 2318 } 1738 - 1739 - return; 1740 2319 } 1741 2320 1742 2321 ··· 1753 2336 1754 2337 if (dev->tgt_available_prots && 1755 2338 !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) { 1756 - nfc_err(&dev->interface->dev, 2339 + nfc_err(dev->dev, 1757 2340 "The target does not support DEP\n"); 1758 2341 rc = -EINVAL; 1759 2342 goto error; ··· 1763 2346 1764 2347 rc = rsp->status & PN533_CMD_RET_MASK; 1765 2348 if (rc != PN533_CMD_RET_SUCCESS) { 1766 - nfc_err(&dev->interface->dev, 2349 + nfc_err(dev->dev, 1767 2350 "Bringing DEP link up failed (error 0x%x)\n", rc); 1768 2351 goto error; 1769 2352 } ··· 1771 2354 if (!dev->tgt_available_prots) { 1772 2355 struct nfc_target nfc_target; 1773 2356 1774 - dev_dbg(&dev->interface->dev, "Creating new target\n"); 2357 + dev_dbg(dev->dev, "Creating new target\n"); 1775 2358 1776 2359 nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK; 1777 2360 nfc_target.nfcid1_len = 10; ··· 1809 2392 u8 *next, *arg, nfcid3[NFC_NFCID3_MAXSIZE]; 1810 2393 u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3}; 1811 2394 1812 - dev_dbg(&dev->interface->dev, "%s\n", __func__); 2395 + dev_dbg(dev->dev, "%s\n", __func__); 1813 2396 1814 2397 if (dev->poll_mod_count) { 1815 - nfc_err(&dev->interface->dev, 2398 + nfc_err(dev->dev, 1816 2399 "Cannot bring the DEP link up while polling\n"); 1817 2400 return -EBUSY; 1818 2401 } 1819 2402 1820 2403 if (dev->tgt_active_prot) { 1821 - nfc_err(&dev->interface->dev, 2404 + nfc_err(dev->dev, 1822 2405 "There is already an active target\n"); 1823 2406 return -EBUSY; 1824 2407 } ··· 1889 2472 { 1890 2473 struct pn533 *dev = nfc_get_drvdata(nfc_dev); 1891 2474 1892 - dev_dbg(&dev->interface->dev, "%s\n", __func__); 2475 + dev_dbg(dev->dev, "%s\n", __func__); 1893 2476 1894 2477 pn533_poll_reset_mod_list(dev); 1895 2478 1896 2479 if (dev->tgt_mode || dev->tgt_active_prot) 1897 - pn533_abort_cmd(dev, GFP_KERNEL); 2480 + dev->phy_ops->abort_cmd(dev, GFP_KERNEL); 1898 2481 1899 2482 dev->tgt_active_prot = 0; 1900 2483 dev->tgt_mode = 0; ··· 1914 2497 struct sk_buff *skb, *tmp, *t; 1915 2498 unsigned int skb_len = 0, tmp_len = 0; 1916 2499 1917 - dev_dbg(&dev->interface->dev, "%s\n", __func__); 2500 + dev_dbg(dev->dev, "%s\n", __func__); 1918 2501 1919 2502 if (skb_queue_empty(&dev->resp_q)) 1920 2503 return NULL; ··· 1927 2510 skb_queue_walk_safe(&dev->resp_q, tmp, t) 1928 2511 skb_len += tmp->len; 1929 2512 1930 - dev_dbg(&dev->interface->dev, "%s total length %d\n", 2513 + dev_dbg(dev->dev, "%s total length %d\n", 1931 2514 __func__, skb_len); 1932 2515 1933 2516 skb = alloc_skb(skb_len, GFP_KERNEL); ··· 1955 2538 int rc = 0; 1956 2539 u8 status, ret, mi; 1957 2540 1958 - dev_dbg(&dev->interface->dev, "%s\n", __func__); 2541 + dev_dbg(dev->dev, "%s\n", __func__); 1959 2542 1960 2543 if (IS_ERR(resp)) { 1961 2544 rc = PTR_ERR(resp); ··· 1969 2552 skb_pull(resp, sizeof(status)); 1970 2553 1971 2554 if (ret != PN533_CMD_RET_SUCCESS) { 1972 - nfc_err(&dev->interface->dev, 2555 + nfc_err(dev->dev, 1973 2556 "Exchanging data failed (error 0x%x)\n", ret); 1974 2557 rc = -EIO; 1975 2558 goto error; ··· 2009 2592 kfree(arg); 2010 2593 return rc; 2011 2594 } 2595 + 2596 + /* 2597 + * Receive an incoming pn533 frame. skb contains only header and payload. 2598 + * If skb == NULL, it is a notification that the link below is dead. 2599 + */ 2600 + void pn533_recv_frame(struct pn533 *dev, struct sk_buff *skb, int status) 2601 + { 2602 + dev->cmd->status = status; 2603 + 2604 + if (skb == NULL) { 2605 + pr_err("NULL Frame -> link is dead\n"); 2606 + goto sched_wq; 2607 + } 2608 + 2609 + if (pn533_rx_frame_is_ack(skb->data)) { 2610 + dev_dbg(dev->dev, "%s: Received ACK frame\n", __func__); 2611 + dev_kfree_skb(skb); 2612 + return; 2613 + } 2614 + 2615 + print_hex_dump_debug("PN533 RX: ", DUMP_PREFIX_NONE, 16, 1, skb->data, 2616 + dev->ops->rx_frame_size(skb->data), false); 2617 + 2618 + if (!dev->ops->rx_is_frame_valid(skb->data, dev)) { 2619 + nfc_err(dev->dev, "Received an invalid frame\n"); 2620 + dev->cmd->status = -EIO; 2621 + } else if (!pn533_rx_frame_is_cmd_response(dev, skb->data)) { 2622 + nfc_err(dev->dev, "It it not the response to the last command\n"); 2623 + dev->cmd->status = -EIO; 2624 + } 2625 + 2626 + dev->cmd->resp = skb; 2627 + 2628 + sched_wq: 2629 + queue_work(dev->wq, &dev->cmd_complete_work); 2630 + } 2631 + EXPORT_SYMBOL(pn533_recv_frame); 2012 2632 2013 2633 /* Split the Tx skb into small chunks */ 2014 2634 static int pn533_fill_fragment_skbs(struct pn533 *dev, struct sk_buff *skb) ··· 2102 2648 struct pn533_data_exchange_arg *arg = NULL; 2103 2649 int rc; 2104 2650 2105 - dev_dbg(&dev->interface->dev, "%s\n", __func__); 2651 + dev_dbg(dev->dev, "%s\n", __func__); 2106 2652 2107 2653 if (!dev->tgt_active_prot) { 2108 - nfc_err(&dev->interface->dev, 2654 + nfc_err(dev->dev, 2109 2655 "Can't exchange data if there is no active target\n"); 2110 2656 rc = -EINVAL; 2111 2657 goto error; ··· 2169 2715 { 2170 2716 u8 status; 2171 2717 2172 - dev_dbg(&dev->interface->dev, "%s\n", __func__); 2718 + dev_dbg(dev->dev, "%s\n", __func__); 2173 2719 2174 2720 if (IS_ERR(resp)) 2175 2721 return PTR_ERR(resp); ··· 2201 2747 struct pn533 *dev = nfc_get_drvdata(nfc_dev); 2202 2748 int rc; 2203 2749 2204 - dev_dbg(&dev->interface->dev, "%s\n", __func__); 2750 + dev_dbg(dev->dev, "%s\n", __func__); 2205 2751 2206 2752 /* let's split in multiple chunks if size's too big */ 2207 2753 if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) { ··· 2239 2785 struct sk_buff *skb; 2240 2786 int rc; 2241 2787 2242 - dev_dbg(&dev->interface->dev, "%s\n", __func__); 2788 + dev_dbg(dev->dev, "%s\n", __func__); 2243 2789 2244 2790 skb = pn533_alloc_skb(dev, PN533_CMD_DATAEXCH_HEAD_LEN); 2245 2791 if (!skb) ··· 2271 2817 if (rc == 0) /* success */ 2272 2818 return; 2273 2819 2274 - nfc_err(&dev->interface->dev, 2820 + nfc_err(dev->dev, 2275 2821 "Error %d when trying to perform data_exchange\n", rc); 2276 2822 2277 2823 dev_kfree_skb(skb); 2278 2824 kfree(dev->cmd_complete_mi_arg); 2279 2825 2280 2826 error: 2281 - pn533_send_ack(dev, GFP_KERNEL); 2827 + dev->phy_ops->send_ack(dev, GFP_KERNEL); 2282 2828 queue_work(dev->wq, &dev->cmd_work); 2283 2829 } 2284 2830 ··· 2288 2834 struct sk_buff *skb; 2289 2835 int rc; 2290 2836 2291 - dev_dbg(&dev->interface->dev, "%s\n", __func__); 2837 + dev_dbg(dev->dev, "%s\n", __func__); 2292 2838 2293 2839 /* Grab the first skb in the queue */ 2294 2840 skb = skb_dequeue(&dev->fragment_skb); ··· 2315 2861 2316 2862 default: 2317 2863 /* Still some fragments? */ 2318 - rc = pn533_send_cmd_direct_async(dev,PN533_CMD_IN_DATA_EXCHANGE, 2864 + rc = pn533_send_cmd_direct_async(dev, 2865 + PN533_CMD_IN_DATA_EXCHANGE, 2319 2866 skb, 2320 2867 pn533_data_exchange_complete, 2321 2868 dev->cmd_complete_dep_arg); ··· 2327 2872 if (rc == 0) /* success */ 2328 2873 return; 2329 2874 2330 - nfc_err(&dev->interface->dev, 2875 + nfc_err(dev->dev, 2331 2876 "Error %d when trying to perform data_exchange\n", rc); 2332 2877 2333 2878 dev_kfree_skb(skb); 2334 2879 kfree(dev->cmd_complete_dep_arg); 2335 2880 2336 2881 error: 2337 - pn533_send_ack(dev, GFP_KERNEL); 2882 + dev->phy_ops->send_ack(dev, GFP_KERNEL); 2338 2883 queue_work(dev->wq, &dev->cmd_work); 2339 2884 } 2340 2885 ··· 2345 2890 struct sk_buff *resp; 2346 2891 int skb_len; 2347 2892 2348 - dev_dbg(&dev->interface->dev, "%s\n", __func__); 2893 + dev_dbg(dev->dev, "%s\n", __func__); 2349 2894 2350 2895 skb_len = sizeof(cfgitem) + cfgdata_len; /* cfgitem + cfgdata */ 2351 2896 ··· 2392 2937 struct sk_buff *skb; 2393 2938 struct sk_buff *resp; 2394 2939 2395 - dev_dbg(&dev->interface->dev, "%s\n", __func__); 2940 + dev_dbg(dev->dev, "%s\n", __func__); 2396 2941 2397 2942 skb = pn533_alloc_skb(dev, sizeof(u8)); 2398 2943 if (!skb) ··· 2409 2954 return 0; 2410 2955 } 2411 2956 2412 - struct pn533_acr122_poweron_rdr_arg { 2413 - int rc; 2414 - struct completion done; 2415 - }; 2416 - 2417 - static void pn533_acr122_poweron_rdr_resp(struct urb *urb) 2418 - { 2419 - struct pn533_acr122_poweron_rdr_arg *arg = urb->context; 2420 - 2421 - dev_dbg(&urb->dev->dev, "%s\n", __func__); 2422 - 2423 - print_hex_dump_debug("ACR122 RX: ", DUMP_PREFIX_NONE, 16, 1, 2424 - urb->transfer_buffer, urb->transfer_buffer_length, 2425 - false); 2426 - 2427 - arg->rc = urb->status; 2428 - complete(&arg->done); 2429 - } 2430 - 2431 - static int pn533_acr122_poweron_rdr(struct pn533 *dev) 2432 - { 2433 - /* Power on th reader (CCID cmd) */ 2434 - u8 cmd[10] = {PN533_ACR122_PC_TO_RDR_ICCPOWERON, 2435 - 0, 0, 0, 0, 0, 0, 3, 0, 0}; 2436 - u8 buf[255]; 2437 - int rc; 2438 - void *cntx; 2439 - struct pn533_acr122_poweron_rdr_arg arg; 2440 - 2441 - dev_dbg(&dev->interface->dev, "%s\n", __func__); 2442 - 2443 - init_completion(&arg.done); 2444 - cntx = dev->in_urb->context; /* backup context */ 2445 - 2446 - dev->in_urb->transfer_buffer = buf; 2447 - dev->in_urb->transfer_buffer_length = 255; 2448 - dev->in_urb->complete = pn533_acr122_poweron_rdr_resp; 2449 - dev->in_urb->context = &arg; 2450 - 2451 - dev->out_urb->transfer_buffer = cmd; 2452 - dev->out_urb->transfer_buffer_length = sizeof(cmd); 2453 - 2454 - print_hex_dump_debug("ACR122 TX: ", DUMP_PREFIX_NONE, 16, 1, 2455 - cmd, sizeof(cmd), false); 2456 - 2457 - rc = usb_submit_urb(dev->out_urb, GFP_KERNEL); 2458 - if (rc) { 2459 - nfc_err(&dev->interface->dev, 2460 - "Reader power on cmd error %d\n", rc); 2461 - return rc; 2462 - } 2463 - 2464 - rc = usb_submit_urb(dev->in_urb, GFP_KERNEL); 2465 - if (rc) { 2466 - nfc_err(&dev->interface->dev, 2467 - "Can't submit reader poweron cmd response %d\n", rc); 2468 - return rc; 2469 - } 2470 - 2471 - wait_for_completion(&arg.done); 2472 - dev->in_urb->context = cntx; /* restore context */ 2473 - 2474 - return arg.rc; 2475 - } 2476 - 2477 2957 static int pn533_rf_field(struct nfc_dev *nfc_dev, u8 rf) 2478 2958 { 2479 2959 struct pn533 *dev = nfc_get_drvdata(nfc_dev); ··· 2420 3030 rc = pn533_set_configuration(dev, PN533_CFGITEM_RF_FIELD, 2421 3031 (u8 *)&rf_field, 1); 2422 3032 if (rc) { 2423 - nfc_err(&dev->interface->dev, "Error on setting RF field\n"); 3033 + nfc_err(dev->dev, "Error on setting RF field\n"); 2424 3034 return rc; 2425 3035 } 2426 3036 ··· 2473 3083 break; 2474 3084 2475 3085 default: 2476 - nfc_err(&dev->interface->dev, "Unknown device type %d\n", 3086 + nfc_err(dev->dev, "Unknown device type %d\n", 2477 3087 dev->device_type); 2478 3088 return -EINVAL; 2479 3089 } ··· 2481 3091 rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES, 2482 3092 (u8 *)&max_retries, sizeof(max_retries)); 2483 3093 if (rc) { 2484 - nfc_err(&dev->interface->dev, 3094 + nfc_err(dev->dev, 2485 3095 "Error on setting MAX_RETRIES config\n"); 2486 3096 return rc; 2487 3097 } ··· 2490 3100 rc = pn533_set_configuration(dev, PN533_CFGITEM_TIMING, 2491 3101 (u8 *)&timing, sizeof(timing)); 2492 3102 if (rc) { 2493 - nfc_err(&dev->interface->dev, "Error on setting RF timings\n"); 3103 + nfc_err(dev->dev, "Error on setting RF timings\n"); 2494 3104 return rc; 2495 3105 } 2496 3106 ··· 2504 3114 rc = pn533_set_configuration(dev, PN533_CFGITEM_PASORI, 2505 3115 pasori_cfg, 3); 2506 3116 if (rc) { 2507 - nfc_err(&dev->interface->dev, 3117 + nfc_err(dev->dev, 2508 3118 "Error while settings PASORI config\n"); 2509 3119 return rc; 2510 3120 } ··· 2517 3127 return 0; 2518 3128 } 2519 3129 2520 - static int pn533_probe(struct usb_interface *interface, 2521 - const struct usb_device_id *id) 3130 + struct pn533 *pn533_register_device(u32 device_type, 3131 + u32 protocols, 3132 + enum pn533_protocol_type protocol_type, 3133 + void *phy, 3134 + struct pn533_phy_ops *phy_ops, 3135 + struct pn533_frame_ops *fops, 3136 + struct device *dev) 2522 3137 { 2523 3138 struct pn533_fw_version fw_ver; 2524 - struct pn533 *dev; 2525 - struct usb_host_interface *iface_desc; 2526 - struct usb_endpoint_descriptor *endpoint; 2527 - int in_endpoint = 0; 2528 - int out_endpoint = 0; 3139 + struct pn533 *priv; 2529 3140 int rc = -ENOMEM; 2530 - int i; 2531 - u32 protocols; 2532 3141 2533 - dev = kzalloc(sizeof(*dev), GFP_KERNEL); 2534 - if (!dev) 2535 - return -ENOMEM; 3142 + priv = kzalloc(sizeof(*priv), GFP_KERNEL); 3143 + if (!priv) 3144 + return ERR_PTR(-ENOMEM); 2536 3145 2537 - dev->udev = usb_get_dev(interface_to_usbdev(interface)); 2538 - dev->interface = interface; 2539 - mutex_init(&dev->cmd_lock); 3146 + priv->phy = phy; 3147 + priv->phy_ops = phy_ops; 3148 + priv->dev = dev; 3149 + if (fops != NULL) 3150 + priv->ops = fops; 3151 + else 3152 + priv->ops = &pn533_std_frame_ops; 2540 3153 2541 - iface_desc = interface->cur_altsetting; 2542 - for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) { 2543 - endpoint = &iface_desc->endpoint[i].desc; 3154 + priv->protocol_type = protocol_type; 3155 + priv->device_type = device_type; 2544 3156 2545 - if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint)) 2546 - in_endpoint = endpoint->bEndpointAddress; 3157 + mutex_init(&priv->cmd_lock); 2547 3158 2548 - if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint)) 2549 - out_endpoint = endpoint->bEndpointAddress; 2550 - } 2551 - 2552 - if (!in_endpoint || !out_endpoint) { 2553 - nfc_err(&interface->dev, 2554 - "Could not find bulk-in or bulk-out endpoint\n"); 2555 - rc = -ENODEV; 2556 - goto error; 2557 - } 2558 - 2559 - dev->in_urb = usb_alloc_urb(0, GFP_KERNEL); 2560 - dev->out_urb = usb_alloc_urb(0, GFP_KERNEL); 2561 - 2562 - if (!dev->in_urb || !dev->out_urb) 3159 + INIT_WORK(&priv->cmd_work, pn533_wq_cmd); 3160 + INIT_WORK(&priv->cmd_complete_work, pn533_wq_cmd_complete); 3161 + INIT_WORK(&priv->mi_rx_work, pn533_wq_mi_recv); 3162 + INIT_WORK(&priv->mi_tx_work, pn533_wq_mi_send); 3163 + INIT_WORK(&priv->tg_work, pn533_wq_tg_get_data); 3164 + INIT_WORK(&priv->mi_tm_rx_work, pn533_wq_tm_mi_recv); 3165 + INIT_WORK(&priv->mi_tm_tx_work, pn533_wq_tm_mi_send); 3166 + INIT_DELAYED_WORK(&priv->poll_work, pn533_wq_poll); 3167 + INIT_WORK(&priv->rf_work, pn533_wq_rf); 3168 + priv->wq = alloc_ordered_workqueue("pn533", 0); 3169 + if (priv->wq == NULL) 2563 3170 goto error; 2564 3171 2565 - usb_fill_bulk_urb(dev->in_urb, dev->udev, 2566 - usb_rcvbulkpipe(dev->udev, in_endpoint), 2567 - NULL, 0, NULL, dev); 2568 - usb_fill_bulk_urb(dev->out_urb, dev->udev, 2569 - usb_sndbulkpipe(dev->udev, out_endpoint), 2570 - NULL, 0, pn533_send_complete, dev); 3172 + init_timer(&priv->listen_timer); 3173 + priv->listen_timer.data = (unsigned long) priv; 3174 + priv->listen_timer.function = pn533_listen_mode_timer; 2571 3175 2572 - INIT_WORK(&dev->cmd_work, pn533_wq_cmd); 2573 - INIT_WORK(&dev->cmd_complete_work, pn533_wq_cmd_complete); 2574 - INIT_WORK(&dev->mi_rx_work, pn533_wq_mi_recv); 2575 - INIT_WORK(&dev->mi_tx_work, pn533_wq_mi_send); 2576 - INIT_WORK(&dev->tg_work, pn533_wq_tg_get_data); 2577 - INIT_WORK(&dev->mi_tm_rx_work, pn533_wq_tm_mi_recv); 2578 - INIT_WORK(&dev->mi_tm_tx_work, pn533_wq_tm_mi_send); 2579 - INIT_DELAYED_WORK(&dev->poll_work, pn533_wq_poll); 2580 - INIT_WORK(&dev->rf_work, pn533_wq_rf); 2581 - dev->wq = alloc_ordered_workqueue("pn533", 0); 2582 - if (dev->wq == NULL) 2583 - goto error; 3176 + skb_queue_head_init(&priv->resp_q); 3177 + skb_queue_head_init(&priv->fragment_skb); 2584 3178 2585 - init_timer(&dev->listen_timer); 2586 - dev->listen_timer.data = (unsigned long) dev; 2587 - dev->listen_timer.function = pn533_listen_mode_timer; 2588 - 2589 - skb_queue_head_init(&dev->resp_q); 2590 - skb_queue_head_init(&dev->fragment_skb); 2591 - 2592 - INIT_LIST_HEAD(&dev->cmd_queue); 2593 - 2594 - usb_set_intfdata(interface, dev); 2595 - 2596 - dev->ops = &pn533_std_frame_ops; 2597 - 2598 - dev->protocol_type = PN533_PROTO_REQ_ACK_RESP; 2599 - dev->device_type = id->driver_info; 2600 - switch (dev->device_type) { 2601 - case PN533_DEVICE_STD: 2602 - protocols = PN533_ALL_PROTOCOLS; 2603 - break; 2604 - 2605 - case PN533_DEVICE_PASORI: 2606 - protocols = PN533_NO_TYPE_B_PROTOCOLS; 2607 - break; 2608 - 2609 - case PN533_DEVICE_ACR122U: 2610 - protocols = PN533_NO_TYPE_B_PROTOCOLS; 2611 - dev->ops = &pn533_acr122_frame_ops; 2612 - dev->protocol_type = PN533_PROTO_REQ_RESP, 2613 - 2614 - rc = pn533_acr122_poweron_rdr(dev); 2615 - if (rc < 0) { 2616 - nfc_err(&dev->interface->dev, 2617 - "Couldn't poweron the reader (error %d)\n", rc); 2618 - goto destroy_wq; 2619 - } 2620 - break; 2621 - 2622 - default: 2623 - nfc_err(&dev->interface->dev, "Unknown device type %d\n", 2624 - dev->device_type); 2625 - rc = -EINVAL; 2626 - goto destroy_wq; 2627 - } 3179 + INIT_LIST_HEAD(&priv->cmd_queue); 2628 3180 2629 3181 memset(&fw_ver, 0, sizeof(fw_ver)); 2630 - rc = pn533_get_firmware_version(dev, &fw_ver); 3182 + rc = pn533_get_firmware_version(priv, &fw_ver); 2631 3183 if (rc < 0) 2632 3184 goto destroy_wq; 2633 3185 2634 - nfc_info(&dev->interface->dev, 2635 - "NXP PN5%02X firmware ver %d.%d now attached\n", 3186 + nfc_info(dev, "NXP PN5%02X firmware ver %d.%d now attached\n", 2636 3187 fw_ver.ic, fw_ver.ver, fw_ver.rev); 2637 3188 2638 3189 2639 - dev->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols, 2640 - dev->ops->tx_header_len + 3190 + priv->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols, 3191 + priv->ops->tx_header_len + 2641 3192 PN533_CMD_DATAEXCH_HEAD_LEN, 2642 - dev->ops->tx_tail_len); 2643 - if (!dev->nfc_dev) { 3193 + priv->ops->tx_tail_len); 3194 + if (!priv->nfc_dev) { 2644 3195 rc = -ENOMEM; 2645 3196 goto destroy_wq; 2646 3197 } 2647 3198 2648 - nfc_set_parent_dev(dev->nfc_dev, &interface->dev); 2649 - nfc_set_drvdata(dev->nfc_dev, dev); 3199 + nfc_set_drvdata(priv->nfc_dev, priv); 2650 3200 2651 - rc = nfc_register_device(dev->nfc_dev); 3201 + rc = nfc_register_device(priv->nfc_dev); 2652 3202 if (rc) 2653 3203 goto free_nfc_dev; 2654 3204 2655 - rc = pn533_setup(dev); 3205 + rc = pn533_setup(priv); 2656 3206 if (rc) 2657 3207 goto unregister_nfc_dev; 2658 3208 2659 - return 0; 3209 + return priv; 2660 3210 2661 3211 unregister_nfc_dev: 2662 - nfc_unregister_device(dev->nfc_dev); 3212 + nfc_unregister_device(priv->nfc_dev); 2663 3213 2664 3214 free_nfc_dev: 2665 - nfc_free_device(dev->nfc_dev); 3215 + nfc_free_device(priv->nfc_dev); 2666 3216 2667 3217 destroy_wq: 2668 - destroy_workqueue(dev->wq); 3218 + destroy_workqueue(priv->wq); 2669 3219 error: 2670 - usb_free_urb(dev->in_urb); 2671 - usb_free_urb(dev->out_urb); 2672 - usb_put_dev(dev->udev); 2673 - kfree(dev); 2674 - return rc; 3220 + kfree(priv); 3221 + return ERR_PTR(rc); 2675 3222 } 3223 + EXPORT_SYMBOL_GPL(pn533_register_device); 2676 3224 2677 - static void pn533_disconnect(struct usb_interface *interface) 3225 + void pn533_unregister_device(struct pn533 *priv) 2678 3226 { 2679 - struct pn533 *dev; 2680 3227 struct pn533_cmd *cmd, *n; 2681 3228 2682 - dev = usb_get_intfdata(interface); 2683 - usb_set_intfdata(interface, NULL); 3229 + nfc_unregister_device(priv->nfc_dev); 3230 + nfc_free_device(priv->nfc_dev); 2684 3231 2685 - nfc_unregister_device(dev->nfc_dev); 2686 - nfc_free_device(dev->nfc_dev); 3232 + flush_delayed_work(&priv->poll_work); 3233 + destroy_workqueue(priv->wq); 2687 3234 2688 - usb_kill_urb(dev->in_urb); 2689 - usb_kill_urb(dev->out_urb); 3235 + skb_queue_purge(&priv->resp_q); 2690 3236 2691 - flush_delayed_work(&dev->poll_work); 2692 - destroy_workqueue(dev->wq); 3237 + del_timer(&priv->listen_timer); 2693 3238 2694 - skb_queue_purge(&dev->resp_q); 2695 - 2696 - del_timer(&dev->listen_timer); 2697 - 2698 - list_for_each_entry_safe(cmd, n, &dev->cmd_queue, queue) { 3239 + list_for_each_entry_safe(cmd, n, &priv->cmd_queue, queue) { 2699 3240 list_del(&cmd->queue); 2700 3241 kfree(cmd); 2701 3242 } 2702 3243 2703 - usb_free_urb(dev->in_urb); 2704 - usb_free_urb(dev->out_urb); 2705 - kfree(dev); 2706 - 2707 - nfc_info(&interface->dev, "NXP PN533 NFC device disconnected\n"); 3244 + kfree(priv); 2708 3245 } 3246 + EXPORT_SYMBOL_GPL(pn533_unregister_device); 2709 3247 2710 - static struct usb_driver pn533_driver = { 2711 - .name = "pn533", 2712 - .probe = pn533_probe, 2713 - .disconnect = pn533_disconnect, 2714 - .id_table = pn533_table, 2715 - }; 2716 - 2717 - module_usb_driver(pn533_driver); 2718 3248 2719 3249 MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>"); 2720 3250 MODULE_AUTHOR("Aloisio Almeida Jr <aloisio.almeida@openbossa.org>"); 2721 3251 MODULE_AUTHOR("Waldemar Rymarkiewicz <waldemar.rymarkiewicz@tieto.com>"); 2722 - MODULE_DESCRIPTION("PN533 usb driver ver " VERSION); 3252 + MODULE_DESCRIPTION("PN533 driver ver " VERSION); 2723 3253 MODULE_VERSION(VERSION); 2724 3254 MODULE_LICENSE("GPL");
+16
drivers/nfc/pn533/Kconfig
··· 1 + config NFC_PN533 2 + tristate 3 + help 4 + NXP PN533 core driver. 5 + This driver provides core functionality for NXP PN533 NFC devices. 6 + 7 + config NFC_PN533_USB 8 + tristate "NFC PN533 device support (USB)" 9 + depends on USB 10 + select NFC_PN533 11 + ---help--- 12 + This module adds support for the NXP pn533 USB interface. 13 + Select this if your platform is using the USB bus. 14 + 15 + If you choose to build a module, it'll be called pn533_usb. 16 + Say N if unsure.
+7
drivers/nfc/pn533/Makefile
··· 1 + # 2 + # Makefile for PN533 NFC driver 3 + # 4 + pn533_usb-objs = usb.o 5 + 6 + obj-$(CONFIG_NFC_PN533) += pn533.o 7 + obj-$(CONFIG_NFC_PN533_USB) += pn533_usb.o
+235
drivers/nfc/pn533/pn533.h
··· 1 + /* 2 + * Driver for NXP PN533 NFC Chip 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 + #define PN533_DEVICE_STD 0x1 22 + #define PN533_DEVICE_PASORI 0x2 23 + #define PN533_DEVICE_ACR122U 0x3 24 + 25 + #define PN533_ALL_PROTOCOLS (NFC_PROTO_JEWEL_MASK | NFC_PROTO_MIFARE_MASK |\ 26 + NFC_PROTO_FELICA_MASK | NFC_PROTO_ISO14443_MASK |\ 27 + NFC_PROTO_NFC_DEP_MASK |\ 28 + NFC_PROTO_ISO14443_B_MASK) 29 + 30 + #define PN533_NO_TYPE_B_PROTOCOLS (NFC_PROTO_JEWEL_MASK | \ 31 + NFC_PROTO_MIFARE_MASK | \ 32 + NFC_PROTO_FELICA_MASK | \ 33 + NFC_PROTO_ISO14443_MASK | \ 34 + NFC_PROTO_NFC_DEP_MASK) 35 + 36 + /* Standard pn533 frame definitions (standard and extended)*/ 37 + #define PN533_STD_FRAME_HEADER_LEN (sizeof(struct pn533_std_frame) \ 38 + + 2) /* data[0] TFI, data[1] CC */ 39 + #define PN533_STD_FRAME_TAIL_LEN 2 /* data[len] DCS, data[len + 1] postamble*/ 40 + 41 + #define PN533_EXT_FRAME_HEADER_LEN (sizeof(struct pn533_ext_frame) \ 42 + + 2) /* data[0] TFI, data[1] CC */ 43 + 44 + #define PN533_CMD_DATAEXCH_HEAD_LEN 1 45 + #define PN533_CMD_DATAEXCH_DATA_MAXLEN 262 46 + #define PN533_CMD_DATAFRAME_MAXLEN 240 /* max data length (send) */ 47 + 48 + /* 49 + * Max extended frame payload len, excluding TFI and CC 50 + * which are already in PN533_FRAME_HEADER_LEN. 51 + */ 52 + #define PN533_STD_FRAME_MAX_PAYLOAD_LEN 263 53 + 54 + 55 + /* Preamble (1), SoPC (2), ACK Code (2), Postamble (1) */ 56 + #define PN533_STD_FRAME_ACK_SIZE 6 57 + #define PN533_STD_FRAME_CHECKSUM(f) (f->data[f->datalen]) 58 + #define PN533_STD_FRAME_POSTAMBLE(f) (f->data[f->datalen + 1]) 59 + /* Half start code (3), LEN (4) should be 0xffff for extended frame */ 60 + #define PN533_STD_IS_EXTENDED(hdr) ((hdr)->datalen == 0xFF \ 61 + && (hdr)->datalen_checksum == 0xFF) 62 + #define PN533_EXT_FRAME_CHECKSUM(f) (f->data[be16_to_cpu(f->datalen)]) 63 + 64 + /* start of frame */ 65 + #define PN533_STD_FRAME_SOF 0x00FF 66 + 67 + /* standard frame identifier: in/out/error */ 68 + #define PN533_STD_FRAME_IDENTIFIER(f) (f->data[0]) /* TFI */ 69 + #define PN533_STD_FRAME_DIR_OUT 0xD4 70 + #define PN533_STD_FRAME_DIR_IN 0xD5 71 + 72 + /* PN533 Commands */ 73 + #define PN533_FRAME_CMD(f) (f->data[1]) 74 + 75 + #define PN533_CMD_GET_FIRMWARE_VERSION 0x02 76 + #define PN533_CMD_RF_CONFIGURATION 0x32 77 + #define PN533_CMD_IN_DATA_EXCHANGE 0x40 78 + #define PN533_CMD_IN_COMM_THRU 0x42 79 + #define PN533_CMD_IN_LIST_PASSIVE_TARGET 0x4A 80 + #define PN533_CMD_IN_ATR 0x50 81 + #define PN533_CMD_IN_RELEASE 0x52 82 + #define PN533_CMD_IN_JUMP_FOR_DEP 0x56 83 + 84 + #define PN533_CMD_TG_INIT_AS_TARGET 0x8c 85 + #define PN533_CMD_TG_GET_DATA 0x86 86 + #define PN533_CMD_TG_SET_DATA 0x8e 87 + #define PN533_CMD_TG_SET_META_DATA 0x94 88 + #define PN533_CMD_UNDEF 0xff 89 + 90 + #define PN533_CMD_RESPONSE(cmd) (cmd + 1) 91 + 92 + /* PN533 Return codes */ 93 + #define PN533_CMD_RET_MASK 0x3F 94 + #define PN533_CMD_MI_MASK 0x40 95 + #define PN533_CMD_RET_SUCCESS 0x00 96 + 97 + 98 + enum pn533_protocol_type { 99 + PN533_PROTO_REQ_ACK_RESP = 0, 100 + PN533_PROTO_REQ_RESP 101 + }; 102 + 103 + /* Poll modulations */ 104 + enum { 105 + PN533_POLL_MOD_106KBPS_A, 106 + PN533_POLL_MOD_212KBPS_FELICA, 107 + PN533_POLL_MOD_424KBPS_FELICA, 108 + PN533_POLL_MOD_106KBPS_JEWEL, 109 + PN533_POLL_MOD_847KBPS_B, 110 + PN533_LISTEN_MOD, 111 + 112 + __PN533_POLL_MOD_AFTER_LAST, 113 + }; 114 + #define PN533_POLL_MOD_MAX (__PN533_POLL_MOD_AFTER_LAST - 1) 115 + 116 + struct pn533_std_frame { 117 + u8 preamble; 118 + __be16 start_frame; 119 + u8 datalen; 120 + u8 datalen_checksum; 121 + u8 data[]; 122 + } __packed; 123 + 124 + struct pn533_ext_frame { /* Extended Information frame */ 125 + u8 preamble; 126 + __be16 start_frame; 127 + __be16 eif_flag; /* fixed to 0xFFFF */ 128 + __be16 datalen; 129 + u8 datalen_checksum; 130 + u8 data[]; 131 + } __packed; 132 + 133 + struct pn533 { 134 + struct nfc_dev *nfc_dev; 135 + u32 device_type; 136 + enum pn533_protocol_type protocol_type; 137 + 138 + struct sk_buff_head resp_q; 139 + struct sk_buff_head fragment_skb; 140 + 141 + struct workqueue_struct *wq; 142 + struct work_struct cmd_work; 143 + struct work_struct cmd_complete_work; 144 + struct delayed_work poll_work; 145 + struct work_struct mi_rx_work; 146 + struct work_struct mi_tx_work; 147 + struct work_struct mi_tm_rx_work; 148 + struct work_struct mi_tm_tx_work; 149 + struct work_struct tg_work; 150 + struct work_struct rf_work; 151 + 152 + struct list_head cmd_queue; 153 + struct pn533_cmd *cmd; 154 + u8 cmd_pending; 155 + struct mutex cmd_lock; /* protects cmd queue */ 156 + 157 + void *cmd_complete_mi_arg; 158 + void *cmd_complete_dep_arg; 159 + 160 + struct pn533_poll_modulations *poll_mod_active[PN533_POLL_MOD_MAX + 1]; 161 + u8 poll_mod_count; 162 + u8 poll_mod_curr; 163 + u8 poll_dep; 164 + u32 poll_protocols; 165 + u32 listen_protocols; 166 + struct timer_list listen_timer; 167 + int cancel_listen; 168 + 169 + u8 *gb; 170 + size_t gb_len; 171 + 172 + u8 tgt_available_prots; 173 + u8 tgt_active_prot; 174 + u8 tgt_mode; 175 + 176 + struct pn533_frame_ops *ops; 177 + 178 + struct device *dev; 179 + void *phy; 180 + struct pn533_phy_ops *phy_ops; 181 + }; 182 + 183 + typedef int (*pn533_send_async_complete_t) (struct pn533 *dev, void *arg, 184 + struct sk_buff *resp); 185 + 186 + struct pn533_cmd { 187 + struct list_head queue; 188 + u8 code; 189 + int status; 190 + struct sk_buff *req; 191 + struct sk_buff *resp; 192 + pn533_send_async_complete_t complete_cb; 193 + void *complete_cb_context; 194 + }; 195 + 196 + 197 + struct pn533_frame_ops { 198 + void (*tx_frame_init)(void *frame, u8 cmd_code); 199 + void (*tx_frame_finish)(void *frame); 200 + void (*tx_update_payload_len)(void *frame, int len); 201 + int tx_header_len; 202 + int tx_tail_len; 203 + 204 + bool (*rx_is_frame_valid)(void *frame, struct pn533 *dev); 205 + bool (*rx_frame_is_ack)(void *frame); 206 + int (*rx_frame_size)(void *frame); 207 + int rx_header_len; 208 + int rx_tail_len; 209 + 210 + int max_payload_len; 211 + u8 (*get_cmd_code)(void *frame); 212 + }; 213 + 214 + 215 + struct pn533_phy_ops { 216 + int (*send_frame)(struct pn533 *priv, 217 + struct sk_buff *out); 218 + int (*send_ack)(struct pn533 *dev, gfp_t flags); 219 + void (*abort_cmd)(struct pn533 *priv, gfp_t flags); 220 + }; 221 + 222 + 223 + struct pn533 *pn533_register_device(u32 device_type, 224 + u32 protocols, 225 + enum pn533_protocol_type protocol_type, 226 + void *phy, 227 + struct pn533_phy_ops *phy_ops, 228 + struct pn533_frame_ops *fops, 229 + struct device *dev); 230 + 231 + void pn533_unregister_device(struct pn533 *priv); 232 + void pn533_recv_frame(struct pn533 *dev, struct sk_buff *skb, int status); 233 + 234 + bool pn533_rx_frame_is_cmd_response(struct pn533 *dev, void *frame); 235 + bool pn533_rx_frame_is_ack(void *_frame);
+598
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 pn533 *priv; 66 + }; 67 + 68 + static 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 + memcpy(skb_put(skb, urb->actual_length), 79 + urb->transfer_buffer, urb->actual_length); 80 + } 81 + } 82 + 83 + pn533_recv_frame(phy->priv, skb, urb->status); 84 + } 85 + 86 + static 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 + 93 + static 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 + 137 + sched_wq: 138 + queue_work(priv->wq, &priv->cmd_complete_work); 139 + } 140 + 141 + static 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 + 148 + static int pn533_usb_send_ack(struct pn533 *dev, gfp_t flags) 149 + { 150 + struct pn533_usb_phy *phy = dev->phy; 151 + 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 = 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 + 162 + static 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 + 195 + error: 196 + usb_unlink_urb(phy->out_urb); 197 + return rc; 198 + } 199 + 200 + static 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 + 237 + struct 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 + 251 + struct pn533_acr122_apdu_hdr { 252 + u8 class; 253 + u8 ins; 254 + u8 p1; 255 + u8 p2; 256 + } __packed; 257 + 258 + struct 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 + 265 + struct pn533_acr122_rx_frame { 266 + struct pn533_acr122_ccid_hdr ccid; 267 + u8 data[]; /* pn533 frame : TFI ... */ 268 + } __packed; 269 + 270 + static 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 + 293 + static 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 + 300 + static 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 + 307 + static 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 + 323 + static 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 + 331 + static 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 + 338 + static 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 + 354 + struct pn533_acr122_poweron_rdr_arg { 355 + int rc; 356 + struct completion done; 357 + }; 358 + 359 + static 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 + 373 + static 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 + 416 + static 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 + 437 + static 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 + 443 + static 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 + rc = -ENOMEM; 469 + goto out_free_phy; 470 + } 471 + 472 + phy->udev = usb_get_dev(interface_to_usbdev(interface)); 473 + phy->interface = interface; 474 + 475 + iface_desc = interface->cur_altsetting; 476 + for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) { 477 + endpoint = &iface_desc->endpoint[i].desc; 478 + 479 + if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint)) 480 + in_endpoint = endpoint->bEndpointAddress; 481 + 482 + if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint)) 483 + out_endpoint = endpoint->bEndpointAddress; 484 + } 485 + 486 + if (!in_endpoint || !out_endpoint) { 487 + nfc_err(&interface->dev, 488 + "Could not find bulk-in or bulk-out endpoint\n"); 489 + rc = -ENODEV; 490 + goto error; 491 + } 492 + 493 + phy->in_urb = usb_alloc_urb(0, GFP_KERNEL); 494 + phy->out_urb = usb_alloc_urb(0, GFP_KERNEL); 495 + 496 + if (!phy->in_urb || !phy->out_urb) 497 + goto error; 498 + 499 + usb_fill_bulk_urb(phy->in_urb, phy->udev, 500 + usb_rcvbulkpipe(phy->udev, in_endpoint), 501 + in_buf, in_buf_len, NULL, phy); 502 + 503 + usb_fill_bulk_urb(phy->out_urb, phy->udev, 504 + usb_sndbulkpipe(phy->udev, out_endpoint), 505 + NULL, 0, pn533_send_complete, phy); 506 + 507 + 508 + switch (id->driver_info) { 509 + case PN533_DEVICE_STD: 510 + protocols = PN533_ALL_PROTOCOLS; 511 + break; 512 + 513 + case PN533_DEVICE_PASORI: 514 + protocols = PN533_NO_TYPE_B_PROTOCOLS; 515 + break; 516 + 517 + case PN533_DEVICE_ACR122U: 518 + protocols = PN533_NO_TYPE_B_PROTOCOLS; 519 + fops = &pn533_acr122_frame_ops; 520 + protocol_type = PN533_PROTO_REQ_RESP, 521 + 522 + rc = pn533_acr122_poweron_rdr(phy); 523 + if (rc < 0) { 524 + nfc_err(&interface->dev, 525 + "Couldn't poweron the reader (error %d)\n", rc); 526 + goto error; 527 + } 528 + break; 529 + 530 + default: 531 + nfc_err(&interface->dev, "Unknown device type %lu\n", 532 + id->driver_info); 533 + rc = -EINVAL; 534 + goto error; 535 + } 536 + 537 + priv = pn533_register_device(id->driver_info, protocols, protocol_type, 538 + phy, &usb_phy_ops, fops, 539 + &phy->udev->dev); 540 + 541 + if (IS_ERR(priv)) { 542 + rc = PTR_ERR(priv); 543 + goto error; 544 + } 545 + 546 + phy->priv = priv; 547 + nfc_set_parent_dev(priv->nfc_dev, &interface->dev); 548 + 549 + usb_set_intfdata(interface, phy); 550 + 551 + return 0; 552 + 553 + error: 554 + usb_free_urb(phy->in_urb); 555 + usb_free_urb(phy->out_urb); 556 + usb_put_dev(phy->udev); 557 + kfree(in_buf); 558 + out_free_phy: 559 + kfree(phy); 560 + return rc; 561 + } 562 + 563 + static 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 + 584 + static 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 + 591 + module_usb_driver(pn533_usb_driver); 592 + 593 + MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>"); 594 + MODULE_AUTHOR("Aloisio Almeida Jr <aloisio.almeida@openbossa.org>"); 595 + MODULE_AUTHOR("Waldemar Rymarkiewicz <waldemar.rymarkiewicz@tieto.com>"); 596 + MODULE_DESCRIPTION("PN533 USB driver ver " VERSION); 597 + MODULE_VERSION(VERSION); 598 + MODULE_LICENSE("GPL");