blob: b952317639116f2f18a7bc1f41ff5887c17f2a49 [file] [log] [blame]
Henrik Rydbergf89bd952008-08-08 14:59:30 -04001/*
2 * Apple USB BCM5974 (Macbook Air and Penryn Macbook Pro) multitouch driver
3 *
4 * Copyright (C) 2008 Henrik Rydberg (rydberg@euromail.se)
5 *
6 * The USB initialization and package decoding was made by
7 * Scott Shawcroft as part of the touchd user-space driver project:
8 * Copyright (C) 2008 Scott Shawcroft (scott.shawcroft@gmail.com)
9 *
10 * The BCM5974 driver is based on the appletouch driver:
11 * Copyright (C) 2001-2004 Greg Kroah-Hartman (greg@kroah.com)
12 * Copyright (C) 2005 Johannes Berg (johannes@sipsolutions.net)
13 * Copyright (C) 2005 Stelian Pop (stelian@popies.net)
14 * Copyright (C) 2005 Frank Arnold (frank@scirocco-5v-turbo.de)
15 * Copyright (C) 2005 Peter Osterlund (petero2@telia.com)
16 * Copyright (C) 2005 Michael Hanselmann (linux-kernel@hansmi.ch)
17 * Copyright (C) 2006 Nicolas Boichat (nicolas@boichat.ch)
18 *
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
23 *
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
32 *
33 */
34
35#include <linux/kernel.h>
36#include <linux/errno.h>
37#include <linux/init.h>
38#include <linux/slab.h>
39#include <linux/module.h>
40#include <linux/usb/input.h>
41#include <linux/hid.h>
42#include <linux/mutex.h>
43
44#define USB_VENDOR_ID_APPLE 0x05ac
45
46/* MacbookAir, aka wellspring */
47#define USB_DEVICE_ID_APPLE_WELLSPRING_ANSI 0x0223
48#define USB_DEVICE_ID_APPLE_WELLSPRING_ISO 0x0224
49#define USB_DEVICE_ID_APPLE_WELLSPRING_JIS 0x0225
50/* MacbookProPenryn, aka wellspring2 */
51#define USB_DEVICE_ID_APPLE_WELLSPRING2_ANSI 0x0230
52#define USB_DEVICE_ID_APPLE_WELLSPRING2_ISO 0x0231
53#define USB_DEVICE_ID_APPLE_WELLSPRING2_JIS 0x0232
Henrik Rydberg158e9282009-04-28 07:03:54 -070054/* Macbook5,1 (unibody), aka wellspring3 */
55#define USB_DEVICE_ID_APPLE_WELLSPRING3_ANSI 0x0236
56#define USB_DEVICE_ID_APPLE_WELLSPRING3_ISO 0x0237
57#define USB_DEVICE_ID_APPLE_WELLSPRING3_JIS 0x0238
Henrik Rydbergf89bd952008-08-08 14:59:30 -040058
59#define BCM5974_DEVICE(prod) { \
60 .match_flags = (USB_DEVICE_ID_MATCH_DEVICE | \
61 USB_DEVICE_ID_MATCH_INT_CLASS | \
62 USB_DEVICE_ID_MATCH_INT_PROTOCOL), \
63 .idVendor = USB_VENDOR_ID_APPLE, \
64 .idProduct = (prod), \
65 .bInterfaceClass = USB_INTERFACE_CLASS_HID, \
66 .bInterfaceProtocol = USB_INTERFACE_PROTOCOL_MOUSE \
67}
68
69/* table of devices that work with this driver */
Henrik Rydberg158e0fb2008-09-04 22:20:10 -040070static const struct usb_device_id bcm5974_table[] = {
Henrik Rydbergf89bd952008-08-08 14:59:30 -040071 /* MacbookAir1.1 */
72 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING_ANSI),
73 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING_ISO),
74 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING_JIS),
75 /* MacbookProPenryn */
76 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING2_ANSI),
77 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING2_ISO),
78 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING2_JIS),
Henrik Rydberg158e9282009-04-28 07:03:54 -070079 /* Macbook5,1 */
80 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING3_ANSI),
81 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING3_ISO),
82 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING3_JIS),
Henrik Rydbergf89bd952008-08-08 14:59:30 -040083 /* Terminating entry */
84 {}
85};
86MODULE_DEVICE_TABLE(usb, bcm5974_table);
87
88MODULE_AUTHOR("Henrik Rydberg");
89MODULE_DESCRIPTION("Apple USB BCM5974 multitouch driver");
90MODULE_LICENSE("GPL");
91
92#define dprintk(level, format, a...)\
93 { if (debug >= level) printk(KERN_DEBUG format, ##a); }
94
95static int debug = 1;
96module_param(debug, int, 0644);
97MODULE_PARM_DESC(debug, "Activate debugging output");
98
99/* button data structure */
100struct bt_data {
101 u8 unknown1; /* constant */
102 u8 button; /* left button */
103 u8 rel_x; /* relative x coordinate */
104 u8 rel_y; /* relative y coordinate */
105};
106
Henrik Rydberg9894cf02009-04-27 11:52:42 -0700107/* trackpad header types */
108enum tp_type {
Henrik Rydberg158e9282009-04-28 07:03:54 -0700109 TYPE1, /* plain trackpad */
110 TYPE2 /* button integrated in trackpad */
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400111};
112
Henrik Rydberg9894cf02009-04-27 11:52:42 -0700113/* trackpad finger data offsets, le16-aligned */
114#define FINGER_TYPE1 (13 * sizeof(__le16))
Henrik Rydberg158e9282009-04-28 07:03:54 -0700115#define FINGER_TYPE2 (15 * sizeof(__le16))
116
117/* trackpad button data offsets */
118#define BUTTON_TYPE2 15
119
120/* list of device capability bits */
121#define HAS_INTEGRATED_BUTTON 1
Henrik Rydberg9894cf02009-04-27 11:52:42 -0700122
123/* trackpad finger structure, le16-aligned */
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400124struct tp_finger {
Henrik Rydberg75e21e32008-09-04 22:28:23 -0400125 __le16 origin; /* zero when switching track finger */
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400126 __le16 abs_x; /* absolute x coodinate */
127 __le16 abs_y; /* absolute y coodinate */
128 __le16 rel_x; /* relative x coodinate */
129 __le16 rel_y; /* relative y coodinate */
130 __le16 size_major; /* finger size, major axis? */
131 __le16 size_minor; /* finger size, minor axis? */
132 __le16 orientation; /* 16384 when point, else 15 bit angle */
133 __le16 force_major; /* trackpad force, major axis? */
134 __le16 force_minor; /* trackpad force, minor axis? */
135 __le16 unused[3]; /* zeros */
136 __le16 multi; /* one finger: varies, more fingers: constant */
Henrik Rydberg9894cf02009-04-27 11:52:42 -0700137} __attribute__((packed,aligned(2)));
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400138
Henrik Rydberg9894cf02009-04-27 11:52:42 -0700139/* trackpad finger data size, empirically at least ten fingers */
140#define SIZEOF_FINGER sizeof(struct tp_finger)
141#define SIZEOF_ALL_FINGERS (16 * SIZEOF_FINGER)
Henrik Rydberg6f2701b2010-01-06 00:32:48 -0800142#define MAX_FINGER_ORIENTATION 16384
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400143
144/* device-specific parameters */
145struct bcm5974_param {
146 int dim; /* logical dimension */
147 int fuzz; /* logical noise value */
148 int devmin; /* device minimum reading */
149 int devmax; /* device maximum reading */
150};
151
152/* device-specific configuration */
153struct bcm5974_config {
154 int ansi, iso, jis; /* the product id of this device */
Henrik Rydberg158e9282009-04-28 07:03:54 -0700155 int caps; /* device capability bitmask */
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400156 int bt_ep; /* the endpoint of the button interface */
157 int bt_datalen; /* data length of the button interface */
158 int tp_ep; /* the endpoint of the trackpad interface */
Henrik Rydberg9894cf02009-04-27 11:52:42 -0700159 enum tp_type tp_type; /* type of trackpad interface */
160 int tp_offset; /* offset to trackpad finger data */
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400161 int tp_datalen; /* data length of the trackpad interface */
162 struct bcm5974_param p; /* finger pressure limits */
163 struct bcm5974_param w; /* finger width limits */
164 struct bcm5974_param x; /* horizontal limits */
165 struct bcm5974_param y; /* vertical limits */
166};
167
168/* logical device structure */
169struct bcm5974 {
170 char phys[64];
171 struct usb_device *udev; /* usb device */
Dmitry Torokhov88da7652008-08-08 14:59:32 -0400172 struct usb_interface *intf; /* our interface */
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400173 struct input_dev *input; /* input dev */
174 struct bcm5974_config cfg; /* device configuration */
175 struct mutex pm_mutex; /* serialize access to open/suspend */
176 int opened; /* 1: opened, 0: closed */
177 struct urb *bt_urb; /* button usb request block */
178 struct bt_data *bt_data; /* button transferred data */
179 struct urb *tp_urb; /* trackpad usb request block */
Henrik Rydberg9894cf02009-04-27 11:52:42 -0700180 u8 *tp_data; /* trackpad transferred data */
Henrik Rydberg75e21e32008-09-04 22:28:23 -0400181 int fingers; /* number of fingers on trackpad */
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400182};
183
184/* logical dimensions */
185#define DIM_PRESSURE 256 /* maximum finger pressure */
186#define DIM_WIDTH 16 /* maximum finger width */
187#define DIM_X 1280 /* maximum trackpad x value */
188#define DIM_Y 800 /* maximum trackpad y value */
189
190/* logical signal quality */
191#define SN_PRESSURE 45 /* pressure signal-to-noise ratio */
192#define SN_WIDTH 100 /* width signal-to-noise ratio */
193#define SN_COORD 250 /* coordinate signal-to-noise ratio */
194
Henrik Rydberg75e21e32008-09-04 22:28:23 -0400195/* pressure thresholds */
196#define PRESSURE_LOW (2 * DIM_PRESSURE / SN_PRESSURE)
197#define PRESSURE_HIGH (3 * PRESSURE_LOW)
198
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400199/* device constants */
200static const struct bcm5974_config bcm5974_config_table[] = {
201 {
202 USB_DEVICE_ID_APPLE_WELLSPRING_ANSI,
203 USB_DEVICE_ID_APPLE_WELLSPRING_ISO,
204 USB_DEVICE_ID_APPLE_WELLSPRING_JIS,
Henrik Rydberg158e9282009-04-28 07:03:54 -0700205 0,
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400206 0x84, sizeof(struct bt_data),
Henrik Rydberg9894cf02009-04-27 11:52:42 -0700207 0x81, TYPE1, FINGER_TYPE1, FINGER_TYPE1 + SIZEOF_ALL_FINGERS,
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400208 { DIM_PRESSURE, DIM_PRESSURE / SN_PRESSURE, 0, 256 },
209 { DIM_WIDTH, DIM_WIDTH / SN_WIDTH, 0, 2048 },
210 { DIM_X, DIM_X / SN_COORD, -4824, 5342 },
211 { DIM_Y, DIM_Y / SN_COORD, -172, 5820 }
212 },
213 {
214 USB_DEVICE_ID_APPLE_WELLSPRING2_ANSI,
215 USB_DEVICE_ID_APPLE_WELLSPRING2_ISO,
216 USB_DEVICE_ID_APPLE_WELLSPRING2_JIS,
Henrik Rydberg158e9282009-04-28 07:03:54 -0700217 0,
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400218 0x84, sizeof(struct bt_data),
Henrik Rydberg9894cf02009-04-27 11:52:42 -0700219 0x81, TYPE1, FINGER_TYPE1, FINGER_TYPE1 + SIZEOF_ALL_FINGERS,
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400220 { DIM_PRESSURE, DIM_PRESSURE / SN_PRESSURE, 0, 256 },
221 { DIM_WIDTH, DIM_WIDTH / SN_WIDTH, 0, 2048 },
222 { DIM_X, DIM_X / SN_COORD, -4824, 4824 },
223 { DIM_Y, DIM_Y / SN_COORD, -172, 4290 }
224 },
Henrik Rydberg158e9282009-04-28 07:03:54 -0700225 {
226 USB_DEVICE_ID_APPLE_WELLSPRING3_ANSI,
227 USB_DEVICE_ID_APPLE_WELLSPRING3_ISO,
228 USB_DEVICE_ID_APPLE_WELLSPRING3_JIS,
229 HAS_INTEGRATED_BUTTON,
230 0x84, sizeof(struct bt_data),
231 0x81, TYPE2, FINGER_TYPE2, FINGER_TYPE2 + SIZEOF_ALL_FINGERS,
232 { DIM_PRESSURE, DIM_PRESSURE / SN_PRESSURE, 0, 300 },
233 { DIM_WIDTH, DIM_WIDTH / SN_WIDTH, 0, 2048 },
234 { DIM_X, DIM_X / SN_COORD, -4460, 5166 },
235 { DIM_Y, DIM_Y / SN_COORD, -75, 6700 }
236 },
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400237 {}
238};
239
240/* return the device-specific configuration by device */
241static const struct bcm5974_config *bcm5974_get_config(struct usb_device *udev)
242{
243 u16 id = le16_to_cpu(udev->descriptor.idProduct);
244 const struct bcm5974_config *cfg;
245
246 for (cfg = bcm5974_config_table; cfg->ansi; ++cfg)
247 if (cfg->ansi == id || cfg->iso == id || cfg->jis == id)
248 return cfg;
249
250 return bcm5974_config_table;
251}
252
253/* convert 16-bit little endian to signed integer */
254static inline int raw2int(__le16 x)
255{
256 return (signed short)le16_to_cpu(x);
257}
258
259/* scale device data to logical dimensions (asserts devmin < devmax) */
260static inline int int2scale(const struct bcm5974_param *p, int x)
261{
262 return x * p->dim / (p->devmax - p->devmin);
263}
264
265/* all logical value ranges are [0,dim). */
266static inline int int2bound(const struct bcm5974_param *p, int x)
267{
268 int s = int2scale(p, x);
269
270 return clamp_val(s, 0, p->dim - 1);
271}
272
273/* setup which logical events to report */
274static void setup_events_to_report(struct input_dev *input_dev,
275 const struct bcm5974_config *cfg)
276{
277 __set_bit(EV_ABS, input_dev->evbit);
278
279 input_set_abs_params(input_dev, ABS_PRESSURE,
280 0, cfg->p.dim, cfg->p.fuzz, 0);
281 input_set_abs_params(input_dev, ABS_TOOL_WIDTH,
282 0, cfg->w.dim, cfg->w.fuzz, 0);
283 input_set_abs_params(input_dev, ABS_X,
284 0, cfg->x.dim, cfg->x.fuzz, 0);
285 input_set_abs_params(input_dev, ABS_Y,
286 0, cfg->y.dim, cfg->y.fuzz, 0);
287
Henrik Rydberg6f2701b2010-01-06 00:32:48 -0800288 /* finger touch area */
289 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
290 cfg->w.devmin, cfg->w.devmax, 0, 0);
291 input_set_abs_params(input_dev, ABS_MT_TOUCH_MINOR,
292 cfg->w.devmin, cfg->w.devmax, 0, 0);
293 /* finger approach area */
294 input_set_abs_params(input_dev, ABS_MT_WIDTH_MAJOR,
295 cfg->w.devmin, cfg->w.devmax, 0, 0);
296 input_set_abs_params(input_dev, ABS_MT_WIDTH_MINOR,
297 cfg->w.devmin, cfg->w.devmax, 0, 0);
298 /* finger orientation */
299 input_set_abs_params(input_dev, ABS_MT_ORIENTATION,
300 -MAX_FINGER_ORIENTATION,
301 MAX_FINGER_ORIENTATION, 0, 0);
302 /* finger position */
303 input_set_abs_params(input_dev, ABS_MT_POSITION_X,
304 cfg->x.devmin, cfg->x.devmax, 0, 0);
305 input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
306 cfg->y.devmin, cfg->y.devmax, 0, 0);
307
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400308 __set_bit(EV_KEY, input_dev->evbit);
Henrik Rydberga6821f32008-09-04 22:28:31 -0400309 __set_bit(BTN_TOUCH, input_dev->keybit);
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400310 __set_bit(BTN_TOOL_FINGER, input_dev->keybit);
311 __set_bit(BTN_TOOL_DOUBLETAP, input_dev->keybit);
312 __set_bit(BTN_TOOL_TRIPLETAP, input_dev->keybit);
Henrik Rydberg6916d972009-04-27 11:52:43 -0700313 __set_bit(BTN_TOOL_QUADTAP, input_dev->keybit);
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400314 __set_bit(BTN_LEFT, input_dev->keybit);
Henrik Rydbergc13aea02010-06-23 09:30:22 -0700315
316 input_set_events_per_packet(input_dev, 60);
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400317}
318
319/* report button data as logical button state */
320static int report_bt_state(struct bcm5974 *dev, int size)
321{
322 if (size != sizeof(struct bt_data))
323 return -EIO;
324
Henrik Rydberg53402192009-04-28 07:04:42 -0700325 dprintk(7,
326 "bcm5974: button data: %x %x %x %x\n",
327 dev->bt_data->unknown1, dev->bt_data->button,
328 dev->bt_data->rel_x, dev->bt_data->rel_y);
329
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400330 input_report_key(dev->input, BTN_LEFT, dev->bt_data->button);
331 input_sync(dev->input);
332
333 return 0;
334}
335
Henrik Rydberg6f2701b2010-01-06 00:32:48 -0800336static void report_finger_data(struct input_dev *input,
337 const struct bcm5974_config *cfg,
338 const struct tp_finger *f)
339{
Henrik Rydberg57157be2010-08-31 17:27:02 -0700340 input_report_abs(input, ABS_MT_TOUCH_MAJOR,
341 raw2int(f->force_major) << 1);
342 input_report_abs(input, ABS_MT_TOUCH_MINOR,
343 raw2int(f->force_minor) << 1);
344 input_report_abs(input, ABS_MT_WIDTH_MAJOR,
345 raw2int(f->size_major) << 1);
346 input_report_abs(input, ABS_MT_WIDTH_MINOR,
347 raw2int(f->size_minor) << 1);
Henrik Rydberg6f2701b2010-01-06 00:32:48 -0800348 input_report_abs(input, ABS_MT_ORIENTATION,
349 MAX_FINGER_ORIENTATION - raw2int(f->orientation));
350 input_report_abs(input, ABS_MT_POSITION_X, raw2int(f->abs_x));
351 input_report_abs(input, ABS_MT_POSITION_Y,
352 cfg->y.devmin + cfg->y.devmax - raw2int(f->abs_y));
353 input_mt_sync(input);
354}
355
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400356/* report trackpad data as logical trackpad state */
357static int report_tp_state(struct bcm5974 *dev, int size)
358{
359 const struct bcm5974_config *c = &dev->cfg;
Henrik Rydberg9894cf02009-04-27 11:52:42 -0700360 const struct tp_finger *f;
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400361 struct input_dev *input = dev->input;
Henrik Rydberg6f2701b2010-01-06 00:32:48 -0800362 int raw_p, raw_w, raw_x, raw_y, raw_n, i;
Henrik Rydberg9de48cc2009-09-13 09:10:11 -0700363 int ptest, origin, ibt = 0, nmin = 0, nmax = 0;
Henrik Rydberg75e21e32008-09-04 22:28:23 -0400364 int abs_p = 0, abs_w = 0, abs_x = 0, abs_y = 0;
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400365
Henrik Rydberg9894cf02009-04-27 11:52:42 -0700366 if (size < c->tp_offset || (size - c->tp_offset) % SIZEOF_FINGER != 0)
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400367 return -EIO;
368
Henrik Rydberg9894cf02009-04-27 11:52:42 -0700369 /* finger data, le16-aligned */
370 f = (const struct tp_finger *)(dev->tp_data + c->tp_offset);
371 raw_n = (size - c->tp_offset) / SIZEOF_FINGER;
372
Henrik Rydberg75e21e32008-09-04 22:28:23 -0400373 /* always track the first finger; when detached, start over */
Henrik Rydberg9894cf02009-04-27 11:52:42 -0700374 if (raw_n) {
Henrik Rydberg6f2701b2010-01-06 00:32:48 -0800375
376 /* report raw trackpad data */
377 for (i = 0; i < raw_n; i++)
378 report_finger_data(input, c, &f[i]);
379
Henrik Rydberg75e21e32008-09-04 22:28:23 -0400380 raw_p = raw2int(f->force_major);
381 raw_w = raw2int(f->size_major);
382 raw_x = raw2int(f->abs_x);
383 raw_y = raw2int(f->abs_y);
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400384
385 dprintk(9,
Henrik Rydberg53402192009-04-28 07:04:42 -0700386 "bcm5974: "
387 "raw: p: %+05d w: %+05d x: %+05d y: %+05d n: %d\n",
388 raw_p, raw_w, raw_x, raw_y, raw_n);
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400389
Henrik Rydberg75e21e32008-09-04 22:28:23 -0400390 ptest = int2bound(&c->p, raw_p);
391 origin = raw2int(f->origin);
Henrik Rydberg158e9282009-04-28 07:03:54 -0700392
393 /* set the integrated button if applicable */
394 if (c->tp_type == TYPE2)
395 ibt = raw2int(dev->tp_data[BUTTON_TYPE2]);
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400396
Henrik Rydberg9de48cc2009-09-13 09:10:11 -0700397 /* while tracking finger still valid, count all fingers */
398 if (ptest > PRESSURE_LOW && origin) {
399 abs_p = ptest;
400 abs_w = int2bound(&c->w, raw_w);
401 abs_x = int2bound(&c->x, raw_x - c->x.devmin);
402 abs_y = int2bound(&c->y, c->y.devmax - raw_y);
403 while (raw_n--) {
404 ptest = int2bound(&c->p,
405 raw2int(f->force_major));
406 if (ptest > PRESSURE_LOW)
407 nmax++;
408 if (ptest > PRESSURE_HIGH)
409 nmin++;
410 f++;
411 }
Henrik Rydberg75e21e32008-09-04 22:28:23 -0400412 }
413 }
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400414
Henrik Rydberg75e21e32008-09-04 22:28:23 -0400415 if (dev->fingers < nmin)
416 dev->fingers = nmin;
417 if (dev->fingers > nmax)
418 dev->fingers = nmax;
419
Henrik Rydberga6821f32008-09-04 22:28:31 -0400420 input_report_key(input, BTN_TOUCH, dev->fingers > 0);
Henrik Rydberg75e21e32008-09-04 22:28:23 -0400421 input_report_key(input, BTN_TOOL_FINGER, dev->fingers == 1);
422 input_report_key(input, BTN_TOOL_DOUBLETAP, dev->fingers == 2);
Henrik Rydberg6916d972009-04-27 11:52:43 -0700423 input_report_key(input, BTN_TOOL_TRIPLETAP, dev->fingers == 3);
424 input_report_key(input, BTN_TOOL_QUADTAP, dev->fingers > 3);
Henrik Rydberg75e21e32008-09-04 22:28:23 -0400425
426 input_report_abs(input, ABS_PRESSURE, abs_p);
427 input_report_abs(input, ABS_TOOL_WIDTH, abs_w);
428
429 if (abs_p) {
430 input_report_abs(input, ABS_X, abs_x);
431 input_report_abs(input, ABS_Y, abs_y);
432
433 dprintk(8,
434 "bcm5974: abs: p: %+05d w: %+05d x: %+05d y: %+05d "
Henrik Rydberg53402192009-04-28 07:04:42 -0700435 "nmin: %d nmax: %d n: %d ibt: %d\n", abs_p, abs_w,
436 abs_x, abs_y, nmin, nmax, dev->fingers, ibt);
Henrik Rydberg75e21e32008-09-04 22:28:23 -0400437
438 }
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400439
Henrik Rydberg158e9282009-04-28 07:03:54 -0700440 /* type 2 reports button events via ibt only */
441 if (c->tp_type == TYPE2)
442 input_report_key(input, BTN_LEFT, ibt);
443
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400444 input_sync(input);
445
446 return 0;
447}
448
449/* Wellspring initialization constants */
450#define BCM5974_WELLSPRING_MODE_READ_REQUEST_ID 1
451#define BCM5974_WELLSPRING_MODE_WRITE_REQUEST_ID 9
452#define BCM5974_WELLSPRING_MODE_REQUEST_VALUE 0x300
453#define BCM5974_WELLSPRING_MODE_REQUEST_INDEX 0
454#define BCM5974_WELLSPRING_MODE_VENDOR_VALUE 0x01
Henrik Rydbergcd72ad32008-09-14 11:52:44 -0400455#define BCM5974_WELLSPRING_MODE_NORMAL_VALUE 0x08
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400456
Henrik Rydbergcd72ad32008-09-14 11:52:44 -0400457static int bcm5974_wellspring_mode(struct bcm5974 *dev, bool on)
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400458{
459 char *data = kmalloc(8, GFP_KERNEL);
460 int retval = 0, size;
461
462 if (!data) {
463 err("bcm5974: out of memory");
464 retval = -ENOMEM;
465 goto out;
466 }
467
468 /* read configuration */
469 size = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
470 BCM5974_WELLSPRING_MODE_READ_REQUEST_ID,
471 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
472 BCM5974_WELLSPRING_MODE_REQUEST_VALUE,
473 BCM5974_WELLSPRING_MODE_REQUEST_INDEX, data, 8, 5000);
474
475 if (size != 8) {
476 err("bcm5974: could not read from device");
477 retval = -EIO;
478 goto out;
479 }
480
481 /* apply the mode switch */
Henrik Rydbergcd72ad32008-09-14 11:52:44 -0400482 data[0] = on ?
483 BCM5974_WELLSPRING_MODE_VENDOR_VALUE :
484 BCM5974_WELLSPRING_MODE_NORMAL_VALUE;
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400485
486 /* write configuration */
487 size = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
488 BCM5974_WELLSPRING_MODE_WRITE_REQUEST_ID,
489 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
490 BCM5974_WELLSPRING_MODE_REQUEST_VALUE,
491 BCM5974_WELLSPRING_MODE_REQUEST_INDEX, data, 8, 5000);
492
493 if (size != 8) {
494 err("bcm5974: could not write to device");
495 retval = -EIO;
496 goto out;
497 }
498
Henrik Rydbergcd72ad32008-09-14 11:52:44 -0400499 dprintk(2, "bcm5974: switched to %s mode.\n",
500 on ? "wellspring" : "normal");
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400501
502 out:
503 kfree(data);
504 return retval;
505}
506
507static void bcm5974_irq_button(struct urb *urb)
508{
509 struct bcm5974 *dev = urb->context;
510 int error;
511
512 switch (urb->status) {
513 case 0:
514 break;
515 case -EOVERFLOW:
516 case -ECONNRESET:
517 case -ENOENT:
518 case -ESHUTDOWN:
519 dbg("bcm5974: button urb shutting down: %d", urb->status);
520 return;
521 default:
522 dbg("bcm5974: button urb status: %d", urb->status);
523 goto exit;
524 }
525
526 if (report_bt_state(dev, dev->bt_urb->actual_length))
527 dprintk(1, "bcm5974: bad button package, length: %d\n",
528 dev->bt_urb->actual_length);
529
530exit:
531 error = usb_submit_urb(dev->bt_urb, GFP_ATOMIC);
532 if (error)
533 err("bcm5974: button urb failed: %d", error);
534}
535
536static void bcm5974_irq_trackpad(struct urb *urb)
537{
538 struct bcm5974 *dev = urb->context;
539 int error;
540
541 switch (urb->status) {
542 case 0:
543 break;
544 case -EOVERFLOW:
545 case -ECONNRESET:
546 case -ENOENT:
547 case -ESHUTDOWN:
548 dbg("bcm5974: trackpad urb shutting down: %d", urb->status);
549 return;
550 default:
551 dbg("bcm5974: trackpad urb status: %d", urb->status);
552 goto exit;
553 }
554
555 /* control response ignored */
556 if (dev->tp_urb->actual_length == 2)
557 goto exit;
558
559 if (report_tp_state(dev, dev->tp_urb->actual_length))
560 dprintk(1, "bcm5974: bad trackpad package, length: %d\n",
561 dev->tp_urb->actual_length);
562
563exit:
564 error = usb_submit_urb(dev->tp_urb, GFP_ATOMIC);
565 if (error)
566 err("bcm5974: trackpad urb failed: %d", error);
567}
568
569/*
570 * The Wellspring trackpad, like many recent Apple trackpads, share
571 * the usb device with the keyboard. Since keyboards are usually
572 * handled by the HID system, the device ends up being handled by two
573 * modules. Setting up the device therefore becomes slightly
574 * complicated. To enable multitouch features, a mode switch is
575 * required, which is usually applied via the control interface of the
576 * device. It can be argued where this switch should take place. In
577 * some drivers, like appletouch, the switch is made during
578 * probe. However, the hid module may also alter the state of the
579 * device, resulting in trackpad malfunction under certain
580 * circumstances. To get around this problem, there is at least one
581 * example that utilizes the USB_QUIRK_RESET_RESUME quirk in order to
582 * recieve a reset_resume request rather than the normal resume.
583 * Since the implementation of reset_resume is equal to mode switch
584 * plus start_traffic, it seems easier to always do the switch when
585 * starting traffic on the device.
586 */
587static int bcm5974_start_traffic(struct bcm5974 *dev)
588{
Luo Jinghua1719ec42010-06-08 01:01:48 -0700589 int error;
590
591 error = bcm5974_wellspring_mode(dev, true);
592 if (error) {
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400593 dprintk(1, "bcm5974: mode switch failed\n");
Luo Jinghua1719ec42010-06-08 01:01:48 -0700594 goto err_out;
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400595 }
596
Luo Jinghua1719ec42010-06-08 01:01:48 -0700597 error = usb_submit_urb(dev->bt_urb, GFP_KERNEL);
598 if (error)
599 goto err_reset_mode;
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400600
Luo Jinghua1719ec42010-06-08 01:01:48 -0700601 error = usb_submit_urb(dev->tp_urb, GFP_KERNEL);
602 if (error)
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400603 goto err_kill_bt;
604
605 return 0;
606
607err_kill_bt:
608 usb_kill_urb(dev->bt_urb);
Luo Jinghua1719ec42010-06-08 01:01:48 -0700609err_reset_mode:
610 bcm5974_wellspring_mode(dev, false);
611err_out:
612 return error;
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400613}
614
615static void bcm5974_pause_traffic(struct bcm5974 *dev)
616{
617 usb_kill_urb(dev->tp_urb);
618 usb_kill_urb(dev->bt_urb);
Henrik Rydbergcd72ad32008-09-14 11:52:44 -0400619 bcm5974_wellspring_mode(dev, false);
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400620}
621
622/*
623 * The code below implements open/close and manual suspend/resume.
624 * All functions may be called in random order.
625 *
626 * Opening a suspended device fails with EACCES - permission denied.
627 *
628 * Failing a resume leaves the device resumed but closed.
629 */
630static int bcm5974_open(struct input_dev *input)
631{
632 struct bcm5974 *dev = input_get_drvdata(input);
633 int error;
634
Dmitry Torokhov88da7652008-08-08 14:59:32 -0400635 error = usb_autopm_get_interface(dev->intf);
636 if (error)
637 return error;
638
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400639 mutex_lock(&dev->pm_mutex);
640
641 error = bcm5974_start_traffic(dev);
642 if (!error)
643 dev->opened = 1;
644
645 mutex_unlock(&dev->pm_mutex);
646
Dmitry Torokhov88da7652008-08-08 14:59:32 -0400647 if (error)
648 usb_autopm_put_interface(dev->intf);
649
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400650 return error;
651}
652
653static void bcm5974_close(struct input_dev *input)
654{
655 struct bcm5974 *dev = input_get_drvdata(input);
656
657 mutex_lock(&dev->pm_mutex);
658
659 bcm5974_pause_traffic(dev);
660 dev->opened = 0;
661
662 mutex_unlock(&dev->pm_mutex);
Dmitry Torokhov88da7652008-08-08 14:59:32 -0400663
664 usb_autopm_put_interface(dev->intf);
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400665}
666
667static int bcm5974_suspend(struct usb_interface *iface, pm_message_t message)
668{
669 struct bcm5974 *dev = usb_get_intfdata(iface);
670
671 mutex_lock(&dev->pm_mutex);
672
673 if (dev->opened)
674 bcm5974_pause_traffic(dev);
675
676 mutex_unlock(&dev->pm_mutex);
677
678 return 0;
679}
680
681static int bcm5974_resume(struct usb_interface *iface)
682{
683 struct bcm5974 *dev = usb_get_intfdata(iface);
684 int error = 0;
685
686 mutex_lock(&dev->pm_mutex);
687
688 if (dev->opened)
689 error = bcm5974_start_traffic(dev);
690
691 mutex_unlock(&dev->pm_mutex);
692
693 return error;
694}
695
696static int bcm5974_probe(struct usb_interface *iface,
697 const struct usb_device_id *id)
698{
699 struct usb_device *udev = interface_to_usbdev(iface);
700 const struct bcm5974_config *cfg;
701 struct bcm5974 *dev;
702 struct input_dev *input_dev;
703 int error = -ENOMEM;
704
705 /* find the product index */
706 cfg = bcm5974_get_config(udev);
707
708 /* allocate memory for our device state and initialize it */
709 dev = kzalloc(sizeof(struct bcm5974), GFP_KERNEL);
710 input_dev = input_allocate_device();
711 if (!dev || !input_dev) {
712 err("bcm5974: out of memory");
713 goto err_free_devs;
714 }
715
716 dev->udev = udev;
Dmitry Torokhov88da7652008-08-08 14:59:32 -0400717 dev->intf = iface;
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400718 dev->input = input_dev;
719 dev->cfg = *cfg;
720 mutex_init(&dev->pm_mutex);
721
722 /* setup urbs */
723 dev->bt_urb = usb_alloc_urb(0, GFP_KERNEL);
724 if (!dev->bt_urb)
725 goto err_free_devs;
726
727 dev->tp_urb = usb_alloc_urb(0, GFP_KERNEL);
728 if (!dev->tp_urb)
729 goto err_free_bt_urb;
730
Daniel Mack997ea582010-04-12 13:17:25 +0200731 dev->bt_data = usb_alloc_coherent(dev->udev,
732 dev->cfg.bt_datalen, GFP_KERNEL,
733 &dev->bt_urb->transfer_dma);
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400734 if (!dev->bt_data)
735 goto err_free_urb;
736
Daniel Mack997ea582010-04-12 13:17:25 +0200737 dev->tp_data = usb_alloc_coherent(dev->udev,
738 dev->cfg.tp_datalen, GFP_KERNEL,
739 &dev->tp_urb->transfer_dma);
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400740 if (!dev->tp_data)
741 goto err_free_bt_buffer;
742
743 usb_fill_int_urb(dev->bt_urb, udev,
744 usb_rcvintpipe(udev, cfg->bt_ep),
745 dev->bt_data, dev->cfg.bt_datalen,
746 bcm5974_irq_button, dev, 1);
747
748 usb_fill_int_urb(dev->tp_urb, udev,
749 usb_rcvintpipe(udev, cfg->tp_ep),
750 dev->tp_data, dev->cfg.tp_datalen,
751 bcm5974_irq_trackpad, dev, 1);
752
753 /* create bcm5974 device */
754 usb_make_path(udev, dev->phys, sizeof(dev->phys));
755 strlcat(dev->phys, "/input0", sizeof(dev->phys));
756
757 input_dev->name = "bcm5974";
758 input_dev->phys = dev->phys;
759 usb_to_input_id(dev->udev, &input_dev->id);
Henrik Rydberg158e9282009-04-28 07:03:54 -0700760 /* report driver capabilities via the version field */
761 input_dev->id.version = cfg->caps;
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400762 input_dev->dev.parent = &iface->dev;
763
764 input_set_drvdata(input_dev, dev);
765
766 input_dev->open = bcm5974_open;
767 input_dev->close = bcm5974_close;
768
769 setup_events_to_report(input_dev, cfg);
770
771 error = input_register_device(dev->input);
772 if (error)
773 goto err_free_buffer;
774
775 /* save our data pointer in this interface device */
776 usb_set_intfdata(iface, dev);
777
778 return 0;
779
780err_free_buffer:
Daniel Mack997ea582010-04-12 13:17:25 +0200781 usb_free_coherent(dev->udev, dev->cfg.tp_datalen,
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400782 dev->tp_data, dev->tp_urb->transfer_dma);
783err_free_bt_buffer:
Daniel Mack997ea582010-04-12 13:17:25 +0200784 usb_free_coherent(dev->udev, dev->cfg.bt_datalen,
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400785 dev->bt_data, dev->bt_urb->transfer_dma);
786err_free_urb:
787 usb_free_urb(dev->tp_urb);
788err_free_bt_urb:
789 usb_free_urb(dev->bt_urb);
790err_free_devs:
791 usb_set_intfdata(iface, NULL);
792 input_free_device(input_dev);
793 kfree(dev);
794 return error;
795}
796
797static void bcm5974_disconnect(struct usb_interface *iface)
798{
799 struct bcm5974 *dev = usb_get_intfdata(iface);
800
801 usb_set_intfdata(iface, NULL);
802
803 input_unregister_device(dev->input);
Daniel Mack997ea582010-04-12 13:17:25 +0200804 usb_free_coherent(dev->udev, dev->cfg.tp_datalen,
805 dev->tp_data, dev->tp_urb->transfer_dma);
806 usb_free_coherent(dev->udev, dev->cfg.bt_datalen,
807 dev->bt_data, dev->bt_urb->transfer_dma);
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400808 usb_free_urb(dev->tp_urb);
809 usb_free_urb(dev->bt_urb);
810 kfree(dev);
811}
812
813static struct usb_driver bcm5974_driver = {
814 .name = "bcm5974",
815 .probe = bcm5974_probe,
816 .disconnect = bcm5974_disconnect,
817 .suspend = bcm5974_suspend,
818 .resume = bcm5974_resume,
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400819 .id_table = bcm5974_table,
Dmitry Torokhov88da7652008-08-08 14:59:32 -0400820 .supports_autosuspend = 1,
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400821};
822
823static int __init bcm5974_init(void)
824{
825 return usb_register(&bcm5974_driver);
826}
827
828static void __exit bcm5974_exit(void)
829{
830 usb_deregister(&bcm5974_driver);
831}
832
833module_init(bcm5974_init);
834module_exit(bcm5974_exit);
835