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, 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,
94 unsigned char *pkt, int len);
108 static struct usb_device_id usbtouch_devices[] = {
109 #ifdef CONFIG_USB_TOUCHSCREEN_EGALAX
110 {USB_DEVICE(0x3823, 0x0001), .driver_info = DEVTYPE_EGALAX},
111 {USB_DEVICE(0x3823, 0x0002), .driver_info = DEVTYPE_EGALAX},
112 {USB_DEVICE(0x0123, 0x0001), .driver_info = DEVTYPE_EGALAX},
113 {USB_DEVICE(0x0eef, 0x0001), .driver_info = DEVTYPE_EGALAX},
114 {USB_DEVICE(0x0eef, 0x0002), .driver_info = DEVTYPE_EGALAX},
115 {USB_DEVICE(0x1234, 0x0001), .driver_info = DEVTYPE_EGALAX},
116 {USB_DEVICE(0x1234, 0x0002), .driver_info = DEVTYPE_EGALAX},
119 #ifdef CONFIG_USB_TOUCHSCREEN_PANJIT
120 {USB_DEVICE(0x134c, 0x0001), .driver_info = DEVTYPE_PANJIT},
121 {USB_DEVICE(0x134c, 0x0002), .driver_info = DEVTYPE_PANJIT},
122 {USB_DEVICE(0x134c, 0x0003), .driver_info = DEVTYPE_PANJIT},
123 {USB_DEVICE(0x134c, 0x0004), .driver_info = DEVTYPE_PANJIT},
126 #ifdef CONFIG_USB_TOUCHSCREEN_3M
127 {USB_DEVICE(0x0596, 0x0001), .driver_info = DEVTYPE_3M},
130 #ifdef CONFIG_USB_TOUCHSCREEN_ITM
131 {USB_DEVICE(0x0403, 0xf9e9), .driver_info = DEVTYPE_ITM},
134 #ifdef CONFIG_USB_TOUCHSCREEN_ETURBO
135 {USB_DEVICE(0x1234, 0x5678), .driver_info = DEVTYPE_ETURBO},
138 #ifdef CONFIG_USB_TOUCHSCREEN_GUNZE
139 {USB_DEVICE(0x0637, 0x0001), .driver_info = DEVTYPE_GUNZE},
146 /*****************************************************************************
150 #ifdef CONFIG_USB_TOUCHSCREEN_EGALAX
152 #define EGALAX_PKT_TYPE_MASK 0xFE
153 #define EGALAX_PKT_TYPE_REPT 0x80
154 #define EGALAX_PKT_TYPE_DIAG 0x0A
156 static int egalax_read_data(unsigned char *pkt, int *x, int *y, int *touch, int *press)
158 if ((pkt[0] & EGALAX_PKT_TYPE_MASK) != EGALAX_PKT_TYPE_REPT)
161 *x = ((pkt[3] & 0x0F) << 7) | (pkt[4] & 0x7F);
162 *y = ((pkt[1] & 0x0F) << 7) | (pkt[2] & 0x7F);
163 *touch = pkt[0] & 0x01;
168 static int egalax_get_pkt_len(unsigned char *buf, int len)
170 switch (buf[0] & EGALAX_PKT_TYPE_MASK) {
171 case EGALAX_PKT_TYPE_REPT:
174 case EGALAX_PKT_TYPE_DIAG:
186 /*****************************************************************************
189 #ifdef CONFIG_USB_TOUCHSCREEN_PANJIT
190 static int panjit_read_data(unsigned char *pkt, int *x, int *y, int *touch, int *press)
192 *x = ((pkt[2] & 0x0F) << 8) | pkt[1];
193 *y = ((pkt[4] & 0x0F) << 8) | pkt[3];
194 *touch = pkt[0] & 0x01;
201 /*****************************************************************************
204 #ifdef CONFIG_USB_TOUCHSCREEN_3M
206 #define MTOUCHUSB_ASYNC_REPORT 1
207 #define MTOUCHUSB_RESET 7
208 #define MTOUCHUSB_REQ_CTRLLR_ID 10
210 static int mtouch_read_data(unsigned char *pkt, int *x, int *y, int *touch, int *press)
212 *x = (pkt[8] << 8) | pkt[7];
213 *y = (pkt[10] << 8) | pkt[9];
214 *touch = (pkt[2] & 0x40) ? 1 : 0;
219 static int mtouch_init(struct usbtouch_usb *usbtouch)
223 ret = usb_control_msg(usbtouch->udev, usb_rcvctrlpipe(usbtouch->udev, 0),
225 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
226 1, 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
227 dbg("%s - usb_control_msg - MTOUCHUSB_RESET - bytes|err: %d",
233 for (i = 0; i < 3; i++) {
234 ret = usb_control_msg(usbtouch->udev, usb_rcvctrlpipe(usbtouch->udev, 0),
235 MTOUCHUSB_ASYNC_REPORT,
236 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
237 1, 1, NULL, 0, USB_CTRL_SET_TIMEOUT);
238 dbg("%s - usb_control_msg - MTOUCHUSB_ASYNC_REPORT - bytes|err: %d",
251 /*****************************************************************************
254 #ifdef CONFIG_USB_TOUCHSCREEN_ITM
255 static int itm_read_data(unsigned char *pkt, int *x, int *y, int *touch, int *press)
257 *x = ((pkt[0] & 0x1F) << 7) | (pkt[3] & 0x7F);
258 *y = ((pkt[1] & 0x1F) << 7) | (pkt[4] & 0x7F);
259 *press = ((pkt[2] & 0x01) << 7) | (pkt[5] & 0x7F);
260 *touch = ~pkt[7] & 0x20;
267 /*****************************************************************************
270 #ifdef CONFIG_USB_TOUCHSCREEN_ETURBO
271 static int eturbo_read_data(unsigned char *pkt, int *x, int *y, int *touch, int *press)
275 /* packets should start with sync */
276 if (!(pkt[0] & 0x80))
279 shift = (6 - (pkt[0] & 0x03));
280 *x = ((pkt[3] << 7) | pkt[4]) >> shift;
281 *y = ((pkt[1] << 7) | pkt[2]) >> shift;
282 *touch = (pkt[0] & 0x10) ? 1 : 0;
287 static int eturbo_get_pkt_len(unsigned char *buf, int len)
298 /*****************************************************************************
301 #ifdef CONFIG_USB_TOUCHSCREEN_GUNZE
302 static int gunze_read_data(unsigned char *pkt, int *x, int *y, int *touch, int *press)
304 if (!(pkt[0] & 0x80) || ((pkt[1] | pkt[2] | pkt[3]) & 0x80))
307 *x = ((pkt[0] & 0x1F) << 7) | (pkt[2] & 0x7F);
308 *y = ((pkt[1] & 0x1F) << 7) | (pkt[3] & 0x7F);
309 *touch = pkt[0] & 0x20;
315 /*****************************************************************************
316 * the different device descriptors
318 static struct usbtouch_device_info usbtouch_dev_info[] = {
319 #ifdef CONFIG_USB_TOUCHSCREEN_EGALAX
326 .flags = USBTOUCH_FLG_BUFFER,
327 .process_pkt = usbtouch_process_multi,
328 .get_pkt_len = egalax_get_pkt_len,
329 .read_data = egalax_read_data,
333 #ifdef CONFIG_USB_TOUCHSCREEN_PANJIT
340 .read_data = panjit_read_data,
344 #ifdef CONFIG_USB_TOUCHSCREEN_3M
351 .read_data = mtouch_read_data,
356 #ifdef CONFIG_USB_TOUCHSCREEN_ITM
364 .read_data = itm_read_data,
368 #ifdef CONFIG_USB_TOUCHSCREEN_ETURBO
375 .flags = USBTOUCH_FLG_BUFFER,
376 .process_pkt = usbtouch_process_multi,
377 .get_pkt_len = eturbo_get_pkt_len,
378 .read_data = eturbo_read_data,
382 #ifdef CONFIG_USB_TOUCHSCREEN_GUNZE
389 .read_data = gunze_read_data,
395 /*****************************************************************************
398 static void usbtouch_process_pkt(struct usbtouch_usb *usbtouch,
399 unsigned char *pkt, int len)
401 int x, y, touch, press;
402 struct usbtouch_device_info *type = usbtouch->type;
404 if (!type->read_data(pkt, &x, &y, &touch, &press))
407 input_report_key(usbtouch->input, BTN_TOUCH, touch);
410 input_report_abs(usbtouch->input, ABS_X, y);
411 input_report_abs(usbtouch->input, ABS_Y, x);
413 input_report_abs(usbtouch->input, ABS_X, x);
414 input_report_abs(usbtouch->input, ABS_Y, y);
417 input_report_abs(usbtouch->input, ABS_PRESSURE, press);
418 input_sync(usbtouch->input);
423 static void usbtouch_process_multi(struct usbtouch_usb *usbtouch,
424 unsigned char *pkt, int len)
426 unsigned char *buffer;
427 int pkt_len, pos, buf_len, tmp;
430 if (unlikely(usbtouch->buf_len)) {
431 /* try to get size */
432 pkt_len = usbtouch->type->get_pkt_len(
433 usbtouch->buffer, usbtouch->buf_len);
436 if (unlikely(!pkt_len))
439 /* need to append -pkt_len bytes before able to get size */
440 if (unlikely(pkt_len < 0)) {
441 int append = -pkt_len;
442 if (unlikely(append > len))
444 if (usbtouch->buf_len + append >= usbtouch->type->rept_size)
446 memcpy(usbtouch->buffer + usbtouch->buf_len, pkt, append);
447 usbtouch->buf_len += append;
449 pkt_len = usbtouch->type->get_pkt_len(
450 usbtouch->buffer, usbtouch->buf_len);
456 tmp = pkt_len - usbtouch->buf_len;
457 if (usbtouch->buf_len + tmp >= usbtouch->type->rept_size)
459 memcpy(usbtouch->buffer + usbtouch->buf_len, pkt, tmp);
460 usbtouch_process_pkt(usbtouch, usbtouch->buffer, pkt_len);
469 /* loop over the received packet, process */
471 while (pos < buf_len) {
473 pkt_len = usbtouch->type->get_pkt_len(buffer + pos, len);
475 /* unknown packet: drop everything */
476 if (unlikely(!pkt_len))
479 /* full packet: process */
480 if (likely((pkt_len > 0) && (pkt_len <= buf_len - pos))) {
481 usbtouch_process_pkt(usbtouch, buffer + pos, pkt_len);
483 /* incomplete packet: save in buffer */
484 memcpy(usbtouch->buffer, buffer + pos, buf_len - pos);
485 usbtouch->buf_len = buf_len - pos;
492 usbtouch->buf_len = 0;
498 static void usbtouch_irq(struct urb *urb)
500 struct usbtouch_usb *usbtouch = urb->context;
503 switch (urb->status) {
508 /* this urb is timing out */
509 dbg("%s - urb timed out - was the device unplugged?",
515 /* this urb is terminated, clean up */
516 dbg("%s - urb shutting down with status: %d",
517 __FUNCTION__, urb->status);
520 dbg("%s - nonzero urb status received: %d",
521 __FUNCTION__, urb->status);
525 usbtouch->type->process_pkt(usbtouch, usbtouch->data, urb->actual_length);
528 retval = usb_submit_urb(urb, GFP_ATOMIC);
530 err("%s - usb_submit_urb failed with result: %d",
531 __FUNCTION__, retval);
534 static int usbtouch_open(struct input_dev *input)
536 struct usbtouch_usb *usbtouch = input->private;
538 usbtouch->irq->dev = usbtouch->udev;
540 if (usb_submit_urb(usbtouch->irq, GFP_KERNEL))
546 static void usbtouch_close(struct input_dev *input)
548 struct usbtouch_usb *usbtouch = input->private;
550 usb_kill_urb(usbtouch->irq);
554 static void usbtouch_free_buffers(struct usb_device *udev,
555 struct usbtouch_usb *usbtouch)
558 usb_buffer_free(udev, usbtouch->type->rept_size,
559 usbtouch->data, usbtouch->data_dma);
560 kfree(usbtouch->buffer);
564 static int usbtouch_probe(struct usb_interface *intf,
565 const struct usb_device_id *id)
567 struct usbtouch_usb *usbtouch;
568 struct input_dev *input_dev;
569 struct usb_host_interface *interface;
570 struct usb_endpoint_descriptor *endpoint;
571 struct usb_device *udev = interface_to_usbdev(intf);
572 struct usbtouch_device_info *type;
575 interface = intf->cur_altsetting;
576 endpoint = &interface->endpoint[0].desc;
578 usbtouch = kzalloc(sizeof(struct usbtouch_usb), GFP_KERNEL);
579 input_dev = input_allocate_device();
580 if (!usbtouch || !input_dev)
583 type = &usbtouch_dev_info[id->driver_info];
584 usbtouch->type = type;
585 if (!type->process_pkt)
586 type->process_pkt = usbtouch_process_pkt;
588 usbtouch->data = usb_buffer_alloc(udev, type->rept_size,
589 SLAB_KERNEL, &usbtouch->data_dma);
593 if (type->flags & USBTOUCH_FLG_BUFFER) {
594 usbtouch->buffer = kmalloc(type->rept_size, GFP_KERNEL);
595 if (!usbtouch->buffer)
596 goto out_free_buffers;
599 usbtouch->irq = usb_alloc_urb(0, GFP_KERNEL);
600 if (!usbtouch->irq) {
601 dbg("%s - usb_alloc_urb failed: usbtouch->irq", __FUNCTION__);
602 goto out_free_buffers;
605 usbtouch->udev = udev;
606 usbtouch->input = input_dev;
608 if (udev->manufacturer)
609 strlcpy(usbtouch->name, udev->manufacturer, sizeof(usbtouch->name));
612 if (udev->manufacturer)
613 strlcat(usbtouch->name, " ", sizeof(usbtouch->name));
614 strlcat(usbtouch->name, udev->product, sizeof(usbtouch->name));
617 if (!strlen(usbtouch->name))
618 snprintf(usbtouch->name, sizeof(usbtouch->name),
619 "USB Touchscreen %04x:%04x",
620 le16_to_cpu(udev->descriptor.idVendor),
621 le16_to_cpu(udev->descriptor.idProduct));
623 usb_make_path(udev, usbtouch->phys, sizeof(usbtouch->phys));
624 strlcpy(usbtouch->phys, "/input0", sizeof(usbtouch->phys));
626 input_dev->name = usbtouch->name;
627 input_dev->phys = usbtouch->phys;
628 usb_to_input_id(udev, &input_dev->id);
629 input_dev->cdev.dev = &intf->dev;
630 input_dev->private = usbtouch;
631 input_dev->open = usbtouch_open;
632 input_dev->close = usbtouch_close;
634 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
635 input_dev->keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH);
636 input_set_abs_params(input_dev, ABS_X, type->min_xc, type->max_xc, 0, 0);
637 input_set_abs_params(input_dev, ABS_Y, type->min_yc, type->max_yc, 0, 0);
639 input_set_abs_params(input_dev, ABS_PRESSURE, type->min_press,
640 type->max_press, 0, 0);
642 usb_fill_int_urb(usbtouch->irq, usbtouch->udev,
643 usb_rcvintpipe(usbtouch->udev, endpoint->bEndpointAddress),
644 usbtouch->data, type->rept_size,
645 usbtouch_irq, usbtouch, endpoint->bInterval);
647 usbtouch->irq->dev = usbtouch->udev;
648 usbtouch->irq->transfer_dma = usbtouch->data_dma;
649 usbtouch->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
651 /* device specific init */
653 err = type->init(usbtouch);
655 dbg("%s - type->init() failed, err: %d", __FUNCTION__, err);
656 goto out_free_buffers;
660 err = input_register_device(usbtouch->input);
662 dbg("%s - input_register_device failed, err: %d", __FUNCTION__, err);
663 goto out_free_buffers;
666 usb_set_intfdata(intf, usbtouch);
671 usbtouch_free_buffers(udev, usbtouch);
673 input_free_device(input_dev);
678 static void usbtouch_disconnect(struct usb_interface *intf)
680 struct usbtouch_usb *usbtouch = usb_get_intfdata(intf);
682 dbg("%s - called", __FUNCTION__);
687 dbg("%s - usbtouch is initialized, cleaning up", __FUNCTION__);
688 usb_set_intfdata(intf, NULL);
689 usb_kill_urb(usbtouch->irq);
690 input_unregister_device(usbtouch->input);
691 usb_free_urb(usbtouch->irq);
692 usbtouch_free_buffers(interface_to_usbdev(intf), usbtouch);
696 MODULE_DEVICE_TABLE(usb, usbtouch_devices);
698 static struct usb_driver usbtouch_driver = {
699 .name = "usbtouchscreen",
700 .probe = usbtouch_probe,
701 .disconnect = usbtouch_disconnect,
702 .id_table = usbtouch_devices,
705 static int __init usbtouch_init(void)
707 return usb_register(&usbtouch_driver);
710 static void __exit usbtouch_cleanup(void)
712 usb_deregister(&usbtouch_driver);
715 module_init(usbtouch_init);
716 module_exit(usbtouch_cleanup);
718 MODULE_AUTHOR(DRIVER_AUTHOR);
719 MODULE_DESCRIPTION(DRIVER_DESC);
720 MODULE_LICENSE("GPL");
722 MODULE_ALIAS("touchkitusb");
723 MODULE_ALIAS("itmtouch");
724 MODULE_ALIAS("mtouchusb");