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