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