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