hid-core.c 44 KB

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