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