hid-core.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674
  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->retry_delay = 0;
  234. if ((hid->quirks & HID_QUIRK_ALWAYS_POLL) && !hid->open)
  235. break;
  236. usbhid_mark_busy(usbhid);
  237. if (!test_bit(HID_RESUME_RUNNING, &usbhid->iofl)) {
  238. hid_input_report(urb->context, HID_INPUT_REPORT,
  239. urb->transfer_buffer,
  240. urb->actual_length, 1);
  241. /*
  242. * autosuspend refused while keys are pressed
  243. * because most keyboards don't wake up when
  244. * a key is released
  245. */
  246. if (hid_check_keys_pressed(hid))
  247. set_bit(HID_KEYS_PRESSED, &usbhid->iofl);
  248. else
  249. clear_bit(HID_KEYS_PRESSED, &usbhid->iofl);
  250. }
  251. break;
  252. case -EPIPE: /* stall */
  253. usbhid_mark_busy(usbhid);
  254. clear_bit(HID_IN_RUNNING, &usbhid->iofl);
  255. set_bit(HID_CLEAR_HALT, &usbhid->iofl);
  256. schedule_work(&usbhid->reset_work);
  257. return;
  258. case -ECONNRESET: /* unlink */
  259. case -ENOENT:
  260. case -ESHUTDOWN: /* unplug */
  261. clear_bit(HID_IN_RUNNING, &usbhid->iofl);
  262. return;
  263. case -EILSEQ: /* protocol error or unplug */
  264. case -EPROTO: /* protocol error or unplug */
  265. case -ETIME: /* protocol error or unplug */
  266. case -ETIMEDOUT: /* Should never happen, but... */
  267. usbhid_mark_busy(usbhid);
  268. clear_bit(HID_IN_RUNNING, &usbhid->iofl);
  269. hid_io_error(hid);
  270. return;
  271. default: /* error */
  272. hid_warn(urb->dev, "input irq status %d received\n",
  273. urb->status);
  274. }
  275. status = usb_submit_urb(urb, GFP_ATOMIC);
  276. if (status) {
  277. clear_bit(HID_IN_RUNNING, &usbhid->iofl);
  278. if (status != -EPERM) {
  279. hid_err(hid, "can't resubmit intr, %s-%s/input%d, status %d\n",
  280. hid_to_usb_dev(hid)->bus->bus_name,
  281. hid_to_usb_dev(hid)->devpath,
  282. usbhid->ifnum, status);
  283. hid_io_error(hid);
  284. }
  285. }
  286. }
  287. static int hid_submit_out(struct hid_device *hid)
  288. {
  289. struct hid_report *report;
  290. char *raw_report;
  291. struct usbhid_device *usbhid = hid->driver_data;
  292. int r;
  293. report = usbhid->out[usbhid->outtail].report;
  294. raw_report = usbhid->out[usbhid->outtail].raw_report;
  295. usbhid->urbout->transfer_buffer_length = hid_report_len(report);
  296. usbhid->urbout->dev = hid_to_usb_dev(hid);
  297. if (raw_report) {
  298. memcpy(usbhid->outbuf, raw_report,
  299. usbhid->urbout->transfer_buffer_length);
  300. kfree(raw_report);
  301. usbhid->out[usbhid->outtail].raw_report = NULL;
  302. }
  303. dbg_hid("submitting out urb\n");
  304. r = usb_submit_urb(usbhid->urbout, GFP_ATOMIC);
  305. if (r < 0) {
  306. hid_err(hid, "usb_submit_urb(out) failed: %d\n", r);
  307. return r;
  308. }
  309. usbhid->last_out = jiffies;
  310. return 0;
  311. }
  312. static int hid_submit_ctrl(struct hid_device *hid)
  313. {
  314. struct hid_report *report;
  315. unsigned char dir;
  316. char *raw_report;
  317. int len, r;
  318. struct usbhid_device *usbhid = hid->driver_data;
  319. report = usbhid->ctrl[usbhid->ctrltail].report;
  320. raw_report = usbhid->ctrl[usbhid->ctrltail].raw_report;
  321. dir = usbhid->ctrl[usbhid->ctrltail].dir;
  322. len = ((report->size - 1) >> 3) + 1 + (report->id > 0);
  323. if (dir == USB_DIR_OUT) {
  324. usbhid->urbctrl->pipe = usb_sndctrlpipe(hid_to_usb_dev(hid), 0);
  325. usbhid->urbctrl->transfer_buffer_length = len;
  326. if (raw_report) {
  327. memcpy(usbhid->ctrlbuf, raw_report, len);
  328. kfree(raw_report);
  329. usbhid->ctrl[usbhid->ctrltail].raw_report = NULL;
  330. }
  331. } else {
  332. int maxpacket, padlen;
  333. usbhid->urbctrl->pipe = usb_rcvctrlpipe(hid_to_usb_dev(hid), 0);
  334. maxpacket = usb_maxpacket(hid_to_usb_dev(hid),
  335. usbhid->urbctrl->pipe, 0);
  336. if (maxpacket > 0) {
  337. padlen = DIV_ROUND_UP(len, maxpacket);
  338. padlen *= maxpacket;
  339. if (padlen > usbhid->bufsize)
  340. padlen = usbhid->bufsize;
  341. } else
  342. padlen = 0;
  343. usbhid->urbctrl->transfer_buffer_length = padlen;
  344. }
  345. usbhid->urbctrl->dev = hid_to_usb_dev(hid);
  346. usbhid->cr->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE | dir;
  347. usbhid->cr->bRequest = (dir == USB_DIR_OUT) ? HID_REQ_SET_REPORT :
  348. HID_REQ_GET_REPORT;
  349. usbhid->cr->wValue = cpu_to_le16(((report->type + 1) << 8) |
  350. report->id);
  351. usbhid->cr->wIndex = cpu_to_le16(usbhid->ifnum);
  352. usbhid->cr->wLength = cpu_to_le16(len);
  353. dbg_hid("submitting ctrl urb: %s wValue=0x%04x wIndex=0x%04x wLength=%u\n",
  354. usbhid->cr->bRequest == HID_REQ_SET_REPORT ? "Set_Report" :
  355. "Get_Report",
  356. usbhid->cr->wValue, usbhid->cr->wIndex, usbhid->cr->wLength);
  357. r = usb_submit_urb(usbhid->urbctrl, GFP_ATOMIC);
  358. if (r < 0) {
  359. hid_err(hid, "usb_submit_urb(ctrl) failed: %d\n", r);
  360. return r;
  361. }
  362. usbhid->last_ctrl = jiffies;
  363. return 0;
  364. }
  365. /*
  366. * Output interrupt completion handler.
  367. */
  368. static void hid_irq_out(struct urb *urb)
  369. {
  370. struct hid_device *hid = urb->context;
  371. struct usbhid_device *usbhid = hid->driver_data;
  372. unsigned long flags;
  373. int unplug = 0;
  374. switch (urb->status) {
  375. case 0: /* success */
  376. break;
  377. case -ESHUTDOWN: /* unplug */
  378. unplug = 1;
  379. case -EILSEQ: /* protocol error or unplug */
  380. case -EPROTO: /* protocol error or unplug */
  381. case -ECONNRESET: /* unlink */
  382. case -ENOENT:
  383. break;
  384. default: /* error */
  385. hid_warn(urb->dev, "output irq status %d received\n",
  386. urb->status);
  387. }
  388. spin_lock_irqsave(&usbhid->lock, flags);
  389. if (unplug) {
  390. usbhid->outtail = usbhid->outhead;
  391. } else {
  392. usbhid->outtail = (usbhid->outtail + 1) & (HID_OUTPUT_FIFO_SIZE - 1);
  393. if (usbhid->outhead != usbhid->outtail &&
  394. hid_submit_out(hid) == 0) {
  395. /* Successfully submitted next urb in queue */
  396. spin_unlock_irqrestore(&usbhid->lock, flags);
  397. return;
  398. }
  399. }
  400. clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
  401. spin_unlock_irqrestore(&usbhid->lock, flags);
  402. usb_autopm_put_interface_async(usbhid->intf);
  403. wake_up(&usbhid->wait);
  404. }
  405. /*
  406. * Control pipe completion handler.
  407. */
  408. static void hid_ctrl(struct urb *urb)
  409. {
  410. struct hid_device *hid = urb->context;
  411. struct usbhid_device *usbhid = hid->driver_data;
  412. int unplug = 0, status = urb->status;
  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. spin_lock(&usbhid->lock);
  432. if (unplug) {
  433. usbhid->ctrltail = usbhid->ctrlhead;
  434. } else {
  435. usbhid->ctrltail = (usbhid->ctrltail + 1) & (HID_CONTROL_FIFO_SIZE - 1);
  436. if (usbhid->ctrlhead != usbhid->ctrltail &&
  437. hid_submit_ctrl(hid) == 0) {
  438. /* Successfully submitted next urb in queue */
  439. spin_unlock(&usbhid->lock);
  440. return;
  441. }
  442. }
  443. clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
  444. spin_unlock(&usbhid->lock);
  445. usb_autopm_put_interface_async(usbhid->intf);
  446. wake_up(&usbhid->wait);
  447. }
  448. static void __usbhid_submit_report(struct hid_device *hid, struct hid_report *report,
  449. unsigned char dir)
  450. {
  451. int head;
  452. struct usbhid_device *usbhid = hid->driver_data;
  453. if (((hid->quirks & HID_QUIRK_NOGET) && dir == USB_DIR_IN) ||
  454. test_bit(HID_DISCONNECTED, &usbhid->iofl))
  455. return;
  456. if (usbhid->urbout && dir == USB_DIR_OUT && report->type == HID_OUTPUT_REPORT) {
  457. if ((head = (usbhid->outhead + 1) & (HID_OUTPUT_FIFO_SIZE - 1)) == usbhid->outtail) {
  458. hid_warn(hid, "output queue full\n");
  459. return;
  460. }
  461. usbhid->out[usbhid->outhead].raw_report = hid_alloc_report_buf(report, GFP_ATOMIC);
  462. if (!usbhid->out[usbhid->outhead].raw_report) {
  463. hid_warn(hid, "output queueing failed\n");
  464. return;
  465. }
  466. hid_output_report(report, usbhid->out[usbhid->outhead].raw_report);
  467. usbhid->out[usbhid->outhead].report = report;
  468. usbhid->outhead = head;
  469. /* If the queue isn't running, restart it */
  470. if (!test_bit(HID_OUT_RUNNING, &usbhid->iofl)) {
  471. usbhid_restart_out_queue(usbhid);
  472. /* Otherwise see if an earlier request has timed out */
  473. } else if (time_after(jiffies, usbhid->last_out + HZ * 5)) {
  474. /* Prevent autosuspend following the unlink */
  475. usb_autopm_get_interface_no_resume(usbhid->intf);
  476. /*
  477. * Prevent resubmission in case the URB completes
  478. * before we can unlink it. We don't want to cancel
  479. * the wrong transfer!
  480. */
  481. usb_block_urb(usbhid->urbout);
  482. /* Drop lock to avoid deadlock if the callback runs */
  483. spin_unlock(&usbhid->lock);
  484. usb_unlink_urb(usbhid->urbout);
  485. spin_lock(&usbhid->lock);
  486. usb_unblock_urb(usbhid->urbout);
  487. /* Unlink might have stopped the queue */
  488. if (!test_bit(HID_OUT_RUNNING, &usbhid->iofl))
  489. usbhid_restart_out_queue(usbhid);
  490. /* Now we can allow autosuspend again */
  491. usb_autopm_put_interface_async(usbhid->intf);
  492. }
  493. return;
  494. }
  495. if ((head = (usbhid->ctrlhead + 1) & (HID_CONTROL_FIFO_SIZE - 1)) == usbhid->ctrltail) {
  496. hid_warn(hid, "control queue full\n");
  497. return;
  498. }
  499. if (dir == USB_DIR_OUT) {
  500. usbhid->ctrl[usbhid->ctrlhead].raw_report = hid_alloc_report_buf(report, GFP_ATOMIC);
  501. if (!usbhid->ctrl[usbhid->ctrlhead].raw_report) {
  502. hid_warn(hid, "control queueing failed\n");
  503. return;
  504. }
  505. hid_output_report(report, usbhid->ctrl[usbhid->ctrlhead].raw_report);
  506. }
  507. usbhid->ctrl[usbhid->ctrlhead].report = report;
  508. usbhid->ctrl[usbhid->ctrlhead].dir = dir;
  509. usbhid->ctrlhead = head;
  510. /* If the queue isn't running, restart it */
  511. if (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl)) {
  512. usbhid_restart_ctrl_queue(usbhid);
  513. /* Otherwise see if an earlier request has timed out */
  514. } else if (time_after(jiffies, usbhid->last_ctrl + HZ * 5)) {
  515. /* Prevent autosuspend following the unlink */
  516. usb_autopm_get_interface_no_resume(usbhid->intf);
  517. /*
  518. * Prevent resubmission in case the URB completes
  519. * before we can unlink it. We don't want to cancel
  520. * the wrong transfer!
  521. */
  522. usb_block_urb(usbhid->urbctrl);
  523. /* Drop lock to avoid deadlock if the callback runs */
  524. spin_unlock(&usbhid->lock);
  525. usb_unlink_urb(usbhid->urbctrl);
  526. spin_lock(&usbhid->lock);
  527. usb_unblock_urb(usbhid->urbctrl);
  528. /* Unlink might have stopped the queue */
  529. if (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl))
  530. usbhid_restart_ctrl_queue(usbhid);
  531. /* Now we can allow autosuspend again */
  532. usb_autopm_put_interface_async(usbhid->intf);
  533. }
  534. }
  535. static void usbhid_submit_report(struct hid_device *hid, struct hid_report *report, unsigned char dir)
  536. {
  537. struct usbhid_device *usbhid = hid->driver_data;
  538. unsigned long flags;
  539. spin_lock_irqsave(&usbhid->lock, flags);
  540. __usbhid_submit_report(hid, report, dir);
  541. spin_unlock_irqrestore(&usbhid->lock, flags);
  542. }
  543. static int usbhid_wait_io(struct hid_device *hid)
  544. {
  545. struct usbhid_device *usbhid = hid->driver_data;
  546. if (!wait_event_timeout(usbhid->wait,
  547. (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl) &&
  548. !test_bit(HID_OUT_RUNNING, &usbhid->iofl)),
  549. 10*HZ)) {
  550. dbg_hid("timeout waiting for ctrl or out queue to clear\n");
  551. return -1;
  552. }
  553. return 0;
  554. }
  555. static int hid_set_idle(struct usb_device *dev, int ifnum, int report, int idle)
  556. {
  557. return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  558. HID_REQ_SET_IDLE, USB_TYPE_CLASS | USB_RECIP_INTERFACE, (idle << 8) | report,
  559. ifnum, NULL, 0, USB_CTRL_SET_TIMEOUT);
  560. }
  561. static int hid_get_class_descriptor(struct usb_device *dev, int ifnum,
  562. unsigned char type, void *buf, int size)
  563. {
  564. int result, retries = 4;
  565. memset(buf, 0, size);
  566. do {
  567. result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  568. USB_REQ_GET_DESCRIPTOR, USB_RECIP_INTERFACE | USB_DIR_IN,
  569. (type << 8), ifnum, buf, size, USB_CTRL_GET_TIMEOUT);
  570. retries--;
  571. } while (result < size && retries);
  572. return result;
  573. }
  574. int usbhid_open(struct hid_device *hid)
  575. {
  576. struct usbhid_device *usbhid = hid->driver_data;
  577. int res = 0;
  578. mutex_lock(&hid_open_mut);
  579. if (!hid->open++) {
  580. res = usb_autopm_get_interface(usbhid->intf);
  581. /* the device must be awake to reliably request remote wakeup */
  582. if (res < 0) {
  583. hid->open--;
  584. res = -EIO;
  585. goto done;
  586. }
  587. usbhid->intf->needs_remote_wakeup = 1;
  588. set_bit(HID_RESUME_RUNNING, &usbhid->iofl);
  589. res = hid_start_in(hid);
  590. if (res) {
  591. if (res != -ENOSPC) {
  592. hid_io_error(hid);
  593. res = 0;
  594. } else {
  595. /* no use opening if resources are insufficient */
  596. hid->open--;
  597. res = -EBUSY;
  598. usbhid->intf->needs_remote_wakeup = 0;
  599. }
  600. }
  601. usb_autopm_put_interface(usbhid->intf);
  602. /*
  603. * In case events are generated while nobody was listening,
  604. * some are released when the device is re-opened.
  605. * Wait 50 msec for the queue to empty before allowing events
  606. * to go through hid.
  607. */
  608. if (res == 0 && !(hid->quirks & HID_QUIRK_ALWAYS_POLL))
  609. msleep(50);
  610. clear_bit(HID_RESUME_RUNNING, &usbhid->iofl);
  611. }
  612. done:
  613. mutex_unlock(&hid_open_mut);
  614. return res;
  615. }
  616. void usbhid_close(struct hid_device *hid)
  617. {
  618. struct usbhid_device *usbhid = hid->driver_data;
  619. mutex_lock(&hid_open_mut);
  620. /* protecting hid->open to make sure we don't restart
  621. * data acquistion due to a resumption we no longer
  622. * care about
  623. */
  624. spin_lock_irq(&usbhid->lock);
  625. if (!--hid->open) {
  626. spin_unlock_irq(&usbhid->lock);
  627. hid_cancel_delayed_stuff(usbhid);
  628. if (!(hid->quirks & HID_QUIRK_ALWAYS_POLL)) {
  629. usb_kill_urb(usbhid->urbin);
  630. usbhid->intf->needs_remote_wakeup = 0;
  631. }
  632. } else {
  633. spin_unlock_irq(&usbhid->lock);
  634. }
  635. mutex_unlock(&hid_open_mut);
  636. }
  637. /*
  638. * Initialize all reports
  639. */
  640. void usbhid_init_reports(struct hid_device *hid)
  641. {
  642. struct hid_report *report;
  643. struct usbhid_device *usbhid = hid->driver_data;
  644. struct hid_report_enum *report_enum;
  645. int err, ret;
  646. if (!(hid->quirks & HID_QUIRK_NO_INIT_INPUT_REPORTS)) {
  647. report_enum = &hid->report_enum[HID_INPUT_REPORT];
  648. list_for_each_entry(report, &report_enum->report_list, list)
  649. usbhid_submit_report(hid, report, USB_DIR_IN);
  650. }
  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 = usbhid_lookup_quirk(le16_to_cpu(dev->descriptor.idVendor),
  837. le16_to_cpu(dev->descriptor.idProduct));
  838. if (quirks & HID_QUIRK_IGNORE)
  839. return -ENODEV;
  840. /* Many keyboards and mice don't like to be polled for reports,
  841. * so we will always set the HID_QUIRK_NOGET flag for them. */
  842. if (interface->desc.bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT) {
  843. if (interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_KEYBOARD ||
  844. interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_MOUSE)
  845. quirks |= HID_QUIRK_NOGET;
  846. }
  847. if (usb_get_extra_descriptor(interface, HID_DT_HID, &hdesc) &&
  848. (!interface->desc.bNumEndpoints ||
  849. usb_get_extra_descriptor(&interface->endpoint[0], HID_DT_HID, &hdesc))) {
  850. dbg_hid("class descriptor not present\n");
  851. return -ENODEV;
  852. }
  853. if (hdesc->bLength < sizeof(struct hid_descriptor)) {
  854. dbg_hid("hid descriptor is too short\n");
  855. return -EINVAL;
  856. }
  857. hid->version = le16_to_cpu(hdesc->bcdHID);
  858. hid->country = hdesc->bCountryCode;
  859. num_descriptors = min_t(int, hdesc->bNumDescriptors,
  860. (hdesc->bLength - offset) / sizeof(struct hid_class_descriptor));
  861. for (n = 0; n < num_descriptors; n++)
  862. if (hdesc->desc[n].bDescriptorType == HID_DT_REPORT)
  863. rsize = le16_to_cpu(hdesc->desc[n].wDescriptorLength);
  864. if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
  865. dbg_hid("weird size of report descriptor (%u)\n", rsize);
  866. return -EINVAL;
  867. }
  868. if (!(rdesc = kmalloc(rsize, GFP_KERNEL))) {
  869. dbg_hid("couldn't allocate rdesc memory\n");
  870. return -ENOMEM;
  871. }
  872. hid_set_idle(dev, interface->desc.bInterfaceNumber, 0, 0);
  873. ret = hid_get_class_descriptor(dev, interface->desc.bInterfaceNumber,
  874. HID_DT_REPORT, rdesc, rsize);
  875. if (ret < 0) {
  876. dbg_hid("reading report descriptor failed\n");
  877. kfree(rdesc);
  878. goto err;
  879. }
  880. ret = hid_parse_report(hid, rdesc, rsize);
  881. kfree(rdesc);
  882. if (ret) {
  883. dbg_hid("parsing report descriptor failed\n");
  884. goto err;
  885. }
  886. hid->quirks |= quirks;
  887. return 0;
  888. err:
  889. return ret;
  890. }
  891. static int usbhid_start(struct hid_device *hid)
  892. {
  893. struct usb_interface *intf = to_usb_interface(hid->dev.parent);
  894. struct usb_host_interface *interface = intf->cur_altsetting;
  895. struct usb_device *dev = interface_to_usbdev(intf);
  896. struct usbhid_device *usbhid = hid->driver_data;
  897. unsigned int n, insize = 0;
  898. int ret;
  899. clear_bit(HID_DISCONNECTED, &usbhid->iofl);
  900. usbhid->bufsize = HID_MIN_BUFFER_SIZE;
  901. hid_find_max_report(hid, HID_INPUT_REPORT, &usbhid->bufsize);
  902. hid_find_max_report(hid, HID_OUTPUT_REPORT, &usbhid->bufsize);
  903. hid_find_max_report(hid, HID_FEATURE_REPORT, &usbhid->bufsize);
  904. if (usbhid->bufsize > HID_MAX_BUFFER_SIZE)
  905. usbhid->bufsize = HID_MAX_BUFFER_SIZE;
  906. hid_find_max_report(hid, HID_INPUT_REPORT, &insize);
  907. if (insize > HID_MAX_BUFFER_SIZE)
  908. insize = HID_MAX_BUFFER_SIZE;
  909. if (hid_alloc_buffers(dev, hid)) {
  910. ret = -ENOMEM;
  911. goto fail;
  912. }
  913. for (n = 0; n < interface->desc.bNumEndpoints; n++) {
  914. struct usb_endpoint_descriptor *endpoint;
  915. int pipe;
  916. int interval;
  917. endpoint = &interface->endpoint[n].desc;
  918. if (!usb_endpoint_xfer_int(endpoint))
  919. continue;
  920. interval = endpoint->bInterval;
  921. /* Some vendors give fullspeed interval on highspeed devides */
  922. if (hid->quirks & HID_QUIRK_FULLSPEED_INTERVAL &&
  923. dev->speed == USB_SPEED_HIGH) {
  924. interval = fls(endpoint->bInterval*8);
  925. printk(KERN_INFO "%s: Fixing fullspeed to highspeed interval: %d -> %d\n",
  926. hid->name, endpoint->bInterval, interval);
  927. }
  928. /* Change the polling interval of mice. */
  929. if (hid->collection->usage == HID_GD_MOUSE && hid_mousepoll_interval > 0)
  930. interval = hid_mousepoll_interval;
  931. ret = -ENOMEM;
  932. if (usb_endpoint_dir_in(endpoint)) {
  933. if (usbhid->urbin)
  934. continue;
  935. if (!(usbhid->urbin = usb_alloc_urb(0, GFP_KERNEL)))
  936. goto fail;
  937. pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
  938. usb_fill_int_urb(usbhid->urbin, dev, pipe, usbhid->inbuf, insize,
  939. hid_irq_in, hid, interval);
  940. usbhid->urbin->transfer_dma = usbhid->inbuf_dma;
  941. usbhid->urbin->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  942. } else {
  943. if (usbhid->urbout)
  944. continue;
  945. if (!(usbhid->urbout = usb_alloc_urb(0, GFP_KERNEL)))
  946. goto fail;
  947. pipe = usb_sndintpipe(dev, endpoint->bEndpointAddress);
  948. usb_fill_int_urb(usbhid->urbout, dev, pipe, usbhid->outbuf, 0,
  949. hid_irq_out, hid, interval);
  950. usbhid->urbout->transfer_dma = usbhid->outbuf_dma;
  951. usbhid->urbout->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  952. }
  953. }
  954. usbhid->urbctrl = usb_alloc_urb(0, GFP_KERNEL);
  955. if (!usbhid->urbctrl) {
  956. ret = -ENOMEM;
  957. goto fail;
  958. }
  959. usb_fill_control_urb(usbhid->urbctrl, dev, 0, (void *) usbhid->cr,
  960. usbhid->ctrlbuf, 1, hid_ctrl, hid);
  961. usbhid->urbctrl->transfer_dma = usbhid->ctrlbuf_dma;
  962. usbhid->urbctrl->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  963. if (!(hid->quirks & HID_QUIRK_NO_INIT_REPORTS))
  964. usbhid_init_reports(hid);
  965. set_bit(HID_STARTED, &usbhid->iofl);
  966. if (hid->quirks & HID_QUIRK_ALWAYS_POLL) {
  967. ret = usb_autopm_get_interface(usbhid->intf);
  968. if (ret)
  969. goto fail;
  970. usbhid->intf->needs_remote_wakeup = 1;
  971. ret = hid_start_in(hid);
  972. if (ret) {
  973. dev_err(&hid->dev,
  974. "failed to start in urb: %d\n", ret);
  975. }
  976. usb_autopm_put_interface(usbhid->intf);
  977. }
  978. /* Some keyboards don't work until their LEDs have been set.
  979. * Since BIOSes do set the LEDs, it must be safe for any device
  980. * that supports the keyboard boot protocol.
  981. * In addition, enable remote wakeup by default for all keyboard
  982. * devices supporting the boot protocol.
  983. */
  984. if (interface->desc.bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT &&
  985. interface->desc.bInterfaceProtocol ==
  986. USB_INTERFACE_PROTOCOL_KEYBOARD) {
  987. usbhid_set_leds(hid);
  988. device_set_wakeup_enable(&dev->dev, 1);
  989. }
  990. return 0;
  991. fail:
  992. usb_free_urb(usbhid->urbin);
  993. usb_free_urb(usbhid->urbout);
  994. usb_free_urb(usbhid->urbctrl);
  995. usbhid->urbin = NULL;
  996. usbhid->urbout = NULL;
  997. usbhid->urbctrl = NULL;
  998. hid_free_buffers(dev, hid);
  999. return ret;
  1000. }
  1001. static void usbhid_stop(struct hid_device *hid)
  1002. {
  1003. struct usbhid_device *usbhid = hid->driver_data;
  1004. if (WARN_ON(!usbhid))
  1005. return;
  1006. if (hid->quirks & HID_QUIRK_ALWAYS_POLL)
  1007. usbhid->intf->needs_remote_wakeup = 0;
  1008. clear_bit(HID_STARTED, &usbhid->iofl);
  1009. spin_lock_irq(&usbhid->lock); /* Sync with error and led handlers */
  1010. set_bit(HID_DISCONNECTED, &usbhid->iofl);
  1011. spin_unlock_irq(&usbhid->lock);
  1012. usb_kill_urb(usbhid->urbin);
  1013. usb_kill_urb(usbhid->urbout);
  1014. usb_kill_urb(usbhid->urbctrl);
  1015. hid_cancel_delayed_stuff(usbhid);
  1016. hid->claimed = 0;
  1017. usb_free_urb(usbhid->urbin);
  1018. usb_free_urb(usbhid->urbctrl);
  1019. usb_free_urb(usbhid->urbout);
  1020. usbhid->urbin = NULL; /* don't mess up next start */
  1021. usbhid->urbctrl = NULL;
  1022. usbhid->urbout = NULL;
  1023. hid_free_buffers(hid_to_usb_dev(hid), hid);
  1024. }
  1025. static int usbhid_power(struct hid_device *hid, int lvl)
  1026. {
  1027. int r = 0;
  1028. switch (lvl) {
  1029. case PM_HINT_FULLON:
  1030. r = usbhid_get_power(hid);
  1031. break;
  1032. case PM_HINT_NORMAL:
  1033. usbhid_put_power(hid);
  1034. break;
  1035. }
  1036. return r;
  1037. }
  1038. static void usbhid_request(struct hid_device *hid, struct hid_report *rep, int reqtype)
  1039. {
  1040. switch (reqtype) {
  1041. case HID_REQ_GET_REPORT:
  1042. usbhid_submit_report(hid, rep, USB_DIR_IN);
  1043. break;
  1044. case HID_REQ_SET_REPORT:
  1045. usbhid_submit_report(hid, rep, USB_DIR_OUT);
  1046. break;
  1047. }
  1048. }
  1049. static int usbhid_raw_request(struct hid_device *hid, unsigned char reportnum,
  1050. __u8 *buf, size_t len, unsigned char rtype,
  1051. int reqtype)
  1052. {
  1053. switch (reqtype) {
  1054. case HID_REQ_GET_REPORT:
  1055. return usbhid_get_raw_report(hid, reportnum, buf, len, rtype);
  1056. case HID_REQ_SET_REPORT:
  1057. return usbhid_set_raw_report(hid, reportnum, buf, len, rtype);
  1058. default:
  1059. return -EIO;
  1060. }
  1061. }
  1062. static int usbhid_idle(struct hid_device *hid, int report, int idle,
  1063. int reqtype)
  1064. {
  1065. struct usb_device *dev = hid_to_usb_dev(hid);
  1066. struct usb_interface *intf = to_usb_interface(hid->dev.parent);
  1067. struct usb_host_interface *interface = intf->cur_altsetting;
  1068. int ifnum = interface->desc.bInterfaceNumber;
  1069. if (reqtype != HID_REQ_SET_IDLE)
  1070. return -EINVAL;
  1071. return hid_set_idle(dev, ifnum, report, idle);
  1072. }
  1073. static struct hid_ll_driver usb_hid_driver = {
  1074. .parse = usbhid_parse,
  1075. .start = usbhid_start,
  1076. .stop = usbhid_stop,
  1077. .open = usbhid_open,
  1078. .close = usbhid_close,
  1079. .power = usbhid_power,
  1080. .request = usbhid_request,
  1081. .wait = usbhid_wait_io,
  1082. .raw_request = usbhid_raw_request,
  1083. .output_report = usbhid_output_report,
  1084. .idle = usbhid_idle,
  1085. };
  1086. static int usbhid_probe(struct usb_interface *intf, const struct usb_device_id *id)
  1087. {
  1088. struct usb_host_interface *interface = intf->cur_altsetting;
  1089. struct usb_device *dev = interface_to_usbdev(intf);
  1090. struct usbhid_device *usbhid;
  1091. struct hid_device *hid;
  1092. unsigned int n, has_in = 0;
  1093. size_t len;
  1094. int ret;
  1095. dbg_hid("HID probe called for ifnum %d\n",
  1096. intf->altsetting->desc.bInterfaceNumber);
  1097. for (n = 0; n < interface->desc.bNumEndpoints; n++)
  1098. if (usb_endpoint_is_int_in(&interface->endpoint[n].desc))
  1099. has_in++;
  1100. if (!has_in) {
  1101. hid_err(intf, "couldn't find an input interrupt endpoint\n");
  1102. return -ENODEV;
  1103. }
  1104. hid = hid_allocate_device();
  1105. if (IS_ERR(hid))
  1106. return PTR_ERR(hid);
  1107. usb_set_intfdata(intf, hid);
  1108. hid->ll_driver = &usb_hid_driver;
  1109. hid->ff_init = hid_pidff_init;
  1110. #ifdef CONFIG_USB_HIDDEV
  1111. hid->hiddev_connect = hiddev_connect;
  1112. hid->hiddev_disconnect = hiddev_disconnect;
  1113. hid->hiddev_hid_event = hiddev_hid_event;
  1114. hid->hiddev_report_event = hiddev_report_event;
  1115. #endif
  1116. hid->dev.parent = &intf->dev;
  1117. hid->bus = BUS_USB;
  1118. hid->vendor = le16_to_cpu(dev->descriptor.idVendor);
  1119. hid->product = le16_to_cpu(dev->descriptor.idProduct);
  1120. hid->name[0] = 0;
  1121. hid->quirks = usbhid_lookup_quirk(hid->vendor, hid->product);
  1122. if (intf->cur_altsetting->desc.bInterfaceProtocol ==
  1123. USB_INTERFACE_PROTOCOL_MOUSE)
  1124. hid->type = HID_TYPE_USBMOUSE;
  1125. else if (intf->cur_altsetting->desc.bInterfaceProtocol == 0)
  1126. hid->type = HID_TYPE_USBNONE;
  1127. if (dev->manufacturer)
  1128. strlcpy(hid->name, dev->manufacturer, sizeof(hid->name));
  1129. if (dev->product) {
  1130. if (dev->manufacturer)
  1131. strlcat(hid->name, " ", sizeof(hid->name));
  1132. strlcat(hid->name, dev->product, sizeof(hid->name));
  1133. }
  1134. if (!strlen(hid->name))
  1135. snprintf(hid->name, sizeof(hid->name), "HID %04x:%04x",
  1136. le16_to_cpu(dev->descriptor.idVendor),
  1137. le16_to_cpu(dev->descriptor.idProduct));
  1138. usb_make_path(dev, hid->phys, sizeof(hid->phys));
  1139. strlcat(hid->phys, "/input", sizeof(hid->phys));
  1140. len = strlen(hid->phys);
  1141. if (len < sizeof(hid->phys) - 1)
  1142. snprintf(hid->phys + len, sizeof(hid->phys) - len,
  1143. "%d", intf->altsetting[0].desc.bInterfaceNumber);
  1144. if (usb_string(dev, dev->descriptor.iSerialNumber, hid->uniq, 64) <= 0)
  1145. hid->uniq[0] = 0;
  1146. usbhid = kzalloc(sizeof(*usbhid), GFP_KERNEL);
  1147. if (usbhid == NULL) {
  1148. ret = -ENOMEM;
  1149. goto err;
  1150. }
  1151. hid->driver_data = usbhid;
  1152. usbhid->hid = hid;
  1153. usbhid->intf = intf;
  1154. usbhid->ifnum = interface->desc.bInterfaceNumber;
  1155. init_waitqueue_head(&usbhid->wait);
  1156. INIT_WORK(&usbhid->reset_work, hid_reset);
  1157. setup_timer(&usbhid->io_retry, hid_retry_timeout, (unsigned long) hid);
  1158. spin_lock_init(&usbhid->lock);
  1159. ret = hid_add_device(hid);
  1160. if (ret) {
  1161. if (ret != -ENODEV)
  1162. hid_err(intf, "can't add hid device: %d\n", ret);
  1163. goto err_free;
  1164. }
  1165. return 0;
  1166. err_free:
  1167. kfree(usbhid);
  1168. err:
  1169. hid_destroy_device(hid);
  1170. return ret;
  1171. }
  1172. static void usbhid_disconnect(struct usb_interface *intf)
  1173. {
  1174. struct hid_device *hid = usb_get_intfdata(intf);
  1175. struct usbhid_device *usbhid;
  1176. if (WARN_ON(!hid))
  1177. return;
  1178. usbhid = hid->driver_data;
  1179. spin_lock_irq(&usbhid->lock); /* Sync with error and led handlers */
  1180. set_bit(HID_DISCONNECTED, &usbhid->iofl);
  1181. spin_unlock_irq(&usbhid->lock);
  1182. hid_destroy_device(hid);
  1183. kfree(usbhid);
  1184. }
  1185. static void hid_cancel_delayed_stuff(struct usbhid_device *usbhid)
  1186. {
  1187. del_timer_sync(&usbhid->io_retry);
  1188. cancel_work_sync(&usbhid->reset_work);
  1189. }
  1190. static void hid_cease_io(struct usbhid_device *usbhid)
  1191. {
  1192. del_timer_sync(&usbhid->io_retry);
  1193. usb_kill_urb(usbhid->urbin);
  1194. usb_kill_urb(usbhid->urbctrl);
  1195. usb_kill_urb(usbhid->urbout);
  1196. }
  1197. static void hid_restart_io(struct hid_device *hid)
  1198. {
  1199. struct usbhid_device *usbhid = hid->driver_data;
  1200. int clear_halt = test_bit(HID_CLEAR_HALT, &usbhid->iofl);
  1201. int reset_pending = test_bit(HID_RESET_PENDING, &usbhid->iofl);
  1202. spin_lock_irq(&usbhid->lock);
  1203. clear_bit(HID_SUSPENDED, &usbhid->iofl);
  1204. usbhid_mark_busy(usbhid);
  1205. if (clear_halt || reset_pending)
  1206. schedule_work(&usbhid->reset_work);
  1207. usbhid->retry_delay = 0;
  1208. spin_unlock_irq(&usbhid->lock);
  1209. if (reset_pending || !test_bit(HID_STARTED, &usbhid->iofl))
  1210. return;
  1211. if (!clear_halt) {
  1212. if (hid_start_in(hid) < 0)
  1213. hid_io_error(hid);
  1214. }
  1215. spin_lock_irq(&usbhid->lock);
  1216. if (usbhid->urbout && !test_bit(HID_OUT_RUNNING, &usbhid->iofl))
  1217. usbhid_restart_out_queue(usbhid);
  1218. if (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl))
  1219. usbhid_restart_ctrl_queue(usbhid);
  1220. spin_unlock_irq(&usbhid->lock);
  1221. }
  1222. /* Treat USB reset pretty much the same as suspend/resume */
  1223. static int hid_pre_reset(struct usb_interface *intf)
  1224. {
  1225. struct hid_device *hid = usb_get_intfdata(intf);
  1226. struct usbhid_device *usbhid = hid->driver_data;
  1227. spin_lock_irq(&usbhid->lock);
  1228. set_bit(HID_RESET_PENDING, &usbhid->iofl);
  1229. spin_unlock_irq(&usbhid->lock);
  1230. hid_cease_io(usbhid);
  1231. return 0;
  1232. }
  1233. /* Same routine used for post_reset and reset_resume */
  1234. static int hid_post_reset(struct usb_interface *intf)
  1235. {
  1236. struct usb_device *dev = interface_to_usbdev (intf);
  1237. struct hid_device *hid = usb_get_intfdata(intf);
  1238. struct usbhid_device *usbhid = hid->driver_data;
  1239. struct usb_host_interface *interface = intf->cur_altsetting;
  1240. int status;
  1241. char *rdesc;
  1242. /* Fetch and examine the HID report descriptor. If this
  1243. * has changed, then rebind. Since usbcore's check of the
  1244. * configuration descriptors passed, we already know that
  1245. * the size of the HID report descriptor has not changed.
  1246. */
  1247. rdesc = kmalloc(hid->dev_rsize, GFP_KERNEL);
  1248. if (!rdesc) {
  1249. dbg_hid("couldn't allocate rdesc memory (post_reset)\n");
  1250. return 1;
  1251. }
  1252. status = hid_get_class_descriptor(dev,
  1253. interface->desc.bInterfaceNumber,
  1254. HID_DT_REPORT, rdesc, hid->dev_rsize);
  1255. if (status < 0) {
  1256. dbg_hid("reading report descriptor failed (post_reset)\n");
  1257. kfree(rdesc);
  1258. return 1;
  1259. }
  1260. status = memcmp(rdesc, hid->dev_rdesc, hid->dev_rsize);
  1261. kfree(rdesc);
  1262. if (status != 0) {
  1263. dbg_hid("report descriptor changed\n");
  1264. return 1;
  1265. }
  1266. /* No need to do another reset or clear a halted endpoint */
  1267. spin_lock_irq(&usbhid->lock);
  1268. clear_bit(HID_RESET_PENDING, &usbhid->iofl);
  1269. clear_bit(HID_CLEAR_HALT, &usbhid->iofl);
  1270. spin_unlock_irq(&usbhid->lock);
  1271. hid_set_idle(dev, intf->cur_altsetting->desc.bInterfaceNumber, 0, 0);
  1272. hid_restart_io(hid);
  1273. return 0;
  1274. }
  1275. int usbhid_get_power(struct hid_device *hid)
  1276. {
  1277. struct usbhid_device *usbhid = hid->driver_data;
  1278. return usb_autopm_get_interface(usbhid->intf);
  1279. }
  1280. void usbhid_put_power(struct hid_device *hid)
  1281. {
  1282. struct usbhid_device *usbhid = hid->driver_data;
  1283. usb_autopm_put_interface(usbhid->intf);
  1284. }
  1285. #ifdef CONFIG_PM
  1286. static int hid_resume_common(struct hid_device *hid, bool driver_suspended)
  1287. {
  1288. int status = 0;
  1289. hid_restart_io(hid);
  1290. if (driver_suspended && hid->driver && hid->driver->resume)
  1291. status = hid->driver->resume(hid);
  1292. return status;
  1293. }
  1294. static int hid_suspend(struct usb_interface *intf, pm_message_t message)
  1295. {
  1296. struct hid_device *hid = usb_get_intfdata(intf);
  1297. struct usbhid_device *usbhid = hid->driver_data;
  1298. int status = 0;
  1299. bool driver_suspended = false;
  1300. unsigned int ledcount;
  1301. if (PMSG_IS_AUTO(message)) {
  1302. ledcount = hidinput_count_leds(hid);
  1303. spin_lock_irq(&usbhid->lock); /* Sync with error handler */
  1304. if (!test_bit(HID_RESET_PENDING, &usbhid->iofl)
  1305. && !test_bit(HID_CLEAR_HALT, &usbhid->iofl)
  1306. && !test_bit(HID_OUT_RUNNING, &usbhid->iofl)
  1307. && !test_bit(HID_CTRL_RUNNING, &usbhid->iofl)
  1308. && !test_bit(HID_KEYS_PRESSED, &usbhid->iofl)
  1309. && (!ledcount || ignoreled))
  1310. {
  1311. set_bit(HID_SUSPENDED, &usbhid->iofl);
  1312. spin_unlock_irq(&usbhid->lock);
  1313. if (hid->driver && hid->driver->suspend) {
  1314. status = hid->driver->suspend(hid, message);
  1315. if (status < 0)
  1316. goto failed;
  1317. }
  1318. driver_suspended = true;
  1319. } else {
  1320. usbhid_mark_busy(usbhid);
  1321. spin_unlock_irq(&usbhid->lock);
  1322. return -EBUSY;
  1323. }
  1324. } else {
  1325. /* TODO: resume() might need to handle suspend failure */
  1326. if (hid->driver && hid->driver->suspend)
  1327. status = hid->driver->suspend(hid, message);
  1328. driver_suspended = true;
  1329. spin_lock_irq(&usbhid->lock);
  1330. set_bit(HID_SUSPENDED, &usbhid->iofl);
  1331. spin_unlock_irq(&usbhid->lock);
  1332. if (usbhid_wait_io(hid) < 0)
  1333. status = -EIO;
  1334. }
  1335. hid_cancel_delayed_stuff(usbhid);
  1336. hid_cease_io(usbhid);
  1337. if (PMSG_IS_AUTO(message) && test_bit(HID_KEYS_PRESSED, &usbhid->iofl)) {
  1338. /* lost race against keypresses */
  1339. status = -EBUSY;
  1340. goto failed;
  1341. }
  1342. dev_dbg(&intf->dev, "suspend\n");
  1343. return status;
  1344. failed:
  1345. hid_resume_common(hid, driver_suspended);
  1346. return status;
  1347. }
  1348. static int hid_resume(struct usb_interface *intf)
  1349. {
  1350. struct hid_device *hid = usb_get_intfdata (intf);
  1351. int status;
  1352. status = hid_resume_common(hid, true);
  1353. dev_dbg(&intf->dev, "resume status %d\n", status);
  1354. return 0;
  1355. }
  1356. static int hid_reset_resume(struct usb_interface *intf)
  1357. {
  1358. struct hid_device *hid = usb_get_intfdata(intf);
  1359. int status;
  1360. status = hid_post_reset(intf);
  1361. if (status >= 0 && hid->driver && hid->driver->reset_resume) {
  1362. int ret = hid->driver->reset_resume(hid);
  1363. if (ret < 0)
  1364. status = ret;
  1365. }
  1366. return status;
  1367. }
  1368. #endif /* CONFIG_PM */
  1369. static const struct usb_device_id hid_usb_ids[] = {
  1370. { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
  1371. .bInterfaceClass = USB_INTERFACE_CLASS_HID },
  1372. { } /* Terminating entry */
  1373. };
  1374. MODULE_DEVICE_TABLE (usb, hid_usb_ids);
  1375. static struct usb_driver hid_driver = {
  1376. .name = "usbhid",
  1377. .probe = usbhid_probe,
  1378. .disconnect = usbhid_disconnect,
  1379. #ifdef CONFIG_PM
  1380. .suspend = hid_suspend,
  1381. .resume = hid_resume,
  1382. .reset_resume = hid_reset_resume,
  1383. #endif
  1384. .pre_reset = hid_pre_reset,
  1385. .post_reset = hid_post_reset,
  1386. .id_table = hid_usb_ids,
  1387. .supports_autosuspend = 1,
  1388. };
  1389. struct usb_interface *usbhid_find_interface(int minor)
  1390. {
  1391. return usb_find_interface(&hid_driver, minor);
  1392. }
  1393. static int __init hid_init(void)
  1394. {
  1395. int retval = -ENOMEM;
  1396. retval = usbhid_quirks_init(quirks_param);
  1397. if (retval)
  1398. goto usbhid_quirks_init_fail;
  1399. retval = usb_register(&hid_driver);
  1400. if (retval)
  1401. goto usb_register_fail;
  1402. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n");
  1403. return 0;
  1404. usb_register_fail:
  1405. usbhid_quirks_exit();
  1406. usbhid_quirks_init_fail:
  1407. return retval;
  1408. }
  1409. static void __exit hid_exit(void)
  1410. {
  1411. usb_deregister(&hid_driver);
  1412. usbhid_quirks_exit();
  1413. }
  1414. module_init(hid_init);
  1415. module_exit(hid_exit);
  1416. MODULE_AUTHOR("Andreas Gal");
  1417. MODULE_AUTHOR("Vojtech Pavlik");
  1418. MODULE_AUTHOR("Jiri Kosina");
  1419. MODULE_DESCRIPTION(DRIVER_DESC);
  1420. MODULE_LICENSE(DRIVER_LICENSE);