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