Add linux-next specific files for 20110831
[linux-2.6/next.git] / drivers / input / mouse / bcm5974.c
blobda280189ef07c573e0a2aa05971eb3f8bbba7f64
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
58 /* MacbookAir3,2 (unibody), aka wellspring5 */
59 #define USB_DEVICE_ID_APPLE_WELLSPRING4_ANSI 0x023f
60 #define USB_DEVICE_ID_APPLE_WELLSPRING4_ISO 0x0240
61 #define USB_DEVICE_ID_APPLE_WELLSPRING4_JIS 0x0241
62 /* MacbookAir3,1 (unibody), aka wellspring4 */
63 #define USB_DEVICE_ID_APPLE_WELLSPRING4A_ANSI 0x0242
64 #define USB_DEVICE_ID_APPLE_WELLSPRING4A_ISO 0x0243
65 #define USB_DEVICE_ID_APPLE_WELLSPRING4A_JIS 0x0244
66 /* Macbook8 (unibody, March 2011) */
67 #define USB_DEVICE_ID_APPLE_WELLSPRING5_ANSI 0x0245
68 #define USB_DEVICE_ID_APPLE_WELLSPRING5_ISO 0x0246
69 #define USB_DEVICE_ID_APPLE_WELLSPRING5_JIS 0x0247
70 /* MacbookAir4,2 (unibody, July 2011) */
71 #define USB_DEVICE_ID_APPLE_WELLSPRING6_ANSI 0x024c
72 #define USB_DEVICE_ID_APPLE_WELLSPRING6_ISO 0x024d
73 #define USB_DEVICE_ID_APPLE_WELLSPRING6_JIS 0x024e
74 /* Macbook8,2 (unibody) */
75 #define USB_DEVICE_ID_APPLE_WELLSPRING5A_ANSI 0x0252
76 #define USB_DEVICE_ID_APPLE_WELLSPRING5A_ISO 0x0253
77 #define USB_DEVICE_ID_APPLE_WELLSPRING5A_JIS 0x0254
79 #define BCM5974_DEVICE(prod) { \
80 .match_flags = (USB_DEVICE_ID_MATCH_DEVICE | \
81 USB_DEVICE_ID_MATCH_INT_CLASS | \
82 USB_DEVICE_ID_MATCH_INT_PROTOCOL), \
83 .idVendor = USB_VENDOR_ID_APPLE, \
84 .idProduct = (prod), \
85 .bInterfaceClass = USB_INTERFACE_CLASS_HID, \
86 .bInterfaceProtocol = USB_INTERFACE_PROTOCOL_MOUSE \
89 /* table of devices that work with this driver */
90 static const struct usb_device_id bcm5974_table[] = {
91 /* MacbookAir1.1 */
92 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING_ANSI),
93 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING_ISO),
94 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING_JIS),
95 /* MacbookProPenryn */
96 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING2_ANSI),
97 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING2_ISO),
98 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING2_JIS),
99 /* Macbook5,1 */
100 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING3_ANSI),
101 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING3_ISO),
102 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING3_JIS),
103 /* MacbookAir3,2 */
104 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING4_ANSI),
105 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING4_ISO),
106 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING4_JIS),
107 /* MacbookAir3,1 */
108 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING4A_ANSI),
109 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING4A_ISO),
110 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING4A_JIS),
111 /* MacbookPro8 */
112 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING5_ANSI),
113 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING5_ISO),
114 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING5_JIS),
115 /* MacbookAir4,2 */
116 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING6_ANSI),
117 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING6_ISO),
118 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING6_JIS),
119 /* MacbookPro8,2 */
120 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING5A_ANSI),
121 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING5A_ISO),
122 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING5A_JIS),
123 /* Terminating entry */
126 MODULE_DEVICE_TABLE(usb, bcm5974_table);
128 MODULE_AUTHOR("Henrik Rydberg");
129 MODULE_DESCRIPTION("Apple USB BCM5974 multitouch driver");
130 MODULE_LICENSE("GPL");
132 #define dprintk(level, format, a...)\
133 { if (debug >= level) printk(KERN_DEBUG format, ##a); }
135 static int debug = 1;
136 module_param(debug, int, 0644);
137 MODULE_PARM_DESC(debug, "Activate debugging output");
139 /* button data structure */
140 struct bt_data {
141 u8 unknown1; /* constant */
142 u8 button; /* left button */
143 u8 rel_x; /* relative x coordinate */
144 u8 rel_y; /* relative y coordinate */
147 /* trackpad header types */
148 enum tp_type {
149 TYPE1, /* plain trackpad */
150 TYPE2 /* button integrated in trackpad */
153 /* trackpad finger data offsets, le16-aligned */
154 #define FINGER_TYPE1 (13 * sizeof(__le16))
155 #define FINGER_TYPE2 (15 * sizeof(__le16))
157 /* trackpad button data offsets */
158 #define BUTTON_TYPE2 15
160 /* list of device capability bits */
161 #define HAS_INTEGRATED_BUTTON 1
163 /* trackpad finger structure, le16-aligned */
164 struct tp_finger {
165 __le16 origin; /* zero when switching track finger */
166 __le16 abs_x; /* absolute x coodinate */
167 __le16 abs_y; /* absolute y coodinate */
168 __le16 rel_x; /* relative x coodinate */
169 __le16 rel_y; /* relative y coodinate */
170 __le16 size_major; /* finger size, major axis? */
171 __le16 size_minor; /* finger size, minor axis? */
172 __le16 orientation; /* 16384 when point, else 15 bit angle */
173 __le16 force_major; /* trackpad force, major axis? */
174 __le16 force_minor; /* trackpad force, minor axis? */
175 __le16 unused[3]; /* zeros */
176 __le16 multi; /* one finger: varies, more fingers: constant */
177 } __attribute__((packed,aligned(2)));
179 /* trackpad finger data size, empirically at least ten fingers */
180 #define SIZEOF_FINGER sizeof(struct tp_finger)
181 #define SIZEOF_ALL_FINGERS (16 * SIZEOF_FINGER)
182 #define MAX_FINGER_ORIENTATION 16384
184 /* device-specific parameters */
185 struct bcm5974_param {
186 int dim; /* logical dimension */
187 int fuzz; /* logical noise value */
188 int devmin; /* device minimum reading */
189 int devmax; /* device maximum reading */
192 /* device-specific configuration */
193 struct bcm5974_config {
194 int ansi, iso, jis; /* the product id of this device */
195 int caps; /* device capability bitmask */
196 int bt_ep; /* the endpoint of the button interface */
197 int bt_datalen; /* data length of the button interface */
198 int tp_ep; /* the endpoint of the trackpad interface */
199 enum tp_type tp_type; /* type of trackpad interface */
200 int tp_offset; /* offset to trackpad finger data */
201 int tp_datalen; /* data length of the trackpad interface */
202 struct bcm5974_param p; /* finger pressure limits */
203 struct bcm5974_param w; /* finger width limits */
204 struct bcm5974_param x; /* horizontal limits */
205 struct bcm5974_param y; /* vertical limits */
208 /* logical device structure */
209 struct bcm5974 {
210 char phys[64];
211 struct usb_device *udev; /* usb device */
212 struct usb_interface *intf; /* our interface */
213 struct input_dev *input; /* input dev */
214 struct bcm5974_config cfg; /* device configuration */
215 struct mutex pm_mutex; /* serialize access to open/suspend */
216 int opened; /* 1: opened, 0: closed */
217 struct urb *bt_urb; /* button usb request block */
218 struct bt_data *bt_data; /* button transferred data */
219 struct urb *tp_urb; /* trackpad usb request block */
220 u8 *tp_data; /* trackpad transferred data */
221 int fingers; /* number of fingers on trackpad */
224 /* logical dimensions */
225 #define DIM_PRESSURE 256 /* maximum finger pressure */
226 #define DIM_WIDTH 16 /* maximum finger width */
227 #define DIM_X 1280 /* maximum trackpad x value */
228 #define DIM_Y 800 /* maximum trackpad y value */
230 /* logical signal quality */
231 #define SN_PRESSURE 45 /* pressure signal-to-noise ratio */
232 #define SN_WIDTH 100 /* width signal-to-noise ratio */
233 #define SN_COORD 250 /* coordinate signal-to-noise ratio */
235 /* pressure thresholds */
236 #define PRESSURE_LOW (2 * DIM_PRESSURE / SN_PRESSURE)
237 #define PRESSURE_HIGH (3 * PRESSURE_LOW)
239 /* device constants */
240 static const struct bcm5974_config bcm5974_config_table[] = {
242 USB_DEVICE_ID_APPLE_WELLSPRING_ANSI,
243 USB_DEVICE_ID_APPLE_WELLSPRING_ISO,
244 USB_DEVICE_ID_APPLE_WELLSPRING_JIS,
246 0x84, sizeof(struct bt_data),
247 0x81, TYPE1, FINGER_TYPE1, FINGER_TYPE1 + SIZEOF_ALL_FINGERS,
248 { DIM_PRESSURE, DIM_PRESSURE / SN_PRESSURE, 0, 256 },
249 { DIM_WIDTH, DIM_WIDTH / SN_WIDTH, 0, 2048 },
250 { DIM_X, DIM_X / SN_COORD, -4824, 5342 },
251 { DIM_Y, DIM_Y / SN_COORD, -172, 5820 }
254 USB_DEVICE_ID_APPLE_WELLSPRING2_ANSI,
255 USB_DEVICE_ID_APPLE_WELLSPRING2_ISO,
256 USB_DEVICE_ID_APPLE_WELLSPRING2_JIS,
258 0x84, sizeof(struct bt_data),
259 0x81, TYPE1, FINGER_TYPE1, FINGER_TYPE1 + SIZEOF_ALL_FINGERS,
260 { DIM_PRESSURE, DIM_PRESSURE / SN_PRESSURE, 0, 256 },
261 { DIM_WIDTH, DIM_WIDTH / SN_WIDTH, 0, 2048 },
262 { DIM_X, DIM_X / SN_COORD, -4824, 4824 },
263 { DIM_Y, DIM_Y / SN_COORD, -172, 4290 }
266 USB_DEVICE_ID_APPLE_WELLSPRING3_ANSI,
267 USB_DEVICE_ID_APPLE_WELLSPRING3_ISO,
268 USB_DEVICE_ID_APPLE_WELLSPRING3_JIS,
269 HAS_INTEGRATED_BUTTON,
270 0x84, sizeof(struct bt_data),
271 0x81, TYPE2, FINGER_TYPE2, FINGER_TYPE2 + SIZEOF_ALL_FINGERS,
272 { DIM_PRESSURE, DIM_PRESSURE / SN_PRESSURE, 0, 300 },
273 { DIM_WIDTH, DIM_WIDTH / SN_WIDTH, 0, 2048 },
274 { DIM_X, DIM_X / SN_COORD, -4460, 5166 },
275 { DIM_Y, DIM_Y / SN_COORD, -75, 6700 }
278 USB_DEVICE_ID_APPLE_WELLSPRING4_ANSI,
279 USB_DEVICE_ID_APPLE_WELLSPRING4_ISO,
280 USB_DEVICE_ID_APPLE_WELLSPRING4_JIS,
281 HAS_INTEGRATED_BUTTON,
282 0x84, sizeof(struct bt_data),
283 0x81, TYPE2, FINGER_TYPE2, FINGER_TYPE2 + SIZEOF_ALL_FINGERS,
284 { DIM_PRESSURE, DIM_PRESSURE / SN_PRESSURE, 0, 300 },
285 { DIM_WIDTH, DIM_WIDTH / SN_WIDTH, 0, 2048 },
286 { DIM_X, DIM_X / SN_COORD, -4620, 5140 },
287 { DIM_Y, DIM_Y / SN_COORD, -150, 6600 }
290 USB_DEVICE_ID_APPLE_WELLSPRING4A_ANSI,
291 USB_DEVICE_ID_APPLE_WELLSPRING4A_ISO,
292 USB_DEVICE_ID_APPLE_WELLSPRING4A_JIS,
293 HAS_INTEGRATED_BUTTON,
294 0x84, sizeof(struct bt_data),
295 0x81, TYPE2, FINGER_TYPE2, FINGER_TYPE2 + SIZEOF_ALL_FINGERS,
296 { DIM_PRESSURE, DIM_PRESSURE / SN_PRESSURE, 0, 300 },
297 { DIM_WIDTH, DIM_WIDTH / SN_WIDTH, 0, 2048 },
298 { DIM_X, DIM_X / SN_COORD, -4616, 5112 },
299 { DIM_Y, DIM_Y / SN_COORD, -142, 5234 }
302 USB_DEVICE_ID_APPLE_WELLSPRING5_ANSI,
303 USB_DEVICE_ID_APPLE_WELLSPRING5_ISO,
304 USB_DEVICE_ID_APPLE_WELLSPRING5_JIS,
305 HAS_INTEGRATED_BUTTON,
306 0x84, sizeof(struct bt_data),
307 0x81, TYPE2, FINGER_TYPE2, FINGER_TYPE2 + SIZEOF_ALL_FINGERS,
308 { DIM_PRESSURE, DIM_PRESSURE / SN_PRESSURE, 0, 300 },
309 { DIM_WIDTH, DIM_WIDTH / SN_WIDTH, 0, 2048 },
310 { DIM_X, DIM_X / SN_COORD, -4415, 5050 },
311 { DIM_Y, DIM_Y / SN_COORD, -55, 6680 }
314 USB_DEVICE_ID_APPLE_WELLSPRING6_ANSI,
315 USB_DEVICE_ID_APPLE_WELLSPRING6_ISO,
316 USB_DEVICE_ID_APPLE_WELLSPRING6_JIS,
317 HAS_INTEGRATED_BUTTON,
318 0x84, sizeof(struct bt_data),
319 0x81, TYPE2, FINGER_TYPE2, FINGER_TYPE2 + SIZEOF_ALL_FINGERS,
320 { DIM_PRESSURE, DIM_PRESSURE / SN_PRESSURE, 0, 300 },
321 { DIM_WIDTH, DIM_WIDTH / SN_WIDTH, 0, 2048 },
322 { DIM_X, DIM_X / SN_COORD, -4620, 5140 },
323 { DIM_Y, DIM_Y / SN_COORD, -150, 6600 }
326 USB_DEVICE_ID_APPLE_WELLSPRING5A_ANSI,
327 USB_DEVICE_ID_APPLE_WELLSPRING5A_ISO,
328 USB_DEVICE_ID_APPLE_WELLSPRING5A_JIS,
329 HAS_INTEGRATED_BUTTON,
330 0x84, sizeof(struct bt_data),
331 0x81, TYPE2, FINGER_TYPE2, FINGER_TYPE2 + SIZEOF_ALL_FINGERS,
332 { DIM_PRESSURE, DIM_PRESSURE / SN_PRESSURE, 0, 300 },
333 { DIM_WIDTH, DIM_WIDTH / SN_WIDTH, 0, 2048 },
334 { DIM_X, DIM_X / SN_COORD, -4750, 5280 },
335 { DIM_Y, DIM_Y / SN_COORD, -150, 6730 }
340 /* return the device-specific configuration by device */
341 static const struct bcm5974_config *bcm5974_get_config(struct usb_device *udev)
343 u16 id = le16_to_cpu(udev->descriptor.idProduct);
344 const struct bcm5974_config *cfg;
346 for (cfg = bcm5974_config_table; cfg->ansi; ++cfg)
347 if (cfg->ansi == id || cfg->iso == id || cfg->jis == id)
348 return cfg;
350 return bcm5974_config_table;
353 /* convert 16-bit little endian to signed integer */
354 static inline int raw2int(__le16 x)
356 return (signed short)le16_to_cpu(x);
359 /* scale device data to logical dimensions (asserts devmin < devmax) */
360 static inline int int2scale(const struct bcm5974_param *p, int x)
362 return x * p->dim / (p->devmax - p->devmin);
365 /* all logical value ranges are [0,dim). */
366 static inline int int2bound(const struct bcm5974_param *p, int x)
368 int s = int2scale(p, x);
370 return clamp_val(s, 0, p->dim - 1);
373 /* setup which logical events to report */
374 static void setup_events_to_report(struct input_dev *input_dev,
375 const struct bcm5974_config *cfg)
377 __set_bit(EV_ABS, input_dev->evbit);
379 input_set_abs_params(input_dev, ABS_PRESSURE,
380 0, cfg->p.dim, cfg->p.fuzz, 0);
381 input_set_abs_params(input_dev, ABS_TOOL_WIDTH,
382 0, cfg->w.dim, cfg->w.fuzz, 0);
383 input_set_abs_params(input_dev, ABS_X,
384 0, cfg->x.dim, cfg->x.fuzz, 0);
385 input_set_abs_params(input_dev, ABS_Y,
386 0, cfg->y.dim, cfg->y.fuzz, 0);
388 /* finger touch area */
389 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
390 cfg->w.devmin, cfg->w.devmax, 0, 0);
391 input_set_abs_params(input_dev, ABS_MT_TOUCH_MINOR,
392 cfg->w.devmin, cfg->w.devmax, 0, 0);
393 /* finger approach area */
394 input_set_abs_params(input_dev, ABS_MT_WIDTH_MAJOR,
395 cfg->w.devmin, cfg->w.devmax, 0, 0);
396 input_set_abs_params(input_dev, ABS_MT_WIDTH_MINOR,
397 cfg->w.devmin, cfg->w.devmax, 0, 0);
398 /* finger orientation */
399 input_set_abs_params(input_dev, ABS_MT_ORIENTATION,
400 -MAX_FINGER_ORIENTATION,
401 MAX_FINGER_ORIENTATION, 0, 0);
402 /* finger position */
403 input_set_abs_params(input_dev, ABS_MT_POSITION_X,
404 cfg->x.devmin, cfg->x.devmax, 0, 0);
405 input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
406 cfg->y.devmin, cfg->y.devmax, 0, 0);
408 __set_bit(EV_KEY, input_dev->evbit);
409 __set_bit(BTN_TOUCH, input_dev->keybit);
410 __set_bit(BTN_TOOL_FINGER, input_dev->keybit);
411 __set_bit(BTN_TOOL_DOUBLETAP, input_dev->keybit);
412 __set_bit(BTN_TOOL_TRIPLETAP, input_dev->keybit);
413 __set_bit(BTN_TOOL_QUADTAP, input_dev->keybit);
414 __set_bit(BTN_LEFT, input_dev->keybit);
416 input_set_events_per_packet(input_dev, 60);
419 /* report button data as logical button state */
420 static int report_bt_state(struct bcm5974 *dev, int size)
422 if (size != sizeof(struct bt_data))
423 return -EIO;
425 dprintk(7,
426 "bcm5974: button data: %x %x %x %x\n",
427 dev->bt_data->unknown1, dev->bt_data->button,
428 dev->bt_data->rel_x, dev->bt_data->rel_y);
430 input_report_key(dev->input, BTN_LEFT, dev->bt_data->button);
431 input_sync(dev->input);
433 return 0;
436 static void report_finger_data(struct input_dev *input,
437 const struct bcm5974_config *cfg,
438 const struct tp_finger *f)
440 input_report_abs(input, ABS_MT_TOUCH_MAJOR,
441 raw2int(f->force_major) << 1);
442 input_report_abs(input, ABS_MT_TOUCH_MINOR,
443 raw2int(f->force_minor) << 1);
444 input_report_abs(input, ABS_MT_WIDTH_MAJOR,
445 raw2int(f->size_major) << 1);
446 input_report_abs(input, ABS_MT_WIDTH_MINOR,
447 raw2int(f->size_minor) << 1);
448 input_report_abs(input, ABS_MT_ORIENTATION,
449 MAX_FINGER_ORIENTATION - raw2int(f->orientation));
450 input_report_abs(input, ABS_MT_POSITION_X, raw2int(f->abs_x));
451 input_report_abs(input, ABS_MT_POSITION_Y,
452 cfg->y.devmin + cfg->y.devmax - raw2int(f->abs_y));
453 input_mt_sync(input);
456 /* report trackpad data as logical trackpad state */
457 static int report_tp_state(struct bcm5974 *dev, int size)
459 const struct bcm5974_config *c = &dev->cfg;
460 const struct tp_finger *f;
461 struct input_dev *input = dev->input;
462 int raw_p, raw_w, raw_x, raw_y, raw_n, i;
463 int ptest, origin, ibt = 0, nmin = 0, nmax = 0;
464 int abs_p = 0, abs_w = 0, abs_x = 0, abs_y = 0;
466 if (size < c->tp_offset || (size - c->tp_offset) % SIZEOF_FINGER != 0)
467 return -EIO;
469 /* finger data, le16-aligned */
470 f = (const struct tp_finger *)(dev->tp_data + c->tp_offset);
471 raw_n = (size - c->tp_offset) / SIZEOF_FINGER;
473 /* always track the first finger; when detached, start over */
474 if (raw_n) {
476 /* report raw trackpad data */
477 for (i = 0; i < raw_n; i++)
478 report_finger_data(input, c, &f[i]);
480 raw_p = raw2int(f->force_major);
481 raw_w = raw2int(f->size_major);
482 raw_x = raw2int(f->abs_x);
483 raw_y = raw2int(f->abs_y);
485 dprintk(9,
486 "bcm5974: "
487 "raw: p: %+05d w: %+05d x: %+05d y: %+05d n: %d\n",
488 raw_p, raw_w, raw_x, raw_y, raw_n);
490 ptest = int2bound(&c->p, raw_p);
491 origin = raw2int(f->origin);
493 /* while tracking finger still valid, count all fingers */
494 if (ptest > PRESSURE_LOW && origin) {
495 abs_p = ptest;
496 abs_w = int2bound(&c->w, raw_w);
497 abs_x = int2bound(&c->x, raw_x - c->x.devmin);
498 abs_y = int2bound(&c->y, c->y.devmax - raw_y);
499 while (raw_n--) {
500 ptest = int2bound(&c->p,
501 raw2int(f->force_major));
502 if (ptest > PRESSURE_LOW)
503 nmax++;
504 if (ptest > PRESSURE_HIGH)
505 nmin++;
506 f++;
511 /* set the integrated button if applicable */
512 if (c->tp_type == TYPE2)
513 ibt = raw2int(dev->tp_data[BUTTON_TYPE2]);
515 if (dev->fingers < nmin)
516 dev->fingers = nmin;
517 if (dev->fingers > nmax)
518 dev->fingers = nmax;
520 input_report_key(input, BTN_TOUCH, dev->fingers > 0);
521 input_report_key(input, BTN_TOOL_FINGER, dev->fingers == 1);
522 input_report_key(input, BTN_TOOL_DOUBLETAP, dev->fingers == 2);
523 input_report_key(input, BTN_TOOL_TRIPLETAP, dev->fingers == 3);
524 input_report_key(input, BTN_TOOL_QUADTAP, dev->fingers > 3);
526 input_report_abs(input, ABS_PRESSURE, abs_p);
527 input_report_abs(input, ABS_TOOL_WIDTH, abs_w);
529 if (abs_p) {
530 input_report_abs(input, ABS_X, abs_x);
531 input_report_abs(input, ABS_Y, abs_y);
533 dprintk(8,
534 "bcm5974: abs: p: %+05d w: %+05d x: %+05d y: %+05d "
535 "nmin: %d nmax: %d n: %d ibt: %d\n", abs_p, abs_w,
536 abs_x, abs_y, nmin, nmax, dev->fingers, ibt);
540 /* type 2 reports button events via ibt only */
541 if (c->tp_type == TYPE2)
542 input_report_key(input, BTN_LEFT, ibt);
544 input_sync(input);
546 return 0;
549 /* Wellspring initialization constants */
550 #define BCM5974_WELLSPRING_MODE_READ_REQUEST_ID 1
551 #define BCM5974_WELLSPRING_MODE_WRITE_REQUEST_ID 9
552 #define BCM5974_WELLSPRING_MODE_REQUEST_VALUE 0x300
553 #define BCM5974_WELLSPRING_MODE_REQUEST_INDEX 0
554 #define BCM5974_WELLSPRING_MODE_VENDOR_VALUE 0x01
555 #define BCM5974_WELLSPRING_MODE_NORMAL_VALUE 0x08
557 static int bcm5974_wellspring_mode(struct bcm5974 *dev, bool on)
559 char *data = kmalloc(8, GFP_KERNEL);
560 int retval = 0, size;
562 if (!data) {
563 err("bcm5974: out of memory");
564 retval = -ENOMEM;
565 goto out;
568 /* read configuration */
569 size = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
570 BCM5974_WELLSPRING_MODE_READ_REQUEST_ID,
571 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
572 BCM5974_WELLSPRING_MODE_REQUEST_VALUE,
573 BCM5974_WELLSPRING_MODE_REQUEST_INDEX, data, 8, 5000);
575 if (size != 8) {
576 err("bcm5974: could not read from device");
577 retval = -EIO;
578 goto out;
581 /* apply the mode switch */
582 data[0] = on ?
583 BCM5974_WELLSPRING_MODE_VENDOR_VALUE :
584 BCM5974_WELLSPRING_MODE_NORMAL_VALUE;
586 /* write configuration */
587 size = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
588 BCM5974_WELLSPRING_MODE_WRITE_REQUEST_ID,
589 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
590 BCM5974_WELLSPRING_MODE_REQUEST_VALUE,
591 BCM5974_WELLSPRING_MODE_REQUEST_INDEX, data, 8, 5000);
593 if (size != 8) {
594 err("bcm5974: could not write to device");
595 retval = -EIO;
596 goto out;
599 dprintk(2, "bcm5974: switched to %s mode.\n",
600 on ? "wellspring" : "normal");
602 out:
603 kfree(data);
604 return retval;
607 static void bcm5974_irq_button(struct urb *urb)
609 struct bcm5974 *dev = urb->context;
610 int error;
612 switch (urb->status) {
613 case 0:
614 break;
615 case -EOVERFLOW:
616 case -ECONNRESET:
617 case -ENOENT:
618 case -ESHUTDOWN:
619 dbg("bcm5974: button urb shutting down: %d", urb->status);
620 return;
621 default:
622 dbg("bcm5974: button urb status: %d", urb->status);
623 goto exit;
626 if (report_bt_state(dev, dev->bt_urb->actual_length))
627 dprintk(1, "bcm5974: bad button package, length: %d\n",
628 dev->bt_urb->actual_length);
630 exit:
631 error = usb_submit_urb(dev->bt_urb, GFP_ATOMIC);
632 if (error)
633 err("bcm5974: button urb failed: %d", error);
636 static void bcm5974_irq_trackpad(struct urb *urb)
638 struct bcm5974 *dev = urb->context;
639 int error;
641 switch (urb->status) {
642 case 0:
643 break;
644 case -EOVERFLOW:
645 case -ECONNRESET:
646 case -ENOENT:
647 case -ESHUTDOWN:
648 dbg("bcm5974: trackpad urb shutting down: %d", urb->status);
649 return;
650 default:
651 dbg("bcm5974: trackpad urb status: %d", urb->status);
652 goto exit;
655 /* control response ignored */
656 if (dev->tp_urb->actual_length == 2)
657 goto exit;
659 if (report_tp_state(dev, dev->tp_urb->actual_length))
660 dprintk(1, "bcm5974: bad trackpad package, length: %d\n",
661 dev->tp_urb->actual_length);
663 exit:
664 error = usb_submit_urb(dev->tp_urb, GFP_ATOMIC);
665 if (error)
666 err("bcm5974: trackpad urb failed: %d", error);
670 * The Wellspring trackpad, like many recent Apple trackpads, share
671 * the usb device with the keyboard. Since keyboards are usually
672 * handled by the HID system, the device ends up being handled by two
673 * modules. Setting up the device therefore becomes slightly
674 * complicated. To enable multitouch features, a mode switch is
675 * required, which is usually applied via the control interface of the
676 * device. It can be argued where this switch should take place. In
677 * some drivers, like appletouch, the switch is made during
678 * probe. However, the hid module may also alter the state of the
679 * device, resulting in trackpad malfunction under certain
680 * circumstances. To get around this problem, there is at least one
681 * example that utilizes the USB_QUIRK_RESET_RESUME quirk in order to
682 * receive a reset_resume request rather than the normal resume.
683 * Since the implementation of reset_resume is equal to mode switch
684 * plus start_traffic, it seems easier to always do the switch when
685 * starting traffic on the device.
687 static int bcm5974_start_traffic(struct bcm5974 *dev)
689 int error;
691 error = bcm5974_wellspring_mode(dev, true);
692 if (error) {
693 dprintk(1, "bcm5974: mode switch failed\n");
694 goto err_out;
697 error = usb_submit_urb(dev->bt_urb, GFP_KERNEL);
698 if (error)
699 goto err_reset_mode;
701 error = usb_submit_urb(dev->tp_urb, GFP_KERNEL);
702 if (error)
703 goto err_kill_bt;
705 return 0;
707 err_kill_bt:
708 usb_kill_urb(dev->bt_urb);
709 err_reset_mode:
710 bcm5974_wellspring_mode(dev, false);
711 err_out:
712 return error;
715 static void bcm5974_pause_traffic(struct bcm5974 *dev)
717 usb_kill_urb(dev->tp_urb);
718 usb_kill_urb(dev->bt_urb);
719 bcm5974_wellspring_mode(dev, false);
723 * The code below implements open/close and manual suspend/resume.
724 * All functions may be called in random order.
726 * Opening a suspended device fails with EACCES - permission denied.
728 * Failing a resume leaves the device resumed but closed.
730 static int bcm5974_open(struct input_dev *input)
732 struct bcm5974 *dev = input_get_drvdata(input);
733 int error;
735 error = usb_autopm_get_interface(dev->intf);
736 if (error)
737 return error;
739 mutex_lock(&dev->pm_mutex);
741 error = bcm5974_start_traffic(dev);
742 if (!error)
743 dev->opened = 1;
745 mutex_unlock(&dev->pm_mutex);
747 if (error)
748 usb_autopm_put_interface(dev->intf);
750 return error;
753 static void bcm5974_close(struct input_dev *input)
755 struct bcm5974 *dev = input_get_drvdata(input);
757 mutex_lock(&dev->pm_mutex);
759 bcm5974_pause_traffic(dev);
760 dev->opened = 0;
762 mutex_unlock(&dev->pm_mutex);
764 usb_autopm_put_interface(dev->intf);
767 static int bcm5974_suspend(struct usb_interface *iface, pm_message_t message)
769 struct bcm5974 *dev = usb_get_intfdata(iface);
771 mutex_lock(&dev->pm_mutex);
773 if (dev->opened)
774 bcm5974_pause_traffic(dev);
776 mutex_unlock(&dev->pm_mutex);
778 return 0;
781 static int bcm5974_resume(struct usb_interface *iface)
783 struct bcm5974 *dev = usb_get_intfdata(iface);
784 int error = 0;
786 mutex_lock(&dev->pm_mutex);
788 if (dev->opened)
789 error = bcm5974_start_traffic(dev);
791 mutex_unlock(&dev->pm_mutex);
793 return error;
796 static int bcm5974_probe(struct usb_interface *iface,
797 const struct usb_device_id *id)
799 struct usb_device *udev = interface_to_usbdev(iface);
800 const struct bcm5974_config *cfg;
801 struct bcm5974 *dev;
802 struct input_dev *input_dev;
803 int error = -ENOMEM;
805 /* find the product index */
806 cfg = bcm5974_get_config(udev);
808 /* allocate memory for our device state and initialize it */
809 dev = kzalloc(sizeof(struct bcm5974), GFP_KERNEL);
810 input_dev = input_allocate_device();
811 if (!dev || !input_dev) {
812 err("bcm5974: out of memory");
813 goto err_free_devs;
816 dev->udev = udev;
817 dev->intf = iface;
818 dev->input = input_dev;
819 dev->cfg = *cfg;
820 mutex_init(&dev->pm_mutex);
822 /* setup urbs */
823 dev->bt_urb = usb_alloc_urb(0, GFP_KERNEL);
824 if (!dev->bt_urb)
825 goto err_free_devs;
827 dev->tp_urb = usb_alloc_urb(0, GFP_KERNEL);
828 if (!dev->tp_urb)
829 goto err_free_bt_urb;
831 dev->bt_data = usb_alloc_coherent(dev->udev,
832 dev->cfg.bt_datalen, GFP_KERNEL,
833 &dev->bt_urb->transfer_dma);
834 if (!dev->bt_data)
835 goto err_free_urb;
837 dev->tp_data = usb_alloc_coherent(dev->udev,
838 dev->cfg.tp_datalen, GFP_KERNEL,
839 &dev->tp_urb->transfer_dma);
840 if (!dev->tp_data)
841 goto err_free_bt_buffer;
843 usb_fill_int_urb(dev->bt_urb, udev,
844 usb_rcvintpipe(udev, cfg->bt_ep),
845 dev->bt_data, dev->cfg.bt_datalen,
846 bcm5974_irq_button, dev, 1);
848 usb_fill_int_urb(dev->tp_urb, udev,
849 usb_rcvintpipe(udev, cfg->tp_ep),
850 dev->tp_data, dev->cfg.tp_datalen,
851 bcm5974_irq_trackpad, dev, 1);
853 /* create bcm5974 device */
854 usb_make_path(udev, dev->phys, sizeof(dev->phys));
855 strlcat(dev->phys, "/input0", sizeof(dev->phys));
857 input_dev->name = "bcm5974";
858 input_dev->phys = dev->phys;
859 usb_to_input_id(dev->udev, &input_dev->id);
860 /* report driver capabilities via the version field */
861 input_dev->id.version = cfg->caps;
862 input_dev->dev.parent = &iface->dev;
864 input_set_drvdata(input_dev, dev);
866 input_dev->open = bcm5974_open;
867 input_dev->close = bcm5974_close;
869 setup_events_to_report(input_dev, cfg);
871 error = input_register_device(dev->input);
872 if (error)
873 goto err_free_buffer;
875 /* save our data pointer in this interface device */
876 usb_set_intfdata(iface, dev);
878 return 0;
880 err_free_buffer:
881 usb_free_coherent(dev->udev, dev->cfg.tp_datalen,
882 dev->tp_data, dev->tp_urb->transfer_dma);
883 err_free_bt_buffer:
884 usb_free_coherent(dev->udev, dev->cfg.bt_datalen,
885 dev->bt_data, dev->bt_urb->transfer_dma);
886 err_free_urb:
887 usb_free_urb(dev->tp_urb);
888 err_free_bt_urb:
889 usb_free_urb(dev->bt_urb);
890 err_free_devs:
891 usb_set_intfdata(iface, NULL);
892 input_free_device(input_dev);
893 kfree(dev);
894 return error;
897 static void bcm5974_disconnect(struct usb_interface *iface)
899 struct bcm5974 *dev = usb_get_intfdata(iface);
901 usb_set_intfdata(iface, NULL);
903 input_unregister_device(dev->input);
904 usb_free_coherent(dev->udev, dev->cfg.tp_datalen,
905 dev->tp_data, dev->tp_urb->transfer_dma);
906 usb_free_coherent(dev->udev, dev->cfg.bt_datalen,
907 dev->bt_data, dev->bt_urb->transfer_dma);
908 usb_free_urb(dev->tp_urb);
909 usb_free_urb(dev->bt_urb);
910 kfree(dev);
913 static struct usb_driver bcm5974_driver = {
914 .name = "bcm5974",
915 .probe = bcm5974_probe,
916 .disconnect = bcm5974_disconnect,
917 .suspend = bcm5974_suspend,
918 .resume = bcm5974_resume,
919 .id_table = bcm5974_table,
920 .supports_autosuspend = 1,
923 static int __init bcm5974_init(void)
925 return usb_register(&bcm5974_driver);
928 static void __exit bcm5974_exit(void)
930 usb_deregister(&bcm5974_driver);
933 module_init(bcm5974_init);
934 module_exit(bcm5974_exit);