hid-core.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956
  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 = kmalloc(sizeof(struct hid_report), GFP_KERNEL)))
  57. return NULL;
  58. memset(report, 0, sizeof(struct hid_report));
  59. if (id != 0)
  60. report_enum->numbered = 1;
  61. report->id = id;
  62. report->type = type;
  63. report->size = 0;
  64. report->device = device;
  65. report_enum->report_id_hash[id] = report;
  66. list_add_tail(&report->list, &report_enum->report_list);
  67. return report;
  68. }
  69. /*
  70. * Register a new field for this report.
  71. */
  72. static struct hid_field *hid_register_field(struct hid_report *report, unsigned usages, unsigned values)
  73. {
  74. struct hid_field *field;
  75. if (report->maxfield == HID_MAX_FIELDS) {
  76. dbg("too many fields in report");
  77. return NULL;
  78. }
  79. if (!(field = kmalloc(sizeof(struct hid_field) + usages * sizeof(struct hid_usage)
  80. + values * sizeof(unsigned), GFP_KERNEL))) return NULL;
  81. memset(field, 0, sizeof(struct hid_field) + usages * sizeof(struct hid_usage)
  82. + values * sizeof(unsigned));
  83. field->index = report->maxfield++;
  84. report->field[field->index] = field;
  85. field->usage = (struct hid_usage *)(field + 1);
  86. field->value = (unsigned *)(field->usage + usages);
  87. field->report = report;
  88. return field;
  89. }
  90. /*
  91. * Open a collection. The type/usage is pushed on the stack.
  92. */
  93. static int open_collection(struct hid_parser *parser, unsigned type)
  94. {
  95. struct hid_collection *collection;
  96. unsigned usage;
  97. usage = parser->local.usage[0];
  98. if (parser->collection_stack_ptr == HID_COLLECTION_STACK_SIZE) {
  99. dbg("collection stack overflow");
  100. return -1;
  101. }
  102. if (parser->device->maxcollection == parser->device->collection_size) {
  103. collection = kmalloc(sizeof(struct hid_collection) *
  104. parser->device->collection_size * 2, GFP_KERNEL);
  105. if (collection == NULL) {
  106. dbg("failed to reallocate collection array");
  107. return -1;
  108. }
  109. memcpy(collection, parser->device->collection,
  110. sizeof(struct hid_collection) *
  111. parser->device->collection_size);
  112. memset(collection + parser->device->collection_size, 0,
  113. sizeof(struct hid_collection) *
  114. parser->device->collection_size);
  115. kfree(parser->device->collection);
  116. parser->device->collection = collection;
  117. parser->device->collection_size *= 2;
  118. }
  119. parser->collection_stack[parser->collection_stack_ptr++] =
  120. parser->device->maxcollection;
  121. collection = parser->device->collection +
  122. parser->device->maxcollection++;
  123. collection->type = type;
  124. collection->usage = usage;
  125. collection->level = parser->collection_stack_ptr - 1;
  126. if (type == HID_COLLECTION_APPLICATION)
  127. parser->device->maxapplication++;
  128. return 0;
  129. }
  130. /*
  131. * Close a collection.
  132. */
  133. static int close_collection(struct hid_parser *parser)
  134. {
  135. if (!parser->collection_stack_ptr) {
  136. dbg("collection stack underflow");
  137. return -1;
  138. }
  139. parser->collection_stack_ptr--;
  140. return 0;
  141. }
  142. /*
  143. * Climb up the stack, search for the specified collection type
  144. * and return the usage.
  145. */
  146. static unsigned hid_lookup_collection(struct hid_parser *parser, unsigned type)
  147. {
  148. int n;
  149. for (n = parser->collection_stack_ptr - 1; n >= 0; n--)
  150. if (parser->device->collection[parser->collection_stack[n]].type == type)
  151. return parser->device->collection[parser->collection_stack[n]].usage;
  152. return 0; /* we know nothing about this usage type */
  153. }
  154. /*
  155. * Add a usage to the temporary parser table.
  156. */
  157. static int hid_add_usage(struct hid_parser *parser, unsigned usage)
  158. {
  159. if (parser->local.usage_index >= HID_MAX_USAGES) {
  160. dbg("usage index exceeded");
  161. return -1;
  162. }
  163. parser->local.usage[parser->local.usage_index] = usage;
  164. parser->local.collection_index[parser->local.usage_index] =
  165. parser->collection_stack_ptr ?
  166. parser->collection_stack[parser->collection_stack_ptr - 1] : 0;
  167. parser->local.usage_index++;
  168. return 0;
  169. }
  170. /*
  171. * Register a new field for this report.
  172. */
  173. static int hid_add_field(struct hid_parser *parser, unsigned report_type, unsigned flags)
  174. {
  175. struct hid_report *report;
  176. struct hid_field *field;
  177. int usages;
  178. unsigned offset;
  179. int i;
  180. if (!(report = hid_register_report(parser->device, report_type, parser->global.report_id))) {
  181. dbg("hid_register_report failed");
  182. return -1;
  183. }
  184. if (parser->global.logical_maximum < parser->global.logical_minimum) {
  185. dbg("logical range invalid %d %d", parser->global.logical_minimum, parser->global.logical_maximum);
  186. return -1;
  187. }
  188. offset = report->size;
  189. report->size += parser->global.report_size * parser->global.report_count;
  190. if (!parser->local.usage_index) /* Ignore padding fields */
  191. return 0;
  192. usages = max_t(int, parser->local.usage_index, parser->global.report_count);
  193. if ((field = hid_register_field(report, usages, parser->global.report_count)) == NULL)
  194. return 0;
  195. field->physical = hid_lookup_collection(parser, HID_COLLECTION_PHYSICAL);
  196. field->logical = hid_lookup_collection(parser, HID_COLLECTION_LOGICAL);
  197. field->application = hid_lookup_collection(parser, HID_COLLECTION_APPLICATION);
  198. for (i = 0; i < usages; i++) {
  199. int j = i;
  200. /* Duplicate the last usage we parsed if we have excess values */
  201. if (i >= parser->local.usage_index)
  202. j = parser->local.usage_index - 1;
  203. field->usage[i].hid = parser->local.usage[j];
  204. field->usage[i].collection_index =
  205. parser->local.collection_index[j];
  206. }
  207. field->maxusage = usages;
  208. field->flags = flags;
  209. field->report_offset = offset;
  210. field->report_type = report_type;
  211. field->report_size = parser->global.report_size;
  212. field->report_count = parser->global.report_count;
  213. field->logical_minimum = parser->global.logical_minimum;
  214. field->logical_maximum = parser->global.logical_maximum;
  215. field->physical_minimum = parser->global.physical_minimum;
  216. field->physical_maximum = parser->global.physical_maximum;
  217. field->unit_exponent = parser->global.unit_exponent;
  218. field->unit = parser->global.unit;
  219. return 0;
  220. }
  221. /*
  222. * Read data value from item.
  223. */
  224. static __inline__ __u32 item_udata(struct hid_item *item)
  225. {
  226. switch (item->size) {
  227. case 1: return item->data.u8;
  228. case 2: return item->data.u16;
  229. case 4: return item->data.u32;
  230. }
  231. return 0;
  232. }
  233. static __inline__ __s32 item_sdata(struct hid_item *item)
  234. {
  235. switch (item->size) {
  236. case 1: return item->data.s8;
  237. case 2: return item->data.s16;
  238. case 4: return item->data.s32;
  239. }
  240. return 0;
  241. }
  242. /*
  243. * Process a global item.
  244. */
  245. static int hid_parser_global(struct hid_parser *parser, struct hid_item *item)
  246. {
  247. switch (item->tag) {
  248. case HID_GLOBAL_ITEM_TAG_PUSH:
  249. if (parser->global_stack_ptr == HID_GLOBAL_STACK_SIZE) {
  250. dbg("global enviroment stack overflow");
  251. return -1;
  252. }
  253. memcpy(parser->global_stack + parser->global_stack_ptr++,
  254. &parser->global, sizeof(struct hid_global));
  255. return 0;
  256. case HID_GLOBAL_ITEM_TAG_POP:
  257. if (!parser->global_stack_ptr) {
  258. dbg("global enviroment stack underflow");
  259. return -1;
  260. }
  261. memcpy(&parser->global, parser->global_stack + --parser->global_stack_ptr,
  262. sizeof(struct hid_global));
  263. return 0;
  264. case HID_GLOBAL_ITEM_TAG_USAGE_PAGE:
  265. parser->global.usage_page = item_udata(item);
  266. return 0;
  267. case HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM:
  268. parser->global.logical_minimum = item_sdata(item);
  269. return 0;
  270. case HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM:
  271. if (parser->global.logical_minimum < 0)
  272. parser->global.logical_maximum = item_sdata(item);
  273. else
  274. parser->global.logical_maximum = item_udata(item);
  275. return 0;
  276. case HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM:
  277. parser->global.physical_minimum = item_sdata(item);
  278. return 0;
  279. case HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM:
  280. if (parser->global.physical_minimum < 0)
  281. parser->global.physical_maximum = item_sdata(item);
  282. else
  283. parser->global.physical_maximum = item_udata(item);
  284. return 0;
  285. case HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT:
  286. parser->global.unit_exponent = item_sdata(item);
  287. return 0;
  288. case HID_GLOBAL_ITEM_TAG_UNIT:
  289. parser->global.unit = item_udata(item);
  290. return 0;
  291. case HID_GLOBAL_ITEM_TAG_REPORT_SIZE:
  292. if ((parser->global.report_size = item_udata(item)) > 32) {
  293. dbg("invalid report_size %d", parser->global.report_size);
  294. return -1;
  295. }
  296. return 0;
  297. case HID_GLOBAL_ITEM_TAG_REPORT_COUNT:
  298. if ((parser->global.report_count = item_udata(item)) > HID_MAX_USAGES) {
  299. dbg("invalid report_count %d", parser->global.report_count);
  300. return -1;
  301. }
  302. return 0;
  303. case HID_GLOBAL_ITEM_TAG_REPORT_ID:
  304. if ((parser->global.report_id = item_udata(item)) == 0) {
  305. dbg("report_id 0 is invalid");
  306. return -1;
  307. }
  308. return 0;
  309. default:
  310. dbg("unknown global tag 0x%x", item->tag);
  311. return -1;
  312. }
  313. }
  314. /*
  315. * Process a local item.
  316. */
  317. static int hid_parser_local(struct hid_parser *parser, struct hid_item *item)
  318. {
  319. __u32 data;
  320. unsigned n;
  321. if (item->size == 0) {
  322. dbg("item data expected for local item");
  323. return -1;
  324. }
  325. data = item_udata(item);
  326. switch (item->tag) {
  327. case HID_LOCAL_ITEM_TAG_DELIMITER:
  328. if (data) {
  329. /*
  330. * We treat items before the first delimiter
  331. * as global to all usage sets (branch 0).
  332. * In the moment we process only these global
  333. * items and the first delimiter set.
  334. */
  335. if (parser->local.delimiter_depth != 0) {
  336. dbg("nested delimiters");
  337. return -1;
  338. }
  339. parser->local.delimiter_depth++;
  340. parser->local.delimiter_branch++;
  341. } else {
  342. if (parser->local.delimiter_depth < 1) {
  343. dbg("bogus close delimiter");
  344. return -1;
  345. }
  346. parser->local.delimiter_depth--;
  347. }
  348. return 1;
  349. case HID_LOCAL_ITEM_TAG_USAGE:
  350. if (parser->local.delimiter_branch > 1) {
  351. dbg("alternative usage ignored");
  352. return 0;
  353. }
  354. if (item->size <= 2)
  355. data = (parser->global.usage_page << 16) + data;
  356. return hid_add_usage(parser, data);
  357. case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM:
  358. if (parser->local.delimiter_branch > 1) {
  359. dbg("alternative usage ignored");
  360. return 0;
  361. }
  362. if (item->size <= 2)
  363. data = (parser->global.usage_page << 16) + data;
  364. parser->local.usage_minimum = data;
  365. return 0;
  366. case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM:
  367. if (parser->local.delimiter_branch > 1) {
  368. dbg("alternative usage ignored");
  369. return 0;
  370. }
  371. if (item->size <= 2)
  372. data = (parser->global.usage_page << 16) + data;
  373. for (n = parser->local.usage_minimum; n <= data; n++)
  374. if (hid_add_usage(parser, n)) {
  375. dbg("hid_add_usage failed\n");
  376. return -1;
  377. }
  378. return 0;
  379. default:
  380. dbg("unknown local item tag 0x%x", item->tag);
  381. return 0;
  382. }
  383. return 0;
  384. }
  385. /*
  386. * Process a main item.
  387. */
  388. static int hid_parser_main(struct hid_parser *parser, struct hid_item *item)
  389. {
  390. __u32 data;
  391. int ret;
  392. data = item_udata(item);
  393. switch (item->tag) {
  394. case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION:
  395. ret = open_collection(parser, data & 0xff);
  396. break;
  397. case HID_MAIN_ITEM_TAG_END_COLLECTION:
  398. ret = close_collection(parser);
  399. break;
  400. case HID_MAIN_ITEM_TAG_INPUT:
  401. ret = hid_add_field(parser, HID_INPUT_REPORT, data);
  402. break;
  403. case HID_MAIN_ITEM_TAG_OUTPUT:
  404. ret = hid_add_field(parser, HID_OUTPUT_REPORT, data);
  405. break;
  406. case HID_MAIN_ITEM_TAG_FEATURE:
  407. ret = hid_add_field(parser, HID_FEATURE_REPORT, data);
  408. break;
  409. default:
  410. dbg("unknown main item tag 0x%x", item->tag);
  411. ret = 0;
  412. }
  413. memset(&parser->local, 0, sizeof(parser->local)); /* Reset the local parser environment */
  414. return ret;
  415. }
  416. /*
  417. * Process a reserved item.
  418. */
  419. static int hid_parser_reserved(struct hid_parser *parser, struct hid_item *item)
  420. {
  421. dbg("reserved item type, tag 0x%x", item->tag);
  422. return 0;
  423. }
  424. /*
  425. * Free a report and all registered fields. The field->usage and
  426. * field->value table's are allocated behind the field, so we need
  427. * only to free(field) itself.
  428. */
  429. static void hid_free_report(struct hid_report *report)
  430. {
  431. unsigned n;
  432. for (n = 0; n < report->maxfield; n++)
  433. kfree(report->field[n]);
  434. kfree(report);
  435. }
  436. /*
  437. * Free a device structure, all reports, and all fields.
  438. */
  439. static void hid_free_device(struct hid_device *device)
  440. {
  441. unsigned i,j;
  442. hid_ff_exit(device);
  443. for (i = 0; i < HID_REPORT_TYPES; i++) {
  444. struct hid_report_enum *report_enum = device->report_enum + i;
  445. for (j = 0; j < 256; j++) {
  446. struct hid_report *report = report_enum->report_id_hash[j];
  447. if (report)
  448. hid_free_report(report);
  449. }
  450. }
  451. kfree(device->rdesc);
  452. kfree(device);
  453. }
  454. /*
  455. * Fetch a report description item from the data stream. We support long
  456. * items, though they are not used yet.
  457. */
  458. static u8 *fetch_item(__u8 *start, __u8 *end, struct hid_item *item)
  459. {
  460. u8 b;
  461. if ((end - start) <= 0)
  462. return NULL;
  463. b = *start++;
  464. item->type = (b >> 2) & 3;
  465. item->tag = (b >> 4) & 15;
  466. if (item->tag == HID_ITEM_TAG_LONG) {
  467. item->format = HID_ITEM_FORMAT_LONG;
  468. if ((end - start) < 2)
  469. return NULL;
  470. item->size = *start++;
  471. item->tag = *start++;
  472. if ((end - start) < item->size)
  473. return NULL;
  474. item->data.longdata = start;
  475. start += item->size;
  476. return start;
  477. }
  478. item->format = HID_ITEM_FORMAT_SHORT;
  479. item->size = b & 3;
  480. switch (item->size) {
  481. case 0:
  482. return start;
  483. case 1:
  484. if ((end - start) < 1)
  485. return NULL;
  486. item->data.u8 = *start++;
  487. return start;
  488. case 2:
  489. if ((end - start) < 2)
  490. return NULL;
  491. item->data.u16 = le16_to_cpu(get_unaligned((__le16*)start));
  492. start = (__u8 *)((__le16 *)start + 1);
  493. return start;
  494. case 3:
  495. item->size++;
  496. if ((end - start) < 4)
  497. return NULL;
  498. item->data.u32 = le32_to_cpu(get_unaligned((__le32*)start));
  499. start = (__u8 *)((__le32 *)start + 1);
  500. return start;
  501. }
  502. return NULL;
  503. }
  504. /*
  505. * Parse a report description into a hid_device structure. Reports are
  506. * enumerated, fields are attached to these reports.
  507. */
  508. static struct hid_device *hid_parse_report(__u8 *start, unsigned size)
  509. {
  510. struct hid_device *device;
  511. struct hid_parser *parser;
  512. struct hid_item item;
  513. __u8 *end;
  514. unsigned i;
  515. static int (*dispatch_type[])(struct hid_parser *parser,
  516. struct hid_item *item) = {
  517. hid_parser_main,
  518. hid_parser_global,
  519. hid_parser_local,
  520. hid_parser_reserved
  521. };
  522. if (!(device = kmalloc(sizeof(struct hid_device), GFP_KERNEL)))
  523. return NULL;
  524. memset(device, 0, sizeof(struct hid_device));
  525. if (!(device->collection = kmalloc(sizeof(struct hid_collection) *
  526. HID_DEFAULT_NUM_COLLECTIONS, GFP_KERNEL))) {
  527. kfree(device);
  528. return NULL;
  529. }
  530. memset(device->collection, 0, sizeof(struct hid_collection) *
  531. HID_DEFAULT_NUM_COLLECTIONS);
  532. device->collection_size = HID_DEFAULT_NUM_COLLECTIONS;
  533. for (i = 0; i < HID_REPORT_TYPES; i++)
  534. INIT_LIST_HEAD(&device->report_enum[i].report_list);
  535. if (!(device->rdesc = (__u8 *)kmalloc(size, GFP_KERNEL))) {
  536. kfree(device->collection);
  537. kfree(device);
  538. return NULL;
  539. }
  540. memcpy(device->rdesc, start, size);
  541. device->rsize = size;
  542. if (!(parser = kmalloc(sizeof(struct hid_parser), GFP_KERNEL))) {
  543. kfree(device->rdesc);
  544. kfree(device->collection);
  545. kfree(device);
  546. return NULL;
  547. }
  548. memset(parser, 0, sizeof(struct hid_parser));
  549. parser->device = device;
  550. end = start + size;
  551. while ((start = fetch_item(start, end, &item)) != NULL) {
  552. if (item.format != HID_ITEM_FORMAT_SHORT) {
  553. dbg("unexpected long global item");
  554. kfree(device->collection);
  555. hid_free_device(device);
  556. kfree(parser);
  557. return NULL;
  558. }
  559. if (dispatch_type[item.type](parser, &item)) {
  560. dbg("item %u %u %u %u parsing failed\n",
  561. item.format, (unsigned)item.size, (unsigned)item.type, (unsigned)item.tag);
  562. kfree(device->collection);
  563. hid_free_device(device);
  564. kfree(parser);
  565. return NULL;
  566. }
  567. if (start == end) {
  568. if (parser->collection_stack_ptr) {
  569. dbg("unbalanced collection at end of report description");
  570. kfree(device->collection);
  571. hid_free_device(device);
  572. kfree(parser);
  573. return NULL;
  574. }
  575. if (parser->local.delimiter_depth) {
  576. dbg("unbalanced delimiter at end of report description");
  577. kfree(device->collection);
  578. hid_free_device(device);
  579. kfree(parser);
  580. return NULL;
  581. }
  582. kfree(parser);
  583. return device;
  584. }
  585. }
  586. dbg("item fetching failed at offset %d\n", (int)(end - start));
  587. kfree(device->collection);
  588. hid_free_device(device);
  589. kfree(parser);
  590. return NULL;
  591. }
  592. /*
  593. * Convert a signed n-bit integer to signed 32-bit integer. Common
  594. * cases are done through the compiler, the screwed things has to be
  595. * done by hand.
  596. */
  597. static __inline__ __s32 snto32(__u32 value, unsigned n)
  598. {
  599. switch (n) {
  600. case 8: return ((__s8)value);
  601. case 16: return ((__s16)value);
  602. case 32: return ((__s32)value);
  603. }
  604. return value & (1 << (n - 1)) ? value | (-1 << n) : value;
  605. }
  606. /*
  607. * Convert a signed 32-bit integer to a signed n-bit integer.
  608. */
  609. static __inline__ __u32 s32ton(__s32 value, unsigned n)
  610. {
  611. __s32 a = value >> (n - 1);
  612. if (a && a != -1)
  613. return value < 0 ? 1 << (n - 1) : (1 << (n - 1)) - 1;
  614. return value & ((1 << n) - 1);
  615. }
  616. /*
  617. * Extract/implement a data field from/to a report.
  618. */
  619. static __inline__ __u32 extract(__u8 *report, unsigned offset, unsigned n)
  620. {
  621. report += (offset >> 5) << 2; offset &= 31;
  622. return (le64_to_cpu(get_unaligned((__le64*)report)) >> offset) & ((1ULL << n) - 1);
  623. }
  624. static __inline__ void implement(__u8 *report, unsigned offset, unsigned n, __u32 value)
  625. {
  626. report += (offset >> 5) << 2; offset &= 31;
  627. put_unaligned((get_unaligned((__le64*)report)
  628. & cpu_to_le64(~((((__u64) 1 << n) - 1) << offset)))
  629. | cpu_to_le64((__u64)value << offset), (__le64*)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, struct pt_regs *regs)
  643. {
  644. hid_dump_input(usage, value);
  645. if (hid->claimed & HID_CLAIMED_INPUT)
  646. hidinput_hid_event(hid, field, usage, value, regs);
  647. if (hid->claimed & HID_CLAIMED_HIDDEV && interrupt)
  648. hiddev_hid_event(hid, field, usage, value, regs);
  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, struct pt_regs *regs)
  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, regs);
  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, regs);
  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, regs);
  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, struct pt_regs *regs)
  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. if (hid->claimed & HID_CLAIMED_HIDDEV)
  729. hiddev_report_event(hid, report);
  730. for (n = 0; n < report->maxfield; n++)
  731. hid_input_field(hid, report->field[n], data, interrupt, regs);
  732. if (hid->claimed & HID_CLAIMED_INPUT)
  733. hidinput_report_event(hid, report);
  734. return 0;
  735. }
  736. /*
  737. * Input interrupt completion handler.
  738. */
  739. static void hid_irq_in(struct urb *urb, struct pt_regs *regs)
  740. {
  741. struct hid_device *hid = urb->context;
  742. int status;
  743. switch (urb->status) {
  744. case 0: /* success */
  745. hid_input_report(HID_INPUT_REPORT, urb, 1, regs);
  746. break;
  747. case -ECONNRESET: /* unlink */
  748. case -ENOENT:
  749. case -EPERM:
  750. case -ESHUTDOWN: /* unplug */
  751. case -EILSEQ: /* unplug timeout on uhci */
  752. return;
  753. case -ETIMEDOUT: /* NAK */
  754. break;
  755. default: /* error */
  756. warn("input irq status %d received", urb->status);
  757. }
  758. status = usb_submit_urb(urb, SLAB_ATOMIC);
  759. if (status)
  760. err("can't resubmit intr, %s-%s/input%d, status %d",
  761. hid->dev->bus->bus_name, hid->dev->devpath,
  762. hid->ifnum, status);
  763. }
  764. /*
  765. * Output the field into the report.
  766. */
  767. static void hid_output_field(struct hid_field *field, __u8 *data)
  768. {
  769. unsigned count = field->report_count;
  770. unsigned offset = field->report_offset;
  771. unsigned size = field->report_size;
  772. unsigned n;
  773. for (n = 0; n < count; n++) {
  774. if (field->logical_minimum < 0) /* signed values */
  775. implement(data, offset + n * size, size, s32ton(field->value[n], size));
  776. else /* unsigned values */
  777. implement(data, offset + n * size, size, field->value[n]);
  778. }
  779. }
  780. /*
  781. * Create a report.
  782. */
  783. static void hid_output_report(struct hid_report *report, __u8 *data)
  784. {
  785. unsigned n;
  786. if (report->id > 0)
  787. *data++ = report->id;
  788. for (n = 0; n < report->maxfield; n++)
  789. hid_output_field(report->field[n], data);
  790. }
  791. /*
  792. * Set a field value. The report this field belongs to has to be
  793. * created and transferred to the device, to set this value in the
  794. * device.
  795. */
  796. int hid_set_field(struct hid_field *field, unsigned offset, __s32 value)
  797. {
  798. unsigned size = field->report_size;
  799. hid_dump_input(field->usage + offset, value);
  800. if (offset >= field->report_count) {
  801. dbg("offset (%d) exceeds report_count (%d)", offset, field->report_count);
  802. hid_dump_field(field, 8);
  803. return -1;
  804. }
  805. if (field->logical_minimum < 0) {
  806. if (value != snto32(s32ton(value, size), size)) {
  807. dbg("value %d is out of range", value);
  808. return -1;
  809. }
  810. }
  811. field->value[offset] = value;
  812. return 0;
  813. }
  814. /*
  815. * Find a report field with a specified HID usage.
  816. */
  817. struct hid_field *hid_find_field_by_usage(struct hid_device *hid, __u32 wanted_usage, int type)
  818. {
  819. struct hid_report *report;
  820. int i;
  821. list_for_each_entry(report, &hid->report_enum[type].report_list, list)
  822. for (i = 0; i < report->maxfield; i++)
  823. if (report->field[i]->logical == wanted_usage)
  824. return report->field[i];
  825. return NULL;
  826. }
  827. static int hid_submit_out(struct hid_device *hid)
  828. {
  829. struct hid_report *report;
  830. report = hid->out[hid->outtail];
  831. hid_output_report(report, hid->outbuf);
  832. hid->urbout->transfer_buffer_length = ((report->size - 1) >> 3) + 1 + (report->id > 0);
  833. hid->urbout->dev = hid->dev;
  834. dbg("submitting out urb");
  835. if (usb_submit_urb(hid->urbout, GFP_ATOMIC)) {
  836. err("usb_submit_urb(out) failed");
  837. return -1;
  838. }
  839. return 0;
  840. }
  841. static int hid_submit_ctrl(struct hid_device *hid)
  842. {
  843. struct hid_report *report;
  844. unsigned char dir;
  845. int len;
  846. report = hid->ctrl[hid->ctrltail].report;
  847. dir = hid->ctrl[hid->ctrltail].dir;
  848. len = ((report->size - 1) >> 3) + 1 + (report->id > 0);
  849. if (dir == USB_DIR_OUT) {
  850. hid_output_report(report, hid->ctrlbuf);
  851. hid->urbctrl->pipe = usb_sndctrlpipe(hid->dev, 0);
  852. hid->urbctrl->transfer_buffer_length = len;
  853. } else {
  854. int maxpacket, padlen;
  855. hid->urbctrl->pipe = usb_rcvctrlpipe(hid->dev, 0);
  856. maxpacket = usb_maxpacket(hid->dev, hid->urbctrl->pipe, 0);
  857. if (maxpacket > 0) {
  858. padlen = (len + maxpacket - 1) / maxpacket;
  859. padlen *= maxpacket;
  860. if (padlen > hid->bufsize)
  861. padlen = hid->bufsize;
  862. } else
  863. padlen = 0;
  864. hid->urbctrl->transfer_buffer_length = padlen;
  865. }
  866. hid->urbctrl->dev = hid->dev;
  867. hid->cr->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE | dir;
  868. hid->cr->bRequest = (dir == USB_DIR_OUT) ? HID_REQ_SET_REPORT : HID_REQ_GET_REPORT;
  869. hid->cr->wValue = cpu_to_le16(((report->type + 1) << 8) | report->id);
  870. hid->cr->wIndex = cpu_to_le16(hid->ifnum);
  871. hid->cr->wLength = cpu_to_le16(len);
  872. dbg("submitting ctrl urb: %s wValue=0x%04x wIndex=0x%04x wLength=%u",
  873. hid->cr->bRequest == HID_REQ_SET_REPORT ? "Set_Report" : "Get_Report",
  874. hid->cr->wValue, hid->cr->wIndex, hid->cr->wLength);
  875. if (usb_submit_urb(hid->urbctrl, GFP_ATOMIC)) {
  876. err("usb_submit_urb(ctrl) failed");
  877. return -1;
  878. }
  879. return 0;
  880. }
  881. /*
  882. * Output interrupt completion handler.
  883. */
  884. static void hid_irq_out(struct urb *urb, struct pt_regs *regs)
  885. {
  886. struct hid_device *hid = urb->context;
  887. unsigned long flags;
  888. int unplug = 0;
  889. switch (urb->status) {
  890. case 0: /* success */
  891. break;
  892. case -ESHUTDOWN: /* unplug */
  893. case -EILSEQ: /* unplug timeout on uhci */
  894. unplug = 1;
  895. case -ECONNRESET: /* unlink */
  896. case -ENOENT:
  897. break;
  898. default: /* error */
  899. warn("output irq status %d received", urb->status);
  900. }
  901. spin_lock_irqsave(&hid->outlock, flags);
  902. if (unplug)
  903. hid->outtail = hid->outhead;
  904. else
  905. hid->outtail = (hid->outtail + 1) & (HID_OUTPUT_FIFO_SIZE - 1);
  906. if (hid->outhead != hid->outtail) {
  907. if (hid_submit_out(hid)) {
  908. clear_bit(HID_OUT_RUNNING, &hid->iofl);;
  909. wake_up(&hid->wait);
  910. }
  911. spin_unlock_irqrestore(&hid->outlock, flags);
  912. return;
  913. }
  914. clear_bit(HID_OUT_RUNNING, &hid->iofl);
  915. spin_unlock_irqrestore(&hid->outlock, flags);
  916. wake_up(&hid->wait);
  917. }
  918. /*
  919. * Control pipe completion handler.
  920. */
  921. static void hid_ctrl(struct urb *urb, struct pt_regs *regs)
  922. {
  923. struct hid_device *hid = urb->context;
  924. unsigned long flags;
  925. int unplug = 0;
  926. spin_lock_irqsave(&hid->ctrllock, flags);
  927. switch (urb->status) {
  928. case 0: /* success */
  929. if (hid->ctrl[hid->ctrltail].dir == USB_DIR_IN)
  930. hid_input_report(hid->ctrl[hid->ctrltail].report->type, urb, 0, regs);
  931. break;
  932. case -ESHUTDOWN: /* unplug */
  933. case -EILSEQ: /* unplug timectrl on uhci */
  934. unplug = 1;
  935. case -ECONNRESET: /* unlink */
  936. case -ENOENT:
  937. case -EPIPE: /* report not available */
  938. break;
  939. default: /* error */
  940. warn("ctrl urb status %d received", urb->status);
  941. }
  942. if (unplug)
  943. hid->ctrltail = hid->ctrlhead;
  944. else
  945. hid->ctrltail = (hid->ctrltail + 1) & (HID_CONTROL_FIFO_SIZE - 1);
  946. if (hid->ctrlhead != hid->ctrltail) {
  947. if (hid_submit_ctrl(hid)) {
  948. clear_bit(HID_CTRL_RUNNING, &hid->iofl);
  949. wake_up(&hid->wait);
  950. }
  951. spin_unlock_irqrestore(&hid->ctrllock, flags);
  952. return;
  953. }
  954. clear_bit(HID_CTRL_RUNNING, &hid->iofl);
  955. spin_unlock_irqrestore(&hid->ctrllock, flags);
  956. wake_up(&hid->wait);
  957. }
  958. void hid_submit_report(struct hid_device *hid, struct hid_report *report, unsigned char dir)
  959. {
  960. int head;
  961. unsigned long flags;
  962. if ((hid->quirks & HID_QUIRK_NOGET) && dir == USB_DIR_IN)
  963. return;
  964. if (hid->urbout && dir == USB_DIR_OUT && report->type == HID_OUTPUT_REPORT) {
  965. spin_lock_irqsave(&hid->outlock, flags);
  966. if ((head = (hid->outhead + 1) & (HID_OUTPUT_FIFO_SIZE - 1)) == hid->outtail) {
  967. spin_unlock_irqrestore(&hid->outlock, flags);
  968. warn("output queue full");
  969. return;
  970. }
  971. hid->out[hid->outhead] = report;
  972. hid->outhead = head;
  973. if (!test_and_set_bit(HID_OUT_RUNNING, &hid->iofl))
  974. if (hid_submit_out(hid))
  975. clear_bit(HID_OUT_RUNNING, &hid->iofl);
  976. spin_unlock_irqrestore(&hid->outlock, flags);
  977. return;
  978. }
  979. spin_lock_irqsave(&hid->ctrllock, flags);
  980. if ((head = (hid->ctrlhead + 1) & (HID_CONTROL_FIFO_SIZE - 1)) == hid->ctrltail) {
  981. spin_unlock_irqrestore(&hid->ctrllock, flags);
  982. warn("control queue full");
  983. return;
  984. }
  985. hid->ctrl[hid->ctrlhead].report = report;
  986. hid->ctrl[hid->ctrlhead].dir = dir;
  987. hid->ctrlhead = head;
  988. if (!test_and_set_bit(HID_CTRL_RUNNING, &hid->iofl))
  989. if (hid_submit_ctrl(hid))
  990. clear_bit(HID_CTRL_RUNNING, &hid->iofl);
  991. spin_unlock_irqrestore(&hid->ctrllock, flags);
  992. }
  993. int hid_wait_io(struct hid_device *hid)
  994. {
  995. if (!wait_event_timeout(hid->wait, (!test_bit(HID_CTRL_RUNNING, &hid->iofl) &&
  996. !test_bit(HID_OUT_RUNNING, &hid->iofl)),
  997. 10*HZ)) {
  998. dbg("timeout waiting for ctrl or out queue to clear");
  999. return -1;
  1000. }
  1001. return 0;
  1002. }
  1003. static int hid_set_idle(struct usb_device *dev, int ifnum, int report, int idle)
  1004. {
  1005. return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  1006. HID_REQ_SET_IDLE, USB_TYPE_CLASS | USB_RECIP_INTERFACE, (idle << 8) | report,
  1007. ifnum, NULL, 0, USB_CTRL_SET_TIMEOUT);
  1008. }
  1009. static int hid_get_class_descriptor(struct usb_device *dev, int ifnum,
  1010. unsigned char type, void *buf, int size)
  1011. {
  1012. int result, retries = 4;
  1013. memset(buf,0,size); // Make sure we parse really received data
  1014. do {
  1015. result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  1016. USB_REQ_GET_DESCRIPTOR, USB_RECIP_INTERFACE | USB_DIR_IN,
  1017. (type << 8), ifnum, buf, size, USB_CTRL_GET_TIMEOUT);
  1018. retries--;
  1019. } while (result < size && retries);
  1020. return result;
  1021. }
  1022. int hid_open(struct hid_device *hid)
  1023. {
  1024. if (hid->open++)
  1025. return 0;
  1026. hid->urbin->dev = hid->dev;
  1027. if (usb_submit_urb(hid->urbin, GFP_KERNEL))
  1028. return -EIO;
  1029. return 0;
  1030. }
  1031. void hid_close(struct hid_device *hid)
  1032. {
  1033. if (!--hid->open)
  1034. usb_kill_urb(hid->urbin);
  1035. }
  1036. /*
  1037. * Initialize all reports
  1038. */
  1039. void hid_init_reports(struct hid_device *hid)
  1040. {
  1041. struct hid_report *report;
  1042. int err, ret;
  1043. list_for_each_entry(report, &hid->report_enum[HID_INPUT_REPORT].report_list, list)
  1044. hid_submit_report(hid, report, USB_DIR_IN);
  1045. list_for_each_entry(report, &hid->report_enum[HID_FEATURE_REPORT].report_list, list)
  1046. hid_submit_report(hid, report, USB_DIR_IN);
  1047. err = 0;
  1048. ret = hid_wait_io(hid);
  1049. while (ret) {
  1050. err |= ret;
  1051. if (test_bit(HID_CTRL_RUNNING, &hid->iofl))
  1052. usb_kill_urb(hid->urbctrl);
  1053. if (test_bit(HID_OUT_RUNNING, &hid->iofl))
  1054. usb_kill_urb(hid->urbout);
  1055. ret = hid_wait_io(hid);
  1056. }
  1057. if (err)
  1058. warn("timeout initializing reports\n");
  1059. }
  1060. #define USB_VENDOR_ID_WACOM 0x056a
  1061. #define USB_DEVICE_ID_WACOM_PENPARTNER 0x0000
  1062. #define USB_DEVICE_ID_WACOM_GRAPHIRE 0x0010
  1063. #define USB_DEVICE_ID_WACOM_INTUOS 0x0020
  1064. #define USB_DEVICE_ID_WACOM_PL 0x0030
  1065. #define USB_DEVICE_ID_WACOM_INTUOS2 0x0040
  1066. #define USB_DEVICE_ID_WACOM_VOLITO 0x0060
  1067. #define USB_DEVICE_ID_WACOM_PTU 0x0003
  1068. #define USB_DEVICE_ID_WACOM_INTUOS3 0x00B0
  1069. #define USB_DEVICE_ID_WACOM_CINTIQ 0x003F
  1070. #define USB_VENDOR_ID_ACECAD 0x0460
  1071. #define USB_DEVICE_ID_ACECAD_FLAIR 0x0004
  1072. #define USB_DEVICE_ID_ACECAD_302 0x0008
  1073. #define USB_VENDOR_ID_KBGEAR 0x084e
  1074. #define USB_DEVICE_ID_KBGEAR_JAMSTUDIO 0x1001
  1075. #define USB_VENDOR_ID_AIPTEK 0x08ca
  1076. #define USB_DEVICE_ID_AIPTEK_01 0x0001
  1077. #define USB_DEVICE_ID_AIPTEK_10 0x0010
  1078. #define USB_DEVICE_ID_AIPTEK_20 0x0020
  1079. #define USB_DEVICE_ID_AIPTEK_21 0x0021
  1080. #define USB_DEVICE_ID_AIPTEK_22 0x0022
  1081. #define USB_DEVICE_ID_AIPTEK_23 0x0023
  1082. #define USB_DEVICE_ID_AIPTEK_24 0x0024
  1083. #define USB_VENDOR_ID_GRIFFIN 0x077d
  1084. #define USB_DEVICE_ID_POWERMATE 0x0410
  1085. #define USB_DEVICE_ID_SOUNDKNOB 0x04AA
  1086. #define USB_VENDOR_ID_ATEN 0x0557
  1087. #define USB_DEVICE_ID_ATEN_UC100KM 0x2004
  1088. #define USB_DEVICE_ID_ATEN_CS124U 0x2202
  1089. #define USB_DEVICE_ID_ATEN_2PORTKVM 0x2204
  1090. #define USB_DEVICE_ID_ATEN_4PORTKVM 0x2205
  1091. #define USB_DEVICE_ID_ATEN_4PORTKVMC 0x2208
  1092. #define USB_VENDOR_ID_TOPMAX 0x0663
  1093. #define USB_DEVICE_ID_TOPMAX_COBRAPAD 0x0103
  1094. #define USB_VENDOR_ID_HAPP 0x078b
  1095. #define USB_DEVICE_ID_UGCI_DRIVING 0x0010
  1096. #define USB_DEVICE_ID_UGCI_FLYING 0x0020
  1097. #define USB_DEVICE_ID_UGCI_FIGHTING 0x0030
  1098. #define USB_VENDOR_ID_MGE 0x0463
  1099. #define USB_DEVICE_ID_MGE_UPS 0xffff
  1100. #define USB_DEVICE_ID_MGE_UPS1 0x0001
  1101. #define USB_VENDOR_ID_ONTRAK 0x0a07
  1102. #define USB_DEVICE_ID_ONTRAK_ADU100 0x0064
  1103. #define USB_VENDOR_ID_TANGTOP 0x0d3d
  1104. #define USB_DEVICE_ID_TANGTOP_USBPS2 0x0001
  1105. #define USB_VENDOR_ID_ESSENTIAL_REALITY 0x0d7f
  1106. #define USB_DEVICE_ID_ESSENTIAL_REALITY_P5 0x0100
  1107. #define USB_VENDOR_ID_A4TECH 0x09da
  1108. #define USB_DEVICE_ID_A4TECH_WCP32PU 0x0006
  1109. #define USB_VENDOR_ID_AASHIMA 0x06d6
  1110. #define USB_DEVICE_ID_AASHIMA_GAMEPAD 0x0025
  1111. #define USB_DEVICE_ID_AASHIMA_PREDATOR 0x0026
  1112. #define USB_VENDOR_ID_CYPRESS 0x04b4
  1113. #define USB_DEVICE_ID_CYPRESS_MOUSE 0x0001
  1114. #define USB_DEVICE_ID_CYPRESS_HIDCOM 0x5500
  1115. #define USB_DEVICE_ID_CYPRESS_ULTRAMOUSE 0x7417
  1116. #define USB_VENDOR_ID_BERKSHIRE 0x0c98
  1117. #define USB_DEVICE_ID_BERKSHIRE_PCWD 0x1140
  1118. #define USB_VENDOR_ID_ALPS 0x0433
  1119. #define USB_DEVICE_ID_IBM_GAMEPAD 0x1101
  1120. #define USB_VENDOR_ID_SAITEK 0x06a3
  1121. #define USB_DEVICE_ID_SAITEK_RUMBLEPAD 0xff17
  1122. #define USB_VENDOR_ID_NEC 0x073e
  1123. #define USB_DEVICE_ID_NEC_USB_GAME_PAD 0x0301
  1124. #define USB_VENDOR_ID_CHIC 0x05fe
  1125. #define USB_DEVICE_ID_CHIC_GAMEPAD 0x0014
  1126. #define USB_VENDOR_ID_GLAB 0x06c2
  1127. #define USB_DEVICE_ID_4_PHIDGETSERVO_30 0x0038
  1128. #define USB_DEVICE_ID_1_PHIDGETSERVO_30 0x0039
  1129. #define USB_DEVICE_ID_8_8_8_IF_KIT 0x0045
  1130. #define USB_DEVICE_ID_0_0_4_IF_KIT 0x0040
  1131. #define USB_DEVICE_ID_0_8_8_IF_KIT 0x0053
  1132. #define USB_VENDOR_ID_WISEGROUP 0x0925
  1133. #define USB_DEVICE_ID_1_PHIDGETSERVO_20 0x8101
  1134. #define USB_DEVICE_ID_4_PHIDGETSERVO_20 0x8104
  1135. #define USB_VENDOR_ID_CODEMERCS 0x07c0
  1136. #define USB_DEVICE_ID_CODEMERCS_IOW40 0x1500
  1137. #define USB_DEVICE_ID_CODEMERCS_IOW24 0x1501
  1138. #define USB_DEVICE_ID_CODEMERCS_IOW48 0x1502
  1139. #define USB_DEVICE_ID_CODEMERCS_IOW28 0x1503
  1140. #define USB_VENDOR_ID_DELORME 0x1163
  1141. #define USB_DEVICE_ID_DELORME_EARTHMATE 0x0100
  1142. #define USB_DEVICE_ID_DELORME_EM_LT20 0x0200
  1143. #define USB_VENDOR_ID_MCC 0x09db
  1144. #define USB_DEVICE_ID_MCC_PMD1024LS 0x0076
  1145. #define USB_DEVICE_ID_MCC_PMD1208LS 0x007a
  1146. #define USB_VENDOR_ID_CHICONY 0x04f2
  1147. #define USB_DEVICE_ID_CHICONY_USBHUB_KB 0x0100
  1148. #define USB_VENDOR_ID_BTC 0x046e
  1149. #define USB_DEVICE_ID_BTC_KEYBOARD 0x5303
  1150. #define USB_VENDOR_ID_VERNIER 0x08f7
  1151. #define USB_DEVICE_ID_VERNIER_LABPRO 0x0001
  1152. #define USB_DEVICE_ID_VERNIER_GOTEMP 0x0002
  1153. #define USB_DEVICE_ID_VERNIER_SKIP 0x0003
  1154. #define USB_DEVICE_ID_VERNIER_CYCLOPS 0x0004
  1155. #define USB_VENDOR_ID_LD 0x0f11
  1156. #define USB_DEVICE_ID_CASSY 0x1000
  1157. #define USB_DEVICE_ID_POCKETCASSY 0x1010
  1158. #define USB_DEVICE_ID_MOBILECASSY 0x1020
  1159. #define USB_DEVICE_ID_JWM 0x1080
  1160. #define USB_DEVICE_ID_DMMP 0x1081
  1161. #define USB_DEVICE_ID_UMIP 0x1090
  1162. #define USB_DEVICE_ID_VIDEOCOM 0x1200
  1163. #define USB_DEVICE_ID_COM3LAB 0x2000
  1164. #define USB_DEVICE_ID_TELEPORT 0x2010
  1165. #define USB_DEVICE_ID_NETWORKANALYSER 0x2020
  1166. #define USB_DEVICE_ID_POWERCONTROL 0x2030
  1167. #define USB_VENDOR_ID_APPLE 0x05ac
  1168. #define USB_DEVICE_ID_APPLE_POWERMOUSE 0x0304
  1169. /*
  1170. * Alphabetically sorted blacklist by quirk type.
  1171. */
  1172. static struct hid_blacklist {
  1173. __u16 idVendor;
  1174. __u16 idProduct;
  1175. unsigned quirks;
  1176. } hid_blacklist[] = {
  1177. { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_01, HID_QUIRK_IGNORE },
  1178. { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_10, HID_QUIRK_IGNORE },
  1179. { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_20, HID_QUIRK_IGNORE },
  1180. { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_21, HID_QUIRK_IGNORE },
  1181. { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_22, HID_QUIRK_IGNORE },
  1182. { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_23, HID_QUIRK_IGNORE },
  1183. { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_24, HID_QUIRK_IGNORE },
  1184. { USB_VENDOR_ID_BERKSHIRE, USB_DEVICE_ID_BERKSHIRE_PCWD, HID_QUIRK_IGNORE },
  1185. { USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW40, HID_QUIRK_IGNORE },
  1186. { USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW24, HID_QUIRK_IGNORE },
  1187. { USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW48, HID_QUIRK_IGNORE },
  1188. { USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW28, HID_QUIRK_IGNORE },
  1189. { USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_HIDCOM, HID_QUIRK_IGNORE },
  1190. { USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_ULTRAMOUSE, HID_QUIRK_IGNORE },
  1191. { USB_VENDOR_ID_DELORME, USB_DEVICE_ID_DELORME_EARTHMATE, HID_QUIRK_IGNORE },
  1192. { USB_VENDOR_ID_DELORME, USB_DEVICE_ID_DELORME_EM_LT20, HID_QUIRK_IGNORE },
  1193. { USB_VENDOR_ID_ESSENTIAL_REALITY, USB_DEVICE_ID_ESSENTIAL_REALITY_P5, HID_QUIRK_IGNORE },
  1194. { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_4_PHIDGETSERVO_30, HID_QUIRK_IGNORE },
  1195. { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_1_PHIDGETSERVO_30, HID_QUIRK_IGNORE },
  1196. { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_8_8_8_IF_KIT, HID_QUIRK_IGNORE },
  1197. { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_0_4_IF_KIT, HID_QUIRK_IGNORE },
  1198. { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_8_8_IF_KIT, HID_QUIRK_IGNORE },
  1199. { USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_POWERMATE, HID_QUIRK_IGNORE },
  1200. { USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_SOUNDKNOB, HID_QUIRK_IGNORE },
  1201. { USB_VENDOR_ID_KBGEAR, USB_DEVICE_ID_KBGEAR_JAMSTUDIO, HID_QUIRK_IGNORE },
  1202. { USB_VENDOR_ID_LD, USB_DEVICE_ID_CASSY, HID_QUIRK_IGNORE },
  1203. { USB_VENDOR_ID_LD, USB_DEVICE_ID_POCKETCASSY, HID_QUIRK_IGNORE },
  1204. { USB_VENDOR_ID_LD, USB_DEVICE_ID_MOBILECASSY, HID_QUIRK_IGNORE },
  1205. { USB_VENDOR_ID_LD, USB_DEVICE_ID_JWM, HID_QUIRK_IGNORE },
  1206. { USB_VENDOR_ID_LD, USB_DEVICE_ID_DMMP, HID_QUIRK_IGNORE },
  1207. { USB_VENDOR_ID_LD, USB_DEVICE_ID_UMIP, HID_QUIRK_IGNORE },
  1208. { USB_VENDOR_ID_LD, USB_DEVICE_ID_VIDEOCOM, HID_QUIRK_IGNORE },
  1209. { USB_VENDOR_ID_LD, USB_DEVICE_ID_COM3LAB, HID_QUIRK_IGNORE },
  1210. { USB_VENDOR_ID_LD, USB_DEVICE_ID_TELEPORT, HID_QUIRK_IGNORE },
  1211. { USB_VENDOR_ID_LD, USB_DEVICE_ID_NETWORKANALYSER, HID_QUIRK_IGNORE },
  1212. { USB_VENDOR_ID_LD, USB_DEVICE_ID_POWERCONTROL, HID_QUIRK_IGNORE },
  1213. { USB_VENDOR_ID_MCC, USB_DEVICE_ID_MCC_PMD1024LS, HID_QUIRK_IGNORE },
  1214. { USB_VENDOR_ID_MCC, USB_DEVICE_ID_MCC_PMD1208LS, HID_QUIRK_IGNORE },
  1215. { USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS, HID_QUIRK_IGNORE },
  1216. { USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS1, HID_QUIRK_IGNORE },
  1217. { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100, HID_QUIRK_IGNORE },
  1218. { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 100, HID_QUIRK_IGNORE },
  1219. { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 200, HID_QUIRK_IGNORE },
  1220. { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 300, HID_QUIRK_IGNORE },
  1221. { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 400, HID_QUIRK_IGNORE },
  1222. { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 500, HID_QUIRK_IGNORE },
  1223. { USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_LABPRO, HID_QUIRK_IGNORE },
  1224. { USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_GOTEMP, HID_QUIRK_IGNORE },
  1225. { USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_SKIP, HID_QUIRK_IGNORE },
  1226. { USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_CYCLOPS, HID_QUIRK_IGNORE },
  1227. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PENPARTNER, HID_QUIRK_IGNORE },
  1228. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE, HID_QUIRK_IGNORE },
  1229. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE + 1, HID_QUIRK_IGNORE },
  1230. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE + 2, HID_QUIRK_IGNORE },
  1231. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE + 3, HID_QUIRK_IGNORE },
  1232. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE + 4, HID_QUIRK_IGNORE },
  1233. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS, HID_QUIRK_IGNORE },
  1234. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS + 1, HID_QUIRK_IGNORE },
  1235. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS + 2, HID_QUIRK_IGNORE },
  1236. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS + 3, HID_QUIRK_IGNORE },
  1237. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS + 4, HID_QUIRK_IGNORE },
  1238. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL, HID_QUIRK_IGNORE },
  1239. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 1, HID_QUIRK_IGNORE },
  1240. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 2, HID_QUIRK_IGNORE },
  1241. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 3, HID_QUIRK_IGNORE },
  1242. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 4, HID_QUIRK_IGNORE },
  1243. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 5, HID_QUIRK_IGNORE },
  1244. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 1, HID_QUIRK_IGNORE },
  1245. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 2, HID_QUIRK_IGNORE },
  1246. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 3, HID_QUIRK_IGNORE },
  1247. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 4, HID_QUIRK_IGNORE },
  1248. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 5, HID_QUIRK_IGNORE },
  1249. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 7, HID_QUIRK_IGNORE },
  1250. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_VOLITO, HID_QUIRK_IGNORE },
  1251. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PTU, HID_QUIRK_IGNORE },
  1252. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS3, HID_QUIRK_IGNORE },
  1253. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS3 + 1, HID_QUIRK_IGNORE },
  1254. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS3 + 2, HID_QUIRK_IGNORE },
  1255. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_CINTIQ, HID_QUIRK_IGNORE },
  1256. { USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_4_PHIDGETSERVO_20, HID_QUIRK_IGNORE },
  1257. { USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_1_PHIDGETSERVO_20, HID_QUIRK_IGNORE },
  1258. { USB_VENDOR_ID_ACECAD, USB_DEVICE_ID_ACECAD_FLAIR, HID_QUIRK_IGNORE },
  1259. { USB_VENDOR_ID_ACECAD, USB_DEVICE_ID_ACECAD_302, HID_QUIRK_IGNORE },
  1260. { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_UC100KM, HID_QUIRK_NOGET },
  1261. { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_CS124U, HID_QUIRK_NOGET },
  1262. { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_2PORTKVM, HID_QUIRK_NOGET },
  1263. { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_4PORTKVM, HID_QUIRK_NOGET },
  1264. { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_4PORTKVMC, HID_QUIRK_NOGET },
  1265. { USB_VENDOR_ID_BTC, USB_DEVICE_ID_BTC_KEYBOARD, HID_QUIRK_NOGET},
  1266. { USB_VENDOR_ID_CHICONY, USB_DEVICE_ID_CHICONY_USBHUB_KB, HID_QUIRK_NOGET},
  1267. { USB_VENDOR_ID_TANGTOP, USB_DEVICE_ID_TANGTOP_USBPS2, HID_QUIRK_NOGET },
  1268. { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_POWERMOUSE, HID_QUIRK_2WHEEL_POWERMOUSE },
  1269. { USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_WCP32PU, HID_QUIRK_2WHEEL_MOUSE_HACK_7 },
  1270. { USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_MOUSE, HID_QUIRK_2WHEEL_MOUSE_HACK_5 },
  1271. { USB_VENDOR_ID_AASHIMA, USB_DEVICE_ID_AASHIMA_GAMEPAD, HID_QUIRK_BADPAD },
  1272. { USB_VENDOR_ID_AASHIMA, USB_DEVICE_ID_AASHIMA_PREDATOR, HID_QUIRK_BADPAD },
  1273. { USB_VENDOR_ID_ALPS, USB_DEVICE_ID_IBM_GAMEPAD, HID_QUIRK_BADPAD },
  1274. { USB_VENDOR_ID_CHIC, USB_DEVICE_ID_CHIC_GAMEPAD, HID_QUIRK_BADPAD },
  1275. { USB_VENDOR_ID_HAPP, USB_DEVICE_ID_UGCI_DRIVING, HID_QUIRK_BADPAD | HID_QUIRK_MULTI_INPUT },
  1276. { USB_VENDOR_ID_HAPP, USB_DEVICE_ID_UGCI_FLYING, HID_QUIRK_BADPAD | HID_QUIRK_MULTI_INPUT },
  1277. { USB_VENDOR_ID_HAPP, USB_DEVICE_ID_UGCI_FIGHTING, HID_QUIRK_BADPAD | HID_QUIRK_MULTI_INPUT },
  1278. { USB_VENDOR_ID_NEC, USB_DEVICE_ID_NEC_USB_GAME_PAD, HID_QUIRK_BADPAD },
  1279. { USB_VENDOR_ID_SAITEK, USB_DEVICE_ID_SAITEK_RUMBLEPAD, HID_QUIRK_BADPAD },
  1280. { USB_VENDOR_ID_TOPMAX, USB_DEVICE_ID_TOPMAX_COBRAPAD, HID_QUIRK_BADPAD },
  1281. { 0, 0 }
  1282. };
  1283. /*
  1284. * Traverse the supplied list of reports and find the longest
  1285. */
  1286. static void hid_find_max_report(struct hid_device *hid, unsigned int type, int *max)
  1287. {
  1288. struct hid_report *report;
  1289. int size;
  1290. list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
  1291. size = ((report->size - 1) >> 3) + 1;
  1292. if (type == HID_INPUT_REPORT && hid->report_enum[type].numbered)
  1293. size++;
  1294. if (*max < size)
  1295. *max = size;
  1296. }
  1297. }
  1298. static int hid_alloc_buffers(struct usb_device *dev, struct hid_device *hid)
  1299. {
  1300. if (!(hid->inbuf = usb_buffer_alloc(dev, hid->bufsize, SLAB_ATOMIC, &hid->inbuf_dma)))
  1301. return -1;
  1302. if (!(hid->outbuf = usb_buffer_alloc(dev, hid->bufsize, SLAB_ATOMIC, &hid->outbuf_dma)))
  1303. return -1;
  1304. if (!(hid->cr = usb_buffer_alloc(dev, sizeof(*(hid->cr)), SLAB_ATOMIC, &hid->cr_dma)))
  1305. return -1;
  1306. if (!(hid->ctrlbuf = usb_buffer_alloc(dev, hid->bufsize, SLAB_ATOMIC, &hid->ctrlbuf_dma)))
  1307. return -1;
  1308. return 0;
  1309. }
  1310. static void hid_free_buffers(struct usb_device *dev, struct hid_device *hid)
  1311. {
  1312. if (hid->inbuf)
  1313. usb_buffer_free(dev, hid->bufsize, hid->inbuf, hid->inbuf_dma);
  1314. if (hid->outbuf)
  1315. usb_buffer_free(dev, hid->bufsize, hid->outbuf, hid->outbuf_dma);
  1316. if (hid->cr)
  1317. usb_buffer_free(dev, sizeof(*(hid->cr)), hid->cr, hid->cr_dma);
  1318. if (hid->ctrlbuf)
  1319. usb_buffer_free(dev, hid->bufsize, hid->ctrlbuf, hid->ctrlbuf_dma);
  1320. }
  1321. static struct hid_device *usb_hid_configure(struct usb_interface *intf)
  1322. {
  1323. struct usb_host_interface *interface = intf->cur_altsetting;
  1324. struct usb_device *dev = interface_to_usbdev (intf);
  1325. struct hid_descriptor *hdesc;
  1326. struct hid_device *hid;
  1327. unsigned quirks = 0, rsize = 0;
  1328. char *buf, *rdesc;
  1329. int n, insize = 0;
  1330. for (n = 0; hid_blacklist[n].idVendor; n++)
  1331. if ((hid_blacklist[n].idVendor == le16_to_cpu(dev->descriptor.idVendor)) &&
  1332. (hid_blacklist[n].idProduct == le16_to_cpu(dev->descriptor.idProduct)))
  1333. quirks = hid_blacklist[n].quirks;
  1334. if (quirks & HID_QUIRK_IGNORE)
  1335. return NULL;
  1336. if (usb_get_extra_descriptor(interface, HID_DT_HID, &hdesc) && ((!interface->desc.bNumEndpoints) ||
  1337. usb_get_extra_descriptor(&interface->endpoint[0], HID_DT_HID, &hdesc))) {
  1338. dbg("class descriptor not present\n");
  1339. return NULL;
  1340. }
  1341. for (n = 0; n < hdesc->bNumDescriptors; n++)
  1342. if (hdesc->desc[n].bDescriptorType == HID_DT_REPORT)
  1343. rsize = le16_to_cpu(hdesc->desc[n].wDescriptorLength);
  1344. if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
  1345. dbg("weird size of report descriptor (%u)", rsize);
  1346. return NULL;
  1347. }
  1348. if (!(rdesc = kmalloc(rsize, GFP_KERNEL))) {
  1349. dbg("couldn't allocate rdesc memory");
  1350. return NULL;
  1351. }
  1352. hid_set_idle(dev, interface->desc.bInterfaceNumber, 0, 0);
  1353. if ((n = hid_get_class_descriptor(dev, interface->desc.bInterfaceNumber, HID_DT_REPORT, rdesc, rsize)) < 0) {
  1354. dbg("reading report descriptor failed");
  1355. kfree(rdesc);
  1356. return NULL;
  1357. }
  1358. #ifdef DEBUG_DATA
  1359. printk(KERN_DEBUG __FILE__ ": report descriptor (size %u, read %d) = ", rsize, n);
  1360. for (n = 0; n < rsize; n++)
  1361. printk(" %02x", (unsigned char) rdesc[n]);
  1362. printk("\n");
  1363. #endif
  1364. if (!(hid = hid_parse_report(rdesc, n))) {
  1365. dbg("parsing report descriptor failed");
  1366. kfree(rdesc);
  1367. return NULL;
  1368. }
  1369. kfree(rdesc);
  1370. hid->quirks = quirks;
  1371. hid->bufsize = HID_MIN_BUFFER_SIZE;
  1372. hid_find_max_report(hid, HID_INPUT_REPORT, &hid->bufsize);
  1373. hid_find_max_report(hid, HID_OUTPUT_REPORT, &hid->bufsize);
  1374. hid_find_max_report(hid, HID_FEATURE_REPORT, &hid->bufsize);
  1375. if (hid->bufsize > HID_MAX_BUFFER_SIZE)
  1376. hid->bufsize = HID_MAX_BUFFER_SIZE;
  1377. hid_find_max_report(hid, HID_INPUT_REPORT, &insize);
  1378. if (insize > HID_MAX_BUFFER_SIZE)
  1379. insize = HID_MAX_BUFFER_SIZE;
  1380. if (hid_alloc_buffers(dev, hid)) {
  1381. hid_free_buffers(dev, hid);
  1382. goto fail;
  1383. }
  1384. for (n = 0; n < interface->desc.bNumEndpoints; n++) {
  1385. struct usb_endpoint_descriptor *endpoint;
  1386. int pipe;
  1387. int interval;
  1388. endpoint = &interface->endpoint[n].desc;
  1389. if ((endpoint->bmAttributes & 3) != 3) /* Not an interrupt endpoint */
  1390. continue;
  1391. /* handle potential highspeed HID correctly */
  1392. interval = endpoint->bInterval;
  1393. if (dev->speed == USB_SPEED_HIGH)
  1394. interval = 1 << (interval - 1);
  1395. /* Change the polling interval of mice. */
  1396. if (hid->collection->usage == HID_GD_MOUSE && hid_mousepoll_interval > 0)
  1397. interval = hid_mousepoll_interval;
  1398. if (endpoint->bEndpointAddress & USB_DIR_IN) {
  1399. if (hid->urbin)
  1400. continue;
  1401. if (!(hid->urbin = usb_alloc_urb(0, GFP_KERNEL)))
  1402. goto fail;
  1403. pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
  1404. usb_fill_int_urb(hid->urbin, dev, pipe, hid->inbuf, insize,
  1405. hid_irq_in, hid, interval);
  1406. hid->urbin->transfer_dma = hid->inbuf_dma;
  1407. hid->urbin->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  1408. } else {
  1409. if (hid->urbout)
  1410. continue;
  1411. if (!(hid->urbout = usb_alloc_urb(0, GFP_KERNEL)))
  1412. goto fail;
  1413. pipe = usb_sndintpipe(dev, endpoint->bEndpointAddress);
  1414. usb_fill_int_urb(hid->urbout, dev, pipe, hid->outbuf, 0,
  1415. hid_irq_out, hid, interval);
  1416. hid->urbout->transfer_dma = hid->outbuf_dma;
  1417. hid->urbout->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  1418. }
  1419. }
  1420. if (!hid->urbin) {
  1421. err("couldn't find an input interrupt endpoint");
  1422. goto fail;
  1423. }
  1424. init_waitqueue_head(&hid->wait);
  1425. spin_lock_init(&hid->outlock);
  1426. spin_lock_init(&hid->ctrllock);
  1427. hid->version = le16_to_cpu(hdesc->bcdHID);
  1428. hid->country = hdesc->bCountryCode;
  1429. hid->dev = dev;
  1430. hid->intf = intf;
  1431. hid->ifnum = interface->desc.bInterfaceNumber;
  1432. hid->name[0] = 0;
  1433. if (!(buf = kmalloc(64, GFP_KERNEL)))
  1434. goto fail;
  1435. if (dev->manufacturer) {
  1436. strcat(hid->name, dev->manufacturer);
  1437. if (dev->product)
  1438. snprintf(hid->name, 64, "%s %s", hid->name, dev->product);
  1439. } else if (dev->product) {
  1440. snprintf(hid->name, 128, "%s", dev->product);
  1441. } else
  1442. snprintf(hid->name, 128, "%04x:%04x",
  1443. le16_to_cpu(dev->descriptor.idVendor),
  1444. le16_to_cpu(dev->descriptor.idProduct));
  1445. usb_make_path(dev, buf, 64);
  1446. snprintf(hid->phys, 64, "%s/input%d", buf,
  1447. intf->altsetting[0].desc.bInterfaceNumber);
  1448. if (usb_string(dev, dev->descriptor.iSerialNumber, hid->uniq, 64) <= 0)
  1449. hid->uniq[0] = 0;
  1450. kfree(buf);
  1451. hid->urbctrl = usb_alloc_urb(0, GFP_KERNEL);
  1452. if (!hid->urbctrl)
  1453. goto fail;
  1454. usb_fill_control_urb(hid->urbctrl, dev, 0, (void *) hid->cr,
  1455. hid->ctrlbuf, 1, hid_ctrl, hid);
  1456. hid->urbctrl->setup_dma = hid->cr_dma;
  1457. hid->urbctrl->transfer_dma = hid->ctrlbuf_dma;
  1458. hid->urbctrl->transfer_flags |= (URB_NO_TRANSFER_DMA_MAP | URB_NO_SETUP_DMA_MAP);
  1459. return hid;
  1460. fail:
  1461. if (hid->urbin)
  1462. usb_free_urb(hid->urbin);
  1463. if (hid->urbout)
  1464. usb_free_urb(hid->urbout);
  1465. if (hid->urbctrl)
  1466. usb_free_urb(hid->urbctrl);
  1467. hid_free_buffers(dev, hid);
  1468. hid_free_device(hid);
  1469. return NULL;
  1470. }
  1471. static void hid_disconnect(struct usb_interface *intf)
  1472. {
  1473. struct hid_device *hid = usb_get_intfdata (intf);
  1474. if (!hid)
  1475. return;
  1476. usb_set_intfdata(intf, NULL);
  1477. usb_kill_urb(hid->urbin);
  1478. usb_kill_urb(hid->urbout);
  1479. usb_kill_urb(hid->urbctrl);
  1480. if (hid->claimed & HID_CLAIMED_INPUT)
  1481. hidinput_disconnect(hid);
  1482. if (hid->claimed & HID_CLAIMED_HIDDEV)
  1483. hiddev_disconnect(hid);
  1484. usb_free_urb(hid->urbin);
  1485. usb_free_urb(hid->urbctrl);
  1486. if (hid->urbout)
  1487. usb_free_urb(hid->urbout);
  1488. hid_free_buffers(hid->dev, hid);
  1489. hid_free_device(hid);
  1490. }
  1491. static int hid_probe(struct usb_interface *intf, const struct usb_device_id *id)
  1492. {
  1493. struct hid_device *hid;
  1494. char path[64];
  1495. int i;
  1496. char *c;
  1497. dbg("HID probe called for ifnum %d",
  1498. intf->altsetting->desc.bInterfaceNumber);
  1499. if (!(hid = usb_hid_configure(intf)))
  1500. return -ENODEV;
  1501. hid_init_reports(hid);
  1502. hid_dump_device(hid);
  1503. if (!hidinput_connect(hid))
  1504. hid->claimed |= HID_CLAIMED_INPUT;
  1505. if (!hiddev_connect(hid))
  1506. hid->claimed |= HID_CLAIMED_HIDDEV;
  1507. usb_set_intfdata(intf, hid);
  1508. if (!hid->claimed) {
  1509. printk ("HID device not claimed by input or hiddev\n");
  1510. hid_disconnect(intf);
  1511. return -ENODEV;
  1512. }
  1513. printk(KERN_INFO);
  1514. if (hid->claimed & HID_CLAIMED_INPUT)
  1515. printk("input");
  1516. if (hid->claimed == (HID_CLAIMED_INPUT | HID_CLAIMED_HIDDEV))
  1517. printk(",");
  1518. if (hid->claimed & HID_CLAIMED_HIDDEV)
  1519. printk("hiddev%d", hid->minor);
  1520. c = "Device";
  1521. for (i = 0; i < hid->maxcollection; i++) {
  1522. if (hid->collection[i].type == HID_COLLECTION_APPLICATION &&
  1523. (hid->collection[i].usage & HID_USAGE_PAGE) == HID_UP_GENDESK &&
  1524. (hid->collection[i].usage & 0xffff) < ARRAY_SIZE(hid_types)) {
  1525. c = hid_types[hid->collection[i].usage & 0xffff];
  1526. break;
  1527. }
  1528. }
  1529. usb_make_path(interface_to_usbdev(intf), path, 63);
  1530. printk(": USB HID v%x.%02x %s [%s] on %s\n",
  1531. hid->version >> 8, hid->version & 0xff, c, hid->name, path);
  1532. return 0;
  1533. }
  1534. static int hid_suspend(struct usb_interface *intf, pm_message_t message)
  1535. {
  1536. struct hid_device *hid = usb_get_intfdata (intf);
  1537. usb_kill_urb(hid->urbin);
  1538. intf->dev.power.power_state = PMSG_SUSPEND;
  1539. dev_dbg(&intf->dev, "suspend\n");
  1540. return 0;
  1541. }
  1542. static int hid_resume(struct usb_interface *intf)
  1543. {
  1544. struct hid_device *hid = usb_get_intfdata (intf);
  1545. int status;
  1546. intf->dev.power.power_state = PMSG_ON;
  1547. if (hid->open)
  1548. status = usb_submit_urb(hid->urbin, GFP_NOIO);
  1549. else
  1550. status = 0;
  1551. dev_dbg(&intf->dev, "resume status %d\n", status);
  1552. return status;
  1553. }
  1554. static struct usb_device_id hid_usb_ids [] = {
  1555. { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
  1556. .bInterfaceClass = USB_INTERFACE_CLASS_HID },
  1557. { } /* Terminating entry */
  1558. };
  1559. MODULE_DEVICE_TABLE (usb, hid_usb_ids);
  1560. static struct usb_driver hid_driver = {
  1561. .owner = THIS_MODULE,
  1562. .name = "usbhid",
  1563. .probe = hid_probe,
  1564. .disconnect = hid_disconnect,
  1565. .suspend = hid_suspend,
  1566. .resume = hid_resume,
  1567. .id_table = hid_usb_ids,
  1568. };
  1569. static int __init hid_init(void)
  1570. {
  1571. int retval;
  1572. retval = hiddev_init();
  1573. if (retval)
  1574. goto hiddev_init_fail;
  1575. retval = usb_register(&hid_driver);
  1576. if (retval)
  1577. goto usb_register_fail;
  1578. info(DRIVER_VERSION ":" DRIVER_DESC);
  1579. return 0;
  1580. usb_register_fail:
  1581. hiddev_exit();
  1582. hiddev_init_fail:
  1583. return retval;
  1584. }
  1585. static void __exit hid_exit(void)
  1586. {
  1587. usb_deregister(&hid_driver);
  1588. hiddev_exit();
  1589. }
  1590. module_init(hid_init);
  1591. module_exit(hid_exit);
  1592. MODULE_AUTHOR(DRIVER_AUTHOR);
  1593. MODULE_DESCRIPTION(DRIVER_DESC);
  1594. MODULE_LICENSE(DRIVER_LICENSE);