hid-core.c 43 KB

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