Merge remote-tracking branch 'grant/devicetree/merge' into dt-fixes
[firefly-linux-kernel-4.4.55.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         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;
119
120         int x, y;
121         int touch, press;
122 };
123
124
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_IDEALTEK,
137         DEVTYPE_GENERAL_TOUCH,
138         DEVTYPE_GOTOP,
139         DEVTYPE_JASTEC,
140         DEVTYPE_E2I,
141         DEVTYPE_ZYTRONIC,
142         DEVTYPE_TC45USB,
143         DEVTYPE_NEXIO,
144         DEVTYPE_ELO,
145         DEVTYPE_ETOUCH,
146 };
147
148 #define USB_DEVICE_HID_CLASS(vend, prod) \
149         .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS \
150                 | USB_DEVICE_ID_MATCH_DEVICE, \
151         .idVendor = (vend), \
152         .idProduct = (prod), \
153         .bInterfaceClass = USB_INTERFACE_CLASS_HID
154
155 static const struct usb_device_id usbtouch_devices[] = {
156 #ifdef CONFIG_TOUCHSCREEN_USB_EGALAX
157         /* ignore the HID capable devices, handled by usbhid */
158         {USB_DEVICE_HID_CLASS(0x0eef, 0x0001), .driver_info = DEVTYPE_IGNORE},
159         {USB_DEVICE_HID_CLASS(0x0eef, 0x0002), .driver_info = DEVTYPE_IGNORE},
160
161         /* normal device IDs */
162         {USB_DEVICE(0x3823, 0x0001), .driver_info = DEVTYPE_EGALAX},
163         {USB_DEVICE(0x3823, 0x0002), .driver_info = DEVTYPE_EGALAX},
164         {USB_DEVICE(0x0123, 0x0001), .driver_info = DEVTYPE_EGALAX},
165         {USB_DEVICE(0x0eef, 0x0001), .driver_info = DEVTYPE_EGALAX},
166         {USB_DEVICE(0x0eef, 0x0002), .driver_info = DEVTYPE_EGALAX},
167         {USB_DEVICE(0x1234, 0x0001), .driver_info = DEVTYPE_EGALAX},
168         {USB_DEVICE(0x1234, 0x0002), .driver_info = DEVTYPE_EGALAX},
169 #endif
170
171 #ifdef CONFIG_TOUCHSCREEN_USB_PANJIT
172         {USB_DEVICE(0x134c, 0x0001), .driver_info = DEVTYPE_PANJIT},
173         {USB_DEVICE(0x134c, 0x0002), .driver_info = DEVTYPE_PANJIT},
174         {USB_DEVICE(0x134c, 0x0003), .driver_info = DEVTYPE_PANJIT},
175         {USB_DEVICE(0x134c, 0x0004), .driver_info = DEVTYPE_PANJIT},
176 #endif
177
178 #ifdef CONFIG_TOUCHSCREEN_USB_3M
179         {USB_DEVICE(0x0596, 0x0001), .driver_info = DEVTYPE_3M},
180 #endif
181
182 #ifdef CONFIG_TOUCHSCREEN_USB_ITM
183         {USB_DEVICE(0x0403, 0xf9e9), .driver_info = DEVTYPE_ITM},
184         {USB_DEVICE(0x16e3, 0xf9e9), .driver_info = DEVTYPE_ITM},
185 #endif
186
187 #ifdef CONFIG_TOUCHSCREEN_USB_ETURBO
188         {USB_DEVICE(0x1234, 0x5678), .driver_info = DEVTYPE_ETURBO},
189 #endif
190
191 #ifdef CONFIG_TOUCHSCREEN_USB_GUNZE
192         {USB_DEVICE(0x0637, 0x0001), .driver_info = DEVTYPE_GUNZE},
193 #endif
194
195 #ifdef CONFIG_TOUCHSCREEN_USB_DMC_TSC10
196         {USB_DEVICE(0x0afa, 0x03e8), .driver_info = DEVTYPE_DMC_TSC10},
197 #endif
198
199 #ifdef CONFIG_TOUCHSCREEN_USB_IRTOUCH
200         {USB_DEVICE(0x595a, 0x0001), .driver_info = DEVTYPE_IRTOUCH},
201         {USB_DEVICE(0x6615, 0x0001), .driver_info = DEVTYPE_IRTOUCH},
202 #endif
203
204 #ifdef CONFIG_TOUCHSCREEN_USB_IDEALTEK
205         {USB_DEVICE(0x1391, 0x1000), .driver_info = DEVTYPE_IDEALTEK},
206 #endif
207
208 #ifdef CONFIG_TOUCHSCREEN_USB_GENERAL_TOUCH
209         {USB_DEVICE(0x0dfc, 0x0001), .driver_info = DEVTYPE_GENERAL_TOUCH},
210 #endif
211
212 #ifdef CONFIG_TOUCHSCREEN_USB_GOTOP
213         {USB_DEVICE(0x08f2, 0x007f), .driver_info = DEVTYPE_GOTOP},
214         {USB_DEVICE(0x08f2, 0x00ce), .driver_info = DEVTYPE_GOTOP},
215         {USB_DEVICE(0x08f2, 0x00f4), .driver_info = DEVTYPE_GOTOP},
216 #endif
217
218 #ifdef CONFIG_TOUCHSCREEN_USB_JASTEC
219         {USB_DEVICE(0x0f92, 0x0001), .driver_info = DEVTYPE_JASTEC},
220 #endif
221
222 #ifdef CONFIG_TOUCHSCREEN_USB_E2I
223         {USB_DEVICE(0x1ac7, 0x0001), .driver_info = DEVTYPE_E2I},
224 #endif
225
226 #ifdef CONFIG_TOUCHSCREEN_USB_ZYTRONIC
227         {USB_DEVICE(0x14c8, 0x0003), .driver_info = DEVTYPE_ZYTRONIC},
228 #endif
229
230 #ifdef CONFIG_TOUCHSCREEN_USB_ETT_TC45USB
231         /* TC5UH */
232         {USB_DEVICE(0x0664, 0x0309), .driver_info = DEVTYPE_TC45USB},
233         /* TC4UM */
234         {USB_DEVICE(0x0664, 0x0306), .driver_info = DEVTYPE_TC45USB},
235 #endif
236
237 #ifdef CONFIG_TOUCHSCREEN_USB_NEXIO
238         /* data interface only */
239         {USB_DEVICE_AND_INTERFACE_INFO(0x10f0, 0x2002, 0x0a, 0x00, 0x00),
240                 .driver_info = DEVTYPE_NEXIO},
241         {USB_DEVICE_AND_INTERFACE_INFO(0x1870, 0x0001, 0x0a, 0x00, 0x00),
242                 .driver_info = DEVTYPE_NEXIO},
243 #endif
244
245 #ifdef CONFIG_TOUCHSCREEN_USB_ELO
246         {USB_DEVICE(0x04e7, 0x0020), .driver_info = DEVTYPE_ELO},
247 #endif
248
249 #ifdef CONFIG_TOUCHSCREEN_USB_EASYTOUCH
250         {USB_DEVICE(0x7374, 0x0001), .driver_info = DEVTYPE_ETOUCH},
251 #endif
252
253         {}
254 };
255
256
257 /*****************************************************************************
258  * e2i Part
259  */
260
261 #ifdef CONFIG_TOUCHSCREEN_USB_E2I
262 static int e2i_init(struct usbtouch_usb *usbtouch)
263 {
264         int ret;
265         struct usb_device *udev = interface_to_usbdev(usbtouch->interface);
266
267         ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
268                               0x01, 0x02, 0x0000, 0x0081,
269                               NULL, 0, USB_CTRL_SET_TIMEOUT);
270
271         dev_dbg(&usbtouch->interface->dev,
272                 "%s - usb_control_msg - E2I_RESET - bytes|err: %d\n",
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_init(struct usbtouch_usb *usbtouch)
307 {
308         int ret, i;
309         unsigned char *buf;
310         struct usb_device *udev = interface_to_usbdev(usbtouch->interface);
311
312         /*
313          * An eGalax diagnostic packet kicks the device into using the right
314          * protocol.  We send a "check active" packet.  The response will be
315          * read later and ignored.
316          */
317
318         buf = kmalloc(3, GFP_KERNEL);
319         if (!buf)
320                 return -ENOMEM;
321
322         buf[0] = EGALAX_PKT_TYPE_DIAG;
323         buf[1] = 1;     /* length */
324         buf[2] = 'A';   /* command - check active */
325
326         for (i = 0; i < 3; i++) {
327                 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
328                                       0,
329                                       USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
330                                       0, 0, buf, 3,
331                                       USB_CTRL_SET_TIMEOUT);
332                 if (ret >= 0) {
333                         ret = 0;
334                         break;
335                 }
336                 if (ret != -EPIPE)
337                         break;
338         }
339
340         kfree(buf);
341
342         return ret;
343 }
344
345 static int egalax_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
346 {
347         if ((pkt[0] & EGALAX_PKT_TYPE_MASK) != EGALAX_PKT_TYPE_REPT)
348                 return 0;
349
350         dev->x = ((pkt[3] & 0x0F) << 7) | (pkt[4] & 0x7F);
351         dev->y = ((pkt[1] & 0x0F) << 7) | (pkt[2] & 0x7F);
352         dev->touch = pkt[0] & 0x01;
353
354         return 1;
355 }
356
357 static int egalax_get_pkt_len(unsigned char *buf, int len)
358 {
359         switch (buf[0] & EGALAX_PKT_TYPE_MASK) {
360         case EGALAX_PKT_TYPE_REPT:
361                 return 5;
362
363         case EGALAX_PKT_TYPE_DIAG:
364                 if (len < 2)
365                         return -1;
366
367                 return buf[1] + 2;
368         }
369
370         return 0;
371 }
372 #endif
373
374 /*****************************************************************************
375  * EasyTouch part
376  */
377
378 #ifdef CONFIG_TOUCHSCREEN_USB_EASYTOUCH
379
380 #ifndef MULTI_PACKET
381 #define MULTI_PACKET
382 #endif
383
384 #define ETOUCH_PKT_TYPE_MASK            0xFE
385 #define ETOUCH_PKT_TYPE_REPT            0x80
386 #define ETOUCH_PKT_TYPE_REPT2           0xB0
387 #define ETOUCH_PKT_TYPE_DIAG            0x0A
388
389 static int etouch_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
390 {
391         if ((pkt[0] & ETOUCH_PKT_TYPE_MASK) != ETOUCH_PKT_TYPE_REPT &&
392                 (pkt[0] & ETOUCH_PKT_TYPE_MASK) != ETOUCH_PKT_TYPE_REPT2)
393                 return 0;
394
395         dev->x = ((pkt[1] & 0x1F) << 7) | (pkt[2] & 0x7F);
396         dev->y = ((pkt[3] & 0x1F) << 7) | (pkt[4] & 0x7F);
397         dev->touch = pkt[0] & 0x01;
398
399         return 1;
400 }
401
402 static int etouch_get_pkt_len(unsigned char *buf, int len)
403 {
404         switch (buf[0] & ETOUCH_PKT_TYPE_MASK) {
405         case ETOUCH_PKT_TYPE_REPT:
406         case ETOUCH_PKT_TYPE_REPT2:
407                 return 5;
408
409         case ETOUCH_PKT_TYPE_DIAG:
410                 if (len < 2)
411                         return -1;
412
413                 return buf[1] + 2;
414         }
415
416         return 0;
417 }
418 #endif
419
420 /*****************************************************************************
421  * PanJit Part
422  */
423 #ifdef CONFIG_TOUCHSCREEN_USB_PANJIT
424 static int panjit_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
425 {
426         dev->x = ((pkt[2] & 0x0F) << 8) | pkt[1];
427         dev->y = ((pkt[4] & 0x0F) << 8) | pkt[3];
428         dev->touch = pkt[0] & 0x01;
429
430         return 1;
431 }
432 #endif
433
434
435 /*****************************************************************************
436  * 3M/Microtouch Part
437  */
438 #ifdef CONFIG_TOUCHSCREEN_USB_3M
439
440 #define MTOUCHUSB_ASYNC_REPORT          1
441 #define MTOUCHUSB_RESET                 7
442 #define MTOUCHUSB_REQ_CTRLLR_ID         10
443
444 static int mtouch_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
445 {
446         if (hwcalib_xy) {
447                 dev->x = (pkt[4] << 8) | pkt[3];
448                 dev->y = 0xffff - ((pkt[6] << 8) | pkt[5]);
449         } else {
450                 dev->x = (pkt[8] << 8) | pkt[7];
451                 dev->y = (pkt[10] << 8) | pkt[9];
452         }
453         dev->touch = (pkt[2] & 0x40) ? 1 : 0;
454
455         return 1;
456 }
457
458 static int mtouch_init(struct usbtouch_usb *usbtouch)
459 {
460         int ret, i;
461         struct usb_device *udev = interface_to_usbdev(usbtouch->interface);
462
463         ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
464                               MTOUCHUSB_RESET,
465                               USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
466                               1, 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
467         dev_dbg(&usbtouch->interface->dev,
468                 "%s - usb_control_msg - MTOUCHUSB_RESET - bytes|err: %d\n",
469                 __func__, ret);
470         if (ret < 0)
471                 return ret;
472         msleep(150);
473
474         for (i = 0; i < 3; i++) {
475                 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
476                                       MTOUCHUSB_ASYNC_REPORT,
477                                       USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
478                                       1, 1, NULL, 0, USB_CTRL_SET_TIMEOUT);
479                 dev_dbg(&usbtouch->interface->dev,
480                         "%s - usb_control_msg - MTOUCHUSB_ASYNC_REPORT - bytes|err: %d\n",
481                         __func__, ret);
482                 if (ret >= 0)
483                         break;
484                 if (ret != -EPIPE)
485                         return ret;
486         }
487
488         /* Default min/max xy are the raw values, override if using hw-calib */
489         if (hwcalib_xy) {
490                 input_set_abs_params(usbtouch->input, ABS_X, 0, 0xffff, 0, 0);
491                 input_set_abs_params(usbtouch->input, ABS_Y, 0, 0xffff, 0, 0);
492         }
493
494         return 0;
495 }
496 #endif
497
498
499 /*****************************************************************************
500  * ITM Part
501  */
502 #ifdef CONFIG_TOUCHSCREEN_USB_ITM
503 static int itm_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
504 {
505         int touch;
506         /*
507          * ITM devices report invalid x/y data if not touched.
508          * if the screen was touched before but is not touched any more
509          * report touch as 0 with the last valid x/y data once. then stop
510          * reporting data until touched again.
511          */
512         dev->press = ((pkt[2] & 0x01) << 7) | (pkt[5] & 0x7F);
513
514         touch = ~pkt[7] & 0x20;
515         if (!touch) {
516                 if (dev->touch) {
517                         dev->touch = 0;
518                         return 1;
519                 }
520
521                 return 0;
522         }
523
524         dev->x = ((pkt[0] & 0x1F) << 7) | (pkt[3] & 0x7F);
525         dev->y = ((pkt[1] & 0x1F) << 7) | (pkt[4] & 0x7F);
526         dev->touch = touch;
527
528         return 1;
529 }
530 #endif
531
532
533 /*****************************************************************************
534  * eTurboTouch part
535  */
536 #ifdef CONFIG_TOUCHSCREEN_USB_ETURBO
537 #ifndef MULTI_PACKET
538 #define MULTI_PACKET
539 #endif
540 static int eturbo_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
541 {
542         unsigned int shift;
543
544         /* packets should start with sync */
545         if (!(pkt[0] & 0x80))
546                 return 0;
547
548         shift = (6 - (pkt[0] & 0x03));
549         dev->x = ((pkt[3] << 7) | pkt[4]) >> shift;
550         dev->y = ((pkt[1] << 7) | pkt[2]) >> shift;
551         dev->touch = (pkt[0] & 0x10) ? 1 : 0;
552
553         return 1;
554 }
555
556 static int eturbo_get_pkt_len(unsigned char *buf, int len)
557 {
558         if (buf[0] & 0x80)
559                 return 5;
560         if (buf[0] == 0x01)
561                 return 3;
562         return 0;
563 }
564 #endif
565
566
567 /*****************************************************************************
568  * Gunze part
569  */
570 #ifdef CONFIG_TOUCHSCREEN_USB_GUNZE
571 static int gunze_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
572 {
573         if (!(pkt[0] & 0x80) || ((pkt[1] | pkt[2] | pkt[3]) & 0x80))
574                 return 0;
575
576         dev->x = ((pkt[0] & 0x1F) << 7) | (pkt[2] & 0x7F);
577         dev->y = ((pkt[1] & 0x1F) << 7) | (pkt[3] & 0x7F);
578         dev->touch = pkt[0] & 0x20;
579
580         return 1;
581 }
582 #endif
583
584 /*****************************************************************************
585  * DMC TSC-10/25 Part
586  *
587  * Documentation about the controller and it's protocol can be found at
588  *   http://www.dmccoltd.com/files/controler/tsc10usb_pi_e.pdf
589  *   http://www.dmccoltd.com/files/controler/tsc25_usb_e.pdf
590  */
591 #ifdef CONFIG_TOUCHSCREEN_USB_DMC_TSC10
592
593 /* supported data rates. currently using 130 */
594 #define TSC10_RATE_POINT        0x50
595 #define TSC10_RATE_30           0x40
596 #define TSC10_RATE_50           0x41
597 #define TSC10_RATE_80           0x42
598 #define TSC10_RATE_100          0x43
599 #define TSC10_RATE_130          0x44
600 #define TSC10_RATE_150          0x45
601
602 /* commands */
603 #define TSC10_CMD_RESET         0x55
604 #define TSC10_CMD_RATE          0x05
605 #define TSC10_CMD_DATA1         0x01
606
607 static int dmc_tsc10_init(struct usbtouch_usb *usbtouch)
608 {
609         struct usb_device *dev = interface_to_usbdev(usbtouch->interface);
610         int ret = -ENOMEM;
611         unsigned char *buf;
612
613         buf = kmalloc(2, GFP_NOIO);
614         if (!buf)
615                 goto err_nobuf;
616         /* reset */
617         buf[0] = buf[1] = 0xFF;
618         ret = usb_control_msg(dev, usb_rcvctrlpipe (dev, 0),
619                               TSC10_CMD_RESET,
620                               USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
621                               0, 0, buf, 2, USB_CTRL_SET_TIMEOUT);
622         if (ret < 0)
623                 goto err_out;
624         if (buf[0] != 0x06) {
625                 ret = -ENODEV;
626                 goto err_out;
627         }
628
629         /* set coordinate output rate */
630         buf[0] = buf[1] = 0xFF;
631         ret = usb_control_msg(dev, usb_rcvctrlpipe (dev, 0),
632                               TSC10_CMD_RATE,
633                               USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
634                               TSC10_RATE_150, 0, buf, 2, USB_CTRL_SET_TIMEOUT);
635         if (ret < 0)
636                 goto err_out;
637         if ((buf[0] != 0x06) && (buf[0] != 0x15 || buf[1] != 0x01)) {
638                 ret = -ENODEV;
639                 goto err_out;
640         }
641
642         /* start sending data */
643         ret = usb_control_msg(dev, usb_rcvctrlpipe (dev, 0),
644                               TSC10_CMD_DATA1,
645                               USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
646                               0, 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
647 err_out:
648         kfree(buf);
649 err_nobuf:
650         return ret;
651 }
652
653
654 static int dmc_tsc10_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
655 {
656         dev->x = ((pkt[2] & 0x03) << 8) | pkt[1];
657         dev->y = ((pkt[4] & 0x03) << 8) | pkt[3];
658         dev->touch = pkt[0] & 0x01;
659
660         return 1;
661 }
662 #endif
663
664
665 /*****************************************************************************
666  * IRTOUCH Part
667  */
668 #ifdef CONFIG_TOUCHSCREEN_USB_IRTOUCH
669 static int irtouch_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
670 {
671         dev->x = (pkt[3] << 8) | pkt[2];
672         dev->y = (pkt[5] << 8) | pkt[4];
673         dev->touch = (pkt[1] & 0x03) ? 1 : 0;
674
675         return 1;
676 }
677 #endif
678
679 /*****************************************************************************
680  * ET&T TC5UH/TC4UM part
681  */
682 #ifdef CONFIG_TOUCHSCREEN_USB_ETT_TC45USB
683 static int tc45usb_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
684 {
685         dev->x = ((pkt[2] & 0x0F) << 8) | pkt[1];
686         dev->y = ((pkt[4] & 0x0F) << 8) | pkt[3];
687         dev->touch = pkt[0] & 0x01;
688
689         return 1;
690 }
691 #endif
692
693 /*****************************************************************************
694  * IdealTEK URTC1000 Part
695  */
696 #ifdef CONFIG_TOUCHSCREEN_USB_IDEALTEK
697 #ifndef MULTI_PACKET
698 #define MULTI_PACKET
699 #endif
700 static int idealtek_get_pkt_len(unsigned char *buf, int len)
701 {
702         if (buf[0] & 0x80)
703                 return 5;
704         if (buf[0] == 0x01)
705                 return len;
706         return 0;
707 }
708
709 static int idealtek_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
710 {
711         switch (pkt[0] & 0x98) {
712         case 0x88:
713                 /* touch data in IdealTEK mode */
714                 dev->x = (pkt[1] << 5) | (pkt[2] >> 2);
715                 dev->y = (pkt[3] << 5) | (pkt[4] >> 2);
716                 dev->touch = (pkt[0] & 0x40) ? 1 : 0;
717                 return 1;
718
719         case 0x98:
720                 /* touch data in MT emulation mode */
721                 dev->x = (pkt[2] << 5) | (pkt[1] >> 2);
722                 dev->y = (pkt[4] << 5) | (pkt[3] >> 2);
723                 dev->touch = (pkt[0] & 0x40) ? 1 : 0;
724                 return 1;
725
726         default:
727                 return 0;
728         }
729 }
730 #endif
731
732 /*****************************************************************************
733  * General Touch Part
734  */
735 #ifdef CONFIG_TOUCHSCREEN_USB_GENERAL_TOUCH
736 static int general_touch_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
737 {
738         dev->x = (pkt[2] << 8) | pkt[1];
739         dev->y = (pkt[4] << 8) | pkt[3];
740         dev->press = pkt[5] & 0xff;
741         dev->touch = pkt[0] & 0x01;
742
743         return 1;
744 }
745 #endif
746
747 /*****************************************************************************
748  * GoTop Part
749  */
750 #ifdef CONFIG_TOUCHSCREEN_USB_GOTOP
751 static int gotop_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
752 {
753         dev->x = ((pkt[1] & 0x38) << 4) | pkt[2];
754         dev->y = ((pkt[1] & 0x07) << 7) | pkt[3];
755         dev->touch = pkt[0] & 0x01;
756
757         return 1;
758 }
759 #endif
760
761 /*****************************************************************************
762  * JASTEC Part
763  */
764 #ifdef CONFIG_TOUCHSCREEN_USB_JASTEC
765 static int jastec_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
766 {
767         dev->x = ((pkt[0] & 0x3f) << 6) | (pkt[2] & 0x3f);
768         dev->y = ((pkt[1] & 0x3f) << 6) | (pkt[3] & 0x3f);
769         dev->touch = (pkt[0] & 0x40) >> 6;
770
771         return 1;
772 }
773 #endif
774
775 /*****************************************************************************
776  * Zytronic Part
777  */
778 #ifdef CONFIG_TOUCHSCREEN_USB_ZYTRONIC
779 static int zytronic_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
780 {
781         struct usb_interface *intf = dev->interface;
782
783         switch (pkt[0]) {
784         case 0x3A: /* command response */
785                 dev_dbg(&intf->dev, "%s: Command response %d\n", __func__, pkt[1]);
786                 break;
787
788         case 0xC0: /* down */
789                 dev->x = (pkt[1] & 0x7f) | ((pkt[2] & 0x07) << 7);
790                 dev->y = (pkt[3] & 0x7f) | ((pkt[4] & 0x07) << 7);
791                 dev->touch = 1;
792                 dev_dbg(&intf->dev, "%s: down %d,%d\n", __func__, dev->x, dev->y);
793                 return 1;
794
795         case 0x80: /* up */
796                 dev->x = (pkt[1] & 0x7f) | ((pkt[2] & 0x07) << 7);
797                 dev->y = (pkt[3] & 0x7f) | ((pkt[4] & 0x07) << 7);
798                 dev->touch = 0;
799                 dev_dbg(&intf->dev, "%s: up %d,%d\n", __func__, dev->x, dev->y);
800                 return 1;
801
802         default:
803                 dev_dbg(&intf->dev, "%s: Unknown return %d\n", __func__, pkt[0]);
804                 break;
805         }
806
807         return 0;
808 }
809 #endif
810
811 /*****************************************************************************
812  * NEXIO Part
813  */
814 #ifdef CONFIG_TOUCHSCREEN_USB_NEXIO
815
816 #define NEXIO_TIMEOUT   5000
817 #define NEXIO_BUFSIZE   1024
818 #define NEXIO_THRESHOLD 50
819
820 struct nexio_priv {
821         struct urb *ack;
822         unsigned char *ack_buf;
823 };
824
825 struct nexio_touch_packet {
826         u8      flags;          /* 0xe1 = touch, 0xe1 = release */
827         __be16  data_len;       /* total bytes of touch data */
828         __be16  x_len;          /* bytes for X axis */
829         __be16  y_len;          /* bytes for Y axis */
830         u8      data[];
831 } __attribute__ ((packed));
832
833 static unsigned char nexio_ack_pkt[2] = { 0xaa, 0x02 };
834 static unsigned char nexio_init_pkt[4] = { 0x82, 0x04, 0x0a, 0x0f };
835
836 static void nexio_ack_complete(struct urb *urb)
837 {
838 }
839
840 static int nexio_alloc(struct usbtouch_usb *usbtouch)
841 {
842         struct nexio_priv *priv;
843         int ret = -ENOMEM;
844
845         usbtouch->priv = kmalloc(sizeof(struct nexio_priv), GFP_KERNEL);
846         if (!usbtouch->priv)
847                 goto out_buf;
848
849         priv = usbtouch->priv;
850
851         priv->ack_buf = kmemdup(nexio_ack_pkt, sizeof(nexio_ack_pkt),
852                                 GFP_KERNEL);
853         if (!priv->ack_buf)
854                 goto err_priv;
855
856         priv->ack = usb_alloc_urb(0, GFP_KERNEL);
857         if (!priv->ack) {
858                 dev_dbg(&usbtouch->interface->dev,
859                         "%s - usb_alloc_urb failed: usbtouch->ack\n", __func__);
860                 goto err_ack_buf;
861         }
862
863         return 0;
864
865 err_ack_buf:
866         kfree(priv->ack_buf);
867 err_priv:
868         kfree(priv);
869 out_buf:
870         return ret;
871 }
872
873 static int nexio_init(struct usbtouch_usb *usbtouch)
874 {
875         struct usb_device *dev = interface_to_usbdev(usbtouch->interface);
876         struct usb_host_interface *interface = usbtouch->interface->cur_altsetting;
877         struct nexio_priv *priv = usbtouch->priv;
878         int ret = -ENOMEM;
879         int actual_len, i;
880         unsigned char *buf;
881         char *firmware_ver = NULL, *device_name = NULL;
882         int input_ep = 0, output_ep = 0;
883
884         /* find first input and output endpoint */
885         for (i = 0; i < interface->desc.bNumEndpoints; i++) {
886                 if (!input_ep &&
887                     usb_endpoint_dir_in(&interface->endpoint[i].desc))
888                         input_ep = interface->endpoint[i].desc.bEndpointAddress;
889                 if (!output_ep &&
890                     usb_endpoint_dir_out(&interface->endpoint[i].desc))
891                         output_ep = interface->endpoint[i].desc.bEndpointAddress;
892         }
893         if (!input_ep || !output_ep)
894                 return -ENXIO;
895
896         buf = kmalloc(NEXIO_BUFSIZE, GFP_NOIO);
897         if (!buf)
898                 goto out_buf;
899
900         /* two empty reads */
901         for (i = 0; i < 2; i++) {
902                 ret = usb_bulk_msg(dev, usb_rcvbulkpipe(dev, input_ep),
903                                    buf, NEXIO_BUFSIZE, &actual_len,
904                                    NEXIO_TIMEOUT);
905                 if (ret < 0)
906                         goto out_buf;
907         }
908
909         /* send init command */
910         memcpy(buf, nexio_init_pkt, sizeof(nexio_init_pkt));
911         ret = usb_bulk_msg(dev, usb_sndbulkpipe(dev, output_ep),
912                            buf, sizeof(nexio_init_pkt), &actual_len,
913                            NEXIO_TIMEOUT);
914         if (ret < 0)
915                 goto out_buf;
916
917         /* read replies */
918         for (i = 0; i < 3; i++) {
919                 memset(buf, 0, NEXIO_BUFSIZE);
920                 ret = usb_bulk_msg(dev, usb_rcvbulkpipe(dev, input_ep),
921                                    buf, NEXIO_BUFSIZE, &actual_len,
922                                    NEXIO_TIMEOUT);
923                 if (ret < 0 || actual_len < 1 || buf[1] != actual_len)
924                         continue;
925                 switch (buf[0]) {
926                 case 0x83:      /* firmware version */
927                         if (!firmware_ver)
928                                 firmware_ver = kstrdup(&buf[2], GFP_NOIO);
929                         break;
930                 case 0x84:      /* device name */
931                         if (!device_name)
932                                 device_name = kstrdup(&buf[2], GFP_NOIO);
933                         break;
934                 }
935         }
936
937         printk(KERN_INFO "Nexio device: %s, firmware version: %s\n",
938                device_name, firmware_ver);
939
940         kfree(firmware_ver);
941         kfree(device_name);
942
943         usb_fill_bulk_urb(priv->ack, dev, usb_sndbulkpipe(dev, output_ep),
944                           priv->ack_buf, sizeof(nexio_ack_pkt),
945                           nexio_ack_complete, usbtouch);
946         ret = 0;
947
948 out_buf:
949         kfree(buf);
950         return ret;
951 }
952
953 static void nexio_exit(struct usbtouch_usb *usbtouch)
954 {
955         struct nexio_priv *priv = usbtouch->priv;
956
957         usb_kill_urb(priv->ack);
958         usb_free_urb(priv->ack);
959         kfree(priv->ack_buf);
960         kfree(priv);
961 }
962
963 static int nexio_read_data(struct usbtouch_usb *usbtouch, unsigned char *pkt)
964 {
965         struct nexio_touch_packet *packet = (void *) pkt;
966         struct nexio_priv *priv = usbtouch->priv;
967         unsigned int data_len = be16_to_cpu(packet->data_len);
968         unsigned int x_len = be16_to_cpu(packet->x_len);
969         unsigned int y_len = be16_to_cpu(packet->y_len);
970         int x, y, begin_x, begin_y, end_x, end_y, w, h, ret;
971
972         /* got touch data? */
973         if ((pkt[0] & 0xe0) != 0xe0)
974                 return 0;
975
976         if (data_len > 0xff)
977                 data_len -= 0x100;
978         if (x_len > 0xff)
979                 x_len -= 0x80;
980
981         /* send ACK */
982         ret = usb_submit_urb(priv->ack, GFP_ATOMIC);
983
984         if (!usbtouch->type->max_xc) {
985                 usbtouch->type->max_xc = 2 * x_len;
986                 input_set_abs_params(usbtouch->input, ABS_X,
987                                      0, usbtouch->type->max_xc, 0, 0);
988                 usbtouch->type->max_yc = 2 * y_len;
989                 input_set_abs_params(usbtouch->input, ABS_Y,
990                                      0, usbtouch->type->max_yc, 0, 0);
991         }
992         /*
993          * The device reports state of IR sensors on X and Y axes.
994          * Each byte represents "darkness" percentage (0-100) of one element.
995          * 17" touchscreen reports only 64 x 52 bytes so the resolution is low.
996          * This also means that there's a limited multi-touch capability but
997          * it's disabled (and untested) here as there's no X driver for that.
998          */
999         begin_x = end_x = begin_y = end_y = -1;
1000         for (x = 0; x < x_len; x++) {
1001                 if (begin_x == -1 && packet->data[x] > NEXIO_THRESHOLD) {
1002                         begin_x = x;
1003                         continue;
1004                 }
1005                 if (end_x == -1 && begin_x != -1 && packet->data[x] < NEXIO_THRESHOLD) {
1006                         end_x = x - 1;
1007                         for (y = x_len; y < data_len; y++) {
1008                                 if (begin_y == -1 && packet->data[y] > NEXIO_THRESHOLD) {
1009                                         begin_y = y - x_len;
1010                                         continue;
1011                                 }
1012                                 if (end_y == -1 &&
1013                                     begin_y != -1 && packet->data[y] < NEXIO_THRESHOLD) {
1014                                         end_y = y - 1 - x_len;
1015                                         w = end_x - begin_x;
1016                                         h = end_y - begin_y;
1017 #if 0
1018                                         /* multi-touch */
1019                                         input_report_abs(usbtouch->input,
1020                                                     ABS_MT_TOUCH_MAJOR, max(w,h));
1021                                         input_report_abs(usbtouch->input,
1022                                                     ABS_MT_TOUCH_MINOR, min(x,h));
1023                                         input_report_abs(usbtouch->input,
1024                                                     ABS_MT_POSITION_X, 2*begin_x+w);
1025                                         input_report_abs(usbtouch->input,
1026                                                     ABS_MT_POSITION_Y, 2*begin_y+h);
1027                                         input_report_abs(usbtouch->input,
1028                                                     ABS_MT_ORIENTATION, w > h);
1029                                         input_mt_sync(usbtouch->input);
1030 #endif
1031                                         /* single touch */
1032                                         usbtouch->x = 2 * begin_x + w;
1033                                         usbtouch->y = 2 * begin_y + h;
1034                                         usbtouch->touch = packet->flags & 0x01;
1035                                         begin_y = end_y = -1;
1036                                         return 1;
1037                                 }
1038                         }
1039                         begin_x = end_x = -1;
1040                 }
1041
1042         }
1043         return 0;
1044 }
1045 #endif
1046
1047
1048 /*****************************************************************************
1049  * ELO part
1050  */
1051
1052 #ifdef CONFIG_TOUCHSCREEN_USB_ELO
1053
1054 static int elo_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
1055 {
1056         dev->x = (pkt[3] << 8) | pkt[2];
1057         dev->y = (pkt[5] << 8) | pkt[4];
1058         dev->touch = pkt[6] > 0;
1059         dev->press = pkt[6];
1060
1061         return 1;
1062 }
1063 #endif
1064
1065
1066 /*****************************************************************************
1067  * the different device descriptors
1068  */
1069 #ifdef MULTI_PACKET
1070 static void usbtouch_process_multi(struct usbtouch_usb *usbtouch,
1071                                    unsigned char *pkt, int len);
1072 #endif
1073
1074 static struct usbtouch_device_info usbtouch_dev_info[] = {
1075 #ifdef CONFIG_TOUCHSCREEN_USB_ELO
1076         [DEVTYPE_ELO] = {
1077                 .min_xc         = 0x0,
1078                 .max_xc         = 0x0fff,
1079                 .min_yc         = 0x0,
1080                 .max_yc         = 0x0fff,
1081                 .max_press      = 0xff,
1082                 .rept_size      = 8,
1083                 .read_data      = elo_read_data,
1084         },
1085 #endif
1086
1087 #ifdef CONFIG_TOUCHSCREEN_USB_EGALAX
1088         [DEVTYPE_EGALAX] = {
1089                 .min_xc         = 0x0,
1090                 .max_xc         = 0x07ff,
1091                 .min_yc         = 0x0,
1092                 .max_yc         = 0x07ff,
1093                 .rept_size      = 16,
1094                 .process_pkt    = usbtouch_process_multi,
1095                 .get_pkt_len    = egalax_get_pkt_len,
1096                 .read_data      = egalax_read_data,
1097                 .init           = egalax_init,
1098         },
1099 #endif
1100
1101 #ifdef CONFIG_TOUCHSCREEN_USB_PANJIT
1102         [DEVTYPE_PANJIT] = {
1103                 .min_xc         = 0x0,
1104                 .max_xc         = 0x0fff,
1105                 .min_yc         = 0x0,
1106                 .max_yc         = 0x0fff,
1107                 .rept_size      = 8,
1108                 .read_data      = panjit_read_data,
1109         },
1110 #endif
1111
1112 #ifdef CONFIG_TOUCHSCREEN_USB_3M
1113         [DEVTYPE_3M] = {
1114                 .min_xc         = 0x0,
1115                 .max_xc         = 0x4000,
1116                 .min_yc         = 0x0,
1117                 .max_yc         = 0x4000,
1118                 .rept_size      = 11,
1119                 .read_data      = mtouch_read_data,
1120                 .init           = mtouch_init,
1121         },
1122 #endif
1123
1124 #ifdef CONFIG_TOUCHSCREEN_USB_ITM
1125         [DEVTYPE_ITM] = {
1126                 .min_xc         = 0x0,
1127                 .max_xc         = 0x0fff,
1128                 .min_yc         = 0x0,
1129                 .max_yc         = 0x0fff,
1130                 .max_press      = 0xff,
1131                 .rept_size      = 8,
1132                 .read_data      = itm_read_data,
1133         },
1134 #endif
1135
1136 #ifdef CONFIG_TOUCHSCREEN_USB_ETURBO
1137         [DEVTYPE_ETURBO] = {
1138                 .min_xc         = 0x0,
1139                 .max_xc         = 0x07ff,
1140                 .min_yc         = 0x0,
1141                 .max_yc         = 0x07ff,
1142                 .rept_size      = 8,
1143                 .process_pkt    = usbtouch_process_multi,
1144                 .get_pkt_len    = eturbo_get_pkt_len,
1145                 .read_data      = eturbo_read_data,
1146         },
1147 #endif
1148
1149 #ifdef CONFIG_TOUCHSCREEN_USB_GUNZE
1150         [DEVTYPE_GUNZE] = {
1151                 .min_xc         = 0x0,
1152                 .max_xc         = 0x0fff,
1153                 .min_yc         = 0x0,
1154                 .max_yc         = 0x0fff,
1155                 .rept_size      = 4,
1156                 .read_data      = gunze_read_data,
1157         },
1158 #endif
1159
1160 #ifdef CONFIG_TOUCHSCREEN_USB_DMC_TSC10
1161         [DEVTYPE_DMC_TSC10] = {
1162                 .min_xc         = 0x0,
1163                 .max_xc         = 0x03ff,
1164                 .min_yc         = 0x0,
1165                 .max_yc         = 0x03ff,
1166                 .rept_size      = 5,
1167                 .init           = dmc_tsc10_init,
1168                 .read_data      = dmc_tsc10_read_data,
1169         },
1170 #endif
1171
1172 #ifdef CONFIG_TOUCHSCREEN_USB_IRTOUCH
1173         [DEVTYPE_IRTOUCH] = {
1174                 .min_xc         = 0x0,
1175                 .max_xc         = 0x0fff,
1176                 .min_yc         = 0x0,
1177                 .max_yc         = 0x0fff,
1178                 .rept_size      = 8,
1179                 .read_data      = irtouch_read_data,
1180         },
1181 #endif
1182
1183 #ifdef CONFIG_TOUCHSCREEN_USB_IDEALTEK
1184         [DEVTYPE_IDEALTEK] = {
1185                 .min_xc         = 0x0,
1186                 .max_xc         = 0x0fff,
1187                 .min_yc         = 0x0,
1188                 .max_yc         = 0x0fff,
1189                 .rept_size      = 8,
1190                 .process_pkt    = usbtouch_process_multi,
1191                 .get_pkt_len    = idealtek_get_pkt_len,
1192                 .read_data      = idealtek_read_data,
1193         },
1194 #endif
1195
1196 #ifdef CONFIG_TOUCHSCREEN_USB_GENERAL_TOUCH
1197         [DEVTYPE_GENERAL_TOUCH] = {
1198                 .min_xc         = 0x0,
1199                 .max_xc         = 0x7fff,
1200                 .min_yc         = 0x0,
1201                 .max_yc         = 0x7fff,
1202                 .rept_size      = 7,
1203                 .read_data      = general_touch_read_data,
1204         },
1205 #endif
1206
1207 #ifdef CONFIG_TOUCHSCREEN_USB_GOTOP
1208         [DEVTYPE_GOTOP] = {
1209                 .min_xc         = 0x0,
1210                 .max_xc         = 0x03ff,
1211                 .min_yc         = 0x0,
1212                 .max_yc         = 0x03ff,
1213                 .rept_size      = 4,
1214                 .read_data      = gotop_read_data,
1215         },
1216 #endif
1217
1218 #ifdef CONFIG_TOUCHSCREEN_USB_JASTEC
1219         [DEVTYPE_JASTEC] = {
1220                 .min_xc         = 0x0,
1221                 .max_xc         = 0x0fff,
1222                 .min_yc         = 0x0,
1223                 .max_yc         = 0x0fff,
1224                 .rept_size      = 4,
1225                 .read_data      = jastec_read_data,
1226         },
1227 #endif
1228
1229 #ifdef CONFIG_TOUCHSCREEN_USB_E2I
1230         [DEVTYPE_E2I] = {
1231                 .min_xc         = 0x0,
1232                 .max_xc         = 0x7fff,
1233                 .min_yc         = 0x0,
1234                 .max_yc         = 0x7fff,
1235                 .rept_size      = 6,
1236                 .init           = e2i_init,
1237                 .read_data      = e2i_read_data,
1238         },
1239 #endif
1240
1241 #ifdef CONFIG_TOUCHSCREEN_USB_ZYTRONIC
1242         [DEVTYPE_ZYTRONIC] = {
1243                 .min_xc         = 0x0,
1244                 .max_xc         = 0x03ff,
1245                 .min_yc         = 0x0,
1246                 .max_yc         = 0x03ff,
1247                 .rept_size      = 5,
1248                 .read_data      = zytronic_read_data,
1249                 .irq_always     = true,
1250         },
1251 #endif
1252
1253 #ifdef CONFIG_TOUCHSCREEN_USB_ETT_TC45USB
1254         [DEVTYPE_TC45USB] = {
1255                 .min_xc         = 0x0,
1256                 .max_xc         = 0x0fff,
1257                 .min_yc         = 0x0,
1258                 .max_yc         = 0x0fff,
1259                 .rept_size      = 5,
1260                 .read_data      = tc45usb_read_data,
1261         },
1262 #endif
1263
1264 #ifdef CONFIG_TOUCHSCREEN_USB_NEXIO
1265         [DEVTYPE_NEXIO] = {
1266                 .rept_size      = 1024,
1267                 .irq_always     = true,
1268                 .read_data      = nexio_read_data,
1269                 .alloc          = nexio_alloc,
1270                 .init           = nexio_init,
1271                 .exit           = nexio_exit,
1272         },
1273 #endif
1274 #ifdef CONFIG_TOUCHSCREEN_USB_EASYTOUCH
1275         [DEVTYPE_ETOUCH] = {
1276                 .min_xc         = 0x0,
1277                 .max_xc         = 0x07ff,
1278                 .min_yc         = 0x0,
1279                 .max_yc         = 0x07ff,
1280                 .rept_size      = 16,
1281                 .process_pkt    = usbtouch_process_multi,
1282                 .get_pkt_len    = etouch_get_pkt_len,
1283                 .read_data      = etouch_read_data,
1284         },
1285 #endif
1286 };
1287
1288
1289 /*****************************************************************************
1290  * Generic Part
1291  */
1292 static void usbtouch_process_pkt(struct usbtouch_usb *usbtouch,
1293                                  unsigned char *pkt, int len)
1294 {
1295         struct usbtouch_device_info *type = usbtouch->type;
1296
1297         if (!type->read_data(usbtouch, pkt))
1298                         return;
1299
1300         input_report_key(usbtouch->input, BTN_TOUCH, usbtouch->touch);
1301
1302         if (swap_xy) {
1303                 input_report_abs(usbtouch->input, ABS_X, usbtouch->y);
1304                 input_report_abs(usbtouch->input, ABS_Y, usbtouch->x);
1305         } else {
1306                 input_report_abs(usbtouch->input, ABS_X, usbtouch->x);
1307                 input_report_abs(usbtouch->input, ABS_Y, usbtouch->y);
1308         }
1309         if (type->max_press)
1310                 input_report_abs(usbtouch->input, ABS_PRESSURE, usbtouch->press);
1311         input_sync(usbtouch->input);
1312 }
1313
1314
1315 #ifdef MULTI_PACKET
1316 static void usbtouch_process_multi(struct usbtouch_usb *usbtouch,
1317                                    unsigned char *pkt, int len)
1318 {
1319         unsigned char *buffer;
1320         int pkt_len, pos, buf_len, tmp;
1321
1322         /* process buffer */
1323         if (unlikely(usbtouch->buf_len)) {
1324                 /* try to get size */
1325                 pkt_len = usbtouch->type->get_pkt_len(
1326                                 usbtouch->buffer, usbtouch->buf_len);
1327
1328                 /* drop? */
1329                 if (unlikely(!pkt_len))
1330                         goto out_flush_buf;
1331
1332                 /* need to append -pkt_len bytes before able to get size */
1333                 if (unlikely(pkt_len < 0)) {
1334                         int append = -pkt_len;
1335                         if (unlikely(append > len))
1336                                append = len;
1337                         if (usbtouch->buf_len + append >= usbtouch->type->rept_size)
1338                                 goto out_flush_buf;
1339                         memcpy(usbtouch->buffer + usbtouch->buf_len, pkt, append);
1340                         usbtouch->buf_len += append;
1341
1342                         pkt_len = usbtouch->type->get_pkt_len(
1343                                         usbtouch->buffer, usbtouch->buf_len);
1344                         if (pkt_len < 0)
1345                                 return;
1346                 }
1347
1348                 /* append */
1349                 tmp = pkt_len - usbtouch->buf_len;
1350                 if (usbtouch->buf_len + tmp >= usbtouch->type->rept_size)
1351                         goto out_flush_buf;
1352                 memcpy(usbtouch->buffer + usbtouch->buf_len, pkt, tmp);
1353                 usbtouch_process_pkt(usbtouch, usbtouch->buffer, pkt_len);
1354
1355                 buffer = pkt + tmp;
1356                 buf_len = len - tmp;
1357         } else {
1358                 buffer = pkt;
1359                 buf_len = len;
1360         }
1361
1362         /* loop over the received packet, process */
1363         pos = 0;
1364         while (pos < buf_len) {
1365                 /* get packet len */
1366                 pkt_len = usbtouch->type->get_pkt_len(buffer + pos,
1367                                                         buf_len - pos);
1368
1369                 /* unknown packet: skip one byte */
1370                 if (unlikely(!pkt_len)) {
1371                         pos++;
1372                         continue;
1373                 }
1374
1375                 /* full packet: process */
1376                 if (likely((pkt_len > 0) && (pkt_len <= buf_len - pos))) {
1377                         usbtouch_process_pkt(usbtouch, buffer + pos, pkt_len);
1378                 } else {
1379                         /* incomplete packet: save in buffer */
1380                         memcpy(usbtouch->buffer, buffer + pos, buf_len - pos);
1381                         usbtouch->buf_len = buf_len - pos;
1382                         return;
1383                 }
1384                 pos += pkt_len;
1385         }
1386
1387 out_flush_buf:
1388         usbtouch->buf_len = 0;
1389         return;
1390 }
1391 #endif
1392
1393
1394 static void usbtouch_irq(struct urb *urb)
1395 {
1396         struct usbtouch_usb *usbtouch = urb->context;
1397         struct device *dev = &usbtouch->interface->dev;
1398         int retval;
1399
1400         switch (urb->status) {
1401         case 0:
1402                 /* success */
1403                 break;
1404         case -ETIME:
1405                 /* this urb is timing out */
1406                 dev_dbg(dev,
1407                         "%s - urb timed out - was the device unplugged?\n",
1408                         __func__);
1409                 return;
1410         case -ECONNRESET:
1411         case -ENOENT:
1412         case -ESHUTDOWN:
1413         case -EPIPE:
1414                 /* this urb is terminated, clean up */
1415                 dev_dbg(dev, "%s - urb shutting down with status: %d\n",
1416                         __func__, urb->status);
1417                 return;
1418         default:
1419                 dev_dbg(dev, "%s - nonzero urb status received: %d\n",
1420                         __func__, urb->status);
1421                 goto exit;
1422         }
1423
1424         usbtouch->type->process_pkt(usbtouch, usbtouch->data, urb->actual_length);
1425
1426 exit:
1427         usb_mark_last_busy(interface_to_usbdev(usbtouch->interface));
1428         retval = usb_submit_urb(urb, GFP_ATOMIC);
1429         if (retval)
1430                 dev_err(dev, "%s - usb_submit_urb failed with result: %d\n",
1431                         __func__, retval);
1432 }
1433
1434 static int usbtouch_open(struct input_dev *input)
1435 {
1436         struct usbtouch_usb *usbtouch = input_get_drvdata(input);
1437         int r;
1438
1439         usbtouch->irq->dev = interface_to_usbdev(usbtouch->interface);
1440
1441         r = usb_autopm_get_interface(usbtouch->interface) ? -EIO : 0;
1442         if (r < 0)
1443                 goto out;
1444
1445         if (!usbtouch->type->irq_always) {
1446                 if (usb_submit_urb(usbtouch->irq, GFP_KERNEL)) {
1447                         r = -EIO;
1448                         goto out_put;
1449                 }
1450         }
1451
1452         usbtouch->interface->needs_remote_wakeup = 1;
1453 out_put:
1454         usb_autopm_put_interface(usbtouch->interface);
1455 out:
1456         return r;
1457 }
1458
1459 static void usbtouch_close(struct input_dev *input)
1460 {
1461         struct usbtouch_usb *usbtouch = input_get_drvdata(input);
1462         int r;
1463
1464         if (!usbtouch->type->irq_always)
1465                 usb_kill_urb(usbtouch->irq);
1466         r = usb_autopm_get_interface(usbtouch->interface);
1467         usbtouch->interface->needs_remote_wakeup = 0;
1468         if (!r)
1469                 usb_autopm_put_interface(usbtouch->interface);
1470 }
1471
1472 static int usbtouch_suspend
1473 (struct usb_interface *intf, pm_message_t message)
1474 {
1475         struct usbtouch_usb *usbtouch = usb_get_intfdata(intf);
1476
1477         usb_kill_urb(usbtouch->irq);
1478
1479         return 0;
1480 }
1481
1482 static int usbtouch_resume(struct usb_interface *intf)
1483 {
1484         struct usbtouch_usb *usbtouch = usb_get_intfdata(intf);
1485         struct input_dev *input = usbtouch->input;
1486         int result = 0;
1487
1488         mutex_lock(&input->mutex);
1489         if (input->users || usbtouch->type->irq_always)
1490                 result = usb_submit_urb(usbtouch->irq, GFP_NOIO);
1491         mutex_unlock(&input->mutex);
1492
1493         return result;
1494 }
1495
1496 static int usbtouch_reset_resume(struct usb_interface *intf)
1497 {
1498         struct usbtouch_usb *usbtouch = usb_get_intfdata(intf);
1499         struct input_dev *input = usbtouch->input;
1500         int err = 0;
1501
1502         /* reinit the device */
1503         if (usbtouch->type->init) {
1504                 err = usbtouch->type->init(usbtouch);
1505                 if (err) {
1506                         dev_dbg(&intf->dev,
1507                                 "%s - type->init() failed, err: %d\n",
1508                                 __func__, err);
1509                         return err;
1510                 }
1511         }
1512
1513         /* restart IO if needed */
1514         mutex_lock(&input->mutex);
1515         if (input->users)
1516                 err = usb_submit_urb(usbtouch->irq, GFP_NOIO);
1517         mutex_unlock(&input->mutex);
1518
1519         return err;
1520 }
1521
1522 static void usbtouch_free_buffers(struct usb_device *udev,
1523                                   struct usbtouch_usb *usbtouch)
1524 {
1525         usb_free_coherent(udev, usbtouch->data_size,
1526                           usbtouch->data, usbtouch->data_dma);
1527         kfree(usbtouch->buffer);
1528 }
1529
1530 static struct usb_endpoint_descriptor *
1531 usbtouch_get_input_endpoint(struct usb_host_interface *interface)
1532 {
1533         int i;
1534
1535         for (i = 0; i < interface->desc.bNumEndpoints; i++)
1536                 if (usb_endpoint_dir_in(&interface->endpoint[i].desc))
1537                         return &interface->endpoint[i].desc;
1538
1539         return NULL;
1540 }
1541
1542 static int usbtouch_probe(struct usb_interface *intf,
1543                           const struct usb_device_id *id)
1544 {
1545         struct usbtouch_usb *usbtouch;
1546         struct input_dev *input_dev;
1547         struct usb_endpoint_descriptor *endpoint;
1548         struct usb_device *udev = interface_to_usbdev(intf);
1549         struct usbtouch_device_info *type;
1550         int err = -ENOMEM;
1551
1552         /* some devices are ignored */
1553         if (id->driver_info == DEVTYPE_IGNORE)
1554                 return -ENODEV;
1555
1556         endpoint = usbtouch_get_input_endpoint(intf->cur_altsetting);
1557         if (!endpoint)
1558                 return -ENXIO;
1559
1560         usbtouch = kzalloc(sizeof(struct usbtouch_usb), GFP_KERNEL);
1561         input_dev = input_allocate_device();
1562         if (!usbtouch || !input_dev)
1563                 goto out_free;
1564
1565         type = &usbtouch_dev_info[id->driver_info];
1566         usbtouch->type = type;
1567         if (!type->process_pkt)
1568                 type->process_pkt = usbtouch_process_pkt;
1569
1570         usbtouch->data_size = type->rept_size;
1571         if (type->get_pkt_len) {
1572                 /*
1573                  * When dealing with variable-length packets we should
1574                  * not request more than wMaxPacketSize bytes at once
1575                  * as we do not know if there is more data coming or
1576                  * we filled exactly wMaxPacketSize bytes and there is
1577                  * nothing else.
1578                  */
1579                 usbtouch->data_size = min(usbtouch->data_size,
1580                                           usb_endpoint_maxp(endpoint));
1581         }
1582
1583         usbtouch->data = usb_alloc_coherent(udev, usbtouch->data_size,
1584                                             GFP_KERNEL, &usbtouch->data_dma);
1585         if (!usbtouch->data)
1586                 goto out_free;
1587
1588         if (type->get_pkt_len) {
1589                 usbtouch->buffer = kmalloc(type->rept_size, GFP_KERNEL);
1590                 if (!usbtouch->buffer)
1591                         goto out_free_buffers;
1592         }
1593
1594         usbtouch->irq = usb_alloc_urb(0, GFP_KERNEL);
1595         if (!usbtouch->irq) {
1596                 dev_dbg(&intf->dev,
1597                         "%s - usb_alloc_urb failed: usbtouch->irq\n", __func__);
1598                 goto out_free_buffers;
1599         }
1600
1601         usbtouch->interface = intf;
1602         usbtouch->input = input_dev;
1603
1604         if (udev->manufacturer)
1605                 strlcpy(usbtouch->name, udev->manufacturer, sizeof(usbtouch->name));
1606
1607         if (udev->product) {
1608                 if (udev->manufacturer)
1609                         strlcat(usbtouch->name, " ", sizeof(usbtouch->name));
1610                 strlcat(usbtouch->name, udev->product, sizeof(usbtouch->name));
1611         }
1612
1613         if (!strlen(usbtouch->name))
1614                 snprintf(usbtouch->name, sizeof(usbtouch->name),
1615                         "USB Touchscreen %04x:%04x",
1616                          le16_to_cpu(udev->descriptor.idVendor),
1617                          le16_to_cpu(udev->descriptor.idProduct));
1618
1619         usb_make_path(udev, usbtouch->phys, sizeof(usbtouch->phys));
1620         strlcat(usbtouch->phys, "/input0", sizeof(usbtouch->phys));
1621
1622         input_dev->name = usbtouch->name;
1623         input_dev->phys = usbtouch->phys;
1624         usb_to_input_id(udev, &input_dev->id);
1625         input_dev->dev.parent = &intf->dev;
1626
1627         input_set_drvdata(input_dev, usbtouch);
1628
1629         input_dev->open = usbtouch_open;
1630         input_dev->close = usbtouch_close;
1631
1632         input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
1633         input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
1634         input_set_abs_params(input_dev, ABS_X, type->min_xc, type->max_xc, 0, 0);
1635         input_set_abs_params(input_dev, ABS_Y, type->min_yc, type->max_yc, 0, 0);
1636         if (type->max_press)
1637                 input_set_abs_params(input_dev, ABS_PRESSURE, type->min_press,
1638                                      type->max_press, 0, 0);
1639
1640         if (usb_endpoint_type(endpoint) == USB_ENDPOINT_XFER_INT)
1641                 usb_fill_int_urb(usbtouch->irq, udev,
1642                          usb_rcvintpipe(udev, endpoint->bEndpointAddress),
1643                          usbtouch->data, usbtouch->data_size,
1644                          usbtouch_irq, usbtouch, endpoint->bInterval);
1645         else
1646                 usb_fill_bulk_urb(usbtouch->irq, udev,
1647                          usb_rcvbulkpipe(udev, endpoint->bEndpointAddress),
1648                          usbtouch->data, usbtouch->data_size,
1649                          usbtouch_irq, usbtouch);
1650
1651         usbtouch->irq->dev = udev;
1652         usbtouch->irq->transfer_dma = usbtouch->data_dma;
1653         usbtouch->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1654
1655         /* device specific allocations */
1656         if (type->alloc) {
1657                 err = type->alloc(usbtouch);
1658                 if (err) {
1659                         dev_dbg(&intf->dev,
1660                                 "%s - type->alloc() failed, err: %d\n",
1661                                 __func__, err);
1662                         goto out_free_urb;
1663                 }
1664         }
1665
1666         /* device specific initialisation*/
1667         if (type->init) {
1668                 err = type->init(usbtouch);
1669                 if (err) {
1670                         dev_dbg(&intf->dev,
1671                                 "%s - type->init() failed, err: %d\n",
1672                                 __func__, err);
1673                         goto out_do_exit;
1674                 }
1675         }
1676
1677         err = input_register_device(usbtouch->input);
1678         if (err) {
1679                 dev_dbg(&intf->dev,
1680                         "%s - input_register_device failed, err: %d\n",
1681                         __func__, err);
1682                 goto out_do_exit;
1683         }
1684
1685         usb_set_intfdata(intf, usbtouch);
1686
1687         if (usbtouch->type->irq_always) {
1688                 /* this can't fail */
1689                 usb_autopm_get_interface(intf);
1690                 err = usb_submit_urb(usbtouch->irq, GFP_KERNEL);
1691                 if (err) {
1692                         usb_autopm_put_interface(intf);
1693                         dev_err(&intf->dev,
1694                                 "%s - usb_submit_urb failed with result: %d\n",
1695                                 __func__, err);
1696                         goto out_unregister_input;
1697                 }
1698         }
1699
1700         return 0;
1701
1702 out_unregister_input:
1703         input_unregister_device(input_dev);
1704         input_dev = NULL;
1705 out_do_exit:
1706         if (type->exit)
1707                 type->exit(usbtouch);
1708 out_free_urb:
1709         usb_free_urb(usbtouch->irq);
1710 out_free_buffers:
1711         usbtouch_free_buffers(udev, usbtouch);
1712 out_free:
1713         input_free_device(input_dev);
1714         kfree(usbtouch);
1715         return err;
1716 }
1717
1718 static void usbtouch_disconnect(struct usb_interface *intf)
1719 {
1720         struct usbtouch_usb *usbtouch = usb_get_intfdata(intf);
1721
1722         if (!usbtouch)
1723                 return;
1724
1725         dev_dbg(&intf->dev,
1726                 "%s - usbtouch is initialized, cleaning up\n", __func__);
1727
1728         usb_set_intfdata(intf, NULL);
1729         /* this will stop IO via close */
1730         input_unregister_device(usbtouch->input);
1731         usb_free_urb(usbtouch->irq);
1732         if (usbtouch->type->exit)
1733                 usbtouch->type->exit(usbtouch);
1734         usbtouch_free_buffers(interface_to_usbdev(intf), usbtouch);
1735         kfree(usbtouch);
1736 }
1737
1738 MODULE_DEVICE_TABLE(usb, usbtouch_devices);
1739
1740 static struct usb_driver usbtouch_driver = {
1741         .name           = "usbtouchscreen",
1742         .probe          = usbtouch_probe,
1743         .disconnect     = usbtouch_disconnect,
1744         .suspend        = usbtouch_suspend,
1745         .resume         = usbtouch_resume,
1746         .reset_resume   = usbtouch_reset_resume,
1747         .id_table       = usbtouch_devices,
1748         .supports_autosuspend = 1,
1749 };
1750
1751 module_usb_driver(usbtouch_driver);
1752
1753 MODULE_AUTHOR(DRIVER_AUTHOR);
1754 MODULE_DESCRIPTION(DRIVER_DESC);
1755 MODULE_LICENSE("GPL");
1756
1757 MODULE_ALIAS("touchkitusb");
1758 MODULE_ALIAS("itmtouch");
1759 MODULE_ALIAS("mtouchusb");