blob: b4423a471f027e03d8e94499b81b72ed935fcd12 [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
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400132/* Geyser initialization constants */
133#define ATP_GEYSER_MODE_READ_REQUEST_ID 1
134#define ATP_GEYSER_MODE_WRITE_REQUEST_ID 9
135#define ATP_GEYSER_MODE_REQUEST_VALUE 0x300
136#define ATP_GEYSER_MODE_REQUEST_INDEX 0
137#define ATP_GEYSER_MODE_VENDOR_VALUE 0x04
Nicolas Boichat9effa972006-04-19 23:36:40 +0200138
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 */
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400145 struct input_dev * input; /* input dev */
146 unsigned char open; /* non-zero if opened */
147 unsigned char valid; /* are the sensors valid ? */
148 unsigned char size_detect_done;
149 unsigned char overflowwarn; /* overflow warning printed? */
Stelian Popf7214ff2005-09-08 10:19:48 +0200150 int x_old; /* last reported x/y, */
151 int y_old; /* used for smoothing */
152 /* current value of the sensors */
153 signed char xy_cur[ATP_XSENSORS + ATP_YSENSORS];
154 /* last value of the sensors */
155 signed char xy_old[ATP_XSENSORS + ATP_YSENSORS];
156 /* accumulated sensors */
157 int xy_acc[ATP_XSENSORS + ATP_YSENSORS];
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500158 int datalen; /* size of an USB urb transfer */
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400159 int idlecount; /* number of empty packets */
160 struct work_struct work;
Stelian Popf7214ff2005-09-08 10:19:48 +0200161};
162
163#define dbg_dump(msg, tab) \
164 if (debug > 1) { \
165 int i; \
166 printk("appletouch: %s %lld", msg, (long long)jiffies); \
167 for (i = 0; i < ATP_XSENSORS + ATP_YSENSORS; i++) \
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500168 printk(" %02x", tab[i]); \
169 printk("\n"); \
Stelian Popf7214ff2005-09-08 10:19:48 +0200170 }
171
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500172#define dprintk(format, a...) \
Stelian Popf7214ff2005-09-08 10:19:48 +0200173 do { \
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400174 if (debug) printk(KERN_DEBUG format, ##a); \
Stelian Popf7214ff2005-09-08 10:19:48 +0200175 } while (0)
176
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500177MODULE_AUTHOR("Johannes Berg, Stelian Pop, Frank Arnold, Michael Hanselmann");
Stelian Popf7214ff2005-09-08 10:19:48 +0200178MODULE_DESCRIPTION("Apple PowerBooks USB touchpad driver");
179MODULE_LICENSE("GPL");
180
Jason Parekh00081d32006-11-17 01:05:58 -0500181/*
182 * Make the threshold a module parameter
183 */
184static int threshold = ATP_THRESHOLD;
185module_param(threshold, int, 0644);
186MODULE_PARM_DESC(threshold, "Discards any change in data from a sensor (trackpad has hundreds of these sensors) less than this value");
187
Stelian Popf7214ff2005-09-08 10:19:48 +0200188static int debug = 1;
189module_param(debug, int, 0644);
190MODULE_PARM_DESC(debug, "Activate debugging output");
191
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400192static inline int atp_is_fountain(struct atp *dev)
193{
194 u16 productId = le16_to_cpu(dev->udev->descriptor.idProduct);
195
196 return productId == FOUNTAIN_ANSI_PRODUCT_ID ||
197 productId == FOUNTAIN_ISO_PRODUCT_ID ||
198 productId == FOUNTAIN_TP_ONLY_PRODUCT_ID;
199}
200
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500201/* Checks if the device a Geyser 2 (ANSI, ISO, JIS) */
202static inline int atp_is_geyser_2(struct atp *dev)
203{
Nicolas Boichat9effa972006-04-19 23:36:40 +0200204 u16 productId = le16_to_cpu(dev->udev->descriptor.idProduct);
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500205
206 return (productId == GEYSER_ANSI_PRODUCT_ID) ||
207 (productId == GEYSER_ISO_PRODUCT_ID) ||
208 (productId == GEYSER_JIS_PRODUCT_ID);
209}
210
Nicolas Boichat9effa972006-04-19 23:36:40 +0200211static inline int atp_is_geyser_3(struct atp *dev)
212{
213 u16 productId = le16_to_cpu(dev->udev->descriptor.idProduct);
214
215 return (productId == GEYSER3_ANSI_PRODUCT_ID) ||
216 (productId == GEYSER3_ISO_PRODUCT_ID) ||
Julien BLACHE009ad0e2006-11-17 01:06:13 -0500217 (productId == GEYSER3_JIS_PRODUCT_ID) ||
218 (productId == GEYSER4_ANSI_PRODUCT_ID) ||
219 (productId == GEYSER4_ISO_PRODUCT_ID) ||
220 (productId == GEYSER4_JIS_PRODUCT_ID);
Nicolas Boichat9effa972006-04-19 23:36:40 +0200221}
222
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400223/*
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400224 * By default newer Geyser devices send standard USB HID mouse
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400225 * packets (Report ID 2). This code changes device mode, so it
226 * sends raw sensor reports (Report ID 5).
227 */
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400228static int atp_geyser_init(struct usb_device *udev)
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400229{
230 char data[8];
231 int size;
232
233 size = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400234 ATP_GEYSER_MODE_READ_REQUEST_ID,
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400235 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400236 ATP_GEYSER_MODE_REQUEST_VALUE,
237 ATP_GEYSER_MODE_REQUEST_INDEX, &data, 8, 5000);
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400238
239 if (size != 8) {
240 err("Could not do mode read request from device"
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400241 " (Geyser Raw mode)");
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400242 return -EIO;
243 }
244
245 /* Apply the mode switch */
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400246 data[0] = ATP_GEYSER_MODE_VENDOR_VALUE;
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400247
248 size = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400249 ATP_GEYSER_MODE_WRITE_REQUEST_ID,
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400250 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400251 ATP_GEYSER_MODE_REQUEST_VALUE,
252 ATP_GEYSER_MODE_REQUEST_INDEX, &data, 8, 5000);
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400253
254 if (size != 8) {
255 err("Could not do mode write request to device"
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400256 " (Geyser Raw mode)");
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400257 return -EIO;
258 }
259 return 0;
260}
261
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400262/*
263 * Reinitialise the device. This usually stops stream of empty packets
264 * coming from it.
265 */
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400266static void atp_reinit(struct work_struct *work)
267{
268 struct atp *dev = container_of(work, struct atp, work);
269 struct usb_device *udev = dev->udev;
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400270 int retval;
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400271
272 dev->idlecount = 0;
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400273
274 atp_geyser_init(udev);
275
276 retval = usb_submit_urb(dev->urb, GFP_ATOMIC);
277 if (retval) {
278 err("%s - usb_submit_urb failed with result %d",
279 __FUNCTION__, retval);
280 }
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400281}
282
Stelian Popf7214ff2005-09-08 10:19:48 +0200283static int atp_calculate_abs(int *xy_sensors, int nb_sensors, int fact,
284 int *z, int *fingers)
285{
286 int i;
287 /* values to calculate mean */
288 int pcum = 0, psum = 0;
Jason Parekh00081d32006-11-17 01:05:58 -0500289 int is_increasing = 0;
Stelian Popf7214ff2005-09-08 10:19:48 +0200290
291 *fingers = 0;
292
293 for (i = 0; i < nb_sensors; i++) {
Jason Parekh00081d32006-11-17 01:05:58 -0500294 if (xy_sensors[i] < threshold) {
295 if (is_increasing)
296 is_increasing = 0;
297
Stelian Popf7214ff2005-09-08 10:19:48 +0200298 continue;
Jason Parekh00081d32006-11-17 01:05:58 -0500299 }
300
301 /*
302 * Makes the finger detection more versatile. For example,
303 * two fingers with no gap will be detected. Also, my
304 * tests show it less likely to have intermittent loss
305 * of multiple finger readings while moving around (scrolling).
306 *
307 * Changes the multiple finger detection to counting humps on
308 * sensors (transitions from nonincreasing to increasing)
309 * instead of counting transitions from low sensors (no
310 * finger reading) to high sensors (finger above
311 * sensor)
312 *
313 * - Jason Parekh <jasonparekh@gmail.com>
314 */
315 if (i < 1 || (!is_increasing && xy_sensors[i - 1] < xy_sensors[i])) {
Stelian Popf7214ff2005-09-08 10:19:48 +0200316 (*fingers)++;
Jason Parekh00081d32006-11-17 01:05:58 -0500317 is_increasing = 1;
318 } else if (i > 0 && xy_sensors[i - 1] >= xy_sensors[i]) {
319 is_increasing = 0;
320 }
321
322 /*
323 * Subtracts threshold so a high sensor that just passes the threshold
324 * won't skew the calculated absolute coordinate. Fixes an issue
325 * where slowly moving the mouse would occassionaly jump a number of
326 * pixels (let me restate--slowly moving the mouse makes this issue
327 * most apparent).
328 */
329 pcum += (xy_sensors[i] - threshold) * i;
330 psum += (xy_sensors[i] - threshold);
Stelian Popf7214ff2005-09-08 10:19:48 +0200331 }
332
333 if (psum > 0) {
334 *z = psum;
335 return pcum * fact / psum;
336 }
337
338 return 0;
339}
340
341static inline void atp_report_fingers(struct input_dev *input, int fingers)
342{
343 input_report_key(input, BTN_TOOL_FINGER, fingers == 1);
344 input_report_key(input, BTN_TOOL_DOUBLETAP, fingers == 2);
345 input_report_key(input, BTN_TOOL_TRIPLETAP, fingers > 2);
346}
347
David Howells7d12e782006-10-05 14:55:46 +0100348static void atp_complete(struct urb* urb)
Stelian Popf7214ff2005-09-08 10:19:48 +0200349{
350 int x, y, x_z, y_z, x_f, y_f;
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500351 int retval, i, j;
Thomas Rohwercb560732007-09-25 00:06:25 -0400352 int key;
Stelian Popf7214ff2005-09-08 10:19:48 +0200353 struct atp *dev = urb->context;
354
355 switch (urb->status) {
356 case 0:
357 /* success */
358 break;
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500359 case -EOVERFLOW:
360 if(!dev->overflowwarn) {
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400361 printk(KERN_WARNING "appletouch: OVERFLOW with data "
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500362 "length %d, actual length is %d\n",
363 dev->datalen, dev->urb->actual_length);
364 dev->overflowwarn = 1;
365 }
Stelian Popf7214ff2005-09-08 10:19:48 +0200366 case -ECONNRESET:
367 case -ENOENT:
368 case -ESHUTDOWN:
369 /* This urb is terminated, clean up */
370 dbg("%s - urb shutting down with status: %d",
371 __FUNCTION__, urb->status);
372 return;
373 default:
374 dbg("%s - nonzero urb status received: %d",
375 __FUNCTION__, urb->status);
376 goto exit;
377 }
378
379 /* drop incomplete datasets */
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500380 if (dev->urb->actual_length != dev->datalen) {
Nicolas Boichat9effa972006-04-19 23:36:40 +0200381 dprintk("appletouch: incomplete data package"
382 " (first byte: %d, length: %d).\n",
383 dev->data[0], dev->urb->actual_length);
Stelian Popf7214ff2005-09-08 10:19:48 +0200384 goto exit;
385 }
386
387 /* reorder the sensors values */
Nicolas Boichat9effa972006-04-19 23:36:40 +0200388 if (atp_is_geyser_3(dev)) {
389 memset(dev->xy_cur, 0, sizeof(dev->xy_cur));
390
391 /*
392 * The values are laid out like this:
393 * -, Y1, Y2, -, Y3, Y4, -, ..., -, X1, X2, -, X3, X4, ...
394 * '-' is an unused value.
395 */
396
397 /* read X values */
398 for (i = 0, j = 19; i < 20; i += 2, j += 3) {
399 dev->xy_cur[i] = dev->data[j + 1];
400 dev->xy_cur[i + 1] = dev->data[j + 2];
401 }
402 /* read Y values */
403 for (i = 0, j = 1; i < 9; i += 2, j += 3) {
404 dev->xy_cur[ATP_XSENSORS + i] = dev->data[j + 1];
405 dev->xy_cur[ATP_XSENSORS + i + 1] = dev->data[j + 2];
406 }
407 } else if (atp_is_geyser_2(dev)) {
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500408 memset(dev->xy_cur, 0, sizeof(dev->xy_cur));
Stelian Popf7214ff2005-09-08 10:19:48 +0200409
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500410 /*
411 * The values are laid out like this:
412 * Y1, Y2, -, Y3, Y4, -, ..., X1, X2, -, X3, X4, -, ...
413 * '-' is an unused value.
414 */
415
416 /* read X values */
417 for (i = 0, j = 19; i < 20; i += 2, j += 3) {
418 dev->xy_cur[i] = dev->data[j];
419 dev->xy_cur[i + 1] = dev->data[j + 1];
420 }
421
422 /* read Y values */
423 for (i = 0, j = 1; i < 9; i += 2, j += 3) {
424 dev->xy_cur[ATP_XSENSORS + i] = dev->data[j];
425 dev->xy_cur[ATP_XSENSORS + i + 1] = dev->data[j + 1];
426 }
427 } else {
428 for (i = 0; i < 8; i++) {
429 /* X values */
430 dev->xy_cur[i ] = dev->data[5 * i + 2];
431 dev->xy_cur[i + 8] = dev->data[5 * i + 4];
432 dev->xy_cur[i + 16] = dev->data[5 * i + 42];
433 if (i < 2)
434 dev->xy_cur[i + 24] = dev->data[5 * i + 44];
435
436 /* Y values */
437 dev->xy_cur[i + 26] = dev->data[5 * i + 1];
438 dev->xy_cur[i + 34] = dev->data[5 * i + 3];
439 }
Stelian Popf7214ff2005-09-08 10:19:48 +0200440 }
441
442 dbg_dump("sample", dev->xy_cur);
443
444 if (!dev->valid) {
445 /* first sample */
446 dev->valid = 1;
447 dev->x_old = dev->y_old = -1;
448 memcpy(dev->xy_old, dev->xy_cur, sizeof(dev->xy_old));
449
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400450 if (dev->size_detect_done ||
451 atp_is_geyser_3(dev)) /* No 17" Macbooks (yet) */
Nicolas Boichat9effa972006-04-19 23:36:40 +0200452 goto exit;
453
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500454 /* 17" Powerbooks have extra X sensors */
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400455 for (i = (atp_is_geyser_2(dev) ? 15 : 16); i < ATP_XSENSORS; i++) {
456 if (!dev->xy_cur[i])
457 continue;
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500458
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400459 printk(KERN_INFO "appletouch: 17\" model detected.\n");
460 if (atp_is_geyser_2(dev))
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500461 input_set_abs_params(dev->input, ABS_X, 0,
462 (20 - 1) *
463 ATP_XFACT - 1,
464 ATP_FUZZ, 0);
465 else
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500466 input_set_abs_params(dev->input, ABS_X, 0,
467 (ATP_XSENSORS - 1) *
Stelian Popf7214ff2005-09-08 10:19:48 +0200468 ATP_XFACT - 1,
469 ATP_FUZZ, 0);
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500470 break;
471 }
Stelian Popf7214ff2005-09-08 10:19:48 +0200472
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400473 dev->size_detect_done = 1;
Stelian Popf7214ff2005-09-08 10:19:48 +0200474 goto exit;
475 }
476
477 for (i = 0; i < ATP_XSENSORS + ATP_YSENSORS; i++) {
478 /* accumulate the change */
479 signed char change = dev->xy_old[i] - dev->xy_cur[i];
480 dev->xy_acc[i] -= change;
481
482 /* prevent down drifting */
483 if (dev->xy_acc[i] < 0)
484 dev->xy_acc[i] = 0;
485 }
486
487 memcpy(dev->xy_old, dev->xy_cur, sizeof(dev->xy_old));
488
489 dbg_dump("accumulator", dev->xy_acc);
490
491 x = atp_calculate_abs(dev->xy_acc, ATP_XSENSORS,
492 ATP_XFACT, &x_z, &x_f);
493 y = atp_calculate_abs(dev->xy_acc + ATP_XSENSORS, ATP_YSENSORS,
494 ATP_YFACT, &y_z, &y_f);
Thomas Rohwercb560732007-09-25 00:06:25 -0400495 key = dev->data[dev->datalen - 1] & 1;
Stelian Popf7214ff2005-09-08 10:19:48 +0200496
497 if (x && y) {
498 if (dev->x_old != -1) {
499 x = (dev->x_old * 3 + x) >> 2;
500 y = (dev->y_old * 3 + y) >> 2;
501 dev->x_old = x;
502 dev->y_old = y;
503
504 if (debug > 1)
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400505 printk(KERN_DEBUG "appletouch: X: %3d Y: %3d "
Stelian Popf7214ff2005-09-08 10:19:48 +0200506 "Xz: %3d Yz: %3d\n",
507 x, y, x_z, y_z);
508
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500509 input_report_key(dev->input, BTN_TOUCH, 1);
510 input_report_abs(dev->input, ABS_X, x);
511 input_report_abs(dev->input, ABS_Y, y);
512 input_report_abs(dev->input, ABS_PRESSURE,
Stelian Popf7214ff2005-09-08 10:19:48 +0200513 min(ATP_PRESSURE, x_z + y_z));
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500514 atp_report_fingers(dev->input, max(x_f, y_f));
Stelian Popf7214ff2005-09-08 10:19:48 +0200515 }
516 dev->x_old = x;
517 dev->y_old = y;
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400518
519 } else if (!x && !y) {
Stelian Popf7214ff2005-09-08 10:19:48 +0200520
521 dev->x_old = dev->y_old = -1;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500522 input_report_key(dev->input, BTN_TOUCH, 0);
523 input_report_abs(dev->input, ABS_PRESSURE, 0);
524 atp_report_fingers(dev->input, 0);
Stelian Popf7214ff2005-09-08 10:19:48 +0200525
526 /* reset the accumulator on release */
527 memset(dev->xy_acc, 0, sizeof(dev->xy_acc));
Soeren Sonnenburg937ad5c2007-10-13 00:31:15 -0400528 }
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400529
Anton Ekblad46249ea2007-10-22 00:59:59 -0400530 input_report_key(dev->input, BTN_LEFT, key);
531 input_sync(dev->input);
532
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400533 /*
534 * Many Geysers will continue to send packets continually after
535 * the first touch unless reinitialised. Do so if it's been
536 * idle for a while in order to avoid waking the kernel up
537 * several hundred times a second. Re-initialization does not
538 * work on Fountain touchpads.
539 */
540 if (!atp_is_fountain(dev)) {
541 if (!x && !y && !key) {
542 dev->idlecount++;
543 if (dev->idlecount == 10) {
544 dev->valid = 0;
545 schedule_work(&dev->work);
546 /* Don't resubmit urb here, wait for reinit */
547 return;
548 }
549 } else
550 dev->idlecount = 0;
551 }
Stelian Popf7214ff2005-09-08 10:19:48 +0200552
553exit:
554 retval = usb_submit_urb(dev->urb, GFP_ATOMIC);
555 if (retval) {
556 err("%s - usb_submit_urb failed with result %d",
557 __FUNCTION__, retval);
558 }
559}
560
561static int atp_open(struct input_dev *input)
562{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400563 struct atp *dev = input_get_drvdata(input);
Stelian Popf7214ff2005-09-08 10:19:48 +0200564
565 if (usb_submit_urb(dev->urb, GFP_ATOMIC))
566 return -EIO;
567
568 dev->open = 1;
569 return 0;
570}
571
572static void atp_close(struct input_dev *input)
573{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400574 struct atp *dev = input_get_drvdata(input);
Stelian Popf7214ff2005-09-08 10:19:48 +0200575
576 usb_kill_urb(dev->urb);
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400577 cancel_work_sync(&dev->work);
Stelian Popf7214ff2005-09-08 10:19:48 +0200578 dev->open = 0;
579}
580
581static int atp_probe(struct usb_interface *iface, const struct usb_device_id *id)
582{
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500583 struct atp *dev;
584 struct input_dev *input_dev;
585 struct usb_device *udev = interface_to_usbdev(iface);
Stelian Popf7214ff2005-09-08 10:19:48 +0200586 struct usb_host_interface *iface_desc;
587 struct usb_endpoint_descriptor *endpoint;
588 int int_in_endpointAddr = 0;
Dmitry Torokhov50141862007-04-12 01:33:39 -0400589 int i, error = -ENOMEM;
Stelian Popf7214ff2005-09-08 10:19:48 +0200590
591 /* set up the endpoint information */
592 /* use only the first interrupt-in endpoint */
593 iface_desc = iface->cur_altsetting;
594 for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
595 endpoint = &iface_desc->endpoint[i].desc;
Luiz Fernando N. Capitulino97b107c2006-09-27 11:58:53 -0700596 if (!int_in_endpointAddr && usb_endpoint_is_int_in(endpoint)) {
Stelian Popf7214ff2005-09-08 10:19:48 +0200597 /* we found an interrupt in endpoint */
598 int_in_endpointAddr = endpoint->bEndpointAddress;
599 break;
600 }
601 }
602 if (!int_in_endpointAddr) {
Stelian Popf7214ff2005-09-08 10:19:48 +0200603 err("Could not find int-in endpoint");
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500604 return -EIO;
Stelian Popf7214ff2005-09-08 10:19:48 +0200605 }
606
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500607 /* allocate memory for our device state and initialize it */
608 dev = kzalloc(sizeof(struct atp), GFP_KERNEL);
609 input_dev = input_allocate_device();
610 if (!dev || !input_dev) {
611 err("Out of memory");
612 goto err_free_devs;
613 }
614
615 dev->udev = udev;
616 dev->input = input_dev;
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500617 dev->overflowwarn = 0;
Nicolas Boichat9effa972006-04-19 23:36:40 +0200618 if (atp_is_geyser_3(dev))
619 dev->datalen = 64;
620 else if (atp_is_geyser_2(dev))
621 dev->datalen = 64;
622 else
623 dev->datalen = 81;
624
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400625 if (!atp_is_fountain(dev)) {
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400626 /* switch to raw sensor mode */
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400627 if (atp_geyser_init(udev))
Nicolas Boichat9effa972006-04-19 23:36:40 +0200628 goto err_free_devs;
Nicolas Boichat9effa972006-04-19 23:36:40 +0200629
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400630 printk(KERN_INFO "appletouch: Geyser mode initialized.\n");
Nicolas Boichat9effa972006-04-19 23:36:40 +0200631 }
Stelian Popf7214ff2005-09-08 10:19:48 +0200632
633 dev->urb = usb_alloc_urb(0, GFP_KERNEL);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400634 if (!dev->urb)
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500635 goto err_free_devs;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500636
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500637 dev->data = usb_buffer_alloc(dev->udev, dev->datalen, GFP_KERNEL,
Stelian Popf7214ff2005-09-08 10:19:48 +0200638 &dev->urb->transfer_dma);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400639 if (!dev->data)
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500640 goto err_free_urb;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500641
642 usb_fill_int_urb(dev->urb, udev,
643 usb_rcvintpipe(udev, int_in_endpointAddr),
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500644 dev->data, dev->datalen, atp_complete, dev, 1);
Stelian Popf7214ff2005-09-08 10:19:48 +0200645
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500646 usb_make_path(udev, dev->phys, sizeof(dev->phys));
647 strlcat(dev->phys, "/input0", sizeof(dev->phys));
Stelian Popf7214ff2005-09-08 10:19:48 +0200648
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500649 input_dev->name = "appletouch";
650 input_dev->phys = dev->phys;
651 usb_to_input_id(dev->udev, &input_dev->id);
Dmitry Torokhovc0f82d52007-04-12 01:35:03 -0400652 input_dev->dev.parent = &iface->dev;
Stelian Popf7214ff2005-09-08 10:19:48 +0200653
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400654 input_set_drvdata(input_dev, dev);
655
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500656 input_dev->open = atp_open;
657 input_dev->close = atp_close;
658
659 set_bit(EV_ABS, input_dev->evbit);
Stelian Popf7214ff2005-09-08 10:19:48 +0200660
Nicolas Boichat9effa972006-04-19 23:36:40 +0200661 if (atp_is_geyser_3(dev)) {
662 /*
663 * MacBook have 20 X sensors, 10 Y sensors
664 */
665 input_set_abs_params(input_dev, ABS_X, 0,
666 ((20 - 1) * ATP_XFACT) - 1, ATP_FUZZ, 0);
667 input_set_abs_params(input_dev, ABS_Y, 0,
668 ((10 - 1) * ATP_YFACT) - 1, ATP_FUZZ, 0);
669 } else if (atp_is_geyser_2(dev)) {
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500670 /*
671 * Oct 2005 15" PowerBooks have 15 X sensors, 17" are detected
672 * later.
673 */
674 input_set_abs_params(input_dev, ABS_X, 0,
675 ((15 - 1) * ATP_XFACT) - 1, ATP_FUZZ, 0);
676 input_set_abs_params(input_dev, ABS_Y, 0,
677 ((9 - 1) * ATP_YFACT) - 1, ATP_FUZZ, 0);
678 } else {
679 /*
680 * 12" and 15" Powerbooks only have 16 x sensors,
681 * 17" models are detected later.
682 */
683 input_set_abs_params(input_dev, ABS_X, 0,
684 (16 - 1) * ATP_XFACT - 1, ATP_FUZZ, 0);
685 input_set_abs_params(input_dev, ABS_Y, 0,
686 (ATP_YSENSORS - 1) * ATP_YFACT - 1, ATP_FUZZ, 0);
687 }
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500688 input_set_abs_params(input_dev, ABS_PRESSURE, 0, ATP_PRESSURE, 0, 0);
Stelian Popf7214ff2005-09-08 10:19:48 +0200689
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500690 set_bit(EV_KEY, input_dev->evbit);
691 set_bit(BTN_TOUCH, input_dev->keybit);
692 set_bit(BTN_TOOL_FINGER, input_dev->keybit);
693 set_bit(BTN_TOOL_DOUBLETAP, input_dev->keybit);
694 set_bit(BTN_TOOL_TRIPLETAP, input_dev->keybit);
695 set_bit(BTN_LEFT, input_dev->keybit);
Stelian Popf7214ff2005-09-08 10:19:48 +0200696
Dmitry Torokhov50141862007-04-12 01:33:39 -0400697 error = input_register_device(dev->input);
698 if (error)
699 goto err_free_buffer;
Stelian Popf7214ff2005-09-08 10:19:48 +0200700
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500701 /* save our data pointer in this interface device */
702 usb_set_intfdata(iface, dev);
Stelian Popf7214ff2005-09-08 10:19:48 +0200703
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400704 INIT_WORK(&dev->work, atp_reinit);
705
Stelian Popf7214ff2005-09-08 10:19:48 +0200706 return 0;
707
Dmitry Torokhov50141862007-04-12 01:33:39 -0400708 err_free_buffer:
709 usb_buffer_free(dev->udev, dev->datalen,
710 dev->data, dev->urb->transfer_dma);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500711 err_free_urb:
Stelian Popf7214ff2005-09-08 10:19:48 +0200712 usb_free_urb(dev->urb);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500713 err_free_devs:
Stelian Popf7214ff2005-09-08 10:19:48 +0200714 usb_set_intfdata(iface, NULL);
Stelian Popf7214ff2005-09-08 10:19:48 +0200715 kfree(dev);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500716 input_free_device(input_dev);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400717 return error;
Stelian Popf7214ff2005-09-08 10:19:48 +0200718}
719
720static void atp_disconnect(struct usb_interface *iface)
721{
722 struct atp *dev = usb_get_intfdata(iface);
723
724 usb_set_intfdata(iface, NULL);
725 if (dev) {
726 usb_kill_urb(dev->urb);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500727 input_unregister_device(dev->input);
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500728 usb_buffer_free(dev->udev, dev->datalen,
Stelian Popf7214ff2005-09-08 10:19:48 +0200729 dev->data, dev->urb->transfer_dma);
Johannes Berg7af569a2006-07-19 15:39:46 +0200730 usb_free_urb(dev->urb);
Stelian Popf7214ff2005-09-08 10:19:48 +0200731 kfree(dev);
732 }
733 printk(KERN_INFO "input: appletouch disconnected\n");
734}
735
736static int atp_suspend(struct usb_interface *iface, pm_message_t message)
737{
738 struct atp *dev = usb_get_intfdata(iface);
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400739
Stelian Popf7214ff2005-09-08 10:19:48 +0200740 usb_kill_urb(dev->urb);
741 dev->valid = 0;
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400742
Stelian Popf7214ff2005-09-08 10:19:48 +0200743 return 0;
744}
745
746static int atp_resume(struct usb_interface *iface)
747{
748 struct atp *dev = usb_get_intfdata(iface);
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400749
Stelian Popf7214ff2005-09-08 10:19:48 +0200750 if (dev->open && usb_submit_urb(dev->urb, GFP_ATOMIC))
751 return -EIO;
752
753 return 0;
754}
755
756static struct usb_driver atp_driver = {
Stelian Popf7214ff2005-09-08 10:19:48 +0200757 .name = "appletouch",
758 .probe = atp_probe,
759 .disconnect = atp_disconnect,
760 .suspend = atp_suspend,
761 .resume = atp_resume,
762 .id_table = atp_table,
763};
764
765static int __init atp_init(void)
766{
767 return usb_register(&atp_driver);
768}
769
770static void __exit atp_exit(void)
771{
772 usb_deregister(&atp_driver);
773}
774
775module_init(atp_init);
776module_exit(atp_exit);