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