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
15 * - GoTop Super_Q2/GogoPen/PenPower tablets
16 * - JASTEC USB touch controller/DigiTech DTR-02U
17 * - Zytronic capacitive touchscreen
19 * - Elo TouchSystems 2700 IntelliTouch
21 * Copyright (C) 2004-2007 by Daniel Ritz <daniel.ritz@gmx.ch>
22 * Copyright (C) by Todd E. Johnson (mtouchusb.c)
24 * This program is free software; you can redistribute it and/or
25 * modify it under the terms of the GNU General Public License as
26 * published by the Free Software Foundation; either version 2 of the
27 * License, or (at your option) any later version.
29 * This program is distributed in the hope that it will be useful, but
30 * WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
32 * General Public License for more details.
34 * You should have received a copy of the GNU General Public License
35 * along with this program; if not, write to the Free Software
36 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
38 * Driver is based on touchkitusb.c
39 * - ITM parts are from itmtouch.c
40 * - 3M parts are from mtouchusb.c
41 * - PanJit parts are from an unmerged driver by Lanslott Gish
42 * - DMC TSC 10/25 are from Holger Schurig, with ideas from an unmerged
43 * driver from Marius Vollmer
45 *****************************************************************************/
49 #include <linux/kernel.h>
50 #include <linux/slab.h>
51 #include <linux/input.h>
52 #include <linux/module.h>
53 #include <linux/init.h>
54 #include <linux/usb.h>
55 #include <linux/usb/input.h>
56 #include <linux/hid.h>
59 #define DRIVER_VERSION "v0.6"
60 #define DRIVER_AUTHOR "Daniel Ritz <daniel.ritz@gmx.ch>"
61 #define DRIVER_DESC "USB Touchscreen Driver"
64 module_param(swap_xy, bool, 0644);
65 MODULE_PARM_DESC(swap_xy, "If set X and Y axes are swapped.");
67 static bool hwcalib_xy;
68 module_param(hwcalib_xy, bool, 0644);
69 MODULE_PARM_DESC(hwcalib_xy, "If set hw-calibrated X/Y are used if available");
71 /* device specifc data/functions */
73 struct usbtouch_device_info {
76 int min_press, max_press;
80 * Always service the USB devices irq not just when the input device is
81 * open. This is useful when devices have a watchdog which prevents us
82 * from periodically polling the device. Leave this unset unless your
83 * touchscreen device requires it, as it does consume more of the USB
88 void (*process_pkt) (struct usbtouch_usb *usbtouch, unsigned char *pkt, int len);
91 * used to get the packet len. possible return values:
94 * < 0: -return value more bytes needed
96 int (*get_pkt_len) (unsigned char *pkt, int len);
98 int (*read_data) (struct usbtouch_usb *usbtouch, unsigned char *pkt);
99 int (*alloc) (struct usbtouch_usb *usbtouch);
100 int (*init) (struct usbtouch_usb *usbtouch);
101 void (*exit) (struct usbtouch_usb *usbtouch);
104 /* a usbtouch device */
105 struct usbtouch_usb {
108 unsigned char *buffer;
111 struct usb_interface *interface;
112 struct input_dev *input;
113 struct usbtouch_device_info *type;
135 DEVTYPE_GENERAL_TOUCH,
145 #define USB_DEVICE_HID_CLASS(vend, prod) \
146 .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS \
147 | USB_DEVICE_ID_MATCH_INT_PROTOCOL \
148 | USB_DEVICE_ID_MATCH_DEVICE, \
149 .idVendor = (vend), \
150 .idProduct = (prod), \
151 .bInterfaceClass = USB_INTERFACE_CLASS_HID, \
152 .bInterfaceProtocol = USB_INTERFACE_PROTOCOL_MOUSE
154 static const struct usb_device_id usbtouch_devices[] = {
155 #ifdef CONFIG_TOUCHSCREEN_USB_EGALAX
156 /* ignore the HID capable devices, handled by usbhid */
157 {USB_DEVICE_HID_CLASS(0x0eef, 0x0001), .driver_info = DEVTYPE_IGNORE},
158 {USB_DEVICE_HID_CLASS(0x0eef, 0x0002), .driver_info = DEVTYPE_IGNORE},
160 /* normal device IDs */
161 {USB_DEVICE(0x3823, 0x0001), .driver_info = DEVTYPE_EGALAX},
162 {USB_DEVICE(0x3823, 0x0002), .driver_info = DEVTYPE_EGALAX},
163 {USB_DEVICE(0x0123, 0x0001), .driver_info = DEVTYPE_EGALAX},
164 {USB_DEVICE(0x0eef, 0x0001), .driver_info = DEVTYPE_EGALAX},
165 {USB_DEVICE(0x0eef, 0x0002), .driver_info = DEVTYPE_EGALAX},
166 {USB_DEVICE(0x1234, 0x0001), .driver_info = DEVTYPE_EGALAX},
167 {USB_DEVICE(0x1234, 0x0002), .driver_info = DEVTYPE_EGALAX},
170 #ifdef CONFIG_TOUCHSCREEN_USB_PANJIT
171 {USB_DEVICE(0x134c, 0x0001), .driver_info = DEVTYPE_PANJIT},
172 {USB_DEVICE(0x134c, 0x0002), .driver_info = DEVTYPE_PANJIT},
173 {USB_DEVICE(0x134c, 0x0003), .driver_info = DEVTYPE_PANJIT},
174 {USB_DEVICE(0x134c, 0x0004), .driver_info = DEVTYPE_PANJIT},
177 #ifdef CONFIG_TOUCHSCREEN_USB_3M
178 {USB_DEVICE(0x0596, 0x0001), .driver_info = DEVTYPE_3M},
181 #ifdef CONFIG_TOUCHSCREEN_USB_ITM
182 {USB_DEVICE(0x0403, 0xf9e9), .driver_info = DEVTYPE_ITM},
183 {USB_DEVICE(0x16e3, 0xf9e9), .driver_info = DEVTYPE_ITM},
186 #ifdef CONFIG_TOUCHSCREEN_USB_ETURBO
187 {USB_DEVICE(0x1234, 0x5678), .driver_info = DEVTYPE_ETURBO},
190 #ifdef CONFIG_TOUCHSCREEN_USB_GUNZE
191 {USB_DEVICE(0x0637, 0x0001), .driver_info = DEVTYPE_GUNZE},
194 #ifdef CONFIG_TOUCHSCREEN_USB_DMC_TSC10
195 {USB_DEVICE(0x0afa, 0x03e8), .driver_info = DEVTYPE_DMC_TSC10},
198 #ifdef CONFIG_TOUCHSCREEN_USB_IRTOUCH
199 {USB_DEVICE(0x595a, 0x0001), .driver_info = DEVTYPE_IRTOUCH},
200 {USB_DEVICE(0x6615, 0x0001), .driver_info = DEVTYPE_IRTOUCH},
203 #ifdef CONFIG_TOUCHSCREEN_USB_IDEALTEK
204 {USB_DEVICE(0x1391, 0x1000), .driver_info = DEVTYPE_IDEALTEK},
207 #ifdef CONFIG_TOUCHSCREEN_USB_GENERAL_TOUCH
208 {USB_DEVICE(0x0dfc, 0x0001), .driver_info = DEVTYPE_GENERAL_TOUCH},
211 #ifdef CONFIG_TOUCHSCREEN_USB_GOTOP
212 {USB_DEVICE(0x08f2, 0x007f), .driver_info = DEVTYPE_GOTOP},
213 {USB_DEVICE(0x08f2, 0x00ce), .driver_info = DEVTYPE_GOTOP},
214 {USB_DEVICE(0x08f2, 0x00f4), .driver_info = DEVTYPE_GOTOP},
217 #ifdef CONFIG_TOUCHSCREEN_USB_JASTEC
218 {USB_DEVICE(0x0f92, 0x0001), .driver_info = DEVTYPE_JASTEC},
221 #ifdef CONFIG_TOUCHSCREEN_USB_E2I
222 {USB_DEVICE(0x1ac7, 0x0001), .driver_info = DEVTYPE_E2I},
225 #ifdef CONFIG_TOUCHSCREEN_USB_ZYTRONIC
226 {USB_DEVICE(0x14c8, 0x0003), .driver_info = DEVTYPE_ZYTRONIC},
229 #ifdef CONFIG_TOUCHSCREEN_USB_ETT_TC45USB
231 {USB_DEVICE(0x0664, 0x0309), .driver_info = DEVTYPE_TC45USB},
233 {USB_DEVICE(0x0664, 0x0306), .driver_info = DEVTYPE_TC45USB},
236 #ifdef CONFIG_TOUCHSCREEN_USB_NEXIO
237 /* data interface only */
238 {USB_DEVICE_AND_INTERFACE_INFO(0x10f0, 0x2002, 0x0a, 0x00, 0x00),
239 .driver_info = DEVTYPE_NEXIO},
240 {USB_DEVICE_AND_INTERFACE_INFO(0x1870, 0x0001, 0x0a, 0x00, 0x00),
241 .driver_info = DEVTYPE_NEXIO},
244 #ifdef CONFIG_TOUCHSCREEN_USB_ELO
245 {USB_DEVICE(0x04e7, 0x0020), .driver_info = DEVTYPE_ELO},
252 /*****************************************************************************
256 #ifdef CONFIG_TOUCHSCREEN_USB_E2I
257 static int e2i_init(struct usbtouch_usb *usbtouch)
260 struct usb_device *udev = interface_to_usbdev(usbtouch->interface);
262 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
263 0x01, 0x02, 0x0000, 0x0081,
264 NULL, 0, USB_CTRL_SET_TIMEOUT);
266 dbg("%s - usb_control_msg - E2I_RESET - bytes|err: %d",
271 static int e2i_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
273 int tmp = (pkt[0] << 8) | pkt[1];
274 dev->x = (pkt[2] << 8) | pkt[3];
275 dev->y = (pkt[4] << 8) | pkt[5];
278 dev->touch = (tmp > 0);
279 dev->press = (tmp > 0 ? tmp : 0);
286 /*****************************************************************************
290 #ifdef CONFIG_TOUCHSCREEN_USB_EGALAX
296 #define EGALAX_PKT_TYPE_MASK 0xFE
297 #define EGALAX_PKT_TYPE_REPT 0x80
298 #define EGALAX_PKT_TYPE_DIAG 0x0A
300 static int egalax_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
302 if ((pkt[0] & EGALAX_PKT_TYPE_MASK) != EGALAX_PKT_TYPE_REPT)
305 dev->x = ((pkt[3] & 0x0F) << 7) | (pkt[4] & 0x7F);
306 dev->y = ((pkt[1] & 0x0F) << 7) | (pkt[2] & 0x7F);
307 dev->touch = pkt[0] & 0x01;
312 static int egalax_get_pkt_len(unsigned char *buf, int len)
314 switch (buf[0] & EGALAX_PKT_TYPE_MASK) {
315 case EGALAX_PKT_TYPE_REPT:
318 case EGALAX_PKT_TYPE_DIAG:
330 /*****************************************************************************
333 #ifdef CONFIG_TOUCHSCREEN_USB_PANJIT
334 static int panjit_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
336 dev->x = ((pkt[2] & 0x0F) << 8) | pkt[1];
337 dev->y = ((pkt[4] & 0x0F) << 8) | pkt[3];
338 dev->touch = pkt[0] & 0x01;
345 /*****************************************************************************
348 #ifdef CONFIG_TOUCHSCREEN_USB_3M
350 #define MTOUCHUSB_ASYNC_REPORT 1
351 #define MTOUCHUSB_RESET 7
352 #define MTOUCHUSB_REQ_CTRLLR_ID 10
354 static int mtouch_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
357 dev->x = (pkt[4] << 8) | pkt[3];
358 dev->y = 0xffff - ((pkt[6] << 8) | pkt[5]);
360 dev->x = (pkt[8] << 8) | pkt[7];
361 dev->y = (pkt[10] << 8) | pkt[9];
363 dev->touch = (pkt[2] & 0x40) ? 1 : 0;
368 static int mtouch_init(struct usbtouch_usb *usbtouch)
371 struct usb_device *udev = interface_to_usbdev(usbtouch->interface);
373 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
375 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
376 1, 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
377 dbg("%s - usb_control_msg - MTOUCHUSB_RESET - bytes|err: %d",
383 for (i = 0; i < 3; i++) {
384 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
385 MTOUCHUSB_ASYNC_REPORT,
386 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
387 1, 1, NULL, 0, USB_CTRL_SET_TIMEOUT);
388 dbg("%s - usb_control_msg - MTOUCHUSB_ASYNC_REPORT - bytes|err: %d",
396 /* Default min/max xy are the raw values, override if using hw-calib */
398 input_set_abs_params(usbtouch->input, ABS_X, 0, 0xffff, 0, 0);
399 input_set_abs_params(usbtouch->input, ABS_Y, 0, 0xffff, 0, 0);
407 /*****************************************************************************
410 #ifdef CONFIG_TOUCHSCREEN_USB_ITM
411 static int itm_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
415 * ITM devices report invalid x/y data if not touched.
416 * if the screen was touched before but is not touched any more
417 * report touch as 0 with the last valid x/y data once. then stop
418 * reporting data until touched again.
420 dev->press = ((pkt[2] & 0x01) << 7) | (pkt[5] & 0x7F);
422 touch = ~pkt[7] & 0x20;
432 dev->x = ((pkt[0] & 0x1F) << 7) | (pkt[3] & 0x7F);
433 dev->y = ((pkt[1] & 0x1F) << 7) | (pkt[4] & 0x7F);
441 /*****************************************************************************
444 #ifdef CONFIG_TOUCHSCREEN_USB_ETURBO
448 static int eturbo_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
452 /* packets should start with sync */
453 if (!(pkt[0] & 0x80))
456 shift = (6 - (pkt[0] & 0x03));
457 dev->x = ((pkt[3] << 7) | pkt[4]) >> shift;
458 dev->y = ((pkt[1] << 7) | pkt[2]) >> shift;
459 dev->touch = (pkt[0] & 0x10) ? 1 : 0;
464 static int eturbo_get_pkt_len(unsigned char *buf, int len)
475 /*****************************************************************************
478 #ifdef CONFIG_TOUCHSCREEN_USB_GUNZE
479 static int gunze_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
481 if (!(pkt[0] & 0x80) || ((pkt[1] | pkt[2] | pkt[3]) & 0x80))
484 dev->x = ((pkt[0] & 0x1F) << 7) | (pkt[2] & 0x7F);
485 dev->y = ((pkt[1] & 0x1F) << 7) | (pkt[3] & 0x7F);
486 dev->touch = pkt[0] & 0x20;
492 /*****************************************************************************
495 * Documentation about the controller and it's protocol can be found at
496 * http://www.dmccoltd.com/files/controler/tsc10usb_pi_e.pdf
497 * http://www.dmccoltd.com/files/controler/tsc25_usb_e.pdf
499 #ifdef CONFIG_TOUCHSCREEN_USB_DMC_TSC10
501 /* supported data rates. currently using 130 */
502 #define TSC10_RATE_POINT 0x50
503 #define TSC10_RATE_30 0x40
504 #define TSC10_RATE_50 0x41
505 #define TSC10_RATE_80 0x42
506 #define TSC10_RATE_100 0x43
507 #define TSC10_RATE_130 0x44
508 #define TSC10_RATE_150 0x45
511 #define TSC10_CMD_RESET 0x55
512 #define TSC10_CMD_RATE 0x05
513 #define TSC10_CMD_DATA1 0x01
515 static int dmc_tsc10_init(struct usbtouch_usb *usbtouch)
517 struct usb_device *dev = interface_to_usbdev(usbtouch->interface);
521 buf = kmalloc(2, GFP_NOIO);
525 buf[0] = buf[1] = 0xFF;
526 ret = usb_control_msg(dev, usb_rcvctrlpipe (dev, 0),
528 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
529 0, 0, buf, 2, USB_CTRL_SET_TIMEOUT);
532 if (buf[0] != 0x06) {
537 /* set coordinate output rate */
538 buf[0] = buf[1] = 0xFF;
539 ret = usb_control_msg(dev, usb_rcvctrlpipe (dev, 0),
541 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
542 TSC10_RATE_150, 0, buf, 2, USB_CTRL_SET_TIMEOUT);
545 if ((buf[0] != 0x06) && (buf[0] != 0x15 || buf[1] != 0x01)) {
550 /* start sending data */
551 ret = usb_control_msg(dev, usb_rcvctrlpipe (dev, 0),
553 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
554 0, 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
562 static int dmc_tsc10_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
564 dev->x = ((pkt[2] & 0x03) << 8) | pkt[1];
565 dev->y = ((pkt[4] & 0x03) << 8) | pkt[3];
566 dev->touch = pkt[0] & 0x01;
573 /*****************************************************************************
576 #ifdef CONFIG_TOUCHSCREEN_USB_IRTOUCH
577 static int irtouch_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
579 dev->x = (pkt[3] << 8) | pkt[2];
580 dev->y = (pkt[5] << 8) | pkt[4];
581 dev->touch = (pkt[1] & 0x03) ? 1 : 0;
587 /*****************************************************************************
588 * ET&T TC5UH/TC4UM part
590 #ifdef CONFIG_TOUCHSCREEN_USB_ETT_TC45USB
591 static int tc45usb_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
593 dev->x = ((pkt[2] & 0x0F) << 8) | pkt[1];
594 dev->y = ((pkt[4] & 0x0F) << 8) | pkt[3];
595 dev->touch = pkt[0] & 0x01;
601 /*****************************************************************************
602 * IdealTEK URTC1000 Part
604 #ifdef CONFIG_TOUCHSCREEN_USB_IDEALTEK
608 static int idealtek_get_pkt_len(unsigned char *buf, int len)
617 static int idealtek_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
619 switch (pkt[0] & 0x98) {
621 /* touch data in IdealTEK mode */
622 dev->x = (pkt[1] << 5) | (pkt[2] >> 2);
623 dev->y = (pkt[3] << 5) | (pkt[4] >> 2);
624 dev->touch = (pkt[0] & 0x40) ? 1 : 0;
628 /* touch data in MT emulation mode */
629 dev->x = (pkt[2] << 5) | (pkt[1] >> 2);
630 dev->y = (pkt[4] << 5) | (pkt[3] >> 2);
631 dev->touch = (pkt[0] & 0x40) ? 1 : 0;
640 /*****************************************************************************
643 #ifdef CONFIG_TOUCHSCREEN_USB_GENERAL_TOUCH
644 static int general_touch_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
646 dev->x = (pkt[2] << 8) | pkt[1];
647 dev->y = (pkt[4] << 8) | pkt[3];
648 dev->press = pkt[5] & 0xff;
649 dev->touch = pkt[0] & 0x01;
655 /*****************************************************************************
658 #ifdef CONFIG_TOUCHSCREEN_USB_GOTOP
659 static int gotop_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
661 dev->x = ((pkt[1] & 0x38) << 4) | pkt[2];
662 dev->y = ((pkt[1] & 0x07) << 7) | pkt[3];
663 dev->touch = pkt[0] & 0x01;
669 /*****************************************************************************
672 #ifdef CONFIG_TOUCHSCREEN_USB_JASTEC
673 static int jastec_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
675 dev->x = ((pkt[0] & 0x3f) << 6) | (pkt[2] & 0x3f);
676 dev->y = ((pkt[1] & 0x3f) << 6) | (pkt[3] & 0x3f);
677 dev->touch = (pkt[0] & 0x40) >> 6;
683 /*****************************************************************************
686 #ifdef CONFIG_TOUCHSCREEN_USB_ZYTRONIC
687 static int zytronic_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
690 case 0x3A: /* command response */
691 dbg("%s: Command response %d", __func__, pkt[1]);
694 case 0xC0: /* down */
695 dev->x = (pkt[1] & 0x7f) | ((pkt[2] & 0x07) << 7);
696 dev->y = (pkt[3] & 0x7f) | ((pkt[4] & 0x07) << 7);
698 dbg("%s: down %d,%d", __func__, dev->x, dev->y);
702 dev->x = (pkt[1] & 0x7f) | ((pkt[2] & 0x07) << 7);
703 dev->y = (pkt[3] & 0x7f) | ((pkt[4] & 0x07) << 7);
705 dbg("%s: up %d,%d", __func__, dev->x, dev->y);
709 dbg("%s: Unknown return %d", __func__, pkt[0]);
717 /*****************************************************************************
720 #ifdef CONFIG_TOUCHSCREEN_USB_NEXIO
722 #define NEXIO_TIMEOUT 5000
723 #define NEXIO_BUFSIZE 1024
724 #define NEXIO_THRESHOLD 50
728 unsigned char *ack_buf;
731 struct nexio_touch_packet {
732 u8 flags; /* 0xe1 = touch, 0xe1 = release */
733 __be16 data_len; /* total bytes of touch data */
734 __be16 x_len; /* bytes for X axis */
735 __be16 y_len; /* bytes for Y axis */
737 } __attribute__ ((packed));
739 static unsigned char nexio_ack_pkt[2] = { 0xaa, 0x02 };
740 static unsigned char nexio_init_pkt[4] = { 0x82, 0x04, 0x0a, 0x0f };
742 static void nexio_ack_complete(struct urb *urb)
746 static int nexio_alloc(struct usbtouch_usb *usbtouch)
748 struct nexio_priv *priv;
751 usbtouch->priv = kmalloc(sizeof(struct nexio_priv), GFP_KERNEL);
755 priv = usbtouch->priv;
757 priv->ack_buf = kmemdup(nexio_ack_pkt, sizeof(nexio_ack_pkt),
762 priv->ack = usb_alloc_urb(0, GFP_KERNEL);
764 dbg("%s - usb_alloc_urb failed: usbtouch->ack", __func__);
771 kfree(priv->ack_buf);
778 static int nexio_init(struct usbtouch_usb *usbtouch)
780 struct usb_device *dev = interface_to_usbdev(usbtouch->interface);
781 struct usb_host_interface *interface = usbtouch->interface->cur_altsetting;
782 struct nexio_priv *priv = usbtouch->priv;
786 char *firmware_ver = NULL, *device_name = NULL;
787 int input_ep = 0, output_ep = 0;
789 /* find first input and output endpoint */
790 for (i = 0; i < interface->desc.bNumEndpoints; i++) {
792 usb_endpoint_dir_in(&interface->endpoint[i].desc))
793 input_ep = interface->endpoint[i].desc.bEndpointAddress;
795 usb_endpoint_dir_out(&interface->endpoint[i].desc))
796 output_ep = interface->endpoint[i].desc.bEndpointAddress;
798 if (!input_ep || !output_ep)
801 buf = kmalloc(NEXIO_BUFSIZE, GFP_NOIO);
805 /* two empty reads */
806 for (i = 0; i < 2; i++) {
807 ret = usb_bulk_msg(dev, usb_rcvbulkpipe(dev, input_ep),
808 buf, NEXIO_BUFSIZE, &actual_len,
814 /* send init command */
815 memcpy(buf, nexio_init_pkt, sizeof(nexio_init_pkt));
816 ret = usb_bulk_msg(dev, usb_sndbulkpipe(dev, output_ep),
817 buf, sizeof(nexio_init_pkt), &actual_len,
823 for (i = 0; i < 3; i++) {
824 memset(buf, 0, NEXIO_BUFSIZE);
825 ret = usb_bulk_msg(dev, usb_rcvbulkpipe(dev, input_ep),
826 buf, NEXIO_BUFSIZE, &actual_len,
828 if (ret < 0 || actual_len < 1 || buf[1] != actual_len)
831 case 0x83: /* firmware version */
833 firmware_ver = kstrdup(&buf[2], GFP_NOIO);
835 case 0x84: /* device name */
837 device_name = kstrdup(&buf[2], GFP_NOIO);
842 printk(KERN_INFO "Nexio device: %s, firmware version: %s\n",
843 device_name, firmware_ver);
848 usb_fill_bulk_urb(priv->ack, dev, usb_sndbulkpipe(dev, output_ep),
849 priv->ack_buf, sizeof(nexio_ack_pkt),
850 nexio_ack_complete, usbtouch);
858 static void nexio_exit(struct usbtouch_usb *usbtouch)
860 struct nexio_priv *priv = usbtouch->priv;
862 usb_kill_urb(priv->ack);
863 usb_free_urb(priv->ack);
864 kfree(priv->ack_buf);
868 static int nexio_read_data(struct usbtouch_usb *usbtouch, unsigned char *pkt)
870 struct nexio_touch_packet *packet = (void *) pkt;
871 struct nexio_priv *priv = usbtouch->priv;
872 unsigned int data_len = be16_to_cpu(packet->data_len);
873 unsigned int x_len = be16_to_cpu(packet->x_len);
874 unsigned int y_len = be16_to_cpu(packet->y_len);
875 int x, y, begin_x, begin_y, end_x, end_y, w, h, ret;
877 /* got touch data? */
878 if ((pkt[0] & 0xe0) != 0xe0)
887 ret = usb_submit_urb(priv->ack, GFP_ATOMIC);
889 if (!usbtouch->type->max_xc) {
890 usbtouch->type->max_xc = 2 * x_len;
891 input_set_abs_params(usbtouch->input, ABS_X,
892 0, usbtouch->type->max_xc, 0, 0);
893 usbtouch->type->max_yc = 2 * y_len;
894 input_set_abs_params(usbtouch->input, ABS_Y,
895 0, usbtouch->type->max_yc, 0, 0);
898 * The device reports state of IR sensors on X and Y axes.
899 * Each byte represents "darkness" percentage (0-100) of one element.
900 * 17" touchscreen reports only 64 x 52 bytes so the resolution is low.
901 * This also means that there's a limited multi-touch capability but
902 * it's disabled (and untested) here as there's no X driver for that.
904 begin_x = end_x = begin_y = end_y = -1;
905 for (x = 0; x < x_len; x++) {
906 if (begin_x == -1 && packet->data[x] > NEXIO_THRESHOLD) {
910 if (end_x == -1 && begin_x != -1 && packet->data[x] < NEXIO_THRESHOLD) {
912 for (y = x_len; y < data_len; y++) {
913 if (begin_y == -1 && packet->data[y] > NEXIO_THRESHOLD) {
918 begin_y != -1 && packet->data[y] < NEXIO_THRESHOLD) {
919 end_y = y - 1 - x_len;
924 input_report_abs(usbtouch->input,
925 ABS_MT_TOUCH_MAJOR, max(w,h));
926 input_report_abs(usbtouch->input,
927 ABS_MT_TOUCH_MINOR, min(x,h));
928 input_report_abs(usbtouch->input,
929 ABS_MT_POSITION_X, 2*begin_x+w);
930 input_report_abs(usbtouch->input,
931 ABS_MT_POSITION_Y, 2*begin_y+h);
932 input_report_abs(usbtouch->input,
933 ABS_MT_ORIENTATION, w > h);
934 input_mt_sync(usbtouch->input);
937 usbtouch->x = 2 * begin_x + w;
938 usbtouch->y = 2 * begin_y + h;
939 usbtouch->touch = packet->flags & 0x01;
940 begin_y = end_y = -1;
944 begin_x = end_x = -1;
953 /*****************************************************************************
957 #ifdef CONFIG_TOUCHSCREEN_USB_ELO
959 static int elo_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
961 dev->x = (pkt[3] << 8) | pkt[2];
962 dev->y = (pkt[5] << 8) | pkt[4];
963 dev->touch = pkt[6] > 0;
971 /*****************************************************************************
972 * the different device descriptors
975 static void usbtouch_process_multi(struct usbtouch_usb *usbtouch,
976 unsigned char *pkt, int len);
979 static struct usbtouch_device_info usbtouch_dev_info[] = {
980 #ifdef CONFIG_TOUCHSCREEN_USB_ELO
988 .read_data = elo_read_data,
992 #ifdef CONFIG_TOUCHSCREEN_USB_EGALAX
999 .process_pkt = usbtouch_process_multi,
1000 .get_pkt_len = egalax_get_pkt_len,
1001 .read_data = egalax_read_data,
1005 #ifdef CONFIG_TOUCHSCREEN_USB_PANJIT
1006 [DEVTYPE_PANJIT] = {
1012 .read_data = panjit_read_data,
1016 #ifdef CONFIG_TOUCHSCREEN_USB_3M
1023 .read_data = mtouch_read_data,
1024 .init = mtouch_init,
1028 #ifdef CONFIG_TOUCHSCREEN_USB_ITM
1036 .read_data = itm_read_data,
1040 #ifdef CONFIG_TOUCHSCREEN_USB_ETURBO
1041 [DEVTYPE_ETURBO] = {
1047 .process_pkt = usbtouch_process_multi,
1048 .get_pkt_len = eturbo_get_pkt_len,
1049 .read_data = eturbo_read_data,
1053 #ifdef CONFIG_TOUCHSCREEN_USB_GUNZE
1060 .read_data = gunze_read_data,
1064 #ifdef CONFIG_TOUCHSCREEN_USB_DMC_TSC10
1065 [DEVTYPE_DMC_TSC10] = {
1071 .init = dmc_tsc10_init,
1072 .read_data = dmc_tsc10_read_data,
1076 #ifdef CONFIG_TOUCHSCREEN_USB_IRTOUCH
1077 [DEVTYPE_IRTOUCH] = {
1083 .read_data = irtouch_read_data,
1087 #ifdef CONFIG_TOUCHSCREEN_USB_IDEALTEK
1088 [DEVTYPE_IDEALTEK] = {
1094 .process_pkt = usbtouch_process_multi,
1095 .get_pkt_len = idealtek_get_pkt_len,
1096 .read_data = idealtek_read_data,
1100 #ifdef CONFIG_TOUCHSCREEN_USB_GENERAL_TOUCH
1101 [DEVTYPE_GENERAL_TOUCH] = {
1107 .read_data = general_touch_read_data,
1111 #ifdef CONFIG_TOUCHSCREEN_USB_GOTOP
1118 .read_data = gotop_read_data,
1122 #ifdef CONFIG_TOUCHSCREEN_USB_JASTEC
1123 [DEVTYPE_JASTEC] = {
1129 .read_data = jastec_read_data,
1133 #ifdef CONFIG_TOUCHSCREEN_USB_E2I
1141 .read_data = e2i_read_data,
1145 #ifdef CONFIG_TOUCHSCREEN_USB_ZYTRONIC
1146 [DEVTYPE_ZYTRONIC] = {
1152 .read_data = zytronic_read_data,
1157 #ifdef CONFIG_TOUCHSCREEN_USB_ETT_TC45USB
1158 [DEVTYPE_TC45USB] = {
1164 .read_data = tc45usb_read_data,
1168 #ifdef CONFIG_TOUCHSCREEN_USB_NEXIO
1172 .read_data = nexio_read_data,
1173 .alloc = nexio_alloc,
1181 /*****************************************************************************
1184 static void usbtouch_process_pkt(struct usbtouch_usb *usbtouch,
1185 unsigned char *pkt, int len)
1187 struct usbtouch_device_info *type = usbtouch->type;
1189 if (!type->read_data(usbtouch, pkt))
1192 input_report_key(usbtouch->input, BTN_TOUCH, usbtouch->touch);
1195 input_report_abs(usbtouch->input, ABS_X, usbtouch->y);
1196 input_report_abs(usbtouch->input, ABS_Y, usbtouch->x);
1198 input_report_abs(usbtouch->input, ABS_X, usbtouch->x);
1199 input_report_abs(usbtouch->input, ABS_Y, usbtouch->y);
1201 if (type->max_press)
1202 input_report_abs(usbtouch->input, ABS_PRESSURE, usbtouch->press);
1203 input_sync(usbtouch->input);
1208 static void usbtouch_process_multi(struct usbtouch_usb *usbtouch,
1209 unsigned char *pkt, int len)
1211 unsigned char *buffer;
1212 int pkt_len, pos, buf_len, tmp;
1214 /* process buffer */
1215 if (unlikely(usbtouch->buf_len)) {
1216 /* try to get size */
1217 pkt_len = usbtouch->type->get_pkt_len(
1218 usbtouch->buffer, usbtouch->buf_len);
1221 if (unlikely(!pkt_len))
1224 /* need to append -pkt_len bytes before able to get size */
1225 if (unlikely(pkt_len < 0)) {
1226 int append = -pkt_len;
1227 if (unlikely(append > len))
1229 if (usbtouch->buf_len + append >= usbtouch->type->rept_size)
1231 memcpy(usbtouch->buffer + usbtouch->buf_len, pkt, append);
1232 usbtouch->buf_len += append;
1234 pkt_len = usbtouch->type->get_pkt_len(
1235 usbtouch->buffer, usbtouch->buf_len);
1241 tmp = pkt_len - usbtouch->buf_len;
1242 if (usbtouch->buf_len + tmp >= usbtouch->type->rept_size)
1244 memcpy(usbtouch->buffer + usbtouch->buf_len, pkt, tmp);
1245 usbtouch_process_pkt(usbtouch, usbtouch->buffer, pkt_len);
1248 buf_len = len - tmp;
1254 /* loop over the received packet, process */
1256 while (pos < buf_len) {
1257 /* get packet len */
1258 pkt_len = usbtouch->type->get_pkt_len(buffer + pos,
1261 /* unknown packet: skip one byte */
1262 if (unlikely(!pkt_len)) {
1267 /* full packet: process */
1268 if (likely((pkt_len > 0) && (pkt_len <= buf_len - pos))) {
1269 usbtouch_process_pkt(usbtouch, buffer + pos, pkt_len);
1271 /* incomplete packet: save in buffer */
1272 memcpy(usbtouch->buffer, buffer + pos, buf_len - pos);
1273 usbtouch->buf_len = buf_len - pos;
1280 usbtouch->buf_len = 0;
1286 static void usbtouch_irq(struct urb *urb)
1288 struct usbtouch_usb *usbtouch = urb->context;
1291 switch (urb->status) {
1296 /* this urb is timing out */
1297 dbg("%s - urb timed out - was the device unplugged?",
1304 /* this urb is terminated, clean up */
1305 dbg("%s - urb shutting down with status: %d",
1306 __func__, urb->status);
1309 dbg("%s - nonzero urb status received: %d",
1310 __func__, urb->status);
1314 usbtouch->type->process_pkt(usbtouch, usbtouch->data, urb->actual_length);
1317 usb_mark_last_busy(interface_to_usbdev(usbtouch->interface));
1318 retval = usb_submit_urb(urb, GFP_ATOMIC);
1320 err("%s - usb_submit_urb failed with result: %d",
1324 static int usbtouch_open(struct input_dev *input)
1326 struct usbtouch_usb *usbtouch = input_get_drvdata(input);
1329 usbtouch->irq->dev = interface_to_usbdev(usbtouch->interface);
1331 r = usb_autopm_get_interface(usbtouch->interface) ? -EIO : 0;
1335 if (!usbtouch->type->irq_always) {
1336 if (usb_submit_urb(usbtouch->irq, GFP_KERNEL)) {
1342 usbtouch->interface->needs_remote_wakeup = 1;
1344 usb_autopm_put_interface(usbtouch->interface);
1349 static void usbtouch_close(struct input_dev *input)
1351 struct usbtouch_usb *usbtouch = input_get_drvdata(input);
1354 if (!usbtouch->type->irq_always)
1355 usb_kill_urb(usbtouch->irq);
1356 r = usb_autopm_get_interface(usbtouch->interface);
1357 usbtouch->interface->needs_remote_wakeup = 0;
1359 usb_autopm_put_interface(usbtouch->interface);
1362 static int usbtouch_suspend
1363 (struct usb_interface *intf, pm_message_t message)
1365 struct usbtouch_usb *usbtouch = usb_get_intfdata(intf);
1367 usb_kill_urb(usbtouch->irq);
1372 static int usbtouch_resume(struct usb_interface *intf)
1374 struct usbtouch_usb *usbtouch = usb_get_intfdata(intf);
1375 struct input_dev *input = usbtouch->input;
1378 mutex_lock(&input->mutex);
1379 if (input->users || usbtouch->type->irq_always)
1380 result = usb_submit_urb(usbtouch->irq, GFP_NOIO);
1381 mutex_unlock(&input->mutex);
1386 static int usbtouch_reset_resume(struct usb_interface *intf)
1388 struct usbtouch_usb *usbtouch = usb_get_intfdata(intf);
1389 struct input_dev *input = usbtouch->input;
1392 /* reinit the device */
1393 if (usbtouch->type->init) {
1394 err = usbtouch->type->init(usbtouch);
1396 dbg("%s - type->init() failed, err: %d",
1402 /* restart IO if needed */
1403 mutex_lock(&input->mutex);
1405 err = usb_submit_urb(usbtouch->irq, GFP_NOIO);
1406 mutex_unlock(&input->mutex);
1411 static void usbtouch_free_buffers(struct usb_device *udev,
1412 struct usbtouch_usb *usbtouch)
1414 usb_free_coherent(udev, usbtouch->type->rept_size,
1415 usbtouch->data, usbtouch->data_dma);
1416 kfree(usbtouch->buffer);
1419 static struct usb_endpoint_descriptor *
1420 usbtouch_get_input_endpoint(struct usb_host_interface *interface)
1424 for (i = 0; i < interface->desc.bNumEndpoints; i++)
1425 if (usb_endpoint_dir_in(&interface->endpoint[i].desc))
1426 return &interface->endpoint[i].desc;
1431 static int usbtouch_probe(struct usb_interface *intf,
1432 const struct usb_device_id *id)
1434 struct usbtouch_usb *usbtouch;
1435 struct input_dev *input_dev;
1436 struct usb_endpoint_descriptor *endpoint;
1437 struct usb_device *udev = interface_to_usbdev(intf);
1438 struct usbtouch_device_info *type;
1441 /* some devices are ignored */
1442 if (id->driver_info == DEVTYPE_IGNORE)
1445 endpoint = usbtouch_get_input_endpoint(intf->cur_altsetting);
1449 usbtouch = kzalloc(sizeof(struct usbtouch_usb), GFP_KERNEL);
1450 input_dev = input_allocate_device();
1451 if (!usbtouch || !input_dev)
1454 type = &usbtouch_dev_info[id->driver_info];
1455 usbtouch->type = type;
1456 if (!type->process_pkt)
1457 type->process_pkt = usbtouch_process_pkt;
1459 usbtouch->data = usb_alloc_coherent(udev, type->rept_size,
1460 GFP_KERNEL, &usbtouch->data_dma);
1461 if (!usbtouch->data)
1464 if (type->get_pkt_len) {
1465 usbtouch->buffer = kmalloc(type->rept_size, GFP_KERNEL);
1466 if (!usbtouch->buffer)
1467 goto out_free_buffers;
1470 usbtouch->irq = usb_alloc_urb(0, GFP_KERNEL);
1471 if (!usbtouch->irq) {
1472 dbg("%s - usb_alloc_urb failed: usbtouch->irq", __func__);
1473 goto out_free_buffers;
1476 usbtouch->interface = intf;
1477 usbtouch->input = input_dev;
1479 if (udev->manufacturer)
1480 strlcpy(usbtouch->name, udev->manufacturer, sizeof(usbtouch->name));
1482 if (udev->product) {
1483 if (udev->manufacturer)
1484 strlcat(usbtouch->name, " ", sizeof(usbtouch->name));
1485 strlcat(usbtouch->name, udev->product, sizeof(usbtouch->name));
1488 if (!strlen(usbtouch->name))
1489 snprintf(usbtouch->name, sizeof(usbtouch->name),
1490 "USB Touchscreen %04x:%04x",
1491 le16_to_cpu(udev->descriptor.idVendor),
1492 le16_to_cpu(udev->descriptor.idProduct));
1494 usb_make_path(udev, usbtouch->phys, sizeof(usbtouch->phys));
1495 strlcat(usbtouch->phys, "/input0", sizeof(usbtouch->phys));
1497 input_dev->name = usbtouch->name;
1498 input_dev->phys = usbtouch->phys;
1499 usb_to_input_id(udev, &input_dev->id);
1500 input_dev->dev.parent = &intf->dev;
1502 input_set_drvdata(input_dev, usbtouch);
1504 input_dev->open = usbtouch_open;
1505 input_dev->close = usbtouch_close;
1507 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
1508 input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
1509 input_set_abs_params(input_dev, ABS_X, type->min_xc, type->max_xc, 0, 0);
1510 input_set_abs_params(input_dev, ABS_Y, type->min_yc, type->max_yc, 0, 0);
1511 if (type->max_press)
1512 input_set_abs_params(input_dev, ABS_PRESSURE, type->min_press,
1513 type->max_press, 0, 0);
1515 if (usb_endpoint_type(endpoint) == USB_ENDPOINT_XFER_INT)
1516 usb_fill_int_urb(usbtouch->irq, udev,
1517 usb_rcvintpipe(udev, endpoint->bEndpointAddress),
1518 usbtouch->data, type->rept_size,
1519 usbtouch_irq, usbtouch, endpoint->bInterval);
1521 usb_fill_bulk_urb(usbtouch->irq, udev,
1522 usb_rcvbulkpipe(udev, endpoint->bEndpointAddress),
1523 usbtouch->data, type->rept_size,
1524 usbtouch_irq, usbtouch);
1526 usbtouch->irq->dev = udev;
1527 usbtouch->irq->transfer_dma = usbtouch->data_dma;
1528 usbtouch->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1530 /* device specific allocations */
1532 err = type->alloc(usbtouch);
1534 dbg("%s - type->alloc() failed, err: %d", __func__, err);
1539 /* device specific initialisation*/
1541 err = type->init(usbtouch);
1543 dbg("%s - type->init() failed, err: %d", __func__, err);
1548 err = input_register_device(usbtouch->input);
1550 dbg("%s - input_register_device failed, err: %d", __func__, err);
1554 usb_set_intfdata(intf, usbtouch);
1556 if (usbtouch->type->irq_always) {
1557 /* this can't fail */
1558 usb_autopm_get_interface(intf);
1559 err = usb_submit_urb(usbtouch->irq, GFP_KERNEL);
1561 usb_autopm_put_interface(intf);
1562 err("%s - usb_submit_urb failed with result: %d",
1564 goto out_unregister_input;
1570 out_unregister_input:
1571 input_unregister_device(input_dev);
1575 type->exit(usbtouch);
1577 usb_free_urb(usbtouch->irq);
1579 usbtouch_free_buffers(udev, usbtouch);
1581 input_free_device(input_dev);
1586 static void usbtouch_disconnect(struct usb_interface *intf)
1588 struct usbtouch_usb *usbtouch = usb_get_intfdata(intf);
1590 dbg("%s - called", __func__);
1595 dbg("%s - usbtouch is initialized, cleaning up", __func__);
1596 usb_set_intfdata(intf, NULL);
1597 /* this will stop IO via close */
1598 input_unregister_device(usbtouch->input);
1599 usb_free_urb(usbtouch->irq);
1600 if (usbtouch->type->exit)
1601 usbtouch->type->exit(usbtouch);
1602 usbtouch_free_buffers(interface_to_usbdev(intf), usbtouch);
1606 MODULE_DEVICE_TABLE(usb, usbtouch_devices);
1608 static struct usb_driver usbtouch_driver = {
1609 .name = "usbtouchscreen",
1610 .probe = usbtouch_probe,
1611 .disconnect = usbtouch_disconnect,
1612 .suspend = usbtouch_suspend,
1613 .resume = usbtouch_resume,
1614 .reset_resume = usbtouch_reset_resume,
1615 .id_table = usbtouch_devices,
1616 .supports_autosuspend = 1,
1619 module_usb_driver(usbtouch_driver);
1621 MODULE_AUTHOR(DRIVER_AUTHOR);
1622 MODULE_DESCRIPTION(DRIVER_DESC);
1623 MODULE_LICENSE("GPL");
1625 MODULE_ALIAS("touchkitusb");
1626 MODULE_ALIAS("itmtouch");
1627 MODULE_ALIAS("mtouchusb");