]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/input/mouse/appletouch.c
Input: hp_sdc.c - fix section mismatch
[mirror_ubuntu-bionic-kernel.git] / drivers / input / mouse / appletouch.c
CommitLineData
f7214ff4 1/*
9effa978 2 * Apple USB Touchpad (for post-February 2005 PowerBooks and MacBooks) driver
f7214ff4
SP
3 *
4 * Copyright (C) 2001-2004 Greg Kroah-Hartman (greg@kroah.com)
5 * Copyright (C) 2005 Johannes Berg (johannes@sipsolutions.net)
6 * Copyright (C) 2005 Stelian Pop (stelian@popies.net)
7 * Copyright (C) 2005 Frank Arnold (frank@scirocco-5v-turbo.de)
8 * Copyright (C) 2005 Peter Osterlund (petero2@telia.com)
e1e02c9f 9 * Copyright (C) 2005 Michael Hanselmann (linux-kernel@hansmi.ch)
9effa978 10 * Copyright (C) 2006 Nicolas Boichat (nicolas@boichat.ch)
f7214ff4
SP
11 *
12 * Thanks to Alex Harper <basilisk@foobox.net> for his inputs.
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 *
28 */
29
f7214ff4
SP
30#include <linux/kernel.h>
31#include <linux/errno.h>
32#include <linux/init.h>
33#include <linux/slab.h>
34#include <linux/module.h>
ae0dadcf 35#include <linux/usb/input.h>
f7214ff4
SP
36
37/* Apple has powerbooks which have the keyboard with different Product IDs */
38#define APPLE_VENDOR_ID 0x05AC
39
e1e02c9f 40/* These names come from Info.plist in AppleUSBTrackpad.kext */
e5d9832c
JB
41#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
e1e02c9f 51
9effa978 52/* MacBook devices */
e5d9832c
JB
53#define GEYSER3_ANSI_PRODUCT_ID 0x0217
54#define GEYSER3_ISO_PRODUCT_ID 0x0218
55#define GEYSER3_JIS_PRODUCT_ID 0x0219
9effa978 56
009ad0e7
JB
57/*
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
f7214ff4 65#define ATP_DEVICE(prod) \
c5b7c7c3 66 .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \
f7214ff4
SP
67 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 [] = {
e5d9832c
JB
76 { 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) },
e1e02c9f
MH
80
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
009ad0e7 86 /* Core Duo MacBook & MacBook Pro */
9effa978
NB
87 { ATP_DEVICE(GEYSER3_ANSI_PRODUCT_ID) },
88 { ATP_DEVICE(GEYSER3_ISO_PRODUCT_ID) },
89 { ATP_DEVICE(GEYSER3_JIS_PRODUCT_ID) },
90
009ad0e7
JB
91 /* 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
e1e02c9f
MH
96 /* Terminating entry */
97 { }
f7214ff4
SP
98};
99MODULE_DEVICE_TABLE (usb, atp_table);
100
f7214ff4
SP
101/*
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:
c5b7c7c3
DT
119 * 0 <= x < 960 on 12" and 15" Powerbooks
120 * 0 <= x < 1600 on 17" Powerbooks
121 * 0 <= y < 646
f7214ff4
SP
122 */
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
009ad0e7 132/* MacBook Pro (Geyser 3 & 4) initialization constants */
9effa978
NB
133#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
f7214ff4
SP
139/* Structure to hold all of our device specific stuff */
140struct atp {
c5b7c7c3 141 char phys[64];
f7214ff4
SP
142 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 */
c5b7c7c3 146 struct input_dev *input; /* input dev */
f7214ff4
SP
147 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];
e1e02c9f
MH
156 int overflowwarn; /* overflow warning printed? */
157 int datalen; /* size of an USB urb transfer */
5a6eb676
SS
158 int idlecount; /* number of empty packets */
159 struct work_struct work;
f7214ff4
SP
160};
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++) \
c5b7c7c3
DT
167 printk(" %02x", tab[i]); \
168 printk("\n"); \
f7214ff4
SP
169 }
170
c5b7c7c3 171#define dprintk(format, a...) \
f7214ff4
SP
172 do { \
173 if (debug) printk(format, ##a); \
174 } while (0)
175
e1e02c9f 176MODULE_AUTHOR("Johannes Berg, Stelian Pop, Frank Arnold, Michael Hanselmann");
f7214ff4
SP
177MODULE_DESCRIPTION("Apple PowerBooks USB touchpad driver");
178MODULE_LICENSE("GPL");
179
00081d37
JP
180/*
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
f7214ff4
SP
187static int debug = 1;
188module_param(debug, int, 0644);
189MODULE_PARM_DESC(debug, "Activate debugging output");
190
e1e02c9f
MH
191/* Checks if the device a Geyser 2 (ANSI, ISO, JIS) */
192static inline int atp_is_geyser_2(struct atp *dev)
193{
9effa978 194 u16 productId = le16_to_cpu(dev->udev->descriptor.idProduct);
e1e02c9f
MH
195
196 return (productId == GEYSER_ANSI_PRODUCT_ID) ||
197 (productId == GEYSER_ISO_PRODUCT_ID) ||
198 (productId == GEYSER_JIS_PRODUCT_ID);
199}
200
9effa978
NB
201static 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) ||
009ad0e7
JB
207 (productId == GEYSER3_JIS_PRODUCT_ID) ||
208 (productId == GEYSER4_ANSI_PRODUCT_ID) ||
209 (productId == GEYSER4_ISO_PRODUCT_ID) ||
210 (productId == GEYSER4_JIS_PRODUCT_ID);
9effa978
NB
211}
212
5a6eb676
SS
213/*
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
f7214ff4
SP
262static 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;
00081d37 268 int is_increasing = 0;
f7214ff4
SP
269
270 *fingers = 0;
271
272 for (i = 0; i < nb_sensors; i++) {
00081d37
JP
273 if (xy_sensors[i] < threshold) {
274 if (is_increasing)
275 is_increasing = 0;
276
f7214ff4 277 continue;
00081d37
JP
278 }
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])) {
f7214ff4 295 (*fingers)++;
00081d37
JP
296 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);
f7214ff4
SP
310 }
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
7d12e780 327static void atp_complete(struct urb* urb)
f7214ff4
SP
328{
329 int x, y, x_z, y_z, x_f, y_f;
e1e02c9f 330 int retval, i, j;
cb560737 331 int key;
f7214ff4
SP
332 struct atp *dev = urb->context;
333
334 switch (urb->status) {
335 case 0:
336 /* success */
337 break;
e1e02c9f
MH
338 case -EOVERFLOW:
339 if(!dev->overflowwarn) {
340 printk("appletouch: OVERFLOW with data "
341 "length %d, actual length is %d\n",
342 dev->datalen, dev->urb->actual_length);
343 dev->overflowwarn = 1;
344 }
f7214ff4
SP
345 case -ECONNRESET:
346 case -ENOENT:
347 case -ESHUTDOWN:
348 /* This urb is terminated, clean up */
349 dbg("%s - urb shutting down with status: %d",
350 __FUNCTION__, urb->status);
351 return;
352 default:
353 dbg("%s - nonzero urb status received: %d",
354 __FUNCTION__, urb->status);
355 goto exit;
356 }
357
358 /* drop incomplete datasets */
e1e02c9f 359 if (dev->urb->actual_length != dev->datalen) {
9effa978
NB
360 dprintk("appletouch: incomplete data package"
361 " (first byte: %d, length: %d).\n",
362 dev->data[0], dev->urb->actual_length);
f7214ff4
SP
363 goto exit;
364 }
365
366 /* reorder the sensors values */
9effa978
NB
367 if (atp_is_geyser_3(dev)) {
368 memset(dev->xy_cur, 0, sizeof(dev->xy_cur));
369
370 /*
371 * The values are laid out like this:
372 * -, Y1, Y2, -, Y3, Y4, -, ..., -, X1, X2, -, X3, X4, ...
373 * '-' is an unused value.
374 */
375
376 /* read X values */
377 for (i = 0, j = 19; i < 20; i += 2, j += 3) {
378 dev->xy_cur[i] = dev->data[j + 1];
379 dev->xy_cur[i + 1] = dev->data[j + 2];
380 }
381 /* read Y values */
382 for (i = 0, j = 1; i < 9; i += 2, j += 3) {
383 dev->xy_cur[ATP_XSENSORS + i] = dev->data[j + 1];
384 dev->xy_cur[ATP_XSENSORS + i + 1] = dev->data[j + 2];
385 }
386 } else if (atp_is_geyser_2(dev)) {
e1e02c9f
MH
387 memset(dev->xy_cur, 0, sizeof(dev->xy_cur));
388
389 /*
390 * The values are laid out like this:
391 * Y1, Y2, -, Y3, Y4, -, ..., X1, X2, -, X3, X4, -, ...
392 * '-' is an unused value.
393 */
394
395 /* read X values */
396 for (i = 0, j = 19; i < 20; i += 2, j += 3) {
397 dev->xy_cur[i] = dev->data[j];
398 dev->xy_cur[i + 1] = dev->data[j + 1];
399 }
400
401 /* read Y values */
402 for (i = 0, j = 1; i < 9; i += 2, j += 3) {
403 dev->xy_cur[ATP_XSENSORS + i] = dev->data[j];
404 dev->xy_cur[ATP_XSENSORS + i + 1] = dev->data[j + 1];
405 }
406 } else {
407 for (i = 0; i < 8; i++) {
408 /* X values */
409 dev->xy_cur[i ] = dev->data[5 * i + 2];
410 dev->xy_cur[i + 8] = dev->data[5 * i + 4];
411 dev->xy_cur[i + 16] = dev->data[5 * i + 42];
412 if (i < 2)
413 dev->xy_cur[i + 24] = dev->data[5 * i + 44];
414
415 /* Y values */
416 dev->xy_cur[i + 26] = dev->data[5 * i + 1];
417 dev->xy_cur[i + 34] = dev->data[5 * i + 3];
418 }
f7214ff4
SP
419 }
420
421 dbg_dump("sample", dev->xy_cur);
422
423 if (!dev->valid) {
424 /* first sample */
425 dev->valid = 1;
426 dev->x_old = dev->y_old = -1;
427 memcpy(dev->xy_old, dev->xy_cur, sizeof(dev->xy_old));
428
9effa978
NB
429 if (atp_is_geyser_3(dev)) /* No 17" Macbooks (yet) */
430 goto exit;
431
e1e02c9f
MH
432 /* 17" Powerbooks have extra X sensors */
433 for (i = (atp_is_geyser_2(dev)?15:16); i < ATP_XSENSORS; i++) {
434 if (!dev->xy_cur[i]) continue;
435
436 printk("appletouch: 17\" model detected.\n");
437 if(atp_is_geyser_2(dev))
438 input_set_abs_params(dev->input, ABS_X, 0,
439 (20 - 1) *
440 ATP_XFACT - 1,
441 ATP_FUZZ, 0);
442 else
c5b7c7c3
DT
443 input_set_abs_params(dev->input, ABS_X, 0,
444 (ATP_XSENSORS - 1) *
f7214ff4
SP
445 ATP_XFACT - 1,
446 ATP_FUZZ, 0);
e1e02c9f
MH
447
448 break;
449 }
f7214ff4
SP
450
451 goto exit;
452 }
453
454 for (i = 0; i < ATP_XSENSORS + ATP_YSENSORS; i++) {
455 /* accumulate the change */
456 signed char change = dev->xy_old[i] - dev->xy_cur[i];
457 dev->xy_acc[i] -= change;
458
459 /* prevent down drifting */
460 if (dev->xy_acc[i] < 0)
461 dev->xy_acc[i] = 0;
462 }
463
464 memcpy(dev->xy_old, dev->xy_cur, sizeof(dev->xy_old));
465
466 dbg_dump("accumulator", dev->xy_acc);
467
468 x = atp_calculate_abs(dev->xy_acc, ATP_XSENSORS,
469 ATP_XFACT, &x_z, &x_f);
470 y = atp_calculate_abs(dev->xy_acc + ATP_XSENSORS, ATP_YSENSORS,
471 ATP_YFACT, &y_z, &y_f);
cb560737 472 key = dev->data[dev->datalen - 1] & 1;
f7214ff4
SP
473
474 if (x && y) {
475 if (dev->x_old != -1) {
476 x = (dev->x_old * 3 + x) >> 2;
477 y = (dev->y_old * 3 + y) >> 2;
478 dev->x_old = x;
479 dev->y_old = y;
480
481 if (debug > 1)
482 printk("appletouch: X: %3d Y: %3d "
483 "Xz: %3d Yz: %3d\n",
484 x, y, x_z, y_z);
485
c5b7c7c3
DT
486 input_report_key(dev->input, BTN_TOUCH, 1);
487 input_report_abs(dev->input, ABS_X, x);
488 input_report_abs(dev->input, ABS_Y, y);
489 input_report_abs(dev->input, ABS_PRESSURE,
f7214ff4 490 min(ATP_PRESSURE, x_z + y_z));
c5b7c7c3 491 atp_report_fingers(dev->input, max(x_f, y_f));
f7214ff4
SP
492 }
493 dev->x_old = x;
494 dev->y_old = y;
5a6eb676
SS
495
496 } else if (!x && !y) {
f7214ff4
SP
497
498 dev->x_old = dev->y_old = -1;
c5b7c7c3
DT
499 input_report_key(dev->input, BTN_TOUCH, 0);
500 input_report_abs(dev->input, ABS_PRESSURE, 0);
501 atp_report_fingers(dev->input, 0);
f7214ff4
SP
502
503 /* reset the accumulator on release */
504 memset(dev->xy_acc, 0, sizeof(dev->xy_acc));
937ad5c1
SS
505 }
506
46249ea6
AE
507 input_report_key(dev->input, BTN_LEFT, key);
508 input_sync(dev->input);
509
510 /* Many Geysers will continue to send packets continually after
937ad5c1
SS
511 the first touch unless reinitialised. Do so if it's been
512 idle for a while in order to avoid waking the kernel up
513 several hundred times a second */
f7214ff4 514
46249ea6
AE
515 if (!x && !y && !key) {
516 dev->idlecount++;
517 if (dev->idlecount == 10) {
518 dev->valid = 0;
519 schedule_work(&dev->work);
5a6eb676 520 }
46249ea6
AE
521 } else
522 dev->idlecount = 0;
f7214ff4
SP
523
524exit:
525 retval = usb_submit_urb(dev->urb, GFP_ATOMIC);
526 if (retval) {
527 err("%s - usb_submit_urb failed with result %d",
528 __FUNCTION__, retval);
529 }
530}
531
532static int atp_open(struct input_dev *input)
533{
7791bdae 534 struct atp *dev = input_get_drvdata(input);
f7214ff4
SP
535
536 if (usb_submit_urb(dev->urb, GFP_ATOMIC))
537 return -EIO;
538
539 dev->open = 1;
540 return 0;
541}
542
543static void atp_close(struct input_dev *input)
544{
7791bdae 545 struct atp *dev = input_get_drvdata(input);
f7214ff4
SP
546
547 usb_kill_urb(dev->urb);
5a6eb676 548 cancel_work_sync(&dev->work);
f7214ff4
SP
549 dev->open = 0;
550}
551
552static int atp_probe(struct usb_interface *iface, const struct usb_device_id *id)
553{
c5b7c7c3
DT
554 struct atp *dev;
555 struct input_dev *input_dev;
556 struct usb_device *udev = interface_to_usbdev(iface);
f7214ff4
SP
557 struct usb_host_interface *iface_desc;
558 struct usb_endpoint_descriptor *endpoint;
559 int int_in_endpointAddr = 0;
5014186d 560 int i, error = -ENOMEM;
f7214ff4
SP
561
562 /* set up the endpoint information */
563 /* use only the first interrupt-in endpoint */
564 iface_desc = iface->cur_altsetting;
565 for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
566 endpoint = &iface_desc->endpoint[i].desc;
97b107ca 567 if (!int_in_endpointAddr && usb_endpoint_is_int_in(endpoint)) {
f7214ff4
SP
568 /* we found an interrupt in endpoint */
569 int_in_endpointAddr = endpoint->bEndpointAddress;
570 break;
571 }
572 }
573 if (!int_in_endpointAddr) {
f7214ff4 574 err("Could not find int-in endpoint");
c5b7c7c3 575 return -EIO;
f7214ff4
SP
576 }
577
c5b7c7c3
DT
578 /* allocate memory for our device state and initialize it */
579 dev = kzalloc(sizeof(struct atp), GFP_KERNEL);
580 input_dev = input_allocate_device();
581 if (!dev || !input_dev) {
582 err("Out of memory");
583 goto err_free_devs;
584 }
585
586 dev->udev = udev;
587 dev->input = input_dev;
e1e02c9f 588 dev->overflowwarn = 0;
9effa978
NB
589 if (atp_is_geyser_3(dev))
590 dev->datalen = 64;
591 else if (atp_is_geyser_2(dev))
592 dev->datalen = 64;
593 else
594 dev->datalen = 81;
595
596 if (atp_is_geyser_3(dev)) {
5a6eb676
SS
597 /* switch to raw sensor mode */
598 if (atp_geyser3_init(udev))
9effa978 599 goto err_free_devs;
9effa978 600
9effa978
NB
601 printk("appletouch Geyser 3 inited.\n");
602 }
f7214ff4
SP
603
604 dev->urb = usb_alloc_urb(0, GFP_KERNEL);
5014186d 605 if (!dev->urb)
c5b7c7c3 606 goto err_free_devs;
c5b7c7c3 607
e1e02c9f 608 dev->data = usb_buffer_alloc(dev->udev, dev->datalen, GFP_KERNEL,
f7214ff4 609 &dev->urb->transfer_dma);
5014186d 610 if (!dev->data)
c5b7c7c3 611 goto err_free_urb;
c5b7c7c3
DT
612
613 usb_fill_int_urb(dev->urb, udev,
614 usb_rcvintpipe(udev, int_in_endpointAddr),
e1e02c9f 615 dev->data, dev->datalen, atp_complete, dev, 1);
f7214ff4 616
c5b7c7c3
DT
617 usb_make_path(udev, dev->phys, sizeof(dev->phys));
618 strlcat(dev->phys, "/input0", sizeof(dev->phys));
619
620 input_dev->name = "appletouch";
621 input_dev->phys = dev->phys;
622 usb_to_input_id(dev->udev, &input_dev->id);
c0f82d57 623 input_dev->dev.parent = &iface->dev;
f7214ff4 624
7791bdae
DT
625 input_set_drvdata(input_dev, dev);
626
c5b7c7c3
DT
627 input_dev->open = atp_open;
628 input_dev->close = atp_close;
f7214ff4 629
c5b7c7c3 630 set_bit(EV_ABS, input_dev->evbit);
f7214ff4 631
9effa978
NB
632 if (atp_is_geyser_3(dev)) {
633 /*
634 * MacBook have 20 X sensors, 10 Y sensors
635 */
636 input_set_abs_params(input_dev, ABS_X, 0,
637 ((20 - 1) * ATP_XFACT) - 1, ATP_FUZZ, 0);
638 input_set_abs_params(input_dev, ABS_Y, 0,
639 ((10 - 1) * ATP_YFACT) - 1, ATP_FUZZ, 0);
640 } else if (atp_is_geyser_2(dev)) {
e1e02c9f
MH
641 /*
642 * Oct 2005 15" PowerBooks have 15 X sensors, 17" are detected
643 * later.
644 */
645 input_set_abs_params(input_dev, ABS_X, 0,
646 ((15 - 1) * ATP_XFACT) - 1, ATP_FUZZ, 0);
647 input_set_abs_params(input_dev, ABS_Y, 0,
648 ((9 - 1) * ATP_YFACT) - 1, ATP_FUZZ, 0);
649 } else {
650 /*
651 * 12" and 15" Powerbooks only have 16 x sensors,
652 * 17" models are detected later.
653 */
654 input_set_abs_params(input_dev, ABS_X, 0,
655 (16 - 1) * ATP_XFACT - 1, ATP_FUZZ, 0);
656 input_set_abs_params(input_dev, ABS_Y, 0,
657 (ATP_YSENSORS - 1) * ATP_YFACT - 1, ATP_FUZZ, 0);
658 }
c5b7c7c3 659 input_set_abs_params(input_dev, ABS_PRESSURE, 0, ATP_PRESSURE, 0, 0);
f7214ff4 660
c5b7c7c3
DT
661 set_bit(EV_KEY, input_dev->evbit);
662 set_bit(BTN_TOUCH, input_dev->keybit);
663 set_bit(BTN_TOOL_FINGER, input_dev->keybit);
664 set_bit(BTN_TOOL_DOUBLETAP, input_dev->keybit);
665 set_bit(BTN_TOOL_TRIPLETAP, input_dev->keybit);
666 set_bit(BTN_LEFT, input_dev->keybit);
f7214ff4 667
5014186d
DT
668 error = input_register_device(dev->input);
669 if (error)
670 goto err_free_buffer;
f7214ff4 671
c5b7c7c3
DT
672 /* save our data pointer in this interface device */
673 usb_set_intfdata(iface, dev);
f7214ff4 674
5a6eb676
SS
675 INIT_WORK(&dev->work, atp_reinit);
676
f7214ff4
SP
677 return 0;
678
5014186d
DT
679 err_free_buffer:
680 usb_buffer_free(dev->udev, dev->datalen,
681 dev->data, dev->urb->transfer_dma);
c5b7c7c3 682 err_free_urb:
f7214ff4 683 usb_free_urb(dev->urb);
c5b7c7c3 684 err_free_devs:
f7214ff4 685 usb_set_intfdata(iface, NULL);
f7214ff4 686 kfree(dev);
c5b7c7c3 687 input_free_device(input_dev);
5014186d 688 return error;
f7214ff4
SP
689}
690
691static void atp_disconnect(struct usb_interface *iface)
692{
693 struct atp *dev = usb_get_intfdata(iface);
694
695 usb_set_intfdata(iface, NULL);
696 if (dev) {
697 usb_kill_urb(dev->urb);
c5b7c7c3 698 input_unregister_device(dev->input);
e1e02c9f 699 usb_buffer_free(dev->udev, dev->datalen,
f7214ff4 700 dev->data, dev->urb->transfer_dma);
7af569ab 701 usb_free_urb(dev->urb);
f7214ff4
SP
702 kfree(dev);
703 }
704 printk(KERN_INFO "input: appletouch disconnected\n");
705}
706
707static int atp_suspend(struct usb_interface *iface, pm_message_t message)
708{
709 struct atp *dev = usb_get_intfdata(iface);
5a6eb676 710
f7214ff4
SP
711 usb_kill_urb(dev->urb);
712 dev->valid = 0;
5a6eb676 713
f7214ff4
SP
714 return 0;
715}
716
717static int atp_resume(struct usb_interface *iface)
718{
719 struct atp *dev = usb_get_intfdata(iface);
5a6eb676 720
f7214ff4
SP
721 if (dev->open && usb_submit_urb(dev->urb, GFP_ATOMIC))
722 return -EIO;
723
724 return 0;
725}
726
727static struct usb_driver atp_driver = {
f7214ff4
SP
728 .name = "appletouch",
729 .probe = atp_probe,
730 .disconnect = atp_disconnect,
731 .suspend = atp_suspend,
732 .resume = atp_resume,
733 .id_table = atp_table,
734};
735
736static int __init atp_init(void)
737{
738 return usb_register(&atp_driver);
739}
740
741static void __exit atp_exit(void)
742{
743 usb_deregister(&atp_driver);
744}
745
746module_init(atp_init);
747module_exit(atp_exit);