Input: usbtouchscreen - add support for e2i touchscreen controller
[linux-2.6.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  *
18  * Copyright (C) 2004-2007 by Daniel Ritz <daniel.ritz@gmx.ch>
19  * Copyright (C) by Todd E. Johnson (mtouchusb.c)
20  *
21  * This program is free software; you can redistribute it and/or
22  * modify it under the terms of the GNU General Public License as
23  * published by the Free Software Foundation; either version 2 of the
24  * License, or (at your option) any later version.
25  *
26  * This program is distributed in the hope that it will be useful, but
27  * WITHOUT ANY WARRANTY; without even the implied warranty of
28  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
29  * General Public License for more details.
30  *
31  * You should have received a copy of the GNU General Public License
32  * along with this program; if not, write to the Free Software
33  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
34  *
35  * Driver is based on touchkitusb.c
36  * - ITM parts are from itmtouch.c
37  * - 3M parts are from mtouchusb.c
38  * - PanJit parts are from an unmerged driver by Lanslott Gish
39  * - DMC TSC 10/25 are from Holger Schurig, with ideas from an unmerged
40  *   driver from Marius Vollmer
41  *
42  *****************************************************************************/
43
44 //#define DEBUG
45
46 #include <linux/kernel.h>
47 #include <linux/slab.h>
48 #include <linux/input.h>
49 #include <linux/module.h>
50 #include <linux/init.h>
51 #include <linux/usb.h>
52 #include <linux/usb/input.h>
53 #include <linux/hid.h>
54
55
56 #define DRIVER_VERSION          "v0.6"
57 #define DRIVER_AUTHOR           "Daniel Ritz <daniel.ritz@gmx.ch>"
58 #define DRIVER_DESC             "USB Touchscreen Driver"
59
60 static int swap_xy;
61 module_param(swap_xy, bool, 0644);
62 MODULE_PARM_DESC(swap_xy, "If set X and Y axes are swapped.");
63
64 static int hwcalib_xy;
65 module_param(hwcalib_xy, bool, 0644);
66 MODULE_PARM_DESC(hwcalib_xy, "If set hw-calibrated X/Y are used if available");
67
68 /* device specifc data/functions */
69 struct usbtouch_usb;
70 struct usbtouch_device_info {
71         int min_xc, max_xc;
72         int min_yc, max_yc;
73         int min_press, max_press;
74         int rept_size;
75
76         void (*process_pkt) (struct usbtouch_usb *usbtouch, unsigned char *pkt, int len);
77
78         /*
79          * used to get the packet len. possible return values:
80          * > 0: packet len
81          * = 0: skip one byte
82          * < 0: -return value more bytes needed
83          */
84         int  (*get_pkt_len) (unsigned char *pkt, int len);
85
86         int  (*read_data)   (struct usbtouch_usb *usbtouch, unsigned char *pkt);
87         int  (*init)        (struct usbtouch_usb *usbtouch);
88 };
89
90 /* a usbtouch device */
91 struct usbtouch_usb {
92         unsigned char *data;
93         dma_addr_t data_dma;
94         unsigned char *buffer;
95         int buf_len;
96         struct urb *irq;
97         struct usb_device *udev;
98         struct input_dev *input;
99         struct usbtouch_device_info *type;
100         char name[128];
101         char phys[64];
102
103         int x, y;
104         int touch, press;
105 };
106
107
108 /* device types */
109 enum {
110         DEVTYPE_IGNORE = -1,
111         DEVTYPE_EGALAX,
112         DEVTYPE_PANJIT,
113         DEVTYPE_3M,
114         DEVTYPE_ITM,
115         DEVTYPE_ETURBO,
116         DEVTYPE_GUNZE,
117         DEVTYPE_DMC_TSC10,
118         DEVTYPE_IRTOUCH,
119         DEVTYPE_IDEALTEK,
120         DEVTYPE_GENERAL_TOUCH,
121         DEVTYPE_GOTOP,
122         DEVTYPE_JASTEC,
123         DEVTYPE_E2I,
124 };
125
126 #define USB_DEVICE_HID_CLASS(vend, prod) \
127         .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS \
128                 | USB_DEVICE_ID_MATCH_INT_PROTOCOL \
129                 | USB_DEVICE_ID_MATCH_DEVICE, \
130         .idVendor = (vend), \
131         .idProduct = (prod), \
132         .bInterfaceClass = USB_INTERFACE_CLASS_HID, \
133         .bInterfaceProtocol = USB_INTERFACE_PROTOCOL_MOUSE
134
135 static struct usb_device_id usbtouch_devices[] = {
136 #ifdef CONFIG_TOUCHSCREEN_USB_EGALAX
137         /* ignore the HID capable devices, handled by usbhid */
138         {USB_DEVICE_HID_CLASS(0x0eef, 0x0001), .driver_info = DEVTYPE_IGNORE},
139         {USB_DEVICE_HID_CLASS(0x0eef, 0x0002), .driver_info = DEVTYPE_IGNORE},
140
141         /* normal device IDs */
142         {USB_DEVICE(0x3823, 0x0001), .driver_info = DEVTYPE_EGALAX},
143         {USB_DEVICE(0x3823, 0x0002), .driver_info = DEVTYPE_EGALAX},
144         {USB_DEVICE(0x0123, 0x0001), .driver_info = DEVTYPE_EGALAX},
145         {USB_DEVICE(0x0eef, 0x0001), .driver_info = DEVTYPE_EGALAX},
146         {USB_DEVICE(0x0eef, 0x0002), .driver_info = DEVTYPE_EGALAX},
147         {USB_DEVICE(0x1234, 0x0001), .driver_info = DEVTYPE_EGALAX},
148         {USB_DEVICE(0x1234, 0x0002), .driver_info = DEVTYPE_EGALAX},
149 #endif
150
151 #ifdef CONFIG_TOUCHSCREEN_USB_PANJIT
152         {USB_DEVICE(0x134c, 0x0001), .driver_info = DEVTYPE_PANJIT},
153         {USB_DEVICE(0x134c, 0x0002), .driver_info = DEVTYPE_PANJIT},
154         {USB_DEVICE(0x134c, 0x0003), .driver_info = DEVTYPE_PANJIT},
155         {USB_DEVICE(0x134c, 0x0004), .driver_info = DEVTYPE_PANJIT},
156 #endif
157
158 #ifdef CONFIG_TOUCHSCREEN_USB_3M
159         {USB_DEVICE(0x0596, 0x0001), .driver_info = DEVTYPE_3M},
160 #endif
161
162 #ifdef CONFIG_TOUCHSCREEN_USB_ITM
163         {USB_DEVICE(0x0403, 0xf9e9), .driver_info = DEVTYPE_ITM},
164 #endif
165
166 #ifdef CONFIG_TOUCHSCREEN_USB_ETURBO
167         {USB_DEVICE(0x1234, 0x5678), .driver_info = DEVTYPE_ETURBO},
168 #endif
169
170 #ifdef CONFIG_TOUCHSCREEN_USB_GUNZE
171         {USB_DEVICE(0x0637, 0x0001), .driver_info = DEVTYPE_GUNZE},
172 #endif
173
174 #ifdef CONFIG_TOUCHSCREEN_USB_DMC_TSC10
175         {USB_DEVICE(0x0afa, 0x03e8), .driver_info = DEVTYPE_DMC_TSC10},
176 #endif
177
178 #ifdef CONFIG_TOUCHSCREEN_USB_IRTOUCH
179         {USB_DEVICE(0x595a, 0x0001), .driver_info = DEVTYPE_IRTOUCH},
180         {USB_DEVICE(0x6615, 0x0001), .driver_info = DEVTYPE_IRTOUCH},
181 #endif
182
183 #ifdef CONFIG_TOUCHSCREEN_USB_IDEALTEK
184         {USB_DEVICE(0x1391, 0x1000), .driver_info = DEVTYPE_IDEALTEK},
185 #endif
186
187 #ifdef CONFIG_TOUCHSCREEN_USB_GENERAL_TOUCH
188         {USB_DEVICE(0x0dfc, 0x0001), .driver_info = DEVTYPE_GENERAL_TOUCH},
189 #endif
190
191 #ifdef CONFIG_TOUCHSCREEN_USB_GOTOP
192         {USB_DEVICE(0x08f2, 0x007f), .driver_info = DEVTYPE_GOTOP},
193         {USB_DEVICE(0x08f2, 0x00ce), .driver_info = DEVTYPE_GOTOP},
194         {USB_DEVICE(0x08f2, 0x00f4), .driver_info = DEVTYPE_GOTOP},
195 #endif
196
197 #ifdef CONFIG_TOUCHSCREEN_USB_JASTEC
198         {USB_DEVICE(0x0f92, 0x0001), .driver_info = DEVTYPE_JASTEC},
199 #endif
200
201 #ifdef CONFIG_TOUCHSCREEN_USB_E2I
202         {USB_DEVICE(0x1ac7, 0x0001), .driver_info = DEVTYPE_E2I},
203 #endif
204         {}
205 };
206
207
208 /*****************************************************************************
209  * e2i Part
210  */
211
212 #ifdef CONFIG_TOUCHSCREEN_USB_E2I
213 static int e2i_init(struct usbtouch_usb *usbtouch)
214 {
215         int ret;
216
217         ret = usb_control_msg(usbtouch->udev, usb_rcvctrlpipe(usbtouch->udev, 0),
218                               0x01, 0x02, 0x0000, 0x0081,
219                               NULL, 0, USB_CTRL_SET_TIMEOUT);
220
221         dbg("%s - usb_control_msg - E2I_RESET - bytes|err: %d",
222             __func__, ret);
223         return ret;
224 }
225
226 static int e2i_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
227 {
228         int tmp = (pkt[0] << 8) | pkt[1];
229         dev->x  = (pkt[2] << 8) | pkt[3];
230         dev->y  = (pkt[4] << 8) | pkt[5];
231
232         tmp = tmp - 0xA000;
233         dev->touch = (tmp > 0);
234         dev->press = (tmp > 0 ? tmp : 0);
235
236         return 1;
237 }
238 #endif
239
240
241 /*****************************************************************************
242  * eGalax part
243  */
244
245 #ifdef CONFIG_TOUCHSCREEN_USB_EGALAX
246
247 #ifndef MULTI_PACKET
248 #define MULTI_PACKET
249 #endif
250
251 #define EGALAX_PKT_TYPE_MASK            0xFE
252 #define EGALAX_PKT_TYPE_REPT            0x80
253 #define EGALAX_PKT_TYPE_DIAG            0x0A
254
255 static int egalax_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
256 {
257         if ((pkt[0] & EGALAX_PKT_TYPE_MASK) != EGALAX_PKT_TYPE_REPT)
258                 return 0;
259
260         dev->x = ((pkt[3] & 0x0F) << 7) | (pkt[4] & 0x7F);
261         dev->y = ((pkt[1] & 0x0F) << 7) | (pkt[2] & 0x7F);
262         dev->touch = pkt[0] & 0x01;
263
264         return 1;
265 }
266
267 static int egalax_get_pkt_len(unsigned char *buf, int len)
268 {
269         switch (buf[0] & EGALAX_PKT_TYPE_MASK) {
270         case EGALAX_PKT_TYPE_REPT:
271                 return 5;
272
273         case EGALAX_PKT_TYPE_DIAG:
274                 if (len < 2)
275                         return -1;
276
277                 return buf[1] + 2;
278         }
279
280         return 0;
281 }
282 #endif
283
284
285 /*****************************************************************************
286  * PanJit Part
287  */
288 #ifdef CONFIG_TOUCHSCREEN_USB_PANJIT
289 static int panjit_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
290 {
291         dev->x = ((pkt[2] & 0x0F) << 8) | pkt[1];
292         dev->y = ((pkt[4] & 0x0F) << 8) | pkt[3];
293         dev->touch = pkt[0] & 0x01;
294
295         return 1;
296 }
297 #endif
298
299
300 /*****************************************************************************
301  * 3M/Microtouch Part
302  */
303 #ifdef CONFIG_TOUCHSCREEN_USB_3M
304
305 #define MTOUCHUSB_ASYNC_REPORT          1
306 #define MTOUCHUSB_RESET                 7
307 #define MTOUCHUSB_REQ_CTRLLR_ID         10
308
309 static int mtouch_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
310 {
311         if (hwcalib_xy) {
312                 dev->x = (pkt[4] << 8) | pkt[3];
313                 dev->y = 0xffff - ((pkt[6] << 8) | pkt[5]);
314         } else {
315                 dev->x = (pkt[8] << 8) | pkt[7];
316                 dev->y = (pkt[10] << 8) | pkt[9];
317         }
318         dev->touch = (pkt[2] & 0x40) ? 1 : 0;
319
320         return 1;
321 }
322
323 static int mtouch_init(struct usbtouch_usb *usbtouch)
324 {
325         int ret, i;
326
327         ret = usb_control_msg(usbtouch->udev, usb_rcvctrlpipe(usbtouch->udev, 0),
328                               MTOUCHUSB_RESET,
329                               USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
330                               1, 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
331         dbg("%s - usb_control_msg - MTOUCHUSB_RESET - bytes|err: %d",
332             __func__, ret);
333         if (ret < 0)
334                 return ret;
335         msleep(150);
336
337         for (i = 0; i < 3; i++) {
338                 ret = usb_control_msg(usbtouch->udev, usb_rcvctrlpipe(usbtouch->udev, 0),
339                                       MTOUCHUSB_ASYNC_REPORT,
340                                       USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
341                                       1, 1, NULL, 0, USB_CTRL_SET_TIMEOUT);
342                 dbg("%s - usb_control_msg - MTOUCHUSB_ASYNC_REPORT - bytes|err: %d",
343                     __func__, ret);
344                 if (ret >= 0)
345                         break;
346                 if (ret != -EPIPE)
347                         return ret;
348         }
349
350         /* Default min/max xy are the raw values, override if using hw-calib */
351         if (hwcalib_xy) {
352                 input_set_abs_params(usbtouch->input, ABS_X, 0, 0xffff, 0, 0);
353                 input_set_abs_params(usbtouch->input, ABS_Y, 0, 0xffff, 0, 0);
354         }
355
356         return 0;
357 }
358 #endif
359
360
361 /*****************************************************************************
362  * ITM Part
363  */
364 #ifdef CONFIG_TOUCHSCREEN_USB_ITM
365 static int itm_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
366 {
367         int touch;
368         /*
369          * ITM devices report invalid x/y data if not touched.
370          * if the screen was touched before but is not touched any more
371          * report touch as 0 with the last valid x/y data once. then stop
372          * reporting data until touched again.
373          */
374         dev->press = ((pkt[2] & 0x01) << 7) | (pkt[5] & 0x7F);
375
376         touch = ~pkt[7] & 0x20;
377         if (!touch) {
378                 if (dev->touch) {
379                         dev->touch = 0;
380                         return 1;
381                 }
382
383                 return 0;
384         }
385
386         dev->x = ((pkt[0] & 0x1F) << 7) | (pkt[3] & 0x7F);
387         dev->y = ((pkt[1] & 0x1F) << 7) | (pkt[4] & 0x7F);
388         dev->touch = touch;
389
390         return 1;
391 }
392 #endif
393
394
395 /*****************************************************************************
396  * eTurboTouch part
397  */
398 #ifdef CONFIG_TOUCHSCREEN_USB_ETURBO
399 #ifndef MULTI_PACKET
400 #define MULTI_PACKET
401 #endif
402 static int eturbo_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
403 {
404         unsigned int shift;
405
406         /* packets should start with sync */
407         if (!(pkt[0] & 0x80))
408                 return 0;
409
410         shift = (6 - (pkt[0] & 0x03));
411         dev->x = ((pkt[3] << 7) | pkt[4]) >> shift;
412         dev->y = ((pkt[1] << 7) | pkt[2]) >> shift;
413         dev->touch = (pkt[0] & 0x10) ? 1 : 0;
414
415         return 1;
416 }
417
418 static int eturbo_get_pkt_len(unsigned char *buf, int len)
419 {
420         if (buf[0] & 0x80)
421                 return 5;
422         if (buf[0] == 0x01)
423                 return 3;
424         return 0;
425 }
426 #endif
427
428
429 /*****************************************************************************
430  * Gunze part
431  */
432 #ifdef CONFIG_TOUCHSCREEN_USB_GUNZE
433 static int gunze_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
434 {
435         if (!(pkt[0] & 0x80) || ((pkt[1] | pkt[2] | pkt[3]) & 0x80))
436                 return 0;
437
438         dev->x = ((pkt[0] & 0x1F) << 7) | (pkt[2] & 0x7F);
439         dev->y = ((pkt[1] & 0x1F) << 7) | (pkt[3] & 0x7F);
440         dev->touch = pkt[0] & 0x20;
441
442         return 1;
443 }
444 #endif
445
446 /*****************************************************************************
447  * DMC TSC-10/25 Part
448  *
449  * Documentation about the controller and it's protocol can be found at
450  *   http://www.dmccoltd.com/files/controler/tsc10usb_pi_e.pdf
451  *   http://www.dmccoltd.com/files/controler/tsc25_usb_e.pdf
452  */
453 #ifdef CONFIG_TOUCHSCREEN_USB_DMC_TSC10
454
455 /* supported data rates. currently using 130 */
456 #define TSC10_RATE_POINT        0x50
457 #define TSC10_RATE_30           0x40
458 #define TSC10_RATE_50           0x41
459 #define TSC10_RATE_80           0x42
460 #define TSC10_RATE_100          0x43
461 #define TSC10_RATE_130          0x44
462 #define TSC10_RATE_150          0x45
463
464 /* commands */
465 #define TSC10_CMD_RESET         0x55
466 #define TSC10_CMD_RATE          0x05
467 #define TSC10_CMD_DATA1         0x01
468
469 static int dmc_tsc10_init(struct usbtouch_usb *usbtouch)
470 {
471         struct usb_device *dev = usbtouch->udev;
472         int ret = -ENOMEM;
473         unsigned char *buf;
474
475         buf = kmalloc(2, GFP_KERNEL);
476         if (!buf)
477                 goto err_nobuf;
478         /* reset */
479         buf[0] = buf[1] = 0xFF;
480         ret = usb_control_msg(dev, usb_rcvctrlpipe (dev, 0),
481                               TSC10_CMD_RESET,
482                               USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
483                               0, 0, buf, 2, USB_CTRL_SET_TIMEOUT);
484         if (ret < 0)
485                 goto err_out;
486         if (buf[0] != 0x06) {
487                 ret = -ENODEV;
488                 goto err_out;
489         }
490
491         /* set coordinate output rate */
492         buf[0] = buf[1] = 0xFF;
493         ret = usb_control_msg(dev, usb_rcvctrlpipe (dev, 0),
494                               TSC10_CMD_RATE,
495                               USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
496                               TSC10_RATE_150, 0, buf, 2, USB_CTRL_SET_TIMEOUT);
497         if (ret < 0)
498                 goto err_out;
499         if ((buf[0] != 0x06) && (buf[0] != 0x15 || buf[1] != 0x01)) {
500                 ret = -ENODEV;
501                 goto err_out;
502         }
503
504         /* start sending data */
505         ret = usb_control_msg(dev, usb_rcvctrlpipe (dev, 0),
506                               TSC10_CMD_DATA1,
507                               USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
508                               0, 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
509 err_out:
510         kfree(buf);
511 err_nobuf:
512         return ret;
513 }
514
515
516 static int dmc_tsc10_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
517 {
518         dev->x = ((pkt[2] & 0x03) << 8) | pkt[1];
519         dev->y = ((pkt[4] & 0x03) << 8) | pkt[3];
520         dev->touch = pkt[0] & 0x01;
521
522         return 1;
523 }
524 #endif
525
526
527 /*****************************************************************************
528  * IRTOUCH Part
529  */
530 #ifdef CONFIG_TOUCHSCREEN_USB_IRTOUCH
531 static int irtouch_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
532 {
533         dev->x = (pkt[3] << 8) | pkt[2];
534         dev->y = (pkt[5] << 8) | pkt[4];
535         dev->touch = (pkt[1] & 0x03) ? 1 : 0;
536
537         return 1;
538 }
539 #endif
540
541
542 /*****************************************************************************
543  * IdealTEK URTC1000 Part
544  */
545 #ifdef CONFIG_TOUCHSCREEN_USB_IDEALTEK
546 #ifndef MULTI_PACKET
547 #define MULTI_PACKET
548 #endif
549 static int idealtek_get_pkt_len(unsigned char *buf, int len)
550 {
551         if (buf[0] & 0x80)
552                 return 5;
553         if (buf[0] == 0x01)
554                 return len;
555         return 0;
556 }
557
558 static int idealtek_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
559 {
560         switch (pkt[0] & 0x98) {
561         case 0x88:
562                 /* touch data in IdealTEK mode */
563                 dev->x = (pkt[1] << 5) | (pkt[2] >> 2);
564                 dev->y = (pkt[3] << 5) | (pkt[4] >> 2);
565                 dev->touch = (pkt[0] & 0x40) ? 1 : 0;
566                 return 1;
567
568         case 0x98:
569                 /* touch data in MT emulation mode */
570                 dev->x = (pkt[2] << 5) | (pkt[1] >> 2);
571                 dev->y = (pkt[4] << 5) | (pkt[3] >> 2);
572                 dev->touch = (pkt[0] & 0x40) ? 1 : 0;
573                 return 1;
574
575         default:
576                 return 0;
577         }
578 }
579 #endif
580
581 /*****************************************************************************
582  * General Touch Part
583  */
584 #ifdef CONFIG_TOUCHSCREEN_USB_GENERAL_TOUCH
585 static int general_touch_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
586 {
587         dev->x = ((pkt[2] & 0x0F) << 8) | pkt[1] ;
588         dev->y = ((pkt[4] & 0x0F) << 8) | pkt[3] ;
589         dev->press = pkt[5] & 0xff;
590         dev->touch = pkt[0] & 0x01;
591
592         return 1;
593 }
594 #endif
595
596 /*****************************************************************************
597  * GoTop Part
598  */
599 #ifdef CONFIG_TOUCHSCREEN_USB_GOTOP
600 static int gotop_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
601 {
602         dev->x = ((pkt[1] & 0x38) << 4) | pkt[2];
603         dev->y = ((pkt[1] & 0x07) << 7) | pkt[3];
604         dev->touch = pkt[0] & 0x01;
605
606         return 1;
607 }
608 #endif
609
610 /*****************************************************************************
611  * JASTEC Part
612  */
613 #ifdef CONFIG_TOUCHSCREEN_USB_JASTEC
614 static int jastec_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
615 {
616         dev->x = ((pkt[0] & 0x3f) << 6) | (pkt[2] & 0x3f);
617         dev->y = ((pkt[1] & 0x3f) << 6) | (pkt[3] & 0x3f);
618         dev->touch = (pkt[0] & 0x40) >> 6;
619
620         return 1;
621 }
622 #endif
623
624
625 /*****************************************************************************
626  * the different device descriptors
627  */
628 #ifdef MULTI_PACKET
629 static void usbtouch_process_multi(struct usbtouch_usb *usbtouch,
630                                    unsigned char *pkt, int len);
631 #endif
632
633 static struct usbtouch_device_info usbtouch_dev_info[] = {
634 #ifdef CONFIG_TOUCHSCREEN_USB_EGALAX
635         [DEVTYPE_EGALAX] = {
636                 .min_xc         = 0x0,
637                 .max_xc         = 0x07ff,
638                 .min_yc         = 0x0,
639                 .max_yc         = 0x07ff,
640                 .rept_size      = 16,
641                 .process_pkt    = usbtouch_process_multi,
642                 .get_pkt_len    = egalax_get_pkt_len,
643                 .read_data      = egalax_read_data,
644         },
645 #endif
646
647 #ifdef CONFIG_TOUCHSCREEN_USB_PANJIT
648         [DEVTYPE_PANJIT] = {
649                 .min_xc         = 0x0,
650                 .max_xc         = 0x0fff,
651                 .min_yc         = 0x0,
652                 .max_yc         = 0x0fff,
653                 .rept_size      = 8,
654                 .read_data      = panjit_read_data,
655         },
656 #endif
657
658 #ifdef CONFIG_TOUCHSCREEN_USB_3M
659         [DEVTYPE_3M] = {
660                 .min_xc         = 0x0,
661                 .max_xc         = 0x4000,
662                 .min_yc         = 0x0,
663                 .max_yc         = 0x4000,
664                 .rept_size      = 11,
665                 .read_data      = mtouch_read_data,
666                 .init           = mtouch_init,
667         },
668 #endif
669
670 #ifdef CONFIG_TOUCHSCREEN_USB_ITM
671         [DEVTYPE_ITM] = {
672                 .min_xc         = 0x0,
673                 .max_xc         = 0x0fff,
674                 .min_yc         = 0x0,
675                 .max_yc         = 0x0fff,
676                 .max_press      = 0xff,
677                 .rept_size      = 8,
678                 .read_data      = itm_read_data,
679         },
680 #endif
681
682 #ifdef CONFIG_TOUCHSCREEN_USB_ETURBO
683         [DEVTYPE_ETURBO] = {
684                 .min_xc         = 0x0,
685                 .max_xc         = 0x07ff,
686                 .min_yc         = 0x0,
687                 .max_yc         = 0x07ff,
688                 .rept_size      = 8,
689                 .process_pkt    = usbtouch_process_multi,
690                 .get_pkt_len    = eturbo_get_pkt_len,
691                 .read_data      = eturbo_read_data,
692         },
693 #endif
694
695 #ifdef CONFIG_TOUCHSCREEN_USB_GUNZE
696         [DEVTYPE_GUNZE] = {
697                 .min_xc         = 0x0,
698                 .max_xc         = 0x0fff,
699                 .min_yc         = 0x0,
700                 .max_yc         = 0x0fff,
701                 .rept_size      = 4,
702                 .read_data      = gunze_read_data,
703         },
704 #endif
705
706 #ifdef CONFIG_TOUCHSCREEN_USB_DMC_TSC10
707         [DEVTYPE_DMC_TSC10] = {
708                 .min_xc         = 0x0,
709                 .max_xc         = 0x03ff,
710                 .min_yc         = 0x0,
711                 .max_yc         = 0x03ff,
712                 .rept_size      = 5,
713                 .init           = dmc_tsc10_init,
714                 .read_data      = dmc_tsc10_read_data,
715         },
716 #endif
717
718 #ifdef CONFIG_TOUCHSCREEN_USB_IRTOUCH
719         [DEVTYPE_IRTOUCH] = {
720                 .min_xc         = 0x0,
721                 .max_xc         = 0x0fff,
722                 .min_yc         = 0x0,
723                 .max_yc         = 0x0fff,
724                 .rept_size      = 8,
725                 .read_data      = irtouch_read_data,
726         },
727 #endif
728
729 #ifdef CONFIG_TOUCHSCREEN_USB_IDEALTEK
730         [DEVTYPE_IDEALTEK] = {
731                 .min_xc         = 0x0,
732                 .max_xc         = 0x0fff,
733                 .min_yc         = 0x0,
734                 .max_yc         = 0x0fff,
735                 .rept_size      = 8,
736                 .process_pkt    = usbtouch_process_multi,
737                 .get_pkt_len    = idealtek_get_pkt_len,
738                 .read_data      = idealtek_read_data,
739         },
740 #endif
741
742 #ifdef CONFIG_TOUCHSCREEN_USB_GENERAL_TOUCH
743         [DEVTYPE_GENERAL_TOUCH] = {
744                 .min_xc         = 0x0,
745                 .max_xc         = 0x0500,
746                 .min_yc         = 0x0,
747                 .max_yc         = 0x0500,
748                 .rept_size      = 7,
749                 .read_data      = general_touch_read_data,
750         },
751 #endif
752
753 #ifdef CONFIG_TOUCHSCREEN_USB_GOTOP
754         [DEVTYPE_GOTOP] = {
755                 .min_xc         = 0x0,
756                 .max_xc         = 0x03ff,
757                 .min_yc         = 0x0,
758                 .max_yc         = 0x03ff,
759                 .rept_size      = 4,
760                 .read_data      = gotop_read_data,
761         },
762 #endif
763
764 #ifdef CONFIG_TOUCHSCREEN_USB_JASTEC
765         [DEVTYPE_JASTEC] = {
766                 .min_xc         = 0x0,
767                 .max_xc         = 0x0fff,
768                 .min_yc         = 0x0,
769                 .max_yc         = 0x0fff,
770                 .rept_size      = 4,
771                 .read_data      = jastec_read_data,
772         },
773 #endif
774
775 #ifdef CONFIG_TOUCHSCREEN_USB_E2I
776         [DEVTYPE_E2I] = {
777                 .min_xc         = 0x0,
778                 .max_xc         = 0x7fff,
779                 .min_yc         = 0x0,
780                 .max_yc         = 0x7fff,
781                 .rept_size      = 6,
782                 .init           = e2i_init,
783                 .read_data      = e2i_read_data,
784         },
785 #endif
786 };
787
788
789 /*****************************************************************************
790  * Generic Part
791  */
792 static void usbtouch_process_pkt(struct usbtouch_usb *usbtouch,
793                                  unsigned char *pkt, int len)
794 {
795         struct usbtouch_device_info *type = usbtouch->type;
796
797         if (!type->read_data(usbtouch, pkt))
798                         return;
799
800         input_report_key(usbtouch->input, BTN_TOUCH, usbtouch->touch);
801
802         if (swap_xy) {
803                 input_report_abs(usbtouch->input, ABS_X, usbtouch->y);
804                 input_report_abs(usbtouch->input, ABS_Y, usbtouch->x);
805         } else {
806                 input_report_abs(usbtouch->input, ABS_X, usbtouch->x);
807                 input_report_abs(usbtouch->input, ABS_Y, usbtouch->y);
808         }
809         if (type->max_press)
810                 input_report_abs(usbtouch->input, ABS_PRESSURE, usbtouch->press);
811         input_sync(usbtouch->input);
812 }
813
814
815 #ifdef MULTI_PACKET
816 static void usbtouch_process_multi(struct usbtouch_usb *usbtouch,
817                                    unsigned char *pkt, int len)
818 {
819         unsigned char *buffer;
820         int pkt_len, pos, buf_len, tmp;
821
822         /* process buffer */
823         if (unlikely(usbtouch->buf_len)) {
824                 /* try to get size */
825                 pkt_len = usbtouch->type->get_pkt_len(
826                                 usbtouch->buffer, usbtouch->buf_len);
827
828                 /* drop? */
829                 if (unlikely(!pkt_len))
830                         goto out_flush_buf;
831
832                 /* need to append -pkt_len bytes before able to get size */
833                 if (unlikely(pkt_len < 0)) {
834                         int append = -pkt_len;
835                         if (unlikely(append > len))
836                                append = len;
837                         if (usbtouch->buf_len + append >= usbtouch->type->rept_size)
838                                 goto out_flush_buf;
839                         memcpy(usbtouch->buffer + usbtouch->buf_len, pkt, append);
840                         usbtouch->buf_len += append;
841
842                         pkt_len = usbtouch->type->get_pkt_len(
843                                         usbtouch->buffer, usbtouch->buf_len);
844                         if (pkt_len < 0)
845                                 return;
846                 }
847
848                 /* append */
849                 tmp = pkt_len - usbtouch->buf_len;
850                 if (usbtouch->buf_len + tmp >= usbtouch->type->rept_size)
851                         goto out_flush_buf;
852                 memcpy(usbtouch->buffer + usbtouch->buf_len, pkt, tmp);
853                 usbtouch_process_pkt(usbtouch, usbtouch->buffer, pkt_len);
854
855                 buffer = pkt + tmp;
856                 buf_len = len - tmp;
857         } else {
858                 buffer = pkt;
859                 buf_len = len;
860         }
861
862         /* loop over the received packet, process */
863         pos = 0;
864         while (pos < buf_len) {
865                 /* get packet len */
866                 pkt_len = usbtouch->type->get_pkt_len(buffer + pos,
867                                                         buf_len - pos);
868
869                 /* unknown packet: skip one byte */
870                 if (unlikely(!pkt_len)) {
871                         pos++;
872                         continue;
873                 }
874
875                 /* full packet: process */
876                 if (likely((pkt_len > 0) && (pkt_len <= buf_len - pos))) {
877                         usbtouch_process_pkt(usbtouch, buffer + pos, pkt_len);
878                 } else {
879                         /* incomplete packet: save in buffer */
880                         memcpy(usbtouch->buffer, buffer + pos, buf_len - pos);
881                         usbtouch->buf_len = buf_len - pos;
882                         return;
883                 }
884                 pos += pkt_len;
885         }
886
887 out_flush_buf:
888         usbtouch->buf_len = 0;
889         return;
890 }
891 #endif
892
893
894 static void usbtouch_irq(struct urb *urb)
895 {
896         struct usbtouch_usb *usbtouch = urb->context;
897         int retval;
898
899         switch (urb->status) {
900         case 0:
901                 /* success */
902                 break;
903         case -ETIME:
904                 /* this urb is timing out */
905                 dbg("%s - urb timed out - was the device unplugged?",
906                     __func__);
907                 return;
908         case -ECONNRESET:
909         case -ENOENT:
910         case -ESHUTDOWN:
911                 /* this urb is terminated, clean up */
912                 dbg("%s - urb shutting down with status: %d",
913                     __func__, urb->status);
914                 return;
915         default:
916                 dbg("%s - nonzero urb status received: %d",
917                     __func__, urb->status);
918                 goto exit;
919         }
920
921         usbtouch->type->process_pkt(usbtouch, usbtouch->data, urb->actual_length);
922
923 exit:
924         retval = usb_submit_urb(urb, GFP_ATOMIC);
925         if (retval)
926                 err("%s - usb_submit_urb failed with result: %d",
927                     __func__, retval);
928 }
929
930 static int usbtouch_open(struct input_dev *input)
931 {
932         struct usbtouch_usb *usbtouch = input_get_drvdata(input);
933
934         usbtouch->irq->dev = usbtouch->udev;
935
936         if (usb_submit_urb(usbtouch->irq, GFP_KERNEL))
937                 return -EIO;
938
939         return 0;
940 }
941
942 static void usbtouch_close(struct input_dev *input)
943 {
944         struct usbtouch_usb *usbtouch = input_get_drvdata(input);
945
946         usb_kill_urb(usbtouch->irq);
947 }
948
949
950 static void usbtouch_free_buffers(struct usb_device *udev,
951                                   struct usbtouch_usb *usbtouch)
952 {
953         usb_buffer_free(udev, usbtouch->type->rept_size,
954                         usbtouch->data, usbtouch->data_dma);
955         kfree(usbtouch->buffer);
956 }
957
958
959 static int usbtouch_probe(struct usb_interface *intf,
960                           const struct usb_device_id *id)
961 {
962         struct usbtouch_usb *usbtouch;
963         struct input_dev *input_dev;
964         struct usb_host_interface *interface;
965         struct usb_endpoint_descriptor *endpoint;
966         struct usb_device *udev = interface_to_usbdev(intf);
967         struct usbtouch_device_info *type;
968         int err = -ENOMEM;
969
970         /* some devices are ignored */
971         if (id->driver_info == DEVTYPE_IGNORE)
972                 return -ENODEV;
973
974         interface = intf->cur_altsetting;
975         endpoint = &interface->endpoint[0].desc;
976
977         usbtouch = kzalloc(sizeof(struct usbtouch_usb), GFP_KERNEL);
978         input_dev = input_allocate_device();
979         if (!usbtouch || !input_dev)
980                 goto out_free;
981
982         type = &usbtouch_dev_info[id->driver_info];
983         usbtouch->type = type;
984         if (!type->process_pkt)
985                 type->process_pkt = usbtouch_process_pkt;
986
987         usbtouch->data = usb_buffer_alloc(udev, type->rept_size,
988                                           GFP_KERNEL, &usbtouch->data_dma);
989         if (!usbtouch->data)
990                 goto out_free;
991
992         if (type->get_pkt_len) {
993                 usbtouch->buffer = kmalloc(type->rept_size, GFP_KERNEL);
994                 if (!usbtouch->buffer)
995                         goto out_free_buffers;
996         }
997
998         usbtouch->irq = usb_alloc_urb(0, GFP_KERNEL);
999         if (!usbtouch->irq) {
1000                 dbg("%s - usb_alloc_urb failed: usbtouch->irq", __func__);
1001                 goto out_free_buffers;
1002         }
1003
1004         usbtouch->udev = udev;
1005         usbtouch->input = input_dev;
1006
1007         if (udev->manufacturer)
1008                 strlcpy(usbtouch->name, udev->manufacturer, sizeof(usbtouch->name));
1009
1010         if (udev->product) {
1011                 if (udev->manufacturer)
1012                         strlcat(usbtouch->name, " ", sizeof(usbtouch->name));
1013                 strlcat(usbtouch->name, udev->product, sizeof(usbtouch->name));
1014         }
1015
1016         if (!strlen(usbtouch->name))
1017                 snprintf(usbtouch->name, sizeof(usbtouch->name),
1018                         "USB Touchscreen %04x:%04x",
1019                          le16_to_cpu(udev->descriptor.idVendor),
1020                          le16_to_cpu(udev->descriptor.idProduct));
1021
1022         usb_make_path(udev, usbtouch->phys, sizeof(usbtouch->phys));
1023         strlcat(usbtouch->phys, "/input0", sizeof(usbtouch->phys));
1024
1025         input_dev->name = usbtouch->name;
1026         input_dev->phys = usbtouch->phys;
1027         usb_to_input_id(udev, &input_dev->id);
1028         input_dev->dev.parent = &intf->dev;
1029
1030         input_set_drvdata(input_dev, usbtouch);
1031
1032         input_dev->open = usbtouch_open;
1033         input_dev->close = usbtouch_close;
1034
1035         input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
1036         input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
1037         input_set_abs_params(input_dev, ABS_X, type->min_xc, type->max_xc, 0, 0);
1038         input_set_abs_params(input_dev, ABS_Y, type->min_yc, type->max_yc, 0, 0);
1039         if (type->max_press)
1040                 input_set_abs_params(input_dev, ABS_PRESSURE, type->min_press,
1041                                      type->max_press, 0, 0);
1042
1043         usb_fill_int_urb(usbtouch->irq, usbtouch->udev,
1044                          usb_rcvintpipe(usbtouch->udev, endpoint->bEndpointAddress),
1045                          usbtouch->data, type->rept_size,
1046                          usbtouch_irq, usbtouch, endpoint->bInterval);
1047
1048         usbtouch->irq->dev = usbtouch->udev;
1049         usbtouch->irq->transfer_dma = usbtouch->data_dma;
1050         usbtouch->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1051
1052         /* device specific init */
1053         if (type->init) {
1054                 err = type->init(usbtouch);
1055                 if (err) {
1056                         dbg("%s - type->init() failed, err: %d", __func__, err);
1057                         goto out_free_buffers;
1058                 }
1059         }
1060
1061         err = input_register_device(usbtouch->input);
1062         if (err) {
1063                 dbg("%s - input_register_device failed, err: %d", __func__, err);
1064                 goto out_free_buffers;
1065         }
1066
1067         usb_set_intfdata(intf, usbtouch);
1068
1069         return 0;
1070
1071 out_free_buffers:
1072         usbtouch_free_buffers(udev, usbtouch);
1073 out_free:
1074         input_free_device(input_dev);
1075         kfree(usbtouch);
1076         return err;
1077 }
1078
1079 static void usbtouch_disconnect(struct usb_interface *intf)
1080 {
1081         struct usbtouch_usb *usbtouch = usb_get_intfdata(intf);
1082
1083         dbg("%s - called", __func__);
1084
1085         if (!usbtouch)
1086                 return;
1087
1088         dbg("%s - usbtouch is initialized, cleaning up", __func__);
1089         usb_set_intfdata(intf, NULL);
1090         usb_kill_urb(usbtouch->irq);
1091         input_unregister_device(usbtouch->input);
1092         usb_free_urb(usbtouch->irq);
1093         usbtouch_free_buffers(interface_to_usbdev(intf), usbtouch);
1094         kfree(usbtouch);
1095 }
1096
1097 MODULE_DEVICE_TABLE(usb, usbtouch_devices);
1098
1099 static struct usb_driver usbtouch_driver = {
1100         .name           = "usbtouchscreen",
1101         .probe          = usbtouch_probe,
1102         .disconnect     = usbtouch_disconnect,
1103         .id_table       = usbtouch_devices,
1104 };
1105
1106 static int __init usbtouch_init(void)
1107 {
1108         return usb_register(&usbtouch_driver);
1109 }
1110
1111 static void __exit usbtouch_cleanup(void)
1112 {
1113         usb_deregister(&usbtouch_driver);
1114 }
1115
1116 module_init(usbtouch_init);
1117 module_exit(usbtouch_cleanup);
1118
1119 MODULE_AUTHOR(DRIVER_AUTHOR);
1120 MODULE_DESCRIPTION(DRIVER_DESC);
1121 MODULE_LICENSE("GPL");
1122
1123 MODULE_ALIAS("touchkitusb");
1124 MODULE_ALIAS("itmtouch");
1125 MODULE_ALIAS("mtouchusb");