linux/drivers/input/mouse/synaptics_usb.c
<<
>>
Prefs
   1/*
   2 * USB Synaptics device driver
   3 *
   4 *  Copyright (c) 2002 Rob Miller (rob@inpharmatica . co . uk)
   5 *  Copyright (c) 2003 Ron Lee (ron@debian.org)
   6 *      cPad driver for kernel 2.4
   7 *
   8 *  Copyright (c) 2004 Jan Steinhoff (cpad@jan-steinhoff . de)
   9 *  Copyright (c) 2004 Ron Lee (ron@debian.org)
  10 *      rewritten for kernel 2.6
  11 *
  12 *  cPad display character device part is not included. It can be found at
  13 *  http://jan-steinhoff.de/linux/synaptics-usb.html
  14 *
  15 * Bases on:    usb_skeleton.c v2.2 by Greg Kroah-Hartman
  16 *              drivers/hid/usbhid/usbmouse.c by Vojtech Pavlik
  17 *              drivers/input/mouse/synaptics.c by Peter Osterlund
  18 *
  19 * This program is free software; you can redistribute it and/or modify it
  20 * under the terms of the GNU General Public License as published by the Free
  21 * Software Foundation; either version 2 of the License, or (at your option)
  22 * any later version.
  23 *
  24 * Trademarks are the property of their respective owners.
  25 */
  26
  27/*
  28 * There are three different types of Synaptics USB devices: Touchpads,
  29 * touchsticks (or trackpoints), and touchscreens. Touchpads are well supported
  30 * by this driver, touchstick support has not been tested much yet, and
  31 * touchscreens have not been tested at all.
  32 *
  33 * Up to three alternate settings are possible:
  34 *      setting 0: one int endpoint for relative movement (used by usbhid.ko)
  35 *      setting 1: one int endpoint for absolute finger position
  36 *      setting 2 (cPad only): one int endpoint for absolute finger position and
  37 *                 two bulk endpoints for the display (in/out)
  38 * This driver uses setting 1.
  39 */
  40
  41#include <linux/kernel.h>
  42#include <linux/init.h>
  43#include <linux/slab.h>
  44#include <linux/module.h>
  45#include <linux/moduleparam.h>
  46#include <linux/usb.h>
  47#include <linux/input.h>
  48#include <linux/usb/input.h>
  49
  50#define USB_VENDOR_ID_SYNAPTICS 0x06cb
  51#define USB_DEVICE_ID_SYNAPTICS_TP      0x0001  /* Synaptics USB TouchPad */
  52#define USB_DEVICE_ID_SYNAPTICS_INT_TP  0x0002  /* Integrated USB TouchPad */
  53#define USB_DEVICE_ID_SYNAPTICS_CPAD    0x0003  /* Synaptics cPad */
  54#define USB_DEVICE_ID_SYNAPTICS_TS      0x0006  /* Synaptics TouchScreen */
  55#define USB_DEVICE_ID_SYNAPTICS_STICK   0x0007  /* Synaptics USB Styk */
  56#define USB_DEVICE_ID_SYNAPTICS_WP      0x0008  /* Synaptics USB WheelPad */
  57#define USB_DEVICE_ID_SYNAPTICS_COMP_TP 0x0009  /* Composite USB TouchPad */
  58#define USB_DEVICE_ID_SYNAPTICS_WTP     0x0010  /* Wireless TouchPad */
  59#define USB_DEVICE_ID_SYNAPTICS_DPAD    0x0013  /* DisplayPad */
  60
  61#define SYNUSB_TOUCHPAD                 (1 << 0)
  62#define SYNUSB_STICK                    (1 << 1)
  63#define SYNUSB_TOUCHSCREEN              (1 << 2)
  64#define SYNUSB_AUXDISPLAY               (1 << 3) /* For cPad */
  65#define SYNUSB_COMBO                    (1 << 4) /* Composite device (TP + stick) */
  66#define SYNUSB_IO_ALWAYS                (1 << 5)
  67
  68#define USB_DEVICE_SYNAPTICS(prod, kind)                \
  69        USB_DEVICE(USB_VENDOR_ID_SYNAPTICS,             \
  70                   USB_DEVICE_ID_SYNAPTICS_##prod),     \
  71        .driver_info = (kind),
  72
  73#define SYNUSB_RECV_SIZE        8
  74
  75#define XMIN_NOMINAL            1472
  76#define XMAX_NOMINAL            5472
  77#define YMIN_NOMINAL            1408
  78#define YMAX_NOMINAL            4448
  79
  80struct synusb {
  81        struct usb_device *udev;
  82        struct usb_interface *intf;
  83        struct urb *urb;
  84        unsigned char *data;
  85
  86        /* input device related data structures */
  87        struct input_dev *input;
  88        char name[128];
  89        char phys[64];
  90
  91        /* characteristics of the device */
  92        unsigned long flags;
  93};
  94
  95static void synusb_report_buttons(struct synusb *synusb)
  96{
  97        struct input_dev *input_dev = synusb->input;
  98
  99        input_report_key(input_dev, BTN_LEFT, synusb->data[1] & 0x04);
 100        input_report_key(input_dev, BTN_RIGHT, synusb->data[1] & 0x01);
 101        input_report_key(input_dev, BTN_MIDDLE, synusb->data[1] & 0x02);
 102}
 103
 104static void synusb_report_stick(struct synusb *synusb)
 105{
 106        struct input_dev *input_dev = synusb->input;
 107        int x, y;
 108        unsigned int pressure;
 109
 110        pressure = synusb->data[6];
 111        x = (s16)(be16_to_cpup((__be16 *)&synusb->data[2]) << 3) >> 7;
 112        y = (s16)(be16_to_cpup((__be16 *)&synusb->data[4]) << 3) >> 7;
 113
 114        if (pressure > 0) {
 115                input_report_rel(input_dev, REL_X, x);
 116                input_report_rel(input_dev, REL_Y, -y);
 117        }
 118
 119        input_report_abs(input_dev, ABS_PRESSURE, pressure);
 120
 121        synusb_report_buttons(synusb);
 122
 123        input_sync(input_dev);
 124}
 125
 126static void synusb_report_touchpad(struct synusb *synusb)
 127{
 128        struct input_dev *input_dev = synusb->input;
 129        unsigned int num_fingers, tool_width;
 130        unsigned int x, y;
 131        unsigned int pressure, w;
 132
 133        pressure = synusb->data[6];
 134        x = be16_to_cpup((__be16 *)&synusb->data[2]);
 135        y = be16_to_cpup((__be16 *)&synusb->data[4]);
 136        w = synusb->data[0] & 0x0f;
 137
 138        if (pressure > 0) {
 139                num_fingers = 1;
 140                tool_width = 5;
 141                switch (w) {
 142                case 0 ... 1:
 143                        num_fingers = 2 + w;
 144                        break;
 145
 146                case 2:                 /* pen, pretend its a finger */
 147                        break;
 148
 149                case 4 ... 15:
 150                        tool_width = w;
 151                        break;
 152                }
 153        } else {
 154                num_fingers = 0;
 155                tool_width = 0;
 156        }
 157
 158        /*
 159         * Post events
 160         * BTN_TOUCH has to be first as mousedev relies on it when doing
 161         * absolute -> relative conversion
 162         */
 163
 164        if (pressure > 30)
 165                input_report_key(input_dev, BTN_TOUCH, 1);
 166        if (pressure < 25)
 167                input_report_key(input_dev, BTN_TOUCH, 0);
 168
 169        if (num_fingers > 0) {
 170                input_report_abs(input_dev, ABS_X, x);
 171                input_report_abs(input_dev, ABS_Y,
 172                                 YMAX_NOMINAL + YMIN_NOMINAL - y);
 173        }
 174
 175        input_report_abs(input_dev, ABS_PRESSURE, pressure);
 176        input_report_abs(input_dev, ABS_TOOL_WIDTH, tool_width);
 177
 178        input_report_key(input_dev, BTN_TOOL_FINGER, num_fingers == 1);
 179        input_report_key(input_dev, BTN_TOOL_DOUBLETAP, num_fingers == 2);
 180        input_report_key(input_dev, BTN_TOOL_TRIPLETAP, num_fingers == 3);
 181
 182        synusb_report_buttons(synusb);
 183        if (synusb->flags & SYNUSB_AUXDISPLAY)
 184                input_report_key(input_dev, BTN_MIDDLE, synusb->data[1] & 0x08);
 185
 186        input_sync(input_dev);
 187}
 188
 189static void synusb_irq(struct urb *urb)
 190{
 191        struct synusb *synusb = urb->context;
 192        int error;
 193
 194        /* Check our status in case we need to bail out early. */
 195        switch (urb->status) {
 196        case 0:
 197                usb_mark_last_busy(synusb->udev);
 198                break;
 199
 200        /* Device went away so don't keep trying to read from it. */
 201        case -ECONNRESET:
 202        case -ENOENT:
 203        case -ESHUTDOWN:
 204                return;
 205
 206        default:
 207                goto resubmit;
 208                break;
 209        }
 210
 211        if (synusb->flags & SYNUSB_STICK)
 212                synusb_report_stick(synusb);
 213        else
 214                synusb_report_touchpad(synusb);
 215
 216resubmit:
 217        error = usb_submit_urb(urb, GFP_ATOMIC);
 218        if (error && error != -EPERM)
 219                dev_err(&synusb->intf->dev,
 220                        "%s - usb_submit_urb failed with result: %d",
 221                        __func__, error);
 222}
 223
 224static struct usb_endpoint_descriptor *
 225synusb_get_in_endpoint(struct usb_host_interface *iface)
 226{
 227
 228        struct usb_endpoint_descriptor *endpoint;
 229        int i;
 230
 231        for (i = 0; i < iface->desc.bNumEndpoints; ++i) {
 232                endpoint = &iface->endpoint[i].desc;
 233
 234                if (usb_endpoint_is_int_in(endpoint)) {
 235                        /* we found our interrupt in endpoint */
 236                        return endpoint;
 237                }
 238        }
 239
 240        return NULL;
 241}
 242
 243static int synusb_open(struct input_dev *dev)
 244{
 245        struct synusb *synusb = input_get_drvdata(dev);
 246        int retval;
 247
 248        retval = usb_autopm_get_interface(synusb->intf);
 249        if (retval) {
 250                dev_err(&synusb->intf->dev,
 251                        "%s - usb_autopm_get_interface failed, error: %d\n",
 252                        __func__, retval);
 253                return retval;
 254        }
 255
 256        retval = usb_submit_urb(synusb->urb, GFP_KERNEL);
 257        if (retval) {
 258                dev_err(&synusb->intf->dev,
 259                        "%s - usb_submit_urb failed, error: %d\n",
 260                        __func__, retval);
 261                retval = -EIO;
 262                goto out;
 263        }
 264
 265        synusb->intf->needs_remote_wakeup = 1;
 266
 267out:
 268        usb_autopm_put_interface(synusb->intf);
 269        return retval;
 270}
 271
 272static void synusb_close(struct input_dev *dev)
 273{
 274        struct synusb *synusb = input_get_drvdata(dev);
 275        int autopm_error;
 276
 277        autopm_error = usb_autopm_get_interface(synusb->intf);
 278
 279        usb_kill_urb(synusb->urb);
 280        synusb->intf->needs_remote_wakeup = 0;
 281
 282        if (!autopm_error)
 283                usb_autopm_put_interface(synusb->intf);
 284}
 285
 286static int synusb_probe(struct usb_interface *intf,
 287                        const struct usb_device_id *id)
 288{
 289        struct usb_device *udev = interface_to_usbdev(intf);
 290        struct usb_endpoint_descriptor *ep;
 291        struct synusb *synusb;
 292        struct input_dev *input_dev;
 293        unsigned int intf_num = intf->cur_altsetting->desc.bInterfaceNumber;
 294        unsigned int altsetting = min(intf->num_altsetting, 1U);
 295        int error;
 296
 297        error = usb_set_interface(udev, intf_num, altsetting);
 298        if (error) {
 299                dev_err(&udev->dev,
 300                        "Can not set alternate setting to %i, error: %i",
 301                        altsetting, error);
 302                return error;
 303        }
 304
 305        ep = synusb_get_in_endpoint(intf->cur_altsetting);
 306        if (!ep)
 307                return -ENODEV;
 308
 309        synusb = kzalloc(sizeof(*synusb), GFP_KERNEL);
 310        input_dev = input_allocate_device();
 311        if (!synusb || !input_dev) {
 312                error = -ENOMEM;
 313                goto err_free_mem;
 314        }
 315
 316        synusb->udev = udev;
 317        synusb->intf = intf;
 318        synusb->input = input_dev;
 319
 320        synusb->flags = id->driver_info;
 321        if (synusb->flags & SYNUSB_COMBO) {
 322                /*
 323                 * This is a combo device, we need to set proper
 324                 * capability, depending on the interface.
 325                 */
 326                synusb->flags |= intf_num == 1 ?
 327                                        SYNUSB_STICK : SYNUSB_TOUCHPAD;
 328        }
 329
 330        synusb->urb = usb_alloc_urb(0, GFP_KERNEL);
 331        if (!synusb->urb) {
 332                error = -ENOMEM;
 333                goto err_free_mem;
 334        }
 335
 336        synusb->data = usb_alloc_coherent(udev, SYNUSB_RECV_SIZE, GFP_KERNEL,
 337                                          &synusb->urb->transfer_dma);
 338        if (!synusb->data) {
 339                error = -ENOMEM;
 340                goto err_free_urb;
 341        }
 342
 343        usb_fill_int_urb(synusb->urb, udev,
 344                         usb_rcvintpipe(udev, ep->bEndpointAddress),
 345                         synusb->data, SYNUSB_RECV_SIZE,
 346                         synusb_irq, synusb,
 347                         ep->bInterval);
 348        synusb->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
 349
 350        if (udev->manufacturer)
 351                strlcpy(synusb->name, udev->manufacturer,
 352                        sizeof(synusb->name));
 353
 354        if (udev->product) {
 355                if (udev->manufacturer)
 356                        strlcat(synusb->name, " ", sizeof(synusb->name));
 357                strlcat(synusb->name, udev->product, sizeof(synusb->name));
 358        }
 359
 360        if (!strlen(synusb->name))
 361                snprintf(synusb->name, sizeof(synusb->name),
 362                         "USB Synaptics Device %04x:%04x",
 363                         le16_to_cpu(udev->descriptor.idVendor),
 364                         le16_to_cpu(udev->descriptor.idProduct));
 365
 366        if (synusb->flags & SYNUSB_STICK)
 367                strlcat(synusb->name, " (Stick)", sizeof(synusb->name));
 368
 369        usb_make_path(udev, synusb->phys, sizeof(synusb->phys));
 370        strlcat(synusb->phys, "/input0", sizeof(synusb->phys));
 371
 372        input_dev->name = synusb->name;
 373        input_dev->phys = synusb->phys;
 374        usb_to_input_id(udev, &input_dev->id);
 375        input_dev->dev.parent = &synusb->intf->dev;
 376
 377        if (!(synusb->flags & SYNUSB_IO_ALWAYS)) {
 378                input_dev->open = synusb_open;
 379                input_dev->close = synusb_close;
 380        }
 381
 382        input_set_drvdata(input_dev, synusb);
 383
 384        __set_bit(EV_ABS, input_dev->evbit);
 385        __set_bit(EV_KEY, input_dev->evbit);
 386
 387        if (synusb->flags & SYNUSB_STICK) {
 388                __set_bit(EV_REL, input_dev->evbit);
 389                __set_bit(REL_X, input_dev->relbit);
 390                __set_bit(REL_Y, input_dev->relbit);
 391                __set_bit(INPUT_PROP_POINTING_STICK, input_dev->propbit);
 392                input_set_abs_params(input_dev, ABS_PRESSURE, 0, 127, 0, 0);
 393        } else {
 394                input_set_abs_params(input_dev, ABS_X,
 395                                     XMIN_NOMINAL, XMAX_NOMINAL, 0, 0);
 396                input_set_abs_params(input_dev, ABS_Y,
 397                                     YMIN_NOMINAL, YMAX_NOMINAL, 0, 0);
 398                input_set_abs_params(input_dev, ABS_PRESSURE, 0, 255, 0, 0);
 399                input_set_abs_params(input_dev, ABS_TOOL_WIDTH, 0, 15, 0, 0);
 400                __set_bit(BTN_TOUCH, input_dev->keybit);
 401                __set_bit(BTN_TOOL_FINGER, input_dev->keybit);
 402                __set_bit(BTN_TOOL_DOUBLETAP, input_dev->keybit);
 403                __set_bit(BTN_TOOL_TRIPLETAP, input_dev->keybit);
 404        }
 405
 406        if (synusb->flags & SYNUSB_TOUCHSCREEN)
 407                __set_bit(INPUT_PROP_DIRECT, input_dev->propbit);
 408        else
 409                __set_bit(INPUT_PROP_POINTER, input_dev->propbit);
 410
 411        __set_bit(BTN_LEFT, input_dev->keybit);
 412        __set_bit(BTN_RIGHT, input_dev->keybit);
 413        __set_bit(BTN_MIDDLE, input_dev->keybit);
 414
 415        usb_set_intfdata(intf, synusb);
 416
 417        if (synusb->flags & SYNUSB_IO_ALWAYS) {
 418                error = synusb_open(input_dev);
 419                if (error)
 420                        goto err_free_dma;
 421        }
 422
 423        error = input_register_device(input_dev);
 424        if (error) {
 425                dev_err(&udev->dev,
 426                        "Failed to register input device, error %d\n",
 427                        error);
 428                goto err_stop_io;
 429        }
 430
 431        return 0;
 432
 433err_stop_io:
 434        if (synusb->flags & SYNUSB_IO_ALWAYS)
 435                synusb_close(synusb->input);
 436err_free_dma:
 437        usb_free_coherent(udev, SYNUSB_RECV_SIZE, synusb->data,
 438                          synusb->urb->transfer_dma);
 439err_free_urb:
 440        usb_free_urb(synusb->urb);
 441err_free_mem:
 442        input_free_device(input_dev);
 443        kfree(synusb);
 444        usb_set_intfdata(intf, NULL);
 445
 446        return error;
 447}
 448
 449static void synusb_disconnect(struct usb_interface *intf)
 450{
 451        struct synusb *synusb = usb_get_intfdata(intf);
 452        struct usb_device *udev = interface_to_usbdev(intf);
 453
 454        if (synusb->flags & SYNUSB_IO_ALWAYS)
 455                synusb_close(synusb->input);
 456
 457        input_unregister_device(synusb->input);
 458
 459        usb_free_coherent(udev, SYNUSB_RECV_SIZE, synusb->data,
 460                          synusb->urb->transfer_dma);
 461        usb_free_urb(synusb->urb);
 462        kfree(synusb);
 463
 464        usb_set_intfdata(intf, NULL);
 465}
 466
 467static int synusb_suspend(struct usb_interface *intf, pm_message_t message)
 468{
 469        struct synusb *synusb = usb_get_intfdata(intf);
 470        struct input_dev *input_dev = synusb->input;
 471
 472        mutex_lock(&input_dev->mutex);
 473        usb_kill_urb(synusb->urb);
 474        mutex_unlock(&input_dev->mutex);
 475
 476        return 0;
 477}
 478
 479static int synusb_resume(struct usb_interface *intf)
 480{
 481        struct synusb *synusb = usb_get_intfdata(intf);
 482        struct input_dev *input_dev = synusb->input;
 483        int retval = 0;
 484
 485        mutex_lock(&input_dev->mutex);
 486
 487        if ((input_dev->users || (synusb->flags & SYNUSB_IO_ALWAYS)) &&
 488            usb_submit_urb(synusb->urb, GFP_NOIO) < 0) {
 489                retval = -EIO;
 490        }
 491
 492        mutex_unlock(&input_dev->mutex);
 493
 494        return retval;
 495}
 496
 497static int synusb_pre_reset(struct usb_interface *intf)
 498{
 499        struct synusb *synusb = usb_get_intfdata(intf);
 500        struct input_dev *input_dev = synusb->input;
 501
 502        mutex_lock(&input_dev->mutex);
 503        usb_kill_urb(synusb->urb);
 504
 505        return 0;
 506}
 507
 508static int synusb_post_reset(struct usb_interface *intf)
 509{
 510        struct synusb *synusb = usb_get_intfdata(intf);
 511        struct input_dev *input_dev = synusb->input;
 512        int retval = 0;
 513
 514        if ((input_dev->users || (synusb->flags & SYNUSB_IO_ALWAYS)) &&
 515            usb_submit_urb(synusb->urb, GFP_NOIO) < 0) {
 516                retval = -EIO;
 517        }
 518
 519        mutex_unlock(&input_dev->mutex);
 520
 521        return retval;
 522}
 523
 524static int synusb_reset_resume(struct usb_interface *intf)
 525{
 526        return synusb_resume(intf);
 527}
 528
 529static struct usb_device_id synusb_idtable[] = {
 530        { USB_DEVICE_SYNAPTICS(TP, SYNUSB_TOUCHPAD) },
 531        { USB_DEVICE_SYNAPTICS(INT_TP, SYNUSB_TOUCHPAD) },
 532        { USB_DEVICE_SYNAPTICS(CPAD,
 533                SYNUSB_TOUCHPAD | SYNUSB_AUXDISPLAY | SYNUSB_IO_ALWAYS) },
 534        { USB_DEVICE_SYNAPTICS(TS, SYNUSB_TOUCHSCREEN) },
 535        { USB_DEVICE_SYNAPTICS(STICK, SYNUSB_STICK) },
 536        { USB_DEVICE_SYNAPTICS(WP, SYNUSB_TOUCHPAD) },
 537        { USB_DEVICE_SYNAPTICS(COMP_TP, SYNUSB_COMBO) },
 538        { USB_DEVICE_SYNAPTICS(WTP, SYNUSB_TOUCHPAD) },
 539        { USB_DEVICE_SYNAPTICS(DPAD, SYNUSB_TOUCHPAD) },
 540        { }
 541};
 542MODULE_DEVICE_TABLE(usb, synusb_idtable);
 543
 544static struct usb_driver synusb_driver = {
 545        .name           = "synaptics_usb",
 546        .probe          = synusb_probe,
 547        .disconnect     = synusb_disconnect,
 548        .id_table       = synusb_idtable,
 549        .suspend        = synusb_suspend,
 550        .resume         = synusb_resume,
 551        .pre_reset      = synusb_pre_reset,
 552        .post_reset     = synusb_post_reset,
 553        .reset_resume   = synusb_reset_resume,
 554        .supports_autosuspend = 1,
 555};
 556
 557module_usb_driver(synusb_driver);
 558
 559MODULE_AUTHOR("Rob Miller <rob@inpharmatica.co.uk>, "
 560              "Ron Lee <ron@debian.org>, "
 561              "Jan Steinhoff <cpad@jan-steinhoff.de>");
 562MODULE_DESCRIPTION("Synaptics USB device driver");
 563MODULE_LICENSE("GPL");
 564