HID: apple: Add support for the 2013 Macbook Air
[deliverable/linux.git] / drivers / input / mouse / bcm5974.c
CommitLineData
f89bd95c
HR
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>
51c80b74 43#include <linux/input/mt.h>
f89bd95c
HR
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
158e9287
HR
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
6021afcf
EH
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
47340bd9
AB
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
1c601bea
PAVM
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
db0b34b0
JD
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
c331eb58
AD
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
3dde22a9
HR
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
8d80da90
DH
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
f89bd95c
HR
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 */
158e0fb6 103static const struct usb_device_id bcm5974_table[] = {
f89bd95c
HR
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),
158e9287
HR
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),
6021afcf
EH
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),
47340bd9
AB
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),
1c601bea
PAVM
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),
db0b34b0
JD
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),
c331eb58
AD
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),
3dde22a9
HR
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),
8d80da90
DH
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),
f89bd95c
HR
148 /* Terminating entry */
149 {}
150};
151MODULE_DEVICE_TABLE(usb, bcm5974_table);
152
153MODULE_AUTHOR("Henrik Rydberg");
154MODULE_DESCRIPTION("Apple USB BCM5974 multitouch driver");
155MODULE_LICENSE("GPL");
156
157#define dprintk(level, format, a...)\
158 { if (debug >= level) printk(KERN_DEBUG format, ##a); }
159
160static int debug = 1;
161module_param(debug, int, 0644);
162MODULE_PARM_DESC(debug, "Activate debugging output");
163
164/* button data structure */
165struct 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
9894cf0f
HR
172/* trackpad header types */
173enum tp_type {
158e9287
HR
174 TYPE1, /* plain trackpad */
175 TYPE2 /* button integrated in trackpad */
f89bd95c
HR
176};
177
9894cf0f
HR
178/* trackpad finger data offsets, le16-aligned */
179#define FINGER_TYPE1 (13 * sizeof(__le16))
158e9287
HR
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
9894cf0f
HR
187
188/* trackpad finger structure, le16-aligned */
f89bd95c 189struct tp_finger {
75e21e3f 190 __le16 origin; /* zero when switching track finger */
f89bd95c
HR
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 */
f17953ab
HR
195 __le16 tool_major; /* tool area, major axis */
196 __le16 tool_minor; /* tool area, minor axis */
f89bd95c 197 __le16 orientation; /* 16384 when point, else 15 bit angle */
f17953ab
HR
198 __le16 touch_major; /* touch area, major axis */
199 __le16 touch_minor; /* touch area, minor axis */
f89bd95c
HR
200 __le16 unused[3]; /* zeros */
201 __le16 multi; /* one finger: varies, more fingers: constant */
9894cf0f 202} __attribute__((packed,aligned(2)));
f89bd95c 203
9894cf0f 204/* trackpad finger data size, empirically at least ten fingers */
f17953ab 205#define MAX_FINGERS 16
9894cf0f 206#define SIZEOF_FINGER sizeof(struct tp_finger)
f17953ab 207#define SIZEOF_ALL_FINGERS (MAX_FINGERS * SIZEOF_FINGER)
6f2701b7 208#define MAX_FINGER_ORIENTATION 16384
f89bd95c
HR
209
210/* device-specific parameters */
211struct bcm5974_param {
0e726966
HR
212 int snratio; /* signal-to-noise ratio */
213 int min; /* device minimum reading */
214 int max; /* device maximum reading */
f89bd95c
HR
215};
216
217/* device-specific configuration */
218struct bcm5974_config {
219 int ansi, iso, jis; /* the product id of this device */
158e9287 220 int caps; /* device capability bitmask */
f89bd95c
HR
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 */
9894cf0f
HR
224 enum tp_type tp_type; /* type of trackpad interface */
225 int tp_offset; /* offset to trackpad finger data */
f89bd95c
HR
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 */
0e726966 231 struct bcm5974_param o; /* orientation limits */
f89bd95c
HR
232};
233
234/* logical device structure */
235struct bcm5974 {
236 char phys[64];
237 struct usb_device *udev; /* usb device */
88da765f 238 struct usb_interface *intf; /* our interface */
f89bd95c
HR
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 */
9894cf0f 246 u8 *tp_data; /* trackpad transferred data */
51c80b74
HR
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 */
f89bd95c
HR
250};
251
f89bd95c
HR
252/* logical signal quality */
253#define SN_PRESSURE 45 /* pressure signal-to-noise ratio */
51c80b74 254#define SN_WIDTH 25 /* width signal-to-noise ratio */
f89bd95c 255#define SN_COORD 250 /* coordinate signal-to-noise ratio */
0e726966 256#define SN_ORIENT 10 /* orientation signal-to-noise ratio */
75e21e3f 257
f89bd95c
HR
258/* device constants */
259static 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,
158e9287 264 0,
f89bd95c 265 0x84, sizeof(struct bt_data),
9894cf0f 266 0x81, TYPE1, FINGER_TYPE1, FINGER_TYPE1 + SIZEOF_ALL_FINGERS,
0e726966
HR
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 }
f89bd95c
HR
272 },
273 {
274 USB_DEVICE_ID_APPLE_WELLSPRING2_ANSI,
275 USB_DEVICE_ID_APPLE_WELLSPRING2_ISO,
276 USB_DEVICE_ID_APPLE_WELLSPRING2_JIS,
158e9287 277 0,
f89bd95c 278 0x84, sizeof(struct bt_data),
9894cf0f 279 0x81, TYPE1, FINGER_TYPE1, FINGER_TYPE1 + SIZEOF_ALL_FINGERS,
0e726966
HR
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 }
f89bd95c 285 },
158e9287
HR
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,
0e726966
HR
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 }
158e9287 298 },
6021afcf
EH
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,
0e726966
HR
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 }
6021afcf
EH
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,
0e726966
HR
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 }
6021afcf 324 },
47340bd9
AB
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,
0e726966
HR
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 }
47340bd9 337 },
db0b34b0
JD
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,
0e726966
HR
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 }
db0b34b0 350 },
c331eb58
AD
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,
0e726966
HR
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 }
c331eb58 363 },
1c601bea
PAVM
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,
0e726966
HR
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 }
1c601bea 376 },
3dde22a9
HR
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,
0e726966
HR
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 }
3dde22a9 389 },
8d80da90
DH
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 },
f89bd95c
HR
403 {}
404};
405
406/* return the device-specific configuration by device */
407static 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 */
420static inline int raw2int(__le16 x)
421{
422 return (signed short)le16_to_cpu(x);
423}
424
0e726966
HR
425static void set_abs(struct input_dev *input, unsigned int code,
426 const struct bcm5974_param *p)
f89bd95c 427{
0e726966
HR
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);
f89bd95c
HR
430}
431
432/* setup which logical events to report */
433static 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
0e726966
HR
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
6f2701b7 442 /* finger touch area */
0e726966
HR
443 set_abs(input_dev, ABS_MT_TOUCH_MAJOR, &cfg->w);
444 set_abs(input_dev, ABS_MT_TOUCH_MINOR, &cfg->w);
6f2701b7 445 /* finger approach area */
0e726966
HR
446 set_abs(input_dev, ABS_MT_WIDTH_MAJOR, &cfg->w);
447 set_abs(input_dev, ABS_MT_WIDTH_MINOR, &cfg->w);
6f2701b7 448 /* finger orientation */
0e726966 449 set_abs(input_dev, ABS_MT_ORIENTATION, &cfg->o);
6f2701b7 450 /* finger position */
0e726966
HR
451 set_abs(input_dev, ABS_MT_POSITION_X, &cfg->x);
452 set_abs(input_dev, ABS_MT_POSITION_Y, &cfg->y);
6f2701b7 453
f89bd95c 454 __set_bit(EV_KEY, input_dev->evbit);
f89bd95c 455 __set_bit(BTN_LEFT, input_dev->keybit);
c13aea03 456
52965cc0
JP
457 if (cfg->caps & HAS_INTEGRATED_BUTTON)
458 __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
459
51c80b74
HR
460 input_mt_init_slots(input_dev, MAX_FINGERS,
461 INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED | INPUT_MT_TRACK);
f89bd95c
HR
462}
463
464/* report button data as logical button state */
465static int report_bt_state(struct bcm5974 *dev, int size)
466{
467 if (size != sizeof(struct bt_data))
468 return -EIO;
469
53402193
HR
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
f89bd95c
HR
475 input_report_key(dev->input, BTN_LEFT, dev->bt_data->button);
476 input_sync(dev->input);
477
478 return 0;
479}
480
51c80b74
HR
481static void report_finger_data(struct input_dev *input, int slot,
482 const struct input_mt_pos *pos,
6f2701b7
HR
483 const struct tp_finger *f)
484{
51c80b74
HR
485 input_mt_slot(input, slot);
486 input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
487
57157bec 488 input_report_abs(input, ABS_MT_TOUCH_MAJOR,
f17953ab 489 raw2int(f->touch_major) << 1);
57157bec 490 input_report_abs(input, ABS_MT_TOUCH_MINOR,
f17953ab 491 raw2int(f->touch_minor) << 1);
57157bec 492 input_report_abs(input, ABS_MT_WIDTH_MAJOR,
f17953ab 493 raw2int(f->tool_major) << 1);
57157bec 494 input_report_abs(input, ABS_MT_WIDTH_MINOR,
f17953ab 495 raw2int(f->tool_minor) << 1);
6f2701b7
HR
496 input_report_abs(input, ABS_MT_ORIENTATION,
497 MAX_FINGER_ORIENTATION - raw2int(f->orientation));
51c80b74
HR
498 input_report_abs(input, ABS_MT_POSITION_X, pos->x);
499 input_report_abs(input, ABS_MT_POSITION_Y, pos->y);
6f2701b7
HR
500}
501
0e726966
HR
502static 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
f89bd95c
HR
521/* report trackpad data as logical trackpad state */
522static int report_tp_state(struct bcm5974 *dev, int size)
523{
524 const struct bcm5974_config *c = &dev->cfg;
9894cf0f 525 const struct tp_finger *f;
f89bd95c 526 struct input_dev *input = dev->input;
51c80b74 527 int raw_n, i, n = 0;
f89bd95c 528
9894cf0f 529 if (size < c->tp_offset || (size - c->tp_offset) % SIZEOF_FINGER != 0)
f89bd95c
HR
530 return -EIO;
531
9894cf0f
HR
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
51c80b74
HR
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];
f89bd95c
HR
542 }
543
51c80b74 544 input_mt_assign_slots(input, dev->slots, dev->pos, n);
75e21e3f 545
51c80b74
HR
546 for (i = 0; i < n; i++)
547 report_finger_data(input, dev->slots[i],
548 &dev->pos[i], dev->index[i]);
f89bd95c 549
51c80b74
HR
550 input_mt_sync_frame(input);
551
552 report_synaptics_data(input, c, f, raw_n);
f89bd95c 553
158e9287 554 /* type 2 reports button events via ibt only */
0e726966
HR
555 if (c->tp_type == TYPE2) {
556 int ibt = raw2int(dev->tp_data[BUTTON_TYPE2]);
158e9287 557 input_report_key(input, BTN_LEFT, ibt);
0e726966 558 }
158e9287 559
f89bd95c
HR
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
cd72ad3f 571#define BCM5974_WELLSPRING_MODE_NORMAL_VALUE 0x08
f89bd95c 572
cd72ad3f 573static int bcm5974_wellspring_mode(struct bcm5974 *dev, bool on)
f89bd95c
HR
574{
575 char *data = kmalloc(8, GFP_KERNEL);
576 int retval = 0, size;
577
578 if (!data) {
ab943ca8 579 dev_err(&dev->intf->dev, "out of memory\n");
f89bd95c
HR
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) {
ab943ca8 592 dev_err(&dev->intf->dev, "could not read from device\n");
f89bd95c
HR
593 retval = -EIO;
594 goto out;
595 }
596
597 /* apply the mode switch */
cd72ad3f
HR
598 data[0] = on ?
599 BCM5974_WELLSPRING_MODE_VENDOR_VALUE :
600 BCM5974_WELLSPRING_MODE_NORMAL_VALUE;
f89bd95c
HR
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) {
ab943ca8 610 dev_err(&dev->intf->dev, "could not write to device\n");
f89bd95c
HR
611 retval = -EIO;
612 goto out;
613 }
614
cd72ad3f
HR
615 dprintk(2, "bcm5974: switched to %s mode.\n",
616 on ? "wellspring" : "normal");
f89bd95c
HR
617
618 out:
619 kfree(data);
620 return retval;
621}
622
623static void bcm5974_irq_button(struct urb *urb)
624{
625 struct bcm5974 *dev = urb->context;
ab943ca8 626 struct usb_interface *intf = dev->intf;
f89bd95c
HR
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:
ab943ca8 636 dev_dbg(&intf->dev, "button urb shutting down: %d\n",
bd028769 637 urb->status);
f89bd95c
HR
638 return;
639 default:
ab943ca8 640 dev_dbg(&intf->dev, "button urb status: %d\n", urb->status);
f89bd95c
HR
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
648exit:
649 error = usb_submit_urb(dev->bt_urb, GFP_ATOMIC);
650 if (error)
ab943ca8 651 dev_err(&intf->dev, "button urb failed: %d\n", error);
f89bd95c
HR
652}
653
654static void bcm5974_irq_trackpad(struct urb *urb)
655{
656 struct bcm5974 *dev = urb->context;
ab943ca8 657 struct usb_interface *intf = dev->intf;
f89bd95c
HR
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:
ab943ca8 667 dev_dbg(&intf->dev, "trackpad urb shutting down: %d\n",
bd028769 668 urb->status);
f89bd95c
HR
669 return;
670 default:
ab943ca8 671 dev_dbg(&intf->dev, "trackpad urb status: %d\n", urb->status);
f89bd95c
HR
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
683exit:
684 error = usb_submit_urb(dev->tp_urb, GFP_ATOMIC);
685 if (error)
ab943ca8 686 dev_err(&intf->dev, "trackpad urb failed: %d\n", error);
f89bd95c
HR
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
25985edc 702 * receive a reset_resume request rather than the normal resume.
f89bd95c
HR
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 */
707static int bcm5974_start_traffic(struct bcm5974 *dev)
708{
1719ec41
LJ
709 int error;
710
711 error = bcm5974_wellspring_mode(dev, true);
712 if (error) {
f89bd95c 713 dprintk(1, "bcm5974: mode switch failed\n");
1719ec41 714 goto err_out;
f89bd95c
HR
715 }
716
43f482b4
HR
717 if (dev->bt_urb) {
718 error = usb_submit_urb(dev->bt_urb, GFP_KERNEL);
719 if (error)
720 goto err_reset_mode;
721 }
f89bd95c 722
1719ec41
LJ
723 error = usb_submit_urb(dev->tp_urb, GFP_KERNEL);
724 if (error)
f89bd95c
HR
725 goto err_kill_bt;
726
727 return 0;
728
729err_kill_bt:
730 usb_kill_urb(dev->bt_urb);
1719ec41
LJ
731err_reset_mode:
732 bcm5974_wellspring_mode(dev, false);
733err_out:
734 return error;
f89bd95c
HR
735}
736
737static void bcm5974_pause_traffic(struct bcm5974 *dev)
738{
739 usb_kill_urb(dev->tp_urb);
740 usb_kill_urb(dev->bt_urb);
cd72ad3f 741 bcm5974_wellspring_mode(dev, false);
f89bd95c
HR
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 */
752static int bcm5974_open(struct input_dev *input)
753{
754 struct bcm5974 *dev = input_get_drvdata(input);
755 int error;
756
88da765f
DT
757 error = usb_autopm_get_interface(dev->intf);
758 if (error)
759 return error;
760
f89bd95c
HR
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
88da765f
DT
769 if (error)
770 usb_autopm_put_interface(dev->intf);
771
f89bd95c
HR
772 return error;
773}
774
775static 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);
88da765f
DT
785
786 usb_autopm_put_interface(dev->intf);
f89bd95c
HR
787}
788
789static 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
803static 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
818static 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) {
6c1d1b24 834 dev_err(&iface->dev, "out of memory\n");
f89bd95c
HR
835 goto err_free_devs;
836 }
837
838 dev->udev = udev;
88da765f 839 dev->intf = iface;
f89bd95c
HR
840 dev->input = input_dev;
841 dev->cfg = *cfg;
842 mutex_init(&dev->pm_mutex);
843
844 /* setup urbs */
43f482b4
HR
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 }
f89bd95c
HR
850
851 dev->tp_urb = usb_alloc_urb(0, GFP_KERNEL);
852 if (!dev->tp_urb)
853 goto err_free_bt_urb;
854
43f482b4
HR
855 if (dev->bt_urb) {
856 dev->bt_data = usb_alloc_coherent(dev->udev,
997ea58e
DM
857 dev->cfg.bt_datalen, GFP_KERNEL,
858 &dev->bt_urb->transfer_dma);
43f482b4
HR
859 if (!dev->bt_data)
860 goto err_free_urb;
861 }
f89bd95c 862
997ea58e
DM
863 dev->tp_data = usb_alloc_coherent(dev->udev,
864 dev->cfg.tp_datalen, GFP_KERNEL,
865 &dev->tp_urb->transfer_dma);
f89bd95c
HR
866 if (!dev->tp_data)
867 goto err_free_bt_buffer;
868
43f482b4
HR
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);
f89bd95c
HR
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);
158e9287
HR
887 /* report driver capabilities via the version field */
888 input_dev->id.version = cfg->caps;
f89bd95c
HR
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
907err_free_buffer:
997ea58e 908 usb_free_coherent(dev->udev, dev->cfg.tp_datalen,
f89bd95c
HR
909 dev->tp_data, dev->tp_urb->transfer_dma);
910err_free_bt_buffer:
43f482b4
HR
911 if (dev->bt_urb)
912 usb_free_coherent(dev->udev, dev->cfg.bt_datalen,
913 dev->bt_data, dev->bt_urb->transfer_dma);
f89bd95c
HR
914err_free_urb:
915 usb_free_urb(dev->tp_urb);
916err_free_bt_urb:
917 usb_free_urb(dev->bt_urb);
918err_free_devs:
919 usb_set_intfdata(iface, NULL);
920 input_free_device(input_dev);
921 kfree(dev);
922 return error;
923}
924
925static 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);
997ea58e
DM
932 usb_free_coherent(dev->udev, dev->cfg.tp_datalen,
933 dev->tp_data, dev->tp_urb->transfer_dma);
43f482b4
HR
934 if (dev->bt_urb)
935 usb_free_coherent(dev->udev, dev->cfg.bt_datalen,
936 dev->bt_data, dev->bt_urb->transfer_dma);
f89bd95c
HR
937 usb_free_urb(dev->tp_urb);
938 usb_free_urb(dev->bt_urb);
939 kfree(dev);
940}
941
942static struct usb_driver bcm5974_driver = {
943 .name = "bcm5974",
944 .probe = bcm5974_probe,
945 .disconnect = bcm5974_disconnect,
946 .suspend = bcm5974_suspend,
947 .resume = bcm5974_resume,
f89bd95c 948 .id_table = bcm5974_table,
88da765f 949 .supports_autosuspend = 1,
f89bd95c
HR
950};
951
08642e7c 952module_usb_driver(bcm5974_driver);
This page took 0.252694 seconds and 5 git commands to generate.