hid-core.c 33 KB

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