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