blob: a5f15477a7eaa46dfe9fc272df641edea01f920e [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 Pop09779672008-10-28 23:20:46 -04006 * Copyright (C) 2005-2008 Stelian Pop (stelian@popies.net)
Stelian Popf7214ff2005-09-08 10:19:48 +02007 * 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
Stelian Pop09779672008-10-28 23:20:46 -040038/*
39 * Note: We try to keep the touchpad aspect ratio while still doing only
40 * simple arithmetics:
41 * 0 <= x <= (xsensors - 1) * xfact
42 * 0 <= y <= (ysensors - 1) * yfact
43 */
44struct atp_info {
45 int xsensors; /* number of X sensors */
46 int xsensors_17; /* 17" models have more sensors */
47 int ysensors; /* number of Y sensors */
48 int xfact; /* X multiplication factor */
49 int yfact; /* Y multiplication factor */
50 int datalen; /* size of USB transfers */
51 void (*callback)(struct urb *); /* callback function */
Sven Anderse9542df2008-05-05 23:57:10 -040052};
Stelian Popf7214ff2005-09-08 10:19:48 +020053
Stelian Pop09779672008-10-28 23:20:46 -040054static void atp_complete_geyser_1_2(struct urb *urb);
55static void atp_complete_geyser_3_4(struct urb *urb);
56
57static const struct atp_info fountain_info = {
58 .xsensors = 16,
59 .xsensors_17 = 26,
60 .ysensors = 16,
61 .xfact = 64,
62 .yfact = 43,
63 .datalen = 81,
64 .callback = atp_complete_geyser_1_2,
65};
66
67static const struct atp_info geyser1_info = {
68 .xsensors = 16,
69 .xsensors_17 = 26,
70 .ysensors = 16,
71 .xfact = 64,
72 .yfact = 43,
73 .datalen = 81,
74 .callback = atp_complete_geyser_1_2,
75};
76
77static const struct atp_info geyser2_info = {
78 .xsensors = 15,
79 .xsensors_17 = 20,
80 .ysensors = 9,
81 .xfact = 64,
82 .yfact = 43,
83 .datalen = 64,
84 .callback = atp_complete_geyser_1_2,
85};
86
87static const struct atp_info geyser3_info = {
88 .xsensors = 20,
89 .ysensors = 10,
90 .xfact = 64,
91 .yfact = 64,
92 .datalen = 64,
93 .callback = atp_complete_geyser_3_4,
94};
95
96static const struct atp_info geyser4_info = {
97 .xsensors = 20,
98 .ysensors = 10,
99 .xfact = 64,
100 .yfact = 64,
101 .datalen = 64,
102 .callback = atp_complete_geyser_3_4,
103};
104
105#define ATP_DEVICE(prod, info) \
Sven Anderse9542df2008-05-05 23:57:10 -0400106{ \
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500107 .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \
Stelian Popf7214ff2005-09-08 10:19:48 +0200108 USB_DEVICE_ID_MATCH_INT_CLASS | \
109 USB_DEVICE_ID_MATCH_INT_PROTOCOL, \
Sven Anderse9542df2008-05-05 23:57:10 -0400110 .idVendor = 0x05ac, /* Apple */ \
Stelian Popf7214ff2005-09-08 10:19:48 +0200111 .idProduct = (prod), \
112 .bInterfaceClass = 0x03, \
Sven Anderse9542df2008-05-05 23:57:10 -0400113 .bInterfaceProtocol = 0x02, \
Stelian Pop09779672008-10-28 23:20:46 -0400114 .driver_info = (unsigned long) &info, \
Sven Anderse9542df2008-05-05 23:57:10 -0400115}
Stelian Popf7214ff2005-09-08 10:19:48 +0200116
Sven Anderse9542df2008-05-05 23:57:10 -0400117/*
118 * Table of devices (Product IDs) that work with this driver.
119 * (The names come from Info.plist in AppleUSBTrackpad.kext,
120 * According to Info.plist Geyser IV is the same as Geyser III.)
121 */
122
Stelian Pop09779672008-10-28 23:20:46 -0400123static struct usb_device_id atp_table[] = {
Johannes Berg7dce8692008-05-05 23:56:55 -0400124 /* PowerBooks Feb 2005, iBooks G4 */
Stelian Pop09779672008-10-28 23:20:46 -0400125 ATP_DEVICE(0x020e, fountain_info), /* FOUNTAIN ANSI */
126 ATP_DEVICE(0x020f, fountain_info), /* FOUNTAIN ISO */
127 ATP_DEVICE(0x030a, fountain_info), /* FOUNTAIN TP ONLY */
128 ATP_DEVICE(0x030b, geyser1_info), /* GEYSER 1 TP ONLY */
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500129
130 /* PowerBooks Oct 2005 */
Stelian Pop09779672008-10-28 23:20:46 -0400131 ATP_DEVICE(0x0214, geyser2_info), /* GEYSER 2 ANSI */
132 ATP_DEVICE(0x0215, geyser2_info), /* GEYSER 2 ISO */
133 ATP_DEVICE(0x0216, geyser2_info), /* GEYSER 2 JIS */
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500134
Julien BLACHE009ad0e2006-11-17 01:06:13 -0500135 /* Core Duo MacBook & MacBook Pro */
Stelian Pop09779672008-10-28 23:20:46 -0400136 ATP_DEVICE(0x0217, geyser3_info), /* GEYSER 3 ANSI */
137 ATP_DEVICE(0x0218, geyser3_info), /* GEYSER 3 ISO */
138 ATP_DEVICE(0x0219, geyser3_info), /* GEYSER 3 JIS */
Nicolas Boichat9effa972006-04-19 23:36:40 +0200139
Julien BLACHE009ad0e2006-11-17 01:06:13 -0500140 /* Core2 Duo MacBook & MacBook Pro */
Stelian Pop09779672008-10-28 23:20:46 -0400141 ATP_DEVICE(0x021a, geyser4_info), /* GEYSER 4 ANSI */
142 ATP_DEVICE(0x021b, geyser4_info), /* GEYSER 4 ISO */
143 ATP_DEVICE(0x021c, geyser4_info), /* GEYSER 4 JIS */
Julien BLACHE009ad0e2006-11-17 01:06:13 -0500144
Johannes Berg7dce8692008-05-05 23:56:55 -0400145 /* Core2 Duo MacBook3,1 */
Stelian Pop09779672008-10-28 23:20:46 -0400146 ATP_DEVICE(0x0229, geyser4_info), /* GEYSER 4 HF ANSI */
147 ATP_DEVICE(0x022a, geyser4_info), /* GEYSER 4 HF ISO */
148 ATP_DEVICE(0x022b, geyser4_info), /* GEYSER 4 HF JIS */
Tobias Mueller0035a1d2008-04-02 10:02:06 -0400149
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500150 /* Terminating entry */
151 { }
Stelian Popf7214ff2005-09-08 10:19:48 +0200152};
Johannes Berg7dce8692008-05-05 23:56:55 -0400153MODULE_DEVICE_TABLE(usb, atp_table);
Stelian Popf7214ff2005-09-08 10:19:48 +0200154
Stelian Pop09779672008-10-28 23:20:46 -0400155/* maximum number of sensors */
Stelian Popf7214ff2005-09-08 10:19:48 +0200156#define ATP_XSENSORS 26
157#define ATP_YSENSORS 16
158
159/* amount of fuzz this touchpad generates */
160#define ATP_FUZZ 16
161
162/* maximum pressure this driver will report */
163#define ATP_PRESSURE 300
Stelian Popf7214ff2005-09-08 10:19:48 +0200164
165/*
166 * Threshold for the touchpad sensors. Any change less than ATP_THRESHOLD is
167 * ignored.
168 */
169#define ATP_THRESHOLD 5
170
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400171/* Geyser initialization constants */
172#define ATP_GEYSER_MODE_READ_REQUEST_ID 1
173#define ATP_GEYSER_MODE_WRITE_REQUEST_ID 9
174#define ATP_GEYSER_MODE_REQUEST_VALUE 0x300
175#define ATP_GEYSER_MODE_REQUEST_INDEX 0
176#define ATP_GEYSER_MODE_VENDOR_VALUE 0x04
Nicolas Boichat9effa972006-04-19 23:36:40 +0200177
Sven Anders82a196f2008-08-08 16:31:33 -0400178/**
179 * enum atp_status_bits - status bit meanings
180 *
181 * These constants represent the meaning of the status bits.
182 * (only Geyser 3/4)
183 *
184 * @ATP_STATUS_BUTTON: The button was pressed
185 * @ATP_STATUS_BASE_UPDATE: Update of the base values (untouched pad)
186 * @ATP_STATUS_FROM_RESET: Reset previously performed
187 */
188enum atp_status_bits {
189 ATP_STATUS_BUTTON = BIT(0),
190 ATP_STATUS_BASE_UPDATE = BIT(2),
191 ATP_STATUS_FROM_RESET = BIT(4),
192};
193
Stelian Popf7214ff2005-09-08 10:19:48 +0200194/* Structure to hold all of our device specific stuff */
195struct atp {
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500196 char phys[64];
Johannes Berg7dce8692008-05-05 23:56:55 -0400197 struct usb_device *udev; /* usb device */
198 struct urb *urb; /* usb request block */
Sven Anders82a196f2008-08-08 16:31:33 -0400199 u8 *data; /* transferred data */
Johannes Berg7dce8692008-05-05 23:56:55 -0400200 struct input_dev *input; /* input dev */
Stelian Pop09779672008-10-28 23:20:46 -0400201 const struct atp_info *info; /* touchpad model */
Johannes Berg7dce8692008-05-05 23:56:55 -0400202 bool open;
203 bool valid; /* are the samples valid? */
204 bool size_detect_done;
205 bool overflow_warned;
Stelian Popf7214ff2005-09-08 10:19:48 +0200206 int x_old; /* last reported x/y, */
207 int y_old; /* used for smoothing */
Benjamin Herrenschmidt6e49c1a2010-08-09 13:48:08 +1000208 signed char xy_cur[ATP_XSENSORS + ATP_YSENSORS];
209 signed char xy_old[ATP_XSENSORS + ATP_YSENSORS];
Stelian Popf7214ff2005-09-08 10:19:48 +0200210 int xy_acc[ATP_XSENSORS + ATP_YSENSORS];
Johannes Berg7dce8692008-05-05 23:56:55 -0400211 int idlecount; /* number of empty packets */
212 struct work_struct work;
Stelian Popf7214ff2005-09-08 10:19:48 +0200213};
214
215#define dbg_dump(msg, tab) \
216 if (debug > 1) { \
Johannes Berg7dce8692008-05-05 23:56:55 -0400217 int __i; \
218 printk(KERN_DEBUG "appletouch: %s", msg); \
219 for (__i = 0; __i < ATP_XSENSORS + ATP_YSENSORS; __i++) \
220 printk(" %02x", tab[__i]); \
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500221 printk("\n"); \
Stelian Popf7214ff2005-09-08 10:19:48 +0200222 }
223
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500224#define dprintk(format, a...) \
Stelian Popf7214ff2005-09-08 10:19:48 +0200225 do { \
Johannes Berg7dce8692008-05-05 23:56:55 -0400226 if (debug) \
227 printk(KERN_DEBUG format, ##a); \
Stelian Popf7214ff2005-09-08 10:19:48 +0200228 } while (0)
229
Johannes Berg7dce8692008-05-05 23:56:55 -0400230MODULE_AUTHOR("Johannes Berg");
231MODULE_AUTHOR("Stelian Pop");
232MODULE_AUTHOR("Frank Arnold");
233MODULE_AUTHOR("Michael Hanselmann");
234MODULE_AUTHOR("Sven Anders");
235MODULE_DESCRIPTION("Apple PowerBook and MacBook USB touchpad driver");
Stelian Popf7214ff2005-09-08 10:19:48 +0200236MODULE_LICENSE("GPL");
237
Jason Parekh00081d32006-11-17 01:05:58 -0500238/*
239 * Make the threshold a module parameter
240 */
241static int threshold = ATP_THRESHOLD;
242module_param(threshold, int, 0644);
Johannes Berg7dce8692008-05-05 23:56:55 -0400243MODULE_PARM_DESC(threshold, "Discard any change in data from a sensor"
244 " (the trackpad has many of these sensors)"
245 " less than this value.");
Jason Parekh00081d32006-11-17 01:05:58 -0500246
Johannes Berg7dce8692008-05-05 23:56:55 -0400247static int debug;
Stelian Popf7214ff2005-09-08 10:19:48 +0200248module_param(debug, int, 0644);
249MODULE_PARM_DESC(debug, "Activate debugging output");
250
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400251/*
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400252 * By default newer Geyser devices send standard USB HID mouse
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400253 * packets (Report ID 2). This code changes device mode, so it
254 * sends raw sensor reports (Report ID 5).
255 */
Greg Kroah-Hartman80f85942012-05-01 20:56:47 -0400256static int atp_geyser_init(struct atp *dev)
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400257{
Greg Kroah-Hartman80f85942012-05-01 20:56:47 -0400258 struct usb_device *udev = dev->udev;
Bob Copeland0385c5e2009-04-28 07:49:53 -0700259 char *data;
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400260 int size;
Johannes Berg7dce8692008-05-05 23:56:55 -0400261 int i;
Bob Copeland0385c5e2009-04-28 07:49:53 -0700262 int ret;
263
264 data = kmalloc(8, GFP_KERNEL);
265 if (!data) {
Greg Kroah-Hartman80f85942012-05-01 20:56:47 -0400266 dev_err(&dev->input->dev, "Out of memory\n");
Bob Copeland0385c5e2009-04-28 07:49:53 -0700267 return -ENOMEM;
268 }
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400269
270 size = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400271 ATP_GEYSER_MODE_READ_REQUEST_ID,
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400272 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400273 ATP_GEYSER_MODE_REQUEST_VALUE,
Bob Copeland0385c5e2009-04-28 07:49:53 -0700274 ATP_GEYSER_MODE_REQUEST_INDEX, data, 8, 5000);
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400275
276 if (size != 8) {
Johannes Berg7dce8692008-05-05 23:56:55 -0400277 dprintk("atp_geyser_init: read error\n");
278 for (i = 0; i < 8; i++)
279 dprintk("appletouch[%d]: %d\n", i, data[i]);
280
Greg Kroah-Hartman80f85942012-05-01 20:56:47 -0400281 dev_err(&dev->input->dev, "Failed to read mode from device.\n");
Bob Copeland0385c5e2009-04-28 07:49:53 -0700282 ret = -EIO;
283 goto out_free;
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400284 }
285
286 /* Apply the mode switch */
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400287 data[0] = ATP_GEYSER_MODE_VENDOR_VALUE;
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400288
289 size = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400290 ATP_GEYSER_MODE_WRITE_REQUEST_ID,
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400291 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400292 ATP_GEYSER_MODE_REQUEST_VALUE,
Bob Copeland0385c5e2009-04-28 07:49:53 -0700293 ATP_GEYSER_MODE_REQUEST_INDEX, data, 8, 5000);
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400294
295 if (size != 8) {
Johannes Berg7dce8692008-05-05 23:56:55 -0400296 dprintk("atp_geyser_init: write error\n");
297 for (i = 0; i < 8; i++)
298 dprintk("appletouch[%d]: %d\n", i, data[i]);
299
Greg Kroah-Hartman80f85942012-05-01 20:56:47 -0400300 dev_err(&dev->input->dev, "Failed to request geyser raw mode\n");
Bob Copeland0385c5e2009-04-28 07:49:53 -0700301 ret = -EIO;
302 goto out_free;
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400303 }
Bob Copeland0385c5e2009-04-28 07:49:53 -0700304 ret = 0;
305out_free:
306 kfree(data);
307 return ret;
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400308}
309
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400310/*
311 * Reinitialise the device. This usually stops stream of empty packets
312 * coming from it.
313 */
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400314static void atp_reinit(struct work_struct *work)
315{
316 struct atp *dev = container_of(work, struct atp, work);
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400317 int retval;
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400318
Johannes Berg7dce8692008-05-05 23:56:55 -0400319 dprintk("appletouch: putting appletouch to sleep (reinit)\n");
Greg Kroah-Hartman80f85942012-05-01 20:56:47 -0400320 atp_geyser_init(dev);
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400321
322 retval = usb_submit_urb(dev->urb, GFP_ATOMIC);
Johannes Berg7dce8692008-05-05 23:56:55 -0400323 if (retval)
Greg Kroah-Hartman80f85942012-05-01 20:56:47 -0400324 dev_err(&dev->input->dev,
Greg Kroah-Hartman9c113dc2012-04-25 14:48:31 -0700325 "atp_reinit: usb_submit_urb failed with error %d\n",
326 retval);
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400327}
328
Stelian Popf7214ff2005-09-08 10:19:48 +0200329static int atp_calculate_abs(int *xy_sensors, int nb_sensors, int fact,
330 int *z, int *fingers)
331{
332 int i;
333 /* values to calculate mean */
334 int pcum = 0, psum = 0;
Jason Parekh00081d32006-11-17 01:05:58 -0500335 int is_increasing = 0;
Stelian Popf7214ff2005-09-08 10:19:48 +0200336
337 *fingers = 0;
338
339 for (i = 0; i < nb_sensors; i++) {
Jason Parekh00081d32006-11-17 01:05:58 -0500340 if (xy_sensors[i] < threshold) {
341 if (is_increasing)
342 is_increasing = 0;
343
Stelian Popf7214ff2005-09-08 10:19:48 +0200344 continue;
Jason Parekh00081d32006-11-17 01:05:58 -0500345 }
346
347 /*
348 * Makes the finger detection more versatile. For example,
349 * two fingers with no gap will be detected. Also, my
350 * tests show it less likely to have intermittent loss
351 * of multiple finger readings while moving around (scrolling).
352 *
353 * Changes the multiple finger detection to counting humps on
354 * sensors (transitions from nonincreasing to increasing)
355 * instead of counting transitions from low sensors (no
356 * finger reading) to high sensors (finger above
357 * sensor)
358 *
359 * - Jason Parekh <jasonparekh@gmail.com>
360 */
Johannes Berg7dce8692008-05-05 23:56:55 -0400361 if (i < 1 ||
362 (!is_increasing && xy_sensors[i - 1] < xy_sensors[i])) {
Stelian Popf7214ff2005-09-08 10:19:48 +0200363 (*fingers)++;
Jason Parekh00081d32006-11-17 01:05:58 -0500364 is_increasing = 1;
Jeremy Huddleston05e882f2009-06-03 07:29:39 -0700365 } else if (i > 0 && (xy_sensors[i - 1] - xy_sensors[i] > threshold)) {
Jason Parekh00081d32006-11-17 01:05:58 -0500366 is_increasing = 0;
367 }
368
369 /*
Johannes Berg7dce8692008-05-05 23:56:55 -0400370 * Subtracts threshold so a high sensor that just passes the
371 * threshold won't skew the calculated absolute coordinate.
372 * Fixes an issue where slowly moving the mouse would
373 * occasionally jump a number of pixels (slowly moving the
374 * finger makes this issue most apparent.)
Jason Parekh00081d32006-11-17 01:05:58 -0500375 */
376 pcum += (xy_sensors[i] - threshold) * i;
377 psum += (xy_sensors[i] - threshold);
Stelian Popf7214ff2005-09-08 10:19:48 +0200378 }
379
380 if (psum > 0) {
381 *z = psum;
382 return pcum * fact / psum;
383 }
384
385 return 0;
386}
387
388static inline void atp_report_fingers(struct input_dev *input, int fingers)
389{
390 input_report_key(input, BTN_TOOL_FINGER, fingers == 1);
391 input_report_key(input, BTN_TOOL_DOUBLETAP, fingers == 2);
392 input_report_key(input, BTN_TOOL_TRIPLETAP, fingers > 2);
393}
394
Sven Andersd83d2132008-08-08 16:31:31 -0400395/* Check URB status and for correct length of data package */
396
397#define ATP_URB_STATUS_SUCCESS 0
398#define ATP_URB_STATUS_ERROR 1
399#define ATP_URB_STATUS_ERROR_FATAL 2
400
401static int atp_status_check(struct urb *urb)
Stelian Popf7214ff2005-09-08 10:19:48 +0200402{
Stelian Popf7214ff2005-09-08 10:19:48 +0200403 struct atp *dev = urb->context;
404
405 switch (urb->status) {
406 case 0:
407 /* success */
408 break;
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500409 case -EOVERFLOW:
Johannes Berg7dce8692008-05-05 23:56:55 -0400410 if (!dev->overflow_warned) {
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400411 printk(KERN_WARNING "appletouch: OVERFLOW with data "
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500412 "length %d, actual length is %d\n",
Stelian Pop09779672008-10-28 23:20:46 -0400413 dev->info->datalen, dev->urb->actual_length);
Johannes Berg7dce8692008-05-05 23:56:55 -0400414 dev->overflow_warned = true;
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500415 }
Stelian Popf7214ff2005-09-08 10:19:48 +0200416 case -ECONNRESET:
417 case -ENOENT:
418 case -ESHUTDOWN:
419 /* This urb is terminated, clean up */
Johannes Berg7dce8692008-05-05 23:56:55 -0400420 dbg("atp_complete: urb shutting down with status: %d",
421 urb->status);
Sven Andersd83d2132008-08-08 16:31:31 -0400422 return ATP_URB_STATUS_ERROR_FATAL;
423
Stelian Popf7214ff2005-09-08 10:19:48 +0200424 default:
Johannes Berg7dce8692008-05-05 23:56:55 -0400425 dbg("atp_complete: nonzero urb status received: %d",
426 urb->status);
Sven Andersd83d2132008-08-08 16:31:31 -0400427 return ATP_URB_STATUS_ERROR;
Stelian Popf7214ff2005-09-08 10:19:48 +0200428 }
429
430 /* drop incomplete datasets */
Stelian Pop09779672008-10-28 23:20:46 -0400431 if (dev->urb->actual_length != dev->info->datalen) {
Nicolas Boichat9effa972006-04-19 23:36:40 +0200432 dprintk("appletouch: incomplete data package"
433 " (first byte: %d, length: %d).\n",
434 dev->data[0], dev->urb->actual_length);
Sven Andersd83d2132008-08-08 16:31:31 -0400435 return ATP_URB_STATUS_ERROR;
Stelian Popf7214ff2005-09-08 10:19:48 +0200436 }
437
Sven Andersd83d2132008-08-08 16:31:31 -0400438 return ATP_URB_STATUS_SUCCESS;
439}
440
Stelian Pop09779672008-10-28 23:20:46 -0400441static void atp_detect_size(struct atp *dev)
442{
443 int i;
444
445 /* 17" Powerbooks have extra X sensors */
446 for (i = dev->info->xsensors; i < ATP_XSENSORS; i++) {
447 if (dev->xy_cur[i]) {
448
449 printk(KERN_INFO "appletouch: 17\" model detected.\n");
450
451 input_set_abs_params(dev->input, ABS_X, 0,
452 (dev->info->xsensors_17 - 1) *
453 dev->info->xfact - 1,
454 ATP_FUZZ, 0);
455 break;
456 }
457 }
458}
459
Sven Andersd83d2132008-08-08 16:31:31 -0400460/*
461 * USB interrupt callback functions
462 */
463
464/* Interrupt function for older touchpads: FOUNTAIN/GEYSER1/GEYSER2 */
465
466static void atp_complete_geyser_1_2(struct urb *urb)
467{
468 int x, y, x_z, y_z, x_f, y_f;
469 int retval, i, j;
470 int key;
471 struct atp *dev = urb->context;
472 int status = atp_status_check(urb);
473
474 if (status == ATP_URB_STATUS_ERROR_FATAL)
475 return;
476 else if (status == ATP_URB_STATUS_ERROR)
477 goto exit;
478
Stelian Popf7214ff2005-09-08 10:19:48 +0200479 /* reorder the sensors values */
Stelian Pop09779672008-10-28 23:20:46 -0400480 if (dev->info == &geyser2_info) {
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500481 memset(dev->xy_cur, 0, sizeof(dev->xy_cur));
Stelian Popf7214ff2005-09-08 10:19:48 +0200482
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500483 /*
484 * The values are laid out like this:
485 * Y1, Y2, -, Y3, Y4, -, ..., X1, X2, -, X3, X4, -, ...
486 * '-' is an unused value.
487 */
488
489 /* read X values */
490 for (i = 0, j = 19; i < 20; i += 2, j += 3) {
491 dev->xy_cur[i] = dev->data[j];
492 dev->xy_cur[i + 1] = dev->data[j + 1];
493 }
494
495 /* read Y values */
496 for (i = 0, j = 1; i < 9; i += 2, j += 3) {
497 dev->xy_cur[ATP_XSENSORS + i] = dev->data[j];
498 dev->xy_cur[ATP_XSENSORS + i + 1] = dev->data[j + 1];
499 }
500 } else {
501 for (i = 0; i < 8; i++) {
502 /* X values */
Johannes Berg7dce8692008-05-05 23:56:55 -0400503 dev->xy_cur[i + 0] = dev->data[5 * i + 2];
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500504 dev->xy_cur[i + 8] = dev->data[5 * i + 4];
505 dev->xy_cur[i + 16] = dev->data[5 * i + 42];
506 if (i < 2)
507 dev->xy_cur[i + 24] = dev->data[5 * i + 44];
508
509 /* Y values */
Stelian Pop09779672008-10-28 23:20:46 -0400510 dev->xy_cur[ATP_XSENSORS + i] = dev->data[5 * i + 1];
511 dev->xy_cur[ATP_XSENSORS + i + 8] = dev->data[5 * i + 3];
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500512 }
Stelian Popf7214ff2005-09-08 10:19:48 +0200513 }
514
515 dbg_dump("sample", dev->xy_cur);
516
517 if (!dev->valid) {
518 /* first sample */
Johannes Berg7dce8692008-05-05 23:56:55 -0400519 dev->valid = true;
Stelian Popf7214ff2005-09-08 10:19:48 +0200520 dev->x_old = dev->y_old = -1;
Sven Andersd83d2132008-08-08 16:31:31 -0400521
522 /* Store first sample */
Stelian Popf7214ff2005-09-08 10:19:48 +0200523 memcpy(dev->xy_old, dev->xy_cur, sizeof(dev->xy_old));
524
Sven Andersd83d2132008-08-08 16:31:31 -0400525 /* Perform size detection, if not done already */
Stelian Pop09779672008-10-28 23:20:46 -0400526 if (unlikely(!dev->size_detect_done)) {
527 atp_detect_size(dev);
Sven Andersd83d2132008-08-08 16:31:31 -0400528 dev->size_detect_done = 1;
Nicolas Boichat9effa972006-04-19 23:36:40 +0200529 goto exit;
Michael Hanselmanne1e02c92005-12-21 00:50:23 -0500530 }
Sven Andersd83d2132008-08-08 16:31:31 -0400531 }
Stelian Popf7214ff2005-09-08 10:19:48 +0200532
Sven Andersd83d2132008-08-08 16:31:31 -0400533 for (i = 0; i < ATP_XSENSORS + ATP_YSENSORS; i++) {
534 /* accumulate the change */
Benjamin Herrenschmidt6e49c1a2010-08-09 13:48:08 +1000535 signed char change = dev->xy_old[i] - dev->xy_cur[i];
Sven Andersd83d2132008-08-08 16:31:31 -0400536 dev->xy_acc[i] -= change;
537
538 /* prevent down drifting */
539 if (dev->xy_acc[i] < 0)
540 dev->xy_acc[i] = 0;
541 }
542
543 memcpy(dev->xy_old, dev->xy_cur, sizeof(dev->xy_old));
544
545 dbg_dump("accumulator", dev->xy_acc);
546
547 x = atp_calculate_abs(dev->xy_acc, ATP_XSENSORS,
Stelian Pop09779672008-10-28 23:20:46 -0400548 dev->info->xfact, &x_z, &x_f);
Sven Andersd83d2132008-08-08 16:31:31 -0400549 y = atp_calculate_abs(dev->xy_acc + ATP_XSENSORS, ATP_YSENSORS,
Stelian Pop09779672008-10-28 23:20:46 -0400550 dev->info->yfact, &y_z, &y_f);
551 key = dev->data[dev->info->datalen - 1] & ATP_STATUS_BUTTON;
Sven Andersd83d2132008-08-08 16:31:31 -0400552
553 if (x && y) {
554 if (dev->x_old != -1) {
555 x = (dev->x_old * 3 + x) >> 2;
556 y = (dev->y_old * 3 + y) >> 2;
557 dev->x_old = x;
558 dev->y_old = y;
559
560 if (debug > 1)
561 printk(KERN_DEBUG "appletouch: "
562 "X: %3d Y: %3d Xz: %3d Yz: %3d\n",
563 x, y, x_z, y_z);
564
565 input_report_key(dev->input, BTN_TOUCH, 1);
566 input_report_abs(dev->input, ABS_X, x);
567 input_report_abs(dev->input, ABS_Y, y);
568 input_report_abs(dev->input, ABS_PRESSURE,
569 min(ATP_PRESSURE, x_z + y_z));
570 atp_report_fingers(dev->input, max(x_f, y_f));
571 }
572 dev->x_old = x;
573 dev->y_old = y;
574
575 } else if (!x && !y) {
576
577 dev->x_old = dev->y_old = -1;
578 input_report_key(dev->input, BTN_TOUCH, 0);
579 input_report_abs(dev->input, ABS_PRESSURE, 0);
580 atp_report_fingers(dev->input, 0);
581
582 /* reset the accumulator on release */
583 memset(dev->xy_acc, 0, sizeof(dev->xy_acc));
584 }
585
586 input_report_key(dev->input, BTN_LEFT, key);
587 input_sync(dev->input);
588
589 exit:
590 retval = usb_submit_urb(dev->urb, GFP_ATOMIC);
591 if (retval)
Greg Kroah-Hartman80f85942012-05-01 20:56:47 -0400592 dev_err(&dev->input->dev,
Greg Kroah-Hartman9c113dc2012-04-25 14:48:31 -0700593 "atp_complete: usb_submit_urb failed with result %d\n",
594 retval);
Sven Andersd83d2132008-08-08 16:31:31 -0400595}
596
597/* Interrupt function for older touchpads: GEYSER3/GEYSER4 */
598
599static void atp_complete_geyser_3_4(struct urb *urb)
600{
601 int x, y, x_z, y_z, x_f, y_f;
602 int retval, i, j;
603 int key;
604 struct atp *dev = urb->context;
605 int status = atp_status_check(urb);
606
607 if (status == ATP_URB_STATUS_ERROR_FATAL)
608 return;
609 else if (status == ATP_URB_STATUS_ERROR)
610 goto exit;
611
612 /* Reorder the sensors values:
613 *
614 * The values are laid out like this:
615 * -, Y1, Y2, -, Y3, Y4, -, ..., -, X1, X2, -, X3, X4, ...
616 * '-' is an unused value.
617 */
618
619 /* read X values */
620 for (i = 0, j = 19; i < 20; i += 2, j += 3) {
621 dev->xy_cur[i] = dev->data[j + 1];
622 dev->xy_cur[i + 1] = dev->data[j + 2];
623 }
624 /* read Y values */
625 for (i = 0, j = 1; i < 9; i += 2, j += 3) {
626 dev->xy_cur[ATP_XSENSORS + i] = dev->data[j + 1];
627 dev->xy_cur[ATP_XSENSORS + i + 1] = dev->data[j + 2];
628 }
629
630 dbg_dump("sample", dev->xy_cur);
631
Sven Anders82a196f2008-08-08 16:31:33 -0400632 /* Just update the base values (i.e. touchpad in untouched state) */
Stelian Pop09779672008-10-28 23:20:46 -0400633 if (dev->data[dev->info->datalen - 1] & ATP_STATUS_BASE_UPDATE) {
Sven Andersd83d2132008-08-08 16:31:31 -0400634
Joe Perchesd745b532010-10-30 17:19:49 -0700635 dprintk("appletouch: updated base values\n");
Sven Anders82a196f2008-08-08 16:31:33 -0400636
637 memcpy(dev->xy_old, dev->xy_cur, sizeof(dev->xy_old));
Stelian Popf7214ff2005-09-08 10:19:48 +0200638 goto exit;
639 }
640
641 for (i = 0; i < ATP_XSENSORS + ATP_YSENSORS; i++) {
Sven Anders82a196f2008-08-08 16:31:33 -0400642 /* calculate the change */
643 dev->xy_acc[i] = dev->xy_cur[i] - dev->xy_old[i];
644
645 /* this is a round-robin value, so couple with that */
646 if (dev->xy_acc[i] > 127)
647 dev->xy_acc[i] -= 256;
648
649 if (dev->xy_acc[i] < -127)
650 dev->xy_acc[i] += 256;
Stelian Popf7214ff2005-09-08 10:19:48 +0200651
652 /* prevent down drifting */
653 if (dev->xy_acc[i] < 0)
654 dev->xy_acc[i] = 0;
655 }
656
Stelian Popf7214ff2005-09-08 10:19:48 +0200657 dbg_dump("accumulator", dev->xy_acc);
658
659 x = atp_calculate_abs(dev->xy_acc, ATP_XSENSORS,
Stelian Pop09779672008-10-28 23:20:46 -0400660 dev->info->xfact, &x_z, &x_f);
Stelian Popf7214ff2005-09-08 10:19:48 +0200661 y = atp_calculate_abs(dev->xy_acc + ATP_XSENSORS, ATP_YSENSORS,
Stelian Pop09779672008-10-28 23:20:46 -0400662 dev->info->yfact, &y_z, &y_f);
663 key = dev->data[dev->info->datalen - 1] & ATP_STATUS_BUTTON;
Stelian Popf7214ff2005-09-08 10:19:48 +0200664
665 if (x && y) {
666 if (dev->x_old != -1) {
667 x = (dev->x_old * 3 + x) >> 2;
668 y = (dev->y_old * 3 + y) >> 2;
669 dev->x_old = x;
670 dev->y_old = y;
671
672 if (debug > 1)
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400673 printk(KERN_DEBUG "appletouch: X: %3d Y: %3d "
Stelian Popf7214ff2005-09-08 10:19:48 +0200674 "Xz: %3d Yz: %3d\n",
675 x, y, x_z, y_z);
676
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500677 input_report_key(dev->input, BTN_TOUCH, 1);
678 input_report_abs(dev->input, ABS_X, x);
679 input_report_abs(dev->input, ABS_Y, y);
680 input_report_abs(dev->input, ABS_PRESSURE,
Stelian Popf7214ff2005-09-08 10:19:48 +0200681 min(ATP_PRESSURE, x_z + y_z));
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500682 atp_report_fingers(dev->input, max(x_f, y_f));
Stelian Popf7214ff2005-09-08 10:19:48 +0200683 }
684 dev->x_old = x;
685 dev->y_old = y;
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400686
687 } else if (!x && !y) {
Stelian Popf7214ff2005-09-08 10:19:48 +0200688
689 dev->x_old = dev->y_old = -1;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500690 input_report_key(dev->input, BTN_TOUCH, 0);
691 input_report_abs(dev->input, ABS_PRESSURE, 0);
692 atp_report_fingers(dev->input, 0);
Stelian Popf7214ff2005-09-08 10:19:48 +0200693
694 /* reset the accumulator on release */
695 memset(dev->xy_acc, 0, sizeof(dev->xy_acc));
Soeren Sonnenburg937ad5c2007-10-13 00:31:15 -0400696 }
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400697
Anton Ekblad46249ea2007-10-22 00:59:59 -0400698 input_report_key(dev->input, BTN_LEFT, key);
699 input_sync(dev->input);
700
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400701 /*
Sven Andersd83d2132008-08-08 16:31:31 -0400702 * Geysers 3/4 will continue to send packets continually after
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400703 * the first touch unless reinitialised. Do so if it's been
704 * idle for a while in order to avoid waking the kernel up
Sven Andersd83d2132008-08-08 16:31:31 -0400705 * several hundred times a second.
Dmitry Torokhov2a3e4802007-11-01 22:13:32 -0400706 */
Sven Andersd83d2132008-08-08 16:31:31 -0400707
708 /*
709 * Button must not be pressed when entering suspend,
710 * otherwise we will never release the button.
711 */
712 if (!x && !y && !key) {
713 dev->idlecount++;
714 if (dev->idlecount == 10) {
Sven Anders82a196f2008-08-08 16:31:33 -0400715 dev->x_old = dev->y_old = -1;
716 dev->idlecount = 0;
Sven Andersd83d2132008-08-08 16:31:31 -0400717 schedule_work(&dev->work);
718 /* Don't resubmit urb here, wait for reinit */
719 return;
720 }
721 } else
722 dev->idlecount = 0;
Stelian Popf7214ff2005-09-08 10:19:48 +0200723
Johannes Berg7dce8692008-05-05 23:56:55 -0400724 exit:
Stelian Popf7214ff2005-09-08 10:19:48 +0200725 retval = usb_submit_urb(dev->urb, GFP_ATOMIC);
Johannes Berg7dce8692008-05-05 23:56:55 -0400726 if (retval)
Greg Kroah-Hartman80f85942012-05-01 20:56:47 -0400727 dev_err(&dev->input->dev,
Greg Kroah-Hartman9c113dc2012-04-25 14:48:31 -0700728 "atp_complete: usb_submit_urb failed with result %d\n",
729 retval);
Stelian Popf7214ff2005-09-08 10:19:48 +0200730}
731
732static int atp_open(struct input_dev *input)
733{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400734 struct atp *dev = input_get_drvdata(input);
Stelian Popf7214ff2005-09-08 10:19:48 +0200735
736 if (usb_submit_urb(dev->urb, GFP_ATOMIC))
737 return -EIO;
738
739 dev->open = 1;
740 return 0;
741}
742
743static void atp_close(struct input_dev *input)
744{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400745 struct atp *dev = input_get_drvdata(input);
Stelian Popf7214ff2005-09-08 10:19:48 +0200746
747 usb_kill_urb(dev->urb);
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400748 cancel_work_sync(&dev->work);
Stelian Popf7214ff2005-09-08 10:19:48 +0200749 dev->open = 0;
750}
751
Oliver Neukum90d95ef2008-06-17 11:56:55 -0400752static int atp_handle_geyser(struct atp *dev)
753{
Stelian Pop09779672008-10-28 23:20:46 -0400754 if (dev->info != &fountain_info) {
Oliver Neukum90d95ef2008-06-17 11:56:55 -0400755 /* switch to raw sensor mode */
Greg Kroah-Hartman80f85942012-05-01 20:56:47 -0400756 if (atp_geyser_init(dev))
Oliver Neukum90d95ef2008-06-17 11:56:55 -0400757 return -EIO;
758
Greg Kroah-Hartman80f85942012-05-01 20:56:47 -0400759 dev_info(&dev->input->dev, "Geyser mode initialized.\n");
Oliver Neukum90d95ef2008-06-17 11:56:55 -0400760 }
761
762 return 0;
763}
764
Johannes Berg7dce8692008-05-05 23:56:55 -0400765static int atp_probe(struct usb_interface *iface,
766 const struct usb_device_id *id)
Stelian Popf7214ff2005-09-08 10:19:48 +0200767{
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500768 struct atp *dev;
769 struct input_dev *input_dev;
770 struct usb_device *udev = interface_to_usbdev(iface);
Stelian Popf7214ff2005-09-08 10:19:48 +0200771 struct usb_host_interface *iface_desc;
772 struct usb_endpoint_descriptor *endpoint;
773 int int_in_endpointAddr = 0;
Dmitry Torokhov50141862007-04-12 01:33:39 -0400774 int i, error = -ENOMEM;
Stelian Pop09779672008-10-28 23:20:46 -0400775 const struct atp_info *info = (const struct atp_info *)id->driver_info;
Stelian Popf7214ff2005-09-08 10:19:48 +0200776
777 /* set up the endpoint information */
778 /* use only the first interrupt-in endpoint */
779 iface_desc = iface->cur_altsetting;
780 for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
781 endpoint = &iface_desc->endpoint[i].desc;
Luiz Fernando N. Capitulino97b107c2006-09-27 11:58:53 -0700782 if (!int_in_endpointAddr && usb_endpoint_is_int_in(endpoint)) {
Stelian Popf7214ff2005-09-08 10:19:48 +0200783 /* we found an interrupt in endpoint */
784 int_in_endpointAddr = endpoint->bEndpointAddress;
785 break;
786 }
787 }
788 if (!int_in_endpointAddr) {
Greg Kroah-Hartman9c113dc2012-04-25 14:48:31 -0700789 dev_err(&iface->dev, "Could not find int-in endpoint\n");
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500790 return -EIO;
Stelian Popf7214ff2005-09-08 10:19:48 +0200791 }
792
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500793 /* allocate memory for our device state and initialize it */
794 dev = kzalloc(sizeof(struct atp), GFP_KERNEL);
795 input_dev = input_allocate_device();
796 if (!dev || !input_dev) {
Greg Kroah-Hartman9c113dc2012-04-25 14:48:31 -0700797 dev_err(&iface->dev, "Out of memory\n");
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500798 goto err_free_devs;
799 }
800
801 dev->udev = udev;
802 dev->input = input_dev;
Stelian Pop09779672008-10-28 23:20:46 -0400803 dev->info = info;
Johannes Berg7dce8692008-05-05 23:56:55 -0400804 dev->overflow_warned = false;
Nicolas Boichat9effa972006-04-19 23:36:40 +0200805
Stelian Popf7214ff2005-09-08 10:19:48 +0200806 dev->urb = usb_alloc_urb(0, GFP_KERNEL);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400807 if (!dev->urb)
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500808 goto err_free_devs;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500809
Daniel Mack997ea582010-04-12 13:17:25 +0200810 dev->data = usb_alloc_coherent(dev->udev, dev->info->datalen, GFP_KERNEL,
811 &dev->urb->transfer_dma);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400812 if (!dev->data)
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500813 goto err_free_urb;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500814
Stelian Pop09779672008-10-28 23:20:46 -0400815 usb_fill_int_urb(dev->urb, udev,
816 usb_rcvintpipe(udev, int_in_endpointAddr),
817 dev->data, dev->info->datalen,
818 dev->info->callback, dev, 1);
Stelian Popf7214ff2005-09-08 10:19:48 +0200819
Oliver Neukum90d95ef2008-06-17 11:56:55 -0400820 error = atp_handle_geyser(dev);
821 if (error)
822 goto err_free_buffer;
823
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500824 usb_make_path(udev, dev->phys, sizeof(dev->phys));
825 strlcat(dev->phys, "/input0", sizeof(dev->phys));
Stelian Popf7214ff2005-09-08 10:19:48 +0200826
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500827 input_dev->name = "appletouch";
828 input_dev->phys = dev->phys;
829 usb_to_input_id(dev->udev, &input_dev->id);
Dmitry Torokhovc0f82d52007-04-12 01:35:03 -0400830 input_dev->dev.parent = &iface->dev;
Stelian Popf7214ff2005-09-08 10:19:48 +0200831
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400832 input_set_drvdata(input_dev, dev);
833
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500834 input_dev->open = atp_open;
835 input_dev->close = atp_close;
836
837 set_bit(EV_ABS, input_dev->evbit);
Stelian Popf7214ff2005-09-08 10:19:48 +0200838
Stelian Pop09779672008-10-28 23:20:46 -0400839 input_set_abs_params(input_dev, ABS_X, 0,
840 (dev->info->xsensors - 1) * dev->info->xfact - 1,
841 ATP_FUZZ, 0);
842 input_set_abs_params(input_dev, ABS_Y, 0,
843 (dev->info->ysensors - 1) * dev->info->yfact - 1,
844 ATP_FUZZ, 0);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500845 input_set_abs_params(input_dev, ABS_PRESSURE, 0, ATP_PRESSURE, 0, 0);
Stelian Popf7214ff2005-09-08 10:19:48 +0200846
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500847 set_bit(EV_KEY, input_dev->evbit);
848 set_bit(BTN_TOUCH, input_dev->keybit);
849 set_bit(BTN_TOOL_FINGER, input_dev->keybit);
850 set_bit(BTN_TOOL_DOUBLETAP, input_dev->keybit);
851 set_bit(BTN_TOOL_TRIPLETAP, input_dev->keybit);
852 set_bit(BTN_LEFT, input_dev->keybit);
Stelian Popf7214ff2005-09-08 10:19:48 +0200853
Dmitry Torokhov50141862007-04-12 01:33:39 -0400854 error = input_register_device(dev->input);
855 if (error)
856 goto err_free_buffer;
Stelian Popf7214ff2005-09-08 10:19:48 +0200857
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500858 /* save our data pointer in this interface device */
859 usb_set_intfdata(iface, dev);
Stelian Popf7214ff2005-09-08 10:19:48 +0200860
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400861 INIT_WORK(&dev->work, atp_reinit);
862
Stelian Popf7214ff2005-09-08 10:19:48 +0200863 return 0;
864
Dmitry Torokhov50141862007-04-12 01:33:39 -0400865 err_free_buffer:
Daniel Mack997ea582010-04-12 13:17:25 +0200866 usb_free_coherent(dev->udev, dev->info->datalen,
867 dev->data, dev->urb->transfer_dma);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500868 err_free_urb:
Stelian Popf7214ff2005-09-08 10:19:48 +0200869 usb_free_urb(dev->urb);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500870 err_free_devs:
Stelian Popf7214ff2005-09-08 10:19:48 +0200871 usb_set_intfdata(iface, NULL);
Stelian Popf7214ff2005-09-08 10:19:48 +0200872 kfree(dev);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500873 input_free_device(input_dev);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400874 return error;
Stelian Popf7214ff2005-09-08 10:19:48 +0200875}
876
877static void atp_disconnect(struct usb_interface *iface)
878{
879 struct atp *dev = usb_get_intfdata(iface);
880
881 usb_set_intfdata(iface, NULL);
882 if (dev) {
883 usb_kill_urb(dev->urb);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500884 input_unregister_device(dev->input);
Daniel Mack997ea582010-04-12 13:17:25 +0200885 usb_free_coherent(dev->udev, dev->info->datalen,
886 dev->data, dev->urb->transfer_dma);
Johannes Berg7af569a2006-07-19 15:39:46 +0200887 usb_free_urb(dev->urb);
Stelian Popf7214ff2005-09-08 10:19:48 +0200888 kfree(dev);
889 }
890 printk(KERN_INFO "input: appletouch disconnected\n");
891}
892
Oliver Neukum90d95ef2008-06-17 11:56:55 -0400893static int atp_recover(struct atp *dev)
894{
895 int error;
896
897 error = atp_handle_geyser(dev);
898 if (error)
899 return error;
900
901 if (dev->open && usb_submit_urb(dev->urb, GFP_ATOMIC))
902 return -EIO;
903
904 return 0;
905}
906
Stelian Popf7214ff2005-09-08 10:19:48 +0200907static int atp_suspend(struct usb_interface *iface, pm_message_t message)
908{
909 struct atp *dev = usb_get_intfdata(iface);
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400910
Stelian Popf7214ff2005-09-08 10:19:48 +0200911 usb_kill_urb(dev->urb);
Stelian Popf7214ff2005-09-08 10:19:48 +0200912 return 0;
913}
914
915static int atp_resume(struct usb_interface *iface)
916{
917 struct atp *dev = usb_get_intfdata(iface);
Soeren Sonnenburg5a6eb672007-07-20 00:29:32 -0400918
Stelian Popf7214ff2005-09-08 10:19:48 +0200919 if (dev->open && usb_submit_urb(dev->urb, GFP_ATOMIC))
920 return -EIO;
921
922 return 0;
923}
924
Oliver Neukum90d95ef2008-06-17 11:56:55 -0400925static int atp_reset_resume(struct usb_interface *iface)
926{
927 struct atp *dev = usb_get_intfdata(iface);
928
929 return atp_recover(dev);
930}
931
Stelian Popf7214ff2005-09-08 10:19:48 +0200932static struct usb_driver atp_driver = {
Stelian Popf7214ff2005-09-08 10:19:48 +0200933 .name = "appletouch",
934 .probe = atp_probe,
935 .disconnect = atp_disconnect,
936 .suspend = atp_suspend,
937 .resume = atp_resume,
Oliver Neukum90d95ef2008-06-17 11:56:55 -0400938 .reset_resume = atp_reset_resume,
Stelian Popf7214ff2005-09-08 10:19:48 +0200939 .id_table = atp_table,
940};
941
Greg Kroah-Hartman08642e72011-11-18 09:48:31 -0800942module_usb_driver(atp_driver);