xref: /linux/drivers/hid/hid-steelseries.c (revision e3966940559d52aa1800a008dcfeec218dd31f88)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  HID driver for Steelseries devices
4  *
5  *  Copyright (c) 2013 Simon Wood
6  *  Copyright (c) 2023 Bastien Nocera
7  */
8 
9 /*
10  */
11 
12 #include <linux/device.h>
13 #include <linux/hid.h>
14 #include <linux/module.h>
15 #include <linux/usb.h>
16 #include <linux/leds.h>
17 
18 #include "hid-ids.h"
19 
20 #define STEELSERIES_SRWS1		BIT(0)
21 #define STEELSERIES_ARCTIS_1		BIT(1)
22 #define STEELSERIES_ARCTIS_9		BIT(2)
23 
24 struct steelseries_device {
25 	struct hid_device *hdev;
26 	unsigned long quirks;
27 
28 	struct delayed_work battery_work;
29 	spinlock_t lock;
30 	bool removed;
31 
32 	struct power_supply_desc battery_desc;
33 	struct power_supply *battery;
34 	uint8_t battery_capacity;
35 	bool headset_connected;
36 	bool battery_charging;
37 };
38 
39 #if IS_BUILTIN(CONFIG_LEDS_CLASS) || \
40     (IS_MODULE(CONFIG_LEDS_CLASS) && IS_MODULE(CONFIG_HID_STEELSERIES))
41 #define SRWS1_NUMBER_LEDS 15
42 struct steelseries_srws1_data {
43 	__u16 led_state;
44 	/* the last element is used for setting all leds simultaneously */
45 	struct led_classdev *led[SRWS1_NUMBER_LEDS + 1];
46 };
47 #endif
48 
49 /* Fixed report descriptor for Steelseries SRW-S1 wheel controller
50  *
51  * The original descriptor hides the sensitivity and assists dials
52  * a custom vendor usage page. This inserts a patch to make them
53  * appear in the 'Generic Desktop' usage.
54  */
55 
56 static const __u8 steelseries_srws1_rdesc_fixed[] = {
57 0x05, 0x01,         /*  Usage Page (Desktop)                */
58 0x09, 0x08,         /*  Usage (MultiAxis), Changed          */
59 0xA1, 0x01,         /*  Collection (Application),           */
60 0xA1, 0x02,         /*      Collection (Logical),           */
61 0x95, 0x01,         /*          Report Count (1),           */
62 0x05, 0x01,         /* Changed  Usage Page (Desktop),       */
63 0x09, 0x30,         /* Changed  Usage (X),                  */
64 0x16, 0xF8, 0xF8,   /*          Logical Minimum (-1800),    */
65 0x26, 0x08, 0x07,   /*          Logical Maximum (1800),     */
66 0x65, 0x14,         /*          Unit (Degrees),             */
67 0x55, 0x0F,         /*          Unit Exponent (15),         */
68 0x75, 0x10,         /*          Report Size (16),           */
69 0x81, 0x02,         /*          Input (Variable),           */
70 0x09, 0x31,         /* Changed  Usage (Y),                  */
71 0x15, 0x00,         /*          Logical Minimum (0),        */
72 0x26, 0xFF, 0x03,   /*          Logical Maximum (1023),     */
73 0x75, 0x0C,         /*          Report Size (12),           */
74 0x81, 0x02,         /*          Input (Variable),           */
75 0x09, 0x32,         /* Changed  Usage (Z),                  */
76 0x15, 0x00,         /*          Logical Minimum (0),        */
77 0x26, 0xFF, 0x03,   /*          Logical Maximum (1023),     */
78 0x75, 0x0C,         /*          Report Size (12),           */
79 0x81, 0x02,         /*          Input (Variable),           */
80 0x05, 0x01,         /*          Usage Page (Desktop),       */
81 0x09, 0x39,         /*          Usage (Hat Switch),         */
82 0x25, 0x07,         /*          Logical Maximum (7),        */
83 0x35, 0x00,         /*          Physical Minimum (0),       */
84 0x46, 0x3B, 0x01,   /*          Physical Maximum (315),     */
85 0x65, 0x14,         /*          Unit (Degrees),             */
86 0x75, 0x04,         /*          Report Size (4),            */
87 0x95, 0x01,         /*          Report Count (1),           */
88 0x81, 0x02,         /*          Input (Variable),           */
89 0x25, 0x01,         /*          Logical Maximum (1),        */
90 0x45, 0x01,         /*          Physical Maximum (1),       */
91 0x65, 0x00,         /*          Unit,                       */
92 0x75, 0x01,         /*          Report Size (1),            */
93 0x95, 0x03,         /*          Report Count (3),           */
94 0x81, 0x01,         /*          Input (Constant),           */
95 0x05, 0x09,         /*          Usage Page (Button),        */
96 0x19, 0x01,         /*          Usage Minimum (01h),        */
97 0x29, 0x11,         /*          Usage Maximum (11h),        */
98 0x95, 0x11,         /*          Report Count (17),          */
99 0x81, 0x02,         /*          Input (Variable),           */
100                     /*   ---- Dial patch starts here ----   */
101 0x05, 0x01,         /*          Usage Page (Desktop),       */
102 0x09, 0x33,         /*          Usage (RX),                 */
103 0x75, 0x04,         /*          Report Size (4),            */
104 0x95, 0x02,         /*          Report Count (2),           */
105 0x15, 0x00,         /*          Logical Minimum (0),        */
106 0x25, 0x0b,         /*          Logical Maximum (b),        */
107 0x81, 0x02,         /*          Input (Variable),           */
108 0x09, 0x35,         /*          Usage (RZ),                 */
109 0x75, 0x04,         /*          Report Size (4),            */
110 0x95, 0x01,         /*          Report Count (1),           */
111 0x25, 0x03,         /*          Logical Maximum (3),        */
112 0x81, 0x02,         /*          Input (Variable),           */
113                     /*    ---- Dial patch ends here ----    */
114 0x06, 0x00, 0xFF,   /*          Usage Page (FF00h),         */
115 0x09, 0x01,         /*          Usage (01h),                */
116 0x75, 0x04,         /* Changed  Report Size (4),            */
117 0x95, 0x0D,         /* Changed  Report Count (13),          */
118 0x81, 0x02,         /*          Input (Variable),           */
119 0xC0,               /*      End Collection,                 */
120 0xA1, 0x02,         /*      Collection (Logical),           */
121 0x09, 0x02,         /*          Usage (02h),                */
122 0x75, 0x08,         /*          Report Size (8),            */
123 0x95, 0x10,         /*          Report Count (16),          */
124 0x91, 0x02,         /*          Output (Variable),          */
125 0xC0,               /*      End Collection,                 */
126 0xC0                /*  End Collection                      */
127 };
128 
129 #if IS_BUILTIN(CONFIG_LEDS_CLASS) || \
130     (IS_MODULE(CONFIG_LEDS_CLASS) && IS_MODULE(CONFIG_HID_STEELSERIES))
131 static void steelseries_srws1_set_leds(struct hid_device *hdev, __u16 leds)
132 {
133 	struct list_head *report_list = &hdev->report_enum[HID_OUTPUT_REPORT].report_list;
134 	struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
135 	__s32 *value = report->field[0]->value;
136 
137 	value[0] = 0x40;
138 	value[1] = leds & 0xFF;
139 	value[2] = leds >> 8;
140 	value[3] = 0x00;
141 	value[4] = 0x00;
142 	value[5] = 0x00;
143 	value[6] = 0x00;
144 	value[7] = 0x00;
145 	value[8] = 0x00;
146 	value[9] = 0x00;
147 	value[10] = 0x00;
148 	value[11] = 0x00;
149 	value[12] = 0x00;
150 	value[13] = 0x00;
151 	value[14] = 0x00;
152 	value[15] = 0x00;
153 
154 	hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
155 
156 	/* Note: LED change does not show on device until the device is read/polled */
157 }
158 
159 static void steelseries_srws1_led_all_set_brightness(struct led_classdev *led_cdev,
160 			enum led_brightness value)
161 {
162 	struct device *dev = led_cdev->dev->parent;
163 	struct hid_device *hid = to_hid_device(dev);
164 	struct steelseries_srws1_data *drv_data = hid_get_drvdata(hid);
165 
166 	if (!drv_data) {
167 		hid_err(hid, "Device data not found.");
168 		return;
169 	}
170 
171 	if (value == LED_OFF)
172 		drv_data->led_state = 0;
173 	else
174 		drv_data->led_state = (1 << (SRWS1_NUMBER_LEDS + 1)) - 1;
175 
176 	steelseries_srws1_set_leds(hid, drv_data->led_state);
177 }
178 
179 static enum led_brightness steelseries_srws1_led_all_get_brightness(struct led_classdev *led_cdev)
180 {
181 	struct device *dev = led_cdev->dev->parent;
182 	struct hid_device *hid = to_hid_device(dev);
183 	struct steelseries_srws1_data *drv_data;
184 
185 	drv_data = hid_get_drvdata(hid);
186 
187 	if (!drv_data) {
188 		hid_err(hid, "Device data not found.");
189 		return LED_OFF;
190 	}
191 
192 	return (drv_data->led_state >> SRWS1_NUMBER_LEDS) ? LED_FULL : LED_OFF;
193 }
194 
195 static void steelseries_srws1_led_set_brightness(struct led_classdev *led_cdev,
196 			enum led_brightness value)
197 {
198 	struct device *dev = led_cdev->dev->parent;
199 	struct hid_device *hid = to_hid_device(dev);
200 	struct steelseries_srws1_data *drv_data = hid_get_drvdata(hid);
201 	int i, state = 0;
202 
203 	if (!drv_data) {
204 		hid_err(hid, "Device data not found.");
205 		return;
206 	}
207 
208 	for (i = 0; i < SRWS1_NUMBER_LEDS; i++) {
209 		if (led_cdev != drv_data->led[i])
210 			continue;
211 
212 		state = (drv_data->led_state >> i) & 1;
213 		if (value == LED_OFF && state) {
214 			drv_data->led_state &= ~(1 << i);
215 			steelseries_srws1_set_leds(hid, drv_data->led_state);
216 		} else if (value != LED_OFF && !state) {
217 			drv_data->led_state |= 1 << i;
218 			steelseries_srws1_set_leds(hid, drv_data->led_state);
219 		}
220 		break;
221 	}
222 }
223 
224 static enum led_brightness steelseries_srws1_led_get_brightness(struct led_classdev *led_cdev)
225 {
226 	struct device *dev = led_cdev->dev->parent;
227 	struct hid_device *hid = to_hid_device(dev);
228 	struct steelseries_srws1_data *drv_data;
229 	int i, value = 0;
230 
231 	drv_data = hid_get_drvdata(hid);
232 
233 	if (!drv_data) {
234 		hid_err(hid, "Device data not found.");
235 		return LED_OFF;
236 	}
237 
238 	for (i = 0; i < SRWS1_NUMBER_LEDS; i++)
239 		if (led_cdev == drv_data->led[i]) {
240 			value = (drv_data->led_state >> i) & 1;
241 			break;
242 		}
243 
244 	return value ? LED_FULL : LED_OFF;
245 }
246 
247 static int steelseries_srws1_probe(struct hid_device *hdev,
248 		const struct hid_device_id *id)
249 {
250 	int ret, i;
251 	struct led_classdev *led;
252 	struct steelseries_srws1_data *drv_data;
253 	size_t name_sz;
254 	char *name;
255 
256 	drv_data = devm_kzalloc(&hdev->dev, sizeof(*drv_data), GFP_KERNEL);
257 	if (drv_data == NULL) {
258 		hid_err(hdev, "can't alloc SRW-S1 memory\n");
259 		return -ENOMEM;
260 	}
261 
262 	hid_set_drvdata(hdev, drv_data);
263 
264 	ret = hid_parse(hdev);
265 	if (ret) {
266 		hid_err(hdev, "parse failed\n");
267 		goto err;
268 	}
269 
270 	if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, 0, 0, 16)) {
271 		ret = -ENODEV;
272 		goto err;
273 	}
274 
275 	ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
276 	if (ret) {
277 		hid_err(hdev, "hw start failed\n");
278 		goto err;
279 	}
280 
281 	/* register led subsystem */
282 	drv_data->led_state = 0;
283 	for (i = 0; i < SRWS1_NUMBER_LEDS + 1; i++)
284 		drv_data->led[i] = NULL;
285 
286 	steelseries_srws1_set_leds(hdev, 0);
287 
288 	name_sz = strlen(hdev->uniq) + 16;
289 
290 	/* 'ALL', for setting all LEDs simultaneously */
291 	led = devm_kzalloc(&hdev->dev, sizeof(struct led_classdev)+name_sz, GFP_KERNEL);
292 	if (!led) {
293 		hid_err(hdev, "can't allocate memory for LED ALL\n");
294 		goto out;
295 	}
296 
297 	name = (void *)(&led[1]);
298 	snprintf(name, name_sz, "SRWS1::%s::RPMALL", hdev->uniq);
299 	led->name = name;
300 	led->brightness = 0;
301 	led->max_brightness = 1;
302 	led->brightness_get = steelseries_srws1_led_all_get_brightness;
303 	led->brightness_set = steelseries_srws1_led_all_set_brightness;
304 
305 	drv_data->led[SRWS1_NUMBER_LEDS] = led;
306 	ret = devm_led_classdev_register(&hdev->dev, led);
307 	if (ret) {
308 		hid_err(hdev, "failed to register LED %d. Aborting.\n", SRWS1_NUMBER_LEDS);
309 		goto out; /* let the driver continue without LEDs */
310 	}
311 
312 	/* Each individual LED */
313 	for (i = 0; i < SRWS1_NUMBER_LEDS; i++) {
314 		led = devm_kzalloc(&hdev->dev, sizeof(struct led_classdev)+name_sz, GFP_KERNEL);
315 		if (!led) {
316 			hid_err(hdev, "can't allocate memory for LED %d\n", i);
317 			break;
318 		}
319 
320 		name = (void *)(&led[1]);
321 		snprintf(name, name_sz, "SRWS1::%s::RPM%d", hdev->uniq, i+1);
322 		led->name = name;
323 		led->brightness = 0;
324 		led->max_brightness = 1;
325 		led->brightness_get = steelseries_srws1_led_get_brightness;
326 		led->brightness_set = steelseries_srws1_led_set_brightness;
327 
328 		drv_data->led[i] = led;
329 		ret = devm_led_classdev_register(&hdev->dev, led);
330 
331 		if (ret) {
332 			hid_err(hdev, "failed to register LED %d. Aborting.\n", i);
333 			break;	/* but let the driver continue without LEDs */
334 		}
335 	}
336 out:
337 	return 0;
338 err:
339 	return ret;
340 }
341 #endif
342 
343 #define STEELSERIES_HEADSET_BATTERY_TIMEOUT_MS	3000
344 
345 #define ARCTIS_1_BATTERY_RESPONSE_LEN		8
346 #define ARCTIS_9_BATTERY_RESPONSE_LEN		64
347 static const char arctis_1_battery_request[] = { 0x06, 0x12 };
348 static const char arctis_9_battery_request[] = { 0x00, 0x20 };
349 
350 static int steelseries_headset_request_battery(struct hid_device *hdev,
351 	const char *request, size_t len)
352 {
353 	u8 *write_buf;
354 	int ret;
355 
356 	/* Request battery information */
357 	write_buf = kmemdup(request, len, GFP_KERNEL);
358 	if (!write_buf)
359 		return -ENOMEM;
360 
361 	hid_dbg(hdev, "Sending battery request report");
362 	ret = hid_hw_raw_request(hdev, request[0], write_buf, len,
363 				 HID_OUTPUT_REPORT, HID_REQ_SET_REPORT);
364 	if (ret < (int)len) {
365 		hid_err(hdev, "hid_hw_raw_request() failed with %d\n", ret);
366 		ret = -ENODATA;
367 	}
368 
369 	kfree(write_buf);
370 	return ret;
371 }
372 
373 static void steelseries_headset_fetch_battery(struct hid_device *hdev)
374 {
375 	int ret = 0;
376 
377 	if (hdev->product == USB_DEVICE_ID_STEELSERIES_ARCTIS_1)
378 		ret = steelseries_headset_request_battery(hdev,
379 			arctis_1_battery_request, sizeof(arctis_1_battery_request));
380 	else if (hdev->product == USB_DEVICE_ID_STEELSERIES_ARCTIS_9)
381 		ret = steelseries_headset_request_battery(hdev,
382 			arctis_9_battery_request, sizeof(arctis_9_battery_request));
383 
384 	if (ret < 0)
385 		hid_dbg(hdev,
386 			"Battery query failed (err: %d)\n", ret);
387 }
388 
389 static int battery_capacity_to_level(int capacity)
390 {
391 	if (capacity >= 50)
392 		return POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
393 	if (capacity >= 20)
394 		return POWER_SUPPLY_CAPACITY_LEVEL_LOW;
395 	return POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
396 }
397 
398 static void steelseries_headset_battery_timer_tick(struct work_struct *work)
399 {
400 	struct steelseries_device *sd = container_of(work,
401 		struct steelseries_device, battery_work.work);
402 	struct hid_device *hdev = sd->hdev;
403 
404 	steelseries_headset_fetch_battery(hdev);
405 }
406 
407 #define STEELSERIES_PREFIX "SteelSeries "
408 #define STEELSERIES_PREFIX_LEN strlen(STEELSERIES_PREFIX)
409 
410 static int steelseries_headset_battery_get_property(struct power_supply *psy,
411 				enum power_supply_property psp,
412 				union power_supply_propval *val)
413 {
414 	struct steelseries_device *sd = power_supply_get_drvdata(psy);
415 	int ret = 0;
416 
417 	switch (psp) {
418 	case POWER_SUPPLY_PROP_MODEL_NAME:
419 		val->strval = sd->hdev->name;
420 		while (!strncmp(val->strval, STEELSERIES_PREFIX, STEELSERIES_PREFIX_LEN))
421 			val->strval += STEELSERIES_PREFIX_LEN;
422 		break;
423 	case POWER_SUPPLY_PROP_MANUFACTURER:
424 		val->strval = "SteelSeries";
425 		break;
426 	case POWER_SUPPLY_PROP_PRESENT:
427 		val->intval = 1;
428 		break;
429 	case POWER_SUPPLY_PROP_STATUS:
430 		if (sd->headset_connected) {
431 			val->intval = sd->battery_charging ?
432 				POWER_SUPPLY_STATUS_CHARGING :
433 				POWER_SUPPLY_STATUS_DISCHARGING;
434 		} else
435 			val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
436 		break;
437 	case POWER_SUPPLY_PROP_SCOPE:
438 		val->intval = POWER_SUPPLY_SCOPE_DEVICE;
439 		break;
440 	case POWER_SUPPLY_PROP_CAPACITY:
441 		val->intval = sd->battery_capacity;
442 		break;
443 	case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
444 		val->intval = battery_capacity_to_level(sd->battery_capacity);
445 		break;
446 	default:
447 		ret = -EINVAL;
448 		break;
449 	}
450 	return ret;
451 }
452 
453 static void
454 steelseries_headset_set_wireless_status(struct hid_device *hdev,
455 					bool connected)
456 {
457 	struct usb_interface *intf;
458 
459 	if (!hid_is_usb(hdev))
460 		return;
461 
462 	intf = to_usb_interface(hdev->dev.parent);
463 	usb_set_wireless_status(intf, connected ?
464 				USB_WIRELESS_STATUS_CONNECTED :
465 				USB_WIRELESS_STATUS_DISCONNECTED);
466 }
467 
468 static enum power_supply_property steelseries_headset_battery_props[] = {
469 	POWER_SUPPLY_PROP_MODEL_NAME,
470 	POWER_SUPPLY_PROP_MANUFACTURER,
471 	POWER_SUPPLY_PROP_PRESENT,
472 	POWER_SUPPLY_PROP_STATUS,
473 	POWER_SUPPLY_PROP_SCOPE,
474 	POWER_SUPPLY_PROP_CAPACITY,
475 	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
476 };
477 
478 static int steelseries_headset_battery_register(struct steelseries_device *sd)
479 {
480 	static atomic_t battery_no = ATOMIC_INIT(0);
481 	struct power_supply_config battery_cfg = { .drv_data = sd, };
482 	unsigned long n;
483 	int ret;
484 
485 	sd->battery_desc.type = POWER_SUPPLY_TYPE_BATTERY;
486 	sd->battery_desc.properties = steelseries_headset_battery_props;
487 	sd->battery_desc.num_properties = ARRAY_SIZE(steelseries_headset_battery_props);
488 	sd->battery_desc.get_property = steelseries_headset_battery_get_property;
489 	sd->battery_desc.use_for_apm = 0;
490 	n = atomic_inc_return(&battery_no) - 1;
491 	sd->battery_desc.name = devm_kasprintf(&sd->hdev->dev, GFP_KERNEL,
492 						    "steelseries_headset_battery_%ld", n);
493 	if (!sd->battery_desc.name)
494 		return -ENOMEM;
495 
496 	/* avoid the warning of 0% battery while waiting for the first info */
497 	steelseries_headset_set_wireless_status(sd->hdev, false);
498 	sd->battery_capacity = 100;
499 	sd->battery_charging = false;
500 
501 	sd->battery = devm_power_supply_register(&sd->hdev->dev,
502 			&sd->battery_desc, &battery_cfg);
503 	if (IS_ERR(sd->battery)) {
504 		ret = PTR_ERR(sd->battery);
505 		hid_err(sd->hdev,
506 				"%s:power_supply_register failed with error %d\n",
507 				__func__, ret);
508 		return ret;
509 	}
510 	power_supply_powers(sd->battery, &sd->hdev->dev);
511 
512 	INIT_DELAYED_WORK(&sd->battery_work, steelseries_headset_battery_timer_tick);
513 	steelseries_headset_fetch_battery(sd->hdev);
514 
515 	if (sd->quirks & STEELSERIES_ARCTIS_9) {
516 		/* The first fetch_battery request can remain unanswered in some cases */
517 		schedule_delayed_work(&sd->battery_work,
518 				msecs_to_jiffies(STEELSERIES_HEADSET_BATTERY_TIMEOUT_MS));
519 	}
520 
521 	return 0;
522 }
523 
524 static bool steelseries_is_vendor_usage_page(struct hid_device *hdev, uint8_t usage_page)
525 {
526 	return hdev->rdesc[0] == 0x06 &&
527 		hdev->rdesc[1] == usage_page &&
528 		hdev->rdesc[2] == 0xff;
529 }
530 
531 static int steelseries_probe(struct hid_device *hdev, const struct hid_device_id *id)
532 {
533 	struct steelseries_device *sd;
534 	int ret;
535 
536 	if (hdev->product == USB_DEVICE_ID_STEELSERIES_SRWS1) {
537 #if IS_BUILTIN(CONFIG_LEDS_CLASS) || \
538     (IS_MODULE(CONFIG_LEDS_CLASS) && IS_MODULE(CONFIG_HID_STEELSERIES))
539 		return steelseries_srws1_probe(hdev, id);
540 #else
541 		return -ENODEV;
542 #endif
543 	}
544 
545 	sd = devm_kzalloc(&hdev->dev, sizeof(*sd), GFP_KERNEL);
546 	if (!sd)
547 		return -ENOMEM;
548 	hid_set_drvdata(hdev, sd);
549 	sd->hdev = hdev;
550 	sd->quirks = id->driver_data;
551 
552 	ret = hid_parse(hdev);
553 	if (ret)
554 		return ret;
555 
556 	if (sd->quirks & STEELSERIES_ARCTIS_9 &&
557 			!steelseries_is_vendor_usage_page(hdev, 0xc0))
558 		return -ENODEV;
559 
560 	spin_lock_init(&sd->lock);
561 
562 	ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
563 	if (ret)
564 		return ret;
565 
566 	ret = hid_hw_open(hdev);
567 	if (ret)
568 		return ret;
569 
570 	if (steelseries_headset_battery_register(sd) < 0)
571 		hid_err(sd->hdev,
572 			"Failed to register battery for headset\n");
573 
574 	return ret;
575 }
576 
577 static void steelseries_remove(struct hid_device *hdev)
578 {
579 	struct steelseries_device *sd;
580 	unsigned long flags;
581 
582 	if (hdev->product == USB_DEVICE_ID_STEELSERIES_SRWS1) {
583 #if IS_BUILTIN(CONFIG_LEDS_CLASS) || \
584     (IS_MODULE(CONFIG_LEDS_CLASS) && IS_MODULE(CONFIG_HID_STEELSERIES))
585 		hid_hw_stop(hdev);
586 #endif
587 		return;
588 	}
589 
590 	sd = hid_get_drvdata(hdev);
591 
592 	spin_lock_irqsave(&sd->lock, flags);
593 	sd->removed = true;
594 	spin_unlock_irqrestore(&sd->lock, flags);
595 
596 	cancel_delayed_work_sync(&sd->battery_work);
597 
598 	hid_hw_close(hdev);
599 	hid_hw_stop(hdev);
600 }
601 
602 static const __u8 *steelseries_srws1_report_fixup(struct hid_device *hdev,
603 		__u8 *rdesc, unsigned int *rsize)
604 {
605 	if (hdev->vendor != USB_VENDOR_ID_STEELSERIES ||
606 	    hdev->product != USB_DEVICE_ID_STEELSERIES_SRWS1)
607 		return rdesc;
608 
609 	if (*rsize >= 115 && rdesc[11] == 0x02 && rdesc[13] == 0xc8
610 			&& rdesc[29] == 0xbb && rdesc[40] == 0xc5) {
611 		hid_info(hdev, "Fixing up Steelseries SRW-S1 report descriptor\n");
612 		*rsize = sizeof(steelseries_srws1_rdesc_fixed);
613 		return steelseries_srws1_rdesc_fixed;
614 	}
615 	return rdesc;
616 }
617 
618 static uint8_t steelseries_headset_map_capacity(uint8_t capacity, uint8_t min_in, uint8_t max_in)
619 {
620 	if (capacity >= max_in)
621 		return 100;
622 	if (capacity <= min_in)
623 		return 0;
624 	return (capacity - min_in) * 100 / (max_in - min_in);
625 }
626 
627 static int steelseries_headset_raw_event(struct hid_device *hdev,
628 					struct hid_report *report, u8 *read_buf,
629 					int size)
630 {
631 	struct steelseries_device *sd = hid_get_drvdata(hdev);
632 	int capacity = sd->battery_capacity;
633 	bool connected = sd->headset_connected;
634 	bool charging = sd->battery_charging;
635 	unsigned long flags;
636 
637 	/* Not a headset */
638 	if (hdev->product == USB_DEVICE_ID_STEELSERIES_SRWS1)
639 		return 0;
640 
641 	if (hdev->product == USB_DEVICE_ID_STEELSERIES_ARCTIS_1) {
642 		hid_dbg(sd->hdev,
643 			"Parsing raw event for Arctis 1 headset (%*ph)\n", size, read_buf);
644 		if (size < ARCTIS_1_BATTERY_RESPONSE_LEN ||
645 		    memcmp(read_buf, arctis_1_battery_request, sizeof(arctis_1_battery_request))) {
646 			if (!delayed_work_pending(&sd->battery_work))
647 				goto request_battery;
648 			return 0;
649 		}
650 		if (read_buf[2] == 0x01) {
651 			connected = false;
652 			capacity = 100;
653 		} else {
654 			connected = true;
655 			capacity = read_buf[3];
656 		}
657 	}
658 
659 	if (hdev->product == USB_DEVICE_ID_STEELSERIES_ARCTIS_9) {
660 		hid_dbg(sd->hdev,
661 			"Parsing raw event for Arctis 9 headset (%*ph)\n", size, read_buf);
662 		if (size < ARCTIS_9_BATTERY_RESPONSE_LEN) {
663 			if (!delayed_work_pending(&sd->battery_work))
664 				goto request_battery;
665 			return 0;
666 		}
667 
668 		if (read_buf[0] == 0xaa && read_buf[1] == 0x01) {
669 			connected = true;
670 			charging = read_buf[4] == 0x01;
671 
672 			/*
673 			 * Found no official documentation about min and max.
674 			 * Values defined by testing.
675 			 */
676 			capacity = steelseries_headset_map_capacity(read_buf[3], 0x68, 0x9d);
677 		} else {
678 			/*
679 			 * Device is off and sends the last known status read_buf[1] == 0x03 or
680 			 * there is no known status of the device read_buf[0] == 0x55
681 			 */
682 			connected = false;
683 			charging = false;
684 		}
685 	}
686 
687 	if (connected != sd->headset_connected) {
688 		hid_dbg(sd->hdev,
689 			"Connected status changed from %sconnected to %sconnected\n",
690 			sd->headset_connected ? "" : "not ",
691 			connected ? "" : "not ");
692 		sd->headset_connected = connected;
693 		steelseries_headset_set_wireless_status(hdev, connected);
694 	}
695 
696 	if (capacity != sd->battery_capacity) {
697 		hid_dbg(sd->hdev,
698 			"Battery capacity changed from %d%% to %d%%\n",
699 			sd->battery_capacity, capacity);
700 		sd->battery_capacity = capacity;
701 		power_supply_changed(sd->battery);
702 	}
703 
704 	if (charging != sd->battery_charging) {
705 		hid_dbg(sd->hdev,
706 			"Battery charging status changed from %scharging to %scharging\n",
707 			sd->battery_charging ? "" : "not ",
708 			charging ? "" : "not ");
709 		sd->battery_charging = charging;
710 		power_supply_changed(sd->battery);
711 	}
712 
713 request_battery:
714 	spin_lock_irqsave(&sd->lock, flags);
715 	if (!sd->removed)
716 		schedule_delayed_work(&sd->battery_work,
717 				msecs_to_jiffies(STEELSERIES_HEADSET_BATTERY_TIMEOUT_MS));
718 	spin_unlock_irqrestore(&sd->lock, flags);
719 
720 	return 0;
721 }
722 
723 static const struct hid_device_id steelseries_devices[] = {
724 	{ HID_USB_DEVICE(USB_VENDOR_ID_STEELSERIES, USB_DEVICE_ID_STEELSERIES_SRWS1),
725 	  .driver_data = STEELSERIES_SRWS1 },
726 
727 	{ /* SteelSeries Arctis 1 Wireless for XBox */
728 	  HID_USB_DEVICE(USB_VENDOR_ID_STEELSERIES, USB_DEVICE_ID_STEELSERIES_ARCTIS_1),
729 	  .driver_data = STEELSERIES_ARCTIS_1 },
730 
731 	{ /* SteelSeries Arctis 9 Wireless for XBox */
732 	  HID_USB_DEVICE(USB_VENDOR_ID_STEELSERIES, USB_DEVICE_ID_STEELSERIES_ARCTIS_9),
733 	  .driver_data = STEELSERIES_ARCTIS_9 },
734 
735 	{ }
736 };
737 MODULE_DEVICE_TABLE(hid, steelseries_devices);
738 
739 static struct hid_driver steelseries_driver = {
740 	.name = "steelseries",
741 	.id_table = steelseries_devices,
742 	.probe = steelseries_probe,
743 	.remove = steelseries_remove,
744 	.report_fixup = steelseries_srws1_report_fixup,
745 	.raw_event = steelseries_headset_raw_event,
746 };
747 
748 module_hid_driver(steelseries_driver);
749 MODULE_DESCRIPTION("HID driver for Steelseries devices");
750 MODULE_LICENSE("GPL");
751 MODULE_AUTHOR("Bastien Nocera <hadess@hadess.net>");
752 MODULE_AUTHOR("Simon Wood <simon@mungewell.org>");
753 MODULE_AUTHOR("Christian Mayer <git@mayer-bgk.de>");
754