forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
port100.c
1652 lines (1333 loc) · 42.1 KB
/
port100.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
// SPDX-License-Identifier: GPL-2.0-only
/*
* Sony NFC Port-100 Series driver
* Copyright (c) 2013, Intel Corporation.
*
* Partly based/Inspired by Stephen Tiedemann's nfcpy
*/
#include <linux/module.h>
#include <linux/usb.h>
#include <net/nfc/digital.h>
#define VERSION "0.1"
#define SONY_VENDOR_ID 0x054c
#define RCS380S_PRODUCT_ID 0x06c1
#define RCS380P_PRODUCT_ID 0x06c3
#define PORT100_PROTOCOLS (NFC_PROTO_JEWEL_MASK | \
NFC_PROTO_MIFARE_MASK | \
NFC_PROTO_FELICA_MASK | \
NFC_PROTO_NFC_DEP_MASK | \
NFC_PROTO_ISO14443_MASK | \
NFC_PROTO_ISO14443_B_MASK)
#define PORT100_CAPABILITIES (NFC_DIGITAL_DRV_CAPS_IN_CRC | \
NFC_DIGITAL_DRV_CAPS_TG_CRC)
/* Standard port100 frame definitions */
#define PORT100_FRAME_HEADER_LEN (sizeof(struct port100_frame) \
+ 2) /* data[0] CC, data[1] SCC */
#define PORT100_FRAME_TAIL_LEN 2 /* data[len] DCS, data[len + 1] postamble*/
#define PORT100_COMM_RF_HEAD_MAX_LEN (sizeof(struct port100_tg_comm_rf_cmd))
/*
* Max extended frame payload len, excluding CC and SCC
* which are already in PORT100_FRAME_HEADER_LEN.
*/
#define PORT100_FRAME_MAX_PAYLOAD_LEN 1001
#define PORT100_FRAME_ACK_SIZE 6 /* Preamble (1), SoPC (2), ACK Code (2),
Postamble (1) */
static u8 ack_frame[PORT100_FRAME_ACK_SIZE] = {
0x00, 0x00, 0xff, 0x00, 0xff, 0x00
};
#define PORT100_FRAME_CHECKSUM(f) (f->data[le16_to_cpu(f->datalen)])
#define PORT100_FRAME_POSTAMBLE(f) (f->data[le16_to_cpu(f->datalen) + 1])
/* start of frame */
#define PORT100_FRAME_SOF 0x00FF
#define PORT100_FRAME_EXT 0xFFFF
#define PORT100_FRAME_ACK 0x00FF
/* Port-100 command: in or out */
#define PORT100_FRAME_DIRECTION(f) (f->data[0]) /* CC */
#define PORT100_FRAME_DIR_OUT 0xD6
#define PORT100_FRAME_DIR_IN 0xD7
/* Port-100 sub-command */
#define PORT100_FRAME_CMD(f) (f->data[1]) /* SCC */
#define PORT100_CMD_GET_FIRMWARE_VERSION 0x20
#define PORT100_CMD_GET_COMMAND_TYPE 0x28
#define PORT100_CMD_SET_COMMAND_TYPE 0x2A
#define PORT100_CMD_IN_SET_RF 0x00
#define PORT100_CMD_IN_SET_PROTOCOL 0x02
#define PORT100_CMD_IN_COMM_RF 0x04
#define PORT100_CMD_TG_SET_RF 0x40
#define PORT100_CMD_TG_SET_PROTOCOL 0x42
#define PORT100_CMD_TG_SET_RF_OFF 0x46
#define PORT100_CMD_TG_COMM_RF 0x48
#define PORT100_CMD_SWITCH_RF 0x06
#define PORT100_CMD_RESPONSE(cmd) (cmd + 1)
#define PORT100_CMD_TYPE_IS_SUPPORTED(mask, cmd_type) \
((mask) & (0x01 << (cmd_type)))
#define PORT100_CMD_TYPE_0 0
#define PORT100_CMD_TYPE_1 1
#define PORT100_CMD_STATUS_OK 0x00
#define PORT100_CMD_STATUS_TIMEOUT 0x80
#define PORT100_MDAA_TGT_HAS_BEEN_ACTIVATED_MASK 0x01
#define PORT100_MDAA_TGT_WAS_ACTIVATED_MASK 0x02
struct port100;
typedef void (*port100_send_async_complete_t)(struct port100 *dev, void *arg,
struct sk_buff *resp);
/**
* Setting sets structure for in_set_rf command
*
* @in_*_set_number: Represent the entry indexes in the port-100 RF Base Table.
* This table contains multiple RF setting sets required for RF
* communication.
*
* @in_*_comm_type: Theses fields set the communication type to be used.
*/
struct port100_in_rf_setting {
u8 in_send_set_number;
u8 in_send_comm_type;
u8 in_recv_set_number;
u8 in_recv_comm_type;
} __packed;
#define PORT100_COMM_TYPE_IN_212F 0x01
#define PORT100_COMM_TYPE_IN_424F 0x02
#define PORT100_COMM_TYPE_IN_106A 0x03
#define PORT100_COMM_TYPE_IN_106B 0x07
static const struct port100_in_rf_setting in_rf_settings[] = {
[NFC_DIGITAL_RF_TECH_212F] = {
.in_send_set_number = 1,
.in_send_comm_type = PORT100_COMM_TYPE_IN_212F,
.in_recv_set_number = 15,
.in_recv_comm_type = PORT100_COMM_TYPE_IN_212F,
},
[NFC_DIGITAL_RF_TECH_424F] = {
.in_send_set_number = 1,
.in_send_comm_type = PORT100_COMM_TYPE_IN_424F,
.in_recv_set_number = 15,
.in_recv_comm_type = PORT100_COMM_TYPE_IN_424F,
},
[NFC_DIGITAL_RF_TECH_106A] = {
.in_send_set_number = 2,
.in_send_comm_type = PORT100_COMM_TYPE_IN_106A,
.in_recv_set_number = 15,
.in_recv_comm_type = PORT100_COMM_TYPE_IN_106A,
},
[NFC_DIGITAL_RF_TECH_106B] = {
.in_send_set_number = 3,
.in_send_comm_type = PORT100_COMM_TYPE_IN_106B,
.in_recv_set_number = 15,
.in_recv_comm_type = PORT100_COMM_TYPE_IN_106B,
},
/* Ensures the array has NFC_DIGITAL_RF_TECH_LAST elements */
[NFC_DIGITAL_RF_TECH_LAST] = { 0 },
};
/**
* Setting sets structure for tg_set_rf command
*
* @tg_set_number: Represents the entry index in the port-100 RF Base Table.
* This table contains multiple RF setting sets required for RF
* communication. this field is used for both send and receive
* settings.
*
* @tg_comm_type: Sets the communication type to be used to send and receive
* data.
*/
struct port100_tg_rf_setting {
u8 tg_set_number;
u8 tg_comm_type;
} __packed;
#define PORT100_COMM_TYPE_TG_106A 0x0B
#define PORT100_COMM_TYPE_TG_212F 0x0C
#define PORT100_COMM_TYPE_TG_424F 0x0D
static const struct port100_tg_rf_setting tg_rf_settings[] = {
[NFC_DIGITAL_RF_TECH_106A] = {
.tg_set_number = 8,
.tg_comm_type = PORT100_COMM_TYPE_TG_106A,
},
[NFC_DIGITAL_RF_TECH_212F] = {
.tg_set_number = 8,
.tg_comm_type = PORT100_COMM_TYPE_TG_212F,
},
[NFC_DIGITAL_RF_TECH_424F] = {
.tg_set_number = 8,
.tg_comm_type = PORT100_COMM_TYPE_TG_424F,
},
/* Ensures the array has NFC_DIGITAL_RF_TECH_LAST elements */
[NFC_DIGITAL_RF_TECH_LAST] = { 0 },
};
#define PORT100_IN_PROT_INITIAL_GUARD_TIME 0x00
#define PORT100_IN_PROT_ADD_CRC 0x01
#define PORT100_IN_PROT_CHECK_CRC 0x02
#define PORT100_IN_PROT_MULTI_CARD 0x03
#define PORT100_IN_PROT_ADD_PARITY 0x04
#define PORT100_IN_PROT_CHECK_PARITY 0x05
#define PORT100_IN_PROT_BITWISE_AC_RECV_MODE 0x06
#define PORT100_IN_PROT_VALID_BIT_NUMBER 0x07
#define PORT100_IN_PROT_CRYPTO1 0x08
#define PORT100_IN_PROT_ADD_SOF 0x09
#define PORT100_IN_PROT_CHECK_SOF 0x0A
#define PORT100_IN_PROT_ADD_EOF 0x0B
#define PORT100_IN_PROT_CHECK_EOF 0x0C
#define PORT100_IN_PROT_DEAF_TIME 0x0E
#define PORT100_IN_PROT_CRM 0x0F
#define PORT100_IN_PROT_CRM_MIN_LEN 0x10
#define PORT100_IN_PROT_T1_TAG_FRAME 0x11
#define PORT100_IN_PROT_RFCA 0x12
#define PORT100_IN_PROT_GUARD_TIME_AT_INITIATOR 0x13
#define PORT100_IN_PROT_END 0x14
#define PORT100_IN_MAX_NUM_PROTOCOLS 19
#define PORT100_TG_PROT_TU 0x00
#define PORT100_TG_PROT_RF_OFF 0x01
#define PORT100_TG_PROT_CRM 0x02
#define PORT100_TG_PROT_END 0x03
#define PORT100_TG_MAX_NUM_PROTOCOLS 3
struct port100_protocol {
u8 number;
u8 value;
} __packed;
static struct port100_protocol
in_protocols[][PORT100_IN_MAX_NUM_PROTOCOLS + 1] = {
[NFC_DIGITAL_FRAMING_NFCA_SHORT] = {
{ PORT100_IN_PROT_INITIAL_GUARD_TIME, 6 },
{ PORT100_IN_PROT_ADD_CRC, 0 },
{ PORT100_IN_PROT_CHECK_CRC, 0 },
{ PORT100_IN_PROT_MULTI_CARD, 0 },
{ PORT100_IN_PROT_ADD_PARITY, 0 },
{ PORT100_IN_PROT_CHECK_PARITY, 1 },
{ PORT100_IN_PROT_BITWISE_AC_RECV_MODE, 0 },
{ PORT100_IN_PROT_VALID_BIT_NUMBER, 7 },
{ PORT100_IN_PROT_CRYPTO1, 0 },
{ PORT100_IN_PROT_ADD_SOF, 0 },
{ PORT100_IN_PROT_CHECK_SOF, 0 },
{ PORT100_IN_PROT_ADD_EOF, 0 },
{ PORT100_IN_PROT_CHECK_EOF, 0 },
{ PORT100_IN_PROT_DEAF_TIME, 4 },
{ PORT100_IN_PROT_CRM, 0 },
{ PORT100_IN_PROT_CRM_MIN_LEN, 0 },
{ PORT100_IN_PROT_T1_TAG_FRAME, 0 },
{ PORT100_IN_PROT_RFCA, 0 },
{ PORT100_IN_PROT_GUARD_TIME_AT_INITIATOR, 6 },
{ PORT100_IN_PROT_END, 0 },
},
[NFC_DIGITAL_FRAMING_NFCA_STANDARD] = {
{ PORT100_IN_PROT_INITIAL_GUARD_TIME, 6 },
{ PORT100_IN_PROT_ADD_CRC, 0 },
{ PORT100_IN_PROT_CHECK_CRC, 0 },
{ PORT100_IN_PROT_MULTI_CARD, 0 },
{ PORT100_IN_PROT_ADD_PARITY, 1 },
{ PORT100_IN_PROT_CHECK_PARITY, 1 },
{ PORT100_IN_PROT_BITWISE_AC_RECV_MODE, 0 },
{ PORT100_IN_PROT_VALID_BIT_NUMBER, 8 },
{ PORT100_IN_PROT_CRYPTO1, 0 },
{ PORT100_IN_PROT_ADD_SOF, 0 },
{ PORT100_IN_PROT_CHECK_SOF, 0 },
{ PORT100_IN_PROT_ADD_EOF, 0 },
{ PORT100_IN_PROT_CHECK_EOF, 0 },
{ PORT100_IN_PROT_DEAF_TIME, 4 },
{ PORT100_IN_PROT_CRM, 0 },
{ PORT100_IN_PROT_CRM_MIN_LEN, 0 },
{ PORT100_IN_PROT_T1_TAG_FRAME, 0 },
{ PORT100_IN_PROT_RFCA, 0 },
{ PORT100_IN_PROT_GUARD_TIME_AT_INITIATOR, 6 },
{ PORT100_IN_PROT_END, 0 },
},
[NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A] = {
{ PORT100_IN_PROT_INITIAL_GUARD_TIME, 6 },
{ PORT100_IN_PROT_ADD_CRC, 1 },
{ PORT100_IN_PROT_CHECK_CRC, 1 },
{ PORT100_IN_PROT_MULTI_CARD, 0 },
{ PORT100_IN_PROT_ADD_PARITY, 1 },
{ PORT100_IN_PROT_CHECK_PARITY, 1 },
{ PORT100_IN_PROT_BITWISE_AC_RECV_MODE, 0 },
{ PORT100_IN_PROT_VALID_BIT_NUMBER, 8 },
{ PORT100_IN_PROT_CRYPTO1, 0 },
{ PORT100_IN_PROT_ADD_SOF, 0 },
{ PORT100_IN_PROT_CHECK_SOF, 0 },
{ PORT100_IN_PROT_ADD_EOF, 0 },
{ PORT100_IN_PROT_CHECK_EOF, 0 },
{ PORT100_IN_PROT_DEAF_TIME, 4 },
{ PORT100_IN_PROT_CRM, 0 },
{ PORT100_IN_PROT_CRM_MIN_LEN, 0 },
{ PORT100_IN_PROT_T1_TAG_FRAME, 0 },
{ PORT100_IN_PROT_RFCA, 0 },
{ PORT100_IN_PROT_GUARD_TIME_AT_INITIATOR, 6 },
{ PORT100_IN_PROT_END, 0 },
},
[NFC_DIGITAL_FRAMING_NFCA_T1T] = {
/* nfc_digital_framing_nfca_short */
{ PORT100_IN_PROT_ADD_CRC, 2 },
{ PORT100_IN_PROT_CHECK_CRC, 2 },
{ PORT100_IN_PROT_VALID_BIT_NUMBER, 8 },
{ PORT100_IN_PROT_T1_TAG_FRAME, 2 },
{ PORT100_IN_PROT_END, 0 },
},
[NFC_DIGITAL_FRAMING_NFCA_T2T] = {
/* nfc_digital_framing_nfca_standard */
{ PORT100_IN_PROT_ADD_CRC, 1 },
{ PORT100_IN_PROT_CHECK_CRC, 0 },
{ PORT100_IN_PROT_END, 0 },
},
[NFC_DIGITAL_FRAMING_NFCA_T4T] = {
/* nfc_digital_framing_nfca_standard_with_crc_a */
{ PORT100_IN_PROT_END, 0 },
},
[NFC_DIGITAL_FRAMING_NFCA_NFC_DEP] = {
/* nfc_digital_framing_nfca_standard */
{ PORT100_IN_PROT_END, 0 },
},
[NFC_DIGITAL_FRAMING_NFCF] = {
{ PORT100_IN_PROT_INITIAL_GUARD_TIME, 18 },
{ PORT100_IN_PROT_ADD_CRC, 1 },
{ PORT100_IN_PROT_CHECK_CRC, 1 },
{ PORT100_IN_PROT_MULTI_CARD, 0 },
{ PORT100_IN_PROT_ADD_PARITY, 0 },
{ PORT100_IN_PROT_CHECK_PARITY, 0 },
{ PORT100_IN_PROT_BITWISE_AC_RECV_MODE, 0 },
{ PORT100_IN_PROT_VALID_BIT_NUMBER, 8 },
{ PORT100_IN_PROT_CRYPTO1, 0 },
{ PORT100_IN_PROT_ADD_SOF, 0 },
{ PORT100_IN_PROT_CHECK_SOF, 0 },
{ PORT100_IN_PROT_ADD_EOF, 0 },
{ PORT100_IN_PROT_CHECK_EOF, 0 },
{ PORT100_IN_PROT_DEAF_TIME, 4 },
{ PORT100_IN_PROT_CRM, 0 },
{ PORT100_IN_PROT_CRM_MIN_LEN, 0 },
{ PORT100_IN_PROT_T1_TAG_FRAME, 0 },
{ PORT100_IN_PROT_RFCA, 0 },
{ PORT100_IN_PROT_GUARD_TIME_AT_INITIATOR, 6 },
{ PORT100_IN_PROT_END, 0 },
},
[NFC_DIGITAL_FRAMING_NFCF_T3T] = {
/* nfc_digital_framing_nfcf */
{ PORT100_IN_PROT_END, 0 },
},
[NFC_DIGITAL_FRAMING_NFCF_NFC_DEP] = {
/* nfc_digital_framing_nfcf */
{ PORT100_IN_PROT_INITIAL_GUARD_TIME, 18 },
{ PORT100_IN_PROT_ADD_CRC, 1 },
{ PORT100_IN_PROT_CHECK_CRC, 1 },
{ PORT100_IN_PROT_MULTI_CARD, 0 },
{ PORT100_IN_PROT_ADD_PARITY, 0 },
{ PORT100_IN_PROT_CHECK_PARITY, 0 },
{ PORT100_IN_PROT_BITWISE_AC_RECV_MODE, 0 },
{ PORT100_IN_PROT_VALID_BIT_NUMBER, 8 },
{ PORT100_IN_PROT_CRYPTO1, 0 },
{ PORT100_IN_PROT_ADD_SOF, 0 },
{ PORT100_IN_PROT_CHECK_SOF, 0 },
{ PORT100_IN_PROT_ADD_EOF, 0 },
{ PORT100_IN_PROT_CHECK_EOF, 0 },
{ PORT100_IN_PROT_DEAF_TIME, 4 },
{ PORT100_IN_PROT_CRM, 0 },
{ PORT100_IN_PROT_CRM_MIN_LEN, 0 },
{ PORT100_IN_PROT_T1_TAG_FRAME, 0 },
{ PORT100_IN_PROT_RFCA, 0 },
{ PORT100_IN_PROT_GUARD_TIME_AT_INITIATOR, 6 },
{ PORT100_IN_PROT_END, 0 },
},
[NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED] = {
{ PORT100_IN_PROT_END, 0 },
},
[NFC_DIGITAL_FRAMING_NFCB] = {
{ PORT100_IN_PROT_INITIAL_GUARD_TIME, 20 },
{ PORT100_IN_PROT_ADD_CRC, 1 },
{ PORT100_IN_PROT_CHECK_CRC, 1 },
{ PORT100_IN_PROT_MULTI_CARD, 0 },
{ PORT100_IN_PROT_ADD_PARITY, 0 },
{ PORT100_IN_PROT_CHECK_PARITY, 0 },
{ PORT100_IN_PROT_BITWISE_AC_RECV_MODE, 0 },
{ PORT100_IN_PROT_VALID_BIT_NUMBER, 8 },
{ PORT100_IN_PROT_CRYPTO1, 0 },
{ PORT100_IN_PROT_ADD_SOF, 1 },
{ PORT100_IN_PROT_CHECK_SOF, 1 },
{ PORT100_IN_PROT_ADD_EOF, 1 },
{ PORT100_IN_PROT_CHECK_EOF, 1 },
{ PORT100_IN_PROT_DEAF_TIME, 4 },
{ PORT100_IN_PROT_CRM, 0 },
{ PORT100_IN_PROT_CRM_MIN_LEN, 0 },
{ PORT100_IN_PROT_T1_TAG_FRAME, 0 },
{ PORT100_IN_PROT_RFCA, 0 },
{ PORT100_IN_PROT_GUARD_TIME_AT_INITIATOR, 6 },
{ PORT100_IN_PROT_END, 0 },
},
[NFC_DIGITAL_FRAMING_NFCB_T4T] = {
/* nfc_digital_framing_nfcb */
{ PORT100_IN_PROT_END, 0 },
},
/* Ensures the array has NFC_DIGITAL_FRAMING_LAST elements */
[NFC_DIGITAL_FRAMING_LAST] = {
{ PORT100_IN_PROT_END, 0 },
},
};
static struct port100_protocol
tg_protocols[][PORT100_TG_MAX_NUM_PROTOCOLS + 1] = {
[NFC_DIGITAL_FRAMING_NFCA_SHORT] = {
{ PORT100_TG_PROT_END, 0 },
},
[NFC_DIGITAL_FRAMING_NFCA_STANDARD] = {
{ PORT100_TG_PROT_END, 0 },
},
[NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A] = {
{ PORT100_TG_PROT_END, 0 },
},
[NFC_DIGITAL_FRAMING_NFCA_T1T] = {
{ PORT100_TG_PROT_END, 0 },
},
[NFC_DIGITAL_FRAMING_NFCA_T2T] = {
{ PORT100_TG_PROT_END, 0 },
},
[NFC_DIGITAL_FRAMING_NFCA_NFC_DEP] = {
{ PORT100_TG_PROT_TU, 1 },
{ PORT100_TG_PROT_RF_OFF, 0 },
{ PORT100_TG_PROT_CRM, 7 },
{ PORT100_TG_PROT_END, 0 },
},
[NFC_DIGITAL_FRAMING_NFCF] = {
{ PORT100_TG_PROT_END, 0 },
},
[NFC_DIGITAL_FRAMING_NFCF_T3T] = {
{ PORT100_TG_PROT_END, 0 },
},
[NFC_DIGITAL_FRAMING_NFCF_NFC_DEP] = {
{ PORT100_TG_PROT_TU, 1 },
{ PORT100_TG_PROT_RF_OFF, 0 },
{ PORT100_TG_PROT_CRM, 7 },
{ PORT100_TG_PROT_END, 0 },
},
[NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED] = {
{ PORT100_TG_PROT_RF_OFF, 1 },
{ PORT100_TG_PROT_END, 0 },
},
/* Ensures the array has NFC_DIGITAL_FRAMING_LAST elements */
[NFC_DIGITAL_FRAMING_LAST] = {
{ PORT100_TG_PROT_END, 0 },
},
};
struct port100 {
struct nfc_digital_dev *nfc_digital_dev;
int skb_headroom;
int skb_tailroom;
struct usb_device *udev;
struct usb_interface *interface;
struct urb *out_urb;
struct urb *in_urb;
/* This mutex protects the out_urb and avoids to submit a new command
* through port100_send_frame_async() while the previous one is being
* canceled through port100_abort_cmd().
*/
struct mutex out_urb_lock;
struct work_struct cmd_complete_work;
u8 cmd_type;
/* The digital stack serializes commands to be sent. There is no need
* for any queuing/locking mechanism at driver level.
*/
struct port100_cmd *cmd;
bool cmd_cancel;
struct completion cmd_cancel_done;
};
struct port100_cmd {
u8 code;
int status;
struct sk_buff *req;
struct sk_buff *resp;
int resp_len;
port100_send_async_complete_t complete_cb;
void *complete_cb_context;
};
struct port100_frame {
u8 preamble;
__be16 start_frame;
__be16 extended_frame;
__le16 datalen;
u8 datalen_checksum;
u8 data[];
} __packed;
struct port100_ack_frame {
u8 preamble;
__be16 start_frame;
__be16 ack_frame;
u8 postambule;
} __packed;
struct port100_cb_arg {
nfc_digital_cmd_complete_t complete_cb;
void *complete_arg;
u8 mdaa;
};
struct port100_tg_comm_rf_cmd {
__le16 guard_time;
__le16 send_timeout;
u8 mdaa;
u8 nfca_param[6];
u8 nfcf_param[18];
u8 mf_halted;
u8 arae_flag;
__le16 recv_timeout;
u8 data[];
} __packed;
struct port100_tg_comm_rf_res {
u8 comm_type;
u8 ar_status;
u8 target_activated;
__le32 status;
u8 data[];
} __packed;
/* The rule: value + checksum = 0 */
static inline u8 port100_checksum(u16 value)
{
return ~(((u8 *)&value)[0] + ((u8 *)&value)[1]) + 1;
}
/* The rule: sum(data elements) + checksum = 0 */
static u8 port100_data_checksum(u8 *data, int datalen)
{
u8 sum = 0;
int i;
for (i = 0; i < datalen; i++)
sum += data[i];
return port100_checksum(sum);
}
static void port100_tx_frame_init(void *_frame, u8 cmd_code)
{
struct port100_frame *frame = _frame;
frame->preamble = 0;
frame->start_frame = cpu_to_be16(PORT100_FRAME_SOF);
frame->extended_frame = cpu_to_be16(PORT100_FRAME_EXT);
PORT100_FRAME_DIRECTION(frame) = PORT100_FRAME_DIR_OUT;
PORT100_FRAME_CMD(frame) = cmd_code;
frame->datalen = cpu_to_le16(2);
}
static void port100_tx_frame_finish(void *_frame)
{
struct port100_frame *frame = _frame;
frame->datalen_checksum = port100_checksum(le16_to_cpu(frame->datalen));
PORT100_FRAME_CHECKSUM(frame) =
port100_data_checksum(frame->data, le16_to_cpu(frame->datalen));
PORT100_FRAME_POSTAMBLE(frame) = 0;
}
static void port100_tx_update_payload_len(void *_frame, int len)
{
struct port100_frame *frame = _frame;
le16_add_cpu(&frame->datalen, len);
}
static bool port100_rx_frame_is_valid(void *_frame)
{
u8 checksum;
struct port100_frame *frame = _frame;
if (frame->start_frame != cpu_to_be16(PORT100_FRAME_SOF) ||
frame->extended_frame != cpu_to_be16(PORT100_FRAME_EXT))
return false;
checksum = port100_checksum(le16_to_cpu(frame->datalen));
if (checksum != frame->datalen_checksum)
return false;
checksum = port100_data_checksum(frame->data,
le16_to_cpu(frame->datalen));
if (checksum != PORT100_FRAME_CHECKSUM(frame))
return false;
return true;
}
static bool port100_rx_frame_is_ack(struct port100_ack_frame *frame)
{
return (frame->start_frame == cpu_to_be16(PORT100_FRAME_SOF) &&
frame->ack_frame == cpu_to_be16(PORT100_FRAME_ACK));
}
static inline int port100_rx_frame_size(void *frame)
{
struct port100_frame *f = frame;
return sizeof(struct port100_frame) + le16_to_cpu(f->datalen) +
PORT100_FRAME_TAIL_LEN;
}
static bool port100_rx_frame_is_cmd_response(struct port100 *dev, void *frame)
{
struct port100_frame *f = frame;
return (PORT100_FRAME_CMD(f) == PORT100_CMD_RESPONSE(dev->cmd->code));
}
static void port100_recv_response(struct urb *urb)
{
struct port100 *dev = urb->context;
struct port100_cmd *cmd = dev->cmd;
u8 *in_frame;
cmd->status = urb->status;
switch (urb->status) {
case 0:
break; /* success */
case -ECONNRESET:
case -ENOENT:
nfc_err(&dev->interface->dev,
"The urb has been canceled (status %d)\n", urb->status);
goto sched_wq;
case -ESHUTDOWN:
default:
nfc_err(&dev->interface->dev, "Urb failure (status %d)\n",
urb->status);
goto sched_wq;
}
in_frame = dev->in_urb->transfer_buffer;
if (!port100_rx_frame_is_valid(in_frame)) {
nfc_err(&dev->interface->dev, "Received an invalid frame\n");
cmd->status = -EIO;
goto sched_wq;
}
print_hex_dump_debug("PORT100 RX: ", DUMP_PREFIX_NONE, 16, 1, in_frame,
port100_rx_frame_size(in_frame), false);
if (!port100_rx_frame_is_cmd_response(dev, in_frame)) {
nfc_err(&dev->interface->dev,
"It's not the response to the last command\n");
cmd->status = -EIO;
goto sched_wq;
}
sched_wq:
schedule_work(&dev->cmd_complete_work);
}
static int port100_submit_urb_for_response(struct port100 *dev, gfp_t flags)
{
dev->in_urb->complete = port100_recv_response;
return usb_submit_urb(dev->in_urb, flags);
}
static void port100_recv_ack(struct urb *urb)
{
struct port100 *dev = urb->context;
struct port100_cmd *cmd = dev->cmd;
struct port100_ack_frame *in_frame;
int rc;
cmd->status = urb->status;
switch (urb->status) {
case 0:
break; /* success */
case -ECONNRESET:
case -ENOENT:
nfc_err(&dev->interface->dev,
"The urb has been stopped (status %d)\n", urb->status);
goto sched_wq;
case -ESHUTDOWN:
default:
nfc_err(&dev->interface->dev, "Urb failure (status %d)\n",
urb->status);
goto sched_wq;
}
in_frame = dev->in_urb->transfer_buffer;
if (!port100_rx_frame_is_ack(in_frame)) {
nfc_err(&dev->interface->dev, "Received an invalid ack\n");
cmd->status = -EIO;
goto sched_wq;
}
rc = port100_submit_urb_for_response(dev, GFP_ATOMIC);
if (rc) {
nfc_err(&dev->interface->dev,
"usb_submit_urb failed with result %d\n", rc);
cmd->status = rc;
goto sched_wq;
}
return;
sched_wq:
schedule_work(&dev->cmd_complete_work);
}
static int port100_submit_urb_for_ack(struct port100 *dev, gfp_t flags)
{
dev->in_urb->complete = port100_recv_ack;
return usb_submit_urb(dev->in_urb, flags);
}
static int port100_send_ack(struct port100 *dev)
{
int rc = 0;
mutex_lock(&dev->out_urb_lock);
/*
* If prior cancel is in-flight (dev->cmd_cancel == true), we
* can skip to send cancel. Then this will wait the prior
* cancel, or merged into the next cancel rarely if next
* cancel was started before waiting done. In any case, this
* will be waked up soon or later.
*/
if (!dev->cmd_cancel) {
reinit_completion(&dev->cmd_cancel_done);
usb_kill_urb(dev->out_urb);
dev->out_urb->transfer_buffer = ack_frame;
dev->out_urb->transfer_buffer_length = sizeof(ack_frame);
rc = usb_submit_urb(dev->out_urb, GFP_KERNEL);
/*
* Set the cmd_cancel flag only if the URB has been
* successfully submitted. It will be reset by the out
* URB completion callback port100_send_complete().
*/
dev->cmd_cancel = !rc;
}
mutex_unlock(&dev->out_urb_lock);
if (!rc)
wait_for_completion(&dev->cmd_cancel_done);
return rc;
}
static int port100_send_frame_async(struct port100 *dev, struct sk_buff *out,
struct sk_buff *in, int in_len)
{
int rc;
mutex_lock(&dev->out_urb_lock);
/* A command cancel frame as been sent through dev->out_urb. Don't try
* to submit a new one.
*/
if (dev->cmd_cancel) {
rc = -EAGAIN;
goto exit;
}
dev->out_urb->transfer_buffer = out->data;
dev->out_urb->transfer_buffer_length = out->len;
dev->in_urb->transfer_buffer = in->data;
dev->in_urb->transfer_buffer_length = in_len;
print_hex_dump_debug("PORT100 TX: ", DUMP_PREFIX_NONE, 16, 1,
out->data, out->len, false);
rc = usb_submit_urb(dev->out_urb, GFP_KERNEL);
if (rc)
goto exit;
rc = port100_submit_urb_for_ack(dev, GFP_KERNEL);
if (rc)
usb_kill_urb(dev->out_urb);
exit:
mutex_unlock(&dev->out_urb_lock);
return rc;
}
static void port100_build_cmd_frame(struct port100 *dev, u8 cmd_code,
struct sk_buff *skb)
{
/* payload is already there, just update datalen */
int payload_len = skb->len;
skb_push(skb, PORT100_FRAME_HEADER_LEN);
skb_put(skb, PORT100_FRAME_TAIL_LEN);
port100_tx_frame_init(skb->data, cmd_code);
port100_tx_update_payload_len(skb->data, payload_len);
port100_tx_frame_finish(skb->data);
}
static void port100_send_async_complete(struct port100 *dev)
{
struct port100_cmd *cmd = dev->cmd;
int status = cmd->status;
struct sk_buff *req = cmd->req;
struct sk_buff *resp = cmd->resp;
dev_kfree_skb(req);
dev->cmd = NULL;
if (status < 0) {
cmd->complete_cb(dev, cmd->complete_cb_context,
ERR_PTR(status));
dev_kfree_skb(resp);
goto done;
}
skb_put(resp, port100_rx_frame_size(resp->data));
skb_pull(resp, PORT100_FRAME_HEADER_LEN);
skb_trim(resp, resp->len - PORT100_FRAME_TAIL_LEN);
cmd->complete_cb(dev, cmd->complete_cb_context, resp);
done:
kfree(cmd);
}
static int port100_send_cmd_async(struct port100 *dev, u8 cmd_code,
struct sk_buff *req,
port100_send_async_complete_t complete_cb,
void *complete_cb_context)
{
struct port100_cmd *cmd;
struct sk_buff *resp;
int rc;
int resp_len = PORT100_FRAME_HEADER_LEN +
PORT100_FRAME_MAX_PAYLOAD_LEN +
PORT100_FRAME_TAIL_LEN;
if (dev->cmd) {
nfc_err(&dev->interface->dev,
"A command is still in process\n");
return -EBUSY;
}
resp = alloc_skb(resp_len, GFP_KERNEL);
if (!resp)
return -ENOMEM;
cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
if (!cmd) {
dev_kfree_skb(resp);
return -ENOMEM;
}
cmd->code = cmd_code;
cmd->req = req;
cmd->resp = resp;
cmd->resp_len = resp_len;
cmd->complete_cb = complete_cb;
cmd->complete_cb_context = complete_cb_context;
port100_build_cmd_frame(dev, cmd_code, req);
dev->cmd = cmd;
rc = port100_send_frame_async(dev, req, resp, resp_len);
if (rc) {
kfree(cmd);
dev_kfree_skb(resp);
dev->cmd = NULL;
}
return rc;
}
struct port100_sync_cmd_response {
struct sk_buff *resp;
struct completion done;
};
static void port100_wq_cmd_complete(struct work_struct *work)
{
struct port100 *dev = container_of(work, struct port100,
cmd_complete_work);
port100_send_async_complete(dev);
}
static void port100_send_sync_complete(struct port100 *dev, void *_arg,
struct sk_buff *resp)
{
struct port100_sync_cmd_response *arg = _arg;
arg->resp = resp;
complete(&arg->done);
}
static struct sk_buff *port100_send_cmd_sync(struct port100 *dev, u8 cmd_code,
struct sk_buff *req)
{
int rc;
struct port100_sync_cmd_response arg;
init_completion(&arg.done);
rc = port100_send_cmd_async(dev, cmd_code, req,
port100_send_sync_complete, &arg);
if (rc) {
dev_kfree_skb(req);
return ERR_PTR(rc);
}
wait_for_completion(&arg.done);
return arg.resp;
}
static void port100_send_complete(struct urb *urb)
{
struct port100 *dev = urb->context;
if (dev->cmd_cancel) {
complete_all(&dev->cmd_cancel_done);
dev->cmd_cancel = false;
}
switch (urb->status) {
case 0:
break; /* success */
case -ECONNRESET:
case -ENOENT:
nfc_err(&dev->interface->dev,
"The urb has been stopped (status %d)\n", urb->status);
break;
case -ESHUTDOWN:
default:
nfc_err(&dev->interface->dev, "Urb failure (status %d)\n",
urb->status);
}
}
static void port100_abort_cmd(struct nfc_digital_dev *ddev)
{
struct port100 *dev = nfc_digital_get_drvdata(ddev);
/* An ack will cancel the last issued command */
port100_send_ack(dev);
/* cancel the urb request */
usb_kill_urb(dev->in_urb);
}
static struct sk_buff *port100_alloc_skb(struct port100 *dev, unsigned int size)
{
struct sk_buff *skb;
skb = alloc_skb(dev->skb_headroom + dev->skb_tailroom + size,
GFP_KERNEL);
if (skb)
skb_reserve(skb, dev->skb_headroom);
return skb;
}
static int port100_set_command_type(struct port100 *dev, u8 command_type)
{
struct sk_buff *skb;
struct sk_buff *resp;
int rc;
skb = port100_alloc_skb(dev, 1);
if (!skb)
return -ENOMEM;
skb_put_u8(skb, command_type);
resp = port100_send_cmd_sync(dev, PORT100_CMD_SET_COMMAND_TYPE, skb);
if (IS_ERR(resp))
return PTR_ERR(resp);
rc = resp->data[0];
dev_kfree_skb(resp);
return rc;
}
static u64 port100_get_command_type_mask(struct port100 *dev)
{
struct sk_buff *skb;