1 /******************************************************************************
3 * Driver for USB Touchscreens, supporting those devices:
5 * includes eTurboTouch CT-410/510/700
6 * - 3M/Microtouch EX II series
12 * Copyright (C) 2004-2006 by Daniel Ritz <daniel.ritz@gmx.ch>
13 * Copyright (C) by Todd E. Johnson (mtouchusb.c)
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License as
17 * published by the Free Software Foundation; either version 2 of the
18 * License, or (at your option) any later version.
20 * This program is distributed in the hope that it will be useful, but
21 * WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 * Driver is based on touchkitusb.c
30 * - ITM parts are from itmtouch.c
31 * - 3M parts are from mtouchusb.c
32 * - PanJit parts are from an unmerged driver by Lanslott Gish
34 *****************************************************************************/
38 #include <linux/kernel.h>
39 #include <linux/slab.h>
40 #include <linux/input.h>
41 #include <linux/module.h>
42 #include <linux/init.h>
43 #include <linux/usb.h>
44 #include <linux/usb/input.h>
47 #define DRIVER_VERSION "v0.4"
48 #define DRIVER_AUTHOR "Daniel Ritz <daniel.ritz@gmx.ch>"
49 #define DRIVER_DESC "USB Touchscreen Driver"
52 module_param(swap_xy
, bool, 0644);
53 MODULE_PARM_DESC(swap_xy
, "If set X and Y axes are swapped.");
55 /* device specifc data/functions */
57 struct usbtouch_device_info
{
60 int min_press
, max_press
;
64 void (*process_pkt
) (struct usbtouch_usb
*usbtouch
, struct pt_regs
*regs
, unsigned char *pkt
, int len
);
65 int (*get_pkt_len
) (unsigned char *pkt
, int len
);
66 int (*read_data
) (unsigned char *pkt
, int *x
, int *y
, int *touch
, int *press
);
67 int (*init
) (struct usbtouch_usb
*usbtouch
);
70 #define USBTOUCH_FLG_BUFFER 0x01
73 /* a usbtouch device */
77 unsigned char *buffer
;
80 struct usb_device
*udev
;
81 struct input_dev
*input
;
82 struct usbtouch_device_info
*type
;
88 #if defined(CONFIG_USB_TOUCHSCREEN_EGALAX) || defined(CONFIG_USB_TOUCHSCREEN_ETURBO)
93 static void usbtouch_process_multi(struct usbtouch_usb
*usbtouch
,
95 unsigned char *pkt
, int len
);
109 static struct usb_device_id usbtouch_devices
[] = {
110 #ifdef CONFIG_USB_TOUCHSCREEN_EGALAX
111 {USB_DEVICE(0x3823, 0x0001), .driver_info
= DEVTYPE_EGALAX
},
112 {USB_DEVICE(0x3823, 0x0002), .driver_info
= DEVTYPE_EGALAX
},
113 {USB_DEVICE(0x0123, 0x0001), .driver_info
= DEVTYPE_EGALAX
},
114 {USB_DEVICE(0x0eef, 0x0001), .driver_info
= DEVTYPE_EGALAX
},
115 {USB_DEVICE(0x0eef, 0x0002), .driver_info
= DEVTYPE_EGALAX
},
116 {USB_DEVICE(0x1234, 0x0001), .driver_info
= DEVTYPE_EGALAX
},
117 {USB_DEVICE(0x1234, 0x0002), .driver_info
= DEVTYPE_EGALAX
},
120 #ifdef CONFIG_USB_TOUCHSCREEN_PANJIT
121 {USB_DEVICE(0x134c, 0x0001), .driver_info
= DEVTYPE_PANJIT
},
122 {USB_DEVICE(0x134c, 0x0002), .driver_info
= DEVTYPE_PANJIT
},
123 {USB_DEVICE(0x134c, 0x0003), .driver_info
= DEVTYPE_PANJIT
},
124 {USB_DEVICE(0x134c, 0x0004), .driver_info
= DEVTYPE_PANJIT
},
127 #ifdef CONFIG_USB_TOUCHSCREEN_3M
128 {USB_DEVICE(0x0596, 0x0001), .driver_info
= DEVTYPE_3M
},
131 #ifdef CONFIG_USB_TOUCHSCREEN_ITM
132 {USB_DEVICE(0x0403, 0xf9e9), .driver_info
= DEVTYPE_ITM
},
135 #ifdef CONFIG_USB_TOUCHSCREEN_ETURBO
136 {USB_DEVICE(0x1234, 0x5678), .driver_info
= DEVTYPE_ETURBO
},
139 #ifdef CONFIG_USB_TOUCHSCREEN_GUNZE
140 {USB_DEVICE(0x0637, 0x0001), .driver_info
= DEVTYPE_GUNZE
},
147 /*****************************************************************************
151 #ifdef CONFIG_USB_TOUCHSCREEN_EGALAX
153 #define EGALAX_PKT_TYPE_MASK 0xFE
154 #define EGALAX_PKT_TYPE_REPT 0x80
155 #define EGALAX_PKT_TYPE_DIAG 0x0A
157 static int egalax_read_data(unsigned char *pkt
, int *x
, int *y
, int *touch
, int *press
)
159 if ((pkt
[0] & EGALAX_PKT_TYPE_MASK
) != EGALAX_PKT_TYPE_REPT
)
162 *x
= ((pkt
[3] & 0x0F) << 7) | (pkt
[4] & 0x7F);
163 *y
= ((pkt
[1] & 0x0F) << 7) | (pkt
[2] & 0x7F);
164 *touch
= pkt
[0] & 0x01;
169 static int egalax_get_pkt_len(unsigned char *buf
, int len
)
171 switch (buf
[0] & EGALAX_PKT_TYPE_MASK
) {
172 case EGALAX_PKT_TYPE_REPT
:
175 case EGALAX_PKT_TYPE_DIAG
:
187 /*****************************************************************************
190 #ifdef CONFIG_USB_TOUCHSCREEN_PANJIT
191 static int panjit_read_data(unsigned char *pkt
, int *x
, int *y
, int *touch
, int *press
)
193 *x
= ((pkt
[2] & 0x0F) << 8) | pkt
[1];
194 *y
= ((pkt
[4] & 0x0F) << 8) | pkt
[3];
195 *touch
= pkt
[0] & 0x01;
202 /*****************************************************************************
205 #ifdef CONFIG_USB_TOUCHSCREEN_3M
207 #define MTOUCHUSB_ASYNC_REPORT 1
208 #define MTOUCHUSB_RESET 7
209 #define MTOUCHUSB_REQ_CTRLLR_ID 10
211 static int mtouch_read_data(unsigned char *pkt
, int *x
, int *y
, int *touch
, int *press
)
213 *x
= (pkt
[8] << 8) | pkt
[7];
214 *y
= (pkt
[10] << 8) | pkt
[9];
215 *touch
= (pkt
[2] & 0x40) ? 1 : 0;
220 static int mtouch_init(struct usbtouch_usb
*usbtouch
)
224 ret
= usb_control_msg(usbtouch
->udev
, usb_rcvctrlpipe(usbtouch
->udev
, 0),
226 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
227 1, 0, NULL
, 0, USB_CTRL_SET_TIMEOUT
);
228 dbg("%s - usb_control_msg - MTOUCHUSB_RESET - bytes|err: %d",
234 for (i
= 0; i
< 3; i
++) {
235 ret
= usb_control_msg(usbtouch
->udev
, usb_rcvctrlpipe(usbtouch
->udev
, 0),
236 MTOUCHUSB_ASYNC_REPORT
,
237 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
238 1, 1, NULL
, 0, USB_CTRL_SET_TIMEOUT
);
239 dbg("%s - usb_control_msg - MTOUCHUSB_ASYNC_REPORT - bytes|err: %d",
252 /*****************************************************************************
255 #ifdef CONFIG_USB_TOUCHSCREEN_ITM
256 static int itm_read_data(unsigned char *pkt
, int *x
, int *y
, int *touch
, int *press
)
258 *x
= ((pkt
[0] & 0x1F) << 7) | (pkt
[3] & 0x7F);
259 *y
= ((pkt
[1] & 0x1F) << 7) | (pkt
[4] & 0x7F);
260 *press
= ((pkt
[2] & 0x1F) << 7) | (pkt
[5] & 0x7F);
261 *touch
= ~pkt
[7] & 0x20;
268 /*****************************************************************************
271 #ifdef CONFIG_USB_TOUCHSCREEN_ETURBO
272 static int eturbo_read_data(unsigned char *pkt
, int *x
, int *y
, int *touch
, int *press
)
276 /* packets should start with sync */
277 if (!(pkt
[0] & 0x80))
280 shift
= (6 - (pkt
[0] & 0x03));
281 *x
= ((pkt
[3] << 7) | pkt
[4]) >> shift
;
282 *y
= ((pkt
[1] << 7) | pkt
[2]) >> shift
;
283 *touch
= (pkt
[0] & 0x10) ? 1 : 0;
288 static int eturbo_get_pkt_len(unsigned char *buf
, int len
)
299 /*****************************************************************************
302 #ifdef CONFIG_USB_TOUCHSCREEN_GUNZE
303 static int gunze_read_data(unsigned char *pkt
, int *x
, int *y
, int *touch
, int *press
)
305 if (!(pkt
[0] & 0x80) || ((pkt
[1] | pkt
[2] | pkt
[3]) & 0x80))
308 *x
= ((pkt
[0] & 0x1F) << 7) | (pkt
[2] & 0x7F);
309 *y
= ((pkt
[1] & 0x1F) << 7) | (pkt
[3] & 0x7F);
310 *touch
= pkt
[0] & 0x20;
316 /*****************************************************************************
317 * the different device descriptors
319 static struct usbtouch_device_info usbtouch_dev_info
[] = {
320 #ifdef CONFIG_USB_TOUCHSCREEN_EGALAX
327 .flags
= USBTOUCH_FLG_BUFFER
,
328 .process_pkt
= usbtouch_process_multi
,
329 .get_pkt_len
= egalax_get_pkt_len
,
330 .read_data
= egalax_read_data
,
334 #ifdef CONFIG_USB_TOUCHSCREEN_PANJIT
341 .read_data
= panjit_read_data
,
345 #ifdef CONFIG_USB_TOUCHSCREEN_3M
352 .read_data
= mtouch_read_data
,
357 #ifdef CONFIG_USB_TOUCHSCREEN_ITM
365 .read_data
= itm_read_data
,
369 #ifdef CONFIG_USB_TOUCHSCREEN_ETURBO
376 .flags
= USBTOUCH_FLG_BUFFER
,
377 .process_pkt
= usbtouch_process_multi
,
378 .get_pkt_len
= eturbo_get_pkt_len
,
379 .read_data
= eturbo_read_data
,
383 #ifdef CONFIG_USB_TOUCHSCREEN_GUNZE
390 .read_data
= gunze_read_data
,
396 /*****************************************************************************
399 static void usbtouch_process_pkt(struct usbtouch_usb
*usbtouch
,
400 struct pt_regs
*regs
, unsigned char *pkt
, int len
)
402 int x
, y
, touch
, press
;
403 struct usbtouch_device_info
*type
= usbtouch
->type
;
405 if (!type
->read_data(pkt
, &x
, &y
, &touch
, &press
))
408 input_regs(usbtouch
->input
, regs
);
409 input_report_key(usbtouch
->input
, BTN_TOUCH
, touch
);
412 input_report_abs(usbtouch
->input
, ABS_X
, y
);
413 input_report_abs(usbtouch
->input
, ABS_Y
, x
);
415 input_report_abs(usbtouch
->input
, ABS_X
, x
);
416 input_report_abs(usbtouch
->input
, ABS_Y
, y
);
419 input_report_abs(usbtouch
->input
, ABS_PRESSURE
, press
);
420 input_sync(usbtouch
->input
);
425 static void usbtouch_process_multi(struct usbtouch_usb
*usbtouch
,
426 struct pt_regs
*regs
,
427 unsigned char *pkt
, int len
)
429 unsigned char *buffer
;
430 int pkt_len
, pos
, buf_len
, tmp
;
433 if (unlikely(usbtouch
->buf_len
)) {
434 /* try to get size */
435 pkt_len
= usbtouch
->type
->get_pkt_len(
436 usbtouch
->buffer
, usbtouch
->buf_len
);
439 if (unlikely(!pkt_len
))
442 /* need to append -pkt_len bytes before able to get size */
443 if (unlikely(pkt_len
< 0)) {
444 int append
= -pkt_len
;
445 if (unlikely(append
> len
))
447 if (usbtouch
->buf_len
+ append
>= usbtouch
->type
->rept_size
)
449 memcpy(usbtouch
->buffer
+ usbtouch
->buf_len
, pkt
, append
);
450 usbtouch
->buf_len
+= append
;
452 pkt_len
= usbtouch
->type
->get_pkt_len(
453 usbtouch
->buffer
, usbtouch
->buf_len
);
459 tmp
= pkt_len
- usbtouch
->buf_len
;
460 if (usbtouch
->buf_len
+ tmp
>= usbtouch
->type
->rept_size
)
462 memcpy(usbtouch
->buffer
+ usbtouch
->buf_len
, pkt
, tmp
);
463 usbtouch_process_pkt(usbtouch
, regs
, usbtouch
->buffer
, pkt_len
);
472 /* loop over the received packet, process */
474 while (pos
< buf_len
) {
476 pkt_len
= usbtouch
->type
->get_pkt_len(buffer
+ pos
, len
);
478 /* unknown packet: drop everything */
479 if (unlikely(!pkt_len
))
482 /* full packet: process */
483 if (likely((pkt_len
> 0) && (pkt_len
<= buf_len
- pos
))) {
484 usbtouch_process_pkt(usbtouch
, regs
, buffer
+ pos
, pkt_len
);
486 /* incomplete packet: save in buffer */
487 memcpy(usbtouch
->buffer
, buffer
+ pos
, buf_len
- pos
);
488 usbtouch
->buf_len
= buf_len
- pos
;
495 usbtouch
->buf_len
= 0;
501 static void usbtouch_irq(struct urb
*urb
, struct pt_regs
*regs
)
503 struct usbtouch_usb
*usbtouch
= urb
->context
;
506 switch (urb
->status
) {
511 /* this urb is timing out */
512 dbg("%s - urb timed out - was the device unplugged?",
518 /* this urb is terminated, clean up */
519 dbg("%s - urb shutting down with status: %d",
520 __FUNCTION__
, urb
->status
);
523 dbg("%s - nonzero urb status received: %d",
524 __FUNCTION__
, urb
->status
);
528 usbtouch
->type
->process_pkt(usbtouch
, regs
, usbtouch
->data
, urb
->actual_length
);
531 retval
= usb_submit_urb(urb
, GFP_ATOMIC
);
533 err("%s - usb_submit_urb failed with result: %d",
534 __FUNCTION__
, retval
);
537 static int usbtouch_open(struct input_dev
*input
)
539 struct usbtouch_usb
*usbtouch
= input
->private;
541 usbtouch
->irq
->dev
= usbtouch
->udev
;
543 if (usb_submit_urb(usbtouch
->irq
, GFP_KERNEL
))
549 static void usbtouch_close(struct input_dev
*input
)
551 struct usbtouch_usb
*usbtouch
= input
->private;
553 usb_kill_urb(usbtouch
->irq
);
557 static void usbtouch_free_buffers(struct usb_device
*udev
,
558 struct usbtouch_usb
*usbtouch
)
561 usb_buffer_free(udev
, usbtouch
->type
->rept_size
,
562 usbtouch
->data
, usbtouch
->data_dma
);
563 kfree(usbtouch
->buffer
);
567 static int usbtouch_probe(struct usb_interface
*intf
,
568 const struct usb_device_id
*id
)
570 struct usbtouch_usb
*usbtouch
;
571 struct input_dev
*input_dev
;
572 struct usb_host_interface
*interface
;
573 struct usb_endpoint_descriptor
*endpoint
;
574 struct usb_device
*udev
= interface_to_usbdev(intf
);
575 struct usbtouch_device_info
*type
;
578 interface
= intf
->cur_altsetting
;
579 endpoint
= &interface
->endpoint
[0].desc
;
581 usbtouch
= kzalloc(sizeof(struct usbtouch_usb
), GFP_KERNEL
);
582 input_dev
= input_allocate_device();
583 if (!usbtouch
|| !input_dev
)
586 type
= &usbtouch_dev_info
[id
->driver_info
];
587 usbtouch
->type
= type
;
588 if (!type
->process_pkt
)
589 type
->process_pkt
= usbtouch_process_pkt
;
591 usbtouch
->data
= usb_buffer_alloc(udev
, type
->rept_size
,
592 SLAB_KERNEL
, &usbtouch
->data_dma
);
596 if (type
->flags
& USBTOUCH_FLG_BUFFER
) {
597 usbtouch
->buffer
= kmalloc(type
->rept_size
, GFP_KERNEL
);
598 if (!usbtouch
->buffer
)
599 goto out_free_buffers
;
602 usbtouch
->irq
= usb_alloc_urb(0, GFP_KERNEL
);
603 if (!usbtouch
->irq
) {
604 dbg("%s - usb_alloc_urb failed: usbtouch->irq", __FUNCTION__
);
605 goto out_free_buffers
;
608 usbtouch
->udev
= udev
;
609 usbtouch
->input
= input_dev
;
611 if (udev
->manufacturer
)
612 strlcpy(usbtouch
->name
, udev
->manufacturer
, sizeof(usbtouch
->name
));
615 if (udev
->manufacturer
)
616 strlcat(usbtouch
->name
, " ", sizeof(usbtouch
->name
));
617 strlcat(usbtouch
->name
, udev
->product
, sizeof(usbtouch
->name
));
620 if (!strlen(usbtouch
->name
))
621 snprintf(usbtouch
->name
, sizeof(usbtouch
->name
),
622 "USB Touchscreen %04x:%04x",
623 le16_to_cpu(udev
->descriptor
.idVendor
),
624 le16_to_cpu(udev
->descriptor
.idProduct
));
626 usb_make_path(udev
, usbtouch
->phys
, sizeof(usbtouch
->phys
));
627 strlcpy(usbtouch
->phys
, "/input0", sizeof(usbtouch
->phys
));
629 input_dev
->name
= usbtouch
->name
;
630 input_dev
->phys
= usbtouch
->phys
;
631 usb_to_input_id(udev
, &input_dev
->id
);
632 input_dev
->cdev
.dev
= &intf
->dev
;
633 input_dev
->private = usbtouch
;
634 input_dev
->open
= usbtouch_open
;
635 input_dev
->close
= usbtouch_close
;
637 input_dev
->evbit
[0] = BIT(EV_KEY
) | BIT(EV_ABS
);
638 input_dev
->keybit
[LONG(BTN_TOUCH
)] = BIT(BTN_TOUCH
);
639 input_set_abs_params(input_dev
, ABS_X
, type
->min_xc
, type
->max_xc
, 0, 0);
640 input_set_abs_params(input_dev
, ABS_Y
, type
->min_yc
, type
->max_yc
, 0, 0);
642 input_set_abs_params(input_dev
, ABS_PRESSURE
, type
->min_press
,
643 type
->max_press
, 0, 0);
645 usb_fill_int_urb(usbtouch
->irq
, usbtouch
->udev
,
646 usb_rcvintpipe(usbtouch
->udev
, 0x81),
647 usbtouch
->data
, type
->rept_size
,
648 usbtouch_irq
, usbtouch
, endpoint
->bInterval
);
650 usbtouch
->irq
->dev
= usbtouch
->udev
;
651 usbtouch
->irq
->transfer_dma
= usbtouch
->data_dma
;
652 usbtouch
->irq
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
654 /* device specific init */
656 err
= type
->init(usbtouch
);
658 dbg("%s - type->init() failed, err: %d", __FUNCTION__
, err
);
659 goto out_free_buffers
;
663 err
= input_register_device(usbtouch
->input
);
665 dbg("%s - input_register_device failed, err: %d", __FUNCTION__
, err
);
666 goto out_free_buffers
;
669 usb_set_intfdata(intf
, usbtouch
);
674 usbtouch_free_buffers(udev
, usbtouch
);
676 input_free_device(input_dev
);
681 static void usbtouch_disconnect(struct usb_interface
*intf
)
683 struct usbtouch_usb
*usbtouch
= usb_get_intfdata(intf
);
685 dbg("%s - called", __FUNCTION__
);
690 dbg("%s - usbtouch is initialized, cleaning up", __FUNCTION__
);
691 usb_set_intfdata(intf
, NULL
);
692 usb_kill_urb(usbtouch
->irq
);
693 input_unregister_device(usbtouch
->input
);
694 usb_free_urb(usbtouch
->irq
);
695 usbtouch_free_buffers(interface_to_usbdev(intf
), usbtouch
);
699 MODULE_DEVICE_TABLE(usb
, usbtouch_devices
);
701 static struct usb_driver usbtouch_driver
= {
702 .name
= "usbtouchscreen",
703 .probe
= usbtouch_probe
,
704 .disconnect
= usbtouch_disconnect
,
705 .id_table
= usbtouch_devices
,
708 static int __init
usbtouch_init(void)
710 return usb_register(&usbtouch_driver
);
713 static void __exit
usbtouch_cleanup(void)
715 usb_deregister(&usbtouch_driver
);
718 module_init(usbtouch_init
);
719 module_exit(usbtouch_cleanup
);
721 MODULE_AUTHOR(DRIVER_AUTHOR
);
722 MODULE_DESCRIPTION(DRIVER_DESC
);
723 MODULE_LICENSE("GPL");
725 MODULE_ALIAS("touchkitusb");
726 MODULE_ALIAS("itmtouch");
727 MODULE_ALIAS("mtouchusb");