hid-core.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225
  1. /*
  2. * USB HID support for Linux
  3. *
  4. * Copyright (c) 1999 Andreas Gal
  5. * Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
  6. * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
  7. */
  8. /*
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the Free
  11. * Software Foundation; either version 2 of the License, or (at your option)
  12. * any later version.
  13. */
  14. #include <linux/module.h>
  15. #include <linux/slab.h>
  16. #include <linux/init.h>
  17. #include <linux/kernel.h>
  18. #include <linux/sched.h>
  19. #include <linux/list.h>
  20. #include <linux/mm.h>
  21. #include <linux/smp_lock.h>
  22. #include <linux/spinlock.h>
  23. #include <asm/unaligned.h>
  24. #include <asm/byteorder.h>
  25. #include <linux/input.h>
  26. #include <linux/wait.h>
  27. #undef DEBUG
  28. #undef DEBUG_DATA
  29. #include <linux/usb.h>
  30. #include "hid.h"
  31. #include <linux/hiddev.h>
  32. /*
  33. * Version Information
  34. */
  35. #define DRIVER_VERSION "v2.6"
  36. #define DRIVER_AUTHOR "Andreas Gal, Vojtech Pavlik"
  37. #define DRIVER_DESC "USB HID core driver"
  38. #define DRIVER_LICENSE "GPL"
  39. static char *hid_types[] = {"Device", "Pointer", "Mouse", "Device", "Joystick",
  40. "Gamepad", "Keyboard", "Keypad", "Multi-Axis Controller"};
  41. /*
  42. * Module parameters.
  43. */
  44. static unsigned int hid_mousepoll_interval;
  45. module_param_named(mousepoll, hid_mousepoll_interval, uint, 0644);
  46. MODULE_PARM_DESC(mousepoll, "Polling interval of mice");
  47. /*
  48. * Register a new report for a device.
  49. */
  50. static struct hid_report *hid_register_report(struct hid_device *device, unsigned type, unsigned id)
  51. {
  52. struct hid_report_enum *report_enum = device->report_enum + type;
  53. struct hid_report *report;
  54. if (report_enum->report_id_hash[id])
  55. return report_enum->report_id_hash[id];
  56. if (!(report = kzalloc(sizeof(struct hid_report), GFP_KERNEL)))
  57. return NULL;
  58. if (id != 0)
  59. report_enum->numbered = 1;
  60. report->id = id;
  61. report->type = type;
  62. report->size = 0;
  63. report->device = device;
  64. report_enum->report_id_hash[id] = report;
  65. list_add_tail(&report->list, &report_enum->report_list);
  66. return report;
  67. }
  68. /*
  69. * Register a new field for this report.
  70. */
  71. static struct hid_field *hid_register_field(struct hid_report *report, unsigned usages, unsigned values)
  72. {
  73. struct hid_field *field;
  74. if (report->maxfield == HID_MAX_FIELDS) {
  75. dbg("too many fields in report");
  76. return NULL;
  77. }
  78. if (!(field = kzalloc(sizeof(struct hid_field) + usages * sizeof(struct hid_usage)
  79. + values * sizeof(unsigned), GFP_KERNEL))) return NULL;
  80. field->index = report->maxfield++;
  81. report->field[field->index] = field;
  82. field->usage = (struct hid_usage *)(field + 1);
  83. field->value = (unsigned *)(field->usage + usages);
  84. field->report = report;
  85. return field;
  86. }
  87. /*
  88. * Open a collection. The type/usage is pushed on the stack.
  89. */
  90. static int open_collection(struct hid_parser *parser, unsigned type)
  91. {
  92. struct hid_collection *collection;
  93. unsigned usage;
  94. usage = parser->local.usage[0];
  95. if (parser->collection_stack_ptr == HID_COLLECTION_STACK_SIZE) {
  96. dbg("collection stack overflow");
  97. return -1;
  98. }
  99. if (parser->device->maxcollection == parser->device->collection_size) {
  100. collection = kmalloc(sizeof(struct hid_collection) *
  101. parser->device->collection_size * 2, GFP_KERNEL);
  102. if (collection == NULL) {
  103. dbg("failed to reallocate collection array");
  104. return -1;
  105. }
  106. memcpy(collection, parser->device->collection,
  107. sizeof(struct hid_collection) *
  108. parser->device->collection_size);
  109. memset(collection + parser->device->collection_size, 0,
  110. sizeof(struct hid_collection) *
  111. parser->device->collection_size);
  112. kfree(parser->device->collection);
  113. parser->device->collection = collection;
  114. parser->device->collection_size *= 2;
  115. }
  116. parser->collection_stack[parser->collection_stack_ptr++] =
  117. parser->device->maxcollection;
  118. collection = parser->device->collection +
  119. parser->device->maxcollection++;
  120. collection->type = type;
  121. collection->usage = usage;
  122. collection->level = parser->collection_stack_ptr - 1;
  123. if (type == HID_COLLECTION_APPLICATION)
  124. parser->device->maxapplication++;
  125. return 0;
  126. }
  127. /*
  128. * Close a collection.
  129. */
  130. static int close_collection(struct hid_parser *parser)
  131. {
  132. if (!parser->collection_stack_ptr) {
  133. dbg("collection stack underflow");
  134. return -1;
  135. }
  136. parser->collection_stack_ptr--;
  137. return 0;
  138. }
  139. /*
  140. * Climb up the stack, search for the specified collection type
  141. * and return the usage.
  142. */
  143. static unsigned hid_lookup_collection(struct hid_parser *parser, unsigned type)
  144. {
  145. int n;
  146. for (n = parser->collection_stack_ptr - 1; n >= 0; n--)
  147. if (parser->device->collection[parser->collection_stack[n]].type == type)
  148. return parser->device->collection[parser->collection_stack[n]].usage;
  149. return 0; /* we know nothing about this usage type */
  150. }
  151. /*
  152. * Add a usage to the temporary parser table.
  153. */
  154. static int hid_add_usage(struct hid_parser *parser, unsigned usage)
  155. {
  156. if (parser->local.usage_index >= HID_MAX_USAGES) {
  157. dbg("usage index exceeded");
  158. return -1;
  159. }
  160. parser->local.usage[parser->local.usage_index] = usage;
  161. parser->local.collection_index[parser->local.usage_index] =
  162. parser->collection_stack_ptr ?
  163. parser->collection_stack[parser->collection_stack_ptr - 1] : 0;
  164. parser->local.usage_index++;
  165. return 0;
  166. }
  167. /*
  168. * Register a new field for this report.
  169. */
  170. static int hid_add_field(struct hid_parser *parser, unsigned report_type, unsigned flags)
  171. {
  172. struct hid_report *report;
  173. struct hid_field *field;
  174. int usages;
  175. unsigned offset;
  176. int i;
  177. if (!(report = hid_register_report(parser->device, report_type, parser->global.report_id))) {
  178. dbg("hid_register_report failed");
  179. return -1;
  180. }
  181. if (parser->global.logical_maximum < parser->global.logical_minimum) {
  182. dbg("logical range invalid %d %d", parser->global.logical_minimum, parser->global.logical_maximum);
  183. return -1;
  184. }
  185. offset = report->size;
  186. report->size += parser->global.report_size * parser->global.report_count;
  187. if (!parser->local.usage_index) /* Ignore padding fields */
  188. return 0;
  189. usages = max_t(int, parser->local.usage_index, parser->global.report_count);
  190. if ((field = hid_register_field(report, usages, parser->global.report_count)) == NULL)
  191. return 0;
  192. field->physical = hid_lookup_collection(parser, HID_COLLECTION_PHYSICAL);
  193. field->logical = hid_lookup_collection(parser, HID_COLLECTION_LOGICAL);
  194. field->application = hid_lookup_collection(parser, HID_COLLECTION_APPLICATION);
  195. for (i = 0; i < usages; i++) {
  196. int j = i;
  197. /* Duplicate the last usage we parsed if we have excess values */
  198. if (i >= parser->local.usage_index)
  199. j = parser->local.usage_index - 1;
  200. field->usage[i].hid = parser->local.usage[j];
  201. field->usage[i].collection_index =
  202. parser->local.collection_index[j];
  203. }
  204. field->maxusage = usages;
  205. field->flags = flags;
  206. field->report_offset = offset;
  207. field->report_type = report_type;
  208. field->report_size = parser->global.report_size;
  209. field->report_count = parser->global.report_count;
  210. field->logical_minimum = parser->global.logical_minimum;
  211. field->logical_maximum = parser->global.logical_maximum;
  212. field->physical_minimum = parser->global.physical_minimum;
  213. field->physical_maximum = parser->global.physical_maximum;
  214. field->unit_exponent = parser->global.unit_exponent;
  215. field->unit = parser->global.unit;
  216. return 0;
  217. }
  218. /*
  219. * Read data value from item.
  220. */
  221. static __inline__ __u32 item_udata(struct hid_item *item)
  222. {
  223. switch (item->size) {
  224. case 1: return item->data.u8;
  225. case 2: return item->data.u16;
  226. case 4: return item->data.u32;
  227. }
  228. return 0;
  229. }
  230. static __inline__ __s32 item_sdata(struct hid_item *item)
  231. {
  232. switch (item->size) {
  233. case 1: return item->data.s8;
  234. case 2: return item->data.s16;
  235. case 4: return item->data.s32;
  236. }
  237. return 0;
  238. }
  239. /*
  240. * Process a global item.
  241. */
  242. static int hid_parser_global(struct hid_parser *parser, struct hid_item *item)
  243. {
  244. switch (item->tag) {
  245. case HID_GLOBAL_ITEM_TAG_PUSH:
  246. if (parser->global_stack_ptr == HID_GLOBAL_STACK_SIZE) {
  247. dbg("global enviroment stack overflow");
  248. return -1;
  249. }
  250. memcpy(parser->global_stack + parser->global_stack_ptr++,
  251. &parser->global, sizeof(struct hid_global));
  252. return 0;
  253. case HID_GLOBAL_ITEM_TAG_POP:
  254. if (!parser->global_stack_ptr) {
  255. dbg("global enviroment stack underflow");
  256. return -1;
  257. }
  258. memcpy(&parser->global, parser->global_stack + --parser->global_stack_ptr,
  259. sizeof(struct hid_global));
  260. return 0;
  261. case HID_GLOBAL_ITEM_TAG_USAGE_PAGE:
  262. parser->global.usage_page = item_udata(item);
  263. return 0;
  264. case HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM:
  265. parser->global.logical_minimum = item_sdata(item);
  266. return 0;
  267. case HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM:
  268. if (parser->global.logical_minimum < 0)
  269. parser->global.logical_maximum = item_sdata(item);
  270. else
  271. parser->global.logical_maximum = item_udata(item);
  272. return 0;
  273. case HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM:
  274. parser->global.physical_minimum = item_sdata(item);
  275. return 0;
  276. case HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM:
  277. if (parser->global.physical_minimum < 0)
  278. parser->global.physical_maximum = item_sdata(item);
  279. else
  280. parser->global.physical_maximum = item_udata(item);
  281. return 0;
  282. case HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT:
  283. parser->global.unit_exponent = item_sdata(item);
  284. return 0;
  285. case HID_GLOBAL_ITEM_TAG_UNIT:
  286. parser->global.unit = item_udata(item);
  287. return 0;
  288. case HID_GLOBAL_ITEM_TAG_REPORT_SIZE:
  289. if ((parser->global.report_size = item_udata(item)) > 32) {
  290. dbg("invalid report_size %d", parser->global.report_size);
  291. return -1;
  292. }
  293. return 0;
  294. case HID_GLOBAL_ITEM_TAG_REPORT_COUNT:
  295. if ((parser->global.report_count = item_udata(item)) > HID_MAX_USAGES) {
  296. dbg("invalid report_count %d", parser->global.report_count);
  297. return -1;
  298. }
  299. return 0;
  300. case HID_GLOBAL_ITEM_TAG_REPORT_ID:
  301. if ((parser->global.report_id = item_udata(item)) == 0) {
  302. dbg("report_id 0 is invalid");
  303. return -1;
  304. }
  305. return 0;
  306. default:
  307. dbg("unknown global tag 0x%x", item->tag);
  308. return -1;
  309. }
  310. }
  311. /*
  312. * Process a local item.
  313. */
  314. static int hid_parser_local(struct hid_parser *parser, struct hid_item *item)
  315. {
  316. __u32 data;
  317. unsigned n;
  318. if (item->size == 0) {
  319. dbg("item data expected for local item");
  320. return -1;
  321. }
  322. data = item_udata(item);
  323. switch (item->tag) {
  324. case HID_LOCAL_ITEM_TAG_DELIMITER:
  325. if (data) {
  326. /*
  327. * We treat items before the first delimiter
  328. * as global to all usage sets (branch 0).
  329. * In the moment we process only these global
  330. * items and the first delimiter set.
  331. */
  332. if (parser->local.delimiter_depth != 0) {
  333. dbg("nested delimiters");
  334. return -1;
  335. }
  336. parser->local.delimiter_depth++;
  337. parser->local.delimiter_branch++;
  338. } else {
  339. if (parser->local.delimiter_depth < 1) {
  340. dbg("bogus close delimiter");
  341. return -1;
  342. }
  343. parser->local.delimiter_depth--;
  344. }
  345. return 1;
  346. case HID_LOCAL_ITEM_TAG_USAGE:
  347. if (parser->local.delimiter_branch > 1) {
  348. dbg("alternative usage ignored");
  349. return 0;
  350. }
  351. if (item->size <= 2)
  352. data = (parser->global.usage_page << 16) + data;
  353. return hid_add_usage(parser, data);
  354. case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM:
  355. if (parser->local.delimiter_branch > 1) {
  356. dbg("alternative usage ignored");
  357. return 0;
  358. }
  359. if (item->size <= 2)
  360. data = (parser->global.usage_page << 16) + data;
  361. parser->local.usage_minimum = data;
  362. return 0;
  363. case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM:
  364. if (parser->local.delimiter_branch > 1) {
  365. dbg("alternative usage ignored");
  366. return 0;
  367. }
  368. if (item->size <= 2)
  369. data = (parser->global.usage_page << 16) + data;
  370. for (n = parser->local.usage_minimum; n <= data; n++)
  371. if (hid_add_usage(parser, n)) {
  372. dbg("hid_add_usage failed\n");
  373. return -1;
  374. }
  375. return 0;
  376. default:
  377. dbg("unknown local item tag 0x%x", item->tag);
  378. return 0;
  379. }
  380. return 0;
  381. }
  382. /*
  383. * Process a main item.
  384. */
  385. static int hid_parser_main(struct hid_parser *parser, struct hid_item *item)
  386. {
  387. __u32 data;
  388. int ret;
  389. data = item_udata(item);
  390. switch (item->tag) {
  391. case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION:
  392. ret = open_collection(parser, data & 0xff);
  393. break;
  394. case HID_MAIN_ITEM_TAG_END_COLLECTION:
  395. ret = close_collection(parser);
  396. break;
  397. case HID_MAIN_ITEM_TAG_INPUT:
  398. ret = hid_add_field(parser, HID_INPUT_REPORT, data);
  399. break;
  400. case HID_MAIN_ITEM_TAG_OUTPUT:
  401. ret = hid_add_field(parser, HID_OUTPUT_REPORT, data);
  402. break;
  403. case HID_MAIN_ITEM_TAG_FEATURE:
  404. ret = hid_add_field(parser, HID_FEATURE_REPORT, data);
  405. break;
  406. default:
  407. dbg("unknown main item tag 0x%x", item->tag);
  408. ret = 0;
  409. }
  410. memset(&parser->local, 0, sizeof(parser->local)); /* Reset the local parser environment */
  411. return ret;
  412. }
  413. /*
  414. * Process a reserved item.
  415. */
  416. static int hid_parser_reserved(struct hid_parser *parser, struct hid_item *item)
  417. {
  418. dbg("reserved item type, tag 0x%x", item->tag);
  419. return 0;
  420. }
  421. /*
  422. * Free a report and all registered fields. The field->usage and
  423. * field->value table's are allocated behind the field, so we need
  424. * only to free(field) itself.
  425. */
  426. static void hid_free_report(struct hid_report *report)
  427. {
  428. unsigned n;
  429. for (n = 0; n < report->maxfield; n++)
  430. kfree(report->field[n]);
  431. kfree(report);
  432. }
  433. /*
  434. * Free a device structure, all reports, and all fields.
  435. */
  436. static void hid_free_device(struct hid_device *device)
  437. {
  438. unsigned i,j;
  439. for (i = 0; i < HID_REPORT_TYPES; i++) {
  440. struct hid_report_enum *report_enum = device->report_enum + i;
  441. for (j = 0; j < 256; j++) {
  442. struct hid_report *report = report_enum->report_id_hash[j];
  443. if (report)
  444. hid_free_report(report);
  445. }
  446. }
  447. kfree(device->rdesc);
  448. kfree(device);
  449. }
  450. /*
  451. * Fetch a report description item from the data stream. We support long
  452. * items, though they are not used yet.
  453. */
  454. static u8 *fetch_item(__u8 *start, __u8 *end, struct hid_item *item)
  455. {
  456. u8 b;
  457. if ((end - start) <= 0)
  458. return NULL;
  459. b = *start++;
  460. item->type = (b >> 2) & 3;
  461. item->tag = (b >> 4) & 15;
  462. if (item->tag == HID_ITEM_TAG_LONG) {
  463. item->format = HID_ITEM_FORMAT_LONG;
  464. if ((end - start) < 2)
  465. return NULL;
  466. item->size = *start++;
  467. item->tag = *start++;
  468. if ((end - start) < item->size)
  469. return NULL;
  470. item->data.longdata = start;
  471. start += item->size;
  472. return start;
  473. }
  474. item->format = HID_ITEM_FORMAT_SHORT;
  475. item->size = b & 3;
  476. switch (item->size) {
  477. case 0:
  478. return start;
  479. case 1:
  480. if ((end - start) < 1)
  481. return NULL;
  482. item->data.u8 = *start++;
  483. return start;
  484. case 2:
  485. if ((end - start) < 2)
  486. return NULL;
  487. item->data.u16 = le16_to_cpu(get_unaligned((__le16*)start));
  488. start = (__u8 *)((__le16 *)start + 1);
  489. return start;
  490. case 3:
  491. item->size++;
  492. if ((end - start) < 4)
  493. return NULL;
  494. item->data.u32 = le32_to_cpu(get_unaligned((__le32*)start));
  495. start = (__u8 *)((__le32 *)start + 1);
  496. return start;
  497. }
  498. return NULL;
  499. }
  500. /*
  501. * Parse a report description into a hid_device structure. Reports are
  502. * enumerated, fields are attached to these reports.
  503. */
  504. static struct hid_device *hid_parse_report(__u8 *start, unsigned size)
  505. {
  506. struct hid_device *device;
  507. struct hid_parser *parser;
  508. struct hid_item item;
  509. __u8 *end;
  510. unsigned i;
  511. static int (*dispatch_type[])(struct hid_parser *parser,
  512. struct hid_item *item) = {
  513. hid_parser_main,
  514. hid_parser_global,
  515. hid_parser_local,
  516. hid_parser_reserved
  517. };
  518. if (!(device = kzalloc(sizeof(struct hid_device), GFP_KERNEL)))
  519. return NULL;
  520. if (!(device->collection = kzalloc(sizeof(struct hid_collection) *
  521. HID_DEFAULT_NUM_COLLECTIONS, GFP_KERNEL))) {
  522. kfree(device);
  523. return NULL;
  524. }
  525. device->collection_size = HID_DEFAULT_NUM_COLLECTIONS;
  526. for (i = 0; i < HID_REPORT_TYPES; i++)
  527. INIT_LIST_HEAD(&device->report_enum[i].report_list);
  528. if (!(device->rdesc = (__u8 *)kmalloc(size, GFP_KERNEL))) {
  529. kfree(device->collection);
  530. kfree(device);
  531. return NULL;
  532. }
  533. memcpy(device->rdesc, start, size);
  534. device->rsize = size;
  535. if (!(parser = kzalloc(sizeof(struct hid_parser), GFP_KERNEL))) {
  536. kfree(device->rdesc);
  537. kfree(device->collection);
  538. kfree(device);
  539. return NULL;
  540. }
  541. parser->device = device;
  542. end = start + size;
  543. while ((start = fetch_item(start, end, &item)) != NULL) {
  544. if (item.format != HID_ITEM_FORMAT_SHORT) {
  545. dbg("unexpected long global item");
  546. kfree(device->collection);
  547. hid_free_device(device);
  548. kfree(parser);
  549. return NULL;
  550. }
  551. if (dispatch_type[item.type](parser, &item)) {
  552. dbg("item %u %u %u %u parsing failed\n",
  553. item.format, (unsigned)item.size, (unsigned)item.type, (unsigned)item.tag);
  554. kfree(device->collection);
  555. hid_free_device(device);
  556. kfree(parser);
  557. return NULL;
  558. }
  559. if (start == end) {
  560. if (parser->collection_stack_ptr) {
  561. dbg("unbalanced collection at end of report description");
  562. kfree(device->collection);
  563. hid_free_device(device);
  564. kfree(parser);
  565. return NULL;
  566. }
  567. if (parser->local.delimiter_depth) {
  568. dbg("unbalanced delimiter at end of report description");
  569. kfree(device->collection);
  570. hid_free_device(device);
  571. kfree(parser);
  572. return NULL;
  573. }
  574. kfree(parser);
  575. return device;
  576. }
  577. }
  578. dbg("item fetching failed at offset %d\n", (int)(end - start));
  579. kfree(device->collection);
  580. hid_free_device(device);
  581. kfree(parser);
  582. return NULL;
  583. }
  584. /*
  585. * Convert a signed n-bit integer to signed 32-bit integer. Common
  586. * cases are done through the compiler, the screwed things has to be
  587. * done by hand.
  588. */
  589. static __inline__ __s32 snto32(__u32 value, unsigned n)
  590. {
  591. switch (n) {
  592. case 8: return ((__s8)value);
  593. case 16: return ((__s16)value);
  594. case 32: return ((__s32)value);
  595. }
  596. return value & (1 << (n - 1)) ? value | (-1 << n) : value;
  597. }
  598. /*
  599. * Convert a signed 32-bit integer to a signed n-bit integer.
  600. */
  601. static __inline__ __u32 s32ton(__s32 value, unsigned n)
  602. {
  603. __s32 a = value >> (n - 1);
  604. if (a && a != -1)
  605. return value < 0 ? 1 << (n - 1) : (1 << (n - 1)) - 1;
  606. return value & ((1 << n) - 1);
  607. }
  608. /*
  609. * Extract/implement a data field from/to a little endian report (bit array).
  610. */
  611. static __inline__ __u32 extract(__u8 *report, unsigned offset, unsigned n)
  612. {
  613. u32 x;
  614. report += offset >> 3; /* adjust byte index */
  615. offset &= 8 - 1;
  616. x = get_unaligned((u32 *) report);
  617. x = le32_to_cpu(x);
  618. x = (x >> offset) & ((1 << n) - 1);
  619. return x;
  620. }
  621. static __inline__ void implement(__u8 *report, unsigned offset, unsigned n, __u32 value)
  622. {
  623. u32 x;
  624. report += offset >> 3;
  625. offset &= 8 - 1;
  626. x = get_unaligned((u32 *)report);
  627. x &= cpu_to_le32(~((((__u32) 1 << n) - 1) << offset));
  628. x |= cpu_to_le32(value << offset);
  629. put_unaligned(x,(u32 *)report);
  630. }
  631. /*
  632. * Search an array for a value.
  633. */
  634. static __inline__ int search(__s32 *array, __s32 value, unsigned n)
  635. {
  636. while (n--) {
  637. if (*array++ == value)
  638. return 0;
  639. }
  640. return -1;
  641. }
  642. static void hid_process_event(struct hid_device *hid, struct hid_field *field, struct hid_usage *usage, __s32 value, int interrupt)
  643. {
  644. hid_dump_input(usage, value);
  645. if (hid->claimed & HID_CLAIMED_INPUT)
  646. hidinput_hid_event(hid, field, usage, value);
  647. if (hid->claimed & HID_CLAIMED_HIDDEV && interrupt)
  648. hiddev_hid_event(hid, field, usage, value);
  649. }
  650. /*
  651. * Analyse a received field, and fetch the data from it. The field
  652. * content is stored for next report processing (we do differential
  653. * reporting to the layer).
  654. */
  655. static void hid_input_field(struct hid_device *hid, struct hid_field *field, __u8 *data, int interrupt)
  656. {
  657. unsigned n;
  658. unsigned count = field->report_count;
  659. unsigned offset = field->report_offset;
  660. unsigned size = field->report_size;
  661. __s32 min = field->logical_minimum;
  662. __s32 max = field->logical_maximum;
  663. __s32 *value;
  664. if (!(value = kmalloc(sizeof(__s32) * count, GFP_ATOMIC)))
  665. return;
  666. for (n = 0; n < count; n++) {
  667. value[n] = min < 0 ? snto32(extract(data, offset + n * size, size), size) :
  668. extract(data, offset + n * size, size);
  669. if (!(field->flags & HID_MAIN_ITEM_VARIABLE) /* Ignore report if ErrorRollOver */
  670. && value[n] >= min && value[n] <= max
  671. && field->usage[value[n] - min].hid == HID_UP_KEYBOARD + 1)
  672. goto exit;
  673. }
  674. for (n = 0; n < count; n++) {
  675. if (HID_MAIN_ITEM_VARIABLE & field->flags) {
  676. hid_process_event(hid, field, &field->usage[n], value[n], interrupt);
  677. continue;
  678. }
  679. if (field->value[n] >= min && field->value[n] <= max
  680. && field->usage[field->value[n] - min].hid
  681. && search(value, field->value[n], count))
  682. hid_process_event(hid, field, &field->usage[field->value[n] - min], 0, interrupt);
  683. if (value[n] >= min && value[n] <= max
  684. && field->usage[value[n] - min].hid
  685. && search(field->value, value[n], count))
  686. hid_process_event(hid, field, &field->usage[value[n] - min], 1, interrupt);
  687. }
  688. memcpy(field->value, value, count * sizeof(__s32));
  689. exit:
  690. kfree(value);
  691. }
  692. static int hid_input_report(int type, struct urb *urb, int interrupt)
  693. {
  694. struct hid_device *hid = urb->context;
  695. struct hid_report_enum *report_enum = hid->report_enum + type;
  696. u8 *data = urb->transfer_buffer;
  697. int len = urb->actual_length;
  698. struct hid_report *report;
  699. int n, size;
  700. if (!len) {
  701. dbg("empty report");
  702. return -1;
  703. }
  704. #ifdef DEBUG_DATA
  705. printk(KERN_DEBUG __FILE__ ": report (size %u) (%snumbered)\n", len, report_enum->numbered ? "" : "un");
  706. #endif
  707. n = 0; /* Normally report number is 0 */
  708. if (report_enum->numbered) { /* Device uses numbered reports, data[0] is report number */
  709. n = *data++;
  710. len--;
  711. }
  712. #ifdef DEBUG_DATA
  713. {
  714. int i;
  715. printk(KERN_DEBUG __FILE__ ": report %d (size %u) = ", n, len);
  716. for (i = 0; i < len; i++)
  717. printk(" %02x", data[i]);
  718. printk("\n");
  719. }
  720. #endif
  721. if (!(report = report_enum->report_id_hash[n])) {
  722. dbg("undefined report_id %d received", n);
  723. return -1;
  724. }
  725. size = ((report->size - 1) >> 3) + 1;
  726. if (len < size) {
  727. dbg("report %d is too short, (%d < %d)", report->id, len, size);
  728. memset(data + len, 0, size - len);
  729. }
  730. if (hid->claimed & HID_CLAIMED_HIDDEV)
  731. hiddev_report_event(hid, report);
  732. for (n = 0; n < report->maxfield; n++)
  733. hid_input_field(hid, report->field[n], data, interrupt);
  734. if (hid->claimed & HID_CLAIMED_INPUT)
  735. hidinput_report_event(hid, report);
  736. return 0;
  737. }
  738. /*
  739. * Input submission and I/O error handler.
  740. */
  741. static void hid_io_error(struct hid_device *hid);
  742. /* Start up the input URB */
  743. static int hid_start_in(struct hid_device *hid)
  744. {
  745. unsigned long flags;
  746. int rc = 0;
  747. spin_lock_irqsave(&hid->inlock, flags);
  748. if (hid->open > 0 && !test_bit(HID_SUSPENDED, &hid->iofl) &&
  749. !test_and_set_bit(HID_IN_RUNNING, &hid->iofl)) {
  750. rc = usb_submit_urb(hid->urbin, GFP_ATOMIC);
  751. if (rc != 0)
  752. clear_bit(HID_IN_RUNNING, &hid->iofl);
  753. }
  754. spin_unlock_irqrestore(&hid->inlock, flags);
  755. return rc;
  756. }
  757. /* I/O retry timer routine */
  758. static void hid_retry_timeout(unsigned long _hid)
  759. {
  760. struct hid_device *hid = (struct hid_device *) _hid;
  761. dev_dbg(&hid->intf->dev, "retrying intr urb\n");
  762. if (hid_start_in(hid))
  763. hid_io_error(hid);
  764. }
  765. /* Workqueue routine to reset the device */
  766. static void hid_reset(void *_hid)
  767. {
  768. struct hid_device *hid = (struct hid_device *) _hid;
  769. int rc_lock, rc;
  770. dev_dbg(&hid->intf->dev, "resetting device\n");
  771. rc = rc_lock = usb_lock_device_for_reset(hid->dev, hid->intf);
  772. if (rc_lock >= 0) {
  773. rc = usb_reset_composite_device(hid->dev, hid->intf);
  774. if (rc_lock)
  775. usb_unlock_device(hid->dev);
  776. }
  777. clear_bit(HID_RESET_PENDING, &hid->iofl);
  778. switch (rc) {
  779. case 0:
  780. if (!test_bit(HID_IN_RUNNING, &hid->iofl))
  781. hid_io_error(hid);
  782. break;
  783. default:
  784. err("can't reset device, %s-%s/input%d, status %d",
  785. hid->dev->bus->bus_name,
  786. hid->dev->devpath,
  787. hid->ifnum, rc);
  788. /* FALLTHROUGH */
  789. case -EHOSTUNREACH:
  790. case -ENODEV:
  791. case -EINTR:
  792. break;
  793. }
  794. }
  795. /* Main I/O error handler */
  796. static void hid_io_error(struct hid_device *hid)
  797. {
  798. unsigned long flags;
  799. spin_lock_irqsave(&hid->inlock, flags);
  800. /* Stop when disconnected */
  801. if (usb_get_intfdata(hid->intf) == NULL)
  802. goto done;
  803. /* When an error occurs, retry at increasing intervals */
  804. if (hid->retry_delay == 0) {
  805. hid->retry_delay = 13; /* Then 26, 52, 104, 104, ... */
  806. hid->stop_retry = jiffies + msecs_to_jiffies(1000);
  807. } else if (hid->retry_delay < 100)
  808. hid->retry_delay *= 2;
  809. if (time_after(jiffies, hid->stop_retry)) {
  810. /* Retries failed, so do a port reset */
  811. if (!test_and_set_bit(HID_RESET_PENDING, &hid->iofl)) {
  812. if (schedule_work(&hid->reset_work))
  813. goto done;
  814. clear_bit(HID_RESET_PENDING, &hid->iofl);
  815. }
  816. }
  817. mod_timer(&hid->io_retry,
  818. jiffies + msecs_to_jiffies(hid->retry_delay));
  819. done:
  820. spin_unlock_irqrestore(&hid->inlock, flags);
  821. }
  822. /*
  823. * Input interrupt completion handler.
  824. */
  825. static void hid_irq_in(struct urb *urb)
  826. {
  827. struct hid_device *hid = urb->context;
  828. int status;
  829. switch (urb->status) {
  830. case 0: /* success */
  831. hid->retry_delay = 0;
  832. hid_input_report(HID_INPUT_REPORT, urb, 1);
  833. break;
  834. case -ECONNRESET: /* unlink */
  835. case -ENOENT:
  836. case -ESHUTDOWN: /* unplug */
  837. clear_bit(HID_IN_RUNNING, &hid->iofl);
  838. return;
  839. case -EILSEQ: /* protocol error or unplug */
  840. case -EPROTO: /* protocol error or unplug */
  841. case -ETIME: /* protocol error or unplug */
  842. case -ETIMEDOUT: /* Should never happen, but... */
  843. clear_bit(HID_IN_RUNNING, &hid->iofl);
  844. hid_io_error(hid);
  845. return;
  846. default: /* error */
  847. warn("input irq status %d received", urb->status);
  848. }
  849. status = usb_submit_urb(urb, SLAB_ATOMIC);
  850. if (status) {
  851. clear_bit(HID_IN_RUNNING, &hid->iofl);
  852. if (status != -EPERM) {
  853. err("can't resubmit intr, %s-%s/input%d, status %d",
  854. hid->dev->bus->bus_name,
  855. hid->dev->devpath,
  856. hid->ifnum, status);
  857. hid_io_error(hid);
  858. }
  859. }
  860. }
  861. /*
  862. * Output the field into the report.
  863. */
  864. static void hid_output_field(struct hid_field *field, __u8 *data)
  865. {
  866. unsigned count = field->report_count;
  867. unsigned offset = field->report_offset;
  868. unsigned size = field->report_size;
  869. unsigned n;
  870. for (n = 0; n < count; n++) {
  871. if (field->logical_minimum < 0) /* signed values */
  872. implement(data, offset + n * size, size, s32ton(field->value[n], size));
  873. else /* unsigned values */
  874. implement(data, offset + n * size, size, field->value[n]);
  875. }
  876. }
  877. /*
  878. * Create a report.
  879. */
  880. static void hid_output_report(struct hid_report *report, __u8 *data)
  881. {
  882. unsigned n;
  883. if (report->id > 0)
  884. *data++ = report->id;
  885. for (n = 0; n < report->maxfield; n++)
  886. hid_output_field(report->field[n], data);
  887. }
  888. /*
  889. * Set a field value. The report this field belongs to has to be
  890. * created and transferred to the device, to set this value in the
  891. * device.
  892. */
  893. int hid_set_field(struct hid_field *field, unsigned offset, __s32 value)
  894. {
  895. unsigned size = field->report_size;
  896. hid_dump_input(field->usage + offset, value);
  897. if (offset >= field->report_count) {
  898. dbg("offset (%d) exceeds report_count (%d)", offset, field->report_count);
  899. hid_dump_field(field, 8);
  900. return -1;
  901. }
  902. if (field->logical_minimum < 0) {
  903. if (value != snto32(s32ton(value, size), size)) {
  904. dbg("value %d is out of range", value);
  905. return -1;
  906. }
  907. }
  908. field->value[offset] = value;
  909. return 0;
  910. }
  911. /*
  912. * Find a report field with a specified HID usage.
  913. */
  914. #if 0
  915. struct hid_field *hid_find_field_by_usage(struct hid_device *hid, __u32 wanted_usage, int type)
  916. {
  917. struct hid_report *report;
  918. int i;
  919. list_for_each_entry(report, &hid->report_enum[type].report_list, list)
  920. for (i = 0; i < report->maxfield; i++)
  921. if (report->field[i]->logical == wanted_usage)
  922. return report->field[i];
  923. return NULL;
  924. }
  925. #endif /* 0 */
  926. static int hid_submit_out(struct hid_device *hid)
  927. {
  928. struct hid_report *report;
  929. report = hid->out[hid->outtail];
  930. hid_output_report(report, hid->outbuf);
  931. hid->urbout->transfer_buffer_length = ((report->size - 1) >> 3) + 1 + (report->id > 0);
  932. hid->urbout->dev = hid->dev;
  933. dbg("submitting out urb");
  934. if (usb_submit_urb(hid->urbout, GFP_ATOMIC)) {
  935. err("usb_submit_urb(out) failed");
  936. return -1;
  937. }
  938. return 0;
  939. }
  940. static int hid_submit_ctrl(struct hid_device *hid)
  941. {
  942. struct hid_report *report;
  943. unsigned char dir;
  944. int len;
  945. report = hid->ctrl[hid->ctrltail].report;
  946. dir = hid->ctrl[hid->ctrltail].dir;
  947. len = ((report->size - 1) >> 3) + 1 + (report->id > 0);
  948. if (dir == USB_DIR_OUT) {
  949. hid_output_report(report, hid->ctrlbuf);
  950. hid->urbctrl->pipe = usb_sndctrlpipe(hid->dev, 0);
  951. hid->urbctrl->transfer_buffer_length = len;
  952. } else {
  953. int maxpacket, padlen;
  954. hid->urbctrl->pipe = usb_rcvctrlpipe(hid->dev, 0);
  955. maxpacket = usb_maxpacket(hid->dev, hid->urbctrl->pipe, 0);
  956. if (maxpacket > 0) {
  957. padlen = (len + maxpacket - 1) / maxpacket;
  958. padlen *= maxpacket;
  959. if (padlen > hid->bufsize)
  960. padlen = hid->bufsize;
  961. } else
  962. padlen = 0;
  963. hid->urbctrl->transfer_buffer_length = padlen;
  964. }
  965. hid->urbctrl->dev = hid->dev;
  966. hid->cr->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE | dir;
  967. hid->cr->bRequest = (dir == USB_DIR_OUT) ? HID_REQ_SET_REPORT : HID_REQ_GET_REPORT;
  968. hid->cr->wValue = cpu_to_le16(((report->type + 1) << 8) | report->id);
  969. hid->cr->wIndex = cpu_to_le16(hid->ifnum);
  970. hid->cr->wLength = cpu_to_le16(len);
  971. dbg("submitting ctrl urb: %s wValue=0x%04x wIndex=0x%04x wLength=%u",
  972. hid->cr->bRequest == HID_REQ_SET_REPORT ? "Set_Report" : "Get_Report",
  973. hid->cr->wValue, hid->cr->wIndex, hid->cr->wLength);
  974. if (usb_submit_urb(hid->urbctrl, GFP_ATOMIC)) {
  975. err("usb_submit_urb(ctrl) failed");
  976. return -1;
  977. }
  978. return 0;
  979. }
  980. /*
  981. * Output interrupt completion handler.
  982. */
  983. static void hid_irq_out(struct urb *urb)
  984. {
  985. struct hid_device *hid = urb->context;
  986. unsigned long flags;
  987. int unplug = 0;
  988. switch (urb->status) {
  989. case 0: /* success */
  990. break;
  991. case -ESHUTDOWN: /* unplug */
  992. unplug = 1;
  993. case -EILSEQ: /* protocol error or unplug */
  994. case -EPROTO: /* protocol error or unplug */
  995. case -ECONNRESET: /* unlink */
  996. case -ENOENT:
  997. break;
  998. default: /* error */
  999. warn("output irq status %d received", urb->status);
  1000. }
  1001. spin_lock_irqsave(&hid->outlock, flags);
  1002. if (unplug)
  1003. hid->outtail = hid->outhead;
  1004. else
  1005. hid->outtail = (hid->outtail + 1) & (HID_OUTPUT_FIFO_SIZE - 1);
  1006. if (hid->outhead != hid->outtail) {
  1007. if (hid_submit_out(hid)) {
  1008. clear_bit(HID_OUT_RUNNING, &hid->iofl);
  1009. wake_up(&hid->wait);
  1010. }
  1011. spin_unlock_irqrestore(&hid->outlock, flags);
  1012. return;
  1013. }
  1014. clear_bit(HID_OUT_RUNNING, &hid->iofl);
  1015. spin_unlock_irqrestore(&hid->outlock, flags);
  1016. wake_up(&hid->wait);
  1017. }
  1018. /*
  1019. * Control pipe completion handler.
  1020. */
  1021. static void hid_ctrl(struct urb *urb)
  1022. {
  1023. struct hid_device *hid = urb->context;
  1024. unsigned long flags;
  1025. int unplug = 0;
  1026. spin_lock_irqsave(&hid->ctrllock, flags);
  1027. switch (urb->status) {
  1028. case 0: /* success */
  1029. if (hid->ctrl[hid->ctrltail].dir == USB_DIR_IN)
  1030. hid_input_report(hid->ctrl[hid->ctrltail].report->type, urb, 0);
  1031. break;
  1032. case -ESHUTDOWN: /* unplug */
  1033. unplug = 1;
  1034. case -EILSEQ: /* protocol error or unplug */
  1035. case -EPROTO: /* protocol error or unplug */
  1036. case -ECONNRESET: /* unlink */
  1037. case -ENOENT:
  1038. case -EPIPE: /* report not available */
  1039. break;
  1040. default: /* error */
  1041. warn("ctrl urb status %d received", urb->status);
  1042. }
  1043. if (unplug)
  1044. hid->ctrltail = hid->ctrlhead;
  1045. else
  1046. hid->ctrltail = (hid->ctrltail + 1) & (HID_CONTROL_FIFO_SIZE - 1);
  1047. if (hid->ctrlhead != hid->ctrltail) {
  1048. if (hid_submit_ctrl(hid)) {
  1049. clear_bit(HID_CTRL_RUNNING, &hid->iofl);
  1050. wake_up(&hid->wait);
  1051. }
  1052. spin_unlock_irqrestore(&hid->ctrllock, flags);
  1053. return;
  1054. }
  1055. clear_bit(HID_CTRL_RUNNING, &hid->iofl);
  1056. spin_unlock_irqrestore(&hid->ctrllock, flags);
  1057. wake_up(&hid->wait);
  1058. }
  1059. void hid_submit_report(struct hid_device *hid, struct hid_report *report, unsigned char dir)
  1060. {
  1061. int head;
  1062. unsigned long flags;
  1063. if ((hid->quirks & HID_QUIRK_NOGET) && dir == USB_DIR_IN)
  1064. return;
  1065. if (hid->urbout && dir == USB_DIR_OUT && report->type == HID_OUTPUT_REPORT) {
  1066. spin_lock_irqsave(&hid->outlock, flags);
  1067. if ((head = (hid->outhead + 1) & (HID_OUTPUT_FIFO_SIZE - 1)) == hid->outtail) {
  1068. spin_unlock_irqrestore(&hid->outlock, flags);
  1069. warn("output queue full");
  1070. return;
  1071. }
  1072. hid->out[hid->outhead] = report;
  1073. hid->outhead = head;
  1074. if (!test_and_set_bit(HID_OUT_RUNNING, &hid->iofl))
  1075. if (hid_submit_out(hid))
  1076. clear_bit(HID_OUT_RUNNING, &hid->iofl);
  1077. spin_unlock_irqrestore(&hid->outlock, flags);
  1078. return;
  1079. }
  1080. spin_lock_irqsave(&hid->ctrllock, flags);
  1081. if ((head = (hid->ctrlhead + 1) & (HID_CONTROL_FIFO_SIZE - 1)) == hid->ctrltail) {
  1082. spin_unlock_irqrestore(&hid->ctrllock, flags);
  1083. warn("control queue full");
  1084. return;
  1085. }
  1086. hid->ctrl[hid->ctrlhead].report = report;
  1087. hid->ctrl[hid->ctrlhead].dir = dir;
  1088. hid->ctrlhead = head;
  1089. if (!test_and_set_bit(HID_CTRL_RUNNING, &hid->iofl))
  1090. if (hid_submit_ctrl(hid))
  1091. clear_bit(HID_CTRL_RUNNING, &hid->iofl);
  1092. spin_unlock_irqrestore(&hid->ctrllock, flags);
  1093. }
  1094. int hid_wait_io(struct hid_device *hid)
  1095. {
  1096. if (!wait_event_timeout(hid->wait, (!test_bit(HID_CTRL_RUNNING, &hid->iofl) &&
  1097. !test_bit(HID_OUT_RUNNING, &hid->iofl)),
  1098. 10*HZ)) {
  1099. dbg("timeout waiting for ctrl or out queue to clear");
  1100. return -1;
  1101. }
  1102. return 0;
  1103. }
  1104. static int hid_set_idle(struct usb_device *dev, int ifnum, int report, int idle)
  1105. {
  1106. return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  1107. HID_REQ_SET_IDLE, USB_TYPE_CLASS | USB_RECIP_INTERFACE, (idle << 8) | report,
  1108. ifnum, NULL, 0, USB_CTRL_SET_TIMEOUT);
  1109. }
  1110. static int hid_get_class_descriptor(struct usb_device *dev, int ifnum,
  1111. unsigned char type, void *buf, int size)
  1112. {
  1113. int result, retries = 4;
  1114. memset(buf,0,size); // Make sure we parse really received data
  1115. do {
  1116. result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  1117. USB_REQ_GET_DESCRIPTOR, USB_RECIP_INTERFACE | USB_DIR_IN,
  1118. (type << 8), ifnum, buf, size, USB_CTRL_GET_TIMEOUT);
  1119. retries--;
  1120. } while (result < size && retries);
  1121. return result;
  1122. }
  1123. int hid_open(struct hid_device *hid)
  1124. {
  1125. ++hid->open;
  1126. if (hid_start_in(hid))
  1127. hid_io_error(hid);
  1128. return 0;
  1129. }
  1130. void hid_close(struct hid_device *hid)
  1131. {
  1132. if (!--hid->open)
  1133. usb_kill_urb(hid->urbin);
  1134. }
  1135. #define USB_VENDOR_ID_PANJIT 0x134c
  1136. /*
  1137. * Initialize all reports
  1138. */
  1139. void hid_init_reports(struct hid_device *hid)
  1140. {
  1141. struct hid_report *report;
  1142. int err, ret;
  1143. list_for_each_entry(report, &hid->report_enum[HID_INPUT_REPORT].report_list, list)
  1144. hid_submit_report(hid, report, USB_DIR_IN);
  1145. list_for_each_entry(report, &hid->report_enum[HID_FEATURE_REPORT].report_list, list)
  1146. hid_submit_report(hid, report, USB_DIR_IN);
  1147. err = 0;
  1148. ret = hid_wait_io(hid);
  1149. while (ret) {
  1150. err |= ret;
  1151. if (test_bit(HID_CTRL_RUNNING, &hid->iofl))
  1152. usb_kill_urb(hid->urbctrl);
  1153. if (test_bit(HID_OUT_RUNNING, &hid->iofl))
  1154. usb_kill_urb(hid->urbout);
  1155. ret = hid_wait_io(hid);
  1156. }
  1157. if (err)
  1158. warn("timeout initializing reports");
  1159. }
  1160. #define USB_VENDOR_ID_GTCO 0x078c
  1161. #define USB_DEVICE_ID_GTCO_90 0x0090
  1162. #define USB_DEVICE_ID_GTCO_100 0x0100
  1163. #define USB_DEVICE_ID_GTCO_101 0x0101
  1164. #define USB_DEVICE_ID_GTCO_103 0x0103
  1165. #define USB_DEVICE_ID_GTCO_104 0x0104
  1166. #define USB_DEVICE_ID_GTCO_105 0x0105
  1167. #define USB_DEVICE_ID_GTCO_106 0x0106
  1168. #define USB_DEVICE_ID_GTCO_107 0x0107
  1169. #define USB_DEVICE_ID_GTCO_108 0x0108
  1170. #define USB_DEVICE_ID_GTCO_200 0x0200
  1171. #define USB_DEVICE_ID_GTCO_201 0x0201
  1172. #define USB_DEVICE_ID_GTCO_202 0x0202
  1173. #define USB_DEVICE_ID_GTCO_203 0x0203
  1174. #define USB_DEVICE_ID_GTCO_204 0x0204
  1175. #define USB_DEVICE_ID_GTCO_205 0x0205
  1176. #define USB_DEVICE_ID_GTCO_206 0x0206
  1177. #define USB_DEVICE_ID_GTCO_207 0x0207
  1178. #define USB_DEVICE_ID_GTCO_300 0x0300
  1179. #define USB_DEVICE_ID_GTCO_301 0x0301
  1180. #define USB_DEVICE_ID_GTCO_302 0x0302
  1181. #define USB_DEVICE_ID_GTCO_303 0x0303
  1182. #define USB_DEVICE_ID_GTCO_304 0x0304
  1183. #define USB_DEVICE_ID_GTCO_305 0x0305
  1184. #define USB_DEVICE_ID_GTCO_306 0x0306
  1185. #define USB_DEVICE_ID_GTCO_307 0x0307
  1186. #define USB_DEVICE_ID_GTCO_308 0x0308
  1187. #define USB_DEVICE_ID_GTCO_309 0x0309
  1188. #define USB_DEVICE_ID_GTCO_400 0x0400
  1189. #define USB_DEVICE_ID_GTCO_401 0x0401
  1190. #define USB_DEVICE_ID_GTCO_402 0x0402
  1191. #define USB_DEVICE_ID_GTCO_403 0x0403
  1192. #define USB_DEVICE_ID_GTCO_404 0x0404
  1193. #define USB_DEVICE_ID_GTCO_405 0x0405
  1194. #define USB_DEVICE_ID_GTCO_500 0x0500
  1195. #define USB_DEVICE_ID_GTCO_501 0x0501
  1196. #define USB_DEVICE_ID_GTCO_502 0x0502
  1197. #define USB_DEVICE_ID_GTCO_503 0x0503
  1198. #define USB_DEVICE_ID_GTCO_504 0x0504
  1199. #define USB_DEVICE_ID_GTCO_1000 0x1000
  1200. #define USB_DEVICE_ID_GTCO_1001 0x1001
  1201. #define USB_DEVICE_ID_GTCO_1002 0x1002
  1202. #define USB_DEVICE_ID_GTCO_1003 0x1003
  1203. #define USB_DEVICE_ID_GTCO_1004 0x1004
  1204. #define USB_DEVICE_ID_GTCO_1005 0x1005
  1205. #define USB_DEVICE_ID_GTCO_1006 0x1006
  1206. #define USB_VENDOR_ID_WACOM 0x056a
  1207. #define USB_VENDOR_ID_ACECAD 0x0460
  1208. #define USB_DEVICE_ID_ACECAD_FLAIR 0x0004
  1209. #define USB_DEVICE_ID_ACECAD_302 0x0008
  1210. #define USB_VENDOR_ID_KBGEAR 0x084e
  1211. #define USB_DEVICE_ID_KBGEAR_JAMSTUDIO 0x1001
  1212. #define USB_VENDOR_ID_AIPTEK 0x08ca
  1213. #define USB_DEVICE_ID_AIPTEK_01 0x0001
  1214. #define USB_DEVICE_ID_AIPTEK_10 0x0010
  1215. #define USB_DEVICE_ID_AIPTEK_20 0x0020
  1216. #define USB_DEVICE_ID_AIPTEK_21 0x0021
  1217. #define USB_DEVICE_ID_AIPTEK_22 0x0022
  1218. #define USB_DEVICE_ID_AIPTEK_23 0x0023
  1219. #define USB_DEVICE_ID_AIPTEK_24 0x0024
  1220. #define USB_VENDOR_ID_GRIFFIN 0x077d
  1221. #define USB_DEVICE_ID_POWERMATE 0x0410
  1222. #define USB_DEVICE_ID_SOUNDKNOB 0x04AA
  1223. #define USB_VENDOR_ID_ATEN 0x0557
  1224. #define USB_DEVICE_ID_ATEN_UC100KM 0x2004
  1225. #define USB_DEVICE_ID_ATEN_CS124U 0x2202
  1226. #define USB_DEVICE_ID_ATEN_2PORTKVM 0x2204
  1227. #define USB_DEVICE_ID_ATEN_4PORTKVM 0x2205
  1228. #define USB_DEVICE_ID_ATEN_4PORTKVMC 0x2208
  1229. #define USB_VENDOR_ID_TOPMAX 0x0663
  1230. #define USB_DEVICE_ID_TOPMAX_COBRAPAD 0x0103
  1231. #define USB_VENDOR_ID_HAPP 0x078b
  1232. #define USB_DEVICE_ID_UGCI_DRIVING 0x0010
  1233. #define USB_DEVICE_ID_UGCI_FLYING 0x0020
  1234. #define USB_DEVICE_ID_UGCI_FIGHTING 0x0030
  1235. #define USB_VENDOR_ID_MGE 0x0463
  1236. #define USB_DEVICE_ID_MGE_UPS 0xffff
  1237. #define USB_DEVICE_ID_MGE_UPS1 0x0001
  1238. #define USB_VENDOR_ID_ONTRAK 0x0a07
  1239. #define USB_DEVICE_ID_ONTRAK_ADU100 0x0064
  1240. #define USB_VENDOR_ID_ESSENTIAL_REALITY 0x0d7f
  1241. #define USB_DEVICE_ID_ESSENTIAL_REALITY_P5 0x0100
  1242. #define USB_VENDOR_ID_A4TECH 0x09da
  1243. #define USB_DEVICE_ID_A4TECH_WCP32PU 0x0006
  1244. #define USB_VENDOR_ID_AASHIMA 0x06d6
  1245. #define USB_DEVICE_ID_AASHIMA_GAMEPAD 0x0025
  1246. #define USB_DEVICE_ID_AASHIMA_PREDATOR 0x0026
  1247. #define USB_VENDOR_ID_CYPRESS 0x04b4
  1248. #define USB_DEVICE_ID_CYPRESS_MOUSE 0x0001
  1249. #define USB_DEVICE_ID_CYPRESS_HIDCOM 0x5500
  1250. #define USB_DEVICE_ID_CYPRESS_ULTRAMOUSE 0x7417
  1251. #define USB_VENDOR_ID_BERKSHIRE 0x0c98
  1252. #define USB_DEVICE_ID_BERKSHIRE_PCWD 0x1140
  1253. #define USB_VENDOR_ID_ALPS 0x0433
  1254. #define USB_DEVICE_ID_IBM_GAMEPAD 0x1101
  1255. #define USB_VENDOR_ID_SAITEK 0x06a3
  1256. #define USB_DEVICE_ID_SAITEK_RUMBLEPAD 0xff17
  1257. #define USB_VENDOR_ID_NEC 0x073e
  1258. #define USB_DEVICE_ID_NEC_USB_GAME_PAD 0x0301
  1259. #define USB_VENDOR_ID_CHIC 0x05fe
  1260. #define USB_DEVICE_ID_CHIC_GAMEPAD 0x0014
  1261. #define USB_VENDOR_ID_GLAB 0x06c2
  1262. #define USB_DEVICE_ID_4_PHIDGETSERVO_30 0x0038
  1263. #define USB_DEVICE_ID_1_PHIDGETSERVO_30 0x0039
  1264. #define USB_DEVICE_ID_0_0_4_IF_KIT 0x0040
  1265. #define USB_DEVICE_ID_0_16_16_IF_KIT 0x0044
  1266. #define USB_DEVICE_ID_8_8_8_IF_KIT 0x0045
  1267. #define USB_DEVICE_ID_0_8_7_IF_KIT 0x0051
  1268. #define USB_DEVICE_ID_0_8_8_IF_KIT 0x0053
  1269. #define USB_DEVICE_ID_PHIDGET_MOTORCONTROL 0x0058
  1270. #define USB_VENDOR_ID_WISEGROUP 0x0925
  1271. #define USB_DEVICE_ID_1_PHIDGETSERVO_20 0x8101
  1272. #define USB_DEVICE_ID_4_PHIDGETSERVO_20 0x8104
  1273. #define USB_DEVICE_ID_8_8_4_IF_KIT 0x8201
  1274. #define USB_DEVICE_ID_DUAL_USB_JOYPAD 0x8866
  1275. #define USB_VENDOR_ID_WISEGROUP_LTD 0x6677
  1276. #define USB_DEVICE_ID_SMARTJOY_DUAL_PLUS 0x8802
  1277. #define USB_VENDOR_ID_CODEMERCS 0x07c0
  1278. #define USB_DEVICE_ID_CODEMERCS_IOW40 0x1500
  1279. #define USB_DEVICE_ID_CODEMERCS_IOW24 0x1501
  1280. #define USB_DEVICE_ID_CODEMERCS_IOW48 0x1502
  1281. #define USB_DEVICE_ID_CODEMERCS_IOW28 0x1503
  1282. #define USB_VENDOR_ID_DELORME 0x1163
  1283. #define USB_DEVICE_ID_DELORME_EARTHMATE 0x0100
  1284. #define USB_DEVICE_ID_DELORME_EM_LT20 0x0200
  1285. #define USB_VENDOR_ID_MCC 0x09db
  1286. #define USB_DEVICE_ID_MCC_PMD1024LS 0x0076
  1287. #define USB_DEVICE_ID_MCC_PMD1208LS 0x007a
  1288. #define USB_VENDOR_ID_VERNIER 0x08f7
  1289. #define USB_DEVICE_ID_VERNIER_LABPRO 0x0001
  1290. #define USB_DEVICE_ID_VERNIER_GOTEMP 0x0002
  1291. #define USB_DEVICE_ID_VERNIER_SKIP 0x0003
  1292. #define USB_DEVICE_ID_VERNIER_CYCLOPS 0x0004
  1293. #define USB_VENDOR_ID_LD 0x0f11
  1294. #define USB_DEVICE_ID_LD_CASSY 0x1000
  1295. #define USB_DEVICE_ID_LD_POCKETCASSY 0x1010
  1296. #define USB_DEVICE_ID_LD_MOBILECASSY 0x1020
  1297. #define USB_DEVICE_ID_LD_JWM 0x1080
  1298. #define USB_DEVICE_ID_LD_DMMP 0x1081
  1299. #define USB_DEVICE_ID_LD_UMIP 0x1090
  1300. #define USB_DEVICE_ID_LD_XRAY1 0x1100
  1301. #define USB_DEVICE_ID_LD_XRAY2 0x1101
  1302. #define USB_DEVICE_ID_LD_VIDEOCOM 0x1200
  1303. #define USB_DEVICE_ID_LD_COM3LAB 0x2000
  1304. #define USB_DEVICE_ID_LD_TELEPORT 0x2010
  1305. #define USB_DEVICE_ID_LD_NETWORKANALYSER 0x2020
  1306. #define USB_DEVICE_ID_LD_POWERCONTROL 0x2030
  1307. #define USB_DEVICE_ID_LD_MACHINETEST 0x2040
  1308. #define USB_VENDOR_ID_APPLE 0x05ac
  1309. #define USB_DEVICE_ID_APPLE_MIGHTYMOUSE 0x0304
  1310. #define USB_VENDOR_ID_CHERRY 0x046a
  1311. #define USB_DEVICE_ID_CHERRY_CYMOTION 0x0023
  1312. #define USB_VENDOR_ID_YEALINK 0x6993
  1313. #define USB_DEVICE_ID_YEALINK_P1K_P4K_B2K 0xb001
  1314. #define USB_VENDOR_ID_ALCOR 0x058f
  1315. #define USB_DEVICE_ID_ALCOR_USBRS232 0x9720
  1316. #define USB_VENDOR_ID_SUN 0x0430
  1317. #define USB_DEVICE_ID_RARITAN_KVM_DONGLE 0xcdab
  1318. /*
  1319. * Alphabetically sorted blacklist by quirk type.
  1320. */
  1321. static const struct hid_blacklist {
  1322. __u16 idVendor;
  1323. __u16 idProduct;
  1324. unsigned quirks;
  1325. } hid_blacklist[] = {
  1326. { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_01, HID_QUIRK_IGNORE },
  1327. { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_10, HID_QUIRK_IGNORE },
  1328. { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_20, HID_QUIRK_IGNORE },
  1329. { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_21, HID_QUIRK_IGNORE },
  1330. { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_22, HID_QUIRK_IGNORE },
  1331. { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_23, HID_QUIRK_IGNORE },
  1332. { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_24, HID_QUIRK_IGNORE },
  1333. { USB_VENDOR_ID_ALCOR, USB_DEVICE_ID_ALCOR_USBRS232, HID_QUIRK_IGNORE },
  1334. { USB_VENDOR_ID_BERKSHIRE, USB_DEVICE_ID_BERKSHIRE_PCWD, HID_QUIRK_IGNORE },
  1335. { USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW40, HID_QUIRK_IGNORE },
  1336. { USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW24, HID_QUIRK_IGNORE },
  1337. { USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW48, HID_QUIRK_IGNORE },
  1338. { USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW28, HID_QUIRK_IGNORE },
  1339. { USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_HIDCOM, HID_QUIRK_IGNORE },
  1340. { USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_ULTRAMOUSE, HID_QUIRK_IGNORE },
  1341. { USB_VENDOR_ID_DELORME, USB_DEVICE_ID_DELORME_EARTHMATE, HID_QUIRK_IGNORE },
  1342. { USB_VENDOR_ID_DELORME, USB_DEVICE_ID_DELORME_EM_LT20, HID_QUIRK_IGNORE },
  1343. { USB_VENDOR_ID_ESSENTIAL_REALITY, USB_DEVICE_ID_ESSENTIAL_REALITY_P5, HID_QUIRK_IGNORE },
  1344. { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_4_PHIDGETSERVO_30, HID_QUIRK_IGNORE },
  1345. { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_1_PHIDGETSERVO_30, HID_QUIRK_IGNORE },
  1346. { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_0_4_IF_KIT, HID_QUIRK_IGNORE },
  1347. { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_16_16_IF_KIT, HID_QUIRK_IGNORE },
  1348. { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_8_8_8_IF_KIT, HID_QUIRK_IGNORE },
  1349. { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_8_7_IF_KIT, HID_QUIRK_IGNORE },
  1350. { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_8_8_IF_KIT, HID_QUIRK_IGNORE },
  1351. { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_PHIDGET_MOTORCONTROL, HID_QUIRK_IGNORE },
  1352. { USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_POWERMATE, HID_QUIRK_IGNORE },
  1353. { USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_SOUNDKNOB, HID_QUIRK_IGNORE },
  1354. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_90, HID_QUIRK_IGNORE },
  1355. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_100, HID_QUIRK_IGNORE },
  1356. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_101, HID_QUIRK_IGNORE },
  1357. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_103, HID_QUIRK_IGNORE },
  1358. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_104, HID_QUIRK_IGNORE },
  1359. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_105, HID_QUIRK_IGNORE },
  1360. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_106, HID_QUIRK_IGNORE },
  1361. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_107, HID_QUIRK_IGNORE },
  1362. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_108, HID_QUIRK_IGNORE },
  1363. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_200, HID_QUIRK_IGNORE },
  1364. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_201, HID_QUIRK_IGNORE },
  1365. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_202, HID_QUIRK_IGNORE },
  1366. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_203, HID_QUIRK_IGNORE },
  1367. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_204, HID_QUIRK_IGNORE },
  1368. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_205, HID_QUIRK_IGNORE },
  1369. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_206, HID_QUIRK_IGNORE },
  1370. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_207, HID_QUIRK_IGNORE },
  1371. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_300, HID_QUIRK_IGNORE },
  1372. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_301, HID_QUIRK_IGNORE },
  1373. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_302, HID_QUIRK_IGNORE },
  1374. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_303, HID_QUIRK_IGNORE },
  1375. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_304, HID_QUIRK_IGNORE },
  1376. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_305, HID_QUIRK_IGNORE },
  1377. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_306, HID_QUIRK_IGNORE },
  1378. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_307, HID_QUIRK_IGNORE },
  1379. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_308, HID_QUIRK_IGNORE },
  1380. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_309, HID_QUIRK_IGNORE },
  1381. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_400, HID_QUIRK_IGNORE },
  1382. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_401, HID_QUIRK_IGNORE },
  1383. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_402, HID_QUIRK_IGNORE },
  1384. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_403, HID_QUIRK_IGNORE },
  1385. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_404, HID_QUIRK_IGNORE },
  1386. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_405, HID_QUIRK_IGNORE },
  1387. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_500, HID_QUIRK_IGNORE },
  1388. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_501, HID_QUIRK_IGNORE },
  1389. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_502, HID_QUIRK_IGNORE },
  1390. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_503, HID_QUIRK_IGNORE },
  1391. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_504, HID_QUIRK_IGNORE },
  1392. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1000, HID_QUIRK_IGNORE },
  1393. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1001, HID_QUIRK_IGNORE },
  1394. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1002, HID_QUIRK_IGNORE },
  1395. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1003, HID_QUIRK_IGNORE },
  1396. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1004, HID_QUIRK_IGNORE },
  1397. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1005, HID_QUIRK_IGNORE },
  1398. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1006, HID_QUIRK_IGNORE },
  1399. { USB_VENDOR_ID_KBGEAR, USB_DEVICE_ID_KBGEAR_JAMSTUDIO, HID_QUIRK_IGNORE },
  1400. { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_CASSY, HID_QUIRK_IGNORE },
  1401. { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POCKETCASSY, HID_QUIRK_IGNORE },
  1402. { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOBILECASSY, HID_QUIRK_IGNORE },
  1403. { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_JWM, HID_QUIRK_IGNORE },
  1404. { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_DMMP, HID_QUIRK_IGNORE },
  1405. { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_UMIP, HID_QUIRK_IGNORE },
  1406. { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY1, HID_QUIRK_IGNORE },
  1407. { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY2, HID_QUIRK_IGNORE },
  1408. { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_VIDEOCOM, HID_QUIRK_IGNORE },
  1409. { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_COM3LAB, HID_QUIRK_IGNORE },
  1410. { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_TELEPORT, HID_QUIRK_IGNORE },
  1411. { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_NETWORKANALYSER, HID_QUIRK_IGNORE },
  1412. { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POWERCONTROL, HID_QUIRK_IGNORE },
  1413. { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MACHINETEST, HID_QUIRK_IGNORE },
  1414. { USB_VENDOR_ID_MCC, USB_DEVICE_ID_MCC_PMD1024LS, HID_QUIRK_IGNORE },
  1415. { USB_VENDOR_ID_MCC, USB_DEVICE_ID_MCC_PMD1208LS, HID_QUIRK_IGNORE },
  1416. { USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS, HID_QUIRK_IGNORE },
  1417. { USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS1, HID_QUIRK_IGNORE },
  1418. { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100, HID_QUIRK_IGNORE },
  1419. { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 20, HID_QUIRK_IGNORE },
  1420. { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 30, HID_QUIRK_IGNORE },
  1421. { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 100, HID_QUIRK_IGNORE },
  1422. { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 108, HID_QUIRK_IGNORE },
  1423. { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 118, HID_QUIRK_IGNORE },
  1424. { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 200, HID_QUIRK_IGNORE },
  1425. { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 300, HID_QUIRK_IGNORE },
  1426. { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 400, HID_QUIRK_IGNORE },
  1427. { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 500, HID_QUIRK_IGNORE },
  1428. { USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_LABPRO, HID_QUIRK_IGNORE },
  1429. { USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_GOTEMP, HID_QUIRK_IGNORE },
  1430. { USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_SKIP, HID_QUIRK_IGNORE },
  1431. { USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_CYCLOPS, HID_QUIRK_IGNORE },
  1432. { USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_4_PHIDGETSERVO_20, HID_QUIRK_IGNORE },
  1433. { USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_1_PHIDGETSERVO_20, HID_QUIRK_IGNORE },
  1434. { USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_8_8_4_IF_KIT, HID_QUIRK_IGNORE },
  1435. { USB_VENDOR_ID_YEALINK, USB_DEVICE_ID_YEALINK_P1K_P4K_B2K, HID_QUIRK_IGNORE },
  1436. { USB_VENDOR_ID_ACECAD, USB_DEVICE_ID_ACECAD_FLAIR, HID_QUIRK_IGNORE },
  1437. { USB_VENDOR_ID_ACECAD, USB_DEVICE_ID_ACECAD_302, HID_QUIRK_IGNORE },
  1438. { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_UC100KM, HID_QUIRK_NOGET },
  1439. { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_CS124U, HID_QUIRK_NOGET },
  1440. { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_2PORTKVM, HID_QUIRK_NOGET },
  1441. { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_4PORTKVM, HID_QUIRK_NOGET },
  1442. { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_4PORTKVMC, HID_QUIRK_NOGET },
  1443. { USB_VENDOR_ID_SUN, USB_DEVICE_ID_RARITAN_KVM_DONGLE, HID_QUIRK_NOGET },
  1444. { USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_DUAL_USB_JOYPAD, HID_QUIRK_NOGET | HID_QUIRK_MULTI_INPUT },
  1445. { USB_VENDOR_ID_WISEGROUP_LTD, USB_DEVICE_ID_SMARTJOY_DUAL_PLUS, HID_QUIRK_NOGET | HID_QUIRK_MULTI_INPUT },
  1446. { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MIGHTYMOUSE, HID_QUIRK_MIGHTYMOUSE | HID_QUIRK_INVERT_HWHEEL },
  1447. { USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_WCP32PU, HID_QUIRK_2WHEEL_MOUSE_HACK_7 },
  1448. { USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_MOUSE, HID_QUIRK_2WHEEL_MOUSE_HACK_5 },
  1449. { USB_VENDOR_ID_AASHIMA, USB_DEVICE_ID_AASHIMA_GAMEPAD, HID_QUIRK_BADPAD },
  1450. { USB_VENDOR_ID_AASHIMA, USB_DEVICE_ID_AASHIMA_PREDATOR, HID_QUIRK_BADPAD },
  1451. { USB_VENDOR_ID_ALPS, USB_DEVICE_ID_IBM_GAMEPAD, HID_QUIRK_BADPAD },
  1452. { USB_VENDOR_ID_CHIC, USB_DEVICE_ID_CHIC_GAMEPAD, HID_QUIRK_BADPAD },
  1453. { USB_VENDOR_ID_HAPP, USB_DEVICE_ID_UGCI_DRIVING, HID_QUIRK_BADPAD | HID_QUIRK_MULTI_INPUT },
  1454. { USB_VENDOR_ID_HAPP, USB_DEVICE_ID_UGCI_FLYING, HID_QUIRK_BADPAD | HID_QUIRK_MULTI_INPUT },
  1455. { USB_VENDOR_ID_HAPP, USB_DEVICE_ID_UGCI_FIGHTING, HID_QUIRK_BADPAD | HID_QUIRK_MULTI_INPUT },
  1456. { USB_VENDOR_ID_NEC, USB_DEVICE_ID_NEC_USB_GAME_PAD, HID_QUIRK_BADPAD },
  1457. { USB_VENDOR_ID_SAITEK, USB_DEVICE_ID_SAITEK_RUMBLEPAD, HID_QUIRK_BADPAD },
  1458. { USB_VENDOR_ID_TOPMAX, USB_DEVICE_ID_TOPMAX_COBRAPAD, HID_QUIRK_BADPAD },
  1459. { USB_VENDOR_ID_CHERRY, USB_DEVICE_ID_CHERRY_CYMOTION, HID_QUIRK_CYMOTION },
  1460. { USB_VENDOR_ID_APPLE, 0x020E, HID_QUIRK_POWERBOOK_HAS_FN },
  1461. { USB_VENDOR_ID_APPLE, 0x020F, HID_QUIRK_POWERBOOK_HAS_FN },
  1462. { USB_VENDOR_ID_APPLE, 0x0214, HID_QUIRK_POWERBOOK_HAS_FN },
  1463. { USB_VENDOR_ID_APPLE, 0x0215, HID_QUIRK_POWERBOOK_HAS_FN },
  1464. { USB_VENDOR_ID_APPLE, 0x0216, HID_QUIRK_POWERBOOK_HAS_FN },
  1465. { USB_VENDOR_ID_APPLE, 0x0217, HID_QUIRK_POWERBOOK_HAS_FN },
  1466. { USB_VENDOR_ID_APPLE, 0x0218, HID_QUIRK_POWERBOOK_HAS_FN },
  1467. { USB_VENDOR_ID_APPLE, 0x0219, HID_QUIRK_POWERBOOK_HAS_FN },
  1468. { USB_VENDOR_ID_APPLE, 0x030A, HID_QUIRK_POWERBOOK_HAS_FN },
  1469. { USB_VENDOR_ID_APPLE, 0x030B, HID_QUIRK_POWERBOOK_HAS_FN },
  1470. { USB_VENDOR_ID_PANJIT, 0x0001, HID_QUIRK_IGNORE },
  1471. { USB_VENDOR_ID_PANJIT, 0x0002, HID_QUIRK_IGNORE },
  1472. { USB_VENDOR_ID_PANJIT, 0x0003, HID_QUIRK_IGNORE },
  1473. { USB_VENDOR_ID_PANJIT, 0x0004, HID_QUIRK_IGNORE },
  1474. { 0, 0 }
  1475. };
  1476. /*
  1477. * Traverse the supplied list of reports and find the longest
  1478. */
  1479. static void hid_find_max_report(struct hid_device *hid, unsigned int type, int *max)
  1480. {
  1481. struct hid_report *report;
  1482. int size;
  1483. list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
  1484. size = ((report->size - 1) >> 3) + 1;
  1485. if (type == HID_INPUT_REPORT && hid->report_enum[type].numbered)
  1486. size++;
  1487. if (*max < size)
  1488. *max = size;
  1489. }
  1490. }
  1491. static int hid_alloc_buffers(struct usb_device *dev, struct hid_device *hid)
  1492. {
  1493. if (!(hid->inbuf = usb_buffer_alloc(dev, hid->bufsize, SLAB_ATOMIC, &hid->inbuf_dma)))
  1494. return -1;
  1495. if (!(hid->outbuf = usb_buffer_alloc(dev, hid->bufsize, SLAB_ATOMIC, &hid->outbuf_dma)))
  1496. return -1;
  1497. if (!(hid->cr = usb_buffer_alloc(dev, sizeof(*(hid->cr)), SLAB_ATOMIC, &hid->cr_dma)))
  1498. return -1;
  1499. if (!(hid->ctrlbuf = usb_buffer_alloc(dev, hid->bufsize, SLAB_ATOMIC, &hid->ctrlbuf_dma)))
  1500. return -1;
  1501. return 0;
  1502. }
  1503. static void hid_free_buffers(struct usb_device *dev, struct hid_device *hid)
  1504. {
  1505. if (hid->inbuf)
  1506. usb_buffer_free(dev, hid->bufsize, hid->inbuf, hid->inbuf_dma);
  1507. if (hid->outbuf)
  1508. usb_buffer_free(dev, hid->bufsize, hid->outbuf, hid->outbuf_dma);
  1509. if (hid->cr)
  1510. usb_buffer_free(dev, sizeof(*(hid->cr)), hid->cr, hid->cr_dma);
  1511. if (hid->ctrlbuf)
  1512. usb_buffer_free(dev, hid->bufsize, hid->ctrlbuf, hid->ctrlbuf_dma);
  1513. }
  1514. /*
  1515. * Cherry Cymotion keyboard have an invalid HID report descriptor,
  1516. * that needs fixing before we can parse it.
  1517. */
  1518. static void hid_fixup_cymotion_descriptor(char *rdesc, int rsize)
  1519. {
  1520. if (rsize >= 17 && rdesc[11] == 0x3c && rdesc[12] == 0x02) {
  1521. info("Fixing up Cherry Cymotion report descriptor");
  1522. rdesc[11] = rdesc[16] = 0xff;
  1523. rdesc[12] = rdesc[17] = 0x03;
  1524. }
  1525. }
  1526. static struct hid_device *usb_hid_configure(struct usb_interface *intf)
  1527. {
  1528. struct usb_host_interface *interface = intf->cur_altsetting;
  1529. struct usb_device *dev = interface_to_usbdev (intf);
  1530. struct hid_descriptor *hdesc;
  1531. struct hid_device *hid;
  1532. unsigned quirks = 0, rsize = 0;
  1533. char *rdesc;
  1534. int n, len, insize = 0;
  1535. /* Ignore all Wacom devices */
  1536. if (le16_to_cpu(dev->descriptor.idVendor) == USB_VENDOR_ID_WACOM)
  1537. return NULL;
  1538. for (n = 0; hid_blacklist[n].idVendor; n++)
  1539. if ((hid_blacklist[n].idVendor == le16_to_cpu(dev->descriptor.idVendor)) &&
  1540. (hid_blacklist[n].idProduct == le16_to_cpu(dev->descriptor.idProduct)))
  1541. quirks = hid_blacklist[n].quirks;
  1542. /* Many keyboards and mice don't like to be polled for reports,
  1543. * so we will always set the HID_QUIRK_NOGET flag for them. */
  1544. if (interface->desc.bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT) {
  1545. if (interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_KEYBOARD ||
  1546. interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_MOUSE)
  1547. quirks |= HID_QUIRK_NOGET;
  1548. }
  1549. if (quirks & HID_QUIRK_IGNORE)
  1550. return NULL;
  1551. if (usb_get_extra_descriptor(interface, HID_DT_HID, &hdesc) &&
  1552. (!interface->desc.bNumEndpoints ||
  1553. usb_get_extra_descriptor(&interface->endpoint[0], HID_DT_HID, &hdesc))) {
  1554. dbg("class descriptor not present\n");
  1555. return NULL;
  1556. }
  1557. for (n = 0; n < hdesc->bNumDescriptors; n++)
  1558. if (hdesc->desc[n].bDescriptorType == HID_DT_REPORT)
  1559. rsize = le16_to_cpu(hdesc->desc[n].wDescriptorLength);
  1560. if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
  1561. dbg("weird size of report descriptor (%u)", rsize);
  1562. return NULL;
  1563. }
  1564. if (!(rdesc = kmalloc(rsize, GFP_KERNEL))) {
  1565. dbg("couldn't allocate rdesc memory");
  1566. return NULL;
  1567. }
  1568. hid_set_idle(dev, interface->desc.bInterfaceNumber, 0, 0);
  1569. if ((n = hid_get_class_descriptor(dev, interface->desc.bInterfaceNumber, HID_DT_REPORT, rdesc, rsize)) < 0) {
  1570. dbg("reading report descriptor failed");
  1571. kfree(rdesc);
  1572. return NULL;
  1573. }
  1574. if ((quirks & HID_QUIRK_CYMOTION))
  1575. hid_fixup_cymotion_descriptor(rdesc, rsize);
  1576. #ifdef DEBUG_DATA
  1577. printk(KERN_DEBUG __FILE__ ": report descriptor (size %u, read %d) = ", rsize, n);
  1578. for (n = 0; n < rsize; n++)
  1579. printk(" %02x", (unsigned char) rdesc[n]);
  1580. printk("\n");
  1581. #endif
  1582. if (!(hid = hid_parse_report(rdesc, n))) {
  1583. dbg("parsing report descriptor failed");
  1584. kfree(rdesc);
  1585. return NULL;
  1586. }
  1587. kfree(rdesc);
  1588. hid->quirks = quirks;
  1589. hid->bufsize = HID_MIN_BUFFER_SIZE;
  1590. hid_find_max_report(hid, HID_INPUT_REPORT, &hid->bufsize);
  1591. hid_find_max_report(hid, HID_OUTPUT_REPORT, &hid->bufsize);
  1592. hid_find_max_report(hid, HID_FEATURE_REPORT, &hid->bufsize);
  1593. if (hid->bufsize > HID_MAX_BUFFER_SIZE)
  1594. hid->bufsize = HID_MAX_BUFFER_SIZE;
  1595. hid_find_max_report(hid, HID_INPUT_REPORT, &insize);
  1596. if (insize > HID_MAX_BUFFER_SIZE)
  1597. insize = HID_MAX_BUFFER_SIZE;
  1598. if (hid_alloc_buffers(dev, hid)) {
  1599. hid_free_buffers(dev, hid);
  1600. goto fail;
  1601. }
  1602. for (n = 0; n < interface->desc.bNumEndpoints; n++) {
  1603. struct usb_endpoint_descriptor *endpoint;
  1604. int pipe;
  1605. int interval;
  1606. endpoint = &interface->endpoint[n].desc;
  1607. if ((endpoint->bmAttributes & 3) != 3) /* Not an interrupt endpoint */
  1608. continue;
  1609. interval = endpoint->bInterval;
  1610. /* Change the polling interval of mice. */
  1611. if (hid->collection->usage == HID_GD_MOUSE && hid_mousepoll_interval > 0)
  1612. interval = hid_mousepoll_interval;
  1613. if (endpoint->bEndpointAddress & USB_DIR_IN) {
  1614. if (hid->urbin)
  1615. continue;
  1616. if (!(hid->urbin = usb_alloc_urb(0, GFP_KERNEL)))
  1617. goto fail;
  1618. pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
  1619. usb_fill_int_urb(hid->urbin, dev, pipe, hid->inbuf, insize,
  1620. hid_irq_in, hid, interval);
  1621. hid->urbin->transfer_dma = hid->inbuf_dma;
  1622. hid->urbin->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  1623. } else {
  1624. if (hid->urbout)
  1625. continue;
  1626. if (!(hid->urbout = usb_alloc_urb(0, GFP_KERNEL)))
  1627. goto fail;
  1628. pipe = usb_sndintpipe(dev, endpoint->bEndpointAddress);
  1629. usb_fill_int_urb(hid->urbout, dev, pipe, hid->outbuf, 0,
  1630. hid_irq_out, hid, interval);
  1631. hid->urbout->transfer_dma = hid->outbuf_dma;
  1632. hid->urbout->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  1633. }
  1634. }
  1635. if (!hid->urbin) {
  1636. err("couldn't find an input interrupt endpoint");
  1637. goto fail;
  1638. }
  1639. init_waitqueue_head(&hid->wait);
  1640. INIT_WORK(&hid->reset_work, hid_reset, hid);
  1641. setup_timer(&hid->io_retry, hid_retry_timeout, (unsigned long) hid);
  1642. spin_lock_init(&hid->inlock);
  1643. spin_lock_init(&hid->outlock);
  1644. spin_lock_init(&hid->ctrllock);
  1645. hid->version = le16_to_cpu(hdesc->bcdHID);
  1646. hid->country = hdesc->bCountryCode;
  1647. hid->dev = dev;
  1648. hid->intf = intf;
  1649. hid->ifnum = interface->desc.bInterfaceNumber;
  1650. hid->name[0] = 0;
  1651. if (dev->manufacturer)
  1652. strlcpy(hid->name, dev->manufacturer, sizeof(hid->name));
  1653. if (dev->product) {
  1654. if (dev->manufacturer)
  1655. strlcat(hid->name, " ", sizeof(hid->name));
  1656. strlcat(hid->name, dev->product, sizeof(hid->name));
  1657. }
  1658. if (!strlen(hid->name))
  1659. snprintf(hid->name, sizeof(hid->name), "HID %04x:%04x",
  1660. le16_to_cpu(dev->descriptor.idVendor),
  1661. le16_to_cpu(dev->descriptor.idProduct));
  1662. usb_make_path(dev, hid->phys, sizeof(hid->phys));
  1663. strlcat(hid->phys, "/input", sizeof(hid->phys));
  1664. len = strlen(hid->phys);
  1665. if (len < sizeof(hid->phys) - 1)
  1666. snprintf(hid->phys + len, sizeof(hid->phys) - len,
  1667. "%d", intf->altsetting[0].desc.bInterfaceNumber);
  1668. if (usb_string(dev, dev->descriptor.iSerialNumber, hid->uniq, 64) <= 0)
  1669. hid->uniq[0] = 0;
  1670. hid->urbctrl = usb_alloc_urb(0, GFP_KERNEL);
  1671. if (!hid->urbctrl)
  1672. goto fail;
  1673. usb_fill_control_urb(hid->urbctrl, dev, 0, (void *) hid->cr,
  1674. hid->ctrlbuf, 1, hid_ctrl, hid);
  1675. hid->urbctrl->setup_dma = hid->cr_dma;
  1676. hid->urbctrl->transfer_dma = hid->ctrlbuf_dma;
  1677. hid->urbctrl->transfer_flags |= (URB_NO_TRANSFER_DMA_MAP | URB_NO_SETUP_DMA_MAP);
  1678. return hid;
  1679. fail:
  1680. if (hid->urbin)
  1681. usb_free_urb(hid->urbin);
  1682. if (hid->urbout)
  1683. usb_free_urb(hid->urbout);
  1684. if (hid->urbctrl)
  1685. usb_free_urb(hid->urbctrl);
  1686. hid_free_buffers(dev, hid);
  1687. hid_free_device(hid);
  1688. return NULL;
  1689. }
  1690. static void hid_disconnect(struct usb_interface *intf)
  1691. {
  1692. struct hid_device *hid = usb_get_intfdata (intf);
  1693. if (!hid)
  1694. return;
  1695. spin_lock_irq(&hid->inlock); /* Sync with error handler */
  1696. usb_set_intfdata(intf, NULL);
  1697. spin_unlock_irq(&hid->inlock);
  1698. usb_kill_urb(hid->urbin);
  1699. usb_kill_urb(hid->urbout);
  1700. usb_kill_urb(hid->urbctrl);
  1701. del_timer_sync(&hid->io_retry);
  1702. flush_scheduled_work();
  1703. if (hid->claimed & HID_CLAIMED_INPUT)
  1704. hidinput_disconnect(hid);
  1705. if (hid->claimed & HID_CLAIMED_HIDDEV)
  1706. hiddev_disconnect(hid);
  1707. usb_free_urb(hid->urbin);
  1708. usb_free_urb(hid->urbctrl);
  1709. if (hid->urbout)
  1710. usb_free_urb(hid->urbout);
  1711. hid_free_buffers(hid->dev, hid);
  1712. hid_free_device(hid);
  1713. }
  1714. static int hid_probe(struct usb_interface *intf, const struct usb_device_id *id)
  1715. {
  1716. struct hid_device *hid;
  1717. char path[64];
  1718. int i;
  1719. char *c;
  1720. dbg("HID probe called for ifnum %d",
  1721. intf->altsetting->desc.bInterfaceNumber);
  1722. if (!(hid = usb_hid_configure(intf)))
  1723. return -ENODEV;
  1724. hid_init_reports(hid);
  1725. hid_dump_device(hid);
  1726. if (!hidinput_connect(hid))
  1727. hid->claimed |= HID_CLAIMED_INPUT;
  1728. if (!hiddev_connect(hid))
  1729. hid->claimed |= HID_CLAIMED_HIDDEV;
  1730. usb_set_intfdata(intf, hid);
  1731. if (!hid->claimed) {
  1732. printk ("HID device not claimed by input or hiddev\n");
  1733. hid_disconnect(intf);
  1734. return -ENODEV;
  1735. }
  1736. printk(KERN_INFO);
  1737. if (hid->claimed & HID_CLAIMED_INPUT)
  1738. printk("input");
  1739. if (hid->claimed == (HID_CLAIMED_INPUT | HID_CLAIMED_HIDDEV))
  1740. printk(",");
  1741. if (hid->claimed & HID_CLAIMED_HIDDEV)
  1742. printk("hiddev%d", hid->minor);
  1743. c = "Device";
  1744. for (i = 0; i < hid->maxcollection; i++) {
  1745. if (hid->collection[i].type == HID_COLLECTION_APPLICATION &&
  1746. (hid->collection[i].usage & HID_USAGE_PAGE) == HID_UP_GENDESK &&
  1747. (hid->collection[i].usage & 0xffff) < ARRAY_SIZE(hid_types)) {
  1748. c = hid_types[hid->collection[i].usage & 0xffff];
  1749. break;
  1750. }
  1751. }
  1752. usb_make_path(interface_to_usbdev(intf), path, 63);
  1753. printk(": USB HID v%x.%02x %s [%s] on %s\n",
  1754. hid->version >> 8, hid->version & 0xff, c, hid->name, path);
  1755. return 0;
  1756. }
  1757. static int hid_suspend(struct usb_interface *intf, pm_message_t message)
  1758. {
  1759. struct hid_device *hid = usb_get_intfdata (intf);
  1760. spin_lock_irq(&hid->inlock); /* Sync with error handler */
  1761. set_bit(HID_SUSPENDED, &hid->iofl);
  1762. spin_unlock_irq(&hid->inlock);
  1763. del_timer(&hid->io_retry);
  1764. usb_kill_urb(hid->urbin);
  1765. dev_dbg(&intf->dev, "suspend\n");
  1766. return 0;
  1767. }
  1768. static int hid_resume(struct usb_interface *intf)
  1769. {
  1770. struct hid_device *hid = usb_get_intfdata (intf);
  1771. int status;
  1772. clear_bit(HID_SUSPENDED, &hid->iofl);
  1773. hid->retry_delay = 0;
  1774. status = hid_start_in(hid);
  1775. dev_dbg(&intf->dev, "resume status %d\n", status);
  1776. return status;
  1777. }
  1778. /* Treat USB reset pretty much the same as suspend/resume */
  1779. static void hid_pre_reset(struct usb_interface *intf)
  1780. {
  1781. /* FIXME: What if the interface is already suspended? */
  1782. hid_suspend(intf, PMSG_ON);
  1783. }
  1784. static void hid_post_reset(struct usb_interface *intf)
  1785. {
  1786. struct usb_device *dev = interface_to_usbdev (intf);
  1787. hid_set_idle(dev, intf->cur_altsetting->desc.bInterfaceNumber, 0, 0);
  1788. /* FIXME: Any more reinitialization needed? */
  1789. hid_resume(intf);
  1790. }
  1791. static struct usb_device_id hid_usb_ids [] = {
  1792. { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
  1793. .bInterfaceClass = USB_INTERFACE_CLASS_HID },
  1794. { } /* Terminating entry */
  1795. };
  1796. MODULE_DEVICE_TABLE (usb, hid_usb_ids);
  1797. static struct usb_driver hid_driver = {
  1798. .name = "usbhid",
  1799. .probe = hid_probe,
  1800. .disconnect = hid_disconnect,
  1801. .suspend = hid_suspend,
  1802. .resume = hid_resume,
  1803. .pre_reset = hid_pre_reset,
  1804. .post_reset = hid_post_reset,
  1805. .id_table = hid_usb_ids,
  1806. };
  1807. static int __init hid_init(void)
  1808. {
  1809. int retval;
  1810. retval = hiddev_init();
  1811. if (retval)
  1812. goto hiddev_init_fail;
  1813. retval = usb_register(&hid_driver);
  1814. if (retval)
  1815. goto usb_register_fail;
  1816. info(DRIVER_VERSION ":" DRIVER_DESC);
  1817. return 0;
  1818. usb_register_fail:
  1819. hiddev_exit();
  1820. hiddev_init_fail:
  1821. return retval;
  1822. }
  1823. static void __exit hid_exit(void)
  1824. {
  1825. usb_deregister(&hid_driver);
  1826. hiddev_exit();
  1827. }
  1828. module_init(hid_init);
  1829. module_exit(hid_exit);
  1830. MODULE_AUTHOR(DRIVER_AUTHOR);
  1831. MODULE_DESCRIPTION(DRIVER_DESC);
  1832. MODULE_LICENSE(DRIVER_LICENSE);