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) |
| 5 | * Copyright (C) 2005 Johannes Berg (johannes@sipsolutions.net) |
| 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) |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 11 | * |
| 12 | * Thanks to Alex Harper <basilisk@foobox.net> for his inputs. |
| 13 | * |
| 14 | * This program is free software; you can redistribute it and/or modify |
| 15 | * it under the terms of the GNU General Public License as published by |
| 16 | * the Free Software Foundation; either version 2 of the License, or |
| 17 | * (at your option) any later version. |
| 18 | * |
| 19 | * This program is distributed in the hope that it will be useful, |
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | * GNU General Public License for more details. |
| 23 | * |
| 24 | * You should have received a copy of the GNU General Public License |
| 25 | * along with this program; if not, write to the Free Software |
| 26 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 27 | * |
| 28 | */ |
| 29 | |
| 30 | #include <linux/config.h> |
| 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 */ |
| 42 | #define GEYSER_ANSI_PRODUCT_ID 0x0214 |
| 43 | #define GEYSER_ISO_PRODUCT_ID 0x0215 |
| 44 | #define GEYSER_JIS_PRODUCT_ID 0x0216 |
| 45 | |
Nicolas Boichat | 9effa97 | 2006-04-19 23:36:40 +0200 | [diff] [blame] | 46 | /* MacBook devices */ |
| 47 | #define GEYSER3_ANSI_PRODUCT_ID 0x0217 |
| 48 | #define GEYSER3_ISO_PRODUCT_ID 0x0218 |
| 49 | #define GEYSER3_JIS_PRODUCT_ID 0x0219 |
| 50 | |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 51 | #define ATP_DEVICE(prod) \ |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 52 | .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \ |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 53 | USB_DEVICE_ID_MATCH_INT_CLASS | \ |
| 54 | USB_DEVICE_ID_MATCH_INT_PROTOCOL, \ |
| 55 | .idVendor = APPLE_VENDOR_ID, \ |
| 56 | .idProduct = (prod), \ |
| 57 | .bInterfaceClass = 0x03, \ |
| 58 | .bInterfaceProtocol = 0x02 |
| 59 | |
| 60 | /* table of devices that work with this driver */ |
| 61 | static struct usb_device_id atp_table [] = { |
| 62 | { ATP_DEVICE(0x020E) }, |
| 63 | { ATP_DEVICE(0x020F) }, |
| 64 | { ATP_DEVICE(0x030A) }, |
| 65 | { ATP_DEVICE(0x030B) }, |
Michael Hanselmann | e1e02c9 | 2005-12-21 00:50:23 -0500 | [diff] [blame] | 66 | |
| 67 | /* PowerBooks Oct 2005 */ |
| 68 | { ATP_DEVICE(GEYSER_ANSI_PRODUCT_ID) }, |
| 69 | { ATP_DEVICE(GEYSER_ISO_PRODUCT_ID) }, |
| 70 | { ATP_DEVICE(GEYSER_JIS_PRODUCT_ID) }, |
| 71 | |
Nicolas Boichat | 9effa97 | 2006-04-19 23:36:40 +0200 | [diff] [blame] | 72 | { ATP_DEVICE(GEYSER3_ANSI_PRODUCT_ID) }, |
| 73 | { ATP_DEVICE(GEYSER3_ISO_PRODUCT_ID) }, |
| 74 | { ATP_DEVICE(GEYSER3_JIS_PRODUCT_ID) }, |
| 75 | |
Michael Hanselmann | e1e02c9 | 2005-12-21 00:50:23 -0500 | [diff] [blame] | 76 | /* Terminating entry */ |
| 77 | { } |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 78 | }; |
| 79 | MODULE_DEVICE_TABLE (usb, atp_table); |
| 80 | |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 81 | /* |
| 82 | * number of sensors. Note that only 16 instead of 26 X (horizontal) |
| 83 | * sensors exist on 12" and 15" PowerBooks. All models have 16 Y |
| 84 | * (vertical) sensors. |
| 85 | */ |
| 86 | #define ATP_XSENSORS 26 |
| 87 | #define ATP_YSENSORS 16 |
| 88 | |
| 89 | /* amount of fuzz this touchpad generates */ |
| 90 | #define ATP_FUZZ 16 |
| 91 | |
| 92 | /* maximum pressure this driver will report */ |
| 93 | #define ATP_PRESSURE 300 |
| 94 | /* |
| 95 | * multiplication factor for the X and Y coordinates. |
| 96 | * We try to keep the touchpad aspect ratio while still doing only simple |
| 97 | * arithmetics. |
| 98 | * The factors below give coordinates like: |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 99 | * 0 <= x < 960 on 12" and 15" Powerbooks |
| 100 | * 0 <= x < 1600 on 17" Powerbooks |
| 101 | * 0 <= y < 646 |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 102 | */ |
| 103 | #define ATP_XFACT 64 |
| 104 | #define ATP_YFACT 43 |
| 105 | |
| 106 | /* |
| 107 | * Threshold for the touchpad sensors. Any change less than ATP_THRESHOLD is |
| 108 | * ignored. |
| 109 | */ |
| 110 | #define ATP_THRESHOLD 5 |
| 111 | |
Nicolas Boichat | 9effa97 | 2006-04-19 23:36:40 +0200 | [diff] [blame] | 112 | /* MacBook Pro (Geyser 3) initialization constants */ |
| 113 | #define ATP_GEYSER3_MODE_READ_REQUEST_ID 1 |
| 114 | #define ATP_GEYSER3_MODE_WRITE_REQUEST_ID 9 |
| 115 | #define ATP_GEYSER3_MODE_REQUEST_VALUE 0x300 |
| 116 | #define ATP_GEYSER3_MODE_REQUEST_INDEX 0 |
| 117 | #define ATP_GEYSER3_MODE_VENDOR_VALUE 0x04 |
| 118 | |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 119 | /* Structure to hold all of our device specific stuff */ |
| 120 | struct atp { |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 121 | char phys[64]; |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 122 | struct usb_device * udev; /* usb device */ |
| 123 | struct urb * urb; /* usb request block */ |
| 124 | signed char * data; /* transferred data */ |
| 125 | int open; /* non-zero if opened */ |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 126 | struct input_dev *input; /* input dev */ |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 127 | int valid; /* are the sensors valid ? */ |
| 128 | int x_old; /* last reported x/y, */ |
| 129 | int y_old; /* used for smoothing */ |
| 130 | /* current value of the sensors */ |
| 131 | signed char xy_cur[ATP_XSENSORS + ATP_YSENSORS]; |
| 132 | /* last value of the sensors */ |
| 133 | signed char xy_old[ATP_XSENSORS + ATP_YSENSORS]; |
| 134 | /* accumulated sensors */ |
| 135 | int xy_acc[ATP_XSENSORS + ATP_YSENSORS]; |
Michael Hanselmann | e1e02c9 | 2005-12-21 00:50:23 -0500 | [diff] [blame] | 136 | int overflowwarn; /* overflow warning printed? */ |
| 137 | int datalen; /* size of an USB urb transfer */ |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 138 | }; |
| 139 | |
| 140 | #define dbg_dump(msg, tab) \ |
| 141 | if (debug > 1) { \ |
| 142 | int i; \ |
| 143 | printk("appletouch: %s %lld", msg, (long long)jiffies); \ |
| 144 | for (i = 0; i < ATP_XSENSORS + ATP_YSENSORS; i++) \ |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 145 | printk(" %02x", tab[i]); \ |
| 146 | printk("\n"); \ |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 147 | } |
| 148 | |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 149 | #define dprintk(format, a...) \ |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 150 | do { \ |
| 151 | if (debug) printk(format, ##a); \ |
| 152 | } while (0) |
| 153 | |
Michael Hanselmann | e1e02c9 | 2005-12-21 00:50:23 -0500 | [diff] [blame] | 154 | MODULE_AUTHOR("Johannes Berg, Stelian Pop, Frank Arnold, Michael Hanselmann"); |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 155 | MODULE_DESCRIPTION("Apple PowerBooks USB touchpad driver"); |
| 156 | MODULE_LICENSE("GPL"); |
| 157 | |
| 158 | static int debug = 1; |
| 159 | module_param(debug, int, 0644); |
| 160 | MODULE_PARM_DESC(debug, "Activate debugging output"); |
| 161 | |
Michael Hanselmann | e1e02c9 | 2005-12-21 00:50:23 -0500 | [diff] [blame] | 162 | /* Checks if the device a Geyser 2 (ANSI, ISO, JIS) */ |
| 163 | static inline int atp_is_geyser_2(struct atp *dev) |
| 164 | { |
Nicolas Boichat | 9effa97 | 2006-04-19 23:36:40 +0200 | [diff] [blame] | 165 | u16 productId = le16_to_cpu(dev->udev->descriptor.idProduct); |
Michael Hanselmann | e1e02c9 | 2005-12-21 00:50:23 -0500 | [diff] [blame] | 166 | |
| 167 | return (productId == GEYSER_ANSI_PRODUCT_ID) || |
| 168 | (productId == GEYSER_ISO_PRODUCT_ID) || |
| 169 | (productId == GEYSER_JIS_PRODUCT_ID); |
| 170 | } |
| 171 | |
Nicolas Boichat | 9effa97 | 2006-04-19 23:36:40 +0200 | [diff] [blame] | 172 | static inline int atp_is_geyser_3(struct atp *dev) |
| 173 | { |
| 174 | u16 productId = le16_to_cpu(dev->udev->descriptor.idProduct); |
| 175 | |
| 176 | return (productId == GEYSER3_ANSI_PRODUCT_ID) || |
| 177 | (productId == GEYSER3_ISO_PRODUCT_ID) || |
| 178 | (productId == GEYSER3_JIS_PRODUCT_ID); |
| 179 | } |
| 180 | |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 181 | static int atp_calculate_abs(int *xy_sensors, int nb_sensors, int fact, |
| 182 | int *z, int *fingers) |
| 183 | { |
| 184 | int i; |
| 185 | /* values to calculate mean */ |
| 186 | int pcum = 0, psum = 0; |
| 187 | |
| 188 | *fingers = 0; |
| 189 | |
| 190 | for (i = 0; i < nb_sensors; i++) { |
| 191 | if (xy_sensors[i] < ATP_THRESHOLD) |
| 192 | continue; |
| 193 | if ((i - 1 < 0) || (xy_sensors[i - 1] < ATP_THRESHOLD)) |
| 194 | (*fingers)++; |
| 195 | pcum += xy_sensors[i] * i; |
| 196 | psum += xy_sensors[i]; |
| 197 | } |
| 198 | |
| 199 | if (psum > 0) { |
| 200 | *z = psum; |
| 201 | return pcum * fact / psum; |
| 202 | } |
| 203 | |
| 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | static inline void atp_report_fingers(struct input_dev *input, int fingers) |
| 208 | { |
| 209 | input_report_key(input, BTN_TOOL_FINGER, fingers == 1); |
| 210 | input_report_key(input, BTN_TOOL_DOUBLETAP, fingers == 2); |
| 211 | input_report_key(input, BTN_TOOL_TRIPLETAP, fingers > 2); |
| 212 | } |
| 213 | |
| 214 | static void atp_complete(struct urb* urb, struct pt_regs* regs) |
| 215 | { |
| 216 | int x, y, x_z, y_z, x_f, y_f; |
Michael Hanselmann | e1e02c9 | 2005-12-21 00:50:23 -0500 | [diff] [blame] | 217 | int retval, i, j; |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 218 | struct atp *dev = urb->context; |
| 219 | |
| 220 | switch (urb->status) { |
| 221 | case 0: |
| 222 | /* success */ |
| 223 | break; |
Michael Hanselmann | e1e02c9 | 2005-12-21 00:50:23 -0500 | [diff] [blame] | 224 | case -EOVERFLOW: |
| 225 | if(!dev->overflowwarn) { |
| 226 | printk("appletouch: OVERFLOW with data " |
| 227 | "length %d, actual length is %d\n", |
| 228 | dev->datalen, dev->urb->actual_length); |
| 229 | dev->overflowwarn = 1; |
| 230 | } |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 231 | case -ECONNRESET: |
| 232 | case -ENOENT: |
| 233 | case -ESHUTDOWN: |
| 234 | /* This urb is terminated, clean up */ |
| 235 | dbg("%s - urb shutting down with status: %d", |
| 236 | __FUNCTION__, urb->status); |
| 237 | return; |
| 238 | default: |
| 239 | dbg("%s - nonzero urb status received: %d", |
| 240 | __FUNCTION__, urb->status); |
| 241 | goto exit; |
| 242 | } |
| 243 | |
| 244 | /* drop incomplete datasets */ |
Michael Hanselmann | e1e02c9 | 2005-12-21 00:50:23 -0500 | [diff] [blame] | 245 | if (dev->urb->actual_length != dev->datalen) { |
Nicolas Boichat | 9effa97 | 2006-04-19 23:36:40 +0200 | [diff] [blame] | 246 | dprintk("appletouch: incomplete data package" |
| 247 | " (first byte: %d, length: %d).\n", |
| 248 | dev->data[0], dev->urb->actual_length); |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 249 | goto exit; |
| 250 | } |
| 251 | |
| 252 | /* reorder the sensors values */ |
Nicolas Boichat | 9effa97 | 2006-04-19 23:36:40 +0200 | [diff] [blame] | 253 | if (atp_is_geyser_3(dev)) { |
| 254 | memset(dev->xy_cur, 0, sizeof(dev->xy_cur)); |
| 255 | |
| 256 | /* |
| 257 | * The values are laid out like this: |
| 258 | * -, Y1, Y2, -, Y3, Y4, -, ..., -, X1, X2, -, X3, X4, ... |
| 259 | * '-' is an unused value. |
| 260 | */ |
| 261 | |
| 262 | /* read X values */ |
| 263 | for (i = 0, j = 19; i < 20; i += 2, j += 3) { |
| 264 | dev->xy_cur[i] = dev->data[j + 1]; |
| 265 | dev->xy_cur[i + 1] = dev->data[j + 2]; |
| 266 | } |
| 267 | /* read Y values */ |
| 268 | for (i = 0, j = 1; i < 9; i += 2, j += 3) { |
| 269 | dev->xy_cur[ATP_XSENSORS + i] = dev->data[j + 1]; |
| 270 | dev->xy_cur[ATP_XSENSORS + i + 1] = dev->data[j + 2]; |
| 271 | } |
| 272 | } else if (atp_is_geyser_2(dev)) { |
Michael Hanselmann | e1e02c9 | 2005-12-21 00:50:23 -0500 | [diff] [blame] | 273 | memset(dev->xy_cur, 0, sizeof(dev->xy_cur)); |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 274 | |
Michael Hanselmann | e1e02c9 | 2005-12-21 00:50:23 -0500 | [diff] [blame] | 275 | /* |
| 276 | * The values are laid out like this: |
| 277 | * Y1, Y2, -, Y3, Y4, -, ..., X1, X2, -, X3, X4, -, ... |
| 278 | * '-' is an unused value. |
| 279 | */ |
| 280 | |
| 281 | /* read X values */ |
| 282 | for (i = 0, j = 19; i < 20; i += 2, j += 3) { |
| 283 | dev->xy_cur[i] = dev->data[j]; |
| 284 | dev->xy_cur[i + 1] = dev->data[j + 1]; |
| 285 | } |
| 286 | |
| 287 | /* read Y values */ |
| 288 | for (i = 0, j = 1; i < 9; i += 2, j += 3) { |
| 289 | dev->xy_cur[ATP_XSENSORS + i] = dev->data[j]; |
| 290 | dev->xy_cur[ATP_XSENSORS + i + 1] = dev->data[j + 1]; |
| 291 | } |
| 292 | } else { |
| 293 | for (i = 0; i < 8; i++) { |
| 294 | /* X values */ |
| 295 | dev->xy_cur[i ] = dev->data[5 * i + 2]; |
| 296 | dev->xy_cur[i + 8] = dev->data[5 * i + 4]; |
| 297 | dev->xy_cur[i + 16] = dev->data[5 * i + 42]; |
| 298 | if (i < 2) |
| 299 | dev->xy_cur[i + 24] = dev->data[5 * i + 44]; |
| 300 | |
| 301 | /* Y values */ |
| 302 | dev->xy_cur[i + 26] = dev->data[5 * i + 1]; |
| 303 | dev->xy_cur[i + 34] = dev->data[5 * i + 3]; |
| 304 | } |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | dbg_dump("sample", dev->xy_cur); |
| 308 | |
| 309 | if (!dev->valid) { |
| 310 | /* first sample */ |
| 311 | dev->valid = 1; |
| 312 | dev->x_old = dev->y_old = -1; |
| 313 | memcpy(dev->xy_old, dev->xy_cur, sizeof(dev->xy_old)); |
| 314 | |
Nicolas Boichat | 9effa97 | 2006-04-19 23:36:40 +0200 | [diff] [blame] | 315 | if (atp_is_geyser_3(dev)) /* No 17" Macbooks (yet) */ |
| 316 | goto exit; |
| 317 | |
Michael Hanselmann | e1e02c9 | 2005-12-21 00:50:23 -0500 | [diff] [blame] | 318 | /* 17" Powerbooks have extra X sensors */ |
| 319 | for (i = (atp_is_geyser_2(dev)?15:16); i < ATP_XSENSORS; i++) { |
| 320 | if (!dev->xy_cur[i]) continue; |
| 321 | |
| 322 | printk("appletouch: 17\" model detected.\n"); |
| 323 | if(atp_is_geyser_2(dev)) |
| 324 | input_set_abs_params(dev->input, ABS_X, 0, |
| 325 | (20 - 1) * |
| 326 | ATP_XFACT - 1, |
| 327 | ATP_FUZZ, 0); |
| 328 | else |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 329 | input_set_abs_params(dev->input, ABS_X, 0, |
| 330 | (ATP_XSENSORS - 1) * |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 331 | ATP_XFACT - 1, |
| 332 | ATP_FUZZ, 0); |
Michael Hanselmann | e1e02c9 | 2005-12-21 00:50:23 -0500 | [diff] [blame] | 333 | |
| 334 | break; |
| 335 | } |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 336 | |
| 337 | goto exit; |
| 338 | } |
| 339 | |
| 340 | for (i = 0; i < ATP_XSENSORS + ATP_YSENSORS; i++) { |
| 341 | /* accumulate the change */ |
| 342 | signed char change = dev->xy_old[i] - dev->xy_cur[i]; |
| 343 | dev->xy_acc[i] -= change; |
| 344 | |
| 345 | /* prevent down drifting */ |
| 346 | if (dev->xy_acc[i] < 0) |
| 347 | dev->xy_acc[i] = 0; |
| 348 | } |
| 349 | |
| 350 | memcpy(dev->xy_old, dev->xy_cur, sizeof(dev->xy_old)); |
| 351 | |
| 352 | dbg_dump("accumulator", dev->xy_acc); |
| 353 | |
| 354 | x = atp_calculate_abs(dev->xy_acc, ATP_XSENSORS, |
| 355 | ATP_XFACT, &x_z, &x_f); |
| 356 | y = atp_calculate_abs(dev->xy_acc + ATP_XSENSORS, ATP_YSENSORS, |
| 357 | ATP_YFACT, &y_z, &y_f); |
| 358 | |
| 359 | if (x && y) { |
| 360 | if (dev->x_old != -1) { |
| 361 | x = (dev->x_old * 3 + x) >> 2; |
| 362 | y = (dev->y_old * 3 + y) >> 2; |
| 363 | dev->x_old = x; |
| 364 | dev->y_old = y; |
| 365 | |
| 366 | if (debug > 1) |
| 367 | printk("appletouch: X: %3d Y: %3d " |
| 368 | "Xz: %3d Yz: %3d\n", |
| 369 | x, y, x_z, y_z); |
| 370 | |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 371 | input_report_key(dev->input, BTN_TOUCH, 1); |
| 372 | input_report_abs(dev->input, ABS_X, x); |
| 373 | input_report_abs(dev->input, ABS_Y, y); |
| 374 | input_report_abs(dev->input, ABS_PRESSURE, |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 375 | min(ATP_PRESSURE, x_z + y_z)); |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 376 | atp_report_fingers(dev->input, max(x_f, y_f)); |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 377 | } |
| 378 | dev->x_old = x; |
| 379 | dev->y_old = y; |
| 380 | } |
| 381 | else if (!x && !y) { |
| 382 | |
| 383 | dev->x_old = dev->y_old = -1; |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 384 | input_report_key(dev->input, BTN_TOUCH, 0); |
| 385 | input_report_abs(dev->input, ABS_PRESSURE, 0); |
| 386 | atp_report_fingers(dev->input, 0); |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 387 | |
| 388 | /* reset the accumulator on release */ |
| 389 | memset(dev->xy_acc, 0, sizeof(dev->xy_acc)); |
| 390 | } |
| 391 | |
Michael Hanselmann | e1e02c9 | 2005-12-21 00:50:23 -0500 | [diff] [blame] | 392 | input_report_key(dev->input, BTN_LEFT, |
| 393 | !!dev->data[dev->datalen - 1]); |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 394 | |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 395 | input_sync(dev->input); |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 396 | |
| 397 | exit: |
| 398 | retval = usb_submit_urb(dev->urb, GFP_ATOMIC); |
| 399 | if (retval) { |
| 400 | err("%s - usb_submit_urb failed with result %d", |
| 401 | __FUNCTION__, retval); |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | static int atp_open(struct input_dev *input) |
| 406 | { |
| 407 | struct atp *dev = input->private; |
| 408 | |
| 409 | if (usb_submit_urb(dev->urb, GFP_ATOMIC)) |
| 410 | return -EIO; |
| 411 | |
| 412 | dev->open = 1; |
| 413 | return 0; |
| 414 | } |
| 415 | |
| 416 | static void atp_close(struct input_dev *input) |
| 417 | { |
| 418 | struct atp *dev = input->private; |
| 419 | |
| 420 | usb_kill_urb(dev->urb); |
| 421 | dev->open = 0; |
| 422 | } |
| 423 | |
| 424 | static int atp_probe(struct usb_interface *iface, const struct usb_device_id *id) |
| 425 | { |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 426 | struct atp *dev; |
| 427 | struct input_dev *input_dev; |
| 428 | struct usb_device *udev = interface_to_usbdev(iface); |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 429 | struct usb_host_interface *iface_desc; |
| 430 | struct usb_endpoint_descriptor *endpoint; |
| 431 | int int_in_endpointAddr = 0; |
| 432 | int i, retval = -ENOMEM; |
| 433 | |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 434 | |
| 435 | /* set up the endpoint information */ |
| 436 | /* use only the first interrupt-in endpoint */ |
| 437 | iface_desc = iface->cur_altsetting; |
| 438 | for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) { |
| 439 | endpoint = &iface_desc->endpoint[i].desc; |
| 440 | if (!int_in_endpointAddr && |
| 441 | (endpoint->bEndpointAddress & USB_DIR_IN) && |
| 442 | ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) |
| 443 | == USB_ENDPOINT_XFER_INT)) { |
| 444 | /* we found an interrupt in endpoint */ |
| 445 | int_in_endpointAddr = endpoint->bEndpointAddress; |
| 446 | break; |
| 447 | } |
| 448 | } |
| 449 | if (!int_in_endpointAddr) { |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 450 | err("Could not find int-in endpoint"); |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 451 | return -EIO; |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 452 | } |
| 453 | |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 454 | /* allocate memory for our device state and initialize it */ |
| 455 | dev = kzalloc(sizeof(struct atp), GFP_KERNEL); |
| 456 | input_dev = input_allocate_device(); |
| 457 | if (!dev || !input_dev) { |
| 458 | err("Out of memory"); |
| 459 | goto err_free_devs; |
| 460 | } |
| 461 | |
| 462 | dev->udev = udev; |
| 463 | dev->input = input_dev; |
Michael Hanselmann | e1e02c9 | 2005-12-21 00:50:23 -0500 | [diff] [blame] | 464 | dev->overflowwarn = 0; |
Nicolas Boichat | 9effa97 | 2006-04-19 23:36:40 +0200 | [diff] [blame] | 465 | if (atp_is_geyser_3(dev)) |
| 466 | dev->datalen = 64; |
| 467 | else if (atp_is_geyser_2(dev)) |
| 468 | dev->datalen = 64; |
| 469 | else |
| 470 | dev->datalen = 81; |
| 471 | |
| 472 | if (atp_is_geyser_3(dev)) { |
| 473 | /* |
| 474 | * By default Geyser 3 device sends standard USB HID mouse |
| 475 | * packets (Report ID 2). This code changes device mode, so it |
| 476 | * sends raw sensor reports (Report ID 5). |
| 477 | */ |
| 478 | char data[8]; |
| 479 | int size; |
| 480 | |
| 481 | size = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), |
| 482 | ATP_GEYSER3_MODE_READ_REQUEST_ID, |
| 483 | USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE, |
| 484 | ATP_GEYSER3_MODE_REQUEST_VALUE, |
| 485 | ATP_GEYSER3_MODE_REQUEST_INDEX, &data, 8, 5000); |
| 486 | |
| 487 | if (size != 8) { |
| 488 | err("Could not do mode read request from device" |
| 489 | " (Geyser 3 mode)"); |
| 490 | goto err_free_devs; |
| 491 | } |
| 492 | |
| 493 | /* Apply the mode switch */ |
| 494 | data[0] = ATP_GEYSER3_MODE_VENDOR_VALUE; |
| 495 | |
| 496 | size = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), |
| 497 | ATP_GEYSER3_MODE_WRITE_REQUEST_ID, |
| 498 | USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE, |
| 499 | ATP_GEYSER3_MODE_REQUEST_VALUE, |
| 500 | ATP_GEYSER3_MODE_REQUEST_INDEX, &data, 8, 5000); |
| 501 | |
| 502 | if (size != 8) { |
| 503 | err("Could not do mode write request to device" |
| 504 | " (Geyser 3 mode)"); |
| 505 | goto err_free_devs; |
| 506 | } |
| 507 | printk("appletouch Geyser 3 inited.\n"); |
| 508 | } |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 509 | |
| 510 | dev->urb = usb_alloc_urb(0, GFP_KERNEL); |
| 511 | if (!dev->urb) { |
| 512 | retval = -ENOMEM; |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 513 | goto err_free_devs; |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 514 | } |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 515 | |
Michael Hanselmann | e1e02c9 | 2005-12-21 00:50:23 -0500 | [diff] [blame] | 516 | dev->data = usb_buffer_alloc(dev->udev, dev->datalen, GFP_KERNEL, |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 517 | &dev->urb->transfer_dma); |
| 518 | if (!dev->data) { |
| 519 | retval = -ENOMEM; |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 520 | goto err_free_urb; |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 521 | } |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 522 | |
| 523 | usb_fill_int_urb(dev->urb, udev, |
| 524 | usb_rcvintpipe(udev, int_in_endpointAddr), |
Michael Hanselmann | e1e02c9 | 2005-12-21 00:50:23 -0500 | [diff] [blame] | 525 | dev->data, dev->datalen, atp_complete, dev, 1); |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 526 | |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 527 | usb_make_path(udev, dev->phys, sizeof(dev->phys)); |
| 528 | strlcat(dev->phys, "/input0", sizeof(dev->phys)); |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 529 | |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 530 | input_dev->name = "appletouch"; |
| 531 | input_dev->phys = dev->phys; |
| 532 | usb_to_input_id(dev->udev, &input_dev->id); |
| 533 | input_dev->cdev.dev = &iface->dev; |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 534 | |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 535 | input_dev->private = dev; |
| 536 | input_dev->open = atp_open; |
| 537 | input_dev->close = atp_close; |
| 538 | |
| 539 | set_bit(EV_ABS, input_dev->evbit); |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 540 | |
Nicolas Boichat | 9effa97 | 2006-04-19 23:36:40 +0200 | [diff] [blame] | 541 | if (atp_is_geyser_3(dev)) { |
| 542 | /* |
| 543 | * MacBook have 20 X sensors, 10 Y sensors |
| 544 | */ |
| 545 | input_set_abs_params(input_dev, ABS_X, 0, |
| 546 | ((20 - 1) * ATP_XFACT) - 1, ATP_FUZZ, 0); |
| 547 | input_set_abs_params(input_dev, ABS_Y, 0, |
| 548 | ((10 - 1) * ATP_YFACT) - 1, ATP_FUZZ, 0); |
| 549 | } else if (atp_is_geyser_2(dev)) { |
Michael Hanselmann | e1e02c9 | 2005-12-21 00:50:23 -0500 | [diff] [blame] | 550 | /* |
| 551 | * Oct 2005 15" PowerBooks have 15 X sensors, 17" are detected |
| 552 | * later. |
| 553 | */ |
| 554 | input_set_abs_params(input_dev, ABS_X, 0, |
| 555 | ((15 - 1) * ATP_XFACT) - 1, ATP_FUZZ, 0); |
| 556 | input_set_abs_params(input_dev, ABS_Y, 0, |
| 557 | ((9 - 1) * ATP_YFACT) - 1, ATP_FUZZ, 0); |
| 558 | } else { |
| 559 | /* |
| 560 | * 12" and 15" Powerbooks only have 16 x sensors, |
| 561 | * 17" models are detected later. |
| 562 | */ |
| 563 | input_set_abs_params(input_dev, ABS_X, 0, |
| 564 | (16 - 1) * ATP_XFACT - 1, ATP_FUZZ, 0); |
| 565 | input_set_abs_params(input_dev, ABS_Y, 0, |
| 566 | (ATP_YSENSORS - 1) * ATP_YFACT - 1, ATP_FUZZ, 0); |
| 567 | } |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 568 | 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] | 569 | |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 570 | set_bit(EV_KEY, input_dev->evbit); |
| 571 | set_bit(BTN_TOUCH, input_dev->keybit); |
| 572 | set_bit(BTN_TOOL_FINGER, input_dev->keybit); |
| 573 | set_bit(BTN_TOOL_DOUBLETAP, input_dev->keybit); |
| 574 | set_bit(BTN_TOOL_TRIPLETAP, input_dev->keybit); |
| 575 | set_bit(BTN_LEFT, input_dev->keybit); |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 576 | |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 577 | input_register_device(dev->input); |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 578 | |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 579 | /* save our data pointer in this interface device */ |
| 580 | usb_set_intfdata(iface, dev); |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 581 | |
| 582 | return 0; |
| 583 | |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 584 | err_free_urb: |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 585 | usb_free_urb(dev->urb); |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 586 | err_free_devs: |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 587 | usb_set_intfdata(iface, NULL); |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 588 | kfree(dev); |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 589 | input_free_device(input_dev); |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 590 | return retval; |
| 591 | } |
| 592 | |
| 593 | static void atp_disconnect(struct usb_interface *iface) |
| 594 | { |
| 595 | struct atp *dev = usb_get_intfdata(iface); |
| 596 | |
| 597 | usb_set_intfdata(iface, NULL); |
| 598 | if (dev) { |
| 599 | usb_kill_urb(dev->urb); |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 600 | input_unregister_device(dev->input); |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 601 | usb_free_urb(dev->urb); |
Michael Hanselmann | e1e02c9 | 2005-12-21 00:50:23 -0500 | [diff] [blame] | 602 | usb_buffer_free(dev->udev, dev->datalen, |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 603 | dev->data, dev->urb->transfer_dma); |
| 604 | kfree(dev); |
| 605 | } |
| 606 | printk(KERN_INFO "input: appletouch disconnected\n"); |
| 607 | } |
| 608 | |
| 609 | static int atp_suspend(struct usb_interface *iface, pm_message_t message) |
| 610 | { |
| 611 | struct atp *dev = usb_get_intfdata(iface); |
| 612 | usb_kill_urb(dev->urb); |
| 613 | dev->valid = 0; |
| 614 | return 0; |
| 615 | } |
| 616 | |
| 617 | static int atp_resume(struct usb_interface *iface) |
| 618 | { |
| 619 | struct atp *dev = usb_get_intfdata(iface); |
| 620 | if (dev->open && usb_submit_urb(dev->urb, GFP_ATOMIC)) |
| 621 | return -EIO; |
| 622 | |
| 623 | return 0; |
| 624 | } |
| 625 | |
| 626 | static struct usb_driver atp_driver = { |
Stelian Pop | f7214ff | 2005-09-08 10:19:48 +0200 | [diff] [blame] | 627 | .name = "appletouch", |
| 628 | .probe = atp_probe, |
| 629 | .disconnect = atp_disconnect, |
| 630 | .suspend = atp_suspend, |
| 631 | .resume = atp_resume, |
| 632 | .id_table = atp_table, |
| 633 | }; |
| 634 | |
| 635 | static int __init atp_init(void) |
| 636 | { |
| 637 | return usb_register(&atp_driver); |
| 638 | } |
| 639 | |
| 640 | static void __exit atp_exit(void) |
| 641 | { |
| 642 | usb_deregister(&atp_driver); |
| 643 | } |
| 644 | |
| 645 | module_init(atp_init); |
| 646 | module_exit(atp_exit); |