blob: 36ebe5c25ee38922383078d0ad16e96fb83fa1a1 [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)
Johannes Berg7dce8692008-05-05 23:56:55 -04005 * Copyright (C) 2005-2008 Johannes Berg (johannes@sipsolutions.net)
Stelian Popf7214ff2005-09-08 10:19:48 +02006 * 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)
Johannes Berg7dce8692008-05-05 23:56:55 -040011 * Copyright (C) 2007-2008 Sven Anders (anders@anduras.de)
Stelian Popf7214ff2005-09-08 10:19:48 +020012 *
13 * Thanks to Alex Harper <basilisk@foobox.net> for his inputs.
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 *
29 */
30
Stelian Popf7214ff2005-09-08 10:19:48 +020031#include <linux/kernel.h>
32#include <linux/errno.h>
33#include <linux/init.h>
34#include <linux/slab.h>
35#include <linux/module.h>
David Brownellae0dadc2006-06-13 10:04:34 -070036#include <linux/usb/input.h>
Stelian Popf7214ff2005-09-08 10:19:48 +020037
Sven Anderse9542df2008-05-05 23:57:10 -040038/* Type of touchpad */
39enum atp_touchpad_type {
40 ATP_FOUNTAIN,
41 ATP_GEYSER1,
42 ATP_GEYSER2,
43 ATP_GEYSER3,
44 ATP_GEYSER4
45};
Stelian Popf7214ff2005-09-08 10:19:48 +020046
Sven Anderse9542df2008-05-05 23:57:10 -040047#define ATP_DEVICE(prod, type) \
48{ \
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -050049 .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \
Stelian Popf7214ff2005-09-08 10:19:48 +020050 USB_DEVICE_ID_MATCH_INT_CLASS | \
51 USB_DEVICE_ID_MATCH_INT_PROTOCOL, \
Sven Anderse9542df2008-05-05 23:57:10 -040052 .idVendor = 0x05ac, /* Apple */ \
Stelian Popf7214ff2005-09-08 10:19:48 +020053 .idProduct = (prod), \
54 .bInterfaceClass = 0x03, \
Sven Anderse9542df2008-05-05 23:57:10 -040055 .bInterfaceProtocol = 0x02, \
56 .driver_info = ATP_ ## type, \
57}
Stelian Popf7214ff2005-09-08 10:19:48 +020058
Sven Anderse9542df2008-05-05 23:57:10 -040059/*
60 * Table of devices (Product IDs) that work with this driver.
61 * (The names come from Info.plist in AppleUSBTrackpad.kext,
62 * According to Info.plist Geyser IV is the same as Geyser III.)
63 */
64
Stelian Popf7214ff2005-09-08 10:19:48 +020065static struct usb_device_id atp_table [] = {
Johannes Berg7dce8692008-05-05 23:56:55 -040066 /* PowerBooks Feb 2005, iBooks G4 */
Sven Anderse9542df2008-05-05 23:57:10 -040067 ATP_DEVICE(0x020e, FOUNTAIN), /* FOUNTAIN ANSI */
68 ATP_DEVICE(0x020f, FOUNTAIN), /* FOUNTAIN ISO */
69 ATP_DEVICE(0x030a, FOUNTAIN), /* FOUNTAIN TP ONLY */
70 ATP_DEVICE(0x030b, GEYSER1), /* GEYSER 1 TP ONLY */
Michael Hanselmanne1e02c92005-12-21 00:50:23 -050071
72 /* PowerBooks Oct 2005 */
Sven Anderse9542df2008-05-05 23:57:10 -040073 ATP_DEVICE(0x0214, GEYSER2), /* GEYSER 2 ANSI */
74 ATP_DEVICE(0x0215, GEYSER2), /* GEYSER 2 ISO */
75 ATP_DEVICE(0x0216, GEYSER2), /* GEYSER 2 JIS */
Michael Hanselmanne1e02c92005-12-21 00:50:23 -050076
Julien BLACHE009ad0e2006-11-17 01:06:13 -050077 /* Core Duo MacBook & MacBook Pro */
Sven Anderse9542df2008-05-05 23:57:10 -040078 ATP_DEVICE(0x0217, GEYSER3), /* GEYSER 3 ANSI */
79 ATP_DEVICE(0x0218, GEYSER3), /* GEYSER 3 ISO */
80 ATP_DEVICE(0x0219, GEYSER3), /* GEYSER 3 JIS */
Nicolas Boichat9effa972006-04-19 23:36:40 +020081
Julien BLACHE009ad0e2006-11-17 01:06:13 -050082 /* Core2 Duo MacBook & MacBook Pro */
Sven Anderse9542df2008-05-05 23:57:10 -040083 ATP_DEVICE(0x021a, GEYSER4), /* GEYSER 4 ANSI */
84 ATP_DEVICE(0x021b, GEYSER4), /* GEYSER 4 ISO */
85 ATP_DEVICE(0x021c, GEYSER4), /* GEYSER 4 JIS */
Julien BLACHE009ad0e2006-11-17 01:06:13 -050086
Johannes Berg7dce8692008-05-05 23:56:55 -040087 /* Core2 Duo MacBook3,1 */
Sven Anderse9542df2008-05-05 23:57:10 -040088 ATP_DEVICE(0x0229, GEYSER4), /* GEYSER 4 HF ANSI */
89 ATP_DEVICE(0x022a, GEYSER4), /* GEYSER 4 HF ISO */
90 ATP_DEVICE(0x022b, GEYSER4), /* GEYSER 4 HF JIS */
Tobias Mueller0035a1d2008-04-02 10:02:06 -040091
Michael Hanselmanne1e02c92005-12-21 00:50:23 -050092 /* Terminating entry */
93 { }
Stelian Popf7214ff2005-09-08 10:19:48 +020094};
Johannes Berg7dce8692008-05-05 23:56:55 -040095MODULE_DEVICE_TABLE(usb, atp_table);
Stelian Popf7214ff2005-09-08 10:19:48 +020096
Stelian Popf7214ff2005-09-08 10:19:48 +020097/*
98 * number of sensors. Note that only 16 instead of 26 X (horizontal)
99 * sensors exist on 12" and 15" PowerBooks. All models have 16 Y
100 * (vertical) sensors.
101 */
102#define ATP_XSENSORS 26
103#define ATP_YSENSORS 16
104
105/* amount of fuzz this touchpad generates */
106#define ATP_FUZZ 16
107
108/* maximum pressure this driver will report */
109#define ATP_PRESSURE 300
110/*
111 * multiplication factor for the X and Y coordinates.
112 * We try to keep the touchpad aspect ratio while still doing only simple
113 * arithmetics.
114 * The factors below give coordinates like:
Sven Anderse9542df2008-05-05 23:57:10 -0400115 *
116 * 0 <= x < 960 on 12" and 15" Powerbooks
117 * 0 <= x < 1600 on 17" Powerbooks and 17" MacBook Pro
118 * 0 <= x < 1216 on MacBooks and 15" MacBook Pro
119 *
120 * 0 <= y < 646 on all Powerbooks
121 * 0 <= y < 774 on all MacBooks
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];
Johannes Berg7dce8692008-05-05 23:56:55 -0400142 struct usb_device *udev; /* usb device */
143 struct urb *urb; /* usb request block */
144 signed char *data; /* transferred data */
145 struct input_dev *input; /* input dev */
Sven Anderse9542df2008-05-05 23:57:10 -0400146 enum atp_touchpad_type type; /* type of touchpad */
Johannes Berg7dce8692008-05-05 23:56:55 -0400147 bool open;
148 bool valid; /* are the samples valid? */
149 bool size_detect_done;
150 bool overflow_warned;
Stelian Popf7214ff2005-09-08 10:19:48 +0200151 int x_old; /* last reported x/y, */
152 int y_old; /* used for smoothing */
Stelian Popf7214ff2005-09-08 10:19:48 +0200153 signed char xy_cur[ATP_XSENSORS + ATP_YSENSORS];
Stelian Popf7214ff2005-09-08 10:19:48 +0200154 signed char xy_old[ATP_XSENSORS + ATP_YSENSORS];
Stelian Popf7214ff2005-09-08 10:19:48 +0200155 int xy_acc[ATP_XSENSORS + ATP_YSENSORS];
Johannes Berg7dce8692008-05-05 23:56:55 -0400156 int datalen; /* size of USB transfer */
157 int idlecount; /* number of empty packets */
158 struct work_struct work;
Stelian Popf7214ff2005-09-08 10:19:48 +0200159};
160
161#define dbg_dump(msg, tab) \
162 if (debug > 1) { \
Johannes Berg7dce8692008-05-05 23:56:55 -0400163 int __i; \
164 printk(KERN_DEBUG "appletouch: %s", msg); \
165 for (__i = 0; __i < ATP_XSENSORS + ATP_YSENSORS; __i++) \
166 printk(" %02x", tab[__i]); \
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500167 printk("\n"); \
Stelian Popf7214ff2005-09-08 10:19:48 +0200168 }
169
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500170#define dprintk(format, a...) \
Stelian Popf7214ff2005-09-08 10:19:48 +0200171 do { \
Johannes Berg7dce8692008-05-05 23:56:55 -0400172 if (debug) \
173 printk(KERN_DEBUG format, ##a); \
Stelian Popf7214ff2005-09-08 10:19:48 +0200174 } while (0)
175
Johannes Berg7dce8692008-05-05 23:56:55 -0400176MODULE_AUTHOR("Johannes Berg");
177MODULE_AUTHOR("Stelian Pop");
178MODULE_AUTHOR("Frank Arnold");
179MODULE_AUTHOR("Michael Hanselmann");
180MODULE_AUTHOR("Sven Anders");
181MODULE_DESCRIPTION("Apple PowerBook and MacBook USB touchpad driver");
Stelian Popf7214ff2005-09-08 10:19:48 +0200182MODULE_LICENSE("GPL");
183
Jason Parekh00081d32006-11-17 01:05:58 -0500184/*
185 * Make the threshold a module parameter
186 */
187static int threshold = ATP_THRESHOLD;
188module_param(threshold, int, 0644);
Johannes Berg7dce8692008-05-05 23:56:55 -0400189MODULE_PARM_DESC(threshold, "Discard any change in data from a sensor"
190 " (the trackpad has many of these sensors)"
191 " less than this value.");
Jason Parekh00081d32006-11-17 01:05:58 -0500192
Johannes Berg7dce8692008-05-05 23:56:55 -0400193static int debug;
Stelian Popf7214ff2005-09-08 10:19:48 +0200194module_param(debug, int, 0644);
195MODULE_PARM_DESC(debug, "Activate debugging output");
196
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400197/*
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400198 * By default newer Geyser devices send standard USB HID mouse
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400199 * packets (Report ID 2). This code changes device mode, so it
200 * sends raw sensor reports (Report ID 5).
201 */
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400202static int atp_geyser_init(struct usb_device *udev)
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400203{
204 char data[8];
205 int size;
Johannes Berg7dce8692008-05-05 23:56:55 -0400206 int i;
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400207
208 size = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400209 ATP_GEYSER_MODE_READ_REQUEST_ID,
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400210 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400211 ATP_GEYSER_MODE_REQUEST_VALUE,
212 ATP_GEYSER_MODE_REQUEST_INDEX, &data, 8, 5000);
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400213
214 if (size != 8) {
Johannes Berg7dce8692008-05-05 23:56:55 -0400215 dprintk("atp_geyser_init: read error\n");
216 for (i = 0; i < 8; i++)
217 dprintk("appletouch[%d]: %d\n", i, data[i]);
218
219 err("Failed to read mode from device.");
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400220 return -EIO;
221 }
222
223 /* Apply the mode switch */
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400224 data[0] = ATP_GEYSER_MODE_VENDOR_VALUE;
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400225
226 size = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400227 ATP_GEYSER_MODE_WRITE_REQUEST_ID,
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400228 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400229 ATP_GEYSER_MODE_REQUEST_VALUE,
230 ATP_GEYSER_MODE_REQUEST_INDEX, &data, 8, 5000);
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400231
232 if (size != 8) {
Johannes Berg7dce8692008-05-05 23:56:55 -0400233 dprintk("atp_geyser_init: write error\n");
234 for (i = 0; i < 8; i++)
235 dprintk("appletouch[%d]: %d\n", i, data[i]);
236
237 err("Failed to request geyser raw mode");
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400238 return -EIO;
239 }
240 return 0;
241}
242
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400243/*
244 * Reinitialise the device. This usually stops stream of empty packets
245 * coming from it.
246 */
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400247static void atp_reinit(struct work_struct *work)
248{
249 struct atp *dev = container_of(work, struct atp, work);
250 struct usb_device *udev = dev->udev;
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400251 int retval;
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400252
Johannes Berg7dce8692008-05-05 23:56:55 -0400253 dprintk("appletouch: putting appletouch to sleep (reinit)\n");
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400254 dev->idlecount = 0;
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400255
256 atp_geyser_init(udev);
257
258 retval = usb_submit_urb(dev->urb, GFP_ATOMIC);
Johannes Berg7dce8692008-05-05 23:56:55 -0400259 if (retval)
260 err("atp_reinit: usb_submit_urb failed with error %d",
261 retval);
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400262}
263
Stelian Popf7214ff2005-09-08 10:19:48 +0200264static int atp_calculate_abs(int *xy_sensors, int nb_sensors, int fact,
265 int *z, int *fingers)
266{
267 int i;
268 /* values to calculate mean */
269 int pcum = 0, psum = 0;
Jason Parekh00081d32006-11-17 01:05:58 -0500270 int is_increasing = 0;
Stelian Popf7214ff2005-09-08 10:19:48 +0200271
272 *fingers = 0;
273
274 for (i = 0; i < nb_sensors; i++) {
Jason Parekh00081d32006-11-17 01:05:58 -0500275 if (xy_sensors[i] < threshold) {
276 if (is_increasing)
277 is_increasing = 0;
278
Stelian Popf7214ff2005-09-08 10:19:48 +0200279 continue;
Jason Parekh00081d32006-11-17 01:05:58 -0500280 }
281
282 /*
283 * Makes the finger detection more versatile. For example,
284 * two fingers with no gap will be detected. Also, my
285 * tests show it less likely to have intermittent loss
286 * of multiple finger readings while moving around (scrolling).
287 *
288 * Changes the multiple finger detection to counting humps on
289 * sensors (transitions from nonincreasing to increasing)
290 * instead of counting transitions from low sensors (no
291 * finger reading) to high sensors (finger above
292 * sensor)
293 *
294 * - Jason Parekh <jasonparekh@gmail.com>
295 */
Johannes Berg7dce8692008-05-05 23:56:55 -0400296 if (i < 1 ||
297 (!is_increasing && xy_sensors[i - 1] < xy_sensors[i])) {
Stelian Popf7214ff2005-09-08 10:19:48 +0200298 (*fingers)++;
Jason Parekh00081d32006-11-17 01:05:58 -0500299 is_increasing = 1;
300 } else if (i > 0 && xy_sensors[i - 1] >= xy_sensors[i]) {
301 is_increasing = 0;
302 }
303
304 /*
Johannes Berg7dce8692008-05-05 23:56:55 -0400305 * Subtracts threshold so a high sensor that just passes the
306 * threshold won't skew the calculated absolute coordinate.
307 * Fixes an issue where slowly moving the mouse would
308 * occasionally jump a number of pixels (slowly moving the
309 * finger makes this issue most apparent.)
Jason Parekh00081d32006-11-17 01:05:58 -0500310 */
311 pcum += (xy_sensors[i] - threshold) * i;
312 psum += (xy_sensors[i] - threshold);
Stelian Popf7214ff2005-09-08 10:19:48 +0200313 }
314
315 if (psum > 0) {
316 *z = psum;
317 return pcum * fact / psum;
318 }
319
320 return 0;
321}
322
323static inline void atp_report_fingers(struct input_dev *input, int fingers)
324{
325 input_report_key(input, BTN_TOOL_FINGER, fingers == 1);
326 input_report_key(input, BTN_TOOL_DOUBLETAP, fingers == 2);
327 input_report_key(input, BTN_TOOL_TRIPLETAP, fingers > 2);
328}
329
Sven Andersd83d2132008-08-08 16:31:31 -0400330/* Check URB status and for correct length of data package */
331
332#define ATP_URB_STATUS_SUCCESS 0
333#define ATP_URB_STATUS_ERROR 1
334#define ATP_URB_STATUS_ERROR_FATAL 2
335
336static int atp_status_check(struct urb *urb)
Stelian Popf7214ff2005-09-08 10:19:48 +0200337{
Stelian Popf7214ff2005-09-08 10:19:48 +0200338 struct atp *dev = urb->context;
339
340 switch (urb->status) {
341 case 0:
342 /* success */
343 break;
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500344 case -EOVERFLOW:
Johannes Berg7dce8692008-05-05 23:56:55 -0400345 if (!dev->overflow_warned) {
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400346 printk(KERN_WARNING "appletouch: OVERFLOW with data "
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500347 "length %d, actual length is %d\n",
348 dev->datalen, dev->urb->actual_length);
Johannes Berg7dce8692008-05-05 23:56:55 -0400349 dev->overflow_warned = true;
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500350 }
Stelian Popf7214ff2005-09-08 10:19:48 +0200351 case -ECONNRESET:
352 case -ENOENT:
353 case -ESHUTDOWN:
354 /* This urb is terminated, clean up */
Johannes Berg7dce8692008-05-05 23:56:55 -0400355 dbg("atp_complete: urb shutting down with status: %d",
356 urb->status);
Sven Andersd83d2132008-08-08 16:31:31 -0400357 return ATP_URB_STATUS_ERROR_FATAL;
358
Stelian Popf7214ff2005-09-08 10:19:48 +0200359 default:
Johannes Berg7dce8692008-05-05 23:56:55 -0400360 dbg("atp_complete: nonzero urb status received: %d",
361 urb->status);
Sven Andersd83d2132008-08-08 16:31:31 -0400362 return ATP_URB_STATUS_ERROR;
Stelian Popf7214ff2005-09-08 10:19:48 +0200363 }
364
365 /* drop incomplete datasets */
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500366 if (dev->urb->actual_length != dev->datalen) {
Nicolas Boichat9effa972006-04-19 23:36:40 +0200367 dprintk("appletouch: incomplete data package"
368 " (first byte: %d, length: %d).\n",
369 dev->data[0], dev->urb->actual_length);
Sven Andersd83d2132008-08-08 16:31:31 -0400370 return ATP_URB_STATUS_ERROR;
Stelian Popf7214ff2005-09-08 10:19:48 +0200371 }
372
Sven Andersd83d2132008-08-08 16:31:31 -0400373 return ATP_URB_STATUS_SUCCESS;
374}
375
376/*
377 * USB interrupt callback functions
378 */
379
380/* Interrupt function for older touchpads: FOUNTAIN/GEYSER1/GEYSER2 */
381
382static void atp_complete_geyser_1_2(struct urb *urb)
383{
384 int x, y, x_z, y_z, x_f, y_f;
385 int retval, i, j;
386 int key;
387 struct atp *dev = urb->context;
388 int status = atp_status_check(urb);
389
390 if (status == ATP_URB_STATUS_ERROR_FATAL)
391 return;
392 else if (status == ATP_URB_STATUS_ERROR)
393 goto exit;
394
Stelian Popf7214ff2005-09-08 10:19:48 +0200395 /* reorder the sensors values */
Sven Andersd83d2132008-08-08 16:31:31 -0400396 if (dev->type == ATP_GEYSER2) {
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500397 memset(dev->xy_cur, 0, sizeof(dev->xy_cur));
Stelian Popf7214ff2005-09-08 10:19:48 +0200398
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500399 /*
400 * The values are laid out like this:
401 * Y1, Y2, -, Y3, Y4, -, ..., X1, X2, -, X3, X4, -, ...
402 * '-' is an unused value.
403 */
404
405 /* read X values */
406 for (i = 0, j = 19; i < 20; i += 2, j += 3) {
407 dev->xy_cur[i] = dev->data[j];
408 dev->xy_cur[i + 1] = dev->data[j + 1];
409 }
410
411 /* read Y values */
412 for (i = 0, j = 1; i < 9; i += 2, j += 3) {
413 dev->xy_cur[ATP_XSENSORS + i] = dev->data[j];
414 dev->xy_cur[ATP_XSENSORS + i + 1] = dev->data[j + 1];
415 }
416 } else {
417 for (i = 0; i < 8; i++) {
418 /* X values */
Johannes Berg7dce8692008-05-05 23:56:55 -0400419 dev->xy_cur[i + 0] = dev->data[5 * i + 2];
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500420 dev->xy_cur[i + 8] = dev->data[5 * i + 4];
421 dev->xy_cur[i + 16] = dev->data[5 * i + 42];
422 if (i < 2)
423 dev->xy_cur[i + 24] = dev->data[5 * i + 44];
424
425 /* Y values */
426 dev->xy_cur[i + 26] = dev->data[5 * i + 1];
427 dev->xy_cur[i + 34] = dev->data[5 * i + 3];
428 }
Stelian Popf7214ff2005-09-08 10:19:48 +0200429 }
430
431 dbg_dump("sample", dev->xy_cur);
432
433 if (!dev->valid) {
434 /* first sample */
Johannes Berg7dce8692008-05-05 23:56:55 -0400435 dev->valid = true;
Stelian Popf7214ff2005-09-08 10:19:48 +0200436 dev->x_old = dev->y_old = -1;
Sven Andersd83d2132008-08-08 16:31:31 -0400437
438 /* Store first sample */
Stelian Popf7214ff2005-09-08 10:19:48 +0200439 memcpy(dev->xy_old, dev->xy_cur, sizeof(dev->xy_old));
440
Sven Andersd83d2132008-08-08 16:31:31 -0400441 /* Perform size detection, if not done already */
442 if (!dev->size_detect_done) {
443
444 /* 17" Powerbooks have extra X sensors */
445 for (i = (dev->type == ATP_GEYSER2 ? 15 : 16);
446 i < ATP_XSENSORS; i++) {
447 if (!dev->xy_cur[i])
448 continue;
449
450 printk(KERN_INFO
451 "appletouch: 17\" model detected.\n");
452
453 if (dev->type == ATP_GEYSER2)
454 input_set_abs_params(dev->input, ABS_X,
455 0,
456 (20 - 1) *
457 ATP_XFACT - 1,
458 ATP_FUZZ, 0);
459 else
460 input_set_abs_params(dev->input, ABS_X,
461 0,
462 (26 - 1) *
463 ATP_XFACT - 1,
464 ATP_FUZZ, 0);
465 break;
466 }
467
468 dev->size_detect_done = 1;
Nicolas Boichat9effa972006-04-19 23:36:40 +0200469 goto exit;
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500470 }
Sven Andersd83d2132008-08-08 16:31:31 -0400471 }
Stelian Popf7214ff2005-09-08 10:19:48 +0200472
Sven Andersd83d2132008-08-08 16:31:31 -0400473 for (i = 0; i < ATP_XSENSORS + ATP_YSENSORS; i++) {
474 /* accumulate the change */
475 signed char change = dev->xy_old[i] - dev->xy_cur[i];
476 dev->xy_acc[i] -= change;
477
478 /* prevent down drifting */
479 if (dev->xy_acc[i] < 0)
480 dev->xy_acc[i] = 0;
481 }
482
483 memcpy(dev->xy_old, dev->xy_cur, sizeof(dev->xy_old));
484
485 dbg_dump("accumulator", dev->xy_acc);
486
487 x = atp_calculate_abs(dev->xy_acc, ATP_XSENSORS,
488 ATP_XFACT, &x_z, &x_f);
489 y = atp_calculate_abs(dev->xy_acc + ATP_XSENSORS, ATP_YSENSORS,
490 ATP_YFACT, &y_z, &y_f);
491 key = dev->data[dev->datalen - 1] & 1;
492
493 if (x && y) {
494 if (dev->x_old != -1) {
495 x = (dev->x_old * 3 + x) >> 2;
496 y = (dev->y_old * 3 + y) >> 2;
497 dev->x_old = x;
498 dev->y_old = y;
499
500 if (debug > 1)
501 printk(KERN_DEBUG "appletouch: "
502 "X: %3d Y: %3d Xz: %3d Yz: %3d\n",
503 x, y, x_z, y_z);
504
505 input_report_key(dev->input, BTN_TOUCH, 1);
506 input_report_abs(dev->input, ABS_X, x);
507 input_report_abs(dev->input, ABS_Y, y);
508 input_report_abs(dev->input, ABS_PRESSURE,
509 min(ATP_PRESSURE, x_z + y_z));
510 atp_report_fingers(dev->input, max(x_f, y_f));
511 }
512 dev->x_old = x;
513 dev->y_old = y;
514
515 } else if (!x && !y) {
516
517 dev->x_old = dev->y_old = -1;
518 input_report_key(dev->input, BTN_TOUCH, 0);
519 input_report_abs(dev->input, ABS_PRESSURE, 0);
520 atp_report_fingers(dev->input, 0);
521
522 /* reset the accumulator on release */
523 memset(dev->xy_acc, 0, sizeof(dev->xy_acc));
524 }
525
526 input_report_key(dev->input, BTN_LEFT, key);
527 input_sync(dev->input);
528
529 exit:
530 retval = usb_submit_urb(dev->urb, GFP_ATOMIC);
531 if (retval)
532 err("atp_complete: usb_submit_urb failed with result %d",
533 retval);
534}
535
536/* Interrupt function for older touchpads: GEYSER3/GEYSER4 */
537
538static void atp_complete_geyser_3_4(struct urb *urb)
539{
540 int x, y, x_z, y_z, x_f, y_f;
541 int retval, i, j;
542 int key;
543 struct atp *dev = urb->context;
544 int status = atp_status_check(urb);
545
546 if (status == ATP_URB_STATUS_ERROR_FATAL)
547 return;
548 else if (status == ATP_URB_STATUS_ERROR)
549 goto exit;
550
551 /* Reorder the sensors values:
552 *
553 * The values are laid out like this:
554 * -, Y1, Y2, -, Y3, Y4, -, ..., -, X1, X2, -, X3, X4, ...
555 * '-' is an unused value.
556 */
557
558 /* read X values */
559 for (i = 0, j = 19; i < 20; i += 2, j += 3) {
560 dev->xy_cur[i] = dev->data[j + 1];
561 dev->xy_cur[i + 1] = dev->data[j + 2];
562 }
563 /* read Y values */
564 for (i = 0, j = 1; i < 9; i += 2, j += 3) {
565 dev->xy_cur[ATP_XSENSORS + i] = dev->data[j + 1];
566 dev->xy_cur[ATP_XSENSORS + i + 1] = dev->data[j + 2];
567 }
568
569 dbg_dump("sample", dev->xy_cur);
570
571 if (!dev->valid) {
572 /* first sample */
573 dev->valid = true;
574 dev->x_old = dev->y_old = -1;
575 memcpy(dev->xy_old, dev->xy_cur, sizeof(dev->xy_old));
576
Stelian Popf7214ff2005-09-08 10:19:48 +0200577 goto exit;
578 }
579
580 for (i = 0; i < ATP_XSENSORS + ATP_YSENSORS; i++) {
581 /* accumulate the change */
582 signed char change = dev->xy_old[i] - dev->xy_cur[i];
583 dev->xy_acc[i] -= change;
584
585 /* prevent down drifting */
586 if (dev->xy_acc[i] < 0)
587 dev->xy_acc[i] = 0;
588 }
589
590 memcpy(dev->xy_old, dev->xy_cur, sizeof(dev->xy_old));
591
592 dbg_dump("accumulator", dev->xy_acc);
593
594 x = atp_calculate_abs(dev->xy_acc, ATP_XSENSORS,
595 ATP_XFACT, &x_z, &x_f);
596 y = atp_calculate_abs(dev->xy_acc + ATP_XSENSORS, ATP_YSENSORS,
597 ATP_YFACT, &y_z, &y_f);
Thomas Rohwercb560732007-09-25 00:06:25 -0400598 key = dev->data[dev->datalen - 1] & 1;
Stelian Popf7214ff2005-09-08 10:19:48 +0200599
600 if (x && y) {
601 if (dev->x_old != -1) {
602 x = (dev->x_old * 3 + x) >> 2;
603 y = (dev->y_old * 3 + y) >> 2;
604 dev->x_old = x;
605 dev->y_old = y;
606
607 if (debug > 1)
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400608 printk(KERN_DEBUG "appletouch: X: %3d Y: %3d "
Stelian Popf7214ff2005-09-08 10:19:48 +0200609 "Xz: %3d Yz: %3d\n",
610 x, y, x_z, y_z);
611
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500612 input_report_key(dev->input, BTN_TOUCH, 1);
613 input_report_abs(dev->input, ABS_X, x);
614 input_report_abs(dev->input, ABS_Y, y);
615 input_report_abs(dev->input, ABS_PRESSURE,
Stelian Popf7214ff2005-09-08 10:19:48 +0200616 min(ATP_PRESSURE, x_z + y_z));
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500617 atp_report_fingers(dev->input, max(x_f, y_f));
Stelian Popf7214ff2005-09-08 10:19:48 +0200618 }
619 dev->x_old = x;
620 dev->y_old = y;
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400621
622 } else if (!x && !y) {
Stelian Popf7214ff2005-09-08 10:19:48 +0200623
624 dev->x_old = dev->y_old = -1;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500625 input_report_key(dev->input, BTN_TOUCH, 0);
626 input_report_abs(dev->input, ABS_PRESSURE, 0);
627 atp_report_fingers(dev->input, 0);
Stelian Popf7214ff2005-09-08 10:19:48 +0200628
629 /* reset the accumulator on release */
630 memset(dev->xy_acc, 0, sizeof(dev->xy_acc));
Soeren Sonnenburg937ad5c2007-10-13 00:31:15 -0400631 }
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400632
Anton Ekblad46249ea2007-10-22 00:59:59 -0400633 input_report_key(dev->input, BTN_LEFT, key);
634 input_sync(dev->input);
635
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400636 /*
Sven Andersd83d2132008-08-08 16:31:31 -0400637 * Geysers 3/4 will continue to send packets continually after
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400638 * the first touch unless reinitialised. Do so if it's been
639 * idle for a while in order to avoid waking the kernel up
Sven Andersd83d2132008-08-08 16:31:31 -0400640 * several hundred times a second.
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400641 */
Sven Andersd83d2132008-08-08 16:31:31 -0400642
643 /*
644 * Button must not be pressed when entering suspend,
645 * otherwise we will never release the button.
646 */
647 if (!x && !y && !key) {
648 dev->idlecount++;
649 if (dev->idlecount == 10) {
650 dev->valid = false;
651 schedule_work(&dev->work);
652 /* Don't resubmit urb here, wait for reinit */
653 return;
654 }
655 } else
656 dev->idlecount = 0;
Stelian Popf7214ff2005-09-08 10:19:48 +0200657
Johannes Berg7dce8692008-05-05 23:56:55 -0400658 exit:
Stelian Popf7214ff2005-09-08 10:19:48 +0200659 retval = usb_submit_urb(dev->urb, GFP_ATOMIC);
Johannes Berg7dce8692008-05-05 23:56:55 -0400660 if (retval)
661 err("atp_complete: usb_submit_urb failed with result %d",
662 retval);
Stelian Popf7214ff2005-09-08 10:19:48 +0200663}
664
665static int atp_open(struct input_dev *input)
666{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400667 struct atp *dev = input_get_drvdata(input);
Stelian Popf7214ff2005-09-08 10:19:48 +0200668
669 if (usb_submit_urb(dev->urb, GFP_ATOMIC))
670 return -EIO;
671
672 dev->open = 1;
673 return 0;
674}
675
676static void atp_close(struct input_dev *input)
677{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400678 struct atp *dev = input_get_drvdata(input);
Stelian Popf7214ff2005-09-08 10:19:48 +0200679
680 usb_kill_urb(dev->urb);
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400681 cancel_work_sync(&dev->work);
Stelian Popf7214ff2005-09-08 10:19:48 +0200682 dev->open = 0;
683}
684
Oliver Neukum90d95ef2008-06-17 11:56:55 -0400685static int atp_handle_geyser(struct atp *dev)
686{
687 struct usb_device *udev = dev->udev;
688
Dmitry Torokhov15439dd2008-06-17 12:02:44 -0400689 if (dev->type != ATP_FOUNTAIN) {
Oliver Neukum90d95ef2008-06-17 11:56:55 -0400690 /* switch to raw sensor mode */
691 if (atp_geyser_init(udev))
692 return -EIO;
693
694 printk(KERN_INFO "appletouch: Geyser mode initialized.\n");
695 }
696
697 return 0;
698}
699
Johannes Berg7dce8692008-05-05 23:56:55 -0400700static int atp_probe(struct usb_interface *iface,
701 const struct usb_device_id *id)
Stelian Popf7214ff2005-09-08 10:19:48 +0200702{
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500703 struct atp *dev;
704 struct input_dev *input_dev;
705 struct usb_device *udev = interface_to_usbdev(iface);
Stelian Popf7214ff2005-09-08 10:19:48 +0200706 struct usb_host_interface *iface_desc;
707 struct usb_endpoint_descriptor *endpoint;
708 int int_in_endpointAddr = 0;
Dmitry Torokhov50141862007-04-12 01:33:39 -0400709 int i, error = -ENOMEM;
Stelian Popf7214ff2005-09-08 10:19:48 +0200710
711 /* set up the endpoint information */
712 /* use only the first interrupt-in endpoint */
713 iface_desc = iface->cur_altsetting;
714 for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
715 endpoint = &iface_desc->endpoint[i].desc;
Luiz Fernando N. Capitulino97b107c2006-09-27 11:58:53 -0700716 if (!int_in_endpointAddr && usb_endpoint_is_int_in(endpoint)) {
Stelian Popf7214ff2005-09-08 10:19:48 +0200717 /* we found an interrupt in endpoint */
718 int_in_endpointAddr = endpoint->bEndpointAddress;
719 break;
720 }
721 }
722 if (!int_in_endpointAddr) {
Stelian Popf7214ff2005-09-08 10:19:48 +0200723 err("Could not find int-in endpoint");
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500724 return -EIO;
Stelian Popf7214ff2005-09-08 10:19:48 +0200725 }
726
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500727 /* allocate memory for our device state and initialize it */
728 dev = kzalloc(sizeof(struct atp), GFP_KERNEL);
729 input_dev = input_allocate_device();
730 if (!dev || !input_dev) {
731 err("Out of memory");
732 goto err_free_devs;
733 }
734
735 dev->udev = udev;
736 dev->input = input_dev;
Sven Anderse9542df2008-05-05 23:57:10 -0400737 dev->type = id->driver_info;
Johannes Berg7dce8692008-05-05 23:56:55 -0400738 dev->overflow_warned = false;
Sven Anderse9542df2008-05-05 23:57:10 -0400739 if (dev->type == ATP_FOUNTAIN || dev->type == ATP_GEYSER1)
Nicolas Boichat9effa972006-04-19 23:36:40 +0200740 dev->datalen = 81;
Sven Anderse9542df2008-05-05 23:57:10 -0400741 else
742 dev->datalen = 64;
Nicolas Boichat9effa972006-04-19 23:36:40 +0200743
Stelian Popf7214ff2005-09-08 10:19:48 +0200744 dev->urb = usb_alloc_urb(0, GFP_KERNEL);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400745 if (!dev->urb)
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500746 goto err_free_devs;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500747
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500748 dev->data = usb_buffer_alloc(dev->udev, dev->datalen, GFP_KERNEL,
Stelian Popf7214ff2005-09-08 10:19:48 +0200749 &dev->urb->transfer_dma);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400750 if (!dev->data)
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500751 goto err_free_urb;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500752
Sven Andersd83d2132008-08-08 16:31:31 -0400753 /* Select the USB complete (callback) function */
754 if (dev->type == ATP_FOUNTAIN ||
755 dev->type == ATP_GEYSER1 ||
756 dev->type == ATP_GEYSER2)
757 usb_fill_int_urb(dev->urb, udev,
758 usb_rcvintpipe(udev, int_in_endpointAddr),
759 dev->data, dev->datalen,
760 atp_complete_geyser_1_2, dev, 1);
761 else
762 usb_fill_int_urb(dev->urb, udev,
763 usb_rcvintpipe(udev, int_in_endpointAddr),
764 dev->data, dev->datalen,
765 atp_complete_geyser_3_4, dev, 1);
Stelian Popf7214ff2005-09-08 10:19:48 +0200766
Oliver Neukum90d95ef2008-06-17 11:56:55 -0400767 error = atp_handle_geyser(dev);
768 if (error)
769 goto err_free_buffer;
770
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500771 usb_make_path(udev, dev->phys, sizeof(dev->phys));
772 strlcat(dev->phys, "/input0", sizeof(dev->phys));
Stelian Popf7214ff2005-09-08 10:19:48 +0200773
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500774 input_dev->name = "appletouch";
775 input_dev->phys = dev->phys;
776 usb_to_input_id(dev->udev, &input_dev->id);
Dmitry Torokhovc0f82d52007-04-12 01:35:03 -0400777 input_dev->dev.parent = &iface->dev;
Stelian Popf7214ff2005-09-08 10:19:48 +0200778
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400779 input_set_drvdata(input_dev, dev);
780
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500781 input_dev->open = atp_open;
782 input_dev->close = atp_close;
783
784 set_bit(EV_ABS, input_dev->evbit);
Stelian Popf7214ff2005-09-08 10:19:48 +0200785
Sven Anderse9542df2008-05-05 23:57:10 -0400786 if (dev->type == ATP_GEYSER3 || dev->type == ATP_GEYSER4) {
Nicolas Boichat9effa972006-04-19 23:36:40 +0200787 /*
788 * MacBook have 20 X sensors, 10 Y sensors
789 */
790 input_set_abs_params(input_dev, ABS_X, 0,
791 ((20 - 1) * ATP_XFACT) - 1, ATP_FUZZ, 0);
792 input_set_abs_params(input_dev, ABS_Y, 0,
793 ((10 - 1) * ATP_YFACT) - 1, ATP_FUZZ, 0);
Sven Anderse9542df2008-05-05 23:57:10 -0400794 } else if (dev->type == ATP_GEYSER2) {
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500795 /*
796 * Oct 2005 15" PowerBooks have 15 X sensors, 17" are detected
797 * later.
798 */
799 input_set_abs_params(input_dev, ABS_X, 0,
800 ((15 - 1) * ATP_XFACT) - 1, ATP_FUZZ, 0);
801 input_set_abs_params(input_dev, ABS_Y, 0,
802 ((9 - 1) * ATP_YFACT) - 1, ATP_FUZZ, 0);
803 } else {
804 /*
805 * 12" and 15" Powerbooks only have 16 x sensors,
806 * 17" models are detected later.
807 */
808 input_set_abs_params(input_dev, ABS_X, 0,
Johannes Berg7dce8692008-05-05 23:56:55 -0400809 (16 - 1) * ATP_XFACT - 1,
810 ATP_FUZZ, 0);
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500811 input_set_abs_params(input_dev, ABS_Y, 0,
Johannes Berg7dce8692008-05-05 23:56:55 -0400812 (ATP_YSENSORS - 1) * ATP_YFACT - 1,
813 ATP_FUZZ, 0);
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500814 }
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500815 input_set_abs_params(input_dev, ABS_PRESSURE, 0, ATP_PRESSURE, 0, 0);
Stelian Popf7214ff2005-09-08 10:19:48 +0200816
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500817 set_bit(EV_KEY, input_dev->evbit);
818 set_bit(BTN_TOUCH, input_dev->keybit);
819 set_bit(BTN_TOOL_FINGER, input_dev->keybit);
820 set_bit(BTN_TOOL_DOUBLETAP, input_dev->keybit);
821 set_bit(BTN_TOOL_TRIPLETAP, input_dev->keybit);
822 set_bit(BTN_LEFT, input_dev->keybit);
Stelian Popf7214ff2005-09-08 10:19:48 +0200823
Dmitry Torokhov50141862007-04-12 01:33:39 -0400824 error = input_register_device(dev->input);
825 if (error)
826 goto err_free_buffer;
Stelian Popf7214ff2005-09-08 10:19:48 +0200827
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500828 /* save our data pointer in this interface device */
829 usb_set_intfdata(iface, dev);
Stelian Popf7214ff2005-09-08 10:19:48 +0200830
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400831 INIT_WORK(&dev->work, atp_reinit);
832
Stelian Popf7214ff2005-09-08 10:19:48 +0200833 return 0;
834
Dmitry Torokhov50141862007-04-12 01:33:39 -0400835 err_free_buffer:
836 usb_buffer_free(dev->udev, dev->datalen,
837 dev->data, dev->urb->transfer_dma);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500838 err_free_urb:
Stelian Popf7214ff2005-09-08 10:19:48 +0200839 usb_free_urb(dev->urb);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500840 err_free_devs:
Stelian Popf7214ff2005-09-08 10:19:48 +0200841 usb_set_intfdata(iface, NULL);
Stelian Popf7214ff2005-09-08 10:19:48 +0200842 kfree(dev);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500843 input_free_device(input_dev);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400844 return error;
Stelian Popf7214ff2005-09-08 10:19:48 +0200845}
846
847static void atp_disconnect(struct usb_interface *iface)
848{
849 struct atp *dev = usb_get_intfdata(iface);
850
851 usb_set_intfdata(iface, NULL);
852 if (dev) {
853 usb_kill_urb(dev->urb);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500854 input_unregister_device(dev->input);
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500855 usb_buffer_free(dev->udev, dev->datalen,
Stelian Popf7214ff2005-09-08 10:19:48 +0200856 dev->data, dev->urb->transfer_dma);
Johannes Berg7af569a2006-07-19 15:39:46 +0200857 usb_free_urb(dev->urb);
Stelian Popf7214ff2005-09-08 10:19:48 +0200858 kfree(dev);
859 }
860 printk(KERN_INFO "input: appletouch disconnected\n");
861}
862
Oliver Neukum90d95ef2008-06-17 11:56:55 -0400863static int atp_recover(struct atp *dev)
864{
865 int error;
866
867 error = atp_handle_geyser(dev);
868 if (error)
869 return error;
870
871 if (dev->open && usb_submit_urb(dev->urb, GFP_ATOMIC))
872 return -EIO;
873
874 return 0;
875}
876
Stelian Popf7214ff2005-09-08 10:19:48 +0200877static int atp_suspend(struct usb_interface *iface, pm_message_t message)
878{
879 struct atp *dev = usb_get_intfdata(iface);
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400880
Stelian Popf7214ff2005-09-08 10:19:48 +0200881 usb_kill_urb(dev->urb);
Johannes Berg7dce8692008-05-05 23:56:55 -0400882 dev->valid = false;
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400883
Stelian Popf7214ff2005-09-08 10:19:48 +0200884 return 0;
885}
886
887static int atp_resume(struct usb_interface *iface)
888{
889 struct atp *dev = usb_get_intfdata(iface);
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400890
Stelian Popf7214ff2005-09-08 10:19:48 +0200891 if (dev->open && usb_submit_urb(dev->urb, GFP_ATOMIC))
892 return -EIO;
893
894 return 0;
895}
896
Oliver Neukum90d95ef2008-06-17 11:56:55 -0400897static int atp_reset_resume(struct usb_interface *iface)
898{
899 struct atp *dev = usb_get_intfdata(iface);
900
901 return atp_recover(dev);
902}
903
Stelian Popf7214ff2005-09-08 10:19:48 +0200904static struct usb_driver atp_driver = {
Stelian Popf7214ff2005-09-08 10:19:48 +0200905 .name = "appletouch",
906 .probe = atp_probe,
907 .disconnect = atp_disconnect,
908 .suspend = atp_suspend,
909 .resume = atp_resume,
Oliver Neukum90d95ef2008-06-17 11:56:55 -0400910 .reset_resume = atp_reset_resume,
Stelian Popf7214ff2005-09-08 10:19:48 +0200911 .id_table = atp_table,
912};
913
914static int __init atp_init(void)
915{
916 return usb_register(&atp_driver);
917}
918
919static void __exit atp_exit(void)
920{
921 usb_deregister(&atp_driver);
922}
923
924module_init(atp_init);
925module_exit(atp_exit);