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