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