1
2
3
4
5
6
7
8
9
10
11
12
13
14#include <linux/device.h>
15#include <linux/hid.h>
16#include <linux/module.h>
17
18#include "hid-ids.h"
19
20#if IS_BUILTIN(CONFIG_LEDS_CLASS) || \
21 (IS_MODULE(CONFIG_LEDS_CLASS) && IS_MODULE(CONFIG_HID_STEELSERIES))
22#define SRWS1_NUMBER_LEDS 15
23struct steelseries_srws1_data {
24 __u16 led_state;
25
26 struct led_classdev *led[SRWS1_NUMBER_LEDS + 1];
27};
28#endif
29
30
31
32
33
34
35
36
37static __u8 steelseries_srws1_rdesc_fixed[] = {
380x05, 0x01,
390x09, 0x08,
400xA1, 0x01,
410xA1, 0x02,
420x95, 0x01,
430x05, 0x01,
440x09, 0x30,
450x16, 0xF8, 0xF8,
460x26, 0x08, 0x07,
470x65, 0x14,
480x55, 0x0F,
490x75, 0x10,
500x81, 0x02,
510x09, 0x31,
520x15, 0x00,
530x26, 0xFF, 0x03,
540x75, 0x0C,
550x81, 0x02,
560x09, 0x32,
570x15, 0x00,
580x26, 0xFF, 0x03,
590x75, 0x0C,
600x81, 0x02,
610x05, 0x01,
620x09, 0x39,
630x25, 0x07,
640x35, 0x00,
650x46, 0x3B, 0x01,
660x65, 0x14,
670x75, 0x04,
680x95, 0x01,
690x81, 0x02,
700x25, 0x01,
710x45, 0x01,
720x65, 0x00,
730x75, 0x01,
740x95, 0x03,
750x81, 0x01,
760x05, 0x09,
770x19, 0x01,
780x29, 0x11,
790x95, 0x11,
800x81, 0x02,
81
820x05, 0x01,
830x09, 0x33,
840x75, 0x04,
850x95, 0x02,
860x15, 0x00,
870x25, 0x0b,
880x81, 0x02,
890x09, 0x35,
900x75, 0x04,
910x95, 0x01,
920x25, 0x03,
930x81, 0x02,
94
950x06, 0x00, 0xFF,
960x09, 0x01,
970x75, 0x04,
980x95, 0x0D,
990x81, 0x02,
1000xC0,
1010xA1, 0x02,
1020x09, 0x02,
1030x75, 0x08,
1040x95, 0x10,
1050x91, 0x02,
1060xC0,
1070xC0
108};
109
110#if IS_BUILTIN(CONFIG_LEDS_CLASS) || \
111 (IS_MODULE(CONFIG_LEDS_CLASS) && IS_MODULE(CONFIG_HID_STEELSERIES))
112static void steelseries_srws1_set_leds(struct hid_device *hdev, __u16 leds)
113{
114 struct list_head *report_list = &hdev->report_enum[HID_OUTPUT_REPORT].report_list;
115 struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
116 __s32 *value = report->field[0]->value;
117
118 value[0] = 0x40;
119 value[1] = leds & 0xFF;
120 value[2] = leds >> 8;
121 value[3] = 0x00;
122 value[4] = 0x00;
123 value[5] = 0x00;
124 value[6] = 0x00;
125 value[7] = 0x00;
126 value[8] = 0x00;
127 value[9] = 0x00;
128 value[10] = 0x00;
129 value[11] = 0x00;
130 value[12] = 0x00;
131 value[13] = 0x00;
132 value[14] = 0x00;
133 value[15] = 0x00;
134
135 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
136
137
138}
139
140static void steelseries_srws1_led_all_set_brightness(struct led_classdev *led_cdev,
141 enum led_brightness value)
142{
143 struct device *dev = led_cdev->dev->parent;
144 struct hid_device *hid = to_hid_device(dev);
145 struct steelseries_srws1_data *drv_data = hid_get_drvdata(hid);
146
147 if (!drv_data) {
148 hid_err(hid, "Device data not found.");
149 return;
150 }
151
152 if (value == LED_OFF)
153 drv_data->led_state = 0;
154 else
155 drv_data->led_state = (1 << (SRWS1_NUMBER_LEDS + 1)) - 1;
156
157 steelseries_srws1_set_leds(hid, drv_data->led_state);
158}
159
160static enum led_brightness steelseries_srws1_led_all_get_brightness(struct led_classdev *led_cdev)
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;
165
166 drv_data = hid_get_drvdata(hid);
167
168 if (!drv_data) {
169 hid_err(hid, "Device data not found.");
170 return LED_OFF;
171 }
172
173 return (drv_data->led_state >> SRWS1_NUMBER_LEDS) ? LED_FULL : LED_OFF;
174}
175
176static void steelseries_srws1_led_set_brightness(struct led_classdev *led_cdev,
177 enum led_brightness value)
178{
179 struct device *dev = led_cdev->dev->parent;
180 struct hid_device *hid = to_hid_device(dev);
181 struct steelseries_srws1_data *drv_data = hid_get_drvdata(hid);
182 int i, state = 0;
183
184 if (!drv_data) {
185 hid_err(hid, "Device data not found.");
186 return;
187 }
188
189 for (i = 0; i < SRWS1_NUMBER_LEDS; i++) {
190 if (led_cdev != drv_data->led[i])
191 continue;
192
193 state = (drv_data->led_state >> i) & 1;
194 if (value == LED_OFF && state) {
195 drv_data->led_state &= ~(1 << i);
196 steelseries_srws1_set_leds(hid, drv_data->led_state);
197 } else if (value != LED_OFF && !state) {
198 drv_data->led_state |= 1 << i;
199 steelseries_srws1_set_leds(hid, drv_data->led_state);
200 }
201 break;
202 }
203}
204
205static enum led_brightness steelseries_srws1_led_get_brightness(struct led_classdev *led_cdev)
206{
207 struct device *dev = led_cdev->dev->parent;
208 struct hid_device *hid = to_hid_device(dev);
209 struct steelseries_srws1_data *drv_data;
210 int i, value = 0;
211
212 drv_data = hid_get_drvdata(hid);
213
214 if (!drv_data) {
215 hid_err(hid, "Device data not found.");
216 return LED_OFF;
217 }
218
219 for (i = 0; i < SRWS1_NUMBER_LEDS; i++)
220 if (led_cdev == drv_data->led[i]) {
221 value = (drv_data->led_state >> i) & 1;
222 break;
223 }
224
225 return value ? LED_FULL : LED_OFF;
226}
227
228static int steelseries_srws1_probe(struct hid_device *hdev,
229 const struct hid_device_id *id)
230{
231 int ret, i;
232 struct led_classdev *led;
233 size_t name_sz;
234 char *name;
235
236 struct steelseries_srws1_data *drv_data = kzalloc(sizeof(*drv_data), GFP_KERNEL);
237
238 if (drv_data == NULL) {
239 hid_err(hdev, "can't alloc SRW-S1 memory\n");
240 return -ENOMEM;
241 }
242
243 hid_set_drvdata(hdev, drv_data);
244
245 ret = hid_parse(hdev);
246 if (ret) {
247 hid_err(hdev, "parse failed\n");
248 goto err_free;
249 }
250
251 if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, 0, 0, 16)) {
252 ret = -ENODEV;
253 goto err_free;
254 }
255
256 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
257 if (ret) {
258 hid_err(hdev, "hw start failed\n");
259 goto err_free;
260 }
261
262
263 drv_data->led_state = 0;
264 for (i = 0; i < SRWS1_NUMBER_LEDS + 1; i++)
265 drv_data->led[i] = NULL;
266
267 steelseries_srws1_set_leds(hdev, 0);
268
269 name_sz = strlen(hdev->uniq) + 16;
270
271
272 led = kzalloc(sizeof(struct led_classdev)+name_sz, GFP_KERNEL);
273 if (!led) {
274 hid_err(hdev, "can't allocate memory for LED ALL\n");
275 goto err_led;
276 }
277
278 name = (void *)(&led[1]);
279 snprintf(name, name_sz, "SRWS1::%s::RPMALL", hdev->uniq);
280 led->name = name;
281 led->brightness = 0;
282 led->max_brightness = 1;
283 led->brightness_get = steelseries_srws1_led_all_get_brightness;
284 led->brightness_set = steelseries_srws1_led_all_set_brightness;
285
286 drv_data->led[SRWS1_NUMBER_LEDS] = led;
287 ret = led_classdev_register(&hdev->dev, led);
288 if (ret)
289 goto err_led;
290
291
292 for (i = 0; i < SRWS1_NUMBER_LEDS; i++) {
293 led = kzalloc(sizeof(struct led_classdev)+name_sz, GFP_KERNEL);
294 if (!led) {
295 hid_err(hdev, "can't allocate memory for LED %d\n", i);
296 goto err_led;
297 }
298
299 name = (void *)(&led[1]);
300 snprintf(name, name_sz, "SRWS1::%s::RPM%d", hdev->uniq, i+1);
301 led->name = name;
302 led->brightness = 0;
303 led->max_brightness = 1;
304 led->brightness_get = steelseries_srws1_led_get_brightness;
305 led->brightness_set = steelseries_srws1_led_set_brightness;
306
307 drv_data->led[i] = led;
308 ret = led_classdev_register(&hdev->dev, led);
309
310 if (ret) {
311 hid_err(hdev, "failed to register LED %d. Aborting.\n", i);
312err_led:
313
314 for (i = 0; i < SRWS1_NUMBER_LEDS + 1; i++) {
315 led = drv_data->led[i];
316 drv_data->led[i] = NULL;
317 if (!led)
318 continue;
319 led_classdev_unregister(led);
320 kfree(led);
321 }
322 goto out;
323 }
324 }
325out:
326 return 0;
327err_free:
328 kfree(drv_data);
329 return ret;
330}
331
332static void steelseries_srws1_remove(struct hid_device *hdev)
333{
334 int i;
335 struct led_classdev *led;
336
337 struct steelseries_srws1_data *drv_data = hid_get_drvdata(hdev);
338
339 if (drv_data) {
340
341 for (i = 0; i < SRWS1_NUMBER_LEDS + 1; i++) {
342 led = drv_data->led[i];
343 drv_data->led[i] = NULL;
344 if (!led)
345 continue;
346 led_classdev_unregister(led);
347 kfree(led);
348 }
349
350 }
351
352 hid_hw_stop(hdev);
353 kfree(drv_data);
354 return;
355}
356#endif
357
358static __u8 *steelseries_srws1_report_fixup(struct hid_device *hdev, __u8 *rdesc,
359 unsigned int *rsize)
360{
361 if (*rsize >= 115 && rdesc[11] == 0x02 && rdesc[13] == 0xc8
362 && rdesc[29] == 0xbb && rdesc[40] == 0xc5) {
363 hid_info(hdev, "Fixing up Steelseries SRW-S1 report descriptor\n");
364 rdesc = steelseries_srws1_rdesc_fixed;
365 *rsize = sizeof(steelseries_srws1_rdesc_fixed);
366 }
367 return rdesc;
368}
369
370static const struct hid_device_id steelseries_srws1_devices[] = {
371 { HID_USB_DEVICE(USB_VENDOR_ID_STEELSERIES, USB_DEVICE_ID_STEELSERIES_SRWS1) },
372 { }
373};
374MODULE_DEVICE_TABLE(hid, steelseries_srws1_devices);
375
376static struct hid_driver steelseries_srws1_driver = {
377 .name = "steelseries_srws1",
378 .id_table = steelseries_srws1_devices,
379#if IS_BUILTIN(CONFIG_LEDS_CLASS) || \
380 (IS_MODULE(CONFIG_LEDS_CLASS) && IS_MODULE(CONFIG_HID_STEELSERIES))
381 .probe = steelseries_srws1_probe,
382 .remove = steelseries_srws1_remove,
383#endif
384 .report_fixup = steelseries_srws1_report_fixup
385};
386
387module_hid_driver(steelseries_srws1_driver);
388MODULE_LICENSE("GPL");
389