blob: 2ddf05e1d85290d58cfb20c52cdb4b3ff7641ddd [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
54
55#define BCM5974_DEVICE(prod) { \
56 .match_flags = (USB_DEVICE_ID_MATCH_DEVICE | \
57 USB_DEVICE_ID_MATCH_INT_CLASS | \
58 USB_DEVICE_ID_MATCH_INT_PROTOCOL), \
59 .idVendor = USB_VENDOR_ID_APPLE, \
60 .idProduct = (prod), \
61 .bInterfaceClass = USB_INTERFACE_CLASS_HID, \
62 .bInterfaceProtocol = USB_INTERFACE_PROTOCOL_MOUSE \
63}
64
65/* table of devices that work with this driver */
Henrik Rydberg158e0fb2008-09-04 22:20:10 -040066static const struct usb_device_id bcm5974_table[] = {
Henrik Rydbergf89bd952008-08-08 14:59:30 -040067 /* MacbookAir1.1 */
68 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING_ANSI),
69 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING_ISO),
70 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING_JIS),
71 /* MacbookProPenryn */
72 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING2_ANSI),
73 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING2_ISO),
74 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING2_JIS),
75 /* Terminating entry */
76 {}
77};
78MODULE_DEVICE_TABLE(usb, bcm5974_table);
79
80MODULE_AUTHOR("Henrik Rydberg");
81MODULE_DESCRIPTION("Apple USB BCM5974 multitouch driver");
82MODULE_LICENSE("GPL");
83
84#define dprintk(level, format, a...)\
85 { if (debug >= level) printk(KERN_DEBUG format, ##a); }
86
87static int debug = 1;
88module_param(debug, int, 0644);
89MODULE_PARM_DESC(debug, "Activate debugging output");
90
91/* button data structure */
92struct bt_data {
93 u8 unknown1; /* constant */
94 u8 button; /* left button */
95 u8 rel_x; /* relative x coordinate */
96 u8 rel_y; /* relative y coordinate */
97};
98
Henrik Rydberg9894cf02009-04-27 11:52:42 -070099/* trackpad header types */
100enum tp_type {
101 TYPE1 /* plain trackpad */
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400102};
103
Henrik Rydberg9894cf02009-04-27 11:52:42 -0700104/* trackpad finger data offsets, le16-aligned */
105#define FINGER_TYPE1 (13 * sizeof(__le16))
106
107/* trackpad finger structure, le16-aligned */
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400108struct tp_finger {
Henrik Rydberg75e21e32008-09-04 22:28:23 -0400109 __le16 origin; /* zero when switching track finger */
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400110 __le16 abs_x; /* absolute x coodinate */
111 __le16 abs_y; /* absolute y coodinate */
112 __le16 rel_x; /* relative x coodinate */
113 __le16 rel_y; /* relative y coodinate */
114 __le16 size_major; /* finger size, major axis? */
115 __le16 size_minor; /* finger size, minor axis? */
116 __le16 orientation; /* 16384 when point, else 15 bit angle */
117 __le16 force_major; /* trackpad force, major axis? */
118 __le16 force_minor; /* trackpad force, minor axis? */
119 __le16 unused[3]; /* zeros */
120 __le16 multi; /* one finger: varies, more fingers: constant */
Henrik Rydberg9894cf02009-04-27 11:52:42 -0700121} __attribute__((packed,aligned(2)));
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400122
Henrik Rydberg9894cf02009-04-27 11:52:42 -0700123/* trackpad finger data size, empirically at least ten fingers */
124#define SIZEOF_FINGER sizeof(struct tp_finger)
125#define SIZEOF_ALL_FINGERS (16 * SIZEOF_FINGER)
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400126
127/* device-specific parameters */
128struct bcm5974_param {
129 int dim; /* logical dimension */
130 int fuzz; /* logical noise value */
131 int devmin; /* device minimum reading */
132 int devmax; /* device maximum reading */
133};
134
135/* device-specific configuration */
136struct bcm5974_config {
137 int ansi, iso, jis; /* the product id of this device */
138 int bt_ep; /* the endpoint of the button interface */
139 int bt_datalen; /* data length of the button interface */
140 int tp_ep; /* the endpoint of the trackpad interface */
Henrik Rydberg9894cf02009-04-27 11:52:42 -0700141 enum tp_type tp_type; /* type of trackpad interface */
142 int tp_offset; /* offset to trackpad finger data */
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400143 int tp_datalen; /* data length of the trackpad interface */
144 struct bcm5974_param p; /* finger pressure limits */
145 struct bcm5974_param w; /* finger width limits */
146 struct bcm5974_param x; /* horizontal limits */
147 struct bcm5974_param y; /* vertical limits */
148};
149
150/* logical device structure */
151struct bcm5974 {
152 char phys[64];
153 struct usb_device *udev; /* usb device */
Dmitry Torokhov88da7652008-08-08 14:59:32 -0400154 struct usb_interface *intf; /* our interface */
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400155 struct input_dev *input; /* input dev */
156 struct bcm5974_config cfg; /* device configuration */
157 struct mutex pm_mutex; /* serialize access to open/suspend */
158 int opened; /* 1: opened, 0: closed */
159 struct urb *bt_urb; /* button usb request block */
160 struct bt_data *bt_data; /* button transferred data */
161 struct urb *tp_urb; /* trackpad usb request block */
Henrik Rydberg9894cf02009-04-27 11:52:42 -0700162 u8 *tp_data; /* trackpad transferred data */
Henrik Rydberg75e21e32008-09-04 22:28:23 -0400163 int fingers; /* number of fingers on trackpad */
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400164};
165
166/* logical dimensions */
167#define DIM_PRESSURE 256 /* maximum finger pressure */
168#define DIM_WIDTH 16 /* maximum finger width */
169#define DIM_X 1280 /* maximum trackpad x value */
170#define DIM_Y 800 /* maximum trackpad y value */
171
172/* logical signal quality */
173#define SN_PRESSURE 45 /* pressure signal-to-noise ratio */
174#define SN_WIDTH 100 /* width signal-to-noise ratio */
175#define SN_COORD 250 /* coordinate signal-to-noise ratio */
176
Henrik Rydberg75e21e32008-09-04 22:28:23 -0400177/* pressure thresholds */
178#define PRESSURE_LOW (2 * DIM_PRESSURE / SN_PRESSURE)
179#define PRESSURE_HIGH (3 * PRESSURE_LOW)
180
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400181/* device constants */
182static const struct bcm5974_config bcm5974_config_table[] = {
183 {
184 USB_DEVICE_ID_APPLE_WELLSPRING_ANSI,
185 USB_DEVICE_ID_APPLE_WELLSPRING_ISO,
186 USB_DEVICE_ID_APPLE_WELLSPRING_JIS,
187 0x84, sizeof(struct bt_data),
Henrik Rydberg9894cf02009-04-27 11:52:42 -0700188 0x81, TYPE1, FINGER_TYPE1, FINGER_TYPE1 + SIZEOF_ALL_FINGERS,
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400189 { DIM_PRESSURE, DIM_PRESSURE / SN_PRESSURE, 0, 256 },
190 { DIM_WIDTH, DIM_WIDTH / SN_WIDTH, 0, 2048 },
191 { DIM_X, DIM_X / SN_COORD, -4824, 5342 },
192 { DIM_Y, DIM_Y / SN_COORD, -172, 5820 }
193 },
194 {
195 USB_DEVICE_ID_APPLE_WELLSPRING2_ANSI,
196 USB_DEVICE_ID_APPLE_WELLSPRING2_ISO,
197 USB_DEVICE_ID_APPLE_WELLSPRING2_JIS,
198 0x84, sizeof(struct bt_data),
Henrik Rydberg9894cf02009-04-27 11:52:42 -0700199 0x81, TYPE1, FINGER_TYPE1, FINGER_TYPE1 + SIZEOF_ALL_FINGERS,
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400200 { DIM_PRESSURE, DIM_PRESSURE / SN_PRESSURE, 0, 256 },
201 { DIM_WIDTH, DIM_WIDTH / SN_WIDTH, 0, 2048 },
202 { DIM_X, DIM_X / SN_COORD, -4824, 4824 },
203 { DIM_Y, DIM_Y / SN_COORD, -172, 4290 }
204 },
205 {}
206};
207
208/* return the device-specific configuration by device */
209static const struct bcm5974_config *bcm5974_get_config(struct usb_device *udev)
210{
211 u16 id = le16_to_cpu(udev->descriptor.idProduct);
212 const struct bcm5974_config *cfg;
213
214 for (cfg = bcm5974_config_table; cfg->ansi; ++cfg)
215 if (cfg->ansi == id || cfg->iso == id || cfg->jis == id)
216 return cfg;
217
218 return bcm5974_config_table;
219}
220
221/* convert 16-bit little endian to signed integer */
222static inline int raw2int(__le16 x)
223{
224 return (signed short)le16_to_cpu(x);
225}
226
227/* scale device data to logical dimensions (asserts devmin < devmax) */
228static inline int int2scale(const struct bcm5974_param *p, int x)
229{
230 return x * p->dim / (p->devmax - p->devmin);
231}
232
233/* all logical value ranges are [0,dim). */
234static inline int int2bound(const struct bcm5974_param *p, int x)
235{
236 int s = int2scale(p, x);
237
238 return clamp_val(s, 0, p->dim - 1);
239}
240
241/* setup which logical events to report */
242static void setup_events_to_report(struct input_dev *input_dev,
243 const struct bcm5974_config *cfg)
244{
245 __set_bit(EV_ABS, input_dev->evbit);
246
247 input_set_abs_params(input_dev, ABS_PRESSURE,
248 0, cfg->p.dim, cfg->p.fuzz, 0);
249 input_set_abs_params(input_dev, ABS_TOOL_WIDTH,
250 0, cfg->w.dim, cfg->w.fuzz, 0);
251 input_set_abs_params(input_dev, ABS_X,
252 0, cfg->x.dim, cfg->x.fuzz, 0);
253 input_set_abs_params(input_dev, ABS_Y,
254 0, cfg->y.dim, cfg->y.fuzz, 0);
255
256 __set_bit(EV_KEY, input_dev->evbit);
Henrik Rydberga6821f32008-09-04 22:28:31 -0400257 __set_bit(BTN_TOUCH, input_dev->keybit);
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400258 __set_bit(BTN_TOOL_FINGER, input_dev->keybit);
259 __set_bit(BTN_TOOL_DOUBLETAP, input_dev->keybit);
260 __set_bit(BTN_TOOL_TRIPLETAP, input_dev->keybit);
Henrik Rydberg6916d972009-04-27 11:52:43 -0700261 __set_bit(BTN_TOOL_QUADTAP, input_dev->keybit);
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400262 __set_bit(BTN_LEFT, input_dev->keybit);
263}
264
265/* report button data as logical button state */
266static int report_bt_state(struct bcm5974 *dev, int size)
267{
268 if (size != sizeof(struct bt_data))
269 return -EIO;
270
271 input_report_key(dev->input, BTN_LEFT, dev->bt_data->button);
272 input_sync(dev->input);
273
274 return 0;
275}
276
277/* report trackpad data as logical trackpad state */
278static int report_tp_state(struct bcm5974 *dev, int size)
279{
280 const struct bcm5974_config *c = &dev->cfg;
Henrik Rydberg9894cf02009-04-27 11:52:42 -0700281 const struct tp_finger *f;
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400282 struct input_dev *input = dev->input;
Henrik Rydberg9894cf02009-04-27 11:52:42 -0700283 int raw_p, raw_w, raw_x, raw_y, raw_n;
Henrik Rydberg75e21e32008-09-04 22:28:23 -0400284 int ptest = 0, origin = 0, nmin = 0, nmax = 0;
285 int abs_p = 0, abs_w = 0, abs_x = 0, abs_y = 0;
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400286
Henrik Rydberg9894cf02009-04-27 11:52:42 -0700287 if (size < c->tp_offset || (size - c->tp_offset) % SIZEOF_FINGER != 0)
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400288 return -EIO;
289
Henrik Rydberg9894cf02009-04-27 11:52:42 -0700290 /* finger data, le16-aligned */
291 f = (const struct tp_finger *)(dev->tp_data + c->tp_offset);
292 raw_n = (size - c->tp_offset) / SIZEOF_FINGER;
293
Henrik Rydberg75e21e32008-09-04 22:28:23 -0400294 /* always track the first finger; when detached, start over */
Henrik Rydberg9894cf02009-04-27 11:52:42 -0700295 if (raw_n) {
Henrik Rydberg75e21e32008-09-04 22:28:23 -0400296 raw_p = raw2int(f->force_major);
297 raw_w = raw2int(f->size_major);
298 raw_x = raw2int(f->abs_x);
299 raw_y = raw2int(f->abs_y);
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400300
301 dprintk(9,
Henrik Rydberg75e21e32008-09-04 22:28:23 -0400302 "bcm5974: raw: p: %+05d w: %+05d x: %+05d y: %+05d\n",
303 raw_p, raw_w, raw_x, raw_y);
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400304
Henrik Rydberg75e21e32008-09-04 22:28:23 -0400305 ptest = int2bound(&c->p, raw_p);
306 origin = raw2int(f->origin);
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400307 }
308
Henrik Rydberg75e21e32008-09-04 22:28:23 -0400309 /* while tracking finger still valid, count all fingers */
310 if (ptest > PRESSURE_LOW && origin) {
311 abs_p = ptest;
312 abs_w = int2bound(&c->w, raw_w);
313 abs_x = int2bound(&c->x, raw_x - c->x.devmin);
314 abs_y = int2bound(&c->y, c->y.devmax - raw_y);
Henrik Rydberg9894cf02009-04-27 11:52:42 -0700315 while (raw_n--) {
Henrik Rydberg75e21e32008-09-04 22:28:23 -0400316 ptest = int2bound(&c->p, raw2int(f->force_major));
317 if (ptest > PRESSURE_LOW)
318 nmax++;
319 if (ptest > PRESSURE_HIGH)
320 nmin++;
Henrik Rydberg9894cf02009-04-27 11:52:42 -0700321 f++;
Henrik Rydberg75e21e32008-09-04 22:28:23 -0400322 }
323 }
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400324
Henrik Rydberg75e21e32008-09-04 22:28:23 -0400325 if (dev->fingers < nmin)
326 dev->fingers = nmin;
327 if (dev->fingers > nmax)
328 dev->fingers = nmax;
329
Henrik Rydberga6821f32008-09-04 22:28:31 -0400330 input_report_key(input, BTN_TOUCH, dev->fingers > 0);
Henrik Rydberg75e21e32008-09-04 22:28:23 -0400331 input_report_key(input, BTN_TOOL_FINGER, dev->fingers == 1);
332 input_report_key(input, BTN_TOOL_DOUBLETAP, dev->fingers == 2);
Henrik Rydberg6916d972009-04-27 11:52:43 -0700333 input_report_key(input, BTN_TOOL_TRIPLETAP, dev->fingers == 3);
334 input_report_key(input, BTN_TOOL_QUADTAP, dev->fingers > 3);
Henrik Rydberg75e21e32008-09-04 22:28:23 -0400335
336 input_report_abs(input, ABS_PRESSURE, abs_p);
337 input_report_abs(input, ABS_TOOL_WIDTH, abs_w);
338
339 if (abs_p) {
340 input_report_abs(input, ABS_X, abs_x);
341 input_report_abs(input, ABS_Y, abs_y);
342
343 dprintk(8,
344 "bcm5974: abs: p: %+05d w: %+05d x: %+05d y: %+05d "
345 "nmin: %d nmax: %d n: %d\n",
346 abs_p, abs_w, abs_x, abs_y, nmin, nmax, dev->fingers);
347
348 }
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400349
350 input_sync(input);
351
352 return 0;
353}
354
355/* Wellspring initialization constants */
356#define BCM5974_WELLSPRING_MODE_READ_REQUEST_ID 1
357#define BCM5974_WELLSPRING_MODE_WRITE_REQUEST_ID 9
358#define BCM5974_WELLSPRING_MODE_REQUEST_VALUE 0x300
359#define BCM5974_WELLSPRING_MODE_REQUEST_INDEX 0
360#define BCM5974_WELLSPRING_MODE_VENDOR_VALUE 0x01
Henrik Rydbergcd72ad32008-09-14 11:52:44 -0400361#define BCM5974_WELLSPRING_MODE_NORMAL_VALUE 0x08
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400362
Henrik Rydbergcd72ad32008-09-14 11:52:44 -0400363static int bcm5974_wellspring_mode(struct bcm5974 *dev, bool on)
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400364{
365 char *data = kmalloc(8, GFP_KERNEL);
366 int retval = 0, size;
367
368 if (!data) {
369 err("bcm5974: out of memory");
370 retval = -ENOMEM;
371 goto out;
372 }
373
374 /* read configuration */
375 size = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
376 BCM5974_WELLSPRING_MODE_READ_REQUEST_ID,
377 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
378 BCM5974_WELLSPRING_MODE_REQUEST_VALUE,
379 BCM5974_WELLSPRING_MODE_REQUEST_INDEX, data, 8, 5000);
380
381 if (size != 8) {
382 err("bcm5974: could not read from device");
383 retval = -EIO;
384 goto out;
385 }
386
387 /* apply the mode switch */
Henrik Rydbergcd72ad32008-09-14 11:52:44 -0400388 data[0] = on ?
389 BCM5974_WELLSPRING_MODE_VENDOR_VALUE :
390 BCM5974_WELLSPRING_MODE_NORMAL_VALUE;
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400391
392 /* write configuration */
393 size = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
394 BCM5974_WELLSPRING_MODE_WRITE_REQUEST_ID,
395 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
396 BCM5974_WELLSPRING_MODE_REQUEST_VALUE,
397 BCM5974_WELLSPRING_MODE_REQUEST_INDEX, data, 8, 5000);
398
399 if (size != 8) {
400 err("bcm5974: could not write to device");
401 retval = -EIO;
402 goto out;
403 }
404
Henrik Rydbergcd72ad32008-09-14 11:52:44 -0400405 dprintk(2, "bcm5974: switched to %s mode.\n",
406 on ? "wellspring" : "normal");
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400407
408 out:
409 kfree(data);
410 return retval;
411}
412
413static void bcm5974_irq_button(struct urb *urb)
414{
415 struct bcm5974 *dev = urb->context;
416 int error;
417
418 switch (urb->status) {
419 case 0:
420 break;
421 case -EOVERFLOW:
422 case -ECONNRESET:
423 case -ENOENT:
424 case -ESHUTDOWN:
425 dbg("bcm5974: button urb shutting down: %d", urb->status);
426 return;
427 default:
428 dbg("bcm5974: button urb status: %d", urb->status);
429 goto exit;
430 }
431
432 if (report_bt_state(dev, dev->bt_urb->actual_length))
433 dprintk(1, "bcm5974: bad button package, length: %d\n",
434 dev->bt_urb->actual_length);
435
436exit:
437 error = usb_submit_urb(dev->bt_urb, GFP_ATOMIC);
438 if (error)
439 err("bcm5974: button urb failed: %d", error);
440}
441
442static void bcm5974_irq_trackpad(struct urb *urb)
443{
444 struct bcm5974 *dev = urb->context;
445 int error;
446
447 switch (urb->status) {
448 case 0:
449 break;
450 case -EOVERFLOW:
451 case -ECONNRESET:
452 case -ENOENT:
453 case -ESHUTDOWN:
454 dbg("bcm5974: trackpad urb shutting down: %d", urb->status);
455 return;
456 default:
457 dbg("bcm5974: trackpad urb status: %d", urb->status);
458 goto exit;
459 }
460
461 /* control response ignored */
462 if (dev->tp_urb->actual_length == 2)
463 goto exit;
464
465 if (report_tp_state(dev, dev->tp_urb->actual_length))
466 dprintk(1, "bcm5974: bad trackpad package, length: %d\n",
467 dev->tp_urb->actual_length);
468
469exit:
470 error = usb_submit_urb(dev->tp_urb, GFP_ATOMIC);
471 if (error)
472 err("bcm5974: trackpad urb failed: %d", error);
473}
474
475/*
476 * The Wellspring trackpad, like many recent Apple trackpads, share
477 * the usb device with the keyboard. Since keyboards are usually
478 * handled by the HID system, the device ends up being handled by two
479 * modules. Setting up the device therefore becomes slightly
480 * complicated. To enable multitouch features, a mode switch is
481 * required, which is usually applied via the control interface of the
482 * device. It can be argued where this switch should take place. In
483 * some drivers, like appletouch, the switch is made during
484 * probe. However, the hid module may also alter the state of the
485 * device, resulting in trackpad malfunction under certain
486 * circumstances. To get around this problem, there is at least one
487 * example that utilizes the USB_QUIRK_RESET_RESUME quirk in order to
488 * recieve a reset_resume request rather than the normal resume.
489 * Since the implementation of reset_resume is equal to mode switch
490 * plus start_traffic, it seems easier to always do the switch when
491 * starting traffic on the device.
492 */
493static int bcm5974_start_traffic(struct bcm5974 *dev)
494{
Henrik Rydbergcd72ad32008-09-14 11:52:44 -0400495 if (bcm5974_wellspring_mode(dev, true)) {
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400496 dprintk(1, "bcm5974: mode switch failed\n");
497 goto error;
498 }
499
500 if (usb_submit_urb(dev->bt_urb, GFP_KERNEL))
501 goto error;
502
503 if (usb_submit_urb(dev->tp_urb, GFP_KERNEL))
504 goto err_kill_bt;
505
506 return 0;
507
508err_kill_bt:
509 usb_kill_urb(dev->bt_urb);
510error:
511 return -EIO;
512}
513
514static void bcm5974_pause_traffic(struct bcm5974 *dev)
515{
516 usb_kill_urb(dev->tp_urb);
517 usb_kill_urb(dev->bt_urb);
Henrik Rydbergcd72ad32008-09-14 11:52:44 -0400518 bcm5974_wellspring_mode(dev, false);
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400519}
520
521/*
522 * The code below implements open/close and manual suspend/resume.
523 * All functions may be called in random order.
524 *
525 * Opening a suspended device fails with EACCES - permission denied.
526 *
527 * Failing a resume leaves the device resumed but closed.
528 */
529static int bcm5974_open(struct input_dev *input)
530{
531 struct bcm5974 *dev = input_get_drvdata(input);
532 int error;
533
Dmitry Torokhov88da7652008-08-08 14:59:32 -0400534 error = usb_autopm_get_interface(dev->intf);
535 if (error)
536 return error;
537
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400538 mutex_lock(&dev->pm_mutex);
539
540 error = bcm5974_start_traffic(dev);
541 if (!error)
542 dev->opened = 1;
543
544 mutex_unlock(&dev->pm_mutex);
545
Dmitry Torokhov88da7652008-08-08 14:59:32 -0400546 if (error)
547 usb_autopm_put_interface(dev->intf);
548
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400549 return error;
550}
551
552static void bcm5974_close(struct input_dev *input)
553{
554 struct bcm5974 *dev = input_get_drvdata(input);
555
556 mutex_lock(&dev->pm_mutex);
557
558 bcm5974_pause_traffic(dev);
559 dev->opened = 0;
560
561 mutex_unlock(&dev->pm_mutex);
Dmitry Torokhov88da7652008-08-08 14:59:32 -0400562
563 usb_autopm_put_interface(dev->intf);
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400564}
565
566static int bcm5974_suspend(struct usb_interface *iface, pm_message_t message)
567{
568 struct bcm5974 *dev = usb_get_intfdata(iface);
569
570 mutex_lock(&dev->pm_mutex);
571
572 if (dev->opened)
573 bcm5974_pause_traffic(dev);
574
575 mutex_unlock(&dev->pm_mutex);
576
577 return 0;
578}
579
580static int bcm5974_resume(struct usb_interface *iface)
581{
582 struct bcm5974 *dev = usb_get_intfdata(iface);
583 int error = 0;
584
585 mutex_lock(&dev->pm_mutex);
586
587 if (dev->opened)
588 error = bcm5974_start_traffic(dev);
589
590 mutex_unlock(&dev->pm_mutex);
591
592 return error;
593}
594
595static int bcm5974_probe(struct usb_interface *iface,
596 const struct usb_device_id *id)
597{
598 struct usb_device *udev = interface_to_usbdev(iface);
599 const struct bcm5974_config *cfg;
600 struct bcm5974 *dev;
601 struct input_dev *input_dev;
602 int error = -ENOMEM;
603
604 /* find the product index */
605 cfg = bcm5974_get_config(udev);
606
607 /* allocate memory for our device state and initialize it */
608 dev = kzalloc(sizeof(struct bcm5974), GFP_KERNEL);
609 input_dev = input_allocate_device();
610 if (!dev || !input_dev) {
611 err("bcm5974: out of memory");
612 goto err_free_devs;
613 }
614
615 dev->udev = udev;
Dmitry Torokhov88da7652008-08-08 14:59:32 -0400616 dev->intf = iface;
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400617 dev->input = input_dev;
618 dev->cfg = *cfg;
619 mutex_init(&dev->pm_mutex);
620
621 /* setup urbs */
622 dev->bt_urb = usb_alloc_urb(0, GFP_KERNEL);
623 if (!dev->bt_urb)
624 goto err_free_devs;
625
626 dev->tp_urb = usb_alloc_urb(0, GFP_KERNEL);
627 if (!dev->tp_urb)
628 goto err_free_bt_urb;
629
630 dev->bt_data = usb_buffer_alloc(dev->udev,
631 dev->cfg.bt_datalen, GFP_KERNEL,
632 &dev->bt_urb->transfer_dma);
633 if (!dev->bt_data)
634 goto err_free_urb;
635
636 dev->tp_data = usb_buffer_alloc(dev->udev,
637 dev->cfg.tp_datalen, GFP_KERNEL,
638 &dev->tp_urb->transfer_dma);
639 if (!dev->tp_data)
640 goto err_free_bt_buffer;
641
642 usb_fill_int_urb(dev->bt_urb, udev,
643 usb_rcvintpipe(udev, cfg->bt_ep),
644 dev->bt_data, dev->cfg.bt_datalen,
645 bcm5974_irq_button, dev, 1);
646
647 usb_fill_int_urb(dev->tp_urb, udev,
648 usb_rcvintpipe(udev, cfg->tp_ep),
649 dev->tp_data, dev->cfg.tp_datalen,
650 bcm5974_irq_trackpad, dev, 1);
651
652 /* create bcm5974 device */
653 usb_make_path(udev, dev->phys, sizeof(dev->phys));
654 strlcat(dev->phys, "/input0", sizeof(dev->phys));
655
656 input_dev->name = "bcm5974";
657 input_dev->phys = dev->phys;
658 usb_to_input_id(dev->udev, &input_dev->id);
659 input_dev->dev.parent = &iface->dev;
660
661 input_set_drvdata(input_dev, dev);
662
663 input_dev->open = bcm5974_open;
664 input_dev->close = bcm5974_close;
665
666 setup_events_to_report(input_dev, cfg);
667
668 error = input_register_device(dev->input);
669 if (error)
670 goto err_free_buffer;
671
672 /* save our data pointer in this interface device */
673 usb_set_intfdata(iface, dev);
674
675 return 0;
676
677err_free_buffer:
678 usb_buffer_free(dev->udev, dev->cfg.tp_datalen,
679 dev->tp_data, dev->tp_urb->transfer_dma);
680err_free_bt_buffer:
681 usb_buffer_free(dev->udev, dev->cfg.bt_datalen,
682 dev->bt_data, dev->bt_urb->transfer_dma);
683err_free_urb:
684 usb_free_urb(dev->tp_urb);
685err_free_bt_urb:
686 usb_free_urb(dev->bt_urb);
687err_free_devs:
688 usb_set_intfdata(iface, NULL);
689 input_free_device(input_dev);
690 kfree(dev);
691 return error;
692}
693
694static void bcm5974_disconnect(struct usb_interface *iface)
695{
696 struct bcm5974 *dev = usb_get_intfdata(iface);
697
698 usb_set_intfdata(iface, NULL);
699
700 input_unregister_device(dev->input);
701 usb_buffer_free(dev->udev, dev->cfg.tp_datalen,
702 dev->tp_data, dev->tp_urb->transfer_dma);
703 usb_buffer_free(dev->udev, dev->cfg.bt_datalen,
704 dev->bt_data, dev->bt_urb->transfer_dma);
705 usb_free_urb(dev->tp_urb);
706 usb_free_urb(dev->bt_urb);
707 kfree(dev);
708}
709
710static struct usb_driver bcm5974_driver = {
711 .name = "bcm5974",
712 .probe = bcm5974_probe,
713 .disconnect = bcm5974_disconnect,
714 .suspend = bcm5974_suspend,
715 .resume = bcm5974_resume,
716 .reset_resume = bcm5974_resume,
717 .id_table = bcm5974_table,
Dmitry Torokhov88da7652008-08-08 14:59:32 -0400718 .supports_autosuspend = 1,
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400719};
720
721static int __init bcm5974_init(void)
722{
723 return usb_register(&bcm5974_driver);
724}
725
726static void __exit bcm5974_exit(void)
727{
728 usb_deregister(&bcm5974_driver);
729}
730
731module_init(bcm5974_init);
732module_exit(bcm5974_exit);
733