hid-core.c 46 KB

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