hid-core.c 32 KB

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