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