hid-core.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535
  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) 2007-2008 Oliver Neukum
  8. * Copyright (c) 2006-2010 Jiri Kosina
  9. */
  10. /*
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms of the GNU General Public License as published by the Free
  13. * Software Foundation; either version 2 of the License, or (at your option)
  14. * any later version.
  15. */
  16. #include <linux/module.h>
  17. #include <linux/slab.h>
  18. #include <linux/init.h>
  19. #include <linux/kernel.h>
  20. #include <linux/list.h>
  21. #include <linux/mm.h>
  22. #include <linux/mutex.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/workqueue.h>
  29. #include <linux/usb.h>
  30. #include <linux/hid.h>
  31. #include <linux/hiddev.h>
  32. #include <linux/hid-debug.h>
  33. #include <linux/hidraw.h>
  34. #include "usbhid.h"
  35. /*
  36. * Version Information
  37. */
  38. #define DRIVER_DESC "USB HID core driver"
  39. #define DRIVER_LICENSE "GPL"
  40. /*
  41. * Module parameters.
  42. */
  43. static unsigned int hid_mousepoll_interval;
  44. module_param_named(mousepoll, hid_mousepoll_interval, uint, 0644);
  45. MODULE_PARM_DESC(mousepoll, "Polling interval of mice");
  46. static unsigned int ignoreled;
  47. module_param_named(ignoreled, ignoreled, uint, 0644);
  48. MODULE_PARM_DESC(ignoreled, "Autosuspend with active leds");
  49. /* Quirks specified at module load time */
  50. static char *quirks_param[MAX_USBHID_BOOT_QUIRKS] = { [ 0 ... (MAX_USBHID_BOOT_QUIRKS - 1) ] = NULL };
  51. module_param_array_named(quirks, quirks_param, charp, NULL, 0444);
  52. MODULE_PARM_DESC(quirks, "Add/modify USB HID quirks by specifying "
  53. " quirks=vendorID:productID:quirks"
  54. " where vendorID, productID, and quirks are all in"
  55. " 0x-prefixed hex");
  56. /*
  57. * Input submission and I/O error handler.
  58. */
  59. static DEFINE_MUTEX(hid_open_mut);
  60. static void hid_io_error(struct hid_device *hid);
  61. static int hid_submit_out(struct hid_device *hid);
  62. static int hid_submit_ctrl(struct hid_device *hid);
  63. static void hid_cancel_delayed_stuff(struct usbhid_device *usbhid);
  64. /* Start up the input URB */
  65. static int hid_start_in(struct hid_device *hid)
  66. {
  67. unsigned long flags;
  68. int rc = 0;
  69. struct usbhid_device *usbhid = hid->driver_data;
  70. spin_lock_irqsave(&usbhid->lock, flags);
  71. if (hid->open > 0 &&
  72. !test_bit(HID_DISCONNECTED, &usbhid->iofl) &&
  73. !test_bit(HID_REPORTED_IDLE, &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->lock, 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 = 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 = usb_lock_device_for_reset(hid_to_usb_dev(hid), usbhid->intf);
  107. if (rc == 0) {
  108. rc = usb_reset_device(hid_to_usb_dev(hid));
  109. usb_unlock_device(hid_to_usb_dev(hid));
  110. }
  111. clear_bit(HID_RESET_PENDING, &usbhid->iofl);
  112. }
  113. switch (rc) {
  114. case 0:
  115. if (!test_bit(HID_IN_RUNNING, &usbhid->iofl))
  116. hid_io_error(hid);
  117. break;
  118. default:
  119. hid_err(hid, "can't reset device, %s-%s/input%d, status %d\n",
  120. hid_to_usb_dev(hid)->bus->bus_name,
  121. hid_to_usb_dev(hid)->devpath,
  122. usbhid->ifnum, rc);
  123. /* FALLTHROUGH */
  124. case -EHOSTUNREACH:
  125. case -ENODEV:
  126. case -EINTR:
  127. break;
  128. }
  129. }
  130. /* Main I/O error handler */
  131. static void hid_io_error(struct hid_device *hid)
  132. {
  133. unsigned long flags;
  134. struct usbhid_device *usbhid = hid->driver_data;
  135. spin_lock_irqsave(&usbhid->lock, flags);
  136. /* Stop when disconnected */
  137. if (test_bit(HID_DISCONNECTED, &usbhid->iofl))
  138. goto done;
  139. /* If it has been a while since the last error, we'll assume
  140. * this a brand new error and reset the retry timeout. */
  141. if (time_after(jiffies, usbhid->stop_retry + HZ/2))
  142. usbhid->retry_delay = 0;
  143. /* When an error occurs, retry at increasing intervals */
  144. if (usbhid->retry_delay == 0) {
  145. usbhid->retry_delay = 13; /* Then 26, 52, 104, 104, ... */
  146. usbhid->stop_retry = jiffies + msecs_to_jiffies(1000);
  147. } else if (usbhid->retry_delay < 100)
  148. usbhid->retry_delay *= 2;
  149. if (time_after(jiffies, usbhid->stop_retry)) {
  150. /* Retries failed, so do a port reset */
  151. if (!test_and_set_bit(HID_RESET_PENDING, &usbhid->iofl)) {
  152. schedule_work(&usbhid->reset_work);
  153. goto done;
  154. }
  155. }
  156. mod_timer(&usbhid->io_retry,
  157. jiffies + msecs_to_jiffies(usbhid->retry_delay));
  158. done:
  159. spin_unlock_irqrestore(&usbhid->lock, flags);
  160. }
  161. static void usbhid_mark_busy(struct usbhid_device *usbhid)
  162. {
  163. struct usb_interface *intf = usbhid->intf;
  164. usb_mark_last_busy(interface_to_usbdev(intf));
  165. }
  166. static int usbhid_restart_out_queue(struct usbhid_device *usbhid)
  167. {
  168. struct hid_device *hid = usb_get_intfdata(usbhid->intf);
  169. int kicked;
  170. int r;
  171. if (!hid)
  172. return 0;
  173. if ((kicked = (usbhid->outhead != usbhid->outtail))) {
  174. dbg("Kicking head %d tail %d", usbhid->outhead, usbhid->outtail);
  175. r = usb_autopm_get_interface_async(usbhid->intf);
  176. if (r < 0)
  177. return r;
  178. /* Asynchronously flush queue. */
  179. set_bit(HID_OUT_RUNNING, &usbhid->iofl);
  180. if (hid_submit_out(hid)) {
  181. clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
  182. usb_autopm_put_interface_async(usbhid->intf);
  183. }
  184. wake_up(&usbhid->wait);
  185. }
  186. return kicked;
  187. }
  188. static int usbhid_restart_ctrl_queue(struct usbhid_device *usbhid)
  189. {
  190. struct hid_device *hid = usb_get_intfdata(usbhid->intf);
  191. int kicked;
  192. int r;
  193. WARN_ON(hid == NULL);
  194. if (!hid)
  195. return 0;
  196. if ((kicked = (usbhid->ctrlhead != usbhid->ctrltail))) {
  197. dbg("Kicking head %d tail %d", usbhid->ctrlhead, usbhid->ctrltail);
  198. r = usb_autopm_get_interface_async(usbhid->intf);
  199. if (r < 0)
  200. return r;
  201. /* Asynchronously flush queue. */
  202. set_bit(HID_CTRL_RUNNING, &usbhid->iofl);
  203. if (hid_submit_ctrl(hid)) {
  204. clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
  205. usb_autopm_put_interface_async(usbhid->intf);
  206. }
  207. wake_up(&usbhid->wait);
  208. }
  209. return kicked;
  210. }
  211. /*
  212. * Input interrupt completion handler.
  213. */
  214. static void hid_irq_in(struct urb *urb)
  215. {
  216. struct hid_device *hid = urb->context;
  217. struct usbhid_device *usbhid = hid->driver_data;
  218. int status;
  219. switch (urb->status) {
  220. case 0: /* success */
  221. usbhid_mark_busy(usbhid);
  222. usbhid->retry_delay = 0;
  223. hid_input_report(urb->context, HID_INPUT_REPORT,
  224. urb->transfer_buffer,
  225. urb->actual_length, 1);
  226. /*
  227. * autosuspend refused while keys are pressed
  228. * because most keyboards don't wake up when
  229. * a key is released
  230. */
  231. if (hid_check_keys_pressed(hid))
  232. set_bit(HID_KEYS_PRESSED, &usbhid->iofl);
  233. else
  234. clear_bit(HID_KEYS_PRESSED, &usbhid->iofl);
  235. break;
  236. case -EPIPE: /* stall */
  237. usbhid_mark_busy(usbhid);
  238. clear_bit(HID_IN_RUNNING, &usbhid->iofl);
  239. set_bit(HID_CLEAR_HALT, &usbhid->iofl);
  240. schedule_work(&usbhid->reset_work);
  241. return;
  242. case -ECONNRESET: /* unlink */
  243. case -ENOENT:
  244. case -ESHUTDOWN: /* unplug */
  245. clear_bit(HID_IN_RUNNING, &usbhid->iofl);
  246. return;
  247. case -EILSEQ: /* protocol error or unplug */
  248. case -EPROTO: /* protocol error or unplug */
  249. case -ETIME: /* protocol error or unplug */
  250. case -ETIMEDOUT: /* Should never happen, but... */
  251. usbhid_mark_busy(usbhid);
  252. clear_bit(HID_IN_RUNNING, &usbhid->iofl);
  253. hid_io_error(hid);
  254. return;
  255. default: /* error */
  256. hid_warn(urb->dev, "input irq status %d received\n",
  257. urb->status);
  258. }
  259. status = usb_submit_urb(urb, GFP_ATOMIC);
  260. if (status) {
  261. clear_bit(HID_IN_RUNNING, &usbhid->iofl);
  262. if (status != -EPERM) {
  263. hid_err(hid, "can't resubmit intr, %s-%s/input%d, status %d\n",
  264. hid_to_usb_dev(hid)->bus->bus_name,
  265. hid_to_usb_dev(hid)->devpath,
  266. usbhid->ifnum, status);
  267. hid_io_error(hid);
  268. }
  269. }
  270. }
  271. static int hid_submit_out(struct hid_device *hid)
  272. {
  273. struct hid_report *report;
  274. char *raw_report;
  275. struct usbhid_device *usbhid = hid->driver_data;
  276. int r;
  277. report = usbhid->out[usbhid->outtail].report;
  278. raw_report = usbhid->out[usbhid->outtail].raw_report;
  279. usbhid->urbout->transfer_buffer_length = ((report->size - 1) >> 3) +
  280. 1 + (report->id > 0);
  281. usbhid->urbout->dev = hid_to_usb_dev(hid);
  282. memcpy(usbhid->outbuf, raw_report,
  283. usbhid->urbout->transfer_buffer_length);
  284. kfree(raw_report);
  285. dbg_hid("submitting out urb\n");
  286. r = usb_submit_urb(usbhid->urbout, GFP_ATOMIC);
  287. if (r < 0) {
  288. hid_err(hid, "usb_submit_urb(out) failed: %d\n", r);
  289. return r;
  290. }
  291. usbhid->last_out = jiffies;
  292. return 0;
  293. }
  294. static int hid_submit_ctrl(struct hid_device *hid)
  295. {
  296. struct hid_report *report;
  297. unsigned char dir;
  298. char *raw_report;
  299. int len, r;
  300. struct usbhid_device *usbhid = hid->driver_data;
  301. report = usbhid->ctrl[usbhid->ctrltail].report;
  302. raw_report = usbhid->ctrl[usbhid->ctrltail].raw_report;
  303. dir = usbhid->ctrl[usbhid->ctrltail].dir;
  304. len = ((report->size - 1) >> 3) + 1 + (report->id > 0);
  305. if (dir == USB_DIR_OUT) {
  306. usbhid->urbctrl->pipe = usb_sndctrlpipe(hid_to_usb_dev(hid), 0);
  307. usbhid->urbctrl->transfer_buffer_length = len;
  308. memcpy(usbhid->ctrlbuf, raw_report, len);
  309. kfree(raw_report);
  310. } else {
  311. int maxpacket, padlen;
  312. usbhid->urbctrl->pipe = usb_rcvctrlpipe(hid_to_usb_dev(hid), 0);
  313. maxpacket = usb_maxpacket(hid_to_usb_dev(hid),
  314. usbhid->urbctrl->pipe, 0);
  315. if (maxpacket > 0) {
  316. padlen = DIV_ROUND_UP(len, maxpacket);
  317. padlen *= maxpacket;
  318. if (padlen > usbhid->bufsize)
  319. padlen = usbhid->bufsize;
  320. } else
  321. padlen = 0;
  322. usbhid->urbctrl->transfer_buffer_length = padlen;
  323. }
  324. usbhid->urbctrl->dev = hid_to_usb_dev(hid);
  325. usbhid->cr->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE | dir;
  326. usbhid->cr->bRequest = (dir == USB_DIR_OUT) ? HID_REQ_SET_REPORT :
  327. HID_REQ_GET_REPORT;
  328. usbhid->cr->wValue = cpu_to_le16(((report->type + 1) << 8) |
  329. report->id);
  330. usbhid->cr->wIndex = cpu_to_le16(usbhid->ifnum);
  331. usbhid->cr->wLength = cpu_to_le16(len);
  332. dbg_hid("submitting ctrl urb: %s wValue=0x%04x wIndex=0x%04x wLength=%u\n",
  333. usbhid->cr->bRequest == HID_REQ_SET_REPORT ? "Set_Report" :
  334. "Get_Report",
  335. usbhid->cr->wValue, usbhid->cr->wIndex, usbhid->cr->wLength);
  336. r = usb_submit_urb(usbhid->urbctrl, GFP_ATOMIC);
  337. if (r < 0) {
  338. hid_err(hid, "usb_submit_urb(ctrl) failed: %d\n", r);
  339. return r;
  340. }
  341. usbhid->last_ctrl = jiffies;
  342. return 0;
  343. }
  344. /*
  345. * Output interrupt completion handler.
  346. */
  347. static void hid_irq_out(struct urb *urb)
  348. {
  349. struct hid_device *hid = urb->context;
  350. struct usbhid_device *usbhid = hid->driver_data;
  351. unsigned long flags;
  352. int unplug = 0;
  353. switch (urb->status) {
  354. case 0: /* success */
  355. break;
  356. case -ESHUTDOWN: /* unplug */
  357. unplug = 1;
  358. case -EILSEQ: /* protocol error or unplug */
  359. case -EPROTO: /* protocol error or unplug */
  360. case -ECONNRESET: /* unlink */
  361. case -ENOENT:
  362. break;
  363. default: /* error */
  364. hid_warn(urb->dev, "output irq status %d received\n",
  365. urb->status);
  366. }
  367. spin_lock_irqsave(&usbhid->lock, flags);
  368. if (unplug)
  369. usbhid->outtail = usbhid->outhead;
  370. else
  371. usbhid->outtail = (usbhid->outtail + 1) & (HID_OUTPUT_FIFO_SIZE - 1);
  372. if (usbhid->outhead != usbhid->outtail && !hid_submit_out(hid)) {
  373. /* Successfully submitted next urb in queue */
  374. spin_unlock_irqrestore(&usbhid->lock, flags);
  375. return;
  376. }
  377. clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
  378. spin_unlock_irqrestore(&usbhid->lock, flags);
  379. usb_autopm_put_interface_async(usbhid->intf);
  380. wake_up(&usbhid->wait);
  381. }
  382. /*
  383. * Control pipe completion handler.
  384. */
  385. static void hid_ctrl(struct urb *urb)
  386. {
  387. struct hid_device *hid = urb->context;
  388. struct usbhid_device *usbhid = hid->driver_data;
  389. int unplug = 0, status = urb->status;
  390. spin_lock(&usbhid->lock);
  391. switch (status) {
  392. case 0: /* success */
  393. if (usbhid->ctrl[usbhid->ctrltail].dir == USB_DIR_IN)
  394. hid_input_report(urb->context,
  395. usbhid->ctrl[usbhid->ctrltail].report->type,
  396. urb->transfer_buffer, urb->actual_length, 0);
  397. break;
  398. case -ESHUTDOWN: /* unplug */
  399. unplug = 1;
  400. case -EILSEQ: /* protocol error or unplug */
  401. case -EPROTO: /* protocol error or unplug */
  402. case -ECONNRESET: /* unlink */
  403. case -ENOENT:
  404. case -EPIPE: /* report not available */
  405. break;
  406. default: /* error */
  407. hid_warn(urb->dev, "ctrl urb status %d received\n", status);
  408. }
  409. if (unplug)
  410. usbhid->ctrltail = usbhid->ctrlhead;
  411. else
  412. usbhid->ctrltail = (usbhid->ctrltail + 1) & (HID_CONTROL_FIFO_SIZE - 1);
  413. if (usbhid->ctrlhead != usbhid->ctrltail && !hid_submit_ctrl(hid)) {
  414. /* Successfully submitted next urb in queue */
  415. spin_unlock(&usbhid->lock);
  416. return;
  417. }
  418. clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
  419. spin_unlock(&usbhid->lock);
  420. usb_autopm_put_interface_async(usbhid->intf);
  421. wake_up(&usbhid->wait);
  422. }
  423. static void __usbhid_submit_report(struct hid_device *hid, struct hid_report *report,
  424. unsigned char dir)
  425. {
  426. int head;
  427. struct usbhid_device *usbhid = hid->driver_data;
  428. int len = ((report->size - 1) >> 3) + 1 + (report->id > 0);
  429. if ((hid->quirks & HID_QUIRK_NOGET) && dir == USB_DIR_IN)
  430. return;
  431. if (usbhid->urbout && dir == USB_DIR_OUT && report->type == HID_OUTPUT_REPORT) {
  432. if ((head = (usbhid->outhead + 1) & (HID_OUTPUT_FIFO_SIZE - 1)) == usbhid->outtail) {
  433. hid_warn(hid, "output queue full\n");
  434. return;
  435. }
  436. usbhid->out[usbhid->outhead].raw_report = kmalloc(len, GFP_ATOMIC);
  437. if (!usbhid->out[usbhid->outhead].raw_report) {
  438. hid_warn(hid, "output queueing failed\n");
  439. return;
  440. }
  441. hid_output_report(report, usbhid->out[usbhid->outhead].raw_report);
  442. usbhid->out[usbhid->outhead].report = report;
  443. usbhid->outhead = head;
  444. /* Try to awake from autosuspend... */
  445. if (usb_autopm_get_interface_async(usbhid->intf) < 0)
  446. return;
  447. /*
  448. * But if still suspended, leave urb enqueued, don't submit.
  449. * Submission will occur if/when resume() drains the queue.
  450. */
  451. if (test_bit(HID_REPORTED_IDLE, &usbhid->iofl))
  452. return;
  453. if (!test_and_set_bit(HID_OUT_RUNNING, &usbhid->iofl)) {
  454. if (hid_submit_out(hid)) {
  455. clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
  456. usb_autopm_put_interface_async(usbhid->intf);
  457. }
  458. wake_up(&usbhid->wait);
  459. } else {
  460. /*
  461. * the queue is known to run
  462. * but an earlier request may be stuck
  463. * we may need to time out
  464. * no race because this is called under
  465. * spinlock
  466. */
  467. if (time_after(jiffies, usbhid->last_out + HZ * 5))
  468. usb_unlink_urb(usbhid->urbout);
  469. }
  470. return;
  471. }
  472. if ((head = (usbhid->ctrlhead + 1) & (HID_CONTROL_FIFO_SIZE - 1)) == usbhid->ctrltail) {
  473. hid_warn(hid, "control queue full\n");
  474. return;
  475. }
  476. if (dir == USB_DIR_OUT) {
  477. usbhid->ctrl[usbhid->ctrlhead].raw_report = kmalloc(len, GFP_ATOMIC);
  478. if (!usbhid->ctrl[usbhid->ctrlhead].raw_report) {
  479. hid_warn(hid, "control queueing failed\n");
  480. return;
  481. }
  482. hid_output_report(report, usbhid->ctrl[usbhid->ctrlhead].raw_report);
  483. }
  484. usbhid->ctrl[usbhid->ctrlhead].report = report;
  485. usbhid->ctrl[usbhid->ctrlhead].dir = dir;
  486. usbhid->ctrlhead = head;
  487. /* Try to awake from autosuspend... */
  488. if (usb_autopm_get_interface_async(usbhid->intf) < 0)
  489. return;
  490. /*
  491. * If already suspended, leave urb enqueued, but don't submit.
  492. * Submission will occur if/when resume() drains the queue.
  493. */
  494. if (test_bit(HID_REPORTED_IDLE, &usbhid->iofl))
  495. return;
  496. if (!test_and_set_bit(HID_CTRL_RUNNING, &usbhid->iofl)) {
  497. if (hid_submit_ctrl(hid)) {
  498. clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
  499. usb_autopm_put_interface_async(usbhid->intf);
  500. }
  501. wake_up(&usbhid->wait);
  502. } else {
  503. /*
  504. * the queue is known to run
  505. * but an earlier request may be stuck
  506. * we may need to time out
  507. * no race because this is called under
  508. * spinlock
  509. */
  510. if (time_after(jiffies, usbhid->last_ctrl + HZ * 5))
  511. usb_unlink_urb(usbhid->urbctrl);
  512. }
  513. }
  514. void usbhid_submit_report(struct hid_device *hid, struct hid_report *report, unsigned char dir)
  515. {
  516. struct usbhid_device *usbhid = hid->driver_data;
  517. unsigned long flags;
  518. spin_lock_irqsave(&usbhid->lock, flags);
  519. __usbhid_submit_report(hid, report, dir);
  520. spin_unlock_irqrestore(&usbhid->lock, flags);
  521. }
  522. EXPORT_SYMBOL_GPL(usbhid_submit_report);
  523. static int usb_hidinput_input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
  524. {
  525. struct hid_device *hid = input_get_drvdata(dev);
  526. struct usbhid_device *usbhid = hid->driver_data;
  527. struct hid_field *field;
  528. unsigned long flags;
  529. int offset;
  530. if (type == EV_FF)
  531. return input_ff_event(dev, type, code, value);
  532. if (type != EV_LED)
  533. return -1;
  534. if ((offset = hidinput_find_field(hid, type, code, &field)) == -1) {
  535. hid_warn(dev, "event field not found\n");
  536. return -1;
  537. }
  538. hid_set_field(field, offset, value);
  539. if (value) {
  540. spin_lock_irqsave(&usbhid->lock, flags);
  541. usbhid->ledcount++;
  542. spin_unlock_irqrestore(&usbhid->lock, flags);
  543. } else {
  544. spin_lock_irqsave(&usbhid->lock, flags);
  545. usbhid->ledcount--;
  546. spin_unlock_irqrestore(&usbhid->lock, flags);
  547. }
  548. usbhid_submit_report(hid, field->report, USB_DIR_OUT);
  549. return 0;
  550. }
  551. int usbhid_wait_io(struct hid_device *hid)
  552. {
  553. struct usbhid_device *usbhid = hid->driver_data;
  554. if (!wait_event_timeout(usbhid->wait,
  555. (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl) &&
  556. !test_bit(HID_OUT_RUNNING, &usbhid->iofl)),
  557. 10*HZ)) {
  558. dbg_hid("timeout waiting for ctrl or out queue to clear\n");
  559. return -1;
  560. }
  561. return 0;
  562. }
  563. EXPORT_SYMBOL_GPL(usbhid_wait_io);
  564. static int hid_set_idle(struct usb_device *dev, int ifnum, int report, int idle)
  565. {
  566. return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  567. HID_REQ_SET_IDLE, USB_TYPE_CLASS | USB_RECIP_INTERFACE, (idle << 8) | report,
  568. ifnum, NULL, 0, USB_CTRL_SET_TIMEOUT);
  569. }
  570. static int hid_get_class_descriptor(struct usb_device *dev, int ifnum,
  571. unsigned char type, void *buf, int size)
  572. {
  573. int result, retries = 4;
  574. memset(buf, 0, size);
  575. do {
  576. result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  577. USB_REQ_GET_DESCRIPTOR, USB_RECIP_INTERFACE | USB_DIR_IN,
  578. (type << 8), ifnum, buf, size, USB_CTRL_GET_TIMEOUT);
  579. retries--;
  580. } while (result < size && retries);
  581. return result;
  582. }
  583. int usbhid_open(struct hid_device *hid)
  584. {
  585. struct usbhid_device *usbhid = hid->driver_data;
  586. int res;
  587. mutex_lock(&hid_open_mut);
  588. if (!hid->open++) {
  589. res = usb_autopm_get_interface(usbhid->intf);
  590. /* the device must be awake to reliably request remote wakeup */
  591. if (res < 0) {
  592. hid->open--;
  593. mutex_unlock(&hid_open_mut);
  594. return -EIO;
  595. }
  596. usbhid->intf->needs_remote_wakeup = 1;
  597. if (hid_start_in(hid))
  598. hid_io_error(hid);
  599. usb_autopm_put_interface(usbhid->intf);
  600. }
  601. mutex_unlock(&hid_open_mut);
  602. return 0;
  603. }
  604. void usbhid_close(struct hid_device *hid)
  605. {
  606. struct usbhid_device *usbhid = hid->driver_data;
  607. mutex_lock(&hid_open_mut);
  608. /* protecting hid->open to make sure we don't restart
  609. * data acquistion due to a resumption we no longer
  610. * care about
  611. */
  612. spin_lock_irq(&usbhid->lock);
  613. if (!--hid->open) {
  614. spin_unlock_irq(&usbhid->lock);
  615. hid_cancel_delayed_stuff(usbhid);
  616. usb_kill_urb(usbhid->urbin);
  617. usbhid->intf->needs_remote_wakeup = 0;
  618. } else {
  619. spin_unlock_irq(&usbhid->lock);
  620. }
  621. mutex_unlock(&hid_open_mut);
  622. }
  623. /*
  624. * Initialize all reports
  625. */
  626. void usbhid_init_reports(struct hid_device *hid)
  627. {
  628. struct hid_report *report;
  629. struct usbhid_device *usbhid = hid->driver_data;
  630. int err, ret;
  631. list_for_each_entry(report, &hid->report_enum[HID_INPUT_REPORT].report_list, list)
  632. usbhid_submit_report(hid, report, USB_DIR_IN);
  633. list_for_each_entry(report, &hid->report_enum[HID_FEATURE_REPORT].report_list, list)
  634. usbhid_submit_report(hid, report, USB_DIR_IN);
  635. err = 0;
  636. ret = usbhid_wait_io(hid);
  637. while (ret) {
  638. err |= ret;
  639. if (test_bit(HID_CTRL_RUNNING, &usbhid->iofl))
  640. usb_kill_urb(usbhid->urbctrl);
  641. if (test_bit(HID_OUT_RUNNING, &usbhid->iofl))
  642. usb_kill_urb(usbhid->urbout);
  643. ret = usbhid_wait_io(hid);
  644. }
  645. if (err)
  646. hid_warn(hid, "timeout initializing reports\n");
  647. }
  648. /*
  649. * Reset LEDs which BIOS might have left on. For now, just NumLock (0x01).
  650. */
  651. static int hid_find_field_early(struct hid_device *hid, unsigned int page,
  652. unsigned int hid_code, struct hid_field **pfield)
  653. {
  654. struct hid_report *report;
  655. struct hid_field *field;
  656. struct hid_usage *usage;
  657. int i, j;
  658. list_for_each_entry(report, &hid->report_enum[HID_OUTPUT_REPORT].report_list, list) {
  659. for (i = 0; i < report->maxfield; i++) {
  660. field = report->field[i];
  661. for (j = 0; j < field->maxusage; j++) {
  662. usage = &field->usage[j];
  663. if ((usage->hid & HID_USAGE_PAGE) == page &&
  664. (usage->hid & 0xFFFF) == hid_code) {
  665. *pfield = field;
  666. return j;
  667. }
  668. }
  669. }
  670. }
  671. return -1;
  672. }
  673. void usbhid_set_leds(struct hid_device *hid)
  674. {
  675. struct hid_field *field;
  676. int offset;
  677. if ((offset = hid_find_field_early(hid, HID_UP_LED, 0x01, &field)) != -1) {
  678. hid_set_field(field, offset, 0);
  679. usbhid_submit_report(hid, field->report, USB_DIR_OUT);
  680. }
  681. }
  682. EXPORT_SYMBOL_GPL(usbhid_set_leds);
  683. /*
  684. * Traverse the supplied list of reports and find the longest
  685. */
  686. static void hid_find_max_report(struct hid_device *hid, unsigned int type,
  687. unsigned int *max)
  688. {
  689. struct hid_report *report;
  690. unsigned int size;
  691. list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
  692. size = ((report->size - 1) >> 3) + 1 + hid->report_enum[type].numbered;
  693. if (*max < size)
  694. *max = size;
  695. }
  696. }
  697. static int hid_alloc_buffers(struct usb_device *dev, struct hid_device *hid)
  698. {
  699. struct usbhid_device *usbhid = hid->driver_data;
  700. usbhid->inbuf = usb_alloc_coherent(dev, usbhid->bufsize, GFP_KERNEL,
  701. &usbhid->inbuf_dma);
  702. usbhid->outbuf = usb_alloc_coherent(dev, usbhid->bufsize, GFP_KERNEL,
  703. &usbhid->outbuf_dma);
  704. usbhid->cr = kmalloc(sizeof(*usbhid->cr), GFP_KERNEL);
  705. usbhid->ctrlbuf = usb_alloc_coherent(dev, usbhid->bufsize, GFP_KERNEL,
  706. &usbhid->ctrlbuf_dma);
  707. if (!usbhid->inbuf || !usbhid->outbuf || !usbhid->cr ||
  708. !usbhid->ctrlbuf)
  709. return -1;
  710. return 0;
  711. }
  712. static int usbhid_get_raw_report(struct hid_device *hid,
  713. unsigned char report_number, __u8 *buf, size_t count,
  714. unsigned char report_type)
  715. {
  716. struct usbhid_device *usbhid = hid->driver_data;
  717. struct usb_device *dev = hid_to_usb_dev(hid);
  718. struct usb_interface *intf = usbhid->intf;
  719. struct usb_host_interface *interface = intf->cur_altsetting;
  720. int skipped_report_id = 0;
  721. int ret;
  722. /* Byte 0 is the report number. Report data starts at byte 1.*/
  723. buf[0] = report_number;
  724. if (report_number == 0x0) {
  725. /* Offset the return buffer by 1, so that the report ID
  726. will remain in byte 0. */
  727. buf++;
  728. count--;
  729. skipped_report_id = 1;
  730. }
  731. ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  732. HID_REQ_GET_REPORT,
  733. USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  734. ((report_type + 1) << 8) | report_number,
  735. interface->desc.bInterfaceNumber, buf, count,
  736. USB_CTRL_SET_TIMEOUT);
  737. /* count also the report id */
  738. if (ret > 0 && skipped_report_id)
  739. ret++;
  740. return ret;
  741. }
  742. static int usbhid_output_raw_report(struct hid_device *hid, __u8 *buf, size_t count,
  743. unsigned char report_type)
  744. {
  745. struct usbhid_device *usbhid = hid->driver_data;
  746. struct usb_device *dev = hid_to_usb_dev(hid);
  747. struct usb_interface *intf = usbhid->intf;
  748. struct usb_host_interface *interface = intf->cur_altsetting;
  749. int ret;
  750. if (usbhid->urbout && report_type != HID_FEATURE_REPORT) {
  751. int actual_length;
  752. int skipped_report_id = 0;
  753. if (buf[0] == 0x0) {
  754. /* Don't send the Report ID */
  755. buf++;
  756. count--;
  757. skipped_report_id = 1;
  758. }
  759. ret = usb_interrupt_msg(dev, usbhid->urbout->pipe,
  760. buf, count, &actual_length,
  761. USB_CTRL_SET_TIMEOUT);
  762. /* return the number of bytes transferred */
  763. if (ret == 0) {
  764. ret = actual_length;
  765. /* count also the report id */
  766. if (skipped_report_id)
  767. ret++;
  768. }
  769. } else {
  770. int skipped_report_id = 0;
  771. int report_id = buf[0];
  772. if (buf[0] == 0x0) {
  773. /* Don't send the Report ID */
  774. buf++;
  775. count--;
  776. skipped_report_id = 1;
  777. }
  778. ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  779. HID_REQ_SET_REPORT,
  780. USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  781. ((report_type + 1) << 8) | report_id,
  782. interface->desc.bInterfaceNumber, buf, count,
  783. USB_CTRL_SET_TIMEOUT);
  784. /* count also the report id, if this was a numbered report. */
  785. if (ret > 0 && skipped_report_id)
  786. ret++;
  787. }
  788. return ret;
  789. }
  790. static void usbhid_restart_queues(struct usbhid_device *usbhid)
  791. {
  792. if (usbhid->urbout)
  793. usbhid_restart_out_queue(usbhid);
  794. usbhid_restart_ctrl_queue(usbhid);
  795. }
  796. static void hid_free_buffers(struct usb_device *dev, struct hid_device *hid)
  797. {
  798. struct usbhid_device *usbhid = hid->driver_data;
  799. usb_free_coherent(dev, usbhid->bufsize, usbhid->inbuf, usbhid->inbuf_dma);
  800. usb_free_coherent(dev, usbhid->bufsize, usbhid->outbuf, usbhid->outbuf_dma);
  801. kfree(usbhid->cr);
  802. usb_free_coherent(dev, usbhid->bufsize, usbhid->ctrlbuf, usbhid->ctrlbuf_dma);
  803. }
  804. static int usbhid_parse(struct hid_device *hid)
  805. {
  806. struct usb_interface *intf = to_usb_interface(hid->dev.parent);
  807. struct usb_host_interface *interface = intf->cur_altsetting;
  808. struct usb_device *dev = interface_to_usbdev (intf);
  809. struct hid_descriptor *hdesc;
  810. u32 quirks = 0;
  811. unsigned int rsize = 0;
  812. char *rdesc;
  813. int ret, n;
  814. quirks = usbhid_lookup_quirk(le16_to_cpu(dev->descriptor.idVendor),
  815. le16_to_cpu(dev->descriptor.idProduct));
  816. if (quirks & HID_QUIRK_IGNORE)
  817. return -ENODEV;
  818. /* Many keyboards and mice don't like to be polled for reports,
  819. * so we will always set the HID_QUIRK_NOGET flag for them. */
  820. if (interface->desc.bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT) {
  821. if (interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_KEYBOARD ||
  822. interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_MOUSE)
  823. quirks |= HID_QUIRK_NOGET;
  824. }
  825. if (usb_get_extra_descriptor(interface, HID_DT_HID, &hdesc) &&
  826. (!interface->desc.bNumEndpoints ||
  827. usb_get_extra_descriptor(&interface->endpoint[0], HID_DT_HID, &hdesc))) {
  828. dbg_hid("class descriptor not present\n");
  829. return -ENODEV;
  830. }
  831. hid->version = le16_to_cpu(hdesc->bcdHID);
  832. hid->country = hdesc->bCountryCode;
  833. for (n = 0; n < hdesc->bNumDescriptors; n++)
  834. if (hdesc->desc[n].bDescriptorType == HID_DT_REPORT)
  835. rsize = le16_to_cpu(hdesc->desc[n].wDescriptorLength);
  836. if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
  837. dbg_hid("weird size of report descriptor (%u)\n", rsize);
  838. return -EINVAL;
  839. }
  840. if (!(rdesc = kmalloc(rsize, GFP_KERNEL))) {
  841. dbg_hid("couldn't allocate rdesc memory\n");
  842. return -ENOMEM;
  843. }
  844. hid_set_idle(dev, interface->desc.bInterfaceNumber, 0, 0);
  845. ret = hid_get_class_descriptor(dev, interface->desc.bInterfaceNumber,
  846. HID_DT_REPORT, rdesc, rsize);
  847. if (ret < 0) {
  848. dbg_hid("reading report descriptor failed\n");
  849. kfree(rdesc);
  850. goto err;
  851. }
  852. ret = hid_parse_report(hid, rdesc, rsize);
  853. kfree(rdesc);
  854. if (ret) {
  855. dbg_hid("parsing report descriptor failed\n");
  856. goto err;
  857. }
  858. hid->quirks |= quirks;
  859. return 0;
  860. err:
  861. return ret;
  862. }
  863. static int usbhid_start(struct hid_device *hid)
  864. {
  865. struct usb_interface *intf = to_usb_interface(hid->dev.parent);
  866. struct usb_host_interface *interface = intf->cur_altsetting;
  867. struct usb_device *dev = interface_to_usbdev(intf);
  868. struct usbhid_device *usbhid = hid->driver_data;
  869. unsigned int n, insize = 0;
  870. int ret;
  871. clear_bit(HID_DISCONNECTED, &usbhid->iofl);
  872. usbhid->bufsize = HID_MIN_BUFFER_SIZE;
  873. hid_find_max_report(hid, HID_INPUT_REPORT, &usbhid->bufsize);
  874. hid_find_max_report(hid, HID_OUTPUT_REPORT, &usbhid->bufsize);
  875. hid_find_max_report(hid, HID_FEATURE_REPORT, &usbhid->bufsize);
  876. if (usbhid->bufsize > HID_MAX_BUFFER_SIZE)
  877. usbhid->bufsize = HID_MAX_BUFFER_SIZE;
  878. hid_find_max_report(hid, HID_INPUT_REPORT, &insize);
  879. if (insize > HID_MAX_BUFFER_SIZE)
  880. insize = HID_MAX_BUFFER_SIZE;
  881. if (hid_alloc_buffers(dev, hid)) {
  882. ret = -ENOMEM;
  883. goto fail;
  884. }
  885. for (n = 0; n < interface->desc.bNumEndpoints; n++) {
  886. struct usb_endpoint_descriptor *endpoint;
  887. int pipe;
  888. int interval;
  889. endpoint = &interface->endpoint[n].desc;
  890. if (!usb_endpoint_xfer_int(endpoint))
  891. continue;
  892. interval = endpoint->bInterval;
  893. /* Some vendors give fullspeed interval on highspeed devides */
  894. if (hid->quirks & HID_QUIRK_FULLSPEED_INTERVAL &&
  895. dev->speed == USB_SPEED_HIGH) {
  896. interval = fls(endpoint->bInterval*8);
  897. printk(KERN_INFO "%s: Fixing fullspeed to highspeed interval: %d -> %d\n",
  898. hid->name, endpoint->bInterval, interval);
  899. }
  900. /* Change the polling interval of mice. */
  901. if (hid->collection->usage == HID_GD_MOUSE && hid_mousepoll_interval > 0)
  902. interval = hid_mousepoll_interval;
  903. ret = -ENOMEM;
  904. if (usb_endpoint_dir_in(endpoint)) {
  905. if (usbhid->urbin)
  906. continue;
  907. if (!(usbhid->urbin = usb_alloc_urb(0, GFP_KERNEL)))
  908. goto fail;
  909. pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
  910. usb_fill_int_urb(usbhid->urbin, dev, pipe, usbhid->inbuf, insize,
  911. hid_irq_in, hid, interval);
  912. usbhid->urbin->transfer_dma = usbhid->inbuf_dma;
  913. usbhid->urbin->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  914. } else {
  915. if (usbhid->urbout)
  916. continue;
  917. if (!(usbhid->urbout = usb_alloc_urb(0, GFP_KERNEL)))
  918. goto fail;
  919. pipe = usb_sndintpipe(dev, endpoint->bEndpointAddress);
  920. usb_fill_int_urb(usbhid->urbout, dev, pipe, usbhid->outbuf, 0,
  921. hid_irq_out, hid, interval);
  922. usbhid->urbout->transfer_dma = usbhid->outbuf_dma;
  923. usbhid->urbout->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  924. }
  925. }
  926. usbhid->urbctrl = usb_alloc_urb(0, GFP_KERNEL);
  927. if (!usbhid->urbctrl) {
  928. ret = -ENOMEM;
  929. goto fail;
  930. }
  931. usb_fill_control_urb(usbhid->urbctrl, dev, 0, (void *) usbhid->cr,
  932. usbhid->ctrlbuf, 1, hid_ctrl, hid);
  933. usbhid->urbctrl->transfer_dma = usbhid->ctrlbuf_dma;
  934. usbhid->urbctrl->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  935. if (!(hid->quirks & HID_QUIRK_NO_INIT_REPORTS))
  936. usbhid_init_reports(hid);
  937. set_bit(HID_STARTED, &usbhid->iofl);
  938. /* Some keyboards don't work until their LEDs have been set.
  939. * Since BIOSes do set the LEDs, it must be safe for any device
  940. * that supports the keyboard boot protocol.
  941. * In addition, enable remote wakeup by default for all keyboard
  942. * devices supporting the boot protocol.
  943. */
  944. if (interface->desc.bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT &&
  945. interface->desc.bInterfaceProtocol ==
  946. USB_INTERFACE_PROTOCOL_KEYBOARD) {
  947. usbhid_set_leds(hid);
  948. device_set_wakeup_enable(&dev->dev, 1);
  949. }
  950. return 0;
  951. fail:
  952. usb_free_urb(usbhid->urbin);
  953. usb_free_urb(usbhid->urbout);
  954. usb_free_urb(usbhid->urbctrl);
  955. usbhid->urbin = NULL;
  956. usbhid->urbout = NULL;
  957. usbhid->urbctrl = NULL;
  958. hid_free_buffers(dev, hid);
  959. return ret;
  960. }
  961. static void usbhid_stop(struct hid_device *hid)
  962. {
  963. struct usbhid_device *usbhid = hid->driver_data;
  964. if (WARN_ON(!usbhid))
  965. return;
  966. clear_bit(HID_STARTED, &usbhid->iofl);
  967. spin_lock_irq(&usbhid->lock); /* Sync with error handler */
  968. set_bit(HID_DISCONNECTED, &usbhid->iofl);
  969. spin_unlock_irq(&usbhid->lock);
  970. usb_kill_urb(usbhid->urbin);
  971. usb_kill_urb(usbhid->urbout);
  972. usb_kill_urb(usbhid->urbctrl);
  973. hid_cancel_delayed_stuff(usbhid);
  974. hid->claimed = 0;
  975. usb_free_urb(usbhid->urbin);
  976. usb_free_urb(usbhid->urbctrl);
  977. usb_free_urb(usbhid->urbout);
  978. usbhid->urbin = NULL; /* don't mess up next start */
  979. usbhid->urbctrl = NULL;
  980. usbhid->urbout = NULL;
  981. hid_free_buffers(hid_to_usb_dev(hid), hid);
  982. }
  983. static int usbhid_power(struct hid_device *hid, int lvl)
  984. {
  985. int r = 0;
  986. switch (lvl) {
  987. case PM_HINT_FULLON:
  988. r = usbhid_get_power(hid);
  989. break;
  990. case PM_HINT_NORMAL:
  991. usbhid_put_power(hid);
  992. break;
  993. }
  994. return r;
  995. }
  996. static struct hid_ll_driver usb_hid_driver = {
  997. .parse = usbhid_parse,
  998. .start = usbhid_start,
  999. .stop = usbhid_stop,
  1000. .open = usbhid_open,
  1001. .close = usbhid_close,
  1002. .power = usbhid_power,
  1003. .hidinput_input_event = usb_hidinput_input_event,
  1004. };
  1005. static int usbhid_probe(struct usb_interface *intf, const struct usb_device_id *id)
  1006. {
  1007. struct usb_host_interface *interface = intf->cur_altsetting;
  1008. struct usb_device *dev = interface_to_usbdev(intf);
  1009. struct usbhid_device *usbhid;
  1010. struct hid_device *hid;
  1011. unsigned int n, has_in = 0;
  1012. size_t len;
  1013. int ret;
  1014. dbg_hid("HID probe called for ifnum %d\n",
  1015. intf->altsetting->desc.bInterfaceNumber);
  1016. for (n = 0; n < interface->desc.bNumEndpoints; n++)
  1017. if (usb_endpoint_is_int_in(&interface->endpoint[n].desc))
  1018. has_in++;
  1019. if (!has_in) {
  1020. hid_err(intf, "couldn't find an input interrupt endpoint\n");
  1021. return -ENODEV;
  1022. }
  1023. hid = hid_allocate_device();
  1024. if (IS_ERR(hid))
  1025. return PTR_ERR(hid);
  1026. usb_set_intfdata(intf, hid);
  1027. hid->ll_driver = &usb_hid_driver;
  1028. hid->hid_get_raw_report = usbhid_get_raw_report;
  1029. hid->hid_output_raw_report = usbhid_output_raw_report;
  1030. hid->ff_init = hid_pidff_init;
  1031. #ifdef CONFIG_USB_HIDDEV
  1032. hid->hiddev_connect = hiddev_connect;
  1033. hid->hiddev_disconnect = hiddev_disconnect;
  1034. hid->hiddev_hid_event = hiddev_hid_event;
  1035. hid->hiddev_report_event = hiddev_report_event;
  1036. #endif
  1037. hid->dev.parent = &intf->dev;
  1038. hid->bus = BUS_USB;
  1039. hid->vendor = le16_to_cpu(dev->descriptor.idVendor);
  1040. hid->product = le16_to_cpu(dev->descriptor.idProduct);
  1041. hid->name[0] = 0;
  1042. hid->quirks = usbhid_lookup_quirk(hid->vendor, hid->product);
  1043. if (intf->cur_altsetting->desc.bInterfaceProtocol ==
  1044. USB_INTERFACE_PROTOCOL_MOUSE)
  1045. hid->type = HID_TYPE_USBMOUSE;
  1046. else if (intf->cur_altsetting->desc.bInterfaceProtocol == 0)
  1047. hid->type = HID_TYPE_USBNONE;
  1048. if (dev->manufacturer)
  1049. strlcpy(hid->name, dev->manufacturer, sizeof(hid->name));
  1050. if (dev->product) {
  1051. if (dev->manufacturer)
  1052. strlcat(hid->name, " ", sizeof(hid->name));
  1053. strlcat(hid->name, dev->product, sizeof(hid->name));
  1054. }
  1055. if (!strlen(hid->name))
  1056. snprintf(hid->name, sizeof(hid->name), "HID %04x:%04x",
  1057. le16_to_cpu(dev->descriptor.idVendor),
  1058. le16_to_cpu(dev->descriptor.idProduct));
  1059. usb_make_path(dev, hid->phys, sizeof(hid->phys));
  1060. strlcat(hid->phys, "/input", sizeof(hid->phys));
  1061. len = strlen(hid->phys);
  1062. if (len < sizeof(hid->phys) - 1)
  1063. snprintf(hid->phys + len, sizeof(hid->phys) - len,
  1064. "%d", intf->altsetting[0].desc.bInterfaceNumber);
  1065. if (usb_string(dev, dev->descriptor.iSerialNumber, hid->uniq, 64) <= 0)
  1066. hid->uniq[0] = 0;
  1067. usbhid = kzalloc(sizeof(*usbhid), GFP_KERNEL);
  1068. if (usbhid == NULL) {
  1069. ret = -ENOMEM;
  1070. goto err;
  1071. }
  1072. hid->driver_data = usbhid;
  1073. usbhid->hid = hid;
  1074. usbhid->intf = intf;
  1075. usbhid->ifnum = interface->desc.bInterfaceNumber;
  1076. init_waitqueue_head(&usbhid->wait);
  1077. INIT_WORK(&usbhid->reset_work, hid_reset);
  1078. setup_timer(&usbhid->io_retry, hid_retry_timeout, (unsigned long) hid);
  1079. spin_lock_init(&usbhid->lock);
  1080. ret = hid_add_device(hid);
  1081. if (ret) {
  1082. if (ret != -ENODEV)
  1083. hid_err(intf, "can't add hid device: %d\n", ret);
  1084. goto err_free;
  1085. }
  1086. return 0;
  1087. err_free:
  1088. kfree(usbhid);
  1089. err:
  1090. hid_destroy_device(hid);
  1091. return ret;
  1092. }
  1093. static void usbhid_disconnect(struct usb_interface *intf)
  1094. {
  1095. struct hid_device *hid = usb_get_intfdata(intf);
  1096. struct usbhid_device *usbhid;
  1097. if (WARN_ON(!hid))
  1098. return;
  1099. usbhid = hid->driver_data;
  1100. hid_destroy_device(hid);
  1101. kfree(usbhid);
  1102. }
  1103. static void hid_cancel_delayed_stuff(struct usbhid_device *usbhid)
  1104. {
  1105. del_timer_sync(&usbhid->io_retry);
  1106. cancel_work_sync(&usbhid->reset_work);
  1107. }
  1108. static void hid_cease_io(struct usbhid_device *usbhid)
  1109. {
  1110. del_timer_sync(&usbhid->io_retry);
  1111. usb_kill_urb(usbhid->urbin);
  1112. usb_kill_urb(usbhid->urbctrl);
  1113. usb_kill_urb(usbhid->urbout);
  1114. }
  1115. /* Treat USB reset pretty much the same as suspend/resume */
  1116. static int hid_pre_reset(struct usb_interface *intf)
  1117. {
  1118. struct hid_device *hid = usb_get_intfdata(intf);
  1119. struct usbhid_device *usbhid = hid->driver_data;
  1120. spin_lock_irq(&usbhid->lock);
  1121. set_bit(HID_RESET_PENDING, &usbhid->iofl);
  1122. spin_unlock_irq(&usbhid->lock);
  1123. hid_cease_io(usbhid);
  1124. return 0;
  1125. }
  1126. /* Same routine used for post_reset and reset_resume */
  1127. static int hid_post_reset(struct usb_interface *intf)
  1128. {
  1129. struct usb_device *dev = interface_to_usbdev (intf);
  1130. struct hid_device *hid = usb_get_intfdata(intf);
  1131. struct usbhid_device *usbhid = hid->driver_data;
  1132. int status;
  1133. spin_lock_irq(&usbhid->lock);
  1134. clear_bit(HID_RESET_PENDING, &usbhid->iofl);
  1135. spin_unlock_irq(&usbhid->lock);
  1136. hid_set_idle(dev, intf->cur_altsetting->desc.bInterfaceNumber, 0, 0);
  1137. status = hid_start_in(hid);
  1138. if (status < 0)
  1139. hid_io_error(hid);
  1140. usbhid_restart_queues(usbhid);
  1141. return 0;
  1142. }
  1143. int usbhid_get_power(struct hid_device *hid)
  1144. {
  1145. struct usbhid_device *usbhid = hid->driver_data;
  1146. return usb_autopm_get_interface(usbhid->intf);
  1147. }
  1148. void usbhid_put_power(struct hid_device *hid)
  1149. {
  1150. struct usbhid_device *usbhid = hid->driver_data;
  1151. usb_autopm_put_interface(usbhid->intf);
  1152. }
  1153. #ifdef CONFIG_PM
  1154. static int hid_suspend(struct usb_interface *intf, pm_message_t message)
  1155. {
  1156. struct hid_device *hid = usb_get_intfdata(intf);
  1157. struct usbhid_device *usbhid = hid->driver_data;
  1158. int status;
  1159. if (PMSG_IS_AUTO(message)) {
  1160. spin_lock_irq(&usbhid->lock); /* Sync with error handler */
  1161. if (!test_bit(HID_RESET_PENDING, &usbhid->iofl)
  1162. && !test_bit(HID_CLEAR_HALT, &usbhid->iofl)
  1163. && !test_bit(HID_OUT_RUNNING, &usbhid->iofl)
  1164. && !test_bit(HID_CTRL_RUNNING, &usbhid->iofl)
  1165. && !test_bit(HID_KEYS_PRESSED, &usbhid->iofl)
  1166. && (!usbhid->ledcount || ignoreled))
  1167. {
  1168. set_bit(HID_REPORTED_IDLE, &usbhid->iofl);
  1169. spin_unlock_irq(&usbhid->lock);
  1170. if (hid->driver && hid->driver->suspend) {
  1171. status = hid->driver->suspend(hid, message);
  1172. if (status < 0)
  1173. return status;
  1174. }
  1175. } else {
  1176. usbhid_mark_busy(usbhid);
  1177. spin_unlock_irq(&usbhid->lock);
  1178. return -EBUSY;
  1179. }
  1180. } else {
  1181. if (hid->driver && hid->driver->suspend) {
  1182. status = hid->driver->suspend(hid, message);
  1183. if (status < 0)
  1184. return status;
  1185. }
  1186. spin_lock_irq(&usbhid->lock);
  1187. set_bit(HID_REPORTED_IDLE, &usbhid->iofl);
  1188. spin_unlock_irq(&usbhid->lock);
  1189. if (usbhid_wait_io(hid) < 0)
  1190. return -EIO;
  1191. }
  1192. hid_cancel_delayed_stuff(usbhid);
  1193. hid_cease_io(usbhid);
  1194. if (PMSG_IS_AUTO(message) && test_bit(HID_KEYS_PRESSED, &usbhid->iofl)) {
  1195. /* lost race against keypresses */
  1196. status = hid_start_in(hid);
  1197. if (status < 0)
  1198. hid_io_error(hid);
  1199. usbhid_mark_busy(usbhid);
  1200. return -EBUSY;
  1201. }
  1202. dev_dbg(&intf->dev, "suspend\n");
  1203. return 0;
  1204. }
  1205. static int hid_resume(struct usb_interface *intf)
  1206. {
  1207. struct hid_device *hid = usb_get_intfdata (intf);
  1208. struct usbhid_device *usbhid = hid->driver_data;
  1209. int status;
  1210. if (!test_bit(HID_STARTED, &usbhid->iofl))
  1211. return 0;
  1212. clear_bit(HID_REPORTED_IDLE, &usbhid->iofl);
  1213. usbhid_mark_busy(usbhid);
  1214. if (test_bit(HID_CLEAR_HALT, &usbhid->iofl) ||
  1215. test_bit(HID_RESET_PENDING, &usbhid->iofl))
  1216. schedule_work(&usbhid->reset_work);
  1217. usbhid->retry_delay = 0;
  1218. status = hid_start_in(hid);
  1219. if (status < 0)
  1220. hid_io_error(hid);
  1221. usbhid_restart_queues(usbhid);
  1222. if (status >= 0 && hid->driver && hid->driver->resume) {
  1223. int ret = hid->driver->resume(hid);
  1224. if (ret < 0)
  1225. status = ret;
  1226. }
  1227. dev_dbg(&intf->dev, "resume status %d\n", status);
  1228. return 0;
  1229. }
  1230. static int hid_reset_resume(struct usb_interface *intf)
  1231. {
  1232. struct hid_device *hid = usb_get_intfdata(intf);
  1233. struct usbhid_device *usbhid = hid->driver_data;
  1234. int status;
  1235. clear_bit(HID_REPORTED_IDLE, &usbhid->iofl);
  1236. status = hid_post_reset(intf);
  1237. if (status >= 0 && hid->driver && hid->driver->reset_resume) {
  1238. int ret = hid->driver->reset_resume(hid);
  1239. if (ret < 0)
  1240. status = ret;
  1241. }
  1242. return status;
  1243. }
  1244. #endif /* CONFIG_PM */
  1245. static const struct usb_device_id hid_usb_ids[] = {
  1246. { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
  1247. .bInterfaceClass = USB_INTERFACE_CLASS_HID },
  1248. { } /* Terminating entry */
  1249. };
  1250. MODULE_DEVICE_TABLE (usb, hid_usb_ids);
  1251. static struct usb_driver hid_driver = {
  1252. .name = "usbhid",
  1253. .probe = usbhid_probe,
  1254. .disconnect = usbhid_disconnect,
  1255. #ifdef CONFIG_PM
  1256. .suspend = hid_suspend,
  1257. .resume = hid_resume,
  1258. .reset_resume = hid_reset_resume,
  1259. #endif
  1260. .pre_reset = hid_pre_reset,
  1261. .post_reset = hid_post_reset,
  1262. .id_table = hid_usb_ids,
  1263. .supports_autosuspend = 1,
  1264. };
  1265. static const struct hid_device_id hid_usb_table[] = {
  1266. { HID_USB_DEVICE(HID_ANY_ID, HID_ANY_ID) },
  1267. { }
  1268. };
  1269. struct usb_interface *usbhid_find_interface(int minor)
  1270. {
  1271. return usb_find_interface(&hid_driver, minor);
  1272. }
  1273. static struct hid_driver hid_usb_driver = {
  1274. .name = "generic-usb",
  1275. .id_table = hid_usb_table,
  1276. };
  1277. static int __init hid_init(void)
  1278. {
  1279. int retval = -ENOMEM;
  1280. retval = hid_register_driver(&hid_usb_driver);
  1281. if (retval)
  1282. goto hid_register_fail;
  1283. retval = usbhid_quirks_init(quirks_param);
  1284. if (retval)
  1285. goto usbhid_quirks_init_fail;
  1286. retval = usb_register(&hid_driver);
  1287. if (retval)
  1288. goto usb_register_fail;
  1289. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n");
  1290. return 0;
  1291. usb_register_fail:
  1292. usbhid_quirks_exit();
  1293. usbhid_quirks_init_fail:
  1294. hid_unregister_driver(&hid_usb_driver);
  1295. hid_register_fail:
  1296. return retval;
  1297. }
  1298. static void __exit hid_exit(void)
  1299. {
  1300. usb_deregister(&hid_driver);
  1301. usbhid_quirks_exit();
  1302. hid_unregister_driver(&hid_usb_driver);
  1303. }
  1304. module_init(hid_init);
  1305. module_exit(hid_exit);
  1306. MODULE_AUTHOR("Andreas Gal");
  1307. MODULE_AUTHOR("Vojtech Pavlik");
  1308. MODULE_AUTHOR("Jiri Kosina");
  1309. MODULE_DESCRIPTION(DRIVER_DESC);
  1310. MODULE_LICENSE(DRIVER_LICENSE);