blob: 0043e6ebcd1f0ada7cc751bab444e92fae6b6e03 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/******************************************************************************
2 * touchkitusb.c -- Driver for eGalax TouchKit USB Touchscreens
3 *
4 * Copyright (C) 2004 by Daniel Ritz
5 * Copyright (C) by Todd E. Johnson (mtouchusb.c)
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of the
10 * License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 *
21 * Based upon mtouchusb.c
22 *
23 *****************************************************************************/
24
25//#define DEBUG
26
27#include <linux/config.h>
28#include <linux/kernel.h>
29#include <linux/slab.h>
30#include <linux/input.h>
31#include <linux/module.h>
32#include <linux/init.h>
33
34#if !defined(DEBUG) && defined(CONFIG_USB_DEBUG)
35#define DEBUG
36#endif
37#include <linux/usb.h>
Dmitry Torokhov16a334c2005-06-30 00:49:08 -050038#include <linux/usb_input.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40#define TOUCHKIT_MIN_XC 0x0
41#define TOUCHKIT_MAX_XC 0x07ff
42#define TOUCHKIT_XC_FUZZ 0x0
43#define TOUCHKIT_XC_FLAT 0x0
44#define TOUCHKIT_MIN_YC 0x0
45#define TOUCHKIT_MAX_YC 0x07ff
46#define TOUCHKIT_YC_FUZZ 0x0
47#define TOUCHKIT_YC_FLAT 0x0
48#define TOUCHKIT_REPORT_DATA_SIZE 8
49
50#define TOUCHKIT_DOWN 0x01
51#define TOUCHKIT_POINT_TOUCH 0x81
52#define TOUCHKIT_POINT_NOTOUCH 0x80
53
54#define TOUCHKIT_GET_TOUCHED(dat) ((((dat)[0]) & TOUCHKIT_DOWN) ? 1 : 0)
55#define TOUCHKIT_GET_X(dat) (((dat)[3] << 7) | (dat)[4])
56#define TOUCHKIT_GET_Y(dat) (((dat)[1] << 7) | (dat)[2])
57
58#define DRIVER_VERSION "v0.1"
59#define DRIVER_AUTHOR "Daniel Ritz <daniel.ritz@gmx.ch>"
60#define DRIVER_DESC "eGalax TouchKit USB HID Touchscreen Driver"
61
62static int swap_xy;
63module_param(swap_xy, bool, 0644);
64MODULE_PARM_DESC(swap_xy, "If set X and Y axes are swapped.");
65
66struct touchkit_usb {
67 unsigned char *data;
68 dma_addr_t data_dma;
69 struct urb *irq;
70 struct usb_device *udev;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -050071 struct input_dev *input;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 char name[128];
73 char phys[64];
74};
75
76static struct usb_device_id touchkit_devices[] = {
77 {USB_DEVICE(0x3823, 0x0001)},
Daniel Ritz3f8c03e2005-09-28 21:30:12 +020078 {USB_DEVICE(0x0123, 0x0001)},
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 {USB_DEVICE(0x0eef, 0x0001)},
Daniel Ritz3f8c03e2005-09-28 21:30:12 +020080 {USB_DEVICE(0x0eef, 0x0002)},
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 {}
82};
83
84static void touchkit_irq(struct urb *urb, struct pt_regs *regs)
85{
86 struct touchkit_usb *touchkit = urb->context;
87 int retval;
88 int x, y;
89
90 switch (urb->status) {
91 case 0:
92 /* success */
93 break;
94 case -ETIMEDOUT:
95 /* this urb is timing out */
96 dbg("%s - urb timed out - was the device unplugged?",
97 __FUNCTION__);
98 return;
99 case -ECONNRESET:
100 case -ENOENT:
101 case -ESHUTDOWN:
102 /* this urb is terminated, clean up */
103 dbg("%s - urb shutting down with status: %d",
104 __FUNCTION__, urb->status);
105 return;
106 default:
107 dbg("%s - nonzero urb status received: %d",
108 __FUNCTION__, urb->status);
109 goto exit;
110 }
111
112 if (swap_xy) {
113 y = TOUCHKIT_GET_X(touchkit->data);
114 x = TOUCHKIT_GET_Y(touchkit->data);
115 } else {
116 x = TOUCHKIT_GET_X(touchkit->data);
117 y = TOUCHKIT_GET_Y(touchkit->data);
118 }
119
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500120 input_regs(touchkit->input, regs);
121 input_report_key(touchkit->input, BTN_TOUCH,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 TOUCHKIT_GET_TOUCHED(touchkit->data));
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500123 input_report_abs(touchkit->input, ABS_X, x);
124 input_report_abs(touchkit->input, ABS_Y, y);
125 input_sync(touchkit->input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127exit:
128 retval = usb_submit_urb(urb, GFP_ATOMIC);
129 if (retval)
130 err("%s - usb_submit_urb failed with result: %d",
131 __FUNCTION__, retval);
132}
133
134static int touchkit_open(struct input_dev *input)
135{
136 struct touchkit_usb *touchkit = input->private;
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 touchkit->irq->dev = touchkit->udev;
139
Dmitry Torokhov65cde542005-05-29 02:29:38 -0500140 if (usb_submit_urb(touchkit->irq, GFP_ATOMIC))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143 return 0;
144}
145
146static void touchkit_close(struct input_dev *input)
147{
148 struct touchkit_usb *touchkit = input->private;
149
Dmitry Torokhov65cde542005-05-29 02:29:38 -0500150 usb_kill_urb(touchkit->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
153static int touchkit_alloc_buffers(struct usb_device *udev,
154 struct touchkit_usb *touchkit)
155{
156 touchkit->data = usb_buffer_alloc(udev, TOUCHKIT_REPORT_DATA_SIZE,
157 SLAB_ATOMIC, &touchkit->data_dma);
158
159 if (!touchkit->data)
160 return -1;
161
162 return 0;
163}
164
165static void touchkit_free_buffers(struct usb_device *udev,
166 struct touchkit_usb *touchkit)
167{
168 if (touchkit->data)
169 usb_buffer_free(udev, TOUCHKIT_REPORT_DATA_SIZE,
170 touchkit->data, touchkit->data_dma);
171}
172
173static int touchkit_probe(struct usb_interface *intf,
174 const struct usb_device_id *id)
175{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 struct touchkit_usb *touchkit;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500177 struct input_dev *input_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 struct usb_host_interface *interface;
179 struct usb_endpoint_descriptor *endpoint;
180 struct usb_device *udev = interface_to_usbdev(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 interface = intf->cur_altsetting;
183 endpoint = &interface->endpoint[0].desc;
184
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500185 touchkit = kzalloc(sizeof(struct touchkit_usb), GFP_KERNEL);
186 input_dev = input_allocate_device();
187 if (!touchkit || !input_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500190 if (touchkit_alloc_buffers(udev, touchkit))
191 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193 touchkit->irq = usb_alloc_urb(0, GFP_KERNEL);
194 if (!touchkit->irq) {
195 dbg("%s - usb_alloc_urb failed: touchkit->irq", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 goto out_free_buffers;
197 }
198
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500199 touchkit->udev = udev;
200 touchkit->input = input_dev;
201
202 if (udev->manufacturer)
203 strlcpy(touchkit->name, udev->manufacturer, sizeof(touchkit->name));
204
205 if (udev->product) {
206 if (udev->manufacturer)
207 strlcat(touchkit->name, " ", sizeof(touchkit->name));
208 strlcat(touchkit->name, udev->product, sizeof(touchkit->name));
209 }
210
211 if (!strlen(touchkit->name))
212 snprintf(touchkit->name, sizeof(touchkit->name),
213 "USB Touchscreen %04x:%04x",
214 le16_to_cpu(udev->descriptor.idVendor),
215 le16_to_cpu(udev->descriptor.idProduct));
216
217 usb_make_path(udev, touchkit->phys, sizeof(touchkit->phys));
218 strlcpy(touchkit->phys, "/input0", sizeof(touchkit->phys));
219
220 input_dev->name = touchkit->name;
221 input_dev->phys = touchkit->phys;
222 usb_to_input_id(udev, &input_dev->id);
223 input_dev->cdev.dev = &intf->dev;
224 input_dev->private = touchkit;
225 input_dev->open = touchkit_open;
226 input_dev->close = touchkit_close;
227
228 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
229 input_dev->keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH);
230 input_set_abs_params(input_dev, ABS_X, TOUCHKIT_MIN_XC, TOUCHKIT_MAX_XC,
231 TOUCHKIT_XC_FUZZ, TOUCHKIT_XC_FLAT);
232 input_set_abs_params(input_dev, ABS_Y, TOUCHKIT_MIN_YC, TOUCHKIT_MAX_YC,
233 TOUCHKIT_YC_FUZZ, TOUCHKIT_YC_FLAT);
234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 usb_fill_int_urb(touchkit->irq, touchkit->udev,
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500236 usb_rcvintpipe(touchkit->udev, 0x81),
237 touchkit->data, TOUCHKIT_REPORT_DATA_SIZE,
238 touchkit_irq, touchkit, endpoint->bInterval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500240 input_register_device(touchkit->input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 usb_set_intfdata(intf, touchkit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 return 0;
244
245out_free_buffers:
246 touchkit_free_buffers(udev, touchkit);
247out_free:
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500248 input_free_device(input_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 kfree(touchkit);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500250 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}
252
253static void touchkit_disconnect(struct usb_interface *intf)
254{
255 struct touchkit_usb *touchkit = usb_get_intfdata(intf);
256
257 dbg("%s - called", __FUNCTION__);
258
259 if (!touchkit)
260 return;
261
262 dbg("%s - touchkit is initialized, cleaning up", __FUNCTION__);
263 usb_set_intfdata(intf, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 usb_kill_urb(touchkit->irq);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500265 input_unregister_device(touchkit->input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 usb_free_urb(touchkit->irq);
267 touchkit_free_buffers(interface_to_usbdev(intf), touchkit);
268 kfree(touchkit);
269}
270
271MODULE_DEVICE_TABLE(usb, touchkit_devices);
272
273static struct usb_driver touchkit_driver = {
274 .owner = THIS_MODULE,
275 .name = "touchkitusb",
276 .probe = touchkit_probe,
277 .disconnect = touchkit_disconnect,
278 .id_table = touchkit_devices,
279};
280
281static int __init touchkit_init(void)
282{
283 return usb_register(&touchkit_driver);
284}
285
286static void __exit touchkit_cleanup(void)
287{
288 usb_deregister(&touchkit_driver);
289}
290
291module_init(touchkit_init);
292module_exit(touchkit_cleanup);
293
294MODULE_AUTHOR(DRIVER_AUTHOR);
295MODULE_DESCRIPTION(DRIVER_DESC);
296MODULE_LICENSE("GPL");