blob: 9b0efb0083feaf4a1f258226aad0426c7b961721 [file] [log] [blame]
Simon Wood75dbb952013-01-31 08:07:07 -07001/*
2 * HID driver for Steelseries SRW-S1
3 *
4 * Copyright (c) 2013 Simon Wood
5 */
6
7/*
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option)
11 * any later version.
12 */
13
14#include <linux/device.h>
Simon Wood2e2daff2013-01-31 08:07:09 -070015#include <linux/usb.h>
Simon Wood75dbb952013-01-31 08:07:07 -070016#include <linux/hid.h>
17#include <linux/module.h>
18
19#include "hid-ids.h"
20
Simon Wood2e2daff2013-01-31 08:07:09 -070021#if defined(CONFIG_LEDS_CLASS) || defined(CONFIG_LEDS_CLASS_MODULE)
22#define SRWS1_NUMBER_LEDS 15
23struct steelseries_srws1_data {
24 __u16 led_state;
Jiri Kosina7e415762013-01-31 16:51:47 +010025 /* the last element is used for setting all leds simultaneously */
26 struct led_classdev *led[SRWS1_NUMBER_LEDS + 1];
Simon Wood2e2daff2013-01-31 08:07:09 -070027};
28#endif
29
Simon Wood54926062013-01-31 08:07:08 -070030/* Fixed report descriptor for Steelseries SRW-S1 wheel controller
31 *
32 * The original descriptor hides the sensitivity and assists dials
33 * a custom vendor usage page. This inserts a patch to make them
34 * appear in the 'Generic Desktop' usage.
35 */
36
37static __u8 steelseries_srws1_rdesc_fixed[] = {
380x05, 0x01, /* Usage Page (Desktop) */
390x09, 0x08, /* Usage (MultiAxis), Changed */
400xA1, 0x01, /* Collection (Application), */
410xA1, 0x02, /* Collection (Logical), */
420x95, 0x01, /* Report Count (1), */
430x05, 0x01, /* Changed Usage Page (Desktop), */
440x09, 0x30, /* Changed Usage (X), */
450x16, 0xF8, 0xF8, /* Logical Minimum (-1800), */
460x26, 0x08, 0x07, /* Logical Maximum (1800), */
470x65, 0x14, /* Unit (Degrees), */
480x55, 0x0F, /* Unit Exponent (15), */
490x75, 0x10, /* Report Size (16), */
500x81, 0x02, /* Input (Variable), */
510x09, 0x31, /* Changed Usage (Y), */
520x15, 0x00, /* Logical Minimum (0), */
530x26, 0xFF, 0x03, /* Logical Maximum (1023), */
540x75, 0x0C, /* Report Size (12), */
550x81, 0x02, /* Input (Variable), */
560x09, 0x32, /* Changed Usage (Z), */
570x15, 0x00, /* Logical Minimum (0), */
580x26, 0xFF, 0x03, /* Logical Maximum (1023), */
590x75, 0x0C, /* Report Size (12), */
600x81, 0x02, /* Input (Variable), */
610x05, 0x01, /* Usage Page (Desktop), */
620x09, 0x39, /* Usage (Hat Switch), */
630x25, 0x07, /* Logical Maximum (7), */
640x35, 0x00, /* Physical Minimum (0), */
650x46, 0x3B, 0x01, /* Physical Maximum (315), */
660x65, 0x14, /* Unit (Degrees), */
670x75, 0x04, /* Report Size (4), */
680x95, 0x01, /* Report Count (1), */
690x81, 0x02, /* Input (Variable), */
700x25, 0x01, /* Logical Maximum (1), */
710x45, 0x01, /* Physical Maximum (1), */
720x65, 0x00, /* Unit, */
730x75, 0x01, /* Report Size (1), */
740x95, 0x03, /* Report Count (3), */
750x81, 0x01, /* Input (Constant), */
760x05, 0x09, /* Usage Page (Button), */
770x19, 0x01, /* Usage Minimum (01h), */
780x29, 0x11, /* Usage Maximum (11h), */
790x95, 0x11, /* Report Count (17), */
800x81, 0x02, /* Input (Variable), */
81 /* ---- Dial patch starts here ---- */
820x05, 0x01, /* Usage Page (Desktop), */
830x09, 0x33, /* Usage (RX), */
840x75, 0x04, /* Report Size (4), */
850x95, 0x02, /* Report Count (2), */
860x15, 0x00, /* Logical Minimum (0), */
870x25, 0x0b, /* Logical Maximum (b), */
880x81, 0x02, /* Input (Variable), */
890x09, 0x35, /* Usage (RZ), */
900x75, 0x04, /* Report Size (4), */
910x95, 0x01, /* Report Count (1), */
920x25, 0x03, /* Logical Maximum (3), */
930x81, 0x02, /* Input (Variable), */
94 /* ---- Dial patch ends here ---- */
950x06, 0x00, 0xFF, /* Usage Page (FF00h), */
960x09, 0x01, /* Usage (01h), */
970x75, 0x04, /* Changed Report Size (4), */
980x95, 0x0D, /* Changed Report Count (13), */
990x81, 0x02, /* Input (Variable), */
1000xC0, /* End Collection, */
1010xA1, 0x02, /* Collection (Logical), */
1020x09, 0x02, /* Usage (02h), */
1030x75, 0x08, /* Report Size (8), */
1040x95, 0x10, /* Report Count (16), */
1050x91, 0x02, /* Output (Variable), */
1060xC0, /* End Collection, */
1070xC0 /* End Collection */
108};
109
Simon Wood2e2daff2013-01-31 08:07:09 -0700110#if defined(CONFIG_LEDS_CLASS) || defined(CONFIG_LEDS_CLASS_MODULE)
111static void steelseries_srws1_set_leds(struct hid_device *hdev, __u16 leds)
112{
113 struct list_head *report_list = &hdev->report_enum[HID_OUTPUT_REPORT].report_list;
114 struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
115 __s32 *value = report->field[0]->value;
116
117 value[0] = 0x40;
118 value[1] = leds & 0xFF;
119 value[2] = leds >> 8;
120 value[3] = 0x00;
121 value[4] = 0x00;
122 value[5] = 0x00;
123 value[6] = 0x00;
124 value[7] = 0x00;
125 value[8] = 0x00;
126 value[9] = 0x00;
127 value[10] = 0x00;
128 value[11] = 0x00;
129 value[12] = 0x00;
130 value[13] = 0x00;
131 value[14] = 0x00;
132 value[15] = 0x00;
133
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100134 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
Simon Wood2e2daff2013-01-31 08:07:09 -0700135
136 /* Note: LED change does not show on device until the device is read/polled */
137}
138
Simon Woode25d7802013-01-31 08:07:10 -0700139static void steelseries_srws1_led_all_set_brightness(struct led_classdev *led_cdev,
140 enum led_brightness value)
141{
142 struct device *dev = led_cdev->dev->parent;
143 struct hid_device *hid = container_of(dev, struct hid_device, dev);
144 struct steelseries_srws1_data *drv_data = hid_get_drvdata(hid);
145
146 if (!drv_data) {
147 hid_err(hid, "Device data not found.");
148 return;
149 }
150
151 if (value == LED_OFF)
152 drv_data->led_state = 0;
153 else
154 drv_data->led_state = (1 << (SRWS1_NUMBER_LEDS + 1)) - 1;
155
156 steelseries_srws1_set_leds(hid, drv_data->led_state);
157}
158
159static enum led_brightness steelseries_srws1_led_all_get_brightness(struct led_classdev *led_cdev)
160{
161 struct device *dev = led_cdev->dev->parent;
162 struct hid_device *hid = container_of(dev, struct hid_device, dev);
163 struct steelseries_srws1_data *drv_data;
164
165 drv_data = hid_get_drvdata(hid);
166
167 if (!drv_data) {
168 hid_err(hid, "Device data not found.");
169 return LED_OFF;
170 }
171
172 return (drv_data->led_state >> SRWS1_NUMBER_LEDS) ? LED_FULL : LED_OFF;
173}
174
Simon Wood2e2daff2013-01-31 08:07:09 -0700175static void steelseries_srws1_led_set_brightness(struct led_classdev *led_cdev,
176 enum led_brightness value)
177{
178 struct device *dev = led_cdev->dev->parent;
179 struct hid_device *hid = container_of(dev, struct hid_device, dev);
180 struct steelseries_srws1_data *drv_data = hid_get_drvdata(hid);
181 int i, state = 0;
182
183 if (!drv_data) {
184 hid_err(hid, "Device data not found.");
185 return;
186 }
187
188 for (i = 0; i < SRWS1_NUMBER_LEDS; i++) {
189 if (led_cdev != drv_data->led[i])
190 continue;
191
192 state = (drv_data->led_state >> i) & 1;
193 if (value == LED_OFF && state) {
194 drv_data->led_state &= ~(1 << i);
195 steelseries_srws1_set_leds(hid, drv_data->led_state);
196 } else if (value != LED_OFF && !state) {
197 drv_data->led_state |= 1 << i;
198 steelseries_srws1_set_leds(hid, drv_data->led_state);
199 }
200 break;
201 }
202}
203
204static enum led_brightness steelseries_srws1_led_get_brightness(struct led_classdev *led_cdev)
205{
206 struct device *dev = led_cdev->dev->parent;
207 struct hid_device *hid = container_of(dev, struct hid_device, dev);
208 struct steelseries_srws1_data *drv_data;
209 int i, value = 0;
210
211 drv_data = hid_get_drvdata(hid);
212
213 if (!drv_data) {
214 hid_err(hid, "Device data not found.");
215 return LED_OFF;
216 }
217
218 for (i = 0; i < SRWS1_NUMBER_LEDS; i++)
219 if (led_cdev == drv_data->led[i]) {
220 value = (drv_data->led_state >> i) & 1;
221 break;
222 }
223
224 return value ? LED_FULL : LED_OFF;
225}
226
227static int steelseries_srws1_probe(struct hid_device *hdev,
228 const struct hid_device_id *id)
229{
230 int ret, i;
231 struct led_classdev *led;
232 size_t name_sz;
233 char *name;
234
235 struct steelseries_srws1_data *drv_data = kzalloc(sizeof(*drv_data), GFP_KERNEL);
236
237 if (drv_data == NULL) {
238 hid_err(hdev, "can't alloc SRW-S1 memory\n");
239 return -ENOMEM;
240 }
241
242 hid_set_drvdata(hdev, drv_data);
243
244 ret = hid_parse(hdev);
245 if (ret) {
246 hid_err(hdev, "parse failed\n");
247 goto err_free;
248 }
249
250 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
251 if (ret) {
252 hid_err(hdev, "hw start failed\n");
253 goto err_free;
254 }
255
256 /* register led subsystem */
257 drv_data->led_state = 0;
Simon Woode25d7802013-01-31 08:07:10 -0700258 for (i = 0; i < SRWS1_NUMBER_LEDS + 1; i++)
Simon Wood2e2daff2013-01-31 08:07:09 -0700259 drv_data->led[i] = NULL;
260
261 steelseries_srws1_set_leds(hdev, 0);
262
Simon Woode25d7802013-01-31 08:07:10 -0700263 name_sz = strlen(hdev->uniq) + 16;
Simon Wood2e2daff2013-01-31 08:07:09 -0700264
Simon Woode25d7802013-01-31 08:07:10 -0700265 /* 'ALL', for setting all LEDs simultaneously */
266 led = kzalloc(sizeof(struct led_classdev)+name_sz, GFP_KERNEL);
267 if (!led) {
268 hid_err(hdev, "can't allocate memory for LED ALL\n");
269 goto err_led;
270 }
271
272 name = (void *)(&led[1]);
273 snprintf(name, name_sz, "SRWS1::%s::RPMALL", hdev->uniq);
274 led->name = name;
275 led->brightness = 0;
276 led->max_brightness = 1;
277 led->brightness_get = steelseries_srws1_led_all_get_brightness;
278 led->brightness_set = steelseries_srws1_led_all_set_brightness;
279
280 drv_data->led[SRWS1_NUMBER_LEDS] = led;
281 ret = led_classdev_register(&hdev->dev, led);
282 if (ret)
283 goto err_led;
284
285 /* Each individual LED */
Simon Wood2e2daff2013-01-31 08:07:09 -0700286 for (i = 0; i < SRWS1_NUMBER_LEDS; i++) {
287 led = kzalloc(sizeof(struct led_classdev)+name_sz, GFP_KERNEL);
288 if (!led) {
289 hid_err(hdev, "can't allocate memory for LED %d\n", i);
290 goto err_led;
291 }
292
293 name = (void *)(&led[1]);
294 snprintf(name, name_sz, "SRWS1::%s::RPM%d", hdev->uniq, i+1);
295 led->name = name;
296 led->brightness = 0;
297 led->max_brightness = 1;
298 led->brightness_get = steelseries_srws1_led_get_brightness;
299 led->brightness_set = steelseries_srws1_led_set_brightness;
300
301 drv_data->led[i] = led;
302 ret = led_classdev_register(&hdev->dev, led);
303
304 if (ret) {
305 hid_err(hdev, "failed to register LED %d. Aborting.\n", i);
306err_led:
307 /* Deregister all LEDs (if any) */
Simon Woode25d7802013-01-31 08:07:10 -0700308 for (i = 0; i < SRWS1_NUMBER_LEDS + 1; i++) {
Simon Wood2e2daff2013-01-31 08:07:09 -0700309 led = drv_data->led[i];
310 drv_data->led[i] = NULL;
311 if (!led)
312 continue;
313 led_classdev_unregister(led);
314 kfree(led);
315 }
316 goto out; /* but let the driver continue without LEDs */
317 }
318 }
319out:
320 return 0;
321err_free:
322 kfree(drv_data);
323 return ret;
324}
325
326static void steelseries_srws1_remove(struct hid_device *hdev)
327{
328 int i;
329 struct led_classdev *led;
330
331 struct steelseries_srws1_data *drv_data = hid_get_drvdata(hdev);
332
333 if (drv_data) {
334 /* Deregister LEDs (if any) */
Simon Woode25d7802013-01-31 08:07:10 -0700335 for (i = 0; i < SRWS1_NUMBER_LEDS + 1; i++) {
Simon Wood2e2daff2013-01-31 08:07:09 -0700336 led = drv_data->led[i];
337 drv_data->led[i] = NULL;
338 if (!led)
339 continue;
340 led_classdev_unregister(led);
341 kfree(led);
342 }
343
344 }
345
346 hid_hw_stop(hdev);
347 kfree(drv_data);
348 return;
349}
350#endif
351
Simon Wood75dbb952013-01-31 08:07:07 -0700352static __u8 *steelseries_srws1_report_fixup(struct hid_device *hdev, __u8 *rdesc,
353 unsigned int *rsize)
354{
355 if (*rsize >= 115 && rdesc[11] == 0x02 && rdesc[13] == 0xc8
356 && rdesc[29] == 0xbb && rdesc[40] == 0xc5) {
357 hid_info(hdev, "Fixing up Steelseries SRW-S1 report descriptor\n");
Simon Wood54926062013-01-31 08:07:08 -0700358 rdesc = steelseries_srws1_rdesc_fixed;
359 *rsize = sizeof(steelseries_srws1_rdesc_fixed);
Simon Wood75dbb952013-01-31 08:07:07 -0700360 }
361 return rdesc;
362}
363
364static const struct hid_device_id steelseries_srws1_devices[] = {
365 { HID_USB_DEVICE(USB_VENDOR_ID_STEELSERIES, USB_DEVICE_ID_STEELSERIES_SRWS1) },
366 { }
367};
368MODULE_DEVICE_TABLE(hid, steelseries_srws1_devices);
369
370static struct hid_driver steelseries_srws1_driver = {
371 .name = "steelseries_srws1",
372 .id_table = steelseries_srws1_devices,
Simon Wood2e2daff2013-01-31 08:07:09 -0700373#if defined(CONFIG_LEDS_CLASS) || defined(CONFIG_LEDS_CLASS_MODULE)
374 .probe = steelseries_srws1_probe,
375 .remove = steelseries_srws1_remove,
376#endif
Simon Wood75dbb952013-01-31 08:07:07 -0700377 .report_fixup = steelseries_srws1_report_fixup
378};
379
Wei Yongjunbb644692013-03-15 23:07:02 +0800380module_hid_driver(steelseries_srws1_driver);
Simon Wood75dbb952013-01-31 08:07:07 -0700381MODULE_LICENSE("GPL");