11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2606bd0a8SJiri Slaby /*
3606bd0a8SJiri Slaby * Force feedback support for hid-compliant for some of the devices from
4606bd0a8SJiri Slaby * Logitech, namely:
5606bd0a8SJiri Slaby * - WingMan Cordless RumblePad
6606bd0a8SJiri Slaby * - WingMan Force 3D
7606bd0a8SJiri Slaby *
8606bd0a8SJiri Slaby * Copyright (c) 2002-2004 Johann Deneux
9606bd0a8SJiri Slaby * Copyright (c) 2006 Anssi Hannula <anssi.hannula@gmail.com>
10606bd0a8SJiri Slaby */
11606bd0a8SJiri Slaby
12606bd0a8SJiri Slaby /*
13606bd0a8SJiri Slaby *
14606bd0a8SJiri Slaby * Should you need to contact me, the author, you can do so by
15606bd0a8SJiri Slaby * e-mail - mail your message to <johann.deneux@it.uu.se>
16606bd0a8SJiri Slaby */
17606bd0a8SJiri Slaby
184291ee30SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
194291ee30SJoe Perches
20606bd0a8SJiri Slaby #include <linux/input.h>
21606bd0a8SJiri Slaby #include <linux/hid.h>
22606bd0a8SJiri Slaby
23606bd0a8SJiri Slaby #include "hid-lg.h"
24606bd0a8SJiri Slaby
25606bd0a8SJiri Slaby struct dev_type {
26606bd0a8SJiri Slaby u16 idVendor;
27606bd0a8SJiri Slaby u16 idProduct;
28606bd0a8SJiri Slaby const signed short *ff;
29606bd0a8SJiri Slaby };
30606bd0a8SJiri Slaby
31606bd0a8SJiri Slaby static const signed short ff_rumble[] = {
32606bd0a8SJiri Slaby FF_RUMBLE,
33606bd0a8SJiri Slaby -1
34606bd0a8SJiri Slaby };
35606bd0a8SJiri Slaby
36606bd0a8SJiri Slaby static const signed short ff_joystick[] = {
37606bd0a8SJiri Slaby FF_CONSTANT,
38606bd0a8SJiri Slaby -1
39606bd0a8SJiri Slaby };
40606bd0a8SJiri Slaby
41f0bca459SSergey Belyashov static const signed short ff_joystick_ac[] = {
42f0bca459SSergey Belyashov FF_CONSTANT,
43f0bca459SSergey Belyashov FF_AUTOCENTER,
44f0bca459SSergey Belyashov -1
45f0bca459SSergey Belyashov };
46f0bca459SSergey Belyashov
47606bd0a8SJiri Slaby static const struct dev_type devices[] = {
48606bd0a8SJiri Slaby { 0x046d, 0xc211, ff_rumble },
49606bd0a8SJiri Slaby { 0x046d, 0xc219, ff_rumble },
50606bd0a8SJiri Slaby { 0x046d, 0xc283, ff_joystick },
51f0bca459SSergey Belyashov { 0x046d, 0xc286, ff_joystick_ac },
5274f292caSGary Stein { 0x046d, 0xc287, ff_joystick_ac },
53fd30ea8cSJiri Kosina { 0x046d, 0xc293, ff_joystick },
54606bd0a8SJiri Slaby { 0x046d, 0xc295, ff_joystick },
55606bd0a8SJiri Slaby };
56606bd0a8SJiri Slaby
hid_lgff_play(struct input_dev * dev,void * data,struct ff_effect * effect)57606bd0a8SJiri Slaby static int hid_lgff_play(struct input_dev *dev, void *data, struct ff_effect *effect)
58606bd0a8SJiri Slaby {
59606bd0a8SJiri Slaby struct hid_device *hid = input_get_drvdata(dev);
60606bd0a8SJiri Slaby struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
61606bd0a8SJiri Slaby struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
62606bd0a8SJiri Slaby int x, y;
63606bd0a8SJiri Slaby unsigned int left, right;
64606bd0a8SJiri Slaby
65606bd0a8SJiri Slaby #define CLAMP(x) if (x < 0) x = 0; if (x > 0xff) x = 0xff
66606bd0a8SJiri Slaby
67606bd0a8SJiri Slaby switch (effect->type) {
68606bd0a8SJiri Slaby case FF_CONSTANT:
69606bd0a8SJiri Slaby x = effect->u.ramp.start_level + 0x7f; /* 0x7f is center */
70606bd0a8SJiri Slaby y = effect->u.ramp.end_level + 0x7f;
71606bd0a8SJiri Slaby CLAMP(x);
72606bd0a8SJiri Slaby CLAMP(y);
73606bd0a8SJiri Slaby report->field[0]->value[0] = 0x51;
74606bd0a8SJiri Slaby report->field[0]->value[1] = 0x08;
75606bd0a8SJiri Slaby report->field[0]->value[2] = x;
76606bd0a8SJiri Slaby report->field[0]->value[3] = y;
77606bd0a8SJiri Slaby dbg_hid("(x, y)=(%04x, %04x)\n", x, y);
78d8814272SBenjamin Tissoires hid_hw_request(hid, report, HID_REQ_SET_REPORT);
79606bd0a8SJiri Slaby break;
80606bd0a8SJiri Slaby
81606bd0a8SJiri Slaby case FF_RUMBLE:
82606bd0a8SJiri Slaby right = effect->u.rumble.strong_magnitude;
83606bd0a8SJiri Slaby left = effect->u.rumble.weak_magnitude;
84606bd0a8SJiri Slaby right = right * 0xff / 0xffff;
85606bd0a8SJiri Slaby left = left * 0xff / 0xffff;
86606bd0a8SJiri Slaby CLAMP(left);
87606bd0a8SJiri Slaby CLAMP(right);
88606bd0a8SJiri Slaby report->field[0]->value[0] = 0x42;
89606bd0a8SJiri Slaby report->field[0]->value[1] = 0x00;
90606bd0a8SJiri Slaby report->field[0]->value[2] = left;
91606bd0a8SJiri Slaby report->field[0]->value[3] = right;
92606bd0a8SJiri Slaby dbg_hid("(left, right)=(%04x, %04x)\n", left, right);
93d8814272SBenjamin Tissoires hid_hw_request(hid, report, HID_REQ_SET_REPORT);
94606bd0a8SJiri Slaby break;
95606bd0a8SJiri Slaby }
96606bd0a8SJiri Slaby return 0;
97606bd0a8SJiri Slaby }
98606bd0a8SJiri Slaby
hid_lgff_set_autocenter(struct input_dev * dev,u16 magnitude)992bea94dbSSergey Belyashov static void hid_lgff_set_autocenter(struct input_dev *dev, u16 magnitude)
1002bea94dbSSergey Belyashov {
1012bea94dbSSergey Belyashov struct hid_device *hid = input_get_drvdata(dev);
1022bea94dbSSergey Belyashov struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
1032bea94dbSSergey Belyashov struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
1042bea94dbSSergey Belyashov __s32 *value = report->field[0]->value;
1052bea94dbSSergey Belyashov magnitude = (magnitude >> 12) & 0xf;
1062bea94dbSSergey Belyashov *value++ = 0xfe;
1072bea94dbSSergey Belyashov *value++ = 0x0d;
1082bea94dbSSergey Belyashov *value++ = magnitude; /* clockwise strength */
1092bea94dbSSergey Belyashov *value++ = magnitude; /* counter-clockwise strength */
1102bea94dbSSergey Belyashov *value++ = 0x80;
1112bea94dbSSergey Belyashov *value++ = 0x00;
1122bea94dbSSergey Belyashov *value = 0x00;
113d8814272SBenjamin Tissoires hid_hw_request(hid, report, HID_REQ_SET_REPORT);
1142bea94dbSSergey Belyashov }
1152bea94dbSSergey Belyashov
lgff_init(struct hid_device * hid)116606bd0a8SJiri Slaby int lgff_init(struct hid_device* hid)
117606bd0a8SJiri Slaby {
118*d9d4b1e4SAlan Stern struct hid_input *hidinput;
119*d9d4b1e4SAlan Stern struct input_dev *dev;
120606bd0a8SJiri Slaby const signed short *ff_bits = ff_joystick;
121606bd0a8SJiri Slaby int error;
122606bd0a8SJiri Slaby int i;
123606bd0a8SJiri Slaby
124*d9d4b1e4SAlan Stern if (list_empty(&hid->inputs)) {
125*d9d4b1e4SAlan Stern hid_err(hid, "no inputs found\n");
126*d9d4b1e4SAlan Stern return -ENODEV;
127*d9d4b1e4SAlan Stern }
128*d9d4b1e4SAlan Stern hidinput = list_entry(hid->inputs.next, struct hid_input, list);
129*d9d4b1e4SAlan Stern dev = hidinput->input;
130*d9d4b1e4SAlan Stern
131606bd0a8SJiri Slaby /* Check that the report looks ok */
1320fb6bd06SKees Cook if (!hid_validate_values(hid, HID_OUTPUT_REPORT, 0, 0, 7))
1330fb6bd06SKees Cook return -ENODEV;
134606bd0a8SJiri Slaby
135606bd0a8SJiri Slaby for (i = 0; i < ARRAY_SIZE(devices); i++) {
136606bd0a8SJiri Slaby if (dev->id.vendor == devices[i].idVendor &&
137606bd0a8SJiri Slaby dev->id.product == devices[i].idProduct) {
138606bd0a8SJiri Slaby ff_bits = devices[i].ff;
139606bd0a8SJiri Slaby break;
140606bd0a8SJiri Slaby }
141606bd0a8SJiri Slaby }
142606bd0a8SJiri Slaby
143606bd0a8SJiri Slaby for (i = 0; ff_bits[i] >= 0; i++)
144606bd0a8SJiri Slaby set_bit(ff_bits[i], dev->ffbit);
145606bd0a8SJiri Slaby
146606bd0a8SJiri Slaby error = input_ff_create_memless(dev, NULL, hid_lgff_play);
147606bd0a8SJiri Slaby if (error)
148606bd0a8SJiri Slaby return error;
149606bd0a8SJiri Slaby
1502bea94dbSSergey Belyashov if ( test_bit(FF_AUTOCENTER, dev->ffbit) )
1512bea94dbSSergey Belyashov dev->ff->set_autocenter = hid_lgff_set_autocenter;
1522bea94dbSSergey Belyashov
1534291ee30SJoe Perches pr_info("Force feedback for Logitech force feedback devices by Johann Deneux <johann.deneux@it.uu.se>\n");
154606bd0a8SJiri Slaby
155606bd0a8SJiri Slaby return 0;
156606bd0a8SJiri Slaby }
157