forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pn533.c
2525 lines (1981 loc) · 58.4 KB
/
pn533.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2011 Instituto Nokia de Tecnologia
*
* Authors:
* Lauro Ramos Venancio <[email protected]>
* Aloisio Almeida Jr <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <linux/device.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/usb.h>
#include <linux/nfc.h>
#include <linux/netdevice.h>
#include <net/nfc/nfc.h>
#define VERSION "0.1"
#define PN533_VENDOR_ID 0x4CC
#define PN533_PRODUCT_ID 0x2533
#define SCM_VENDOR_ID 0x4E6
#define SCL3711_PRODUCT_ID 0x5591
#define SONY_VENDOR_ID 0x054c
#define PASORI_PRODUCT_ID 0x02e1
#define PN533_QUIRKS_TYPE_A BIT(0)
#define PN533_QUIRKS_TYPE_F BIT(1)
#define PN533_QUIRKS_DEP BIT(2)
#define PN533_QUIRKS_RAW_EXCHANGE BIT(3)
#define PN533_DEVICE_STD 0x1
#define PN533_DEVICE_PASORI 0x2
#define PN533_ALL_PROTOCOLS (NFC_PROTO_JEWEL_MASK | NFC_PROTO_MIFARE_MASK |\
NFC_PROTO_FELICA_MASK | NFC_PROTO_ISO14443_MASK |\
NFC_PROTO_NFC_DEP_MASK |\
NFC_PROTO_ISO14443_B_MASK)
#define PN533_NO_TYPE_B_PROTOCOLS (NFC_PROTO_JEWEL_MASK | \
NFC_PROTO_MIFARE_MASK | \
NFC_PROTO_FELICA_MASK | \
NFC_PROTO_ISO14443_MASK | \
NFC_PROTO_NFC_DEP_MASK)
static const struct usb_device_id pn533_table[] = {
{ .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
.idVendor = PN533_VENDOR_ID,
.idProduct = PN533_PRODUCT_ID,
.driver_info = PN533_DEVICE_STD,
},
{ .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
.idVendor = SCM_VENDOR_ID,
.idProduct = SCL3711_PRODUCT_ID,
.driver_info = PN533_DEVICE_STD,
},
{ .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
.idVendor = SONY_VENDOR_ID,
.idProduct = PASORI_PRODUCT_ID,
.driver_info = PN533_DEVICE_PASORI,
},
{ }
};
MODULE_DEVICE_TABLE(usb, pn533_table);
/* How much time we spend listening for initiators */
#define PN533_LISTEN_TIME 2
/* frame definitions */
#define PN533_FRAME_TAIL_SIZE 2
#define PN533_FRAME_SIZE(f) (sizeof(struct pn533_frame) + f->datalen + \
PN533_FRAME_TAIL_SIZE)
#define PN533_FRAME_ACK_SIZE (sizeof(struct pn533_frame) + 1)
#define PN533_FRAME_CHECKSUM(f) (f->data[f->datalen])
#define PN533_FRAME_POSTAMBLE(f) (f->data[f->datalen + 1])
/* start of frame */
#define PN533_SOF 0x00FF
/* frame identifier: in/out/error */
#define PN533_FRAME_IDENTIFIER(f) (f->data[0])
#define PN533_DIR_OUT 0xD4
#define PN533_DIR_IN 0xD5
/* PN533 Commands */
#define PN533_FRAME_CMD(f) (f->data[1])
#define PN533_FRAME_CMD_PARAMS_PTR(f) (&f->data[2])
#define PN533_FRAME_CMD_PARAMS_LEN(f) (f->datalen - 2)
#define PN533_CMD_GET_FIRMWARE_VERSION 0x02
#define PN533_CMD_RF_CONFIGURATION 0x32
#define PN533_CMD_IN_DATA_EXCHANGE 0x40
#define PN533_CMD_IN_COMM_THRU 0x42
#define PN533_CMD_IN_LIST_PASSIVE_TARGET 0x4A
#define PN533_CMD_IN_ATR 0x50
#define PN533_CMD_IN_RELEASE 0x52
#define PN533_CMD_IN_JUMP_FOR_DEP 0x56
#define PN533_CMD_TG_INIT_AS_TARGET 0x8c
#define PN533_CMD_TG_GET_DATA 0x86
#define PN533_CMD_TG_SET_DATA 0x8e
#define PN533_CMD_RESPONSE(cmd) (cmd + 1)
/* PN533 Return codes */
#define PN533_CMD_RET_MASK 0x3F
#define PN533_CMD_MI_MASK 0x40
#define PN533_CMD_RET_SUCCESS 0x00
/* PN533 status codes */
#define PN533_STATUS_TARGET_RELEASED 0x29
struct pn533;
typedef int (*pn533_cmd_complete_t) (struct pn533 *dev, void *arg,
u8 *params, int params_len);
/* structs for pn533 commands */
/* PN533_CMD_GET_FIRMWARE_VERSION */
struct pn533_fw_version {
u8 ic;
u8 ver;
u8 rev;
u8 support;
};
/* PN533_CMD_RF_CONFIGURATION */
#define PN533_CFGITEM_TIMING 0x02
#define PN533_CFGITEM_MAX_RETRIES 0x05
#define PN533_CFGITEM_PASORI 0x82
#define PN533_CONFIG_TIMING_102 0xb
#define PN533_CONFIG_TIMING_204 0xc
#define PN533_CONFIG_TIMING_409 0xd
#define PN533_CONFIG_TIMING_819 0xe
#define PN533_CONFIG_MAX_RETRIES_NO_RETRY 0x00
#define PN533_CONFIG_MAX_RETRIES_ENDLESS 0xFF
struct pn533_config_max_retries {
u8 mx_rty_atr;
u8 mx_rty_psl;
u8 mx_rty_passive_act;
} __packed;
struct pn533_config_timing {
u8 rfu;
u8 atr_res_timeout;
u8 dep_timeout;
} __packed;
/* PN533_CMD_IN_LIST_PASSIVE_TARGET */
/* felica commands opcode */
#define PN533_FELICA_OPC_SENSF_REQ 0
#define PN533_FELICA_OPC_SENSF_RES 1
/* felica SENSF_REQ parameters */
#define PN533_FELICA_SENSF_SC_ALL 0xFFFF
#define PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE 0
#define PN533_FELICA_SENSF_RC_SYSTEM_CODE 1
#define PN533_FELICA_SENSF_RC_ADVANCED_PROTOCOL 2
/* type B initiator_data values */
#define PN533_TYPE_B_AFI_ALL_FAMILIES 0
#define PN533_TYPE_B_POLL_METHOD_TIMESLOT 0
#define PN533_TYPE_B_POLL_METHOD_PROBABILISTIC 1
union pn533_cmd_poll_initdata {
struct {
u8 afi;
u8 polling_method;
} __packed type_b;
struct {
u8 opcode;
__be16 sc;
u8 rc;
u8 tsn;
} __packed felica;
};
/* Poll modulations */
enum {
PN533_POLL_MOD_106KBPS_A,
PN533_POLL_MOD_212KBPS_FELICA,
PN533_POLL_MOD_424KBPS_FELICA,
PN533_POLL_MOD_106KBPS_JEWEL,
PN533_POLL_MOD_847KBPS_B,
PN533_LISTEN_MOD,
__PN533_POLL_MOD_AFTER_LAST,
};
#define PN533_POLL_MOD_MAX (__PN533_POLL_MOD_AFTER_LAST - 1)
struct pn533_poll_modulations {
struct {
u8 maxtg;
u8 brty;
union pn533_cmd_poll_initdata initiator_data;
} __packed data;
u8 len;
};
const struct pn533_poll_modulations poll_mod[] = {
[PN533_POLL_MOD_106KBPS_A] = {
.data = {
.maxtg = 1,
.brty = 0,
},
.len = 2,
},
[PN533_POLL_MOD_212KBPS_FELICA] = {
.data = {
.maxtg = 1,
.brty = 1,
.initiator_data.felica = {
.opcode = PN533_FELICA_OPC_SENSF_REQ,
.sc = PN533_FELICA_SENSF_SC_ALL,
.rc = PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE,
.tsn = 0,
},
},
.len = 7,
},
[PN533_POLL_MOD_424KBPS_FELICA] = {
.data = {
.maxtg = 1,
.brty = 2,
.initiator_data.felica = {
.opcode = PN533_FELICA_OPC_SENSF_REQ,
.sc = PN533_FELICA_SENSF_SC_ALL,
.rc = PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE,
.tsn = 0,
},
},
.len = 7,
},
[PN533_POLL_MOD_106KBPS_JEWEL] = {
.data = {
.maxtg = 1,
.brty = 4,
},
.len = 2,
},
[PN533_POLL_MOD_847KBPS_B] = {
.data = {
.maxtg = 1,
.brty = 8,
.initiator_data.type_b = {
.afi = PN533_TYPE_B_AFI_ALL_FAMILIES,
.polling_method =
PN533_TYPE_B_POLL_METHOD_TIMESLOT,
},
},
.len = 3,
},
[PN533_LISTEN_MOD] = {
.len = 0,
},
};
/* PN533_CMD_IN_ATR */
struct pn533_cmd_activate_param {
u8 tg;
u8 next;
} __packed;
struct pn533_cmd_activate_response {
u8 status;
u8 nfcid3t[10];
u8 didt;
u8 bst;
u8 brt;
u8 to;
u8 ppt;
/* optional */
u8 gt[];
} __packed;
/* PN533_CMD_IN_JUMP_FOR_DEP */
struct pn533_cmd_jump_dep {
u8 active;
u8 baud;
u8 next;
u8 data[];
} __packed;
struct pn533_cmd_jump_dep_response {
u8 status;
u8 tg;
u8 nfcid3t[10];
u8 didt;
u8 bst;
u8 brt;
u8 to;
u8 ppt;
/* optional */
u8 gt[];
} __packed;
/* PN533_TG_INIT_AS_TARGET */
#define PN533_INIT_TARGET_PASSIVE 0x1
#define PN533_INIT_TARGET_DEP 0x2
#define PN533_INIT_TARGET_RESP_FRAME_MASK 0x3
#define PN533_INIT_TARGET_RESP_ACTIVE 0x1
#define PN533_INIT_TARGET_RESP_DEP 0x4
struct pn533_cmd_init_target {
u8 mode;
u8 mifare[6];
u8 felica[18];
u8 nfcid3[10];
u8 gb_len;
u8 gb[];
} __packed;
struct pn533_cmd_init_target_response {
u8 mode;
u8 cmd[];
} __packed;
struct pn533 {
struct usb_device *udev;
struct usb_interface *interface;
struct nfc_dev *nfc_dev;
struct urb *out_urb;
int out_maxlen;
struct pn533_frame *out_frame;
struct urb *in_urb;
int in_maxlen;
struct pn533_frame *in_frame;
struct sk_buff_head resp_q;
struct workqueue_struct *wq;
struct work_struct cmd_work;
struct work_struct cmd_complete_work;
struct work_struct poll_work;
struct work_struct mi_work;
struct work_struct tg_work;
struct timer_list listen_timer;
struct pn533_frame *wq_in_frame;
int wq_in_error;
int cancel_listen;
pn533_cmd_complete_t cmd_complete;
void *cmd_complete_arg;
struct mutex cmd_lock;
u8 cmd;
struct pn533_poll_modulations *poll_mod_active[PN533_POLL_MOD_MAX + 1];
u8 poll_mod_count;
u8 poll_mod_curr;
u32 poll_protocols;
u32 listen_protocols;
u8 *gb;
size_t gb_len;
u8 tgt_available_prots;
u8 tgt_active_prot;
u8 tgt_mode;
u32 device_type;
struct list_head cmd_queue;
u8 cmd_pending;
};
struct pn533_cmd {
struct list_head queue;
struct pn533_frame *out_frame;
struct pn533_frame *in_frame;
int in_frame_len;
pn533_cmd_complete_t cmd_complete;
void *arg;
gfp_t flags;
};
struct pn533_frame {
u8 preamble;
__be16 start_frame;
u8 datalen;
u8 datalen_checksum;
u8 data[];
} __packed;
/* The rule: value + checksum = 0 */
static inline u8 pn533_checksum(u8 value)
{
return ~value + 1;
}
/* The rule: sum(data elements) + checksum = 0 */
static u8 pn533_data_checksum(u8 *data, int datalen)
{
u8 sum = 0;
int i;
for (i = 0; i < datalen; i++)
sum += data[i];
return pn533_checksum(sum);
}
/**
* pn533_tx_frame_ack - create a ack frame
* @frame: The frame to be set as ack
*
* Ack is different type of standard frame. As a standard frame, it has
* preamble and start_frame. However the checksum of this frame must fail,
* i.e. datalen + datalen_checksum must NOT be zero. When the checksum test
* fails and datalen = 0 and datalen_checksum = 0xFF, the frame is a ack.
* After datalen_checksum field, the postamble is placed.
*/
static void pn533_tx_frame_ack(struct pn533_frame *frame)
{
frame->preamble = 0;
frame->start_frame = cpu_to_be16(PN533_SOF);
frame->datalen = 0;
frame->datalen_checksum = 0xFF;
/* data[0] is used as postamble */
frame->data[0] = 0;
}
static void pn533_tx_frame_init(struct pn533_frame *frame, u8 cmd)
{
frame->preamble = 0;
frame->start_frame = cpu_to_be16(PN533_SOF);
PN533_FRAME_IDENTIFIER(frame) = PN533_DIR_OUT;
PN533_FRAME_CMD(frame) = cmd;
frame->datalen = 2;
}
static void pn533_tx_frame_finish(struct pn533_frame *frame)
{
frame->datalen_checksum = pn533_checksum(frame->datalen);
PN533_FRAME_CHECKSUM(frame) =
pn533_data_checksum(frame->data, frame->datalen);
PN533_FRAME_POSTAMBLE(frame) = 0;
}
static bool pn533_rx_frame_is_valid(struct pn533_frame *frame)
{
u8 checksum;
if (frame->start_frame != cpu_to_be16(PN533_SOF))
return false;
checksum = pn533_checksum(frame->datalen);
if (checksum != frame->datalen_checksum)
return false;
checksum = pn533_data_checksum(frame->data, frame->datalen);
if (checksum != PN533_FRAME_CHECKSUM(frame))
return false;
return true;
}
static bool pn533_rx_frame_is_ack(struct pn533_frame *frame)
{
if (frame->start_frame != cpu_to_be16(PN533_SOF))
return false;
if (frame->datalen != 0 || frame->datalen_checksum != 0xFF)
return false;
return true;
}
static bool pn533_rx_frame_is_cmd_response(struct pn533_frame *frame, u8 cmd)
{
return (PN533_FRAME_CMD(frame) == PN533_CMD_RESPONSE(cmd));
}
static void pn533_wq_cmd_complete(struct work_struct *work)
{
struct pn533 *dev = container_of(work, struct pn533, cmd_complete_work);
struct pn533_frame *in_frame;
int rc;
in_frame = dev->wq_in_frame;
if (dev->wq_in_error)
rc = dev->cmd_complete(dev, dev->cmd_complete_arg, NULL,
dev->wq_in_error);
else
rc = dev->cmd_complete(dev, dev->cmd_complete_arg,
PN533_FRAME_CMD_PARAMS_PTR(in_frame),
PN533_FRAME_CMD_PARAMS_LEN(in_frame));
if (rc != -EINPROGRESS)
queue_work(dev->wq, &dev->cmd_work);
}
static void pn533_recv_response(struct urb *urb)
{
struct pn533 *dev = urb->context;
struct pn533_frame *in_frame;
dev->wq_in_frame = NULL;
switch (urb->status) {
case 0:
/* success */
break;
case -ECONNRESET:
case -ENOENT:
case -ESHUTDOWN:
nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
" status: %d", urb->status);
dev->wq_in_error = urb->status;
goto sched_wq;
default:
nfc_dev_err(&dev->interface->dev, "Nonzero urb status received:"
" %d", urb->status);
dev->wq_in_error = urb->status;
goto sched_wq;
}
in_frame = dev->in_urb->transfer_buffer;
if (!pn533_rx_frame_is_valid(in_frame)) {
nfc_dev_err(&dev->interface->dev, "Received an invalid frame");
dev->wq_in_error = -EIO;
goto sched_wq;
}
if (!pn533_rx_frame_is_cmd_response(in_frame, dev->cmd)) {
nfc_dev_err(&dev->interface->dev, "The received frame is not "
"response to the last command");
dev->wq_in_error = -EIO;
goto sched_wq;
}
nfc_dev_dbg(&dev->interface->dev, "Received a valid frame");
dev->wq_in_error = 0;
dev->wq_in_frame = in_frame;
sched_wq:
queue_work(dev->wq, &dev->cmd_complete_work);
}
static int pn533_submit_urb_for_response(struct pn533 *dev, gfp_t flags)
{
dev->in_urb->complete = pn533_recv_response;
return usb_submit_urb(dev->in_urb, flags);
}
static void pn533_recv_ack(struct urb *urb)
{
struct pn533 *dev = urb->context;
struct pn533_frame *in_frame;
int rc;
switch (urb->status) {
case 0:
/* success */
break;
case -ECONNRESET:
case -ENOENT:
case -ESHUTDOWN:
nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
" status: %d", urb->status);
dev->wq_in_error = urb->status;
goto sched_wq;
default:
nfc_dev_err(&dev->interface->dev, "Nonzero urb status received:"
" %d", urb->status);
dev->wq_in_error = urb->status;
goto sched_wq;
}
in_frame = dev->in_urb->transfer_buffer;
if (!pn533_rx_frame_is_ack(in_frame)) {
nfc_dev_err(&dev->interface->dev, "Received an invalid ack");
dev->wq_in_error = -EIO;
goto sched_wq;
}
nfc_dev_dbg(&dev->interface->dev, "Received a valid ack");
rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC);
if (rc) {
nfc_dev_err(&dev->interface->dev, "usb_submit_urb failed with"
" result %d", rc);
dev->wq_in_error = rc;
goto sched_wq;
}
return;
sched_wq:
dev->wq_in_frame = NULL;
queue_work(dev->wq, &dev->cmd_complete_work);
}
static int pn533_submit_urb_for_ack(struct pn533 *dev, gfp_t flags)
{
dev->in_urb->complete = pn533_recv_ack;
return usb_submit_urb(dev->in_urb, flags);
}
static int pn533_send_ack(struct pn533 *dev, gfp_t flags)
{
int rc;
nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
pn533_tx_frame_ack(dev->out_frame);
dev->out_urb->transfer_buffer = dev->out_frame;
dev->out_urb->transfer_buffer_length = PN533_FRAME_ACK_SIZE;
rc = usb_submit_urb(dev->out_urb, flags);
return rc;
}
static int __pn533_send_cmd_frame_async(struct pn533 *dev,
struct pn533_frame *out_frame,
struct pn533_frame *in_frame,
int in_frame_len,
pn533_cmd_complete_t cmd_complete,
void *arg, gfp_t flags)
{
int rc;
nfc_dev_dbg(&dev->interface->dev, "Sending command 0x%x",
PN533_FRAME_CMD(out_frame));
dev->cmd = PN533_FRAME_CMD(out_frame);
dev->cmd_complete = cmd_complete;
dev->cmd_complete_arg = arg;
dev->out_urb->transfer_buffer = out_frame;
dev->out_urb->transfer_buffer_length =
PN533_FRAME_SIZE(out_frame);
dev->in_urb->transfer_buffer = in_frame;
dev->in_urb->transfer_buffer_length = in_frame_len;
rc = usb_submit_urb(dev->out_urb, flags);
if (rc)
return rc;
rc = pn533_submit_urb_for_ack(dev, flags);
if (rc)
goto error;
return 0;
error:
usb_unlink_urb(dev->out_urb);
return rc;
}
static void pn533_wq_cmd(struct work_struct *work)
{
struct pn533 *dev = container_of(work, struct pn533, cmd_work);
struct pn533_cmd *cmd;
mutex_lock(&dev->cmd_lock);
if (list_empty(&dev->cmd_queue)) {
dev->cmd_pending = 0;
mutex_unlock(&dev->cmd_lock);
return;
}
cmd = list_first_entry(&dev->cmd_queue, struct pn533_cmd, queue);
mutex_unlock(&dev->cmd_lock);
__pn533_send_cmd_frame_async(dev, cmd->out_frame, cmd->in_frame,
cmd->in_frame_len, cmd->cmd_complete,
cmd->arg, cmd->flags);
list_del(&cmd->queue);
kfree(cmd);
}
static int pn533_send_cmd_frame_async(struct pn533 *dev,
struct pn533_frame *out_frame,
struct pn533_frame *in_frame,
int in_frame_len,
pn533_cmd_complete_t cmd_complete,
void *arg, gfp_t flags)
{
struct pn533_cmd *cmd;
int rc = 0;
nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
mutex_lock(&dev->cmd_lock);
if (!dev->cmd_pending) {
rc = __pn533_send_cmd_frame_async(dev, out_frame, in_frame,
in_frame_len, cmd_complete,
arg, flags);
if (!rc)
dev->cmd_pending = 1;
goto unlock;
}
nfc_dev_dbg(&dev->interface->dev, "%s Queueing command", __func__);
cmd = kzalloc(sizeof(struct pn533_cmd), flags);
if (!cmd) {
rc = -ENOMEM;
goto unlock;
}
INIT_LIST_HEAD(&cmd->queue);
cmd->out_frame = out_frame;
cmd->in_frame = in_frame;
cmd->in_frame_len = in_frame_len;
cmd->cmd_complete = cmd_complete;
cmd->arg = arg;
cmd->flags = flags;
list_add_tail(&cmd->queue, &dev->cmd_queue);
unlock:
mutex_unlock(&dev->cmd_lock);
return rc;
}
struct pn533_sync_cmd_response {
int rc;
struct completion done;
};
static int pn533_sync_cmd_complete(struct pn533 *dev, void *_arg,
u8 *params, int params_len)
{
struct pn533_sync_cmd_response *arg = _arg;
nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
arg->rc = 0;
if (params_len < 0) /* error */
arg->rc = params_len;
complete(&arg->done);
return 0;
}
static int pn533_send_cmd_frame_sync(struct pn533 *dev,
struct pn533_frame *out_frame,
struct pn533_frame *in_frame,
int in_frame_len)
{
int rc;
struct pn533_sync_cmd_response arg;
nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
init_completion(&arg.done);
rc = pn533_send_cmd_frame_async(dev, out_frame, in_frame, in_frame_len,
pn533_sync_cmd_complete, &arg, GFP_KERNEL);
if (rc)
return rc;
wait_for_completion(&arg.done);
return arg.rc;
}
static void pn533_send_complete(struct urb *urb)
{
struct pn533 *dev = urb->context;
nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
switch (urb->status) {
case 0:
/* success */
break;
case -ECONNRESET:
case -ENOENT:
case -ESHUTDOWN:
nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
" status: %d", urb->status);
break;
default:
nfc_dev_dbg(&dev->interface->dev, "Nonzero urb status received:"
" %d", urb->status);
}
}
struct pn533_target_type_a {
__be16 sens_res;
u8 sel_res;
u8 nfcid_len;
u8 nfcid_data[];
} __packed;
#define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6))
#define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0))
#define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8))
#define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
#define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C
#define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
#define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)
#define PN533_TYPE_A_SEL_PROT_MIFARE 0
#define PN533_TYPE_A_SEL_PROT_ISO14443 1
#define PN533_TYPE_A_SEL_PROT_DEP 2
#define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3
static bool pn533_target_type_a_is_valid(struct pn533_target_type_a *type_a,
int target_data_len)
{
u8 ssd;
u8 platconf;
if (target_data_len < sizeof(struct pn533_target_type_a))
return false;
/* The lenght check of nfcid[] and ats[] are not being performed because
the values are not being used */
/* Requirement 4.6.3.3 from NFC Forum Digital Spec */
ssd = PN533_TYPE_A_SENS_RES_SSD(type_a->sens_res);
platconf = PN533_TYPE_A_SENS_RES_PLATCONF(type_a->sens_res);
if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
(ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
return false;
/* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
if (PN533_TYPE_A_SEL_CASCADE(type_a->sel_res) != 0)
return false;
return true;
}
static int pn533_target_found_type_a(struct nfc_target *nfc_tgt, u8 *tgt_data,
int tgt_data_len)
{
struct pn533_target_type_a *tgt_type_a;
tgt_type_a = (struct pn533_target_type_a *) tgt_data;
if (!pn533_target_type_a_is_valid(tgt_type_a, tgt_data_len))
return -EPROTO;
switch (PN533_TYPE_A_SEL_PROT(tgt_type_a->sel_res)) {
case PN533_TYPE_A_SEL_PROT_MIFARE:
nfc_tgt->supported_protocols = NFC_PROTO_MIFARE_MASK;
break;
case PN533_TYPE_A_SEL_PROT_ISO14443:
nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK;
break;
case PN533_TYPE_A_SEL_PROT_DEP:
nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
break;
case PN533_TYPE_A_SEL_PROT_ISO14443_DEP:
nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK |
NFC_PROTO_NFC_DEP_MASK;
break;
}
nfc_tgt->sens_res = be16_to_cpu(tgt_type_a->sens_res);
nfc_tgt->sel_res = tgt_type_a->sel_res;
nfc_tgt->nfcid1_len = tgt_type_a->nfcid_len;
memcpy(nfc_tgt->nfcid1, tgt_type_a->nfcid_data, nfc_tgt->nfcid1_len);
return 0;
}
struct pn533_target_felica {
u8 pol_res;
u8 opcode;
u8 nfcid2[8];
u8 pad[8];
/* optional */
u8 syst_code[];
} __packed;
#define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
#define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
static bool pn533_target_felica_is_valid(struct pn533_target_felica *felica,
int target_data_len)
{
if (target_data_len < sizeof(struct pn533_target_felica))
return false;
if (felica->opcode != PN533_FELICA_OPC_SENSF_RES)
return false;
return true;
}
static int pn533_target_found_felica(struct nfc_target *nfc_tgt, u8 *tgt_data,
int tgt_data_len)
{
struct pn533_target_felica *tgt_felica;
tgt_felica = (struct pn533_target_felica *) tgt_data;
if (!pn533_target_felica_is_valid(tgt_felica, tgt_data_len))
return -EPROTO;
if (tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1 &&
tgt_felica->nfcid2[1] ==
PN533_FELICA_SENSF_NFCID2_DEP_B2)
nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
else
nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK;
memcpy(nfc_tgt->sensf_res, &tgt_felica->opcode, 9);
nfc_tgt->sensf_res_len = 9;
return 0;
}
struct pn533_target_jewel {
__be16 sens_res;
u8 jewelid[4];
} __packed;
static bool pn533_target_jewel_is_valid(struct pn533_target_jewel *jewel,
int target_data_len)
{
u8 ssd;
u8 platconf;
if (target_data_len < sizeof(struct pn533_target_jewel))
return false;
/* Requirement 4.6.3.3 from NFC Forum Digital Spec */
ssd = PN533_TYPE_A_SENS_RES_SSD(jewel->sens_res);
platconf = PN533_TYPE_A_SENS_RES_PLATCONF(jewel->sens_res);
if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
(ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
return false;
return true;
}
static int pn533_target_found_jewel(struct nfc_target *nfc_tgt, u8 *tgt_data,
int tgt_data_len)
{
struct pn533_target_jewel *tgt_jewel;
tgt_jewel = (struct pn533_target_jewel *) tgt_data;
if (!pn533_target_jewel_is_valid(tgt_jewel, tgt_data_len))
return -EPROTO;
nfc_tgt->supported_protocols = NFC_PROTO_JEWEL_MASK;
nfc_tgt->sens_res = be16_to_cpu(tgt_jewel->sens_res);
nfc_tgt->nfcid1_len = 4;
memcpy(nfc_tgt->nfcid1, tgt_jewel->jewelid, nfc_tgt->nfcid1_len);
return 0;