-
Notifications
You must be signed in to change notification settings - Fork 54k
/
hid-nintendo.c
2823 lines (2477 loc) · 86.2 KB
/
hid-nintendo.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 Joy-Cons and Pro 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]>
* Copyright (c) 2023 Ryan McClelland <[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, Sega Genesis, and N64.
*
* 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 <linux/unaligned.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/kernel.h>
#include <linux/hid.h>
#include <linux/idr.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 JC_OUTPUT_RUMBLE_AND_SUBCMD 0x01
#define JC_OUTPUT_FW_UPDATE_PKT 0x03
#define JC_OUTPUT_RUMBLE_ONLY 0x10
#define JC_OUTPUT_MCU_DATA 0x11
#define JC_OUTPUT_USB_CMD 0x80
/* Subcommand IDs */
#define JC_SUBCMD_STATE 0x00
#define JC_SUBCMD_MANUAL_BT_PAIRING 0x01
#define JC_SUBCMD_REQ_DEV_INFO 0x02
#define JC_SUBCMD_SET_REPORT_MODE 0x03
#define JC_SUBCMD_TRIGGERS_ELAPSED 0x04
#define JC_SUBCMD_GET_PAGE_LIST_STATE 0x05
#define JC_SUBCMD_SET_HCI_STATE 0x06
#define JC_SUBCMD_RESET_PAIRING_INFO 0x07
#define JC_SUBCMD_LOW_POWER_MODE 0x08
#define JC_SUBCMD_SPI_FLASH_READ 0x10
#define JC_SUBCMD_SPI_FLASH_WRITE 0x11
#define JC_SUBCMD_RESET_MCU 0x20
#define JC_SUBCMD_SET_MCU_CONFIG 0x21
#define JC_SUBCMD_SET_MCU_STATE 0x22
#define JC_SUBCMD_SET_PLAYER_LIGHTS 0x30
#define JC_SUBCMD_GET_PLAYER_LIGHTS 0x31
#define JC_SUBCMD_SET_HOME_LIGHT 0x38
#define JC_SUBCMD_ENABLE_IMU 0x40
#define JC_SUBCMD_SET_IMU_SENSITIVITY 0x41
#define JC_SUBCMD_WRITE_IMU_REG 0x42
#define JC_SUBCMD_READ_IMU_REG 0x43
#define JC_SUBCMD_ENABLE_VIBRATION 0x48
#define JC_SUBCMD_GET_REGULATED_VOLTAGE 0x50
/* Input Reports */
#define JC_INPUT_BUTTON_EVENT 0x3F
#define JC_INPUT_SUBCMD_REPLY 0x21
#define JC_INPUT_IMU_DATA 0x30
#define JC_INPUT_MCU_DATA 0x31
#define JC_INPUT_USB_RESPONSE 0x81
/* Feature Reports */
#define JC_FEATURE_LAST_SUBCMD 0x02
#define JC_FEATURE_OTA_FW_UPGRADE 0x70
#define JC_FEATURE_SETUP_MEM_READ 0x71
#define JC_FEATURE_MEM_READ 0x72
#define JC_FEATURE_ERASE_MEM_SECTOR 0x73
#define JC_FEATURE_MEM_WRITE 0x74
#define JC_FEATURE_LAUNCH 0x75
/* USB Commands */
#define JC_USB_CMD_CONN_STATUS 0x01
#define JC_USB_CMD_HANDSHAKE 0x02
#define JC_USB_CMD_BAUDRATE_3M 0x03
#define JC_USB_CMD_NO_TIMEOUT 0x04
#define JC_USB_CMD_EN_TIMEOUT 0x05
#define JC_USB_RESET 0x06
#define JC_USB_PRE_HANDSHAKE 0x91
#define JC_USB_SEND_UART 0x92
/* Magic value denoting presence of user calibration */
#define JC_CAL_USR_MAGIC_0 0xB2
#define JC_CAL_USR_MAGIC_1 0xA1
#define JC_CAL_USR_MAGIC_SIZE 2
/* SPI storage addresses of user calibration data */
#define JC_CAL_USR_LEFT_MAGIC_ADDR 0x8010
#define JC_CAL_USR_LEFT_DATA_ADDR 0x8012
#define JC_CAL_USR_LEFT_DATA_END 0x801A
#define JC_CAL_USR_RIGHT_MAGIC_ADDR 0x801B
#define JC_CAL_USR_RIGHT_DATA_ADDR 0x801D
#define JC_CAL_STICK_DATA_SIZE \
(JC_CAL_USR_LEFT_DATA_END - JC_CAL_USR_LEFT_DATA_ADDR + 1)
/* SPI storage addresses of factory calibration data */
#define JC_CAL_FCT_DATA_LEFT_ADDR 0x603d
#define JC_CAL_FCT_DATA_RIGHT_ADDR 0x6046
/* SPI storage addresses of IMU factory calibration data */
#define JC_IMU_CAL_FCT_DATA_ADDR 0x6020
#define JC_IMU_CAL_FCT_DATA_END 0x6037
#define JC_IMU_CAL_DATA_SIZE \
(JC_IMU_CAL_FCT_DATA_END - JC_IMU_CAL_FCT_DATA_ADDR + 1)
/* SPI storage addresses of IMU user calibration data */
#define JC_IMU_CAL_USR_MAGIC_ADDR 0x8026
#define JC_IMU_CAL_USR_DATA_ADDR 0x8028
/* The raw analog joystick values will be mapped in terms of this magnitude */
#define JC_MAX_STICK_MAG 32767
#define JC_STICK_FUZZ 250
#define JC_STICK_FLAT 500
/* Hat values for pro controller's d-pad */
#define JC_MAX_DPAD_MAG 1
#define JC_DPAD_FUZZ 0
#define JC_DPAD_FLAT 0
/* Under most circumstances IMU reports are pushed every 15ms; use as default */
#define JC_IMU_DFLT_AVG_DELTA_MS 15
/* How many samples to sum before calculating average IMU report delta */
#define JC_IMU_SAMPLES_PER_DELTA_AVG 300
/* Controls how many dropped IMU packets at once trigger a warning message */
#define JC_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 JC_IMU_MAX_ACCEL_MAG 32767
#define JC_IMU_ACCEL_RES_PER_G 4096
#define JC_IMU_ACCEL_FUZZ 10
#define JC_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 JC_IMU_PREC_RANGE_SCALE 1000
/* Note: change mag and res_per_dps if prec_range_scale is ever altered */
#define JC_IMU_MAX_GYRO_MAG 32767000 /* (2^16-1)*1000 */
#define JC_IMU_GYRO_RES_PER_DPS 14247 /* (14.247*1000) */
#define JC_IMU_GYRO_FUZZ 10
#define JC_IMU_GYRO_FLAT 0
/* frequency/amplitude tables for rumble */
struct joycon_rumble_freq_data {
u16 high;
u8 low;
u16 freq; /* Hz*/
};
struct joycon_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 joycon_rumble_freq_data joycon_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 joycon_max_rumble_amp (1003)
static const struct joycon_rumble_amp_data joycon_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, joycon_max_rumble_amp }
};
static const u16 JC_RUMBLE_DFLT_LOW_FREQ = 160;
static const u16 JC_RUMBLE_DFLT_HIGH_FREQ = 320;
static const unsigned short JC_RUMBLE_ZERO_AMP_PKT_CNT = 5;
#endif /* IS_ENABLED(CONFIG_NINTENDO_FF) */
static const u16 JC_RUMBLE_PERIOD_MS = 50;
/* States for controller state machine */
enum joycon_ctlr_state {
JOYCON_CTLR_STATE_INIT,
JOYCON_CTLR_STATE_READ,
JOYCON_CTLR_STATE_REMOVED,
};
/* Controller type received as part of device info */
enum joycon_ctlr_type {
JOYCON_CTLR_TYPE_JCL = 0x01,
JOYCON_CTLR_TYPE_JCR = 0x02,
JOYCON_CTLR_TYPE_PRO = 0x03,
JOYCON_CTLR_TYPE_NESL = 0x09,
JOYCON_CTLR_TYPE_NESR = 0x0A,
JOYCON_CTLR_TYPE_SNES = 0x0B,
JOYCON_CTLR_TYPE_GEN = 0x0D,
JOYCON_CTLR_TYPE_N64 = 0x0C,
};
struct joycon_stick_cal {
s32 max;
s32 min;
s32 center;
};
struct joycon_imu_cal {
s16 offset[3];
s16 scale[3];
};
/*
* All the controller's button values are stored in a u32.
* They can be accessed with bitwise ANDs.
*/
#define JC_BTN_Y BIT(0)
#define JC_BTN_X BIT(1)
#define JC_BTN_B BIT(2)
#define JC_BTN_A BIT(3)
#define JC_BTN_SR_R BIT(4)
#define JC_BTN_SL_R BIT(5)
#define JC_BTN_R BIT(6)
#define JC_BTN_ZR BIT(7)
#define JC_BTN_MINUS BIT(8)
#define JC_BTN_PLUS BIT(9)
#define JC_BTN_RSTICK BIT(10)
#define JC_BTN_LSTICK BIT(11)
#define JC_BTN_HOME BIT(12)
#define JC_BTN_CAP BIT(13) /* capture button */
#define JC_BTN_DOWN BIT(16)
#define JC_BTN_UP BIT(17)
#define JC_BTN_RIGHT BIT(18)
#define JC_BTN_LEFT BIT(19)
#define JC_BTN_SR_L BIT(20)
#define JC_BTN_SL_L BIT(21)
#define JC_BTN_L BIT(22)
#define JC_BTN_ZL BIT(23)
struct joycon_ctlr_button_mapping {
u32 code;
u32 bit;
};
/*
* D-pad is configured as buttons for the left Joy-Con only!
*/
static const struct joycon_ctlr_button_mapping left_joycon_button_mappings[] = {
{ BTN_TL, JC_BTN_L, },
{ BTN_TL2, JC_BTN_ZL, },
{ BTN_SELECT, JC_BTN_MINUS, },
{ BTN_THUMBL, JC_BTN_LSTICK, },
{ BTN_DPAD_UP, JC_BTN_UP, },
{ BTN_DPAD_DOWN, JC_BTN_DOWN, },
{ BTN_DPAD_LEFT, JC_BTN_LEFT, },
{ BTN_DPAD_RIGHT, JC_BTN_RIGHT, },
{ BTN_Z, JC_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 joycon_ctlr_button_mapping left_joycon_s_button_mappings[] = {
{ BTN_TR, JC_BTN_SL_L, },
{ BTN_TR2, JC_BTN_SR_L, },
{ /* sentinel */ },
};
static const struct joycon_ctlr_button_mapping right_joycon_button_mappings[] = {
{ BTN_EAST, JC_BTN_A, },
{ BTN_SOUTH, JC_BTN_B, },
{ BTN_NORTH, JC_BTN_X, },
{ BTN_WEST, JC_BTN_Y, },
{ BTN_TR, JC_BTN_R, },
{ BTN_TR2, JC_BTN_ZR, },
{ BTN_START, JC_BTN_PLUS, },
{ BTN_THUMBR, JC_BTN_RSTICK, },
{ BTN_MODE, JC_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 joycon_ctlr_button_mapping right_joycon_s_button_mappings[] = {
{ BTN_TL, JC_BTN_SL_R, },
{ BTN_TL2, JC_BTN_SR_R, },
{ /* sentinel */ },
};
static const struct joycon_ctlr_button_mapping procon_button_mappings[] = {
{ BTN_EAST, JC_BTN_A, },
{ BTN_SOUTH, JC_BTN_B, },
{ BTN_NORTH, JC_BTN_X, },
{ BTN_WEST, JC_BTN_Y, },
{ BTN_TL, JC_BTN_L, },
{ BTN_TR, JC_BTN_R, },
{ BTN_TL2, JC_BTN_ZL, },
{ BTN_TR2, JC_BTN_ZR, },
{ BTN_SELECT, JC_BTN_MINUS, },
{ BTN_START, JC_BTN_PLUS, },
{ BTN_THUMBL, JC_BTN_LSTICK, },
{ BTN_THUMBR, JC_BTN_RSTICK, },
{ BTN_MODE, JC_BTN_HOME, },
{ BTN_Z, JC_BTN_CAP, },
{ /* sentinel */ },
};
static const struct joycon_ctlr_button_mapping nescon_button_mappings[] = {
{ BTN_SOUTH, JC_BTN_A, },
{ BTN_EAST, JC_BTN_B, },
{ BTN_TL, JC_BTN_L, },
{ BTN_TR, JC_BTN_R, },
{ BTN_SELECT, JC_BTN_MINUS, },
{ BTN_START, JC_BTN_PLUS, },
{ /* sentinel */ },
};
static const struct joycon_ctlr_button_mapping snescon_button_mappings[] = {
{ BTN_EAST, JC_BTN_A, },
{ BTN_SOUTH, JC_BTN_B, },
{ BTN_NORTH, JC_BTN_X, },
{ BTN_WEST, JC_BTN_Y, },
{ BTN_TL, JC_BTN_L, },
{ BTN_TR, JC_BTN_R, },
{ BTN_TL2, JC_BTN_ZL, },
{ BTN_TR2, JC_BTN_ZR, },
{ BTN_SELECT, JC_BTN_MINUS, },
{ BTN_START, JC_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 joycon_ctlr_button_mapping gencon_button_mappings[] = {
{ BTN_SOUTH, JC_BTN_A, },
{ BTN_EAST, JC_BTN_B, },
{ BTN_WEST, JC_BTN_R, },
{ BTN_SELECT, JC_BTN_ZR, },
{ BTN_START, JC_BTN_PLUS, },
{ BTN_MODE, JC_BTN_HOME, },
{ BTN_Z, JC_BTN_CAP, },
{ /* sentinel */ },
};
/*
* N64's C buttons get assigned to d-pad directions and registered as buttons.
*/
static const struct joycon_ctlr_button_mapping n64con_button_mappings[] = {
{ BTN_A, JC_BTN_A, },
{ BTN_B, JC_BTN_B, },
{ BTN_TL2, JC_BTN_ZL, }, /* Z */
{ BTN_TL, JC_BTN_L, },
{ BTN_TR, JC_BTN_R, },
{ BTN_TR2, JC_BTN_LSTICK, }, /* ZR */
{ BTN_START, JC_BTN_PLUS, },
{ BTN_SELECT, JC_BTN_Y, }, /* C UP */
{ BTN_X, JC_BTN_ZR, }, /* C DOWN */
{ BTN_Y, JC_BTN_X, }, /* C LEFT */
{ BTN_C, JC_BTN_MINUS, }, /* C RIGHT */
{ BTN_MODE, JC_BTN_HOME, },
{ BTN_Z, JC_BTN_CAP, },
{ /* sentinel */ },
};
enum joycon_msg_type {
JOYCON_MSG_TYPE_NONE,
JOYCON_MSG_TYPE_USB,
JOYCON_MSG_TYPE_SUBCMD,
};
struct joycon_rumble_output {
u8 output_id;
u8 packet_num;
u8 rumble_data[8];
} __packed;
struct joycon_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 joycon_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 joycon_imu_data {
s16 accel_x;
s16 accel_y;
s16 accel_z;
s16 gyro_x;
s16 gyro_y;
s16 gyro_z;
} __packed;
struct joycon_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 joycon_subcmd_reply subcmd_reply;
/* IMU input reports contain 3 samples */
u8 imu_raw_bytes[sizeof(struct joycon_imu_data) * 3];
};
} __packed;
#define JC_MAX_RESP_SIZE (sizeof(struct joycon_input_report) + 35)
#define JC_RUMBLE_DATA_SIZE 8
#define JC_RUMBLE_QUEUE_SIZE 8
static const char * const joycon_player_led_names[] = {
LED_FUNCTION_PLAYER1,
LED_FUNCTION_PLAYER2,
LED_FUNCTION_PLAYER3,
LED_FUNCTION_PLAYER4,
};
#define JC_NUM_LEDS ARRAY_SIZE(joycon_player_led_names)
#define JC_NUM_LED_PATTERNS 8
/* Taken from https://www.nintendo.com/my/support/qa/detail/33822 */
static const enum led_brightness joycon_player_led_patterns[JC_NUM_LED_PATTERNS][JC_NUM_LEDS] = {
{ 1, 0, 0, 0 },
{ 1, 1, 0, 0 },
{ 1, 1, 1, 0 },
{ 1, 1, 1, 1 },
{ 1, 0, 0, 1 },
{ 1, 0, 1, 0 },
{ 1, 0, 1, 1 },
{ 0, 1, 1, 0 },
};
/* Each physical controller is associated with a joycon_ctlr struct */
struct joycon_ctlr {
struct hid_device *hdev;
struct input_dev *input;
u32 player_id;
struct led_classdev leds[JC_NUM_LEDS]; /* player leds */
struct led_classdev home_led;
enum joycon_ctlr_state ctlr_state;
spinlock_t lock;
u8 mac_addr[6];
char *mac_addr_str;
enum joycon_ctlr_type ctlr_type;
/* The following members are used for synchronous sends/receives */
enum joycon_msg_type msg_type;
u8 subcmd_num;
struct mutex output_mutex;
u8 input_buf[JC_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_input_report_msecs;
unsigned int last_subcmd_sent_msecs;
unsigned int consecutive_valid_report_deltas;
/* factory calibration data */
struct joycon_stick_cal left_stick_cal_x;
struct joycon_stick_cal left_stick_cal_y;
struct joycon_stick_cal right_stick_cal_x;
struct joycon_stick_cal right_stick_cal_y;
struct joycon_imu_cal accel_cal;
struct joycon_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[JC_RUMBLE_QUEUE_SIZE][JC_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_input;
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;
};
/* Helper macros for checking controller type */
#define jc_type_is_joycon(ctlr) \
(ctlr->hdev->product == USB_DEVICE_ID_NINTENDO_JOYCONL || \
ctlr->hdev->product == USB_DEVICE_ID_NINTENDO_JOYCONR || \
ctlr->hdev->product == USB_DEVICE_ID_NINTENDO_CHRGGRIP)
#define jc_type_is_procon(ctlr) \
(ctlr->hdev->product == USB_DEVICE_ID_NINTENDO_PROCON)
#define jc_type_is_chrggrip(ctlr) \
(ctlr->hdev->product == USB_DEVICE_ID_NINTENDO_CHRGGRIP)
/* Does this controller have inputs associated with left joycon? */
#define jc_type_has_left(ctlr) \
(ctlr->ctlr_type == JOYCON_CTLR_TYPE_JCL || \
ctlr->ctlr_type == JOYCON_CTLR_TYPE_PRO || \
ctlr->ctlr_type == JOYCON_CTLR_TYPE_N64)
/* Does this controller have inputs associated with right joycon? */
#define jc_type_has_right(ctlr) \
(ctlr->ctlr_type == JOYCON_CTLR_TYPE_JCR || \
ctlr->ctlr_type == JOYCON_CTLR_TYPE_PRO)
/*
* 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 joycon_device_is_chrggrip(struct joycon_ctlr *ctlr)
{
return ctlr->hdev->product == USB_DEVICE_ID_NINTENDO_CHRGGRIP;
}
/*
* 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 joycon_type_is_left_joycon(struct joycon_ctlr *ctlr)
{
return ctlr->ctlr_type == JOYCON_CTLR_TYPE_JCL;
}
static inline bool joycon_type_is_right_joycon(struct joycon_ctlr *ctlr)
{
return ctlr->ctlr_type == JOYCON_CTLR_TYPE_JCR;
}
static inline bool joycon_type_is_procon(struct joycon_ctlr *ctlr)
{
return ctlr->ctlr_type == JOYCON_CTLR_TYPE_PRO;
}
static inline bool joycon_type_is_snescon(struct joycon_ctlr *ctlr)
{
return ctlr->ctlr_type == JOYCON_CTLR_TYPE_SNES;
}
static inline bool joycon_type_is_gencon(struct joycon_ctlr *ctlr)
{
return ctlr->ctlr_type == JOYCON_CTLR_TYPE_GEN;
}
static inline bool joycon_type_is_n64con(struct joycon_ctlr *ctlr)
{
return ctlr->ctlr_type == JOYCON_CTLR_TYPE_N64;
}
static inline bool joycon_type_is_left_nescon(struct joycon_ctlr *ctlr)
{
return ctlr->ctlr_type == JOYCON_CTLR_TYPE_NESL;
}
static inline bool joycon_type_is_right_nescon(struct joycon_ctlr *ctlr)
{
return ctlr->ctlr_type == JOYCON_CTLR_TYPE_NESR;
}
static inline bool joycon_type_is_any_joycon(struct joycon_ctlr *ctlr)
{
return joycon_type_is_left_joycon(ctlr) ||
joycon_type_is_right_joycon(ctlr) ||
joycon_device_is_chrggrip(ctlr);
}
static inline bool joycon_type_is_any_nescon(struct joycon_ctlr *ctlr)
{
return joycon_type_is_left_nescon(ctlr) ||
joycon_type_is_right_nescon(ctlr);
}
/*
* 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 joycon_has_imu(struct joycon_ctlr *ctlr)
{
return joycon_device_is_chrggrip(ctlr) ||
joycon_type_is_any_joycon(ctlr) ||
joycon_type_is_procon(ctlr);
}
static inline bool joycon_has_joysticks(struct joycon_ctlr *ctlr)
{
return joycon_device_is_chrggrip(ctlr) ||
joycon_type_is_any_joycon(ctlr) ||
joycon_type_is_procon(ctlr) ||
joycon_type_is_n64con(ctlr);
}
static inline bool joycon_has_rumble(struct joycon_ctlr *ctlr)
{
return joycon_device_is_chrggrip(ctlr) ||
joycon_type_is_any_joycon(ctlr) ||
joycon_type_is_procon(ctlr) ||
joycon_type_is_n64con(ctlr);
}
static inline bool joycon_using_usb(struct joycon_ctlr *ctlr)
{
return ctlr->hdev->bus == BUS_USB;
}
static int __joycon_hid_send(struct hid_device *hdev, u8 *data, size_t len)
{
u8 *buf;
int ret;
buf = kmemdup(data, len, GFP_KERNEL);
if (!buf)
return -ENOMEM;
ret = hid_hw_output_report(hdev, buf, len);
kfree(buf);
if (ret < 0)
hid_dbg(hdev, "Failed to send output report ret=%d\n", ret);
return ret;
}
static void joycon_wait_for_input_report(struct joycon_ctlr *ctlr)
{
int ret;
/*
* If we are in the proper reporting mode, wait for an input
* report prior to sending the subcommand. This improves
* reliability considerably.
*/
if (ctlr->ctlr_state == JOYCON_CTLR_STATE_READ) {
unsigned long flags;
spin_lock_irqsave(&ctlr->lock, flags);
ctlr->received_input_report = false;
spin_unlock_irqrestore(&ctlr->lock, flags);
ret = wait_event_timeout(ctlr->wait,
ctlr->received_input_report,
HZ / 4);
/* We will still proceed, even with a timeout here */
if (!ret)
hid_warn(ctlr->hdev,
"timeout waiting for input report\n");
}
}
/*
* Sending subcommands and/or rumble data at too high a rate can cause bluetooth
* controller disconnections.
*/
#define JC_INPUT_REPORT_MIN_DELTA 8
#define JC_INPUT_REPORT_MAX_DELTA 17
#define JC_SUBCMD_TX_OFFSET_MS 4
#define JC_SUBCMD_VALID_DELTA_REQ 3
#define JC_SUBCMD_RATE_MAX_ATTEMPTS 500
#define JC_SUBCMD_RATE_LIMITER_USB_MS 20
#define JC_SUBCMD_RATE_LIMITER_BT_MS 60
#define JC_SUBCMD_RATE_LIMITER_MS(ctlr) ((ctlr)->hdev->bus == BUS_USB ? JC_SUBCMD_RATE_LIMITER_USB_MS : JC_SUBCMD_RATE_LIMITER_BT_MS)
static void joycon_enforce_subcmd_rate(struct joycon_ctlr *ctlr)
{
unsigned int current_ms;
unsigned long subcmd_delta;
int consecutive_valid_deltas = 0;
int attempts = 0;
unsigned long flags;
if (unlikely(ctlr->ctlr_state != JOYCON_CTLR_STATE_READ))
return;
do {
joycon_wait_for_input_report(ctlr);
current_ms = jiffies_to_msecs(jiffies);
subcmd_delta = current_ms - ctlr->last_subcmd_sent_msecs;
spin_lock_irqsave(&ctlr->lock, flags);
consecutive_valid_deltas = ctlr->consecutive_valid_report_deltas;
spin_unlock_irqrestore(&ctlr->lock, flags);
attempts++;
} while ((consecutive_valid_deltas < JC_SUBCMD_VALID_DELTA_REQ ||
subcmd_delta < JC_SUBCMD_RATE_LIMITER_MS(ctlr)) &&
ctlr->ctlr_state == JOYCON_CTLR_STATE_READ &&
attempts < JC_SUBCMD_RATE_MAX_ATTEMPTS);
if (attempts >= JC_SUBCMD_RATE_MAX_ATTEMPTS) {
hid_warn(ctlr->hdev, "%s: exceeded max attempts", __func__);
return;
}
ctlr->last_subcmd_sent_msecs = current_ms;
/*
* Wait a short time after receiving an input report before
* transmitting. This should reduce odds of a TX coinciding with an RX.
* Minimizing concurrent BT traffic with the controller seems to lower
* the rate of disconnections.
*/
msleep(JC_SUBCMD_TX_OFFSET_MS);
}
static int joycon_hid_send_sync(struct joycon_ctlr *ctlr, 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--) {
joycon_enforce_subcmd_rate(ctlr);
ret = __joycon_hid_send(ctlr->hdev, data, len);
if (ret < 0) {
memset(ctlr->input_buf, 0, JC_MAX_RESP_SIZE);
return ret;
}
ret = wait_event_timeout(ctlr->wait, ctlr->received_resp,
timeout);
if (!ret) {
hid_dbg(ctlr->hdev,
"synchronous send/receive timed out\n");
if (tries) {
hid_dbg(ctlr->hdev,
"retrying sync send after timeout\n");
}
memset(ctlr->input_buf, 0, JC_MAX_RESP_SIZE);
ret = -ETIMEDOUT;
} else {
ret = 0;
break;
}
}
ctlr->received_resp = false;
return ret;
}
static int joycon_send_usb(struct joycon_ctlr *ctlr, u8 cmd, u32 timeout)
{
int ret;
u8 buf[2] = {JC_OUTPUT_USB_CMD};
buf[1] = cmd;
ctlr->usb_ack_match = cmd;
ctlr->msg_type = JOYCON_MSG_TYPE_USB;
ret = joycon_hid_send_sync(ctlr, buf, sizeof(buf), timeout);
if (ret)
hid_dbg(ctlr->hdev, "send usb command failed; ret=%d\n", ret);
return ret;
}
static int joycon_send_subcmd(struct joycon_ctlr *ctlr,
struct joycon_subcmd_request *subcmd,
size_t data_len, u32 timeout)
{
int ret;
unsigned long flags;
spin_lock_irqsave(&ctlr->lock, flags);
/*
* If the controller has been removed, just return ENODEV so the LED
* subsystem doesn't print invalid errors on removal.
*/
if (ctlr->ctlr_state == JOYCON_CTLR_STATE_REMOVED) {
spin_unlock_irqrestore(&ctlr->lock, flags);
return -ENODEV;
}
memcpy(subcmd->rumble_data, ctlr->rumble_data[ctlr->rumble_queue_tail],
JC_RUMBLE_DATA_SIZE);
spin_unlock_irqrestore(&ctlr->lock, flags);
subcmd->output_id = JC_OUTPUT_RUMBLE_AND_SUBCMD;
subcmd->packet_num = ctlr->subcmd_num;
if (++ctlr->subcmd_num > 0xF)
ctlr->subcmd_num = 0;
ctlr->subcmd_ack_match = subcmd->subcmd_id;
ctlr->msg_type = JOYCON_MSG_TYPE_SUBCMD;
ret = joycon_hid_send_sync(ctlr, (u8 *)subcmd,
sizeof(*subcmd) + data_len, timeout);
if (ret < 0)
hid_dbg(ctlr->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 joycon_set_player_leds(struct joycon_ctlr *ctlr, u8 flash, u8 on)
{
struct joycon_subcmd_request *req;
u8 buffer[sizeof(*req) + 1] = { 0 };
req = (struct joycon_subcmd_request *)buffer;
req->subcmd_id = JC_SUBCMD_SET_PLAYER_LIGHTS;
req->data[0] = (flash << 4) | on;
hid_dbg(ctlr->hdev, "setting player leds\n");
return joycon_send_subcmd(ctlr, req, 1, HZ/4);
}
static int joycon_set_home_led(struct joycon_ctlr *ctlr, enum led_brightness brightness)
{
struct joycon_subcmd_request *req;
u8 buffer[sizeof(*req) + 5] = { 0 };
u8 *data;
req = (struct joycon_subcmd_request *)buffer;
req->subcmd_id = JC_SUBCMD_SET_HOME_LIGHT;
data = req->data;
data[0] = 0x01;
data[1] = brightness << 4;
data[2] = brightness | (brightness << 4);
data[3] = 0x11;
data[4] = 0x11;
hid_dbg(ctlr->hdev, "setting home led brightness\n");
return joycon_send_subcmd(ctlr, req, 5, HZ/4);
}
static int joycon_request_spi_flash_read(struct joycon_ctlr *ctlr,
u32 start_addr, u8 size, u8 **reply)
{
struct joycon_subcmd_request *req;
struct joycon_input_report *report;
u8 buffer[sizeof(*req) + 5] = { 0 };
u8 *data;