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