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