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