Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #include <linux/kernel.h> |
| 2 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | #include <linux/module.h> |
| 4 | #include <linux/init.h> |
David Brownell | ae0dadc | 2006-06-13 10:04:34 -0700 | [diff] [blame] | 5 | #include <linux/usb/input.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | #include <asm/unaligned.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | |
| 8 | /* |
| 9 | * Version Information |
| 10 | * v0.0.1 - Original, extremely basic version, 2.4.xx only |
| 11 | * v0.0.2 - Updated, works with 2.5.62 and 2.4.20; |
| 12 | * - added pressure-threshold modules param code from |
| 13 | * Alex Perry <alex.perry@ieee.org> |
| 14 | */ |
| 15 | |
| 16 | #define DRIVER_VERSION "v0.0.2" |
| 17 | #define DRIVER_AUTHOR "Josh Myer <josh@joshisanerd.com>" |
| 18 | #define DRIVER_DESC "USB KB Gear JamStudio Tablet driver" |
| 19 | #define DRIVER_LICENSE "GPL" |
| 20 | |
| 21 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 22 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 23 | MODULE_LICENSE(DRIVER_LICENSE); |
| 24 | |
| 25 | #define USB_VENDOR_ID_KBGEAR 0x084e |
| 26 | |
| 27 | static int kb_pressure_click = 0x10; |
| 28 | module_param(kb_pressure_click, int, 0); |
| 29 | MODULE_PARM_DESC(kb_pressure_click, "pressure threshold for clicks"); |
| 30 | |
| 31 | struct kbtab { |
| 32 | signed char *data; |
| 33 | dma_addr_t data_dma; |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 34 | struct input_dev *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | struct usb_device *usbdev; |
| 36 | struct urb *irq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | int x, y; |
| 38 | int button; |
| 39 | int pressure; |
| 40 | __u32 serial[2]; |
| 41 | char phys[32]; |
| 42 | }; |
| 43 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 44 | static void kbtab_irq(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | { |
| 46 | struct kbtab *kbtab = urb->context; |
| 47 | unsigned char *data = kbtab->data; |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 48 | struct input_dev *dev = kbtab->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | int retval; |
| 50 | |
| 51 | switch (urb->status) { |
| 52 | case 0: |
| 53 | /* success */ |
| 54 | break; |
| 55 | case -ECONNRESET: |
| 56 | case -ENOENT: |
| 57 | case -ESHUTDOWN: |
| 58 | /* this urb is terminated, clean up */ |
| 59 | dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status); |
| 60 | return; |
| 61 | default: |
| 62 | dbg("%s - nonzero urb status received: %d", __FUNCTION__, urb->status); |
| 63 | goto exit; |
| 64 | } |
| 65 | |
| 66 | kbtab->x = le16_to_cpu(get_unaligned((__le16 *) &data[1])); |
| 67 | kbtab->y = le16_to_cpu(get_unaligned((__le16 *) &data[3])); |
| 68 | |
| 69 | kbtab->pressure = (data[5]); |
| 70 | |
| 71 | input_report_key(dev, BTN_TOOL_PEN, 1); |
| 72 | |
| 73 | input_report_abs(dev, ABS_X, kbtab->x); |
| 74 | input_report_abs(dev, ABS_Y, kbtab->y); |
| 75 | |
| 76 | /*input_report_key(dev, BTN_TOUCH , data[0] & 0x01);*/ |
| 77 | input_report_key(dev, BTN_RIGHT, data[0] & 0x02); |
| 78 | |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 79 | if (-1 == kb_pressure_click) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | input_report_abs(dev, ABS_PRESSURE, kbtab->pressure); |
| 81 | } else { |
| 82 | input_report_key(dev, BTN_LEFT, (kbtab->pressure > kb_pressure_click) ? 1 : 0); |
| 83 | }; |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 84 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | input_sync(dev); |
| 86 | |
| 87 | exit: |
| 88 | retval = usb_submit_urb (urb, GFP_ATOMIC); |
| 89 | if (retval) |
| 90 | err ("%s - usb_submit_urb failed with result %d", |
| 91 | __FUNCTION__, retval); |
| 92 | } |
| 93 | |
| 94 | static struct usb_device_id kbtab_ids[] = { |
| 95 | { USB_DEVICE(USB_VENDOR_ID_KBGEAR, 0x1001), .driver_info = 0 }, |
| 96 | { } |
| 97 | }; |
| 98 | |
| 99 | MODULE_DEVICE_TABLE(usb, kbtab_ids); |
| 100 | |
| 101 | static int kbtab_open(struct input_dev *dev) |
| 102 | { |
Dmitry Torokhov | 7791bda | 2007-04-12 01:34:39 -0400 | [diff] [blame] | 103 | struct kbtab *kbtab = input_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | kbtab->irq->dev = kbtab->usbdev; |
Dmitry Torokhov | 65cde54 | 2005-05-29 02:29:38 -0500 | [diff] [blame] | 106 | if (usb_submit_urb(kbtab->irq, GFP_KERNEL)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | static void kbtab_close(struct input_dev *dev) |
| 113 | { |
Dmitry Torokhov | 7791bda | 2007-04-12 01:34:39 -0400 | [diff] [blame] | 114 | struct kbtab *kbtab = input_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
Dmitry Torokhov | 65cde54 | 2005-05-29 02:29:38 -0500 | [diff] [blame] | 116 | usb_kill_urb(kbtab->irq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | static int kbtab_probe(struct usb_interface *intf, const struct usb_device_id *id) |
| 120 | { |
| 121 | struct usb_device *dev = interface_to_usbdev(intf); |
| 122 | struct usb_endpoint_descriptor *endpoint; |
| 123 | struct kbtab *kbtab; |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 124 | struct input_dev *input_dev; |
Dmitry Torokhov | 5014186 | 2007-04-12 01:33:39 -0400 | [diff] [blame] | 125 | int error = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 127 | kbtab = kzalloc(sizeof(struct kbtab), GFP_KERNEL); |
| 128 | input_dev = input_allocate_device(); |
| 129 | if (!kbtab || !input_dev) |
| 130 | goto fail1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | |
| 132 | kbtab->data = usb_buffer_alloc(dev, 8, GFP_KERNEL, &kbtab->data_dma); |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 133 | if (!kbtab->data) |
| 134 | goto fail1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | |
| 136 | kbtab->irq = usb_alloc_urb(0, GFP_KERNEL); |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 137 | if (!kbtab->irq) |
| 138 | goto fail2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | kbtab->usbdev = dev; |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 141 | kbtab->dev = input_dev; |
| 142 | |
| 143 | usb_make_path(dev, kbtab->phys, sizeof(kbtab->phys)); |
| 144 | strlcat(kbtab->phys, "/input0", sizeof(kbtab->phys)); |
| 145 | |
| 146 | input_dev->name = "KB Gear Tablet"; |
| 147 | input_dev->phys = kbtab->phys; |
| 148 | usb_to_input_id(dev, &input_dev->id); |
Dmitry Torokhov | c0f82d5 | 2007-04-12 01:35:03 -0400 | [diff] [blame] | 149 | input_dev->dev.parent = &intf->dev; |
Dmitry Torokhov | 7791bda | 2007-04-12 01:34:39 -0400 | [diff] [blame] | 150 | |
| 151 | input_set_drvdata(input_dev, kbtab); |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 152 | |
| 153 | input_dev->open = kbtab_open; |
| 154 | input_dev->close = kbtab_close; |
| 155 | |
| 156 | input_dev->evbit[0] |= BIT(EV_KEY) | BIT(EV_ABS) | BIT(EV_MSC); |
| 157 | input_dev->keybit[LONG(BTN_LEFT)] |= BIT(BTN_LEFT) | BIT(BTN_RIGHT) | BIT(BTN_MIDDLE); |
| 158 | input_dev->keybit[LONG(BTN_DIGI)] |= BIT(BTN_TOOL_PEN) | BIT(BTN_TOUCH); |
| 159 | input_dev->mscbit[0] |= BIT(MSC_SERIAL); |
| 160 | input_set_abs_params(input_dev, ABS_X, 0, 0x2000, 4, 0); |
Dmitry Torokhov | 1994754 | 2005-12-29 22:19:07 -0500 | [diff] [blame] | 161 | input_set_abs_params(input_dev, ABS_Y, 0, 0x1750, 4, 0); |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 162 | input_set_abs_params(input_dev, ABS_PRESSURE, 0, 0xff, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | |
| 164 | endpoint = &intf->cur_altsetting->endpoint[0].desc; |
| 165 | |
| 166 | usb_fill_int_urb(kbtab->irq, dev, |
| 167 | usb_rcvintpipe(dev, endpoint->bEndpointAddress), |
| 168 | kbtab->data, 8, |
| 169 | kbtab_irq, kbtab, endpoint->bInterval); |
| 170 | kbtab->irq->transfer_dma = kbtab->data_dma; |
| 171 | kbtab->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; |
| 172 | |
Dmitry Torokhov | 5014186 | 2007-04-12 01:33:39 -0400 | [diff] [blame] | 173 | error = input_register_device(kbtab->dev); |
| 174 | if (error) |
| 175 | goto fail3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | |
| 177 | usb_set_intfdata(intf, kbtab); |
Dmitry Torokhov | 5014186 | 2007-04-12 01:33:39 -0400 | [diff] [blame] | 178 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | return 0; |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 180 | |
Dmitry Torokhov | 5014186 | 2007-04-12 01:33:39 -0400 | [diff] [blame] | 181 | fail3: usb_free_urb(kbtab->irq); |
| 182 | fail2: usb_buffer_free(dev, 10, kbtab->data, kbtab->data_dma); |
| 183 | fail1: input_free_device(input_dev); |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 184 | kfree(kbtab); |
Dmitry Torokhov | 5014186 | 2007-04-12 01:33:39 -0400 | [diff] [blame] | 185 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | static void kbtab_disconnect(struct usb_interface *intf) |
| 189 | { |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 190 | struct kbtab *kbtab = usb_get_intfdata(intf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | |
| 192 | usb_set_intfdata(intf, NULL); |
| 193 | if (kbtab) { |
| 194 | usb_kill_urb(kbtab->irq); |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 195 | input_unregister_device(kbtab->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | usb_free_urb(kbtab->irq); |
| 197 | usb_buffer_free(interface_to_usbdev(intf), 10, kbtab->data, kbtab->data_dma); |
| 198 | kfree(kbtab); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | static struct usb_driver kbtab_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | .name = "kbtab", |
| 204 | .probe = kbtab_probe, |
| 205 | .disconnect = kbtab_disconnect, |
| 206 | .id_table = kbtab_ids, |
| 207 | }; |
| 208 | |
| 209 | static int __init kbtab_init(void) |
| 210 | { |
| 211 | int retval; |
| 212 | retval = usb_register(&kbtab_driver); |
| 213 | if (retval) |
| 214 | goto out; |
| 215 | info(DRIVER_VERSION ":" DRIVER_DESC); |
| 216 | out: |
| 217 | return retval; |
| 218 | } |
| 219 | |
| 220 | static void __exit kbtab_exit(void) |
| 221 | { |
| 222 | usb_deregister(&kbtab_driver); |
| 223 | } |
| 224 | |
| 225 | module_init(kbtab_init); |
| 226 | module_exit(kbtab_exit); |