USB: usb-storage: unusual_devs update for Super TOP SATA bridge
[linux/fpc-iii.git] / drivers / input / touchscreen / usbtouchscreen.c
blob22cd96f58c99b33451f2cc99757fb561ab738232
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/init.h>
55 #include <linux/usb.h>
56 #include <linux/usb/input.h>
57 #include <linux/hid.h>
60 #define DRIVER_VERSION "v0.6"
61 #define DRIVER_AUTHOR "Daniel Ritz <daniel.ritz@gmx.ch>"
62 #define DRIVER_DESC "USB Touchscreen Driver"
64 static bool swap_xy;
65 module_param(swap_xy, bool, 0644);
66 MODULE_PARM_DESC(swap_xy, "If set X and Y axes are swapped.");
68 static bool hwcalib_xy;
69 module_param(hwcalib_xy, bool, 0644);
70 MODULE_PARM_DESC(hwcalib_xy, "If set hw-calibrated X/Y are used if available");
72 /* device specifc data/functions */
73 struct usbtouch_usb;
74 struct usbtouch_device_info {
75 int min_xc, max_xc;
76 int min_yc, max_yc;
77 int min_press, max_press;
78 int rept_size;
81 * Always service the USB devices irq not just when the input device is
82 * open. This is useful when devices have a watchdog which prevents us
83 * from periodically polling the device. Leave this unset unless your
84 * touchscreen device requires it, as it does consume more of the USB
85 * bandwidth.
87 bool irq_always;
89 void (*process_pkt) (struct usbtouch_usb *usbtouch, unsigned char *pkt, int len);
92 * used to get the packet len. possible return values:
93 * > 0: packet len
94 * = 0: skip one byte
95 * < 0: -return value more bytes needed
97 int (*get_pkt_len) (unsigned char *pkt, int len);
99 int (*read_data) (struct usbtouch_usb *usbtouch, unsigned char *pkt);
100 int (*alloc) (struct usbtouch_usb *usbtouch);
101 int (*init) (struct usbtouch_usb *usbtouch);
102 void (*exit) (struct usbtouch_usb *usbtouch);
105 /* a usbtouch device */
106 struct usbtouch_usb {
107 unsigned char *data;
108 dma_addr_t data_dma;
109 unsigned char *buffer;
110 int buf_len;
111 struct urb *irq;
112 struct usb_interface *interface;
113 struct input_dev *input;
114 struct usbtouch_device_info *type;
115 char name[128];
116 char phys[64];
117 void *priv;
119 int x, y;
120 int touch, press;
124 /* device types */
125 enum {
126 DEVTYPE_IGNORE = -1,
127 DEVTYPE_EGALAX,
128 DEVTYPE_PANJIT,
129 DEVTYPE_3M,
130 DEVTYPE_ITM,
131 DEVTYPE_ETURBO,
132 DEVTYPE_GUNZE,
133 DEVTYPE_DMC_TSC10,
134 DEVTYPE_IRTOUCH,
135 DEVTYPE_IDEALTEK,
136 DEVTYPE_GENERAL_TOUCH,
137 DEVTYPE_GOTOP,
138 DEVTYPE_JASTEC,
139 DEVTYPE_E2I,
140 DEVTYPE_ZYTRONIC,
141 DEVTYPE_TC45USB,
142 DEVTYPE_NEXIO,
143 DEVTYPE_ELO,
144 DEVTYPE_ETOUCH,
147 #define USB_DEVICE_HID_CLASS(vend, prod) \
148 .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS \
149 | USB_DEVICE_ID_MATCH_INT_PROTOCOL \
150 | USB_DEVICE_ID_MATCH_DEVICE, \
151 .idVendor = (vend), \
152 .idProduct = (prod), \
153 .bInterfaceClass = USB_INTERFACE_CLASS_HID, \
154 .bInterfaceProtocol = USB_INTERFACE_PROTOCOL_MOUSE
156 static const struct usb_device_id usbtouch_devices[] = {
157 #ifdef CONFIG_TOUCHSCREEN_USB_EGALAX
158 /* ignore the HID capable devices, handled by usbhid */
159 {USB_DEVICE_HID_CLASS(0x0eef, 0x0001), .driver_info = DEVTYPE_IGNORE},
160 {USB_DEVICE_HID_CLASS(0x0eef, 0x0002), .driver_info = DEVTYPE_IGNORE},
162 /* normal device IDs */
163 {USB_DEVICE(0x3823, 0x0001), .driver_info = DEVTYPE_EGALAX},
164 {USB_DEVICE(0x3823, 0x0002), .driver_info = DEVTYPE_EGALAX},
165 {USB_DEVICE(0x0123, 0x0001), .driver_info = DEVTYPE_EGALAX},
166 {USB_DEVICE(0x0eef, 0x0001), .driver_info = DEVTYPE_EGALAX},
167 {USB_DEVICE(0x0eef, 0x0002), .driver_info = DEVTYPE_EGALAX},
168 {USB_DEVICE(0x1234, 0x0001), .driver_info = DEVTYPE_EGALAX},
169 {USB_DEVICE(0x1234, 0x0002), .driver_info = DEVTYPE_EGALAX},
170 #endif
172 #ifdef CONFIG_TOUCHSCREEN_USB_PANJIT
173 {USB_DEVICE(0x134c, 0x0001), .driver_info = DEVTYPE_PANJIT},
174 {USB_DEVICE(0x134c, 0x0002), .driver_info = DEVTYPE_PANJIT},
175 {USB_DEVICE(0x134c, 0x0003), .driver_info = DEVTYPE_PANJIT},
176 {USB_DEVICE(0x134c, 0x0004), .driver_info = DEVTYPE_PANJIT},
177 #endif
179 #ifdef CONFIG_TOUCHSCREEN_USB_3M
180 {USB_DEVICE(0x0596, 0x0001), .driver_info = DEVTYPE_3M},
181 #endif
183 #ifdef CONFIG_TOUCHSCREEN_USB_ITM
184 {USB_DEVICE(0x0403, 0xf9e9), .driver_info = DEVTYPE_ITM},
185 {USB_DEVICE(0x16e3, 0xf9e9), .driver_info = DEVTYPE_ITM},
186 #endif
188 #ifdef CONFIG_TOUCHSCREEN_USB_ETURBO
189 {USB_DEVICE(0x1234, 0x5678), .driver_info = DEVTYPE_ETURBO},
190 #endif
192 #ifdef CONFIG_TOUCHSCREEN_USB_GUNZE
193 {USB_DEVICE(0x0637, 0x0001), .driver_info = DEVTYPE_GUNZE},
194 #endif
196 #ifdef CONFIG_TOUCHSCREEN_USB_DMC_TSC10
197 {USB_DEVICE(0x0afa, 0x03e8), .driver_info = DEVTYPE_DMC_TSC10},
198 #endif
200 #ifdef CONFIG_TOUCHSCREEN_USB_IRTOUCH
201 {USB_DEVICE(0x595a, 0x0001), .driver_info = DEVTYPE_IRTOUCH},
202 {USB_DEVICE(0x6615, 0x0001), .driver_info = DEVTYPE_IRTOUCH},
203 #endif
205 #ifdef CONFIG_TOUCHSCREEN_USB_IDEALTEK
206 {USB_DEVICE(0x1391, 0x1000), .driver_info = DEVTYPE_IDEALTEK},
207 #endif
209 #ifdef CONFIG_TOUCHSCREEN_USB_GENERAL_TOUCH
210 {USB_DEVICE(0x0dfc, 0x0001), .driver_info = DEVTYPE_GENERAL_TOUCH},
211 #endif
213 #ifdef CONFIG_TOUCHSCREEN_USB_GOTOP
214 {USB_DEVICE(0x08f2, 0x007f), .driver_info = DEVTYPE_GOTOP},
215 {USB_DEVICE(0x08f2, 0x00ce), .driver_info = DEVTYPE_GOTOP},
216 {USB_DEVICE(0x08f2, 0x00f4), .driver_info = DEVTYPE_GOTOP},
217 #endif
219 #ifdef CONFIG_TOUCHSCREEN_USB_JASTEC
220 {USB_DEVICE(0x0f92, 0x0001), .driver_info = DEVTYPE_JASTEC},
221 #endif
223 #ifdef CONFIG_TOUCHSCREEN_USB_E2I
224 {USB_DEVICE(0x1ac7, 0x0001), .driver_info = DEVTYPE_E2I},
225 #endif
227 #ifdef CONFIG_TOUCHSCREEN_USB_ZYTRONIC
228 {USB_DEVICE(0x14c8, 0x0003), .driver_info = DEVTYPE_ZYTRONIC},
229 #endif
231 #ifdef CONFIG_TOUCHSCREEN_USB_ETT_TC45USB
232 /* TC5UH */
233 {USB_DEVICE(0x0664, 0x0309), .driver_info = DEVTYPE_TC45USB},
234 /* TC4UM */
235 {USB_DEVICE(0x0664, 0x0306), .driver_info = DEVTYPE_TC45USB},
236 #endif
238 #ifdef CONFIG_TOUCHSCREEN_USB_NEXIO
239 /* data interface only */
240 {USB_DEVICE_AND_INTERFACE_INFO(0x10f0, 0x2002, 0x0a, 0x00, 0x00),
241 .driver_info = DEVTYPE_NEXIO},
242 {USB_DEVICE_AND_INTERFACE_INFO(0x1870, 0x0001, 0x0a, 0x00, 0x00),
243 .driver_info = DEVTYPE_NEXIO},
244 #endif
246 #ifdef CONFIG_TOUCHSCREEN_USB_ELO
247 {USB_DEVICE(0x04e7, 0x0020), .driver_info = DEVTYPE_ELO},
248 #endif
250 #ifdef CONFIG_TOUCHSCREEN_USB_EASYTOUCH
251 {USB_DEVICE(0x7374, 0x0001), .driver_info = DEVTYPE_ETOUCH},
252 #endif
258 /*****************************************************************************
259 * e2i Part
262 #ifdef CONFIG_TOUCHSCREEN_USB_E2I
263 static int e2i_init(struct usbtouch_usb *usbtouch)
265 int ret;
266 struct usb_device *udev = interface_to_usbdev(usbtouch->interface);
268 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
269 0x01, 0x02, 0x0000, 0x0081,
270 NULL, 0, USB_CTRL_SET_TIMEOUT);
272 dbg("%s - usb_control_msg - E2I_RESET - bytes|err: %d",
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_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
308 if ((pkt[0] & EGALAX_PKT_TYPE_MASK) != EGALAX_PKT_TYPE_REPT)
309 return 0;
311 dev->x = ((pkt[3] & 0x0F) << 7) | (pkt[4] & 0x7F);
312 dev->y = ((pkt[1] & 0x0F) << 7) | (pkt[2] & 0x7F);
313 dev->touch = pkt[0] & 0x01;
315 return 1;
318 static int egalax_get_pkt_len(unsigned char *buf, int len)
320 switch (buf[0] & EGALAX_PKT_TYPE_MASK) {
321 case EGALAX_PKT_TYPE_REPT:
322 return 5;
324 case EGALAX_PKT_TYPE_DIAG:
325 if (len < 2)
326 return -1;
328 return buf[1] + 2;
331 return 0;
333 #endif
335 /*****************************************************************************
336 * EasyTouch part
339 #ifdef CONFIG_TOUCHSCREEN_USB_EASYTOUCH
341 #ifndef MULTI_PACKET
342 #define MULTI_PACKET
343 #endif
345 #define ETOUCH_PKT_TYPE_MASK 0xFE
346 #define ETOUCH_PKT_TYPE_REPT 0x80
347 #define ETOUCH_PKT_TYPE_REPT2 0xB0
348 #define ETOUCH_PKT_TYPE_DIAG 0x0A
350 static int etouch_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
352 if ((pkt[0] & ETOUCH_PKT_TYPE_MASK) != ETOUCH_PKT_TYPE_REPT &&
353 (pkt[0] & ETOUCH_PKT_TYPE_MASK) != ETOUCH_PKT_TYPE_REPT2)
354 return 0;
356 dev->x = ((pkt[1] & 0x1F) << 7) | (pkt[2] & 0x7F);
357 dev->y = ((pkt[3] & 0x1F) << 7) | (pkt[4] & 0x7F);
358 dev->touch = pkt[0] & 0x01;
360 return 1;
363 static int etouch_get_pkt_len(unsigned char *buf, int len)
365 switch (buf[0] & ETOUCH_PKT_TYPE_MASK) {
366 case ETOUCH_PKT_TYPE_REPT:
367 case ETOUCH_PKT_TYPE_REPT2:
368 return 5;
370 case ETOUCH_PKT_TYPE_DIAG:
371 if (len < 2)
372 return -1;
374 return buf[1] + 2;
377 return 0;
379 #endif
381 /*****************************************************************************
382 * PanJit Part
384 #ifdef CONFIG_TOUCHSCREEN_USB_PANJIT
385 static int panjit_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
387 dev->x = ((pkt[2] & 0x0F) << 8) | pkt[1];
388 dev->y = ((pkt[4] & 0x0F) << 8) | pkt[3];
389 dev->touch = pkt[0] & 0x01;
391 return 1;
393 #endif
396 /*****************************************************************************
397 * 3M/Microtouch Part
399 #ifdef CONFIG_TOUCHSCREEN_USB_3M
401 #define MTOUCHUSB_ASYNC_REPORT 1
402 #define MTOUCHUSB_RESET 7
403 #define MTOUCHUSB_REQ_CTRLLR_ID 10
405 static int mtouch_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
407 if (hwcalib_xy) {
408 dev->x = (pkt[4] << 8) | pkt[3];
409 dev->y = 0xffff - ((pkt[6] << 8) | pkt[5]);
410 } else {
411 dev->x = (pkt[8] << 8) | pkt[7];
412 dev->y = (pkt[10] << 8) | pkt[9];
414 dev->touch = (pkt[2] & 0x40) ? 1 : 0;
416 return 1;
419 static int mtouch_init(struct usbtouch_usb *usbtouch)
421 int ret, i;
422 struct usb_device *udev = interface_to_usbdev(usbtouch->interface);
424 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
425 MTOUCHUSB_RESET,
426 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
427 1, 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
428 dbg("%s - usb_control_msg - MTOUCHUSB_RESET - bytes|err: %d",
429 __func__, ret);
430 if (ret < 0)
431 return ret;
432 msleep(150);
434 for (i = 0; i < 3; i++) {
435 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
436 MTOUCHUSB_ASYNC_REPORT,
437 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
438 1, 1, NULL, 0, USB_CTRL_SET_TIMEOUT);
439 dbg("%s - usb_control_msg - MTOUCHUSB_ASYNC_REPORT - bytes|err: %d",
440 __func__, ret);
441 if (ret >= 0)
442 break;
443 if (ret != -EPIPE)
444 return ret;
447 /* Default min/max xy are the raw values, override if using hw-calib */
448 if (hwcalib_xy) {
449 input_set_abs_params(usbtouch->input, ABS_X, 0, 0xffff, 0, 0);
450 input_set_abs_params(usbtouch->input, ABS_Y, 0, 0xffff, 0, 0);
453 return 0;
455 #endif
458 /*****************************************************************************
459 * ITM Part
461 #ifdef CONFIG_TOUCHSCREEN_USB_ITM
462 static int itm_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
464 int touch;
466 * ITM devices report invalid x/y data if not touched.
467 * if the screen was touched before but is not touched any more
468 * report touch as 0 with the last valid x/y data once. then stop
469 * reporting data until touched again.
471 dev->press = ((pkt[2] & 0x01) << 7) | (pkt[5] & 0x7F);
473 touch = ~pkt[7] & 0x20;
474 if (!touch) {
475 if (dev->touch) {
476 dev->touch = 0;
477 return 1;
480 return 0;
483 dev->x = ((pkt[0] & 0x1F) << 7) | (pkt[3] & 0x7F);
484 dev->y = ((pkt[1] & 0x1F) << 7) | (pkt[4] & 0x7F);
485 dev->touch = touch;
487 return 1;
489 #endif
492 /*****************************************************************************
493 * eTurboTouch part
495 #ifdef CONFIG_TOUCHSCREEN_USB_ETURBO
496 #ifndef MULTI_PACKET
497 #define MULTI_PACKET
498 #endif
499 static int eturbo_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
501 unsigned int shift;
503 /* packets should start with sync */
504 if (!(pkt[0] & 0x80))
505 return 0;
507 shift = (6 - (pkt[0] & 0x03));
508 dev->x = ((pkt[3] << 7) | pkt[4]) >> shift;
509 dev->y = ((pkt[1] << 7) | pkt[2]) >> shift;
510 dev->touch = (pkt[0] & 0x10) ? 1 : 0;
512 return 1;
515 static int eturbo_get_pkt_len(unsigned char *buf, int len)
517 if (buf[0] & 0x80)
518 return 5;
519 if (buf[0] == 0x01)
520 return 3;
521 return 0;
523 #endif
526 /*****************************************************************************
527 * Gunze part
529 #ifdef CONFIG_TOUCHSCREEN_USB_GUNZE
530 static int gunze_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
532 if (!(pkt[0] & 0x80) || ((pkt[1] | pkt[2] | pkt[3]) & 0x80))
533 return 0;
535 dev->x = ((pkt[0] & 0x1F) << 7) | (pkt[2] & 0x7F);
536 dev->y = ((pkt[1] & 0x1F) << 7) | (pkt[3] & 0x7F);
537 dev->touch = pkt[0] & 0x20;
539 return 1;
541 #endif
543 /*****************************************************************************
544 * DMC TSC-10/25 Part
546 * Documentation about the controller and it's protocol can be found at
547 * http://www.dmccoltd.com/files/controler/tsc10usb_pi_e.pdf
548 * http://www.dmccoltd.com/files/controler/tsc25_usb_e.pdf
550 #ifdef CONFIG_TOUCHSCREEN_USB_DMC_TSC10
552 /* supported data rates. currently using 130 */
553 #define TSC10_RATE_POINT 0x50
554 #define TSC10_RATE_30 0x40
555 #define TSC10_RATE_50 0x41
556 #define TSC10_RATE_80 0x42
557 #define TSC10_RATE_100 0x43
558 #define TSC10_RATE_130 0x44
559 #define TSC10_RATE_150 0x45
561 /* commands */
562 #define TSC10_CMD_RESET 0x55
563 #define TSC10_CMD_RATE 0x05
564 #define TSC10_CMD_DATA1 0x01
566 static int dmc_tsc10_init(struct usbtouch_usb *usbtouch)
568 struct usb_device *dev = interface_to_usbdev(usbtouch->interface);
569 int ret = -ENOMEM;
570 unsigned char *buf;
572 buf = kmalloc(2, GFP_NOIO);
573 if (!buf)
574 goto err_nobuf;
575 /* reset */
576 buf[0] = buf[1] = 0xFF;
577 ret = usb_control_msg(dev, usb_rcvctrlpipe (dev, 0),
578 TSC10_CMD_RESET,
579 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
580 0, 0, buf, 2, USB_CTRL_SET_TIMEOUT);
581 if (ret < 0)
582 goto err_out;
583 if (buf[0] != 0x06) {
584 ret = -ENODEV;
585 goto err_out;
588 /* set coordinate output rate */
589 buf[0] = buf[1] = 0xFF;
590 ret = usb_control_msg(dev, usb_rcvctrlpipe (dev, 0),
591 TSC10_CMD_RATE,
592 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
593 TSC10_RATE_150, 0, buf, 2, USB_CTRL_SET_TIMEOUT);
594 if (ret < 0)
595 goto err_out;
596 if ((buf[0] != 0x06) && (buf[0] != 0x15 || buf[1] != 0x01)) {
597 ret = -ENODEV;
598 goto err_out;
601 /* start sending data */
602 ret = usb_control_msg(dev, usb_rcvctrlpipe (dev, 0),
603 TSC10_CMD_DATA1,
604 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
605 0, 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
606 err_out:
607 kfree(buf);
608 err_nobuf:
609 return ret;
613 static int dmc_tsc10_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
615 dev->x = ((pkt[2] & 0x03) << 8) | pkt[1];
616 dev->y = ((pkt[4] & 0x03) << 8) | pkt[3];
617 dev->touch = pkt[0] & 0x01;
619 return 1;
621 #endif
624 /*****************************************************************************
625 * IRTOUCH Part
627 #ifdef CONFIG_TOUCHSCREEN_USB_IRTOUCH
628 static int irtouch_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
630 dev->x = (pkt[3] << 8) | pkt[2];
631 dev->y = (pkt[5] << 8) | pkt[4];
632 dev->touch = (pkt[1] & 0x03) ? 1 : 0;
634 return 1;
636 #endif
638 /*****************************************************************************
639 * ET&T TC5UH/TC4UM part
641 #ifdef CONFIG_TOUCHSCREEN_USB_ETT_TC45USB
642 static int tc45usb_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
644 dev->x = ((pkt[2] & 0x0F) << 8) | pkt[1];
645 dev->y = ((pkt[4] & 0x0F) << 8) | pkt[3];
646 dev->touch = pkt[0] & 0x01;
648 return 1;
650 #endif
652 /*****************************************************************************
653 * IdealTEK URTC1000 Part
655 #ifdef CONFIG_TOUCHSCREEN_USB_IDEALTEK
656 #ifndef MULTI_PACKET
657 #define MULTI_PACKET
658 #endif
659 static int idealtek_get_pkt_len(unsigned char *buf, int len)
661 if (buf[0] & 0x80)
662 return 5;
663 if (buf[0] == 0x01)
664 return len;
665 return 0;
668 static int idealtek_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
670 switch (pkt[0] & 0x98) {
671 case 0x88:
672 /* touch data in IdealTEK mode */
673 dev->x = (pkt[1] << 5) | (pkt[2] >> 2);
674 dev->y = (pkt[3] << 5) | (pkt[4] >> 2);
675 dev->touch = (pkt[0] & 0x40) ? 1 : 0;
676 return 1;
678 case 0x98:
679 /* touch data in MT emulation mode */
680 dev->x = (pkt[2] << 5) | (pkt[1] >> 2);
681 dev->y = (pkt[4] << 5) | (pkt[3] >> 2);
682 dev->touch = (pkt[0] & 0x40) ? 1 : 0;
683 return 1;
685 default:
686 return 0;
689 #endif
691 /*****************************************************************************
692 * General Touch Part
694 #ifdef CONFIG_TOUCHSCREEN_USB_GENERAL_TOUCH
695 static int general_touch_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
697 dev->x = (pkt[2] << 8) | pkt[1];
698 dev->y = (pkt[4] << 8) | pkt[3];
699 dev->press = pkt[5] & 0xff;
700 dev->touch = pkt[0] & 0x01;
702 return 1;
704 #endif
706 /*****************************************************************************
707 * GoTop Part
709 #ifdef CONFIG_TOUCHSCREEN_USB_GOTOP
710 static int gotop_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
712 dev->x = ((pkt[1] & 0x38) << 4) | pkt[2];
713 dev->y = ((pkt[1] & 0x07) << 7) | pkt[3];
714 dev->touch = pkt[0] & 0x01;
716 return 1;
718 #endif
720 /*****************************************************************************
721 * JASTEC Part
723 #ifdef CONFIG_TOUCHSCREEN_USB_JASTEC
724 static int jastec_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
726 dev->x = ((pkt[0] & 0x3f) << 6) | (pkt[2] & 0x3f);
727 dev->y = ((pkt[1] & 0x3f) << 6) | (pkt[3] & 0x3f);
728 dev->touch = (pkt[0] & 0x40) >> 6;
730 return 1;
732 #endif
734 /*****************************************************************************
735 * Zytronic Part
737 #ifdef CONFIG_TOUCHSCREEN_USB_ZYTRONIC
738 static int zytronic_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
740 switch (pkt[0]) {
741 case 0x3A: /* command response */
742 dbg("%s: Command response %d", __func__, pkt[1]);
743 break;
745 case 0xC0: /* down */
746 dev->x = (pkt[1] & 0x7f) | ((pkt[2] & 0x07) << 7);
747 dev->y = (pkt[3] & 0x7f) | ((pkt[4] & 0x07) << 7);
748 dev->touch = 1;
749 dbg("%s: down %d,%d", __func__, dev->x, dev->y);
750 return 1;
752 case 0x80: /* up */
753 dev->x = (pkt[1] & 0x7f) | ((pkt[2] & 0x07) << 7);
754 dev->y = (pkt[3] & 0x7f) | ((pkt[4] & 0x07) << 7);
755 dev->touch = 0;
756 dbg("%s: up %d,%d", __func__, dev->x, dev->y);
757 return 1;
759 default:
760 dbg("%s: Unknown return %d", __func__, pkt[0]);
761 break;
764 return 0;
766 #endif
768 /*****************************************************************************
769 * NEXIO Part
771 #ifdef CONFIG_TOUCHSCREEN_USB_NEXIO
773 #define NEXIO_TIMEOUT 5000
774 #define NEXIO_BUFSIZE 1024
775 #define NEXIO_THRESHOLD 50
777 struct nexio_priv {
778 struct urb *ack;
779 unsigned char *ack_buf;
782 struct nexio_touch_packet {
783 u8 flags; /* 0xe1 = touch, 0xe1 = release */
784 __be16 data_len; /* total bytes of touch data */
785 __be16 x_len; /* bytes for X axis */
786 __be16 y_len; /* bytes for Y axis */
787 u8 data[];
788 } __attribute__ ((packed));
790 static unsigned char nexio_ack_pkt[2] = { 0xaa, 0x02 };
791 static unsigned char nexio_init_pkt[4] = { 0x82, 0x04, 0x0a, 0x0f };
793 static void nexio_ack_complete(struct urb *urb)
797 static int nexio_alloc(struct usbtouch_usb *usbtouch)
799 struct nexio_priv *priv;
800 int ret = -ENOMEM;
802 usbtouch->priv = kmalloc(sizeof(struct nexio_priv), GFP_KERNEL);
803 if (!usbtouch->priv)
804 goto out_buf;
806 priv = usbtouch->priv;
808 priv->ack_buf = kmemdup(nexio_ack_pkt, sizeof(nexio_ack_pkt),
809 GFP_KERNEL);
810 if (!priv->ack_buf)
811 goto err_priv;
813 priv->ack = usb_alloc_urb(0, GFP_KERNEL);
814 if (!priv->ack) {
815 dbg("%s - usb_alloc_urb failed: usbtouch->ack", __func__);
816 goto err_ack_buf;
819 return 0;
821 err_ack_buf:
822 kfree(priv->ack_buf);
823 err_priv:
824 kfree(priv);
825 out_buf:
826 return ret;
829 static int nexio_init(struct usbtouch_usb *usbtouch)
831 struct usb_device *dev = interface_to_usbdev(usbtouch->interface);
832 struct usb_host_interface *interface = usbtouch->interface->cur_altsetting;
833 struct nexio_priv *priv = usbtouch->priv;
834 int ret = -ENOMEM;
835 int actual_len, i;
836 unsigned char *buf;
837 char *firmware_ver = NULL, *device_name = NULL;
838 int input_ep = 0, output_ep = 0;
840 /* find first input and output endpoint */
841 for (i = 0; i < interface->desc.bNumEndpoints; i++) {
842 if (!input_ep &&
843 usb_endpoint_dir_in(&interface->endpoint[i].desc))
844 input_ep = interface->endpoint[i].desc.bEndpointAddress;
845 if (!output_ep &&
846 usb_endpoint_dir_out(&interface->endpoint[i].desc))
847 output_ep = interface->endpoint[i].desc.bEndpointAddress;
849 if (!input_ep || !output_ep)
850 return -ENXIO;
852 buf = kmalloc(NEXIO_BUFSIZE, GFP_NOIO);
853 if (!buf)
854 goto out_buf;
856 /* two empty reads */
857 for (i = 0; i < 2; i++) {
858 ret = usb_bulk_msg(dev, usb_rcvbulkpipe(dev, input_ep),
859 buf, NEXIO_BUFSIZE, &actual_len,
860 NEXIO_TIMEOUT);
861 if (ret < 0)
862 goto out_buf;
865 /* send init command */
866 memcpy(buf, nexio_init_pkt, sizeof(nexio_init_pkt));
867 ret = usb_bulk_msg(dev, usb_sndbulkpipe(dev, output_ep),
868 buf, sizeof(nexio_init_pkt), &actual_len,
869 NEXIO_TIMEOUT);
870 if (ret < 0)
871 goto out_buf;
873 /* read replies */
874 for (i = 0; i < 3; i++) {
875 memset(buf, 0, NEXIO_BUFSIZE);
876 ret = usb_bulk_msg(dev, usb_rcvbulkpipe(dev, input_ep),
877 buf, NEXIO_BUFSIZE, &actual_len,
878 NEXIO_TIMEOUT);
879 if (ret < 0 || actual_len < 1 || buf[1] != actual_len)
880 continue;
881 switch (buf[0]) {
882 case 0x83: /* firmware version */
883 if (!firmware_ver)
884 firmware_ver = kstrdup(&buf[2], GFP_NOIO);
885 break;
886 case 0x84: /* device name */
887 if (!device_name)
888 device_name = kstrdup(&buf[2], GFP_NOIO);
889 break;
893 printk(KERN_INFO "Nexio device: %s, firmware version: %s\n",
894 device_name, firmware_ver);
896 kfree(firmware_ver);
897 kfree(device_name);
899 usb_fill_bulk_urb(priv->ack, dev, usb_sndbulkpipe(dev, output_ep),
900 priv->ack_buf, sizeof(nexio_ack_pkt),
901 nexio_ack_complete, usbtouch);
902 ret = 0;
904 out_buf:
905 kfree(buf);
906 return ret;
909 static void nexio_exit(struct usbtouch_usb *usbtouch)
911 struct nexio_priv *priv = usbtouch->priv;
913 usb_kill_urb(priv->ack);
914 usb_free_urb(priv->ack);
915 kfree(priv->ack_buf);
916 kfree(priv);
919 static int nexio_read_data(struct usbtouch_usb *usbtouch, unsigned char *pkt)
921 struct nexio_touch_packet *packet = (void *) pkt;
922 struct nexio_priv *priv = usbtouch->priv;
923 unsigned int data_len = be16_to_cpu(packet->data_len);
924 unsigned int x_len = be16_to_cpu(packet->x_len);
925 unsigned int y_len = be16_to_cpu(packet->y_len);
926 int x, y, begin_x, begin_y, end_x, end_y, w, h, ret;
928 /* got touch data? */
929 if ((pkt[0] & 0xe0) != 0xe0)
930 return 0;
932 if (data_len > 0xff)
933 data_len -= 0x100;
934 if (x_len > 0xff)
935 x_len -= 0x80;
937 /* send ACK */
938 ret = usb_submit_urb(priv->ack, GFP_ATOMIC);
940 if (!usbtouch->type->max_xc) {
941 usbtouch->type->max_xc = 2 * x_len;
942 input_set_abs_params(usbtouch->input, ABS_X,
943 0, usbtouch->type->max_xc, 0, 0);
944 usbtouch->type->max_yc = 2 * y_len;
945 input_set_abs_params(usbtouch->input, ABS_Y,
946 0, usbtouch->type->max_yc, 0, 0);
949 * The device reports state of IR sensors on X and Y axes.
950 * Each byte represents "darkness" percentage (0-100) of one element.
951 * 17" touchscreen reports only 64 x 52 bytes so the resolution is low.
952 * This also means that there's a limited multi-touch capability but
953 * it's disabled (and untested) here as there's no X driver for that.
955 begin_x = end_x = begin_y = end_y = -1;
956 for (x = 0; x < x_len; x++) {
957 if (begin_x == -1 && packet->data[x] > NEXIO_THRESHOLD) {
958 begin_x = x;
959 continue;
961 if (end_x == -1 && begin_x != -1 && packet->data[x] < NEXIO_THRESHOLD) {
962 end_x = x - 1;
963 for (y = x_len; y < data_len; y++) {
964 if (begin_y == -1 && packet->data[y] > NEXIO_THRESHOLD) {
965 begin_y = y - x_len;
966 continue;
968 if (end_y == -1 &&
969 begin_y != -1 && packet->data[y] < NEXIO_THRESHOLD) {
970 end_y = y - 1 - x_len;
971 w = end_x - begin_x;
972 h = end_y - begin_y;
973 #if 0
974 /* multi-touch */
975 input_report_abs(usbtouch->input,
976 ABS_MT_TOUCH_MAJOR, max(w,h));
977 input_report_abs(usbtouch->input,
978 ABS_MT_TOUCH_MINOR, min(x,h));
979 input_report_abs(usbtouch->input,
980 ABS_MT_POSITION_X, 2*begin_x+w);
981 input_report_abs(usbtouch->input,
982 ABS_MT_POSITION_Y, 2*begin_y+h);
983 input_report_abs(usbtouch->input,
984 ABS_MT_ORIENTATION, w > h);
985 input_mt_sync(usbtouch->input);
986 #endif
987 /* single touch */
988 usbtouch->x = 2 * begin_x + w;
989 usbtouch->y = 2 * begin_y + h;
990 usbtouch->touch = packet->flags & 0x01;
991 begin_y = end_y = -1;
992 return 1;
995 begin_x = end_x = -1;
999 return 0;
1001 #endif
1004 /*****************************************************************************
1005 * ELO part
1008 #ifdef CONFIG_TOUCHSCREEN_USB_ELO
1010 static int elo_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
1012 dev->x = (pkt[3] << 8) | pkt[2];
1013 dev->y = (pkt[5] << 8) | pkt[4];
1014 dev->touch = pkt[6] > 0;
1015 dev->press = pkt[6];
1017 return 1;
1019 #endif
1022 /*****************************************************************************
1023 * the different device descriptors
1025 #ifdef MULTI_PACKET
1026 static void usbtouch_process_multi(struct usbtouch_usb *usbtouch,
1027 unsigned char *pkt, int len);
1028 #endif
1030 static struct usbtouch_device_info usbtouch_dev_info[] = {
1031 #ifdef CONFIG_TOUCHSCREEN_USB_ELO
1032 [DEVTYPE_ELO] = {
1033 .min_xc = 0x0,
1034 .max_xc = 0x0fff,
1035 .min_yc = 0x0,
1036 .max_yc = 0x0fff,
1037 .max_press = 0xff,
1038 .rept_size = 8,
1039 .read_data = elo_read_data,
1041 #endif
1043 #ifdef CONFIG_TOUCHSCREEN_USB_EGALAX
1044 [DEVTYPE_EGALAX] = {
1045 .min_xc = 0x0,
1046 .max_xc = 0x07ff,
1047 .min_yc = 0x0,
1048 .max_yc = 0x07ff,
1049 .rept_size = 16,
1050 .process_pkt = usbtouch_process_multi,
1051 .get_pkt_len = egalax_get_pkt_len,
1052 .read_data = egalax_read_data,
1054 #endif
1056 #ifdef CONFIG_TOUCHSCREEN_USB_PANJIT
1057 [DEVTYPE_PANJIT] = {
1058 .min_xc = 0x0,
1059 .max_xc = 0x0fff,
1060 .min_yc = 0x0,
1061 .max_yc = 0x0fff,
1062 .rept_size = 8,
1063 .read_data = panjit_read_data,
1065 #endif
1067 #ifdef CONFIG_TOUCHSCREEN_USB_3M
1068 [DEVTYPE_3M] = {
1069 .min_xc = 0x0,
1070 .max_xc = 0x4000,
1071 .min_yc = 0x0,
1072 .max_yc = 0x4000,
1073 .rept_size = 11,
1074 .read_data = mtouch_read_data,
1075 .init = mtouch_init,
1077 #endif
1079 #ifdef CONFIG_TOUCHSCREEN_USB_ITM
1080 [DEVTYPE_ITM] = {
1081 .min_xc = 0x0,
1082 .max_xc = 0x0fff,
1083 .min_yc = 0x0,
1084 .max_yc = 0x0fff,
1085 .max_press = 0xff,
1086 .rept_size = 8,
1087 .read_data = itm_read_data,
1089 #endif
1091 #ifdef CONFIG_TOUCHSCREEN_USB_ETURBO
1092 [DEVTYPE_ETURBO] = {
1093 .min_xc = 0x0,
1094 .max_xc = 0x07ff,
1095 .min_yc = 0x0,
1096 .max_yc = 0x07ff,
1097 .rept_size = 8,
1098 .process_pkt = usbtouch_process_multi,
1099 .get_pkt_len = eturbo_get_pkt_len,
1100 .read_data = eturbo_read_data,
1102 #endif
1104 #ifdef CONFIG_TOUCHSCREEN_USB_GUNZE
1105 [DEVTYPE_GUNZE] = {
1106 .min_xc = 0x0,
1107 .max_xc = 0x0fff,
1108 .min_yc = 0x0,
1109 .max_yc = 0x0fff,
1110 .rept_size = 4,
1111 .read_data = gunze_read_data,
1113 #endif
1115 #ifdef CONFIG_TOUCHSCREEN_USB_DMC_TSC10
1116 [DEVTYPE_DMC_TSC10] = {
1117 .min_xc = 0x0,
1118 .max_xc = 0x03ff,
1119 .min_yc = 0x0,
1120 .max_yc = 0x03ff,
1121 .rept_size = 5,
1122 .init = dmc_tsc10_init,
1123 .read_data = dmc_tsc10_read_data,
1125 #endif
1127 #ifdef CONFIG_TOUCHSCREEN_USB_IRTOUCH
1128 [DEVTYPE_IRTOUCH] = {
1129 .min_xc = 0x0,
1130 .max_xc = 0x0fff,
1131 .min_yc = 0x0,
1132 .max_yc = 0x0fff,
1133 .rept_size = 8,
1134 .read_data = irtouch_read_data,
1136 #endif
1138 #ifdef CONFIG_TOUCHSCREEN_USB_IDEALTEK
1139 [DEVTYPE_IDEALTEK] = {
1140 .min_xc = 0x0,
1141 .max_xc = 0x0fff,
1142 .min_yc = 0x0,
1143 .max_yc = 0x0fff,
1144 .rept_size = 8,
1145 .process_pkt = usbtouch_process_multi,
1146 .get_pkt_len = idealtek_get_pkt_len,
1147 .read_data = idealtek_read_data,
1149 #endif
1151 #ifdef CONFIG_TOUCHSCREEN_USB_GENERAL_TOUCH
1152 [DEVTYPE_GENERAL_TOUCH] = {
1153 .min_xc = 0x0,
1154 .max_xc = 0x7fff,
1155 .min_yc = 0x0,
1156 .max_yc = 0x7fff,
1157 .rept_size = 7,
1158 .read_data = general_touch_read_data,
1160 #endif
1162 #ifdef CONFIG_TOUCHSCREEN_USB_GOTOP
1163 [DEVTYPE_GOTOP] = {
1164 .min_xc = 0x0,
1165 .max_xc = 0x03ff,
1166 .min_yc = 0x0,
1167 .max_yc = 0x03ff,
1168 .rept_size = 4,
1169 .read_data = gotop_read_data,
1171 #endif
1173 #ifdef CONFIG_TOUCHSCREEN_USB_JASTEC
1174 [DEVTYPE_JASTEC] = {
1175 .min_xc = 0x0,
1176 .max_xc = 0x0fff,
1177 .min_yc = 0x0,
1178 .max_yc = 0x0fff,
1179 .rept_size = 4,
1180 .read_data = jastec_read_data,
1182 #endif
1184 #ifdef CONFIG_TOUCHSCREEN_USB_E2I
1185 [DEVTYPE_E2I] = {
1186 .min_xc = 0x0,
1187 .max_xc = 0x7fff,
1188 .min_yc = 0x0,
1189 .max_yc = 0x7fff,
1190 .rept_size = 6,
1191 .init = e2i_init,
1192 .read_data = e2i_read_data,
1194 #endif
1196 #ifdef CONFIG_TOUCHSCREEN_USB_ZYTRONIC
1197 [DEVTYPE_ZYTRONIC] = {
1198 .min_xc = 0x0,
1199 .max_xc = 0x03ff,
1200 .min_yc = 0x0,
1201 .max_yc = 0x03ff,
1202 .rept_size = 5,
1203 .read_data = zytronic_read_data,
1204 .irq_always = true,
1206 #endif
1208 #ifdef CONFIG_TOUCHSCREEN_USB_ETT_TC45USB
1209 [DEVTYPE_TC45USB] = {
1210 .min_xc = 0x0,
1211 .max_xc = 0x0fff,
1212 .min_yc = 0x0,
1213 .max_yc = 0x0fff,
1214 .rept_size = 5,
1215 .read_data = tc45usb_read_data,
1217 #endif
1219 #ifdef CONFIG_TOUCHSCREEN_USB_NEXIO
1220 [DEVTYPE_NEXIO] = {
1221 .rept_size = 1024,
1222 .irq_always = true,
1223 .read_data = nexio_read_data,
1224 .alloc = nexio_alloc,
1225 .init = nexio_init,
1226 .exit = nexio_exit,
1228 #endif
1229 #ifdef CONFIG_TOUCHSCREEN_USB_EASYTOUCH
1230 [DEVTYPE_ETOUCH] = {
1231 .min_xc = 0x0,
1232 .max_xc = 0x07ff,
1233 .min_yc = 0x0,
1234 .max_yc = 0x07ff,
1235 .rept_size = 16,
1236 .process_pkt = usbtouch_process_multi,
1237 .get_pkt_len = etouch_get_pkt_len,
1238 .read_data = etouch_read_data,
1240 #endif
1244 /*****************************************************************************
1245 * Generic Part
1247 static void usbtouch_process_pkt(struct usbtouch_usb *usbtouch,
1248 unsigned char *pkt, int len)
1250 struct usbtouch_device_info *type = usbtouch->type;
1252 if (!type->read_data(usbtouch, pkt))
1253 return;
1255 input_report_key(usbtouch->input, BTN_TOUCH, usbtouch->touch);
1257 if (swap_xy) {
1258 input_report_abs(usbtouch->input, ABS_X, usbtouch->y);
1259 input_report_abs(usbtouch->input, ABS_Y, usbtouch->x);
1260 } else {
1261 input_report_abs(usbtouch->input, ABS_X, usbtouch->x);
1262 input_report_abs(usbtouch->input, ABS_Y, usbtouch->y);
1264 if (type->max_press)
1265 input_report_abs(usbtouch->input, ABS_PRESSURE, usbtouch->press);
1266 input_sync(usbtouch->input);
1270 #ifdef MULTI_PACKET
1271 static void usbtouch_process_multi(struct usbtouch_usb *usbtouch,
1272 unsigned char *pkt, int len)
1274 unsigned char *buffer;
1275 int pkt_len, pos, buf_len, tmp;
1277 /* process buffer */
1278 if (unlikely(usbtouch->buf_len)) {
1279 /* try to get size */
1280 pkt_len = usbtouch->type->get_pkt_len(
1281 usbtouch->buffer, usbtouch->buf_len);
1283 /* drop? */
1284 if (unlikely(!pkt_len))
1285 goto out_flush_buf;
1287 /* need to append -pkt_len bytes before able to get size */
1288 if (unlikely(pkt_len < 0)) {
1289 int append = -pkt_len;
1290 if (unlikely(append > len))
1291 append = len;
1292 if (usbtouch->buf_len + append >= usbtouch->type->rept_size)
1293 goto out_flush_buf;
1294 memcpy(usbtouch->buffer + usbtouch->buf_len, pkt, append);
1295 usbtouch->buf_len += append;
1297 pkt_len = usbtouch->type->get_pkt_len(
1298 usbtouch->buffer, usbtouch->buf_len);
1299 if (pkt_len < 0)
1300 return;
1303 /* append */
1304 tmp = pkt_len - usbtouch->buf_len;
1305 if (usbtouch->buf_len + tmp >= usbtouch->type->rept_size)
1306 goto out_flush_buf;
1307 memcpy(usbtouch->buffer + usbtouch->buf_len, pkt, tmp);
1308 usbtouch_process_pkt(usbtouch, usbtouch->buffer, pkt_len);
1310 buffer = pkt + tmp;
1311 buf_len = len - tmp;
1312 } else {
1313 buffer = pkt;
1314 buf_len = len;
1317 /* loop over the received packet, process */
1318 pos = 0;
1319 while (pos < buf_len) {
1320 /* get packet len */
1321 pkt_len = usbtouch->type->get_pkt_len(buffer + pos,
1322 buf_len - pos);
1324 /* unknown packet: skip one byte */
1325 if (unlikely(!pkt_len)) {
1326 pos++;
1327 continue;
1330 /* full packet: process */
1331 if (likely((pkt_len > 0) && (pkt_len <= buf_len - pos))) {
1332 usbtouch_process_pkt(usbtouch, buffer + pos, pkt_len);
1333 } else {
1334 /* incomplete packet: save in buffer */
1335 memcpy(usbtouch->buffer, buffer + pos, buf_len - pos);
1336 usbtouch->buf_len = buf_len - pos;
1337 return;
1339 pos += pkt_len;
1342 out_flush_buf:
1343 usbtouch->buf_len = 0;
1344 return;
1346 #endif
1349 static void usbtouch_irq(struct urb *urb)
1351 struct usbtouch_usb *usbtouch = urb->context;
1352 int retval;
1354 switch (urb->status) {
1355 case 0:
1356 /* success */
1357 break;
1358 case -ETIME:
1359 /* this urb is timing out */
1360 dbg("%s - urb timed out - was the device unplugged?",
1361 __func__);
1362 return;
1363 case -ECONNRESET:
1364 case -ENOENT:
1365 case -ESHUTDOWN:
1366 case -EPIPE:
1367 /* this urb is terminated, clean up */
1368 dbg("%s - urb shutting down with status: %d",
1369 __func__, urb->status);
1370 return;
1371 default:
1372 dbg("%s - nonzero urb status received: %d",
1373 __func__, urb->status);
1374 goto exit;
1377 usbtouch->type->process_pkt(usbtouch, usbtouch->data, urb->actual_length);
1379 exit:
1380 usb_mark_last_busy(interface_to_usbdev(usbtouch->interface));
1381 retval = usb_submit_urb(urb, GFP_ATOMIC);
1382 if (retval)
1383 err("%s - usb_submit_urb failed with result: %d",
1384 __func__, retval);
1387 static int usbtouch_open(struct input_dev *input)
1389 struct usbtouch_usb *usbtouch = input_get_drvdata(input);
1390 int r;
1392 usbtouch->irq->dev = interface_to_usbdev(usbtouch->interface);
1394 r = usb_autopm_get_interface(usbtouch->interface) ? -EIO : 0;
1395 if (r < 0)
1396 goto out;
1398 if (!usbtouch->type->irq_always) {
1399 if (usb_submit_urb(usbtouch->irq, GFP_KERNEL)) {
1400 r = -EIO;
1401 goto out_put;
1405 usbtouch->interface->needs_remote_wakeup = 1;
1406 out_put:
1407 usb_autopm_put_interface(usbtouch->interface);
1408 out:
1409 return r;
1412 static void usbtouch_close(struct input_dev *input)
1414 struct usbtouch_usb *usbtouch = input_get_drvdata(input);
1415 int r;
1417 if (!usbtouch->type->irq_always)
1418 usb_kill_urb(usbtouch->irq);
1419 r = usb_autopm_get_interface(usbtouch->interface);
1420 usbtouch->interface->needs_remote_wakeup = 0;
1421 if (!r)
1422 usb_autopm_put_interface(usbtouch->interface);
1425 static int usbtouch_suspend
1426 (struct usb_interface *intf, pm_message_t message)
1428 struct usbtouch_usb *usbtouch = usb_get_intfdata(intf);
1430 usb_kill_urb(usbtouch->irq);
1432 return 0;
1435 static int usbtouch_resume(struct usb_interface *intf)
1437 struct usbtouch_usb *usbtouch = usb_get_intfdata(intf);
1438 struct input_dev *input = usbtouch->input;
1439 int result = 0;
1441 mutex_lock(&input->mutex);
1442 if (input->users || usbtouch->type->irq_always)
1443 result = usb_submit_urb(usbtouch->irq, GFP_NOIO);
1444 mutex_unlock(&input->mutex);
1446 return result;
1449 static int usbtouch_reset_resume(struct usb_interface *intf)
1451 struct usbtouch_usb *usbtouch = usb_get_intfdata(intf);
1452 struct input_dev *input = usbtouch->input;
1453 int err = 0;
1455 /* reinit the device */
1456 if (usbtouch->type->init) {
1457 err = usbtouch->type->init(usbtouch);
1458 if (err) {
1459 dbg("%s - type->init() failed, err: %d",
1460 __func__, err);
1461 return err;
1465 /* restart IO if needed */
1466 mutex_lock(&input->mutex);
1467 if (input->users)
1468 err = usb_submit_urb(usbtouch->irq, GFP_NOIO);
1469 mutex_unlock(&input->mutex);
1471 return err;
1474 static void usbtouch_free_buffers(struct usb_device *udev,
1475 struct usbtouch_usb *usbtouch)
1477 usb_free_coherent(udev, usbtouch->type->rept_size,
1478 usbtouch->data, usbtouch->data_dma);
1479 kfree(usbtouch->buffer);
1482 static struct usb_endpoint_descriptor *
1483 usbtouch_get_input_endpoint(struct usb_host_interface *interface)
1485 int i;
1487 for (i = 0; i < interface->desc.bNumEndpoints; i++)
1488 if (usb_endpoint_dir_in(&interface->endpoint[i].desc))
1489 return &interface->endpoint[i].desc;
1491 return NULL;
1494 static int usbtouch_probe(struct usb_interface *intf,
1495 const struct usb_device_id *id)
1497 struct usbtouch_usb *usbtouch;
1498 struct input_dev *input_dev;
1499 struct usb_endpoint_descriptor *endpoint;
1500 struct usb_device *udev = interface_to_usbdev(intf);
1501 struct usbtouch_device_info *type;
1502 int err = -ENOMEM;
1504 /* some devices are ignored */
1505 if (id->driver_info == DEVTYPE_IGNORE)
1506 return -ENODEV;
1508 endpoint = usbtouch_get_input_endpoint(intf->cur_altsetting);
1509 if (!endpoint)
1510 return -ENXIO;
1512 usbtouch = kzalloc(sizeof(struct usbtouch_usb), GFP_KERNEL);
1513 input_dev = input_allocate_device();
1514 if (!usbtouch || !input_dev)
1515 goto out_free;
1517 type = &usbtouch_dev_info[id->driver_info];
1518 usbtouch->type = type;
1519 if (!type->process_pkt)
1520 type->process_pkt = usbtouch_process_pkt;
1522 usbtouch->data = usb_alloc_coherent(udev, type->rept_size,
1523 GFP_KERNEL, &usbtouch->data_dma);
1524 if (!usbtouch->data)
1525 goto out_free;
1527 if (type->get_pkt_len) {
1528 usbtouch->buffer = kmalloc(type->rept_size, GFP_KERNEL);
1529 if (!usbtouch->buffer)
1530 goto out_free_buffers;
1533 usbtouch->irq = usb_alloc_urb(0, GFP_KERNEL);
1534 if (!usbtouch->irq) {
1535 dbg("%s - usb_alloc_urb failed: usbtouch->irq", __func__);
1536 goto out_free_buffers;
1539 usbtouch->interface = intf;
1540 usbtouch->input = input_dev;
1542 if (udev->manufacturer)
1543 strlcpy(usbtouch->name, udev->manufacturer, sizeof(usbtouch->name));
1545 if (udev->product) {
1546 if (udev->manufacturer)
1547 strlcat(usbtouch->name, " ", sizeof(usbtouch->name));
1548 strlcat(usbtouch->name, udev->product, sizeof(usbtouch->name));
1551 if (!strlen(usbtouch->name))
1552 snprintf(usbtouch->name, sizeof(usbtouch->name),
1553 "USB Touchscreen %04x:%04x",
1554 le16_to_cpu(udev->descriptor.idVendor),
1555 le16_to_cpu(udev->descriptor.idProduct));
1557 usb_make_path(udev, usbtouch->phys, sizeof(usbtouch->phys));
1558 strlcat(usbtouch->phys, "/input0", sizeof(usbtouch->phys));
1560 input_dev->name = usbtouch->name;
1561 input_dev->phys = usbtouch->phys;
1562 usb_to_input_id(udev, &input_dev->id);
1563 input_dev->dev.parent = &intf->dev;
1565 input_set_drvdata(input_dev, usbtouch);
1567 input_dev->open = usbtouch_open;
1568 input_dev->close = usbtouch_close;
1570 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
1571 input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
1572 input_set_abs_params(input_dev, ABS_X, type->min_xc, type->max_xc, 0, 0);
1573 input_set_abs_params(input_dev, ABS_Y, type->min_yc, type->max_yc, 0, 0);
1574 if (type->max_press)
1575 input_set_abs_params(input_dev, ABS_PRESSURE, type->min_press,
1576 type->max_press, 0, 0);
1578 if (usb_endpoint_type(endpoint) == USB_ENDPOINT_XFER_INT)
1579 usb_fill_int_urb(usbtouch->irq, udev,
1580 usb_rcvintpipe(udev, endpoint->bEndpointAddress),
1581 usbtouch->data, type->rept_size,
1582 usbtouch_irq, usbtouch, endpoint->bInterval);
1583 else
1584 usb_fill_bulk_urb(usbtouch->irq, udev,
1585 usb_rcvbulkpipe(udev, endpoint->bEndpointAddress),
1586 usbtouch->data, type->rept_size,
1587 usbtouch_irq, usbtouch);
1589 usbtouch->irq->dev = udev;
1590 usbtouch->irq->transfer_dma = usbtouch->data_dma;
1591 usbtouch->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1593 /* device specific allocations */
1594 if (type->alloc) {
1595 err = type->alloc(usbtouch);
1596 if (err) {
1597 dbg("%s - type->alloc() failed, err: %d", __func__, err);
1598 goto out_free_urb;
1602 /* device specific initialisation*/
1603 if (type->init) {
1604 err = type->init(usbtouch);
1605 if (err) {
1606 dbg("%s - type->init() failed, err: %d", __func__, err);
1607 goto out_do_exit;
1611 err = input_register_device(usbtouch->input);
1612 if (err) {
1613 dbg("%s - input_register_device failed, err: %d", __func__, err);
1614 goto out_do_exit;
1617 usb_set_intfdata(intf, usbtouch);
1619 if (usbtouch->type->irq_always) {
1620 /* this can't fail */
1621 usb_autopm_get_interface(intf);
1622 err = usb_submit_urb(usbtouch->irq, GFP_KERNEL);
1623 if (err) {
1624 usb_autopm_put_interface(intf);
1625 err("%s - usb_submit_urb failed with result: %d",
1626 __func__, err);
1627 goto out_unregister_input;
1631 return 0;
1633 out_unregister_input:
1634 input_unregister_device(input_dev);
1635 input_dev = NULL;
1636 out_do_exit:
1637 if (type->exit)
1638 type->exit(usbtouch);
1639 out_free_urb:
1640 usb_free_urb(usbtouch->irq);
1641 out_free_buffers:
1642 usbtouch_free_buffers(udev, usbtouch);
1643 out_free:
1644 input_free_device(input_dev);
1645 kfree(usbtouch);
1646 return err;
1649 static void usbtouch_disconnect(struct usb_interface *intf)
1651 struct usbtouch_usb *usbtouch = usb_get_intfdata(intf);
1653 dbg("%s - called", __func__);
1655 if (!usbtouch)
1656 return;
1658 dbg("%s - usbtouch is initialized, cleaning up", __func__);
1659 usb_set_intfdata(intf, NULL);
1660 /* this will stop IO via close */
1661 input_unregister_device(usbtouch->input);
1662 usb_free_urb(usbtouch->irq);
1663 if (usbtouch->type->exit)
1664 usbtouch->type->exit(usbtouch);
1665 usbtouch_free_buffers(interface_to_usbdev(intf), usbtouch);
1666 kfree(usbtouch);
1669 MODULE_DEVICE_TABLE(usb, usbtouch_devices);
1671 static struct usb_driver usbtouch_driver = {
1672 .name = "usbtouchscreen",
1673 .probe = usbtouch_probe,
1674 .disconnect = usbtouch_disconnect,
1675 .suspend = usbtouch_suspend,
1676 .resume = usbtouch_resume,
1677 .reset_resume = usbtouch_reset_resume,
1678 .id_table = usbtouch_devices,
1679 .supports_autosuspend = 1,
1682 module_usb_driver(usbtouch_driver);
1684 MODULE_AUTHOR(DRIVER_AUTHOR);
1685 MODULE_DESCRIPTION(DRIVER_DESC);
1686 MODULE_LICENSE("GPL");
1688 MODULE_ALIAS("touchkitusb");
1689 MODULE_ALIAS("itmtouch");
1690 MODULE_ALIAS("mtouchusb");