1 /******************************************************************************
2 * touchkitusb.c -- Driver for eGalax TouchKit USB Touchscreens
4 * Copyright (C) 2004-2005 by Daniel Ritz <daniel.ritz@gmx.ch>
5 * Copyright (C) by Todd E. Johnson (mtouchusb.c)
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.
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.
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.
21 * Based upon mtouchusb.c
23 *****************************************************************************/
27 #include <linux/kernel.h>
28 #include <linux/slab.h>
29 #include <linux/module.h>
30 #include <linux/init.h>
31 #include <linux/usb/input.h>
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
41 #define TOUCHKIT_REPORT_DATA_SIZE 16
43 #define TOUCHKIT_DOWN 0x01
45 #define TOUCHKIT_PKT_TYPE_MASK 0xFE
46 #define TOUCHKIT_PKT_TYPE_REPT 0x80
47 #define TOUCHKIT_PKT_TYPE_DIAG 0x0A
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"
54 module_param(swap_xy
, bool, 0644);
55 MODULE_PARM_DESC(swap_xy
, "If set X and Y axes are swapped.");
60 char buffer
[TOUCHKIT_REPORT_DATA_SIZE
];
63 struct usb_device
*udev
;
64 struct input_dev
*input
;
69 static struct usb_device_id touchkit_devices
[] = {
70 {USB_DEVICE(0x3823, 0x0001)},
71 {USB_DEVICE(0x0123, 0x0001)},
72 {USB_DEVICE(0x0eef, 0x0001)},
73 {USB_DEVICE(0x0eef, 0x0002)},
77 /* helpers to read the data */
78 static inline int touchkit_get_touched(char *data
)
80 return (data
[0] & TOUCHKIT_DOWN
) ? 1 : 0;
83 static inline int touchkit_get_x(char *data
)
85 return ((data
[3] & 0x0F) << 7) | (data
[4] & 0x7F);
88 static inline int touchkit_get_y(char *data
)
90 return ((data
[1] & 0x0F) << 7) | (data
[2] & 0x7F);
94 /* processes one input packet. */
95 static void touchkit_process_pkt(struct touchkit_usb
*touchkit
, char *pkt
)
99 /* only process report packets */
100 if ((pkt
[0] & TOUCHKIT_PKT_TYPE_MASK
) != TOUCHKIT_PKT_TYPE_REPT
)
104 y
= touchkit_get_x(pkt
);
105 x
= touchkit_get_y(pkt
);
107 x
= touchkit_get_x(pkt
);
108 y
= touchkit_get_y(pkt
);
111 input_report_key(touchkit
->input
, BTN_TOUCH
, touchkit_get_touched(pkt
));
112 input_report_abs(touchkit
->input
, ABS_X
, x
);
113 input_report_abs(touchkit
->input
, ABS_Y
, y
);
114 input_sync(touchkit
->input
);
118 static int touchkit_get_pkt_len(char *buf
)
120 switch (buf
[0] & TOUCHKIT_PKT_TYPE_MASK
) {
121 case TOUCHKIT_PKT_TYPE_REPT
:
124 case TOUCHKIT_PKT_TYPE_DIAG
:
131 static void touchkit_process(struct touchkit_usb
*touchkit
, int len
)
134 int pkt_len
, buf_len
, pos
;
136 /* if the buffer contains data, append */
137 if (unlikely(touchkit
->buf_len
)) {
140 /* if only 1 byte in buffer, add another one to get length */
141 if (touchkit
->buf_len
== 1)
142 touchkit
->buffer
[1] = touchkit
->data
[0];
144 pkt_len
= touchkit_get_pkt_len(touchkit
->buffer
);
146 /* unknown packet: drop everything */
150 /* append, process */
151 tmp
= pkt_len
- touchkit
->buf_len
;
152 memcpy(touchkit
->buffer
+ touchkit
->buf_len
, touchkit
->data
, tmp
);
153 touchkit_process_pkt(touchkit
, touchkit
->buffer
);
155 buffer
= touchkit
->data
+ tmp
;
158 buffer
= touchkit
->data
;
162 /* only one byte left in buffer */
163 if (unlikely(buf_len
== 1)) {
164 touchkit
->buffer
[0] = buffer
[0];
165 touchkit
->buf_len
= 1;
169 /* loop over the buffer */
171 while (pos
< buf_len
) {
173 pkt_len
= touchkit_get_pkt_len(buffer
+ pos
);
175 /* unknown packet: drop everything */
176 if (unlikely(!pkt_len
))
179 /* full packet: process */
180 if (likely(pkt_len
<= buf_len
)) {
181 touchkit_process_pkt(touchkit
, buffer
+ pos
);
183 /* incomplete packet: save in buffer */
184 memcpy(touchkit
->buffer
, buffer
+ pos
, buf_len
- pos
);
185 touchkit
->buf_len
= buf_len
- pos
;
192 static void touchkit_irq(struct urb
*urb
)
194 struct touchkit_usb
*touchkit
= urb
->context
;
197 switch (urb
->status
) {
202 /* this urb is timing out */
203 dbg("%s - urb timed out - was the device unplugged?",
209 /* this urb is terminated, clean up */
210 dbg("%s - urb shutting down with status: %d",
211 __FUNCTION__
, urb
->status
);
214 dbg("%s - nonzero urb status received: %d",
215 __FUNCTION__
, urb
->status
);
219 touchkit_process(touchkit
, urb
->actual_length
);
222 retval
= usb_submit_urb(urb
, GFP_ATOMIC
);
224 err("%s - usb_submit_urb failed with result: %d",
225 __FUNCTION__
, retval
);
228 static int touchkit_open(struct input_dev
*input
)
230 struct touchkit_usb
*touchkit
= input
->private;
232 touchkit
->irq
->dev
= touchkit
->udev
;
234 if (usb_submit_urb(touchkit
->irq
, GFP_ATOMIC
))
240 static void touchkit_close(struct input_dev
*input
)
242 struct touchkit_usb
*touchkit
= input
->private;
244 usb_kill_urb(touchkit
->irq
);
247 static int touchkit_alloc_buffers(struct usb_device
*udev
,
248 struct touchkit_usb
*touchkit
)
250 touchkit
->data
= usb_buffer_alloc(udev
, TOUCHKIT_REPORT_DATA_SIZE
,
251 SLAB_ATOMIC
, &touchkit
->data_dma
);
259 static void touchkit_free_buffers(struct usb_device
*udev
,
260 struct touchkit_usb
*touchkit
)
263 usb_buffer_free(udev
, TOUCHKIT_REPORT_DATA_SIZE
,
264 touchkit
->data
, touchkit
->data_dma
);
267 static int touchkit_probe(struct usb_interface
*intf
,
268 const struct usb_device_id
*id
)
270 struct touchkit_usb
*touchkit
;
271 struct input_dev
*input_dev
;
272 struct usb_host_interface
*interface
;
273 struct usb_endpoint_descriptor
*endpoint
;
274 struct usb_device
*udev
= interface_to_usbdev(intf
);
276 interface
= intf
->cur_altsetting
;
277 endpoint
= &interface
->endpoint
[0].desc
;
279 touchkit
= kzalloc(sizeof(struct touchkit_usb
), GFP_KERNEL
);
280 input_dev
= input_allocate_device();
281 if (!touchkit
|| !input_dev
)
284 if (touchkit_alloc_buffers(udev
, touchkit
))
287 touchkit
->irq
= usb_alloc_urb(0, GFP_KERNEL
);
288 if (!touchkit
->irq
) {
289 dbg("%s - usb_alloc_urb failed: touchkit->irq", __FUNCTION__
);
290 goto out_free_buffers
;
293 touchkit
->udev
= udev
;
294 touchkit
->input
= input_dev
;
296 if (udev
->manufacturer
)
297 strlcpy(touchkit
->name
, udev
->manufacturer
, sizeof(touchkit
->name
));
300 if (udev
->manufacturer
)
301 strlcat(touchkit
->name
, " ", sizeof(touchkit
->name
));
302 strlcat(touchkit
->name
, udev
->product
, sizeof(touchkit
->name
));
305 if (!strlen(touchkit
->name
))
306 snprintf(touchkit
->name
, sizeof(touchkit
->name
),
307 "USB Touchscreen %04x:%04x",
308 le16_to_cpu(udev
->descriptor
.idVendor
),
309 le16_to_cpu(udev
->descriptor
.idProduct
));
311 usb_make_path(udev
, touchkit
->phys
, sizeof(touchkit
->phys
));
312 strlcpy(touchkit
->phys
, "/input0", sizeof(touchkit
->phys
));
314 input_dev
->name
= touchkit
->name
;
315 input_dev
->phys
= touchkit
->phys
;
316 usb_to_input_id(udev
, &input_dev
->id
);
317 input_dev
->cdev
.dev
= &intf
->dev
;
318 input_dev
->private = touchkit
;
319 input_dev
->open
= touchkit_open
;
320 input_dev
->close
= touchkit_close
;
322 input_dev
->evbit
[0] = BIT(EV_KEY
) | BIT(EV_ABS
);
323 input_dev
->keybit
[LONG(BTN_TOUCH
)] = BIT(BTN_TOUCH
);
324 input_set_abs_params(input_dev
, ABS_X
, TOUCHKIT_MIN_XC
, TOUCHKIT_MAX_XC
,
325 TOUCHKIT_XC_FUZZ
, TOUCHKIT_XC_FLAT
);
326 input_set_abs_params(input_dev
, ABS_Y
, TOUCHKIT_MIN_YC
, TOUCHKIT_MAX_YC
,
327 TOUCHKIT_YC_FUZZ
, TOUCHKIT_YC_FLAT
);
329 usb_fill_int_urb(touchkit
->irq
, touchkit
->udev
,
330 usb_rcvintpipe(touchkit
->udev
, 0x81),
331 touchkit
->data
, TOUCHKIT_REPORT_DATA_SIZE
,
332 touchkit_irq
, touchkit
, endpoint
->bInterval
);
334 touchkit
->irq
->transfer_dma
= touchkit
->data_dma
;
335 touchkit
->irq
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
337 input_register_device(touchkit
->input
);
339 usb_set_intfdata(intf
, touchkit
);
343 touchkit_free_buffers(udev
, touchkit
);
345 input_free_device(input_dev
);
350 static void touchkit_disconnect(struct usb_interface
*intf
)
352 struct touchkit_usb
*touchkit
= usb_get_intfdata(intf
);
354 dbg("%s - called", __FUNCTION__
);
359 dbg("%s - touchkit is initialized, cleaning up", __FUNCTION__
);
360 usb_set_intfdata(intf
, NULL
);
361 usb_kill_urb(touchkit
->irq
);
362 input_unregister_device(touchkit
->input
);
363 usb_free_urb(touchkit
->irq
);
364 touchkit_free_buffers(interface_to_usbdev(intf
), touchkit
);
368 MODULE_DEVICE_TABLE(usb
, touchkit_devices
);
370 static struct usb_driver touchkit_driver
= {
371 .name
= "touchkitusb",
372 .probe
= touchkit_probe
,
373 .disconnect
= touchkit_disconnect
,
374 .id_table
= touchkit_devices
,
377 static int __init
touchkit_init(void)
379 return usb_register(&touchkit_driver
);
382 static void __exit
touchkit_cleanup(void)
384 usb_deregister(&touchkit_driver
);
387 module_init(touchkit_init
);
388 module_exit(touchkit_cleanup
);
390 MODULE_AUTHOR(DRIVER_AUTHOR
);
391 MODULE_DESCRIPTION(DRIVER_DESC
);
392 MODULE_LICENSE("GPL");