hid-core.c 38 KB

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