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