hid-core.c 42 KB

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