Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Apple USB Touchpad (for post-February 2005 PowerBooks and MacBooks) driver
0004  *
0005  * Copyright (C) 2001-2004 Greg Kroah-Hartman (greg@kroah.com)
0006  * Copyright (C) 2005-2008 Johannes Berg (johannes@sipsolutions.net)
0007  * Copyright (C) 2005-2008 Stelian Pop (stelian@popies.net)
0008  * Copyright (C) 2005      Frank Arnold (frank@scirocco-5v-turbo.de)
0009  * Copyright (C) 2005      Peter Osterlund (petero2@telia.com)
0010  * Copyright (C) 2005      Michael Hanselmann (linux-kernel@hansmi.ch)
0011  * Copyright (C) 2006      Nicolas Boichat (nicolas@boichat.ch)
0012  * Copyright (C) 2007-2008 Sven Anders (anders@anduras.de)
0013  *
0014  * Thanks to Alex Harper <basilisk@foobox.net> for his inputs.
0015  */
0016 
0017 #include <linux/kernel.h>
0018 #include <linux/errno.h>
0019 #include <linux/slab.h>
0020 #include <linux/module.h>
0021 #include <linux/usb/input.h>
0022 
0023 /*
0024  * Note: We try to keep the touchpad aspect ratio while still doing only
0025  * simple arithmetics:
0026  *  0 <= x <= (xsensors - 1) * xfact
0027  *  0 <= y <= (ysensors - 1) * yfact
0028  */
0029 struct atp_info {
0030     int xsensors;               /* number of X sensors */
0031     int xsensors_17;            /* 17" models have more sensors */
0032     int ysensors;               /* number of Y sensors */
0033     int xfact;              /* X multiplication factor */
0034     int yfact;              /* Y multiplication factor */
0035     int datalen;                /* size of USB transfers */
0036     void (*callback)(struct urb *);     /* callback function */
0037     int fuzz;               /* fuzz touchpad generates */
0038 };
0039 
0040 static void atp_complete_geyser_1_2(struct urb *urb);
0041 static void atp_complete_geyser_3_4(struct urb *urb);
0042 
0043 static const struct atp_info fountain_info = {
0044     .xsensors   = 16,
0045     .xsensors_17    = 26,
0046     .ysensors   = 16,
0047     .xfact      = 64,
0048     .yfact      = 43,
0049     .datalen    = 81,
0050     .callback   = atp_complete_geyser_1_2,
0051     .fuzz       = 16,
0052 };
0053 
0054 static const struct atp_info geyser1_info = {
0055     .xsensors   = 16,
0056     .xsensors_17    = 26,
0057     .ysensors   = 16,
0058     .xfact      = 64,
0059     .yfact      = 43,
0060     .datalen    = 81,
0061     .callback   = atp_complete_geyser_1_2,
0062     .fuzz       = 16,
0063 };
0064 
0065 static const struct atp_info geyser2_info = {
0066     .xsensors   = 15,
0067     .xsensors_17    = 20,
0068     .ysensors   = 9,
0069     .xfact      = 64,
0070     .yfact      = 43,
0071     .datalen    = 64,
0072     .callback   = atp_complete_geyser_1_2,
0073     .fuzz       = 0,
0074 };
0075 
0076 static const struct atp_info geyser3_info = {
0077     .xsensors   = 20,
0078     .ysensors   = 10,
0079     .xfact      = 64,
0080     .yfact      = 64,
0081     .datalen    = 64,
0082     .callback   = atp_complete_geyser_3_4,
0083     .fuzz       = 0,
0084 };
0085 
0086 static const struct atp_info geyser4_info = {
0087     .xsensors   = 20,
0088     .ysensors   = 10,
0089     .xfact      = 64,
0090     .yfact      = 64,
0091     .datalen    = 64,
0092     .callback   = atp_complete_geyser_3_4,
0093     .fuzz       = 0,
0094 };
0095 
0096 #define ATP_DEVICE(prod, info)                  \
0097 {                               \
0098     .match_flags = USB_DEVICE_ID_MATCH_DEVICE |     \
0099                USB_DEVICE_ID_MATCH_INT_CLASS |      \
0100                USB_DEVICE_ID_MATCH_INT_PROTOCOL,    \
0101     .idVendor = 0x05ac, /* Apple */             \
0102     .idProduct = (prod),                    \
0103     .bInterfaceClass = 0x03,                \
0104     .bInterfaceProtocol = 0x02,             \
0105     .driver_info = (unsigned long) &info,           \
0106 }
0107 
0108 /*
0109  * Table of devices (Product IDs) that work with this driver.
0110  * (The names come from Info.plist in AppleUSBTrackpad.kext,
0111  *  According to Info.plist Geyser IV is the same as Geyser III.)
0112  */
0113 
0114 static const struct usb_device_id atp_table[] = {
0115     /* PowerBooks Feb 2005, iBooks G4 */
0116     ATP_DEVICE(0x020e, fountain_info),  /* FOUNTAIN ANSI */
0117     ATP_DEVICE(0x020f, fountain_info),  /* FOUNTAIN ISO */
0118     ATP_DEVICE(0x030a, fountain_info),  /* FOUNTAIN TP ONLY */
0119     ATP_DEVICE(0x030b, geyser1_info),   /* GEYSER 1 TP ONLY */
0120 
0121     /* PowerBooks Oct 2005 */
0122     ATP_DEVICE(0x0214, geyser2_info),   /* GEYSER 2 ANSI */
0123     ATP_DEVICE(0x0215, geyser2_info),   /* GEYSER 2 ISO */
0124     ATP_DEVICE(0x0216, geyser2_info),   /* GEYSER 2 JIS */
0125 
0126     /* Core Duo MacBook & MacBook Pro */
0127     ATP_DEVICE(0x0217, geyser3_info),   /* GEYSER 3 ANSI */
0128     ATP_DEVICE(0x0218, geyser3_info),   /* GEYSER 3 ISO */
0129     ATP_DEVICE(0x0219, geyser3_info),   /* GEYSER 3 JIS */
0130 
0131     /* Core2 Duo MacBook & MacBook Pro */
0132     ATP_DEVICE(0x021a, geyser4_info),   /* GEYSER 4 ANSI */
0133     ATP_DEVICE(0x021b, geyser4_info),   /* GEYSER 4 ISO */
0134     ATP_DEVICE(0x021c, geyser4_info),   /* GEYSER 4 JIS */
0135 
0136     /* Core2 Duo MacBook3,1 */
0137     ATP_DEVICE(0x0229, geyser4_info),   /* GEYSER 4 HF ANSI */
0138     ATP_DEVICE(0x022a, geyser4_info),   /* GEYSER 4 HF ISO */
0139     ATP_DEVICE(0x022b, geyser4_info),   /* GEYSER 4 HF JIS */
0140 
0141     /* Terminating entry */
0142     { }
0143 };
0144 MODULE_DEVICE_TABLE(usb, atp_table);
0145 
0146 /* maximum number of sensors */
0147 #define ATP_XSENSORS    26
0148 #define ATP_YSENSORS    16
0149 
0150 /*
0151  * The largest possible bank of sensors with additional buffer of 4 extra values
0152  * on either side, for an array of smoothed sensor values.
0153  */
0154 #define ATP_SMOOTHSIZE  34
0155 
0156 /* maximum pressure this driver will report */
0157 #define ATP_PRESSURE    300
0158 
0159 /*
0160  * Threshold for the touchpad sensors. Any change less than ATP_THRESHOLD is
0161  * ignored.
0162  */
0163 #define ATP_THRESHOLD   5
0164 
0165 /*
0166  * How far we'll bitshift our sensor values before averaging them. Mitigates
0167  * rounding errors.
0168  */
0169 #define ATP_SCALE   12
0170 
0171 /* Geyser initialization constants */
0172 #define ATP_GEYSER_MODE_READ_REQUEST_ID     1
0173 #define ATP_GEYSER_MODE_WRITE_REQUEST_ID    9
0174 #define ATP_GEYSER_MODE_REQUEST_VALUE       0x300
0175 #define ATP_GEYSER_MODE_REQUEST_INDEX       0
0176 #define ATP_GEYSER_MODE_VENDOR_VALUE        0x04
0177 
0178 /**
0179  * enum atp_status_bits - status bit meanings
0180  *
0181  * These constants represent the meaning of the status bits.
0182  * (only Geyser 3/4)
0183  *
0184  * @ATP_STATUS_BUTTON: The button was pressed
0185  * @ATP_STATUS_BASE_UPDATE: Update of the base values (untouched pad)
0186  * @ATP_STATUS_FROM_RESET: Reset previously performed
0187  */
0188 enum atp_status_bits {
0189     ATP_STATUS_BUTTON   = BIT(0),
0190     ATP_STATUS_BASE_UPDATE  = BIT(2),
0191     ATP_STATUS_FROM_RESET   = BIT(4),
0192 };
0193 
0194 /* Structure to hold all of our device specific stuff */
0195 struct atp {
0196     char            phys[64];
0197     struct usb_device   *udev;      /* usb device */
0198     struct usb_interface    *intf;      /* usb interface */
0199     struct urb      *urb;       /* usb request block */
0200     u8          *data;      /* transferred data */
0201     struct input_dev    *input;     /* input dev */
0202     const struct atp_info   *info;      /* touchpad model */
0203     bool            open;
0204     bool            valid;      /* are the samples valid? */
0205     bool            size_detect_done;
0206     bool            overflow_warned;
0207     int         fingers_old;    /* last reported finger count */
0208     int         x_old;      /* last reported x/y, */
0209     int         y_old;      /* used for smoothing */
0210     signed char     xy_cur[ATP_XSENSORS + ATP_YSENSORS];
0211     signed char     xy_old[ATP_XSENSORS + ATP_YSENSORS];
0212     int         xy_acc[ATP_XSENSORS + ATP_YSENSORS];
0213     int         smooth[ATP_SMOOTHSIZE];
0214     int         smooth_tmp[ATP_SMOOTHSIZE];
0215     int         idlecount;  /* number of empty packets */
0216     struct work_struct  work;
0217 };
0218 
0219 #define dbg_dump(msg, tab) \
0220     if (debug > 1) {                        \
0221         int __i;                        \
0222         printk(KERN_DEBUG "appletouch: %s", msg);       \
0223         for (__i = 0; __i < ATP_XSENSORS + ATP_YSENSORS; __i++) \
0224             printk(" %02x", tab[__i]);          \
0225         printk("\n");                       \
0226     }
0227 
0228 #define dprintk(format, a...)                       \
0229     do {                                \
0230         if (debug)                      \
0231             printk(KERN_DEBUG format, ##a);         \
0232     } while (0)
0233 
0234 MODULE_AUTHOR("Johannes Berg");
0235 MODULE_AUTHOR("Stelian Pop");
0236 MODULE_AUTHOR("Frank Arnold");
0237 MODULE_AUTHOR("Michael Hanselmann");
0238 MODULE_AUTHOR("Sven Anders");
0239 MODULE_DESCRIPTION("Apple PowerBook and MacBook USB touchpad driver");
0240 MODULE_LICENSE("GPL");
0241 
0242 /*
0243  * Make the threshold a module parameter
0244  */
0245 static int threshold = ATP_THRESHOLD;
0246 module_param(threshold, int, 0644);
0247 MODULE_PARM_DESC(threshold, "Discard any change in data from a sensor"
0248                 " (the trackpad has many of these sensors)"
0249                 " less than this value.");
0250 
0251 static int debug;
0252 module_param(debug, int, 0644);
0253 MODULE_PARM_DESC(debug, "Activate debugging output");
0254 
0255 /*
0256  * By default newer Geyser devices send standard USB HID mouse
0257  * packets (Report ID 2). This code changes device mode, so it
0258  * sends raw sensor reports (Report ID 5).
0259  */
0260 static int atp_geyser_init(struct atp *dev)
0261 {
0262     struct usb_device *udev = dev->udev;
0263     char *data;
0264     int size;
0265     int i;
0266     int ret;
0267 
0268     data = kmalloc(8, GFP_KERNEL);
0269     if (!data) {
0270         dev_err(&dev->intf->dev, "Out of memory\n");
0271         return -ENOMEM;
0272     }
0273 
0274     size = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
0275             ATP_GEYSER_MODE_READ_REQUEST_ID,
0276             USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
0277             ATP_GEYSER_MODE_REQUEST_VALUE,
0278             ATP_GEYSER_MODE_REQUEST_INDEX, data, 8, 5000);
0279 
0280     if (size != 8) {
0281         dprintk("atp_geyser_init: read error\n");
0282         for (i = 0; i < 8; i++)
0283             dprintk("appletouch[%d]: %d\n", i, data[i]);
0284 
0285         dev_err(&dev->intf->dev, "Failed to read mode from device.\n");
0286         ret = -EIO;
0287         goto out_free;
0288     }
0289 
0290     /* Apply the mode switch */
0291     data[0] = ATP_GEYSER_MODE_VENDOR_VALUE;
0292 
0293     size = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
0294             ATP_GEYSER_MODE_WRITE_REQUEST_ID,
0295             USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
0296             ATP_GEYSER_MODE_REQUEST_VALUE,
0297             ATP_GEYSER_MODE_REQUEST_INDEX, data, 8, 5000);
0298 
0299     if (size != 8) {
0300         dprintk("atp_geyser_init: write error\n");
0301         for (i = 0; i < 8; i++)
0302             dprintk("appletouch[%d]: %d\n", i, data[i]);
0303 
0304         dev_err(&dev->intf->dev, "Failed to request geyser raw mode\n");
0305         ret = -EIO;
0306         goto out_free;
0307     }
0308     ret = 0;
0309 out_free:
0310     kfree(data);
0311     return ret;
0312 }
0313 
0314 /*
0315  * Reinitialise the device. This usually stops stream of empty packets
0316  * coming from it.
0317  */
0318 static void atp_reinit(struct work_struct *work)
0319 {
0320     struct atp *dev = container_of(work, struct atp, work);
0321     int retval;
0322 
0323     dprintk("appletouch: putting appletouch to sleep (reinit)\n");
0324     atp_geyser_init(dev);
0325 
0326     retval = usb_submit_urb(dev->urb, GFP_ATOMIC);
0327     if (retval)
0328         dev_err(&dev->intf->dev,
0329             "atp_reinit: usb_submit_urb failed with error %d\n",
0330             retval);
0331 }
0332 
0333 static int atp_calculate_abs(struct atp *dev, int offset, int nb_sensors,
0334                  int fact, int *z, int *fingers)
0335 {
0336     int i, pass;
0337 
0338     /*
0339      * Use offset to point xy_sensors at the first value in dev->xy_acc
0340      * for whichever dimension we're looking at this particular go-round.
0341      */
0342     int *xy_sensors = dev->xy_acc + offset;
0343 
0344     /* values to calculate mean */
0345     int pcum = 0, psum = 0;
0346     int is_increasing = 0;
0347 
0348     *fingers = 0;
0349 
0350     for (i = 0; i < nb_sensors; i++) {
0351         if (xy_sensors[i] < threshold) {
0352             if (is_increasing)
0353                 is_increasing = 0;
0354 
0355         /*
0356          * Makes the finger detection more versatile.  For example,
0357          * two fingers with no gap will be detected.  Also, my
0358          * tests show it less likely to have intermittent loss
0359          * of multiple finger readings while moving around (scrolling).
0360          *
0361          * Changes the multiple finger detection to counting humps on
0362          * sensors (transitions from nonincreasing to increasing)
0363          * instead of counting transitions from low sensors (no
0364          * finger reading) to high sensors (finger above
0365          * sensor)
0366          *
0367          * - Jason Parekh <jasonparekh@gmail.com>
0368          */
0369 
0370         } else if (i < 1 ||
0371             (!is_increasing && xy_sensors[i - 1] < xy_sensors[i])) {
0372             (*fingers)++;
0373             is_increasing = 1;
0374         } else if (i > 0 && (xy_sensors[i - 1] - xy_sensors[i] > threshold)) {
0375             is_increasing = 0;
0376         }
0377     }
0378 
0379     if (*fingers < 1)     /* No need to continue if no fingers are found. */
0380         return 0;
0381 
0382     /*
0383      * Use a smoothed version of sensor data for movement calculations, to
0384      * combat noise without needing to rely so heavily on a threshold.
0385      * This improves tracking.
0386      *
0387      * The smoothed array is bigger than the original so that the smoothing
0388      * doesn't result in edge values being truncated.
0389      */
0390 
0391     memset(dev->smooth, 0, 4 * sizeof(dev->smooth[0]));
0392     /* Pull base values, scaled up to help avoid truncation errors. */
0393     for (i = 0; i < nb_sensors; i++)
0394         dev->smooth[i + 4] = xy_sensors[i] << ATP_SCALE;
0395     memset(&dev->smooth[nb_sensors + 4], 0, 4 * sizeof(dev->smooth[0]));
0396 
0397     for (pass = 0; pass < 4; pass++) {
0398         /* Handle edge. */
0399         dev->smooth_tmp[0] = (dev->smooth[0] + dev->smooth[1]) / 2;
0400 
0401         /* Average values with neighbors. */
0402         for (i = 1; i < nb_sensors + 7; i++)
0403             dev->smooth_tmp[i] = (dev->smooth[i - 1] +
0404                           dev->smooth[i] * 2 +
0405                           dev->smooth[i + 1]) / 4;
0406 
0407         /* Handle other edge. */
0408         dev->smooth_tmp[i] = (dev->smooth[i - 1] + dev->smooth[i]) / 2;
0409 
0410         memcpy(dev->smooth, dev->smooth_tmp, sizeof(dev->smooth));
0411     }
0412 
0413     for (i = 0; i < nb_sensors + 8; i++) {
0414         /*
0415          * Skip values if they're small enough to be truncated to 0
0416          * by scale. Mostly noise.
0417          */
0418         if ((dev->smooth[i] >> ATP_SCALE) > 0) {
0419             pcum += dev->smooth[i] * i;
0420             psum += dev->smooth[i];
0421         }
0422     }
0423 
0424     if (psum > 0) {
0425         *z = psum >> ATP_SCALE;        /* Scale down pressure output. */
0426         return pcum * fact / psum;
0427     }
0428 
0429     return 0;
0430 }
0431 
0432 static inline void atp_report_fingers(struct input_dev *input, int fingers)
0433 {
0434     input_report_key(input, BTN_TOOL_FINGER, fingers == 1);
0435     input_report_key(input, BTN_TOOL_DOUBLETAP, fingers == 2);
0436     input_report_key(input, BTN_TOOL_TRIPLETAP, fingers > 2);
0437 }
0438 
0439 /* Check URB status and for correct length of data package */
0440 
0441 #define ATP_URB_STATUS_SUCCESS      0
0442 #define ATP_URB_STATUS_ERROR        1
0443 #define ATP_URB_STATUS_ERROR_FATAL  2
0444 
0445 static int atp_status_check(struct urb *urb)
0446 {
0447     struct atp *dev = urb->context;
0448     struct usb_interface *intf = dev->intf;
0449 
0450     switch (urb->status) {
0451     case 0:
0452         /* success */
0453         break;
0454     case -EOVERFLOW:
0455         if (!dev->overflow_warned) {
0456             dev_warn(&intf->dev,
0457                 "appletouch: OVERFLOW with data length %d, actual length is %d\n",
0458                 dev->info->datalen, dev->urb->actual_length);
0459             dev->overflow_warned = true;
0460         }
0461         fallthrough;
0462     case -ECONNRESET:
0463     case -ENOENT:
0464     case -ESHUTDOWN:
0465         /* This urb is terminated, clean up */
0466         dev_dbg(&intf->dev,
0467             "atp_complete: urb shutting down with status: %d\n",
0468             urb->status);
0469         return ATP_URB_STATUS_ERROR_FATAL;
0470 
0471     default:
0472         dev_dbg(&intf->dev,
0473             "atp_complete: nonzero urb status received: %d\n",
0474             urb->status);
0475         return ATP_URB_STATUS_ERROR;
0476     }
0477 
0478     /* drop incomplete datasets */
0479     if (dev->urb->actual_length != dev->info->datalen) {
0480         dprintk("appletouch: incomplete data package"
0481             " (first byte: %d, length: %d).\n",
0482             dev->data[0], dev->urb->actual_length);
0483         return ATP_URB_STATUS_ERROR;
0484     }
0485 
0486     return ATP_URB_STATUS_SUCCESS;
0487 }
0488 
0489 static void atp_detect_size(struct atp *dev)
0490 {
0491     int i;
0492 
0493     /* 17" Powerbooks have extra X sensors */
0494     for (i = dev->info->xsensors; i < ATP_XSENSORS; i++) {
0495         if (dev->xy_cur[i]) {
0496 
0497             dev_info(&dev->intf->dev,
0498                 "appletouch: 17\" model detected.\n");
0499 
0500             input_set_abs_params(dev->input, ABS_X, 0,
0501                          (dev->info->xsensors_17 - 1) *
0502                             dev->info->xfact - 1,
0503                          dev->info->fuzz, 0);
0504             break;
0505         }
0506     }
0507 }
0508 
0509 /*
0510  * USB interrupt callback functions
0511  */
0512 
0513 /* Interrupt function for older touchpads: FOUNTAIN/GEYSER1/GEYSER2 */
0514 
0515 static void atp_complete_geyser_1_2(struct urb *urb)
0516 {
0517     int x, y, x_z, y_z, x_f, y_f;
0518     int retval, i, j;
0519     int key, fingers;
0520     struct atp *dev = urb->context;
0521     int status = atp_status_check(urb);
0522 
0523     if (status == ATP_URB_STATUS_ERROR_FATAL)
0524         return;
0525     else if (status == ATP_URB_STATUS_ERROR)
0526         goto exit;
0527 
0528     /* reorder the sensors values */
0529     if (dev->info == &geyser2_info) {
0530         memset(dev->xy_cur, 0, sizeof(dev->xy_cur));
0531 
0532         /*
0533          * The values are laid out like this:
0534          * Y1, Y2, -, Y3, Y4, -, ..., X1, X2, -, X3, X4, -, ...
0535          * '-' is an unused value.
0536          */
0537 
0538         /* read X values */
0539         for (i = 0, j = 19; i < 20; i += 2, j += 3) {
0540             dev->xy_cur[i] = dev->data[j];
0541             dev->xy_cur[i + 1] = dev->data[j + 1];
0542         }
0543 
0544         /* read Y values */
0545         for (i = 0, j = 1; i < 9; i += 2, j += 3) {
0546             dev->xy_cur[ATP_XSENSORS + i] = dev->data[j];
0547             dev->xy_cur[ATP_XSENSORS + i + 1] = dev->data[j + 1];
0548         }
0549     } else {
0550         for (i = 0; i < 8; i++) {
0551             /* X values */
0552             dev->xy_cur[i +  0] = dev->data[5 * i +  2];
0553             dev->xy_cur[i +  8] = dev->data[5 * i +  4];
0554             dev->xy_cur[i + 16] = dev->data[5 * i + 42];
0555             if (i < 2)
0556                 dev->xy_cur[i + 24] = dev->data[5 * i + 44];
0557 
0558             /* Y values */
0559             dev->xy_cur[ATP_XSENSORS + i] = dev->data[5 * i +  1];
0560             dev->xy_cur[ATP_XSENSORS + i + 8] = dev->data[5 * i + 3];
0561         }
0562     }
0563 
0564     dbg_dump("sample", dev->xy_cur);
0565 
0566     if (!dev->valid) {
0567         /* first sample */
0568         dev->valid = true;
0569         dev->x_old = dev->y_old = -1;
0570 
0571         /* Store first sample */
0572         memcpy(dev->xy_old, dev->xy_cur, sizeof(dev->xy_old));
0573 
0574         /* Perform size detection, if not done already */
0575         if (unlikely(!dev->size_detect_done)) {
0576             atp_detect_size(dev);
0577             dev->size_detect_done = true;
0578             goto exit;
0579         }
0580     }
0581 
0582     for (i = 0; i < ATP_XSENSORS + ATP_YSENSORS; i++) {
0583         /* accumulate the change */
0584         signed char change = dev->xy_old[i] - dev->xy_cur[i];
0585         dev->xy_acc[i] -= change;
0586 
0587         /* prevent down drifting */
0588         if (dev->xy_acc[i] < 0)
0589             dev->xy_acc[i] = 0;
0590     }
0591 
0592     memcpy(dev->xy_old, dev->xy_cur, sizeof(dev->xy_old));
0593 
0594     dbg_dump("accumulator", dev->xy_acc);
0595 
0596     x = atp_calculate_abs(dev, 0, ATP_XSENSORS,
0597                   dev->info->xfact, &x_z, &x_f);
0598     y = atp_calculate_abs(dev, ATP_XSENSORS, ATP_YSENSORS,
0599                   dev->info->yfact, &y_z, &y_f);
0600     key = dev->data[dev->info->datalen - 1] & ATP_STATUS_BUTTON;
0601 
0602     fingers = max(x_f, y_f);
0603 
0604     if (x && y && fingers == dev->fingers_old) {
0605         if (dev->x_old != -1) {
0606             x = (dev->x_old * 7 + x) >> 3;
0607             y = (dev->y_old * 7 + y) >> 3;
0608             dev->x_old = x;
0609             dev->y_old = y;
0610 
0611             if (debug > 1)
0612                 printk(KERN_DEBUG "appletouch: "
0613                     "X: %3d Y: %3d Xz: %3d Yz: %3d\n",
0614                     x, y, x_z, y_z);
0615 
0616             input_report_key(dev->input, BTN_TOUCH, 1);
0617             input_report_abs(dev->input, ABS_X, x);
0618             input_report_abs(dev->input, ABS_Y, y);
0619             input_report_abs(dev->input, ABS_PRESSURE,
0620                      min(ATP_PRESSURE, x_z + y_z));
0621             atp_report_fingers(dev->input, fingers);
0622         }
0623         dev->x_old = x;
0624         dev->y_old = y;
0625 
0626     } else if (!x && !y) {
0627 
0628         dev->x_old = dev->y_old = -1;
0629         dev->fingers_old = 0;
0630         input_report_key(dev->input, BTN_TOUCH, 0);
0631         input_report_abs(dev->input, ABS_PRESSURE, 0);
0632         atp_report_fingers(dev->input, 0);
0633 
0634         /* reset the accumulator on release */
0635         memset(dev->xy_acc, 0, sizeof(dev->xy_acc));
0636     }
0637 
0638     if (fingers != dev->fingers_old)
0639         dev->x_old = dev->y_old = -1;
0640     dev->fingers_old = fingers;
0641 
0642     input_report_key(dev->input, BTN_LEFT, key);
0643     input_sync(dev->input);
0644 
0645  exit:
0646     retval = usb_submit_urb(dev->urb, GFP_ATOMIC);
0647     if (retval)
0648         dev_err(&dev->intf->dev,
0649             "atp_complete: usb_submit_urb failed with result %d\n",
0650             retval);
0651 }
0652 
0653 /* Interrupt function for older touchpads: GEYSER3/GEYSER4 */
0654 
0655 static void atp_complete_geyser_3_4(struct urb *urb)
0656 {
0657     int x, y, x_z, y_z, x_f, y_f;
0658     int retval, i, j;
0659     int key, fingers;
0660     struct atp *dev = urb->context;
0661     int status = atp_status_check(urb);
0662 
0663     if (status == ATP_URB_STATUS_ERROR_FATAL)
0664         return;
0665     else if (status == ATP_URB_STATUS_ERROR)
0666         goto exit;
0667 
0668     /* Reorder the sensors values:
0669      *
0670      * The values are laid out like this:
0671      * -, Y1, Y2, -, Y3, Y4, -, ..., -, X1, X2, -, X3, X4, ...
0672      * '-' is an unused value.
0673      */
0674 
0675     /* read X values */
0676     for (i = 0, j = 19; i < 20; i += 2, j += 3) {
0677         dev->xy_cur[i] = dev->data[j + 1];
0678         dev->xy_cur[i + 1] = dev->data[j + 2];
0679     }
0680     /* read Y values */
0681     for (i = 0, j = 1; i < 9; i += 2, j += 3) {
0682         dev->xy_cur[ATP_XSENSORS + i] = dev->data[j + 1];
0683         dev->xy_cur[ATP_XSENSORS + i + 1] = dev->data[j + 2];
0684     }
0685 
0686     dbg_dump("sample", dev->xy_cur);
0687 
0688     /* Just update the base values (i.e. touchpad in untouched state) */
0689     if (dev->data[dev->info->datalen - 1] & ATP_STATUS_BASE_UPDATE) {
0690 
0691         dprintk("appletouch: updated base values\n");
0692 
0693         memcpy(dev->xy_old, dev->xy_cur, sizeof(dev->xy_old));
0694         goto exit;
0695     }
0696 
0697     for (i = 0; i < ATP_XSENSORS + ATP_YSENSORS; i++) {
0698         /* calculate the change */
0699         dev->xy_acc[i] = dev->xy_cur[i] - dev->xy_old[i];
0700 
0701         /* this is a round-robin value, so couple with that */
0702         if (dev->xy_acc[i] > 127)
0703             dev->xy_acc[i] -= 256;
0704 
0705         if (dev->xy_acc[i] < -127)
0706             dev->xy_acc[i] += 256;
0707 
0708         /* prevent down drifting */
0709         if (dev->xy_acc[i] < 0)
0710             dev->xy_acc[i] = 0;
0711     }
0712 
0713     dbg_dump("accumulator", dev->xy_acc);
0714 
0715     x = atp_calculate_abs(dev, 0, ATP_XSENSORS,
0716                   dev->info->xfact, &x_z, &x_f);
0717     y = atp_calculate_abs(dev, ATP_XSENSORS, ATP_YSENSORS,
0718                   dev->info->yfact, &y_z, &y_f);
0719 
0720     key = dev->data[dev->info->datalen - 1] & ATP_STATUS_BUTTON;
0721 
0722     fingers = max(x_f, y_f);
0723 
0724     if (x && y && fingers == dev->fingers_old) {
0725         if (dev->x_old != -1) {
0726             x = (dev->x_old * 7 + x) >> 3;
0727             y = (dev->y_old * 7 + y) >> 3;
0728             dev->x_old = x;
0729             dev->y_old = y;
0730 
0731             if (debug > 1)
0732                 printk(KERN_DEBUG "appletouch: X: %3d Y: %3d "
0733                        "Xz: %3d Yz: %3d\n",
0734                        x, y, x_z, y_z);
0735 
0736             input_report_key(dev->input, BTN_TOUCH, 1);
0737             input_report_abs(dev->input, ABS_X, x);
0738             input_report_abs(dev->input, ABS_Y, y);
0739             input_report_abs(dev->input, ABS_PRESSURE,
0740                      min(ATP_PRESSURE, x_z + y_z));
0741             atp_report_fingers(dev->input, fingers);
0742         }
0743         dev->x_old = x;
0744         dev->y_old = y;
0745 
0746     } else if (!x && !y) {
0747 
0748         dev->x_old = dev->y_old = -1;
0749         dev->fingers_old = 0;
0750         input_report_key(dev->input, BTN_TOUCH, 0);
0751         input_report_abs(dev->input, ABS_PRESSURE, 0);
0752         atp_report_fingers(dev->input, 0);
0753 
0754         /* reset the accumulator on release */
0755         memset(dev->xy_acc, 0, sizeof(dev->xy_acc));
0756     }
0757 
0758     if (fingers != dev->fingers_old)
0759         dev->x_old = dev->y_old = -1;
0760     dev->fingers_old = fingers;
0761 
0762     input_report_key(dev->input, BTN_LEFT, key);
0763     input_sync(dev->input);
0764 
0765     /*
0766      * Geysers 3/4 will continue to send packets continually after
0767      * the first touch unless reinitialised. Do so if it's been
0768      * idle for a while in order to avoid waking the kernel up
0769      * several hundred times a second.
0770      */
0771 
0772     /*
0773      * Button must not be pressed when entering suspend,
0774      * otherwise we will never release the button.
0775      */
0776     if (!x && !y && !key) {
0777         dev->idlecount++;
0778         if (dev->idlecount == 10) {
0779             dev->x_old = dev->y_old = -1;
0780             dev->idlecount = 0;
0781             schedule_work(&dev->work);
0782             /* Don't resubmit urb here, wait for reinit */
0783             return;
0784         }
0785     } else
0786         dev->idlecount = 0;
0787 
0788  exit:
0789     retval = usb_submit_urb(dev->urb, GFP_ATOMIC);
0790     if (retval)
0791         dev_err(&dev->intf->dev,
0792             "atp_complete: usb_submit_urb failed with result %d\n",
0793             retval);
0794 }
0795 
0796 static int atp_open(struct input_dev *input)
0797 {
0798     struct atp *dev = input_get_drvdata(input);
0799 
0800     if (usb_submit_urb(dev->urb, GFP_KERNEL))
0801         return -EIO;
0802 
0803     dev->open = true;
0804     return 0;
0805 }
0806 
0807 static void atp_close(struct input_dev *input)
0808 {
0809     struct atp *dev = input_get_drvdata(input);
0810 
0811     usb_kill_urb(dev->urb);
0812     cancel_work_sync(&dev->work);
0813     dev->open = false;
0814 }
0815 
0816 static int atp_handle_geyser(struct atp *dev)
0817 {
0818     if (dev->info != &fountain_info) {
0819         /* switch to raw sensor mode */
0820         if (atp_geyser_init(dev))
0821             return -EIO;
0822 
0823         dev_info(&dev->intf->dev, "Geyser mode initialized.\n");
0824     }
0825 
0826     return 0;
0827 }
0828 
0829 static int atp_probe(struct usb_interface *iface,
0830              const struct usb_device_id *id)
0831 {
0832     struct atp *dev;
0833     struct input_dev *input_dev;
0834     struct usb_device *udev = interface_to_usbdev(iface);
0835     struct usb_host_interface *iface_desc;
0836     struct usb_endpoint_descriptor *endpoint;
0837     int int_in_endpointAddr = 0;
0838     int i, error = -ENOMEM;
0839     const struct atp_info *info = (const struct atp_info *)id->driver_info;
0840 
0841     /* set up the endpoint information */
0842     /* use only the first interrupt-in endpoint */
0843     iface_desc = iface->cur_altsetting;
0844     for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
0845         endpoint = &iface_desc->endpoint[i].desc;
0846         if (!int_in_endpointAddr && usb_endpoint_is_int_in(endpoint)) {
0847             /* we found an interrupt in endpoint */
0848             int_in_endpointAddr = endpoint->bEndpointAddress;
0849             break;
0850         }
0851     }
0852     if (!int_in_endpointAddr) {
0853         dev_err(&iface->dev, "Could not find int-in endpoint\n");
0854         return -EIO;
0855     }
0856 
0857     /* allocate memory for our device state and initialize it */
0858     dev = kzalloc(sizeof(struct atp), GFP_KERNEL);
0859     input_dev = input_allocate_device();
0860     if (!dev || !input_dev) {
0861         dev_err(&iface->dev, "Out of memory\n");
0862         goto err_free_devs;
0863     }
0864 
0865     dev->udev = udev;
0866     dev->intf = iface;
0867     dev->input = input_dev;
0868     dev->info = info;
0869     dev->overflow_warned = false;
0870 
0871     dev->urb = usb_alloc_urb(0, GFP_KERNEL);
0872     if (!dev->urb)
0873         goto err_free_devs;
0874 
0875     dev->data = usb_alloc_coherent(dev->udev, dev->info->datalen, GFP_KERNEL,
0876                        &dev->urb->transfer_dma);
0877     if (!dev->data)
0878         goto err_free_urb;
0879 
0880     usb_fill_int_urb(dev->urb, udev,
0881              usb_rcvintpipe(udev, int_in_endpointAddr),
0882              dev->data, dev->info->datalen,
0883              dev->info->callback, dev, 1);
0884 
0885     error = atp_handle_geyser(dev);
0886     if (error)
0887         goto err_free_buffer;
0888 
0889     usb_make_path(udev, dev->phys, sizeof(dev->phys));
0890     strlcat(dev->phys, "/input0", sizeof(dev->phys));
0891 
0892     input_dev->name = "appletouch";
0893     input_dev->phys = dev->phys;
0894     usb_to_input_id(dev->udev, &input_dev->id);
0895     input_dev->dev.parent = &iface->dev;
0896 
0897     input_set_drvdata(input_dev, dev);
0898 
0899     input_dev->open = atp_open;
0900     input_dev->close = atp_close;
0901 
0902     set_bit(EV_ABS, input_dev->evbit);
0903 
0904     input_set_abs_params(input_dev, ABS_X, 0,
0905                  (dev->info->xsensors - 1) * dev->info->xfact - 1,
0906                  dev->info->fuzz, 0);
0907     input_set_abs_params(input_dev, ABS_Y, 0,
0908                  (dev->info->ysensors - 1) * dev->info->yfact - 1,
0909                  dev->info->fuzz, 0);
0910     input_set_abs_params(input_dev, ABS_PRESSURE, 0, ATP_PRESSURE, 0, 0);
0911 
0912     set_bit(EV_KEY, input_dev->evbit);
0913     set_bit(BTN_TOUCH, input_dev->keybit);
0914     set_bit(BTN_TOOL_FINGER, input_dev->keybit);
0915     set_bit(BTN_TOOL_DOUBLETAP, input_dev->keybit);
0916     set_bit(BTN_TOOL_TRIPLETAP, input_dev->keybit);
0917     set_bit(BTN_LEFT, input_dev->keybit);
0918 
0919     INIT_WORK(&dev->work, atp_reinit);
0920 
0921     error = input_register_device(dev->input);
0922     if (error)
0923         goto err_free_buffer;
0924 
0925     /* save our data pointer in this interface device */
0926     usb_set_intfdata(iface, dev);
0927 
0928     return 0;
0929 
0930  err_free_buffer:
0931     usb_free_coherent(dev->udev, dev->info->datalen,
0932               dev->data, dev->urb->transfer_dma);
0933  err_free_urb:
0934     usb_free_urb(dev->urb);
0935  err_free_devs:
0936     usb_set_intfdata(iface, NULL);
0937     kfree(dev);
0938     input_free_device(input_dev);
0939     return error;
0940 }
0941 
0942 static void atp_disconnect(struct usb_interface *iface)
0943 {
0944     struct atp *dev = usb_get_intfdata(iface);
0945 
0946     usb_set_intfdata(iface, NULL);
0947     if (dev) {
0948         usb_kill_urb(dev->urb);
0949         input_unregister_device(dev->input);
0950         usb_free_coherent(dev->udev, dev->info->datalen,
0951                   dev->data, dev->urb->transfer_dma);
0952         usb_free_urb(dev->urb);
0953         kfree(dev);
0954     }
0955     dev_info(&iface->dev, "input: appletouch disconnected\n");
0956 }
0957 
0958 static int atp_recover(struct atp *dev)
0959 {
0960     int error;
0961 
0962     error = atp_handle_geyser(dev);
0963     if (error)
0964         return error;
0965 
0966     if (dev->open && usb_submit_urb(dev->urb, GFP_KERNEL))
0967         return -EIO;
0968 
0969     return 0;
0970 }
0971 
0972 static int atp_suspend(struct usb_interface *iface, pm_message_t message)
0973 {
0974     struct atp *dev = usb_get_intfdata(iface);
0975 
0976     usb_kill_urb(dev->urb);
0977     return 0;
0978 }
0979 
0980 static int atp_resume(struct usb_interface *iface)
0981 {
0982     struct atp *dev = usb_get_intfdata(iface);
0983 
0984     if (dev->open && usb_submit_urb(dev->urb, GFP_KERNEL))
0985         return -EIO;
0986 
0987     return 0;
0988 }
0989 
0990 static int atp_reset_resume(struct usb_interface *iface)
0991 {
0992     struct atp *dev = usb_get_intfdata(iface);
0993 
0994     return atp_recover(dev);
0995 }
0996 
0997 static struct usb_driver atp_driver = {
0998     .name       = "appletouch",
0999     .probe      = atp_probe,
1000     .disconnect = atp_disconnect,
1001     .suspend    = atp_suspend,
1002     .resume     = atp_resume,
1003     .reset_resume   = atp_reset_resume,
1004     .id_table   = atp_table,
1005 };
1006 
1007 module_usb_driver(atp_driver);