blob: 484e3eec2f88be2b740536fdae06cdc2e60dbc65 [file] [log] [blame]
Anssi Hannula224ee882006-07-19 01:40:47 -04001/*
2 * Force feedback driver for USB HID PID compliant devices
3 *
4 * Copyright (c) 2005, 2006 Anssi Hannula <anssi.hannula@gmail.com>
5 */
6
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.
12 *
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.
17 *
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
21 */
22
23/* #define DEBUG */
24
25#define debug(format, arg...) pr_debug("hid-pidff: " format "\n" , ## arg)
26
Anssi Hannula224ee882006-07-19 01:40:47 -040027#include <linux/input.h>
28#include <linux/usb.h>
29
Jiri Kosinadde58452006-12-08 18:40:44 +010030#include <linux/hid.h>
31
32#include "usbhid.h"
Anssi Hannula224ee882006-07-19 01:40:47 -040033
34#define PID_EFFECTS_MAX 64
35
36/* Report usage table used to put reports into an array */
37
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
46
47#define PID_REQUIRED_REPORTS 7
48
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
54static const u8 pidff_reports[] = {
55 0x21, 0x77, 0x7d, 0x7f, 0x89, 0x90, 0x96, 0xab,
56 0x5a, 0x5f, 0x6e, 0x73, 0x74
57};
58
59/* device_control is really 0x95, but 0x96 specified as it is the usage of
60the only field in that report */
61
62/* Value usage tables used to put fields and values into arrays */
63
64#define PID_EFFECT_BLOCK_INDEX 0
65
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
72static const u8 pidff_set_effect[] = {
73 0x22, 0x50, 0x52, 0x53, 0x54, 0x56, 0xa7
74};
75
76#define PID_ATTACK_LEVEL 1
77#define PID_ATTACK_TIME 2
78#define PID_FADE_LEVEL 3
79#define PID_FADE_TIME 4
80static const u8 pidff_set_envelope[] = { 0x22, 0x5b, 0x5c, 0x5d, 0x5e };
81
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
89static const u8 pidff_set_condition[] = {
90 0x22, 0x23, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65
91};
92
93#define PID_MAGNITUDE 1
94#define PID_OFFSET 2
95#define PID_PHASE 3
96#define PID_PERIOD 4
97static const u8 pidff_set_periodic[] = { 0x22, 0x70, 0x6f, 0x71, 0x72 };
98static const u8 pidff_set_constant[] = { 0x22, 0x70 };
99
100#define PID_RAMP_START 1
101#define PID_RAMP_END 2
102static const u8 pidff_set_ramp[] = { 0x22, 0x75, 0x76 };
103
104#define PID_RAM_POOL_AVAILABLE 1
105static const u8 pidff_block_load[] = { 0x22, 0xac };
106
107#define PID_LOOP_COUNT 1
108static const u8 pidff_effect_operation[] = { 0x22, 0x7c };
109
110static const u8 pidff_block_free[] = { 0x22 };
111
112#define PID_DEVICE_GAIN_FIELD 0
113static const u8 pidff_device_gain[] = { 0x7e };
114
115#define PID_RAM_POOL_SIZE 0
116#define PID_SIMULTANEOUS_MAX 1
117#define PID_DEVICE_MANAGED_POOL 2
118static const u8 pidff_pool[] = { 0x80, 0x83, 0xa9 };
119
120/* Special field key tables used to put special field keys into arrays */
121
122#define PID_ENABLE_ACTUATORS 0
123#define PID_RESET 1
124static const u8 pidff_device_control[] = { 0x97, 0x9a };
125
126#define PID_CONSTANT 0
127#define PID_RAMP 1
128#define PID_SQUARE 2
129#define PID_SINE 3
130#define PID_TRIANGLE 4
131#define PID_SAW_UP 5
132#define PID_SAW_DOWN 6
133#define PID_SPRING 7
134#define PID_DAMPER 8
135#define PID_INERTIA 9
136#define PID_FRICTION 10
137static const u8 pidff_effect_types[] = {
138 0x26, 0x27, 0x30, 0x31, 0x32, 0x33, 0x34,
139 0x40, 0x41, 0x42, 0x43
140};
141
142#define PID_BLOCK_LOAD_SUCCESS 0
143#define PID_BLOCK_LOAD_FULL 1
144static const u8 pidff_block_load_status[] = { 0x8c, 0x8d };
145
146#define PID_EFFECT_START 0
147#define PID_EFFECT_STOP 1
148static const u8 pidff_effect_operation_status[] = { 0x79, 0x7b };
149
150struct pidff_usage {
151 struct hid_field *field;
152 s32 *value;
153};
154
155struct pidff_device {
156 struct hid_device *hid;
157
158 struct hid_report *reports[sizeof(pidff_reports)];
159
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)];
166
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)];
172
173 /* Special field is a field that is not composed of
174 usage<->value pairs that pidff_usage values are */
175
176 /* Special field in create_new_effect */
177 struct hid_field *create_new_effect_type;
178
179 /* Special fields in set_effect */
180 struct hid_field *set_effect_type;
181 struct hid_field *effect_direction;
182
183 /* Special field in device_control */
184 struct hid_field *device_control;
185
186 /* Special field in block_load */
187 struct hid_field *block_load_status;
188
189 /* Special field in effect_operation */
190 struct hid_field *effect_operation_status;
191
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)];
196
197 int pid_id[PID_EFFECTS_MAX];
198};
199
200/*
201 * Scale an unsigned value with range 0..max for the given field
202 */
203static int pidff_rescale(int i, int max, struct hid_field *field)
204{
205 return i * (field->logical_maximum - field->logical_minimum) / max +
206 field->logical_minimum;
207}
208
209/*
210 * Scale a signed value in range -0x8000..0x7fff for the given field
211 */
212static int pidff_rescale_signed(int i, struct hid_field *field)
213{
214 return i == 0 ? 0 : i >
215 0 ? i * field->logical_maximum / 0x7fff : i *
216 field->logical_minimum / -0x8000;
217}
218
219static void pidff_set(struct pidff_usage *usage, u16 value)
220{
221 usage->value[0] = pidff_rescale(value, 0xffff, usage->field);
222 debug("calculated from %d to %d", value, usage->value[0]);
223}
224
225static void pidff_set_signed(struct pidff_usage *usage, s16 value)
226{
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);
236 }
237 debug("calculated from %d to %d", value, usage->value[0]);
238}
239
240/*
241 * Send envelope report to the device
242 */
243static void pidff_set_envelope_report(struct pidff_device *pidff,
244 struct ff_envelope *envelope)
245{
246 pidff->set_envelope[PID_EFFECT_BLOCK_INDEX].value[0] =
247 pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0];
248
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);
257
258 pidff->set_envelope[PID_ATTACK_TIME].value[0] = envelope->attack_length;
259 pidff->set_envelope[PID_FADE_TIME].value[0] = envelope->fade_length;
260
261 debug("attack %u => %d", envelope->attack_level,
262 pidff->set_envelope[PID_ATTACK_LEVEL].value[0]);
263
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100264 usbhid_submit_report(pidff->hid, pidff->reports[PID_SET_ENVELOPE],
Anssi Hannula224ee882006-07-19 01:40:47 -0400265 USB_DIR_OUT);
266}
267
268/*
269 * Test if the new envelope differs from old one
270 */
271static int pidff_needs_set_envelope(struct ff_envelope *envelope,
272 struct ff_envelope *old)
273{
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;
278}
279
280/*
281 * Send constant force report to the device
282 */
283static void pidff_set_constant_force_report(struct pidff_device *pidff,
284 struct ff_effect *effect)
285{
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);
290
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100291 usbhid_submit_report(pidff->hid, pidff->reports[PID_SET_CONSTANT],
Anssi Hannula224ee882006-07-19 01:40:47 -0400292 USB_DIR_OUT);
293}
294
295/*
296 * Test if the constant parameters have changed between effects
297 */
298static int pidff_needs_set_constant(struct ff_effect *effect,
299 struct ff_effect *old)
300{
301 return effect->u.constant.level != old->u.constant.level;
302}
303
304/*
305 * Send set effect report to the device
306 */
307static void pidff_set_effect_report(struct pidff_device *pidff,
308 struct ff_effect *effect)
309{
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;
325
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100326 usbhid_submit_report(pidff->hid, pidff->reports[PID_SET_EFFECT],
Anssi Hannula224ee882006-07-19 01:40:47 -0400327 USB_DIR_OUT);
328}
329
330/*
331 * Test if the values used in set_effect have changed
332 */
333static int pidff_needs_set_effect(struct ff_effect *effect,
334 struct ff_effect *old)
335{
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;
341}
342
343/*
344 * Send periodic effect report to the device
345 */
346static void pidff_set_periodic_report(struct pidff_device *pidff,
347 struct ff_effect *effect)
348{
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;
357
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100358 usbhid_submit_report(pidff->hid, pidff->reports[PID_SET_PERIODIC],
Anssi Hannula224ee882006-07-19 01:40:47 -0400359 USB_DIR_OUT);
360
361}
362
363/*
364 * Test if periodic effect parameters have changed
365 */
366static int pidff_needs_set_periodic(struct ff_effect *effect,
367 struct ff_effect *old)
368{
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;
373}
374
375/*
376 * Send condition effect reports to the device
377 */
378static void pidff_set_condition_report(struct pidff_device *pidff,
379 struct ff_effect *effect)
380{
381 int i;
382
383 pidff->set_condition[PID_EFFECT_BLOCK_INDEX].value[0] =
384 pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0];
385
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);
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100400 usbhid_submit_report(pidff->hid, pidff->reports[PID_SET_CONDITION],
Anssi Hannula224ee882006-07-19 01:40:47 -0400401 USB_DIR_OUT);
402 }
403}
404
405/*
406 * Test if condition effect parameters have changed
407 */
408static int pidff_needs_set_condition(struct ff_effect *effect,
409 struct ff_effect *old)
410{
411 int i;
412 int ret = 0;
413
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];
417
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;
424 }
425
426 return ret;
427}
428
429/*
430 * Send ramp force report to the device
431 */
432static void pidff_set_ramp_force_report(struct pidff_device *pidff,
433 struct ff_effect *effect)
434{
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);
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100441 usbhid_submit_report(pidff->hid, pidff->reports[PID_SET_RAMP],
Anssi Hannula224ee882006-07-19 01:40:47 -0400442 USB_DIR_OUT);
443}
444
445/*
446 * Test if ramp force parameters have changed
447 */
448static int pidff_needs_set_ramp(struct ff_effect *effect, struct ff_effect *old)
449{
450 return effect->u.ramp.start_level != old->u.ramp.start_level ||
451 effect->u.ramp.end_level != old->u.ramp.end_level;
452}
453
454/*
455 * Send a request for effect upload to the device
456 *
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.
460 */
461static int pidff_request_effect_upload(struct pidff_device *pidff, int efnum)
462{
463 int j;
464
465 pidff->create_new_effect_type->value[0] = efnum;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100466 usbhid_submit_report(pidff->hid, pidff->reports[PID_CREATE_NEW_EFFECT],
Anssi Hannula224ee882006-07-19 01:40:47 -0400467 USB_DIR_OUT);
468 debug("create_new_effect sent, type: %d", efnum);
469
470 pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0] = 0;
471 pidff->block_load_status->value[0] = 0;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100472 usbhid_wait_io(pidff->hid);
Anssi Hannula224ee882006-07-19 01:40:47 -0400473
474 for (j = 0; j < 60; j++) {
475 debug("pid_block_load requested");
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100476 usbhid_submit_report(pidff->hid, pidff->reports[PID_BLOCK_LOAD],
Anssi Hannula224ee882006-07-19 01:40:47 -0400477 USB_DIR_IN);
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100478 usbhid_wait_io(pidff->hid);
Anssi Hannula224ee882006-07-19 01:40:47 -0400479 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);
484 return 0;
485 }
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);
491 return -ENOSPC;
492 }
493 }
494 printk(KERN_ERR "hid-pidff: pid_block_load failed 60 times\n");
495 return -EIO;
496}
497
498/*
499 * Play the effect with PID id n times
500 */
501static void pidff_playback_pid(struct pidff_device *pidff, int pid_id, int n)
502{
503 pidff->effect_operation[PID_EFFECT_BLOCK_INDEX].value[0] = pid_id;
504
505 if (n == 0) {
506 pidff->effect_operation_status->value[0] =
507 pidff->operation_id[PID_EFFECT_STOP];
508 } else {
509 pidff->effect_operation_status->value[0] =
510 pidff->operation_id[PID_EFFECT_START];
511 pidff->effect_operation[PID_LOOP_COUNT].value[0] = n;
512 }
513
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100514 usbhid_submit_report(pidff->hid, pidff->reports[PID_EFFECT_OPERATION],
Anssi Hannula224ee882006-07-19 01:40:47 -0400515 USB_DIR_OUT);
516}
517
518/**
519 * Play the effect with effect id @effect_id for @value times
520 */
521static int pidff_playback(struct input_dev *dev, int effect_id, int value)
522{
523 struct pidff_device *pidff = dev->ff->private;
524
525 pidff_playback_pid(pidff, pidff->pid_id[effect_id], value);
526
527 return 0;
528}
529
530/*
531 * Erase effect with PID id
532 */
533static void pidff_erase_pid(struct pidff_device *pidff, int pid_id)
534{
535 pidff->block_free[PID_EFFECT_BLOCK_INDEX].value[0] = pid_id;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100536 usbhid_submit_report(pidff->hid, pidff->reports[PID_BLOCK_FREE],
Anssi Hannula224ee882006-07-19 01:40:47 -0400537 USB_DIR_OUT);
538}
539
540/*
541 * Stop and erase effect with effect_id
542 */
543static int pidff_erase_effect(struct input_dev *dev, int effect_id)
544{
545 struct pidff_device *pidff = dev->ff->private;
546 int pid_id = pidff->pid_id[effect_id];
547
548 debug("starting to erase %d/%d", effect_id, pidff->pid_id[effect_id]);
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200549 /* Wait for the queue to clear. We do not want a full fifo to
550 prevent the effect removal. */
551 usbhid_wait_io(pidff->hid);
Anssi Hannula224ee882006-07-19 01:40:47 -0400552 pidff_playback_pid(pidff, pid_id, 0);
553 pidff_erase_pid(pidff, pid_id);
554
555 return 0;
556}
557
558/*
559 * Effect upload handler
560 */
561static int pidff_upload_effect(struct input_dev *dev, struct ff_effect *effect,
562 struct ff_effect *old)
563{
564 struct pidff_device *pidff = dev->ff->private;
565 int type_id;
566 int error;
567
568 switch (effect->type) {
569 case FF_CONSTANT:
570 if (!old) {
571 error = pidff_request_effect_upload(pidff,
572 pidff->type_id[PID_CONSTANT]);
573 if (error)
574 return error;
575 }
576 if (!old || pidff_needs_set_effect(effect, old))
577 pidff_set_effect_report(pidff, effect);
578 if (!old || pidff_needs_set_constant(effect, old))
579 pidff_set_constant_force_report(pidff, effect);
580 if (!old ||
581 pidff_needs_set_envelope(&effect->u.constant.envelope,
582 &old->u.constant.envelope))
583 pidff_set_envelope_report(pidff,
584 &effect->u.constant.envelope);
585 break;
586
587 case FF_PERIODIC:
588 if (!old) {
589 switch (effect->u.periodic.waveform) {
590 case FF_SQUARE:
591 type_id = PID_SQUARE;
592 break;
593 case FF_TRIANGLE:
594 type_id = PID_TRIANGLE;
595 break;
596 case FF_SINE:
597 type_id = PID_SINE;
598 break;
599 case FF_SAW_UP:
600 type_id = PID_SAW_UP;
601 break;
602 case FF_SAW_DOWN:
603 type_id = PID_SAW_DOWN;
604 break;
605 default:
606 printk(KERN_ERR
607 "hid-pidff: invalid waveform\n");
608 return -EINVAL;
609 }
610
611 error = pidff_request_effect_upload(pidff,
612 pidff->type_id[type_id]);
613 if (error)
614 return error;
615 }
616 if (!old || pidff_needs_set_effect(effect, old))
617 pidff_set_effect_report(pidff, effect);
618 if (!old || pidff_needs_set_periodic(effect, old))
619 pidff_set_periodic_report(pidff, effect);
620 if (!old ||
621 pidff_needs_set_envelope(&effect->u.periodic.envelope,
622 &old->u.periodic.envelope))
623 pidff_set_envelope_report(pidff,
624 &effect->u.periodic.envelope);
625 break;
626
627 case FF_RAMP:
628 if (!old) {
629 error = pidff_request_effect_upload(pidff,
630 pidff->type_id[PID_RAMP]);
631 if (error)
632 return error;
633 }
634 if (!old || pidff_needs_set_effect(effect, old))
635 pidff_set_effect_report(pidff, effect);
636 if (!old || pidff_needs_set_ramp(effect, old))
637 pidff_set_ramp_force_report(pidff, effect);
638 if (!old ||
639 pidff_needs_set_envelope(&effect->u.ramp.envelope,
640 &old->u.ramp.envelope))
641 pidff_set_envelope_report(pidff,
642 &effect->u.ramp.envelope);
643 break;
644
645 case FF_SPRING:
646 if (!old) {
647 error = pidff_request_effect_upload(pidff,
648 pidff->type_id[PID_SPRING]);
649 if (error)
650 return error;
651 }
652 if (!old || pidff_needs_set_effect(effect, old))
653 pidff_set_effect_report(pidff, effect);
654 if (!old || pidff_needs_set_condition(effect, old))
655 pidff_set_condition_report(pidff, effect);
656 break;
657
658 case FF_FRICTION:
659 if (!old) {
660 error = pidff_request_effect_upload(pidff,
661 pidff->type_id[PID_FRICTION]);
662 if (error)
663 return error;
664 }
665 if (!old || pidff_needs_set_effect(effect, old))
666 pidff_set_effect_report(pidff, effect);
667 if (!old || pidff_needs_set_condition(effect, old))
668 pidff_set_condition_report(pidff, effect);
669 break;
670
671 case FF_DAMPER:
672 if (!old) {
673 error = pidff_request_effect_upload(pidff,
674 pidff->type_id[PID_DAMPER]);
675 if (error)
676 return error;
677 }
678 if (!old || pidff_needs_set_effect(effect, old))
679 pidff_set_effect_report(pidff, effect);
680 if (!old || pidff_needs_set_condition(effect, old))
681 pidff_set_condition_report(pidff, effect);
682 break;
683
684 case FF_INERTIA:
685 if (!old) {
686 error = pidff_request_effect_upload(pidff,
687 pidff->type_id[PID_INERTIA]);
688 if (error)
689 return error;
690 }
691 if (!old || pidff_needs_set_effect(effect, old))
692 pidff_set_effect_report(pidff, effect);
693 if (!old || pidff_needs_set_condition(effect, old))
694 pidff_set_condition_report(pidff, effect);
695 break;
696
697 default:
698 printk(KERN_ERR "hid-pidff: invalid type\n");
699 return -EINVAL;
700 }
701
702 if (!old)
703 pidff->pid_id[effect->id] =
704 pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0];
705
706 debug("uploaded");
707
708 return 0;
709}
710
711/*
712 * set_gain() handler
713 */
714static void pidff_set_gain(struct input_dev *dev, u16 gain)
715{
716 struct pidff_device *pidff = dev->ff->private;
717
718 pidff_set(&pidff->device_gain[PID_DEVICE_GAIN_FIELD], gain);
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100719 usbhid_submit_report(pidff->hid, pidff->reports[PID_DEVICE_GAIN],
Anssi Hannula224ee882006-07-19 01:40:47 -0400720 USB_DIR_OUT);
721}
722
723static void pidff_autocenter(struct pidff_device *pidff, u16 magnitude)
724{
725 struct hid_field *field =
726 pidff->block_load[PID_EFFECT_BLOCK_INDEX].field;
727
728 if (!magnitude) {
729 pidff_playback_pid(pidff, field->logical_minimum, 0);
730 return;
731 }
732
733 pidff_playback_pid(pidff, field->logical_minimum, 1);
734
735 pidff->set_effect[PID_EFFECT_BLOCK_INDEX].value[0] =
736 pidff->block_load[PID_EFFECT_BLOCK_INDEX].field->logical_minimum;
737 pidff->set_effect_type->value[0] = pidff->type_id[PID_SPRING];
738 pidff->set_effect[PID_DURATION].value[0] = 0;
739 pidff->set_effect[PID_TRIGGER_BUTTON].value[0] = 0;
740 pidff->set_effect[PID_TRIGGER_REPEAT_INT].value[0] = 0;
741 pidff_set(&pidff->set_effect[PID_GAIN], magnitude);
Diogo Kastrup816cbfd2007-06-19 14:16:15 +0200742 pidff->set_effect[PID_DIRECTION_ENABLE].value[0] = 1;
Anssi Hannula224ee882006-07-19 01:40:47 -0400743 pidff->set_effect[PID_START_DELAY].value[0] = 0;
744
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100745 usbhid_submit_report(pidff->hid, pidff->reports[PID_SET_EFFECT],
Anssi Hannula224ee882006-07-19 01:40:47 -0400746 USB_DIR_OUT);
747}
748
749/*
750 * pidff_set_autocenter() handler
751 */
752static void pidff_set_autocenter(struct input_dev *dev, u16 magnitude)
753{
754 struct pidff_device *pidff = dev->ff->private;
755
756 pidff_autocenter(pidff, magnitude);
757}
758
759/*
760 * Find fields from a report and fill a pidff_usage
761 */
762static int pidff_find_fields(struct pidff_usage *usage, const u8 *table,
763 struct hid_report *report, int count, int strict)
764{
765 int i, j, k, found;
766
767 for (k = 0; k < count; k++) {
768 found = 0;
769 for (i = 0; i < report->maxfield; i++) {
770 if (report->field[i]->maxusage !=
771 report->field[i]->report_count) {
772 debug("maxusage and report_count do not match, "
773 "skipping");
774 continue;
775 }
776 for (j = 0; j < report->field[i]->maxusage; j++) {
777 if (report->field[i]->usage[j].hid ==
778 (HID_UP_PID | table[k])) {
779 debug("found %d at %d->%d", k, i, j);
780 usage[k].field = report->field[i];
781 usage[k].value =
782 &report->field[i]->value[j];
783 found = 1;
784 break;
785 }
786 }
787 if (found)
788 break;
789 }
790 if (!found && strict) {
791 debug("failed to locate %d", k);
792 return -1;
793 }
794 }
795 return 0;
796}
797
798/*
799 * Return index into pidff_reports for the given usage
800 */
801static int pidff_check_usage(int usage)
802{
803 int i;
804
805 for (i = 0; i < sizeof(pidff_reports); i++)
806 if (usage == (HID_UP_PID | pidff_reports[i]))
807 return i;
808
809 return -1;
810}
811
812/*
813 * Find the reports and fill pidff->reports[]
814 * report_type specifies either OUTPUT or FEATURE reports
815 */
816static void pidff_find_reports(struct hid_device *hid, int report_type,
817 struct pidff_device *pidff)
818{
819 struct hid_report *report;
820 int i, ret;
821
822 list_for_each_entry(report,
823 &hid->report_enum[report_type].report_list, list) {
824 if (report->maxfield < 1)
825 continue;
826 ret = pidff_check_usage(report->field[0]->logical);
827 if (ret != -1) {
828 debug("found usage 0x%02x from field->logical",
829 pidff_reports[ret]);
830 pidff->reports[ret] = report;
831 continue;
832 }
833
834 /*
835 * Sometimes logical collections are stacked to indicate
836 * different usages for the report and the field, in which
837 * case we want the usage of the parent. However, Linux HID
838 * implementation hides this fact, so we have to dig it up
839 * ourselves
840 */
841 i = report->field[0]->usage[0].collection_index;
842 if (i <= 0 ||
843 hid->collection[i - 1].type != HID_COLLECTION_LOGICAL)
844 continue;
845 ret = pidff_check_usage(hid->collection[i - 1].usage);
846 if (ret != -1 && !pidff->reports[ret]) {
847 debug("found usage 0x%02x from collection array",
848 pidff_reports[ret]);
849 pidff->reports[ret] = report;
850 }
851 }
852}
853
854/*
855 * Test if the required reports have been found
856 */
857static int pidff_reports_ok(struct pidff_device *pidff)
858{
859 int i;
860
861 for (i = 0; i <= PID_REQUIRED_REPORTS; i++) {
862 if (!pidff->reports[i]) {
863 debug("%d missing", i);
864 return 0;
865 }
866 }
867
868 return 1;
869}
870
871/*
872 * Find a field with a specific usage within a report
873 */
874static struct hid_field *pidff_find_special_field(struct hid_report *report,
875 int usage, int enforce_min)
876{
877 int i;
878
879 for (i = 0; i < report->maxfield; i++) {
880 if (report->field[i]->logical == (HID_UP_PID | usage) &&
881 report->field[i]->report_count > 0) {
882 if (!enforce_min ||
883 report->field[i]->logical_minimum == 1)
884 return report->field[i];
885 else {
886 printk(KERN_ERR "hid-pidff: logical_minimum "
887 "is not 1 as it should be\n");
888 return NULL;
889 }
890 }
891 }
892 return NULL;
893}
894
895/*
896 * Fill a pidff->*_id struct table
897 */
898static int pidff_find_special_keys(int *keys, struct hid_field *fld,
899 const u8 *usagetable, int count)
900{
901
902 int i, j;
903 int found = 0;
904
905 for (i = 0; i < count; i++) {
906 for (j = 0; j < fld->maxusage; j++) {
907 if (fld->usage[j].hid == (HID_UP_PID | usagetable[i])) {
908 keys[i] = j + 1;
909 found++;
910 break;
911 }
912 }
913 }
914 return found;
915}
916
917#define PIDFF_FIND_SPECIAL_KEYS(keys, field, name) \
918 pidff_find_special_keys(pidff->keys, pidff->field, pidff_ ## name, \
919 sizeof(pidff_ ## name))
920
921/*
922 * Find and check the special fields
923 */
924static int pidff_find_special_fields(struct pidff_device *pidff)
925{
926 debug("finding special fields");
927
928 pidff->create_new_effect_type =
929 pidff_find_special_field(pidff->reports[PID_CREATE_NEW_EFFECT],
930 0x25, 1);
931 pidff->set_effect_type =
932 pidff_find_special_field(pidff->reports[PID_SET_EFFECT],
933 0x25, 1);
934 pidff->effect_direction =
935 pidff_find_special_field(pidff->reports[PID_SET_EFFECT],
936 0x57, 0);
937 pidff->device_control =
938 pidff_find_special_field(pidff->reports[PID_DEVICE_CONTROL],
939 0x96, 1);
940 pidff->block_load_status =
941 pidff_find_special_field(pidff->reports[PID_BLOCK_LOAD],
942 0x8b, 1);
943 pidff->effect_operation_status =
944 pidff_find_special_field(pidff->reports[PID_EFFECT_OPERATION],
945 0x78, 1);
946
947 debug("search done");
948
949 if (!pidff->create_new_effect_type || !pidff->set_effect_type) {
950 printk(KERN_ERR "hid-pidff: effect lists not found\n");
951 return -1;
952 }
953
954 if (!pidff->effect_direction) {
955 printk(KERN_ERR "hid-pidff: direction field not found\n");
956 return -1;
957 }
958
959 if (!pidff->device_control) {
960 printk(KERN_ERR "hid-pidff: device control field not found\n");
961 return -1;
962 }
963
964 if (!pidff->block_load_status) {
965 printk(KERN_ERR
966 "hid-pidff: block load status field not found\n");
967 return -1;
968 }
969
970 if (!pidff->effect_operation_status) {
971 printk(KERN_ERR
972 "hid-pidff: effect operation field not found\n");
973 return -1;
974 }
975
976 pidff_find_special_keys(pidff->control_id, pidff->device_control,
977 pidff_device_control,
978 sizeof(pidff_device_control));
979
980 PIDFF_FIND_SPECIAL_KEYS(control_id, device_control, device_control);
981
982 if (!PIDFF_FIND_SPECIAL_KEYS(type_id, create_new_effect_type,
983 effect_types)) {
984 printk(KERN_ERR "hid-pidff: no effect types found\n");
985 return -1;
986 }
987
988 if (PIDFF_FIND_SPECIAL_KEYS(status_id, block_load_status,
989 block_load_status) !=
990 sizeof(pidff_block_load_status)) {
991 printk(KERN_ERR
992 "hidpidff: block load status identifiers not found\n");
993 return -1;
994 }
995
996 if (PIDFF_FIND_SPECIAL_KEYS(operation_id, effect_operation_status,
997 effect_operation_status) !=
998 sizeof(pidff_effect_operation_status)) {
999 printk(KERN_ERR
1000 "hidpidff: effect operation identifiers not found\n");
1001 return -1;
1002 }
1003
1004 return 0;
1005}
1006
1007/**
1008 * Find the implemented effect types
1009 */
1010static int pidff_find_effects(struct pidff_device *pidff,
1011 struct input_dev *dev)
1012{
1013 int i;
1014
1015 for (i = 0; i < sizeof(pidff_effect_types); i++) {
1016 int pidff_type = pidff->type_id[i];
1017 if (pidff->set_effect_type->usage[pidff_type].hid !=
1018 pidff->create_new_effect_type->usage[pidff_type].hid) {
1019 printk(KERN_ERR "hid-pidff: "
1020 "effect type number %d is invalid\n", i);
1021 return -1;
1022 }
1023 }
1024
1025 if (pidff->type_id[PID_CONSTANT])
1026 set_bit(FF_CONSTANT, dev->ffbit);
1027 if (pidff->type_id[PID_RAMP])
1028 set_bit(FF_RAMP, dev->ffbit);
1029 if (pidff->type_id[PID_SQUARE]) {
1030 set_bit(FF_SQUARE, dev->ffbit);
1031 set_bit(FF_PERIODIC, dev->ffbit);
1032 }
1033 if (pidff->type_id[PID_SINE]) {
1034 set_bit(FF_SINE, dev->ffbit);
1035 set_bit(FF_PERIODIC, dev->ffbit);
1036 }
1037 if (pidff->type_id[PID_TRIANGLE]) {
1038 set_bit(FF_TRIANGLE, dev->ffbit);
1039 set_bit(FF_PERIODIC, dev->ffbit);
1040 }
1041 if (pidff->type_id[PID_SAW_UP]) {
1042 set_bit(FF_SAW_UP, dev->ffbit);
1043 set_bit(FF_PERIODIC, dev->ffbit);
1044 }
1045 if (pidff->type_id[PID_SAW_DOWN]) {
1046 set_bit(FF_SAW_DOWN, dev->ffbit);
1047 set_bit(FF_PERIODIC, dev->ffbit);
1048 }
1049 if (pidff->type_id[PID_SPRING])
1050 set_bit(FF_SPRING, dev->ffbit);
1051 if (pidff->type_id[PID_DAMPER])
1052 set_bit(FF_DAMPER, dev->ffbit);
1053 if (pidff->type_id[PID_INERTIA])
1054 set_bit(FF_INERTIA, dev->ffbit);
1055 if (pidff->type_id[PID_FRICTION])
1056 set_bit(FF_FRICTION, dev->ffbit);
1057
1058 return 0;
1059
1060}
1061
1062#define PIDFF_FIND_FIELDS(name, report, strict) \
1063 pidff_find_fields(pidff->name, pidff_ ## name, \
1064 pidff->reports[report], \
1065 sizeof(pidff_ ## name), strict)
1066
1067/*
1068 * Fill and check the pidff_usages
1069 */
1070static int pidff_init_fields(struct pidff_device *pidff, struct input_dev *dev)
1071{
1072 int envelope_ok = 0;
1073
1074 if (PIDFF_FIND_FIELDS(set_effect, PID_SET_EFFECT, 1)) {
1075 printk(KERN_ERR
1076 "hid-pidff: unknown set_effect report layout\n");
1077 return -ENODEV;
1078 }
1079
1080 PIDFF_FIND_FIELDS(block_load, PID_BLOCK_LOAD, 0);
1081 if (!pidff->block_load[PID_EFFECT_BLOCK_INDEX].value) {
1082 printk(KERN_ERR
1083 "hid-pidff: unknown pid_block_load report layout\n");
1084 return -ENODEV;
1085 }
1086
1087 if (PIDFF_FIND_FIELDS(effect_operation, PID_EFFECT_OPERATION, 1)) {
1088 printk(KERN_ERR
1089 "hid-pidff: unknown effect_operation report layout\n");
1090 return -ENODEV;
1091 }
1092
1093 if (PIDFF_FIND_FIELDS(block_free, PID_BLOCK_FREE, 1)) {
1094 printk(KERN_ERR
1095 "hid-pidff: unknown pid_block_free report layout\n");
1096 return -ENODEV;
1097 }
1098
1099 if (!PIDFF_FIND_FIELDS(set_envelope, PID_SET_ENVELOPE, 1))
1100 envelope_ok = 1;
1101
1102 if (pidff_find_special_fields(pidff) || pidff_find_effects(pidff, dev))
1103 return -ENODEV;
1104
1105 if (!envelope_ok) {
1106 if (test_and_clear_bit(FF_CONSTANT, dev->ffbit))
1107 printk(KERN_WARNING "hid-pidff: "
1108 "has constant effect but no envelope\n");
1109 if (test_and_clear_bit(FF_RAMP, dev->ffbit))
1110 printk(KERN_WARNING "hid-pidff: "
1111 "has ramp effect but no envelope\n");
1112
1113 if (test_and_clear_bit(FF_PERIODIC, dev->ffbit))
1114 printk(KERN_WARNING "hid-pidff: "
1115 "has periodic effect but no envelope\n");
1116 }
1117
1118 if (test_bit(FF_CONSTANT, dev->ffbit) &&
1119 PIDFF_FIND_FIELDS(set_constant, PID_SET_CONSTANT, 1)) {
1120 printk(KERN_WARNING
1121 "hid-pidff: unknown constant effect layout\n");
1122 clear_bit(FF_CONSTANT, dev->ffbit);
1123 }
1124
1125 if (test_bit(FF_RAMP, dev->ffbit) &&
1126 PIDFF_FIND_FIELDS(set_ramp, PID_SET_RAMP, 1)) {
1127 printk(KERN_WARNING "hid-pidff: unknown ramp effect layout\n");
1128 clear_bit(FF_RAMP, dev->ffbit);
1129 }
1130
1131 if ((test_bit(FF_SPRING, dev->ffbit) ||
1132 test_bit(FF_DAMPER, dev->ffbit) ||
1133 test_bit(FF_FRICTION, dev->ffbit) ||
1134 test_bit(FF_INERTIA, dev->ffbit)) &&
1135 PIDFF_FIND_FIELDS(set_condition, PID_SET_CONDITION, 1)) {
1136 printk(KERN_WARNING
1137 "hid-pidff: unknown condition effect layout\n");
1138 clear_bit(FF_SPRING, dev->ffbit);
1139 clear_bit(FF_DAMPER, dev->ffbit);
1140 clear_bit(FF_FRICTION, dev->ffbit);
1141 clear_bit(FF_INERTIA, dev->ffbit);
1142 }
1143
1144 if (test_bit(FF_PERIODIC, dev->ffbit) &&
1145 PIDFF_FIND_FIELDS(set_periodic, PID_SET_PERIODIC, 1)) {
1146 printk(KERN_WARNING
1147 "hid-pidff: unknown periodic effect layout\n");
1148 clear_bit(FF_PERIODIC, dev->ffbit);
1149 }
1150
1151 PIDFF_FIND_FIELDS(pool, PID_POOL, 0);
1152
1153 if (!PIDFF_FIND_FIELDS(device_gain, PID_DEVICE_GAIN, 1))
1154 set_bit(FF_GAIN, dev->ffbit);
1155
1156 return 0;
1157}
1158
1159/*
1160 * Reset the device
1161 */
1162static void pidff_reset(struct pidff_device *pidff)
1163{
1164 struct hid_device *hid = pidff->hid;
1165 int i = 0;
1166
1167 pidff->device_control->value[0] = pidff->control_id[PID_RESET];
1168 /* We reset twice as sometimes hid_wait_io isn't waiting long enough */
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001169 usbhid_submit_report(hid, pidff->reports[PID_DEVICE_CONTROL], USB_DIR_OUT);
1170 usbhid_wait_io(hid);
1171 usbhid_submit_report(hid, pidff->reports[PID_DEVICE_CONTROL], USB_DIR_OUT);
1172 usbhid_wait_io(hid);
Anssi Hannula224ee882006-07-19 01:40:47 -04001173
1174 pidff->device_control->value[0] =
1175 pidff->control_id[PID_ENABLE_ACTUATORS];
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001176 usbhid_submit_report(hid, pidff->reports[PID_DEVICE_CONTROL], USB_DIR_OUT);
1177 usbhid_wait_io(hid);
Anssi Hannula224ee882006-07-19 01:40:47 -04001178
1179 /* pool report is sometimes messed up, refetch it */
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001180 usbhid_submit_report(hid, pidff->reports[PID_POOL], USB_DIR_IN);
1181 usbhid_wait_io(hid);
Anssi Hannula224ee882006-07-19 01:40:47 -04001182
1183 if (pidff->pool[PID_SIMULTANEOUS_MAX].value) {
1184 int sim_effects = pidff->pool[PID_SIMULTANEOUS_MAX].value[0];
1185 while (sim_effects < 2) {
1186 if (i++ > 20) {
1187 printk(KERN_WARNING "hid-pidff: device reports "
1188 "%d simultaneous effects\n",
1189 sim_effects);
1190 break;
1191 }
1192 debug("pid_pool requested again");
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001193 usbhid_submit_report(hid, pidff->reports[PID_POOL],
Anssi Hannula224ee882006-07-19 01:40:47 -04001194 USB_DIR_IN);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001195 usbhid_wait_io(hid);
Anssi Hannula224ee882006-07-19 01:40:47 -04001196 }
1197 }
1198}
1199
1200/*
1201 * Test if autocenter modification is using the supported method
1202 */
1203static int pidff_check_autocenter(struct pidff_device *pidff,
1204 struct input_dev *dev)
1205{
1206 int error;
1207
1208 /*
1209 * Let's find out if autocenter modification is supported
1210 * Specification doesn't specify anything, so we request an
1211 * effect upload and cancel it immediately. If the approved
1212 * effect id was one above the minimum, then we assume the first
1213 * effect id is a built-in spring type effect used for autocenter
1214 */
1215
1216 error = pidff_request_effect_upload(pidff, 1);
1217 if (error) {
1218 printk(KERN_ERR "hid-pidff: upload request failed\n");
1219 return error;
1220 }
1221
1222 if (pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0] ==
1223 pidff->block_load[PID_EFFECT_BLOCK_INDEX].field->logical_minimum + 1) {
1224 pidff_autocenter(pidff, 0xffff);
1225 set_bit(FF_AUTOCENTER, dev->ffbit);
1226 } else {
1227 printk(KERN_NOTICE "hid-pidff: "
1228 "device has unknown autocenter control method\n");
1229 }
1230
1231 pidff_erase_pid(pidff,
1232 pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0]);
1233
1234 return 0;
1235
1236}
1237
1238/*
1239 * Check if the device is PID and initialize it
1240 */
1241int hid_pidff_init(struct hid_device *hid)
1242{
1243 struct pidff_device *pidff;
1244 struct hid_input *hidinput = list_entry(hid->inputs.next,
1245 struct hid_input, list);
1246 struct input_dev *dev = hidinput->input;
1247 struct ff_device *ff;
1248 int max_effects;
1249 int error;
1250
1251 debug("starting pid init");
1252
1253 if (list_empty(&hid->report_enum[HID_OUTPUT_REPORT].report_list)) {
1254 debug("not a PID device, no output report");
1255 return -ENODEV;
1256 }
1257
1258 pidff = kzalloc(sizeof(*pidff), GFP_KERNEL);
1259 if (!pidff)
1260 return -ENOMEM;
1261
1262 pidff->hid = hid;
1263
1264 pidff_find_reports(hid, HID_OUTPUT_REPORT, pidff);
1265 pidff_find_reports(hid, HID_FEATURE_REPORT, pidff);
1266
1267 if (!pidff_reports_ok(pidff)) {
1268 debug("reports not ok, aborting");
1269 error = -ENODEV;
1270 goto fail;
1271 }
1272
1273 error = pidff_init_fields(pidff, dev);
1274 if (error)
1275 goto fail;
1276
1277 pidff_reset(pidff);
1278
1279 if (test_bit(FF_GAIN, dev->ffbit)) {
1280 pidff_set(&pidff->device_gain[PID_DEVICE_GAIN_FIELD], 0xffff);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001281 usbhid_submit_report(pidff->hid, pidff->reports[PID_DEVICE_GAIN],
Anssi Hannula224ee882006-07-19 01:40:47 -04001282 USB_DIR_OUT);
1283 }
1284
1285 error = pidff_check_autocenter(pidff, dev);
1286 if (error)
1287 goto fail;
1288
1289 max_effects =
1290 pidff->block_load[PID_EFFECT_BLOCK_INDEX].field->logical_maximum -
1291 pidff->block_load[PID_EFFECT_BLOCK_INDEX].field->logical_minimum +
1292 1;
1293 debug("max effects is %d", max_effects);
1294
1295 if (max_effects > PID_EFFECTS_MAX)
1296 max_effects = PID_EFFECTS_MAX;
1297
1298 if (pidff->pool[PID_SIMULTANEOUS_MAX].value)
1299 debug("max simultaneous effects is %d",
1300 pidff->pool[PID_SIMULTANEOUS_MAX].value[0]);
1301
1302 if (pidff->pool[PID_RAM_POOL_SIZE].value)
1303 debug("device memory size is %d bytes",
1304 pidff->pool[PID_RAM_POOL_SIZE].value[0]);
1305
1306 if (pidff->pool[PID_DEVICE_MANAGED_POOL].value &&
1307 pidff->pool[PID_DEVICE_MANAGED_POOL].value[0] == 0) {
1308 printk(KERN_NOTICE "hid-pidff: "
1309 "device does not support device managed pool\n");
1310 goto fail;
1311 }
1312
1313 error = input_ff_create(dev, max_effects);
1314 if (error)
1315 goto fail;
1316
1317 ff = dev->ff;
1318 ff->private = pidff;
1319 ff->upload = pidff_upload_effect;
1320 ff->erase = pidff_erase_effect;
1321 ff->set_gain = pidff_set_gain;
1322 ff->set_autocenter = pidff_set_autocenter;
1323 ff->playback = pidff_playback;
1324
1325 printk(KERN_INFO "Force feedback for USB HID PID devices by "
1326 "Anssi Hannula <anssi.hannula@gmail.com>\n");
1327
1328 return 0;
1329
1330 fail:
1331 kfree(pidff);
1332 return error;
1333}