blob: 09b5cc7c66de15167a5445bae090694cb6e9b08f [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>
56
57#define MTOUCHUSB_MIN_XC 0x0
58#define MTOUCHUSB_MAX_RAW_XC 0x4000
59#define MTOUCHUSB_MAX_CALIB_XC 0xffff
60#define MTOUCHUSB_XC_FUZZ 0x0
61#define MTOUCHUSB_XC_FLAT 0x0
62#define MTOUCHUSB_MIN_YC 0x0
63#define MTOUCHUSB_MAX_RAW_YC 0x4000
64#define MTOUCHUSB_MAX_CALIB_YC 0xffff
65#define MTOUCHUSB_YC_FUZZ 0x0
66#define MTOUCHUSB_YC_FLAT 0x0
67
68#define MTOUCHUSB_ASYNC_REPORT 1
69#define MTOUCHUSB_RESET 7
70#define MTOUCHUSB_REPORT_DATA_SIZE 11
71#define MTOUCHUSB_REQ_CTRLLR_ID 10
72
73#define MTOUCHUSB_GET_RAW_XC(data) (data[8]<<8 | data[7])
74#define MTOUCHUSB_GET_CALIB_XC(data) (data[4]<<8 | data[3])
75#define MTOUCHUSB_GET_RAW_YC(data) (data[10]<<8 | data[9])
76#define MTOUCHUSB_GET_CALIB_YC(data) (data[6]<<8 | data[5])
77#define MTOUCHUSB_GET_XC(data) (raw_coordinates ? \
78 MTOUCHUSB_GET_RAW_XC(data) : \
79 MTOUCHUSB_GET_CALIB_XC(data))
80#define MTOUCHUSB_GET_YC(data) (raw_coordinates ? \
81 MTOUCHUSB_GET_RAW_YC(data) : \
82 MTOUCHUSB_GET_CALIB_YC(data))
83#define MTOUCHUSB_GET_TOUCHED(data) ((data[2] & 0x40) ? 1:0)
84
85#define DRIVER_VERSION "v1.5"
86#define DRIVER_AUTHOR "Todd E. Johnson, tejohnson@yahoo.com"
87#define DRIVER_DESC "3M USB Touchscreen Driver"
88#define DRIVER_LICENSE "GPL"
89
90static int raw_coordinates = 1;
91
92module_param(raw_coordinates, bool, S_IRUGO | S_IWUSR);
93MODULE_PARM_DESC(raw_coordinates, "report raw coordinate values (y, default) or hardware-calibrated coordinate values (n)");
94
95struct mtouch_usb {
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -050096 unsigned char *data;
97 dma_addr_t data_dma;
98 struct urb *irq;
99 struct usb_device *udev;
100 struct input_dev input;
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500101 char name[128];
102 char phys[64];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103};
104
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500105static struct usb_device_id mtouchusb_devices[] = {
106 { USB_DEVICE(0x0596, 0x0001) },
107 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108};
109
110static void mtouchusb_irq(struct urb *urb, struct pt_regs *regs)
111{
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500112 struct mtouch_usb *mtouch = urb->context;
113 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500115 switch (urb->status) {
116 case 0:
117 /* success */
118 break;
119 case -ETIMEDOUT:
120 /* this urb is timing out */
121 dbg("%s - urb timed out - was the device unplugged?",
122 __FUNCTION__);
123 return;
124 case -ECONNRESET:
125 case -ENOENT:
126 case -ESHUTDOWN:
127 /* this urb is terminated, clean up */
128 dbg("%s - urb shutting down with status: %d",
129 __FUNCTION__, urb->status);
130 return;
131 default:
132 dbg("%s - nonzero urb status received: %d",
133 __FUNCTION__, urb->status);
134 goto exit;
135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500137 input_regs(&mtouch->input, regs);
138 input_report_key(&mtouch->input, BTN_TOUCH,
139 MTOUCHUSB_GET_TOUCHED(mtouch->data));
140 input_report_abs(&mtouch->input, ABS_X, MTOUCHUSB_GET_XC(mtouch->data));
141 input_report_abs(&mtouch->input, ABS_Y,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 (raw_coordinates ? MTOUCHUSB_MAX_RAW_YC : MTOUCHUSB_MAX_CALIB_YC)
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500143 - MTOUCHUSB_GET_YC(mtouch->data));
144 input_sync(&mtouch->input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146exit:
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500147 retval = usb_submit_urb(urb, GFP_ATOMIC);
148 if (retval)
149 err("%s - usb_submit_urb failed with result: %d",
150 __FUNCTION__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500153static int mtouchusb_open(struct input_dev *input)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500155 struct mtouch_usb *mtouch = input->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500157 mtouch->irq->dev = mtouch->udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Dmitry Torokhov65cde542005-05-29 02:29:38 -0500159 if (usb_submit_urb(mtouch->irq, GFP_ATOMIC))
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500160 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500162 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
164
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500165static void mtouchusb_close(struct input_dev *input)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500167 struct mtouch_usb *mtouch = input->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Dmitry Torokhov65cde542005-05-29 02:29:38 -0500169 usb_kill_urb(mtouch->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
172static int mtouchusb_alloc_buffers(struct usb_device *udev, struct mtouch_usb *mtouch)
173{
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500174 dbg("%s - called", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500176 mtouch->data = usb_buffer_alloc(udev, MTOUCHUSB_REPORT_DATA_SIZE,
177 SLAB_ATOMIC, &mtouch->data_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500179 if (!mtouch->data)
180 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500182 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183}
184
185static void mtouchusb_free_buffers(struct usb_device *udev, struct mtouch_usb *mtouch)
186{
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500187 dbg("%s - called", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500189 if (mtouch->data)
190 usb_buffer_free(udev, MTOUCHUSB_REPORT_DATA_SIZE,
191 mtouch->data, mtouch->data_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
193
194static int mtouchusb_probe(struct usb_interface *intf, const struct usb_device_id *id)
195{
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500196 struct mtouch_usb *mtouch;
197 struct usb_host_interface *interface;
198 struct usb_endpoint_descriptor *endpoint;
199 struct usb_device *udev = interface_to_usbdev(intf);
200 char path[64];
201 int nRet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500203 dbg("%s - called", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500205 dbg("%s - setting interface", __FUNCTION__);
206 interface = intf->cur_altsetting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500208 dbg("%s - setting endpoint", __FUNCTION__);
209 endpoint = &interface->endpoint[0].desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500211 if (!(mtouch = kmalloc(sizeof(struct mtouch_usb), GFP_KERNEL))) {
212 err("%s - Out of memory.", __FUNCTION__);
213 return -ENOMEM;
214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500216 memset(mtouch, 0, sizeof(struct mtouch_usb));
217 mtouch->udev = udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500219 dbg("%s - allocating buffers", __FUNCTION__);
220 if (mtouchusb_alloc_buffers(udev, mtouch)) {
221 mtouchusb_free_buffers(udev, mtouch);
222 kfree(mtouch);
223 return -ENOMEM;
224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500226 mtouch->input.private = mtouch;
227 mtouch->input.open = mtouchusb_open;
228 mtouch->input.close = mtouchusb_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500230 usb_make_path(udev, path, 64);
231 sprintf(mtouch->phys, "%s/input0", path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500233 mtouch->input.name = mtouch->name;
234 mtouch->input.phys = mtouch->phys;
235 mtouch->input.id.bustype = BUS_USB;
236 mtouch->input.id.vendor = le16_to_cpu(udev->descriptor.idVendor);
237 mtouch->input.id.product = le16_to_cpu(udev->descriptor.idProduct);
238 mtouch->input.id.version = le16_to_cpu(udev->descriptor.bcdDevice);
239 mtouch->input.dev = &intf->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500241 mtouch->input.evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
242 mtouch->input.absbit[0] = BIT(ABS_X) | BIT(ABS_Y);
243 mtouch->input.keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500245 /* Used to Scale Compensated Data and Flip Y */
246 mtouch->input.absmin[ABS_X] = MTOUCHUSB_MIN_XC;
247 mtouch->input.absmax[ABS_X] = raw_coordinates ?
248 MTOUCHUSB_MAX_RAW_XC : MTOUCHUSB_MAX_CALIB_XC;
249 mtouch->input.absfuzz[ABS_X] = MTOUCHUSB_XC_FUZZ;
250 mtouch->input.absflat[ABS_X] = MTOUCHUSB_XC_FLAT;
251 mtouch->input.absmin[ABS_Y] = MTOUCHUSB_MIN_YC;
252 mtouch->input.absmax[ABS_Y] = raw_coordinates ?
253 MTOUCHUSB_MAX_RAW_YC : MTOUCHUSB_MAX_CALIB_YC;
254 mtouch->input.absfuzz[ABS_Y] = MTOUCHUSB_YC_FUZZ;
255 mtouch->input.absflat[ABS_Y] = MTOUCHUSB_YC_FLAT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 if (udev->manufacturer)
258 strcat(mtouch->name, udev->manufacturer);
259 if (udev->product)
260 sprintf(mtouch->name, "%s %s", mtouch->name, udev->product);
261
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500262 if (!strlen(mtouch->name))
263 sprintf(mtouch->name, "USB Touchscreen %04x:%04x",
264 mtouch->input.id.vendor, mtouch->input.id.product);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500266 nRet = usb_control_msg(mtouch->udev, usb_rcvctrlpipe(udev, 0),
267 MTOUCHUSB_RESET,
268 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
269 1, 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
270 dbg("%s - usb_control_msg - MTOUCHUSB_RESET - bytes|err: %d",
271 __FUNCTION__, nRet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500273 dbg("%s - usb_alloc_urb: mtouch->irq", __FUNCTION__);
274 mtouch->irq = usb_alloc_urb(0, GFP_KERNEL);
275 if (!mtouch->irq) {
276 dbg("%s - usb_alloc_urb failed: mtouch->irq", __FUNCTION__);
277 mtouchusb_free_buffers(udev, mtouch);
278 kfree(mtouch);
279 return -ENOMEM;
280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500282 dbg("%s - usb_fill_int_urb", __FUNCTION__);
283 usb_fill_int_urb(mtouch->irq, mtouch->udev,
284 usb_rcvintpipe(mtouch->udev, 0x81),
285 mtouch->data, MTOUCHUSB_REPORT_DATA_SIZE,
286 mtouchusb_irq, mtouch, endpoint->bInterval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500288 dbg("%s - input_register_device", __FUNCTION__);
289 input_register_device(&mtouch->input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500291 nRet = usb_control_msg(mtouch->udev, usb_rcvctrlpipe(udev, 0),
292 MTOUCHUSB_ASYNC_REPORT,
293 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
294 1, 1, NULL, 0, USB_CTRL_SET_TIMEOUT);
295 dbg("%s - usb_control_msg - MTOUCHUSB_ASYNC_REPORT - bytes|err: %d",
296 __FUNCTION__, nRet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500298 printk(KERN_INFO "input: %s on %s\n", mtouch->name, path);
299 usb_set_intfdata(intf, mtouch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500301 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302}
303
304static void mtouchusb_disconnect(struct usb_interface *intf)
305{
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500306 struct mtouch_usb *mtouch = usb_get_intfdata(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500308 dbg("%s - called", __FUNCTION__);
309 usb_set_intfdata(intf, NULL);
310 if (mtouch) {
311 dbg("%s - mtouch is initialized, cleaning up", __FUNCTION__);
312 usb_kill_urb(mtouch->irq);
313 input_unregister_device(&mtouch->input);
314 usb_free_urb(mtouch->irq);
315 mtouchusb_free_buffers(interface_to_usbdev(intf), mtouch);
316 kfree(mtouch);
317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318}
319
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500320MODULE_DEVICE_TABLE(usb, mtouchusb_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
322static struct usb_driver mtouchusb_driver = {
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500323 .owner = THIS_MODULE,
324 .name = "mtouchusb",
325 .probe = mtouchusb_probe,
326 .disconnect = mtouchusb_disconnect,
327 .id_table = mtouchusb_devices,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328};
329
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500330static int __init mtouchusb_init(void)
331{
332 dbg("%s - called", __FUNCTION__);
333 return usb_register(&mtouchusb_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334}
335
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500336static void __exit mtouchusb_cleanup(void)
337{
338 dbg("%s - called", __FUNCTION__);
339 usb_deregister(&mtouchusb_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}
341
342module_init(mtouchusb_init);
343module_exit(mtouchusb_cleanup);
344
Dmitry Torokhov8baf9ed2005-05-29 02:29:08 -0500345MODULE_AUTHOR(DRIVER_AUTHOR);
346MODULE_DESCRIPTION(DRIVER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347MODULE_LICENSE("GPL");