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/config.h>
39 #include <linux/kernel.h>
40 #include <linux/slab.h>
41 #include <linux/input.h>
42 #include <linux/module.h>
43 #include <linux/init.h>
44 #include <linux/usb.h>
45 #include <linux/usb/input.h>
48 #define DRIVER_VERSION "v0.4"
49 #define DRIVER_AUTHOR "Daniel Ritz <daniel.ritz@gmx.ch>"
50 #define DRIVER_DESC "USB Touchscreen Driver"
53 module_param(swap_xy
, bool, 0644);
54 MODULE_PARM_DESC(swap_xy
, "If set X and Y axes are swapped.");
56 /* device specifc data/functions */
58 struct usbtouch_device_info
{
61 int min_press
, max_press
;
65 void (*process_pkt
) (struct usbtouch_usb
*usbtouch
, struct pt_regs
*regs
, unsigned char *pkt
, int len
);
66 int (*get_pkt_len
) (unsigned char *pkt
, int len
);
67 int (*read_data
) (unsigned char *pkt
, int *x
, int *y
, int *touch
, int *press
);
68 int (*init
) (struct usbtouch_usb
*usbtouch
);
71 #define USBTOUCH_FLG_BUFFER 0x01
74 /* a usbtouch device */
78 unsigned char *buffer
;
81 struct usb_device
*udev
;
82 struct input_dev
*input
;
83 struct usbtouch_device_info
*type
;
89 #if defined(CONFIG_USB_TOUCHSCREEN_EGALAX) || defined(CONFIG_USB_TOUCHSCREEN_ETURBO)
94 static void usbtouch_process_multi(struct usbtouch_usb
*usbtouch
,
96 unsigned char *pkt
, int len
);
110 static struct usb_device_id usbtouch_devices
[] = {
111 #ifdef CONFIG_USB_TOUCHSCREEN_EGALAX
112 {USB_DEVICE(0x3823, 0x0001), .driver_info
= DEVTYPE_EGALAX
},
113 {USB_DEVICE(0x3823, 0x0002), .driver_info
= DEVTYPE_EGALAX
},
114 {USB_DEVICE(0x0123, 0x0001), .driver_info
= DEVTYPE_EGALAX
},
115 {USB_DEVICE(0x0eef, 0x0001), .driver_info
= DEVTYPE_EGALAX
},
116 {USB_DEVICE(0x0eef, 0x0002), .driver_info
= DEVTYPE_EGALAX
},
117 {USB_DEVICE(0x1234, 0x0001), .driver_info
= DEVTYPE_EGALAX
},
118 {USB_DEVICE(0x1234, 0x0002), .driver_info
= DEVTYPE_EGALAX
},
121 #ifdef CONFIG_USB_TOUCHSCREEN_PANJIT
122 {USB_DEVICE(0x134c, 0x0001), .driver_info
= DEVTYPE_PANJIT
},
123 {USB_DEVICE(0x134c, 0x0002), .driver_info
= DEVTYPE_PANJIT
},
124 {USB_DEVICE(0x134c, 0x0003), .driver_info
= DEVTYPE_PANJIT
},
125 {USB_DEVICE(0x134c, 0x0004), .driver_info
= DEVTYPE_PANJIT
},
128 #ifdef CONFIG_USB_TOUCHSCREEN_3M
129 {USB_DEVICE(0x0596, 0x0001), .driver_info
= DEVTYPE_3M
},
132 #ifdef CONFIG_USB_TOUCHSCREEN_ITM
133 {USB_DEVICE(0x0403, 0xf9e9), .driver_info
= DEVTYPE_ITM
},
136 #ifdef CONFIG_USB_TOUCHSCREEN_ETURBO
137 {USB_DEVICE(0x1234, 0x5678), .driver_info
= DEVTYPE_ETURBO
},
140 #ifdef CONFIG_USB_TOUCHSCREEN_GUNZE
141 {USB_DEVICE(0x0637, 0x0001), .driver_info
= DEVTYPE_GUNZE
},
148 /*****************************************************************************
152 #ifdef CONFIG_USB_TOUCHSCREEN_EGALAX
154 #define EGALAX_PKT_TYPE_MASK 0xFE
155 #define EGALAX_PKT_TYPE_REPT 0x80
156 #define EGALAX_PKT_TYPE_DIAG 0x0A
158 static int egalax_read_data(unsigned char *pkt
, int *x
, int *y
, int *touch
, int *press
)
160 if ((pkt
[0] & EGALAX_PKT_TYPE_MASK
) != EGALAX_PKT_TYPE_REPT
)
163 *x
= ((pkt
[3] & 0x0F) << 7) | (pkt
[4] & 0x7F);
164 *y
= ((pkt
[1] & 0x0F) << 7) | (pkt
[2] & 0x7F);
165 *touch
= pkt
[0] & 0x01;
170 static int egalax_get_pkt_len(unsigned char *buf
, int len
)
172 switch (buf
[0] & EGALAX_PKT_TYPE_MASK
) {
173 case EGALAX_PKT_TYPE_REPT
:
176 case EGALAX_PKT_TYPE_DIAG
:
188 /*****************************************************************************
191 #ifdef CONFIG_USB_TOUCHSCREEN_PANJIT
192 static int panjit_read_data(unsigned char *pkt
, int *x
, int *y
, int *touch
, int *press
)
194 *x
= ((pkt
[2] & 0x0F) << 8) | pkt
[1];
195 *y
= ((pkt
[4] & 0x0F) << 8) | pkt
[3];
196 *touch
= pkt
[0] & 0x01;
203 /*****************************************************************************
206 #ifdef CONFIG_USB_TOUCHSCREEN_3M
208 #define MTOUCHUSB_ASYNC_REPORT 1
209 #define MTOUCHUSB_RESET 7
210 #define MTOUCHUSB_REQ_CTRLLR_ID 10
212 static int mtouch_read_data(unsigned char *pkt
, int *x
, int *y
, int *touch
, int *press
)
214 *x
= (pkt
[8] << 8) | pkt
[7];
215 *y
= (pkt
[10] << 8) | pkt
[9];
216 *touch
= (pkt
[2] & 0x40) ? 1 : 0;
221 static int mtouch_init(struct usbtouch_usb
*usbtouch
)
225 ret
= usb_control_msg(usbtouch
->udev
, usb_rcvctrlpipe(usbtouch
->udev
, 0),
227 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
228 1, 0, NULL
, 0, USB_CTRL_SET_TIMEOUT
);
229 dbg("%s - usb_control_msg - MTOUCHUSB_RESET - bytes|err: %d",
235 for (i
= 0; i
< 3; i
++) {
236 ret
= usb_control_msg(usbtouch
->udev
, usb_rcvctrlpipe(usbtouch
->udev
, 0),
237 MTOUCHUSB_ASYNC_REPORT
,
238 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
239 1, 1, NULL
, 0, USB_CTRL_SET_TIMEOUT
);
240 dbg("%s - usb_control_msg - MTOUCHUSB_ASYNC_REPORT - bytes|err: %d",
253 /*****************************************************************************
256 #ifdef CONFIG_USB_TOUCHSCREEN_ITM
257 static int itm_read_data(unsigned char *pkt
, int *x
, int *y
, int *touch
, int *press
)
259 *x
= ((pkt
[0] & 0x1F) << 7) | (pkt
[3] & 0x7F);
260 *y
= ((pkt
[1] & 0x1F) << 7) | (pkt
[4] & 0x7F);
261 *press
= ((pkt
[2] & 0x1F) << 7) | (pkt
[5] & 0x7F);
262 *touch
= ~pkt
[7] & 0x20;
269 /*****************************************************************************
272 #ifdef CONFIG_USB_TOUCHSCREEN_ETURBO
273 static int eturbo_read_data(unsigned char *pkt
, int *x
, int *y
, int *touch
, int *press
)
277 /* packets should start with sync */
278 if (!(pkt
[0] & 0x80))
281 shift
= (6 - (pkt
[0] & 0x03));
282 *x
= ((pkt
[3] << 7) | pkt
[4]) >> shift
;
283 *y
= ((pkt
[1] << 7) | pkt
[2]) >> shift
;
284 *touch
= (pkt
[0] & 0x10) ? 1 : 0;
289 static int eturbo_get_pkt_len(unsigned char *buf
, int len
)
300 /*****************************************************************************
303 #ifdef CONFIG_USB_TOUCHSCREEN_GUNZE
304 static int gunze_read_data(unsigned char *pkt
, int *x
, int *y
, int *touch
, int *press
)
306 if (!(pkt
[0] & 0x80) || ((pkt
[1] | pkt
[2] | pkt
[3]) & 0x80))
309 *x
= ((pkt
[0] & 0x1F) << 7) | (pkt
[2] & 0x7F);
310 *y
= ((pkt
[1] & 0x1F) << 7) | (pkt
[3] & 0x7F);
311 *touch
= pkt
[0] & 0x20;
317 /*****************************************************************************
318 * the different device descriptors
320 static struct usbtouch_device_info usbtouch_dev_info
[] = {
321 #ifdef CONFIG_USB_TOUCHSCREEN_EGALAX
328 .flags
= USBTOUCH_FLG_BUFFER
,
329 .process_pkt
= usbtouch_process_multi
,
330 .get_pkt_len
= egalax_get_pkt_len
,
331 .read_data
= egalax_read_data
,
335 #ifdef CONFIG_USB_TOUCHSCREEN_PANJIT
342 .read_data
= panjit_read_data
,
346 #ifdef CONFIG_USB_TOUCHSCREEN_3M
353 .read_data
= mtouch_read_data
,
358 #ifdef CONFIG_USB_TOUCHSCREEN_ITM
366 .read_data
= itm_read_data
,
370 #ifdef CONFIG_USB_TOUCHSCREEN_ETURBO
377 .flags
= USBTOUCH_FLG_BUFFER
,
378 .process_pkt
= usbtouch_process_multi
,
379 .get_pkt_len
= eturbo_get_pkt_len
,
380 .read_data
= eturbo_read_data
,
384 #ifdef CONFIG_USB_TOUCHSCREEN_GUNZE
391 .read_data
= gunze_read_data
,
397 /*****************************************************************************
400 static void usbtouch_process_pkt(struct usbtouch_usb
*usbtouch
,
401 struct pt_regs
*regs
, unsigned char *pkt
, int len
)
403 int x
, y
, touch
, press
;
404 struct usbtouch_device_info
*type
= usbtouch
->type
;
406 if (!type
->read_data(pkt
, &x
, &y
, &touch
, &press
))
409 input_regs(usbtouch
->input
, regs
);
410 input_report_key(usbtouch
->input
, BTN_TOUCH
, touch
);
413 input_report_abs(usbtouch
->input
, ABS_X
, y
);
414 input_report_abs(usbtouch
->input
, ABS_Y
, x
);
416 input_report_abs(usbtouch
->input
, ABS_X
, x
);
417 input_report_abs(usbtouch
->input
, ABS_Y
, y
);
420 input_report_abs(usbtouch
->input
, ABS_PRESSURE
, press
);
421 input_sync(usbtouch
->input
);
426 static void usbtouch_process_multi(struct usbtouch_usb
*usbtouch
,
427 struct pt_regs
*regs
,
428 unsigned char *pkt
, int len
)
430 unsigned char *buffer
;
431 int pkt_len
, pos
, buf_len
, tmp
;
434 if (unlikely(usbtouch
->buf_len
)) {
435 /* try to get size */
436 pkt_len
= usbtouch
->type
->get_pkt_len(
437 usbtouch
->buffer
, usbtouch
->buf_len
);
440 if (unlikely(!pkt_len
))
443 /* need to append -pkt_len bytes before able to get size */
444 if (unlikely(pkt_len
< 0)) {
445 int append
= -pkt_len
;
446 if (unlikely(append
> len
))
448 if (usbtouch
->buf_len
+ append
>= usbtouch
->type
->rept_size
)
450 memcpy(usbtouch
->buffer
+ usbtouch
->buf_len
, pkt
, append
);
451 usbtouch
->buf_len
+= append
;
453 pkt_len
= usbtouch
->type
->get_pkt_len(
454 usbtouch
->buffer
, usbtouch
->buf_len
);
460 tmp
= pkt_len
- usbtouch
->buf_len
;
461 if (usbtouch
->buf_len
+ tmp
>= usbtouch
->type
->rept_size
)
463 memcpy(usbtouch
->buffer
+ usbtouch
->buf_len
, pkt
, tmp
);
464 usbtouch_process_pkt(usbtouch
, regs
, usbtouch
->buffer
, pkt_len
);
473 /* loop over the received packet, process */
475 while (pos
< buf_len
) {
477 pkt_len
= usbtouch
->type
->get_pkt_len(buffer
+ pos
, len
);
479 /* unknown packet: drop everything */
480 if (unlikely(!pkt_len
))
483 /* full packet: process */
484 if (likely((pkt_len
> 0) && (pkt_len
<= buf_len
- pos
))) {
485 usbtouch_process_pkt(usbtouch
, regs
, buffer
+ pos
, pkt_len
);
487 /* incomplete packet: save in buffer */
488 memcpy(usbtouch
->buffer
, buffer
+ pos
, buf_len
- pos
);
489 usbtouch
->buf_len
= buf_len
- pos
;
496 usbtouch
->buf_len
= 0;
502 static void usbtouch_irq(struct urb
*urb
, struct pt_regs
*regs
)
504 struct usbtouch_usb
*usbtouch
= urb
->context
;
507 switch (urb
->status
) {
512 /* this urb is timing out */
513 dbg("%s - urb timed out - was the device unplugged?",
519 /* this urb is terminated, clean up */
520 dbg("%s - urb shutting down with status: %d",
521 __FUNCTION__
, urb
->status
);
524 dbg("%s - nonzero urb status received: %d",
525 __FUNCTION__
, urb
->status
);
529 usbtouch
->type
->process_pkt(usbtouch
, regs
, usbtouch
->data
, urb
->actual_length
);
532 retval
= usb_submit_urb(urb
, GFP_ATOMIC
);
534 err("%s - usb_submit_urb failed with result: %d",
535 __FUNCTION__
, retval
);
538 static int usbtouch_open(struct input_dev
*input
)
540 struct usbtouch_usb
*usbtouch
= input
->private;
542 usbtouch
->irq
->dev
= usbtouch
->udev
;
544 if (usb_submit_urb(usbtouch
->irq
, GFP_KERNEL
))
550 static void usbtouch_close(struct input_dev
*input
)
552 struct usbtouch_usb
*usbtouch
= input
->private;
554 usb_kill_urb(usbtouch
->irq
);
558 static void usbtouch_free_buffers(struct usb_device
*udev
,
559 struct usbtouch_usb
*usbtouch
)
562 usb_buffer_free(udev
, usbtouch
->type
->rept_size
,
563 usbtouch
->data
, usbtouch
->data_dma
);
564 kfree(usbtouch
->buffer
);
568 static int usbtouch_probe(struct usb_interface
*intf
,
569 const struct usb_device_id
*id
)
571 struct usbtouch_usb
*usbtouch
;
572 struct input_dev
*input_dev
;
573 struct usb_host_interface
*interface
;
574 struct usb_endpoint_descriptor
*endpoint
;
575 struct usb_device
*udev
= interface_to_usbdev(intf
);
576 struct usbtouch_device_info
*type
;
579 interface
= intf
->cur_altsetting
;
580 endpoint
= &interface
->endpoint
[0].desc
;
582 usbtouch
= kzalloc(sizeof(struct usbtouch_usb
), GFP_KERNEL
);
583 input_dev
= input_allocate_device();
584 if (!usbtouch
|| !input_dev
)
587 type
= &usbtouch_dev_info
[id
->driver_info
];
588 usbtouch
->type
= type
;
589 if (!type
->process_pkt
)
590 type
->process_pkt
= usbtouch_process_pkt
;
592 usbtouch
->data
= usb_buffer_alloc(udev
, type
->rept_size
,
593 SLAB_KERNEL
, &usbtouch
->data_dma
);
597 if (type
->flags
& USBTOUCH_FLG_BUFFER
) {
598 usbtouch
->buffer
= kmalloc(type
->rept_size
, GFP_KERNEL
);
599 if (!usbtouch
->buffer
)
600 goto out_free_buffers
;
603 usbtouch
->irq
= usb_alloc_urb(0, GFP_KERNEL
);
604 if (!usbtouch
->irq
) {
605 dbg("%s - usb_alloc_urb failed: usbtouch->irq", __FUNCTION__
);
606 goto out_free_buffers
;
609 usbtouch
->udev
= udev
;
610 usbtouch
->input
= input_dev
;
612 if (udev
->manufacturer
)
613 strlcpy(usbtouch
->name
, udev
->manufacturer
, sizeof(usbtouch
->name
));
616 if (udev
->manufacturer
)
617 strlcat(usbtouch
->name
, " ", sizeof(usbtouch
->name
));
618 strlcat(usbtouch
->name
, udev
->product
, sizeof(usbtouch
->name
));
621 if (!strlen(usbtouch
->name
))
622 snprintf(usbtouch
->name
, sizeof(usbtouch
->name
),
623 "USB Touchscreen %04x:%04x",
624 le16_to_cpu(udev
->descriptor
.idVendor
),
625 le16_to_cpu(udev
->descriptor
.idProduct
));
627 usb_make_path(udev
, usbtouch
->phys
, sizeof(usbtouch
->phys
));
628 strlcpy(usbtouch
->phys
, "/input0", sizeof(usbtouch
->phys
));
630 input_dev
->name
= usbtouch
->name
;
631 input_dev
->phys
= usbtouch
->phys
;
632 usb_to_input_id(udev
, &input_dev
->id
);
633 input_dev
->cdev
.dev
= &intf
->dev
;
634 input_dev
->private = usbtouch
;
635 input_dev
->open
= usbtouch_open
;
636 input_dev
->close
= usbtouch_close
;
638 input_dev
->evbit
[0] = BIT(EV_KEY
) | BIT(EV_ABS
);
639 input_dev
->keybit
[LONG(BTN_TOUCH
)] = BIT(BTN_TOUCH
);
640 input_set_abs_params(input_dev
, ABS_X
, type
->min_xc
, type
->max_xc
, 0, 0);
641 input_set_abs_params(input_dev
, ABS_Y
, type
->min_yc
, type
->max_yc
, 0, 0);
643 input_set_abs_params(input_dev
, ABS_PRESSURE
, type
->min_press
,
644 type
->max_press
, 0, 0);
646 usb_fill_int_urb(usbtouch
->irq
, usbtouch
->udev
,
647 usb_rcvintpipe(usbtouch
->udev
, 0x81),
648 usbtouch
->data
, type
->rept_size
,
649 usbtouch_irq
, usbtouch
, endpoint
->bInterval
);
651 usbtouch
->irq
->dev
= usbtouch
->udev
;
652 usbtouch
->irq
->transfer_dma
= usbtouch
->data_dma
;
653 usbtouch
->irq
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
655 /* device specific init */
657 err
= type
->init(usbtouch
);
659 dbg("%s - type->init() failed, err: %d", __FUNCTION__
, err
);
660 goto out_free_buffers
;
664 err
= input_register_device(usbtouch
->input
);
666 dbg("%s - input_register_device failed, err: %d", __FUNCTION__
, err
);
667 goto out_free_buffers
;
670 usb_set_intfdata(intf
, usbtouch
);
675 usbtouch_free_buffers(udev
, usbtouch
);
677 input_free_device(input_dev
);
682 static void usbtouch_disconnect(struct usb_interface
*intf
)
684 struct usbtouch_usb
*usbtouch
= usb_get_intfdata(intf
);
686 dbg("%s - called", __FUNCTION__
);
691 dbg("%s - usbtouch is initialized, cleaning up", __FUNCTION__
);
692 usb_set_intfdata(intf
, NULL
);
693 usb_kill_urb(usbtouch
->irq
);
694 input_unregister_device(usbtouch
->input
);
695 usb_free_urb(usbtouch
->irq
);
696 usbtouch_free_buffers(interface_to_usbdev(intf
), usbtouch
);
700 MODULE_DEVICE_TABLE(usb
, usbtouch_devices
);
702 static struct usb_driver usbtouch_driver
= {
703 .name
= "usbtouchscreen",
704 .probe
= usbtouch_probe
,
705 .disconnect
= usbtouch_disconnect
,
706 .id_table
= usbtouch_devices
,
709 static int __init
usbtouch_init(void)
711 return usb_register(&usbtouch_driver
);
714 static void __exit
usbtouch_cleanup(void)
716 usb_deregister(&usbtouch_driver
);
719 module_init(usbtouch_init
);
720 module_exit(usbtouch_cleanup
);
722 MODULE_AUTHOR(DRIVER_AUTHOR
);
723 MODULE_DESCRIPTION(DRIVER_DESC
);
724 MODULE_LICENSE("GPL");
726 MODULE_ALIAS("touchkitusb");
727 MODULE_ALIAS("itmtouch");
728 MODULE_ALIAS("mtouchusb");