hid-core.c 40 KB

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