hid-core.c 64 KB

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