• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/linux/linux-2.6.36/drivers/input/touchscreen/
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  (*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 */
104struct 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 */
123enum {
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
152static 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
250static 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
264static 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
293static 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
305static 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
327static 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
347static 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
361static 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
404static 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
441static 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
457static 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
472static 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
508static 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);
548err_out:
549	kfree(buf);
550err_nobuf:
551	return ret;
552}
553
554
555static 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
570static 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
584static 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
601static 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
610static 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
637static 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
652static 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
666static 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
680static 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
719struct nexio_priv {
720	struct urb *ack;
721	unsigned char *ack_buf;
722};
723
724struct 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
732static unsigned char nexio_ack_pkt[2] = { 0xaa, 0x02 };
733static unsigned char nexio_init_pkt[4] = { 0x82, 0x04, 0x0a, 0x0f };
734
735static void nexio_ack_complete(struct urb *urb)
736{
737}
738
739static 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
763err_ack_buf:
764	kfree(priv->ack_buf);
765err_priv:
766	kfree(priv);
767out_buf:
768	return ret;
769}
770
771static 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
846out_buf:
847	kfree(buf);
848	return ret;
849}
850
851static 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
861static 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					/* single touch */
916					usbtouch->x = 2 * begin_x + w;
917					usbtouch->y = 2 * begin_y + h;
918					usbtouch->touch = packet->flags & 0x01;
919					begin_y = end_y = -1;
920					return 1;
921				}
922			}
923			begin_x = end_x = -1;
924		}
925
926	}
927	return 0;
928}
929#endif
930
931
932/*****************************************************************************
933 * the different device descriptors
934 */
935#ifdef MULTI_PACKET
936static void usbtouch_process_multi(struct usbtouch_usb *usbtouch,
937				   unsigned char *pkt, int len);
938#endif
939
940static struct usbtouch_device_info usbtouch_dev_info[] = {
941#ifdef CONFIG_TOUCHSCREEN_USB_EGALAX
942	[DEVTYPE_EGALAX] = {
943		.min_xc		= 0x0,
944		.max_xc		= 0x07ff,
945		.min_yc		= 0x0,
946		.max_yc		= 0x07ff,
947		.rept_size	= 16,
948		.process_pkt	= usbtouch_process_multi,
949		.get_pkt_len	= egalax_get_pkt_len,
950		.read_data	= egalax_read_data,
951	},
952#endif
953
954#ifdef CONFIG_TOUCHSCREEN_USB_PANJIT
955	[DEVTYPE_PANJIT] = {
956		.min_xc		= 0x0,
957		.max_xc		= 0x0fff,
958		.min_yc		= 0x0,
959		.max_yc		= 0x0fff,
960		.rept_size	= 8,
961		.read_data	= panjit_read_data,
962	},
963#endif
964
965#ifdef CONFIG_TOUCHSCREEN_USB_3M
966	[DEVTYPE_3M] = {
967		.min_xc		= 0x0,
968		.max_xc		= 0x4000,
969		.min_yc		= 0x0,
970		.max_yc		= 0x4000,
971		.rept_size	= 11,
972		.read_data	= mtouch_read_data,
973		.init		= mtouch_init,
974	},
975#endif
976
977#ifdef CONFIG_TOUCHSCREEN_USB_ITM
978	[DEVTYPE_ITM] = {
979		.min_xc		= 0x0,
980		.max_xc		= 0x0fff,
981		.min_yc		= 0x0,
982		.max_yc		= 0x0fff,
983		.max_press	= 0xff,
984		.rept_size	= 8,
985		.read_data	= itm_read_data,
986	},
987#endif
988
989#ifdef CONFIG_TOUCHSCREEN_USB_ETURBO
990	[DEVTYPE_ETURBO] = {
991		.min_xc		= 0x0,
992		.max_xc		= 0x07ff,
993		.min_yc		= 0x0,
994		.max_yc		= 0x07ff,
995		.rept_size	= 8,
996		.process_pkt	= usbtouch_process_multi,
997		.get_pkt_len	= eturbo_get_pkt_len,
998		.read_data	= eturbo_read_data,
999	},
1000#endif
1001
1002#ifdef CONFIG_TOUCHSCREEN_USB_GUNZE
1003	[DEVTYPE_GUNZE] = {
1004		.min_xc		= 0x0,
1005		.max_xc		= 0x0fff,
1006		.min_yc		= 0x0,
1007		.max_yc		= 0x0fff,
1008		.rept_size	= 4,
1009		.read_data	= gunze_read_data,
1010	},
1011#endif
1012
1013#ifdef CONFIG_TOUCHSCREEN_USB_DMC_TSC10
1014	[DEVTYPE_DMC_TSC10] = {
1015		.min_xc		= 0x0,
1016		.max_xc		= 0x03ff,
1017		.min_yc		= 0x0,
1018		.max_yc		= 0x03ff,
1019		.rept_size	= 5,
1020		.init		= dmc_tsc10_init,
1021		.read_data	= dmc_tsc10_read_data,
1022	},
1023#endif
1024
1025#ifdef CONFIG_TOUCHSCREEN_USB_IRTOUCH
1026	[DEVTYPE_IRTOUCH] = {
1027		.min_xc		= 0x0,
1028		.max_xc		= 0x0fff,
1029		.min_yc		= 0x0,
1030		.max_yc		= 0x0fff,
1031		.rept_size	= 8,
1032		.read_data	= irtouch_read_data,
1033	},
1034#endif
1035
1036#ifdef CONFIG_TOUCHSCREEN_USB_IDEALTEK
1037	[DEVTYPE_IDEALTEK] = {
1038		.min_xc		= 0x0,
1039		.max_xc		= 0x0fff,
1040		.min_yc		= 0x0,
1041		.max_yc		= 0x0fff,
1042		.rept_size	= 8,
1043		.process_pkt	= usbtouch_process_multi,
1044		.get_pkt_len	= idealtek_get_pkt_len,
1045		.read_data	= idealtek_read_data,
1046	},
1047#endif
1048
1049#ifdef CONFIG_TOUCHSCREEN_USB_GENERAL_TOUCH
1050	[DEVTYPE_GENERAL_TOUCH] = {
1051		.min_xc		= 0x0,
1052		.max_xc		= 0x7fff,
1053		.min_yc		= 0x0,
1054		.max_yc		= 0x7fff,
1055		.rept_size	= 7,
1056		.read_data	= general_touch_read_data,
1057	},
1058#endif
1059
1060#ifdef CONFIG_TOUCHSCREEN_USB_GOTOP
1061	[DEVTYPE_GOTOP] = {
1062		.min_xc		= 0x0,
1063		.max_xc		= 0x03ff,
1064		.min_yc		= 0x0,
1065		.max_yc		= 0x03ff,
1066		.rept_size	= 4,
1067		.read_data	= gotop_read_data,
1068	},
1069#endif
1070
1071#ifdef CONFIG_TOUCHSCREEN_USB_JASTEC
1072	[DEVTYPE_JASTEC] = {
1073		.min_xc		= 0x0,
1074		.max_xc		= 0x0fff,
1075		.min_yc		= 0x0,
1076		.max_yc		= 0x0fff,
1077		.rept_size	= 4,
1078		.read_data	= jastec_read_data,
1079	},
1080#endif
1081
1082#ifdef CONFIG_TOUCHSCREEN_USB_E2I
1083	[DEVTYPE_E2I] = {
1084		.min_xc		= 0x0,
1085		.max_xc		= 0x7fff,
1086		.min_yc		= 0x0,
1087		.max_yc		= 0x7fff,
1088		.rept_size	= 6,
1089		.init		= e2i_init,
1090		.read_data	= e2i_read_data,
1091	},
1092#endif
1093
1094#ifdef CONFIG_TOUCHSCREEN_USB_ZYTRONIC
1095	[DEVTYPE_ZYTRONIC] = {
1096		.min_xc		= 0x0,
1097		.max_xc		= 0x03ff,
1098		.min_yc		= 0x0,
1099		.max_yc		= 0x03ff,
1100		.rept_size	= 5,
1101		.read_data	= zytronic_read_data,
1102		.irq_always     = true,
1103	},
1104#endif
1105
1106#ifdef CONFIG_TOUCHSCREEN_USB_ETT_TC45USB
1107	[DEVTYPE_TC45USB] = {
1108		.min_xc		= 0x0,
1109		.max_xc		= 0x0fff,
1110		.min_yc		= 0x0,
1111		.max_yc		= 0x0fff,
1112		.rept_size	= 5,
1113		.read_data	= tc45usb_read_data,
1114	},
1115#endif
1116
1117#ifdef CONFIG_TOUCHSCREEN_USB_NEXIO
1118	[DEVTYPE_NEXIO] = {
1119		.rept_size	= 1024,
1120		.irq_always	= true,
1121		.read_data	= nexio_read_data,
1122		.alloc		= nexio_alloc,
1123		.init		= nexio_init,
1124		.exit		= nexio_exit,
1125	},
1126#endif
1127};
1128
1129
1130/*****************************************************************************
1131 * Generic Part
1132 */
1133static void usbtouch_process_pkt(struct usbtouch_usb *usbtouch,
1134                                 unsigned char *pkt, int len)
1135{
1136	struct usbtouch_device_info *type = usbtouch->type;
1137
1138	if (!type->read_data(usbtouch, pkt))
1139			return;
1140
1141	input_report_key(usbtouch->input, BTN_TOUCH, usbtouch->touch);
1142
1143	if (swap_xy) {
1144		input_report_abs(usbtouch->input, ABS_X, usbtouch->y);
1145		input_report_abs(usbtouch->input, ABS_Y, usbtouch->x);
1146	} else {
1147		input_report_abs(usbtouch->input, ABS_X, usbtouch->x);
1148		input_report_abs(usbtouch->input, ABS_Y, usbtouch->y);
1149	}
1150	if (type->max_press)
1151		input_report_abs(usbtouch->input, ABS_PRESSURE, usbtouch->press);
1152	input_sync(usbtouch->input);
1153}
1154
1155
1156#ifdef MULTI_PACKET
1157static void usbtouch_process_multi(struct usbtouch_usb *usbtouch,
1158                                   unsigned char *pkt, int len)
1159{
1160	unsigned char *buffer;
1161	int pkt_len, pos, buf_len, tmp;
1162
1163	/* process buffer */
1164	if (unlikely(usbtouch->buf_len)) {
1165		/* try to get size */
1166		pkt_len = usbtouch->type->get_pkt_len(
1167				usbtouch->buffer, usbtouch->buf_len);
1168
1169		/* drop? */
1170		if (unlikely(!pkt_len))
1171			goto out_flush_buf;
1172
1173		/* need to append -pkt_len bytes before able to get size */
1174		if (unlikely(pkt_len < 0)) {
1175			int append = -pkt_len;
1176			if (unlikely(append > len))
1177			       append = len;
1178			if (usbtouch->buf_len + append >= usbtouch->type->rept_size)
1179				goto out_flush_buf;
1180			memcpy(usbtouch->buffer + usbtouch->buf_len, pkt, append);
1181			usbtouch->buf_len += append;
1182
1183			pkt_len = usbtouch->type->get_pkt_len(
1184					usbtouch->buffer, usbtouch->buf_len);
1185			if (pkt_len < 0)
1186				return;
1187		}
1188
1189		/* append */
1190		tmp = pkt_len - usbtouch->buf_len;
1191		if (usbtouch->buf_len + tmp >= usbtouch->type->rept_size)
1192			goto out_flush_buf;
1193		memcpy(usbtouch->buffer + usbtouch->buf_len, pkt, tmp);
1194		usbtouch_process_pkt(usbtouch, usbtouch->buffer, pkt_len);
1195
1196		buffer = pkt + tmp;
1197		buf_len = len - tmp;
1198	} else {
1199		buffer = pkt;
1200		buf_len = len;
1201	}
1202
1203	/* loop over the received packet, process */
1204	pos = 0;
1205	while (pos < buf_len) {
1206		/* get packet len */
1207		pkt_len = usbtouch->type->get_pkt_len(buffer + pos,
1208							buf_len - pos);
1209
1210		/* unknown packet: skip one byte */
1211		if (unlikely(!pkt_len)) {
1212			pos++;
1213			continue;
1214		}
1215
1216		/* full packet: process */
1217		if (likely((pkt_len > 0) && (pkt_len <= buf_len - pos))) {
1218			usbtouch_process_pkt(usbtouch, buffer + pos, pkt_len);
1219		} else {
1220			/* incomplete packet: save in buffer */
1221			memcpy(usbtouch->buffer, buffer + pos, buf_len - pos);
1222			usbtouch->buf_len = buf_len - pos;
1223			return;
1224		}
1225		pos += pkt_len;
1226	}
1227
1228out_flush_buf:
1229	usbtouch->buf_len = 0;
1230	return;
1231}
1232#endif
1233
1234
1235static void usbtouch_irq(struct urb *urb)
1236{
1237	struct usbtouch_usb *usbtouch = urb->context;
1238	int retval;
1239
1240	switch (urb->status) {
1241	case 0:
1242		/* success */
1243		break;
1244	case -ETIME:
1245		/* this urb is timing out */
1246		dbg("%s - urb timed out - was the device unplugged?",
1247		    __func__);
1248		return;
1249	case -ECONNRESET:
1250	case -ENOENT:
1251	case -ESHUTDOWN:
1252	case -EPIPE:
1253		/* this urb is terminated, clean up */
1254		dbg("%s - urb shutting down with status: %d",
1255		    __func__, urb->status);
1256		return;
1257	default:
1258		dbg("%s - nonzero urb status received: %d",
1259		    __func__, urb->status);
1260		goto exit;
1261	}
1262
1263	usbtouch->type->process_pkt(usbtouch, usbtouch->data, urb->actual_length);
1264
1265exit:
1266	usb_mark_last_busy(interface_to_usbdev(usbtouch->interface));
1267	retval = usb_submit_urb(urb, GFP_ATOMIC);
1268	if (retval)
1269		err("%s - usb_submit_urb failed with result: %d",
1270		    __func__, retval);
1271}
1272
1273static int usbtouch_open(struct input_dev *input)
1274{
1275	struct usbtouch_usb *usbtouch = input_get_drvdata(input);
1276	int r;
1277
1278	usbtouch->irq->dev = interface_to_usbdev(usbtouch->interface);
1279
1280	r = usb_autopm_get_interface(usbtouch->interface) ? -EIO : 0;
1281	if (r < 0)
1282		goto out;
1283
1284	if (!usbtouch->type->irq_always) {
1285		if (usb_submit_urb(usbtouch->irq, GFP_KERNEL)) {
1286			r = -EIO;
1287			goto out_put;
1288		}
1289	}
1290
1291	usbtouch->interface->needs_remote_wakeup = 1;
1292out_put:
1293	usb_autopm_put_interface(usbtouch->interface);
1294out:
1295	return r;
1296}
1297
1298static void usbtouch_close(struct input_dev *input)
1299{
1300	struct usbtouch_usb *usbtouch = input_get_drvdata(input);
1301	int r;
1302
1303	if (!usbtouch->type->irq_always)
1304		usb_kill_urb(usbtouch->irq);
1305	r = usb_autopm_get_interface(usbtouch->interface);
1306	usbtouch->interface->needs_remote_wakeup = 0;
1307	if (!r)
1308		usb_autopm_put_interface(usbtouch->interface);
1309}
1310
1311static int usbtouch_suspend
1312(struct usb_interface *intf, pm_message_t message)
1313{
1314	struct usbtouch_usb *usbtouch = usb_get_intfdata(intf);
1315
1316	usb_kill_urb(usbtouch->irq);
1317
1318	return 0;
1319}
1320
1321static int usbtouch_resume(struct usb_interface *intf)
1322{
1323	struct usbtouch_usb *usbtouch = usb_get_intfdata(intf);
1324	struct input_dev *input = usbtouch->input;
1325	int result = 0;
1326
1327	mutex_lock(&input->mutex);
1328	if (input->users || usbtouch->type->irq_always)
1329		result = usb_submit_urb(usbtouch->irq, GFP_NOIO);
1330	mutex_unlock(&input->mutex);
1331
1332	return result;
1333}
1334
1335static int usbtouch_reset_resume(struct usb_interface *intf)
1336{
1337	struct usbtouch_usb *usbtouch = usb_get_intfdata(intf);
1338	struct input_dev *input = usbtouch->input;
1339	int err = 0;
1340
1341	/* reinit the device */
1342	if (usbtouch->type->init) {
1343		err = usbtouch->type->init(usbtouch);
1344		if (err) {
1345			dbg("%s - type->init() failed, err: %d",
1346			    __func__, err);
1347			return err;
1348		}
1349	}
1350
1351	/* restart IO if needed */
1352	mutex_lock(&input->mutex);
1353	if (input->users)
1354		err = usb_submit_urb(usbtouch->irq, GFP_NOIO);
1355	mutex_unlock(&input->mutex);
1356
1357	return err;
1358}
1359
1360static void usbtouch_free_buffers(struct usb_device *udev,
1361				  struct usbtouch_usb *usbtouch)
1362{
1363	usb_free_coherent(udev, usbtouch->type->rept_size,
1364			  usbtouch->data, usbtouch->data_dma);
1365	kfree(usbtouch->buffer);
1366}
1367
1368static struct usb_endpoint_descriptor *
1369usbtouch_get_input_endpoint(struct usb_host_interface *interface)
1370{
1371	int i;
1372
1373	for (i = 0; i < interface->desc.bNumEndpoints; i++)
1374		if (usb_endpoint_dir_in(&interface->endpoint[i].desc))
1375			return &interface->endpoint[i].desc;
1376
1377	return NULL;
1378}
1379
1380static int usbtouch_probe(struct usb_interface *intf,
1381			  const struct usb_device_id *id)
1382{
1383	struct usbtouch_usb *usbtouch;
1384	struct input_dev *input_dev;
1385	struct usb_endpoint_descriptor *endpoint;
1386	struct usb_device *udev = interface_to_usbdev(intf);
1387	struct usbtouch_device_info *type;
1388	int err = -ENOMEM;
1389
1390	/* some devices are ignored */
1391	if (id->driver_info == DEVTYPE_IGNORE)
1392		return -ENODEV;
1393
1394	endpoint = usbtouch_get_input_endpoint(intf->cur_altsetting);
1395	if (!endpoint)
1396		return -ENXIO;
1397
1398	usbtouch = kzalloc(sizeof(struct usbtouch_usb), GFP_KERNEL);
1399	input_dev = input_allocate_device();
1400	if (!usbtouch || !input_dev)
1401		goto out_free;
1402
1403	type = &usbtouch_dev_info[id->driver_info];
1404	usbtouch->type = type;
1405	if (!type->process_pkt)
1406		type->process_pkt = usbtouch_process_pkt;
1407
1408	usbtouch->data = usb_alloc_coherent(udev, type->rept_size,
1409					    GFP_KERNEL, &usbtouch->data_dma);
1410	if (!usbtouch->data)
1411		goto out_free;
1412
1413	if (type->get_pkt_len) {
1414		usbtouch->buffer = kmalloc(type->rept_size, GFP_KERNEL);
1415		if (!usbtouch->buffer)
1416			goto out_free_buffers;
1417	}
1418
1419	usbtouch->irq = usb_alloc_urb(0, GFP_KERNEL);
1420	if (!usbtouch->irq) {
1421		dbg("%s - usb_alloc_urb failed: usbtouch->irq", __func__);
1422		goto out_free_buffers;
1423	}
1424
1425	usbtouch->interface = intf;
1426	usbtouch->input = input_dev;
1427
1428	if (udev->manufacturer)
1429		strlcpy(usbtouch->name, udev->manufacturer, sizeof(usbtouch->name));
1430
1431	if (udev->product) {
1432		if (udev->manufacturer)
1433			strlcat(usbtouch->name, " ", sizeof(usbtouch->name));
1434		strlcat(usbtouch->name, udev->product, sizeof(usbtouch->name));
1435	}
1436
1437	if (!strlen(usbtouch->name))
1438		snprintf(usbtouch->name, sizeof(usbtouch->name),
1439			"USB Touchscreen %04x:%04x",
1440			 le16_to_cpu(udev->descriptor.idVendor),
1441			 le16_to_cpu(udev->descriptor.idProduct));
1442
1443	usb_make_path(udev, usbtouch->phys, sizeof(usbtouch->phys));
1444	strlcat(usbtouch->phys, "/input0", sizeof(usbtouch->phys));
1445
1446	input_dev->name = usbtouch->name;
1447	input_dev->phys = usbtouch->phys;
1448	usb_to_input_id(udev, &input_dev->id);
1449	input_dev->dev.parent = &intf->dev;
1450
1451	input_set_drvdata(input_dev, usbtouch);
1452
1453	input_dev->open = usbtouch_open;
1454	input_dev->close = usbtouch_close;
1455
1456	input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
1457	input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
1458	input_set_abs_params(input_dev, ABS_X, type->min_xc, type->max_xc, 0, 0);
1459	input_set_abs_params(input_dev, ABS_Y, type->min_yc, type->max_yc, 0, 0);
1460	if (type->max_press)
1461		input_set_abs_params(input_dev, ABS_PRESSURE, type->min_press,
1462		                     type->max_press, 0, 0);
1463
1464	if (usb_endpoint_type(endpoint) == USB_ENDPOINT_XFER_INT)
1465		usb_fill_int_urb(usbtouch->irq, udev,
1466			 usb_rcvintpipe(udev, endpoint->bEndpointAddress),
1467			 usbtouch->data, type->rept_size,
1468			 usbtouch_irq, usbtouch, endpoint->bInterval);
1469	else
1470		usb_fill_bulk_urb(usbtouch->irq, udev,
1471			 usb_rcvbulkpipe(udev, endpoint->bEndpointAddress),
1472			 usbtouch->data, type->rept_size,
1473			 usbtouch_irq, usbtouch);
1474
1475	usbtouch->irq->dev = udev;
1476	usbtouch->irq->transfer_dma = usbtouch->data_dma;
1477	usbtouch->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1478
1479	/* device specific allocations */
1480	if (type->alloc) {
1481		err = type->alloc(usbtouch);
1482		if (err) {
1483			dbg("%s - type->alloc() failed, err: %d", __func__, err);
1484			goto out_free_urb;
1485		}
1486	}
1487
1488	/* device specific initialisation*/
1489	if (type->init) {
1490		err = type->init(usbtouch);
1491		if (err) {
1492			dbg("%s - type->init() failed, err: %d", __func__, err);
1493			goto out_do_exit;
1494		}
1495	}
1496
1497	err = input_register_device(usbtouch->input);
1498	if (err) {
1499		dbg("%s - input_register_device failed, err: %d", __func__, err);
1500		goto out_do_exit;
1501	}
1502
1503	usb_set_intfdata(intf, usbtouch);
1504
1505	if (usbtouch->type->irq_always) {
1506		/* this can't fail */
1507		usb_autopm_get_interface(intf);
1508		err = usb_submit_urb(usbtouch->irq, GFP_KERNEL);
1509		if (err) {
1510			usb_autopm_put_interface(intf);
1511			err("%s - usb_submit_urb failed with result: %d",
1512			    __func__, err);
1513			goto out_unregister_input;
1514		}
1515	}
1516
1517	return 0;
1518
1519out_unregister_input:
1520	input_unregister_device(input_dev);
1521	input_dev = NULL;
1522out_do_exit:
1523	if (type->exit)
1524		type->exit(usbtouch);
1525out_free_urb:
1526	usb_free_urb(usbtouch->irq);
1527out_free_buffers:
1528	usbtouch_free_buffers(udev, usbtouch);
1529out_free:
1530	input_free_device(input_dev);
1531	kfree(usbtouch);
1532	return err;
1533}
1534
1535static void usbtouch_disconnect(struct usb_interface *intf)
1536{
1537	struct usbtouch_usb *usbtouch = usb_get_intfdata(intf);
1538
1539	dbg("%s - called", __func__);
1540
1541	if (!usbtouch)
1542		return;
1543
1544	dbg("%s - usbtouch is initialized, cleaning up", __func__);
1545	usb_set_intfdata(intf, NULL);
1546	/* this will stop IO via close */
1547	input_unregister_device(usbtouch->input);
1548	usb_free_urb(usbtouch->irq);
1549	if (usbtouch->type->exit)
1550		usbtouch->type->exit(usbtouch);
1551	usbtouch_free_buffers(interface_to_usbdev(intf), usbtouch);
1552	kfree(usbtouch);
1553}
1554
1555MODULE_DEVICE_TABLE(usb, usbtouch_devices);
1556
1557static struct usb_driver usbtouch_driver = {
1558	.name		= "usbtouchscreen",
1559	.probe		= usbtouch_probe,
1560	.disconnect	= usbtouch_disconnect,
1561	.suspend	= usbtouch_suspend,
1562	.resume		= usbtouch_resume,
1563	.reset_resume	= usbtouch_reset_resume,
1564	.id_table	= usbtouch_devices,
1565	.supports_autosuspend = 1,
1566};
1567
1568static int __init usbtouch_init(void)
1569{
1570	return usb_register(&usbtouch_driver);
1571}
1572
1573static void __exit usbtouch_cleanup(void)
1574{
1575	usb_deregister(&usbtouch_driver);
1576}
1577
1578module_init(usbtouch_init);
1579module_exit(usbtouch_cleanup);
1580
1581MODULE_AUTHOR(DRIVER_AUTHOR);
1582MODULE_DESCRIPTION(DRIVER_DESC);
1583MODULE_LICENSE("GPL");
1584
1585MODULE_ALIAS("touchkitusb");
1586MODULE_ALIAS("itmtouch");
1587MODULE_ALIAS("mtouchusb");
1588