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