1 /******************************************************************************
3 * Driver for USB Touchscreens, supporting those devices:
5 * includes eTurboTouch CT-410/510/700
6 * - 3M/Microtouch EX II series
13 * Copyright (C) 2004-2006 by Daniel Ritz <daniel.ritz@gmx.ch>
14 * Copyright (C) by Todd E. Johnson (mtouchusb.c)
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License as
18 * published by the Free Software Foundation; either version 2 of the
19 * License, or (at your option) any later version.
21 * This program is distributed in the hope that it will be useful, but
22 * WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 * General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 * Driver is based on touchkitusb.c
31 * - ITM parts are from itmtouch.c
32 * - 3M parts are from mtouchusb.c
33 * - PanJit parts are from an unmerged driver by Lanslott Gish
34 * - DMC TSC 10/25 are from Holger Schurig, with ideas from an unmerged
35 * driver from Marius Vollmer
37 *****************************************************************************/
41 #include <linux/kernel.h>
42 #include <linux/slab.h>
43 #include <linux/input.h>
44 #include <linux/module.h>
45 #include <linux/init.h>
46 #include <linux/usb.h>
47 #include <linux/usb/input.h>
50 #define DRIVER_VERSION "v0.5"
51 #define DRIVER_AUTHOR "Daniel Ritz <daniel.ritz@gmx.ch>"
52 #define DRIVER_DESC "USB Touchscreen Driver"
55 module_param(swap_xy, bool, 0644);
56 MODULE_PARM_DESC(swap_xy, "If set X and Y axes are swapped.");
58 /* device specifc data/functions */
60 struct usbtouch_device_info {
63 int min_press, max_press;
67 void (*process_pkt) (struct usbtouch_usb *usbtouch, unsigned char *pkt, int len);
68 int (*get_pkt_len) (unsigned char *pkt, int len);
69 int (*read_data) (struct usbtouch_usb *usbtouch, unsigned char *pkt);
70 int (*init) (struct usbtouch_usb *usbtouch);
73 #define USBTOUCH_FLG_BUFFER 0x01
76 /* a usbtouch device */
80 unsigned char *buffer;
83 struct usb_device *udev;
84 struct input_dev *input;
85 struct usbtouch_device_info *type;
94 #if defined(CONFIG_USB_TOUCHSCREEN_EGALAX) || defined(CONFIG_USB_TOUCHSCREEN_ETURBO)
99 static void usbtouch_process_multi(struct usbtouch_usb *usbtouch,
100 unsigned char *pkt, int len);
115 static struct usb_device_id usbtouch_devices[] = {
116 #ifdef CONFIG_USB_TOUCHSCREEN_EGALAX
117 {USB_DEVICE(0x3823, 0x0001), .driver_info = DEVTYPE_EGALAX},
118 {USB_DEVICE(0x3823, 0x0002), .driver_info = DEVTYPE_EGALAX},
119 {USB_DEVICE(0x0123, 0x0001), .driver_info = DEVTYPE_EGALAX},
120 {USB_DEVICE(0x0eef, 0x0001), .driver_info = DEVTYPE_EGALAX},
121 {USB_DEVICE(0x0eef, 0x0002), .driver_info = DEVTYPE_EGALAX},
122 {USB_DEVICE(0x1234, 0x0001), .driver_info = DEVTYPE_EGALAX},
123 {USB_DEVICE(0x1234, 0x0002), .driver_info = DEVTYPE_EGALAX},
126 #ifdef CONFIG_USB_TOUCHSCREEN_PANJIT
127 {USB_DEVICE(0x134c, 0x0001), .driver_info = DEVTYPE_PANJIT},
128 {USB_DEVICE(0x134c, 0x0002), .driver_info = DEVTYPE_PANJIT},
129 {USB_DEVICE(0x134c, 0x0003), .driver_info = DEVTYPE_PANJIT},
130 {USB_DEVICE(0x134c, 0x0004), .driver_info = DEVTYPE_PANJIT},
133 #ifdef CONFIG_USB_TOUCHSCREEN_3M
134 {USB_DEVICE(0x0596, 0x0001), .driver_info = DEVTYPE_3M},
137 #ifdef CONFIG_USB_TOUCHSCREEN_ITM
138 {USB_DEVICE(0x0403, 0xf9e9), .driver_info = DEVTYPE_ITM},
141 #ifdef CONFIG_USB_TOUCHSCREEN_ETURBO
142 {USB_DEVICE(0x1234, 0x5678), .driver_info = DEVTYPE_ETURBO},
145 #ifdef CONFIG_USB_TOUCHSCREEN_GUNZE
146 {USB_DEVICE(0x0637, 0x0001), .driver_info = DEVTYPE_GUNZE},
149 #ifdef CONFIG_USB_TOUCHSCREEN_DMC_TSC10
150 {USB_DEVICE(0x0afa, 0x03e8), .driver_info = DEVTYPE_DMC_TSC10},
157 /*****************************************************************************
161 #ifdef CONFIG_USB_TOUCHSCREEN_EGALAX
163 #define EGALAX_PKT_TYPE_MASK 0xFE
164 #define EGALAX_PKT_TYPE_REPT 0x80
165 #define EGALAX_PKT_TYPE_DIAG 0x0A
167 static int egalax_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
169 if ((pkt[0] & EGALAX_PKT_TYPE_MASK) != EGALAX_PKT_TYPE_REPT)
172 dev->x = ((pkt[3] & 0x0F) << 7) | (pkt[4] & 0x7F);
173 dev->y = ((pkt[1] & 0x0F) << 7) | (pkt[2] & 0x7F);
174 dev->touch = pkt[0] & 0x01;
179 static int egalax_get_pkt_len(unsigned char *buf, int len)
181 switch (buf[0] & EGALAX_PKT_TYPE_MASK) {
182 case EGALAX_PKT_TYPE_REPT:
185 case EGALAX_PKT_TYPE_DIAG:
197 /*****************************************************************************
200 #ifdef CONFIG_USB_TOUCHSCREEN_PANJIT
201 static int panjit_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
203 dev->x = ((pkt[2] & 0x0F) << 8) | pkt[1];
204 dev->y = ((pkt[4] & 0x0F) << 8) | pkt[3];
205 dev->touch = pkt[0] & 0x01;
212 /*****************************************************************************
215 #ifdef CONFIG_USB_TOUCHSCREEN_3M
217 #define MTOUCHUSB_ASYNC_REPORT 1
218 #define MTOUCHUSB_RESET 7
219 #define MTOUCHUSB_REQ_CTRLLR_ID 10
221 static int mtouch_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
223 dev->x = (pkt[8] << 8) | pkt[7];
224 dev->y = (pkt[10] << 8) | pkt[9];
225 dev->touch = (pkt[2] & 0x40) ? 1 : 0;
230 static int mtouch_init(struct usbtouch_usb *usbtouch)
234 ret = usb_control_msg(usbtouch->udev, usb_rcvctrlpipe(usbtouch->udev, 0),
236 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
237 1, 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
238 dbg("%s - usb_control_msg - MTOUCHUSB_RESET - bytes|err: %d",
244 for (i = 0; i < 3; i++) {
245 ret = usb_control_msg(usbtouch->udev, usb_rcvctrlpipe(usbtouch->udev, 0),
246 MTOUCHUSB_ASYNC_REPORT,
247 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
248 1, 1, NULL, 0, USB_CTRL_SET_TIMEOUT);
249 dbg("%s - usb_control_msg - MTOUCHUSB_ASYNC_REPORT - bytes|err: %d",
262 /*****************************************************************************
265 #ifdef CONFIG_USB_TOUCHSCREEN_ITM
266 static int itm_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
270 * ITM devices report invalid x/y data if not touched.
271 * if the screen was touched before but is not touched any more
272 * report touch as 0 with the last valid x/y data once. then stop
273 * reporting data until touched again.
275 dev->press = ((pkt[2] & 0x01) << 7) | (pkt[5] & 0x7F);
277 touch = ~pkt[7] & 0x20;
287 dev->x = ((pkt[0] & 0x1F) << 7) | (pkt[3] & 0x7F);
288 dev->y = ((pkt[1] & 0x1F) << 7) | (pkt[4] & 0x7F);
296 /*****************************************************************************
299 #ifdef CONFIG_USB_TOUCHSCREEN_ETURBO
300 static int eturbo_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
304 /* packets should start with sync */
305 if (!(pkt[0] & 0x80))
308 shift = (6 - (pkt[0] & 0x03));
309 dev->x = ((pkt[3] << 7) | pkt[4]) >> shift;
310 dev->y = ((pkt[1] << 7) | pkt[2]) >> shift;
311 dev->touch = (pkt[0] & 0x10) ? 1 : 0;
316 static int eturbo_get_pkt_len(unsigned char *buf, int len)
327 /*****************************************************************************
330 #ifdef CONFIG_USB_TOUCHSCREEN_GUNZE
331 static int gunze_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
333 if (!(pkt[0] & 0x80) || ((pkt[1] | pkt[2] | pkt[3]) & 0x80))
336 dev->x = ((pkt[0] & 0x1F) << 7) | (pkt[2] & 0x7F);
337 dev->y = ((pkt[1] & 0x1F) << 7) | (pkt[3] & 0x7F);
338 dev->touch = pkt[0] & 0x20;
344 /*****************************************************************************
347 * Documentation about the controller and it's protocol can be found at
348 * http://www.dmccoltd.com/files/controler/tsc10usb_pi_e.pdf
349 * http://www.dmccoltd.com/files/controler/tsc25_usb_e.pdf
351 #ifdef CONFIG_USB_TOUCHSCREEN_DMC_TSC10
353 /* supported data rates. currently using 130 */
354 #define TSC10_RATE_POINT 0x50
355 #define TSC10_RATE_30 0x40
356 #define TSC10_RATE_50 0x41
357 #define TSC10_RATE_80 0x42
358 #define TSC10_RATE_100 0x43
359 #define TSC10_RATE_130 0x44
360 #define TSC10_RATE_150 0x45
363 #define TSC10_CMD_RESET 0x55
364 #define TSC10_CMD_RATE 0x05
365 #define TSC10_CMD_DATA1 0x01
367 static int dmc_tsc10_init(struct usbtouch_usb *usbtouch)
369 struct usb_device *dev = usbtouch->udev;
371 unsigned char buf[2];
374 buf[0] = buf[1] = 0xFF;
375 ret = usb_control_msg(dev, usb_rcvctrlpipe (dev, 0),
377 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
378 0, 0, buf, 2, USB_CTRL_SET_TIMEOUT);
381 if (buf[0] != 0x06 || buf[1] != 0x00)
384 /* set coordinate output rate */
385 buf[0] = buf[1] = 0xFF;
386 ret = usb_control_msg(dev, usb_rcvctrlpipe (dev, 0),
388 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
389 TSC10_RATE_150, 0, buf, 2, USB_CTRL_SET_TIMEOUT);
392 if (buf[0] != 0x06 || buf[1] != 0x00)
395 /* start sending data */
396 ret = usb_control_msg(dev, usb_rcvctrlpipe (dev, 0),
398 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
399 0, 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
407 static int dmc_tsc10_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
409 dev->x = ((pkt[2] & 0x03) << 8) | pkt[1];
410 dev->y = ((pkt[4] & 0x03) << 8) | pkt[3];
411 dev->touch = pkt[0] & 0x01;
418 /*****************************************************************************
419 * the different device descriptors
421 static struct usbtouch_device_info usbtouch_dev_info[] = {
422 #ifdef CONFIG_USB_TOUCHSCREEN_EGALAX
429 .flags = USBTOUCH_FLG_BUFFER,
430 .process_pkt = usbtouch_process_multi,
431 .get_pkt_len = egalax_get_pkt_len,
432 .read_data = egalax_read_data,
436 #ifdef CONFIG_USB_TOUCHSCREEN_PANJIT
443 .read_data = panjit_read_data,
447 #ifdef CONFIG_USB_TOUCHSCREEN_3M
454 .read_data = mtouch_read_data,
459 #ifdef CONFIG_USB_TOUCHSCREEN_ITM
467 .read_data = itm_read_data,
471 #ifdef CONFIG_USB_TOUCHSCREEN_ETURBO
478 .flags = USBTOUCH_FLG_BUFFER,
479 .process_pkt = usbtouch_process_multi,
480 .get_pkt_len = eturbo_get_pkt_len,
481 .read_data = eturbo_read_data,
485 #ifdef CONFIG_USB_TOUCHSCREEN_GUNZE
492 .read_data = gunze_read_data,
496 #ifdef CONFIG_USB_TOUCHSCREEN_DMC_TSC10
497 [DEVTYPE_DMC_TSC10] = {
503 .init = dmc_tsc10_init,
504 .read_data = dmc_tsc10_read_data,
510 /*****************************************************************************
513 static void usbtouch_process_pkt(struct usbtouch_usb *usbtouch,
514 unsigned char *pkt, int len)
516 struct usbtouch_device_info *type = usbtouch->type;
518 if (!type->read_data(usbtouch, pkt))
521 input_report_key(usbtouch->input, BTN_TOUCH, usbtouch->touch);
524 input_report_abs(usbtouch->input, ABS_X, usbtouch->y);
525 input_report_abs(usbtouch->input, ABS_Y, usbtouch->x);
527 input_report_abs(usbtouch->input, ABS_X, usbtouch->x);
528 input_report_abs(usbtouch->input, ABS_Y, usbtouch->y);
531 input_report_abs(usbtouch->input, ABS_PRESSURE, usbtouch->press);
532 input_sync(usbtouch->input);
537 static void usbtouch_process_multi(struct usbtouch_usb *usbtouch,
538 unsigned char *pkt, int len)
540 unsigned char *buffer;
541 int pkt_len, pos, buf_len, tmp;
544 if (unlikely(usbtouch->buf_len)) {
545 /* try to get size */
546 pkt_len = usbtouch->type->get_pkt_len(
547 usbtouch->buffer, usbtouch->buf_len);
550 if (unlikely(!pkt_len))
553 /* need to append -pkt_len bytes before able to get size */
554 if (unlikely(pkt_len < 0)) {
555 int append = -pkt_len;
556 if (unlikely(append > len))
558 if (usbtouch->buf_len + append >= usbtouch->type->rept_size)
560 memcpy(usbtouch->buffer + usbtouch->buf_len, pkt, append);
561 usbtouch->buf_len += append;
563 pkt_len = usbtouch->type->get_pkt_len(
564 usbtouch->buffer, usbtouch->buf_len);
570 tmp = pkt_len - usbtouch->buf_len;
571 if (usbtouch->buf_len + tmp >= usbtouch->type->rept_size)
573 memcpy(usbtouch->buffer + usbtouch->buf_len, pkt, tmp);
574 usbtouch_process_pkt(usbtouch, usbtouch->buffer, pkt_len);
583 /* loop over the received packet, process */
585 while (pos < buf_len) {
587 pkt_len = usbtouch->type->get_pkt_len(buffer + pos, len);
589 /* unknown packet: drop everything */
590 if (unlikely(!pkt_len))
593 /* full packet: process */
594 if (likely((pkt_len > 0) && (pkt_len <= buf_len - pos))) {
595 usbtouch_process_pkt(usbtouch, buffer + pos, pkt_len);
597 /* incomplete packet: save in buffer */
598 memcpy(usbtouch->buffer, buffer + pos, buf_len - pos);
599 usbtouch->buf_len = buf_len - pos;
606 usbtouch->buf_len = 0;
612 static void usbtouch_irq(struct urb *urb)
614 struct usbtouch_usb *usbtouch = urb->context;
617 switch (urb->status) {
622 /* this urb is timing out */
623 dbg("%s - urb timed out - was the device unplugged?",
629 /* this urb is terminated, clean up */
630 dbg("%s - urb shutting down with status: %d",
631 __FUNCTION__, urb->status);
634 dbg("%s - nonzero urb status received: %d",
635 __FUNCTION__, urb->status);
639 usbtouch->type->process_pkt(usbtouch, usbtouch->data, urb->actual_length);
642 retval = usb_submit_urb(urb, GFP_ATOMIC);
644 err("%s - usb_submit_urb failed with result: %d",
645 __FUNCTION__, retval);
648 static int usbtouch_open(struct input_dev *input)
650 struct usbtouch_usb *usbtouch = input->private;
652 usbtouch->irq->dev = usbtouch->udev;
654 if (usb_submit_urb(usbtouch->irq, GFP_KERNEL))
660 static void usbtouch_close(struct input_dev *input)
662 struct usbtouch_usb *usbtouch = input->private;
664 usb_kill_urb(usbtouch->irq);
668 static void usbtouch_free_buffers(struct usb_device *udev,
669 struct usbtouch_usb *usbtouch)
672 usb_buffer_free(udev, usbtouch->type->rept_size,
673 usbtouch->data, usbtouch->data_dma);
674 kfree(usbtouch->buffer);
678 static int usbtouch_probe(struct usb_interface *intf,
679 const struct usb_device_id *id)
681 struct usbtouch_usb *usbtouch;
682 struct input_dev *input_dev;
683 struct usb_host_interface *interface;
684 struct usb_endpoint_descriptor *endpoint;
685 struct usb_device *udev = interface_to_usbdev(intf);
686 struct usbtouch_device_info *type;
689 interface = intf->cur_altsetting;
690 endpoint = &interface->endpoint[0].desc;
692 usbtouch = kzalloc(sizeof(struct usbtouch_usb), GFP_KERNEL);
693 input_dev = input_allocate_device();
694 if (!usbtouch || !input_dev)
697 type = &usbtouch_dev_info[id->driver_info];
698 usbtouch->type = type;
699 if (!type->process_pkt)
700 type->process_pkt = usbtouch_process_pkt;
702 usbtouch->data = usb_buffer_alloc(udev, type->rept_size,
703 GFP_KERNEL, &usbtouch->data_dma);
707 if (type->flags & USBTOUCH_FLG_BUFFER) {
708 usbtouch->buffer = kmalloc(type->rept_size, GFP_KERNEL);
709 if (!usbtouch->buffer)
710 goto out_free_buffers;
713 usbtouch->irq = usb_alloc_urb(0, GFP_KERNEL);
714 if (!usbtouch->irq) {
715 dbg("%s - usb_alloc_urb failed: usbtouch->irq", __FUNCTION__);
716 goto out_free_buffers;
719 usbtouch->udev = udev;
720 usbtouch->input = input_dev;
722 if (udev->manufacturer)
723 strlcpy(usbtouch->name, udev->manufacturer, sizeof(usbtouch->name));
726 if (udev->manufacturer)
727 strlcat(usbtouch->name, " ", sizeof(usbtouch->name));
728 strlcat(usbtouch->name, udev->product, sizeof(usbtouch->name));
731 if (!strlen(usbtouch->name))
732 snprintf(usbtouch->name, sizeof(usbtouch->name),
733 "USB Touchscreen %04x:%04x",
734 le16_to_cpu(udev->descriptor.idVendor),
735 le16_to_cpu(udev->descriptor.idProduct));
737 usb_make_path(udev, usbtouch->phys, sizeof(usbtouch->phys));
738 strlcpy(usbtouch->phys, "/input0", sizeof(usbtouch->phys));
740 input_dev->name = usbtouch->name;
741 input_dev->phys = usbtouch->phys;
742 usb_to_input_id(udev, &input_dev->id);
743 input_dev->cdev.dev = &intf->dev;
744 input_dev->private = usbtouch;
745 input_dev->open = usbtouch_open;
746 input_dev->close = usbtouch_close;
748 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
749 input_dev->keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH);
750 input_set_abs_params(input_dev, ABS_X, type->min_xc, type->max_xc, 0, 0);
751 input_set_abs_params(input_dev, ABS_Y, type->min_yc, type->max_yc, 0, 0);
753 input_set_abs_params(input_dev, ABS_PRESSURE, type->min_press,
754 type->max_press, 0, 0);
756 usb_fill_int_urb(usbtouch->irq, usbtouch->udev,
757 usb_rcvintpipe(usbtouch->udev, endpoint->bEndpointAddress),
758 usbtouch->data, type->rept_size,
759 usbtouch_irq, usbtouch, endpoint->bInterval);
761 usbtouch->irq->dev = usbtouch->udev;
762 usbtouch->irq->transfer_dma = usbtouch->data_dma;
763 usbtouch->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
765 /* device specific init */
767 err = type->init(usbtouch);
769 dbg("%s - type->init() failed, err: %d", __FUNCTION__, err);
770 goto out_free_buffers;
774 err = input_register_device(usbtouch->input);
776 dbg("%s - input_register_device failed, err: %d", __FUNCTION__, err);
777 goto out_free_buffers;
780 usb_set_intfdata(intf, usbtouch);
785 usbtouch_free_buffers(udev, usbtouch);
787 input_free_device(input_dev);
792 static void usbtouch_disconnect(struct usb_interface *intf)
794 struct usbtouch_usb *usbtouch = usb_get_intfdata(intf);
796 dbg("%s - called", __FUNCTION__);
801 dbg("%s - usbtouch is initialized, cleaning up", __FUNCTION__);
802 usb_set_intfdata(intf, NULL);
803 usb_kill_urb(usbtouch->irq);
804 input_unregister_device(usbtouch->input);
805 usb_free_urb(usbtouch->irq);
806 usbtouch_free_buffers(interface_to_usbdev(intf), usbtouch);
810 MODULE_DEVICE_TABLE(usb, usbtouch_devices);
812 static struct usb_driver usbtouch_driver = {
813 .name = "usbtouchscreen",
814 .probe = usbtouch_probe,
815 .disconnect = usbtouch_disconnect,
816 .id_table = usbtouch_devices,
819 static int __init usbtouch_init(void)
821 return usb_register(&usbtouch_driver);
824 static void __exit usbtouch_cleanup(void)
826 usb_deregister(&usbtouch_driver);
829 module_init(usbtouch_init);
830 module_exit(usbtouch_cleanup);
832 MODULE_AUTHOR(DRIVER_AUTHOR);
833 MODULE_DESCRIPTION(DRIVER_DESC);
834 MODULE_LICENSE("GPL");
836 MODULE_ALIAS("touchkitusb");
837 MODULE_ALIAS("itmtouch");
838 MODULE_ALIAS("mtouchusb");