hid-core.c 45 KB

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