blob: 079816e6b23b042bff9a883aa984ffea757ee5a6 [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
Sven Anders82a196f2008-08-08 16:31:33 -0400139/**
140 * enum atp_status_bits - status bit meanings
141 *
142 * These constants represent the meaning of the status bits.
143 * (only Geyser 3/4)
144 *
145 * @ATP_STATUS_BUTTON: The button was pressed
146 * @ATP_STATUS_BASE_UPDATE: Update of the base values (untouched pad)
147 * @ATP_STATUS_FROM_RESET: Reset previously performed
148 */
149enum atp_status_bits {
150 ATP_STATUS_BUTTON = BIT(0),
151 ATP_STATUS_BASE_UPDATE = BIT(2),
152 ATP_STATUS_FROM_RESET = BIT(4),
153};
154
Stelian Popf7214ff2005-09-08 10:19:48 +0200155/* Structure to hold all of our device specific stuff */
156struct atp {
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500157 char phys[64];
Johannes Berg7dce8692008-05-05 23:56:55 -0400158 struct usb_device *udev; /* usb device */
159 struct urb *urb; /* usb request block */
Sven Anders82a196f2008-08-08 16:31:33 -0400160 u8 *data; /* transferred data */
Johannes Berg7dce8692008-05-05 23:56:55 -0400161 struct input_dev *input; /* input dev */
Sven Anderse9542df2008-05-05 23:57:10 -0400162 enum atp_touchpad_type type; /* type of touchpad */
Johannes Berg7dce8692008-05-05 23:56:55 -0400163 bool open;
164 bool valid; /* are the samples valid? */
165 bool size_detect_done;
166 bool overflow_warned;
Stelian Popf7214ff2005-09-08 10:19:48 +0200167 int x_old; /* last reported x/y, */
168 int y_old; /* used for smoothing */
Stelian Popf7214ff2005-09-08 10:19:48 +0200169 signed char xy_cur[ATP_XSENSORS + ATP_YSENSORS];
Stelian Popf7214ff2005-09-08 10:19:48 +0200170 signed char xy_old[ATP_XSENSORS + ATP_YSENSORS];
Stelian Popf7214ff2005-09-08 10:19:48 +0200171 int xy_acc[ATP_XSENSORS + ATP_YSENSORS];
Johannes Berg7dce8692008-05-05 23:56:55 -0400172 int datalen; /* size of USB transfer */
173 int idlecount; /* number of empty packets */
174 struct work_struct work;
Stelian Popf7214ff2005-09-08 10:19:48 +0200175};
176
177#define dbg_dump(msg, tab) \
178 if (debug > 1) { \
Johannes Berg7dce8692008-05-05 23:56:55 -0400179 int __i; \
180 printk(KERN_DEBUG "appletouch: %s", msg); \
181 for (__i = 0; __i < ATP_XSENSORS + ATP_YSENSORS; __i++) \
182 printk(" %02x", tab[__i]); \
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500183 printk("\n"); \
Stelian Popf7214ff2005-09-08 10:19:48 +0200184 }
185
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500186#define dprintk(format, a...) \
Stelian Popf7214ff2005-09-08 10:19:48 +0200187 do { \
Johannes Berg7dce8692008-05-05 23:56:55 -0400188 if (debug) \
189 printk(KERN_DEBUG format, ##a); \
Stelian Popf7214ff2005-09-08 10:19:48 +0200190 } while (0)
191
Johannes Berg7dce8692008-05-05 23:56:55 -0400192MODULE_AUTHOR("Johannes Berg");
193MODULE_AUTHOR("Stelian Pop");
194MODULE_AUTHOR("Frank Arnold");
195MODULE_AUTHOR("Michael Hanselmann");
196MODULE_AUTHOR("Sven Anders");
197MODULE_DESCRIPTION("Apple PowerBook and MacBook USB touchpad driver");
Stelian Popf7214ff2005-09-08 10:19:48 +0200198MODULE_LICENSE("GPL");
199
Jason Parekh00081d32006-11-17 01:05:58 -0500200/*
201 * Make the threshold a module parameter
202 */
203static int threshold = ATP_THRESHOLD;
204module_param(threshold, int, 0644);
Johannes Berg7dce8692008-05-05 23:56:55 -0400205MODULE_PARM_DESC(threshold, "Discard any change in data from a sensor"
206 " (the trackpad has many of these sensors)"
207 " less than this value.");
Jason Parekh00081d32006-11-17 01:05:58 -0500208
Johannes Berg7dce8692008-05-05 23:56:55 -0400209static int debug;
Stelian Popf7214ff2005-09-08 10:19:48 +0200210module_param(debug, int, 0644);
211MODULE_PARM_DESC(debug, "Activate debugging output");
212
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400213/*
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400214 * By default newer Geyser devices send standard USB HID mouse
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400215 * packets (Report ID 2). This code changes device mode, so it
216 * sends raw sensor reports (Report ID 5).
217 */
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400218static int atp_geyser_init(struct usb_device *udev)
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400219{
220 char data[8];
221 int size;
Johannes Berg7dce8692008-05-05 23:56:55 -0400222 int i;
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400223
224 size = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400225 ATP_GEYSER_MODE_READ_REQUEST_ID,
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400226 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400227 ATP_GEYSER_MODE_REQUEST_VALUE,
228 ATP_GEYSER_MODE_REQUEST_INDEX, &data, 8, 5000);
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400229
230 if (size != 8) {
Johannes Berg7dce8692008-05-05 23:56:55 -0400231 dprintk("atp_geyser_init: read error\n");
232 for (i = 0; i < 8; i++)
233 dprintk("appletouch[%d]: %d\n", i, data[i]);
234
235 err("Failed to read mode from device.");
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400236 return -EIO;
237 }
238
239 /* Apply the mode switch */
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400240 data[0] = ATP_GEYSER_MODE_VENDOR_VALUE;
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400241
242 size = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400243 ATP_GEYSER_MODE_WRITE_REQUEST_ID,
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400244 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400245 ATP_GEYSER_MODE_REQUEST_VALUE,
246 ATP_GEYSER_MODE_REQUEST_INDEX, &data, 8, 5000);
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400247
248 if (size != 8) {
Johannes Berg7dce8692008-05-05 23:56:55 -0400249 dprintk("atp_geyser_init: write error\n");
250 for (i = 0; i < 8; i++)
251 dprintk("appletouch[%d]: %d\n", i, data[i]);
252
253 err("Failed to request geyser raw mode");
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400254 return -EIO;
255 }
256 return 0;
257}
258
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400259/*
260 * Reinitialise the device. This usually stops stream of empty packets
261 * coming from it.
262 */
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400263static void atp_reinit(struct work_struct *work)
264{
265 struct atp *dev = container_of(work, struct atp, work);
266 struct usb_device *udev = dev->udev;
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400267 int retval;
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400268
Johannes Berg7dce8692008-05-05 23:56:55 -0400269 dprintk("appletouch: putting appletouch to sleep (reinit)\n");
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400270 atp_geyser_init(udev);
271
272 retval = usb_submit_urb(dev->urb, GFP_ATOMIC);
Johannes Berg7dce8692008-05-05 23:56:55 -0400273 if (retval)
274 err("atp_reinit: usb_submit_urb failed with error %d",
275 retval);
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400276}
277
Stelian Popf7214ff2005-09-08 10:19:48 +0200278static int atp_calculate_abs(int *xy_sensors, int nb_sensors, int fact,
279 int *z, int *fingers)
280{
281 int i;
282 /* values to calculate mean */
283 int pcum = 0, psum = 0;
Jason Parekh00081d32006-11-17 01:05:58 -0500284 int is_increasing = 0;
Stelian Popf7214ff2005-09-08 10:19:48 +0200285
286 *fingers = 0;
287
288 for (i = 0; i < nb_sensors; i++) {
Jason Parekh00081d32006-11-17 01:05:58 -0500289 if (xy_sensors[i] < threshold) {
290 if (is_increasing)
291 is_increasing = 0;
292
Stelian Popf7214ff2005-09-08 10:19:48 +0200293 continue;
Jason Parekh00081d32006-11-17 01:05:58 -0500294 }
295
296 /*
297 * Makes the finger detection more versatile. For example,
298 * two fingers with no gap will be detected. Also, my
299 * tests show it less likely to have intermittent loss
300 * of multiple finger readings while moving around (scrolling).
301 *
302 * Changes the multiple finger detection to counting humps on
303 * sensors (transitions from nonincreasing to increasing)
304 * instead of counting transitions from low sensors (no
305 * finger reading) to high sensors (finger above
306 * sensor)
307 *
308 * - Jason Parekh <jasonparekh@gmail.com>
309 */
Johannes Berg7dce8692008-05-05 23:56:55 -0400310 if (i < 1 ||
311 (!is_increasing && xy_sensors[i - 1] < xy_sensors[i])) {
Stelian Popf7214ff2005-09-08 10:19:48 +0200312 (*fingers)++;
Jason Parekh00081d32006-11-17 01:05:58 -0500313 is_increasing = 1;
314 } else if (i > 0 && xy_sensors[i - 1] >= xy_sensors[i]) {
315 is_increasing = 0;
316 }
317
318 /*
Johannes Berg7dce8692008-05-05 23:56:55 -0400319 * Subtracts threshold so a high sensor that just passes the
320 * threshold won't skew the calculated absolute coordinate.
321 * Fixes an issue where slowly moving the mouse would
322 * occasionally jump a number of pixels (slowly moving the
323 * finger makes this issue most apparent.)
Jason Parekh00081d32006-11-17 01:05:58 -0500324 */
325 pcum += (xy_sensors[i] - threshold) * i;
326 psum += (xy_sensors[i] - threshold);
Stelian Popf7214ff2005-09-08 10:19:48 +0200327 }
328
329 if (psum > 0) {
330 *z = psum;
331 return pcum * fact / psum;
332 }
333
334 return 0;
335}
336
337static inline void atp_report_fingers(struct input_dev *input, int fingers)
338{
339 input_report_key(input, BTN_TOOL_FINGER, fingers == 1);
340 input_report_key(input, BTN_TOOL_DOUBLETAP, fingers == 2);
341 input_report_key(input, BTN_TOOL_TRIPLETAP, fingers > 2);
342}
343
Sven Andersd83d2132008-08-08 16:31:31 -0400344/* Check URB status and for correct length of data package */
345
346#define ATP_URB_STATUS_SUCCESS 0
347#define ATP_URB_STATUS_ERROR 1
348#define ATP_URB_STATUS_ERROR_FATAL 2
349
350static int atp_status_check(struct urb *urb)
Stelian Popf7214ff2005-09-08 10:19:48 +0200351{
Stelian Popf7214ff2005-09-08 10:19:48 +0200352 struct atp *dev = urb->context;
353
354 switch (urb->status) {
355 case 0:
356 /* success */
357 break;
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500358 case -EOVERFLOW:
Johannes Berg7dce8692008-05-05 23:56:55 -0400359 if (!dev->overflow_warned) {
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400360 printk(KERN_WARNING "appletouch: OVERFLOW with data "
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500361 "length %d, actual length is %d\n",
362 dev->datalen, dev->urb->actual_length);
Johannes Berg7dce8692008-05-05 23:56:55 -0400363 dev->overflow_warned = true;
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500364 }
Stelian Popf7214ff2005-09-08 10:19:48 +0200365 case -ECONNRESET:
366 case -ENOENT:
367 case -ESHUTDOWN:
368 /* This urb is terminated, clean up */
Johannes Berg7dce8692008-05-05 23:56:55 -0400369 dbg("atp_complete: urb shutting down with status: %d",
370 urb->status);
Sven Andersd83d2132008-08-08 16:31:31 -0400371 return ATP_URB_STATUS_ERROR_FATAL;
372
Stelian Popf7214ff2005-09-08 10:19:48 +0200373 default:
Johannes Berg7dce8692008-05-05 23:56:55 -0400374 dbg("atp_complete: nonzero urb status received: %d",
375 urb->status);
Sven Andersd83d2132008-08-08 16:31:31 -0400376 return ATP_URB_STATUS_ERROR;
Stelian Popf7214ff2005-09-08 10:19:48 +0200377 }
378
379 /* drop incomplete datasets */
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500380 if (dev->urb->actual_length != dev->datalen) {
Nicolas Boichat9effa972006-04-19 23:36:40 +0200381 dprintk("appletouch: incomplete data package"
382 " (first byte: %d, length: %d).\n",
383 dev->data[0], dev->urb->actual_length);
Sven Andersd83d2132008-08-08 16:31:31 -0400384 return ATP_URB_STATUS_ERROR;
Stelian Popf7214ff2005-09-08 10:19:48 +0200385 }
386
Sven Andersd83d2132008-08-08 16:31:31 -0400387 return ATP_URB_STATUS_SUCCESS;
388}
389
390/*
391 * USB interrupt callback functions
392 */
393
394/* Interrupt function for older touchpads: FOUNTAIN/GEYSER1/GEYSER2 */
395
396static void atp_complete_geyser_1_2(struct urb *urb)
397{
398 int x, y, x_z, y_z, x_f, y_f;
399 int retval, i, j;
400 int key;
401 struct atp *dev = urb->context;
402 int status = atp_status_check(urb);
403
404 if (status == ATP_URB_STATUS_ERROR_FATAL)
405 return;
406 else if (status == ATP_URB_STATUS_ERROR)
407 goto exit;
408
Stelian Popf7214ff2005-09-08 10:19:48 +0200409 /* reorder the sensors values */
Sven Andersd83d2132008-08-08 16:31:31 -0400410 if (dev->type == ATP_GEYSER2) {
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500411 memset(dev->xy_cur, 0, sizeof(dev->xy_cur));
Stelian Popf7214ff2005-09-08 10:19:48 +0200412
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500413 /*
414 * The values are laid out like this:
415 * Y1, Y2, -, Y3, Y4, -, ..., X1, X2, -, X3, X4, -, ...
416 * '-' is an unused value.
417 */
418
419 /* read X values */
420 for (i = 0, j = 19; i < 20; i += 2, j += 3) {
421 dev->xy_cur[i] = dev->data[j];
422 dev->xy_cur[i + 1] = dev->data[j + 1];
423 }
424
425 /* read Y values */
426 for (i = 0, j = 1; i < 9; i += 2, j += 3) {
427 dev->xy_cur[ATP_XSENSORS + i] = dev->data[j];
428 dev->xy_cur[ATP_XSENSORS + i + 1] = dev->data[j + 1];
429 }
430 } else {
431 for (i = 0; i < 8; i++) {
432 /* X values */
Johannes Berg7dce8692008-05-05 23:56:55 -0400433 dev->xy_cur[i + 0] = dev->data[5 * i + 2];
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500434 dev->xy_cur[i + 8] = dev->data[5 * i + 4];
435 dev->xy_cur[i + 16] = dev->data[5 * i + 42];
436 if (i < 2)
437 dev->xy_cur[i + 24] = dev->data[5 * i + 44];
438
439 /* Y values */
440 dev->xy_cur[i + 26] = dev->data[5 * i + 1];
441 dev->xy_cur[i + 34] = dev->data[5 * i + 3];
442 }
Stelian Popf7214ff2005-09-08 10:19:48 +0200443 }
444
445 dbg_dump("sample", dev->xy_cur);
446
447 if (!dev->valid) {
448 /* first sample */
Johannes Berg7dce8692008-05-05 23:56:55 -0400449 dev->valid = true;
Stelian Popf7214ff2005-09-08 10:19:48 +0200450 dev->x_old = dev->y_old = -1;
Sven Andersd83d2132008-08-08 16:31:31 -0400451
452 /* Store first sample */
Stelian Popf7214ff2005-09-08 10:19:48 +0200453 memcpy(dev->xy_old, dev->xy_cur, sizeof(dev->xy_old));
454
Sven Andersd83d2132008-08-08 16:31:31 -0400455 /* Perform size detection, if not done already */
456 if (!dev->size_detect_done) {
457
458 /* 17" Powerbooks have extra X sensors */
459 for (i = (dev->type == ATP_GEYSER2 ? 15 : 16);
460 i < ATP_XSENSORS; i++) {
461 if (!dev->xy_cur[i])
462 continue;
463
464 printk(KERN_INFO
465 "appletouch: 17\" model detected.\n");
466
467 if (dev->type == ATP_GEYSER2)
468 input_set_abs_params(dev->input, ABS_X,
469 0,
470 (20 - 1) *
471 ATP_XFACT - 1,
472 ATP_FUZZ, 0);
473 else
474 input_set_abs_params(dev->input, ABS_X,
475 0,
476 (26 - 1) *
477 ATP_XFACT - 1,
478 ATP_FUZZ, 0);
479 break;
480 }
481
482 dev->size_detect_done = 1;
Nicolas Boichat9effa972006-04-19 23:36:40 +0200483 goto exit;
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500484 }
Sven Andersd83d2132008-08-08 16:31:31 -0400485 }
Stelian Popf7214ff2005-09-08 10:19:48 +0200486
Sven Andersd83d2132008-08-08 16:31:31 -0400487 for (i = 0; i < ATP_XSENSORS + ATP_YSENSORS; i++) {
488 /* accumulate the change */
489 signed char change = dev->xy_old[i] - dev->xy_cur[i];
490 dev->xy_acc[i] -= change;
491
492 /* prevent down drifting */
493 if (dev->xy_acc[i] < 0)
494 dev->xy_acc[i] = 0;
495 }
496
497 memcpy(dev->xy_old, dev->xy_cur, sizeof(dev->xy_old));
498
499 dbg_dump("accumulator", dev->xy_acc);
500
501 x = atp_calculate_abs(dev->xy_acc, ATP_XSENSORS,
502 ATP_XFACT, &x_z, &x_f);
503 y = atp_calculate_abs(dev->xy_acc + ATP_XSENSORS, ATP_YSENSORS,
504 ATP_YFACT, &y_z, &y_f);
Sven Anders82a196f2008-08-08 16:31:33 -0400505 key = dev->data[dev->datalen - 1] & ATP_STATUS_BUTTON;
Sven Andersd83d2132008-08-08 16:31:31 -0400506
507 if (x && y) {
508 if (dev->x_old != -1) {
509 x = (dev->x_old * 3 + x) >> 2;
510 y = (dev->y_old * 3 + y) >> 2;
511 dev->x_old = x;
512 dev->y_old = y;
513
514 if (debug > 1)
515 printk(KERN_DEBUG "appletouch: "
516 "X: %3d Y: %3d Xz: %3d Yz: %3d\n",
517 x, y, x_z, y_z);
518
519 input_report_key(dev->input, BTN_TOUCH, 1);
520 input_report_abs(dev->input, ABS_X, x);
521 input_report_abs(dev->input, ABS_Y, y);
522 input_report_abs(dev->input, ABS_PRESSURE,
523 min(ATP_PRESSURE, x_z + y_z));
524 atp_report_fingers(dev->input, max(x_f, y_f));
525 }
526 dev->x_old = x;
527 dev->y_old = y;
528
529 } else if (!x && !y) {
530
531 dev->x_old = dev->y_old = -1;
532 input_report_key(dev->input, BTN_TOUCH, 0);
533 input_report_abs(dev->input, ABS_PRESSURE, 0);
534 atp_report_fingers(dev->input, 0);
535
536 /* reset the accumulator on release */
537 memset(dev->xy_acc, 0, sizeof(dev->xy_acc));
538 }
539
540 input_report_key(dev->input, BTN_LEFT, key);
541 input_sync(dev->input);
542
543 exit:
544 retval = usb_submit_urb(dev->urb, GFP_ATOMIC);
545 if (retval)
546 err("atp_complete: usb_submit_urb failed with result %d",
547 retval);
548}
549
550/* Interrupt function for older touchpads: GEYSER3/GEYSER4 */
551
552static void atp_complete_geyser_3_4(struct urb *urb)
553{
554 int x, y, x_z, y_z, x_f, y_f;
555 int retval, i, j;
556 int key;
557 struct atp *dev = urb->context;
558 int status = atp_status_check(urb);
559
560 if (status == ATP_URB_STATUS_ERROR_FATAL)
561 return;
562 else if (status == ATP_URB_STATUS_ERROR)
563 goto exit;
564
565 /* Reorder the sensors values:
566 *
567 * The values are laid out like this:
568 * -, Y1, Y2, -, Y3, Y4, -, ..., -, X1, X2, -, X3, X4, ...
569 * '-' is an unused value.
570 */
571
572 /* read X values */
573 for (i = 0, j = 19; i < 20; i += 2, j += 3) {
574 dev->xy_cur[i] = dev->data[j + 1];
575 dev->xy_cur[i + 1] = dev->data[j + 2];
576 }
577 /* read Y values */
578 for (i = 0, j = 1; i < 9; i += 2, j += 3) {
579 dev->xy_cur[ATP_XSENSORS + i] = dev->data[j + 1];
580 dev->xy_cur[ATP_XSENSORS + i + 1] = dev->data[j + 2];
581 }
582
583 dbg_dump("sample", dev->xy_cur);
584
Sven Anders82a196f2008-08-08 16:31:33 -0400585 /* Just update the base values (i.e. touchpad in untouched state) */
586 if (dev->data[dev->datalen - 1] & ATP_STATUS_BASE_UPDATE) {
Sven Andersd83d2132008-08-08 16:31:31 -0400587
Sven Anders82a196f2008-08-08 16:31:33 -0400588 dprintk(KERN_DEBUG "appletouch: updated base values\n");
589
590 memcpy(dev->xy_old, dev->xy_cur, sizeof(dev->xy_old));
Stelian Popf7214ff2005-09-08 10:19:48 +0200591 goto exit;
592 }
593
594 for (i = 0; i < ATP_XSENSORS + ATP_YSENSORS; i++) {
Sven Anders82a196f2008-08-08 16:31:33 -0400595 /* calculate the change */
596 dev->xy_acc[i] = dev->xy_cur[i] - dev->xy_old[i];
597
598 /* this is a round-robin value, so couple with that */
599 if (dev->xy_acc[i] > 127)
600 dev->xy_acc[i] -= 256;
601
602 if (dev->xy_acc[i] < -127)
603 dev->xy_acc[i] += 256;
Stelian Popf7214ff2005-09-08 10:19:48 +0200604
605 /* prevent down drifting */
606 if (dev->xy_acc[i] < 0)
607 dev->xy_acc[i] = 0;
608 }
609
Stelian Popf7214ff2005-09-08 10:19:48 +0200610 dbg_dump("accumulator", dev->xy_acc);
611
612 x = atp_calculate_abs(dev->xy_acc, ATP_XSENSORS,
613 ATP_XFACT, &x_z, &x_f);
614 y = atp_calculate_abs(dev->xy_acc + ATP_XSENSORS, ATP_YSENSORS,
615 ATP_YFACT, &y_z, &y_f);
Sven Anders82a196f2008-08-08 16:31:33 -0400616 key = dev->data[dev->datalen - 1] & ATP_STATUS_BUTTON;
Stelian Popf7214ff2005-09-08 10:19:48 +0200617
618 if (x && y) {
619 if (dev->x_old != -1) {
620 x = (dev->x_old * 3 + x) >> 2;
621 y = (dev->y_old * 3 + y) >> 2;
622 dev->x_old = x;
623 dev->y_old = y;
624
625 if (debug > 1)
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400626 printk(KERN_DEBUG "appletouch: X: %3d Y: %3d "
Stelian Popf7214ff2005-09-08 10:19:48 +0200627 "Xz: %3d Yz: %3d\n",
628 x, y, x_z, y_z);
629
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500630 input_report_key(dev->input, BTN_TOUCH, 1);
631 input_report_abs(dev->input, ABS_X, x);
632 input_report_abs(dev->input, ABS_Y, y);
633 input_report_abs(dev->input, ABS_PRESSURE,
Stelian Popf7214ff2005-09-08 10:19:48 +0200634 min(ATP_PRESSURE, x_z + y_z));
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500635 atp_report_fingers(dev->input, max(x_f, y_f));
Stelian Popf7214ff2005-09-08 10:19:48 +0200636 }
637 dev->x_old = x;
638 dev->y_old = y;
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400639
640 } else if (!x && !y) {
Stelian Popf7214ff2005-09-08 10:19:48 +0200641
642 dev->x_old = dev->y_old = -1;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500643 input_report_key(dev->input, BTN_TOUCH, 0);
644 input_report_abs(dev->input, ABS_PRESSURE, 0);
645 atp_report_fingers(dev->input, 0);
Stelian Popf7214ff2005-09-08 10:19:48 +0200646
647 /* reset the accumulator on release */
648 memset(dev->xy_acc, 0, sizeof(dev->xy_acc));
Soeren Sonnenburg937ad5c2007-10-13 00:31:15 -0400649 }
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400650
Anton Ekblad46249ea2007-10-22 00:59:59 -0400651 input_report_key(dev->input, BTN_LEFT, key);
652 input_sync(dev->input);
653
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400654 /*
Sven Andersd83d2132008-08-08 16:31:31 -0400655 * Geysers 3/4 will continue to send packets continually after
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400656 * the first touch unless reinitialised. Do so if it's been
657 * idle for a while in order to avoid waking the kernel up
Sven Andersd83d2132008-08-08 16:31:31 -0400658 * several hundred times a second.
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400659 */
Sven Andersd83d2132008-08-08 16:31:31 -0400660
661 /*
662 * Button must not be pressed when entering suspend,
663 * otherwise we will never release the button.
664 */
665 if (!x && !y && !key) {
666 dev->idlecount++;
667 if (dev->idlecount == 10) {
Sven Anders82a196f2008-08-08 16:31:33 -0400668 dev->x_old = dev->y_old = -1;
669 dev->idlecount = 0;
Sven Andersd83d2132008-08-08 16:31:31 -0400670 schedule_work(&dev->work);
671 /* Don't resubmit urb here, wait for reinit */
672 return;
673 }
674 } else
675 dev->idlecount = 0;
Stelian Popf7214ff2005-09-08 10:19:48 +0200676
Johannes Berg7dce8692008-05-05 23:56:55 -0400677 exit:
Stelian Popf7214ff2005-09-08 10:19:48 +0200678 retval = usb_submit_urb(dev->urb, GFP_ATOMIC);
Johannes Berg7dce8692008-05-05 23:56:55 -0400679 if (retval)
680 err("atp_complete: usb_submit_urb failed with result %d",
681 retval);
Stelian Popf7214ff2005-09-08 10:19:48 +0200682}
683
684static int atp_open(struct input_dev *input)
685{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400686 struct atp *dev = input_get_drvdata(input);
Stelian Popf7214ff2005-09-08 10:19:48 +0200687
688 if (usb_submit_urb(dev->urb, GFP_ATOMIC))
689 return -EIO;
690
691 dev->open = 1;
692 return 0;
693}
694
695static void atp_close(struct input_dev *input)
696{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400697 struct atp *dev = input_get_drvdata(input);
Stelian Popf7214ff2005-09-08 10:19:48 +0200698
699 usb_kill_urb(dev->urb);
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400700 cancel_work_sync(&dev->work);
Stelian Popf7214ff2005-09-08 10:19:48 +0200701 dev->open = 0;
702}
703
Oliver Neukum90d95ef2008-06-17 11:56:55 -0400704static int atp_handle_geyser(struct atp *dev)
705{
706 struct usb_device *udev = dev->udev;
707
Dmitry Torokhov15439dd2008-06-17 12:02:44 -0400708 if (dev->type != ATP_FOUNTAIN) {
Oliver Neukum90d95ef2008-06-17 11:56:55 -0400709 /* switch to raw sensor mode */
710 if (atp_geyser_init(udev))
711 return -EIO;
712
713 printk(KERN_INFO "appletouch: Geyser mode initialized.\n");
714 }
715
716 return 0;
717}
718
Johannes Berg7dce8692008-05-05 23:56:55 -0400719static int atp_probe(struct usb_interface *iface,
720 const struct usb_device_id *id)
Stelian Popf7214ff2005-09-08 10:19:48 +0200721{
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500722 struct atp *dev;
723 struct input_dev *input_dev;
724 struct usb_device *udev = interface_to_usbdev(iface);
Stelian Popf7214ff2005-09-08 10:19:48 +0200725 struct usb_host_interface *iface_desc;
726 struct usb_endpoint_descriptor *endpoint;
727 int int_in_endpointAddr = 0;
Dmitry Torokhov50141862007-04-12 01:33:39 -0400728 int i, error = -ENOMEM;
Stelian Popf7214ff2005-09-08 10:19:48 +0200729
730 /* set up the endpoint information */
731 /* use only the first interrupt-in endpoint */
732 iface_desc = iface->cur_altsetting;
733 for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
734 endpoint = &iface_desc->endpoint[i].desc;
Luiz Fernando N. Capitulino97b107c2006-09-27 11:58:53 -0700735 if (!int_in_endpointAddr && usb_endpoint_is_int_in(endpoint)) {
Stelian Popf7214ff2005-09-08 10:19:48 +0200736 /* we found an interrupt in endpoint */
737 int_in_endpointAddr = endpoint->bEndpointAddress;
738 break;
739 }
740 }
741 if (!int_in_endpointAddr) {
Stelian Popf7214ff2005-09-08 10:19:48 +0200742 err("Could not find int-in endpoint");
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500743 return -EIO;
Stelian Popf7214ff2005-09-08 10:19:48 +0200744 }
745
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500746 /* allocate memory for our device state and initialize it */
747 dev = kzalloc(sizeof(struct atp), GFP_KERNEL);
748 input_dev = input_allocate_device();
749 if (!dev || !input_dev) {
750 err("Out of memory");
751 goto err_free_devs;
752 }
753
754 dev->udev = udev;
755 dev->input = input_dev;
Sven Anderse9542df2008-05-05 23:57:10 -0400756 dev->type = id->driver_info;
Johannes Berg7dce8692008-05-05 23:56:55 -0400757 dev->overflow_warned = false;
Sven Anderse9542df2008-05-05 23:57:10 -0400758 if (dev->type == ATP_FOUNTAIN || dev->type == ATP_GEYSER1)
Nicolas Boichat9effa972006-04-19 23:36:40 +0200759 dev->datalen = 81;
Sven Anderse9542df2008-05-05 23:57:10 -0400760 else
761 dev->datalen = 64;
Nicolas Boichat9effa972006-04-19 23:36:40 +0200762
Stelian Popf7214ff2005-09-08 10:19:48 +0200763 dev->urb = usb_alloc_urb(0, GFP_KERNEL);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400764 if (!dev->urb)
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500765 goto err_free_devs;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500766
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500767 dev->data = usb_buffer_alloc(dev->udev, dev->datalen, GFP_KERNEL,
Stelian Popf7214ff2005-09-08 10:19:48 +0200768 &dev->urb->transfer_dma);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400769 if (!dev->data)
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500770 goto err_free_urb;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500771
Sven Andersd83d2132008-08-08 16:31:31 -0400772 /* Select the USB complete (callback) function */
773 if (dev->type == ATP_FOUNTAIN ||
774 dev->type == ATP_GEYSER1 ||
775 dev->type == ATP_GEYSER2)
776 usb_fill_int_urb(dev->urb, udev,
777 usb_rcvintpipe(udev, int_in_endpointAddr),
778 dev->data, dev->datalen,
779 atp_complete_geyser_1_2, dev, 1);
780 else
781 usb_fill_int_urb(dev->urb, udev,
782 usb_rcvintpipe(udev, int_in_endpointAddr),
783 dev->data, dev->datalen,
784 atp_complete_geyser_3_4, dev, 1);
Stelian Popf7214ff2005-09-08 10:19:48 +0200785
Oliver Neukum90d95ef2008-06-17 11:56:55 -0400786 error = atp_handle_geyser(dev);
787 if (error)
788 goto err_free_buffer;
789
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500790 usb_make_path(udev, dev->phys, sizeof(dev->phys));
791 strlcat(dev->phys, "/input0", sizeof(dev->phys));
Stelian Popf7214ff2005-09-08 10:19:48 +0200792
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500793 input_dev->name = "appletouch";
794 input_dev->phys = dev->phys;
795 usb_to_input_id(dev->udev, &input_dev->id);
Dmitry Torokhovc0f82d52007-04-12 01:35:03 -0400796 input_dev->dev.parent = &iface->dev;
Stelian Popf7214ff2005-09-08 10:19:48 +0200797
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400798 input_set_drvdata(input_dev, dev);
799
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500800 input_dev->open = atp_open;
801 input_dev->close = atp_close;
802
803 set_bit(EV_ABS, input_dev->evbit);
Stelian Popf7214ff2005-09-08 10:19:48 +0200804
Sven Anderse9542df2008-05-05 23:57:10 -0400805 if (dev->type == ATP_GEYSER3 || dev->type == ATP_GEYSER4) {
Nicolas Boichat9effa972006-04-19 23:36:40 +0200806 /*
807 * MacBook have 20 X sensors, 10 Y sensors
808 */
809 input_set_abs_params(input_dev, ABS_X, 0,
810 ((20 - 1) * ATP_XFACT) - 1, ATP_FUZZ, 0);
811 input_set_abs_params(input_dev, ABS_Y, 0,
812 ((10 - 1) * ATP_YFACT) - 1, ATP_FUZZ, 0);
Sven Anderse9542df2008-05-05 23:57:10 -0400813 } else if (dev->type == ATP_GEYSER2) {
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500814 /*
815 * Oct 2005 15" PowerBooks have 15 X sensors, 17" are detected
816 * later.
817 */
818 input_set_abs_params(input_dev, ABS_X, 0,
819 ((15 - 1) * ATP_XFACT) - 1, ATP_FUZZ, 0);
820 input_set_abs_params(input_dev, ABS_Y, 0,
821 ((9 - 1) * ATP_YFACT) - 1, ATP_FUZZ, 0);
822 } else {
823 /*
824 * 12" and 15" Powerbooks only have 16 x sensors,
825 * 17" models are detected later.
826 */
827 input_set_abs_params(input_dev, ABS_X, 0,
Johannes Berg7dce8692008-05-05 23:56:55 -0400828 (16 - 1) * ATP_XFACT - 1,
829 ATP_FUZZ, 0);
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500830 input_set_abs_params(input_dev, ABS_Y, 0,
Johannes Berg7dce8692008-05-05 23:56:55 -0400831 (ATP_YSENSORS - 1) * ATP_YFACT - 1,
832 ATP_FUZZ, 0);
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500833 }
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500834 input_set_abs_params(input_dev, ABS_PRESSURE, 0, ATP_PRESSURE, 0, 0);
Stelian Popf7214ff2005-09-08 10:19:48 +0200835
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500836 set_bit(EV_KEY, input_dev->evbit);
837 set_bit(BTN_TOUCH, input_dev->keybit);
838 set_bit(BTN_TOOL_FINGER, input_dev->keybit);
839 set_bit(BTN_TOOL_DOUBLETAP, input_dev->keybit);
840 set_bit(BTN_TOOL_TRIPLETAP, input_dev->keybit);
841 set_bit(BTN_LEFT, input_dev->keybit);
Stelian Popf7214ff2005-09-08 10:19:48 +0200842
Dmitry Torokhov50141862007-04-12 01:33:39 -0400843 error = input_register_device(dev->input);
844 if (error)
845 goto err_free_buffer;
Stelian Popf7214ff2005-09-08 10:19:48 +0200846
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500847 /* save our data pointer in this interface device */
848 usb_set_intfdata(iface, dev);
Stelian Popf7214ff2005-09-08 10:19:48 +0200849
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400850 INIT_WORK(&dev->work, atp_reinit);
851
Stelian Popf7214ff2005-09-08 10:19:48 +0200852 return 0;
853
Dmitry Torokhov50141862007-04-12 01:33:39 -0400854 err_free_buffer:
855 usb_buffer_free(dev->udev, dev->datalen,
856 dev->data, dev->urb->transfer_dma);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500857 err_free_urb:
Stelian Popf7214ff2005-09-08 10:19:48 +0200858 usb_free_urb(dev->urb);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500859 err_free_devs:
Stelian Popf7214ff2005-09-08 10:19:48 +0200860 usb_set_intfdata(iface, NULL);
Stelian Popf7214ff2005-09-08 10:19:48 +0200861 kfree(dev);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500862 input_free_device(input_dev);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400863 return error;
Stelian Popf7214ff2005-09-08 10:19:48 +0200864}
865
866static void atp_disconnect(struct usb_interface *iface)
867{
868 struct atp *dev = usb_get_intfdata(iface);
869
870 usb_set_intfdata(iface, NULL);
871 if (dev) {
872 usb_kill_urb(dev->urb);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500873 input_unregister_device(dev->input);
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500874 usb_buffer_free(dev->udev, dev->datalen,
Stelian Popf7214ff2005-09-08 10:19:48 +0200875 dev->data, dev->urb->transfer_dma);
Johannes Berg7af569a2006-07-19 15:39:46 +0200876 usb_free_urb(dev->urb);
Stelian Popf7214ff2005-09-08 10:19:48 +0200877 kfree(dev);
878 }
879 printk(KERN_INFO "input: appletouch disconnected\n");
880}
881
Oliver Neukum90d95ef2008-06-17 11:56:55 -0400882static int atp_recover(struct atp *dev)
883{
884 int error;
885
886 error = atp_handle_geyser(dev);
887 if (error)
888 return error;
889
890 if (dev->open && usb_submit_urb(dev->urb, GFP_ATOMIC))
891 return -EIO;
892
893 return 0;
894}
895
Stelian Popf7214ff2005-09-08 10:19:48 +0200896static int atp_suspend(struct usb_interface *iface, pm_message_t message)
897{
898 struct atp *dev = usb_get_intfdata(iface);
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400899
Stelian Popf7214ff2005-09-08 10:19:48 +0200900 usb_kill_urb(dev->urb);
Stelian Popf7214ff2005-09-08 10:19:48 +0200901 return 0;
902}
903
904static int atp_resume(struct usb_interface *iface)
905{
906 struct atp *dev = usb_get_intfdata(iface);
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400907
Stelian Popf7214ff2005-09-08 10:19:48 +0200908 if (dev->open && usb_submit_urb(dev->urb, GFP_ATOMIC))
909 return -EIO;
910
911 return 0;
912}
913
Oliver Neukum90d95ef2008-06-17 11:56:55 -0400914static int atp_reset_resume(struct usb_interface *iface)
915{
916 struct atp *dev = usb_get_intfdata(iface);
917
918 return atp_recover(dev);
919}
920
Stelian Popf7214ff2005-09-08 10:19:48 +0200921static struct usb_driver atp_driver = {
Stelian Popf7214ff2005-09-08 10:19:48 +0200922 .name = "appletouch",
923 .probe = atp_probe,
924 .disconnect = atp_disconnect,
925 .suspend = atp_suspend,
926 .resume = atp_resume,
Oliver Neukum90d95ef2008-06-17 11:56:55 -0400927 .reset_resume = atp_reset_resume,
Stelian Popf7214ff2005-09-08 10:19:48 +0200928 .id_table = atp_table,
929};
930
931static int __init atp_init(void)
932{
933 return usb_register(&atp_driver);
934}
935
936static void __exit atp_exit(void)
937{
938 usb_deregister(&atp_driver);
939}
940
941module_init(atp_init);
942module_exit(atp_exit);