hid-core.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429
  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_VERSION "v2.6"
  39. #define DRIVER_DESC "USB HID core driver"
  40. #define DRIVER_LICENSE "GPL"
  41. /*
  42. * Module parameters.
  43. */
  44. static unsigned int hid_mousepoll_interval;
  45. module_param_named(mousepoll, hid_mousepoll_interval, uint, 0644);
  46. MODULE_PARM_DESC(mousepoll, "Polling interval of mice");
  47. static unsigned int ignoreled;
  48. module_param_named(ignoreled, ignoreled, uint, 0644);
  49. MODULE_PARM_DESC(ignoreled, "Autosuspend with active leds");
  50. /* Quirks specified at module load time */
  51. static char *quirks_param[MAX_USBHID_BOOT_QUIRKS] = { [ 0 ... (MAX_USBHID_BOOT_QUIRKS - 1) ] = NULL };
  52. module_param_array_named(quirks, quirks_param, charp, NULL, 0444);
  53. MODULE_PARM_DESC(quirks, "Add/modify USB HID quirks by specifying "
  54. " quirks=vendorID:productID:quirks"
  55. " where vendorID, productID, and quirks are all in"
  56. " 0x-prefixed hex");
  57. /*
  58. * Input submission and I/O error handler.
  59. */
  60. static DEFINE_MUTEX(hid_open_mut);
  61. static struct workqueue_struct *resumption_waker;
  62. static void hid_io_error(struct hid_device *hid);
  63. static int hid_submit_out(struct hid_device *hid);
  64. static int hid_submit_ctrl(struct hid_device *hid);
  65. static void hid_cancel_delayed_stuff(struct usbhid_device *usbhid);
  66. /* Start up the input URB */
  67. static int hid_start_in(struct hid_device *hid)
  68. {
  69. unsigned long flags;
  70. int rc = 0;
  71. struct usbhid_device *usbhid = hid->driver_data;
  72. spin_lock_irqsave(&usbhid->lock, flags);
  73. if (hid->open > 0 &&
  74. !test_bit(HID_DISCONNECTED, &usbhid->iofl) &&
  75. !test_bit(HID_REPORTED_IDLE, &usbhid->iofl) &&
  76. !test_and_set_bit(HID_IN_RUNNING, &usbhid->iofl)) {
  77. rc = usb_submit_urb(usbhid->urbin, GFP_ATOMIC);
  78. if (rc != 0)
  79. clear_bit(HID_IN_RUNNING, &usbhid->iofl);
  80. }
  81. spin_unlock_irqrestore(&usbhid->lock, flags);
  82. return rc;
  83. }
  84. /* I/O retry timer routine */
  85. static void hid_retry_timeout(unsigned long _hid)
  86. {
  87. struct hid_device *hid = (struct hid_device *) _hid;
  88. struct usbhid_device *usbhid = hid->driver_data;
  89. dev_dbg(&usbhid->intf->dev, "retrying intr urb\n");
  90. if (hid_start_in(hid))
  91. hid_io_error(hid);
  92. }
  93. /* Workqueue routine to reset the device or clear a halt */
  94. static void hid_reset(struct work_struct *work)
  95. {
  96. struct usbhid_device *usbhid =
  97. container_of(work, struct usbhid_device, reset_work);
  98. struct hid_device *hid = usbhid->hid;
  99. int rc = 0;
  100. if (test_bit(HID_CLEAR_HALT, &usbhid->iofl)) {
  101. dev_dbg(&usbhid->intf->dev, "clear halt\n");
  102. rc = usb_clear_halt(hid_to_usb_dev(hid), usbhid->urbin->pipe);
  103. clear_bit(HID_CLEAR_HALT, &usbhid->iofl);
  104. hid_start_in(hid);
  105. }
  106. else if (test_bit(HID_RESET_PENDING, &usbhid->iofl)) {
  107. dev_dbg(&usbhid->intf->dev, "resetting device\n");
  108. rc = usb_lock_device_for_reset(hid_to_usb_dev(hid), usbhid->intf);
  109. if (rc == 0) {
  110. rc = usb_reset_device(hid_to_usb_dev(hid));
  111. usb_unlock_device(hid_to_usb_dev(hid));
  112. }
  113. clear_bit(HID_RESET_PENDING, &usbhid->iofl);
  114. }
  115. switch (rc) {
  116. case 0:
  117. if (!test_bit(HID_IN_RUNNING, &usbhid->iofl))
  118. hid_io_error(hid);
  119. break;
  120. default:
  121. err_hid("can't reset device, %s-%s/input%d, status %d",
  122. hid_to_usb_dev(hid)->bus->bus_name,
  123. hid_to_usb_dev(hid)->devpath,
  124. usbhid->ifnum, rc);
  125. /* FALLTHROUGH */
  126. case -EHOSTUNREACH:
  127. case -ENODEV:
  128. case -EINTR:
  129. break;
  130. }
  131. }
  132. /* Main I/O error handler */
  133. static void hid_io_error(struct hid_device *hid)
  134. {
  135. unsigned long flags;
  136. struct usbhid_device *usbhid = hid->driver_data;
  137. spin_lock_irqsave(&usbhid->lock, flags);
  138. /* Stop when disconnected */
  139. if (test_bit(HID_DISCONNECTED, &usbhid->iofl))
  140. goto done;
  141. /* If it has been a while since the last error, we'll assume
  142. * this a brand new error and reset the retry timeout. */
  143. if (time_after(jiffies, usbhid->stop_retry + HZ/2))
  144. usbhid->retry_delay = 0;
  145. /* When an error occurs, retry at increasing intervals */
  146. if (usbhid->retry_delay == 0) {
  147. usbhid->retry_delay = 13; /* Then 26, 52, 104, 104, ... */
  148. usbhid->stop_retry = jiffies + msecs_to_jiffies(1000);
  149. } else if (usbhid->retry_delay < 100)
  150. usbhid->retry_delay *= 2;
  151. if (time_after(jiffies, usbhid->stop_retry)) {
  152. /* Retries failed, so do a port reset */
  153. if (!test_and_set_bit(HID_RESET_PENDING, &usbhid->iofl)) {
  154. schedule_work(&usbhid->reset_work);
  155. goto done;
  156. }
  157. }
  158. mod_timer(&usbhid->io_retry,
  159. jiffies + msecs_to_jiffies(usbhid->retry_delay));
  160. done:
  161. spin_unlock_irqrestore(&usbhid->lock, flags);
  162. }
  163. static void usbhid_mark_busy(struct usbhid_device *usbhid)
  164. {
  165. struct usb_interface *intf = usbhid->intf;
  166. usb_mark_last_busy(interface_to_usbdev(intf));
  167. }
  168. static int usbhid_restart_out_queue(struct usbhid_device *usbhid)
  169. {
  170. struct hid_device *hid = usb_get_intfdata(usbhid->intf);
  171. int kicked;
  172. if (!hid)
  173. return 0;
  174. if ((kicked = (usbhid->outhead != usbhid->outtail))) {
  175. dbg("Kicking head %d tail %d", usbhid->outhead, usbhid->outtail);
  176. if (hid_submit_out(hid)) {
  177. clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
  178. wake_up(&usbhid->wait);
  179. }
  180. }
  181. return kicked;
  182. }
  183. static int usbhid_restart_ctrl_queue(struct usbhid_device *usbhid)
  184. {
  185. struct hid_device *hid = usb_get_intfdata(usbhid->intf);
  186. int kicked;
  187. WARN_ON(hid == NULL);
  188. if (!hid)
  189. return 0;
  190. if ((kicked = (usbhid->ctrlhead != usbhid->ctrltail))) {
  191. dbg("Kicking head %d tail %d", usbhid->ctrlhead, usbhid->ctrltail);
  192. if (hid_submit_ctrl(hid)) {
  193. clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
  194. wake_up(&usbhid->wait);
  195. }
  196. }
  197. return kicked;
  198. }
  199. /*
  200. * Input interrupt completion handler.
  201. */
  202. static void hid_irq_in(struct urb *urb)
  203. {
  204. struct hid_device *hid = urb->context;
  205. struct usbhid_device *usbhid = hid->driver_data;
  206. int status;
  207. switch (urb->status) {
  208. case 0: /* success */
  209. usbhid_mark_busy(usbhid);
  210. usbhid->retry_delay = 0;
  211. hid_input_report(urb->context, HID_INPUT_REPORT,
  212. urb->transfer_buffer,
  213. urb->actual_length, 1);
  214. /*
  215. * autosuspend refused while keys are pressed
  216. * because most keyboards don't wake up when
  217. * a key is released
  218. */
  219. if (hid_check_keys_pressed(hid))
  220. set_bit(HID_KEYS_PRESSED, &usbhid->iofl);
  221. else
  222. clear_bit(HID_KEYS_PRESSED, &usbhid->iofl);
  223. break;
  224. case -EPIPE: /* stall */
  225. usbhid_mark_busy(usbhid);
  226. clear_bit(HID_IN_RUNNING, &usbhid->iofl);
  227. set_bit(HID_CLEAR_HALT, &usbhid->iofl);
  228. schedule_work(&usbhid->reset_work);
  229. return;
  230. case -ECONNRESET: /* unlink */
  231. case -ENOENT:
  232. case -ESHUTDOWN: /* unplug */
  233. clear_bit(HID_IN_RUNNING, &usbhid->iofl);
  234. return;
  235. case -EILSEQ: /* protocol error or unplug */
  236. case -EPROTO: /* protocol error or unplug */
  237. case -ETIME: /* protocol error or unplug */
  238. case -ETIMEDOUT: /* Should never happen, but... */
  239. usbhid_mark_busy(usbhid);
  240. clear_bit(HID_IN_RUNNING, &usbhid->iofl);
  241. hid_io_error(hid);
  242. return;
  243. default: /* error */
  244. dev_warn(&urb->dev->dev, "input irq status %d "
  245. "received\n", urb->status);
  246. }
  247. status = usb_submit_urb(urb, GFP_ATOMIC);
  248. if (status) {
  249. clear_bit(HID_IN_RUNNING, &usbhid->iofl);
  250. if (status != -EPERM) {
  251. err_hid("can't resubmit intr, %s-%s/input%d, status %d",
  252. hid_to_usb_dev(hid)->bus->bus_name,
  253. hid_to_usb_dev(hid)->devpath,
  254. usbhid->ifnum, status);
  255. hid_io_error(hid);
  256. }
  257. }
  258. }
  259. static int hid_submit_out(struct hid_device *hid)
  260. {
  261. struct hid_report *report;
  262. char *raw_report;
  263. struct usbhid_device *usbhid = hid->driver_data;
  264. report = usbhid->out[usbhid->outtail].report;
  265. raw_report = usbhid->out[usbhid->outtail].raw_report;
  266. if (!test_bit(HID_REPORTED_IDLE, &usbhid->iofl)) {
  267. usbhid->urbout->transfer_buffer_length = ((report->size - 1) >> 3) + 1 + (report->id > 0);
  268. usbhid->urbout->dev = hid_to_usb_dev(hid);
  269. memcpy(usbhid->outbuf, raw_report, usbhid->urbout->transfer_buffer_length);
  270. kfree(raw_report);
  271. dbg_hid("submitting out urb\n");
  272. if (usb_submit_urb(usbhid->urbout, GFP_ATOMIC)) {
  273. err_hid("usb_submit_urb(out) failed");
  274. return -1;
  275. }
  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. } else {
  330. /*
  331. * queue work to wake up the device.
  332. * as the work queue is freezeable, this is safe
  333. * with respect to STD and STR
  334. */
  335. queue_work(resumption_waker, &usbhid->restart_work);
  336. }
  337. return 0;
  338. }
  339. /*
  340. * Output interrupt completion handler.
  341. */
  342. static void hid_irq_out(struct urb *urb)
  343. {
  344. struct hid_device *hid = urb->context;
  345. struct usbhid_device *usbhid = hid->driver_data;
  346. unsigned long flags;
  347. int unplug = 0;
  348. switch (urb->status) {
  349. case 0: /* success */
  350. break;
  351. case -ESHUTDOWN: /* unplug */
  352. unplug = 1;
  353. case -EILSEQ: /* protocol error or unplug */
  354. case -EPROTO: /* protocol error or unplug */
  355. case -ECONNRESET: /* unlink */
  356. case -ENOENT:
  357. break;
  358. default: /* error */
  359. dev_warn(&urb->dev->dev, "output irq status %d "
  360. "received\n", urb->status);
  361. }
  362. spin_lock_irqsave(&usbhid->lock, flags);
  363. if (unplug)
  364. usbhid->outtail = usbhid->outhead;
  365. else
  366. usbhid->outtail = (usbhid->outtail + 1) & (HID_OUTPUT_FIFO_SIZE - 1);
  367. if (usbhid->outhead != usbhid->outtail) {
  368. if (hid_submit_out(hid)) {
  369. clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
  370. wake_up(&usbhid->wait);
  371. }
  372. spin_unlock_irqrestore(&usbhid->lock, flags);
  373. return;
  374. }
  375. clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
  376. spin_unlock_irqrestore(&usbhid->lock, flags);
  377. wake_up(&usbhid->wait);
  378. }
  379. /*
  380. * Control pipe completion handler.
  381. */
  382. static void hid_ctrl(struct urb *urb)
  383. {
  384. struct hid_device *hid = urb->context;
  385. struct usbhid_device *usbhid = hid->driver_data;
  386. int unplug = 0, status = urb->status;
  387. spin_lock(&usbhid->lock);
  388. switch (status) {
  389. case 0: /* success */
  390. if (usbhid->ctrl[usbhid->ctrltail].dir == USB_DIR_IN)
  391. hid_input_report(urb->context,
  392. usbhid->ctrl[usbhid->ctrltail].report->type,
  393. urb->transfer_buffer, urb->actual_length, 0);
  394. break;
  395. case -ESHUTDOWN: /* unplug */
  396. unplug = 1;
  397. case -EILSEQ: /* protocol error or unplug */
  398. case -EPROTO: /* protocol error or unplug */
  399. case -ECONNRESET: /* unlink */
  400. case -ENOENT:
  401. case -EPIPE: /* report not available */
  402. break;
  403. default: /* error */
  404. dev_warn(&urb->dev->dev, "ctrl urb status %d "
  405. "received\n", status);
  406. }
  407. if (unplug)
  408. usbhid->ctrltail = usbhid->ctrlhead;
  409. else
  410. usbhid->ctrltail = (usbhid->ctrltail + 1) & (HID_CONTROL_FIFO_SIZE - 1);
  411. if (usbhid->ctrlhead != usbhid->ctrltail) {
  412. if (hid_submit_ctrl(hid)) {
  413. clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
  414. wake_up(&usbhid->wait);
  415. }
  416. spin_unlock(&usbhid->lock);
  417. return;
  418. }
  419. clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
  420. spin_unlock(&usbhid->lock);
  421. wake_up(&usbhid->wait);
  422. }
  423. static void __usbhid_submit_report(struct hid_device *hid, struct hid_report *report,
  424. unsigned char dir)
  425. {
  426. int head;
  427. struct usbhid_device *usbhid = hid->driver_data;
  428. int len = ((report->size - 1) >> 3) + 1 + (report->id > 0);
  429. if ((hid->quirks & HID_QUIRK_NOGET) && dir == USB_DIR_IN)
  430. return;
  431. if (usbhid->urbout && dir == USB_DIR_OUT && report->type == HID_OUTPUT_REPORT) {
  432. if ((head = (usbhid->outhead + 1) & (HID_OUTPUT_FIFO_SIZE - 1)) == usbhid->outtail) {
  433. dev_warn(&hid->dev, "output queue full\n");
  434. return;
  435. }
  436. usbhid->out[usbhid->outhead].raw_report = kmalloc(len, GFP_ATOMIC);
  437. if (!usbhid->out[usbhid->outhead].raw_report) {
  438. dev_warn(&hid->dev, "output queueing failed\n");
  439. return;
  440. }
  441. hid_output_report(report, usbhid->out[usbhid->outhead].raw_report);
  442. usbhid->out[usbhid->outhead].report = report;
  443. usbhid->outhead = head;
  444. if (!test_and_set_bit(HID_OUT_RUNNING, &usbhid->iofl))
  445. if (hid_submit_out(hid))
  446. clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
  447. return;
  448. }
  449. if ((head = (usbhid->ctrlhead + 1) & (HID_CONTROL_FIFO_SIZE - 1)) == usbhid->ctrltail) {
  450. dev_warn(&hid->dev, "control queue full\n");
  451. return;
  452. }
  453. if (dir == USB_DIR_OUT) {
  454. usbhid->ctrl[usbhid->ctrlhead].raw_report = kmalloc(len, GFP_ATOMIC);
  455. if (!usbhid->ctrl[usbhid->ctrlhead].raw_report) {
  456. dev_warn(&hid->dev, "control queueing failed\n");
  457. return;
  458. }
  459. hid_output_report(report, usbhid->ctrl[usbhid->ctrlhead].raw_report);
  460. }
  461. usbhid->ctrl[usbhid->ctrlhead].report = report;
  462. usbhid->ctrl[usbhid->ctrlhead].dir = dir;
  463. usbhid->ctrlhead = head;
  464. if (!test_and_set_bit(HID_CTRL_RUNNING, &usbhid->iofl))
  465. if (hid_submit_ctrl(hid))
  466. clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
  467. }
  468. void usbhid_submit_report(struct hid_device *hid, struct hid_report *report, unsigned char dir)
  469. {
  470. struct usbhid_device *usbhid = hid->driver_data;
  471. unsigned long flags;
  472. spin_lock_irqsave(&usbhid->lock, flags);
  473. __usbhid_submit_report(hid, report, dir);
  474. spin_unlock_irqrestore(&usbhid->lock, flags);
  475. }
  476. EXPORT_SYMBOL_GPL(usbhid_submit_report);
  477. static int usb_hidinput_input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
  478. {
  479. struct hid_device *hid = input_get_drvdata(dev);
  480. struct usbhid_device *usbhid = hid->driver_data;
  481. struct hid_field *field;
  482. unsigned long flags;
  483. int offset;
  484. if (type == EV_FF)
  485. return input_ff_event(dev, type, code, value);
  486. if (type != EV_LED)
  487. return -1;
  488. if ((offset = hidinput_find_field(hid, type, code, &field)) == -1) {
  489. dev_warn(&dev->dev, "event field not found\n");
  490. return -1;
  491. }
  492. hid_set_field(field, offset, value);
  493. if (value) {
  494. spin_lock_irqsave(&usbhid->lock, flags);
  495. usbhid->ledcount++;
  496. spin_unlock_irqrestore(&usbhid->lock, flags);
  497. } else {
  498. spin_lock_irqsave(&usbhid->lock, flags);
  499. usbhid->ledcount--;
  500. spin_unlock_irqrestore(&usbhid->lock, flags);
  501. }
  502. usbhid_submit_report(hid, field->report, USB_DIR_OUT);
  503. return 0;
  504. }
  505. int usbhid_wait_io(struct hid_device *hid)
  506. {
  507. struct usbhid_device *usbhid = hid->driver_data;
  508. if (!wait_event_timeout(usbhid->wait,
  509. (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl) &&
  510. !test_bit(HID_OUT_RUNNING, &usbhid->iofl)),
  511. 10*HZ)) {
  512. dbg_hid("timeout waiting for ctrl or out queue to clear\n");
  513. return -1;
  514. }
  515. return 0;
  516. }
  517. static int hid_set_idle(struct usb_device *dev, int ifnum, int report, int idle)
  518. {
  519. return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  520. HID_REQ_SET_IDLE, USB_TYPE_CLASS | USB_RECIP_INTERFACE, (idle << 8) | report,
  521. ifnum, NULL, 0, USB_CTRL_SET_TIMEOUT);
  522. }
  523. static int hid_get_class_descriptor(struct usb_device *dev, int ifnum,
  524. unsigned char type, void *buf, int size)
  525. {
  526. int result, retries = 4;
  527. memset(buf, 0, size);
  528. do {
  529. result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  530. USB_REQ_GET_DESCRIPTOR, USB_RECIP_INTERFACE | USB_DIR_IN,
  531. (type << 8), ifnum, buf, size, USB_CTRL_GET_TIMEOUT);
  532. retries--;
  533. } while (result < size && retries);
  534. return result;
  535. }
  536. int usbhid_open(struct hid_device *hid)
  537. {
  538. struct usbhid_device *usbhid = hid->driver_data;
  539. int res;
  540. mutex_lock(&hid_open_mut);
  541. if (!hid->open++) {
  542. res = usb_autopm_get_interface(usbhid->intf);
  543. /* the device must be awake to reliable request remote wakeup */
  544. if (res < 0) {
  545. hid->open--;
  546. mutex_unlock(&hid_open_mut);
  547. return -EIO;
  548. }
  549. usbhid->intf->needs_remote_wakeup = 1;
  550. if (hid_start_in(hid))
  551. hid_io_error(hid);
  552. usb_autopm_put_interface(usbhid->intf);
  553. }
  554. mutex_unlock(&hid_open_mut);
  555. return 0;
  556. }
  557. void usbhid_close(struct hid_device *hid)
  558. {
  559. struct usbhid_device *usbhid = hid->driver_data;
  560. mutex_lock(&hid_open_mut);
  561. /* protecting hid->open to make sure we don't restart
  562. * data acquistion due to a resumption we no longer
  563. * care about
  564. */
  565. spin_lock_irq(&usbhid->lock);
  566. if (!--hid->open) {
  567. spin_unlock_irq(&usbhid->lock);
  568. hid_cancel_delayed_stuff(usbhid);
  569. usb_kill_urb(usbhid->urbin);
  570. usbhid->intf->needs_remote_wakeup = 0;
  571. } else {
  572. spin_unlock_irq(&usbhid->lock);
  573. }
  574. mutex_unlock(&hid_open_mut);
  575. }
  576. /*
  577. * Initialize all reports
  578. */
  579. void usbhid_init_reports(struct hid_device *hid)
  580. {
  581. struct hid_report *report;
  582. struct usbhid_device *usbhid = hid->driver_data;
  583. int err, ret;
  584. list_for_each_entry(report, &hid->report_enum[HID_INPUT_REPORT].report_list, list)
  585. usbhid_submit_report(hid, report, USB_DIR_IN);
  586. list_for_each_entry(report, &hid->report_enum[HID_FEATURE_REPORT].report_list, list)
  587. usbhid_submit_report(hid, report, USB_DIR_IN);
  588. err = 0;
  589. ret = usbhid_wait_io(hid);
  590. while (ret) {
  591. err |= ret;
  592. if (test_bit(HID_CTRL_RUNNING, &usbhid->iofl))
  593. usb_kill_urb(usbhid->urbctrl);
  594. if (test_bit(HID_OUT_RUNNING, &usbhid->iofl))
  595. usb_kill_urb(usbhid->urbout);
  596. ret = usbhid_wait_io(hid);
  597. }
  598. if (err)
  599. dev_warn(&hid->dev, "timeout initializing reports\n");
  600. }
  601. /*
  602. * Reset LEDs which BIOS might have left on. For now, just NumLock (0x01).
  603. */
  604. static int hid_find_field_early(struct hid_device *hid, unsigned int page,
  605. unsigned int hid_code, struct hid_field **pfield)
  606. {
  607. struct hid_report *report;
  608. struct hid_field *field;
  609. struct hid_usage *usage;
  610. int i, j;
  611. list_for_each_entry(report, &hid->report_enum[HID_OUTPUT_REPORT].report_list, list) {
  612. for (i = 0; i < report->maxfield; i++) {
  613. field = report->field[i];
  614. for (j = 0; j < field->maxusage; j++) {
  615. usage = &field->usage[j];
  616. if ((usage->hid & HID_USAGE_PAGE) == page &&
  617. (usage->hid & 0xFFFF) == hid_code) {
  618. *pfield = field;
  619. return j;
  620. }
  621. }
  622. }
  623. }
  624. return -1;
  625. }
  626. void usbhid_set_leds(struct hid_device *hid)
  627. {
  628. struct hid_field *field;
  629. int offset;
  630. if ((offset = hid_find_field_early(hid, HID_UP_LED, 0x01, &field)) != -1) {
  631. hid_set_field(field, offset, 0);
  632. usbhid_submit_report(hid, field->report, USB_DIR_OUT);
  633. }
  634. }
  635. EXPORT_SYMBOL_GPL(usbhid_set_leds);
  636. /*
  637. * Traverse the supplied list of reports and find the longest
  638. */
  639. static void hid_find_max_report(struct hid_device *hid, unsigned int type,
  640. unsigned int *max)
  641. {
  642. struct hid_report *report;
  643. unsigned int size;
  644. list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
  645. size = ((report->size - 1) >> 3) + 1 + hid->report_enum[type].numbered;
  646. if (*max < size)
  647. *max = size;
  648. }
  649. }
  650. static int hid_alloc_buffers(struct usb_device *dev, struct hid_device *hid)
  651. {
  652. struct usbhid_device *usbhid = hid->driver_data;
  653. usbhid->inbuf = usb_buffer_alloc(dev, usbhid->bufsize, GFP_KERNEL,
  654. &usbhid->inbuf_dma);
  655. usbhid->outbuf = usb_buffer_alloc(dev, usbhid->bufsize, GFP_KERNEL,
  656. &usbhid->outbuf_dma);
  657. usbhid->cr = usb_buffer_alloc(dev, sizeof(*usbhid->cr), GFP_KERNEL,
  658. &usbhid->cr_dma);
  659. usbhid->ctrlbuf = usb_buffer_alloc(dev, usbhid->bufsize, GFP_KERNEL,
  660. &usbhid->ctrlbuf_dma);
  661. if (!usbhid->inbuf || !usbhid->outbuf || !usbhid->cr ||
  662. !usbhid->ctrlbuf)
  663. return -1;
  664. return 0;
  665. }
  666. static int usbhid_output_raw_report(struct hid_device *hid, __u8 *buf, size_t count)
  667. {
  668. struct usbhid_device *usbhid = hid->driver_data;
  669. struct usb_device *dev = hid_to_usb_dev(hid);
  670. struct usb_interface *intf = usbhid->intf;
  671. struct usb_host_interface *interface = intf->cur_altsetting;
  672. int ret;
  673. ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  674. HID_REQ_SET_REPORT,
  675. USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  676. ((HID_OUTPUT_REPORT + 1) << 8) | *buf,
  677. interface->desc.bInterfaceNumber, buf + 1, count - 1,
  678. USB_CTRL_SET_TIMEOUT);
  679. /* count also the report id */
  680. if (ret > 0)
  681. ret++;
  682. return ret;
  683. }
  684. static void usbhid_restart_queues(struct usbhid_device *usbhid)
  685. {
  686. if (usbhid->urbout)
  687. usbhid_restart_out_queue(usbhid);
  688. usbhid_restart_ctrl_queue(usbhid);
  689. }
  690. static void __usbhid_restart_queues(struct work_struct *work)
  691. {
  692. struct usbhid_device *usbhid =
  693. container_of(work, struct usbhid_device, restart_work);
  694. int r;
  695. r = usb_autopm_get_interface(usbhid->intf);
  696. if (r < 0)
  697. return;
  698. usb_autopm_put_interface(usbhid->intf);
  699. }
  700. static void hid_free_buffers(struct usb_device *dev, struct hid_device *hid)
  701. {
  702. struct usbhid_device *usbhid = hid->driver_data;
  703. usb_buffer_free(dev, usbhid->bufsize, usbhid->inbuf, usbhid->inbuf_dma);
  704. usb_buffer_free(dev, usbhid->bufsize, usbhid->outbuf, usbhid->outbuf_dma);
  705. usb_buffer_free(dev, sizeof(*(usbhid->cr)), usbhid->cr, usbhid->cr_dma);
  706. usb_buffer_free(dev, usbhid->bufsize, usbhid->ctrlbuf, usbhid->ctrlbuf_dma);
  707. }
  708. static int usbhid_parse(struct hid_device *hid)
  709. {
  710. struct usb_interface *intf = to_usb_interface(hid->dev.parent);
  711. struct usb_host_interface *interface = intf->cur_altsetting;
  712. struct usb_device *dev = interface_to_usbdev (intf);
  713. struct hid_descriptor *hdesc;
  714. u32 quirks = 0;
  715. unsigned int rsize = 0;
  716. char *rdesc;
  717. int ret, n;
  718. quirks = usbhid_lookup_quirk(le16_to_cpu(dev->descriptor.idVendor),
  719. le16_to_cpu(dev->descriptor.idProduct));
  720. if (quirks & HID_QUIRK_IGNORE)
  721. return -ENODEV;
  722. /* Many keyboards and mice don't like to be polled for reports,
  723. * so we will always set the HID_QUIRK_NOGET flag for them. */
  724. if (interface->desc.bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT) {
  725. if (interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_KEYBOARD ||
  726. interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_MOUSE)
  727. quirks |= HID_QUIRK_NOGET;
  728. }
  729. if (usb_get_extra_descriptor(interface, HID_DT_HID, &hdesc) &&
  730. (!interface->desc.bNumEndpoints ||
  731. usb_get_extra_descriptor(&interface->endpoint[0], HID_DT_HID, &hdesc))) {
  732. dbg_hid("class descriptor not present\n");
  733. return -ENODEV;
  734. }
  735. hid->version = le16_to_cpu(hdesc->bcdHID);
  736. hid->country = hdesc->bCountryCode;
  737. for (n = 0; n < hdesc->bNumDescriptors; n++)
  738. if (hdesc->desc[n].bDescriptorType == HID_DT_REPORT)
  739. rsize = le16_to_cpu(hdesc->desc[n].wDescriptorLength);
  740. if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
  741. dbg_hid("weird size of report descriptor (%u)\n", rsize);
  742. return -EINVAL;
  743. }
  744. if (!(rdesc = kmalloc(rsize, GFP_KERNEL))) {
  745. dbg_hid("couldn't allocate rdesc memory\n");
  746. return -ENOMEM;
  747. }
  748. hid_set_idle(dev, interface->desc.bInterfaceNumber, 0, 0);
  749. ret = hid_get_class_descriptor(dev, interface->desc.bInterfaceNumber,
  750. HID_DT_REPORT, rdesc, rsize);
  751. if (ret < 0) {
  752. dbg_hid("reading report descriptor failed\n");
  753. kfree(rdesc);
  754. goto err;
  755. }
  756. ret = hid_parse_report(hid, rdesc, rsize);
  757. kfree(rdesc);
  758. if (ret) {
  759. dbg_hid("parsing report descriptor failed\n");
  760. goto err;
  761. }
  762. hid->quirks |= quirks;
  763. return 0;
  764. err:
  765. return ret;
  766. }
  767. static int usbhid_start(struct hid_device *hid)
  768. {
  769. struct usb_interface *intf = to_usb_interface(hid->dev.parent);
  770. struct usb_host_interface *interface = intf->cur_altsetting;
  771. struct usb_device *dev = interface_to_usbdev(intf);
  772. struct usbhid_device *usbhid = hid->driver_data;
  773. unsigned int n, insize = 0;
  774. int ret;
  775. clear_bit(HID_DISCONNECTED, &usbhid->iofl);
  776. usbhid->bufsize = HID_MIN_BUFFER_SIZE;
  777. hid_find_max_report(hid, HID_INPUT_REPORT, &usbhid->bufsize);
  778. hid_find_max_report(hid, HID_OUTPUT_REPORT, &usbhid->bufsize);
  779. hid_find_max_report(hid, HID_FEATURE_REPORT, &usbhid->bufsize);
  780. if (usbhid->bufsize > HID_MAX_BUFFER_SIZE)
  781. usbhid->bufsize = HID_MAX_BUFFER_SIZE;
  782. hid_find_max_report(hid, HID_INPUT_REPORT, &insize);
  783. if (insize > HID_MAX_BUFFER_SIZE)
  784. insize = HID_MAX_BUFFER_SIZE;
  785. if (hid_alloc_buffers(dev, hid)) {
  786. ret = -ENOMEM;
  787. goto fail;
  788. }
  789. for (n = 0; n < interface->desc.bNumEndpoints; n++) {
  790. struct usb_endpoint_descriptor *endpoint;
  791. int pipe;
  792. int interval;
  793. endpoint = &interface->endpoint[n].desc;
  794. if (!usb_endpoint_xfer_int(endpoint))
  795. continue;
  796. interval = endpoint->bInterval;
  797. /* Some vendors give fullspeed interval on highspeed devides */
  798. if (hid->quirks & HID_QUIRK_FULLSPEED_INTERVAL &&
  799. dev->speed == USB_SPEED_HIGH) {
  800. interval = fls(endpoint->bInterval*8);
  801. printk(KERN_INFO "%s: Fixing fullspeed to highspeed interval: %d -> %d\n",
  802. hid->name, endpoint->bInterval, interval);
  803. }
  804. /* Change the polling interval of mice. */
  805. if (hid->collection->usage == HID_GD_MOUSE && hid_mousepoll_interval > 0)
  806. interval = hid_mousepoll_interval;
  807. ret = -ENOMEM;
  808. if (usb_endpoint_dir_in(endpoint)) {
  809. if (usbhid->urbin)
  810. continue;
  811. if (!(usbhid->urbin = usb_alloc_urb(0, GFP_KERNEL)))
  812. goto fail;
  813. pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
  814. usb_fill_int_urb(usbhid->urbin, dev, pipe, usbhid->inbuf, insize,
  815. hid_irq_in, hid, interval);
  816. usbhid->urbin->transfer_dma = usbhid->inbuf_dma;
  817. usbhid->urbin->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  818. } else {
  819. if (usbhid->urbout)
  820. continue;
  821. if (!(usbhid->urbout = usb_alloc_urb(0, GFP_KERNEL)))
  822. goto fail;
  823. pipe = usb_sndintpipe(dev, endpoint->bEndpointAddress);
  824. usb_fill_int_urb(usbhid->urbout, dev, pipe, usbhid->outbuf, 0,
  825. hid_irq_out, hid, interval);
  826. usbhid->urbout->transfer_dma = usbhid->outbuf_dma;
  827. usbhid->urbout->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  828. }
  829. }
  830. init_waitqueue_head(&usbhid->wait);
  831. INIT_WORK(&usbhid->reset_work, hid_reset);
  832. INIT_WORK(&usbhid->restart_work, __usbhid_restart_queues);
  833. setup_timer(&usbhid->io_retry, hid_retry_timeout, (unsigned long) hid);
  834. spin_lock_init(&usbhid->lock);
  835. usbhid->intf = intf;
  836. usbhid->ifnum = interface->desc.bInterfaceNumber;
  837. usbhid->urbctrl = usb_alloc_urb(0, GFP_KERNEL);
  838. if (!usbhid->urbctrl) {
  839. ret = -ENOMEM;
  840. goto fail;
  841. }
  842. usb_fill_control_urb(usbhid->urbctrl, dev, 0, (void *) usbhid->cr,
  843. usbhid->ctrlbuf, 1, hid_ctrl, hid);
  844. usbhid->urbctrl->setup_dma = usbhid->cr_dma;
  845. usbhid->urbctrl->transfer_dma = usbhid->ctrlbuf_dma;
  846. usbhid->urbctrl->transfer_flags |= (URB_NO_TRANSFER_DMA_MAP | URB_NO_SETUP_DMA_MAP);
  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. struct usb_device *udev = interface_to_usbdev(intf);
  1059. int status;
  1060. if (udev->auto_pm) {
  1061. spin_lock_irq(&usbhid->lock); /* Sync with error handler */
  1062. if (!test_bit(HID_RESET_PENDING, &usbhid->iofl)
  1063. && !test_bit(HID_CLEAR_HALT, &usbhid->iofl)
  1064. && !test_bit(HID_OUT_RUNNING, &usbhid->iofl)
  1065. && !test_bit(HID_CTRL_RUNNING, &usbhid->iofl)
  1066. && !test_bit(HID_KEYS_PRESSED, &usbhid->iofl)
  1067. && (!usbhid->ledcount || ignoreled))
  1068. {
  1069. set_bit(HID_REPORTED_IDLE, &usbhid->iofl);
  1070. spin_unlock_irq(&usbhid->lock);
  1071. } else {
  1072. usbhid_mark_busy(usbhid);
  1073. spin_unlock_irq(&usbhid->lock);
  1074. return -EBUSY;
  1075. }
  1076. } else {
  1077. spin_lock_irq(&usbhid->lock);
  1078. set_bit(HID_REPORTED_IDLE, &usbhid->iofl);
  1079. spin_unlock_irq(&usbhid->lock);
  1080. if (usbhid_wait_io(hid) < 0)
  1081. return -EIO;
  1082. }
  1083. if (!ignoreled && udev->auto_pm) {
  1084. spin_lock_irq(&usbhid->lock);
  1085. if (test_bit(HID_LED_ON, &usbhid->iofl)) {
  1086. spin_unlock_irq(&usbhid->lock);
  1087. usbhid_mark_busy(usbhid);
  1088. return -EBUSY;
  1089. }
  1090. spin_unlock_irq(&usbhid->lock);
  1091. }
  1092. hid_cancel_delayed_stuff(usbhid);
  1093. hid_cease_io(usbhid);
  1094. if (udev->auto_pm && 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_VERSION ":"
  1180. DRIVER_DESC "\n");
  1181. return 0;
  1182. usb_register_fail:
  1183. hiddev_exit();
  1184. hiddev_init_fail:
  1185. usbhid_quirks_exit();
  1186. usbhid_quirks_init_fail:
  1187. hid_unregister_driver(&hid_usb_driver);
  1188. hid_register_fail:
  1189. destroy_workqueue(resumption_waker);
  1190. no_queue:
  1191. return retval;
  1192. }
  1193. static void __exit hid_exit(void)
  1194. {
  1195. usb_deregister(&hid_driver);
  1196. hiddev_exit();
  1197. usbhid_quirks_exit();
  1198. hid_unregister_driver(&hid_usb_driver);
  1199. destroy_workqueue(resumption_waker);
  1200. }
  1201. module_init(hid_init);
  1202. module_exit(hid_exit);
  1203. MODULE_AUTHOR("Andreas Gal");
  1204. MODULE_AUTHOR("Vojtech Pavlik");
  1205. MODULE_AUTHOR("Jiri Kosina");
  1206. MODULE_DESCRIPTION(DRIVER_DESC);
  1207. MODULE_LICENSE(DRIVER_LICENSE);