mfd: wm8350-i2c: Make sure the i2c regmap functions are compiled
[linux/fpc-iii.git] / drivers / input / touchscreen / usbtouchscreen.c
blob5679cd9003cca9420cccce44fb8edb0f5433b2c2
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 int data_size;
110 unsigned char *buffer;
111 int buf_len;
112 struct urb *irq;
113 struct usb_interface *interface;
114 struct input_dev *input;
115 struct usbtouch_device_info *type;
116 char name[128];
117 char phys[64];
118 void *priv;
120 int x, y;
121 int touch, press;
125 /* device types */
126 enum {
127 DEVTYPE_IGNORE = -1,
128 DEVTYPE_EGALAX,
129 DEVTYPE_PANJIT,
130 DEVTYPE_3M,
131 DEVTYPE_ITM,
132 DEVTYPE_ETURBO,
133 DEVTYPE_GUNZE,
134 DEVTYPE_DMC_TSC10,
135 DEVTYPE_IRTOUCH,
136 DEVTYPE_IRTOUCH_HIRES,
137 DEVTYPE_IDEALTEK,
138 DEVTYPE_GENERAL_TOUCH,
139 DEVTYPE_GOTOP,
140 DEVTYPE_JASTEC,
141 DEVTYPE_E2I,
142 DEVTYPE_ZYTRONIC,
143 DEVTYPE_TC45USB,
144 DEVTYPE_NEXIO,
145 DEVTYPE_ELO,
146 DEVTYPE_ETOUCH,
149 #define USB_DEVICE_HID_CLASS(vend, prod) \
150 .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS \
151 | USB_DEVICE_ID_MATCH_DEVICE, \
152 .idVendor = (vend), \
153 .idProduct = (prod), \
154 .bInterfaceClass = USB_INTERFACE_CLASS_HID
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 {USB_DEVICE(0x6615, 0x0012), .driver_info = DEVTYPE_IRTOUCH_HIRES},
204 #endif
206 #ifdef CONFIG_TOUCHSCREEN_USB_IDEALTEK
207 {USB_DEVICE(0x1391, 0x1000), .driver_info = DEVTYPE_IDEALTEK},
208 #endif
210 #ifdef CONFIG_TOUCHSCREEN_USB_GENERAL_TOUCH
211 {USB_DEVICE(0x0dfc, 0x0001), .driver_info = DEVTYPE_GENERAL_TOUCH},
212 #endif
214 #ifdef CONFIG_TOUCHSCREEN_USB_GOTOP
215 {USB_DEVICE(0x08f2, 0x007f), .driver_info = DEVTYPE_GOTOP},
216 {USB_DEVICE(0x08f2, 0x00ce), .driver_info = DEVTYPE_GOTOP},
217 {USB_DEVICE(0x08f2, 0x00f4), .driver_info = DEVTYPE_GOTOP},
218 #endif
220 #ifdef CONFIG_TOUCHSCREEN_USB_JASTEC
221 {USB_DEVICE(0x0f92, 0x0001), .driver_info = DEVTYPE_JASTEC},
222 #endif
224 #ifdef CONFIG_TOUCHSCREEN_USB_E2I
225 {USB_DEVICE(0x1ac7, 0x0001), .driver_info = DEVTYPE_E2I},
226 #endif
228 #ifdef CONFIG_TOUCHSCREEN_USB_ZYTRONIC
229 {USB_DEVICE(0x14c8, 0x0003), .driver_info = DEVTYPE_ZYTRONIC},
230 #endif
232 #ifdef CONFIG_TOUCHSCREEN_USB_ETT_TC45USB
233 /* TC5UH */
234 {USB_DEVICE(0x0664, 0x0309), .driver_info = DEVTYPE_TC45USB},
235 /* TC4UM */
236 {USB_DEVICE(0x0664, 0x0306), .driver_info = DEVTYPE_TC45USB},
237 #endif
239 #ifdef CONFIG_TOUCHSCREEN_USB_NEXIO
240 /* data interface only */
241 {USB_DEVICE_AND_INTERFACE_INFO(0x10f0, 0x2002, 0x0a, 0x00, 0x00),
242 .driver_info = DEVTYPE_NEXIO},
243 {USB_DEVICE_AND_INTERFACE_INFO(0x1870, 0x0001, 0x0a, 0x00, 0x00),
244 .driver_info = DEVTYPE_NEXIO},
245 #endif
247 #ifdef CONFIG_TOUCHSCREEN_USB_ELO
248 {USB_DEVICE(0x04e7, 0x0020), .driver_info = DEVTYPE_ELO},
249 #endif
251 #ifdef CONFIG_TOUCHSCREEN_USB_EASYTOUCH
252 {USB_DEVICE(0x7374, 0x0001), .driver_info = DEVTYPE_ETOUCH},
253 #endif
259 /*****************************************************************************
260 * e2i Part
263 #ifdef CONFIG_TOUCHSCREEN_USB_E2I
264 static int e2i_init(struct usbtouch_usb *usbtouch)
266 int ret;
267 struct usb_device *udev = interface_to_usbdev(usbtouch->interface);
269 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
270 0x01, 0x02, 0x0000, 0x0081,
271 NULL, 0, USB_CTRL_SET_TIMEOUT);
273 dev_dbg(&usbtouch->interface->dev,
274 "%s - usb_control_msg - E2I_RESET - bytes|err: %d\n",
275 __func__, ret);
276 return ret;
279 static int e2i_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
281 int tmp = (pkt[0] << 8) | pkt[1];
282 dev->x = (pkt[2] << 8) | pkt[3];
283 dev->y = (pkt[4] << 8) | pkt[5];
285 tmp = tmp - 0xA000;
286 dev->touch = (tmp > 0);
287 dev->press = (tmp > 0 ? tmp : 0);
289 return 1;
291 #endif
294 /*****************************************************************************
295 * eGalax part
298 #ifdef CONFIG_TOUCHSCREEN_USB_EGALAX
300 #ifndef MULTI_PACKET
301 #define MULTI_PACKET
302 #endif
304 #define EGALAX_PKT_TYPE_MASK 0xFE
305 #define EGALAX_PKT_TYPE_REPT 0x80
306 #define EGALAX_PKT_TYPE_DIAG 0x0A
308 static int egalax_init(struct usbtouch_usb *usbtouch)
310 int ret, i;
311 unsigned char *buf;
312 struct usb_device *udev = interface_to_usbdev(usbtouch->interface);
315 * An eGalax diagnostic packet kicks the device into using the right
316 * protocol. We send a "check active" packet. The response will be
317 * read later and ignored.
320 buf = kmalloc(3, GFP_KERNEL);
321 if (!buf)
322 return -ENOMEM;
324 buf[0] = EGALAX_PKT_TYPE_DIAG;
325 buf[1] = 1; /* length */
326 buf[2] = 'A'; /* command - check active */
328 for (i = 0; i < 3; i++) {
329 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
331 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
332 0, 0, buf, 3,
333 USB_CTRL_SET_TIMEOUT);
334 if (ret >= 0) {
335 ret = 0;
336 break;
338 if (ret != -EPIPE)
339 break;
342 kfree(buf);
344 return ret;
347 static int egalax_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
349 if ((pkt[0] & EGALAX_PKT_TYPE_MASK) != EGALAX_PKT_TYPE_REPT)
350 return 0;
352 dev->x = ((pkt[3] & 0x0F) << 7) | (pkt[4] & 0x7F);
353 dev->y = ((pkt[1] & 0x0F) << 7) | (pkt[2] & 0x7F);
354 dev->touch = pkt[0] & 0x01;
356 return 1;
359 static int egalax_get_pkt_len(unsigned char *buf, int len)
361 switch (buf[0] & EGALAX_PKT_TYPE_MASK) {
362 case EGALAX_PKT_TYPE_REPT:
363 return 5;
365 case EGALAX_PKT_TYPE_DIAG:
366 if (len < 2)
367 return -1;
369 return buf[1] + 2;
372 return 0;
374 #endif
376 /*****************************************************************************
377 * EasyTouch part
380 #ifdef CONFIG_TOUCHSCREEN_USB_EASYTOUCH
382 #ifndef MULTI_PACKET
383 #define MULTI_PACKET
384 #endif
386 #define ETOUCH_PKT_TYPE_MASK 0xFE
387 #define ETOUCH_PKT_TYPE_REPT 0x80
388 #define ETOUCH_PKT_TYPE_REPT2 0xB0
389 #define ETOUCH_PKT_TYPE_DIAG 0x0A
391 static int etouch_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
393 if ((pkt[0] & ETOUCH_PKT_TYPE_MASK) != ETOUCH_PKT_TYPE_REPT &&
394 (pkt[0] & ETOUCH_PKT_TYPE_MASK) != ETOUCH_PKT_TYPE_REPT2)
395 return 0;
397 dev->x = ((pkt[1] & 0x1F) << 7) | (pkt[2] & 0x7F);
398 dev->y = ((pkt[3] & 0x1F) << 7) | (pkt[4] & 0x7F);
399 dev->touch = pkt[0] & 0x01;
401 return 1;
404 static int etouch_get_pkt_len(unsigned char *buf, int len)
406 switch (buf[0] & ETOUCH_PKT_TYPE_MASK) {
407 case ETOUCH_PKT_TYPE_REPT:
408 case ETOUCH_PKT_TYPE_REPT2:
409 return 5;
411 case ETOUCH_PKT_TYPE_DIAG:
412 if (len < 2)
413 return -1;
415 return buf[1] + 2;
418 return 0;
420 #endif
422 /*****************************************************************************
423 * PanJit Part
425 #ifdef CONFIG_TOUCHSCREEN_USB_PANJIT
426 static int panjit_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
428 dev->x = ((pkt[2] & 0x0F) << 8) | pkt[1];
429 dev->y = ((pkt[4] & 0x0F) << 8) | pkt[3];
430 dev->touch = pkt[0] & 0x01;
432 return 1;
434 #endif
437 /*****************************************************************************
438 * 3M/Microtouch Part
440 #ifdef CONFIG_TOUCHSCREEN_USB_3M
442 #define MTOUCHUSB_ASYNC_REPORT 1
443 #define MTOUCHUSB_RESET 7
444 #define MTOUCHUSB_REQ_CTRLLR_ID 10
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 static int mtouch_init(struct usbtouch_usb *usbtouch)
462 int ret, i;
463 struct usb_device *udev = interface_to_usbdev(usbtouch->interface);
465 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
466 MTOUCHUSB_RESET,
467 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
468 1, 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
469 dev_dbg(&usbtouch->interface->dev,
470 "%s - usb_control_msg - MTOUCHUSB_RESET - bytes|err: %d\n",
471 __func__, ret);
472 if (ret < 0)
473 return ret;
474 msleep(150);
476 for (i = 0; i < 3; i++) {
477 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
478 MTOUCHUSB_ASYNC_REPORT,
479 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
480 1, 1, NULL, 0, USB_CTRL_SET_TIMEOUT);
481 dev_dbg(&usbtouch->interface->dev,
482 "%s - usb_control_msg - MTOUCHUSB_ASYNC_REPORT - bytes|err: %d\n",
483 __func__, ret);
484 if (ret >= 0)
485 break;
486 if (ret != -EPIPE)
487 return ret;
490 /* Default min/max xy are the raw values, override if using hw-calib */
491 if (hwcalib_xy) {
492 input_set_abs_params(usbtouch->input, ABS_X, 0, 0xffff, 0, 0);
493 input_set_abs_params(usbtouch->input, ABS_Y, 0, 0xffff, 0, 0);
496 return 0;
498 #endif
501 /*****************************************************************************
502 * ITM Part
504 #ifdef CONFIG_TOUCHSCREEN_USB_ITM
505 static int itm_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
507 int touch;
509 * ITM devices report invalid x/y data if not touched.
510 * if the screen was touched before but is not touched any more
511 * report touch as 0 with the last valid x/y data once. then stop
512 * reporting data until touched again.
514 dev->press = ((pkt[2] & 0x01) << 7) | (pkt[5] & 0x7F);
516 touch = ~pkt[7] & 0x20;
517 if (!touch) {
518 if (dev->touch) {
519 dev->touch = 0;
520 return 1;
523 return 0;
526 dev->x = ((pkt[0] & 0x1F) << 7) | (pkt[3] & 0x7F);
527 dev->y = ((pkt[1] & 0x1F) << 7) | (pkt[4] & 0x7F);
528 dev->touch = touch;
530 return 1;
532 #endif
535 /*****************************************************************************
536 * eTurboTouch part
538 #ifdef CONFIG_TOUCHSCREEN_USB_ETURBO
539 #ifndef MULTI_PACKET
540 #define MULTI_PACKET
541 #endif
542 static int eturbo_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
544 unsigned int shift;
546 /* packets should start with sync */
547 if (!(pkt[0] & 0x80))
548 return 0;
550 shift = (6 - (pkt[0] & 0x03));
551 dev->x = ((pkt[3] << 7) | pkt[4]) >> shift;
552 dev->y = ((pkt[1] << 7) | pkt[2]) >> shift;
553 dev->touch = (pkt[0] & 0x10) ? 1 : 0;
555 return 1;
558 static int eturbo_get_pkt_len(unsigned char *buf, int len)
560 if (buf[0] & 0x80)
561 return 5;
562 if (buf[0] == 0x01)
563 return 3;
564 return 0;
566 #endif
569 /*****************************************************************************
570 * Gunze part
572 #ifdef CONFIG_TOUCHSCREEN_USB_GUNZE
573 static int gunze_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
575 if (!(pkt[0] & 0x80) || ((pkt[1] | pkt[2] | pkt[3]) & 0x80))
576 return 0;
578 dev->x = ((pkt[0] & 0x1F) << 7) | (pkt[2] & 0x7F);
579 dev->y = ((pkt[1] & 0x1F) << 7) | (pkt[3] & 0x7F);
580 dev->touch = pkt[0] & 0x20;
582 return 1;
584 #endif
586 /*****************************************************************************
587 * DMC TSC-10/25 Part
589 * Documentation about the controller and it's protocol can be found at
590 * http://www.dmccoltd.com/files/controler/tsc10usb_pi_e.pdf
591 * http://www.dmccoltd.com/files/controler/tsc25_usb_e.pdf
593 #ifdef CONFIG_TOUCHSCREEN_USB_DMC_TSC10
595 /* supported data rates. currently using 130 */
596 #define TSC10_RATE_POINT 0x50
597 #define TSC10_RATE_30 0x40
598 #define TSC10_RATE_50 0x41
599 #define TSC10_RATE_80 0x42
600 #define TSC10_RATE_100 0x43
601 #define TSC10_RATE_130 0x44
602 #define TSC10_RATE_150 0x45
604 /* commands */
605 #define TSC10_CMD_RESET 0x55
606 #define TSC10_CMD_RATE 0x05
607 #define TSC10_CMD_DATA1 0x01
609 static int dmc_tsc10_init(struct usbtouch_usb *usbtouch)
611 struct usb_device *dev = interface_to_usbdev(usbtouch->interface);
612 int ret = -ENOMEM;
613 unsigned char *buf;
615 buf = kmalloc(2, GFP_NOIO);
616 if (!buf)
617 goto err_nobuf;
618 /* reset */
619 buf[0] = buf[1] = 0xFF;
620 ret = usb_control_msg(dev, usb_rcvctrlpipe (dev, 0),
621 TSC10_CMD_RESET,
622 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
623 0, 0, buf, 2, USB_CTRL_SET_TIMEOUT);
624 if (ret < 0)
625 goto err_out;
626 if (buf[0] != 0x06) {
627 ret = -ENODEV;
628 goto err_out;
631 /* TSC-25 data sheet specifies a delay after the RESET command */
632 msleep(150);
634 /* set coordinate output rate */
635 buf[0] = buf[1] = 0xFF;
636 ret = usb_control_msg(dev, usb_rcvctrlpipe (dev, 0),
637 TSC10_CMD_RATE,
638 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
639 TSC10_RATE_150, 0, buf, 2, USB_CTRL_SET_TIMEOUT);
640 if (ret < 0)
641 goto err_out;
642 if ((buf[0] != 0x06) && (buf[0] != 0x15 || buf[1] != 0x01)) {
643 ret = -ENODEV;
644 goto err_out;
647 /* start sending data */
648 ret = usb_control_msg(dev, usb_rcvctrlpipe (dev, 0),
649 TSC10_CMD_DATA1,
650 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
651 0, 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
652 err_out:
653 kfree(buf);
654 err_nobuf:
655 return ret;
659 static int dmc_tsc10_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
661 dev->x = ((pkt[2] & 0x03) << 8) | pkt[1];
662 dev->y = ((pkt[4] & 0x03) << 8) | pkt[3];
663 dev->touch = pkt[0] & 0x01;
665 return 1;
667 #endif
670 /*****************************************************************************
671 * IRTOUCH Part
673 #ifdef CONFIG_TOUCHSCREEN_USB_IRTOUCH
674 static int irtouch_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
676 dev->x = (pkt[3] << 8) | pkt[2];
677 dev->y = (pkt[5] << 8) | pkt[4];
678 dev->touch = (pkt[1] & 0x03) ? 1 : 0;
680 return 1;
682 #endif
684 /*****************************************************************************
685 * ET&T TC5UH/TC4UM part
687 #ifdef CONFIG_TOUCHSCREEN_USB_ETT_TC45USB
688 static int tc45usb_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
690 dev->x = ((pkt[2] & 0x0F) << 8) | pkt[1];
691 dev->y = ((pkt[4] & 0x0F) << 8) | pkt[3];
692 dev->touch = pkt[0] & 0x01;
694 return 1;
696 #endif
698 /*****************************************************************************
699 * IdealTEK URTC1000 Part
701 #ifdef CONFIG_TOUCHSCREEN_USB_IDEALTEK
702 #ifndef MULTI_PACKET
703 #define MULTI_PACKET
704 #endif
705 static int idealtek_get_pkt_len(unsigned char *buf, int len)
707 if (buf[0] & 0x80)
708 return 5;
709 if (buf[0] == 0x01)
710 return len;
711 return 0;
714 static int idealtek_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
716 switch (pkt[0] & 0x98) {
717 case 0x88:
718 /* touch data in IdealTEK mode */
719 dev->x = (pkt[1] << 5) | (pkt[2] >> 2);
720 dev->y = (pkt[3] << 5) | (pkt[4] >> 2);
721 dev->touch = (pkt[0] & 0x40) ? 1 : 0;
722 return 1;
724 case 0x98:
725 /* touch data in MT emulation mode */
726 dev->x = (pkt[2] << 5) | (pkt[1] >> 2);
727 dev->y = (pkt[4] << 5) | (pkt[3] >> 2);
728 dev->touch = (pkt[0] & 0x40) ? 1 : 0;
729 return 1;
731 default:
732 return 0;
735 #endif
737 /*****************************************************************************
738 * General Touch Part
740 #ifdef CONFIG_TOUCHSCREEN_USB_GENERAL_TOUCH
741 static int general_touch_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
743 dev->x = (pkt[2] << 8) | pkt[1];
744 dev->y = (pkt[4] << 8) | pkt[3];
745 dev->press = pkt[5] & 0xff;
746 dev->touch = pkt[0] & 0x01;
748 return 1;
750 #endif
752 /*****************************************************************************
753 * GoTop Part
755 #ifdef CONFIG_TOUCHSCREEN_USB_GOTOP
756 static int gotop_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
758 dev->x = ((pkt[1] & 0x38) << 4) | pkt[2];
759 dev->y = ((pkt[1] & 0x07) << 7) | pkt[3];
760 dev->touch = pkt[0] & 0x01;
762 return 1;
764 #endif
766 /*****************************************************************************
767 * JASTEC Part
769 #ifdef CONFIG_TOUCHSCREEN_USB_JASTEC
770 static int jastec_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
772 dev->x = ((pkt[0] & 0x3f) << 6) | (pkt[2] & 0x3f);
773 dev->y = ((pkt[1] & 0x3f) << 6) | (pkt[3] & 0x3f);
774 dev->touch = (pkt[0] & 0x40) >> 6;
776 return 1;
778 #endif
780 /*****************************************************************************
781 * Zytronic Part
783 #ifdef CONFIG_TOUCHSCREEN_USB_ZYTRONIC
784 static int zytronic_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
786 struct usb_interface *intf = dev->interface;
788 switch (pkt[0]) {
789 case 0x3A: /* command response */
790 dev_dbg(&intf->dev, "%s: Command response %d\n", __func__, pkt[1]);
791 break;
793 case 0xC0: /* down */
794 dev->x = (pkt[1] & 0x7f) | ((pkt[2] & 0x07) << 7);
795 dev->y = (pkt[3] & 0x7f) | ((pkt[4] & 0x07) << 7);
796 dev->touch = 1;
797 dev_dbg(&intf->dev, "%s: down %d,%d\n", __func__, dev->x, dev->y);
798 return 1;
800 case 0x80: /* up */
801 dev->x = (pkt[1] & 0x7f) | ((pkt[2] & 0x07) << 7);
802 dev->y = (pkt[3] & 0x7f) | ((pkt[4] & 0x07) << 7);
803 dev->touch = 0;
804 dev_dbg(&intf->dev, "%s: up %d,%d\n", __func__, dev->x, dev->y);
805 return 1;
807 default:
808 dev_dbg(&intf->dev, "%s: Unknown return %d\n", __func__, pkt[0]);
809 break;
812 return 0;
814 #endif
816 /*****************************************************************************
817 * NEXIO Part
819 #ifdef CONFIG_TOUCHSCREEN_USB_NEXIO
821 #define NEXIO_TIMEOUT 5000
822 #define NEXIO_BUFSIZE 1024
823 #define NEXIO_THRESHOLD 50
825 struct nexio_priv {
826 struct urb *ack;
827 unsigned char *ack_buf;
830 struct nexio_touch_packet {
831 u8 flags; /* 0xe1 = touch, 0xe1 = release */
832 __be16 data_len; /* total bytes of touch data */
833 __be16 x_len; /* bytes for X axis */
834 __be16 y_len; /* bytes for Y axis */
835 u8 data[];
836 } __attribute__ ((packed));
838 static unsigned char nexio_ack_pkt[2] = { 0xaa, 0x02 };
839 static unsigned char nexio_init_pkt[4] = { 0x82, 0x04, 0x0a, 0x0f };
841 static void nexio_ack_complete(struct urb *urb)
845 static int nexio_alloc(struct usbtouch_usb *usbtouch)
847 struct nexio_priv *priv;
848 int ret = -ENOMEM;
850 usbtouch->priv = kmalloc(sizeof(struct nexio_priv), GFP_KERNEL);
851 if (!usbtouch->priv)
852 goto out_buf;
854 priv = usbtouch->priv;
856 priv->ack_buf = kmemdup(nexio_ack_pkt, sizeof(nexio_ack_pkt),
857 GFP_KERNEL);
858 if (!priv->ack_buf)
859 goto err_priv;
861 priv->ack = usb_alloc_urb(0, GFP_KERNEL);
862 if (!priv->ack) {
863 dev_dbg(&usbtouch->interface->dev,
864 "%s - usb_alloc_urb failed: usbtouch->ack\n", __func__);
865 goto err_ack_buf;
868 return 0;
870 err_ack_buf:
871 kfree(priv->ack_buf);
872 err_priv:
873 kfree(priv);
874 out_buf:
875 return ret;
878 static int nexio_init(struct usbtouch_usb *usbtouch)
880 struct usb_device *dev = interface_to_usbdev(usbtouch->interface);
881 struct usb_host_interface *interface = usbtouch->interface->cur_altsetting;
882 struct nexio_priv *priv = usbtouch->priv;
883 int ret = -ENOMEM;
884 int actual_len, i;
885 unsigned char *buf;
886 char *firmware_ver = NULL, *device_name = NULL;
887 int input_ep = 0, output_ep = 0;
889 /* find first input and output endpoint */
890 for (i = 0; i < interface->desc.bNumEndpoints; i++) {
891 if (!input_ep &&
892 usb_endpoint_dir_in(&interface->endpoint[i].desc))
893 input_ep = interface->endpoint[i].desc.bEndpointAddress;
894 if (!output_ep &&
895 usb_endpoint_dir_out(&interface->endpoint[i].desc))
896 output_ep = interface->endpoint[i].desc.bEndpointAddress;
898 if (!input_ep || !output_ep)
899 return -ENXIO;
901 buf = kmalloc(NEXIO_BUFSIZE, GFP_NOIO);
902 if (!buf)
903 goto out_buf;
905 /* two empty reads */
906 for (i = 0; i < 2; i++) {
907 ret = usb_bulk_msg(dev, usb_rcvbulkpipe(dev, input_ep),
908 buf, NEXIO_BUFSIZE, &actual_len,
909 NEXIO_TIMEOUT);
910 if (ret < 0)
911 goto out_buf;
914 /* send init command */
915 memcpy(buf, nexio_init_pkt, sizeof(nexio_init_pkt));
916 ret = usb_bulk_msg(dev, usb_sndbulkpipe(dev, output_ep),
917 buf, sizeof(nexio_init_pkt), &actual_len,
918 NEXIO_TIMEOUT);
919 if (ret < 0)
920 goto out_buf;
922 /* read replies */
923 for (i = 0; i < 3; i++) {
924 memset(buf, 0, NEXIO_BUFSIZE);
925 ret = usb_bulk_msg(dev, usb_rcvbulkpipe(dev, input_ep),
926 buf, NEXIO_BUFSIZE, &actual_len,
927 NEXIO_TIMEOUT);
928 if (ret < 0 || actual_len < 1 || buf[1] != actual_len)
929 continue;
930 switch (buf[0]) {
931 case 0x83: /* firmware version */
932 if (!firmware_ver)
933 firmware_ver = kstrdup(&buf[2], GFP_NOIO);
934 break;
935 case 0x84: /* device name */
936 if (!device_name)
937 device_name = kstrdup(&buf[2], GFP_NOIO);
938 break;
942 printk(KERN_INFO "Nexio device: %s, firmware version: %s\n",
943 device_name, firmware_ver);
945 kfree(firmware_ver);
946 kfree(device_name);
948 usb_fill_bulk_urb(priv->ack, dev, usb_sndbulkpipe(dev, output_ep),
949 priv->ack_buf, sizeof(nexio_ack_pkt),
950 nexio_ack_complete, usbtouch);
951 ret = 0;
953 out_buf:
954 kfree(buf);
955 return ret;
958 static void nexio_exit(struct usbtouch_usb *usbtouch)
960 struct nexio_priv *priv = usbtouch->priv;
962 usb_kill_urb(priv->ack);
963 usb_free_urb(priv->ack);
964 kfree(priv->ack_buf);
965 kfree(priv);
968 static int nexio_read_data(struct usbtouch_usb *usbtouch, unsigned char *pkt)
970 struct nexio_touch_packet *packet = (void *) pkt;
971 struct nexio_priv *priv = usbtouch->priv;
972 unsigned int data_len = be16_to_cpu(packet->data_len);
973 unsigned int x_len = be16_to_cpu(packet->x_len);
974 unsigned int y_len = be16_to_cpu(packet->y_len);
975 int x, y, begin_x, begin_y, end_x, end_y, w, h, ret;
977 /* got touch data? */
978 if ((pkt[0] & 0xe0) != 0xe0)
979 return 0;
981 if (data_len > 0xff)
982 data_len -= 0x100;
983 if (x_len > 0xff)
984 x_len -= 0x80;
986 /* send ACK */
987 ret = usb_submit_urb(priv->ack, GFP_ATOMIC);
989 if (!usbtouch->type->max_xc) {
990 usbtouch->type->max_xc = 2 * x_len;
991 input_set_abs_params(usbtouch->input, ABS_X,
992 0, usbtouch->type->max_xc, 0, 0);
993 usbtouch->type->max_yc = 2 * y_len;
994 input_set_abs_params(usbtouch->input, ABS_Y,
995 0, usbtouch->type->max_yc, 0, 0);
998 * The device reports state of IR sensors on X and Y axes.
999 * Each byte represents "darkness" percentage (0-100) of one element.
1000 * 17" touchscreen reports only 64 x 52 bytes so the resolution is low.
1001 * This also means that there's a limited multi-touch capability but
1002 * it's disabled (and untested) here as there's no X driver for that.
1004 begin_x = end_x = begin_y = end_y = -1;
1005 for (x = 0; x < x_len; x++) {
1006 if (begin_x == -1 && packet->data[x] > NEXIO_THRESHOLD) {
1007 begin_x = x;
1008 continue;
1010 if (end_x == -1 && begin_x != -1 && packet->data[x] < NEXIO_THRESHOLD) {
1011 end_x = x - 1;
1012 for (y = x_len; y < data_len; y++) {
1013 if (begin_y == -1 && packet->data[y] > NEXIO_THRESHOLD) {
1014 begin_y = y - x_len;
1015 continue;
1017 if (end_y == -1 &&
1018 begin_y != -1 && packet->data[y] < NEXIO_THRESHOLD) {
1019 end_y = y - 1 - x_len;
1020 w = end_x - begin_x;
1021 h = end_y - begin_y;
1022 #if 0
1023 /* multi-touch */
1024 input_report_abs(usbtouch->input,
1025 ABS_MT_TOUCH_MAJOR, max(w,h));
1026 input_report_abs(usbtouch->input,
1027 ABS_MT_TOUCH_MINOR, min(x,h));
1028 input_report_abs(usbtouch->input,
1029 ABS_MT_POSITION_X, 2*begin_x+w);
1030 input_report_abs(usbtouch->input,
1031 ABS_MT_POSITION_Y, 2*begin_y+h);
1032 input_report_abs(usbtouch->input,
1033 ABS_MT_ORIENTATION, w > h);
1034 input_mt_sync(usbtouch->input);
1035 #endif
1036 /* single touch */
1037 usbtouch->x = 2 * begin_x + w;
1038 usbtouch->y = 2 * begin_y + h;
1039 usbtouch->touch = packet->flags & 0x01;
1040 begin_y = end_y = -1;
1041 return 1;
1044 begin_x = end_x = -1;
1048 return 0;
1050 #endif
1053 /*****************************************************************************
1054 * ELO part
1057 #ifdef CONFIG_TOUCHSCREEN_USB_ELO
1059 static int elo_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
1061 dev->x = (pkt[3] << 8) | pkt[2];
1062 dev->y = (pkt[5] << 8) | pkt[4];
1063 dev->touch = pkt[6] > 0;
1064 dev->press = pkt[6];
1066 return 1;
1068 #endif
1071 /*****************************************************************************
1072 * the different device descriptors
1074 #ifdef MULTI_PACKET
1075 static void usbtouch_process_multi(struct usbtouch_usb *usbtouch,
1076 unsigned char *pkt, int len);
1077 #endif
1079 static struct usbtouch_device_info usbtouch_dev_info[] = {
1080 #ifdef CONFIG_TOUCHSCREEN_USB_ELO
1081 [DEVTYPE_ELO] = {
1082 .min_xc = 0x0,
1083 .max_xc = 0x0fff,
1084 .min_yc = 0x0,
1085 .max_yc = 0x0fff,
1086 .max_press = 0xff,
1087 .rept_size = 8,
1088 .read_data = elo_read_data,
1090 #endif
1092 #ifdef CONFIG_TOUCHSCREEN_USB_EGALAX
1093 [DEVTYPE_EGALAX] = {
1094 .min_xc = 0x0,
1095 .max_xc = 0x07ff,
1096 .min_yc = 0x0,
1097 .max_yc = 0x07ff,
1098 .rept_size = 16,
1099 .process_pkt = usbtouch_process_multi,
1100 .get_pkt_len = egalax_get_pkt_len,
1101 .read_data = egalax_read_data,
1102 .init = egalax_init,
1104 #endif
1106 #ifdef CONFIG_TOUCHSCREEN_USB_PANJIT
1107 [DEVTYPE_PANJIT] = {
1108 .min_xc = 0x0,
1109 .max_xc = 0x0fff,
1110 .min_yc = 0x0,
1111 .max_yc = 0x0fff,
1112 .rept_size = 8,
1113 .read_data = panjit_read_data,
1115 #endif
1117 #ifdef CONFIG_TOUCHSCREEN_USB_3M
1118 [DEVTYPE_3M] = {
1119 .min_xc = 0x0,
1120 .max_xc = 0x4000,
1121 .min_yc = 0x0,
1122 .max_yc = 0x4000,
1123 .rept_size = 11,
1124 .read_data = mtouch_read_data,
1125 .init = mtouch_init,
1127 #endif
1129 #ifdef CONFIG_TOUCHSCREEN_USB_ITM
1130 [DEVTYPE_ITM] = {
1131 .min_xc = 0x0,
1132 .max_xc = 0x0fff,
1133 .min_yc = 0x0,
1134 .max_yc = 0x0fff,
1135 .max_press = 0xff,
1136 .rept_size = 8,
1137 .read_data = itm_read_data,
1139 #endif
1141 #ifdef CONFIG_TOUCHSCREEN_USB_ETURBO
1142 [DEVTYPE_ETURBO] = {
1143 .min_xc = 0x0,
1144 .max_xc = 0x07ff,
1145 .min_yc = 0x0,
1146 .max_yc = 0x07ff,
1147 .rept_size = 8,
1148 .process_pkt = usbtouch_process_multi,
1149 .get_pkt_len = eturbo_get_pkt_len,
1150 .read_data = eturbo_read_data,
1152 #endif
1154 #ifdef CONFIG_TOUCHSCREEN_USB_GUNZE
1155 [DEVTYPE_GUNZE] = {
1156 .min_xc = 0x0,
1157 .max_xc = 0x0fff,
1158 .min_yc = 0x0,
1159 .max_yc = 0x0fff,
1160 .rept_size = 4,
1161 .read_data = gunze_read_data,
1163 #endif
1165 #ifdef CONFIG_TOUCHSCREEN_USB_DMC_TSC10
1166 [DEVTYPE_DMC_TSC10] = {
1167 .min_xc = 0x0,
1168 .max_xc = 0x03ff,
1169 .min_yc = 0x0,
1170 .max_yc = 0x03ff,
1171 .rept_size = 5,
1172 .init = dmc_tsc10_init,
1173 .read_data = dmc_tsc10_read_data,
1175 #endif
1177 #ifdef CONFIG_TOUCHSCREEN_USB_IRTOUCH
1178 [DEVTYPE_IRTOUCH] = {
1179 .min_xc = 0x0,
1180 .max_xc = 0x0fff,
1181 .min_yc = 0x0,
1182 .max_yc = 0x0fff,
1183 .rept_size = 8,
1184 .read_data = irtouch_read_data,
1187 [DEVTYPE_IRTOUCH_HIRES] = {
1188 .min_xc = 0x0,
1189 .max_xc = 0x7fff,
1190 .min_yc = 0x0,
1191 .max_yc = 0x7fff,
1192 .rept_size = 8,
1193 .read_data = irtouch_read_data,
1195 #endif
1197 #ifdef CONFIG_TOUCHSCREEN_USB_IDEALTEK
1198 [DEVTYPE_IDEALTEK] = {
1199 .min_xc = 0x0,
1200 .max_xc = 0x0fff,
1201 .min_yc = 0x0,
1202 .max_yc = 0x0fff,
1203 .rept_size = 8,
1204 .process_pkt = usbtouch_process_multi,
1205 .get_pkt_len = idealtek_get_pkt_len,
1206 .read_data = idealtek_read_data,
1208 #endif
1210 #ifdef CONFIG_TOUCHSCREEN_USB_GENERAL_TOUCH
1211 [DEVTYPE_GENERAL_TOUCH] = {
1212 .min_xc = 0x0,
1213 .max_xc = 0x7fff,
1214 .min_yc = 0x0,
1215 .max_yc = 0x7fff,
1216 .rept_size = 7,
1217 .read_data = general_touch_read_data,
1219 #endif
1221 #ifdef CONFIG_TOUCHSCREEN_USB_GOTOP
1222 [DEVTYPE_GOTOP] = {
1223 .min_xc = 0x0,
1224 .max_xc = 0x03ff,
1225 .min_yc = 0x0,
1226 .max_yc = 0x03ff,
1227 .rept_size = 4,
1228 .read_data = gotop_read_data,
1230 #endif
1232 #ifdef CONFIG_TOUCHSCREEN_USB_JASTEC
1233 [DEVTYPE_JASTEC] = {
1234 .min_xc = 0x0,
1235 .max_xc = 0x0fff,
1236 .min_yc = 0x0,
1237 .max_yc = 0x0fff,
1238 .rept_size = 4,
1239 .read_data = jastec_read_data,
1241 #endif
1243 #ifdef CONFIG_TOUCHSCREEN_USB_E2I
1244 [DEVTYPE_E2I] = {
1245 .min_xc = 0x0,
1246 .max_xc = 0x7fff,
1247 .min_yc = 0x0,
1248 .max_yc = 0x7fff,
1249 .rept_size = 6,
1250 .init = e2i_init,
1251 .read_data = e2i_read_data,
1253 #endif
1255 #ifdef CONFIG_TOUCHSCREEN_USB_ZYTRONIC
1256 [DEVTYPE_ZYTRONIC] = {
1257 .min_xc = 0x0,
1258 .max_xc = 0x03ff,
1259 .min_yc = 0x0,
1260 .max_yc = 0x03ff,
1261 .rept_size = 5,
1262 .read_data = zytronic_read_data,
1263 .irq_always = true,
1265 #endif
1267 #ifdef CONFIG_TOUCHSCREEN_USB_ETT_TC45USB
1268 [DEVTYPE_TC45USB] = {
1269 .min_xc = 0x0,
1270 .max_xc = 0x0fff,
1271 .min_yc = 0x0,
1272 .max_yc = 0x0fff,
1273 .rept_size = 5,
1274 .read_data = tc45usb_read_data,
1276 #endif
1278 #ifdef CONFIG_TOUCHSCREEN_USB_NEXIO
1279 [DEVTYPE_NEXIO] = {
1280 .rept_size = 1024,
1281 .irq_always = true,
1282 .read_data = nexio_read_data,
1283 .alloc = nexio_alloc,
1284 .init = nexio_init,
1285 .exit = nexio_exit,
1287 #endif
1288 #ifdef CONFIG_TOUCHSCREEN_USB_EASYTOUCH
1289 [DEVTYPE_ETOUCH] = {
1290 .min_xc = 0x0,
1291 .max_xc = 0x07ff,
1292 .min_yc = 0x0,
1293 .max_yc = 0x07ff,
1294 .rept_size = 16,
1295 .process_pkt = usbtouch_process_multi,
1296 .get_pkt_len = etouch_get_pkt_len,
1297 .read_data = etouch_read_data,
1299 #endif
1303 /*****************************************************************************
1304 * Generic Part
1306 static void usbtouch_process_pkt(struct usbtouch_usb *usbtouch,
1307 unsigned char *pkt, int len)
1309 struct usbtouch_device_info *type = usbtouch->type;
1311 if (!type->read_data(usbtouch, pkt))
1312 return;
1314 input_report_key(usbtouch->input, BTN_TOUCH, usbtouch->touch);
1316 if (swap_xy) {
1317 input_report_abs(usbtouch->input, ABS_X, usbtouch->y);
1318 input_report_abs(usbtouch->input, ABS_Y, usbtouch->x);
1319 } else {
1320 input_report_abs(usbtouch->input, ABS_X, usbtouch->x);
1321 input_report_abs(usbtouch->input, ABS_Y, usbtouch->y);
1323 if (type->max_press)
1324 input_report_abs(usbtouch->input, ABS_PRESSURE, usbtouch->press);
1325 input_sync(usbtouch->input);
1329 #ifdef MULTI_PACKET
1330 static void usbtouch_process_multi(struct usbtouch_usb *usbtouch,
1331 unsigned char *pkt, int len)
1333 unsigned char *buffer;
1334 int pkt_len, pos, buf_len, tmp;
1336 /* process buffer */
1337 if (unlikely(usbtouch->buf_len)) {
1338 /* try to get size */
1339 pkt_len = usbtouch->type->get_pkt_len(
1340 usbtouch->buffer, usbtouch->buf_len);
1342 /* drop? */
1343 if (unlikely(!pkt_len))
1344 goto out_flush_buf;
1346 /* need to append -pkt_len bytes before able to get size */
1347 if (unlikely(pkt_len < 0)) {
1348 int append = -pkt_len;
1349 if (unlikely(append > len))
1350 append = len;
1351 if (usbtouch->buf_len + append >= usbtouch->type->rept_size)
1352 goto out_flush_buf;
1353 memcpy(usbtouch->buffer + usbtouch->buf_len, pkt, append);
1354 usbtouch->buf_len += append;
1356 pkt_len = usbtouch->type->get_pkt_len(
1357 usbtouch->buffer, usbtouch->buf_len);
1358 if (pkt_len < 0)
1359 return;
1362 /* append */
1363 tmp = pkt_len - usbtouch->buf_len;
1364 if (usbtouch->buf_len + tmp >= usbtouch->type->rept_size)
1365 goto out_flush_buf;
1366 memcpy(usbtouch->buffer + usbtouch->buf_len, pkt, tmp);
1367 usbtouch_process_pkt(usbtouch, usbtouch->buffer, pkt_len);
1369 buffer = pkt + tmp;
1370 buf_len = len - tmp;
1371 } else {
1372 buffer = pkt;
1373 buf_len = len;
1376 /* loop over the received packet, process */
1377 pos = 0;
1378 while (pos < buf_len) {
1379 /* get packet len */
1380 pkt_len = usbtouch->type->get_pkt_len(buffer + pos,
1381 buf_len - pos);
1383 /* unknown packet: skip one byte */
1384 if (unlikely(!pkt_len)) {
1385 pos++;
1386 continue;
1389 /* full packet: process */
1390 if (likely((pkt_len > 0) && (pkt_len <= buf_len - pos))) {
1391 usbtouch_process_pkt(usbtouch, buffer + pos, pkt_len);
1392 } else {
1393 /* incomplete packet: save in buffer */
1394 memcpy(usbtouch->buffer, buffer + pos, buf_len - pos);
1395 usbtouch->buf_len = buf_len - pos;
1396 return;
1398 pos += pkt_len;
1401 out_flush_buf:
1402 usbtouch->buf_len = 0;
1403 return;
1405 #endif
1408 static void usbtouch_irq(struct urb *urb)
1410 struct usbtouch_usb *usbtouch = urb->context;
1411 struct device *dev = &usbtouch->interface->dev;
1412 int retval;
1414 switch (urb->status) {
1415 case 0:
1416 /* success */
1417 break;
1418 case -ETIME:
1419 /* this urb is timing out */
1420 dev_dbg(dev,
1421 "%s - urb timed out - was the device unplugged?\n",
1422 __func__);
1423 return;
1424 case -ECONNRESET:
1425 case -ENOENT:
1426 case -ESHUTDOWN:
1427 case -EPIPE:
1428 /* this urb is terminated, clean up */
1429 dev_dbg(dev, "%s - urb shutting down with status: %d\n",
1430 __func__, urb->status);
1431 return;
1432 default:
1433 dev_dbg(dev, "%s - nonzero urb status received: %d\n",
1434 __func__, urb->status);
1435 goto exit;
1438 usbtouch->type->process_pkt(usbtouch, usbtouch->data, urb->actual_length);
1440 exit:
1441 usb_mark_last_busy(interface_to_usbdev(usbtouch->interface));
1442 retval = usb_submit_urb(urb, GFP_ATOMIC);
1443 if (retval)
1444 dev_err(dev, "%s - usb_submit_urb failed with result: %d\n",
1445 __func__, retval);
1448 static int usbtouch_open(struct input_dev *input)
1450 struct usbtouch_usb *usbtouch = input_get_drvdata(input);
1451 int r;
1453 usbtouch->irq->dev = interface_to_usbdev(usbtouch->interface);
1455 r = usb_autopm_get_interface(usbtouch->interface) ? -EIO : 0;
1456 if (r < 0)
1457 goto out;
1459 if (!usbtouch->type->irq_always) {
1460 if (usb_submit_urb(usbtouch->irq, GFP_KERNEL)) {
1461 r = -EIO;
1462 goto out_put;
1466 usbtouch->interface->needs_remote_wakeup = 1;
1467 out_put:
1468 usb_autopm_put_interface(usbtouch->interface);
1469 out:
1470 return r;
1473 static void usbtouch_close(struct input_dev *input)
1475 struct usbtouch_usb *usbtouch = input_get_drvdata(input);
1476 int r;
1478 if (!usbtouch->type->irq_always)
1479 usb_kill_urb(usbtouch->irq);
1480 r = usb_autopm_get_interface(usbtouch->interface);
1481 usbtouch->interface->needs_remote_wakeup = 0;
1482 if (!r)
1483 usb_autopm_put_interface(usbtouch->interface);
1486 static int usbtouch_suspend
1487 (struct usb_interface *intf, pm_message_t message)
1489 struct usbtouch_usb *usbtouch = usb_get_intfdata(intf);
1491 usb_kill_urb(usbtouch->irq);
1493 return 0;
1496 static int usbtouch_resume(struct usb_interface *intf)
1498 struct usbtouch_usb *usbtouch = usb_get_intfdata(intf);
1499 struct input_dev *input = usbtouch->input;
1500 int result = 0;
1502 mutex_lock(&input->mutex);
1503 if (input->users || usbtouch->type->irq_always)
1504 result = usb_submit_urb(usbtouch->irq, GFP_NOIO);
1505 mutex_unlock(&input->mutex);
1507 return result;
1510 static int usbtouch_reset_resume(struct usb_interface *intf)
1512 struct usbtouch_usb *usbtouch = usb_get_intfdata(intf);
1513 struct input_dev *input = usbtouch->input;
1514 int err = 0;
1516 /* reinit the device */
1517 if (usbtouch->type->init) {
1518 err = usbtouch->type->init(usbtouch);
1519 if (err) {
1520 dev_dbg(&intf->dev,
1521 "%s - type->init() failed, err: %d\n",
1522 __func__, err);
1523 return err;
1527 /* restart IO if needed */
1528 mutex_lock(&input->mutex);
1529 if (input->users)
1530 err = usb_submit_urb(usbtouch->irq, GFP_NOIO);
1531 mutex_unlock(&input->mutex);
1533 return err;
1536 static void usbtouch_free_buffers(struct usb_device *udev,
1537 struct usbtouch_usb *usbtouch)
1539 usb_free_coherent(udev, usbtouch->data_size,
1540 usbtouch->data, usbtouch->data_dma);
1541 kfree(usbtouch->buffer);
1544 static struct usb_endpoint_descriptor *
1545 usbtouch_get_input_endpoint(struct usb_host_interface *interface)
1547 int i;
1549 for (i = 0; i < interface->desc.bNumEndpoints; i++)
1550 if (usb_endpoint_dir_in(&interface->endpoint[i].desc))
1551 return &interface->endpoint[i].desc;
1553 return NULL;
1556 static int usbtouch_probe(struct usb_interface *intf,
1557 const struct usb_device_id *id)
1559 struct usbtouch_usb *usbtouch;
1560 struct input_dev *input_dev;
1561 struct usb_endpoint_descriptor *endpoint;
1562 struct usb_device *udev = interface_to_usbdev(intf);
1563 struct usbtouch_device_info *type;
1564 int err = -ENOMEM;
1566 /* some devices are ignored */
1567 if (id->driver_info == DEVTYPE_IGNORE)
1568 return -ENODEV;
1570 endpoint = usbtouch_get_input_endpoint(intf->cur_altsetting);
1571 if (!endpoint)
1572 return -ENXIO;
1574 usbtouch = kzalloc(sizeof(struct usbtouch_usb), GFP_KERNEL);
1575 input_dev = input_allocate_device();
1576 if (!usbtouch || !input_dev)
1577 goto out_free;
1579 type = &usbtouch_dev_info[id->driver_info];
1580 usbtouch->type = type;
1581 if (!type->process_pkt)
1582 type->process_pkt = usbtouch_process_pkt;
1584 usbtouch->data_size = type->rept_size;
1585 if (type->get_pkt_len) {
1587 * When dealing with variable-length packets we should
1588 * not request more than wMaxPacketSize bytes at once
1589 * as we do not know if there is more data coming or
1590 * we filled exactly wMaxPacketSize bytes and there is
1591 * nothing else.
1593 usbtouch->data_size = min(usbtouch->data_size,
1594 usb_endpoint_maxp(endpoint));
1597 usbtouch->data = usb_alloc_coherent(udev, usbtouch->data_size,
1598 GFP_KERNEL, &usbtouch->data_dma);
1599 if (!usbtouch->data)
1600 goto out_free;
1602 if (type->get_pkt_len) {
1603 usbtouch->buffer = kmalloc(type->rept_size, GFP_KERNEL);
1604 if (!usbtouch->buffer)
1605 goto out_free_buffers;
1608 usbtouch->irq = usb_alloc_urb(0, GFP_KERNEL);
1609 if (!usbtouch->irq) {
1610 dev_dbg(&intf->dev,
1611 "%s - usb_alloc_urb failed: usbtouch->irq\n", __func__);
1612 goto out_free_buffers;
1615 usbtouch->interface = intf;
1616 usbtouch->input = input_dev;
1618 if (udev->manufacturer)
1619 strlcpy(usbtouch->name, udev->manufacturer, sizeof(usbtouch->name));
1621 if (udev->product) {
1622 if (udev->manufacturer)
1623 strlcat(usbtouch->name, " ", sizeof(usbtouch->name));
1624 strlcat(usbtouch->name, udev->product, sizeof(usbtouch->name));
1627 if (!strlen(usbtouch->name))
1628 snprintf(usbtouch->name, sizeof(usbtouch->name),
1629 "USB Touchscreen %04x:%04x",
1630 le16_to_cpu(udev->descriptor.idVendor),
1631 le16_to_cpu(udev->descriptor.idProduct));
1633 usb_make_path(udev, usbtouch->phys, sizeof(usbtouch->phys));
1634 strlcat(usbtouch->phys, "/input0", sizeof(usbtouch->phys));
1636 input_dev->name = usbtouch->name;
1637 input_dev->phys = usbtouch->phys;
1638 usb_to_input_id(udev, &input_dev->id);
1639 input_dev->dev.parent = &intf->dev;
1641 input_set_drvdata(input_dev, usbtouch);
1643 input_dev->open = usbtouch_open;
1644 input_dev->close = usbtouch_close;
1646 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
1647 input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
1648 input_set_abs_params(input_dev, ABS_X, type->min_xc, type->max_xc, 0, 0);
1649 input_set_abs_params(input_dev, ABS_Y, type->min_yc, type->max_yc, 0, 0);
1650 if (type->max_press)
1651 input_set_abs_params(input_dev, ABS_PRESSURE, type->min_press,
1652 type->max_press, 0, 0);
1654 if (usb_endpoint_type(endpoint) == USB_ENDPOINT_XFER_INT)
1655 usb_fill_int_urb(usbtouch->irq, udev,
1656 usb_rcvintpipe(udev, endpoint->bEndpointAddress),
1657 usbtouch->data, usbtouch->data_size,
1658 usbtouch_irq, usbtouch, endpoint->bInterval);
1659 else
1660 usb_fill_bulk_urb(usbtouch->irq, udev,
1661 usb_rcvbulkpipe(udev, endpoint->bEndpointAddress),
1662 usbtouch->data, usbtouch->data_size,
1663 usbtouch_irq, usbtouch);
1665 usbtouch->irq->dev = udev;
1666 usbtouch->irq->transfer_dma = usbtouch->data_dma;
1667 usbtouch->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1669 /* device specific allocations */
1670 if (type->alloc) {
1671 err = type->alloc(usbtouch);
1672 if (err) {
1673 dev_dbg(&intf->dev,
1674 "%s - type->alloc() failed, err: %d\n",
1675 __func__, err);
1676 goto out_free_urb;
1680 /* device specific initialisation*/
1681 if (type->init) {
1682 err = type->init(usbtouch);
1683 if (err) {
1684 dev_dbg(&intf->dev,
1685 "%s - type->init() failed, err: %d\n",
1686 __func__, err);
1687 goto out_do_exit;
1691 err = input_register_device(usbtouch->input);
1692 if (err) {
1693 dev_dbg(&intf->dev,
1694 "%s - input_register_device failed, err: %d\n",
1695 __func__, err);
1696 goto out_do_exit;
1699 usb_set_intfdata(intf, usbtouch);
1701 if (usbtouch->type->irq_always) {
1702 /* this can't fail */
1703 usb_autopm_get_interface(intf);
1704 err = usb_submit_urb(usbtouch->irq, GFP_KERNEL);
1705 if (err) {
1706 usb_autopm_put_interface(intf);
1707 dev_err(&intf->dev,
1708 "%s - usb_submit_urb failed with result: %d\n",
1709 __func__, err);
1710 goto out_unregister_input;
1714 return 0;
1716 out_unregister_input:
1717 input_unregister_device(input_dev);
1718 input_dev = NULL;
1719 out_do_exit:
1720 if (type->exit)
1721 type->exit(usbtouch);
1722 out_free_urb:
1723 usb_free_urb(usbtouch->irq);
1724 out_free_buffers:
1725 usbtouch_free_buffers(udev, usbtouch);
1726 out_free:
1727 input_free_device(input_dev);
1728 kfree(usbtouch);
1729 return err;
1732 static void usbtouch_disconnect(struct usb_interface *intf)
1734 struct usbtouch_usb *usbtouch = usb_get_intfdata(intf);
1736 if (!usbtouch)
1737 return;
1739 dev_dbg(&intf->dev,
1740 "%s - usbtouch is initialized, cleaning up\n", __func__);
1742 usb_set_intfdata(intf, NULL);
1743 /* this will stop IO via close */
1744 input_unregister_device(usbtouch->input);
1745 usb_free_urb(usbtouch->irq);
1746 if (usbtouch->type->exit)
1747 usbtouch->type->exit(usbtouch);
1748 usbtouch_free_buffers(interface_to_usbdev(intf), usbtouch);
1749 kfree(usbtouch);
1752 MODULE_DEVICE_TABLE(usb, usbtouch_devices);
1754 static struct usb_driver usbtouch_driver = {
1755 .name = "usbtouchscreen",
1756 .probe = usbtouch_probe,
1757 .disconnect = usbtouch_disconnect,
1758 .suspend = usbtouch_suspend,
1759 .resume = usbtouch_resume,
1760 .reset_resume = usbtouch_reset_resume,
1761 .id_table = usbtouch_devices,
1762 .supports_autosuspend = 1,
1765 module_usb_driver(usbtouch_driver);
1767 MODULE_AUTHOR(DRIVER_AUTHOR);
1768 MODULE_DESCRIPTION(DRIVER_DESC);
1769 MODULE_LICENSE("GPL");
1771 MODULE_ALIAS("touchkitusb");
1772 MODULE_ALIAS("itmtouch");
1773 MODULE_ALIAS("mtouchusb");