xref: /openbmc/linux/drivers/hid/hid-steelseries.c (revision 16921921)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  HID driver for Steelseries SRW-S1
4  *
5  *  Copyright (c) 2013 Simon Wood
6  */
7 
8 /*
9  */
10 
11 #include <linux/device.h>
12 #include <linux/hid.h>
13 #include <linux/module.h>
14 #include <linux/leds.h>
15 
16 #include "hid-ids.h"
17 
18 #if IS_BUILTIN(CONFIG_LEDS_CLASS) || \
19     (IS_MODULE(CONFIG_LEDS_CLASS) && IS_MODULE(CONFIG_HID_STEELSERIES))
20 #define SRWS1_NUMBER_LEDS 15
21 struct steelseries_srws1_data {
22 	__u16 led_state;
23 	/* the last element is used for setting all leds simultaneously */
24 	struct led_classdev *led[SRWS1_NUMBER_LEDS + 1];
25 };
26 #endif
27 
28 /* Fixed report descriptor for Steelseries SRW-S1 wheel controller
29  *
30  * The original descriptor hides the sensitivity and assists dials
31  * a custom vendor usage page. This inserts a patch to make them
32  * appear in the 'Generic Desktop' usage.
33  */
34 
35 static __u8 steelseries_srws1_rdesc_fixed[] = {
36 0x05, 0x01,         /*  Usage Page (Desktop)                */
37 0x09, 0x08,         /*  Usage (MultiAxis), Changed          */
38 0xA1, 0x01,         /*  Collection (Application),           */
39 0xA1, 0x02,         /*      Collection (Logical),           */
40 0x95, 0x01,         /*          Report Count (1),           */
41 0x05, 0x01,         /* Changed  Usage Page (Desktop),       */
42 0x09, 0x30,         /* Changed  Usage (X),                  */
43 0x16, 0xF8, 0xF8,   /*          Logical Minimum (-1800),    */
44 0x26, 0x08, 0x07,   /*          Logical Maximum (1800),     */
45 0x65, 0x14,         /*          Unit (Degrees),             */
46 0x55, 0x0F,         /*          Unit Exponent (15),         */
47 0x75, 0x10,         /*          Report Size (16),           */
48 0x81, 0x02,         /*          Input (Variable),           */
49 0x09, 0x31,         /* Changed  Usage (Y),                  */
50 0x15, 0x00,         /*          Logical Minimum (0),        */
51 0x26, 0xFF, 0x03,   /*          Logical Maximum (1023),     */
52 0x75, 0x0C,         /*          Report Size (12),           */
53 0x81, 0x02,         /*          Input (Variable),           */
54 0x09, 0x32,         /* Changed  Usage (Z),                  */
55 0x15, 0x00,         /*          Logical Minimum (0),        */
56 0x26, 0xFF, 0x03,   /*          Logical Maximum (1023),     */
57 0x75, 0x0C,         /*          Report Size (12),           */
58 0x81, 0x02,         /*          Input (Variable),           */
59 0x05, 0x01,         /*          Usage Page (Desktop),       */
60 0x09, 0x39,         /*          Usage (Hat Switch),         */
61 0x25, 0x07,         /*          Logical Maximum (7),        */
62 0x35, 0x00,         /*          Physical Minimum (0),       */
63 0x46, 0x3B, 0x01,   /*          Physical Maximum (315),     */
64 0x65, 0x14,         /*          Unit (Degrees),             */
65 0x75, 0x04,         /*          Report Size (4),            */
66 0x95, 0x01,         /*          Report Count (1),           */
67 0x81, 0x02,         /*          Input (Variable),           */
68 0x25, 0x01,         /*          Logical Maximum (1),        */
69 0x45, 0x01,         /*          Physical Maximum (1),       */
70 0x65, 0x00,         /*          Unit,                       */
71 0x75, 0x01,         /*          Report Size (1),            */
72 0x95, 0x03,         /*          Report Count (3),           */
73 0x81, 0x01,         /*          Input (Constant),           */
74 0x05, 0x09,         /*          Usage Page (Button),        */
75 0x19, 0x01,         /*          Usage Minimum (01h),        */
76 0x29, 0x11,         /*          Usage Maximum (11h),        */
77 0x95, 0x11,         /*          Report Count (17),          */
78 0x81, 0x02,         /*          Input (Variable),           */
79                     /*   ---- Dial patch starts here ----   */
80 0x05, 0x01,         /*          Usage Page (Desktop),       */
81 0x09, 0x33,         /*          Usage (RX),                 */
82 0x75, 0x04,         /*          Report Size (4),            */
83 0x95, 0x02,         /*          Report Count (2),           */
84 0x15, 0x00,         /*          Logical Minimum (0),        */
85 0x25, 0x0b,         /*          Logical Maximum (b),        */
86 0x81, 0x02,         /*          Input (Variable),           */
87 0x09, 0x35,         /*          Usage (RZ),                 */
88 0x75, 0x04,         /*          Report Size (4),            */
89 0x95, 0x01,         /*          Report Count (1),           */
90 0x25, 0x03,         /*          Logical Maximum (3),        */
91 0x81, 0x02,         /*          Input (Variable),           */
92                     /*    ---- Dial patch ends here ----    */
93 0x06, 0x00, 0xFF,   /*          Usage Page (FF00h),         */
94 0x09, 0x01,         /*          Usage (01h),                */
95 0x75, 0x04,         /* Changed  Report Size (4),            */
96 0x95, 0x0D,         /* Changed  Report Count (13),          */
97 0x81, 0x02,         /*          Input (Variable),           */
98 0xC0,               /*      End Collection,                 */
99 0xA1, 0x02,         /*      Collection (Logical),           */
100 0x09, 0x02,         /*          Usage (02h),                */
101 0x75, 0x08,         /*          Report Size (8),            */
102 0x95, 0x10,         /*          Report Count (16),          */
103 0x91, 0x02,         /*          Output (Variable),          */
104 0xC0,               /*      End Collection,                 */
105 0xC0                /*  End Collection                      */
106 };
107 
108 #if IS_BUILTIN(CONFIG_LEDS_CLASS) || \
109     (IS_MODULE(CONFIG_LEDS_CLASS) && IS_MODULE(CONFIG_HID_STEELSERIES))
110 static void steelseries_srws1_set_leds(struct hid_device *hdev, __u16 leds)
111 {
112 	struct list_head *report_list = &hdev->report_enum[HID_OUTPUT_REPORT].report_list;
113 	struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
114 	__s32 *value = report->field[0]->value;
115 
116 	value[0] = 0x40;
117 	value[1] = leds & 0xFF;
118 	value[2] = leds >> 8;
119 	value[3] = 0x00;
120 	value[4] = 0x00;
121 	value[5] = 0x00;
122 	value[6] = 0x00;
123 	value[7] = 0x00;
124 	value[8] = 0x00;
125 	value[9] = 0x00;
126 	value[10] = 0x00;
127 	value[11] = 0x00;
128 	value[12] = 0x00;
129 	value[13] = 0x00;
130 	value[14] = 0x00;
131 	value[15] = 0x00;
132 
133 	hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
134 
135 	/* Note: LED change does not show on device until the device is read/polled */
136 }
137 
138 static void steelseries_srws1_led_all_set_brightness(struct led_classdev *led_cdev,
139 			enum led_brightness value)
140 {
141 	struct device *dev = led_cdev->dev->parent;
142 	struct hid_device *hid = to_hid_device(dev);
143 	struct steelseries_srws1_data *drv_data = hid_get_drvdata(hid);
144 
145 	if (!drv_data) {
146 		hid_err(hid, "Device data not found.");
147 		return;
148 	}
149 
150 	if (value == LED_OFF)
151 		drv_data->led_state = 0;
152 	else
153 		drv_data->led_state = (1 << (SRWS1_NUMBER_LEDS + 1)) - 1;
154 
155 	steelseries_srws1_set_leds(hid, drv_data->led_state);
156 }
157 
158 static enum led_brightness steelseries_srws1_led_all_get_brightness(struct led_classdev *led_cdev)
159 {
160 	struct device *dev = led_cdev->dev->parent;
161 	struct hid_device *hid = to_hid_device(dev);
162 	struct steelseries_srws1_data *drv_data;
163 
164 	drv_data = hid_get_drvdata(hid);
165 
166 	if (!drv_data) {
167 		hid_err(hid, "Device data not found.");
168 		return LED_OFF;
169 	}
170 
171 	return (drv_data->led_state >> SRWS1_NUMBER_LEDS) ? LED_FULL : LED_OFF;
172 }
173 
174 static void steelseries_srws1_led_set_brightness(struct led_classdev *led_cdev,
175 			enum led_brightness value)
176 {
177 	struct device *dev = led_cdev->dev->parent;
178 	struct hid_device *hid = to_hid_device(dev);
179 	struct steelseries_srws1_data *drv_data = hid_get_drvdata(hid);
180 	int i, state = 0;
181 
182 	if (!drv_data) {
183 		hid_err(hid, "Device data not found.");
184 		return;
185 	}
186 
187 	for (i = 0; i < SRWS1_NUMBER_LEDS; i++) {
188 		if (led_cdev != drv_data->led[i])
189 			continue;
190 
191 		state = (drv_data->led_state >> i) & 1;
192 		if (value == LED_OFF && state) {
193 			drv_data->led_state &= ~(1 << i);
194 			steelseries_srws1_set_leds(hid, drv_data->led_state);
195 		} else if (value != LED_OFF && !state) {
196 			drv_data->led_state |= 1 << i;
197 			steelseries_srws1_set_leds(hid, drv_data->led_state);
198 		}
199 		break;
200 	}
201 }
202 
203 static enum led_brightness steelseries_srws1_led_get_brightness(struct led_classdev *led_cdev)
204 {
205 	struct device *dev = led_cdev->dev->parent;
206 	struct hid_device *hid = to_hid_device(dev);
207 	struct steelseries_srws1_data *drv_data;
208 	int i, value = 0;
209 
210 	drv_data = hid_get_drvdata(hid);
211 
212 	if (!drv_data) {
213 		hid_err(hid, "Device data not found.");
214 		return LED_OFF;
215 	}
216 
217 	for (i = 0; i < SRWS1_NUMBER_LEDS; i++)
218 		if (led_cdev == drv_data->led[i]) {
219 			value = (drv_data->led_state >> i) & 1;
220 			break;
221 		}
222 
223 	return value ? LED_FULL : LED_OFF;
224 }
225 
226 static int steelseries_srws1_probe(struct hid_device *hdev,
227 		const struct hid_device_id *id)
228 {
229 	int ret, i;
230 	struct led_classdev *led;
231 	size_t name_sz;
232 	char *name;
233 
234 	struct steelseries_srws1_data *drv_data = kzalloc(sizeof(*drv_data), GFP_KERNEL);
235 
236 	if (drv_data == NULL) {
237 		hid_err(hdev, "can't alloc SRW-S1 memory\n");
238 		return -ENOMEM;
239 	}
240 
241 	hid_set_drvdata(hdev, drv_data);
242 
243 	ret = hid_parse(hdev);
244 	if (ret) {
245 		hid_err(hdev, "parse failed\n");
246 		goto err_free;
247 	}
248 
249 	if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, 0, 0, 16)) {
250 		ret = -ENODEV;
251 		goto err_free;
252 	}
253 
254 	ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
255 	if (ret) {
256 		hid_err(hdev, "hw start failed\n");
257 		goto err_free;
258 	}
259 
260 	/* register led subsystem */
261 	drv_data->led_state = 0;
262 	for (i = 0; i < SRWS1_NUMBER_LEDS + 1; i++)
263 		drv_data->led[i] = NULL;
264 
265 	steelseries_srws1_set_leds(hdev, 0);
266 
267 	name_sz = strlen(hdev->uniq) + 16;
268 
269 	/* 'ALL', for setting all LEDs simultaneously */
270 	led = kzalloc(sizeof(struct led_classdev)+name_sz, GFP_KERNEL);
271 	if (!led) {
272 		hid_err(hdev, "can't allocate memory for LED ALL\n");
273 		goto err_led;
274 	}
275 
276 	name = (void *)(&led[1]);
277 	snprintf(name, name_sz, "SRWS1::%s::RPMALL", hdev->uniq);
278 	led->name = name;
279 	led->brightness = 0;
280 	led->max_brightness = 1;
281 	led->brightness_get = steelseries_srws1_led_all_get_brightness;
282 	led->brightness_set = steelseries_srws1_led_all_set_brightness;
283 
284 	drv_data->led[SRWS1_NUMBER_LEDS] = led;
285 	ret = led_classdev_register(&hdev->dev, led);
286 	if (ret)
287 		goto err_led;
288 
289 	/* Each individual LED */
290 	for (i = 0; i < SRWS1_NUMBER_LEDS; i++) {
291 		led = kzalloc(sizeof(struct led_classdev)+name_sz, GFP_KERNEL);
292 		if (!led) {
293 			hid_err(hdev, "can't allocate memory for LED %d\n", i);
294 			goto err_led;
295 		}
296 
297 		name = (void *)(&led[1]);
298 		snprintf(name, name_sz, "SRWS1::%s::RPM%d", hdev->uniq, i+1);
299 		led->name = name;
300 		led->brightness = 0;
301 		led->max_brightness = 1;
302 		led->brightness_get = steelseries_srws1_led_get_brightness;
303 		led->brightness_set = steelseries_srws1_led_set_brightness;
304 
305 		drv_data->led[i] = led;
306 		ret = led_classdev_register(&hdev->dev, led);
307 
308 		if (ret) {
309 			hid_err(hdev, "failed to register LED %d. Aborting.\n", i);
310 err_led:
311 			/* Deregister all LEDs (if any) */
312 			for (i = 0; i < SRWS1_NUMBER_LEDS + 1; i++) {
313 				led = drv_data->led[i];
314 				drv_data->led[i] = NULL;
315 				if (!led)
316 					continue;
317 				led_classdev_unregister(led);
318 				kfree(led);
319 			}
320 			goto out;	/* but let the driver continue without LEDs */
321 		}
322 	}
323 out:
324 	return 0;
325 err_free:
326 	kfree(drv_data);
327 	return ret;
328 }
329 
330 static void steelseries_srws1_remove(struct hid_device *hdev)
331 {
332 	int i;
333 	struct led_classdev *led;
334 
335 	struct steelseries_srws1_data *drv_data = hid_get_drvdata(hdev);
336 
337 	if (drv_data) {
338 		/* Deregister LEDs (if any) */
339 		for (i = 0; i < SRWS1_NUMBER_LEDS + 1; i++) {
340 			led = drv_data->led[i];
341 			drv_data->led[i] = NULL;
342 			if (!led)
343 				continue;
344 			led_classdev_unregister(led);
345 			kfree(led);
346 		}
347 
348 	}
349 
350 	hid_hw_stop(hdev);
351 	kfree(drv_data);
352 	return;
353 }
354 #endif
355 
356 static __u8 *steelseries_srws1_report_fixup(struct hid_device *hdev, __u8 *rdesc,
357 		unsigned int *rsize)
358 {
359 	if (*rsize >= 115 && rdesc[11] == 0x02 && rdesc[13] == 0xc8
360 			&& rdesc[29] == 0xbb && rdesc[40] == 0xc5) {
361 		hid_info(hdev, "Fixing up Steelseries SRW-S1 report descriptor\n");
362 		rdesc = steelseries_srws1_rdesc_fixed;
363 		*rsize = sizeof(steelseries_srws1_rdesc_fixed);
364 	}
365 	return rdesc;
366 }
367 
368 static const struct hid_device_id steelseries_srws1_devices[] = {
369 	{ HID_USB_DEVICE(USB_VENDOR_ID_STEELSERIES, USB_DEVICE_ID_STEELSERIES_SRWS1) },
370 	{ }
371 };
372 MODULE_DEVICE_TABLE(hid, steelseries_srws1_devices);
373 
374 static struct hid_driver steelseries_srws1_driver = {
375 	.name = "steelseries_srws1",
376 	.id_table = steelseries_srws1_devices,
377 #if IS_BUILTIN(CONFIG_LEDS_CLASS) || \
378     (IS_MODULE(CONFIG_LEDS_CLASS) && IS_MODULE(CONFIG_HID_STEELSERIES))
379 	.probe = steelseries_srws1_probe,
380 	.remove = steelseries_srws1_remove,
381 #endif
382 	.report_fixup = steelseries_srws1_report_fixup
383 };
384 
385 module_hid_driver(steelseries_srws1_driver);
386 MODULE_LICENSE("GPL");
387