blob: bda873393b0d8ddd7f4acf36e5a9b6a1fe85c813 [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);
261 __set_bit(BTN_LEFT, input_dev->keybit);
262}
263
264/* report button data as logical button state */
265static int report_bt_state(struct bcm5974 *dev, int size)
266{
267 if (size != sizeof(struct bt_data))
268 return -EIO;
269
270 input_report_key(dev->input, BTN_LEFT, dev->bt_data->button);
271 input_sync(dev->input);
272
273 return 0;
274}
275
276/* report trackpad data as logical trackpad state */
277static int report_tp_state(struct bcm5974 *dev, int size)
278{
279 const struct bcm5974_config *c = &dev->cfg;
Henrik Rydberg9894cf02009-04-27 11:52:42 -0700280 const struct tp_finger *f;
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400281 struct input_dev *input = dev->input;
Henrik Rydberg9894cf02009-04-27 11:52:42 -0700282 int raw_p, raw_w, raw_x, raw_y, raw_n;
Henrik Rydberg75e21e32008-09-04 22:28:23 -0400283 int ptest = 0, origin = 0, nmin = 0, nmax = 0;
284 int abs_p = 0, abs_w = 0, abs_x = 0, abs_y = 0;
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400285
Henrik Rydberg9894cf02009-04-27 11:52:42 -0700286 if (size < c->tp_offset || (size - c->tp_offset) % SIZEOF_FINGER != 0)
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400287 return -EIO;
288
Henrik Rydberg9894cf02009-04-27 11:52:42 -0700289 /* finger data, le16-aligned */
290 f = (const struct tp_finger *)(dev->tp_data + c->tp_offset);
291 raw_n = (size - c->tp_offset) / SIZEOF_FINGER;
292
Henrik Rydberg75e21e32008-09-04 22:28:23 -0400293 /* always track the first finger; when detached, start over */
Henrik Rydberg9894cf02009-04-27 11:52:42 -0700294 if (raw_n) {
Henrik Rydberg75e21e32008-09-04 22:28:23 -0400295 raw_p = raw2int(f->force_major);
296 raw_w = raw2int(f->size_major);
297 raw_x = raw2int(f->abs_x);
298 raw_y = raw2int(f->abs_y);
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400299
300 dprintk(9,
Henrik Rydberg75e21e32008-09-04 22:28:23 -0400301 "bcm5974: raw: p: %+05d w: %+05d x: %+05d y: %+05d\n",
302 raw_p, raw_w, raw_x, raw_y);
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400303
Henrik Rydberg75e21e32008-09-04 22:28:23 -0400304 ptest = int2bound(&c->p, raw_p);
305 origin = raw2int(f->origin);
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400306 }
307
Henrik Rydberg75e21e32008-09-04 22:28:23 -0400308 /* while tracking finger still valid, count all fingers */
309 if (ptest > PRESSURE_LOW && origin) {
310 abs_p = ptest;
311 abs_w = int2bound(&c->w, raw_w);
312 abs_x = int2bound(&c->x, raw_x - c->x.devmin);
313 abs_y = int2bound(&c->y, c->y.devmax - raw_y);
Henrik Rydberg9894cf02009-04-27 11:52:42 -0700314 while (raw_n--) {
Henrik Rydberg75e21e32008-09-04 22:28:23 -0400315 ptest = int2bound(&c->p, raw2int(f->force_major));
316 if (ptest > PRESSURE_LOW)
317 nmax++;
318 if (ptest > PRESSURE_HIGH)
319 nmin++;
Henrik Rydberg9894cf02009-04-27 11:52:42 -0700320 f++;
Henrik Rydberg75e21e32008-09-04 22:28:23 -0400321 }
322 }
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400323
Henrik Rydberg75e21e32008-09-04 22:28:23 -0400324 if (dev->fingers < nmin)
325 dev->fingers = nmin;
326 if (dev->fingers > nmax)
327 dev->fingers = nmax;
328
Henrik Rydberga6821f32008-09-04 22:28:31 -0400329 input_report_key(input, BTN_TOUCH, dev->fingers > 0);
Henrik Rydberg75e21e32008-09-04 22:28:23 -0400330 input_report_key(input, BTN_TOOL_FINGER, dev->fingers == 1);
331 input_report_key(input, BTN_TOOL_DOUBLETAP, dev->fingers == 2);
332 input_report_key(input, BTN_TOOL_TRIPLETAP, dev->fingers > 2);
333
334 input_report_abs(input, ABS_PRESSURE, abs_p);
335 input_report_abs(input, ABS_TOOL_WIDTH, abs_w);
336
337 if (abs_p) {
338 input_report_abs(input, ABS_X, abs_x);
339 input_report_abs(input, ABS_Y, abs_y);
340
341 dprintk(8,
342 "bcm5974: abs: p: %+05d w: %+05d x: %+05d y: %+05d "
343 "nmin: %d nmax: %d n: %d\n",
344 abs_p, abs_w, abs_x, abs_y, nmin, nmax, dev->fingers);
345
346 }
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400347
348 input_sync(input);
349
350 return 0;
351}
352
353/* Wellspring initialization constants */
354#define BCM5974_WELLSPRING_MODE_READ_REQUEST_ID 1
355#define BCM5974_WELLSPRING_MODE_WRITE_REQUEST_ID 9
356#define BCM5974_WELLSPRING_MODE_REQUEST_VALUE 0x300
357#define BCM5974_WELLSPRING_MODE_REQUEST_INDEX 0
358#define BCM5974_WELLSPRING_MODE_VENDOR_VALUE 0x01
Henrik Rydbergcd72ad32008-09-14 11:52:44 -0400359#define BCM5974_WELLSPRING_MODE_NORMAL_VALUE 0x08
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400360
Henrik Rydbergcd72ad32008-09-14 11:52:44 -0400361static int bcm5974_wellspring_mode(struct bcm5974 *dev, bool on)
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400362{
363 char *data = kmalloc(8, GFP_KERNEL);
364 int retval = 0, size;
365
366 if (!data) {
367 err("bcm5974: out of memory");
368 retval = -ENOMEM;
369 goto out;
370 }
371
372 /* read configuration */
373 size = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
374 BCM5974_WELLSPRING_MODE_READ_REQUEST_ID,
375 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
376 BCM5974_WELLSPRING_MODE_REQUEST_VALUE,
377 BCM5974_WELLSPRING_MODE_REQUEST_INDEX, data, 8, 5000);
378
379 if (size != 8) {
380 err("bcm5974: could not read from device");
381 retval = -EIO;
382 goto out;
383 }
384
385 /* apply the mode switch */
Henrik Rydbergcd72ad32008-09-14 11:52:44 -0400386 data[0] = on ?
387 BCM5974_WELLSPRING_MODE_VENDOR_VALUE :
388 BCM5974_WELLSPRING_MODE_NORMAL_VALUE;
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400389
390 /* write configuration */
391 size = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
392 BCM5974_WELLSPRING_MODE_WRITE_REQUEST_ID,
393 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
394 BCM5974_WELLSPRING_MODE_REQUEST_VALUE,
395 BCM5974_WELLSPRING_MODE_REQUEST_INDEX, data, 8, 5000);
396
397 if (size != 8) {
398 err("bcm5974: could not write to device");
399 retval = -EIO;
400 goto out;
401 }
402
Henrik Rydbergcd72ad32008-09-14 11:52:44 -0400403 dprintk(2, "bcm5974: switched to %s mode.\n",
404 on ? "wellspring" : "normal");
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400405
406 out:
407 kfree(data);
408 return retval;
409}
410
411static void bcm5974_irq_button(struct urb *urb)
412{
413 struct bcm5974 *dev = urb->context;
414 int error;
415
416 switch (urb->status) {
417 case 0:
418 break;
419 case -EOVERFLOW:
420 case -ECONNRESET:
421 case -ENOENT:
422 case -ESHUTDOWN:
423 dbg("bcm5974: button urb shutting down: %d", urb->status);
424 return;
425 default:
426 dbg("bcm5974: button urb status: %d", urb->status);
427 goto exit;
428 }
429
430 if (report_bt_state(dev, dev->bt_urb->actual_length))
431 dprintk(1, "bcm5974: bad button package, length: %d\n",
432 dev->bt_urb->actual_length);
433
434exit:
435 error = usb_submit_urb(dev->bt_urb, GFP_ATOMIC);
436 if (error)
437 err("bcm5974: button urb failed: %d", error);
438}
439
440static void bcm5974_irq_trackpad(struct urb *urb)
441{
442 struct bcm5974 *dev = urb->context;
443 int error;
444
445 switch (urb->status) {
446 case 0:
447 break;
448 case -EOVERFLOW:
449 case -ECONNRESET:
450 case -ENOENT:
451 case -ESHUTDOWN:
452 dbg("bcm5974: trackpad urb shutting down: %d", urb->status);
453 return;
454 default:
455 dbg("bcm5974: trackpad urb status: %d", urb->status);
456 goto exit;
457 }
458
459 /* control response ignored */
460 if (dev->tp_urb->actual_length == 2)
461 goto exit;
462
463 if (report_tp_state(dev, dev->tp_urb->actual_length))
464 dprintk(1, "bcm5974: bad trackpad package, length: %d\n",
465 dev->tp_urb->actual_length);
466
467exit:
468 error = usb_submit_urb(dev->tp_urb, GFP_ATOMIC);
469 if (error)
470 err("bcm5974: trackpad urb failed: %d", error);
471}
472
473/*
474 * The Wellspring trackpad, like many recent Apple trackpads, share
475 * the usb device with the keyboard. Since keyboards are usually
476 * handled by the HID system, the device ends up being handled by two
477 * modules. Setting up the device therefore becomes slightly
478 * complicated. To enable multitouch features, a mode switch is
479 * required, which is usually applied via the control interface of the
480 * device. It can be argued where this switch should take place. In
481 * some drivers, like appletouch, the switch is made during
482 * probe. However, the hid module may also alter the state of the
483 * device, resulting in trackpad malfunction under certain
484 * circumstances. To get around this problem, there is at least one
485 * example that utilizes the USB_QUIRK_RESET_RESUME quirk in order to
486 * recieve a reset_resume request rather than the normal resume.
487 * Since the implementation of reset_resume is equal to mode switch
488 * plus start_traffic, it seems easier to always do the switch when
489 * starting traffic on the device.
490 */
491static int bcm5974_start_traffic(struct bcm5974 *dev)
492{
Henrik Rydbergcd72ad32008-09-14 11:52:44 -0400493 if (bcm5974_wellspring_mode(dev, true)) {
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400494 dprintk(1, "bcm5974: mode switch failed\n");
495 goto error;
496 }
497
498 if (usb_submit_urb(dev->bt_urb, GFP_KERNEL))
499 goto error;
500
501 if (usb_submit_urb(dev->tp_urb, GFP_KERNEL))
502 goto err_kill_bt;
503
504 return 0;
505
506err_kill_bt:
507 usb_kill_urb(dev->bt_urb);
508error:
509 return -EIO;
510}
511
512static void bcm5974_pause_traffic(struct bcm5974 *dev)
513{
514 usb_kill_urb(dev->tp_urb);
515 usb_kill_urb(dev->bt_urb);
Henrik Rydbergcd72ad32008-09-14 11:52:44 -0400516 bcm5974_wellspring_mode(dev, false);
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400517}
518
519/*
520 * The code below implements open/close and manual suspend/resume.
521 * All functions may be called in random order.
522 *
523 * Opening a suspended device fails with EACCES - permission denied.
524 *
525 * Failing a resume leaves the device resumed but closed.
526 */
527static int bcm5974_open(struct input_dev *input)
528{
529 struct bcm5974 *dev = input_get_drvdata(input);
530 int error;
531
Dmitry Torokhov88da7652008-08-08 14:59:32 -0400532 error = usb_autopm_get_interface(dev->intf);
533 if (error)
534 return error;
535
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400536 mutex_lock(&dev->pm_mutex);
537
538 error = bcm5974_start_traffic(dev);
539 if (!error)
540 dev->opened = 1;
541
542 mutex_unlock(&dev->pm_mutex);
543
Dmitry Torokhov88da7652008-08-08 14:59:32 -0400544 if (error)
545 usb_autopm_put_interface(dev->intf);
546
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400547 return error;
548}
549
550static void bcm5974_close(struct input_dev *input)
551{
552 struct bcm5974 *dev = input_get_drvdata(input);
553
554 mutex_lock(&dev->pm_mutex);
555
556 bcm5974_pause_traffic(dev);
557 dev->opened = 0;
558
559 mutex_unlock(&dev->pm_mutex);
Dmitry Torokhov88da7652008-08-08 14:59:32 -0400560
561 usb_autopm_put_interface(dev->intf);
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400562}
563
564static int bcm5974_suspend(struct usb_interface *iface, pm_message_t message)
565{
566 struct bcm5974 *dev = usb_get_intfdata(iface);
567
568 mutex_lock(&dev->pm_mutex);
569
570 if (dev->opened)
571 bcm5974_pause_traffic(dev);
572
573 mutex_unlock(&dev->pm_mutex);
574
575 return 0;
576}
577
578static int bcm5974_resume(struct usb_interface *iface)
579{
580 struct bcm5974 *dev = usb_get_intfdata(iface);
581 int error = 0;
582
583 mutex_lock(&dev->pm_mutex);
584
585 if (dev->opened)
586 error = bcm5974_start_traffic(dev);
587
588 mutex_unlock(&dev->pm_mutex);
589
590 return error;
591}
592
593static int bcm5974_probe(struct usb_interface *iface,
594 const struct usb_device_id *id)
595{
596 struct usb_device *udev = interface_to_usbdev(iface);
597 const struct bcm5974_config *cfg;
598 struct bcm5974 *dev;
599 struct input_dev *input_dev;
600 int error = -ENOMEM;
601
602 /* find the product index */
603 cfg = bcm5974_get_config(udev);
604
605 /* allocate memory for our device state and initialize it */
606 dev = kzalloc(sizeof(struct bcm5974), GFP_KERNEL);
607 input_dev = input_allocate_device();
608 if (!dev || !input_dev) {
609 err("bcm5974: out of memory");
610 goto err_free_devs;
611 }
612
613 dev->udev = udev;
Dmitry Torokhov88da7652008-08-08 14:59:32 -0400614 dev->intf = iface;
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400615 dev->input = input_dev;
616 dev->cfg = *cfg;
617 mutex_init(&dev->pm_mutex);
618
619 /* setup urbs */
620 dev->bt_urb = usb_alloc_urb(0, GFP_KERNEL);
621 if (!dev->bt_urb)
622 goto err_free_devs;
623
624 dev->tp_urb = usb_alloc_urb(0, GFP_KERNEL);
625 if (!dev->tp_urb)
626 goto err_free_bt_urb;
627
628 dev->bt_data = usb_buffer_alloc(dev->udev,
629 dev->cfg.bt_datalen, GFP_KERNEL,
630 &dev->bt_urb->transfer_dma);
631 if (!dev->bt_data)
632 goto err_free_urb;
633
634 dev->tp_data = usb_buffer_alloc(dev->udev,
635 dev->cfg.tp_datalen, GFP_KERNEL,
636 &dev->tp_urb->transfer_dma);
637 if (!dev->tp_data)
638 goto err_free_bt_buffer;
639
640 usb_fill_int_urb(dev->bt_urb, udev,
641 usb_rcvintpipe(udev, cfg->bt_ep),
642 dev->bt_data, dev->cfg.bt_datalen,
643 bcm5974_irq_button, dev, 1);
644
645 usb_fill_int_urb(dev->tp_urb, udev,
646 usb_rcvintpipe(udev, cfg->tp_ep),
647 dev->tp_data, dev->cfg.tp_datalen,
648 bcm5974_irq_trackpad, dev, 1);
649
650 /* create bcm5974 device */
651 usb_make_path(udev, dev->phys, sizeof(dev->phys));
652 strlcat(dev->phys, "/input0", sizeof(dev->phys));
653
654 input_dev->name = "bcm5974";
655 input_dev->phys = dev->phys;
656 usb_to_input_id(dev->udev, &input_dev->id);
657 input_dev->dev.parent = &iface->dev;
658
659 input_set_drvdata(input_dev, dev);
660
661 input_dev->open = bcm5974_open;
662 input_dev->close = bcm5974_close;
663
664 setup_events_to_report(input_dev, cfg);
665
666 error = input_register_device(dev->input);
667 if (error)
668 goto err_free_buffer;
669
670 /* save our data pointer in this interface device */
671 usb_set_intfdata(iface, dev);
672
673 return 0;
674
675err_free_buffer:
676 usb_buffer_free(dev->udev, dev->cfg.tp_datalen,
677 dev->tp_data, dev->tp_urb->transfer_dma);
678err_free_bt_buffer:
679 usb_buffer_free(dev->udev, dev->cfg.bt_datalen,
680 dev->bt_data, dev->bt_urb->transfer_dma);
681err_free_urb:
682 usb_free_urb(dev->tp_urb);
683err_free_bt_urb:
684 usb_free_urb(dev->bt_urb);
685err_free_devs:
686 usb_set_intfdata(iface, NULL);
687 input_free_device(input_dev);
688 kfree(dev);
689 return error;
690}
691
692static void bcm5974_disconnect(struct usb_interface *iface)
693{
694 struct bcm5974 *dev = usb_get_intfdata(iface);
695
696 usb_set_intfdata(iface, NULL);
697
698 input_unregister_device(dev->input);
699 usb_buffer_free(dev->udev, dev->cfg.tp_datalen,
700 dev->tp_data, dev->tp_urb->transfer_dma);
701 usb_buffer_free(dev->udev, dev->cfg.bt_datalen,
702 dev->bt_data, dev->bt_urb->transfer_dma);
703 usb_free_urb(dev->tp_urb);
704 usb_free_urb(dev->bt_urb);
705 kfree(dev);
706}
707
708static struct usb_driver bcm5974_driver = {
709 .name = "bcm5974",
710 .probe = bcm5974_probe,
711 .disconnect = bcm5974_disconnect,
712 .suspend = bcm5974_suspend,
713 .resume = bcm5974_resume,
714 .reset_resume = bcm5974_resume,
715 .id_table = bcm5974_table,
Dmitry Torokhov88da7652008-08-08 14:59:32 -0400716 .supports_autosuspend = 1,
Henrik Rydbergf89bd952008-08-08 14:59:30 -0400717};
718
719static int __init bcm5974_init(void)
720{
721 return usb_register(&bcm5974_driver);
722}
723
724static void __exit bcm5974_exit(void)
725{
726 usb_deregister(&bcm5974_driver);
727}
728
729module_init(bcm5974_init);
730module_exit(bcm5974_exit);
731