Repository navigation
Expand file tree
/
Copy pathhid-pidff-patch.patch
More file actions
2105 lines (1973 loc) · 64 KB
/
Copy pathhid-pidff-patch.patch
File metadata and controls
2105 lines (1973 loc) · 64 KB
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
--- drivers/hid/usbhid/hid-pidff.c.orig 2020-01-17 20:30:44.000000000 +0200
+++ drivers/hid/usbhid/hid-pidff.c 2020-01-17 21:53:44.557004200 +0200
@@ -1,26 +1,22 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Force feedback driver for USB HID PID compliant devices
*
* Copyright (c) 2005, 2006 Anssi Hannula <anssi.hannula@gmail.com>
+ * 2014 Lauri Peltonen <lauri.peltonen@gmail.com>
*/
/*
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
-/* #define DEBUG */
+#define DEBUG
+
+/* Enable this to debug driver managed memory pool allocations */
+#define DEBUG_MEM_ALLOC
+
+/* Enable this to debug scaling of parameters */
+#define DEBUG_SCALING
+
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
@@ -30,9 +26,19 @@
#include <linux/hid.h>
+#include <linux/list.h>
+
#include "usbhid.h"
+#define IS_DEVICE_MANAGED(device) (test_bit(PID_SUPPORTS_DEVICE_MANAGED, \
+ &device->flags))
+
#define PID_EFFECTS_MAX 64
+/* Only 2 axes are currently supported in the driver.
+ * Some code sections would support more,
+ * but in some sections 2 is still a hard coded limit.
+ */
+#define PID_AXES_MAX 2
/* Report usage table used to put reports into an array */
@@ -40,67 +46,86 @@
#define PID_EFFECT_OPERATION 1
#define PID_DEVICE_GAIN 2
#define PID_POOL 3
-#define PID_BLOCK_LOAD 4
-#define PID_BLOCK_FREE 5
-#define PID_DEVICE_CONTROL 6
+#define PID_DEVICE_CONTROL 4
+
+#define PID_BLOCK_LOAD 5
+#define PID_BLOCK_FREE 6
#define PID_CREATE_NEW_EFFECT 7
-#define PID_REQUIRED_REPORTS 7
+#define PID_POOL_MOVE 8
-#define PID_SET_ENVELOPE 8
-#define PID_SET_CONDITION 9
-#define PID_SET_PERIODIC 10
-#define PID_SET_CONSTANT 11
-#define PID_SET_RAMP 12
+#define PID_SET_ENVELOPE 9
+#define PID_SET_CONDITION 10
+#define PID_SET_PERIODIC 11
+#define PID_SET_CONSTANT 12
+#define PID_SET_RAMP 13
+
+#define PID_REQUIRED_REPORTS 4
+#define PID_REQUIRED_DEVICE_MANAGED 7
+#define PID_REQUIRED_POOL_MOVE 8
static const u8 pidff_reports[] = {
- 0x21, 0x77, 0x7d, 0x7f, 0x89, 0x90, 0x96, 0xab,
- 0x5a, 0x5f, 0x6e, 0x73, 0x74
+ 0x21, 0x77, 0x7d, 0x7f, 0x96, /* Required for all devices */
+ 0x89, 0x90, 0xab, /* Required for device managed */
+ 0x85, /* Required for defragmentation */
+ 0x5a, 0x5f, 0x6e, 0x73, 0x74 /* Others */
};
-
/* device_control is really 0x95, but 0x96 specified as it is the usage of
-the only field in that report */
+ *the only field in that report
+ */
/* Value usage tables used to put fields and values into arrays */
-#define PID_EFFECT_BLOCK_INDEX 0
+#define PID_EFFECT_BLOCK_INDEX 0
+#define PID_PARAM_BLOCK_OFFSET 0
-#define PID_DURATION 1
-#define PID_GAIN 2
-#define PID_TRIGGER_BUTTON 3
-#define PID_TRIGGER_REPEAT_INT 4
-#define PID_DIRECTION_ENABLE 5
-#define PID_START_DELAY 6
+#define PID_INDEX_PLACEHOLDER 0xff
+#define PID_EFFECT_BLOCK_INDEX_CODE 0x22
+#define PID_PARAM_BLOCK_OFFSET_CODE 0x23
+
+#define PID_DURATION 1
+#define PID_TRIGGER_BUTTON 2
+#define PID_TRIGGER_REPEAT_INT 3
+#define PID_DIRECTION_ENABLE 4
+#define PID_START_DELAY 5
static const u8 pidff_set_effect[] = {
- 0x22, 0x50, 0x52, 0x53, 0x54, 0x56, 0xa7
+ 0x22, 0x50, 0x53, 0x54, 0x56, 0xa7
};
+#define PID_GAIN 0
+static const u8 pidff_set_effect_optional[] = { 0x52 };
+
#define PID_ATTACK_LEVEL 1
#define PID_ATTACK_TIME 2
#define PID_FADE_LEVEL 3
#define PID_FADE_TIME 4
-static const u8 pidff_set_envelope[] = { 0x22, 0x5b, 0x5c, 0x5d, 0x5e };
+static const u8 pidff_set_envelope[] = {
+ PID_INDEX_PLACEHOLDER, 0x5b, 0x5c, 0x5d, 0x5e
+};
+
-#define PID_PARAM_BLOCK_OFFSET 1
-#define PID_CP_OFFSET 2
-#define PID_POS_COEFFICIENT 3
-#define PID_NEG_COEFFICIENT 4
-#define PID_POS_SATURATION 5
-#define PID_NEG_SATURATION 6
-#define PID_DEAD_BAND 7
+#define PID_CP_OFFSET 1
+#define PID_POS_COEFFICIENT 2
+#define PID_NEG_COEFFICIENT 3
+#define PID_POS_SATURATION 4
+#define PID_NEG_SATURATION 5
+#define PID_DEAD_BAND 6
static const u8 pidff_set_condition[] = {
- 0x22, 0x23, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65
+ PID_INDEX_PLACEHOLDER, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65
};
-#define PID_MAGNITUDE 1
+#define PID_MAGNITUDE 1
#define PID_OFFSET 2
#define PID_PHASE 3
#define PID_PERIOD 4
-static const u8 pidff_set_periodic[] = { 0x22, 0x70, 0x6f, 0x71, 0x72 };
-static const u8 pidff_set_constant[] = { 0x22, 0x70 };
+static const u8 pidff_set_periodic[] = {
+ PID_INDEX_PLACEHOLDER, 0x70, 0x6f, 0x71, 0x72
+};
+
+static const u8 pidff_set_constant[] = { PID_INDEX_PLACEHOLDER, 0x70 };
#define PID_RAMP_START 1
#define PID_RAMP_END 2
-static const u8 pidff_set_ramp[] = { 0x22, 0x75, 0x76 };
+static const u8 pidff_set_ramp[] = { PID_INDEX_PLACEHOLDER, 0x75, 0x76 };
#define PID_RAM_POOL_AVAILABLE 1
static const u8 pidff_block_load[] = { 0x22, 0xac };
@@ -113,10 +138,16 @@ static const u8 pidff_block_free[] = { 0
#define PID_DEVICE_GAIN_FIELD 0
static const u8 pidff_device_gain[] = { 0x7e };
-#define PID_RAM_POOL_SIZE 0
+#define PID_RAM_POOL_SIZE 0
#define PID_SIMULTANEOUS_MAX 1
#define PID_DEVICE_MANAGED_POOL 2
-static const u8 pidff_pool[] = { 0x80, 0x83, 0xa9 };
+#define PID_POOL_ALIGNMENT 3
+static const u8 pidff_pool[] = { 0x80, 0x83, 0xa9, 0x84 };
+
+#define PID_MOVE_SOURCE 0
+#define PID_MOVE_DESTINATION 1
+#define PID_MOVE_LENGTH 2
+static const u8 pidff_pool_move[] = { 0x86, 0x87, 0x88 };
/* Special field key tables used to put special field keys into arrays */
@@ -148,17 +179,40 @@ static const u8 pidff_block_load_status[
#define PID_EFFECT_STOP 1
static const u8 pidff_effect_operation_status[] = { 0x79, 0x7b };
+/* Flags to indicate capabilities of the device */
+
+#define PID_SUPPORTS_DEVICE_MANAGED 1
+#define PID_SUPPORTS_POOL_MOVE 2
+
struct pidff_usage {
struct hid_field *field;
s32 *value;
};
+struct pidff_memory_block {
+ unsigned int block_index; /* Effect block index */
+ unsigned int block_offset; /* Effect block offset */
+ unsigned int size; /* Block size (reserved memory) */
+ u8 offset_num;
+
+ struct list_head list;
+};
+
+struct pidff_info {
+ int id;
+ int effect_type_id;
+ struct pidff_memory_block *offset[PID_AXES_MAX];
+};
+
struct pidff_device {
struct hid_device *hid;
struct hid_report *reports[sizeof(pidff_reports)];
+ int report_size[sizeof(pidff_reports)];
struct pidff_usage set_effect[sizeof(pidff_set_effect)];
+ struct pidff_usage set_effect_optional[sizeof(pidff_set_effect_optional)];
+ struct pidff_usage block_offset[PID_AXES_MAX];
struct pidff_usage set_envelope[sizeof(pidff_set_envelope)];
struct pidff_usage set_condition[sizeof(pidff_set_condition)];
struct pidff_usage set_periodic[sizeof(pidff_set_periodic)];
@@ -168,11 +222,13 @@ struct pidff_device {
struct pidff_usage device_gain[sizeof(pidff_device_gain)];
struct pidff_usage block_load[sizeof(pidff_block_load)];
struct pidff_usage pool[sizeof(pidff_pool)];
+ struct pidff_usage pool_move[sizeof(pidff_pool_move)];
struct pidff_usage effect_operation[sizeof(pidff_effect_operation)];
struct pidff_usage block_free[sizeof(pidff_block_free)];
/* Special field is a field that is not composed of
- usage<->value pairs that pidff_usage values are */
+ * usage<->value pairs that pidff_usage values are
+ */
/* Special field in create_new_effect */
struct hid_field *create_new_effect_type;
@@ -180,6 +236,7 @@ struct pidff_device {
/* Special fields in set_effect */
struct hid_field *set_effect_type;
struct hid_field *effect_direction;
+ struct hid_field *axes_enable;
/* Special field in device_control */
struct hid_field *device_control;
@@ -195,10 +252,347 @@ struct pidff_device {
int status_id[sizeof(pidff_block_load_status)];
int operation_id[sizeof(pidff_effect_operation_status)];
- int pid_id[PID_EFFECTS_MAX];
+ unsigned int pid_total_ram, pid_used_ram;
+ int max_effects;
+
+ unsigned long flags;
+
+ struct pidff_info effect[PID_EFFECTS_MAX];
+ struct pidff_info active;
+ int active_effect_id;
+
+ /* struct pidff_memory_block *memory; */
+ struct list_head memory;
+ int alignment;
};
/*
+ * Calculate a report storage size in device memory, i.e. the report size
+ * minus block offsets and effect id. Calculation is used as a fallback, if
+ * device does not fill the report sizes in PID_POOL. The resulting size may
+ * not be correct, but it's the best guess we can do...
+ */
+static int pidff_calculate_report_store_size(struct hid_report *report,
+ struct pidff_usage *usage)
+{
+ if (report->field[0]->logical == (HID_UP_PID |
+ pidff_reports[PID_SET_EFFECT]))
+ return (report->size
+ - usage[PID_EFFECT_BLOCK_INDEX].field->report_size) / 8;
+ else
+ return (report->size
+ - usage[PID_PARAM_BLOCK_OFFSET].field->report_size) / 8;
+}
+
+/*
+ * Get the report size when stored into device memory.
+ */
+static int pidff_report_store_size(struct pidff_device *pidff, int report)
+{
+ int size;
+
+ size = pidff->report_size[report];
+ if (size) {
+ /* hid_dbg(pidff->hid, "Report %d size %d.\n", report, size); */
+ return size;
+ }
+
+ /* Fallback, calculate size */
+ hid_warn(pidff->hid, "Calculating size for report %d.\n", report);
+ switch (report) {
+ case PID_SET_EFFECT:
+ /* Calculation removed since it seemed to cause issues
+ * pidff->report_size[report] = pidff_calculate_report_store_size(
+ * pidff->reports[PID_SET_EFFECT], pidff->set_effect);
+ */
+ /* If set report is not defined, set it to zero (not stored in pool) */
+ pidff->report_size[report] = 0;
+ break;
+ case PID_SET_ENVELOPE:
+ pidff->report_size[report] = pidff_calculate_report_store_size(
+ pidff->reports[PID_SET_ENVELOPE], pidff->set_envelope);
+ break;
+ case PID_SET_CONDITION:
+ pidff->report_size[report] = pidff_calculate_report_store_size(
+ pidff->reports[PID_SET_CONDITION],
+ pidff->set_condition);
+ break;
+ case PID_SET_PERIODIC:
+ pidff->report_size[report] = pidff_calculate_report_store_size(
+ pidff->reports[PID_SET_PERIODIC], pidff->set_periodic);
+ break;
+ case PID_SET_CONSTANT:
+ pidff->report_size[report] = pidff_calculate_report_store_size(
+ pidff->reports[PID_SET_CONSTANT], pidff->set_constant);
+ break;
+ case PID_SET_RAMP:
+ pidff->report_size[report] = pidff_calculate_report_store_size(
+ pidff->reports[PID_SET_RAMP], pidff->set_ramp);
+ break;
+ default:
+ hid_dbg(pidff->hid, "Unknown report size queried\n");
+ return 0;
+ };
+ hid_warn(pidff->hid, "Calculated to be %d bytes.\n", pidff->report_size[report]);
+ return pidff->report_size[report];
+}
+
+/*
+ * Find report store sizes in PID Pool report, if in driver managed mode.
+ */
+static void pidff_report_sizes(struct pidff_device *pidff)
+{
+ int i, j, k;
+ struct hid_report *report = pidff->reports[PID_POOL];
+
+ if (!report)
+ return;
+
+ for (i = 0; i < report->maxfield; i++) {
+ if (report->field[i]->maxusage !=
+ report->field[i]->report_count)
+ continue;
+
+ /* Parameter block size report = 0xA8 */
+ if ((report->field[i]->logical & 0xff) != 0xa8)
+ continue;
+
+ for (j = 0; j < report->field[i]->maxusage; j++) {
+ for (k = 0; k < sizeof(pidff_reports); k++) {
+ if (pidff_reports[k] ==
+ (report->field[i]->usage[j].hid
+ & 0xff)) {
+ pidff->report_size[k] =
+ report->field[i]->value[j];
+ break;
+ }
+ }
+ }
+ break;
+ }
+}
+
+/*
+ * Return a new free memory block offset. NULL on error.
+ */
+static struct pidff_memory_block *pidff_allocate_memory_block(
+ struct pidff_device *pidff, int size)
+{
+ struct pidff_memory_block *block, *new_block;
+ int offset, free_mem;
+
+ if (!size)
+ return NULL;
+
+ /* Make sure alignment is as the device wants */
+ size += size % pidff->alignment;
+
+ if (pidff->pid_total_ram < (pidff->pid_used_ram + size))
+ return NULL;
+
+ if (list_empty(&pidff->memory)) {
+ new_block = kzalloc(sizeof(*new_block),
+ GFP_KERNEL);
+ if (!new_block)
+ return NULL;
+
+ list_add(&new_block->list, &pidff->memory);
+
+ offset = pidff_report_store_size(pidff, PID_SET_EFFECT);
+ if (offset == 0) {
+ /* If SET_EFFECT report size is not defined, assume they
+ * are not stored in pool and blocks can be stored at the
+ * beginning of pool. However address 0 does not seem to
+ * work so starting at first aligned offset.
+ */
+ if(pidff->block_offset[0].field) {
+ offset = pidff->block_offset[0].field->logical_minimum;
+ } else {
+ offset = pidff->alignment; /* This is a hack... */
+ }
+ } else {
+ /* If SET_EFFECT size was defined, assume they are stored
+ * at the beginning of pool so the blocks must start after
+ * the maximum amount of effects.
+ */
+ offset += offset % pidff->alignment;
+ offset *= pidff->max_effects;
+ }
+
+ new_block->block_index = pidff->active.id;
+ new_block->block_offset = offset;
+ new_block->size = size;
+
+ pidff->pid_used_ram = offset + size;
+#ifdef DEBUG_MEM_ALLOC
+ hid_dbg(pidff->hid, "First block allocated at 0x%x, size %d, ram used %d\n",
+ new_block->block_offset, new_block->size,
+ pidff->pid_used_ram);
+#endif
+ return new_block;
+ }
+
+ /* Try to find first large enough memory slot */
+ list_for_each_entry(block, &pidff->memory, list) {
+ if (!list_is_last(&block->list, &pidff->memory)) {
+ free_mem = list_next_entry(block, list)->block_offset -
+ block->block_offset - block->size;
+
+ /* Found large enough memory slot */
+ if (free_mem >= size) {
+ new_block = kzalloc(sizeof(*new_block), GFP_KERNEL);
+ if (!new_block)
+ return NULL;
+
+ offset = block->block_offset + block->size;
+ new_block->block_index = pidff->active.id;
+ new_block->block_offset = offset;
+ new_block->size = size;
+
+ list_add(&new_block->list, &block->list);
+
+ pidff->pid_used_ram += size;
+
+#ifdef DEBUG_MEM_ALLOC
+ hid_dbg(pidff->hid, "Block allocated at 0x%x size %d, ram used%d\n",
+ new_block->block_offset,
+ new_block->size,
+ pidff->pid_used_ram);
+#endif
+ return new_block;
+ }
+
+ /* Last block and large enough area at the end */
+ } else if(size <= (pidff->pid_total_ram -
+ block->block_offset - block->size)) {
+ new_block = kzalloc(sizeof(*new_block), GFP_KERNEL);
+ if (!new_block)
+ return NULL;
+
+ offset = block->block_offset + block->size;
+ new_block->block_index = pidff->active.id;
+ new_block->block_offset = offset;
+ new_block->size = size;
+
+ list_add(&new_block->list, &block->list);
+
+ pidff->pid_used_ram += size;
+
+#ifdef DEBUG_MEM_ALLOC
+ hid_dbg(pidff->hid, "Block allocated at 0x%x size %d, ram used %d\n",
+ new_block->block_offset, size,
+ pidff->pid_used_ram);
+#endif
+ return new_block;
+
+ } else {
+ /* TODO: Enough free memory but not in consecutive
+ * area so need to move old blocks around (defragment)
+ * to fit the new effect. For now, just fail.
+ */
+ return NULL;
+ }
+ }
+ return NULL;
+}
+
+/*
+ * Free whole memory, i.e. delete the linked list
+ */
+static void pidff_empty_memory(struct pidff_device *pidff)
+{
+ struct pidff_memory_block *block, *temp;
+
+ list_for_each_entry_safe(block, temp, &pidff->memory, list) {
+ list_del(&block->list);
+ kfree(block);
+ }
+
+ pidff->pid_used_ram = 0;
+}
+
+/*
+ * Free an allocated memory block
+ */
+static void pidff_free_memory_block(struct pidff_device *pidff,
+ struct pidff_memory_block *block)
+{
+ list_del(&block->list);
+ pidff->pid_used_ram -= block->size;
+#ifdef DEBUG_MEM_ALLOC
+ hid_dbg(pidff->hid, "Block freed from 0x%x, ram used %d\n",
+ block->block_offset, pidff->pid_used_ram);
+#endif
+ kfree(block);
+}
+
+/*
+ * Get existing block or allocate a new block for effect info.
+ * n is which block offset/axis is used.
+ * 1 is magnitude, period, ramp or X axis
+ * 2 is envelope or Y axis
+ * Returns the offset or -1 on error.
+ */
+static int pidff_get_or_allocate_block(struct pidff_device *pidff,
+ int effect_id, int size, int n)
+{
+ int i, offset;
+ struct pidff_memory_block *block;
+
+ /* Offsets start from 1..., scale to 0... */
+ n--;
+ if (n < 0 || n >= PID_AXES_MAX)
+ return -1;
+
+ /* Make sure the size alignment is correct */
+ size += size % pidff->alignment;
+
+ for (i = 0; i < pidff->max_effects; i++) {
+ if (pidff->effect[i].id == effect_id) {
+ if (!pidff->effect[i].offset[n]) {
+ /* Memory not yet allocated */
+ block = pidff_allocate_memory_block(pidff, size);
+ if (!block)
+ return -1;
+
+ offset = block->block_offset;
+ block->offset_num = n;
+ pidff->effect[i].offset[n] = block;
+
+#ifdef DEBUG_MEM_ALLOC
+ hid_dbg(pidff->hid, "New block allocated");
+#endif
+ } else if (pidff->effect[i].offset[n]->size == size) {
+ /* Block can be re-used */
+ offset = pidff->effect[i].offset[n]->block_offset;
+#ifdef DEBUG_MEM_ALLOC
+ hid_dbg(pidff->hid, "Block re-used");
+#endif
+ } else {
+ /* Block was wrong size */
+#ifdef DEBUG_MEM_ALLOC
+ hid_dbg(pidff->hid, "Wrong size %d!=%d block re-allocated", pidff->effect[i].offset[n]->size, size);
+#endif
+ pidff_free_memory_block(pidff, pidff->effect[i].offset[n]);
+ block = pidff_allocate_memory_block(pidff, size);
+ if (!block)
+ return -1;
+
+ offset = block->block_offset;
+ block->offset_num = n;
+ pidff->effect[i].offset[n] = block;
+ }
+#ifdef DEBUG_MEM_ALLOC
+ hid_dbg(pidff->hid, "Block for %d (%d) at 0x%x\n",
+ effect_id, n+1, offset);
+#endif
+ return offset;
+ }
+ }
+ return -1;
+}
+
+/*
* Scale an unsigned value with range 0..max for the given field
*/
static int pidff_rescale(int i, int max, struct hid_field *field)
@@ -219,8 +613,12 @@ static int pidff_rescale_signed(int i, s
static void pidff_set(struct pidff_usage *usage, u16 value)
{
+ if (!usage)
+ return;
usage->value[0] = pidff_rescale(value, 0xffff, usage->field);
+#ifdef DEBUG_SCALING
pr_debug("calculated from %d to %d\n", value, usage->value[0]);
+#endif
}
static void pidff_set_signed(struct pidff_usage *usage, s16 value)
@@ -235,36 +633,53 @@ static void pidff_set_signed(struct pidf
usage->value[0] =
pidff_rescale(value, 0x7fff, usage->field);
}
+#ifdef DEBUG_SCALING
pr_debug("calculated from %d to %d\n", value, usage->value[0]);
+#endif
}
/*
* Send envelope report to the device
*/
-static void pidff_set_envelope_report(struct pidff_device *pidff,
+static int pidff_set_envelope_report(struct pidff_device *pidff,
struct ff_envelope *envelope)
{
- pidff->set_envelope[PID_EFFECT_BLOCK_INDEX].value[0] =
- pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0];
+ int offset;
+
+ if (IS_DEVICE_MANAGED(pidff)) {
+ pidff->set_envelope[PID_EFFECT_BLOCK_INDEX].value[0] =
+ pidff->active.id;
+ } else {
+ offset = pidff_get_or_allocate_block(pidff, pidff->active.id,
+ pidff_report_store_size(pidff,
+ PID_SET_ENVELOPE), 2);
+ if (offset < 0)
+ return -ENOSPC;
+
+ pidff->set_envelope[PID_PARAM_BLOCK_OFFSET].value[0] = offset;
+ }
pidff->set_envelope[PID_ATTACK_LEVEL].value[0] =
pidff_rescale(envelope->attack_level >
- 0x7fff ? 0x7fff : envelope->attack_level, 0x7fff,
- pidff->set_envelope[PID_ATTACK_LEVEL].field);
+ 0x7fff ? 0x7fff : envelope->attack_level, 0x7fff,
+ pidff->set_envelope[PID_ATTACK_LEVEL].field);
pidff->set_envelope[PID_FADE_LEVEL].value[0] =
pidff_rescale(envelope->fade_level >
- 0x7fff ? 0x7fff : envelope->fade_level, 0x7fff,
- pidff->set_envelope[PID_FADE_LEVEL].field);
+ 0x7fff ? 0x7fff : envelope->fade_level, 0x7fff,
+ pidff->set_envelope[PID_FADE_LEVEL].field);
pidff->set_envelope[PID_ATTACK_TIME].value[0] = envelope->attack_length;
pidff->set_envelope[PID_FADE_TIME].value[0] = envelope->fade_length;
+#ifdef DEBUG_SCALING
hid_dbg(pidff->hid, "attack %u => %d\n",
envelope->attack_level,
pidff->set_envelope[PID_ATTACK_LEVEL].value[0]);
+#endif
hid_hw_request(pidff->hid, pidff->reports[PID_SET_ENVELOPE],
- HID_REQ_SET_REPORT);
+ HID_REQ_SET_REPORT);
+ return 0;
}
/*
@@ -282,16 +697,30 @@ static int pidff_needs_set_envelope(stru
/*
* Send constant force report to the device
*/
-static void pidff_set_constant_force_report(struct pidff_device *pidff,
+static int pidff_set_constant_force_report(struct pidff_device *pidff,
struct ff_effect *effect)
{
- pidff->set_constant[PID_EFFECT_BLOCK_INDEX].value[0] =
- pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0];
+ int offset;
+
+ if (IS_DEVICE_MANAGED(pidff)) {
+ pidff->set_constant[PID_EFFECT_BLOCK_INDEX].value[0] =
+ pidff->active.id;
+ } else {
+ offset = pidff_get_or_allocate_block(pidff, pidff->active.id,
+ pidff_report_store_size(pidff,
+ PID_SET_CONSTANT), 1);
+ if (offset < 0)
+ return -ENOSPC;
+
+ pidff->set_constant[PID_PARAM_BLOCK_OFFSET].value[0] = offset;
+ }
+
pidff_set_signed(&pidff->set_constant[PID_MAGNITUDE],
- effect->u.constant.level);
+ effect->u.constant.level);
hid_hw_request(pidff->hid, pidff->reports[PID_SET_CONSTANT],
- HID_REQ_SET_REPORT);
+ HID_REQ_SET_REPORT);
+ return 0;
}
/*
@@ -309,24 +738,70 @@ static int pidff_needs_set_constant(stru
static void pidff_set_effect_report(struct pidff_device *pidff,
struct ff_effect *effect)
{
+ int i;
+
pidff->set_effect[PID_EFFECT_BLOCK_INDEX].value[0] =
- pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0];
- pidff->set_effect_type->value[0] =
- pidff->create_new_effect_type->value[0];
- pidff->set_effect[PID_DURATION].value[0] = effect->replay.length;
+ pidff->active.id;
+
+ if (IS_DEVICE_MANAGED(pidff)) {
+ pidff->set_effect_type->value[0] =
+ pidff->create_new_effect_type->value[0];
+ } else {
+ pidff->set_effect_type->value[0] = pidff->active.effect_type_id;
+ if (pidff->active.offset[0]) {
+ pidff->block_offset[0].value[0] = pidff->active.
+ offset[0]->block_offset;
+ /* hid_dbg(pidff->hid, "Has offset 1 at 0x%x\n", pidff->active.offset[0]->block_offset); */
+ }
+ else
+ pidff->block_offset[0].value[0] = 0;
+
+ if (pidff->active.offset[1]) {
+ pidff->block_offset[1].value[0] = pidff->active.
+ offset[1]->block_offset;
+ /* hid_dbg(pidff->hid, "Has offset 2 at 0x%x\n", pidff->active.offset[1]->block_offset); */
+ }
+ else
+ pidff->block_offset[1].value[0] = 0;
+ }
+
+ if (effect->replay.length == 0) {
+ pidff->set_effect[PID_DURATION].value[0] =
+ (1U << pidff->set_effect[PID_DURATION].field->report_size) - 1;
+ hid_dbg(pidff->hid, "Set inf. length (%d) to 0x%x\n",
+ pidff->set_effect[PID_DURATION].field->report_size,
+ (1U << pidff->set_effect[PID_DURATION].field->report_size) - 1);
+ } else {
+ pidff->set_effect[PID_DURATION].value[0] =
+ effect->replay.length;
+ }
+
pidff->set_effect[PID_TRIGGER_BUTTON].value[0] = effect->trigger.button;
pidff->set_effect[PID_TRIGGER_REPEAT_INT].value[0] =
effect->trigger.interval;
- pidff->set_effect[PID_GAIN].value[0] =
- pidff->set_effect[PID_GAIN].field->logical_maximum;
- pidff->set_effect[PID_DIRECTION_ENABLE].value[0] = 1;
- pidff->effect_direction->value[0] =
- pidff_rescale(effect->direction, 0xffff,
+ if (pidff->set_effect_optional[PID_GAIN].value)
+ pidff->set_effect_optional[PID_GAIN].value[0] =
+ pidff->set_effect_optional[PID_GAIN].field->logical_maximum;
+
+ if ((effect->type == FF_SPRING || effect->type == FF_DAMPER ||
+ effect->type == FF_FRICTION || effect->type != FF_INERTIA) &&
+ pidff->axes_enable) {
+ pidff->set_effect[PID_DIRECTION_ENABLE].value[0] = 0;
+
+ for (i = 0; i < pidff->axes_enable->report_count && i < 2; i++) {
+ pidff->axes_enable->value[i] = 1;
+ }
+ } else {
+ pidff->set_effect[PID_DIRECTION_ENABLE].value[0] = 1;
+ pidff->effect_direction->value[0] =
+ pidff_rescale(effect->direction, 0xffff,
pidff->effect_direction);
+ }
+
pidff->set_effect[PID_START_DELAY].value[0] = effect->replay.delay;
hid_hw_request(pidff->hid, pidff->reports[PID_SET_EFFECT],
- HID_REQ_SET_REPORT);
+ HID_REQ_SET_REPORT);
}
/*
@@ -345,21 +820,34 @@ static int pidff_needs_set_effect(struct
/*
* Send periodic effect report to the device
*/
-static void pidff_set_periodic_report(struct pidff_device *pidff,
+static int pidff_set_periodic_report(struct pidff_device *pidff,
struct ff_effect *effect)
{
- pidff->set_periodic[PID_EFFECT_BLOCK_INDEX].value[0] =
- pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0];
+ int offset;
+
+ if (IS_DEVICE_MANAGED(pidff)) {
+ pidff->set_periodic[PID_EFFECT_BLOCK_INDEX].value[0] =
+ pidff->active.id;
+ } else {
+ offset = pidff_get_or_allocate_block(pidff, pidff->active.id,
+ pidff_report_store_size(pidff,
+ PID_SET_PERIODIC), 1);
+ if (offset < 0)
+ return -ENOSPC;
+
+ pidff->set_periodic[PID_PARAM_BLOCK_OFFSET].value[0] = offset;
+ }
+
pidff_set_signed(&pidff->set_periodic[PID_MAGNITUDE],
- effect->u.periodic.magnitude);
+ effect->u.periodic.magnitude);
pidff_set_signed(&pidff->set_periodic[PID_OFFSET],
- effect->u.periodic.offset);
+ effect->u.periodic.offset);
pidff_set(&pidff->set_periodic[PID_PHASE], effect->u.periodic.phase);
pidff->set_periodic[PID_PERIOD].value[0] = effect->u.periodic.period;
hid_hw_request(pidff->hid, pidff->reports[PID_SET_PERIODIC],
- HID_REQ_SET_REPORT);
-
+ HID_REQ_SET_REPORT);
+ return 0;
}
/*
@@ -377,31 +865,48 @@ static int pidff_needs_set_periodic(stru
/*
* Send condition effect reports to the device
*/
-static void pidff_set_condition_report(struct pidff_device *pidff,
+static int pidff_set_condition_report(struct pidff_device *pidff,
struct ff_effect *effect)
{
- int i;
+ int i, offset;
- pidff->set_condition[PID_EFFECT_BLOCK_INDEX].value[0] =
- pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0];
+ if (IS_DEVICE_MANAGED(pidff))
+ pidff->set_condition[PID_EFFECT_BLOCK_INDEX].value[0] =
+ pidff->active.id;
for (i = 0; i < 2; i++) {
- pidff->set_condition[PID_PARAM_BLOCK_OFFSET].value[0] = i;
+ if (IS_DEVICE_MANAGED(pidff)) {
+ pidff->set_condition[PID_PARAM_BLOCK_OFFSET].value[0] =
+ i;
+ } else {
+ offset = pidff_get_or_allocate_block(pidff,
+ pidff->active.id, pidff_report_store_size(pidff,
+ PID_SET_CONDITION), i+1);
+ if (offset < 0)
+ return -ENOSPC;
+
+ pidff->set_condition[PID_PARAM_BLOCK_OFFSET].value[0] =
+ offset;
+ }
+
pidff_set_signed(&pidff->set_condition[PID_CP_OFFSET],
- effect->u.condition[i].center);
+ effect->u.condition[i].center);
pidff_set_signed(&pidff->set_condition[PID_POS_COEFFICIENT],
- effect->u.condition[i].right_coeff);
+ effect->u.condition[i].right_coeff);
pidff_set_signed(&pidff->set_condition[PID_NEG_COEFFICIENT],
- effect->u.condition[i].left_coeff);
+ effect->u.condition[i].left_coeff);
pidff_set(&pidff->set_condition[PID_POS_SATURATION],
- effect->u.condition[i].right_saturation);
+ effect->u.condition[i].right_saturation);
pidff_set(&pidff->set_condition[PID_NEG_SATURATION],
- effect->u.condition[i].left_saturation);
+ effect->u.condition[i].left_saturation);
pidff_set(&pidff->set_condition[PID_DEAD_BAND],
- effect->u.condition[i].deadband);
+ effect->u.condition[i].deadband);
+
hid_hw_request(pidff->hid, pidff->reports[PID_SET_CONDITION],
- HID_REQ_SET_REPORT);
+ HID_REQ_SET_REPORT);
+ hid_hw_wait(pidff->hid);
}
+ return 0;
}
/*
@@ -418,11 +923,11 @@ static int pidff_needs_set_condition(str
struct ff_condition_effect *old_cond = &old->u.condition[i];
ret |= cond->center != old_cond->center ||
- cond->right_coeff != old_cond->right_coeff ||
- cond->left_coeff != old_cond->left_coeff ||
- cond->right_saturation != old_cond->right_saturation ||
- cond->left_saturation != old_cond->left_saturation ||
- cond->deadband != old_cond->deadband;
+ cond->right_coeff != old_cond->right_coeff ||
+ cond->left_coeff != old_cond->left_coeff ||
+ cond->right_saturation != old_cond->right_saturation ||
+ cond->left_saturation != old_cond->left_saturation ||
+ cond->deadband != old_cond->deadband;
}
return ret;
@@ -431,17 +936,31 @@ static int pidff_needs_set_condition(str
/*
* Send ramp force report to the device
*/
-static void pidff_set_ramp_force_report(struct pidff_device *pidff,
+static int pidff_set_ramp_force_report(struct pidff_device *pidff,
struct ff_effect *effect)
{
- pidff->set_ramp[PID_EFFECT_BLOCK_INDEX].value[0] =
- pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0];
+ int offset;
+
+ if (IS_DEVICE_MANAGED(pidff)) {
+ pidff->set_ramp[PID_EFFECT_BLOCK_INDEX].value[0] =
+ pidff->active.id;
+ } else {
+ offset = pidff_get_or_allocate_block(pidff, pidff->active.id,
+ pidff_report_store_size(pidff, PID_SET_RAMP),
+ 1);
+ if (offset < 0)
+ return -ENOSPC;
+
+ pidff->set_ramp[PID_PARAM_BLOCK_OFFSET].value[0] = offset;
+ }
+
pidff_set_signed(&pidff->set_ramp[PID_RAMP_START],
- effect->u.ramp.start_level);
+ effect->u.ramp.start_level);
pidff_set_signed(&pidff->set_ramp[PID_RAMP_END],
- effect->u.ramp.end_level);
+ effect->u.ramp.end_level);
hid_hw_request(pidff->hid, pidff->reports[PID_SET_RAMP],
- HID_REQ_SET_REPORT);
+ HID_REQ_SET_REPORT);
+ return 0;
}
/*
@@ -464,36 +983,63 @@ static int pidff_request_effect_upload(s
{
int j;
- pidff->create_new_effect_type->value[0] = efnum;
- hid_hw_request(pidff->hid, pidff->reports[PID_CREATE_NEW_EFFECT],
+ if (IS_DEVICE_MANAGED(pidff)) {
+ pidff->create_new_effect_type->value[0] = efnum;
+ hid_hw_request(pidff->hid,
+ pidff->reports[PID_CREATE_NEW_EFFECT],
HID_REQ_SET_REPORT);
- hid_dbg(pidff->hid, "create_new_effect sent, type: %d\n", efnum);
+ hid_dbg(pidff->hid, "create_new_effect sent, type: %d\n",
+ efnum);
- pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0] = 0;
- pidff->block_load_status->value[0] = 0;
- hid_hw_wait(pidff->hid);
+ pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0] = 0;
+ pidff->block_load_status->value[0] = 0;
+ hid_hw_wait(pidff->hid);
- for (j = 0; j < 60; j++) {
- hid_dbg(pidff->hid, "pid_block_load requested\n");
- hid_hw_request(pidff->hid, pidff->reports[PID_BLOCK_LOAD],
+ for (j = 0; j < 60; j++) {
+ hid_dbg(pidff->hid, "pid_block_load requested\n");
+ hid_hw_request(pidff->hid,
+ pidff->reports[PID_BLOCK_LOAD],
HID_REQ_GET_REPORT);
- hid_hw_wait(pidff->hid);
- if (pidff->block_load_status->value[0] ==
- pidff->status_id[PID_BLOCK_LOAD_SUCCESS]) {
- hid_dbg(pidff->hid, "device reported free memory: %d bytes\n",
- pidff->block_load[PID_RAM_POOL_AVAILABLE].value ?
- pidff->block_load[PID_RAM_POOL_AVAILABLE].value[0] : -1);
- return 0;
+ hid_hw_wait(pidff->hid);