xref: /linux/drivers/hid/usbhid/hid-pidff.c (revision a4119108d2530747e61c7cbf52e2affd089cb1f6)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Force feedback driver for USB HID PID compliant devices
4  *
5  *  Copyright (c) 2005, 2006 Anssi Hannula <anssi.hannula@gmail.com>
6  */
7 
8 /*
9  */
10 
11 /* #define DEBUG */
12 
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 
15 #include <linux/input.h>
16 #include <linux/slab.h>
17 #include <linux/usb.h>
18 #include <linux/hid.h>
19 #include <linux/minmax.h>
20 
21 
22 #define	PID_EFFECTS_MAX		64
23 #define	PID_INFINITE		0xffff
24 
25 /* Report usage table used to put reports into an array */
26 
27 #define PID_SET_EFFECT		0
28 #define PID_EFFECT_OPERATION	1
29 #define PID_DEVICE_GAIN		2
30 #define PID_POOL		3
31 #define PID_BLOCK_LOAD		4
32 #define PID_BLOCK_FREE		5
33 #define PID_DEVICE_CONTROL	6
34 #define PID_CREATE_NEW_EFFECT	7
35 
36 #define PID_REQUIRED_REPORTS	7
37 
38 #define PID_SET_ENVELOPE	8
39 #define PID_SET_CONDITION	9
40 #define PID_SET_PERIODIC	10
41 #define PID_SET_CONSTANT	11
42 #define PID_SET_RAMP		12
43 static const u8 pidff_reports[] = {
44 	0x21, 0x77, 0x7d, 0x7f, 0x89, 0x90, 0x96, 0xab,
45 	0x5a, 0x5f, 0x6e, 0x73, 0x74
46 };
47 
48 /* device_control is really 0x95, but 0x96 specified as it is the usage of
49 the only field in that report */
50 
51 /* Value usage tables used to put fields and values into arrays */
52 
53 #define PID_EFFECT_BLOCK_INDEX	0
54 
55 #define PID_DURATION		1
56 #define PID_GAIN		2
57 #define PID_TRIGGER_BUTTON	3
58 #define PID_TRIGGER_REPEAT_INT	4
59 #define PID_DIRECTION_ENABLE	5
60 #define PID_START_DELAY		6
61 static const u8 pidff_set_effect[] = {
62 	0x22, 0x50, 0x52, 0x53, 0x54, 0x56, 0xa7
63 };
64 
65 #define PID_ATTACK_LEVEL	1
66 #define PID_ATTACK_TIME		2
67 #define PID_FADE_LEVEL		3
68 #define PID_FADE_TIME		4
69 static const u8 pidff_set_envelope[] = { 0x22, 0x5b, 0x5c, 0x5d, 0x5e };
70 
71 #define PID_PARAM_BLOCK_OFFSET	1
72 #define PID_CP_OFFSET		2
73 #define PID_POS_COEFFICIENT	3
74 #define PID_NEG_COEFFICIENT	4
75 #define PID_POS_SATURATION	5
76 #define PID_NEG_SATURATION	6
77 #define PID_DEAD_BAND		7
78 static const u8 pidff_set_condition[] = {
79 	0x22, 0x23, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65
80 };
81 
82 #define PID_MAGNITUDE		1
83 #define PID_OFFSET		2
84 #define PID_PHASE		3
85 #define PID_PERIOD		4
86 static const u8 pidff_set_periodic[] = { 0x22, 0x70, 0x6f, 0x71, 0x72 };
87 static const u8 pidff_set_constant[] = { 0x22, 0x70 };
88 
89 #define PID_RAMP_START		1
90 #define PID_RAMP_END		2
91 static const u8 pidff_set_ramp[] = { 0x22, 0x75, 0x76 };
92 
93 #define PID_RAM_POOL_AVAILABLE	1
94 static const u8 pidff_block_load[] = { 0x22, 0xac };
95 
96 #define PID_LOOP_COUNT		1
97 static const u8 pidff_effect_operation[] = { 0x22, 0x7c };
98 
99 static const u8 pidff_block_free[] = { 0x22 };
100 
101 #define PID_DEVICE_GAIN_FIELD	0
102 static const u8 pidff_device_gain[] = { 0x7e };
103 
104 #define PID_RAM_POOL_SIZE	0
105 #define PID_SIMULTANEOUS_MAX	1
106 #define PID_DEVICE_MANAGED_POOL	2
107 static const u8 pidff_pool[] = { 0x80, 0x83, 0xa9 };
108 
109 /* Special field key tables used to put special field keys into arrays */
110 
111 #define PID_ENABLE_ACTUATORS	0
112 #define PID_RESET		1
113 static const u8 pidff_device_control[] = { 0x97, 0x9a };
114 
115 #define PID_CONSTANT	0
116 #define PID_RAMP	1
117 #define PID_SQUARE	2
118 #define PID_SINE	3
119 #define PID_TRIANGLE	4
120 #define PID_SAW_UP	5
121 #define PID_SAW_DOWN	6
122 #define PID_SPRING	7
123 #define PID_DAMPER	8
124 #define PID_INERTIA	9
125 #define PID_FRICTION	10
126 static const u8 pidff_effect_types[] = {
127 	0x26, 0x27, 0x30, 0x31, 0x32, 0x33, 0x34,
128 	0x40, 0x41, 0x42, 0x43
129 };
130 
131 #define PID_BLOCK_LOAD_SUCCESS	0
132 #define PID_BLOCK_LOAD_FULL	1
133 static const u8 pidff_block_load_status[] = { 0x8c, 0x8d };
134 
135 #define PID_EFFECT_START	0
136 #define PID_EFFECT_STOP		1
137 static const u8 pidff_effect_operation_status[] = { 0x79, 0x7b };
138 
139 struct pidff_usage {
140 	struct hid_field *field;
141 	s32 *value;
142 };
143 
144 struct pidff_device {
145 	struct hid_device *hid;
146 
147 	struct hid_report *reports[sizeof(pidff_reports)];
148 
149 	struct pidff_usage set_effect[sizeof(pidff_set_effect)];
150 	struct pidff_usage set_envelope[sizeof(pidff_set_envelope)];
151 	struct pidff_usage set_condition[sizeof(pidff_set_condition)];
152 	struct pidff_usage set_periodic[sizeof(pidff_set_periodic)];
153 	struct pidff_usage set_constant[sizeof(pidff_set_constant)];
154 	struct pidff_usage set_ramp[sizeof(pidff_set_ramp)];
155 
156 	struct pidff_usage device_gain[sizeof(pidff_device_gain)];
157 	struct pidff_usage block_load[sizeof(pidff_block_load)];
158 	struct pidff_usage pool[sizeof(pidff_pool)];
159 	struct pidff_usage effect_operation[sizeof(pidff_effect_operation)];
160 	struct pidff_usage block_free[sizeof(pidff_block_free)];
161 
162 	/* Special field is a field that is not composed of
163 	   usage<->value pairs that pidff_usage values are */
164 
165 	/* Special field in create_new_effect */
166 	struct hid_field *create_new_effect_type;
167 
168 	/* Special fields in set_effect */
169 	struct hid_field *set_effect_type;
170 	struct hid_field *effect_direction;
171 
172 	/* Special field in device_control */
173 	struct hid_field *device_control;
174 
175 	/* Special field in block_load */
176 	struct hid_field *block_load_status;
177 
178 	/* Special field in effect_operation */
179 	struct hid_field *effect_operation_status;
180 
181 	int control_id[sizeof(pidff_device_control)];
182 	int type_id[sizeof(pidff_effect_types)];
183 	int status_id[sizeof(pidff_block_load_status)];
184 	int operation_id[sizeof(pidff_effect_operation_status)];
185 
186 	int pid_id[PID_EFFECTS_MAX];
187 
188 	u32 quirks;
189 };
190 
191 /*
192  * Clamp value for a given field
193  */
194 static s32 pidff_clamp(s32 i, struct hid_field *field)
195 {
196 	s32 clamped = clamp(i, field->logical_minimum, field->logical_maximum);
197 	pr_debug("clamped from %d to %d", i, clamped);
198 	return clamped;
199 }
200 
201 /*
202  * Scale an unsigned value with range 0..max for the given field
203  */
204 static 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  */
213 static 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 
220 static void pidff_set(struct pidff_usage *usage, u16 value)
221 {
222 	usage->value[0] = pidff_rescale(value, 0xffff, usage->field);
223 	pr_debug("calculated from %d to %d\n", value, usage->value[0]);
224 }
225 
226 static 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 	pr_debug("calculated from %d to %d\n", value, usage->value[0]);
239 }
240 
241 /*
242  * Send envelope report to the device
243  */
244 static 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 	hid_dbg(pidff->hid, "attack %u => %d\n",
263 		envelope->attack_level,
264 		pidff->set_envelope[PID_ATTACK_LEVEL].value[0]);
265 
266 	hid_hw_request(pidff->hid, pidff->reports[PID_SET_ENVELOPE],
267 			HID_REQ_SET_REPORT);
268 }
269 
270 /*
271  * Test if the new envelope differs from old one
272  */
273 static int pidff_needs_set_envelope(struct ff_envelope *envelope,
274 				    struct ff_envelope *old)
275 {
276 	bool needs_new_envelope;
277 	needs_new_envelope = envelope->attack_level  != 0 ||
278 			     envelope->fade_level    != 0 ||
279 			     envelope->attack_length != 0 ||
280 			     envelope->fade_length   != 0;
281 
282 	if (!needs_new_envelope)
283 		return false;
284 
285 	if (!old)
286 		return needs_new_envelope;
287 
288 	return envelope->attack_level  != old->attack_level  ||
289 	       envelope->fade_level    != old->fade_level    ||
290 	       envelope->attack_length != old->attack_length ||
291 	       envelope->fade_length   != old->fade_length;
292 }
293 
294 /*
295  * Send constant force report to the device
296  */
297 static void pidff_set_constant_force_report(struct pidff_device *pidff,
298 					    struct ff_effect *effect)
299 {
300 	pidff->set_constant[PID_EFFECT_BLOCK_INDEX].value[0] =
301 		pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0];
302 	pidff_set_signed(&pidff->set_constant[PID_MAGNITUDE],
303 			 effect->u.constant.level);
304 
305 	hid_hw_request(pidff->hid, pidff->reports[PID_SET_CONSTANT],
306 			HID_REQ_SET_REPORT);
307 }
308 
309 /*
310  * Test if the constant parameters have changed between effects
311  */
312 static int pidff_needs_set_constant(struct ff_effect *effect,
313 				    struct ff_effect *old)
314 {
315 	return effect->u.constant.level != old->u.constant.level;
316 }
317 
318 /*
319  * Send set effect report to the device
320  */
321 static void pidff_set_effect_report(struct pidff_device *pidff,
322 				    struct ff_effect *effect)
323 {
324 	pidff->set_effect[PID_EFFECT_BLOCK_INDEX].value[0] =
325 		pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0];
326 	pidff->set_effect_type->value[0] =
327 		pidff->create_new_effect_type->value[0];
328 
329 	/* Convert infinite length from Linux API (0)
330 	   to PID standard (NULL) if needed */
331 	pidff->set_effect[PID_DURATION].value[0] =
332 		effect->replay.length == 0 ? PID_INFINITE : effect->replay.length;
333 
334 	pidff->set_effect[PID_TRIGGER_BUTTON].value[0] = effect->trigger.button;
335 	pidff->set_effect[PID_TRIGGER_REPEAT_INT].value[0] =
336 		effect->trigger.interval;
337 	pidff->set_effect[PID_GAIN].value[0] =
338 		pidff->set_effect[PID_GAIN].field->logical_maximum;
339 	pidff->set_effect[PID_DIRECTION_ENABLE].value[0] = 1;
340 	pidff->effect_direction->value[0] =
341 		pidff_rescale(effect->direction, 0xffff,
342 				pidff->effect_direction);
343 
344 	/* Omit setting delay field if it's missing */
345 	if (!(pidff->quirks & HID_PIDFF_QUIRK_MISSING_DELAY))
346 		pidff->set_effect[PID_START_DELAY].value[0] = effect->replay.delay;
347 
348 	hid_hw_request(pidff->hid, pidff->reports[PID_SET_EFFECT],
349 			HID_REQ_SET_REPORT);
350 }
351 
352 /*
353  * Test if the values used in set_effect have changed
354  */
355 static int pidff_needs_set_effect(struct ff_effect *effect,
356 				  struct ff_effect *old)
357 {
358 	return effect->replay.length != old->replay.length ||
359 	       effect->trigger.interval != old->trigger.interval ||
360 	       effect->trigger.button != old->trigger.button ||
361 	       effect->direction != old->direction ||
362 	       effect->replay.delay != old->replay.delay;
363 }
364 
365 /*
366  * Send periodic effect report to the device
367  */
368 static void pidff_set_periodic_report(struct pidff_device *pidff,
369 				      struct ff_effect *effect)
370 {
371 	pidff->set_periodic[PID_EFFECT_BLOCK_INDEX].value[0] =
372 		pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0];
373 	pidff_set_signed(&pidff->set_periodic[PID_MAGNITUDE],
374 			 effect->u.periodic.magnitude);
375 	pidff_set_signed(&pidff->set_periodic[PID_OFFSET],
376 			 effect->u.periodic.offset);
377 	pidff_set(&pidff->set_periodic[PID_PHASE], effect->u.periodic.phase);
378 
379 	/* Clamp period to ensure the device can play the effect */
380 	pidff->set_periodic[PID_PERIOD].value[0] =
381 		pidff_clamp(effect->u.periodic.period,
382 			pidff->set_periodic[PID_PERIOD].field);
383 
384 	hid_hw_request(pidff->hid, pidff->reports[PID_SET_PERIODIC],
385 			HID_REQ_SET_REPORT);
386 
387 }
388 
389 /*
390  * Test if periodic effect parameters have changed
391  */
392 static int pidff_needs_set_periodic(struct ff_effect *effect,
393 				    struct ff_effect *old)
394 {
395 	return effect->u.periodic.magnitude != old->u.periodic.magnitude ||
396 	       effect->u.periodic.offset != old->u.periodic.offset ||
397 	       effect->u.periodic.phase != old->u.periodic.phase ||
398 	       effect->u.periodic.period != old->u.periodic.period;
399 }
400 
401 /*
402  * Send condition effect reports to the device
403  */
404 static void pidff_set_condition_report(struct pidff_device *pidff,
405 				       struct ff_effect *effect)
406 {
407 	int i, max_axis;
408 
409 	/* Devices missing Parameter Block Offset can only have one axis */
410 	max_axis = pidff->quirks & HID_PIDFF_QUIRK_MISSING_PBO ? 1 : 2;
411 
412 	pidff->set_condition[PID_EFFECT_BLOCK_INDEX].value[0] =
413 		pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0];
414 
415 	for (i = 0; i < max_axis; i++) {
416 		/* Omit Parameter Block Offset if missing */
417 		if (!(pidff->quirks & HID_PIDFF_QUIRK_MISSING_PBO))
418 			pidff->set_condition[PID_PARAM_BLOCK_OFFSET].value[0] = i;
419 
420 		pidff_set_signed(&pidff->set_condition[PID_CP_OFFSET],
421 				 effect->u.condition[i].center);
422 		pidff_set_signed(&pidff->set_condition[PID_POS_COEFFICIENT],
423 				 effect->u.condition[i].right_coeff);
424 		pidff_set_signed(&pidff->set_condition[PID_NEG_COEFFICIENT],
425 				 effect->u.condition[i].left_coeff);
426 		pidff_set(&pidff->set_condition[PID_POS_SATURATION],
427 			  effect->u.condition[i].right_saturation);
428 		pidff_set(&pidff->set_condition[PID_NEG_SATURATION],
429 			  effect->u.condition[i].left_saturation);
430 		pidff_set(&pidff->set_condition[PID_DEAD_BAND],
431 			  effect->u.condition[i].deadband);
432 		hid_hw_request(pidff->hid, pidff->reports[PID_SET_CONDITION],
433 				HID_REQ_SET_REPORT);
434 	}
435 }
436 
437 /*
438  * Test if condition effect parameters have changed
439  */
440 static int pidff_needs_set_condition(struct ff_effect *effect,
441 				     struct ff_effect *old)
442 {
443 	int i;
444 	int ret = 0;
445 
446 	for (i = 0; i < 2; i++) {
447 		struct ff_condition_effect *cond = &effect->u.condition[i];
448 		struct ff_condition_effect *old_cond = &old->u.condition[i];
449 
450 		ret |= cond->center != old_cond->center ||
451 		       cond->right_coeff != old_cond->right_coeff ||
452 		       cond->left_coeff != old_cond->left_coeff ||
453 		       cond->right_saturation != old_cond->right_saturation ||
454 		       cond->left_saturation != old_cond->left_saturation ||
455 		       cond->deadband != old_cond->deadband;
456 	}
457 
458 	return ret;
459 }
460 
461 /*
462  * Send ramp force report to the device
463  */
464 static void pidff_set_ramp_force_report(struct pidff_device *pidff,
465 					struct ff_effect *effect)
466 {
467 	pidff->set_ramp[PID_EFFECT_BLOCK_INDEX].value[0] =
468 		pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0];
469 	pidff_set_signed(&pidff->set_ramp[PID_RAMP_START],
470 			 effect->u.ramp.start_level);
471 	pidff_set_signed(&pidff->set_ramp[PID_RAMP_END],
472 			 effect->u.ramp.end_level);
473 	hid_hw_request(pidff->hid, pidff->reports[PID_SET_RAMP],
474 			HID_REQ_SET_REPORT);
475 }
476 
477 /*
478  * Test if ramp force parameters have changed
479  */
480 static int pidff_needs_set_ramp(struct ff_effect *effect, struct ff_effect *old)
481 {
482 	return effect->u.ramp.start_level != old->u.ramp.start_level ||
483 	       effect->u.ramp.end_level != old->u.ramp.end_level;
484 }
485 
486 /*
487  * Send a request for effect upload to the device
488  *
489  * Returns 0 if device reported success, -ENOSPC if the device reported memory
490  * is full. Upon unknown response the function will retry for 60 times, if
491  * still unsuccessful -EIO is returned.
492  */
493 static int pidff_request_effect_upload(struct pidff_device *pidff, int efnum)
494 {
495 	int j;
496 
497 	pidff->create_new_effect_type->value[0] = efnum;
498 	hid_hw_request(pidff->hid, pidff->reports[PID_CREATE_NEW_EFFECT],
499 			HID_REQ_SET_REPORT);
500 	hid_dbg(pidff->hid, "create_new_effect sent, type: %d\n", efnum);
501 
502 	pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0] = 0;
503 	pidff->block_load_status->value[0] = 0;
504 	hid_hw_wait(pidff->hid);
505 
506 	for (j = 0; j < 60; j++) {
507 		hid_dbg(pidff->hid, "pid_block_load requested\n");
508 		hid_hw_request(pidff->hid, pidff->reports[PID_BLOCK_LOAD],
509 				HID_REQ_GET_REPORT);
510 		hid_hw_wait(pidff->hid);
511 		if (pidff->block_load_status->value[0] ==
512 		    pidff->status_id[PID_BLOCK_LOAD_SUCCESS]) {
513 			hid_dbg(pidff->hid, "device reported free memory: %d bytes\n",
514 				 pidff->block_load[PID_RAM_POOL_AVAILABLE].value ?
515 				 pidff->block_load[PID_RAM_POOL_AVAILABLE].value[0] : -1);
516 			return 0;
517 		}
518 		if (pidff->block_load_status->value[0] ==
519 		    pidff->status_id[PID_BLOCK_LOAD_FULL]) {
520 			hid_dbg(pidff->hid, "not enough memory free: %d bytes\n",
521 				pidff->block_load[PID_RAM_POOL_AVAILABLE].value ?
522 				pidff->block_load[PID_RAM_POOL_AVAILABLE].value[0] : -1);
523 			return -ENOSPC;
524 		}
525 	}
526 	hid_err(pidff->hid, "pid_block_load failed 60 times\n");
527 	return -EIO;
528 }
529 
530 /*
531  * Play the effect with PID id n times
532  */
533 static void pidff_playback_pid(struct pidff_device *pidff, int pid_id, int n)
534 {
535 	pidff->effect_operation[PID_EFFECT_BLOCK_INDEX].value[0] = pid_id;
536 
537 	if (n == 0) {
538 		pidff->effect_operation_status->value[0] =
539 			pidff->operation_id[PID_EFFECT_STOP];
540 	} else {
541 		pidff->effect_operation_status->value[0] =
542 			pidff->operation_id[PID_EFFECT_START];
543 		pidff->effect_operation[PID_LOOP_COUNT].value[0] = n;
544 	}
545 
546 	hid_hw_request(pidff->hid, pidff->reports[PID_EFFECT_OPERATION],
547 			HID_REQ_SET_REPORT);
548 }
549 
550 /*
551  * Play the effect with effect id @effect_id for @value times
552  */
553 static int pidff_playback(struct input_dev *dev, int effect_id, int value)
554 {
555 	struct pidff_device *pidff = dev->ff->private;
556 
557 	pidff_playback_pid(pidff, pidff->pid_id[effect_id], value);
558 
559 	return 0;
560 }
561 
562 /*
563  * Erase effect with PID id
564  */
565 static void pidff_erase_pid(struct pidff_device *pidff, int pid_id)
566 {
567 	pidff->block_free[PID_EFFECT_BLOCK_INDEX].value[0] = pid_id;
568 	hid_hw_request(pidff->hid, pidff->reports[PID_BLOCK_FREE],
569 			HID_REQ_SET_REPORT);
570 }
571 
572 /*
573  * Stop and erase effect with effect_id
574  */
575 static int pidff_erase_effect(struct input_dev *dev, int effect_id)
576 {
577 	struct pidff_device *pidff = dev->ff->private;
578 	int pid_id = pidff->pid_id[effect_id];
579 
580 	hid_dbg(pidff->hid, "starting to erase %d/%d\n",
581 		effect_id, pidff->pid_id[effect_id]);
582 	/* Wait for the queue to clear. We do not want a full fifo to
583 	   prevent the effect removal. */
584 	hid_hw_wait(pidff->hid);
585 	pidff_playback_pid(pidff, pid_id, 0);
586 	pidff_erase_pid(pidff, pid_id);
587 
588 	return 0;
589 }
590 
591 /*
592  * Effect upload handler
593  */
594 static int pidff_upload_effect(struct input_dev *dev, struct ff_effect *effect,
595 			       struct ff_effect *old)
596 {
597 	struct pidff_device *pidff = dev->ff->private;
598 	int type_id;
599 	int error;
600 
601 	pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0] = 0;
602 	if (old) {
603 		pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0] =
604 			pidff->pid_id[effect->id];
605 	}
606 
607 	switch (effect->type) {
608 	case FF_CONSTANT:
609 		if (!old) {
610 			error = pidff_request_effect_upload(pidff,
611 					pidff->type_id[PID_CONSTANT]);
612 			if (error)
613 				return error;
614 		}
615 		if (!old || pidff_needs_set_effect(effect, old))
616 			pidff_set_effect_report(pidff, effect);
617 		if (!old || pidff_needs_set_constant(effect, old))
618 			pidff_set_constant_force_report(pidff, effect);
619 		if (pidff_needs_set_envelope(&effect->u.constant.envelope,
620 					old ? &old->u.constant.envelope : NULL))
621 			pidff_set_envelope_report(pidff, &effect->u.constant.envelope);
622 		break;
623 
624 	case FF_PERIODIC:
625 		if (!old) {
626 			switch (effect->u.periodic.waveform) {
627 			case FF_SQUARE:
628 				type_id = PID_SQUARE;
629 				break;
630 			case FF_TRIANGLE:
631 				type_id = PID_TRIANGLE;
632 				break;
633 			case FF_SINE:
634 				type_id = PID_SINE;
635 				break;
636 			case FF_SAW_UP:
637 				type_id = PID_SAW_UP;
638 				break;
639 			case FF_SAW_DOWN:
640 				type_id = PID_SAW_DOWN;
641 				break;
642 			default:
643 				hid_err(pidff->hid, "invalid waveform\n");
644 				return -EINVAL;
645 			}
646 
647 			error = pidff_request_effect_upload(pidff,
648 					pidff->type_id[type_id]);
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_periodic(effect, old))
655 			pidff_set_periodic_report(pidff, effect);
656 		if (pidff_needs_set_envelope(&effect->u.periodic.envelope,
657 					old ? &old->u.periodic.envelope : NULL))
658 			pidff_set_envelope_report(pidff, &effect->u.periodic.envelope);
659 		break;
660 
661 	case FF_RAMP:
662 		if (!old) {
663 			error = pidff_request_effect_upload(pidff,
664 					pidff->type_id[PID_RAMP]);
665 			if (error)
666 				return error;
667 		}
668 		if (!old || pidff_needs_set_effect(effect, old))
669 			pidff_set_effect_report(pidff, effect);
670 		if (!old || pidff_needs_set_ramp(effect, old))
671 			pidff_set_ramp_force_report(pidff, effect);
672 		if (pidff_needs_set_envelope(&effect->u.ramp.envelope,
673 					old ? &old->u.ramp.envelope : NULL))
674 			pidff_set_envelope_report(pidff, &effect->u.ramp.envelope);
675 		break;
676 
677 	case FF_SPRING:
678 		if (!old) {
679 			error = pidff_request_effect_upload(pidff,
680 					pidff->type_id[PID_SPRING]);
681 			if (error)
682 				return error;
683 		}
684 		if (!old || pidff_needs_set_effect(effect, old))
685 			pidff_set_effect_report(pidff, effect);
686 		if (!old || pidff_needs_set_condition(effect, old))
687 			pidff_set_condition_report(pidff, effect);
688 		break;
689 
690 	case FF_FRICTION:
691 		if (!old) {
692 			error = pidff_request_effect_upload(pidff,
693 					pidff->type_id[PID_FRICTION]);
694 			if (error)
695 				return error;
696 		}
697 		if (!old || pidff_needs_set_effect(effect, old))
698 			pidff_set_effect_report(pidff, effect);
699 		if (!old || pidff_needs_set_condition(effect, old))
700 			pidff_set_condition_report(pidff, effect);
701 		break;
702 
703 	case FF_DAMPER:
704 		if (!old) {
705 			error = pidff_request_effect_upload(pidff,
706 					pidff->type_id[PID_DAMPER]);
707 			if (error)
708 				return error;
709 		}
710 		if (!old || pidff_needs_set_effect(effect, old))
711 			pidff_set_effect_report(pidff, effect);
712 		if (!old || pidff_needs_set_condition(effect, old))
713 			pidff_set_condition_report(pidff, effect);
714 		break;
715 
716 	case FF_INERTIA:
717 		if (!old) {
718 			error = pidff_request_effect_upload(pidff,
719 					pidff->type_id[PID_INERTIA]);
720 			if (error)
721 				return error;
722 		}
723 		if (!old || pidff_needs_set_effect(effect, old))
724 			pidff_set_effect_report(pidff, effect);
725 		if (!old || pidff_needs_set_condition(effect, old))
726 			pidff_set_condition_report(pidff, effect);
727 		break;
728 
729 	default:
730 		hid_err(pidff->hid, "invalid type\n");
731 		return -EINVAL;
732 	}
733 
734 	if (!old)
735 		pidff->pid_id[effect->id] =
736 		    pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0];
737 
738 	hid_dbg(pidff->hid, "uploaded\n");
739 
740 	return 0;
741 }
742 
743 /*
744  * set_gain() handler
745  */
746 static void pidff_set_gain(struct input_dev *dev, u16 gain)
747 {
748 	struct pidff_device *pidff = dev->ff->private;
749 
750 	pidff_set(&pidff->device_gain[PID_DEVICE_GAIN_FIELD], gain);
751 	hid_hw_request(pidff->hid, pidff->reports[PID_DEVICE_GAIN],
752 			HID_REQ_SET_REPORT);
753 }
754 
755 static void pidff_autocenter(struct pidff_device *pidff, u16 magnitude)
756 {
757 	struct hid_field *field =
758 		pidff->block_load[PID_EFFECT_BLOCK_INDEX].field;
759 
760 	if (!magnitude) {
761 		pidff_playback_pid(pidff, field->logical_minimum, 0);
762 		return;
763 	}
764 
765 	pidff_playback_pid(pidff, field->logical_minimum, 1);
766 
767 	pidff->set_effect[PID_EFFECT_BLOCK_INDEX].value[0] =
768 		pidff->block_load[PID_EFFECT_BLOCK_INDEX].field->logical_minimum;
769 	pidff->set_effect_type->value[0] = pidff->type_id[PID_SPRING];
770 	pidff->set_effect[PID_DURATION].value[0] = 0;
771 	pidff->set_effect[PID_TRIGGER_BUTTON].value[0] = 0;
772 	pidff->set_effect[PID_TRIGGER_REPEAT_INT].value[0] = 0;
773 	pidff_set(&pidff->set_effect[PID_GAIN], magnitude);
774 	pidff->set_effect[PID_DIRECTION_ENABLE].value[0] = 1;
775 
776 	/* Omit setting delay field if it's missing */
777 	if (!(pidff->quirks & HID_PIDFF_QUIRK_MISSING_DELAY))
778 		pidff->set_effect[PID_START_DELAY].value[0] = 0;
779 
780 	hid_hw_request(pidff->hid, pidff->reports[PID_SET_EFFECT],
781 			HID_REQ_SET_REPORT);
782 }
783 
784 /*
785  * pidff_set_autocenter() handler
786  */
787 static void pidff_set_autocenter(struct input_dev *dev, u16 magnitude)
788 {
789 	struct pidff_device *pidff = dev->ff->private;
790 
791 	pidff_autocenter(pidff, magnitude);
792 }
793 
794 /*
795  * Find fields from a report and fill a pidff_usage
796  */
797 static int pidff_find_fields(struct pidff_usage *usage, const u8 *table,
798 			     struct hid_report *report, int count, int strict)
799 {
800 	int i, j, k, found;
801 	int return_value = 0;
802 
803 	for (k = 0; k < count; k++) {
804 		found = 0;
805 		for (i = 0; i < report->maxfield; i++) {
806 			if (report->field[i]->maxusage !=
807 			    report->field[i]->report_count) {
808 				pr_debug("maxusage and report_count do not match, skipping\n");
809 				continue;
810 			}
811 			for (j = 0; j < report->field[i]->maxusage; j++) {
812 				if (report->field[i]->usage[j].hid ==
813 				    (HID_UP_PID | table[k])) {
814 					pr_debug("found %d at %d->%d\n",
815 						 k, i, j);
816 					usage[k].field = report->field[i];
817 					usage[k].value =
818 						&report->field[i]->value[j];
819 					found = 1;
820 					break;
821 				}
822 			}
823 			if (found)
824 				break;
825 		}
826 		if (!found && table[k] == pidff_set_effect[PID_START_DELAY]) {
827 			pr_debug("Delay field not found, but that's OK\n");
828 			pr_debug("Setting MISSING_DELAY quirk\n");
829 			return_value |= HID_PIDFF_QUIRK_MISSING_DELAY;
830 		}
831 		else if (!found && table[k] == pidff_set_condition[PID_PARAM_BLOCK_OFFSET]) {
832 			pr_debug("PBO field not found, but that's OK\n");
833 			pr_debug("Setting MISSING_PBO quirk\n");
834 			return_value |= HID_PIDFF_QUIRK_MISSING_PBO;
835 		}
836 		else if (!found && strict) {
837 			pr_debug("failed to locate %d\n", k);
838 			return -1;
839 		}
840 	}
841 	return return_value;
842 }
843 
844 /*
845  * Return index into pidff_reports for the given usage
846  */
847 static int pidff_check_usage(int usage)
848 {
849 	int i;
850 
851 	for (i = 0; i < sizeof(pidff_reports); i++)
852 		if (usage == (HID_UP_PID | pidff_reports[i]))
853 			return i;
854 
855 	return -1;
856 }
857 
858 /*
859  * Find the reports and fill pidff->reports[]
860  * report_type specifies either OUTPUT or FEATURE reports
861  */
862 static void pidff_find_reports(struct hid_device *hid, int report_type,
863 			       struct pidff_device *pidff)
864 {
865 	struct hid_report *report;
866 	int i, ret;
867 
868 	list_for_each_entry(report,
869 			    &hid->report_enum[report_type].report_list, list) {
870 		if (report->maxfield < 1)
871 			continue;
872 		ret = pidff_check_usage(report->field[0]->logical);
873 		if (ret != -1) {
874 			hid_dbg(hid, "found usage 0x%02x from field->logical\n",
875 				pidff_reports[ret]);
876 			pidff->reports[ret] = report;
877 			continue;
878 		}
879 
880 		/*
881 		 * Sometimes logical collections are stacked to indicate
882 		 * different usages for the report and the field, in which
883 		 * case we want the usage of the parent. However, Linux HID
884 		 * implementation hides this fact, so we have to dig it up
885 		 * ourselves
886 		 */
887 		i = report->field[0]->usage[0].collection_index;
888 		if (i <= 0 ||
889 		    hid->collection[i - 1].type != HID_COLLECTION_LOGICAL)
890 			continue;
891 		ret = pidff_check_usage(hid->collection[i - 1].usage);
892 		if (ret != -1 && !pidff->reports[ret]) {
893 			hid_dbg(hid,
894 				"found usage 0x%02x from collection array\n",
895 				pidff_reports[ret]);
896 			pidff->reports[ret] = report;
897 		}
898 	}
899 }
900 
901 /*
902  * Test if the required reports have been found
903  */
904 static int pidff_reports_ok(struct pidff_device *pidff)
905 {
906 	int i;
907 
908 	for (i = 0; i <= PID_REQUIRED_REPORTS; i++) {
909 		if (!pidff->reports[i]) {
910 			hid_dbg(pidff->hid, "%d missing\n", i);
911 			return 0;
912 		}
913 	}
914 
915 	return 1;
916 }
917 
918 /*
919  * Find a field with a specific usage within a report
920  */
921 static struct hid_field *pidff_find_special_field(struct hid_report *report,
922 						  int usage, int enforce_min)
923 {
924 	int i;
925 
926 	for (i = 0; i < report->maxfield; i++) {
927 		if (report->field[i]->logical == (HID_UP_PID | usage) &&
928 		    report->field[i]->report_count > 0) {
929 			if (!enforce_min ||
930 			    report->field[i]->logical_minimum == 1)
931 				return report->field[i];
932 			else {
933 				pr_err("logical_minimum is not 1 as it should be\n");
934 				return NULL;
935 			}
936 		}
937 	}
938 	return NULL;
939 }
940 
941 /*
942  * Fill a pidff->*_id struct table
943  */
944 static int pidff_find_special_keys(int *keys, struct hid_field *fld,
945 				   const u8 *usagetable, int count)
946 {
947 
948 	int i, j;
949 	int found = 0;
950 
951 	for (i = 0; i < count; i++) {
952 		for (j = 0; j < fld->maxusage; j++) {
953 			if (fld->usage[j].hid == (HID_UP_PID | usagetable[i])) {
954 				keys[i] = j + 1;
955 				found++;
956 				break;
957 			}
958 		}
959 	}
960 	return found;
961 }
962 
963 #define PIDFF_FIND_SPECIAL_KEYS(keys, field, name) \
964 	pidff_find_special_keys(pidff->keys, pidff->field, pidff_ ## name, \
965 		sizeof(pidff_ ## name))
966 
967 /*
968  * Find and check the special fields
969  */
970 static int pidff_find_special_fields(struct pidff_device *pidff)
971 {
972 	hid_dbg(pidff->hid, "finding special fields\n");
973 
974 	pidff->create_new_effect_type =
975 		pidff_find_special_field(pidff->reports[PID_CREATE_NEW_EFFECT],
976 					 0x25, 1);
977 	pidff->set_effect_type =
978 		pidff_find_special_field(pidff->reports[PID_SET_EFFECT],
979 					 0x25, 1);
980 	pidff->effect_direction =
981 		pidff_find_special_field(pidff->reports[PID_SET_EFFECT],
982 					 0x57, 0);
983 	pidff->device_control =
984 		pidff_find_special_field(pidff->reports[PID_DEVICE_CONTROL],
985 			0x96, !(pidff->quirks & HID_PIDFF_QUIRK_PERMISSIVE_CONTROL));
986 
987 	pidff->block_load_status =
988 		pidff_find_special_field(pidff->reports[PID_BLOCK_LOAD],
989 					 0x8b, 1);
990 	pidff->effect_operation_status =
991 		pidff_find_special_field(pidff->reports[PID_EFFECT_OPERATION],
992 					 0x78, 1);
993 
994 	hid_dbg(pidff->hid, "search done\n");
995 
996 	if (!pidff->create_new_effect_type || !pidff->set_effect_type) {
997 		hid_err(pidff->hid, "effect lists not found\n");
998 		return -1;
999 	}
1000 
1001 	if (!pidff->effect_direction) {
1002 		hid_err(pidff->hid, "direction field not found\n");
1003 		return -1;
1004 	}
1005 
1006 	if (!pidff->device_control) {
1007 		hid_err(pidff->hid, "device control field not found\n");
1008 		return -1;
1009 	}
1010 
1011 	if (!pidff->block_load_status) {
1012 		hid_err(pidff->hid, "block load status field not found\n");
1013 		return -1;
1014 	}
1015 
1016 	if (!pidff->effect_operation_status) {
1017 		hid_err(pidff->hid, "effect operation field not found\n");
1018 		return -1;
1019 	}
1020 
1021 	pidff_find_special_keys(pidff->control_id, pidff->device_control,
1022 				pidff_device_control,
1023 				sizeof(pidff_device_control));
1024 
1025 	PIDFF_FIND_SPECIAL_KEYS(control_id, device_control, device_control);
1026 
1027 	if (!PIDFF_FIND_SPECIAL_KEYS(type_id, create_new_effect_type,
1028 				     effect_types)) {
1029 		hid_err(pidff->hid, "no effect types found\n");
1030 		return -1;
1031 	}
1032 
1033 	if (PIDFF_FIND_SPECIAL_KEYS(status_id, block_load_status,
1034 				    block_load_status) !=
1035 			sizeof(pidff_block_load_status)) {
1036 		hid_err(pidff->hid,
1037 			"block load status identifiers not found\n");
1038 		return -1;
1039 	}
1040 
1041 	if (PIDFF_FIND_SPECIAL_KEYS(operation_id, effect_operation_status,
1042 				    effect_operation_status) !=
1043 			sizeof(pidff_effect_operation_status)) {
1044 		hid_err(pidff->hid, "effect operation identifiers not found\n");
1045 		return -1;
1046 	}
1047 
1048 	return 0;
1049 }
1050 
1051 /*
1052  * Find the implemented effect types
1053  */
1054 static int pidff_find_effects(struct pidff_device *pidff,
1055 			      struct input_dev *dev)
1056 {
1057 	int i;
1058 
1059 	for (i = 0; i < sizeof(pidff_effect_types); i++) {
1060 		int pidff_type = pidff->type_id[i];
1061 		if (pidff->set_effect_type->usage[pidff_type].hid !=
1062 		    pidff->create_new_effect_type->usage[pidff_type].hid) {
1063 			hid_err(pidff->hid,
1064 				"effect type number %d is invalid\n", i);
1065 			return -1;
1066 		}
1067 	}
1068 
1069 	if (pidff->type_id[PID_CONSTANT])
1070 		set_bit(FF_CONSTANT, dev->ffbit);
1071 	if (pidff->type_id[PID_RAMP])
1072 		set_bit(FF_RAMP, dev->ffbit);
1073 	if (pidff->type_id[PID_SQUARE]) {
1074 		set_bit(FF_SQUARE, dev->ffbit);
1075 		set_bit(FF_PERIODIC, dev->ffbit);
1076 	}
1077 	if (pidff->type_id[PID_SINE]) {
1078 		set_bit(FF_SINE, dev->ffbit);
1079 		set_bit(FF_PERIODIC, dev->ffbit);
1080 	}
1081 	if (pidff->type_id[PID_TRIANGLE]) {
1082 		set_bit(FF_TRIANGLE, dev->ffbit);
1083 		set_bit(FF_PERIODIC, dev->ffbit);
1084 	}
1085 	if (pidff->type_id[PID_SAW_UP]) {
1086 		set_bit(FF_SAW_UP, dev->ffbit);
1087 		set_bit(FF_PERIODIC, dev->ffbit);
1088 	}
1089 	if (pidff->type_id[PID_SAW_DOWN]) {
1090 		set_bit(FF_SAW_DOWN, dev->ffbit);
1091 		set_bit(FF_PERIODIC, dev->ffbit);
1092 	}
1093 	if (pidff->type_id[PID_SPRING])
1094 		set_bit(FF_SPRING, dev->ffbit);
1095 	if (pidff->type_id[PID_DAMPER])
1096 		set_bit(FF_DAMPER, dev->ffbit);
1097 	if (pidff->type_id[PID_INERTIA])
1098 		set_bit(FF_INERTIA, dev->ffbit);
1099 	if (pidff->type_id[PID_FRICTION])
1100 		set_bit(FF_FRICTION, dev->ffbit);
1101 
1102 	return 0;
1103 
1104 }
1105 
1106 #define PIDFF_FIND_FIELDS(name, report, strict) \
1107 	pidff_find_fields(pidff->name, pidff_ ## name, \
1108 		pidff->reports[report], \
1109 		sizeof(pidff_ ## name), strict)
1110 
1111 /*
1112  * Fill and check the pidff_usages
1113  */
1114 static int pidff_init_fields(struct pidff_device *pidff, struct input_dev *dev)
1115 {
1116 	int status = 0;
1117 
1118 	/* Save info about the device not having the DELAY ffb field. */
1119 	status = PIDFF_FIND_FIELDS(set_effect, PID_SET_EFFECT, 1);
1120 	if (status == -1) {
1121 		hid_err(pidff->hid, "unknown set_effect report layout\n");
1122 		return -ENODEV;
1123 	}
1124 	pidff->quirks |= status;
1125 
1126 	if (status & HID_PIDFF_QUIRK_MISSING_DELAY)
1127 		hid_dbg(pidff->hid, "Adding MISSING_DELAY quirk\n");
1128 
1129 
1130 	PIDFF_FIND_FIELDS(block_load, PID_BLOCK_LOAD, 0);
1131 	if (!pidff->block_load[PID_EFFECT_BLOCK_INDEX].value) {
1132 		hid_err(pidff->hid, "unknown pid_block_load report layout\n");
1133 		return -ENODEV;
1134 	}
1135 
1136 	if (PIDFF_FIND_FIELDS(effect_operation, PID_EFFECT_OPERATION, 1)) {
1137 		hid_err(pidff->hid, "unknown effect_operation report layout\n");
1138 		return -ENODEV;
1139 	}
1140 
1141 	if (PIDFF_FIND_FIELDS(block_free, PID_BLOCK_FREE, 1)) {
1142 		hid_err(pidff->hid, "unknown pid_block_free report layout\n");
1143 		return -ENODEV;
1144 	}
1145 
1146 	if (pidff_find_special_fields(pidff) || pidff_find_effects(pidff, dev))
1147 		return -ENODEV;
1148 
1149 	if (PIDFF_FIND_FIELDS(set_envelope, PID_SET_ENVELOPE, 1)) {
1150 		if (test_and_clear_bit(FF_CONSTANT, dev->ffbit))
1151 			hid_warn(pidff->hid,
1152 				 "has constant effect but no envelope\n");
1153 		if (test_and_clear_bit(FF_RAMP, dev->ffbit))
1154 			hid_warn(pidff->hid,
1155 				 "has ramp effect but no envelope\n");
1156 
1157 		if (test_and_clear_bit(FF_PERIODIC, dev->ffbit))
1158 			hid_warn(pidff->hid,
1159 				 "has periodic effect but no envelope\n");
1160 	}
1161 
1162 	if (test_bit(FF_CONSTANT, dev->ffbit) &&
1163 	    PIDFF_FIND_FIELDS(set_constant, PID_SET_CONSTANT, 1)) {
1164 		hid_warn(pidff->hid, "unknown constant effect layout\n");
1165 		clear_bit(FF_CONSTANT, dev->ffbit);
1166 	}
1167 
1168 	if (test_bit(FF_RAMP, dev->ffbit) &&
1169 	    PIDFF_FIND_FIELDS(set_ramp, PID_SET_RAMP, 1)) {
1170 		hid_warn(pidff->hid, "unknown ramp effect layout\n");
1171 		clear_bit(FF_RAMP, dev->ffbit);
1172 	}
1173 
1174 	if (test_bit(FF_SPRING, dev->ffbit) ||
1175 	    test_bit(FF_DAMPER, dev->ffbit) ||
1176 	    test_bit(FF_FRICTION, dev->ffbit) ||
1177 	    test_bit(FF_INERTIA, dev->ffbit)) {
1178 		status = PIDFF_FIND_FIELDS(set_condition, PID_SET_CONDITION, 1);
1179 
1180 		if (status < 0) {
1181 			hid_warn(pidff->hid, "unknown condition effect layout\n");
1182 			clear_bit(FF_SPRING, dev->ffbit);
1183 			clear_bit(FF_DAMPER, dev->ffbit);
1184 			clear_bit(FF_FRICTION, dev->ffbit);
1185 			clear_bit(FF_INERTIA, dev->ffbit);
1186 		}
1187 		pidff->quirks |= status;
1188 	}
1189 
1190 	if (test_bit(FF_PERIODIC, dev->ffbit) &&
1191 	    PIDFF_FIND_FIELDS(set_periodic, PID_SET_PERIODIC, 1)) {
1192 		hid_warn(pidff->hid, "unknown periodic effect layout\n");
1193 		clear_bit(FF_PERIODIC, dev->ffbit);
1194 	}
1195 
1196 	PIDFF_FIND_FIELDS(pool, PID_POOL, 0);
1197 
1198 	if (!PIDFF_FIND_FIELDS(device_gain, PID_DEVICE_GAIN, 1))
1199 		set_bit(FF_GAIN, dev->ffbit);
1200 
1201 	return 0;
1202 }
1203 
1204 /*
1205  * Reset the device
1206  */
1207 static void pidff_reset(struct pidff_device *pidff)
1208 {
1209 	struct hid_device *hid = pidff->hid;
1210 	int i = 0;
1211 
1212 	pidff->device_control->value[0] = pidff->control_id[PID_RESET];
1213 	/* We reset twice as sometimes hid_wait_io isn't waiting long enough */
1214 	hid_hw_request(hid, pidff->reports[PID_DEVICE_CONTROL], HID_REQ_SET_REPORT);
1215 	hid_hw_wait(hid);
1216 	hid_hw_request(hid, pidff->reports[PID_DEVICE_CONTROL], HID_REQ_SET_REPORT);
1217 	hid_hw_wait(hid);
1218 
1219 	pidff->device_control->value[0] =
1220 		pidff->control_id[PID_ENABLE_ACTUATORS];
1221 	hid_hw_request(hid, pidff->reports[PID_DEVICE_CONTROL], HID_REQ_SET_REPORT);
1222 	hid_hw_wait(hid);
1223 
1224 	/* pool report is sometimes messed up, refetch it */
1225 	hid_hw_request(hid, pidff->reports[PID_POOL], HID_REQ_GET_REPORT);
1226 	hid_hw_wait(hid);
1227 
1228 	if (pidff->pool[PID_SIMULTANEOUS_MAX].value) {
1229 		while (pidff->pool[PID_SIMULTANEOUS_MAX].value[0] < 2) {
1230 			if (i++ > 20) {
1231 				hid_warn(pidff->hid,
1232 					 "device reports %d simultaneous effects\n",
1233 					 pidff->pool[PID_SIMULTANEOUS_MAX].value[0]);
1234 				break;
1235 			}
1236 			hid_dbg(pidff->hid, "pid_pool requested again\n");
1237 			hid_hw_request(hid, pidff->reports[PID_POOL],
1238 					  HID_REQ_GET_REPORT);
1239 			hid_hw_wait(hid);
1240 		}
1241 	}
1242 }
1243 
1244 /*
1245  * Test if autocenter modification is using the supported method
1246  */
1247 static int pidff_check_autocenter(struct pidff_device *pidff,
1248 				  struct input_dev *dev)
1249 {
1250 	int error;
1251 
1252 	/*
1253 	 * Let's find out if autocenter modification is supported
1254 	 * Specification doesn't specify anything, so we request an
1255 	 * effect upload and cancel it immediately. If the approved
1256 	 * effect id was one above the minimum, then we assume the first
1257 	 * effect id is a built-in spring type effect used for autocenter
1258 	 */
1259 
1260 	error = pidff_request_effect_upload(pidff, 1);
1261 	if (error) {
1262 		hid_err(pidff->hid, "upload request failed\n");
1263 		return error;
1264 	}
1265 
1266 	if (pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0] ==
1267 	    pidff->block_load[PID_EFFECT_BLOCK_INDEX].field->logical_minimum + 1) {
1268 		pidff_autocenter(pidff, 0xffff);
1269 		set_bit(FF_AUTOCENTER, dev->ffbit);
1270 	} else {
1271 		hid_notice(pidff->hid,
1272 			   "device has unknown autocenter control method\n");
1273 	}
1274 
1275 	pidff_erase_pid(pidff,
1276 			pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0]);
1277 
1278 	return 0;
1279 
1280 }
1281 
1282 /*
1283  * Check if the device is PID and initialize it
1284  */
1285 int hid_pidff_init(struct hid_device *hid)
1286 {
1287 	struct pidff_device *pidff;
1288 	struct hid_input *hidinput = list_entry(hid->inputs.next,
1289 						struct hid_input, list);
1290 	struct input_dev *dev = hidinput->input;
1291 	struct ff_device *ff;
1292 	int max_effects;
1293 	int error;
1294 
1295 	hid_dbg(hid, "starting pid init\n");
1296 
1297 	if (list_empty(&hid->report_enum[HID_OUTPUT_REPORT].report_list)) {
1298 		hid_dbg(hid, "not a PID device, no output report\n");
1299 		return -ENODEV;
1300 	}
1301 
1302 	pidff = kzalloc(sizeof(*pidff), GFP_KERNEL);
1303 	if (!pidff)
1304 		return -ENOMEM;
1305 
1306 	pidff->hid = hid;
1307 
1308 	hid_device_io_start(hid);
1309 
1310 	pidff_find_reports(hid, HID_OUTPUT_REPORT, pidff);
1311 	pidff_find_reports(hid, HID_FEATURE_REPORT, pidff);
1312 
1313 	if (!pidff_reports_ok(pidff)) {
1314 		hid_dbg(hid, "reports not ok, aborting\n");
1315 		error = -ENODEV;
1316 		goto fail;
1317 	}
1318 
1319 	error = pidff_init_fields(pidff, dev);
1320 	if (error)
1321 		goto fail;
1322 
1323 	pidff_reset(pidff);
1324 
1325 	if (test_bit(FF_GAIN, dev->ffbit)) {
1326 		pidff_set(&pidff->device_gain[PID_DEVICE_GAIN_FIELD], 0xffff);
1327 		hid_hw_request(hid, pidff->reports[PID_DEVICE_GAIN],
1328 				     HID_REQ_SET_REPORT);
1329 	}
1330 
1331 	error = pidff_check_autocenter(pidff, dev);
1332 	if (error)
1333 		goto fail;
1334 
1335 	max_effects =
1336 	    pidff->block_load[PID_EFFECT_BLOCK_INDEX].field->logical_maximum -
1337 	    pidff->block_load[PID_EFFECT_BLOCK_INDEX].field->logical_minimum +
1338 	    1;
1339 	hid_dbg(hid, "max effects is %d\n", max_effects);
1340 
1341 	if (max_effects > PID_EFFECTS_MAX)
1342 		max_effects = PID_EFFECTS_MAX;
1343 
1344 	if (pidff->pool[PID_SIMULTANEOUS_MAX].value)
1345 		hid_dbg(hid, "max simultaneous effects is %d\n",
1346 			pidff->pool[PID_SIMULTANEOUS_MAX].value[0]);
1347 
1348 	if (pidff->pool[PID_RAM_POOL_SIZE].value)
1349 		hid_dbg(hid, "device memory size is %d bytes\n",
1350 			pidff->pool[PID_RAM_POOL_SIZE].value[0]);
1351 
1352 	if (pidff->pool[PID_DEVICE_MANAGED_POOL].value &&
1353 	    pidff->pool[PID_DEVICE_MANAGED_POOL].value[0] == 0) {
1354 		error = -EPERM;
1355 		hid_notice(hid,
1356 			   "device does not support device managed pool\n");
1357 		goto fail;
1358 	}
1359 
1360 	error = input_ff_create(dev, max_effects);
1361 	if (error)
1362 		goto fail;
1363 
1364 	ff = dev->ff;
1365 	ff->private = pidff;
1366 	ff->upload = pidff_upload_effect;
1367 	ff->erase = pidff_erase_effect;
1368 	ff->set_gain = pidff_set_gain;
1369 	ff->set_autocenter = pidff_set_autocenter;
1370 	ff->playback = pidff_playback;
1371 
1372 	hid_info(dev, "Force feedback for USB HID PID devices by Anssi Hannula <anssi.hannula@gmail.com>\n");
1373 	hid_dbg(dev, "Active quirks mask: 0x%x\n", pidff->quirks);
1374 
1375 	hid_device_io_stop(hid);
1376 
1377 	return 0;
1378 
1379  fail:
1380 	hid_device_io_stop(hid);
1381 
1382 	kfree(pidff);
1383 	return error;
1384 }
1385