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> |
David Brownell | ae0dadc | 2006-06-13 10:04:34 -0700 | [diff] [blame] | 4 | #include <linux/usb/input.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | #include <asm/unaligned.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | |
| 7 | /* |
| 8 | * Version Information |
| 9 | * v0.0.1 - Original, extremely basic version, 2.4.xx only |
| 10 | * v0.0.2 - Updated, works with 2.5.62 and 2.4.20; |
| 11 | * - added pressure-threshold modules param code from |
| 12 | * Alex Perry <alex.perry@ieee.org> |
| 13 | */ |
| 14 | |
| 15 | #define DRIVER_VERSION "v0.0.2" |
| 16 | #define DRIVER_AUTHOR "Josh Myer <josh@joshisanerd.com>" |
| 17 | #define DRIVER_DESC "USB KB Gear JamStudio Tablet driver" |
| 18 | #define DRIVER_LICENSE "GPL" |
| 19 | |
| 20 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 21 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 22 | MODULE_LICENSE(DRIVER_LICENSE); |
| 23 | |
| 24 | #define USB_VENDOR_ID_KBGEAR 0x084e |
| 25 | |
| 26 | static int kb_pressure_click = 0x10; |
| 27 | module_param(kb_pressure_click, int, 0); |
| 28 | MODULE_PARM_DESC(kb_pressure_click, "pressure threshold for clicks"); |
| 29 | |
| 30 | struct kbtab { |
Dmitry Torokhov | 4ee1fc8 | 2007-05-03 00:54:54 -0400 | [diff] [blame] | 31 | unsigned char *data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | dma_addr_t data_dma; |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 33 | struct input_dev *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | struct usb_device *usbdev; |
Greg Kroah-Hartman | ed2b2f2 | 2012-05-04 15:33:11 -0700 | [diff] [blame] | 35 | struct usb_interface *intf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | struct urb *irq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | char phys[32]; |
| 38 | }; |
| 39 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 40 | static void kbtab_irq(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | { |
| 42 | struct kbtab *kbtab = urb->context; |
| 43 | unsigned char *data = kbtab->data; |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 44 | struct input_dev *dev = kbtab->dev; |
Dmitry Torokhov | f319209 | 2010-04-20 22:15:44 -0700 | [diff] [blame] | 45 | int pressure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | int retval; |
| 47 | |
| 48 | switch (urb->status) { |
| 49 | case 0: |
| 50 | /* success */ |
| 51 | break; |
| 52 | case -ECONNRESET: |
| 53 | case -ENOENT: |
| 54 | case -ESHUTDOWN: |
| 55 | /* this urb is terminated, clean up */ |
Greg Kroah-Hartman | ed2b2f2 | 2012-05-04 15:33:11 -0700 | [diff] [blame] | 56 | dev_dbg(&kbtab->intf->dev, |
| 57 | "%s - urb shutting down with status: %d\n", |
Greg Kroah-Hartman | 6d0f7dc | 2012-05-01 21:33:20 -0700 | [diff] [blame] | 58 | __func__, urb->status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | return; |
| 60 | default: |
Greg Kroah-Hartman | ed2b2f2 | 2012-05-04 15:33:11 -0700 | [diff] [blame] | 61 | dev_dbg(&kbtab->intf->dev, |
| 62 | "%s - nonzero urb status received: %d\n", |
Greg Kroah-Hartman | 6d0f7dc | 2012-05-01 21:33:20 -0700 | [diff] [blame] | 63 | __func__, urb->status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | goto exit; |
| 65 | } |
| 66 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
| 68 | input_report_key(dev, BTN_TOOL_PEN, 1); |
| 69 | |
Dmitry Torokhov | f319209 | 2010-04-20 22:15:44 -0700 | [diff] [blame] | 70 | input_report_abs(dev, ABS_X, get_unaligned_le16(&data[1])); |
| 71 | input_report_abs(dev, ABS_Y, get_unaligned_le16(&data[3])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | |
| 73 | /*input_report_key(dev, BTN_TOUCH , data[0] & 0x01);*/ |
| 74 | input_report_key(dev, BTN_RIGHT, data[0] & 0x02); |
| 75 | |
Dmitry Torokhov | f319209 | 2010-04-20 22:15:44 -0700 | [diff] [blame] | 76 | pressure = data[5]; |
| 77 | if (kb_pressure_click == -1) |
| 78 | input_report_abs(dev, ABS_PRESSURE, pressure); |
| 79 | else |
| 80 | input_report_key(dev, BTN_LEFT, pressure > kb_pressure_click ? 1 : 0); |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 81 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | input_sync(dev); |
| 83 | |
| 84 | exit: |
Dmitry Torokhov | f319209 | 2010-04-20 22:15:44 -0700 | [diff] [blame] | 85 | retval = usb_submit_urb(urb, GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | if (retval) |
Greg Kroah-Hartman | ed2b2f2 | 2012-05-04 15:33:11 -0700 | [diff] [blame] | 87 | dev_err(&kbtab->intf->dev, |
Greg Kroah-Hartman | 202712c | 2012-04-25 14:48:41 -0700 | [diff] [blame] | 88 | "%s - usb_submit_urb failed with result %d\n", |
| 89 | __func__, retval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | static struct usb_device_id kbtab_ids[] = { |
| 93 | { USB_DEVICE(USB_VENDOR_ID_KBGEAR, 0x1001), .driver_info = 0 }, |
| 94 | { } |
| 95 | }; |
| 96 | |
| 97 | MODULE_DEVICE_TABLE(usb, kbtab_ids); |
| 98 | |
| 99 | static int kbtab_open(struct input_dev *dev) |
| 100 | { |
Dmitry Torokhov | 7791bda | 2007-04-12 01:34:39 -0400 | [diff] [blame] | 101 | struct kbtab *kbtab = input_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | kbtab->irq->dev = kbtab->usbdev; |
Dmitry Torokhov | 65cde54 | 2005-05-29 02:29:38 -0500 | [diff] [blame] | 104 | if (usb_submit_urb(kbtab->irq, GFP_KERNEL)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | static void kbtab_close(struct input_dev *dev) |
| 111 | { |
Dmitry Torokhov | 7791bda | 2007-04-12 01:34:39 -0400 | [diff] [blame] | 112 | struct kbtab *kbtab = input_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | |
Dmitry Torokhov | 65cde54 | 2005-05-29 02:29:38 -0500 | [diff] [blame] | 114 | usb_kill_urb(kbtab->irq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | static int kbtab_probe(struct usb_interface *intf, const struct usb_device_id *id) |
| 118 | { |
| 119 | struct usb_device *dev = interface_to_usbdev(intf); |
| 120 | struct usb_endpoint_descriptor *endpoint; |
| 121 | struct kbtab *kbtab; |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 122 | struct input_dev *input_dev; |
Dmitry Torokhov | 5014186 | 2007-04-12 01:33:39 -0400 | [diff] [blame] | 123 | int error = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 125 | kbtab = kzalloc(sizeof(struct kbtab), GFP_KERNEL); |
| 126 | input_dev = input_allocate_device(); |
| 127 | if (!kbtab || !input_dev) |
| 128 | goto fail1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | |
Daniel Mack | 997ea58 | 2010-04-12 13:17:25 +0200 | [diff] [blame] | 130 | kbtab->data = usb_alloc_coherent(dev, 8, GFP_KERNEL, &kbtab->data_dma); |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 131 | if (!kbtab->data) |
| 132 | goto fail1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | |
| 134 | kbtab->irq = usb_alloc_urb(0, GFP_KERNEL); |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 135 | if (!kbtab->irq) |
| 136 | goto fail2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | kbtab->usbdev = dev; |
Greg Kroah-Hartman | ed2b2f2 | 2012-05-04 15:33:11 -0700 | [diff] [blame] | 139 | kbtab->intf = intf; |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 140 | kbtab->dev = input_dev; |
| 141 | |
| 142 | usb_make_path(dev, kbtab->phys, sizeof(kbtab->phys)); |
| 143 | strlcat(kbtab->phys, "/input0", sizeof(kbtab->phys)); |
| 144 | |
| 145 | input_dev->name = "KB Gear Tablet"; |
| 146 | input_dev->phys = kbtab->phys; |
| 147 | usb_to_input_id(dev, &input_dev->id); |
Dmitry Torokhov | c0f82d5 | 2007-04-12 01:35:03 -0400 | [diff] [blame] | 148 | input_dev->dev.parent = &intf->dev; |
Dmitry Torokhov | 7791bda | 2007-04-12 01:34:39 -0400 | [diff] [blame] | 149 | |
| 150 | input_set_drvdata(input_dev, kbtab); |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 151 | |
| 152 | input_dev->open = kbtab_open; |
| 153 | input_dev->close = kbtab_close; |
| 154 | |
Dmitry Torokhov | f319209 | 2010-04-20 22:15:44 -0700 | [diff] [blame] | 155 | input_dev->evbit[0] |= BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS); |
| 156 | input_dev->keybit[BIT_WORD(BTN_LEFT)] |= |
| 157 | BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT); |
| 158 | input_dev->keybit[BIT_WORD(BTN_DIGI)] |= |
| 159 | BIT_MASK(BTN_TOOL_PEN) | BIT_MASK(BTN_TOUCH); |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 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); |
Linus Torvalds | 7a9b149 | 2010-05-20 21:26:12 -0700 | [diff] [blame] | 182 | fail2: usb_free_coherent(dev, 8, kbtab->data, kbtab->data_dma); |
Dmitry Torokhov | 5014186 | 2007-04-12 01:33:39 -0400 | [diff] [blame] | 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); |
Dmitry Torokhov | 331cb02 | 2010-04-20 22:06:53 -0700 | [diff] [blame] | 193 | |
| 194 | input_unregister_device(kbtab->dev); |
| 195 | usb_free_urb(kbtab->irq); |
Linus Torvalds | 7a9b149 | 2010-05-20 21:26:12 -0700 | [diff] [blame] | 196 | usb_free_coherent(kbtab->usbdev, 8, kbtab->data, kbtab->data_dma); |
Dmitry Torokhov | 331cb02 | 2010-04-20 22:06:53 -0700 | [diff] [blame] | 197 | kfree(kbtab); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | static struct usb_driver kbtab_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | .name = "kbtab", |
| 202 | .probe = kbtab_probe, |
| 203 | .disconnect = kbtab_disconnect, |
| 204 | .id_table = kbtab_ids, |
| 205 | }; |
| 206 | |
Greg Kroah-Hartman | 08642e7 | 2011-11-18 09:48:31 -0800 | [diff] [blame] | 207 | module_usb_driver(kbtab_driver); |