hid-core.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177
  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. * Copyright (c) 2006-2008 Jiri Kosina
  8. */
  9. /*
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the Free
  12. * Software Foundation; either version 2 of the License, or (at your option)
  13. * any later version.
  14. */
  15. #include <linux/module.h>
  16. #include <linux/slab.h>
  17. #include <linux/init.h>
  18. #include <linux/kernel.h>
  19. #include <linux/list.h>
  20. #include <linux/mm.h>
  21. #include <linux/mutex.h>
  22. #include <linux/smp_lock.h>
  23. #include <linux/spinlock.h>
  24. #include <asm/unaligned.h>
  25. #include <asm/byteorder.h>
  26. #include <linux/input.h>
  27. #include <linux/wait.h>
  28. #include <linux/usb.h>
  29. #include <linux/hid.h>
  30. #include <linux/hiddev.h>
  31. #include <linux/hid-debug.h>
  32. #include <linux/hidraw.h>
  33. #include "usbhid.h"
  34. /*
  35. * Version Information
  36. */
  37. #define DRIVER_VERSION "v2.6"
  38. #define DRIVER_AUTHOR "Andreas Gal, Vojtech Pavlik, Jiri Kosina"
  39. #define DRIVER_DESC "USB HID core driver"
  40. #define DRIVER_LICENSE "GPL"
  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. /* Quirks specified at module load time */
  48. static char *quirks_param[MAX_USBHID_BOOT_QUIRKS] = { [ 0 ... (MAX_USBHID_BOOT_QUIRKS - 1) ] = NULL };
  49. module_param_array_named(quirks, quirks_param, charp, NULL, 0444);
  50. MODULE_PARM_DESC(quirks, "Add/modify USB HID quirks by specifying "
  51. " quirks=vendorID:productID:quirks"
  52. " where vendorID, productID, and quirks are all in"
  53. " 0x-prefixed hex");
  54. /*
  55. * Input submission and I/O error handler.
  56. */
  57. static void hid_io_error(struct hid_device *hid);
  58. /* Start up the input URB */
  59. static int hid_start_in(struct hid_device *hid)
  60. {
  61. unsigned long flags;
  62. int rc = 0;
  63. struct usbhid_device *usbhid = hid->driver_data;
  64. spin_lock_irqsave(&usbhid->inlock, flags);
  65. if (hid->open > 0 && !test_bit(HID_SUSPENDED, &usbhid->iofl) &&
  66. !test_bit(HID_DISCONNECTED, &usbhid->iofl) &&
  67. !test_and_set_bit(HID_IN_RUNNING, &usbhid->iofl)) {
  68. rc = usb_submit_urb(usbhid->urbin, GFP_ATOMIC);
  69. if (rc != 0)
  70. clear_bit(HID_IN_RUNNING, &usbhid->iofl);
  71. }
  72. spin_unlock_irqrestore(&usbhid->inlock, flags);
  73. return rc;
  74. }
  75. /* I/O retry timer routine */
  76. static void hid_retry_timeout(unsigned long _hid)
  77. {
  78. struct hid_device *hid = (struct hid_device *) _hid;
  79. struct usbhid_device *usbhid = hid->driver_data;
  80. dev_dbg(&usbhid->intf->dev, "retrying intr urb\n");
  81. if (hid_start_in(hid))
  82. hid_io_error(hid);
  83. }
  84. /* Workqueue routine to reset the device or clear a halt */
  85. static void hid_reset(struct work_struct *work)
  86. {
  87. struct usbhid_device *usbhid =
  88. container_of(work, struct usbhid_device, reset_work);
  89. struct hid_device *hid = usbhid->hid;
  90. int rc = 0;
  91. if (test_bit(HID_CLEAR_HALT, &usbhid->iofl)) {
  92. dev_dbg(&usbhid->intf->dev, "clear halt\n");
  93. rc = usb_clear_halt(hid_to_usb_dev(hid), usbhid->urbin->pipe);
  94. clear_bit(HID_CLEAR_HALT, &usbhid->iofl);
  95. hid_start_in(hid);
  96. }
  97. else if (test_bit(HID_RESET_PENDING, &usbhid->iofl)) {
  98. dev_dbg(&usbhid->intf->dev, "resetting device\n");
  99. rc = usb_lock_device_for_reset(hid_to_usb_dev(hid), usbhid->intf);
  100. if (rc == 0) {
  101. rc = usb_reset_device(hid_to_usb_dev(hid));
  102. usb_unlock_device(hid_to_usb_dev(hid));
  103. }
  104. clear_bit(HID_RESET_PENDING, &usbhid->iofl);
  105. }
  106. switch (rc) {
  107. case 0:
  108. if (!test_bit(HID_IN_RUNNING, &usbhid->iofl))
  109. hid_io_error(hid);
  110. break;
  111. default:
  112. err_hid("can't reset device, %s-%s/input%d, status %d",
  113. hid_to_usb_dev(hid)->bus->bus_name,
  114. hid_to_usb_dev(hid)->devpath,
  115. usbhid->ifnum, rc);
  116. /* FALLTHROUGH */
  117. case -EHOSTUNREACH:
  118. case -ENODEV:
  119. case -EINTR:
  120. break;
  121. }
  122. }
  123. /* Main I/O error handler */
  124. static void hid_io_error(struct hid_device *hid)
  125. {
  126. unsigned long flags;
  127. struct usbhid_device *usbhid = hid->driver_data;
  128. spin_lock_irqsave(&usbhid->inlock, flags);
  129. /* Stop when disconnected */
  130. if (test_bit(HID_DISCONNECTED, &usbhid->iofl))
  131. goto done;
  132. /* If it has been a while since the last error, we'll assume
  133. * this a brand new error and reset the retry timeout. */
  134. if (time_after(jiffies, usbhid->stop_retry + HZ/2))
  135. usbhid->retry_delay = 0;
  136. /* When an error occurs, retry at increasing intervals */
  137. if (usbhid->retry_delay == 0) {
  138. usbhid->retry_delay = 13; /* Then 26, 52, 104, 104, ... */
  139. usbhid->stop_retry = jiffies + msecs_to_jiffies(1000);
  140. } else if (usbhid->retry_delay < 100)
  141. usbhid->retry_delay *= 2;
  142. if (time_after(jiffies, usbhid->stop_retry)) {
  143. /* Retries failed, so do a port reset */
  144. if (!test_and_set_bit(HID_RESET_PENDING, &usbhid->iofl)) {
  145. schedule_work(&usbhid->reset_work);
  146. goto done;
  147. }
  148. }
  149. mod_timer(&usbhid->io_retry,
  150. jiffies + msecs_to_jiffies(usbhid->retry_delay));
  151. done:
  152. spin_unlock_irqrestore(&usbhid->inlock, flags);
  153. }
  154. /*
  155. * Input interrupt completion handler.
  156. */
  157. static void hid_irq_in(struct urb *urb)
  158. {
  159. struct hid_device *hid = urb->context;
  160. struct usbhid_device *usbhid = hid->driver_data;
  161. int status;
  162. switch (urb->status) {
  163. case 0: /* success */
  164. usbhid->retry_delay = 0;
  165. hid_input_report(urb->context, HID_INPUT_REPORT,
  166. urb->transfer_buffer,
  167. urb->actual_length, 1);
  168. break;
  169. case -EPIPE: /* stall */
  170. clear_bit(HID_IN_RUNNING, &usbhid->iofl);
  171. set_bit(HID_CLEAR_HALT, &usbhid->iofl);
  172. schedule_work(&usbhid->reset_work);
  173. return;
  174. case -ECONNRESET: /* unlink */
  175. case -ENOENT:
  176. case -ESHUTDOWN: /* unplug */
  177. clear_bit(HID_IN_RUNNING, &usbhid->iofl);
  178. return;
  179. case -EILSEQ: /* protocol error or unplug */
  180. case -EPROTO: /* protocol error or unplug */
  181. case -ETIME: /* protocol error or unplug */
  182. case -ETIMEDOUT: /* Should never happen, but... */
  183. clear_bit(HID_IN_RUNNING, &usbhid->iofl);
  184. hid_io_error(hid);
  185. return;
  186. default: /* error */
  187. dev_warn(&urb->dev->dev, "input irq status %d "
  188. "received\n", urb->status);
  189. }
  190. status = usb_submit_urb(urb, GFP_ATOMIC);
  191. if (status) {
  192. clear_bit(HID_IN_RUNNING, &usbhid->iofl);
  193. if (status != -EPERM) {
  194. err_hid("can't resubmit intr, %s-%s/input%d, status %d",
  195. hid_to_usb_dev(hid)->bus->bus_name,
  196. hid_to_usb_dev(hid)->devpath,
  197. usbhid->ifnum, status);
  198. hid_io_error(hid);
  199. }
  200. }
  201. }
  202. static int hid_submit_out(struct hid_device *hid)
  203. {
  204. struct hid_report *report;
  205. char *raw_report;
  206. struct usbhid_device *usbhid = hid->driver_data;
  207. report = usbhid->out[usbhid->outtail].report;
  208. raw_report = usbhid->out[usbhid->outtail].raw_report;
  209. usbhid->urbout->transfer_buffer_length = ((report->size - 1) >> 3) + 1 + (report->id > 0);
  210. usbhid->urbout->dev = hid_to_usb_dev(hid);
  211. memcpy(usbhid->outbuf, raw_report, usbhid->urbout->transfer_buffer_length);
  212. kfree(raw_report);
  213. dbg_hid("submitting out urb\n");
  214. if (usb_submit_urb(usbhid->urbout, GFP_ATOMIC)) {
  215. err_hid("usb_submit_urb(out) failed");
  216. return -1;
  217. }
  218. return 0;
  219. }
  220. static int hid_submit_ctrl(struct hid_device *hid)
  221. {
  222. struct hid_report *report;
  223. unsigned char dir;
  224. char *raw_report;
  225. int len;
  226. struct usbhid_device *usbhid = hid->driver_data;
  227. report = usbhid->ctrl[usbhid->ctrltail].report;
  228. raw_report = usbhid->ctrl[usbhid->ctrltail].raw_report;
  229. dir = usbhid->ctrl[usbhid->ctrltail].dir;
  230. len = ((report->size - 1) >> 3) + 1 + (report->id > 0);
  231. if (dir == USB_DIR_OUT) {
  232. usbhid->urbctrl->pipe = usb_sndctrlpipe(hid_to_usb_dev(hid), 0);
  233. usbhid->urbctrl->transfer_buffer_length = len;
  234. memcpy(usbhid->ctrlbuf, raw_report, len);
  235. kfree(raw_report);
  236. } else {
  237. int maxpacket, padlen;
  238. usbhid->urbctrl->pipe = usb_rcvctrlpipe(hid_to_usb_dev(hid), 0);
  239. maxpacket = usb_maxpacket(hid_to_usb_dev(hid), usbhid->urbctrl->pipe, 0);
  240. if (maxpacket > 0) {
  241. padlen = DIV_ROUND_UP(len, maxpacket);
  242. padlen *= maxpacket;
  243. if (padlen > usbhid->bufsize)
  244. padlen = usbhid->bufsize;
  245. } else
  246. padlen = 0;
  247. usbhid->urbctrl->transfer_buffer_length = padlen;
  248. }
  249. usbhid->urbctrl->dev = hid_to_usb_dev(hid);
  250. usbhid->cr->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE | dir;
  251. usbhid->cr->bRequest = (dir == USB_DIR_OUT) ? HID_REQ_SET_REPORT : HID_REQ_GET_REPORT;
  252. usbhid->cr->wValue = cpu_to_le16(((report->type + 1) << 8) | report->id);
  253. usbhid->cr->wIndex = cpu_to_le16(usbhid->ifnum);
  254. usbhid->cr->wLength = cpu_to_le16(len);
  255. dbg_hid("submitting ctrl urb: %s wValue=0x%04x wIndex=0x%04x wLength=%u\n",
  256. usbhid->cr->bRequest == HID_REQ_SET_REPORT ? "Set_Report" : "Get_Report",
  257. usbhid->cr->wValue, usbhid->cr->wIndex, usbhid->cr->wLength);
  258. if (usb_submit_urb(usbhid->urbctrl, GFP_ATOMIC)) {
  259. err_hid("usb_submit_urb(ctrl) failed");
  260. return -1;
  261. }
  262. return 0;
  263. }
  264. /*
  265. * Output interrupt completion handler.
  266. */
  267. static void hid_irq_out(struct urb *urb)
  268. {
  269. struct hid_device *hid = urb->context;
  270. struct usbhid_device *usbhid = hid->driver_data;
  271. unsigned long flags;
  272. int unplug = 0;
  273. switch (urb->status) {
  274. case 0: /* success */
  275. break;
  276. case -ESHUTDOWN: /* unplug */
  277. unplug = 1;
  278. case -EILSEQ: /* protocol error or unplug */
  279. case -EPROTO: /* protocol error or unplug */
  280. case -ECONNRESET: /* unlink */
  281. case -ENOENT:
  282. break;
  283. default: /* error */
  284. dev_warn(&urb->dev->dev, "output irq status %d "
  285. "received\n", urb->status);
  286. }
  287. spin_lock_irqsave(&usbhid->outlock, flags);
  288. if (unplug)
  289. usbhid->outtail = usbhid->outhead;
  290. else
  291. usbhid->outtail = (usbhid->outtail + 1) & (HID_OUTPUT_FIFO_SIZE - 1);
  292. if (usbhid->outhead != usbhid->outtail) {
  293. if (hid_submit_out(hid)) {
  294. clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
  295. wake_up(&usbhid->wait);
  296. }
  297. spin_unlock_irqrestore(&usbhid->outlock, flags);
  298. return;
  299. }
  300. clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
  301. spin_unlock_irqrestore(&usbhid->outlock, flags);
  302. wake_up(&usbhid->wait);
  303. }
  304. /*
  305. * Control pipe completion handler.
  306. */
  307. static void hid_ctrl(struct urb *urb)
  308. {
  309. struct hid_device *hid = urb->context;
  310. struct usbhid_device *usbhid = hid->driver_data;
  311. unsigned long flags;
  312. int unplug = 0;
  313. spin_lock_irqsave(&usbhid->ctrllock, flags);
  314. switch (urb->status) {
  315. case 0: /* success */
  316. if (usbhid->ctrl[usbhid->ctrltail].dir == USB_DIR_IN)
  317. hid_input_report(urb->context,
  318. usbhid->ctrl[usbhid->ctrltail].report->type,
  319. urb->transfer_buffer, urb->actual_length, 0);
  320. break;
  321. case -ESHUTDOWN: /* unplug */
  322. unplug = 1;
  323. case -EILSEQ: /* protocol error or unplug */
  324. case -EPROTO: /* protocol error or unplug */
  325. case -ECONNRESET: /* unlink */
  326. case -ENOENT:
  327. case -EPIPE: /* report not available */
  328. break;
  329. default: /* error */
  330. dev_warn(&urb->dev->dev, "ctrl urb status %d "
  331. "received\n", urb->status);
  332. }
  333. if (unplug)
  334. usbhid->ctrltail = usbhid->ctrlhead;
  335. else
  336. usbhid->ctrltail = (usbhid->ctrltail + 1) & (HID_CONTROL_FIFO_SIZE - 1);
  337. if (usbhid->ctrlhead != usbhid->ctrltail) {
  338. if (hid_submit_ctrl(hid)) {
  339. clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
  340. wake_up(&usbhid->wait);
  341. }
  342. spin_unlock_irqrestore(&usbhid->ctrllock, flags);
  343. return;
  344. }
  345. clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
  346. spin_unlock_irqrestore(&usbhid->ctrllock, flags);
  347. wake_up(&usbhid->wait);
  348. }
  349. void usbhid_submit_report(struct hid_device *hid, struct hid_report *report, unsigned char dir)
  350. {
  351. int head;
  352. unsigned long flags;
  353. struct usbhid_device *usbhid = hid->driver_data;
  354. int len = ((report->size - 1) >> 3) + 1 + (report->id > 0);
  355. if ((hid->quirks & HID_QUIRK_NOGET) && dir == USB_DIR_IN)
  356. return;
  357. if (usbhid->urbout && dir == USB_DIR_OUT && report->type == HID_OUTPUT_REPORT) {
  358. spin_lock_irqsave(&usbhid->outlock, flags);
  359. if ((head = (usbhid->outhead + 1) & (HID_OUTPUT_FIFO_SIZE - 1)) == usbhid->outtail) {
  360. spin_unlock_irqrestore(&usbhid->outlock, flags);
  361. dev_warn(&hid->dev, "output queue full\n");
  362. return;
  363. }
  364. usbhid->out[usbhid->outhead].raw_report = kmalloc(len, GFP_ATOMIC);
  365. if (!usbhid->out[usbhid->outhead].raw_report) {
  366. spin_unlock_irqrestore(&usbhid->outlock, flags);
  367. dev_warn(&hid->dev, "output queueing failed\n");
  368. return;
  369. }
  370. hid_output_report(report, usbhid->out[usbhid->outhead].raw_report);
  371. usbhid->out[usbhid->outhead].report = report;
  372. usbhid->outhead = head;
  373. if (!test_and_set_bit(HID_OUT_RUNNING, &usbhid->iofl))
  374. if (hid_submit_out(hid))
  375. clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
  376. spin_unlock_irqrestore(&usbhid->outlock, flags);
  377. return;
  378. }
  379. spin_lock_irqsave(&usbhid->ctrllock, flags);
  380. if ((head = (usbhid->ctrlhead + 1) & (HID_CONTROL_FIFO_SIZE - 1)) == usbhid->ctrltail) {
  381. spin_unlock_irqrestore(&usbhid->ctrllock, flags);
  382. dev_warn(&hid->dev, "control queue full\n");
  383. return;
  384. }
  385. if (dir == USB_DIR_OUT) {
  386. usbhid->ctrl[usbhid->ctrlhead].raw_report = kmalloc(len, GFP_ATOMIC);
  387. if (!usbhid->ctrl[usbhid->ctrlhead].raw_report) {
  388. spin_unlock_irqrestore(&usbhid->ctrllock, flags);
  389. dev_warn(&hid->dev, "control queueing failed\n");
  390. return;
  391. }
  392. hid_output_report(report, usbhid->ctrl[usbhid->ctrlhead].raw_report);
  393. }
  394. usbhid->ctrl[usbhid->ctrlhead].report = report;
  395. usbhid->ctrl[usbhid->ctrlhead].dir = dir;
  396. usbhid->ctrlhead = head;
  397. if (!test_and_set_bit(HID_CTRL_RUNNING, &usbhid->iofl))
  398. if (hid_submit_ctrl(hid))
  399. clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
  400. spin_unlock_irqrestore(&usbhid->ctrllock, flags);
  401. }
  402. EXPORT_SYMBOL_GPL(usbhid_submit_report);
  403. static int usb_hidinput_input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
  404. {
  405. struct hid_device *hid = input_get_drvdata(dev);
  406. struct hid_field *field;
  407. int offset;
  408. if (type == EV_FF)
  409. return input_ff_event(dev, type, code, value);
  410. if (type != EV_LED)
  411. return -1;
  412. if ((offset = hidinput_find_field(hid, type, code, &field)) == -1) {
  413. dev_warn(&dev->dev, "event field not found\n");
  414. return -1;
  415. }
  416. hid_set_field(field, offset, value);
  417. usbhid_submit_report(hid, field->report, USB_DIR_OUT);
  418. return 0;
  419. }
  420. int usbhid_wait_io(struct hid_device *hid)
  421. {
  422. struct usbhid_device *usbhid = hid->driver_data;
  423. if (!wait_event_timeout(usbhid->wait,
  424. (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl) &&
  425. !test_bit(HID_OUT_RUNNING, &usbhid->iofl)),
  426. 10*HZ)) {
  427. dbg_hid("timeout waiting for ctrl or out queue to clear\n");
  428. return -1;
  429. }
  430. return 0;
  431. }
  432. static int hid_set_idle(struct usb_device *dev, int ifnum, int report, int idle)
  433. {
  434. return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  435. HID_REQ_SET_IDLE, USB_TYPE_CLASS | USB_RECIP_INTERFACE, (idle << 8) | report,
  436. ifnum, NULL, 0, USB_CTRL_SET_TIMEOUT);
  437. }
  438. static int hid_get_class_descriptor(struct usb_device *dev, int ifnum,
  439. unsigned char type, void *buf, int size)
  440. {
  441. int result, retries = 4;
  442. memset(buf, 0, size);
  443. do {
  444. result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  445. USB_REQ_GET_DESCRIPTOR, USB_RECIP_INTERFACE | USB_DIR_IN,
  446. (type << 8), ifnum, buf, size, USB_CTRL_GET_TIMEOUT);
  447. retries--;
  448. } while (result < size && retries);
  449. return result;
  450. }
  451. int usbhid_open(struct hid_device *hid)
  452. {
  453. struct usbhid_device *usbhid = hid->driver_data;
  454. int res;
  455. if (!hid->open++) {
  456. res = usb_autopm_get_interface(usbhid->intf);
  457. if (res < 0) {
  458. hid->open--;
  459. return -EIO;
  460. }
  461. }
  462. if (hid_start_in(hid))
  463. hid_io_error(hid);
  464. return 0;
  465. }
  466. void usbhid_close(struct hid_device *hid)
  467. {
  468. struct usbhid_device *usbhid = hid->driver_data;
  469. if (!--hid->open) {
  470. usb_kill_urb(usbhid->urbin);
  471. usb_autopm_put_interface(usbhid->intf);
  472. }
  473. }
  474. /*
  475. * Initialize all reports
  476. */
  477. void usbhid_init_reports(struct hid_device *hid)
  478. {
  479. struct hid_report *report;
  480. struct usbhid_device *usbhid = hid->driver_data;
  481. int err, ret;
  482. list_for_each_entry(report, &hid->report_enum[HID_INPUT_REPORT].report_list, list)
  483. usbhid_submit_report(hid, report, USB_DIR_IN);
  484. list_for_each_entry(report, &hid->report_enum[HID_FEATURE_REPORT].report_list, list)
  485. usbhid_submit_report(hid, report, USB_DIR_IN);
  486. err = 0;
  487. ret = usbhid_wait_io(hid);
  488. while (ret) {
  489. err |= ret;
  490. if (test_bit(HID_CTRL_RUNNING, &usbhid->iofl))
  491. usb_kill_urb(usbhid->urbctrl);
  492. if (test_bit(HID_OUT_RUNNING, &usbhid->iofl))
  493. usb_kill_urb(usbhid->urbout);
  494. ret = usbhid_wait_io(hid);
  495. }
  496. if (err)
  497. dev_warn(&hid->dev, "timeout initializing reports\n");
  498. }
  499. /*
  500. * Reset LEDs which BIOS might have left on. For now, just NumLock (0x01).
  501. */
  502. static int hid_find_field_early(struct hid_device *hid, unsigned int page,
  503. unsigned int hid_code, struct hid_field **pfield)
  504. {
  505. struct hid_report *report;
  506. struct hid_field *field;
  507. struct hid_usage *usage;
  508. int i, j;
  509. list_for_each_entry(report, &hid->report_enum[HID_OUTPUT_REPORT].report_list, list) {
  510. for (i = 0; i < report->maxfield; i++) {
  511. field = report->field[i];
  512. for (j = 0; j < field->maxusage; j++) {
  513. usage = &field->usage[j];
  514. if ((usage->hid & HID_USAGE_PAGE) == page &&
  515. (usage->hid & 0xFFFF) == hid_code) {
  516. *pfield = field;
  517. return j;
  518. }
  519. }
  520. }
  521. }
  522. return -1;
  523. }
  524. void usbhid_set_leds(struct hid_device *hid)
  525. {
  526. struct hid_field *field;
  527. int offset;
  528. if ((offset = hid_find_field_early(hid, HID_UP_LED, 0x01, &field)) != -1) {
  529. hid_set_field(field, offset, 0);
  530. usbhid_submit_report(hid, field->report, USB_DIR_OUT);
  531. }
  532. }
  533. EXPORT_SYMBOL_GPL(usbhid_set_leds);
  534. /*
  535. * Traverse the supplied list of reports and find the longest
  536. */
  537. static void hid_find_max_report(struct hid_device *hid, unsigned int type,
  538. unsigned int *max)
  539. {
  540. struct hid_report *report;
  541. unsigned int size;
  542. list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
  543. size = ((report->size - 1) >> 3) + 1 + hid->report_enum[type].numbered;
  544. if (*max < size)
  545. *max = size;
  546. }
  547. }
  548. static int hid_alloc_buffers(struct usb_device *dev, struct hid_device *hid)
  549. {
  550. struct usbhid_device *usbhid = hid->driver_data;
  551. usbhid->inbuf = usb_buffer_alloc(dev, usbhid->bufsize, GFP_KERNEL,
  552. &usbhid->inbuf_dma);
  553. usbhid->outbuf = usb_buffer_alloc(dev, usbhid->bufsize, GFP_KERNEL,
  554. &usbhid->outbuf_dma);
  555. usbhid->cr = usb_buffer_alloc(dev, sizeof(*usbhid->cr), GFP_KERNEL,
  556. &usbhid->cr_dma);
  557. usbhid->ctrlbuf = usb_buffer_alloc(dev, usbhid->bufsize, GFP_KERNEL,
  558. &usbhid->ctrlbuf_dma);
  559. if (!usbhid->inbuf || !usbhid->outbuf || !usbhid->cr ||
  560. !usbhid->ctrlbuf)
  561. return -1;
  562. return 0;
  563. }
  564. static int usbhid_output_raw_report(struct hid_device *hid, __u8 *buf, size_t count)
  565. {
  566. struct usbhid_device *usbhid = hid->driver_data;
  567. struct usb_device *dev = hid_to_usb_dev(hid);
  568. struct usb_interface *intf = usbhid->intf;
  569. struct usb_host_interface *interface = intf->cur_altsetting;
  570. int ret;
  571. ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  572. HID_REQ_SET_REPORT,
  573. USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  574. ((HID_OUTPUT_REPORT + 1) << 8) | *buf,
  575. interface->desc.bInterfaceNumber, buf + 1, count - 1,
  576. USB_CTRL_SET_TIMEOUT);
  577. /* count also the report id */
  578. if (ret > 0)
  579. ret++;
  580. return ret;
  581. }
  582. static void hid_free_buffers(struct usb_device *dev, struct hid_device *hid)
  583. {
  584. struct usbhid_device *usbhid = hid->driver_data;
  585. usb_buffer_free(dev, usbhid->bufsize, usbhid->inbuf, usbhid->inbuf_dma);
  586. usb_buffer_free(dev, usbhid->bufsize, usbhid->outbuf, usbhid->outbuf_dma);
  587. usb_buffer_free(dev, sizeof(*(usbhid->cr)), usbhid->cr, usbhid->cr_dma);
  588. usb_buffer_free(dev, usbhid->bufsize, usbhid->ctrlbuf, usbhid->ctrlbuf_dma);
  589. }
  590. static int usbhid_parse(struct hid_device *hid)
  591. {
  592. struct usb_interface *intf = to_usb_interface(hid->dev.parent);
  593. struct usb_host_interface *interface = intf->cur_altsetting;
  594. struct usb_device *dev = interface_to_usbdev (intf);
  595. struct hid_descriptor *hdesc;
  596. u32 quirks = 0;
  597. unsigned int rsize = 0;
  598. char *rdesc;
  599. int ret, n;
  600. quirks = usbhid_lookup_quirk(le16_to_cpu(dev->descriptor.idVendor),
  601. le16_to_cpu(dev->descriptor.idProduct));
  602. /* Many keyboards and mice don't like to be polled for reports,
  603. * so we will always set the HID_QUIRK_NOGET flag for them. */
  604. if (interface->desc.bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT) {
  605. if (interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_KEYBOARD ||
  606. interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_MOUSE)
  607. quirks |= HID_QUIRK_NOGET;
  608. }
  609. if (usb_get_extra_descriptor(interface, HID_DT_HID, &hdesc) &&
  610. (!interface->desc.bNumEndpoints ||
  611. usb_get_extra_descriptor(&interface->endpoint[0], HID_DT_HID, &hdesc))) {
  612. dbg_hid("class descriptor not present\n");
  613. return -ENODEV;
  614. }
  615. hid->version = le16_to_cpu(hdesc->bcdHID);
  616. hid->country = hdesc->bCountryCode;
  617. for (n = 0; n < hdesc->bNumDescriptors; n++)
  618. if (hdesc->desc[n].bDescriptorType == HID_DT_REPORT)
  619. rsize = le16_to_cpu(hdesc->desc[n].wDescriptorLength);
  620. if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
  621. dbg_hid("weird size of report descriptor (%u)\n", rsize);
  622. return -EINVAL;
  623. }
  624. if (!(rdesc = kmalloc(rsize, GFP_KERNEL))) {
  625. dbg_hid("couldn't allocate rdesc memory\n");
  626. return -ENOMEM;
  627. }
  628. hid_set_idle(dev, interface->desc.bInterfaceNumber, 0, 0);
  629. ret = hid_get_class_descriptor(dev, interface->desc.bInterfaceNumber,
  630. HID_DT_REPORT, rdesc, rsize);
  631. if (ret < 0) {
  632. dbg_hid("reading report descriptor failed\n");
  633. kfree(rdesc);
  634. goto err;
  635. }
  636. dbg_hid("report descriptor (size %u, read %d) = ", rsize, n);
  637. for (n = 0; n < rsize; n++)
  638. dbg_hid_line(" %02x", (unsigned char) rdesc[n]);
  639. dbg_hid_line("\n");
  640. ret = hid_parse_report(hid, rdesc, rsize);
  641. kfree(rdesc);
  642. if (ret) {
  643. dbg_hid("parsing report descriptor failed\n");
  644. goto err;
  645. }
  646. hid->quirks = quirks;
  647. return 0;
  648. err:
  649. return ret;
  650. }
  651. static int usbhid_start(struct hid_device *hid)
  652. {
  653. struct usb_interface *intf = to_usb_interface(hid->dev.parent);
  654. struct usb_host_interface *interface = intf->cur_altsetting;
  655. struct usb_device *dev = interface_to_usbdev(intf);
  656. struct usbhid_device *usbhid = hid->driver_data;
  657. unsigned int n, insize = 0;
  658. int ret;
  659. clear_bit(HID_DISCONNECTED, &usbhid->iofl);
  660. usbhid->bufsize = HID_MIN_BUFFER_SIZE;
  661. hid_find_max_report(hid, HID_INPUT_REPORT, &usbhid->bufsize);
  662. hid_find_max_report(hid, HID_OUTPUT_REPORT, &usbhid->bufsize);
  663. hid_find_max_report(hid, HID_FEATURE_REPORT, &usbhid->bufsize);
  664. if (usbhid->bufsize > HID_MAX_BUFFER_SIZE)
  665. usbhid->bufsize = HID_MAX_BUFFER_SIZE;
  666. hid_find_max_report(hid, HID_INPUT_REPORT, &insize);
  667. if (insize > HID_MAX_BUFFER_SIZE)
  668. insize = HID_MAX_BUFFER_SIZE;
  669. if (hid_alloc_buffers(dev, hid)) {
  670. ret = -ENOMEM;
  671. goto fail;
  672. }
  673. for (n = 0; n < interface->desc.bNumEndpoints; n++) {
  674. struct usb_endpoint_descriptor *endpoint;
  675. int pipe;
  676. int interval;
  677. endpoint = &interface->endpoint[n].desc;
  678. if (!usb_endpoint_xfer_int(endpoint))
  679. continue;
  680. interval = endpoint->bInterval;
  681. /* Some vendors give fullspeed interval on highspeed devides */
  682. if (hid->quirks & HID_QUIRK_FULLSPEED_INTERVAL &&
  683. dev->speed == USB_SPEED_HIGH) {
  684. interval = fls(endpoint->bInterval*8);
  685. printk(KERN_INFO "%s: Fixing fullspeed to highspeed interval: %d -> %d\n",
  686. hid->name, endpoint->bInterval, interval);
  687. }
  688. /* Change the polling interval of mice. */
  689. if (hid->collection->usage == HID_GD_MOUSE && hid_mousepoll_interval > 0)
  690. interval = hid_mousepoll_interval;
  691. ret = -ENOMEM;
  692. if (usb_endpoint_dir_in(endpoint)) {
  693. if (usbhid->urbin)
  694. continue;
  695. if (!(usbhid->urbin = usb_alloc_urb(0, GFP_KERNEL)))
  696. goto fail;
  697. pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
  698. usb_fill_int_urb(usbhid->urbin, dev, pipe, usbhid->inbuf, insize,
  699. hid_irq_in, hid, interval);
  700. usbhid->urbin->transfer_dma = usbhid->inbuf_dma;
  701. usbhid->urbin->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  702. } else {
  703. if (usbhid->urbout)
  704. continue;
  705. if (!(usbhid->urbout = usb_alloc_urb(0, GFP_KERNEL)))
  706. goto fail;
  707. pipe = usb_sndintpipe(dev, endpoint->bEndpointAddress);
  708. usb_fill_int_urb(usbhid->urbout, dev, pipe, usbhid->outbuf, 0,
  709. hid_irq_out, hid, interval);
  710. usbhid->urbout->transfer_dma = usbhid->outbuf_dma;
  711. usbhid->urbout->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  712. }
  713. }
  714. init_waitqueue_head(&usbhid->wait);
  715. INIT_WORK(&usbhid->reset_work, hid_reset);
  716. setup_timer(&usbhid->io_retry, hid_retry_timeout, (unsigned long) hid);
  717. spin_lock_init(&usbhid->inlock);
  718. spin_lock_init(&usbhid->outlock);
  719. spin_lock_init(&usbhid->ctrllock);
  720. usbhid->intf = intf;
  721. usbhid->ifnum = interface->desc.bInterfaceNumber;
  722. usbhid->urbctrl = usb_alloc_urb(0, GFP_KERNEL);
  723. if (!usbhid->urbctrl) {
  724. ret = -ENOMEM;
  725. goto fail;
  726. }
  727. usb_fill_control_urb(usbhid->urbctrl, dev, 0, (void *) usbhid->cr,
  728. usbhid->ctrlbuf, 1, hid_ctrl, hid);
  729. usbhid->urbctrl->setup_dma = usbhid->cr_dma;
  730. usbhid->urbctrl->transfer_dma = usbhid->ctrlbuf_dma;
  731. usbhid->urbctrl->transfer_flags |= (URB_NO_TRANSFER_DMA_MAP | URB_NO_SETUP_DMA_MAP);
  732. usbhid_init_reports(hid);
  733. hid_dump_device(hid);
  734. set_bit(HID_STARTED, &usbhid->iofl);
  735. /* Some keyboards don't work until their LEDs have been set.
  736. * Since BIOSes do set the LEDs, it must be safe for any device
  737. * that supports the keyboard boot protocol.
  738. */
  739. if (interface->desc.bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT &&
  740. interface->desc.bInterfaceProtocol ==
  741. USB_INTERFACE_PROTOCOL_KEYBOARD)
  742. usbhid_set_leds(hid);
  743. return 0;
  744. fail:
  745. usb_free_urb(usbhid->urbin);
  746. usb_free_urb(usbhid->urbout);
  747. usb_free_urb(usbhid->urbctrl);
  748. usbhid->urbin = NULL;
  749. usbhid->urbout = NULL;
  750. usbhid->urbctrl = NULL;
  751. hid_free_buffers(dev, hid);
  752. return ret;
  753. }
  754. static void usbhid_stop(struct hid_device *hid)
  755. {
  756. struct usbhid_device *usbhid = hid->driver_data;
  757. if (WARN_ON(!usbhid))
  758. return;
  759. clear_bit(HID_STARTED, &usbhid->iofl);
  760. spin_lock_irq(&usbhid->inlock); /* Sync with error handler */
  761. set_bit(HID_DISCONNECTED, &usbhid->iofl);
  762. spin_unlock_irq(&usbhid->inlock);
  763. usb_kill_urb(usbhid->urbin);
  764. usb_kill_urb(usbhid->urbout);
  765. usb_kill_urb(usbhid->urbctrl);
  766. del_timer_sync(&usbhid->io_retry);
  767. cancel_work_sync(&usbhid->reset_work);
  768. if (hid->claimed & HID_CLAIMED_INPUT)
  769. hidinput_disconnect(hid);
  770. if (hid->claimed & HID_CLAIMED_HIDDEV)
  771. hiddev_disconnect(hid);
  772. if (hid->claimed & HID_CLAIMED_HIDRAW)
  773. hidraw_disconnect(hid);
  774. hid->claimed = 0;
  775. usb_free_urb(usbhid->urbin);
  776. usb_free_urb(usbhid->urbctrl);
  777. usb_free_urb(usbhid->urbout);
  778. usbhid->urbin = NULL; /* don't mess up next start */
  779. usbhid->urbctrl = NULL;
  780. usbhid->urbout = NULL;
  781. hid_free_buffers(hid_to_usb_dev(hid), hid);
  782. }
  783. static struct hid_ll_driver usb_hid_driver = {
  784. .parse = usbhid_parse,
  785. .start = usbhid_start,
  786. .stop = usbhid_stop,
  787. .open = usbhid_open,
  788. .close = usbhid_close,
  789. .hidinput_input_event = usb_hidinput_input_event,
  790. };
  791. static int hid_probe(struct usb_interface *intf, const struct usb_device_id *id)
  792. {
  793. struct usb_host_interface *interface = intf->cur_altsetting;
  794. struct usb_device *dev = interface_to_usbdev(intf);
  795. struct usbhid_device *usbhid;
  796. struct hid_device *hid;
  797. unsigned int n, has_in = 0;
  798. size_t len;
  799. int ret;
  800. dbg_hid("HID probe called for ifnum %d\n",
  801. intf->altsetting->desc.bInterfaceNumber);
  802. for (n = 0; n < interface->desc.bNumEndpoints; n++)
  803. if (usb_endpoint_is_int_in(&interface->endpoint[n].desc))
  804. has_in++;
  805. if (!has_in) {
  806. dev_err(&intf->dev, "couldn't find an input interrupt "
  807. "endpoint\n");
  808. return -ENODEV;
  809. }
  810. hid = hid_allocate_device();
  811. if (IS_ERR(hid))
  812. return PTR_ERR(hid);
  813. usb_set_intfdata(intf, hid);
  814. hid->ll_driver = &usb_hid_driver;
  815. hid->hid_output_raw_report = usbhid_output_raw_report;
  816. hid->ff_init = hid_pidff_init;
  817. #ifdef CONFIG_USB_HIDDEV
  818. hid->hiddev_connect = hiddev_connect;
  819. hid->hiddev_hid_event = hiddev_hid_event;
  820. hid->hiddev_report_event = hiddev_report_event;
  821. #endif
  822. hid->dev.parent = &intf->dev;
  823. hid->bus = BUS_USB;
  824. hid->vendor = le16_to_cpu(dev->descriptor.idVendor);
  825. hid->product = le16_to_cpu(dev->descriptor.idProduct);
  826. hid->name[0] = 0;
  827. if (intf->cur_altsetting->desc.bInterfaceProtocol ==
  828. USB_INTERFACE_PROTOCOL_MOUSE)
  829. hid->type = HID_TYPE_USBMOUSE;
  830. if (dev->manufacturer)
  831. strlcpy(hid->name, dev->manufacturer, sizeof(hid->name));
  832. if (dev->product) {
  833. if (dev->manufacturer)
  834. strlcat(hid->name, " ", sizeof(hid->name));
  835. strlcat(hid->name, dev->product, sizeof(hid->name));
  836. }
  837. if (!strlen(hid->name))
  838. snprintf(hid->name, sizeof(hid->name), "HID %04x:%04x",
  839. le16_to_cpu(dev->descriptor.idVendor),
  840. le16_to_cpu(dev->descriptor.idProduct));
  841. usb_make_path(dev, hid->phys, sizeof(hid->phys));
  842. strlcat(hid->phys, "/input", sizeof(hid->phys));
  843. len = strlen(hid->phys);
  844. if (len < sizeof(hid->phys) - 1)
  845. snprintf(hid->phys + len, sizeof(hid->phys) - len,
  846. "%d", intf->altsetting[0].desc.bInterfaceNumber);
  847. if (usb_string(dev, dev->descriptor.iSerialNumber, hid->uniq, 64) <= 0)
  848. hid->uniq[0] = 0;
  849. usbhid = kzalloc(sizeof(*usbhid), GFP_KERNEL);
  850. if (usbhid == NULL) {
  851. ret = -ENOMEM;
  852. goto err;
  853. }
  854. hid->driver_data = usbhid;
  855. usbhid->hid = hid;
  856. ret = hid_add_device(hid);
  857. if (ret) {
  858. if (ret != -ENODEV)
  859. dev_err(&intf->dev, "can't add hid device: %d\n", ret);
  860. goto err_free;
  861. }
  862. return 0;
  863. err_free:
  864. kfree(usbhid);
  865. err:
  866. hid_destroy_device(hid);
  867. return ret;
  868. }
  869. static void hid_disconnect(struct usb_interface *intf)
  870. {
  871. struct hid_device *hid = usb_get_intfdata(intf);
  872. struct usbhid_device *usbhid;
  873. if (WARN_ON(!hid))
  874. return;
  875. usbhid = hid->driver_data;
  876. hid_destroy_device(hid);
  877. kfree(usbhid);
  878. }
  879. static int hid_suspend(struct usb_interface *intf, pm_message_t message)
  880. {
  881. struct hid_device *hid = usb_get_intfdata (intf);
  882. struct usbhid_device *usbhid = hid->driver_data;
  883. if (!test_bit(HID_STARTED, &usbhid->iofl))
  884. return 0;
  885. spin_lock_irq(&usbhid->inlock); /* Sync with error handler */
  886. set_bit(HID_SUSPENDED, &usbhid->iofl);
  887. spin_unlock_irq(&usbhid->inlock);
  888. del_timer_sync(&usbhid->io_retry);
  889. usb_kill_urb(usbhid->urbin);
  890. dev_dbg(&intf->dev, "suspend\n");
  891. return 0;
  892. }
  893. static int hid_resume(struct usb_interface *intf)
  894. {
  895. struct hid_device *hid = usb_get_intfdata (intf);
  896. struct usbhid_device *usbhid = hid->driver_data;
  897. int status;
  898. if (!test_bit(HID_STARTED, &usbhid->iofl))
  899. return 0;
  900. clear_bit(HID_SUSPENDED, &usbhid->iofl);
  901. usbhid->retry_delay = 0;
  902. status = hid_start_in(hid);
  903. dev_dbg(&intf->dev, "resume status %d\n", status);
  904. return status;
  905. }
  906. /* Treat USB reset pretty much the same as suspend/resume */
  907. static int hid_pre_reset(struct usb_interface *intf)
  908. {
  909. /* FIXME: What if the interface is already suspended? */
  910. hid_suspend(intf, PMSG_ON);
  911. return 0;
  912. }
  913. /* Same routine used for post_reset and reset_resume */
  914. static int hid_post_reset(struct usb_interface *intf)
  915. {
  916. struct usb_device *dev = interface_to_usbdev (intf);
  917. hid_set_idle(dev, intf->cur_altsetting->desc.bInterfaceNumber, 0, 0);
  918. /* FIXME: Any more reinitialization needed? */
  919. return hid_resume(intf);
  920. }
  921. static struct usb_device_id hid_usb_ids [] = {
  922. { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
  923. .bInterfaceClass = USB_INTERFACE_CLASS_HID },
  924. { } /* Terminating entry */
  925. };
  926. MODULE_DEVICE_TABLE (usb, hid_usb_ids);
  927. static struct usb_driver hid_driver = {
  928. .name = "usbhid",
  929. .probe = hid_probe,
  930. .disconnect = hid_disconnect,
  931. .suspend = hid_suspend,
  932. .resume = hid_resume,
  933. .reset_resume = hid_post_reset,
  934. .pre_reset = hid_pre_reset,
  935. .post_reset = hid_post_reset,
  936. .id_table = hid_usb_ids,
  937. .supports_autosuspend = 1,
  938. };
  939. static const struct hid_device_id hid_usb_table[] = {
  940. { HID_USB_DEVICE(HID_ANY_ID, HID_ANY_ID) },
  941. { }
  942. };
  943. static struct hid_driver hid_usb_driver = {
  944. .name = "generic-usb",
  945. .id_table = hid_usb_table,
  946. };
  947. static int __init hid_init(void)
  948. {
  949. int retval;
  950. retval = hid_register_driver(&hid_usb_driver);
  951. if (retval)
  952. goto hid_register_fail;
  953. retval = usbhid_quirks_init(quirks_param);
  954. if (retval)
  955. goto usbhid_quirks_init_fail;
  956. retval = hiddev_init();
  957. if (retval)
  958. goto hiddev_init_fail;
  959. retval = usb_register(&hid_driver);
  960. if (retval)
  961. goto usb_register_fail;
  962. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
  963. DRIVER_DESC "\n");
  964. return 0;
  965. usb_register_fail:
  966. hiddev_exit();
  967. hiddev_init_fail:
  968. usbhid_quirks_exit();
  969. usbhid_quirks_init_fail:
  970. hid_unregister_driver(&hid_usb_driver);
  971. hid_register_fail:
  972. return retval;
  973. }
  974. static void __exit hid_exit(void)
  975. {
  976. usb_deregister(&hid_driver);
  977. hiddev_exit();
  978. usbhid_quirks_exit();
  979. hid_unregister_driver(&hid_usb_driver);
  980. }
  981. module_init(hid_init);
  982. module_exit(hid_exit);
  983. MODULE_AUTHOR(DRIVER_AUTHOR);
  984. MODULE_DESCRIPTION(DRIVER_DESC);
  985. MODULE_LICENSE(DRIVER_LICENSE);