On Tue, Nov 06, 2007 at 02:33:53AM -0800, akpm@linux-foundation.org wrote:
[mmotm.git] / drivers / input / mouse / bcm5974.c
blob9c360acccad7525db2bd331b71db160585dfb9d1
1 /*
2 * Apple USB BCM5974 (Macbook Air and Penryn Macbook Pro) multitouch driver
4 * Copyright (C) 2008 Henrik Rydberg (rydberg@euromail.se)
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)
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)
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.
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.
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.
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>
44 #define USB_VENDOR_ID_APPLE 0x05ac
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 /* 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
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 \
69 /* table of devices that work with this driver */
70 static const struct usb_device_id bcm5974_table[] = {
71 /* 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),
79 /* 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),
83 /* Terminating entry */
86 MODULE_DEVICE_TABLE(usb, bcm5974_table);
88 MODULE_AUTHOR("Henrik Rydberg");
89 MODULE_DESCRIPTION("Apple USB BCM5974 multitouch driver");
90 MODULE_LICENSE("GPL");
92 #define dprintk(level, format, a...)\
93 { if (debug >= level) printk(KERN_DEBUG format, ##a); }
95 static int debug = 1;
96 module_param(debug, int, 0644);
97 MODULE_PARM_DESC(debug, "Activate debugging output");
99 static int nomt;
100 module_param(nomt, int, 0644);
101 MODULE_PARM_DESC(nomt, "Disable multitouch (MT) events");
103 /* button data structure */
104 struct bt_data {
105 u8 unknown1; /* constant */
106 u8 button; /* left button */
107 u8 rel_x; /* relative x coordinate */
108 u8 rel_y; /* relative y coordinate */
111 /* trackpad header types */
112 enum tp_type {
113 TYPE1, /* plain trackpad */
114 TYPE2 /* button integrated in trackpad */
117 /* trackpad finger data offsets, le16-aligned */
118 #define FINGER_TYPE1 (13 * sizeof(__le16))
119 #define FINGER_TYPE2 (15 * sizeof(__le16))
121 /* trackpad button data offsets */
122 #define BUTTON_TYPE2 15
124 /* list of device capability bits */
125 #define HAS_INTEGRATED_BUTTON 1
127 /* trackpad finger structure, le16-aligned */
128 struct tp_finger {
129 __le16 origin; /* zero when switching track finger */
130 __le16 abs_x; /* absolute x coodinate */
131 __le16 abs_y; /* absolute y coodinate */
132 __le16 rel_x; /* relative x coodinate */
133 __le16 rel_y; /* relative y coodinate */
134 __le16 size_major; /* finger size, major axis? */
135 __le16 size_minor; /* finger size, minor axis? */
136 __le16 orientation; /* 16384 when point, else 15 bit angle */
137 __le16 force_major; /* trackpad force, major axis? */
138 __le16 force_minor; /* trackpad force, minor axis? */
139 __le16 unused[3]; /* zeros */
140 __le16 multi; /* one finger: varies, more fingers: constant */
141 } __attribute__((packed,aligned(2)));
143 /* trackpad finger data size, empirically at least ten fingers */
144 #define SIZEOF_FINGER sizeof(struct tp_finger)
145 #define SIZEOF_ALL_FINGERS (16 * SIZEOF_FINGER)
146 #define MAX_FINGER_ORIENTATION 16384
148 /* device-specific parameters */
149 struct bcm5974_param {
150 int dim; /* logical dimension */
151 int fuzz; /* logical noise value */
152 int devmin; /* device minimum reading */
153 int devmax; /* device maximum reading */
156 /* device-specific configuration */
157 struct bcm5974_config {
158 int ansi, iso, jis; /* the product id of this device */
159 int caps; /* device capability bitmask */
160 int bt_ep; /* the endpoint of the button interface */
161 int bt_datalen; /* data length of the button interface */
162 int tp_ep; /* the endpoint of the trackpad interface */
163 enum tp_type tp_type; /* type of trackpad interface */
164 int tp_offset; /* offset to trackpad finger data */
165 int tp_datalen; /* data length of the trackpad interface */
166 struct bcm5974_param p; /* finger pressure limits */
167 struct bcm5974_param w; /* finger width limits */
168 struct bcm5974_param x; /* horizontal limits */
169 struct bcm5974_param y; /* vertical limits */
172 /* logical device structure */
173 struct bcm5974 {
174 char phys[64];
175 struct usb_device *udev; /* usb device */
176 struct usb_interface *intf; /* our interface */
177 struct input_dev *input; /* input dev */
178 struct bcm5974_config cfg; /* device configuration */
179 struct mutex pm_mutex; /* serialize access to open/suspend */
180 int opened; /* 1: opened, 0: closed */
181 struct urb *bt_urb; /* button usb request block */
182 struct bt_data *bt_data; /* button transferred data */
183 struct urb *tp_urb; /* trackpad usb request block */
184 u8 *tp_data; /* trackpad transferred data */
185 int fingers; /* number of fingers on trackpad */
188 /* logical dimensions */
189 #define DIM_PRESSURE 256 /* maximum finger pressure */
190 #define DIM_WIDTH 16 /* maximum finger width */
191 #define DIM_X 1280 /* maximum trackpad x value */
192 #define DIM_Y 800 /* maximum trackpad y value */
194 /* logical signal quality */
195 #define SN_PRESSURE 45 /* pressure signal-to-noise ratio */
196 #define SN_WIDTH 100 /* width signal-to-noise ratio */
197 #define SN_COORD 250 /* coordinate signal-to-noise ratio */
199 /* pressure thresholds */
200 #define PRESSURE_LOW (2 * DIM_PRESSURE / SN_PRESSURE)
201 #define PRESSURE_HIGH (3 * PRESSURE_LOW)
203 /* device constants */
204 static const struct bcm5974_config bcm5974_config_table[] = {
206 USB_DEVICE_ID_APPLE_WELLSPRING_ANSI,
207 USB_DEVICE_ID_APPLE_WELLSPRING_ISO,
208 USB_DEVICE_ID_APPLE_WELLSPRING_JIS,
210 0x84, sizeof(struct bt_data),
211 0x81, TYPE1, FINGER_TYPE1, FINGER_TYPE1 + SIZEOF_ALL_FINGERS,
212 { DIM_PRESSURE, DIM_PRESSURE / SN_PRESSURE, 0, 256 },
213 { DIM_WIDTH, DIM_WIDTH / SN_WIDTH, 0, 2048 },
214 { DIM_X, DIM_X / SN_COORD, -4824, 5342 },
215 { DIM_Y, DIM_Y / SN_COORD, -172, 5820 }
218 USB_DEVICE_ID_APPLE_WELLSPRING2_ANSI,
219 USB_DEVICE_ID_APPLE_WELLSPRING2_ISO,
220 USB_DEVICE_ID_APPLE_WELLSPRING2_JIS,
222 0x84, sizeof(struct bt_data),
223 0x81, TYPE1, FINGER_TYPE1, FINGER_TYPE1 + SIZEOF_ALL_FINGERS,
224 { DIM_PRESSURE, DIM_PRESSURE / SN_PRESSURE, 0, 256 },
225 { DIM_WIDTH, DIM_WIDTH / SN_WIDTH, 0, 2048 },
226 { DIM_X, DIM_X / SN_COORD, -4824, 4824 },
227 { DIM_Y, DIM_Y / SN_COORD, -172, 4290 }
230 USB_DEVICE_ID_APPLE_WELLSPRING3_ANSI,
231 USB_DEVICE_ID_APPLE_WELLSPRING3_ISO,
232 USB_DEVICE_ID_APPLE_WELLSPRING3_JIS,
233 HAS_INTEGRATED_BUTTON,
234 0x84, sizeof(struct bt_data),
235 0x81, TYPE2, FINGER_TYPE2, FINGER_TYPE2 + SIZEOF_ALL_FINGERS,
236 { DIM_PRESSURE, DIM_PRESSURE / SN_PRESSURE, 0, 300 },
237 { DIM_WIDTH, DIM_WIDTH / SN_WIDTH, 0, 2048 },
238 { DIM_X, DIM_X / SN_COORD, -4460, 5166 },
239 { DIM_Y, DIM_Y / SN_COORD, -75, 6700 }
244 /* return the device-specific configuration by device */
245 static const struct bcm5974_config *bcm5974_get_config(struct usb_device *udev)
247 u16 id = le16_to_cpu(udev->descriptor.idProduct);
248 const struct bcm5974_config *cfg;
250 for (cfg = bcm5974_config_table; cfg->ansi; ++cfg)
251 if (cfg->ansi == id || cfg->iso == id || cfg->jis == id)
252 return cfg;
254 return bcm5974_config_table;
257 /* convert 16-bit little endian to signed integer */
258 static inline int raw2int(__le16 x)
260 return (signed short)le16_to_cpu(x);
263 /* scale device data to logical dimensions (asserts devmin < devmax) */
264 static inline int int2scale(const struct bcm5974_param *p, int x)
266 return x * p->dim / (p->devmax - p->devmin);
269 /* all logical value ranges are [0,dim). */
270 static inline int int2bound(const struct bcm5974_param *p, int x)
272 int s = int2scale(p, x);
274 return clamp_val(s, 0, p->dim - 1);
277 /* setup which logical events to report */
278 static void setup_events_to_report(struct input_dev *input_dev,
279 const struct bcm5974_config *cfg)
281 __set_bit(EV_ABS, input_dev->evbit);
283 input_set_abs_params(input_dev, ABS_PRESSURE,
284 0, cfg->p.dim, cfg->p.fuzz, 0);
285 input_set_abs_params(input_dev, ABS_TOOL_WIDTH,
286 0, cfg->w.dim, cfg->w.fuzz, 0);
287 input_set_abs_params(input_dev, ABS_X,
288 0, cfg->x.dim, cfg->x.fuzz, 0);
289 input_set_abs_params(input_dev, ABS_Y,
290 0, cfg->y.dim, cfg->y.fuzz, 0);
292 if (!nomt) {
293 /* finger touch area */
294 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
295 cfg->w.devmin, cfg->w.devmax, 0, 0);
296 input_set_abs_params(input_dev, ABS_MT_TOUCH_MINOR,
297 cfg->w.devmin, cfg->w.devmax, 0, 0);
298 /* finger approach area */
299 input_set_abs_params(input_dev, ABS_MT_WIDTH_MAJOR,
300 cfg->w.devmin, cfg->w.devmax, 0, 0);
301 input_set_abs_params(input_dev, ABS_MT_WIDTH_MINOR,
302 cfg->w.devmin, cfg->w.devmax, 0, 0);
303 /* finger orientation */
304 input_set_abs_params(input_dev, ABS_MT_ORIENTATION,
305 -MAX_FINGER_ORIENTATION,
306 MAX_FINGER_ORIENTATION, 0, 0);
307 /* finger position */
308 input_set_abs_params(input_dev, ABS_MT_POSITION_X,
309 cfg->x.devmin, cfg->x.devmax, 0, 0);
310 input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
311 cfg->y.devmin, cfg->y.devmax, 0, 0);
314 __set_bit(EV_KEY, input_dev->evbit);
315 __set_bit(BTN_TOUCH, input_dev->keybit);
316 __set_bit(BTN_TOOL_FINGER, input_dev->keybit);
317 __set_bit(BTN_TOOL_DOUBLETAP, input_dev->keybit);
318 __set_bit(BTN_TOOL_TRIPLETAP, input_dev->keybit);
319 __set_bit(BTN_TOOL_QUADTAP, input_dev->keybit);
320 __set_bit(BTN_LEFT, input_dev->keybit);
323 /* report button data as logical button state */
324 static int report_bt_state(struct bcm5974 *dev, int size)
326 if (size != sizeof(struct bt_data))
327 return -EIO;
329 dprintk(7,
330 "bcm5974: button data: %x %x %x %x\n",
331 dev->bt_data->unknown1, dev->bt_data->button,
332 dev->bt_data->rel_x, dev->bt_data->rel_y);
334 input_report_key(dev->input, BTN_LEFT, dev->bt_data->button);
335 input_sync(dev->input);
337 return 0;
340 /* report trackpad data as logical trackpad state */
341 static int report_tp_state(struct bcm5974 *dev, int size)
343 const struct bcm5974_config *c = &dev->cfg;
344 const struct tp_finger *f;
345 struct input_dev *input = dev->input;
346 int raw_p, raw_w, raw_x, raw_y, raw_n, i;
347 int ptest, origin, ibt = 0, nmin = 0, nmax = 0;
348 int abs_p = 0, abs_w = 0, abs_x = 0, abs_y = 0;
350 if (size < c->tp_offset || (size - c->tp_offset) % SIZEOF_FINGER != 0)
351 return -EIO;
353 /* finger data, le16-aligned */
354 f = (const struct tp_finger *)(dev->tp_data + c->tp_offset);
355 raw_n = (size - c->tp_offset) / SIZEOF_FINGER;
357 /* always track the first finger; when detached, start over */
358 if (raw_n) {
360 /* report raw trackpad data */
361 if (!nomt) {
362 for (i = 0; i < raw_n; i++) {
363 int y = c->y.devmin + c->y.devmax;
364 input_report_abs(input, ABS_MT_TOUCH_MAJOR,
365 raw2int(f[i].force_major));
366 input_report_abs(input, ABS_MT_TOUCH_MINOR,
367 raw2int(f[i].force_minor));
368 input_report_abs(input, ABS_MT_WIDTH_MAJOR,
369 raw2int(f[i].size_major));
370 input_report_abs(input, ABS_MT_WIDTH_MINOR,
371 raw2int(f[i].size_minor));
372 input_report_abs(input, ABS_MT_ORIENTATION,
373 MAX_FINGER_ORIENTATION -
374 raw2int(f[i].orientation));
375 input_report_abs(input, ABS_MT_POSITION_X,
376 raw2int(f[i].abs_x));
377 input_report_abs(input, ABS_MT_POSITION_Y,
378 y - raw2int(f[i].abs_y));
379 input_mt_sync(input);
383 raw_p = raw2int(f->force_major);
384 raw_w = raw2int(f->size_major);
385 raw_x = raw2int(f->abs_x);
386 raw_y = raw2int(f->abs_y);
388 dprintk(9,
389 "bcm5974: "
390 "raw: p: %+05d w: %+05d x: %+05d y: %+05d n: %d\n",
391 raw_p, raw_w, raw_x, raw_y, raw_n);
393 ptest = int2bound(&c->p, raw_p);
394 origin = raw2int(f->origin);
396 /* set the integrated button if applicable */
397 if (c->tp_type == TYPE2)
398 ibt = raw2int(dev->tp_data[BUTTON_TYPE2]);
400 /* while tracking finger still valid, count all fingers */
401 if (ptest > PRESSURE_LOW && origin) {
402 abs_p = ptest;
403 abs_w = int2bound(&c->w, raw_w);
404 abs_x = int2bound(&c->x, raw_x - c->x.devmin);
405 abs_y = int2bound(&c->y, c->y.devmax - raw_y);
406 while (raw_n--) {
407 ptest = int2bound(&c->p,
408 raw2int(f->force_major));
409 if (ptest > PRESSURE_LOW)
410 nmax++;
411 if (ptest > PRESSURE_HIGH)
412 nmin++;
413 f++;
418 if (dev->fingers < nmin)
419 dev->fingers = nmin;
420 if (dev->fingers > nmax)
421 dev->fingers = nmax;
423 input_report_key(input, BTN_TOUCH, dev->fingers > 0);
424 input_report_key(input, BTN_TOOL_FINGER, dev->fingers == 1);
425 input_report_key(input, BTN_TOOL_DOUBLETAP, dev->fingers == 2);
426 input_report_key(input, BTN_TOOL_TRIPLETAP, dev->fingers == 3);
427 input_report_key(input, BTN_TOOL_QUADTAP, dev->fingers > 3);
429 input_report_abs(input, ABS_PRESSURE, abs_p);
430 input_report_abs(input, ABS_TOOL_WIDTH, abs_w);
432 if (abs_p) {
433 input_report_abs(input, ABS_X, abs_x);
434 input_report_abs(input, ABS_Y, abs_y);
436 dprintk(8,
437 "bcm5974: abs: p: %+05d w: %+05d x: %+05d y: %+05d "
438 "nmin: %d nmax: %d n: %d ibt: %d\n", abs_p, abs_w,
439 abs_x, abs_y, nmin, nmax, dev->fingers, ibt);
443 /* type 2 reports button events via ibt only */
444 if (c->tp_type == TYPE2)
445 input_report_key(input, BTN_LEFT, ibt);
447 input_sync(input);
449 return 0;
452 /* Wellspring initialization constants */
453 #define BCM5974_WELLSPRING_MODE_READ_REQUEST_ID 1
454 #define BCM5974_WELLSPRING_MODE_WRITE_REQUEST_ID 9
455 #define BCM5974_WELLSPRING_MODE_REQUEST_VALUE 0x300
456 #define BCM5974_WELLSPRING_MODE_REQUEST_INDEX 0
457 #define BCM5974_WELLSPRING_MODE_VENDOR_VALUE 0x01
458 #define BCM5974_WELLSPRING_MODE_NORMAL_VALUE 0x08
460 static int bcm5974_wellspring_mode(struct bcm5974 *dev, bool on)
462 char *data = kmalloc(8, GFP_KERNEL);
463 int retval = 0, size;
465 if (!data) {
466 err("bcm5974: out of memory");
467 retval = -ENOMEM;
468 goto out;
471 /* read configuration */
472 size = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
473 BCM5974_WELLSPRING_MODE_READ_REQUEST_ID,
474 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
475 BCM5974_WELLSPRING_MODE_REQUEST_VALUE,
476 BCM5974_WELLSPRING_MODE_REQUEST_INDEX, data, 8, 5000);
478 if (size != 8) {
479 err("bcm5974: could not read from device");
480 retval = -EIO;
481 goto out;
484 /* apply the mode switch */
485 data[0] = on ?
486 BCM5974_WELLSPRING_MODE_VENDOR_VALUE :
487 BCM5974_WELLSPRING_MODE_NORMAL_VALUE;
489 /* write configuration */
490 size = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
491 BCM5974_WELLSPRING_MODE_WRITE_REQUEST_ID,
492 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
493 BCM5974_WELLSPRING_MODE_REQUEST_VALUE,
494 BCM5974_WELLSPRING_MODE_REQUEST_INDEX, data, 8, 5000);
496 if (size != 8) {
497 err("bcm5974: could not write to device");
498 retval = -EIO;
499 goto out;
502 dprintk(2, "bcm5974: switched to %s mode.\n",
503 on ? "wellspring" : "normal");
505 out:
506 kfree(data);
507 return retval;
510 static void bcm5974_irq_button(struct urb *urb)
512 struct bcm5974 *dev = urb->context;
513 int error;
515 switch (urb->status) {
516 case 0:
517 break;
518 case -EOVERFLOW:
519 case -ECONNRESET:
520 case -ENOENT:
521 case -ESHUTDOWN:
522 dbg("bcm5974: button urb shutting down: %d", urb->status);
523 return;
524 default:
525 dbg("bcm5974: button urb status: %d", urb->status);
526 goto exit;
529 if (report_bt_state(dev, dev->bt_urb->actual_length))
530 dprintk(1, "bcm5974: bad button package, length: %d\n",
531 dev->bt_urb->actual_length);
533 exit:
534 error = usb_submit_urb(dev->bt_urb, GFP_ATOMIC);
535 if (error)
536 err("bcm5974: button urb failed: %d", error);
539 static void bcm5974_irq_trackpad(struct urb *urb)
541 struct bcm5974 *dev = urb->context;
542 int error;
544 switch (urb->status) {
545 case 0:
546 break;
547 case -EOVERFLOW:
548 case -ECONNRESET:
549 case -ENOENT:
550 case -ESHUTDOWN:
551 dbg("bcm5974: trackpad urb shutting down: %d", urb->status);
552 return;
553 default:
554 dbg("bcm5974: trackpad urb status: %d", urb->status);
555 goto exit;
558 /* control response ignored */
559 if (dev->tp_urb->actual_length == 2)
560 goto exit;
562 if (report_tp_state(dev, dev->tp_urb->actual_length))
563 dprintk(1, "bcm5974: bad trackpad package, length: %d\n",
564 dev->tp_urb->actual_length);
566 exit:
567 error = usb_submit_urb(dev->tp_urb, GFP_ATOMIC);
568 if (error)
569 err("bcm5974: trackpad urb failed: %d", error);
573 * The Wellspring trackpad, like many recent Apple trackpads, share
574 * the usb device with the keyboard. Since keyboards are usually
575 * handled by the HID system, the device ends up being handled by two
576 * modules. Setting up the device therefore becomes slightly
577 * complicated. To enable multitouch features, a mode switch is
578 * required, which is usually applied via the control interface of the
579 * device. It can be argued where this switch should take place. In
580 * some drivers, like appletouch, the switch is made during
581 * probe. However, the hid module may also alter the state of the
582 * device, resulting in trackpad malfunction under certain
583 * circumstances. To get around this problem, there is at least one
584 * example that utilizes the USB_QUIRK_RESET_RESUME quirk in order to
585 * recieve a reset_resume request rather than the normal resume.
586 * Since the implementation of reset_resume is equal to mode switch
587 * plus start_traffic, it seems easier to always do the switch when
588 * starting traffic on the device.
590 static int bcm5974_start_traffic(struct bcm5974 *dev)
592 if (bcm5974_wellspring_mode(dev, true)) {
593 dprintk(1, "bcm5974: mode switch failed\n");
594 goto error;
597 if (usb_submit_urb(dev->bt_urb, GFP_KERNEL))
598 goto error;
600 if (usb_submit_urb(dev->tp_urb, GFP_KERNEL))
601 goto err_kill_bt;
603 return 0;
605 err_kill_bt:
606 usb_kill_urb(dev->bt_urb);
607 error:
608 return -EIO;
611 static void bcm5974_pause_traffic(struct bcm5974 *dev)
613 usb_kill_urb(dev->tp_urb);
614 usb_kill_urb(dev->bt_urb);
615 bcm5974_wellspring_mode(dev, false);
619 * The code below implements open/close and manual suspend/resume.
620 * All functions may be called in random order.
622 * Opening a suspended device fails with EACCES - permission denied.
624 * Failing a resume leaves the device resumed but closed.
626 static int bcm5974_open(struct input_dev *input)
628 struct bcm5974 *dev = input_get_drvdata(input);
629 int error;
631 error = usb_autopm_get_interface(dev->intf);
632 if (error)
633 return error;
635 mutex_lock(&dev->pm_mutex);
637 error = bcm5974_start_traffic(dev);
638 if (!error)
639 dev->opened = 1;
641 mutex_unlock(&dev->pm_mutex);
643 if (error)
644 usb_autopm_put_interface(dev->intf);
646 return error;
649 static void bcm5974_close(struct input_dev *input)
651 struct bcm5974 *dev = input_get_drvdata(input);
653 mutex_lock(&dev->pm_mutex);
655 bcm5974_pause_traffic(dev);
656 dev->opened = 0;
658 mutex_unlock(&dev->pm_mutex);
660 usb_autopm_put_interface(dev->intf);
663 static int bcm5974_suspend(struct usb_interface *iface, pm_message_t message)
665 struct bcm5974 *dev = usb_get_intfdata(iface);
667 mutex_lock(&dev->pm_mutex);
669 if (dev->opened)
670 bcm5974_pause_traffic(dev);
672 mutex_unlock(&dev->pm_mutex);
674 return 0;
677 static int bcm5974_resume(struct usb_interface *iface)
679 struct bcm5974 *dev = usb_get_intfdata(iface);
680 int error = 0;
682 mutex_lock(&dev->pm_mutex);
684 if (dev->opened)
685 error = bcm5974_start_traffic(dev);
687 mutex_unlock(&dev->pm_mutex);
689 return error;
692 static int bcm5974_probe(struct usb_interface *iface,
693 const struct usb_device_id *id)
695 struct usb_device *udev = interface_to_usbdev(iface);
696 const struct bcm5974_config *cfg;
697 struct bcm5974 *dev;
698 struct input_dev *input_dev;
699 int error = -ENOMEM;
701 /* find the product index */
702 cfg = bcm5974_get_config(udev);
704 /* allocate memory for our device state and initialize it */
705 dev = kzalloc(sizeof(struct bcm5974), GFP_KERNEL);
706 input_dev = input_allocate_device();
707 if (!dev || !input_dev) {
708 err("bcm5974: out of memory");
709 goto err_free_devs;
712 dev->udev = udev;
713 dev->intf = iface;
714 dev->input = input_dev;
715 dev->cfg = *cfg;
716 mutex_init(&dev->pm_mutex);
718 /* setup urbs */
719 dev->bt_urb = usb_alloc_urb(0, GFP_KERNEL);
720 if (!dev->bt_urb)
721 goto err_free_devs;
723 dev->tp_urb = usb_alloc_urb(0, GFP_KERNEL);
724 if (!dev->tp_urb)
725 goto err_free_bt_urb;
727 dev->bt_data = usb_buffer_alloc(dev->udev,
728 dev->cfg.bt_datalen, GFP_KERNEL,
729 &dev->bt_urb->transfer_dma);
730 if (!dev->bt_data)
731 goto err_free_urb;
733 dev->tp_data = usb_buffer_alloc(dev->udev,
734 dev->cfg.tp_datalen, GFP_KERNEL,
735 &dev->tp_urb->transfer_dma);
736 if (!dev->tp_data)
737 goto err_free_bt_buffer;
739 usb_fill_int_urb(dev->bt_urb, udev,
740 usb_rcvintpipe(udev, cfg->bt_ep),
741 dev->bt_data, dev->cfg.bt_datalen,
742 bcm5974_irq_button, dev, 1);
744 usb_fill_int_urb(dev->tp_urb, udev,
745 usb_rcvintpipe(udev, cfg->tp_ep),
746 dev->tp_data, dev->cfg.tp_datalen,
747 bcm5974_irq_trackpad, dev, 1);
749 /* create bcm5974 device */
750 usb_make_path(udev, dev->phys, sizeof(dev->phys));
751 strlcat(dev->phys, "/input0", sizeof(dev->phys));
753 input_dev->name = "bcm5974";
754 input_dev->phys = dev->phys;
755 usb_to_input_id(dev->udev, &input_dev->id);
756 /* report driver capabilities via the version field */
757 input_dev->id.version = cfg->caps;
758 input_dev->dev.parent = &iface->dev;
760 input_set_drvdata(input_dev, dev);
762 input_dev->open = bcm5974_open;
763 input_dev->close = bcm5974_close;
765 setup_events_to_report(input_dev, cfg);
767 error = input_register_device(dev->input);
768 if (error)
769 goto err_free_buffer;
771 /* save our data pointer in this interface device */
772 usb_set_intfdata(iface, dev);
774 return 0;
776 err_free_buffer:
777 usb_buffer_free(dev->udev, dev->cfg.tp_datalen,
778 dev->tp_data, dev->tp_urb->transfer_dma);
779 err_free_bt_buffer:
780 usb_buffer_free(dev->udev, dev->cfg.bt_datalen,
781 dev->bt_data, dev->bt_urb->transfer_dma);
782 err_free_urb:
783 usb_free_urb(dev->tp_urb);
784 err_free_bt_urb:
785 usb_free_urb(dev->bt_urb);
786 err_free_devs:
787 usb_set_intfdata(iface, NULL);
788 input_free_device(input_dev);
789 kfree(dev);
790 return error;
793 static void bcm5974_disconnect(struct usb_interface *iface)
795 struct bcm5974 *dev = usb_get_intfdata(iface);
797 usb_set_intfdata(iface, NULL);
799 input_unregister_device(dev->input);
800 usb_buffer_free(dev->udev, dev->cfg.tp_datalen,
801 dev->tp_data, dev->tp_urb->transfer_dma);
802 usb_buffer_free(dev->udev, dev->cfg.bt_datalen,
803 dev->bt_data, dev->bt_urb->transfer_dma);
804 usb_free_urb(dev->tp_urb);
805 usb_free_urb(dev->bt_urb);
806 kfree(dev);
809 static struct usb_driver bcm5974_driver = {
810 .name = "bcm5974",
811 .probe = bcm5974_probe,
812 .disconnect = bcm5974_disconnect,
813 .suspend = bcm5974_suspend,
814 .resume = bcm5974_resume,
815 .id_table = bcm5974_table,
816 .supports_autosuspend = 1,
819 static int __init bcm5974_init(void)
821 return usb_register(&bcm5974_driver);
824 static void __exit bcm5974_exit(void)
826 usb_deregister(&bcm5974_driver);
829 module_init(bcm5974_init);
830 module_exit(bcm5974_exit);