[PATCH] i386: Account spinlocks to the caller during profiling for !FP kernels
[linux-2.6/verdex.git] / drivers / usb / input / touchkitusb.c
blob0149043ffb975366b6e41cc3522734773c83ee32
1 /******************************************************************************
2 * touchkitusb.c -- Driver for eGalax TouchKit USB Touchscreens
4 * Copyright (C) 2004-2005 by Daniel Ritz <daniel.ritz@gmx.ch>
5 * Copyright (C) by Todd E. Johnson (mtouchusb.c)
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.
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.
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.
21 * Based upon mtouchusb.c
23 *****************************************************************************/
25 //#define DEBUG
27 #include <linux/kernel.h>
28 #include <linux/slab.h>
29 #include <linux/module.h>
30 #include <linux/init.h>
31 #include <linux/usb/input.h>
33 #define TOUCHKIT_MIN_XC 0x0
34 #define TOUCHKIT_MAX_XC 0x07ff
35 #define TOUCHKIT_XC_FUZZ 0x0
36 #define TOUCHKIT_XC_FLAT 0x0
37 #define TOUCHKIT_MIN_YC 0x0
38 #define TOUCHKIT_MAX_YC 0x07ff
39 #define TOUCHKIT_YC_FUZZ 0x0
40 #define TOUCHKIT_YC_FLAT 0x0
41 #define TOUCHKIT_REPORT_DATA_SIZE 16
43 #define TOUCHKIT_DOWN 0x01
45 #define TOUCHKIT_PKT_TYPE_MASK 0xFE
46 #define TOUCHKIT_PKT_TYPE_REPT 0x80
47 #define TOUCHKIT_PKT_TYPE_DIAG 0x0A
49 #define DRIVER_VERSION "v0.1"
50 #define DRIVER_AUTHOR "Daniel Ritz <daniel.ritz@gmx.ch>"
51 #define DRIVER_DESC "eGalax TouchKit USB HID Touchscreen Driver"
53 static int swap_xy;
54 module_param(swap_xy, bool, 0644);
55 MODULE_PARM_DESC(swap_xy, "If set X and Y axes are swapped.");
57 struct touchkit_usb {
58 unsigned char *data;
59 dma_addr_t data_dma;
60 char buffer[TOUCHKIT_REPORT_DATA_SIZE];
61 int buf_len;
62 struct urb *irq;
63 struct usb_device *udev;
64 struct input_dev *input;
65 char name[128];
66 char phys[64];
69 static struct usb_device_id touchkit_devices[] = {
70 {USB_DEVICE(0x3823, 0x0001)},
71 {USB_DEVICE(0x0123, 0x0001)},
72 {USB_DEVICE(0x0eef, 0x0001)},
73 {USB_DEVICE(0x0eef, 0x0002)},
77 /* helpers to read the data */
78 static inline int touchkit_get_touched(char *data)
80 return (data[0] & TOUCHKIT_DOWN) ? 1 : 0;
83 static inline int touchkit_get_x(char *data)
85 return ((data[3] & 0x0F) << 7) | (data[4] & 0x7F);
88 static inline int touchkit_get_y(char *data)
90 return ((data[1] & 0x0F) << 7) | (data[2] & 0x7F);
94 /* processes one input packet. */
95 static void touchkit_process_pkt(struct touchkit_usb *touchkit,
96 struct pt_regs *regs, char *pkt)
98 int x, y;
100 /* only process report packets */
101 if ((pkt[0] & TOUCHKIT_PKT_TYPE_MASK) != TOUCHKIT_PKT_TYPE_REPT)
102 return;
104 if (swap_xy) {
105 y = touchkit_get_x(pkt);
106 x = touchkit_get_y(pkt);
107 } else {
108 x = touchkit_get_x(pkt);
109 y = touchkit_get_y(pkt);
112 input_regs(touchkit->input, regs);
113 input_report_key(touchkit->input, BTN_TOUCH, touchkit_get_touched(pkt));
114 input_report_abs(touchkit->input, ABS_X, x);
115 input_report_abs(touchkit->input, ABS_Y, y);
116 input_sync(touchkit->input);
120 static int touchkit_get_pkt_len(char *buf)
122 switch (buf[0] & TOUCHKIT_PKT_TYPE_MASK) {
123 case TOUCHKIT_PKT_TYPE_REPT:
124 return 5;
126 case TOUCHKIT_PKT_TYPE_DIAG:
127 return buf[1] + 2;
130 return 0;
133 static void touchkit_process(struct touchkit_usb *touchkit, int len,
134 struct pt_regs *regs)
136 char *buffer;
137 int pkt_len, buf_len, pos;
139 /* if the buffer contains data, append */
140 if (unlikely(touchkit->buf_len)) {
141 int tmp;
143 /* if only 1 byte in buffer, add another one to get length */
144 if (touchkit->buf_len == 1)
145 touchkit->buffer[1] = touchkit->data[0];
147 pkt_len = touchkit_get_pkt_len(touchkit->buffer);
149 /* unknown packet: drop everything */
150 if (!pkt_len)
151 return;
153 /* append, process */
154 tmp = pkt_len - touchkit->buf_len;
155 memcpy(touchkit->buffer + touchkit->buf_len, touchkit->data, tmp);
156 touchkit_process_pkt(touchkit, regs, touchkit->buffer);
158 buffer = touchkit->data + tmp;
159 buf_len = len - tmp;
160 } else {
161 buffer = touchkit->data;
162 buf_len = len;
165 /* only one byte left in buffer */
166 if (unlikely(buf_len == 1)) {
167 touchkit->buffer[0] = buffer[0];
168 touchkit->buf_len = 1;
169 return;
172 /* loop over the buffer */
173 pos = 0;
174 while (pos < buf_len) {
175 /* get packet len */
176 pkt_len = touchkit_get_pkt_len(buffer + pos);
178 /* unknown packet: drop everything */
179 if (unlikely(!pkt_len))
180 return;
182 /* full packet: process */
183 if (likely(pkt_len <= buf_len)) {
184 touchkit_process_pkt(touchkit, regs, buffer + pos);
185 } else {
186 /* incomplete packet: save in buffer */
187 memcpy(touchkit->buffer, buffer + pos, buf_len - pos);
188 touchkit->buf_len = buf_len - pos;
190 pos += pkt_len;
195 static void touchkit_irq(struct urb *urb, struct pt_regs *regs)
197 struct touchkit_usb *touchkit = urb->context;
198 int retval;
200 switch (urb->status) {
201 case 0:
202 /* success */
203 break;
204 case -ETIMEDOUT:
205 /* this urb is timing out */
206 dbg("%s - urb timed out - was the device unplugged?",
207 __FUNCTION__);
208 return;
209 case -ECONNRESET:
210 case -ENOENT:
211 case -ESHUTDOWN:
212 /* this urb is terminated, clean up */
213 dbg("%s - urb shutting down with status: %d",
214 __FUNCTION__, urb->status);
215 return;
216 default:
217 dbg("%s - nonzero urb status received: %d",
218 __FUNCTION__, urb->status);
219 goto exit;
222 touchkit_process(touchkit, urb->actual_length, regs);
224 exit:
225 retval = usb_submit_urb(urb, GFP_ATOMIC);
226 if (retval)
227 err("%s - usb_submit_urb failed with result: %d",
228 __FUNCTION__, retval);
231 static int touchkit_open(struct input_dev *input)
233 struct touchkit_usb *touchkit = input->private;
235 touchkit->irq->dev = touchkit->udev;
237 if (usb_submit_urb(touchkit->irq, GFP_ATOMIC))
238 return -EIO;
240 return 0;
243 static void touchkit_close(struct input_dev *input)
245 struct touchkit_usb *touchkit = input->private;
247 usb_kill_urb(touchkit->irq);
250 static int touchkit_alloc_buffers(struct usb_device *udev,
251 struct touchkit_usb *touchkit)
253 touchkit->data = usb_buffer_alloc(udev, TOUCHKIT_REPORT_DATA_SIZE,
254 SLAB_ATOMIC, &touchkit->data_dma);
256 if (!touchkit->data)
257 return -1;
259 return 0;
262 static void touchkit_free_buffers(struct usb_device *udev,
263 struct touchkit_usb *touchkit)
265 if (touchkit->data)
266 usb_buffer_free(udev, TOUCHKIT_REPORT_DATA_SIZE,
267 touchkit->data, touchkit->data_dma);
270 static int touchkit_probe(struct usb_interface *intf,
271 const struct usb_device_id *id)
273 struct touchkit_usb *touchkit;
274 struct input_dev *input_dev;
275 struct usb_host_interface *interface;
276 struct usb_endpoint_descriptor *endpoint;
277 struct usb_device *udev = interface_to_usbdev(intf);
279 interface = intf->cur_altsetting;
280 endpoint = &interface->endpoint[0].desc;
282 touchkit = kzalloc(sizeof(struct touchkit_usb), GFP_KERNEL);
283 input_dev = input_allocate_device();
284 if (!touchkit || !input_dev)
285 goto out_free;
287 if (touchkit_alloc_buffers(udev, touchkit))
288 goto out_free;
290 touchkit->irq = usb_alloc_urb(0, GFP_KERNEL);
291 if (!touchkit->irq) {
292 dbg("%s - usb_alloc_urb failed: touchkit->irq", __FUNCTION__);
293 goto out_free_buffers;
296 touchkit->udev = udev;
297 touchkit->input = input_dev;
299 if (udev->manufacturer)
300 strlcpy(touchkit->name, udev->manufacturer, sizeof(touchkit->name));
302 if (udev->product) {
303 if (udev->manufacturer)
304 strlcat(touchkit->name, " ", sizeof(touchkit->name));
305 strlcat(touchkit->name, udev->product, sizeof(touchkit->name));
308 if (!strlen(touchkit->name))
309 snprintf(touchkit->name, sizeof(touchkit->name),
310 "USB Touchscreen %04x:%04x",
311 le16_to_cpu(udev->descriptor.idVendor),
312 le16_to_cpu(udev->descriptor.idProduct));
314 usb_make_path(udev, touchkit->phys, sizeof(touchkit->phys));
315 strlcpy(touchkit->phys, "/input0", sizeof(touchkit->phys));
317 input_dev->name = touchkit->name;
318 input_dev->phys = touchkit->phys;
319 usb_to_input_id(udev, &input_dev->id);
320 input_dev->cdev.dev = &intf->dev;
321 input_dev->private = touchkit;
322 input_dev->open = touchkit_open;
323 input_dev->close = touchkit_close;
325 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
326 input_dev->keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH);
327 input_set_abs_params(input_dev, ABS_X, TOUCHKIT_MIN_XC, TOUCHKIT_MAX_XC,
328 TOUCHKIT_XC_FUZZ, TOUCHKIT_XC_FLAT);
329 input_set_abs_params(input_dev, ABS_Y, TOUCHKIT_MIN_YC, TOUCHKIT_MAX_YC,
330 TOUCHKIT_YC_FUZZ, TOUCHKIT_YC_FLAT);
332 usb_fill_int_urb(touchkit->irq, touchkit->udev,
333 usb_rcvintpipe(touchkit->udev, 0x81),
334 touchkit->data, TOUCHKIT_REPORT_DATA_SIZE,
335 touchkit_irq, touchkit, endpoint->bInterval);
337 touchkit->irq->transfer_dma = touchkit->data_dma;
338 touchkit->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
340 input_register_device(touchkit->input);
342 usb_set_intfdata(intf, touchkit);
343 return 0;
345 out_free_buffers:
346 touchkit_free_buffers(udev, touchkit);
347 out_free:
348 input_free_device(input_dev);
349 kfree(touchkit);
350 return -ENOMEM;
353 static void touchkit_disconnect(struct usb_interface *intf)
355 struct touchkit_usb *touchkit = usb_get_intfdata(intf);
357 dbg("%s - called", __FUNCTION__);
359 if (!touchkit)
360 return;
362 dbg("%s - touchkit is initialized, cleaning up", __FUNCTION__);
363 usb_set_intfdata(intf, NULL);
364 usb_kill_urb(touchkit->irq);
365 input_unregister_device(touchkit->input);
366 usb_free_urb(touchkit->irq);
367 touchkit_free_buffers(interface_to_usbdev(intf), touchkit);
368 kfree(touchkit);
371 MODULE_DEVICE_TABLE(usb, touchkit_devices);
373 static struct usb_driver touchkit_driver = {
374 .name = "touchkitusb",
375 .probe = touchkit_probe,
376 .disconnect = touchkit_disconnect,
377 .id_table = touchkit_devices,
380 static int __init touchkit_init(void)
382 return usb_register(&touchkit_driver);
385 static void __exit touchkit_cleanup(void)
387 usb_deregister(&touchkit_driver);
390 module_init(touchkit_init);
391 module_exit(touchkit_cleanup);
393 MODULE_AUTHOR(DRIVER_AUTHOR);
394 MODULE_DESCRIPTION(DRIVER_DESC);
395 MODULE_LICENSE("GPL");