hid-core.c 40 KB

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