1 /******************************************************************************
 
   3  * Driver for USB Touchscreens, supporting those devices:
 
   9  * Copyright (C) 2004-2006 by Daniel Ritz <daniel.ritz@gmx.ch>
 
  10  * Copyright (C) by Todd E. Johnson (mtouchusb.c)
 
  12  * This program is free software; you can redistribute it and/or
 
  13  * modify it under the terms of the GNU General Public License as
 
  14  * published by the Free Software Foundation; either version 2 of the
 
  15  * License, or (at your option) any later version.
 
  17  * This program is distributed in the hope that it will be useful, but
 
  18  * WITHOUT ANY WARRANTY; without even the implied warranty of
 
  19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
  20  * General Public License for more details.
 
  22  * You should have received a copy of the GNU General Public License
 
  23  * along with this program; if not, write to the Free Software
 
  24  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
  26  * Driver is based on touchkitusb.c
 
  27  * - ITM parts are from itmtouch.c
 
  28  * - 3M parts are from mtouchusb.c
 
  29  * - PanJit parts are from an unmerged driver by Lanslott Gish
 
  31  *****************************************************************************/
 
  35 #include <linux/config.h>
 
  36 #include <linux/kernel.h>
 
  37 #include <linux/slab.h>
 
  38 #include <linux/input.h>
 
  39 #include <linux/module.h>
 
  40 #include <linux/init.h>
 
  41 #include <linux/usb.h>
 
  42 #include <linux/usb/input.h>
 
  45 #define DRIVER_VERSION          "v0.3"
 
  46 #define DRIVER_AUTHOR           "Daniel Ritz <daniel.ritz@gmx.ch>"
 
  47 #define DRIVER_DESC             "USB Touchscreen Driver"
 
  50 module_param(swap_xy, bool, 0644);
 
  51 MODULE_PARM_DESC(swap_xy, "If set X and Y axes are swapped.");
 
  53 /* device specifc data/functions */
 
  55 struct usbtouch_device_info {
 
  58         int min_press, max_press;
 
  62         void (*process_pkt) (struct usbtouch_usb *usbtouch, struct pt_regs *regs, unsigned char *pkt, int len);
 
  63         int  (*read_data)   (unsigned char *pkt, int *x, int *y, int *touch, int *press);
 
  64         int  (*init)        (struct usbtouch_usb *usbtouch);
 
  67 #define USBTOUCH_FLG_BUFFER     0x01
 
  70 /* a usbtouch device */
 
  74         unsigned char *buffer;
 
  77         struct usb_device *udev;
 
  78         struct input_dev *input;
 
  79         struct usbtouch_device_info *type;
 
  84 static void usbtouch_process_pkt(struct usbtouch_usb *usbtouch,
 
  85                                  struct pt_regs *regs, unsigned char *pkt, int len);
 
  96 static struct usb_device_id usbtouch_devices[] = {
 
  97 #ifdef CONFIG_USB_TOUCHSCREEN_EGALAX
 
  98         {USB_DEVICE(0x3823, 0x0001), .driver_info = DEVTYPE_EGALAX},
 
  99         {USB_DEVICE(0x0123, 0x0001), .driver_info = DEVTYPE_EGALAX},
 
 100         {USB_DEVICE(0x0eef, 0x0001), .driver_info = DEVTYPE_EGALAX},
 
 101         {USB_DEVICE(0x0eef, 0x0002), .driver_info = DEVTYPE_EGALAX},
 
 104 #ifdef CONFIG_USB_TOUCHSCREEN_PANJIT
 
 105         {USB_DEVICE(0x134c, 0x0001), .driver_info = DEVTYPE_PANJIT},
 
 106         {USB_DEVICE(0x134c, 0x0002), .driver_info = DEVTYPE_PANJIT},
 
 107         {USB_DEVICE(0x134c, 0x0003), .driver_info = DEVTYPE_PANJIT},
 
 108         {USB_DEVICE(0x134c, 0x0004), .driver_info = DEVTYPE_PANJIT},
 
 111 #ifdef CONFIG_USB_TOUCHSCREEN_3M
 
 112         {USB_DEVICE(0x0596, 0x0001), .driver_info = DEVTYPE_3M},
 
 115 #ifdef CONFIG_USB_TOUCHSCREEN_ITM
 
 116         {USB_DEVICE(0x0403, 0xf9e9), .driver_info = DEVTYPE_ITM},
 
 123 /*****************************************************************************
 
 127 #ifdef CONFIG_USB_TOUCHSCREEN_EGALAX
 
 129 #define EGALAX_PKT_TYPE_MASK            0xFE
 
 130 #define EGALAX_PKT_TYPE_REPT            0x80
 
 131 #define EGALAX_PKT_TYPE_DIAG            0x0A
 
 133 static int egalax_read_data(unsigned char *pkt, int *x, int *y, int *touch, int *press)
 
 135         if ((pkt[0] & EGALAX_PKT_TYPE_MASK) != EGALAX_PKT_TYPE_REPT)
 
 138         *x = ((pkt[3] & 0x0F) << 7) | (pkt[4] & 0x7F);
 
 139         *y = ((pkt[1] & 0x0F) << 7) | (pkt[2] & 0x7F);
 
 140         *touch = pkt[0] & 0x01;
 
 146 static int egalax_get_pkt_len(unsigned char *buf)
 
 148         switch (buf[0] & EGALAX_PKT_TYPE_MASK) {
 
 149         case EGALAX_PKT_TYPE_REPT:
 
 152         case EGALAX_PKT_TYPE_DIAG:
 
 159 static void egalax_process(struct usbtouch_usb *usbtouch, struct pt_regs *regs,
 
 160                            unsigned char *pkt, int len)
 
 162         unsigned char *buffer;
 
 163         int pkt_len, buf_len, pos;
 
 165         /* if the buffer contains data, append */
 
 166         if (unlikely(usbtouch->buf_len)) {
 
 169                 /* if only 1 byte in buffer, add another one to get length */
 
 170                 if (usbtouch->buf_len == 1)
 
 171                         usbtouch->buffer[1] = pkt[0];
 
 173                 pkt_len = egalax_get_pkt_len(usbtouch->buffer);
 
 175                 /* unknown packet: drop everything */
 
 179                 /* append, process */
 
 180                 tmp = pkt_len - usbtouch->buf_len;
 
 181                 memcpy(usbtouch->buffer + usbtouch->buf_len, pkt, tmp);
 
 182                 usbtouch_process_pkt(usbtouch, regs, usbtouch->buffer, pkt_len);
 
 191         /* only one byte left in buffer */
 
 192         if (unlikely(buf_len == 1)) {
 
 193                 usbtouch->buffer[0] = buffer[0];
 
 194                 usbtouch->buf_len = 1;
 
 198         /* loop over the buffer */
 
 200         while (pos < buf_len) {
 
 202                 pkt_len = egalax_get_pkt_len(buffer + pos);
 
 204                 /* unknown packet: drop everything */
 
 205                 if (unlikely(!pkt_len))
 
 208                 /* full packet: process */
 
 209                 if (likely(pkt_len <= buf_len)) {
 
 210                         usbtouch_process_pkt(usbtouch, regs, buffer + pos, pkt_len);
 
 212                         /* incomplete packet: save in buffer */
 
 213                         memcpy(usbtouch->buffer, buffer + pos, buf_len - pos);
 
 214                         usbtouch->buf_len = buf_len - pos;
 
 222 /*****************************************************************************
 
 225 #ifdef CONFIG_USB_TOUCHSCREEN_PANJIT
 
 226 static int panjit_read_data(unsigned char *pkt, int *x, int *y, int *touch, int *press)
 
 228         *x = ((pkt[2] & 0x0F) << 8) | pkt[1];
 
 229         *y = ((pkt[4] & 0x0F) << 8) | pkt[3];
 
 230         *touch = pkt[0] & 0x01;
 
 237 /*****************************************************************************
 
 240 #ifdef CONFIG_USB_TOUCHSCREEN_3M
 
 242 #define MTOUCHUSB_ASYNC_REPORT          1
 
 243 #define MTOUCHUSB_RESET                 7
 
 244 #define MTOUCHUSB_REQ_CTRLLR_ID         10
 
 246 static int mtouch_read_data(unsigned char *pkt, int *x, int *y, int *touch, int *press)
 
 248         *x = (pkt[8] << 8) | pkt[7];
 
 249         *y = (pkt[10] << 8) | pkt[9];
 
 250         *touch = (pkt[2] & 0x40) ? 1 : 0;
 
 255 static int mtouch_init(struct usbtouch_usb *usbtouch)
 
 259         ret = usb_control_msg(usbtouch->udev, usb_rcvctrlpipe(usbtouch->udev, 0),
 
 261                               USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
 
 262                               1, 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
 
 263         dbg("%s - usb_control_msg - MTOUCHUSB_RESET - bytes|err: %d",
 
 268         ret = usb_control_msg(usbtouch->udev, usb_rcvctrlpipe(usbtouch->udev, 0),
 
 269                               MTOUCHUSB_ASYNC_REPORT,
 
 270                               USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
 
 271                               1, 1, NULL, 0, USB_CTRL_SET_TIMEOUT);
 
 272         dbg("%s - usb_control_msg - MTOUCHUSB_ASYNC_REPORT - bytes|err: %d",
 
 282 /*****************************************************************************
 
 285 #ifdef CONFIG_USB_TOUCHSCREEN_ITM
 
 286 static int itm_read_data(unsigned char *pkt, int *x, int *y, int *touch, int *press)
 
 288         *x = ((pkt[0] & 0x1F) << 7) | (pkt[3] & 0x7F);
 
 289         *x = ((pkt[1] & 0x1F) << 7) | (pkt[4] & 0x7F);
 
 290         *press = ((pkt[2] & 0x1F) << 7) | (pkt[5] & 0x7F);
 
 291         *touch = ~pkt[7] & 0x20;
 
 298 /*****************************************************************************
 
 299  * the different device descriptors
 
 301 static struct usbtouch_device_info usbtouch_dev_info[] = {
 
 302 #ifdef CONFIG_USB_TOUCHSCREEN_EGALAX
 
 309                 .flags          = USBTOUCH_FLG_BUFFER,
 
 310                 .process_pkt    = egalax_process,
 
 311                 .read_data      = egalax_read_data,
 
 315 #ifdef CONFIG_USB_TOUCHSCREEN_PANJIT
 
 322                 .read_data      = panjit_read_data,
 
 326 #ifdef CONFIG_USB_TOUCHSCREEN_3M
 
 333                 .read_data      = mtouch_read_data,
 
 338 #ifdef CONFIG_USB_TOUCHSCREEN_ITM
 
 346                 .read_data      = itm_read_data,
 
 352 /*****************************************************************************
 
 355 static void usbtouch_process_pkt(struct usbtouch_usb *usbtouch,
 
 356                                  struct pt_regs *regs, unsigned char *pkt, int len)
 
 358         int x, y, touch, press;
 
 359         struct usbtouch_device_info *type = usbtouch->type;
 
 361         if (!type->read_data(pkt, &x, &y, &touch, &press))
 
 364         input_regs(usbtouch->input, regs);
 
 365         input_report_key(usbtouch->input, BTN_TOUCH, touch);
 
 368                 input_report_abs(usbtouch->input, ABS_X, y);
 
 369                 input_report_abs(usbtouch->input, ABS_Y, x);
 
 371                 input_report_abs(usbtouch->input, ABS_X, x);
 
 372                 input_report_abs(usbtouch->input, ABS_Y, y);
 
 375                 input_report_abs(usbtouch->input, ABS_PRESSURE, press);
 
 376         input_sync(usbtouch->input);
 
 380 static void usbtouch_irq(struct urb *urb, struct pt_regs *regs)
 
 382         struct usbtouch_usb *usbtouch = urb->context;
 
 385         switch (urb->status) {
 
 390                 /* this urb is timing out */
 
 391                 dbg("%s - urb timed out - was the device unplugged?",
 
 397                 /* this urb is terminated, clean up */
 
 398                 dbg("%s - urb shutting down with status: %d",
 
 399                     __FUNCTION__, urb->status);
 
 402                 dbg("%s - nonzero urb status received: %d",
 
 403                     __FUNCTION__, urb->status);
 
 407         usbtouch->type->process_pkt(usbtouch, regs, usbtouch->data, urb->actual_length);
 
 410         retval = usb_submit_urb(urb, GFP_ATOMIC);
 
 412                 err("%s - usb_submit_urb failed with result: %d",
 
 413                     __FUNCTION__, retval);
 
 416 static int usbtouch_open(struct input_dev *input)
 
 418         struct usbtouch_usb *usbtouch = input->private;
 
 420         usbtouch->irq->dev = usbtouch->udev;
 
 422         if (usb_submit_urb(usbtouch->irq, GFP_KERNEL))
 
 428 static void usbtouch_close(struct input_dev *input)
 
 430         struct usbtouch_usb *usbtouch = input->private;
 
 432         usb_kill_urb(usbtouch->irq);
 
 436 static void usbtouch_free_buffers(struct usb_device *udev,
 
 437                                   struct usbtouch_usb *usbtouch)
 
 440                 usb_buffer_free(udev, usbtouch->type->rept_size,
 
 441                                 usbtouch->data, usbtouch->data_dma);
 
 442         kfree(usbtouch->buffer);
 
 446 static int usbtouch_probe(struct usb_interface *intf,
 
 447                           const struct usb_device_id *id)
 
 449         struct usbtouch_usb *usbtouch;
 
 450         struct input_dev *input_dev;
 
 451         struct usb_host_interface *interface;
 
 452         struct usb_endpoint_descriptor *endpoint;
 
 453         struct usb_device *udev = interface_to_usbdev(intf);
 
 454         struct usbtouch_device_info *type;
 
 457         interface = intf->cur_altsetting;
 
 458         endpoint = &interface->endpoint[0].desc;
 
 460         usbtouch = kzalloc(sizeof(struct usbtouch_usb), GFP_KERNEL);
 
 461         input_dev = input_allocate_device();
 
 462         if (!usbtouch || !input_dev)
 
 465         type = &usbtouch_dev_info[id->driver_info];
 
 466         usbtouch->type = type;
 
 467         if (!type->process_pkt)
 
 468                 type->process_pkt = usbtouch_process_pkt;
 
 470         usbtouch->data = usb_buffer_alloc(udev, type->rept_size,
 
 471                                           SLAB_KERNEL, &usbtouch->data_dma);
 
 475         if (type->flags & USBTOUCH_FLG_BUFFER) {
 
 476                 usbtouch->buffer = kmalloc(type->rept_size, GFP_KERNEL);
 
 477                 if (!usbtouch->buffer)
 
 478                         goto out_free_buffers;
 
 481         usbtouch->irq = usb_alloc_urb(0, GFP_KERNEL);
 
 482         if (!usbtouch->irq) {
 
 483                 dbg("%s - usb_alloc_urb failed: usbtouch->irq", __FUNCTION__);
 
 484                 goto out_free_buffers;
 
 487         usbtouch->udev = udev;
 
 488         usbtouch->input = input_dev;
 
 490         if (udev->manufacturer)
 
 491                 strlcpy(usbtouch->name, udev->manufacturer, sizeof(usbtouch->name));
 
 494                 if (udev->manufacturer)
 
 495                         strlcat(usbtouch->name, " ", sizeof(usbtouch->name));
 
 496                 strlcat(usbtouch->name, udev->product, sizeof(usbtouch->name));
 
 499         if (!strlen(usbtouch->name))
 
 500                 snprintf(usbtouch->name, sizeof(usbtouch->name),
 
 501                         "USB Touchscreen %04x:%04x",
 
 502                          le16_to_cpu(udev->descriptor.idVendor),
 
 503                          le16_to_cpu(udev->descriptor.idProduct));
 
 505         usb_make_path(udev, usbtouch->phys, sizeof(usbtouch->phys));
 
 506         strlcpy(usbtouch->phys, "/input0", sizeof(usbtouch->phys));
 
 508         input_dev->name = usbtouch->name;
 
 509         input_dev->phys = usbtouch->phys;
 
 510         usb_to_input_id(udev, &input_dev->id);
 
 511         input_dev->cdev.dev = &intf->dev;
 
 512         input_dev->private = usbtouch;
 
 513         input_dev->open = usbtouch_open;
 
 514         input_dev->close = usbtouch_close;
 
 516         input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
 
 517         input_dev->keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH);
 
 518         input_set_abs_params(input_dev, ABS_X, type->min_xc, type->max_xc, 0, 0);
 
 519         input_set_abs_params(input_dev, ABS_Y, type->min_yc, type->max_yc, 0, 0);
 
 521                 input_set_abs_params(input_dev, ABS_PRESSURE, type->min_press,
 
 522                                      type->max_press, 0, 0);
 
 524         usb_fill_int_urb(usbtouch->irq, usbtouch->udev,
 
 525                          usb_rcvintpipe(usbtouch->udev, 0x81),
 
 526                          usbtouch->data, type->rept_size,
 
 527                          usbtouch_irq, usbtouch, endpoint->bInterval);
 
 529         usbtouch->irq->transfer_dma = usbtouch->data_dma;
 
 530         usbtouch->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
 
 532         /* device specific init */
 
 534                 err = type->init(usbtouch);
 
 536                         dbg("%s - type->init() failed, err: %d", __FUNCTION__, err);
 
 537                         goto out_free_buffers;
 
 541         err = input_register_device(usbtouch->input);
 
 543                 dbg("%s - input_register_device failed, err: %d", __FUNCTION__, err);
 
 544                 goto out_free_buffers;
 
 547         usb_set_intfdata(intf, usbtouch);
 
 552         usbtouch_free_buffers(udev, usbtouch);
 
 554         input_free_device(input_dev);
 
 559 static void usbtouch_disconnect(struct usb_interface *intf)
 
 561         struct usbtouch_usb *usbtouch = usb_get_intfdata(intf);
 
 563         dbg("%s - called", __FUNCTION__);
 
 568         dbg("%s - usbtouch is initialized, cleaning up", __FUNCTION__);
 
 569         usb_set_intfdata(intf, NULL);
 
 570         usb_kill_urb(usbtouch->irq);
 
 571         input_unregister_device(usbtouch->input);
 
 572         usb_free_urb(usbtouch->irq);
 
 573         usbtouch_free_buffers(interface_to_usbdev(intf), usbtouch);
 
 577 MODULE_DEVICE_TABLE(usb, usbtouch_devices);
 
 579 static struct usb_driver usbtouch_driver = {
 
 580         .name           = "usbtouchscreen",
 
 581         .probe          = usbtouch_probe,
 
 582         .disconnect     = usbtouch_disconnect,
 
 583         .id_table       = usbtouch_devices,
 
 586 static int __init usbtouch_init(void)
 
 588         return usb_register(&usbtouch_driver);
 
 591 static void __exit usbtouch_cleanup(void)
 
 593         usb_deregister(&usbtouch_driver);
 
 596 module_init(usbtouch_init);
 
 597 module_exit(usbtouch_cleanup);
 
 599 MODULE_AUTHOR(DRIVER_AUTHOR);
 
 600 MODULE_DESCRIPTION(DRIVER_DESC);
 
 601 MODULE_LICENSE("GPL");
 
 603 MODULE_ALIAS("touchkitusb");
 
 604 MODULE_ALIAS("itmtouch");
 
 605 MODULE_ALIAS("mtouchusb");