hid-core.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864
  1. /*
  2. * USB HID support for Linux
  3. *
  4. * Copyright (c) 1999 Andreas Gal
  5. * Copyright (c) 2000-2001 Vojtech Pavlik <vojtech@suse.cz>
  6. */
  7. /*
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the Free
  10. * Software Foundation; either version 2 of the License, or (at your option)
  11. * any later version.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/slab.h>
  15. #include <linux/init.h>
  16. #include <linux/kernel.h>
  17. #include <linux/sched.h>
  18. #include <linux/list.h>
  19. #include <linux/mm.h>
  20. #include <linux/smp_lock.h>
  21. #include <linux/spinlock.h>
  22. #include <asm/unaligned.h>
  23. #include <asm/byteorder.h>
  24. #include <linux/input.h>
  25. #include <linux/wait.h>
  26. #undef DEBUG
  27. #undef DEBUG_DATA
  28. #include <linux/usb.h>
  29. #include "hid.h"
  30. #include <linux/hiddev.h>
  31. /*
  32. * Version Information
  33. */
  34. #define DRIVER_VERSION "v2.01"
  35. #define DRIVER_AUTHOR "Andreas Gal, Vojtech Pavlik"
  36. #define DRIVER_DESC "USB HID core driver"
  37. #define DRIVER_LICENSE "GPL"
  38. static char *hid_types[] = {"Device", "Pointer", "Mouse", "Device", "Joystick",
  39. "Gamepad", "Keyboard", "Keypad", "Multi-Axis Controller"};
  40. /*
  41. * Module parameters.
  42. */
  43. static unsigned int hid_mousepoll_interval;
  44. module_param_named(mousepoll, hid_mousepoll_interval, uint, 0644);
  45. MODULE_PARM_DESC(mousepoll, "Polling interval of mice");
  46. /*
  47. * Register a new report for a device.
  48. */
  49. static struct hid_report *hid_register_report(struct hid_device *device, unsigned type, unsigned id)
  50. {
  51. struct hid_report_enum *report_enum = device->report_enum + type;
  52. struct hid_report *report;
  53. if (report_enum->report_id_hash[id])
  54. return report_enum->report_id_hash[id];
  55. if (!(report = kmalloc(sizeof(struct hid_report), GFP_KERNEL)))
  56. return NULL;
  57. memset(report, 0, sizeof(struct hid_report));
  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 = kmalloc(sizeof(struct hid_field) + usages * sizeof(struct hid_usage)
  79. + values * sizeof(unsigned), GFP_KERNEL))) return NULL;
  80. memset(field, 0, sizeof(struct hid_field) + usages * sizeof(struct hid_usage)
  81. + values * sizeof(unsigned));
  82. field->index = report->maxfield++;
  83. report->field[field->index] = field;
  84. field->usage = (struct hid_usage *)(field + 1);
  85. field->value = (unsigned *)(field->usage + usages);
  86. field->report = report;
  87. return field;
  88. }
  89. /*
  90. * Open a collection. The type/usage is pushed on the stack.
  91. */
  92. static int open_collection(struct hid_parser *parser, unsigned type)
  93. {
  94. struct hid_collection *collection;
  95. unsigned usage;
  96. usage = parser->local.usage[0];
  97. if (parser->collection_stack_ptr == HID_COLLECTION_STACK_SIZE) {
  98. dbg("collection stack overflow");
  99. return -1;
  100. }
  101. if (parser->device->maxcollection == parser->device->collection_size) {
  102. collection = kmalloc(sizeof(struct hid_collection) *
  103. parser->device->collection_size * 2, GFP_KERNEL);
  104. if (collection == NULL) {
  105. dbg("failed to reallocate collection array");
  106. return -1;
  107. }
  108. memcpy(collection, parser->device->collection,
  109. sizeof(struct hid_collection) *
  110. parser->device->collection_size);
  111. memset(collection + parser->device->collection_size, 0,
  112. sizeof(struct hid_collection) *
  113. parser->device->collection_size);
  114. kfree(parser->device->collection);
  115. parser->device->collection = collection;
  116. parser->device->collection_size *= 2;
  117. }
  118. parser->collection_stack[parser->collection_stack_ptr++] =
  119. parser->device->maxcollection;
  120. collection = parser->device->collection +
  121. parser->device->maxcollection++;
  122. collection->type = type;
  123. collection->usage = usage;
  124. collection->level = parser->collection_stack_ptr - 1;
  125. if (type == HID_COLLECTION_APPLICATION)
  126. parser->device->maxapplication++;
  127. return 0;
  128. }
  129. /*
  130. * Close a collection.
  131. */
  132. static int close_collection(struct hid_parser *parser)
  133. {
  134. if (!parser->collection_stack_ptr) {
  135. dbg("collection stack underflow");
  136. return -1;
  137. }
  138. parser->collection_stack_ptr--;
  139. return 0;
  140. }
  141. /*
  142. * Climb up the stack, search for the specified collection type
  143. * and return the usage.
  144. */
  145. static unsigned hid_lookup_collection(struct hid_parser *parser, unsigned type)
  146. {
  147. int n;
  148. for (n = parser->collection_stack_ptr - 1; n >= 0; n--)
  149. if (parser->device->collection[parser->collection_stack[n]].type == type)
  150. return parser->device->collection[parser->collection_stack[n]].usage;
  151. return 0; /* we know nothing about this usage type */
  152. }
  153. /*
  154. * Add a usage to the temporary parser table.
  155. */
  156. static int hid_add_usage(struct hid_parser *parser, unsigned usage)
  157. {
  158. if (parser->local.usage_index >= HID_MAX_USAGES) {
  159. dbg("usage index exceeded");
  160. return -1;
  161. }
  162. parser->local.usage[parser->local.usage_index] = usage;
  163. parser->local.collection_index[parser->local.usage_index] =
  164. parser->collection_stack_ptr ?
  165. parser->collection_stack[parser->collection_stack_ptr - 1] : 0;
  166. parser->local.usage_index++;
  167. return 0;
  168. }
  169. /*
  170. * Register a new field for this report.
  171. */
  172. static int hid_add_field(struct hid_parser *parser, unsigned report_type, unsigned flags)
  173. {
  174. struct hid_report *report;
  175. struct hid_field *field;
  176. int usages;
  177. unsigned offset;
  178. int i;
  179. if (!(report = hid_register_report(parser->device, report_type, parser->global.report_id))) {
  180. dbg("hid_register_report failed");
  181. return -1;
  182. }
  183. if (parser->global.logical_maximum < parser->global.logical_minimum) {
  184. dbg("logical range invalid %d %d", parser->global.logical_minimum, parser->global.logical_maximum);
  185. return -1;
  186. }
  187. offset = report->size;
  188. report->size += parser->global.report_size * parser->global.report_count;
  189. if (!parser->local.usage_index) /* Ignore padding fields */
  190. return 0;
  191. usages = max_t(int, parser->local.usage_index, parser->global.report_count);
  192. if ((field = hid_register_field(report, usages, parser->global.report_count)) == NULL)
  193. return 0;
  194. field->physical = hid_lookup_collection(parser, HID_COLLECTION_PHYSICAL);
  195. field->logical = hid_lookup_collection(parser, HID_COLLECTION_LOGICAL);
  196. field->application = hid_lookup_collection(parser, HID_COLLECTION_APPLICATION);
  197. for (i = 0; i < usages; i++) {
  198. int j = i;
  199. /* Duplicate the last usage we parsed if we have excess values */
  200. if (i >= parser->local.usage_index)
  201. j = parser->local.usage_index - 1;
  202. field->usage[i].hid = parser->local.usage[j];
  203. field->usage[i].collection_index =
  204. parser->local.collection_index[j];
  205. }
  206. field->maxusage = usages;
  207. field->flags = flags;
  208. field->report_offset = offset;
  209. field->report_type = report_type;
  210. field->report_size = parser->global.report_size;
  211. field->report_count = parser->global.report_count;
  212. field->logical_minimum = parser->global.logical_minimum;
  213. field->logical_maximum = parser->global.logical_maximum;
  214. field->physical_minimum = parser->global.physical_minimum;
  215. field->physical_maximum = parser->global.physical_maximum;
  216. field->unit_exponent = parser->global.unit_exponent;
  217. field->unit = parser->global.unit;
  218. return 0;
  219. }
  220. /*
  221. * Read data value from item.
  222. */
  223. static __inline__ __u32 item_udata(struct hid_item *item)
  224. {
  225. switch (item->size) {
  226. case 1: return item->data.u8;
  227. case 2: return item->data.u16;
  228. case 4: return item->data.u32;
  229. }
  230. return 0;
  231. }
  232. static __inline__ __s32 item_sdata(struct hid_item *item)
  233. {
  234. switch (item->size) {
  235. case 1: return item->data.s8;
  236. case 2: return item->data.s16;
  237. case 4: return item->data.s32;
  238. }
  239. return 0;
  240. }
  241. /*
  242. * Process a global item.
  243. */
  244. static int hid_parser_global(struct hid_parser *parser, struct hid_item *item)
  245. {
  246. switch (item->tag) {
  247. case HID_GLOBAL_ITEM_TAG_PUSH:
  248. if (parser->global_stack_ptr == HID_GLOBAL_STACK_SIZE) {
  249. dbg("global enviroment stack overflow");
  250. return -1;
  251. }
  252. memcpy(parser->global_stack + parser->global_stack_ptr++,
  253. &parser->global, sizeof(struct hid_global));
  254. return 0;
  255. case HID_GLOBAL_ITEM_TAG_POP:
  256. if (!parser->global_stack_ptr) {
  257. dbg("global enviroment stack underflow");
  258. return -1;
  259. }
  260. memcpy(&parser->global, parser->global_stack + --parser->global_stack_ptr,
  261. sizeof(struct hid_global));
  262. return 0;
  263. case HID_GLOBAL_ITEM_TAG_USAGE_PAGE:
  264. parser->global.usage_page = item_udata(item);
  265. return 0;
  266. case HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM:
  267. parser->global.logical_minimum = item_sdata(item);
  268. return 0;
  269. case HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM:
  270. if (parser->global.logical_minimum < 0)
  271. parser->global.logical_maximum = item_sdata(item);
  272. else
  273. parser->global.logical_maximum = item_udata(item);
  274. return 0;
  275. case HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM:
  276. parser->global.physical_minimum = item_sdata(item);
  277. return 0;
  278. case HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM:
  279. if (parser->global.physical_minimum < 0)
  280. parser->global.physical_maximum = item_sdata(item);
  281. else
  282. parser->global.physical_maximum = item_udata(item);
  283. return 0;
  284. case HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT:
  285. parser->global.unit_exponent = item_sdata(item);
  286. return 0;
  287. case HID_GLOBAL_ITEM_TAG_UNIT:
  288. parser->global.unit = item_udata(item);
  289. return 0;
  290. case HID_GLOBAL_ITEM_TAG_REPORT_SIZE:
  291. if ((parser->global.report_size = item_udata(item)) > 32) {
  292. dbg("invalid report_size %d", parser->global.report_size);
  293. return -1;
  294. }
  295. return 0;
  296. case HID_GLOBAL_ITEM_TAG_REPORT_COUNT:
  297. if ((parser->global.report_count = item_udata(item)) > HID_MAX_USAGES) {
  298. dbg("invalid report_count %d", parser->global.report_count);
  299. return -1;
  300. }
  301. return 0;
  302. case HID_GLOBAL_ITEM_TAG_REPORT_ID:
  303. if ((parser->global.report_id = item_udata(item)) == 0) {
  304. dbg("report_id 0 is invalid");
  305. return -1;
  306. }
  307. return 0;
  308. default:
  309. dbg("unknown global tag 0x%x", item->tag);
  310. return -1;
  311. }
  312. }
  313. /*
  314. * Process a local item.
  315. */
  316. static int hid_parser_local(struct hid_parser *parser, struct hid_item *item)
  317. {
  318. __u32 data;
  319. unsigned n;
  320. if (item->size == 0) {
  321. dbg("item data expected for local item");
  322. return -1;
  323. }
  324. data = item_udata(item);
  325. switch (item->tag) {
  326. case HID_LOCAL_ITEM_TAG_DELIMITER:
  327. if (data) {
  328. /*
  329. * We treat items before the first delimiter
  330. * as global to all usage sets (branch 0).
  331. * In the moment we process only these global
  332. * items and the first delimiter set.
  333. */
  334. if (parser->local.delimiter_depth != 0) {
  335. dbg("nested delimiters");
  336. return -1;
  337. }
  338. parser->local.delimiter_depth++;
  339. parser->local.delimiter_branch++;
  340. } else {
  341. if (parser->local.delimiter_depth < 1) {
  342. dbg("bogus close delimiter");
  343. return -1;
  344. }
  345. parser->local.delimiter_depth--;
  346. }
  347. return 1;
  348. case HID_LOCAL_ITEM_TAG_USAGE:
  349. if (parser->local.delimiter_branch > 1) {
  350. dbg("alternative usage ignored");
  351. return 0;
  352. }
  353. if (item->size <= 2)
  354. data = (parser->global.usage_page << 16) + data;
  355. return hid_add_usage(parser, data);
  356. case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM:
  357. if (parser->local.delimiter_branch > 1) {
  358. dbg("alternative usage ignored");
  359. return 0;
  360. }
  361. if (item->size <= 2)
  362. data = (parser->global.usage_page << 16) + data;
  363. parser->local.usage_minimum = data;
  364. return 0;
  365. case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM:
  366. if (parser->local.delimiter_branch > 1) {
  367. dbg("alternative usage ignored");
  368. return 0;
  369. }
  370. if (item->size <= 2)
  371. data = (parser->global.usage_page << 16) + data;
  372. for (n = parser->local.usage_minimum; n <= data; n++)
  373. if (hid_add_usage(parser, n)) {
  374. dbg("hid_add_usage failed\n");
  375. return -1;
  376. }
  377. return 0;
  378. default:
  379. dbg("unknown local item tag 0x%x", item->tag);
  380. return 0;
  381. }
  382. return 0;
  383. }
  384. /*
  385. * Process a main item.
  386. */
  387. static int hid_parser_main(struct hid_parser *parser, struct hid_item *item)
  388. {
  389. __u32 data;
  390. int ret;
  391. data = item_udata(item);
  392. switch (item->tag) {
  393. case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION:
  394. ret = open_collection(parser, data & 0xff);
  395. break;
  396. case HID_MAIN_ITEM_TAG_END_COLLECTION:
  397. ret = close_collection(parser);
  398. break;
  399. case HID_MAIN_ITEM_TAG_INPUT:
  400. ret = hid_add_field(parser, HID_INPUT_REPORT, data);
  401. break;
  402. case HID_MAIN_ITEM_TAG_OUTPUT:
  403. ret = hid_add_field(parser, HID_OUTPUT_REPORT, data);
  404. break;
  405. case HID_MAIN_ITEM_TAG_FEATURE:
  406. ret = hid_add_field(parser, HID_FEATURE_REPORT, data);
  407. break;
  408. default:
  409. dbg("unknown main item tag 0x%x", item->tag);
  410. ret = 0;
  411. }
  412. memset(&parser->local, 0, sizeof(parser->local)); /* Reset the local parser environment */
  413. return ret;
  414. }
  415. /*
  416. * Process a reserved item.
  417. */
  418. static int hid_parser_reserved(struct hid_parser *parser, struct hid_item *item)
  419. {
  420. dbg("reserved item type, tag 0x%x", item->tag);
  421. return 0;
  422. }
  423. /*
  424. * Free a report and all registered fields. The field->usage and
  425. * field->value table's are allocated behind the field, so we need
  426. * only to free(field) itself.
  427. */
  428. static void hid_free_report(struct hid_report *report)
  429. {
  430. unsigned n;
  431. for (n = 0; n < report->maxfield; n++)
  432. kfree(report->field[n]);
  433. kfree(report);
  434. }
  435. /*
  436. * Free a device structure, all reports, and all fields.
  437. */
  438. static void hid_free_device(struct hid_device *device)
  439. {
  440. unsigned i,j;
  441. hid_ff_exit(device);
  442. for (i = 0; i < HID_REPORT_TYPES; i++) {
  443. struct hid_report_enum *report_enum = device->report_enum + i;
  444. for (j = 0; j < 256; j++) {
  445. struct hid_report *report = report_enum->report_id_hash[j];
  446. if (report)
  447. hid_free_report(report);
  448. }
  449. }
  450. if (device->rdesc)
  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) & ((1 << 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, 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)
  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, 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], 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, 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, 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, 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, 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, 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_BUFFER_SIZE)
  861. padlen = HID_BUFFER_SIZE;
  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. case -ESHUTDOWN: /* unplug */
  892. case -EILSEQ: /* unplug timeout on uhci */
  893. unplug = 1;
  894. case -ECONNRESET: /* unlink */
  895. case -ENOENT:
  896. break;
  897. default: /* error */
  898. warn("output irq status %d received", urb->status);
  899. }
  900. spin_lock_irqsave(&hid->outlock, flags);
  901. if (unplug)
  902. hid->outtail = hid->outhead;
  903. else
  904. hid->outtail = (hid->outtail + 1) & (HID_OUTPUT_FIFO_SIZE - 1);
  905. if (hid->outhead != hid->outtail) {
  906. if (hid_submit_out(hid)) {
  907. clear_bit(HID_OUT_RUNNING, &hid->iofl);;
  908. wake_up(&hid->wait);
  909. }
  910. spin_unlock_irqrestore(&hid->outlock, flags);
  911. return;
  912. }
  913. clear_bit(HID_OUT_RUNNING, &hid->iofl);
  914. spin_unlock_irqrestore(&hid->outlock, flags);
  915. wake_up(&hid->wait);
  916. }
  917. /*
  918. * Control pipe completion handler.
  919. */
  920. static void hid_ctrl(struct urb *urb, struct pt_regs *regs)
  921. {
  922. struct hid_device *hid = urb->context;
  923. unsigned long flags;
  924. int unplug = 0;
  925. spin_lock_irqsave(&hid->ctrllock, flags);
  926. switch (urb->status) {
  927. case 0: /* success */
  928. if (hid->ctrl[hid->ctrltail].dir == USB_DIR_IN)
  929. hid_input_report(hid->ctrl[hid->ctrltail].report->type, urb, regs);
  930. case -ESHUTDOWN: /* unplug */
  931. case -EILSEQ: /* unplug timectrl on uhci */
  932. unplug = 1;
  933. case -ECONNRESET: /* unlink */
  934. case -ENOENT:
  935. case -EPIPE: /* report not available */
  936. break;
  937. default: /* error */
  938. warn("ctrl urb status %d received", urb->status);
  939. }
  940. if (unplug)
  941. hid->ctrltail = hid->ctrlhead;
  942. else
  943. hid->ctrltail = (hid->ctrltail + 1) & (HID_CONTROL_FIFO_SIZE - 1);
  944. if (hid->ctrlhead != hid->ctrltail) {
  945. if (hid_submit_ctrl(hid)) {
  946. clear_bit(HID_CTRL_RUNNING, &hid->iofl);
  947. wake_up(&hid->wait);
  948. }
  949. spin_unlock_irqrestore(&hid->ctrllock, flags);
  950. return;
  951. }
  952. clear_bit(HID_CTRL_RUNNING, &hid->iofl);
  953. spin_unlock_irqrestore(&hid->ctrllock, flags);
  954. wake_up(&hid->wait);
  955. }
  956. void hid_submit_report(struct hid_device *hid, struct hid_report *report, unsigned char dir)
  957. {
  958. int head;
  959. unsigned long flags;
  960. if ((hid->quirks & HID_QUIRK_NOGET) && dir == USB_DIR_IN)
  961. return;
  962. if (hid->urbout && dir == USB_DIR_OUT && report->type == HID_OUTPUT_REPORT) {
  963. spin_lock_irqsave(&hid->outlock, flags);
  964. if ((head = (hid->outhead + 1) & (HID_OUTPUT_FIFO_SIZE - 1)) == hid->outtail) {
  965. spin_unlock_irqrestore(&hid->outlock, flags);
  966. warn("output queue full");
  967. return;
  968. }
  969. hid->out[hid->outhead] = report;
  970. hid->outhead = head;
  971. if (!test_and_set_bit(HID_OUT_RUNNING, &hid->iofl))
  972. if (hid_submit_out(hid))
  973. clear_bit(HID_OUT_RUNNING, &hid->iofl);
  974. spin_unlock_irqrestore(&hid->outlock, flags);
  975. return;
  976. }
  977. spin_lock_irqsave(&hid->ctrllock, flags);
  978. if ((head = (hid->ctrlhead + 1) & (HID_CONTROL_FIFO_SIZE - 1)) == hid->ctrltail) {
  979. spin_unlock_irqrestore(&hid->ctrllock, flags);
  980. warn("control queue full");
  981. return;
  982. }
  983. hid->ctrl[hid->ctrlhead].report = report;
  984. hid->ctrl[hid->ctrlhead].dir = dir;
  985. hid->ctrlhead = head;
  986. if (!test_and_set_bit(HID_CTRL_RUNNING, &hid->iofl))
  987. if (hid_submit_ctrl(hid))
  988. clear_bit(HID_CTRL_RUNNING, &hid->iofl);
  989. spin_unlock_irqrestore(&hid->ctrllock, flags);
  990. }
  991. int hid_wait_io(struct hid_device *hid)
  992. {
  993. if (!wait_event_timeout(hid->wait, (!test_bit(HID_CTRL_RUNNING, &hid->iofl) &&
  994. !test_bit(HID_OUT_RUNNING, &hid->iofl)),
  995. 10*HZ)) {
  996. dbg("timeout waiting for ctrl or out queue to clear");
  997. return -1;
  998. }
  999. return 0;
  1000. }
  1001. static int hid_get_class_descriptor(struct usb_device *dev, int ifnum,
  1002. unsigned char type, void *buf, int size)
  1003. {
  1004. int result, retries = 4;
  1005. memset(buf,0,size); // Make sure we parse really received data
  1006. do {
  1007. result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  1008. USB_REQ_GET_DESCRIPTOR, USB_RECIP_INTERFACE | USB_DIR_IN,
  1009. (type << 8), ifnum, buf, size, USB_CTRL_GET_TIMEOUT);
  1010. retries--;
  1011. } while (result < size && retries);
  1012. return result;
  1013. }
  1014. int hid_open(struct hid_device *hid)
  1015. {
  1016. if (hid->open++)
  1017. return 0;
  1018. hid->urbin->dev = hid->dev;
  1019. if (usb_submit_urb(hid->urbin, GFP_KERNEL))
  1020. return -EIO;
  1021. return 0;
  1022. }
  1023. void hid_close(struct hid_device *hid)
  1024. {
  1025. if (!--hid->open)
  1026. usb_kill_urb(hid->urbin);
  1027. }
  1028. /*
  1029. * Initialize all reports
  1030. */
  1031. void hid_init_reports(struct hid_device *hid)
  1032. {
  1033. struct hid_report *report;
  1034. int err, ret;
  1035. list_for_each_entry(report, &hid->report_enum[HID_INPUT_REPORT].report_list, list) {
  1036. int size = ((report->size - 1) >> 3) + 1 + hid->report_enum[HID_INPUT_REPORT].numbered;
  1037. if (size > HID_BUFFER_SIZE) size = HID_BUFFER_SIZE;
  1038. if (size > hid->urbin->transfer_buffer_length)
  1039. hid->urbin->transfer_buffer_length = size;
  1040. hid_submit_report(hid, report, USB_DIR_IN);
  1041. }
  1042. list_for_each_entry(report, &hid->report_enum[HID_FEATURE_REPORT].report_list, list)
  1043. hid_submit_report(hid, report, USB_DIR_IN);
  1044. err = 0;
  1045. ret = hid_wait_io(hid);
  1046. while (ret) {
  1047. err |= ret;
  1048. if (test_bit(HID_CTRL_RUNNING, &hid->iofl))
  1049. usb_kill_urb(hid->urbctrl);
  1050. if (test_bit(HID_OUT_RUNNING, &hid->iofl))
  1051. usb_kill_urb(hid->urbout);
  1052. ret = hid_wait_io(hid);
  1053. }
  1054. if (err)
  1055. warn("timeout initializing reports\n");
  1056. usb_control_msg(hid->dev, usb_sndctrlpipe(hid->dev, 0),
  1057. HID_REQ_SET_IDLE, USB_TYPE_CLASS | USB_RECIP_INTERFACE, 0,
  1058. hid->ifnum, NULL, 0, USB_CTRL_SET_TIMEOUT);
  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_VENDOR_ID_KBGEAR 0x084e
  1069. #define USB_DEVICE_ID_KBGEAR_JAMSTUDIO 0x1001
  1070. #define USB_VENDOR_ID_AIPTEK 0x08ca
  1071. #define USB_DEVICE_ID_AIPTEK_01 0x0001
  1072. #define USB_DEVICE_ID_AIPTEK_10 0x0010
  1073. #define USB_DEVICE_ID_AIPTEK_20 0x0020
  1074. #define USB_DEVICE_ID_AIPTEK_21 0x0021
  1075. #define USB_DEVICE_ID_AIPTEK_22 0x0022
  1076. #define USB_DEVICE_ID_AIPTEK_23 0x0023
  1077. #define USB_DEVICE_ID_AIPTEK_24 0x0024
  1078. #define USB_VENDOR_ID_GRIFFIN 0x077d
  1079. #define USB_DEVICE_ID_POWERMATE 0x0410
  1080. #define USB_DEVICE_ID_SOUNDKNOB 0x04AA
  1081. #define USB_VENDOR_ID_ATEN 0x0557
  1082. #define USB_DEVICE_ID_ATEN_UC100KM 0x2004
  1083. #define USB_DEVICE_ID_ATEN_CS124U 0x2202
  1084. #define USB_DEVICE_ID_ATEN_2PORTKVM 0x2204
  1085. #define USB_DEVICE_ID_ATEN_4PORTKVM 0x2205
  1086. #define USB_DEVICE_ID_ATEN_4PORTKVMC 0x2208
  1087. #define USB_VENDOR_ID_TOPMAX 0x0663
  1088. #define USB_DEVICE_ID_TOPMAX_COBRAPAD 0x0103
  1089. #define USB_VENDOR_ID_HAPP 0x078b
  1090. #define USB_DEVICE_ID_UGCI_DRIVING 0x0010
  1091. #define USB_DEVICE_ID_UGCI_FLYING 0x0020
  1092. #define USB_DEVICE_ID_UGCI_FIGHTING 0x0030
  1093. #define USB_VENDOR_ID_MGE 0x0463
  1094. #define USB_DEVICE_ID_MGE_UPS 0xffff
  1095. #define USB_DEVICE_ID_MGE_UPS1 0x0001
  1096. #define USB_VENDOR_ID_ONTRAK 0x0a07
  1097. #define USB_DEVICE_ID_ONTRAK_ADU100 0x0064
  1098. #define USB_VENDOR_ID_TANGTOP 0x0d3d
  1099. #define USB_DEVICE_ID_TANGTOP_USBPS2 0x0001
  1100. #define USB_VENDOR_ID_ESSENTIAL_REALITY 0x0d7f
  1101. #define USB_DEVICE_ID_ESSENTIAL_REALITY_P5 0x0100
  1102. #define USB_VENDOR_ID_A4TECH 0x09da
  1103. #define USB_DEVICE_ID_A4TECH_WCP32PU 0x0006
  1104. #define USB_VENDOR_ID_CYPRESS 0x04b4
  1105. #define USB_DEVICE_ID_CYPRESS_MOUSE 0x0001
  1106. #define USB_DEVICE_ID_CYPRESS_HIDCOM 0x5500
  1107. #define USB_VENDOR_ID_BERKSHIRE 0x0c98
  1108. #define USB_DEVICE_ID_BERKSHIRE_PCWD 0x1140
  1109. #define USB_VENDOR_ID_ALPS 0x0433
  1110. #define USB_DEVICE_ID_IBM_GAMEPAD 0x1101
  1111. #define USB_VENDOR_ID_SAITEK 0x06a3
  1112. #define USB_DEVICE_ID_SAITEK_RUMBLEPAD 0xff17
  1113. #define USB_VENDOR_ID_NEC 0x073e
  1114. #define USB_DEVICE_ID_NEC_USB_GAME_PAD 0x0301
  1115. #define USB_VENDOR_ID_CHIC 0x05fe
  1116. #define USB_DEVICE_ID_CHIC_GAMEPAD 0x0014
  1117. #define USB_VENDOR_ID_GLAB 0x06c2
  1118. #define USB_DEVICE_ID_4_PHIDGETSERVO_30 0x0038
  1119. #define USB_DEVICE_ID_1_PHIDGETSERVO_30 0x0039
  1120. #define USB_DEVICE_ID_8_8_8_IF_KIT 0x0045
  1121. #define USB_DEVICE_ID_0_0_4_IF_KIT 0x0040
  1122. #define USB_DEVICE_ID_0_8_8_IF_KIT 0x0053
  1123. #define USB_VENDOR_ID_WISEGROUP 0x0925
  1124. #define USB_DEVICE_ID_1_PHIDGETSERVO_20 0x8101
  1125. #define USB_DEVICE_ID_4_PHIDGETSERVO_20 0x8104
  1126. #define USB_VENDOR_ID_CODEMERCS 0x07c0
  1127. #define USB_DEVICE_ID_CODEMERCS_IOW40 0x1500
  1128. #define USB_DEVICE_ID_CODEMERCS_IOW24 0x1501
  1129. #define USB_DEVICE_ID_CODEMERCS_IOW48 0x1502
  1130. #define USB_DEVICE_ID_CODEMERCS_IOW28 0x1503
  1131. #define USB_VENDOR_ID_DELORME 0x1163
  1132. #define USB_DEVICE_ID_DELORME_EARTHMATE 0x0100
  1133. #define USB_VENDOR_ID_MCC 0x09db
  1134. #define USB_DEVICE_ID_MCC_PMD1024LS 0x0076
  1135. #define USB_DEVICE_ID_MCC_PMD1208LS 0x007a
  1136. #define USB_VENDOR_ID_CHICONY 0x04f2
  1137. #define USB_DEVICE_ID_CHICONY_USBHUB_KB 0x0100
  1138. #define USB_VENDOR_ID_BTC 0x046e
  1139. #define USB_DEVICE_ID_BTC_KEYBOARD 0x5303
  1140. /*
  1141. * Alphabetically sorted blacklist by quirk type.
  1142. */
  1143. static struct hid_blacklist {
  1144. __u16 idVendor;
  1145. __u16 idProduct;
  1146. unsigned quirks;
  1147. } hid_blacklist[] = {
  1148. { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_01, HID_QUIRK_IGNORE },
  1149. { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_10, HID_QUIRK_IGNORE },
  1150. { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_20, HID_QUIRK_IGNORE },
  1151. { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_21, HID_QUIRK_IGNORE },
  1152. { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_22, HID_QUIRK_IGNORE },
  1153. { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_23, HID_QUIRK_IGNORE },
  1154. { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_24, HID_QUIRK_IGNORE },
  1155. { USB_VENDOR_ID_BERKSHIRE, USB_DEVICE_ID_BERKSHIRE_PCWD, HID_QUIRK_IGNORE },
  1156. { USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW40, HID_QUIRK_IGNORE },
  1157. { USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW24, HID_QUIRK_IGNORE },
  1158. { USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW48, HID_QUIRK_IGNORE },
  1159. { USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW28, HID_QUIRK_IGNORE },
  1160. { USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_HIDCOM, HID_QUIRK_IGNORE },
  1161. { USB_VENDOR_ID_DELORME, USB_DEVICE_ID_DELORME_EARTHMATE, HID_QUIRK_IGNORE },
  1162. { USB_VENDOR_ID_ESSENTIAL_REALITY, USB_DEVICE_ID_ESSENTIAL_REALITY_P5, HID_QUIRK_IGNORE },
  1163. { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_4_PHIDGETSERVO_30, HID_QUIRK_IGNORE },
  1164. { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_1_PHIDGETSERVO_30, HID_QUIRK_IGNORE },
  1165. { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_8_8_8_IF_KIT, HID_QUIRK_IGNORE },
  1166. { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_0_4_IF_KIT, HID_QUIRK_IGNORE },
  1167. { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_8_8_IF_KIT, HID_QUIRK_IGNORE },
  1168. { USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_POWERMATE, HID_QUIRK_IGNORE },
  1169. { USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_SOUNDKNOB, HID_QUIRK_IGNORE },
  1170. { USB_VENDOR_ID_KBGEAR, USB_DEVICE_ID_KBGEAR_JAMSTUDIO, HID_QUIRK_IGNORE },
  1171. { USB_VENDOR_ID_MCC, USB_DEVICE_ID_MCC_PMD1024LS, HID_QUIRK_IGNORE },
  1172. { USB_VENDOR_ID_MCC, USB_DEVICE_ID_MCC_PMD1208LS, HID_QUIRK_IGNORE },
  1173. { USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS, HID_QUIRK_IGNORE },
  1174. { USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS1, HID_QUIRK_IGNORE },
  1175. { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100, HID_QUIRK_IGNORE },
  1176. { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 100, HID_QUIRK_IGNORE },
  1177. { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 200, HID_QUIRK_IGNORE },
  1178. { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 300, HID_QUIRK_IGNORE },
  1179. { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 400, HID_QUIRK_IGNORE },
  1180. { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 500, HID_QUIRK_IGNORE },
  1181. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PENPARTNER, HID_QUIRK_IGNORE },
  1182. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE, HID_QUIRK_IGNORE },
  1183. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE + 1, HID_QUIRK_IGNORE },
  1184. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE + 2, HID_QUIRK_IGNORE },
  1185. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE + 3, HID_QUIRK_IGNORE },
  1186. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE + 4, HID_QUIRK_IGNORE },
  1187. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS, HID_QUIRK_IGNORE },
  1188. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS + 1, HID_QUIRK_IGNORE },
  1189. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS + 2, HID_QUIRK_IGNORE },
  1190. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS + 3, HID_QUIRK_IGNORE },
  1191. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS + 4, HID_QUIRK_IGNORE },
  1192. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL, HID_QUIRK_IGNORE },
  1193. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 1, HID_QUIRK_IGNORE },
  1194. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 2, HID_QUIRK_IGNORE },
  1195. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 3, HID_QUIRK_IGNORE },
  1196. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 4, HID_QUIRK_IGNORE },
  1197. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 5, HID_QUIRK_IGNORE },
  1198. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 1, HID_QUIRK_IGNORE },
  1199. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 2, HID_QUIRK_IGNORE },
  1200. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 3, HID_QUIRK_IGNORE },
  1201. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 4, HID_QUIRK_IGNORE },
  1202. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 5, HID_QUIRK_IGNORE },
  1203. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 7, HID_QUIRK_IGNORE },
  1204. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_VOLITO, HID_QUIRK_IGNORE },
  1205. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PTU, HID_QUIRK_IGNORE },
  1206. { USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_4_PHIDGETSERVO_20, HID_QUIRK_IGNORE },
  1207. { USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_1_PHIDGETSERVO_20, HID_QUIRK_IGNORE },
  1208. { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_UC100KM, HID_QUIRK_NOGET },
  1209. { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_CS124U, HID_QUIRK_NOGET },
  1210. { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_2PORTKVM, HID_QUIRK_NOGET },
  1211. { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_4PORTKVM, HID_QUIRK_NOGET },
  1212. { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_4PORTKVMC, HID_QUIRK_NOGET },
  1213. { USB_VENDOR_ID_BTC, USB_DEVICE_ID_BTC_KEYBOARD, HID_QUIRK_NOGET},
  1214. { USB_VENDOR_ID_CHICONY, USB_DEVICE_ID_CHICONY_USBHUB_KB, HID_QUIRK_NOGET},
  1215. { USB_VENDOR_ID_TANGTOP, USB_DEVICE_ID_TANGTOP_USBPS2, HID_QUIRK_NOGET },
  1216. { USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_WCP32PU, HID_QUIRK_2WHEEL_MOUSE_HACK_7 },
  1217. { USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_MOUSE, HID_QUIRK_2WHEEL_MOUSE_HACK_5 },
  1218. { USB_VENDOR_ID_ALPS, USB_DEVICE_ID_IBM_GAMEPAD, HID_QUIRK_BADPAD },
  1219. { USB_VENDOR_ID_CHIC, USB_DEVICE_ID_CHIC_GAMEPAD, HID_QUIRK_BADPAD },
  1220. { USB_VENDOR_ID_HAPP, USB_DEVICE_ID_UGCI_DRIVING, HID_QUIRK_BADPAD | HID_QUIRK_MULTI_INPUT },
  1221. { USB_VENDOR_ID_HAPP, USB_DEVICE_ID_UGCI_FLYING, HID_QUIRK_BADPAD | HID_QUIRK_MULTI_INPUT },
  1222. { USB_VENDOR_ID_HAPP, USB_DEVICE_ID_UGCI_FIGHTING, HID_QUIRK_BADPAD | HID_QUIRK_MULTI_INPUT },
  1223. { USB_VENDOR_ID_NEC, USB_DEVICE_ID_NEC_USB_GAME_PAD, HID_QUIRK_BADPAD },
  1224. { USB_VENDOR_ID_SAITEK, USB_DEVICE_ID_SAITEK_RUMBLEPAD, HID_QUIRK_BADPAD },
  1225. { USB_VENDOR_ID_TOPMAX, USB_DEVICE_ID_TOPMAX_COBRAPAD, HID_QUIRK_BADPAD },
  1226. { 0, 0 }
  1227. };
  1228. static int hid_alloc_buffers(struct usb_device *dev, struct hid_device *hid)
  1229. {
  1230. if (!(hid->inbuf = usb_buffer_alloc(dev, HID_BUFFER_SIZE, SLAB_ATOMIC, &hid->inbuf_dma)))
  1231. return -1;
  1232. if (!(hid->outbuf = usb_buffer_alloc(dev, HID_BUFFER_SIZE, SLAB_ATOMIC, &hid->outbuf_dma)))
  1233. return -1;
  1234. if (!(hid->cr = usb_buffer_alloc(dev, sizeof(*(hid->cr)), SLAB_ATOMIC, &hid->cr_dma)))
  1235. return -1;
  1236. if (!(hid->ctrlbuf = usb_buffer_alloc(dev, HID_BUFFER_SIZE, SLAB_ATOMIC, &hid->ctrlbuf_dma)))
  1237. return -1;
  1238. return 0;
  1239. }
  1240. static void hid_free_buffers(struct usb_device *dev, struct hid_device *hid)
  1241. {
  1242. if (hid->inbuf)
  1243. usb_buffer_free(dev, HID_BUFFER_SIZE, hid->inbuf, hid->inbuf_dma);
  1244. if (hid->outbuf)
  1245. usb_buffer_free(dev, HID_BUFFER_SIZE, hid->outbuf, hid->outbuf_dma);
  1246. if (hid->cr)
  1247. usb_buffer_free(dev, sizeof(*(hid->cr)), hid->cr, hid->cr_dma);
  1248. if (hid->ctrlbuf)
  1249. usb_buffer_free(dev, HID_BUFFER_SIZE, hid->ctrlbuf, hid->ctrlbuf_dma);
  1250. }
  1251. static struct hid_device *usb_hid_configure(struct usb_interface *intf)
  1252. {
  1253. struct usb_host_interface *interface = intf->cur_altsetting;
  1254. struct usb_device *dev = interface_to_usbdev (intf);
  1255. struct hid_descriptor *hdesc;
  1256. struct hid_device *hid;
  1257. unsigned quirks = 0, rsize = 0;
  1258. char *buf, *rdesc;
  1259. int n;
  1260. for (n = 0; hid_blacklist[n].idVendor; n++)
  1261. if ((hid_blacklist[n].idVendor == le16_to_cpu(dev->descriptor.idVendor)) &&
  1262. (hid_blacklist[n].idProduct == le16_to_cpu(dev->descriptor.idProduct)))
  1263. quirks = hid_blacklist[n].quirks;
  1264. if (quirks & HID_QUIRK_IGNORE)
  1265. return NULL;
  1266. if (usb_get_extra_descriptor(interface, HID_DT_HID, &hdesc) && ((!interface->desc.bNumEndpoints) ||
  1267. usb_get_extra_descriptor(&interface->endpoint[0], HID_DT_HID, &hdesc))) {
  1268. dbg("class descriptor not present\n");
  1269. return NULL;
  1270. }
  1271. for (n = 0; n < hdesc->bNumDescriptors; n++)
  1272. if (hdesc->desc[n].bDescriptorType == HID_DT_REPORT)
  1273. rsize = le16_to_cpu(hdesc->desc[n].wDescriptorLength);
  1274. if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
  1275. dbg("weird size of report descriptor (%u)", rsize);
  1276. return NULL;
  1277. }
  1278. if (!(rdesc = kmalloc(rsize, GFP_KERNEL))) {
  1279. dbg("couldn't allocate rdesc memory");
  1280. return NULL;
  1281. }
  1282. if ((n = hid_get_class_descriptor(dev, interface->desc.bInterfaceNumber, HID_DT_REPORT, rdesc, rsize)) < 0) {
  1283. dbg("reading report descriptor failed");
  1284. kfree(rdesc);
  1285. return NULL;
  1286. }
  1287. #ifdef DEBUG_DATA
  1288. printk(KERN_DEBUG __FILE__ ": report descriptor (size %u, read %d) = ", rsize, n);
  1289. for (n = 0; n < rsize; n++)
  1290. printk(" %02x", (unsigned char) rdesc[n]);
  1291. printk("\n");
  1292. #endif
  1293. if (!(hid = hid_parse_report(rdesc, n))) {
  1294. dbg("parsing report descriptor failed");
  1295. kfree(rdesc);
  1296. return NULL;
  1297. }
  1298. kfree(rdesc);
  1299. hid->quirks = quirks;
  1300. if (hid_alloc_buffers(dev, hid)) {
  1301. hid_free_buffers(dev, hid);
  1302. goto fail;
  1303. }
  1304. for (n = 0; n < interface->desc.bNumEndpoints; n++) {
  1305. struct usb_endpoint_descriptor *endpoint;
  1306. int pipe;
  1307. int interval;
  1308. endpoint = &interface->endpoint[n].desc;
  1309. if ((endpoint->bmAttributes & 3) != 3) /* Not an interrupt endpoint */
  1310. continue;
  1311. /* handle potential highspeed HID correctly */
  1312. interval = endpoint->bInterval;
  1313. if (dev->speed == USB_SPEED_HIGH)
  1314. interval = 1 << (interval - 1);
  1315. /* Change the polling interval of mice. */
  1316. if (hid->collection->usage == HID_GD_MOUSE && hid_mousepoll_interval > 0)
  1317. interval = hid_mousepoll_interval;
  1318. if (endpoint->bEndpointAddress & USB_DIR_IN) {
  1319. if (hid->urbin)
  1320. continue;
  1321. if (!(hid->urbin = usb_alloc_urb(0, GFP_KERNEL)))
  1322. goto fail;
  1323. pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
  1324. usb_fill_int_urb(hid->urbin, dev, pipe, hid->inbuf, 0,
  1325. hid_irq_in, hid, interval);
  1326. hid->urbin->transfer_dma = hid->inbuf_dma;
  1327. hid->urbin->transfer_flags |=(URB_NO_TRANSFER_DMA_MAP | URB_ASYNC_UNLINK);
  1328. } else {
  1329. if (hid->urbout)
  1330. continue;
  1331. if (!(hid->urbout = usb_alloc_urb(0, GFP_KERNEL)))
  1332. goto fail;
  1333. pipe = usb_sndintpipe(dev, endpoint->bEndpointAddress);
  1334. usb_fill_int_urb(hid->urbout, dev, pipe, hid->outbuf, 0,
  1335. hid_irq_out, hid, interval);
  1336. hid->urbout->transfer_dma = hid->outbuf_dma;
  1337. hid->urbout->transfer_flags |= (URB_NO_TRANSFER_DMA_MAP | URB_ASYNC_UNLINK);
  1338. }
  1339. }
  1340. if (!hid->urbin) {
  1341. err("couldn't find an input interrupt endpoint");
  1342. goto fail;
  1343. }
  1344. init_waitqueue_head(&hid->wait);
  1345. spin_lock_init(&hid->outlock);
  1346. spin_lock_init(&hid->ctrllock);
  1347. hid->version = le16_to_cpu(hdesc->bcdHID);
  1348. hid->country = hdesc->bCountryCode;
  1349. hid->dev = dev;
  1350. hid->intf = intf;
  1351. hid->ifnum = interface->desc.bInterfaceNumber;
  1352. hid->name[0] = 0;
  1353. if (!(buf = kmalloc(64, GFP_KERNEL)))
  1354. goto fail;
  1355. if (dev->manufacturer) {
  1356. strcat(hid->name, dev->manufacturer);
  1357. if (dev->product)
  1358. snprintf(hid->name, 64, "%s %s", hid->name, dev->product);
  1359. } else if (dev->product) {
  1360. snprintf(hid->name, 128, "%s", dev->product);
  1361. } else
  1362. snprintf(hid->name, 128, "%04x:%04x",
  1363. le16_to_cpu(dev->descriptor.idVendor),
  1364. le16_to_cpu(dev->descriptor.idProduct));
  1365. usb_make_path(dev, buf, 64);
  1366. snprintf(hid->phys, 64, "%s/input%d", buf,
  1367. intf->altsetting[0].desc.bInterfaceNumber);
  1368. if (usb_string(dev, dev->descriptor.iSerialNumber, hid->uniq, 64) <= 0)
  1369. hid->uniq[0] = 0;
  1370. kfree(buf);
  1371. hid->urbctrl = usb_alloc_urb(0, GFP_KERNEL);
  1372. if (!hid->urbctrl)
  1373. goto fail;
  1374. usb_fill_control_urb(hid->urbctrl, dev, 0, (void *) hid->cr,
  1375. hid->ctrlbuf, 1, hid_ctrl, hid);
  1376. hid->urbctrl->setup_dma = hid->cr_dma;
  1377. hid->urbctrl->transfer_dma = hid->ctrlbuf_dma;
  1378. hid->urbctrl->transfer_flags |= (URB_NO_TRANSFER_DMA_MAP | URB_NO_SETUP_DMA_MAP | URB_ASYNC_UNLINK);
  1379. return hid;
  1380. fail:
  1381. if (hid->urbin)
  1382. usb_free_urb(hid->urbin);
  1383. if (hid->urbout)
  1384. usb_free_urb(hid->urbout);
  1385. if (hid->urbctrl)
  1386. usb_free_urb(hid->urbctrl);
  1387. hid_free_buffers(dev, hid);
  1388. hid_free_device(hid);
  1389. return NULL;
  1390. }
  1391. static void hid_disconnect(struct usb_interface *intf)
  1392. {
  1393. struct hid_device *hid = usb_get_intfdata (intf);
  1394. if (!hid)
  1395. return;
  1396. usb_set_intfdata(intf, NULL);
  1397. usb_kill_urb(hid->urbin);
  1398. usb_kill_urb(hid->urbout);
  1399. usb_kill_urb(hid->urbctrl);
  1400. if (hid->claimed & HID_CLAIMED_INPUT)
  1401. hidinput_disconnect(hid);
  1402. if (hid->claimed & HID_CLAIMED_HIDDEV)
  1403. hiddev_disconnect(hid);
  1404. usb_free_urb(hid->urbin);
  1405. usb_free_urb(hid->urbctrl);
  1406. if (hid->urbout)
  1407. usb_free_urb(hid->urbout);
  1408. hid_free_buffers(hid->dev, hid);
  1409. hid_free_device(hid);
  1410. }
  1411. static int hid_probe(struct usb_interface *intf, const struct usb_device_id *id)
  1412. {
  1413. struct hid_device *hid;
  1414. char path[64];
  1415. int i;
  1416. char *c;
  1417. dbg("HID probe called for ifnum %d",
  1418. intf->altsetting->desc.bInterfaceNumber);
  1419. if (!(hid = usb_hid_configure(intf)))
  1420. return -EIO;
  1421. hid_init_reports(hid);
  1422. hid_dump_device(hid);
  1423. if (!hidinput_connect(hid))
  1424. hid->claimed |= HID_CLAIMED_INPUT;
  1425. if (!hiddev_connect(hid))
  1426. hid->claimed |= HID_CLAIMED_HIDDEV;
  1427. usb_set_intfdata(intf, hid);
  1428. if (!hid->claimed) {
  1429. printk ("HID device not claimed by input or hiddev\n");
  1430. hid_disconnect(intf);
  1431. return -EIO;
  1432. }
  1433. printk(KERN_INFO);
  1434. if (hid->claimed & HID_CLAIMED_INPUT)
  1435. printk("input");
  1436. if (hid->claimed == (HID_CLAIMED_INPUT | HID_CLAIMED_HIDDEV))
  1437. printk(",");
  1438. if (hid->claimed & HID_CLAIMED_HIDDEV)
  1439. printk("hiddev%d", hid->minor);
  1440. c = "Device";
  1441. for (i = 0; i < hid->maxcollection; i++) {
  1442. if (hid->collection[i].type == HID_COLLECTION_APPLICATION &&
  1443. (hid->collection[i].usage & HID_USAGE_PAGE) == HID_UP_GENDESK &&
  1444. (hid->collection[i].usage & 0xffff) < ARRAY_SIZE(hid_types)) {
  1445. c = hid_types[hid->collection[i].usage & 0xffff];
  1446. break;
  1447. }
  1448. }
  1449. usb_make_path(interface_to_usbdev(intf), path, 63);
  1450. printk(": USB HID v%x.%02x %s [%s] on %s\n",
  1451. hid->version >> 8, hid->version & 0xff, c, hid->name, path);
  1452. return 0;
  1453. }
  1454. static int hid_suspend(struct usb_interface *intf, pm_message_t message)
  1455. {
  1456. struct hid_device *hid = usb_get_intfdata (intf);
  1457. usb_kill_urb(hid->urbin);
  1458. intf->dev.power.power_state = PMSG_SUSPEND;
  1459. dev_dbg(&intf->dev, "suspend\n");
  1460. return 0;
  1461. }
  1462. static int hid_resume(struct usb_interface *intf)
  1463. {
  1464. struct hid_device *hid = usb_get_intfdata (intf);
  1465. int status;
  1466. intf->dev.power.power_state = PMSG_ON;
  1467. if (hid->open)
  1468. status = usb_submit_urb(hid->urbin, GFP_NOIO);
  1469. else
  1470. status = 0;
  1471. dev_dbg(&intf->dev, "resume status %d\n", status);
  1472. return status;
  1473. }
  1474. static struct usb_device_id hid_usb_ids [] = {
  1475. { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
  1476. .bInterfaceClass = USB_INTERFACE_CLASS_HID },
  1477. { } /* Terminating entry */
  1478. };
  1479. MODULE_DEVICE_TABLE (usb, hid_usb_ids);
  1480. static struct usb_driver hid_driver = {
  1481. .owner = THIS_MODULE,
  1482. .name = "usbhid",
  1483. .probe = hid_probe,
  1484. .disconnect = hid_disconnect,
  1485. .suspend = hid_suspend,
  1486. .resume = hid_resume,
  1487. .id_table = hid_usb_ids,
  1488. };
  1489. static int __init hid_init(void)
  1490. {
  1491. int retval;
  1492. retval = hiddev_init();
  1493. if (retval)
  1494. goto hiddev_init_fail;
  1495. retval = usb_register(&hid_driver);
  1496. if (retval)
  1497. goto usb_register_fail;
  1498. info(DRIVER_VERSION ":" DRIVER_DESC);
  1499. return 0;
  1500. usb_register_fail:
  1501. hiddev_exit();
  1502. hiddev_init_fail:
  1503. return retval;
  1504. }
  1505. static void __exit hid_exit(void)
  1506. {
  1507. usb_deregister(&hid_driver);
  1508. hiddev_exit();
  1509. }
  1510. module_init(hid_init);
  1511. module_exit(hid_exit);
  1512. MODULE_AUTHOR(DRIVER_AUTHOR);
  1513. MODULE_DESCRIPTION(DRIVER_DESC);
  1514. MODULE_LICENSE(DRIVER_LICENSE);