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