hid-core.c 45 KB

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