hid-input.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241
  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. static __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. if (code == ABS_X || code == ABS_Y || code == ABS_Z) {
  198. if (field->unit == 0x11) { /* If centimeters */
  199. /* Convert to millimeters */
  200. unit_exponent += 1;
  201. } else if (field->unit == 0x13) { /* If inches */
  202. /* Convert to millimeters */
  203. prev = physical_extents;
  204. physical_extents *= 254;
  205. if (physical_extents < prev)
  206. return 0;
  207. unit_exponent -= 1;
  208. } else {
  209. return 0;
  210. }
  211. } else if (code == ABS_RX || code == ABS_RY || code == ABS_RZ) {
  212. if (field->unit == 0x14) { /* If degrees */
  213. /* Convert to radians */
  214. prev = logical_extents;
  215. logical_extents *= 573;
  216. if (logical_extents < prev)
  217. return 0;
  218. unit_exponent += 1;
  219. } else if (field->unit != 0x12) { /* If not radians */
  220. return 0;
  221. }
  222. } else {
  223. return 0;
  224. }
  225. /* Apply negative unit exponent */
  226. for (; unit_exponent < 0; unit_exponent++) {
  227. prev = logical_extents;
  228. logical_extents *= 10;
  229. if (logical_extents < prev)
  230. return 0;
  231. }
  232. /* Apply positive unit exponent */
  233. for (; unit_exponent > 0; unit_exponent--) {
  234. prev = physical_extents;
  235. physical_extents *= 10;
  236. if (physical_extents < prev)
  237. return 0;
  238. }
  239. /* Calculate resolution */
  240. return logical_extents / physical_extents;
  241. }
  242. #ifdef CONFIG_HID_BATTERY_STRENGTH
  243. static enum power_supply_property hidinput_battery_props[] = {
  244. POWER_SUPPLY_PROP_PRESENT,
  245. POWER_SUPPLY_PROP_ONLINE,
  246. POWER_SUPPLY_PROP_CAPACITY,
  247. POWER_SUPPLY_PROP_MODEL_NAME,
  248. POWER_SUPPLY_PROP_STATUS
  249. };
  250. #define HID_BATTERY_QUIRK_PERCENT (1 << 0) /* always reports percent */
  251. #define HID_BATTERY_QUIRK_FEATURE (1 << 1) /* ask for feature report */
  252. static const struct hid_device_id hid_battery_quirks[] = {
  253. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE,
  254. USB_DEVICE_ID_APPLE_ALU_WIRELESS_2011_ANSI),
  255. HID_BATTERY_QUIRK_PERCENT | HID_BATTERY_QUIRK_FEATURE },
  256. {}
  257. };
  258. static unsigned find_battery_quirk(struct hid_device *hdev)
  259. {
  260. unsigned quirks = 0;
  261. const struct hid_device_id *match;
  262. match = hid_match_id(hdev, hid_battery_quirks);
  263. if (match != NULL)
  264. quirks = match->driver_data;
  265. return quirks;
  266. }
  267. static int hidinput_get_battery_property(struct power_supply *psy,
  268. enum power_supply_property prop,
  269. union power_supply_propval *val)
  270. {
  271. struct hid_device *dev = container_of(psy, struct hid_device, battery);
  272. int ret = 0;
  273. __u8 buf[2] = {};
  274. switch (prop) {
  275. case POWER_SUPPLY_PROP_PRESENT:
  276. case POWER_SUPPLY_PROP_ONLINE:
  277. val->intval = 1;
  278. break;
  279. case POWER_SUPPLY_PROP_CAPACITY:
  280. ret = dev->hid_get_raw_report(dev, dev->battery_report_id,
  281. buf, sizeof(buf),
  282. dev->battery_report_type);
  283. if (ret != 2) {
  284. if (ret >= 0)
  285. ret = -EINVAL;
  286. break;
  287. }
  288. if (dev->battery_min < dev->battery_max &&
  289. buf[1] >= dev->battery_min &&
  290. buf[1] <= dev->battery_max)
  291. val->intval = (100 * (buf[1] - dev->battery_min)) /
  292. (dev->battery_max - dev->battery_min);
  293. break;
  294. case POWER_SUPPLY_PROP_MODEL_NAME:
  295. val->strval = dev->name;
  296. break;
  297. case POWER_SUPPLY_PROP_STATUS:
  298. val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
  299. break;
  300. default:
  301. ret = -EINVAL;
  302. break;
  303. }
  304. return ret;
  305. }
  306. static bool hidinput_setup_battery(struct hid_device *dev, unsigned report_type, struct hid_field *field)
  307. {
  308. struct power_supply *battery = &dev->battery;
  309. int ret;
  310. unsigned quirks;
  311. s32 min, max;
  312. if (field->usage->hid != HID_DC_BATTERYSTRENGTH)
  313. return false; /* no match */
  314. if (battery->name != NULL)
  315. goto out; /* already initialized? */
  316. battery->name = kasprintf(GFP_KERNEL, "hid-%s-battery", dev->uniq);
  317. if (battery->name == NULL)
  318. goto out;
  319. battery->type = POWER_SUPPLY_TYPE_BATTERY;
  320. battery->properties = hidinput_battery_props;
  321. battery->num_properties = ARRAY_SIZE(hidinput_battery_props);
  322. battery->use_for_apm = 0;
  323. battery->get_property = hidinput_get_battery_property;
  324. quirks = find_battery_quirk(dev);
  325. hid_dbg(dev, "device %x:%x:%x %d quirks %d\n",
  326. dev->bus, dev->vendor, dev->product, dev->version, quirks);
  327. min = field->logical_minimum;
  328. max = field->logical_maximum;
  329. if (quirks & HID_BATTERY_QUIRK_PERCENT) {
  330. min = 0;
  331. max = 100;
  332. }
  333. if (quirks & HID_BATTERY_QUIRK_FEATURE)
  334. report_type = HID_FEATURE_REPORT;
  335. dev->battery_min = min;
  336. dev->battery_max = max;
  337. dev->battery_report_type = report_type;
  338. dev->battery_report_id = field->report->id;
  339. ret = power_supply_register(&dev->dev, battery);
  340. if (ret != 0) {
  341. hid_warn(dev, "can't register power supply: %d\n", ret);
  342. kfree(battery->name);
  343. battery->name = NULL;
  344. }
  345. out:
  346. return true;
  347. }
  348. static void hidinput_cleanup_battery(struct hid_device *dev)
  349. {
  350. if (!dev->battery.name)
  351. return;
  352. power_supply_unregister(&dev->battery);
  353. kfree(dev->battery.name);
  354. dev->battery.name = NULL;
  355. }
  356. #else /* !CONFIG_HID_BATTERY_STRENGTH */
  357. static bool hidinput_setup_battery(struct hid_device *dev, unsigned report_type,
  358. struct hid_field *field)
  359. {
  360. return false;
  361. }
  362. static void hidinput_cleanup_battery(struct hid_device *dev)
  363. {
  364. }
  365. #endif /* CONFIG_HID_BATTERY_STRENGTH */
  366. static void hidinput_configure_usage(struct hid_input *hidinput, struct hid_field *field,
  367. struct hid_usage *usage)
  368. {
  369. struct input_dev *input = hidinput->input;
  370. struct hid_device *device = input_get_drvdata(input);
  371. int max = 0, code;
  372. unsigned long *bit = NULL;
  373. field->hidinput = hidinput;
  374. if (field->flags & HID_MAIN_ITEM_CONSTANT)
  375. goto ignore;
  376. /* only LED usages are supported in output fields */
  377. if (field->report_type == HID_OUTPUT_REPORT &&
  378. (usage->hid & HID_USAGE_PAGE) != HID_UP_LED) {
  379. goto ignore;
  380. }
  381. if (device->driver->input_mapping) {
  382. int ret = device->driver->input_mapping(device, hidinput, field,
  383. usage, &bit, &max);
  384. if (ret > 0)
  385. goto mapped;
  386. if (ret < 0)
  387. goto ignore;
  388. }
  389. switch (usage->hid & HID_USAGE_PAGE) {
  390. case HID_UP_UNDEFINED:
  391. goto ignore;
  392. case HID_UP_KEYBOARD:
  393. set_bit(EV_REP, input->evbit);
  394. if ((usage->hid & HID_USAGE) < 256) {
  395. if (!hid_keyboard[usage->hid & HID_USAGE]) goto ignore;
  396. map_key_clear(hid_keyboard[usage->hid & HID_USAGE]);
  397. } else
  398. map_key(KEY_UNKNOWN);
  399. break;
  400. case HID_UP_BUTTON:
  401. code = ((usage->hid - 1) & HID_USAGE);
  402. switch (field->application) {
  403. case HID_GD_MOUSE:
  404. case HID_GD_POINTER: code += BTN_MOUSE; break;
  405. case HID_GD_JOYSTICK:
  406. if (code <= 0xf)
  407. code += BTN_JOYSTICK;
  408. else
  409. code += BTN_TRIGGER_HAPPY;
  410. break;
  411. case HID_GD_GAMEPAD: code += BTN_GAMEPAD; break;
  412. default:
  413. switch (field->physical) {
  414. case HID_GD_MOUSE:
  415. case HID_GD_POINTER: code += BTN_MOUSE; break;
  416. case HID_GD_JOYSTICK: code += BTN_JOYSTICK; break;
  417. case HID_GD_GAMEPAD: code += BTN_GAMEPAD; break;
  418. default: code += BTN_MISC;
  419. }
  420. }
  421. map_key(code);
  422. break;
  423. case HID_UP_SIMULATION:
  424. switch (usage->hid & 0xffff) {
  425. case 0xba: map_abs(ABS_RUDDER); break;
  426. case 0xbb: map_abs(ABS_THROTTLE); break;
  427. case 0xc4: map_abs(ABS_GAS); break;
  428. case 0xc5: map_abs(ABS_BRAKE); break;
  429. case 0xc8: map_abs(ABS_WHEEL); break;
  430. default: goto ignore;
  431. }
  432. break;
  433. case HID_UP_GENDESK:
  434. if ((usage->hid & 0xf0) == 0x80) { /* SystemControl */
  435. switch (usage->hid & 0xf) {
  436. case 0x1: map_key_clear(KEY_POWER); break;
  437. case 0x2: map_key_clear(KEY_SLEEP); break;
  438. case 0x3: map_key_clear(KEY_WAKEUP); break;
  439. case 0x4: map_key_clear(KEY_CONTEXT_MENU); break;
  440. case 0x5: map_key_clear(KEY_MENU); break;
  441. case 0x6: map_key_clear(KEY_PROG1); break;
  442. case 0x7: map_key_clear(KEY_HELP); break;
  443. case 0x8: map_key_clear(KEY_EXIT); break;
  444. case 0x9: map_key_clear(KEY_SELECT); break;
  445. case 0xa: map_key_clear(KEY_RIGHT); break;
  446. case 0xb: map_key_clear(KEY_LEFT); break;
  447. case 0xc: map_key_clear(KEY_UP); break;
  448. case 0xd: map_key_clear(KEY_DOWN); break;
  449. case 0xe: map_key_clear(KEY_POWER2); break;
  450. case 0xf: map_key_clear(KEY_RESTART); break;
  451. default: goto unknown;
  452. }
  453. break;
  454. }
  455. if ((usage->hid & 0xf0) == 0x90) { /* D-pad */
  456. switch (usage->hid) {
  457. case HID_GD_UP: usage->hat_dir = 1; break;
  458. case HID_GD_DOWN: usage->hat_dir = 5; break;
  459. case HID_GD_RIGHT: usage->hat_dir = 3; break;
  460. case HID_GD_LEFT: usage->hat_dir = 7; break;
  461. default: goto unknown;
  462. }
  463. if (field->dpad) {
  464. map_abs(field->dpad);
  465. goto ignore;
  466. }
  467. map_abs(ABS_HAT0X);
  468. break;
  469. }
  470. switch (usage->hid) {
  471. /* These usage IDs map directly to the usage codes. */
  472. case HID_GD_X: case HID_GD_Y: case HID_GD_Z:
  473. case HID_GD_RX: case HID_GD_RY: case HID_GD_RZ:
  474. case HID_GD_SLIDER: case HID_GD_DIAL: case HID_GD_WHEEL:
  475. if (field->flags & HID_MAIN_ITEM_RELATIVE)
  476. map_rel(usage->hid & 0xf);
  477. else
  478. map_abs(usage->hid & 0xf);
  479. break;
  480. case HID_GD_HATSWITCH:
  481. usage->hat_min = field->logical_minimum;
  482. usage->hat_max = field->logical_maximum;
  483. map_abs(ABS_HAT0X);
  484. break;
  485. case HID_GD_START: map_key_clear(BTN_START); break;
  486. case HID_GD_SELECT: map_key_clear(BTN_SELECT); break;
  487. default: goto unknown;
  488. }
  489. break;
  490. case HID_UP_LED:
  491. switch (usage->hid & 0xffff) { /* HID-Value: */
  492. case 0x01: map_led (LED_NUML); break; /* "Num Lock" */
  493. case 0x02: map_led (LED_CAPSL); break; /* "Caps Lock" */
  494. case 0x03: map_led (LED_SCROLLL); break; /* "Scroll Lock" */
  495. case 0x04: map_led (LED_COMPOSE); break; /* "Compose" */
  496. case 0x05: map_led (LED_KANA); break; /* "Kana" */
  497. case 0x27: map_led (LED_SLEEP); break; /* "Stand-By" */
  498. case 0x4c: map_led (LED_SUSPEND); break; /* "System Suspend" */
  499. case 0x09: map_led (LED_MUTE); break; /* "Mute" */
  500. case 0x4b: map_led (LED_MISC); break; /* "Generic Indicator" */
  501. case 0x19: map_led (LED_MAIL); break; /* "Message Waiting" */
  502. case 0x4d: map_led (LED_CHARGING); break; /* "External Power Connected" */
  503. default: goto ignore;
  504. }
  505. break;
  506. case HID_UP_DIGITIZER:
  507. switch (usage->hid & 0xff) {
  508. case 0x00: /* Undefined */
  509. goto ignore;
  510. case 0x30: /* TipPressure */
  511. if (!test_bit(BTN_TOUCH, input->keybit)) {
  512. device->quirks |= HID_QUIRK_NOTOUCH;
  513. set_bit(EV_KEY, input->evbit);
  514. set_bit(BTN_TOUCH, input->keybit);
  515. }
  516. map_abs_clear(ABS_PRESSURE);
  517. break;
  518. case 0x32: /* InRange */
  519. switch (field->physical & 0xff) {
  520. case 0x21: map_key(BTN_TOOL_MOUSE); break;
  521. case 0x22: map_key(BTN_TOOL_FINGER); break;
  522. default: map_key(BTN_TOOL_PEN); break;
  523. }
  524. break;
  525. case 0x3c: /* Invert */
  526. map_key_clear(BTN_TOOL_RUBBER);
  527. break;
  528. case 0x33: /* Touch */
  529. case 0x42: /* TipSwitch */
  530. case 0x43: /* TipSwitch2 */
  531. device->quirks &= ~HID_QUIRK_NOTOUCH;
  532. map_key_clear(BTN_TOUCH);
  533. break;
  534. case 0x44: /* BarrelSwitch */
  535. map_key_clear(BTN_STYLUS);
  536. break;
  537. case 0x46: /* TabletPick */
  538. map_key_clear(BTN_STYLUS2);
  539. break;
  540. case 0x51: /* ContactID */
  541. device->quirks |= HID_QUIRK_MULTITOUCH;
  542. goto unknown;
  543. default: goto unknown;
  544. }
  545. break;
  546. case HID_UP_CONSUMER: /* USB HUT v1.12, pages 75-84 */
  547. switch (usage->hid & HID_USAGE) {
  548. case 0x000: goto ignore;
  549. case 0x030: map_key_clear(KEY_POWER); break;
  550. case 0x031: map_key_clear(KEY_RESTART); break;
  551. case 0x032: map_key_clear(KEY_SLEEP); break;
  552. case 0x034: map_key_clear(KEY_SLEEP); break;
  553. case 0x035: map_key_clear(KEY_KBDILLUMTOGGLE); break;
  554. case 0x036: map_key_clear(BTN_MISC); break;
  555. case 0x040: map_key_clear(KEY_MENU); break; /* Menu */
  556. case 0x041: map_key_clear(KEY_SELECT); break; /* Menu Pick */
  557. case 0x042: map_key_clear(KEY_UP); break; /* Menu Up */
  558. case 0x043: map_key_clear(KEY_DOWN); break; /* Menu Down */
  559. case 0x044: map_key_clear(KEY_LEFT); break; /* Menu Left */
  560. case 0x045: map_key_clear(KEY_RIGHT); break; /* Menu Right */
  561. case 0x046: map_key_clear(KEY_ESC); break; /* Menu Escape */
  562. case 0x047: map_key_clear(KEY_KPPLUS); break; /* Menu Value Increase */
  563. case 0x048: map_key_clear(KEY_KPMINUS); break; /* Menu Value Decrease */
  564. case 0x060: map_key_clear(KEY_INFO); break; /* Data On Screen */
  565. case 0x061: map_key_clear(KEY_SUBTITLE); break; /* Closed Caption */
  566. case 0x063: map_key_clear(KEY_VCR); break; /* VCR/TV */
  567. case 0x065: map_key_clear(KEY_CAMERA); break; /* Snapshot */
  568. case 0x069: map_key_clear(KEY_RED); break;
  569. case 0x06a: map_key_clear(KEY_GREEN); break;
  570. case 0x06b: map_key_clear(KEY_BLUE); break;
  571. case 0x06c: map_key_clear(KEY_YELLOW); break;
  572. case 0x06d: map_key_clear(KEY_ZOOM); break;
  573. case 0x082: map_key_clear(KEY_VIDEO_NEXT); break;
  574. case 0x083: map_key_clear(KEY_LAST); break;
  575. case 0x084: map_key_clear(KEY_ENTER); break;
  576. case 0x088: map_key_clear(KEY_PC); break;
  577. case 0x089: map_key_clear(KEY_TV); break;
  578. case 0x08a: map_key_clear(KEY_WWW); break;
  579. case 0x08b: map_key_clear(KEY_DVD); break;
  580. case 0x08c: map_key_clear(KEY_PHONE); break;
  581. case 0x08d: map_key_clear(KEY_PROGRAM); break;
  582. case 0x08e: map_key_clear(KEY_VIDEOPHONE); break;
  583. case 0x08f: map_key_clear(KEY_GAMES); break;
  584. case 0x090: map_key_clear(KEY_MEMO); break;
  585. case 0x091: map_key_clear(KEY_CD); break;
  586. case 0x092: map_key_clear(KEY_VCR); break;
  587. case 0x093: map_key_clear(KEY_TUNER); break;
  588. case 0x094: map_key_clear(KEY_EXIT); break;
  589. case 0x095: map_key_clear(KEY_HELP); break;
  590. case 0x096: map_key_clear(KEY_TAPE); break;
  591. case 0x097: map_key_clear(KEY_TV2); break;
  592. case 0x098: map_key_clear(KEY_SAT); break;
  593. case 0x09a: map_key_clear(KEY_PVR); break;
  594. case 0x09c: map_key_clear(KEY_CHANNELUP); break;
  595. case 0x09d: map_key_clear(KEY_CHANNELDOWN); break;
  596. case 0x0a0: map_key_clear(KEY_VCR2); break;
  597. case 0x0b0: map_key_clear(KEY_PLAY); break;
  598. case 0x0b1: map_key_clear(KEY_PAUSE); break;
  599. case 0x0b2: map_key_clear(KEY_RECORD); break;
  600. case 0x0b3: map_key_clear(KEY_FASTFORWARD); break;
  601. case 0x0b4: map_key_clear(KEY_REWIND); break;
  602. case 0x0b5: map_key_clear(KEY_NEXTSONG); break;
  603. case 0x0b6: map_key_clear(KEY_PREVIOUSSONG); break;
  604. case 0x0b7: map_key_clear(KEY_STOPCD); break;
  605. case 0x0b8: map_key_clear(KEY_EJECTCD); break;
  606. case 0x0bc: map_key_clear(KEY_MEDIA_REPEAT); break;
  607. case 0x0b9: map_key_clear(KEY_SHUFFLE); break;
  608. case 0x0bf: map_key_clear(KEY_SLOW); break;
  609. case 0x0cd: map_key_clear(KEY_PLAYPAUSE); break;
  610. case 0x0e0: map_abs_clear(ABS_VOLUME); break;
  611. case 0x0e2: map_key_clear(KEY_MUTE); break;
  612. case 0x0e5: map_key_clear(KEY_BASSBOOST); break;
  613. case 0x0e9: map_key_clear(KEY_VOLUMEUP); break;
  614. case 0x0ea: map_key_clear(KEY_VOLUMEDOWN); break;
  615. case 0x0f5: map_key_clear(KEY_SLOW); break;
  616. case 0x182: map_key_clear(KEY_BOOKMARKS); break;
  617. case 0x183: map_key_clear(KEY_CONFIG); break;
  618. case 0x184: map_key_clear(KEY_WORDPROCESSOR); break;
  619. case 0x185: map_key_clear(KEY_EDITOR); break;
  620. case 0x186: map_key_clear(KEY_SPREADSHEET); break;
  621. case 0x187: map_key_clear(KEY_GRAPHICSEDITOR); break;
  622. case 0x188: map_key_clear(KEY_PRESENTATION); break;
  623. case 0x189: map_key_clear(KEY_DATABASE); break;
  624. case 0x18a: map_key_clear(KEY_MAIL); break;
  625. case 0x18b: map_key_clear(KEY_NEWS); break;
  626. case 0x18c: map_key_clear(KEY_VOICEMAIL); break;
  627. case 0x18d: map_key_clear(KEY_ADDRESSBOOK); break;
  628. case 0x18e: map_key_clear(KEY_CALENDAR); break;
  629. case 0x191: map_key_clear(KEY_FINANCE); break;
  630. case 0x192: map_key_clear(KEY_CALC); break;
  631. case 0x193: map_key_clear(KEY_PLAYER); break;
  632. case 0x194: map_key_clear(KEY_FILE); break;
  633. case 0x196: map_key_clear(KEY_WWW); break;
  634. case 0x199: map_key_clear(KEY_CHAT); break;
  635. case 0x19c: map_key_clear(KEY_LOGOFF); break;
  636. case 0x19e: map_key_clear(KEY_COFFEE); break;
  637. case 0x1a6: map_key_clear(KEY_HELP); break;
  638. case 0x1a7: map_key_clear(KEY_DOCUMENTS); break;
  639. case 0x1ab: map_key_clear(KEY_SPELLCHECK); break;
  640. case 0x1ae: map_key_clear(KEY_KEYBOARD); break;
  641. case 0x1b6: map_key_clear(KEY_IMAGES); break;
  642. case 0x1b7: map_key_clear(KEY_AUDIO); break;
  643. case 0x1b8: map_key_clear(KEY_VIDEO); break;
  644. case 0x1bc: map_key_clear(KEY_MESSENGER); break;
  645. case 0x1bd: map_key_clear(KEY_INFO); break;
  646. case 0x201: map_key_clear(KEY_NEW); break;
  647. case 0x202: map_key_clear(KEY_OPEN); break;
  648. case 0x203: map_key_clear(KEY_CLOSE); break;
  649. case 0x204: map_key_clear(KEY_EXIT); break;
  650. case 0x207: map_key_clear(KEY_SAVE); break;
  651. case 0x208: map_key_clear(KEY_PRINT); break;
  652. case 0x209: map_key_clear(KEY_PROPS); break;
  653. case 0x21a: map_key_clear(KEY_UNDO); break;
  654. case 0x21b: map_key_clear(KEY_COPY); break;
  655. case 0x21c: map_key_clear(KEY_CUT); break;
  656. case 0x21d: map_key_clear(KEY_PASTE); break;
  657. case 0x21f: map_key_clear(KEY_FIND); break;
  658. case 0x221: map_key_clear(KEY_SEARCH); break;
  659. case 0x222: map_key_clear(KEY_GOTO); break;
  660. case 0x223: map_key_clear(KEY_HOMEPAGE); break;
  661. case 0x224: map_key_clear(KEY_BACK); break;
  662. case 0x225: map_key_clear(KEY_FORWARD); break;
  663. case 0x226: map_key_clear(KEY_STOP); break;
  664. case 0x227: map_key_clear(KEY_REFRESH); break;
  665. case 0x22a: map_key_clear(KEY_BOOKMARKS); break;
  666. case 0x22d: map_key_clear(KEY_ZOOMIN); break;
  667. case 0x22e: map_key_clear(KEY_ZOOMOUT); break;
  668. case 0x22f: map_key_clear(KEY_ZOOMRESET); break;
  669. case 0x233: map_key_clear(KEY_SCROLLUP); break;
  670. case 0x234: map_key_clear(KEY_SCROLLDOWN); break;
  671. case 0x238: map_rel(REL_HWHEEL); break;
  672. case 0x23d: map_key_clear(KEY_EDIT); break;
  673. case 0x25f: map_key_clear(KEY_CANCEL); break;
  674. case 0x269: map_key_clear(KEY_INSERT); break;
  675. case 0x26a: map_key_clear(KEY_DELETE); break;
  676. case 0x279: map_key_clear(KEY_REDO); break;
  677. case 0x289: map_key_clear(KEY_REPLY); break;
  678. case 0x28b: map_key_clear(KEY_FORWARDMAIL); break;
  679. case 0x28c: map_key_clear(KEY_SEND); break;
  680. default: goto ignore;
  681. }
  682. break;
  683. case HID_UP_GENDEVCTRLS:
  684. if (hidinput_setup_battery(device, HID_INPUT_REPORT, field))
  685. goto ignore;
  686. else
  687. goto unknown;
  688. break;
  689. case HID_UP_HPVENDOR: /* Reported on a Dutch layout HP5308 */
  690. set_bit(EV_REP, input->evbit);
  691. switch (usage->hid & HID_USAGE) {
  692. case 0x021: map_key_clear(KEY_PRINT); break;
  693. case 0x070: map_key_clear(KEY_HP); break;
  694. case 0x071: map_key_clear(KEY_CAMERA); break;
  695. case 0x072: map_key_clear(KEY_SOUND); break;
  696. case 0x073: map_key_clear(KEY_QUESTION); break;
  697. case 0x080: map_key_clear(KEY_EMAIL); break;
  698. case 0x081: map_key_clear(KEY_CHAT); break;
  699. case 0x082: map_key_clear(KEY_SEARCH); break;
  700. case 0x083: map_key_clear(KEY_CONNECT); break;
  701. case 0x084: map_key_clear(KEY_FINANCE); break;
  702. case 0x085: map_key_clear(KEY_SPORT); break;
  703. case 0x086: map_key_clear(KEY_SHOP); break;
  704. default: goto ignore;
  705. }
  706. break;
  707. case HID_UP_MSVENDOR:
  708. goto ignore;
  709. case HID_UP_CUSTOM: /* Reported on Logitech and Apple USB keyboards */
  710. set_bit(EV_REP, input->evbit);
  711. goto ignore;
  712. case HID_UP_LOGIVENDOR:
  713. goto ignore;
  714. case HID_UP_PID:
  715. switch (usage->hid & HID_USAGE) {
  716. case 0xa4: map_key_clear(BTN_DEAD); break;
  717. default: goto ignore;
  718. }
  719. break;
  720. default:
  721. unknown:
  722. if (field->report_size == 1) {
  723. if (field->report->type == HID_OUTPUT_REPORT) {
  724. map_led(LED_MISC);
  725. break;
  726. }
  727. map_key(BTN_MISC);
  728. break;
  729. }
  730. if (field->flags & HID_MAIN_ITEM_RELATIVE) {
  731. map_rel(REL_MISC);
  732. break;
  733. }
  734. map_abs(ABS_MISC);
  735. break;
  736. }
  737. mapped:
  738. if (device->driver->input_mapped && device->driver->input_mapped(device,
  739. hidinput, field, usage, &bit, &max) < 0)
  740. goto ignore;
  741. set_bit(usage->type, input->evbit);
  742. while (usage->code <= max && test_and_set_bit(usage->code, bit))
  743. usage->code = find_next_zero_bit(bit, max + 1, usage->code);
  744. if (usage->code > max)
  745. goto ignore;
  746. if (usage->type == EV_ABS) {
  747. int a = field->logical_minimum;
  748. int b = field->logical_maximum;
  749. if ((device->quirks & HID_QUIRK_BADPAD) && (usage->code == ABS_X || usage->code == ABS_Y)) {
  750. a = field->logical_minimum = 0;
  751. b = field->logical_maximum = 255;
  752. }
  753. if (field->application == HID_GD_GAMEPAD || field->application == HID_GD_JOYSTICK)
  754. input_set_abs_params(input, usage->code, a, b, (b - a) >> 8, (b - a) >> 4);
  755. else input_set_abs_params(input, usage->code, a, b, 0, 0);
  756. input_abs_set_res(input, usage->code,
  757. hidinput_calc_abs_res(field, usage->code));
  758. /* use a larger default input buffer for MT devices */
  759. if (usage->code == ABS_MT_POSITION_X && input->hint_events_per_packet == 0)
  760. input_set_events_per_packet(input, 60);
  761. }
  762. if (usage->type == EV_ABS &&
  763. (usage->hat_min < usage->hat_max || usage->hat_dir)) {
  764. int i;
  765. for (i = usage->code; i < usage->code + 2 && i <= max; i++) {
  766. input_set_abs_params(input, i, -1, 1, 0, 0);
  767. set_bit(i, input->absbit);
  768. }
  769. if (usage->hat_dir && !field->dpad)
  770. field->dpad = usage->code;
  771. }
  772. /* for those devices which produce Consumer volume usage as relative,
  773. * we emulate pressing volumeup/volumedown appropriate number of times
  774. * in hidinput_hid_event()
  775. */
  776. if ((usage->type == EV_ABS) && (field->flags & HID_MAIN_ITEM_RELATIVE) &&
  777. (usage->code == ABS_VOLUME)) {
  778. set_bit(KEY_VOLUMEUP, input->keybit);
  779. set_bit(KEY_VOLUMEDOWN, input->keybit);
  780. }
  781. if (usage->type == EV_KEY) {
  782. set_bit(EV_MSC, input->evbit);
  783. set_bit(MSC_SCAN, input->mscbit);
  784. }
  785. ignore:
  786. return;
  787. }
  788. void hidinput_hid_event(struct hid_device *hid, struct hid_field *field, struct hid_usage *usage, __s32 value)
  789. {
  790. struct input_dev *input;
  791. unsigned *quirks = &hid->quirks;
  792. if (!field->hidinput)
  793. return;
  794. input = field->hidinput->input;
  795. if (!usage->type)
  796. return;
  797. if (usage->hat_min < usage->hat_max || usage->hat_dir) {
  798. int hat_dir = usage->hat_dir;
  799. if (!hat_dir)
  800. hat_dir = (value - usage->hat_min) * 8 / (usage->hat_max - usage->hat_min + 1) + 1;
  801. if (hat_dir < 0 || hat_dir > 8) hat_dir = 0;
  802. input_event(input, usage->type, usage->code , hid_hat_to_axis[hat_dir].x);
  803. input_event(input, usage->type, usage->code + 1, hid_hat_to_axis[hat_dir].y);
  804. return;
  805. }
  806. if (usage->hid == (HID_UP_DIGITIZER | 0x003c)) { /* Invert */
  807. *quirks = value ? (*quirks | HID_QUIRK_INVERT) : (*quirks & ~HID_QUIRK_INVERT);
  808. return;
  809. }
  810. if (usage->hid == (HID_UP_DIGITIZER | 0x0032)) { /* InRange */
  811. if (value) {
  812. input_event(input, usage->type, (*quirks & HID_QUIRK_INVERT) ? BTN_TOOL_RUBBER : usage->code, 1);
  813. return;
  814. }
  815. input_event(input, usage->type, usage->code, 0);
  816. input_event(input, usage->type, BTN_TOOL_RUBBER, 0);
  817. return;
  818. }
  819. if (usage->hid == (HID_UP_DIGITIZER | 0x0030) && (*quirks & HID_QUIRK_NOTOUCH)) { /* Pressure */
  820. int a = field->logical_minimum;
  821. int b = field->logical_maximum;
  822. input_event(input, EV_KEY, BTN_TOUCH, value > a + ((b - a) >> 3));
  823. }
  824. if (usage->hid == (HID_UP_PID | 0x83UL)) { /* Simultaneous Effects Max */
  825. dbg_hid("Maximum Effects - %d\n",value);
  826. return;
  827. }
  828. if (usage->hid == (HID_UP_PID | 0x7fUL)) {
  829. dbg_hid("PID Pool Report\n");
  830. return;
  831. }
  832. if ((usage->type == EV_KEY) && (usage->code == 0)) /* Key 0 is "unassigned", not KEY_UNKNOWN */
  833. return;
  834. if ((usage->type == EV_ABS) && (field->flags & HID_MAIN_ITEM_RELATIVE) &&
  835. (usage->code == ABS_VOLUME)) {
  836. int count = abs(value);
  837. int direction = value > 0 ? KEY_VOLUMEUP : KEY_VOLUMEDOWN;
  838. int i;
  839. for (i = 0; i < count; i++) {
  840. input_event(input, EV_KEY, direction, 1);
  841. input_sync(input);
  842. input_event(input, EV_KEY, direction, 0);
  843. input_sync(input);
  844. }
  845. return;
  846. }
  847. /*
  848. * Ignore out-of-range values as per HID specification,
  849. * section 5.10 and 6.2.25
  850. */
  851. if ((field->flags & HID_MAIN_ITEM_VARIABLE) &&
  852. (value < field->logical_minimum ||
  853. value > field->logical_maximum)) {
  854. dbg_hid("Ignoring out-of-range value %x\n", value);
  855. return;
  856. }
  857. /* report the usage code as scancode if the key status has changed */
  858. if (usage->type == EV_KEY && !!test_bit(usage->code, input->key) != value)
  859. input_event(input, EV_MSC, MSC_SCAN, usage->hid);
  860. input_event(input, usage->type, usage->code, value);
  861. if ((field->flags & HID_MAIN_ITEM_RELATIVE) && (usage->type == EV_KEY))
  862. input_event(input, usage->type, usage->code, 0);
  863. }
  864. void hidinput_report_event(struct hid_device *hid, struct hid_report *report)
  865. {
  866. struct hid_input *hidinput;
  867. if (hid->quirks & HID_QUIRK_NO_INPUT_SYNC)
  868. return;
  869. list_for_each_entry(hidinput, &hid->inputs, list)
  870. input_sync(hidinput->input);
  871. }
  872. EXPORT_SYMBOL_GPL(hidinput_report_event);
  873. int hidinput_find_field(struct hid_device *hid, unsigned int type, unsigned int code, struct hid_field **field)
  874. {
  875. struct hid_report *report;
  876. int i, j;
  877. list_for_each_entry(report, &hid->report_enum[HID_OUTPUT_REPORT].report_list, list) {
  878. for (i = 0; i < report->maxfield; i++) {
  879. *field = report->field[i];
  880. for (j = 0; j < (*field)->maxusage; j++)
  881. if ((*field)->usage[j].type == type && (*field)->usage[j].code == code)
  882. return j;
  883. }
  884. }
  885. return -1;
  886. }
  887. EXPORT_SYMBOL_GPL(hidinput_find_field);
  888. struct hid_field *hidinput_get_led_field(struct hid_device *hid)
  889. {
  890. struct hid_report *report;
  891. struct hid_field *field;
  892. int i, j;
  893. list_for_each_entry(report,
  894. &hid->report_enum[HID_OUTPUT_REPORT].report_list,
  895. list) {
  896. for (i = 0; i < report->maxfield; i++) {
  897. field = report->field[i];
  898. for (j = 0; j < field->maxusage; j++)
  899. if (field->usage[j].type == EV_LED)
  900. return field;
  901. }
  902. }
  903. return NULL;
  904. }
  905. EXPORT_SYMBOL_GPL(hidinput_get_led_field);
  906. unsigned int hidinput_count_leds(struct hid_device *hid)
  907. {
  908. struct hid_report *report;
  909. struct hid_field *field;
  910. int i, j;
  911. unsigned int count = 0;
  912. list_for_each_entry(report,
  913. &hid->report_enum[HID_OUTPUT_REPORT].report_list,
  914. list) {
  915. for (i = 0; i < report->maxfield; i++) {
  916. field = report->field[i];
  917. for (j = 0; j < field->maxusage; j++)
  918. if (field->usage[j].type == EV_LED &&
  919. field->value[j])
  920. count += 1;
  921. }
  922. }
  923. return count;
  924. }
  925. EXPORT_SYMBOL_GPL(hidinput_count_leds);
  926. static int hidinput_open(struct input_dev *dev)
  927. {
  928. struct hid_device *hid = input_get_drvdata(dev);
  929. return hid_hw_open(hid);
  930. }
  931. static void hidinput_close(struct input_dev *dev)
  932. {
  933. struct hid_device *hid = input_get_drvdata(dev);
  934. hid_hw_close(hid);
  935. }
  936. static void report_features(struct hid_device *hid)
  937. {
  938. struct hid_driver *drv = hid->driver;
  939. struct hid_report_enum *rep_enum;
  940. struct hid_report *rep;
  941. int i, j;
  942. rep_enum = &hid->report_enum[HID_FEATURE_REPORT];
  943. list_for_each_entry(rep, &rep_enum->report_list, list)
  944. for (i = 0; i < rep->maxfield; i++)
  945. for (j = 0; j < rep->field[i]->maxusage; j++) {
  946. /* Verify if Battery Strength feature is available */
  947. hidinput_setup_battery(hid, HID_FEATURE_REPORT, rep->field[i]);
  948. if (drv->feature_mapping)
  949. drv->feature_mapping(hid, rep->field[i],
  950. rep->field[i]->usage + j);
  951. }
  952. }
  953. /*
  954. * Register the input device; print a message.
  955. * Configure the input layer interface
  956. * Read all reports and initialize the absolute field values.
  957. */
  958. int hidinput_connect(struct hid_device *hid, unsigned int force)
  959. {
  960. struct hid_report *report;
  961. struct hid_input *hidinput = NULL;
  962. struct input_dev *input_dev;
  963. int i, j, k;
  964. INIT_LIST_HEAD(&hid->inputs);
  965. if (!force) {
  966. for (i = 0; i < hid->maxcollection; i++) {
  967. struct hid_collection *col = &hid->collection[i];
  968. if (col->type == HID_COLLECTION_APPLICATION ||
  969. col->type == HID_COLLECTION_PHYSICAL)
  970. if (IS_INPUT_APPLICATION(col->usage))
  971. break;
  972. }
  973. if (i == hid->maxcollection)
  974. return -1;
  975. }
  976. report_features(hid);
  977. for (k = HID_INPUT_REPORT; k <= HID_OUTPUT_REPORT; k++) {
  978. if (k == HID_OUTPUT_REPORT &&
  979. hid->quirks & HID_QUIRK_SKIP_OUTPUT_REPORTS)
  980. continue;
  981. list_for_each_entry(report, &hid->report_enum[k].report_list, list) {
  982. if (!report->maxfield)
  983. continue;
  984. if (!hidinput) {
  985. hidinput = kzalloc(sizeof(*hidinput), GFP_KERNEL);
  986. input_dev = input_allocate_device();
  987. if (!hidinput || !input_dev) {
  988. kfree(hidinput);
  989. input_free_device(input_dev);
  990. hid_err(hid, "Out of memory during hid input probe\n");
  991. goto out_unwind;
  992. }
  993. input_set_drvdata(input_dev, hid);
  994. input_dev->event =
  995. hid->ll_driver->hidinput_input_event;
  996. input_dev->open = hidinput_open;
  997. input_dev->close = hidinput_close;
  998. input_dev->setkeycode = hidinput_setkeycode;
  999. input_dev->getkeycode = hidinput_getkeycode;
  1000. input_dev->name = hid->name;
  1001. input_dev->phys = hid->phys;
  1002. input_dev->uniq = hid->uniq;
  1003. input_dev->id.bustype = hid->bus;
  1004. input_dev->id.vendor = hid->vendor;
  1005. input_dev->id.product = hid->product;
  1006. input_dev->id.version = hid->version;
  1007. input_dev->dev.parent = hid->dev.parent;
  1008. hidinput->input = input_dev;
  1009. list_add_tail(&hidinput->list, &hid->inputs);
  1010. }
  1011. for (i = 0; i < report->maxfield; i++)
  1012. for (j = 0; j < report->field[i]->maxusage; j++)
  1013. hidinput_configure_usage(hidinput, report->field[i],
  1014. report->field[i]->usage + j);
  1015. if (hid->quirks & HID_QUIRK_MULTI_INPUT) {
  1016. /* This will leave hidinput NULL, so that it
  1017. * allocates another one if we have more inputs on
  1018. * the same interface. Some devices (e.g. Happ's
  1019. * UGCI) cram a lot of unrelated inputs into the
  1020. * same interface. */
  1021. hidinput->report = report;
  1022. if (input_register_device(hidinput->input))
  1023. goto out_cleanup;
  1024. hidinput = NULL;
  1025. }
  1026. }
  1027. }
  1028. if (hid->quirks & HID_QUIRK_MULTITOUCH) {
  1029. /* generic hid does not know how to handle multitouch devices */
  1030. if (hidinput)
  1031. goto out_cleanup;
  1032. goto out_unwind;
  1033. }
  1034. if (hidinput && input_register_device(hidinput->input))
  1035. goto out_cleanup;
  1036. return 0;
  1037. out_cleanup:
  1038. list_del(&hidinput->list);
  1039. input_free_device(hidinput->input);
  1040. kfree(hidinput);
  1041. out_unwind:
  1042. /* unwind the ones we already registered */
  1043. hidinput_disconnect(hid);
  1044. return -1;
  1045. }
  1046. EXPORT_SYMBOL_GPL(hidinput_connect);
  1047. void hidinput_disconnect(struct hid_device *hid)
  1048. {
  1049. struct hid_input *hidinput, *next;
  1050. hidinput_cleanup_battery(hid);
  1051. list_for_each_entry_safe(hidinput, next, &hid->inputs, list) {
  1052. list_del(&hidinput->list);
  1053. input_unregister_device(hidinput->input);
  1054. kfree(hidinput);
  1055. }
  1056. }
  1057. EXPORT_SYMBOL_GPL(hidinput_disconnect);