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