hid-core.c 44 KB

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