hid-core.c 45 KB

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