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