Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
at v2.6.18 395 lines 10 kB view raw
1/****************************************************************************** 2 * touchkitusb.c -- Driver for eGalax TouchKit USB Touchscreens 3 * 4 * Copyright (C) 2004-2005 by Daniel Ritz <daniel.ritz@gmx.ch> 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/kernel.h> 28#include <linux/slab.h> 29#include <linux/module.h> 30#include <linux/init.h> 31#include <linux/usb/input.h> 32 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 42 43#define TOUCHKIT_DOWN 0x01 44 45#define TOUCHKIT_PKT_TYPE_MASK 0xFE 46#define TOUCHKIT_PKT_TYPE_REPT 0x80 47#define TOUCHKIT_PKT_TYPE_DIAG 0x0A 48 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" 52 53static int swap_xy; 54module_param(swap_xy, bool, 0644); 55MODULE_PARM_DESC(swap_xy, "If set X and Y axes are swapped."); 56 57struct 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]; 67}; 68 69static 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)}, 74 {} 75}; 76 77/* helpers to read the data */ 78static inline int touchkit_get_touched(char *data) 79{ 80 return (data[0] & TOUCHKIT_DOWN) ? 1 : 0; 81} 82 83static inline int touchkit_get_x(char *data) 84{ 85 return ((data[3] & 0x0F) << 7) | (data[4] & 0x7F); 86} 87 88static inline int touchkit_get_y(char *data) 89{ 90 return ((data[1] & 0x0F) << 7) | (data[2] & 0x7F); 91} 92 93 94/* processes one input packet. */ 95static void touchkit_process_pkt(struct touchkit_usb *touchkit, 96 struct pt_regs *regs, char *pkt) 97{ 98 int x, y; 99 100 /* only process report packets */ 101 if ((pkt[0] & TOUCHKIT_PKT_TYPE_MASK) != TOUCHKIT_PKT_TYPE_REPT) 102 return; 103 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); 110 } 111 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); 117} 118 119 120static int touchkit_get_pkt_len(char *buf) 121{ 122 switch (buf[0] & TOUCHKIT_PKT_TYPE_MASK) { 123 case TOUCHKIT_PKT_TYPE_REPT: 124 return 5; 125 126 case TOUCHKIT_PKT_TYPE_DIAG: 127 return buf[1] + 2; 128 } 129 130 return 0; 131} 132 133static void touchkit_process(struct touchkit_usb *touchkit, int len, 134 struct pt_regs *regs) 135{ 136 char *buffer; 137 int pkt_len, buf_len, pos; 138 139 /* if the buffer contains data, append */ 140 if (unlikely(touchkit->buf_len)) { 141 int tmp; 142 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]; 146 147 pkt_len = touchkit_get_pkt_len(touchkit->buffer); 148 149 /* unknown packet: drop everything */ 150 if (!pkt_len) 151 return; 152 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); 157 158 buffer = touchkit->data + tmp; 159 buf_len = len - tmp; 160 } else { 161 buffer = touchkit->data; 162 buf_len = len; 163 } 164 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; 170 } 171 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); 177 178 /* unknown packet: drop everything */ 179 if (unlikely(!pkt_len)) 180 return; 181 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; 189 } 190 pos += pkt_len; 191 } 192} 193 194 195static void touchkit_irq(struct urb *urb, struct pt_regs *regs) 196{ 197 struct touchkit_usb *touchkit = urb->context; 198 int retval; 199 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; 220 } 221 222 touchkit_process(touchkit, urb->actual_length, regs); 223 224exit: 225 retval = usb_submit_urb(urb, GFP_ATOMIC); 226 if (retval) 227 err("%s - usb_submit_urb failed with result: %d", 228 __FUNCTION__, retval); 229} 230 231static int touchkit_open(struct input_dev *input) 232{ 233 struct touchkit_usb *touchkit = input->private; 234 235 touchkit->irq->dev = touchkit->udev; 236 237 if (usb_submit_urb(touchkit->irq, GFP_ATOMIC)) 238 return -EIO; 239 240 return 0; 241} 242 243static void touchkit_close(struct input_dev *input) 244{ 245 struct touchkit_usb *touchkit = input->private; 246 247 usb_kill_urb(touchkit->irq); 248} 249 250static int touchkit_alloc_buffers(struct usb_device *udev, 251 struct touchkit_usb *touchkit) 252{ 253 touchkit->data = usb_buffer_alloc(udev, TOUCHKIT_REPORT_DATA_SIZE, 254 SLAB_ATOMIC, &touchkit->data_dma); 255 256 if (!touchkit->data) 257 return -1; 258 259 return 0; 260} 261 262static void touchkit_free_buffers(struct usb_device *udev, 263 struct touchkit_usb *touchkit) 264{ 265 if (touchkit->data) 266 usb_buffer_free(udev, TOUCHKIT_REPORT_DATA_SIZE, 267 touchkit->data, touchkit->data_dma); 268} 269 270static int touchkit_probe(struct usb_interface *intf, 271 const struct usb_device_id *id) 272{ 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); 278 279 interface = intf->cur_altsetting; 280 endpoint = &interface->endpoint[0].desc; 281 282 touchkit = kzalloc(sizeof(struct touchkit_usb), GFP_KERNEL); 283 input_dev = input_allocate_device(); 284 if (!touchkit || !input_dev) 285 goto out_free; 286 287 if (touchkit_alloc_buffers(udev, touchkit)) 288 goto out_free; 289 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; 294 } 295 296 touchkit->udev = udev; 297 touchkit->input = input_dev; 298 299 if (udev->manufacturer) 300 strlcpy(touchkit->name, udev->manufacturer, sizeof(touchkit->name)); 301 302 if (udev->product) { 303 if (udev->manufacturer) 304 strlcat(touchkit->name, " ", sizeof(touchkit->name)); 305 strlcat(touchkit->name, udev->product, sizeof(touchkit->name)); 306 } 307 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)); 313 314 usb_make_path(udev, touchkit->phys, sizeof(touchkit->phys)); 315 strlcpy(touchkit->phys, "/input0", sizeof(touchkit->phys)); 316 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; 324 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); 331 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); 336 337 touchkit->irq->transfer_dma = touchkit->data_dma; 338 touchkit->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; 339 340 input_register_device(touchkit->input); 341 342 usb_set_intfdata(intf, touchkit); 343 return 0; 344 345out_free_buffers: 346 touchkit_free_buffers(udev, touchkit); 347out_free: 348 input_free_device(input_dev); 349 kfree(touchkit); 350 return -ENOMEM; 351} 352 353static void touchkit_disconnect(struct usb_interface *intf) 354{ 355 struct touchkit_usb *touchkit = usb_get_intfdata(intf); 356 357 dbg("%s - called", __FUNCTION__); 358 359 if (!touchkit) 360 return; 361 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); 369} 370 371MODULE_DEVICE_TABLE(usb, touchkit_devices); 372 373static struct usb_driver touchkit_driver = { 374 .name = "touchkitusb", 375 .probe = touchkit_probe, 376 .disconnect = touchkit_disconnect, 377 .id_table = touchkit_devices, 378}; 379 380static int __init touchkit_init(void) 381{ 382 return usb_register(&touchkit_driver); 383} 384 385static void __exit touchkit_cleanup(void) 386{ 387 usb_deregister(&touchkit_driver); 388} 389 390module_init(touchkit_init); 391module_exit(touchkit_cleanup); 392 393MODULE_AUTHOR(DRIVER_AUTHOR); 394MODULE_DESCRIPTION(DRIVER_DESC); 395MODULE_LICENSE("GPL");