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