hid-core.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202
  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. hid_ff_exit(device);
  440. for (i = 0; i < HID_REPORT_TYPES; i++) {
  441. struct hid_report_enum *report_enum = device->report_enum + i;
  442. for (j = 0; j < 256; j++) {
  443. struct hid_report *report = report_enum->report_id_hash[j];
  444. if (report)
  445. hid_free_report(report);
  446. }
  447. }
  448. kfree(device->rdesc);
  449. kfree(device);
  450. }
  451. /*
  452. * Fetch a report description item from the data stream. We support long
  453. * items, though they are not used yet.
  454. */
  455. static u8 *fetch_item(__u8 *start, __u8 *end, struct hid_item *item)
  456. {
  457. u8 b;
  458. if ((end - start) <= 0)
  459. return NULL;
  460. b = *start++;
  461. item->type = (b >> 2) & 3;
  462. item->tag = (b >> 4) & 15;
  463. if (item->tag == HID_ITEM_TAG_LONG) {
  464. item->format = HID_ITEM_FORMAT_LONG;
  465. if ((end - start) < 2)
  466. return NULL;
  467. item->size = *start++;
  468. item->tag = *start++;
  469. if ((end - start) < item->size)
  470. return NULL;
  471. item->data.longdata = start;
  472. start += item->size;
  473. return start;
  474. }
  475. item->format = HID_ITEM_FORMAT_SHORT;
  476. item->size = b & 3;
  477. switch (item->size) {
  478. case 0:
  479. return start;
  480. case 1:
  481. if ((end - start) < 1)
  482. return NULL;
  483. item->data.u8 = *start++;
  484. return start;
  485. case 2:
  486. if ((end - start) < 2)
  487. return NULL;
  488. item->data.u16 = le16_to_cpu(get_unaligned((__le16*)start));
  489. start = (__u8 *)((__le16 *)start + 1);
  490. return start;
  491. case 3:
  492. item->size++;
  493. if ((end - start) < 4)
  494. return NULL;
  495. item->data.u32 = le32_to_cpu(get_unaligned((__le32*)start));
  496. start = (__u8 *)((__le32 *)start + 1);
  497. return start;
  498. }
  499. return NULL;
  500. }
  501. /*
  502. * Parse a report description into a hid_device structure. Reports are
  503. * enumerated, fields are attached to these reports.
  504. */
  505. static struct hid_device *hid_parse_report(__u8 *start, unsigned size)
  506. {
  507. struct hid_device *device;
  508. struct hid_parser *parser;
  509. struct hid_item item;
  510. __u8 *end;
  511. unsigned i;
  512. static int (*dispatch_type[])(struct hid_parser *parser,
  513. struct hid_item *item) = {
  514. hid_parser_main,
  515. hid_parser_global,
  516. hid_parser_local,
  517. hid_parser_reserved
  518. };
  519. if (!(device = kzalloc(sizeof(struct hid_device), GFP_KERNEL)))
  520. return NULL;
  521. if (!(device->collection = kzalloc(sizeof(struct hid_collection) *
  522. HID_DEFAULT_NUM_COLLECTIONS, GFP_KERNEL))) {
  523. kfree(device);
  524. return NULL;
  525. }
  526. device->collection_size = HID_DEFAULT_NUM_COLLECTIONS;
  527. for (i = 0; i < HID_REPORT_TYPES; i++)
  528. INIT_LIST_HEAD(&device->report_enum[i].report_list);
  529. if (!(device->rdesc = (__u8 *)kmalloc(size, GFP_KERNEL))) {
  530. kfree(device->collection);
  531. kfree(device);
  532. return NULL;
  533. }
  534. memcpy(device->rdesc, start, size);
  535. device->rsize = size;
  536. if (!(parser = kzalloc(sizeof(struct hid_parser), GFP_KERNEL))) {
  537. kfree(device->rdesc);
  538. kfree(device->collection);
  539. kfree(device);
  540. return NULL;
  541. }
  542. parser->device = device;
  543. end = start + size;
  544. while ((start = fetch_item(start, end, &item)) != NULL) {
  545. if (item.format != HID_ITEM_FORMAT_SHORT) {
  546. dbg("unexpected long global item");
  547. kfree(device->collection);
  548. hid_free_device(device);
  549. kfree(parser);
  550. return NULL;
  551. }
  552. if (dispatch_type[item.type](parser, &item)) {
  553. dbg("item %u %u %u %u parsing failed\n",
  554. item.format, (unsigned)item.size, (unsigned)item.type, (unsigned)item.tag);
  555. kfree(device->collection);
  556. hid_free_device(device);
  557. kfree(parser);
  558. return NULL;
  559. }
  560. if (start == end) {
  561. if (parser->collection_stack_ptr) {
  562. dbg("unbalanced collection at end of report description");
  563. kfree(device->collection);
  564. hid_free_device(device);
  565. kfree(parser);
  566. return NULL;
  567. }
  568. if (parser->local.delimiter_depth) {
  569. dbg("unbalanced delimiter at end of report description");
  570. kfree(device->collection);
  571. hid_free_device(device);
  572. kfree(parser);
  573. return NULL;
  574. }
  575. kfree(parser);
  576. return device;
  577. }
  578. }
  579. dbg("item fetching failed at offset %d\n", (int)(end - start));
  580. kfree(device->collection);
  581. hid_free_device(device);
  582. kfree(parser);
  583. return NULL;
  584. }
  585. /*
  586. * Convert a signed n-bit integer to signed 32-bit integer. Common
  587. * cases are done through the compiler, the screwed things has to be
  588. * done by hand.
  589. */
  590. static __inline__ __s32 snto32(__u32 value, unsigned n)
  591. {
  592. switch (n) {
  593. case 8: return ((__s8)value);
  594. case 16: return ((__s16)value);
  595. case 32: return ((__s32)value);
  596. }
  597. return value & (1 << (n - 1)) ? value | (-1 << n) : value;
  598. }
  599. /*
  600. * Convert a signed 32-bit integer to a signed n-bit integer.
  601. */
  602. static __inline__ __u32 s32ton(__s32 value, unsigned n)
  603. {
  604. __s32 a = value >> (n - 1);
  605. if (a && a != -1)
  606. return value < 0 ? 1 << (n - 1) : (1 << (n - 1)) - 1;
  607. return value & ((1 << n) - 1);
  608. }
  609. /*
  610. * Extract/implement a data field from/to a report.
  611. */
  612. static __inline__ __u32 extract(__u8 *report, unsigned offset, unsigned n)
  613. {
  614. report += (offset >> 5) << 2; offset &= 31;
  615. return (le64_to_cpu(get_unaligned((__le64*)report)) >> offset) & ((1ULL << n) - 1);
  616. }
  617. static __inline__ void implement(__u8 *report, unsigned offset, unsigned n, __u32 value)
  618. {
  619. report += (offset >> 5) << 2; offset &= 31;
  620. put_unaligned((get_unaligned((__le64*)report)
  621. & cpu_to_le64(~((((__u64) 1 << n) - 1) << offset)))
  622. | cpu_to_le64((__u64)value << offset), (__le64*)report);
  623. }
  624. /*
  625. * Search an array for a value.
  626. */
  627. static __inline__ int search(__s32 *array, __s32 value, unsigned n)
  628. {
  629. while (n--) {
  630. if (*array++ == value)
  631. return 0;
  632. }
  633. return -1;
  634. }
  635. static void hid_process_event(struct hid_device *hid, struct hid_field *field, struct hid_usage *usage, __s32 value, int interrupt, struct pt_regs *regs)
  636. {
  637. hid_dump_input(usage, value);
  638. if (hid->claimed & HID_CLAIMED_INPUT)
  639. hidinput_hid_event(hid, field, usage, value, regs);
  640. if (hid->claimed & HID_CLAIMED_HIDDEV && interrupt)
  641. hiddev_hid_event(hid, field, usage, value, regs);
  642. }
  643. /*
  644. * Analyse a received field, and fetch the data from it. The field
  645. * content is stored for next report processing (we do differential
  646. * reporting to the layer).
  647. */
  648. static void hid_input_field(struct hid_device *hid, struct hid_field *field, __u8 *data, int interrupt, struct pt_regs *regs)
  649. {
  650. unsigned n;
  651. unsigned count = field->report_count;
  652. unsigned offset = field->report_offset;
  653. unsigned size = field->report_size;
  654. __s32 min = field->logical_minimum;
  655. __s32 max = field->logical_maximum;
  656. __s32 *value;
  657. if (!(value = kmalloc(sizeof(__s32) * count, GFP_ATOMIC)))
  658. return;
  659. for (n = 0; n < count; n++) {
  660. value[n] = min < 0 ? snto32(extract(data, offset + n * size, size), size) :
  661. extract(data, offset + n * size, size);
  662. if (!(field->flags & HID_MAIN_ITEM_VARIABLE) /* Ignore report if ErrorRollOver */
  663. && value[n] >= min && value[n] <= max
  664. && field->usage[value[n] - min].hid == HID_UP_KEYBOARD + 1)
  665. goto exit;
  666. }
  667. for (n = 0; n < count; n++) {
  668. if (HID_MAIN_ITEM_VARIABLE & field->flags) {
  669. hid_process_event(hid, field, &field->usage[n], value[n], interrupt, regs);
  670. continue;
  671. }
  672. if (field->value[n] >= min && field->value[n] <= max
  673. && field->usage[field->value[n] - min].hid
  674. && search(value, field->value[n], count))
  675. hid_process_event(hid, field, &field->usage[field->value[n] - min], 0, interrupt, regs);
  676. if (value[n] >= min && value[n] <= max
  677. && field->usage[value[n] - min].hid
  678. && search(field->value, value[n], count))
  679. hid_process_event(hid, field, &field->usage[value[n] - min], 1, interrupt, regs);
  680. }
  681. memcpy(field->value, value, count * sizeof(__s32));
  682. exit:
  683. kfree(value);
  684. }
  685. static int hid_input_report(int type, struct urb *urb, int interrupt, struct pt_regs *regs)
  686. {
  687. struct hid_device *hid = urb->context;
  688. struct hid_report_enum *report_enum = hid->report_enum + type;
  689. u8 *data = urb->transfer_buffer;
  690. int len = urb->actual_length;
  691. struct hid_report *report;
  692. int n, size;
  693. if (!len) {
  694. dbg("empty report");
  695. return -1;
  696. }
  697. #ifdef DEBUG_DATA
  698. printk(KERN_DEBUG __FILE__ ": report (size %u) (%snumbered)\n", len, report_enum->numbered ? "" : "un");
  699. #endif
  700. n = 0; /* Normally report number is 0 */
  701. if (report_enum->numbered) { /* Device uses numbered reports, data[0] is report number */
  702. n = *data++;
  703. len--;
  704. }
  705. #ifdef DEBUG_DATA
  706. {
  707. int i;
  708. printk(KERN_DEBUG __FILE__ ": report %d (size %u) = ", n, len);
  709. for (i = 0; i < len; i++)
  710. printk(" %02x", data[i]);
  711. printk("\n");
  712. }
  713. #endif
  714. if (!(report = report_enum->report_id_hash[n])) {
  715. dbg("undefined report_id %d received", n);
  716. return -1;
  717. }
  718. size = ((report->size - 1) >> 3) + 1;
  719. if (len < size) {
  720. dbg("report %d is too short, (%d < %d)", report->id, len, size);
  721. memset(data + len, 0, size - len);
  722. }
  723. if (hid->claimed & HID_CLAIMED_HIDDEV)
  724. hiddev_report_event(hid, report);
  725. for (n = 0; n < report->maxfield; n++)
  726. hid_input_field(hid, report->field[n], data, interrupt, regs);
  727. if (hid->claimed & HID_CLAIMED_INPUT)
  728. hidinput_report_event(hid, report);
  729. return 0;
  730. }
  731. /*
  732. * Input submission and I/O error handler.
  733. */
  734. static void hid_io_error(struct hid_device *hid);
  735. /* Start up the input URB */
  736. static int hid_start_in(struct hid_device *hid)
  737. {
  738. unsigned long flags;
  739. int rc = 0;
  740. spin_lock_irqsave(&hid->inlock, flags);
  741. if (hid->open > 0 && !test_bit(HID_SUSPENDED, &hid->iofl) &&
  742. !test_and_set_bit(HID_IN_RUNNING, &hid->iofl)) {
  743. rc = usb_submit_urb(hid->urbin, GFP_ATOMIC);
  744. if (rc != 0)
  745. clear_bit(HID_IN_RUNNING, &hid->iofl);
  746. }
  747. spin_unlock_irqrestore(&hid->inlock, flags);
  748. return rc;
  749. }
  750. /* I/O retry timer routine */
  751. static void hid_retry_timeout(unsigned long _hid)
  752. {
  753. struct hid_device *hid = (struct hid_device *) _hid;
  754. dev_dbg(&hid->intf->dev, "retrying intr urb\n");
  755. if (hid_start_in(hid))
  756. hid_io_error(hid);
  757. }
  758. /* Workqueue routine to reset the device */
  759. static void hid_reset(void *_hid)
  760. {
  761. struct hid_device *hid = (struct hid_device *) _hid;
  762. int rc_lock, rc;
  763. dev_dbg(&hid->intf->dev, "resetting device\n");
  764. rc = rc_lock = usb_lock_device_for_reset(hid->dev, hid->intf);
  765. if (rc_lock >= 0) {
  766. rc = usb_reset_composite_device(hid->dev, hid->intf);
  767. if (rc_lock)
  768. usb_unlock_device(hid->dev);
  769. }
  770. clear_bit(HID_RESET_PENDING, &hid->iofl);
  771. switch (rc) {
  772. case 0:
  773. if (!test_bit(HID_IN_RUNNING, &hid->iofl))
  774. hid_io_error(hid);
  775. break;
  776. default:
  777. err("can't reset device, %s-%s/input%d, status %d",
  778. hid->dev->bus->bus_name,
  779. hid->dev->devpath,
  780. hid->ifnum, rc);
  781. /* FALLTHROUGH */
  782. case -EHOSTUNREACH:
  783. case -ENODEV:
  784. case -EINTR:
  785. break;
  786. }
  787. }
  788. /* Main I/O error handler */
  789. static void hid_io_error(struct hid_device *hid)
  790. {
  791. unsigned long flags;
  792. spin_lock_irqsave(&hid->inlock, flags);
  793. /* Stop when disconnected */
  794. if (usb_get_intfdata(hid->intf) == NULL)
  795. goto done;
  796. /* When an error occurs, retry at increasing intervals */
  797. if (hid->retry_delay == 0) {
  798. hid->retry_delay = 13; /* Then 26, 52, 104, 104, ... */
  799. hid->stop_retry = jiffies + msecs_to_jiffies(1000);
  800. } else if (hid->retry_delay < 100)
  801. hid->retry_delay *= 2;
  802. if (time_after(jiffies, hid->stop_retry)) {
  803. /* Retries failed, so do a port reset */
  804. if (!test_and_set_bit(HID_RESET_PENDING, &hid->iofl)) {
  805. if (schedule_work(&hid->reset_work))
  806. goto done;
  807. clear_bit(HID_RESET_PENDING, &hid->iofl);
  808. }
  809. }
  810. mod_timer(&hid->io_retry,
  811. jiffies + msecs_to_jiffies(hid->retry_delay));
  812. done:
  813. spin_unlock_irqrestore(&hid->inlock, flags);
  814. }
  815. /*
  816. * Input interrupt completion handler.
  817. */
  818. static void hid_irq_in(struct urb *urb, struct pt_regs *regs)
  819. {
  820. struct hid_device *hid = urb->context;
  821. int status;
  822. switch (urb->status) {
  823. case 0: /* success */
  824. hid->retry_delay = 0;
  825. hid_input_report(HID_INPUT_REPORT, urb, 1, regs);
  826. break;
  827. case -ECONNRESET: /* unlink */
  828. case -ENOENT:
  829. case -ESHUTDOWN: /* unplug */
  830. clear_bit(HID_IN_RUNNING, &hid->iofl);
  831. return;
  832. case -EILSEQ: /* protocol error or unplug */
  833. case -EPROTO: /* protocol error or unplug */
  834. case -ETIMEDOUT: /* NAK */
  835. clear_bit(HID_IN_RUNNING, &hid->iofl);
  836. hid_io_error(hid);
  837. return;
  838. default: /* error */
  839. warn("input irq status %d received", urb->status);
  840. }
  841. status = usb_submit_urb(urb, SLAB_ATOMIC);
  842. if (status) {
  843. clear_bit(HID_IN_RUNNING, &hid->iofl);
  844. if (status != -EPERM) {
  845. err("can't resubmit intr, %s-%s/input%d, status %d",
  846. hid->dev->bus->bus_name,
  847. hid->dev->devpath,
  848. hid->ifnum, status);
  849. hid_io_error(hid);
  850. }
  851. }
  852. }
  853. /*
  854. * Output the field into the report.
  855. */
  856. static void hid_output_field(struct hid_field *field, __u8 *data)
  857. {
  858. unsigned count = field->report_count;
  859. unsigned offset = field->report_offset;
  860. unsigned size = field->report_size;
  861. unsigned n;
  862. for (n = 0; n < count; n++) {
  863. if (field->logical_minimum < 0) /* signed values */
  864. implement(data, offset + n * size, size, s32ton(field->value[n], size));
  865. else /* unsigned values */
  866. implement(data, offset + n * size, size, field->value[n]);
  867. }
  868. }
  869. /*
  870. * Create a report.
  871. */
  872. static void hid_output_report(struct hid_report *report, __u8 *data)
  873. {
  874. unsigned n;
  875. if (report->id > 0)
  876. *data++ = report->id;
  877. for (n = 0; n < report->maxfield; n++)
  878. hid_output_field(report->field[n], data);
  879. }
  880. /*
  881. * Set a field value. The report this field belongs to has to be
  882. * created and transferred to the device, to set this value in the
  883. * device.
  884. */
  885. int hid_set_field(struct hid_field *field, unsigned offset, __s32 value)
  886. {
  887. unsigned size = field->report_size;
  888. hid_dump_input(field->usage + offset, value);
  889. if (offset >= field->report_count) {
  890. dbg("offset (%d) exceeds report_count (%d)", offset, field->report_count);
  891. hid_dump_field(field, 8);
  892. return -1;
  893. }
  894. if (field->logical_minimum < 0) {
  895. if (value != snto32(s32ton(value, size), size)) {
  896. dbg("value %d is out of range", value);
  897. return -1;
  898. }
  899. }
  900. field->value[offset] = value;
  901. return 0;
  902. }
  903. /*
  904. * Find a report field with a specified HID usage.
  905. */
  906. struct hid_field *hid_find_field_by_usage(struct hid_device *hid, __u32 wanted_usage, int type)
  907. {
  908. struct hid_report *report;
  909. int i;
  910. list_for_each_entry(report, &hid->report_enum[type].report_list, list)
  911. for (i = 0; i < report->maxfield; i++)
  912. if (report->field[i]->logical == wanted_usage)
  913. return report->field[i];
  914. return NULL;
  915. }
  916. static int hid_submit_out(struct hid_device *hid)
  917. {
  918. struct hid_report *report;
  919. report = hid->out[hid->outtail];
  920. hid_output_report(report, hid->outbuf);
  921. hid->urbout->transfer_buffer_length = ((report->size - 1) >> 3) + 1 + (report->id > 0);
  922. hid->urbout->dev = hid->dev;
  923. dbg("submitting out urb");
  924. if (usb_submit_urb(hid->urbout, GFP_ATOMIC)) {
  925. err("usb_submit_urb(out) failed");
  926. return -1;
  927. }
  928. return 0;
  929. }
  930. static int hid_submit_ctrl(struct hid_device *hid)
  931. {
  932. struct hid_report *report;
  933. unsigned char dir;
  934. int len;
  935. report = hid->ctrl[hid->ctrltail].report;
  936. dir = hid->ctrl[hid->ctrltail].dir;
  937. len = ((report->size - 1) >> 3) + 1 + (report->id > 0);
  938. if (dir == USB_DIR_OUT) {
  939. hid_output_report(report, hid->ctrlbuf);
  940. hid->urbctrl->pipe = usb_sndctrlpipe(hid->dev, 0);
  941. hid->urbctrl->transfer_buffer_length = len;
  942. } else {
  943. int maxpacket, padlen;
  944. hid->urbctrl->pipe = usb_rcvctrlpipe(hid->dev, 0);
  945. maxpacket = usb_maxpacket(hid->dev, hid->urbctrl->pipe, 0);
  946. if (maxpacket > 0) {
  947. padlen = (len + maxpacket - 1) / maxpacket;
  948. padlen *= maxpacket;
  949. if (padlen > hid->bufsize)
  950. padlen = hid->bufsize;
  951. } else
  952. padlen = 0;
  953. hid->urbctrl->transfer_buffer_length = padlen;
  954. }
  955. hid->urbctrl->dev = hid->dev;
  956. hid->cr->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE | dir;
  957. hid->cr->bRequest = (dir == USB_DIR_OUT) ? HID_REQ_SET_REPORT : HID_REQ_GET_REPORT;
  958. hid->cr->wValue = cpu_to_le16(((report->type + 1) << 8) | report->id);
  959. hid->cr->wIndex = cpu_to_le16(hid->ifnum);
  960. hid->cr->wLength = cpu_to_le16(len);
  961. dbg("submitting ctrl urb: %s wValue=0x%04x wIndex=0x%04x wLength=%u",
  962. hid->cr->bRequest == HID_REQ_SET_REPORT ? "Set_Report" : "Get_Report",
  963. hid->cr->wValue, hid->cr->wIndex, hid->cr->wLength);
  964. if (usb_submit_urb(hid->urbctrl, GFP_ATOMIC)) {
  965. err("usb_submit_urb(ctrl) failed");
  966. return -1;
  967. }
  968. return 0;
  969. }
  970. /*
  971. * Output interrupt completion handler.
  972. */
  973. static void hid_irq_out(struct urb *urb, struct pt_regs *regs)
  974. {
  975. struct hid_device *hid = urb->context;
  976. unsigned long flags;
  977. int unplug = 0;
  978. switch (urb->status) {
  979. case 0: /* success */
  980. break;
  981. case -ESHUTDOWN: /* unplug */
  982. unplug = 1;
  983. case -EILSEQ: /* protocol error or unplug */
  984. case -EPROTO: /* protocol error or unplug */
  985. case -ECONNRESET: /* unlink */
  986. case -ENOENT:
  987. break;
  988. default: /* error */
  989. warn("output irq status %d received", urb->status);
  990. }
  991. spin_lock_irqsave(&hid->outlock, flags);
  992. if (unplug)
  993. hid->outtail = hid->outhead;
  994. else
  995. hid->outtail = (hid->outtail + 1) & (HID_OUTPUT_FIFO_SIZE - 1);
  996. if (hid->outhead != hid->outtail) {
  997. if (hid_submit_out(hid)) {
  998. clear_bit(HID_OUT_RUNNING, &hid->iofl);
  999. wake_up(&hid->wait);
  1000. }
  1001. spin_unlock_irqrestore(&hid->outlock, flags);
  1002. return;
  1003. }
  1004. clear_bit(HID_OUT_RUNNING, &hid->iofl);
  1005. spin_unlock_irqrestore(&hid->outlock, flags);
  1006. wake_up(&hid->wait);
  1007. }
  1008. /*
  1009. * Control pipe completion handler.
  1010. */
  1011. static void hid_ctrl(struct urb *urb, struct pt_regs *regs)
  1012. {
  1013. struct hid_device *hid = urb->context;
  1014. unsigned long flags;
  1015. int unplug = 0;
  1016. spin_lock_irqsave(&hid->ctrllock, flags);
  1017. switch (urb->status) {
  1018. case 0: /* success */
  1019. if (hid->ctrl[hid->ctrltail].dir == USB_DIR_IN)
  1020. hid_input_report(hid->ctrl[hid->ctrltail].report->type, urb, 0, regs);
  1021. break;
  1022. case -ESHUTDOWN: /* unplug */
  1023. unplug = 1;
  1024. case -EILSEQ: /* protocol error or unplug */
  1025. case -EPROTO: /* protocol error or unplug */
  1026. case -ECONNRESET: /* unlink */
  1027. case -ENOENT:
  1028. case -EPIPE: /* report not available */
  1029. break;
  1030. default: /* error */
  1031. warn("ctrl urb status %d received", urb->status);
  1032. }
  1033. if (unplug)
  1034. hid->ctrltail = hid->ctrlhead;
  1035. else
  1036. hid->ctrltail = (hid->ctrltail + 1) & (HID_CONTROL_FIFO_SIZE - 1);
  1037. if (hid->ctrlhead != hid->ctrltail) {
  1038. if (hid_submit_ctrl(hid)) {
  1039. clear_bit(HID_CTRL_RUNNING, &hid->iofl);
  1040. wake_up(&hid->wait);
  1041. }
  1042. spin_unlock_irqrestore(&hid->ctrllock, flags);
  1043. return;
  1044. }
  1045. clear_bit(HID_CTRL_RUNNING, &hid->iofl);
  1046. spin_unlock_irqrestore(&hid->ctrllock, flags);
  1047. wake_up(&hid->wait);
  1048. }
  1049. void hid_submit_report(struct hid_device *hid, struct hid_report *report, unsigned char dir)
  1050. {
  1051. int head;
  1052. unsigned long flags;
  1053. if ((hid->quirks & HID_QUIRK_NOGET) && dir == USB_DIR_IN)
  1054. return;
  1055. if (hid->urbout && dir == USB_DIR_OUT && report->type == HID_OUTPUT_REPORT) {
  1056. spin_lock_irqsave(&hid->outlock, flags);
  1057. if ((head = (hid->outhead + 1) & (HID_OUTPUT_FIFO_SIZE - 1)) == hid->outtail) {
  1058. spin_unlock_irqrestore(&hid->outlock, flags);
  1059. warn("output queue full");
  1060. return;
  1061. }
  1062. hid->out[hid->outhead] = report;
  1063. hid->outhead = head;
  1064. if (!test_and_set_bit(HID_OUT_RUNNING, &hid->iofl))
  1065. if (hid_submit_out(hid))
  1066. clear_bit(HID_OUT_RUNNING, &hid->iofl);
  1067. spin_unlock_irqrestore(&hid->outlock, flags);
  1068. return;
  1069. }
  1070. spin_lock_irqsave(&hid->ctrllock, flags);
  1071. if ((head = (hid->ctrlhead + 1) & (HID_CONTROL_FIFO_SIZE - 1)) == hid->ctrltail) {
  1072. spin_unlock_irqrestore(&hid->ctrllock, flags);
  1073. warn("control queue full");
  1074. return;
  1075. }
  1076. hid->ctrl[hid->ctrlhead].report = report;
  1077. hid->ctrl[hid->ctrlhead].dir = dir;
  1078. hid->ctrlhead = head;
  1079. if (!test_and_set_bit(HID_CTRL_RUNNING, &hid->iofl))
  1080. if (hid_submit_ctrl(hid))
  1081. clear_bit(HID_CTRL_RUNNING, &hid->iofl);
  1082. spin_unlock_irqrestore(&hid->ctrllock, flags);
  1083. }
  1084. int hid_wait_io(struct hid_device *hid)
  1085. {
  1086. if (!wait_event_timeout(hid->wait, (!test_bit(HID_CTRL_RUNNING, &hid->iofl) &&
  1087. !test_bit(HID_OUT_RUNNING, &hid->iofl)),
  1088. 10*HZ)) {
  1089. dbg("timeout waiting for ctrl or out queue to clear");
  1090. return -1;
  1091. }
  1092. return 0;
  1093. }
  1094. static int hid_set_idle(struct usb_device *dev, int ifnum, int report, int idle)
  1095. {
  1096. return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  1097. HID_REQ_SET_IDLE, USB_TYPE_CLASS | USB_RECIP_INTERFACE, (idle << 8) | report,
  1098. ifnum, NULL, 0, USB_CTRL_SET_TIMEOUT);
  1099. }
  1100. static int hid_get_class_descriptor(struct usb_device *dev, int ifnum,
  1101. unsigned char type, void *buf, int size)
  1102. {
  1103. int result, retries = 4;
  1104. memset(buf,0,size); // Make sure we parse really received data
  1105. do {
  1106. result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  1107. USB_REQ_GET_DESCRIPTOR, USB_RECIP_INTERFACE | USB_DIR_IN,
  1108. (type << 8), ifnum, buf, size, USB_CTRL_GET_TIMEOUT);
  1109. retries--;
  1110. } while (result < size && retries);
  1111. return result;
  1112. }
  1113. int hid_open(struct hid_device *hid)
  1114. {
  1115. ++hid->open;
  1116. if (hid_start_in(hid))
  1117. hid_io_error(hid);
  1118. return 0;
  1119. }
  1120. void hid_close(struct hid_device *hid)
  1121. {
  1122. if (!--hid->open)
  1123. usb_kill_urb(hid->urbin);
  1124. }
  1125. #define USB_VENDOR_ID_PANJIT 0x134c
  1126. /*
  1127. * Initialize all reports
  1128. */
  1129. void hid_init_reports(struct hid_device *hid)
  1130. {
  1131. struct hid_report *report;
  1132. int err, ret;
  1133. list_for_each_entry(report, &hid->report_enum[HID_INPUT_REPORT].report_list, list)
  1134. hid_submit_report(hid, report, USB_DIR_IN);
  1135. list_for_each_entry(report, &hid->report_enum[HID_FEATURE_REPORT].report_list, list)
  1136. hid_submit_report(hid, report, USB_DIR_IN);
  1137. err = 0;
  1138. ret = hid_wait_io(hid);
  1139. while (ret) {
  1140. err |= ret;
  1141. if (test_bit(HID_CTRL_RUNNING, &hid->iofl))
  1142. usb_kill_urb(hid->urbctrl);
  1143. if (test_bit(HID_OUT_RUNNING, &hid->iofl))
  1144. usb_kill_urb(hid->urbout);
  1145. ret = hid_wait_io(hid);
  1146. }
  1147. if (err)
  1148. warn("timeout initializing reports");
  1149. }
  1150. #define USB_VENDOR_ID_GTCO 0x078c
  1151. #define USB_DEVICE_ID_GTCO_90 0x0090
  1152. #define USB_DEVICE_ID_GTCO_100 0x0100
  1153. #define USB_DEVICE_ID_GTCO_101 0x0101
  1154. #define USB_DEVICE_ID_GTCO_103 0x0103
  1155. #define USB_DEVICE_ID_GTCO_104 0x0104
  1156. #define USB_DEVICE_ID_GTCO_105 0x0105
  1157. #define USB_DEVICE_ID_GTCO_106 0x0106
  1158. #define USB_DEVICE_ID_GTCO_107 0x0107
  1159. #define USB_DEVICE_ID_GTCO_108 0x0108
  1160. #define USB_DEVICE_ID_GTCO_200 0x0200
  1161. #define USB_DEVICE_ID_GTCO_201 0x0201
  1162. #define USB_DEVICE_ID_GTCO_202 0x0202
  1163. #define USB_DEVICE_ID_GTCO_203 0x0203
  1164. #define USB_DEVICE_ID_GTCO_204 0x0204
  1165. #define USB_DEVICE_ID_GTCO_205 0x0205
  1166. #define USB_DEVICE_ID_GTCO_206 0x0206
  1167. #define USB_DEVICE_ID_GTCO_207 0x0207
  1168. #define USB_DEVICE_ID_GTCO_300 0x0300
  1169. #define USB_DEVICE_ID_GTCO_301 0x0301
  1170. #define USB_DEVICE_ID_GTCO_302 0x0302
  1171. #define USB_DEVICE_ID_GTCO_303 0x0303
  1172. #define USB_DEVICE_ID_GTCO_304 0x0304
  1173. #define USB_DEVICE_ID_GTCO_305 0x0305
  1174. #define USB_DEVICE_ID_GTCO_306 0x0306
  1175. #define USB_DEVICE_ID_GTCO_307 0x0307
  1176. #define USB_DEVICE_ID_GTCO_308 0x0308
  1177. #define USB_DEVICE_ID_GTCO_309 0x0309
  1178. #define USB_DEVICE_ID_GTCO_400 0x0400
  1179. #define USB_DEVICE_ID_GTCO_401 0x0401
  1180. #define USB_DEVICE_ID_GTCO_402 0x0402
  1181. #define USB_DEVICE_ID_GTCO_403 0x0403
  1182. #define USB_DEVICE_ID_GTCO_404 0x0404
  1183. #define USB_DEVICE_ID_GTCO_405 0x0405
  1184. #define USB_DEVICE_ID_GTCO_500 0x0500
  1185. #define USB_DEVICE_ID_GTCO_501 0x0501
  1186. #define USB_DEVICE_ID_GTCO_502 0x0502
  1187. #define USB_DEVICE_ID_GTCO_503 0x0503
  1188. #define USB_DEVICE_ID_GTCO_504 0x0504
  1189. #define USB_DEVICE_ID_GTCO_1000 0x1000
  1190. #define USB_DEVICE_ID_GTCO_1001 0x1001
  1191. #define USB_DEVICE_ID_GTCO_1002 0x1002
  1192. #define USB_DEVICE_ID_GTCO_1003 0x1003
  1193. #define USB_DEVICE_ID_GTCO_1004 0x1004
  1194. #define USB_DEVICE_ID_GTCO_1005 0x1005
  1195. #define USB_DEVICE_ID_GTCO_1006 0x1006
  1196. #define USB_VENDOR_ID_WACOM 0x056a
  1197. #define USB_VENDOR_ID_ACECAD 0x0460
  1198. #define USB_DEVICE_ID_ACECAD_FLAIR 0x0004
  1199. #define USB_DEVICE_ID_ACECAD_302 0x0008
  1200. #define USB_VENDOR_ID_KBGEAR 0x084e
  1201. #define USB_DEVICE_ID_KBGEAR_JAMSTUDIO 0x1001
  1202. #define USB_VENDOR_ID_AIPTEK 0x08ca
  1203. #define USB_DEVICE_ID_AIPTEK_01 0x0001
  1204. #define USB_DEVICE_ID_AIPTEK_10 0x0010
  1205. #define USB_DEVICE_ID_AIPTEK_20 0x0020
  1206. #define USB_DEVICE_ID_AIPTEK_21 0x0021
  1207. #define USB_DEVICE_ID_AIPTEK_22 0x0022
  1208. #define USB_DEVICE_ID_AIPTEK_23 0x0023
  1209. #define USB_DEVICE_ID_AIPTEK_24 0x0024
  1210. #define USB_VENDOR_ID_GRIFFIN 0x077d
  1211. #define USB_DEVICE_ID_POWERMATE 0x0410
  1212. #define USB_DEVICE_ID_SOUNDKNOB 0x04AA
  1213. #define USB_VENDOR_ID_ATEN 0x0557
  1214. #define USB_DEVICE_ID_ATEN_UC100KM 0x2004
  1215. #define USB_DEVICE_ID_ATEN_CS124U 0x2202
  1216. #define USB_DEVICE_ID_ATEN_2PORTKVM 0x2204
  1217. #define USB_DEVICE_ID_ATEN_4PORTKVM 0x2205
  1218. #define USB_DEVICE_ID_ATEN_4PORTKVMC 0x2208
  1219. #define USB_VENDOR_ID_TOPMAX 0x0663
  1220. #define USB_DEVICE_ID_TOPMAX_COBRAPAD 0x0103
  1221. #define USB_VENDOR_ID_HAPP 0x078b
  1222. #define USB_DEVICE_ID_UGCI_DRIVING 0x0010
  1223. #define USB_DEVICE_ID_UGCI_FLYING 0x0020
  1224. #define USB_DEVICE_ID_UGCI_FIGHTING 0x0030
  1225. #define USB_VENDOR_ID_MGE 0x0463
  1226. #define USB_DEVICE_ID_MGE_UPS 0xffff
  1227. #define USB_DEVICE_ID_MGE_UPS1 0x0001
  1228. #define USB_VENDOR_ID_ONTRAK 0x0a07
  1229. #define USB_DEVICE_ID_ONTRAK_ADU100 0x0064
  1230. #define USB_VENDOR_ID_ESSENTIAL_REALITY 0x0d7f
  1231. #define USB_DEVICE_ID_ESSENTIAL_REALITY_P5 0x0100
  1232. #define USB_VENDOR_ID_A4TECH 0x09da
  1233. #define USB_DEVICE_ID_A4TECH_WCP32PU 0x0006
  1234. #define USB_VENDOR_ID_AASHIMA 0x06d6
  1235. #define USB_DEVICE_ID_AASHIMA_GAMEPAD 0x0025
  1236. #define USB_DEVICE_ID_AASHIMA_PREDATOR 0x0026
  1237. #define USB_VENDOR_ID_CYPRESS 0x04b4
  1238. #define USB_DEVICE_ID_CYPRESS_MOUSE 0x0001
  1239. #define USB_DEVICE_ID_CYPRESS_HIDCOM 0x5500
  1240. #define USB_DEVICE_ID_CYPRESS_ULTRAMOUSE 0x7417
  1241. #define USB_VENDOR_ID_BERKSHIRE 0x0c98
  1242. #define USB_DEVICE_ID_BERKSHIRE_PCWD 0x1140
  1243. #define USB_VENDOR_ID_ALPS 0x0433
  1244. #define USB_DEVICE_ID_IBM_GAMEPAD 0x1101
  1245. #define USB_VENDOR_ID_SAITEK 0x06a3
  1246. #define USB_DEVICE_ID_SAITEK_RUMBLEPAD 0xff17
  1247. #define USB_VENDOR_ID_NEC 0x073e
  1248. #define USB_DEVICE_ID_NEC_USB_GAME_PAD 0x0301
  1249. #define USB_VENDOR_ID_CHIC 0x05fe
  1250. #define USB_DEVICE_ID_CHIC_GAMEPAD 0x0014
  1251. #define USB_VENDOR_ID_GLAB 0x06c2
  1252. #define USB_DEVICE_ID_4_PHIDGETSERVO_30 0x0038
  1253. #define USB_DEVICE_ID_1_PHIDGETSERVO_30 0x0039
  1254. #define USB_DEVICE_ID_0_0_4_IF_KIT 0x0040
  1255. #define USB_DEVICE_ID_0_16_16_IF_KIT 0x0044
  1256. #define USB_DEVICE_ID_8_8_8_IF_KIT 0x0045
  1257. #define USB_DEVICE_ID_0_8_7_IF_KIT 0x0051
  1258. #define USB_DEVICE_ID_0_8_8_IF_KIT 0x0053
  1259. #define USB_DEVICE_ID_PHIDGET_MOTORCONTROL 0x0058
  1260. #define USB_VENDOR_ID_WISEGROUP 0x0925
  1261. #define USB_DEVICE_ID_1_PHIDGETSERVO_20 0x8101
  1262. #define USB_DEVICE_ID_4_PHIDGETSERVO_20 0x8104
  1263. #define USB_DEVICE_ID_8_8_4_IF_KIT 0x8201
  1264. #define USB_DEVICE_ID_DUAL_USB_JOYPAD 0x8866
  1265. #define USB_VENDOR_ID_WISEGROUP_LTD 0x6677
  1266. #define USB_DEVICE_ID_SMARTJOY_DUAL_PLUS 0x8802
  1267. #define USB_VENDOR_ID_CODEMERCS 0x07c0
  1268. #define USB_DEVICE_ID_CODEMERCS_IOW40 0x1500
  1269. #define USB_DEVICE_ID_CODEMERCS_IOW24 0x1501
  1270. #define USB_DEVICE_ID_CODEMERCS_IOW48 0x1502
  1271. #define USB_DEVICE_ID_CODEMERCS_IOW28 0x1503
  1272. #define USB_VENDOR_ID_DELORME 0x1163
  1273. #define USB_DEVICE_ID_DELORME_EARTHMATE 0x0100
  1274. #define USB_DEVICE_ID_DELORME_EM_LT20 0x0200
  1275. #define USB_VENDOR_ID_MCC 0x09db
  1276. #define USB_DEVICE_ID_MCC_PMD1024LS 0x0076
  1277. #define USB_DEVICE_ID_MCC_PMD1208LS 0x007a
  1278. #define USB_VENDOR_ID_VERNIER 0x08f7
  1279. #define USB_DEVICE_ID_VERNIER_LABPRO 0x0001
  1280. #define USB_DEVICE_ID_VERNIER_GOTEMP 0x0002
  1281. #define USB_DEVICE_ID_VERNIER_SKIP 0x0003
  1282. #define USB_DEVICE_ID_VERNIER_CYCLOPS 0x0004
  1283. #define USB_VENDOR_ID_LD 0x0f11
  1284. #define USB_DEVICE_ID_LD_CASSY 0x1000
  1285. #define USB_DEVICE_ID_LD_POCKETCASSY 0x1010
  1286. #define USB_DEVICE_ID_LD_MOBILECASSY 0x1020
  1287. #define USB_DEVICE_ID_LD_JWM 0x1080
  1288. #define USB_DEVICE_ID_LD_DMMP 0x1081
  1289. #define USB_DEVICE_ID_LD_UMIP 0x1090
  1290. #define USB_DEVICE_ID_LD_XRAY1 0x1100
  1291. #define USB_DEVICE_ID_LD_XRAY2 0x1101
  1292. #define USB_DEVICE_ID_LD_VIDEOCOM 0x1200
  1293. #define USB_DEVICE_ID_LD_COM3LAB 0x2000
  1294. #define USB_DEVICE_ID_LD_TELEPORT 0x2010
  1295. #define USB_DEVICE_ID_LD_NETWORKANALYSER 0x2020
  1296. #define USB_DEVICE_ID_LD_POWERCONTROL 0x2030
  1297. #define USB_DEVICE_ID_LD_MACHINETEST 0x2040
  1298. #define USB_VENDOR_ID_APPLE 0x05ac
  1299. #define USB_DEVICE_ID_APPLE_MIGHTYMOUSE 0x0304
  1300. #define USB_VENDOR_ID_CHERRY 0x046a
  1301. #define USB_DEVICE_ID_CHERRY_CYMOTION 0x0023
  1302. #define USB_VENDOR_ID_YEALINK 0x6993
  1303. #define USB_DEVICE_ID_YEALINK_P1K_P4K_B2K 0xb001
  1304. /*
  1305. * Alphabetically sorted blacklist by quirk type.
  1306. */
  1307. static const struct hid_blacklist {
  1308. __u16 idVendor;
  1309. __u16 idProduct;
  1310. unsigned quirks;
  1311. } hid_blacklist[] = {
  1312. { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_01, HID_QUIRK_IGNORE },
  1313. { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_10, HID_QUIRK_IGNORE },
  1314. { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_20, HID_QUIRK_IGNORE },
  1315. { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_21, HID_QUIRK_IGNORE },
  1316. { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_22, HID_QUIRK_IGNORE },
  1317. { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_23, HID_QUIRK_IGNORE },
  1318. { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_24, HID_QUIRK_IGNORE },
  1319. { USB_VENDOR_ID_BERKSHIRE, USB_DEVICE_ID_BERKSHIRE_PCWD, HID_QUIRK_IGNORE },
  1320. { USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW40, HID_QUIRK_IGNORE },
  1321. { USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW24, HID_QUIRK_IGNORE },
  1322. { USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW48, HID_QUIRK_IGNORE },
  1323. { USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW28, HID_QUIRK_IGNORE },
  1324. { USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_HIDCOM, HID_QUIRK_IGNORE },
  1325. { USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_ULTRAMOUSE, HID_QUIRK_IGNORE },
  1326. { USB_VENDOR_ID_DELORME, USB_DEVICE_ID_DELORME_EARTHMATE, HID_QUIRK_IGNORE },
  1327. { USB_VENDOR_ID_DELORME, USB_DEVICE_ID_DELORME_EM_LT20, HID_QUIRK_IGNORE },
  1328. { USB_VENDOR_ID_ESSENTIAL_REALITY, USB_DEVICE_ID_ESSENTIAL_REALITY_P5, HID_QUIRK_IGNORE },
  1329. { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_4_PHIDGETSERVO_30, HID_QUIRK_IGNORE },
  1330. { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_1_PHIDGETSERVO_30, HID_QUIRK_IGNORE },
  1331. { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_0_4_IF_KIT, HID_QUIRK_IGNORE },
  1332. { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_16_16_IF_KIT, HID_QUIRK_IGNORE },
  1333. { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_8_8_8_IF_KIT, HID_QUIRK_IGNORE },
  1334. { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_8_7_IF_KIT, HID_QUIRK_IGNORE },
  1335. { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_8_8_IF_KIT, HID_QUIRK_IGNORE },
  1336. { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_PHIDGET_MOTORCONTROL, HID_QUIRK_IGNORE },
  1337. { USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_POWERMATE, HID_QUIRK_IGNORE },
  1338. { USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_SOUNDKNOB, HID_QUIRK_IGNORE },
  1339. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_90, HID_QUIRK_IGNORE },
  1340. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_100, HID_QUIRK_IGNORE },
  1341. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_101, HID_QUIRK_IGNORE },
  1342. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_103, HID_QUIRK_IGNORE },
  1343. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_104, HID_QUIRK_IGNORE },
  1344. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_105, HID_QUIRK_IGNORE },
  1345. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_106, HID_QUIRK_IGNORE },
  1346. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_107, HID_QUIRK_IGNORE },
  1347. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_108, HID_QUIRK_IGNORE },
  1348. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_200, HID_QUIRK_IGNORE },
  1349. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_201, HID_QUIRK_IGNORE },
  1350. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_202, HID_QUIRK_IGNORE },
  1351. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_203, HID_QUIRK_IGNORE },
  1352. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_204, HID_QUIRK_IGNORE },
  1353. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_205, HID_QUIRK_IGNORE },
  1354. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_206, HID_QUIRK_IGNORE },
  1355. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_207, HID_QUIRK_IGNORE },
  1356. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_300, HID_QUIRK_IGNORE },
  1357. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_301, HID_QUIRK_IGNORE },
  1358. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_302, HID_QUIRK_IGNORE },
  1359. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_303, HID_QUIRK_IGNORE },
  1360. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_304, HID_QUIRK_IGNORE },
  1361. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_305, HID_QUIRK_IGNORE },
  1362. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_306, HID_QUIRK_IGNORE },
  1363. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_307, HID_QUIRK_IGNORE },
  1364. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_308, HID_QUIRK_IGNORE },
  1365. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_309, HID_QUIRK_IGNORE },
  1366. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_400, HID_QUIRK_IGNORE },
  1367. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_401, HID_QUIRK_IGNORE },
  1368. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_402, HID_QUIRK_IGNORE },
  1369. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_403, HID_QUIRK_IGNORE },
  1370. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_404, HID_QUIRK_IGNORE },
  1371. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_405, HID_QUIRK_IGNORE },
  1372. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_500, HID_QUIRK_IGNORE },
  1373. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_501, HID_QUIRK_IGNORE },
  1374. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_502, HID_QUIRK_IGNORE },
  1375. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_503, HID_QUIRK_IGNORE },
  1376. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_504, HID_QUIRK_IGNORE },
  1377. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1000, HID_QUIRK_IGNORE },
  1378. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1001, HID_QUIRK_IGNORE },
  1379. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1002, HID_QUIRK_IGNORE },
  1380. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1003, HID_QUIRK_IGNORE },
  1381. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1004, HID_QUIRK_IGNORE },
  1382. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1005, HID_QUIRK_IGNORE },
  1383. { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1006, HID_QUIRK_IGNORE },
  1384. { USB_VENDOR_ID_KBGEAR, USB_DEVICE_ID_KBGEAR_JAMSTUDIO, HID_QUIRK_IGNORE },
  1385. { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_CASSY, HID_QUIRK_IGNORE },
  1386. { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POCKETCASSY, HID_QUIRK_IGNORE },
  1387. { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOBILECASSY, HID_QUIRK_IGNORE },
  1388. { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_JWM, HID_QUIRK_IGNORE },
  1389. { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_DMMP, HID_QUIRK_IGNORE },
  1390. { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_UMIP, HID_QUIRK_IGNORE },
  1391. { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY1, HID_QUIRK_IGNORE },
  1392. { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY2, HID_QUIRK_IGNORE },
  1393. { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_VIDEOCOM, HID_QUIRK_IGNORE },
  1394. { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_COM3LAB, HID_QUIRK_IGNORE },
  1395. { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_TELEPORT, HID_QUIRK_IGNORE },
  1396. { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_NETWORKANALYSER, HID_QUIRK_IGNORE },
  1397. { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POWERCONTROL, HID_QUIRK_IGNORE },
  1398. { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MACHINETEST, HID_QUIRK_IGNORE },
  1399. { USB_VENDOR_ID_MCC, USB_DEVICE_ID_MCC_PMD1024LS, HID_QUIRK_IGNORE },
  1400. { USB_VENDOR_ID_MCC, USB_DEVICE_ID_MCC_PMD1208LS, HID_QUIRK_IGNORE },
  1401. { USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS, HID_QUIRK_IGNORE },
  1402. { USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS1, HID_QUIRK_IGNORE },
  1403. { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100, HID_QUIRK_IGNORE },
  1404. { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 100, HID_QUIRK_IGNORE },
  1405. { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 200, HID_QUIRK_IGNORE },
  1406. { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 300, HID_QUIRK_IGNORE },
  1407. { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 400, HID_QUIRK_IGNORE },
  1408. { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 500, HID_QUIRK_IGNORE },
  1409. { USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_LABPRO, HID_QUIRK_IGNORE },
  1410. { USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_GOTEMP, HID_QUIRK_IGNORE },
  1411. { USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_SKIP, HID_QUIRK_IGNORE },
  1412. { USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_CYCLOPS, HID_QUIRK_IGNORE },
  1413. { USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_4_PHIDGETSERVO_20, HID_QUIRK_IGNORE },
  1414. { USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_1_PHIDGETSERVO_20, HID_QUIRK_IGNORE },
  1415. { USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_8_8_4_IF_KIT, HID_QUIRK_IGNORE },
  1416. { USB_VENDOR_ID_YEALINK, USB_DEVICE_ID_YEALINK_P1K_P4K_B2K, HID_QUIRK_IGNORE },
  1417. { USB_VENDOR_ID_ACECAD, USB_DEVICE_ID_ACECAD_FLAIR, HID_QUIRK_IGNORE },
  1418. { USB_VENDOR_ID_ACECAD, USB_DEVICE_ID_ACECAD_302, HID_QUIRK_IGNORE },
  1419. { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_UC100KM, HID_QUIRK_NOGET },
  1420. { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_CS124U, HID_QUIRK_NOGET },
  1421. { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_2PORTKVM, HID_QUIRK_NOGET },
  1422. { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_4PORTKVM, HID_QUIRK_NOGET },
  1423. { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_4PORTKVMC, HID_QUIRK_NOGET },
  1424. { USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_DUAL_USB_JOYPAD, HID_QUIRK_NOGET | HID_QUIRK_MULTI_INPUT },
  1425. { USB_VENDOR_ID_WISEGROUP_LTD, USB_DEVICE_ID_SMARTJOY_DUAL_PLUS, HID_QUIRK_NOGET | HID_QUIRK_MULTI_INPUT },
  1426. { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MIGHTYMOUSE, HID_QUIRK_MIGHTYMOUSE | HID_QUIRK_INVERT_HWHEEL },
  1427. { USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_WCP32PU, HID_QUIRK_2WHEEL_MOUSE_HACK_7 },
  1428. { USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_MOUSE, HID_QUIRK_2WHEEL_MOUSE_HACK_5 },
  1429. { USB_VENDOR_ID_AASHIMA, USB_DEVICE_ID_AASHIMA_GAMEPAD, HID_QUIRK_BADPAD },
  1430. { USB_VENDOR_ID_AASHIMA, USB_DEVICE_ID_AASHIMA_PREDATOR, HID_QUIRK_BADPAD },
  1431. { USB_VENDOR_ID_ALPS, USB_DEVICE_ID_IBM_GAMEPAD, HID_QUIRK_BADPAD },
  1432. { USB_VENDOR_ID_CHIC, USB_DEVICE_ID_CHIC_GAMEPAD, HID_QUIRK_BADPAD },
  1433. { USB_VENDOR_ID_HAPP, USB_DEVICE_ID_UGCI_DRIVING, HID_QUIRK_BADPAD | HID_QUIRK_MULTI_INPUT },
  1434. { USB_VENDOR_ID_HAPP, USB_DEVICE_ID_UGCI_FLYING, HID_QUIRK_BADPAD | HID_QUIRK_MULTI_INPUT },
  1435. { USB_VENDOR_ID_HAPP, USB_DEVICE_ID_UGCI_FIGHTING, HID_QUIRK_BADPAD | HID_QUIRK_MULTI_INPUT },
  1436. { USB_VENDOR_ID_NEC, USB_DEVICE_ID_NEC_USB_GAME_PAD, HID_QUIRK_BADPAD },
  1437. { USB_VENDOR_ID_SAITEK, USB_DEVICE_ID_SAITEK_RUMBLEPAD, HID_QUIRK_BADPAD },
  1438. { USB_VENDOR_ID_TOPMAX, USB_DEVICE_ID_TOPMAX_COBRAPAD, HID_QUIRK_BADPAD },
  1439. { USB_VENDOR_ID_CHERRY, USB_DEVICE_ID_CHERRY_CYMOTION, HID_QUIRK_CYMOTION },
  1440. { USB_VENDOR_ID_APPLE, 0x020E, HID_QUIRK_POWERBOOK_HAS_FN },
  1441. { USB_VENDOR_ID_APPLE, 0x020F, HID_QUIRK_POWERBOOK_HAS_FN },
  1442. { USB_VENDOR_ID_APPLE, 0x0214, HID_QUIRK_POWERBOOK_HAS_FN },
  1443. { USB_VENDOR_ID_APPLE, 0x0215, HID_QUIRK_POWERBOOK_HAS_FN },
  1444. { USB_VENDOR_ID_APPLE, 0x0216, HID_QUIRK_POWERBOOK_HAS_FN },
  1445. { USB_VENDOR_ID_APPLE, 0x0217, HID_QUIRK_POWERBOOK_HAS_FN },
  1446. { USB_VENDOR_ID_APPLE, 0x0218, HID_QUIRK_POWERBOOK_HAS_FN },
  1447. { USB_VENDOR_ID_APPLE, 0x0219, HID_QUIRK_POWERBOOK_HAS_FN },
  1448. { USB_VENDOR_ID_APPLE, 0x030A, HID_QUIRK_POWERBOOK_HAS_FN },
  1449. { USB_VENDOR_ID_APPLE, 0x030B, HID_QUIRK_POWERBOOK_HAS_FN },
  1450. { USB_VENDOR_ID_PANJIT, 0x0001, HID_QUIRK_IGNORE },
  1451. { USB_VENDOR_ID_PANJIT, 0x0002, HID_QUIRK_IGNORE },
  1452. { USB_VENDOR_ID_PANJIT, 0x0003, HID_QUIRK_IGNORE },
  1453. { USB_VENDOR_ID_PANJIT, 0x0004, HID_QUIRK_IGNORE },
  1454. { 0, 0 }
  1455. };
  1456. /*
  1457. * Traverse the supplied list of reports and find the longest
  1458. */
  1459. static void hid_find_max_report(struct hid_device *hid, unsigned int type, int *max)
  1460. {
  1461. struct hid_report *report;
  1462. int size;
  1463. list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
  1464. size = ((report->size - 1) >> 3) + 1;
  1465. if (type == HID_INPUT_REPORT && hid->report_enum[type].numbered)
  1466. size++;
  1467. if (*max < size)
  1468. *max = size;
  1469. }
  1470. }
  1471. static int hid_alloc_buffers(struct usb_device *dev, struct hid_device *hid)
  1472. {
  1473. if (!(hid->inbuf = usb_buffer_alloc(dev, hid->bufsize, SLAB_ATOMIC, &hid->inbuf_dma)))
  1474. return -1;
  1475. if (!(hid->outbuf = usb_buffer_alloc(dev, hid->bufsize, SLAB_ATOMIC, &hid->outbuf_dma)))
  1476. return -1;
  1477. if (!(hid->cr = usb_buffer_alloc(dev, sizeof(*(hid->cr)), SLAB_ATOMIC, &hid->cr_dma)))
  1478. return -1;
  1479. if (!(hid->ctrlbuf = usb_buffer_alloc(dev, hid->bufsize, SLAB_ATOMIC, &hid->ctrlbuf_dma)))
  1480. return -1;
  1481. return 0;
  1482. }
  1483. static void hid_free_buffers(struct usb_device *dev, struct hid_device *hid)
  1484. {
  1485. if (hid->inbuf)
  1486. usb_buffer_free(dev, hid->bufsize, hid->inbuf, hid->inbuf_dma);
  1487. if (hid->outbuf)
  1488. usb_buffer_free(dev, hid->bufsize, hid->outbuf, hid->outbuf_dma);
  1489. if (hid->cr)
  1490. usb_buffer_free(dev, sizeof(*(hid->cr)), hid->cr, hid->cr_dma);
  1491. if (hid->ctrlbuf)
  1492. usb_buffer_free(dev, hid->bufsize, hid->ctrlbuf, hid->ctrlbuf_dma);
  1493. }
  1494. /*
  1495. * Cherry Cymotion keyboard have an invalid HID report descriptor,
  1496. * that needs fixing before we can parse it.
  1497. */
  1498. static void hid_fixup_cymotion_descriptor(char *rdesc, int rsize)
  1499. {
  1500. if (rsize >= 17 && rdesc[11] == 0x3c && rdesc[12] == 0x02) {
  1501. info("Fixing up Cherry Cymotion report descriptor");
  1502. rdesc[11] = rdesc[16] = 0xff;
  1503. rdesc[12] = rdesc[17] = 0x03;
  1504. }
  1505. }
  1506. static struct hid_device *usb_hid_configure(struct usb_interface *intf)
  1507. {
  1508. struct usb_host_interface *interface = intf->cur_altsetting;
  1509. struct usb_device *dev = interface_to_usbdev (intf);
  1510. struct hid_descriptor *hdesc;
  1511. struct hid_device *hid;
  1512. unsigned quirks = 0, rsize = 0;
  1513. char *rdesc;
  1514. int n, len, insize = 0;
  1515. /* Ignore all Wacom devices */
  1516. if (le16_to_cpu(dev->descriptor.idVendor) == USB_VENDOR_ID_WACOM)
  1517. return NULL;
  1518. for (n = 0; hid_blacklist[n].idVendor; n++)
  1519. if ((hid_blacklist[n].idVendor == le16_to_cpu(dev->descriptor.idVendor)) &&
  1520. (hid_blacklist[n].idProduct == le16_to_cpu(dev->descriptor.idProduct)))
  1521. quirks = hid_blacklist[n].quirks;
  1522. /* Many keyboards and mice don't like to be polled for reports,
  1523. * so we will always set the HID_QUIRK_NOGET flag for them. */
  1524. if (interface->desc.bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT) {
  1525. if (interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_KEYBOARD ||
  1526. interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_MOUSE)
  1527. quirks |= HID_QUIRK_NOGET;
  1528. }
  1529. if (quirks & HID_QUIRK_IGNORE)
  1530. return NULL;
  1531. if (usb_get_extra_descriptor(interface, HID_DT_HID, &hdesc) &&
  1532. (!interface->desc.bNumEndpoints ||
  1533. usb_get_extra_descriptor(&interface->endpoint[0], HID_DT_HID, &hdesc))) {
  1534. dbg("class descriptor not present\n");
  1535. return NULL;
  1536. }
  1537. for (n = 0; n < hdesc->bNumDescriptors; n++)
  1538. if (hdesc->desc[n].bDescriptorType == HID_DT_REPORT)
  1539. rsize = le16_to_cpu(hdesc->desc[n].wDescriptorLength);
  1540. if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
  1541. dbg("weird size of report descriptor (%u)", rsize);
  1542. return NULL;
  1543. }
  1544. if (!(rdesc = kmalloc(rsize, GFP_KERNEL))) {
  1545. dbg("couldn't allocate rdesc memory");
  1546. return NULL;
  1547. }
  1548. hid_set_idle(dev, interface->desc.bInterfaceNumber, 0, 0);
  1549. if ((n = hid_get_class_descriptor(dev, interface->desc.bInterfaceNumber, HID_DT_REPORT, rdesc, rsize)) < 0) {
  1550. dbg("reading report descriptor failed");
  1551. kfree(rdesc);
  1552. return NULL;
  1553. }
  1554. if ((quirks & HID_QUIRK_CYMOTION))
  1555. hid_fixup_cymotion_descriptor(rdesc, rsize);
  1556. #ifdef DEBUG_DATA
  1557. printk(KERN_DEBUG __FILE__ ": report descriptor (size %u, read %d) = ", rsize, n);
  1558. for (n = 0; n < rsize; n++)
  1559. printk(" %02x", (unsigned char) rdesc[n]);
  1560. printk("\n");
  1561. #endif
  1562. if (!(hid = hid_parse_report(rdesc, n))) {
  1563. dbg("parsing report descriptor failed");
  1564. kfree(rdesc);
  1565. return NULL;
  1566. }
  1567. kfree(rdesc);
  1568. hid->quirks = quirks;
  1569. hid->bufsize = HID_MIN_BUFFER_SIZE;
  1570. hid_find_max_report(hid, HID_INPUT_REPORT, &hid->bufsize);
  1571. hid_find_max_report(hid, HID_OUTPUT_REPORT, &hid->bufsize);
  1572. hid_find_max_report(hid, HID_FEATURE_REPORT, &hid->bufsize);
  1573. if (hid->bufsize > HID_MAX_BUFFER_SIZE)
  1574. hid->bufsize = HID_MAX_BUFFER_SIZE;
  1575. hid_find_max_report(hid, HID_INPUT_REPORT, &insize);
  1576. if (insize > HID_MAX_BUFFER_SIZE)
  1577. insize = HID_MAX_BUFFER_SIZE;
  1578. if (hid_alloc_buffers(dev, hid)) {
  1579. hid_free_buffers(dev, hid);
  1580. goto fail;
  1581. }
  1582. for (n = 0; n < interface->desc.bNumEndpoints; n++) {
  1583. struct usb_endpoint_descriptor *endpoint;
  1584. int pipe;
  1585. int interval;
  1586. endpoint = &interface->endpoint[n].desc;
  1587. if ((endpoint->bmAttributes & 3) != 3) /* Not an interrupt endpoint */
  1588. continue;
  1589. interval = endpoint->bInterval;
  1590. /* Change the polling interval of mice. */
  1591. if (hid->collection->usage == HID_GD_MOUSE && hid_mousepoll_interval > 0)
  1592. interval = hid_mousepoll_interval;
  1593. if (endpoint->bEndpointAddress & USB_DIR_IN) {
  1594. if (hid->urbin)
  1595. continue;
  1596. if (!(hid->urbin = usb_alloc_urb(0, GFP_KERNEL)))
  1597. goto fail;
  1598. pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
  1599. usb_fill_int_urb(hid->urbin, dev, pipe, hid->inbuf, insize,
  1600. hid_irq_in, hid, interval);
  1601. hid->urbin->transfer_dma = hid->inbuf_dma;
  1602. hid->urbin->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  1603. } else {
  1604. if (hid->urbout)
  1605. continue;
  1606. if (!(hid->urbout = usb_alloc_urb(0, GFP_KERNEL)))
  1607. goto fail;
  1608. pipe = usb_sndintpipe(dev, endpoint->bEndpointAddress);
  1609. usb_fill_int_urb(hid->urbout, dev, pipe, hid->outbuf, 0,
  1610. hid_irq_out, hid, interval);
  1611. hid->urbout->transfer_dma = hid->outbuf_dma;
  1612. hid->urbout->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  1613. }
  1614. }
  1615. if (!hid->urbin) {
  1616. err("couldn't find an input interrupt endpoint");
  1617. goto fail;
  1618. }
  1619. init_waitqueue_head(&hid->wait);
  1620. INIT_WORK(&hid->reset_work, hid_reset, hid);
  1621. setup_timer(&hid->io_retry, hid_retry_timeout, (unsigned long) hid);
  1622. spin_lock_init(&hid->inlock);
  1623. spin_lock_init(&hid->outlock);
  1624. spin_lock_init(&hid->ctrllock);
  1625. hid->version = le16_to_cpu(hdesc->bcdHID);
  1626. hid->country = hdesc->bCountryCode;
  1627. hid->dev = dev;
  1628. hid->intf = intf;
  1629. hid->ifnum = interface->desc.bInterfaceNumber;
  1630. hid->name[0] = 0;
  1631. if (dev->manufacturer)
  1632. strlcpy(hid->name, dev->manufacturer, sizeof(hid->name));
  1633. if (dev->product) {
  1634. if (dev->manufacturer)
  1635. strlcat(hid->name, " ", sizeof(hid->name));
  1636. strlcat(hid->name, dev->product, sizeof(hid->name));
  1637. }
  1638. if (!strlen(hid->name))
  1639. snprintf(hid->name, sizeof(hid->name), "HID %04x:%04x",
  1640. le16_to_cpu(dev->descriptor.idVendor),
  1641. le16_to_cpu(dev->descriptor.idProduct));
  1642. usb_make_path(dev, hid->phys, sizeof(hid->phys));
  1643. strlcat(hid->phys, "/input", sizeof(hid->phys));
  1644. len = strlen(hid->phys);
  1645. if (len < sizeof(hid->phys) - 1)
  1646. snprintf(hid->phys + len, sizeof(hid->phys) - len,
  1647. "%d", intf->altsetting[0].desc.bInterfaceNumber);
  1648. if (usb_string(dev, dev->descriptor.iSerialNumber, hid->uniq, 64) <= 0)
  1649. hid->uniq[0] = 0;
  1650. hid->urbctrl = usb_alloc_urb(0, GFP_KERNEL);
  1651. if (!hid->urbctrl)
  1652. goto fail;
  1653. usb_fill_control_urb(hid->urbctrl, dev, 0, (void *) hid->cr,
  1654. hid->ctrlbuf, 1, hid_ctrl, hid);
  1655. hid->urbctrl->setup_dma = hid->cr_dma;
  1656. hid->urbctrl->transfer_dma = hid->ctrlbuf_dma;
  1657. hid->urbctrl->transfer_flags |= (URB_NO_TRANSFER_DMA_MAP | URB_NO_SETUP_DMA_MAP);
  1658. return hid;
  1659. fail:
  1660. if (hid->urbin)
  1661. usb_free_urb(hid->urbin);
  1662. if (hid->urbout)
  1663. usb_free_urb(hid->urbout);
  1664. if (hid->urbctrl)
  1665. usb_free_urb(hid->urbctrl);
  1666. hid_free_buffers(dev, hid);
  1667. hid_free_device(hid);
  1668. return NULL;
  1669. }
  1670. static void hid_disconnect(struct usb_interface *intf)
  1671. {
  1672. struct hid_device *hid = usb_get_intfdata (intf);
  1673. if (!hid)
  1674. return;
  1675. spin_lock_irq(&hid->inlock); /* Sync with error handler */
  1676. usb_set_intfdata(intf, NULL);
  1677. spin_unlock_irq(&hid->inlock);
  1678. usb_kill_urb(hid->urbin);
  1679. usb_kill_urb(hid->urbout);
  1680. usb_kill_urb(hid->urbctrl);
  1681. del_timer_sync(&hid->io_retry);
  1682. flush_scheduled_work();
  1683. if (hid->claimed & HID_CLAIMED_INPUT)
  1684. hidinput_disconnect(hid);
  1685. if (hid->claimed & HID_CLAIMED_HIDDEV)
  1686. hiddev_disconnect(hid);
  1687. usb_free_urb(hid->urbin);
  1688. usb_free_urb(hid->urbctrl);
  1689. if (hid->urbout)
  1690. usb_free_urb(hid->urbout);
  1691. hid_free_buffers(hid->dev, hid);
  1692. hid_free_device(hid);
  1693. }
  1694. static int hid_probe(struct usb_interface *intf, const struct usb_device_id *id)
  1695. {
  1696. struct hid_device *hid;
  1697. char path[64];
  1698. int i;
  1699. char *c;
  1700. dbg("HID probe called for ifnum %d",
  1701. intf->altsetting->desc.bInterfaceNumber);
  1702. if (!(hid = usb_hid_configure(intf)))
  1703. return -ENODEV;
  1704. hid_init_reports(hid);
  1705. hid_dump_device(hid);
  1706. if (!hidinput_connect(hid))
  1707. hid->claimed |= HID_CLAIMED_INPUT;
  1708. if (!hiddev_connect(hid))
  1709. hid->claimed |= HID_CLAIMED_HIDDEV;
  1710. usb_set_intfdata(intf, hid);
  1711. if (!hid->claimed) {
  1712. printk ("HID device not claimed by input or hiddev\n");
  1713. hid_disconnect(intf);
  1714. return -ENODEV;
  1715. }
  1716. printk(KERN_INFO);
  1717. if (hid->claimed & HID_CLAIMED_INPUT)
  1718. printk("input");
  1719. if (hid->claimed == (HID_CLAIMED_INPUT | HID_CLAIMED_HIDDEV))
  1720. printk(",");
  1721. if (hid->claimed & HID_CLAIMED_HIDDEV)
  1722. printk("hiddev%d", hid->minor);
  1723. c = "Device";
  1724. for (i = 0; i < hid->maxcollection; i++) {
  1725. if (hid->collection[i].type == HID_COLLECTION_APPLICATION &&
  1726. (hid->collection[i].usage & HID_USAGE_PAGE) == HID_UP_GENDESK &&
  1727. (hid->collection[i].usage & 0xffff) < ARRAY_SIZE(hid_types)) {
  1728. c = hid_types[hid->collection[i].usage & 0xffff];
  1729. break;
  1730. }
  1731. }
  1732. usb_make_path(interface_to_usbdev(intf), path, 63);
  1733. printk(": USB HID v%x.%02x %s [%s] on %s\n",
  1734. hid->version >> 8, hid->version & 0xff, c, hid->name, path);
  1735. return 0;
  1736. }
  1737. static int hid_suspend(struct usb_interface *intf, pm_message_t message)
  1738. {
  1739. struct hid_device *hid = usb_get_intfdata (intf);
  1740. spin_lock_irq(&hid->inlock); /* Sync with error handler */
  1741. set_bit(HID_SUSPENDED, &hid->iofl);
  1742. spin_unlock_irq(&hid->inlock);
  1743. del_timer(&hid->io_retry);
  1744. usb_kill_urb(hid->urbin);
  1745. dev_dbg(&intf->dev, "suspend\n");
  1746. return 0;
  1747. }
  1748. static int hid_resume(struct usb_interface *intf)
  1749. {
  1750. struct hid_device *hid = usb_get_intfdata (intf);
  1751. int status;
  1752. clear_bit(HID_SUSPENDED, &hid->iofl);
  1753. hid->retry_delay = 0;
  1754. status = hid_start_in(hid);
  1755. dev_dbg(&intf->dev, "resume status %d\n", status);
  1756. return status;
  1757. }
  1758. /* Treat USB reset pretty much the same as suspend/resume */
  1759. static void hid_pre_reset(struct usb_interface *intf)
  1760. {
  1761. /* FIXME: What if the interface is already suspended? */
  1762. hid_suspend(intf, PMSG_ON);
  1763. }
  1764. static void hid_post_reset(struct usb_interface *intf)
  1765. {
  1766. struct usb_device *dev = interface_to_usbdev (intf);
  1767. hid_set_idle(dev, intf->cur_altsetting->desc.bInterfaceNumber, 0, 0);
  1768. /* FIXME: Any more reinitialization needed? */
  1769. hid_resume(intf);
  1770. }
  1771. static struct usb_device_id hid_usb_ids [] = {
  1772. { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
  1773. .bInterfaceClass = USB_INTERFACE_CLASS_HID },
  1774. { } /* Terminating entry */
  1775. };
  1776. MODULE_DEVICE_TABLE (usb, hid_usb_ids);
  1777. static struct usb_driver hid_driver = {
  1778. .name = "usbhid",
  1779. .probe = hid_probe,
  1780. .disconnect = hid_disconnect,
  1781. .suspend = hid_suspend,
  1782. .resume = hid_resume,
  1783. .pre_reset = hid_pre_reset,
  1784. .post_reset = hid_post_reset,
  1785. .id_table = hid_usb_ids,
  1786. };
  1787. static int __init hid_init(void)
  1788. {
  1789. int retval;
  1790. retval = hiddev_init();
  1791. if (retval)
  1792. goto hiddev_init_fail;
  1793. retval = usb_register(&hid_driver);
  1794. if (retval)
  1795. goto usb_register_fail;
  1796. info(DRIVER_VERSION ":" DRIVER_DESC);
  1797. return 0;
  1798. usb_register_fail:
  1799. hiddev_exit();
  1800. hiddev_init_fail:
  1801. return retval;
  1802. }
  1803. static void __exit hid_exit(void)
  1804. {
  1805. usb_deregister(&hid_driver);
  1806. hiddev_exit();
  1807. }
  1808. module_init(hid_init);
  1809. module_exit(hid_exit);
  1810. MODULE_AUTHOR(DRIVER_AUTHOR);
  1811. MODULE_DESCRIPTION(DRIVER_DESC);
  1812. MODULE_LICENSE(DRIVER_LICENSE);