blob: c07be07a69bbafdd0559432c807e73e1ed548bc7 [file] [log] [blame]
Daniel Ritz1d3e2022006-03-29 22:41:07 +02001/******************************************************************************
2 * usbtouchscreen.c
3 * Driver for USB Touchscreens, supporting those devices:
4 * - eGalax Touchkit
Daniel Ritz5d892662006-07-31 21:43:24 +02005 * includes eTurboTouch CT-410/510/700
6 * - 3M/Microtouch EX II series
Daniel Ritz1d3e2022006-03-29 22:41:07 +02007 * - ITM
8 * - PanJit TouchSet
Daniel Ritz5d892662006-07-31 21:43:24 +02009 * - eTurboTouch
10 * - Gunze AHL61
Holger Schurig24ced062006-11-17 22:50:15 +010011 * - DMC TSC-10/25
Ondrej Zarydf561fc2007-07-03 01:55:03 -040012 * - IRTOUCHSYSTEMS/UNITOP
Ondrej Zarya14a8402007-09-04 23:45:01 -040013 * - IdealTEK URTC1000
Daniel Ritz62aa3662008-01-17 12:01:18 -050014 * - General Touch
Jerrold Jones14e40202007-10-22 00:59:47 -040015 * - GoTop Super_Q2/GogoPen/PenPower tablets
Jim Perssonf7370692009-07-07 22:07:59 -070016 * - JASTEC USB touch controller/DigiTech DTR-02U
Daniel Ritz1d3e2022006-03-29 22:41:07 +020017 *
Jerrold Jones14e40202007-10-22 00:59:47 -040018 * Copyright (C) 2004-2007 by Daniel Ritz <daniel.ritz@gmx.ch>
Daniel Ritz1d3e2022006-03-29 22:41:07 +020019 * Copyright (C) by Todd E. Johnson (mtouchusb.c)
20 *
21 * This program is free software; you can redistribute it and/or
22 * modify it under the terms of the GNU General Public License as
23 * published by the Free Software Foundation; either version 2 of the
24 * License, or (at your option) any later version.
25 *
26 * This program is distributed in the hope that it will be useful, but
27 * WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
29 * General Public License for more details.
30 *
31 * You should have received a copy of the GNU General Public License
32 * along with this program; if not, write to the Free Software
33 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
34 *
35 * Driver is based on touchkitusb.c
36 * - ITM parts are from itmtouch.c
37 * - 3M parts are from mtouchusb.c
38 * - PanJit parts are from an unmerged driver by Lanslott Gish
Holger Schurig24ced062006-11-17 22:50:15 +010039 * - DMC TSC 10/25 are from Holger Schurig, with ideas from an unmerged
40 * driver from Marius Vollmer
Daniel Ritz1d3e2022006-03-29 22:41:07 +020041 *
42 *****************************************************************************/
43
44//#define DEBUG
45
Daniel Ritz1d3e2022006-03-29 22:41:07 +020046#include <linux/kernel.h>
47#include <linux/slab.h>
48#include <linux/input.h>
49#include <linux/module.h>
50#include <linux/init.h>
51#include <linux/usb.h>
David Brownellae0dadc2006-06-13 10:04:34 -070052#include <linux/usb/input.h>
Daniel Ritzec42d442008-07-03 10:45:37 -040053#include <linux/hid.h>
Daniel Ritz1d3e2022006-03-29 22:41:07 +020054
55
Daniel Ritz62aa3662008-01-17 12:01:18 -050056#define DRIVER_VERSION "v0.6"
Daniel Ritz1d3e2022006-03-29 22:41:07 +020057#define DRIVER_AUTHOR "Daniel Ritz <daniel.ritz@gmx.ch>"
58#define DRIVER_DESC "USB Touchscreen Driver"
59
60static int swap_xy;
61module_param(swap_xy, bool, 0644);
62MODULE_PARM_DESC(swap_xy, "If set X and Y axes are swapped.");
63
Dan Streetmanc9cbf3d2009-01-10 23:44:22 -080064static int hwcalib_xy;
65module_param(hwcalib_xy, bool, 0644);
66MODULE_PARM_DESC(hwcalib_xy, "If set hw-calibrated X/Y are used if available");
67
Daniel Ritz1d3e2022006-03-29 22:41:07 +020068/* device specifc data/functions */
69struct usbtouch_usb;
70struct usbtouch_device_info {
71 int min_xc, max_xc;
72 int min_yc, max_yc;
73 int min_press, max_press;
74 int rept_size;
Daniel Ritz1d3e2022006-03-29 22:41:07 +020075
David Howells7d12e782006-10-05 14:55:46 +010076 void (*process_pkt) (struct usbtouch_usb *usbtouch, unsigned char *pkt, int len);
Daniel Ritz62aa3662008-01-17 12:01:18 -050077
78 /*
79 * used to get the packet len. possible return values:
80 * > 0: packet len
81 * = 0: skip one byte
82 * < 0: -return value more bytes needed
83 */
Daniel Ritz5d892662006-07-31 21:43:24 +020084 int (*get_pkt_len) (unsigned char *pkt, int len);
Daniel Ritz62aa3662008-01-17 12:01:18 -050085
Daniel Ritzc9d8c2b2006-12-11 18:17:45 +010086 int (*read_data) (struct usbtouch_usb *usbtouch, unsigned char *pkt);
Daniel Ritz1d3e2022006-03-29 22:41:07 +020087 int (*init) (struct usbtouch_usb *usbtouch);
88};
89
Daniel Ritz1d3e2022006-03-29 22:41:07 +020090/* a usbtouch device */
91struct usbtouch_usb {
92 unsigned char *data;
93 dma_addr_t data_dma;
94 unsigned char *buffer;
95 int buf_len;
96 struct urb *irq;
97 struct usb_device *udev;
98 struct input_dev *input;
99 struct usbtouch_device_info *type;
100 char name[128];
101 char phys[64];
Daniel Ritzc9d8c2b2006-12-11 18:17:45 +0100102
103 int x, y;
104 int touch, press;
Daniel Ritz1d3e2022006-03-29 22:41:07 +0200105};
106
Daniel Ritz5d892662006-07-31 21:43:24 +0200107
Daniel Ritz1d3e2022006-03-29 22:41:07 +0200108/* device types */
109enum {
Daniel Ritzec42d442008-07-03 10:45:37 -0400110 DEVTYPE_IGNORE = -1,
Daniel Ritz1d3e2022006-03-29 22:41:07 +0200111 DEVTYPE_EGALAX,
112 DEVTYPE_PANJIT,
113 DEVTYPE_3M,
114 DEVTYPE_ITM,
Daniel Ritz5d892662006-07-31 21:43:24 +0200115 DEVTYPE_ETURBO,
116 DEVTYPE_GUNZE,
Holger Schurig24ced062006-11-17 22:50:15 +0100117 DEVTYPE_DMC_TSC10,
Ondrej Zarydf561fc2007-07-03 01:55:03 -0400118 DEVTYPE_IRTOUCH,
Ondrej Zarya14a8402007-09-04 23:45:01 -0400119 DEVTYPE_IDEALTEK,
Ilya Frolov9d5657d2007-10-12 14:19:40 -0400120 DEVTYPE_GENERAL_TOUCH,
Jerrold Jones14e40202007-10-22 00:59:47 -0400121 DEVTYPE_GOTOP,
Jim Perssonf7370692009-07-07 22:07:59 -0700122 DEVTYPE_JASTEC,
Daniel Ritz1d3e2022006-03-29 22:41:07 +0200123};
124
Daniel Ritzec42d442008-07-03 10:45:37 -0400125#define USB_DEVICE_HID_CLASS(vend, prod) \
126 .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS \
Daniel Ritz139ebe82009-02-28 12:50:54 -0800127 | USB_DEVICE_ID_MATCH_INT_PROTOCOL \
Daniel Ritzec42d442008-07-03 10:45:37 -0400128 | USB_DEVICE_ID_MATCH_DEVICE, \
129 .idVendor = (vend), \
130 .idProduct = (prod), \
131 .bInterfaceClass = USB_INTERFACE_CLASS_HID, \
132 .bInterfaceProtocol = USB_INTERFACE_PROTOCOL_MOUSE
133
Daniel Ritz1d3e2022006-03-29 22:41:07 +0200134static struct usb_device_id usbtouch_devices[] = {
Ondrej Zaryc6f8d702007-06-12 00:33:13 -0400135#ifdef CONFIG_TOUCHSCREEN_USB_EGALAX
Daniel Ritzec42d442008-07-03 10:45:37 -0400136 /* ignore the HID capable devices, handled by usbhid */
137 {USB_DEVICE_HID_CLASS(0x0eef, 0x0001), .driver_info = DEVTYPE_IGNORE},
138 {USB_DEVICE_HID_CLASS(0x0eef, 0x0002), .driver_info = DEVTYPE_IGNORE},
139
140 /* normal device IDs */
Daniel Ritz1d3e2022006-03-29 22:41:07 +0200141 {USB_DEVICE(0x3823, 0x0001), .driver_info = DEVTYPE_EGALAX},
Daniel Ritz5d892662006-07-31 21:43:24 +0200142 {USB_DEVICE(0x3823, 0x0002), .driver_info = DEVTYPE_EGALAX},
Daniel Ritz1d3e2022006-03-29 22:41:07 +0200143 {USB_DEVICE(0x0123, 0x0001), .driver_info = DEVTYPE_EGALAX},
144 {USB_DEVICE(0x0eef, 0x0001), .driver_info = DEVTYPE_EGALAX},
145 {USB_DEVICE(0x0eef, 0x0002), .driver_info = DEVTYPE_EGALAX},
Daniel Ritz5d892662006-07-31 21:43:24 +0200146 {USB_DEVICE(0x1234, 0x0001), .driver_info = DEVTYPE_EGALAX},
147 {USB_DEVICE(0x1234, 0x0002), .driver_info = DEVTYPE_EGALAX},
Daniel Ritz1d3e2022006-03-29 22:41:07 +0200148#endif
149
Ondrej Zaryc6f8d702007-06-12 00:33:13 -0400150#ifdef CONFIG_TOUCHSCREEN_USB_PANJIT
Daniel Ritz1d3e2022006-03-29 22:41:07 +0200151 {USB_DEVICE(0x134c, 0x0001), .driver_info = DEVTYPE_PANJIT},
152 {USB_DEVICE(0x134c, 0x0002), .driver_info = DEVTYPE_PANJIT},
153 {USB_DEVICE(0x134c, 0x0003), .driver_info = DEVTYPE_PANJIT},
154 {USB_DEVICE(0x134c, 0x0004), .driver_info = DEVTYPE_PANJIT},
155#endif
156
Ondrej Zaryc6f8d702007-06-12 00:33:13 -0400157#ifdef CONFIG_TOUCHSCREEN_USB_3M
Daniel Ritz1d3e2022006-03-29 22:41:07 +0200158 {USB_DEVICE(0x0596, 0x0001), .driver_info = DEVTYPE_3M},
159#endif
160
Ondrej Zaryc6f8d702007-06-12 00:33:13 -0400161#ifdef CONFIG_TOUCHSCREEN_USB_ITM
Daniel Ritz1d3e2022006-03-29 22:41:07 +0200162 {USB_DEVICE(0x0403, 0xf9e9), .driver_info = DEVTYPE_ITM},
163#endif
164
Ondrej Zaryc6f8d702007-06-12 00:33:13 -0400165#ifdef CONFIG_TOUCHSCREEN_USB_ETURBO
Daniel Ritz5d892662006-07-31 21:43:24 +0200166 {USB_DEVICE(0x1234, 0x5678), .driver_info = DEVTYPE_ETURBO},
167#endif
168
Ondrej Zaryc6f8d702007-06-12 00:33:13 -0400169#ifdef CONFIG_TOUCHSCREEN_USB_GUNZE
Daniel Ritz5d892662006-07-31 21:43:24 +0200170 {USB_DEVICE(0x0637, 0x0001), .driver_info = DEVTYPE_GUNZE},
171#endif
172
Ondrej Zaryc6f8d702007-06-12 00:33:13 -0400173#ifdef CONFIG_TOUCHSCREEN_USB_DMC_TSC10
Holger Schurig24ced062006-11-17 22:50:15 +0100174 {USB_DEVICE(0x0afa, 0x03e8), .driver_info = DEVTYPE_DMC_TSC10},
175#endif
176
Ondrej Zarydf561fc2007-07-03 01:55:03 -0400177#ifdef CONFIG_TOUCHSCREEN_USB_IRTOUCH
178 {USB_DEVICE(0x595a, 0x0001), .driver_info = DEVTYPE_IRTOUCH},
179 {USB_DEVICE(0x6615, 0x0001), .driver_info = DEVTYPE_IRTOUCH},
180#endif
181
Ondrej Zarya14a8402007-09-04 23:45:01 -0400182#ifdef CONFIG_TOUCHSCREEN_USB_IDEALTEK
183 {USB_DEVICE(0x1391, 0x1000), .driver_info = DEVTYPE_IDEALTEK},
184#endif
185
Ilya Frolov9d5657d2007-10-12 14:19:40 -0400186#ifdef CONFIG_TOUCHSCREEN_USB_GENERAL_TOUCH
187 {USB_DEVICE(0x0dfc, 0x0001), .driver_info = DEVTYPE_GENERAL_TOUCH},
188#endif
189
Jerrold Jones14e40202007-10-22 00:59:47 -0400190#ifdef CONFIG_TOUCHSCREEN_USB_GOTOP
191 {USB_DEVICE(0x08f2, 0x007f), .driver_info = DEVTYPE_GOTOP},
192 {USB_DEVICE(0x08f2, 0x00ce), .driver_info = DEVTYPE_GOTOP},
193 {USB_DEVICE(0x08f2, 0x00f4), .driver_info = DEVTYPE_GOTOP},
194#endif
195
Jim Perssonf7370692009-07-07 22:07:59 -0700196#ifdef CONFIG_TOUCHSCREEN_USB_JASTEC
197 {USB_DEVICE(0x0f92, 0x0001), .driver_info = DEVTYPE_JASTEC},
198#endif
199
Daniel Ritz1d3e2022006-03-29 22:41:07 +0200200 {}
201};
202
203
204/*****************************************************************************
205 * eGalax part
206 */
207
Ondrej Zaryc6f8d702007-06-12 00:33:13 -0400208#ifdef CONFIG_TOUCHSCREEN_USB_EGALAX
Daniel Ritz1d3e2022006-03-29 22:41:07 +0200209
Daniel Ritz62aa3662008-01-17 12:01:18 -0500210#ifndef MULTI_PACKET
211#define MULTI_PACKET
212#endif
213
Daniel Ritz1d3e2022006-03-29 22:41:07 +0200214#define EGALAX_PKT_TYPE_MASK 0xFE
215#define EGALAX_PKT_TYPE_REPT 0x80
216#define EGALAX_PKT_TYPE_DIAG 0x0A
217
Daniel Ritzc9d8c2b2006-12-11 18:17:45 +0100218static int egalax_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
Daniel Ritz1d3e2022006-03-29 22:41:07 +0200219{
220 if ((pkt[0] & EGALAX_PKT_TYPE_MASK) != EGALAX_PKT_TYPE_REPT)
221 return 0;
222
Daniel Ritzc9d8c2b2006-12-11 18:17:45 +0100223 dev->x = ((pkt[3] & 0x0F) << 7) | (pkt[4] & 0x7F);
224 dev->y = ((pkt[1] & 0x0F) << 7) | (pkt[2] & 0x7F);
225 dev->touch = pkt[0] & 0x01;
Daniel Ritz1d3e2022006-03-29 22:41:07 +0200226
227 return 1;
Daniel Ritz1d3e2022006-03-29 22:41:07 +0200228}
229
Daniel Ritz5d892662006-07-31 21:43:24 +0200230static int egalax_get_pkt_len(unsigned char *buf, int len)
Daniel Ritz1d3e2022006-03-29 22:41:07 +0200231{
232 switch (buf[0] & EGALAX_PKT_TYPE_MASK) {
233 case EGALAX_PKT_TYPE_REPT:
234 return 5;
235
236 case EGALAX_PKT_TYPE_DIAG:
Daniel Ritz5d892662006-07-31 21:43:24 +0200237 if (len < 2)
238 return -1;
239
Daniel Ritz1d3e2022006-03-29 22:41:07 +0200240 return buf[1] + 2;
241 }
242
243 return 0;
244}
Daniel Ritz1d3e2022006-03-29 22:41:07 +0200245#endif
246
247
248/*****************************************************************************
249 * PanJit Part
250 */
Ondrej Zaryc6f8d702007-06-12 00:33:13 -0400251#ifdef CONFIG_TOUCHSCREEN_USB_PANJIT
Daniel Ritzc9d8c2b2006-12-11 18:17:45 +0100252static int panjit_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
Daniel Ritz1d3e2022006-03-29 22:41:07 +0200253{
Daniel Ritzc9d8c2b2006-12-11 18:17:45 +0100254 dev->x = ((pkt[2] & 0x0F) << 8) | pkt[1];
255 dev->y = ((pkt[4] & 0x0F) << 8) | pkt[3];
256 dev->touch = pkt[0] & 0x01;
Daniel Ritz1d3e2022006-03-29 22:41:07 +0200257
258 return 1;
259}
260#endif
261
262
263/*****************************************************************************
264 * 3M/Microtouch Part
265 */
Ondrej Zaryc6f8d702007-06-12 00:33:13 -0400266#ifdef CONFIG_TOUCHSCREEN_USB_3M
Daniel Ritz1d3e2022006-03-29 22:41:07 +0200267
268#define MTOUCHUSB_ASYNC_REPORT 1
269#define MTOUCHUSB_RESET 7
270#define MTOUCHUSB_REQ_CTRLLR_ID 10
271
Daniel Ritzc9d8c2b2006-12-11 18:17:45 +0100272static int mtouch_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
Daniel Ritz1d3e2022006-03-29 22:41:07 +0200273{
Dan Streetmanc9cbf3d2009-01-10 23:44:22 -0800274 if (hwcalib_xy) {
275 dev->x = (pkt[4] << 8) | pkt[3];
276 dev->y = 0xffff - ((pkt[6] << 8) | pkt[5]);
277 } else {
278 dev->x = (pkt[8] << 8) | pkt[7];
279 dev->y = (pkt[10] << 8) | pkt[9];
280 }
Daniel Ritzc9d8c2b2006-12-11 18:17:45 +0100281 dev->touch = (pkt[2] & 0x40) ? 1 : 0;
Daniel Ritz1d3e2022006-03-29 22:41:07 +0200282
283 return 1;
284}
285
286static int mtouch_init(struct usbtouch_usb *usbtouch)
287{
Daniel Ritz5d892662006-07-31 21:43:24 +0200288 int ret, i;
Daniel Ritz1d3e2022006-03-29 22:41:07 +0200289
290 ret = usb_control_msg(usbtouch->udev, usb_rcvctrlpipe(usbtouch->udev, 0),
291 MTOUCHUSB_RESET,
292 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
293 1, 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
294 dbg("%s - usb_control_msg - MTOUCHUSB_RESET - bytes|err: %d",
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400295 __func__, ret);
Daniel Ritz1d3e2022006-03-29 22:41:07 +0200296 if (ret < 0)
297 return ret;
Daniel Ritz5d892662006-07-31 21:43:24 +0200298 msleep(150);
Daniel Ritz1d3e2022006-03-29 22:41:07 +0200299
Daniel Ritz5d892662006-07-31 21:43:24 +0200300 for (i = 0; i < 3; i++) {
301 ret = usb_control_msg(usbtouch->udev, usb_rcvctrlpipe(usbtouch->udev, 0),
302 MTOUCHUSB_ASYNC_REPORT,
303 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
304 1, 1, NULL, 0, USB_CTRL_SET_TIMEOUT);
305 dbg("%s - usb_control_msg - MTOUCHUSB_ASYNC_REPORT - bytes|err: %d",
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400306 __func__, ret);
Daniel Ritz5d892662006-07-31 21:43:24 +0200307 if (ret >= 0)
308 break;
309 if (ret != -EPIPE)
310 return ret;
311 }
Daniel Ritz1d3e2022006-03-29 22:41:07 +0200312
Dan Streetmanc9cbf3d2009-01-10 23:44:22 -0800313 /* Default min/max xy are the raw values, override if using hw-calib */
314 if (hwcalib_xy) {
315 input_set_abs_params(usbtouch->input, ABS_X, 0, 0xffff, 0, 0);
316 input_set_abs_params(usbtouch->input, ABS_Y, 0, 0xffff, 0, 0);
317 }
318
Daniel Ritz1d3e2022006-03-29 22:41:07 +0200319 return 0;
320}
321#endif
322
323
324/*****************************************************************************
325 * ITM Part
326 */
Ondrej Zaryc6f8d702007-06-12 00:33:13 -0400327#ifdef CONFIG_TOUCHSCREEN_USB_ITM
Daniel Ritzc9d8c2b2006-12-11 18:17:45 +0100328static int itm_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
Daniel Ritz1d3e2022006-03-29 22:41:07 +0200329{
Daniel Ritzc9d8c2b2006-12-11 18:17:45 +0100330 int touch;
331 /*
332 * ITM devices report invalid x/y data if not touched.
333 * if the screen was touched before but is not touched any more
334 * report touch as 0 with the last valid x/y data once. then stop
335 * reporting data until touched again.
336 */
337 dev->press = ((pkt[2] & 0x01) << 7) | (pkt[5] & 0x7F);
Daniel Ritz1d3e2022006-03-29 22:41:07 +0200338
Daniel Ritzc9d8c2b2006-12-11 18:17:45 +0100339 touch = ~pkt[7] & 0x20;
340 if (!touch) {
341 if (dev->touch) {
342 dev->touch = 0;
343 return 1;
344 }
345
346 return 0;
347 }
348
349 dev->x = ((pkt[0] & 0x1F) << 7) | (pkt[3] & 0x7F);
350 dev->y = ((pkt[1] & 0x1F) << 7) | (pkt[4] & 0x7F);
351 dev->touch = touch;
352
353 return 1;
Daniel Ritz1d3e2022006-03-29 22:41:07 +0200354}
355#endif
356
357
358/*****************************************************************************
Daniel Ritz5d892662006-07-31 21:43:24 +0200359 * eTurboTouch part
360 */
Ondrej Zaryc6f8d702007-06-12 00:33:13 -0400361#ifdef CONFIG_TOUCHSCREEN_USB_ETURBO
Daniel Ritz62aa3662008-01-17 12:01:18 -0500362#ifndef MULTI_PACKET
363#define MULTI_PACKET
364#endif
Daniel Ritzc9d8c2b2006-12-11 18:17:45 +0100365static int eturbo_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
Daniel Ritz5d892662006-07-31 21:43:24 +0200366{
367 unsigned int shift;
368
369 /* packets should start with sync */
370 if (!(pkt[0] & 0x80))
371 return 0;
372
373 shift = (6 - (pkt[0] & 0x03));
Daniel Ritzc9d8c2b2006-12-11 18:17:45 +0100374 dev->x = ((pkt[3] << 7) | pkt[4]) >> shift;
375 dev->y = ((pkt[1] << 7) | pkt[2]) >> shift;
376 dev->touch = (pkt[0] & 0x10) ? 1 : 0;
Daniel Ritz5d892662006-07-31 21:43:24 +0200377
378 return 1;
379}
380
381static int eturbo_get_pkt_len(unsigned char *buf, int len)
382{
383 if (buf[0] & 0x80)
384 return 5;
385 if (buf[0] == 0x01)
386 return 3;
387 return 0;
388}
389#endif
390
391
392/*****************************************************************************
393 * Gunze part
394 */
Ondrej Zaryc6f8d702007-06-12 00:33:13 -0400395#ifdef CONFIG_TOUCHSCREEN_USB_GUNZE
Daniel Ritzc9d8c2b2006-12-11 18:17:45 +0100396static int gunze_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
Daniel Ritz5d892662006-07-31 21:43:24 +0200397{
398 if (!(pkt[0] & 0x80) || ((pkt[1] | pkt[2] | pkt[3]) & 0x80))
399 return 0;
400
Daniel Ritzc9d8c2b2006-12-11 18:17:45 +0100401 dev->x = ((pkt[0] & 0x1F) << 7) | (pkt[2] & 0x7F);
402 dev->y = ((pkt[1] & 0x1F) << 7) | (pkt[3] & 0x7F);
403 dev->touch = pkt[0] & 0x20;
Daniel Ritz5d892662006-07-31 21:43:24 +0200404
405 return 1;
406}
407#endif
408
409/*****************************************************************************
Holger Schurig24ced062006-11-17 22:50:15 +0100410 * DMC TSC-10/25 Part
411 *
412 * Documentation about the controller and it's protocol can be found at
413 * http://www.dmccoltd.com/files/controler/tsc10usb_pi_e.pdf
414 * http://www.dmccoltd.com/files/controler/tsc25_usb_e.pdf
415 */
Ondrej Zaryc6f8d702007-06-12 00:33:13 -0400416#ifdef CONFIG_TOUCHSCREEN_USB_DMC_TSC10
Holger Schurig24ced062006-11-17 22:50:15 +0100417
418/* supported data rates. currently using 130 */
419#define TSC10_RATE_POINT 0x50
420#define TSC10_RATE_30 0x40
421#define TSC10_RATE_50 0x41
422#define TSC10_RATE_80 0x42
423#define TSC10_RATE_100 0x43
424#define TSC10_RATE_130 0x44
425#define TSC10_RATE_150 0x45
426
427/* commands */
428#define TSC10_CMD_RESET 0x55
429#define TSC10_CMD_RATE 0x05
430#define TSC10_CMD_DATA1 0x01
431
432static int dmc_tsc10_init(struct usbtouch_usb *usbtouch)
433{
434 struct usb_device *dev = usbtouch->udev;
Oliver Neukum76d057c2008-04-04 15:31:47 -0400435 int ret = -ENOMEM;
436 unsigned char *buf;
Holger Schurig24ced062006-11-17 22:50:15 +0100437
Oliver Neukum76d057c2008-04-04 15:31:47 -0400438 buf = kmalloc(2, GFP_KERNEL);
439 if (!buf)
440 goto err_nobuf;
Holger Schurig24ced062006-11-17 22:50:15 +0100441 /* reset */
442 buf[0] = buf[1] = 0xFF;
443 ret = usb_control_msg(dev, usb_rcvctrlpipe (dev, 0),
444 TSC10_CMD_RESET,
445 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
446 0, 0, buf, 2, USB_CTRL_SET_TIMEOUT);
447 if (ret < 0)
Oliver Neukum76d057c2008-04-04 15:31:47 -0400448 goto err_out;
Nuno Lucas2ec6f242008-11-11 16:48:04 -0500449 if (buf[0] != 0x06) {
Oliver Neukum76d057c2008-04-04 15:31:47 -0400450 ret = -ENODEV;
451 goto err_out;
452 }
Holger Schurig24ced062006-11-17 22:50:15 +0100453
454 /* set coordinate output rate */
455 buf[0] = buf[1] = 0xFF;
456 ret = usb_control_msg(dev, usb_rcvctrlpipe (dev, 0),
457 TSC10_CMD_RATE,
458 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
459 TSC10_RATE_150, 0, buf, 2, USB_CTRL_SET_TIMEOUT);
460 if (ret < 0)
Oliver Neukum76d057c2008-04-04 15:31:47 -0400461 goto err_out;
Nuno Lucas2ec6f242008-11-11 16:48:04 -0500462 if ((buf[0] != 0x06) && (buf[0] != 0x15 || buf[1] != 0x01)) {
Oliver Neukum76d057c2008-04-04 15:31:47 -0400463 ret = -ENODEV;
464 goto err_out;
465 }
Holger Schurig24ced062006-11-17 22:50:15 +0100466
467 /* start sending data */
468 ret = usb_control_msg(dev, usb_rcvctrlpipe (dev, 0),
469 TSC10_CMD_DATA1,
470 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
471 0, 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
Oliver Neukum76d057c2008-04-04 15:31:47 -0400472err_out:
473 kfree(buf);
474err_nobuf:
475 return ret;
Holger Schurig24ced062006-11-17 22:50:15 +0100476}
477
478
Daniel Ritzc9d8c2b2006-12-11 18:17:45 +0100479static int dmc_tsc10_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
Holger Schurig24ced062006-11-17 22:50:15 +0100480{
Daniel Ritzc9d8c2b2006-12-11 18:17:45 +0100481 dev->x = ((pkt[2] & 0x03) << 8) | pkt[1];
482 dev->y = ((pkt[4] & 0x03) << 8) | pkt[3];
483 dev->touch = pkt[0] & 0x01;
Holger Schurig24ced062006-11-17 22:50:15 +0100484
485 return 1;
486}
487#endif
488
489
490/*****************************************************************************
Ondrej Zarydf561fc2007-07-03 01:55:03 -0400491 * IRTOUCH Part
492 */
493#ifdef CONFIG_TOUCHSCREEN_USB_IRTOUCH
494static int irtouch_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
495{
496 dev->x = (pkt[3] << 8) | pkt[2];
497 dev->y = (pkt[5] << 8) | pkt[4];
498 dev->touch = (pkt[1] & 0x03) ? 1 : 0;
499
500 return 1;
501}
502#endif
503
504
505/*****************************************************************************
Ondrej Zarya14a8402007-09-04 23:45:01 -0400506 * IdealTEK URTC1000 Part
507 */
508#ifdef CONFIG_TOUCHSCREEN_USB_IDEALTEK
Daniel Ritz62aa3662008-01-17 12:01:18 -0500509#ifndef MULTI_PACKET
510#define MULTI_PACKET
511#endif
Ondrej Zarya14a8402007-09-04 23:45:01 -0400512static int idealtek_get_pkt_len(unsigned char *buf, int len)
513{
514 if (buf[0] & 0x80)
515 return 5;
516 if (buf[0] == 0x01)
517 return len;
518 return 0;
519}
520
521static int idealtek_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
522{
523 switch (pkt[0] & 0x98) {
524 case 0x88:
525 /* touch data in IdealTEK mode */
526 dev->x = (pkt[1] << 5) | (pkt[2] >> 2);
527 dev->y = (pkt[3] << 5) | (pkt[4] >> 2);
528 dev->touch = (pkt[0] & 0x40) ? 1 : 0;
529 return 1;
530
531 case 0x98:
532 /* touch data in MT emulation mode */
533 dev->x = (pkt[2] << 5) | (pkt[1] >> 2);
534 dev->y = (pkt[4] << 5) | (pkt[3] >> 2);
535 dev->touch = (pkt[0] & 0x40) ? 1 : 0;
536 return 1;
537
538 default:
539 return 0;
540 }
541}
542#endif
543
Ilya Frolov9d5657d2007-10-12 14:19:40 -0400544/*****************************************************************************
545 * General Touch Part
546 */
547#ifdef CONFIG_TOUCHSCREEN_USB_GENERAL_TOUCH
548static int general_touch_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
549{
550 dev->x = ((pkt[2] & 0x0F) << 8) | pkt[1] ;
551 dev->y = ((pkt[4] & 0x0F) << 8) | pkt[3] ;
552 dev->press = pkt[5] & 0xff;
553 dev->touch = pkt[0] & 0x01;
554
555 return 1;
556}
557#endif
Ondrej Zarya14a8402007-09-04 23:45:01 -0400558
559/*****************************************************************************
Jerrold Jones14e40202007-10-22 00:59:47 -0400560 * GoTop Part
561 */
562#ifdef CONFIG_TOUCHSCREEN_USB_GOTOP
563static int gotop_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
564{
565 dev->x = ((pkt[1] & 0x38) << 4) | pkt[2];
566 dev->y = ((pkt[1] & 0x07) << 7) | pkt[3];
567 dev->touch = pkt[0] & 0x01;
Jim Perssonf7370692009-07-07 22:07:59 -0700568
569 return 1;
570}
571#endif
572
573/*****************************************************************************
574 * JASTEC Part
575 */
576#ifdef CONFIG_TOUCHSCREEN_USB_JASTEC
577static int jastec_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
578{
579 dev->x = ((pkt[0] & 0x3f) << 6) | (pkt[2] & 0x3f);
580 dev->y = ((pkt[1] & 0x3f) << 6) | (pkt[3] & 0x3f);
581 dev->touch = (pkt[0] & 0x40) >> 6;
582
Jerrold Jones14e40202007-10-22 00:59:47 -0400583 return 1;
584}
585#endif
586
587
588/*****************************************************************************
Daniel Ritz1d3e2022006-03-29 22:41:07 +0200589 * the different device descriptors
590 */
Daniel Ritz62aa3662008-01-17 12:01:18 -0500591#ifdef MULTI_PACKET
592static void usbtouch_process_multi(struct usbtouch_usb *usbtouch,
593 unsigned char *pkt, int len);
594#endif
595
Daniel Ritz1d3e2022006-03-29 22:41:07 +0200596static struct usbtouch_device_info usbtouch_dev_info[] = {
Ondrej Zaryc6f8d702007-06-12 00:33:13 -0400597#ifdef CONFIG_TOUCHSCREEN_USB_EGALAX
Daniel Ritz1d3e2022006-03-29 22:41:07 +0200598 [DEVTYPE_EGALAX] = {
599 .min_xc = 0x0,
600 .max_xc = 0x07ff,
601 .min_yc = 0x0,
602 .max_yc = 0x07ff,
603 .rept_size = 16,
Daniel Ritz5d892662006-07-31 21:43:24 +0200604 .process_pkt = usbtouch_process_multi,
605 .get_pkt_len = egalax_get_pkt_len,
Daniel Ritz1d3e2022006-03-29 22:41:07 +0200606 .read_data = egalax_read_data,
607 },
608#endif
609
Ondrej Zaryc6f8d702007-06-12 00:33:13 -0400610#ifdef CONFIG_TOUCHSCREEN_USB_PANJIT
Daniel Ritz1d3e2022006-03-29 22:41:07 +0200611 [DEVTYPE_PANJIT] = {
612 .min_xc = 0x0,
613 .max_xc = 0x0fff,
614 .min_yc = 0x0,
615 .max_yc = 0x0fff,
616 .rept_size = 8,
617 .read_data = panjit_read_data,
618 },
619#endif
620
Ondrej Zaryc6f8d702007-06-12 00:33:13 -0400621#ifdef CONFIG_TOUCHSCREEN_USB_3M
Daniel Ritz1d3e2022006-03-29 22:41:07 +0200622 [DEVTYPE_3M] = {
623 .min_xc = 0x0,
624 .max_xc = 0x4000,
625 .min_yc = 0x0,
626 .max_yc = 0x4000,
627 .rept_size = 11,
628 .read_data = mtouch_read_data,
629 .init = mtouch_init,
630 },
631#endif
632
Ondrej Zaryc6f8d702007-06-12 00:33:13 -0400633#ifdef CONFIG_TOUCHSCREEN_USB_ITM
Daniel Ritz1d3e2022006-03-29 22:41:07 +0200634 [DEVTYPE_ITM] = {
635 .min_xc = 0x0,
636 .max_xc = 0x0fff,
637 .min_yc = 0x0,
638 .max_yc = 0x0fff,
639 .max_press = 0xff,
640 .rept_size = 8,
641 .read_data = itm_read_data,
642 },
643#endif
Daniel Ritz5d892662006-07-31 21:43:24 +0200644
Ondrej Zaryc6f8d702007-06-12 00:33:13 -0400645#ifdef CONFIG_TOUCHSCREEN_USB_ETURBO
Daniel Ritz5d892662006-07-31 21:43:24 +0200646 [DEVTYPE_ETURBO] = {
647 .min_xc = 0x0,
648 .max_xc = 0x07ff,
649 .min_yc = 0x0,
650 .max_yc = 0x07ff,
651 .rept_size = 8,
Daniel Ritz5d892662006-07-31 21:43:24 +0200652 .process_pkt = usbtouch_process_multi,
653 .get_pkt_len = eturbo_get_pkt_len,
654 .read_data = eturbo_read_data,
655 },
656#endif
657
Ondrej Zaryc6f8d702007-06-12 00:33:13 -0400658#ifdef CONFIG_TOUCHSCREEN_USB_GUNZE
Daniel Ritz5d892662006-07-31 21:43:24 +0200659 [DEVTYPE_GUNZE] = {
660 .min_xc = 0x0,
661 .max_xc = 0x0fff,
662 .min_yc = 0x0,
663 .max_yc = 0x0fff,
664 .rept_size = 4,
665 .read_data = gunze_read_data,
666 },
667#endif
Holger Schurig24ced062006-11-17 22:50:15 +0100668
Ondrej Zaryc6f8d702007-06-12 00:33:13 -0400669#ifdef CONFIG_TOUCHSCREEN_USB_DMC_TSC10
Holger Schurig24ced062006-11-17 22:50:15 +0100670 [DEVTYPE_DMC_TSC10] = {
671 .min_xc = 0x0,
672 .max_xc = 0x03ff,
673 .min_yc = 0x0,
674 .max_yc = 0x03ff,
675 .rept_size = 5,
676 .init = dmc_tsc10_init,
677 .read_data = dmc_tsc10_read_data,
678 },
679#endif
Ondrej Zarydf561fc2007-07-03 01:55:03 -0400680
681#ifdef CONFIG_TOUCHSCREEN_USB_IRTOUCH
682 [DEVTYPE_IRTOUCH] = {
683 .min_xc = 0x0,
684 .max_xc = 0x0fff,
685 .min_yc = 0x0,
686 .max_yc = 0x0fff,
687 .rept_size = 8,
688 .read_data = irtouch_read_data,
689 },
690#endif
Ondrej Zarya14a8402007-09-04 23:45:01 -0400691
692#ifdef CONFIG_TOUCHSCREEN_USB_IDEALTEK
693 [DEVTYPE_IDEALTEK] = {
694 .min_xc = 0x0,
695 .max_xc = 0x0fff,
696 .min_yc = 0x0,
697 .max_yc = 0x0fff,
698 .rept_size = 8,
Ondrej Zarya14a8402007-09-04 23:45:01 -0400699 .process_pkt = usbtouch_process_multi,
700 .get_pkt_len = idealtek_get_pkt_len,
701 .read_data = idealtek_read_data,
702 },
703#endif
Ilya Frolov9d5657d2007-10-12 14:19:40 -0400704
705#ifdef CONFIG_TOUCHSCREEN_USB_GENERAL_TOUCH
706 [DEVTYPE_GENERAL_TOUCH] = {
707 .min_xc = 0x0,
708 .max_xc = 0x0500,
709 .min_yc = 0x0,
710 .max_yc = 0x0500,
711 .rept_size = 7,
712 .read_data = general_touch_read_data,
Jerrold Jones14e40202007-10-22 00:59:47 -0400713 },
Ilya Frolov9d5657d2007-10-12 14:19:40 -0400714#endif
715
Jerrold Jones14e40202007-10-22 00:59:47 -0400716#ifdef CONFIG_TOUCHSCREEN_USB_GOTOP
717 [DEVTYPE_GOTOP] = {
718 .min_xc = 0x0,
719 .max_xc = 0x03ff,
720 .min_yc = 0x0,
721 .max_yc = 0x03ff,
722 .rept_size = 4,
723 .read_data = gotop_read_data,
724 },
725#endif
Jim Perssonf7370692009-07-07 22:07:59 -0700726
727#ifdef CONFIG_TOUCHSCREEN_USB_JASTEC
728 [DEVTYPE_JASTEC] = {
729 .min_xc = 0x0,
730 .max_xc = 0x0fff,
731 .min_yc = 0x0,
732 .max_yc = 0x0fff,
733 .rept_size = 4,
734 .read_data = jastec_read_data,
735 },
736#endif
Daniel Ritz1d3e2022006-03-29 22:41:07 +0200737};
738
739
740/*****************************************************************************
741 * Generic Part
742 */
743static void usbtouch_process_pkt(struct usbtouch_usb *usbtouch,
David Howells7d12e782006-10-05 14:55:46 +0100744 unsigned char *pkt, int len)
Daniel Ritz1d3e2022006-03-29 22:41:07 +0200745{
Daniel Ritz1d3e2022006-03-29 22:41:07 +0200746 struct usbtouch_device_info *type = usbtouch->type;
747
Daniel Ritzc9d8c2b2006-12-11 18:17:45 +0100748 if (!type->read_data(usbtouch, pkt))
Daniel Ritz1d3e2022006-03-29 22:41:07 +0200749 return;
750
Daniel Ritzc9d8c2b2006-12-11 18:17:45 +0100751 input_report_key(usbtouch->input, BTN_TOUCH, usbtouch->touch);
Daniel Ritz1d3e2022006-03-29 22:41:07 +0200752
753 if (swap_xy) {
Daniel Ritzc9d8c2b2006-12-11 18:17:45 +0100754 input_report_abs(usbtouch->input, ABS_X, usbtouch->y);
755 input_report_abs(usbtouch->input, ABS_Y, usbtouch->x);
Daniel Ritz1d3e2022006-03-29 22:41:07 +0200756 } else {
Daniel Ritzc9d8c2b2006-12-11 18:17:45 +0100757 input_report_abs(usbtouch->input, ABS_X, usbtouch->x);
758 input_report_abs(usbtouch->input, ABS_Y, usbtouch->y);
Daniel Ritz1d3e2022006-03-29 22:41:07 +0200759 }
760 if (type->max_press)
Daniel Ritzc9d8c2b2006-12-11 18:17:45 +0100761 input_report_abs(usbtouch->input, ABS_PRESSURE, usbtouch->press);
Daniel Ritz1d3e2022006-03-29 22:41:07 +0200762 input_sync(usbtouch->input);
763}
764
765
Daniel Ritz5d892662006-07-31 21:43:24 +0200766#ifdef MULTI_PACKET
767static void usbtouch_process_multi(struct usbtouch_usb *usbtouch,
Daniel Ritz5d892662006-07-31 21:43:24 +0200768 unsigned char *pkt, int len)
769{
770 unsigned char *buffer;
771 int pkt_len, pos, buf_len, tmp;
772
773 /* process buffer */
774 if (unlikely(usbtouch->buf_len)) {
775 /* try to get size */
776 pkt_len = usbtouch->type->get_pkt_len(
777 usbtouch->buffer, usbtouch->buf_len);
778
779 /* drop? */
780 if (unlikely(!pkt_len))
781 goto out_flush_buf;
782
783 /* need to append -pkt_len bytes before able to get size */
784 if (unlikely(pkt_len < 0)) {
785 int append = -pkt_len;
786 if (unlikely(append > len))
787 append = len;
788 if (usbtouch->buf_len + append >= usbtouch->type->rept_size)
789 goto out_flush_buf;
790 memcpy(usbtouch->buffer + usbtouch->buf_len, pkt, append);
791 usbtouch->buf_len += append;
792
793 pkt_len = usbtouch->type->get_pkt_len(
794 usbtouch->buffer, usbtouch->buf_len);
795 if (pkt_len < 0)
796 return;
797 }
798
799 /* append */
800 tmp = pkt_len - usbtouch->buf_len;
801 if (usbtouch->buf_len + tmp >= usbtouch->type->rept_size)
802 goto out_flush_buf;
803 memcpy(usbtouch->buffer + usbtouch->buf_len, pkt, tmp);
David Howells7d12e782006-10-05 14:55:46 +0100804 usbtouch_process_pkt(usbtouch, usbtouch->buffer, pkt_len);
Daniel Ritz5d892662006-07-31 21:43:24 +0200805
806 buffer = pkt + tmp;
807 buf_len = len - tmp;
808 } else {
809 buffer = pkt;
810 buf_len = len;
811 }
812
813 /* loop over the received packet, process */
814 pos = 0;
815 while (pos < buf_len) {
816 /* get packet len */
Daniel Ritz62aa3662008-01-17 12:01:18 -0500817 pkt_len = usbtouch->type->get_pkt_len(buffer + pos,
818 buf_len - pos);
Daniel Ritz5d892662006-07-31 21:43:24 +0200819
Daniel Ritz62aa3662008-01-17 12:01:18 -0500820 /* unknown packet: skip one byte */
821 if (unlikely(!pkt_len)) {
822 pos++;
823 continue;
824 }
Daniel Ritz5d892662006-07-31 21:43:24 +0200825
826 /* full packet: process */
827 if (likely((pkt_len > 0) && (pkt_len <= buf_len - pos))) {
David Howells7d12e782006-10-05 14:55:46 +0100828 usbtouch_process_pkt(usbtouch, buffer + pos, pkt_len);
Daniel Ritz5d892662006-07-31 21:43:24 +0200829 } else {
830 /* incomplete packet: save in buffer */
831 memcpy(usbtouch->buffer, buffer + pos, buf_len - pos);
832 usbtouch->buf_len = buf_len - pos;
833 return;
834 }
835 pos += pkt_len;
836 }
837
838out_flush_buf:
839 usbtouch->buf_len = 0;
840 return;
841}
842#endif
843
844
David Howells7d12e782006-10-05 14:55:46 +0100845static void usbtouch_irq(struct urb *urb)
Daniel Ritz1d3e2022006-03-29 22:41:07 +0200846{
847 struct usbtouch_usb *usbtouch = urb->context;
848 int retval;
849
850 switch (urb->status) {
851 case 0:
852 /* success */
853 break;
Pete Zaitcev38e2bfc2006-09-18 22:49:02 -0700854 case -ETIME:
Daniel Ritz1d3e2022006-03-29 22:41:07 +0200855 /* this urb is timing out */
856 dbg("%s - urb timed out - was the device unplugged?",
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400857 __func__);
Daniel Ritz1d3e2022006-03-29 22:41:07 +0200858 return;
859 case -ECONNRESET:
860 case -ENOENT:
861 case -ESHUTDOWN:
862 /* this urb is terminated, clean up */
863 dbg("%s - urb shutting down with status: %d",
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400864 __func__, urb->status);
Daniel Ritz1d3e2022006-03-29 22:41:07 +0200865 return;
866 default:
867 dbg("%s - nonzero urb status received: %d",
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400868 __func__, urb->status);
Daniel Ritz1d3e2022006-03-29 22:41:07 +0200869 goto exit;
870 }
871
David Howells7d12e782006-10-05 14:55:46 +0100872 usbtouch->type->process_pkt(usbtouch, usbtouch->data, urb->actual_length);
Daniel Ritz1d3e2022006-03-29 22:41:07 +0200873
874exit:
875 retval = usb_submit_urb(urb, GFP_ATOMIC);
876 if (retval)
877 err("%s - usb_submit_urb failed with result: %d",
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400878 __func__, retval);
Daniel Ritz1d3e2022006-03-29 22:41:07 +0200879}
880
881static int usbtouch_open(struct input_dev *input)
882{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400883 struct usbtouch_usb *usbtouch = input_get_drvdata(input);
Daniel Ritz1d3e2022006-03-29 22:41:07 +0200884
885 usbtouch->irq->dev = usbtouch->udev;
886
887 if (usb_submit_urb(usbtouch->irq, GFP_KERNEL))
888 return -EIO;
889
890 return 0;
891}
892
893static void usbtouch_close(struct input_dev *input)
894{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400895 struct usbtouch_usb *usbtouch = input_get_drvdata(input);
Daniel Ritz1d3e2022006-03-29 22:41:07 +0200896
897 usb_kill_urb(usbtouch->irq);
898}
899
900
901static void usbtouch_free_buffers(struct usb_device *udev,
902 struct usbtouch_usb *usbtouch)
903{
Dmitry Torokhove37a97d2007-05-03 00:57:29 -0400904 usb_buffer_free(udev, usbtouch->type->rept_size,
905 usbtouch->data, usbtouch->data_dma);
Daniel Ritz1d3e2022006-03-29 22:41:07 +0200906 kfree(usbtouch->buffer);
907}
908
909
910static int usbtouch_probe(struct usb_interface *intf,
911 const struct usb_device_id *id)
912{
913 struct usbtouch_usb *usbtouch;
914 struct input_dev *input_dev;
915 struct usb_host_interface *interface;
916 struct usb_endpoint_descriptor *endpoint;
917 struct usb_device *udev = interface_to_usbdev(intf);
918 struct usbtouch_device_info *type;
Daniel Ritz5d892662006-07-31 21:43:24 +0200919 int err = -ENOMEM;
Daniel Ritz1d3e2022006-03-29 22:41:07 +0200920
Daniel Ritzec42d442008-07-03 10:45:37 -0400921 /* some devices are ignored */
922 if (id->driver_info == DEVTYPE_IGNORE)
923 return -ENODEV;
924
Daniel Ritz1d3e2022006-03-29 22:41:07 +0200925 interface = intf->cur_altsetting;
926 endpoint = &interface->endpoint[0].desc;
927
928 usbtouch = kzalloc(sizeof(struct usbtouch_usb), GFP_KERNEL);
929 input_dev = input_allocate_device();
930 if (!usbtouch || !input_dev)
931 goto out_free;
932
933 type = &usbtouch_dev_info[id->driver_info];
934 usbtouch->type = type;
935 if (!type->process_pkt)
936 type->process_pkt = usbtouch_process_pkt;
937
938 usbtouch->data = usb_buffer_alloc(udev, type->rept_size,
Christoph Lametere94b1762006-12-06 20:33:17 -0800939 GFP_KERNEL, &usbtouch->data_dma);
Daniel Ritz1d3e2022006-03-29 22:41:07 +0200940 if (!usbtouch->data)
941 goto out_free;
942
Daniel Ritz62aa3662008-01-17 12:01:18 -0500943 if (type->get_pkt_len) {
Daniel Ritz1d3e2022006-03-29 22:41:07 +0200944 usbtouch->buffer = kmalloc(type->rept_size, GFP_KERNEL);
945 if (!usbtouch->buffer)
946 goto out_free_buffers;
947 }
948
949 usbtouch->irq = usb_alloc_urb(0, GFP_KERNEL);
950 if (!usbtouch->irq) {
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400951 dbg("%s - usb_alloc_urb failed: usbtouch->irq", __func__);
Daniel Ritz1d3e2022006-03-29 22:41:07 +0200952 goto out_free_buffers;
953 }
954
955 usbtouch->udev = udev;
956 usbtouch->input = input_dev;
957
958 if (udev->manufacturer)
959 strlcpy(usbtouch->name, udev->manufacturer, sizeof(usbtouch->name));
960
961 if (udev->product) {
962 if (udev->manufacturer)
963 strlcat(usbtouch->name, " ", sizeof(usbtouch->name));
964 strlcat(usbtouch->name, udev->product, sizeof(usbtouch->name));
965 }
966
967 if (!strlen(usbtouch->name))
968 snprintf(usbtouch->name, sizeof(usbtouch->name),
969 "USB Touchscreen %04x:%04x",
970 le16_to_cpu(udev->descriptor.idVendor),
971 le16_to_cpu(udev->descriptor.idProduct));
972
973 usb_make_path(udev, usbtouch->phys, sizeof(usbtouch->phys));
Vladimir Shebordaev7b6dff92007-09-06 21:32:16 -0400974 strlcat(usbtouch->phys, "/input0", sizeof(usbtouch->phys));
Daniel Ritz1d3e2022006-03-29 22:41:07 +0200975
976 input_dev->name = usbtouch->name;
977 input_dev->phys = usbtouch->phys;
978 usb_to_input_id(udev, &input_dev->id);
Dmitry Torokhovc0f82d52007-04-12 01:35:03 -0400979 input_dev->dev.parent = &intf->dev;
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400980
981 input_set_drvdata(input_dev, usbtouch);
982
Daniel Ritz1d3e2022006-03-29 22:41:07 +0200983 input_dev->open = usbtouch_open;
984 input_dev->close = usbtouch_close;
985
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700986 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
987 input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
Daniel Ritz1d3e2022006-03-29 22:41:07 +0200988 input_set_abs_params(input_dev, ABS_X, type->min_xc, type->max_xc, 0, 0);
989 input_set_abs_params(input_dev, ABS_Y, type->min_yc, type->max_yc, 0, 0);
990 if (type->max_press)
991 input_set_abs_params(input_dev, ABS_PRESSURE, type->min_press,
992 type->max_press, 0, 0);
993
994 usb_fill_int_urb(usbtouch->irq, usbtouch->udev,
Daniel Ritzd8fa59a2006-10-27 22:46:03 +0200995 usb_rcvintpipe(usbtouch->udev, endpoint->bEndpointAddress),
Daniel Ritz1d3e2022006-03-29 22:41:07 +0200996 usbtouch->data, type->rept_size,
997 usbtouch_irq, usbtouch, endpoint->bInterval);
998
Daniel Ritz5d892662006-07-31 21:43:24 +0200999 usbtouch->irq->dev = usbtouch->udev;
Daniel Ritz1d3e2022006-03-29 22:41:07 +02001000 usbtouch->irq->transfer_dma = usbtouch->data_dma;
1001 usbtouch->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1002
1003 /* device specific init */
1004 if (type->init) {
1005 err = type->init(usbtouch);
1006 if (err) {
Harvey Harrisonea3e6c52008-05-05 11:36:18 -04001007 dbg("%s - type->init() failed, err: %d", __func__, err);
Daniel Ritz1d3e2022006-03-29 22:41:07 +02001008 goto out_free_buffers;
1009 }
1010 }
1011
1012 err = input_register_device(usbtouch->input);
1013 if (err) {
Harvey Harrisonea3e6c52008-05-05 11:36:18 -04001014 dbg("%s - input_register_device failed, err: %d", __func__, err);
Daniel Ritz1d3e2022006-03-29 22:41:07 +02001015 goto out_free_buffers;
1016 }
1017
1018 usb_set_intfdata(intf, usbtouch);
1019
1020 return 0;
1021
1022out_free_buffers:
1023 usbtouch_free_buffers(udev, usbtouch);
1024out_free:
1025 input_free_device(input_dev);
1026 kfree(usbtouch);
Daniel Ritz5d892662006-07-31 21:43:24 +02001027 return err;
Daniel Ritz1d3e2022006-03-29 22:41:07 +02001028}
1029
1030static void usbtouch_disconnect(struct usb_interface *intf)
1031{
1032 struct usbtouch_usb *usbtouch = usb_get_intfdata(intf);
1033
Harvey Harrisonea3e6c52008-05-05 11:36:18 -04001034 dbg("%s - called", __func__);
Daniel Ritz1d3e2022006-03-29 22:41:07 +02001035
1036 if (!usbtouch)
1037 return;
1038
Harvey Harrisonea3e6c52008-05-05 11:36:18 -04001039 dbg("%s - usbtouch is initialized, cleaning up", __func__);
Daniel Ritz1d3e2022006-03-29 22:41:07 +02001040 usb_set_intfdata(intf, NULL);
1041 usb_kill_urb(usbtouch->irq);
1042 input_unregister_device(usbtouch->input);
1043 usb_free_urb(usbtouch->irq);
1044 usbtouch_free_buffers(interface_to_usbdev(intf), usbtouch);
1045 kfree(usbtouch);
1046}
1047
1048MODULE_DEVICE_TABLE(usb, usbtouch_devices);
1049
1050static struct usb_driver usbtouch_driver = {
1051 .name = "usbtouchscreen",
1052 .probe = usbtouch_probe,
1053 .disconnect = usbtouch_disconnect,
1054 .id_table = usbtouch_devices,
1055};
1056
1057static int __init usbtouch_init(void)
1058{
1059 return usb_register(&usbtouch_driver);
1060}
1061
1062static void __exit usbtouch_cleanup(void)
1063{
1064 usb_deregister(&usbtouch_driver);
1065}
1066
1067module_init(usbtouch_init);
1068module_exit(usbtouch_cleanup);
1069
1070MODULE_AUTHOR(DRIVER_AUTHOR);
1071MODULE_DESCRIPTION(DRIVER_DESC);
1072MODULE_LICENSE("GPL");
1073
1074MODULE_ALIAS("touchkitusb");
1075MODULE_ALIAS("itmtouch");
1076MODULE_ALIAS("mtouchusb");