gpio: rcar: Fix runtime PM imbalance on error
[linux/fpc-iii.git] / drivers / hid / usbhid / hid-pidff.c
blobfddac7c72f6454cc8c488d6cd77b6f71bcb6db96
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Force feedback driver for USB HID PID compliant devices
5 * Copyright (c) 2005, 2006 Anssi Hannula <anssi.hannula@gmail.com>
6 */
8 /*
9 */
11 /* #define DEBUG */
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/input.h>
16 #include <linux/slab.h>
17 #include <linux/usb.h>
19 #include <linux/hid.h>
21 #include "usbhid.h"
23 #define PID_EFFECTS_MAX 64
25 /* 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
36 #define PID_REQUIRED_REPORTS 7
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
48 /* device_control is really 0x95, but 0x96 specified as it is the usage of
49 the only field in that report */
51 /* Value usage tables used to put fields and values into arrays */
53 #define PID_EFFECT_BLOCK_INDEX 0
55 #define PID_DURATION 1
56 #define PID_GAIN 2
57 #define PID_TRIGGER_BUTTON 3
58 #define PID_TRIGGER_REPEAT_INT 4
59 #define PID_DIRECTION_ENABLE 5
60 #define PID_START_DELAY 6
61 static const u8 pidff_set_effect[] = {
62 0x22, 0x50, 0x52, 0x53, 0x54, 0x56, 0xa7
65 #define PID_ATTACK_LEVEL 1
66 #define PID_ATTACK_TIME 2
67 #define PID_FADE_LEVEL 3
68 #define PID_FADE_TIME 4
69 static const u8 pidff_set_envelope[] = { 0x22, 0x5b, 0x5c, 0x5d, 0x5e };
71 #define PID_PARAM_BLOCK_OFFSET 1
72 #define PID_CP_OFFSET 2
73 #define PID_POS_COEFFICIENT 3
74 #define PID_NEG_COEFFICIENT 4
75 #define PID_POS_SATURATION 5
76 #define PID_NEG_SATURATION 6
77 #define PID_DEAD_BAND 7
78 static const u8 pidff_set_condition[] = {
79 0x22, 0x23, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65
82 #define PID_MAGNITUDE 1
83 #define PID_OFFSET 2
84 #define PID_PHASE 3
85 #define PID_PERIOD 4
86 static const u8 pidff_set_periodic[] = { 0x22, 0x70, 0x6f, 0x71, 0x72 };
87 static const u8 pidff_set_constant[] = { 0x22, 0x70 };
89 #define PID_RAMP_START 1
90 #define PID_RAMP_END 2
91 static const u8 pidff_set_ramp[] = { 0x22, 0x75, 0x76 };
93 #define PID_RAM_POOL_AVAILABLE 1
94 static const u8 pidff_block_load[] = { 0x22, 0xac };
96 #define PID_LOOP_COUNT 1
97 static const u8 pidff_effect_operation[] = { 0x22, 0x7c };
99 static const u8 pidff_block_free[] = { 0x22 };
101 #define PID_DEVICE_GAIN_FIELD 0
102 static const u8 pidff_device_gain[] = { 0x7e };
104 #define PID_RAM_POOL_SIZE 0
105 #define PID_SIMULTANEOUS_MAX 1
106 #define PID_DEVICE_MANAGED_POOL 2
107 static const u8 pidff_pool[] = { 0x80, 0x83, 0xa9 };
109 /* Special field key tables used to put special field keys into arrays */
111 #define PID_ENABLE_ACTUATORS 0
112 #define PID_RESET 1
113 static const u8 pidff_device_control[] = { 0x97, 0x9a };
115 #define PID_CONSTANT 0
116 #define PID_RAMP 1
117 #define PID_SQUARE 2
118 #define PID_SINE 3
119 #define PID_TRIANGLE 4
120 #define PID_SAW_UP 5
121 #define PID_SAW_DOWN 6
122 #define PID_SPRING 7
123 #define PID_DAMPER 8
124 #define PID_INERTIA 9
125 #define PID_FRICTION 10
126 static const u8 pidff_effect_types[] = {
127 0x26, 0x27, 0x30, 0x31, 0x32, 0x33, 0x34,
128 0x40, 0x41, 0x42, 0x43
131 #define PID_BLOCK_LOAD_SUCCESS 0
132 #define PID_BLOCK_LOAD_FULL 1
133 static const u8 pidff_block_load_status[] = { 0x8c, 0x8d };
135 #define PID_EFFECT_START 0
136 #define PID_EFFECT_STOP 1
137 static const u8 pidff_effect_operation_status[] = { 0x79, 0x7b };
139 struct pidff_usage {
140 struct hid_field *field;
141 s32 *value;
144 struct pidff_device {
145 struct hid_device *hid;
147 struct hid_report *reports[sizeof(pidff_reports)];
149 struct pidff_usage set_effect[sizeof(pidff_set_effect)];
150 struct pidff_usage set_envelope[sizeof(pidff_set_envelope)];
151 struct pidff_usage set_condition[sizeof(pidff_set_condition)];
152 struct pidff_usage set_periodic[sizeof(pidff_set_periodic)];
153 struct pidff_usage set_constant[sizeof(pidff_set_constant)];
154 struct pidff_usage set_ramp[sizeof(pidff_set_ramp)];
156 struct pidff_usage device_gain[sizeof(pidff_device_gain)];
157 struct pidff_usage block_load[sizeof(pidff_block_load)];
158 struct pidff_usage pool[sizeof(pidff_pool)];
159 struct pidff_usage effect_operation[sizeof(pidff_effect_operation)];
160 struct pidff_usage block_free[sizeof(pidff_block_free)];
162 /* Special field is a field that is not composed of
163 usage<->value pairs that pidff_usage values are */
165 /* Special field in create_new_effect */
166 struct hid_field *create_new_effect_type;
168 /* Special fields in set_effect */
169 struct hid_field *set_effect_type;
170 struct hid_field *effect_direction;
172 /* Special field in device_control */
173 struct hid_field *device_control;
175 /* Special field in block_load */
176 struct hid_field *block_load_status;
178 /* Special field in effect_operation */
179 struct hid_field *effect_operation_status;
181 int control_id[sizeof(pidff_device_control)];
182 int type_id[sizeof(pidff_effect_types)];
183 int status_id[sizeof(pidff_block_load_status)];
184 int operation_id[sizeof(pidff_effect_operation_status)];
186 int pid_id[PID_EFFECTS_MAX];
190 * Scale an unsigned value with range 0..max for the given field
192 static int pidff_rescale(int i, int max, struct hid_field *field)
194 return i * (field->logical_maximum - field->logical_minimum) / max +
195 field->logical_minimum;
199 * Scale a signed value in range -0x8000..0x7fff for the given field
201 static int pidff_rescale_signed(int i, struct hid_field *field)
203 return i == 0 ? 0 : i >
204 0 ? i * field->logical_maximum / 0x7fff : i *
205 field->logical_minimum / -0x8000;
208 static void pidff_set(struct pidff_usage *usage, u16 value)
210 usage->value[0] = pidff_rescale(value, 0xffff, usage->field);
211 pr_debug("calculated from %d to %d\n", value, usage->value[0]);
214 static void pidff_set_signed(struct pidff_usage *usage, s16 value)
216 if (usage->field->logical_minimum < 0)
217 usage->value[0] = pidff_rescale_signed(value, usage->field);
218 else {
219 if (value < 0)
220 usage->value[0] =
221 pidff_rescale(-value, 0x8000, usage->field);
222 else
223 usage->value[0] =
224 pidff_rescale(value, 0x7fff, usage->field);
226 pr_debug("calculated from %d to %d\n", value, usage->value[0]);
230 * Send envelope report to the device
232 static void pidff_set_envelope_report(struct pidff_device *pidff,
233 struct ff_envelope *envelope)
235 pidff->set_envelope[PID_EFFECT_BLOCK_INDEX].value[0] =
236 pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0];
238 pidff->set_envelope[PID_ATTACK_LEVEL].value[0] =
239 pidff_rescale(envelope->attack_level >
240 0x7fff ? 0x7fff : envelope->attack_level, 0x7fff,
241 pidff->set_envelope[PID_ATTACK_LEVEL].field);
242 pidff->set_envelope[PID_FADE_LEVEL].value[0] =
243 pidff_rescale(envelope->fade_level >
244 0x7fff ? 0x7fff : envelope->fade_level, 0x7fff,
245 pidff->set_envelope[PID_FADE_LEVEL].field);
247 pidff->set_envelope[PID_ATTACK_TIME].value[0] = envelope->attack_length;
248 pidff->set_envelope[PID_FADE_TIME].value[0] = envelope->fade_length;
250 hid_dbg(pidff->hid, "attack %u => %d\n",
251 envelope->attack_level,
252 pidff->set_envelope[PID_ATTACK_LEVEL].value[0]);
254 hid_hw_request(pidff->hid, pidff->reports[PID_SET_ENVELOPE],
255 HID_REQ_SET_REPORT);
259 * Test if the new envelope differs from old one
261 static int pidff_needs_set_envelope(struct ff_envelope *envelope,
262 struct ff_envelope *old)
264 return envelope->attack_level != old->attack_level ||
265 envelope->fade_level != old->fade_level ||
266 envelope->attack_length != old->attack_length ||
267 envelope->fade_length != old->fade_length;
271 * Send constant force report to the device
273 static void pidff_set_constant_force_report(struct pidff_device *pidff,
274 struct ff_effect *effect)
276 pidff->set_constant[PID_EFFECT_BLOCK_INDEX].value[0] =
277 pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0];
278 pidff_set_signed(&pidff->set_constant[PID_MAGNITUDE],
279 effect->u.constant.level);
281 hid_hw_request(pidff->hid, pidff->reports[PID_SET_CONSTANT],
282 HID_REQ_SET_REPORT);
286 * Test if the constant parameters have changed between effects
288 static int pidff_needs_set_constant(struct ff_effect *effect,
289 struct ff_effect *old)
291 return effect->u.constant.level != old->u.constant.level;
295 * Send set effect report to the device
297 static void pidff_set_effect_report(struct pidff_device *pidff,
298 struct ff_effect *effect)
300 pidff->set_effect[PID_EFFECT_BLOCK_INDEX].value[0] =
301 pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0];
302 pidff->set_effect_type->value[0] =
303 pidff->create_new_effect_type->value[0];
304 pidff->set_effect[PID_DURATION].value[0] = effect->replay.length;
305 pidff->set_effect[PID_TRIGGER_BUTTON].value[0] = effect->trigger.button;
306 pidff->set_effect[PID_TRIGGER_REPEAT_INT].value[0] =
307 effect->trigger.interval;
308 pidff->set_effect[PID_GAIN].value[0] =
309 pidff->set_effect[PID_GAIN].field->logical_maximum;
310 pidff->set_effect[PID_DIRECTION_ENABLE].value[0] = 1;
311 pidff->effect_direction->value[0] =
312 pidff_rescale(effect->direction, 0xffff,
313 pidff->effect_direction);
314 pidff->set_effect[PID_START_DELAY].value[0] = effect->replay.delay;
316 hid_hw_request(pidff->hid, pidff->reports[PID_SET_EFFECT],
317 HID_REQ_SET_REPORT);
321 * Test if the values used in set_effect have changed
323 static int pidff_needs_set_effect(struct ff_effect *effect,
324 struct ff_effect *old)
326 return effect->replay.length != old->replay.length ||
327 effect->trigger.interval != old->trigger.interval ||
328 effect->trigger.button != old->trigger.button ||
329 effect->direction != old->direction ||
330 effect->replay.delay != old->replay.delay;
334 * Send periodic effect report to the device
336 static void pidff_set_periodic_report(struct pidff_device *pidff,
337 struct ff_effect *effect)
339 pidff->set_periodic[PID_EFFECT_BLOCK_INDEX].value[0] =
340 pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0];
341 pidff_set_signed(&pidff->set_periodic[PID_MAGNITUDE],
342 effect->u.periodic.magnitude);
343 pidff_set_signed(&pidff->set_periodic[PID_OFFSET],
344 effect->u.periodic.offset);
345 pidff_set(&pidff->set_periodic[PID_PHASE], effect->u.periodic.phase);
346 pidff->set_periodic[PID_PERIOD].value[0] = effect->u.periodic.period;
348 hid_hw_request(pidff->hid, pidff->reports[PID_SET_PERIODIC],
349 HID_REQ_SET_REPORT);
354 * Test if periodic effect parameters have changed
356 static int pidff_needs_set_periodic(struct ff_effect *effect,
357 struct ff_effect *old)
359 return effect->u.periodic.magnitude != old->u.periodic.magnitude ||
360 effect->u.periodic.offset != old->u.periodic.offset ||
361 effect->u.periodic.phase != old->u.periodic.phase ||
362 effect->u.periodic.period != old->u.periodic.period;
366 * Send condition effect reports to the device
368 static void pidff_set_condition_report(struct pidff_device *pidff,
369 struct ff_effect *effect)
371 int i;
373 pidff->set_condition[PID_EFFECT_BLOCK_INDEX].value[0] =
374 pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0];
376 for (i = 0; i < 2; i++) {
377 pidff->set_condition[PID_PARAM_BLOCK_OFFSET].value[0] = i;
378 pidff_set_signed(&pidff->set_condition[PID_CP_OFFSET],
379 effect->u.condition[i].center);
380 pidff_set_signed(&pidff->set_condition[PID_POS_COEFFICIENT],
381 effect->u.condition[i].right_coeff);
382 pidff_set_signed(&pidff->set_condition[PID_NEG_COEFFICIENT],
383 effect->u.condition[i].left_coeff);
384 pidff_set(&pidff->set_condition[PID_POS_SATURATION],
385 effect->u.condition[i].right_saturation);
386 pidff_set(&pidff->set_condition[PID_NEG_SATURATION],
387 effect->u.condition[i].left_saturation);
388 pidff_set(&pidff->set_condition[PID_DEAD_BAND],
389 effect->u.condition[i].deadband);
390 hid_hw_request(pidff->hid, pidff->reports[PID_SET_CONDITION],
391 HID_REQ_SET_REPORT);
396 * Test if condition effect parameters have changed
398 static int pidff_needs_set_condition(struct ff_effect *effect,
399 struct ff_effect *old)
401 int i;
402 int ret = 0;
404 for (i = 0; i < 2; i++) {
405 struct ff_condition_effect *cond = &effect->u.condition[i];
406 struct ff_condition_effect *old_cond = &old->u.condition[i];
408 ret |= cond->center != old_cond->center ||
409 cond->right_coeff != old_cond->right_coeff ||
410 cond->left_coeff != old_cond->left_coeff ||
411 cond->right_saturation != old_cond->right_saturation ||
412 cond->left_saturation != old_cond->left_saturation ||
413 cond->deadband != old_cond->deadband;
416 return ret;
420 * Send ramp force report to the device
422 static void pidff_set_ramp_force_report(struct pidff_device *pidff,
423 struct ff_effect *effect)
425 pidff->set_ramp[PID_EFFECT_BLOCK_INDEX].value[0] =
426 pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0];
427 pidff_set_signed(&pidff->set_ramp[PID_RAMP_START],
428 effect->u.ramp.start_level);
429 pidff_set_signed(&pidff->set_ramp[PID_RAMP_END],
430 effect->u.ramp.end_level);
431 hid_hw_request(pidff->hid, pidff->reports[PID_SET_RAMP],
432 HID_REQ_SET_REPORT);
436 * Test if ramp force parameters have changed
438 static int pidff_needs_set_ramp(struct ff_effect *effect, struct ff_effect *old)
440 return effect->u.ramp.start_level != old->u.ramp.start_level ||
441 effect->u.ramp.end_level != old->u.ramp.end_level;
445 * Send a request for effect upload to the device
447 * Returns 0 if device reported success, -ENOSPC if the device reported memory
448 * is full. Upon unknown response the function will retry for 60 times, if
449 * still unsuccessful -EIO is returned.
451 static int pidff_request_effect_upload(struct pidff_device *pidff, int efnum)
453 int j;
455 pidff->create_new_effect_type->value[0] = efnum;
456 hid_hw_request(pidff->hid, pidff->reports[PID_CREATE_NEW_EFFECT],
457 HID_REQ_SET_REPORT);
458 hid_dbg(pidff->hid, "create_new_effect sent, type: %d\n", efnum);
460 pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0] = 0;
461 pidff->block_load_status->value[0] = 0;
462 hid_hw_wait(pidff->hid);
464 for (j = 0; j < 60; j++) {
465 hid_dbg(pidff->hid, "pid_block_load requested\n");
466 hid_hw_request(pidff->hid, pidff->reports[PID_BLOCK_LOAD],
467 HID_REQ_GET_REPORT);
468 hid_hw_wait(pidff->hid);
469 if (pidff->block_load_status->value[0] ==
470 pidff->status_id[PID_BLOCK_LOAD_SUCCESS]) {
471 hid_dbg(pidff->hid, "device reported free memory: %d bytes\n",
472 pidff->block_load[PID_RAM_POOL_AVAILABLE].value ?
473 pidff->block_load[PID_RAM_POOL_AVAILABLE].value[0] : -1);
474 return 0;
476 if (pidff->block_load_status->value[0] ==
477 pidff->status_id[PID_BLOCK_LOAD_FULL]) {
478 hid_dbg(pidff->hid, "not enough memory free: %d bytes\n",
479 pidff->block_load[PID_RAM_POOL_AVAILABLE].value ?
480 pidff->block_load[PID_RAM_POOL_AVAILABLE].value[0] : -1);
481 return -ENOSPC;
484 hid_err(pidff->hid, "pid_block_load failed 60 times\n");
485 return -EIO;
489 * Play the effect with PID id n times
491 static void pidff_playback_pid(struct pidff_device *pidff, int pid_id, int n)
493 pidff->effect_operation[PID_EFFECT_BLOCK_INDEX].value[0] = pid_id;
495 if (n == 0) {
496 pidff->effect_operation_status->value[0] =
497 pidff->operation_id[PID_EFFECT_STOP];
498 } else {
499 pidff->effect_operation_status->value[0] =
500 pidff->operation_id[PID_EFFECT_START];
501 pidff->effect_operation[PID_LOOP_COUNT].value[0] = n;
504 hid_hw_request(pidff->hid, pidff->reports[PID_EFFECT_OPERATION],
505 HID_REQ_SET_REPORT);
509 * Play the effect with effect id @effect_id for @value times
511 static int pidff_playback(struct input_dev *dev, int effect_id, int value)
513 struct pidff_device *pidff = dev->ff->private;
515 pidff_playback_pid(pidff, pidff->pid_id[effect_id], value);
517 return 0;
521 * Erase effect with PID id
523 static void pidff_erase_pid(struct pidff_device *pidff, int pid_id)
525 pidff->block_free[PID_EFFECT_BLOCK_INDEX].value[0] = pid_id;
526 hid_hw_request(pidff->hid, pidff->reports[PID_BLOCK_FREE],
527 HID_REQ_SET_REPORT);
531 * Stop and erase effect with effect_id
533 static int pidff_erase_effect(struct input_dev *dev, int effect_id)
535 struct pidff_device *pidff = dev->ff->private;
536 int pid_id = pidff->pid_id[effect_id];
538 hid_dbg(pidff->hid, "starting to erase %d/%d\n",
539 effect_id, pidff->pid_id[effect_id]);
540 /* Wait for the queue to clear. We do not want a full fifo to
541 prevent the effect removal. */
542 hid_hw_wait(pidff->hid);
543 pidff_playback_pid(pidff, pid_id, 0);
544 pidff_erase_pid(pidff, pid_id);
546 return 0;
550 * Effect upload handler
552 static int pidff_upload_effect(struct input_dev *dev, struct ff_effect *effect,
553 struct ff_effect *old)
555 struct pidff_device *pidff = dev->ff->private;
556 int type_id;
557 int error;
559 pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0] = 0;
560 if (old) {
561 pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0] =
562 pidff->pid_id[effect->id];
565 switch (effect->type) {
566 case FF_CONSTANT:
567 if (!old) {
568 error = pidff_request_effect_upload(pidff,
569 pidff->type_id[PID_CONSTANT]);
570 if (error)
571 return error;
573 if (!old || pidff_needs_set_effect(effect, old))
574 pidff_set_effect_report(pidff, effect);
575 if (!old || pidff_needs_set_constant(effect, old))
576 pidff_set_constant_force_report(pidff, effect);
577 if (!old ||
578 pidff_needs_set_envelope(&effect->u.constant.envelope,
579 &old->u.constant.envelope))
580 pidff_set_envelope_report(pidff,
581 &effect->u.constant.envelope);
582 break;
584 case FF_PERIODIC:
585 if (!old) {
586 switch (effect->u.periodic.waveform) {
587 case FF_SQUARE:
588 type_id = PID_SQUARE;
589 break;
590 case FF_TRIANGLE:
591 type_id = PID_TRIANGLE;
592 break;
593 case FF_SINE:
594 type_id = PID_SINE;
595 break;
596 case FF_SAW_UP:
597 type_id = PID_SAW_UP;
598 break;
599 case FF_SAW_DOWN:
600 type_id = PID_SAW_DOWN;
601 break;
602 default:
603 hid_err(pidff->hid, "invalid waveform\n");
604 return -EINVAL;
607 error = pidff_request_effect_upload(pidff,
608 pidff->type_id[type_id]);
609 if (error)
610 return error;
612 if (!old || pidff_needs_set_effect(effect, old))
613 pidff_set_effect_report(pidff, effect);
614 if (!old || pidff_needs_set_periodic(effect, old))
615 pidff_set_periodic_report(pidff, effect);
616 if (!old ||
617 pidff_needs_set_envelope(&effect->u.periodic.envelope,
618 &old->u.periodic.envelope))
619 pidff_set_envelope_report(pidff,
620 &effect->u.periodic.envelope);
621 break;
623 case FF_RAMP:
624 if (!old) {
625 error = pidff_request_effect_upload(pidff,
626 pidff->type_id[PID_RAMP]);
627 if (error)
628 return error;
630 if (!old || pidff_needs_set_effect(effect, old))
631 pidff_set_effect_report(pidff, effect);
632 if (!old || pidff_needs_set_ramp(effect, old))
633 pidff_set_ramp_force_report(pidff, effect);
634 if (!old ||
635 pidff_needs_set_envelope(&effect->u.ramp.envelope,
636 &old->u.ramp.envelope))
637 pidff_set_envelope_report(pidff,
638 &effect->u.ramp.envelope);
639 break;
641 case FF_SPRING:
642 if (!old) {
643 error = pidff_request_effect_upload(pidff,
644 pidff->type_id[PID_SPRING]);
645 if (error)
646 return error;
648 if (!old || pidff_needs_set_effect(effect, old))
649 pidff_set_effect_report(pidff, effect);
650 if (!old || pidff_needs_set_condition(effect, old))
651 pidff_set_condition_report(pidff, effect);
652 break;
654 case FF_FRICTION:
655 if (!old) {
656 error = pidff_request_effect_upload(pidff,
657 pidff->type_id[PID_FRICTION]);
658 if (error)
659 return error;
661 if (!old || pidff_needs_set_effect(effect, old))
662 pidff_set_effect_report(pidff, effect);
663 if (!old || pidff_needs_set_condition(effect, old))
664 pidff_set_condition_report(pidff, effect);
665 break;
667 case FF_DAMPER:
668 if (!old) {
669 error = pidff_request_effect_upload(pidff,
670 pidff->type_id[PID_DAMPER]);
671 if (error)
672 return error;
674 if (!old || pidff_needs_set_effect(effect, old))
675 pidff_set_effect_report(pidff, effect);
676 if (!old || pidff_needs_set_condition(effect, old))
677 pidff_set_condition_report(pidff, effect);
678 break;
680 case FF_INERTIA:
681 if (!old) {
682 error = pidff_request_effect_upload(pidff,
683 pidff->type_id[PID_INERTIA]);
684 if (error)
685 return error;
687 if (!old || pidff_needs_set_effect(effect, old))
688 pidff_set_effect_report(pidff, effect);
689 if (!old || pidff_needs_set_condition(effect, old))
690 pidff_set_condition_report(pidff, effect);
691 break;
693 default:
694 hid_err(pidff->hid, "invalid type\n");
695 return -EINVAL;
698 if (!old)
699 pidff->pid_id[effect->id] =
700 pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0];
702 hid_dbg(pidff->hid, "uploaded\n");
704 return 0;
708 * set_gain() handler
710 static void pidff_set_gain(struct input_dev *dev, u16 gain)
712 struct pidff_device *pidff = dev->ff->private;
714 pidff_set(&pidff->device_gain[PID_DEVICE_GAIN_FIELD], gain);
715 hid_hw_request(pidff->hid, pidff->reports[PID_DEVICE_GAIN],
716 HID_REQ_SET_REPORT);
719 static void pidff_autocenter(struct pidff_device *pidff, u16 magnitude)
721 struct hid_field *field =
722 pidff->block_load[PID_EFFECT_BLOCK_INDEX].field;
724 if (!magnitude) {
725 pidff_playback_pid(pidff, field->logical_minimum, 0);
726 return;
729 pidff_playback_pid(pidff, field->logical_minimum, 1);
731 pidff->set_effect[PID_EFFECT_BLOCK_INDEX].value[0] =
732 pidff->block_load[PID_EFFECT_BLOCK_INDEX].field->logical_minimum;
733 pidff->set_effect_type->value[0] = pidff->type_id[PID_SPRING];
734 pidff->set_effect[PID_DURATION].value[0] = 0;
735 pidff->set_effect[PID_TRIGGER_BUTTON].value[0] = 0;
736 pidff->set_effect[PID_TRIGGER_REPEAT_INT].value[0] = 0;
737 pidff_set(&pidff->set_effect[PID_GAIN], magnitude);
738 pidff->set_effect[PID_DIRECTION_ENABLE].value[0] = 1;
739 pidff->set_effect[PID_START_DELAY].value[0] = 0;
741 hid_hw_request(pidff->hid, pidff->reports[PID_SET_EFFECT],
742 HID_REQ_SET_REPORT);
746 * pidff_set_autocenter() handler
748 static void pidff_set_autocenter(struct input_dev *dev, u16 magnitude)
750 struct pidff_device *pidff = dev->ff->private;
752 pidff_autocenter(pidff, magnitude);
756 * Find fields from a report and fill a pidff_usage
758 static int pidff_find_fields(struct pidff_usage *usage, const u8 *table,
759 struct hid_report *report, int count, int strict)
761 int i, j, k, found;
763 for (k = 0; k < count; k++) {
764 found = 0;
765 for (i = 0; i < report->maxfield; i++) {
766 if (report->field[i]->maxusage !=
767 report->field[i]->report_count) {
768 pr_debug("maxusage and report_count do not match, skipping\n");
769 continue;
771 for (j = 0; j < report->field[i]->maxusage; j++) {
772 if (report->field[i]->usage[j].hid ==
773 (HID_UP_PID | table[k])) {
774 pr_debug("found %d at %d->%d\n",
775 k, i, j);
776 usage[k].field = report->field[i];
777 usage[k].value =
778 &report->field[i]->value[j];
779 found = 1;
780 break;
783 if (found)
784 break;
786 if (!found && strict) {
787 pr_debug("failed to locate %d\n", k);
788 return -1;
791 return 0;
795 * Return index into pidff_reports for the given usage
797 static int pidff_check_usage(int usage)
799 int i;
801 for (i = 0; i < sizeof(pidff_reports); i++)
802 if (usage == (HID_UP_PID | pidff_reports[i]))
803 return i;
805 return -1;
809 * Find the reports and fill pidff->reports[]
810 * report_type specifies either OUTPUT or FEATURE reports
812 static void pidff_find_reports(struct hid_device *hid, int report_type,
813 struct pidff_device *pidff)
815 struct hid_report *report;
816 int i, ret;
818 list_for_each_entry(report,
819 &hid->report_enum[report_type].report_list, list) {
820 if (report->maxfield < 1)
821 continue;
822 ret = pidff_check_usage(report->field[0]->logical);
823 if (ret != -1) {
824 hid_dbg(hid, "found usage 0x%02x from field->logical\n",
825 pidff_reports[ret]);
826 pidff->reports[ret] = report;
827 continue;
831 * Sometimes logical collections are stacked to indicate
832 * different usages for the report and the field, in which
833 * case we want the usage of the parent. However, Linux HID
834 * implementation hides this fact, so we have to dig it up
835 * ourselves
837 i = report->field[0]->usage[0].collection_index;
838 if (i <= 0 ||
839 hid->collection[i - 1].type != HID_COLLECTION_LOGICAL)
840 continue;
841 ret = pidff_check_usage(hid->collection[i - 1].usage);
842 if (ret != -1 && !pidff->reports[ret]) {
843 hid_dbg(hid,
844 "found usage 0x%02x from collection array\n",
845 pidff_reports[ret]);
846 pidff->reports[ret] = report;
852 * Test if the required reports have been found
854 static int pidff_reports_ok(struct pidff_device *pidff)
856 int i;
858 for (i = 0; i <= PID_REQUIRED_REPORTS; i++) {
859 if (!pidff->reports[i]) {
860 hid_dbg(pidff->hid, "%d missing\n", i);
861 return 0;
865 return 1;
869 * Find a field with a specific usage within a report
871 static struct hid_field *pidff_find_special_field(struct hid_report *report,
872 int usage, int enforce_min)
874 int i;
876 for (i = 0; i < report->maxfield; i++) {
877 if (report->field[i]->logical == (HID_UP_PID | usage) &&
878 report->field[i]->report_count > 0) {
879 if (!enforce_min ||
880 report->field[i]->logical_minimum == 1)
881 return report->field[i];
882 else {
883 pr_err("logical_minimum is not 1 as it should be\n");
884 return NULL;
888 return NULL;
892 * Fill a pidff->*_id struct table
894 static int pidff_find_special_keys(int *keys, struct hid_field *fld,
895 const u8 *usagetable, int count)
898 int i, j;
899 int found = 0;
901 for (i = 0; i < count; i++) {
902 for (j = 0; j < fld->maxusage; j++) {
903 if (fld->usage[j].hid == (HID_UP_PID | usagetable[i])) {
904 keys[i] = j + 1;
905 found++;
906 break;
910 return found;
913 #define PIDFF_FIND_SPECIAL_KEYS(keys, field, name) \
914 pidff_find_special_keys(pidff->keys, pidff->field, pidff_ ## name, \
915 sizeof(pidff_ ## name))
918 * Find and check the special fields
920 static int pidff_find_special_fields(struct pidff_device *pidff)
922 hid_dbg(pidff->hid, "finding special fields\n");
924 pidff->create_new_effect_type =
925 pidff_find_special_field(pidff->reports[PID_CREATE_NEW_EFFECT],
926 0x25, 1);
927 pidff->set_effect_type =
928 pidff_find_special_field(pidff->reports[PID_SET_EFFECT],
929 0x25, 1);
930 pidff->effect_direction =
931 pidff_find_special_field(pidff->reports[PID_SET_EFFECT],
932 0x57, 0);
933 pidff->device_control =
934 pidff_find_special_field(pidff->reports[PID_DEVICE_CONTROL],
935 0x96, 1);
936 pidff->block_load_status =
937 pidff_find_special_field(pidff->reports[PID_BLOCK_LOAD],
938 0x8b, 1);
939 pidff->effect_operation_status =
940 pidff_find_special_field(pidff->reports[PID_EFFECT_OPERATION],
941 0x78, 1);
943 hid_dbg(pidff->hid, "search done\n");
945 if (!pidff->create_new_effect_type || !pidff->set_effect_type) {
946 hid_err(pidff->hid, "effect lists not found\n");
947 return -1;
950 if (!pidff->effect_direction) {
951 hid_err(pidff->hid, "direction field not found\n");
952 return -1;
955 if (!pidff->device_control) {
956 hid_err(pidff->hid, "device control field not found\n");
957 return -1;
960 if (!pidff->block_load_status) {
961 hid_err(pidff->hid, "block load status field not found\n");
962 return -1;
965 if (!pidff->effect_operation_status) {
966 hid_err(pidff->hid, "effect operation field not found\n");
967 return -1;
970 pidff_find_special_keys(pidff->control_id, pidff->device_control,
971 pidff_device_control,
972 sizeof(pidff_device_control));
974 PIDFF_FIND_SPECIAL_KEYS(control_id, device_control, device_control);
976 if (!PIDFF_FIND_SPECIAL_KEYS(type_id, create_new_effect_type,
977 effect_types)) {
978 hid_err(pidff->hid, "no effect types found\n");
979 return -1;
982 if (PIDFF_FIND_SPECIAL_KEYS(status_id, block_load_status,
983 block_load_status) !=
984 sizeof(pidff_block_load_status)) {
985 hid_err(pidff->hid,
986 "block load status identifiers not found\n");
987 return -1;
990 if (PIDFF_FIND_SPECIAL_KEYS(operation_id, effect_operation_status,
991 effect_operation_status) !=
992 sizeof(pidff_effect_operation_status)) {
993 hid_err(pidff->hid, "effect operation identifiers not found\n");
994 return -1;
997 return 0;
1001 * Find the implemented effect types
1003 static int pidff_find_effects(struct pidff_device *pidff,
1004 struct input_dev *dev)
1006 int i;
1008 for (i = 0; i < sizeof(pidff_effect_types); i++) {
1009 int pidff_type = pidff->type_id[i];
1010 if (pidff->set_effect_type->usage[pidff_type].hid !=
1011 pidff->create_new_effect_type->usage[pidff_type].hid) {
1012 hid_err(pidff->hid,
1013 "effect type number %d is invalid\n", i);
1014 return -1;
1018 if (pidff->type_id[PID_CONSTANT])
1019 set_bit(FF_CONSTANT, dev->ffbit);
1020 if (pidff->type_id[PID_RAMP])
1021 set_bit(FF_RAMP, dev->ffbit);
1022 if (pidff->type_id[PID_SQUARE]) {
1023 set_bit(FF_SQUARE, dev->ffbit);
1024 set_bit(FF_PERIODIC, dev->ffbit);
1026 if (pidff->type_id[PID_SINE]) {
1027 set_bit(FF_SINE, dev->ffbit);
1028 set_bit(FF_PERIODIC, dev->ffbit);
1030 if (pidff->type_id[PID_TRIANGLE]) {
1031 set_bit(FF_TRIANGLE, dev->ffbit);
1032 set_bit(FF_PERIODIC, dev->ffbit);
1034 if (pidff->type_id[PID_SAW_UP]) {
1035 set_bit(FF_SAW_UP, dev->ffbit);
1036 set_bit(FF_PERIODIC, dev->ffbit);
1038 if (pidff->type_id[PID_SAW_DOWN]) {
1039 set_bit(FF_SAW_DOWN, dev->ffbit);
1040 set_bit(FF_PERIODIC, dev->ffbit);
1042 if (pidff->type_id[PID_SPRING])
1043 set_bit(FF_SPRING, dev->ffbit);
1044 if (pidff->type_id[PID_DAMPER])
1045 set_bit(FF_DAMPER, dev->ffbit);
1046 if (pidff->type_id[PID_INERTIA])
1047 set_bit(FF_INERTIA, dev->ffbit);
1048 if (pidff->type_id[PID_FRICTION])
1049 set_bit(FF_FRICTION, dev->ffbit);
1051 return 0;
1055 #define PIDFF_FIND_FIELDS(name, report, strict) \
1056 pidff_find_fields(pidff->name, pidff_ ## name, \
1057 pidff->reports[report], \
1058 sizeof(pidff_ ## name), strict)
1061 * Fill and check the pidff_usages
1063 static int pidff_init_fields(struct pidff_device *pidff, struct input_dev *dev)
1065 int envelope_ok = 0;
1067 if (PIDFF_FIND_FIELDS(set_effect, PID_SET_EFFECT, 1)) {
1068 hid_err(pidff->hid, "unknown set_effect report layout\n");
1069 return -ENODEV;
1072 PIDFF_FIND_FIELDS(block_load, PID_BLOCK_LOAD, 0);
1073 if (!pidff->block_load[PID_EFFECT_BLOCK_INDEX].value) {
1074 hid_err(pidff->hid, "unknown pid_block_load report layout\n");
1075 return -ENODEV;
1078 if (PIDFF_FIND_FIELDS(effect_operation, PID_EFFECT_OPERATION, 1)) {
1079 hid_err(pidff->hid, "unknown effect_operation report layout\n");
1080 return -ENODEV;
1083 if (PIDFF_FIND_FIELDS(block_free, PID_BLOCK_FREE, 1)) {
1084 hid_err(pidff->hid, "unknown pid_block_free report layout\n");
1085 return -ENODEV;
1088 if (!PIDFF_FIND_FIELDS(set_envelope, PID_SET_ENVELOPE, 1))
1089 envelope_ok = 1;
1091 if (pidff_find_special_fields(pidff) || pidff_find_effects(pidff, dev))
1092 return -ENODEV;
1094 if (!envelope_ok) {
1095 if (test_and_clear_bit(FF_CONSTANT, dev->ffbit))
1096 hid_warn(pidff->hid,
1097 "has constant effect but no envelope\n");
1098 if (test_and_clear_bit(FF_RAMP, dev->ffbit))
1099 hid_warn(pidff->hid,
1100 "has ramp effect but no envelope\n");
1102 if (test_and_clear_bit(FF_PERIODIC, dev->ffbit))
1103 hid_warn(pidff->hid,
1104 "has periodic effect but no envelope\n");
1107 if (test_bit(FF_CONSTANT, dev->ffbit) &&
1108 PIDFF_FIND_FIELDS(set_constant, PID_SET_CONSTANT, 1)) {
1109 hid_warn(pidff->hid, "unknown constant effect layout\n");
1110 clear_bit(FF_CONSTANT, dev->ffbit);
1113 if (test_bit(FF_RAMP, dev->ffbit) &&
1114 PIDFF_FIND_FIELDS(set_ramp, PID_SET_RAMP, 1)) {
1115 hid_warn(pidff->hid, "unknown ramp effect layout\n");
1116 clear_bit(FF_RAMP, dev->ffbit);
1119 if ((test_bit(FF_SPRING, dev->ffbit) ||
1120 test_bit(FF_DAMPER, dev->ffbit) ||
1121 test_bit(FF_FRICTION, dev->ffbit) ||
1122 test_bit(FF_INERTIA, dev->ffbit)) &&
1123 PIDFF_FIND_FIELDS(set_condition, PID_SET_CONDITION, 1)) {
1124 hid_warn(pidff->hid, "unknown condition effect layout\n");
1125 clear_bit(FF_SPRING, dev->ffbit);
1126 clear_bit(FF_DAMPER, dev->ffbit);
1127 clear_bit(FF_FRICTION, dev->ffbit);
1128 clear_bit(FF_INERTIA, dev->ffbit);
1131 if (test_bit(FF_PERIODIC, dev->ffbit) &&
1132 PIDFF_FIND_FIELDS(set_periodic, PID_SET_PERIODIC, 1)) {
1133 hid_warn(pidff->hid, "unknown periodic effect layout\n");
1134 clear_bit(FF_PERIODIC, dev->ffbit);
1137 PIDFF_FIND_FIELDS(pool, PID_POOL, 0);
1139 if (!PIDFF_FIND_FIELDS(device_gain, PID_DEVICE_GAIN, 1))
1140 set_bit(FF_GAIN, dev->ffbit);
1142 return 0;
1146 * Reset the device
1148 static void pidff_reset(struct pidff_device *pidff)
1150 struct hid_device *hid = pidff->hid;
1151 int i = 0;
1153 pidff->device_control->value[0] = pidff->control_id[PID_RESET];
1154 /* We reset twice as sometimes hid_wait_io isn't waiting long enough */
1155 hid_hw_request(hid, pidff->reports[PID_DEVICE_CONTROL], HID_REQ_SET_REPORT);
1156 hid_hw_wait(hid);
1157 hid_hw_request(hid, pidff->reports[PID_DEVICE_CONTROL], HID_REQ_SET_REPORT);
1158 hid_hw_wait(hid);
1160 pidff->device_control->value[0] =
1161 pidff->control_id[PID_ENABLE_ACTUATORS];
1162 hid_hw_request(hid, pidff->reports[PID_DEVICE_CONTROL], HID_REQ_SET_REPORT);
1163 hid_hw_wait(hid);
1165 /* pool report is sometimes messed up, refetch it */
1166 hid_hw_request(hid, pidff->reports[PID_POOL], HID_REQ_GET_REPORT);
1167 hid_hw_wait(hid);
1169 if (pidff->pool[PID_SIMULTANEOUS_MAX].value) {
1170 while (pidff->pool[PID_SIMULTANEOUS_MAX].value[0] < 2) {
1171 if (i++ > 20) {
1172 hid_warn(pidff->hid,
1173 "device reports %d simultaneous effects\n",
1174 pidff->pool[PID_SIMULTANEOUS_MAX].value[0]);
1175 break;
1177 hid_dbg(pidff->hid, "pid_pool requested again\n");
1178 hid_hw_request(hid, pidff->reports[PID_POOL],
1179 HID_REQ_GET_REPORT);
1180 hid_hw_wait(hid);
1186 * Test if autocenter modification is using the supported method
1188 static int pidff_check_autocenter(struct pidff_device *pidff,
1189 struct input_dev *dev)
1191 int error;
1194 * Let's find out if autocenter modification is supported
1195 * Specification doesn't specify anything, so we request an
1196 * effect upload and cancel it immediately. If the approved
1197 * effect id was one above the minimum, then we assume the first
1198 * effect id is a built-in spring type effect used for autocenter
1201 error = pidff_request_effect_upload(pidff, 1);
1202 if (error) {
1203 hid_err(pidff->hid, "upload request failed\n");
1204 return error;
1207 if (pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0] ==
1208 pidff->block_load[PID_EFFECT_BLOCK_INDEX].field->logical_minimum + 1) {
1209 pidff_autocenter(pidff, 0xffff);
1210 set_bit(FF_AUTOCENTER, dev->ffbit);
1211 } else {
1212 hid_notice(pidff->hid,
1213 "device has unknown autocenter control method\n");
1216 pidff_erase_pid(pidff,
1217 pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0]);
1219 return 0;
1224 * Check if the device is PID and initialize it
1226 int hid_pidff_init(struct hid_device *hid)
1228 struct pidff_device *pidff;
1229 struct hid_input *hidinput = list_entry(hid->inputs.next,
1230 struct hid_input, list);
1231 struct input_dev *dev = hidinput->input;
1232 struct ff_device *ff;
1233 int max_effects;
1234 int error;
1236 hid_dbg(hid, "starting pid init\n");
1238 if (list_empty(&hid->report_enum[HID_OUTPUT_REPORT].report_list)) {
1239 hid_dbg(hid, "not a PID device, no output report\n");
1240 return -ENODEV;
1243 pidff = kzalloc(sizeof(*pidff), GFP_KERNEL);
1244 if (!pidff)
1245 return -ENOMEM;
1247 pidff->hid = hid;
1249 hid_device_io_start(hid);
1251 pidff_find_reports(hid, HID_OUTPUT_REPORT, pidff);
1252 pidff_find_reports(hid, HID_FEATURE_REPORT, pidff);
1254 if (!pidff_reports_ok(pidff)) {
1255 hid_dbg(hid, "reports not ok, aborting\n");
1256 error = -ENODEV;
1257 goto fail;
1260 error = pidff_init_fields(pidff, dev);
1261 if (error)
1262 goto fail;
1264 pidff_reset(pidff);
1266 if (test_bit(FF_GAIN, dev->ffbit)) {
1267 pidff_set(&pidff->device_gain[PID_DEVICE_GAIN_FIELD], 0xffff);
1268 hid_hw_request(hid, pidff->reports[PID_DEVICE_GAIN],
1269 HID_REQ_SET_REPORT);
1272 error = pidff_check_autocenter(pidff, dev);
1273 if (error)
1274 goto fail;
1276 max_effects =
1277 pidff->block_load[PID_EFFECT_BLOCK_INDEX].field->logical_maximum -
1278 pidff->block_load[PID_EFFECT_BLOCK_INDEX].field->logical_minimum +
1280 hid_dbg(hid, "max effects is %d\n", max_effects);
1282 if (max_effects > PID_EFFECTS_MAX)
1283 max_effects = PID_EFFECTS_MAX;
1285 if (pidff->pool[PID_SIMULTANEOUS_MAX].value)
1286 hid_dbg(hid, "max simultaneous effects is %d\n",
1287 pidff->pool[PID_SIMULTANEOUS_MAX].value[0]);
1289 if (pidff->pool[PID_RAM_POOL_SIZE].value)
1290 hid_dbg(hid, "device memory size is %d bytes\n",
1291 pidff->pool[PID_RAM_POOL_SIZE].value[0]);
1293 if (pidff->pool[PID_DEVICE_MANAGED_POOL].value &&
1294 pidff->pool[PID_DEVICE_MANAGED_POOL].value[0] == 0) {
1295 hid_notice(hid,
1296 "device does not support device managed pool\n");
1297 goto fail;
1300 error = input_ff_create(dev, max_effects);
1301 if (error)
1302 goto fail;
1304 ff = dev->ff;
1305 ff->private = pidff;
1306 ff->upload = pidff_upload_effect;
1307 ff->erase = pidff_erase_effect;
1308 ff->set_gain = pidff_set_gain;
1309 ff->set_autocenter = pidff_set_autocenter;
1310 ff->playback = pidff_playback;
1312 hid_info(dev, "Force feedback for USB HID PID devices by Anssi Hannula <anssi.hannula@gmail.com>\n");
1314 hid_device_io_stop(hid);
1316 return 0;
1318 fail:
1319 hid_device_io_stop(hid);
1321 kfree(pidff);
1322 return error;