hid-core.c 43 KB

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