Linux 4.19.133
[linux/fpc-iii.git] / drivers / input / touchscreen / usbtouchscreen.c
blobd939c17985182d2fb7f158815a0f5090363e52cb
1 /******************************************************************************
2 * usbtouchscreen.c
3 * Driver for USB Touchscreens, supporting those devices:
4 * - eGalax Touchkit
5 * includes eTurboTouch CT-410/510/700
6 * - 3M/Microtouch EX II series
7 * - ITM
8 * - PanJit TouchSet
9 * - eTurboTouch
10 * - Gunze AHL61
11 * - DMC TSC-10/25
12 * - IRTOUCHSYSTEMS/UNITOP
13 * - IdealTEK URTC1000
14 * - General Touch
15 * - GoTop Super_Q2/GogoPen/PenPower tablets
16 * - JASTEC USB touch controller/DigiTech DTR-02U
17 * - Zytronic capacitive touchscreen
18 * - NEXIO/iNexio
19 * - Elo TouchSystems 2700 IntelliTouch
20 * - EasyTouch USB Dual/Multi touch controller from Data Modul
22 * Copyright (C) 2004-2007 by Daniel Ritz <daniel.ritz@gmx.ch>
23 * Copyright (C) by Todd E. Johnson (mtouchusb.c)
25 * This program is free software; you can redistribute it and/or
26 * modify it under the terms of the GNU General Public License as
27 * published by the Free Software Foundation; either version 2 of the
28 * License, or (at your option) any later version.
30 * This program is distributed in the hope that it will be useful, but
31 * WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
33 * General Public License for more details.
35 * You should have received a copy of the GNU General Public License
36 * along with this program; if not, write to the Free Software
37 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
39 * Driver is based on touchkitusb.c
40 * - ITM parts are from itmtouch.c
41 * - 3M parts are from mtouchusb.c
42 * - PanJit parts are from an unmerged driver by Lanslott Gish
43 * - DMC TSC 10/25 are from Holger Schurig, with ideas from an unmerged
44 * driver from Marius Vollmer
46 *****************************************************************************/
48 //#define DEBUG
50 #include <linux/kernel.h>
51 #include <linux/slab.h>
52 #include <linux/input.h>
53 #include <linux/module.h>
54 #include <linux/usb.h>
55 #include <linux/usb/input.h>
56 #include <linux/hid.h>
57 #include <linux/mutex.h>
59 static bool swap_xy;
60 module_param(swap_xy, bool, 0644);
61 MODULE_PARM_DESC(swap_xy, "If set X and Y axes are swapped.");
63 static bool hwcalib_xy;
64 module_param(hwcalib_xy, bool, 0644);
65 MODULE_PARM_DESC(hwcalib_xy, "If set hw-calibrated X/Y are used if available");
67 /* device specifc data/functions */
68 struct usbtouch_usb;
69 struct usbtouch_device_info {
70 int min_xc, max_xc;
71 int min_yc, max_yc;
72 int min_press, max_press;
73 int rept_size;
76 * Always service the USB devices irq not just when the input device is
77 * open. This is useful when devices have a watchdog which prevents us
78 * from periodically polling the device. Leave this unset unless your
79 * touchscreen device requires it, as it does consume more of the USB
80 * bandwidth.
82 bool irq_always;
84 void (*process_pkt) (struct usbtouch_usb *usbtouch, unsigned char *pkt, int len);
87 * used to get the packet len. possible return values:
88 * > 0: packet len
89 * = 0: skip one byte
90 * < 0: -return value more bytes needed
92 int (*get_pkt_len) (unsigned char *pkt, int len);
94 int (*read_data) (struct usbtouch_usb *usbtouch, unsigned char *pkt);
95 int (*alloc) (struct usbtouch_usb *usbtouch);
96 int (*init) (struct usbtouch_usb *usbtouch);
97 void (*exit) (struct usbtouch_usb *usbtouch);
100 /* a usbtouch device */
101 struct usbtouch_usb {
102 unsigned char *data;
103 dma_addr_t data_dma;
104 int data_size;
105 unsigned char *buffer;
106 int buf_len;
107 struct urb *irq;
108 struct usb_interface *interface;
109 struct input_dev *input;
110 struct usbtouch_device_info *type;
111 struct mutex pm_mutex; /* serialize access to open/suspend */
112 bool is_open;
113 char name[128];
114 char phys[64];
115 void *priv;
117 int x, y;
118 int touch, press;
122 /* device types */
123 enum {
124 DEVTYPE_IGNORE = -1,
125 DEVTYPE_EGALAX,
126 DEVTYPE_PANJIT,
127 DEVTYPE_3M,
128 DEVTYPE_ITM,
129 DEVTYPE_ETURBO,
130 DEVTYPE_GUNZE,
131 DEVTYPE_DMC_TSC10,
132 DEVTYPE_IRTOUCH,
133 DEVTYPE_IRTOUCH_HIRES,
134 DEVTYPE_IDEALTEK,
135 DEVTYPE_GENERAL_TOUCH,
136 DEVTYPE_GOTOP,
137 DEVTYPE_JASTEC,
138 DEVTYPE_E2I,
139 DEVTYPE_ZYTRONIC,
140 DEVTYPE_TC45USB,
141 DEVTYPE_NEXIO,
142 DEVTYPE_ELO,
143 DEVTYPE_ETOUCH,
146 #define USB_DEVICE_HID_CLASS(vend, prod) \
147 .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS \
148 | USB_DEVICE_ID_MATCH_DEVICE, \
149 .idVendor = (vend), \
150 .idProduct = (prod), \
151 .bInterfaceClass = USB_INTERFACE_CLASS_HID
153 static const struct usb_device_id usbtouch_devices[] = {
154 #ifdef CONFIG_TOUCHSCREEN_USB_EGALAX
155 /* ignore the HID capable devices, handled by usbhid */
156 {USB_DEVICE_HID_CLASS(0x0eef, 0x0001), .driver_info = DEVTYPE_IGNORE},
157 {USB_DEVICE_HID_CLASS(0x0eef, 0x0002), .driver_info = DEVTYPE_IGNORE},
159 /* normal device IDs */
160 {USB_DEVICE(0x3823, 0x0001), .driver_info = DEVTYPE_EGALAX},
161 {USB_DEVICE(0x3823, 0x0002), .driver_info = DEVTYPE_EGALAX},
162 {USB_DEVICE(0x0123, 0x0001), .driver_info = DEVTYPE_EGALAX},
163 {USB_DEVICE(0x0eef, 0x0001), .driver_info = DEVTYPE_EGALAX},
164 {USB_DEVICE(0x0eef, 0x0002), .driver_info = DEVTYPE_EGALAX},
165 {USB_DEVICE(0x1234, 0x0001), .driver_info = DEVTYPE_EGALAX},
166 {USB_DEVICE(0x1234, 0x0002), .driver_info = DEVTYPE_EGALAX},
167 #endif
169 #ifdef CONFIG_TOUCHSCREEN_USB_PANJIT
170 {USB_DEVICE(0x134c, 0x0001), .driver_info = DEVTYPE_PANJIT},
171 {USB_DEVICE(0x134c, 0x0002), .driver_info = DEVTYPE_PANJIT},
172 {USB_DEVICE(0x134c, 0x0003), .driver_info = DEVTYPE_PANJIT},
173 {USB_DEVICE(0x134c, 0x0004), .driver_info = DEVTYPE_PANJIT},
174 #endif
176 #ifdef CONFIG_TOUCHSCREEN_USB_3M
177 {USB_DEVICE(0x0596, 0x0001), .driver_info = DEVTYPE_3M},
178 #endif
180 #ifdef CONFIG_TOUCHSCREEN_USB_ITM
181 {USB_DEVICE(0x0403, 0xf9e9), .driver_info = DEVTYPE_ITM},
182 {USB_DEVICE(0x16e3, 0xf9e9), .driver_info = DEVTYPE_ITM},
183 #endif
185 #ifdef CONFIG_TOUCHSCREEN_USB_ETURBO
186 {USB_DEVICE(0x1234, 0x5678), .driver_info = DEVTYPE_ETURBO},
187 #endif
189 #ifdef CONFIG_TOUCHSCREEN_USB_GUNZE
190 {USB_DEVICE(0x0637, 0x0001), .driver_info = DEVTYPE_GUNZE},
191 #endif
193 #ifdef CONFIG_TOUCHSCREEN_USB_DMC_TSC10
194 {USB_DEVICE(0x0afa, 0x03e8), .driver_info = DEVTYPE_DMC_TSC10},
195 #endif
197 #ifdef CONFIG_TOUCHSCREEN_USB_IRTOUCH
198 {USB_DEVICE(0x255e, 0x0001), .driver_info = DEVTYPE_IRTOUCH},
199 {USB_DEVICE(0x595a, 0x0001), .driver_info = DEVTYPE_IRTOUCH},
200 {USB_DEVICE(0x6615, 0x0001), .driver_info = DEVTYPE_IRTOUCH},
201 {USB_DEVICE(0x6615, 0x0012), .driver_info = DEVTYPE_IRTOUCH_HIRES},
202 #endif
204 #ifdef CONFIG_TOUCHSCREEN_USB_IDEALTEK
205 {USB_DEVICE(0x1391, 0x1000), .driver_info = DEVTYPE_IDEALTEK},
206 #endif
208 #ifdef CONFIG_TOUCHSCREEN_USB_GENERAL_TOUCH
209 {USB_DEVICE(0x0dfc, 0x0001), .driver_info = DEVTYPE_GENERAL_TOUCH},
210 #endif
212 #ifdef CONFIG_TOUCHSCREEN_USB_GOTOP
213 {USB_DEVICE(0x08f2, 0x007f), .driver_info = DEVTYPE_GOTOP},
214 {USB_DEVICE(0x08f2, 0x00ce), .driver_info = DEVTYPE_GOTOP},
215 {USB_DEVICE(0x08f2, 0x00f4), .driver_info = DEVTYPE_GOTOP},
216 #endif
218 #ifdef CONFIG_TOUCHSCREEN_USB_JASTEC
219 {USB_DEVICE(0x0f92, 0x0001), .driver_info = DEVTYPE_JASTEC},
220 #endif
222 #ifdef CONFIG_TOUCHSCREEN_USB_E2I
223 {USB_DEVICE(0x1ac7, 0x0001), .driver_info = DEVTYPE_E2I},
224 #endif
226 #ifdef CONFIG_TOUCHSCREEN_USB_ZYTRONIC
227 {USB_DEVICE(0x14c8, 0x0003), .driver_info = DEVTYPE_ZYTRONIC},
228 #endif
230 #ifdef CONFIG_TOUCHSCREEN_USB_ETT_TC45USB
231 /* TC5UH */
232 {USB_DEVICE(0x0664, 0x0309), .driver_info = DEVTYPE_TC45USB},
233 /* TC4UM */
234 {USB_DEVICE(0x0664, 0x0306), .driver_info = DEVTYPE_TC45USB},
235 #endif
237 #ifdef CONFIG_TOUCHSCREEN_USB_NEXIO
238 /* data interface only */
239 {USB_DEVICE_AND_INTERFACE_INFO(0x10f0, 0x2002, 0x0a, 0x00, 0x00),
240 .driver_info = DEVTYPE_NEXIO},
241 {USB_DEVICE_AND_INTERFACE_INFO(0x1870, 0x0001, 0x0a, 0x00, 0x00),
242 .driver_info = DEVTYPE_NEXIO},
243 #endif
245 #ifdef CONFIG_TOUCHSCREEN_USB_ELO
246 {USB_DEVICE(0x04e7, 0x0020), .driver_info = DEVTYPE_ELO},
247 #endif
249 #ifdef CONFIG_TOUCHSCREEN_USB_EASYTOUCH
250 {USB_DEVICE(0x7374, 0x0001), .driver_info = DEVTYPE_ETOUCH},
251 #endif
257 /*****************************************************************************
258 * e2i Part
261 #ifdef CONFIG_TOUCHSCREEN_USB_E2I
262 static int e2i_init(struct usbtouch_usb *usbtouch)
264 int ret;
265 struct usb_device *udev = interface_to_usbdev(usbtouch->interface);
267 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
268 0x01, 0x02, 0x0000, 0x0081,
269 NULL, 0, USB_CTRL_SET_TIMEOUT);
271 dev_dbg(&usbtouch->interface->dev,
272 "%s - usb_control_msg - E2I_RESET - bytes|err: %d\n",
273 __func__, ret);
274 return ret;
277 static int e2i_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
279 int tmp = (pkt[0] << 8) | pkt[1];
280 dev->x = (pkt[2] << 8) | pkt[3];
281 dev->y = (pkt[4] << 8) | pkt[5];
283 tmp = tmp - 0xA000;
284 dev->touch = (tmp > 0);
285 dev->press = (tmp > 0 ? tmp : 0);
287 return 1;
289 #endif
292 /*****************************************************************************
293 * eGalax part
296 #ifdef CONFIG_TOUCHSCREEN_USB_EGALAX
298 #ifndef MULTI_PACKET
299 #define MULTI_PACKET
300 #endif
302 #define EGALAX_PKT_TYPE_MASK 0xFE
303 #define EGALAX_PKT_TYPE_REPT 0x80
304 #define EGALAX_PKT_TYPE_DIAG 0x0A
306 static int egalax_init(struct usbtouch_usb *usbtouch)
308 int ret, i;
309 unsigned char *buf;
310 struct usb_device *udev = interface_to_usbdev(usbtouch->interface);
313 * An eGalax diagnostic packet kicks the device into using the right
314 * protocol. We send a "check active" packet. The response will be
315 * read later and ignored.
318 buf = kmalloc(3, GFP_KERNEL);
319 if (!buf)
320 return -ENOMEM;
322 buf[0] = EGALAX_PKT_TYPE_DIAG;
323 buf[1] = 1; /* length */
324 buf[2] = 'A'; /* command - check active */
326 for (i = 0; i < 3; i++) {
327 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
329 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
330 0, 0, buf, 3,
331 USB_CTRL_SET_TIMEOUT);
332 if (ret >= 0) {
333 ret = 0;
334 break;
336 if (ret != -EPIPE)
337 break;
340 kfree(buf);
342 return ret;
345 static int egalax_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
347 if ((pkt[0] & EGALAX_PKT_TYPE_MASK) != EGALAX_PKT_TYPE_REPT)
348 return 0;
350 dev->x = ((pkt[3] & 0x0F) << 7) | (pkt[4] & 0x7F);
351 dev->y = ((pkt[1] & 0x0F) << 7) | (pkt[2] & 0x7F);
352 dev->touch = pkt[0] & 0x01;
354 return 1;
357 static int egalax_get_pkt_len(unsigned char *buf, int len)
359 switch (buf[0] & EGALAX_PKT_TYPE_MASK) {
360 case EGALAX_PKT_TYPE_REPT:
361 return 5;
363 case EGALAX_PKT_TYPE_DIAG:
364 if (len < 2)
365 return -1;
367 return buf[1] + 2;
370 return 0;
372 #endif
374 /*****************************************************************************
375 * EasyTouch part
378 #ifdef CONFIG_TOUCHSCREEN_USB_EASYTOUCH
380 #ifndef MULTI_PACKET
381 #define MULTI_PACKET
382 #endif
384 #define ETOUCH_PKT_TYPE_MASK 0xFE
385 #define ETOUCH_PKT_TYPE_REPT 0x80
386 #define ETOUCH_PKT_TYPE_REPT2 0xB0
387 #define ETOUCH_PKT_TYPE_DIAG 0x0A
389 static int etouch_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
391 if ((pkt[0] & ETOUCH_PKT_TYPE_MASK) != ETOUCH_PKT_TYPE_REPT &&
392 (pkt[0] & ETOUCH_PKT_TYPE_MASK) != ETOUCH_PKT_TYPE_REPT2)
393 return 0;
395 dev->x = ((pkt[1] & 0x1F) << 7) | (pkt[2] & 0x7F);
396 dev->y = ((pkt[3] & 0x1F) << 7) | (pkt[4] & 0x7F);
397 dev->touch = pkt[0] & 0x01;
399 return 1;
402 static int etouch_get_pkt_len(unsigned char *buf, int len)
404 switch (buf[0] & ETOUCH_PKT_TYPE_MASK) {
405 case ETOUCH_PKT_TYPE_REPT:
406 case ETOUCH_PKT_TYPE_REPT2:
407 return 5;
409 case ETOUCH_PKT_TYPE_DIAG:
410 if (len < 2)
411 return -1;
413 return buf[1] + 2;
416 return 0;
418 #endif
420 /*****************************************************************************
421 * PanJit Part
423 #ifdef CONFIG_TOUCHSCREEN_USB_PANJIT
424 static int panjit_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
426 dev->x = ((pkt[2] & 0x0F) << 8) | pkt[1];
427 dev->y = ((pkt[4] & 0x0F) << 8) | pkt[3];
428 dev->touch = pkt[0] & 0x01;
430 return 1;
432 #endif
435 /*****************************************************************************
436 * 3M/Microtouch Part
438 #ifdef CONFIG_TOUCHSCREEN_USB_3M
440 #define MTOUCHUSB_ASYNC_REPORT 1
441 #define MTOUCHUSB_RESET 7
442 #define MTOUCHUSB_REQ_CTRLLR_ID 10
444 #define MTOUCHUSB_REQ_CTRLLR_ID_LEN 16
446 static int mtouch_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
448 if (hwcalib_xy) {
449 dev->x = (pkt[4] << 8) | pkt[3];
450 dev->y = 0xffff - ((pkt[6] << 8) | pkt[5]);
451 } else {
452 dev->x = (pkt[8] << 8) | pkt[7];
453 dev->y = (pkt[10] << 8) | pkt[9];
455 dev->touch = (pkt[2] & 0x40) ? 1 : 0;
457 return 1;
460 struct mtouch_priv {
461 u8 fw_rev_major;
462 u8 fw_rev_minor;
465 static ssize_t mtouch_firmware_rev_show(struct device *dev,
466 struct device_attribute *attr, char *output)
468 struct usb_interface *intf = to_usb_interface(dev);
469 struct usbtouch_usb *usbtouch = usb_get_intfdata(intf);
470 struct mtouch_priv *priv = usbtouch->priv;
472 return scnprintf(output, PAGE_SIZE, "%1x.%1x\n",
473 priv->fw_rev_major, priv->fw_rev_minor);
475 static DEVICE_ATTR(firmware_rev, 0444, mtouch_firmware_rev_show, NULL);
477 static struct attribute *mtouch_attrs[] = {
478 &dev_attr_firmware_rev.attr,
479 NULL
482 static const struct attribute_group mtouch_attr_group = {
483 .attrs = mtouch_attrs,
486 static int mtouch_get_fw_revision(struct usbtouch_usb *usbtouch)
488 struct usb_device *udev = interface_to_usbdev(usbtouch->interface);
489 struct mtouch_priv *priv = usbtouch->priv;
490 u8 *buf;
491 int ret;
493 buf = kzalloc(MTOUCHUSB_REQ_CTRLLR_ID_LEN, GFP_NOIO);
494 if (!buf)
495 return -ENOMEM;
497 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
498 MTOUCHUSB_REQ_CTRLLR_ID,
499 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
500 0, 0, buf, MTOUCHUSB_REQ_CTRLLR_ID_LEN,
501 USB_CTRL_SET_TIMEOUT);
502 if (ret != MTOUCHUSB_REQ_CTRLLR_ID_LEN) {
503 dev_warn(&usbtouch->interface->dev,
504 "Failed to read FW rev: %d\n", ret);
505 ret = ret < 0 ? ret : -EIO;
506 goto free;
509 priv->fw_rev_major = buf[3];
510 priv->fw_rev_minor = buf[4];
512 ret = 0;
514 free:
515 kfree(buf);
516 return ret;
519 static int mtouch_alloc(struct usbtouch_usb *usbtouch)
521 int ret;
523 usbtouch->priv = kmalloc(sizeof(struct mtouch_priv), GFP_KERNEL);
524 if (!usbtouch->priv)
525 return -ENOMEM;
527 ret = sysfs_create_group(&usbtouch->interface->dev.kobj,
528 &mtouch_attr_group);
529 if (ret) {
530 kfree(usbtouch->priv);
531 usbtouch->priv = NULL;
532 return ret;
535 return 0;
538 static int mtouch_init(struct usbtouch_usb *usbtouch)
540 int ret, i;
541 struct usb_device *udev = interface_to_usbdev(usbtouch->interface);
543 ret = mtouch_get_fw_revision(usbtouch);
544 if (ret)
545 return ret;
547 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
548 MTOUCHUSB_RESET,
549 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
550 1, 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
551 dev_dbg(&usbtouch->interface->dev,
552 "%s - usb_control_msg - MTOUCHUSB_RESET - bytes|err: %d\n",
553 __func__, ret);
554 if (ret < 0)
555 return ret;
556 msleep(150);
558 for (i = 0; i < 3; i++) {
559 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
560 MTOUCHUSB_ASYNC_REPORT,
561 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
562 1, 1, NULL, 0, USB_CTRL_SET_TIMEOUT);
563 dev_dbg(&usbtouch->interface->dev,
564 "%s - usb_control_msg - MTOUCHUSB_ASYNC_REPORT - bytes|err: %d\n",
565 __func__, ret);
566 if (ret >= 0)
567 break;
568 if (ret != -EPIPE)
569 return ret;
572 /* Default min/max xy are the raw values, override if using hw-calib */
573 if (hwcalib_xy) {
574 input_set_abs_params(usbtouch->input, ABS_X, 0, 0xffff, 0, 0);
575 input_set_abs_params(usbtouch->input, ABS_Y, 0, 0xffff, 0, 0);
578 return 0;
581 static void mtouch_exit(struct usbtouch_usb *usbtouch)
583 struct mtouch_priv *priv = usbtouch->priv;
585 sysfs_remove_group(&usbtouch->interface->dev.kobj, &mtouch_attr_group);
586 kfree(priv);
588 #endif
591 /*****************************************************************************
592 * ITM Part
594 #ifdef CONFIG_TOUCHSCREEN_USB_ITM
595 static int itm_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
597 int touch;
599 * ITM devices report invalid x/y data if not touched.
600 * if the screen was touched before but is not touched any more
601 * report touch as 0 with the last valid x/y data once. then stop
602 * reporting data until touched again.
604 dev->press = ((pkt[2] & 0x01) << 7) | (pkt[5] & 0x7F);
606 touch = ~pkt[7] & 0x20;
607 if (!touch) {
608 if (dev->touch) {
609 dev->touch = 0;
610 return 1;
613 return 0;
616 dev->x = ((pkt[0] & 0x1F) << 7) | (pkt[3] & 0x7F);
617 dev->y = ((pkt[1] & 0x1F) << 7) | (pkt[4] & 0x7F);
618 dev->touch = touch;
620 return 1;
622 #endif
625 /*****************************************************************************
626 * eTurboTouch part
628 #ifdef CONFIG_TOUCHSCREEN_USB_ETURBO
629 #ifndef MULTI_PACKET
630 #define MULTI_PACKET
631 #endif
632 static int eturbo_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
634 unsigned int shift;
636 /* packets should start with sync */
637 if (!(pkt[0] & 0x80))
638 return 0;
640 shift = (6 - (pkt[0] & 0x03));
641 dev->x = ((pkt[3] << 7) | pkt[4]) >> shift;
642 dev->y = ((pkt[1] << 7) | pkt[2]) >> shift;
643 dev->touch = (pkt[0] & 0x10) ? 1 : 0;
645 return 1;
648 static int eturbo_get_pkt_len(unsigned char *buf, int len)
650 if (buf[0] & 0x80)
651 return 5;
652 if (buf[0] == 0x01)
653 return 3;
654 return 0;
656 #endif
659 /*****************************************************************************
660 * Gunze part
662 #ifdef CONFIG_TOUCHSCREEN_USB_GUNZE
663 static int gunze_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
665 if (!(pkt[0] & 0x80) || ((pkt[1] | pkt[2] | pkt[3]) & 0x80))
666 return 0;
668 dev->x = ((pkt[0] & 0x1F) << 7) | (pkt[2] & 0x7F);
669 dev->y = ((pkt[1] & 0x1F) << 7) | (pkt[3] & 0x7F);
670 dev->touch = pkt[0] & 0x20;
672 return 1;
674 #endif
676 /*****************************************************************************
677 * DMC TSC-10/25 Part
679 * Documentation about the controller and it's protocol can be found at
680 * http://www.dmccoltd.com/files/controler/tsc10usb_pi_e.pdf
681 * http://www.dmccoltd.com/files/controler/tsc25_usb_e.pdf
683 #ifdef CONFIG_TOUCHSCREEN_USB_DMC_TSC10
685 /* supported data rates. currently using 130 */
686 #define TSC10_RATE_POINT 0x50
687 #define TSC10_RATE_30 0x40
688 #define TSC10_RATE_50 0x41
689 #define TSC10_RATE_80 0x42
690 #define TSC10_RATE_100 0x43
691 #define TSC10_RATE_130 0x44
692 #define TSC10_RATE_150 0x45
694 /* commands */
695 #define TSC10_CMD_RESET 0x55
696 #define TSC10_CMD_RATE 0x05
697 #define TSC10_CMD_DATA1 0x01
699 static int dmc_tsc10_init(struct usbtouch_usb *usbtouch)
701 struct usb_device *dev = interface_to_usbdev(usbtouch->interface);
702 int ret = -ENOMEM;
703 unsigned char *buf;
705 buf = kmalloc(2, GFP_NOIO);
706 if (!buf)
707 goto err_nobuf;
708 /* reset */
709 buf[0] = buf[1] = 0xFF;
710 ret = usb_control_msg(dev, usb_rcvctrlpipe (dev, 0),
711 TSC10_CMD_RESET,
712 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
713 0, 0, buf, 2, USB_CTRL_SET_TIMEOUT);
714 if (ret < 0)
715 goto err_out;
716 if (buf[0] != 0x06) {
717 ret = -ENODEV;
718 goto err_out;
721 /* TSC-25 data sheet specifies a delay after the RESET command */
722 msleep(150);
724 /* set coordinate output rate */
725 buf[0] = buf[1] = 0xFF;
726 ret = usb_control_msg(dev, usb_rcvctrlpipe (dev, 0),
727 TSC10_CMD_RATE,
728 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
729 TSC10_RATE_150, 0, buf, 2, USB_CTRL_SET_TIMEOUT);
730 if (ret < 0)
731 goto err_out;
732 if ((buf[0] != 0x06) && (buf[0] != 0x15 || buf[1] != 0x01)) {
733 ret = -ENODEV;
734 goto err_out;
737 /* start sending data */
738 ret = usb_control_msg(dev, usb_rcvctrlpipe (dev, 0),
739 TSC10_CMD_DATA1,
740 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
741 0, 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
742 err_out:
743 kfree(buf);
744 err_nobuf:
745 return ret;
749 static int dmc_tsc10_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
751 dev->x = ((pkt[2] & 0x03) << 8) | pkt[1];
752 dev->y = ((pkt[4] & 0x03) << 8) | pkt[3];
753 dev->touch = pkt[0] & 0x01;
755 return 1;
757 #endif
760 /*****************************************************************************
761 * IRTOUCH Part
763 #ifdef CONFIG_TOUCHSCREEN_USB_IRTOUCH
764 static int irtouch_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
766 dev->x = (pkt[3] << 8) | pkt[2];
767 dev->y = (pkt[5] << 8) | pkt[4];
768 dev->touch = (pkt[1] & 0x03) ? 1 : 0;
770 return 1;
772 #endif
774 /*****************************************************************************
775 * ET&T TC5UH/TC4UM part
777 #ifdef CONFIG_TOUCHSCREEN_USB_ETT_TC45USB
778 static int tc45usb_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
780 dev->x = ((pkt[2] & 0x0F) << 8) | pkt[1];
781 dev->y = ((pkt[4] & 0x0F) << 8) | pkt[3];
782 dev->touch = pkt[0] & 0x01;
784 return 1;
786 #endif
788 /*****************************************************************************
789 * IdealTEK URTC1000 Part
791 #ifdef CONFIG_TOUCHSCREEN_USB_IDEALTEK
792 #ifndef MULTI_PACKET
793 #define MULTI_PACKET
794 #endif
795 static int idealtek_get_pkt_len(unsigned char *buf, int len)
797 if (buf[0] & 0x80)
798 return 5;
799 if (buf[0] == 0x01)
800 return len;
801 return 0;
804 static int idealtek_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
806 switch (pkt[0] & 0x98) {
807 case 0x88:
808 /* touch data in IdealTEK mode */
809 dev->x = (pkt[1] << 5) | (pkt[2] >> 2);
810 dev->y = (pkt[3] << 5) | (pkt[4] >> 2);
811 dev->touch = (pkt[0] & 0x40) ? 1 : 0;
812 return 1;
814 case 0x98:
815 /* touch data in MT emulation mode */
816 dev->x = (pkt[2] << 5) | (pkt[1] >> 2);
817 dev->y = (pkt[4] << 5) | (pkt[3] >> 2);
818 dev->touch = (pkt[0] & 0x40) ? 1 : 0;
819 return 1;
821 default:
822 return 0;
825 #endif
827 /*****************************************************************************
828 * General Touch Part
830 #ifdef CONFIG_TOUCHSCREEN_USB_GENERAL_TOUCH
831 static int general_touch_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
833 dev->x = (pkt[2] << 8) | pkt[1];
834 dev->y = (pkt[4] << 8) | pkt[3];
835 dev->press = pkt[5] & 0xff;
836 dev->touch = pkt[0] & 0x01;
838 return 1;
840 #endif
842 /*****************************************************************************
843 * GoTop Part
845 #ifdef CONFIG_TOUCHSCREEN_USB_GOTOP
846 static int gotop_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
848 dev->x = ((pkt[1] & 0x38) << 4) | pkt[2];
849 dev->y = ((pkt[1] & 0x07) << 7) | pkt[3];
850 dev->touch = pkt[0] & 0x01;
852 return 1;
854 #endif
856 /*****************************************************************************
857 * JASTEC Part
859 #ifdef CONFIG_TOUCHSCREEN_USB_JASTEC
860 static int jastec_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
862 dev->x = ((pkt[0] & 0x3f) << 6) | (pkt[2] & 0x3f);
863 dev->y = ((pkt[1] & 0x3f) << 6) | (pkt[3] & 0x3f);
864 dev->touch = (pkt[0] & 0x40) >> 6;
866 return 1;
868 #endif
870 /*****************************************************************************
871 * Zytronic Part
873 #ifdef CONFIG_TOUCHSCREEN_USB_ZYTRONIC
874 static int zytronic_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
876 struct usb_interface *intf = dev->interface;
878 switch (pkt[0]) {
879 case 0x3A: /* command response */
880 dev_dbg(&intf->dev, "%s: Command response %d\n", __func__, pkt[1]);
881 break;
883 case 0xC0: /* down */
884 dev->x = (pkt[1] & 0x7f) | ((pkt[2] & 0x07) << 7);
885 dev->y = (pkt[3] & 0x7f) | ((pkt[4] & 0x07) << 7);
886 dev->touch = 1;
887 dev_dbg(&intf->dev, "%s: down %d,%d\n", __func__, dev->x, dev->y);
888 return 1;
890 case 0x80: /* up */
891 dev->x = (pkt[1] & 0x7f) | ((pkt[2] & 0x07) << 7);
892 dev->y = (pkt[3] & 0x7f) | ((pkt[4] & 0x07) << 7);
893 dev->touch = 0;
894 dev_dbg(&intf->dev, "%s: up %d,%d\n", __func__, dev->x, dev->y);
895 return 1;
897 default:
898 dev_dbg(&intf->dev, "%s: Unknown return %d\n", __func__, pkt[0]);
899 break;
902 return 0;
904 #endif
906 /*****************************************************************************
907 * NEXIO Part
909 #ifdef CONFIG_TOUCHSCREEN_USB_NEXIO
911 #define NEXIO_TIMEOUT 5000
912 #define NEXIO_BUFSIZE 1024
913 #define NEXIO_THRESHOLD 50
915 struct nexio_priv {
916 struct urb *ack;
917 unsigned char *ack_buf;
920 struct nexio_touch_packet {
921 u8 flags; /* 0xe1 = touch, 0xe1 = release */
922 __be16 data_len; /* total bytes of touch data */
923 __be16 x_len; /* bytes for X axis */
924 __be16 y_len; /* bytes for Y axis */
925 u8 data[];
926 } __attribute__ ((packed));
928 static unsigned char nexio_ack_pkt[2] = { 0xaa, 0x02 };
929 static unsigned char nexio_init_pkt[4] = { 0x82, 0x04, 0x0a, 0x0f };
931 static void nexio_ack_complete(struct urb *urb)
935 static int nexio_alloc(struct usbtouch_usb *usbtouch)
937 struct nexio_priv *priv;
938 int ret = -ENOMEM;
940 usbtouch->priv = kmalloc(sizeof(struct nexio_priv), GFP_KERNEL);
941 if (!usbtouch->priv)
942 goto out_buf;
944 priv = usbtouch->priv;
946 priv->ack_buf = kmemdup(nexio_ack_pkt, sizeof(nexio_ack_pkt),
947 GFP_KERNEL);
948 if (!priv->ack_buf)
949 goto err_priv;
951 priv->ack = usb_alloc_urb(0, GFP_KERNEL);
952 if (!priv->ack) {
953 dev_dbg(&usbtouch->interface->dev,
954 "%s - usb_alloc_urb failed: usbtouch->ack\n", __func__);
955 goto err_ack_buf;
958 return 0;
960 err_ack_buf:
961 kfree(priv->ack_buf);
962 err_priv:
963 kfree(priv);
964 out_buf:
965 return ret;
968 static int nexio_init(struct usbtouch_usb *usbtouch)
970 struct usb_device *dev = interface_to_usbdev(usbtouch->interface);
971 struct usb_host_interface *interface = usbtouch->interface->cur_altsetting;
972 struct nexio_priv *priv = usbtouch->priv;
973 int ret = -ENOMEM;
974 int actual_len, i;
975 unsigned char *buf;
976 char *firmware_ver = NULL, *device_name = NULL;
977 int input_ep = 0, output_ep = 0;
979 /* find first input and output endpoint */
980 for (i = 0; i < interface->desc.bNumEndpoints; i++) {
981 if (!input_ep &&
982 usb_endpoint_dir_in(&interface->endpoint[i].desc))
983 input_ep = interface->endpoint[i].desc.bEndpointAddress;
984 if (!output_ep &&
985 usb_endpoint_dir_out(&interface->endpoint[i].desc))
986 output_ep = interface->endpoint[i].desc.bEndpointAddress;
988 if (!input_ep || !output_ep)
989 return -ENXIO;
991 buf = kmalloc(NEXIO_BUFSIZE, GFP_NOIO);
992 if (!buf)
993 goto out_buf;
995 /* two empty reads */
996 for (i = 0; i < 2; i++) {
997 ret = usb_bulk_msg(dev, usb_rcvbulkpipe(dev, input_ep),
998 buf, NEXIO_BUFSIZE, &actual_len,
999 NEXIO_TIMEOUT);
1000 if (ret < 0)
1001 goto out_buf;
1004 /* send init command */
1005 memcpy(buf, nexio_init_pkt, sizeof(nexio_init_pkt));
1006 ret = usb_bulk_msg(dev, usb_sndbulkpipe(dev, output_ep),
1007 buf, sizeof(nexio_init_pkt), &actual_len,
1008 NEXIO_TIMEOUT);
1009 if (ret < 0)
1010 goto out_buf;
1012 /* read replies */
1013 for (i = 0; i < 3; i++) {
1014 memset(buf, 0, NEXIO_BUFSIZE);
1015 ret = usb_bulk_msg(dev, usb_rcvbulkpipe(dev, input_ep),
1016 buf, NEXIO_BUFSIZE, &actual_len,
1017 NEXIO_TIMEOUT);
1018 if (ret < 0 || actual_len < 1 || buf[1] != actual_len)
1019 continue;
1020 switch (buf[0]) {
1021 case 0x83: /* firmware version */
1022 if (!firmware_ver)
1023 firmware_ver = kstrdup(&buf[2], GFP_NOIO);
1024 break;
1025 case 0x84: /* device name */
1026 if (!device_name)
1027 device_name = kstrdup(&buf[2], GFP_NOIO);
1028 break;
1032 printk(KERN_INFO "Nexio device: %s, firmware version: %s\n",
1033 device_name, firmware_ver);
1035 kfree(firmware_ver);
1036 kfree(device_name);
1038 usb_fill_bulk_urb(priv->ack, dev, usb_sndbulkpipe(dev, output_ep),
1039 priv->ack_buf, sizeof(nexio_ack_pkt),
1040 nexio_ack_complete, usbtouch);
1041 ret = 0;
1043 out_buf:
1044 kfree(buf);
1045 return ret;
1048 static void nexio_exit(struct usbtouch_usb *usbtouch)
1050 struct nexio_priv *priv = usbtouch->priv;
1052 usb_kill_urb(priv->ack);
1053 usb_free_urb(priv->ack);
1054 kfree(priv->ack_buf);
1055 kfree(priv);
1058 static int nexio_read_data(struct usbtouch_usb *usbtouch, unsigned char *pkt)
1060 struct nexio_touch_packet *packet = (void *) pkt;
1061 struct nexio_priv *priv = usbtouch->priv;
1062 unsigned int data_len = be16_to_cpu(packet->data_len);
1063 unsigned int x_len = be16_to_cpu(packet->x_len);
1064 unsigned int y_len = be16_to_cpu(packet->y_len);
1065 int x, y, begin_x, begin_y, end_x, end_y, w, h, ret;
1067 /* got touch data? */
1068 if ((pkt[0] & 0xe0) != 0xe0)
1069 return 0;
1071 if (data_len > 0xff)
1072 data_len -= 0x100;
1073 if (x_len > 0xff)
1074 x_len -= 0x80;
1076 /* send ACK */
1077 ret = usb_submit_urb(priv->ack, GFP_ATOMIC);
1079 if (!usbtouch->type->max_xc) {
1080 usbtouch->type->max_xc = 2 * x_len;
1081 input_set_abs_params(usbtouch->input, ABS_X,
1082 0, usbtouch->type->max_xc, 0, 0);
1083 usbtouch->type->max_yc = 2 * y_len;
1084 input_set_abs_params(usbtouch->input, ABS_Y,
1085 0, usbtouch->type->max_yc, 0, 0);
1088 * The device reports state of IR sensors on X and Y axes.
1089 * Each byte represents "darkness" percentage (0-100) of one element.
1090 * 17" touchscreen reports only 64 x 52 bytes so the resolution is low.
1091 * This also means that there's a limited multi-touch capability but
1092 * it's disabled (and untested) here as there's no X driver for that.
1094 begin_x = end_x = begin_y = end_y = -1;
1095 for (x = 0; x < x_len; x++) {
1096 if (begin_x == -1 && packet->data[x] > NEXIO_THRESHOLD) {
1097 begin_x = x;
1098 continue;
1100 if (end_x == -1 && begin_x != -1 && packet->data[x] < NEXIO_THRESHOLD) {
1101 end_x = x - 1;
1102 for (y = x_len; y < data_len; y++) {
1103 if (begin_y == -1 && packet->data[y] > NEXIO_THRESHOLD) {
1104 begin_y = y - x_len;
1105 continue;
1107 if (end_y == -1 &&
1108 begin_y != -1 && packet->data[y] < NEXIO_THRESHOLD) {
1109 end_y = y - 1 - x_len;
1110 w = end_x - begin_x;
1111 h = end_y - begin_y;
1112 #if 0
1113 /* multi-touch */
1114 input_report_abs(usbtouch->input,
1115 ABS_MT_TOUCH_MAJOR, max(w,h));
1116 input_report_abs(usbtouch->input,
1117 ABS_MT_TOUCH_MINOR, min(x,h));
1118 input_report_abs(usbtouch->input,
1119 ABS_MT_POSITION_X, 2*begin_x+w);
1120 input_report_abs(usbtouch->input,
1121 ABS_MT_POSITION_Y, 2*begin_y+h);
1122 input_report_abs(usbtouch->input,
1123 ABS_MT_ORIENTATION, w > h);
1124 input_mt_sync(usbtouch->input);
1125 #endif
1126 /* single touch */
1127 usbtouch->x = 2 * begin_x + w;
1128 usbtouch->y = 2 * begin_y + h;
1129 usbtouch->touch = packet->flags & 0x01;
1130 begin_y = end_y = -1;
1131 return 1;
1134 begin_x = end_x = -1;
1138 return 0;
1140 #endif
1143 /*****************************************************************************
1144 * ELO part
1147 #ifdef CONFIG_TOUCHSCREEN_USB_ELO
1149 static int elo_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
1151 dev->x = (pkt[3] << 8) | pkt[2];
1152 dev->y = (pkt[5] << 8) | pkt[4];
1153 dev->touch = pkt[6] > 0;
1154 dev->press = pkt[6];
1156 return 1;
1158 #endif
1161 /*****************************************************************************
1162 * the different device descriptors
1164 #ifdef MULTI_PACKET
1165 static void usbtouch_process_multi(struct usbtouch_usb *usbtouch,
1166 unsigned char *pkt, int len);
1167 #endif
1169 static struct usbtouch_device_info usbtouch_dev_info[] = {
1170 #ifdef CONFIG_TOUCHSCREEN_USB_ELO
1171 [DEVTYPE_ELO] = {
1172 .min_xc = 0x0,
1173 .max_xc = 0x0fff,
1174 .min_yc = 0x0,
1175 .max_yc = 0x0fff,
1176 .max_press = 0xff,
1177 .rept_size = 8,
1178 .read_data = elo_read_data,
1180 #endif
1182 #ifdef CONFIG_TOUCHSCREEN_USB_EGALAX
1183 [DEVTYPE_EGALAX] = {
1184 .min_xc = 0x0,
1185 .max_xc = 0x07ff,
1186 .min_yc = 0x0,
1187 .max_yc = 0x07ff,
1188 .rept_size = 16,
1189 .process_pkt = usbtouch_process_multi,
1190 .get_pkt_len = egalax_get_pkt_len,
1191 .read_data = egalax_read_data,
1192 .init = egalax_init,
1194 #endif
1196 #ifdef CONFIG_TOUCHSCREEN_USB_PANJIT
1197 [DEVTYPE_PANJIT] = {
1198 .min_xc = 0x0,
1199 .max_xc = 0x0fff,
1200 .min_yc = 0x0,
1201 .max_yc = 0x0fff,
1202 .rept_size = 8,
1203 .read_data = panjit_read_data,
1205 #endif
1207 #ifdef CONFIG_TOUCHSCREEN_USB_3M
1208 [DEVTYPE_3M] = {
1209 .min_xc = 0x0,
1210 .max_xc = 0x4000,
1211 .min_yc = 0x0,
1212 .max_yc = 0x4000,
1213 .rept_size = 11,
1214 .read_data = mtouch_read_data,
1215 .alloc = mtouch_alloc,
1216 .init = mtouch_init,
1217 .exit = mtouch_exit,
1219 #endif
1221 #ifdef CONFIG_TOUCHSCREEN_USB_ITM
1222 [DEVTYPE_ITM] = {
1223 .min_xc = 0x0,
1224 .max_xc = 0x0fff,
1225 .min_yc = 0x0,
1226 .max_yc = 0x0fff,
1227 .max_press = 0xff,
1228 .rept_size = 8,
1229 .read_data = itm_read_data,
1231 #endif
1233 #ifdef CONFIG_TOUCHSCREEN_USB_ETURBO
1234 [DEVTYPE_ETURBO] = {
1235 .min_xc = 0x0,
1236 .max_xc = 0x07ff,
1237 .min_yc = 0x0,
1238 .max_yc = 0x07ff,
1239 .rept_size = 8,
1240 .process_pkt = usbtouch_process_multi,
1241 .get_pkt_len = eturbo_get_pkt_len,
1242 .read_data = eturbo_read_data,
1244 #endif
1246 #ifdef CONFIG_TOUCHSCREEN_USB_GUNZE
1247 [DEVTYPE_GUNZE] = {
1248 .min_xc = 0x0,
1249 .max_xc = 0x0fff,
1250 .min_yc = 0x0,
1251 .max_yc = 0x0fff,
1252 .rept_size = 4,
1253 .read_data = gunze_read_data,
1255 #endif
1257 #ifdef CONFIG_TOUCHSCREEN_USB_DMC_TSC10
1258 [DEVTYPE_DMC_TSC10] = {
1259 .min_xc = 0x0,
1260 .max_xc = 0x03ff,
1261 .min_yc = 0x0,
1262 .max_yc = 0x03ff,
1263 .rept_size = 5,
1264 .init = dmc_tsc10_init,
1265 .read_data = dmc_tsc10_read_data,
1267 #endif
1269 #ifdef CONFIG_TOUCHSCREEN_USB_IRTOUCH
1270 [DEVTYPE_IRTOUCH] = {
1271 .min_xc = 0x0,
1272 .max_xc = 0x0fff,
1273 .min_yc = 0x0,
1274 .max_yc = 0x0fff,
1275 .rept_size = 8,
1276 .read_data = irtouch_read_data,
1279 [DEVTYPE_IRTOUCH_HIRES] = {
1280 .min_xc = 0x0,
1281 .max_xc = 0x7fff,
1282 .min_yc = 0x0,
1283 .max_yc = 0x7fff,
1284 .rept_size = 8,
1285 .read_data = irtouch_read_data,
1287 #endif
1289 #ifdef CONFIG_TOUCHSCREEN_USB_IDEALTEK
1290 [DEVTYPE_IDEALTEK] = {
1291 .min_xc = 0x0,
1292 .max_xc = 0x0fff,
1293 .min_yc = 0x0,
1294 .max_yc = 0x0fff,
1295 .rept_size = 8,
1296 .process_pkt = usbtouch_process_multi,
1297 .get_pkt_len = idealtek_get_pkt_len,
1298 .read_data = idealtek_read_data,
1300 #endif
1302 #ifdef CONFIG_TOUCHSCREEN_USB_GENERAL_TOUCH
1303 [DEVTYPE_GENERAL_TOUCH] = {
1304 .min_xc = 0x0,
1305 .max_xc = 0x7fff,
1306 .min_yc = 0x0,
1307 .max_yc = 0x7fff,
1308 .rept_size = 7,
1309 .read_data = general_touch_read_data,
1311 #endif
1313 #ifdef CONFIG_TOUCHSCREEN_USB_GOTOP
1314 [DEVTYPE_GOTOP] = {
1315 .min_xc = 0x0,
1316 .max_xc = 0x03ff,
1317 .min_yc = 0x0,
1318 .max_yc = 0x03ff,
1319 .rept_size = 4,
1320 .read_data = gotop_read_data,
1322 #endif
1324 #ifdef CONFIG_TOUCHSCREEN_USB_JASTEC
1325 [DEVTYPE_JASTEC] = {
1326 .min_xc = 0x0,
1327 .max_xc = 0x0fff,
1328 .min_yc = 0x0,
1329 .max_yc = 0x0fff,
1330 .rept_size = 4,
1331 .read_data = jastec_read_data,
1333 #endif
1335 #ifdef CONFIG_TOUCHSCREEN_USB_E2I
1336 [DEVTYPE_E2I] = {
1337 .min_xc = 0x0,
1338 .max_xc = 0x7fff,
1339 .min_yc = 0x0,
1340 .max_yc = 0x7fff,
1341 .rept_size = 6,
1342 .init = e2i_init,
1343 .read_data = e2i_read_data,
1345 #endif
1347 #ifdef CONFIG_TOUCHSCREEN_USB_ZYTRONIC
1348 [DEVTYPE_ZYTRONIC] = {
1349 .min_xc = 0x0,
1350 .max_xc = 0x03ff,
1351 .min_yc = 0x0,
1352 .max_yc = 0x03ff,
1353 .rept_size = 5,
1354 .read_data = zytronic_read_data,
1355 .irq_always = true,
1357 #endif
1359 #ifdef CONFIG_TOUCHSCREEN_USB_ETT_TC45USB
1360 [DEVTYPE_TC45USB] = {
1361 .min_xc = 0x0,
1362 .max_xc = 0x0fff,
1363 .min_yc = 0x0,
1364 .max_yc = 0x0fff,
1365 .rept_size = 5,
1366 .read_data = tc45usb_read_data,
1368 #endif
1370 #ifdef CONFIG_TOUCHSCREEN_USB_NEXIO
1371 [DEVTYPE_NEXIO] = {
1372 .rept_size = 1024,
1373 .irq_always = true,
1374 .read_data = nexio_read_data,
1375 .alloc = nexio_alloc,
1376 .init = nexio_init,
1377 .exit = nexio_exit,
1379 #endif
1380 #ifdef CONFIG_TOUCHSCREEN_USB_EASYTOUCH
1381 [DEVTYPE_ETOUCH] = {
1382 .min_xc = 0x0,
1383 .max_xc = 0x07ff,
1384 .min_yc = 0x0,
1385 .max_yc = 0x07ff,
1386 .rept_size = 16,
1387 .process_pkt = usbtouch_process_multi,
1388 .get_pkt_len = etouch_get_pkt_len,
1389 .read_data = etouch_read_data,
1391 #endif
1395 /*****************************************************************************
1396 * Generic Part
1398 static void usbtouch_process_pkt(struct usbtouch_usb *usbtouch,
1399 unsigned char *pkt, int len)
1401 struct usbtouch_device_info *type = usbtouch->type;
1403 if (!type->read_data(usbtouch, pkt))
1404 return;
1406 input_report_key(usbtouch->input, BTN_TOUCH, usbtouch->touch);
1408 if (swap_xy) {
1409 input_report_abs(usbtouch->input, ABS_X, usbtouch->y);
1410 input_report_abs(usbtouch->input, ABS_Y, usbtouch->x);
1411 } else {
1412 input_report_abs(usbtouch->input, ABS_X, usbtouch->x);
1413 input_report_abs(usbtouch->input, ABS_Y, usbtouch->y);
1415 if (type->max_press)
1416 input_report_abs(usbtouch->input, ABS_PRESSURE, usbtouch->press);
1417 input_sync(usbtouch->input);
1421 #ifdef MULTI_PACKET
1422 static void usbtouch_process_multi(struct usbtouch_usb *usbtouch,
1423 unsigned char *pkt, int len)
1425 unsigned char *buffer;
1426 int pkt_len, pos, buf_len, tmp;
1428 /* process buffer */
1429 if (unlikely(usbtouch->buf_len)) {
1430 /* try to get size */
1431 pkt_len = usbtouch->type->get_pkt_len(
1432 usbtouch->buffer, usbtouch->buf_len);
1434 /* drop? */
1435 if (unlikely(!pkt_len))
1436 goto out_flush_buf;
1438 /* need to append -pkt_len bytes before able to get size */
1439 if (unlikely(pkt_len < 0)) {
1440 int append = -pkt_len;
1441 if (unlikely(append > len))
1442 append = len;
1443 if (usbtouch->buf_len + append >= usbtouch->type->rept_size)
1444 goto out_flush_buf;
1445 memcpy(usbtouch->buffer + usbtouch->buf_len, pkt, append);
1446 usbtouch->buf_len += append;
1448 pkt_len = usbtouch->type->get_pkt_len(
1449 usbtouch->buffer, usbtouch->buf_len);
1450 if (pkt_len < 0)
1451 return;
1454 /* append */
1455 tmp = pkt_len - usbtouch->buf_len;
1456 if (usbtouch->buf_len + tmp >= usbtouch->type->rept_size)
1457 goto out_flush_buf;
1458 memcpy(usbtouch->buffer + usbtouch->buf_len, pkt, tmp);
1459 usbtouch_process_pkt(usbtouch, usbtouch->buffer, pkt_len);
1461 buffer = pkt + tmp;
1462 buf_len = len - tmp;
1463 } else {
1464 buffer = pkt;
1465 buf_len = len;
1468 /* loop over the received packet, process */
1469 pos = 0;
1470 while (pos < buf_len) {
1471 /* get packet len */
1472 pkt_len = usbtouch->type->get_pkt_len(buffer + pos,
1473 buf_len - pos);
1475 /* unknown packet: skip one byte */
1476 if (unlikely(!pkt_len)) {
1477 pos++;
1478 continue;
1481 /* full packet: process */
1482 if (likely((pkt_len > 0) && (pkt_len <= buf_len - pos))) {
1483 usbtouch_process_pkt(usbtouch, buffer + pos, pkt_len);
1484 } else {
1485 /* incomplete packet: save in buffer */
1486 memcpy(usbtouch->buffer, buffer + pos, buf_len - pos);
1487 usbtouch->buf_len = buf_len - pos;
1488 return;
1490 pos += pkt_len;
1493 out_flush_buf:
1494 usbtouch->buf_len = 0;
1495 return;
1497 #endif
1500 static void usbtouch_irq(struct urb *urb)
1502 struct usbtouch_usb *usbtouch = urb->context;
1503 struct device *dev = &usbtouch->interface->dev;
1504 int retval;
1506 switch (urb->status) {
1507 case 0:
1508 /* success */
1509 break;
1510 case -ETIME:
1511 /* this urb is timing out */
1512 dev_dbg(dev,
1513 "%s - urb timed out - was the device unplugged?\n",
1514 __func__);
1515 return;
1516 case -ECONNRESET:
1517 case -ENOENT:
1518 case -ESHUTDOWN:
1519 case -EPIPE:
1520 /* this urb is terminated, clean up */
1521 dev_dbg(dev, "%s - urb shutting down with status: %d\n",
1522 __func__, urb->status);
1523 return;
1524 default:
1525 dev_dbg(dev, "%s - nonzero urb status received: %d\n",
1526 __func__, urb->status);
1527 goto exit;
1530 usbtouch->type->process_pkt(usbtouch, usbtouch->data, urb->actual_length);
1532 exit:
1533 usb_mark_last_busy(interface_to_usbdev(usbtouch->interface));
1534 retval = usb_submit_urb(urb, GFP_ATOMIC);
1535 if (retval)
1536 dev_err(dev, "%s - usb_submit_urb failed with result: %d\n",
1537 __func__, retval);
1540 static int usbtouch_open(struct input_dev *input)
1542 struct usbtouch_usb *usbtouch = input_get_drvdata(input);
1543 int r;
1545 usbtouch->irq->dev = interface_to_usbdev(usbtouch->interface);
1547 r = usb_autopm_get_interface(usbtouch->interface) ? -EIO : 0;
1548 if (r < 0)
1549 goto out;
1551 mutex_lock(&usbtouch->pm_mutex);
1552 if (!usbtouch->type->irq_always) {
1553 if (usb_submit_urb(usbtouch->irq, GFP_KERNEL)) {
1554 r = -EIO;
1555 goto out_put;
1559 usbtouch->interface->needs_remote_wakeup = 1;
1560 usbtouch->is_open = true;
1561 out_put:
1562 mutex_unlock(&usbtouch->pm_mutex);
1563 usb_autopm_put_interface(usbtouch->interface);
1564 out:
1565 return r;
1568 static void usbtouch_close(struct input_dev *input)
1570 struct usbtouch_usb *usbtouch = input_get_drvdata(input);
1571 int r;
1573 mutex_lock(&usbtouch->pm_mutex);
1574 if (!usbtouch->type->irq_always)
1575 usb_kill_urb(usbtouch->irq);
1576 usbtouch->is_open = false;
1577 mutex_unlock(&usbtouch->pm_mutex);
1579 r = usb_autopm_get_interface(usbtouch->interface);
1580 usbtouch->interface->needs_remote_wakeup = 0;
1581 if (!r)
1582 usb_autopm_put_interface(usbtouch->interface);
1585 static int usbtouch_suspend
1586 (struct usb_interface *intf, pm_message_t message)
1588 struct usbtouch_usb *usbtouch = usb_get_intfdata(intf);
1590 usb_kill_urb(usbtouch->irq);
1592 return 0;
1595 static int usbtouch_resume(struct usb_interface *intf)
1597 struct usbtouch_usb *usbtouch = usb_get_intfdata(intf);
1598 int result = 0;
1600 mutex_lock(&usbtouch->pm_mutex);
1601 if (usbtouch->is_open || usbtouch->type->irq_always)
1602 result = usb_submit_urb(usbtouch->irq, GFP_NOIO);
1603 mutex_unlock(&usbtouch->pm_mutex);
1605 return result;
1608 static int usbtouch_reset_resume(struct usb_interface *intf)
1610 struct usbtouch_usb *usbtouch = usb_get_intfdata(intf);
1611 int err = 0;
1613 /* reinit the device */
1614 if (usbtouch->type->init) {
1615 err = usbtouch->type->init(usbtouch);
1616 if (err) {
1617 dev_dbg(&intf->dev,
1618 "%s - type->init() failed, err: %d\n",
1619 __func__, err);
1620 return err;
1624 /* restart IO if needed */
1625 mutex_lock(&usbtouch->pm_mutex);
1626 if (usbtouch->is_open)
1627 err = usb_submit_urb(usbtouch->irq, GFP_NOIO);
1628 mutex_unlock(&usbtouch->pm_mutex);
1630 return err;
1633 static void usbtouch_free_buffers(struct usb_device *udev,
1634 struct usbtouch_usb *usbtouch)
1636 usb_free_coherent(udev, usbtouch->data_size,
1637 usbtouch->data, usbtouch->data_dma);
1638 kfree(usbtouch->buffer);
1641 static struct usb_endpoint_descriptor *
1642 usbtouch_get_input_endpoint(struct usb_host_interface *interface)
1644 int i;
1646 for (i = 0; i < interface->desc.bNumEndpoints; i++)
1647 if (usb_endpoint_dir_in(&interface->endpoint[i].desc))
1648 return &interface->endpoint[i].desc;
1650 return NULL;
1653 static int usbtouch_probe(struct usb_interface *intf,
1654 const struct usb_device_id *id)
1656 struct usbtouch_usb *usbtouch;
1657 struct input_dev *input_dev;
1658 struct usb_endpoint_descriptor *endpoint;
1659 struct usb_device *udev = interface_to_usbdev(intf);
1660 struct usbtouch_device_info *type;
1661 int err = -ENOMEM;
1663 /* some devices are ignored */
1664 if (id->driver_info == DEVTYPE_IGNORE)
1665 return -ENODEV;
1667 endpoint = usbtouch_get_input_endpoint(intf->cur_altsetting);
1668 if (!endpoint)
1669 return -ENXIO;
1671 usbtouch = kzalloc(sizeof(struct usbtouch_usb), GFP_KERNEL);
1672 input_dev = input_allocate_device();
1673 if (!usbtouch || !input_dev)
1674 goto out_free;
1676 mutex_init(&usbtouch->pm_mutex);
1678 type = &usbtouch_dev_info[id->driver_info];
1679 usbtouch->type = type;
1680 if (!type->process_pkt)
1681 type->process_pkt = usbtouch_process_pkt;
1683 usbtouch->data_size = type->rept_size;
1684 if (type->get_pkt_len) {
1686 * When dealing with variable-length packets we should
1687 * not request more than wMaxPacketSize bytes at once
1688 * as we do not know if there is more data coming or
1689 * we filled exactly wMaxPacketSize bytes and there is
1690 * nothing else.
1692 usbtouch->data_size = min(usbtouch->data_size,
1693 usb_endpoint_maxp(endpoint));
1696 usbtouch->data = usb_alloc_coherent(udev, usbtouch->data_size,
1697 GFP_KERNEL, &usbtouch->data_dma);
1698 if (!usbtouch->data)
1699 goto out_free;
1701 if (type->get_pkt_len) {
1702 usbtouch->buffer = kmalloc(type->rept_size, GFP_KERNEL);
1703 if (!usbtouch->buffer)
1704 goto out_free_buffers;
1707 usbtouch->irq = usb_alloc_urb(0, GFP_KERNEL);
1708 if (!usbtouch->irq) {
1709 dev_dbg(&intf->dev,
1710 "%s - usb_alloc_urb failed: usbtouch->irq\n", __func__);
1711 goto out_free_buffers;
1714 usbtouch->interface = intf;
1715 usbtouch->input = input_dev;
1717 if (udev->manufacturer)
1718 strlcpy(usbtouch->name, udev->manufacturer, sizeof(usbtouch->name));
1720 if (udev->product) {
1721 if (udev->manufacturer)
1722 strlcat(usbtouch->name, " ", sizeof(usbtouch->name));
1723 strlcat(usbtouch->name, udev->product, sizeof(usbtouch->name));
1726 if (!strlen(usbtouch->name))
1727 snprintf(usbtouch->name, sizeof(usbtouch->name),
1728 "USB Touchscreen %04x:%04x",
1729 le16_to_cpu(udev->descriptor.idVendor),
1730 le16_to_cpu(udev->descriptor.idProduct));
1732 usb_make_path(udev, usbtouch->phys, sizeof(usbtouch->phys));
1733 strlcat(usbtouch->phys, "/input0", sizeof(usbtouch->phys));
1735 input_dev->name = usbtouch->name;
1736 input_dev->phys = usbtouch->phys;
1737 usb_to_input_id(udev, &input_dev->id);
1738 input_dev->dev.parent = &intf->dev;
1740 input_set_drvdata(input_dev, usbtouch);
1742 input_dev->open = usbtouch_open;
1743 input_dev->close = usbtouch_close;
1745 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
1746 input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
1747 input_set_abs_params(input_dev, ABS_X, type->min_xc, type->max_xc, 0, 0);
1748 input_set_abs_params(input_dev, ABS_Y, type->min_yc, type->max_yc, 0, 0);
1749 if (type->max_press)
1750 input_set_abs_params(input_dev, ABS_PRESSURE, type->min_press,
1751 type->max_press, 0, 0);
1753 if (usb_endpoint_type(endpoint) == USB_ENDPOINT_XFER_INT)
1754 usb_fill_int_urb(usbtouch->irq, udev,
1755 usb_rcvintpipe(udev, endpoint->bEndpointAddress),
1756 usbtouch->data, usbtouch->data_size,
1757 usbtouch_irq, usbtouch, endpoint->bInterval);
1758 else
1759 usb_fill_bulk_urb(usbtouch->irq, udev,
1760 usb_rcvbulkpipe(udev, endpoint->bEndpointAddress),
1761 usbtouch->data, usbtouch->data_size,
1762 usbtouch_irq, usbtouch);
1764 usbtouch->irq->dev = udev;
1765 usbtouch->irq->transfer_dma = usbtouch->data_dma;
1766 usbtouch->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1768 /* device specific allocations */
1769 if (type->alloc) {
1770 err = type->alloc(usbtouch);
1771 if (err) {
1772 dev_dbg(&intf->dev,
1773 "%s - type->alloc() failed, err: %d\n",
1774 __func__, err);
1775 goto out_free_urb;
1779 /* device specific initialisation*/
1780 if (type->init) {
1781 err = type->init(usbtouch);
1782 if (err) {
1783 dev_dbg(&intf->dev,
1784 "%s - type->init() failed, err: %d\n",
1785 __func__, err);
1786 goto out_do_exit;
1790 err = input_register_device(usbtouch->input);
1791 if (err) {
1792 dev_dbg(&intf->dev,
1793 "%s - input_register_device failed, err: %d\n",
1794 __func__, err);
1795 goto out_do_exit;
1798 usb_set_intfdata(intf, usbtouch);
1800 if (usbtouch->type->irq_always) {
1801 /* this can't fail */
1802 usb_autopm_get_interface(intf);
1803 err = usb_submit_urb(usbtouch->irq, GFP_KERNEL);
1804 if (err) {
1805 usb_autopm_put_interface(intf);
1806 dev_err(&intf->dev,
1807 "%s - usb_submit_urb failed with result: %d\n",
1808 __func__, err);
1809 goto out_unregister_input;
1813 return 0;
1815 out_unregister_input:
1816 input_unregister_device(input_dev);
1817 input_dev = NULL;
1818 out_do_exit:
1819 if (type->exit)
1820 type->exit(usbtouch);
1821 out_free_urb:
1822 usb_free_urb(usbtouch->irq);
1823 out_free_buffers:
1824 usbtouch_free_buffers(udev, usbtouch);
1825 out_free:
1826 input_free_device(input_dev);
1827 kfree(usbtouch);
1828 return err;
1831 static void usbtouch_disconnect(struct usb_interface *intf)
1833 struct usbtouch_usb *usbtouch = usb_get_intfdata(intf);
1835 if (!usbtouch)
1836 return;
1838 dev_dbg(&intf->dev,
1839 "%s - usbtouch is initialized, cleaning up\n", __func__);
1841 usb_set_intfdata(intf, NULL);
1842 /* this will stop IO via close */
1843 input_unregister_device(usbtouch->input);
1844 usb_free_urb(usbtouch->irq);
1845 if (usbtouch->type->exit)
1846 usbtouch->type->exit(usbtouch);
1847 usbtouch_free_buffers(interface_to_usbdev(intf), usbtouch);
1848 kfree(usbtouch);
1851 MODULE_DEVICE_TABLE(usb, usbtouch_devices);
1853 static struct usb_driver usbtouch_driver = {
1854 .name = "usbtouchscreen",
1855 .probe = usbtouch_probe,
1856 .disconnect = usbtouch_disconnect,
1857 .suspend = usbtouch_suspend,
1858 .resume = usbtouch_resume,
1859 .reset_resume = usbtouch_reset_resume,
1860 .id_table = usbtouch_devices,
1861 .supports_autosuspend = 1,
1864 module_usb_driver(usbtouch_driver);
1866 MODULE_AUTHOR("Daniel Ritz <daniel.ritz@gmx.ch>");
1867 MODULE_DESCRIPTION("USB Touchscreen Driver");
1868 MODULE_LICENSE("GPL");
1870 MODULE_ALIAS("touchkitusb");
1871 MODULE_ALIAS("itmtouch");
1872 MODULE_ALIAS("mtouchusb");