1 /******************************************************************************
3 * Driver for USB Touchscreens, supporting those devices:
5 * includes eTurboTouch CT-410/510/700
6 * - 3M/Microtouch EX II series
12 * - IRTOUCHSYSTEMS/UNITOP
14 * Copyright (C) 2004-2006 by Daniel Ritz <daniel.ritz@gmx.ch>
15 * Copyright (C) by Todd E. Johnson (mtouchusb.c)
17 * This program is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU General Public License as
19 * published by the Free Software Foundation; either version 2 of the
20 * License, or (at your option) any later version.
22 * This program is distributed in the hope that it will be useful, but
23 * WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 * General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31 * Driver is based on touchkitusb.c
32 * - ITM parts are from itmtouch.c
33 * - 3M parts are from mtouchusb.c
34 * - PanJit parts are from an unmerged driver by Lanslott Gish
35 * - DMC TSC 10/25 are from Holger Schurig, with ideas from an unmerged
36 * driver from Marius Vollmer
38 *****************************************************************************/
42 #include <linux/kernel.h>
43 #include <linux/slab.h>
44 #include <linux/input.h>
45 #include <linux/module.h>
46 #include <linux/init.h>
47 #include <linux/usb.h>
48 #include <linux/usb/input.h>
51 #define DRIVER_VERSION "v0.5"
52 #define DRIVER_AUTHOR "Daniel Ritz <daniel.ritz@gmx.ch>"
53 #define DRIVER_DESC "USB Touchscreen Driver"
56 module_param(swap_xy, bool, 0644);
57 MODULE_PARM_DESC(swap_xy, "If set X and Y axes are swapped.");
59 /* device specifc data/functions */
61 struct usbtouch_device_info {
64 int min_press, max_press;
68 void (*process_pkt) (struct usbtouch_usb *usbtouch, unsigned char *pkt, int len);
69 int (*get_pkt_len) (unsigned char *pkt, int len);
70 int (*read_data) (struct usbtouch_usb *usbtouch, unsigned char *pkt);
71 int (*init) (struct usbtouch_usb *usbtouch);
74 #define USBTOUCH_FLG_BUFFER 0x01
77 /* a usbtouch device */
81 unsigned char *buffer;
84 struct usb_device *udev;
85 struct input_dev *input;
86 struct usbtouch_device_info *type;
95 #if defined(CONFIG_TOUCHSCREEN_USB_EGALAX) || defined(CONFIG_TOUCHSCREEN_USB_ETURBO)
100 static void usbtouch_process_multi(struct usbtouch_usb *usbtouch,
101 unsigned char *pkt, int len);
117 static struct usb_device_id usbtouch_devices[] = {
118 #ifdef CONFIG_TOUCHSCREEN_USB_EGALAX
119 {USB_DEVICE(0x3823, 0x0001), .driver_info = DEVTYPE_EGALAX},
120 {USB_DEVICE(0x3823, 0x0002), .driver_info = DEVTYPE_EGALAX},
121 {USB_DEVICE(0x0123, 0x0001), .driver_info = DEVTYPE_EGALAX},
122 {USB_DEVICE(0x0eef, 0x0001), .driver_info = DEVTYPE_EGALAX},
123 {USB_DEVICE(0x0eef, 0x0002), .driver_info = DEVTYPE_EGALAX},
124 {USB_DEVICE(0x1234, 0x0001), .driver_info = DEVTYPE_EGALAX},
125 {USB_DEVICE(0x1234, 0x0002), .driver_info = DEVTYPE_EGALAX},
128 #ifdef CONFIG_TOUCHSCREEN_USB_PANJIT
129 {USB_DEVICE(0x134c, 0x0001), .driver_info = DEVTYPE_PANJIT},
130 {USB_DEVICE(0x134c, 0x0002), .driver_info = DEVTYPE_PANJIT},
131 {USB_DEVICE(0x134c, 0x0003), .driver_info = DEVTYPE_PANJIT},
132 {USB_DEVICE(0x134c, 0x0004), .driver_info = DEVTYPE_PANJIT},
135 #ifdef CONFIG_TOUCHSCREEN_USB_3M
136 {USB_DEVICE(0x0596, 0x0001), .driver_info = DEVTYPE_3M},
139 #ifdef CONFIG_TOUCHSCREEN_USB_ITM
140 {USB_DEVICE(0x0403, 0xf9e9), .driver_info = DEVTYPE_ITM},
143 #ifdef CONFIG_TOUCHSCREEN_USB_ETURBO
144 {USB_DEVICE(0x1234, 0x5678), .driver_info = DEVTYPE_ETURBO},
147 #ifdef CONFIG_TOUCHSCREEN_USB_GUNZE
148 {USB_DEVICE(0x0637, 0x0001), .driver_info = DEVTYPE_GUNZE},
151 #ifdef CONFIG_TOUCHSCREEN_USB_DMC_TSC10
152 {USB_DEVICE(0x0afa, 0x03e8), .driver_info = DEVTYPE_DMC_TSC10},
155 #ifdef CONFIG_TOUCHSCREEN_USB_IRTOUCH
156 {USB_DEVICE(0x595a, 0x0001), .driver_info = DEVTYPE_IRTOUCH},
157 {USB_DEVICE(0x6615, 0x0001), .driver_info = DEVTYPE_IRTOUCH},
164 /*****************************************************************************
168 #ifdef CONFIG_TOUCHSCREEN_USB_EGALAX
170 #define EGALAX_PKT_TYPE_MASK 0xFE
171 #define EGALAX_PKT_TYPE_REPT 0x80
172 #define EGALAX_PKT_TYPE_DIAG 0x0A
174 static int egalax_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
176 if ((pkt[0] & EGALAX_PKT_TYPE_MASK) != EGALAX_PKT_TYPE_REPT)
179 dev->x = ((pkt[3] & 0x0F) << 7) | (pkt[4] & 0x7F);
180 dev->y = ((pkt[1] & 0x0F) << 7) | (pkt[2] & 0x7F);
181 dev->touch = pkt[0] & 0x01;
186 static int egalax_get_pkt_len(unsigned char *buf, int len)
188 switch (buf[0] & EGALAX_PKT_TYPE_MASK) {
189 case EGALAX_PKT_TYPE_REPT:
192 case EGALAX_PKT_TYPE_DIAG:
204 /*****************************************************************************
207 #ifdef CONFIG_TOUCHSCREEN_USB_PANJIT
208 static int panjit_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
210 dev->x = ((pkt[2] & 0x0F) << 8) | pkt[1];
211 dev->y = ((pkt[4] & 0x0F) << 8) | pkt[3];
212 dev->touch = pkt[0] & 0x01;
219 /*****************************************************************************
222 #ifdef CONFIG_TOUCHSCREEN_USB_3M
224 #define MTOUCHUSB_ASYNC_REPORT 1
225 #define MTOUCHUSB_RESET 7
226 #define MTOUCHUSB_REQ_CTRLLR_ID 10
228 static int mtouch_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
230 dev->x = (pkt[8] << 8) | pkt[7];
231 dev->y = (pkt[10] << 8) | pkt[9];
232 dev->touch = (pkt[2] & 0x40) ? 1 : 0;
237 static int mtouch_init(struct usbtouch_usb *usbtouch)
241 ret = usb_control_msg(usbtouch->udev, usb_rcvctrlpipe(usbtouch->udev, 0),
243 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
244 1, 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
245 dbg("%s - usb_control_msg - MTOUCHUSB_RESET - bytes|err: %d",
251 for (i = 0; i < 3; i++) {
252 ret = usb_control_msg(usbtouch->udev, usb_rcvctrlpipe(usbtouch->udev, 0),
253 MTOUCHUSB_ASYNC_REPORT,
254 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
255 1, 1, NULL, 0, USB_CTRL_SET_TIMEOUT);
256 dbg("%s - usb_control_msg - MTOUCHUSB_ASYNC_REPORT - bytes|err: %d",
269 /*****************************************************************************
272 #ifdef CONFIG_TOUCHSCREEN_USB_ITM
273 static int itm_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
277 * ITM devices report invalid x/y data if not touched.
278 * if the screen was touched before but is not touched any more
279 * report touch as 0 with the last valid x/y data once. then stop
280 * reporting data until touched again.
282 dev->press = ((pkt[2] & 0x01) << 7) | (pkt[5] & 0x7F);
284 touch = ~pkt[7] & 0x20;
294 dev->x = ((pkt[0] & 0x1F) << 7) | (pkt[3] & 0x7F);
295 dev->y = ((pkt[1] & 0x1F) << 7) | (pkt[4] & 0x7F);
303 /*****************************************************************************
306 #ifdef CONFIG_TOUCHSCREEN_USB_ETURBO
307 static int eturbo_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
311 /* packets should start with sync */
312 if (!(pkt[0] & 0x80))
315 shift = (6 - (pkt[0] & 0x03));
316 dev->x = ((pkt[3] << 7) | pkt[4]) >> shift;
317 dev->y = ((pkt[1] << 7) | pkt[2]) >> shift;
318 dev->touch = (pkt[0] & 0x10) ? 1 : 0;
323 static int eturbo_get_pkt_len(unsigned char *buf, int len)
334 /*****************************************************************************
337 #ifdef CONFIG_TOUCHSCREEN_USB_GUNZE
338 static int gunze_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
340 if (!(pkt[0] & 0x80) || ((pkt[1] | pkt[2] | pkt[3]) & 0x80))
343 dev->x = ((pkt[0] & 0x1F) << 7) | (pkt[2] & 0x7F);
344 dev->y = ((pkt[1] & 0x1F) << 7) | (pkt[3] & 0x7F);
345 dev->touch = pkt[0] & 0x20;
351 /*****************************************************************************
354 * Documentation about the controller and it's protocol can be found at
355 * http://www.dmccoltd.com/files/controler/tsc10usb_pi_e.pdf
356 * http://www.dmccoltd.com/files/controler/tsc25_usb_e.pdf
358 #ifdef CONFIG_TOUCHSCREEN_USB_DMC_TSC10
360 /* supported data rates. currently using 130 */
361 #define TSC10_RATE_POINT 0x50
362 #define TSC10_RATE_30 0x40
363 #define TSC10_RATE_50 0x41
364 #define TSC10_RATE_80 0x42
365 #define TSC10_RATE_100 0x43
366 #define TSC10_RATE_130 0x44
367 #define TSC10_RATE_150 0x45
370 #define TSC10_CMD_RESET 0x55
371 #define TSC10_CMD_RATE 0x05
372 #define TSC10_CMD_DATA1 0x01
374 static int dmc_tsc10_init(struct usbtouch_usb *usbtouch)
376 struct usb_device *dev = usbtouch->udev;
378 unsigned char buf[2];
381 buf[0] = buf[1] = 0xFF;
382 ret = usb_control_msg(dev, usb_rcvctrlpipe (dev, 0),
384 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
385 0, 0, buf, 2, USB_CTRL_SET_TIMEOUT);
388 if (buf[0] != 0x06 || buf[1] != 0x00)
391 /* set coordinate output rate */
392 buf[0] = buf[1] = 0xFF;
393 ret = usb_control_msg(dev, usb_rcvctrlpipe (dev, 0),
395 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
396 TSC10_RATE_150, 0, buf, 2, USB_CTRL_SET_TIMEOUT);
399 if (buf[0] != 0x06 || buf[1] != 0x00)
402 /* start sending data */
403 ret = usb_control_msg(dev, usb_rcvctrlpipe (dev, 0),
405 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
406 0, 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
414 static int dmc_tsc10_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
416 dev->x = ((pkt[2] & 0x03) << 8) | pkt[1];
417 dev->y = ((pkt[4] & 0x03) << 8) | pkt[3];
418 dev->touch = pkt[0] & 0x01;
425 /*****************************************************************************
428 #ifdef CONFIG_TOUCHSCREEN_USB_IRTOUCH
429 static int irtouch_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
431 dev->x = (pkt[3] << 8) | pkt[2];
432 dev->y = (pkt[5] << 8) | pkt[4];
433 dev->touch = (pkt[1] & 0x03) ? 1 : 0;
440 /*****************************************************************************
441 * the different device descriptors
443 static struct usbtouch_device_info usbtouch_dev_info[] = {
444 #ifdef CONFIG_TOUCHSCREEN_USB_EGALAX
451 .flags = USBTOUCH_FLG_BUFFER,
452 .process_pkt = usbtouch_process_multi,
453 .get_pkt_len = egalax_get_pkt_len,
454 .read_data = egalax_read_data,
458 #ifdef CONFIG_TOUCHSCREEN_USB_PANJIT
465 .read_data = panjit_read_data,
469 #ifdef CONFIG_TOUCHSCREEN_USB_3M
476 .read_data = mtouch_read_data,
481 #ifdef CONFIG_TOUCHSCREEN_USB_ITM
489 .read_data = itm_read_data,
493 #ifdef CONFIG_TOUCHSCREEN_USB_ETURBO
500 .flags = USBTOUCH_FLG_BUFFER,
501 .process_pkt = usbtouch_process_multi,
502 .get_pkt_len = eturbo_get_pkt_len,
503 .read_data = eturbo_read_data,
507 #ifdef CONFIG_TOUCHSCREEN_USB_GUNZE
514 .read_data = gunze_read_data,
518 #ifdef CONFIG_TOUCHSCREEN_USB_DMC_TSC10
519 [DEVTYPE_DMC_TSC10] = {
525 .init = dmc_tsc10_init,
526 .read_data = dmc_tsc10_read_data,
530 #ifdef CONFIG_TOUCHSCREEN_USB_IRTOUCH
531 [DEVTYPE_IRTOUCH] = {
537 .read_data = irtouch_read_data,
543 /*****************************************************************************
546 static void usbtouch_process_pkt(struct usbtouch_usb *usbtouch,
547 unsigned char *pkt, int len)
549 struct usbtouch_device_info *type = usbtouch->type;
551 if (!type->read_data(usbtouch, pkt))
554 input_report_key(usbtouch->input, BTN_TOUCH, usbtouch->touch);
557 input_report_abs(usbtouch->input, ABS_X, usbtouch->y);
558 input_report_abs(usbtouch->input, ABS_Y, usbtouch->x);
560 input_report_abs(usbtouch->input, ABS_X, usbtouch->x);
561 input_report_abs(usbtouch->input, ABS_Y, usbtouch->y);
564 input_report_abs(usbtouch->input, ABS_PRESSURE, usbtouch->press);
565 input_sync(usbtouch->input);
570 static void usbtouch_process_multi(struct usbtouch_usb *usbtouch,
571 unsigned char *pkt, int len)
573 unsigned char *buffer;
574 int pkt_len, pos, buf_len, tmp;
577 if (unlikely(usbtouch->buf_len)) {
578 /* try to get size */
579 pkt_len = usbtouch->type->get_pkt_len(
580 usbtouch->buffer, usbtouch->buf_len);
583 if (unlikely(!pkt_len))
586 /* need to append -pkt_len bytes before able to get size */
587 if (unlikely(pkt_len < 0)) {
588 int append = -pkt_len;
589 if (unlikely(append > len))
591 if (usbtouch->buf_len + append >= usbtouch->type->rept_size)
593 memcpy(usbtouch->buffer + usbtouch->buf_len, pkt, append);
594 usbtouch->buf_len += append;
596 pkt_len = usbtouch->type->get_pkt_len(
597 usbtouch->buffer, usbtouch->buf_len);
603 tmp = pkt_len - usbtouch->buf_len;
604 if (usbtouch->buf_len + tmp >= usbtouch->type->rept_size)
606 memcpy(usbtouch->buffer + usbtouch->buf_len, pkt, tmp);
607 usbtouch_process_pkt(usbtouch, usbtouch->buffer, pkt_len);
616 /* loop over the received packet, process */
618 while (pos < buf_len) {
620 pkt_len = usbtouch->type->get_pkt_len(buffer + pos, len);
622 /* unknown packet: drop everything */
623 if (unlikely(!pkt_len))
626 /* full packet: process */
627 if (likely((pkt_len > 0) && (pkt_len <= buf_len - pos))) {
628 usbtouch_process_pkt(usbtouch, buffer + pos, pkt_len);
630 /* incomplete packet: save in buffer */
631 memcpy(usbtouch->buffer, buffer + pos, buf_len - pos);
632 usbtouch->buf_len = buf_len - pos;
639 usbtouch->buf_len = 0;
645 static void usbtouch_irq(struct urb *urb)
647 struct usbtouch_usb *usbtouch = urb->context;
650 switch (urb->status) {
655 /* this urb is timing out */
656 dbg("%s - urb timed out - was the device unplugged?",
662 /* this urb is terminated, clean up */
663 dbg("%s - urb shutting down with status: %d",
664 __FUNCTION__, urb->status);
667 dbg("%s - nonzero urb status received: %d",
668 __FUNCTION__, urb->status);
672 usbtouch->type->process_pkt(usbtouch, usbtouch->data, urb->actual_length);
675 retval = usb_submit_urb(urb, GFP_ATOMIC);
677 err("%s - usb_submit_urb failed with result: %d",
678 __FUNCTION__, retval);
681 static int usbtouch_open(struct input_dev *input)
683 struct usbtouch_usb *usbtouch = input_get_drvdata(input);
685 usbtouch->irq->dev = usbtouch->udev;
687 if (usb_submit_urb(usbtouch->irq, GFP_KERNEL))
693 static void usbtouch_close(struct input_dev *input)
695 struct usbtouch_usb *usbtouch = input_get_drvdata(input);
697 usb_kill_urb(usbtouch->irq);
701 static void usbtouch_free_buffers(struct usb_device *udev,
702 struct usbtouch_usb *usbtouch)
704 usb_buffer_free(udev, usbtouch->type->rept_size,
705 usbtouch->data, usbtouch->data_dma);
706 kfree(usbtouch->buffer);
710 static int usbtouch_probe(struct usb_interface *intf,
711 const struct usb_device_id *id)
713 struct usbtouch_usb *usbtouch;
714 struct input_dev *input_dev;
715 struct usb_host_interface *interface;
716 struct usb_endpoint_descriptor *endpoint;
717 struct usb_device *udev = interface_to_usbdev(intf);
718 struct usbtouch_device_info *type;
721 interface = intf->cur_altsetting;
722 endpoint = &interface->endpoint[0].desc;
724 usbtouch = kzalloc(sizeof(struct usbtouch_usb), GFP_KERNEL);
725 input_dev = input_allocate_device();
726 if (!usbtouch || !input_dev)
729 type = &usbtouch_dev_info[id->driver_info];
730 usbtouch->type = type;
731 if (!type->process_pkt)
732 type->process_pkt = usbtouch_process_pkt;
734 usbtouch->data = usb_buffer_alloc(udev, type->rept_size,
735 GFP_KERNEL, &usbtouch->data_dma);
739 if (type->flags & USBTOUCH_FLG_BUFFER) {
740 usbtouch->buffer = kmalloc(type->rept_size, GFP_KERNEL);
741 if (!usbtouch->buffer)
742 goto out_free_buffers;
745 usbtouch->irq = usb_alloc_urb(0, GFP_KERNEL);
746 if (!usbtouch->irq) {
747 dbg("%s - usb_alloc_urb failed: usbtouch->irq", __FUNCTION__);
748 goto out_free_buffers;
751 usbtouch->udev = udev;
752 usbtouch->input = input_dev;
754 if (udev->manufacturer)
755 strlcpy(usbtouch->name, udev->manufacturer, sizeof(usbtouch->name));
758 if (udev->manufacturer)
759 strlcat(usbtouch->name, " ", sizeof(usbtouch->name));
760 strlcat(usbtouch->name, udev->product, sizeof(usbtouch->name));
763 if (!strlen(usbtouch->name))
764 snprintf(usbtouch->name, sizeof(usbtouch->name),
765 "USB Touchscreen %04x:%04x",
766 le16_to_cpu(udev->descriptor.idVendor),
767 le16_to_cpu(udev->descriptor.idProduct));
769 usb_make_path(udev, usbtouch->phys, sizeof(usbtouch->phys));
770 strlcpy(usbtouch->phys, "/input0", sizeof(usbtouch->phys));
772 input_dev->name = usbtouch->name;
773 input_dev->phys = usbtouch->phys;
774 usb_to_input_id(udev, &input_dev->id);
775 input_dev->dev.parent = &intf->dev;
777 input_set_drvdata(input_dev, usbtouch);
779 input_dev->open = usbtouch_open;
780 input_dev->close = usbtouch_close;
782 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
783 input_dev->keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH);
784 input_set_abs_params(input_dev, ABS_X, type->min_xc, type->max_xc, 0, 0);
785 input_set_abs_params(input_dev, ABS_Y, type->min_yc, type->max_yc, 0, 0);
787 input_set_abs_params(input_dev, ABS_PRESSURE, type->min_press,
788 type->max_press, 0, 0);
790 usb_fill_int_urb(usbtouch->irq, usbtouch->udev,
791 usb_rcvintpipe(usbtouch->udev, endpoint->bEndpointAddress),
792 usbtouch->data, type->rept_size,
793 usbtouch_irq, usbtouch, endpoint->bInterval);
795 usbtouch->irq->dev = usbtouch->udev;
796 usbtouch->irq->transfer_dma = usbtouch->data_dma;
797 usbtouch->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
799 /* device specific init */
801 err = type->init(usbtouch);
803 dbg("%s - type->init() failed, err: %d", __FUNCTION__, err);
804 goto out_free_buffers;
808 err = input_register_device(usbtouch->input);
810 dbg("%s - input_register_device failed, err: %d", __FUNCTION__, err);
811 goto out_free_buffers;
814 usb_set_intfdata(intf, usbtouch);
819 usbtouch_free_buffers(udev, usbtouch);
821 input_free_device(input_dev);
826 static void usbtouch_disconnect(struct usb_interface *intf)
828 struct usbtouch_usb *usbtouch = usb_get_intfdata(intf);
830 dbg("%s - called", __FUNCTION__);
835 dbg("%s - usbtouch is initialized, cleaning up", __FUNCTION__);
836 usb_set_intfdata(intf, NULL);
837 usb_kill_urb(usbtouch->irq);
838 input_unregister_device(usbtouch->input);
839 usb_free_urb(usbtouch->irq);
840 usbtouch_free_buffers(interface_to_usbdev(intf), usbtouch);
844 MODULE_DEVICE_TABLE(usb, usbtouch_devices);
846 static struct usb_driver usbtouch_driver = {
847 .name = "usbtouchscreen",
848 .probe = usbtouch_probe,
849 .disconnect = usbtouch_disconnect,
850 .id_table = usbtouch_devices,
853 static int __init usbtouch_init(void)
855 return usb_register(&usbtouch_driver);
858 static void __exit usbtouch_cleanup(void)
860 usb_deregister(&usbtouch_driver);
863 module_init(usbtouch_init);
864 module_exit(usbtouch_cleanup);
866 MODULE_AUTHOR(DRIVER_AUTHOR);
867 MODULE_DESCRIPTION(DRIVER_DESC);
868 MODULE_LICENSE("GPL");
870 MODULE_ALIAS("touchkitusb");
871 MODULE_ALIAS("itmtouch");
872 MODULE_ALIAS("mtouchusb");