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