blob: e850d7e8afbc4d22afb1bc73b2e99fc3f1cf5e8f [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>
David Brownellae0dadc2006-06-13 10:04:34 -07004#include <linux/usb/input.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006
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
20MODULE_AUTHOR(DRIVER_AUTHOR);
21MODULE_DESCRIPTION(DRIVER_DESC);
22MODULE_LICENSE(DRIVER_LICENSE);
23
24#define USB_VENDOR_ID_KBGEAR 0x084e
25
26static int kb_pressure_click = 0x10;
27module_param(kb_pressure_click, int, 0);
28MODULE_PARM_DESC(kb_pressure_click, "pressure threshold for clicks");
29
30struct kbtab {
Dmitry Torokhov4ee1fc82007-05-03 00:54:54 -040031 unsigned char *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 dma_addr_t data_dma;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -050033 struct input_dev *dev;
Greg Kroah-Hartmaned2b2f22012-05-04 15:33:11 -070034 struct usb_interface *intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 struct urb *irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 char phys[32];
37};
38
David Howells7d12e782006-10-05 14:55:46 +010039static void kbtab_irq(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040{
41 struct kbtab *kbtab = urb->context;
42 unsigned char *data = kbtab->data;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -050043 struct input_dev *dev = kbtab->dev;
Dmitry Torokhovf3192092010-04-20 22:15:44 -070044 int pressure;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 int retval;
46
47 switch (urb->status) {
48 case 0:
49 /* success */
50 break;
51 case -ECONNRESET:
52 case -ENOENT:
53 case -ESHUTDOWN:
54 /* this urb is terminated, clean up */
Greg Kroah-Hartmaned2b2f22012-05-04 15:33:11 -070055 dev_dbg(&kbtab->intf->dev,
56 "%s - urb shutting down with status: %d\n",
Greg Kroah-Hartman6d0f7dc2012-05-01 21:33:20 -070057 __func__, urb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 return;
59 default:
Greg Kroah-Hartmaned2b2f22012-05-04 15:33:11 -070060 dev_dbg(&kbtab->intf->dev,
61 "%s - nonzero urb status received: %d\n",
Greg Kroah-Hartman6d0f7dc2012-05-01 21:33:20 -070062 __func__, urb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 goto exit;
64 }
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67 input_report_key(dev, BTN_TOOL_PEN, 1);
68
Dmitry Torokhovf3192092010-04-20 22:15:44 -070069 input_report_abs(dev, ABS_X, get_unaligned_le16(&data[1]));
70 input_report_abs(dev, ABS_Y, get_unaligned_le16(&data[3]));
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72 /*input_report_key(dev, BTN_TOUCH , data[0] & 0x01);*/
73 input_report_key(dev, BTN_RIGHT, data[0] & 0x02);
74
Dmitry Torokhovf3192092010-04-20 22:15:44 -070075 pressure = data[5];
76 if (kb_pressure_click == -1)
77 input_report_abs(dev, ABS_PRESSURE, pressure);
78 else
79 input_report_key(dev, BTN_LEFT, pressure > kb_pressure_click ? 1 : 0);
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -050080
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 input_sync(dev);
82
83 exit:
Dmitry Torokhovf3192092010-04-20 22:15:44 -070084 retval = usb_submit_urb(urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 if (retval)
Greg Kroah-Hartmaned2b2f22012-05-04 15:33:11 -070086 dev_err(&kbtab->intf->dev,
Greg Kroah-Hartman202712c2012-04-25 14:48:41 -070087 "%s - usb_submit_urb failed with result %d\n",
88 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089}
90
91static struct usb_device_id kbtab_ids[] = {
92 { USB_DEVICE(USB_VENDOR_ID_KBGEAR, 0x1001), .driver_info = 0 },
93 { }
94};
95
96MODULE_DEVICE_TABLE(usb, kbtab_ids);
97
98static int kbtab_open(struct input_dev *dev)
99{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400100 struct kbtab *kbtab = input_get_drvdata(dev);
Oliver Neukum8f7292e2016-03-31 11:01:48 -0700101 struct usb_device *udev = interface_to_usbdev(kbtab->intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Oliver Neukum8f7292e2016-03-31 11:01:48 -0700103 kbtab->irq->dev = udev;
Dmitry Torokhov65cde542005-05-29 02:29:38 -0500104 if (usb_submit_urb(kbtab->irq, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107 return 0;
108}
109
110static void kbtab_close(struct input_dev *dev)
111{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400112 struct kbtab *kbtab = input_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Dmitry Torokhov65cde542005-05-29 02:29:38 -0500114 usb_kill_urb(kbtab->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
117static 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 Torokhovc5b7c7c2005-09-15 02:01:47 -0500122 struct input_dev *input_dev;
Dmitry Torokhov50141862007-04-12 01:33:39 -0400123 int error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500125 kbtab = kzalloc(sizeof(struct kbtab), GFP_KERNEL);
126 input_dev = input_allocate_device();
127 if (!kbtab || !input_dev)
128 goto fail1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Daniel Mack997ea582010-04-12 13:17:25 +0200130 kbtab->data = usb_alloc_coherent(dev, 8, GFP_KERNEL, &kbtab->data_dma);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500131 if (!kbtab->data)
132 goto fail1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134 kbtab->irq = usb_alloc_urb(0, GFP_KERNEL);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500135 if (!kbtab->irq)
136 goto fail2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Greg Kroah-Hartmaned2b2f22012-05-04 15:33:11 -0700138 kbtab->intf = intf;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500139 kbtab->dev = input_dev;
140
141 usb_make_path(dev, kbtab->phys, sizeof(kbtab->phys));
142 strlcat(kbtab->phys, "/input0", sizeof(kbtab->phys));
143
144 input_dev->name = "KB Gear Tablet";
145 input_dev->phys = kbtab->phys;
146 usb_to_input_id(dev, &input_dev->id);
Dmitry Torokhovc0f82d52007-04-12 01:35:03 -0400147 input_dev->dev.parent = &intf->dev;
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400148
149 input_set_drvdata(input_dev, kbtab);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500150
151 input_dev->open = kbtab_open;
152 input_dev->close = kbtab_close;
153
Dmitry Torokhovf3192092010-04-20 22:15:44 -0700154 input_dev->evbit[0] |= BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
155 input_dev->keybit[BIT_WORD(BTN_LEFT)] |=
156 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
157 input_dev->keybit[BIT_WORD(BTN_DIGI)] |=
158 BIT_MASK(BTN_TOOL_PEN) | BIT_MASK(BTN_TOUCH);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500159 input_set_abs_params(input_dev, ABS_X, 0, 0x2000, 4, 0);
Dmitry Torokhov19947542005-12-29 22:19:07 -0500160 input_set_abs_params(input_dev, ABS_Y, 0, 0x1750, 4, 0);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500161 input_set_abs_params(input_dev, ABS_PRESSURE, 0, 0xff, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163 endpoint = &intf->cur_altsetting->endpoint[0].desc;
164
165 usb_fill_int_urb(kbtab->irq, dev,
166 usb_rcvintpipe(dev, endpoint->bEndpointAddress),
167 kbtab->data, 8,
168 kbtab_irq, kbtab, endpoint->bInterval);
169 kbtab->irq->transfer_dma = kbtab->data_dma;
170 kbtab->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
171
Dmitry Torokhov50141862007-04-12 01:33:39 -0400172 error = input_register_device(kbtab->dev);
173 if (error)
174 goto fail3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176 usb_set_intfdata(intf, kbtab);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 return 0;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500179
Dmitry Torokhov50141862007-04-12 01:33:39 -0400180 fail3: usb_free_urb(kbtab->irq);
Linus Torvalds7a9b1492010-05-20 21:26:12 -0700181 fail2: usb_free_coherent(dev, 8, kbtab->data, kbtab->data_dma);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400182 fail1: input_free_device(input_dev);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500183 kfree(kbtab);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400184 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
186
187static void kbtab_disconnect(struct usb_interface *intf)
188{
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500189 struct kbtab *kbtab = usb_get_intfdata(intf);
Oliver Neukum8f7292e2016-03-31 11:01:48 -0700190 struct usb_device *udev = interface_to_usbdev(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 usb_set_intfdata(intf, NULL);
Dmitry Torokhov331cb022010-04-20 22:06:53 -0700193
194 input_unregister_device(kbtab->dev);
195 usb_free_urb(kbtab->irq);
Oliver Neukum8f7292e2016-03-31 11:01:48 -0700196 usb_free_coherent(udev, 8, kbtab->data, kbtab->data_dma);
Dmitry Torokhov331cb022010-04-20 22:06:53 -0700197 kfree(kbtab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198}
199
200static struct usb_driver kbtab_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 .name = "kbtab",
202 .probe = kbtab_probe,
203 .disconnect = kbtab_disconnect,
204 .id_table = kbtab_ids,
205};
206
Greg Kroah-Hartman08642e72011-11-18 09:48:31 -0800207module_usb_driver(kbtab_driver);