blob: 4825db4b2566d9ccfdd8f130afab074932c08c53 [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
Stelian Popf7214ff2005-09-08 10:19:48 +020050#define ATP_DEVICE(prod) \
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -050051 .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \
Stelian Popf7214ff2005-09-08 10:19:48 +020052 USB_DEVICE_ID_MATCH_INT_CLASS | \
53 USB_DEVICE_ID_MATCH_INT_PROTOCOL, \
54 .idVendor = APPLE_VENDOR_ID, \
55 .idProduct = (prod), \
56 .bInterfaceClass = 0x03, \
57 .bInterfaceProtocol = 0x02
58
59/* table of devices that work with this driver */
60static struct usb_device_id atp_table [] = {
61 { ATP_DEVICE(0x020E) },
62 { ATP_DEVICE(0x020F) },
63 { ATP_DEVICE(0x030A) },
64 { ATP_DEVICE(0x030B) },
Michael Hanselmanne1e02c92005-12-21 00:50:23 -050065
66 /* PowerBooks Oct 2005 */
67 { ATP_DEVICE(GEYSER_ANSI_PRODUCT_ID) },
68 { ATP_DEVICE(GEYSER_ISO_PRODUCT_ID) },
69 { ATP_DEVICE(GEYSER_JIS_PRODUCT_ID) },
70
Nicolas Boichat9effa972006-04-19 23:36:40 +020071 { ATP_DEVICE(GEYSER3_ANSI_PRODUCT_ID) },
72 { ATP_DEVICE(GEYSER3_ISO_PRODUCT_ID) },
73 { ATP_DEVICE(GEYSER3_JIS_PRODUCT_ID) },
74
Michael Hanselmanne1e02c92005-12-21 00:50:23 -050075 /* Terminating entry */
76 { }
Stelian Popf7214ff2005-09-08 10:19:48 +020077};
78MODULE_DEVICE_TABLE (usb, atp_table);
79
Stelian Popf7214ff2005-09-08 10:19:48 +020080/*
81 * number of sensors. Note that only 16 instead of 26 X (horizontal)
82 * sensors exist on 12" and 15" PowerBooks. All models have 16 Y
83 * (vertical) sensors.
84 */
85#define ATP_XSENSORS 26
86#define ATP_YSENSORS 16
87
88/* amount of fuzz this touchpad generates */
89#define ATP_FUZZ 16
90
91/* maximum pressure this driver will report */
92#define ATP_PRESSURE 300
93/*
94 * multiplication factor for the X and Y coordinates.
95 * We try to keep the touchpad aspect ratio while still doing only simple
96 * arithmetics.
97 * The factors below give coordinates like:
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -050098 * 0 <= x < 960 on 12" and 15" Powerbooks
99 * 0 <= x < 1600 on 17" Powerbooks
100 * 0 <= y < 646
Stelian Popf7214ff2005-09-08 10:19:48 +0200101 */
102#define ATP_XFACT 64
103#define ATP_YFACT 43
104
105/*
106 * Threshold for the touchpad sensors. Any change less than ATP_THRESHOLD is
107 * ignored.
108 */
109#define ATP_THRESHOLD 5
110
Nicolas Boichat9effa972006-04-19 23:36:40 +0200111/* MacBook Pro (Geyser 3) initialization constants */
112#define ATP_GEYSER3_MODE_READ_REQUEST_ID 1
113#define ATP_GEYSER3_MODE_WRITE_REQUEST_ID 9
114#define ATP_GEYSER3_MODE_REQUEST_VALUE 0x300
115#define ATP_GEYSER3_MODE_REQUEST_INDEX 0
116#define ATP_GEYSER3_MODE_VENDOR_VALUE 0x04
117
Stelian Popf7214ff2005-09-08 10:19:48 +0200118/* Structure to hold all of our device specific stuff */
119struct atp {
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500120 char phys[64];
Stelian Popf7214ff2005-09-08 10:19:48 +0200121 struct usb_device * udev; /* usb device */
122 struct urb * urb; /* usb request block */
123 signed char * data; /* transferred data */
124 int open; /* non-zero if opened */
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500125 struct input_dev *input; /* input dev */
Stelian Popf7214ff2005-09-08 10:19:48 +0200126 int valid; /* are the sensors valid ? */
127 int x_old; /* last reported x/y, */
128 int y_old; /* used for smoothing */
129 /* current value of the sensors */
130 signed char xy_cur[ATP_XSENSORS + ATP_YSENSORS];
131 /* last value of the sensors */
132 signed char xy_old[ATP_XSENSORS + ATP_YSENSORS];
133 /* accumulated sensors */
134 int xy_acc[ATP_XSENSORS + ATP_YSENSORS];
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500135 int overflowwarn; /* overflow warning printed? */
136 int datalen; /* size of an USB urb transfer */
Stelian Popf7214ff2005-09-08 10:19:48 +0200137};
138
139#define dbg_dump(msg, tab) \
140 if (debug > 1) { \
141 int i; \
142 printk("appletouch: %s %lld", msg, (long long)jiffies); \
143 for (i = 0; i < ATP_XSENSORS + ATP_YSENSORS; i++) \
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500144 printk(" %02x", tab[i]); \
145 printk("\n"); \
Stelian Popf7214ff2005-09-08 10:19:48 +0200146 }
147
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500148#define dprintk(format, a...) \
Stelian Popf7214ff2005-09-08 10:19:48 +0200149 do { \
150 if (debug) printk(format, ##a); \
151 } while (0)
152
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500153MODULE_AUTHOR("Johannes Berg, Stelian Pop, Frank Arnold, Michael Hanselmann");
Stelian Popf7214ff2005-09-08 10:19:48 +0200154MODULE_DESCRIPTION("Apple PowerBooks USB touchpad driver");
155MODULE_LICENSE("GPL");
156
Jason Parekh00081d32006-11-17 01:05:58 -0500157/*
158 * Make the threshold a module parameter
159 */
160static int threshold = ATP_THRESHOLD;
161module_param(threshold, int, 0644);
162MODULE_PARM_DESC(threshold, "Discards any change in data from a sensor (trackpad has hundreds of these sensors) less than this value");
163
Stelian Popf7214ff2005-09-08 10:19:48 +0200164static int debug = 1;
165module_param(debug, int, 0644);
166MODULE_PARM_DESC(debug, "Activate debugging output");
167
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500168/* Checks if the device a Geyser 2 (ANSI, ISO, JIS) */
169static inline int atp_is_geyser_2(struct atp *dev)
170{
Nicolas Boichat9effa972006-04-19 23:36:40 +0200171 u16 productId = le16_to_cpu(dev->udev->descriptor.idProduct);
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500172
173 return (productId == GEYSER_ANSI_PRODUCT_ID) ||
174 (productId == GEYSER_ISO_PRODUCT_ID) ||
175 (productId == GEYSER_JIS_PRODUCT_ID);
176}
177
Nicolas Boichat9effa972006-04-19 23:36:40 +0200178static inline int atp_is_geyser_3(struct atp *dev)
179{
180 u16 productId = le16_to_cpu(dev->udev->descriptor.idProduct);
181
182 return (productId == GEYSER3_ANSI_PRODUCT_ID) ||
183 (productId == GEYSER3_ISO_PRODUCT_ID) ||
184 (productId == GEYSER3_JIS_PRODUCT_ID);
185}
186
Stelian Popf7214ff2005-09-08 10:19:48 +0200187static int atp_calculate_abs(int *xy_sensors, int nb_sensors, int fact,
188 int *z, int *fingers)
189{
190 int i;
191 /* values to calculate mean */
192 int pcum = 0, psum = 0;
Jason Parekh00081d32006-11-17 01:05:58 -0500193 int is_increasing = 0;
Stelian Popf7214ff2005-09-08 10:19:48 +0200194
195 *fingers = 0;
196
197 for (i = 0; i < nb_sensors; i++) {
Jason Parekh00081d32006-11-17 01:05:58 -0500198 if (xy_sensors[i] < threshold) {
199 if (is_increasing)
200 is_increasing = 0;
201
Stelian Popf7214ff2005-09-08 10:19:48 +0200202 continue;
Jason Parekh00081d32006-11-17 01:05:58 -0500203 }
204
205 /*
206 * Makes the finger detection more versatile. For example,
207 * two fingers with no gap will be detected. Also, my
208 * tests show it less likely to have intermittent loss
209 * of multiple finger readings while moving around (scrolling).
210 *
211 * Changes the multiple finger detection to counting humps on
212 * sensors (transitions from nonincreasing to increasing)
213 * instead of counting transitions from low sensors (no
214 * finger reading) to high sensors (finger above
215 * sensor)
216 *
217 * - Jason Parekh <jasonparekh@gmail.com>
218 */
219 if (i < 1 || (!is_increasing && xy_sensors[i - 1] < xy_sensors[i])) {
Stelian Popf7214ff2005-09-08 10:19:48 +0200220 (*fingers)++;
Jason Parekh00081d32006-11-17 01:05:58 -0500221 is_increasing = 1;
222 } else if (i > 0 && xy_sensors[i - 1] >= xy_sensors[i]) {
223 is_increasing = 0;
224 }
225
226 /*
227 * Subtracts threshold so a high sensor that just passes the threshold
228 * won't skew the calculated absolute coordinate. Fixes an issue
229 * where slowly moving the mouse would occassionaly jump a number of
230 * pixels (let me restate--slowly moving the mouse makes this issue
231 * most apparent).
232 */
233 pcum += (xy_sensors[i] - threshold) * i;
234 psum += (xy_sensors[i] - threshold);
Stelian Popf7214ff2005-09-08 10:19:48 +0200235 }
236
237 if (psum > 0) {
238 *z = psum;
239 return pcum * fact / psum;
240 }
241
242 return 0;
243}
244
245static inline void atp_report_fingers(struct input_dev *input, int fingers)
246{
247 input_report_key(input, BTN_TOOL_FINGER, fingers == 1);
248 input_report_key(input, BTN_TOOL_DOUBLETAP, fingers == 2);
249 input_report_key(input, BTN_TOOL_TRIPLETAP, fingers > 2);
250}
251
David Howells7d12e782006-10-05 14:55:46 +0100252static void atp_complete(struct urb* urb)
Stelian Popf7214ff2005-09-08 10:19:48 +0200253{
254 int x, y, x_z, y_z, x_f, y_f;
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500255 int retval, i, j;
Stelian Popf7214ff2005-09-08 10:19:48 +0200256 struct atp *dev = urb->context;
257
258 switch (urb->status) {
259 case 0:
260 /* success */
261 break;
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500262 case -EOVERFLOW:
263 if(!dev->overflowwarn) {
264 printk("appletouch: OVERFLOW with data "
265 "length %d, actual length is %d\n",
266 dev->datalen, dev->urb->actual_length);
267 dev->overflowwarn = 1;
268 }
Stelian Popf7214ff2005-09-08 10:19:48 +0200269 case -ECONNRESET:
270 case -ENOENT:
271 case -ESHUTDOWN:
272 /* This urb is terminated, clean up */
273 dbg("%s - urb shutting down with status: %d",
274 __FUNCTION__, urb->status);
275 return;
276 default:
277 dbg("%s - nonzero urb status received: %d",
278 __FUNCTION__, urb->status);
279 goto exit;
280 }
281
282 /* drop incomplete datasets */
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500283 if (dev->urb->actual_length != dev->datalen) {
Nicolas Boichat9effa972006-04-19 23:36:40 +0200284 dprintk("appletouch: incomplete data package"
285 " (first byte: %d, length: %d).\n",
286 dev->data[0], dev->urb->actual_length);
Stelian Popf7214ff2005-09-08 10:19:48 +0200287 goto exit;
288 }
289
290 /* reorder the sensors values */
Nicolas Boichat9effa972006-04-19 23:36:40 +0200291 if (atp_is_geyser_3(dev)) {
292 memset(dev->xy_cur, 0, sizeof(dev->xy_cur));
293
294 /*
295 * The values are laid out like this:
296 * -, Y1, Y2, -, Y3, Y4, -, ..., -, X1, X2, -, X3, X4, ...
297 * '-' is an unused value.
298 */
299
300 /* read X values */
301 for (i = 0, j = 19; i < 20; i += 2, j += 3) {
302 dev->xy_cur[i] = dev->data[j + 1];
303 dev->xy_cur[i + 1] = dev->data[j + 2];
304 }
305 /* read Y values */
306 for (i = 0, j = 1; i < 9; i += 2, j += 3) {
307 dev->xy_cur[ATP_XSENSORS + i] = dev->data[j + 1];
308 dev->xy_cur[ATP_XSENSORS + i + 1] = dev->data[j + 2];
309 }
310 } else if (atp_is_geyser_2(dev)) {
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500311 memset(dev->xy_cur, 0, sizeof(dev->xy_cur));
Stelian Popf7214ff2005-09-08 10:19:48 +0200312
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500313 /*
314 * The values are laid out like this:
315 * Y1, Y2, -, Y3, Y4, -, ..., X1, X2, -, X3, X4, -, ...
316 * '-' is an unused value.
317 */
318
319 /* read X values */
320 for (i = 0, j = 19; i < 20; i += 2, j += 3) {
321 dev->xy_cur[i] = dev->data[j];
322 dev->xy_cur[i + 1] = dev->data[j + 1];
323 }
324
325 /* read Y values */
326 for (i = 0, j = 1; i < 9; i += 2, j += 3) {
327 dev->xy_cur[ATP_XSENSORS + i] = dev->data[j];
328 dev->xy_cur[ATP_XSENSORS + i + 1] = dev->data[j + 1];
329 }
330 } else {
331 for (i = 0; i < 8; i++) {
332 /* X values */
333 dev->xy_cur[i ] = dev->data[5 * i + 2];
334 dev->xy_cur[i + 8] = dev->data[5 * i + 4];
335 dev->xy_cur[i + 16] = dev->data[5 * i + 42];
336 if (i < 2)
337 dev->xy_cur[i + 24] = dev->data[5 * i + 44];
338
339 /* Y values */
340 dev->xy_cur[i + 26] = dev->data[5 * i + 1];
341 dev->xy_cur[i + 34] = dev->data[5 * i + 3];
342 }
Stelian Popf7214ff2005-09-08 10:19:48 +0200343 }
344
345 dbg_dump("sample", dev->xy_cur);
346
347 if (!dev->valid) {
348 /* first sample */
349 dev->valid = 1;
350 dev->x_old = dev->y_old = -1;
351 memcpy(dev->xy_old, dev->xy_cur, sizeof(dev->xy_old));
352
Nicolas Boichat9effa972006-04-19 23:36:40 +0200353 if (atp_is_geyser_3(dev)) /* No 17" Macbooks (yet) */
354 goto exit;
355
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500356 /* 17" Powerbooks have extra X sensors */
357 for (i = (atp_is_geyser_2(dev)?15:16); i < ATP_XSENSORS; i++) {
358 if (!dev->xy_cur[i]) continue;
359
360 printk("appletouch: 17\" model detected.\n");
361 if(atp_is_geyser_2(dev))
362 input_set_abs_params(dev->input, ABS_X, 0,
363 (20 - 1) *
364 ATP_XFACT - 1,
365 ATP_FUZZ, 0);
366 else
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500367 input_set_abs_params(dev->input, ABS_X, 0,
368 (ATP_XSENSORS - 1) *
Stelian Popf7214ff2005-09-08 10:19:48 +0200369 ATP_XFACT - 1,
370 ATP_FUZZ, 0);
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500371
372 break;
373 }
Stelian Popf7214ff2005-09-08 10:19:48 +0200374
375 goto exit;
376 }
377
378 for (i = 0; i < ATP_XSENSORS + ATP_YSENSORS; i++) {
379 /* accumulate the change */
380 signed char change = dev->xy_old[i] - dev->xy_cur[i];
381 dev->xy_acc[i] -= change;
382
383 /* prevent down drifting */
384 if (dev->xy_acc[i] < 0)
385 dev->xy_acc[i] = 0;
386 }
387
388 memcpy(dev->xy_old, dev->xy_cur, sizeof(dev->xy_old));
389
390 dbg_dump("accumulator", dev->xy_acc);
391
392 x = atp_calculate_abs(dev->xy_acc, ATP_XSENSORS,
393 ATP_XFACT, &x_z, &x_f);
394 y = atp_calculate_abs(dev->xy_acc + ATP_XSENSORS, ATP_YSENSORS,
395 ATP_YFACT, &y_z, &y_f);
396
397 if (x && y) {
398 if (dev->x_old != -1) {
399 x = (dev->x_old * 3 + x) >> 2;
400 y = (dev->y_old * 3 + y) >> 2;
401 dev->x_old = x;
402 dev->y_old = y;
403
404 if (debug > 1)
405 printk("appletouch: X: %3d Y: %3d "
406 "Xz: %3d Yz: %3d\n",
407 x, y, x_z, y_z);
408
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500409 input_report_key(dev->input, BTN_TOUCH, 1);
410 input_report_abs(dev->input, ABS_X, x);
411 input_report_abs(dev->input, ABS_Y, y);
412 input_report_abs(dev->input, ABS_PRESSURE,
Stelian Popf7214ff2005-09-08 10:19:48 +0200413 min(ATP_PRESSURE, x_z + y_z));
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500414 atp_report_fingers(dev->input, max(x_f, y_f));
Stelian Popf7214ff2005-09-08 10:19:48 +0200415 }
416 dev->x_old = x;
417 dev->y_old = y;
418 }
419 else if (!x && !y) {
420
421 dev->x_old = dev->y_old = -1;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500422 input_report_key(dev->input, BTN_TOUCH, 0);
423 input_report_abs(dev->input, ABS_PRESSURE, 0);
424 atp_report_fingers(dev->input, 0);
Stelian Popf7214ff2005-09-08 10:19:48 +0200425
426 /* reset the accumulator on release */
427 memset(dev->xy_acc, 0, sizeof(dev->xy_acc));
428 }
429
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500430 input_report_key(dev->input, BTN_LEFT,
431 !!dev->data[dev->datalen - 1]);
Stelian Popf7214ff2005-09-08 10:19:48 +0200432
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500433 input_sync(dev->input);
Stelian Popf7214ff2005-09-08 10:19:48 +0200434
435exit:
436 retval = usb_submit_urb(dev->urb, GFP_ATOMIC);
437 if (retval) {
438 err("%s - usb_submit_urb failed with result %d",
439 __FUNCTION__, retval);
440 }
441}
442
443static int atp_open(struct input_dev *input)
444{
445 struct atp *dev = input->private;
446
447 if (usb_submit_urb(dev->urb, GFP_ATOMIC))
448 return -EIO;
449
450 dev->open = 1;
451 return 0;
452}
453
454static void atp_close(struct input_dev *input)
455{
456 struct atp *dev = input->private;
457
458 usb_kill_urb(dev->urb);
459 dev->open = 0;
460}
461
462static int atp_probe(struct usb_interface *iface, const struct usb_device_id *id)
463{
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500464 struct atp *dev;
465 struct input_dev *input_dev;
466 struct usb_device *udev = interface_to_usbdev(iface);
Stelian Popf7214ff2005-09-08 10:19:48 +0200467 struct usb_host_interface *iface_desc;
468 struct usb_endpoint_descriptor *endpoint;
469 int int_in_endpointAddr = 0;
470 int i, retval = -ENOMEM;
471
Stelian Popf7214ff2005-09-08 10:19:48 +0200472
473 /* set up the endpoint information */
474 /* use only the first interrupt-in endpoint */
475 iface_desc = iface->cur_altsetting;
476 for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
477 endpoint = &iface_desc->endpoint[i].desc;
Luiz Fernando N. Capitulino97b107c2006-09-27 11:58:53 -0700478 if (!int_in_endpointAddr && usb_endpoint_is_int_in(endpoint)) {
Stelian Popf7214ff2005-09-08 10:19:48 +0200479 /* we found an interrupt in endpoint */
480 int_in_endpointAddr = endpoint->bEndpointAddress;
481 break;
482 }
483 }
484 if (!int_in_endpointAddr) {
Stelian Popf7214ff2005-09-08 10:19:48 +0200485 err("Could not find int-in endpoint");
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500486 return -EIO;
Stelian Popf7214ff2005-09-08 10:19:48 +0200487 }
488
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500489 /* allocate memory for our device state and initialize it */
490 dev = kzalloc(sizeof(struct atp), GFP_KERNEL);
491 input_dev = input_allocate_device();
492 if (!dev || !input_dev) {
493 err("Out of memory");
494 goto err_free_devs;
495 }
496
497 dev->udev = udev;
498 dev->input = input_dev;
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500499 dev->overflowwarn = 0;
Nicolas Boichat9effa972006-04-19 23:36:40 +0200500 if (atp_is_geyser_3(dev))
501 dev->datalen = 64;
502 else if (atp_is_geyser_2(dev))
503 dev->datalen = 64;
504 else
505 dev->datalen = 81;
506
507 if (atp_is_geyser_3(dev)) {
508 /*
509 * By default Geyser 3 device sends standard USB HID mouse
510 * packets (Report ID 2). This code changes device mode, so it
511 * sends raw sensor reports (Report ID 5).
512 */
513 char data[8];
514 int size;
515
516 size = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
517 ATP_GEYSER3_MODE_READ_REQUEST_ID,
518 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
519 ATP_GEYSER3_MODE_REQUEST_VALUE,
520 ATP_GEYSER3_MODE_REQUEST_INDEX, &data, 8, 5000);
521
522 if (size != 8) {
523 err("Could not do mode read request from device"
524 " (Geyser 3 mode)");
525 goto err_free_devs;
526 }
527
528 /* Apply the mode switch */
529 data[0] = ATP_GEYSER3_MODE_VENDOR_VALUE;
530
531 size = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
532 ATP_GEYSER3_MODE_WRITE_REQUEST_ID,
533 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
534 ATP_GEYSER3_MODE_REQUEST_VALUE,
535 ATP_GEYSER3_MODE_REQUEST_INDEX, &data, 8, 5000);
536
537 if (size != 8) {
538 err("Could not do mode write request to device"
539 " (Geyser 3 mode)");
540 goto err_free_devs;
541 }
542 printk("appletouch Geyser 3 inited.\n");
543 }
Stelian Popf7214ff2005-09-08 10:19:48 +0200544
545 dev->urb = usb_alloc_urb(0, GFP_KERNEL);
546 if (!dev->urb) {
547 retval = -ENOMEM;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500548 goto err_free_devs;
Stelian Popf7214ff2005-09-08 10:19:48 +0200549 }
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500550
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500551 dev->data = usb_buffer_alloc(dev->udev, dev->datalen, GFP_KERNEL,
Stelian Popf7214ff2005-09-08 10:19:48 +0200552 &dev->urb->transfer_dma);
553 if (!dev->data) {
554 retval = -ENOMEM;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500555 goto err_free_urb;
Stelian Popf7214ff2005-09-08 10:19:48 +0200556 }
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500557
558 usb_fill_int_urb(dev->urb, udev,
559 usb_rcvintpipe(udev, int_in_endpointAddr),
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500560 dev->data, dev->datalen, atp_complete, dev, 1);
Stelian Popf7214ff2005-09-08 10:19:48 +0200561
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500562 usb_make_path(udev, dev->phys, sizeof(dev->phys));
563 strlcat(dev->phys, "/input0", sizeof(dev->phys));
Stelian Popf7214ff2005-09-08 10:19:48 +0200564
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500565 input_dev->name = "appletouch";
566 input_dev->phys = dev->phys;
567 usb_to_input_id(dev->udev, &input_dev->id);
568 input_dev->cdev.dev = &iface->dev;
Stelian Popf7214ff2005-09-08 10:19:48 +0200569
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500570 input_dev->private = dev;
571 input_dev->open = atp_open;
572 input_dev->close = atp_close;
573
574 set_bit(EV_ABS, input_dev->evbit);
Stelian Popf7214ff2005-09-08 10:19:48 +0200575
Nicolas Boichat9effa972006-04-19 23:36:40 +0200576 if (atp_is_geyser_3(dev)) {
577 /*
578 * MacBook have 20 X sensors, 10 Y sensors
579 */
580 input_set_abs_params(input_dev, ABS_X, 0,
581 ((20 - 1) * ATP_XFACT) - 1, ATP_FUZZ, 0);
582 input_set_abs_params(input_dev, ABS_Y, 0,
583 ((10 - 1) * ATP_YFACT) - 1, ATP_FUZZ, 0);
584 } else if (atp_is_geyser_2(dev)) {
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500585 /*
586 * Oct 2005 15" PowerBooks have 15 X sensors, 17" are detected
587 * later.
588 */
589 input_set_abs_params(input_dev, ABS_X, 0,
590 ((15 - 1) * ATP_XFACT) - 1, ATP_FUZZ, 0);
591 input_set_abs_params(input_dev, ABS_Y, 0,
592 ((9 - 1) * ATP_YFACT) - 1, ATP_FUZZ, 0);
593 } else {
594 /*
595 * 12" and 15" Powerbooks only have 16 x sensors,
596 * 17" models are detected later.
597 */
598 input_set_abs_params(input_dev, ABS_X, 0,
599 (16 - 1) * ATP_XFACT - 1, ATP_FUZZ, 0);
600 input_set_abs_params(input_dev, ABS_Y, 0,
601 (ATP_YSENSORS - 1) * ATP_YFACT - 1, ATP_FUZZ, 0);
602 }
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500603 input_set_abs_params(input_dev, ABS_PRESSURE, 0, ATP_PRESSURE, 0, 0);
Stelian Popf7214ff2005-09-08 10:19:48 +0200604
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500605 set_bit(EV_KEY, input_dev->evbit);
606 set_bit(BTN_TOUCH, input_dev->keybit);
607 set_bit(BTN_TOOL_FINGER, input_dev->keybit);
608 set_bit(BTN_TOOL_DOUBLETAP, input_dev->keybit);
609 set_bit(BTN_TOOL_TRIPLETAP, input_dev->keybit);
610 set_bit(BTN_LEFT, input_dev->keybit);
Stelian Popf7214ff2005-09-08 10:19:48 +0200611
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500612 input_register_device(dev->input);
Stelian Popf7214ff2005-09-08 10:19:48 +0200613
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500614 /* save our data pointer in this interface device */
615 usb_set_intfdata(iface, dev);
Stelian Popf7214ff2005-09-08 10:19:48 +0200616
617 return 0;
618
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500619 err_free_urb:
Stelian Popf7214ff2005-09-08 10:19:48 +0200620 usb_free_urb(dev->urb);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500621 err_free_devs:
Stelian Popf7214ff2005-09-08 10:19:48 +0200622 usb_set_intfdata(iface, NULL);
Stelian Popf7214ff2005-09-08 10:19:48 +0200623 kfree(dev);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500624 input_free_device(input_dev);
Stelian Popf7214ff2005-09-08 10:19:48 +0200625 return retval;
626}
627
628static void atp_disconnect(struct usb_interface *iface)
629{
630 struct atp *dev = usb_get_intfdata(iface);
631
632 usb_set_intfdata(iface, NULL);
633 if (dev) {
634 usb_kill_urb(dev->urb);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500635 input_unregister_device(dev->input);
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500636 usb_buffer_free(dev->udev, dev->datalen,
Stelian Popf7214ff2005-09-08 10:19:48 +0200637 dev->data, dev->urb->transfer_dma);
Johannes Berg7af569a2006-07-19 15:39:46 +0200638 usb_free_urb(dev->urb);
Stelian Popf7214ff2005-09-08 10:19:48 +0200639 kfree(dev);
640 }
641 printk(KERN_INFO "input: appletouch disconnected\n");
642}
643
644static int atp_suspend(struct usb_interface *iface, pm_message_t message)
645{
646 struct atp *dev = usb_get_intfdata(iface);
647 usb_kill_urb(dev->urb);
648 dev->valid = 0;
649 return 0;
650}
651
652static int atp_resume(struct usb_interface *iface)
653{
654 struct atp *dev = usb_get_intfdata(iface);
655 if (dev->open && usb_submit_urb(dev->urb, GFP_ATOMIC))
656 return -EIO;
657
658 return 0;
659}
660
661static struct usb_driver atp_driver = {
Stelian Popf7214ff2005-09-08 10:19:48 +0200662 .name = "appletouch",
663 .probe = atp_probe,
664 .disconnect = atp_disconnect,
665 .suspend = atp_suspend,
666 .resume = atp_resume,
667 .id_table = atp_table,
668};
669
670static int __init atp_init(void)
671{
672 return usb_register(&atp_driver);
673}
674
675static void __exit atp_exit(void)
676{
677 usb_deregister(&atp_driver);
678}
679
680module_init(atp_init);
681module_exit(atp_exit);