Jiri Slaby | 78a849a68 | 2008-06-20 21:26:11 +0200 | [diff] [blame] | 1 | /* |
| 2 | * HID driver for some microsoft "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 |
Jiri Slaby | 78a849a68 | 2008-06-20 21:26:11 +0200 | [diff] [blame] | 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/input.h> |
| 20 | #include <linux/hid.h> |
| 21 | #include <linux/module.h> |
| 22 | |
| 23 | #include "hid-ids.h" |
| 24 | |
Ari Savolainen | 23c10be | 2011-07-11 21:42:52 +0300 | [diff] [blame] | 25 | #define MS_HIDINPUT 0x01 |
| 26 | #define MS_ERGONOMY 0x02 |
| 27 | #define MS_PRESENTER 0x04 |
| 28 | #define MS_RDESC 0x08 |
| 29 | #define MS_NOGET 0x10 |
| 30 | #define MS_DUPLICATE_USAGES 0x20 |
Jiri Slaby | 78a849a68 | 2008-06-20 21:26:11 +0200 | [diff] [blame] | 31 | |
Nikolai Kondrashov | 73e4008 | 2010-08-06 23:03:06 +0400 | [diff] [blame] | 32 | static __u8 *ms_report_fixup(struct hid_device *hdev, __u8 *rdesc, |
| 33 | unsigned int *rsize) |
Jiri Slaby | 78a849a68 | 2008-06-20 21:26:11 +0200 | [diff] [blame] | 34 | { |
| 35 | unsigned long quirks = (unsigned long)hid_get_drvdata(hdev); |
| 36 | |
Jiri Slaby | 3ccc60f | 2012-10-19 13:28:46 +0200 | [diff] [blame] | 37 | /* |
| 38 | * Microsoft Wireless Desktop Receiver (Model 1028) has |
| 39 | * 'Usage Min/Max' where it ought to have 'Physical Min/Max' |
| 40 | */ |
Nikolai Kondrashov | 73e4008 | 2010-08-06 23:03:06 +0400 | [diff] [blame] | 41 | if ((quirks & MS_RDESC) && *rsize == 571 && rdesc[557] == 0x19 && |
Jiri Slaby | 78a849a68 | 2008-06-20 21:26:11 +0200 | [diff] [blame] | 42 | rdesc[559] == 0x29) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 43 | hid_info(hdev, "fixing up Microsoft Wireless Receiver Model 1028 report descriptor\n"); |
Jiri Kosina | 0fb21de | 2009-01-14 03:03:21 +0100 | [diff] [blame] | 44 | rdesc[557] = 0x35; |
| 45 | rdesc[559] = 0x45; |
Jiri Slaby | 78a849a68 | 2008-06-20 21:26:11 +0200 | [diff] [blame] | 46 | } |
Nikolai Kondrashov | 73e4008 | 2010-08-06 23:03:06 +0400 | [diff] [blame] | 47 | return rdesc; |
Jiri Slaby | 78a849a68 | 2008-06-20 21:26:11 +0200 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | #define ms_map_key_clear(c) hid_map_usage_clear(hi, usage, bit, max, \ |
| 51 | EV_KEY, (c)) |
| 52 | static int ms_ergonomy_kb_quirk(struct hid_input *hi, struct hid_usage *usage, |
| 53 | unsigned long **bit, int *max) |
| 54 | { |
| 55 | struct input_dev *input = hi->input; |
| 56 | |
Hans de Goede | 34e75dc | 2014-01-29 17:57:44 +0100 | [diff] [blame] | 57 | if ((usage->hid & HID_USAGE_PAGE) == HID_UP_CONSUMER) { |
| 58 | switch (usage->hid & HID_USAGE) { |
| 59 | /* |
| 60 | * Microsoft uses these 2 reserved usage ids for 2 keys on |
| 61 | * the MS office kb labelled "Office Home" and "Task Pane". |
| 62 | */ |
| 63 | case 0x29d: |
| 64 | ms_map_key_clear(KEY_PROG1); |
| 65 | return 1; |
| 66 | case 0x29e: |
| 67 | ms_map_key_clear(KEY_PROG2); |
| 68 | return 1; |
| 69 | } |
| 70 | return 0; |
| 71 | } |
| 72 | |
Hans de Goede | aac59f6 | 2014-01-29 17:57:42 +0100 | [diff] [blame] | 73 | if ((usage->hid & HID_USAGE_PAGE) != HID_UP_MSVENDOR) |
| 74 | return 0; |
| 75 | |
Jiri Slaby | 78a849a68 | 2008-06-20 21:26:11 +0200 | [diff] [blame] | 76 | switch (usage->hid & HID_USAGE) { |
| 77 | case 0xfd06: ms_map_key_clear(KEY_CHAT); break; |
| 78 | case 0xfd07: ms_map_key_clear(KEY_PHONE); break; |
Hans de Goede | 3faed1a | 2014-01-29 17:57:43 +0100 | [diff] [blame] | 79 | case 0xff00: |
| 80 | /* Special keypad keys */ |
| 81 | ms_map_key_clear(KEY_KPEQUAL); |
| 82 | set_bit(KEY_KPLEFTPAREN, input->keybit); |
| 83 | set_bit(KEY_KPRIGHTPAREN, input->keybit); |
| 84 | break; |
| 85 | case 0xff01: |
| 86 | /* Scroll wheel */ |
| 87 | hid_map_usage_clear(hi, usage, bit, max, EV_REL, REL_WHEEL); |
| 88 | break; |
| 89 | case 0xff02: |
| 90 | /* |
| 91 | * This byte contains a copy of the modifier keys byte of a |
| 92 | * standard hid keyboard report, as send by interface 0 |
| 93 | * (this usage is found on interface 1). |
| 94 | * |
| 95 | * This byte only gets send when another key in the same report |
| 96 | * changes state, and as such is useless, ignore it. |
| 97 | */ |
| 98 | return -1; |
Jiri Slaby | 78a849a68 | 2008-06-20 21:26:11 +0200 | [diff] [blame] | 99 | case 0xff05: |
| 100 | set_bit(EV_REP, input->evbit); |
| 101 | ms_map_key_clear(KEY_F13); |
| 102 | set_bit(KEY_F14, input->keybit); |
| 103 | set_bit(KEY_F15, input->keybit); |
| 104 | set_bit(KEY_F16, input->keybit); |
| 105 | set_bit(KEY_F17, input->keybit); |
| 106 | set_bit(KEY_F18, input->keybit); |
Jiri Kosina | b2b98ea | 2013-12-15 18:25:26 +0100 | [diff] [blame] | 107 | break; |
Jiri Slaby | 78a849a68 | 2008-06-20 21:26:11 +0200 | [diff] [blame] | 108 | default: |
| 109 | return 0; |
| 110 | } |
| 111 | return 1; |
| 112 | } |
| 113 | |
| 114 | static int ms_presenter_8k_quirk(struct hid_input *hi, struct hid_usage *usage, |
| 115 | unsigned long **bit, int *max) |
| 116 | { |
Hans de Goede | aac59f6 | 2014-01-29 17:57:42 +0100 | [diff] [blame] | 117 | if ((usage->hid & HID_USAGE_PAGE) != HID_UP_MSVENDOR) |
| 118 | return 0; |
| 119 | |
Jiri Slaby | 78a849a68 | 2008-06-20 21:26:11 +0200 | [diff] [blame] | 120 | set_bit(EV_REP, hi->input->evbit); |
| 121 | switch (usage->hid & HID_USAGE) { |
| 122 | case 0xfd08: ms_map_key_clear(KEY_FORWARD); break; |
| 123 | case 0xfd09: ms_map_key_clear(KEY_BACK); break; |
| 124 | case 0xfd0b: ms_map_key_clear(KEY_PLAYPAUSE); break; |
| 125 | case 0xfd0e: ms_map_key_clear(KEY_CLOSE); break; |
| 126 | case 0xfd0f: ms_map_key_clear(KEY_PLAY); break; |
| 127 | default: |
| 128 | return 0; |
| 129 | } |
| 130 | return 1; |
| 131 | } |
| 132 | |
| 133 | static int ms_input_mapping(struct hid_device *hdev, struct hid_input *hi, |
| 134 | struct hid_field *field, struct hid_usage *usage, |
| 135 | unsigned long **bit, int *max) |
| 136 | { |
| 137 | unsigned long quirks = (unsigned long)hid_get_drvdata(hdev); |
| 138 | |
Jiri Slaby | 78a849a68 | 2008-06-20 21:26:11 +0200 | [diff] [blame] | 139 | if (quirks & MS_ERGONOMY) { |
| 140 | int ret = ms_ergonomy_kb_quirk(hi, usage, bit, max); |
| 141 | if (ret) |
| 142 | return ret; |
| 143 | } |
| 144 | |
| 145 | if ((quirks & MS_PRESENTER) && |
| 146 | ms_presenter_8k_quirk(hi, usage, bit, max)) |
| 147 | return 1; |
| 148 | |
| 149 | return 0; |
| 150 | } |
| 151 | |
Ari Savolainen | 23c10be | 2011-07-11 21:42:52 +0300 | [diff] [blame] | 152 | static int ms_input_mapped(struct hid_device *hdev, struct hid_input *hi, |
| 153 | struct hid_field *field, struct hid_usage *usage, |
| 154 | unsigned long **bit, int *max) |
| 155 | { |
| 156 | unsigned long quirks = (unsigned long)hid_get_drvdata(hdev); |
| 157 | |
| 158 | if (quirks & MS_DUPLICATE_USAGES) |
| 159 | clear_bit(usage->code, *bit); |
| 160 | |
| 161 | return 0; |
| 162 | } |
| 163 | |
Jiri Slaby | 78a849a68 | 2008-06-20 21:26:11 +0200 | [diff] [blame] | 164 | static int ms_event(struct hid_device *hdev, struct hid_field *field, |
| 165 | struct hid_usage *usage, __s32 value) |
| 166 | { |
| 167 | unsigned long quirks = (unsigned long)hid_get_drvdata(hdev); |
Hans de Goede | 3faed1a | 2014-01-29 17:57:43 +0100 | [diff] [blame] | 168 | struct input_dev *input; |
Jiri Slaby | 78a849a68 | 2008-06-20 21:26:11 +0200 | [diff] [blame] | 169 | |
| 170 | if (!(hdev->claimed & HID_CLAIMED_INPUT) || !field->hidinput || |
| 171 | !usage->type) |
| 172 | return 0; |
| 173 | |
Hans de Goede | 3faed1a | 2014-01-29 17:57:43 +0100 | [diff] [blame] | 174 | input = field->hidinput->input; |
| 175 | |
Jiri Slaby | 78a849a68 | 2008-06-20 21:26:11 +0200 | [diff] [blame] | 176 | /* Handling MS keyboards special buttons */ |
Hans de Goede | 3faed1a | 2014-01-29 17:57:43 +0100 | [diff] [blame] | 177 | if (quirks & MS_ERGONOMY && usage->hid == (HID_UP_MSVENDOR | 0xff00)) { |
| 178 | /* Special keypad keys */ |
| 179 | input_report_key(input, KEY_KPEQUAL, value & 0x01); |
| 180 | input_report_key(input, KEY_KPLEFTPAREN, value & 0x02); |
| 181 | input_report_key(input, KEY_KPRIGHTPAREN, value & 0x04); |
| 182 | return 1; |
| 183 | } |
| 184 | |
| 185 | if (quirks & MS_ERGONOMY && usage->hid == (HID_UP_MSVENDOR | 0xff01)) { |
| 186 | /* Scroll wheel */ |
| 187 | int step = ((value & 0x60) >> 5) + 1; |
| 188 | |
| 189 | switch (value & 0x1f) { |
| 190 | case 0x01: |
| 191 | input_report_rel(input, REL_WHEEL, step); |
| 192 | break; |
| 193 | case 0x1f: |
| 194 | input_report_rel(input, REL_WHEEL, -step); |
| 195 | break; |
| 196 | } |
| 197 | return 1; |
| 198 | } |
| 199 | |
Jiri Slaby | 78a849a68 | 2008-06-20 21:26:11 +0200 | [diff] [blame] | 200 | if (quirks & MS_ERGONOMY && usage->hid == (HID_UP_MSVENDOR | 0xff05)) { |
Jiri Slaby | 78a849a68 | 2008-06-20 21:26:11 +0200 | [diff] [blame] | 201 | static unsigned int last_key = 0; |
| 202 | unsigned int key = 0; |
| 203 | switch (value) { |
| 204 | case 0x01: key = KEY_F14; break; |
| 205 | case 0x02: key = KEY_F15; break; |
| 206 | case 0x04: key = KEY_F16; break; |
| 207 | case 0x08: key = KEY_F17; break; |
| 208 | case 0x10: key = KEY_F18; break; |
| 209 | } |
| 210 | if (key) { |
| 211 | input_event(input, usage->type, key, 1); |
| 212 | last_key = key; |
| 213 | } else |
| 214 | input_event(input, usage->type, last_key, 0); |
| 215 | |
| 216 | return 1; |
| 217 | } |
| 218 | |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | static int ms_probe(struct hid_device *hdev, const struct hid_device_id *id) |
| 223 | { |
| 224 | unsigned long quirks = id->driver_data; |
| 225 | int ret; |
| 226 | |
| 227 | hid_set_drvdata(hdev, (void *)quirks); |
| 228 | |
Jiri Slaby | 78a849a68 | 2008-06-20 21:26:11 +0200 | [diff] [blame] | 229 | if (quirks & MS_NOGET) |
| 230 | hdev->quirks |= HID_QUIRK_NOGET; |
| 231 | |
| 232 | ret = hid_parse(hdev); |
| 233 | if (ret) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 234 | hid_err(hdev, "parse failed\n"); |
Jiri Slaby | 78a849a68 | 2008-06-20 21:26:11 +0200 | [diff] [blame] | 235 | goto err_free; |
| 236 | } |
| 237 | |
Jiri Slaby | 93c1013 | 2008-06-27 00:04:24 +0200 | [diff] [blame] | 238 | ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT | ((quirks & MS_HIDINPUT) ? |
| 239 | HID_CONNECT_HIDINPUT_FORCE : 0)); |
Jiri Slaby | 78a849a68 | 2008-06-20 21:26:11 +0200 | [diff] [blame] | 240 | if (ret) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 241 | hid_err(hdev, "hw start failed\n"); |
Jiri Slaby | 78a849a68 | 2008-06-20 21:26:11 +0200 | [diff] [blame] | 242 | goto err_free; |
| 243 | } |
| 244 | |
| 245 | return 0; |
| 246 | err_free: |
| 247 | return ret; |
| 248 | } |
| 249 | |
| 250 | static const struct hid_device_id ms_devices[] = { |
| 251 | { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_SIDEWINDER_GV), |
| 252 | .driver_data = MS_HIDINPUT }, |
Hans de Goede | 3faed1a | 2014-01-29 17:57:43 +0100 | [diff] [blame] | 253 | { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_OFFICE_KB), |
| 254 | .driver_data = MS_ERGONOMY }, |
Jiri Slaby | 78a849a68 | 2008-06-20 21:26:11 +0200 | [diff] [blame] | 255 | { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_NE4K), |
| 256 | .driver_data = MS_ERGONOMY }, |
Adam Jiang | 89759e2 | 2013-04-29 13:27:31 +0900 | [diff] [blame] | 257 | { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_NE4K_JP), |
| 258 | .driver_data = MS_ERGONOMY }, |
Jakub Sitnicki | ef567cf | 2015-02-21 20:51:08 +0100 | [diff] [blame] | 259 | { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_NE7K), |
| 260 | .driver_data = MS_ERGONOMY }, |
Jiri Slaby | 78a849a68 | 2008-06-20 21:26:11 +0200 | [diff] [blame] | 261 | { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_LK6K), |
| 262 | .driver_data = MS_ERGONOMY | MS_RDESC }, |
| 263 | { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_PRESENTER_8K_USB), |
| 264 | .driver_data = MS_PRESENTER }, |
Jiri Kosina | f3b83d7 | 2011-06-13 23:08:18 +0300 | [diff] [blame] | 265 | { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_DIGITAL_MEDIA_3K), |
Michel Hermier | 52dc085 | 2016-09-20 19:50:51 +0200 | [diff] [blame] | 266 | .driver_data = MS_ERGONOMY }, |
Alistair Leslie-Hughes | c847a89 | 2016-04-04 20:51:40 +1000 | [diff] [blame] | 267 | { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_DIGITAL_MEDIA_7K), |
| 268 | .driver_data = MS_ERGONOMY }, |
| 269 | { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_DIGITAL_MEDIA_600), |
| 270 | .driver_data = MS_ERGONOMY }, |
| 271 | { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_DIGITAL_MEDIA_3KV1), |
| 272 | .driver_data = MS_ERGONOMY }, |
Jiri Slaby | 78a849a68 | 2008-06-20 21:26:11 +0200 | [diff] [blame] | 273 | { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_WIRELESS_OPTICAL_DESKTOP_3_0), |
| 274 | .driver_data = MS_NOGET }, |
Ari Savolainen | 23c10be | 2011-07-11 21:42:52 +0300 | [diff] [blame] | 275 | { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_COMFORT_MOUSE_4500), |
| 276 | .driver_data = MS_DUPLICATE_USAGES }, |
Raimund Roth | 18eec2c | 2015-06-08 11:11:38 +0200 | [diff] [blame] | 277 | { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_POWER_COVER), |
| 278 | .driver_data = MS_HIDINPUT }, |
Slava Bacherikov | f9a82c2 | 2016-03-11 18:57:24 +0200 | [diff] [blame] | 279 | { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_COMFORT_KEYBOARD), |
| 280 | .driver_data = MS_ERGONOMY}, |
Jiri Slaby | 78a849a68 | 2008-06-20 21:26:11 +0200 | [diff] [blame] | 281 | |
| 282 | { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_PRESENTER_8K_BT), |
| 283 | .driver_data = MS_PRESENTER }, |
| 284 | { } |
| 285 | }; |
| 286 | MODULE_DEVICE_TABLE(hid, ms_devices); |
| 287 | |
| 288 | static struct hid_driver ms_driver = { |
| 289 | .name = "microsoft", |
| 290 | .id_table = ms_devices, |
| 291 | .report_fixup = ms_report_fixup, |
| 292 | .input_mapping = ms_input_mapping, |
Ari Savolainen | 23c10be | 2011-07-11 21:42:52 +0300 | [diff] [blame] | 293 | .input_mapped = ms_input_mapped, |
Jiri Slaby | 78a849a68 | 2008-06-20 21:26:11 +0200 | [diff] [blame] | 294 | .event = ms_event, |
| 295 | .probe = ms_probe, |
| 296 | }; |
H Hartley Sweeten | f425458 | 2012-12-17 15:28:26 -0700 | [diff] [blame] | 297 | module_hid_driver(ms_driver); |
Jiri Slaby | 78a849a68 | 2008-06-20 21:26:11 +0200 | [diff] [blame] | 298 | |
Jiri Slaby | 78a849a68 | 2008-06-20 21:26:11 +0200 | [diff] [blame] | 299 | MODULE_LICENSE("GPL"); |