hid-core.c 44 KB

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