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