hid-core.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661
  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. spin_lock(&usbhid->lock);
  414. switch (status) {
  415. case 0: /* success */
  416. if (usbhid->ctrl[usbhid->ctrltail].dir == USB_DIR_IN)
  417. hid_input_report(urb->context,
  418. usbhid->ctrl[usbhid->ctrltail].report->type,
  419. urb->transfer_buffer, urb->actual_length, 0);
  420. break;
  421. case -ESHUTDOWN: /* unplug */
  422. unplug = 1;
  423. case -EILSEQ: /* protocol error or unplug */
  424. case -EPROTO: /* protocol error or unplug */
  425. case -ECONNRESET: /* unlink */
  426. case -ENOENT:
  427. case -EPIPE: /* report not available */
  428. break;
  429. default: /* error */
  430. hid_warn(urb->dev, "ctrl urb status %d received\n", status);
  431. }
  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. msleep(50);
  609. clear_bit(HID_RESUME_RUNNING, &usbhid->iofl);
  610. }
  611. done:
  612. mutex_unlock(&hid_open_mut);
  613. return res;
  614. }
  615. void usbhid_close(struct hid_device *hid)
  616. {
  617. struct usbhid_device *usbhid = hid->driver_data;
  618. mutex_lock(&hid_open_mut);
  619. /* protecting hid->open to make sure we don't restart
  620. * data acquistion due to a resumption we no longer
  621. * care about
  622. */
  623. spin_lock_irq(&usbhid->lock);
  624. if (!--hid->open) {
  625. spin_unlock_irq(&usbhid->lock);
  626. hid_cancel_delayed_stuff(usbhid);
  627. if (!(hid->quirks & HID_QUIRK_ALWAYS_POLL)) {
  628. usb_kill_urb(usbhid->urbin);
  629. usbhid->intf->needs_remote_wakeup = 0;
  630. }
  631. } else {
  632. spin_unlock_irq(&usbhid->lock);
  633. }
  634. mutex_unlock(&hid_open_mut);
  635. }
  636. /*
  637. * Initialize all reports
  638. */
  639. void usbhid_init_reports(struct hid_device *hid)
  640. {
  641. struct hid_report *report;
  642. struct usbhid_device *usbhid = hid->driver_data;
  643. struct hid_report_enum *report_enum;
  644. int err, ret;
  645. if (!(hid->quirks & HID_QUIRK_NO_INIT_INPUT_REPORTS)) {
  646. report_enum = &hid->report_enum[HID_INPUT_REPORT];
  647. list_for_each_entry(report, &report_enum->report_list, list)
  648. usbhid_submit_report(hid, report, USB_DIR_IN);
  649. }
  650. report_enum = &hid->report_enum[HID_FEATURE_REPORT];
  651. list_for_each_entry(report, &report_enum->report_list, list)
  652. usbhid_submit_report(hid, report, USB_DIR_IN);
  653. err = 0;
  654. ret = usbhid_wait_io(hid);
  655. while (ret) {
  656. err |= ret;
  657. if (test_bit(HID_CTRL_RUNNING, &usbhid->iofl))
  658. usb_kill_urb(usbhid->urbctrl);
  659. if (test_bit(HID_OUT_RUNNING, &usbhid->iofl))
  660. usb_kill_urb(usbhid->urbout);
  661. ret = usbhid_wait_io(hid);
  662. }
  663. if (err)
  664. hid_warn(hid, "timeout initializing reports\n");
  665. }
  666. /*
  667. * Reset LEDs which BIOS might have left on. For now, just NumLock (0x01).
  668. */
  669. static int hid_find_field_early(struct hid_device *hid, unsigned int page,
  670. unsigned int hid_code, struct hid_field **pfield)
  671. {
  672. struct hid_report *report;
  673. struct hid_field *field;
  674. struct hid_usage *usage;
  675. int i, j;
  676. list_for_each_entry(report, &hid->report_enum[HID_OUTPUT_REPORT].report_list, list) {
  677. for (i = 0; i < report->maxfield; i++) {
  678. field = report->field[i];
  679. for (j = 0; j < field->maxusage; j++) {
  680. usage = &field->usage[j];
  681. if ((usage->hid & HID_USAGE_PAGE) == page &&
  682. (usage->hid & 0xFFFF) == hid_code) {
  683. *pfield = field;
  684. return j;
  685. }
  686. }
  687. }
  688. }
  689. return -1;
  690. }
  691. static void usbhid_set_leds(struct hid_device *hid)
  692. {
  693. struct hid_field *field;
  694. int offset;
  695. if ((offset = hid_find_field_early(hid, HID_UP_LED, 0x01, &field)) != -1) {
  696. hid_set_field(field, offset, 0);
  697. usbhid_submit_report(hid, field->report, USB_DIR_OUT);
  698. }
  699. }
  700. /*
  701. * Traverse the supplied list of reports and find the longest
  702. */
  703. static void hid_find_max_report(struct hid_device *hid, unsigned int type,
  704. unsigned int *max)
  705. {
  706. struct hid_report *report;
  707. unsigned int size;
  708. list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
  709. size = ((report->size - 1) >> 3) + 1 + hid->report_enum[type].numbered;
  710. if (*max < size)
  711. *max = size;
  712. }
  713. }
  714. static int hid_alloc_buffers(struct usb_device *dev, struct hid_device *hid)
  715. {
  716. struct usbhid_device *usbhid = hid->driver_data;
  717. usbhid->inbuf = usb_alloc_coherent(dev, usbhid->bufsize, GFP_KERNEL,
  718. &usbhid->inbuf_dma);
  719. usbhid->outbuf = usb_alloc_coherent(dev, usbhid->bufsize, GFP_KERNEL,
  720. &usbhid->outbuf_dma);
  721. usbhid->cr = kmalloc(sizeof(*usbhid->cr), GFP_KERNEL);
  722. usbhid->ctrlbuf = usb_alloc_coherent(dev, usbhid->bufsize, GFP_KERNEL,
  723. &usbhid->ctrlbuf_dma);
  724. if (!usbhid->inbuf || !usbhid->outbuf || !usbhid->cr ||
  725. !usbhid->ctrlbuf)
  726. return -1;
  727. return 0;
  728. }
  729. static int usbhid_get_raw_report(struct hid_device *hid,
  730. unsigned char report_number, __u8 *buf, size_t count,
  731. unsigned char report_type)
  732. {
  733. struct usbhid_device *usbhid = hid->driver_data;
  734. struct usb_device *dev = hid_to_usb_dev(hid);
  735. struct usb_interface *intf = usbhid->intf;
  736. struct usb_host_interface *interface = intf->cur_altsetting;
  737. int skipped_report_id = 0;
  738. int ret;
  739. /* Byte 0 is the report number. Report data starts at byte 1.*/
  740. buf[0] = report_number;
  741. if (report_number == 0x0) {
  742. /* Offset the return buffer by 1, so that the report ID
  743. will remain in byte 0. */
  744. buf++;
  745. count--;
  746. skipped_report_id = 1;
  747. }
  748. ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  749. HID_REQ_GET_REPORT,
  750. USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  751. ((report_type + 1) << 8) | report_number,
  752. interface->desc.bInterfaceNumber, buf, count,
  753. USB_CTRL_SET_TIMEOUT);
  754. /* count also the report id */
  755. if (ret > 0 && skipped_report_id)
  756. ret++;
  757. return ret;
  758. }
  759. static int usbhid_set_raw_report(struct hid_device *hid, unsigned int reportnum,
  760. __u8 *buf, size_t count, unsigned char rtype)
  761. {
  762. struct usbhid_device *usbhid = hid->driver_data;
  763. struct usb_device *dev = hid_to_usb_dev(hid);
  764. struct usb_interface *intf = usbhid->intf;
  765. struct usb_host_interface *interface = intf->cur_altsetting;
  766. int ret, skipped_report_id = 0;
  767. /* Byte 0 is the report number. Report data starts at byte 1.*/
  768. if ((rtype == HID_OUTPUT_REPORT) &&
  769. (hid->quirks & HID_QUIRK_SKIP_OUTPUT_REPORT_ID))
  770. buf[0] = 0;
  771. else
  772. buf[0] = reportnum;
  773. if (buf[0] == 0x0) {
  774. /* Don't send the Report ID */
  775. buf++;
  776. count--;
  777. skipped_report_id = 1;
  778. }
  779. ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  780. HID_REQ_SET_REPORT,
  781. USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  782. ((rtype + 1) << 8) | reportnum,
  783. interface->desc.bInterfaceNumber, buf, count,
  784. USB_CTRL_SET_TIMEOUT);
  785. /* count also the report id, if this was a numbered report. */
  786. if (ret > 0 && skipped_report_id)
  787. ret++;
  788. return ret;
  789. }
  790. static int usbhid_output_report(struct hid_device *hid, __u8 *buf, size_t count)
  791. {
  792. struct usbhid_device *usbhid = hid->driver_data;
  793. struct usb_device *dev = hid_to_usb_dev(hid);
  794. int actual_length, skipped_report_id = 0, ret;
  795. if (!usbhid->urbout)
  796. return -ENOSYS;
  797. if (buf[0] == 0x0) {
  798. /* Don't send the Report ID */
  799. buf++;
  800. count--;
  801. skipped_report_id = 1;
  802. }
  803. ret = usb_interrupt_msg(dev, usbhid->urbout->pipe,
  804. buf, count, &actual_length,
  805. USB_CTRL_SET_TIMEOUT);
  806. /* return the number of bytes transferred */
  807. if (ret == 0) {
  808. ret = actual_length;
  809. /* count also the report id */
  810. if (skipped_report_id)
  811. ret++;
  812. }
  813. return ret;
  814. }
  815. static void usbhid_restart_queues(struct usbhid_device *usbhid)
  816. {
  817. if (usbhid->urbout && !test_bit(HID_OUT_RUNNING, &usbhid->iofl))
  818. usbhid_restart_out_queue(usbhid);
  819. if (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl))
  820. usbhid_restart_ctrl_queue(usbhid);
  821. }
  822. static void hid_free_buffers(struct usb_device *dev, struct hid_device *hid)
  823. {
  824. struct usbhid_device *usbhid = hid->driver_data;
  825. usb_free_coherent(dev, usbhid->bufsize, usbhid->inbuf, usbhid->inbuf_dma);
  826. usb_free_coherent(dev, usbhid->bufsize, usbhid->outbuf, usbhid->outbuf_dma);
  827. kfree(usbhid->cr);
  828. usb_free_coherent(dev, usbhid->bufsize, usbhid->ctrlbuf, usbhid->ctrlbuf_dma);
  829. }
  830. static int usbhid_parse(struct hid_device *hid)
  831. {
  832. struct usb_interface *intf = to_usb_interface(hid->dev.parent);
  833. struct usb_host_interface *interface = intf->cur_altsetting;
  834. struct usb_device *dev = interface_to_usbdev (intf);
  835. struct hid_descriptor *hdesc;
  836. u32 quirks = 0;
  837. unsigned int rsize = 0;
  838. char *rdesc;
  839. int ret, n;
  840. quirks = usbhid_lookup_quirk(le16_to_cpu(dev->descriptor.idVendor),
  841. le16_to_cpu(dev->descriptor.idProduct));
  842. if (quirks & HID_QUIRK_IGNORE)
  843. return -ENODEV;
  844. /* Many keyboards and mice don't like to be polled for reports,
  845. * so we will always set the HID_QUIRK_NOGET flag for them. */
  846. if (interface->desc.bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT) {
  847. if (interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_KEYBOARD ||
  848. interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_MOUSE)
  849. quirks |= HID_QUIRK_NOGET;
  850. }
  851. if (usb_get_extra_descriptor(interface, HID_DT_HID, &hdesc) &&
  852. (!interface->desc.bNumEndpoints ||
  853. usb_get_extra_descriptor(&interface->endpoint[0], HID_DT_HID, &hdesc))) {
  854. dbg_hid("class descriptor not present\n");
  855. return -ENODEV;
  856. }
  857. hid->version = le16_to_cpu(hdesc->bcdHID);
  858. hid->country = hdesc->bCountryCode;
  859. for (n = 0; n < hdesc->bNumDescriptors; n++)
  860. if (hdesc->desc[n].bDescriptorType == HID_DT_REPORT)
  861. rsize = le16_to_cpu(hdesc->desc[n].wDescriptorLength);
  862. if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
  863. dbg_hid("weird size of report descriptor (%u)\n", rsize);
  864. return -EINVAL;
  865. }
  866. if (!(rdesc = kmalloc(rsize, GFP_KERNEL))) {
  867. dbg_hid("couldn't allocate rdesc memory\n");
  868. return -ENOMEM;
  869. }
  870. hid_set_idle(dev, interface->desc.bInterfaceNumber, 0, 0);
  871. ret = hid_get_class_descriptor(dev, interface->desc.bInterfaceNumber,
  872. HID_DT_REPORT, rdesc, rsize);
  873. if (ret < 0) {
  874. dbg_hid("reading report descriptor failed\n");
  875. kfree(rdesc);
  876. goto err;
  877. }
  878. ret = hid_parse_report(hid, rdesc, rsize);
  879. kfree(rdesc);
  880. if (ret) {
  881. dbg_hid("parsing report descriptor failed\n");
  882. goto err;
  883. }
  884. hid->quirks |= quirks;
  885. return 0;
  886. err:
  887. return ret;
  888. }
  889. static int usbhid_start(struct hid_device *hid)
  890. {
  891. struct usb_interface *intf = to_usb_interface(hid->dev.parent);
  892. struct usb_host_interface *interface = intf->cur_altsetting;
  893. struct usb_device *dev = interface_to_usbdev(intf);
  894. struct usbhid_device *usbhid = hid->driver_data;
  895. unsigned int n, insize = 0;
  896. int ret;
  897. clear_bit(HID_DISCONNECTED, &usbhid->iofl);
  898. usbhid->bufsize = HID_MIN_BUFFER_SIZE;
  899. hid_find_max_report(hid, HID_INPUT_REPORT, &usbhid->bufsize);
  900. hid_find_max_report(hid, HID_OUTPUT_REPORT, &usbhid->bufsize);
  901. hid_find_max_report(hid, HID_FEATURE_REPORT, &usbhid->bufsize);
  902. if (usbhid->bufsize > HID_MAX_BUFFER_SIZE)
  903. usbhid->bufsize = HID_MAX_BUFFER_SIZE;
  904. hid_find_max_report(hid, HID_INPUT_REPORT, &insize);
  905. if (insize > HID_MAX_BUFFER_SIZE)
  906. insize = HID_MAX_BUFFER_SIZE;
  907. if (hid_alloc_buffers(dev, hid)) {
  908. ret = -ENOMEM;
  909. goto fail;
  910. }
  911. for (n = 0; n < interface->desc.bNumEndpoints; n++) {
  912. struct usb_endpoint_descriptor *endpoint;
  913. int pipe;
  914. int interval;
  915. endpoint = &interface->endpoint[n].desc;
  916. if (!usb_endpoint_xfer_int(endpoint))
  917. continue;
  918. interval = endpoint->bInterval;
  919. /* Some vendors give fullspeed interval on highspeed devides */
  920. if (hid->quirks & HID_QUIRK_FULLSPEED_INTERVAL &&
  921. dev->speed == USB_SPEED_HIGH) {
  922. interval = fls(endpoint->bInterval*8);
  923. printk(KERN_INFO "%s: Fixing fullspeed to highspeed interval: %d -> %d\n",
  924. hid->name, endpoint->bInterval, interval);
  925. }
  926. /* Change the polling interval of mice. */
  927. if (hid->collection->usage == HID_GD_MOUSE && hid_mousepoll_interval > 0)
  928. interval = hid_mousepoll_interval;
  929. ret = -ENOMEM;
  930. if (usb_endpoint_dir_in(endpoint)) {
  931. if (usbhid->urbin)
  932. continue;
  933. if (!(usbhid->urbin = usb_alloc_urb(0, GFP_KERNEL)))
  934. goto fail;
  935. pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
  936. usb_fill_int_urb(usbhid->urbin, dev, pipe, usbhid->inbuf, insize,
  937. hid_irq_in, hid, interval);
  938. usbhid->urbin->transfer_dma = usbhid->inbuf_dma;
  939. usbhid->urbin->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  940. } else {
  941. if (usbhid->urbout)
  942. continue;
  943. if (!(usbhid->urbout = usb_alloc_urb(0, GFP_KERNEL)))
  944. goto fail;
  945. pipe = usb_sndintpipe(dev, endpoint->bEndpointAddress);
  946. usb_fill_int_urb(usbhid->urbout, dev, pipe, usbhid->outbuf, 0,
  947. hid_irq_out, hid, interval);
  948. usbhid->urbout->transfer_dma = usbhid->outbuf_dma;
  949. usbhid->urbout->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  950. }
  951. }
  952. usbhid->urbctrl = usb_alloc_urb(0, GFP_KERNEL);
  953. if (!usbhid->urbctrl) {
  954. ret = -ENOMEM;
  955. goto fail;
  956. }
  957. usb_fill_control_urb(usbhid->urbctrl, dev, 0, (void *) usbhid->cr,
  958. usbhid->ctrlbuf, 1, hid_ctrl, hid);
  959. usbhid->urbctrl->transfer_dma = usbhid->ctrlbuf_dma;
  960. usbhid->urbctrl->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  961. if (!(hid->quirks & HID_QUIRK_NO_INIT_REPORTS))
  962. usbhid_init_reports(hid);
  963. set_bit(HID_STARTED, &usbhid->iofl);
  964. if (hid->quirks & HID_QUIRK_ALWAYS_POLL) {
  965. ret = usb_autopm_get_interface(usbhid->intf);
  966. if (ret)
  967. goto fail;
  968. usbhid->intf->needs_remote_wakeup = 1;
  969. ret = hid_start_in(hid);
  970. if (ret) {
  971. dev_err(&hid->dev,
  972. "failed to start in urb: %d\n", ret);
  973. }
  974. usb_autopm_put_interface(usbhid->intf);
  975. }
  976. /* Some keyboards don't work until their LEDs have been set.
  977. * Since BIOSes do set the LEDs, it must be safe for any device
  978. * that supports the keyboard boot protocol.
  979. * In addition, enable remote wakeup by default for all keyboard
  980. * devices supporting the boot protocol.
  981. */
  982. if (interface->desc.bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT &&
  983. interface->desc.bInterfaceProtocol ==
  984. USB_INTERFACE_PROTOCOL_KEYBOARD) {
  985. usbhid_set_leds(hid);
  986. device_set_wakeup_enable(&dev->dev, 1);
  987. }
  988. return 0;
  989. fail:
  990. usb_free_urb(usbhid->urbin);
  991. usb_free_urb(usbhid->urbout);
  992. usb_free_urb(usbhid->urbctrl);
  993. usbhid->urbin = NULL;
  994. usbhid->urbout = NULL;
  995. usbhid->urbctrl = NULL;
  996. hid_free_buffers(dev, hid);
  997. return ret;
  998. }
  999. static void usbhid_stop(struct hid_device *hid)
  1000. {
  1001. struct usbhid_device *usbhid = hid->driver_data;
  1002. if (WARN_ON(!usbhid))
  1003. return;
  1004. if (hid->quirks & HID_QUIRK_ALWAYS_POLL)
  1005. usbhid->intf->needs_remote_wakeup = 0;
  1006. clear_bit(HID_STARTED, &usbhid->iofl);
  1007. spin_lock_irq(&usbhid->lock); /* Sync with error and led handlers */
  1008. set_bit(HID_DISCONNECTED, &usbhid->iofl);
  1009. spin_unlock_irq(&usbhid->lock);
  1010. usb_kill_urb(usbhid->urbin);
  1011. usb_kill_urb(usbhid->urbout);
  1012. usb_kill_urb(usbhid->urbctrl);
  1013. hid_cancel_delayed_stuff(usbhid);
  1014. hid->claimed = 0;
  1015. usb_free_urb(usbhid->urbin);
  1016. usb_free_urb(usbhid->urbctrl);
  1017. usb_free_urb(usbhid->urbout);
  1018. usbhid->urbin = NULL; /* don't mess up next start */
  1019. usbhid->urbctrl = NULL;
  1020. usbhid->urbout = NULL;
  1021. hid_free_buffers(hid_to_usb_dev(hid), hid);
  1022. }
  1023. static int usbhid_power(struct hid_device *hid, int lvl)
  1024. {
  1025. int r = 0;
  1026. switch (lvl) {
  1027. case PM_HINT_FULLON:
  1028. r = usbhid_get_power(hid);
  1029. break;
  1030. case PM_HINT_NORMAL:
  1031. usbhid_put_power(hid);
  1032. break;
  1033. }
  1034. return r;
  1035. }
  1036. static void usbhid_request(struct hid_device *hid, struct hid_report *rep, int reqtype)
  1037. {
  1038. switch (reqtype) {
  1039. case HID_REQ_GET_REPORT:
  1040. usbhid_submit_report(hid, rep, USB_DIR_IN);
  1041. break;
  1042. case HID_REQ_SET_REPORT:
  1043. usbhid_submit_report(hid, rep, USB_DIR_OUT);
  1044. break;
  1045. }
  1046. }
  1047. static int usbhid_raw_request(struct hid_device *hid, unsigned char reportnum,
  1048. __u8 *buf, size_t len, unsigned char rtype,
  1049. int reqtype)
  1050. {
  1051. switch (reqtype) {
  1052. case HID_REQ_GET_REPORT:
  1053. return usbhid_get_raw_report(hid, reportnum, buf, len, rtype);
  1054. case HID_REQ_SET_REPORT:
  1055. return usbhid_set_raw_report(hid, reportnum, buf, len, rtype);
  1056. default:
  1057. return -EIO;
  1058. }
  1059. }
  1060. static int usbhid_idle(struct hid_device *hid, int report, int idle,
  1061. int reqtype)
  1062. {
  1063. struct usb_device *dev = hid_to_usb_dev(hid);
  1064. struct usb_interface *intf = to_usb_interface(hid->dev.parent);
  1065. struct usb_host_interface *interface = intf->cur_altsetting;
  1066. int ifnum = interface->desc.bInterfaceNumber;
  1067. if (reqtype != HID_REQ_SET_IDLE)
  1068. return -EINVAL;
  1069. return hid_set_idle(dev, ifnum, report, idle);
  1070. }
  1071. static struct hid_ll_driver usb_hid_driver = {
  1072. .parse = usbhid_parse,
  1073. .start = usbhid_start,
  1074. .stop = usbhid_stop,
  1075. .open = usbhid_open,
  1076. .close = usbhid_close,
  1077. .power = usbhid_power,
  1078. .request = usbhid_request,
  1079. .wait = usbhid_wait_io,
  1080. .raw_request = usbhid_raw_request,
  1081. .output_report = usbhid_output_report,
  1082. .idle = usbhid_idle,
  1083. };
  1084. static int usbhid_probe(struct usb_interface *intf, const struct usb_device_id *id)
  1085. {
  1086. struct usb_host_interface *interface = intf->cur_altsetting;
  1087. struct usb_device *dev = interface_to_usbdev(intf);
  1088. struct usbhid_device *usbhid;
  1089. struct hid_device *hid;
  1090. unsigned int n, has_in = 0;
  1091. size_t len;
  1092. int ret;
  1093. dbg_hid("HID probe called for ifnum %d\n",
  1094. intf->altsetting->desc.bInterfaceNumber);
  1095. for (n = 0; n < interface->desc.bNumEndpoints; n++)
  1096. if (usb_endpoint_is_int_in(&interface->endpoint[n].desc))
  1097. has_in++;
  1098. if (!has_in) {
  1099. hid_err(intf, "couldn't find an input interrupt endpoint\n");
  1100. return -ENODEV;
  1101. }
  1102. hid = hid_allocate_device();
  1103. if (IS_ERR(hid))
  1104. return PTR_ERR(hid);
  1105. usb_set_intfdata(intf, hid);
  1106. hid->ll_driver = &usb_hid_driver;
  1107. hid->ff_init = hid_pidff_init;
  1108. #ifdef CONFIG_USB_HIDDEV
  1109. hid->hiddev_connect = hiddev_connect;
  1110. hid->hiddev_disconnect = hiddev_disconnect;
  1111. hid->hiddev_hid_event = hiddev_hid_event;
  1112. hid->hiddev_report_event = hiddev_report_event;
  1113. #endif
  1114. hid->dev.parent = &intf->dev;
  1115. hid->bus = BUS_USB;
  1116. hid->vendor = le16_to_cpu(dev->descriptor.idVendor);
  1117. hid->product = le16_to_cpu(dev->descriptor.idProduct);
  1118. hid->name[0] = 0;
  1119. hid->quirks = usbhid_lookup_quirk(hid->vendor, hid->product);
  1120. if (intf->cur_altsetting->desc.bInterfaceProtocol ==
  1121. USB_INTERFACE_PROTOCOL_MOUSE)
  1122. hid->type = HID_TYPE_USBMOUSE;
  1123. else if (intf->cur_altsetting->desc.bInterfaceProtocol == 0)
  1124. hid->type = HID_TYPE_USBNONE;
  1125. if (dev->manufacturer)
  1126. strlcpy(hid->name, dev->manufacturer, sizeof(hid->name));
  1127. if (dev->product) {
  1128. if (dev->manufacturer)
  1129. strlcat(hid->name, " ", sizeof(hid->name));
  1130. strlcat(hid->name, dev->product, sizeof(hid->name));
  1131. }
  1132. if (!strlen(hid->name))
  1133. snprintf(hid->name, sizeof(hid->name), "HID %04x:%04x",
  1134. le16_to_cpu(dev->descriptor.idVendor),
  1135. le16_to_cpu(dev->descriptor.idProduct));
  1136. usb_make_path(dev, hid->phys, sizeof(hid->phys));
  1137. strlcat(hid->phys, "/input", sizeof(hid->phys));
  1138. len = strlen(hid->phys);
  1139. if (len < sizeof(hid->phys) - 1)
  1140. snprintf(hid->phys + len, sizeof(hid->phys) - len,
  1141. "%d", intf->altsetting[0].desc.bInterfaceNumber);
  1142. if (usb_string(dev, dev->descriptor.iSerialNumber, hid->uniq, 64) <= 0)
  1143. hid->uniq[0] = 0;
  1144. usbhid = kzalloc(sizeof(*usbhid), GFP_KERNEL);
  1145. if (usbhid == NULL) {
  1146. ret = -ENOMEM;
  1147. goto err;
  1148. }
  1149. hid->driver_data = usbhid;
  1150. usbhid->hid = hid;
  1151. usbhid->intf = intf;
  1152. usbhid->ifnum = interface->desc.bInterfaceNumber;
  1153. init_waitqueue_head(&usbhid->wait);
  1154. INIT_WORK(&usbhid->reset_work, hid_reset);
  1155. setup_timer(&usbhid->io_retry, hid_retry_timeout, (unsigned long) hid);
  1156. spin_lock_init(&usbhid->lock);
  1157. ret = hid_add_device(hid);
  1158. if (ret) {
  1159. if (ret != -ENODEV)
  1160. hid_err(intf, "can't add hid device: %d\n", ret);
  1161. goto err_free;
  1162. }
  1163. return 0;
  1164. err_free:
  1165. kfree(usbhid);
  1166. err:
  1167. hid_destroy_device(hid);
  1168. return ret;
  1169. }
  1170. static void usbhid_disconnect(struct usb_interface *intf)
  1171. {
  1172. struct hid_device *hid = usb_get_intfdata(intf);
  1173. struct usbhid_device *usbhid;
  1174. if (WARN_ON(!hid))
  1175. return;
  1176. usbhid = hid->driver_data;
  1177. spin_lock_irq(&usbhid->lock); /* Sync with error and led handlers */
  1178. set_bit(HID_DISCONNECTED, &usbhid->iofl);
  1179. spin_unlock_irq(&usbhid->lock);
  1180. hid_destroy_device(hid);
  1181. kfree(usbhid);
  1182. }
  1183. static void hid_cancel_delayed_stuff(struct usbhid_device *usbhid)
  1184. {
  1185. del_timer_sync(&usbhid->io_retry);
  1186. cancel_work_sync(&usbhid->reset_work);
  1187. }
  1188. static void hid_cease_io(struct usbhid_device *usbhid)
  1189. {
  1190. del_timer_sync(&usbhid->io_retry);
  1191. usb_kill_urb(usbhid->urbin);
  1192. usb_kill_urb(usbhid->urbctrl);
  1193. usb_kill_urb(usbhid->urbout);
  1194. }
  1195. /* Treat USB reset pretty much the same as suspend/resume */
  1196. static int hid_pre_reset(struct usb_interface *intf)
  1197. {
  1198. struct hid_device *hid = usb_get_intfdata(intf);
  1199. struct usbhid_device *usbhid = hid->driver_data;
  1200. spin_lock_irq(&usbhid->lock);
  1201. set_bit(HID_RESET_PENDING, &usbhid->iofl);
  1202. spin_unlock_irq(&usbhid->lock);
  1203. hid_cease_io(usbhid);
  1204. return 0;
  1205. }
  1206. /* Same routine used for post_reset and reset_resume */
  1207. static int hid_post_reset(struct usb_interface *intf)
  1208. {
  1209. struct usb_device *dev = interface_to_usbdev (intf);
  1210. struct hid_device *hid = usb_get_intfdata(intf);
  1211. struct usbhid_device *usbhid = hid->driver_data;
  1212. struct usb_host_interface *interface = intf->cur_altsetting;
  1213. int status;
  1214. char *rdesc;
  1215. /* Fetch and examine the HID report descriptor. If this
  1216. * has changed, then rebind. Since usbcore's check of the
  1217. * configuration descriptors passed, we already know that
  1218. * the size of the HID report descriptor has not changed.
  1219. */
  1220. rdesc = kmalloc(hid->dev_rsize, GFP_KERNEL);
  1221. if (!rdesc) {
  1222. dbg_hid("couldn't allocate rdesc memory (post_reset)\n");
  1223. return 1;
  1224. }
  1225. status = hid_get_class_descriptor(dev,
  1226. interface->desc.bInterfaceNumber,
  1227. HID_DT_REPORT, rdesc, hid->dev_rsize);
  1228. if (status < 0) {
  1229. dbg_hid("reading report descriptor failed (post_reset)\n");
  1230. kfree(rdesc);
  1231. return 1;
  1232. }
  1233. status = memcmp(rdesc, hid->dev_rdesc, hid->dev_rsize);
  1234. kfree(rdesc);
  1235. if (status != 0) {
  1236. dbg_hid("report descriptor changed\n");
  1237. return 1;
  1238. }
  1239. spin_lock_irq(&usbhid->lock);
  1240. clear_bit(HID_RESET_PENDING, &usbhid->iofl);
  1241. spin_unlock_irq(&usbhid->lock);
  1242. hid_set_idle(dev, intf->cur_altsetting->desc.bInterfaceNumber, 0, 0);
  1243. status = hid_start_in(hid);
  1244. if (status < 0)
  1245. hid_io_error(hid);
  1246. usbhid_restart_queues(usbhid);
  1247. return 0;
  1248. }
  1249. int usbhid_get_power(struct hid_device *hid)
  1250. {
  1251. struct usbhid_device *usbhid = hid->driver_data;
  1252. return usb_autopm_get_interface(usbhid->intf);
  1253. }
  1254. void usbhid_put_power(struct hid_device *hid)
  1255. {
  1256. struct usbhid_device *usbhid = hid->driver_data;
  1257. usb_autopm_put_interface(usbhid->intf);
  1258. }
  1259. #ifdef CONFIG_PM
  1260. static int hid_resume_common(struct hid_device *hid, bool driver_suspended)
  1261. {
  1262. struct usbhid_device *usbhid = hid->driver_data;
  1263. int status;
  1264. spin_lock_irq(&usbhid->lock);
  1265. clear_bit(HID_SUSPENDED, &usbhid->iofl);
  1266. usbhid_mark_busy(usbhid);
  1267. if (test_bit(HID_CLEAR_HALT, &usbhid->iofl) ||
  1268. test_bit(HID_RESET_PENDING, &usbhid->iofl))
  1269. schedule_work(&usbhid->reset_work);
  1270. usbhid->retry_delay = 0;
  1271. usbhid_restart_queues(usbhid);
  1272. spin_unlock_irq(&usbhid->lock);
  1273. status = hid_start_in(hid);
  1274. if (status < 0)
  1275. hid_io_error(hid);
  1276. if (driver_suspended && hid->driver && hid->driver->resume)
  1277. status = hid->driver->resume(hid);
  1278. return status;
  1279. }
  1280. static int hid_suspend(struct usb_interface *intf, pm_message_t message)
  1281. {
  1282. struct hid_device *hid = usb_get_intfdata(intf);
  1283. struct usbhid_device *usbhid = hid->driver_data;
  1284. int status = 0;
  1285. bool driver_suspended = false;
  1286. unsigned int ledcount;
  1287. if (PMSG_IS_AUTO(message)) {
  1288. ledcount = hidinput_count_leds(hid);
  1289. spin_lock_irq(&usbhid->lock); /* Sync with error handler */
  1290. if (!test_bit(HID_RESET_PENDING, &usbhid->iofl)
  1291. && !test_bit(HID_CLEAR_HALT, &usbhid->iofl)
  1292. && !test_bit(HID_OUT_RUNNING, &usbhid->iofl)
  1293. && !test_bit(HID_CTRL_RUNNING, &usbhid->iofl)
  1294. && !test_bit(HID_KEYS_PRESSED, &usbhid->iofl)
  1295. && (!ledcount || ignoreled))
  1296. {
  1297. set_bit(HID_SUSPENDED, &usbhid->iofl);
  1298. spin_unlock_irq(&usbhid->lock);
  1299. if (hid->driver && hid->driver->suspend) {
  1300. status = hid->driver->suspend(hid, message);
  1301. if (status < 0)
  1302. goto failed;
  1303. }
  1304. driver_suspended = true;
  1305. } else {
  1306. usbhid_mark_busy(usbhid);
  1307. spin_unlock_irq(&usbhid->lock);
  1308. return -EBUSY;
  1309. }
  1310. } else {
  1311. /* TODO: resume() might need to handle suspend failure */
  1312. if (hid->driver && hid->driver->suspend)
  1313. status = hid->driver->suspend(hid, message);
  1314. driver_suspended = true;
  1315. spin_lock_irq(&usbhid->lock);
  1316. set_bit(HID_SUSPENDED, &usbhid->iofl);
  1317. spin_unlock_irq(&usbhid->lock);
  1318. if (usbhid_wait_io(hid) < 0)
  1319. status = -EIO;
  1320. }
  1321. hid_cancel_delayed_stuff(usbhid);
  1322. hid_cease_io(usbhid);
  1323. if (PMSG_IS_AUTO(message) && test_bit(HID_KEYS_PRESSED, &usbhid->iofl)) {
  1324. /* lost race against keypresses */
  1325. status = -EBUSY;
  1326. goto failed;
  1327. }
  1328. dev_dbg(&intf->dev, "suspend\n");
  1329. return status;
  1330. failed:
  1331. hid_resume_common(hid, driver_suspended);
  1332. return status;
  1333. }
  1334. static int hid_resume(struct usb_interface *intf)
  1335. {
  1336. struct hid_device *hid = usb_get_intfdata (intf);
  1337. struct usbhid_device *usbhid = hid->driver_data;
  1338. int status;
  1339. if (!test_bit(HID_STARTED, &usbhid->iofl))
  1340. return 0;
  1341. status = hid_resume_common(hid, true);
  1342. dev_dbg(&intf->dev, "resume status %d\n", status);
  1343. return 0;
  1344. }
  1345. static int hid_reset_resume(struct usb_interface *intf)
  1346. {
  1347. struct hid_device *hid = usb_get_intfdata(intf);
  1348. struct usbhid_device *usbhid = hid->driver_data;
  1349. int status;
  1350. clear_bit(HID_SUSPENDED, &usbhid->iofl);
  1351. status = hid_post_reset(intf);
  1352. if (status >= 0 && hid->driver && hid->driver->reset_resume) {
  1353. int ret = hid->driver->reset_resume(hid);
  1354. if (ret < 0)
  1355. status = ret;
  1356. }
  1357. return status;
  1358. }
  1359. #endif /* CONFIG_PM */
  1360. static const struct usb_device_id hid_usb_ids[] = {
  1361. { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
  1362. .bInterfaceClass = USB_INTERFACE_CLASS_HID },
  1363. { } /* Terminating entry */
  1364. };
  1365. MODULE_DEVICE_TABLE (usb, hid_usb_ids);
  1366. static struct usb_driver hid_driver = {
  1367. .name = "usbhid",
  1368. .probe = usbhid_probe,
  1369. .disconnect = usbhid_disconnect,
  1370. #ifdef CONFIG_PM
  1371. .suspend = hid_suspend,
  1372. .resume = hid_resume,
  1373. .reset_resume = hid_reset_resume,
  1374. #endif
  1375. .pre_reset = hid_pre_reset,
  1376. .post_reset = hid_post_reset,
  1377. .id_table = hid_usb_ids,
  1378. .supports_autosuspend = 1,
  1379. };
  1380. struct usb_interface *usbhid_find_interface(int minor)
  1381. {
  1382. return usb_find_interface(&hid_driver, minor);
  1383. }
  1384. static int __init hid_init(void)
  1385. {
  1386. int retval = -ENOMEM;
  1387. retval = usbhid_quirks_init(quirks_param);
  1388. if (retval)
  1389. goto usbhid_quirks_init_fail;
  1390. retval = usb_register(&hid_driver);
  1391. if (retval)
  1392. goto usb_register_fail;
  1393. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n");
  1394. return 0;
  1395. usb_register_fail:
  1396. usbhid_quirks_exit();
  1397. usbhid_quirks_init_fail:
  1398. return retval;
  1399. }
  1400. static void __exit hid_exit(void)
  1401. {
  1402. usb_deregister(&hid_driver);
  1403. usbhid_quirks_exit();
  1404. }
  1405. module_init(hid_init);
  1406. module_exit(hid_exit);
  1407. MODULE_AUTHOR("Andreas Gal");
  1408. MODULE_AUTHOR("Vojtech Pavlik");
  1409. MODULE_AUTHOR("Jiri Kosina");
  1410. MODULE_DESCRIPTION(DRIVER_DESC);
  1411. MODULE_LICENSE(DRIVER_LICENSE);