blob: da7b0bf51aff80d8b6eda7334ddfd3627c0401ff [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/******************************************************************************
2 * touchkitusb.c -- Driver for eGalax TouchKit USB Touchscreens
3 *
Daniel Ritz5d320292005-11-27 22:23:38 +01004 * Copyright (C) 2004-2005 by Daniel Ritz <daniel.ritz@gmx.ch>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Copyright (C) by Todd E. Johnson (mtouchusb.c)
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of the
10 * License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 *
21 * Based upon mtouchusb.c
22 *
23 *****************************************************************************/
24
25//#define DEBUG
26
27#include <linux/config.h>
28#include <linux/kernel.h>
29#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/module.h>
31#include <linux/init.h>
David Brownellae0dadc2006-06-13 10:04:34 -070032#include <linux/usb/input.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34#define TOUCHKIT_MIN_XC 0x0
35#define TOUCHKIT_MAX_XC 0x07ff
36#define TOUCHKIT_XC_FUZZ 0x0
37#define TOUCHKIT_XC_FLAT 0x0
38#define TOUCHKIT_MIN_YC 0x0
39#define TOUCHKIT_MAX_YC 0x07ff
40#define TOUCHKIT_YC_FUZZ 0x0
41#define TOUCHKIT_YC_FLAT 0x0
Daniel Ritz5d320292005-11-27 22:23:38 +010042#define TOUCHKIT_REPORT_DATA_SIZE 16
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44#define TOUCHKIT_DOWN 0x01
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Daniel Ritz5d320292005-11-27 22:23:38 +010046#define TOUCHKIT_PKT_TYPE_MASK 0xFE
47#define TOUCHKIT_PKT_TYPE_REPT 0x80
48#define TOUCHKIT_PKT_TYPE_DIAG 0x0A
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50#define DRIVER_VERSION "v0.1"
51#define DRIVER_AUTHOR "Daniel Ritz <daniel.ritz@gmx.ch>"
52#define DRIVER_DESC "eGalax TouchKit USB HID Touchscreen Driver"
53
54static int swap_xy;
55module_param(swap_xy, bool, 0644);
56MODULE_PARM_DESC(swap_xy, "If set X and Y axes are swapped.");
57
58struct touchkit_usb {
59 unsigned char *data;
60 dma_addr_t data_dma;
Daniel Ritz5d320292005-11-27 22:23:38 +010061 char buffer[TOUCHKIT_REPORT_DATA_SIZE];
62 int buf_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 struct urb *irq;
64 struct usb_device *udev;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -050065 struct input_dev *input;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 char name[128];
67 char phys[64];
68};
69
70static struct usb_device_id touchkit_devices[] = {
71 {USB_DEVICE(0x3823, 0x0001)},
Daniel Ritz3f8c03e2005-09-28 21:30:12 +020072 {USB_DEVICE(0x0123, 0x0001)},
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 {USB_DEVICE(0x0eef, 0x0001)},
Daniel Ritz3f8c03e2005-09-28 21:30:12 +020074 {USB_DEVICE(0x0eef, 0x0002)},
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 {}
76};
77
Daniel Ritz5d320292005-11-27 22:23:38 +010078/* helpers to read the data */
79static inline int touchkit_get_touched(char *data)
80{
81 return (data[0] & TOUCHKIT_DOWN) ? 1 : 0;
82}
83
84static inline int touchkit_get_x(char *data)
85{
86 return ((data[3] & 0x0F) << 7) | (data[4] & 0x7F);
87}
88
89static inline int touchkit_get_y(char *data)
90{
91 return ((data[1] & 0x0F) << 7) | (data[2] & 0x7F);
92}
93
94
95/* processes one input packet. */
96static void touchkit_process_pkt(struct touchkit_usb *touchkit,
97 struct pt_regs *regs, char *pkt)
98{
99 int x, y;
100
101 /* only process report packets */
102 if ((pkt[0] & TOUCHKIT_PKT_TYPE_MASK) != TOUCHKIT_PKT_TYPE_REPT)
103 return;
104
105 if (swap_xy) {
106 y = touchkit_get_x(pkt);
107 x = touchkit_get_y(pkt);
108 } else {
109 x = touchkit_get_x(pkt);
110 y = touchkit_get_y(pkt);
111 }
112
113 input_regs(touchkit->input, regs);
114 input_report_key(touchkit->input, BTN_TOUCH, touchkit_get_touched(pkt));
115 input_report_abs(touchkit->input, ABS_X, x);
116 input_report_abs(touchkit->input, ABS_Y, y);
117 input_sync(touchkit->input);
118}
119
120
121static int touchkit_get_pkt_len(char *buf)
122{
123 switch (buf[0] & TOUCHKIT_PKT_TYPE_MASK) {
124 case TOUCHKIT_PKT_TYPE_REPT:
125 return 5;
126
127 case TOUCHKIT_PKT_TYPE_DIAG:
128 return buf[1] + 2;
129 }
130
131 return 0;
132}
133
134static void touchkit_process(struct touchkit_usb *touchkit, int len,
135 struct pt_regs *regs)
136{
137 char *buffer;
138 int pkt_len, buf_len, pos;
139
140 /* if the buffer contains data, append */
141 if (unlikely(touchkit->buf_len)) {
142 int tmp;
143
144 /* if only 1 byte in buffer, add another one to get length */
145 if (touchkit->buf_len == 1)
146 touchkit->buffer[1] = touchkit->data[0];
147
148 pkt_len = touchkit_get_pkt_len(touchkit->buffer);
149
150 /* unknown packet: drop everything */
151 if (!pkt_len)
152 return;
153
154 /* append, process */
155 tmp = pkt_len - touchkit->buf_len;
156 memcpy(touchkit->buffer + touchkit->buf_len, touchkit->data, tmp);
157 touchkit_process_pkt(touchkit, regs, touchkit->buffer);
158
159 buffer = touchkit->data + tmp;
160 buf_len = len - tmp;
161 } else {
162 buffer = touchkit->data;
163 buf_len = len;
164 }
165
166 /* only one byte left in buffer */
167 if (unlikely(buf_len == 1)) {
168 touchkit->buffer[0] = buffer[0];
169 touchkit->buf_len = 1;
170 return;
171 }
172
173 /* loop over the buffer */
174 pos = 0;
175 while (pos < buf_len) {
176 /* get packet len */
177 pkt_len = touchkit_get_pkt_len(buffer + pos);
178
179 /* unknown packet: drop everything */
180 if (unlikely(!pkt_len))
181 return;
182
183 /* full packet: process */
184 if (likely(pkt_len <= buf_len)) {
185 touchkit_process_pkt(touchkit, regs, buffer + pos);
186 } else {
187 /* incomplete packet: save in buffer */
188 memcpy(touchkit->buffer, buffer + pos, buf_len - pos);
189 touchkit->buf_len = buf_len - pos;
190 }
191 pos += pkt_len;
192 }
193}
194
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196static void touchkit_irq(struct urb *urb, struct pt_regs *regs)
197{
198 struct touchkit_usb *touchkit = urb->context;
199 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 switch (urb->status) {
202 case 0:
203 /* success */
204 break;
205 case -ETIMEDOUT:
206 /* this urb is timing out */
207 dbg("%s - urb timed out - was the device unplugged?",
208 __FUNCTION__);
209 return;
210 case -ECONNRESET:
211 case -ENOENT:
212 case -ESHUTDOWN:
213 /* this urb is terminated, clean up */
214 dbg("%s - urb shutting down with status: %d",
215 __FUNCTION__, urb->status);
216 return;
217 default:
218 dbg("%s - nonzero urb status received: %d",
219 __FUNCTION__, urb->status);
220 goto exit;
221 }
222
Daniel Ritz5d320292005-11-27 22:23:38 +0100223 touchkit_process(touchkit, urb->actual_length, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225exit:
226 retval = usb_submit_urb(urb, GFP_ATOMIC);
227 if (retval)
228 err("%s - usb_submit_urb failed with result: %d",
229 __FUNCTION__, retval);
230}
231
232static int touchkit_open(struct input_dev *input)
233{
234 struct touchkit_usb *touchkit = input->private;
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 touchkit->irq->dev = touchkit->udev;
237
Dmitry Torokhov65cde542005-05-29 02:29:38 -0500238 if (usb_submit_urb(touchkit->irq, GFP_ATOMIC))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241 return 0;
242}
243
244static void touchkit_close(struct input_dev *input)
245{
246 struct touchkit_usb *touchkit = input->private;
247
Dmitry Torokhov65cde542005-05-29 02:29:38 -0500248 usb_kill_urb(touchkit->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
251static int touchkit_alloc_buffers(struct usb_device *udev,
252 struct touchkit_usb *touchkit)
253{
254 touchkit->data = usb_buffer_alloc(udev, TOUCHKIT_REPORT_DATA_SIZE,
255 SLAB_ATOMIC, &touchkit->data_dma);
256
257 if (!touchkit->data)
258 return -1;
259
260 return 0;
261}
262
263static void touchkit_free_buffers(struct usb_device *udev,
264 struct touchkit_usb *touchkit)
265{
266 if (touchkit->data)
267 usb_buffer_free(udev, TOUCHKIT_REPORT_DATA_SIZE,
268 touchkit->data, touchkit->data_dma);
269}
270
271static int touchkit_probe(struct usb_interface *intf,
272 const struct usb_device_id *id)
273{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 struct touchkit_usb *touchkit;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500275 struct input_dev *input_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 struct usb_host_interface *interface;
277 struct usb_endpoint_descriptor *endpoint;
278 struct usb_device *udev = interface_to_usbdev(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280 interface = intf->cur_altsetting;
281 endpoint = &interface->endpoint[0].desc;
282
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500283 touchkit = kzalloc(sizeof(struct touchkit_usb), GFP_KERNEL);
284 input_dev = input_allocate_device();
285 if (!touchkit || !input_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500288 if (touchkit_alloc_buffers(udev, touchkit))
289 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291 touchkit->irq = usb_alloc_urb(0, GFP_KERNEL);
292 if (!touchkit->irq) {
293 dbg("%s - usb_alloc_urb failed: touchkit->irq", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 goto out_free_buffers;
295 }
296
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500297 touchkit->udev = udev;
298 touchkit->input = input_dev;
299
300 if (udev->manufacturer)
301 strlcpy(touchkit->name, udev->manufacturer, sizeof(touchkit->name));
302
303 if (udev->product) {
304 if (udev->manufacturer)
305 strlcat(touchkit->name, " ", sizeof(touchkit->name));
306 strlcat(touchkit->name, udev->product, sizeof(touchkit->name));
307 }
308
309 if (!strlen(touchkit->name))
310 snprintf(touchkit->name, sizeof(touchkit->name),
311 "USB Touchscreen %04x:%04x",
312 le16_to_cpu(udev->descriptor.idVendor),
313 le16_to_cpu(udev->descriptor.idProduct));
314
315 usb_make_path(udev, touchkit->phys, sizeof(touchkit->phys));
316 strlcpy(touchkit->phys, "/input0", sizeof(touchkit->phys));
317
318 input_dev->name = touchkit->name;
319 input_dev->phys = touchkit->phys;
320 usb_to_input_id(udev, &input_dev->id);
321 input_dev->cdev.dev = &intf->dev;
322 input_dev->private = touchkit;
323 input_dev->open = touchkit_open;
324 input_dev->close = touchkit_close;
325
326 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
327 input_dev->keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH);
328 input_set_abs_params(input_dev, ABS_X, TOUCHKIT_MIN_XC, TOUCHKIT_MAX_XC,
329 TOUCHKIT_XC_FUZZ, TOUCHKIT_XC_FLAT);
330 input_set_abs_params(input_dev, ABS_Y, TOUCHKIT_MIN_YC, TOUCHKIT_MAX_YC,
331 TOUCHKIT_YC_FUZZ, TOUCHKIT_YC_FLAT);
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 usb_fill_int_urb(touchkit->irq, touchkit->udev,
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500334 usb_rcvintpipe(touchkit->udev, 0x81),
335 touchkit->data, TOUCHKIT_REPORT_DATA_SIZE,
336 touchkit_irq, touchkit, endpoint->bInterval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Juergen Schindelea0011002006-01-09 08:51:48 +0100338 touchkit->irq->transfer_dma = touchkit->data_dma;
339 touchkit->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
340
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500341 input_register_device(touchkit->input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 usb_set_intfdata(intf, touchkit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 return 0;
345
346out_free_buffers:
347 touchkit_free_buffers(udev, touchkit);
348out_free:
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500349 input_free_device(input_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 kfree(touchkit);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500351 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352}
353
354static void touchkit_disconnect(struct usb_interface *intf)
355{
356 struct touchkit_usb *touchkit = usb_get_intfdata(intf);
357
358 dbg("%s - called", __FUNCTION__);
359
360 if (!touchkit)
361 return;
362
363 dbg("%s - touchkit is initialized, cleaning up", __FUNCTION__);
364 usb_set_intfdata(intf, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 usb_kill_urb(touchkit->irq);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500366 input_unregister_device(touchkit->input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 usb_free_urb(touchkit->irq);
368 touchkit_free_buffers(interface_to_usbdev(intf), touchkit);
369 kfree(touchkit);
370}
371
372MODULE_DEVICE_TABLE(usb, touchkit_devices);
373
374static struct usb_driver touchkit_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 .name = "touchkitusb",
376 .probe = touchkit_probe,
377 .disconnect = touchkit_disconnect,
378 .id_table = touchkit_devices,
379};
380
381static int __init touchkit_init(void)
382{
383 return usb_register(&touchkit_driver);
384}
385
386static void __exit touchkit_cleanup(void)
387{
388 usb_deregister(&touchkit_driver);
389}
390
391module_init(touchkit_init);
392module_exit(touchkit_cleanup);
393
394MODULE_AUTHOR(DRIVER_AUTHOR);
395MODULE_DESCRIPTION(DRIVER_DESC);
396MODULE_LICENSE("GPL");