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