hid-core.c 44 KB

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