i2c-eg20t: change timeout value 50msec to 1000msec
[zen-stable.git] / drivers / input / touchscreen / usbtouchscreen.c
blob3a5ebf452e810bde7b032650f3e942b3fd6d085e
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
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 *****************************************************************************/
47 //#define DEBUG
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"
63 static bool swap_xy;
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 */
72 struct usbtouch_usb;
73 struct usbtouch_device_info {
74 int min_xc, max_xc;
75 int min_yc, max_yc;
76 int min_press, max_press;
77 int rept_size;
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
84 * bandwidth.
86 bool irq_always;
88 void (*process_pkt) (struct usbtouch_usb *usbtouch, unsigned char *pkt, int len);
91 * used to get the packet len. possible return values:
92 * > 0: packet len
93 * = 0: skip one byte
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 {
106 unsigned char *data;
107 dma_addr_t data_dma;
108 unsigned char *buffer;
109 int buf_len;
110 struct urb *irq;
111 struct usb_interface *interface;
112 struct input_dev *input;
113 struct usbtouch_device_info *type;
114 char name[128];
115 char phys[64];
116 void *priv;
118 int x, y;
119 int touch, press;
123 /* device types */
124 enum {
125 DEVTYPE_IGNORE = -1,
126 DEVTYPE_EGALAX,
127 DEVTYPE_PANJIT,
128 DEVTYPE_3M,
129 DEVTYPE_ITM,
130 DEVTYPE_ETURBO,
131 DEVTYPE_GUNZE,
132 DEVTYPE_DMC_TSC10,
133 DEVTYPE_IRTOUCH,
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,
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},
168 #endif
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},
175 #endif
177 #ifdef CONFIG_TOUCHSCREEN_USB_3M
178 {USB_DEVICE(0x0596, 0x0001), .driver_info = DEVTYPE_3M},
179 #endif
181 #ifdef CONFIG_TOUCHSCREEN_USB_ITM
182 {USB_DEVICE(0x0403, 0xf9e9), .driver_info = DEVTYPE_ITM},
183 {USB_DEVICE(0x16e3, 0xf9e9), .driver_info = DEVTYPE_ITM},
184 #endif
186 #ifdef CONFIG_TOUCHSCREEN_USB_ETURBO
187 {USB_DEVICE(0x1234, 0x5678), .driver_info = DEVTYPE_ETURBO},
188 #endif
190 #ifdef CONFIG_TOUCHSCREEN_USB_GUNZE
191 {USB_DEVICE(0x0637, 0x0001), .driver_info = DEVTYPE_GUNZE},
192 #endif
194 #ifdef CONFIG_TOUCHSCREEN_USB_DMC_TSC10
195 {USB_DEVICE(0x0afa, 0x03e8), .driver_info = DEVTYPE_DMC_TSC10},
196 #endif
198 #ifdef CONFIG_TOUCHSCREEN_USB_IRTOUCH
199 {USB_DEVICE(0x595a, 0x0001), .driver_info = DEVTYPE_IRTOUCH},
200 {USB_DEVICE(0x6615, 0x0001), .driver_info = DEVTYPE_IRTOUCH},
201 #endif
203 #ifdef CONFIG_TOUCHSCREEN_USB_IDEALTEK
204 {USB_DEVICE(0x1391, 0x1000), .driver_info = DEVTYPE_IDEALTEK},
205 #endif
207 #ifdef CONFIG_TOUCHSCREEN_USB_GENERAL_TOUCH
208 {USB_DEVICE(0x0dfc, 0x0001), .driver_info = DEVTYPE_GENERAL_TOUCH},
209 #endif
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},
215 #endif
217 #ifdef CONFIG_TOUCHSCREEN_USB_JASTEC
218 {USB_DEVICE(0x0f92, 0x0001), .driver_info = DEVTYPE_JASTEC},
219 #endif
221 #ifdef CONFIG_TOUCHSCREEN_USB_E2I
222 {USB_DEVICE(0x1ac7, 0x0001), .driver_info = DEVTYPE_E2I},
223 #endif
225 #ifdef CONFIG_TOUCHSCREEN_USB_ZYTRONIC
226 {USB_DEVICE(0x14c8, 0x0003), .driver_info = DEVTYPE_ZYTRONIC},
227 #endif
229 #ifdef CONFIG_TOUCHSCREEN_USB_ETT_TC45USB
230 /* TC5UH */
231 {USB_DEVICE(0x0664, 0x0309), .driver_info = DEVTYPE_TC45USB},
232 /* TC4UM */
233 {USB_DEVICE(0x0664, 0x0306), .driver_info = DEVTYPE_TC45USB},
234 #endif
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},
242 #endif
244 #ifdef CONFIG_TOUCHSCREEN_USB_ELO
245 {USB_DEVICE(0x04e7, 0x0020), .driver_info = DEVTYPE_ELO},
246 #endif
252 /*****************************************************************************
253 * e2i Part
256 #ifdef CONFIG_TOUCHSCREEN_USB_E2I
257 static int e2i_init(struct usbtouch_usb *usbtouch)
259 int ret;
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",
267 __func__, ret);
268 return ret;
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];
277 tmp = tmp - 0xA000;
278 dev->touch = (tmp > 0);
279 dev->press = (tmp > 0 ? tmp : 0);
281 return 1;
283 #endif
286 /*****************************************************************************
287 * eGalax part
290 #ifdef CONFIG_TOUCHSCREEN_USB_EGALAX
292 #ifndef MULTI_PACKET
293 #define MULTI_PACKET
294 #endif
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)
303 return 0;
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;
309 return 1;
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:
316 return 5;
318 case EGALAX_PKT_TYPE_DIAG:
319 if (len < 2)
320 return -1;
322 return buf[1] + 2;
325 return 0;
327 #endif
330 /*****************************************************************************
331 * PanJit Part
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;
340 return 1;
342 #endif
345 /*****************************************************************************
346 * 3M/Microtouch Part
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)
356 if (hwcalib_xy) {
357 dev->x = (pkt[4] << 8) | pkt[3];
358 dev->y = 0xffff - ((pkt[6] << 8) | pkt[5]);
359 } else {
360 dev->x = (pkt[8] << 8) | pkt[7];
361 dev->y = (pkt[10] << 8) | pkt[9];
363 dev->touch = (pkt[2] & 0x40) ? 1 : 0;
365 return 1;
368 static int mtouch_init(struct usbtouch_usb *usbtouch)
370 int ret, i;
371 struct usb_device *udev = interface_to_usbdev(usbtouch->interface);
373 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
374 MTOUCHUSB_RESET,
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",
378 __func__, ret);
379 if (ret < 0)
380 return ret;
381 msleep(150);
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",
389 __func__, ret);
390 if (ret >= 0)
391 break;
392 if (ret != -EPIPE)
393 return ret;
396 /* Default min/max xy are the raw values, override if using hw-calib */
397 if (hwcalib_xy) {
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);
402 return 0;
404 #endif
407 /*****************************************************************************
408 * ITM Part
410 #ifdef CONFIG_TOUCHSCREEN_USB_ITM
411 static int itm_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
413 int touch;
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;
423 if (!touch) {
424 if (dev->touch) {
425 dev->touch = 0;
426 return 1;
429 return 0;
432 dev->x = ((pkt[0] & 0x1F) << 7) | (pkt[3] & 0x7F);
433 dev->y = ((pkt[1] & 0x1F) << 7) | (pkt[4] & 0x7F);
434 dev->touch = touch;
436 return 1;
438 #endif
441 /*****************************************************************************
442 * eTurboTouch part
444 #ifdef CONFIG_TOUCHSCREEN_USB_ETURBO
445 #ifndef MULTI_PACKET
446 #define MULTI_PACKET
447 #endif
448 static int eturbo_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
450 unsigned int shift;
452 /* packets should start with sync */
453 if (!(pkt[0] & 0x80))
454 return 0;
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;
461 return 1;
464 static int eturbo_get_pkt_len(unsigned char *buf, int len)
466 if (buf[0] & 0x80)
467 return 5;
468 if (buf[0] == 0x01)
469 return 3;
470 return 0;
472 #endif
475 /*****************************************************************************
476 * Gunze part
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))
482 return 0;
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;
488 return 1;
490 #endif
492 /*****************************************************************************
493 * DMC TSC-10/25 Part
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
510 /* commands */
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);
518 int ret = -ENOMEM;
519 unsigned char *buf;
521 buf = kmalloc(2, GFP_NOIO);
522 if (!buf)
523 goto err_nobuf;
524 /* reset */
525 buf[0] = buf[1] = 0xFF;
526 ret = usb_control_msg(dev, usb_rcvctrlpipe (dev, 0),
527 TSC10_CMD_RESET,
528 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
529 0, 0, buf, 2, USB_CTRL_SET_TIMEOUT);
530 if (ret < 0)
531 goto err_out;
532 if (buf[0] != 0x06) {
533 ret = -ENODEV;
534 goto err_out;
537 /* set coordinate output rate */
538 buf[0] = buf[1] = 0xFF;
539 ret = usb_control_msg(dev, usb_rcvctrlpipe (dev, 0),
540 TSC10_CMD_RATE,
541 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
542 TSC10_RATE_150, 0, buf, 2, USB_CTRL_SET_TIMEOUT);
543 if (ret < 0)
544 goto err_out;
545 if ((buf[0] != 0x06) && (buf[0] != 0x15 || buf[1] != 0x01)) {
546 ret = -ENODEV;
547 goto err_out;
550 /* start sending data */
551 ret = usb_control_msg(dev, usb_rcvctrlpipe (dev, 0),
552 TSC10_CMD_DATA1,
553 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
554 0, 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
555 err_out:
556 kfree(buf);
557 err_nobuf:
558 return ret;
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;
568 return 1;
570 #endif
573 /*****************************************************************************
574 * IRTOUCH Part
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;
583 return 1;
585 #endif
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;
597 return 1;
599 #endif
601 /*****************************************************************************
602 * IdealTEK URTC1000 Part
604 #ifdef CONFIG_TOUCHSCREEN_USB_IDEALTEK
605 #ifndef MULTI_PACKET
606 #define MULTI_PACKET
607 #endif
608 static int idealtek_get_pkt_len(unsigned char *buf, int len)
610 if (buf[0] & 0x80)
611 return 5;
612 if (buf[0] == 0x01)
613 return len;
614 return 0;
617 static int idealtek_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
619 switch (pkt[0] & 0x98) {
620 case 0x88:
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;
625 return 1;
627 case 0x98:
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;
632 return 1;
634 default:
635 return 0;
638 #endif
640 /*****************************************************************************
641 * General Touch Part
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;
651 return 1;
653 #endif
655 /*****************************************************************************
656 * GoTop Part
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;
665 return 1;
667 #endif
669 /*****************************************************************************
670 * JASTEC Part
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;
679 return 1;
681 #endif
683 /*****************************************************************************
684 * Zytronic Part
686 #ifdef CONFIG_TOUCHSCREEN_USB_ZYTRONIC
687 static int zytronic_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
689 switch (pkt[0]) {
690 case 0x3A: /* command response */
691 dbg("%s: Command response %d", __func__, pkt[1]);
692 break;
694 case 0xC0: /* down */
695 dev->x = (pkt[1] & 0x7f) | ((pkt[2] & 0x07) << 7);
696 dev->y = (pkt[3] & 0x7f) | ((pkt[4] & 0x07) << 7);
697 dev->touch = 1;
698 dbg("%s: down %d,%d", __func__, dev->x, dev->y);
699 return 1;
701 case 0x80: /* up */
702 dev->x = (pkt[1] & 0x7f) | ((pkt[2] & 0x07) << 7);
703 dev->y = (pkt[3] & 0x7f) | ((pkt[4] & 0x07) << 7);
704 dev->touch = 0;
705 dbg("%s: up %d,%d", __func__, dev->x, dev->y);
706 return 1;
708 default:
709 dbg("%s: Unknown return %d", __func__, pkt[0]);
710 break;
713 return 0;
715 #endif
717 /*****************************************************************************
718 * NEXIO Part
720 #ifdef CONFIG_TOUCHSCREEN_USB_NEXIO
722 #define NEXIO_TIMEOUT 5000
723 #define NEXIO_BUFSIZE 1024
724 #define NEXIO_THRESHOLD 50
726 struct nexio_priv {
727 struct urb *ack;
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 */
736 u8 data[];
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;
749 int ret = -ENOMEM;
751 usbtouch->priv = kmalloc(sizeof(struct nexio_priv), GFP_KERNEL);
752 if (!usbtouch->priv)
753 goto out_buf;
755 priv = usbtouch->priv;
757 priv->ack_buf = kmemdup(nexio_ack_pkt, sizeof(nexio_ack_pkt),
758 GFP_KERNEL);
759 if (!priv->ack_buf)
760 goto err_priv;
762 priv->ack = usb_alloc_urb(0, GFP_KERNEL);
763 if (!priv->ack) {
764 dbg("%s - usb_alloc_urb failed: usbtouch->ack", __func__);
765 goto err_ack_buf;
768 return 0;
770 err_ack_buf:
771 kfree(priv->ack_buf);
772 err_priv:
773 kfree(priv);
774 out_buf:
775 return ret;
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;
783 int ret = -ENOMEM;
784 int actual_len, i;
785 unsigned char *buf;
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++) {
791 if (!input_ep &&
792 usb_endpoint_dir_in(&interface->endpoint[i].desc))
793 input_ep = interface->endpoint[i].desc.bEndpointAddress;
794 if (!output_ep &&
795 usb_endpoint_dir_out(&interface->endpoint[i].desc))
796 output_ep = interface->endpoint[i].desc.bEndpointAddress;
798 if (!input_ep || !output_ep)
799 return -ENXIO;
801 buf = kmalloc(NEXIO_BUFSIZE, GFP_NOIO);
802 if (!buf)
803 goto out_buf;
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,
809 NEXIO_TIMEOUT);
810 if (ret < 0)
811 goto out_buf;
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,
818 NEXIO_TIMEOUT);
819 if (ret < 0)
820 goto out_buf;
822 /* read replies */
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,
827 NEXIO_TIMEOUT);
828 if (ret < 0 || actual_len < 1 || buf[1] != actual_len)
829 continue;
830 switch (buf[0]) {
831 case 0x83: /* firmware version */
832 if (!firmware_ver)
833 firmware_ver = kstrdup(&buf[2], GFP_NOIO);
834 break;
835 case 0x84: /* device name */
836 if (!device_name)
837 device_name = kstrdup(&buf[2], GFP_NOIO);
838 break;
842 printk(KERN_INFO "Nexio device: %s, firmware version: %s\n",
843 device_name, firmware_ver);
845 kfree(firmware_ver);
846 kfree(device_name);
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);
851 ret = 0;
853 out_buf:
854 kfree(buf);
855 return ret;
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);
865 kfree(priv);
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)
879 return 0;
881 if (data_len > 0xff)
882 data_len -= 0x100;
883 if (x_len > 0xff)
884 x_len -= 0x80;
886 /* send ACK */
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) {
907 begin_x = x;
908 continue;
910 if (end_x == -1 && begin_x != -1 && packet->data[x] < NEXIO_THRESHOLD) {
911 end_x = x - 1;
912 for (y = x_len; y < data_len; y++) {
913 if (begin_y == -1 && packet->data[y] > NEXIO_THRESHOLD) {
914 begin_y = y - x_len;
915 continue;
917 if (end_y == -1 &&
918 begin_y != -1 && packet->data[y] < NEXIO_THRESHOLD) {
919 end_y = y - 1 - x_len;
920 w = end_x - begin_x;
921 h = end_y - begin_y;
922 #if 0
923 /* multi-touch */
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);
935 #endif
936 /* single touch */
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;
941 return 1;
944 begin_x = end_x = -1;
948 return 0;
950 #endif
953 /*****************************************************************************
954 * ELO part
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;
964 dev->press = pkt[6];
966 return 1;
968 #endif
971 /*****************************************************************************
972 * the different device descriptors
974 #ifdef MULTI_PACKET
975 static void usbtouch_process_multi(struct usbtouch_usb *usbtouch,
976 unsigned char *pkt, int len);
977 #endif
979 static struct usbtouch_device_info usbtouch_dev_info[] = {
980 #ifdef CONFIG_TOUCHSCREEN_USB_ELO
981 [DEVTYPE_ELO] = {
982 .min_xc = 0x0,
983 .max_xc = 0x0fff,
984 .min_yc = 0x0,
985 .max_yc = 0x0fff,
986 .max_press = 0xff,
987 .rept_size = 8,
988 .read_data = elo_read_data,
990 #endif
992 #ifdef CONFIG_TOUCHSCREEN_USB_EGALAX
993 [DEVTYPE_EGALAX] = {
994 .min_xc = 0x0,
995 .max_xc = 0x07ff,
996 .min_yc = 0x0,
997 .max_yc = 0x07ff,
998 .rept_size = 16,
999 .process_pkt = usbtouch_process_multi,
1000 .get_pkt_len = egalax_get_pkt_len,
1001 .read_data = egalax_read_data,
1003 #endif
1005 #ifdef CONFIG_TOUCHSCREEN_USB_PANJIT
1006 [DEVTYPE_PANJIT] = {
1007 .min_xc = 0x0,
1008 .max_xc = 0x0fff,
1009 .min_yc = 0x0,
1010 .max_yc = 0x0fff,
1011 .rept_size = 8,
1012 .read_data = panjit_read_data,
1014 #endif
1016 #ifdef CONFIG_TOUCHSCREEN_USB_3M
1017 [DEVTYPE_3M] = {
1018 .min_xc = 0x0,
1019 .max_xc = 0x4000,
1020 .min_yc = 0x0,
1021 .max_yc = 0x4000,
1022 .rept_size = 11,
1023 .read_data = mtouch_read_data,
1024 .init = mtouch_init,
1026 #endif
1028 #ifdef CONFIG_TOUCHSCREEN_USB_ITM
1029 [DEVTYPE_ITM] = {
1030 .min_xc = 0x0,
1031 .max_xc = 0x0fff,
1032 .min_yc = 0x0,
1033 .max_yc = 0x0fff,
1034 .max_press = 0xff,
1035 .rept_size = 8,
1036 .read_data = itm_read_data,
1038 #endif
1040 #ifdef CONFIG_TOUCHSCREEN_USB_ETURBO
1041 [DEVTYPE_ETURBO] = {
1042 .min_xc = 0x0,
1043 .max_xc = 0x07ff,
1044 .min_yc = 0x0,
1045 .max_yc = 0x07ff,
1046 .rept_size = 8,
1047 .process_pkt = usbtouch_process_multi,
1048 .get_pkt_len = eturbo_get_pkt_len,
1049 .read_data = eturbo_read_data,
1051 #endif
1053 #ifdef CONFIG_TOUCHSCREEN_USB_GUNZE
1054 [DEVTYPE_GUNZE] = {
1055 .min_xc = 0x0,
1056 .max_xc = 0x0fff,
1057 .min_yc = 0x0,
1058 .max_yc = 0x0fff,
1059 .rept_size = 4,
1060 .read_data = gunze_read_data,
1062 #endif
1064 #ifdef CONFIG_TOUCHSCREEN_USB_DMC_TSC10
1065 [DEVTYPE_DMC_TSC10] = {
1066 .min_xc = 0x0,
1067 .max_xc = 0x03ff,
1068 .min_yc = 0x0,
1069 .max_yc = 0x03ff,
1070 .rept_size = 5,
1071 .init = dmc_tsc10_init,
1072 .read_data = dmc_tsc10_read_data,
1074 #endif
1076 #ifdef CONFIG_TOUCHSCREEN_USB_IRTOUCH
1077 [DEVTYPE_IRTOUCH] = {
1078 .min_xc = 0x0,
1079 .max_xc = 0x0fff,
1080 .min_yc = 0x0,
1081 .max_yc = 0x0fff,
1082 .rept_size = 8,
1083 .read_data = irtouch_read_data,
1085 #endif
1087 #ifdef CONFIG_TOUCHSCREEN_USB_IDEALTEK
1088 [DEVTYPE_IDEALTEK] = {
1089 .min_xc = 0x0,
1090 .max_xc = 0x0fff,
1091 .min_yc = 0x0,
1092 .max_yc = 0x0fff,
1093 .rept_size = 8,
1094 .process_pkt = usbtouch_process_multi,
1095 .get_pkt_len = idealtek_get_pkt_len,
1096 .read_data = idealtek_read_data,
1098 #endif
1100 #ifdef CONFIG_TOUCHSCREEN_USB_GENERAL_TOUCH
1101 [DEVTYPE_GENERAL_TOUCH] = {
1102 .min_xc = 0x0,
1103 .max_xc = 0x7fff,
1104 .min_yc = 0x0,
1105 .max_yc = 0x7fff,
1106 .rept_size = 7,
1107 .read_data = general_touch_read_data,
1109 #endif
1111 #ifdef CONFIG_TOUCHSCREEN_USB_GOTOP
1112 [DEVTYPE_GOTOP] = {
1113 .min_xc = 0x0,
1114 .max_xc = 0x03ff,
1115 .min_yc = 0x0,
1116 .max_yc = 0x03ff,
1117 .rept_size = 4,
1118 .read_data = gotop_read_data,
1120 #endif
1122 #ifdef CONFIG_TOUCHSCREEN_USB_JASTEC
1123 [DEVTYPE_JASTEC] = {
1124 .min_xc = 0x0,
1125 .max_xc = 0x0fff,
1126 .min_yc = 0x0,
1127 .max_yc = 0x0fff,
1128 .rept_size = 4,
1129 .read_data = jastec_read_data,
1131 #endif
1133 #ifdef CONFIG_TOUCHSCREEN_USB_E2I
1134 [DEVTYPE_E2I] = {
1135 .min_xc = 0x0,
1136 .max_xc = 0x7fff,
1137 .min_yc = 0x0,
1138 .max_yc = 0x7fff,
1139 .rept_size = 6,
1140 .init = e2i_init,
1141 .read_data = e2i_read_data,
1143 #endif
1145 #ifdef CONFIG_TOUCHSCREEN_USB_ZYTRONIC
1146 [DEVTYPE_ZYTRONIC] = {
1147 .min_xc = 0x0,
1148 .max_xc = 0x03ff,
1149 .min_yc = 0x0,
1150 .max_yc = 0x03ff,
1151 .rept_size = 5,
1152 .read_data = zytronic_read_data,
1153 .irq_always = true,
1155 #endif
1157 #ifdef CONFIG_TOUCHSCREEN_USB_ETT_TC45USB
1158 [DEVTYPE_TC45USB] = {
1159 .min_xc = 0x0,
1160 .max_xc = 0x0fff,
1161 .min_yc = 0x0,
1162 .max_yc = 0x0fff,
1163 .rept_size = 5,
1164 .read_data = tc45usb_read_data,
1166 #endif
1168 #ifdef CONFIG_TOUCHSCREEN_USB_NEXIO
1169 [DEVTYPE_NEXIO] = {
1170 .rept_size = 1024,
1171 .irq_always = true,
1172 .read_data = nexio_read_data,
1173 .alloc = nexio_alloc,
1174 .init = nexio_init,
1175 .exit = nexio_exit,
1177 #endif
1181 /*****************************************************************************
1182 * Generic Part
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))
1190 return;
1192 input_report_key(usbtouch->input, BTN_TOUCH, usbtouch->touch);
1194 if (swap_xy) {
1195 input_report_abs(usbtouch->input, ABS_X, usbtouch->y);
1196 input_report_abs(usbtouch->input, ABS_Y, usbtouch->x);
1197 } else {
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);
1207 #ifdef MULTI_PACKET
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);
1220 /* drop? */
1221 if (unlikely(!pkt_len))
1222 goto out_flush_buf;
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))
1228 append = len;
1229 if (usbtouch->buf_len + append >= usbtouch->type->rept_size)
1230 goto out_flush_buf;
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);
1236 if (pkt_len < 0)
1237 return;
1240 /* append */
1241 tmp = pkt_len - usbtouch->buf_len;
1242 if (usbtouch->buf_len + tmp >= usbtouch->type->rept_size)
1243 goto out_flush_buf;
1244 memcpy(usbtouch->buffer + usbtouch->buf_len, pkt, tmp);
1245 usbtouch_process_pkt(usbtouch, usbtouch->buffer, pkt_len);
1247 buffer = pkt + tmp;
1248 buf_len = len - tmp;
1249 } else {
1250 buffer = pkt;
1251 buf_len = len;
1254 /* loop over the received packet, process */
1255 pos = 0;
1256 while (pos < buf_len) {
1257 /* get packet len */
1258 pkt_len = usbtouch->type->get_pkt_len(buffer + pos,
1259 buf_len - pos);
1261 /* unknown packet: skip one byte */
1262 if (unlikely(!pkt_len)) {
1263 pos++;
1264 continue;
1267 /* full packet: process */
1268 if (likely((pkt_len > 0) && (pkt_len <= buf_len - pos))) {
1269 usbtouch_process_pkt(usbtouch, buffer + pos, pkt_len);
1270 } else {
1271 /* incomplete packet: save in buffer */
1272 memcpy(usbtouch->buffer, buffer + pos, buf_len - pos);
1273 usbtouch->buf_len = buf_len - pos;
1274 return;
1276 pos += pkt_len;
1279 out_flush_buf:
1280 usbtouch->buf_len = 0;
1281 return;
1283 #endif
1286 static void usbtouch_irq(struct urb *urb)
1288 struct usbtouch_usb *usbtouch = urb->context;
1289 int retval;
1291 switch (urb->status) {
1292 case 0:
1293 /* success */
1294 break;
1295 case -ETIME:
1296 /* this urb is timing out */
1297 dbg("%s - urb timed out - was the device unplugged?",
1298 __func__);
1299 return;
1300 case -ECONNRESET:
1301 case -ENOENT:
1302 case -ESHUTDOWN:
1303 case -EPIPE:
1304 /* this urb is terminated, clean up */
1305 dbg("%s - urb shutting down with status: %d",
1306 __func__, urb->status);
1307 return;
1308 default:
1309 dbg("%s - nonzero urb status received: %d",
1310 __func__, urb->status);
1311 goto exit;
1314 usbtouch->type->process_pkt(usbtouch, usbtouch->data, urb->actual_length);
1316 exit:
1317 usb_mark_last_busy(interface_to_usbdev(usbtouch->interface));
1318 retval = usb_submit_urb(urb, GFP_ATOMIC);
1319 if (retval)
1320 err("%s - usb_submit_urb failed with result: %d",
1321 __func__, retval);
1324 static int usbtouch_open(struct input_dev *input)
1326 struct usbtouch_usb *usbtouch = input_get_drvdata(input);
1327 int r;
1329 usbtouch->irq->dev = interface_to_usbdev(usbtouch->interface);
1331 r = usb_autopm_get_interface(usbtouch->interface) ? -EIO : 0;
1332 if (r < 0)
1333 goto out;
1335 if (!usbtouch->type->irq_always) {
1336 if (usb_submit_urb(usbtouch->irq, GFP_KERNEL)) {
1337 r = -EIO;
1338 goto out_put;
1342 usbtouch->interface->needs_remote_wakeup = 1;
1343 out_put:
1344 usb_autopm_put_interface(usbtouch->interface);
1345 out:
1346 return r;
1349 static void usbtouch_close(struct input_dev *input)
1351 struct usbtouch_usb *usbtouch = input_get_drvdata(input);
1352 int r;
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;
1358 if (!r)
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);
1369 return 0;
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;
1376 int result = 0;
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);
1383 return result;
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;
1390 int err = 0;
1392 /* reinit the device */
1393 if (usbtouch->type->init) {
1394 err = usbtouch->type->init(usbtouch);
1395 if (err) {
1396 dbg("%s - type->init() failed, err: %d",
1397 __func__, err);
1398 return err;
1402 /* restart IO if needed */
1403 mutex_lock(&input->mutex);
1404 if (input->users)
1405 err = usb_submit_urb(usbtouch->irq, GFP_NOIO);
1406 mutex_unlock(&input->mutex);
1408 return err;
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)
1422 int i;
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;
1428 return NULL;
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;
1439 int err = -ENOMEM;
1441 /* some devices are ignored */
1442 if (id->driver_info == DEVTYPE_IGNORE)
1443 return -ENODEV;
1445 endpoint = usbtouch_get_input_endpoint(intf->cur_altsetting);
1446 if (!endpoint)
1447 return -ENXIO;
1449 usbtouch = kzalloc(sizeof(struct usbtouch_usb), GFP_KERNEL);
1450 input_dev = input_allocate_device();
1451 if (!usbtouch || !input_dev)
1452 goto out_free;
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)
1462 goto out_free;
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);
1520 else
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 */
1531 if (type->alloc) {
1532 err = type->alloc(usbtouch);
1533 if (err) {
1534 dbg("%s - type->alloc() failed, err: %d", __func__, err);
1535 goto out_free_urb;
1539 /* device specific initialisation*/
1540 if (type->init) {
1541 err = type->init(usbtouch);
1542 if (err) {
1543 dbg("%s - type->init() failed, err: %d", __func__, err);
1544 goto out_do_exit;
1548 err = input_register_device(usbtouch->input);
1549 if (err) {
1550 dbg("%s - input_register_device failed, err: %d", __func__, err);
1551 goto out_do_exit;
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);
1560 if (err) {
1561 usb_autopm_put_interface(intf);
1562 err("%s - usb_submit_urb failed with result: %d",
1563 __func__, err);
1564 goto out_unregister_input;
1568 return 0;
1570 out_unregister_input:
1571 input_unregister_device(input_dev);
1572 input_dev = NULL;
1573 out_do_exit:
1574 if (type->exit)
1575 type->exit(usbtouch);
1576 out_free_urb:
1577 usb_free_urb(usbtouch->irq);
1578 out_free_buffers:
1579 usbtouch_free_buffers(udev, usbtouch);
1580 out_free:
1581 input_free_device(input_dev);
1582 kfree(usbtouch);
1583 return err;
1586 static void usbtouch_disconnect(struct usb_interface *intf)
1588 struct usbtouch_usb *usbtouch = usb_get_intfdata(intf);
1590 dbg("%s - called", __func__);
1592 if (!usbtouch)
1593 return;
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);
1603 kfree(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");