blob: 3f7d1254b8ed0d07e4cb6dac1dafd6bd7ebcac54 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#include <linux/kernel.h>
2#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07003#include <linux/module.h>
4#include <linux/init.h>
David Brownellae0dadc2006-06-13 10:04:34 -07005#include <linux/usb/input.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007
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
21MODULE_AUTHOR(DRIVER_AUTHOR);
22MODULE_DESCRIPTION(DRIVER_DESC);
23MODULE_LICENSE(DRIVER_LICENSE);
24
25#define USB_VENDOR_ID_KBGEAR 0x084e
26
27static int kb_pressure_click = 0x10;
28module_param(kb_pressure_click, int, 0);
29MODULE_PARM_DESC(kb_pressure_click, "pressure threshold for clicks");
30
31struct kbtab {
Dmitry Torokhov4ee1fc82007-05-03 00:54:54 -040032 unsigned char *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 dma_addr_t data_dma;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -050034 struct input_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 struct usb_device *usbdev;
36 struct urb *irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 int x, y;
38 int button;
39 int pressure;
40 __u32 serial[2];
41 char phys[32];
42};
43
David Howells7d12e782006-10-05 14:55:46 +010044static void kbtab_irq(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
46 struct kbtab *kbtab = urb->context;
47 unsigned char *data = kbtab->data;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -050048 struct input_dev *dev = kbtab->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 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 */
Harvey Harrisonea3e6c52008-05-05 11:36:18 -040059 dbg("%s - urb shutting down with status: %d", __func__, urb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 return;
61 default:
Harvey Harrisonea3e6c52008-05-05 11:36:18 -040062 dbg("%s - nonzero urb status received: %d", __func__, urb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 goto exit;
64 }
65
Harvey Harrison858ad082008-04-29 01:03:34 -070066 kbtab->x = get_unaligned_le16(&data[1]);
67 kbtab->y = get_unaligned_le16(&data[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
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 Torokhov05f091ab2005-05-29 02:29:01 -050079 if (-1 == kb_pressure_click) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 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 Torokhov05f091ab2005-05-29 02:29:01 -050084
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 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",
Harvey Harrisonea3e6c52008-05-05 11:36:18 -040091 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092}
93
94static struct usb_device_id kbtab_ids[] = {
95 { USB_DEVICE(USB_VENDOR_ID_KBGEAR, 0x1001), .driver_info = 0 },
96 { }
97};
98
99MODULE_DEVICE_TABLE(usb, kbtab_ids);
100
101static int kbtab_open(struct input_dev *dev)
102{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400103 struct kbtab *kbtab = input_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 kbtab->irq->dev = kbtab->usbdev;
Dmitry Torokhov65cde542005-05-29 02:29:38 -0500106 if (usb_submit_urb(kbtab->irq, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109 return 0;
110}
111
112static void kbtab_close(struct input_dev *dev)
113{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400114 struct kbtab *kbtab = input_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Dmitry Torokhov65cde542005-05-29 02:29:38 -0500116 usb_kill_urb(kbtab->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117}
118
119static 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 Torokhovc5b7c7c2005-09-15 02:01:47 -0500124 struct input_dev *input_dev;
Dmitry Torokhov50141862007-04-12 01:33:39 -0400125 int error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500127 kbtab = kzalloc(sizeof(struct kbtab), GFP_KERNEL);
128 input_dev = input_allocate_device();
129 if (!kbtab || !input_dev)
130 goto fail1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132 kbtab->data = usb_buffer_alloc(dev, 8, GFP_KERNEL, &kbtab->data_dma);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500133 if (!kbtab->data)
134 goto fail1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136 kbtab->irq = usb_alloc_urb(0, GFP_KERNEL);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500137 if (!kbtab->irq)
138 goto fail2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 kbtab->usbdev = dev;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500141 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 Torokhovc0f82d52007-04-12 01:35:03 -0400149 input_dev->dev.parent = &intf->dev;
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400150
151 input_set_drvdata(input_dev, kbtab);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500152
153 input_dev->open = kbtab_open;
154 input_dev->close = kbtab_close;
155
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700156 input_dev->evbit[0] |= BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS) |
157 BIT_MASK(EV_MSC);
158 input_dev->keybit[BIT_WORD(BTN_LEFT)] |= BIT_MASK(BTN_LEFT) |
159 BIT_MASK(BTN_RIGHT) | BIT_MASK(BTN_MIDDLE);
160 input_dev->keybit[BIT_WORD(BTN_DIGI)] |= BIT_MASK(BTN_TOOL_PEN) |
161 BIT_MASK(BTN_TOUCH);
162 input_dev->mscbit[0] |= BIT_MASK(MSC_SERIAL);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500163 input_set_abs_params(input_dev, ABS_X, 0, 0x2000, 4, 0);
Dmitry Torokhov19947542005-12-29 22:19:07 -0500164 input_set_abs_params(input_dev, ABS_Y, 0, 0x1750, 4, 0);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500165 input_set_abs_params(input_dev, ABS_PRESSURE, 0, 0xff, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 endpoint = &intf->cur_altsetting->endpoint[0].desc;
168
169 usb_fill_int_urb(kbtab->irq, dev,
170 usb_rcvintpipe(dev, endpoint->bEndpointAddress),
171 kbtab->data, 8,
172 kbtab_irq, kbtab, endpoint->bInterval);
173 kbtab->irq->transfer_dma = kbtab->data_dma;
174 kbtab->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
175
Dmitry Torokhov50141862007-04-12 01:33:39 -0400176 error = input_register_device(kbtab->dev);
177 if (error)
178 goto fail3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 usb_set_intfdata(intf, kbtab);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 return 0;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500183
Dmitry Torokhov50141862007-04-12 01:33:39 -0400184 fail3: usb_free_urb(kbtab->irq);
Axel Linee7aa6c2010-04-20 21:59:51 -0700185 fail2: usb_buffer_free(dev, 8, kbtab->data, kbtab->data_dma);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400186 fail1: input_free_device(input_dev);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500187 kfree(kbtab);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400188 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189}
190
191static void kbtab_disconnect(struct usb_interface *intf)
192{
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500193 struct kbtab *kbtab = usb_get_intfdata(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195 usb_set_intfdata(intf, NULL);
Dmitry Torokhov331cb022010-04-20 22:06:53 -0700196
197 input_unregister_device(kbtab->dev);
198 usb_free_urb(kbtab->irq);
199 usb_buffer_free(kbtab->usbdev, 8, kbtab->data, kbtab->data_dma);
200 kfree(kbtab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201}
202
203static struct usb_driver kbtab_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 .name = "kbtab",
205 .probe = kbtab_probe,
206 .disconnect = kbtab_disconnect,
207 .id_table = kbtab_ids,
208};
209
210static int __init kbtab_init(void)
211{
212 int retval;
213 retval = usb_register(&kbtab_driver);
214 if (retval)
215 goto out;
Greg Kroah-Hartman899ef6e2008-08-18 13:21:04 -0700216 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
217 DRIVER_DESC "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218out:
219 return retval;
220}
221
222static void __exit kbtab_exit(void)
223{
224 usb_deregister(&kbtab_driver);
225}
226
227module_init(kbtab_init);
228module_exit(kbtab_exit);