GNU Linux-libre 4.4.289-gnu1
[releases.git] / drivers / hid / hid-cypress.c
1 /*
2  *  HID driver for some cypress "special" devices
3  *
4  *  Copyright (c) 1999 Andreas Gal
5  *  Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
6  *  Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
7  *  Copyright (c) 2006-2007 Jiri Kosina
8  *  Copyright (c) 2008 Jiri Slaby
9  */
10
11 /*
12  * This program is free software; you can redistribute it and/or modify it
13  * under the terms of the GNU General Public License as published by the Free
14  * Software Foundation; either version 2 of the License, or (at your option)
15  * any later version.
16  */
17
18 #include <linux/device.h>
19 #include <linux/hid.h>
20 #include <linux/input.h>
21 #include <linux/module.h>
22
23 #include "hid-ids.h"
24
25 #define CP_RDESC_SWAPPED_MIN_MAX        0x01
26 #define CP_2WHEEL_MOUSE_HACK            0x02
27 #define CP_2WHEEL_MOUSE_HACK_ON         0x04
28
29 #define VA_INVAL_LOGICAL_BOUNDARY       0x08
30
31 /*
32  * Some USB barcode readers from cypress have usage min and usage max in
33  * the wrong order
34  */
35 static __u8 *cp_rdesc_fixup(struct hid_device *hdev, __u8 *rdesc,
36                 unsigned int *rsize)
37 {
38         unsigned int i;
39
40         if (*rsize < 4)
41                 return rdesc;
42
43         for (i = 0; i < *rsize - 4; i++)
44                 if (rdesc[i] == 0x29 && rdesc[i + 2] == 0x19) {
45                         rdesc[i] = 0x19;
46                         rdesc[i + 2] = 0x29;
47                         swap(rdesc[i + 3], rdesc[i + 1]);
48                 }
49         return rdesc;
50 }
51
52 static __u8 *va_logical_boundary_fixup(struct hid_device *hdev, __u8 *rdesc,
53                 unsigned int *rsize)
54 {
55         /*
56          * Varmilo VA104M (with VID Cypress and device ID 07B1) incorrectly
57          * reports Logical Minimum of its Consumer Control device as 572
58          * (0x02 0x3c). Fix this by setting its Logical Minimum to zero.
59          */
60         if (*rsize == 25 &&
61                         rdesc[0] == 0x05 && rdesc[1] == 0x0c &&
62                         rdesc[2] == 0x09 && rdesc[3] == 0x01 &&
63                         rdesc[6] == 0x19 && rdesc[7] == 0x00 &&
64                         rdesc[11] == 0x16 && rdesc[12] == 0x3c && rdesc[13] == 0x02) {
65                 hid_info(hdev,
66                          "fixing up varmilo VA104M consumer control report descriptor\n");
67                 rdesc[12] = 0x00;
68                 rdesc[13] = 0x00;
69         }
70         return rdesc;
71 }
72
73 static __u8 *cp_report_fixup(struct hid_device *hdev, __u8 *rdesc,
74                 unsigned int *rsize)
75 {
76         unsigned long quirks = (unsigned long)hid_get_drvdata(hdev);
77
78         if (quirks & CP_RDESC_SWAPPED_MIN_MAX)
79                 rdesc = cp_rdesc_fixup(hdev, rdesc, rsize);
80         if (quirks & VA_INVAL_LOGICAL_BOUNDARY)
81                 rdesc = va_logical_boundary_fixup(hdev, rdesc, rsize);
82
83         return rdesc;
84 }
85
86 static int cp_input_mapped(struct hid_device *hdev, struct hid_input *hi,
87                 struct hid_field *field, struct hid_usage *usage,
88                 unsigned long **bit, int *max)
89 {
90         unsigned long quirks = (unsigned long)hid_get_drvdata(hdev);
91
92         if (!(quirks & CP_2WHEEL_MOUSE_HACK))
93                 return 0;
94
95         if (usage->type == EV_REL && usage->code == REL_WHEEL)
96                 set_bit(REL_HWHEEL, *bit);
97         if (usage->hid == 0x00090005)
98                 return -1;
99
100         return 0;
101 }
102
103 static int cp_event(struct hid_device *hdev, struct hid_field *field,
104                 struct hid_usage *usage, __s32 value)
105 {
106         unsigned long quirks = (unsigned long)hid_get_drvdata(hdev);
107
108         if (!(hdev->claimed & HID_CLAIMED_INPUT) || !field->hidinput ||
109                         !usage->type || !(quirks & CP_2WHEEL_MOUSE_HACK))
110                 return 0;
111
112         if (usage->hid == 0x00090005) {
113                 if (value)
114                         quirks |=  CP_2WHEEL_MOUSE_HACK_ON;
115                 else
116                         quirks &= ~CP_2WHEEL_MOUSE_HACK_ON;
117                 hid_set_drvdata(hdev, (void *)quirks);
118                 return 1;
119         }
120
121         if (usage->code == REL_WHEEL && (quirks & CP_2WHEEL_MOUSE_HACK_ON)) {
122                 struct input_dev *input = field->hidinput->input;
123
124                 input_event(input, usage->type, REL_HWHEEL, value);
125                 return 1;
126         }
127
128         return 0;
129 }
130
131 static int cp_probe(struct hid_device *hdev, const struct hid_device_id *id)
132 {
133         unsigned long quirks = id->driver_data;
134         int ret;
135
136         hid_set_drvdata(hdev, (void *)quirks);
137
138         ret = hid_parse(hdev);
139         if (ret) {
140                 hid_err(hdev, "parse failed\n");
141                 goto err_free;
142         }
143
144         ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
145         if (ret) {
146                 hid_err(hdev, "hw start failed\n");
147                 goto err_free;
148         }
149
150         return 0;
151 err_free:
152         return ret;
153 }
154
155 static const struct hid_device_id cp_devices[] = {
156         { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_BARCODE_1),
157                 .driver_data = CP_RDESC_SWAPPED_MIN_MAX },
158         { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_BARCODE_2),
159                 .driver_data = CP_RDESC_SWAPPED_MIN_MAX },
160         { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_BARCODE_3),
161                 .driver_data = CP_RDESC_SWAPPED_MIN_MAX },
162         { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_BARCODE_4),
163                 .driver_data = CP_RDESC_SWAPPED_MIN_MAX },
164         { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_MOUSE),
165                 .driver_data = CP_2WHEEL_MOUSE_HACK },
166         { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_VARMILO_VA104M_07B1),
167                 .driver_data = VA_INVAL_LOGICAL_BOUNDARY },
168         { }
169 };
170 MODULE_DEVICE_TABLE(hid, cp_devices);
171
172 static struct hid_driver cp_driver = {
173         .name = "cypress",
174         .id_table = cp_devices,
175         .report_fixup = cp_report_fixup,
176         .input_mapped = cp_input_mapped,
177         .event = cp_event,
178         .probe = cp_probe,
179 };
180 module_hid_driver(cp_driver);
181
182 MODULE_LICENSE("GPL");