-
Notifications
You must be signed in to change notification settings - Fork 7
/
hid-nx.c
2700 lines (2344 loc) · 83.1 KB
/
hid-nx.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+
/*
* HID driver for Nintendo Switch Controllers
*
* Copyright (c) 2019-2021 Daniel J. Ogorchock <[email protected]>
* Portions Copyright (c) 2020 Nadia Holmquist Pedersen <[email protected]>
* Copyright (c) 2022 Emily Strickland <[email protected]>
*
* The following resources/projects were referenced for this driver:
* https://github.com/dekuNukem/Nintendo_Switch_Reverse_Engineering
* https://gitlab.com/pjranki/joycon-linux-kernel (Peter Rankin)
* https://github.com/FrotBot/SwitchProConLinuxUSB
* https://github.com/MTCKC/ProconXInput
* https://github.com/Davidobot/BetterJoyForCemu
* hid-wiimote kernel hid driver
* hid-logitech-hidpp driver
* hid-sony driver
*
* This driver supports the Nintendo Switch Joy-Cons and Pro Controllers. The
* Pro Controllers can either be used over USB or Bluetooth.
*
* This driver also incorporates support for Nintendo Switch Online controllers
* for the NES, SNES, and Sega Genesis.
*
* The driver will retrieve the factory calibration info from the controllers,
* so little to no user calibration should be required.
*
*/
#include "hid-ids.h"
#include <asm/unaligned.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/kernel.h>
#include <linux/hid.h>
#include <linux/input.h>
#include <linux/jiffies.h>
#include <linux/leds.h>
#include <linux/module.h>
#include <linux/power_supply.h>
#include <linux/spinlock.h>
/*
* Reference the url below for the following HID report defines:
* https://github.com/dekuNukem/Nintendo_Switch_Reverse_Engineering
*/
/* Output Reports */
#define NX_CON_OUTPUT_RUMBLE_AND_SUBCMD 0x01
#define NX_CON_OUTPUT_FW_UPDATE_PKT 0x03
#define NX_CON_OUTPUT_RUMBLE_ONLY 0x10
#define NX_CON_OUTPUT_MCU_DATA 0x11
#define NX_CON_OUTPUT_USB_CMD 0x80
/* Subcommand IDs */
#define NX_CON_SUBCMD_STATE 0x00
#define NX_CON_SUBCMD_MANUAL_BT_PAIRING 0x01
#define NX_CON_SUBCMD_REQ_DEV_INFO 0x02
#define NX_CON_SUBCMD_SET_REPORT_MODE 0x03
#define NX_CON_SUBCMD_TRIGGERS_ELAPSED 0x04
#define NX_CON_SUBCMD_GET_PAGE_LIST_STATE 0x05
#define NX_CON_SUBCMD_SET_HCI_STATE 0x06
#define NX_CON_SUBCMD_RESET_PAIRING_INFO 0x07
#define NX_CON_SUBCMD_LOW_POWER_MODE 0x08
#define NX_CON_SUBCMD_SPI_FLASH_READ 0x10
#define NX_CON_SUBCMD_SPI_FLASH_WRITE 0x11
#define NX_CON_SUBCMD_RESET_MCU 0x20
#define NX_CON_SUBCMD_SET_MCU_CONFIG 0x21
#define NX_CON_SUBCMD_SET_MCU_STATE 0x22
#define NX_CON_SUBCMD_SET_PLAYER_LIGHTS 0x30
#define NX_CON_SUBCMD_GET_PLAYER_LIGHTS 0x31
#define NX_CON_SUBCMD_SET_HOME_LIGHT 0x38
#define NX_CON_SUBCMD_ENABLE_IMU 0x40
#define NX_CON_SUBCMD_SET_IMU_SENSITIVITY 0x41
#define NX_CON_SUBCMD_WRITE_IMU_REG 0x42
#define NX_CON_SUBCMD_READ_IMU_REG 0x43
#define NX_CON_SUBCMD_ENABLE_VIBRATION 0x48
#define NX_CON_SUBCMD_GET_REGULATED_VOLTAGE 0x50
/* Input Reports */
#define NX_CON_INPUT_BUTTON_EVENT 0x3F
#define NX_CON_INPUT_SUBCMD_REPLY 0x21
#define NX_CON_INPUT_IMU_DATA 0x30
#define NX_CON_INPUT_MCU_DATA 0x31
#define NX_CON_INPUT_USB_RESPONSE 0x81
/* Feature Reports */
#define NX_CON_FEATURE_LAST_SUBCMD 0x02
#define NX_CON_FEATURE_OTA_FW_UPGRADE 0x70
#define NX_CON_FEATURE_SETUP_MEM_READ 0x71
#define NX_CON_FEATURE_MEM_READ 0x72
#define NX_CON_FEATURE_ERASE_MEM_SECTOR 0x73
#define NX_CON_FEATURE_MEM_WRITE 0x74
#define NX_CON_FEATURE_LAUNCH 0x75
/* USB Commands */
#define NX_CON_USB_CMD_CONN_STATUS 0x01
#define NX_CON_USB_CMD_HANDSHAKE 0x02
#define NX_CON_USB_CMD_BAUDRATE_3M 0x03
#define NX_CON_USB_CMD_NO_TIMEOUT 0x04
#define NX_CON_USB_CMD_EN_TIMEOUT 0x05
#define NX_CON_USB_RESET 0x06
#define NX_CON_USB_PRE_HANDSHAKE 0x91
#define NX_CON_USB_SEND_UART 0x92
/* Magic value denoting presence of user calibration */
#define NX_CON_CAL_USR_MAGIC_0 0xB2
#define NX_CON_CAL_USR_MAGIC_1 0xA1
#define NX_CON_CAL_USR_MAGIC_SIZE 2
/* SPI storage addresses of user calibration data */
#define NX_CON_CAL_USR_LEFT_MAGIC_ADDR 0x8010
#define NX_CON_CAL_USR_LEFT_DATA_ADDR 0x8012
#define NX_CON_CAL_USR_LEFT_DATA_END 0x801A
#define NX_CON_CAL_USR_RIGHT_MAGIC_ADDR 0x801B
#define NX_CON_CAL_USR_RIGHT_DATA_ADDR 0x801D
#define NX_CON_CAL_STICK_DATA_SIZE \
(NX_CON_CAL_USR_LEFT_DATA_END - NX_CON_CAL_USR_LEFT_DATA_ADDR + 1)
/* SPI storage addresses of factory calibration data */
#define NX_CON_CAL_FCT_DATA_LEFT_ADDR 0x603d
#define NX_CON_CAL_FCT_DATA_RIGHT_ADDR 0x6046
/* SPI storage addresses of IMU factory calibration data */
#define NX_CON_IMU_CAL_FCT_DATA_ADDR 0x6020
#define NX_CON_IMU_CAL_FCT_DATA_END 0x6037
#define NX_CON_IMU_CAL_DATA_SIZE \
(NX_CON_IMU_CAL_FCT_DATA_END - NX_CON_IMU_CAL_FCT_DATA_ADDR + 1)
/* SPI storage addresses of IMU user calibration data */
#define NX_CON_IMU_CAL_USR_MAGIC_ADDR 0x8026
#define NX_CON_IMU_CAL_USR_DATA_ADDR 0x8028
/* The raw analog joystick values will be mapped in terms of this magnitude */
#define NX_CON_MAX_STICK_MAG 32767
#define NX_CON_STICK_FUZZ 250
#define NX_CON_STICK_FLAT 500
/* Hat values for pro controller's d-pad */
#define NX_CON_MAX_DPAD_MAG 1
#define NX_CON_DPAD_FUZZ 0
#define NX_CON_DPAD_FLAT 0
/* Under most circumstances IMU reports are pushed every 15ms; use as default */
#define NX_CON_IMU_DFLT_AVG_DELTA_MS 15
/* How many samples to sum before calculating average IMU report delta */
#define NX_CON_IMU_SAMPLES_PER_DELTA_AVG 300
/* Controls how many dropped IMU packets at once trigger a warning message */
#define NX_CON_IMU_DROPPED_PKT_WARNING 3
/*
* The controller's accelerometer has a sensor resolution of 16bits and is
* configured with a range of +-8000 milliGs. Therefore, the resolution can be
* calculated thus: (2^16-1)/(8000 * 2) = 4.096 digits per milliG
* Resolution per G (rather than per millliG): 4.096 * 1000 = 4096 digits per G
* Alternatively: 1/4096 = .0002441 Gs per digit
*/
#define NX_CON_IMU_MAX_ACCEL_MAG 32767
#define NX_CON_IMU_ACCEL_RES_PER_G 4096
#define NX_CON_IMU_ACCEL_FUZZ 10
#define NX_CON_IMU_ACCEL_FLAT 0
/*
* The controller's gyroscope has a sensor resolution of 16bits and is
* configured with a range of +-2000 degrees/second.
* Digits per dps: (2^16 -1)/(2000*2) = 16.38375
* dps per digit: 16.38375E-1 = .0610
*
* STMicro recommends in the datasheet to add 15% to the dps/digit. This allows
* the full sensitivity range to be saturated without clipping. This yields more
* accurate results, so it's the technique this driver uses.
* dps per digit (corrected): .0610 * 1.15 = .0702
* digits per dps (corrected): .0702E-1 = 14.247
*
* Now, 14.247 truncating to 14 loses a lot of precision, so we rescale the
* min/max range by 1000.
*/
#define NX_CON_IMU_PREC_RANGE_SCALE 1000
/* Note: change mag and res_per_dps if prec_range_scale is ever altered */
#define NX_CON_IMU_MAX_GYRO_MAG 32767000 /* (2^16-1)*1000 */
#define NX_CON_IMU_GYRO_RES_PER_DPS 14247 /* (14.247*1000) */
#define NX_CON_IMU_GYRO_FUZZ 10
#define NX_CON_IMU_GYRO_FLAT 0
/* frequency/amplitude tables for rumble */
struct nx_con_rumble_freq_data {
u16 high;
u8 low;
u16 freq; /* Hz*/
};
struct nx_con_rumble_amp_data {
u8 high;
u16 low;
u16 amp;
};
#if IS_ENABLED(CONFIG_NINTENDO_FF)
/*
* These tables are from
* https://github.com/dekuNukem/Nintendo_Switch_Reverse_Engineering/blob/master/rumble_data_table.md
*/
static const struct nx_con_rumble_freq_data nx_con_rumble_frequencies[] = {
/* high, low, freq */
{ 0x0000, 0x01, 41 }, { 0x0000, 0x02, 42 }, { 0x0000, 0x03, 43 },
{ 0x0000, 0x04, 44 }, { 0x0000, 0x05, 45 }, { 0x0000, 0x06, 46 },
{ 0x0000, 0x07, 47 }, { 0x0000, 0x08, 48 }, { 0x0000, 0x09, 49 },
{ 0x0000, 0x0A, 50 }, { 0x0000, 0x0B, 51 }, { 0x0000, 0x0C, 52 },
{ 0x0000, 0x0D, 53 }, { 0x0000, 0x0E, 54 }, { 0x0000, 0x0F, 55 },
{ 0x0000, 0x10, 57 }, { 0x0000, 0x11, 58 }, { 0x0000, 0x12, 59 },
{ 0x0000, 0x13, 60 }, { 0x0000, 0x14, 62 }, { 0x0000, 0x15, 63 },
{ 0x0000, 0x16, 64 }, { 0x0000, 0x17, 66 }, { 0x0000, 0x18, 67 },
{ 0x0000, 0x19, 69 }, { 0x0000, 0x1A, 70 }, { 0x0000, 0x1B, 72 },
{ 0x0000, 0x1C, 73 }, { 0x0000, 0x1D, 75 }, { 0x0000, 0x1e, 77 },
{ 0x0000, 0x1f, 78 }, { 0x0000, 0x20, 80 }, { 0x0400, 0x21, 82 },
{ 0x0800, 0x22, 84 }, { 0x0c00, 0x23, 85 }, { 0x1000, 0x24, 87 },
{ 0x1400, 0x25, 89 }, { 0x1800, 0x26, 91 }, { 0x1c00, 0x27, 93 },
{ 0x2000, 0x28, 95 }, { 0x2400, 0x29, 97 }, { 0x2800, 0x2a, 99 },
{ 0x2c00, 0x2b, 102 }, { 0x3000, 0x2c, 104 }, { 0x3400, 0x2d, 106 },
{ 0x3800, 0x2e, 108 }, { 0x3c00, 0x2f, 111 }, { 0x4000, 0x30, 113 },
{ 0x4400, 0x31, 116 }, { 0x4800, 0x32, 118 }, { 0x4c00, 0x33, 121 },
{ 0x5000, 0x34, 123 }, { 0x5400, 0x35, 126 }, { 0x5800, 0x36, 129 },
{ 0x5c00, 0x37, 132 }, { 0x6000, 0x38, 135 }, { 0x6400, 0x39, 137 },
{ 0x6800, 0x3a, 141 }, { 0x6c00, 0x3b, 144 }, { 0x7000, 0x3c, 147 },
{ 0x7400, 0x3d, 150 }, { 0x7800, 0x3e, 153 }, { 0x7c00, 0x3f, 157 },
{ 0x8000, 0x40, 160 }, { 0x8400, 0x41, 164 }, { 0x8800, 0x42, 167 },
{ 0x8c00, 0x43, 171 }, { 0x9000, 0x44, 174 }, { 0x9400, 0x45, 178 },
{ 0x9800, 0x46, 182 }, { 0x9c00, 0x47, 186 }, { 0xa000, 0x48, 190 },
{ 0xa400, 0x49, 194 }, { 0xa800, 0x4a, 199 }, { 0xac00, 0x4b, 203 },
{ 0xb000, 0x4c, 207 }, { 0xb400, 0x4d, 212 }, { 0xb800, 0x4e, 217 },
{ 0xbc00, 0x4f, 221 }, { 0xc000, 0x50, 226 }, { 0xc400, 0x51, 231 },
{ 0xc800, 0x52, 236 }, { 0xcc00, 0x53, 241 }, { 0xd000, 0x54, 247 },
{ 0xd400, 0x55, 252 }, { 0xd800, 0x56, 258 }, { 0xdc00, 0x57, 263 },
{ 0xe000, 0x58, 269 }, { 0xe400, 0x59, 275 }, { 0xe800, 0x5a, 281 },
{ 0xec00, 0x5b, 287 }, { 0xf000, 0x5c, 293 }, { 0xf400, 0x5d, 300 },
{ 0xf800, 0x5e, 306 }, { 0xfc00, 0x5f, 313 }, { 0x0001, 0x60, 320 },
{ 0x0401, 0x61, 327 }, { 0x0801, 0x62, 334 }, { 0x0c01, 0x63, 341 },
{ 0x1001, 0x64, 349 }, { 0x1401, 0x65, 357 }, { 0x1801, 0x66, 364 },
{ 0x1c01, 0x67, 372 }, { 0x2001, 0x68, 381 }, { 0x2401, 0x69, 389 },
{ 0x2801, 0x6a, 397 }, { 0x2c01, 0x6b, 406 }, { 0x3001, 0x6c, 415 },
{ 0x3401, 0x6d, 424 }, { 0x3801, 0x6e, 433 }, { 0x3c01, 0x6f, 443 },
{ 0x4001, 0x70, 453 }, { 0x4401, 0x71, 462 }, { 0x4801, 0x72, 473 },
{ 0x4c01, 0x73, 483 }, { 0x5001, 0x74, 494 }, { 0x5401, 0x75, 504 },
{ 0x5801, 0x76, 515 }, { 0x5c01, 0x77, 527 }, { 0x6001, 0x78, 538 },
{ 0x6401, 0x79, 550 }, { 0x6801, 0x7a, 562 }, { 0x6c01, 0x7b, 574 },
{ 0x7001, 0x7c, 587 }, { 0x7401, 0x7d, 600 }, { 0x7801, 0x7e, 613 },
{ 0x7c01, 0x7f, 626 }, { 0x8001, 0x00, 640 }, { 0x8401, 0x00, 654 },
{ 0x8801, 0x00, 668 }, { 0x8c01, 0x00, 683 }, { 0x9001, 0x00, 698 },
{ 0x9401, 0x00, 713 }, { 0x9801, 0x00, 729 }, { 0x9c01, 0x00, 745 },
{ 0xa001, 0x00, 761 }, { 0xa401, 0x00, 778 }, { 0xa801, 0x00, 795 },
{ 0xac01, 0x00, 812 }, { 0xb001, 0x00, 830 }, { 0xb401, 0x00, 848 },
{ 0xb801, 0x00, 867 }, { 0xbc01, 0x00, 886 }, { 0xc001, 0x00, 905 },
{ 0xc401, 0x00, 925 }, { 0xc801, 0x00, 945 }, { 0xcc01, 0x00, 966 },
{ 0xd001, 0x00, 987 }, { 0xd401, 0x00, 1009 }, { 0xd801, 0x00, 1031 },
{ 0xdc01, 0x00, 1053 }, { 0xe001, 0x00, 1076 }, { 0xe401, 0x00, 1100 },
{ 0xe801, 0x00, 1124 }, { 0xec01, 0x00, 1149 }, { 0xf001, 0x00, 1174 },
{ 0xf401, 0x00, 1199 }, { 0xf801, 0x00, 1226 }, { 0xfc01, 0x00, 1253 }
};
#define nx_con_max_rumble_amp (1003)
static const struct nx_con_rumble_amp_data nx_con_rumble_amplitudes[] = {
/* high, low, amp */
{ 0x00, 0x0040, 0 },
{ 0x02, 0x8040, 10 }, { 0x04, 0x0041, 12 }, { 0x06, 0x8041, 14 },
{ 0x08, 0x0042, 17 }, { 0x0a, 0x8042, 20 }, { 0x0c, 0x0043, 24 },
{ 0x0e, 0x8043, 28 }, { 0x10, 0x0044, 33 }, { 0x12, 0x8044, 40 },
{ 0x14, 0x0045, 47 }, { 0x16, 0x8045, 56 }, { 0x18, 0x0046, 67 },
{ 0x1a, 0x8046, 80 }, { 0x1c, 0x0047, 95 }, { 0x1e, 0x8047, 112 },
{ 0x20, 0x0048, 117 }, { 0x22, 0x8048, 123 }, { 0x24, 0x0049, 128 },
{ 0x26, 0x8049, 134 }, { 0x28, 0x004a, 140 }, { 0x2a, 0x804a, 146 },
{ 0x2c, 0x004b, 152 }, { 0x2e, 0x804b, 159 }, { 0x30, 0x004c, 166 },
{ 0x32, 0x804c, 173 }, { 0x34, 0x004d, 181 }, { 0x36, 0x804d, 189 },
{ 0x38, 0x004e, 198 }, { 0x3a, 0x804e, 206 }, { 0x3c, 0x004f, 215 },
{ 0x3e, 0x804f, 225 }, { 0x40, 0x0050, 230 }, { 0x42, 0x8050, 235 },
{ 0x44, 0x0051, 240 }, { 0x46, 0x8051, 245 }, { 0x48, 0x0052, 251 },
{ 0x4a, 0x8052, 256 }, { 0x4c, 0x0053, 262 }, { 0x4e, 0x8053, 268 },
{ 0x50, 0x0054, 273 }, { 0x52, 0x8054, 279 }, { 0x54, 0x0055, 286 },
{ 0x56, 0x8055, 292 }, { 0x58, 0x0056, 298 }, { 0x5a, 0x8056, 305 },
{ 0x5c, 0x0057, 311 }, { 0x5e, 0x8057, 318 }, { 0x60, 0x0058, 325 },
{ 0x62, 0x8058, 332 }, { 0x64, 0x0059, 340 }, { 0x66, 0x8059, 347 },
{ 0x68, 0x005a, 355 }, { 0x6a, 0x805a, 362 }, { 0x6c, 0x005b, 370 },
{ 0x6e, 0x805b, 378 }, { 0x70, 0x005c, 387 }, { 0x72, 0x805c, 395 },
{ 0x74, 0x005d, 404 }, { 0x76, 0x805d, 413 }, { 0x78, 0x005e, 422 },
{ 0x7a, 0x805e, 431 }, { 0x7c, 0x005f, 440 }, { 0x7e, 0x805f, 450 },
{ 0x80, 0x0060, 460 }, { 0x82, 0x8060, 470 }, { 0x84, 0x0061, 480 },
{ 0x86, 0x8061, 491 }, { 0x88, 0x0062, 501 }, { 0x8a, 0x8062, 512 },
{ 0x8c, 0x0063, 524 }, { 0x8e, 0x8063, 535 }, { 0x90, 0x0064, 547 },
{ 0x92, 0x8064, 559 }, { 0x94, 0x0065, 571 }, { 0x96, 0x8065, 584 },
{ 0x98, 0x0066, 596 }, { 0x9a, 0x8066, 609 }, { 0x9c, 0x0067, 623 },
{ 0x9e, 0x8067, 636 }, { 0xa0, 0x0068, 650 }, { 0xa2, 0x8068, 665 },
{ 0xa4, 0x0069, 679 }, { 0xa6, 0x8069, 694 }, { 0xa8, 0x006a, 709 },
{ 0xaa, 0x806a, 725 }, { 0xac, 0x006b, 741 }, { 0xae, 0x806b, 757 },
{ 0xb0, 0x006c, 773 }, { 0xb2, 0x806c, 790 }, { 0xb4, 0x006d, 808 },
{ 0xb6, 0x806d, 825 }, { 0xb8, 0x006e, 843 }, { 0xba, 0x806e, 862 },
{ 0xbc, 0x006f, 881 }, { 0xbe, 0x806f, 900 }, { 0xc0, 0x0070, 920 },
{ 0xc2, 0x8070, 940 }, { 0xc4, 0x0071, 960 }, { 0xc6, 0x8071, 981 },
{ 0xc8, 0x0072, nx_con_max_rumble_amp }
};
static const u16 NX_CON_RUMBLE_DFLT_LOW_FREQ = 160;
static const u16 NX_CON_RUMBLE_DFLT_HIGH_FREQ = 320;
#endif /* IS_ENABLED(CONFIG_NINTENDO_FF) */
static const u16 NX_CON_RUMBLE_PERIOD_MS = 50;
/* States for controller state machine */
enum nx_con_state {
NX_CON_STATE_INIT,
NX_CON_STATE_READ,
NX_CON_STATE_REMOVED,
};
/* Controller type received as part of device info */
enum nx_con_type {
NX_CON_TYPE_JCL = 0x01,
NX_CON_TYPE_JCR = 0x02,
NX_CON_TYPE_PRO = 0x03,
NX_CON_TYPE_NESL = 0x09,
NX_CON_TYPE_NESR = 0x0A,
NX_CON_TYPE_SNES = 0x0B,
NX_CON_TYPE_GEN = 0x0D,
NX_CON_TYPE_N64 = 0x0C,
};
struct nx_con_stick_cal {
s32 max;
s32 min;
s32 center;
};
struct nx_con_imu_cal {
s16 offset[3];
s16 scale[3];
};
static const u32 NX_CON_BTN_Y = BIT(0);
static const u32 NX_CON_BTN_X = BIT(1);
static const u32 NX_CON_BTN_B = BIT(2);
static const u32 NX_CON_BTN_A = BIT(3);
static const u32 NX_CON_BTN_SR_R = BIT(4);
static const u32 NX_CON_BTN_SL_R = BIT(5);
static const u32 NX_CON_BTN_R = BIT(6);
static const u32 NX_CON_BTN_ZR = BIT(7);
static const u32 NX_CON_BTN_MINUS = BIT(8);
static const u32 NX_CON_BTN_PLUS = BIT(9);
static const u32 NX_CON_BTN_RSTICK = BIT(10);
static const u32 NX_CON_BTN_LSTICK = BIT(11);
static const u32 NX_CON_BTN_HOME = BIT(12);
static const u32 NX_CON_BTN_CAP = BIT(13);
static const u32 NX_CON_BTN_DOWN = BIT(16);
static const u32 NX_CON_BTN_UP = BIT(17);
static const u32 NX_CON_BTN_RIGHT = BIT(18);
static const u32 NX_CON_BTN_LEFT = BIT(19);
static const u32 NX_CON_BTN_SR_L = BIT(20);
static const u32 NX_CON_BTN_SL_L = BIT(21);
static const u32 NX_CON_BTN_L = BIT(22);
static const u32 NX_CON_BTN_ZL = BIT(23);
struct nx_con_button_mapping {
u32 code;
u32 bit;
};
/*
* D-pad is configured as buttons for the left Joy-Con only!
*/
static const struct nx_con_button_mapping left_joycon_button_mappings[] = {
{ BTN_TL, NX_CON_BTN_L, },
{ BTN_TL2, NX_CON_BTN_ZL, },
{ BTN_SELECT, NX_CON_BTN_MINUS, },
{ BTN_THUMBL, NX_CON_BTN_LSTICK, },
{ BTN_DPAD_UP, NX_CON_BTN_UP, },
{ BTN_DPAD_DOWN, NX_CON_BTN_DOWN, },
{ BTN_DPAD_LEFT, NX_CON_BTN_LEFT, },
{ BTN_DPAD_RIGHT, NX_CON_BTN_RIGHT, },
{ BTN_1, NX_CON_BTN_CAP, },
{ /* sentinel */ },
};
/*
* The unused *right*-side triggers become the SL/SR triggers for the *left*
* Joy-Con, if and only if we're not using a charging grip.
*/
static const struct nx_con_button_mapping left_joycon_s_button_mappings[] = {
{ BTN_TR, NX_CON_BTN_SL_L, },
{ BTN_TR2, NX_CON_BTN_SR_L, },
{ /* sentinel */ },
};
static const struct nx_con_button_mapping right_joycon_button_mappings[] = {
{ BTN_EAST, NX_CON_BTN_A, },
{ BTN_SOUTH, NX_CON_BTN_B, },
{ BTN_NORTH, NX_CON_BTN_X, },
{ BTN_WEST, NX_CON_BTN_Y, },
{ BTN_TR, NX_CON_BTN_R, },
{ BTN_TR2, NX_CON_BTN_ZR, },
{ BTN_START, NX_CON_BTN_PLUS, },
{ BTN_THUMBR, NX_CON_BTN_RSTICK, },
{ BTN_0, NX_CON_BTN_HOME, },
{ /* sentinel */ },
};
/*
* The unused *left*-side triggers become the SL/SR triggers for the *right*
* Joy-Con, if and only if we're not using a charging grip.
*/
static const struct nx_con_button_mapping right_joycon_s_button_mappings[] = {
{ BTN_TL, NX_CON_BTN_SL_R, },
{ BTN_TL2, NX_CON_BTN_SR_R, },
{ /* sentinel */ },
};
static const struct nx_con_button_mapping procon_button_mappings[] = {
{ BTN_EAST, NX_CON_BTN_A, },
{ BTN_SOUTH, NX_CON_BTN_B, },
{ BTN_NORTH, NX_CON_BTN_X, },
{ BTN_WEST, NX_CON_BTN_Y, },
{ BTN_TL, NX_CON_BTN_L, },
{ BTN_TR, NX_CON_BTN_R, },
{ BTN_TL2, NX_CON_BTN_ZL, },
{ BTN_TR2, NX_CON_BTN_ZR, },
{ BTN_SELECT, NX_CON_BTN_MINUS, },
{ BTN_START, NX_CON_BTN_PLUS, },
{ BTN_THUMBL, NX_CON_BTN_LSTICK, },
{ BTN_THUMBR, NX_CON_BTN_RSTICK, },
{ BTN_0, NX_CON_BTN_HOME, },
{ BTN_1, NX_CON_BTN_CAP, },
{ /* sentinel */ },
};
static const struct nx_con_button_mapping nescon_button_mappings[] = {
{ BTN_SOUTH, NX_CON_BTN_A, },
{ BTN_EAST, NX_CON_BTN_B, },
{ BTN_TL, NX_CON_BTN_L, },
{ BTN_TR, NX_CON_BTN_R, },
{ BTN_SELECT, NX_CON_BTN_MINUS, },
{ BTN_START, NX_CON_BTN_PLUS, },
{ /* sentinel */ },
};
static const struct nx_con_button_mapping snescon_button_mappings[] = {
{ BTN_SOUTH, NX_CON_BTN_A, },
{ BTN_EAST, NX_CON_BTN_B, },
{ BTN_NORTH, NX_CON_BTN_X, },
{ BTN_WEST, NX_CON_BTN_Y, },
{ BTN_TL, NX_CON_BTN_L, },
{ BTN_TR, NX_CON_BTN_R, },
{ BTN_TL2, NX_CON_BTN_ZL, },
{ BTN_TR2, NX_CON_BTN_ZR, },
{ BTN_SELECT, NX_CON_BTN_MINUS, },
{ BTN_START, NX_CON_BTN_PLUS, },
{ /* sentinel */ },
};
/*
* "A", "B", and "C" are mapped positionally, rather than by label (e.g., "A"
* gets assigned to BTN_EAST instead of BTN_A).
*/
static const struct nx_con_button_mapping gencon_button_mappings[] = {
{ BTN_SOUTH, NX_CON_BTN_A, },
{ BTN_EAST, NX_CON_BTN_B, },
{ BTN_WEST, NX_CON_BTN_R, },
{ BTN_SELECT, NX_CON_BTN_ZR, },
{ BTN_START, NX_CON_BTN_PLUS, },
{ BTN_0, NX_CON_BTN_HOME, },
{ BTN_1, NX_CON_BTN_CAP, },
{ /* sentinel */ },
};
/*
* N64's C buttons get assigned to d-pad directions and registered as buttons.
*/
static const struct nx_con_button_mapping n64con_button_mappings[] = {
{ BTN_A, NX_CON_BTN_A, },
{ BTN_B, NX_CON_BTN_B, },
{ BTN_Z, NX_CON_BTN_ZL, },
{ BTN_TL, NX_CON_BTN_L, },
{ BTN_TR, NX_CON_BTN_R, },
{ BTN_TR2, NX_CON_BTN_LSTICK, },
{ BTN_START, NX_CON_BTN_PLUS, },
{ BTN_DPAD_UP, NX_CON_BTN_Y, },
{ BTN_DPAD_DOWN, NX_CON_BTN_ZR, },
{ BTN_DPAD_LEFT, NX_CON_BTN_X, },
{ BTN_DPAD_RIGHT, NX_CON_BTN_MINUS, },
{ BTN_0, NX_CON_BTN_HOME, },
{ BTN_1, NX_CON_BTN_CAP, },
{ /* sentinel */ },
};
enum nx_con_msg_type {
NX_CON_MSG_TYPE_NONE,
NX_CON_MSG_TYPE_USB,
NX_CON_MSG_TYPE_SUBCMD,
};
struct nx_con_rumble_output {
u8 output_id;
u8 packet_num;
u8 rumble_data[8];
} __packed;
struct nx_con_subcmd_request {
u8 output_id; /* must be 0x01 for subcommand, 0x10 for rumble only */
u8 packet_num; /* incremented every send */
u8 rumble_data[8];
u8 subcmd_id;
u8 data[]; /* length depends on the subcommand */
} __packed;
struct nx_con_subcmd_reply {
u8 ack; /* MSB 1 for ACK, 0 for NACK */
u8 id; /* id of requested subcmd */
u8 data[]; /* will be at most 35 bytes */
} __packed;
struct nx_con_imu_data {
s16 accel_x;
s16 accel_y;
s16 accel_z;
s16 gyro_x;
s16 gyro_y;
s16 gyro_z;
} __packed;
struct nx_con_input_report {
u8 id;
u8 timer;
u8 bat_con; /* battery and connection info */
u8 button_status[3];
u8 left_stick[3];
u8 right_stick[3];
u8 vibrator_report;
union {
struct nx_con_subcmd_reply subcmd_reply;
/* IMU input reports contain 3 samples */
u8 imu_raw_bytes[sizeof(struct nx_con_imu_data) * 3];
};
} __packed;
#define NX_CON_MAX_RESP_SIZE (sizeof(struct nx_con_input_report) + 35)
#define NX_CON_RUMBLE_DATA_SIZE 8
#define NX_CON_RUMBLE_QUEUE_SIZE 8
static const unsigned short NX_CON_RUMBLE_ZERO_AMP_PKT_CNT = 5;
/* for compat with kernels before 5.16 */
#ifndef LED_FUNCTION_PLAYER1
#define LED_FUNCTION_PLAYER1 "player-1"
#define LED_FUNCTION_PLAYER2 "player-2"
#define LED_FUNCTION_PLAYER3 "player-3"
#define LED_FUNCTION_PLAYER4 "player-4"
#define LED_FUNCTION_PLAYER5 "player-5"
#endif
static const char * nx_con_player_led_names[] = {
LED_FUNCTION_PLAYER1,
LED_FUNCTION_PLAYER2,
LED_FUNCTION_PLAYER3,
LED_FUNCTION_PLAYER4,
};
#define NX_CON_NUM_LEDS ARRAY_SIZE(nx_con_player_led_names)
/* Each physical controller is associated with a nx_con struct */
struct nx_con {
struct hid_device *hdev;
struct input_dev *idev;
struct led_classdev leds[NX_CON_NUM_LEDS]; /* player leds */
struct led_classdev home_led;
enum nx_con_state state;
spinlock_t lock;
u8 mac_addr[6];
char *mac_addr_str;
enum nx_con_type type;
/* The following members are used for synchronous sends/receives */
enum nx_con_msg_type msg_type;
u8 subcmd_num;
struct mutex output_mutex;
u8 input_buf[NX_CON_MAX_RESP_SIZE];
wait_queue_head_t wait;
bool received_resp;
u8 usb_ack_match;
u8 subcmd_ack_match;
bool received_input_report;
unsigned int last_subcmd_sent_msecs;
/* factory calibration data */
struct nx_con_stick_cal left_stick_cal_x;
struct nx_con_stick_cal left_stick_cal_y;
struct nx_con_stick_cal right_stick_cal_x;
struct nx_con_stick_cal right_stick_cal_y;
struct nx_con_imu_cal accel_cal;
struct nx_con_imu_cal gyro_cal;
/* prevents needlessly recalculating these divisors every sample */
s32 imu_cal_accel_divisor[3];
s32 imu_cal_gyro_divisor[3];
/* power supply data */
struct power_supply *battery;
struct power_supply_desc battery_desc;
u8 battery_capacity;
bool battery_charging;
bool host_powered;
/* rumble */
u8 rumble_data[NX_CON_RUMBLE_QUEUE_SIZE][NX_CON_RUMBLE_DATA_SIZE];
int rumble_queue_head;
int rumble_queue_tail;
struct workqueue_struct *rumble_queue;
struct work_struct rumble_worker;
unsigned int rumble_msecs;
u16 rumble_ll_freq;
u16 rumble_lh_freq;
u16 rumble_rl_freq;
u16 rumble_rh_freq;
unsigned short rumble_zero_countdown;
/* imu */
struct input_dev *imu_idev;
bool imu_first_packet_received; /* helps in initiating timestamp */
unsigned int imu_timestamp_us; /* timestamp we report to userspace */
unsigned int imu_last_pkt_ms; /* used to calc imu report delta */
/* the following are used to track the average imu report time delta */
unsigned int imu_delta_samples_count;
unsigned int imu_delta_samples_sum;
unsigned int imu_avg_delta_ms;
};
/*
* Controller device helpers
*
* These look at the device ID known to the HID subsystem to identify a device,
* but take caution: some NSO devices lie about themselves (NES Joy-Cons and
* Sega Genesis controller). See type helpers below.
*
* These helpers are most useful early during the HID probe or in conjunction
* with the capability helpers below.
*/
static inline bool nx_con_device_is_left_joycon(struct nx_con *con)
{
return con->hdev->product == USB_DEVICE_ID_NINTENDO_JOYCONL;
}
static inline bool nx_con_device_is_right_joycon(struct nx_con *con)
{
return con->hdev->product == USB_DEVICE_ID_NINTENDO_JOYCONR;
}
static inline bool nx_con_device_is_procon(struct nx_con *con)
{
return con->hdev->product == USB_DEVICE_ID_NINTENDO_PROCON;
}
static inline bool nx_con_device_is_chrggrip(struct nx_con *con)
{
return con->hdev->product == USB_DEVICE_ID_NINTENDO_CHRGGRIP;
}
static inline bool nx_con_device_is_snescon(struct nx_con *con)
{
return con->hdev->product == USB_DEVICE_ID_NINTENDO_SNESCON;
}
static inline bool nx_con_device_is_gencon(struct nx_con *con)
{
return con->hdev->product == USB_DEVICE_ID_NINTENDO_GENCON;
}
static inline bool nx_con_device_is_n64con(struct nx_con *con)
{
return con->hdev->product == USB_DEVICE_ID_NINTENDO_N64CON;
}
static inline bool nx_con_device_has_usb(struct nx_con *con)
{
return nx_con_device_is_procon(con) ||
nx_con_device_is_chrggrip(con) ||
nx_con_device_is_snescon(con) ||
nx_con_device_is_gencon(con) ||
nx_con_device_is_n64con(con);
}
/*
* Controller type helpers
*
* These are slightly different than the device-ID-based helpers above. They are
* generally more reliable, since they can distinguish between, e.g., Genesis
* versus SNES, or NES Joy-Cons versus regular Switch Joy-Cons. They're most
* useful for reporting available inputs. For other kinds of distinctions, see
* the capability helpers below.
*
* They have two major drawbacks: (1) they're not available until after we set
* the reporting method and then request the device info; (2) they can't
* distinguish all controllers (like the Charging Grip from the Pro controller.)
*/
static inline bool nx_con_type_is_left_joycon(struct nx_con *con)
{
return con->type == NX_CON_TYPE_JCL;
}
static inline bool nx_con_type_is_right_joycon(struct nx_con *con)
{
return con->type == NX_CON_TYPE_JCR;
}
static inline bool nx_con_type_is_procon(struct nx_con *con)
{
return con->type == NX_CON_TYPE_PRO;
}
static inline bool nx_con_type_is_snescon(struct nx_con *con)
{
return con->type == NX_CON_TYPE_SNES;
}
static inline bool nx_con_type_is_gencon(struct nx_con *con)
{
return con->type == NX_CON_TYPE_GEN;
}
static inline bool nx_con_type_is_n64con(struct nx_con *con)
{
return con->type == NX_CON_TYPE_N64;
}
static inline bool nx_con_type_is_left_nescon(struct nx_con *con)
{
return con->type == NX_CON_TYPE_NESL;
}
static inline bool nx_con_type_is_right_nescon(struct nx_con *con)
{
return con->type == NX_CON_TYPE_NESR;
}
static inline bool nx_con_type_has_left_controls(struct nx_con *con)
{
return nx_con_type_is_left_joycon(con) ||
nx_con_type_is_procon(con);
}
static inline bool nx_con_type_has_right_controls(struct nx_con *con)
{
return nx_con_type_is_right_joycon(con) ||
nx_con_type_is_procon(con);
}
static inline bool nx_con_type_is_any_joycon(struct nx_con *con)
{
return nx_con_type_is_left_joycon(con) ||
nx_con_type_is_right_joycon(con) ||
nx_con_device_is_chrggrip(con);
}
static inline bool nx_con_type_is_any_nescon(struct nx_con *con)
{
return nx_con_type_is_left_nescon(con) ||
nx_con_type_is_right_nescon(con);
}
/*
* Controller capability helpers
*
* These helpers combine the use of the helpers above to detect certain
* capabilities during initialization. They are always accurate but (since they
* use type helpers) cannot be used early in the HID probe.
*/
static inline bool nx_con_has_imu(struct nx_con *con)
{
return nx_con_device_is_chrggrip(con) ||
nx_con_type_is_any_joycon(con) ||
nx_con_type_is_procon(con);
}
static inline bool nx_con_has_joysticks(struct nx_con *con)
{
return nx_con_device_is_chrggrip(con) ||
nx_con_type_is_any_joycon(con) ||
nx_con_type_is_procon(con) ||
nx_con_type_is_n64con(con);
}
static inline bool nx_con_has_rumble(struct nx_con *con)
{
return nx_con_device_is_chrggrip(con) ||
nx_con_type_is_any_joycon(con) ||
nx_con_type_is_procon(con) ||
nx_con_type_is_n64con(con);
}
static int __nx_con_hid_send(struct hid_device *hdev, u8 *data, size_t len)
{
u8 *buf;
int ret;
if (!(buf = kmemdup(data, len, GFP_KERNEL)))
return -ENOMEM;
if ((ret = hid_hw_output_report(hdev, buf, len)) < 0)
hid_dbg(hdev, "Failed to send output report ret=%d\n", ret);
kfree(buf);
return ret;
}
static void nx_con_wait_for_input_report(struct nx_con *con)
{
/*
* If we are in the proper reporting mode, wait for an input
* report prior to sending the subcommand. This improves
* reliability considerably.
*/
if (con->state == NX_CON_STATE_READ) {
unsigned long flags;
spin_lock_irqsave(&con->lock, flags);
con->received_input_report = false;
spin_unlock_irqrestore(&con->lock, flags);
/* We will still proceed, even with a timeout here */
if (!wait_event_timeout(con->wait, con->received_input_report, HZ / 4))
hid_warn(con->hdev, "timeout waiting for input report\n");
}
}
/*
* Sending subcommands and/or rumble data at too high a rate can cause bluetooth
* controller disconnections.
*/
static void nx_con_enforce_subcmd_rate(struct nx_con *con)
{
static const unsigned int max_subcmd_rate_ms = 25;
unsigned int current_ms = jiffies_to_msecs(jiffies);
unsigned int delta_ms = current_ms - con->last_subcmd_sent_msecs;
while (delta_ms < max_subcmd_rate_ms &&
con->state == NX_CON_STATE_READ) {
nx_con_wait_for_input_report(con);
current_ms = jiffies_to_msecs(jiffies);
delta_ms = current_ms - con->last_subcmd_sent_msecs;
}
con->last_subcmd_sent_msecs = current_ms;
}
static int nx_con_hid_send_sync(struct nx_con *con, u8 *data, size_t len, u32 timeout)
{
int ret;
int tries = 2;
/*
* The controller occasionally seems to drop subcommands. In testing,
* doing one retry after a timeout appears to always work.
*/
while (tries--) {
nx_con_enforce_subcmd_rate(con);
if ((ret = __nx_con_hid_send(con->hdev, data, len)) < 0) {
memset(con->input_buf, 0, NX_CON_MAX_RESP_SIZE);
return ret;
}
if (!wait_event_timeout(con->wait, con->received_resp, timeout)) {
hid_dbg(con->hdev, "synchronous send/receive timed out\n");
if (tries) {
hid_dbg(con->hdev, "retrying sync send after timeout\n");
}
memset(con->input_buf, 0, NX_CON_MAX_RESP_SIZE);
ret = -ETIMEDOUT;
} else {
ret = 0;
break;
}
}
con->received_resp = false;
return ret;
}
static int nx_con_send_usb(struct nx_con *con, u8 cmd, u32 timeout)
{
int ret;
u8 buf[64] = {NX_CON_OUTPUT_USB_CMD};
buf[1] = cmd;
con->usb_ack_match = cmd;
con->msg_type = NX_CON_MSG_TYPE_USB;
if ((ret = nx_con_hid_send_sync(con, buf, sizeof(buf), timeout)))
hid_dbg(con->hdev, "send usb command failed; ret=%d\n", ret);
return ret;
}
static int nx_con_send_subcmd(struct nx_con *con,
struct nx_con_subcmd_request *subcmd,
size_t data_len,
u32 timeout)
{
int ret;
unsigned long flags;
spin_lock_irqsave(&con->lock, flags);
/*
* If the controller has been removed, just return ENODEV so the LED
* subsystem doesn't print invalid errors on removal.
*/
if (con->state == NX_CON_STATE_REMOVED) {
spin_unlock_irqrestore(&con->lock, flags);
return -ENODEV;
}
memcpy(subcmd->rumble_data, con->rumble_data[con->rumble_queue_tail],
NX_CON_RUMBLE_DATA_SIZE);
spin_unlock_irqrestore(&con->lock, flags);
subcmd->output_id = NX_CON_OUTPUT_RUMBLE_AND_SUBCMD;
subcmd->packet_num = con->subcmd_num;
if (++con->subcmd_num > 0xF)
con->subcmd_num = 0;
con->subcmd_ack_match = subcmd->subcmd_id;
con->msg_type = NX_CON_MSG_TYPE_SUBCMD;
if ((ret = nx_con_hid_send_sync(con,
(u8 *)subcmd,
sizeof(*subcmd) + data_len,
timeout)) < 0)
hid_dbg(con->hdev, "send subcommand failed; ret=%d\n", ret);
else
ret = 0;
return ret;
}
/* Supply nibbles for flash and on. Ones correspond to active */
static int nx_con_set_player_leds(struct nx_con *con, u8 flash, u8 on)
{
struct nx_con_subcmd_request *req;
u8 buffer[sizeof(*req) + 1] = { 0 };
req = (struct nx_con_subcmd_request *)buffer;
req->subcmd_id = NX_CON_SUBCMD_SET_PLAYER_LIGHTS;
req->data[0] = (flash << 4) | on;
hid_dbg(con->hdev, "setting player leds\n");
return nx_con_send_subcmd(con, req, 1, HZ/4);
}
static int nx_con_request_spi_flash_read(struct nx_con *con,
u32 start_addr,
u8 size,
u8 **reply)
{
struct nx_con_subcmd_request *req;
struct nx_con_input_report *report;
u8 buffer[sizeof(*req) + 5] = { 0 };
u8 *data;
int ret;
if (!reply)
return -EINVAL;
req = (struct nx_con_subcmd_request *)buffer;
req->subcmd_id = NX_CON_SUBCMD_SPI_FLASH_READ;
data = req->data;
put_unaligned_le32(start_addr, data);
data[4] = size;
hid_dbg(con->hdev, "requesting SPI flash data\n");
if ((ret = nx_con_send_subcmd(con, req, 5, HZ))) {
hid_err(con->hdev, "failed reading SPI flash; ret=%d\n", ret);
} else {
report = (struct nx_con_input_report *)con->input_buf;
/* The read data starts at the 6th byte */
*reply = &report->subcmd_reply.data[5];
}
return ret;
}
/*
* User calibration's presence is denoted with a magic byte preceding it.
* returns 0 if magic val is present, 1 if not present, < 0 on error
*/
static int nx_con_check_for_cal_magic(struct nx_con *con, u32 flash_addr)
{
int ret;
u8 *reply;
if ((ret = nx_con_request_spi_flash_read(con,
flash_addr,
NX_CON_CAL_USR_MAGIC_SIZE,
&reply)))
return ret;
return reply[0] != NX_CON_CAL_USR_MAGIC_0 ||
reply[1] != NX_CON_CAL_USR_MAGIC_1;
}
static int nx_con_read_stick_calibration(struct nx_con *con,
u16 cal_addr,
struct nx_con_stick_cal *cal_x,
struct nx_con_stick_cal *cal_y,
bool left_stick)