hid-core.c 44 KB

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