Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * Copyright (c) 1999-2001 Vojtech Pavlik |
| 3 | * |
| 4 | * USB HIDBP Mouse support |
| 5 | */ |
| 6 | |
| 7 | /* |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 10 | * the Free Software Foundation; either version 2 of the License, or |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | * (at your option) any later version. |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 12 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 17 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 21 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | * Should you need to contact me, the author, you can do so either by |
| 23 | * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail: |
| 24 | * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic |
| 25 | */ |
| 26 | |
| 27 | #include <linux/kernel.h> |
| 28 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <linux/module.h> |
| 30 | #include <linux/init.h> |
David Brownell | ae0dadc | 2006-06-13 10:04:34 -0700 | [diff] [blame] | 31 | #include <linux/usb/input.h> |
Michael Opdenacker | 4ef2e23 | 2007-02-21 22:51:25 +0100 | [diff] [blame] | 32 | #include <linux/hid.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
Jiri Slaby | 8c19a51 | 2008-06-18 23:36:49 +0200 | [diff] [blame] | 34 | /* for apple IDs */ |
| 35 | #ifdef CONFIG_USB_HID_MODULE |
| 36 | #include "../hid-ids.h" |
| 37 | #endif |
| 38 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | /* |
| 40 | * Version Information |
| 41 | */ |
| 42 | #define DRIVER_VERSION "v1.6" |
| 43 | #define DRIVER_AUTHOR "Vojtech Pavlik <vojtech@ucw.cz>" |
| 44 | #define DRIVER_DESC "USB HID Boot Protocol mouse driver" |
| 45 | #define DRIVER_LICENSE "GPL" |
| 46 | |
| 47 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 48 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 49 | MODULE_LICENSE(DRIVER_LICENSE); |
| 50 | |
| 51 | struct usb_mouse { |
| 52 | char name[128]; |
| 53 | char phys[64]; |
| 54 | struct usb_device *usbdev; |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 55 | struct input_dev *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | struct urb *irq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
| 58 | signed char *data; |
| 59 | dma_addr_t data_dma; |
| 60 | }; |
| 61 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 62 | static void usb_mouse_irq(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | { |
| 64 | struct usb_mouse *mouse = urb->context; |
| 65 | signed char *data = mouse->data; |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 66 | struct input_dev *dev = mouse->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | int status; |
| 68 | |
| 69 | switch (urb->status) { |
| 70 | case 0: /* success */ |
| 71 | break; |
| 72 | case -ECONNRESET: /* unlink */ |
| 73 | case -ENOENT: |
| 74 | case -ESHUTDOWN: |
| 75 | return; |
| 76 | /* -EPIPE: should clear the halt */ |
| 77 | default: /* error */ |
| 78 | goto resubmit; |
| 79 | } |
| 80 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | input_report_key(dev, BTN_LEFT, data[0] & 0x01); |
| 82 | input_report_key(dev, BTN_RIGHT, data[0] & 0x02); |
| 83 | input_report_key(dev, BTN_MIDDLE, data[0] & 0x04); |
| 84 | input_report_key(dev, BTN_SIDE, data[0] & 0x08); |
| 85 | input_report_key(dev, BTN_EXTRA, data[0] & 0x10); |
| 86 | |
| 87 | input_report_rel(dev, REL_X, data[1]); |
| 88 | input_report_rel(dev, REL_Y, data[2]); |
| 89 | input_report_rel(dev, REL_WHEEL, data[3]); |
| 90 | |
| 91 | input_sync(dev); |
| 92 | resubmit: |
Christoph Lameter | 54e6ecb | 2006-12-06 20:33:16 -0800 | [diff] [blame] | 93 | status = usb_submit_urb (urb, GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | if (status) |
| 95 | err ("can't resubmit intr, %s-%s/input0, status %d", |
| 96 | mouse->usbdev->bus->bus_name, |
| 97 | mouse->usbdev->devpath, status); |
| 98 | } |
| 99 | |
| 100 | static int usb_mouse_open(struct input_dev *dev) |
| 101 | { |
Dmitry Torokhov | e071298 | 2007-05-09 10:17:31 +0200 | [diff] [blame] | 102 | struct usb_mouse *mouse = input_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | mouse->irq->dev = mouse->usbdev; |
Dmitry Torokhov | 65cde54 | 2005-05-29 02:29:38 -0500 | [diff] [blame] | 105 | if (usb_submit_urb(mouse->irq, GFP_KERNEL)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | static void usb_mouse_close(struct input_dev *dev) |
| 112 | { |
Dmitry Torokhov | e071298 | 2007-05-09 10:17:31 +0200 | [diff] [blame] | 113 | struct usb_mouse *mouse = input_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | |
Dmitry Torokhov | 65cde54 | 2005-05-29 02:29:38 -0500 | [diff] [blame] | 115 | usb_kill_urb(mouse->irq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 118 | static int usb_mouse_probe(struct usb_interface *intf, const struct usb_device_id *id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | { |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 120 | struct usb_device *dev = interface_to_usbdev(intf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | struct usb_host_interface *interface; |
| 122 | struct usb_endpoint_descriptor *endpoint; |
| 123 | struct usb_mouse *mouse; |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 124 | struct input_dev *input_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | int pipe, maxp; |
Dmitry Torokhov | 5d6341c | 2007-04-04 10:40:57 +0200 | [diff] [blame] | 126 | int error = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | |
| 128 | interface = intf->cur_altsetting; |
| 129 | |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 130 | if (interface->desc.bNumEndpoints != 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | return -ENODEV; |
| 132 | |
| 133 | endpoint = &interface->endpoint[0].desc; |
Luiz Fernando N. Capitulino | 30f36ef | 2006-10-26 13:03:00 -0300 | [diff] [blame] | 134 | if (!usb_endpoint_is_int_in(endpoint)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | return -ENODEV; |
| 136 | |
| 137 | pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress); |
| 138 | maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe)); |
| 139 | |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 140 | mouse = kzalloc(sizeof(struct usb_mouse), GFP_KERNEL); |
| 141 | input_dev = input_allocate_device(); |
| 142 | if (!mouse || !input_dev) |
| 143 | goto fail1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | |
Daniel Mack | 997ea58 | 2010-04-12 13:17:25 +0200 | [diff] [blame] | 145 | mouse->data = usb_alloc_coherent(dev, 8, GFP_ATOMIC, &mouse->data_dma); |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 146 | if (!mouse->data) |
| 147 | goto fail1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | |
| 149 | mouse->irq = usb_alloc_urb(0, GFP_KERNEL); |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 150 | if (!mouse->irq) |
| 151 | goto fail2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | |
| 153 | mouse->usbdev = dev; |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 154 | mouse->dev = input_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | |
| 156 | if (dev->manufacturer) |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 157 | strlcpy(mouse->name, dev->manufacturer, sizeof(mouse->name)); |
| 158 | |
| 159 | if (dev->product) { |
| 160 | if (dev->manufacturer) |
| 161 | strlcat(mouse->name, " ", sizeof(mouse->name)); |
| 162 | strlcat(mouse->name, dev->product, sizeof(mouse->name)); |
| 163 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | |
| 165 | if (!strlen(mouse->name)) |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 166 | snprintf(mouse->name, sizeof(mouse->name), |
| 167 | "USB HIDBP Mouse %04x:%04x", |
| 168 | le16_to_cpu(dev->descriptor.idVendor), |
| 169 | le16_to_cpu(dev->descriptor.idProduct)); |
| 170 | |
| 171 | usb_make_path(dev, mouse->phys, sizeof(mouse->phys)); |
| 172 | strlcat(mouse->phys, "/input0", sizeof(mouse->phys)); |
| 173 | |
| 174 | input_dev->name = mouse->name; |
| 175 | input_dev->phys = mouse->phys; |
| 176 | usb_to_input_id(dev, &input_dev->id); |
Dmitry Torokhov | e071298 | 2007-05-09 10:17:31 +0200 | [diff] [blame] | 177 | input_dev->dev.parent = &intf->dev; |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 178 | |
Jiri Slaby | 7b19ada | 2007-10-18 23:40:32 -0700 | [diff] [blame] | 179 | input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL); |
| 180 | input_dev->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_LEFT) | |
| 181 | BIT_MASK(BTN_RIGHT) | BIT_MASK(BTN_MIDDLE); |
| 182 | input_dev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y); |
| 183 | input_dev->keybit[BIT_WORD(BTN_MOUSE)] |= BIT_MASK(BTN_SIDE) | |
| 184 | BIT_MASK(BTN_EXTRA); |
| 185 | input_dev->relbit[0] |= BIT_MASK(REL_WHEEL); |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 186 | |
Dmitry Torokhov | e071298 | 2007-05-09 10:17:31 +0200 | [diff] [blame] | 187 | input_set_drvdata(input_dev, mouse); |
| 188 | |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 189 | input_dev->open = usb_mouse_open; |
| 190 | input_dev->close = usb_mouse_close; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | |
| 192 | usb_fill_int_urb(mouse->irq, dev, pipe, mouse->data, |
| 193 | (maxp > 8 ? 8 : maxp), |
| 194 | usb_mouse_irq, mouse, endpoint->bInterval); |
| 195 | mouse->irq->transfer_dma = mouse->data_dma; |
| 196 | mouse->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; |
| 197 | |
Dmitry Torokhov | 5d6341c | 2007-04-04 10:40:57 +0200 | [diff] [blame] | 198 | error = input_register_device(mouse->dev); |
| 199 | if (error) |
| 200 | goto fail3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | |
| 202 | usb_set_intfdata(intf, mouse); |
| 203 | return 0; |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 204 | |
Dmitry Torokhov | 5d6341c | 2007-04-04 10:40:57 +0200 | [diff] [blame] | 205 | fail3: |
| 206 | usb_free_urb(mouse->irq); |
| 207 | fail2: |
Daniel Mack | 997ea58 | 2010-04-12 13:17:25 +0200 | [diff] [blame] | 208 | usb_free_coherent(dev, 8, mouse->data, mouse->data_dma); |
Dmitry Torokhov | 5d6341c | 2007-04-04 10:40:57 +0200 | [diff] [blame] | 209 | fail1: |
| 210 | input_free_device(input_dev); |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 211 | kfree(mouse); |
Dmitry Torokhov | 5d6341c | 2007-04-04 10:40:57 +0200 | [diff] [blame] | 212 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | static void usb_mouse_disconnect(struct usb_interface *intf) |
| 216 | { |
| 217 | struct usb_mouse *mouse = usb_get_intfdata (intf); |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 218 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | usb_set_intfdata(intf, NULL); |
| 220 | if (mouse) { |
| 221 | usb_kill_urb(mouse->irq); |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 222 | input_unregister_device(mouse->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | usb_free_urb(mouse->irq); |
Daniel Mack | 997ea58 | 2010-04-12 13:17:25 +0200 | [diff] [blame] | 224 | usb_free_coherent(interface_to_usbdev(intf), 8, mouse->data, mouse->data_dma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | kfree(mouse); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | static struct usb_device_id usb_mouse_id_table [] = { |
Michael Opdenacker | 4ef2e23 | 2007-02-21 22:51:25 +0100 | [diff] [blame] | 230 | { USB_INTERFACE_INFO(USB_INTERFACE_CLASS_HID, USB_INTERFACE_SUBCLASS_BOOT, |
| 231 | USB_INTERFACE_PROTOCOL_MOUSE) }, |
Reiner Herrmann | 14acdcd | 2006-09-06 02:37:21 +0200 | [diff] [blame] | 232 | { } /* Terminating entry */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | }; |
| 234 | |
| 235 | MODULE_DEVICE_TABLE (usb, usb_mouse_id_table); |
| 236 | |
| 237 | static struct usb_driver usb_mouse_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | .name = "usbmouse", |
| 239 | .probe = usb_mouse_probe, |
| 240 | .disconnect = usb_mouse_disconnect, |
| 241 | .id_table = usb_mouse_id_table, |
| 242 | }; |
| 243 | |
| 244 | static int __init usb_mouse_init(void) |
| 245 | { |
| 246 | int retval = usb_register(&usb_mouse_driver); |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 247 | if (retval == 0) |
Greg Kroah-Hartman | ddbe324 | 2008-10-12 00:14:23 +0200 | [diff] [blame] | 248 | printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":" |
| 249 | DRIVER_DESC "\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | return retval; |
| 251 | } |
| 252 | |
| 253 | static void __exit usb_mouse_exit(void) |
| 254 | { |
| 255 | usb_deregister(&usb_mouse_driver); |
| 256 | } |
| 257 | |
| 258 | module_init(usb_mouse_init); |
| 259 | module_exit(usb_mouse_exit); |