-
Notifications
You must be signed in to change notification settings - Fork 22
/
hid-lg4ff.c
2568 lines (2194 loc) · 71.3 KB
/
hid-lg4ff.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-or-later
/*
* Force feedback support for Logitech Gaming Wheels
*
* Including G27, G25, DFP, DFGT, FFEX, Momo, Momo2 &
* Speed Force Wireless (WiiWheel)
*
* Copyright (c) 2010 Simon Wood <[email protected]>
* Copyright (c) 2019 Bernat Arlandis <[email protected]>
*/
#include <linux/module.h>
#include <linux/input.h>
#include <linux/usb.h>
#include <linux/hid.h>
#include <linux/fixp-arith.h>
#include <linux/hrtimer.h>
#include <linux/ktime.h>
#include "usbhid/usbhid.h"
#include "hid-lg.h"
#include "hid-lg4ff.h"
#include "hid-ids.h"
#define LG4FF_VERSION "0.4.2"
#define LG4FF_MMODE_IS_MULTIMODE 0
#define LG4FF_MMODE_SWITCHED 1
#define LG4FF_MMODE_NOT_MULTIMODE 2
/* Device has a "friction" effect in firmware */
#define LG4FF_CAP_FRICTION 1
#define LG4FF_MODE_NATIVE_IDX 0
#define LG4FF_MODE_DFEX_IDX 1
#define LG4FF_MODE_DFP_IDX 2
#define LG4FF_MODE_G25_IDX 3
#define LG4FF_MODE_DFGT_IDX 4
#define LG4FF_MODE_G27_IDX 5
#define LG4FF_MODE_G29_IDX 6
#define LG4FF_MODE_G923_PS_IDX 7
#define LG4FF_MODE_G923_IDX 8
#define LG4FF_MODE_MAX_IDX 9
#define LG4FF_MODE_NATIVE BIT(LG4FF_MODE_NATIVE_IDX)
#define LG4FF_MODE_DFEX BIT(LG4FF_MODE_DFEX_IDX)
#define LG4FF_MODE_DFP BIT(LG4FF_MODE_DFP_IDX)
#define LG4FF_MODE_G25 BIT(LG4FF_MODE_G25_IDX)
#define LG4FF_MODE_DFGT BIT(LG4FF_MODE_DFGT_IDX)
#define LG4FF_MODE_G27 BIT(LG4FF_MODE_G27_IDX)
#define LG4FF_MODE_G29 BIT(LG4FF_MODE_G29_IDX)
#define LG4FF_MODE_G923_PS BIT(LG4FF_MODE_G923_PS_IDX)
#define LG4FF_MODE_G923 BIT(LG4FF_MODE_G923_IDX)
#define LG4FF_DFEX_TAG "DF-EX"
#define LG4FF_DFEX_NAME "Driving Force / Formula EX"
#define LG4FF_DFP_TAG "DFP"
#define LG4FF_DFP_NAME "Driving Force Pro"
#define LG4FF_G25_TAG "G25"
#define LG4FF_G25_NAME "G25 Racing Wheel"
#define LG4FF_G27_TAG "G27"
#define LG4FF_G27_NAME "G27 Racing Wheel"
#define LG4FF_G29_TAG "G29"
#define LG4FF_G29_NAME "G29 Racing Wheel"
#define LG4FF_G923_TAG "G923"
#define LG4FF_G923_NAME "G923 Racing Wheel"
#define LG4FF_G923_PS_TAG "G923"
#define LG4FF_G923_PS_NAME "G923 Racing Wheel (Playstation mode)"
#define LG4FF_DFGT_TAG "DFGT"
#define LG4FF_DFGT_NAME "Driving Force GT"
#define LG4FF_FFEX_REV_MAJ 0x21
#define LG4FF_FFEX_REV_MIN 0x00
#define DEBUG(...) pr_debug("lg4ff: " __VA_ARGS__)
#define time_diff(a,b) ({ \
typecheck(unsigned long, a); \
typecheck(unsigned long, b); \
((a) - (long)(b)); })
#define CLAMP_VALUE_U16(x) ((unsigned short)((x) > 0xffff ? 0xffff : (x)))
#define CLAMP_VALUE_S16(x) ((unsigned short)((x) <= -0x8000 ? -0x8000 : ((x) > 0x7fff ? 0x7fff : (x))))
#define SCALE_VALUE_U16(x, bits) (CLAMP_VALUE_U16(x) >> (16 - bits))
#define SCALE_COEFF(x, bits) SCALE_VALUE_U16(abs(x) * 2, bits)
#define TRANSLATE_FORCE(x) ((CLAMP_VALUE_S16(x) + 0x8000) >> 8)
#define STOP_EFFECT(state) ((state)->flags = 0)
#define JIFFIES2MS(jiffies) ((jiffies) * 1000 / HZ)
#undef fixp_sin16
#define fixp_sin16(v) (((v % 360) > 180)? -(fixp_sin32((v % 360) - 180) >> 16) : fixp_sin32(v) >> 16)
#define DEFAULT_TIMER_PERIOD 2
#define LG4FF_MAX_EFFECTS 16
#define FF_EFFECT_STARTED 0
#define FF_EFFECT_ALLSET 1
#define FF_EFFECT_PLAYING 2
#define FF_EFFECT_UPDATING 3
struct lg4ff_effect_state {
struct ff_effect effect;
struct ff_envelope *envelope;
unsigned long start_at;
unsigned long play_at;
unsigned long stop_at;
unsigned long flags;
unsigned long time_playing;
unsigned long updated_at;
unsigned int phase;
unsigned int phase_adj;
unsigned int count;
unsigned int cmd;
unsigned int cmd_start_time;
unsigned int cmd_start_count;
int direction_gain;
int slope;
unsigned int slot;
};
struct lg4ff_effect_parameters {
int level;
int d1;
int d2;
int k1;
int k2;
unsigned int clip;
};
struct lg4ff_slot {
int id;
struct lg4ff_effect_parameters parameters;
u8 current_cmd[7];
int cmd_op;
int is_updated;
int effect_type;
};
struct lg4ff_wheel_data {
const u32 product_id;
u16 combine;
u16 range;
u16 autocenter;
u16 master_gain;
u16 gain;
const u16 min_range;
const u16 max_range;
#ifdef CONFIG_LEDS_CLASS
u8 led_state;
struct led_classdev *led[5];
#endif
const u32 alternate_modes;
const char * const real_tag;
const char * const real_name;
const u16 real_product_id;
const u16 capabilities;
void (*set_range)(struct hid_device *hid, u16 range);
};
struct lg4ff_device_entry {
spinlock_t report_lock; /* Protect output HID report */
spinlock_t timer_lock;
struct hid_report *report;
struct lg4ff_wheel_data wdata;
struct hid_device *hid;
struct timer_list timer;
struct hrtimer hrtimer;
struct lg4ff_slot slots[4];
struct lg4ff_effect_state states[LG4FF_MAX_EFFECTS];
unsigned peak_ffb_level;
int effects_used;
#ifdef CONFIG_LEDS_CLASS
int has_leds;
#endif
};
static const signed short lg4ff_wheel_effects[] = {
FF_CONSTANT,
FF_SPRING,
FF_DAMPER,
FF_AUTOCENTER,
FF_PERIODIC,
FF_SINE,
FF_SQUARE,
FF_TRIANGLE,
FF_SAW_UP,
FF_SAW_DOWN,
FF_RAMP,
FF_FRICTION,
FF_INERTIA,
-1
};
static const signed short no_wheel_effects[] = {
-1
};
struct lg4ff_wheel {
const u32 product_id;
const signed short *ff_effects;
const u16 min_range;
const u16 max_range;
const u16 capabilities;
void (*set_range)(struct hid_device *hid, u16 range);
};
struct lg4ff_compat_mode_switch {
const u8 cmd_count; /* Number of commands to send */
const u8 cmd[];
};
struct lg4ff_wheel_ident_info {
const u32 modes;
const u16 mask;
const u16 result;
const u16 real_product_id;
};
struct lg4ff_multimode_wheel {
const u16 product_id;
const u32 alternate_modes;
const char *real_tag;
const char *real_name;
};
struct lg4ff_alternate_mode {
const u16 product_id;
const char *tag;
const char *name;
};
static void lg4ff_set_range_dfp(struct hid_device *hid, u16 range);
static void lg4ff_set_range_g25(struct hid_device *hid, u16 range);
#ifdef CONFIG_LEDS_CLASS
static void lg4ff_set_leds(struct hid_device *hid, u8 leds);
#endif
static const struct lg4ff_wheel lg4ff_devices[] = {
{USB_DEVICE_ID_LOGITECH_WINGMAN_FG,
no_wheel_effects, 40, 180, 0, NULL},
{USB_DEVICE_ID_LOGITECH_WINGMAN_FFG,
lg4ff_wheel_effects, 40, 180, 0, NULL},
{USB_DEVICE_ID_LOGITECH_WHEEL,
lg4ff_wheel_effects, 40, 270, 0, NULL},
{USB_DEVICE_ID_LOGITECH_MOMO_WHEEL,
lg4ff_wheel_effects, 40, 270, 0, NULL},
{USB_DEVICE_ID_LOGITECH_DFP_WHEEL,
lg4ff_wheel_effects, 40, 900, LG4FF_CAP_FRICTION, lg4ff_set_range_dfp},
{USB_DEVICE_ID_LOGITECH_G25_WHEEL,
lg4ff_wheel_effects, 40, 900, LG4FF_CAP_FRICTION, lg4ff_set_range_g25},
{USB_DEVICE_ID_LOGITECH_DFGT_WHEEL,
lg4ff_wheel_effects, 40, 900, LG4FF_CAP_FRICTION, lg4ff_set_range_g25},
{USB_DEVICE_ID_LOGITECH_G27_WHEEL,
lg4ff_wheel_effects, 40, 900, LG4FF_CAP_FRICTION, lg4ff_set_range_g25},
{USB_DEVICE_ID_LOGITECH_G29_WHEEL,
lg4ff_wheel_effects, 40, 900, 0, lg4ff_set_range_g25},
{USB_DEVICE_ID_LOGITECH_G923_WHEEL,
lg4ff_wheel_effects, 40, 900, 0, lg4ff_set_range_g25},
{USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2,
lg4ff_wheel_effects, 40, 270, 0, NULL},
{USB_DEVICE_ID_LOGITECH_WII_WHEEL,
lg4ff_wheel_effects, 40, 270, 0, NULL}
};
static const struct lg4ff_multimode_wheel lg4ff_multimode_wheels[] = {
{USB_DEVICE_ID_LOGITECH_DFP_WHEEL,
LG4FF_MODE_NATIVE | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
LG4FF_DFP_TAG, LG4FF_DFP_NAME},
{USB_DEVICE_ID_LOGITECH_G25_WHEEL,
LG4FF_MODE_NATIVE | LG4FF_MODE_G25 | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
LG4FF_G25_TAG, LG4FF_G25_NAME},
{USB_DEVICE_ID_LOGITECH_DFGT_WHEEL,
LG4FF_MODE_NATIVE | LG4FF_MODE_DFGT | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
LG4FF_DFGT_TAG, LG4FF_DFGT_NAME},
{USB_DEVICE_ID_LOGITECH_G27_WHEEL,
LG4FF_MODE_NATIVE | LG4FF_MODE_G27 | LG4FF_MODE_G25 | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
LG4FF_G27_TAG, LG4FF_G27_NAME},
{USB_DEVICE_ID_LOGITECH_G29_WHEEL,
LG4FF_MODE_NATIVE | LG4FF_MODE_G29 | LG4FF_MODE_G27 | LG4FF_MODE_G25 | LG4FF_MODE_DFGT | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
LG4FF_G29_TAG, LG4FF_G29_NAME},
{USB_DEVICE_ID_LOGITECH_G923_PS_WHEEL,
LG4FF_MODE_G923_PS | LG4FF_MODE_G923,
LG4FF_G923_PS_TAG, LG4FF_G923_PS_NAME},
{USB_DEVICE_ID_LOGITECH_G923_WHEEL,
LG4FF_MODE_G923,
LG4FF_G923_TAG, LG4FF_G923_NAME},
};
static const struct lg4ff_alternate_mode lg4ff_alternate_modes[] = {
[LG4FF_MODE_NATIVE_IDX] = {0, "native", ""},
[LG4FF_MODE_DFEX_IDX] = {USB_DEVICE_ID_LOGITECH_WHEEL, LG4FF_DFEX_TAG, LG4FF_DFEX_NAME},
[LG4FF_MODE_DFP_IDX] = {USB_DEVICE_ID_LOGITECH_DFP_WHEEL, LG4FF_DFP_TAG, LG4FF_DFP_NAME},
[LG4FF_MODE_G25_IDX] = {USB_DEVICE_ID_LOGITECH_G25_WHEEL, LG4FF_G25_TAG, LG4FF_G25_NAME},
[LG4FF_MODE_DFGT_IDX] = {USB_DEVICE_ID_LOGITECH_DFGT_WHEEL, LG4FF_DFGT_TAG, LG4FF_DFGT_NAME},
[LG4FF_MODE_G27_IDX] = {USB_DEVICE_ID_LOGITECH_G27_WHEEL, LG4FF_G27_TAG, LG4FF_G27_NAME},
[LG4FF_MODE_G29_IDX] = {USB_DEVICE_ID_LOGITECH_G29_WHEEL, LG4FF_G29_TAG, LG4FF_G29_NAME},
[LG4FF_MODE_G923_PS_IDX] = {USB_DEVICE_ID_LOGITECH_G923_PS_WHEEL, LG4FF_G923_PS_TAG, LG4FF_G923_PS_NAME},
[LG4FF_MODE_G923_IDX] = {USB_DEVICE_ID_LOGITECH_G923_WHEEL, LG4FF_G923_TAG, LG4FF_G923_NAME},
};
/* Multimode wheel identificators */
static const struct lg4ff_wheel_ident_info lg4ff_dfp_ident_info = {
LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
0xf000,
0x1000,
USB_DEVICE_ID_LOGITECH_DFP_WHEEL
};
static const struct lg4ff_wheel_ident_info lg4ff_g25_ident_info = {
LG4FF_MODE_G25 | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
0xff00,
0x1200,
USB_DEVICE_ID_LOGITECH_G25_WHEEL
};
static const struct lg4ff_wheel_ident_info lg4ff_g27_ident_info = {
LG4FF_MODE_G27 | LG4FF_MODE_G25 | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
0xfff0,
0x1230,
USB_DEVICE_ID_LOGITECH_G27_WHEEL
};
static const struct lg4ff_wheel_ident_info lg4ff_dfgt_ident_info = {
LG4FF_MODE_DFGT | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
0xff00,
0x1300,
USB_DEVICE_ID_LOGITECH_DFGT_WHEEL
};
static const struct lg4ff_wheel_ident_info lg4ff_g29_ident_info = {
LG4FF_MODE_G29 | LG4FF_MODE_G27 | LG4FF_MODE_G25 | LG4FF_MODE_DFGT | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
0xfff8,
0x1350,
USB_DEVICE_ID_LOGITECH_G29_WHEEL
};
static const struct lg4ff_wheel_ident_info lg4ff_g29_ident_info2 = {
LG4FF_MODE_G29 | LG4FF_MODE_G27 | LG4FF_MODE_G25 | LG4FF_MODE_DFGT | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
0xff00,
0x8900,
USB_DEVICE_ID_LOGITECH_G29_WHEEL
};
static const struct lg4ff_wheel_ident_info lg4ff_g923_ident_info = {
LG4FF_MODE_G923_PS | LG4FF_MODE_G923,
0xff00,
0x3800,
USB_DEVICE_ID_LOGITECH_G923_WHEEL
};
/* Multimode wheel identification checklists */
static const struct lg4ff_wheel_ident_info *lg4ff_main_checklist[] = {
&lg4ff_g29_ident_info,
&lg4ff_g29_ident_info2,
&lg4ff_g923_ident_info,
&lg4ff_dfgt_ident_info,
&lg4ff_g27_ident_info,
&lg4ff_g25_ident_info,
&lg4ff_dfp_ident_info
};
/* Compatibility mode switching commands */
/* EXT_CMD9 - Understood by G27 and DFGT */
static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_dfex = {
2,
{0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */
0xf8, 0x09, 0x00, 0x01, 0x00, 0x00, 0x00} /* Switch mode to DF-EX with detach */
};
static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_dfp = {
2,
{0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */
0xf8, 0x09, 0x01, 0x01, 0x00, 0x00, 0x00} /* Switch mode to DFP with detach */
};
static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_g25 = {
2,
{0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */
0xf8, 0x09, 0x02, 0x01, 0x00, 0x00, 0x00} /* Switch mode to G25 with detach */
};
static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_dfgt = {
2,
{0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */
0xf8, 0x09, 0x03, 0x01, 0x00, 0x00, 0x00} /* Switch mode to DFGT with detach */
};
static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_g27 = {
2,
{0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */
0xf8, 0x09, 0x04, 0x01, 0x00, 0x00, 0x00} /* Switch mode to G27 with detach */
};
static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_g29 = {
2,
{0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */
0xf8, 0x09, 0x05, 0x01, 0x01, 0x00, 0x00} /* Switch mode to G29 with detach */
};
static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_g923 = {
2,
{0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */
0xf8, 0x09, 0x07, 0x01, 0x01, 0x00, 0x00} /* Switch mode to G923 with detach */
};
/* EXT_CMD1 - Understood by DFP, G25, G27 and DFGT */
static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext01_dfp = {
1,
{0xf8, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00}
};
/* EXT_CMD16 - Understood by G25 and G27 */
static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext16_g25 = {
1,
{0xf8, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00}
};
/* 0x30 - PS mode - Understood by G923 PS */
// Report ID must be 0x30
static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_30_g923 = {
1,
{0xf8, 0x09, 0x07, 0x01, 0x01, 0x00, 0x00} /* Switch mode to G923 with detach */
};
static int timer_msecs = DEFAULT_TIMER_PERIOD;
module_param(timer_msecs, int, 0660);
MODULE_PARM_DESC(timer_msecs, "Timer resolution in msecs.");
static int fixed_loop = 0;
module_param(fixed_loop, int, 0);
MODULE_PARM_DESC(fixed_loop, "Put the device into fixed loop mode.");
static int timer_mode = 2;
module_param(timer_mode, int, 0660);
MODULE_PARM_DESC(timer_mode, "Timer mode: 0) fixed, 1) static, 2) dynamic (default).");
static int profile = 0;
module_param(profile, int, 0660);
MODULE_PARM_DESC(profile, "Enable profile debug messages.");
#ifdef CONFIG_LEDS_CLASS
static int ffb_leds = 0;
module_param(ffb_leds, int, 0);
MODULE_PARM_DESC(ffb_leds, "Use leds to display FFB levels for calibration.");
#endif
static int spring_level = 30;
module_param(spring_level, int, 0);
MODULE_PARM_DESC(spring_level, "Level of spring force (0-100).");
static int damper_level = 30;
module_param(damper_level, int, 0);
MODULE_PARM_DESC(damper_level, "Level of damper force (0-100).");
static int friction_level = 30;
module_param(friction_level, int, 0);
MODULE_PARM_DESC(friction_level, "Level of friction force (0-100).");
static struct lg4ff_device_entry *lg4ff_get_device_entry(struct hid_device *hid)
{
struct lg_drv_data *drv_data;
struct lg4ff_device_entry *entry;
if (!hid) {
hid_err(hid, "HID not found!\n");
return NULL;
}
drv_data = hid_get_drvdata(hid);
if (!drv_data) {
hid_err(hid, "Private driver data not found!\n");
return NULL;
}
entry = drv_data->device_props;
if (!entry) {
hid_err(hid, "Device properties not found!\n");
return NULL;
}
return entry;
}
static void lg4ff_send_cmd_with_id(struct lg4ff_device_entry *entry, u8 *cmd, u8 id) {
unsigned long flags;
s32 *value = entry->report->field[0]->value;
spin_lock_irqsave(&entry->report_lock, flags);
entry->report->id = id;
value[0] = cmd[0];
value[1] = cmd[1];
value[2] = cmd[2];
value[3] = cmd[3];
value[4] = cmd[4];
value[5] = cmd[5];
value[6] = cmd[6];
hid_hw_request(entry->hid, entry->report, HID_REQ_SET_REPORT);
spin_unlock_irqrestore(&entry->report_lock, flags);
DEBUG("send_cmd: %02X %02X %02X %02X %02X %02X %02X %02X\n", id, cmd[0], cmd[1], cmd[2], cmd[3], cmd[4], cmd[5], cmd[6]);
}
static void lg4ff_send_cmd(struct lg4ff_device_entry *entry, u8 *cmd)
{
unsigned long flags;
s32 *value = entry->report->field[0]->value;
spin_lock_irqsave(&entry->report_lock, flags);
value[0] = cmd[0];
value[1] = cmd[1];
value[2] = cmd[2];
value[3] = cmd[3];
value[4] = cmd[4];
value[5] = cmd[5];
value[6] = cmd[6];
hid_hw_request(entry->hid, entry->report, HID_REQ_SET_REPORT);
spin_unlock_irqrestore(&entry->report_lock, flags);
DEBUG("send_cmd: %02X %02X %02X %02X %02X %02X %02X", cmd[0], cmd[1], cmd[2], cmd[3], cmd[4], cmd[5], cmd[6]);
}
static void lg4ff_update_slot(struct lg4ff_slot *slot, struct lg4ff_effect_parameters *parameters)
{
u8 original_cmd[7];
int d1;
int d2;
int k1;
int k2;
int s1;
int s2;
memcpy(original_cmd, slot->current_cmd, sizeof(original_cmd));
if ((original_cmd[0] & 0xf) == 1) {
original_cmd[0] = (original_cmd[0] & 0xf0) + 0xc;
}
if (slot->effect_type == FF_CONSTANT) {
if (slot->cmd_op == 0) {
slot->cmd_op = 1;
} else {
slot->cmd_op = 0xc;
}
} else {
if (parameters->clip == 0 || slot->effect_type == 0) {
slot->cmd_op = 3;
} else if (slot->cmd_op == 3) {
slot->cmd_op = 1;
} else {
slot->cmd_op = 0xc;
}
}
slot->current_cmd[0] = (0x10 << slot->id) + slot->cmd_op;
if (slot->cmd_op == 3) {
slot->current_cmd[1] = 0;
slot->current_cmd[2] = 0;
slot->current_cmd[3] = 0;
slot->current_cmd[4] = 0;
slot->current_cmd[5] = 0;
slot->current_cmd[6] = 0;
} else {
switch (slot->effect_type) {
case FF_CONSTANT:
slot->current_cmd[1] = 0x00;
slot->current_cmd[2] = 0;
slot->current_cmd[3] = 0;
slot->current_cmd[4] = 0;
slot->current_cmd[5] = 0;
slot->current_cmd[6] = 0;
slot->current_cmd[2 + slot->id] = TRANSLATE_FORCE(parameters->level);
break;
case FF_SPRING:
d1 = SCALE_VALUE_U16(((parameters->d1) + 0x8000) & 0xffff, 11);
d2 = SCALE_VALUE_U16(((parameters->d2) + 0x8000) & 0xffff, 11);
s1 = parameters->k1 < 0;
s2 = parameters->k2 < 0;
k1 = abs(parameters->k1);
k2 = abs(parameters->k2);
if (k1 < 2048) {
d1 = 0;
} else {
k1 -= 2048;
}
if (k2 < 2048) {
d2 = 2047;
} else {
k2 -= 2048;
}
slot->current_cmd[1] = 0x0b;
slot->current_cmd[2] = d1 >> 3;
slot->current_cmd[3] = d2 >> 3;
slot->current_cmd[4] = (SCALE_COEFF(k2, 4) << 4) + SCALE_COEFF(k1, 4);
slot->current_cmd[5] = ((d2 & 7) << 5) + ((d1 & 7) << 1) + (s2 << 4) + s1;
slot->current_cmd[6] = SCALE_VALUE_U16(parameters->clip, 8);
break;
case FF_DAMPER:
s1 = parameters->k1 < 0;
s2 = parameters->k2 < 0;
slot->current_cmd[1] = 0x0c;
slot->current_cmd[2] = SCALE_COEFF(parameters->k1, 4);
slot->current_cmd[3] = s1;
slot->current_cmd[4] = SCALE_COEFF(parameters->k2, 4);
slot->current_cmd[5] = s2;
slot->current_cmd[6] = SCALE_VALUE_U16(parameters->clip, 8);
break;
case FF_FRICTION:
s1 = parameters->k1 < 0;
s2 = parameters->k2 < 0;
slot->current_cmd[1] = 0x0e;
slot->current_cmd[2] = SCALE_COEFF(parameters->k1, 8);
slot->current_cmd[3] = SCALE_COEFF(parameters->k2, 8);
slot->current_cmd[4] = SCALE_VALUE_U16(parameters->clip, 8);
slot->current_cmd[5] = (s2 << 4) + s1;
slot->current_cmd[6] = 0;
break;
}
}
if (memcmp(original_cmd, slot->current_cmd, sizeof(original_cmd))) {
slot->is_updated = 1;
}
}
static __always_inline int lg4ff_calculate_constant(struct lg4ff_effect_state *state)
{
int level_sign;
int level = state->effect.u.constant.level;
int d, t;
if (state->time_playing < state->envelope->attack_length) {
level_sign = level < 0 ? -1 : 1;
d = level - level_sign * state->envelope->attack_level;
level = level_sign * state->envelope->attack_level + d * state->time_playing / state->envelope->attack_length;
} else if (state->effect.replay.length) {
t = state->time_playing - state->effect.replay.length + state->envelope->fade_length;
if (t > 0) {
level_sign = level < 0 ? -1 : 1;
d = level - level_sign * state->envelope->fade_level;
level = level - d * t / state->envelope->fade_length;
}
}
return state->direction_gain * level / 0x7fff;
}
static __always_inline int lg4ff_calculate_ramp(struct lg4ff_effect_state *state)
{
struct ff_ramp_effect *ramp = &state->effect.u.ramp;
int level_sign;
int level = INT_MAX;
int d, t;
if (state->time_playing < state->envelope->attack_length) {
level = ramp->start_level;
level_sign = level < 0 ? -1 : 1;
t = state->envelope->attack_length - state->time_playing;
d = level - level_sign * state->envelope->attack_level;
level = level_sign * state->envelope->attack_level + d * t / state->envelope->attack_length;
} else if (state->effect.replay.length && state->time_playing >= state->effect.replay.length - state->envelope->fade_length) {
level = ramp->end_level;
level_sign = level < 0 ? -1 : 1;
t = state->time_playing - state->effect.replay.length + state->envelope->fade_length;
d = level_sign * state->envelope->fade_level - level;
level = level - d * t / state->envelope->fade_length;
} else {
t = state->time_playing - state->envelope->attack_length;
level = ramp->start_level + ((t * state->slope) >> 16);
}
return state->direction_gain * level / 0x7fff;
}
static __always_inline int lg4ff_calculate_periodic(struct lg4ff_effect_state *state)
{
struct ff_periodic_effect *periodic = &state->effect.u.periodic;
int magnitude = periodic->magnitude;
int magnitude_sign = magnitude < 0 ? -1 : 1;
int level = periodic->offset;
int d, t;
if (state->time_playing < state->envelope->attack_length) {
d = magnitude - magnitude_sign * state->envelope->attack_level;
magnitude = magnitude_sign * state->envelope->attack_level + d * state->time_playing / state->envelope->attack_length;
} else if (state->effect.replay.length) {
t = state->time_playing - state->effect.replay.length + state->envelope->fade_length;
if (t > 0) {
d = magnitude - magnitude_sign * state->envelope->fade_level;
magnitude = magnitude - d * t / state->envelope->fade_length;
}
}
switch (periodic->waveform) {
case FF_SINE:
level += fixp_sin16(state->phase) * magnitude / 0x7fff;
break;
case FF_SQUARE:
level += (state->phase < 180 ? 1 : -1) * magnitude;
break;
case FF_TRIANGLE:
level += abs(state->phase * magnitude * 2 / 360 - magnitude) * 2 - magnitude;
break;
case FF_SAW_UP:
level += state->phase * magnitude * 2 / 360 - magnitude;
break;
case FF_SAW_DOWN:
level += magnitude - state->phase * magnitude * 2 / 360;
break;
}
return state->direction_gain * level / 0x7fff;
}
static __always_inline void lg4ff_calculate_spring(struct lg4ff_effect_state *state, struct lg4ff_effect_parameters *parameters)
{
struct ff_condition_effect *condition = &state->effect.u.condition[0];
parameters->d1 = ((int)condition->center) - condition->deadband / 2;
parameters->d2 = ((int)condition->center) + condition->deadband / 2;
parameters->k1 = condition->left_coeff;
parameters->k2 = condition->right_coeff;
parameters->clip = (unsigned)condition->right_saturation;
}
static __always_inline void lg4ff_calculate_resistance(struct lg4ff_effect_state *state, struct lg4ff_effect_parameters *parameters)
{
struct ff_condition_effect *condition = &state->effect.u.condition[0];
parameters->k1 = condition->left_coeff;
parameters->k2 = condition->right_coeff;
parameters->clip = (unsigned)condition->right_saturation;
}
static __always_inline struct ff_envelope *lg4ff_effect_envelope(struct ff_effect *effect)
{
switch (effect->type) {
case FF_CONSTANT:
return &effect->u.constant.envelope;
case FF_RAMP:
return &effect->u.ramp.envelope;
case FF_PERIODIC:
return &effect->u.periodic.envelope;
}
return NULL;
}
static __always_inline void lg4ff_update_state(struct lg4ff_effect_state *state, const unsigned long now)
{
struct ff_effect *effect = &state->effect;
unsigned long phase_time;
if (!__test_and_set_bit(FF_EFFECT_ALLSET, &state->flags)) {
state->play_at = state->start_at + effect->replay.delay;
if (!test_bit(FF_EFFECT_UPDATING, &state->flags)) {
state->updated_at = state->play_at;
}
state->direction_gain = fixp_sin16(effect->direction * 360 / 0x10000);
if (effect->type == FF_PERIODIC) {
state->phase_adj = effect->u.periodic.phase * 360 / effect->u.periodic.period;
}
if (effect->replay.length) {
state->stop_at = state->play_at + effect->replay.length;
}
}
if (__test_and_clear_bit(FF_EFFECT_UPDATING, &state->flags)) {
__clear_bit(FF_EFFECT_PLAYING, &state->flags);
state->play_at = state->updated_at + effect->replay.delay;
state->direction_gain = fixp_sin16(effect->direction * 360 / 0x10000);
if (effect->replay.length) {
state->stop_at = state->updated_at + effect->replay.length;
}
if (effect->type == FF_PERIODIC) {
state->phase_adj = state->phase;
}
}
state->envelope = lg4ff_effect_envelope(effect);
state->slope = 0;
if (effect->type == FF_RAMP && effect->replay.length) {
state->slope = ((effect->u.ramp.end_level - effect->u.ramp.start_level) << 16) / (effect->replay.length - state->envelope->attack_length - state->envelope->fade_length);
}
if (!test_bit(FF_EFFECT_PLAYING, &state->flags) && time_after_eq(now,
state->play_at) && (effect->replay.length == 0 ||
time_before(now, state->stop_at))) {
__set_bit(FF_EFFECT_PLAYING, &state->flags);
}
if (test_bit(FF_EFFECT_PLAYING, &state->flags)) {
state->time_playing = time_diff(now, state->play_at);
if (effect->type == FF_PERIODIC) {
phase_time = time_diff(now, state->updated_at);
state->phase = (phase_time % effect->u.periodic.period) * 360 / effect->u.periodic.period;
state->phase += state->phase_adj % 360;
}
}
}
static __always_inline int lg4ff_timer(struct lg4ff_device_entry *entry)
{
struct usbhid_device *usbhid = entry->hid->driver_data;
struct lg4ff_slot *slot;
struct lg4ff_effect_state *state;
struct lg4ff_effect_parameters parameters[4];
unsigned long jiffies_now = jiffies;
unsigned long now = JIFFIES2MS(jiffies_now);
unsigned long flags;
unsigned gain;
int current_period;
int count;
int effect_id;
int i;
int ffb_level;
#ifdef CONFIG_LEDS_CLASS
static int leds_timer = 0;
static int leds_level = 0;
u8 led_states;
#endif
if (timer_mode > 0 && usbhid->outhead != usbhid->outtail) {
current_period = timer_msecs;
if (timer_mode == 1) {
timer_msecs *= 2;
hid_info(entry->hid, "Commands stacking up, increasing timer period to %d ms.", timer_msecs);
} else {
DEBUG("Commands stacking up, delaying timer.");
}
return current_period;
}
memset(parameters, 0, sizeof(parameters));
gain = (unsigned)entry->wdata.master_gain * entry->wdata.gain / 0xffff;
spin_lock_irqsave(&entry->timer_lock, flags);
count = entry->effects_used;
for (effect_id = 0; effect_id < LG4FF_MAX_EFFECTS; effect_id++) {
if (!count) {
break;
}
state = &entry->states[effect_id];
if (!test_bit(FF_EFFECT_STARTED, &state->flags)) {
continue;
}
count--;
if (test_bit(FF_EFFECT_ALLSET, &state->flags)) {
if (state->effect.replay.length && time_after_eq(now, state->stop_at)) {
STOP_EFFECT(state);
if (!--state->count) {
entry->effects_used--;
continue;
}
__set_bit(FF_EFFECT_STARTED, &state->flags);
state->start_at = state->stop_at;
}
}
lg4ff_update_state(state, now);
if (!test_bit(FF_EFFECT_PLAYING, &state->flags)) {
continue;
}
switch (state->effect.type) {
case FF_CONSTANT:
parameters[0].level += lg4ff_calculate_constant(state);
break;
case FF_RAMP:
parameters[0].level += lg4ff_calculate_ramp(state);
break;
case FF_PERIODIC:
parameters[0].level += lg4ff_calculate_periodic(state);
break;
case FF_SPRING:
if (state->slot != 0) {
lg4ff_calculate_spring(state, ¶meters[state->slot]);
}
break;
case FF_DAMPER:
case FF_FRICTION:
case FF_INERTIA:
if (state->slot != 0) {
lg4ff_calculate_resistance(state, ¶meters[state->slot]);
}
}
}
spin_unlock_irqrestore(&entry->timer_lock, flags);
parameters[0].level = (long)parameters[0].level * gain / 0xffff;
ffb_level = abs(parameters[0].level);
for (i = 1; i < 4; i++) {
parameters[i].k1 = (long)parameters[i].k1 * gain / 0xffff;
parameters[i].k2 = (long)parameters[i].k2 * gain / 0xffff;
switch (entry->slots[i].effect_type) {
case FF_SPRING:
parameters[i].clip = parameters[i].clip * spring_level / 100;
break;
case FF_DAMPER:
parameters[i].clip = parameters[i].clip * damper_level / 100;
break;
case FF_FRICTION:
parameters[i].clip = parameters[i].clip * friction_level / 100;
break;
}
parameters[i].clip = parameters[i].clip * gain / 0xffff;
ffb_level += parameters[i].clip * 0x7fff / 0xffff;
}
if (ffb_level > entry->peak_ffb_level) {
entry->peak_ffb_level = ffb_level;
}
for (i = 0; i < 4; i++) {
slot = &entry->slots[i];
lg4ff_update_slot(slot, ¶meters[i]);
if (slot->is_updated) {
lg4ff_send_cmd(entry, slot->current_cmd);
slot->is_updated = 0;
}
}
#ifdef CONFIG_LEDS_CLASS
if (ffb_leds || leds_level > 0) {
if (ffb_level > leds_level) {
leds_level = ffb_level;
}
if (!ffb_leds || entry->effects_used == 0) {
leds_timer = 0;
leds_level = 0;
}
if (leds_timer == 0) {
leds_timer = 480 / timer_msecs;
if (leds_level < 2458) { // < 7.5%
led_states = 0;
} else if (leds_level < 8192) { // < 25%
led_states = 1;
} else if (leds_level < 16384) { // < 50%
led_states = 3;
} else if (leds_level < 24576) { // < 75%
led_states = 7;
} else if (leds_level < 29491) { // < 90%
led_states = 15;
} else if (leds_level <= 32768) { // <= 100%
led_states = 31;
} else if (leds_level < 36045) { // < 110%
led_states = 30;
} else if (leds_level < 40960) { // < 125%
led_states = 28;
} else if (leds_level < 49152) { // < 150%
led_states = 24;
} else {
led_states = 16;
}
lg4ff_set_leds(entry->hid, led_states);
leds_level = 0;
}
leds_timer--;
}
#endif
return 0;
}
static enum hrtimer_restart lg4ff_timer_hires(struct hrtimer *t)
{
struct lg4ff_device_entry *entry = container_of(t, struct lg4ff_device_entry, hrtimer);
int delay_timer;
int overruns;
delay_timer = lg4ff_timer(entry);
if (delay_timer) {
hrtimer_forward_now(&entry->hrtimer, ms_to_ktime(delay_timer));
return HRTIMER_RESTART;
}
if (entry->effects_used) {
overruns = hrtimer_forward_now(&entry->hrtimer, ms_to_ktime(timer_msecs));
overruns--;
if (unlikely(profile && overruns > 0))
DEBUG("Overruns: %d", overruns);
return HRTIMER_RESTART;
} else {
if (unlikely(profile))
DEBUG("Stop timer.");
return HRTIMER_NORESTART;
}
}
static void lg4ff_init_slots(struct lg4ff_device_entry *entry)
{
struct lg4ff_effect_parameters parameters;
u8 cmd[8] = {0};