blob: 3fba74b9b602be947e03a00aad198fd7ca658bf3 [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;
Greg Kroah-Hartmaned2b2f22012-05-04 15:33:11 -070036 struct usb_interface *intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 struct urb *irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 char phys[32];
39};
40
David Howells7d12e782006-10-05 14:55:46 +010041static void kbtab_irq(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{
43 struct kbtab *kbtab = urb->context;
44 unsigned char *data = kbtab->data;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -050045 struct input_dev *dev = kbtab->dev;
Dmitry Torokhovf3192092010-04-20 22:15:44 -070046 int pressure;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 int retval;
48
49 switch (urb->status) {
50 case 0:
51 /* success */
52 break;
53 case -ECONNRESET:
54 case -ENOENT:
55 case -ESHUTDOWN:
56 /* this urb is terminated, clean up */
Greg Kroah-Hartmaned2b2f22012-05-04 15:33:11 -070057 dev_dbg(&kbtab->intf->dev,
58 "%s - urb shutting down with status: %d\n",
Greg Kroah-Hartman6d0f7dc2012-05-01 21:33:20 -070059 __func__, urb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 return;
61 default:
Greg Kroah-Hartmaned2b2f22012-05-04 15:33:11 -070062 dev_dbg(&kbtab->intf->dev,
63 "%s - nonzero urb status received: %d\n",
Greg Kroah-Hartman6d0f7dc2012-05-01 21:33:20 -070064 __func__, urb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 goto exit;
66 }
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69 input_report_key(dev, BTN_TOOL_PEN, 1);
70
Dmitry Torokhovf3192092010-04-20 22:15:44 -070071 input_report_abs(dev, ABS_X, get_unaligned_le16(&data[1]));
72 input_report_abs(dev, ABS_Y, get_unaligned_le16(&data[3]));
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74 /*input_report_key(dev, BTN_TOUCH , data[0] & 0x01);*/
75 input_report_key(dev, BTN_RIGHT, data[0] & 0x02);
76
Dmitry Torokhovf3192092010-04-20 22:15:44 -070077 pressure = data[5];
78 if (kb_pressure_click == -1)
79 input_report_abs(dev, ABS_PRESSURE, pressure);
80 else
81 input_report_key(dev, BTN_LEFT, pressure > kb_pressure_click ? 1 : 0);
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -050082
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 input_sync(dev);
84
85 exit:
Dmitry Torokhovf3192092010-04-20 22:15:44 -070086 retval = usb_submit_urb(urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 if (retval)
Greg Kroah-Hartmaned2b2f22012-05-04 15:33:11 -070088 dev_err(&kbtab->intf->dev,
Greg Kroah-Hartman202712c2012-04-25 14:48:41 -070089 "%s - usb_submit_urb failed with result %d\n",
90 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
92
93static struct usb_device_id kbtab_ids[] = {
94 { USB_DEVICE(USB_VENDOR_ID_KBGEAR, 0x1001), .driver_info = 0 },
95 { }
96};
97
98MODULE_DEVICE_TABLE(usb, kbtab_ids);
99
100static int kbtab_open(struct input_dev *dev)
101{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400102 struct kbtab *kbtab = input_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 kbtab->irq->dev = kbtab->usbdev;
Dmitry Torokhov65cde542005-05-29 02:29:38 -0500105 if (usb_submit_urb(kbtab->irq, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108 return 0;
109}
110
111static void kbtab_close(struct input_dev *dev)
112{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400113 struct kbtab *kbtab = input_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Dmitry Torokhov65cde542005-05-29 02:29:38 -0500115 usb_kill_urb(kbtab->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
118static int kbtab_probe(struct usb_interface *intf, const struct usb_device_id *id)
119{
120 struct usb_device *dev = interface_to_usbdev(intf);
121 struct usb_endpoint_descriptor *endpoint;
122 struct kbtab *kbtab;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500123 struct input_dev *input_dev;
Dmitry Torokhov50141862007-04-12 01:33:39 -0400124 int error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500126 kbtab = kzalloc(sizeof(struct kbtab), GFP_KERNEL);
127 input_dev = input_allocate_device();
128 if (!kbtab || !input_dev)
129 goto fail1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Daniel Mack997ea582010-04-12 13:17:25 +0200131 kbtab->data = usb_alloc_coherent(dev, 8, GFP_KERNEL, &kbtab->data_dma);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500132 if (!kbtab->data)
133 goto fail1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135 kbtab->irq = usb_alloc_urb(0, GFP_KERNEL);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500136 if (!kbtab->irq)
137 goto fail2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 kbtab->usbdev = dev;
Greg Kroah-Hartmaned2b2f22012-05-04 15:33:11 -0700140 kbtab->intf = intf;
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
Dmitry Torokhovf3192092010-04-20 22:15:44 -0700156 input_dev->evbit[0] |= BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
157 input_dev->keybit[BIT_WORD(BTN_LEFT)] |=
158 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
159 input_dev->keybit[BIT_WORD(BTN_DIGI)] |=
160 BIT_MASK(BTN_TOOL_PEN) | BIT_MASK(BTN_TOUCH);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500161 input_set_abs_params(input_dev, ABS_X, 0, 0x2000, 4, 0);
Dmitry Torokhov19947542005-12-29 22:19:07 -0500162 input_set_abs_params(input_dev, ABS_Y, 0, 0x1750, 4, 0);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500163 input_set_abs_params(input_dev, ABS_PRESSURE, 0, 0xff, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165 endpoint = &intf->cur_altsetting->endpoint[0].desc;
166
167 usb_fill_int_urb(kbtab->irq, dev,
168 usb_rcvintpipe(dev, endpoint->bEndpointAddress),
169 kbtab->data, 8,
170 kbtab_irq, kbtab, endpoint->bInterval);
171 kbtab->irq->transfer_dma = kbtab->data_dma;
172 kbtab->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
173
Dmitry Torokhov50141862007-04-12 01:33:39 -0400174 error = input_register_device(kbtab->dev);
175 if (error)
176 goto fail3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178 usb_set_intfdata(intf, kbtab);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 return 0;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500181
Dmitry Torokhov50141862007-04-12 01:33:39 -0400182 fail3: usb_free_urb(kbtab->irq);
Linus Torvalds7a9b1492010-05-20 21:26:12 -0700183 fail2: usb_free_coherent(dev, 8, kbtab->data, kbtab->data_dma);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400184 fail1: input_free_device(input_dev);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500185 kfree(kbtab);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400186 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187}
188
189static void kbtab_disconnect(struct usb_interface *intf)
190{
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500191 struct kbtab *kbtab = usb_get_intfdata(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193 usb_set_intfdata(intf, NULL);
Dmitry Torokhov331cb022010-04-20 22:06:53 -0700194
195 input_unregister_device(kbtab->dev);
196 usb_free_urb(kbtab->irq);
Linus Torvalds7a9b1492010-05-20 21:26:12 -0700197 usb_free_coherent(kbtab->usbdev, 8, kbtab->data, kbtab->data_dma);
Dmitry Torokhov331cb022010-04-20 22:06:53 -0700198 kfree(kbtab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
200
201static struct usb_driver kbtab_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 .name = "kbtab",
203 .probe = kbtab_probe,
204 .disconnect = kbtab_disconnect,
205 .id_table = kbtab_ids,
206};
207
Greg Kroah-Hartman08642e72011-11-18 09:48:31 -0800208module_usb_driver(kbtab_driver);