Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 1 | /* |
Nicolas Boichat | 9effa97 | 2006-04-19 23:36:40 +0200 | [diff] [blame] | 2 | * Apple USB Touchpad (for post-February 2005 PowerBooks and MacBooks) driver |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2001-2004 Greg Kroah-Hartman (greg@kroah.com) |
Johannes Berg | 7dce869 | 2008-05-05 23:56:55 -0400 | [diff] [blame^] | 5 | * Copyright (C) 2005-2008 Johannes Berg (johannes@sipsolutions.net) |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 6 | * Copyright (C) 2005 Stelian Pop (stelian@popies.net) |
| 7 | * Copyright (C) 2005 Frank Arnold (frank@scirocco-5v-turbo.de) |
| 8 | * Copyright (C) 2005 Peter Osterlund (petero2@telia.com) |
Michael Hanselmann | e1e02c9 | 2005-12-21 00:50:23 -0500 | [diff] [blame] | 9 | * Copyright (C) 2005 Michael Hanselmann (linux-kernel@hansmi.ch) |
Nicolas Boichat | 9effa97 | 2006-04-19 23:36:40 +0200 | [diff] [blame] | 10 | * Copyright (C) 2006 Nicolas Boichat (nicolas@boichat.ch) |
Johannes Berg | 7dce869 | 2008-05-05 23:56:55 -0400 | [diff] [blame^] | 11 | * Copyright (C) 2007-2008 Sven Anders (anders@anduras.de) |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 12 | * |
| 13 | * Thanks to Alex Harper <basilisk@foobox.net> for his inputs. |
| 14 | * |
| 15 | * This program is free software; you can redistribute it and/or modify |
| 16 | * it under the terms of the GNU General Public License as published by |
| 17 | * the Free Software Foundation; either version 2 of the License, or |
| 18 | * (at your option) any later version. |
| 19 | * |
| 20 | * This program is distributed in the hope that it will be useful, |
| 21 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 23 | * GNU General Public License for more details. |
| 24 | * |
| 25 | * You should have received a copy of the GNU General Public License |
| 26 | * along with this program; if not, write to the Free Software |
| 27 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 28 | * |
| 29 | */ |
| 30 | |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 31 | #include <linux/kernel.h> |
| 32 | #include <linux/errno.h> |
| 33 | #include <linux/init.h> |
| 34 | #include <linux/slab.h> |
| 35 | #include <linux/module.h> |
David Brownell | ae0dadc | 2006-06-13 10:04:34 -0700 | [diff] [blame] | 36 | #include <linux/usb/input.h> |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 37 | |
| 38 | /* Apple has powerbooks which have the keyboard with different Product IDs */ |
| 39 | #define APPLE_VENDOR_ID 0x05AC |
| 40 | |
Michael Hanselmann | e1e02c9 | 2005-12-21 00:50:23 -0500 | [diff] [blame] | 41 | /* These names come from Info.plist in AppleUSBTrackpad.kext */ |
Johannes Berg | 7dce869 | 2008-05-05 23:56:55 -0400 | [diff] [blame^] | 42 | |
| 43 | /* PowerBooks Feb 2005 / iBooks */ |
Julien BLACHE | e5d9832 | 2006-11-17 01:06:25 -0500 | [diff] [blame] | 44 | #define FOUNTAIN_ANSI_PRODUCT_ID 0x020E |
| 45 | #define FOUNTAIN_ISO_PRODUCT_ID 0x020F |
| 46 | |
| 47 | #define FOUNTAIN_TP_ONLY_PRODUCT_ID 0x030A |
| 48 | |
| 49 | #define GEYSER1_TP_ONLY_PRODUCT_ID 0x030B |
| 50 | |
Johannes Berg | 7dce869 | 2008-05-05 23:56:55 -0400 | [diff] [blame^] | 51 | /* PowerBooks Oct 2005 */ |
| 52 | #define GEYSER2_ANSI_PRODUCT_ID 0x0214 |
| 53 | #define GEYSER2_ISO_PRODUCT_ID 0x0215 |
| 54 | #define GEYSER2_JIS_PRODUCT_ID 0x0216 |
Michael Hanselmann | e1e02c9 | 2005-12-21 00:50:23 -0500 | [diff] [blame] | 55 | |
Nicolas Boichat | 9effa97 | 2006-04-19 23:36:40 +0200 | [diff] [blame] | 56 | /* MacBook devices */ |
Julien BLACHE | e5d9832 | 2006-11-17 01:06:25 -0500 | [diff] [blame] | 57 | #define GEYSER3_ANSI_PRODUCT_ID 0x0217 |
| 58 | #define GEYSER3_ISO_PRODUCT_ID 0x0218 |
| 59 | #define GEYSER3_JIS_PRODUCT_ID 0x0219 |
Nicolas Boichat | 9effa97 | 2006-04-19 23:36:40 +0200 | [diff] [blame] | 60 | |
Julien BLACHE | 009ad0e | 2006-11-17 01:06:13 -0500 | [diff] [blame] | 61 | /* |
Johannes Berg | 7dce869 | 2008-05-05 23:56:55 -0400 | [diff] [blame^] | 62 | * Geyser IV: same as Geyser III according to Info.plist in OSX's |
| 63 | * AppleUSBTrackpad.kext |
Julien BLACHE | 009ad0e | 2006-11-17 01:06:13 -0500 | [diff] [blame] | 64 | * -> same IOClass (AppleUSBGrIIITrackpad), same acceleration tables |
| 65 | */ |
Johannes Berg | 7dce869 | 2008-05-05 23:56:55 -0400 | [diff] [blame^] | 66 | #define GEYSER4_ANSI_PRODUCT_ID 0x021A |
| 67 | #define GEYSER4_ISO_PRODUCT_ID 0x021B |
| 68 | #define GEYSER4_JIS_PRODUCT_ID 0x021C |
Julien BLACHE | 009ad0e | 2006-11-17 01:06:13 -0500 | [diff] [blame] | 69 | |
Johannes Berg | 7dce869 | 2008-05-05 23:56:55 -0400 | [diff] [blame^] | 70 | /* Macbook3,1 devices */ |
Tobias Mueller | 0035a1d | 2008-04-02 10:02:06 -0400 | [diff] [blame] | 71 | #define GEYSER4_HF_ANSI_PRODUCT_ID 0x0229 |
| 72 | #define GEYSER4_HF_ISO_PRODUCT_ID 0x022A |
| 73 | #define GEYSER4_HF_JIS_PRODUCT_ID 0x022B |
| 74 | |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 75 | #define ATP_DEVICE(prod) \ |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 76 | .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \ |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 77 | USB_DEVICE_ID_MATCH_INT_CLASS | \ |
| 78 | USB_DEVICE_ID_MATCH_INT_PROTOCOL, \ |
| 79 | .idVendor = APPLE_VENDOR_ID, \ |
| 80 | .idProduct = (prod), \ |
| 81 | .bInterfaceClass = 0x03, \ |
| 82 | .bInterfaceProtocol = 0x02 |
| 83 | |
| 84 | /* table of devices that work with this driver */ |
| 85 | static struct usb_device_id atp_table [] = { |
Johannes Berg | 7dce869 | 2008-05-05 23:56:55 -0400 | [diff] [blame^] | 86 | /* PowerBooks Feb 2005, iBooks G4 */ |
Julien BLACHE | e5d9832 | 2006-11-17 01:06:25 -0500 | [diff] [blame] | 87 | { ATP_DEVICE(FOUNTAIN_ANSI_PRODUCT_ID) }, |
| 88 | { ATP_DEVICE(FOUNTAIN_ISO_PRODUCT_ID) }, |
| 89 | { ATP_DEVICE(FOUNTAIN_TP_ONLY_PRODUCT_ID) }, |
| 90 | { ATP_DEVICE(GEYSER1_TP_ONLY_PRODUCT_ID) }, |
Michael Hanselmann | e1e02c9 | 2005-12-21 00:50:23 -0500 | [diff] [blame] | 91 | |
| 92 | /* PowerBooks Oct 2005 */ |
Johannes Berg | 7dce869 | 2008-05-05 23:56:55 -0400 | [diff] [blame^] | 93 | { ATP_DEVICE(GEYSER2_ANSI_PRODUCT_ID) }, |
| 94 | { ATP_DEVICE(GEYSER2_ISO_PRODUCT_ID) }, |
| 95 | { ATP_DEVICE(GEYSER2_JIS_PRODUCT_ID) }, |
Michael Hanselmann | e1e02c9 | 2005-12-21 00:50:23 -0500 | [diff] [blame] | 96 | |
Julien BLACHE | 009ad0e | 2006-11-17 01:06:13 -0500 | [diff] [blame] | 97 | /* Core Duo MacBook & MacBook Pro */ |
Nicolas Boichat | 9effa97 | 2006-04-19 23:36:40 +0200 | [diff] [blame] | 98 | { ATP_DEVICE(GEYSER3_ANSI_PRODUCT_ID) }, |
| 99 | { ATP_DEVICE(GEYSER3_ISO_PRODUCT_ID) }, |
| 100 | { ATP_DEVICE(GEYSER3_JIS_PRODUCT_ID) }, |
| 101 | |
Julien BLACHE | 009ad0e | 2006-11-17 01:06:13 -0500 | [diff] [blame] | 102 | /* Core2 Duo MacBook & MacBook Pro */ |
| 103 | { ATP_DEVICE(GEYSER4_ANSI_PRODUCT_ID) }, |
| 104 | { ATP_DEVICE(GEYSER4_ISO_PRODUCT_ID) }, |
| 105 | { ATP_DEVICE(GEYSER4_JIS_PRODUCT_ID) }, |
| 106 | |
Johannes Berg | 7dce869 | 2008-05-05 23:56:55 -0400 | [diff] [blame^] | 107 | /* Core2 Duo MacBook3,1 */ |
Tobias Mueller | 0035a1d | 2008-04-02 10:02:06 -0400 | [diff] [blame] | 108 | { ATP_DEVICE(GEYSER4_HF_ANSI_PRODUCT_ID) }, |
| 109 | { ATP_DEVICE(GEYSER4_HF_ISO_PRODUCT_ID) }, |
| 110 | { ATP_DEVICE(GEYSER4_HF_JIS_PRODUCT_ID) }, |
| 111 | |
Michael Hanselmann | e1e02c9 | 2005-12-21 00:50:23 -0500 | [diff] [blame] | 112 | /* Terminating entry */ |
| 113 | { } |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 114 | }; |
Johannes Berg | 7dce869 | 2008-05-05 23:56:55 -0400 | [diff] [blame^] | 115 | MODULE_DEVICE_TABLE(usb, atp_table); |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 116 | |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 117 | /* |
| 118 | * number of sensors. Note that only 16 instead of 26 X (horizontal) |
| 119 | * sensors exist on 12" and 15" PowerBooks. All models have 16 Y |
| 120 | * (vertical) sensors. |
| 121 | */ |
| 122 | #define ATP_XSENSORS 26 |
| 123 | #define ATP_YSENSORS 16 |
| 124 | |
| 125 | /* amount of fuzz this touchpad generates */ |
| 126 | #define ATP_FUZZ 16 |
| 127 | |
| 128 | /* maximum pressure this driver will report */ |
| 129 | #define ATP_PRESSURE 300 |
| 130 | /* |
| 131 | * multiplication factor for the X and Y coordinates. |
| 132 | * We try to keep the touchpad aspect ratio while still doing only simple |
| 133 | * arithmetics. |
| 134 | * The factors below give coordinates like: |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 135 | * 0 <= x < 960 on 12" and 15" Powerbooks |
| 136 | * 0 <= x < 1600 on 17" Powerbooks |
| 137 | * 0 <= y < 646 |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 138 | */ |
| 139 | #define ATP_XFACT 64 |
| 140 | #define ATP_YFACT 43 |
| 141 | |
| 142 | /* |
| 143 | * Threshold for the touchpad sensors. Any change less than ATP_THRESHOLD is |
| 144 | * ignored. |
| 145 | */ |
| 146 | #define ATP_THRESHOLD 5 |
| 147 | |
Dmitry Torokhov | 2a3e480 | 2007-11-01 22:13:32 -0400 | [diff] [blame] | 148 | /* Geyser initialization constants */ |
| 149 | #define ATP_GEYSER_MODE_READ_REQUEST_ID 1 |
| 150 | #define ATP_GEYSER_MODE_WRITE_REQUEST_ID 9 |
| 151 | #define ATP_GEYSER_MODE_REQUEST_VALUE 0x300 |
| 152 | #define ATP_GEYSER_MODE_REQUEST_INDEX 0 |
| 153 | #define ATP_GEYSER_MODE_VENDOR_VALUE 0x04 |
Nicolas Boichat | 9effa97 | 2006-04-19 23:36:40 +0200 | [diff] [blame] | 154 | |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 155 | /* Structure to hold all of our device specific stuff */ |
| 156 | struct atp { |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 157 | char phys[64]; |
Johannes Berg | 7dce869 | 2008-05-05 23:56:55 -0400 | [diff] [blame^] | 158 | struct usb_device *udev; /* usb device */ |
| 159 | struct urb *urb; /* usb request block */ |
| 160 | signed char *data; /* transferred data */ |
| 161 | struct input_dev *input; /* input dev */ |
| 162 | bool open; |
| 163 | bool valid; /* are the samples valid? */ |
| 164 | bool size_detect_done; |
| 165 | bool overflow_warned; |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 166 | int x_old; /* last reported x/y, */ |
| 167 | int y_old; /* used for smoothing */ |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 168 | signed char xy_cur[ATP_XSENSORS + ATP_YSENSORS]; |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 169 | signed char xy_old[ATP_XSENSORS + ATP_YSENSORS]; |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 170 | int xy_acc[ATP_XSENSORS + ATP_YSENSORS]; |
Johannes Berg | 7dce869 | 2008-05-05 23:56:55 -0400 | [diff] [blame^] | 171 | int datalen; /* size of USB transfer */ |
| 172 | int idlecount; /* number of empty packets */ |
| 173 | struct work_struct work; |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 174 | }; |
| 175 | |
| 176 | #define dbg_dump(msg, tab) \ |
| 177 | if (debug > 1) { \ |
Johannes Berg | 7dce869 | 2008-05-05 23:56:55 -0400 | [diff] [blame^] | 178 | int __i; \ |
| 179 | printk(KERN_DEBUG "appletouch: %s", msg); \ |
| 180 | for (__i = 0; __i < ATP_XSENSORS + ATP_YSENSORS; __i++) \ |
| 181 | printk(" %02x", tab[__i]); \ |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 182 | printk("\n"); \ |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 183 | } |
| 184 | |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 185 | #define dprintk(format, a...) \ |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 186 | do { \ |
Johannes Berg | 7dce869 | 2008-05-05 23:56:55 -0400 | [diff] [blame^] | 187 | if (debug) \ |
| 188 | printk(KERN_DEBUG format, ##a); \ |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 189 | } while (0) |
| 190 | |
Johannes Berg | 7dce869 | 2008-05-05 23:56:55 -0400 | [diff] [blame^] | 191 | MODULE_AUTHOR("Johannes Berg"); |
| 192 | MODULE_AUTHOR("Stelian Pop"); |
| 193 | MODULE_AUTHOR("Frank Arnold"); |
| 194 | MODULE_AUTHOR("Michael Hanselmann"); |
| 195 | MODULE_AUTHOR("Sven Anders"); |
| 196 | MODULE_DESCRIPTION("Apple PowerBook and MacBook USB touchpad driver"); |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 197 | MODULE_LICENSE("GPL"); |
| 198 | |
Jason Parekh | 00081d3 | 2006-11-17 01:05:58 -0500 | [diff] [blame] | 199 | /* |
| 200 | * Make the threshold a module parameter |
| 201 | */ |
| 202 | static int threshold = ATP_THRESHOLD; |
| 203 | module_param(threshold, int, 0644); |
Johannes Berg | 7dce869 | 2008-05-05 23:56:55 -0400 | [diff] [blame^] | 204 | MODULE_PARM_DESC(threshold, "Discard any change in data from a sensor" |
| 205 | " (the trackpad has many of these sensors)" |
| 206 | " less than this value."); |
Jason Parekh | 00081d3 | 2006-11-17 01:05:58 -0500 | [diff] [blame] | 207 | |
Johannes Berg | 7dce869 | 2008-05-05 23:56:55 -0400 | [diff] [blame^] | 208 | static int debug; |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 209 | module_param(debug, int, 0644); |
| 210 | MODULE_PARM_DESC(debug, "Activate debugging output"); |
| 211 | |
Dmitry Torokhov | 2a3e480 | 2007-11-01 22:13:32 -0400 | [diff] [blame] | 212 | static inline int atp_is_fountain(struct atp *dev) |
| 213 | { |
| 214 | u16 productId = le16_to_cpu(dev->udev->descriptor.idProduct); |
| 215 | |
| 216 | return productId == FOUNTAIN_ANSI_PRODUCT_ID || |
| 217 | productId == FOUNTAIN_ISO_PRODUCT_ID || |
| 218 | productId == FOUNTAIN_TP_ONLY_PRODUCT_ID; |
| 219 | } |
| 220 | |
Michael Hanselmann | e1e02c9 | 2005-12-21 00:50:23 -0500 | [diff] [blame] | 221 | /* Checks if the device a Geyser 2 (ANSI, ISO, JIS) */ |
| 222 | static inline int atp_is_geyser_2(struct atp *dev) |
| 223 | { |
Nicolas Boichat | 9effa97 | 2006-04-19 23:36:40 +0200 | [diff] [blame] | 224 | u16 productId = le16_to_cpu(dev->udev->descriptor.idProduct); |
Michael Hanselmann | e1e02c9 | 2005-12-21 00:50:23 -0500 | [diff] [blame] | 225 | |
Johannes Berg | 7dce869 | 2008-05-05 23:56:55 -0400 | [diff] [blame^] | 226 | return (productId == GEYSER2_ANSI_PRODUCT_ID) || |
| 227 | (productId == GEYSER2_ISO_PRODUCT_ID) || |
| 228 | (productId == GEYSER2_JIS_PRODUCT_ID); |
Michael Hanselmann | e1e02c9 | 2005-12-21 00:50:23 -0500 | [diff] [blame] | 229 | } |
| 230 | |
Nicolas Boichat | 9effa97 | 2006-04-19 23:36:40 +0200 | [diff] [blame] | 231 | static inline int atp_is_geyser_3(struct atp *dev) |
| 232 | { |
| 233 | u16 productId = le16_to_cpu(dev->udev->descriptor.idProduct); |
| 234 | |
| 235 | return (productId == GEYSER3_ANSI_PRODUCT_ID) || |
| 236 | (productId == GEYSER3_ISO_PRODUCT_ID) || |
Julien BLACHE | 009ad0e | 2006-11-17 01:06:13 -0500 | [diff] [blame] | 237 | (productId == GEYSER3_JIS_PRODUCT_ID) || |
| 238 | (productId == GEYSER4_ANSI_PRODUCT_ID) || |
| 239 | (productId == GEYSER4_ISO_PRODUCT_ID) || |
Tobias Mueller | 0035a1d | 2008-04-02 10:02:06 -0400 | [diff] [blame] | 240 | (productId == GEYSER4_JIS_PRODUCT_ID) || |
| 241 | (productId == GEYSER4_HF_ANSI_PRODUCT_ID) || |
| 242 | (productId == GEYSER4_HF_ISO_PRODUCT_ID) || |
| 243 | (productId == GEYSER4_HF_JIS_PRODUCT_ID); |
Nicolas Boichat | 9effa97 | 2006-04-19 23:36:40 +0200 | [diff] [blame] | 244 | } |
| 245 | |
Soeren Sonnenburg | 5a6eb67 | 2007-07-20 00:29:32 -0400 | [diff] [blame] | 246 | /* |
Dmitry Torokhov | 2a3e480 | 2007-11-01 22:13:32 -0400 | [diff] [blame] | 247 | * By default newer Geyser devices send standard USB HID mouse |
Soeren Sonnenburg | 5a6eb67 | 2007-07-20 00:29:32 -0400 | [diff] [blame] | 248 | * packets (Report ID 2). This code changes device mode, so it |
| 249 | * sends raw sensor reports (Report ID 5). |
| 250 | */ |
Dmitry Torokhov | 2a3e480 | 2007-11-01 22:13:32 -0400 | [diff] [blame] | 251 | static int atp_geyser_init(struct usb_device *udev) |
Soeren Sonnenburg | 5a6eb67 | 2007-07-20 00:29:32 -0400 | [diff] [blame] | 252 | { |
| 253 | char data[8]; |
| 254 | int size; |
Johannes Berg | 7dce869 | 2008-05-05 23:56:55 -0400 | [diff] [blame^] | 255 | int i; |
Soeren Sonnenburg | 5a6eb67 | 2007-07-20 00:29:32 -0400 | [diff] [blame] | 256 | |
| 257 | size = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), |
Dmitry Torokhov | 2a3e480 | 2007-11-01 22:13:32 -0400 | [diff] [blame] | 258 | ATP_GEYSER_MODE_READ_REQUEST_ID, |
Soeren Sonnenburg | 5a6eb67 | 2007-07-20 00:29:32 -0400 | [diff] [blame] | 259 | USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE, |
Dmitry Torokhov | 2a3e480 | 2007-11-01 22:13:32 -0400 | [diff] [blame] | 260 | ATP_GEYSER_MODE_REQUEST_VALUE, |
| 261 | ATP_GEYSER_MODE_REQUEST_INDEX, &data, 8, 5000); |
Soeren Sonnenburg | 5a6eb67 | 2007-07-20 00:29:32 -0400 | [diff] [blame] | 262 | |
| 263 | if (size != 8) { |
Johannes Berg | 7dce869 | 2008-05-05 23:56:55 -0400 | [diff] [blame^] | 264 | dprintk("atp_geyser_init: read error\n"); |
| 265 | for (i = 0; i < 8; i++) |
| 266 | dprintk("appletouch[%d]: %d\n", i, data[i]); |
| 267 | |
| 268 | err("Failed to read mode from device."); |
Soeren Sonnenburg | 5a6eb67 | 2007-07-20 00:29:32 -0400 | [diff] [blame] | 269 | return -EIO; |
| 270 | } |
| 271 | |
| 272 | /* Apply the mode switch */ |
Dmitry Torokhov | 2a3e480 | 2007-11-01 22:13:32 -0400 | [diff] [blame] | 273 | data[0] = ATP_GEYSER_MODE_VENDOR_VALUE; |
Soeren Sonnenburg | 5a6eb67 | 2007-07-20 00:29:32 -0400 | [diff] [blame] | 274 | |
| 275 | size = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), |
Dmitry Torokhov | 2a3e480 | 2007-11-01 22:13:32 -0400 | [diff] [blame] | 276 | ATP_GEYSER_MODE_WRITE_REQUEST_ID, |
Soeren Sonnenburg | 5a6eb67 | 2007-07-20 00:29:32 -0400 | [diff] [blame] | 277 | USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE, |
Dmitry Torokhov | 2a3e480 | 2007-11-01 22:13:32 -0400 | [diff] [blame] | 278 | ATP_GEYSER_MODE_REQUEST_VALUE, |
| 279 | ATP_GEYSER_MODE_REQUEST_INDEX, &data, 8, 5000); |
Soeren Sonnenburg | 5a6eb67 | 2007-07-20 00:29:32 -0400 | [diff] [blame] | 280 | |
| 281 | if (size != 8) { |
Johannes Berg | 7dce869 | 2008-05-05 23:56:55 -0400 | [diff] [blame^] | 282 | dprintk("atp_geyser_init: write error\n"); |
| 283 | for (i = 0; i < 8; i++) |
| 284 | dprintk("appletouch[%d]: %d\n", i, data[i]); |
| 285 | |
| 286 | err("Failed to request geyser raw mode"); |
Soeren Sonnenburg | 5a6eb67 | 2007-07-20 00:29:32 -0400 | [diff] [blame] | 287 | return -EIO; |
| 288 | } |
| 289 | return 0; |
| 290 | } |
| 291 | |
Dmitry Torokhov | 2a3e480 | 2007-11-01 22:13:32 -0400 | [diff] [blame] | 292 | /* |
| 293 | * Reinitialise the device. This usually stops stream of empty packets |
| 294 | * coming from it. |
| 295 | */ |
Soeren Sonnenburg | 5a6eb67 | 2007-07-20 00:29:32 -0400 | [diff] [blame] | 296 | static void atp_reinit(struct work_struct *work) |
| 297 | { |
| 298 | struct atp *dev = container_of(work, struct atp, work); |
| 299 | struct usb_device *udev = dev->udev; |
Dmitry Torokhov | 2a3e480 | 2007-11-01 22:13:32 -0400 | [diff] [blame] | 300 | int retval; |
Soeren Sonnenburg | 5a6eb67 | 2007-07-20 00:29:32 -0400 | [diff] [blame] | 301 | |
Johannes Berg | 7dce869 | 2008-05-05 23:56:55 -0400 | [diff] [blame^] | 302 | dprintk("appletouch: putting appletouch to sleep (reinit)\n"); |
Soeren Sonnenburg | 5a6eb67 | 2007-07-20 00:29:32 -0400 | [diff] [blame] | 303 | dev->idlecount = 0; |
Dmitry Torokhov | 2a3e480 | 2007-11-01 22:13:32 -0400 | [diff] [blame] | 304 | |
| 305 | atp_geyser_init(udev); |
| 306 | |
| 307 | retval = usb_submit_urb(dev->urb, GFP_ATOMIC); |
Johannes Berg | 7dce869 | 2008-05-05 23:56:55 -0400 | [diff] [blame^] | 308 | if (retval) |
| 309 | err("atp_reinit: usb_submit_urb failed with error %d", |
| 310 | retval); |
Soeren Sonnenburg | 5a6eb67 | 2007-07-20 00:29:32 -0400 | [diff] [blame] | 311 | } |
| 312 | |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 313 | static int atp_calculate_abs(int *xy_sensors, int nb_sensors, int fact, |
| 314 | int *z, int *fingers) |
| 315 | { |
| 316 | int i; |
| 317 | /* values to calculate mean */ |
| 318 | int pcum = 0, psum = 0; |
Jason Parekh | 00081d3 | 2006-11-17 01:05:58 -0500 | [diff] [blame] | 319 | int is_increasing = 0; |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 320 | |
| 321 | *fingers = 0; |
| 322 | |
| 323 | for (i = 0; i < nb_sensors; i++) { |
Jason Parekh | 00081d3 | 2006-11-17 01:05:58 -0500 | [diff] [blame] | 324 | if (xy_sensors[i] < threshold) { |
| 325 | if (is_increasing) |
| 326 | is_increasing = 0; |
| 327 | |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 328 | continue; |
Jason Parekh | 00081d3 | 2006-11-17 01:05:58 -0500 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | /* |
| 332 | * Makes the finger detection more versatile. For example, |
| 333 | * two fingers with no gap will be detected. Also, my |
| 334 | * tests show it less likely to have intermittent loss |
| 335 | * of multiple finger readings while moving around (scrolling). |
| 336 | * |
| 337 | * Changes the multiple finger detection to counting humps on |
| 338 | * sensors (transitions from nonincreasing to increasing) |
| 339 | * instead of counting transitions from low sensors (no |
| 340 | * finger reading) to high sensors (finger above |
| 341 | * sensor) |
| 342 | * |
| 343 | * - Jason Parekh <jasonparekh@gmail.com> |
| 344 | */ |
Johannes Berg | 7dce869 | 2008-05-05 23:56:55 -0400 | [diff] [blame^] | 345 | if (i < 1 || |
| 346 | (!is_increasing && xy_sensors[i - 1] < xy_sensors[i])) { |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 347 | (*fingers)++; |
Jason Parekh | 00081d3 | 2006-11-17 01:05:58 -0500 | [diff] [blame] | 348 | is_increasing = 1; |
| 349 | } else if (i > 0 && xy_sensors[i - 1] >= xy_sensors[i]) { |
| 350 | is_increasing = 0; |
| 351 | } |
| 352 | |
| 353 | /* |
Johannes Berg | 7dce869 | 2008-05-05 23:56:55 -0400 | [diff] [blame^] | 354 | * Subtracts threshold so a high sensor that just passes the |
| 355 | * threshold won't skew the calculated absolute coordinate. |
| 356 | * Fixes an issue where slowly moving the mouse would |
| 357 | * occasionally jump a number of pixels (slowly moving the |
| 358 | * finger makes this issue most apparent.) |
Jason Parekh | 00081d3 | 2006-11-17 01:05:58 -0500 | [diff] [blame] | 359 | */ |
| 360 | pcum += (xy_sensors[i] - threshold) * i; |
| 361 | psum += (xy_sensors[i] - threshold); |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | if (psum > 0) { |
| 365 | *z = psum; |
| 366 | return pcum * fact / psum; |
| 367 | } |
| 368 | |
| 369 | return 0; |
| 370 | } |
| 371 | |
| 372 | static inline void atp_report_fingers(struct input_dev *input, int fingers) |
| 373 | { |
| 374 | input_report_key(input, BTN_TOOL_FINGER, fingers == 1); |
| 375 | input_report_key(input, BTN_TOOL_DOUBLETAP, fingers == 2); |
| 376 | input_report_key(input, BTN_TOOL_TRIPLETAP, fingers > 2); |
| 377 | } |
| 378 | |
Johannes Berg | 7dce869 | 2008-05-05 23:56:55 -0400 | [diff] [blame^] | 379 | static void atp_complete(struct urb *urb) |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 380 | { |
| 381 | int x, y, x_z, y_z, x_f, y_f; |
Michael Hanselmann | e1e02c9 | 2005-12-21 00:50:23 -0500 | [diff] [blame] | 382 | int retval, i, j; |
Thomas Rohwer | cb56073 | 2007-09-25 00:06:25 -0400 | [diff] [blame] | 383 | int key; |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 384 | struct atp *dev = urb->context; |
| 385 | |
| 386 | switch (urb->status) { |
| 387 | case 0: |
| 388 | /* success */ |
| 389 | break; |
Michael Hanselmann | e1e02c9 | 2005-12-21 00:50:23 -0500 | [diff] [blame] | 390 | case -EOVERFLOW: |
Johannes Berg | 7dce869 | 2008-05-05 23:56:55 -0400 | [diff] [blame^] | 391 | if (!dev->overflow_warned) { |
Dmitry Torokhov | 2a3e480 | 2007-11-01 22:13:32 -0400 | [diff] [blame] | 392 | printk(KERN_WARNING "appletouch: OVERFLOW with data " |
Michael Hanselmann | e1e02c9 | 2005-12-21 00:50:23 -0500 | [diff] [blame] | 393 | "length %d, actual length is %d\n", |
| 394 | dev->datalen, dev->urb->actual_length); |
Johannes Berg | 7dce869 | 2008-05-05 23:56:55 -0400 | [diff] [blame^] | 395 | dev->overflow_warned = true; |
Michael Hanselmann | e1e02c9 | 2005-12-21 00:50:23 -0500 | [diff] [blame] | 396 | } |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 397 | case -ECONNRESET: |
| 398 | case -ENOENT: |
| 399 | case -ESHUTDOWN: |
| 400 | /* This urb is terminated, clean up */ |
Johannes Berg | 7dce869 | 2008-05-05 23:56:55 -0400 | [diff] [blame^] | 401 | dbg("atp_complete: urb shutting down with status: %d", |
| 402 | urb->status); |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 403 | return; |
| 404 | default: |
Johannes Berg | 7dce869 | 2008-05-05 23:56:55 -0400 | [diff] [blame^] | 405 | dbg("atp_complete: nonzero urb status received: %d", |
| 406 | urb->status); |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 407 | goto exit; |
| 408 | } |
| 409 | |
| 410 | /* drop incomplete datasets */ |
Michael Hanselmann | e1e02c9 | 2005-12-21 00:50:23 -0500 | [diff] [blame] | 411 | if (dev->urb->actual_length != dev->datalen) { |
Nicolas Boichat | 9effa97 | 2006-04-19 23:36:40 +0200 | [diff] [blame] | 412 | dprintk("appletouch: incomplete data package" |
| 413 | " (first byte: %d, length: %d).\n", |
| 414 | dev->data[0], dev->urb->actual_length); |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 415 | goto exit; |
| 416 | } |
| 417 | |
| 418 | /* reorder the sensors values */ |
Nicolas Boichat | 9effa97 | 2006-04-19 23:36:40 +0200 | [diff] [blame] | 419 | if (atp_is_geyser_3(dev)) { |
| 420 | memset(dev->xy_cur, 0, sizeof(dev->xy_cur)); |
| 421 | |
| 422 | /* |
| 423 | * The values are laid out like this: |
| 424 | * -, Y1, Y2, -, Y3, Y4, -, ..., -, X1, X2, -, X3, X4, ... |
| 425 | * '-' is an unused value. |
| 426 | */ |
| 427 | |
| 428 | /* read X values */ |
| 429 | for (i = 0, j = 19; i < 20; i += 2, j += 3) { |
| 430 | dev->xy_cur[i] = dev->data[j + 1]; |
| 431 | dev->xy_cur[i + 1] = dev->data[j + 2]; |
| 432 | } |
| 433 | /* read Y values */ |
| 434 | for (i = 0, j = 1; i < 9; i += 2, j += 3) { |
| 435 | dev->xy_cur[ATP_XSENSORS + i] = dev->data[j + 1]; |
| 436 | dev->xy_cur[ATP_XSENSORS + i + 1] = dev->data[j + 2]; |
| 437 | } |
| 438 | } else if (atp_is_geyser_2(dev)) { |
Michael Hanselmann | e1e02c9 | 2005-12-21 00:50:23 -0500 | [diff] [blame] | 439 | memset(dev->xy_cur, 0, sizeof(dev->xy_cur)); |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 440 | |
Michael Hanselmann | e1e02c9 | 2005-12-21 00:50:23 -0500 | [diff] [blame] | 441 | /* |
| 442 | * The values are laid out like this: |
| 443 | * Y1, Y2, -, Y3, Y4, -, ..., X1, X2, -, X3, X4, -, ... |
| 444 | * '-' is an unused value. |
| 445 | */ |
| 446 | |
| 447 | /* read X values */ |
| 448 | for (i = 0, j = 19; i < 20; i += 2, j += 3) { |
| 449 | dev->xy_cur[i] = dev->data[j]; |
| 450 | dev->xy_cur[i + 1] = dev->data[j + 1]; |
| 451 | } |
| 452 | |
| 453 | /* read Y values */ |
| 454 | for (i = 0, j = 1; i < 9; i += 2, j += 3) { |
| 455 | dev->xy_cur[ATP_XSENSORS + i] = dev->data[j]; |
| 456 | dev->xy_cur[ATP_XSENSORS + i + 1] = dev->data[j + 1]; |
| 457 | } |
| 458 | } else { |
| 459 | for (i = 0; i < 8; i++) { |
| 460 | /* X values */ |
Johannes Berg | 7dce869 | 2008-05-05 23:56:55 -0400 | [diff] [blame^] | 461 | dev->xy_cur[i + 0] = dev->data[5 * i + 2]; |
Michael Hanselmann | e1e02c9 | 2005-12-21 00:50:23 -0500 | [diff] [blame] | 462 | dev->xy_cur[i + 8] = dev->data[5 * i + 4]; |
| 463 | dev->xy_cur[i + 16] = dev->data[5 * i + 42]; |
| 464 | if (i < 2) |
| 465 | dev->xy_cur[i + 24] = dev->data[5 * i + 44]; |
| 466 | |
| 467 | /* Y values */ |
| 468 | dev->xy_cur[i + 26] = dev->data[5 * i + 1]; |
| 469 | dev->xy_cur[i + 34] = dev->data[5 * i + 3]; |
| 470 | } |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | dbg_dump("sample", dev->xy_cur); |
| 474 | |
| 475 | if (!dev->valid) { |
| 476 | /* first sample */ |
Johannes Berg | 7dce869 | 2008-05-05 23:56:55 -0400 | [diff] [blame^] | 477 | dev->valid = true; |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 478 | dev->x_old = dev->y_old = -1; |
| 479 | memcpy(dev->xy_old, dev->xy_cur, sizeof(dev->xy_old)); |
| 480 | |
Dmitry Torokhov | 2a3e480 | 2007-11-01 22:13:32 -0400 | [diff] [blame] | 481 | if (dev->size_detect_done || |
| 482 | atp_is_geyser_3(dev)) /* No 17" Macbooks (yet) */ |
Nicolas Boichat | 9effa97 | 2006-04-19 23:36:40 +0200 | [diff] [blame] | 483 | goto exit; |
| 484 | |
Michael Hanselmann | e1e02c9 | 2005-12-21 00:50:23 -0500 | [diff] [blame] | 485 | /* 17" Powerbooks have extra X sensors */ |
Johannes Berg | 7dce869 | 2008-05-05 23:56:55 -0400 | [diff] [blame^] | 486 | for (i = (atp_is_geyser_2(dev) ? 15 : 16); |
| 487 | i < ATP_XSENSORS; i++) { |
Dmitry Torokhov | 2a3e480 | 2007-11-01 22:13:32 -0400 | [diff] [blame] | 488 | if (!dev->xy_cur[i]) |
| 489 | continue; |
Michael Hanselmann | e1e02c9 | 2005-12-21 00:50:23 -0500 | [diff] [blame] | 490 | |
Dmitry Torokhov | 2a3e480 | 2007-11-01 22:13:32 -0400 | [diff] [blame] | 491 | printk(KERN_INFO "appletouch: 17\" model detected.\n"); |
| 492 | if (atp_is_geyser_2(dev)) |
Michael Hanselmann | e1e02c9 | 2005-12-21 00:50:23 -0500 | [diff] [blame] | 493 | input_set_abs_params(dev->input, ABS_X, 0, |
| 494 | (20 - 1) * |
| 495 | ATP_XFACT - 1, |
| 496 | ATP_FUZZ, 0); |
| 497 | else |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 498 | input_set_abs_params(dev->input, ABS_X, 0, |
| 499 | (ATP_XSENSORS - 1) * |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 500 | ATP_XFACT - 1, |
| 501 | ATP_FUZZ, 0); |
Michael Hanselmann | e1e02c9 | 2005-12-21 00:50:23 -0500 | [diff] [blame] | 502 | break; |
| 503 | } |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 504 | |
Dmitry Torokhov | 2a3e480 | 2007-11-01 22:13:32 -0400 | [diff] [blame] | 505 | dev->size_detect_done = 1; |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 506 | goto exit; |
| 507 | } |
| 508 | |
| 509 | for (i = 0; i < ATP_XSENSORS + ATP_YSENSORS; i++) { |
| 510 | /* accumulate the change */ |
| 511 | signed char change = dev->xy_old[i] - dev->xy_cur[i]; |
| 512 | dev->xy_acc[i] -= change; |
| 513 | |
| 514 | /* prevent down drifting */ |
| 515 | if (dev->xy_acc[i] < 0) |
| 516 | dev->xy_acc[i] = 0; |
| 517 | } |
| 518 | |
| 519 | memcpy(dev->xy_old, dev->xy_cur, sizeof(dev->xy_old)); |
| 520 | |
| 521 | dbg_dump("accumulator", dev->xy_acc); |
| 522 | |
| 523 | x = atp_calculate_abs(dev->xy_acc, ATP_XSENSORS, |
| 524 | ATP_XFACT, &x_z, &x_f); |
| 525 | y = atp_calculate_abs(dev->xy_acc + ATP_XSENSORS, ATP_YSENSORS, |
| 526 | ATP_YFACT, &y_z, &y_f); |
Thomas Rohwer | cb56073 | 2007-09-25 00:06:25 -0400 | [diff] [blame] | 527 | key = dev->data[dev->datalen - 1] & 1; |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 528 | |
| 529 | if (x && y) { |
| 530 | if (dev->x_old != -1) { |
| 531 | x = (dev->x_old * 3 + x) >> 2; |
| 532 | y = (dev->y_old * 3 + y) >> 2; |
| 533 | dev->x_old = x; |
| 534 | dev->y_old = y; |
| 535 | |
| 536 | if (debug > 1) |
Dmitry Torokhov | 2a3e480 | 2007-11-01 22:13:32 -0400 | [diff] [blame] | 537 | printk(KERN_DEBUG "appletouch: X: %3d Y: %3d " |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 538 | "Xz: %3d Yz: %3d\n", |
| 539 | x, y, x_z, y_z); |
| 540 | |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 541 | input_report_key(dev->input, BTN_TOUCH, 1); |
| 542 | input_report_abs(dev->input, ABS_X, x); |
| 543 | input_report_abs(dev->input, ABS_Y, y); |
| 544 | input_report_abs(dev->input, ABS_PRESSURE, |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 545 | min(ATP_PRESSURE, x_z + y_z)); |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 546 | atp_report_fingers(dev->input, max(x_f, y_f)); |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 547 | } |
| 548 | dev->x_old = x; |
| 549 | dev->y_old = y; |
Soeren Sonnenburg | 5a6eb67 | 2007-07-20 00:29:32 -0400 | [diff] [blame] | 550 | |
| 551 | } else if (!x && !y) { |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 552 | |
| 553 | dev->x_old = dev->y_old = -1; |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 554 | input_report_key(dev->input, BTN_TOUCH, 0); |
| 555 | input_report_abs(dev->input, ABS_PRESSURE, 0); |
| 556 | atp_report_fingers(dev->input, 0); |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 557 | |
| 558 | /* reset the accumulator on release */ |
| 559 | memset(dev->xy_acc, 0, sizeof(dev->xy_acc)); |
Soeren Sonnenburg | 937ad5c | 2007-10-13 00:31:15 -0400 | [diff] [blame] | 560 | } |
Soeren Sonnenburg | 5a6eb67 | 2007-07-20 00:29:32 -0400 | [diff] [blame] | 561 | |
Anton Ekblad | 46249ea | 2007-10-22 00:59:59 -0400 | [diff] [blame] | 562 | input_report_key(dev->input, BTN_LEFT, key); |
| 563 | input_sync(dev->input); |
| 564 | |
Dmitry Torokhov | 2a3e480 | 2007-11-01 22:13:32 -0400 | [diff] [blame] | 565 | /* |
| 566 | * Many Geysers will continue to send packets continually after |
| 567 | * the first touch unless reinitialised. Do so if it's been |
| 568 | * idle for a while in order to avoid waking the kernel up |
| 569 | * several hundred times a second. Re-initialization does not |
| 570 | * work on Fountain touchpads. |
| 571 | */ |
| 572 | if (!atp_is_fountain(dev)) { |
Johannes Berg | 7dce869 | 2008-05-05 23:56:55 -0400 | [diff] [blame^] | 573 | /* |
| 574 | * Button must not be pressed when entering suspend, |
| 575 | * otherwise we will never release the button. |
| 576 | */ |
Dmitry Torokhov | 2a3e480 | 2007-11-01 22:13:32 -0400 | [diff] [blame] | 577 | if (!x && !y && !key) { |
| 578 | dev->idlecount++; |
| 579 | if (dev->idlecount == 10) { |
Johannes Berg | 7dce869 | 2008-05-05 23:56:55 -0400 | [diff] [blame^] | 580 | dev->valid = false; |
Dmitry Torokhov | 2a3e480 | 2007-11-01 22:13:32 -0400 | [diff] [blame] | 581 | schedule_work(&dev->work); |
| 582 | /* Don't resubmit urb here, wait for reinit */ |
| 583 | return; |
| 584 | } |
| 585 | } else |
| 586 | dev->idlecount = 0; |
| 587 | } |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 588 | |
Johannes Berg | 7dce869 | 2008-05-05 23:56:55 -0400 | [diff] [blame^] | 589 | exit: |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 590 | retval = usb_submit_urb(dev->urb, GFP_ATOMIC); |
Johannes Berg | 7dce869 | 2008-05-05 23:56:55 -0400 | [diff] [blame^] | 591 | if (retval) |
| 592 | err("atp_complete: usb_submit_urb failed with result %d", |
| 593 | retval); |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | static int atp_open(struct input_dev *input) |
| 597 | { |
Dmitry Torokhov | 7791bda | 2007-04-12 01:34:39 -0400 | [diff] [blame] | 598 | struct atp *dev = input_get_drvdata(input); |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 599 | |
| 600 | if (usb_submit_urb(dev->urb, GFP_ATOMIC)) |
| 601 | return -EIO; |
| 602 | |
| 603 | dev->open = 1; |
| 604 | return 0; |
| 605 | } |
| 606 | |
| 607 | static void atp_close(struct input_dev *input) |
| 608 | { |
Dmitry Torokhov | 7791bda | 2007-04-12 01:34:39 -0400 | [diff] [blame] | 609 | struct atp *dev = input_get_drvdata(input); |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 610 | |
| 611 | usb_kill_urb(dev->urb); |
Soeren Sonnenburg | 5a6eb67 | 2007-07-20 00:29:32 -0400 | [diff] [blame] | 612 | cancel_work_sync(&dev->work); |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 613 | dev->open = 0; |
| 614 | } |
| 615 | |
Johannes Berg | 7dce869 | 2008-05-05 23:56:55 -0400 | [diff] [blame^] | 616 | static int atp_probe(struct usb_interface *iface, |
| 617 | const struct usb_device_id *id) |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 618 | { |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 619 | struct atp *dev; |
| 620 | struct input_dev *input_dev; |
| 621 | struct usb_device *udev = interface_to_usbdev(iface); |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 622 | struct usb_host_interface *iface_desc; |
| 623 | struct usb_endpoint_descriptor *endpoint; |
| 624 | int int_in_endpointAddr = 0; |
Dmitry Torokhov | 5014186 | 2007-04-12 01:33:39 -0400 | [diff] [blame] | 625 | int i, error = -ENOMEM; |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 626 | |
| 627 | /* set up the endpoint information */ |
| 628 | /* use only the first interrupt-in endpoint */ |
| 629 | iface_desc = iface->cur_altsetting; |
| 630 | for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) { |
| 631 | endpoint = &iface_desc->endpoint[i].desc; |
Luiz Fernando N. Capitulino | 97b107c | 2006-09-27 11:58:53 -0700 | [diff] [blame] | 632 | if (!int_in_endpointAddr && usb_endpoint_is_int_in(endpoint)) { |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 633 | /* we found an interrupt in endpoint */ |
| 634 | int_in_endpointAddr = endpoint->bEndpointAddress; |
| 635 | break; |
| 636 | } |
| 637 | } |
| 638 | if (!int_in_endpointAddr) { |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 639 | err("Could not find int-in endpoint"); |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 640 | return -EIO; |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 641 | } |
| 642 | |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 643 | /* allocate memory for our device state and initialize it */ |
| 644 | dev = kzalloc(sizeof(struct atp), GFP_KERNEL); |
| 645 | input_dev = input_allocate_device(); |
| 646 | if (!dev || !input_dev) { |
| 647 | err("Out of memory"); |
| 648 | goto err_free_devs; |
| 649 | } |
| 650 | |
| 651 | dev->udev = udev; |
| 652 | dev->input = input_dev; |
Johannes Berg | 7dce869 | 2008-05-05 23:56:55 -0400 | [diff] [blame^] | 653 | dev->overflow_warned = false; |
Nicolas Boichat | 9effa97 | 2006-04-19 23:36:40 +0200 | [diff] [blame] | 654 | if (atp_is_geyser_3(dev)) |
| 655 | dev->datalen = 64; |
| 656 | else if (atp_is_geyser_2(dev)) |
| 657 | dev->datalen = 64; |
| 658 | else |
| 659 | dev->datalen = 81; |
| 660 | |
Dmitry Torokhov | 2a3e480 | 2007-11-01 22:13:32 -0400 | [diff] [blame] | 661 | if (!atp_is_fountain(dev)) { |
Soeren Sonnenburg | 5a6eb67 | 2007-07-20 00:29:32 -0400 | [diff] [blame] | 662 | /* switch to raw sensor mode */ |
Dmitry Torokhov | 2a3e480 | 2007-11-01 22:13:32 -0400 | [diff] [blame] | 663 | if (atp_geyser_init(udev)) |
Nicolas Boichat | 9effa97 | 2006-04-19 23:36:40 +0200 | [diff] [blame] | 664 | goto err_free_devs; |
Nicolas Boichat | 9effa97 | 2006-04-19 23:36:40 +0200 | [diff] [blame] | 665 | |
Dmitry Torokhov | 2a3e480 | 2007-11-01 22:13:32 -0400 | [diff] [blame] | 666 | printk(KERN_INFO "appletouch: Geyser mode initialized.\n"); |
Nicolas Boichat | 9effa97 | 2006-04-19 23:36:40 +0200 | [diff] [blame] | 667 | } |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 668 | |
| 669 | dev->urb = usb_alloc_urb(0, GFP_KERNEL); |
Dmitry Torokhov | 5014186 | 2007-04-12 01:33:39 -0400 | [diff] [blame] | 670 | if (!dev->urb) |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 671 | goto err_free_devs; |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 672 | |
Michael Hanselmann | e1e02c9 | 2005-12-21 00:50:23 -0500 | [diff] [blame] | 673 | dev->data = usb_buffer_alloc(dev->udev, dev->datalen, GFP_KERNEL, |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 674 | &dev->urb->transfer_dma); |
Dmitry Torokhov | 5014186 | 2007-04-12 01:33:39 -0400 | [diff] [blame] | 675 | if (!dev->data) |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 676 | goto err_free_urb; |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 677 | |
| 678 | usb_fill_int_urb(dev->urb, udev, |
| 679 | usb_rcvintpipe(udev, int_in_endpointAddr), |
Michael Hanselmann | e1e02c9 | 2005-12-21 00:50:23 -0500 | [diff] [blame] | 680 | dev->data, dev->datalen, atp_complete, dev, 1); |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 681 | |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 682 | usb_make_path(udev, dev->phys, sizeof(dev->phys)); |
| 683 | strlcat(dev->phys, "/input0", sizeof(dev->phys)); |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 684 | |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 685 | input_dev->name = "appletouch"; |
| 686 | input_dev->phys = dev->phys; |
| 687 | usb_to_input_id(dev->udev, &input_dev->id); |
Dmitry Torokhov | c0f82d5 | 2007-04-12 01:35:03 -0400 | [diff] [blame] | 688 | input_dev->dev.parent = &iface->dev; |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 689 | |
Dmitry Torokhov | 7791bda | 2007-04-12 01:34:39 -0400 | [diff] [blame] | 690 | input_set_drvdata(input_dev, dev); |
| 691 | |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 692 | input_dev->open = atp_open; |
| 693 | input_dev->close = atp_close; |
| 694 | |
| 695 | set_bit(EV_ABS, input_dev->evbit); |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 696 | |
Nicolas Boichat | 9effa97 | 2006-04-19 23:36:40 +0200 | [diff] [blame] | 697 | if (atp_is_geyser_3(dev)) { |
| 698 | /* |
| 699 | * MacBook have 20 X sensors, 10 Y sensors |
| 700 | */ |
| 701 | input_set_abs_params(input_dev, ABS_X, 0, |
| 702 | ((20 - 1) * ATP_XFACT) - 1, ATP_FUZZ, 0); |
| 703 | input_set_abs_params(input_dev, ABS_Y, 0, |
| 704 | ((10 - 1) * ATP_YFACT) - 1, ATP_FUZZ, 0); |
| 705 | } else if (atp_is_geyser_2(dev)) { |
Michael Hanselmann | e1e02c9 | 2005-12-21 00:50:23 -0500 | [diff] [blame] | 706 | /* |
| 707 | * Oct 2005 15" PowerBooks have 15 X sensors, 17" are detected |
| 708 | * later. |
| 709 | */ |
| 710 | input_set_abs_params(input_dev, ABS_X, 0, |
| 711 | ((15 - 1) * ATP_XFACT) - 1, ATP_FUZZ, 0); |
| 712 | input_set_abs_params(input_dev, ABS_Y, 0, |
| 713 | ((9 - 1) * ATP_YFACT) - 1, ATP_FUZZ, 0); |
| 714 | } else { |
| 715 | /* |
| 716 | * 12" and 15" Powerbooks only have 16 x sensors, |
| 717 | * 17" models are detected later. |
| 718 | */ |
| 719 | input_set_abs_params(input_dev, ABS_X, 0, |
Johannes Berg | 7dce869 | 2008-05-05 23:56:55 -0400 | [diff] [blame^] | 720 | (16 - 1) * ATP_XFACT - 1, |
| 721 | ATP_FUZZ, 0); |
Michael Hanselmann | e1e02c9 | 2005-12-21 00:50:23 -0500 | [diff] [blame] | 722 | input_set_abs_params(input_dev, ABS_Y, 0, |
Johannes Berg | 7dce869 | 2008-05-05 23:56:55 -0400 | [diff] [blame^] | 723 | (ATP_YSENSORS - 1) * ATP_YFACT - 1, |
| 724 | ATP_FUZZ, 0); |
Michael Hanselmann | e1e02c9 | 2005-12-21 00:50:23 -0500 | [diff] [blame] | 725 | } |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 726 | input_set_abs_params(input_dev, ABS_PRESSURE, 0, ATP_PRESSURE, 0, 0); |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 727 | |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 728 | set_bit(EV_KEY, input_dev->evbit); |
| 729 | set_bit(BTN_TOUCH, input_dev->keybit); |
| 730 | set_bit(BTN_TOOL_FINGER, input_dev->keybit); |
| 731 | set_bit(BTN_TOOL_DOUBLETAP, input_dev->keybit); |
| 732 | set_bit(BTN_TOOL_TRIPLETAP, input_dev->keybit); |
| 733 | set_bit(BTN_LEFT, input_dev->keybit); |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 734 | |
Dmitry Torokhov | 5014186 | 2007-04-12 01:33:39 -0400 | [diff] [blame] | 735 | error = input_register_device(dev->input); |
| 736 | if (error) |
| 737 | goto err_free_buffer; |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 738 | |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 739 | /* save our data pointer in this interface device */ |
| 740 | usb_set_intfdata(iface, dev); |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 741 | |
Soeren Sonnenburg | 5a6eb67 | 2007-07-20 00:29:32 -0400 | [diff] [blame] | 742 | INIT_WORK(&dev->work, atp_reinit); |
| 743 | |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 744 | return 0; |
| 745 | |
Dmitry Torokhov | 5014186 | 2007-04-12 01:33:39 -0400 | [diff] [blame] | 746 | err_free_buffer: |
| 747 | usb_buffer_free(dev->udev, dev->datalen, |
| 748 | dev->data, dev->urb->transfer_dma); |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 749 | err_free_urb: |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 750 | usb_free_urb(dev->urb); |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 751 | err_free_devs: |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 752 | usb_set_intfdata(iface, NULL); |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 753 | kfree(dev); |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 754 | input_free_device(input_dev); |
Dmitry Torokhov | 5014186 | 2007-04-12 01:33:39 -0400 | [diff] [blame] | 755 | return error; |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | static void atp_disconnect(struct usb_interface *iface) |
| 759 | { |
| 760 | struct atp *dev = usb_get_intfdata(iface); |
| 761 | |
| 762 | usb_set_intfdata(iface, NULL); |
| 763 | if (dev) { |
| 764 | usb_kill_urb(dev->urb); |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 765 | input_unregister_device(dev->input); |
Michael Hanselmann | e1e02c9 | 2005-12-21 00:50:23 -0500 | [diff] [blame] | 766 | usb_buffer_free(dev->udev, dev->datalen, |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 767 | dev->data, dev->urb->transfer_dma); |
Johannes Berg | 7af569a | 2006-07-19 15:39:46 +0200 | [diff] [blame] | 768 | usb_free_urb(dev->urb); |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 769 | kfree(dev); |
| 770 | } |
| 771 | printk(KERN_INFO "input: appletouch disconnected\n"); |
| 772 | } |
| 773 | |
| 774 | static int atp_suspend(struct usb_interface *iface, pm_message_t message) |
| 775 | { |
| 776 | struct atp *dev = usb_get_intfdata(iface); |
Soeren Sonnenburg | 5a6eb67 | 2007-07-20 00:29:32 -0400 | [diff] [blame] | 777 | |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 778 | usb_kill_urb(dev->urb); |
Johannes Berg | 7dce869 | 2008-05-05 23:56:55 -0400 | [diff] [blame^] | 779 | dev->valid = false; |
Soeren Sonnenburg | 5a6eb67 | 2007-07-20 00:29:32 -0400 | [diff] [blame] | 780 | |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 781 | return 0; |
| 782 | } |
| 783 | |
| 784 | static int atp_resume(struct usb_interface *iface) |
| 785 | { |
| 786 | struct atp *dev = usb_get_intfdata(iface); |
Soeren Sonnenburg | 5a6eb67 | 2007-07-20 00:29:32 -0400 | [diff] [blame] | 787 | |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 788 | if (dev->open && usb_submit_urb(dev->urb, GFP_ATOMIC)) |
| 789 | return -EIO; |
| 790 | |
| 791 | return 0; |
| 792 | } |
| 793 | |
| 794 | static struct usb_driver atp_driver = { |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 795 | .name = "appletouch", |
| 796 | .probe = atp_probe, |
| 797 | .disconnect = atp_disconnect, |
| 798 | .suspend = atp_suspend, |
| 799 | .resume = atp_resume, |
| 800 | .id_table = atp_table, |
| 801 | }; |
| 802 | |
| 803 | static int __init atp_init(void) |
| 804 | { |
| 805 | return usb_register(&atp_driver); |
| 806 | } |
| 807 | |
| 808 | static void __exit atp_exit(void) |
| 809 | { |
| 810 | usb_deregister(&atp_driver); |
| 811 | } |
| 812 | |
| 813 | module_init(atp_init); |
| 814 | module_exit(atp_exit); |