blob: 30b9f820e7a894e7b1db04ec347483b5177d3735 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/kernel.h>
28#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/module.h>
30#include <linux/init.h>
David Brownellae0dadc2006-06-13 10:04:34 -070031#include <linux/usb/input.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33#define TOUCHKIT_MIN_XC 0x0
34#define TOUCHKIT_MAX_XC 0x07ff
35#define TOUCHKIT_XC_FUZZ 0x0
36#define TOUCHKIT_XC_FLAT 0x0
37#define TOUCHKIT_MIN_YC 0x0
38#define TOUCHKIT_MAX_YC 0x07ff
39#define TOUCHKIT_YC_FUZZ 0x0
40#define TOUCHKIT_YC_FLAT 0x0
Daniel Ritz5d320292005-11-27 22:23:38 +010041#define TOUCHKIT_REPORT_DATA_SIZE 16
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#define TOUCHKIT_DOWN 0x01
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Daniel Ritz5d320292005-11-27 22:23:38 +010045#define TOUCHKIT_PKT_TYPE_MASK 0xFE
46#define TOUCHKIT_PKT_TYPE_REPT 0x80
47#define TOUCHKIT_PKT_TYPE_DIAG 0x0A
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49#define DRIVER_VERSION "v0.1"
50#define DRIVER_AUTHOR "Daniel Ritz <daniel.ritz@gmx.ch>"
51#define DRIVER_DESC "eGalax TouchKit USB HID Touchscreen Driver"
52
53static int swap_xy;
54module_param(swap_xy, bool, 0644);
55MODULE_PARM_DESC(swap_xy, "If set X and Y axes are swapped.");
56
57struct touchkit_usb {
58 unsigned char *data;
59 dma_addr_t data_dma;
Daniel Ritz5d320292005-11-27 22:23:38 +010060 char buffer[TOUCHKIT_REPORT_DATA_SIZE];
61 int buf_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 struct urb *irq;
63 struct usb_device *udev;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -050064 struct input_dev *input;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 char name[128];
66 char phys[64];
67};
68
69static struct usb_device_id touchkit_devices[] = {
70 {USB_DEVICE(0x3823, 0x0001)},
Daniel Ritz3f8c03e2005-09-28 21:30:12 +020071 {USB_DEVICE(0x0123, 0x0001)},
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 {USB_DEVICE(0x0eef, 0x0001)},
Daniel Ritz3f8c03e2005-09-28 21:30:12 +020073 {USB_DEVICE(0x0eef, 0x0002)},
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 {}
75};
76
Daniel Ritz5d320292005-11-27 22:23:38 +010077/* helpers to read the data */
78static inline int touchkit_get_touched(char *data)
79{
80 return (data[0] & TOUCHKIT_DOWN) ? 1 : 0;
81}
82
83static inline int touchkit_get_x(char *data)
84{
85 return ((data[3] & 0x0F) << 7) | (data[4] & 0x7F);
86}
87
88static inline int touchkit_get_y(char *data)
89{
90 return ((data[1] & 0x0F) << 7) | (data[2] & 0x7F);
91}
92
93
94/* processes one input packet. */
95static void touchkit_process_pkt(struct touchkit_usb *touchkit,
96 struct pt_regs *regs, char *pkt)
97{
98 int x, y;
99
100 /* only process report packets */
101 if ((pkt[0] & TOUCHKIT_PKT_TYPE_MASK) != TOUCHKIT_PKT_TYPE_REPT)
102 return;
103
104 if (swap_xy) {
105 y = touchkit_get_x(pkt);
106 x = touchkit_get_y(pkt);
107 } else {
108 x = touchkit_get_x(pkt);
109 y = touchkit_get_y(pkt);
110 }
111
112 input_regs(touchkit->input, regs);
113 input_report_key(touchkit->input, BTN_TOUCH, touchkit_get_touched(pkt));
114 input_report_abs(touchkit->input, ABS_X, x);
115 input_report_abs(touchkit->input, ABS_Y, y);
116 input_sync(touchkit->input);
117}
118
119
120static int touchkit_get_pkt_len(char *buf)
121{
122 switch (buf[0] & TOUCHKIT_PKT_TYPE_MASK) {
123 case TOUCHKIT_PKT_TYPE_REPT:
124 return 5;
125
126 case TOUCHKIT_PKT_TYPE_DIAG:
127 return buf[1] + 2;
128 }
129
130 return 0;
131}
132
133static void touchkit_process(struct touchkit_usb *touchkit, int len,
134 struct pt_regs *regs)
135{
136 char *buffer;
137 int pkt_len, buf_len, pos;
138
139 /* if the buffer contains data, append */
140 if (unlikely(touchkit->buf_len)) {
141 int tmp;
142
143 /* if only 1 byte in buffer, add another one to get length */
144 if (touchkit->buf_len == 1)
145 touchkit->buffer[1] = touchkit->data[0];
146
147 pkt_len = touchkit_get_pkt_len(touchkit->buffer);
148
149 /* unknown packet: drop everything */
150 if (!pkt_len)
151 return;
152
153 /* append, process */
154 tmp = pkt_len - touchkit->buf_len;
155 memcpy(touchkit->buffer + touchkit->buf_len, touchkit->data, tmp);
156 touchkit_process_pkt(touchkit, regs, touchkit->buffer);
157
158 buffer = touchkit->data + tmp;
159 buf_len = len - tmp;
160 } else {
161 buffer = touchkit->data;
162 buf_len = len;
163 }
164
165 /* only one byte left in buffer */
166 if (unlikely(buf_len == 1)) {
167 touchkit->buffer[0] = buffer[0];
168 touchkit->buf_len = 1;
169 return;
170 }
171
172 /* loop over the buffer */
173 pos = 0;
174 while (pos < buf_len) {
175 /* get packet len */
176 pkt_len = touchkit_get_pkt_len(buffer + pos);
177
178 /* unknown packet: drop everything */
179 if (unlikely(!pkt_len))
180 return;
181
182 /* full packet: process */
183 if (likely(pkt_len <= buf_len)) {
184 touchkit_process_pkt(touchkit, regs, buffer + pos);
185 } else {
186 /* incomplete packet: save in buffer */
187 memcpy(touchkit->buffer, buffer + pos, buf_len - pos);
188 touchkit->buf_len = buf_len - pos;
189 }
190 pos += pkt_len;
191 }
192}
193
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195static void touchkit_irq(struct urb *urb, struct pt_regs *regs)
196{
197 struct touchkit_usb *touchkit = urb->context;
198 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200 switch (urb->status) {
201 case 0:
202 /* success */
203 break;
Pete Zaitcev38e2bfc2006-09-18 22:49:02 -0700204 case -ETIME:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 /* this urb is timing out */
206 dbg("%s - urb timed out - was the device unplugged?",
207 __FUNCTION__);
208 return;
209 case -ECONNRESET:
210 case -ENOENT:
211 case -ESHUTDOWN:
212 /* this urb is terminated, clean up */
213 dbg("%s - urb shutting down with status: %d",
214 __FUNCTION__, urb->status);
215 return;
216 default:
217 dbg("%s - nonzero urb status received: %d",
218 __FUNCTION__, urb->status);
219 goto exit;
220 }
221
Daniel Ritz5d320292005-11-27 22:23:38 +0100222 touchkit_process(touchkit, urb->actual_length, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224exit:
225 retval = usb_submit_urb(urb, GFP_ATOMIC);
226 if (retval)
227 err("%s - usb_submit_urb failed with result: %d",
228 __FUNCTION__, retval);
229}
230
231static int touchkit_open(struct input_dev *input)
232{
233 struct touchkit_usb *touchkit = input->private;
234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 touchkit->irq->dev = touchkit->udev;
236
Dmitry Torokhov65cde542005-05-29 02:29:38 -0500237 if (usb_submit_urb(touchkit->irq, GFP_ATOMIC))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240 return 0;
241}
242
243static void touchkit_close(struct input_dev *input)
244{
245 struct touchkit_usb *touchkit = input->private;
246
Dmitry Torokhov65cde542005-05-29 02:29:38 -0500247 usb_kill_urb(touchkit->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248}
249
250static int touchkit_alloc_buffers(struct usb_device *udev,
251 struct touchkit_usb *touchkit)
252{
253 touchkit->data = usb_buffer_alloc(udev, TOUCHKIT_REPORT_DATA_SIZE,
254 SLAB_ATOMIC, &touchkit->data_dma);
255
256 if (!touchkit->data)
257 return -1;
258
259 return 0;
260}
261
262static void touchkit_free_buffers(struct usb_device *udev,
263 struct touchkit_usb *touchkit)
264{
265 if (touchkit->data)
266 usb_buffer_free(udev, TOUCHKIT_REPORT_DATA_SIZE,
267 touchkit->data, touchkit->data_dma);
268}
269
270static int touchkit_probe(struct usb_interface *intf,
271 const struct usb_device_id *id)
272{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 struct touchkit_usb *touchkit;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500274 struct input_dev *input_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 struct usb_host_interface *interface;
276 struct usb_endpoint_descriptor *endpoint;
277 struct usb_device *udev = interface_to_usbdev(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 interface = intf->cur_altsetting;
280 endpoint = &interface->endpoint[0].desc;
281
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500282 touchkit = kzalloc(sizeof(struct touchkit_usb), GFP_KERNEL);
283 input_dev = input_allocate_device();
284 if (!touchkit || !input_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500287 if (touchkit_alloc_buffers(udev, touchkit))
288 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290 touchkit->irq = usb_alloc_urb(0, GFP_KERNEL);
291 if (!touchkit->irq) {
292 dbg("%s - usb_alloc_urb failed: touchkit->irq", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 goto out_free_buffers;
294 }
295
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500296 touchkit->udev = udev;
297 touchkit->input = input_dev;
298
299 if (udev->manufacturer)
300 strlcpy(touchkit->name, udev->manufacturer, sizeof(touchkit->name));
301
302 if (udev->product) {
303 if (udev->manufacturer)
304 strlcat(touchkit->name, " ", sizeof(touchkit->name));
305 strlcat(touchkit->name, udev->product, sizeof(touchkit->name));
306 }
307
308 if (!strlen(touchkit->name))
309 snprintf(touchkit->name, sizeof(touchkit->name),
310 "USB Touchscreen %04x:%04x",
311 le16_to_cpu(udev->descriptor.idVendor),
312 le16_to_cpu(udev->descriptor.idProduct));
313
314 usb_make_path(udev, touchkit->phys, sizeof(touchkit->phys));
315 strlcpy(touchkit->phys, "/input0", sizeof(touchkit->phys));
316
317 input_dev->name = touchkit->name;
318 input_dev->phys = touchkit->phys;
319 usb_to_input_id(udev, &input_dev->id);
320 input_dev->cdev.dev = &intf->dev;
321 input_dev->private = touchkit;
322 input_dev->open = touchkit_open;
323 input_dev->close = touchkit_close;
324
325 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
326 input_dev->keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH);
327 input_set_abs_params(input_dev, ABS_X, TOUCHKIT_MIN_XC, TOUCHKIT_MAX_XC,
328 TOUCHKIT_XC_FUZZ, TOUCHKIT_XC_FLAT);
329 input_set_abs_params(input_dev, ABS_Y, TOUCHKIT_MIN_YC, TOUCHKIT_MAX_YC,
330 TOUCHKIT_YC_FUZZ, TOUCHKIT_YC_FLAT);
331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 usb_fill_int_urb(touchkit->irq, touchkit->udev,
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500333 usb_rcvintpipe(touchkit->udev, 0x81),
334 touchkit->data, TOUCHKIT_REPORT_DATA_SIZE,
335 touchkit_irq, touchkit, endpoint->bInterval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Juergen Schindelea0011002006-01-09 08:51:48 +0100337 touchkit->irq->transfer_dma = touchkit->data_dma;
338 touchkit->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
339
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500340 input_register_device(touchkit->input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 usb_set_intfdata(intf, touchkit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 return 0;
344
345out_free_buffers:
346 touchkit_free_buffers(udev, touchkit);
347out_free:
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500348 input_free_device(input_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 kfree(touchkit);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500350 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351}
352
353static void touchkit_disconnect(struct usb_interface *intf)
354{
355 struct touchkit_usb *touchkit = usb_get_intfdata(intf);
356
357 dbg("%s - called", __FUNCTION__);
358
359 if (!touchkit)
360 return;
361
362 dbg("%s - touchkit is initialized, cleaning up", __FUNCTION__);
363 usb_set_intfdata(intf, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 usb_kill_urb(touchkit->irq);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500365 input_unregister_device(touchkit->input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 usb_free_urb(touchkit->irq);
367 touchkit_free_buffers(interface_to_usbdev(intf), touchkit);
368 kfree(touchkit);
369}
370
371MODULE_DEVICE_TABLE(usb, touchkit_devices);
372
373static struct usb_driver touchkit_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 .name = "touchkitusb",
375 .probe = touchkit_probe,
376 .disconnect = touchkit_disconnect,
377 .id_table = touchkit_devices,
378};
379
380static int __init touchkit_init(void)
381{
382 return usb_register(&touchkit_driver);
383}
384
385static void __exit touchkit_cleanup(void)
386{
387 usb_deregister(&touchkit_driver);
388}
389
390module_init(touchkit_init);
391module_exit(touchkit_cleanup);
392
393MODULE_AUTHOR(DRIVER_AUTHOR);
394MODULE_DESCRIPTION(DRIVER_DESC);
395MODULE_LICENSE("GPL");