2 * Force feedback driver for USB HID PID compliant devices
4 * Copyright (c) 2005, 2006 Anssi Hannula <anssi.hannula@gmail.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #define debug(format, arg...) pr_debug("hid-pidff: " format "\n" , ## arg)
27 #include <linux/sched.h>
28 #include <linux/input.h>
29 #include <linux/usb.h>
33 #define PID_EFFECTS_MAX 64
35 /* Report usage table used to put reports into an array */
37 #define PID_SET_EFFECT 0
38 #define PID_EFFECT_OPERATION 1
39 #define PID_DEVICE_GAIN 2
41 #define PID_BLOCK_LOAD 4
42 #define PID_BLOCK_FREE 5
43 #define PID_DEVICE_CONTROL 6
44 #define PID_CREATE_NEW_EFFECT 7
46 #define PID_REQUIRED_REPORTS 7
48 #define PID_SET_ENVELOPE 8
49 #define PID_SET_CONDITION 9
50 #define PID_SET_PERIODIC 10
51 #define PID_SET_CONSTANT 11
52 #define PID_SET_RAMP 12
53 static const u8 pidff_reports[] = {
54 0x21, 0x77, 0x7d, 0x7f, 0x89, 0x90, 0x96, 0xab,
55 0x5a, 0x5f, 0x6e, 0x73, 0x74
58 /* device_control is really 0x95, but 0x96 specified as it is the usage of
59 the only field in that report */
61 /* Value usage tables used to put fields and values into arrays */
63 #define PID_EFFECT_BLOCK_INDEX 0
65 #define PID_DURATION 1
67 #define PID_TRIGGER_BUTTON 3
68 #define PID_TRIGGER_REPEAT_INT 4
69 #define PID_DIRECTION_ENABLE 5
70 #define PID_START_DELAY 6
71 static const u8 pidff_set_effect[] = {
72 0x22, 0x50, 0x52, 0x53, 0x54, 0x56, 0xa7
75 #define PID_ATTACK_LEVEL 1
76 #define PID_ATTACK_TIME 2
77 #define PID_FADE_LEVEL 3
78 #define PID_FADE_TIME 4
79 static const u8 pidff_set_envelope[] = { 0x22, 0x5b, 0x5c, 0x5d, 0x5e };
81 #define PID_PARAM_BLOCK_OFFSET 1
82 #define PID_CP_OFFSET 2
83 #define PID_POS_COEFFICIENT 3
84 #define PID_NEG_COEFFICIENT 4
85 #define PID_POS_SATURATION 5
86 #define PID_NEG_SATURATION 6
87 #define PID_DEAD_BAND 7
88 static const u8 pidff_set_condition[] = {
89 0x22, 0x23, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65
92 #define PID_MAGNITUDE 1
96 static const u8 pidff_set_periodic[] = { 0x22, 0x70, 0x6f, 0x71, 0x72 };
97 static const u8 pidff_set_constant[] = { 0x22, 0x70 };
99 #define PID_RAMP_START 1
100 #define PID_RAMP_END 2
101 static const u8 pidff_set_ramp[] = { 0x22, 0x75, 0x76 };
103 #define PID_RAM_POOL_AVAILABLE 1
104 static const u8 pidff_block_load[] = { 0x22, 0xac };
106 #define PID_LOOP_COUNT 1
107 static const u8 pidff_effect_operation[] = { 0x22, 0x7c };
109 static const u8 pidff_block_free[] = { 0x22 };
111 #define PID_DEVICE_GAIN_FIELD 0
112 static const u8 pidff_device_gain[] = { 0x7e };
114 #define PID_RAM_POOL_SIZE 0
115 #define PID_SIMULTANEOUS_MAX 1
116 #define PID_DEVICE_MANAGED_POOL 2
117 static const u8 pidff_pool[] = { 0x80, 0x83, 0xa9 };
119 /* Special field key tables used to put special field keys into arrays */
121 #define PID_ENABLE_ACTUATORS 0
123 static const u8 pidff_device_control[] = { 0x97, 0x9a };
125 #define PID_CONSTANT 0
129 #define PID_TRIANGLE 4
131 #define PID_SAW_DOWN 6
134 #define PID_INERTIA 9
135 #define PID_FRICTION 10
136 static const u8 pidff_effect_types[] = {
137 0x26, 0x27, 0x30, 0x31, 0x32, 0x33, 0x34,
138 0x40, 0x41, 0x42, 0x43
141 #define PID_BLOCK_LOAD_SUCCESS 0
142 #define PID_BLOCK_LOAD_FULL 1
143 static const u8 pidff_block_load_status[] = { 0x8c, 0x8d };
145 #define PID_EFFECT_START 0
146 #define PID_EFFECT_STOP 1
147 static const u8 pidff_effect_operation_status[] = { 0x79, 0x7b };
150 struct hid_field *field;
154 struct pidff_device {
155 struct hid_device *hid;
157 struct hid_report *reports[sizeof(pidff_reports)];
159 struct pidff_usage set_effect[sizeof(pidff_set_effect)];
160 struct pidff_usage set_envelope[sizeof(pidff_set_envelope)];
161 struct pidff_usage set_condition[sizeof(pidff_set_condition)];
162 struct pidff_usage set_periodic[sizeof(pidff_set_periodic)];
163 struct pidff_usage set_constant[sizeof(pidff_set_constant)];
164 struct pidff_usage set_ramp[sizeof(pidff_set_ramp)];
166 struct pidff_usage device_gain[sizeof(pidff_device_gain)];
167 struct pidff_usage block_load[sizeof(pidff_block_load)];
168 struct pidff_usage pool[sizeof(pidff_pool)];
169 struct pidff_usage effect_operation[sizeof(pidff_effect_operation)];
170 struct pidff_usage block_free[sizeof(pidff_block_free)];
172 /* Special field is a field that is not composed of
173 usage<->value pairs that pidff_usage values are */
175 /* Special field in create_new_effect */
176 struct hid_field *create_new_effect_type;
178 /* Special fields in set_effect */
179 struct hid_field *set_effect_type;
180 struct hid_field *effect_direction;
182 /* Special field in device_control */
183 struct hid_field *device_control;
185 /* Special field in block_load */
186 struct hid_field *block_load_status;
188 /* Special field in effect_operation */
189 struct hid_field *effect_operation_status;
191 int control_id[sizeof(pidff_device_control)];
192 int type_id[sizeof(pidff_effect_types)];
193 int status_id[sizeof(pidff_block_load_status)];
194 int operation_id[sizeof(pidff_effect_operation_status)];
196 int pid_id[PID_EFFECTS_MAX];
200 * Scale an unsigned value with range 0..max for the given field
202 static int pidff_rescale(int i, int max, struct hid_field *field)
204 return i * (field->logical_maximum - field->logical_minimum) / max +
205 field->logical_minimum;
209 * Scale a signed value in range -0x8000..0x7fff for the given field
211 static int pidff_rescale_signed(int i, struct hid_field *field)
213 return i == 0 ? 0 : i >
214 0 ? i * field->logical_maximum / 0x7fff : i *
215 field->logical_minimum / -0x8000;
218 static void pidff_set(struct pidff_usage *usage, u16 value)
220 usage->value[0] = pidff_rescale(value, 0xffff, usage->field);
221 debug("calculated from %d to %d", value, usage->value[0]);
224 static void pidff_set_signed(struct pidff_usage *usage, s16 value)
226 if (usage->field->logical_minimum < 0)
227 usage->value[0] = pidff_rescale_signed(value, usage->field);
231 pidff_rescale(-value, 0x8000, usage->field);
234 pidff_rescale(value, 0x7fff, usage->field);
236 debug("calculated from %d to %d", value, usage->value[0]);
240 * Send envelope report to the device
242 static void pidff_set_envelope_report(struct pidff_device *pidff,
243 struct ff_envelope *envelope)
245 pidff->set_envelope[PID_EFFECT_BLOCK_INDEX].value[0] =
246 pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0];
248 pidff->set_envelope[PID_ATTACK_LEVEL].value[0] =
249 pidff_rescale(envelope->attack_level >
250 0x7fff ? 0x7fff : envelope->attack_level, 0x7fff,
251 pidff->set_envelope[PID_ATTACK_LEVEL].field);
252 pidff->set_envelope[PID_FADE_LEVEL].value[0] =
253 pidff_rescale(envelope->fade_level >
254 0x7fff ? 0x7fff : envelope->fade_level, 0x7fff,
255 pidff->set_envelope[PID_FADE_LEVEL].field);
257 pidff->set_envelope[PID_ATTACK_TIME].value[0] = envelope->attack_length;
258 pidff->set_envelope[PID_FADE_TIME].value[0] = envelope->fade_length;
260 debug("attack %u => %d", envelope->attack_level,
261 pidff->set_envelope[PID_ATTACK_LEVEL].value[0]);
263 hid_submit_report(pidff->hid, pidff->reports[PID_SET_ENVELOPE],
268 * Test if the new envelope differs from old one
270 static int pidff_needs_set_envelope(struct ff_envelope *envelope,
271 struct ff_envelope *old)
273 return envelope->attack_level != old->attack_level ||
274 envelope->fade_level != old->fade_level ||
275 envelope->attack_length != old->attack_length ||
276 envelope->fade_length != old->fade_length;
280 * Send constant force report to the device
282 static void pidff_set_constant_force_report(struct pidff_device *pidff,
283 struct ff_effect *effect)
285 pidff->set_constant[PID_EFFECT_BLOCK_INDEX].value[0] =
286 pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0];
287 pidff_set_signed(&pidff->set_constant[PID_MAGNITUDE],
288 effect->u.constant.level);
290 hid_submit_report(pidff->hid, pidff->reports[PID_SET_CONSTANT],
295 * Test if the constant parameters have changed between effects
297 static int pidff_needs_set_constant(struct ff_effect *effect,
298 struct ff_effect *old)
300 return effect->u.constant.level != old->u.constant.level;
304 * Send set effect report to the device
306 static void pidff_set_effect_report(struct pidff_device *pidff,
307 struct ff_effect *effect)
309 pidff->set_effect[PID_EFFECT_BLOCK_INDEX].value[0] =
310 pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0];
311 pidff->set_effect_type->value[0] =
312 pidff->create_new_effect_type->value[0];
313 pidff->set_effect[PID_DURATION].value[0] = effect->replay.length;
314 pidff->set_effect[PID_TRIGGER_BUTTON].value[0] = effect->trigger.button;
315 pidff->set_effect[PID_TRIGGER_REPEAT_INT].value[0] =
316 effect->trigger.interval;
317 pidff->set_effect[PID_GAIN].value[0] =
318 pidff->set_effect[PID_GAIN].field->logical_maximum;
319 pidff->set_effect[PID_DIRECTION_ENABLE].value[0] = 1;
320 pidff->effect_direction->value[0] =
321 pidff_rescale(effect->direction, 0xffff,
322 pidff->effect_direction);
323 pidff->set_effect[PID_START_DELAY].value[0] = effect->replay.delay;
325 hid_submit_report(pidff->hid, pidff->reports[PID_SET_EFFECT],
330 * Test if the values used in set_effect have changed
332 static int pidff_needs_set_effect(struct ff_effect *effect,
333 struct ff_effect *old)
335 return effect->replay.length != old->replay.length ||
336 effect->trigger.interval != old->trigger.interval ||
337 effect->trigger.button != old->trigger.button ||
338 effect->direction != old->direction ||
339 effect->replay.delay != old->replay.delay;
343 * Send periodic effect report to the device
345 static void pidff_set_periodic_report(struct pidff_device *pidff,
346 struct ff_effect *effect)
348 pidff->set_periodic[PID_EFFECT_BLOCK_INDEX].value[0] =
349 pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0];
350 pidff_set_signed(&pidff->set_periodic[PID_MAGNITUDE],
351 effect->u.periodic.magnitude);
352 pidff_set_signed(&pidff->set_periodic[PID_OFFSET],
353 effect->u.periodic.offset);
354 pidff_set(&pidff->set_periodic[PID_PHASE], effect->u.periodic.phase);
355 pidff->set_periodic[PID_PERIOD].value[0] = effect->u.periodic.period;
357 hid_submit_report(pidff->hid, pidff->reports[PID_SET_PERIODIC],
363 * Test if periodic effect parameters have changed
365 static int pidff_needs_set_periodic(struct ff_effect *effect,
366 struct ff_effect *old)
368 return effect->u.periodic.magnitude != old->u.periodic.magnitude ||
369 effect->u.periodic.offset != old->u.periodic.offset ||
370 effect->u.periodic.phase != old->u.periodic.phase ||
371 effect->u.periodic.period != old->u.periodic.period;
375 * Send condition effect reports to the device
377 static void pidff_set_condition_report(struct pidff_device *pidff,
378 struct ff_effect *effect)
382 pidff->set_condition[PID_EFFECT_BLOCK_INDEX].value[0] =
383 pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0];
385 for (i = 0; i < 2; i++) {
386 pidff->set_condition[PID_PARAM_BLOCK_OFFSET].value[0] = i;
387 pidff_set_signed(&pidff->set_condition[PID_CP_OFFSET],
388 effect->u.condition[i].center);
389 pidff_set_signed(&pidff->set_condition[PID_POS_COEFFICIENT],
390 effect->u.condition[i].right_coeff);
391 pidff_set_signed(&pidff->set_condition[PID_NEG_COEFFICIENT],
392 effect->u.condition[i].left_coeff);
393 pidff_set(&pidff->set_condition[PID_POS_SATURATION],
394 effect->u.condition[i].right_saturation);
395 pidff_set(&pidff->set_condition[PID_NEG_SATURATION],
396 effect->u.condition[i].left_saturation);
397 pidff_set(&pidff->set_condition[PID_DEAD_BAND],
398 effect->u.condition[i].deadband);
399 hid_wait_io(pidff->hid);
400 hid_submit_report(pidff->hid, pidff->reports[PID_SET_CONDITION],
406 * Test if condition effect parameters have changed
408 static int pidff_needs_set_condition(struct ff_effect *effect,
409 struct ff_effect *old)
414 for (i = 0; i < 2; i++) {
415 struct ff_condition_effect *cond = &effect->u.condition[i];
416 struct ff_condition_effect *old_cond = &old->u.condition[i];
418 ret |= cond->center != old_cond->center ||
419 cond->right_coeff != old_cond->right_coeff ||
420 cond->left_coeff != old_cond->left_coeff ||
421 cond->right_saturation != old_cond->right_saturation ||
422 cond->left_saturation != old_cond->left_saturation ||
423 cond->deadband != old_cond->deadband;
430 * Send ramp force report to the device
432 static void pidff_set_ramp_force_report(struct pidff_device *pidff,
433 struct ff_effect *effect)
435 pidff->set_ramp[PID_EFFECT_BLOCK_INDEX].value[0] =
436 pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0];
437 pidff_set_signed(&pidff->set_ramp[PID_RAMP_START],
438 effect->u.ramp.start_level);
439 pidff_set_signed(&pidff->set_ramp[PID_RAMP_END],
440 effect->u.ramp.end_level);
441 hid_submit_report(pidff->hid, pidff->reports[PID_SET_RAMP],
446 * Test if ramp force parameters have changed
448 static int pidff_needs_set_ramp(struct ff_effect *effect, struct ff_effect *old)
450 return effect->u.ramp.start_level != old->u.ramp.start_level ||
451 effect->u.ramp.end_level != old->u.ramp.end_level;
455 * Send a request for effect upload to the device
457 * Returns 0 if device reported success, -ENOSPC if the device reported memory
458 * is full. Upon unknown response the function will retry for 60 times, if
459 * still unsuccessful -EIO is returned.
461 static int pidff_request_effect_upload(struct pidff_device *pidff, int efnum)
465 pidff->create_new_effect_type->value[0] = efnum;
466 hid_submit_report(pidff->hid, pidff->reports[PID_CREATE_NEW_EFFECT],
468 debug("create_new_effect sent, type: %d", efnum);
470 pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0] = 0;
471 pidff->block_load_status->value[0] = 0;
472 hid_wait_io(pidff->hid);
474 for (j = 0; j < 60; j++) {
475 debug("pid_block_load requested");
476 hid_submit_report(pidff->hid, pidff->reports[PID_BLOCK_LOAD],
478 hid_wait_io(pidff->hid);
479 if (pidff->block_load_status->value[0] ==
480 pidff->status_id[PID_BLOCK_LOAD_SUCCESS]) {
481 debug("device reported free memory: %d bytes",
482 pidff->block_load[PID_RAM_POOL_AVAILABLE].value ?
483 pidff->block_load[PID_RAM_POOL_AVAILABLE].value[0] : -1);
486 if (pidff->block_load_status->value[0] ==
487 pidff->status_id[PID_BLOCK_LOAD_FULL]) {
488 debug("not enough memory free: %d bytes",
489 pidff->block_load[PID_RAM_POOL_AVAILABLE].value ?
490 pidff->block_load[PID_RAM_POOL_AVAILABLE].value[0] : -1);
494 printk(KERN_ERR "hid-pidff: pid_block_load failed 60 times\n");
499 * Play the effect with PID id n times
501 static void pidff_playback_pid(struct pidff_device *pidff, int pid_id, int n)
503 pidff->effect_operation[PID_EFFECT_BLOCK_INDEX].value[0] = pid_id;
506 pidff->effect_operation_status->value[0] =
507 pidff->operation_id[PID_EFFECT_STOP];
509 pidff->effect_operation_status->value[0] =
510 pidff->operation_id[PID_EFFECT_START];
511 pidff->effect_operation[PID_LOOP_COUNT].value[0] = n;
514 hid_wait_io(pidff->hid);
515 hid_submit_report(pidff->hid, pidff->reports[PID_EFFECT_OPERATION],
520 * Play the effect with effect id @effect_id for @value times
522 static int pidff_playback(struct input_dev *dev, int effect_id, int value)
524 struct pidff_device *pidff = dev->ff->private;
526 pidff_playback_pid(pidff, pidff->pid_id[effect_id], value);
532 * Erase effect with PID id
534 static void pidff_erase_pid(struct pidff_device *pidff, int pid_id)
536 pidff->block_free[PID_EFFECT_BLOCK_INDEX].value[0] = pid_id;
537 hid_submit_report(pidff->hid, pidff->reports[PID_BLOCK_FREE],
542 * Stop and erase effect with effect_id
544 static int pidff_erase_effect(struct input_dev *dev, int effect_id)
546 struct pidff_device *pidff = dev->ff->private;
547 int pid_id = pidff->pid_id[effect_id];
549 debug("starting to erase %d/%d", effect_id, pidff->pid_id[effect_id]);
550 pidff_playback_pid(pidff, pid_id, 0);
551 pidff_erase_pid(pidff, pid_id);
557 * Effect upload handler
559 static int pidff_upload_effect(struct input_dev *dev, struct ff_effect *effect,
560 struct ff_effect *old)
562 struct pidff_device *pidff = dev->ff->private;
566 switch (effect->type) {
569 error = pidff_request_effect_upload(pidff,
570 pidff->type_id[PID_CONSTANT]);
574 if (!old || pidff_needs_set_effect(effect, old))
575 pidff_set_effect_report(pidff, effect);
576 if (!old || pidff_needs_set_constant(effect, old))
577 pidff_set_constant_force_report(pidff, effect);
579 pidff_needs_set_envelope(&effect->u.constant.envelope,
580 &old->u.constant.envelope))
581 pidff_set_envelope_report(pidff,
582 &effect->u.constant.envelope);
587 switch (effect->u.periodic.waveform) {
589 type_id = PID_SQUARE;
592 type_id = PID_TRIANGLE;
598 type_id = PID_SAW_UP;
601 type_id = PID_SAW_DOWN;
605 "hid-pidff: invalid waveform\n");
609 error = pidff_request_effect_upload(pidff,
610 pidff->type_id[type_id]);
614 if (!old || pidff_needs_set_effect(effect, old))
615 pidff_set_effect_report(pidff, effect);
616 if (!old || pidff_needs_set_periodic(effect, old))
617 pidff_set_periodic_report(pidff, effect);
619 pidff_needs_set_envelope(&effect->u.periodic.envelope,
620 &old->u.periodic.envelope))
621 pidff_set_envelope_report(pidff,
622 &effect->u.periodic.envelope);
627 error = pidff_request_effect_upload(pidff,
628 pidff->type_id[PID_RAMP]);
632 if (!old || pidff_needs_set_effect(effect, old))
633 pidff_set_effect_report(pidff, effect);
634 if (!old || pidff_needs_set_ramp(effect, old))
635 pidff_set_ramp_force_report(pidff, effect);
637 pidff_needs_set_envelope(&effect->u.ramp.envelope,
638 &old->u.ramp.envelope))
639 pidff_set_envelope_report(pidff,
640 &effect->u.ramp.envelope);
645 error = pidff_request_effect_upload(pidff,
646 pidff->type_id[PID_SPRING]);
650 if (!old || pidff_needs_set_effect(effect, old))
651 pidff_set_effect_report(pidff, effect);
652 if (!old || pidff_needs_set_condition(effect, old))
653 pidff_set_condition_report(pidff, effect);
658 error = pidff_request_effect_upload(pidff,
659 pidff->type_id[PID_FRICTION]);
663 if (!old || pidff_needs_set_effect(effect, old))
664 pidff_set_effect_report(pidff, effect);
665 if (!old || pidff_needs_set_condition(effect, old))
666 pidff_set_condition_report(pidff, effect);
671 error = pidff_request_effect_upload(pidff,
672 pidff->type_id[PID_DAMPER]);
676 if (!old || pidff_needs_set_effect(effect, old))
677 pidff_set_effect_report(pidff, effect);
678 if (!old || pidff_needs_set_condition(effect, old))
679 pidff_set_condition_report(pidff, effect);
684 error = pidff_request_effect_upload(pidff,
685 pidff->type_id[PID_INERTIA]);
689 if (!old || pidff_needs_set_effect(effect, old))
690 pidff_set_effect_report(pidff, effect);
691 if (!old || pidff_needs_set_condition(effect, old))
692 pidff_set_condition_report(pidff, effect);
696 printk(KERN_ERR "hid-pidff: invalid type\n");
701 pidff->pid_id[effect->id] =
702 pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0];
712 static void pidff_set_gain(struct input_dev *dev, u16 gain)
714 struct pidff_device *pidff = dev->ff->private;
716 pidff_set(&pidff->device_gain[PID_DEVICE_GAIN_FIELD], gain);
717 hid_submit_report(pidff->hid, pidff->reports[PID_DEVICE_GAIN],
721 static void pidff_autocenter(struct pidff_device *pidff, u16 magnitude)
723 struct hid_field *field =
724 pidff->block_load[PID_EFFECT_BLOCK_INDEX].field;
727 pidff_playback_pid(pidff, field->logical_minimum, 0);
731 pidff_playback_pid(pidff, field->logical_minimum, 1);
733 pidff->set_effect[PID_EFFECT_BLOCK_INDEX].value[0] =
734 pidff->block_load[PID_EFFECT_BLOCK_INDEX].field->logical_minimum;
735 pidff->set_effect_type->value[0] = pidff->type_id[PID_SPRING];
736 pidff->set_effect[PID_DURATION].value[0] = 0;
737 pidff->set_effect[PID_TRIGGER_BUTTON].value[0] = 0;
738 pidff->set_effect[PID_TRIGGER_REPEAT_INT].value[0] = 0;
739 pidff_set(&pidff->set_effect[PID_GAIN], magnitude);
740 pidff->set_effect[PID_START_DELAY].value[0] = 0;
742 hid_submit_report(pidff->hid, pidff->reports[PID_SET_EFFECT],
747 * pidff_set_autocenter() handler
749 static void pidff_set_autocenter(struct input_dev *dev, u16 magnitude)
751 struct pidff_device *pidff = dev->ff->private;
753 pidff_autocenter(pidff, magnitude);
757 * Find fields from a report and fill a pidff_usage
759 static int pidff_find_fields(struct pidff_usage *usage, const u8 *table,
760 struct hid_report *report, int count, int strict)
764 for (k = 0; k < count; k++) {
766 for (i = 0; i < report->maxfield; i++) {
767 if (report->field[i]->maxusage !=
768 report->field[i]->report_count) {
769 debug("maxusage and report_count do not match, "
773 for (j = 0; j < report->field[i]->maxusage; j++) {
774 if (report->field[i]->usage[j].hid ==
775 (HID_UP_PID | table[k])) {
776 debug("found %d at %d->%d", k, i, j);
777 usage[k].field = report->field[i];
779 &report->field[i]->value[j];
787 if (!found && strict) {
788 debug("failed to locate %d", k);
796 * Return index into pidff_reports for the given usage
798 static int pidff_check_usage(int usage)
802 for (i = 0; i < sizeof(pidff_reports); i++)
803 if (usage == (HID_UP_PID | pidff_reports[i]))
810 * Find the reports and fill pidff->reports[]
811 * report_type specifies either OUTPUT or FEATURE reports
813 static void pidff_find_reports(struct hid_device *hid, int report_type,
814 struct pidff_device *pidff)
816 struct hid_report *report;
819 list_for_each_entry(report,
820 &hid->report_enum[report_type].report_list, list) {
821 if (report->maxfield < 1)
823 ret = pidff_check_usage(report->field[0]->logical);
825 debug("found usage 0x%02x from field->logical",
827 pidff->reports[ret] = report;
832 * Sometimes logical collections are stacked to indicate
833 * different usages for the report and the field, in which
834 * case we want the usage of the parent. However, Linux HID
835 * implementation hides this fact, so we have to dig it up
838 i = report->field[0]->usage[0].collection_index;
840 hid->collection[i - 1].type != HID_COLLECTION_LOGICAL)
842 ret = pidff_check_usage(hid->collection[i - 1].usage);
843 if (ret != -1 && !pidff->reports[ret]) {
844 debug("found usage 0x%02x from collection array",
846 pidff->reports[ret] = report;
852 * Test if the required reports have been found
854 static int pidff_reports_ok(struct pidff_device *pidff)
858 for (i = 0; i <= PID_REQUIRED_REPORTS; i++) {
859 if (!pidff->reports[i]) {
860 debug("%d missing", i);
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)
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) {
880 report->field[i]->logical_minimum == 1)
881 return report->field[i];
883 printk(KERN_ERR "hid-pidff: logical_minimum "
884 "is not 1 as it should be\n");
893 * Fill a pidff->*_id struct table
895 static int pidff_find_special_keys(int *keys, struct hid_field *fld,
896 const u8 *usagetable, int count)
902 for (i = 0; i < count; i++) {
903 for (j = 0; j < fld->maxusage; j++) {
904 if (fld->usage[j].hid == (HID_UP_PID | usagetable[i])) {
914 #define PIDFF_FIND_SPECIAL_KEYS(keys, field, name) \
915 pidff_find_special_keys(pidff->keys, pidff->field, pidff_ ## name, \
916 sizeof(pidff_ ## name))
919 * Find and check the special fields
921 static int pidff_find_special_fields(struct pidff_device *pidff)
923 debug("finding special fields");
925 pidff->create_new_effect_type =
926 pidff_find_special_field(pidff->reports[PID_CREATE_NEW_EFFECT],
928 pidff->set_effect_type =
929 pidff_find_special_field(pidff->reports[PID_SET_EFFECT],
931 pidff->effect_direction =
932 pidff_find_special_field(pidff->reports[PID_SET_EFFECT],
934 pidff->device_control =
935 pidff_find_special_field(pidff->reports[PID_DEVICE_CONTROL],
937 pidff->block_load_status =
938 pidff_find_special_field(pidff->reports[PID_BLOCK_LOAD],
940 pidff->effect_operation_status =
941 pidff_find_special_field(pidff->reports[PID_EFFECT_OPERATION],
944 debug("search done");
946 if (!pidff->create_new_effect_type || !pidff->set_effect_type) {
947 printk(KERN_ERR "hid-pidff: effect lists not found\n");
951 if (!pidff->effect_direction) {
952 printk(KERN_ERR "hid-pidff: direction field not found\n");
956 if (!pidff->device_control) {
957 printk(KERN_ERR "hid-pidff: device control field not found\n");
961 if (!pidff->block_load_status) {
963 "hid-pidff: block load status field not found\n");
967 if (!pidff->effect_operation_status) {
969 "hid-pidff: effect operation field not found\n");
973 pidff_find_special_keys(pidff->control_id, pidff->device_control,
974 pidff_device_control,
975 sizeof(pidff_device_control));
977 PIDFF_FIND_SPECIAL_KEYS(control_id, device_control, device_control);
979 if (!PIDFF_FIND_SPECIAL_KEYS(type_id, create_new_effect_type,
981 printk(KERN_ERR "hid-pidff: no effect types found\n");
985 if (PIDFF_FIND_SPECIAL_KEYS(status_id, block_load_status,
986 block_load_status) !=
987 sizeof(pidff_block_load_status)) {
989 "hidpidff: block load status identifiers not found\n");
993 if (PIDFF_FIND_SPECIAL_KEYS(operation_id, effect_operation_status,
994 effect_operation_status) !=
995 sizeof(pidff_effect_operation_status)) {
997 "hidpidff: effect operation identifiers not found\n");
1005 * Find the implemented effect types
1007 static int pidff_find_effects(struct pidff_device *pidff,
1008 struct input_dev *dev)
1012 for (i = 0; i < sizeof(pidff_effect_types); i++) {
1013 int pidff_type = pidff->type_id[i];
1014 if (pidff->set_effect_type->usage[pidff_type].hid !=
1015 pidff->create_new_effect_type->usage[pidff_type].hid) {
1016 printk(KERN_ERR "hid-pidff: "
1017 "effect type number %d is invalid\n", i);
1022 if (pidff->type_id[PID_CONSTANT])
1023 set_bit(FF_CONSTANT, dev->ffbit);
1024 if (pidff->type_id[PID_RAMP])
1025 set_bit(FF_RAMP, dev->ffbit);
1026 if (pidff->type_id[PID_SQUARE]) {
1027 set_bit(FF_SQUARE, dev->ffbit);
1028 set_bit(FF_PERIODIC, dev->ffbit);
1030 if (pidff->type_id[PID_SINE]) {
1031 set_bit(FF_SINE, dev->ffbit);
1032 set_bit(FF_PERIODIC, dev->ffbit);
1034 if (pidff->type_id[PID_TRIANGLE]) {
1035 set_bit(FF_TRIANGLE, dev->ffbit);
1036 set_bit(FF_PERIODIC, dev->ffbit);
1038 if (pidff->type_id[PID_SAW_UP]) {
1039 set_bit(FF_SAW_UP, dev->ffbit);
1040 set_bit(FF_PERIODIC, dev->ffbit);
1042 if (pidff->type_id[PID_SAW_DOWN]) {
1043 set_bit(FF_SAW_DOWN, dev->ffbit);
1044 set_bit(FF_PERIODIC, dev->ffbit);
1046 if (pidff->type_id[PID_SPRING])
1047 set_bit(FF_SPRING, dev->ffbit);
1048 if (pidff->type_id[PID_DAMPER])
1049 set_bit(FF_DAMPER, dev->ffbit);
1050 if (pidff->type_id[PID_INERTIA])
1051 set_bit(FF_INERTIA, dev->ffbit);
1052 if (pidff->type_id[PID_FRICTION])
1053 set_bit(FF_FRICTION, dev->ffbit);
1059 #define PIDFF_FIND_FIELDS(name, report, strict) \
1060 pidff_find_fields(pidff->name, pidff_ ## name, \
1061 pidff->reports[report], \
1062 sizeof(pidff_ ## name), strict)
1065 * Fill and check the pidff_usages
1067 static int pidff_init_fields(struct pidff_device *pidff, struct input_dev *dev)
1069 int envelope_ok = 0;
1071 if (PIDFF_FIND_FIELDS(set_effect, PID_SET_EFFECT, 1)) {
1073 "hid-pidff: unknown set_effect report layout\n");
1077 PIDFF_FIND_FIELDS(block_load, PID_BLOCK_LOAD, 0);
1078 if (!pidff->block_load[PID_EFFECT_BLOCK_INDEX].value) {
1080 "hid-pidff: unknown pid_block_load report layout\n");
1084 if (PIDFF_FIND_FIELDS(effect_operation, PID_EFFECT_OPERATION, 1)) {
1086 "hid-pidff: unknown effect_operation report layout\n");
1090 if (PIDFF_FIND_FIELDS(block_free, PID_BLOCK_FREE, 1)) {
1092 "hid-pidff: unknown pid_block_free report layout\n");
1096 if (!PIDFF_FIND_FIELDS(set_envelope, PID_SET_ENVELOPE, 1))
1099 if (pidff_find_special_fields(pidff) || pidff_find_effects(pidff, dev))
1103 if (test_and_clear_bit(FF_CONSTANT, dev->ffbit))
1104 printk(KERN_WARNING "hid-pidff: "
1105 "has constant effect but no envelope\n");
1106 if (test_and_clear_bit(FF_RAMP, dev->ffbit))
1107 printk(KERN_WARNING "hid-pidff: "
1108 "has ramp effect but no envelope\n");
1110 if (test_and_clear_bit(FF_PERIODIC, dev->ffbit))
1111 printk(KERN_WARNING "hid-pidff: "
1112 "has periodic effect but no envelope\n");
1115 if (test_bit(FF_CONSTANT, dev->ffbit) &&
1116 PIDFF_FIND_FIELDS(set_constant, PID_SET_CONSTANT, 1)) {
1118 "hid-pidff: unknown constant effect layout\n");
1119 clear_bit(FF_CONSTANT, dev->ffbit);
1122 if (test_bit(FF_RAMP, dev->ffbit) &&
1123 PIDFF_FIND_FIELDS(set_ramp, PID_SET_RAMP, 1)) {
1124 printk(KERN_WARNING "hid-pidff: unknown ramp effect layout\n");
1125 clear_bit(FF_RAMP, dev->ffbit);
1128 if ((test_bit(FF_SPRING, dev->ffbit) ||
1129 test_bit(FF_DAMPER, dev->ffbit) ||
1130 test_bit(FF_FRICTION, dev->ffbit) ||
1131 test_bit(FF_INERTIA, dev->ffbit)) &&
1132 PIDFF_FIND_FIELDS(set_condition, PID_SET_CONDITION, 1)) {
1134 "hid-pidff: unknown condition effect layout\n");
1135 clear_bit(FF_SPRING, dev->ffbit);
1136 clear_bit(FF_DAMPER, dev->ffbit);
1137 clear_bit(FF_FRICTION, dev->ffbit);
1138 clear_bit(FF_INERTIA, dev->ffbit);
1141 if (test_bit(FF_PERIODIC, dev->ffbit) &&
1142 PIDFF_FIND_FIELDS(set_periodic, PID_SET_PERIODIC, 1)) {
1144 "hid-pidff: unknown periodic effect layout\n");
1145 clear_bit(FF_PERIODIC, dev->ffbit);
1148 PIDFF_FIND_FIELDS(pool, PID_POOL, 0);
1150 if (!PIDFF_FIND_FIELDS(device_gain, PID_DEVICE_GAIN, 1))
1151 set_bit(FF_GAIN, dev->ffbit);
1159 static void pidff_reset(struct pidff_device *pidff)
1161 struct hid_device *hid = pidff->hid;
1164 pidff->device_control->value[0] = pidff->control_id[PID_RESET];
1165 /* We reset twice as sometimes hid_wait_io isn't waiting long enough */
1166 hid_submit_report(hid, pidff->reports[PID_DEVICE_CONTROL], USB_DIR_OUT);
1168 hid_submit_report(hid, pidff->reports[PID_DEVICE_CONTROL], USB_DIR_OUT);
1171 pidff->device_control->value[0] =
1172 pidff->control_id[PID_ENABLE_ACTUATORS];
1173 hid_submit_report(hid, pidff->reports[PID_DEVICE_CONTROL], USB_DIR_OUT);
1176 /* pool report is sometimes messed up, refetch it */
1177 hid_submit_report(hid, pidff->reports[PID_POOL], USB_DIR_IN);
1180 if (pidff->pool[PID_SIMULTANEOUS_MAX].value) {
1181 int sim_effects = pidff->pool[PID_SIMULTANEOUS_MAX].value[0];
1182 while (sim_effects < 2) {
1184 printk(KERN_WARNING "hid-pidff: device reports "
1185 "%d simultaneous effects\n",
1189 debug("pid_pool requested again");
1190 hid_submit_report(hid, pidff->reports[PID_POOL],
1198 * Test if autocenter modification is using the supported method
1200 static int pidff_check_autocenter(struct pidff_device *pidff,
1201 struct input_dev *dev)
1206 * Let's find out if autocenter modification is supported
1207 * Specification doesn't specify anything, so we request an
1208 * effect upload and cancel it immediately. If the approved
1209 * effect id was one above the minimum, then we assume the first
1210 * effect id is a built-in spring type effect used for autocenter
1213 error = pidff_request_effect_upload(pidff, 1);
1215 printk(KERN_ERR "hid-pidff: upload request failed\n");
1219 if (pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0] ==
1220 pidff->block_load[PID_EFFECT_BLOCK_INDEX].field->logical_minimum + 1) {
1221 pidff_autocenter(pidff, 0xffff);
1222 set_bit(FF_AUTOCENTER, dev->ffbit);
1224 printk(KERN_NOTICE "hid-pidff: "
1225 "device has unknown autocenter control method\n");
1228 pidff_erase_pid(pidff,
1229 pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0]);
1236 * Check if the device is PID and initialize it
1238 int hid_pidff_init(struct hid_device *hid)
1240 struct pidff_device *pidff;
1241 struct hid_input *hidinput = list_entry(hid->inputs.next,
1242 struct hid_input, list);
1243 struct input_dev *dev = hidinput->input;
1244 struct ff_device *ff;
1248 debug("starting pid init");
1250 if (list_empty(&hid->report_enum[HID_OUTPUT_REPORT].report_list)) {
1251 debug("not a PID device, no output report");
1255 pidff = kzalloc(sizeof(*pidff), GFP_KERNEL);
1261 pidff_find_reports(hid, HID_OUTPUT_REPORT, pidff);
1262 pidff_find_reports(hid, HID_FEATURE_REPORT, pidff);
1264 if (!pidff_reports_ok(pidff)) {
1265 debug("reports not ok, aborting");
1270 error = pidff_init_fields(pidff, dev);
1276 if (test_bit(FF_GAIN, dev->ffbit)) {
1277 pidff_set(&pidff->device_gain[PID_DEVICE_GAIN_FIELD], 0xffff);
1278 hid_submit_report(pidff->hid, pidff->reports[PID_DEVICE_GAIN],
1282 error = pidff_check_autocenter(pidff, dev);
1287 pidff->block_load[PID_EFFECT_BLOCK_INDEX].field->logical_maximum -
1288 pidff->block_load[PID_EFFECT_BLOCK_INDEX].field->logical_minimum +
1290 debug("max effects is %d", max_effects);
1292 if (max_effects > PID_EFFECTS_MAX)
1293 max_effects = PID_EFFECTS_MAX;
1295 if (pidff->pool[PID_SIMULTANEOUS_MAX].value)
1296 debug("max simultaneous effects is %d",
1297 pidff->pool[PID_SIMULTANEOUS_MAX].value[0]);
1299 if (pidff->pool[PID_RAM_POOL_SIZE].value)
1300 debug("device memory size is %d bytes",
1301 pidff->pool[PID_RAM_POOL_SIZE].value[0]);
1303 if (pidff->pool[PID_DEVICE_MANAGED_POOL].value &&
1304 pidff->pool[PID_DEVICE_MANAGED_POOL].value[0] == 0) {
1305 printk(KERN_NOTICE "hid-pidff: "
1306 "device does not support device managed pool\n");
1310 error = input_ff_create(dev, max_effects);
1315 ff->private = pidff;
1316 ff->upload = pidff_upload_effect;
1317 ff->erase = pidff_erase_effect;
1318 ff->set_gain = pidff_set_gain;
1319 ff->set_autocenter = pidff_set_autocenter;
1320 ff->playback = pidff_playback;
1322 printk(KERN_INFO "Force feedback for USB HID PID devices by "
1323 "Anssi Hannula <anssi.hannula@gmail.com>\n");