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