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