hid-core.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
  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) 2006-2007 Jiri Kosina
  8. */
  9. /*
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the Free
  12. * Software Foundation; either version 2 of the License, or (at your option)
  13. * any later version.
  14. */
  15. #include <linux/module.h>
  16. #include <linux/slab.h>
  17. #include <linux/init.h>
  18. #include <linux/kernel.h>
  19. #include <linux/list.h>
  20. #include <linux/mm.h>
  21. #include <linux/smp_lock.h>
  22. #include <linux/spinlock.h>
  23. #include <asm/unaligned.h>
  24. #include <asm/byteorder.h>
  25. #include <linux/input.h>
  26. #include <linux/wait.h>
  27. #include <linux/usb.h>
  28. #include <linux/hid.h>
  29. #include <linux/hiddev.h>
  30. #include <linux/hid-debug.h>
  31. #include "usbhid.h"
  32. /*
  33. * Version Information
  34. */
  35. #define DRIVER_VERSION "v2.6"
  36. #define DRIVER_AUTHOR "Andreas Gal, Vojtech Pavlik, Jiri Kosina"
  37. #define DRIVER_DESC "USB HID core driver"
  38. #define DRIVER_LICENSE "GPL"
  39. static char *hid_types[] = {"Device", "Pointer", "Mouse", "Device", "Joystick",
  40. "Gamepad", "Keyboard", "Keypad", "Multi-Axis Controller"};
  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. /*
  48. * Input submission and I/O error handler.
  49. */
  50. static void hid_io_error(struct hid_device *hid);
  51. /* Start up the input URB */
  52. static int hid_start_in(struct hid_device *hid)
  53. {
  54. unsigned long flags;
  55. int rc = 0;
  56. struct usbhid_device *usbhid = hid->driver_data;
  57. spin_lock_irqsave(&usbhid->inlock, flags);
  58. if (hid->open > 0 && !test_bit(HID_SUSPENDED, &usbhid->iofl) &&
  59. !test_and_set_bit(HID_IN_RUNNING, &usbhid->iofl)) {
  60. rc = usb_submit_urb(usbhid->urbin, GFP_ATOMIC);
  61. if (rc != 0)
  62. clear_bit(HID_IN_RUNNING, &usbhid->iofl);
  63. }
  64. spin_unlock_irqrestore(&usbhid->inlock, flags);
  65. return rc;
  66. }
  67. /* I/O retry timer routine */
  68. static void hid_retry_timeout(unsigned long _hid)
  69. {
  70. struct hid_device *hid = (struct hid_device *) _hid;
  71. struct usbhid_device *usbhid = hid->driver_data;
  72. dev_dbg(&usbhid->intf->dev, "retrying intr urb\n");
  73. if (hid_start_in(hid))
  74. hid_io_error(hid);
  75. }
  76. /* Workqueue routine to reset the device or clear a halt */
  77. static void hid_reset(struct work_struct *work)
  78. {
  79. struct usbhid_device *usbhid =
  80. container_of(work, struct usbhid_device, reset_work);
  81. struct hid_device *hid = usbhid->hid;
  82. int rc_lock, rc = 0;
  83. if (test_bit(HID_CLEAR_HALT, &usbhid->iofl)) {
  84. dev_dbg(&usbhid->intf->dev, "clear halt\n");
  85. rc = usb_clear_halt(hid_to_usb_dev(hid), usbhid->urbin->pipe);
  86. clear_bit(HID_CLEAR_HALT, &usbhid->iofl);
  87. hid_start_in(hid);
  88. }
  89. else if (test_bit(HID_RESET_PENDING, &usbhid->iofl)) {
  90. dev_dbg(&usbhid->intf->dev, "resetting device\n");
  91. rc = rc_lock = usb_lock_device_for_reset(hid_to_usb_dev(hid), usbhid->intf);
  92. if (rc_lock >= 0) {
  93. rc = usb_reset_composite_device(hid_to_usb_dev(hid), usbhid->intf);
  94. if (rc_lock)
  95. usb_unlock_device(hid_to_usb_dev(hid));
  96. }
  97. clear_bit(HID_RESET_PENDING, &usbhid->iofl);
  98. }
  99. switch (rc) {
  100. case 0:
  101. if (!test_bit(HID_IN_RUNNING, &usbhid->iofl))
  102. hid_io_error(hid);
  103. break;
  104. default:
  105. err("can't reset device, %s-%s/input%d, status %d",
  106. hid_to_usb_dev(hid)->bus->bus_name,
  107. hid_to_usb_dev(hid)->devpath,
  108. usbhid->ifnum, rc);
  109. /* FALLTHROUGH */
  110. case -EHOSTUNREACH:
  111. case -ENODEV:
  112. case -EINTR:
  113. break;
  114. }
  115. }
  116. /* Main I/O error handler */
  117. static void hid_io_error(struct hid_device *hid)
  118. {
  119. unsigned long flags;
  120. struct usbhid_device *usbhid = hid->driver_data;
  121. spin_lock_irqsave(&usbhid->inlock, flags);
  122. /* Stop when disconnected */
  123. if (usb_get_intfdata(usbhid->intf) == NULL)
  124. goto done;
  125. /* If it has been a while since the last error, we'll assume
  126. * this a brand new error and reset the retry timeout. */
  127. if (time_after(jiffies, usbhid->stop_retry + HZ/2))
  128. usbhid->retry_delay = 0;
  129. /* When an error occurs, retry at increasing intervals */
  130. if (usbhid->retry_delay == 0) {
  131. usbhid->retry_delay = 13; /* Then 26, 52, 104, 104, ... */
  132. usbhid->stop_retry = jiffies + msecs_to_jiffies(1000);
  133. } else if (usbhid->retry_delay < 100)
  134. usbhid->retry_delay *= 2;
  135. if (time_after(jiffies, usbhid->stop_retry)) {
  136. /* Retries failed, so do a port reset */
  137. if (!test_and_set_bit(HID_RESET_PENDING, &usbhid->iofl)) {
  138. schedule_work(&usbhid->reset_work);
  139. goto done;
  140. }
  141. }
  142. mod_timer(&usbhid->io_retry,
  143. jiffies + msecs_to_jiffies(usbhid->retry_delay));
  144. done:
  145. spin_unlock_irqrestore(&usbhid->inlock, flags);
  146. }
  147. /*
  148. * Input interrupt completion handler.
  149. */
  150. static void hid_irq_in(struct urb *urb)
  151. {
  152. struct hid_device *hid = urb->context;
  153. struct usbhid_device *usbhid = hid->driver_data;
  154. int status;
  155. switch (urb->status) {
  156. case 0: /* success */
  157. usbhid->retry_delay = 0;
  158. hid_input_report(urb->context, HID_INPUT_REPORT,
  159. urb->transfer_buffer,
  160. urb->actual_length, 1);
  161. break;
  162. case -EPIPE: /* stall */
  163. clear_bit(HID_IN_RUNNING, &usbhid->iofl);
  164. set_bit(HID_CLEAR_HALT, &usbhid->iofl);
  165. schedule_work(&usbhid->reset_work);
  166. return;
  167. case -ECONNRESET: /* unlink */
  168. case -ENOENT:
  169. case -ESHUTDOWN: /* unplug */
  170. clear_bit(HID_IN_RUNNING, &usbhid->iofl);
  171. return;
  172. case -EILSEQ: /* protocol error or unplug */
  173. case -EPROTO: /* protocol error or unplug */
  174. case -ETIME: /* protocol error or unplug */
  175. case -ETIMEDOUT: /* Should never happen, but... */
  176. clear_bit(HID_IN_RUNNING, &usbhid->iofl);
  177. hid_io_error(hid);
  178. return;
  179. default: /* error */
  180. warn("input irq status %d received", urb->status);
  181. }
  182. status = usb_submit_urb(urb, GFP_ATOMIC);
  183. if (status) {
  184. clear_bit(HID_IN_RUNNING, &usbhid->iofl);
  185. if (status != -EPERM) {
  186. err("can't resubmit intr, %s-%s/input%d, status %d",
  187. hid_to_usb_dev(hid)->bus->bus_name,
  188. hid_to_usb_dev(hid)->devpath,
  189. usbhid->ifnum, status);
  190. hid_io_error(hid);
  191. }
  192. }
  193. }
  194. static int hid_submit_out(struct hid_device *hid)
  195. {
  196. struct hid_report *report;
  197. struct usbhid_device *usbhid = hid->driver_data;
  198. report = usbhid->out[usbhid->outtail];
  199. hid_output_report(report, usbhid->outbuf);
  200. usbhid->urbout->transfer_buffer_length = ((report->size - 1) >> 3) + 1 + (report->id > 0);
  201. usbhid->urbout->dev = hid_to_usb_dev(hid);
  202. dbg("submitting out urb");
  203. if (usb_submit_urb(usbhid->urbout, GFP_ATOMIC)) {
  204. err("usb_submit_urb(out) failed");
  205. return -1;
  206. }
  207. return 0;
  208. }
  209. static int hid_submit_ctrl(struct hid_device *hid)
  210. {
  211. struct hid_report *report;
  212. unsigned char dir;
  213. int len;
  214. struct usbhid_device *usbhid = hid->driver_data;
  215. report = usbhid->ctrl[usbhid->ctrltail].report;
  216. dir = usbhid->ctrl[usbhid->ctrltail].dir;
  217. len = ((report->size - 1) >> 3) + 1 + (report->id > 0);
  218. if (dir == USB_DIR_OUT) {
  219. hid_output_report(report, usbhid->ctrlbuf);
  220. usbhid->urbctrl->pipe = usb_sndctrlpipe(hid_to_usb_dev(hid), 0);
  221. usbhid->urbctrl->transfer_buffer_length = len;
  222. } else {
  223. int maxpacket, padlen;
  224. usbhid->urbctrl->pipe = usb_rcvctrlpipe(hid_to_usb_dev(hid), 0);
  225. maxpacket = usb_maxpacket(hid_to_usb_dev(hid), usbhid->urbctrl->pipe, 0);
  226. if (maxpacket > 0) {
  227. padlen = (len + maxpacket - 1) / maxpacket;
  228. padlen *= maxpacket;
  229. if (padlen > usbhid->bufsize)
  230. padlen = usbhid->bufsize;
  231. } else
  232. padlen = 0;
  233. usbhid->urbctrl->transfer_buffer_length = padlen;
  234. }
  235. usbhid->urbctrl->dev = hid_to_usb_dev(hid);
  236. usbhid->cr->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE | dir;
  237. usbhid->cr->bRequest = (dir == USB_DIR_OUT) ? HID_REQ_SET_REPORT : HID_REQ_GET_REPORT;
  238. usbhid->cr->wValue = cpu_to_le16(((report->type + 1) << 8) | report->id);
  239. usbhid->cr->wIndex = cpu_to_le16(usbhid->ifnum);
  240. usbhid->cr->wLength = cpu_to_le16(len);
  241. dbg("submitting ctrl urb: %s wValue=0x%04x wIndex=0x%04x wLength=%u",
  242. usbhid->cr->bRequest == HID_REQ_SET_REPORT ? "Set_Report" : "Get_Report",
  243. usbhid->cr->wValue, usbhid->cr->wIndex, usbhid->cr->wLength);
  244. if (usb_submit_urb(usbhid->urbctrl, GFP_ATOMIC)) {
  245. err("usb_submit_urb(ctrl) failed");
  246. return -1;
  247. }
  248. return 0;
  249. }
  250. /*
  251. * Output interrupt completion handler.
  252. */
  253. static void hid_irq_out(struct urb *urb)
  254. {
  255. struct hid_device *hid = urb->context;
  256. struct usbhid_device *usbhid = hid->driver_data;
  257. unsigned long flags;
  258. int unplug = 0;
  259. switch (urb->status) {
  260. case 0: /* success */
  261. break;
  262. case -ESHUTDOWN: /* unplug */
  263. unplug = 1;
  264. case -EILSEQ: /* protocol error or unplug */
  265. case -EPROTO: /* protocol error or unplug */
  266. case -ECONNRESET: /* unlink */
  267. case -ENOENT:
  268. break;
  269. default: /* error */
  270. warn("output irq status %d received", urb->status);
  271. }
  272. spin_lock_irqsave(&usbhid->outlock, flags);
  273. if (unplug)
  274. usbhid->outtail = usbhid->outhead;
  275. else
  276. usbhid->outtail = (usbhid->outtail + 1) & (HID_OUTPUT_FIFO_SIZE - 1);
  277. if (usbhid->outhead != usbhid->outtail) {
  278. if (hid_submit_out(hid)) {
  279. clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
  280. wake_up(&hid->wait);
  281. }
  282. spin_unlock_irqrestore(&usbhid->outlock, flags);
  283. return;
  284. }
  285. clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
  286. spin_unlock_irqrestore(&usbhid->outlock, flags);
  287. wake_up(&hid->wait);
  288. }
  289. /*
  290. * Control pipe completion handler.
  291. */
  292. static void hid_ctrl(struct urb *urb)
  293. {
  294. struct hid_device *hid = urb->context;
  295. struct usbhid_device *usbhid = hid->driver_data;
  296. unsigned long flags;
  297. int unplug = 0;
  298. spin_lock_irqsave(&usbhid->ctrllock, flags);
  299. switch (urb->status) {
  300. case 0: /* success */
  301. if (usbhid->ctrl[usbhid->ctrltail].dir == USB_DIR_IN)
  302. hid_input_report(urb->context, usbhid->ctrl[usbhid->ctrltail].report->type,
  303. urb->transfer_buffer, urb->actual_length, 0);
  304. break;
  305. case -ESHUTDOWN: /* unplug */
  306. unplug = 1;
  307. case -EILSEQ: /* protocol error or unplug */
  308. case -EPROTO: /* protocol error or unplug */
  309. case -ECONNRESET: /* unlink */
  310. case -ENOENT:
  311. case -EPIPE: /* report not available */
  312. break;
  313. default: /* error */
  314. warn("ctrl urb status %d received", urb->status);
  315. }
  316. if (unplug)
  317. usbhid->ctrltail = usbhid->ctrlhead;
  318. else
  319. usbhid->ctrltail = (usbhid->ctrltail + 1) & (HID_CONTROL_FIFO_SIZE - 1);
  320. if (usbhid->ctrlhead != usbhid->ctrltail) {
  321. if (hid_submit_ctrl(hid)) {
  322. clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
  323. wake_up(&hid->wait);
  324. }
  325. spin_unlock_irqrestore(&usbhid->ctrllock, flags);
  326. return;
  327. }
  328. clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
  329. spin_unlock_irqrestore(&usbhid->ctrllock, flags);
  330. wake_up(&hid->wait);
  331. }
  332. void usbhid_submit_report(struct hid_device *hid, struct hid_report *report, unsigned char dir)
  333. {
  334. int head;
  335. unsigned long flags;
  336. struct usbhid_device *usbhid = hid->driver_data;
  337. if ((hid->quirks & HID_QUIRK_NOGET) && dir == USB_DIR_IN)
  338. return;
  339. if (usbhid->urbout && dir == USB_DIR_OUT && report->type == HID_OUTPUT_REPORT) {
  340. spin_lock_irqsave(&usbhid->outlock, flags);
  341. if ((head = (usbhid->outhead + 1) & (HID_OUTPUT_FIFO_SIZE - 1)) == usbhid->outtail) {
  342. spin_unlock_irqrestore(&usbhid->outlock, flags);
  343. warn("output queue full");
  344. return;
  345. }
  346. usbhid->out[usbhid->outhead] = report;
  347. usbhid->outhead = head;
  348. if (!test_and_set_bit(HID_OUT_RUNNING, &usbhid->iofl))
  349. if (hid_submit_out(hid))
  350. clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
  351. spin_unlock_irqrestore(&usbhid->outlock, flags);
  352. return;
  353. }
  354. spin_lock_irqsave(&usbhid->ctrllock, flags);
  355. if ((head = (usbhid->ctrlhead + 1) & (HID_CONTROL_FIFO_SIZE - 1)) == usbhid->ctrltail) {
  356. spin_unlock_irqrestore(&usbhid->ctrllock, flags);
  357. warn("control queue full");
  358. return;
  359. }
  360. usbhid->ctrl[usbhid->ctrlhead].report = report;
  361. usbhid->ctrl[usbhid->ctrlhead].dir = dir;
  362. usbhid->ctrlhead = head;
  363. if (!test_and_set_bit(HID_CTRL_RUNNING, &usbhid->iofl))
  364. if (hid_submit_ctrl(hid))
  365. clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
  366. spin_unlock_irqrestore(&usbhid->ctrllock, flags);
  367. }
  368. static int usb_hidinput_input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
  369. {
  370. struct hid_device *hid = dev->private;
  371. struct hid_field *field;
  372. int offset;
  373. if (type == EV_FF)
  374. return input_ff_event(dev, type, code, value);
  375. if (type != EV_LED)
  376. return -1;
  377. if ((offset = hidinput_find_field(hid, type, code, &field)) == -1) {
  378. warn("event field not found");
  379. return -1;
  380. }
  381. hid_set_field(field, offset, value);
  382. usbhid_submit_report(hid, field->report, USB_DIR_OUT);
  383. return 0;
  384. }
  385. int usbhid_wait_io(struct hid_device *hid)
  386. {
  387. struct usbhid_device *usbhid = hid->driver_data;
  388. if (!wait_event_timeout(hid->wait, (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl) &&
  389. !test_bit(HID_OUT_RUNNING, &usbhid->iofl)),
  390. 10*HZ)) {
  391. dbg("timeout waiting for ctrl or out queue to clear");
  392. return -1;
  393. }
  394. return 0;
  395. }
  396. static int hid_set_idle(struct usb_device *dev, int ifnum, int report, int idle)
  397. {
  398. return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  399. HID_REQ_SET_IDLE, USB_TYPE_CLASS | USB_RECIP_INTERFACE, (idle << 8) | report,
  400. ifnum, NULL, 0, USB_CTRL_SET_TIMEOUT);
  401. }
  402. static int hid_get_class_descriptor(struct usb_device *dev, int ifnum,
  403. unsigned char type, void *buf, int size)
  404. {
  405. int result, retries = 4;
  406. memset(buf, 0, size);
  407. do {
  408. result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  409. USB_REQ_GET_DESCRIPTOR, USB_RECIP_INTERFACE | USB_DIR_IN,
  410. (type << 8), ifnum, buf, size, USB_CTRL_GET_TIMEOUT);
  411. retries--;
  412. } while (result < size && retries);
  413. return result;
  414. }
  415. int usbhid_open(struct hid_device *hid)
  416. {
  417. ++hid->open;
  418. if (hid_start_in(hid))
  419. hid_io_error(hid);
  420. return 0;
  421. }
  422. void usbhid_close(struct hid_device *hid)
  423. {
  424. struct usbhid_device *usbhid = hid->driver_data;
  425. if (!--hid->open)
  426. usb_kill_urb(usbhid->urbin);
  427. }
  428. /*
  429. * Initialize all reports
  430. */
  431. void usbhid_init_reports(struct hid_device *hid)
  432. {
  433. struct hid_report *report;
  434. struct usbhid_device *usbhid = hid->driver_data;
  435. int err, ret;
  436. list_for_each_entry(report, &hid->report_enum[HID_INPUT_REPORT].report_list, list)
  437. usbhid_submit_report(hid, report, USB_DIR_IN);
  438. list_for_each_entry(report, &hid->report_enum[HID_FEATURE_REPORT].report_list, list)
  439. usbhid_submit_report(hid, report, USB_DIR_IN);
  440. err = 0;
  441. ret = usbhid_wait_io(hid);
  442. while (ret) {
  443. err |= ret;
  444. if (test_bit(HID_CTRL_RUNNING, &usbhid->iofl))
  445. usb_kill_urb(usbhid->urbctrl);
  446. if (test_bit(HID_OUT_RUNNING, &usbhid->iofl))
  447. usb_kill_urb(usbhid->urbout);
  448. ret = usbhid_wait_io(hid);
  449. }
  450. if (err)
  451. warn("timeout initializing reports");
  452. }
  453. /*
  454. * Reset LEDs which BIOS might have left on. For now, just NumLock (0x01).
  455. */
  456. static int hid_find_field_early(struct hid_device *hid, unsigned int page,
  457. unsigned int hid_code, struct hid_field **pfield)
  458. {
  459. struct hid_report *report;
  460. struct hid_field *field;
  461. struct hid_usage *usage;
  462. int i, j;
  463. list_for_each_entry(report, &hid->report_enum[HID_OUTPUT_REPORT].report_list, list) {
  464. for (i = 0; i < report->maxfield; i++) {
  465. field = report->field[i];
  466. for (j = 0; j < field->maxusage; j++) {
  467. usage = &field->usage[j];
  468. if ((usage->hid & HID_USAGE_PAGE) == page &&
  469. (usage->hid & 0xFFFF) == hid_code) {
  470. *pfield = field;
  471. return j;
  472. }
  473. }
  474. }
  475. }
  476. return -1;
  477. }
  478. static void usbhid_set_leds(struct hid_device *hid)
  479. {
  480. struct hid_field *field;
  481. int offset;
  482. if ((offset = hid_find_field_early(hid, HID_UP_LED, 0x01, &field)) != -1) {
  483. hid_set_field(field, offset, 0);
  484. usbhid_submit_report(hid, field->report, USB_DIR_OUT);
  485. }
  486. }
  487. /*
  488. * Traverse the supplied list of reports and find the longest
  489. */
  490. static void hid_find_max_report(struct hid_device *hid, unsigned int type, int *max)
  491. {
  492. struct hid_report *report;
  493. int size;
  494. list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
  495. size = ((report->size - 1) >> 3) + 1;
  496. if (type == HID_INPUT_REPORT && hid->report_enum[type].numbered)
  497. size++;
  498. if (*max < size)
  499. *max = size;
  500. }
  501. }
  502. static int hid_alloc_buffers(struct usb_device *dev, struct hid_device *hid)
  503. {
  504. struct usbhid_device *usbhid = hid->driver_data;
  505. if (!(usbhid->inbuf = usb_buffer_alloc(dev, usbhid->bufsize, GFP_ATOMIC, &usbhid->inbuf_dma)))
  506. return -1;
  507. if (!(usbhid->outbuf = usb_buffer_alloc(dev, usbhid->bufsize, GFP_ATOMIC, &usbhid->outbuf_dma)))
  508. return -1;
  509. if (!(usbhid->cr = usb_buffer_alloc(dev, sizeof(*(usbhid->cr)), GFP_ATOMIC, &usbhid->cr_dma)))
  510. return -1;
  511. if (!(usbhid->ctrlbuf = usb_buffer_alloc(dev, usbhid->bufsize, GFP_ATOMIC, &usbhid->ctrlbuf_dma)))
  512. return -1;
  513. return 0;
  514. }
  515. static void hid_free_buffers(struct usb_device *dev, struct hid_device *hid)
  516. {
  517. struct usbhid_device *usbhid = hid->driver_data;
  518. if (usbhid->inbuf)
  519. usb_buffer_free(dev, usbhid->bufsize, usbhid->inbuf, usbhid->inbuf_dma);
  520. if (usbhid->outbuf)
  521. usb_buffer_free(dev, usbhid->bufsize, usbhid->outbuf, usbhid->outbuf_dma);
  522. if (usbhid->cr)
  523. usb_buffer_free(dev, sizeof(*(usbhid->cr)), usbhid->cr, usbhid->cr_dma);
  524. if (usbhid->ctrlbuf)
  525. usb_buffer_free(dev, usbhid->bufsize, usbhid->ctrlbuf, usbhid->ctrlbuf_dma);
  526. }
  527. /*
  528. * Cherry Cymotion keyboard have an invalid HID report descriptor,
  529. * that needs fixing before we can parse it.
  530. */
  531. static void hid_fixup_cymotion_descriptor(char *rdesc, int rsize)
  532. {
  533. if (rsize >= 17 && rdesc[11] == 0x3c && rdesc[12] == 0x02) {
  534. info("Fixing up Cherry Cymotion report descriptor");
  535. rdesc[11] = rdesc[16] = 0xff;
  536. rdesc[12] = rdesc[17] = 0x03;
  537. }
  538. }
  539. /*
  540. * Sending HID_REQ_GET_REPORT changes the operation mode of the ps3 controller
  541. * to "operational". Without this, the ps3 controller will not report any
  542. * events.
  543. */
  544. static void hid_fixup_sony_ps3_controller(struct usb_device *dev, int ifnum)
  545. {
  546. int result;
  547. char *buf = kmalloc(18, GFP_KERNEL);
  548. if (!buf)
  549. return;
  550. result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  551. HID_REQ_GET_REPORT,
  552. USB_DIR_IN | USB_TYPE_CLASS |
  553. USB_RECIP_INTERFACE,
  554. (3 << 8) | 0xf2, ifnum, buf, 17,
  555. USB_CTRL_GET_TIMEOUT);
  556. if (result < 0)
  557. err("%s failed: %d\n", __func__, result);
  558. kfree(buf);
  559. }
  560. /*
  561. * Certain Logitech keyboards send in report #3 keys which are far
  562. * above the logical maximum described in descriptor. This extends
  563. * the original value of 0x28c of logical maximum to 0x104d
  564. */
  565. static void hid_fixup_logitech_descriptor(unsigned char *rdesc, int rsize)
  566. {
  567. if (rsize >= 90 && rdesc[83] == 0x26
  568. && rdesc[84] == 0x8c
  569. && rdesc[85] == 0x02) {
  570. info("Fixing up Logitech keyboard report descriptor");
  571. rdesc[84] = rdesc[89] = 0x4d;
  572. rdesc[85] = rdesc[90] = 0x10;
  573. }
  574. }
  575. static struct hid_device *usb_hid_configure(struct usb_interface *intf)
  576. {
  577. struct usb_host_interface *interface = intf->cur_altsetting;
  578. struct usb_device *dev = interface_to_usbdev (intf);
  579. struct hid_descriptor *hdesc;
  580. struct hid_device *hid;
  581. u32 quirks = 0;
  582. unsigned rsize = 0;
  583. char *rdesc;
  584. int n, len, insize = 0;
  585. struct usbhid_device *usbhid;
  586. quirks = usbhid_lookup_quirk(le16_to_cpu(dev->descriptor.idVendor),
  587. le16_to_cpu(dev->descriptor.idProduct));
  588. /* Many keyboards and mice don't like to be polled for reports,
  589. * so we will always set the HID_QUIRK_NOGET flag for them. */
  590. if (interface->desc.bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT) {
  591. if (interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_KEYBOARD ||
  592. interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_MOUSE)
  593. quirks |= HID_QUIRK_NOGET;
  594. }
  595. if (quirks & HID_QUIRK_IGNORE)
  596. return NULL;
  597. if ((quirks & HID_QUIRK_IGNORE_MOUSE) &&
  598. (interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_MOUSE))
  599. return NULL;
  600. if (usb_get_extra_descriptor(interface, HID_DT_HID, &hdesc) &&
  601. (!interface->desc.bNumEndpoints ||
  602. usb_get_extra_descriptor(&interface->endpoint[0], HID_DT_HID, &hdesc))) {
  603. dbg("class descriptor not present\n");
  604. return NULL;
  605. }
  606. for (n = 0; n < hdesc->bNumDescriptors; n++)
  607. if (hdesc->desc[n].bDescriptorType == HID_DT_REPORT)
  608. rsize = le16_to_cpu(hdesc->desc[n].wDescriptorLength);
  609. if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
  610. dbg("weird size of report descriptor (%u)", rsize);
  611. return NULL;
  612. }
  613. if (!(rdesc = kmalloc(rsize, GFP_KERNEL))) {
  614. dbg("couldn't allocate rdesc memory");
  615. return NULL;
  616. }
  617. hid_set_idle(dev, interface->desc.bInterfaceNumber, 0, 0);
  618. if ((n = hid_get_class_descriptor(dev, interface->desc.bInterfaceNumber, HID_DT_REPORT, rdesc, rsize)) < 0) {
  619. dbg("reading report descriptor failed");
  620. kfree(rdesc);
  621. return NULL;
  622. }
  623. if ((quirks & HID_QUIRK_CYMOTION))
  624. hid_fixup_cymotion_descriptor(rdesc, rsize);
  625. if (quirks & HID_QUIRK_LOGITECH_DESCRIPTOR)
  626. hid_fixup_logitech_descriptor(rdesc, rsize);
  627. #ifdef CONFIG_HID_DEBUG
  628. printk(KERN_DEBUG __FILE__ ": report descriptor (size %u, read %d) = ", rsize, n);
  629. for (n = 0; n < rsize; n++)
  630. printk(" %02x", (unsigned char) rdesc[n]);
  631. printk("\n");
  632. #endif
  633. if (!(hid = hid_parse_report(rdesc, n))) {
  634. dbg("parsing report descriptor failed");
  635. kfree(rdesc);
  636. return NULL;
  637. }
  638. kfree(rdesc);
  639. hid->quirks = quirks;
  640. if (!(usbhid = kzalloc(sizeof(struct usbhid_device), GFP_KERNEL)))
  641. goto fail;
  642. hid->driver_data = usbhid;
  643. usbhid->hid = hid;
  644. usbhid->bufsize = HID_MIN_BUFFER_SIZE;
  645. hid_find_max_report(hid, HID_INPUT_REPORT, &usbhid->bufsize);
  646. hid_find_max_report(hid, HID_OUTPUT_REPORT, &usbhid->bufsize);
  647. hid_find_max_report(hid, HID_FEATURE_REPORT, &usbhid->bufsize);
  648. if (usbhid->bufsize > HID_MAX_BUFFER_SIZE)
  649. usbhid->bufsize = HID_MAX_BUFFER_SIZE;
  650. hid_find_max_report(hid, HID_INPUT_REPORT, &insize);
  651. if (insize > HID_MAX_BUFFER_SIZE)
  652. insize = HID_MAX_BUFFER_SIZE;
  653. if (hid_alloc_buffers(dev, hid)) {
  654. hid_free_buffers(dev, hid);
  655. goto fail;
  656. }
  657. for (n = 0; n < interface->desc.bNumEndpoints; n++) {
  658. struct usb_endpoint_descriptor *endpoint;
  659. int pipe;
  660. int interval;
  661. endpoint = &interface->endpoint[n].desc;
  662. if ((endpoint->bmAttributes & 3) != 3) /* Not an interrupt endpoint */
  663. continue;
  664. interval = endpoint->bInterval;
  665. /* Change the polling interval of mice. */
  666. if (hid->collection->usage == HID_GD_MOUSE && hid_mousepoll_interval > 0)
  667. interval = hid_mousepoll_interval;
  668. if (usb_endpoint_dir_in(endpoint)) {
  669. if (usbhid->urbin)
  670. continue;
  671. if (!(usbhid->urbin = usb_alloc_urb(0, GFP_KERNEL)))
  672. goto fail;
  673. pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
  674. usb_fill_int_urb(usbhid->urbin, dev, pipe, usbhid->inbuf, insize,
  675. hid_irq_in, hid, interval);
  676. usbhid->urbin->transfer_dma = usbhid->inbuf_dma;
  677. usbhid->urbin->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  678. } else {
  679. if (usbhid->urbout)
  680. continue;
  681. if (!(usbhid->urbout = usb_alloc_urb(0, GFP_KERNEL)))
  682. goto fail;
  683. pipe = usb_sndintpipe(dev, endpoint->bEndpointAddress);
  684. usb_fill_int_urb(usbhid->urbout, dev, pipe, usbhid->outbuf, 0,
  685. hid_irq_out, hid, interval);
  686. usbhid->urbout->transfer_dma = usbhid->outbuf_dma;
  687. usbhid->urbout->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  688. }
  689. }
  690. if (!usbhid->urbin) {
  691. err("couldn't find an input interrupt endpoint");
  692. goto fail;
  693. }
  694. init_waitqueue_head(&hid->wait);
  695. INIT_WORK(&usbhid->reset_work, hid_reset);
  696. setup_timer(&usbhid->io_retry, hid_retry_timeout, (unsigned long) hid);
  697. spin_lock_init(&usbhid->inlock);
  698. spin_lock_init(&usbhid->outlock);
  699. spin_lock_init(&usbhid->ctrllock);
  700. hid->version = le16_to_cpu(hdesc->bcdHID);
  701. hid->country = hdesc->bCountryCode;
  702. hid->dev = &intf->dev;
  703. usbhid->intf = intf;
  704. usbhid->ifnum = interface->desc.bInterfaceNumber;
  705. hid->name[0] = 0;
  706. if (dev->manufacturer)
  707. strlcpy(hid->name, dev->manufacturer, sizeof(hid->name));
  708. if (dev->product) {
  709. if (dev->manufacturer)
  710. strlcat(hid->name, " ", sizeof(hid->name));
  711. strlcat(hid->name, dev->product, sizeof(hid->name));
  712. }
  713. if (!strlen(hid->name))
  714. snprintf(hid->name, sizeof(hid->name), "HID %04x:%04x",
  715. le16_to_cpu(dev->descriptor.idVendor),
  716. le16_to_cpu(dev->descriptor.idProduct));
  717. hid->bus = BUS_USB;
  718. hid->vendor = le16_to_cpu(dev->descriptor.idVendor);
  719. hid->product = le16_to_cpu(dev->descriptor.idProduct);
  720. usb_make_path(dev, hid->phys, sizeof(hid->phys));
  721. strlcat(hid->phys, "/input", sizeof(hid->phys));
  722. len = strlen(hid->phys);
  723. if (len < sizeof(hid->phys) - 1)
  724. snprintf(hid->phys + len, sizeof(hid->phys) - len,
  725. "%d", intf->altsetting[0].desc.bInterfaceNumber);
  726. if (usb_string(dev, dev->descriptor.iSerialNumber, hid->uniq, 64) <= 0)
  727. hid->uniq[0] = 0;
  728. usbhid->urbctrl = usb_alloc_urb(0, GFP_KERNEL);
  729. if (!usbhid->urbctrl)
  730. goto fail;
  731. usb_fill_control_urb(usbhid->urbctrl, dev, 0, (void *) usbhid->cr,
  732. usbhid->ctrlbuf, 1, hid_ctrl, hid);
  733. usbhid->urbctrl->setup_dma = usbhid->cr_dma;
  734. usbhid->urbctrl->transfer_dma = usbhid->ctrlbuf_dma;
  735. usbhid->urbctrl->transfer_flags |= (URB_NO_TRANSFER_DMA_MAP | URB_NO_SETUP_DMA_MAP);
  736. hid->hidinput_input_event = usb_hidinput_input_event;
  737. hid->hid_open = usbhid_open;
  738. hid->hid_close = usbhid_close;
  739. #ifdef CONFIG_USB_HIDDEV
  740. hid->hiddev_hid_event = hiddev_hid_event;
  741. hid->hiddev_report_event = hiddev_report_event;
  742. #endif
  743. return hid;
  744. fail:
  745. usb_free_urb(usbhid->urbin);
  746. usb_free_urb(usbhid->urbout);
  747. usb_free_urb(usbhid->urbctrl);
  748. hid_free_buffers(dev, hid);
  749. hid_free_device(hid);
  750. return NULL;
  751. }
  752. static void hid_disconnect(struct usb_interface *intf)
  753. {
  754. struct hid_device *hid = usb_get_intfdata (intf);
  755. struct usbhid_device *usbhid;
  756. if (!hid)
  757. return;
  758. usbhid = hid->driver_data;
  759. spin_lock_irq(&usbhid->inlock); /* Sync with error handler */
  760. usb_set_intfdata(intf, NULL);
  761. spin_unlock_irq(&usbhid->inlock);
  762. usb_kill_urb(usbhid->urbin);
  763. usb_kill_urb(usbhid->urbout);
  764. usb_kill_urb(usbhid->urbctrl);
  765. del_timer_sync(&usbhid->io_retry);
  766. flush_scheduled_work();
  767. if (hid->claimed & HID_CLAIMED_INPUT)
  768. hidinput_disconnect(hid);
  769. if (hid->claimed & HID_CLAIMED_HIDDEV)
  770. hiddev_disconnect(hid);
  771. usb_free_urb(usbhid->urbin);
  772. usb_free_urb(usbhid->urbctrl);
  773. usb_free_urb(usbhid->urbout);
  774. hid_free_buffers(hid_to_usb_dev(hid), hid);
  775. hid_free_device(hid);
  776. }
  777. static int hid_probe(struct usb_interface *intf, const struct usb_device_id *id)
  778. {
  779. struct hid_device *hid;
  780. char path[64];
  781. int i;
  782. char *c;
  783. dbg("HID probe called for ifnum %d",
  784. intf->altsetting->desc.bInterfaceNumber);
  785. if (!(hid = usb_hid_configure(intf)))
  786. return -ENODEV;
  787. usbhid_init_reports(hid);
  788. hid_dump_device(hid);
  789. if (hid->quirks & HID_QUIRK_RESET_LEDS)
  790. usbhid_set_leds(hid);
  791. if (!hidinput_connect(hid))
  792. hid->claimed |= HID_CLAIMED_INPUT;
  793. if (!hiddev_connect(hid))
  794. hid->claimed |= HID_CLAIMED_HIDDEV;
  795. usb_set_intfdata(intf, hid);
  796. if (!hid->claimed) {
  797. printk ("HID device not claimed by input or hiddev\n");
  798. hid_disconnect(intf);
  799. return -ENODEV;
  800. }
  801. if ((hid->claimed & HID_CLAIMED_INPUT))
  802. hid_ff_init(hid);
  803. if (hid->quirks & HID_QUIRK_SONY_PS3_CONTROLLER)
  804. hid_fixup_sony_ps3_controller(interface_to_usbdev(intf),
  805. intf->cur_altsetting->desc.bInterfaceNumber);
  806. printk(KERN_INFO);
  807. if (hid->claimed & HID_CLAIMED_INPUT)
  808. printk("input");
  809. if (hid->claimed == (HID_CLAIMED_INPUT | HID_CLAIMED_HIDDEV))
  810. printk(",");
  811. if (hid->claimed & HID_CLAIMED_HIDDEV)
  812. printk("hiddev%d", hid->minor);
  813. c = "Device";
  814. for (i = 0; i < hid->maxcollection; i++) {
  815. if (hid->collection[i].type == HID_COLLECTION_APPLICATION &&
  816. (hid->collection[i].usage & HID_USAGE_PAGE) == HID_UP_GENDESK &&
  817. (hid->collection[i].usage & 0xffff) < ARRAY_SIZE(hid_types)) {
  818. c = hid_types[hid->collection[i].usage & 0xffff];
  819. break;
  820. }
  821. }
  822. usb_make_path(interface_to_usbdev(intf), path, 63);
  823. printk(": USB HID v%x.%02x %s [%s] on %s\n",
  824. hid->version >> 8, hid->version & 0xff, c, hid->name, path);
  825. return 0;
  826. }
  827. static int hid_suspend(struct usb_interface *intf, pm_message_t message)
  828. {
  829. struct hid_device *hid = usb_get_intfdata (intf);
  830. struct usbhid_device *usbhid = hid->driver_data;
  831. spin_lock_irq(&usbhid->inlock); /* Sync with error handler */
  832. set_bit(HID_SUSPENDED, &usbhid->iofl);
  833. spin_unlock_irq(&usbhid->inlock);
  834. del_timer(&usbhid->io_retry);
  835. usb_kill_urb(usbhid->urbin);
  836. dev_dbg(&intf->dev, "suspend\n");
  837. return 0;
  838. }
  839. static int hid_resume(struct usb_interface *intf)
  840. {
  841. struct hid_device *hid = usb_get_intfdata (intf);
  842. struct usbhid_device *usbhid = hid->driver_data;
  843. int status;
  844. clear_bit(HID_SUSPENDED, &usbhid->iofl);
  845. usbhid->retry_delay = 0;
  846. status = hid_start_in(hid);
  847. dev_dbg(&intf->dev, "resume status %d\n", status);
  848. return status;
  849. }
  850. /* Treat USB reset pretty much the same as suspend/resume */
  851. static void hid_pre_reset(struct usb_interface *intf)
  852. {
  853. /* FIXME: What if the interface is already suspended? */
  854. hid_suspend(intf, PMSG_ON);
  855. }
  856. static void hid_post_reset(struct usb_interface *intf)
  857. {
  858. struct usb_device *dev = interface_to_usbdev (intf);
  859. hid_set_idle(dev, intf->cur_altsetting->desc.bInterfaceNumber, 0, 0);
  860. /* FIXME: Any more reinitialization needed? */
  861. hid_resume(intf);
  862. }
  863. static struct usb_device_id hid_usb_ids [] = {
  864. { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
  865. .bInterfaceClass = USB_INTERFACE_CLASS_HID },
  866. { } /* Terminating entry */
  867. };
  868. MODULE_DEVICE_TABLE (usb, hid_usb_ids);
  869. static struct usb_driver hid_driver = {
  870. .name = "usbhid",
  871. .probe = hid_probe,
  872. .disconnect = hid_disconnect,
  873. .suspend = hid_suspend,
  874. .resume = hid_resume,
  875. .pre_reset = hid_pre_reset,
  876. .post_reset = hid_post_reset,
  877. .id_table = hid_usb_ids,
  878. };
  879. static int __init hid_init(void)
  880. {
  881. int retval;
  882. retval = hiddev_init();
  883. if (retval)
  884. goto hiddev_init_fail;
  885. retval = usb_register(&hid_driver);
  886. if (retval)
  887. goto usb_register_fail;
  888. info(DRIVER_VERSION ":" DRIVER_DESC);
  889. return 0;
  890. usb_register_fail:
  891. hiddev_exit();
  892. hiddev_init_fail:
  893. return retval;
  894. }
  895. static void __exit hid_exit(void)
  896. {
  897. usb_deregister(&hid_driver);
  898. hiddev_exit();
  899. }
  900. module_init(hid_init);
  901. module_exit(hid_exit);
  902. MODULE_AUTHOR(DRIVER_AUTHOR);
  903. MODULE_DESCRIPTION(DRIVER_DESC);
  904. MODULE_LICENSE(DRIVER_LICENSE);