blob: 2bea1b2c631c81274b80304f3e4be11216013068 [file] [log] [blame]
Stelian Popf7214ff2005-09-08 10:19:48 +02001/*
Nicolas Boichat9effa972006-04-19 23:36:40 +02002 * Apple USB Touchpad (for post-February 2005 PowerBooks and MacBooks) driver
Stelian Popf7214ff2005-09-08 10:19:48 +02003 *
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 Hanselmanne1e02c92005-12-21 00:50:23 -05009 * Copyright (C) 2005 Michael Hanselmann (linux-kernel@hansmi.ch)
Nicolas Boichat9effa972006-04-19 23:36:40 +020010 * Copyright (C) 2006 Nicolas Boichat (nicolas@boichat.ch)
Stelian Popf7214ff2005-09-08 10:19:48 +020011 *
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
Stelian Popf7214ff2005-09-08 10:19:48 +020030#include <linux/kernel.h>
31#include <linux/errno.h>
32#include <linux/init.h>
33#include <linux/slab.h>
34#include <linux/module.h>
David Brownellae0dadc2006-06-13 10:04:34 -070035#include <linux/usb/input.h>
Stelian Popf7214ff2005-09-08 10:19:48 +020036
37/* Apple has powerbooks which have the keyboard with different Product IDs */
38#define APPLE_VENDOR_ID 0x05AC
39
Michael Hanselmanne1e02c92005-12-21 00:50:23 -050040/* These names come from Info.plist in AppleUSBTrackpad.kext */
Julien BLACHEe5d98322006-11-17 01:06:25 -050041#define FOUNTAIN_ANSI_PRODUCT_ID 0x020E
42#define FOUNTAIN_ISO_PRODUCT_ID 0x020F
43
44#define FOUNTAIN_TP_ONLY_PRODUCT_ID 0x030A
45
46#define GEYSER1_TP_ONLY_PRODUCT_ID 0x030B
47
48#define GEYSER_ANSI_PRODUCT_ID 0x0214
49#define GEYSER_ISO_PRODUCT_ID 0x0215
50#define GEYSER_JIS_PRODUCT_ID 0x0216
Michael Hanselmanne1e02c92005-12-21 00:50:23 -050051
Nicolas Boichat9effa972006-04-19 23:36:40 +020052/* MacBook devices */
Julien BLACHEe5d98322006-11-17 01:06:25 -050053#define GEYSER3_ANSI_PRODUCT_ID 0x0217
54#define GEYSER3_ISO_PRODUCT_ID 0x0218
55#define GEYSER3_JIS_PRODUCT_ID 0x0219
Nicolas Boichat9effa972006-04-19 23:36:40 +020056
Julien BLACHE009ad0e2006-11-17 01:06:13 -050057/*
58 * Geyser IV: same as Geyser III according to Info.plist in AppleUSBTrackpad.kext
59 * -> same IOClass (AppleUSBGrIIITrackpad), same acceleration tables
60 */
61#define GEYSER4_ANSI_PRODUCT_ID 0x021A
62#define GEYSER4_ISO_PRODUCT_ID 0x021B
63#define GEYSER4_JIS_PRODUCT_ID 0x021C
64
Stelian Popf7214ff2005-09-08 10:19:48 +020065#define ATP_DEVICE(prod) \
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -050066 .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \
Stelian Popf7214ff2005-09-08 10:19:48 +020067 USB_DEVICE_ID_MATCH_INT_CLASS | \
68 USB_DEVICE_ID_MATCH_INT_PROTOCOL, \
69 .idVendor = APPLE_VENDOR_ID, \
70 .idProduct = (prod), \
71 .bInterfaceClass = 0x03, \
72 .bInterfaceProtocol = 0x02
73
74/* table of devices that work with this driver */
75static struct usb_device_id atp_table [] = {
Julien BLACHEe5d98322006-11-17 01:06:25 -050076 { ATP_DEVICE(FOUNTAIN_ANSI_PRODUCT_ID) },
77 { ATP_DEVICE(FOUNTAIN_ISO_PRODUCT_ID) },
78 { ATP_DEVICE(FOUNTAIN_TP_ONLY_PRODUCT_ID) },
79 { ATP_DEVICE(GEYSER1_TP_ONLY_PRODUCT_ID) },
Michael Hanselmanne1e02c92005-12-21 00:50:23 -050080
81 /* PowerBooks Oct 2005 */
82 { ATP_DEVICE(GEYSER_ANSI_PRODUCT_ID) },
83 { ATP_DEVICE(GEYSER_ISO_PRODUCT_ID) },
84 { ATP_DEVICE(GEYSER_JIS_PRODUCT_ID) },
85
Julien BLACHE009ad0e2006-11-17 01:06:13 -050086 /* Core Duo MacBook & MacBook Pro */
Nicolas Boichat9effa972006-04-19 23:36:40 +020087 { ATP_DEVICE(GEYSER3_ANSI_PRODUCT_ID) },
88 { ATP_DEVICE(GEYSER3_ISO_PRODUCT_ID) },
89 { ATP_DEVICE(GEYSER3_JIS_PRODUCT_ID) },
90
Julien BLACHE009ad0e2006-11-17 01:06:13 -050091 /* Core2 Duo MacBook & MacBook Pro */
92 { ATP_DEVICE(GEYSER4_ANSI_PRODUCT_ID) },
93 { ATP_DEVICE(GEYSER4_ISO_PRODUCT_ID) },
94 { ATP_DEVICE(GEYSER4_JIS_PRODUCT_ID) },
95
Michael Hanselmanne1e02c92005-12-21 00:50:23 -050096 /* Terminating entry */
97 { }
Stelian Popf7214ff2005-09-08 10:19:48 +020098};
99MODULE_DEVICE_TABLE (usb, atp_table);
100
Stelian Popf7214ff2005-09-08 10:19:48 +0200101/*
102 * number of sensors. Note that only 16 instead of 26 X (horizontal)
103 * sensors exist on 12" and 15" PowerBooks. All models have 16 Y
104 * (vertical) sensors.
105 */
106#define ATP_XSENSORS 26
107#define ATP_YSENSORS 16
108
109/* amount of fuzz this touchpad generates */
110#define ATP_FUZZ 16
111
112/* maximum pressure this driver will report */
113#define ATP_PRESSURE 300
114/*
115 * multiplication factor for the X and Y coordinates.
116 * We try to keep the touchpad aspect ratio while still doing only simple
117 * arithmetics.
118 * The factors below give coordinates like:
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500119 * 0 <= x < 960 on 12" and 15" Powerbooks
120 * 0 <= x < 1600 on 17" Powerbooks
121 * 0 <= y < 646
Stelian Popf7214ff2005-09-08 10:19:48 +0200122 */
123#define ATP_XFACT 64
124#define ATP_YFACT 43
125
126/*
127 * Threshold for the touchpad sensors. Any change less than ATP_THRESHOLD is
128 * ignored.
129 */
130#define ATP_THRESHOLD 5
131
Julien BLACHE009ad0e2006-11-17 01:06:13 -0500132/* MacBook Pro (Geyser 3 & 4) initialization constants */
Nicolas Boichat9effa972006-04-19 23:36:40 +0200133#define ATP_GEYSER3_MODE_READ_REQUEST_ID 1
134#define ATP_GEYSER3_MODE_WRITE_REQUEST_ID 9
135#define ATP_GEYSER3_MODE_REQUEST_VALUE 0x300
136#define ATP_GEYSER3_MODE_REQUEST_INDEX 0
137#define ATP_GEYSER3_MODE_VENDOR_VALUE 0x04
138
Stelian Popf7214ff2005-09-08 10:19:48 +0200139/* Structure to hold all of our device specific stuff */
140struct atp {
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500141 char phys[64];
Stelian Popf7214ff2005-09-08 10:19:48 +0200142 struct usb_device * udev; /* usb device */
143 struct urb * urb; /* usb request block */
144 signed char * data; /* transferred data */
145 int open; /* non-zero if opened */
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500146 struct input_dev *input; /* input dev */
Stelian Popf7214ff2005-09-08 10:19:48 +0200147 int valid; /* are the sensors valid ? */
148 int x_old; /* last reported x/y, */
149 int y_old; /* used for smoothing */
150 /* current value of the sensors */
151 signed char xy_cur[ATP_XSENSORS + ATP_YSENSORS];
152 /* last value of the sensors */
153 signed char xy_old[ATP_XSENSORS + ATP_YSENSORS];
154 /* accumulated sensors */
155 int xy_acc[ATP_XSENSORS + ATP_YSENSORS];
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500156 int overflowwarn; /* overflow warning printed? */
157 int datalen; /* size of an USB urb transfer */
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400158 int idlecount; /* number of empty packets */
159 struct work_struct work;
Stelian Popf7214ff2005-09-08 10:19:48 +0200160};
161
162#define dbg_dump(msg, tab) \
163 if (debug > 1) { \
164 int i; \
165 printk("appletouch: %s %lld", msg, (long long)jiffies); \
166 for (i = 0; i < ATP_XSENSORS + ATP_YSENSORS; i++) \
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500167 printk(" %02x", tab[i]); \
168 printk("\n"); \
Stelian Popf7214ff2005-09-08 10:19:48 +0200169 }
170
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500171#define dprintk(format, a...) \
Stelian Popf7214ff2005-09-08 10:19:48 +0200172 do { \
173 if (debug) printk(format, ##a); \
174 } while (0)
175
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500176MODULE_AUTHOR("Johannes Berg, Stelian Pop, Frank Arnold, Michael Hanselmann");
Stelian Popf7214ff2005-09-08 10:19:48 +0200177MODULE_DESCRIPTION("Apple PowerBooks USB touchpad driver");
178MODULE_LICENSE("GPL");
179
Jason Parekh00081d32006-11-17 01:05:58 -0500180/*
181 * Make the threshold a module parameter
182 */
183static int threshold = ATP_THRESHOLD;
184module_param(threshold, int, 0644);
185MODULE_PARM_DESC(threshold, "Discards any change in data from a sensor (trackpad has hundreds of these sensors) less than this value");
186
Stelian Popf7214ff2005-09-08 10:19:48 +0200187static int debug = 1;
188module_param(debug, int, 0644);
189MODULE_PARM_DESC(debug, "Activate debugging output");
190
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500191/* Checks if the device a Geyser 2 (ANSI, ISO, JIS) */
192static inline int atp_is_geyser_2(struct atp *dev)
193{
Nicolas Boichat9effa972006-04-19 23:36:40 +0200194 u16 productId = le16_to_cpu(dev->udev->descriptor.idProduct);
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500195
196 return (productId == GEYSER_ANSI_PRODUCT_ID) ||
197 (productId == GEYSER_ISO_PRODUCT_ID) ||
198 (productId == GEYSER_JIS_PRODUCT_ID);
199}
200
Nicolas Boichat9effa972006-04-19 23:36:40 +0200201static inline int atp_is_geyser_3(struct atp *dev)
202{
203 u16 productId = le16_to_cpu(dev->udev->descriptor.idProduct);
204
205 return (productId == GEYSER3_ANSI_PRODUCT_ID) ||
206 (productId == GEYSER3_ISO_PRODUCT_ID) ||
Julien BLACHE009ad0e2006-11-17 01:06:13 -0500207 (productId == GEYSER3_JIS_PRODUCT_ID) ||
208 (productId == GEYSER4_ANSI_PRODUCT_ID) ||
209 (productId == GEYSER4_ISO_PRODUCT_ID) ||
210 (productId == GEYSER4_JIS_PRODUCT_ID);
Nicolas Boichat9effa972006-04-19 23:36:40 +0200211}
212
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400213/*
214 * By default Geyser 3 device sends standard USB HID mouse
215 * packets (Report ID 2). This code changes device mode, so it
216 * sends raw sensor reports (Report ID 5).
217 */
218static int atp_geyser3_init(struct usb_device *udev)
219{
220 char data[8];
221 int size;
222
223 size = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
224 ATP_GEYSER3_MODE_READ_REQUEST_ID,
225 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
226 ATP_GEYSER3_MODE_REQUEST_VALUE,
227 ATP_GEYSER3_MODE_REQUEST_INDEX, &data, 8, 5000);
228
229 if (size != 8) {
230 err("Could not do mode read request from device"
231 " (Geyser 3 mode)");
232 return -EIO;
233 }
234
235 /* Apply the mode switch */
236 data[0] = ATP_GEYSER3_MODE_VENDOR_VALUE;
237
238 size = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
239 ATP_GEYSER3_MODE_WRITE_REQUEST_ID,
240 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
241 ATP_GEYSER3_MODE_REQUEST_VALUE,
242 ATP_GEYSER3_MODE_REQUEST_INDEX, &data, 8, 5000);
243
244 if (size != 8) {
245 err("Could not do mode write request to device"
246 " (Geyser 3 mode)");
247 return -EIO;
248 }
249 return 0;
250}
251
252/* Reinitialise the device if it's a geyser 3 */
253static void atp_reinit(struct work_struct *work)
254{
255 struct atp *dev = container_of(work, struct atp, work);
256 struct usb_device *udev = dev->udev;
257
258 dev->idlecount = 0;
259 atp_geyser3_init(udev);
260}
261
Stelian Popf7214ff2005-09-08 10:19:48 +0200262static int atp_calculate_abs(int *xy_sensors, int nb_sensors, int fact,
263 int *z, int *fingers)
264{
265 int i;
266 /* values to calculate mean */
267 int pcum = 0, psum = 0;
Jason Parekh00081d32006-11-17 01:05:58 -0500268 int is_increasing = 0;
Stelian Popf7214ff2005-09-08 10:19:48 +0200269
270 *fingers = 0;
271
272 for (i = 0; i < nb_sensors; i++) {
Jason Parekh00081d32006-11-17 01:05:58 -0500273 if (xy_sensors[i] < threshold) {
274 if (is_increasing)
275 is_increasing = 0;
276
Stelian Popf7214ff2005-09-08 10:19:48 +0200277 continue;
Jason Parekh00081d32006-11-17 01:05:58 -0500278 }
279
280 /*
281 * Makes the finger detection more versatile. For example,
282 * two fingers with no gap will be detected. Also, my
283 * tests show it less likely to have intermittent loss
284 * of multiple finger readings while moving around (scrolling).
285 *
286 * Changes the multiple finger detection to counting humps on
287 * sensors (transitions from nonincreasing to increasing)
288 * instead of counting transitions from low sensors (no
289 * finger reading) to high sensors (finger above
290 * sensor)
291 *
292 * - Jason Parekh <jasonparekh@gmail.com>
293 */
294 if (i < 1 || (!is_increasing && xy_sensors[i - 1] < xy_sensors[i])) {
Stelian Popf7214ff2005-09-08 10:19:48 +0200295 (*fingers)++;
Jason Parekh00081d32006-11-17 01:05:58 -0500296 is_increasing = 1;
297 } else if (i > 0 && xy_sensors[i - 1] >= xy_sensors[i]) {
298 is_increasing = 0;
299 }
300
301 /*
302 * Subtracts threshold so a high sensor that just passes the threshold
303 * won't skew the calculated absolute coordinate. Fixes an issue
304 * where slowly moving the mouse would occassionaly jump a number of
305 * pixels (let me restate--slowly moving the mouse makes this issue
306 * most apparent).
307 */
308 pcum += (xy_sensors[i] - threshold) * i;
309 psum += (xy_sensors[i] - threshold);
Stelian Popf7214ff2005-09-08 10:19:48 +0200310 }
311
312 if (psum > 0) {
313 *z = psum;
314 return pcum * fact / psum;
315 }
316
317 return 0;
318}
319
320static inline void atp_report_fingers(struct input_dev *input, int fingers)
321{
322 input_report_key(input, BTN_TOOL_FINGER, fingers == 1);
323 input_report_key(input, BTN_TOOL_DOUBLETAP, fingers == 2);
324 input_report_key(input, BTN_TOOL_TRIPLETAP, fingers > 2);
325}
326
David Howells7d12e782006-10-05 14:55:46 +0100327static void atp_complete(struct urb* urb)
Stelian Popf7214ff2005-09-08 10:19:48 +0200328{
329 int x, y, x_z, y_z, x_f, y_f;
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500330 int retval, i, j;
Stelian Popf7214ff2005-09-08 10:19:48 +0200331 struct atp *dev = urb->context;
332
333 switch (urb->status) {
334 case 0:
335 /* success */
336 break;
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500337 case -EOVERFLOW:
338 if(!dev->overflowwarn) {
339 printk("appletouch: OVERFLOW with data "
340 "length %d, actual length is %d\n",
341 dev->datalen, dev->urb->actual_length);
342 dev->overflowwarn = 1;
343 }
Stelian Popf7214ff2005-09-08 10:19:48 +0200344 case -ECONNRESET:
345 case -ENOENT:
346 case -ESHUTDOWN:
347 /* This urb is terminated, clean up */
348 dbg("%s - urb shutting down with status: %d",
349 __FUNCTION__, urb->status);
350 return;
351 default:
352 dbg("%s - nonzero urb status received: %d",
353 __FUNCTION__, urb->status);
354 goto exit;
355 }
356
357 /* drop incomplete datasets */
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500358 if (dev->urb->actual_length != dev->datalen) {
Nicolas Boichat9effa972006-04-19 23:36:40 +0200359 dprintk("appletouch: incomplete data package"
360 " (first byte: %d, length: %d).\n",
361 dev->data[0], dev->urb->actual_length);
Stelian Popf7214ff2005-09-08 10:19:48 +0200362 goto exit;
363 }
364
365 /* reorder the sensors values */
Nicolas Boichat9effa972006-04-19 23:36:40 +0200366 if (atp_is_geyser_3(dev)) {
367 memset(dev->xy_cur, 0, sizeof(dev->xy_cur));
368
369 /*
370 * The values are laid out like this:
371 * -, Y1, Y2, -, Y3, Y4, -, ..., -, X1, X2, -, X3, X4, ...
372 * '-' is an unused value.
373 */
374
375 /* read X values */
376 for (i = 0, j = 19; i < 20; i += 2, j += 3) {
377 dev->xy_cur[i] = dev->data[j + 1];
378 dev->xy_cur[i + 1] = dev->data[j + 2];
379 }
380 /* read Y values */
381 for (i = 0, j = 1; i < 9; i += 2, j += 3) {
382 dev->xy_cur[ATP_XSENSORS + i] = dev->data[j + 1];
383 dev->xy_cur[ATP_XSENSORS + i + 1] = dev->data[j + 2];
384 }
385 } else if (atp_is_geyser_2(dev)) {
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500386 memset(dev->xy_cur, 0, sizeof(dev->xy_cur));
Stelian Popf7214ff2005-09-08 10:19:48 +0200387
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500388 /*
389 * The values are laid out like this:
390 * Y1, Y2, -, Y3, Y4, -, ..., X1, X2, -, X3, X4, -, ...
391 * '-' is an unused value.
392 */
393
394 /* read X values */
395 for (i = 0, j = 19; i < 20; i += 2, j += 3) {
396 dev->xy_cur[i] = dev->data[j];
397 dev->xy_cur[i + 1] = dev->data[j + 1];
398 }
399
400 /* read Y values */
401 for (i = 0, j = 1; i < 9; i += 2, j += 3) {
402 dev->xy_cur[ATP_XSENSORS + i] = dev->data[j];
403 dev->xy_cur[ATP_XSENSORS + i + 1] = dev->data[j + 1];
404 }
405 } else {
406 for (i = 0; i < 8; i++) {
407 /* X values */
408 dev->xy_cur[i ] = dev->data[5 * i + 2];
409 dev->xy_cur[i + 8] = dev->data[5 * i + 4];
410 dev->xy_cur[i + 16] = dev->data[5 * i + 42];
411 if (i < 2)
412 dev->xy_cur[i + 24] = dev->data[5 * i + 44];
413
414 /* Y values */
415 dev->xy_cur[i + 26] = dev->data[5 * i + 1];
416 dev->xy_cur[i + 34] = dev->data[5 * i + 3];
417 }
Stelian Popf7214ff2005-09-08 10:19:48 +0200418 }
419
420 dbg_dump("sample", dev->xy_cur);
421
422 if (!dev->valid) {
423 /* first sample */
424 dev->valid = 1;
425 dev->x_old = dev->y_old = -1;
426 memcpy(dev->xy_old, dev->xy_cur, sizeof(dev->xy_old));
427
Nicolas Boichat9effa972006-04-19 23:36:40 +0200428 if (atp_is_geyser_3(dev)) /* No 17" Macbooks (yet) */
429 goto exit;
430
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500431 /* 17" Powerbooks have extra X sensors */
432 for (i = (atp_is_geyser_2(dev)?15:16); i < ATP_XSENSORS; i++) {
433 if (!dev->xy_cur[i]) continue;
434
435 printk("appletouch: 17\" model detected.\n");
436 if(atp_is_geyser_2(dev))
437 input_set_abs_params(dev->input, ABS_X, 0,
438 (20 - 1) *
439 ATP_XFACT - 1,
440 ATP_FUZZ, 0);
441 else
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500442 input_set_abs_params(dev->input, ABS_X, 0,
443 (ATP_XSENSORS - 1) *
Stelian Popf7214ff2005-09-08 10:19:48 +0200444 ATP_XFACT - 1,
445 ATP_FUZZ, 0);
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500446
447 break;
448 }
Stelian Popf7214ff2005-09-08 10:19:48 +0200449
450 goto exit;
451 }
452
453 for (i = 0; i < ATP_XSENSORS + ATP_YSENSORS; i++) {
454 /* accumulate the change */
455 signed char change = dev->xy_old[i] - dev->xy_cur[i];
456 dev->xy_acc[i] -= change;
457
458 /* prevent down drifting */
459 if (dev->xy_acc[i] < 0)
460 dev->xy_acc[i] = 0;
461 }
462
463 memcpy(dev->xy_old, dev->xy_cur, sizeof(dev->xy_old));
464
465 dbg_dump("accumulator", dev->xy_acc);
466
467 x = atp_calculate_abs(dev->xy_acc, ATP_XSENSORS,
468 ATP_XFACT, &x_z, &x_f);
469 y = atp_calculate_abs(dev->xy_acc + ATP_XSENSORS, ATP_YSENSORS,
470 ATP_YFACT, &y_z, &y_f);
471
472 if (x && y) {
473 if (dev->x_old != -1) {
474 x = (dev->x_old * 3 + x) >> 2;
475 y = (dev->y_old * 3 + y) >> 2;
476 dev->x_old = x;
477 dev->y_old = y;
478
479 if (debug > 1)
480 printk("appletouch: X: %3d Y: %3d "
481 "Xz: %3d Yz: %3d\n",
482 x, y, x_z, y_z);
483
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500484 input_report_key(dev->input, BTN_TOUCH, 1);
485 input_report_abs(dev->input, ABS_X, x);
486 input_report_abs(dev->input, ABS_Y, y);
487 input_report_abs(dev->input, ABS_PRESSURE,
Stelian Popf7214ff2005-09-08 10:19:48 +0200488 min(ATP_PRESSURE, x_z + y_z));
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500489 atp_report_fingers(dev->input, max(x_f, y_f));
Stelian Popf7214ff2005-09-08 10:19:48 +0200490 }
491 dev->x_old = x;
492 dev->y_old = y;
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400493
494 } else if (!x && !y) {
Stelian Popf7214ff2005-09-08 10:19:48 +0200495
496 dev->x_old = dev->y_old = -1;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500497 input_report_key(dev->input, BTN_TOUCH, 0);
498 input_report_abs(dev->input, ABS_PRESSURE, 0);
499 atp_report_fingers(dev->input, 0);
Stelian Popf7214ff2005-09-08 10:19:48 +0200500
501 /* reset the accumulator on release */
502 memset(dev->xy_acc, 0, sizeof(dev->xy_acc));
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400503
504 /* Geyser 3 will continue to send packets continually after
505 the first touch unless reinitialised. Do so if it's been
506 idle for a while in order to avoid waking the kernel up
507 several hundred times a second */
508 if (atp_is_geyser_3(dev)) {
509 dev->idlecount++;
510 if (dev->idlecount == 10) {
511 dev->valid = 0;
512 schedule_work(&dev->work);
513 }
514 }
Stelian Popf7214ff2005-09-08 10:19:48 +0200515 }
516
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400517 input_report_key(dev->input, BTN_LEFT, dev->data[dev->datalen - 1] & 1);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500518 input_sync(dev->input);
Stelian Popf7214ff2005-09-08 10:19:48 +0200519
520exit:
521 retval = usb_submit_urb(dev->urb, GFP_ATOMIC);
522 if (retval) {
523 err("%s - usb_submit_urb failed with result %d",
524 __FUNCTION__, retval);
525 }
526}
527
528static int atp_open(struct input_dev *input)
529{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400530 struct atp *dev = input_get_drvdata(input);
Stelian Popf7214ff2005-09-08 10:19:48 +0200531
532 if (usb_submit_urb(dev->urb, GFP_ATOMIC))
533 return -EIO;
534
535 dev->open = 1;
536 return 0;
537}
538
539static void atp_close(struct input_dev *input)
540{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400541 struct atp *dev = input_get_drvdata(input);
Stelian Popf7214ff2005-09-08 10:19:48 +0200542
543 usb_kill_urb(dev->urb);
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400544 cancel_work_sync(&dev->work);
Stelian Popf7214ff2005-09-08 10:19:48 +0200545 dev->open = 0;
546}
547
548static int atp_probe(struct usb_interface *iface, const struct usb_device_id *id)
549{
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500550 struct atp *dev;
551 struct input_dev *input_dev;
552 struct usb_device *udev = interface_to_usbdev(iface);
Stelian Popf7214ff2005-09-08 10:19:48 +0200553 struct usb_host_interface *iface_desc;
554 struct usb_endpoint_descriptor *endpoint;
555 int int_in_endpointAddr = 0;
Dmitry Torokhov50141862007-04-12 01:33:39 -0400556 int i, error = -ENOMEM;
Stelian Popf7214ff2005-09-08 10:19:48 +0200557
558 /* set up the endpoint information */
559 /* use only the first interrupt-in endpoint */
560 iface_desc = iface->cur_altsetting;
561 for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
562 endpoint = &iface_desc->endpoint[i].desc;
Luiz Fernando N. Capitulino97b107c2006-09-27 11:58:53 -0700563 if (!int_in_endpointAddr && usb_endpoint_is_int_in(endpoint)) {
Stelian Popf7214ff2005-09-08 10:19:48 +0200564 /* we found an interrupt in endpoint */
565 int_in_endpointAddr = endpoint->bEndpointAddress;
566 break;
567 }
568 }
569 if (!int_in_endpointAddr) {
Stelian Popf7214ff2005-09-08 10:19:48 +0200570 err("Could not find int-in endpoint");
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500571 return -EIO;
Stelian Popf7214ff2005-09-08 10:19:48 +0200572 }
573
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500574 /* allocate memory for our device state and initialize it */
575 dev = kzalloc(sizeof(struct atp), GFP_KERNEL);
576 input_dev = input_allocate_device();
577 if (!dev || !input_dev) {
578 err("Out of memory");
579 goto err_free_devs;
580 }
581
582 dev->udev = udev;
583 dev->input = input_dev;
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500584 dev->overflowwarn = 0;
Nicolas Boichat9effa972006-04-19 23:36:40 +0200585 if (atp_is_geyser_3(dev))
586 dev->datalen = 64;
587 else if (atp_is_geyser_2(dev))
588 dev->datalen = 64;
589 else
590 dev->datalen = 81;
591
592 if (atp_is_geyser_3(dev)) {
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400593 /* switch to raw sensor mode */
594 if (atp_geyser3_init(udev))
Nicolas Boichat9effa972006-04-19 23:36:40 +0200595 goto err_free_devs;
Nicolas Boichat9effa972006-04-19 23:36:40 +0200596
Nicolas Boichat9effa972006-04-19 23:36:40 +0200597 printk("appletouch Geyser 3 inited.\n");
598 }
Stelian Popf7214ff2005-09-08 10:19:48 +0200599
600 dev->urb = usb_alloc_urb(0, GFP_KERNEL);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400601 if (!dev->urb)
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500602 goto err_free_devs;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500603
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500604 dev->data = usb_buffer_alloc(dev->udev, dev->datalen, GFP_KERNEL,
Stelian Popf7214ff2005-09-08 10:19:48 +0200605 &dev->urb->transfer_dma);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400606 if (!dev->data)
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500607 goto err_free_urb;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500608
609 usb_fill_int_urb(dev->urb, udev,
610 usb_rcvintpipe(udev, int_in_endpointAddr),
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500611 dev->data, dev->datalen, atp_complete, dev, 1);
Stelian Popf7214ff2005-09-08 10:19:48 +0200612
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500613 usb_make_path(udev, dev->phys, sizeof(dev->phys));
614 strlcat(dev->phys, "/input0", sizeof(dev->phys));
Stelian Popf7214ff2005-09-08 10:19:48 +0200615
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500616 input_dev->name = "appletouch";
617 input_dev->phys = dev->phys;
618 usb_to_input_id(dev->udev, &input_dev->id);
Dmitry Torokhovc0f82d52007-04-12 01:35:03 -0400619 input_dev->dev.parent = &iface->dev;
Stelian Popf7214ff2005-09-08 10:19:48 +0200620
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400621 input_set_drvdata(input_dev, dev);
622
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500623 input_dev->open = atp_open;
624 input_dev->close = atp_close;
625
626 set_bit(EV_ABS, input_dev->evbit);
Stelian Popf7214ff2005-09-08 10:19:48 +0200627
Nicolas Boichat9effa972006-04-19 23:36:40 +0200628 if (atp_is_geyser_3(dev)) {
629 /*
630 * MacBook have 20 X sensors, 10 Y sensors
631 */
632 input_set_abs_params(input_dev, ABS_X, 0,
633 ((20 - 1) * ATP_XFACT) - 1, ATP_FUZZ, 0);
634 input_set_abs_params(input_dev, ABS_Y, 0,
635 ((10 - 1) * ATP_YFACT) - 1, ATP_FUZZ, 0);
636 } else if (atp_is_geyser_2(dev)) {
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500637 /*
638 * Oct 2005 15" PowerBooks have 15 X sensors, 17" are detected
639 * later.
640 */
641 input_set_abs_params(input_dev, ABS_X, 0,
642 ((15 - 1) * ATP_XFACT) - 1, ATP_FUZZ, 0);
643 input_set_abs_params(input_dev, ABS_Y, 0,
644 ((9 - 1) * ATP_YFACT) - 1, ATP_FUZZ, 0);
645 } else {
646 /*
647 * 12" and 15" Powerbooks only have 16 x sensors,
648 * 17" models are detected later.
649 */
650 input_set_abs_params(input_dev, ABS_X, 0,
651 (16 - 1) * ATP_XFACT - 1, ATP_FUZZ, 0);
652 input_set_abs_params(input_dev, ABS_Y, 0,
653 (ATP_YSENSORS - 1) * ATP_YFACT - 1, ATP_FUZZ, 0);
654 }
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500655 input_set_abs_params(input_dev, ABS_PRESSURE, 0, ATP_PRESSURE, 0, 0);
Stelian Popf7214ff2005-09-08 10:19:48 +0200656
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500657 set_bit(EV_KEY, input_dev->evbit);
658 set_bit(BTN_TOUCH, input_dev->keybit);
659 set_bit(BTN_TOOL_FINGER, input_dev->keybit);
660 set_bit(BTN_TOOL_DOUBLETAP, input_dev->keybit);
661 set_bit(BTN_TOOL_TRIPLETAP, input_dev->keybit);
662 set_bit(BTN_LEFT, input_dev->keybit);
Stelian Popf7214ff2005-09-08 10:19:48 +0200663
Dmitry Torokhov50141862007-04-12 01:33:39 -0400664 error = input_register_device(dev->input);
665 if (error)
666 goto err_free_buffer;
Stelian Popf7214ff2005-09-08 10:19:48 +0200667
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500668 /* save our data pointer in this interface device */
669 usb_set_intfdata(iface, dev);
Stelian Popf7214ff2005-09-08 10:19:48 +0200670
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400671 INIT_WORK(&dev->work, atp_reinit);
672
Stelian Popf7214ff2005-09-08 10:19:48 +0200673 return 0;
674
Dmitry Torokhov50141862007-04-12 01:33:39 -0400675 err_free_buffer:
676 usb_buffer_free(dev->udev, dev->datalen,
677 dev->data, dev->urb->transfer_dma);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500678 err_free_urb:
Stelian Popf7214ff2005-09-08 10:19:48 +0200679 usb_free_urb(dev->urb);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500680 err_free_devs:
Stelian Popf7214ff2005-09-08 10:19:48 +0200681 usb_set_intfdata(iface, NULL);
Stelian Popf7214ff2005-09-08 10:19:48 +0200682 kfree(dev);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500683 input_free_device(input_dev);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400684 return error;
Stelian Popf7214ff2005-09-08 10:19:48 +0200685}
686
687static void atp_disconnect(struct usb_interface *iface)
688{
689 struct atp *dev = usb_get_intfdata(iface);
690
691 usb_set_intfdata(iface, NULL);
692 if (dev) {
693 usb_kill_urb(dev->urb);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500694 input_unregister_device(dev->input);
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500695 usb_buffer_free(dev->udev, dev->datalen,
Stelian Popf7214ff2005-09-08 10:19:48 +0200696 dev->data, dev->urb->transfer_dma);
Johannes Berg7af569a2006-07-19 15:39:46 +0200697 usb_free_urb(dev->urb);
Stelian Popf7214ff2005-09-08 10:19:48 +0200698 kfree(dev);
699 }
700 printk(KERN_INFO "input: appletouch disconnected\n");
701}
702
703static int atp_suspend(struct usb_interface *iface, pm_message_t message)
704{
705 struct atp *dev = usb_get_intfdata(iface);
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400706
Stelian Popf7214ff2005-09-08 10:19:48 +0200707 usb_kill_urb(dev->urb);
708 dev->valid = 0;
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400709
Stelian Popf7214ff2005-09-08 10:19:48 +0200710 return 0;
711}
712
713static int atp_resume(struct usb_interface *iface)
714{
715 struct atp *dev = usb_get_intfdata(iface);
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400716
Stelian Popf7214ff2005-09-08 10:19:48 +0200717 if (dev->open && usb_submit_urb(dev->urb, GFP_ATOMIC))
718 return -EIO;
719
720 return 0;
721}
722
723static struct usb_driver atp_driver = {
Stelian Popf7214ff2005-09-08 10:19:48 +0200724 .name = "appletouch",
725 .probe = atp_probe,
726 .disconnect = atp_disconnect,
727 .suspend = atp_suspend,
728 .resume = atp_resume,
729 .id_table = atp_table,
730};
731
732static int __init atp_init(void)
733{
734 return usb_register(&atp_driver);
735}
736
737static void __exit atp_exit(void)
738{
739 usb_deregister(&atp_driver);
740}
741
742module_init(atp_init);
743module_exit(atp_exit);