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