hid-core.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638
  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. memcpy(usbhid->outbuf, raw_report,
  290. usbhid->urbout->transfer_buffer_length);
  291. kfree(raw_report);
  292. dbg_hid("submitting out urb\n");
  293. r = usb_submit_urb(usbhid->urbout, GFP_ATOMIC);
  294. if (r < 0) {
  295. hid_err(hid, "usb_submit_urb(out) failed: %d\n", r);
  296. return r;
  297. }
  298. usbhid->last_out = jiffies;
  299. return 0;
  300. }
  301. static int hid_submit_ctrl(struct hid_device *hid)
  302. {
  303. struct hid_report *report;
  304. unsigned char dir;
  305. char *raw_report;
  306. int len, r;
  307. struct usbhid_device *usbhid = hid->driver_data;
  308. report = usbhid->ctrl[usbhid->ctrltail].report;
  309. raw_report = usbhid->ctrl[usbhid->ctrltail].raw_report;
  310. dir = usbhid->ctrl[usbhid->ctrltail].dir;
  311. len = ((report->size - 1) >> 3) + 1 + (report->id > 0);
  312. if (dir == USB_DIR_OUT) {
  313. usbhid->urbctrl->pipe = usb_sndctrlpipe(hid_to_usb_dev(hid), 0);
  314. usbhid->urbctrl->transfer_buffer_length = len;
  315. memcpy(usbhid->ctrlbuf, raw_report, len);
  316. kfree(raw_report);
  317. } else {
  318. int maxpacket, padlen;
  319. usbhid->urbctrl->pipe = usb_rcvctrlpipe(hid_to_usb_dev(hid), 0);
  320. maxpacket = usb_maxpacket(hid_to_usb_dev(hid),
  321. usbhid->urbctrl->pipe, 0);
  322. if (maxpacket > 0) {
  323. padlen = DIV_ROUND_UP(len, maxpacket);
  324. padlen *= maxpacket;
  325. if (padlen > usbhid->bufsize)
  326. padlen = usbhid->bufsize;
  327. } else
  328. padlen = 0;
  329. usbhid->urbctrl->transfer_buffer_length = padlen;
  330. }
  331. usbhid->urbctrl->dev = hid_to_usb_dev(hid);
  332. usbhid->cr->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE | dir;
  333. usbhid->cr->bRequest = (dir == USB_DIR_OUT) ? HID_REQ_SET_REPORT :
  334. HID_REQ_GET_REPORT;
  335. usbhid->cr->wValue = cpu_to_le16(((report->type + 1) << 8) |
  336. report->id);
  337. usbhid->cr->wIndex = cpu_to_le16(usbhid->ifnum);
  338. usbhid->cr->wLength = cpu_to_le16(len);
  339. dbg_hid("submitting ctrl urb: %s wValue=0x%04x wIndex=0x%04x wLength=%u\n",
  340. usbhid->cr->bRequest == HID_REQ_SET_REPORT ? "Set_Report" :
  341. "Get_Report",
  342. usbhid->cr->wValue, usbhid->cr->wIndex, usbhid->cr->wLength);
  343. r = usb_submit_urb(usbhid->urbctrl, GFP_ATOMIC);
  344. if (r < 0) {
  345. hid_err(hid, "usb_submit_urb(ctrl) failed: %d\n", r);
  346. return r;
  347. }
  348. usbhid->last_ctrl = jiffies;
  349. return 0;
  350. }
  351. /*
  352. * Output interrupt completion handler.
  353. */
  354. static int irq_out_pump_restart(struct hid_device *hid)
  355. {
  356. struct usbhid_device *usbhid = hid->driver_data;
  357. if (usbhid->outhead != usbhid->outtail)
  358. return hid_submit_out(hid);
  359. else
  360. return -1;
  361. }
  362. static void hid_irq_out(struct urb *urb)
  363. {
  364. struct hid_device *hid = urb->context;
  365. struct usbhid_device *usbhid = hid->driver_data;
  366. unsigned long flags;
  367. int unplug = 0;
  368. switch (urb->status) {
  369. case 0: /* success */
  370. break;
  371. case -ESHUTDOWN: /* unplug */
  372. unplug = 1;
  373. case -EILSEQ: /* protocol error or unplug */
  374. case -EPROTO: /* protocol error or unplug */
  375. case -ECONNRESET: /* unlink */
  376. case -ENOENT:
  377. break;
  378. default: /* error */
  379. hid_warn(urb->dev, "output irq status %d received\n",
  380. urb->status);
  381. }
  382. spin_lock_irqsave(&usbhid->lock, flags);
  383. if (unplug)
  384. usbhid->outtail = usbhid->outhead;
  385. else
  386. usbhid->outtail = (usbhid->outtail + 1) & (HID_OUTPUT_FIFO_SIZE - 1);
  387. if (!irq_out_pump_restart(hid)) {
  388. /* Successfully submitted next urb in queue */
  389. spin_unlock_irqrestore(&usbhid->lock, flags);
  390. return;
  391. }
  392. clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
  393. spin_unlock_irqrestore(&usbhid->lock, flags);
  394. usb_autopm_put_interface_async(usbhid->intf);
  395. wake_up(&usbhid->wait);
  396. }
  397. /*
  398. * Control pipe completion handler.
  399. */
  400. static int ctrl_pump_restart(struct hid_device *hid)
  401. {
  402. struct usbhid_device *usbhid = hid->driver_data;
  403. if (usbhid->ctrlhead != usbhid->ctrltail)
  404. return hid_submit_ctrl(hid);
  405. else
  406. return -1;
  407. }
  408. static void hid_ctrl(struct urb *urb)
  409. {
  410. struct hid_device *hid = urb->context;
  411. struct usbhid_device *usbhid = hid->driver_data;
  412. int unplug = 0, status = urb->status;
  413. spin_lock(&usbhid->lock);
  414. switch (status) {
  415. case 0: /* success */
  416. if (usbhid->ctrl[usbhid->ctrltail].dir == USB_DIR_IN)
  417. hid_input_report(urb->context,
  418. usbhid->ctrl[usbhid->ctrltail].report->type,
  419. urb->transfer_buffer, urb->actual_length, 0);
  420. break;
  421. case -ESHUTDOWN: /* unplug */
  422. unplug = 1;
  423. case -EILSEQ: /* protocol error or unplug */
  424. case -EPROTO: /* protocol error or unplug */
  425. case -ECONNRESET: /* unlink */
  426. case -ENOENT:
  427. case -EPIPE: /* report not available */
  428. break;
  429. default: /* error */
  430. hid_warn(urb->dev, "ctrl urb status %d received\n", status);
  431. }
  432. if (unplug)
  433. usbhid->ctrltail = usbhid->ctrlhead;
  434. else
  435. usbhid->ctrltail = (usbhid->ctrltail + 1) & (HID_CONTROL_FIFO_SIZE - 1);
  436. if (!ctrl_pump_restart(hid)) {
  437. /* Successfully submitted next urb in queue */
  438. spin_unlock(&usbhid->lock);
  439. return;
  440. }
  441. clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
  442. spin_unlock(&usbhid->lock);
  443. usb_autopm_put_interface_async(usbhid->intf);
  444. wake_up(&usbhid->wait);
  445. }
  446. static void __usbhid_submit_report(struct hid_device *hid, struct hid_report *report,
  447. unsigned char dir)
  448. {
  449. int head;
  450. struct usbhid_device *usbhid = hid->driver_data;
  451. int len = ((report->size - 1) >> 3) + 1 + (report->id > 0);
  452. if ((hid->quirks & HID_QUIRK_NOGET) && dir == USB_DIR_IN)
  453. return;
  454. if (usbhid->urbout && dir == USB_DIR_OUT && report->type == HID_OUTPUT_REPORT) {
  455. if ((head = (usbhid->outhead + 1) & (HID_OUTPUT_FIFO_SIZE - 1)) == usbhid->outtail) {
  456. hid_warn(hid, "output queue full\n");
  457. return;
  458. }
  459. usbhid->out[usbhid->outhead].raw_report = kmalloc(len, GFP_ATOMIC);
  460. if (!usbhid->out[usbhid->outhead].raw_report) {
  461. hid_warn(hid, "output queueing failed\n");
  462. return;
  463. }
  464. hid_output_report(report, usbhid->out[usbhid->outhead].raw_report);
  465. usbhid->out[usbhid->outhead].report = report;
  466. usbhid->outhead = head;
  467. /* Try to awake from autosuspend... */
  468. if (usb_autopm_get_interface_async(usbhid->intf) < 0)
  469. return;
  470. /*
  471. * But if still suspended, leave urb enqueued, don't submit.
  472. * Submission will occur if/when resume() drains the queue.
  473. */
  474. if (test_bit(HID_REPORTED_IDLE, &usbhid->iofl))
  475. return;
  476. if (!test_and_set_bit(HID_OUT_RUNNING, &usbhid->iofl)) {
  477. if (hid_submit_out(hid)) {
  478. clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
  479. usb_autopm_put_interface_async(usbhid->intf);
  480. }
  481. wake_up(&usbhid->wait);
  482. } else {
  483. /*
  484. * the queue is known to run
  485. * but an earlier request may be stuck
  486. * we may need to time out
  487. * no race because the URB is blocked under
  488. * spinlock
  489. */
  490. if (time_after(jiffies, usbhid->last_out + HZ * 5)) {
  491. usb_block_urb(usbhid->urbout);
  492. /* drop lock to not deadlock if the callback is called */
  493. spin_unlock(&usbhid->lock);
  494. usb_unlink_urb(usbhid->urbout);
  495. spin_lock(&usbhid->lock);
  496. usb_unblock_urb(usbhid->urbout);
  497. /*
  498. * if the unlinking has already completed
  499. * the pump will have been stopped
  500. * it must be restarted now
  501. */
  502. if (!test_bit(HID_OUT_RUNNING, &usbhid->iofl))
  503. if (!irq_out_pump_restart(hid))
  504. set_bit(HID_OUT_RUNNING, &usbhid->iofl);
  505. }
  506. }
  507. return;
  508. }
  509. if ((head = (usbhid->ctrlhead + 1) & (HID_CONTROL_FIFO_SIZE - 1)) == usbhid->ctrltail) {
  510. hid_warn(hid, "control queue full\n");
  511. return;
  512. }
  513. if (dir == USB_DIR_OUT) {
  514. usbhid->ctrl[usbhid->ctrlhead].raw_report = kmalloc(len, GFP_ATOMIC);
  515. if (!usbhid->ctrl[usbhid->ctrlhead].raw_report) {
  516. hid_warn(hid, "control queueing failed\n");
  517. return;
  518. }
  519. hid_output_report(report, usbhid->ctrl[usbhid->ctrlhead].raw_report);
  520. }
  521. usbhid->ctrl[usbhid->ctrlhead].report = report;
  522. usbhid->ctrl[usbhid->ctrlhead].dir = dir;
  523. usbhid->ctrlhead = head;
  524. /* Try to awake from autosuspend... */
  525. if (usb_autopm_get_interface_async(usbhid->intf) < 0)
  526. return;
  527. /*
  528. * If already suspended, leave urb enqueued, but don't submit.
  529. * Submission will occur if/when resume() drains the queue.
  530. */
  531. if (test_bit(HID_REPORTED_IDLE, &usbhid->iofl))
  532. return;
  533. if (!test_and_set_bit(HID_CTRL_RUNNING, &usbhid->iofl)) {
  534. if (hid_submit_ctrl(hid)) {
  535. clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
  536. usb_autopm_put_interface_async(usbhid->intf);
  537. }
  538. wake_up(&usbhid->wait);
  539. } else {
  540. /*
  541. * the queue is known to run
  542. * but an earlier request may be stuck
  543. * we may need to time out
  544. * no race because the URB is blocked under
  545. * spinlock
  546. */
  547. if (time_after(jiffies, usbhid->last_ctrl + HZ * 5)) {
  548. usb_block_urb(usbhid->urbctrl);
  549. /* drop lock to not deadlock if the callback is called */
  550. spin_unlock(&usbhid->lock);
  551. usb_unlink_urb(usbhid->urbctrl);
  552. spin_lock(&usbhid->lock);
  553. usb_unblock_urb(usbhid->urbctrl);
  554. /*
  555. * if the unlinking has already completed
  556. * the pump will have been stopped
  557. * it must be restarted now
  558. */
  559. if (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl))
  560. if (!ctrl_pump_restart(hid))
  561. set_bit(HID_CTRL_RUNNING, &usbhid->iofl);
  562. }
  563. }
  564. }
  565. void usbhid_submit_report(struct hid_device *hid, struct hid_report *report, unsigned char dir)
  566. {
  567. struct usbhid_device *usbhid = hid->driver_data;
  568. unsigned long flags;
  569. spin_lock_irqsave(&usbhid->lock, flags);
  570. __usbhid_submit_report(hid, report, dir);
  571. spin_unlock_irqrestore(&usbhid->lock, flags);
  572. }
  573. EXPORT_SYMBOL_GPL(usbhid_submit_report);
  574. /* Workqueue routine to send requests to change LEDs */
  575. static void hid_led(struct work_struct *work)
  576. {
  577. struct usbhid_device *usbhid =
  578. container_of(work, struct usbhid_device, led_work);
  579. struct hid_device *hid = usbhid->hid;
  580. struct hid_field *field;
  581. unsigned long flags;
  582. field = hidinput_get_led_field(hid);
  583. if (!field) {
  584. hid_warn(hid, "LED event field not found\n");
  585. return;
  586. }
  587. spin_lock_irqsave(&usbhid->lock, flags);
  588. if (!test_bit(HID_DISCONNECTED, &usbhid->iofl)) {
  589. usbhid->ledcount = hidinput_count_leds(hid);
  590. hid_dbg(usbhid->hid, "New ledcount = %u\n", usbhid->ledcount);
  591. __usbhid_submit_report(hid, field->report, USB_DIR_OUT);
  592. }
  593. spin_unlock_irqrestore(&usbhid->lock, flags);
  594. }
  595. static int usb_hidinput_input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
  596. {
  597. struct hid_device *hid = input_get_drvdata(dev);
  598. struct usbhid_device *usbhid = hid->driver_data;
  599. struct hid_field *field;
  600. unsigned long flags;
  601. int offset;
  602. if (type == EV_FF)
  603. return input_ff_event(dev, type, code, value);
  604. if (type != EV_LED)
  605. return -1;
  606. if ((offset = hidinput_find_field(hid, type, code, &field)) == -1) {
  607. hid_warn(dev, "event field not found\n");
  608. return -1;
  609. }
  610. spin_lock_irqsave(&usbhid->lock, flags);
  611. hid_set_field(field, offset, value);
  612. spin_unlock_irqrestore(&usbhid->lock, flags);
  613. /*
  614. * Defer performing requested LED action.
  615. * This is more likely gather all LED changes into a single URB.
  616. */
  617. schedule_work(&usbhid->led_work);
  618. return 0;
  619. }
  620. int usbhid_wait_io(struct hid_device *hid)
  621. {
  622. struct usbhid_device *usbhid = hid->driver_data;
  623. if (!wait_event_timeout(usbhid->wait,
  624. (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl) &&
  625. !test_bit(HID_OUT_RUNNING, &usbhid->iofl)),
  626. 10*HZ)) {
  627. dbg_hid("timeout waiting for ctrl or out queue to clear\n");
  628. return -1;
  629. }
  630. return 0;
  631. }
  632. EXPORT_SYMBOL_GPL(usbhid_wait_io);
  633. static int hid_set_idle(struct usb_device *dev, int ifnum, int report, int idle)
  634. {
  635. return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  636. HID_REQ_SET_IDLE, USB_TYPE_CLASS | USB_RECIP_INTERFACE, (idle << 8) | report,
  637. ifnum, NULL, 0, USB_CTRL_SET_TIMEOUT);
  638. }
  639. static int hid_get_class_descriptor(struct usb_device *dev, int ifnum,
  640. unsigned char type, void *buf, int size)
  641. {
  642. int result, retries = 4;
  643. memset(buf, 0, size);
  644. do {
  645. result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  646. USB_REQ_GET_DESCRIPTOR, USB_RECIP_INTERFACE | USB_DIR_IN,
  647. (type << 8), ifnum, buf, size, USB_CTRL_GET_TIMEOUT);
  648. retries--;
  649. } while (result < size && retries);
  650. return result;
  651. }
  652. int usbhid_open(struct hid_device *hid)
  653. {
  654. struct usbhid_device *usbhid = hid->driver_data;
  655. int res = 0;
  656. mutex_lock(&hid_open_mut);
  657. if (!hid->open++) {
  658. res = usb_autopm_get_interface(usbhid->intf);
  659. /* the device must be awake to reliably request remote wakeup */
  660. if (res < 0) {
  661. hid->open--;
  662. res = -EIO;
  663. goto done;
  664. }
  665. usbhid->intf->needs_remote_wakeup = 1;
  666. res = hid_start_in(hid);
  667. if (res) {
  668. if (res != -ENOSPC) {
  669. hid_io_error(hid);
  670. res = 0;
  671. } else {
  672. /* no use opening if resources are insufficient */
  673. hid->open--;
  674. res = -EBUSY;
  675. usbhid->intf->needs_remote_wakeup = 0;
  676. }
  677. }
  678. usb_autopm_put_interface(usbhid->intf);
  679. }
  680. done:
  681. mutex_unlock(&hid_open_mut);
  682. return res;
  683. }
  684. void usbhid_close(struct hid_device *hid)
  685. {
  686. struct usbhid_device *usbhid = hid->driver_data;
  687. mutex_lock(&hid_open_mut);
  688. /* protecting hid->open to make sure we don't restart
  689. * data acquistion due to a resumption we no longer
  690. * care about
  691. */
  692. spin_lock_irq(&usbhid->lock);
  693. if (!--hid->open) {
  694. spin_unlock_irq(&usbhid->lock);
  695. hid_cancel_delayed_stuff(usbhid);
  696. usb_kill_urb(usbhid->urbin);
  697. usbhid->intf->needs_remote_wakeup = 0;
  698. } else {
  699. spin_unlock_irq(&usbhid->lock);
  700. }
  701. mutex_unlock(&hid_open_mut);
  702. }
  703. /*
  704. * Initialize all reports
  705. */
  706. void usbhid_init_reports(struct hid_device *hid)
  707. {
  708. struct hid_report *report;
  709. struct usbhid_device *usbhid = hid->driver_data;
  710. int err, ret;
  711. list_for_each_entry(report, &hid->report_enum[HID_INPUT_REPORT].report_list, list)
  712. usbhid_submit_report(hid, report, USB_DIR_IN);
  713. list_for_each_entry(report, &hid->report_enum[HID_FEATURE_REPORT].report_list, list)
  714. usbhid_submit_report(hid, report, USB_DIR_IN);
  715. err = 0;
  716. ret = usbhid_wait_io(hid);
  717. while (ret) {
  718. err |= ret;
  719. if (test_bit(HID_CTRL_RUNNING, &usbhid->iofl))
  720. usb_kill_urb(usbhid->urbctrl);
  721. if (test_bit(HID_OUT_RUNNING, &usbhid->iofl))
  722. usb_kill_urb(usbhid->urbout);
  723. ret = usbhid_wait_io(hid);
  724. }
  725. if (err)
  726. hid_warn(hid, "timeout initializing reports\n");
  727. }
  728. /*
  729. * Reset LEDs which BIOS might have left on. For now, just NumLock (0x01).
  730. */
  731. static int hid_find_field_early(struct hid_device *hid, unsigned int page,
  732. unsigned int hid_code, struct hid_field **pfield)
  733. {
  734. struct hid_report *report;
  735. struct hid_field *field;
  736. struct hid_usage *usage;
  737. int i, j;
  738. list_for_each_entry(report, &hid->report_enum[HID_OUTPUT_REPORT].report_list, list) {
  739. for (i = 0; i < report->maxfield; i++) {
  740. field = report->field[i];
  741. for (j = 0; j < field->maxusage; j++) {
  742. usage = &field->usage[j];
  743. if ((usage->hid & HID_USAGE_PAGE) == page &&
  744. (usage->hid & 0xFFFF) == hid_code) {
  745. *pfield = field;
  746. return j;
  747. }
  748. }
  749. }
  750. }
  751. return -1;
  752. }
  753. void usbhid_set_leds(struct hid_device *hid)
  754. {
  755. struct hid_field *field;
  756. int offset;
  757. if ((offset = hid_find_field_early(hid, HID_UP_LED, 0x01, &field)) != -1) {
  758. hid_set_field(field, offset, 0);
  759. usbhid_submit_report(hid, field->report, USB_DIR_OUT);
  760. }
  761. }
  762. EXPORT_SYMBOL_GPL(usbhid_set_leds);
  763. /*
  764. * Traverse the supplied list of reports and find the longest
  765. */
  766. static void hid_find_max_report(struct hid_device *hid, unsigned int type,
  767. unsigned int *max)
  768. {
  769. struct hid_report *report;
  770. unsigned int size;
  771. list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
  772. size = ((report->size - 1) >> 3) + 1 + hid->report_enum[type].numbered;
  773. if (*max < size)
  774. *max = size;
  775. }
  776. }
  777. static int hid_alloc_buffers(struct usb_device *dev, struct hid_device *hid)
  778. {
  779. struct usbhid_device *usbhid = hid->driver_data;
  780. usbhid->inbuf = usb_alloc_coherent(dev, usbhid->bufsize, GFP_KERNEL,
  781. &usbhid->inbuf_dma);
  782. usbhid->outbuf = usb_alloc_coherent(dev, usbhid->bufsize, GFP_KERNEL,
  783. &usbhid->outbuf_dma);
  784. usbhid->cr = kmalloc(sizeof(*usbhid->cr), GFP_KERNEL);
  785. usbhid->ctrlbuf = usb_alloc_coherent(dev, usbhid->bufsize, GFP_KERNEL,
  786. &usbhid->ctrlbuf_dma);
  787. if (!usbhid->inbuf || !usbhid->outbuf || !usbhid->cr ||
  788. !usbhid->ctrlbuf)
  789. return -1;
  790. return 0;
  791. }
  792. static int usbhid_get_raw_report(struct hid_device *hid,
  793. unsigned char report_number, __u8 *buf, size_t count,
  794. unsigned char report_type)
  795. {
  796. struct usbhid_device *usbhid = hid->driver_data;
  797. struct usb_device *dev = hid_to_usb_dev(hid);
  798. struct usb_interface *intf = usbhid->intf;
  799. struct usb_host_interface *interface = intf->cur_altsetting;
  800. int skipped_report_id = 0;
  801. int ret;
  802. /* Byte 0 is the report number. Report data starts at byte 1.*/
  803. buf[0] = report_number;
  804. if (report_number == 0x0) {
  805. /* Offset the return buffer by 1, so that the report ID
  806. will remain in byte 0. */
  807. buf++;
  808. count--;
  809. skipped_report_id = 1;
  810. }
  811. ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  812. HID_REQ_GET_REPORT,
  813. USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  814. ((report_type + 1) << 8) | report_number,
  815. interface->desc.bInterfaceNumber, buf, count,
  816. USB_CTRL_SET_TIMEOUT);
  817. /* count also the report id */
  818. if (ret > 0 && skipped_report_id)
  819. ret++;
  820. return ret;
  821. }
  822. static int usbhid_output_raw_report(struct hid_device *hid, __u8 *buf, size_t count,
  823. unsigned char report_type)
  824. {
  825. struct usbhid_device *usbhid = hid->driver_data;
  826. struct usb_device *dev = hid_to_usb_dev(hid);
  827. struct usb_interface *intf = usbhid->intf;
  828. struct usb_host_interface *interface = intf->cur_altsetting;
  829. int ret;
  830. if (usbhid->urbout && report_type != HID_FEATURE_REPORT) {
  831. int actual_length;
  832. int skipped_report_id = 0;
  833. if (buf[0] == 0x0) {
  834. /* Don't send the Report ID */
  835. buf++;
  836. count--;
  837. skipped_report_id = 1;
  838. }
  839. ret = usb_interrupt_msg(dev, usbhid->urbout->pipe,
  840. buf, count, &actual_length,
  841. USB_CTRL_SET_TIMEOUT);
  842. /* return the number of bytes transferred */
  843. if (ret == 0) {
  844. ret = actual_length;
  845. /* count also the report id */
  846. if (skipped_report_id)
  847. ret++;
  848. }
  849. } else {
  850. int skipped_report_id = 0;
  851. int report_id = buf[0];
  852. if (buf[0] == 0x0) {
  853. /* Don't send the Report ID */
  854. buf++;
  855. count--;
  856. skipped_report_id = 1;
  857. }
  858. ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  859. HID_REQ_SET_REPORT,
  860. USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  861. ((report_type + 1) << 8) | report_id,
  862. interface->desc.bInterfaceNumber, buf, count,
  863. USB_CTRL_SET_TIMEOUT);
  864. /* count also the report id, if this was a numbered report. */
  865. if (ret > 0 && skipped_report_id)
  866. ret++;
  867. }
  868. return ret;
  869. }
  870. static void usbhid_restart_queues(struct usbhid_device *usbhid)
  871. {
  872. if (usbhid->urbout)
  873. usbhid_restart_out_queue(usbhid);
  874. usbhid_restart_ctrl_queue(usbhid);
  875. }
  876. static void hid_free_buffers(struct usb_device *dev, struct hid_device *hid)
  877. {
  878. struct usbhid_device *usbhid = hid->driver_data;
  879. usb_free_coherent(dev, usbhid->bufsize, usbhid->inbuf, usbhid->inbuf_dma);
  880. usb_free_coherent(dev, usbhid->bufsize, usbhid->outbuf, usbhid->outbuf_dma);
  881. kfree(usbhid->cr);
  882. usb_free_coherent(dev, usbhid->bufsize, usbhid->ctrlbuf, usbhid->ctrlbuf_dma);
  883. }
  884. static int usbhid_parse(struct hid_device *hid)
  885. {
  886. struct usb_interface *intf = to_usb_interface(hid->dev.parent);
  887. struct usb_host_interface *interface = intf->cur_altsetting;
  888. struct usb_device *dev = interface_to_usbdev (intf);
  889. struct hid_descriptor *hdesc;
  890. u32 quirks = 0;
  891. unsigned int rsize = 0;
  892. char *rdesc;
  893. int ret, n;
  894. quirks = usbhid_lookup_quirk(le16_to_cpu(dev->descriptor.idVendor),
  895. le16_to_cpu(dev->descriptor.idProduct));
  896. if (quirks & HID_QUIRK_IGNORE)
  897. return -ENODEV;
  898. /* Many keyboards and mice don't like to be polled for reports,
  899. * so we will always set the HID_QUIRK_NOGET flag for them. */
  900. if (interface->desc.bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT) {
  901. if (interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_KEYBOARD ||
  902. interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_MOUSE)
  903. quirks |= HID_QUIRK_NOGET;
  904. }
  905. if (usb_get_extra_descriptor(interface, HID_DT_HID, &hdesc) &&
  906. (!interface->desc.bNumEndpoints ||
  907. usb_get_extra_descriptor(&interface->endpoint[0], HID_DT_HID, &hdesc))) {
  908. dbg_hid("class descriptor not present\n");
  909. return -ENODEV;
  910. }
  911. hid->version = le16_to_cpu(hdesc->bcdHID);
  912. hid->country = hdesc->bCountryCode;
  913. for (n = 0; n < hdesc->bNumDescriptors; n++)
  914. if (hdesc->desc[n].bDescriptorType == HID_DT_REPORT)
  915. rsize = le16_to_cpu(hdesc->desc[n].wDescriptorLength);
  916. if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
  917. dbg_hid("weird size of report descriptor (%u)\n", rsize);
  918. return -EINVAL;
  919. }
  920. if (!(rdesc = kmalloc(rsize, GFP_KERNEL))) {
  921. dbg_hid("couldn't allocate rdesc memory\n");
  922. return -ENOMEM;
  923. }
  924. hid_set_idle(dev, interface->desc.bInterfaceNumber, 0, 0);
  925. ret = hid_get_class_descriptor(dev, interface->desc.bInterfaceNumber,
  926. HID_DT_REPORT, rdesc, rsize);
  927. if (ret < 0) {
  928. dbg_hid("reading report descriptor failed\n");
  929. kfree(rdesc);
  930. goto err;
  931. }
  932. ret = hid_parse_report(hid, rdesc, rsize);
  933. kfree(rdesc);
  934. if (ret) {
  935. dbg_hid("parsing report descriptor failed\n");
  936. goto err;
  937. }
  938. hid->quirks |= quirks;
  939. return 0;
  940. err:
  941. return ret;
  942. }
  943. static int usbhid_start(struct hid_device *hid)
  944. {
  945. struct usb_interface *intf = to_usb_interface(hid->dev.parent);
  946. struct usb_host_interface *interface = intf->cur_altsetting;
  947. struct usb_device *dev = interface_to_usbdev(intf);
  948. struct usbhid_device *usbhid = hid->driver_data;
  949. unsigned int n, insize = 0;
  950. int ret;
  951. clear_bit(HID_DISCONNECTED, &usbhid->iofl);
  952. usbhid->bufsize = HID_MIN_BUFFER_SIZE;
  953. hid_find_max_report(hid, HID_INPUT_REPORT, &usbhid->bufsize);
  954. hid_find_max_report(hid, HID_OUTPUT_REPORT, &usbhid->bufsize);
  955. hid_find_max_report(hid, HID_FEATURE_REPORT, &usbhid->bufsize);
  956. if (usbhid->bufsize > HID_MAX_BUFFER_SIZE)
  957. usbhid->bufsize = HID_MAX_BUFFER_SIZE;
  958. hid_find_max_report(hid, HID_INPUT_REPORT, &insize);
  959. if (insize > HID_MAX_BUFFER_SIZE)
  960. insize = HID_MAX_BUFFER_SIZE;
  961. if (hid_alloc_buffers(dev, hid)) {
  962. ret = -ENOMEM;
  963. goto fail;
  964. }
  965. for (n = 0; n < interface->desc.bNumEndpoints; n++) {
  966. struct usb_endpoint_descriptor *endpoint;
  967. int pipe;
  968. int interval;
  969. endpoint = &interface->endpoint[n].desc;
  970. if (!usb_endpoint_xfer_int(endpoint))
  971. continue;
  972. interval = endpoint->bInterval;
  973. /* Some vendors give fullspeed interval on highspeed devides */
  974. if (hid->quirks & HID_QUIRK_FULLSPEED_INTERVAL &&
  975. dev->speed == USB_SPEED_HIGH) {
  976. interval = fls(endpoint->bInterval*8);
  977. printk(KERN_INFO "%s: Fixing fullspeed to highspeed interval: %d -> %d\n",
  978. hid->name, endpoint->bInterval, interval);
  979. }
  980. /* Change the polling interval of mice. */
  981. if (hid->collection->usage == HID_GD_MOUSE && hid_mousepoll_interval > 0)
  982. interval = hid_mousepoll_interval;
  983. ret = -ENOMEM;
  984. if (usb_endpoint_dir_in(endpoint)) {
  985. if (usbhid->urbin)
  986. continue;
  987. if (!(usbhid->urbin = usb_alloc_urb(0, GFP_KERNEL)))
  988. goto fail;
  989. pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
  990. usb_fill_int_urb(usbhid->urbin, dev, pipe, usbhid->inbuf, insize,
  991. hid_irq_in, hid, interval);
  992. usbhid->urbin->transfer_dma = usbhid->inbuf_dma;
  993. usbhid->urbin->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  994. } else {
  995. if (usbhid->urbout)
  996. continue;
  997. if (!(usbhid->urbout = usb_alloc_urb(0, GFP_KERNEL)))
  998. goto fail;
  999. pipe = usb_sndintpipe(dev, endpoint->bEndpointAddress);
  1000. usb_fill_int_urb(usbhid->urbout, dev, pipe, usbhid->outbuf, 0,
  1001. hid_irq_out, hid, interval);
  1002. usbhid->urbout->transfer_dma = usbhid->outbuf_dma;
  1003. usbhid->urbout->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  1004. }
  1005. }
  1006. usbhid->urbctrl = usb_alloc_urb(0, GFP_KERNEL);
  1007. if (!usbhid->urbctrl) {
  1008. ret = -ENOMEM;
  1009. goto fail;
  1010. }
  1011. usb_fill_control_urb(usbhid->urbctrl, dev, 0, (void *) usbhid->cr,
  1012. usbhid->ctrlbuf, 1, hid_ctrl, hid);
  1013. usbhid->urbctrl->transfer_dma = usbhid->ctrlbuf_dma;
  1014. usbhid->urbctrl->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  1015. if (!(hid->quirks & HID_QUIRK_NO_INIT_REPORTS))
  1016. usbhid_init_reports(hid);
  1017. set_bit(HID_STARTED, &usbhid->iofl);
  1018. /* Some keyboards don't work until their LEDs have been set.
  1019. * Since BIOSes do set the LEDs, it must be safe for any device
  1020. * that supports the keyboard boot protocol.
  1021. * In addition, enable remote wakeup by default for all keyboard
  1022. * devices supporting the boot protocol.
  1023. */
  1024. if (interface->desc.bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT &&
  1025. interface->desc.bInterfaceProtocol ==
  1026. USB_INTERFACE_PROTOCOL_KEYBOARD) {
  1027. usbhid_set_leds(hid);
  1028. device_set_wakeup_enable(&dev->dev, 1);
  1029. }
  1030. return 0;
  1031. fail:
  1032. usb_free_urb(usbhid->urbin);
  1033. usb_free_urb(usbhid->urbout);
  1034. usb_free_urb(usbhid->urbctrl);
  1035. usbhid->urbin = NULL;
  1036. usbhid->urbout = NULL;
  1037. usbhid->urbctrl = NULL;
  1038. hid_free_buffers(dev, hid);
  1039. return ret;
  1040. }
  1041. static void usbhid_stop(struct hid_device *hid)
  1042. {
  1043. struct usbhid_device *usbhid = hid->driver_data;
  1044. if (WARN_ON(!usbhid))
  1045. return;
  1046. clear_bit(HID_STARTED, &usbhid->iofl);
  1047. spin_lock_irq(&usbhid->lock); /* Sync with error and led handlers */
  1048. set_bit(HID_DISCONNECTED, &usbhid->iofl);
  1049. spin_unlock_irq(&usbhid->lock);
  1050. usb_kill_urb(usbhid->urbin);
  1051. usb_kill_urb(usbhid->urbout);
  1052. usb_kill_urb(usbhid->urbctrl);
  1053. hid_cancel_delayed_stuff(usbhid);
  1054. hid->claimed = 0;
  1055. usb_free_urb(usbhid->urbin);
  1056. usb_free_urb(usbhid->urbctrl);
  1057. usb_free_urb(usbhid->urbout);
  1058. usbhid->urbin = NULL; /* don't mess up next start */
  1059. usbhid->urbctrl = NULL;
  1060. usbhid->urbout = NULL;
  1061. hid_free_buffers(hid_to_usb_dev(hid), hid);
  1062. }
  1063. static int usbhid_power(struct hid_device *hid, int lvl)
  1064. {
  1065. int r = 0;
  1066. switch (lvl) {
  1067. case PM_HINT_FULLON:
  1068. r = usbhid_get_power(hid);
  1069. break;
  1070. case PM_HINT_NORMAL:
  1071. usbhid_put_power(hid);
  1072. break;
  1073. }
  1074. return r;
  1075. }
  1076. static struct hid_ll_driver usb_hid_driver = {
  1077. .parse = usbhid_parse,
  1078. .start = usbhid_start,
  1079. .stop = usbhid_stop,
  1080. .open = usbhid_open,
  1081. .close = usbhid_close,
  1082. .power = usbhid_power,
  1083. .hidinput_input_event = usb_hidinput_input_event,
  1084. };
  1085. static int usbhid_probe(struct usb_interface *intf, const struct usb_device_id *id)
  1086. {
  1087. struct usb_host_interface *interface = intf->cur_altsetting;
  1088. struct usb_device *dev = interface_to_usbdev(intf);
  1089. struct usbhid_device *usbhid;
  1090. struct hid_device *hid;
  1091. unsigned int n, has_in = 0;
  1092. size_t len;
  1093. int ret;
  1094. dbg_hid("HID probe called for ifnum %d\n",
  1095. intf->altsetting->desc.bInterfaceNumber);
  1096. for (n = 0; n < interface->desc.bNumEndpoints; n++)
  1097. if (usb_endpoint_is_int_in(&interface->endpoint[n].desc))
  1098. has_in++;
  1099. if (!has_in) {
  1100. hid_err(intf, "couldn't find an input interrupt endpoint\n");
  1101. return -ENODEV;
  1102. }
  1103. hid = hid_allocate_device();
  1104. if (IS_ERR(hid))
  1105. return PTR_ERR(hid);
  1106. usb_set_intfdata(intf, hid);
  1107. hid->ll_driver = &usb_hid_driver;
  1108. hid->hid_get_raw_report = usbhid_get_raw_report;
  1109. hid->hid_output_raw_report = usbhid_output_raw_report;
  1110. hid->ff_init = hid_pidff_init;
  1111. #ifdef CONFIG_USB_HIDDEV
  1112. hid->hiddev_connect = hiddev_connect;
  1113. hid->hiddev_disconnect = hiddev_disconnect;
  1114. hid->hiddev_hid_event = hiddev_hid_event;
  1115. hid->hiddev_report_event = hiddev_report_event;
  1116. #endif
  1117. hid->dev.parent = &intf->dev;
  1118. hid->bus = BUS_USB;
  1119. hid->vendor = le16_to_cpu(dev->descriptor.idVendor);
  1120. hid->product = le16_to_cpu(dev->descriptor.idProduct);
  1121. hid->name[0] = 0;
  1122. hid->quirks = usbhid_lookup_quirk(hid->vendor, hid->product);
  1123. if (intf->cur_altsetting->desc.bInterfaceProtocol ==
  1124. USB_INTERFACE_PROTOCOL_MOUSE)
  1125. hid->type = HID_TYPE_USBMOUSE;
  1126. else if (intf->cur_altsetting->desc.bInterfaceProtocol == 0)
  1127. hid->type = HID_TYPE_USBNONE;
  1128. if (dev->manufacturer)
  1129. strlcpy(hid->name, dev->manufacturer, sizeof(hid->name));
  1130. if (dev->product) {
  1131. if (dev->manufacturer)
  1132. strlcat(hid->name, " ", sizeof(hid->name));
  1133. strlcat(hid->name, dev->product, sizeof(hid->name));
  1134. }
  1135. if (!strlen(hid->name))
  1136. snprintf(hid->name, sizeof(hid->name), "HID %04x:%04x",
  1137. le16_to_cpu(dev->descriptor.idVendor),
  1138. le16_to_cpu(dev->descriptor.idProduct));
  1139. usb_make_path(dev, hid->phys, sizeof(hid->phys));
  1140. strlcat(hid->phys, "/input", sizeof(hid->phys));
  1141. len = strlen(hid->phys);
  1142. if (len < sizeof(hid->phys) - 1)
  1143. snprintf(hid->phys + len, sizeof(hid->phys) - len,
  1144. "%d", intf->altsetting[0].desc.bInterfaceNumber);
  1145. if (usb_string(dev, dev->descriptor.iSerialNumber, hid->uniq, 64) <= 0)
  1146. hid->uniq[0] = 0;
  1147. usbhid = kzalloc(sizeof(*usbhid), GFP_KERNEL);
  1148. if (usbhid == NULL) {
  1149. ret = -ENOMEM;
  1150. goto err;
  1151. }
  1152. hid->driver_data = usbhid;
  1153. usbhid->hid = hid;
  1154. usbhid->intf = intf;
  1155. usbhid->ifnum = interface->desc.bInterfaceNumber;
  1156. init_waitqueue_head(&usbhid->wait);
  1157. INIT_WORK(&usbhid->reset_work, hid_reset);
  1158. setup_timer(&usbhid->io_retry, hid_retry_timeout, (unsigned long) hid);
  1159. spin_lock_init(&usbhid->lock);
  1160. INIT_WORK(&usbhid->led_work, hid_led);
  1161. ret = hid_add_device(hid);
  1162. if (ret) {
  1163. if (ret != -ENODEV)
  1164. hid_err(intf, "can't add hid device: %d\n", ret);
  1165. goto err_free;
  1166. }
  1167. return 0;
  1168. err_free:
  1169. kfree(usbhid);
  1170. err:
  1171. hid_destroy_device(hid);
  1172. return ret;
  1173. }
  1174. static void usbhid_disconnect(struct usb_interface *intf)
  1175. {
  1176. struct hid_device *hid = usb_get_intfdata(intf);
  1177. struct usbhid_device *usbhid;
  1178. if (WARN_ON(!hid))
  1179. return;
  1180. usbhid = hid->driver_data;
  1181. hid_destroy_device(hid);
  1182. kfree(usbhid);
  1183. }
  1184. static void hid_cancel_delayed_stuff(struct usbhid_device *usbhid)
  1185. {
  1186. del_timer_sync(&usbhid->io_retry);
  1187. cancel_work_sync(&usbhid->reset_work);
  1188. cancel_work_sync(&usbhid->led_work);
  1189. }
  1190. static void hid_cease_io(struct usbhid_device *usbhid)
  1191. {
  1192. del_timer_sync(&usbhid->io_retry);
  1193. usb_kill_urb(usbhid->urbin);
  1194. usb_kill_urb(usbhid->urbctrl);
  1195. usb_kill_urb(usbhid->urbout);
  1196. }
  1197. /* Treat USB reset pretty much the same as suspend/resume */
  1198. static int hid_pre_reset(struct usb_interface *intf)
  1199. {
  1200. struct hid_device *hid = usb_get_intfdata(intf);
  1201. struct usbhid_device *usbhid = hid->driver_data;
  1202. spin_lock_irq(&usbhid->lock);
  1203. set_bit(HID_RESET_PENDING, &usbhid->iofl);
  1204. spin_unlock_irq(&usbhid->lock);
  1205. hid_cease_io(usbhid);
  1206. return 0;
  1207. }
  1208. /* Same routine used for post_reset and reset_resume */
  1209. static int hid_post_reset(struct usb_interface *intf)
  1210. {
  1211. struct usb_device *dev = interface_to_usbdev (intf);
  1212. struct hid_device *hid = usb_get_intfdata(intf);
  1213. struct usbhid_device *usbhid = hid->driver_data;
  1214. struct usb_host_interface *interface = intf->cur_altsetting;
  1215. int status;
  1216. char *rdesc;
  1217. /* Fetch and examine the HID report descriptor. If this
  1218. * has changed, then rebind. Since usbcore's check of the
  1219. * configuration descriptors passed, we already know that
  1220. * the size of the HID report descriptor has not changed.
  1221. */
  1222. rdesc = kmalloc(hid->rsize, GFP_KERNEL);
  1223. if (!rdesc) {
  1224. dbg_hid("couldn't allocate rdesc memory (post_reset)\n");
  1225. return 1;
  1226. }
  1227. status = hid_get_class_descriptor(dev,
  1228. interface->desc.bInterfaceNumber,
  1229. HID_DT_REPORT, rdesc, hid->rsize);
  1230. if (status < 0) {
  1231. dbg_hid("reading report descriptor failed (post_reset)\n");
  1232. kfree(rdesc);
  1233. return 1;
  1234. }
  1235. status = memcmp(rdesc, hid->rdesc, hid->rsize);
  1236. kfree(rdesc);
  1237. if (status != 0) {
  1238. dbg_hid("report descriptor changed\n");
  1239. return 1;
  1240. }
  1241. spin_lock_irq(&usbhid->lock);
  1242. clear_bit(HID_RESET_PENDING, &usbhid->iofl);
  1243. spin_unlock_irq(&usbhid->lock);
  1244. hid_set_idle(dev, intf->cur_altsetting->desc.bInterfaceNumber, 0, 0);
  1245. status = hid_start_in(hid);
  1246. if (status < 0)
  1247. hid_io_error(hid);
  1248. usbhid_restart_queues(usbhid);
  1249. return 0;
  1250. }
  1251. int usbhid_get_power(struct hid_device *hid)
  1252. {
  1253. struct usbhid_device *usbhid = hid->driver_data;
  1254. return usb_autopm_get_interface(usbhid->intf);
  1255. }
  1256. void usbhid_put_power(struct hid_device *hid)
  1257. {
  1258. struct usbhid_device *usbhid = hid->driver_data;
  1259. usb_autopm_put_interface(usbhid->intf);
  1260. }
  1261. #ifdef CONFIG_PM
  1262. static int hid_suspend(struct usb_interface *intf, pm_message_t message)
  1263. {
  1264. struct hid_device *hid = usb_get_intfdata(intf);
  1265. struct usbhid_device *usbhid = hid->driver_data;
  1266. int status;
  1267. if (PMSG_IS_AUTO(message)) {
  1268. spin_lock_irq(&usbhid->lock); /* Sync with error handler */
  1269. if (!test_bit(HID_RESET_PENDING, &usbhid->iofl)
  1270. && !test_bit(HID_CLEAR_HALT, &usbhid->iofl)
  1271. && !test_bit(HID_OUT_RUNNING, &usbhid->iofl)
  1272. && !test_bit(HID_CTRL_RUNNING, &usbhid->iofl)
  1273. && !test_bit(HID_KEYS_PRESSED, &usbhid->iofl)
  1274. && (!usbhid->ledcount || ignoreled))
  1275. {
  1276. set_bit(HID_REPORTED_IDLE, &usbhid->iofl);
  1277. spin_unlock_irq(&usbhid->lock);
  1278. if (hid->driver && hid->driver->suspend) {
  1279. status = hid->driver->suspend(hid, message);
  1280. if (status < 0)
  1281. return status;
  1282. }
  1283. } else {
  1284. usbhid_mark_busy(usbhid);
  1285. spin_unlock_irq(&usbhid->lock);
  1286. return -EBUSY;
  1287. }
  1288. } else {
  1289. if (hid->driver && hid->driver->suspend) {
  1290. status = hid->driver->suspend(hid, message);
  1291. if (status < 0)
  1292. return status;
  1293. }
  1294. spin_lock_irq(&usbhid->lock);
  1295. set_bit(HID_REPORTED_IDLE, &usbhid->iofl);
  1296. spin_unlock_irq(&usbhid->lock);
  1297. if (usbhid_wait_io(hid) < 0)
  1298. return -EIO;
  1299. }
  1300. hid_cancel_delayed_stuff(usbhid);
  1301. hid_cease_io(usbhid);
  1302. if (PMSG_IS_AUTO(message) && test_bit(HID_KEYS_PRESSED, &usbhid->iofl)) {
  1303. /* lost race against keypresses */
  1304. status = hid_start_in(hid);
  1305. if (status < 0)
  1306. hid_io_error(hid);
  1307. usbhid_mark_busy(usbhid);
  1308. return -EBUSY;
  1309. }
  1310. dev_dbg(&intf->dev, "suspend\n");
  1311. return 0;
  1312. }
  1313. static int hid_resume(struct usb_interface *intf)
  1314. {
  1315. struct hid_device *hid = usb_get_intfdata (intf);
  1316. struct usbhid_device *usbhid = hid->driver_data;
  1317. int status;
  1318. if (!test_bit(HID_STARTED, &usbhid->iofl))
  1319. return 0;
  1320. clear_bit(HID_REPORTED_IDLE, &usbhid->iofl);
  1321. usbhid_mark_busy(usbhid);
  1322. if (test_bit(HID_CLEAR_HALT, &usbhid->iofl) ||
  1323. test_bit(HID_RESET_PENDING, &usbhid->iofl))
  1324. schedule_work(&usbhid->reset_work);
  1325. usbhid->retry_delay = 0;
  1326. status = hid_start_in(hid);
  1327. if (status < 0)
  1328. hid_io_error(hid);
  1329. usbhid_restart_queues(usbhid);
  1330. if (status >= 0 && hid->driver && hid->driver->resume) {
  1331. int ret = hid->driver->resume(hid);
  1332. if (ret < 0)
  1333. status = ret;
  1334. }
  1335. dev_dbg(&intf->dev, "resume status %d\n", status);
  1336. return 0;
  1337. }
  1338. static int hid_reset_resume(struct usb_interface *intf)
  1339. {
  1340. struct hid_device *hid = usb_get_intfdata(intf);
  1341. struct usbhid_device *usbhid = hid->driver_data;
  1342. int status;
  1343. clear_bit(HID_REPORTED_IDLE, &usbhid->iofl);
  1344. status = hid_post_reset(intf);
  1345. if (status >= 0 && hid->driver && hid->driver->reset_resume) {
  1346. int ret = hid->driver->reset_resume(hid);
  1347. if (ret < 0)
  1348. status = ret;
  1349. }
  1350. return status;
  1351. }
  1352. #endif /* CONFIG_PM */
  1353. static const struct usb_device_id hid_usb_ids[] = {
  1354. { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
  1355. .bInterfaceClass = USB_INTERFACE_CLASS_HID },
  1356. { } /* Terminating entry */
  1357. };
  1358. MODULE_DEVICE_TABLE (usb, hid_usb_ids);
  1359. static struct usb_driver hid_driver = {
  1360. .name = "usbhid",
  1361. .probe = usbhid_probe,
  1362. .disconnect = usbhid_disconnect,
  1363. #ifdef CONFIG_PM
  1364. .suspend = hid_suspend,
  1365. .resume = hid_resume,
  1366. .reset_resume = hid_reset_resume,
  1367. #endif
  1368. .pre_reset = hid_pre_reset,
  1369. .post_reset = hid_post_reset,
  1370. .id_table = hid_usb_ids,
  1371. .supports_autosuspend = 1,
  1372. };
  1373. struct usb_interface *usbhid_find_interface(int minor)
  1374. {
  1375. return usb_find_interface(&hid_driver, minor);
  1376. }
  1377. static int __init hid_init(void)
  1378. {
  1379. int retval = -ENOMEM;
  1380. retval = usbhid_quirks_init(quirks_param);
  1381. if (retval)
  1382. goto usbhid_quirks_init_fail;
  1383. retval = usb_register(&hid_driver);
  1384. if (retval)
  1385. goto usb_register_fail;
  1386. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n");
  1387. return 0;
  1388. usb_register_fail:
  1389. usbhid_quirks_exit();
  1390. usbhid_quirks_init_fail:
  1391. return retval;
  1392. }
  1393. static void __exit hid_exit(void)
  1394. {
  1395. usb_deregister(&hid_driver);
  1396. usbhid_quirks_exit();
  1397. }
  1398. module_init(hid_init);
  1399. module_exit(hid_exit);
  1400. MODULE_AUTHOR("Andreas Gal");
  1401. MODULE_AUTHOR("Vojtech Pavlik");
  1402. MODULE_AUTHOR("Jiri Kosina");
  1403. MODULE_DESCRIPTION(DRIVER_DESC);
  1404. MODULE_LICENSE(DRIVER_LICENSE);