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