Merge commit 'v3.3-rc6' into next
[linux-flexiantxendom0-3.2.10.git] / drivers / input / touchscreen / usbtouchscreen.c
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
21  *
22  * Copyright (C) 2004-2007 by Daniel Ritz <daniel.ritz@gmx.ch>
23  * Copyright (C) by Todd E. Johnson (mtouchusb.c)
24  *
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.
29  *
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.
34  *
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.
38  *
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
45  *
46  *****************************************************************************/
47
48 //#define DEBUG
49
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>
58
59
60 #define DRIVER_VERSION          "v0.6"
61 #define DRIVER_AUTHOR           "Daniel Ritz <daniel.ritz@gmx.ch>"
62 #define DRIVER_DESC             "USB Touchscreen Driver"
63
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.");
67
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");
71
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;
79
80         /*
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.
86          */
87         bool irq_always;
88
89         void (*process_pkt) (struct usbtouch_usb *usbtouch, unsigned char *pkt, int len);
90
91         /*
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
96          */
97         int  (*get_pkt_len) (unsigned char *pkt, int len);
98
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);
103 };
104
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;
118
119         int x, y;
120         int touch, press;
121 };
122
123
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,
145 };
146
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
155
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},
161
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
171
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
178
179 #ifdef CONFIG_TOUCHSCREEN_USB_3M
180         {USB_DEVICE(0x0596, 0x0001), .driver_info = DEVTYPE_3M},
181 #endif
182
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
187
188 #ifdef CONFIG_TOUCHSCREEN_USB_ETURBO
189         {USB_DEVICE(0x1234, 0x5678), .driver_info = DEVTYPE_ETURBO},
190 #endif
191
192 #ifdef CONFIG_TOUCHSCREEN_USB_GUNZE
193         {USB_DEVICE(0x0637, 0x0001), .driver_info = DEVTYPE_GUNZE},
194 #endif
195
196 #ifdef CONFIG_TOUCHSCREEN_USB_DMC_TSC10
197         {USB_DEVICE(0x0afa, 0x03e8), .driver_info = DEVTYPE_DMC_TSC10},
198 #endif
199
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
204
205 #ifdef CONFIG_TOUCHSCREEN_USB_IDEALTEK
206         {USB_DEVICE(0x1391, 0x1000), .driver_info = DEVTYPE_IDEALTEK},
207 #endif
208
209 #ifdef CONFIG_TOUCHSCREEN_USB_GENERAL_TOUCH
210         {USB_DEVICE(0x0dfc, 0x0001), .driver_info = DEVTYPE_GENERAL_TOUCH},
211 #endif
212
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
218
219 #ifdef CONFIG_TOUCHSCREEN_USB_JASTEC
220         {USB_DEVICE(0x0f92, 0x0001), .driver_info = DEVTYPE_JASTEC},
221 #endif
222
223 #ifdef CONFIG_TOUCHSCREEN_USB_E2I
224         {USB_DEVICE(0x1ac7, 0x0001), .driver_info = DEVTYPE_E2I},
225 #endif
226
227 #ifdef CONFIG_TOUCHSCREEN_USB_ZYTRONIC
228         {USB_DEVICE(0x14c8, 0x0003), .driver_info = DEVTYPE_ZYTRONIC},
229 #endif
230
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
237
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
245
246 #ifdef CONFIG_TOUCHSCREEN_USB_ELO
247         {USB_DEVICE(0x04e7, 0x0020), .driver_info = DEVTYPE_ELO},
248 #endif
249
250 #ifdef CONFIG_TOUCHSCREEN_USB_EASYTOUCH
251         {USB_DEVICE(0x7374, 0x0001), .driver_info = DEVTYPE_ETOUCH},
252 #endif
253
254         {}
255 };
256
257
258 /*****************************************************************************
259  * e2i Part
260  */
261
262 #ifdef CONFIG_TOUCHSCREEN_USB_E2I
263 static int e2i_init(struct usbtouch_usb *usbtouch)
264 {
265         int ret;
266         struct usb_device *udev = interface_to_usbdev(usbtouch->interface);
267
268         ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
269                               0x01, 0x02, 0x0000, 0x0081,
270                               NULL, 0, USB_CTRL_SET_TIMEOUT);
271
272         dbg("%s - usb_control_msg - E2I_RESET - bytes|err: %d",
273             __func__, ret);
274         return ret;
275 }
276
277 static int e2i_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
278 {
279         int tmp = (pkt[0] << 8) | pkt[1];
280         dev->x  = (pkt[2] << 8) | pkt[3];
281         dev->y  = (pkt[4] << 8) | pkt[5];
282
283         tmp = tmp - 0xA000;
284         dev->touch = (tmp > 0);
285         dev->press = (tmp > 0 ? tmp : 0);
286
287         return 1;
288 }
289 #endif
290
291
292 /*****************************************************************************
293  * eGalax part
294  */
295
296 #ifdef CONFIG_TOUCHSCREEN_USB_EGALAX
297
298 #ifndef MULTI_PACKET
299 #define MULTI_PACKET
300 #endif
301
302 #define EGALAX_PKT_TYPE_MASK            0xFE
303 #define EGALAX_PKT_TYPE_REPT            0x80
304 #define EGALAX_PKT_TYPE_DIAG            0x0A
305
306 static int egalax_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
307 {
308         if ((pkt[0] & EGALAX_PKT_TYPE_MASK) != EGALAX_PKT_TYPE_REPT)
309                 return 0;
310
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;
314
315         return 1;
316 }
317
318 static int egalax_get_pkt_len(unsigned char *buf, int len)
319 {
320         switch (buf[0] & EGALAX_PKT_TYPE_MASK) {
321         case EGALAX_PKT_TYPE_REPT:
322                 return 5;
323
324         case EGALAX_PKT_TYPE_DIAG:
325                 if (len < 2)
326                         return -1;
327
328                 return buf[1] + 2;
329         }
330
331         return 0;
332 }
333 #endif
334
335 /*****************************************************************************
336  * EasyTouch part
337  */
338
339 #ifdef CONFIG_TOUCHSCREEN_USB_EASYTOUCH
340
341 #ifndef MULTI_PACKET
342 #define MULTI_PACKET
343 #endif
344
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
349
350 static int etouch_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
351 {
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;
355
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;
359
360         return 1;
361 }
362
363 static int etouch_get_pkt_len(unsigned char *buf, int len)
364 {
365         switch (buf[0] & ETOUCH_PKT_TYPE_MASK) {
366         case ETOUCH_PKT_TYPE_REPT:
367         case ETOUCH_PKT_TYPE_REPT2:
368                 return 5;
369
370         case ETOUCH_PKT_TYPE_DIAG:
371                 if (len < 2)
372                         return -1;
373
374                 return buf[1] + 2;
375         }
376
377         return 0;
378 }
379 #endif
380
381 /*****************************************************************************
382  * PanJit Part
383  */
384 #ifdef CONFIG_TOUCHSCREEN_USB_PANJIT
385 static int panjit_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
386 {
387         dev->x = ((pkt[2] & 0x0F) << 8) | pkt[1];
388         dev->y = ((pkt[4] & 0x0F) << 8) | pkt[3];
389         dev->touch = pkt[0] & 0x01;
390
391         return 1;
392 }
393 #endif
394
395
396 /*****************************************************************************
397  * 3M/Microtouch Part
398  */
399 #ifdef CONFIG_TOUCHSCREEN_USB_3M
400
401 #define MTOUCHUSB_ASYNC_REPORT          1
402 #define MTOUCHUSB_RESET                 7
403 #define MTOUCHUSB_REQ_CTRLLR_ID         10
404
405 static int mtouch_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
406 {
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];
413         }
414         dev->touch = (pkt[2] & 0x40) ? 1 : 0;
415
416         return 1;
417 }
418
419 static int mtouch_init(struct usbtouch_usb *usbtouch)
420 {
421         int ret, i;
422         struct usb_device *udev = interface_to_usbdev(usbtouch->interface);
423
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);
433
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;
445         }
446
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);
451         }
452
453         return 0;
454 }
455 #endif
456
457
458 /*****************************************************************************
459  * ITM Part
460  */
461 #ifdef CONFIG_TOUCHSCREEN_USB_ITM
462 static int itm_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
463 {
464         int touch;
465         /*
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.
470          */
471         dev->press = ((pkt[2] & 0x01) << 7) | (pkt[5] & 0x7F);
472
473         touch = ~pkt[7] & 0x20;
474         if (!touch) {
475                 if (dev->touch) {
476                         dev->touch = 0;
477                         return 1;
478                 }
479
480                 return 0;
481         }
482
483         dev->x = ((pkt[0] & 0x1F) << 7) | (pkt[3] & 0x7F);
484         dev->y = ((pkt[1] & 0x1F) << 7) | (pkt[4] & 0x7F);
485         dev->touch = touch;
486
487         return 1;
488 }
489 #endif
490
491
492 /*****************************************************************************
493  * eTurboTouch part
494  */
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)
500 {
501         unsigned int shift;
502
503         /* packets should start with sync */
504         if (!(pkt[0] & 0x80))
505                 return 0;
506
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;
511
512         return 1;
513 }
514
515 static int eturbo_get_pkt_len(unsigned char *buf, int len)
516 {
517         if (buf[0] & 0x80)
518                 return 5;
519         if (buf[0] == 0x01)
520                 return 3;
521         return 0;
522 }
523 #endif
524
525
526 /*****************************************************************************
527  * Gunze part
528  */
529 #ifdef CONFIG_TOUCHSCREEN_USB_GUNZE
530 static int gunze_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
531 {
532         if (!(pkt[0] & 0x80) || ((pkt[1] | pkt[2] | pkt[3]) & 0x80))
533                 return 0;
534
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;
538
539         return 1;
540 }
541 #endif
542
543 /*****************************************************************************
544  * DMC TSC-10/25 Part
545  *
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
549  */
550 #ifdef CONFIG_TOUCHSCREEN_USB_DMC_TSC10
551
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
560
561 /* commands */
562 #define TSC10_CMD_RESET         0x55
563 #define TSC10_CMD_RATE          0x05
564 #define TSC10_CMD_DATA1         0x01
565
566 static int dmc_tsc10_init(struct usbtouch_usb *usbtouch)
567 {
568         struct usb_device *dev = interface_to_usbdev(usbtouch->interface);
569         int ret = -ENOMEM;
570         unsigned char *buf;
571
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;
586         }
587
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;
599         }
600
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;
610 }
611
612
613 static int dmc_tsc10_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
614 {
615         dev->x = ((pkt[2] & 0x03) << 8) | pkt[1];
616         dev->y = ((pkt[4] & 0x03) << 8) | pkt[3];
617         dev->touch = pkt[0] & 0x01;
618
619         return 1;
620 }
621 #endif
622
623
624 /*****************************************************************************
625  * IRTOUCH Part
626  */
627 #ifdef CONFIG_TOUCHSCREEN_USB_IRTOUCH
628 static int irtouch_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
629 {
630         dev->x = (pkt[3] << 8) | pkt[2];
631         dev->y = (pkt[5] << 8) | pkt[4];
632         dev->touch = (pkt[1] & 0x03) ? 1 : 0;
633
634         return 1;
635 }
636 #endif
637
638 /*****************************************************************************
639  * ET&T TC5UH/TC4UM part
640  */
641 #ifdef CONFIG_TOUCHSCREEN_USB_ETT_TC45USB
642 static int tc45usb_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
643 {
644         dev->x = ((pkt[2] & 0x0F) << 8) | pkt[1];
645         dev->y = ((pkt[4] & 0x0F) << 8) | pkt[3];
646         dev->touch = pkt[0] & 0x01;
647
648         return 1;
649 }
650 #endif
651
652 /*****************************************************************************
653  * IdealTEK URTC1000 Part
654  */
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)
660 {
661         if (buf[0] & 0x80)
662                 return 5;
663         if (buf[0] == 0x01)
664                 return len;
665         return 0;
666 }
667
668 static int idealtek_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
669 {
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;
677
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;
684
685         default:
686                 return 0;
687         }
688 }
689 #endif
690
691 /*****************************************************************************
692  * General Touch Part
693  */
694 #ifdef CONFIG_TOUCHSCREEN_USB_GENERAL_TOUCH
695 static int general_touch_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
696 {
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;
701
702         return 1;
703 }
704 #endif
705
706 /*****************************************************************************
707  * GoTop Part
708  */
709 #ifdef CONFIG_TOUCHSCREEN_USB_GOTOP
710 static int gotop_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
711 {
712         dev->x = ((pkt[1] & 0x38) << 4) | pkt[2];
713         dev->y = ((pkt[1] & 0x07) << 7) | pkt[3];
714         dev->touch = pkt[0] & 0x01;
715
716         return 1;
717 }
718 #endif
719
720 /*****************************************************************************
721  * JASTEC Part
722  */
723 #ifdef CONFIG_TOUCHSCREEN_USB_JASTEC
724 static int jastec_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
725 {
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;
729
730         return 1;
731 }
732 #endif
733
734 /*****************************************************************************
735  * Zytronic Part
736  */
737 #ifdef CONFIG_TOUCHSCREEN_USB_ZYTRONIC
738 static int zytronic_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
739 {
740         switch (pkt[0]) {
741         case 0x3A: /* command response */
742                 dbg("%s: Command response %d", __func__, pkt[1]);
743                 break;
744
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;
751
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;
758
759         default:
760                 dbg("%s: Unknown return %d", __func__, pkt[0]);
761                 break;
762         }
763
764         return 0;
765 }
766 #endif
767
768 /*****************************************************************************
769  * NEXIO Part
770  */
771 #ifdef CONFIG_TOUCHSCREEN_USB_NEXIO
772
773 #define NEXIO_TIMEOUT   5000
774 #define NEXIO_BUFSIZE   1024
775 #define NEXIO_THRESHOLD 50
776
777 struct nexio_priv {
778         struct urb *ack;
779         unsigned char *ack_buf;
780 };
781
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));
789
790 static unsigned char nexio_ack_pkt[2] = { 0xaa, 0x02 };
791 static unsigned char nexio_init_pkt[4] = { 0x82, 0x04, 0x0a, 0x0f };
792
793 static void nexio_ack_complete(struct urb *urb)
794 {
795 }
796
797 static int nexio_alloc(struct usbtouch_usb *usbtouch)
798 {
799         struct nexio_priv *priv;
800         int ret = -ENOMEM;
801
802         usbtouch->priv = kmalloc(sizeof(struct nexio_priv), GFP_KERNEL);
803         if (!usbtouch->priv)
804                 goto out_buf;
805
806         priv = usbtouch->priv;
807
808         priv->ack_buf = kmemdup(nexio_ack_pkt, sizeof(nexio_ack_pkt),
809                                 GFP_KERNEL);
810         if (!priv->ack_buf)
811                 goto err_priv;
812
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;
817         }
818
819         return 0;
820
821 err_ack_buf:
822         kfree(priv->ack_buf);
823 err_priv:
824         kfree(priv);
825 out_buf:
826         return ret;
827 }
828
829 static int nexio_init(struct usbtouch_usb *usbtouch)
830 {
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;
839
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;
848         }
849         if (!input_ep || !output_ep)
850                 return -ENXIO;
851
852         buf = kmalloc(NEXIO_BUFSIZE, GFP_NOIO);
853         if (!buf)
854                 goto out_buf;
855
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;
863         }
864
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;
872
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;
890                 }
891         }
892
893         printk(KERN_INFO "Nexio device: %s, firmware version: %s\n",
894                device_name, firmware_ver);
895
896         kfree(firmware_ver);
897         kfree(device_name);
898
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;
903
904 out_buf:
905         kfree(buf);
906         return ret;
907 }
908
909 static void nexio_exit(struct usbtouch_usb *usbtouch)
910 {
911         struct nexio_priv *priv = usbtouch->priv;
912
913         usb_kill_urb(priv->ack);
914         usb_free_urb(priv->ack);
915         kfree(priv->ack_buf);
916         kfree(priv);
917 }
918
919 static int nexio_read_data(struct usbtouch_usb *usbtouch, unsigned char *pkt)
920 {
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;
927
928         /* got touch data? */
929         if ((pkt[0] & 0xe0) != 0xe0)
930                 return 0;
931
932         if (data_len > 0xff)
933                 data_len -= 0x100;
934         if (x_len > 0xff)
935                 x_len -= 0x80;
936
937         /* send ACK */
938         ret = usb_submit_urb(priv->ack, GFP_ATOMIC);
939
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);
947         }
948         /*
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.
954          */
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;
960                 }
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;
967                                 }
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;
993                                 }
994                         }
995                         begin_x = end_x = -1;
996                 }
997
998         }
999         return 0;
1000 }
1001 #endif
1002
1003
1004 /*****************************************************************************
1005  * ELO part
1006  */
1007
1008 #ifdef CONFIG_TOUCHSCREEN_USB_ELO
1009
1010 static int elo_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
1011 {
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];
1016
1017         return 1;
1018 }
1019 #endif
1020
1021
1022 /*****************************************************************************
1023  * the different device descriptors
1024  */
1025 #ifdef MULTI_PACKET
1026 static void usbtouch_process_multi(struct usbtouch_usb *usbtouch,
1027                                    unsigned char *pkt, int len);
1028 #endif
1029
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,
1040         },
1041 #endif
1042
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,
1053         },
1054 #endif
1055
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,
1064         },
1065 #endif
1066
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,
1076         },
1077 #endif
1078
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,
1088         },
1089 #endif
1090
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,
1101         },
1102 #endif
1103
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,
1112         },
1113 #endif
1114
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,
1124         },
1125 #endif
1126
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,
1135         },
1136 #endif
1137
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,
1148         },
1149 #endif
1150
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,
1159         },
1160 #endif
1161
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,
1170         },
1171 #endif
1172
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,
1181         },
1182 #endif
1183
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,
1193         },
1194 #endif
1195
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,
1205         },
1206 #endif
1207
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,
1216         },
1217 #endif
1218
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,
1227         },
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,
1239         },
1240 #endif
1241 };
1242
1243
1244 /*****************************************************************************
1245  * Generic Part
1246  */
1247 static void usbtouch_process_pkt(struct usbtouch_usb *usbtouch,
1248                                  unsigned char *pkt, int len)
1249 {
1250         struct usbtouch_device_info *type = usbtouch->type;
1251
1252         if (!type->read_data(usbtouch, pkt))
1253                         return;
1254
1255         input_report_key(usbtouch->input, BTN_TOUCH, usbtouch->touch);
1256
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);
1263         }
1264         if (type->max_press)
1265                 input_report_abs(usbtouch->input, ABS_PRESSURE, usbtouch->press);
1266         input_sync(usbtouch->input);
1267 }
1268
1269
1270 #ifdef MULTI_PACKET
1271 static void usbtouch_process_multi(struct usbtouch_usb *usbtouch,
1272                                    unsigned char *pkt, int len)
1273 {
1274         unsigned char *buffer;
1275         int pkt_len, pos, buf_len, tmp;
1276
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);
1282
1283                 /* drop? */
1284                 if (unlikely(!pkt_len))
1285                         goto out_flush_buf;
1286
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;
1296
1297                         pkt_len = usbtouch->type->get_pkt_len(
1298                                         usbtouch->buffer, usbtouch->buf_len);
1299                         if (pkt_len < 0)
1300                                 return;
1301                 }
1302
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);
1309
1310                 buffer = pkt + tmp;
1311                 buf_len = len - tmp;
1312         } else {
1313                 buffer = pkt;
1314                 buf_len = len;
1315         }
1316
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);
1323
1324                 /* unknown packet: skip one byte */
1325                 if (unlikely(!pkt_len)) {
1326                         pos++;
1327                         continue;
1328                 }
1329
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;
1338                 }
1339                 pos += pkt_len;
1340         }
1341
1342 out_flush_buf:
1343         usbtouch->buf_len = 0;
1344         return;
1345 }
1346 #endif
1347
1348
1349 static void usbtouch_irq(struct urb *urb)
1350 {
1351         struct usbtouch_usb *usbtouch = urb->context;
1352         int retval;
1353
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;
1375         }
1376
1377         usbtouch->type->process_pkt(usbtouch, usbtouch->data, urb->actual_length);
1378
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);
1385 }
1386
1387 static int usbtouch_open(struct input_dev *input)
1388 {
1389         struct usbtouch_usb *usbtouch = input_get_drvdata(input);
1390         int r;
1391
1392         usbtouch->irq->dev = interface_to_usbdev(usbtouch->interface);
1393
1394         r = usb_autopm_get_interface(usbtouch->interface) ? -EIO : 0;
1395         if (r < 0)
1396                 goto out;
1397
1398         if (!usbtouch->type->irq_always) {
1399                 if (usb_submit_urb(usbtouch->irq, GFP_KERNEL)) {
1400                         r = -EIO;
1401                         goto out_put;
1402                 }
1403         }
1404
1405         usbtouch->interface->needs_remote_wakeup = 1;
1406 out_put:
1407         usb_autopm_put_interface(usbtouch->interface);
1408 out:
1409         return r;
1410 }
1411
1412 static void usbtouch_close(struct input_dev *input)
1413 {
1414         struct usbtouch_usb *usbtouch = input_get_drvdata(input);
1415         int r;
1416
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);
1423 }
1424
1425 static int usbtouch_suspend
1426 (struct usb_interface *intf, pm_message_t message)
1427 {
1428         struct usbtouch_usb *usbtouch = usb_get_intfdata(intf);
1429
1430         usb_kill_urb(usbtouch->irq);
1431
1432         return 0;
1433 }
1434
1435 static int usbtouch_resume(struct usb_interface *intf)
1436 {
1437         struct usbtouch_usb *usbtouch = usb_get_intfdata(intf);
1438         struct input_dev *input = usbtouch->input;
1439         int result = 0;
1440
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);
1445
1446         return result;
1447 }
1448
1449 static int usbtouch_reset_resume(struct usb_interface *intf)
1450 {
1451         struct usbtouch_usb *usbtouch = usb_get_intfdata(intf);
1452         struct input_dev *input = usbtouch->input;
1453         int err = 0;
1454
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;
1462                 }
1463         }
1464
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);
1470
1471         return err;
1472 }
1473
1474 static void usbtouch_free_buffers(struct usb_device *udev,
1475                                   struct usbtouch_usb *usbtouch)
1476 {
1477         usb_free_coherent(udev, usbtouch->type->rept_size,
1478                           usbtouch->data, usbtouch->data_dma);
1479         kfree(usbtouch->buffer);
1480 }
1481
1482 static struct usb_endpoint_descriptor *
1483 usbtouch_get_input_endpoint(struct usb_host_interface *interface)
1484 {
1485         int i;
1486
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;
1490
1491         return NULL;
1492 }
1493
1494 static int usbtouch_probe(struct usb_interface *intf,
1495                           const struct usb_device_id *id)
1496 {
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;
1503
1504         /* some devices are ignored */
1505         if (id->driver_info == DEVTYPE_IGNORE)
1506                 return -ENODEV;
1507
1508         endpoint = usbtouch_get_input_endpoint(intf->cur_altsetting);
1509         if (!endpoint)
1510                 return -ENXIO;
1511
1512         usbtouch = kzalloc(sizeof(struct usbtouch_usb), GFP_KERNEL);
1513         input_dev = input_allocate_device();
1514         if (!usbtouch || !input_dev)
1515                 goto out_free;
1516
1517         type = &usbtouch_dev_info[id->driver_info];
1518         usbtouch->type = type;
1519         if (!type->process_pkt)
1520                 type->process_pkt = usbtouch_process_pkt;
1521
1522         usbtouch->data = usb_alloc_coherent(udev, type->rept_size,
1523                                             GFP_KERNEL, &usbtouch->data_dma);
1524         if (!usbtouch->data)
1525                 goto out_free;
1526
1527         if (type->get_pkt_len) {
1528                 usbtouch->buffer = kmalloc(type->rept_size, GFP_KERNEL);
1529                 if (!usbtouch->buffer)
1530                         goto out_free_buffers;
1531         }
1532
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;
1537         }
1538
1539         usbtouch->interface = intf;
1540         usbtouch->input = input_dev;
1541
1542         if (udev->manufacturer)
1543                 strlcpy(usbtouch->name, udev->manufacturer, sizeof(usbtouch->name));
1544
1545         if (udev->product) {
1546                 if (udev->manufacturer)
1547                         strlcat(usbtouch->name, " ", sizeof(usbtouch->name));
1548                 strlcat(usbtouch->name, udev->product, sizeof(usbtouch->name));
1549         }
1550
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));
1556
1557         usb_make_path(udev, usbtouch->phys, sizeof(usbtouch->phys));
1558         strlcat(usbtouch->phys, "/input0", sizeof(usbtouch->phys));
1559
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;
1564
1565         input_set_drvdata(input_dev, usbtouch);
1566
1567         input_dev->open = usbtouch_open;
1568         input_dev->close = usbtouch_close;
1569
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);
1577
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);
1588
1589         usbtouch->irq->dev = udev;
1590         usbtouch->irq->transfer_dma = usbtouch->data_dma;
1591         usbtouch->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1592
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;
1599                 }
1600         }
1601
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;
1608                 }
1609         }
1610
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;
1615         }
1616
1617         usb_set_intfdata(intf, usbtouch);
1618
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;
1628                 }
1629         }
1630
1631         return 0;
1632
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;
1647 }
1648
1649 static void usbtouch_disconnect(struct usb_interface *intf)
1650 {
1651         struct usbtouch_usb *usbtouch = usb_get_intfdata(intf);
1652
1653         dbg("%s - called", __func__);
1654
1655         if (!usbtouch)
1656                 return;
1657
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);
1667 }
1668
1669 MODULE_DEVICE_TABLE(usb, usbtouch_devices);
1670
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,
1680 };
1681
1682 module_usb_driver(usbtouch_driver);
1683
1684 MODULE_AUTHOR(DRIVER_AUTHOR);
1685 MODULE_DESCRIPTION(DRIVER_DESC);
1686 MODULE_LICENSE("GPL");
1687
1688 MODULE_ALIAS("touchkitusb");
1689 MODULE_ALIAS("itmtouch");
1690 MODULE_ALIAS("mtouchusb");