hid-input.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607
  1. /*
  2. * Copyright (c) 2000-2001 Vojtech Pavlik
  3. * Copyright (c) 2006-2010 Jiri Kosina
  4. *
  5. * HID to Linux Input mapping
  6. */
  7. /*
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. * Should you need to contact me, the author, you can do so either by
  23. * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
  24. * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
  25. */
  26. #include <linux/module.h>
  27. #include <linux/slab.h>
  28. #include <linux/kernel.h>
  29. #include <linux/hid.h>
  30. #include <linux/hid-debug.h>
  31. #include "hid-ids.h"
  32. #define unk KEY_UNKNOWN
  33. static const unsigned char hid_keyboard[256] = {
  34. 0, 0, 0, 0, 30, 48, 46, 32, 18, 33, 34, 35, 23, 36, 37, 38,
  35. 50, 49, 24, 25, 16, 19, 31, 20, 22, 47, 17, 45, 21, 44, 2, 3,
  36. 4, 5, 6, 7, 8, 9, 10, 11, 28, 1, 14, 15, 57, 12, 13, 26,
  37. 27, 43, 43, 39, 40, 41, 51, 52, 53, 58, 59, 60, 61, 62, 63, 64,
  38. 65, 66, 67, 68, 87, 88, 99, 70,119,110,102,104,111,107,109,106,
  39. 105,108,103, 69, 98, 55, 74, 78, 96, 79, 80, 81, 75, 76, 77, 71,
  40. 72, 73, 82, 83, 86,127,116,117,183,184,185,186,187,188,189,190,
  41. 191,192,193,194,134,138,130,132,128,129,131,137,133,135,136,113,
  42. 115,114,unk,unk,unk,121,unk, 89, 93,124, 92, 94, 95,unk,unk,unk,
  43. 122,123, 90, 91, 85,unk,unk,unk,unk,unk,unk,unk,111,unk,unk,unk,
  44. unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,
  45. unk,unk,unk,unk,unk,unk,179,180,unk,unk,unk,unk,unk,unk,unk,unk,
  46. unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,
  47. unk,unk,unk,unk,unk,unk,unk,unk,111,unk,unk,unk,unk,unk,unk,unk,
  48. 29, 42, 56,125, 97, 54,100,126,164,166,165,163,161,115,114,113,
  49. 150,158,159,128,136,177,178,176,142,152,173,140,unk,unk,unk,unk
  50. };
  51. static const struct {
  52. __s32 x;
  53. __s32 y;
  54. } hid_hat_to_axis[] = {{ 0, 0}, { 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}};
  55. #define map_abs(c) hid_map_usage(hidinput, usage, &bit, &max, EV_ABS, (c))
  56. #define map_rel(c) hid_map_usage(hidinput, usage, &bit, &max, EV_REL, (c))
  57. #define map_key(c) hid_map_usage(hidinput, usage, &bit, &max, EV_KEY, (c))
  58. #define map_led(c) hid_map_usage(hidinput, usage, &bit, &max, EV_LED, (c))
  59. #define map_abs_clear(c) hid_map_usage_clear(hidinput, usage, &bit, \
  60. &max, EV_ABS, (c))
  61. #define map_key_clear(c) hid_map_usage_clear(hidinput, usage, &bit, \
  62. &max, EV_KEY, (c))
  63. static bool match_scancode(struct hid_usage *usage,
  64. unsigned int cur_idx, unsigned int scancode)
  65. {
  66. return (usage->hid & (HID_USAGE_PAGE | HID_USAGE)) == scancode;
  67. }
  68. static bool match_keycode(struct hid_usage *usage,
  69. unsigned int cur_idx, unsigned int keycode)
  70. {
  71. /*
  72. * We should exclude unmapped usages when doing lookup by keycode.
  73. */
  74. return (usage->type == EV_KEY && usage->code == keycode);
  75. }
  76. static bool match_index(struct hid_usage *usage,
  77. unsigned int cur_idx, unsigned int idx)
  78. {
  79. return cur_idx == idx;
  80. }
  81. typedef bool (*hid_usage_cmp_t)(struct hid_usage *usage,
  82. unsigned int cur_idx, unsigned int val);
  83. static struct hid_usage *hidinput_find_key(struct hid_device *hid,
  84. hid_usage_cmp_t match,
  85. unsigned int value,
  86. unsigned int *usage_idx)
  87. {
  88. unsigned int i, j, k, cur_idx = 0;
  89. struct hid_report *report;
  90. struct hid_usage *usage;
  91. for (k = HID_INPUT_REPORT; k <= HID_OUTPUT_REPORT; k++) {
  92. list_for_each_entry(report, &hid->report_enum[k].report_list, list) {
  93. for (i = 0; i < report->maxfield; i++) {
  94. for (j = 0; j < report->field[i]->maxusage; j++) {
  95. usage = report->field[i]->usage + j;
  96. if (usage->type == EV_KEY || usage->type == 0) {
  97. if (match(usage, cur_idx, value)) {
  98. if (usage_idx)
  99. *usage_idx = cur_idx;
  100. return usage;
  101. }
  102. cur_idx++;
  103. }
  104. }
  105. }
  106. }
  107. }
  108. return NULL;
  109. }
  110. static struct hid_usage *hidinput_locate_usage(struct hid_device *hid,
  111. const struct input_keymap_entry *ke,
  112. unsigned int *index)
  113. {
  114. struct hid_usage *usage;
  115. unsigned int scancode;
  116. if (ke->flags & INPUT_KEYMAP_BY_INDEX)
  117. usage = hidinput_find_key(hid, match_index, ke->index, index);
  118. else if (input_scancode_to_scalar(ke, &scancode) == 0)
  119. usage = hidinput_find_key(hid, match_scancode, scancode, index);
  120. else
  121. usage = NULL;
  122. return usage;
  123. }
  124. static int hidinput_getkeycode(struct input_dev *dev,
  125. struct input_keymap_entry *ke)
  126. {
  127. struct hid_device *hid = input_get_drvdata(dev);
  128. struct hid_usage *usage;
  129. unsigned int scancode, index;
  130. usage = hidinput_locate_usage(hid, ke, &index);
  131. if (usage) {
  132. ke->keycode = usage->type == EV_KEY ?
  133. usage->code : KEY_RESERVED;
  134. ke->index = index;
  135. scancode = usage->hid & (HID_USAGE_PAGE | HID_USAGE);
  136. ke->len = sizeof(scancode);
  137. memcpy(ke->scancode, &scancode, sizeof(scancode));
  138. return 0;
  139. }
  140. return -EINVAL;
  141. }
  142. static int hidinput_setkeycode(struct input_dev *dev,
  143. const struct input_keymap_entry *ke,
  144. unsigned int *old_keycode)
  145. {
  146. struct hid_device *hid = input_get_drvdata(dev);
  147. struct hid_usage *usage;
  148. usage = hidinput_locate_usage(hid, ke, NULL);
  149. if (usage) {
  150. *old_keycode = usage->type == EV_KEY ?
  151. usage->code : KEY_RESERVED;
  152. usage->code = ke->keycode;
  153. clear_bit(*old_keycode, dev->keybit);
  154. set_bit(usage->code, dev->keybit);
  155. dbg_hid("Assigned keycode %d to HID usage code %x\n",
  156. usage->code, usage->hid);
  157. /*
  158. * Set the keybit for the old keycode if the old keycode is used
  159. * by another key
  160. */
  161. if (hidinput_find_key(hid, match_keycode, *old_keycode, NULL))
  162. set_bit(*old_keycode, dev->keybit);
  163. return 0;
  164. }
  165. return -EINVAL;
  166. }
  167. /**
  168. * hidinput_calc_abs_res - calculate an absolute axis resolution
  169. * @field: the HID report field to calculate resolution for
  170. * @code: axis code
  171. *
  172. * The formula is:
  173. * (logical_maximum - logical_minimum)
  174. * resolution = ----------------------------------------------------------
  175. * (physical_maximum - physical_minimum) * 10 ^ unit_exponent
  176. *
  177. * as seen in the HID specification v1.11 6.2.2.7 Global Items.
  178. *
  179. * Only exponent 1 length units are processed. Centimeters and inches are
  180. * converted to millimeters. Degrees are converted to radians.
  181. */
  182. __s32 hidinput_calc_abs_res(const struct hid_field *field, __u16 code)
  183. {
  184. __s32 unit_exponent = field->unit_exponent;
  185. __s32 logical_extents = field->logical_maximum -
  186. field->logical_minimum;
  187. __s32 physical_extents = field->physical_maximum -
  188. field->physical_minimum;
  189. __s32 prev;
  190. /* Check if the extents are sane */
  191. if (logical_extents <= 0 || physical_extents <= 0)
  192. return 0;
  193. /*
  194. * Verify and convert units.
  195. * See HID specification v1.11 6.2.2.7 Global Items for unit decoding
  196. */
  197. switch (code) {
  198. case ABS_X:
  199. case ABS_Y:
  200. case ABS_Z:
  201. case ABS_MT_POSITION_X:
  202. case ABS_MT_POSITION_Y:
  203. case ABS_MT_TOOL_X:
  204. case ABS_MT_TOOL_Y:
  205. case ABS_MT_TOUCH_MAJOR:
  206. case ABS_MT_TOUCH_MINOR:
  207. if (field->unit == 0x11) { /* If centimeters */
  208. /* Convert to millimeters */
  209. unit_exponent += 1;
  210. } else if (field->unit == 0x13) { /* If inches */
  211. /* Convert to millimeters */
  212. prev = physical_extents;
  213. physical_extents *= 254;
  214. if (physical_extents < prev)
  215. return 0;
  216. unit_exponent -= 1;
  217. } else {
  218. return 0;
  219. }
  220. break;
  221. case ABS_RX:
  222. case ABS_RY:
  223. case ABS_RZ:
  224. case ABS_TILT_X:
  225. case ABS_TILT_Y:
  226. if (field->unit == 0x14) { /* If degrees */
  227. /* Convert to radians */
  228. prev = logical_extents;
  229. logical_extents *= 573;
  230. if (logical_extents < prev)
  231. return 0;
  232. unit_exponent += 1;
  233. } else if (field->unit != 0x12) { /* If not radians */
  234. return 0;
  235. }
  236. break;
  237. default:
  238. return 0;
  239. }
  240. /* Apply negative unit exponent */
  241. for (; unit_exponent < 0; unit_exponent++) {
  242. prev = logical_extents;
  243. logical_extents *= 10;
  244. if (logical_extents < prev)
  245. return 0;
  246. }
  247. /* Apply positive unit exponent */
  248. for (; unit_exponent > 0; unit_exponent--) {
  249. prev = physical_extents;
  250. physical_extents *= 10;
  251. if (physical_extents < prev)
  252. return 0;
  253. }
  254. /* Calculate resolution */
  255. return DIV_ROUND_CLOSEST(logical_extents, physical_extents);
  256. }
  257. EXPORT_SYMBOL_GPL(hidinput_calc_abs_res);
  258. #ifdef CONFIG_HID_BATTERY_STRENGTH
  259. static enum power_supply_property hidinput_battery_props[] = {
  260. POWER_SUPPLY_PROP_PRESENT,
  261. POWER_SUPPLY_PROP_ONLINE,
  262. POWER_SUPPLY_PROP_CAPACITY,
  263. POWER_SUPPLY_PROP_MODEL_NAME,
  264. POWER_SUPPLY_PROP_STATUS,
  265. POWER_SUPPLY_PROP_SCOPE,
  266. };
  267. #define HID_BATTERY_QUIRK_PERCENT (1 << 0) /* always reports percent */
  268. #define HID_BATTERY_QUIRK_FEATURE (1 << 1) /* ask for feature report */
  269. #define HID_BATTERY_QUIRK_IGNORE (1 << 2) /* completely ignore the battery */
  270. static const struct hid_device_id hid_battery_quirks[] = {
  271. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE,
  272. USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_ISO),
  273. HID_BATTERY_QUIRK_PERCENT | HID_BATTERY_QUIRK_FEATURE },
  274. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE,
  275. USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_ANSI),
  276. HID_BATTERY_QUIRK_PERCENT | HID_BATTERY_QUIRK_FEATURE },
  277. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE,
  278. USB_DEVICE_ID_APPLE_ALU_WIRELESS_2011_ANSI),
  279. HID_BATTERY_QUIRK_PERCENT | HID_BATTERY_QUIRK_FEATURE },
  280. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE,
  281. USB_DEVICE_ID_APPLE_ALU_WIRELESS_2011_ISO),
  282. HID_BATTERY_QUIRK_PERCENT | HID_BATTERY_QUIRK_FEATURE },
  283. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE,
  284. USB_DEVICE_ID_APPLE_ALU_WIRELESS_ANSI),
  285. HID_BATTERY_QUIRK_PERCENT | HID_BATTERY_QUIRK_FEATURE },
  286. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_ELECOM,
  287. USB_DEVICE_ID_ELECOM_BM084),
  288. HID_BATTERY_QUIRK_IGNORE },
  289. {}
  290. };
  291. static unsigned find_battery_quirk(struct hid_device *hdev)
  292. {
  293. unsigned quirks = 0;
  294. const struct hid_device_id *match;
  295. match = hid_match_id(hdev, hid_battery_quirks);
  296. if (match != NULL)
  297. quirks = match->driver_data;
  298. return quirks;
  299. }
  300. static int hidinput_get_battery_property(struct power_supply *psy,
  301. enum power_supply_property prop,
  302. union power_supply_propval *val)
  303. {
  304. struct hid_device *dev = power_supply_get_drvdata(psy);
  305. int ret = 0;
  306. __u8 *buf;
  307. switch (prop) {
  308. case POWER_SUPPLY_PROP_PRESENT:
  309. case POWER_SUPPLY_PROP_ONLINE:
  310. val->intval = 1;
  311. break;
  312. case POWER_SUPPLY_PROP_CAPACITY:
  313. buf = kmalloc(2 * sizeof(__u8), GFP_KERNEL);
  314. if (!buf) {
  315. ret = -ENOMEM;
  316. break;
  317. }
  318. ret = hid_hw_raw_request(dev, dev->battery_report_id, buf, 2,
  319. dev->battery_report_type,
  320. HID_REQ_GET_REPORT);
  321. if (ret != 2) {
  322. ret = -ENODATA;
  323. kfree(buf);
  324. break;
  325. }
  326. ret = 0;
  327. if (dev->battery_min < dev->battery_max &&
  328. buf[1] >= dev->battery_min &&
  329. buf[1] <= dev->battery_max)
  330. val->intval = (100 * (buf[1] - dev->battery_min)) /
  331. (dev->battery_max - dev->battery_min);
  332. kfree(buf);
  333. break;
  334. case POWER_SUPPLY_PROP_MODEL_NAME:
  335. val->strval = dev->name;
  336. break;
  337. case POWER_SUPPLY_PROP_STATUS:
  338. val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
  339. break;
  340. case POWER_SUPPLY_PROP_SCOPE:
  341. val->intval = POWER_SUPPLY_SCOPE_DEVICE;
  342. break;
  343. default:
  344. ret = -EINVAL;
  345. break;
  346. }
  347. return ret;
  348. }
  349. static bool hidinput_setup_battery(struct hid_device *dev, unsigned report_type, struct hid_field *field)
  350. {
  351. struct power_supply_desc *psy_desc = NULL;
  352. struct power_supply_config psy_cfg = { .drv_data = dev, };
  353. unsigned quirks;
  354. s32 min, max;
  355. if (field->usage->hid != HID_DC_BATTERYSTRENGTH)
  356. return false; /* no match */
  357. if (dev->battery != NULL)
  358. goto out; /* already initialized? */
  359. quirks = find_battery_quirk(dev);
  360. hid_dbg(dev, "device %x:%x:%x %d quirks %d\n",
  361. dev->bus, dev->vendor, dev->product, dev->version, quirks);
  362. if (quirks & HID_BATTERY_QUIRK_IGNORE)
  363. goto out;
  364. psy_desc = kzalloc(sizeof(*psy_desc), GFP_KERNEL);
  365. if (psy_desc == NULL)
  366. goto out;
  367. psy_desc->name = kasprintf(GFP_KERNEL, "hid-%s-battery", dev->uniq);
  368. if (psy_desc->name == NULL) {
  369. kfree(psy_desc);
  370. goto out;
  371. }
  372. psy_desc->type = POWER_SUPPLY_TYPE_BATTERY;
  373. psy_desc->properties = hidinput_battery_props;
  374. psy_desc->num_properties = ARRAY_SIZE(hidinput_battery_props);
  375. psy_desc->use_for_apm = 0;
  376. psy_desc->get_property = hidinput_get_battery_property;
  377. min = field->logical_minimum;
  378. max = field->logical_maximum;
  379. if (quirks & HID_BATTERY_QUIRK_PERCENT) {
  380. min = 0;
  381. max = 100;
  382. }
  383. if (quirks & HID_BATTERY_QUIRK_FEATURE)
  384. report_type = HID_FEATURE_REPORT;
  385. dev->battery_min = min;
  386. dev->battery_max = max;
  387. dev->battery_report_type = report_type;
  388. dev->battery_report_id = field->report->id;
  389. dev->battery = power_supply_register(&dev->dev, psy_desc, &psy_cfg);
  390. if (IS_ERR(dev->battery)) {
  391. hid_warn(dev, "can't register power supply: %ld\n",
  392. PTR_ERR(dev->battery));
  393. kfree(psy_desc->name);
  394. kfree(psy_desc);
  395. dev->battery = NULL;
  396. } else {
  397. power_supply_powers(dev->battery, &dev->dev);
  398. }
  399. out:
  400. return true;
  401. }
  402. static void hidinput_cleanup_battery(struct hid_device *dev)
  403. {
  404. const struct power_supply_desc *psy_desc;
  405. if (!dev->battery)
  406. return;
  407. psy_desc = dev->battery->desc;
  408. power_supply_unregister(dev->battery);
  409. kfree(psy_desc->name);
  410. kfree(psy_desc);
  411. dev->battery = NULL;
  412. }
  413. #else /* !CONFIG_HID_BATTERY_STRENGTH */
  414. static bool hidinput_setup_battery(struct hid_device *dev, unsigned report_type,
  415. struct hid_field *field)
  416. {
  417. return false;
  418. }
  419. static void hidinput_cleanup_battery(struct hid_device *dev)
  420. {
  421. }
  422. #endif /* CONFIG_HID_BATTERY_STRENGTH */
  423. static void hidinput_configure_usage(struct hid_input *hidinput, struct hid_field *field,
  424. struct hid_usage *usage)
  425. {
  426. struct input_dev *input = hidinput->input;
  427. struct hid_device *device = input_get_drvdata(input);
  428. int max = 0, code;
  429. unsigned long *bit = NULL;
  430. field->hidinput = hidinput;
  431. if (field->flags & HID_MAIN_ITEM_CONSTANT)
  432. goto ignore;
  433. /* Ignore if report count is out of bounds. */
  434. if (field->report_count < 1)
  435. goto ignore;
  436. /* only LED usages are supported in output fields */
  437. if (field->report_type == HID_OUTPUT_REPORT &&
  438. (usage->hid & HID_USAGE_PAGE) != HID_UP_LED) {
  439. goto ignore;
  440. }
  441. if (device->driver->input_mapping) {
  442. int ret = device->driver->input_mapping(device, hidinput, field,
  443. usage, &bit, &max);
  444. if (ret > 0)
  445. goto mapped;
  446. if (ret < 0)
  447. goto ignore;
  448. }
  449. switch (usage->hid & HID_USAGE_PAGE) {
  450. case HID_UP_UNDEFINED:
  451. goto ignore;
  452. case HID_UP_KEYBOARD:
  453. set_bit(EV_REP, input->evbit);
  454. if ((usage->hid & HID_USAGE) < 256) {
  455. if (!hid_keyboard[usage->hid & HID_USAGE]) goto ignore;
  456. map_key_clear(hid_keyboard[usage->hid & HID_USAGE]);
  457. } else
  458. map_key(KEY_UNKNOWN);
  459. break;
  460. case HID_UP_BUTTON:
  461. code = ((usage->hid - 1) & HID_USAGE);
  462. switch (field->application) {
  463. case HID_GD_MOUSE:
  464. case HID_GD_POINTER: code += BTN_MOUSE; break;
  465. case HID_GD_JOYSTICK:
  466. if (code <= 0xf)
  467. code += BTN_JOYSTICK;
  468. else
  469. code += BTN_TRIGGER_HAPPY - 0x10;
  470. break;
  471. case HID_GD_GAMEPAD:
  472. if (code <= 0xf)
  473. code += BTN_GAMEPAD;
  474. else
  475. code += BTN_TRIGGER_HAPPY - 0x10;
  476. break;
  477. default:
  478. switch (field->physical) {
  479. case HID_GD_MOUSE:
  480. case HID_GD_POINTER: code += BTN_MOUSE; break;
  481. case HID_GD_JOYSTICK: code += BTN_JOYSTICK; break;
  482. case HID_GD_GAMEPAD: code += BTN_GAMEPAD; break;
  483. default: code += BTN_MISC;
  484. }
  485. }
  486. map_key(code);
  487. break;
  488. case HID_UP_SIMULATION:
  489. switch (usage->hid & 0xffff) {
  490. case 0xba: map_abs(ABS_RUDDER); break;
  491. case 0xbb: map_abs(ABS_THROTTLE); break;
  492. case 0xc4: map_abs(ABS_GAS); break;
  493. case 0xc5: map_abs(ABS_BRAKE); break;
  494. case 0xc8: map_abs(ABS_WHEEL); break;
  495. default: goto ignore;
  496. }
  497. break;
  498. case HID_UP_GENDESK:
  499. if ((usage->hid & 0xf0) == 0x80) { /* SystemControl */
  500. switch (usage->hid & 0xf) {
  501. case 0x1: map_key_clear(KEY_POWER); break;
  502. case 0x2: map_key_clear(KEY_SLEEP); break;
  503. case 0x3: map_key_clear(KEY_WAKEUP); break;
  504. case 0x4: map_key_clear(KEY_CONTEXT_MENU); break;
  505. case 0x5: map_key_clear(KEY_MENU); break;
  506. case 0x6: map_key_clear(KEY_PROG1); break;
  507. case 0x7: map_key_clear(KEY_HELP); break;
  508. case 0x8: map_key_clear(KEY_EXIT); break;
  509. case 0x9: map_key_clear(KEY_SELECT); break;
  510. case 0xa: map_key_clear(KEY_RIGHT); break;
  511. case 0xb: map_key_clear(KEY_LEFT); break;
  512. case 0xc: map_key_clear(KEY_UP); break;
  513. case 0xd: map_key_clear(KEY_DOWN); break;
  514. case 0xe: map_key_clear(KEY_POWER2); break;
  515. case 0xf: map_key_clear(KEY_RESTART); break;
  516. default: goto unknown;
  517. }
  518. break;
  519. }
  520. /*
  521. * Some lazy vendors declare 255 usages for System Control,
  522. * leading to the creation of ABS_X|Y axis and too many others.
  523. * It wouldn't be a problem if joydev doesn't consider the
  524. * device as a joystick then.
  525. */
  526. if (field->application == HID_GD_SYSTEM_CONTROL)
  527. goto ignore;
  528. if ((usage->hid & 0xf0) == 0x90) { /* D-pad */
  529. switch (usage->hid) {
  530. case HID_GD_UP: usage->hat_dir = 1; break;
  531. case HID_GD_DOWN: usage->hat_dir = 5; break;
  532. case HID_GD_RIGHT: usage->hat_dir = 3; break;
  533. case HID_GD_LEFT: usage->hat_dir = 7; break;
  534. default: goto unknown;
  535. }
  536. if (field->dpad) {
  537. map_abs(field->dpad);
  538. goto ignore;
  539. }
  540. map_abs(ABS_HAT0X);
  541. break;
  542. }
  543. switch (usage->hid) {
  544. /* These usage IDs map directly to the usage codes. */
  545. case HID_GD_X: case HID_GD_Y: case HID_GD_Z:
  546. case HID_GD_RX: case HID_GD_RY: case HID_GD_RZ:
  547. if (field->flags & HID_MAIN_ITEM_RELATIVE)
  548. map_rel(usage->hid & 0xf);
  549. else
  550. map_abs_clear(usage->hid & 0xf);
  551. break;
  552. case HID_GD_SLIDER: case HID_GD_DIAL: case HID_GD_WHEEL:
  553. if (field->flags & HID_MAIN_ITEM_RELATIVE)
  554. map_rel(usage->hid & 0xf);
  555. else
  556. map_abs(usage->hid & 0xf);
  557. break;
  558. case HID_GD_HATSWITCH:
  559. usage->hat_min = field->logical_minimum;
  560. usage->hat_max = field->logical_maximum;
  561. map_abs(ABS_HAT0X);
  562. break;
  563. case HID_GD_START: map_key_clear(BTN_START); break;
  564. case HID_GD_SELECT: map_key_clear(BTN_SELECT); break;
  565. default: goto unknown;
  566. }
  567. break;
  568. case HID_UP_LED:
  569. switch (usage->hid & 0xffff) { /* HID-Value: */
  570. case 0x01: map_led (LED_NUML); break; /* "Num Lock" */
  571. case 0x02: map_led (LED_CAPSL); break; /* "Caps Lock" */
  572. case 0x03: map_led (LED_SCROLLL); break; /* "Scroll Lock" */
  573. case 0x04: map_led (LED_COMPOSE); break; /* "Compose" */
  574. case 0x05: map_led (LED_KANA); break; /* "Kana" */
  575. case 0x27: map_led (LED_SLEEP); break; /* "Stand-By" */
  576. case 0x4c: map_led (LED_SUSPEND); break; /* "System Suspend" */
  577. case 0x09: map_led (LED_MUTE); break; /* "Mute" */
  578. case 0x4b: map_led (LED_MISC); break; /* "Generic Indicator" */
  579. case 0x19: map_led (LED_MAIL); break; /* "Message Waiting" */
  580. case 0x4d: map_led (LED_CHARGING); break; /* "External Power Connected" */
  581. default: goto ignore;
  582. }
  583. break;
  584. case HID_UP_DIGITIZER:
  585. switch (usage->hid & 0xff) {
  586. case 0x00: /* Undefined */
  587. goto ignore;
  588. case 0x30: /* TipPressure */
  589. if (!test_bit(BTN_TOUCH, input->keybit)) {
  590. device->quirks |= HID_QUIRK_NOTOUCH;
  591. set_bit(EV_KEY, input->evbit);
  592. set_bit(BTN_TOUCH, input->keybit);
  593. }
  594. map_abs_clear(ABS_PRESSURE);
  595. break;
  596. case 0x32: /* InRange */
  597. switch (field->physical & 0xff) {
  598. case 0x21: map_key(BTN_TOOL_MOUSE); break;
  599. case 0x22: map_key(BTN_TOOL_FINGER); break;
  600. default: map_key(BTN_TOOL_PEN); break;
  601. }
  602. break;
  603. case 0x3c: /* Invert */
  604. map_key_clear(BTN_TOOL_RUBBER);
  605. break;
  606. case 0x3d: /* X Tilt */
  607. map_abs_clear(ABS_TILT_X);
  608. break;
  609. case 0x3e: /* Y Tilt */
  610. map_abs_clear(ABS_TILT_Y);
  611. break;
  612. case 0x33: /* Touch */
  613. case 0x42: /* TipSwitch */
  614. case 0x43: /* TipSwitch2 */
  615. device->quirks &= ~HID_QUIRK_NOTOUCH;
  616. map_key_clear(BTN_TOUCH);
  617. break;
  618. case 0x44: /* BarrelSwitch */
  619. map_key_clear(BTN_STYLUS);
  620. break;
  621. case 0x46: /* TabletPick */
  622. case 0x5a: /* SecondaryBarrelSwitch */
  623. map_key_clear(BTN_STYLUS2);
  624. break;
  625. case 0x5b: /* TransducerSerialNumber */
  626. usage->type = EV_MSC;
  627. usage->code = MSC_SERIAL;
  628. bit = input->mscbit;
  629. max = MSC_MAX;
  630. break;
  631. default: goto unknown;
  632. }
  633. break;
  634. case HID_UP_TELEPHONY:
  635. switch (usage->hid & HID_USAGE) {
  636. case 0x2f: map_key_clear(KEY_MICMUTE); break;
  637. case 0xb0: map_key_clear(KEY_NUMERIC_0); break;
  638. case 0xb1: map_key_clear(KEY_NUMERIC_1); break;
  639. case 0xb2: map_key_clear(KEY_NUMERIC_2); break;
  640. case 0xb3: map_key_clear(KEY_NUMERIC_3); break;
  641. case 0xb4: map_key_clear(KEY_NUMERIC_4); break;
  642. case 0xb5: map_key_clear(KEY_NUMERIC_5); break;
  643. case 0xb6: map_key_clear(KEY_NUMERIC_6); break;
  644. case 0xb7: map_key_clear(KEY_NUMERIC_7); break;
  645. case 0xb8: map_key_clear(KEY_NUMERIC_8); break;
  646. case 0xb9: map_key_clear(KEY_NUMERIC_9); break;
  647. case 0xba: map_key_clear(KEY_NUMERIC_STAR); break;
  648. case 0xbb: map_key_clear(KEY_NUMERIC_POUND); break;
  649. case 0xbc: map_key_clear(KEY_NUMERIC_A); break;
  650. case 0xbd: map_key_clear(KEY_NUMERIC_B); break;
  651. case 0xbe: map_key_clear(KEY_NUMERIC_C); break;
  652. case 0xbf: map_key_clear(KEY_NUMERIC_D); break;
  653. default: goto ignore;
  654. }
  655. break;
  656. case HID_UP_CONSUMER: /* USB HUT v1.12, pages 75-84 */
  657. switch (usage->hid & HID_USAGE) {
  658. case 0x000: goto ignore;
  659. case 0x030: map_key_clear(KEY_POWER); break;
  660. case 0x031: map_key_clear(KEY_RESTART); break;
  661. case 0x032: map_key_clear(KEY_SLEEP); break;
  662. case 0x034: map_key_clear(KEY_SLEEP); break;
  663. case 0x035: map_key_clear(KEY_KBDILLUMTOGGLE); break;
  664. case 0x036: map_key_clear(BTN_MISC); break;
  665. case 0x040: map_key_clear(KEY_MENU); break; /* Menu */
  666. case 0x041: map_key_clear(KEY_SELECT); break; /* Menu Pick */
  667. case 0x042: map_key_clear(KEY_UP); break; /* Menu Up */
  668. case 0x043: map_key_clear(KEY_DOWN); break; /* Menu Down */
  669. case 0x044: map_key_clear(KEY_LEFT); break; /* Menu Left */
  670. case 0x045: map_key_clear(KEY_RIGHT); break; /* Menu Right */
  671. case 0x046: map_key_clear(KEY_ESC); break; /* Menu Escape */
  672. case 0x047: map_key_clear(KEY_KPPLUS); break; /* Menu Value Increase */
  673. case 0x048: map_key_clear(KEY_KPMINUS); break; /* Menu Value Decrease */
  674. case 0x060: map_key_clear(KEY_INFO); break; /* Data On Screen */
  675. case 0x061: map_key_clear(KEY_SUBTITLE); break; /* Closed Caption */
  676. case 0x063: map_key_clear(KEY_VCR); break; /* VCR/TV */
  677. case 0x065: map_key_clear(KEY_CAMERA); break; /* Snapshot */
  678. case 0x069: map_key_clear(KEY_RED); break;
  679. case 0x06a: map_key_clear(KEY_GREEN); break;
  680. case 0x06b: map_key_clear(KEY_BLUE); break;
  681. case 0x06c: map_key_clear(KEY_YELLOW); break;
  682. case 0x06d: map_key_clear(KEY_ZOOM); break;
  683. case 0x06f: map_key_clear(KEY_BRIGHTNESSUP); break;
  684. case 0x070: map_key_clear(KEY_BRIGHTNESSDOWN); break;
  685. case 0x072: map_key_clear(KEY_BRIGHTNESS_TOGGLE); break;
  686. case 0x073: map_key_clear(KEY_BRIGHTNESS_MIN); break;
  687. case 0x074: map_key_clear(KEY_BRIGHTNESS_MAX); break;
  688. case 0x075: map_key_clear(KEY_BRIGHTNESS_AUTO); break;
  689. case 0x082: map_key_clear(KEY_VIDEO_NEXT); break;
  690. case 0x083: map_key_clear(KEY_LAST); break;
  691. case 0x084: map_key_clear(KEY_ENTER); break;
  692. case 0x088: map_key_clear(KEY_PC); break;
  693. case 0x089: map_key_clear(KEY_TV); break;
  694. case 0x08a: map_key_clear(KEY_WWW); break;
  695. case 0x08b: map_key_clear(KEY_DVD); break;
  696. case 0x08c: map_key_clear(KEY_PHONE); break;
  697. case 0x08d: map_key_clear(KEY_PROGRAM); break;
  698. case 0x08e: map_key_clear(KEY_VIDEOPHONE); break;
  699. case 0x08f: map_key_clear(KEY_GAMES); break;
  700. case 0x090: map_key_clear(KEY_MEMO); break;
  701. case 0x091: map_key_clear(KEY_CD); break;
  702. case 0x092: map_key_clear(KEY_VCR); break;
  703. case 0x093: map_key_clear(KEY_TUNER); break;
  704. case 0x094: map_key_clear(KEY_EXIT); break;
  705. case 0x095: map_key_clear(KEY_HELP); break;
  706. case 0x096: map_key_clear(KEY_TAPE); break;
  707. case 0x097: map_key_clear(KEY_TV2); break;
  708. case 0x098: map_key_clear(KEY_SAT); break;
  709. case 0x09a: map_key_clear(KEY_PVR); break;
  710. case 0x09c: map_key_clear(KEY_CHANNELUP); break;
  711. case 0x09d: map_key_clear(KEY_CHANNELDOWN); break;
  712. case 0x0a0: map_key_clear(KEY_VCR2); break;
  713. case 0x0b0: map_key_clear(KEY_PLAY); break;
  714. case 0x0b1: map_key_clear(KEY_PAUSE); break;
  715. case 0x0b2: map_key_clear(KEY_RECORD); break;
  716. case 0x0b3: map_key_clear(KEY_FASTFORWARD); break;
  717. case 0x0b4: map_key_clear(KEY_REWIND); break;
  718. case 0x0b5: map_key_clear(KEY_NEXTSONG); break;
  719. case 0x0b6: map_key_clear(KEY_PREVIOUSSONG); break;
  720. case 0x0b7: map_key_clear(KEY_STOPCD); break;
  721. case 0x0b8: map_key_clear(KEY_EJECTCD); break;
  722. case 0x0bc: map_key_clear(KEY_MEDIA_REPEAT); break;
  723. case 0x0b9: map_key_clear(KEY_SHUFFLE); break;
  724. case 0x0bf: map_key_clear(KEY_SLOW); break;
  725. case 0x0cd: map_key_clear(KEY_PLAYPAUSE); break;
  726. case 0x0cf: map_key_clear(KEY_VOICECOMMAND); break;
  727. case 0x0e0: map_abs_clear(ABS_VOLUME); break;
  728. case 0x0e2: map_key_clear(KEY_MUTE); break;
  729. case 0x0e5: map_key_clear(KEY_BASSBOOST); break;
  730. case 0x0e9: map_key_clear(KEY_VOLUMEUP); break;
  731. case 0x0ea: map_key_clear(KEY_VOLUMEDOWN); break;
  732. case 0x0f5: map_key_clear(KEY_SLOW); break;
  733. case 0x181: map_key_clear(KEY_BUTTONCONFIG); break;
  734. case 0x182: map_key_clear(KEY_BOOKMARKS); break;
  735. case 0x183: map_key_clear(KEY_CONFIG); break;
  736. case 0x184: map_key_clear(KEY_WORDPROCESSOR); break;
  737. case 0x185: map_key_clear(KEY_EDITOR); break;
  738. case 0x186: map_key_clear(KEY_SPREADSHEET); break;
  739. case 0x187: map_key_clear(KEY_GRAPHICSEDITOR); break;
  740. case 0x188: map_key_clear(KEY_PRESENTATION); break;
  741. case 0x189: map_key_clear(KEY_DATABASE); break;
  742. case 0x18a: map_key_clear(KEY_MAIL); break;
  743. case 0x18b: map_key_clear(KEY_NEWS); break;
  744. case 0x18c: map_key_clear(KEY_VOICEMAIL); break;
  745. case 0x18d: map_key_clear(KEY_ADDRESSBOOK); break;
  746. case 0x18e: map_key_clear(KEY_CALENDAR); break;
  747. case 0x18f: map_key_clear(KEY_TASKMANAGER); break;
  748. case 0x190: map_key_clear(KEY_JOURNAL); break;
  749. case 0x191: map_key_clear(KEY_FINANCE); break;
  750. case 0x192: map_key_clear(KEY_CALC); break;
  751. case 0x193: map_key_clear(KEY_PLAYER); break;
  752. case 0x194: map_key_clear(KEY_FILE); break;
  753. case 0x196: map_key_clear(KEY_WWW); break;
  754. case 0x199: map_key_clear(KEY_CHAT); break;
  755. case 0x19c: map_key_clear(KEY_LOGOFF); break;
  756. case 0x19e: map_key_clear(KEY_COFFEE); break;
  757. case 0x19f: map_key_clear(KEY_CONTROLPANEL); break;
  758. case 0x1a2: map_key_clear(KEY_APPSELECT); break;
  759. case 0x1a3: map_key_clear(KEY_NEXT); break;
  760. case 0x1a4: map_key_clear(KEY_PREVIOUS); break;
  761. case 0x1a6: map_key_clear(KEY_HELP); break;
  762. case 0x1a7: map_key_clear(KEY_DOCUMENTS); break;
  763. case 0x1ab: map_key_clear(KEY_SPELLCHECK); break;
  764. case 0x1ae: map_key_clear(KEY_KEYBOARD); break;
  765. case 0x1b1: map_key_clear(KEY_SCREENSAVER); break;
  766. case 0x1b4: map_key_clear(KEY_FILE); break;
  767. case 0x1b6: map_key_clear(KEY_IMAGES); break;
  768. case 0x1b7: map_key_clear(KEY_AUDIO); break;
  769. case 0x1b8: map_key_clear(KEY_VIDEO); break;
  770. case 0x1bc: map_key_clear(KEY_MESSENGER); break;
  771. case 0x1bd: map_key_clear(KEY_INFO); break;
  772. case 0x201: map_key_clear(KEY_NEW); break;
  773. case 0x202: map_key_clear(KEY_OPEN); break;
  774. case 0x203: map_key_clear(KEY_CLOSE); break;
  775. case 0x204: map_key_clear(KEY_EXIT); break;
  776. case 0x207: map_key_clear(KEY_SAVE); break;
  777. case 0x208: map_key_clear(KEY_PRINT); break;
  778. case 0x209: map_key_clear(KEY_PROPS); break;
  779. case 0x21a: map_key_clear(KEY_UNDO); break;
  780. case 0x21b: map_key_clear(KEY_COPY); break;
  781. case 0x21c: map_key_clear(KEY_CUT); break;
  782. case 0x21d: map_key_clear(KEY_PASTE); break;
  783. case 0x21f: map_key_clear(KEY_FIND); break;
  784. case 0x221: map_key_clear(KEY_SEARCH); break;
  785. case 0x222: map_key_clear(KEY_GOTO); break;
  786. case 0x223: map_key_clear(KEY_HOMEPAGE); break;
  787. case 0x224: map_key_clear(KEY_BACK); break;
  788. case 0x225: map_key_clear(KEY_FORWARD); break;
  789. case 0x226: map_key_clear(KEY_STOP); break;
  790. case 0x227: map_key_clear(KEY_REFRESH); break;
  791. case 0x22a: map_key_clear(KEY_BOOKMARKS); break;
  792. case 0x22d: map_key_clear(KEY_ZOOMIN); break;
  793. case 0x22e: map_key_clear(KEY_ZOOMOUT); break;
  794. case 0x22f: map_key_clear(KEY_ZOOMRESET); break;
  795. case 0x233: map_key_clear(KEY_SCROLLUP); break;
  796. case 0x234: map_key_clear(KEY_SCROLLDOWN); break;
  797. case 0x238: map_rel(REL_HWHEEL); break;
  798. case 0x23d: map_key_clear(KEY_EDIT); break;
  799. case 0x25f: map_key_clear(KEY_CANCEL); break;
  800. case 0x269: map_key_clear(KEY_INSERT); break;
  801. case 0x26a: map_key_clear(KEY_DELETE); break;
  802. case 0x279: map_key_clear(KEY_REDO); break;
  803. case 0x289: map_key_clear(KEY_REPLY); break;
  804. case 0x28b: map_key_clear(KEY_FORWARDMAIL); break;
  805. case 0x28c: map_key_clear(KEY_SEND); break;
  806. case 0x2c7: map_key_clear(KEY_KBDINPUTASSIST_PREV); break;
  807. case 0x2c8: map_key_clear(KEY_KBDINPUTASSIST_NEXT); break;
  808. case 0x2c9: map_key_clear(KEY_KBDINPUTASSIST_PREVGROUP); break;
  809. case 0x2ca: map_key_clear(KEY_KBDINPUTASSIST_NEXTGROUP); break;
  810. case 0x2cb: map_key_clear(KEY_KBDINPUTASSIST_ACCEPT); break;
  811. case 0x2cc: map_key_clear(KEY_KBDINPUTASSIST_CANCEL); break;
  812. default: map_key_clear(KEY_UNKNOWN);
  813. }
  814. break;
  815. case HID_UP_GENDEVCTRLS:
  816. if (hidinput_setup_battery(device, HID_INPUT_REPORT, field))
  817. goto ignore;
  818. else
  819. goto unknown;
  820. break;
  821. case HID_UP_HPVENDOR: /* Reported on a Dutch layout HP5308 */
  822. set_bit(EV_REP, input->evbit);
  823. switch (usage->hid & HID_USAGE) {
  824. case 0x021: map_key_clear(KEY_PRINT); break;
  825. case 0x070: map_key_clear(KEY_HP); break;
  826. case 0x071: map_key_clear(KEY_CAMERA); break;
  827. case 0x072: map_key_clear(KEY_SOUND); break;
  828. case 0x073: map_key_clear(KEY_QUESTION); break;
  829. case 0x080: map_key_clear(KEY_EMAIL); break;
  830. case 0x081: map_key_clear(KEY_CHAT); break;
  831. case 0x082: map_key_clear(KEY_SEARCH); break;
  832. case 0x083: map_key_clear(KEY_CONNECT); break;
  833. case 0x084: map_key_clear(KEY_FINANCE); break;
  834. case 0x085: map_key_clear(KEY_SPORT); break;
  835. case 0x086: map_key_clear(KEY_SHOP); break;
  836. default: goto ignore;
  837. }
  838. break;
  839. case HID_UP_HPVENDOR2:
  840. set_bit(EV_REP, input->evbit);
  841. switch (usage->hid & HID_USAGE) {
  842. case 0x001: map_key_clear(KEY_MICMUTE); break;
  843. case 0x003: map_key_clear(KEY_BRIGHTNESSDOWN); break;
  844. case 0x004: map_key_clear(KEY_BRIGHTNESSUP); break;
  845. default: goto ignore;
  846. }
  847. break;
  848. case HID_UP_MSVENDOR:
  849. goto ignore;
  850. case HID_UP_CUSTOM: /* Reported on Logitech and Apple USB keyboards */
  851. set_bit(EV_REP, input->evbit);
  852. goto ignore;
  853. case HID_UP_LOGIVENDOR:
  854. /* intentional fallback */
  855. case HID_UP_LOGIVENDOR2:
  856. /* intentional fallback */
  857. case HID_UP_LOGIVENDOR3:
  858. goto ignore;
  859. case HID_UP_PID:
  860. switch (usage->hid & HID_USAGE) {
  861. case 0xa4: map_key_clear(BTN_DEAD); break;
  862. default: goto ignore;
  863. }
  864. break;
  865. default:
  866. unknown:
  867. if (field->report_size == 1) {
  868. if (field->report->type == HID_OUTPUT_REPORT) {
  869. map_led(LED_MISC);
  870. break;
  871. }
  872. map_key(BTN_MISC);
  873. break;
  874. }
  875. if (field->flags & HID_MAIN_ITEM_RELATIVE) {
  876. map_rel(REL_MISC);
  877. break;
  878. }
  879. map_abs(ABS_MISC);
  880. break;
  881. }
  882. mapped:
  883. if (device->driver->input_mapped && device->driver->input_mapped(device,
  884. hidinput, field, usage, &bit, &max) < 0)
  885. goto ignore;
  886. set_bit(usage->type, input->evbit);
  887. while (usage->code <= max && test_and_set_bit(usage->code, bit))
  888. usage->code = find_next_zero_bit(bit, max + 1, usage->code);
  889. if (usage->code > max)
  890. goto ignore;
  891. if (usage->type == EV_ABS) {
  892. int a = field->logical_minimum;
  893. int b = field->logical_maximum;
  894. if ((device->quirks & HID_QUIRK_BADPAD) && (usage->code == ABS_X || usage->code == ABS_Y)) {
  895. a = field->logical_minimum = 0;
  896. b = field->logical_maximum = 255;
  897. }
  898. if (field->application == HID_GD_GAMEPAD || field->application == HID_GD_JOYSTICK)
  899. input_set_abs_params(input, usage->code, a, b, (b - a) >> 8, (b - a) >> 4);
  900. else input_set_abs_params(input, usage->code, a, b, 0, 0);
  901. input_abs_set_res(input, usage->code,
  902. hidinput_calc_abs_res(field, usage->code));
  903. /* use a larger default input buffer for MT devices */
  904. if (usage->code == ABS_MT_POSITION_X && input->hint_events_per_packet == 0)
  905. input_set_events_per_packet(input, 60);
  906. }
  907. if (usage->type == EV_ABS &&
  908. (usage->hat_min < usage->hat_max || usage->hat_dir)) {
  909. int i;
  910. for (i = usage->code; i < usage->code + 2 && i <= max; i++) {
  911. input_set_abs_params(input, i, -1, 1, 0, 0);
  912. set_bit(i, input->absbit);
  913. }
  914. if (usage->hat_dir && !field->dpad)
  915. field->dpad = usage->code;
  916. }
  917. /* for those devices which produce Consumer volume usage as relative,
  918. * we emulate pressing volumeup/volumedown appropriate number of times
  919. * in hidinput_hid_event()
  920. */
  921. if ((usage->type == EV_ABS) && (field->flags & HID_MAIN_ITEM_RELATIVE) &&
  922. (usage->code == ABS_VOLUME)) {
  923. set_bit(KEY_VOLUMEUP, input->keybit);
  924. set_bit(KEY_VOLUMEDOWN, input->keybit);
  925. }
  926. if (usage->type == EV_KEY) {
  927. set_bit(EV_MSC, input->evbit);
  928. set_bit(MSC_SCAN, input->mscbit);
  929. }
  930. ignore:
  931. return;
  932. }
  933. void hidinput_hid_event(struct hid_device *hid, struct hid_field *field, struct hid_usage *usage, __s32 value)
  934. {
  935. struct input_dev *input;
  936. unsigned *quirks = &hid->quirks;
  937. if (!field->hidinput)
  938. return;
  939. input = field->hidinput->input;
  940. if (!usage->type)
  941. return;
  942. if (usage->hat_min < usage->hat_max || usage->hat_dir) {
  943. int hat_dir = usage->hat_dir;
  944. if (!hat_dir)
  945. hat_dir = (value - usage->hat_min) * 8 / (usage->hat_max - usage->hat_min + 1) + 1;
  946. if (hat_dir < 0 || hat_dir > 8) hat_dir = 0;
  947. input_event(input, usage->type, usage->code , hid_hat_to_axis[hat_dir].x);
  948. input_event(input, usage->type, usage->code + 1, hid_hat_to_axis[hat_dir].y);
  949. return;
  950. }
  951. if (usage->hid == (HID_UP_DIGITIZER | 0x003c)) { /* Invert */
  952. *quirks = value ? (*quirks | HID_QUIRK_INVERT) : (*quirks & ~HID_QUIRK_INVERT);
  953. return;
  954. }
  955. if (usage->hid == (HID_UP_DIGITIZER | 0x0032)) { /* InRange */
  956. if (value) {
  957. input_event(input, usage->type, (*quirks & HID_QUIRK_INVERT) ? BTN_TOOL_RUBBER : usage->code, 1);
  958. return;
  959. }
  960. input_event(input, usage->type, usage->code, 0);
  961. input_event(input, usage->type, BTN_TOOL_RUBBER, 0);
  962. return;
  963. }
  964. if (usage->hid == (HID_UP_DIGITIZER | 0x0030) && (*quirks & HID_QUIRK_NOTOUCH)) { /* Pressure */
  965. int a = field->logical_minimum;
  966. int b = field->logical_maximum;
  967. input_event(input, EV_KEY, BTN_TOUCH, value > a + ((b - a) >> 3));
  968. }
  969. if (usage->hid == (HID_UP_PID | 0x83UL)) { /* Simultaneous Effects Max */
  970. dbg_hid("Maximum Effects - %d\n",value);
  971. return;
  972. }
  973. if (usage->hid == (HID_UP_PID | 0x7fUL)) {
  974. dbg_hid("PID Pool Report\n");
  975. return;
  976. }
  977. if ((usage->type == EV_KEY) && (usage->code == 0)) /* Key 0 is "unassigned", not KEY_UNKNOWN */
  978. return;
  979. if ((usage->type == EV_ABS) && (field->flags & HID_MAIN_ITEM_RELATIVE) &&
  980. (usage->code == ABS_VOLUME)) {
  981. int count = abs(value);
  982. int direction = value > 0 ? KEY_VOLUMEUP : KEY_VOLUMEDOWN;
  983. int i;
  984. for (i = 0; i < count; i++) {
  985. input_event(input, EV_KEY, direction, 1);
  986. input_sync(input);
  987. input_event(input, EV_KEY, direction, 0);
  988. input_sync(input);
  989. }
  990. return;
  991. }
  992. /*
  993. * Ignore out-of-range values as per HID specification,
  994. * section 5.10 and 6.2.25, when NULL state bit is present.
  995. * When it's not, clamp the value to match Microsoft's input
  996. * driver as mentioned in "Required HID usages for digitizers":
  997. * https://msdn.microsoft.com/en-us/library/windows/hardware/dn672278(v=vs.85).asp
  998. *
  999. * The logical_minimum < logical_maximum check is done so that we
  1000. * don't unintentionally discard values sent by devices which
  1001. * don't specify logical min and max.
  1002. */
  1003. if ((field->flags & HID_MAIN_ITEM_VARIABLE) &&
  1004. (field->logical_minimum < field->logical_maximum)) {
  1005. if (field->flags & HID_MAIN_ITEM_NULL_STATE &&
  1006. (value < field->logical_minimum ||
  1007. value > field->logical_maximum)) {
  1008. dbg_hid("Ignoring out-of-range value %x\n", value);
  1009. return;
  1010. }
  1011. value = clamp(value,
  1012. field->logical_minimum,
  1013. field->logical_maximum);
  1014. }
  1015. /*
  1016. * Ignore reports for absolute data if the data didn't change. This is
  1017. * not only an optimization but also fixes 'dead' key reports. Some
  1018. * RollOver implementations for localized keys (like BACKSLASH/PIPE; HID
  1019. * 0x31 and 0x32) report multiple keys, even though a localized keyboard
  1020. * can only have one of them physically available. The 'dead' keys
  1021. * report constant 0. As all map to the same keycode, they'd confuse
  1022. * the input layer. If we filter the 'dead' keys on the HID level, we
  1023. * skip the keycode translation and only forward real events.
  1024. */
  1025. if (!(field->flags & (HID_MAIN_ITEM_RELATIVE |
  1026. HID_MAIN_ITEM_BUFFERED_BYTE)) &&
  1027. (field->flags & HID_MAIN_ITEM_VARIABLE) &&
  1028. usage->usage_index < field->maxusage &&
  1029. value == field->value[usage->usage_index])
  1030. return;
  1031. /* report the usage code as scancode if the key status has changed */
  1032. if (usage->type == EV_KEY &&
  1033. (!test_bit(usage->code, input->key)) == value)
  1034. input_event(input, EV_MSC, MSC_SCAN, usage->hid);
  1035. input_event(input, usage->type, usage->code, value);
  1036. if ((field->flags & HID_MAIN_ITEM_RELATIVE) &&
  1037. usage->type == EV_KEY && value) {
  1038. input_sync(input);
  1039. input_event(input, usage->type, usage->code, 0);
  1040. }
  1041. }
  1042. void hidinput_report_event(struct hid_device *hid, struct hid_report *report)
  1043. {
  1044. struct hid_input *hidinput;
  1045. if (hid->quirks & HID_QUIRK_NO_INPUT_SYNC)
  1046. return;
  1047. list_for_each_entry(hidinput, &hid->inputs, list)
  1048. input_sync(hidinput->input);
  1049. }
  1050. EXPORT_SYMBOL_GPL(hidinput_report_event);
  1051. int hidinput_find_field(struct hid_device *hid, unsigned int type, unsigned int code, struct hid_field **field)
  1052. {
  1053. struct hid_report *report;
  1054. int i, j;
  1055. list_for_each_entry(report, &hid->report_enum[HID_OUTPUT_REPORT].report_list, list) {
  1056. for (i = 0; i < report->maxfield; i++) {
  1057. *field = report->field[i];
  1058. for (j = 0; j < (*field)->maxusage; j++)
  1059. if ((*field)->usage[j].type == type && (*field)->usage[j].code == code)
  1060. return j;
  1061. }
  1062. }
  1063. return -1;
  1064. }
  1065. EXPORT_SYMBOL_GPL(hidinput_find_field);
  1066. struct hid_field *hidinput_get_led_field(struct hid_device *hid)
  1067. {
  1068. struct hid_report *report;
  1069. struct hid_field *field;
  1070. int i, j;
  1071. list_for_each_entry(report,
  1072. &hid->report_enum[HID_OUTPUT_REPORT].report_list,
  1073. list) {
  1074. for (i = 0; i < report->maxfield; i++) {
  1075. field = report->field[i];
  1076. for (j = 0; j < field->maxusage; j++)
  1077. if (field->usage[j].type == EV_LED)
  1078. return field;
  1079. }
  1080. }
  1081. return NULL;
  1082. }
  1083. EXPORT_SYMBOL_GPL(hidinput_get_led_field);
  1084. unsigned int hidinput_count_leds(struct hid_device *hid)
  1085. {
  1086. struct hid_report *report;
  1087. struct hid_field *field;
  1088. int i, j;
  1089. unsigned int count = 0;
  1090. list_for_each_entry(report,
  1091. &hid->report_enum[HID_OUTPUT_REPORT].report_list,
  1092. list) {
  1093. for (i = 0; i < report->maxfield; i++) {
  1094. field = report->field[i];
  1095. for (j = 0; j < field->maxusage; j++)
  1096. if (field->usage[j].type == EV_LED &&
  1097. field->value[j])
  1098. count += 1;
  1099. }
  1100. }
  1101. return count;
  1102. }
  1103. EXPORT_SYMBOL_GPL(hidinput_count_leds);
  1104. static void hidinput_led_worker(struct work_struct *work)
  1105. {
  1106. struct hid_device *hid = container_of(work, struct hid_device,
  1107. led_work);
  1108. struct hid_field *field;
  1109. struct hid_report *report;
  1110. int ret;
  1111. u32 len;
  1112. __u8 *buf;
  1113. field = hidinput_get_led_field(hid);
  1114. if (!field)
  1115. return;
  1116. /*
  1117. * field->report is accessed unlocked regarding HID core. So there might
  1118. * be another incoming SET-LED request from user-space, which changes
  1119. * the LED state while we assemble our outgoing buffer. However, this
  1120. * doesn't matter as hid_output_report() correctly converts it into a
  1121. * boolean value no matter what information is currently set on the LED
  1122. * field (even garbage). So the remote device will always get a valid
  1123. * request.
  1124. * And in case we send a wrong value, a next led worker is spawned
  1125. * for every SET-LED request so the following worker will send the
  1126. * correct value, guaranteed!
  1127. */
  1128. report = field->report;
  1129. /* use custom SET_REPORT request if possible (asynchronous) */
  1130. if (hid->ll_driver->request)
  1131. return hid->ll_driver->request(hid, report, HID_REQ_SET_REPORT);
  1132. /* fall back to generic raw-output-report */
  1133. len = hid_report_len(report);
  1134. buf = hid_alloc_report_buf(report, GFP_KERNEL);
  1135. if (!buf)
  1136. return;
  1137. hid_output_report(report, buf);
  1138. /* synchronous output report */
  1139. ret = hid_hw_output_report(hid, buf, len);
  1140. if (ret == -ENOSYS)
  1141. hid_hw_raw_request(hid, report->id, buf, len, HID_OUTPUT_REPORT,
  1142. HID_REQ_SET_REPORT);
  1143. kfree(buf);
  1144. }
  1145. static int hidinput_input_event(struct input_dev *dev, unsigned int type,
  1146. unsigned int code, int value)
  1147. {
  1148. struct hid_device *hid = input_get_drvdata(dev);
  1149. struct hid_field *field;
  1150. int offset;
  1151. if (type == EV_FF)
  1152. return input_ff_event(dev, type, code, value);
  1153. if (type != EV_LED)
  1154. return -1;
  1155. if ((offset = hidinput_find_field(hid, type, code, &field)) == -1) {
  1156. hid_warn(dev, "event field not found\n");
  1157. return -1;
  1158. }
  1159. hid_set_field(field, offset, value);
  1160. schedule_work(&hid->led_work);
  1161. return 0;
  1162. }
  1163. static int hidinput_open(struct input_dev *dev)
  1164. {
  1165. struct hid_device *hid = input_get_drvdata(dev);
  1166. return hid_hw_open(hid);
  1167. }
  1168. static void hidinput_close(struct input_dev *dev)
  1169. {
  1170. struct hid_device *hid = input_get_drvdata(dev);
  1171. hid_hw_close(hid);
  1172. }
  1173. static void report_features(struct hid_device *hid)
  1174. {
  1175. struct hid_driver *drv = hid->driver;
  1176. struct hid_report_enum *rep_enum;
  1177. struct hid_report *rep;
  1178. int i, j;
  1179. rep_enum = &hid->report_enum[HID_FEATURE_REPORT];
  1180. list_for_each_entry(rep, &rep_enum->report_list, list)
  1181. for (i = 0; i < rep->maxfield; i++) {
  1182. /* Ignore if report count is out of bounds. */
  1183. if (rep->field[i]->report_count < 1)
  1184. continue;
  1185. for (j = 0; j < rep->field[i]->maxusage; j++) {
  1186. /* Verify if Battery Strength feature is available */
  1187. hidinput_setup_battery(hid, HID_FEATURE_REPORT, rep->field[i]);
  1188. if (drv->feature_mapping)
  1189. drv->feature_mapping(hid, rep->field[i],
  1190. rep->field[i]->usage + j);
  1191. }
  1192. }
  1193. }
  1194. static struct hid_input *hidinput_allocate(struct hid_device *hid)
  1195. {
  1196. struct hid_input *hidinput = kzalloc(sizeof(*hidinput), GFP_KERNEL);
  1197. struct input_dev *input_dev = input_allocate_device();
  1198. if (!hidinput || !input_dev) {
  1199. kfree(hidinput);
  1200. input_free_device(input_dev);
  1201. hid_err(hid, "Out of memory during hid input probe\n");
  1202. return NULL;
  1203. }
  1204. input_set_drvdata(input_dev, hid);
  1205. input_dev->event = hidinput_input_event;
  1206. input_dev->open = hidinput_open;
  1207. input_dev->close = hidinput_close;
  1208. input_dev->setkeycode = hidinput_setkeycode;
  1209. input_dev->getkeycode = hidinput_getkeycode;
  1210. input_dev->name = hid->name;
  1211. input_dev->phys = hid->phys;
  1212. input_dev->uniq = hid->uniq;
  1213. input_dev->id.bustype = hid->bus;
  1214. input_dev->id.vendor = hid->vendor;
  1215. input_dev->id.product = hid->product;
  1216. input_dev->id.version = hid->version;
  1217. input_dev->dev.parent = &hid->dev;
  1218. hidinput->input = input_dev;
  1219. list_add_tail(&hidinput->list, &hid->inputs);
  1220. return hidinput;
  1221. }
  1222. static bool hidinput_has_been_populated(struct hid_input *hidinput)
  1223. {
  1224. int i;
  1225. unsigned long r = 0;
  1226. for (i = 0; i < BITS_TO_LONGS(EV_CNT); i++)
  1227. r |= hidinput->input->evbit[i];
  1228. for (i = 0; i < BITS_TO_LONGS(KEY_CNT); i++)
  1229. r |= hidinput->input->keybit[i];
  1230. for (i = 0; i < BITS_TO_LONGS(REL_CNT); i++)
  1231. r |= hidinput->input->relbit[i];
  1232. for (i = 0; i < BITS_TO_LONGS(ABS_CNT); i++)
  1233. r |= hidinput->input->absbit[i];
  1234. for (i = 0; i < BITS_TO_LONGS(MSC_CNT); i++)
  1235. r |= hidinput->input->mscbit[i];
  1236. for (i = 0; i < BITS_TO_LONGS(LED_CNT); i++)
  1237. r |= hidinput->input->ledbit[i];
  1238. for (i = 0; i < BITS_TO_LONGS(SND_CNT); i++)
  1239. r |= hidinput->input->sndbit[i];
  1240. for (i = 0; i < BITS_TO_LONGS(FF_CNT); i++)
  1241. r |= hidinput->input->ffbit[i];
  1242. for (i = 0; i < BITS_TO_LONGS(SW_CNT); i++)
  1243. r |= hidinput->input->swbit[i];
  1244. return !!r;
  1245. }
  1246. static void hidinput_cleanup_hidinput(struct hid_device *hid,
  1247. struct hid_input *hidinput)
  1248. {
  1249. struct hid_report *report;
  1250. int i, k;
  1251. list_del(&hidinput->list);
  1252. input_free_device(hidinput->input);
  1253. for (k = HID_INPUT_REPORT; k <= HID_OUTPUT_REPORT; k++) {
  1254. if (k == HID_OUTPUT_REPORT &&
  1255. hid->quirks & HID_QUIRK_SKIP_OUTPUT_REPORTS)
  1256. continue;
  1257. list_for_each_entry(report, &hid->report_enum[k].report_list,
  1258. list) {
  1259. for (i = 0; i < report->maxfield; i++)
  1260. if (report->field[i]->hidinput == hidinput)
  1261. report->field[i]->hidinput = NULL;
  1262. }
  1263. }
  1264. kfree(hidinput);
  1265. }
  1266. /*
  1267. * Register the input device; print a message.
  1268. * Configure the input layer interface
  1269. * Read all reports and initialize the absolute field values.
  1270. */
  1271. int hidinput_connect(struct hid_device *hid, unsigned int force)
  1272. {
  1273. struct hid_driver *drv = hid->driver;
  1274. struct hid_report *report;
  1275. struct hid_input *hidinput = NULL;
  1276. int i, j, k;
  1277. INIT_LIST_HEAD(&hid->inputs);
  1278. INIT_WORK(&hid->led_work, hidinput_led_worker);
  1279. if (!force) {
  1280. for (i = 0; i < hid->maxcollection; i++) {
  1281. struct hid_collection *col = &hid->collection[i];
  1282. if (col->type == HID_COLLECTION_APPLICATION ||
  1283. col->type == HID_COLLECTION_PHYSICAL)
  1284. if (IS_INPUT_APPLICATION(col->usage))
  1285. break;
  1286. }
  1287. if (i == hid->maxcollection)
  1288. return -1;
  1289. }
  1290. report_features(hid);
  1291. for (k = HID_INPUT_REPORT; k <= HID_OUTPUT_REPORT; k++) {
  1292. if (k == HID_OUTPUT_REPORT &&
  1293. hid->quirks & HID_QUIRK_SKIP_OUTPUT_REPORTS)
  1294. continue;
  1295. list_for_each_entry(report, &hid->report_enum[k].report_list, list) {
  1296. if (!report->maxfield)
  1297. continue;
  1298. if (!hidinput) {
  1299. hidinput = hidinput_allocate(hid);
  1300. if (!hidinput)
  1301. goto out_unwind;
  1302. }
  1303. for (i = 0; i < report->maxfield; i++)
  1304. for (j = 0; j < report->field[i]->maxusage; j++)
  1305. hidinput_configure_usage(hidinput, report->field[i],
  1306. report->field[i]->usage + j);
  1307. if ((hid->quirks & HID_QUIRK_NO_EMPTY_INPUT) &&
  1308. !hidinput_has_been_populated(hidinput))
  1309. continue;
  1310. if (hid->quirks & HID_QUIRK_MULTI_INPUT) {
  1311. /* This will leave hidinput NULL, so that it
  1312. * allocates another one if we have more inputs on
  1313. * the same interface. Some devices (e.g. Happ's
  1314. * UGCI) cram a lot of unrelated inputs into the
  1315. * same interface. */
  1316. hidinput->report = report;
  1317. if (drv->input_configured &&
  1318. drv->input_configured(hid, hidinput))
  1319. goto out_cleanup;
  1320. if (input_register_device(hidinput->input))
  1321. goto out_cleanup;
  1322. hidinput = NULL;
  1323. }
  1324. }
  1325. }
  1326. if (hidinput && (hid->quirks & HID_QUIRK_NO_EMPTY_INPUT) &&
  1327. !hidinput_has_been_populated(hidinput)) {
  1328. /* no need to register an input device not populated */
  1329. hidinput_cleanup_hidinput(hid, hidinput);
  1330. hidinput = NULL;
  1331. }
  1332. if (list_empty(&hid->inputs)) {
  1333. hid_err(hid, "No inputs registered, leaving\n");
  1334. goto out_unwind;
  1335. }
  1336. if (hidinput) {
  1337. if (drv->input_configured &&
  1338. drv->input_configured(hid, hidinput))
  1339. goto out_cleanup;
  1340. if (input_register_device(hidinput->input))
  1341. goto out_cleanup;
  1342. }
  1343. return 0;
  1344. out_cleanup:
  1345. list_del(&hidinput->list);
  1346. input_free_device(hidinput->input);
  1347. kfree(hidinput);
  1348. out_unwind:
  1349. /* unwind the ones we already registered */
  1350. hidinput_disconnect(hid);
  1351. return -1;
  1352. }
  1353. EXPORT_SYMBOL_GPL(hidinput_connect);
  1354. void hidinput_disconnect(struct hid_device *hid)
  1355. {
  1356. struct hid_input *hidinput, *next;
  1357. hidinput_cleanup_battery(hid);
  1358. list_for_each_entry_safe(hidinput, next, &hid->inputs, list) {
  1359. list_del(&hidinput->list);
  1360. input_unregister_device(hidinput->input);
  1361. kfree(hidinput);
  1362. }
  1363. /* led_work is spawned by input_dev callbacks, but doesn't access the
  1364. * parent input_dev at all. Once all input devices are removed, we
  1365. * know that led_work will never get restarted, so we can cancel it
  1366. * synchronously and are safe. */
  1367. cancel_work_sync(&hid->led_work);
  1368. }
  1369. EXPORT_SYMBOL_GPL(hidinput_disconnect);