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