hid-core.c 40 KB

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