174ba9207SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2b5da20f8SDmitry Torokhov /*
3b5da20f8SDmitry Torokhov * Apple USB Touchpad (for post-February 2005 PowerBooks and MacBooks) driver
4b5da20f8SDmitry Torokhov *
5b5da20f8SDmitry Torokhov * Copyright (C) 2001-2004 Greg Kroah-Hartman (greg@kroah.com)
67dce869fSJohannes Berg * Copyright (C) 2005-2008 Johannes Berg (johannes@sipsolutions.net)
709779678SStelian Pop * Copyright (C) 2005-2008 Stelian Pop (stelian@popies.net)
8b5da20f8SDmitry Torokhov * Copyright (C) 2005 Frank Arnold (frank@scirocco-5v-turbo.de)
9b5da20f8SDmitry Torokhov * Copyright (C) 2005 Peter Osterlund (petero2@telia.com)
10b5da20f8SDmitry Torokhov * Copyright (C) 2005 Michael Hanselmann (linux-kernel@hansmi.ch)
11b5da20f8SDmitry Torokhov * Copyright (C) 2006 Nicolas Boichat (nicolas@boichat.ch)
127dce869fSJohannes Berg * Copyright (C) 2007-2008 Sven Anders (anders@anduras.de)
13b5da20f8SDmitry Torokhov *
14b5da20f8SDmitry Torokhov * Thanks to Alex Harper <basilisk@foobox.net> for his inputs.
15b5da20f8SDmitry Torokhov */
16b5da20f8SDmitry Torokhov
17b5da20f8SDmitry Torokhov #include <linux/kernel.h>
18b5da20f8SDmitry Torokhov #include <linux/errno.h>
19b5da20f8SDmitry Torokhov #include <linux/slab.h>
20b5da20f8SDmitry Torokhov #include <linux/module.h>
21b5da20f8SDmitry Torokhov #include <linux/usb/input.h>
22b5da20f8SDmitry Torokhov
2309779678SStelian Pop /*
2409779678SStelian Pop * Note: We try to keep the touchpad aspect ratio while still doing only
2509779678SStelian Pop * simple arithmetics:
2609779678SStelian Pop * 0 <= x <= (xsensors - 1) * xfact
2709779678SStelian Pop * 0 <= y <= (ysensors - 1) * yfact
2809779678SStelian Pop */
2909779678SStelian Pop struct atp_info {
3009779678SStelian Pop int xsensors; /* number of X sensors */
3109779678SStelian Pop int xsensors_17; /* 17" models have more sensors */
3209779678SStelian Pop int ysensors; /* number of Y sensors */
3309779678SStelian Pop int xfact; /* X multiplication factor */
3409779678SStelian Pop int yfact; /* Y multiplication factor */
3509779678SStelian Pop int datalen; /* size of USB transfers */
3609779678SStelian Pop void (*callback)(struct urb *); /* callback function */
37703e1488SClinton Sprain int fuzz; /* fuzz touchpad generates */
38e9542dffSSven Anders };
39b5da20f8SDmitry Torokhov
4009779678SStelian Pop static void atp_complete_geyser_1_2(struct urb *urb);
4109779678SStelian Pop static void atp_complete_geyser_3_4(struct urb *urb);
4209779678SStelian Pop
4309779678SStelian Pop static const struct atp_info fountain_info = {
4409779678SStelian Pop .xsensors = 16,
4509779678SStelian Pop .xsensors_17 = 26,
4609779678SStelian Pop .ysensors = 16,
4709779678SStelian Pop .xfact = 64,
4809779678SStelian Pop .yfact = 43,
4909779678SStelian Pop .datalen = 81,
5009779678SStelian Pop .callback = atp_complete_geyser_1_2,
51703e1488SClinton Sprain .fuzz = 16,
5209779678SStelian Pop };
5309779678SStelian Pop
5409779678SStelian Pop static const struct atp_info geyser1_info = {
5509779678SStelian Pop .xsensors = 16,
5609779678SStelian Pop .xsensors_17 = 26,
5709779678SStelian Pop .ysensors = 16,
5809779678SStelian Pop .xfact = 64,
5909779678SStelian Pop .yfact = 43,
6009779678SStelian Pop .datalen = 81,
6109779678SStelian Pop .callback = atp_complete_geyser_1_2,
62703e1488SClinton Sprain .fuzz = 16,
6309779678SStelian Pop };
6409779678SStelian Pop
6509779678SStelian Pop static const struct atp_info geyser2_info = {
6609779678SStelian Pop .xsensors = 15,
6709779678SStelian Pop .xsensors_17 = 20,
6809779678SStelian Pop .ysensors = 9,
6909779678SStelian Pop .xfact = 64,
7009779678SStelian Pop .yfact = 43,
7109779678SStelian Pop .datalen = 64,
7209779678SStelian Pop .callback = atp_complete_geyser_1_2,
73703e1488SClinton Sprain .fuzz = 0,
7409779678SStelian Pop };
7509779678SStelian Pop
7609779678SStelian Pop static const struct atp_info geyser3_info = {
7709779678SStelian Pop .xsensors = 20,
7809779678SStelian Pop .ysensors = 10,
7909779678SStelian Pop .xfact = 64,
8009779678SStelian Pop .yfact = 64,
8109779678SStelian Pop .datalen = 64,
8209779678SStelian Pop .callback = atp_complete_geyser_3_4,
83703e1488SClinton Sprain .fuzz = 0,
8409779678SStelian Pop };
8509779678SStelian Pop
8609779678SStelian Pop static const struct atp_info geyser4_info = {
8709779678SStelian Pop .xsensors = 20,
8809779678SStelian Pop .ysensors = 10,
8909779678SStelian Pop .xfact = 64,
9009779678SStelian Pop .yfact = 64,
9109779678SStelian Pop .datalen = 64,
9209779678SStelian Pop .callback = atp_complete_geyser_3_4,
93703e1488SClinton Sprain .fuzz = 0,
9409779678SStelian Pop };
9509779678SStelian Pop
9609779678SStelian Pop #define ATP_DEVICE(prod, info) \
97e9542dffSSven Anders { \
98b5da20f8SDmitry Torokhov .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \
99b5da20f8SDmitry Torokhov USB_DEVICE_ID_MATCH_INT_CLASS | \
100b5da20f8SDmitry Torokhov USB_DEVICE_ID_MATCH_INT_PROTOCOL, \
101e9542dffSSven Anders .idVendor = 0x05ac, /* Apple */ \
102b5da20f8SDmitry Torokhov .idProduct = (prod), \
103b5da20f8SDmitry Torokhov .bInterfaceClass = 0x03, \
104e9542dffSSven Anders .bInterfaceProtocol = 0x02, \
10509779678SStelian Pop .driver_info = (unsigned long) &info, \
106e9542dffSSven Anders }
107b5da20f8SDmitry Torokhov
108e9542dffSSven Anders /*
109e9542dffSSven Anders * Table of devices (Product IDs) that work with this driver.
110e9542dffSSven Anders * (The names come from Info.plist in AppleUSBTrackpad.kext,
111e9542dffSSven Anders * According to Info.plist Geyser IV is the same as Geyser III.)
112e9542dffSSven Anders */
113e9542dffSSven Anders
114a45232d6SArvind Yadav static const struct usb_device_id atp_table[] = {
1157dce869fSJohannes Berg /* PowerBooks Feb 2005, iBooks G4 */
11609779678SStelian Pop ATP_DEVICE(0x020e, fountain_info), /* FOUNTAIN ANSI */
11709779678SStelian Pop ATP_DEVICE(0x020f, fountain_info), /* FOUNTAIN ISO */
11809779678SStelian Pop ATP_DEVICE(0x030a, fountain_info), /* FOUNTAIN TP ONLY */
11909779678SStelian Pop ATP_DEVICE(0x030b, geyser1_info), /* GEYSER 1 TP ONLY */
120b5da20f8SDmitry Torokhov
121b5da20f8SDmitry Torokhov /* PowerBooks Oct 2005 */
12209779678SStelian Pop ATP_DEVICE(0x0214, geyser2_info), /* GEYSER 2 ANSI */
12309779678SStelian Pop ATP_DEVICE(0x0215, geyser2_info), /* GEYSER 2 ISO */
12409779678SStelian Pop ATP_DEVICE(0x0216, geyser2_info), /* GEYSER 2 JIS */
125b5da20f8SDmitry Torokhov
126b5da20f8SDmitry Torokhov /* Core Duo MacBook & MacBook Pro */
12709779678SStelian Pop ATP_DEVICE(0x0217, geyser3_info), /* GEYSER 3 ANSI */
12809779678SStelian Pop ATP_DEVICE(0x0218, geyser3_info), /* GEYSER 3 ISO */
12909779678SStelian Pop ATP_DEVICE(0x0219, geyser3_info), /* GEYSER 3 JIS */
130b5da20f8SDmitry Torokhov
131b5da20f8SDmitry Torokhov /* Core2 Duo MacBook & MacBook Pro */
13209779678SStelian Pop ATP_DEVICE(0x021a, geyser4_info), /* GEYSER 4 ANSI */
13309779678SStelian Pop ATP_DEVICE(0x021b, geyser4_info), /* GEYSER 4 ISO */
13409779678SStelian Pop ATP_DEVICE(0x021c, geyser4_info), /* GEYSER 4 JIS */
135b5da20f8SDmitry Torokhov
1367dce869fSJohannes Berg /* Core2 Duo MacBook3,1 */
13709779678SStelian Pop ATP_DEVICE(0x0229, geyser4_info), /* GEYSER 4 HF ANSI */
13809779678SStelian Pop ATP_DEVICE(0x022a, geyser4_info), /* GEYSER 4 HF ISO */
13909779678SStelian Pop ATP_DEVICE(0x022b, geyser4_info), /* GEYSER 4 HF JIS */
1400035a1dcSTobias Mueller
141b5da20f8SDmitry Torokhov /* Terminating entry */
142b5da20f8SDmitry Torokhov { }
143b5da20f8SDmitry Torokhov };
144b5da20f8SDmitry Torokhov MODULE_DEVICE_TABLE(usb, atp_table);
145b5da20f8SDmitry Torokhov
14609779678SStelian Pop /* maximum number of sensors */
147b5da20f8SDmitry Torokhov #define ATP_XSENSORS 26
148b5da20f8SDmitry Torokhov #define ATP_YSENSORS 16
149b5da20f8SDmitry Torokhov
150739204bcSClinton Sprain /*
151739204bcSClinton Sprain * The largest possible bank of sensors with additional buffer of 4 extra values
152739204bcSClinton Sprain * on either side, for an array of smoothed sensor values.
153739204bcSClinton Sprain */
154739204bcSClinton Sprain #define ATP_SMOOTHSIZE 34
155739204bcSClinton Sprain
156b5da20f8SDmitry Torokhov /* maximum pressure this driver will report */
157b5da20f8SDmitry Torokhov #define ATP_PRESSURE 300
158b5da20f8SDmitry Torokhov
159b5da20f8SDmitry Torokhov /*
160b5da20f8SDmitry Torokhov * Threshold for the touchpad sensors. Any change less than ATP_THRESHOLD is
161b5da20f8SDmitry Torokhov * ignored.
162b5da20f8SDmitry Torokhov */
163b5da20f8SDmitry Torokhov #define ATP_THRESHOLD 5
164b5da20f8SDmitry Torokhov
165739204bcSClinton Sprain /*
166739204bcSClinton Sprain * How far we'll bitshift our sensor values before averaging them. Mitigates
167739204bcSClinton Sprain * rounding errors.
168739204bcSClinton Sprain */
169739204bcSClinton Sprain #define ATP_SCALE 12
170739204bcSClinton Sprain
1712a3e480dSDmitry Torokhov /* Geyser initialization constants */
1722a3e480dSDmitry Torokhov #define ATP_GEYSER_MODE_READ_REQUEST_ID 1
1732a3e480dSDmitry Torokhov #define ATP_GEYSER_MODE_WRITE_REQUEST_ID 9
1742a3e480dSDmitry Torokhov #define ATP_GEYSER_MODE_REQUEST_VALUE 0x300
1752a3e480dSDmitry Torokhov #define ATP_GEYSER_MODE_REQUEST_INDEX 0
1762a3e480dSDmitry Torokhov #define ATP_GEYSER_MODE_VENDOR_VALUE 0x04
177b5da20f8SDmitry Torokhov
17882a196f4SSven Anders /**
17982a196f4SSven Anders * enum atp_status_bits - status bit meanings
18082a196f4SSven Anders *
18182a196f4SSven Anders * These constants represent the meaning of the status bits.
18282a196f4SSven Anders * (only Geyser 3/4)
18382a196f4SSven Anders *
18482a196f4SSven Anders * @ATP_STATUS_BUTTON: The button was pressed
18582a196f4SSven Anders * @ATP_STATUS_BASE_UPDATE: Update of the base values (untouched pad)
18682a196f4SSven Anders * @ATP_STATUS_FROM_RESET: Reset previously performed
18782a196f4SSven Anders */
18882a196f4SSven Anders enum atp_status_bits {
18982a196f4SSven Anders ATP_STATUS_BUTTON = BIT(0),
19082a196f4SSven Anders ATP_STATUS_BASE_UPDATE = BIT(2),
19182a196f4SSven Anders ATP_STATUS_FROM_RESET = BIT(4),
19282a196f4SSven Anders };
19382a196f4SSven Anders
194b5da20f8SDmitry Torokhov /* Structure to hold all of our device specific stuff */
195b5da20f8SDmitry Torokhov struct atp {
196b5da20f8SDmitry Torokhov char phys[64];
197b5da20f8SDmitry Torokhov struct usb_device *udev; /* usb device */
1982d744b09SGreg Kroah-Hartman struct usb_interface *intf; /* usb interface */
199b5da20f8SDmitry Torokhov struct urb *urb; /* usb request block */
20082a196f4SSven Anders u8 *data; /* transferred data */
201b5da20f8SDmitry Torokhov struct input_dev *input; /* input dev */
20209779678SStelian Pop const struct atp_info *info; /* touchpad model */
2037dce869fSJohannes Berg bool open;
2047dce869fSJohannes Berg bool valid; /* are the samples valid? */
2057dce869fSJohannes Berg bool size_detect_done;
2067dce869fSJohannes Berg bool overflow_warned;
2077bbdba56SClinton Sprain int fingers_old; /* last reported finger count */
208b5da20f8SDmitry Torokhov int x_old; /* last reported x/y, */
209b5da20f8SDmitry Torokhov int y_old; /* used for smoothing */
2106e49c1a4SBenjamin Herrenschmidt signed char xy_cur[ATP_XSENSORS + ATP_YSENSORS];
2116e49c1a4SBenjamin Herrenschmidt signed char xy_old[ATP_XSENSORS + ATP_YSENSORS];
212b5da20f8SDmitry Torokhov int xy_acc[ATP_XSENSORS + ATP_YSENSORS];
213739204bcSClinton Sprain int smooth[ATP_SMOOTHSIZE];
214739204bcSClinton Sprain int smooth_tmp[ATP_SMOOTHSIZE];
2155a6eb676SSoeren Sonnenburg int idlecount; /* number of empty packets */
2165a6eb676SSoeren Sonnenburg struct work_struct work;
217b5da20f8SDmitry Torokhov };
218b5da20f8SDmitry Torokhov
219b5da20f8SDmitry Torokhov #define dbg_dump(msg, tab) \
220b5da20f8SDmitry Torokhov if (debug > 1) { \
2217dce869fSJohannes Berg int __i; \
2227dce869fSJohannes Berg printk(KERN_DEBUG "appletouch: %s", msg); \
2237dce869fSJohannes Berg for (__i = 0; __i < ATP_XSENSORS + ATP_YSENSORS; __i++) \
2247dce869fSJohannes Berg printk(" %02x", tab[__i]); \
225b5da20f8SDmitry Torokhov printk("\n"); \
226b5da20f8SDmitry Torokhov }
227b5da20f8SDmitry Torokhov
228b5da20f8SDmitry Torokhov #define dprintk(format, a...) \
229b5da20f8SDmitry Torokhov do { \
2307dce869fSJohannes Berg if (debug) \
2317dce869fSJohannes Berg printk(KERN_DEBUG format, ##a); \
232b5da20f8SDmitry Torokhov } while (0)
233b5da20f8SDmitry Torokhov
2347dce869fSJohannes Berg MODULE_AUTHOR("Johannes Berg");
2357dce869fSJohannes Berg MODULE_AUTHOR("Stelian Pop");
2367dce869fSJohannes Berg MODULE_AUTHOR("Frank Arnold");
2377dce869fSJohannes Berg MODULE_AUTHOR("Michael Hanselmann");
2387dce869fSJohannes Berg MODULE_AUTHOR("Sven Anders");
2397dce869fSJohannes Berg MODULE_DESCRIPTION("Apple PowerBook and MacBook USB touchpad driver");
240b5da20f8SDmitry Torokhov MODULE_LICENSE("GPL");
241b5da20f8SDmitry Torokhov
242b5da20f8SDmitry Torokhov /*
243b5da20f8SDmitry Torokhov * Make the threshold a module parameter
244b5da20f8SDmitry Torokhov */
245b5da20f8SDmitry Torokhov static int threshold = ATP_THRESHOLD;
246b5da20f8SDmitry Torokhov module_param(threshold, int, 0644);
2477dce869fSJohannes Berg MODULE_PARM_DESC(threshold, "Discard any change in data from a sensor"
2487dce869fSJohannes Berg " (the trackpad has many of these sensors)"
2497dce869fSJohannes Berg " less than this value.");
250b5da20f8SDmitry Torokhov
2517dce869fSJohannes Berg static int debug;
252b5da20f8SDmitry Torokhov module_param(debug, int, 0644);
253b5da20f8SDmitry Torokhov MODULE_PARM_DESC(debug, "Activate debugging output");
254b5da20f8SDmitry Torokhov
2555a6eb676SSoeren Sonnenburg /*
2562a3e480dSDmitry Torokhov * By default newer Geyser devices send standard USB HID mouse
2575a6eb676SSoeren Sonnenburg * packets (Report ID 2). This code changes device mode, so it
2585a6eb676SSoeren Sonnenburg * sends raw sensor reports (Report ID 5).
2595a6eb676SSoeren Sonnenburg */
atp_geyser_init(struct atp * dev)26080f8594fSGreg Kroah-Hartman static int atp_geyser_init(struct atp *dev)
2615a6eb676SSoeren Sonnenburg {
26280f8594fSGreg Kroah-Hartman struct usb_device *udev = dev->udev;
2630385c5eeSBob Copeland char *data;
2645a6eb676SSoeren Sonnenburg int size;
2657dce869fSJohannes Berg int i;
2660385c5eeSBob Copeland int ret;
2670385c5eeSBob Copeland
2680385c5eeSBob Copeland data = kmalloc(8, GFP_KERNEL);
2690385c5eeSBob Copeland if (!data) {
2702d744b09SGreg Kroah-Hartman dev_err(&dev->intf->dev, "Out of memory\n");
2710385c5eeSBob Copeland return -ENOMEM;
2720385c5eeSBob Copeland }
2735a6eb676SSoeren Sonnenburg
2745a6eb676SSoeren Sonnenburg size = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
2752a3e480dSDmitry Torokhov ATP_GEYSER_MODE_READ_REQUEST_ID,
2765a6eb676SSoeren Sonnenburg USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
2772a3e480dSDmitry Torokhov ATP_GEYSER_MODE_REQUEST_VALUE,
2780385c5eeSBob Copeland ATP_GEYSER_MODE_REQUEST_INDEX, data, 8, 5000);
2795a6eb676SSoeren Sonnenburg
2805a6eb676SSoeren Sonnenburg if (size != 8) {
2817dce869fSJohannes Berg dprintk("atp_geyser_init: read error\n");
2827dce869fSJohannes Berg for (i = 0; i < 8; i++)
2837dce869fSJohannes Berg dprintk("appletouch[%d]: %d\n", i, data[i]);
2847dce869fSJohannes Berg
2852d744b09SGreg Kroah-Hartman dev_err(&dev->intf->dev, "Failed to read mode from device.\n");
2860385c5eeSBob Copeland ret = -EIO;
2870385c5eeSBob Copeland goto out_free;
2885a6eb676SSoeren Sonnenburg }
2895a6eb676SSoeren Sonnenburg
2905a6eb676SSoeren Sonnenburg /* Apply the mode switch */
2912a3e480dSDmitry Torokhov data[0] = ATP_GEYSER_MODE_VENDOR_VALUE;
2925a6eb676SSoeren Sonnenburg
2935a6eb676SSoeren Sonnenburg size = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2942a3e480dSDmitry Torokhov ATP_GEYSER_MODE_WRITE_REQUEST_ID,
2955a6eb676SSoeren Sonnenburg USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
2962a3e480dSDmitry Torokhov ATP_GEYSER_MODE_REQUEST_VALUE,
2970385c5eeSBob Copeland ATP_GEYSER_MODE_REQUEST_INDEX, data, 8, 5000);
2985a6eb676SSoeren Sonnenburg
2995a6eb676SSoeren Sonnenburg if (size != 8) {
3007dce869fSJohannes Berg dprintk("atp_geyser_init: write error\n");
3017dce869fSJohannes Berg for (i = 0; i < 8; i++)
3027dce869fSJohannes Berg dprintk("appletouch[%d]: %d\n", i, data[i]);
3037dce869fSJohannes Berg
3042d744b09SGreg Kroah-Hartman dev_err(&dev->intf->dev, "Failed to request geyser raw mode\n");
3050385c5eeSBob Copeland ret = -EIO;
3060385c5eeSBob Copeland goto out_free;
3075a6eb676SSoeren Sonnenburg }
3080385c5eeSBob Copeland ret = 0;
3090385c5eeSBob Copeland out_free:
3100385c5eeSBob Copeland kfree(data);
3110385c5eeSBob Copeland return ret;
3125a6eb676SSoeren Sonnenburg }
3135a6eb676SSoeren Sonnenburg
3142a3e480dSDmitry Torokhov /*
3152a3e480dSDmitry Torokhov * Reinitialise the device. This usually stops stream of empty packets
3162a3e480dSDmitry Torokhov * coming from it.
3172a3e480dSDmitry Torokhov */
atp_reinit(struct work_struct * work)3185a6eb676SSoeren Sonnenburg static void atp_reinit(struct work_struct *work)
3195a6eb676SSoeren Sonnenburg {
3205a6eb676SSoeren Sonnenburg struct atp *dev = container_of(work, struct atp, work);
3212a3e480dSDmitry Torokhov int retval;
3225a6eb676SSoeren Sonnenburg
3237dce869fSJohannes Berg dprintk("appletouch: putting appletouch to sleep (reinit)\n");
32480f8594fSGreg Kroah-Hartman atp_geyser_init(dev);
3252a3e480dSDmitry Torokhov
3262a3e480dSDmitry Torokhov retval = usb_submit_urb(dev->urb, GFP_ATOMIC);
3277dce869fSJohannes Berg if (retval)
3282d744b09SGreg Kroah-Hartman dev_err(&dev->intf->dev,
3299c113dc6SGreg Kroah-Hartman "atp_reinit: usb_submit_urb failed with error %d\n",
3307dce869fSJohannes Berg retval);
3315a6eb676SSoeren Sonnenburg }
3325a6eb676SSoeren Sonnenburg
atp_calculate_abs(struct atp * dev,int offset,int nb_sensors,int fact,int * z,int * fingers)333739204bcSClinton Sprain static int atp_calculate_abs(struct atp *dev, int offset, int nb_sensors,
334739204bcSClinton Sprain int fact, int *z, int *fingers)
335b5da20f8SDmitry Torokhov {
336739204bcSClinton Sprain int i, pass;
337739204bcSClinton Sprain
338739204bcSClinton Sprain /*
339739204bcSClinton Sprain * Use offset to point xy_sensors at the first value in dev->xy_acc
340739204bcSClinton Sprain * for whichever dimension we're looking at this particular go-round.
341739204bcSClinton Sprain */
342739204bcSClinton Sprain int *xy_sensors = dev->xy_acc + offset;
343739204bcSClinton Sprain
344b5da20f8SDmitry Torokhov /* values to calculate mean */
345b5da20f8SDmitry Torokhov int pcum = 0, psum = 0;
346b5da20f8SDmitry Torokhov int is_increasing = 0;
347b5da20f8SDmitry Torokhov
348b5da20f8SDmitry Torokhov *fingers = 0;
349b5da20f8SDmitry Torokhov
350b5da20f8SDmitry Torokhov for (i = 0; i < nb_sensors; i++) {
351b5da20f8SDmitry Torokhov if (xy_sensors[i] < threshold) {
352b5da20f8SDmitry Torokhov if (is_increasing)
353b5da20f8SDmitry Torokhov is_increasing = 0;
354b5da20f8SDmitry Torokhov
355b5da20f8SDmitry Torokhov /*
356b5da20f8SDmitry Torokhov * Makes the finger detection more versatile. For example,
357b5da20f8SDmitry Torokhov * two fingers with no gap will be detected. Also, my
358b5da20f8SDmitry Torokhov * tests show it less likely to have intermittent loss
359b5da20f8SDmitry Torokhov * of multiple finger readings while moving around (scrolling).
360b5da20f8SDmitry Torokhov *
361b5da20f8SDmitry Torokhov * Changes the multiple finger detection to counting humps on
362b5da20f8SDmitry Torokhov * sensors (transitions from nonincreasing to increasing)
363b5da20f8SDmitry Torokhov * instead of counting transitions from low sensors (no
364b5da20f8SDmitry Torokhov * finger reading) to high sensors (finger above
365b5da20f8SDmitry Torokhov * sensor)
366b5da20f8SDmitry Torokhov *
367b5da20f8SDmitry Torokhov * - Jason Parekh <jasonparekh@gmail.com>
368b5da20f8SDmitry Torokhov */
369739204bcSClinton Sprain
370739204bcSClinton Sprain } else if (i < 1 ||
3717dce869fSJohannes Berg (!is_increasing && xy_sensors[i - 1] < xy_sensors[i])) {
372b5da20f8SDmitry Torokhov (*fingers)++;
373b5da20f8SDmitry Torokhov is_increasing = 1;
37405e882f8SJeremy Huddleston } else if (i > 0 && (xy_sensors[i - 1] - xy_sensors[i] > threshold)) {
375b5da20f8SDmitry Torokhov is_increasing = 0;
376b5da20f8SDmitry Torokhov }
377739204bcSClinton Sprain }
378739204bcSClinton Sprain
379739204bcSClinton Sprain if (*fingers < 1) /* No need to continue if no fingers are found. */
380739204bcSClinton Sprain return 0;
381b5da20f8SDmitry Torokhov
382b5da20f8SDmitry Torokhov /*
383739204bcSClinton Sprain * Use a smoothed version of sensor data for movement calculations, to
384739204bcSClinton Sprain * combat noise without needing to rely so heavily on a threshold.
385739204bcSClinton Sprain * This improves tracking.
386739204bcSClinton Sprain *
387739204bcSClinton Sprain * The smoothed array is bigger than the original so that the smoothing
388739204bcSClinton Sprain * doesn't result in edge values being truncated.
389b5da20f8SDmitry Torokhov */
390739204bcSClinton Sprain
391739204bcSClinton Sprain memset(dev->smooth, 0, 4 * sizeof(dev->smooth[0]));
392739204bcSClinton Sprain /* Pull base values, scaled up to help avoid truncation errors. */
393739204bcSClinton Sprain for (i = 0; i < nb_sensors; i++)
394739204bcSClinton Sprain dev->smooth[i + 4] = xy_sensors[i] << ATP_SCALE;
395739204bcSClinton Sprain memset(&dev->smooth[nb_sensors + 4], 0, 4 * sizeof(dev->smooth[0]));
396739204bcSClinton Sprain
397739204bcSClinton Sprain for (pass = 0; pass < 4; pass++) {
398739204bcSClinton Sprain /* Handle edge. */
399739204bcSClinton Sprain dev->smooth_tmp[0] = (dev->smooth[0] + dev->smooth[1]) / 2;
400739204bcSClinton Sprain
401739204bcSClinton Sprain /* Average values with neighbors. */
402739204bcSClinton Sprain for (i = 1; i < nb_sensors + 7; i++)
403739204bcSClinton Sprain dev->smooth_tmp[i] = (dev->smooth[i - 1] +
404739204bcSClinton Sprain dev->smooth[i] * 2 +
405739204bcSClinton Sprain dev->smooth[i + 1]) / 4;
406739204bcSClinton Sprain
407739204bcSClinton Sprain /* Handle other edge. */
408739204bcSClinton Sprain dev->smooth_tmp[i] = (dev->smooth[i - 1] + dev->smooth[i]) / 2;
409739204bcSClinton Sprain
410739204bcSClinton Sprain memcpy(dev->smooth, dev->smooth_tmp, sizeof(dev->smooth));
411739204bcSClinton Sprain }
412739204bcSClinton Sprain
413739204bcSClinton Sprain for (i = 0; i < nb_sensors + 8; i++) {
414739204bcSClinton Sprain /*
415739204bcSClinton Sprain * Skip values if they're small enough to be truncated to 0
416739204bcSClinton Sprain * by scale. Mostly noise.
417739204bcSClinton Sprain */
418739204bcSClinton Sprain if ((dev->smooth[i] >> ATP_SCALE) > 0) {
419739204bcSClinton Sprain pcum += dev->smooth[i] * i;
420739204bcSClinton Sprain psum += dev->smooth[i];
421739204bcSClinton Sprain }
422b5da20f8SDmitry Torokhov }
423b5da20f8SDmitry Torokhov
424b5da20f8SDmitry Torokhov if (psum > 0) {
425739204bcSClinton Sprain *z = psum >> ATP_SCALE; /* Scale down pressure output. */
426b5da20f8SDmitry Torokhov return pcum * fact / psum;
427b5da20f8SDmitry Torokhov }
428b5da20f8SDmitry Torokhov
429b5da20f8SDmitry Torokhov return 0;
430b5da20f8SDmitry Torokhov }
431b5da20f8SDmitry Torokhov
atp_report_fingers(struct input_dev * input,int fingers)432b5da20f8SDmitry Torokhov static inline void atp_report_fingers(struct input_dev *input, int fingers)
433b5da20f8SDmitry Torokhov {
434b5da20f8SDmitry Torokhov input_report_key(input, BTN_TOOL_FINGER, fingers == 1);
435b5da20f8SDmitry Torokhov input_report_key(input, BTN_TOOL_DOUBLETAP, fingers == 2);
436b5da20f8SDmitry Torokhov input_report_key(input, BTN_TOOL_TRIPLETAP, fingers > 2);
437b5da20f8SDmitry Torokhov }
438b5da20f8SDmitry Torokhov
439d83d213dSSven Anders /* Check URB status and for correct length of data package */
440d83d213dSSven Anders
441d83d213dSSven Anders #define ATP_URB_STATUS_SUCCESS 0
442d83d213dSSven Anders #define ATP_URB_STATUS_ERROR 1
443d83d213dSSven Anders #define ATP_URB_STATUS_ERROR_FATAL 2
444d83d213dSSven Anders
atp_status_check(struct urb * urb)445d83d213dSSven Anders static int atp_status_check(struct urb *urb)
446b5da20f8SDmitry Torokhov {
447b5da20f8SDmitry Torokhov struct atp *dev = urb->context;
4482d744b09SGreg Kroah-Hartman struct usb_interface *intf = dev->intf;
449b5da20f8SDmitry Torokhov
450b5da20f8SDmitry Torokhov switch (urb->status) {
451b5da20f8SDmitry Torokhov case 0:
452b5da20f8SDmitry Torokhov /* success */
453b5da20f8SDmitry Torokhov break;
454b5da20f8SDmitry Torokhov case -EOVERFLOW:
4557dce869fSJohannes Berg if (!dev->overflow_warned) {
4562d744b09SGreg Kroah-Hartman dev_warn(&intf->dev,
45767946d13SGreg Kroah-Hartman "appletouch: OVERFLOW with data length %d, actual length is %d\n",
45809779678SStelian Pop dev->info->datalen, dev->urb->actual_length);
4597dce869fSJohannes Berg dev->overflow_warned = true;
460b5da20f8SDmitry Torokhov }
4616f49c4f5SGustavo A. R. Silva fallthrough;
462b5da20f8SDmitry Torokhov case -ECONNRESET:
463b5da20f8SDmitry Torokhov case -ENOENT:
464b5da20f8SDmitry Torokhov case -ESHUTDOWN:
465b5da20f8SDmitry Torokhov /* This urb is terminated, clean up */
4662d744b09SGreg Kroah-Hartman dev_dbg(&intf->dev,
46767946d13SGreg Kroah-Hartman "atp_complete: urb shutting down with status: %d\n",
4687dce869fSJohannes Berg urb->status);
469d83d213dSSven Anders return ATP_URB_STATUS_ERROR_FATAL;
470d83d213dSSven Anders
471b5da20f8SDmitry Torokhov default:
4722d744b09SGreg Kroah-Hartman dev_dbg(&intf->dev,
47367946d13SGreg Kroah-Hartman "atp_complete: nonzero urb status received: %d\n",
4747dce869fSJohannes Berg urb->status);
475d83d213dSSven Anders return ATP_URB_STATUS_ERROR;
476b5da20f8SDmitry Torokhov }
477b5da20f8SDmitry Torokhov
478b5da20f8SDmitry Torokhov /* drop incomplete datasets */
47909779678SStelian Pop if (dev->urb->actual_length != dev->info->datalen) {
480b5da20f8SDmitry Torokhov dprintk("appletouch: incomplete data package"
481b5da20f8SDmitry Torokhov " (first byte: %d, length: %d).\n",
482b5da20f8SDmitry Torokhov dev->data[0], dev->urb->actual_length);
483d83d213dSSven Anders return ATP_URB_STATUS_ERROR;
484b5da20f8SDmitry Torokhov }
485b5da20f8SDmitry Torokhov
486d83d213dSSven Anders return ATP_URB_STATUS_SUCCESS;
487d83d213dSSven Anders }
488b5da20f8SDmitry Torokhov
atp_detect_size(struct atp * dev)48909779678SStelian Pop static void atp_detect_size(struct atp *dev)
49009779678SStelian Pop {
49109779678SStelian Pop int i;
49209779678SStelian Pop
49309779678SStelian Pop /* 17" Powerbooks have extra X sensors */
49409779678SStelian Pop for (i = dev->info->xsensors; i < ATP_XSENSORS; i++) {
49509779678SStelian Pop if (dev->xy_cur[i]) {
49609779678SStelian Pop
4972d744b09SGreg Kroah-Hartman dev_info(&dev->intf->dev,
49867946d13SGreg Kroah-Hartman "appletouch: 17\" model detected.\n");
49909779678SStelian Pop
50009779678SStelian Pop input_set_abs_params(dev->input, ABS_X, 0,
50109779678SStelian Pop (dev->info->xsensors_17 - 1) *
50209779678SStelian Pop dev->info->xfact - 1,
503703e1488SClinton Sprain dev->info->fuzz, 0);
50409779678SStelian Pop break;
50509779678SStelian Pop }
50609779678SStelian Pop }
50709779678SStelian Pop }
50809779678SStelian Pop
509b5da20f8SDmitry Torokhov /*
510d83d213dSSven Anders * USB interrupt callback functions
511b5da20f8SDmitry Torokhov */
512b5da20f8SDmitry Torokhov
513d83d213dSSven Anders /* Interrupt function for older touchpads: FOUNTAIN/GEYSER1/GEYSER2 */
514d83d213dSSven Anders
atp_complete_geyser_1_2(struct urb * urb)515d83d213dSSven Anders static void atp_complete_geyser_1_2(struct urb *urb)
516d83d213dSSven Anders {
517d83d213dSSven Anders int x, y, x_z, y_z, x_f, y_f;
518d83d213dSSven Anders int retval, i, j;
5197bbdba56SClinton Sprain int key, fingers;
520d83d213dSSven Anders struct atp *dev = urb->context;
521d83d213dSSven Anders int status = atp_status_check(urb);
522d83d213dSSven Anders
523d83d213dSSven Anders if (status == ATP_URB_STATUS_ERROR_FATAL)
524d83d213dSSven Anders return;
525d83d213dSSven Anders else if (status == ATP_URB_STATUS_ERROR)
526d83d213dSSven Anders goto exit;
527d83d213dSSven Anders
528d83d213dSSven Anders /* reorder the sensors values */
52909779678SStelian Pop if (dev->info == &geyser2_info) {
530b5da20f8SDmitry Torokhov memset(dev->xy_cur, 0, sizeof(dev->xy_cur));
531b5da20f8SDmitry Torokhov
532b5da20f8SDmitry Torokhov /*
533b5da20f8SDmitry Torokhov * The values are laid out like this:
534b5da20f8SDmitry Torokhov * Y1, Y2, -, Y3, Y4, -, ..., X1, X2, -, X3, X4, -, ...
535b5da20f8SDmitry Torokhov * '-' is an unused value.
536b5da20f8SDmitry Torokhov */
537b5da20f8SDmitry Torokhov
538b5da20f8SDmitry Torokhov /* read X values */
539b5da20f8SDmitry Torokhov for (i = 0, j = 19; i < 20; i += 2, j += 3) {
540b5da20f8SDmitry Torokhov dev->xy_cur[i] = dev->data[j];
541b5da20f8SDmitry Torokhov dev->xy_cur[i + 1] = dev->data[j + 1];
542b5da20f8SDmitry Torokhov }
543b5da20f8SDmitry Torokhov
544b5da20f8SDmitry Torokhov /* read Y values */
545b5da20f8SDmitry Torokhov for (i = 0, j = 1; i < 9; i += 2, j += 3) {
546b5da20f8SDmitry Torokhov dev->xy_cur[ATP_XSENSORS + i] = dev->data[j];
547b5da20f8SDmitry Torokhov dev->xy_cur[ATP_XSENSORS + i + 1] = dev->data[j + 1];
548b5da20f8SDmitry Torokhov }
549b5da20f8SDmitry Torokhov } else {
550b5da20f8SDmitry Torokhov for (i = 0; i < 8; i++) {
551b5da20f8SDmitry Torokhov /* X values */
5527dce869fSJohannes Berg dev->xy_cur[i + 0] = dev->data[5 * i + 2];
553b5da20f8SDmitry Torokhov dev->xy_cur[i + 8] = dev->data[5 * i + 4];
554b5da20f8SDmitry Torokhov dev->xy_cur[i + 16] = dev->data[5 * i + 42];
555b5da20f8SDmitry Torokhov if (i < 2)
556b5da20f8SDmitry Torokhov dev->xy_cur[i + 24] = dev->data[5 * i + 44];
557b5da20f8SDmitry Torokhov
558b5da20f8SDmitry Torokhov /* Y values */
55909779678SStelian Pop dev->xy_cur[ATP_XSENSORS + i] = dev->data[5 * i + 1];
56009779678SStelian Pop dev->xy_cur[ATP_XSENSORS + i + 8] = dev->data[5 * i + 3];
561b5da20f8SDmitry Torokhov }
562b5da20f8SDmitry Torokhov }
563b5da20f8SDmitry Torokhov
564b5da20f8SDmitry Torokhov dbg_dump("sample", dev->xy_cur);
565b5da20f8SDmitry Torokhov
566b5da20f8SDmitry Torokhov if (!dev->valid) {
567b5da20f8SDmitry Torokhov /* first sample */
5687dce869fSJohannes Berg dev->valid = true;
569b5da20f8SDmitry Torokhov dev->x_old = dev->y_old = -1;
570d83d213dSSven Anders
571d83d213dSSven Anders /* Store first sample */
572b5da20f8SDmitry Torokhov memcpy(dev->xy_old, dev->xy_cur, sizeof(dev->xy_old));
573b5da20f8SDmitry Torokhov
574d83d213dSSven Anders /* Perform size detection, if not done already */
57509779678SStelian Pop if (unlikely(!dev->size_detect_done)) {
57609779678SStelian Pop atp_detect_size(dev);
57770851233SGustavo A. R. Silva dev->size_detect_done = true;
578b5da20f8SDmitry Torokhov goto exit;
579b5da20f8SDmitry Torokhov }
580d83d213dSSven Anders }
581d83d213dSSven Anders
582d83d213dSSven Anders for (i = 0; i < ATP_XSENSORS + ATP_YSENSORS; i++) {
583d83d213dSSven Anders /* accumulate the change */
5846e49c1a4SBenjamin Herrenschmidt signed char change = dev->xy_old[i] - dev->xy_cur[i];
585d83d213dSSven Anders dev->xy_acc[i] -= change;
586d83d213dSSven Anders
587d83d213dSSven Anders /* prevent down drifting */
588d83d213dSSven Anders if (dev->xy_acc[i] < 0)
589d83d213dSSven Anders dev->xy_acc[i] = 0;
590d83d213dSSven Anders }
591d83d213dSSven Anders
592d83d213dSSven Anders memcpy(dev->xy_old, dev->xy_cur, sizeof(dev->xy_old));
593d83d213dSSven Anders
594d83d213dSSven Anders dbg_dump("accumulator", dev->xy_acc);
595d83d213dSSven Anders
596739204bcSClinton Sprain x = atp_calculate_abs(dev, 0, ATP_XSENSORS,
59709779678SStelian Pop dev->info->xfact, &x_z, &x_f);
598739204bcSClinton Sprain y = atp_calculate_abs(dev, ATP_XSENSORS, ATP_YSENSORS,
59909779678SStelian Pop dev->info->yfact, &y_z, &y_f);
60009779678SStelian Pop key = dev->data[dev->info->datalen - 1] & ATP_STATUS_BUTTON;
601d83d213dSSven Anders
6027bbdba56SClinton Sprain fingers = max(x_f, y_f);
6037bbdba56SClinton Sprain
6047bbdba56SClinton Sprain if (x && y && fingers == dev->fingers_old) {
605d83d213dSSven Anders if (dev->x_old != -1) {
606739204bcSClinton Sprain x = (dev->x_old * 7 + x) >> 3;
607739204bcSClinton Sprain y = (dev->y_old * 7 + y) >> 3;
608d83d213dSSven Anders dev->x_old = x;
609d83d213dSSven Anders dev->y_old = y;
610d83d213dSSven Anders
611d83d213dSSven Anders if (debug > 1)
612d83d213dSSven Anders printk(KERN_DEBUG "appletouch: "
613d83d213dSSven Anders "X: %3d Y: %3d Xz: %3d Yz: %3d\n",
614d83d213dSSven Anders x, y, x_z, y_z);
615d83d213dSSven Anders
616d83d213dSSven Anders input_report_key(dev->input, BTN_TOUCH, 1);
617d83d213dSSven Anders input_report_abs(dev->input, ABS_X, x);
618d83d213dSSven Anders input_report_abs(dev->input, ABS_Y, y);
619d83d213dSSven Anders input_report_abs(dev->input, ABS_PRESSURE,
620d83d213dSSven Anders min(ATP_PRESSURE, x_z + y_z));
6217bbdba56SClinton Sprain atp_report_fingers(dev->input, fingers);
622d83d213dSSven Anders }
623d83d213dSSven Anders dev->x_old = x;
624d83d213dSSven Anders dev->y_old = y;
625d83d213dSSven Anders
626d83d213dSSven Anders } else if (!x && !y) {
627d83d213dSSven Anders
628d83d213dSSven Anders dev->x_old = dev->y_old = -1;
6297bbdba56SClinton Sprain dev->fingers_old = 0;
630d83d213dSSven Anders input_report_key(dev->input, BTN_TOUCH, 0);
631d83d213dSSven Anders input_report_abs(dev->input, ABS_PRESSURE, 0);
632d83d213dSSven Anders atp_report_fingers(dev->input, 0);
633d83d213dSSven Anders
634d83d213dSSven Anders /* reset the accumulator on release */
635d83d213dSSven Anders memset(dev->xy_acc, 0, sizeof(dev->xy_acc));
636d83d213dSSven Anders }
637d83d213dSSven Anders
6387bbdba56SClinton Sprain if (fingers != dev->fingers_old)
6397bbdba56SClinton Sprain dev->x_old = dev->y_old = -1;
6407bbdba56SClinton Sprain dev->fingers_old = fingers;
6417bbdba56SClinton Sprain
642d83d213dSSven Anders input_report_key(dev->input, BTN_LEFT, key);
643d83d213dSSven Anders input_sync(dev->input);
644d83d213dSSven Anders
645d83d213dSSven Anders exit:
646d83d213dSSven Anders retval = usb_submit_urb(dev->urb, GFP_ATOMIC);
647d83d213dSSven Anders if (retval)
6482d744b09SGreg Kroah-Hartman dev_err(&dev->intf->dev,
6499c113dc6SGreg Kroah-Hartman "atp_complete: usb_submit_urb failed with result %d\n",
650d83d213dSSven Anders retval);
651d83d213dSSven Anders }
652d83d213dSSven Anders
653d83d213dSSven Anders /* Interrupt function for older touchpads: GEYSER3/GEYSER4 */
654d83d213dSSven Anders
atp_complete_geyser_3_4(struct urb * urb)655d83d213dSSven Anders static void atp_complete_geyser_3_4(struct urb *urb)
656d83d213dSSven Anders {
657d83d213dSSven Anders int x, y, x_z, y_z, x_f, y_f;
658d83d213dSSven Anders int retval, i, j;
6597bbdba56SClinton Sprain int key, fingers;
660d83d213dSSven Anders struct atp *dev = urb->context;
661d83d213dSSven Anders int status = atp_status_check(urb);
662d83d213dSSven Anders
663d83d213dSSven Anders if (status == ATP_URB_STATUS_ERROR_FATAL)
664d83d213dSSven Anders return;
665d83d213dSSven Anders else if (status == ATP_URB_STATUS_ERROR)
666d83d213dSSven Anders goto exit;
667d83d213dSSven Anders
668d83d213dSSven Anders /* Reorder the sensors values:
669d83d213dSSven Anders *
670d83d213dSSven Anders * The values are laid out like this:
671d83d213dSSven Anders * -, Y1, Y2, -, Y3, Y4, -, ..., -, X1, X2, -, X3, X4, ...
672d83d213dSSven Anders * '-' is an unused value.
673d83d213dSSven Anders */
674d83d213dSSven Anders
675d83d213dSSven Anders /* read X values */
676d83d213dSSven Anders for (i = 0, j = 19; i < 20; i += 2, j += 3) {
677d83d213dSSven Anders dev->xy_cur[i] = dev->data[j + 1];
678d83d213dSSven Anders dev->xy_cur[i + 1] = dev->data[j + 2];
679d83d213dSSven Anders }
680d83d213dSSven Anders /* read Y values */
681d83d213dSSven Anders for (i = 0, j = 1; i < 9; i += 2, j += 3) {
682d83d213dSSven Anders dev->xy_cur[ATP_XSENSORS + i] = dev->data[j + 1];
683d83d213dSSven Anders dev->xy_cur[ATP_XSENSORS + i + 1] = dev->data[j + 2];
684d83d213dSSven Anders }
685d83d213dSSven Anders
686d83d213dSSven Anders dbg_dump("sample", dev->xy_cur);
687d83d213dSSven Anders
68882a196f4SSven Anders /* Just update the base values (i.e. touchpad in untouched state) */
68909779678SStelian Pop if (dev->data[dev->info->datalen - 1] & ATP_STATUS_BASE_UPDATE) {
690d83d213dSSven Anders
691d745b532SJoe Perches dprintk("appletouch: updated base values\n");
69282a196f4SSven Anders
69382a196f4SSven Anders memcpy(dev->xy_old, dev->xy_cur, sizeof(dev->xy_old));
694d83d213dSSven Anders goto exit;
695d83d213dSSven Anders }
696b5da20f8SDmitry Torokhov
697b5da20f8SDmitry Torokhov for (i = 0; i < ATP_XSENSORS + ATP_YSENSORS; i++) {
69882a196f4SSven Anders /* calculate the change */
69982a196f4SSven Anders dev->xy_acc[i] = dev->xy_cur[i] - dev->xy_old[i];
70082a196f4SSven Anders
70182a196f4SSven Anders /* this is a round-robin value, so couple with that */
70282a196f4SSven Anders if (dev->xy_acc[i] > 127)
70382a196f4SSven Anders dev->xy_acc[i] -= 256;
70482a196f4SSven Anders
70582a196f4SSven Anders if (dev->xy_acc[i] < -127)
70682a196f4SSven Anders dev->xy_acc[i] += 256;
707b5da20f8SDmitry Torokhov
708b5da20f8SDmitry Torokhov /* prevent down drifting */
709b5da20f8SDmitry Torokhov if (dev->xy_acc[i] < 0)
710b5da20f8SDmitry Torokhov dev->xy_acc[i] = 0;
711b5da20f8SDmitry Torokhov }
712b5da20f8SDmitry Torokhov
713b5da20f8SDmitry Torokhov dbg_dump("accumulator", dev->xy_acc);
714b5da20f8SDmitry Torokhov
715739204bcSClinton Sprain x = atp_calculate_abs(dev, 0, ATP_XSENSORS,
71609779678SStelian Pop dev->info->xfact, &x_z, &x_f);
717739204bcSClinton Sprain y = atp_calculate_abs(dev, ATP_XSENSORS, ATP_YSENSORS,
71809779678SStelian Pop dev->info->yfact, &y_z, &y_f);
719739204bcSClinton Sprain
72009779678SStelian Pop key = dev->data[dev->info->datalen - 1] & ATP_STATUS_BUTTON;
721b5da20f8SDmitry Torokhov
7227bbdba56SClinton Sprain fingers = max(x_f, y_f);
7237bbdba56SClinton Sprain
7247bbdba56SClinton Sprain if (x && y && fingers == dev->fingers_old) {
725b5da20f8SDmitry Torokhov if (dev->x_old != -1) {
726739204bcSClinton Sprain x = (dev->x_old * 7 + x) >> 3;
727739204bcSClinton Sprain y = (dev->y_old * 7 + y) >> 3;
728b5da20f8SDmitry Torokhov dev->x_old = x;
729b5da20f8SDmitry Torokhov dev->y_old = y;
730b5da20f8SDmitry Torokhov
731b5da20f8SDmitry Torokhov if (debug > 1)
7322a3e480dSDmitry Torokhov printk(KERN_DEBUG "appletouch: X: %3d Y: %3d "
733b5da20f8SDmitry Torokhov "Xz: %3d Yz: %3d\n",
734b5da20f8SDmitry Torokhov x, y, x_z, y_z);
735b5da20f8SDmitry Torokhov
736b5da20f8SDmitry Torokhov input_report_key(dev->input, BTN_TOUCH, 1);
737b5da20f8SDmitry Torokhov input_report_abs(dev->input, ABS_X, x);
738b5da20f8SDmitry Torokhov input_report_abs(dev->input, ABS_Y, y);
739b5da20f8SDmitry Torokhov input_report_abs(dev->input, ABS_PRESSURE,
740b5da20f8SDmitry Torokhov min(ATP_PRESSURE, x_z + y_z));
7417bbdba56SClinton Sprain atp_report_fingers(dev->input, fingers);
742b5da20f8SDmitry Torokhov }
743b5da20f8SDmitry Torokhov dev->x_old = x;
744b5da20f8SDmitry Torokhov dev->y_old = y;
7455a6eb676SSoeren Sonnenburg
7465a6eb676SSoeren Sonnenburg } else if (!x && !y) {
747b5da20f8SDmitry Torokhov
748b5da20f8SDmitry Torokhov dev->x_old = dev->y_old = -1;
7497bbdba56SClinton Sprain dev->fingers_old = 0;
750b5da20f8SDmitry Torokhov input_report_key(dev->input, BTN_TOUCH, 0);
751b5da20f8SDmitry Torokhov input_report_abs(dev->input, ABS_PRESSURE, 0);
752b5da20f8SDmitry Torokhov atp_report_fingers(dev->input, 0);
753b5da20f8SDmitry Torokhov
754b5da20f8SDmitry Torokhov /* reset the accumulator on release */
755b5da20f8SDmitry Torokhov memset(dev->xy_acc, 0, sizeof(dev->xy_acc));
756937ad5c1SSoeren Sonnenburg }
7575a6eb676SSoeren Sonnenburg
7587bbdba56SClinton Sprain if (fingers != dev->fingers_old)
7597bbdba56SClinton Sprain dev->x_old = dev->y_old = -1;
7607bbdba56SClinton Sprain dev->fingers_old = fingers;
7617bbdba56SClinton Sprain
76246249ea6SAnton Ekblad input_report_key(dev->input, BTN_LEFT, key);
76346249ea6SAnton Ekblad input_sync(dev->input);
76446249ea6SAnton Ekblad
7652a3e480dSDmitry Torokhov /*
766d83d213dSSven Anders * Geysers 3/4 will continue to send packets continually after
7672a3e480dSDmitry Torokhov * the first touch unless reinitialised. Do so if it's been
7682a3e480dSDmitry Torokhov * idle for a while in order to avoid waking the kernel up
769d83d213dSSven Anders * several hundred times a second.
7702a3e480dSDmitry Torokhov */
771d83d213dSSven Anders
7727dce869fSJohannes Berg /*
7737dce869fSJohannes Berg * Button must not be pressed when entering suspend,
7747dce869fSJohannes Berg * otherwise we will never release the button.
7757dce869fSJohannes Berg */
776937ad5c1SSoeren Sonnenburg if (!x && !y && !key) {
7775a6eb676SSoeren Sonnenburg dev->idlecount++;
7785a6eb676SSoeren Sonnenburg if (dev->idlecount == 10) {
77982a196f4SSven Anders dev->x_old = dev->y_old = -1;
78082a196f4SSven Anders dev->idlecount = 0;
7815a6eb676SSoeren Sonnenburg schedule_work(&dev->work);
7822a3e480dSDmitry Torokhov /* Don't resubmit urb here, wait for reinit */
7832a3e480dSDmitry Torokhov return;
7845a6eb676SSoeren Sonnenburg }
78546249ea6SAnton Ekblad } else
786937ad5c1SSoeren Sonnenburg dev->idlecount = 0;
787b5da20f8SDmitry Torokhov
788b5da20f8SDmitry Torokhov exit:
789b5da20f8SDmitry Torokhov retval = usb_submit_urb(dev->urb, GFP_ATOMIC);
7907dce869fSJohannes Berg if (retval)
7912d744b09SGreg Kroah-Hartman dev_err(&dev->intf->dev,
7929c113dc6SGreg Kroah-Hartman "atp_complete: usb_submit_urb failed with result %d\n",
7937dce869fSJohannes Berg retval);
794b5da20f8SDmitry Torokhov }
795b5da20f8SDmitry Torokhov
atp_open(struct input_dev * input)796b5da20f8SDmitry Torokhov static int atp_open(struct input_dev *input)
797b5da20f8SDmitry Torokhov {
798b5da20f8SDmitry Torokhov struct atp *dev = input_get_drvdata(input);
799b5da20f8SDmitry Torokhov
8007a082a24SJia-Ju Bai if (usb_submit_urb(dev->urb, GFP_KERNEL))
801b5da20f8SDmitry Torokhov return -EIO;
802b5da20f8SDmitry Torokhov
80370851233SGustavo A. R. Silva dev->open = true;
804b5da20f8SDmitry Torokhov return 0;
805b5da20f8SDmitry Torokhov }
806b5da20f8SDmitry Torokhov
atp_close(struct input_dev * input)807b5da20f8SDmitry Torokhov static void atp_close(struct input_dev *input)
808b5da20f8SDmitry Torokhov {
809b5da20f8SDmitry Torokhov struct atp *dev = input_get_drvdata(input);
810b5da20f8SDmitry Torokhov
811b5da20f8SDmitry Torokhov usb_kill_urb(dev->urb);
8125a6eb676SSoeren Sonnenburg cancel_work_sync(&dev->work);
81370851233SGustavo A. R. Silva dev->open = false;
814b5da20f8SDmitry Torokhov }
815b5da20f8SDmitry Torokhov
atp_handle_geyser(struct atp * dev)81690d95ef6SOliver Neukum static int atp_handle_geyser(struct atp *dev)
81790d95ef6SOliver Neukum {
81809779678SStelian Pop if (dev->info != &fountain_info) {
81990d95ef6SOliver Neukum /* switch to raw sensor mode */
82080f8594fSGreg Kroah-Hartman if (atp_geyser_init(dev))
82190d95ef6SOliver Neukum return -EIO;
82290d95ef6SOliver Neukum
8232d744b09SGreg Kroah-Hartman dev_info(&dev->intf->dev, "Geyser mode initialized.\n");
82490d95ef6SOliver Neukum }
82590d95ef6SOliver Neukum
82690d95ef6SOliver Neukum return 0;
82790d95ef6SOliver Neukum }
82890d95ef6SOliver Neukum
atp_probe(struct usb_interface * iface,const struct usb_device_id * id)8297dce869fSJohannes Berg static int atp_probe(struct usb_interface *iface,
8307dce869fSJohannes Berg const struct usb_device_id *id)
831b5da20f8SDmitry Torokhov {
832b5da20f8SDmitry Torokhov struct atp *dev;
833b5da20f8SDmitry Torokhov struct input_dev *input_dev;
834b5da20f8SDmitry Torokhov struct usb_device *udev = interface_to_usbdev(iface);
835b5da20f8SDmitry Torokhov struct usb_host_interface *iface_desc;
836b5da20f8SDmitry Torokhov struct usb_endpoint_descriptor *endpoint;
837b5da20f8SDmitry Torokhov int int_in_endpointAddr = 0;
838b5da20f8SDmitry Torokhov int i, error = -ENOMEM;
83909779678SStelian Pop const struct atp_info *info = (const struct atp_info *)id->driver_info;
840b5da20f8SDmitry Torokhov
841b5da20f8SDmitry Torokhov /* set up the endpoint information */
842b5da20f8SDmitry Torokhov /* use only the first interrupt-in endpoint */
843b5da20f8SDmitry Torokhov iface_desc = iface->cur_altsetting;
844b5da20f8SDmitry Torokhov for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
845b5da20f8SDmitry Torokhov endpoint = &iface_desc->endpoint[i].desc;
846b5da20f8SDmitry Torokhov if (!int_in_endpointAddr && usb_endpoint_is_int_in(endpoint)) {
847b5da20f8SDmitry Torokhov /* we found an interrupt in endpoint */
848b5da20f8SDmitry Torokhov int_in_endpointAddr = endpoint->bEndpointAddress;
849b5da20f8SDmitry Torokhov break;
850b5da20f8SDmitry Torokhov }
851b5da20f8SDmitry Torokhov }
852b5da20f8SDmitry Torokhov if (!int_in_endpointAddr) {
8539c113dc6SGreg Kroah-Hartman dev_err(&iface->dev, "Could not find int-in endpoint\n");
854b5da20f8SDmitry Torokhov return -EIO;
855b5da20f8SDmitry Torokhov }
856b5da20f8SDmitry Torokhov
857b5da20f8SDmitry Torokhov /* allocate memory for our device state and initialize it */
858b5da20f8SDmitry Torokhov dev = kzalloc(sizeof(struct atp), GFP_KERNEL);
859b5da20f8SDmitry Torokhov input_dev = input_allocate_device();
860b5da20f8SDmitry Torokhov if (!dev || !input_dev) {
8619c113dc6SGreg Kroah-Hartman dev_err(&iface->dev, "Out of memory\n");
862b5da20f8SDmitry Torokhov goto err_free_devs;
863b5da20f8SDmitry Torokhov }
864b5da20f8SDmitry Torokhov
865b5da20f8SDmitry Torokhov dev->udev = udev;
8662d744b09SGreg Kroah-Hartman dev->intf = iface;
867b5da20f8SDmitry Torokhov dev->input = input_dev;
86809779678SStelian Pop dev->info = info;
8697dce869fSJohannes Berg dev->overflow_warned = false;
870b5da20f8SDmitry Torokhov
871b5da20f8SDmitry Torokhov dev->urb = usb_alloc_urb(0, GFP_KERNEL);
872b5da20f8SDmitry Torokhov if (!dev->urb)
873b5da20f8SDmitry Torokhov goto err_free_devs;
874b5da20f8SDmitry Torokhov
875997ea58eSDaniel Mack dev->data = usb_alloc_coherent(dev->udev, dev->info->datalen, GFP_KERNEL,
876b5da20f8SDmitry Torokhov &dev->urb->transfer_dma);
877b5da20f8SDmitry Torokhov if (!dev->data)
878b5da20f8SDmitry Torokhov goto err_free_urb;
879b5da20f8SDmitry Torokhov
880b5da20f8SDmitry Torokhov usb_fill_int_urb(dev->urb, udev,
881b5da20f8SDmitry Torokhov usb_rcvintpipe(udev, int_in_endpointAddr),
88209779678SStelian Pop dev->data, dev->info->datalen,
88309779678SStelian Pop dev->info->callback, dev, 1);
884b5da20f8SDmitry Torokhov
88590d95ef6SOliver Neukum error = atp_handle_geyser(dev);
88690d95ef6SOliver Neukum if (error)
88790d95ef6SOliver Neukum goto err_free_buffer;
88890d95ef6SOliver Neukum
889b5da20f8SDmitry Torokhov usb_make_path(udev, dev->phys, sizeof(dev->phys));
890b5da20f8SDmitry Torokhov strlcat(dev->phys, "/input0", sizeof(dev->phys));
891b5da20f8SDmitry Torokhov
892b5da20f8SDmitry Torokhov input_dev->name = "appletouch";
893b5da20f8SDmitry Torokhov input_dev->phys = dev->phys;
894b5da20f8SDmitry Torokhov usb_to_input_id(dev->udev, &input_dev->id);
895b5da20f8SDmitry Torokhov input_dev->dev.parent = &iface->dev;
896b5da20f8SDmitry Torokhov
897b5da20f8SDmitry Torokhov input_set_drvdata(input_dev, dev);
898b5da20f8SDmitry Torokhov
899b5da20f8SDmitry Torokhov input_dev->open = atp_open;
900b5da20f8SDmitry Torokhov input_dev->close = atp_close;
901b5da20f8SDmitry Torokhov
902b5da20f8SDmitry Torokhov set_bit(EV_ABS, input_dev->evbit);
903b5da20f8SDmitry Torokhov
904b5da20f8SDmitry Torokhov input_set_abs_params(input_dev, ABS_X, 0,
90509779678SStelian Pop (dev->info->xsensors - 1) * dev->info->xfact - 1,
906703e1488SClinton Sprain dev->info->fuzz, 0);
907b5da20f8SDmitry Torokhov input_set_abs_params(input_dev, ABS_Y, 0,
90809779678SStelian Pop (dev->info->ysensors - 1) * dev->info->yfact - 1,
909703e1488SClinton Sprain dev->info->fuzz, 0);
910b5da20f8SDmitry Torokhov input_set_abs_params(input_dev, ABS_PRESSURE, 0, ATP_PRESSURE, 0, 0);
911b5da20f8SDmitry Torokhov
912b5da20f8SDmitry Torokhov set_bit(EV_KEY, input_dev->evbit);
913b5da20f8SDmitry Torokhov set_bit(BTN_TOUCH, input_dev->keybit);
914b5da20f8SDmitry Torokhov set_bit(BTN_TOOL_FINGER, input_dev->keybit);
915b5da20f8SDmitry Torokhov set_bit(BTN_TOOL_DOUBLETAP, input_dev->keybit);
916b5da20f8SDmitry Torokhov set_bit(BTN_TOOL_TRIPLETAP, input_dev->keybit);
917b5da20f8SDmitry Torokhov set_bit(BTN_LEFT, input_dev->keybit);
918b5da20f8SDmitry Torokhov
919*9f3ccdc3SPavel Skripkin INIT_WORK(&dev->work, atp_reinit);
920*9f3ccdc3SPavel Skripkin
921b5da20f8SDmitry Torokhov error = input_register_device(dev->input);
922b5da20f8SDmitry Torokhov if (error)
923b5da20f8SDmitry Torokhov goto err_free_buffer;
924b5da20f8SDmitry Torokhov
925b5da20f8SDmitry Torokhov /* save our data pointer in this interface device */
926b5da20f8SDmitry Torokhov usb_set_intfdata(iface, dev);
927b5da20f8SDmitry Torokhov
928b5da20f8SDmitry Torokhov return 0;
929b5da20f8SDmitry Torokhov
930b5da20f8SDmitry Torokhov err_free_buffer:
931997ea58eSDaniel Mack usb_free_coherent(dev->udev, dev->info->datalen,
932b5da20f8SDmitry Torokhov dev->data, dev->urb->transfer_dma);
933b5da20f8SDmitry Torokhov err_free_urb:
934b5da20f8SDmitry Torokhov usb_free_urb(dev->urb);
935b5da20f8SDmitry Torokhov err_free_devs:
936b5da20f8SDmitry Torokhov usb_set_intfdata(iface, NULL);
937b5da20f8SDmitry Torokhov kfree(dev);
938b5da20f8SDmitry Torokhov input_free_device(input_dev);
939b5da20f8SDmitry Torokhov return error;
940b5da20f8SDmitry Torokhov }
941b5da20f8SDmitry Torokhov
atp_disconnect(struct usb_interface * iface)942b5da20f8SDmitry Torokhov static void atp_disconnect(struct usb_interface *iface)
943b5da20f8SDmitry Torokhov {
944b5da20f8SDmitry Torokhov struct atp *dev = usb_get_intfdata(iface);
945b5da20f8SDmitry Torokhov
946b5da20f8SDmitry Torokhov usb_set_intfdata(iface, NULL);
947b5da20f8SDmitry Torokhov if (dev) {
948b5da20f8SDmitry Torokhov usb_kill_urb(dev->urb);
949b5da20f8SDmitry Torokhov input_unregister_device(dev->input);
950997ea58eSDaniel Mack usb_free_coherent(dev->udev, dev->info->datalen,
951b5da20f8SDmitry Torokhov dev->data, dev->urb->transfer_dma);
952b5da20f8SDmitry Torokhov usb_free_urb(dev->urb);
953b5da20f8SDmitry Torokhov kfree(dev);
954b5da20f8SDmitry Torokhov }
9552d744b09SGreg Kroah-Hartman dev_info(&iface->dev, "input: appletouch disconnected\n");
956b5da20f8SDmitry Torokhov }
957b5da20f8SDmitry Torokhov
atp_recover(struct atp * dev)95890d95ef6SOliver Neukum static int atp_recover(struct atp *dev)
95990d95ef6SOliver Neukum {
96090d95ef6SOliver Neukum int error;
96190d95ef6SOliver Neukum
96290d95ef6SOliver Neukum error = atp_handle_geyser(dev);
96390d95ef6SOliver Neukum if (error)
96490d95ef6SOliver Neukum return error;
96590d95ef6SOliver Neukum
9667a082a24SJia-Ju Bai if (dev->open && usb_submit_urb(dev->urb, GFP_KERNEL))
96790d95ef6SOliver Neukum return -EIO;
96890d95ef6SOliver Neukum
96990d95ef6SOliver Neukum return 0;
97090d95ef6SOliver Neukum }
97190d95ef6SOliver Neukum
atp_suspend(struct usb_interface * iface,pm_message_t message)972b5da20f8SDmitry Torokhov static int atp_suspend(struct usb_interface *iface, pm_message_t message)
973b5da20f8SDmitry Torokhov {
974b5da20f8SDmitry Torokhov struct atp *dev = usb_get_intfdata(iface);
9755a6eb676SSoeren Sonnenburg
976b5da20f8SDmitry Torokhov usb_kill_urb(dev->urb);
977b5da20f8SDmitry Torokhov return 0;
978b5da20f8SDmitry Torokhov }
979b5da20f8SDmitry Torokhov
atp_resume(struct usb_interface * iface)980b5da20f8SDmitry Torokhov static int atp_resume(struct usb_interface *iface)
981b5da20f8SDmitry Torokhov {
982b5da20f8SDmitry Torokhov struct atp *dev = usb_get_intfdata(iface);
9835a6eb676SSoeren Sonnenburg
9847a082a24SJia-Ju Bai if (dev->open && usb_submit_urb(dev->urb, GFP_KERNEL))
985b5da20f8SDmitry Torokhov return -EIO;
986b5da20f8SDmitry Torokhov
987b5da20f8SDmitry Torokhov return 0;
988b5da20f8SDmitry Torokhov }
989b5da20f8SDmitry Torokhov
atp_reset_resume(struct usb_interface * iface)99090d95ef6SOliver Neukum static int atp_reset_resume(struct usb_interface *iface)
99190d95ef6SOliver Neukum {
99290d95ef6SOliver Neukum struct atp *dev = usb_get_intfdata(iface);
99390d95ef6SOliver Neukum
99490d95ef6SOliver Neukum return atp_recover(dev);
99590d95ef6SOliver Neukum }
99690d95ef6SOliver Neukum
997b5da20f8SDmitry Torokhov static struct usb_driver atp_driver = {
998b5da20f8SDmitry Torokhov .name = "appletouch",
999b5da20f8SDmitry Torokhov .probe = atp_probe,
1000b5da20f8SDmitry Torokhov .disconnect = atp_disconnect,
1001b5da20f8SDmitry Torokhov .suspend = atp_suspend,
1002b5da20f8SDmitry Torokhov .resume = atp_resume,
100390d95ef6SOliver Neukum .reset_resume = atp_reset_resume,
1004b5da20f8SDmitry Torokhov .id_table = atp_table,
1005b5da20f8SDmitry Torokhov };
1006b5da20f8SDmitry Torokhov
100708642e7cSGreg Kroah-Hartman module_usb_driver(atp_driver);
1008