hid-core.c 45 KB

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