pandora: defconfig: update
[pandora-kernel.git] / drivers / input / touchscreen / usbtouchscreen.c
1 /******************************************************************************
2  * usbtouchscreen.c
3  * Driver for USB Touchscreens, supporting those devices:
4  *  - eGalax Touchkit
5  *    includes eTurboTouch CT-410/510/700
6  *  - 3M/Microtouch  EX II series
7  *  - ITM
8  *  - PanJit TouchSet
9  *  - eTurboTouch
10  *  - Gunze AHL61
11  *  - DMC TSC-10/25
12  *  - IRTOUCHSYSTEMS/UNITOP
13  *  - IdealTEK URTC1000
14  *  - General Touch
15  *  - GoTop Super_Q2/GogoPen/PenPower tablets
16  *  - JASTEC USB touch controller/DigiTech DTR-02U
17  *  - Zytronic capacitive touchscreen
18  *  - NEXIO/iNexio
19  *
20  * Copyright (C) 2004-2007 by Daniel Ritz <daniel.ritz@gmx.ch>
21  * Copyright (C) by Todd E. Johnson (mtouchusb.c)
22  *
23  * This program is free software; you can redistribute it and/or
24  * modify it under the terms of the GNU General Public License as
25  * published by the Free Software Foundation; either version 2 of the
26  * License, or (at your option) any later version.
27  *
28  * This program is distributed in the hope that it will be useful, but
29  * WITHOUT ANY WARRANTY; without even the implied warranty of
30  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
31  * General Public License for more details.
32  *
33  * You should have received a copy of the GNU General Public License
34  * along with this program; if not, write to the Free Software
35  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
36  *
37  * Driver is based on touchkitusb.c
38  * - ITM parts are from itmtouch.c
39  * - 3M parts are from mtouchusb.c
40  * - PanJit parts are from an unmerged driver by Lanslott Gish
41  * - DMC TSC 10/25 are from Holger Schurig, with ideas from an unmerged
42  *   driver from Marius Vollmer
43  *
44  *****************************************************************************/
45
46 //#define DEBUG
47
48 #include <linux/kernel.h>
49 #include <linux/slab.h>
50 #include <linux/input.h>
51 #include <linux/module.h>
52 #include <linux/init.h>
53 #include <linux/usb.h>
54 #include <linux/usb/input.h>
55 #include <linux/hid.h>
56
57
58 #define DRIVER_VERSION          "v0.6"
59 #define DRIVER_AUTHOR           "Daniel Ritz <daniel.ritz@gmx.ch>"
60 #define DRIVER_DESC             "USB Touchscreen Driver"
61
62 static int swap_xy;
63 module_param(swap_xy, bool, 0644);
64 MODULE_PARM_DESC(swap_xy, "If set X and Y axes are swapped.");
65
66 static int hwcalib_xy;
67 module_param(hwcalib_xy, bool, 0644);
68 MODULE_PARM_DESC(hwcalib_xy, "If set hw-calibrated X/Y are used if available");
69
70 /* device specifc data/functions */
71 struct usbtouch_usb;
72 struct usbtouch_device_info {
73         int min_xc, max_xc;
74         int min_yc, max_yc;
75         int min_press, max_press;
76         int rept_size;
77
78         /*
79          * Always service the USB devices irq not just when the input device is
80          * open. This is useful when devices have a watchdog which prevents us
81          * from periodically polling the device. Leave this unset unless your
82          * touchscreen device requires it, as it does consume more of the USB
83          * bandwidth.
84          */
85         bool irq_always;
86
87         void (*process_pkt) (struct usbtouch_usb *usbtouch, unsigned char *pkt, int len);
88
89         /*
90          * used to get the packet len. possible return values:
91          * > 0: packet len
92          * = 0: skip one byte
93          * < 0: -return value more bytes needed
94          */
95         int  (*get_pkt_len) (unsigned char *pkt, int len);
96
97         int  (*read_data)   (struct usbtouch_usb *usbtouch, unsigned char *pkt);
98         int  (*alloc)       (struct usbtouch_usb *usbtouch);
99         int  (*init)        (struct usbtouch_usb *usbtouch);
100         void (*exit)        (struct usbtouch_usb *usbtouch);
101 };
102
103 /* a usbtouch device */
104 struct usbtouch_usb {
105         unsigned char *data;
106         dma_addr_t data_dma;
107         unsigned char *buffer;
108         int buf_len;
109         struct urb *irq;
110         struct usb_interface *interface;
111         struct input_dev *input;
112         struct usbtouch_device_info *type;
113         char name[128];
114         char phys[64];
115         void *priv;
116
117         int x, y;
118         int touch, press;
119 };
120
121
122 /* device types */
123 enum {
124         DEVTYPE_IGNORE = -1,
125         DEVTYPE_EGALAX,
126         DEVTYPE_PANJIT,
127         DEVTYPE_3M,
128         DEVTYPE_ITM,
129         DEVTYPE_ETURBO,
130         DEVTYPE_GUNZE,
131         DEVTYPE_DMC_TSC10,
132         DEVTYPE_IRTOUCH,
133         DEVTYPE_IDEALTEK,
134         DEVTYPE_GENERAL_TOUCH,
135         DEVTYPE_GOTOP,
136         DEVTYPE_JASTEC,
137         DEVTYPE_E2I,
138         DEVTYPE_ZYTRONIC,
139         DEVTYPE_TC45USB,
140         DEVTYPE_NEXIO,
141 };
142
143 #define USB_DEVICE_HID_CLASS(vend, prod) \
144         .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS \
145                 | USB_DEVICE_ID_MATCH_INT_PROTOCOL \
146                 | USB_DEVICE_ID_MATCH_DEVICE, \
147         .idVendor = (vend), \
148         .idProduct = (prod), \
149         .bInterfaceClass = USB_INTERFACE_CLASS_HID, \
150         .bInterfaceProtocol = USB_INTERFACE_PROTOCOL_MOUSE
151
152 static const struct usb_device_id usbtouch_devices[] = {
153 #ifdef CONFIG_TOUCHSCREEN_USB_EGALAX
154         /* ignore the HID capable devices, handled by usbhid */
155         {USB_DEVICE_HID_CLASS(0x0eef, 0x0001), .driver_info = DEVTYPE_IGNORE},
156         {USB_DEVICE_HID_CLASS(0x0eef, 0x0002), .driver_info = DEVTYPE_IGNORE},
157
158         /* normal device IDs */
159         {USB_DEVICE(0x3823, 0x0001), .driver_info = DEVTYPE_EGALAX},
160         {USB_DEVICE(0x3823, 0x0002), .driver_info = DEVTYPE_EGALAX},
161         {USB_DEVICE(0x0123, 0x0001), .driver_info = DEVTYPE_EGALAX},
162         {USB_DEVICE(0x0eef, 0x0001), .driver_info = DEVTYPE_EGALAX},
163         {USB_DEVICE(0x0eef, 0x0002), .driver_info = DEVTYPE_EGALAX},
164         {USB_DEVICE(0x1234, 0x0001), .driver_info = DEVTYPE_EGALAX},
165         {USB_DEVICE(0x1234, 0x0002), .driver_info = DEVTYPE_EGALAX},
166 #endif
167
168 #ifdef CONFIG_TOUCHSCREEN_USB_PANJIT
169         {USB_DEVICE(0x134c, 0x0001), .driver_info = DEVTYPE_PANJIT},
170         {USB_DEVICE(0x134c, 0x0002), .driver_info = DEVTYPE_PANJIT},
171         {USB_DEVICE(0x134c, 0x0003), .driver_info = DEVTYPE_PANJIT},
172         {USB_DEVICE(0x134c, 0x0004), .driver_info = DEVTYPE_PANJIT},
173 #endif
174
175 #ifdef CONFIG_TOUCHSCREEN_USB_3M
176         {USB_DEVICE(0x0596, 0x0001), .driver_info = DEVTYPE_3M},
177 #endif
178
179 #ifdef CONFIG_TOUCHSCREEN_USB_ITM
180         {USB_DEVICE(0x0403, 0xf9e9), .driver_info = DEVTYPE_ITM},
181         {USB_DEVICE(0x16e3, 0xf9e9), .driver_info = DEVTYPE_ITM},
182 #endif
183
184 #ifdef CONFIG_TOUCHSCREEN_USB_ETURBO
185         {USB_DEVICE(0x1234, 0x5678), .driver_info = DEVTYPE_ETURBO},
186 #endif
187
188 #ifdef CONFIG_TOUCHSCREEN_USB_GUNZE
189         {USB_DEVICE(0x0637, 0x0001), .driver_info = DEVTYPE_GUNZE},
190 #endif
191
192 #ifdef CONFIG_TOUCHSCREEN_USB_DMC_TSC10
193         {USB_DEVICE(0x0afa, 0x03e8), .driver_info = DEVTYPE_DMC_TSC10},
194 #endif
195
196 #ifdef CONFIG_TOUCHSCREEN_USB_IRTOUCH
197         {USB_DEVICE(0x595a, 0x0001), .driver_info = DEVTYPE_IRTOUCH},
198         {USB_DEVICE(0x6615, 0x0001), .driver_info = DEVTYPE_IRTOUCH},
199 #endif
200
201 #ifdef CONFIG_TOUCHSCREEN_USB_IDEALTEK
202         {USB_DEVICE(0x1391, 0x1000), .driver_info = DEVTYPE_IDEALTEK},
203 #endif
204
205 #ifdef CONFIG_TOUCHSCREEN_USB_GENERAL_TOUCH
206         {USB_DEVICE(0x0dfc, 0x0001), .driver_info = DEVTYPE_GENERAL_TOUCH},
207 #endif
208
209 #ifdef CONFIG_TOUCHSCREEN_USB_GOTOP
210         {USB_DEVICE(0x08f2, 0x007f), .driver_info = DEVTYPE_GOTOP},
211         {USB_DEVICE(0x08f2, 0x00ce), .driver_info = DEVTYPE_GOTOP},
212         {USB_DEVICE(0x08f2, 0x00f4), .driver_info = DEVTYPE_GOTOP},
213 #endif
214
215 #ifdef CONFIG_TOUCHSCREEN_USB_JASTEC
216         {USB_DEVICE(0x0f92, 0x0001), .driver_info = DEVTYPE_JASTEC},
217 #endif
218
219 #ifdef CONFIG_TOUCHSCREEN_USB_E2I
220         {USB_DEVICE(0x1ac7, 0x0001), .driver_info = DEVTYPE_E2I},
221 #endif
222
223 #ifdef CONFIG_TOUCHSCREEN_USB_ZYTRONIC
224         {USB_DEVICE(0x14c8, 0x0003), .driver_info = DEVTYPE_ZYTRONIC},
225 #endif
226
227 #ifdef CONFIG_TOUCHSCREEN_USB_ETT_TC45USB
228         /* TC5UH */
229         {USB_DEVICE(0x0664, 0x0309), .driver_info = DEVTYPE_TC45USB},
230         /* TC4UM */
231         {USB_DEVICE(0x0664, 0x0306), .driver_info = DEVTYPE_TC45USB},
232 #endif
233
234 #ifdef CONFIG_TOUCHSCREEN_USB_NEXIO
235         /* data interface only */
236         {USB_DEVICE_AND_INTERFACE_INFO(0x10f0, 0x2002, 0x0a, 0x00, 0x00),
237                 .driver_info = DEVTYPE_NEXIO},
238         {USB_DEVICE_AND_INTERFACE_INFO(0x1870, 0x0001, 0x0a, 0x00, 0x00),
239                 .driver_info = DEVTYPE_NEXIO},
240 #endif
241
242         {}
243 };
244
245
246 /*****************************************************************************
247  * e2i Part
248  */
249
250 #ifdef CONFIG_TOUCHSCREEN_USB_E2I
251 static int e2i_init(struct usbtouch_usb *usbtouch)
252 {
253         int ret;
254         struct usb_device *udev = interface_to_usbdev(usbtouch->interface);
255
256         ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
257                               0x01, 0x02, 0x0000, 0x0081,
258                               NULL, 0, USB_CTRL_SET_TIMEOUT);
259
260         dbg("%s - usb_control_msg - E2I_RESET - bytes|err: %d",
261             __func__, ret);
262         return ret;
263 }
264
265 static int e2i_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
266 {
267         int tmp = (pkt[0] << 8) | pkt[1];
268         dev->x  = (pkt[2] << 8) | pkt[3];
269         dev->y  = (pkt[4] << 8) | pkt[5];
270
271         tmp = tmp - 0xA000;
272         dev->touch = (tmp > 0);
273         dev->press = (tmp > 0 ? tmp : 0);
274
275         return 1;
276 }
277 #endif
278
279
280 /*****************************************************************************
281  * eGalax part
282  */
283
284 #ifdef CONFIG_TOUCHSCREEN_USB_EGALAX
285
286 #ifndef MULTI_PACKET
287 #define MULTI_PACKET
288 #endif
289
290 #define EGALAX_PKT_TYPE_MASK            0xFE
291 #define EGALAX_PKT_TYPE_REPT            0x80
292 #define EGALAX_PKT_TYPE_DIAG            0x0A
293
294 static int egalax_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
295 {
296         if ((pkt[0] & EGALAX_PKT_TYPE_MASK) != EGALAX_PKT_TYPE_REPT)
297                 return 0;
298
299         dev->x = ((pkt[3] & 0x0F) << 7) | (pkt[4] & 0x7F);
300         dev->y = ((pkt[1] & 0x0F) << 7) | (pkt[2] & 0x7F);
301         dev->touch = pkt[0] & 0x01;
302
303         return 1;
304 }
305
306 static int egalax_get_pkt_len(unsigned char *buf, int len)
307 {
308         switch (buf[0] & EGALAX_PKT_TYPE_MASK) {
309         case EGALAX_PKT_TYPE_REPT:
310                 return 5;
311
312         case EGALAX_PKT_TYPE_DIAG:
313                 if (len < 2)
314                         return -1;
315
316                 return buf[1] + 2;
317         }
318
319         return 0;
320 }
321 #endif
322
323
324 /*****************************************************************************
325  * PanJit Part
326  */
327 #ifdef CONFIG_TOUCHSCREEN_USB_PANJIT
328 static int panjit_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
329 {
330         dev->x = ((pkt[2] & 0x0F) << 8) | pkt[1];
331         dev->y = ((pkt[4] & 0x0F) << 8) | pkt[3];
332         dev->touch = pkt[0] & 0x01;
333
334         return 1;
335 }
336 #endif
337
338
339 /*****************************************************************************
340  * 3M/Microtouch Part
341  */
342 #ifdef CONFIG_TOUCHSCREEN_USB_3M
343
344 #define MTOUCHUSB_ASYNC_REPORT          1
345 #define MTOUCHUSB_RESET                 7
346 #define MTOUCHUSB_REQ_CTRLLR_ID         10
347
348 static int mtouch_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
349 {
350         if (hwcalib_xy) {
351                 dev->x = (pkt[4] << 8) | pkt[3];
352                 dev->y = 0xffff - ((pkt[6] << 8) | pkt[5]);
353         } else {
354                 dev->x = (pkt[8] << 8) | pkt[7];
355                 dev->y = (pkt[10] << 8) | pkt[9];
356         }
357         dev->touch = (pkt[2] & 0x40) ? 1 : 0;
358
359         return 1;
360 }
361
362 static int mtouch_init(struct usbtouch_usb *usbtouch)
363 {
364         int ret, i;
365         struct usb_device *udev = interface_to_usbdev(usbtouch->interface);
366
367         ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
368                               MTOUCHUSB_RESET,
369                               USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
370                               1, 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
371         dbg("%s - usb_control_msg - MTOUCHUSB_RESET - bytes|err: %d",
372             __func__, ret);
373         if (ret < 0)
374                 return ret;
375         msleep(150);
376
377         for (i = 0; i < 3; i++) {
378                 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
379                                       MTOUCHUSB_ASYNC_REPORT,
380                                       USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
381                                       1, 1, NULL, 0, USB_CTRL_SET_TIMEOUT);
382                 dbg("%s - usb_control_msg - MTOUCHUSB_ASYNC_REPORT - bytes|err: %d",
383                     __func__, ret);
384                 if (ret >= 0)
385                         break;
386                 if (ret != -EPIPE)
387                         return ret;
388         }
389
390         /* Default min/max xy are the raw values, override if using hw-calib */
391         if (hwcalib_xy) {
392                 input_set_abs_params(usbtouch->input, ABS_X, 0, 0xffff, 0, 0);
393                 input_set_abs_params(usbtouch->input, ABS_Y, 0, 0xffff, 0, 0);
394         }
395
396         return 0;
397 }
398 #endif
399
400
401 /*****************************************************************************
402  * ITM Part
403  */
404 #ifdef CONFIG_TOUCHSCREEN_USB_ITM
405 static int itm_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
406 {
407         int touch;
408         /*
409          * ITM devices report invalid x/y data if not touched.
410          * if the screen was touched before but is not touched any more
411          * report touch as 0 with the last valid x/y data once. then stop
412          * reporting data until touched again.
413          */
414         dev->press = ((pkt[2] & 0x01) << 7) | (pkt[5] & 0x7F);
415
416         touch = ~pkt[7] & 0x20;
417         if (!touch) {
418                 if (dev->touch) {
419                         dev->touch = 0;
420                         return 1;
421                 }
422
423                 return 0;
424         }
425
426         dev->x = ((pkt[0] & 0x1F) << 7) | (pkt[3] & 0x7F);
427         dev->y = ((pkt[1] & 0x1F) << 7) | (pkt[4] & 0x7F);
428         dev->touch = touch;
429
430         return 1;
431 }
432 #endif
433
434
435 /*****************************************************************************
436  * eTurboTouch part
437  */
438 #ifdef CONFIG_TOUCHSCREEN_USB_ETURBO
439 #ifndef MULTI_PACKET
440 #define MULTI_PACKET
441 #endif
442 static int eturbo_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
443 {
444         unsigned int shift;
445
446         /* packets should start with sync */
447         if (!(pkt[0] & 0x80))
448                 return 0;
449
450         shift = (6 - (pkt[0] & 0x03));
451         dev->x = ((pkt[3] << 7) | pkt[4]) >> shift;
452         dev->y = ((pkt[1] << 7) | pkt[2]) >> shift;
453         dev->touch = (pkt[0] & 0x10) ? 1 : 0;
454
455         return 1;
456 }
457
458 static int eturbo_get_pkt_len(unsigned char *buf, int len)
459 {
460         if (buf[0] & 0x80)
461                 return 5;
462         if (buf[0] == 0x01)
463                 return 3;
464         return 0;
465 }
466 #endif
467
468
469 /*****************************************************************************
470  * Gunze part
471  */
472 #ifdef CONFIG_TOUCHSCREEN_USB_GUNZE
473 static int gunze_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
474 {
475         if (!(pkt[0] & 0x80) || ((pkt[1] | pkt[2] | pkt[3]) & 0x80))
476                 return 0;
477
478         dev->x = ((pkt[0] & 0x1F) << 7) | (pkt[2] & 0x7F);
479         dev->y = ((pkt[1] & 0x1F) << 7) | (pkt[3] & 0x7F);
480         dev->touch = pkt[0] & 0x20;
481
482         return 1;
483 }
484 #endif
485
486 /*****************************************************************************
487  * DMC TSC-10/25 Part
488  *
489  * Documentation about the controller and it's protocol can be found at
490  *   http://www.dmccoltd.com/files/controler/tsc10usb_pi_e.pdf
491  *   http://www.dmccoltd.com/files/controler/tsc25_usb_e.pdf
492  */
493 #ifdef CONFIG_TOUCHSCREEN_USB_DMC_TSC10
494
495 /* supported data rates. currently using 130 */
496 #define TSC10_RATE_POINT        0x50
497 #define TSC10_RATE_30           0x40
498 #define TSC10_RATE_50           0x41
499 #define TSC10_RATE_80           0x42
500 #define TSC10_RATE_100          0x43
501 #define TSC10_RATE_130          0x44
502 #define TSC10_RATE_150          0x45
503
504 /* commands */
505 #define TSC10_CMD_RESET         0x55
506 #define TSC10_CMD_RATE          0x05
507 #define TSC10_CMD_DATA1         0x01
508
509 static int dmc_tsc10_init(struct usbtouch_usb *usbtouch)
510 {
511         struct usb_device *dev = interface_to_usbdev(usbtouch->interface);
512         int ret = -ENOMEM;
513         unsigned char *buf;
514
515         buf = kmalloc(2, GFP_NOIO);
516         if (!buf)
517                 goto err_nobuf;
518         /* reset */
519         buf[0] = buf[1] = 0xFF;
520         ret = usb_control_msg(dev, usb_rcvctrlpipe (dev, 0),
521                               TSC10_CMD_RESET,
522                               USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
523                               0, 0, buf, 2, USB_CTRL_SET_TIMEOUT);
524         if (ret < 0)
525                 goto err_out;
526         if (buf[0] != 0x06) {
527                 ret = -ENODEV;
528                 goto err_out;
529         }
530
531         /* TSC-25 data sheet specifies a delay after the RESET command */
532         msleep(150);
533
534         /* set coordinate output rate */
535         buf[0] = buf[1] = 0xFF;
536         ret = usb_control_msg(dev, usb_rcvctrlpipe (dev, 0),
537                               TSC10_CMD_RATE,
538                               USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
539                               TSC10_RATE_150, 0, buf, 2, USB_CTRL_SET_TIMEOUT);
540         if (ret < 0)
541                 goto err_out;
542         if ((buf[0] != 0x06) && (buf[0] != 0x15 || buf[1] != 0x01)) {
543                 ret = -ENODEV;
544                 goto err_out;
545         }
546
547         /* start sending data */
548         ret = usb_control_msg(dev, usb_rcvctrlpipe (dev, 0),
549                               TSC10_CMD_DATA1,
550                               USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
551                               0, 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
552 err_out:
553         kfree(buf);
554 err_nobuf:
555         return ret;
556 }
557
558
559 static int dmc_tsc10_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
560 {
561         dev->x = ((pkt[2] & 0x03) << 8) | pkt[1];
562         dev->y = ((pkt[4] & 0x03) << 8) | pkt[3];
563         dev->touch = pkt[0] & 0x01;
564
565         return 1;
566 }
567 #endif
568
569
570 /*****************************************************************************
571  * IRTOUCH Part
572  */
573 #ifdef CONFIG_TOUCHSCREEN_USB_IRTOUCH
574 static int irtouch_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
575 {
576         dev->x = (pkt[3] << 8) | pkt[2];
577         dev->y = (pkt[5] << 8) | pkt[4];
578         dev->touch = (pkt[1] & 0x03) ? 1 : 0;
579
580         return 1;
581 }
582 #endif
583
584 /*****************************************************************************
585  * ET&T TC5UH/TC4UM part
586  */
587 #ifdef CONFIG_TOUCHSCREEN_USB_ETT_TC45USB
588 static int tc45usb_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
589 {
590         dev->x = ((pkt[2] & 0x0F) << 8) | pkt[1];
591         dev->y = ((pkt[4] & 0x0F) << 8) | pkt[3];
592         dev->touch = pkt[0] & 0x01;
593
594         return 1;
595 }
596 #endif
597
598 /*****************************************************************************
599  * IdealTEK URTC1000 Part
600  */
601 #ifdef CONFIG_TOUCHSCREEN_USB_IDEALTEK
602 #ifndef MULTI_PACKET
603 #define MULTI_PACKET
604 #endif
605 static int idealtek_get_pkt_len(unsigned char *buf, int len)
606 {
607         if (buf[0] & 0x80)
608                 return 5;
609         if (buf[0] == 0x01)
610                 return len;
611         return 0;
612 }
613
614 static int idealtek_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
615 {
616         switch (pkt[0] & 0x98) {
617         case 0x88:
618                 /* touch data in IdealTEK mode */
619                 dev->x = (pkt[1] << 5) | (pkt[2] >> 2);
620                 dev->y = (pkt[3] << 5) | (pkt[4] >> 2);
621                 dev->touch = (pkt[0] & 0x40) ? 1 : 0;
622                 return 1;
623
624         case 0x98:
625                 /* touch data in MT emulation mode */
626                 dev->x = (pkt[2] << 5) | (pkt[1] >> 2);
627                 dev->y = (pkt[4] << 5) | (pkt[3] >> 2);
628                 dev->touch = (pkt[0] & 0x40) ? 1 : 0;
629                 return 1;
630
631         default:
632                 return 0;
633         }
634 }
635 #endif
636
637 /*****************************************************************************
638  * General Touch Part
639  */
640 #ifdef CONFIG_TOUCHSCREEN_USB_GENERAL_TOUCH
641 static int general_touch_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
642 {
643         dev->x = (pkt[2] << 8) | pkt[1];
644         dev->y = (pkt[4] << 8) | pkt[3];
645         dev->press = pkt[5] & 0xff;
646         dev->touch = pkt[0] & 0x01;
647
648         return 1;
649 }
650 #endif
651
652 /*****************************************************************************
653  * GoTop Part
654  */
655 #ifdef CONFIG_TOUCHSCREEN_USB_GOTOP
656 static int gotop_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
657 {
658         dev->x = ((pkt[1] & 0x38) << 4) | pkt[2];
659         dev->y = ((pkt[1] & 0x07) << 7) | pkt[3];
660         dev->touch = pkt[0] & 0x01;
661
662         return 1;
663 }
664 #endif
665
666 /*****************************************************************************
667  * JASTEC Part
668  */
669 #ifdef CONFIG_TOUCHSCREEN_USB_JASTEC
670 static int jastec_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
671 {
672         dev->x = ((pkt[0] & 0x3f) << 6) | (pkt[2] & 0x3f);
673         dev->y = ((pkt[1] & 0x3f) << 6) | (pkt[3] & 0x3f);
674         dev->touch = (pkt[0] & 0x40) >> 6;
675
676         return 1;
677 }
678 #endif
679
680 /*****************************************************************************
681  * Zytronic Part
682  */
683 #ifdef CONFIG_TOUCHSCREEN_USB_ZYTRONIC
684 static int zytronic_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
685 {
686         switch (pkt[0]) {
687         case 0x3A: /* command response */
688                 dbg("%s: Command response %d", __func__, pkt[1]);
689                 break;
690
691         case 0xC0: /* down */
692                 dev->x = (pkt[1] & 0x7f) | ((pkt[2] & 0x07) << 7);
693                 dev->y = (pkt[3] & 0x7f) | ((pkt[4] & 0x07) << 7);
694                 dev->touch = 1;
695                 dbg("%s: down %d,%d", __func__, dev->x, dev->y);
696                 return 1;
697
698         case 0x80: /* up */
699                 dev->x = (pkt[1] & 0x7f) | ((pkt[2] & 0x07) << 7);
700                 dev->y = (pkt[3] & 0x7f) | ((pkt[4] & 0x07) << 7);
701                 dev->touch = 0;
702                 dbg("%s: up %d,%d", __func__, dev->x, dev->y);
703                 return 1;
704
705         default:
706                 dbg("%s: Unknown return %d", __func__, pkt[0]);
707                 break;
708         }
709
710         return 0;
711 }
712 #endif
713
714 /*****************************************************************************
715  * NEXIO Part
716  */
717 #ifdef CONFIG_TOUCHSCREEN_USB_NEXIO
718
719 #define NEXIO_TIMEOUT   5000
720 #define NEXIO_BUFSIZE   1024
721 #define NEXIO_THRESHOLD 50
722
723 struct nexio_priv {
724         struct urb *ack;
725         unsigned char *ack_buf;
726 };
727
728 struct nexio_touch_packet {
729         u8      flags;          /* 0xe1 = touch, 0xe1 = release */
730         __be16  data_len;       /* total bytes of touch data */
731         __be16  x_len;          /* bytes for X axis */
732         __be16  y_len;          /* bytes for Y axis */
733         u8      data[];
734 } __attribute__ ((packed));
735
736 static unsigned char nexio_ack_pkt[2] = { 0xaa, 0x02 };
737 static unsigned char nexio_init_pkt[4] = { 0x82, 0x04, 0x0a, 0x0f };
738
739 static void nexio_ack_complete(struct urb *urb)
740 {
741 }
742
743 static int nexio_alloc(struct usbtouch_usb *usbtouch)
744 {
745         struct nexio_priv *priv;
746         int ret = -ENOMEM;
747
748         usbtouch->priv = kmalloc(sizeof(struct nexio_priv), GFP_KERNEL);
749         if (!usbtouch->priv)
750                 goto out_buf;
751
752         priv = usbtouch->priv;
753
754         priv->ack_buf = kmemdup(nexio_ack_pkt, sizeof(nexio_ack_pkt),
755                                 GFP_KERNEL);
756         if (!priv->ack_buf)
757                 goto err_priv;
758
759         priv->ack = usb_alloc_urb(0, GFP_KERNEL);
760         if (!priv->ack) {
761                 dbg("%s - usb_alloc_urb failed: usbtouch->ack", __func__);
762                 goto err_ack_buf;
763         }
764
765         return 0;
766
767 err_ack_buf:
768         kfree(priv->ack_buf);
769 err_priv:
770         kfree(priv);
771 out_buf:
772         return ret;
773 }
774
775 static int nexio_init(struct usbtouch_usb *usbtouch)
776 {
777         struct usb_device *dev = interface_to_usbdev(usbtouch->interface);
778         struct usb_host_interface *interface = usbtouch->interface->cur_altsetting;
779         struct nexio_priv *priv = usbtouch->priv;
780         int ret = -ENOMEM;
781         int actual_len, i;
782         unsigned char *buf;
783         char *firmware_ver = NULL, *device_name = NULL;
784         int input_ep = 0, output_ep = 0;
785
786         /* find first input and output endpoint */
787         for (i = 0; i < interface->desc.bNumEndpoints; i++) {
788                 if (!input_ep &&
789                     usb_endpoint_dir_in(&interface->endpoint[i].desc))
790                         input_ep = interface->endpoint[i].desc.bEndpointAddress;
791                 if (!output_ep &&
792                     usb_endpoint_dir_out(&interface->endpoint[i].desc))
793                         output_ep = interface->endpoint[i].desc.bEndpointAddress;
794         }
795         if (!input_ep || !output_ep)
796                 return -ENXIO;
797
798         buf = kmalloc(NEXIO_BUFSIZE, GFP_NOIO);
799         if (!buf)
800                 goto out_buf;
801
802         /* two empty reads */
803         for (i = 0; i < 2; i++) {
804                 ret = usb_bulk_msg(dev, usb_rcvbulkpipe(dev, input_ep),
805                                    buf, NEXIO_BUFSIZE, &actual_len,
806                                    NEXIO_TIMEOUT);
807                 if (ret < 0)
808                         goto out_buf;
809         }
810
811         /* send init command */
812         memcpy(buf, nexio_init_pkt, sizeof(nexio_init_pkt));
813         ret = usb_bulk_msg(dev, usb_sndbulkpipe(dev, output_ep),
814                            buf, sizeof(nexio_init_pkt), &actual_len,
815                            NEXIO_TIMEOUT);
816         if (ret < 0)
817                 goto out_buf;
818
819         /* read replies */
820         for (i = 0; i < 3; i++) {
821                 memset(buf, 0, NEXIO_BUFSIZE);
822                 ret = usb_bulk_msg(dev, usb_rcvbulkpipe(dev, input_ep),
823                                    buf, NEXIO_BUFSIZE, &actual_len,
824                                    NEXIO_TIMEOUT);
825                 if (ret < 0 || actual_len < 1 || buf[1] != actual_len)
826                         continue;
827                 switch (buf[0]) {
828                 case 0x83:      /* firmware version */
829                         if (!firmware_ver)
830                                 firmware_ver = kstrdup(&buf[2], GFP_NOIO);
831                         break;
832                 case 0x84:      /* device name */
833                         if (!device_name)
834                                 device_name = kstrdup(&buf[2], GFP_NOIO);
835                         break;
836                 }
837         }
838
839         printk(KERN_INFO "Nexio device: %s, firmware version: %s\n",
840                device_name, firmware_ver);
841
842         kfree(firmware_ver);
843         kfree(device_name);
844
845         usb_fill_bulk_urb(priv->ack, dev, usb_sndbulkpipe(dev, output_ep),
846                           priv->ack_buf, sizeof(nexio_ack_pkt),
847                           nexio_ack_complete, usbtouch);
848         ret = 0;
849
850 out_buf:
851         kfree(buf);
852         return ret;
853 }
854
855 static void nexio_exit(struct usbtouch_usb *usbtouch)
856 {
857         struct nexio_priv *priv = usbtouch->priv;
858
859         usb_kill_urb(priv->ack);
860         usb_free_urb(priv->ack);
861         kfree(priv->ack_buf);
862         kfree(priv);
863 }
864
865 static int nexio_read_data(struct usbtouch_usb *usbtouch, unsigned char *pkt)
866 {
867         struct nexio_touch_packet *packet = (void *) pkt;
868         struct nexio_priv *priv = usbtouch->priv;
869         unsigned int data_len = be16_to_cpu(packet->data_len);
870         unsigned int x_len = be16_to_cpu(packet->x_len);
871         unsigned int y_len = be16_to_cpu(packet->y_len);
872         int x, y, begin_x, begin_y, end_x, end_y, w, h, ret;
873
874         /* got touch data? */
875         if ((pkt[0] & 0xe0) != 0xe0)
876                 return 0;
877
878         if (data_len > 0xff)
879                 data_len -= 0x100;
880         if (x_len > 0xff)
881                 x_len -= 0x80;
882
883         /* send ACK */
884         ret = usb_submit_urb(priv->ack, GFP_ATOMIC);
885
886         if (!usbtouch->type->max_xc) {
887                 usbtouch->type->max_xc = 2 * x_len;
888                 input_set_abs_params(usbtouch->input, ABS_X,
889                                      0, usbtouch->type->max_xc, 0, 0);
890                 usbtouch->type->max_yc = 2 * y_len;
891                 input_set_abs_params(usbtouch->input, ABS_Y,
892                                      0, usbtouch->type->max_yc, 0, 0);
893         }
894         /*
895          * The device reports state of IR sensors on X and Y axes.
896          * Each byte represents "darkness" percentage (0-100) of one element.
897          * 17" touchscreen reports only 64 x 52 bytes so the resolution is low.
898          * This also means that there's a limited multi-touch capability but
899          * it's disabled (and untested) here as there's no X driver for that.
900          */
901         begin_x = end_x = begin_y = end_y = -1;
902         for (x = 0; x < x_len; x++) {
903                 if (begin_x == -1 && packet->data[x] > NEXIO_THRESHOLD) {
904                         begin_x = x;
905                         continue;
906                 }
907                 if (end_x == -1 && begin_x != -1 && packet->data[x] < NEXIO_THRESHOLD) {
908                         end_x = x - 1;
909                         for (y = x_len; y < data_len; y++) {
910                                 if (begin_y == -1 && packet->data[y] > NEXIO_THRESHOLD) {
911                                         begin_y = y - x_len;
912                                         continue;
913                                 }
914                                 if (end_y == -1 &&
915                                     begin_y != -1 && packet->data[y] < NEXIO_THRESHOLD) {
916                                         end_y = y - 1 - x_len;
917                                         w = end_x - begin_x;
918                                         h = end_y - begin_y;
919 #if 0
920                                         /* multi-touch */
921                                         input_report_abs(usbtouch->input,
922                                                     ABS_MT_TOUCH_MAJOR, max(w,h));
923                                         input_report_abs(usbtouch->input,
924                                                     ABS_MT_TOUCH_MINOR, min(x,h));
925                                         input_report_abs(usbtouch->input,
926                                                     ABS_MT_POSITION_X, 2*begin_x+w);
927                                         input_report_abs(usbtouch->input,
928                                                     ABS_MT_POSITION_Y, 2*begin_y+h);
929                                         input_report_abs(usbtouch->input,
930                                                     ABS_MT_ORIENTATION, w > h);
931                                         input_mt_sync(usbtouch->input);
932 #endif
933                                         /* single touch */
934                                         usbtouch->x = 2 * begin_x + w;
935                                         usbtouch->y = 2 * begin_y + h;
936                                         usbtouch->touch = packet->flags & 0x01;
937                                         begin_y = end_y = -1;
938                                         return 1;
939                                 }
940                         }
941                         begin_x = end_x = -1;
942                 }
943
944         }
945         return 0;
946 }
947 #endif
948
949
950 /*****************************************************************************
951  * the different device descriptors
952  */
953 #ifdef MULTI_PACKET
954 static void usbtouch_process_multi(struct usbtouch_usb *usbtouch,
955                                    unsigned char *pkt, int len);
956 #endif
957
958 static struct usbtouch_device_info usbtouch_dev_info[] = {
959 #ifdef CONFIG_TOUCHSCREEN_USB_EGALAX
960         [DEVTYPE_EGALAX] = {
961                 .min_xc         = 0x0,
962                 .max_xc         = 0x07ff,
963                 .min_yc         = 0x0,
964                 .max_yc         = 0x07ff,
965                 .rept_size      = 16,
966                 .process_pkt    = usbtouch_process_multi,
967                 .get_pkt_len    = egalax_get_pkt_len,
968                 .read_data      = egalax_read_data,
969         },
970 #endif
971
972 #ifdef CONFIG_TOUCHSCREEN_USB_PANJIT
973         [DEVTYPE_PANJIT] = {
974                 .min_xc         = 0x0,
975                 .max_xc         = 0x0fff,
976                 .min_yc         = 0x0,
977                 .max_yc         = 0x0fff,
978                 .rept_size      = 8,
979                 .read_data      = panjit_read_data,
980         },
981 #endif
982
983 #ifdef CONFIG_TOUCHSCREEN_USB_3M
984         [DEVTYPE_3M] = {
985                 .min_xc         = 0x0,
986                 .max_xc         = 0x4000,
987                 .min_yc         = 0x0,
988                 .max_yc         = 0x4000,
989                 .rept_size      = 11,
990                 .read_data      = mtouch_read_data,
991                 .init           = mtouch_init,
992         },
993 #endif
994
995 #ifdef CONFIG_TOUCHSCREEN_USB_ITM
996         [DEVTYPE_ITM] = {
997                 .min_xc         = 0x0,
998                 .max_xc         = 0x0fff,
999                 .min_yc         = 0x0,
1000                 .max_yc         = 0x0fff,
1001                 .max_press      = 0xff,
1002                 .rept_size      = 8,
1003                 .read_data      = itm_read_data,
1004         },
1005 #endif
1006
1007 #ifdef CONFIG_TOUCHSCREEN_USB_ETURBO
1008         [DEVTYPE_ETURBO] = {
1009                 .min_xc         = 0x0,
1010                 .max_xc         = 0x07ff,
1011                 .min_yc         = 0x0,
1012                 .max_yc         = 0x07ff,
1013                 .rept_size      = 8,
1014                 .process_pkt    = usbtouch_process_multi,
1015                 .get_pkt_len    = eturbo_get_pkt_len,
1016                 .read_data      = eturbo_read_data,
1017         },
1018 #endif
1019
1020 #ifdef CONFIG_TOUCHSCREEN_USB_GUNZE
1021         [DEVTYPE_GUNZE] = {
1022                 .min_xc         = 0x0,
1023                 .max_xc         = 0x0fff,
1024                 .min_yc         = 0x0,
1025                 .max_yc         = 0x0fff,
1026                 .rept_size      = 4,
1027                 .read_data      = gunze_read_data,
1028         },
1029 #endif
1030
1031 #ifdef CONFIG_TOUCHSCREEN_USB_DMC_TSC10
1032         [DEVTYPE_DMC_TSC10] = {
1033                 .min_xc         = 0x0,
1034                 .max_xc         = 0x03ff,
1035                 .min_yc         = 0x0,
1036                 .max_yc         = 0x03ff,
1037                 .rept_size      = 5,
1038                 .init           = dmc_tsc10_init,
1039                 .read_data      = dmc_tsc10_read_data,
1040         },
1041 #endif
1042
1043 #ifdef CONFIG_TOUCHSCREEN_USB_IRTOUCH
1044         [DEVTYPE_IRTOUCH] = {
1045                 .min_xc         = 0x0,
1046                 .max_xc         = 0x0fff,
1047                 .min_yc         = 0x0,
1048                 .max_yc         = 0x0fff,
1049                 .rept_size      = 8,
1050                 .read_data      = irtouch_read_data,
1051         },
1052 #endif
1053
1054 #ifdef CONFIG_TOUCHSCREEN_USB_IDEALTEK
1055         [DEVTYPE_IDEALTEK] = {
1056                 .min_xc         = 0x0,
1057                 .max_xc         = 0x0fff,
1058                 .min_yc         = 0x0,
1059                 .max_yc         = 0x0fff,
1060                 .rept_size      = 8,
1061                 .process_pkt    = usbtouch_process_multi,
1062                 .get_pkt_len    = idealtek_get_pkt_len,
1063                 .read_data      = idealtek_read_data,
1064         },
1065 #endif
1066
1067 #ifdef CONFIG_TOUCHSCREEN_USB_GENERAL_TOUCH
1068         [DEVTYPE_GENERAL_TOUCH] = {
1069                 .min_xc         = 0x0,
1070                 .max_xc         = 0x7fff,
1071                 .min_yc         = 0x0,
1072                 .max_yc         = 0x7fff,
1073                 .rept_size      = 7,
1074                 .read_data      = general_touch_read_data,
1075         },
1076 #endif
1077
1078 #ifdef CONFIG_TOUCHSCREEN_USB_GOTOP
1079         [DEVTYPE_GOTOP] = {
1080                 .min_xc         = 0x0,
1081                 .max_xc         = 0x03ff,
1082                 .min_yc         = 0x0,
1083                 .max_yc         = 0x03ff,
1084                 .rept_size      = 4,
1085                 .read_data      = gotop_read_data,
1086         },
1087 #endif
1088
1089 #ifdef CONFIG_TOUCHSCREEN_USB_JASTEC
1090         [DEVTYPE_JASTEC] = {
1091                 .min_xc         = 0x0,
1092                 .max_xc         = 0x0fff,
1093                 .min_yc         = 0x0,
1094                 .max_yc         = 0x0fff,
1095                 .rept_size      = 4,
1096                 .read_data      = jastec_read_data,
1097         },
1098 #endif
1099
1100 #ifdef CONFIG_TOUCHSCREEN_USB_E2I
1101         [DEVTYPE_E2I] = {
1102                 .min_xc         = 0x0,
1103                 .max_xc         = 0x7fff,
1104                 .min_yc         = 0x0,
1105                 .max_yc         = 0x7fff,
1106                 .rept_size      = 6,
1107                 .init           = e2i_init,
1108                 .read_data      = e2i_read_data,
1109         },
1110 #endif
1111
1112 #ifdef CONFIG_TOUCHSCREEN_USB_ZYTRONIC
1113         [DEVTYPE_ZYTRONIC] = {
1114                 .min_xc         = 0x0,
1115                 .max_xc         = 0x03ff,
1116                 .min_yc         = 0x0,
1117                 .max_yc         = 0x03ff,
1118                 .rept_size      = 5,
1119                 .read_data      = zytronic_read_data,
1120                 .irq_always     = true,
1121         },
1122 #endif
1123
1124 #ifdef CONFIG_TOUCHSCREEN_USB_ETT_TC45USB
1125         [DEVTYPE_TC45USB] = {
1126                 .min_xc         = 0x0,
1127                 .max_xc         = 0x0fff,
1128                 .min_yc         = 0x0,
1129                 .max_yc         = 0x0fff,
1130                 .rept_size      = 5,
1131                 .read_data      = tc45usb_read_data,
1132         },
1133 #endif
1134
1135 #ifdef CONFIG_TOUCHSCREEN_USB_NEXIO
1136         [DEVTYPE_NEXIO] = {
1137                 .rept_size      = 1024,
1138                 .irq_always     = true,
1139                 .read_data      = nexio_read_data,
1140                 .alloc          = nexio_alloc,
1141                 .init           = nexio_init,
1142                 .exit           = nexio_exit,
1143         },
1144 #endif
1145 };
1146
1147
1148 /*****************************************************************************
1149  * Generic Part
1150  */
1151 static void usbtouch_process_pkt(struct usbtouch_usb *usbtouch,
1152                                  unsigned char *pkt, int len)
1153 {
1154         struct usbtouch_device_info *type = usbtouch->type;
1155
1156         if (!type->read_data(usbtouch, pkt))
1157                         return;
1158
1159         input_report_key(usbtouch->input, BTN_TOUCH, usbtouch->touch);
1160
1161         if (swap_xy) {
1162                 input_report_abs(usbtouch->input, ABS_X, usbtouch->y);
1163                 input_report_abs(usbtouch->input, ABS_Y, usbtouch->x);
1164         } else {
1165                 input_report_abs(usbtouch->input, ABS_X, usbtouch->x);
1166                 input_report_abs(usbtouch->input, ABS_Y, usbtouch->y);
1167         }
1168         if (type->max_press)
1169                 input_report_abs(usbtouch->input, ABS_PRESSURE, usbtouch->press);
1170         input_sync(usbtouch->input);
1171 }
1172
1173
1174 #ifdef MULTI_PACKET
1175 static void usbtouch_process_multi(struct usbtouch_usb *usbtouch,
1176                                    unsigned char *pkt, int len)
1177 {
1178         unsigned char *buffer;
1179         int pkt_len, pos, buf_len, tmp;
1180
1181         /* process buffer */
1182         if (unlikely(usbtouch->buf_len)) {
1183                 /* try to get size */
1184                 pkt_len = usbtouch->type->get_pkt_len(
1185                                 usbtouch->buffer, usbtouch->buf_len);
1186
1187                 /* drop? */
1188                 if (unlikely(!pkt_len))
1189                         goto out_flush_buf;
1190
1191                 /* need to append -pkt_len bytes before able to get size */
1192                 if (unlikely(pkt_len < 0)) {
1193                         int append = -pkt_len;
1194                         if (unlikely(append > len))
1195                                append = len;
1196                         if (usbtouch->buf_len + append >= usbtouch->type->rept_size)
1197                                 goto out_flush_buf;
1198                         memcpy(usbtouch->buffer + usbtouch->buf_len, pkt, append);
1199                         usbtouch->buf_len += append;
1200
1201                         pkt_len = usbtouch->type->get_pkt_len(
1202                                         usbtouch->buffer, usbtouch->buf_len);
1203                         if (pkt_len < 0)
1204                                 return;
1205                 }
1206
1207                 /* append */
1208                 tmp = pkt_len - usbtouch->buf_len;
1209                 if (usbtouch->buf_len + tmp >= usbtouch->type->rept_size)
1210                         goto out_flush_buf;
1211                 memcpy(usbtouch->buffer + usbtouch->buf_len, pkt, tmp);
1212                 usbtouch_process_pkt(usbtouch, usbtouch->buffer, pkt_len);
1213
1214                 buffer = pkt + tmp;
1215                 buf_len = len - tmp;
1216         } else {
1217                 buffer = pkt;
1218                 buf_len = len;
1219         }
1220
1221         /* loop over the received packet, process */
1222         pos = 0;
1223         while (pos < buf_len) {
1224                 /* get packet len */
1225                 pkt_len = usbtouch->type->get_pkt_len(buffer + pos,
1226                                                         buf_len - pos);
1227
1228                 /* unknown packet: skip one byte */
1229                 if (unlikely(!pkt_len)) {
1230                         pos++;
1231                         continue;
1232                 }
1233
1234                 /* full packet: process */
1235                 if (likely((pkt_len > 0) && (pkt_len <= buf_len - pos))) {
1236                         usbtouch_process_pkt(usbtouch, buffer + pos, pkt_len);
1237                 } else {
1238                         /* incomplete packet: save in buffer */
1239                         memcpy(usbtouch->buffer, buffer + pos, buf_len - pos);
1240                         usbtouch->buf_len = buf_len - pos;
1241                         return;
1242                 }
1243                 pos += pkt_len;
1244         }
1245
1246 out_flush_buf:
1247         usbtouch->buf_len = 0;
1248         return;
1249 }
1250 #endif
1251
1252
1253 static void usbtouch_irq(struct urb *urb)
1254 {
1255         struct usbtouch_usb *usbtouch = urb->context;
1256         int retval;
1257
1258         switch (urb->status) {
1259         case 0:
1260                 /* success */
1261                 break;
1262         case -ETIME:
1263                 /* this urb is timing out */
1264                 dbg("%s - urb timed out - was the device unplugged?",
1265                     __func__);
1266                 return;
1267         case -ECONNRESET:
1268         case -ENOENT:
1269         case -ESHUTDOWN:
1270         case -EPIPE:
1271                 /* this urb is terminated, clean up */
1272                 dbg("%s - urb shutting down with status: %d",
1273                     __func__, urb->status);
1274                 return;
1275         default:
1276                 dbg("%s - nonzero urb status received: %d",
1277                     __func__, urb->status);
1278                 goto exit;
1279         }
1280
1281         usbtouch->type->process_pkt(usbtouch, usbtouch->data, urb->actual_length);
1282
1283 exit:
1284         usb_mark_last_busy(interface_to_usbdev(usbtouch->interface));
1285         retval = usb_submit_urb(urb, GFP_ATOMIC);
1286         if (retval)
1287                 err("%s - usb_submit_urb failed with result: %d",
1288                     __func__, retval);
1289 }
1290
1291 static int usbtouch_open(struct input_dev *input)
1292 {
1293         struct usbtouch_usb *usbtouch = input_get_drvdata(input);
1294         int r;
1295
1296         usbtouch->irq->dev = interface_to_usbdev(usbtouch->interface);
1297
1298         r = usb_autopm_get_interface(usbtouch->interface) ? -EIO : 0;
1299         if (r < 0)
1300                 goto out;
1301
1302         if (!usbtouch->type->irq_always) {
1303                 if (usb_submit_urb(usbtouch->irq, GFP_KERNEL)) {
1304                         r = -EIO;
1305                         goto out_put;
1306                 }
1307         }
1308
1309         usbtouch->interface->needs_remote_wakeup = 1;
1310 out_put:
1311         usb_autopm_put_interface(usbtouch->interface);
1312 out:
1313         return r;
1314 }
1315
1316 static void usbtouch_close(struct input_dev *input)
1317 {
1318         struct usbtouch_usb *usbtouch = input_get_drvdata(input);
1319         int r;
1320
1321         if (!usbtouch->type->irq_always)
1322                 usb_kill_urb(usbtouch->irq);
1323         r = usb_autopm_get_interface(usbtouch->interface);
1324         usbtouch->interface->needs_remote_wakeup = 0;
1325         if (!r)
1326                 usb_autopm_put_interface(usbtouch->interface);
1327 }
1328
1329 static int usbtouch_suspend
1330 (struct usb_interface *intf, pm_message_t message)
1331 {
1332         struct usbtouch_usb *usbtouch = usb_get_intfdata(intf);
1333
1334         usb_kill_urb(usbtouch->irq);
1335
1336         return 0;
1337 }
1338
1339 static int usbtouch_resume(struct usb_interface *intf)
1340 {
1341         struct usbtouch_usb *usbtouch = usb_get_intfdata(intf);
1342         struct input_dev *input = usbtouch->input;
1343         int result = 0;
1344
1345         mutex_lock(&input->mutex);
1346         if (input->users || usbtouch->type->irq_always)
1347                 result = usb_submit_urb(usbtouch->irq, GFP_NOIO);
1348         mutex_unlock(&input->mutex);
1349
1350         return result;
1351 }
1352
1353 static int usbtouch_reset_resume(struct usb_interface *intf)
1354 {
1355         struct usbtouch_usb *usbtouch = usb_get_intfdata(intf);
1356         struct input_dev *input = usbtouch->input;
1357         int err = 0;
1358
1359         /* reinit the device */
1360         if (usbtouch->type->init) {
1361                 err = usbtouch->type->init(usbtouch);
1362                 if (err) {
1363                         dbg("%s - type->init() failed, err: %d",
1364                             __func__, err);
1365                         return err;
1366                 }
1367         }
1368
1369         /* restart IO if needed */
1370         mutex_lock(&input->mutex);
1371         if (input->users)
1372                 err = usb_submit_urb(usbtouch->irq, GFP_NOIO);
1373         mutex_unlock(&input->mutex);
1374
1375         return err;
1376 }
1377
1378 static void usbtouch_free_buffers(struct usb_device *udev,
1379                                   struct usbtouch_usb *usbtouch)
1380 {
1381         usb_free_coherent(udev, usbtouch->type->rept_size,
1382                           usbtouch->data, usbtouch->data_dma);
1383         kfree(usbtouch->buffer);
1384 }
1385
1386 static struct usb_endpoint_descriptor *
1387 usbtouch_get_input_endpoint(struct usb_host_interface *interface)
1388 {
1389         int i;
1390
1391         for (i = 0; i < interface->desc.bNumEndpoints; i++)
1392                 if (usb_endpoint_dir_in(&interface->endpoint[i].desc))
1393                         return &interface->endpoint[i].desc;
1394
1395         return NULL;
1396 }
1397
1398 static int usbtouch_probe(struct usb_interface *intf,
1399                           const struct usb_device_id *id)
1400 {
1401         struct usbtouch_usb *usbtouch;
1402         struct input_dev *input_dev;
1403         struct usb_endpoint_descriptor *endpoint;
1404         struct usb_device *udev = interface_to_usbdev(intf);
1405         struct usbtouch_device_info *type;
1406         int err = -ENOMEM;
1407
1408         /* some devices are ignored */
1409         if (id->driver_info == DEVTYPE_IGNORE)
1410                 return -ENODEV;
1411
1412         endpoint = usbtouch_get_input_endpoint(intf->cur_altsetting);
1413         if (!endpoint)
1414                 return -ENXIO;
1415
1416         usbtouch = kzalloc(sizeof(struct usbtouch_usb), GFP_KERNEL);
1417         input_dev = input_allocate_device();
1418         if (!usbtouch || !input_dev)
1419                 goto out_free;
1420
1421         type = &usbtouch_dev_info[id->driver_info];
1422         usbtouch->type = type;
1423         if (!type->process_pkt)
1424                 type->process_pkt = usbtouch_process_pkt;
1425
1426         usbtouch->data = usb_alloc_coherent(udev, type->rept_size,
1427                                             GFP_KERNEL, &usbtouch->data_dma);
1428         if (!usbtouch->data)
1429                 goto out_free;
1430
1431         if (type->get_pkt_len) {
1432                 usbtouch->buffer = kmalloc(type->rept_size, GFP_KERNEL);
1433                 if (!usbtouch->buffer)
1434                         goto out_free_buffers;
1435         }
1436
1437         usbtouch->irq = usb_alloc_urb(0, GFP_KERNEL);
1438         if (!usbtouch->irq) {
1439                 dbg("%s - usb_alloc_urb failed: usbtouch->irq", __func__);
1440                 goto out_free_buffers;
1441         }
1442
1443         usbtouch->interface = intf;
1444         usbtouch->input = input_dev;
1445
1446         if (udev->manufacturer)
1447                 strlcpy(usbtouch->name, udev->manufacturer, sizeof(usbtouch->name));
1448
1449         if (udev->product) {
1450                 if (udev->manufacturer)
1451                         strlcat(usbtouch->name, " ", sizeof(usbtouch->name));
1452                 strlcat(usbtouch->name, udev->product, sizeof(usbtouch->name));
1453         }
1454
1455         if (!strlen(usbtouch->name))
1456                 snprintf(usbtouch->name, sizeof(usbtouch->name),
1457                         "USB Touchscreen %04x:%04x",
1458                          le16_to_cpu(udev->descriptor.idVendor),
1459                          le16_to_cpu(udev->descriptor.idProduct));
1460
1461         usb_make_path(udev, usbtouch->phys, sizeof(usbtouch->phys));
1462         strlcat(usbtouch->phys, "/input0", sizeof(usbtouch->phys));
1463
1464         input_dev->name = usbtouch->name;
1465         input_dev->phys = usbtouch->phys;
1466         usb_to_input_id(udev, &input_dev->id);
1467         input_dev->dev.parent = &intf->dev;
1468
1469         input_set_drvdata(input_dev, usbtouch);
1470
1471         input_dev->open = usbtouch_open;
1472         input_dev->close = usbtouch_close;
1473
1474         input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
1475         input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
1476         input_set_abs_params(input_dev, ABS_X, type->min_xc, type->max_xc, 0, 0);
1477         input_set_abs_params(input_dev, ABS_Y, type->min_yc, type->max_yc, 0, 0);
1478         if (type->max_press)
1479                 input_set_abs_params(input_dev, ABS_PRESSURE, type->min_press,
1480                                      type->max_press, 0, 0);
1481
1482         if (usb_endpoint_type(endpoint) == USB_ENDPOINT_XFER_INT)
1483                 usb_fill_int_urb(usbtouch->irq, udev,
1484                          usb_rcvintpipe(udev, endpoint->bEndpointAddress),
1485                          usbtouch->data, type->rept_size,
1486                          usbtouch_irq, usbtouch, endpoint->bInterval);
1487         else
1488                 usb_fill_bulk_urb(usbtouch->irq, udev,
1489                          usb_rcvbulkpipe(udev, endpoint->bEndpointAddress),
1490                          usbtouch->data, type->rept_size,
1491                          usbtouch_irq, usbtouch);
1492
1493         usbtouch->irq->dev = udev;
1494         usbtouch->irq->transfer_dma = usbtouch->data_dma;
1495         usbtouch->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1496
1497         /* device specific allocations */
1498         if (type->alloc) {
1499                 err = type->alloc(usbtouch);
1500                 if (err) {
1501                         dbg("%s - type->alloc() failed, err: %d", __func__, err);
1502                         goto out_free_urb;
1503                 }
1504         }
1505
1506         /* device specific initialisation*/
1507         if (type->init) {
1508                 err = type->init(usbtouch);
1509                 if (err) {
1510                         dbg("%s - type->init() failed, err: %d", __func__, err);
1511                         goto out_do_exit;
1512                 }
1513         }
1514
1515         err = input_register_device(usbtouch->input);
1516         if (err) {
1517                 dbg("%s - input_register_device failed, err: %d", __func__, err);
1518                 goto out_do_exit;
1519         }
1520
1521         usb_set_intfdata(intf, usbtouch);
1522
1523         if (usbtouch->type->irq_always) {
1524                 /* this can't fail */
1525                 usb_autopm_get_interface(intf);
1526                 err = usb_submit_urb(usbtouch->irq, GFP_KERNEL);
1527                 if (err) {
1528                         usb_autopm_put_interface(intf);
1529                         err("%s - usb_submit_urb failed with result: %d",
1530                             __func__, err);
1531                         goto out_unregister_input;
1532                 }
1533         }
1534
1535         return 0;
1536
1537 out_unregister_input:
1538         input_unregister_device(input_dev);
1539         input_dev = NULL;
1540 out_do_exit:
1541         if (type->exit)
1542                 type->exit(usbtouch);
1543 out_free_urb:
1544         usb_free_urb(usbtouch->irq);
1545 out_free_buffers:
1546         usbtouch_free_buffers(udev, usbtouch);
1547 out_free:
1548         input_free_device(input_dev);
1549         kfree(usbtouch);
1550         return err;
1551 }
1552
1553 static void usbtouch_disconnect(struct usb_interface *intf)
1554 {
1555         struct usbtouch_usb *usbtouch = usb_get_intfdata(intf);
1556
1557         dbg("%s - called", __func__);
1558
1559         if (!usbtouch)
1560                 return;
1561
1562         dbg("%s - usbtouch is initialized, cleaning up", __func__);
1563         usb_set_intfdata(intf, NULL);
1564         /* this will stop IO via close */
1565         input_unregister_device(usbtouch->input);
1566         usb_free_urb(usbtouch->irq);
1567         if (usbtouch->type->exit)
1568                 usbtouch->type->exit(usbtouch);
1569         usbtouch_free_buffers(interface_to_usbdev(intf), usbtouch);
1570         kfree(usbtouch);
1571 }
1572
1573 MODULE_DEVICE_TABLE(usb, usbtouch_devices);
1574
1575 static struct usb_driver usbtouch_driver = {
1576         .name           = "usbtouchscreen",
1577         .probe          = usbtouch_probe,
1578         .disconnect     = usbtouch_disconnect,
1579         .suspend        = usbtouch_suspend,
1580         .resume         = usbtouch_resume,
1581         .reset_resume   = usbtouch_reset_resume,
1582         .id_table       = usbtouch_devices,
1583         .supports_autosuspend = 1,
1584 };
1585
1586 static int __init usbtouch_init(void)
1587 {
1588         return usb_register(&usbtouch_driver);
1589 }
1590
1591 static void __exit usbtouch_cleanup(void)
1592 {
1593         usb_deregister(&usbtouch_driver);
1594 }
1595
1596 module_init(usbtouch_init);
1597 module_exit(usbtouch_cleanup);
1598
1599 MODULE_AUTHOR(DRIVER_AUTHOR);
1600 MODULE_DESCRIPTION(DRIVER_DESC);
1601 MODULE_LICENSE("GPL");
1602
1603 MODULE_ALIAS("touchkitusb");
1604 MODULE_ALIAS("itmtouch");
1605 MODULE_ALIAS("mtouchusb");