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