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