V4L/DVB (6715): ivtv: Remove unnecessary register update
[linux-2.6/verdex.git] / drivers / hid / usbhid / hid-pidff.c
blob011326178c0670c5b5e680a47d741661101a45aa
1 /*
2 * Force feedback driver for USB HID PID compliant devices
4 * Copyright (c) 2005, 2006 Anssi Hannula <anssi.hannula@gmail.com>
5 */
7 /*
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
23 /* #define DEBUG */
25 #define debug(format, arg...) pr_debug("hid-pidff: " format "\n" , ## arg)
27 #include <linux/input.h>
28 #include <linux/usb.h>
30 #include <linux/hid.h>
32 #include "usbhid.h"
34 #define PID_EFFECTS_MAX 64
36 /* Report usage table used to put reports into an array */
38 #define PID_SET_EFFECT 0
39 #define PID_EFFECT_OPERATION 1
40 #define PID_DEVICE_GAIN 2
41 #define PID_POOL 3
42 #define PID_BLOCK_LOAD 4
43 #define PID_BLOCK_FREE 5
44 #define PID_DEVICE_CONTROL 6
45 #define PID_CREATE_NEW_EFFECT 7
47 #define PID_REQUIRED_REPORTS 7
49 #define PID_SET_ENVELOPE 8
50 #define PID_SET_CONDITION 9
51 #define PID_SET_PERIODIC 10
52 #define PID_SET_CONSTANT 11
53 #define PID_SET_RAMP 12
54 static const u8 pidff_reports[] = {
55 0x21, 0x77, 0x7d, 0x7f, 0x89, 0x90, 0x96, 0xab,
56 0x5a, 0x5f, 0x6e, 0x73, 0x74
59 /* device_control is really 0x95, but 0x96 specified as it is the usage of
60 the only field in that report */
62 /* Value usage tables used to put fields and values into arrays */
64 #define PID_EFFECT_BLOCK_INDEX 0
66 #define PID_DURATION 1
67 #define PID_GAIN 2
68 #define PID_TRIGGER_BUTTON 3
69 #define PID_TRIGGER_REPEAT_INT 4
70 #define PID_DIRECTION_ENABLE 5
71 #define PID_START_DELAY 6
72 static const u8 pidff_set_effect[] = {
73 0x22, 0x50, 0x52, 0x53, 0x54, 0x56, 0xa7
76 #define PID_ATTACK_LEVEL 1
77 #define PID_ATTACK_TIME 2
78 #define PID_FADE_LEVEL 3
79 #define PID_FADE_TIME 4
80 static const u8 pidff_set_envelope[] = { 0x22, 0x5b, 0x5c, 0x5d, 0x5e };
82 #define PID_PARAM_BLOCK_OFFSET 1
83 #define PID_CP_OFFSET 2
84 #define PID_POS_COEFFICIENT 3
85 #define PID_NEG_COEFFICIENT 4
86 #define PID_POS_SATURATION 5
87 #define PID_NEG_SATURATION 6
88 #define PID_DEAD_BAND 7
89 static const u8 pidff_set_condition[] = {
90 0x22, 0x23, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65
93 #define PID_MAGNITUDE 1
94 #define PID_OFFSET 2
95 #define PID_PHASE 3
96 #define PID_PERIOD 4
97 static const u8 pidff_set_periodic[] = { 0x22, 0x70, 0x6f, 0x71, 0x72 };
98 static const u8 pidff_set_constant[] = { 0x22, 0x70 };
100 #define PID_RAMP_START 1
101 #define PID_RAMP_END 2
102 static const u8 pidff_set_ramp[] = { 0x22, 0x75, 0x76 };
104 #define PID_RAM_POOL_AVAILABLE 1
105 static const u8 pidff_block_load[] = { 0x22, 0xac };
107 #define PID_LOOP_COUNT 1
108 static const u8 pidff_effect_operation[] = { 0x22, 0x7c };
110 static const u8 pidff_block_free[] = { 0x22 };
112 #define PID_DEVICE_GAIN_FIELD 0
113 static const u8 pidff_device_gain[] = { 0x7e };
115 #define PID_RAM_POOL_SIZE 0
116 #define PID_SIMULTANEOUS_MAX 1
117 #define PID_DEVICE_MANAGED_POOL 2
118 static const u8 pidff_pool[] = { 0x80, 0x83, 0xa9 };
120 /* Special field key tables used to put special field keys into arrays */
122 #define PID_ENABLE_ACTUATORS 0
123 #define PID_RESET 1
124 static const u8 pidff_device_control[] = { 0x97, 0x9a };
126 #define PID_CONSTANT 0
127 #define PID_RAMP 1
128 #define PID_SQUARE 2
129 #define PID_SINE 3
130 #define PID_TRIANGLE 4
131 #define PID_SAW_UP 5
132 #define PID_SAW_DOWN 6
133 #define PID_SPRING 7
134 #define PID_DAMPER 8
135 #define PID_INERTIA 9
136 #define PID_FRICTION 10
137 static const u8 pidff_effect_types[] = {
138 0x26, 0x27, 0x30, 0x31, 0x32, 0x33, 0x34,
139 0x40, 0x41, 0x42, 0x43
142 #define PID_BLOCK_LOAD_SUCCESS 0
143 #define PID_BLOCK_LOAD_FULL 1
144 static const u8 pidff_block_load_status[] = { 0x8c, 0x8d };
146 #define PID_EFFECT_START 0
147 #define PID_EFFECT_STOP 1
148 static const u8 pidff_effect_operation_status[] = { 0x79, 0x7b };
150 struct pidff_usage {
151 struct hid_field *field;
152 s32 *value;
155 struct pidff_device {
156 struct hid_device *hid;
158 struct hid_report *reports[sizeof(pidff_reports)];
160 struct pidff_usage set_effect[sizeof(pidff_set_effect)];
161 struct pidff_usage set_envelope[sizeof(pidff_set_envelope)];
162 struct pidff_usage set_condition[sizeof(pidff_set_condition)];
163 struct pidff_usage set_periodic[sizeof(pidff_set_periodic)];
164 struct pidff_usage set_constant[sizeof(pidff_set_constant)];
165 struct pidff_usage set_ramp[sizeof(pidff_set_ramp)];
167 struct pidff_usage device_gain[sizeof(pidff_device_gain)];
168 struct pidff_usage block_load[sizeof(pidff_block_load)];
169 struct pidff_usage pool[sizeof(pidff_pool)];
170 struct pidff_usage effect_operation[sizeof(pidff_effect_operation)];
171 struct pidff_usage block_free[sizeof(pidff_block_free)];
173 /* Special field is a field that is not composed of
174 usage<->value pairs that pidff_usage values are */
176 /* Special field in create_new_effect */
177 struct hid_field *create_new_effect_type;
179 /* Special fields in set_effect */
180 struct hid_field *set_effect_type;
181 struct hid_field *effect_direction;
183 /* Special field in device_control */
184 struct hid_field *device_control;
186 /* Special field in block_load */
187 struct hid_field *block_load_status;
189 /* Special field in effect_operation */
190 struct hid_field *effect_operation_status;
192 int control_id[sizeof(pidff_device_control)];
193 int type_id[sizeof(pidff_effect_types)];
194 int status_id[sizeof(pidff_block_load_status)];
195 int operation_id[sizeof(pidff_effect_operation_status)];
197 int pid_id[PID_EFFECTS_MAX];
201 * Scale an unsigned value with range 0..max for the given field
203 static int pidff_rescale(int i, int max, struct hid_field *field)
205 return i * (field->logical_maximum - field->logical_minimum) / max +
206 field->logical_minimum;
210 * Scale a signed value in range -0x8000..0x7fff for the given field
212 static int pidff_rescale_signed(int i, struct hid_field *field)
214 return i == 0 ? 0 : i >
215 0 ? i * field->logical_maximum / 0x7fff : i *
216 field->logical_minimum / -0x8000;
219 static void pidff_set(struct pidff_usage *usage, u16 value)
221 usage->value[0] = pidff_rescale(value, 0xffff, usage->field);
222 debug("calculated from %d to %d", value, usage->value[0]);
225 static void pidff_set_signed(struct pidff_usage *usage, s16 value)
227 if (usage->field->logical_minimum < 0)
228 usage->value[0] = pidff_rescale_signed(value, usage->field);
229 else {
230 if (value < 0)
231 usage->value[0] =
232 pidff_rescale(-value, 0x8000, usage->field);
233 else
234 usage->value[0] =
235 pidff_rescale(value, 0x7fff, usage->field);
237 debug("calculated from %d to %d", value, usage->value[0]);
241 * Send envelope report to the device
243 static void pidff_set_envelope_report(struct pidff_device *pidff,
244 struct ff_envelope *envelope)
246 pidff->set_envelope[PID_EFFECT_BLOCK_INDEX].value[0] =
247 pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0];
249 pidff->set_envelope[PID_ATTACK_LEVEL].value[0] =
250 pidff_rescale(envelope->attack_level >
251 0x7fff ? 0x7fff : envelope->attack_level, 0x7fff,
252 pidff->set_envelope[PID_ATTACK_LEVEL].field);
253 pidff->set_envelope[PID_FADE_LEVEL].value[0] =
254 pidff_rescale(envelope->fade_level >
255 0x7fff ? 0x7fff : envelope->fade_level, 0x7fff,
256 pidff->set_envelope[PID_FADE_LEVEL].field);
258 pidff->set_envelope[PID_ATTACK_TIME].value[0] = envelope->attack_length;
259 pidff->set_envelope[PID_FADE_TIME].value[0] = envelope->fade_length;
261 debug("attack %u => %d", envelope->attack_level,
262 pidff->set_envelope[PID_ATTACK_LEVEL].value[0]);
264 usbhid_submit_report(pidff->hid, pidff->reports[PID_SET_ENVELOPE],
265 USB_DIR_OUT);
269 * Test if the new envelope differs from old one
271 static int pidff_needs_set_envelope(struct ff_envelope *envelope,
272 struct ff_envelope *old)
274 return envelope->attack_level != old->attack_level ||
275 envelope->fade_level != old->fade_level ||
276 envelope->attack_length != old->attack_length ||
277 envelope->fade_length != old->fade_length;
281 * Send constant force report to the device
283 static void pidff_set_constant_force_report(struct pidff_device *pidff,
284 struct ff_effect *effect)
286 pidff->set_constant[PID_EFFECT_BLOCK_INDEX].value[0] =
287 pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0];
288 pidff_set_signed(&pidff->set_constant[PID_MAGNITUDE],
289 effect->u.constant.level);
291 usbhid_submit_report(pidff->hid, pidff->reports[PID_SET_CONSTANT],
292 USB_DIR_OUT);
296 * Test if the constant parameters have changed between effects
298 static int pidff_needs_set_constant(struct ff_effect *effect,
299 struct ff_effect *old)
301 return effect->u.constant.level != old->u.constant.level;
305 * Send set effect report to the device
307 static void pidff_set_effect_report(struct pidff_device *pidff,
308 struct ff_effect *effect)
310 pidff->set_effect[PID_EFFECT_BLOCK_INDEX].value[0] =
311 pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0];
312 pidff->set_effect_type->value[0] =
313 pidff->create_new_effect_type->value[0];
314 pidff->set_effect[PID_DURATION].value[0] = effect->replay.length;
315 pidff->set_effect[PID_TRIGGER_BUTTON].value[0] = effect->trigger.button;
316 pidff->set_effect[PID_TRIGGER_REPEAT_INT].value[0] =
317 effect->trigger.interval;
318 pidff->set_effect[PID_GAIN].value[0] =
319 pidff->set_effect[PID_GAIN].field->logical_maximum;
320 pidff->set_effect[PID_DIRECTION_ENABLE].value[0] = 1;
321 pidff->effect_direction->value[0] =
322 pidff_rescale(effect->direction, 0xffff,
323 pidff->effect_direction);
324 pidff->set_effect[PID_START_DELAY].value[0] = effect->replay.delay;
326 usbhid_submit_report(pidff->hid, pidff->reports[PID_SET_EFFECT],
327 USB_DIR_OUT);
331 * Test if the values used in set_effect have changed
333 static int pidff_needs_set_effect(struct ff_effect *effect,
334 struct ff_effect *old)
336 return effect->replay.length != old->replay.length ||
337 effect->trigger.interval != old->trigger.interval ||
338 effect->trigger.button != old->trigger.button ||
339 effect->direction != old->direction ||
340 effect->replay.delay != old->replay.delay;
344 * Send periodic effect report to the device
346 static void pidff_set_periodic_report(struct pidff_device *pidff,
347 struct ff_effect *effect)
349 pidff->set_periodic[PID_EFFECT_BLOCK_INDEX].value[0] =
350 pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0];
351 pidff_set_signed(&pidff->set_periodic[PID_MAGNITUDE],
352 effect->u.periodic.magnitude);
353 pidff_set_signed(&pidff->set_periodic[PID_OFFSET],
354 effect->u.periodic.offset);
355 pidff_set(&pidff->set_periodic[PID_PHASE], effect->u.periodic.phase);
356 pidff->set_periodic[PID_PERIOD].value[0] = effect->u.periodic.period;
358 usbhid_submit_report(pidff->hid, pidff->reports[PID_SET_PERIODIC],
359 USB_DIR_OUT);
364 * Test if periodic effect parameters have changed
366 static int pidff_needs_set_periodic(struct ff_effect *effect,
367 struct ff_effect *old)
369 return effect->u.periodic.magnitude != old->u.periodic.magnitude ||
370 effect->u.periodic.offset != old->u.periodic.offset ||
371 effect->u.periodic.phase != old->u.periodic.phase ||
372 effect->u.periodic.period != old->u.periodic.period;
376 * Send condition effect reports to the device
378 static void pidff_set_condition_report(struct pidff_device *pidff,
379 struct ff_effect *effect)
381 int i;
383 pidff->set_condition[PID_EFFECT_BLOCK_INDEX].value[0] =
384 pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0];
386 for (i = 0; i < 2; i++) {
387 pidff->set_condition[PID_PARAM_BLOCK_OFFSET].value[0] = i;
388 pidff_set_signed(&pidff->set_condition[PID_CP_OFFSET],
389 effect->u.condition[i].center);
390 pidff_set_signed(&pidff->set_condition[PID_POS_COEFFICIENT],
391 effect->u.condition[i].right_coeff);
392 pidff_set_signed(&pidff->set_condition[PID_NEG_COEFFICIENT],
393 effect->u.condition[i].left_coeff);
394 pidff_set(&pidff->set_condition[PID_POS_SATURATION],
395 effect->u.condition[i].right_saturation);
396 pidff_set(&pidff->set_condition[PID_NEG_SATURATION],
397 effect->u.condition[i].left_saturation);
398 pidff_set(&pidff->set_condition[PID_DEAD_BAND],
399 effect->u.condition[i].deadband);
400 usbhid_wait_io(pidff->hid);
401 usbhid_submit_report(pidff->hid, pidff->reports[PID_SET_CONDITION],
402 USB_DIR_OUT);
407 * Test if condition effect parameters have changed
409 static int pidff_needs_set_condition(struct ff_effect *effect,
410 struct ff_effect *old)
412 int i;
413 int ret = 0;
415 for (i = 0; i < 2; i++) {
416 struct ff_condition_effect *cond = &effect->u.condition[i];
417 struct ff_condition_effect *old_cond = &old->u.condition[i];
419 ret |= cond->center != old_cond->center ||
420 cond->right_coeff != old_cond->right_coeff ||
421 cond->left_coeff != old_cond->left_coeff ||
422 cond->right_saturation != old_cond->right_saturation ||
423 cond->left_saturation != old_cond->left_saturation ||
424 cond->deadband != old_cond->deadband;
427 return ret;
431 * Send ramp force report to the device
433 static void pidff_set_ramp_force_report(struct pidff_device *pidff,
434 struct ff_effect *effect)
436 pidff->set_ramp[PID_EFFECT_BLOCK_INDEX].value[0] =
437 pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0];
438 pidff_set_signed(&pidff->set_ramp[PID_RAMP_START],
439 effect->u.ramp.start_level);
440 pidff_set_signed(&pidff->set_ramp[PID_RAMP_END],
441 effect->u.ramp.end_level);
442 usbhid_submit_report(pidff->hid, pidff->reports[PID_SET_RAMP],
443 USB_DIR_OUT);
447 * Test if ramp force parameters have changed
449 static int pidff_needs_set_ramp(struct ff_effect *effect, struct ff_effect *old)
451 return effect->u.ramp.start_level != old->u.ramp.start_level ||
452 effect->u.ramp.end_level != old->u.ramp.end_level;
456 * Send a request for effect upload to the device
458 * Returns 0 if device reported success, -ENOSPC if the device reported memory
459 * is full. Upon unknown response the function will retry for 60 times, if
460 * still unsuccessful -EIO is returned.
462 static int pidff_request_effect_upload(struct pidff_device *pidff, int efnum)
464 int j;
466 pidff->create_new_effect_type->value[0] = efnum;
467 usbhid_submit_report(pidff->hid, pidff->reports[PID_CREATE_NEW_EFFECT],
468 USB_DIR_OUT);
469 debug("create_new_effect sent, type: %d", efnum);
471 pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0] = 0;
472 pidff->block_load_status->value[0] = 0;
473 usbhid_wait_io(pidff->hid);
475 for (j = 0; j < 60; j++) {
476 debug("pid_block_load requested");
477 usbhid_submit_report(pidff->hid, pidff->reports[PID_BLOCK_LOAD],
478 USB_DIR_IN);
479 usbhid_wait_io(pidff->hid);
480 if (pidff->block_load_status->value[0] ==
481 pidff->status_id[PID_BLOCK_LOAD_SUCCESS]) {
482 debug("device reported free memory: %d bytes",
483 pidff->block_load[PID_RAM_POOL_AVAILABLE].value ?
484 pidff->block_load[PID_RAM_POOL_AVAILABLE].value[0] : -1);
485 return 0;
487 if (pidff->block_load_status->value[0] ==
488 pidff->status_id[PID_BLOCK_LOAD_FULL]) {
489 debug("not enough memory free: %d bytes",
490 pidff->block_load[PID_RAM_POOL_AVAILABLE].value ?
491 pidff->block_load[PID_RAM_POOL_AVAILABLE].value[0] : -1);
492 return -ENOSPC;
495 printk(KERN_ERR "hid-pidff: pid_block_load failed 60 times\n");
496 return -EIO;
500 * Play the effect with PID id n times
502 static void pidff_playback_pid(struct pidff_device *pidff, int pid_id, int n)
504 pidff->effect_operation[PID_EFFECT_BLOCK_INDEX].value[0] = pid_id;
506 if (n == 0) {
507 pidff->effect_operation_status->value[0] =
508 pidff->operation_id[PID_EFFECT_STOP];
509 } else {
510 pidff->effect_operation_status->value[0] =
511 pidff->operation_id[PID_EFFECT_START];
512 pidff->effect_operation[PID_LOOP_COUNT].value[0] = n;
515 usbhid_wait_io(pidff->hid);
516 usbhid_submit_report(pidff->hid, pidff->reports[PID_EFFECT_OPERATION],
517 USB_DIR_OUT);
521 * Play the effect with effect id @effect_id for @value times
523 static int pidff_playback(struct input_dev *dev, int effect_id, int value)
525 struct pidff_device *pidff = dev->ff->private;
527 pidff_playback_pid(pidff, pidff->pid_id[effect_id], value);
529 return 0;
533 * Erase effect with PID id
535 static void pidff_erase_pid(struct pidff_device *pidff, int pid_id)
537 pidff->block_free[PID_EFFECT_BLOCK_INDEX].value[0] = pid_id;
538 usbhid_submit_report(pidff->hid, pidff->reports[PID_BLOCK_FREE],
539 USB_DIR_OUT);
543 * Stop and erase effect with effect_id
545 static int pidff_erase_effect(struct input_dev *dev, int effect_id)
547 struct pidff_device *pidff = dev->ff->private;
548 int pid_id = pidff->pid_id[effect_id];
550 debug("starting to erase %d/%d", effect_id, pidff->pid_id[effect_id]);
551 pidff_playback_pid(pidff, pid_id, 0);
552 pidff_erase_pid(pidff, pid_id);
554 return 0;
558 * Effect upload handler
560 static int pidff_upload_effect(struct input_dev *dev, struct ff_effect *effect,
561 struct ff_effect *old)
563 struct pidff_device *pidff = dev->ff->private;
564 int type_id;
565 int error;
567 switch (effect->type) {
568 case FF_CONSTANT:
569 if (!old) {
570 error = pidff_request_effect_upload(pidff,
571 pidff->type_id[PID_CONSTANT]);
572 if (error)
573 return error;
575 if (!old || pidff_needs_set_effect(effect, old))
576 pidff_set_effect_report(pidff, effect);
577 if (!old || pidff_needs_set_constant(effect, old))
578 pidff_set_constant_force_report(pidff, effect);
579 if (!old ||
580 pidff_needs_set_envelope(&effect->u.constant.envelope,
581 &old->u.constant.envelope))
582 pidff_set_envelope_report(pidff,
583 &effect->u.constant.envelope);
584 break;
586 case FF_PERIODIC:
587 if (!old) {
588 switch (effect->u.periodic.waveform) {
589 case FF_SQUARE:
590 type_id = PID_SQUARE;
591 break;
592 case FF_TRIANGLE:
593 type_id = PID_TRIANGLE;
594 break;
595 case FF_SINE:
596 type_id = PID_SINE;
597 break;
598 case FF_SAW_UP:
599 type_id = PID_SAW_UP;
600 break;
601 case FF_SAW_DOWN:
602 type_id = PID_SAW_DOWN;
603 break;
604 default:
605 printk(KERN_ERR
606 "hid-pidff: invalid waveform\n");
607 return -EINVAL;
610 error = pidff_request_effect_upload(pidff,
611 pidff->type_id[type_id]);
612 if (error)
613 return error;
615 if (!old || pidff_needs_set_effect(effect, old))
616 pidff_set_effect_report(pidff, effect);
617 if (!old || pidff_needs_set_periodic(effect, old))
618 pidff_set_periodic_report(pidff, effect);
619 if (!old ||
620 pidff_needs_set_envelope(&effect->u.periodic.envelope,
621 &old->u.periodic.envelope))
622 pidff_set_envelope_report(pidff,
623 &effect->u.periodic.envelope);
624 break;
626 case FF_RAMP:
627 if (!old) {
628 error = pidff_request_effect_upload(pidff,
629 pidff->type_id[PID_RAMP]);
630 if (error)
631 return error;
633 if (!old || pidff_needs_set_effect(effect, old))
634 pidff_set_effect_report(pidff, effect);
635 if (!old || pidff_needs_set_ramp(effect, old))
636 pidff_set_ramp_force_report(pidff, effect);
637 if (!old ||
638 pidff_needs_set_envelope(&effect->u.ramp.envelope,
639 &old->u.ramp.envelope))
640 pidff_set_envelope_report(pidff,
641 &effect->u.ramp.envelope);
642 break;
644 case FF_SPRING:
645 if (!old) {
646 error = pidff_request_effect_upload(pidff,
647 pidff->type_id[PID_SPRING]);
648 if (error)
649 return error;
651 if (!old || pidff_needs_set_effect(effect, old))
652 pidff_set_effect_report(pidff, effect);
653 if (!old || pidff_needs_set_condition(effect, old))
654 pidff_set_condition_report(pidff, effect);
655 break;
657 case FF_FRICTION:
658 if (!old) {
659 error = pidff_request_effect_upload(pidff,
660 pidff->type_id[PID_FRICTION]);
661 if (error)
662 return error;
664 if (!old || pidff_needs_set_effect(effect, old))
665 pidff_set_effect_report(pidff, effect);
666 if (!old || pidff_needs_set_condition(effect, old))
667 pidff_set_condition_report(pidff, effect);
668 break;
670 case FF_DAMPER:
671 if (!old) {
672 error = pidff_request_effect_upload(pidff,
673 pidff->type_id[PID_DAMPER]);
674 if (error)
675 return error;
677 if (!old || pidff_needs_set_effect(effect, old))
678 pidff_set_effect_report(pidff, effect);
679 if (!old || pidff_needs_set_condition(effect, old))
680 pidff_set_condition_report(pidff, effect);
681 break;
683 case FF_INERTIA:
684 if (!old) {
685 error = pidff_request_effect_upload(pidff,
686 pidff->type_id[PID_INERTIA]);
687 if (error)
688 return error;
690 if (!old || pidff_needs_set_effect(effect, old))
691 pidff_set_effect_report(pidff, effect);
692 if (!old || pidff_needs_set_condition(effect, old))
693 pidff_set_condition_report(pidff, effect);
694 break;
696 default:
697 printk(KERN_ERR "hid-pidff: invalid type\n");
698 return -EINVAL;
701 if (!old)
702 pidff->pid_id[effect->id] =
703 pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0];
705 debug("uploaded");
707 return 0;
711 * set_gain() handler
713 static void pidff_set_gain(struct input_dev *dev, u16 gain)
715 struct pidff_device *pidff = dev->ff->private;
717 pidff_set(&pidff->device_gain[PID_DEVICE_GAIN_FIELD], gain);
718 usbhid_submit_report(pidff->hid, pidff->reports[PID_DEVICE_GAIN],
719 USB_DIR_OUT);
722 static void pidff_autocenter(struct pidff_device *pidff, u16 magnitude)
724 struct hid_field *field =
725 pidff->block_load[PID_EFFECT_BLOCK_INDEX].field;
727 if (!magnitude) {
728 pidff_playback_pid(pidff, field->logical_minimum, 0);
729 return;
732 pidff_playback_pid(pidff, field->logical_minimum, 1);
734 pidff->set_effect[PID_EFFECT_BLOCK_INDEX].value[0] =
735 pidff->block_load[PID_EFFECT_BLOCK_INDEX].field->logical_minimum;
736 pidff->set_effect_type->value[0] = pidff->type_id[PID_SPRING];
737 pidff->set_effect[PID_DURATION].value[0] = 0;
738 pidff->set_effect[PID_TRIGGER_BUTTON].value[0] = 0;
739 pidff->set_effect[PID_TRIGGER_REPEAT_INT].value[0] = 0;
740 pidff_set(&pidff->set_effect[PID_GAIN], magnitude);
741 pidff->set_effect[PID_DIRECTION_ENABLE].value[0] = 1;
742 pidff->set_effect[PID_START_DELAY].value[0] = 0;
744 usbhid_submit_report(pidff->hid, pidff->reports[PID_SET_EFFECT],
745 USB_DIR_OUT);
749 * pidff_set_autocenter() handler
751 static void pidff_set_autocenter(struct input_dev *dev, u16 magnitude)
753 struct pidff_device *pidff = dev->ff->private;
755 pidff_autocenter(pidff, magnitude);
759 * Find fields from a report and fill a pidff_usage
761 static int pidff_find_fields(struct pidff_usage *usage, const u8 *table,
762 struct hid_report *report, int count, int strict)
764 int i, j, k, found;
766 for (k = 0; k < count; k++) {
767 found = 0;
768 for (i = 0; i < report->maxfield; i++) {
769 if (report->field[i]->maxusage !=
770 report->field[i]->report_count) {
771 debug("maxusage and report_count do not match, "
772 "skipping");
773 continue;
775 for (j = 0; j < report->field[i]->maxusage; j++) {
776 if (report->field[i]->usage[j].hid ==
777 (HID_UP_PID | table[k])) {
778 debug("found %d at %d->%d", k, i, j);
779 usage[k].field = report->field[i];
780 usage[k].value =
781 &report->field[i]->value[j];
782 found = 1;
783 break;
786 if (found)
787 break;
789 if (!found && strict) {
790 debug("failed to locate %d", k);
791 return -1;
794 return 0;
798 * Return index into pidff_reports for the given usage
800 static int pidff_check_usage(int usage)
802 int i;
804 for (i = 0; i < sizeof(pidff_reports); i++)
805 if (usage == (HID_UP_PID | pidff_reports[i]))
806 return i;
808 return -1;
812 * Find the reports and fill pidff->reports[]
813 * report_type specifies either OUTPUT or FEATURE reports
815 static void pidff_find_reports(struct hid_device *hid, int report_type,
816 struct pidff_device *pidff)
818 struct hid_report *report;
819 int i, ret;
821 list_for_each_entry(report,
822 &hid->report_enum[report_type].report_list, list) {
823 if (report->maxfield < 1)
824 continue;
825 ret = pidff_check_usage(report->field[0]->logical);
826 if (ret != -1) {
827 debug("found usage 0x%02x from field->logical",
828 pidff_reports[ret]);
829 pidff->reports[ret] = report;
830 continue;
834 * Sometimes logical collections are stacked to indicate
835 * different usages for the report and the field, in which
836 * case we want the usage of the parent. However, Linux HID
837 * implementation hides this fact, so we have to dig it up
838 * ourselves
840 i = report->field[0]->usage[0].collection_index;
841 if (i <= 0 ||
842 hid->collection[i - 1].type != HID_COLLECTION_LOGICAL)
843 continue;
844 ret = pidff_check_usage(hid->collection[i - 1].usage);
845 if (ret != -1 && !pidff->reports[ret]) {
846 debug("found usage 0x%02x from collection array",
847 pidff_reports[ret]);
848 pidff->reports[ret] = report;
854 * Test if the required reports have been found
856 static int pidff_reports_ok(struct pidff_device *pidff)
858 int i;
860 for (i = 0; i <= PID_REQUIRED_REPORTS; i++) {
861 if (!pidff->reports[i]) {
862 debug("%d missing", i);
863 return 0;
867 return 1;
871 * Find a field with a specific usage within a report
873 static struct hid_field *pidff_find_special_field(struct hid_report *report,
874 int usage, int enforce_min)
876 int i;
878 for (i = 0; i < report->maxfield; i++) {
879 if (report->field[i]->logical == (HID_UP_PID | usage) &&
880 report->field[i]->report_count > 0) {
881 if (!enforce_min ||
882 report->field[i]->logical_minimum == 1)
883 return report->field[i];
884 else {
885 printk(KERN_ERR "hid-pidff: logical_minimum "
886 "is not 1 as it should be\n");
887 return NULL;
891 return NULL;
895 * Fill a pidff->*_id struct table
897 static int pidff_find_special_keys(int *keys, struct hid_field *fld,
898 const u8 *usagetable, int count)
901 int i, j;
902 int found = 0;
904 for (i = 0; i < count; i++) {
905 for (j = 0; j < fld->maxusage; j++) {
906 if (fld->usage[j].hid == (HID_UP_PID | usagetable[i])) {
907 keys[i] = j + 1;
908 found++;
909 break;
913 return found;
916 #define PIDFF_FIND_SPECIAL_KEYS(keys, field, name) \
917 pidff_find_special_keys(pidff->keys, pidff->field, pidff_ ## name, \
918 sizeof(pidff_ ## name))
921 * Find and check the special fields
923 static int pidff_find_special_fields(struct pidff_device *pidff)
925 debug("finding special fields");
927 pidff->create_new_effect_type =
928 pidff_find_special_field(pidff->reports[PID_CREATE_NEW_EFFECT],
929 0x25, 1);
930 pidff->set_effect_type =
931 pidff_find_special_field(pidff->reports[PID_SET_EFFECT],
932 0x25, 1);
933 pidff->effect_direction =
934 pidff_find_special_field(pidff->reports[PID_SET_EFFECT],
935 0x57, 0);
936 pidff->device_control =
937 pidff_find_special_field(pidff->reports[PID_DEVICE_CONTROL],
938 0x96, 1);
939 pidff->block_load_status =
940 pidff_find_special_field(pidff->reports[PID_BLOCK_LOAD],
941 0x8b, 1);
942 pidff->effect_operation_status =
943 pidff_find_special_field(pidff->reports[PID_EFFECT_OPERATION],
944 0x78, 1);
946 debug("search done");
948 if (!pidff->create_new_effect_type || !pidff->set_effect_type) {
949 printk(KERN_ERR "hid-pidff: effect lists not found\n");
950 return -1;
953 if (!pidff->effect_direction) {
954 printk(KERN_ERR "hid-pidff: direction field not found\n");
955 return -1;
958 if (!pidff->device_control) {
959 printk(KERN_ERR "hid-pidff: device control field not found\n");
960 return -1;
963 if (!pidff->block_load_status) {
964 printk(KERN_ERR
965 "hid-pidff: block load status field not found\n");
966 return -1;
969 if (!pidff->effect_operation_status) {
970 printk(KERN_ERR
971 "hid-pidff: effect operation field not found\n");
972 return -1;
975 pidff_find_special_keys(pidff->control_id, pidff->device_control,
976 pidff_device_control,
977 sizeof(pidff_device_control));
979 PIDFF_FIND_SPECIAL_KEYS(control_id, device_control, device_control);
981 if (!PIDFF_FIND_SPECIAL_KEYS(type_id, create_new_effect_type,
982 effect_types)) {
983 printk(KERN_ERR "hid-pidff: no effect types found\n");
984 return -1;
987 if (PIDFF_FIND_SPECIAL_KEYS(status_id, block_load_status,
988 block_load_status) !=
989 sizeof(pidff_block_load_status)) {
990 printk(KERN_ERR
991 "hidpidff: block load status identifiers not found\n");
992 return -1;
995 if (PIDFF_FIND_SPECIAL_KEYS(operation_id, effect_operation_status,
996 effect_operation_status) !=
997 sizeof(pidff_effect_operation_status)) {
998 printk(KERN_ERR
999 "hidpidff: effect operation identifiers not found\n");
1000 return -1;
1003 return 0;
1007 * Find the implemented effect types
1009 static int pidff_find_effects(struct pidff_device *pidff,
1010 struct input_dev *dev)
1012 int i;
1014 for (i = 0; i < sizeof(pidff_effect_types); i++) {
1015 int pidff_type = pidff->type_id[i];
1016 if (pidff->set_effect_type->usage[pidff_type].hid !=
1017 pidff->create_new_effect_type->usage[pidff_type].hid) {
1018 printk(KERN_ERR "hid-pidff: "
1019 "effect type number %d is invalid\n", i);
1020 return -1;
1024 if (pidff->type_id[PID_CONSTANT])
1025 set_bit(FF_CONSTANT, dev->ffbit);
1026 if (pidff->type_id[PID_RAMP])
1027 set_bit(FF_RAMP, dev->ffbit);
1028 if (pidff->type_id[PID_SQUARE]) {
1029 set_bit(FF_SQUARE, dev->ffbit);
1030 set_bit(FF_PERIODIC, dev->ffbit);
1032 if (pidff->type_id[PID_SINE]) {
1033 set_bit(FF_SINE, dev->ffbit);
1034 set_bit(FF_PERIODIC, dev->ffbit);
1036 if (pidff->type_id[PID_TRIANGLE]) {
1037 set_bit(FF_TRIANGLE, dev->ffbit);
1038 set_bit(FF_PERIODIC, dev->ffbit);
1040 if (pidff->type_id[PID_SAW_UP]) {
1041 set_bit(FF_SAW_UP, dev->ffbit);
1042 set_bit(FF_PERIODIC, dev->ffbit);
1044 if (pidff->type_id[PID_SAW_DOWN]) {
1045 set_bit(FF_SAW_DOWN, dev->ffbit);
1046 set_bit(FF_PERIODIC, dev->ffbit);
1048 if (pidff->type_id[PID_SPRING])
1049 set_bit(FF_SPRING, dev->ffbit);
1050 if (pidff->type_id[PID_DAMPER])
1051 set_bit(FF_DAMPER, dev->ffbit);
1052 if (pidff->type_id[PID_INERTIA])
1053 set_bit(FF_INERTIA, dev->ffbit);
1054 if (pidff->type_id[PID_FRICTION])
1055 set_bit(FF_FRICTION, dev->ffbit);
1057 return 0;
1061 #define PIDFF_FIND_FIELDS(name, report, strict) \
1062 pidff_find_fields(pidff->name, pidff_ ## name, \
1063 pidff->reports[report], \
1064 sizeof(pidff_ ## name), strict)
1067 * Fill and check the pidff_usages
1069 static int pidff_init_fields(struct pidff_device *pidff, struct input_dev *dev)
1071 int envelope_ok = 0;
1073 if (PIDFF_FIND_FIELDS(set_effect, PID_SET_EFFECT, 1)) {
1074 printk(KERN_ERR
1075 "hid-pidff: unknown set_effect report layout\n");
1076 return -ENODEV;
1079 PIDFF_FIND_FIELDS(block_load, PID_BLOCK_LOAD, 0);
1080 if (!pidff->block_load[PID_EFFECT_BLOCK_INDEX].value) {
1081 printk(KERN_ERR
1082 "hid-pidff: unknown pid_block_load report layout\n");
1083 return -ENODEV;
1086 if (PIDFF_FIND_FIELDS(effect_operation, PID_EFFECT_OPERATION, 1)) {
1087 printk(KERN_ERR
1088 "hid-pidff: unknown effect_operation report layout\n");
1089 return -ENODEV;
1092 if (PIDFF_FIND_FIELDS(block_free, PID_BLOCK_FREE, 1)) {
1093 printk(KERN_ERR
1094 "hid-pidff: unknown pid_block_free report layout\n");
1095 return -ENODEV;
1098 if (!PIDFF_FIND_FIELDS(set_envelope, PID_SET_ENVELOPE, 1))
1099 envelope_ok = 1;
1101 if (pidff_find_special_fields(pidff) || pidff_find_effects(pidff, dev))
1102 return -ENODEV;
1104 if (!envelope_ok) {
1105 if (test_and_clear_bit(FF_CONSTANT, dev->ffbit))
1106 printk(KERN_WARNING "hid-pidff: "
1107 "has constant effect but no envelope\n");
1108 if (test_and_clear_bit(FF_RAMP, dev->ffbit))
1109 printk(KERN_WARNING "hid-pidff: "
1110 "has ramp effect but no envelope\n");
1112 if (test_and_clear_bit(FF_PERIODIC, dev->ffbit))
1113 printk(KERN_WARNING "hid-pidff: "
1114 "has periodic effect but no envelope\n");
1117 if (test_bit(FF_CONSTANT, dev->ffbit) &&
1118 PIDFF_FIND_FIELDS(set_constant, PID_SET_CONSTANT, 1)) {
1119 printk(KERN_WARNING
1120 "hid-pidff: unknown constant effect layout\n");
1121 clear_bit(FF_CONSTANT, dev->ffbit);
1124 if (test_bit(FF_RAMP, dev->ffbit) &&
1125 PIDFF_FIND_FIELDS(set_ramp, PID_SET_RAMP, 1)) {
1126 printk(KERN_WARNING "hid-pidff: unknown ramp effect layout\n");
1127 clear_bit(FF_RAMP, dev->ffbit);
1130 if ((test_bit(FF_SPRING, dev->ffbit) ||
1131 test_bit(FF_DAMPER, dev->ffbit) ||
1132 test_bit(FF_FRICTION, dev->ffbit) ||
1133 test_bit(FF_INERTIA, dev->ffbit)) &&
1134 PIDFF_FIND_FIELDS(set_condition, PID_SET_CONDITION, 1)) {
1135 printk(KERN_WARNING
1136 "hid-pidff: unknown condition effect layout\n");
1137 clear_bit(FF_SPRING, dev->ffbit);
1138 clear_bit(FF_DAMPER, dev->ffbit);
1139 clear_bit(FF_FRICTION, dev->ffbit);
1140 clear_bit(FF_INERTIA, dev->ffbit);
1143 if (test_bit(FF_PERIODIC, dev->ffbit) &&
1144 PIDFF_FIND_FIELDS(set_periodic, PID_SET_PERIODIC, 1)) {
1145 printk(KERN_WARNING
1146 "hid-pidff: unknown periodic effect layout\n");
1147 clear_bit(FF_PERIODIC, dev->ffbit);
1150 PIDFF_FIND_FIELDS(pool, PID_POOL, 0);
1152 if (!PIDFF_FIND_FIELDS(device_gain, PID_DEVICE_GAIN, 1))
1153 set_bit(FF_GAIN, dev->ffbit);
1155 return 0;
1159 * Reset the device
1161 static void pidff_reset(struct pidff_device *pidff)
1163 struct hid_device *hid = pidff->hid;
1164 int i = 0;
1166 pidff->device_control->value[0] = pidff->control_id[PID_RESET];
1167 /* We reset twice as sometimes hid_wait_io isn't waiting long enough */
1168 usbhid_submit_report(hid, pidff->reports[PID_DEVICE_CONTROL], USB_DIR_OUT);
1169 usbhid_wait_io(hid);
1170 usbhid_submit_report(hid, pidff->reports[PID_DEVICE_CONTROL], USB_DIR_OUT);
1171 usbhid_wait_io(hid);
1173 pidff->device_control->value[0] =
1174 pidff->control_id[PID_ENABLE_ACTUATORS];
1175 usbhid_submit_report(hid, pidff->reports[PID_DEVICE_CONTROL], USB_DIR_OUT);
1176 usbhid_wait_io(hid);
1178 /* pool report is sometimes messed up, refetch it */
1179 usbhid_submit_report(hid, pidff->reports[PID_POOL], USB_DIR_IN);
1180 usbhid_wait_io(hid);
1182 if (pidff->pool[PID_SIMULTANEOUS_MAX].value) {
1183 int sim_effects = pidff->pool[PID_SIMULTANEOUS_MAX].value[0];
1184 while (sim_effects < 2) {
1185 if (i++ > 20) {
1186 printk(KERN_WARNING "hid-pidff: device reports "
1187 "%d simultaneous effects\n",
1188 sim_effects);
1189 break;
1191 debug("pid_pool requested again");
1192 usbhid_submit_report(hid, pidff->reports[PID_POOL],
1193 USB_DIR_IN);
1194 usbhid_wait_io(hid);
1200 * Test if autocenter modification is using the supported method
1202 static int pidff_check_autocenter(struct pidff_device *pidff,
1203 struct input_dev *dev)
1205 int error;
1208 * Let's find out if autocenter modification is supported
1209 * Specification doesn't specify anything, so we request an
1210 * effect upload and cancel it immediately. If the approved
1211 * effect id was one above the minimum, then we assume the first
1212 * effect id is a built-in spring type effect used for autocenter
1215 error = pidff_request_effect_upload(pidff, 1);
1216 if (error) {
1217 printk(KERN_ERR "hid-pidff: upload request failed\n");
1218 return error;
1221 if (pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0] ==
1222 pidff->block_load[PID_EFFECT_BLOCK_INDEX].field->logical_minimum + 1) {
1223 pidff_autocenter(pidff, 0xffff);
1224 set_bit(FF_AUTOCENTER, dev->ffbit);
1225 } else {
1226 printk(KERN_NOTICE "hid-pidff: "
1227 "device has unknown autocenter control method\n");
1230 pidff_erase_pid(pidff,
1231 pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0]);
1233 return 0;
1238 * Check if the device is PID and initialize it
1240 int hid_pidff_init(struct hid_device *hid)
1242 struct pidff_device *pidff;
1243 struct hid_input *hidinput = list_entry(hid->inputs.next,
1244 struct hid_input, list);
1245 struct input_dev *dev = hidinput->input;
1246 struct ff_device *ff;
1247 int max_effects;
1248 int error;
1250 debug("starting pid init");
1252 if (list_empty(&hid->report_enum[HID_OUTPUT_REPORT].report_list)) {
1253 debug("not a PID device, no output report");
1254 return -ENODEV;
1257 pidff = kzalloc(sizeof(*pidff), GFP_KERNEL);
1258 if (!pidff)
1259 return -ENOMEM;
1261 pidff->hid = hid;
1263 pidff_find_reports(hid, HID_OUTPUT_REPORT, pidff);
1264 pidff_find_reports(hid, HID_FEATURE_REPORT, pidff);
1266 if (!pidff_reports_ok(pidff)) {
1267 debug("reports not ok, aborting");
1268 error = -ENODEV;
1269 goto fail;
1272 error = pidff_init_fields(pidff, dev);
1273 if (error)
1274 goto fail;
1276 pidff_reset(pidff);
1278 if (test_bit(FF_GAIN, dev->ffbit)) {
1279 pidff_set(&pidff->device_gain[PID_DEVICE_GAIN_FIELD], 0xffff);
1280 usbhid_submit_report(pidff->hid, pidff->reports[PID_DEVICE_GAIN],
1281 USB_DIR_OUT);
1284 error = pidff_check_autocenter(pidff, dev);
1285 if (error)
1286 goto fail;
1288 max_effects =
1289 pidff->block_load[PID_EFFECT_BLOCK_INDEX].field->logical_maximum -
1290 pidff->block_load[PID_EFFECT_BLOCK_INDEX].field->logical_minimum +
1292 debug("max effects is %d", max_effects);
1294 if (max_effects > PID_EFFECTS_MAX)
1295 max_effects = PID_EFFECTS_MAX;
1297 if (pidff->pool[PID_SIMULTANEOUS_MAX].value)
1298 debug("max simultaneous effects is %d",
1299 pidff->pool[PID_SIMULTANEOUS_MAX].value[0]);
1301 if (pidff->pool[PID_RAM_POOL_SIZE].value)
1302 debug("device memory size is %d bytes",
1303 pidff->pool[PID_RAM_POOL_SIZE].value[0]);
1305 if (pidff->pool[PID_DEVICE_MANAGED_POOL].value &&
1306 pidff->pool[PID_DEVICE_MANAGED_POOL].value[0] == 0) {
1307 printk(KERN_NOTICE "hid-pidff: "
1308 "device does not support device managed pool\n");
1309 goto fail;
1312 error = input_ff_create(dev, max_effects);
1313 if (error)
1314 goto fail;
1316 ff = dev->ff;
1317 ff->private = pidff;
1318 ff->upload = pidff_upload_effect;
1319 ff->erase = pidff_erase_effect;
1320 ff->set_gain = pidff_set_gain;
1321 ff->set_autocenter = pidff_set_autocenter;
1322 ff->playback = pidff_playback;
1324 printk(KERN_INFO "Force feedback for USB HID PID devices by "
1325 "Anssi Hannula <anssi.hannula@gmail.com>\n");
1327 return 0;
1329 fail:
1330 kfree(pidff);
1331 return error;