blob: 69c445620f15b436eccab5ed8a218a60d10b03f8 [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 */
41#define GEYSER_ANSI_PRODUCT_ID 0x0214
42#define GEYSER_ISO_PRODUCT_ID 0x0215
43#define GEYSER_JIS_PRODUCT_ID 0x0216
44
Nicolas Boichat9effa972006-04-19 23:36:40 +020045/* MacBook devices */
46#define GEYSER3_ANSI_PRODUCT_ID 0x0217
47#define GEYSER3_ISO_PRODUCT_ID 0x0218
48#define GEYSER3_JIS_PRODUCT_ID 0x0219
49
Julien BLACHE009ad0e2006-11-17 01:06:13 -050050/*
51 * Geyser IV: same as Geyser III according to Info.plist in AppleUSBTrackpad.kext
52 * -> same IOClass (AppleUSBGrIIITrackpad), same acceleration tables
53 */
54#define GEYSER4_ANSI_PRODUCT_ID 0x021A
55#define GEYSER4_ISO_PRODUCT_ID 0x021B
56#define GEYSER4_JIS_PRODUCT_ID 0x021C
57
Stelian Popf7214ff2005-09-08 10:19:48 +020058#define ATP_DEVICE(prod) \
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -050059 .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \
Stelian Popf7214ff2005-09-08 10:19:48 +020060 USB_DEVICE_ID_MATCH_INT_CLASS | \
61 USB_DEVICE_ID_MATCH_INT_PROTOCOL, \
62 .idVendor = APPLE_VENDOR_ID, \
63 .idProduct = (prod), \
64 .bInterfaceClass = 0x03, \
65 .bInterfaceProtocol = 0x02
66
67/* table of devices that work with this driver */
68static struct usb_device_id atp_table [] = {
69 { ATP_DEVICE(0x020E) },
70 { ATP_DEVICE(0x020F) },
71 { ATP_DEVICE(0x030A) },
72 { ATP_DEVICE(0x030B) },
Michael Hanselmanne1e02c92005-12-21 00:50:23 -050073
74 /* PowerBooks Oct 2005 */
75 { ATP_DEVICE(GEYSER_ANSI_PRODUCT_ID) },
76 { ATP_DEVICE(GEYSER_ISO_PRODUCT_ID) },
77 { ATP_DEVICE(GEYSER_JIS_PRODUCT_ID) },
78
Julien BLACHE009ad0e2006-11-17 01:06:13 -050079 /* Core Duo MacBook & MacBook Pro */
Nicolas Boichat9effa972006-04-19 23:36:40 +020080 { ATP_DEVICE(GEYSER3_ANSI_PRODUCT_ID) },
81 { ATP_DEVICE(GEYSER3_ISO_PRODUCT_ID) },
82 { ATP_DEVICE(GEYSER3_JIS_PRODUCT_ID) },
83
Julien BLACHE009ad0e2006-11-17 01:06:13 -050084 /* Core2 Duo MacBook & MacBook Pro */
85 { ATP_DEVICE(GEYSER4_ANSI_PRODUCT_ID) },
86 { ATP_DEVICE(GEYSER4_ISO_PRODUCT_ID) },
87 { ATP_DEVICE(GEYSER4_JIS_PRODUCT_ID) },
88
Michael Hanselmanne1e02c92005-12-21 00:50:23 -050089 /* Terminating entry */
90 { }
Stelian Popf7214ff2005-09-08 10:19:48 +020091};
92MODULE_DEVICE_TABLE (usb, atp_table);
93
Stelian Popf7214ff2005-09-08 10:19:48 +020094/*
95 * number of sensors. Note that only 16 instead of 26 X (horizontal)
96 * sensors exist on 12" and 15" PowerBooks. All models have 16 Y
97 * (vertical) sensors.
98 */
99#define ATP_XSENSORS 26
100#define ATP_YSENSORS 16
101
102/* amount of fuzz this touchpad generates */
103#define ATP_FUZZ 16
104
105/* maximum pressure this driver will report */
106#define ATP_PRESSURE 300
107/*
108 * multiplication factor for the X and Y coordinates.
109 * We try to keep the touchpad aspect ratio while still doing only simple
110 * arithmetics.
111 * The factors below give coordinates like:
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500112 * 0 <= x < 960 on 12" and 15" Powerbooks
113 * 0 <= x < 1600 on 17" Powerbooks
114 * 0 <= y < 646
Stelian Popf7214ff2005-09-08 10:19:48 +0200115 */
116#define ATP_XFACT 64
117#define ATP_YFACT 43
118
119/*
120 * Threshold for the touchpad sensors. Any change less than ATP_THRESHOLD is
121 * ignored.
122 */
123#define ATP_THRESHOLD 5
124
Julien BLACHE009ad0e2006-11-17 01:06:13 -0500125/* MacBook Pro (Geyser 3 & 4) initialization constants */
Nicolas Boichat9effa972006-04-19 23:36:40 +0200126#define ATP_GEYSER3_MODE_READ_REQUEST_ID 1
127#define ATP_GEYSER3_MODE_WRITE_REQUEST_ID 9
128#define ATP_GEYSER3_MODE_REQUEST_VALUE 0x300
129#define ATP_GEYSER3_MODE_REQUEST_INDEX 0
130#define ATP_GEYSER3_MODE_VENDOR_VALUE 0x04
131
Stelian Popf7214ff2005-09-08 10:19:48 +0200132/* Structure to hold all of our device specific stuff */
133struct atp {
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500134 char phys[64];
Stelian Popf7214ff2005-09-08 10:19:48 +0200135 struct usb_device * udev; /* usb device */
136 struct urb * urb; /* usb request block */
137 signed char * data; /* transferred data */
138 int open; /* non-zero if opened */
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500139 struct input_dev *input; /* input dev */
Stelian Popf7214ff2005-09-08 10:19:48 +0200140 int valid; /* are the sensors valid ? */
141 int x_old; /* last reported x/y, */
142 int y_old; /* used for smoothing */
143 /* current value of the sensors */
144 signed char xy_cur[ATP_XSENSORS + ATP_YSENSORS];
145 /* last value of the sensors */
146 signed char xy_old[ATP_XSENSORS + ATP_YSENSORS];
147 /* accumulated sensors */
148 int xy_acc[ATP_XSENSORS + ATP_YSENSORS];
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500149 int overflowwarn; /* overflow warning printed? */
150 int datalen; /* size of an USB urb transfer */
Stelian Popf7214ff2005-09-08 10:19:48 +0200151};
152
153#define dbg_dump(msg, tab) \
154 if (debug > 1) { \
155 int i; \
156 printk("appletouch: %s %lld", msg, (long long)jiffies); \
157 for (i = 0; i < ATP_XSENSORS + ATP_YSENSORS; i++) \
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500158 printk(" %02x", tab[i]); \
159 printk("\n"); \
Stelian Popf7214ff2005-09-08 10:19:48 +0200160 }
161
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500162#define dprintk(format, a...) \
Stelian Popf7214ff2005-09-08 10:19:48 +0200163 do { \
164 if (debug) printk(format, ##a); \
165 } while (0)
166
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500167MODULE_AUTHOR("Johannes Berg, Stelian Pop, Frank Arnold, Michael Hanselmann");
Stelian Popf7214ff2005-09-08 10:19:48 +0200168MODULE_DESCRIPTION("Apple PowerBooks USB touchpad driver");
169MODULE_LICENSE("GPL");
170
Jason Parekh00081d32006-11-17 01:05:58 -0500171/*
172 * Make the threshold a module parameter
173 */
174static int threshold = ATP_THRESHOLD;
175module_param(threshold, int, 0644);
176MODULE_PARM_DESC(threshold, "Discards any change in data from a sensor (trackpad has hundreds of these sensors) less than this value");
177
Stelian Popf7214ff2005-09-08 10:19:48 +0200178static int debug = 1;
179module_param(debug, int, 0644);
180MODULE_PARM_DESC(debug, "Activate debugging output");
181
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500182/* Checks if the device a Geyser 2 (ANSI, ISO, JIS) */
183static inline int atp_is_geyser_2(struct atp *dev)
184{
Nicolas Boichat9effa972006-04-19 23:36:40 +0200185 u16 productId = le16_to_cpu(dev->udev->descriptor.idProduct);
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500186
187 return (productId == GEYSER_ANSI_PRODUCT_ID) ||
188 (productId == GEYSER_ISO_PRODUCT_ID) ||
189 (productId == GEYSER_JIS_PRODUCT_ID);
190}
191
Nicolas Boichat9effa972006-04-19 23:36:40 +0200192static inline int atp_is_geyser_3(struct atp *dev)
193{
194 u16 productId = le16_to_cpu(dev->udev->descriptor.idProduct);
195
196 return (productId == GEYSER3_ANSI_PRODUCT_ID) ||
197 (productId == GEYSER3_ISO_PRODUCT_ID) ||
Julien BLACHE009ad0e2006-11-17 01:06:13 -0500198 (productId == GEYSER3_JIS_PRODUCT_ID) ||
199 (productId == GEYSER4_ANSI_PRODUCT_ID) ||
200 (productId == GEYSER4_ISO_PRODUCT_ID) ||
201 (productId == GEYSER4_JIS_PRODUCT_ID);
Nicolas Boichat9effa972006-04-19 23:36:40 +0200202}
203
Stelian Popf7214ff2005-09-08 10:19:48 +0200204static int atp_calculate_abs(int *xy_sensors, int nb_sensors, int fact,
205 int *z, int *fingers)
206{
207 int i;
208 /* values to calculate mean */
209 int pcum = 0, psum = 0;
Jason Parekh00081d32006-11-17 01:05:58 -0500210 int is_increasing = 0;
Stelian Popf7214ff2005-09-08 10:19:48 +0200211
212 *fingers = 0;
213
214 for (i = 0; i < nb_sensors; i++) {
Jason Parekh00081d32006-11-17 01:05:58 -0500215 if (xy_sensors[i] < threshold) {
216 if (is_increasing)
217 is_increasing = 0;
218
Stelian Popf7214ff2005-09-08 10:19:48 +0200219 continue;
Jason Parekh00081d32006-11-17 01:05:58 -0500220 }
221
222 /*
223 * Makes the finger detection more versatile. For example,
224 * two fingers with no gap will be detected. Also, my
225 * tests show it less likely to have intermittent loss
226 * of multiple finger readings while moving around (scrolling).
227 *
228 * Changes the multiple finger detection to counting humps on
229 * sensors (transitions from nonincreasing to increasing)
230 * instead of counting transitions from low sensors (no
231 * finger reading) to high sensors (finger above
232 * sensor)
233 *
234 * - Jason Parekh <jasonparekh@gmail.com>
235 */
236 if (i < 1 || (!is_increasing && xy_sensors[i - 1] < xy_sensors[i])) {
Stelian Popf7214ff2005-09-08 10:19:48 +0200237 (*fingers)++;
Jason Parekh00081d32006-11-17 01:05:58 -0500238 is_increasing = 1;
239 } else if (i > 0 && xy_sensors[i - 1] >= xy_sensors[i]) {
240 is_increasing = 0;
241 }
242
243 /*
244 * Subtracts threshold so a high sensor that just passes the threshold
245 * won't skew the calculated absolute coordinate. Fixes an issue
246 * where slowly moving the mouse would occassionaly jump a number of
247 * pixels (let me restate--slowly moving the mouse makes this issue
248 * most apparent).
249 */
250 pcum += (xy_sensors[i] - threshold) * i;
251 psum += (xy_sensors[i] - threshold);
Stelian Popf7214ff2005-09-08 10:19:48 +0200252 }
253
254 if (psum > 0) {
255 *z = psum;
256 return pcum * fact / psum;
257 }
258
259 return 0;
260}
261
262static inline void atp_report_fingers(struct input_dev *input, int fingers)
263{
264 input_report_key(input, BTN_TOOL_FINGER, fingers == 1);
265 input_report_key(input, BTN_TOOL_DOUBLETAP, fingers == 2);
266 input_report_key(input, BTN_TOOL_TRIPLETAP, fingers > 2);
267}
268
David Howells7d12e782006-10-05 14:55:46 +0100269static void atp_complete(struct urb* urb)
Stelian Popf7214ff2005-09-08 10:19:48 +0200270{
271 int x, y, x_z, y_z, x_f, y_f;
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500272 int retval, i, j;
Stelian Popf7214ff2005-09-08 10:19:48 +0200273 struct atp *dev = urb->context;
274
275 switch (urb->status) {
276 case 0:
277 /* success */
278 break;
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500279 case -EOVERFLOW:
280 if(!dev->overflowwarn) {
281 printk("appletouch: OVERFLOW with data "
282 "length %d, actual length is %d\n",
283 dev->datalen, dev->urb->actual_length);
284 dev->overflowwarn = 1;
285 }
Stelian Popf7214ff2005-09-08 10:19:48 +0200286 case -ECONNRESET:
287 case -ENOENT:
288 case -ESHUTDOWN:
289 /* This urb is terminated, clean up */
290 dbg("%s - urb shutting down with status: %d",
291 __FUNCTION__, urb->status);
292 return;
293 default:
294 dbg("%s - nonzero urb status received: %d",
295 __FUNCTION__, urb->status);
296 goto exit;
297 }
298
299 /* drop incomplete datasets */
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500300 if (dev->urb->actual_length != dev->datalen) {
Nicolas Boichat9effa972006-04-19 23:36:40 +0200301 dprintk("appletouch: incomplete data package"
302 " (first byte: %d, length: %d).\n",
303 dev->data[0], dev->urb->actual_length);
Stelian Popf7214ff2005-09-08 10:19:48 +0200304 goto exit;
305 }
306
307 /* reorder the sensors values */
Nicolas Boichat9effa972006-04-19 23:36:40 +0200308 if (atp_is_geyser_3(dev)) {
309 memset(dev->xy_cur, 0, sizeof(dev->xy_cur));
310
311 /*
312 * The values are laid out like this:
313 * -, Y1, Y2, -, Y3, Y4, -, ..., -, X1, X2, -, X3, X4, ...
314 * '-' is an unused value.
315 */
316
317 /* read X values */
318 for (i = 0, j = 19; i < 20; i += 2, j += 3) {
319 dev->xy_cur[i] = dev->data[j + 1];
320 dev->xy_cur[i + 1] = dev->data[j + 2];
321 }
322 /* read Y values */
323 for (i = 0, j = 1; i < 9; i += 2, j += 3) {
324 dev->xy_cur[ATP_XSENSORS + i] = dev->data[j + 1];
325 dev->xy_cur[ATP_XSENSORS + i + 1] = dev->data[j + 2];
326 }
327 } else if (atp_is_geyser_2(dev)) {
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500328 memset(dev->xy_cur, 0, sizeof(dev->xy_cur));
Stelian Popf7214ff2005-09-08 10:19:48 +0200329
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500330 /*
331 * The values are laid out like this:
332 * Y1, Y2, -, Y3, Y4, -, ..., X1, X2, -, X3, X4, -, ...
333 * '-' is an unused value.
334 */
335
336 /* read X values */
337 for (i = 0, j = 19; i < 20; i += 2, j += 3) {
338 dev->xy_cur[i] = dev->data[j];
339 dev->xy_cur[i + 1] = dev->data[j + 1];
340 }
341
342 /* read Y values */
343 for (i = 0, j = 1; i < 9; i += 2, j += 3) {
344 dev->xy_cur[ATP_XSENSORS + i] = dev->data[j];
345 dev->xy_cur[ATP_XSENSORS + i + 1] = dev->data[j + 1];
346 }
347 } else {
348 for (i = 0; i < 8; i++) {
349 /* X values */
350 dev->xy_cur[i ] = dev->data[5 * i + 2];
351 dev->xy_cur[i + 8] = dev->data[5 * i + 4];
352 dev->xy_cur[i + 16] = dev->data[5 * i + 42];
353 if (i < 2)
354 dev->xy_cur[i + 24] = dev->data[5 * i + 44];
355
356 /* Y values */
357 dev->xy_cur[i + 26] = dev->data[5 * i + 1];
358 dev->xy_cur[i + 34] = dev->data[5 * i + 3];
359 }
Stelian Popf7214ff2005-09-08 10:19:48 +0200360 }
361
362 dbg_dump("sample", dev->xy_cur);
363
364 if (!dev->valid) {
365 /* first sample */
366 dev->valid = 1;
367 dev->x_old = dev->y_old = -1;
368 memcpy(dev->xy_old, dev->xy_cur, sizeof(dev->xy_old));
369
Nicolas Boichat9effa972006-04-19 23:36:40 +0200370 if (atp_is_geyser_3(dev)) /* No 17" Macbooks (yet) */
371 goto exit;
372
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500373 /* 17" Powerbooks have extra X sensors */
374 for (i = (atp_is_geyser_2(dev)?15:16); i < ATP_XSENSORS; i++) {
375 if (!dev->xy_cur[i]) continue;
376
377 printk("appletouch: 17\" model detected.\n");
378 if(atp_is_geyser_2(dev))
379 input_set_abs_params(dev->input, ABS_X, 0,
380 (20 - 1) *
381 ATP_XFACT - 1,
382 ATP_FUZZ, 0);
383 else
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500384 input_set_abs_params(dev->input, ABS_X, 0,
385 (ATP_XSENSORS - 1) *
Stelian Popf7214ff2005-09-08 10:19:48 +0200386 ATP_XFACT - 1,
387 ATP_FUZZ, 0);
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500388
389 break;
390 }
Stelian Popf7214ff2005-09-08 10:19:48 +0200391
392 goto exit;
393 }
394
395 for (i = 0; i < ATP_XSENSORS + ATP_YSENSORS; i++) {
396 /* accumulate the change */
397 signed char change = dev->xy_old[i] - dev->xy_cur[i];
398 dev->xy_acc[i] -= change;
399
400 /* prevent down drifting */
401 if (dev->xy_acc[i] < 0)
402 dev->xy_acc[i] = 0;
403 }
404
405 memcpy(dev->xy_old, dev->xy_cur, sizeof(dev->xy_old));
406
407 dbg_dump("accumulator", dev->xy_acc);
408
409 x = atp_calculate_abs(dev->xy_acc, ATP_XSENSORS,
410 ATP_XFACT, &x_z, &x_f);
411 y = atp_calculate_abs(dev->xy_acc + ATP_XSENSORS, ATP_YSENSORS,
412 ATP_YFACT, &y_z, &y_f);
413
414 if (x && y) {
415 if (dev->x_old != -1) {
416 x = (dev->x_old * 3 + x) >> 2;
417 y = (dev->y_old * 3 + y) >> 2;
418 dev->x_old = x;
419 dev->y_old = y;
420
421 if (debug > 1)
422 printk("appletouch: X: %3d Y: %3d "
423 "Xz: %3d Yz: %3d\n",
424 x, y, x_z, y_z);
425
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500426 input_report_key(dev->input, BTN_TOUCH, 1);
427 input_report_abs(dev->input, ABS_X, x);
428 input_report_abs(dev->input, ABS_Y, y);
429 input_report_abs(dev->input, ABS_PRESSURE,
Stelian Popf7214ff2005-09-08 10:19:48 +0200430 min(ATP_PRESSURE, x_z + y_z));
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500431 atp_report_fingers(dev->input, max(x_f, y_f));
Stelian Popf7214ff2005-09-08 10:19:48 +0200432 }
433 dev->x_old = x;
434 dev->y_old = y;
435 }
436 else if (!x && !y) {
437
438 dev->x_old = dev->y_old = -1;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500439 input_report_key(dev->input, BTN_TOUCH, 0);
440 input_report_abs(dev->input, ABS_PRESSURE, 0);
441 atp_report_fingers(dev->input, 0);
Stelian Popf7214ff2005-09-08 10:19:48 +0200442
443 /* reset the accumulator on release */
444 memset(dev->xy_acc, 0, sizeof(dev->xy_acc));
445 }
446
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500447 input_report_key(dev->input, BTN_LEFT,
448 !!dev->data[dev->datalen - 1]);
Stelian Popf7214ff2005-09-08 10:19:48 +0200449
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500450 input_sync(dev->input);
Stelian Popf7214ff2005-09-08 10:19:48 +0200451
452exit:
453 retval = usb_submit_urb(dev->urb, GFP_ATOMIC);
454 if (retval) {
455 err("%s - usb_submit_urb failed with result %d",
456 __FUNCTION__, retval);
457 }
458}
459
460static int atp_open(struct input_dev *input)
461{
462 struct atp *dev = input->private;
463
464 if (usb_submit_urb(dev->urb, GFP_ATOMIC))
465 return -EIO;
466
467 dev->open = 1;
468 return 0;
469}
470
471static void atp_close(struct input_dev *input)
472{
473 struct atp *dev = input->private;
474
475 usb_kill_urb(dev->urb);
476 dev->open = 0;
477}
478
479static int atp_probe(struct usb_interface *iface, const struct usb_device_id *id)
480{
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500481 struct atp *dev;
482 struct input_dev *input_dev;
483 struct usb_device *udev = interface_to_usbdev(iface);
Stelian Popf7214ff2005-09-08 10:19:48 +0200484 struct usb_host_interface *iface_desc;
485 struct usb_endpoint_descriptor *endpoint;
486 int int_in_endpointAddr = 0;
487 int i, retval = -ENOMEM;
488
Stelian Popf7214ff2005-09-08 10:19:48 +0200489
490 /* set up the endpoint information */
491 /* use only the first interrupt-in endpoint */
492 iface_desc = iface->cur_altsetting;
493 for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
494 endpoint = &iface_desc->endpoint[i].desc;
Luiz Fernando N. Capitulino97b107c2006-09-27 11:58:53 -0700495 if (!int_in_endpointAddr && usb_endpoint_is_int_in(endpoint)) {
Stelian Popf7214ff2005-09-08 10:19:48 +0200496 /* we found an interrupt in endpoint */
497 int_in_endpointAddr = endpoint->bEndpointAddress;
498 break;
499 }
500 }
501 if (!int_in_endpointAddr) {
Stelian Popf7214ff2005-09-08 10:19:48 +0200502 err("Could not find int-in endpoint");
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500503 return -EIO;
Stelian Popf7214ff2005-09-08 10:19:48 +0200504 }
505
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500506 /* allocate memory for our device state and initialize it */
507 dev = kzalloc(sizeof(struct atp), GFP_KERNEL);
508 input_dev = input_allocate_device();
509 if (!dev || !input_dev) {
510 err("Out of memory");
511 goto err_free_devs;
512 }
513
514 dev->udev = udev;
515 dev->input = input_dev;
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500516 dev->overflowwarn = 0;
Nicolas Boichat9effa972006-04-19 23:36:40 +0200517 if (atp_is_geyser_3(dev))
518 dev->datalen = 64;
519 else if (atp_is_geyser_2(dev))
520 dev->datalen = 64;
521 else
522 dev->datalen = 81;
523
524 if (atp_is_geyser_3(dev)) {
525 /*
526 * By default Geyser 3 device sends standard USB HID mouse
527 * packets (Report ID 2). This code changes device mode, so it
528 * sends raw sensor reports (Report ID 5).
529 */
530 char data[8];
531 int size;
532
533 size = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
534 ATP_GEYSER3_MODE_READ_REQUEST_ID,
535 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
536 ATP_GEYSER3_MODE_REQUEST_VALUE,
537 ATP_GEYSER3_MODE_REQUEST_INDEX, &data, 8, 5000);
538
539 if (size != 8) {
540 err("Could not do mode read request from device"
541 " (Geyser 3 mode)");
542 goto err_free_devs;
543 }
544
545 /* Apply the mode switch */
546 data[0] = ATP_GEYSER3_MODE_VENDOR_VALUE;
547
548 size = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
549 ATP_GEYSER3_MODE_WRITE_REQUEST_ID,
550 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
551 ATP_GEYSER3_MODE_REQUEST_VALUE,
552 ATP_GEYSER3_MODE_REQUEST_INDEX, &data, 8, 5000);
553
554 if (size != 8) {
555 err("Could not do mode write request to device"
556 " (Geyser 3 mode)");
557 goto err_free_devs;
558 }
559 printk("appletouch Geyser 3 inited.\n");
560 }
Stelian Popf7214ff2005-09-08 10:19:48 +0200561
562 dev->urb = usb_alloc_urb(0, GFP_KERNEL);
563 if (!dev->urb) {
564 retval = -ENOMEM;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500565 goto err_free_devs;
Stelian Popf7214ff2005-09-08 10:19:48 +0200566 }
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500567
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500568 dev->data = usb_buffer_alloc(dev->udev, dev->datalen, GFP_KERNEL,
Stelian Popf7214ff2005-09-08 10:19:48 +0200569 &dev->urb->transfer_dma);
570 if (!dev->data) {
571 retval = -ENOMEM;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500572 goto err_free_urb;
Stelian Popf7214ff2005-09-08 10:19:48 +0200573 }
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500574
575 usb_fill_int_urb(dev->urb, udev,
576 usb_rcvintpipe(udev, int_in_endpointAddr),
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500577 dev->data, dev->datalen, atp_complete, dev, 1);
Stelian Popf7214ff2005-09-08 10:19:48 +0200578
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500579 usb_make_path(udev, dev->phys, sizeof(dev->phys));
580 strlcat(dev->phys, "/input0", sizeof(dev->phys));
Stelian Popf7214ff2005-09-08 10:19:48 +0200581
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500582 input_dev->name = "appletouch";
583 input_dev->phys = dev->phys;
584 usb_to_input_id(dev->udev, &input_dev->id);
585 input_dev->cdev.dev = &iface->dev;
Stelian Popf7214ff2005-09-08 10:19:48 +0200586
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500587 input_dev->private = dev;
588 input_dev->open = atp_open;
589 input_dev->close = atp_close;
590
591 set_bit(EV_ABS, input_dev->evbit);
Stelian Popf7214ff2005-09-08 10:19:48 +0200592
Nicolas Boichat9effa972006-04-19 23:36:40 +0200593 if (atp_is_geyser_3(dev)) {
594 /*
595 * MacBook have 20 X sensors, 10 Y sensors
596 */
597 input_set_abs_params(input_dev, ABS_X, 0,
598 ((20 - 1) * ATP_XFACT) - 1, ATP_FUZZ, 0);
599 input_set_abs_params(input_dev, ABS_Y, 0,
600 ((10 - 1) * ATP_YFACT) - 1, ATP_FUZZ, 0);
601 } else if (atp_is_geyser_2(dev)) {
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500602 /*
603 * Oct 2005 15" PowerBooks have 15 X sensors, 17" are detected
604 * later.
605 */
606 input_set_abs_params(input_dev, ABS_X, 0,
607 ((15 - 1) * ATP_XFACT) - 1, ATP_FUZZ, 0);
608 input_set_abs_params(input_dev, ABS_Y, 0,
609 ((9 - 1) * ATP_YFACT) - 1, ATP_FUZZ, 0);
610 } else {
611 /*
612 * 12" and 15" Powerbooks only have 16 x sensors,
613 * 17" models are detected later.
614 */
615 input_set_abs_params(input_dev, ABS_X, 0,
616 (16 - 1) * ATP_XFACT - 1, ATP_FUZZ, 0);
617 input_set_abs_params(input_dev, ABS_Y, 0,
618 (ATP_YSENSORS - 1) * ATP_YFACT - 1, ATP_FUZZ, 0);
619 }
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500620 input_set_abs_params(input_dev, ABS_PRESSURE, 0, ATP_PRESSURE, 0, 0);
Stelian Popf7214ff2005-09-08 10:19:48 +0200621
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500622 set_bit(EV_KEY, input_dev->evbit);
623 set_bit(BTN_TOUCH, input_dev->keybit);
624 set_bit(BTN_TOOL_FINGER, input_dev->keybit);
625 set_bit(BTN_TOOL_DOUBLETAP, input_dev->keybit);
626 set_bit(BTN_TOOL_TRIPLETAP, input_dev->keybit);
627 set_bit(BTN_LEFT, input_dev->keybit);
Stelian Popf7214ff2005-09-08 10:19:48 +0200628
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500629 input_register_device(dev->input);
Stelian Popf7214ff2005-09-08 10:19:48 +0200630
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500631 /* save our data pointer in this interface device */
632 usb_set_intfdata(iface, dev);
Stelian Popf7214ff2005-09-08 10:19:48 +0200633
634 return 0;
635
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500636 err_free_urb:
Stelian Popf7214ff2005-09-08 10:19:48 +0200637 usb_free_urb(dev->urb);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500638 err_free_devs:
Stelian Popf7214ff2005-09-08 10:19:48 +0200639 usb_set_intfdata(iface, NULL);
Stelian Popf7214ff2005-09-08 10:19:48 +0200640 kfree(dev);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500641 input_free_device(input_dev);
Stelian Popf7214ff2005-09-08 10:19:48 +0200642 return retval;
643}
644
645static void atp_disconnect(struct usb_interface *iface)
646{
647 struct atp *dev = usb_get_intfdata(iface);
648
649 usb_set_intfdata(iface, NULL);
650 if (dev) {
651 usb_kill_urb(dev->urb);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500652 input_unregister_device(dev->input);
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500653 usb_buffer_free(dev->udev, dev->datalen,
Stelian Popf7214ff2005-09-08 10:19:48 +0200654 dev->data, dev->urb->transfer_dma);
Johannes Berg7af569a2006-07-19 15:39:46 +0200655 usb_free_urb(dev->urb);
Stelian Popf7214ff2005-09-08 10:19:48 +0200656 kfree(dev);
657 }
658 printk(KERN_INFO "input: appletouch disconnected\n");
659}
660
661static int atp_suspend(struct usb_interface *iface, pm_message_t message)
662{
663 struct atp *dev = usb_get_intfdata(iface);
664 usb_kill_urb(dev->urb);
665 dev->valid = 0;
666 return 0;
667}
668
669static int atp_resume(struct usb_interface *iface)
670{
671 struct atp *dev = usb_get_intfdata(iface);
672 if (dev->open && usb_submit_urb(dev->urb, GFP_ATOMIC))
673 return -EIO;
674
675 return 0;
676}
677
678static struct usb_driver atp_driver = {
Stelian Popf7214ff2005-09-08 10:19:48 +0200679 .name = "appletouch",
680 .probe = atp_probe,
681 .disconnect = atp_disconnect,
682 .suspend = atp_suspend,
683 .resume = atp_resume,
684 .id_table = atp_table,
685};
686
687static int __init atp_init(void)
688{
689 return usb_register(&atp_driver);
690}
691
692static void __exit atp_exit(void)
693{
694 usb_deregister(&atp_driver);
695}
696
697module_init(atp_init);
698module_exit(atp_exit);