1b5da20f8SDmitry Torokhov /* 2b5da20f8SDmitry Torokhov * Apple USB Touchpad (for post-February 2005 PowerBooks and MacBooks) driver 3b5da20f8SDmitry Torokhov * 4b5da20f8SDmitry Torokhov * Copyright (C) 2001-2004 Greg Kroah-Hartman (greg@kroah.com) 57dce869fSJohannes Berg * Copyright (C) 2005-2008 Johannes Berg (johannes@sipsolutions.net) 609779678SStelian Pop * Copyright (C) 2005-2008 Stelian Pop (stelian@popies.net) 7b5da20f8SDmitry Torokhov * Copyright (C) 2005 Frank Arnold (frank@scirocco-5v-turbo.de) 8b5da20f8SDmitry Torokhov * Copyright (C) 2005 Peter Osterlund (petero2@telia.com) 9b5da20f8SDmitry Torokhov * Copyright (C) 2005 Michael Hanselmann (linux-kernel@hansmi.ch) 10b5da20f8SDmitry Torokhov * Copyright (C) 2006 Nicolas Boichat (nicolas@boichat.ch) 117dce869fSJohannes Berg * Copyright (C) 2007-2008 Sven Anders (anders@anduras.de) 12b5da20f8SDmitry Torokhov * 13b5da20f8SDmitry Torokhov * Thanks to Alex Harper <basilisk@foobox.net> for his inputs. 14b5da20f8SDmitry Torokhov * 15b5da20f8SDmitry Torokhov * This program is free software; you can redistribute it and/or modify 16b5da20f8SDmitry Torokhov * it under the terms of the GNU General Public License as published by 17b5da20f8SDmitry Torokhov * the Free Software Foundation; either version 2 of the License, or 18b5da20f8SDmitry Torokhov * (at your option) any later version. 19b5da20f8SDmitry Torokhov * 20b5da20f8SDmitry Torokhov * This program is distributed in the hope that it will be useful, 21b5da20f8SDmitry Torokhov * but WITHOUT ANY WARRANTY; without even the implied warranty of 22b5da20f8SDmitry Torokhov * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 23b5da20f8SDmitry Torokhov * GNU General Public License for more details. 24b5da20f8SDmitry Torokhov * 25b5da20f8SDmitry Torokhov * You should have received a copy of the GNU General Public License 26b5da20f8SDmitry Torokhov * along with this program; if not, write to the Free Software 27b5da20f8SDmitry Torokhov * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 28b5da20f8SDmitry Torokhov * 29b5da20f8SDmitry Torokhov */ 30b5da20f8SDmitry Torokhov 31b5da20f8SDmitry Torokhov #include <linux/kernel.h> 32b5da20f8SDmitry Torokhov #include <linux/errno.h> 33b5da20f8SDmitry Torokhov #include <linux/slab.h> 34b5da20f8SDmitry Torokhov #include <linux/module.h> 35b5da20f8SDmitry Torokhov #include <linux/usb/input.h> 36b5da20f8SDmitry Torokhov 3709779678SStelian Pop /* 3809779678SStelian Pop * Note: We try to keep the touchpad aspect ratio while still doing only 3909779678SStelian Pop * simple arithmetics: 4009779678SStelian Pop * 0 <= x <= (xsensors - 1) * xfact 4109779678SStelian Pop * 0 <= y <= (ysensors - 1) * yfact 4209779678SStelian Pop */ 4309779678SStelian Pop struct atp_info { 4409779678SStelian Pop int xsensors; /* number of X sensors */ 4509779678SStelian Pop int xsensors_17; /* 17" models have more sensors */ 4609779678SStelian Pop int ysensors; /* number of Y sensors */ 4709779678SStelian Pop int xfact; /* X multiplication factor */ 4809779678SStelian Pop int yfact; /* Y multiplication factor */ 4909779678SStelian Pop int datalen; /* size of USB transfers */ 5009779678SStelian Pop void (*callback)(struct urb *); /* callback function */ 51703e1488SClinton Sprain int fuzz; /* fuzz touchpad generates */ 52e9542dffSSven Anders }; 53b5da20f8SDmitry Torokhov 5409779678SStelian Pop static void atp_complete_geyser_1_2(struct urb *urb); 5509779678SStelian Pop static void atp_complete_geyser_3_4(struct urb *urb); 5609779678SStelian Pop 5709779678SStelian Pop static const struct atp_info fountain_info = { 5809779678SStelian Pop .xsensors = 16, 5909779678SStelian Pop .xsensors_17 = 26, 6009779678SStelian Pop .ysensors = 16, 6109779678SStelian Pop .xfact = 64, 6209779678SStelian Pop .yfact = 43, 6309779678SStelian Pop .datalen = 81, 6409779678SStelian Pop .callback = atp_complete_geyser_1_2, 65703e1488SClinton Sprain .fuzz = 16, 6609779678SStelian Pop }; 6709779678SStelian Pop 6809779678SStelian Pop static const struct atp_info geyser1_info = { 6909779678SStelian Pop .xsensors = 16, 7009779678SStelian Pop .xsensors_17 = 26, 7109779678SStelian Pop .ysensors = 16, 7209779678SStelian Pop .xfact = 64, 7309779678SStelian Pop .yfact = 43, 7409779678SStelian Pop .datalen = 81, 7509779678SStelian Pop .callback = atp_complete_geyser_1_2, 76703e1488SClinton Sprain .fuzz = 16, 7709779678SStelian Pop }; 7809779678SStelian Pop 7909779678SStelian Pop static const struct atp_info geyser2_info = { 8009779678SStelian Pop .xsensors = 15, 8109779678SStelian Pop .xsensors_17 = 20, 8209779678SStelian Pop .ysensors = 9, 8309779678SStelian Pop .xfact = 64, 8409779678SStelian Pop .yfact = 43, 8509779678SStelian Pop .datalen = 64, 8609779678SStelian Pop .callback = atp_complete_geyser_1_2, 87703e1488SClinton Sprain .fuzz = 0, 8809779678SStelian Pop }; 8909779678SStelian Pop 9009779678SStelian Pop static const struct atp_info geyser3_info = { 9109779678SStelian Pop .xsensors = 20, 9209779678SStelian Pop .ysensors = 10, 9309779678SStelian Pop .xfact = 64, 9409779678SStelian Pop .yfact = 64, 9509779678SStelian Pop .datalen = 64, 9609779678SStelian Pop .callback = atp_complete_geyser_3_4, 97703e1488SClinton Sprain .fuzz = 0, 9809779678SStelian Pop }; 9909779678SStelian Pop 10009779678SStelian Pop static const struct atp_info geyser4_info = { 10109779678SStelian Pop .xsensors = 20, 10209779678SStelian Pop .ysensors = 10, 10309779678SStelian Pop .xfact = 64, 10409779678SStelian Pop .yfact = 64, 10509779678SStelian Pop .datalen = 64, 10609779678SStelian Pop .callback = atp_complete_geyser_3_4, 107703e1488SClinton Sprain .fuzz = 0, 10809779678SStelian Pop }; 10909779678SStelian Pop 11009779678SStelian Pop #define ATP_DEVICE(prod, info) \ 111e9542dffSSven Anders { \ 112b5da20f8SDmitry Torokhov .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \ 113b5da20f8SDmitry Torokhov USB_DEVICE_ID_MATCH_INT_CLASS | \ 114b5da20f8SDmitry Torokhov USB_DEVICE_ID_MATCH_INT_PROTOCOL, \ 115e9542dffSSven Anders .idVendor = 0x05ac, /* Apple */ \ 116b5da20f8SDmitry Torokhov .idProduct = (prod), \ 117b5da20f8SDmitry Torokhov .bInterfaceClass = 0x03, \ 118e9542dffSSven Anders .bInterfaceProtocol = 0x02, \ 11909779678SStelian Pop .driver_info = (unsigned long) &info, \ 120e9542dffSSven Anders } 121b5da20f8SDmitry Torokhov 122e9542dffSSven Anders /* 123e9542dffSSven Anders * Table of devices (Product IDs) that work with this driver. 124e9542dffSSven Anders * (The names come from Info.plist in AppleUSBTrackpad.kext, 125e9542dffSSven Anders * According to Info.plist Geyser IV is the same as Geyser III.) 126e9542dffSSven Anders */ 127e9542dffSSven Anders 128a45232d6SArvind Yadav static const struct usb_device_id atp_table[] = { 1297dce869fSJohannes Berg /* PowerBooks Feb 2005, iBooks G4 */ 13009779678SStelian Pop ATP_DEVICE(0x020e, fountain_info), /* FOUNTAIN ANSI */ 13109779678SStelian Pop ATP_DEVICE(0x020f, fountain_info), /* FOUNTAIN ISO */ 13209779678SStelian Pop ATP_DEVICE(0x030a, fountain_info), /* FOUNTAIN TP ONLY */ 13309779678SStelian Pop ATP_DEVICE(0x030b, geyser1_info), /* GEYSER 1 TP ONLY */ 134b5da20f8SDmitry Torokhov 135b5da20f8SDmitry Torokhov /* PowerBooks Oct 2005 */ 13609779678SStelian Pop ATP_DEVICE(0x0214, geyser2_info), /* GEYSER 2 ANSI */ 13709779678SStelian Pop ATP_DEVICE(0x0215, geyser2_info), /* GEYSER 2 ISO */ 13809779678SStelian Pop ATP_DEVICE(0x0216, geyser2_info), /* GEYSER 2 JIS */ 139b5da20f8SDmitry Torokhov 140b5da20f8SDmitry Torokhov /* Core Duo MacBook & MacBook Pro */ 14109779678SStelian Pop ATP_DEVICE(0x0217, geyser3_info), /* GEYSER 3 ANSI */ 14209779678SStelian Pop ATP_DEVICE(0x0218, geyser3_info), /* GEYSER 3 ISO */ 14309779678SStelian Pop ATP_DEVICE(0x0219, geyser3_info), /* GEYSER 3 JIS */ 144b5da20f8SDmitry Torokhov 145b5da20f8SDmitry Torokhov /* Core2 Duo MacBook & MacBook Pro */ 14609779678SStelian Pop ATP_DEVICE(0x021a, geyser4_info), /* GEYSER 4 ANSI */ 14709779678SStelian Pop ATP_DEVICE(0x021b, geyser4_info), /* GEYSER 4 ISO */ 14809779678SStelian Pop ATP_DEVICE(0x021c, geyser4_info), /* GEYSER 4 JIS */ 149b5da20f8SDmitry Torokhov 1507dce869fSJohannes Berg /* Core2 Duo MacBook3,1 */ 15109779678SStelian Pop ATP_DEVICE(0x0229, geyser4_info), /* GEYSER 4 HF ANSI */ 15209779678SStelian Pop ATP_DEVICE(0x022a, geyser4_info), /* GEYSER 4 HF ISO */ 15309779678SStelian Pop ATP_DEVICE(0x022b, geyser4_info), /* GEYSER 4 HF JIS */ 1540035a1dcSTobias Mueller 155b5da20f8SDmitry Torokhov /* Terminating entry */ 156b5da20f8SDmitry Torokhov { } 157b5da20f8SDmitry Torokhov }; 158b5da20f8SDmitry Torokhov MODULE_DEVICE_TABLE(usb, atp_table); 159b5da20f8SDmitry Torokhov 16009779678SStelian Pop /* maximum number of sensors */ 161b5da20f8SDmitry Torokhov #define ATP_XSENSORS 26 162b5da20f8SDmitry Torokhov #define ATP_YSENSORS 16 163b5da20f8SDmitry Torokhov 164739204bcSClinton Sprain /* 165739204bcSClinton Sprain * The largest possible bank of sensors with additional buffer of 4 extra values 166739204bcSClinton Sprain * on either side, for an array of smoothed sensor values. 167739204bcSClinton Sprain */ 168739204bcSClinton Sprain #define ATP_SMOOTHSIZE 34 169739204bcSClinton Sprain 170b5da20f8SDmitry Torokhov /* maximum pressure this driver will report */ 171b5da20f8SDmitry Torokhov #define ATP_PRESSURE 300 172b5da20f8SDmitry Torokhov 173b5da20f8SDmitry Torokhov /* 174b5da20f8SDmitry Torokhov * Threshold for the touchpad sensors. Any change less than ATP_THRESHOLD is 175b5da20f8SDmitry Torokhov * ignored. 176b5da20f8SDmitry Torokhov */ 177b5da20f8SDmitry Torokhov #define ATP_THRESHOLD 5 178b5da20f8SDmitry Torokhov 179739204bcSClinton Sprain /* 180739204bcSClinton Sprain * How far we'll bitshift our sensor values before averaging them. Mitigates 181739204bcSClinton Sprain * rounding errors. 182739204bcSClinton Sprain */ 183739204bcSClinton Sprain #define ATP_SCALE 12 184739204bcSClinton Sprain 1852a3e480dSDmitry Torokhov /* Geyser initialization constants */ 1862a3e480dSDmitry Torokhov #define ATP_GEYSER_MODE_READ_REQUEST_ID 1 1872a3e480dSDmitry Torokhov #define ATP_GEYSER_MODE_WRITE_REQUEST_ID 9 1882a3e480dSDmitry Torokhov #define ATP_GEYSER_MODE_REQUEST_VALUE 0x300 1892a3e480dSDmitry Torokhov #define ATP_GEYSER_MODE_REQUEST_INDEX 0 1902a3e480dSDmitry Torokhov #define ATP_GEYSER_MODE_VENDOR_VALUE 0x04 191b5da20f8SDmitry Torokhov 19282a196f4SSven Anders /** 19382a196f4SSven Anders * enum atp_status_bits - status bit meanings 19482a196f4SSven Anders * 19582a196f4SSven Anders * These constants represent the meaning of the status bits. 19682a196f4SSven Anders * (only Geyser 3/4) 19782a196f4SSven Anders * 19882a196f4SSven Anders * @ATP_STATUS_BUTTON: The button was pressed 19982a196f4SSven Anders * @ATP_STATUS_BASE_UPDATE: Update of the base values (untouched pad) 20082a196f4SSven Anders * @ATP_STATUS_FROM_RESET: Reset previously performed 20182a196f4SSven Anders */ 20282a196f4SSven Anders enum atp_status_bits { 20382a196f4SSven Anders ATP_STATUS_BUTTON = BIT(0), 20482a196f4SSven Anders ATP_STATUS_BASE_UPDATE = BIT(2), 20582a196f4SSven Anders ATP_STATUS_FROM_RESET = BIT(4), 20682a196f4SSven Anders }; 20782a196f4SSven Anders 208b5da20f8SDmitry Torokhov /* Structure to hold all of our device specific stuff */ 209b5da20f8SDmitry Torokhov struct atp { 210b5da20f8SDmitry Torokhov char phys[64]; 211b5da20f8SDmitry Torokhov struct usb_device *udev; /* usb device */ 2122d744b09SGreg Kroah-Hartman struct usb_interface *intf; /* usb interface */ 213b5da20f8SDmitry Torokhov struct urb *urb; /* usb request block */ 21482a196f4SSven Anders u8 *data; /* transferred data */ 215b5da20f8SDmitry Torokhov struct input_dev *input; /* input dev */ 21609779678SStelian Pop const struct atp_info *info; /* touchpad model */ 2177dce869fSJohannes Berg bool open; 2187dce869fSJohannes Berg bool valid; /* are the samples valid? */ 2197dce869fSJohannes Berg bool size_detect_done; 2207dce869fSJohannes Berg bool overflow_warned; 2217bbdba56SClinton Sprain int fingers_old; /* last reported finger count */ 222b5da20f8SDmitry Torokhov int x_old; /* last reported x/y, */ 223b5da20f8SDmitry Torokhov int y_old; /* used for smoothing */ 2246e49c1a4SBenjamin Herrenschmidt signed char xy_cur[ATP_XSENSORS + ATP_YSENSORS]; 2256e49c1a4SBenjamin Herrenschmidt signed char xy_old[ATP_XSENSORS + ATP_YSENSORS]; 226b5da20f8SDmitry Torokhov int xy_acc[ATP_XSENSORS + ATP_YSENSORS]; 227739204bcSClinton Sprain int smooth[ATP_SMOOTHSIZE]; 228739204bcSClinton Sprain int smooth_tmp[ATP_SMOOTHSIZE]; 2295a6eb676SSoeren Sonnenburg int idlecount; /* number of empty packets */ 2305a6eb676SSoeren Sonnenburg struct work_struct work; 231b5da20f8SDmitry Torokhov }; 232b5da20f8SDmitry Torokhov 233b5da20f8SDmitry Torokhov #define dbg_dump(msg, tab) \ 234b5da20f8SDmitry Torokhov if (debug > 1) { \ 2357dce869fSJohannes Berg int __i; \ 2367dce869fSJohannes Berg printk(KERN_DEBUG "appletouch: %s", msg); \ 2377dce869fSJohannes Berg for (__i = 0; __i < ATP_XSENSORS + ATP_YSENSORS; __i++) \ 2387dce869fSJohannes Berg printk(" %02x", tab[__i]); \ 239b5da20f8SDmitry Torokhov printk("\n"); \ 240b5da20f8SDmitry Torokhov } 241b5da20f8SDmitry Torokhov 242b5da20f8SDmitry Torokhov #define dprintk(format, a...) \ 243b5da20f8SDmitry Torokhov do { \ 2447dce869fSJohannes Berg if (debug) \ 2457dce869fSJohannes Berg printk(KERN_DEBUG format, ##a); \ 246b5da20f8SDmitry Torokhov } while (0) 247b5da20f8SDmitry Torokhov 2487dce869fSJohannes Berg MODULE_AUTHOR("Johannes Berg"); 2497dce869fSJohannes Berg MODULE_AUTHOR("Stelian Pop"); 2507dce869fSJohannes Berg MODULE_AUTHOR("Frank Arnold"); 2517dce869fSJohannes Berg MODULE_AUTHOR("Michael Hanselmann"); 2527dce869fSJohannes Berg MODULE_AUTHOR("Sven Anders"); 2537dce869fSJohannes Berg MODULE_DESCRIPTION("Apple PowerBook and MacBook USB touchpad driver"); 254b5da20f8SDmitry Torokhov MODULE_LICENSE("GPL"); 255b5da20f8SDmitry Torokhov 256b5da20f8SDmitry Torokhov /* 257b5da20f8SDmitry Torokhov * Make the threshold a module parameter 258b5da20f8SDmitry Torokhov */ 259b5da20f8SDmitry Torokhov static int threshold = ATP_THRESHOLD; 260b5da20f8SDmitry Torokhov module_param(threshold, int, 0644); 2617dce869fSJohannes Berg MODULE_PARM_DESC(threshold, "Discard any change in data from a sensor" 2627dce869fSJohannes Berg " (the trackpad has many of these sensors)" 2637dce869fSJohannes Berg " less than this value."); 264b5da20f8SDmitry Torokhov 2657dce869fSJohannes Berg static int debug; 266b5da20f8SDmitry Torokhov module_param(debug, int, 0644); 267b5da20f8SDmitry Torokhov MODULE_PARM_DESC(debug, "Activate debugging output"); 268b5da20f8SDmitry Torokhov 2695a6eb676SSoeren Sonnenburg /* 2702a3e480dSDmitry Torokhov * By default newer Geyser devices send standard USB HID mouse 2715a6eb676SSoeren Sonnenburg * packets (Report ID 2). This code changes device mode, so it 2725a6eb676SSoeren Sonnenburg * sends raw sensor reports (Report ID 5). 2735a6eb676SSoeren Sonnenburg */ 27480f8594fSGreg Kroah-Hartman static int atp_geyser_init(struct atp *dev) 2755a6eb676SSoeren Sonnenburg { 27680f8594fSGreg Kroah-Hartman struct usb_device *udev = dev->udev; 2770385c5eeSBob Copeland char *data; 2785a6eb676SSoeren Sonnenburg int size; 2797dce869fSJohannes Berg int i; 2800385c5eeSBob Copeland int ret; 2810385c5eeSBob Copeland 2820385c5eeSBob Copeland data = kmalloc(8, GFP_KERNEL); 2830385c5eeSBob Copeland if (!data) { 2842d744b09SGreg Kroah-Hartman dev_err(&dev->intf->dev, "Out of memory\n"); 2850385c5eeSBob Copeland return -ENOMEM; 2860385c5eeSBob Copeland } 2875a6eb676SSoeren Sonnenburg 2885a6eb676SSoeren Sonnenburg size = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 2892a3e480dSDmitry Torokhov ATP_GEYSER_MODE_READ_REQUEST_ID, 2905a6eb676SSoeren Sonnenburg USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE, 2912a3e480dSDmitry Torokhov ATP_GEYSER_MODE_REQUEST_VALUE, 2920385c5eeSBob Copeland ATP_GEYSER_MODE_REQUEST_INDEX, data, 8, 5000); 2935a6eb676SSoeren Sonnenburg 2945a6eb676SSoeren Sonnenburg if (size != 8) { 2957dce869fSJohannes Berg dprintk("atp_geyser_init: read error\n"); 2967dce869fSJohannes Berg for (i = 0; i < 8; i++) 2977dce869fSJohannes Berg dprintk("appletouch[%d]: %d\n", i, data[i]); 2987dce869fSJohannes Berg 2992d744b09SGreg Kroah-Hartman dev_err(&dev->intf->dev, "Failed to read mode from device.\n"); 3000385c5eeSBob Copeland ret = -EIO; 3010385c5eeSBob Copeland goto out_free; 3025a6eb676SSoeren Sonnenburg } 3035a6eb676SSoeren Sonnenburg 3045a6eb676SSoeren Sonnenburg /* Apply the mode switch */ 3052a3e480dSDmitry Torokhov data[0] = ATP_GEYSER_MODE_VENDOR_VALUE; 3065a6eb676SSoeren Sonnenburg 3075a6eb676SSoeren Sonnenburg size = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 3082a3e480dSDmitry Torokhov ATP_GEYSER_MODE_WRITE_REQUEST_ID, 3095a6eb676SSoeren Sonnenburg USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE, 3102a3e480dSDmitry Torokhov ATP_GEYSER_MODE_REQUEST_VALUE, 3110385c5eeSBob Copeland ATP_GEYSER_MODE_REQUEST_INDEX, data, 8, 5000); 3125a6eb676SSoeren Sonnenburg 3135a6eb676SSoeren Sonnenburg if (size != 8) { 3147dce869fSJohannes Berg dprintk("atp_geyser_init: write error\n"); 3157dce869fSJohannes Berg for (i = 0; i < 8; i++) 3167dce869fSJohannes Berg dprintk("appletouch[%d]: %d\n", i, data[i]); 3177dce869fSJohannes Berg 3182d744b09SGreg Kroah-Hartman dev_err(&dev->intf->dev, "Failed to request geyser raw mode\n"); 3190385c5eeSBob Copeland ret = -EIO; 3200385c5eeSBob Copeland goto out_free; 3215a6eb676SSoeren Sonnenburg } 3220385c5eeSBob Copeland ret = 0; 3230385c5eeSBob Copeland out_free: 3240385c5eeSBob Copeland kfree(data); 3250385c5eeSBob Copeland return ret; 3265a6eb676SSoeren Sonnenburg } 3275a6eb676SSoeren Sonnenburg 3282a3e480dSDmitry Torokhov /* 3292a3e480dSDmitry Torokhov * Reinitialise the device. This usually stops stream of empty packets 3302a3e480dSDmitry Torokhov * coming from it. 3312a3e480dSDmitry Torokhov */ 3325a6eb676SSoeren Sonnenburg static void atp_reinit(struct work_struct *work) 3335a6eb676SSoeren Sonnenburg { 3345a6eb676SSoeren Sonnenburg struct atp *dev = container_of(work, struct atp, work); 3352a3e480dSDmitry Torokhov int retval; 3365a6eb676SSoeren Sonnenburg 3377dce869fSJohannes Berg dprintk("appletouch: putting appletouch to sleep (reinit)\n"); 33880f8594fSGreg Kroah-Hartman atp_geyser_init(dev); 3392a3e480dSDmitry Torokhov 3402a3e480dSDmitry Torokhov retval = usb_submit_urb(dev->urb, GFP_ATOMIC); 3417dce869fSJohannes Berg if (retval) 3422d744b09SGreg Kroah-Hartman dev_err(&dev->intf->dev, 3439c113dc6SGreg Kroah-Hartman "atp_reinit: usb_submit_urb failed with error %d\n", 3447dce869fSJohannes Berg retval); 3455a6eb676SSoeren Sonnenburg } 3465a6eb676SSoeren Sonnenburg 347739204bcSClinton Sprain static int atp_calculate_abs(struct atp *dev, int offset, int nb_sensors, 348739204bcSClinton Sprain int fact, int *z, int *fingers) 349b5da20f8SDmitry Torokhov { 350739204bcSClinton Sprain int i, pass; 351739204bcSClinton Sprain 352739204bcSClinton Sprain /* 353739204bcSClinton Sprain * Use offset to point xy_sensors at the first value in dev->xy_acc 354739204bcSClinton Sprain * for whichever dimension we're looking at this particular go-round. 355739204bcSClinton Sprain */ 356739204bcSClinton Sprain int *xy_sensors = dev->xy_acc + offset; 357739204bcSClinton Sprain 358b5da20f8SDmitry Torokhov /* values to calculate mean */ 359b5da20f8SDmitry Torokhov int pcum = 0, psum = 0; 360b5da20f8SDmitry Torokhov int is_increasing = 0; 361b5da20f8SDmitry Torokhov 362b5da20f8SDmitry Torokhov *fingers = 0; 363b5da20f8SDmitry Torokhov 364b5da20f8SDmitry Torokhov for (i = 0; i < nb_sensors; i++) { 365b5da20f8SDmitry Torokhov if (xy_sensors[i] < threshold) { 366b5da20f8SDmitry Torokhov if (is_increasing) 367b5da20f8SDmitry Torokhov is_increasing = 0; 368b5da20f8SDmitry Torokhov 369b5da20f8SDmitry Torokhov /* 370b5da20f8SDmitry Torokhov * Makes the finger detection more versatile. For example, 371b5da20f8SDmitry Torokhov * two fingers with no gap will be detected. Also, my 372b5da20f8SDmitry Torokhov * tests show it less likely to have intermittent loss 373b5da20f8SDmitry Torokhov * of multiple finger readings while moving around (scrolling). 374b5da20f8SDmitry Torokhov * 375b5da20f8SDmitry Torokhov * Changes the multiple finger detection to counting humps on 376b5da20f8SDmitry Torokhov * sensors (transitions from nonincreasing to increasing) 377b5da20f8SDmitry Torokhov * instead of counting transitions from low sensors (no 378b5da20f8SDmitry Torokhov * finger reading) to high sensors (finger above 379b5da20f8SDmitry Torokhov * sensor) 380b5da20f8SDmitry Torokhov * 381b5da20f8SDmitry Torokhov * - Jason Parekh <jasonparekh@gmail.com> 382b5da20f8SDmitry Torokhov */ 383739204bcSClinton Sprain 384739204bcSClinton Sprain } else if (i < 1 || 3857dce869fSJohannes Berg (!is_increasing && xy_sensors[i - 1] < xy_sensors[i])) { 386b5da20f8SDmitry Torokhov (*fingers)++; 387b5da20f8SDmitry Torokhov is_increasing = 1; 38805e882f8SJeremy Huddleston } else if (i > 0 && (xy_sensors[i - 1] - xy_sensors[i] > threshold)) { 389b5da20f8SDmitry Torokhov is_increasing = 0; 390b5da20f8SDmitry Torokhov } 391739204bcSClinton Sprain } 392739204bcSClinton Sprain 393739204bcSClinton Sprain if (*fingers < 1) /* No need to continue if no fingers are found. */ 394739204bcSClinton Sprain return 0; 395b5da20f8SDmitry Torokhov 396b5da20f8SDmitry Torokhov /* 397739204bcSClinton Sprain * Use a smoothed version of sensor data for movement calculations, to 398739204bcSClinton Sprain * combat noise without needing to rely so heavily on a threshold. 399739204bcSClinton Sprain * This improves tracking. 400739204bcSClinton Sprain * 401739204bcSClinton Sprain * The smoothed array is bigger than the original so that the smoothing 402739204bcSClinton Sprain * doesn't result in edge values being truncated. 403b5da20f8SDmitry Torokhov */ 404739204bcSClinton Sprain 405739204bcSClinton Sprain memset(dev->smooth, 0, 4 * sizeof(dev->smooth[0])); 406739204bcSClinton Sprain /* Pull base values, scaled up to help avoid truncation errors. */ 407739204bcSClinton Sprain for (i = 0; i < nb_sensors; i++) 408739204bcSClinton Sprain dev->smooth[i + 4] = xy_sensors[i] << ATP_SCALE; 409739204bcSClinton Sprain memset(&dev->smooth[nb_sensors + 4], 0, 4 * sizeof(dev->smooth[0])); 410739204bcSClinton Sprain 411739204bcSClinton Sprain for (pass = 0; pass < 4; pass++) { 412739204bcSClinton Sprain /* Handle edge. */ 413739204bcSClinton Sprain dev->smooth_tmp[0] = (dev->smooth[0] + dev->smooth[1]) / 2; 414739204bcSClinton Sprain 415739204bcSClinton Sprain /* Average values with neighbors. */ 416739204bcSClinton Sprain for (i = 1; i < nb_sensors + 7; i++) 417739204bcSClinton Sprain dev->smooth_tmp[i] = (dev->smooth[i - 1] + 418739204bcSClinton Sprain dev->smooth[i] * 2 + 419739204bcSClinton Sprain dev->smooth[i + 1]) / 4; 420739204bcSClinton Sprain 421739204bcSClinton Sprain /* Handle other edge. */ 422739204bcSClinton Sprain dev->smooth_tmp[i] = (dev->smooth[i - 1] + dev->smooth[i]) / 2; 423739204bcSClinton Sprain 424739204bcSClinton Sprain memcpy(dev->smooth, dev->smooth_tmp, sizeof(dev->smooth)); 425739204bcSClinton Sprain } 426739204bcSClinton Sprain 427739204bcSClinton Sprain for (i = 0; i < nb_sensors + 8; i++) { 428739204bcSClinton Sprain /* 429739204bcSClinton Sprain * Skip values if they're small enough to be truncated to 0 430739204bcSClinton Sprain * by scale. Mostly noise. 431739204bcSClinton Sprain */ 432739204bcSClinton Sprain if ((dev->smooth[i] >> ATP_SCALE) > 0) { 433739204bcSClinton Sprain pcum += dev->smooth[i] * i; 434739204bcSClinton Sprain psum += dev->smooth[i]; 435739204bcSClinton Sprain } 436b5da20f8SDmitry Torokhov } 437b5da20f8SDmitry Torokhov 438b5da20f8SDmitry Torokhov if (psum > 0) { 439739204bcSClinton Sprain *z = psum >> ATP_SCALE; /* Scale down pressure output. */ 440b5da20f8SDmitry Torokhov return pcum * fact / psum; 441b5da20f8SDmitry Torokhov } 442b5da20f8SDmitry Torokhov 443b5da20f8SDmitry Torokhov return 0; 444b5da20f8SDmitry Torokhov } 445b5da20f8SDmitry Torokhov 446b5da20f8SDmitry Torokhov static inline void atp_report_fingers(struct input_dev *input, int fingers) 447b5da20f8SDmitry Torokhov { 448b5da20f8SDmitry Torokhov input_report_key(input, BTN_TOOL_FINGER, fingers == 1); 449b5da20f8SDmitry Torokhov input_report_key(input, BTN_TOOL_DOUBLETAP, fingers == 2); 450b5da20f8SDmitry Torokhov input_report_key(input, BTN_TOOL_TRIPLETAP, fingers > 2); 451b5da20f8SDmitry Torokhov } 452b5da20f8SDmitry Torokhov 453d83d213dSSven Anders /* Check URB status and for correct length of data package */ 454d83d213dSSven Anders 455d83d213dSSven Anders #define ATP_URB_STATUS_SUCCESS 0 456d83d213dSSven Anders #define ATP_URB_STATUS_ERROR 1 457d83d213dSSven Anders #define ATP_URB_STATUS_ERROR_FATAL 2 458d83d213dSSven Anders 459d83d213dSSven Anders static int atp_status_check(struct urb *urb) 460b5da20f8SDmitry Torokhov { 461b5da20f8SDmitry Torokhov struct atp *dev = urb->context; 4622d744b09SGreg Kroah-Hartman struct usb_interface *intf = dev->intf; 463b5da20f8SDmitry Torokhov 464b5da20f8SDmitry Torokhov switch (urb->status) { 465b5da20f8SDmitry Torokhov case 0: 466b5da20f8SDmitry Torokhov /* success */ 467b5da20f8SDmitry Torokhov break; 468b5da20f8SDmitry Torokhov case -EOVERFLOW: 4697dce869fSJohannes Berg if (!dev->overflow_warned) { 4702d744b09SGreg Kroah-Hartman dev_warn(&intf->dev, 47167946d13SGreg Kroah-Hartman "appletouch: OVERFLOW with data length %d, actual length is %d\n", 47209779678SStelian Pop dev->info->datalen, dev->urb->actual_length); 4737dce869fSJohannes Berg dev->overflow_warned = true; 474b5da20f8SDmitry Torokhov } 475b5da20f8SDmitry Torokhov case -ECONNRESET: 476b5da20f8SDmitry Torokhov case -ENOENT: 477b5da20f8SDmitry Torokhov case -ESHUTDOWN: 478b5da20f8SDmitry Torokhov /* This urb is terminated, clean up */ 4792d744b09SGreg Kroah-Hartman dev_dbg(&intf->dev, 48067946d13SGreg Kroah-Hartman "atp_complete: urb shutting down with status: %d\n", 4817dce869fSJohannes Berg urb->status); 482d83d213dSSven Anders return ATP_URB_STATUS_ERROR_FATAL; 483d83d213dSSven Anders 484b5da20f8SDmitry Torokhov default: 4852d744b09SGreg Kroah-Hartman dev_dbg(&intf->dev, 48667946d13SGreg Kroah-Hartman "atp_complete: nonzero urb status received: %d\n", 4877dce869fSJohannes Berg urb->status); 488d83d213dSSven Anders return ATP_URB_STATUS_ERROR; 489b5da20f8SDmitry Torokhov } 490b5da20f8SDmitry Torokhov 491b5da20f8SDmitry Torokhov /* drop incomplete datasets */ 49209779678SStelian Pop if (dev->urb->actual_length != dev->info->datalen) { 493b5da20f8SDmitry Torokhov dprintk("appletouch: incomplete data package" 494b5da20f8SDmitry Torokhov " (first byte: %d, length: %d).\n", 495b5da20f8SDmitry Torokhov dev->data[0], dev->urb->actual_length); 496d83d213dSSven Anders return ATP_URB_STATUS_ERROR; 497b5da20f8SDmitry Torokhov } 498b5da20f8SDmitry Torokhov 499d83d213dSSven Anders return ATP_URB_STATUS_SUCCESS; 500d83d213dSSven Anders } 501b5da20f8SDmitry Torokhov 50209779678SStelian Pop static void atp_detect_size(struct atp *dev) 50309779678SStelian Pop { 50409779678SStelian Pop int i; 50509779678SStelian Pop 50609779678SStelian Pop /* 17" Powerbooks have extra X sensors */ 50709779678SStelian Pop for (i = dev->info->xsensors; i < ATP_XSENSORS; i++) { 50809779678SStelian Pop if (dev->xy_cur[i]) { 50909779678SStelian Pop 5102d744b09SGreg Kroah-Hartman dev_info(&dev->intf->dev, 51167946d13SGreg Kroah-Hartman "appletouch: 17\" model detected.\n"); 51209779678SStelian Pop 51309779678SStelian Pop input_set_abs_params(dev->input, ABS_X, 0, 51409779678SStelian Pop (dev->info->xsensors_17 - 1) * 51509779678SStelian Pop dev->info->xfact - 1, 516703e1488SClinton Sprain dev->info->fuzz, 0); 51709779678SStelian Pop break; 51809779678SStelian Pop } 51909779678SStelian Pop } 52009779678SStelian Pop } 52109779678SStelian Pop 522b5da20f8SDmitry Torokhov /* 523d83d213dSSven Anders * USB interrupt callback functions 524b5da20f8SDmitry Torokhov */ 525b5da20f8SDmitry Torokhov 526d83d213dSSven Anders /* Interrupt function for older touchpads: FOUNTAIN/GEYSER1/GEYSER2 */ 527d83d213dSSven Anders 528d83d213dSSven Anders static void atp_complete_geyser_1_2(struct urb *urb) 529d83d213dSSven Anders { 530d83d213dSSven Anders int x, y, x_z, y_z, x_f, y_f; 531d83d213dSSven Anders int retval, i, j; 5327bbdba56SClinton Sprain int key, fingers; 533d83d213dSSven Anders struct atp *dev = urb->context; 534d83d213dSSven Anders int status = atp_status_check(urb); 535d83d213dSSven Anders 536d83d213dSSven Anders if (status == ATP_URB_STATUS_ERROR_FATAL) 537d83d213dSSven Anders return; 538d83d213dSSven Anders else if (status == ATP_URB_STATUS_ERROR) 539d83d213dSSven Anders goto exit; 540d83d213dSSven Anders 541d83d213dSSven Anders /* reorder the sensors values */ 54209779678SStelian Pop if (dev->info == &geyser2_info) { 543b5da20f8SDmitry Torokhov memset(dev->xy_cur, 0, sizeof(dev->xy_cur)); 544b5da20f8SDmitry Torokhov 545b5da20f8SDmitry Torokhov /* 546b5da20f8SDmitry Torokhov * The values are laid out like this: 547b5da20f8SDmitry Torokhov * Y1, Y2, -, Y3, Y4, -, ..., X1, X2, -, X3, X4, -, ... 548b5da20f8SDmitry Torokhov * '-' is an unused value. 549b5da20f8SDmitry Torokhov */ 550b5da20f8SDmitry Torokhov 551b5da20f8SDmitry Torokhov /* read X values */ 552b5da20f8SDmitry Torokhov for (i = 0, j = 19; i < 20; i += 2, j += 3) { 553b5da20f8SDmitry Torokhov dev->xy_cur[i] = dev->data[j]; 554b5da20f8SDmitry Torokhov dev->xy_cur[i + 1] = dev->data[j + 1]; 555b5da20f8SDmitry Torokhov } 556b5da20f8SDmitry Torokhov 557b5da20f8SDmitry Torokhov /* read Y values */ 558b5da20f8SDmitry Torokhov for (i = 0, j = 1; i < 9; i += 2, j += 3) { 559b5da20f8SDmitry Torokhov dev->xy_cur[ATP_XSENSORS + i] = dev->data[j]; 560b5da20f8SDmitry Torokhov dev->xy_cur[ATP_XSENSORS + i + 1] = dev->data[j + 1]; 561b5da20f8SDmitry Torokhov } 562b5da20f8SDmitry Torokhov } else { 563b5da20f8SDmitry Torokhov for (i = 0; i < 8; i++) { 564b5da20f8SDmitry Torokhov /* X values */ 5657dce869fSJohannes Berg dev->xy_cur[i + 0] = dev->data[5 * i + 2]; 566b5da20f8SDmitry Torokhov dev->xy_cur[i + 8] = dev->data[5 * i + 4]; 567b5da20f8SDmitry Torokhov dev->xy_cur[i + 16] = dev->data[5 * i + 42]; 568b5da20f8SDmitry Torokhov if (i < 2) 569b5da20f8SDmitry Torokhov dev->xy_cur[i + 24] = dev->data[5 * i + 44]; 570b5da20f8SDmitry Torokhov 571b5da20f8SDmitry Torokhov /* Y values */ 57209779678SStelian Pop dev->xy_cur[ATP_XSENSORS + i] = dev->data[5 * i + 1]; 57309779678SStelian Pop dev->xy_cur[ATP_XSENSORS + i + 8] = dev->data[5 * i + 3]; 574b5da20f8SDmitry Torokhov } 575b5da20f8SDmitry Torokhov } 576b5da20f8SDmitry Torokhov 577b5da20f8SDmitry Torokhov dbg_dump("sample", dev->xy_cur); 578b5da20f8SDmitry Torokhov 579b5da20f8SDmitry Torokhov if (!dev->valid) { 580b5da20f8SDmitry Torokhov /* first sample */ 5817dce869fSJohannes Berg dev->valid = true; 582b5da20f8SDmitry Torokhov dev->x_old = dev->y_old = -1; 583d83d213dSSven Anders 584d83d213dSSven Anders /* Store first sample */ 585b5da20f8SDmitry Torokhov memcpy(dev->xy_old, dev->xy_cur, sizeof(dev->xy_old)); 586b5da20f8SDmitry Torokhov 587d83d213dSSven Anders /* Perform size detection, if not done already */ 58809779678SStelian Pop if (unlikely(!dev->size_detect_done)) { 58909779678SStelian Pop atp_detect_size(dev); 59070851233SGustavo A. R. Silva dev->size_detect_done = true; 591b5da20f8SDmitry Torokhov goto exit; 592b5da20f8SDmitry Torokhov } 593d83d213dSSven Anders } 594d83d213dSSven Anders 595d83d213dSSven Anders for (i = 0; i < ATP_XSENSORS + ATP_YSENSORS; i++) { 596d83d213dSSven Anders /* accumulate the change */ 5976e49c1a4SBenjamin Herrenschmidt signed char change = dev->xy_old[i] - dev->xy_cur[i]; 598d83d213dSSven Anders dev->xy_acc[i] -= change; 599d83d213dSSven Anders 600d83d213dSSven Anders /* prevent down drifting */ 601d83d213dSSven Anders if (dev->xy_acc[i] < 0) 602d83d213dSSven Anders dev->xy_acc[i] = 0; 603d83d213dSSven Anders } 604d83d213dSSven Anders 605d83d213dSSven Anders memcpy(dev->xy_old, dev->xy_cur, sizeof(dev->xy_old)); 606d83d213dSSven Anders 607d83d213dSSven Anders dbg_dump("accumulator", dev->xy_acc); 608d83d213dSSven Anders 609739204bcSClinton Sprain x = atp_calculate_abs(dev, 0, ATP_XSENSORS, 61009779678SStelian Pop dev->info->xfact, &x_z, &x_f); 611739204bcSClinton Sprain y = atp_calculate_abs(dev, ATP_XSENSORS, ATP_YSENSORS, 61209779678SStelian Pop dev->info->yfact, &y_z, &y_f); 61309779678SStelian Pop key = dev->data[dev->info->datalen - 1] & ATP_STATUS_BUTTON; 614d83d213dSSven Anders 6157bbdba56SClinton Sprain fingers = max(x_f, y_f); 6167bbdba56SClinton Sprain 6177bbdba56SClinton Sprain if (x && y && fingers == dev->fingers_old) { 618d83d213dSSven Anders if (dev->x_old != -1) { 619739204bcSClinton Sprain x = (dev->x_old * 7 + x) >> 3; 620739204bcSClinton Sprain y = (dev->y_old * 7 + y) >> 3; 621d83d213dSSven Anders dev->x_old = x; 622d83d213dSSven Anders dev->y_old = y; 623d83d213dSSven Anders 624d83d213dSSven Anders if (debug > 1) 625d83d213dSSven Anders printk(KERN_DEBUG "appletouch: " 626d83d213dSSven Anders "X: %3d Y: %3d Xz: %3d Yz: %3d\n", 627d83d213dSSven Anders x, y, x_z, y_z); 628d83d213dSSven Anders 629d83d213dSSven Anders input_report_key(dev->input, BTN_TOUCH, 1); 630d83d213dSSven Anders input_report_abs(dev->input, ABS_X, x); 631d83d213dSSven Anders input_report_abs(dev->input, ABS_Y, y); 632d83d213dSSven Anders input_report_abs(dev->input, ABS_PRESSURE, 633d83d213dSSven Anders min(ATP_PRESSURE, x_z + y_z)); 6347bbdba56SClinton Sprain atp_report_fingers(dev->input, fingers); 635d83d213dSSven Anders } 636d83d213dSSven Anders dev->x_old = x; 637d83d213dSSven Anders dev->y_old = y; 638d83d213dSSven Anders 639d83d213dSSven Anders } else if (!x && !y) { 640d83d213dSSven Anders 641d83d213dSSven Anders dev->x_old = dev->y_old = -1; 6427bbdba56SClinton Sprain dev->fingers_old = 0; 643d83d213dSSven Anders input_report_key(dev->input, BTN_TOUCH, 0); 644d83d213dSSven Anders input_report_abs(dev->input, ABS_PRESSURE, 0); 645d83d213dSSven Anders atp_report_fingers(dev->input, 0); 646d83d213dSSven Anders 647d83d213dSSven Anders /* reset the accumulator on release */ 648d83d213dSSven Anders memset(dev->xy_acc, 0, sizeof(dev->xy_acc)); 649d83d213dSSven Anders } 650d83d213dSSven Anders 6517bbdba56SClinton Sprain if (fingers != dev->fingers_old) 6527bbdba56SClinton Sprain dev->x_old = dev->y_old = -1; 6537bbdba56SClinton Sprain dev->fingers_old = fingers; 6547bbdba56SClinton Sprain 655d83d213dSSven Anders input_report_key(dev->input, BTN_LEFT, key); 656d83d213dSSven Anders input_sync(dev->input); 657d83d213dSSven Anders 658d83d213dSSven Anders exit: 659d83d213dSSven Anders retval = usb_submit_urb(dev->urb, GFP_ATOMIC); 660d83d213dSSven Anders if (retval) 6612d744b09SGreg Kroah-Hartman dev_err(&dev->intf->dev, 6629c113dc6SGreg Kroah-Hartman "atp_complete: usb_submit_urb failed with result %d\n", 663d83d213dSSven Anders retval); 664d83d213dSSven Anders } 665d83d213dSSven Anders 666d83d213dSSven Anders /* Interrupt function for older touchpads: GEYSER3/GEYSER4 */ 667d83d213dSSven Anders 668d83d213dSSven Anders static void atp_complete_geyser_3_4(struct urb *urb) 669d83d213dSSven Anders { 670d83d213dSSven Anders int x, y, x_z, y_z, x_f, y_f; 671d83d213dSSven Anders int retval, i, j; 6727bbdba56SClinton Sprain int key, fingers; 673d83d213dSSven Anders struct atp *dev = urb->context; 674d83d213dSSven Anders int status = atp_status_check(urb); 675d83d213dSSven Anders 676d83d213dSSven Anders if (status == ATP_URB_STATUS_ERROR_FATAL) 677d83d213dSSven Anders return; 678d83d213dSSven Anders else if (status == ATP_URB_STATUS_ERROR) 679d83d213dSSven Anders goto exit; 680d83d213dSSven Anders 681d83d213dSSven Anders /* Reorder the sensors values: 682d83d213dSSven Anders * 683d83d213dSSven Anders * The values are laid out like this: 684d83d213dSSven Anders * -, Y1, Y2, -, Y3, Y4, -, ..., -, X1, X2, -, X3, X4, ... 685d83d213dSSven Anders * '-' is an unused value. 686d83d213dSSven Anders */ 687d83d213dSSven Anders 688d83d213dSSven Anders /* read X values */ 689d83d213dSSven Anders for (i = 0, j = 19; i < 20; i += 2, j += 3) { 690d83d213dSSven Anders dev->xy_cur[i] = dev->data[j + 1]; 691d83d213dSSven Anders dev->xy_cur[i + 1] = dev->data[j + 2]; 692d83d213dSSven Anders } 693d83d213dSSven Anders /* read Y values */ 694d83d213dSSven Anders for (i = 0, j = 1; i < 9; i += 2, j += 3) { 695d83d213dSSven Anders dev->xy_cur[ATP_XSENSORS + i] = dev->data[j + 1]; 696d83d213dSSven Anders dev->xy_cur[ATP_XSENSORS + i + 1] = dev->data[j + 2]; 697d83d213dSSven Anders } 698d83d213dSSven Anders 699d83d213dSSven Anders dbg_dump("sample", dev->xy_cur); 700d83d213dSSven Anders 70182a196f4SSven Anders /* Just update the base values (i.e. touchpad in untouched state) */ 70209779678SStelian Pop if (dev->data[dev->info->datalen - 1] & ATP_STATUS_BASE_UPDATE) { 703d83d213dSSven Anders 704d745b532SJoe Perches dprintk("appletouch: updated base values\n"); 70582a196f4SSven Anders 70682a196f4SSven Anders memcpy(dev->xy_old, dev->xy_cur, sizeof(dev->xy_old)); 707d83d213dSSven Anders goto exit; 708d83d213dSSven Anders } 709b5da20f8SDmitry Torokhov 710b5da20f8SDmitry Torokhov for (i = 0; i < ATP_XSENSORS + ATP_YSENSORS; i++) { 71182a196f4SSven Anders /* calculate the change */ 71282a196f4SSven Anders dev->xy_acc[i] = dev->xy_cur[i] - dev->xy_old[i]; 71382a196f4SSven Anders 71482a196f4SSven Anders /* this is a round-robin value, so couple with that */ 71582a196f4SSven Anders if (dev->xy_acc[i] > 127) 71682a196f4SSven Anders dev->xy_acc[i] -= 256; 71782a196f4SSven Anders 71882a196f4SSven Anders if (dev->xy_acc[i] < -127) 71982a196f4SSven Anders dev->xy_acc[i] += 256; 720b5da20f8SDmitry Torokhov 721b5da20f8SDmitry Torokhov /* prevent down drifting */ 722b5da20f8SDmitry Torokhov if (dev->xy_acc[i] < 0) 723b5da20f8SDmitry Torokhov dev->xy_acc[i] = 0; 724b5da20f8SDmitry Torokhov } 725b5da20f8SDmitry Torokhov 726b5da20f8SDmitry Torokhov dbg_dump("accumulator", dev->xy_acc); 727b5da20f8SDmitry Torokhov 728739204bcSClinton Sprain x = atp_calculate_abs(dev, 0, ATP_XSENSORS, 72909779678SStelian Pop dev->info->xfact, &x_z, &x_f); 730739204bcSClinton Sprain y = atp_calculate_abs(dev, ATP_XSENSORS, ATP_YSENSORS, 73109779678SStelian Pop dev->info->yfact, &y_z, &y_f); 732739204bcSClinton Sprain 73309779678SStelian Pop key = dev->data[dev->info->datalen - 1] & ATP_STATUS_BUTTON; 734b5da20f8SDmitry Torokhov 7357bbdba56SClinton Sprain fingers = max(x_f, y_f); 7367bbdba56SClinton Sprain 7377bbdba56SClinton Sprain if (x && y && fingers == dev->fingers_old) { 738b5da20f8SDmitry Torokhov if (dev->x_old != -1) { 739739204bcSClinton Sprain x = (dev->x_old * 7 + x) >> 3; 740739204bcSClinton Sprain y = (dev->y_old * 7 + y) >> 3; 741b5da20f8SDmitry Torokhov dev->x_old = x; 742b5da20f8SDmitry Torokhov dev->y_old = y; 743b5da20f8SDmitry Torokhov 744b5da20f8SDmitry Torokhov if (debug > 1) 7452a3e480dSDmitry Torokhov printk(KERN_DEBUG "appletouch: X: %3d Y: %3d " 746b5da20f8SDmitry Torokhov "Xz: %3d Yz: %3d\n", 747b5da20f8SDmitry Torokhov x, y, x_z, y_z); 748b5da20f8SDmitry Torokhov 749b5da20f8SDmitry Torokhov input_report_key(dev->input, BTN_TOUCH, 1); 750b5da20f8SDmitry Torokhov input_report_abs(dev->input, ABS_X, x); 751b5da20f8SDmitry Torokhov input_report_abs(dev->input, ABS_Y, y); 752b5da20f8SDmitry Torokhov input_report_abs(dev->input, ABS_PRESSURE, 753b5da20f8SDmitry Torokhov min(ATP_PRESSURE, x_z + y_z)); 7547bbdba56SClinton Sprain atp_report_fingers(dev->input, fingers); 755b5da20f8SDmitry Torokhov } 756b5da20f8SDmitry Torokhov dev->x_old = x; 757b5da20f8SDmitry Torokhov dev->y_old = y; 7585a6eb676SSoeren Sonnenburg 7595a6eb676SSoeren Sonnenburg } else if (!x && !y) { 760b5da20f8SDmitry Torokhov 761b5da20f8SDmitry Torokhov dev->x_old = dev->y_old = -1; 7627bbdba56SClinton Sprain dev->fingers_old = 0; 763b5da20f8SDmitry Torokhov input_report_key(dev->input, BTN_TOUCH, 0); 764b5da20f8SDmitry Torokhov input_report_abs(dev->input, ABS_PRESSURE, 0); 765b5da20f8SDmitry Torokhov atp_report_fingers(dev->input, 0); 766b5da20f8SDmitry Torokhov 767b5da20f8SDmitry Torokhov /* reset the accumulator on release */ 768b5da20f8SDmitry Torokhov memset(dev->xy_acc, 0, sizeof(dev->xy_acc)); 769937ad5c1SSoeren Sonnenburg } 7705a6eb676SSoeren Sonnenburg 7717bbdba56SClinton Sprain if (fingers != dev->fingers_old) 7727bbdba56SClinton Sprain dev->x_old = dev->y_old = -1; 7737bbdba56SClinton Sprain dev->fingers_old = fingers; 7747bbdba56SClinton Sprain 77546249ea6SAnton Ekblad input_report_key(dev->input, BTN_LEFT, key); 77646249ea6SAnton Ekblad input_sync(dev->input); 77746249ea6SAnton Ekblad 7782a3e480dSDmitry Torokhov /* 779d83d213dSSven Anders * Geysers 3/4 will continue to send packets continually after 7802a3e480dSDmitry Torokhov * the first touch unless reinitialised. Do so if it's been 7812a3e480dSDmitry Torokhov * idle for a while in order to avoid waking the kernel up 782d83d213dSSven Anders * several hundred times a second. 7832a3e480dSDmitry Torokhov */ 784d83d213dSSven Anders 7857dce869fSJohannes Berg /* 7867dce869fSJohannes Berg * Button must not be pressed when entering suspend, 7877dce869fSJohannes Berg * otherwise we will never release the button. 7887dce869fSJohannes Berg */ 789937ad5c1SSoeren Sonnenburg if (!x && !y && !key) { 7905a6eb676SSoeren Sonnenburg dev->idlecount++; 7915a6eb676SSoeren Sonnenburg if (dev->idlecount == 10) { 79282a196f4SSven Anders dev->x_old = dev->y_old = -1; 79382a196f4SSven Anders dev->idlecount = 0; 7945a6eb676SSoeren Sonnenburg schedule_work(&dev->work); 7952a3e480dSDmitry Torokhov /* Don't resubmit urb here, wait for reinit */ 7962a3e480dSDmitry Torokhov return; 7975a6eb676SSoeren Sonnenburg } 79846249ea6SAnton Ekblad } else 799937ad5c1SSoeren Sonnenburg dev->idlecount = 0; 800b5da20f8SDmitry Torokhov 801b5da20f8SDmitry Torokhov exit: 802b5da20f8SDmitry Torokhov retval = usb_submit_urb(dev->urb, GFP_ATOMIC); 8037dce869fSJohannes Berg if (retval) 8042d744b09SGreg Kroah-Hartman dev_err(&dev->intf->dev, 8059c113dc6SGreg Kroah-Hartman "atp_complete: usb_submit_urb failed with result %d\n", 8067dce869fSJohannes Berg retval); 807b5da20f8SDmitry Torokhov } 808b5da20f8SDmitry Torokhov 809b5da20f8SDmitry Torokhov static int atp_open(struct input_dev *input) 810b5da20f8SDmitry Torokhov { 811b5da20f8SDmitry Torokhov struct atp *dev = input_get_drvdata(input); 812b5da20f8SDmitry Torokhov 813*7a082a24SJia-Ju Bai if (usb_submit_urb(dev->urb, GFP_KERNEL)) 814b5da20f8SDmitry Torokhov return -EIO; 815b5da20f8SDmitry Torokhov 81670851233SGustavo A. R. Silva dev->open = true; 817b5da20f8SDmitry Torokhov return 0; 818b5da20f8SDmitry Torokhov } 819b5da20f8SDmitry Torokhov 820b5da20f8SDmitry Torokhov static void atp_close(struct input_dev *input) 821b5da20f8SDmitry Torokhov { 822b5da20f8SDmitry Torokhov struct atp *dev = input_get_drvdata(input); 823b5da20f8SDmitry Torokhov 824b5da20f8SDmitry Torokhov usb_kill_urb(dev->urb); 8255a6eb676SSoeren Sonnenburg cancel_work_sync(&dev->work); 82670851233SGustavo A. R. Silva dev->open = false; 827b5da20f8SDmitry Torokhov } 828b5da20f8SDmitry Torokhov 82990d95ef6SOliver Neukum static int atp_handle_geyser(struct atp *dev) 83090d95ef6SOliver Neukum { 83109779678SStelian Pop if (dev->info != &fountain_info) { 83290d95ef6SOliver Neukum /* switch to raw sensor mode */ 83380f8594fSGreg Kroah-Hartman if (atp_geyser_init(dev)) 83490d95ef6SOliver Neukum return -EIO; 83590d95ef6SOliver Neukum 8362d744b09SGreg Kroah-Hartman dev_info(&dev->intf->dev, "Geyser mode initialized.\n"); 83790d95ef6SOliver Neukum } 83890d95ef6SOliver Neukum 83990d95ef6SOliver Neukum return 0; 84090d95ef6SOliver Neukum } 84190d95ef6SOliver Neukum 8427dce869fSJohannes Berg static int atp_probe(struct usb_interface *iface, 8437dce869fSJohannes Berg const struct usb_device_id *id) 844b5da20f8SDmitry Torokhov { 845b5da20f8SDmitry Torokhov struct atp *dev; 846b5da20f8SDmitry Torokhov struct input_dev *input_dev; 847b5da20f8SDmitry Torokhov struct usb_device *udev = interface_to_usbdev(iface); 848b5da20f8SDmitry Torokhov struct usb_host_interface *iface_desc; 849b5da20f8SDmitry Torokhov struct usb_endpoint_descriptor *endpoint; 850b5da20f8SDmitry Torokhov int int_in_endpointAddr = 0; 851b5da20f8SDmitry Torokhov int i, error = -ENOMEM; 85209779678SStelian Pop const struct atp_info *info = (const struct atp_info *)id->driver_info; 853b5da20f8SDmitry Torokhov 854b5da20f8SDmitry Torokhov /* set up the endpoint information */ 855b5da20f8SDmitry Torokhov /* use only the first interrupt-in endpoint */ 856b5da20f8SDmitry Torokhov iface_desc = iface->cur_altsetting; 857b5da20f8SDmitry Torokhov for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) { 858b5da20f8SDmitry Torokhov endpoint = &iface_desc->endpoint[i].desc; 859b5da20f8SDmitry Torokhov if (!int_in_endpointAddr && usb_endpoint_is_int_in(endpoint)) { 860b5da20f8SDmitry Torokhov /* we found an interrupt in endpoint */ 861b5da20f8SDmitry Torokhov int_in_endpointAddr = endpoint->bEndpointAddress; 862b5da20f8SDmitry Torokhov break; 863b5da20f8SDmitry Torokhov } 864b5da20f8SDmitry Torokhov } 865b5da20f8SDmitry Torokhov if (!int_in_endpointAddr) { 8669c113dc6SGreg Kroah-Hartman dev_err(&iface->dev, "Could not find int-in endpoint\n"); 867b5da20f8SDmitry Torokhov return -EIO; 868b5da20f8SDmitry Torokhov } 869b5da20f8SDmitry Torokhov 870b5da20f8SDmitry Torokhov /* allocate memory for our device state and initialize it */ 871b5da20f8SDmitry Torokhov dev = kzalloc(sizeof(struct atp), GFP_KERNEL); 872b5da20f8SDmitry Torokhov input_dev = input_allocate_device(); 873b5da20f8SDmitry Torokhov if (!dev || !input_dev) { 8749c113dc6SGreg Kroah-Hartman dev_err(&iface->dev, "Out of memory\n"); 875b5da20f8SDmitry Torokhov goto err_free_devs; 876b5da20f8SDmitry Torokhov } 877b5da20f8SDmitry Torokhov 878b5da20f8SDmitry Torokhov dev->udev = udev; 8792d744b09SGreg Kroah-Hartman dev->intf = iface; 880b5da20f8SDmitry Torokhov dev->input = input_dev; 88109779678SStelian Pop dev->info = info; 8827dce869fSJohannes Berg dev->overflow_warned = false; 883b5da20f8SDmitry Torokhov 884b5da20f8SDmitry Torokhov dev->urb = usb_alloc_urb(0, GFP_KERNEL); 885b5da20f8SDmitry Torokhov if (!dev->urb) 886b5da20f8SDmitry Torokhov goto err_free_devs; 887b5da20f8SDmitry Torokhov 888997ea58eSDaniel Mack dev->data = usb_alloc_coherent(dev->udev, dev->info->datalen, GFP_KERNEL, 889b5da20f8SDmitry Torokhov &dev->urb->transfer_dma); 890b5da20f8SDmitry Torokhov if (!dev->data) 891b5da20f8SDmitry Torokhov goto err_free_urb; 892b5da20f8SDmitry Torokhov 893b5da20f8SDmitry Torokhov usb_fill_int_urb(dev->urb, udev, 894b5da20f8SDmitry Torokhov usb_rcvintpipe(udev, int_in_endpointAddr), 89509779678SStelian Pop dev->data, dev->info->datalen, 89609779678SStelian Pop dev->info->callback, dev, 1); 897b5da20f8SDmitry Torokhov 89890d95ef6SOliver Neukum error = atp_handle_geyser(dev); 89990d95ef6SOliver Neukum if (error) 90090d95ef6SOliver Neukum goto err_free_buffer; 90190d95ef6SOliver Neukum 902b5da20f8SDmitry Torokhov usb_make_path(udev, dev->phys, sizeof(dev->phys)); 903b5da20f8SDmitry Torokhov strlcat(dev->phys, "/input0", sizeof(dev->phys)); 904b5da20f8SDmitry Torokhov 905b5da20f8SDmitry Torokhov input_dev->name = "appletouch"; 906b5da20f8SDmitry Torokhov input_dev->phys = dev->phys; 907b5da20f8SDmitry Torokhov usb_to_input_id(dev->udev, &input_dev->id); 908b5da20f8SDmitry Torokhov input_dev->dev.parent = &iface->dev; 909b5da20f8SDmitry Torokhov 910b5da20f8SDmitry Torokhov input_set_drvdata(input_dev, dev); 911b5da20f8SDmitry Torokhov 912b5da20f8SDmitry Torokhov input_dev->open = atp_open; 913b5da20f8SDmitry Torokhov input_dev->close = atp_close; 914b5da20f8SDmitry Torokhov 915b5da20f8SDmitry Torokhov set_bit(EV_ABS, input_dev->evbit); 916b5da20f8SDmitry Torokhov 917b5da20f8SDmitry Torokhov input_set_abs_params(input_dev, ABS_X, 0, 91809779678SStelian Pop (dev->info->xsensors - 1) * dev->info->xfact - 1, 919703e1488SClinton Sprain dev->info->fuzz, 0); 920b5da20f8SDmitry Torokhov input_set_abs_params(input_dev, ABS_Y, 0, 92109779678SStelian Pop (dev->info->ysensors - 1) * dev->info->yfact - 1, 922703e1488SClinton Sprain dev->info->fuzz, 0); 923b5da20f8SDmitry Torokhov input_set_abs_params(input_dev, ABS_PRESSURE, 0, ATP_PRESSURE, 0, 0); 924b5da20f8SDmitry Torokhov 925b5da20f8SDmitry Torokhov set_bit(EV_KEY, input_dev->evbit); 926b5da20f8SDmitry Torokhov set_bit(BTN_TOUCH, input_dev->keybit); 927b5da20f8SDmitry Torokhov set_bit(BTN_TOOL_FINGER, input_dev->keybit); 928b5da20f8SDmitry Torokhov set_bit(BTN_TOOL_DOUBLETAP, input_dev->keybit); 929b5da20f8SDmitry Torokhov set_bit(BTN_TOOL_TRIPLETAP, input_dev->keybit); 930b5da20f8SDmitry Torokhov set_bit(BTN_LEFT, input_dev->keybit); 931b5da20f8SDmitry Torokhov 932b5da20f8SDmitry Torokhov error = input_register_device(dev->input); 933b5da20f8SDmitry Torokhov if (error) 934b5da20f8SDmitry Torokhov goto err_free_buffer; 935b5da20f8SDmitry Torokhov 936b5da20f8SDmitry Torokhov /* save our data pointer in this interface device */ 937b5da20f8SDmitry Torokhov usb_set_intfdata(iface, dev); 938b5da20f8SDmitry Torokhov 9395a6eb676SSoeren Sonnenburg INIT_WORK(&dev->work, atp_reinit); 9405a6eb676SSoeren Sonnenburg 941b5da20f8SDmitry Torokhov return 0; 942b5da20f8SDmitry Torokhov 943b5da20f8SDmitry Torokhov err_free_buffer: 944997ea58eSDaniel Mack usb_free_coherent(dev->udev, dev->info->datalen, 945b5da20f8SDmitry Torokhov dev->data, dev->urb->transfer_dma); 946b5da20f8SDmitry Torokhov err_free_urb: 947b5da20f8SDmitry Torokhov usb_free_urb(dev->urb); 948b5da20f8SDmitry Torokhov err_free_devs: 949b5da20f8SDmitry Torokhov usb_set_intfdata(iface, NULL); 950b5da20f8SDmitry Torokhov kfree(dev); 951b5da20f8SDmitry Torokhov input_free_device(input_dev); 952b5da20f8SDmitry Torokhov return error; 953b5da20f8SDmitry Torokhov } 954b5da20f8SDmitry Torokhov 955b5da20f8SDmitry Torokhov static void atp_disconnect(struct usb_interface *iface) 956b5da20f8SDmitry Torokhov { 957b5da20f8SDmitry Torokhov struct atp *dev = usb_get_intfdata(iface); 958b5da20f8SDmitry Torokhov 959b5da20f8SDmitry Torokhov usb_set_intfdata(iface, NULL); 960b5da20f8SDmitry Torokhov if (dev) { 961b5da20f8SDmitry Torokhov usb_kill_urb(dev->urb); 962b5da20f8SDmitry Torokhov input_unregister_device(dev->input); 963997ea58eSDaniel Mack usb_free_coherent(dev->udev, dev->info->datalen, 964b5da20f8SDmitry Torokhov dev->data, dev->urb->transfer_dma); 965b5da20f8SDmitry Torokhov usb_free_urb(dev->urb); 966b5da20f8SDmitry Torokhov kfree(dev); 967b5da20f8SDmitry Torokhov } 9682d744b09SGreg Kroah-Hartman dev_info(&iface->dev, "input: appletouch disconnected\n"); 969b5da20f8SDmitry Torokhov } 970b5da20f8SDmitry Torokhov 97190d95ef6SOliver Neukum static int atp_recover(struct atp *dev) 97290d95ef6SOliver Neukum { 97390d95ef6SOliver Neukum int error; 97490d95ef6SOliver Neukum 97590d95ef6SOliver Neukum error = atp_handle_geyser(dev); 97690d95ef6SOliver Neukum if (error) 97790d95ef6SOliver Neukum return error; 97890d95ef6SOliver Neukum 979*7a082a24SJia-Ju Bai if (dev->open && usb_submit_urb(dev->urb, GFP_KERNEL)) 98090d95ef6SOliver Neukum return -EIO; 98190d95ef6SOliver Neukum 98290d95ef6SOliver Neukum return 0; 98390d95ef6SOliver Neukum } 98490d95ef6SOliver Neukum 985b5da20f8SDmitry Torokhov static int atp_suspend(struct usb_interface *iface, pm_message_t message) 986b5da20f8SDmitry Torokhov { 987b5da20f8SDmitry Torokhov struct atp *dev = usb_get_intfdata(iface); 9885a6eb676SSoeren Sonnenburg 989b5da20f8SDmitry Torokhov usb_kill_urb(dev->urb); 990b5da20f8SDmitry Torokhov return 0; 991b5da20f8SDmitry Torokhov } 992b5da20f8SDmitry Torokhov 993b5da20f8SDmitry Torokhov static int atp_resume(struct usb_interface *iface) 994b5da20f8SDmitry Torokhov { 995b5da20f8SDmitry Torokhov struct atp *dev = usb_get_intfdata(iface); 9965a6eb676SSoeren Sonnenburg 997*7a082a24SJia-Ju Bai if (dev->open && usb_submit_urb(dev->urb, GFP_KERNEL)) 998b5da20f8SDmitry Torokhov return -EIO; 999b5da20f8SDmitry Torokhov 1000b5da20f8SDmitry Torokhov return 0; 1001b5da20f8SDmitry Torokhov } 1002b5da20f8SDmitry Torokhov 100390d95ef6SOliver Neukum static int atp_reset_resume(struct usb_interface *iface) 100490d95ef6SOliver Neukum { 100590d95ef6SOliver Neukum struct atp *dev = usb_get_intfdata(iface); 100690d95ef6SOliver Neukum 100790d95ef6SOliver Neukum return atp_recover(dev); 100890d95ef6SOliver Neukum } 100990d95ef6SOliver Neukum 1010b5da20f8SDmitry Torokhov static struct usb_driver atp_driver = { 1011b5da20f8SDmitry Torokhov .name = "appletouch", 1012b5da20f8SDmitry Torokhov .probe = atp_probe, 1013b5da20f8SDmitry Torokhov .disconnect = atp_disconnect, 1014b5da20f8SDmitry Torokhov .suspend = atp_suspend, 1015b5da20f8SDmitry Torokhov .resume = atp_resume, 101690d95ef6SOliver Neukum .reset_resume = atp_reset_resume, 1017b5da20f8SDmitry Torokhov .id_table = atp_table, 1018b5da20f8SDmitry Torokhov }; 1019b5da20f8SDmitry Torokhov 102008642e7cSGreg Kroah-Hartman module_usb_driver(atp_driver); 1021