hid-core.c 41 KB

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