28 #include <linux/module.h>
29 #include <linux/slab.h>
30 #include <linux/kernel.h>
32 #include <linux/hid.h>
37 #define unk KEY_UNKNOWN
39 static const unsigned char hid_keyboard[256] = {
40 0, 0, 0, 0, 30, 48, 46, 32, 18, 33, 34, 35, 23, 36, 37, 38,
41 50, 49, 24, 25, 16, 19, 31, 20, 22, 47, 17, 45, 21, 44, 2, 3,
42 4, 5, 6, 7, 8, 9, 10, 11, 28, 1, 14, 15, 57, 12, 13, 26,
43 27, 43, 43, 39, 40, 41, 51, 52, 53, 58, 59, 60, 61, 62, 63, 64,
44 65, 66, 67, 68, 87, 88, 99, 70,119,110,102,104,111,107,109,106,
45 105,108,103, 69, 98, 55, 74, 78, 96, 79, 80, 81, 75, 76, 77, 71,
46 72, 73, 82, 83, 86,127,116,117,183,184,185,186,187,188,189,190,
47 191,192,193,194,134,138,130,132,128,129,131,137,133,135,136,113,
48 115,114,
unk,
unk,
unk,121,
unk, 89, 93,124, 92, 94, 95,
unk,
unk,
unk,
49 122,123, 90, 91, 85,
unk,
unk,
unk,
unk,
unk,
unk,
unk,111,
unk,
unk,
unk,
50 unk,
unk,
unk,
unk,
unk,
unk,
unk,
unk,
unk,
unk,
unk,
unk,
unk,
unk,
unk,
unk,
51 unk,
unk,
unk,
unk,
unk,
unk,179,180,
unk,
unk,
unk,
unk,
unk,
unk,
unk,
unk,
52 unk,
unk,
unk,
unk,
unk,
unk,
unk,
unk,
unk,
unk,
unk,
unk,
unk,
unk,
unk,
unk,
53 unk,
unk,
unk,
unk,
unk,
unk,
unk,
unk,111,
unk,
unk,
unk,
unk,
unk,
unk,
unk,
54 29, 42, 56,125, 97, 54,100,126,164,166,165,163,161,115,114,113,
55 150,158,159,128,136,177,178,176,142,152,173,140,
unk,
unk,
unk,unk
61 } hid_hat_to_axis[] = {{ 0, 0}, { 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}};
63 #define map_abs(c) hid_map_usage(hidinput, usage, &bit, &max, EV_ABS, (c))
64 #define map_rel(c) hid_map_usage(hidinput, usage, &bit, &max, EV_REL, (c))
65 #define map_key(c) hid_map_usage(hidinput, usage, &bit, &max, EV_KEY, (c))
66 #define map_led(c) hid_map_usage(hidinput, usage, &bit, &max, EV_LED, (c))
68 #define map_abs_clear(c) hid_map_usage_clear(hidinput, usage, &bit, \
70 #define map_key_clear(c) hid_map_usage_clear(hidinput, usage, &bit, \
74 unsigned int cur_idx,
unsigned int scancode)
88 static bool match_index(
struct hid_usage *usage,
89 unsigned int cur_idx,
unsigned int idx)
91 return cur_idx ==
idx;
100 unsigned int *usage_idx)
102 unsigned int i,
j,
k, cur_idx = 0;
108 for (i = 0; i < report->
maxfield; i++) {
109 for (j = 0; j < report->
field[
i]->maxusage; j++) {
110 usage = report->
field[
i]->usage +
j;
112 if (
match(usage, cur_idx, value)) {
131 unsigned int scancode;
134 usage = hidinput_find_key(hid, match_index, ke->
index, index);
135 else if (input_scancode_to_scalar(ke, &scancode) == 0)
136 usage = hidinput_find_key(hid, match_scancode, scancode, index);
143 static int hidinput_getkeycode(
struct input_dev *
dev,
146 struct hid_device *hid = input_get_drvdata(dev);
148 unsigned int scancode,
index;
150 usage = hidinput_locate_usage(hid, ke, &index);
156 ke->
len =
sizeof(scancode);
164 static int hidinput_setkeycode(
struct input_dev *dev,
166 unsigned int *old_keycode)
168 struct hid_device *hid = input_get_drvdata(dev);
171 usage = hidinput_locate_usage(hid, ke,
NULL);
179 dbg_hid(
"Assigned keycode %d to HID usage code %x\n",
186 if (hidinput_find_key(hid, match_keycode, *old_keycode,
NULL))
187 set_bit(*old_keycode, dev->keybit);
221 if (logical_extents <= 0 || physical_extents <= 0)
232 if (field->
unit == 0x11) {
235 }
else if (field->
unit == 0x13) {
237 prev = physical_extents;
238 physical_extents *= 254;
239 if (physical_extents < prev)
252 if (field->
unit == 0x14) {
254 prev = logical_extents;
255 logical_extents *= 573;
256 if (logical_extents < prev)
259 }
else if (field->
unit != 0x12) {
269 for (; unit_exponent < 0; unit_exponent++) {
270 prev = logical_extents;
271 logical_extents *= 10;
272 if (logical_extents < prev)
276 for (; unit_exponent > 0; unit_exponent--) {
277 prev = physical_extents;
278 physical_extents *= 10;
279 if (physical_extents < prev)
284 return logical_extents / physical_extents;
287 #ifdef CONFIG_HID_BATTERY_STRENGTH
297 #define HID_BATTERY_QUIRK_PERCENT (1 << 0)
298 #define HID_BATTERY_QUIRK_FEATURE (1 << 1)
303 HID_BATTERY_QUIRK_PERCENT | HID_BATTERY_QUIRK_FEATURE },
306 HID_BATTERY_QUIRK_PERCENT | HID_BATTERY_QUIRK_FEATURE },
310 static unsigned find_battery_quirk(
struct hid_device *hdev)
317 quirks = match->driver_data;
322 static int hidinput_get_battery_property(
struct power_supply *psy,
339 dev->battery_report_type);
347 if (dev->battery_min < dev->battery_max &&
348 buf[1] >= dev->battery_min &&
349 buf[1] <= dev->battery_max)
350 val->
intval = (100 * (buf[1] - dev->battery_min)) /
351 (dev->battery_max - dev->battery_min);
374 static bool hidinput_setup_battery(
struct hid_device *dev,
unsigned report_type,
struct hid_field *field)
397 quirks = find_battery_quirk(dev);
399 hid_dbg(dev,
"device %x:%x:%x %d quirks %d\n",
405 if (quirks & HID_BATTERY_QUIRK_PERCENT) {
410 if (quirks & HID_BATTERY_QUIRK_FEATURE)
413 dev->battery_min =
min;
414 dev->battery_max =
max;
415 dev->battery_report_type = report_type;
416 dev->battery_report_id = field->
report->id;
420 hid_warn(dev,
"can't register power supply: %d\n", ret);
431 static void hidinput_cleanup_battery(
struct hid_device *dev)
433 if (!dev->battery.
name)
441 static bool hidinput_setup_battery(
struct hid_device *dev,
unsigned report_type,
447 static void hidinput_cleanup_battery(
struct hid_device *dev)
452 static void hidinput_configure_usage(
struct hid_input *hidinput,
struct hid_field *field,
471 if (device->
driver->input_mapping) {
472 int ret = device->
driver->input_mapping(device, hidinput, field,
522 switch (usage->
hid & 0xffff) {
528 default:
goto ignore;
533 if ((usage->
hid & 0xf0) == 0x80) {
534 switch (usage->
hid & 0xf) {
555 if ((usage->
hid & 0xf0) == 0x90) {
556 switch (usage->
hid) {
571 switch (usage->
hid) {
597 switch (usage->
hid & 0xffff) {
610 default:
goto ignore;
615 switch (usage->
hid & 0xff) {
669 case 0x000:
goto ignore;
810 default:
goto ignore;
836 default:
goto ignore;
845 default:
goto ignore;
862 default:
goto ignore;
885 if (device->
driver->input_mapped && device->
driver->input_mapped(device,
886 hidinput, field, usage, &bit, &max) < 0)
894 if (usage->
code > max)
909 input_set_abs_params(input, usage->
code, a, b, (b - a) >> 8, (b - a) >> 4);
910 else input_set_abs_params(input, usage->
code, a, b, 0, 0);
912 input_abs_set_res(input, usage->
code,
913 hidinput_calc_abs_res(field, usage->
code));
917 input_set_events_per_packet(input, 60);
923 for (i = usage->
code; i < usage->code + 2 && i <= max; i++) {
924 input_set_abs_params(input, i, -1, 1, 0, 0);
953 struct input_dev *
input;
954 unsigned *quirks = &hid->
quirks;
968 if (hat_dir < 0 || hat_dir > 8) hat_dir = 0;
996 dbg_hid(
"Maximum Effects - %d\n",value);
1014 for (i = 0; i <
count; i++) {
1028 (value < field->logical_minimum ||
1030 dbg_hid(
"Ignoring out-of-range value %x\n", value);
1052 input_sync(hidinput->input);
1062 for (i = 0; i < report->
maxfield; i++) {
1063 *field = report->
field[
i];
1064 for (j = 0; j < (*field)->maxusage; j++)
1065 if ((*field)->usage[j].type == type && (*field)->usage[j].code == code)
1082 for (i = 0; i < report->
maxfield; i++) {
1083 field = report->
field[
i];
1084 for (j = 0; j < field->
maxusage; j++)
1098 unsigned int count = 0;
1103 for (i = 0; i < report->
maxfield; i++) {
1104 field = report->
field[
i];
1105 for (j = 0; j < field->
maxusage; j++)
1115 static int hidinput_open(
struct input_dev *dev)
1117 struct hid_device *hid = input_get_drvdata(dev);
1119 return hid_hw_open(hid);
1122 static void hidinput_close(
struct input_dev *dev)
1124 struct hid_device *hid = input_get_drvdata(dev);
1129 static void report_features(
struct hid_device *hid)
1139 for (j = 0; j < rep->field[i]->maxusage; j++) {
1145 rep->
field[i]->usage + j);
1160 struct input_dev *input_dev;
1163 INIT_LIST_HEAD(&hid->
inputs);
1178 report_features(hid);
1191 hidinput = kzalloc(
sizeof(*hidinput),
GFP_KERNEL);
1192 input_dev = input_allocate_device();
1193 if (!hidinput || !input_dev) {
1195 input_free_device(input_dev);
1196 hid_err(hid,
"Out of memory during hid input probe\n");
1200 input_set_drvdata(input_dev, hid);
1203 input_dev->open = hidinput_open;
1204 input_dev->close = hidinput_close;
1205 input_dev->setkeycode = hidinput_setkeycode;
1206 input_dev->getkeycode = hidinput_getkeycode;
1208 input_dev->name = hid->
name;
1209 input_dev->phys = hid->
phys;
1210 input_dev->uniq = hid->
uniq;
1211 input_dev->id.bustype = hid->
bus;
1212 input_dev->id.vendor = hid->
vendor;
1213 input_dev->id.product = hid->
product;
1214 input_dev->id.version = hid->
version;
1215 input_dev->dev.parent = hid->
dev.parent;
1216 hidinput->
input = input_dev;
1220 for (i = 0; i < report->
maxfield; i++)
1221 for (j = 0; j < report->
field[
i]->maxusage; j++)
1222 hidinput_configure_usage(hidinput, report->
field[i],
1223 report->
field[i]->usage + j);
1234 if (input_register_device(hidinput->
input))
1244 if (input_register_device(hidinput->
input))
1252 input_free_device(hidinput->
input);
1266 hidinput_cleanup_battery(hid);
1270 input_unregister_device(hidinput->
input);