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