hid-core.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871
  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. kfree(device->rdesc);
  451. kfree(device);
  452. }
  453. /*
  454. * Fetch a report description item from the data stream. We support long
  455. * items, though they are not used yet.
  456. */
  457. static u8 *fetch_item(__u8 *start, __u8 *end, struct hid_item *item)
  458. {
  459. u8 b;
  460. if ((end - start) <= 0)
  461. return NULL;
  462. b = *start++;
  463. item->type = (b >> 2) & 3;
  464. item->tag = (b >> 4) & 15;
  465. if (item->tag == HID_ITEM_TAG_LONG) {
  466. item->format = HID_ITEM_FORMAT_LONG;
  467. if ((end - start) < 2)
  468. return NULL;
  469. item->size = *start++;
  470. item->tag = *start++;
  471. if ((end - start) < item->size)
  472. return NULL;
  473. item->data.longdata = start;
  474. start += item->size;
  475. return start;
  476. }
  477. item->format = HID_ITEM_FORMAT_SHORT;
  478. item->size = b & 3;
  479. switch (item->size) {
  480. case 0:
  481. return start;
  482. case 1:
  483. if ((end - start) < 1)
  484. return NULL;
  485. item->data.u8 = *start++;
  486. return start;
  487. case 2:
  488. if ((end - start) < 2)
  489. return NULL;
  490. item->data.u16 = le16_to_cpu(get_unaligned((__le16*)start));
  491. start = (__u8 *)((__le16 *)start + 1);
  492. return start;
  493. case 3:
  494. item->size++;
  495. if ((end - start) < 4)
  496. return NULL;
  497. item->data.u32 = le32_to_cpu(get_unaligned((__le32*)start));
  498. start = (__u8 *)((__le32 *)start + 1);
  499. return start;
  500. }
  501. return NULL;
  502. }
  503. /*
  504. * Parse a report description into a hid_device structure. Reports are
  505. * enumerated, fields are attached to these reports.
  506. */
  507. static struct hid_device *hid_parse_report(__u8 *start, unsigned size)
  508. {
  509. struct hid_device *device;
  510. struct hid_parser *parser;
  511. struct hid_item item;
  512. __u8 *end;
  513. unsigned i;
  514. static int (*dispatch_type[])(struct hid_parser *parser,
  515. struct hid_item *item) = {
  516. hid_parser_main,
  517. hid_parser_global,
  518. hid_parser_local,
  519. hid_parser_reserved
  520. };
  521. if (!(device = kmalloc(sizeof(struct hid_device), GFP_KERNEL)))
  522. return NULL;
  523. memset(device, 0, sizeof(struct hid_device));
  524. if (!(device->collection = kmalloc(sizeof(struct hid_collection) *
  525. HID_DEFAULT_NUM_COLLECTIONS, GFP_KERNEL))) {
  526. kfree(device);
  527. return NULL;
  528. }
  529. memset(device->collection, 0, sizeof(struct hid_collection) *
  530. HID_DEFAULT_NUM_COLLECTIONS);
  531. device->collection_size = HID_DEFAULT_NUM_COLLECTIONS;
  532. for (i = 0; i < HID_REPORT_TYPES; i++)
  533. INIT_LIST_HEAD(&device->report_enum[i].report_list);
  534. if (!(device->rdesc = (__u8 *)kmalloc(size, GFP_KERNEL))) {
  535. kfree(device->collection);
  536. kfree(device);
  537. return NULL;
  538. }
  539. memcpy(device->rdesc, start, size);
  540. device->rsize = size;
  541. if (!(parser = kmalloc(sizeof(struct hid_parser), GFP_KERNEL))) {
  542. kfree(device->rdesc);
  543. kfree(device->collection);
  544. kfree(device);
  545. return NULL;
  546. }
  547. memset(parser, 0, sizeof(struct hid_parser));
  548. parser->device = device;
  549. end = start + size;
  550. while ((start = fetch_item(start, end, &item)) != NULL) {
  551. if (item.format != HID_ITEM_FORMAT_SHORT) {
  552. dbg("unexpected long global item");
  553. kfree(device->collection);
  554. hid_free_device(device);
  555. kfree(parser);
  556. return NULL;
  557. }
  558. if (dispatch_type[item.type](parser, &item)) {
  559. dbg("item %u %u %u %u parsing failed\n",
  560. item.format, (unsigned)item.size, (unsigned)item.type, (unsigned)item.tag);
  561. kfree(device->collection);
  562. hid_free_device(device);
  563. kfree(parser);
  564. return NULL;
  565. }
  566. if (start == end) {
  567. if (parser->collection_stack_ptr) {
  568. dbg("unbalanced collection at end of report description");
  569. kfree(device->collection);
  570. hid_free_device(device);
  571. kfree(parser);
  572. return NULL;
  573. }
  574. if (parser->local.delimiter_depth) {
  575. dbg("unbalanced delimiter at end of report description");
  576. kfree(device->collection);
  577. hid_free_device(device);
  578. kfree(parser);
  579. return NULL;
  580. }
  581. kfree(parser);
  582. return device;
  583. }
  584. }
  585. dbg("item fetching failed at offset %d\n", (int)(end - start));
  586. kfree(device->collection);
  587. hid_free_device(device);
  588. kfree(parser);
  589. return NULL;
  590. }
  591. /*
  592. * Convert a signed n-bit integer to signed 32-bit integer. Common
  593. * cases are done through the compiler, the screwed things has to be
  594. * done by hand.
  595. */
  596. static __inline__ __s32 snto32(__u32 value, unsigned n)
  597. {
  598. switch (n) {
  599. case 8: return ((__s8)value);
  600. case 16: return ((__s16)value);
  601. case 32: return ((__s32)value);
  602. }
  603. return value & (1 << (n - 1)) ? value | (-1 << n) : value;
  604. }
  605. /*
  606. * Convert a signed 32-bit integer to a signed n-bit integer.
  607. */
  608. static __inline__ __u32 s32ton(__s32 value, unsigned n)
  609. {
  610. __s32 a = value >> (n - 1);
  611. if (a && a != -1)
  612. return value < 0 ? 1 << (n - 1) : (1 << (n - 1)) - 1;
  613. return value & ((1 << n) - 1);
  614. }
  615. /*
  616. * Extract/implement a data field from/to a report.
  617. */
  618. static __inline__ __u32 extract(__u8 *report, unsigned offset, unsigned n)
  619. {
  620. report += (offset >> 5) << 2; offset &= 31;
  621. return (le64_to_cpu(get_unaligned((__le64*)report)) >> offset) & ((1 << n) - 1);
  622. }
  623. static __inline__ void implement(__u8 *report, unsigned offset, unsigned n, __u32 value)
  624. {
  625. report += (offset >> 5) << 2; offset &= 31;
  626. put_unaligned((get_unaligned((__le64*)report)
  627. & cpu_to_le64(~((((__u64) 1 << n) - 1) << offset)))
  628. | cpu_to_le64((__u64)value << offset), (__le64*)report);
  629. }
  630. /*
  631. * Search an array for a value.
  632. */
  633. static __inline__ int search(__s32 *array, __s32 value, unsigned n)
  634. {
  635. while (n--) {
  636. if (*array++ == value)
  637. return 0;
  638. }
  639. return -1;
  640. }
  641. static void hid_process_event(struct hid_device *hid, struct hid_field *field, struct hid_usage *usage, __s32 value, struct pt_regs *regs)
  642. {
  643. hid_dump_input(usage, value);
  644. if (hid->claimed & HID_CLAIMED_INPUT)
  645. hidinput_hid_event(hid, field, usage, value, regs);
  646. if (hid->claimed & HID_CLAIMED_HIDDEV)
  647. hiddev_hid_event(hid, field, usage, value, regs);
  648. }
  649. /*
  650. * Analyse a received field, and fetch the data from it. The field
  651. * content is stored for next report processing (we do differential
  652. * reporting to the layer).
  653. */
  654. static void hid_input_field(struct hid_device *hid, struct hid_field *field, __u8 *data, struct pt_regs *regs)
  655. {
  656. unsigned n;
  657. unsigned count = field->report_count;
  658. unsigned offset = field->report_offset;
  659. unsigned size = field->report_size;
  660. __s32 min = field->logical_minimum;
  661. __s32 max = field->logical_maximum;
  662. __s32 *value;
  663. if (!(value = kmalloc(sizeof(__s32) * count, GFP_ATOMIC)))
  664. return;
  665. for (n = 0; n < count; n++) {
  666. value[n] = min < 0 ? snto32(extract(data, offset + n * size, size), size) :
  667. extract(data, offset + n * size, size);
  668. if (!(field->flags & HID_MAIN_ITEM_VARIABLE) /* Ignore report if ErrorRollOver */
  669. && value[n] >= min && value[n] <= max
  670. && field->usage[value[n] - min].hid == HID_UP_KEYBOARD + 1)
  671. goto exit;
  672. }
  673. for (n = 0; n < count; n++) {
  674. if (HID_MAIN_ITEM_VARIABLE & field->flags) {
  675. hid_process_event(hid, field, &field->usage[n], value[n], regs);
  676. continue;
  677. }
  678. if (field->value[n] >= min && field->value[n] <= max
  679. && field->usage[field->value[n] - min].hid
  680. && search(value, field->value[n], count))
  681. hid_process_event(hid, field, &field->usage[field->value[n] - min], 0, regs);
  682. if (value[n] >= min && value[n] <= max
  683. && field->usage[value[n] - min].hid
  684. && search(field->value, value[n], count))
  685. hid_process_event(hid, field, &field->usage[value[n] - min], 1, regs);
  686. }
  687. memcpy(field->value, value, count * sizeof(__s32));
  688. exit:
  689. kfree(value);
  690. }
  691. static int hid_input_report(int type, struct urb *urb, struct pt_regs *regs)
  692. {
  693. struct hid_device *hid = urb->context;
  694. struct hid_report_enum *report_enum = hid->report_enum + type;
  695. u8 *data = urb->transfer_buffer;
  696. int len = urb->actual_length;
  697. struct hid_report *report;
  698. int n, size;
  699. if (!len) {
  700. dbg("empty report");
  701. return -1;
  702. }
  703. #ifdef DEBUG_DATA
  704. printk(KERN_DEBUG __FILE__ ": report (size %u) (%snumbered)\n", len, report_enum->numbered ? "" : "un");
  705. #endif
  706. n = 0; /* Normally report number is 0 */
  707. if (report_enum->numbered) { /* Device uses numbered reports, data[0] is report number */
  708. n = *data++;
  709. len--;
  710. }
  711. #ifdef DEBUG_DATA
  712. {
  713. int i;
  714. printk(KERN_DEBUG __FILE__ ": report %d (size %u) = ", n, len);
  715. for (i = 0; i < len; i++)
  716. printk(" %02x", data[i]);
  717. printk("\n");
  718. }
  719. #endif
  720. if (!(report = report_enum->report_id_hash[n])) {
  721. dbg("undefined report_id %d received", n);
  722. return -1;
  723. }
  724. size = ((report->size - 1) >> 3) + 1;
  725. if (len < size)
  726. dbg("report %d is too short, (%d < %d)", report->id, len, size);
  727. if (hid->claimed & HID_CLAIMED_HIDDEV)
  728. hiddev_report_event(hid, report);
  729. for (n = 0; n < report->maxfield; n++)
  730. hid_input_field(hid, report->field[n], data, regs);
  731. if (hid->claimed & HID_CLAIMED_INPUT)
  732. hidinput_report_event(hid, report);
  733. return 0;
  734. }
  735. /*
  736. * Input interrupt completion handler.
  737. */
  738. static void hid_irq_in(struct urb *urb, struct pt_regs *regs)
  739. {
  740. struct hid_device *hid = urb->context;
  741. int status;
  742. switch (urb->status) {
  743. case 0: /* success */
  744. hid_input_report(HID_INPUT_REPORT, urb, regs);
  745. break;
  746. case -ECONNRESET: /* unlink */
  747. case -ENOENT:
  748. case -EPERM:
  749. case -ESHUTDOWN: /* unplug */
  750. case -EILSEQ: /* unplug timeout on uhci */
  751. return;
  752. case -ETIMEDOUT: /* NAK */
  753. break;
  754. default: /* error */
  755. warn("input irq status %d received", urb->status);
  756. }
  757. status = usb_submit_urb(urb, SLAB_ATOMIC);
  758. if (status)
  759. err("can't resubmit intr, %s-%s/input%d, status %d",
  760. hid->dev->bus->bus_name, hid->dev->devpath,
  761. hid->ifnum, status);
  762. }
  763. /*
  764. * Output the field into the report.
  765. */
  766. static void hid_output_field(struct hid_field *field, __u8 *data)
  767. {
  768. unsigned count = field->report_count;
  769. unsigned offset = field->report_offset;
  770. unsigned size = field->report_size;
  771. unsigned n;
  772. for (n = 0; n < count; n++) {
  773. if (field->logical_minimum < 0) /* signed values */
  774. implement(data, offset + n * size, size, s32ton(field->value[n], size));
  775. else /* unsigned values */
  776. implement(data, offset + n * size, size, field->value[n]);
  777. }
  778. }
  779. /*
  780. * Create a report.
  781. */
  782. static void hid_output_report(struct hid_report *report, __u8 *data)
  783. {
  784. unsigned n;
  785. if (report->id > 0)
  786. *data++ = report->id;
  787. for (n = 0; n < report->maxfield; n++)
  788. hid_output_field(report->field[n], data);
  789. }
  790. /*
  791. * Set a field value. The report this field belongs to has to be
  792. * created and transferred to the device, to set this value in the
  793. * device.
  794. */
  795. int hid_set_field(struct hid_field *field, unsigned offset, __s32 value)
  796. {
  797. unsigned size = field->report_size;
  798. hid_dump_input(field->usage + offset, value);
  799. if (offset >= field->report_count) {
  800. dbg("offset (%d) exceeds report_count (%d)", offset, field->report_count);
  801. hid_dump_field(field, 8);
  802. return -1;
  803. }
  804. if (field->logical_minimum < 0) {
  805. if (value != snto32(s32ton(value, size), size)) {
  806. dbg("value %d is out of range", value);
  807. return -1;
  808. }
  809. }
  810. field->value[offset] = value;
  811. return 0;
  812. }
  813. /*
  814. * Find a report field with a specified HID usage.
  815. */
  816. struct hid_field *hid_find_field_by_usage(struct hid_device *hid, __u32 wanted_usage, int type)
  817. {
  818. struct hid_report *report;
  819. int i;
  820. list_for_each_entry(report, &hid->report_enum[type].report_list, list)
  821. for (i = 0; i < report->maxfield; i++)
  822. if (report->field[i]->logical == wanted_usage)
  823. return report->field[i];
  824. return NULL;
  825. }
  826. static int hid_submit_out(struct hid_device *hid)
  827. {
  828. struct hid_report *report;
  829. report = hid->out[hid->outtail];
  830. hid_output_report(report, hid->outbuf);
  831. hid->urbout->transfer_buffer_length = ((report->size - 1) >> 3) + 1 + (report->id > 0);
  832. hid->urbout->dev = hid->dev;
  833. dbg("submitting out urb");
  834. if (usb_submit_urb(hid->urbout, GFP_ATOMIC)) {
  835. err("usb_submit_urb(out) failed");
  836. return -1;
  837. }
  838. return 0;
  839. }
  840. static int hid_submit_ctrl(struct hid_device *hid)
  841. {
  842. struct hid_report *report;
  843. unsigned char dir;
  844. int len;
  845. report = hid->ctrl[hid->ctrltail].report;
  846. dir = hid->ctrl[hid->ctrltail].dir;
  847. len = ((report->size - 1) >> 3) + 1 + (report->id > 0);
  848. if (dir == USB_DIR_OUT) {
  849. hid_output_report(report, hid->ctrlbuf);
  850. hid->urbctrl->pipe = usb_sndctrlpipe(hid->dev, 0);
  851. hid->urbctrl->transfer_buffer_length = len;
  852. } else {
  853. int maxpacket, padlen;
  854. hid->urbctrl->pipe = usb_rcvctrlpipe(hid->dev, 0);
  855. maxpacket = usb_maxpacket(hid->dev, hid->urbctrl->pipe, 0);
  856. if (maxpacket > 0) {
  857. padlen = (len + maxpacket - 1) / maxpacket;
  858. padlen *= maxpacket;
  859. if (padlen > HID_BUFFER_SIZE)
  860. padlen = HID_BUFFER_SIZE;
  861. } else
  862. padlen = 0;
  863. hid->urbctrl->transfer_buffer_length = padlen;
  864. }
  865. hid->urbctrl->dev = hid->dev;
  866. hid->cr->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE | dir;
  867. hid->cr->bRequest = (dir == USB_DIR_OUT) ? HID_REQ_SET_REPORT : HID_REQ_GET_REPORT;
  868. hid->cr->wValue = cpu_to_le16(((report->type + 1) << 8) | report->id);
  869. hid->cr->wIndex = cpu_to_le16(hid->ifnum);
  870. hid->cr->wLength = cpu_to_le16(len);
  871. dbg("submitting ctrl urb: %s wValue=0x%04x wIndex=0x%04x wLength=%u",
  872. hid->cr->bRequest == HID_REQ_SET_REPORT ? "Set_Report" : "Get_Report",
  873. hid->cr->wValue, hid->cr->wIndex, hid->cr->wLength);
  874. if (usb_submit_urb(hid->urbctrl, GFP_ATOMIC)) {
  875. err("usb_submit_urb(ctrl) failed");
  876. return -1;
  877. }
  878. return 0;
  879. }
  880. /*
  881. * Output interrupt completion handler.
  882. */
  883. static void hid_irq_out(struct urb *urb, struct pt_regs *regs)
  884. {
  885. struct hid_device *hid = urb->context;
  886. unsigned long flags;
  887. int unplug = 0;
  888. switch (urb->status) {
  889. case 0: /* success */
  890. case -ESHUTDOWN: /* unplug */
  891. case -EILSEQ: /* unplug timeout on uhci */
  892. unplug = 1;
  893. case -ECONNRESET: /* unlink */
  894. case -ENOENT:
  895. break;
  896. default: /* error */
  897. warn("output irq status %d received", urb->status);
  898. }
  899. spin_lock_irqsave(&hid->outlock, flags);
  900. if (unplug)
  901. hid->outtail = hid->outhead;
  902. else
  903. hid->outtail = (hid->outtail + 1) & (HID_OUTPUT_FIFO_SIZE - 1);
  904. if (hid->outhead != hid->outtail) {
  905. if (hid_submit_out(hid)) {
  906. clear_bit(HID_OUT_RUNNING, &hid->iofl);;
  907. wake_up(&hid->wait);
  908. }
  909. spin_unlock_irqrestore(&hid->outlock, flags);
  910. return;
  911. }
  912. clear_bit(HID_OUT_RUNNING, &hid->iofl);
  913. spin_unlock_irqrestore(&hid->outlock, flags);
  914. wake_up(&hid->wait);
  915. }
  916. /*
  917. * Control pipe completion handler.
  918. */
  919. static void hid_ctrl(struct urb *urb, struct pt_regs *regs)
  920. {
  921. struct hid_device *hid = urb->context;
  922. unsigned long flags;
  923. int unplug = 0;
  924. spin_lock_irqsave(&hid->ctrllock, flags);
  925. switch (urb->status) {
  926. case 0: /* success */
  927. if (hid->ctrl[hid->ctrltail].dir == USB_DIR_IN)
  928. hid_input_report(hid->ctrl[hid->ctrltail].report->type, urb, regs);
  929. case -ESHUTDOWN: /* unplug */
  930. case -EILSEQ: /* unplug timectrl on uhci */
  931. unplug = 1;
  932. case -ECONNRESET: /* unlink */
  933. case -ENOENT:
  934. case -EPIPE: /* report not available */
  935. break;
  936. default: /* error */
  937. warn("ctrl urb status %d received", urb->status);
  938. }
  939. if (unplug)
  940. hid->ctrltail = hid->ctrlhead;
  941. else
  942. hid->ctrltail = (hid->ctrltail + 1) & (HID_CONTROL_FIFO_SIZE - 1);
  943. if (hid->ctrlhead != hid->ctrltail) {
  944. if (hid_submit_ctrl(hid)) {
  945. clear_bit(HID_CTRL_RUNNING, &hid->iofl);
  946. wake_up(&hid->wait);
  947. }
  948. spin_unlock_irqrestore(&hid->ctrllock, flags);
  949. return;
  950. }
  951. clear_bit(HID_CTRL_RUNNING, &hid->iofl);
  952. spin_unlock_irqrestore(&hid->ctrllock, flags);
  953. wake_up(&hid->wait);
  954. }
  955. void hid_submit_report(struct hid_device *hid, struct hid_report *report, unsigned char dir)
  956. {
  957. int head;
  958. unsigned long flags;
  959. if ((hid->quirks & HID_QUIRK_NOGET) && dir == USB_DIR_IN)
  960. return;
  961. if (hid->urbout && dir == USB_DIR_OUT && report->type == HID_OUTPUT_REPORT) {
  962. spin_lock_irqsave(&hid->outlock, flags);
  963. if ((head = (hid->outhead + 1) & (HID_OUTPUT_FIFO_SIZE - 1)) == hid->outtail) {
  964. spin_unlock_irqrestore(&hid->outlock, flags);
  965. warn("output queue full");
  966. return;
  967. }
  968. hid->out[hid->outhead] = report;
  969. hid->outhead = head;
  970. if (!test_and_set_bit(HID_OUT_RUNNING, &hid->iofl))
  971. if (hid_submit_out(hid))
  972. clear_bit(HID_OUT_RUNNING, &hid->iofl);
  973. spin_unlock_irqrestore(&hid->outlock, flags);
  974. return;
  975. }
  976. spin_lock_irqsave(&hid->ctrllock, flags);
  977. if ((head = (hid->ctrlhead + 1) & (HID_CONTROL_FIFO_SIZE - 1)) == hid->ctrltail) {
  978. spin_unlock_irqrestore(&hid->ctrllock, flags);
  979. warn("control queue full");
  980. return;
  981. }
  982. hid->ctrl[hid->ctrlhead].report = report;
  983. hid->ctrl[hid->ctrlhead].dir = dir;
  984. hid->ctrlhead = head;
  985. if (!test_and_set_bit(HID_CTRL_RUNNING, &hid->iofl))
  986. if (hid_submit_ctrl(hid))
  987. clear_bit(HID_CTRL_RUNNING, &hid->iofl);
  988. spin_unlock_irqrestore(&hid->ctrllock, flags);
  989. }
  990. int hid_wait_io(struct hid_device *hid)
  991. {
  992. if (!wait_event_timeout(hid->wait, (!test_bit(HID_CTRL_RUNNING, &hid->iofl) &&
  993. !test_bit(HID_OUT_RUNNING, &hid->iofl)),
  994. 10*HZ)) {
  995. dbg("timeout waiting for ctrl or out queue to clear");
  996. return -1;
  997. }
  998. return 0;
  999. }
  1000. static int hid_get_class_descriptor(struct usb_device *dev, int ifnum,
  1001. unsigned char type, void *buf, int size)
  1002. {
  1003. int result, retries = 4;
  1004. memset(buf,0,size); // Make sure we parse really received data
  1005. do {
  1006. result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  1007. USB_REQ_GET_DESCRIPTOR, USB_RECIP_INTERFACE | USB_DIR_IN,
  1008. (type << 8), ifnum, buf, size, USB_CTRL_GET_TIMEOUT);
  1009. retries--;
  1010. } while (result < size && retries);
  1011. return result;
  1012. }
  1013. int hid_open(struct hid_device *hid)
  1014. {
  1015. if (hid->open++)
  1016. return 0;
  1017. hid->urbin->dev = hid->dev;
  1018. if (usb_submit_urb(hid->urbin, GFP_KERNEL))
  1019. return -EIO;
  1020. return 0;
  1021. }
  1022. void hid_close(struct hid_device *hid)
  1023. {
  1024. if (!--hid->open)
  1025. usb_kill_urb(hid->urbin);
  1026. }
  1027. /*
  1028. * Initialize all reports
  1029. */
  1030. void hid_init_reports(struct hid_device *hid)
  1031. {
  1032. struct hid_report *report;
  1033. int err, ret;
  1034. list_for_each_entry(report, &hid->report_enum[HID_INPUT_REPORT].report_list, list) {
  1035. int size = ((report->size - 1) >> 3) + 1 + hid->report_enum[HID_INPUT_REPORT].numbered;
  1036. if (size > HID_BUFFER_SIZE) size = HID_BUFFER_SIZE;
  1037. if (size > hid->urbin->transfer_buffer_length)
  1038. hid->urbin->transfer_buffer_length = size;
  1039. hid_submit_report(hid, report, USB_DIR_IN);
  1040. }
  1041. list_for_each_entry(report, &hid->report_enum[HID_FEATURE_REPORT].report_list, list)
  1042. hid_submit_report(hid, report, USB_DIR_IN);
  1043. err = 0;
  1044. ret = hid_wait_io(hid);
  1045. while (ret) {
  1046. err |= ret;
  1047. if (test_bit(HID_CTRL_RUNNING, &hid->iofl))
  1048. usb_kill_urb(hid->urbctrl);
  1049. if (test_bit(HID_OUT_RUNNING, &hid->iofl))
  1050. usb_kill_urb(hid->urbout);
  1051. ret = hid_wait_io(hid);
  1052. }
  1053. if (err)
  1054. warn("timeout initializing reports\n");
  1055. usb_control_msg(hid->dev, usb_sndctrlpipe(hid->dev, 0),
  1056. HID_REQ_SET_IDLE, USB_TYPE_CLASS | USB_RECIP_INTERFACE, 0,
  1057. hid->ifnum, NULL, 0, USB_CTRL_SET_TIMEOUT);
  1058. }
  1059. #define USB_VENDOR_ID_WACOM 0x056a
  1060. #define USB_DEVICE_ID_WACOM_PENPARTNER 0x0000
  1061. #define USB_DEVICE_ID_WACOM_GRAPHIRE 0x0010
  1062. #define USB_DEVICE_ID_WACOM_INTUOS 0x0020
  1063. #define USB_DEVICE_ID_WACOM_PL 0x0030
  1064. #define USB_DEVICE_ID_WACOM_INTUOS2 0x0040
  1065. #define USB_DEVICE_ID_WACOM_VOLITO 0x0060
  1066. #define USB_DEVICE_ID_WACOM_PTU 0x0003
  1067. #define USB_DEVICE_ID_WACOM_INTUOS3 0x00B0
  1068. #define USB_DEVICE_ID_WACOM_CINTIQ 0x003F
  1069. #define USB_VENDOR_ID_KBGEAR 0x084e
  1070. #define USB_DEVICE_ID_KBGEAR_JAMSTUDIO 0x1001
  1071. #define USB_VENDOR_ID_AIPTEK 0x08ca
  1072. #define USB_DEVICE_ID_AIPTEK_01 0x0001
  1073. #define USB_DEVICE_ID_AIPTEK_10 0x0010
  1074. #define USB_DEVICE_ID_AIPTEK_20 0x0020
  1075. #define USB_DEVICE_ID_AIPTEK_21 0x0021
  1076. #define USB_DEVICE_ID_AIPTEK_22 0x0022
  1077. #define USB_DEVICE_ID_AIPTEK_23 0x0023
  1078. #define USB_DEVICE_ID_AIPTEK_24 0x0024
  1079. #define USB_VENDOR_ID_GRIFFIN 0x077d
  1080. #define USB_DEVICE_ID_POWERMATE 0x0410
  1081. #define USB_DEVICE_ID_SOUNDKNOB 0x04AA
  1082. #define USB_VENDOR_ID_ATEN 0x0557
  1083. #define USB_DEVICE_ID_ATEN_UC100KM 0x2004
  1084. #define USB_DEVICE_ID_ATEN_CS124U 0x2202
  1085. #define USB_DEVICE_ID_ATEN_2PORTKVM 0x2204
  1086. #define USB_DEVICE_ID_ATEN_4PORTKVM 0x2205
  1087. #define USB_DEVICE_ID_ATEN_4PORTKVMC 0x2208
  1088. #define USB_VENDOR_ID_TOPMAX 0x0663
  1089. #define USB_DEVICE_ID_TOPMAX_COBRAPAD 0x0103
  1090. #define USB_VENDOR_ID_HAPP 0x078b
  1091. #define USB_DEVICE_ID_UGCI_DRIVING 0x0010
  1092. #define USB_DEVICE_ID_UGCI_FLYING 0x0020
  1093. #define USB_DEVICE_ID_UGCI_FIGHTING 0x0030
  1094. #define USB_VENDOR_ID_MGE 0x0463
  1095. #define USB_DEVICE_ID_MGE_UPS 0xffff
  1096. #define USB_DEVICE_ID_MGE_UPS1 0x0001
  1097. #define USB_VENDOR_ID_ONTRAK 0x0a07
  1098. #define USB_DEVICE_ID_ONTRAK_ADU100 0x0064
  1099. #define USB_VENDOR_ID_TANGTOP 0x0d3d
  1100. #define USB_DEVICE_ID_TANGTOP_USBPS2 0x0001
  1101. #define USB_VENDOR_ID_ESSENTIAL_REALITY 0x0d7f
  1102. #define USB_DEVICE_ID_ESSENTIAL_REALITY_P5 0x0100
  1103. #define USB_VENDOR_ID_A4TECH 0x09da
  1104. #define USB_DEVICE_ID_A4TECH_WCP32PU 0x0006
  1105. #define USB_VENDOR_ID_CYPRESS 0x04b4
  1106. #define USB_DEVICE_ID_CYPRESS_MOUSE 0x0001
  1107. #define USB_DEVICE_ID_CYPRESS_HIDCOM 0x5500
  1108. #define USB_VENDOR_ID_BERKSHIRE 0x0c98
  1109. #define USB_DEVICE_ID_BERKSHIRE_PCWD 0x1140
  1110. #define USB_VENDOR_ID_ALPS 0x0433
  1111. #define USB_DEVICE_ID_IBM_GAMEPAD 0x1101
  1112. #define USB_VENDOR_ID_SAITEK 0x06a3
  1113. #define USB_DEVICE_ID_SAITEK_RUMBLEPAD 0xff17
  1114. #define USB_VENDOR_ID_NEC 0x073e
  1115. #define USB_DEVICE_ID_NEC_USB_GAME_PAD 0x0301
  1116. #define USB_VENDOR_ID_CHIC 0x05fe
  1117. #define USB_DEVICE_ID_CHIC_GAMEPAD 0x0014
  1118. #define USB_VENDOR_ID_GLAB 0x06c2
  1119. #define USB_DEVICE_ID_4_PHIDGETSERVO_30 0x0038
  1120. #define USB_DEVICE_ID_1_PHIDGETSERVO_30 0x0039
  1121. #define USB_DEVICE_ID_8_8_8_IF_KIT 0x0045
  1122. #define USB_DEVICE_ID_0_0_4_IF_KIT 0x0040
  1123. #define USB_DEVICE_ID_0_8_8_IF_KIT 0x0053
  1124. #define USB_VENDOR_ID_WISEGROUP 0x0925
  1125. #define USB_DEVICE_ID_1_PHIDGETSERVO_20 0x8101
  1126. #define USB_DEVICE_ID_4_PHIDGETSERVO_20 0x8104
  1127. #define USB_VENDOR_ID_CODEMERCS 0x07c0
  1128. #define USB_DEVICE_ID_CODEMERCS_IOW40 0x1500
  1129. #define USB_DEVICE_ID_CODEMERCS_IOW24 0x1501
  1130. #define USB_DEVICE_ID_CODEMERCS_IOW48 0x1502
  1131. #define USB_DEVICE_ID_CODEMERCS_IOW28 0x1503
  1132. #define USB_VENDOR_ID_DELORME 0x1163
  1133. #define USB_DEVICE_ID_DELORME_EARTHMATE 0x0100
  1134. #define USB_DEVICE_ID_DELORME_EM_LT20 0x0200
  1135. #define USB_VENDOR_ID_MCC 0x09db
  1136. #define USB_DEVICE_ID_MCC_PMD1024LS 0x0076
  1137. #define USB_DEVICE_ID_MCC_PMD1208LS 0x007a
  1138. #define USB_VENDOR_ID_CHICONY 0x04f2
  1139. #define USB_DEVICE_ID_CHICONY_USBHUB_KB 0x0100
  1140. #define USB_VENDOR_ID_BTC 0x046e
  1141. #define USB_DEVICE_ID_BTC_KEYBOARD 0x5303
  1142. /*
  1143. * Alphabetically sorted blacklist by quirk type.
  1144. */
  1145. static struct hid_blacklist {
  1146. __u16 idVendor;
  1147. __u16 idProduct;
  1148. unsigned quirks;
  1149. } hid_blacklist[] = {
  1150. { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_01, HID_QUIRK_IGNORE },
  1151. { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_10, HID_QUIRK_IGNORE },
  1152. { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_20, HID_QUIRK_IGNORE },
  1153. { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_21, HID_QUIRK_IGNORE },
  1154. { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_22, HID_QUIRK_IGNORE },
  1155. { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_23, HID_QUIRK_IGNORE },
  1156. { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_24, HID_QUIRK_IGNORE },
  1157. { USB_VENDOR_ID_BERKSHIRE, USB_DEVICE_ID_BERKSHIRE_PCWD, HID_QUIRK_IGNORE },
  1158. { USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW40, HID_QUIRK_IGNORE },
  1159. { USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW24, HID_QUIRK_IGNORE },
  1160. { USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW48, HID_QUIRK_IGNORE },
  1161. { USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW28, HID_QUIRK_IGNORE },
  1162. { USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_HIDCOM, HID_QUIRK_IGNORE },
  1163. { USB_VENDOR_ID_DELORME, USB_DEVICE_ID_DELORME_EARTHMATE, HID_QUIRK_IGNORE },
  1164. { USB_VENDOR_ID_DELORME, USB_DEVICE_ID_DELORME_EM_LT20, HID_QUIRK_IGNORE },
  1165. { USB_VENDOR_ID_ESSENTIAL_REALITY, USB_DEVICE_ID_ESSENTIAL_REALITY_P5, HID_QUIRK_IGNORE },
  1166. { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_4_PHIDGETSERVO_30, HID_QUIRK_IGNORE },
  1167. { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_1_PHIDGETSERVO_30, HID_QUIRK_IGNORE },
  1168. { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_8_8_8_IF_KIT, HID_QUIRK_IGNORE },
  1169. { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_0_4_IF_KIT, HID_QUIRK_IGNORE },
  1170. { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_8_8_IF_KIT, HID_QUIRK_IGNORE },
  1171. { USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_POWERMATE, HID_QUIRK_IGNORE },
  1172. { USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_SOUNDKNOB, HID_QUIRK_IGNORE },
  1173. { USB_VENDOR_ID_KBGEAR, USB_DEVICE_ID_KBGEAR_JAMSTUDIO, HID_QUIRK_IGNORE },
  1174. { USB_VENDOR_ID_MCC, USB_DEVICE_ID_MCC_PMD1024LS, HID_QUIRK_IGNORE },
  1175. { USB_VENDOR_ID_MCC, USB_DEVICE_ID_MCC_PMD1208LS, HID_QUIRK_IGNORE },
  1176. { USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS, HID_QUIRK_IGNORE },
  1177. { USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS1, HID_QUIRK_IGNORE },
  1178. { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100, HID_QUIRK_IGNORE },
  1179. { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 100, HID_QUIRK_IGNORE },
  1180. { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 200, HID_QUIRK_IGNORE },
  1181. { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 300, HID_QUIRK_IGNORE },
  1182. { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 400, HID_QUIRK_IGNORE },
  1183. { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 500, HID_QUIRK_IGNORE },
  1184. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PENPARTNER, HID_QUIRK_IGNORE },
  1185. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE, HID_QUIRK_IGNORE },
  1186. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE + 1, HID_QUIRK_IGNORE },
  1187. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE + 2, HID_QUIRK_IGNORE },
  1188. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE + 3, HID_QUIRK_IGNORE },
  1189. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE + 4, HID_QUIRK_IGNORE },
  1190. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS, HID_QUIRK_IGNORE },
  1191. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS + 1, HID_QUIRK_IGNORE },
  1192. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS + 2, HID_QUIRK_IGNORE },
  1193. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS + 3, HID_QUIRK_IGNORE },
  1194. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS + 4, HID_QUIRK_IGNORE },
  1195. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL, HID_QUIRK_IGNORE },
  1196. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 1, HID_QUIRK_IGNORE },
  1197. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 2, HID_QUIRK_IGNORE },
  1198. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 3, HID_QUIRK_IGNORE },
  1199. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 4, HID_QUIRK_IGNORE },
  1200. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 5, HID_QUIRK_IGNORE },
  1201. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 1, HID_QUIRK_IGNORE },
  1202. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 2, HID_QUIRK_IGNORE },
  1203. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 3, HID_QUIRK_IGNORE },
  1204. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 4, HID_QUIRK_IGNORE },
  1205. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 5, HID_QUIRK_IGNORE },
  1206. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 7, HID_QUIRK_IGNORE },
  1207. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_VOLITO, HID_QUIRK_IGNORE },
  1208. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PTU, HID_QUIRK_IGNORE },
  1209. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS3, HID_QUIRK_IGNORE },
  1210. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS3 + 1, HID_QUIRK_IGNORE },
  1211. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS3 + 2, HID_QUIRK_IGNORE },
  1212. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_CINTIQ, HID_QUIRK_IGNORE },
  1213. { USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_4_PHIDGETSERVO_20, HID_QUIRK_IGNORE },
  1214. { USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_1_PHIDGETSERVO_20, HID_QUIRK_IGNORE },
  1215. { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_UC100KM, HID_QUIRK_NOGET },
  1216. { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_CS124U, HID_QUIRK_NOGET },
  1217. { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_2PORTKVM, HID_QUIRK_NOGET },
  1218. { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_4PORTKVM, HID_QUIRK_NOGET },
  1219. { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_4PORTKVMC, HID_QUIRK_NOGET },
  1220. { USB_VENDOR_ID_BTC, USB_DEVICE_ID_BTC_KEYBOARD, HID_QUIRK_NOGET},
  1221. { USB_VENDOR_ID_CHICONY, USB_DEVICE_ID_CHICONY_USBHUB_KB, HID_QUIRK_NOGET},
  1222. { USB_VENDOR_ID_TANGTOP, USB_DEVICE_ID_TANGTOP_USBPS2, HID_QUIRK_NOGET },
  1223. { USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_WCP32PU, HID_QUIRK_2WHEEL_MOUSE_HACK_7 },
  1224. { USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_MOUSE, HID_QUIRK_2WHEEL_MOUSE_HACK_5 },
  1225. { USB_VENDOR_ID_ALPS, USB_DEVICE_ID_IBM_GAMEPAD, HID_QUIRK_BADPAD },
  1226. { USB_VENDOR_ID_CHIC, USB_DEVICE_ID_CHIC_GAMEPAD, HID_QUIRK_BADPAD },
  1227. { USB_VENDOR_ID_HAPP, USB_DEVICE_ID_UGCI_DRIVING, HID_QUIRK_BADPAD | HID_QUIRK_MULTI_INPUT },
  1228. { USB_VENDOR_ID_HAPP, USB_DEVICE_ID_UGCI_FLYING, HID_QUIRK_BADPAD | HID_QUIRK_MULTI_INPUT },
  1229. { USB_VENDOR_ID_HAPP, USB_DEVICE_ID_UGCI_FIGHTING, HID_QUIRK_BADPAD | HID_QUIRK_MULTI_INPUT },
  1230. { USB_VENDOR_ID_NEC, USB_DEVICE_ID_NEC_USB_GAME_PAD, HID_QUIRK_BADPAD },
  1231. { USB_VENDOR_ID_SAITEK, USB_DEVICE_ID_SAITEK_RUMBLEPAD, HID_QUIRK_BADPAD },
  1232. { USB_VENDOR_ID_TOPMAX, USB_DEVICE_ID_TOPMAX_COBRAPAD, HID_QUIRK_BADPAD },
  1233. { 0, 0 }
  1234. };
  1235. static int hid_alloc_buffers(struct usb_device *dev, struct hid_device *hid)
  1236. {
  1237. if (!(hid->inbuf = usb_buffer_alloc(dev, HID_BUFFER_SIZE, SLAB_ATOMIC, &hid->inbuf_dma)))
  1238. return -1;
  1239. if (!(hid->outbuf = usb_buffer_alloc(dev, HID_BUFFER_SIZE, SLAB_ATOMIC, &hid->outbuf_dma)))
  1240. return -1;
  1241. if (!(hid->cr = usb_buffer_alloc(dev, sizeof(*(hid->cr)), SLAB_ATOMIC, &hid->cr_dma)))
  1242. return -1;
  1243. if (!(hid->ctrlbuf = usb_buffer_alloc(dev, HID_BUFFER_SIZE, SLAB_ATOMIC, &hid->ctrlbuf_dma)))
  1244. return -1;
  1245. return 0;
  1246. }
  1247. static void hid_free_buffers(struct usb_device *dev, struct hid_device *hid)
  1248. {
  1249. if (hid->inbuf)
  1250. usb_buffer_free(dev, HID_BUFFER_SIZE, hid->inbuf, hid->inbuf_dma);
  1251. if (hid->outbuf)
  1252. usb_buffer_free(dev, HID_BUFFER_SIZE, hid->outbuf, hid->outbuf_dma);
  1253. if (hid->cr)
  1254. usb_buffer_free(dev, sizeof(*(hid->cr)), hid->cr, hid->cr_dma);
  1255. if (hid->ctrlbuf)
  1256. usb_buffer_free(dev, HID_BUFFER_SIZE, hid->ctrlbuf, hid->ctrlbuf_dma);
  1257. }
  1258. static struct hid_device *usb_hid_configure(struct usb_interface *intf)
  1259. {
  1260. struct usb_host_interface *interface = intf->cur_altsetting;
  1261. struct usb_device *dev = interface_to_usbdev (intf);
  1262. struct hid_descriptor *hdesc;
  1263. struct hid_device *hid;
  1264. unsigned quirks = 0, rsize = 0;
  1265. char *buf, *rdesc;
  1266. int n;
  1267. for (n = 0; hid_blacklist[n].idVendor; n++)
  1268. if ((hid_blacklist[n].idVendor == le16_to_cpu(dev->descriptor.idVendor)) &&
  1269. (hid_blacklist[n].idProduct == le16_to_cpu(dev->descriptor.idProduct)))
  1270. quirks = hid_blacklist[n].quirks;
  1271. if (quirks & HID_QUIRK_IGNORE)
  1272. return NULL;
  1273. if (usb_get_extra_descriptor(interface, HID_DT_HID, &hdesc) && ((!interface->desc.bNumEndpoints) ||
  1274. usb_get_extra_descriptor(&interface->endpoint[0], HID_DT_HID, &hdesc))) {
  1275. dbg("class descriptor not present\n");
  1276. return NULL;
  1277. }
  1278. for (n = 0; n < hdesc->bNumDescriptors; n++)
  1279. if (hdesc->desc[n].bDescriptorType == HID_DT_REPORT)
  1280. rsize = le16_to_cpu(hdesc->desc[n].wDescriptorLength);
  1281. if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
  1282. dbg("weird size of report descriptor (%u)", rsize);
  1283. return NULL;
  1284. }
  1285. if (!(rdesc = kmalloc(rsize, GFP_KERNEL))) {
  1286. dbg("couldn't allocate rdesc memory");
  1287. return NULL;
  1288. }
  1289. if ((n = hid_get_class_descriptor(dev, interface->desc.bInterfaceNumber, HID_DT_REPORT, rdesc, rsize)) < 0) {
  1290. dbg("reading report descriptor failed");
  1291. kfree(rdesc);
  1292. return NULL;
  1293. }
  1294. #ifdef DEBUG_DATA
  1295. printk(KERN_DEBUG __FILE__ ": report descriptor (size %u, read %d) = ", rsize, n);
  1296. for (n = 0; n < rsize; n++)
  1297. printk(" %02x", (unsigned char) rdesc[n]);
  1298. printk("\n");
  1299. #endif
  1300. if (!(hid = hid_parse_report(rdesc, n))) {
  1301. dbg("parsing report descriptor failed");
  1302. kfree(rdesc);
  1303. return NULL;
  1304. }
  1305. kfree(rdesc);
  1306. hid->quirks = quirks;
  1307. if (hid_alloc_buffers(dev, hid)) {
  1308. hid_free_buffers(dev, hid);
  1309. goto fail;
  1310. }
  1311. for (n = 0; n < interface->desc.bNumEndpoints; n++) {
  1312. struct usb_endpoint_descriptor *endpoint;
  1313. int pipe;
  1314. int interval;
  1315. endpoint = &interface->endpoint[n].desc;
  1316. if ((endpoint->bmAttributes & 3) != 3) /* Not an interrupt endpoint */
  1317. continue;
  1318. /* handle potential highspeed HID correctly */
  1319. interval = endpoint->bInterval;
  1320. if (dev->speed == USB_SPEED_HIGH)
  1321. interval = 1 << (interval - 1);
  1322. /* Change the polling interval of mice. */
  1323. if (hid->collection->usage == HID_GD_MOUSE && hid_mousepoll_interval > 0)
  1324. interval = hid_mousepoll_interval;
  1325. if (endpoint->bEndpointAddress & USB_DIR_IN) {
  1326. if (hid->urbin)
  1327. continue;
  1328. if (!(hid->urbin = usb_alloc_urb(0, GFP_KERNEL)))
  1329. goto fail;
  1330. pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
  1331. usb_fill_int_urb(hid->urbin, dev, pipe, hid->inbuf, 0,
  1332. hid_irq_in, hid, interval);
  1333. hid->urbin->transfer_dma = hid->inbuf_dma;
  1334. hid->urbin->transfer_flags |=(URB_NO_TRANSFER_DMA_MAP | URB_ASYNC_UNLINK);
  1335. } else {
  1336. if (hid->urbout)
  1337. continue;
  1338. if (!(hid->urbout = usb_alloc_urb(0, GFP_KERNEL)))
  1339. goto fail;
  1340. pipe = usb_sndintpipe(dev, endpoint->bEndpointAddress);
  1341. usb_fill_int_urb(hid->urbout, dev, pipe, hid->outbuf, 0,
  1342. hid_irq_out, hid, interval);
  1343. hid->urbout->transfer_dma = hid->outbuf_dma;
  1344. hid->urbout->transfer_flags |= (URB_NO_TRANSFER_DMA_MAP | URB_ASYNC_UNLINK);
  1345. }
  1346. }
  1347. if (!hid->urbin) {
  1348. err("couldn't find an input interrupt endpoint");
  1349. goto fail;
  1350. }
  1351. init_waitqueue_head(&hid->wait);
  1352. spin_lock_init(&hid->outlock);
  1353. spin_lock_init(&hid->ctrllock);
  1354. hid->version = le16_to_cpu(hdesc->bcdHID);
  1355. hid->country = hdesc->bCountryCode;
  1356. hid->dev = dev;
  1357. hid->intf = intf;
  1358. hid->ifnum = interface->desc.bInterfaceNumber;
  1359. hid->name[0] = 0;
  1360. if (!(buf = kmalloc(64, GFP_KERNEL)))
  1361. goto fail;
  1362. if (dev->manufacturer) {
  1363. strcat(hid->name, dev->manufacturer);
  1364. if (dev->product)
  1365. snprintf(hid->name, 64, "%s %s", hid->name, dev->product);
  1366. } else if (dev->product) {
  1367. snprintf(hid->name, 128, "%s", dev->product);
  1368. } else
  1369. snprintf(hid->name, 128, "%04x:%04x",
  1370. le16_to_cpu(dev->descriptor.idVendor),
  1371. le16_to_cpu(dev->descriptor.idProduct));
  1372. usb_make_path(dev, buf, 64);
  1373. snprintf(hid->phys, 64, "%s/input%d", buf,
  1374. intf->altsetting[0].desc.bInterfaceNumber);
  1375. if (usb_string(dev, dev->descriptor.iSerialNumber, hid->uniq, 64) <= 0)
  1376. hid->uniq[0] = 0;
  1377. kfree(buf);
  1378. hid->urbctrl = usb_alloc_urb(0, GFP_KERNEL);
  1379. if (!hid->urbctrl)
  1380. goto fail;
  1381. usb_fill_control_urb(hid->urbctrl, dev, 0, (void *) hid->cr,
  1382. hid->ctrlbuf, 1, hid_ctrl, hid);
  1383. hid->urbctrl->setup_dma = hid->cr_dma;
  1384. hid->urbctrl->transfer_dma = hid->ctrlbuf_dma;
  1385. hid->urbctrl->transfer_flags |= (URB_NO_TRANSFER_DMA_MAP | URB_NO_SETUP_DMA_MAP | URB_ASYNC_UNLINK);
  1386. return hid;
  1387. fail:
  1388. if (hid->urbin)
  1389. usb_free_urb(hid->urbin);
  1390. if (hid->urbout)
  1391. usb_free_urb(hid->urbout);
  1392. if (hid->urbctrl)
  1393. usb_free_urb(hid->urbctrl);
  1394. hid_free_buffers(dev, hid);
  1395. hid_free_device(hid);
  1396. return NULL;
  1397. }
  1398. static void hid_disconnect(struct usb_interface *intf)
  1399. {
  1400. struct hid_device *hid = usb_get_intfdata (intf);
  1401. if (!hid)
  1402. return;
  1403. usb_set_intfdata(intf, NULL);
  1404. usb_kill_urb(hid->urbin);
  1405. usb_kill_urb(hid->urbout);
  1406. usb_kill_urb(hid->urbctrl);
  1407. if (hid->claimed & HID_CLAIMED_INPUT)
  1408. hidinput_disconnect(hid);
  1409. if (hid->claimed & HID_CLAIMED_HIDDEV)
  1410. hiddev_disconnect(hid);
  1411. usb_free_urb(hid->urbin);
  1412. usb_free_urb(hid->urbctrl);
  1413. if (hid->urbout)
  1414. usb_free_urb(hid->urbout);
  1415. hid_free_buffers(hid->dev, hid);
  1416. hid_free_device(hid);
  1417. }
  1418. static int hid_probe(struct usb_interface *intf, const struct usb_device_id *id)
  1419. {
  1420. struct hid_device *hid;
  1421. char path[64];
  1422. int i;
  1423. char *c;
  1424. dbg("HID probe called for ifnum %d",
  1425. intf->altsetting->desc.bInterfaceNumber);
  1426. if (!(hid = usb_hid_configure(intf)))
  1427. return -EIO;
  1428. hid_init_reports(hid);
  1429. hid_dump_device(hid);
  1430. if (!hidinput_connect(hid))
  1431. hid->claimed |= HID_CLAIMED_INPUT;
  1432. if (!hiddev_connect(hid))
  1433. hid->claimed |= HID_CLAIMED_HIDDEV;
  1434. usb_set_intfdata(intf, hid);
  1435. if (!hid->claimed) {
  1436. printk ("HID device not claimed by input or hiddev\n");
  1437. hid_disconnect(intf);
  1438. return -EIO;
  1439. }
  1440. printk(KERN_INFO);
  1441. if (hid->claimed & HID_CLAIMED_INPUT)
  1442. printk("input");
  1443. if (hid->claimed == (HID_CLAIMED_INPUT | HID_CLAIMED_HIDDEV))
  1444. printk(",");
  1445. if (hid->claimed & HID_CLAIMED_HIDDEV)
  1446. printk("hiddev%d", hid->minor);
  1447. c = "Device";
  1448. for (i = 0; i < hid->maxcollection; i++) {
  1449. if (hid->collection[i].type == HID_COLLECTION_APPLICATION &&
  1450. (hid->collection[i].usage & HID_USAGE_PAGE) == HID_UP_GENDESK &&
  1451. (hid->collection[i].usage & 0xffff) < ARRAY_SIZE(hid_types)) {
  1452. c = hid_types[hid->collection[i].usage & 0xffff];
  1453. break;
  1454. }
  1455. }
  1456. usb_make_path(interface_to_usbdev(intf), path, 63);
  1457. printk(": USB HID v%x.%02x %s [%s] on %s\n",
  1458. hid->version >> 8, hid->version & 0xff, c, hid->name, path);
  1459. return 0;
  1460. }
  1461. static int hid_suspend(struct usb_interface *intf, pm_message_t message)
  1462. {
  1463. struct hid_device *hid = usb_get_intfdata (intf);
  1464. usb_kill_urb(hid->urbin);
  1465. intf->dev.power.power_state = PMSG_SUSPEND;
  1466. dev_dbg(&intf->dev, "suspend\n");
  1467. return 0;
  1468. }
  1469. static int hid_resume(struct usb_interface *intf)
  1470. {
  1471. struct hid_device *hid = usb_get_intfdata (intf);
  1472. int status;
  1473. intf->dev.power.power_state = PMSG_ON;
  1474. if (hid->open)
  1475. status = usb_submit_urb(hid->urbin, GFP_NOIO);
  1476. else
  1477. status = 0;
  1478. dev_dbg(&intf->dev, "resume status %d\n", status);
  1479. return status;
  1480. }
  1481. static struct usb_device_id hid_usb_ids [] = {
  1482. { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
  1483. .bInterfaceClass = USB_INTERFACE_CLASS_HID },
  1484. { } /* Terminating entry */
  1485. };
  1486. MODULE_DEVICE_TABLE (usb, hid_usb_ids);
  1487. static struct usb_driver hid_driver = {
  1488. .owner = THIS_MODULE,
  1489. .name = "usbhid",
  1490. .probe = hid_probe,
  1491. .disconnect = hid_disconnect,
  1492. .suspend = hid_suspend,
  1493. .resume = hid_resume,
  1494. .id_table = hid_usb_ids,
  1495. };
  1496. static int __init hid_init(void)
  1497. {
  1498. int retval;
  1499. retval = hiddev_init();
  1500. if (retval)
  1501. goto hiddev_init_fail;
  1502. retval = usb_register(&hid_driver);
  1503. if (retval)
  1504. goto usb_register_fail;
  1505. info(DRIVER_VERSION ":" DRIVER_DESC);
  1506. return 0;
  1507. usb_register_fail:
  1508. hiddev_exit();
  1509. hiddev_init_fail:
  1510. return retval;
  1511. }
  1512. static void __exit hid_exit(void)
  1513. {
  1514. usb_deregister(&hid_driver);
  1515. hiddev_exit();
  1516. }
  1517. module_init(hid_init);
  1518. module_exit(hid_exit);
  1519. MODULE_AUTHOR(DRIVER_AUTHOR);
  1520. MODULE_DESCRIPTION(DRIVER_DESC);
  1521. MODULE_LICENSE(DRIVER_LICENSE);