blob: c6ee632bfd68df219e24701e3dcadbf1b810bc1e [file] [log] [blame]
Jiri Kosinadde58452006-12-08 18:40:44 +01001/*
Jiri Kosinadde58452006-12-08 18:40:44 +01002 * Copyright (c) 2000-2001 Vojtech Pavlik
Jiri Kosina7d39e842010-02-02 20:46:34 +01003 * Copyright (c) 2006-2010 Jiri Kosina
Jiri Kosinadde58452006-12-08 18:40:44 +01004 *
5 * HID to Linux Input mapping
6 */
7
8/*
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 * Should you need to contact me, the author, you can do so either by
24 * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
25 * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
26 */
27
28#include <linux/module.h>
29#include <linux/slab.h>
30#include <linux/kernel.h>
Jiri Kosinadde58452006-12-08 18:40:44 +010031
Jiri Kosinadde58452006-12-08 18:40:44 +010032#include <linux/hid.h>
Jiri Kosinac080d892007-01-25 11:43:31 +010033#include <linux/hid-debug.h>
Jiri Kosinadde58452006-12-08 18:40:44 +010034
35#define unk KEY_UNKNOWN
36
37static const unsigned char hid_keyboard[256] = {
38 0, 0, 0, 0, 30, 48, 46, 32, 18, 33, 34, 35, 23, 36, 37, 38,
39 50, 49, 24, 25, 16, 19, 31, 20, 22, 47, 17, 45, 21, 44, 2, 3,
40 4, 5, 6, 7, 8, 9, 10, 11, 28, 1, 14, 15, 57, 12, 13, 26,
41 27, 43, 43, 39, 40, 41, 51, 52, 53, 58, 59, 60, 61, 62, 63, 64,
42 65, 66, 67, 68, 87, 88, 99, 70,119,110,102,104,111,107,109,106,
43 105,108,103, 69, 98, 55, 74, 78, 96, 79, 80, 81, 75, 76, 77, 71,
44 72, 73, 82, 83, 86,127,116,117,183,184,185,186,187,188,189,190,
45 191,192,193,194,134,138,130,132,128,129,131,137,133,135,136,113,
46 115,114,unk,unk,unk,121,unk, 89, 93,124, 92, 94, 95,unk,unk,unk,
Jarod Wilson437f3b12011-04-19 15:28:30 -040047 122,123, 90, 91, 85,unk,unk,unk,unk,unk,unk,unk,111,unk,unk,unk,
Jiri Kosinadde58452006-12-08 18:40:44 +010048 unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,
Jiri Kosina1fe8736d2007-08-09 13:24:11 +020049 unk,unk,unk,unk,unk,unk,179,180,unk,unk,unk,unk,unk,unk,unk,unk,
Jiri Kosinadde58452006-12-08 18:40:44 +010050 unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,
Jarod Wilson437f3b12011-04-19 15:28:30 -040051 unk,unk,unk,unk,unk,unk,unk,unk,111,unk,unk,unk,unk,unk,unk,unk,
Jiri Kosinadde58452006-12-08 18:40:44 +010052 29, 42, 56,125, 97, 54,100,126,164,166,165,163,161,115,114,113,
53 150,158,159,128,136,177,178,176,142,152,173,140,unk,unk,unk,unk
54};
55
56static const struct {
57 __s32 x;
58 __s32 y;
59} hid_hat_to_axis[] = {{ 0, 0}, { 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}};
60
Jiri Slaby022e8c42008-05-16 11:49:18 +020061#define map_abs(c) hid_map_usage(hidinput, usage, &bit, &max, EV_ABS, (c))
62#define map_rel(c) hid_map_usage(hidinput, usage, &bit, &max, EV_REL, (c))
63#define map_key(c) hid_map_usage(hidinput, usage, &bit, &max, EV_KEY, (c))
64#define map_led(c) hid_map_usage(hidinput, usage, &bit, &max, EV_LED, (c))
Jiri Kosinadde58452006-12-08 18:40:44 +010065
Jiri Slaby022e8c42008-05-16 11:49:18 +020066#define map_abs_clear(c) hid_map_usage_clear(hidinput, usage, &bit, \
67 &max, EV_ABS, (c))
68#define map_key_clear(c) hid_map_usage_clear(hidinput, usage, &bit, \
69 &max, EV_KEY, (c))
Jiri Kosinadde58452006-12-08 18:40:44 +010070
Dmitry Torokhova0bf0ea2010-09-09 21:57:17 -070071static bool match_scancode(struct hid_usage *usage,
72 unsigned int cur_idx, unsigned int scancode)
Marvin Raaijmakersfe7ba312007-05-09 10:57:20 +020073{
Dmitry Torokhova0bf0ea2010-09-09 21:57:17 -070074 return (usage->hid & (HID_USAGE_PAGE | HID_USAGE)) == scancode;
Marvin Raaijmakersfe7ba312007-05-09 10:57:20 +020075}
76
Dmitry Torokhova0bf0ea2010-09-09 21:57:17 -070077static bool match_keycode(struct hid_usage *usage,
78 unsigned int cur_idx, unsigned int keycode)
Marvin Raaijmakersfe7ba312007-05-09 10:57:20 +020079{
Dmitry Torokhovf5854fa2010-09-15 19:36:56 -070080 /*
81 * We should exclude unmapped usages when doing lookup by keycode.
82 */
83 return (usage->type == EV_KEY && usage->code == keycode);
Marvin Raaijmakersfe7ba312007-05-09 10:57:20 +020084}
85
Dmitry Torokhova0bf0ea2010-09-09 21:57:17 -070086static bool match_index(struct hid_usage *usage,
87 unsigned int cur_idx, unsigned int idx)
88{
89 return cur_idx == idx;
90}
91
92typedef bool (*hid_usage_cmp_t)(struct hid_usage *usage,
93 unsigned int cur_idx, unsigned int val);
94
Marvin Raaijmakersfe7ba312007-05-09 10:57:20 +020095static struct hid_usage *hidinput_find_key(struct hid_device *hid,
Dmitry Torokhova0bf0ea2010-09-09 21:57:17 -070096 hid_usage_cmp_t match,
97 unsigned int value,
98 unsigned int *usage_idx)
Marvin Raaijmakersfe7ba312007-05-09 10:57:20 +020099{
Dmitry Torokhova0bf0ea2010-09-09 21:57:17 -0700100 unsigned int i, j, k, cur_idx = 0;
Marvin Raaijmakersfe7ba312007-05-09 10:57:20 +0200101 struct hid_report *report;
102 struct hid_usage *usage;
103
104 for (k = HID_INPUT_REPORT; k <= HID_OUTPUT_REPORT; k++) {
105 list_for_each_entry(report, &hid->report_enum[k].report_list, list) {
106 for (i = 0; i < report->maxfield; i++) {
Dmitry Torokhova0bf0ea2010-09-09 21:57:17 -0700107 for (j = 0; j < report->field[i]->maxusage; j++) {
Marvin Raaijmakersfe7ba312007-05-09 10:57:20 +0200108 usage = report->field[i]->usage + j;
Dmitry Torokhovf5854fa2010-09-15 19:36:56 -0700109 if (usage->type == EV_KEY || usage->type == 0) {
Dmitry Torokhova0bf0ea2010-09-09 21:57:17 -0700110 if (match(usage, cur_idx, value)) {
111 if (usage_idx)
112 *usage_idx = cur_idx;
113 return usage;
114 }
115 cur_idx++;
116 }
Marvin Raaijmakersfe7ba312007-05-09 10:57:20 +0200117 }
118 }
119 }
120 }
121 return NULL;
122}
123
Dmitry Torokhova0bf0ea2010-09-09 21:57:17 -0700124static struct hid_usage *hidinput_locate_usage(struct hid_device *hid,
125 const struct input_keymap_entry *ke,
126 unsigned int *index)
127{
128 struct hid_usage *usage;
129 unsigned int scancode;
130
131 if (ke->flags & INPUT_KEYMAP_BY_INDEX)
132 usage = hidinput_find_key(hid, match_index, ke->index, index);
133 else if (input_scancode_to_scalar(ke, &scancode) == 0)
134 usage = hidinput_find_key(hid, match_scancode, scancode, index);
135 else
136 usage = NULL;
137
138 return usage;
139}
140
Dmitry Torokhov58b93992010-03-08 22:37:10 -0800141static int hidinput_getkeycode(struct input_dev *dev,
Dmitry Torokhova0bf0ea2010-09-09 21:57:17 -0700142 struct input_keymap_entry *ke)
Marvin Raaijmakersfe7ba312007-05-09 10:57:20 +0200143{
Dmitry Torokhovf202df62007-10-31 12:33:26 +0100144 struct hid_device *hid = input_get_drvdata(dev);
Marvin Raaijmakersfe7ba312007-05-09 10:57:20 +0200145 struct hid_usage *usage;
Dmitry Torokhova0bf0ea2010-09-09 21:57:17 -0700146 unsigned int scancode, index;
Jiri Kosina77b29662007-08-09 14:04:56 +0200147
Dmitry Torokhova0bf0ea2010-09-09 21:57:17 -0700148 usage = hidinput_locate_usage(hid, ke, &index);
Marvin Raaijmakersfe7ba312007-05-09 10:57:20 +0200149 if (usage) {
Dmitry Torokhovf5854fa2010-09-15 19:36:56 -0700150 ke->keycode = usage->type == EV_KEY ?
151 usage->code : KEY_RESERVED;
Dmitry Torokhova0bf0ea2010-09-09 21:57:17 -0700152 ke->index = index;
153 scancode = usage->hid & (HID_USAGE_PAGE | HID_USAGE);
154 ke->len = sizeof(scancode);
155 memcpy(ke->scancode, &scancode, sizeof(scancode));
Marvin Raaijmakersfe7ba312007-05-09 10:57:20 +0200156 return 0;
157 }
Dmitry Torokhova0bf0ea2010-09-09 21:57:17 -0700158
Marvin Raaijmakersfe7ba312007-05-09 10:57:20 +0200159 return -EINVAL;
160}
161
Dmitry Torokhov58b93992010-03-08 22:37:10 -0800162static int hidinput_setkeycode(struct input_dev *dev,
Dmitry Torokhova0bf0ea2010-09-09 21:57:17 -0700163 const struct input_keymap_entry *ke,
164 unsigned int *old_keycode)
Marvin Raaijmakersfe7ba312007-05-09 10:57:20 +0200165{
Dmitry Torokhovf202df62007-10-31 12:33:26 +0100166 struct hid_device *hid = input_get_drvdata(dev);
Marvin Raaijmakersfe7ba312007-05-09 10:57:20 +0200167 struct hid_usage *usage;
Jiri Kosina77b29662007-08-09 14:04:56 +0200168
Dmitry Torokhova0bf0ea2010-09-09 21:57:17 -0700169 usage = hidinput_locate_usage(hid, ke, NULL);
Marvin Raaijmakersfe7ba312007-05-09 10:57:20 +0200170 if (usage) {
Dmitry Torokhovf5854fa2010-09-15 19:36:56 -0700171 *old_keycode = usage->type == EV_KEY ?
172 usage->code : KEY_RESERVED;
Dmitry Torokhova0bf0ea2010-09-09 21:57:17 -0700173 usage->code = ke->keycode;
Jiri Kosina77b29662007-08-09 14:04:56 +0200174
Dmitry Torokhova0bf0ea2010-09-09 21:57:17 -0700175 clear_bit(*old_keycode, dev->keybit);
Marvin Raaijmakersfe7ba312007-05-09 10:57:20 +0200176 set_bit(usage->code, dev->keybit);
Joe Perches587d1452010-10-25 19:44:21 -0700177 dbg_hid("Assigned keycode %d to HID usage code %x\n",
Dmitry Torokhova0bf0ea2010-09-09 21:57:17 -0700178 usage->code, usage->hid);
179
180 /*
181 * Set the keybit for the old keycode if the old keycode is used
182 * by another key
183 */
184 if (hidinput_find_key(hid, match_keycode, *old_keycode, NULL))
185 set_bit(*old_keycode, dev->keybit);
Jiri Kosina77b29662007-08-09 14:04:56 +0200186
Marvin Raaijmakersfe7ba312007-05-09 10:57:20 +0200187 return 0;
188 }
Jiri Kosina77b29662007-08-09 14:04:56 +0200189
Marvin Raaijmakersfe7ba312007-05-09 10:57:20 +0200190 return -EINVAL;
191}
192
193
Nikolai Kondrashov4ea6e4f2010-09-15 14:51:14 +0400194/**
195 * hidinput_calc_abs_res - calculate an absolute axis resolution
196 * @field: the HID report field to calculate resolution for
197 * @code: axis code
198 *
199 * The formula is:
200 * (logical_maximum - logical_minimum)
201 * resolution = ----------------------------------------------------------
202 * (physical_maximum - physical_minimum) * 10 ^ unit_exponent
203 *
204 * as seen in the HID specification v1.11 6.2.2.7 Global Items.
205 *
Dmitry Torokhov83ed79c2010-12-02 11:49:06 +0100206 * Only exponent 1 length units are processed. Centimeters and inches are
207 * converted to millimeters. Degrees are converted to radians.
Nikolai Kondrashov4ea6e4f2010-09-15 14:51:14 +0400208 */
209static __s32 hidinput_calc_abs_res(const struct hid_field *field, __u16 code)
210{
211 __s32 unit_exponent = field->unit_exponent;
212 __s32 logical_extents = field->logical_maximum -
213 field->logical_minimum;
214 __s32 physical_extents = field->physical_maximum -
215 field->physical_minimum;
216 __s32 prev;
217
218 /* Check if the extents are sane */
219 if (logical_extents <= 0 || physical_extents <= 0)
220 return 0;
221
222 /*
223 * Verify and convert units.
224 * See HID specification v1.11 6.2.2.7 Global Items for unit decoding
225 */
226 if (code == ABS_X || code == ABS_Y || code == ABS_Z) {
227 if (field->unit == 0x11) { /* If centimeters */
Dmitry Torokhov83ed79c2010-12-02 11:49:06 +0100228 /* Convert to millimeters */
229 unit_exponent += 1;
230 } else if (field->unit == 0x13) { /* If inches */
231 /* Convert to millimeters */
232 prev = physical_extents;
233 physical_extents *= 254;
234 if (physical_extents < prev)
Nikolai Kondrashov4ea6e4f2010-09-15 14:51:14 +0400235 return 0;
Dmitry Torokhov83ed79c2010-12-02 11:49:06 +0100236 unit_exponent -= 1;
237 } else {
Nikolai Kondrashov4ea6e4f2010-09-15 14:51:14 +0400238 return 0;
239 }
240 } else if (code == ABS_RX || code == ABS_RY || code == ABS_RZ) {
241 if (field->unit == 0x14) { /* If degrees */
242 /* Convert to radians */
243 prev = logical_extents;
244 logical_extents *= 573;
245 if (logical_extents < prev)
246 return 0;
247 unit_exponent += 1;
248 } else if (field->unit != 0x12) { /* If not radians */
249 return 0;
250 }
251 } else {
252 return 0;
253 }
254
255 /* Apply negative unit exponent */
256 for (; unit_exponent < 0; unit_exponent++) {
257 prev = logical_extents;
258 logical_extents *= 10;
259 if (logical_extents < prev)
260 return 0;
261 }
262 /* Apply positive unit exponent */
263 for (; unit_exponent > 0; unit_exponent--) {
264 prev = physical_extents;
265 physical_extents *= 10;
266 if (physical_extents < prev)
267 return 0;
268 }
269
270 /* Calculate resolution */
271 return logical_extents / physical_extents;
272}
273
Jeremy Fitzhardinge4f5ca832011-11-23 00:49:14 -0800274#ifdef CONFIG_HID_BATTERY_STRENGTH
275static enum power_supply_property hidinput_battery_props[] = {
276 POWER_SUPPLY_PROP_PRESENT,
277 POWER_SUPPLY_PROP_ONLINE,
278 POWER_SUPPLY_PROP_CAPACITY,
279 POWER_SUPPLY_PROP_MODEL_NAME,
280};
281
282static int hidinput_get_battery_property(struct power_supply *psy,
283 enum power_supply_property prop,
284 union power_supply_propval *val)
285{
286 struct hid_device *dev = container_of(psy, struct hid_device, battery);
287 int ret = 0;
288
289 switch (prop) {
290 case POWER_SUPPLY_PROP_PRESENT:
291 case POWER_SUPPLY_PROP_ONLINE:
292 val->intval = 1;
293 break;
294
295 case POWER_SUPPLY_PROP_CAPACITY:
296 if (dev->battery_min < dev->battery_max &&
297 dev->battery_val >= dev->battery_min &&
298 dev->battery_val <= dev->battery_max)
299 val->intval = (100 * (dev->battery_val - dev->battery_min)) /
300 (dev->battery_max - dev->battery_min);
301 else
302 ret = -EINVAL;
303 break;
304
305 case POWER_SUPPLY_PROP_MODEL_NAME:
306 val->strval = dev->name;
307 break;
308
309 default:
310 ret = -EINVAL;
311 break;
312 }
313
314 return ret;
315}
316
317static void hidinput_setup_battery(struct hid_device *dev, s32 min, s32 max)
318{
319 struct power_supply *battery = &dev->battery;
320 int ret;
321
322 if (battery->name != NULL)
323 return; /* already initialized? */
324
325 battery->name = kasprintf(GFP_KERNEL, "hid-%s-battery", dev->uniq);
326 if (battery->name == NULL)
327 return;
328
329 battery->type = POWER_SUPPLY_TYPE_BATTERY;
330 battery->properties = hidinput_battery_props;
331 battery->num_properties = ARRAY_SIZE(hidinput_battery_props);
332 battery->use_for_apm = 0;
333 battery->get_property = hidinput_get_battery_property;
334
335 dev->battery_min = min;
336 dev->battery_max = max;
337
338 ret = power_supply_register(&dev->dev, battery);
339 if (ret != 0) {
340 hid_warn(dev, "can't register power supply: %d\n", ret);
341 kfree(battery->name);
342 battery->name = NULL;
343 }
344}
345
346static void hidinput_cleanup_battery(struct hid_device *dev)
347{
348 if (!dev->battery.name)
349 return;
350
351 power_supply_unregister(&dev->battery);
352 kfree(dev->battery.name);
353 dev->battery.name = NULL;
354}
355#else /* !CONFIG_HID_BATTERY_STRENGTH */
356static void hidinput_setup_battery(struct hid_device *dev, s32 min, s32 max)
357{
358}
359
360static void hidinput_cleanup_battery(struct hid_device *dev)
361{
362}
363#endif /* CONFIG_HID_BATTERY_STRENGTH */
364
Jiri Kosinadde58452006-12-08 18:40:44 +0100365static void hidinput_configure_usage(struct hid_input *hidinput, struct hid_field *field,
366 struct hid_usage *usage)
367{
368 struct input_dev *input = hidinput->input;
Dmitry Torokhove0712982007-05-09 10:17:31 +0200369 struct hid_device *device = input_get_drvdata(input);
Jiri Slaby3715ade2008-07-31 11:09:37 +0200370 int max = 0, code;
Jiri Kosinadde58452006-12-08 18:40:44 +0100371 unsigned long *bit = NULL;
372
373 field->hidinput = hidinput;
374
Jiri Kosinadde58452006-12-08 18:40:44 +0100375 if (field->flags & HID_MAIN_ITEM_CONSTANT)
376 goto ignore;
377
Jiri Kosina82eb1212007-08-11 23:39:42 +0200378 /* only LED usages are supported in output fields */
379 if (field->report_type == HID_OUTPUT_REPORT &&
380 (usage->hid & HID_USAGE_PAGE) != HID_UP_LED) {
Jiri Kosina82eb1212007-08-11 23:39:42 +0200381 goto ignore;
382 }
383
Jiri Slabyc500c972008-05-16 11:49:16 +0200384 if (device->driver->input_mapping) {
385 int ret = device->driver->input_mapping(device, hidinput, field,
386 usage, &bit, &max);
387 if (ret > 0)
388 goto mapped;
389 if (ret < 0)
390 goto ignore;
391 }
392
Jiri Kosinadde58452006-12-08 18:40:44 +0100393 switch (usage->hid & HID_USAGE_PAGE) {
Jiri Slaby880d29f2008-06-18 23:55:41 +0200394 case HID_UP_UNDEFINED:
395 goto ignore;
Jiri Kosinadde58452006-12-08 18:40:44 +0100396
Jiri Slaby880d29f2008-06-18 23:55:41 +0200397 case HID_UP_KEYBOARD:
398 set_bit(EV_REP, input->evbit);
Jiri Kosinadde58452006-12-08 18:40:44 +0100399
Jiri Slaby880d29f2008-06-18 23:55:41 +0200400 if ((usage->hid & HID_USAGE) < 256) {
401 if (!hid_keyboard[usage->hid & HID_USAGE]) goto ignore;
402 map_key_clear(hid_keyboard[usage->hid & HID_USAGE]);
403 } else
404 map_key(KEY_UNKNOWN);
Jiri Kosinadde58452006-12-08 18:40:44 +0100405
Jiri Slaby880d29f2008-06-18 23:55:41 +0200406 break;
Jiri Kosinadde58452006-12-08 18:40:44 +0100407
Jiri Slaby880d29f2008-06-18 23:55:41 +0200408 case HID_UP_BUTTON:
Jiri Kosina7f978b92010-02-17 09:36:35 +0100409 code = ((usage->hid - 1) & HID_USAGE);
Jiri Kosinadde58452006-12-08 18:40:44 +0100410
Jiri Slaby880d29f2008-06-18 23:55:41 +0200411 switch (field->application) {
412 case HID_GD_MOUSE:
Florian Fainelli18745422010-12-14 11:56:56 +0100413 case HID_GD_POINTER: code += BTN_MOUSE; break;
Jiri Kosinacf2f7652010-01-04 12:20:56 +0100414 case HID_GD_JOYSTICK:
Daniel Mackfc99f222010-07-12 19:28:26 +0200415 if (code <= 0xf)
416 code += BTN_JOYSTICK;
417 else
418 code += BTN_TRIGGER_HAPPY;
419 break;
Florian Fainelli18745422010-12-14 11:56:56 +0100420 case HID_GD_GAMEPAD: code += BTN_GAMEPAD; break;
Jiri Kosinadde58452006-12-08 18:40:44 +0100421 default:
Jiri Slaby880d29f2008-06-18 23:55:41 +0200422 switch (field->physical) {
423 case HID_GD_MOUSE:
Florian Fainelli18745422010-12-14 11:56:56 +0100424 case HID_GD_POINTER: code += BTN_MOUSE; break;
425 case HID_GD_JOYSTICK: code += BTN_JOYSTICK; break;
426 case HID_GD_GAMEPAD: code += BTN_GAMEPAD; break;
427 default: code += BTN_MISC;
Jiri Kosinadde58452006-12-08 18:40:44 +0100428 }
Jiri Slaby880d29f2008-06-18 23:55:41 +0200429 }
430
431 map_key(code);
432 break;
433
434 case HID_UP_SIMULATION:
435 switch (usage->hid & 0xffff) {
436 case 0xba: map_abs(ABS_RUDDER); break;
437 case 0xbb: map_abs(ABS_THROTTLE); break;
438 case 0xc4: map_abs(ABS_GAS); break;
439 case 0xc5: map_abs(ABS_BRAKE); break;
440 case 0xc8: map_abs(ABS_WHEEL); break;
441 default: goto ignore;
442 }
443 break;
444
445 case HID_UP_GENDESK:
446 if ((usage->hid & 0xf0) == 0x80) { /* SystemControl */
447 switch (usage->hid & 0xf) {
448 case 0x1: map_key_clear(KEY_POWER); break;
449 case 0x2: map_key_clear(KEY_SLEEP); break;
450 case 0x3: map_key_clear(KEY_WAKEUP); break;
Jarod Wilson437f3b12011-04-19 15:28:30 -0400451 case 0x4: map_key_clear(KEY_CONTEXT_MENU); break;
452 case 0x5: map_key_clear(KEY_MENU); break;
453 case 0x6: map_key_clear(KEY_PROG1); break;
454 case 0x7: map_key_clear(KEY_HELP); break;
455 case 0x8: map_key_clear(KEY_EXIT); break;
456 case 0x9: map_key_clear(KEY_SELECT); break;
457 case 0xa: map_key_clear(KEY_RIGHT); break;
458 case 0xb: map_key_clear(KEY_LEFT); break;
459 case 0xc: map_key_clear(KEY_UP); break;
460 case 0xd: map_key_clear(KEY_DOWN); break;
461 case 0xe: map_key_clear(KEY_POWER2); break;
462 case 0xf: map_key_clear(KEY_RESTART); break;
Jiri Slaby880d29f2008-06-18 23:55:41 +0200463 default: goto unknown;
Jiri Kosinadde58452006-12-08 18:40:44 +0100464 }
Jiri Kosinadde58452006-12-08 18:40:44 +0100465 break;
Jiri Slaby880d29f2008-06-18 23:55:41 +0200466 }
467
468 if ((usage->hid & 0xf0) == 0x90) { /* D-pad */
469 switch (usage->hid) {
470 case HID_GD_UP: usage->hat_dir = 1; break;
471 case HID_GD_DOWN: usage->hat_dir = 5; break;
472 case HID_GD_RIGHT: usage->hat_dir = 3; break;
473 case HID_GD_LEFT: usage->hat_dir = 7; break;
474 default: goto unknown;
475 }
476 if (field->dpad) {
477 map_abs(field->dpad);
478 goto ignore;
479 }
480 map_abs(ABS_HAT0X);
481 break;
482 }
483
484 switch (usage->hid) {
485 /* These usage IDs map directly to the usage codes. */
486 case HID_GD_X: case HID_GD_Y: case HID_GD_Z:
487 case HID_GD_RX: case HID_GD_RY: case HID_GD_RZ:
488 case HID_GD_SLIDER: case HID_GD_DIAL: case HID_GD_WHEEL:
489 if (field->flags & HID_MAIN_ITEM_RELATIVE)
490 map_rel(usage->hid & 0xf);
491 else
492 map_abs(usage->hid & 0xf);
493 break;
494
495 case HID_GD_HATSWITCH:
496 usage->hat_min = field->logical_minimum;
497 usage->hat_max = field->logical_maximum;
498 map_abs(ABS_HAT0X);
499 break;
500
501 case HID_GD_START: map_key_clear(BTN_START); break;
502 case HID_GD_SELECT: map_key_clear(BTN_SELECT); break;
503
504 default: goto unknown;
505 }
506
507 break;
508
509 case HID_UP_LED:
510 switch (usage->hid & 0xffff) { /* HID-Value: */
511 case 0x01: map_led (LED_NUML); break; /* "Num Lock" */
512 case 0x02: map_led (LED_CAPSL); break; /* "Caps Lock" */
513 case 0x03: map_led (LED_SCROLLL); break; /* "Scroll Lock" */
514 case 0x04: map_led (LED_COMPOSE); break; /* "Compose" */
515 case 0x05: map_led (LED_KANA); break; /* "Kana" */
516 case 0x27: map_led (LED_SLEEP); break; /* "Stand-By" */
517 case 0x4c: map_led (LED_SUSPEND); break; /* "System Suspend" */
518 case 0x09: map_led (LED_MUTE); break; /* "Mute" */
519 case 0x4b: map_led (LED_MISC); break; /* "Generic Indicator" */
520 case 0x19: map_led (LED_MAIL); break; /* "Message Waiting" */
521 case 0x4d: map_led (LED_CHARGING); break; /* "External Power Connected" */
522
523 default: goto ignore;
524 }
525 break;
526
527 case HID_UP_DIGITIZER:
528 switch (usage->hid & 0xff) {
Forest Bond8c8b01c2010-07-13 23:50:57 +0200529 case 0x00: /* Undefined */
530 goto ignore;
531
Jiri Slaby880d29f2008-06-18 23:55:41 +0200532 case 0x30: /* TipPressure */
533 if (!test_bit(BTN_TOUCH, input->keybit)) {
534 device->quirks |= HID_QUIRK_NOTOUCH;
535 set_bit(EV_KEY, input->evbit);
536 set_bit(BTN_TOUCH, input->keybit);
537 }
538 map_abs_clear(ABS_PRESSURE);
539 break;
540
541 case 0x32: /* InRange */
542 switch (field->physical & 0xff) {
543 case 0x21: map_key(BTN_TOOL_MOUSE); break;
544 case 0x22: map_key(BTN_TOOL_FINGER); break;
545 default: map_key(BTN_TOOL_PEN); break;
546 }
547 break;
548
549 case 0x3c: /* Invert */
550 map_key_clear(BTN_TOOL_RUBBER);
551 break;
552
553 case 0x33: /* Touch */
554 case 0x42: /* TipSwitch */
555 case 0x43: /* TipSwitch2 */
556 device->quirks &= ~HID_QUIRK_NOTOUCH;
557 map_key_clear(BTN_TOUCH);
558 break;
559
560 case 0x44: /* BarrelSwitch */
561 map_key_clear(BTN_STYLUS);
562 break;
563
Nikolai Kondrashov50b636972010-08-06 23:03:07 +0400564 case 0x46: /* TabletPick */
565 map_key_clear(BTN_STYLUS2);
566 break;
567
Benjamin Tissoiresb77c3922011-09-21 16:56:54 +0200568 case 0x51: /* ContactID */
569 device->quirks |= HID_QUIRK_MULTITOUCH;
570 goto unknown;
571
Jiri Slaby880d29f2008-06-18 23:55:41 +0200572 default: goto unknown;
573 }
574 break;
575
Jarod Wilson437f3b12011-04-19 15:28:30 -0400576 case HID_UP_CONSUMER: /* USB HUT v1.12, pages 75-84 */
Jiri Slaby880d29f2008-06-18 23:55:41 +0200577 switch (usage->hid & HID_USAGE) {
578 case 0x000: goto ignore;
Jarod Wilson437f3b12011-04-19 15:28:30 -0400579 case 0x030: map_key_clear(KEY_POWER); break;
580 case 0x031: map_key_clear(KEY_RESTART); break;
581 case 0x032: map_key_clear(KEY_SLEEP); break;
Jiri Slaby880d29f2008-06-18 23:55:41 +0200582 case 0x034: map_key_clear(KEY_SLEEP); break;
Jarod Wilson437f3b12011-04-19 15:28:30 -0400583 case 0x035: map_key_clear(KEY_KBDILLUMTOGGLE); break;
Jiri Slaby880d29f2008-06-18 23:55:41 +0200584 case 0x036: map_key_clear(BTN_MISC); break;
585
Jarod Wilson437f3b12011-04-19 15:28:30 -0400586 case 0x040: map_key_clear(KEY_MENU); break; /* Menu */
587 case 0x041: map_key_clear(KEY_SELECT); break; /* Menu Pick */
588 case 0x042: map_key_clear(KEY_UP); break; /* Menu Up */
589 case 0x043: map_key_clear(KEY_DOWN); break; /* Menu Down */
590 case 0x044: map_key_clear(KEY_LEFT); break; /* Menu Left */
591 case 0x045: map_key_clear(KEY_RIGHT); break; /* Menu Right */
592 case 0x046: map_key_clear(KEY_ESC); break; /* Menu Escape */
593 case 0x047: map_key_clear(KEY_KPPLUS); break; /* Menu Value Increase */
594 case 0x048: map_key_clear(KEY_KPMINUS); break; /* Menu Value Decrease */
Jiri Slaby880d29f2008-06-18 23:55:41 +0200595
Jarod Wilson437f3b12011-04-19 15:28:30 -0400596 case 0x060: map_key_clear(KEY_INFO); break; /* Data On Screen */
597 case 0x061: map_key_clear(KEY_SUBTITLE); break; /* Closed Caption */
598 case 0x063: map_key_clear(KEY_VCR); break; /* VCR/TV */
599 case 0x065: map_key_clear(KEY_CAMERA); break; /* Snapshot */
600 case 0x069: map_key_clear(KEY_RED); break;
601 case 0x06a: map_key_clear(KEY_GREEN); break;
602 case 0x06b: map_key_clear(KEY_BLUE); break;
603 case 0x06c: map_key_clear(KEY_YELLOW); break;
604 case 0x06d: map_key_clear(KEY_ZOOM); break;
605
606 case 0x082: map_key_clear(KEY_VIDEO_NEXT); break;
Jiri Slaby880d29f2008-06-18 23:55:41 +0200607 case 0x083: map_key_clear(KEY_LAST); break;
Jarod Wilson437f3b12011-04-19 15:28:30 -0400608 case 0x084: map_key_clear(KEY_ENTER); break;
Jiri Slaby880d29f2008-06-18 23:55:41 +0200609 case 0x088: map_key_clear(KEY_PC); break;
610 case 0x089: map_key_clear(KEY_TV); break;
611 case 0x08a: map_key_clear(KEY_WWW); break;
612 case 0x08b: map_key_clear(KEY_DVD); break;
613 case 0x08c: map_key_clear(KEY_PHONE); break;
614 case 0x08d: map_key_clear(KEY_PROGRAM); break;
615 case 0x08e: map_key_clear(KEY_VIDEOPHONE); break;
616 case 0x08f: map_key_clear(KEY_GAMES); break;
617 case 0x090: map_key_clear(KEY_MEMO); break;
618 case 0x091: map_key_clear(KEY_CD); break;
619 case 0x092: map_key_clear(KEY_VCR); break;
620 case 0x093: map_key_clear(KEY_TUNER); break;
621 case 0x094: map_key_clear(KEY_EXIT); break;
622 case 0x095: map_key_clear(KEY_HELP); break;
623 case 0x096: map_key_clear(KEY_TAPE); break;
624 case 0x097: map_key_clear(KEY_TV2); break;
625 case 0x098: map_key_clear(KEY_SAT); break;
626 case 0x09a: map_key_clear(KEY_PVR); break;
627
628 case 0x09c: map_key_clear(KEY_CHANNELUP); break;
629 case 0x09d: map_key_clear(KEY_CHANNELDOWN); break;
630 case 0x0a0: map_key_clear(KEY_VCR2); break;
631
632 case 0x0b0: map_key_clear(KEY_PLAY); break;
633 case 0x0b1: map_key_clear(KEY_PAUSE); break;
634 case 0x0b2: map_key_clear(KEY_RECORD); break;
635 case 0x0b3: map_key_clear(KEY_FASTFORWARD); break;
636 case 0x0b4: map_key_clear(KEY_REWIND); break;
637 case 0x0b5: map_key_clear(KEY_NEXTSONG); break;
638 case 0x0b6: map_key_clear(KEY_PREVIOUSSONG); break;
639 case 0x0b7: map_key_clear(KEY_STOPCD); break;
640 case 0x0b8: map_key_clear(KEY_EJECTCD); break;
641 case 0x0bc: map_key_clear(KEY_MEDIA_REPEAT); break;
Jarod Wilson437f3b12011-04-19 15:28:30 -0400642 case 0x0b9: map_key_clear(KEY_SHUFFLE); break;
643 case 0x0bf: map_key_clear(KEY_SLOW); break;
Jiri Slaby880d29f2008-06-18 23:55:41 +0200644
645 case 0x0cd: map_key_clear(KEY_PLAYPAUSE); break;
646 case 0x0e0: map_abs_clear(ABS_VOLUME); break;
647 case 0x0e2: map_key_clear(KEY_MUTE); break;
648 case 0x0e5: map_key_clear(KEY_BASSBOOST); break;
649 case 0x0e9: map_key_clear(KEY_VOLUMEUP); break;
650 case 0x0ea: map_key_clear(KEY_VOLUMEDOWN); break;
Jarod Wilson437f3b12011-04-19 15:28:30 -0400651 case 0x0f5: map_key_clear(KEY_SLOW); break;
Jiri Slaby880d29f2008-06-18 23:55:41 +0200652
653 case 0x182: map_key_clear(KEY_BOOKMARKS); break;
654 case 0x183: map_key_clear(KEY_CONFIG); break;
655 case 0x184: map_key_clear(KEY_WORDPROCESSOR); break;
656 case 0x185: map_key_clear(KEY_EDITOR); break;
657 case 0x186: map_key_clear(KEY_SPREADSHEET); break;
658 case 0x187: map_key_clear(KEY_GRAPHICSEDITOR); break;
659 case 0x188: map_key_clear(KEY_PRESENTATION); break;
660 case 0x189: map_key_clear(KEY_DATABASE); break;
661 case 0x18a: map_key_clear(KEY_MAIL); break;
662 case 0x18b: map_key_clear(KEY_NEWS); break;
663 case 0x18c: map_key_clear(KEY_VOICEMAIL); break;
664 case 0x18d: map_key_clear(KEY_ADDRESSBOOK); break;
665 case 0x18e: map_key_clear(KEY_CALENDAR); break;
666 case 0x191: map_key_clear(KEY_FINANCE); break;
667 case 0x192: map_key_clear(KEY_CALC); break;
Jarod Wilson437f3b12011-04-19 15:28:30 -0400668 case 0x193: map_key_clear(KEY_PLAYER); break;
Jiri Slaby880d29f2008-06-18 23:55:41 +0200669 case 0x194: map_key_clear(KEY_FILE); break;
670 case 0x196: map_key_clear(KEY_WWW); break;
Leo P White06905352010-02-08 13:02:05 +0000671 case 0x199: map_key_clear(KEY_CHAT); break;
Jiri Slaby880d29f2008-06-18 23:55:41 +0200672 case 0x19c: map_key_clear(KEY_LOGOFF); break;
673 case 0x19e: map_key_clear(KEY_COFFEE); break;
674 case 0x1a6: map_key_clear(KEY_HELP); break;
675 case 0x1a7: map_key_clear(KEY_DOCUMENTS); break;
676 case 0x1ab: map_key_clear(KEY_SPELLCHECK); break;
Jarod Wilson437f3b12011-04-19 15:28:30 -0400677 case 0x1ae: map_key_clear(KEY_KEYBOARD); break;
678 case 0x1b6: map_key_clear(KEY_IMAGES); break;
679 case 0x1b7: map_key_clear(KEY_AUDIO); break;
680 case 0x1b8: map_key_clear(KEY_VIDEO); break;
Jiri Slaby880d29f2008-06-18 23:55:41 +0200681 case 0x1bc: map_key_clear(KEY_MESSENGER); break;
682 case 0x1bd: map_key_clear(KEY_INFO); break;
683 case 0x201: map_key_clear(KEY_NEW); break;
684 case 0x202: map_key_clear(KEY_OPEN); break;
685 case 0x203: map_key_clear(KEY_CLOSE); break;
686 case 0x204: map_key_clear(KEY_EXIT); break;
687 case 0x207: map_key_clear(KEY_SAVE); break;
688 case 0x208: map_key_clear(KEY_PRINT); break;
689 case 0x209: map_key_clear(KEY_PROPS); break;
690 case 0x21a: map_key_clear(KEY_UNDO); break;
691 case 0x21b: map_key_clear(KEY_COPY); break;
692 case 0x21c: map_key_clear(KEY_CUT); break;
693 case 0x21d: map_key_clear(KEY_PASTE); break;
694 case 0x21f: map_key_clear(KEY_FIND); break;
695 case 0x221: map_key_clear(KEY_SEARCH); break;
696 case 0x222: map_key_clear(KEY_GOTO); break;
697 case 0x223: map_key_clear(KEY_HOMEPAGE); break;
698 case 0x224: map_key_clear(KEY_BACK); break;
699 case 0x225: map_key_clear(KEY_FORWARD); break;
700 case 0x226: map_key_clear(KEY_STOP); break;
701 case 0x227: map_key_clear(KEY_REFRESH); break;
702 case 0x22a: map_key_clear(KEY_BOOKMARKS); break;
703 case 0x22d: map_key_clear(KEY_ZOOMIN); break;
704 case 0x22e: map_key_clear(KEY_ZOOMOUT); break;
705 case 0x22f: map_key_clear(KEY_ZOOMRESET); break;
706 case 0x233: map_key_clear(KEY_SCROLLUP); break;
707 case 0x234: map_key_clear(KEY_SCROLLDOWN); break;
708 case 0x238: map_rel(REL_HWHEEL); break;
Jarod Wilson437f3b12011-04-19 15:28:30 -0400709 case 0x23d: map_key_clear(KEY_EDIT); break;
Jiri Slaby880d29f2008-06-18 23:55:41 +0200710 case 0x25f: map_key_clear(KEY_CANCEL); break;
Jarod Wilson437f3b12011-04-19 15:28:30 -0400711 case 0x269: map_key_clear(KEY_INSERT); break;
712 case 0x26a: map_key_clear(KEY_DELETE); break;
Jiri Slaby880d29f2008-06-18 23:55:41 +0200713 case 0x279: map_key_clear(KEY_REDO); break;
714
715 case 0x289: map_key_clear(KEY_REPLY); break;
716 case 0x28b: map_key_clear(KEY_FORWARDMAIL); break;
717 case 0x28c: map_key_clear(KEY_SEND); break;
718
719 default: goto ignore;
720 }
721 break;
722
Jeremy Fitzhardinge4f5ca832011-11-23 00:49:14 -0800723 case HID_UP_GENDEVCTRLS:
724 if ((usage->hid & HID_USAGE) == 0x20) { /* Battery Strength */
725 hidinput_setup_battery(device,
726 field->logical_minimum,
727 field->logical_maximum);
728 goto ignore;
729 } else
730 goto unknown;
731 break;
732
Jiri Slaby880d29f2008-06-18 23:55:41 +0200733 case HID_UP_HPVENDOR: /* Reported on a Dutch layout HP5308 */
734 set_bit(EV_REP, input->evbit);
735 switch (usage->hid & HID_USAGE) {
736 case 0x021: map_key_clear(KEY_PRINT); break;
737 case 0x070: map_key_clear(KEY_HP); break;
738 case 0x071: map_key_clear(KEY_CAMERA); break;
739 case 0x072: map_key_clear(KEY_SOUND); break;
740 case 0x073: map_key_clear(KEY_QUESTION); break;
741 case 0x080: map_key_clear(KEY_EMAIL); break;
742 case 0x081: map_key_clear(KEY_CHAT); break;
743 case 0x082: map_key_clear(KEY_SEARCH); break;
744 case 0x083: map_key_clear(KEY_CONNECT); break;
745 case 0x084: map_key_clear(KEY_FINANCE); break;
746 case 0x085: map_key_clear(KEY_SPORT); break;
747 case 0x086: map_key_clear(KEY_SHOP); break;
748 default: goto ignore;
749 }
750 break;
751
752 case HID_UP_MSVENDOR:
753 goto ignore;
754
755 case HID_UP_CUSTOM: /* Reported on Logitech and Apple USB keyboards */
756 set_bit(EV_REP, input->evbit);
757 goto ignore;
758
759 case HID_UP_LOGIVENDOR:
760 goto ignore;
Daniel Mackfc99f222010-07-12 19:28:26 +0200761
Jiri Slaby880d29f2008-06-18 23:55:41 +0200762 case HID_UP_PID:
763 switch (usage->hid & HID_USAGE) {
764 case 0xa4: map_key_clear(BTN_DEAD); break;
765 default: goto ignore;
766 }
767 break;
768
769 default:
770 unknown:
771 if (field->report_size == 1) {
772 if (field->report->type == HID_OUTPUT_REPORT) {
773 map_led(LED_MISC);
774 break;
775 }
776 map_key(BTN_MISC);
777 break;
778 }
779 if (field->flags & HID_MAIN_ITEM_RELATIVE) {
780 map_rel(REL_MISC);
781 break;
782 }
783 map_abs(ABS_MISC);
784 break;
Jiri Kosinadde58452006-12-08 18:40:44 +0100785 }
786
Jiri Kosina10bd0652007-11-22 15:18:18 +0100787mapped:
Jiri Slabyc500c972008-05-16 11:49:16 +0200788 if (device->driver->input_mapped && device->driver->input_mapped(device,
789 hidinput, field, usage, &bit, &max) < 0)
790 goto ignore;
791
Jiri Kosinadde58452006-12-08 18:40:44 +0100792 set_bit(usage->type, input->evbit);
793
794 while (usage->code <= max && test_and_set_bit(usage->code, bit))
795 usage->code = find_next_zero_bit(bit, max + 1, usage->code);
796
797 if (usage->code > max)
798 goto ignore;
799
800
801 if (usage->type == EV_ABS) {
802
803 int a = field->logical_minimum;
804 int b = field->logical_maximum;
805
806 if ((device->quirks & HID_QUIRK_BADPAD) && (usage->code == ABS_X || usage->code == ABS_Y)) {
807 a = field->logical_minimum = 0;
808 b = field->logical_maximum = 255;
809 }
810
811 if (field->application == HID_GD_GAMEPAD || field->application == HID_GD_JOYSTICK)
812 input_set_abs_params(input, usage->code, a, b, (b - a) >> 8, (b - a) >> 4);
813 else input_set_abs_params(input, usage->code, a, b, 0, 0);
814
Nikolai Kondrashov4ea6e4f2010-09-15 14:51:14 +0400815 input_abs_set_res(input, usage->code,
816 hidinput_calc_abs_res(field, usage->code));
817
Henrik Rydberg6967b4d2010-06-23 09:31:37 -0700818 /* use a larger default input buffer for MT devices */
819 if (usage->code == ABS_MT_POSITION_X && input->hint_events_per_packet == 0)
820 input_set_events_per_packet(input, 60);
Jiri Kosinadde58452006-12-08 18:40:44 +0100821 }
822
823 if (usage->type == EV_ABS &&
824 (usage->hat_min < usage->hat_max || usage->hat_dir)) {
825 int i;
826 for (i = usage->code; i < usage->code + 2 && i <= max; i++) {
827 input_set_abs_params(input, i, -1, 1, 0, 0);
828 set_bit(i, input->absbit);
829 }
830 if (usage->hat_dir && !field->dpad)
831 field->dpad = usage->code;
832 }
833
Jiri Kosina2c1d8ae2007-07-04 16:45:59 +0200834 /* for those devices which produce Consumer volume usage as relative,
835 * we emulate pressing volumeup/volumedown appropriate number of times
836 * in hidinput_hid_event()
837 */
838 if ((usage->type == EV_ABS) && (field->flags & HID_MAIN_ITEM_RELATIVE) &&
839 (usage->code == ABS_VOLUME)) {
840 set_bit(KEY_VOLUMEUP, input->keybit);
841 set_bit(KEY_VOLUMEDOWN, input->keybit);
842 }
843
Jiri Kosinac01d50d2007-08-20 12:13:34 +0200844 if (usage->type == EV_KEY) {
845 set_bit(EV_MSC, input->evbit);
846 set_bit(MSC_SCAN, input->mscbit);
847 }
848
Jiri Kosinadde58452006-12-08 18:40:44 +0100849ignore:
Jiri Kosinadde58452006-12-08 18:40:44 +0100850 return;
Jiri Kosinaa635f9d2009-06-12 15:20:55 +0200851
Jiri Kosinadde58452006-12-08 18:40:44 +0100852}
853
854void hidinput_hid_event(struct hid_device *hid, struct hid_field *field, struct hid_usage *usage, __s32 value)
855{
856 struct input_dev *input;
Al Virob4482a42007-10-14 19:35:40 +0100857 unsigned *quirks = &hid->quirks;
Jiri Kosinadde58452006-12-08 18:40:44 +0100858
859 if (!field->hidinput)
860 return;
861
862 input = field->hidinput->input;
863
Jeremy Fitzhardinge67200792011-11-30 14:42:04 +0100864#ifdef CONFIG_HID_BATTERY_STRENGTH
Jeremy Fitzhardinge4f5ca832011-11-23 00:49:14 -0800865 if (usage->hid == HID_DC_BATTERYSTRENGTH) {
866 hid->battery_val = value;
867 hid_dbg(hid, "battery value is %d (range %d-%d)\n",
868 value, hid->battery_min, hid->battery_max);
869 return;
870 }
Jeremy Fitzhardinge67200792011-11-30 14:42:04 +0100871#endif
Jiri Kosinadde58452006-12-08 18:40:44 +0100872 if (!usage->type)
873 return;
874
Jiri Kosinadde58452006-12-08 18:40:44 +0100875 if (usage->hat_min < usage->hat_max || usage->hat_dir) {
876 int hat_dir = usage->hat_dir;
877 if (!hat_dir)
878 hat_dir = (value - usage->hat_min) * 8 / (usage->hat_max - usage->hat_min + 1) + 1;
879 if (hat_dir < 0 || hat_dir > 8) hat_dir = 0;
880 input_event(input, usage->type, usage->code , hid_hat_to_axis[hat_dir].x);
Daniel Mackfc99f222010-07-12 19:28:26 +0200881 input_event(input, usage->type, usage->code + 1, hid_hat_to_axis[hat_dir].y);
882 return;
883 }
Jiri Kosinadde58452006-12-08 18:40:44 +0100884
885 if (usage->hid == (HID_UP_DIGITIZER | 0x003c)) { /* Invert */
886 *quirks = value ? (*quirks | HID_QUIRK_INVERT) : (*quirks & ~HID_QUIRK_INVERT);
887 return;
888 }
889
890 if (usage->hid == (HID_UP_DIGITIZER | 0x0032)) { /* InRange */
891 if (value) {
892 input_event(input, usage->type, (*quirks & HID_QUIRK_INVERT) ? BTN_TOOL_RUBBER : usage->code, 1);
893 return;
894 }
895 input_event(input, usage->type, usage->code, 0);
896 input_event(input, usage->type, BTN_TOOL_RUBBER, 0);
897 return;
898 }
899
900 if (usage->hid == (HID_UP_DIGITIZER | 0x0030) && (*quirks & HID_QUIRK_NOTOUCH)) { /* Pressure */
901 int a = field->logical_minimum;
902 int b = field->logical_maximum;
903 input_event(input, EV_KEY, BTN_TOUCH, value > a + ((b - a) >> 3));
904 }
905
906 if (usage->hid == (HID_UP_PID | 0x83UL)) { /* Simultaneous Effects Max */
Jiri Kosina58037eb2007-05-30 15:07:13 +0200907 dbg_hid("Maximum Effects - %d\n",value);
Jiri Kosinadde58452006-12-08 18:40:44 +0100908 return;
909 }
910
911 if (usage->hid == (HID_UP_PID | 0x7fUL)) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200912 dbg_hid("PID Pool Report\n");
Jiri Kosinadde58452006-12-08 18:40:44 +0100913 return;
914 }
915
916 if ((usage->type == EV_KEY) && (usage->code == 0)) /* Key 0 is "unassigned", not KEY_UNKNOWN */
917 return;
918
Jiri Kosina2c1d8ae2007-07-04 16:45:59 +0200919 if ((usage->type == EV_ABS) && (field->flags & HID_MAIN_ITEM_RELATIVE) &&
920 (usage->code == ABS_VOLUME)) {
921 int count = abs(value);
922 int direction = value > 0 ? KEY_VOLUMEUP : KEY_VOLUMEDOWN;
923 int i;
924
925 for (i = 0; i < count; i++) {
926 input_event(input, EV_KEY, direction, 1);
927 input_sync(input);
928 input_event(input, EV_KEY, direction, 0);
929 input_sync(input);
930 }
931 return;
932 }
933
Jiri Kosinab4b583d2011-10-31 16:26:22 +0100934 /* Ignore out-of-range values as per HID specification, section 5.10 */
935 if (value < field->logical_minimum || value > field->logical_maximum) {
Jiri Kosina6da70662011-10-28 18:15:02 +0200936 dbg_hid("Ignoring out-of-range value %x\n", value);
937 return;
938 }
939
Jiri Kosinac01d50d2007-08-20 12:13:34 +0200940 /* report the usage code as scancode if the key status has changed */
941 if (usage->type == EV_KEY && !!test_bit(usage->code, input->key) != value)
942 input_event(input, EV_MSC, MSC_SCAN, usage->hid);
Jiri Kosina1fe8736d2007-08-09 13:24:11 +0200943
Jiri Kosinadde58452006-12-08 18:40:44 +0100944 input_event(input, usage->type, usage->code, value);
945
946 if ((field->flags & HID_MAIN_ITEM_RELATIVE) && (usage->type == EV_KEY))
947 input_event(input, usage->type, usage->code, 0);
948}
949
950void hidinput_report_event(struct hid_device *hid, struct hid_report *report)
951{
952 struct hid_input *hidinput;
953
Henrik Rydberg24750f32010-08-24 10:54:44 +0200954 if (hid->quirks & HID_QUIRK_NO_INPUT_SYNC)
955 return;
956
Jiri Kosinadde58452006-12-08 18:40:44 +0100957 list_for_each_entry(hidinput, &hid->inputs, list)
958 input_sync(hidinput->input);
959}
Jiri Kosina229695e2006-12-08 18:40:53 +0100960EXPORT_SYMBOL_GPL(hidinput_report_event);
Jiri Kosinadde58452006-12-08 18:40:44 +0100961
Jiri Kosina229695e2006-12-08 18:40:53 +0100962int hidinput_find_field(struct hid_device *hid, unsigned int type, unsigned int code, struct hid_field **field)
Jiri Kosinadde58452006-12-08 18:40:44 +0100963{
964 struct hid_report *report;
965 int i, j;
966
967 list_for_each_entry(report, &hid->report_enum[HID_OUTPUT_REPORT].report_list, list) {
968 for (i = 0; i < report->maxfield; i++) {
969 *field = report->field[i];
970 for (j = 0; j < (*field)->maxusage; j++)
971 if ((*field)->usage[j].type == type && (*field)->usage[j].code == code)
972 return j;
973 }
974 }
975 return -1;
976}
Jiri Kosina229695e2006-12-08 18:40:53 +0100977EXPORT_SYMBOL_GPL(hidinput_find_field);
Jiri Kosinadde58452006-12-08 18:40:44 +0100978
Daniel Kurtz4371ea82011-11-17 19:23:50 +0800979struct hid_field *hidinput_get_led_field(struct hid_device *hid)
980{
981 struct hid_report *report;
982 struct hid_field *field;
983 int i, j;
984
985 list_for_each_entry(report,
986 &hid->report_enum[HID_OUTPUT_REPORT].report_list,
987 list) {
988 for (i = 0; i < report->maxfield; i++) {
989 field = report->field[i];
990 for (j = 0; j < field->maxusage; j++)
991 if (field->usage[j].type == EV_LED)
992 return field;
993 }
994 }
995 return NULL;
996}
997EXPORT_SYMBOL_GPL(hidinput_get_led_field);
998
999unsigned int hidinput_count_leds(struct hid_device *hid)
1000{
1001 struct hid_report *report;
1002 struct hid_field *field;
1003 int i, j;
1004 unsigned int count = 0;
1005
1006 list_for_each_entry(report,
1007 &hid->report_enum[HID_OUTPUT_REPORT].report_list,
1008 list) {
1009 for (i = 0; i < report->maxfield; i++) {
1010 field = report->field[i];
1011 for (j = 0; j < field->maxusage; j++)
1012 if (field->usage[j].type == EV_LED &&
1013 field->value[j])
1014 count += 1;
1015 }
1016 }
1017 return count;
1018}
1019EXPORT_SYMBOL_GPL(hidinput_count_leds);
1020
Jiri Kosina7c379142007-01-24 11:54:19 +01001021static int hidinput_open(struct input_dev *dev)
1022{
Dmitry Torokhove0712982007-05-09 10:17:31 +02001023 struct hid_device *hid = input_get_drvdata(dev);
1024
Dmitry Torokhov5bea7662010-12-07 23:02:48 -08001025 return hid_hw_open(hid);
Jiri Kosina7c379142007-01-24 11:54:19 +01001026}
1027
1028static void hidinput_close(struct input_dev *dev)
1029{
Dmitry Torokhove0712982007-05-09 10:17:31 +02001030 struct hid_device *hid = input_get_drvdata(dev);
1031
Dmitry Torokhov5bea7662010-12-07 23:02:48 -08001032 hid_hw_close(hid);
Jiri Kosina7c379142007-01-24 11:54:19 +01001033}
1034
Henrik Rydbergf635bd12011-02-24 19:30:59 +01001035static void report_features(struct hid_device *hid)
1036{
1037 struct hid_driver *drv = hid->driver;
1038 struct hid_report_enum *rep_enum;
1039 struct hid_report *rep;
1040 int i, j;
1041
1042 if (!drv->feature_mapping)
1043 return;
1044
1045 rep_enum = &hid->report_enum[HID_FEATURE_REPORT];
1046 list_for_each_entry(rep, &rep_enum->report_list, list)
1047 for (i = 0; i < rep->maxfield; i++)
1048 for (j = 0; j < rep->field[i]->maxusage; j++)
1049 drv->feature_mapping(hid, rep->field[i],
1050 rep->field[i]->usage + j);
1051}
1052
Jiri Kosinadde58452006-12-08 18:40:44 +01001053/*
1054 * Register the input device; print a message.
1055 * Configure the input layer interface
1056 * Read all reports and initialize the absolute field values.
1057 */
1058
Jiri Slaby93c10132008-06-27 00:04:24 +02001059int hidinput_connect(struct hid_device *hid, unsigned int force)
Jiri Kosinadde58452006-12-08 18:40:44 +01001060{
Jiri Kosinadde58452006-12-08 18:40:44 +01001061 struct hid_report *report;
1062 struct hid_input *hidinput = NULL;
1063 struct input_dev *input_dev;
1064 int i, j, k;
1065
1066 INIT_LIST_HEAD(&hid->inputs);
1067
Jiri Slaby93c10132008-06-27 00:04:24 +02001068 if (!force) {
1069 for (i = 0; i < hid->maxcollection; i++) {
1070 struct hid_collection *col = &hid->collection[i];
1071 if (col->type == HID_COLLECTION_APPLICATION ||
1072 col->type == HID_COLLECTION_PHYSICAL)
1073 if (IS_INPUT_APPLICATION(col->usage))
1074 break;
1075 }
Jiri Kosinadde58452006-12-08 18:40:44 +01001076
Jiri Slaby93c10132008-06-27 00:04:24 +02001077 if (i == hid->maxcollection)
1078 return -1;
1079 }
Jiri Kosinadde58452006-12-08 18:40:44 +01001080
Henrik Rydbergf635bd12011-02-24 19:30:59 +01001081 report_features(hid);
1082
1083 for (k = HID_INPUT_REPORT; k <= HID_OUTPUT_REPORT; k++) {
Benjamin Tissoires0d2689c2011-01-07 23:44:32 +01001084 if (k == HID_OUTPUT_REPORT &&
1085 hid->quirks & HID_QUIRK_SKIP_OUTPUT_REPORTS)
1086 continue;
Anssi Hannula5556fea2007-01-11 16:51:17 +02001087
Jiri Kosinadde58452006-12-08 18:40:44 +01001088 list_for_each_entry(report, &hid->report_enum[k].report_list, list) {
1089
1090 if (!report->maxfield)
1091 continue;
1092
1093 if (!hidinput) {
1094 hidinput = kzalloc(sizeof(*hidinput), GFP_KERNEL);
1095 input_dev = input_allocate_device();
1096 if (!hidinput || !input_dev) {
1097 kfree(hidinput);
1098 input_free_device(input_dev);
Joe Perches4291ee32010-12-09 19:29:03 -08001099 hid_err(hid, "Out of memory during hid input probe\n");
Dirk Hohndel368d2902007-10-30 13:02:44 +01001100 goto out_unwind;
Jiri Kosinadde58452006-12-08 18:40:44 +01001101 }
1102
Dmitry Torokhove0712982007-05-09 10:17:31 +02001103 input_set_drvdata(input_dev, hid);
Jiri Slabyc500c972008-05-16 11:49:16 +02001104 input_dev->event =
1105 hid->ll_driver->hidinput_input_event;
Jiri Kosina7c379142007-01-24 11:54:19 +01001106 input_dev->open = hidinput_open;
1107 input_dev->close = hidinput_close;
Dmitry Torokhovaebd6362011-01-31 21:06:39 -08001108 input_dev->setkeycode = hidinput_setkeycode;
1109 input_dev->getkeycode = hidinput_getkeycode;
Jiri Kosinadde58452006-12-08 18:40:44 +01001110
1111 input_dev->name = hid->name;
1112 input_dev->phys = hid->phys;
1113 input_dev->uniq = hid->uniq;
Jiri Kosina229695e2006-12-08 18:40:53 +01001114 input_dev->id.bustype = hid->bus;
1115 input_dev->id.vendor = hid->vendor;
1116 input_dev->id.product = hid->product;
1117 input_dev->id.version = hid->version;
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001118 input_dev->dev.parent = hid->dev.parent;
Jiri Kosinadde58452006-12-08 18:40:44 +01001119 hidinput->input = input_dev;
1120 list_add_tail(&hidinput->list, &hid->inputs);
1121 }
1122
1123 for (i = 0; i < report->maxfield; i++)
1124 for (j = 0; j < report->field[i]->maxusage; j++)
1125 hidinput_configure_usage(hidinput, report->field[i],
1126 report->field[i]->usage + j);
1127
1128 if (hid->quirks & HID_QUIRK_MULTI_INPUT) {
1129 /* This will leave hidinput NULL, so that it
1130 * allocates another one if we have more inputs on
1131 * the same interface. Some devices (e.g. Happ's
1132 * UGCI) cram a lot of unrelated inputs into the
1133 * same interface. */
1134 hidinput->report = report;
Dirk Hohndel368d2902007-10-30 13:02:44 +01001135 if (input_register_device(hidinput->input))
1136 goto out_cleanup;
Jiri Kosinadde58452006-12-08 18:40:44 +01001137 hidinput = NULL;
1138 }
1139 }
Benjamin Tissoires0d2689c2011-01-07 23:44:32 +01001140 }
Jiri Kosinadde58452006-12-08 18:40:44 +01001141
Benjamin Tissoiresb77c3922011-09-21 16:56:54 +02001142 if (hid->quirks & HID_QUIRK_MULTITOUCH) {
1143 /* generic hid does not know how to handle multitouch devices */
1144 if (hidinput)
1145 goto out_cleanup;
1146 goto out_unwind;
1147 }
1148
Dirk Hohndel368d2902007-10-30 13:02:44 +01001149 if (hidinput && input_register_device(hidinput->input))
1150 goto out_cleanup;
Jiri Kosinadde58452006-12-08 18:40:44 +01001151
1152 return 0;
Dirk Hohndel368d2902007-10-30 13:02:44 +01001153
1154out_cleanup:
Benjamin Tissoiresd9236302011-02-15 15:41:10 +01001155 list_del(&hidinput->list);
Dirk Hohndel368d2902007-10-30 13:02:44 +01001156 input_free_device(hidinput->input);
1157 kfree(hidinput);
1158out_unwind:
1159 /* unwind the ones we already registered */
1160 hidinput_disconnect(hid);
1161
1162 return -1;
Jiri Kosinadde58452006-12-08 18:40:44 +01001163}
Jiri Kosina229695e2006-12-08 18:40:53 +01001164EXPORT_SYMBOL_GPL(hidinput_connect);
Jiri Kosinadde58452006-12-08 18:40:44 +01001165
1166void hidinput_disconnect(struct hid_device *hid)
1167{
1168 struct hid_input *hidinput, *next;
1169
Jeremy Fitzhardinge4f5ca832011-11-23 00:49:14 -08001170 hidinput_cleanup_battery(hid);
1171
Jiri Kosinadde58452006-12-08 18:40:44 +01001172 list_for_each_entry_safe(hidinput, next, &hid->inputs, list) {
1173 list_del(&hidinput->list);
1174 input_unregister_device(hidinput->input);
1175 kfree(hidinput);
1176 }
1177}
Jiri Kosina229695e2006-12-08 18:40:53 +01001178EXPORT_SYMBOL_GPL(hidinput_disconnect);
1179