• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/linux/linux-2.6.36/drivers/hid/usbhid/
1/*
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
27#include <linux/input.h>
28#include <linux/slab.h>
29#include <linux/usb.h>
30
31#include <linux/hid.h>
32
33#include "usbhid.h"
34
35#define	PID_EFFECTS_MAX		64
36
37/* Report usage table used to put reports into an array */
38
39#define PID_SET_EFFECT		0
40#define PID_EFFECT_OPERATION	1
41#define PID_DEVICE_GAIN		2
42#define PID_POOL		3
43#define PID_BLOCK_LOAD		4
44#define PID_BLOCK_FREE		5
45#define PID_DEVICE_CONTROL	6
46#define PID_CREATE_NEW_EFFECT	7
47
48#define PID_REQUIRED_REPORTS	7
49
50#define PID_SET_ENVELOPE	8
51#define PID_SET_CONDITION	9
52#define PID_SET_PERIODIC	10
53#define PID_SET_CONSTANT	11
54#define PID_SET_RAMP		12
55static const u8 pidff_reports[] = {
56	0x21, 0x77, 0x7d, 0x7f, 0x89, 0x90, 0x96, 0xab,
57	0x5a, 0x5f, 0x6e, 0x73, 0x74
58};
59
60/* device_control is really 0x95, but 0x96 specified as it is the usage of
61the only field in that report */
62
63/* Value usage tables used to put fields and values into arrays */
64
65#define PID_EFFECT_BLOCK_INDEX	0
66
67#define PID_DURATION		1
68#define PID_GAIN		2
69#define PID_TRIGGER_BUTTON	3
70#define PID_TRIGGER_REPEAT_INT	4
71#define PID_DIRECTION_ENABLE	5
72#define PID_START_DELAY		6
73static const u8 pidff_set_effect[] = {
74	0x22, 0x50, 0x52, 0x53, 0x54, 0x56, 0xa7
75};
76
77#define PID_ATTACK_LEVEL	1
78#define PID_ATTACK_TIME		2
79#define PID_FADE_LEVEL		3
80#define PID_FADE_TIME		4
81static const u8 pidff_set_envelope[] = { 0x22, 0x5b, 0x5c, 0x5d, 0x5e };
82
83#define PID_PARAM_BLOCK_OFFSET	1
84#define PID_CP_OFFSET		2
85#define PID_POS_COEFFICIENT	3
86#define PID_NEG_COEFFICIENT	4
87#define PID_POS_SATURATION	5
88#define PID_NEG_SATURATION	6
89#define PID_DEAD_BAND		7
90static const u8 pidff_set_condition[] = {
91	0x22, 0x23, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65
92};
93
94#define PID_MAGNITUDE		1
95#define PID_OFFSET		2
96#define PID_PHASE		3
97#define PID_PERIOD		4
98static const u8 pidff_set_periodic[] = { 0x22, 0x70, 0x6f, 0x71, 0x72 };
99static const u8 pidff_set_constant[] = { 0x22, 0x70 };
100
101#define PID_RAMP_START		1
102#define PID_RAMP_END		2
103static const u8 pidff_set_ramp[] = { 0x22, 0x75, 0x76 };
104
105#define PID_RAM_POOL_AVAILABLE	1
106static const u8 pidff_block_load[] = { 0x22, 0xac };
107
108#define PID_LOOP_COUNT		1
109static const u8 pidff_effect_operation[] = { 0x22, 0x7c };
110
111static const u8 pidff_block_free[] = { 0x22 };
112
113#define PID_DEVICE_GAIN_FIELD	0
114static const u8 pidff_device_gain[] = { 0x7e };
115
116#define PID_RAM_POOL_SIZE	0
117#define PID_SIMULTANEOUS_MAX	1
118#define PID_DEVICE_MANAGED_POOL	2
119static const u8 pidff_pool[] = { 0x80, 0x83, 0xa9 };
120
121/* Special field key tables used to put special field keys into arrays */
122
123#define PID_ENABLE_ACTUATORS	0
124#define PID_RESET		1
125static const u8 pidff_device_control[] = { 0x97, 0x9a };
126
127#define PID_CONSTANT	0
128#define PID_RAMP	1
129#define PID_SQUARE	2
130#define PID_SINE	3
131#define PID_TRIANGLE	4
132#define PID_SAW_UP	5
133#define PID_SAW_DOWN	6
134#define PID_SPRING	7
135#define PID_DAMPER	8
136#define PID_INERTIA	9
137#define PID_FRICTION	10
138static const u8 pidff_effect_types[] = {
139	0x26, 0x27, 0x30, 0x31, 0x32, 0x33, 0x34,
140	0x40, 0x41, 0x42, 0x43
141};
142
143#define PID_BLOCK_LOAD_SUCCESS	0
144#define PID_BLOCK_LOAD_FULL	1
145static const u8 pidff_block_load_status[] = { 0x8c, 0x8d };
146
147#define PID_EFFECT_START	0
148#define PID_EFFECT_STOP		1
149static const u8 pidff_effect_operation_status[] = { 0x79, 0x7b };
150
151struct pidff_usage {
152	struct hid_field *field;
153	s32 *value;
154};
155
156struct pidff_device {
157	struct hid_device *hid;
158
159	struct hid_report *reports[sizeof(pidff_reports)];
160
161	struct pidff_usage set_effect[sizeof(pidff_set_effect)];
162	struct pidff_usage set_envelope[sizeof(pidff_set_envelope)];
163	struct pidff_usage set_condition[sizeof(pidff_set_condition)];
164	struct pidff_usage set_periodic[sizeof(pidff_set_periodic)];
165	struct pidff_usage set_constant[sizeof(pidff_set_constant)];
166	struct pidff_usage set_ramp[sizeof(pidff_set_ramp)];
167
168	struct pidff_usage device_gain[sizeof(pidff_device_gain)];
169	struct pidff_usage block_load[sizeof(pidff_block_load)];
170	struct pidff_usage pool[sizeof(pidff_pool)];
171	struct pidff_usage effect_operation[sizeof(pidff_effect_operation)];
172	struct pidff_usage block_free[sizeof(pidff_block_free)];
173
174	/* Special field is a field that is not composed of
175	   usage<->value pairs that pidff_usage values are */
176
177	/* Special field in create_new_effect */
178	struct hid_field *create_new_effect_type;
179
180	/* Special fields in set_effect */
181	struct hid_field *set_effect_type;
182	struct hid_field *effect_direction;
183
184	/* Special field in device_control */
185	struct hid_field *device_control;
186
187	/* Special field in block_load */
188	struct hid_field *block_load_status;
189
190	/* Special field in effect_operation */
191	struct hid_field *effect_operation_status;
192
193	int control_id[sizeof(pidff_device_control)];
194	int type_id[sizeof(pidff_effect_types)];
195	int status_id[sizeof(pidff_block_load_status)];
196	int operation_id[sizeof(pidff_effect_operation_status)];
197
198	int pid_id[PID_EFFECTS_MAX];
199};
200
201/*
202 * Scale an unsigned value with range 0..max for the given field
203 */
204static int pidff_rescale(int i, int max, struct hid_field *field)
205{
206	return i * (field->logical_maximum - field->logical_minimum) / max +
207	    field->logical_minimum;
208}
209
210/*
211 * Scale a signed value in range -0x8000..0x7fff for the given field
212 */
213static int pidff_rescale_signed(int i, struct hid_field *field)
214{
215	return i == 0 ? 0 : i >
216	    0 ? i * field->logical_maximum / 0x7fff : i *
217	    field->logical_minimum / -0x8000;
218}
219
220static void pidff_set(struct pidff_usage *usage, u16 value)
221{
222	usage->value[0] = pidff_rescale(value, 0xffff, usage->field);
223	debug("calculated from %d to %d", value, usage->value[0]);
224}
225
226static void pidff_set_signed(struct pidff_usage *usage, s16 value)
227{
228	if (usage->field->logical_minimum < 0)
229		usage->value[0] = pidff_rescale_signed(value, usage->field);
230	else {
231		if (value < 0)
232			usage->value[0] =
233			    pidff_rescale(-value, 0x8000, usage->field);
234		else
235			usage->value[0] =
236			    pidff_rescale(value, 0x7fff, usage->field);
237	}
238	debug("calculated from %d to %d", value, usage->value[0]);
239}
240
241/*
242 * Send envelope report to the device
243 */
244static void pidff_set_envelope_report(struct pidff_device *pidff,
245				      struct ff_envelope *envelope)
246{
247	pidff->set_envelope[PID_EFFECT_BLOCK_INDEX].value[0] =
248	    pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0];
249
250	pidff->set_envelope[PID_ATTACK_LEVEL].value[0] =
251	    pidff_rescale(envelope->attack_level >
252			  0x7fff ? 0x7fff : envelope->attack_level, 0x7fff,
253			  pidff->set_envelope[PID_ATTACK_LEVEL].field);
254	pidff->set_envelope[PID_FADE_LEVEL].value[0] =
255	    pidff_rescale(envelope->fade_level >
256			  0x7fff ? 0x7fff : envelope->fade_level, 0x7fff,
257			  pidff->set_envelope[PID_FADE_LEVEL].field);
258
259	pidff->set_envelope[PID_ATTACK_TIME].value[0] = envelope->attack_length;
260	pidff->set_envelope[PID_FADE_TIME].value[0] = envelope->fade_length;
261
262	debug("attack %u => %d", envelope->attack_level,
263	      pidff->set_envelope[PID_ATTACK_LEVEL].value[0]);
264
265	usbhid_submit_report(pidff->hid, pidff->reports[PID_SET_ENVELOPE],
266			  USB_DIR_OUT);
267}
268
269/*
270 * Test if the new envelope differs from old one
271 */
272static int pidff_needs_set_envelope(struct ff_envelope *envelope,
273				    struct ff_envelope *old)
274{
275	return envelope->attack_level != old->attack_level ||
276	       envelope->fade_level != old->fade_level ||
277	       envelope->attack_length != old->attack_length ||
278	       envelope->fade_length != old->fade_length;
279}
280
281/*
282 * Send constant force report to the device
283 */
284static void pidff_set_constant_force_report(struct pidff_device *pidff,
285					    struct ff_effect *effect)
286{
287	pidff->set_constant[PID_EFFECT_BLOCK_INDEX].value[0] =
288		pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0];
289	pidff_set_signed(&pidff->set_constant[PID_MAGNITUDE],
290			 effect->u.constant.level);
291
292	usbhid_submit_report(pidff->hid, pidff->reports[PID_SET_CONSTANT],
293			  USB_DIR_OUT);
294}
295
296/*
297 * Test if the constant parameters have changed between effects
298 */
299static int pidff_needs_set_constant(struct ff_effect *effect,
300				    struct ff_effect *old)
301{
302	return effect->u.constant.level != old->u.constant.level;
303}
304
305/*
306 * Send set effect report to the device
307 */
308static void pidff_set_effect_report(struct pidff_device *pidff,
309				    struct ff_effect *effect)
310{
311	pidff->set_effect[PID_EFFECT_BLOCK_INDEX].value[0] =
312		pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0];
313	pidff->set_effect_type->value[0] =
314		pidff->create_new_effect_type->value[0];
315	pidff->set_effect[PID_DURATION].value[0] = effect->replay.length;
316	pidff->set_effect[PID_TRIGGER_BUTTON].value[0] = effect->trigger.button;
317	pidff->set_effect[PID_TRIGGER_REPEAT_INT].value[0] =
318		effect->trigger.interval;
319	pidff->set_effect[PID_GAIN].value[0] =
320		pidff->set_effect[PID_GAIN].field->logical_maximum;
321	pidff->set_effect[PID_DIRECTION_ENABLE].value[0] = 1;
322	pidff->effect_direction->value[0] =
323		pidff_rescale(effect->direction, 0xffff,
324				pidff->effect_direction);
325	pidff->set_effect[PID_START_DELAY].value[0] = effect->replay.delay;
326
327	usbhid_submit_report(pidff->hid, pidff->reports[PID_SET_EFFECT],
328			  USB_DIR_OUT);
329}
330
331/*
332 * Test if the values used in set_effect have changed
333 */
334static int pidff_needs_set_effect(struct ff_effect *effect,
335				  struct ff_effect *old)
336{
337	return effect->replay.length != old->replay.length ||
338	       effect->trigger.interval != old->trigger.interval ||
339	       effect->trigger.button != old->trigger.button ||
340	       effect->direction != old->direction ||
341	       effect->replay.delay != old->replay.delay;
342}
343
344/*
345 * Send periodic effect report to the device
346 */
347static void pidff_set_periodic_report(struct pidff_device *pidff,
348				      struct ff_effect *effect)
349{
350	pidff->set_periodic[PID_EFFECT_BLOCK_INDEX].value[0] =
351		pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0];
352	pidff_set_signed(&pidff->set_periodic[PID_MAGNITUDE],
353			 effect->u.periodic.magnitude);
354	pidff_set_signed(&pidff->set_periodic[PID_OFFSET],
355			 effect->u.periodic.offset);
356	pidff_set(&pidff->set_periodic[PID_PHASE], effect->u.periodic.phase);
357	pidff->set_periodic[PID_PERIOD].value[0] = effect->u.periodic.period;
358
359	usbhid_submit_report(pidff->hid, pidff->reports[PID_SET_PERIODIC],
360			  USB_DIR_OUT);
361
362}
363
364/*
365 * Test if periodic effect parameters have changed
366 */
367static int pidff_needs_set_periodic(struct ff_effect *effect,
368				    struct ff_effect *old)
369{
370	return effect->u.periodic.magnitude != old->u.periodic.magnitude ||
371	       effect->u.periodic.offset != old->u.periodic.offset ||
372	       effect->u.periodic.phase != old->u.periodic.phase ||
373	       effect->u.periodic.period != old->u.periodic.period;
374}
375
376/*
377 * Send condition effect reports to the device
378 */
379static void pidff_set_condition_report(struct pidff_device *pidff,
380				       struct ff_effect *effect)
381{
382	int i;
383
384	pidff->set_condition[PID_EFFECT_BLOCK_INDEX].value[0] =
385		pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0];
386
387	for (i = 0; i < 2; i++) {
388		pidff->set_condition[PID_PARAM_BLOCK_OFFSET].value[0] = i;
389		pidff_set_signed(&pidff->set_condition[PID_CP_OFFSET],
390				 effect->u.condition[i].center);
391		pidff_set_signed(&pidff->set_condition[PID_POS_COEFFICIENT],
392				 effect->u.condition[i].right_coeff);
393		pidff_set_signed(&pidff->set_condition[PID_NEG_COEFFICIENT],
394				 effect->u.condition[i].left_coeff);
395		pidff_set(&pidff->set_condition[PID_POS_SATURATION],
396			  effect->u.condition[i].right_saturation);
397		pidff_set(&pidff->set_condition[PID_NEG_SATURATION],
398			  effect->u.condition[i].left_saturation);
399		pidff_set(&pidff->set_condition[PID_DEAD_BAND],
400			  effect->u.condition[i].deadband);
401		usbhid_submit_report(pidff->hid, pidff->reports[PID_SET_CONDITION],
402				  USB_DIR_OUT);
403	}
404}
405
406/*
407 * Test if condition effect parameters have changed
408 */
409static int pidff_needs_set_condition(struct ff_effect *effect,
410				     struct ff_effect *old)
411{
412	int i;
413	int ret = 0;
414
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];
418
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;
425	}
426
427	return ret;
428}
429
430/*
431 * Send ramp force report to the device
432 */
433static void pidff_set_ramp_force_report(struct pidff_device *pidff,
434					struct ff_effect *effect)
435{
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);
444}
445
446/*
447 * Test if ramp force parameters have changed
448 */
449static int pidff_needs_set_ramp(struct ff_effect *effect, struct ff_effect *old)
450{
451	return effect->u.ramp.start_level != old->u.ramp.start_level ||
452	       effect->u.ramp.end_level != old->u.ramp.end_level;
453}
454
455/*
456 * Send a request for effect upload to the device
457 *
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.
461 */
462static int pidff_request_effect_upload(struct pidff_device *pidff, int efnum)
463{
464	int j;
465
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);
470
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);
474
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;
486		}
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;
493		}
494	}
495	printk(KERN_ERR "hid-pidff: pid_block_load failed 60 times\n");
496	return -EIO;
497}
498
499/*
500 * Play the effect with PID id n times
501 */
502static void pidff_playback_pid(struct pidff_device *pidff, int pid_id, int n)
503{
504	pidff->effect_operation[PID_EFFECT_BLOCK_INDEX].value[0] = pid_id;
505
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;
513	}
514
515	usbhid_submit_report(pidff->hid, pidff->reports[PID_EFFECT_OPERATION],
516			  USB_DIR_OUT);
517}
518
519/**
520 * Play the effect with effect id @effect_id for @value times
521 */
522static int pidff_playback(struct input_dev *dev, int effect_id, int value)
523{
524	struct pidff_device *pidff = dev->ff->private;
525
526	pidff_playback_pid(pidff, pidff->pid_id[effect_id], value);
527
528	return 0;
529}
530
531/*
532 * Erase effect with PID id
533 */
534static void pidff_erase_pid(struct pidff_device *pidff, int pid_id)
535{
536	pidff->block_free[PID_EFFECT_BLOCK_INDEX].value[0] = pid_id;
537	usbhid_submit_report(pidff->hid, pidff->reports[PID_BLOCK_FREE],
538			  USB_DIR_OUT);
539}
540
541/*
542 * Stop and erase effect with effect_id
543 */
544static int pidff_erase_effect(struct input_dev *dev, int effect_id)
545{
546	struct pidff_device *pidff = dev->ff->private;
547	int pid_id = pidff->pid_id[effect_id];
548
549	debug("starting to erase %d/%d", effect_id, pidff->pid_id[effect_id]);
550	/* Wait for the queue to clear. We do not want a full fifo to
551	   prevent the effect removal. */
552	usbhid_wait_io(pidff->hid);
553	pidff_playback_pid(pidff, pid_id, 0);
554	pidff_erase_pid(pidff, pid_id);
555
556	return 0;
557}
558
559/*
560 * Effect upload handler
561 */
562static int pidff_upload_effect(struct input_dev *dev, struct ff_effect *effect,
563			       struct ff_effect *old)
564{
565	struct pidff_device *pidff = dev->ff->private;
566	int type_id;
567	int error;
568
569	switch (effect->type) {
570	case FF_CONSTANT:
571		if (!old) {
572			error = pidff_request_effect_upload(pidff,
573					pidff->type_id[PID_CONSTANT]);
574			if (error)
575				return error;
576		}
577		if (!old || pidff_needs_set_effect(effect, old))
578			pidff_set_effect_report(pidff, effect);
579		if (!old || pidff_needs_set_constant(effect, old))
580			pidff_set_constant_force_report(pidff, effect);
581		if (!old ||
582		    pidff_needs_set_envelope(&effect->u.constant.envelope,
583					&old->u.constant.envelope))
584			pidff_set_envelope_report(pidff,
585					&effect->u.constant.envelope);
586		break;
587
588	case FF_PERIODIC:
589		if (!old) {
590			switch (effect->u.periodic.waveform) {
591			case FF_SQUARE:
592				type_id = PID_SQUARE;
593				break;
594			case FF_TRIANGLE:
595				type_id = PID_TRIANGLE;
596				break;
597			case FF_SINE:
598				type_id = PID_SINE;
599				break;
600			case FF_SAW_UP:
601				type_id = PID_SAW_UP;
602				break;
603			case FF_SAW_DOWN:
604				type_id = PID_SAW_DOWN;
605				break;
606			default:
607				printk(KERN_ERR
608				       "hid-pidff: invalid waveform\n");
609				return -EINVAL;
610			}
611
612			error = pidff_request_effect_upload(pidff,
613					pidff->type_id[type_id]);
614			if (error)
615				return error;
616		}
617		if (!old || pidff_needs_set_effect(effect, old))
618			pidff_set_effect_report(pidff, effect);
619		if (!old || pidff_needs_set_periodic(effect, old))
620			pidff_set_periodic_report(pidff, effect);
621		if (!old ||
622		    pidff_needs_set_envelope(&effect->u.periodic.envelope,
623					&old->u.periodic.envelope))
624			pidff_set_envelope_report(pidff,
625					&effect->u.periodic.envelope);
626		break;
627
628	case FF_RAMP:
629		if (!old) {
630			error = pidff_request_effect_upload(pidff,
631					pidff->type_id[PID_RAMP]);
632			if (error)
633				return error;
634		}
635		if (!old || pidff_needs_set_effect(effect, old))
636			pidff_set_effect_report(pidff, effect);
637		if (!old || pidff_needs_set_ramp(effect, old))
638			pidff_set_ramp_force_report(pidff, effect);
639		if (!old ||
640		    pidff_needs_set_envelope(&effect->u.ramp.envelope,
641					&old->u.ramp.envelope))
642			pidff_set_envelope_report(pidff,
643					&effect->u.ramp.envelope);
644		break;
645
646	case FF_SPRING:
647		if (!old) {
648			error = pidff_request_effect_upload(pidff,
649					pidff->type_id[PID_SPRING]);
650			if (error)
651				return error;
652		}
653		if (!old || pidff_needs_set_effect(effect, old))
654			pidff_set_effect_report(pidff, effect);
655		if (!old || pidff_needs_set_condition(effect, old))
656			pidff_set_condition_report(pidff, effect);
657		break;
658
659	case FF_FRICTION:
660		if (!old) {
661			error = pidff_request_effect_upload(pidff,
662					pidff->type_id[PID_FRICTION]);
663			if (error)
664				return error;
665		}
666		if (!old || pidff_needs_set_effect(effect, old))
667			pidff_set_effect_report(pidff, effect);
668		if (!old || pidff_needs_set_condition(effect, old))
669			pidff_set_condition_report(pidff, effect);
670		break;
671
672	case FF_DAMPER:
673		if (!old) {
674			error = pidff_request_effect_upload(pidff,
675					pidff->type_id[PID_DAMPER]);
676			if (error)
677				return error;
678		}
679		if (!old || pidff_needs_set_effect(effect, old))
680			pidff_set_effect_report(pidff, effect);
681		if (!old || pidff_needs_set_condition(effect, old))
682			pidff_set_condition_report(pidff, effect);
683		break;
684
685	case FF_INERTIA:
686		if (!old) {
687			error = pidff_request_effect_upload(pidff,
688					pidff->type_id[PID_INERTIA]);
689			if (error)
690				return error;
691		}
692		if (!old || pidff_needs_set_effect(effect, old))
693			pidff_set_effect_report(pidff, effect);
694		if (!old || pidff_needs_set_condition(effect, old))
695			pidff_set_condition_report(pidff, effect);
696		break;
697
698	default:
699		printk(KERN_ERR "hid-pidff: invalid type\n");
700		return -EINVAL;
701	}
702
703	if (!old)
704		pidff->pid_id[effect->id] =
705		    pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0];
706
707	debug("uploaded");
708
709	return 0;
710}
711
712/*
713 * set_gain() handler
714 */
715static void pidff_set_gain(struct input_dev *dev, u16 gain)
716{
717	struct pidff_device *pidff = dev->ff->private;
718
719	pidff_set(&pidff->device_gain[PID_DEVICE_GAIN_FIELD], gain);
720	usbhid_submit_report(pidff->hid, pidff->reports[PID_DEVICE_GAIN],
721			  USB_DIR_OUT);
722}
723
724static void pidff_autocenter(struct pidff_device *pidff, u16 magnitude)
725{
726	struct hid_field *field =
727		pidff->block_load[PID_EFFECT_BLOCK_INDEX].field;
728
729	if (!magnitude) {
730		pidff_playback_pid(pidff, field->logical_minimum, 0);
731		return;
732	}
733
734	pidff_playback_pid(pidff, field->logical_minimum, 1);
735
736	pidff->set_effect[PID_EFFECT_BLOCK_INDEX].value[0] =
737		pidff->block_load[PID_EFFECT_BLOCK_INDEX].field->logical_minimum;
738	pidff->set_effect_type->value[0] = pidff->type_id[PID_SPRING];
739	pidff->set_effect[PID_DURATION].value[0] = 0;
740	pidff->set_effect[PID_TRIGGER_BUTTON].value[0] = 0;
741	pidff->set_effect[PID_TRIGGER_REPEAT_INT].value[0] = 0;
742	pidff_set(&pidff->set_effect[PID_GAIN], magnitude);
743	pidff->set_effect[PID_DIRECTION_ENABLE].value[0] = 1;
744	pidff->set_effect[PID_START_DELAY].value[0] = 0;
745
746	usbhid_submit_report(pidff->hid, pidff->reports[PID_SET_EFFECT],
747			  USB_DIR_OUT);
748}
749
750/*
751 * pidff_set_autocenter() handler
752 */
753static void pidff_set_autocenter(struct input_dev *dev, u16 magnitude)
754{
755	struct pidff_device *pidff = dev->ff->private;
756
757	pidff_autocenter(pidff, magnitude);
758}
759
760/*
761 * Find fields from a report and fill a pidff_usage
762 */
763static int pidff_find_fields(struct pidff_usage *usage, const u8 *table,
764			     struct hid_report *report, int count, int strict)
765{
766	int i, j, k, found;
767
768	for (k = 0; k < count; k++) {
769		found = 0;
770		for (i = 0; i < report->maxfield; i++) {
771			if (report->field[i]->maxusage !=
772			    report->field[i]->report_count) {
773				debug("maxusage and report_count do not match, "
774				      "skipping");
775				continue;
776			}
777			for (j = 0; j < report->field[i]->maxusage; j++) {
778				if (report->field[i]->usage[j].hid ==
779				    (HID_UP_PID | table[k])) {
780					debug("found %d at %d->%d", k, i, j);
781					usage[k].field = report->field[i];
782					usage[k].value =
783						&report->field[i]->value[j];
784					found = 1;
785					break;
786				}
787			}
788			if (found)
789				break;
790		}
791		if (!found && strict) {
792			debug("failed to locate %d", k);
793			return -1;
794		}
795	}
796	return 0;
797}
798
799/*
800 * Return index into pidff_reports for the given usage
801 */
802static int pidff_check_usage(int usage)
803{
804	int i;
805
806	for (i = 0; i < sizeof(pidff_reports); i++)
807		if (usage == (HID_UP_PID | pidff_reports[i]))
808			return i;
809
810	return -1;
811}
812
813/*
814 * Find the reports and fill pidff->reports[]
815 * report_type specifies either OUTPUT or FEATURE reports
816 */
817static void pidff_find_reports(struct hid_device *hid, int report_type,
818			       struct pidff_device *pidff)
819{
820	struct hid_report *report;
821	int i, ret;
822
823	list_for_each_entry(report,
824			    &hid->report_enum[report_type].report_list, list) {
825		if (report->maxfield < 1)
826			continue;
827		ret = pidff_check_usage(report->field[0]->logical);
828		if (ret != -1) {
829			debug("found usage 0x%02x from field->logical",
830			      pidff_reports[ret]);
831			pidff->reports[ret] = report;
832			continue;
833		}
834
835		/*
836		 * Sometimes logical collections are stacked to indicate
837		 * different usages for the report and the field, in which
838		 * case we want the usage of the parent. However, Linux HID
839		 * implementation hides this fact, so we have to dig it up
840		 * ourselves
841		 */
842		i = report->field[0]->usage[0].collection_index;
843		if (i <= 0 ||
844		    hid->collection[i - 1].type != HID_COLLECTION_LOGICAL)
845			continue;
846		ret = pidff_check_usage(hid->collection[i - 1].usage);
847		if (ret != -1 && !pidff->reports[ret]) {
848			debug("found usage 0x%02x from collection array",
849			      pidff_reports[ret]);
850			pidff->reports[ret] = report;
851		}
852	}
853}
854
855/*
856 * Test if the required reports have been found
857 */
858static int pidff_reports_ok(struct pidff_device *pidff)
859{
860	int i;
861
862	for (i = 0; i <= PID_REQUIRED_REPORTS; i++) {
863		if (!pidff->reports[i]) {
864			debug("%d missing", i);
865			return 0;
866		}
867	}
868
869	return 1;
870}
871
872/*
873 * Find a field with a specific usage within a report
874 */
875static struct hid_field *pidff_find_special_field(struct hid_report *report,
876						  int usage, int enforce_min)
877{
878	int i;
879
880	for (i = 0; i < report->maxfield; i++) {
881		if (report->field[i]->logical == (HID_UP_PID | usage) &&
882		    report->field[i]->report_count > 0) {
883			if (!enforce_min ||
884			    report->field[i]->logical_minimum == 1)
885				return report->field[i];
886			else {
887				printk(KERN_ERR "hid-pidff: logical_minimum "
888					"is not 1 as it should be\n");
889				return NULL;
890			}
891		}
892	}
893	return NULL;
894}
895
896/*
897 * Fill a pidff->*_id struct table
898 */
899static int pidff_find_special_keys(int *keys, struct hid_field *fld,
900				   const u8 *usagetable, int count)
901{
902
903	int i, j;
904	int found = 0;
905
906	for (i = 0; i < count; i++) {
907		for (j = 0; j < fld->maxusage; j++) {
908			if (fld->usage[j].hid == (HID_UP_PID | usagetable[i])) {
909				keys[i] = j + 1;
910				found++;
911				break;
912			}
913		}
914	}
915	return found;
916}
917
918#define PIDFF_FIND_SPECIAL_KEYS(keys, field, name) \
919	pidff_find_special_keys(pidff->keys, pidff->field, pidff_ ## name, \
920		sizeof(pidff_ ## name))
921
922/*
923 * Find and check the special fields
924 */
925static int pidff_find_special_fields(struct pidff_device *pidff)
926{
927	debug("finding special fields");
928
929	pidff->create_new_effect_type =
930		pidff_find_special_field(pidff->reports[PID_CREATE_NEW_EFFECT],
931					 0x25, 1);
932	pidff->set_effect_type =
933		pidff_find_special_field(pidff->reports[PID_SET_EFFECT],
934					 0x25, 1);
935	pidff->effect_direction =
936		pidff_find_special_field(pidff->reports[PID_SET_EFFECT],
937					 0x57, 0);
938	pidff->device_control =
939		pidff_find_special_field(pidff->reports[PID_DEVICE_CONTROL],
940					 0x96, 1);
941	pidff->block_load_status =
942		pidff_find_special_field(pidff->reports[PID_BLOCK_LOAD],
943					 0x8b, 1);
944	pidff->effect_operation_status =
945		pidff_find_special_field(pidff->reports[PID_EFFECT_OPERATION],
946					 0x78, 1);
947
948	debug("search done");
949
950	if (!pidff->create_new_effect_type || !pidff->set_effect_type) {
951		printk(KERN_ERR "hid-pidff: effect lists not found\n");
952		return -1;
953	}
954
955	if (!pidff->effect_direction) {
956		printk(KERN_ERR "hid-pidff: direction field not found\n");
957		return -1;
958	}
959
960	if (!pidff->device_control) {
961		printk(KERN_ERR "hid-pidff: device control field not found\n");
962		return -1;
963	}
964
965	if (!pidff->block_load_status) {
966		printk(KERN_ERR
967		       "hid-pidff: block load status field not found\n");
968		return -1;
969	}
970
971	if (!pidff->effect_operation_status) {
972		printk(KERN_ERR
973		       "hid-pidff: effect operation field not found\n");
974		return -1;
975	}
976
977	pidff_find_special_keys(pidff->control_id, pidff->device_control,
978				pidff_device_control,
979				sizeof(pidff_device_control));
980
981	PIDFF_FIND_SPECIAL_KEYS(control_id, device_control, device_control);
982
983	if (!PIDFF_FIND_SPECIAL_KEYS(type_id, create_new_effect_type,
984				     effect_types)) {
985		printk(KERN_ERR "hid-pidff: no effect types found\n");
986		return -1;
987	}
988
989	if (PIDFF_FIND_SPECIAL_KEYS(status_id, block_load_status,
990				    block_load_status) !=
991			sizeof(pidff_block_load_status)) {
992		printk(KERN_ERR
993		       "hidpidff: block load status identifiers not found\n");
994		return -1;
995	}
996
997	if (PIDFF_FIND_SPECIAL_KEYS(operation_id, effect_operation_status,
998				    effect_operation_status) !=
999			sizeof(pidff_effect_operation_status)) {
1000		printk(KERN_ERR
1001		       "hidpidff: effect operation identifiers not found\n");
1002		return -1;
1003	}
1004
1005	return 0;
1006}
1007
1008/**
1009 * Find the implemented effect types
1010 */
1011static int pidff_find_effects(struct pidff_device *pidff,
1012			      struct input_dev *dev)
1013{
1014	int i;
1015
1016	for (i = 0; i < sizeof(pidff_effect_types); i++) {
1017		int pidff_type = pidff->type_id[i];
1018		if (pidff->set_effect_type->usage[pidff_type].hid !=
1019		    pidff->create_new_effect_type->usage[pidff_type].hid) {
1020			printk(KERN_ERR "hid-pidff: "
1021			       "effect type number %d is invalid\n", i);
1022			return -1;
1023		}
1024	}
1025
1026	if (pidff->type_id[PID_CONSTANT])
1027		set_bit(FF_CONSTANT, dev->ffbit);
1028	if (pidff->type_id[PID_RAMP])
1029		set_bit(FF_RAMP, dev->ffbit);
1030	if (pidff->type_id[PID_SQUARE]) {
1031		set_bit(FF_SQUARE, dev->ffbit);
1032		set_bit(FF_PERIODIC, dev->ffbit);
1033	}
1034	if (pidff->type_id[PID_SINE]) {
1035		set_bit(FF_SINE, dev->ffbit);
1036		set_bit(FF_PERIODIC, dev->ffbit);
1037	}
1038	if (pidff->type_id[PID_TRIANGLE]) {
1039		set_bit(FF_TRIANGLE, dev->ffbit);
1040		set_bit(FF_PERIODIC, dev->ffbit);
1041	}
1042	if (pidff->type_id[PID_SAW_UP]) {
1043		set_bit(FF_SAW_UP, dev->ffbit);
1044		set_bit(FF_PERIODIC, dev->ffbit);
1045	}
1046	if (pidff->type_id[PID_SAW_DOWN]) {
1047		set_bit(FF_SAW_DOWN, dev->ffbit);
1048		set_bit(FF_PERIODIC, dev->ffbit);
1049	}
1050	if (pidff->type_id[PID_SPRING])
1051		set_bit(FF_SPRING, dev->ffbit);
1052	if (pidff->type_id[PID_DAMPER])
1053		set_bit(FF_DAMPER, dev->ffbit);
1054	if (pidff->type_id[PID_INERTIA])
1055		set_bit(FF_INERTIA, dev->ffbit);
1056	if (pidff->type_id[PID_FRICTION])
1057		set_bit(FF_FRICTION, dev->ffbit);
1058
1059	return 0;
1060
1061}
1062
1063#define PIDFF_FIND_FIELDS(name, report, strict) \
1064	pidff_find_fields(pidff->name, pidff_ ## name, \
1065		pidff->reports[report], \
1066		sizeof(pidff_ ## name), strict)
1067
1068/*
1069 * Fill and check the pidff_usages
1070 */
1071static int pidff_init_fields(struct pidff_device *pidff, struct input_dev *dev)
1072{
1073	int envelope_ok = 0;
1074
1075	if (PIDFF_FIND_FIELDS(set_effect, PID_SET_EFFECT, 1)) {
1076		printk(KERN_ERR
1077		       "hid-pidff: unknown set_effect report layout\n");
1078		return -ENODEV;
1079	}
1080
1081	PIDFF_FIND_FIELDS(block_load, PID_BLOCK_LOAD, 0);
1082	if (!pidff->block_load[PID_EFFECT_BLOCK_INDEX].value) {
1083		printk(KERN_ERR
1084		       "hid-pidff: unknown pid_block_load report layout\n");
1085		return -ENODEV;
1086	}
1087
1088	if (PIDFF_FIND_FIELDS(effect_operation, PID_EFFECT_OPERATION, 1)) {
1089		printk(KERN_ERR
1090		       "hid-pidff: unknown effect_operation report layout\n");
1091		return -ENODEV;
1092	}
1093
1094	if (PIDFF_FIND_FIELDS(block_free, PID_BLOCK_FREE, 1)) {
1095		printk(KERN_ERR
1096		       "hid-pidff: unknown pid_block_free report layout\n");
1097		return -ENODEV;
1098	}
1099
1100	if (!PIDFF_FIND_FIELDS(set_envelope, PID_SET_ENVELOPE, 1))
1101		envelope_ok = 1;
1102
1103	if (pidff_find_special_fields(pidff) || pidff_find_effects(pidff, dev))
1104		return -ENODEV;
1105
1106	if (!envelope_ok) {
1107		if (test_and_clear_bit(FF_CONSTANT, dev->ffbit))
1108			printk(KERN_WARNING "hid-pidff: "
1109			       "has constant effect but no envelope\n");
1110		if (test_and_clear_bit(FF_RAMP, dev->ffbit))
1111			printk(KERN_WARNING "hid-pidff: "
1112				"has ramp effect but no envelope\n");
1113
1114		if (test_and_clear_bit(FF_PERIODIC, dev->ffbit))
1115			printk(KERN_WARNING "hid-pidff: "
1116				"has periodic effect but no envelope\n");
1117	}
1118
1119	if (test_bit(FF_CONSTANT, dev->ffbit) &&
1120	    PIDFF_FIND_FIELDS(set_constant, PID_SET_CONSTANT, 1)) {
1121		printk(KERN_WARNING
1122		       "hid-pidff: unknown constant effect layout\n");
1123		clear_bit(FF_CONSTANT, dev->ffbit);
1124	}
1125
1126	if (test_bit(FF_RAMP, dev->ffbit) &&
1127	    PIDFF_FIND_FIELDS(set_ramp, PID_SET_RAMP, 1)) {
1128		printk(KERN_WARNING "hid-pidff: unknown ramp effect layout\n");
1129		clear_bit(FF_RAMP, dev->ffbit);
1130	}
1131
1132	if ((test_bit(FF_SPRING, dev->ffbit) ||
1133	     test_bit(FF_DAMPER, dev->ffbit) ||
1134	     test_bit(FF_FRICTION, dev->ffbit) ||
1135	     test_bit(FF_INERTIA, dev->ffbit)) &&
1136	    PIDFF_FIND_FIELDS(set_condition, PID_SET_CONDITION, 1)) {
1137		printk(KERN_WARNING
1138		       "hid-pidff: unknown condition effect layout\n");
1139		clear_bit(FF_SPRING, dev->ffbit);
1140		clear_bit(FF_DAMPER, dev->ffbit);
1141		clear_bit(FF_FRICTION, dev->ffbit);
1142		clear_bit(FF_INERTIA, dev->ffbit);
1143	}
1144
1145	if (test_bit(FF_PERIODIC, dev->ffbit) &&
1146	    PIDFF_FIND_FIELDS(set_periodic, PID_SET_PERIODIC, 1)) {
1147		printk(KERN_WARNING
1148		       "hid-pidff: unknown periodic effect layout\n");
1149		clear_bit(FF_PERIODIC, dev->ffbit);
1150	}
1151
1152	PIDFF_FIND_FIELDS(pool, PID_POOL, 0);
1153
1154	if (!PIDFF_FIND_FIELDS(device_gain, PID_DEVICE_GAIN, 1))
1155		set_bit(FF_GAIN, dev->ffbit);
1156
1157	return 0;
1158}
1159
1160/*
1161 * Reset the device
1162 */
1163static void pidff_reset(struct pidff_device *pidff)
1164{
1165	struct hid_device *hid = pidff->hid;
1166	int i = 0;
1167
1168	pidff->device_control->value[0] = pidff->control_id[PID_RESET];
1169	/* We reset twice as sometimes hid_wait_io isn't waiting long enough */
1170	usbhid_submit_report(hid, pidff->reports[PID_DEVICE_CONTROL], USB_DIR_OUT);
1171	usbhid_wait_io(hid);
1172	usbhid_submit_report(hid, pidff->reports[PID_DEVICE_CONTROL], USB_DIR_OUT);
1173	usbhid_wait_io(hid);
1174
1175	pidff->device_control->value[0] =
1176		pidff->control_id[PID_ENABLE_ACTUATORS];
1177	usbhid_submit_report(hid, pidff->reports[PID_DEVICE_CONTROL], USB_DIR_OUT);
1178	usbhid_wait_io(hid);
1179
1180	/* pool report is sometimes messed up, refetch it */
1181	usbhid_submit_report(hid, pidff->reports[PID_POOL], USB_DIR_IN);
1182	usbhid_wait_io(hid);
1183
1184	if (pidff->pool[PID_SIMULTANEOUS_MAX].value) {
1185		while (pidff->pool[PID_SIMULTANEOUS_MAX].value[0] < 2) {
1186			if (i++ > 20) {
1187				printk(KERN_WARNING "hid-pidff: device reports "
1188				       "%d simultaneous effects\n",
1189				       pidff->pool[PID_SIMULTANEOUS_MAX].value[0]);
1190				break;
1191			}
1192			debug("pid_pool requested again");
1193			usbhid_submit_report(hid, pidff->reports[PID_POOL],
1194					  USB_DIR_IN);
1195			usbhid_wait_io(hid);
1196		}
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);
1281		usbhid_submit_report(pidff->hid, pidff->reports[PID_DEVICE_GAIN],
1282				  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}
1334