blob: 7fce526560ca0f396496a4cd3d9c1f154404c7a3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/******************************************************************************
2 * mtouchusb.c -- Driver for Microtouch (Now 3M) USB Touchscreens
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 *
18 * Based upon original work by Radoslaw Garbacz (usb-support@ite.pl)
19 * (http://freshmeat.net/projects/3mtouchscreendriver)
20 *
21 * History
22 *
23 * 0.3 & 0.4 2002 (TEJ) tejohnson@yahoo.com
24 * Updated to 2.4.18, then 2.4.19
25 * Old version still relied on stealing a minor
26 *
27 * 0.5 02/26/2004 (TEJ) tejohnson@yahoo.com
28 * Complete rewrite using Linux Input in 2.6.3
29 * Unfortunately no calibration support at this time
30 *
31 * 1.4 04/25/2004 (TEJ) tejohnson@yahoo.com
32 * Changed reset from standard USB dev reset to vendor reset
33 * Changed data sent to host from compensated to raw coordinates
34 * Eliminated vendor/product module params
Steven Cole093cf722005-05-03 19:07:24 -060035 * Performed multiple successful tests with an EXII-5010UC
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 *
37 * 1.5 02/27/2005 ddstreet@ieee.org
38 * Added module parameter to select raw or hw-calibrated coordinate reporting
39 *
40 *****************************************************************************/
41
42#include <linux/config.h>
43
44#ifdef CONFIG_USB_DEBUG
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -050045 #define DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#else
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -050047 #undef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#endif
49
50#include <linux/kernel.h>
51#include <linux/slab.h>
52#include <linux/input.h>
53#include <linux/module.h>
54#include <linux/init.h>
55#include <linux/usb.h>
Dmitry Torokhov16a334c2005-06-30 00:49:08 -050056#include <linux/usb_input.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58#define MTOUCHUSB_MIN_XC 0x0
59#define MTOUCHUSB_MAX_RAW_XC 0x4000
60#define MTOUCHUSB_MAX_CALIB_XC 0xffff
61#define MTOUCHUSB_XC_FUZZ 0x0
62#define MTOUCHUSB_XC_FLAT 0x0
63#define MTOUCHUSB_MIN_YC 0x0
64#define MTOUCHUSB_MAX_RAW_YC 0x4000
65#define MTOUCHUSB_MAX_CALIB_YC 0xffff
66#define MTOUCHUSB_YC_FUZZ 0x0
67#define MTOUCHUSB_YC_FLAT 0x0
68
69#define MTOUCHUSB_ASYNC_REPORT 1
70#define MTOUCHUSB_RESET 7
71#define MTOUCHUSB_REPORT_DATA_SIZE 11
72#define MTOUCHUSB_REQ_CTRLLR_ID 10
73
74#define MTOUCHUSB_GET_RAW_XC(data) (data[8]<<8 | data[7])
75#define MTOUCHUSB_GET_CALIB_XC(data) (data[4]<<8 | data[3])
76#define MTOUCHUSB_GET_RAW_YC(data) (data[10]<<8 | data[9])
77#define MTOUCHUSB_GET_CALIB_YC(data) (data[6]<<8 | data[5])
78#define MTOUCHUSB_GET_XC(data) (raw_coordinates ? \
79 MTOUCHUSB_GET_RAW_XC(data) : \
80 MTOUCHUSB_GET_CALIB_XC(data))
81#define MTOUCHUSB_GET_YC(data) (raw_coordinates ? \
82 MTOUCHUSB_GET_RAW_YC(data) : \
83 MTOUCHUSB_GET_CALIB_YC(data))
84#define MTOUCHUSB_GET_TOUCHED(data) ((data[2] & 0x40) ? 1:0)
85
86#define DRIVER_VERSION "v1.5"
87#define DRIVER_AUTHOR "Todd E. Johnson, tejohnson@yahoo.com"
88#define DRIVER_DESC "3M USB Touchscreen Driver"
89#define DRIVER_LICENSE "GPL"
90
91static int raw_coordinates = 1;
92
93module_param(raw_coordinates, bool, S_IRUGO | S_IWUSR);
94MODULE_PARM_DESC(raw_coordinates, "report raw coordinate values (y, default) or hardware-calibrated coordinate values (n)");
95
96struct mtouch_usb {
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -050097 unsigned char *data;
98 dma_addr_t data_dma;
99 struct urb *irq;
100 struct usb_device *udev;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500101 struct input_dev *input;
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500102 char name[128];
103 char phys[64];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104};
105
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500106static struct usb_device_id mtouchusb_devices[] = {
107 { USB_DEVICE(0x0596, 0x0001) },
108 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109};
110
111static void mtouchusb_irq(struct urb *urb, struct pt_regs *regs)
112{
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500113 struct mtouch_usb *mtouch = urb->context;
114 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500116 switch (urb->status) {
117 case 0:
118 /* success */
119 break;
120 case -ETIMEDOUT:
121 /* this urb is timing out */
122 dbg("%s - urb timed out - was the device unplugged?",
123 __FUNCTION__);
124 return;
125 case -ECONNRESET:
126 case -ENOENT:
127 case -ESHUTDOWN:
128 /* this urb is terminated, clean up */
129 dbg("%s - urb shutting down with status: %d",
130 __FUNCTION__, urb->status);
131 return;
132 default:
133 dbg("%s - nonzero urb status received: %d",
134 __FUNCTION__, urb->status);
135 goto exit;
136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500138 input_regs(mtouch->input, regs);
139 input_report_key(mtouch->input, BTN_TOUCH,
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500140 MTOUCHUSB_GET_TOUCHED(mtouch->data));
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500141 input_report_abs(mtouch->input, ABS_X, MTOUCHUSB_GET_XC(mtouch->data));
142 input_report_abs(mtouch->input, ABS_Y,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 (raw_coordinates ? MTOUCHUSB_MAX_RAW_YC : MTOUCHUSB_MAX_CALIB_YC)
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500144 - MTOUCHUSB_GET_YC(mtouch->data));
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500145 input_sync(mtouch->input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147exit:
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500148 retval = usb_submit_urb(urb, GFP_ATOMIC);
149 if (retval)
150 err("%s - usb_submit_urb failed with result: %d",
151 __FUNCTION__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152}
153
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500154static int mtouchusb_open(struct input_dev *input)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500156 struct mtouch_usb *mtouch = input->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500158 mtouch->irq->dev = mtouch->udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Dmitry Torokhov65cde542005-05-29 02:29:38 -0500160 if (usb_submit_urb(mtouch->irq, GFP_ATOMIC))
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500161 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500163 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164}
165
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500166static void mtouchusb_close(struct input_dev *input)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500168 struct mtouch_usb *mtouch = input->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Dmitry Torokhov65cde542005-05-29 02:29:38 -0500170 usb_kill_urb(mtouch->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
173static int mtouchusb_alloc_buffers(struct usb_device *udev, struct mtouch_usb *mtouch)
174{
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500175 dbg("%s - called", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500177 mtouch->data = usb_buffer_alloc(udev, MTOUCHUSB_REPORT_DATA_SIZE,
178 SLAB_ATOMIC, &mtouch->data_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500180 if (!mtouch->data)
181 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500183 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
185
186static void mtouchusb_free_buffers(struct usb_device *udev, struct mtouch_usb *mtouch)
187{
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500188 dbg("%s - called", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500190 if (mtouch->data)
191 usb_buffer_free(udev, MTOUCHUSB_REPORT_DATA_SIZE,
192 mtouch->data, mtouch->data_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
194
195static int mtouchusb_probe(struct usb_interface *intf, const struct usb_device_id *id)
196{
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500197 struct mtouch_usb *mtouch;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500198 struct input_dev *input_dev;
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500199 struct usb_host_interface *interface;
200 struct usb_endpoint_descriptor *endpoint;
201 struct usb_device *udev = interface_to_usbdev(intf);
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500202 int nRet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500204 dbg("%s - called", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500206 dbg("%s - setting interface", __FUNCTION__);
207 interface = intf->cur_altsetting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500209 dbg("%s - setting endpoint", __FUNCTION__);
210 endpoint = &interface->endpoint[0].desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500212 mtouch = kzalloc(sizeof(struct mtouch_usb), GFP_KERNEL);
213 input_dev = input_allocate_device();
214 if (!mtouch || !input_dev) {
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500215 err("%s - Out of memory.", __FUNCTION__);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500216 goto fail1;
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500219 dbg("%s - allocating buffers", __FUNCTION__);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500220 if (mtouchusb_alloc_buffers(udev, mtouch))
221 goto fail2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500223 mtouch->udev = udev;
224 mtouch->input = input_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 if (udev->manufacturer)
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500227 strlcpy(mtouch->name, udev->manufacturer, sizeof(mtouch->name));
228
229 if (udev->product) {
230 if (udev->manufacturer)
231 strlcat(mtouch->name, " ", sizeof(mtouch->name));
232 strlcat(mtouch->name, udev->product, sizeof(mtouch->name));
233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500235 if (!strlen(mtouch->name))
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500236 snprintf(mtouch->name, sizeof(mtouch->name),
237 "USB Touchscreen %04x:%04x",
238 le16_to_cpu(udev->descriptor.idVendor),
239 le16_to_cpu(udev->descriptor.idProduct));
240
241 usb_make_path(udev, mtouch->phys, sizeof(mtouch->phys));
242 strlcpy(mtouch->phys, "/input0", sizeof(mtouch->phys));
243
244 input_dev->name = mtouch->name;
245 input_dev->phys = mtouch->phys;
246 usb_to_input_id(udev, &input_dev->id);
247 input_dev->cdev.dev = &intf->dev;
248 input_dev->private = mtouch;
249
250 input_dev->open = mtouchusb_open;
251 input_dev->close = mtouchusb_close;
252
253 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
254 input_dev->keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH);
255 input_set_abs_params(input_dev, ABS_X, MTOUCHUSB_MIN_XC,
256 raw_coordinates ? MTOUCHUSB_MAX_RAW_XC : MTOUCHUSB_MAX_CALIB_XC,
257 MTOUCHUSB_XC_FUZZ, MTOUCHUSB_XC_FLAT);
258 input_set_abs_params(input_dev, ABS_Y, MTOUCHUSB_MIN_YC,
259 raw_coordinates ? MTOUCHUSB_MAX_RAW_YC : MTOUCHUSB_MAX_CALIB_YC,
260 MTOUCHUSB_YC_FUZZ, MTOUCHUSB_YC_FLAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500262 nRet = usb_control_msg(mtouch->udev, usb_rcvctrlpipe(udev, 0),
263 MTOUCHUSB_RESET,
264 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
265 1, 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
266 dbg("%s - usb_control_msg - MTOUCHUSB_RESET - bytes|err: %d",
267 __FUNCTION__, nRet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500269 dbg("%s - usb_alloc_urb: mtouch->irq", __FUNCTION__);
270 mtouch->irq = usb_alloc_urb(0, GFP_KERNEL);
271 if (!mtouch->irq) {
272 dbg("%s - usb_alloc_urb failed: mtouch->irq", __FUNCTION__);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500273 goto fail2;
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500276 dbg("%s - usb_fill_int_urb", __FUNCTION__);
277 usb_fill_int_urb(mtouch->irq, mtouch->udev,
278 usb_rcvintpipe(mtouch->udev, 0x81),
279 mtouch->data, MTOUCHUSB_REPORT_DATA_SIZE,
280 mtouchusb_irq, mtouch, endpoint->bInterval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500282 dbg("%s - input_register_device", __FUNCTION__);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500283 input_register_device(mtouch->input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500285 nRet = usb_control_msg(mtouch->udev, usb_rcvctrlpipe(udev, 0),
286 MTOUCHUSB_ASYNC_REPORT,
287 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
288 1, 1, NULL, 0, USB_CTRL_SET_TIMEOUT);
289 dbg("%s - usb_control_msg - MTOUCHUSB_ASYNC_REPORT - bytes|err: %d",
290 __FUNCTION__, nRet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500292 usb_set_intfdata(intf, mtouch);
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500293 return 0;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500294
295fail2: mtouchusb_free_buffers(udev, mtouch);
296fail1: input_free_device(input_dev);
297 kfree(mtouch);
298 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
300
301static void mtouchusb_disconnect(struct usb_interface *intf)
302{
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500303 struct mtouch_usb *mtouch = usb_get_intfdata(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500305 dbg("%s - called", __FUNCTION__);
306 usb_set_intfdata(intf, NULL);
307 if (mtouch) {
308 dbg("%s - mtouch is initialized, cleaning up", __FUNCTION__);
309 usb_kill_urb(mtouch->irq);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500310 input_unregister_device(mtouch->input);
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500311 usb_free_urb(mtouch->irq);
312 mtouchusb_free_buffers(interface_to_usbdev(intf), mtouch);
313 kfree(mtouch);
314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315}
316
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500317MODULE_DEVICE_TABLE(usb, mtouchusb_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319static struct usb_driver mtouchusb_driver = {
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500320 .owner = THIS_MODULE,
321 .name = "mtouchusb",
322 .probe = mtouchusb_probe,
323 .disconnect = mtouchusb_disconnect,
324 .id_table = mtouchusb_devices,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325};
326
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500327static int __init mtouchusb_init(void)
328{
329 dbg("%s - called", __FUNCTION__);
330 return usb_register(&mtouchusb_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331}
332
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500333static void __exit mtouchusb_cleanup(void)
334{
335 dbg("%s - called", __FUNCTION__);
336 usb_deregister(&mtouchusb_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
338
339module_init(mtouchusb_init);
340module_exit(mtouchusb_cleanup);
341
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500342MODULE_AUTHOR(DRIVER_AUTHOR);
343MODULE_DESCRIPTION(DRIVER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344MODULE_LICENSE("GPL");