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