usbtouchscreen.c 43.4 KB
Newer Older
1 2 3 4
/******************************************************************************
 * usbtouchscreen.c
 * Driver for USB Touchscreens, supporting those devices:
 *  - eGalax Touchkit
Daniel Ritz's avatar
Daniel Ritz committed
5 6
 *    includes eTurboTouch CT-410/510/700
 *  - 3M/Microtouch  EX II series
7 8
 *  - ITM
 *  - PanJit TouchSet
Daniel Ritz's avatar
Daniel Ritz committed
9 10
 *  - eTurboTouch
 *  - 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
 *  - EasyTouch USB Dual/Multi touch controller from Data Modul
21
 *
22
 * Copyright (C) 2004-2007 by Daniel Ritz <daniel.ritz@gmx.ch>
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
 * Copyright (C) by Todd E. Johnson (mtouchusb.c)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * Driver is based on touchkitusb.c
 * - ITM parts are from itmtouch.c
 * - 3M parts are from mtouchusb.c
 * - PanJit parts are from an unmerged driver by Lanslott Gish
43 44
 * - DMC TSC 10/25 are from Holger Schurig, with ideas from an unmerged
 *   driver from Marius Vollmer
45 46 47 48 49 50 51 52 53 54
 *
 *****************************************************************************/

//#define DEBUG

#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/input.h>
#include <linux/module.h>
#include <linux/usb.h>
55
#include <linux/usb/input.h>
56
#include <linux/hid.h>
57 58


59
#define DRIVER_VERSION		"v0.6"
60 61 62
#define DRIVER_AUTHOR		"Daniel Ritz <daniel.ritz@gmx.ch>"
#define DRIVER_DESC		"USB Touchscreen Driver"

63
static bool swap_xy;
64 65 66
module_param(swap_xy, bool, 0644);
MODULE_PARM_DESC(swap_xy, "If set X and Y axes are swapped.");

67
static bool hwcalib_xy;
68 69 70
module_param(hwcalib_xy, bool, 0644);
MODULE_PARM_DESC(hwcalib_xy, "If set hw-calibrated X/Y are used if available");

71 72 73 74 75 76 77 78
/* device specifc data/functions */
struct usbtouch_usb;
struct usbtouch_device_info {
	int min_xc, max_xc;
	int min_yc, max_yc;
	int min_press, max_press;
	int rept_size;

79 80 81 82 83 84 85 86 87
	/*
	 * Always service the USB devices irq not just when the input device is
	 * open. This is useful when devices have a watchdog which prevents us
	 * from periodically polling the device. Leave this unset unless your
	 * touchscreen device requires it, as it does consume more of the USB
	 * bandwidth.
	 */
	bool irq_always;

88
	void (*process_pkt) (struct usbtouch_usb *usbtouch, unsigned char *pkt, int len);
89 90 91 92 93 94 95

	/*
	 * used to get the packet len. possible return values:
	 * > 0: packet len
	 * = 0: skip one byte
	 * < 0: -return value more bytes needed
	 */
Daniel Ritz's avatar
Daniel Ritz committed
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 105 106 107
};

/* a usbtouch device */
struct usbtouch_usb {
	unsigned char *data;
	dma_addr_t data_dma;
108
	int data_size;
109 110 111
	unsigned char *buffer;
	int buf_len;
	struct urb *irq;
112
	struct usb_interface *interface;
113 114 115 116
	struct input_dev *input;
	struct usbtouch_device_info *type;
	char name[128];
	char phys[64];
117
	void *priv;
118 119 120

	int x, y;
	int touch, press;
121 122
};

Daniel Ritz's avatar
Daniel Ritz committed
123

124 125
/* device types */
enum {
126
	DEVTYPE_IGNORE = -1,
127 128 129 130
	DEVTYPE_EGALAX,
	DEVTYPE_PANJIT,
	DEVTYPE_3M,
	DEVTYPE_ITM,
Daniel Ritz's avatar
Daniel Ritz committed
131 132
	DEVTYPE_ETURBO,
	DEVTYPE_GUNZE,
133
	DEVTYPE_DMC_TSC10,
134
	DEVTYPE_IRTOUCH,
135
	DEVTYPE_IRTOUCH_HIRES,
136
	DEVTYPE_IDEALTEK,
137
	DEVTYPE_GENERAL_TOUCH,
138
	DEVTYPE_GOTOP,
139
	DEVTYPE_JASTEC,
140
	DEVTYPE_E2I,
141
	DEVTYPE_ZYTRONIC,
142
	DEVTYPE_TC45USB,
143
	DEVTYPE_NEXIO,
144
	DEVTYPE_ELO,
145
	DEVTYPE_ETOUCH,
146 147
};

148 149 150 151 152
#define USB_DEVICE_HID_CLASS(vend, prod) \
	.match_flags = USB_DEVICE_ID_MATCH_INT_CLASS \
		| USB_DEVICE_ID_MATCH_DEVICE, \
	.idVendor = (vend), \
	.idProduct = (prod), \
153
	.bInterfaceClass = USB_INTERFACE_CLASS_HID
154

155
static const struct usb_device_id usbtouch_devices[] = {
156
#ifdef CONFIG_TOUCHSCREEN_USB_EGALAX
157 158 159 160 161
	/* ignore the HID capable devices, handled by usbhid */
	{USB_DEVICE_HID_CLASS(0x0eef, 0x0001), .driver_info = DEVTYPE_IGNORE},
	{USB_DEVICE_HID_CLASS(0x0eef, 0x0002), .driver_info = DEVTYPE_IGNORE},

	/* normal device IDs */
162
	{USB_DEVICE(0x3823, 0x0001), .driver_info = DEVTYPE_EGALAX},
Daniel Ritz's avatar
Daniel Ritz committed
163
	{USB_DEVICE(0x3823, 0x0002), .driver_info = DEVTYPE_EGALAX},
164 165 166
	{USB_DEVICE(0x0123, 0x0001), .driver_info = DEVTYPE_EGALAX},
	{USB_DEVICE(0x0eef, 0x0001), .driver_info = DEVTYPE_EGALAX},
	{USB_DEVICE(0x0eef, 0x0002), .driver_info = DEVTYPE_EGALAX},
Daniel Ritz's avatar
Daniel Ritz committed
167 168
	{USB_DEVICE(0x1234, 0x0001), .driver_info = DEVTYPE_EGALAX},
	{USB_DEVICE(0x1234, 0x0002), .driver_info = DEVTYPE_EGALAX},
169 170
#endif

171
#ifdef CONFIG_TOUCHSCREEN_USB_PANJIT
172 173 174 175 176 177
	{USB_DEVICE(0x134c, 0x0001), .driver_info = DEVTYPE_PANJIT},
	{USB_DEVICE(0x134c, 0x0002), .driver_info = DEVTYPE_PANJIT},
	{USB_DEVICE(0x134c, 0x0003), .driver_info = DEVTYPE_PANJIT},
	{USB_DEVICE(0x134c, 0x0004), .driver_info = DEVTYPE_PANJIT},
#endif

178
#ifdef CONFIG_TOUCHSCREEN_USB_3M
179 180 181
	{USB_DEVICE(0x0596, 0x0001), .driver_info = DEVTYPE_3M},
#endif

182
#ifdef CONFIG_TOUCHSCREEN_USB_ITM
183
	{USB_DEVICE(0x0403, 0xf9e9), .driver_info = DEVTYPE_ITM},
184
	{USB_DEVICE(0x16e3, 0xf9e9), .driver_info = DEVTYPE_ITM},
185 186
#endif

187
#ifdef CONFIG_TOUCHSCREEN_USB_ETURBO
Daniel Ritz's avatar
Daniel Ritz committed
188 189 190
	{USB_DEVICE(0x1234, 0x5678), .driver_info = DEVTYPE_ETURBO},
#endif

191
#ifdef CONFIG_TOUCHSCREEN_USB_GUNZE
Daniel Ritz's avatar
Daniel Ritz committed
192 193 194
	{USB_DEVICE(0x0637, 0x0001), .driver_info = DEVTYPE_GUNZE},
#endif

195
#ifdef CONFIG_TOUCHSCREEN_USB_DMC_TSC10
196 197 198
	{USB_DEVICE(0x0afa, 0x03e8), .driver_info = DEVTYPE_DMC_TSC10},
#endif

199 200 201
#ifdef CONFIG_TOUCHSCREEN_USB_IRTOUCH
	{USB_DEVICE(0x595a, 0x0001), .driver_info = DEVTYPE_IRTOUCH},
	{USB_DEVICE(0x6615, 0x0001), .driver_info = DEVTYPE_IRTOUCH},
202
	{USB_DEVICE(0x6615, 0x0012), .driver_info = DEVTYPE_IRTOUCH_HIRES},
203 204
#endif

205 206 207 208
#ifdef CONFIG_TOUCHSCREEN_USB_IDEALTEK
	{USB_DEVICE(0x1391, 0x1000), .driver_info = DEVTYPE_IDEALTEK},
#endif

209 210 211 212
#ifdef CONFIG_TOUCHSCREEN_USB_GENERAL_TOUCH
	{USB_DEVICE(0x0dfc, 0x0001), .driver_info = DEVTYPE_GENERAL_TOUCH},
#endif

213 214 215 216 217 218
#ifdef CONFIG_TOUCHSCREEN_USB_GOTOP
	{USB_DEVICE(0x08f2, 0x007f), .driver_info = DEVTYPE_GOTOP},
	{USB_DEVICE(0x08f2, 0x00ce), .driver_info = DEVTYPE_GOTOP},
	{USB_DEVICE(0x08f2, 0x00f4), .driver_info = DEVTYPE_GOTOP},
#endif

219 220 221 222
#ifdef CONFIG_TOUCHSCREEN_USB_JASTEC
	{USB_DEVICE(0x0f92, 0x0001), .driver_info = DEVTYPE_JASTEC},
#endif

223 224 225
#ifdef CONFIG_TOUCHSCREEN_USB_E2I
	{USB_DEVICE(0x1ac7, 0x0001), .driver_info = DEVTYPE_E2I},
#endif
226 227 228 229 230

#ifdef CONFIG_TOUCHSCREEN_USB_ZYTRONIC
	{USB_DEVICE(0x14c8, 0x0003), .driver_info = DEVTYPE_ZYTRONIC},
#endif

231 232 233 234 235
#ifdef CONFIG_TOUCHSCREEN_USB_ETT_TC45USB
	/* TC5UH */
	{USB_DEVICE(0x0664, 0x0309), .driver_info = DEVTYPE_TC45USB},
	/* TC4UM */
	{USB_DEVICE(0x0664, 0x0306), .driver_info = DEVTYPE_TC45USB},
236 237
#endif

238 239 240 241 242 243 244 245
#ifdef CONFIG_TOUCHSCREEN_USB_NEXIO
	/* data interface only */
	{USB_DEVICE_AND_INTERFACE_INFO(0x10f0, 0x2002, 0x0a, 0x00, 0x00),
		.driver_info = DEVTYPE_NEXIO},
	{USB_DEVICE_AND_INTERFACE_INFO(0x1870, 0x0001, 0x0a, 0x00, 0x00),
		.driver_info = DEVTYPE_NEXIO},
#endif

246 247 248 249
#ifdef CONFIG_TOUCHSCREEN_USB_ELO
	{USB_DEVICE(0x04e7, 0x0020), .driver_info = DEVTYPE_ELO},
#endif

250 251 252 253
#ifdef CONFIG_TOUCHSCREEN_USB_EASYTOUCH
	{USB_DEVICE(0x7374, 0x0001), .driver_info = DEVTYPE_ETOUCH},
#endif

254 255 256 257
	{}
};


258 259 260 261 262 263 264 265
/*****************************************************************************
 * e2i Part
 */

#ifdef CONFIG_TOUCHSCREEN_USB_E2I
static int e2i_init(struct usbtouch_usb *usbtouch)
{
	int ret;
266
	struct usb_device *udev = interface_to_usbdev(usbtouch->interface);
267

268
	ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
269 270 271
	                      0x01, 0x02, 0x0000, 0x0081,
	                      NULL, 0, USB_CTRL_SET_TIMEOUT);

272
	dev_dbg(&usbtouch->interface->dev,
273 274
		"%s - usb_control_msg - E2I_RESET - bytes|err: %d\n",
		__func__, ret);
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
	return ret;
}

static int e2i_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
{
	int tmp = (pkt[0] << 8) | pkt[1];
	dev->x  = (pkt[2] << 8) | pkt[3];
	dev->y  = (pkt[4] << 8) | pkt[5];

	tmp = tmp - 0xA000;
	dev->touch = (tmp > 0);
	dev->press = (tmp > 0 ? tmp : 0);

	return 1;
}
#endif


293 294 295 296
/*****************************************************************************
 * eGalax part
 */

297
#ifdef CONFIG_TOUCHSCREEN_USB_EGALAX
298

299 300 301 302
#ifndef MULTI_PACKET
#define MULTI_PACKET
#endif

303 304 305 306
#define EGALAX_PKT_TYPE_MASK		0xFE
#define EGALAX_PKT_TYPE_REPT		0x80
#define EGALAX_PKT_TYPE_DIAG		0x0A

307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
static int egalax_init(struct usbtouch_usb *usbtouch)
{
	int ret, i;
	unsigned char *buf;
	struct usb_device *udev = interface_to_usbdev(usbtouch->interface);

	/*
	 * An eGalax diagnostic packet kicks the device into using the right
	 * protocol.  We send a "check active" packet.  The response will be
	 * read later and ignored.
	 */

	buf = kmalloc(3, GFP_KERNEL);
	if (!buf)
		return -ENOMEM;

	buf[0] = EGALAX_PKT_TYPE_DIAG;
	buf[1] = 1;	/* length */
	buf[2] = 'A';	/* command - check active */

	for (i = 0; i < 3; i++) {
		ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
				      0,
				      USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
				      0, 0, buf, 3,
				      USB_CTRL_SET_TIMEOUT);
		if (ret >= 0) {
			ret = 0;
			break;
		}
		if (ret != -EPIPE)
			break;
	}

	kfree(buf);

	return ret;
}

346
static int egalax_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
347 348 349 350
{
	if ((pkt[0] & EGALAX_PKT_TYPE_MASK) != EGALAX_PKT_TYPE_REPT)
		return 0;

351 352 353
	dev->x = ((pkt[3] & 0x0F) << 7) | (pkt[4] & 0x7F);
	dev->y = ((pkt[1] & 0x0F) << 7) | (pkt[2] & 0x7F);
	dev->touch = pkt[0] & 0x01;
354 355 356 357

	return 1;
}

Daniel Ritz's avatar
Daniel Ritz committed
358
static int egalax_get_pkt_len(unsigned char *buf, int len)
359 360 361 362 363 364
{
	switch (buf[0] & EGALAX_PKT_TYPE_MASK) {
	case EGALAX_PKT_TYPE_REPT:
		return 5;

	case EGALAX_PKT_TYPE_DIAG:
Daniel Ritz's avatar
Daniel Ritz committed
365 366 367
		if (len < 2)
			return -1;

368 369 370 371 372 373 374
		return buf[1] + 2;
	}

	return 0;
}
#endif

375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
/*****************************************************************************
 * EasyTouch part
 */

#ifdef CONFIG_TOUCHSCREEN_USB_EASYTOUCH

#ifndef MULTI_PACKET
#define MULTI_PACKET
#endif

#define ETOUCH_PKT_TYPE_MASK		0xFE
#define ETOUCH_PKT_TYPE_REPT		0x80
#define ETOUCH_PKT_TYPE_REPT2		0xB0
#define ETOUCH_PKT_TYPE_DIAG		0x0A

static int etouch_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
{
	if ((pkt[0] & ETOUCH_PKT_TYPE_MASK) != ETOUCH_PKT_TYPE_REPT &&
		(pkt[0] & ETOUCH_PKT_TYPE_MASK) != ETOUCH_PKT_TYPE_REPT2)
		return 0;

	dev->x = ((pkt[1] & 0x1F) << 7) | (pkt[2] & 0x7F);
	dev->y = ((pkt[3] & 0x1F) << 7) | (pkt[4] & 0x7F);
	dev->touch = pkt[0] & 0x01;

	return 1;
}

static int etouch_get_pkt_len(unsigned char *buf, int len)
{
	switch (buf[0] & ETOUCH_PKT_TYPE_MASK) {
	case ETOUCH_PKT_TYPE_REPT:
	case ETOUCH_PKT_TYPE_REPT2:
		return 5;

	case ETOUCH_PKT_TYPE_DIAG:
		if (len < 2)
			return -1;

		return buf[1] + 2;
	}

	return 0;
}
#endif
420 421 422 423

/*****************************************************************************
 * PanJit Part
 */
424
#ifdef CONFIG_TOUCHSCREEN_USB_PANJIT
425
static int panjit_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
426
{
427 428 429
	dev->x = ((pkt[2] & 0x0F) << 8) | pkt[1];
	dev->y = ((pkt[4] & 0x0F) << 8) | pkt[3];
	dev->touch = pkt[0] & 0x01;
430 431 432 433 434 435 436 437 438

	return 1;
}
#endif


/*****************************************************************************
 * 3M/Microtouch Part
 */
439
#ifdef CONFIG_TOUCHSCREEN_USB_3M
440 441 442 443 444

#define MTOUCHUSB_ASYNC_REPORT          1
#define MTOUCHUSB_RESET                 7
#define MTOUCHUSB_REQ_CTRLLR_ID         10

445
static int mtouch_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
446
{
447 448 449 450 451 452 453
	if (hwcalib_xy) {
		dev->x = (pkt[4] << 8) | pkt[3];
		dev->y = 0xffff - ((pkt[6] << 8) | pkt[5]);
	} else {
		dev->x = (pkt[8] << 8) | pkt[7];
		dev->y = (pkt[10] << 8) | pkt[9];
	}
454
	dev->touch = (pkt[2] & 0x40) ? 1 : 0;
455 456 457 458 459 460

	return 1;
}

static int mtouch_init(struct usbtouch_usb *usbtouch)
{
Daniel Ritz's avatar
Daniel Ritz committed
461
	int ret, i;
462
	struct usb_device *udev = interface_to_usbdev(usbtouch->interface);
463

464
	ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
465 466 467
	                      MTOUCHUSB_RESET,
	                      USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
	                      1, 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
468
	dev_dbg(&usbtouch->interface->dev,
469 470
		"%s - usb_control_msg - MTOUCHUSB_RESET - bytes|err: %d\n",
		__func__, ret);
471 472
	if (ret < 0)
		return ret;
Daniel Ritz's avatar
Daniel Ritz committed
473 474 475
	msleep(150);

	for (i = 0; i < 3; i++) {
476
		ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
Daniel Ritz's avatar
Daniel Ritz committed
477 478 479
				      MTOUCHUSB_ASYNC_REPORT,
				      USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
				      1, 1, NULL, 0, USB_CTRL_SET_TIMEOUT);
480
		dev_dbg(&usbtouch->interface->dev,
481 482
			"%s - usb_control_msg - MTOUCHUSB_ASYNC_REPORT - bytes|err: %d\n",
			__func__, ret);
Daniel Ritz's avatar
Daniel Ritz committed
483 484 485 486 487
		if (ret >= 0)
			break;
		if (ret != -EPIPE)
			return ret;
	}
488

489 490 491 492 493 494
	/* Default min/max xy are the raw values, override if using hw-calib */
	if (hwcalib_xy) {
		input_set_abs_params(usbtouch->input, ABS_X, 0, 0xffff, 0, 0);
		input_set_abs_params(usbtouch->input, ABS_Y, 0, 0xffff, 0, 0);
	}

495 496 497 498 499 500 501 502
	return 0;
}
#endif


/*****************************************************************************
 * ITM Part
 */
503
#ifdef CONFIG_TOUCHSCREEN_USB_ITM
504
static int itm_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
505
{
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
	int touch;
	/*
	 * ITM devices report invalid x/y data if not touched.
	 * if the screen was touched before but is not touched any more
	 * report touch as 0 with the last valid x/y data once. then stop
	 * reporting data until touched again.
	 */
	dev->press = ((pkt[2] & 0x01) << 7) | (pkt[5] & 0x7F);

	touch = ~pkt[7] & 0x20;
	if (!touch) {
		if (dev->touch) {
			dev->touch = 0;
			return 1;
		}

		return 0;
	}

	dev->x = ((pkt[0] & 0x1F) << 7) | (pkt[3] & 0x7F);
	dev->y = ((pkt[1] & 0x1F) << 7) | (pkt[4] & 0x7F);
	dev->touch = touch;
528

529
	return 1;
530 531 532 533
}
#endif


Daniel Ritz's avatar
Daniel Ritz committed
534 535 536
/*****************************************************************************
 * eTurboTouch part
 */
537
#ifdef CONFIG_TOUCHSCREEN_USB_ETURBO
538 539 540
#ifndef MULTI_PACKET
#define MULTI_PACKET
#endif
541
static int eturbo_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
Daniel Ritz's avatar
Daniel Ritz committed
542 543 544 545 546 547 548 549
{
	unsigned int shift;

	/* packets should start with sync */
	if (!(pkt[0] & 0x80))
		return 0;

	shift = (6 - (pkt[0] & 0x03));
550 551 552
	dev->x = ((pkt[3] << 7) | pkt[4]) >> shift;
	dev->y = ((pkt[1] << 7) | pkt[2]) >> shift;
	dev->touch = (pkt[0] & 0x10) ? 1 : 0;
Daniel Ritz's avatar
Daniel Ritz committed
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570

	return 1;
}

static int eturbo_get_pkt_len(unsigned char *buf, int len)
{
	if (buf[0] & 0x80)
		return 5;
	if (buf[0] == 0x01)
		return 3;
	return 0;
}
#endif


/*****************************************************************************
 * Gunze part
 */
571
#ifdef CONFIG_TOUCHSCREEN_USB_GUNZE
572
static int gunze_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
Daniel Ritz's avatar
Daniel Ritz committed
573 574 575 576
{
	if (!(pkt[0] & 0x80) || ((pkt[1] | pkt[2] | pkt[3]) & 0x80))
		return 0;

577 578 579
	dev->x = ((pkt[0] & 0x1F) << 7) | (pkt[2] & 0x7F);
	dev->y = ((pkt[1] & 0x1F) << 7) | (pkt[3] & 0x7F);
	dev->touch = pkt[0] & 0x20;
Daniel Ritz's avatar
Daniel Ritz committed
580 581 582 583 584

	return 1;
}
#endif

585 586 587 588 589 590 591
/*****************************************************************************
 * DMC TSC-10/25 Part
 *
 * Documentation about the controller and it's protocol can be found at
 *   http://www.dmccoltd.com/files/controler/tsc10usb_pi_e.pdf
 *   http://www.dmccoltd.com/files/controler/tsc25_usb_e.pdf
 */
592
#ifdef CONFIG_TOUCHSCREEN_USB_DMC_TSC10
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609

/* supported data rates. currently using 130 */
#define TSC10_RATE_POINT	0x50
#define TSC10_RATE_30		0x40
#define TSC10_RATE_50		0x41
#define TSC10_RATE_80		0x42
#define TSC10_RATE_100		0x43
#define TSC10_RATE_130		0x44
#define TSC10_RATE_150		0x45

/* commands */
#define TSC10_CMD_RESET		0x55
#define TSC10_CMD_RATE		0x05
#define TSC10_CMD_DATA1		0x01

static int dmc_tsc10_init(struct usbtouch_usb *usbtouch)
{
610
	struct usb_device *dev = interface_to_usbdev(usbtouch->interface);
611 612
	int ret = -ENOMEM;
	unsigned char *buf;
613

614
	buf = kmalloc(2, GFP_NOIO);
615 616
	if (!buf)
		goto err_nobuf;
617 618 619 620 621 622 623
	/* reset */
	buf[0] = buf[1] = 0xFF;
	ret = usb_control_msg(dev, usb_rcvctrlpipe (dev, 0),
	                      TSC10_CMD_RESET,
	                      USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
	                      0, 0, buf, 2, USB_CTRL_SET_TIMEOUT);
	if (ret < 0)
624
		goto err_out;
625
	if (buf[0] != 0x06) {
626 627 628
		ret = -ENODEV;
		goto err_out;
	}
629

630 631 632
	/* TSC-25 data sheet specifies a delay after the RESET command */
	msleep(150);

633 634 635 636 637 638 639
	/* set coordinate output rate */
	buf[0] = buf[1] = 0xFF;
	ret = usb_control_msg(dev, usb_rcvctrlpipe (dev, 0),
	                      TSC10_CMD_RATE,
	                      USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
	                      TSC10_RATE_150, 0, buf, 2, USB_CTRL_SET_TIMEOUT);
	if (ret < 0)
640
		goto err_out;
641
	if ((buf[0] != 0x06) && (buf[0] != 0x15 || buf[1] != 0x01)) {
642 643 644
		ret = -ENODEV;
		goto err_out;
	}
645 646 647 648 649 650

	/* start sending data */
	ret = usb_control_msg(dev, usb_rcvctrlpipe (dev, 0),
	                      TSC10_CMD_DATA1,
	                      USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
	                      0, 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
651 652 653 654
err_out:
	kfree(buf);
err_nobuf:
	return ret;
655 656 657
}


658
static int dmc_tsc10_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
659
{
660 661 662
	dev->x = ((pkt[2] & 0x03) << 8) | pkt[1];
	dev->y = ((pkt[4] & 0x03) << 8) | pkt[3];
	dev->touch = pkt[0] & 0x01;
663 664 665 666 667 668

	return 1;
}
#endif


669 670 671 672 673 674 675 676 677 678 679 680 681 682
/*****************************************************************************
 * IRTOUCH Part
 */
#ifdef CONFIG_TOUCHSCREEN_USB_IRTOUCH
static int irtouch_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
{
	dev->x = (pkt[3] << 8) | pkt[2];
	dev->y = (pkt[5] << 8) | pkt[4];
	dev->touch = (pkt[1] & 0x03) ? 1 : 0;

	return 1;
}
#endif

683
/*****************************************************************************
684
 * ET&T TC5UH/TC4UM part
685
 */
686 687
#ifdef CONFIG_TOUCHSCREEN_USB_ETT_TC45USB
static int tc45usb_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
688 689 690 691 692 693 694 695
{
	dev->x = ((pkt[2] & 0x0F) << 8) | pkt[1];
	dev->y = ((pkt[4] & 0x0F) << 8) | pkt[3];
	dev->touch = pkt[0] & 0x01;

	return 1;
}
#endif
696

697 698 699 700
/*****************************************************************************
 * IdealTEK URTC1000 Part
 */
#ifdef CONFIG_TOUCHSCREEN_USB_IDEALTEK
701 702 703
#ifndef MULTI_PACKET
#define MULTI_PACKET
#endif
704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735
static int idealtek_get_pkt_len(unsigned char *buf, int len)
{
	if (buf[0] & 0x80)
		return 5;
	if (buf[0] == 0x01)
		return len;
	return 0;
}

static int idealtek_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
{
	switch (pkt[0] & 0x98) {
	case 0x88:
		/* touch data in IdealTEK mode */
		dev->x = (pkt[1] << 5) | (pkt[2] >> 2);
		dev->y = (pkt[3] << 5) | (pkt[4] >> 2);
		dev->touch = (pkt[0] & 0x40) ? 1 : 0;
		return 1;

	case 0x98:
		/* touch data in MT emulation mode */
		dev->x = (pkt[2] << 5) | (pkt[1] >> 2);
		dev->y = (pkt[4] << 5) | (pkt[3] >> 2);
		dev->touch = (pkt[0] & 0x40) ? 1 : 0;
		return 1;

	default:
		return 0;
	}
}
#endif

736 737 738 739 740 741
/*****************************************************************************
 * General Touch Part
 */
#ifdef CONFIG_TOUCHSCREEN_USB_GENERAL_TOUCH
static int general_touch_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
{
742 743
	dev->x = (pkt[2] << 8) | pkt[1];
	dev->y = (pkt[4] << 8) | pkt[3];
744 745 746 747 748 749
	dev->press = pkt[5] & 0xff;
	dev->touch = pkt[0] & 0x01;

	return 1;
}
#endif
750

751 752 753 754 755 756 757 758 759
/*****************************************************************************
 * GoTop Part
 */
#ifdef CONFIG_TOUCHSCREEN_USB_GOTOP
static int gotop_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
{
	dev->x = ((pkt[1] & 0x38) << 4) | pkt[2];
	dev->y = ((pkt[1] & 0x07) << 7) | pkt[3];
	dev->touch = pkt[0] & 0x01;
760 761 762 763 764 765 766 767 768 769 770 771 772 773 774

	return 1;
}
#endif

/*****************************************************************************
 * JASTEC Part
 */
#ifdef CONFIG_TOUCHSCREEN_USB_JASTEC
static int jastec_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
{
	dev->x = ((pkt[0] & 0x3f) << 6) | (pkt[2] & 0x3f);
	dev->y = ((pkt[1] & 0x3f) << 6) | (pkt[3] & 0x3f);
	dev->touch = (pkt[0] & 0x40) >> 6;

775 776 777 778
	return 1;
}
#endif

779 780 781 782 783 784
/*****************************************************************************
 * Zytronic Part
 */
#ifdef CONFIG_TOUCHSCREEN_USB_ZYTRONIC
static int zytronic_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
{
785
	struct usb_interface *intf = dev->interface;
786

787 788
	switch (pkt[0]) {
	case 0x3A: /* command response */
789
		dev_dbg(&intf->dev, "%s: Command response %d\n", __func__, pkt[1]);
790 791 792 793 794 795
		break;

	case 0xC0: /* down */
		dev->x = (pkt[1] & 0x7f) | ((pkt[2] & 0x07) << 7);
		dev->y = (pkt[3] & 0x7f) | ((pkt[4] & 0x07) << 7);
		dev->touch = 1;
796
		dev_dbg(&intf->dev, "%s: down %d,%d\n", __func__, dev->x, dev->y);
797 798 799 800 801 802
		return 1;

	case 0x80: /* up */
		dev->x = (pkt[1] & 0x7f) | ((pkt[2] & 0x07) << 7);
		dev->y = (pkt[3] & 0x7f) | ((pkt[4] & 0x07) << 7);
		dev->touch = 0;
803
		dev_dbg(&intf->dev, "%s: up %d,%d\n", __func__, dev->x, dev->y);
804 805 806
		return 1;

	default:
807
		dev_dbg(&intf->dev, "%s: Unknown return %d\n", __func__, pkt[0]);
808 809 810 811 812 813
		break;
	}

	return 0;
}
#endif
814

815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843
/*****************************************************************************
 * NEXIO Part
 */
#ifdef CONFIG_TOUCHSCREEN_USB_NEXIO

#define NEXIO_TIMEOUT	5000
#define NEXIO_BUFSIZE	1024
#define NEXIO_THRESHOLD	50

struct nexio_priv {
	struct urb *ack;
	unsigned char *ack_buf;
};

struct nexio_touch_packet {
	u8	flags;		/* 0xe1 = touch, 0xe1 = release */
	__be16	data_len;	/* total bytes of touch data */
	__be16	x_len;		/* bytes for X axis */
	__be16	y_len;		/* bytes for Y axis */
	u8	data[];
} __attribute__ ((packed));

static unsigned char nexio_ack_pkt[2] = { 0xaa, 0x02 };
static unsigned char nexio_init_pkt[4] = { 0x82, 0x04, 0x0a, 0x0f };

static void nexio_ack_complete(struct urb *urb)
{
}

844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861
static int nexio_alloc(struct usbtouch_usb *usbtouch)
{
	struct nexio_priv *priv;
	int ret = -ENOMEM;

	usbtouch->priv = kmalloc(sizeof(struct nexio_priv), GFP_KERNEL);
	if (!usbtouch->priv)
		goto out_buf;

	priv = usbtouch->priv;

	priv->ack_buf = kmemdup(nexio_ack_pkt, sizeof(nexio_ack_pkt),
				GFP_KERNEL);
	if (!priv->ack_buf)
		goto err_priv;

	priv->ack = usb_alloc_urb(0, GFP_KERNEL);
	if (!priv->ack) {
862
		dev_dbg(&usbtouch->interface->dev,
863
			"%s - usb_alloc_urb failed: usbtouch->ack\n", __func__);
864 865 866 867 868 869 870 871 872 873 874 875 876
		goto err_ack_buf;
	}

	return 0;

err_ack_buf:
	kfree(priv->ack_buf);
err_priv:
	kfree(priv);
out_buf:
	return ret;
}

877 878 879 880
static int nexio_init(struct usbtouch_usb *usbtouch)
{
	struct usb_device *dev = interface_to_usbdev(usbtouch->interface);
	struct usb_host_interface *interface = usbtouch->interface->cur_altsetting;
881
	struct nexio_priv *priv = usbtouch->priv;
882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899
	int ret = -ENOMEM;
	int actual_len, i;
	unsigned char *buf;
	char *firmware_ver = NULL, *device_name = NULL;
	int input_ep = 0, output_ep = 0;

	/* find first input and output endpoint */
	for (i = 0; i < interface->desc.bNumEndpoints; i++) {
		if (!input_ep &&
		    usb_endpoint_dir_in(&interface->endpoint[i].desc))
			input_ep = interface->endpoint[i].desc.bEndpointAddress;
		if (!output_ep &&
		    usb_endpoint_dir_out(&interface->endpoint[i].desc))
			output_ep = interface->endpoint[i].desc.bEndpointAddress;
	}
	if (!input_ep || !output_ep)
		return -ENXIO;

900
	buf = kmalloc(NEXIO_BUFSIZE, GFP_NOIO);
901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931
	if (!buf)
		goto out_buf;

	/* two empty reads */
	for (i = 0; i < 2; i++) {
		ret = usb_bulk_msg(dev, usb_rcvbulkpipe(dev, input_ep),
				   buf, NEXIO_BUFSIZE, &actual_len,
				   NEXIO_TIMEOUT);
		if (ret < 0)
			goto out_buf;
	}

	/* send init command */
	memcpy(buf, nexio_init_pkt, sizeof(nexio_init_pkt));
	ret = usb_bulk_msg(dev, usb_sndbulkpipe(dev, output_ep),
			   buf, sizeof(nexio_init_pkt), &actual_len,
			   NEXIO_TIMEOUT);
	if (ret < 0)
		goto out_buf;

	/* read replies */
	for (i = 0; i < 3; i++) {
		memset(buf, 0, NEXIO_BUFSIZE);
		ret = usb_bulk_msg(dev, usb_rcvbulkpipe(dev, input_ep),
				   buf, NEXIO_BUFSIZE, &actual_len,
				   NEXIO_TIMEOUT);
		if (ret < 0 || actual_len < 1 || buf[1] != actual_len)
			continue;
		switch (buf[0]) {
		case 0x83:	/* firmware version */
			if (!firmware_ver)
932
				firmware_ver = kstrdup(&buf[2], GFP_NOIO);
933 934 935
			break;
		case 0x84:	/* device name */
			if (!device_name)
936
				device_name = kstrdup(&buf[2], GFP_NOIO);
937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970
			break;
		}
	}

	printk(KERN_INFO "Nexio device: %s, firmware version: %s\n",
	       device_name, firmware_ver);

	kfree(firmware_ver);
	kfree(device_name);

	usb_fill_bulk_urb(priv->ack, dev, usb_sndbulkpipe(dev, output_ep),
			  priv->ack_buf, sizeof(nexio_ack_pkt),
			  nexio_ack_complete, usbtouch);
	ret = 0;

out_buf:
	kfree(buf);
	return ret;
}

static void nexio_exit(struct usbtouch_usb *usbtouch)
{
	struct nexio_priv *priv = usbtouch->priv;

	usb_kill_urb(priv->ack);
	usb_free_urb(priv->ack);
	kfree(priv->ack_buf);
	kfree(priv);
}

static int nexio_read_data(struct usbtouch_usb *usbtouch, unsigned char *pkt)
{
	struct nexio_touch_packet *packet = (void *) pkt;
	struct nexio_priv *priv = usbtouch->priv;
971 972 973 974
	unsigned int data_len = be16_to_cpu(packet->data_len);
	unsigned int x_len = be16_to_cpu(packet->x_len);
	unsigned int y_len = be16_to_cpu(packet->y_len);
	int x, y, begin_x, begin_y, end_x, end_y, w, h, ret;
975 976 977 978 979

	/* got touch data? */
	if ((pkt[0] & 0xe0) != 0xe0)
		return 0;

980 981 982 983
	if (data_len > 0xff)
		data_len -= 0x100;
	if (x_len > 0xff)
		x_len -= 0x80;
984

985 986 987 988
	/* send ACK */
	ret = usb_submit_urb(priv->ack, GFP_ATOMIC);

	if (!usbtouch->type->max_xc) {
989 990 991 992 993 994
		usbtouch->type->max_xc = 2 * x_len;
		input_set_abs_params(usbtouch->input, ABS_X,
				     0, usbtouch->type->max_xc, 0, 0);
		usbtouch->type->max_yc = 2 * y_len;
		input_set_abs_params(usbtouch->input, ABS_Y,
				     0, usbtouch->type->max_yc, 0, 0);
995 996 997 998 999 1000 1001 1002 1003
	}
	/*
	 * The device reports state of IR sensors on X and Y axes.
	 * Each byte represents "darkness" percentage (0-100) of one element.
	 * 17" touchscreen reports only 64 x 52 bytes so the resolution is low.
	 * This also means that there's a limited multi-touch capability but
	 * it's disabled (and untested) here as there's no X driver for that.
	 */
	begin_x = end_x = begin_y = end_y = -1;
1004
	for (x = 0; x < x_len; x++) {
1005 1006 1007 1008 1009 1010
		if (begin_x == -1 && packet->data[x] > NEXIO_THRESHOLD) {
			begin_x = x;
			continue;
		}
		if (end_x == -1 && begin_x != -1 && packet->data[x] < NEXIO_THRESHOLD) {
			end_x = x - 1;
1011
			for (y = x_len; y < data_len; y++) {
1012
				if (begin_y == -1 && packet->data[y] > NEXIO_THRESHOLD) {
1013
					begin_y = y - x_len;
1014 1015 1016 1017
					continue;
				}
				if (end_y == -1 &&
				    begin_y != -1 && packet->data[y] < NEXIO_THRESHOLD) {
1018
					end_y = y - 1 - x_len;
1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
					w = end_x - begin_x;
					h = end_y - begin_y;
#if 0
					/* multi-touch */
					input_report_abs(usbtouch->input,
						    ABS_MT_TOUCH_MAJOR, max(w,h));
					input_report_abs(usbtouch->input,
						    ABS_MT_TOUCH_MINOR, min(x,h));
					input_report_abs(usbtouch->input,
						    ABS_MT_POSITION_X, 2*begin_x+w);
					input_report_abs(usbtouch->input,
						    ABS_MT_POSITION_Y, 2*begin_y+h);
					input_report_abs(usbtouch->input,
						    ABS_MT_ORIENTATION, w > h);
					input_mt_sync(usbtouch->input);
#endif
					/* single touch */
					usbtouch->x = 2 * begin_x + w;
					usbtouch->y = 2 * begin_y + h;
					usbtouch->touch = packet->flags & 0x01;
					begin_y = end_y = -1;
					return 1;
				}
			}
			begin_x = end_x = -1;
		}

	}
	return 0;
}
#endif


1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
/*****************************************************************************
 * ELO part
 */

#ifdef CONFIG_TOUCHSCREEN_USB_ELO

static int elo_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
{
	dev->x = (pkt[3] << 8) | pkt[2];
	dev->y = (pkt[5] << 8) | pkt[4];
	dev->touch = pkt[6] > 0;
	dev->press = pkt[6];

	return 1;
}
#endif


1070 1071 1072
/*****************************************************************************
 * the different device descriptors
 */
1073 1074 1075 1076 1077
#ifdef MULTI_PACKET
static void usbtouch_process_multi(struct usbtouch_usb *usbtouch,
				   unsigned char *pkt, int len);
#endif

1078
static struct usbtouch_device_info usbtouch_dev_info[] = {
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090
#ifdef CONFIG_TOUCHSCREEN_USB_ELO
	[DEVTYPE_ELO] = {
		.min_xc		= 0x0,
		.max_xc		= 0x0fff,
		.min_yc		= 0x0,
		.max_yc		= 0x0fff,
		.max_press	= 0xff,
		.rept_size	= 8,
		.read_data	= elo_read_data,
	},
#endif

1091
#ifdef CONFIG_TOUCHSCREEN_USB_EGALAX
1092 1093 1094 1095 1096 1097
	[DEVTYPE_EGALAX] = {
		.min_xc		= 0x0,
		.max_xc		= 0x07ff,
		.min_yc		= 0x0,
		.max_yc		= 0x07ff,
		.rept_size	= 16,
Daniel Ritz's avatar
Daniel Ritz committed
1098 1099
		.process_pkt	= usbtouch_process_multi,
		.get_pkt_len	= egalax_get_pkt_len,
1100
		.read_data	= egalax_read_data,
1101
		.init		= egalax_init,
1102 1103 1104
	},
#endif

1105
#ifdef CONFIG_TOUCHSCREEN_USB_PANJIT
1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
	[DEVTYPE_PANJIT] = {
		.min_xc		= 0x0,
		.max_xc		= 0x0fff,
		.min_yc		= 0x0,
		.max_yc		= 0x0fff,
		.rept_size	= 8,
		.read_data	= panjit_read_data,
	},
#endif

1116
#ifdef CONFIG_TOUCHSCREEN_USB_3M
1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127
	[DEVTYPE_3M] = {
		.min_xc		= 0x0,
		.max_xc		= 0x4000,
		.min_yc		= 0x0,
		.max_yc		= 0x4000,
		.rept_size	= 11,
		.read_data	= mtouch_read_data,
		.init		= mtouch_init,
	},
#endif

1128
#ifdef CONFIG_TOUCHSCREEN_USB_ITM
1129 1130 1131 1132 1133 1134 1135 1136 1137 1138
	[DEVTYPE_ITM] = {
		.min_xc		= 0x0,
		.max_xc		= 0x0fff,
		.min_yc		= 0x0,
		.max_yc		= 0x0fff,
		.max_press	= 0xff,
		.rept_size	= 8,
		.read_data	= itm_read_data,
	},
#endif
Daniel Ritz's avatar
Daniel Ritz committed
1139

1140
#ifdef CONFIG_TOUCHSCREEN_USB_ETURBO
Daniel Ritz's avatar
Daniel Ritz committed
1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152
	[DEVTYPE_ETURBO] = {
		.min_xc		= 0x0,
		.max_xc		= 0x07ff,
		.min_yc		= 0x0,
		.max_yc		= 0x07ff,
		.rept_size	= 8,
		.process_pkt	= usbtouch_process_multi,
		.get_pkt_len	= eturbo_get_pkt_len,
		.read_data	= eturbo_read_data,
	},
#endif

1153
#ifdef CONFIG_TOUCHSCREEN_USB_GUNZE
Daniel Ritz's avatar
Daniel Ritz committed
1154 1155 1156 1157 1158 1159 1160 1161 1162
	[DEVTYPE_GUNZE] = {
		.min_xc		= 0x0,
		.max_xc		= 0x0fff,
		.min_yc		= 0x0,
		.max_yc		= 0x0fff,
		.rept_size	= 4,
		.read_data	= gunze_read_data,
	},
#endif
1163

1164
#ifdef CONFIG_TOUCHSCREEN_USB_DMC_TSC10
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
	[DEVTYPE_DMC_TSC10] = {
		.min_xc		= 0x0,
		.max_xc		= 0x03ff,
		.min_yc		= 0x0,
		.max_yc		= 0x03ff,
		.rept_size	= 5,
		.init		= dmc_tsc10_init,
		.read_data	= dmc_tsc10_read_data,
	},
#endif
1175 1176 1177 1178 1179 1180 1181 1182 1183 1184

#ifdef CONFIG_TOUCHSCREEN_USB_IRTOUCH
	[DEVTYPE_IRTOUCH] = {
		.min_xc		= 0x0,
		.max_xc		= 0x0fff,
		.min_yc		= 0x0,
		.max_yc		= 0x0fff,
		.rept_size	= 8,
		.read_data	= irtouch_read_data,
	},
1185 1186 1187 1188 1189 1190 1191 1192 1193

	[DEVTYPE_IRTOUCH_HIRES] = {
		.min_xc		= 0x0,
		.max_xc		= 0x7fff,
		.min_yc		= 0x0,
		.max_yc		= 0x7fff,
		.rept_size	= 8,
		.read_data	= irtouch_read_data,
	},
1194
#endif
1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207

#ifdef CONFIG_TOUCHSCREEN_USB_IDEALTEK
	[DEVTYPE_IDEALTEK] = {
		.min_xc		= 0x0,
		.max_xc		= 0x0fff,
		.min_yc		= 0x0,
		.max_yc		= 0x0fff,
		.rept_size	= 8,
		.process_pkt	= usbtouch_process_multi,
		.get_pkt_len	= idealtek_get_pkt_len,
		.read_data	= idealtek_read_data,
	},
#endif
1208 1209 1210 1211

#ifdef CONFIG_TOUCHSCREEN_USB_GENERAL_TOUCH
	[DEVTYPE_GENERAL_TOUCH] = {
		.min_xc		= 0x0,
1212
		.max_xc		= 0x7fff,
1213
		.min_yc		= 0x0,
1214
		.max_yc		= 0x7fff,
1215 1216
		.rept_size	= 7,
		.read_data	= general_touch_read_data,
1217
	},
1218 1219
#endif

1220 1221 1222 1223 1224 1225 1226 1227 1228 1229
#ifdef CONFIG_TOUCHSCREEN_USB_GOTOP
	[DEVTYPE_GOTOP] = {
		.min_xc		= 0x0,
		.max_xc		= 0x03ff,
		.min_yc		= 0x0,
		.max_yc		= 0x03ff,
		.rept_size	= 4,
		.read_data	= gotop_read_data,
	},
#endif
1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240

#ifdef CONFIG_TOUCHSCREEN_USB_JASTEC
	[DEVTYPE_JASTEC] = {
		.min_xc		= 0x0,
		.max_xc		= 0x0fff,
		.min_yc		= 0x0,
		.max_yc		= 0x0fff,
		.rept_size	= 4,
		.read_data	= jastec_read_data,
	},
#endif
1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252

#ifdef CONFIG_TOUCHSCREEN_USB_E2I
	[DEVTYPE_E2I] = {
		.min_xc		= 0x0,
		.max_xc		= 0x7fff,
		.min_yc		= 0x0,
		.max_yc		= 0x7fff,
		.rept_size	= 6,
		.init		= e2i_init,
		.read_data	= e2i_read_data,
	},
#endif
1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264

#ifdef CONFIG_TOUCHSCREEN_USB_ZYTRONIC
	[DEVTYPE_ZYTRONIC] = {
		.min_xc		= 0x0,
		.max_xc		= 0x03ff,
		.min_yc		= 0x0,
		.max_yc		= 0x03ff,
		.rept_size	= 5,
		.read_data	= zytronic_read_data,
		.irq_always     = true,
	},
#endif
1265

1266 1267
#ifdef CONFIG_TOUCHSCREEN_USB_ETT_TC45USB
	[DEVTYPE_TC45USB] = {
1268 1269 1270 1271 1272
		.min_xc		= 0x0,
		.max_xc		= 0x0fff,
		.min_yc		= 0x0,
		.max_yc		= 0x0fff,
		.rept_size	= 5,
1273
		.read_data	= tc45usb_read_data,
1274 1275
	},
#endif
1276 1277 1278

#ifdef CONFIG_TOUCHSCREEN_USB_NEXIO
	[DEVTYPE_NEXIO] = {
1279
		.rept_size	= 1024,
1280 1281
		.irq_always	= true,
		.read_data	= nexio_read_data,
1282
		.alloc		= nexio_alloc,
1283 1284 1285 1286
		.init		= nexio_init,
		.exit		= nexio_exit,
	},
#endif
1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298
#ifdef CONFIG_TOUCHSCREEN_USB_EASYTOUCH
	[DEVTYPE_ETOUCH] = {
		.min_xc		= 0x0,
		.max_xc		= 0x07ff,
		.min_yc		= 0x0,
		.max_yc		= 0x07ff,
		.rept_size	= 16,
		.process_pkt	= usbtouch_process_multi,
		.get_pkt_len	= etouch_get_pkt_len,
		.read_data	= etouch_read_data,
	},
#endif
1299 1300 1301 1302 1303 1304 1305
};


/*****************************************************************************
 * Generic Part
 */
static void usbtouch_process_pkt(struct usbtouch_usb *usbtouch,
1306
                                 unsigned char *pkt, int len)
1307 1308 1309
{
	struct usbtouch_device_info *type = usbtouch->type;

1310
	if (!type->read_data(usbtouch, pkt))
1311 1312
			return;

1313
	input_report_key(usbtouch->input, BTN_TOUCH, usbtouch->touch);
1314 1315

	if (swap_xy) {
1316 1317
		input_report_abs(usbtouch->input, ABS_X, usbtouch->y);
		input_report_abs(usbtouch->input, ABS_Y, usbtouch->x);
1318
	} else {
1319 1320
		input_report_abs(usbtouch->input, ABS_X, usbtouch->x);
		input_report_abs(usbtouch->input, ABS_Y, usbtouch->y);
1321 1322
	}
	if (type->max_press)
1323
		input_report_abs(usbtouch->input, ABS_PRESSURE, usbtouch->press);
1324 1325 1326 1327
	input_sync(usbtouch->input);
}


Daniel Ritz's avatar
Daniel Ritz committed
1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365
#ifdef MULTI_PACKET
static void usbtouch_process_multi(struct usbtouch_usb *usbtouch,
                                   unsigned char *pkt, int len)
{
	unsigned char *buffer;
	int pkt_len, pos, buf_len, tmp;

	/* process buffer */
	if (unlikely(usbtouch->buf_len)) {
		/* try to get size */
		pkt_len = usbtouch->type->get_pkt_len(
				usbtouch->buffer, usbtouch->buf_len);

		/* drop? */
		if (unlikely(!pkt_len))
			goto out_flush_buf;

		/* need to append -pkt_len bytes before able to get size */
		if (unlikely(pkt_len < 0)) {
			int append = -pkt_len;
			if (unlikely(append > len))
			       append = len;
			if (usbtouch->buf_len + append >= usbtouch->type->rept_size)
				goto out_flush_buf;
			memcpy(usbtouch->buffer + usbtouch->buf_len, pkt, append);
			usbtouch->buf_len += append;

			pkt_len = usbtouch->type->get_pkt_len(
					usbtouch->buffer, usbtouch->buf_len);
			if (pkt_len < 0)
				return;
		}

		/* append */
		tmp = pkt_len - usbtouch->buf_len;
		if (usbtouch->buf_len + tmp >= usbtouch->type->rept_size)
			goto out_flush_buf;
		memcpy(usbtouch->buffer + usbtouch->buf_len, pkt, tmp);
1366
		usbtouch_process_pkt(usbtouch, usbtouch->buffer, pkt_len);
Daniel Ritz's avatar
Daniel Ritz committed
1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378

		buffer = pkt + tmp;
		buf_len = len - tmp;
	} else {
		buffer = pkt;
		buf_len = len;
	}

	/* loop over the received packet, process */
	pos = 0;
	while (pos < buf_len) {
		/* get packet len */
1379 1380
		pkt_len = usbtouch->type->get_pkt_len(buffer + pos,
							buf_len - pos);
Daniel Ritz's avatar
Daniel Ritz committed
1381

1382 1383 1384 1385 1386
		/* unknown packet: skip one byte */
		if (unlikely(!pkt_len)) {
			pos++;
			continue;
		}
Daniel Ritz's avatar
Daniel Ritz committed
1387 1388 1389

		/* full packet: process */
		if (likely((pkt_len > 0) && (pkt_len <= buf_len - pos))) {
1390
			usbtouch_process_pkt(usbtouch, buffer + pos, pkt_len);
Daniel Ritz's avatar
Daniel Ritz committed
1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406
		} else {
			/* incomplete packet: save in buffer */
			memcpy(usbtouch->buffer, buffer + pos, buf_len - pos);
			usbtouch->buf_len = buf_len - pos;
			return;
		}
		pos += pkt_len;
	}

out_flush_buf:
	usbtouch->buf_len = 0;
	return;
}
#endif


1407
static void usbtouch_irq(struct urb *urb)
1408 1409
{
	struct usbtouch_usb *usbtouch = urb->context;
1410
	struct device *dev = &usbtouch->interface->dev;
1411 1412 1413 1414 1415 1416
	int retval;

	switch (urb->status) {
	case 0:
		/* success */
		break;
1417
	case -ETIME:
1418
		/* this urb is timing out */
1419 1420 1421
		dev_dbg(dev,
			"%s - urb timed out - was the device unplugged?\n",
			__func__);
1422 1423 1424 1425
		return;
	case -ECONNRESET:
	case -ENOENT:
	case -ESHUTDOWN:
1426
	case -EPIPE:
1427
		/* this urb is terminated, clean up */
1428 1429
		dev_dbg(dev, "%s - urb shutting down with status: %d\n",
			__func__, urb->status);
1430 1431
		return;
	default:
1432 1433
		dev_dbg(dev, "%s - nonzero urb status received: %d\n",
			__func__, urb->status);
1434 1435 1436
		goto exit;
	}

1437
	usbtouch->type->process_pkt(usbtouch, usbtouch->data, urb->actual_length);
1438 1439

exit:
1440
	usb_mark_last_busy(interface_to_usbdev(usbtouch->interface));
1441 1442
	retval = usb_submit_urb(urb, GFP_ATOMIC);
	if (retval)
1443
		dev_err(dev, "%s - usb_submit_urb failed with result: %d\n",
1444
			__func__, retval);
1445 1446 1447 1448
}

static int usbtouch_open(struct input_dev *input)
{
1449
	struct usbtouch_usb *usbtouch = input_get_drvdata(input);
1450
	int r;
1451

1452
	usbtouch->irq->dev = interface_to_usbdev(usbtouch->interface);
1453

1454 1455 1456 1457
	r = usb_autopm_get_interface(usbtouch->interface) ? -EIO : 0;
	if (r < 0)
		goto out;

1458
	if (!usbtouch->type->irq_always) {
1459 1460 1461 1462
		if (usb_submit_urb(usbtouch->irq, GFP_KERNEL)) {
			r = -EIO;
			goto out_put;
		}
1463
	}
1464

1465 1466 1467 1468 1469
	usbtouch->interface->needs_remote_wakeup = 1;
out_put:
	usb_autopm_put_interface(usbtouch->interface);
out:
	return r;
1470 1471 1472 1473
}

static void usbtouch_close(struct input_dev *input)
{
1474
	struct usbtouch_usb *usbtouch = input_get_drvdata(input);
1475
	int r;
1476

1477 1478
	if (!usbtouch->type->irq_always)
		usb_kill_urb(usbtouch->irq);
1479 1480 1481 1482
	r = usb_autopm_get_interface(usbtouch->interface);
	usbtouch->interface->needs_remote_wakeup = 0;
	if (!r)
		usb_autopm_put_interface(usbtouch->interface);
1483 1484
}

1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507
static int usbtouch_suspend
(struct usb_interface *intf, pm_message_t message)
{
	struct usbtouch_usb *usbtouch = usb_get_intfdata(intf);

	usb_kill_urb(usbtouch->irq);

	return 0;
}

static int usbtouch_resume(struct usb_interface *intf)
{
	struct usbtouch_usb *usbtouch = usb_get_intfdata(intf);
	struct input_dev *input = usbtouch->input;
	int result = 0;

	mutex_lock(&input->mutex);
	if (input->users || usbtouch->type->irq_always)
		result = usb_submit_urb(usbtouch->irq, GFP_NOIO);
	mutex_unlock(&input->mutex);

	return result;
}
1508

1509 1510 1511 1512 1513 1514 1515 1516 1517 1518
static int usbtouch_reset_resume(struct usb_interface *intf)
{
	struct usbtouch_usb *usbtouch = usb_get_intfdata(intf);
	struct input_dev *input = usbtouch->input;
	int err = 0;

	/* reinit the device */
	if (usbtouch->type->init) {
		err = usbtouch->type->init(usbtouch);
		if (err) {
1519
			dev_dbg(&intf->dev,
1520 1521
				"%s - type->init() failed, err: %d\n",
				__func__, err);
1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534
			return err;
		}
	}

	/* restart IO if needed */
	mutex_lock(&input->mutex);
	if (input->users)
		err = usb_submit_urb(usbtouch->irq, GFP_NOIO);
	mutex_unlock(&input->mutex);

	return err;
}

1535 1536 1537
static void usbtouch_free_buffers(struct usb_device *udev,
				  struct usbtouch_usb *usbtouch)
{
1538
	usb_free_coherent(udev, usbtouch->data_size,
1539
			  usbtouch->data, usbtouch->data_dma);
1540 1541 1542
	kfree(usbtouch->buffer);
}

1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553
static struct usb_endpoint_descriptor *
usbtouch_get_input_endpoint(struct usb_host_interface *interface)
{
	int i;

	for (i = 0; i < interface->desc.bNumEndpoints; i++)
		if (usb_endpoint_dir_in(&interface->endpoint[i].desc))
			return &interface->endpoint[i].desc;

	return NULL;
}
1554 1555 1556 1557 1558 1559 1560 1561 1562

static int usbtouch_probe(struct usb_interface *intf,
			  const struct usb_device_id *id)
{
	struct usbtouch_usb *usbtouch;
	struct input_dev *input_dev;
	struct usb_endpoint_descriptor *endpoint;
	struct usb_device *udev = interface_to_usbdev(intf);
	struct usbtouch_device_info *type;
Daniel Ritz's avatar
Daniel Ritz committed
1563
	int err = -ENOMEM;
1564 1565 1566 1567

	/* some devices are ignored */
	if (id->driver_info == DEVTYPE_IGNORE)
		return -ENODEV;
1568

1569 1570 1571
	endpoint = usbtouch_get_input_endpoint(intf->cur_altsetting);
	if (!endpoint)
		return -ENXIO;
1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582

	usbtouch = kzalloc(sizeof(struct usbtouch_usb), GFP_KERNEL);
	input_dev = input_allocate_device();
	if (!usbtouch || !input_dev)
		goto out_free;

	type = &usbtouch_dev_info[id->driver_info];
	usbtouch->type = type;
	if (!type->process_pkt)
		type->process_pkt = usbtouch_process_pkt;

1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596
	usbtouch->data_size = type->rept_size;
	if (type->get_pkt_len) {
		/*
		 * When dealing with variable-length packets we should
		 * not request more than wMaxPacketSize bytes at once
		 * as we do not know if there is more data coming or
		 * we filled exactly wMaxPacketSize bytes and there is
		 * nothing else.
		 */
		usbtouch->data_size = min(usbtouch->data_size,
					  usb_endpoint_maxp(endpoint));
	}

	usbtouch->data = usb_alloc_coherent(udev, usbtouch->data_size,
1597
					    GFP_KERNEL, &usbtouch->data_dma);
1598 1599 1600
	if (!usbtouch->data)
		goto out_free;

1601
	if (type->get_pkt_len) {
1602 1603 1604 1605 1606 1607 1608
		usbtouch->buffer = kmalloc(type->rept_size, GFP_KERNEL);
		if (!usbtouch->buffer)
			goto out_free_buffers;
	}

	usbtouch->irq = usb_alloc_urb(0, GFP_KERNEL);
	if (!usbtouch->irq) {
1609 1610
		dev_dbg(&intf->dev,
			"%s - usb_alloc_urb failed: usbtouch->irq\n", __func__);
1611 1612 1613
		goto out_free_buffers;
	}

1614
	usbtouch->interface = intf;
1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632
	usbtouch->input = input_dev;

	if (udev->manufacturer)
		strlcpy(usbtouch->name, udev->manufacturer, sizeof(usbtouch->name));

	if (udev->product) {
		if (udev->manufacturer)
			strlcat(usbtouch->name, " ", sizeof(usbtouch->name));
		strlcat(usbtouch->name, udev->product, sizeof(usbtouch->name));
	}

	if (!strlen(usbtouch->name))
		snprintf(usbtouch->name, sizeof(usbtouch->name),
			"USB Touchscreen %04x:%04x",
			 le16_to_cpu(udev->descriptor.idVendor),
			 le16_to_cpu(udev->descriptor.idProduct));

	usb_make_path(udev, usbtouch->phys, sizeof(usbtouch->phys));
1633
	strlcat(usbtouch->phys, "/input0", sizeof(usbtouch->phys));
1634 1635 1636 1637

	input_dev->name = usbtouch->name;
	input_dev->phys = usbtouch->phys;
	usb_to_input_id(udev, &input_dev->id);
1638
	input_dev->dev.parent = &intf->dev;
1639 1640 1641

	input_set_drvdata(input_dev, usbtouch);

1642 1643 1644
	input_dev->open = usbtouch_open;
	input_dev->close = usbtouch_close;

1645 1646
	input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
	input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
1647 1648 1649 1650 1651 1652
	input_set_abs_params(input_dev, ABS_X, type->min_xc, type->max_xc, 0, 0);
	input_set_abs_params(input_dev, ABS_Y, type->min_yc, type->max_yc, 0, 0);
	if (type->max_press)
		input_set_abs_params(input_dev, ABS_PRESSURE, type->min_press,
		                     type->max_press, 0, 0);

1653 1654
	if (usb_endpoint_type(endpoint) == USB_ENDPOINT_XFER_INT)
		usb_fill_int_urb(usbtouch->irq, udev,
1655
			 usb_rcvintpipe(udev, endpoint->bEndpointAddress),
1656
			 usbtouch->data, usbtouch->data_size,
1657
			 usbtouch_irq, usbtouch, endpoint->bInterval);
1658 1659 1660
	else
		usb_fill_bulk_urb(usbtouch->irq, udev,
			 usb_rcvbulkpipe(udev, endpoint->bEndpointAddress),
1661
			 usbtouch->data, usbtouch->data_size,
1662
			 usbtouch_irq, usbtouch);
1663

1664
	usbtouch->irq->dev = udev;
1665 1666 1667
	usbtouch->irq->transfer_dma = usbtouch->data_dma;
	usbtouch->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;

1668 1669 1670 1671
	/* device specific allocations */
	if (type->alloc) {
		err = type->alloc(usbtouch);
		if (err) {
1672 1673 1674
			dev_dbg(&intf->dev,
				"%s - type->alloc() failed, err: %d\n",
				__func__, err);
1675 1676 1677 1678 1679
			goto out_free_urb;
		}
	}

	/* device specific initialisation*/
1680 1681 1682
	if (type->init) {
		err = type->init(usbtouch);
		if (err) {
1683 1684 1685
			dev_dbg(&intf->dev,
				"%s - type->init() failed, err: %d\n",
				__func__, err);
1686
			goto out_do_exit;
1687 1688 1689 1690 1691
		}
	}

	err = input_register_device(usbtouch->input);
	if (err) {
1692 1693 1694
		dev_dbg(&intf->dev,
			"%s - input_register_device failed, err: %d\n",
			__func__, err);
1695
		goto out_do_exit;
1696 1697 1698 1699
	}

	usb_set_intfdata(intf, usbtouch);

1700
	if (usbtouch->type->irq_always) {
1701 1702
		/* this can't fail */
		usb_autopm_get_interface(intf);
1703 1704
		err = usb_submit_urb(usbtouch->irq, GFP_KERNEL);
		if (err) {
1705
			usb_autopm_put_interface(intf);
1706
			dev_err(&intf->dev,
1707 1708
				"%s - usb_submit_urb failed with result: %d\n",
				__func__, err);
1709 1710 1711
			goto out_unregister_input;
		}
	}
1712

1713 1714
	return 0;

1715 1716 1717
out_unregister_input:
	input_unregister_device(input_dev);
	input_dev = NULL;
1718 1719 1720
out_do_exit:
	if (type->exit)
		type->exit(usbtouch);
1721 1722
out_free_urb:
	usb_free_urb(usbtouch->irq);
1723 1724 1725 1726 1727
out_free_buffers:
	usbtouch_free_buffers(udev, usbtouch);
out_free:
	input_free_device(input_dev);
	kfree(usbtouch);
Daniel Ritz's avatar
Daniel Ritz committed
1728
	return err;
1729 1730 1731 1732 1733 1734 1735 1736 1737
}

static void usbtouch_disconnect(struct usb_interface *intf)
{
	struct usbtouch_usb *usbtouch = usb_get_intfdata(intf);

	if (!usbtouch)
		return;

1738
	dev_dbg(&intf->dev,
1739 1740
		"%s - usbtouch is initialized, cleaning up\n", __func__);

1741
	usb_set_intfdata(intf, NULL);
1742
	/* this will stop IO via close */
1743 1744
	input_unregister_device(usbtouch->input);
	usb_free_urb(usbtouch->irq);
1745 1746
	if (usbtouch->type->exit)
		usbtouch->type->exit(usbtouch);
1747 1748 1749 1750 1751 1752 1753 1754 1755 1756
	usbtouch_free_buffers(interface_to_usbdev(intf), usbtouch);
	kfree(usbtouch);
}

MODULE_DEVICE_TABLE(usb, usbtouch_devices);

static struct usb_driver usbtouch_driver = {
	.name		= "usbtouchscreen",
	.probe		= usbtouch_probe,
	.disconnect	= usbtouch_disconnect,
1757 1758
	.suspend	= usbtouch_suspend,
	.resume		= usbtouch_resume,
1759
	.reset_resume	= usbtouch_reset_resume,
1760
	.id_table	= usbtouch_devices,
1761
	.supports_autosuspend = 1,
1762 1763
};

1764
module_usb_driver(usbtouch_driver);
1765 1766 1767 1768 1769 1770 1771 1772

MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_LICENSE("GPL");

MODULE_ALIAS("touchkitusb");
MODULE_ALIAS("itmtouch");
MODULE_ALIAS("mtouchusb");