Debian Codegraph @

drivers/hid/usbhid/hid-pidff.c

raw
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Force feedback driver for USB HID PID compliant devices
4 *
5 * Copyright (c) 2005, 2006 Anssi Hannula <[email protected]>
6 * Upgraded 2025 by Oleg Makarenko and Tomasz PakuĊ‚a
7 */
8
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
11 #include "hid-pidff.h"
12 #include <linux/hid.h>
13 #include <linux/input.h>
14 #include <linux/math64.h>
15 #include <linux/minmax.h>
16 #include <linux/slab.h>
17 #include <linux/usb.h>
18
19 #define PID_EFFECTS_MAX 64
20 #define PID_INFINITE U16_MAX
21
22 /* Linux Force Feedback API uses miliseconds as time unit */
23 #define FF_TIME_EXPONENT -3
24 #define FF_INFINITE 0
25
26 /* Report usage table used to put reports into an array */
27 #define PID_SET_EFFECT 0
28 #define PID_EFFECT_OPERATION 1
29 #define PID_DEVICE_GAIN 2
30 #define PID_POOL 3
31 #define PID_BLOCK_LOAD 4
32 #define PID_BLOCK_FREE 5
33 #define PID_DEVICE_CONTROL 6
34 #define PID_CREATE_NEW_EFFECT 7
35
36 #define PID_REQUIRED_REPORTS 8
37
38 #define PID_SET_ENVELOPE 8
39 #define PID_SET_CONDITION 9
40 #define PID_SET_PERIODIC 10
41 #define PID_SET_CONSTANT 11
42 #define PID_SET_RAMP 12
43 static const u8 pidff_reports[] = {
44 0x21, 0x77, 0x7d, 0x7f, 0x89, 0x90, 0x96, 0xab,
45 0x5a, 0x5f, 0x6e, 0x73, 0x74
46 };
47 /*
48 * device_control is really 0x95, but 0x96 specified
49 * as it is the usage of the only field in that report.
50 */
51
52 /* PID special fields */
53 #define PID_EFFECT_TYPE 0x25
54 #define PID_AXES_ENABLE 0x55
55 #define PID_DIRECTION 0x57
56 #define PID_EFFECT_OPERATION_ARRAY 0x78
57 #define PID_BLOCK_LOAD_STATUS 0x8b
58 #define PID_DEVICE_CONTROL_ARRAY 0x96
59
60 /* Value usage tables used to put fields and values into arrays */
61 #define PID_EFFECT_BLOCK_INDEX 0
62
63 #define PID_DURATION 1
64 #define PID_GAIN 2
65 #define PID_TRIGGER_BUTTON 3
66 #define PID_TRIGGER_REPEAT_INT 4
67 #define PID_DIRECTION_ENABLE 5
68 #define PID_START_DELAY 6
69 static const u8 pidff_set_effect[] = {
70 0x22, 0x50, 0x52, 0x53, 0x54, 0x56, 0xa7
71 };
72
73 #define PID_ATTACK_LEVEL 1
74 #define PID_ATTACK_TIME 2
75 #define PID_FADE_LEVEL 3
76 #define PID_FADE_TIME 4
77 static const u8 pidff_set_envelope[] = { 0x22, 0x5b, 0x5c, 0x5d, 0x5e };
78
79 #define PID_PARAM_BLOCK_OFFSET 1
80 #define PID_CP_OFFSET 2
81 #define PID_POS_COEFFICIENT 3
82 #define PID_NEG_COEFFICIENT 4
83 #define PID_POS_SATURATION 5
84 #define PID_NEG_SATURATION 6
85 #define PID_DEAD_BAND 7
86 static const u8 pidff_set_condition[] = {
87 0x22, 0x23, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65
88 };
89
90 #define PID_MAGNITUDE 1
91 #define PID_OFFSET 2
92 #define PID_PHASE 3
93 #define PID_PERIOD 4
94 static const u8 pidff_set_periodic[] = { 0x22, 0x70, 0x6f, 0x71, 0x72 };
95 static const u8 pidff_set_constant[] = { 0x22, 0x70 };
96
97 #define PID_RAMP_START 1
98 #define PID_RAMP_END 2
99 static const u8 pidff_set_ramp[] = { 0x22, 0x75, 0x76 };
100
101 #define PID_RAM_POOL_AVAILABLE 1
102 static const u8 pidff_block_load[] = { 0x22, 0xac };
103
104 #define PID_LOOP_COUNT 1
105 static const u8 pidff_effect_operation[] = { 0x22, 0x7c };
106
107 static const u8 pidff_block_free[] = { 0x22 };
108
109 #define PID_DEVICE_GAIN_FIELD 0
110 static const u8 pidff_device_gain[] = { 0x7e };
111
112 #define PID_RAM_POOL_SIZE 0
113 #define PID_SIMULTANEOUS_MAX 1
114 #define PID_DEVICE_MANAGED_POOL 2
115 static const u8 pidff_pool[] = { 0x80, 0x83, 0xa9 };
116
117 /* Special field key tables used to put special field keys into arrays */
118 #define PID_ENABLE_ACTUATORS 0
119 #define PID_DISABLE_ACTUATORS 1
120 #define PID_STOP_ALL_EFFECTS 2
121 #define PID_RESET 3
122 #define PID_PAUSE 4
123 #define PID_CONTINUE 5
124 static const u8 pidff_device_control[] = { 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c };
125
126 #define PID_CONSTANT 0
127 #define PID_RAMP 1
128 #define PID_SQUARE 2
129 #define PID_SINE 3
130 #define PID_TRIANGLE 4
131 #define PID_SAW_UP 5
132 #define PID_SAW_DOWN 6
133 #define PID_SPRING 7
134 #define PID_DAMPER 8
135 #define PID_INERTIA 9
136 #define PID_FRICTION 10
137 static const u8 pidff_effect_types[] = {
138 0x26, 0x27, 0x30, 0x31, 0x32, 0x33, 0x34,
139 0x40, 0x41, 0x42, 0x43
140 };
141
142 #define PID_BLOCK_LOAD_SUCCESS 0
143 #define PID_BLOCK_LOAD_FULL 1
144 #define PID_BLOCK_LOAD_ERROR 2
145 static const u8 pidff_block_load_status[] = { 0x8c, 0x8d, 0x8e };
146
147 #define PID_EFFECT_START 0
148 #define PID_EFFECT_STOP 1
149 static const u8 pidff_effect_operation_status[] = { 0x79, 0x7b };
150
151 #define PID_DIRECTION_NORTH 0x0000
152 #define PID_DIRECTION_EAST 0x4000
153 #define PID_DIRECTION_SOUTH 0x8000
154 #define PID_DIRECTION_WEST 0xc000
155
156 #define PIDFF_FIXED_WHEEL_DIRECTION PID_DIRECTION_EAST
157
158 /* AXES_ENABLE and DIRECTION axes */
159 enum pid_axes {
160 PID_AXIS_X,
161 PID_AXIS_Y,
162 PID_AXIS_Z,
163 PID_AXIS_RX,
164 PID_AXIS_RY,
165 PID_AXIS_RZ,
166 PID_AXIS_SLIDER,
167 PID_AXIS_DIAL,
168 PID_AXIS_WHEEL,
169 PID_AXES_COUNT,
170 };
171 static const u8 pidff_direction_axis[] = {
172 HID_USAGE & HID_GD_X,
173 HID_USAGE & HID_GD_Y,
174 HID_USAGE & HID_GD_Z,
175 HID_USAGE & HID_GD_RX,
176 HID_USAGE & HID_GD_RY,
177 HID_USAGE & HID_GD_RZ,
178 HID_USAGE & HID_GD_SLIDER,
179 HID_USAGE & HID_GD_DIAL,
180 HID_USAGE & HID_GD_WHEEL,
181 };
182
183 struct pidff_usage {
184 struct hid_field *field;
185 s32 *value;
186 };
187
188 struct pidff_effect {
189 int pid_id;
190 int is_infinite;
191 unsigned int loop_count;
192 };
193
194 struct pidff_device {
195 struct hid_device *hid;
196
197 struct hid_report *reports[ARRAY_SIZE(pidff_reports)];
198
199 struct pidff_usage set_effect[ARRAY_SIZE(pidff_set_effect)];
200 struct pidff_usage set_envelope[ARRAY_SIZE(pidff_set_envelope)];
201 struct pidff_usage set_condition[ARRAY_SIZE(pidff_set_condition)];
202 struct pidff_usage set_periodic[ARRAY_SIZE(pidff_set_periodic)];
203 struct pidff_usage set_constant[ARRAY_SIZE(pidff_set_constant)];
204 struct pidff_usage set_ramp[ARRAY_SIZE(pidff_set_ramp)];
205
206 struct pidff_usage device_gain[ARRAY_SIZE(pidff_device_gain)];
207 struct pidff_usage block_load[ARRAY_SIZE(pidff_block_load)];
208 struct pidff_usage pool[ARRAY_SIZE(pidff_pool)];
209 struct pidff_usage effect_operation[ARRAY_SIZE(pidff_effect_operation)];
210 struct pidff_usage block_free[ARRAY_SIZE(pidff_block_free)];
211
212 struct pidff_effect effect[PID_EFFECTS_MAX];
213
214 /*
215 * Special field is a field that is not composed of
216 * usage<->value pairs that pidff_usage values are
217 */
218
219 /* Special field in create_new_effect */
220 struct hid_field *create_new_effect_type;
221
222 /* Special fields in set_effect */
223 struct hid_field *set_effect_type;
224 struct hid_field *effect_direction;
225 struct hid_field *axes_enable;
226
227 /* Special field in device_control */
228 struct hid_field *device_control;
229
230 /* Special field in block_load */
231 struct hid_field *block_load_status;
232
233 /* Special field in effect_operation */
234 struct hid_field *effect_operation_status;
235
236 int control_id[ARRAY_SIZE(pidff_device_control)];
237 int type_id[ARRAY_SIZE(pidff_effect_types)];
238 int status_id[ARRAY_SIZE(pidff_block_load_status)];
239 int operation_id[ARRAY_SIZE(pidff_effect_operation_status)];
240 int direction_axis_id[ARRAY_SIZE(pidff_direction_axis)];
241
242 u32 quirks;
243 u8 effect_count;
244 u8 axis_count;
245 };
246
247 static int pidff_is_effect_conditional(struct ff_effect *effect)
248 {
249 return effect->type == FF_SPRING ||
250 effect->type == FF_DAMPER ||
251 effect->type == FF_INERTIA ||
252 effect->type == FF_FRICTION;
253 }
254
255 static int pidff_is_duration_infinite(u16 duration)
256 {
257 return duration == FF_INFINITE || duration == PID_INFINITE;
258 }
259
260 /*
261 * Get PID effect index from FF effect type.
262 * Return 0 if invalid.
263 */
264 static int pidff_effect_ff_to_pid(struct ff_effect *effect)
265 {
266 switch (effect->type) {
267 case FF_CONSTANT:
268 return PID_CONSTANT;
269 case FF_RAMP:
270 return PID_RAMP;
271 case FF_SPRING:
272 return PID_SPRING;
273 case FF_DAMPER:
274 return PID_DAMPER;
275 case FF_INERTIA:
276 return PID_INERTIA;
277 case FF_FRICTION:
278 return PID_FRICTION;
279 case FF_PERIODIC:
280 switch (effect->u.periodic.waveform) {
281 case FF_SQUARE:
282 return PID_SQUARE;
283 case FF_TRIANGLE:
284 return PID_TRIANGLE;
285 case FF_SINE:
286 return PID_SINE;
287 case FF_SAW_UP:
288 return PID_SAW_UP;
289 case FF_SAW_DOWN:
290 return PID_SAW_DOWN;
291 }
292 }
293 pr_err("invalid effect type\n");
294 return -EINVAL;
295 }
296
297 /*
298 * Get effect id in the device descriptor.
299 * Return 0 if invalid.
300 */
301 static int pidff_get_effect_type_id(struct pidff_device *pidff,
302 struct ff_effect *effect)
303 {
304 int id = pidff_effect_ff_to_pid(effect);
305
306 if (id < 0)
307 return 0;
308
309 if (effect->type == FF_PERIODIC &&
310 pidff->quirks & HID_PIDFF_QUIRK_PERIODIC_SINE_ONLY)
311 id = PID_SINE;
312
313 return pidff->type_id[id];
314 }
315
316 /*
317 * Clamp value for a given field
318 */
319 static s32 pidff_clamp(s32 i, struct hid_field *field)
320 {
321 return (s32)clamp(i, field->logical_minimum, field->logical_maximum);
322 }
323
324 /*
325 * Scale an unsigned value with range 0..max for the given field
326 */
327 static int pidff_rescale(int i, int max, struct hid_field *field)
328 {
329 /* 64 bits needed for big values during rescale */
330 s64 result = field->logical_maximum - field->logical_minimum;
331
332 return div_s64(result * i, max) + field->logical_minimum;
333 }
334
335 /*
336 * Scale a signed value in range S16_MIN..S16_MAX for the given field
337 */
338 static int pidff_rescale_signed(int i, struct hid_field *field)
339 {
340 if (i > 0)
341 return i * field->logical_maximum / S16_MAX;
342 if (i < 0)
343 return i * field->logical_minimum / S16_MIN;
344 return 0;
345 }
346
347 /*
348 * Scale time value from Linux default (ms) to field units
349 */
350 static u32 pidff_rescale_time(u16 time, struct hid_field *field)
351 {
352 u32 scaled_time = time;
353 int exponent = field->unit_exponent;
354
355 pr_debug("time field exponent: %d\n", exponent);
356 for (; exponent < FF_TIME_EXPONENT; exponent++)
357 scaled_time *= 10;
358 for (; exponent > FF_TIME_EXPONENT; exponent--)
359 scaled_time /= 10;
360
361 pr_debug("time calculated from %d to %d\n", time, scaled_time);
362 return scaled_time;
363 }
364
365 static void pidff_set(struct pidff_usage *usage, u16 value)
366 {
367 usage->value[0] = pidff_rescale(value, U16_MAX, usage->field);
368 pr_debug("calculated from %d to %d\n", value, usage->value[0]);
369 }
370
371 static void pidff_set_signed(struct pidff_usage *usage, s16 value)
372 {
373 if (usage->field->logical_minimum < 0)
374 usage->value[0] = pidff_rescale_signed(value, usage->field);
375 else {
376 if (value < 0)
377 usage->value[0] =
378 pidff_rescale(-value, -S16_MIN, usage->field);
379 else
380 usage->value[0] =
381 pidff_rescale(value, S16_MAX, usage->field);
382 }
383 pr_debug("calculated from %d to %d\n", value, usage->value[0]);
384 }
385
386 static void pidff_set_time(struct pidff_usage *usage, u16 time)
387 {
388 usage->value[0] = pidff_clamp(pidff_rescale_time(time, usage->field),
389 usage->field);
390 }
391
392 static void pidff_set_duration(struct pidff_usage *usage, u16 duration)
393 {
394 /* PID defines INFINITE as the max possible value for duration field */
395 if (pidff_is_duration_infinite(duration)) {
396 usage->value[0] = (1U << usage->field->report_size) - 1;
397 return;
398 }
399
400 pidff_set_time(usage, duration);
401 }
402
403 static void pidff_set_effect_direction(struct pidff_device *pidff,
404 struct ff_effect *effect)
405 {
406 u16 direction = effect->direction;
407 int direction_enable = 1;
408
409 /* Use fixed direction if needed */
410 if (pidff->quirks & HID_PIDFF_QUIRK_FIX_CONDITIONAL_DIRECTION &&
411 pidff_is_effect_conditional(effect))
412 direction = PIDFF_FIXED_WHEEL_DIRECTION;
413
414 pidff->set_effect[PID_DIRECTION_ENABLE].value[0] = direction_enable;
415 pidff->effect_direction->value[0] =
416 pidff_rescale(direction, U16_MAX, pidff->effect_direction);
417
418 if (direction_enable)
419 return;
420
421 /*
422 * For use with improved FFB API
423 * We want to read the selected axes and their direction from the effect
424 * struct and only enable those. For now, enable all axes.
425 *
426 */
427 for (int i = 0; i < PID_AXES_COUNT; i++) {
428 /* HID index starts with 1 */
429 int index = pidff->direction_axis_id[i] - 1;
430
431 if (index < 0)
432 continue;
433
434 pidff->axes_enable->value[index] = 1;
435 pidff->effect_direction->value[index] = pidff_rescale(
436 direction, U16_MAX, pidff->effect_direction);
437 }
438 }
439
440 /*
441 * Send envelope report to the device
442 */
443 static void pidff_set_envelope_report(struct pidff_device *pidff,
444 struct ff_envelope *envelope)
445 {
446 pidff->set_envelope[PID_EFFECT_BLOCK_INDEX].value[0] =
447 pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0];
448
449 pidff->set_envelope[PID_ATTACK_LEVEL].value[0] =
450 pidff_rescale(envelope->attack_level >
451 S16_MAX ? S16_MAX : envelope->attack_level, S16_MAX,
452 pidff->set_envelope[PID_ATTACK_LEVEL].field);
453 pidff->set_envelope[PID_FADE_LEVEL].value[0] =
454 pidff_rescale(envelope->fade_level >
455 S16_MAX ? S16_MAX : envelope->fade_level, S16_MAX,
456 pidff->set_envelope[PID_FADE_LEVEL].field);
457
458 pidff_set_time(&pidff->set_envelope[PID_ATTACK_TIME],
459 envelope->attack_length);
460 pidff_set_time(&pidff->set_envelope[PID_FADE_TIME],
461 envelope->fade_length);
462
463 hid_hw_request(pidff->hid, pidff->reports[PID_SET_ENVELOPE],
464 HID_REQ_SET_REPORT);
465 }
466
467 /*
468 * Test if the new envelope differs from old one
469 */
470 static int pidff_needs_set_envelope(struct ff_envelope *envelope,
471 struct ff_envelope *old)
472 {
473 int needs_new_envelope;
474
475 needs_new_envelope = envelope->attack_level != 0 ||
476 envelope->fade_level != 0 ||
477 envelope->attack_length != 0 ||
478 envelope->fade_length != 0;
479
480 if (!needs_new_envelope)
481 return 0;
482 if (!old)
483 return needs_new_envelope;
484
485 return envelope->attack_level != old->attack_level ||
486 envelope->fade_level != old->fade_level ||
487 envelope->attack_length != old->attack_length ||
488 envelope->fade_length != old->fade_length;
489 }
490
491 /*
492 * Send constant force report to the device
493 */
494 static void pidff_set_constant_report(struct pidff_device *pidff,
495 struct ff_effect *effect)
496 {
497 pidff->set_constant[PID_EFFECT_BLOCK_INDEX].value[0] =
498 pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0];
499 pidff_set_signed(&pidff->set_constant[PID_MAGNITUDE],
500 effect->u.constant.level);
501
502 hid_hw_request(pidff->hid, pidff->reports[PID_SET_CONSTANT],
503 HID_REQ_SET_REPORT);
504 }
505
506 /*
507 * Test if the constant parameters have changed between effects
508 */
509 static int pidff_needs_set_constant(struct ff_effect *effect,
510 struct ff_effect *old)
511 {
512 return effect->u.constant.level != old->u.constant.level;
513 }
514
515 /*
516 * Send set effect report to the device
517 */
518 static void pidff_set_effect_report(struct pidff_device *pidff,
519 struct ff_effect *effect)
520 {
521 pidff->set_effect[PID_EFFECT_BLOCK_INDEX].value[0] =
522 pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0];
523 pidff->set_effect_type->value[0] =
524 pidff->create_new_effect_type->value[0];
525
526 pidff_set_duration(&pidff->set_effect[PID_DURATION],
527 effect->replay.length);
528
529 /* Some games set this to random values that can be out of range */
530 s32 trigger_button_max =
531 pidff->set_effect[PID_TRIGGER_BUTTON].field->logical_maximum;
532 if (effect->trigger.button <= trigger_button_max) {
533 pidff->set_effect[PID_TRIGGER_BUTTON].value[0] =
534 effect->trigger.button;
535 pidff_set_time(&pidff->set_effect[PID_TRIGGER_REPEAT_INT],
536 effect->trigger.interval);
537 } else {
538 pidff->set_effect[PID_TRIGGER_BUTTON].value[0] = 0;
539 pidff->set_effect[PID_TRIGGER_REPEAT_INT].value[0] = 0;
540 }
541
542 pidff->set_effect[PID_GAIN].value[0] =
543 pidff->set_effect[PID_GAIN].field->logical_maximum;
544
545 pidff_set_effect_direction(pidff, effect);
546
547 /* Omit setting delay field if it's missing */
548 if (!(pidff->quirks & HID_PIDFF_QUIRK_MISSING_DELAY))
549 pidff_set_time(&pidff->set_effect[PID_START_DELAY],
550 effect->replay.delay);
551
552 hid_hw_request(pidff->hid, pidff->reports[PID_SET_EFFECT],
553 HID_REQ_SET_REPORT);
554 }
555
556 /*
557 * Test if the values used in set_effect have changed
558 */
559 static int pidff_needs_set_effect(struct ff_effect *effect,
560 struct ff_effect *old)
561 {
562 return effect->replay.length != old->replay.length ||
563 effect->trigger.interval != old->trigger.interval ||
564 effect->trigger.button != old->trigger.button ||
565 effect->direction != old->direction ||
566 effect->replay.delay != old->replay.delay;
567 }
568
569 /*
570 * Send periodic effect report to the device
571 */
572 static void pidff_set_periodic_report(struct pidff_device *pidff,
573 struct ff_effect *effect)
574 {
575 pidff->set_periodic[PID_EFFECT_BLOCK_INDEX].value[0] =
576 pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0];
577 pidff_set_signed(&pidff->set_periodic[PID_MAGNITUDE],
578 effect->u.periodic.magnitude);
579 pidff_set_signed(&pidff->set_periodic[PID_OFFSET],
580 effect->u.periodic.offset);
581 pidff_set(&pidff->set_periodic[PID_PHASE], effect->u.periodic.phase);
582 pidff_set_time(&pidff->set_periodic[PID_PERIOD],
583 effect->u.periodic.period);
584
585 hid_hw_request(pidff->hid, pidff->reports[PID_SET_PERIODIC],
586 HID_REQ_SET_REPORT);
587 }
588
589 /*
590 * Test if periodic effect parameters have changed
591 */
592 static int pidff_needs_set_periodic(struct ff_effect *effect,
593 struct ff_effect *old)
594 {
595 return effect->u.periodic.magnitude != old->u.periodic.magnitude ||
596 effect->u.periodic.offset != old->u.periodic.offset ||
597 effect->u.periodic.phase != old->u.periodic.phase ||
598 effect->u.periodic.period != old->u.periodic.period;
599 }
600
601 /*
602 * Send condition effect reports to the device
603 */
604 static void pidff_set_condition_report(struct pidff_device *pidff,
605 struct ff_effect *effect)
606 {
607 int i, max_axis;
608
609 /* Devices missing Parameter Block Offset can only have one axis */
610 max_axis = pidff->quirks & HID_PIDFF_QUIRK_MISSING_PBO ? 1 : 2;
611
612 pidff->set_condition[PID_EFFECT_BLOCK_INDEX].value[0] =
613 pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0];
614
615 for (i = 0; i < max_axis; i++) {
616 /* Omit Parameter Block Offset if missing */
617 if (!(pidff->quirks & HID_PIDFF_QUIRK_MISSING_PBO))
618 pidff->set_condition[PID_PARAM_BLOCK_OFFSET].value[0] = i;
619
620 pidff_set_signed(&pidff->set_condition[PID_CP_OFFSET],
621 effect->u.condition[i].center);
622 pidff_set_signed(&pidff->set_condition[PID_POS_COEFFICIENT],
623 effect->u.condition[i].right_coeff);
624 pidff_set_signed(&pidff->set_condition[PID_NEG_COEFFICIENT],
625 effect->u.condition[i].left_coeff);
626 pidff_set(&pidff->set_condition[PID_POS_SATURATION],
627 effect->u.condition[i].right_saturation);
628 pidff_set(&pidff->set_condition[PID_NEG_SATURATION],
629 effect->u.condition[i].left_saturation);
630 pidff_set(&pidff->set_condition[PID_DEAD_BAND],
631 effect->u.condition[i].deadband);
632 hid_hw_request(pidff->hid, pidff->reports[PID_SET_CONDITION],
633 HID_REQ_SET_REPORT);
634 }
635 }
636
637 /*
638 * Test if condition effect parameters have changed
639 */
640 static int pidff_needs_set_condition(struct ff_effect *effect,
641 struct ff_effect *old)
642 {
643 int i;
644 int ret = 0;
645
646 for (i = 0; i < 2; i++) {
647 struct ff_condition_effect *cond = &effect->u.condition[i];
648 struct ff_condition_effect *old_cond = &old->u.condition[i];
649
650 ret |= cond->center != old_cond->center ||
651 cond->right_coeff != old_cond->right_coeff ||
652 cond->left_coeff != old_cond->left_coeff ||
653 cond->right_saturation != old_cond->right_saturation ||
654 cond->left_saturation != old_cond->left_saturation ||
655 cond->deadband != old_cond->deadband;
656 }
657
658 return ret;
659 }
660
661 /*
662 * Send ramp force report to the device
663 */
664 static void pidff_set_ramp_report(struct pidff_device *pidff,
665 struct ff_effect *effect)
666 {
667 pidff->set_ramp[PID_EFFECT_BLOCK_INDEX].value[0] =
668 pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0];
669 pidff_set_signed(&pidff->set_ramp[PID_RAMP_START],
670 effect->u.ramp.start_level);
671 pidff_set_signed(&pidff->set_ramp[PID_RAMP_END],
672 effect->u.ramp.end_level);
673 hid_hw_request(pidff->hid, pidff->reports[PID_SET_RAMP],
674 HID_REQ_SET_REPORT);
675 }
676
677 /*
678 * Test if ramp force parameters have changed
679 */
680 static int pidff_needs_set_ramp(struct ff_effect *effect, struct ff_effect *old)
681 {
682 return effect->u.ramp.start_level != old->u.ramp.start_level ||
683 effect->u.ramp.end_level != old->u.ramp.end_level;
684 }
685
686 /*
687 * Set device gain
688 */
689 static void pidff_set_gain_report(struct pidff_device *pidff, u16 gain)
690 {
691 if (!pidff->device_gain[PID_DEVICE_GAIN_FIELD].field)
692 return;
693
694 pidff_set(&pidff->device_gain[PID_DEVICE_GAIN_FIELD], gain);
695 hid_hw_request(pidff->hid, pidff->reports[PID_DEVICE_GAIN],
696 HID_REQ_SET_REPORT);
697 }
698
699 /*
700 * Send device control report to the device
701 */
702 static void pidff_set_device_control(struct pidff_device *pidff, int field)
703 {
704 const int field_index = pidff->control_id[field];
705
706 if (field_index < 1)
707 return;
708
709 /* Detect if the field is a bitmask variable or an array */
710 if (pidff->device_control->flags & HID_MAIN_ITEM_VARIABLE) {
711 hid_dbg(pidff->hid, "DEVICE_CONTROL is a bitmask\n");
712
713 /* Clear current bitmask */
714 for (int i = 0; i < ARRAY_SIZE(pidff_device_control); i++) {
715 int index = pidff->control_id[i];
716
717 if (index < 1)
718 continue;
719
720 pidff->device_control->value[index - 1] = 0;
721 }
722
723 pidff->device_control->value[field_index - 1] = 1;
724 } else {
725 hid_dbg(pidff->hid, "DEVICE_CONTROL is an array\n");
726 pidff->device_control->value[0] = field_index;
727 }
728
729 hid_hw_request(pidff->hid, pidff->reports[PID_DEVICE_CONTROL], HID_REQ_SET_REPORT);
730 hid_hw_wait(pidff->hid);
731 hid_dbg(pidff->hid, "Device control command 0x%02x sent",
732 pidff_device_control[field]);
733 }
734
735 /*
736 * Reset the device, stop all effects, enable actuators
737 */
738 static void pidff_reset(struct pidff_device *pidff)
739 {
740 /* We reset twice as sometimes hid_wait_io isn't waiting long enough */
741 pidff_set_device_control(pidff, PID_RESET);
742 pidff_set_device_control(pidff, PID_RESET);
743 pidff->effect_count = 0;
744
745 pidff_set_device_control(pidff, PID_STOP_ALL_EFFECTS);
746 pidff_set_device_control(pidff, PID_ENABLE_ACTUATORS);
747 }
748
749 /*
750 * Fetch pool report
751 */
752 static void pidff_fetch_pool(struct pidff_device *pidff)
753 {
754 int i;
755 struct hid_device *hid = pidff->hid;
756
757 /* Repeat if PID_SIMULTANEOUS_MAX < 2 to make sure it's correct */
758 for (i = 0; i < 20; i++) {
759 hid_hw_request(hid, pidff->reports[PID_POOL], HID_REQ_GET_REPORT);
760 hid_hw_wait(hid);
761
762 if (!pidff->pool[PID_SIMULTANEOUS_MAX].value)
763 return;
764 if (pidff->pool[PID_SIMULTANEOUS_MAX].value[0] >= 2)
765 return;
766 }
767 hid_warn(hid, "device reports %d simultaneous effects\n",
768 pidff->pool[PID_SIMULTANEOUS_MAX].value[0]);
769 }
770
771 /*
772 * Send a request for effect upload to the device
773 *
774 * Reset and enable actuators if no effects were present on the device
775 *
776 * Returns 0 if device reported success, -ENOSPC if the device reported memory
777 * is full. Upon unknown response the function will retry for 60 times, if
778 * still unsuccessful -EIO is returned.
779 */
780 static int pidff_request_effect_upload(struct pidff_device *pidff, int efnum)
781 {
782 pidff->create_new_effect_type->value[0] = efnum;
783 hid_hw_request(pidff->hid, pidff->reports[PID_CREATE_NEW_EFFECT],
784 HID_REQ_SET_REPORT);
785 hid_dbg(pidff->hid, "create_new_effect sent, type: %d\n", efnum);
786
787 pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0] = 0;
788 pidff->block_load_status->value[0] = 0;
789 hid_hw_wait(pidff->hid);
790
791 for (int i = 0; i < 60; i++) {
792 hid_dbg(pidff->hid, "pid_block_load requested\n");
793 hid_hw_request(pidff->hid, pidff->reports[PID_BLOCK_LOAD],
794 HID_REQ_GET_REPORT);
795 hid_hw_wait(pidff->hid);
796 if (pidff->block_load_status->value[0] ==
797 pidff->status_id[PID_BLOCK_LOAD_SUCCESS]) {
798 hid_dbg(pidff->hid, "device reported free memory: %d bytes\n",
799 pidff->block_load[PID_RAM_POOL_AVAILABLE].value ?
800 pidff->block_load[PID_RAM_POOL_AVAILABLE].value[0] : -1);
801 return 0;
802 }
803 if (pidff->block_load_status->value[0] ==
804 pidff->status_id[PID_BLOCK_LOAD_FULL]) {
805 hid_dbg(pidff->hid, "not enough memory free: %d bytes\n",
806 pidff->block_load[PID_RAM_POOL_AVAILABLE].value ?
807 pidff->block_load[PID_RAM_POOL_AVAILABLE].value[0] : -1);
808 return -ENOSPC;
809 }
810 if (pidff->block_load_status->value[0] ==
811 pidff->status_id[PID_BLOCK_LOAD_ERROR]) {
812 hid_dbg(pidff->hid, "device error during effect creation\n");
813 return -EREMOTEIO;
814 }
815 }
816 hid_err(pidff->hid, "pid_block_load failed 60 times\n");
817 return -EIO;
818 }
819
820 static int pidff_needs_playback(struct pidff_device *pidff, int effect_id, int n)
821 {
822 return !pidff->effect[effect_id].is_infinite ||
823 pidff->effect[effect_id].loop_count != n;
824 }
825
826 /*
827 * Play the effect with PID id n times
828 */
829 static void pidff_playback_pid(struct pidff_device *pidff, int pid_id, int n)
830 {
831 pidff->effect_operation[PID_EFFECT_BLOCK_INDEX].value[0] = pid_id;
832
833 hid_dbg(pidff->hid, "%s PID effect %d", n == 0 ? "stopping" : "playing",
834 pid_id);
835
836 if (n == 0) {
837 pidff->effect_operation_status->value[0] =
838 pidff->operation_id[PID_EFFECT_STOP];
839 } else {
840 pidff->effect_operation_status->value[0] =
841 pidff->operation_id[PID_EFFECT_START];
842 pidff->effect_operation[PID_LOOP_COUNT].value[0] =
843 pidff_clamp(n, pidff->effect_operation[PID_LOOP_COUNT].field);
844 }
845
846 hid_hw_request(pidff->hid, pidff->reports[PID_EFFECT_OPERATION],
847 HID_REQ_SET_REPORT);
848 }
849
850 /*
851 * Play the effect with effect id @effect_id for @value times
852 */
853 static int pidff_playback(struct input_dev *dev, int effect_id, int value)
854 {
855 struct pidff_device *pidff = dev->ff->private;
856
857 if (!pidff_needs_playback(pidff, effect_id, value))
858 return 0;
859
860 hid_dbg(pidff->hid, "requesting %s on FF effect %d",
861 value == 0 ? "stop" : "playback", effect_id);
862
863 pidff->effect[effect_id].loop_count = value;
864 pidff_playback_pid(pidff, pidff->effect[effect_id].pid_id, value);
865 return 0;
866 }
867
868 /*
869 * Erase effect with PID id
870 * Decrease the device effect counter
871 */
872 static void pidff_erase_pid(struct pidff_device *pidff, int pid_id)
873 {
874 pidff->block_free[PID_EFFECT_BLOCK_INDEX].value[0] = pid_id;
875 hid_hw_request(pidff->hid, pidff->reports[PID_BLOCK_FREE],
876 HID_REQ_SET_REPORT);
877 }
878
879 /*
880 * Stop and erase effect with effect_id
881 */
882 static int pidff_erase_effect(struct input_dev *dev, int effect_id)
883 {
884 struct pidff_device *pidff = dev->ff->private;
885 int pid_id = pidff->effect[effect_id].pid_id;
886
887 hid_dbg(pidff->hid, "starting to erase %d/%d\n", effect_id, pid_id);
888
889 /*
890 * Wait for the queue to clear. We do not want
891 * a full fifo to prevent the effect removal.
892 */
893 hid_hw_wait(pidff->hid);
894 pidff_playback_pid(pidff, pid_id, 0);
895 pidff_erase_pid(pidff, pid_id);
896
897 if (pidff->effect_count > 0)
898 pidff->effect_count--;
899
900 hid_dbg(pidff->hid, "current effect count: %d", pidff->effect_count);
901 return 0;
902 }
903
904 #define PIDFF_SET_REPORT_IF_NEEDED(type, effect, old) \
905 ({ if (!old || pidff_needs_set_## type(effect, old)) \
906 pidff_set_ ##type## _report(pidff, effect); })
907
908 #define PIDFF_SET_ENVELOPE_IF_NEEDED(type, effect, old) \
909 ({ if (pidff_needs_set_envelope(&effect->u.type.envelope, \
910 old ? &old->u.type.envelope : NULL)) \
911 pidff_set_envelope_report(pidff, &effect->u.type.envelope); })
912
913 /*
914 * Effect upload handler
915 */
916 static int pidff_upload_effect(struct input_dev *dev, struct ff_effect *new,
917 struct ff_effect *old)
918 {
919 struct pidff_device *pidff = dev->ff->private;
920 const int type_id = pidff_get_effect_type_id(pidff, new);
921
922 if (!type_id) {
923 hid_err(pidff->hid, "effect type not supported\n");
924 return -EINVAL;
925 }
926
927 if (!pidff->effect_count)
928 pidff_reset(pidff);
929
930 if (!old) {
931 int error = pidff_request_effect_upload(pidff, type_id);
932
933 if (error)
934 return error;
935
936 pidff->effect_count++;
937 hid_dbg(pidff->hid, "current effect count: %d", pidff->effect_count);
938 pidff->effect[new->id].loop_count = 0;
939 pidff->effect[new->id].pid_id =
940 pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0];
941 }
942
943 pidff->effect[new->id].is_infinite =
944 pidff_is_duration_infinite(new->replay.length);
945
946 pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0] =
947 pidff->effect[new->id].pid_id;
948
949 PIDFF_SET_REPORT_IF_NEEDED(effect, new, old);
950 switch (new->type) {
951 case FF_CONSTANT:
952 PIDFF_SET_REPORT_IF_NEEDED(constant, new, old);
953 PIDFF_SET_ENVELOPE_IF_NEEDED(constant, new, old);
954 break;
955
956 case FF_PERIODIC:
957 PIDFF_SET_REPORT_IF_NEEDED(periodic, new, old);
958 PIDFF_SET_ENVELOPE_IF_NEEDED(periodic, new, old);
959 break;
960
961 case FF_RAMP:
962 PIDFF_SET_REPORT_IF_NEEDED(ramp, new, old);
963 PIDFF_SET_ENVELOPE_IF_NEEDED(ramp, new, old);
964 break;
965
966 case FF_SPRING:
967 case FF_DAMPER:
968 case FF_INERTIA:
969 case FF_FRICTION:
970 PIDFF_SET_REPORT_IF_NEEDED(condition, new, old);
971 break;
972 }
973 hid_dbg(pidff->hid, "uploaded\n");
974 return 0;
975 }
976
977 /*
978 * set_gain() handler
979 */
980 static void pidff_set_gain(struct input_dev *dev, u16 gain)
981 {
982 pidff_set_gain_report(dev->ff->private, gain);
983 }
984
985 static void pidff_autocenter(struct pidff_device *pidff, u16 magnitude)
986 {
987 struct hid_field *field =
988 pidff->block_load[PID_EFFECT_BLOCK_INDEX].field;
989
990 if (!magnitude) {
991 pidff_playback_pid(pidff, field->logical_minimum, 0);
992 return;
993 }
994
995 pidff_playback_pid(pidff, field->logical_minimum, 1);
996
997 pidff->set_effect[PID_EFFECT_BLOCK_INDEX].value[0] =
998 pidff->block_load[PID_EFFECT_BLOCK_INDEX].field->logical_minimum;
999 pidff->set_effect_type->value[0] = pidff->type_id[PID_SPRING];
1000 pidff->set_effect[PID_DURATION].value[0] = 0;
1001 pidff->set_effect[PID_TRIGGER_BUTTON].value[0] = 0;
1002 pidff->set_effect[PID_TRIGGER_REPEAT_INT].value[0] = 0;
1003 pidff_set(&pidff->set_effect[PID_GAIN], magnitude);
1004 pidff->set_effect[PID_DIRECTION_ENABLE].value[0] = 1;
1005
1006 /* Omit setting delay field if it's missing */
1007 if (!(pidff->quirks & HID_PIDFF_QUIRK_MISSING_DELAY))
1008 pidff->set_effect[PID_START_DELAY].value[0] = 0;
1009
1010 hid_hw_request(pidff->hid, pidff->reports[PID_SET_EFFECT],
1011 HID_REQ_SET_REPORT);
1012 }
1013
1014 /*
1015 * pidff_set_autocenter() handler
1016 */
1017 static void pidff_set_autocenter(struct input_dev *dev, u16 magnitude)
1018 {
1019 pidff_autocenter(dev->ff->private, magnitude);
1020 }
1021
1022 /*
1023 * Find specific usage in a given hid_field
1024 */
1025 static int pidff_find_usage(struct hid_field *fld, unsigned int usage_code)
1026 {
1027 for (int i = 0; i < fld->maxusage; i++) {
1028 if (fld->usage[i].hid == usage_code)
1029 return i;
1030 }
1031 return -1;
1032 }
1033
1034 /*
1035 * Find hid_field with a specific usage. Return the usage index as well
1036 */
1037 static int pidff_find_field_with_usage(int *usage_index,
1038 struct hid_report *report,
1039 unsigned int usage_code)
1040 {
1041 for (int i = 0; i < report->maxfield; i++) {
1042 struct hid_field *fld = report->field[i];
1043
1044 if (fld->maxusage != fld->report_count) {
1045 pr_debug("maxusage and report_count do not match, skipping\n");
1046 continue;
1047 }
1048
1049 int index = pidff_find_usage(fld, usage_code);
1050
1051 if (index >= 0) {
1052 *usage_index = index;
1053 return i;
1054 }
1055 }
1056 return -1;
1057 }
1058
1059 /*
1060 * Find fields from a report and fill a pidff_usage
1061 */
1062 static int pidff_find_fields(struct pidff_usage *usage, const u8 *table,
1063 struct hid_report *report, int count, int strict,
1064 u32 *quirks)
1065 {
1066 const u8 block_offset = pidff_set_condition[PID_PARAM_BLOCK_OFFSET];
1067 const u8 delay = pidff_set_effect[PID_START_DELAY];
1068
1069 if (!report) {
1070 pr_debug("%s, null report\n", __func__);
1071 return -1;
1072 }
1073
1074 for (int i = 0; i < count; i++) {
1075 int index;
1076 int found = pidff_find_field_with_usage(&index, report,
1077 HID_UP_PID | table[i]);
1078
1079 if (found >= 0) {
1080 pr_debug("found %d at %d->%d\n", i, found, index);
1081 usage[i].field = report->field[found];
1082 usage[i].value = &report->field[found]->value[index];
1083 continue;
1084 }
1085
1086 if (table[i] == delay) {
1087 pr_debug("Delay field not found, but that's OK\n");
1088 pr_debug("Setting MISSING_DELAY quirk\n");
1089 *quirks |= HID_PIDFF_QUIRK_MISSING_DELAY;
1090
1091 } else if (table[i] == block_offset) {
1092 pr_debug("PBO field not found, but that's OK\n");
1093 pr_debug("Setting MISSING_PBO quirk\n");
1094 *quirks |= HID_PIDFF_QUIRK_MISSING_PBO;
1095
1096 } else if (strict) {
1097 pr_debug("failed to locate %d\n", i);
1098 return -1;
1099 }
1100 }
1101 return 0;
1102 }
1103
1104 /*
1105 * Return index into pidff_reports for the given usage
1106 */
1107 static int pidff_check_usage(int usage)
1108 {
1109 int i;
1110
1111 for (i = 0; i < ARRAY_SIZE(pidff_reports); i++)
1112 if (usage == (HID_UP_PID | pidff_reports[i]))
1113 return i;
1114
1115 return -1;
1116 }
1117
1118 /*
1119 * Find the reports and fill pidff->reports[]
1120 * report_type specifies either OUTPUT or FEATURE reports
1121 */
1122 static void pidff_find_reports(struct hid_device *hid, int report_type,
1123 struct pidff_device *pidff)
1124 {
1125 struct hid_report *report;
1126 int i, ret;
1127
1128 list_for_each_entry(report,
1129 &hid->report_enum[report_type].report_list, list) {
1130 if (report->maxfield < 1)
1131 continue;
1132 ret = pidff_check_usage(report->field[0]->logical);
1133 if (ret != -1) {
1134 hid_dbg(hid, "found usage 0x%02x from field->logical\n",
1135 pidff_reports[ret]);
1136 pidff->reports[ret] = report;
1137 continue;
1138 }
1139
1140 /*
1141 * Sometimes logical collections are stacked to indicate
1142 * different usages for the report and the field, in which
1143 * case we want the usage of the parent. However, Linux HID
1144 * implementation hides this fact, so we have to dig it up
1145 * ourselves
1146 */
1147 i = report->field[0]->usage[0].collection_index;
1148 if (i <= 0 ||
1149 hid->collection[i - 1].type != HID_COLLECTION_LOGICAL)
1150 continue;
1151 ret = pidff_check_usage(hid->collection[i - 1].usage);
1152 if (ret != -1 && !pidff->reports[ret]) {
1153 hid_dbg(hid,
1154 "found usage 0x%02x from collection array\n",
1155 pidff_reports[ret]);
1156 pidff->reports[ret] = report;
1157 }
1158 }
1159 }
1160
1161 /*
1162 * Test if the required reports have been found
1163 */
1164 static int pidff_reports_ok(struct pidff_device *pidff)
1165 {
1166 for (int i = 0; i < PID_REQUIRED_REPORTS; i++) {
1167 if (!pidff->reports[i]) {
1168 hid_dbg(pidff->hid, "%d missing\n", i);
1169 return 0;
1170 }
1171 }
1172
1173 return 1;
1174 }
1175
1176 /*
1177 * Find a field with a specific usage within a report
1178 */
1179 static struct hid_field *pidff_find_special_field(struct hid_report *report,
1180 int usage, int enforce_min)
1181 {
1182 if (!report) {
1183 pr_debug("%s, null report\n", __func__);
1184 return NULL;
1185 }
1186
1187 for (int i = 0; i < report->maxfield; i++) {
1188 if (report->field[i]->logical == (HID_UP_PID | usage) &&
1189 report->field[i]->report_count > 0) {
1190 if (!enforce_min ||
1191 report->field[i]->logical_minimum == 1)
1192 return report->field[i];
1193
1194 pr_err("logical_minimum is not 1 as it should be\n");
1195 return NULL;
1196 }
1197 }
1198 return NULL;
1199 }
1200
1201 /*
1202 * Fill a pidff->*_id struct table
1203 */
1204 static int pidff_find_special_keys(int *keys, struct hid_field *fld,
1205 const u8 *usagetable, int count,
1206 unsigned int usage_page)
1207 {
1208 int found = 0;
1209
1210 if (!fld)
1211 return 0;
1212
1213 for (int i = 0; i < count; i++) {
1214 keys[i] = pidff_find_usage(fld, usage_page | usagetable[i]) + 1;
1215 if (keys[i])
1216 found++;
1217 }
1218 return found;
1219 }
1220
1221 #define PIDFF_FIND_SPECIAL_KEYS(keys, field, name) \
1222 pidff_find_special_keys(pidff->keys, pidff->field, pidff_ ## name, \
1223 ARRAY_SIZE(pidff_ ## name), HID_UP_PID)
1224
1225 #define PIDFF_FIND_GENERAL_DESKTOP(keys, field, name) \
1226 pidff_find_special_keys(pidff->keys, pidff->field, pidff_ ## name, \
1227 ARRAY_SIZE(pidff_ ## name), HID_UP_GENDESK)
1228
1229 /*
1230 * Find and check the special fields
1231 */
1232 static int pidff_find_special_fields(struct pidff_device *pidff)
1233 {
1234 hid_dbg(pidff->hid, "finding special fields\n");
1235
1236 pidff->create_new_effect_type =
1237 pidff_find_special_field(pidff->reports[PID_CREATE_NEW_EFFECT],
1238 PID_EFFECT_TYPE, 1);
1239 pidff->set_effect_type =
1240 pidff_find_special_field(pidff->reports[PID_SET_EFFECT],
1241 PID_EFFECT_TYPE, 1);
1242 pidff->axes_enable =
1243 pidff_find_special_field(pidff->reports[PID_SET_EFFECT],
1244 PID_AXES_ENABLE, 0);
1245 pidff->effect_direction =
1246 pidff_find_special_field(pidff->reports[PID_SET_EFFECT],
1247 PID_DIRECTION, 0);
1248 pidff->device_control =
1249 pidff_find_special_field(pidff->reports[PID_DEVICE_CONTROL],
1250 PID_DEVICE_CONTROL_ARRAY, 1);
1251
1252 /* Detect and set permissive control quirk */
1253 if (!pidff->device_control) {
1254 pr_debug("Setting PERMISSIVE_CONTROL quirk\n");
1255 pidff->quirks |= HID_PIDFF_QUIRK_PERMISSIVE_CONTROL;
1256 pidff->device_control = pidff_find_special_field(
1257 pidff->reports[PID_DEVICE_CONTROL],
1258 PID_DEVICE_CONTROL_ARRAY, 0);
1259 }
1260
1261 pidff->block_load_status =
1262 pidff_find_special_field(pidff->reports[PID_BLOCK_LOAD],
1263 PID_BLOCK_LOAD_STATUS, 1);
1264 pidff->effect_operation_status =
1265 pidff_find_special_field(pidff->reports[PID_EFFECT_OPERATION],
1266 PID_EFFECT_OPERATION_ARRAY, 1);
1267
1268 hid_dbg(pidff->hid, "search done\n");
1269
1270 if (!pidff->create_new_effect_type || !pidff->set_effect_type) {
1271 hid_err(pidff->hid, "effect lists not found\n");
1272 return -1;
1273 }
1274
1275 if (!pidff->effect_direction) {
1276 hid_err(pidff->hid, "direction field not found\n");
1277 return -1;
1278 }
1279
1280 if (!pidff->device_control) {
1281 hid_err(pidff->hid, "device control field not found\n");
1282 return -1;
1283 }
1284
1285 if (!pidff->block_load_status) {
1286 hid_err(pidff->hid, "block load status field not found\n");
1287 return -1;
1288 }
1289
1290 if (!pidff->effect_operation_status) {
1291 hid_err(pidff->hid, "effect operation field not found\n");
1292 return -1;
1293 }
1294
1295 PIDFF_FIND_SPECIAL_KEYS(control_id, device_control, device_control);
1296
1297 if (!PIDFF_FIND_SPECIAL_KEYS(type_id, create_new_effect_type,
1298 effect_types)) {
1299 hid_err(pidff->hid, "no effect types found\n");
1300 return -1;
1301 }
1302
1303 if (PIDFF_FIND_SPECIAL_KEYS(status_id, block_load_status,
1304 block_load_status) !=
1305 ARRAY_SIZE(pidff_block_load_status)) {
1306 hid_err(pidff->hid,
1307 "block load status identifiers not found\n");
1308 return -1;
1309 }
1310
1311 if (PIDFF_FIND_SPECIAL_KEYS(operation_id, effect_operation_status,
1312 effect_operation_status) !=
1313 ARRAY_SIZE(pidff_effect_operation_status)) {
1314 hid_err(pidff->hid, "effect operation identifiers not found\n");
1315 return -1;
1316 }
1317
1318 if (!pidff->axes_enable) {
1319 hid_info(pidff->hid, "axes enable field not found!\n");
1320 return 0;
1321 }
1322
1323 hid_dbg(pidff->hid, "axes enable report count: %u\n",
1324 pidff->axes_enable->report_count);
1325
1326 uint found = PIDFF_FIND_GENERAL_DESKTOP(direction_axis_id, axes_enable,
1327 direction_axis);
1328
1329 pidff->axis_count = found;
1330 hid_dbg(pidff->hid, "found direction axes: %u", found);
1331
1332 for (int i = 0; i < ARRAY_SIZE(pidff_direction_axis); i++) {
1333 if (!pidff->direction_axis_id[i])
1334 continue;
1335
1336 hid_dbg(pidff->hid, "axis %d, usage: 0x%04x, index: %d", i + 1,
1337 pidff_direction_axis[i], pidff->direction_axis_id[i]);
1338 }
1339
1340 if (pidff->axes_enable && found != pidff->axes_enable->report_count)
1341 hid_warn(pidff->hid, "axes_enable: %u != direction axes: %u",
1342 pidff->axes_enable->report_count, found);
1343
1344 return 0;
1345 }
1346
1347 /*
1348 * Find the implemented effect types
1349 */
1350 static int pidff_find_effects(struct pidff_device *pidff,
1351 struct input_dev *dev)
1352 {
1353 int i;
1354
1355 for (i = 0; i < ARRAY_SIZE(pidff_effect_types); i++) {
1356 int pidff_type = pidff->type_id[i];
1357
1358 if (pidff->set_effect_type->usage[pidff_type].hid !=
1359 pidff->create_new_effect_type->usage[pidff_type].hid) {
1360 hid_err(pidff->hid,
1361 "effect type number %d is invalid\n", i);
1362 return -1;
1363 }
1364 }
1365
1366 if (pidff->type_id[PID_CONSTANT])
1367 set_bit(FF_CONSTANT, dev->ffbit);
1368 if (pidff->type_id[PID_RAMP])
1369 set_bit(FF_RAMP, dev->ffbit);
1370 if (pidff->type_id[PID_SQUARE]) {
1371 set_bit(FF_SQUARE, dev->ffbit);
1372 set_bit(FF_PERIODIC, dev->ffbit);
1373 }
1374 if (pidff->type_id[PID_SINE]) {
1375 set_bit(FF_SINE, dev->ffbit);
1376 set_bit(FF_PERIODIC, dev->ffbit);
1377 }
1378 if (pidff->type_id[PID_TRIANGLE]) {
1379 set_bit(FF_TRIANGLE, dev->ffbit);
1380 set_bit(FF_PERIODIC, dev->ffbit);
1381 }
1382 if (pidff->type_id[PID_SAW_UP]) {
1383 set_bit(FF_SAW_UP, dev->ffbit);
1384 set_bit(FF_PERIODIC, dev->ffbit);
1385 }
1386 if (pidff->type_id[PID_SAW_DOWN]) {
1387 set_bit(FF_SAW_DOWN, dev->ffbit);
1388 set_bit(FF_PERIODIC, dev->ffbit);
1389 }
1390 if (pidff->type_id[PID_SPRING])
1391 set_bit(FF_SPRING, dev->ffbit);
1392 if (pidff->type_id[PID_DAMPER])
1393 set_bit(FF_DAMPER, dev->ffbit);
1394 if (pidff->type_id[PID_INERTIA])
1395 set_bit(FF_INERTIA, dev->ffbit);
1396 if (pidff->type_id[PID_FRICTION])
1397 set_bit(FF_FRICTION, dev->ffbit);
1398
1399 return 0;
1400 }
1401
1402 #define PIDFF_FIND_FIELDS(name, report, strict) \
1403 pidff_find_fields(pidff->name, pidff_ ## name, \
1404 pidff->reports[report], \
1405 ARRAY_SIZE(pidff_ ## name), strict, &pidff->quirks)
1406
1407 /*
1408 * Fill and check the pidff_usages
1409 */
1410 static int pidff_init_fields(struct pidff_device *pidff, struct input_dev *dev)
1411 {
1412 if (PIDFF_FIND_FIELDS(set_effect, PID_SET_EFFECT, 1)) {
1413 hid_err(pidff->hid, "unknown set_effect report layout\n");
1414 return -ENODEV;
1415 }
1416
1417 PIDFF_FIND_FIELDS(block_load, PID_BLOCK_LOAD, 0);
1418 if (!pidff->block_load[PID_EFFECT_BLOCK_INDEX].value) {
1419 hid_err(pidff->hid, "unknown pid_block_load report layout\n");
1420 return -ENODEV;
1421 }
1422
1423 if (PIDFF_FIND_FIELDS(effect_operation, PID_EFFECT_OPERATION, 1)) {
1424 hid_err(pidff->hid, "unknown effect_operation report layout\n");
1425 return -ENODEV;
1426 }
1427
1428 if (PIDFF_FIND_FIELDS(block_free, PID_BLOCK_FREE, 1)) {
1429 hid_err(pidff->hid, "unknown pid_block_free report layout\n");
1430 return -ENODEV;
1431 }
1432
1433 if (pidff_find_special_fields(pidff) || pidff_find_effects(pidff, dev))
1434 return -ENODEV;
1435
1436 if (PIDFF_FIND_FIELDS(set_envelope, PID_SET_ENVELOPE, 1)) {
1437 if (test_and_clear_bit(FF_CONSTANT, dev->ffbit))
1438 hid_warn(pidff->hid,
1439 "has constant effect but no envelope\n");
1440 if (test_and_clear_bit(FF_RAMP, dev->ffbit))
1441 hid_warn(pidff->hid,
1442 "has ramp effect but no envelope\n");
1443
1444 if (test_and_clear_bit(FF_PERIODIC, dev->ffbit))
1445 hid_warn(pidff->hid,
1446 "has periodic effect but no envelope\n");
1447 }
1448
1449 if (PIDFF_FIND_FIELDS(set_constant, PID_SET_CONSTANT, 1) &&
1450 test_and_clear_bit(FF_CONSTANT, dev->ffbit))
1451 hid_warn(pidff->hid, "unknown constant effect layout\n");
1452
1453 if (PIDFF_FIND_FIELDS(set_ramp, PID_SET_RAMP, 1) &&
1454 test_and_clear_bit(FF_RAMP, dev->ffbit))
1455 hid_warn(pidff->hid, "unknown ramp effect layout\n");
1456
1457 if (PIDFF_FIND_FIELDS(set_condition, PID_SET_CONDITION, 1)) {
1458 bool test = false;
1459
1460 test |= test_and_clear_bit(FF_SPRING, dev->ffbit);
1461 test |= test_and_clear_bit(FF_DAMPER, dev->ffbit);
1462 test |= test_and_clear_bit(FF_FRICTION, dev->ffbit);
1463 test |= test_and_clear_bit(FF_INERTIA, dev->ffbit);
1464 if (test)
1465 hid_warn(pidff->hid, "unknown condition effect layout\n");
1466 }
1467
1468 if (PIDFF_FIND_FIELDS(set_periodic, PID_SET_PERIODIC, 1) &&
1469 test_and_clear_bit(FF_PERIODIC, dev->ffbit))
1470 hid_warn(pidff->hid, "unknown periodic effect layout\n");
1471
1472 PIDFF_FIND_FIELDS(pool, PID_POOL, 0);
1473
1474 if (!PIDFF_FIND_FIELDS(device_gain, PID_DEVICE_GAIN, 1))
1475 set_bit(FF_GAIN, dev->ffbit);
1476
1477 return 0;
1478 }
1479
1480 /*
1481 * Test if autocenter modification is using the supported method
1482 */
1483 static int pidff_check_autocenter(struct pidff_device *pidff,
1484 struct input_dev *dev)
1485 {
1486 int error;
1487
1488 /*
1489 * Let's find out if autocenter modification is supported
1490 * Specification doesn't specify anything, so we request an
1491 * effect upload and cancel it immediately. If the approved
1492 * effect id was one above the minimum, then we assume the first
1493 * effect id is a built-in spring type effect used for autocenter
1494 */
1495
1496 error = pidff_request_effect_upload(pidff, 1);
1497 if (error) {
1498 hid_err(pidff->hid, "upload request failed\n");
1499 return error;
1500 }
1501
1502 if (pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0] ==
1503 pidff->block_load[PID_EFFECT_BLOCK_INDEX].field->logical_minimum + 1) {
1504 pidff_autocenter(pidff, U16_MAX);
1505 set_bit(FF_AUTOCENTER, dev->ffbit);
1506 } else {
1507 hid_notice(pidff->hid,
1508 "device has unknown autocenter control method\n");
1509 }
1510 pidff_erase_pid(pidff,
1511 pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0]);
1512
1513 return 0;
1514 }
1515
1516 /*
1517 * Check if the device is PID and initialize it
1518 * Set initial quirks
1519 */
1520 int hid_pidff_init_with_quirks(struct hid_device *hid, u32 initial_quirks)
1521 {
1522 struct pidff_device *pidff;
1523 struct hid_input *hidinput =
1524 list_entry(hid->inputs.next, struct hid_input, list);
1525 struct input_dev *dev = hidinput->input;
1526 struct ff_device *ff;
1527 int max_effects;
1528 int error;
1529
1530 hid_dbg(hid, "starting pid init\n");
1531
1532 if (list_empty(&hid->report_enum[HID_OUTPUT_REPORT].report_list)) {
1533 hid_dbg(hid, "not a PID device, no output report\n");
1534 return -ENODEV;
1535 }
1536
1537 pidff = kzalloc_obj(*pidff);
1538 if (!pidff)
1539 return -ENOMEM;
1540
1541 pidff->hid = hid;
1542 pidff->quirks = initial_quirks;
1543 pidff->effect_count = 0;
1544
1545 hid_device_io_start(hid);
1546
1547 pidff_find_reports(hid, HID_OUTPUT_REPORT, pidff);
1548 pidff_find_reports(hid, HID_FEATURE_REPORT, pidff);
1549
1550 if (!pidff_reports_ok(pidff)) {
1551 hid_dbg(hid, "reports not ok, aborting\n");
1552 error = -ENODEV;
1553 goto fail;
1554 }
1555
1556 error = pidff_init_fields(pidff, dev);
1557 if (error)
1558 goto fail;
1559
1560 /* pool report is sometimes messed up, refetch it */
1561 pidff_fetch_pool(pidff);
1562 pidff_set_gain_report(pidff, U16_MAX);
1563 error = pidff_check_autocenter(pidff, dev);
1564 if (error)
1565 goto fail;
1566
1567 max_effects =
1568 pidff->block_load[PID_EFFECT_BLOCK_INDEX].field->logical_maximum -
1569 pidff->block_load[PID_EFFECT_BLOCK_INDEX].field->logical_minimum +
1570 1;
1571 hid_dbg(hid, "max effects is %d\n", max_effects);
1572
1573 if (max_effects > PID_EFFECTS_MAX)
1574 max_effects = PID_EFFECTS_MAX;
1575
1576 if (pidff->pool[PID_SIMULTANEOUS_MAX].value)
1577 hid_dbg(hid, "max simultaneous effects is %d\n",
1578 pidff->pool[PID_SIMULTANEOUS_MAX].value[0]);
1579
1580 if (pidff->pool[PID_RAM_POOL_SIZE].value)
1581 hid_dbg(hid, "device memory size is %d bytes\n",
1582 pidff->pool[PID_RAM_POOL_SIZE].value[0]);
1583
1584 if (pidff->pool[PID_DEVICE_MANAGED_POOL].value &&
1585 pidff->pool[PID_DEVICE_MANAGED_POOL].value[0] == 0) {
1586 error = -EPERM;
1587 hid_notice(hid,
1588 "device does not support device managed pool\n");
1589 goto fail;
1590 }
1591
1592 error = input_ff_create(dev, max_effects);
1593 if (error)
1594 goto fail;
1595
1596 ff = dev->ff;
1597 ff->private = pidff;
1598 ff->upload = pidff_upload_effect;
1599 ff->erase = pidff_erase_effect;
1600 ff->set_gain = pidff_set_gain;
1601 ff->set_autocenter = pidff_set_autocenter;
1602 ff->playback = pidff_playback;
1603
1604 hid_info(dev, "Force feedback for USB HID PID devices by Anssi Hannula\n");
1605 hid_dbg(dev, "Active quirks mask: 0x%08x\n", pidff->quirks);
1606
1607 hid_device_io_stop(hid);
1608
1609 return 0;
1610
1611 fail:
1612 hid_device_io_stop(hid);
1613
1614 kfree(pidff);
1615 return error;
1616 }
1617 EXPORT_SYMBOL_GPL(hid_pidff_init_with_quirks);
1618
1619 /*
1620 * Check if the device is PID and initialize it
1621 * Wrapper made to keep the compatibility with old
1622 * init function
1623 */
1624 int hid_pidff_init(struct hid_device *hid)
1625 {
1626 return hid_pidff_init_with_quirks(hid, 0);
1627 }