2 * Apple USB BCM5974 (Macbook Air and Penryn Macbook Pro) multitouch driver
4 * Copyright (C) 2008 Henrik Rydberg (rydberg@euromail.se)
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)
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)
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.
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.
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.
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 #include <linux/input/mt.h>
45 #define USB_VENDOR_ID_APPLE 0x05ac
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
88 #define BCM5974_DEVICE(prod) { \
89 .match_flags = (USB_DEVICE_ID_MATCH_DEVICE | \
90 USB_DEVICE_ID_MATCH_INT_CLASS | \
91 USB_DEVICE_ID_MATCH_INT_PROTOCOL), \
92 .idVendor = USB_VENDOR_ID_APPLE, \
93 .idProduct = (prod), \
94 .bInterfaceClass = USB_INTERFACE_CLASS_HID, \
95 .bInterfaceProtocol = USB_INTERFACE_PROTOCOL_MOUSE \
98 /* table of devices that work with this driver */
99 static const struct usb_device_id bcm5974_table[] = {
101 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING_ANSI),
102 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING_ISO),
103 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING_JIS),
104 /* MacbookProPenryn */
105 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING2_ANSI),
106 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING2_ISO),
107 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING2_JIS),
109 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING3_ANSI),
110 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING3_ISO),
111 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING3_JIS),
113 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING4_ANSI),
114 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING4_ISO),
115 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING4_JIS),
117 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING4A_ANSI),
118 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING4A_ISO),
119 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING4A_JIS),
121 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING5_ANSI),
122 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING5_ISO),
123 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING5_JIS),
125 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING6A_ANSI),
126 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING6A_ISO),
127 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING6A_JIS),
129 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING6_ANSI),
130 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING6_ISO),
131 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING6_JIS),
133 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING5A_ANSI),
134 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING5A_ISO),
135 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING5A_JIS),
137 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING7_ANSI),
138 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING7_ISO),
139 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING7_JIS),
140 /* Terminating entry */
143 MODULE_DEVICE_TABLE(usb, bcm5974_table);
145 MODULE_AUTHOR("Henrik Rydberg");
146 MODULE_DESCRIPTION("Apple USB BCM5974 multitouch driver");
147 MODULE_LICENSE("GPL");
149 #define dprintk(level, format, a...)\
150 { if (debug >= level) printk(KERN_DEBUG format, ##a); }
152 static int debug = 1;
153 module_param(debug, int, 0644);
154 MODULE_PARM_DESC(debug, "Activate debugging output");
156 /* button data structure */
158 u8 unknown1; /* constant */
159 u8 button; /* left button */
160 u8 rel_x; /* relative x coordinate */
161 u8 rel_y; /* relative y coordinate */
164 /* trackpad header types */
166 TYPE1, /* plain trackpad */
167 TYPE2 /* button integrated in trackpad */
170 /* trackpad finger data offsets, le16-aligned */
171 #define FINGER_TYPE1 (13 * sizeof(__le16))
172 #define FINGER_TYPE2 (15 * sizeof(__le16))
174 /* trackpad button data offsets */
175 #define BUTTON_TYPE2 15
177 /* list of device capability bits */
178 #define HAS_INTEGRATED_BUTTON 1
180 /* trackpad finger structure, le16-aligned */
182 __le16 origin; /* zero when switching track finger */
183 __le16 abs_x; /* absolute x coodinate */
184 __le16 abs_y; /* absolute y coodinate */
185 __le16 rel_x; /* relative x coodinate */
186 __le16 rel_y; /* relative y coodinate */
187 __le16 tool_major; /* tool area, major axis */
188 __le16 tool_minor; /* tool area, minor axis */
189 __le16 orientation; /* 16384 when point, else 15 bit angle */
190 __le16 touch_major; /* touch area, major axis */
191 __le16 touch_minor; /* touch area, minor axis */
192 __le16 unused[3]; /* zeros */
193 __le16 multi; /* one finger: varies, more fingers: constant */
194 } __attribute__((packed,aligned(2)));
196 /* trackpad finger data size, empirically at least ten fingers */
197 #define MAX_FINGERS 16
198 #define SIZEOF_FINGER sizeof(struct tp_finger)
199 #define SIZEOF_ALL_FINGERS (MAX_FINGERS * SIZEOF_FINGER)
200 #define MAX_FINGER_ORIENTATION 16384
202 /* device-specific parameters */
203 struct bcm5974_param {
204 int snratio; /* signal-to-noise ratio */
205 int min; /* device minimum reading */
206 int max; /* device maximum reading */
209 /* device-specific configuration */
210 struct bcm5974_config {
211 int ansi, iso, jis; /* the product id of this device */
212 int caps; /* device capability bitmask */
213 int bt_ep; /* the endpoint of the button interface */
214 int bt_datalen; /* data length of the button interface */
215 int tp_ep; /* the endpoint of the trackpad interface */
216 enum tp_type tp_type; /* type of trackpad interface */
217 int tp_offset; /* offset to trackpad finger data */
218 int tp_datalen; /* data length of the trackpad interface */
219 struct bcm5974_param p; /* finger pressure limits */
220 struct bcm5974_param w; /* finger width limits */
221 struct bcm5974_param x; /* horizontal limits */
222 struct bcm5974_param y; /* vertical limits */
223 struct bcm5974_param o; /* orientation limits */
226 /* logical device structure */
229 struct usb_device *udev; /* usb device */
230 struct usb_interface *intf; /* our interface */
231 struct input_dev *input; /* input dev */
232 struct bcm5974_config cfg; /* device configuration */
233 struct mutex pm_mutex; /* serialize access to open/suspend */
234 int opened; /* 1: opened, 0: closed */
235 struct urb *bt_urb; /* button usb request block */
236 struct bt_data *bt_data; /* button transferred data */
237 struct urb *tp_urb; /* trackpad usb request block */
238 u8 *tp_data; /* trackpad transferred data */
239 const struct tp_finger *index[MAX_FINGERS]; /* finger index data */
240 struct input_mt_pos pos[MAX_FINGERS]; /* position array */
241 int slots[MAX_FINGERS]; /* slot assignments */
244 /* logical signal quality */
245 #define SN_PRESSURE 45 /* pressure signal-to-noise ratio */
246 #define SN_WIDTH 25 /* width signal-to-noise ratio */
247 #define SN_COORD 250 /* coordinate signal-to-noise ratio */
248 #define SN_ORIENT 10 /* orientation signal-to-noise ratio */
250 /* device constants */
251 static const struct bcm5974_config bcm5974_config_table[] = {
253 USB_DEVICE_ID_APPLE_WELLSPRING_ANSI,
254 USB_DEVICE_ID_APPLE_WELLSPRING_ISO,
255 USB_DEVICE_ID_APPLE_WELLSPRING_JIS,
257 0x84, sizeof(struct bt_data),
258 0x81, TYPE1, FINGER_TYPE1, FINGER_TYPE1 + SIZEOF_ALL_FINGERS,
259 { SN_PRESSURE, 0, 256 },
260 { SN_WIDTH, 0, 2048 },
261 { SN_COORD, -4824, 5342 },
262 { SN_COORD, -172, 5820 },
263 { SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION }
266 USB_DEVICE_ID_APPLE_WELLSPRING2_ANSI,
267 USB_DEVICE_ID_APPLE_WELLSPRING2_ISO,
268 USB_DEVICE_ID_APPLE_WELLSPRING2_JIS,
270 0x84, sizeof(struct bt_data),
271 0x81, TYPE1, FINGER_TYPE1, FINGER_TYPE1 + SIZEOF_ALL_FINGERS,
272 { SN_PRESSURE, 0, 256 },
273 { SN_WIDTH, 0, 2048 },
274 { SN_COORD, -4824, 4824 },
275 { SN_COORD, -172, 4290 },
276 { SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION }
279 USB_DEVICE_ID_APPLE_WELLSPRING3_ANSI,
280 USB_DEVICE_ID_APPLE_WELLSPRING3_ISO,
281 USB_DEVICE_ID_APPLE_WELLSPRING3_JIS,
282 HAS_INTEGRATED_BUTTON,
283 0x84, sizeof(struct bt_data),
284 0x81, TYPE2, FINGER_TYPE2, FINGER_TYPE2 + SIZEOF_ALL_FINGERS,
285 { SN_PRESSURE, 0, 300 },
286 { SN_WIDTH, 0, 2048 },
287 { SN_COORD, -4460, 5166 },
288 { SN_COORD, -75, 6700 },
289 { SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION }
292 USB_DEVICE_ID_APPLE_WELLSPRING4_ANSI,
293 USB_DEVICE_ID_APPLE_WELLSPRING4_ISO,
294 USB_DEVICE_ID_APPLE_WELLSPRING4_JIS,
295 HAS_INTEGRATED_BUTTON,
296 0x84, sizeof(struct bt_data),
297 0x81, TYPE2, FINGER_TYPE2, FINGER_TYPE2 + SIZEOF_ALL_FINGERS,
298 { SN_PRESSURE, 0, 300 },
299 { SN_WIDTH, 0, 2048 },
300 { SN_COORD, -4620, 5140 },
301 { SN_COORD, -150, 6600 },
302 { SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION }
305 USB_DEVICE_ID_APPLE_WELLSPRING4A_ANSI,
306 USB_DEVICE_ID_APPLE_WELLSPRING4A_ISO,
307 USB_DEVICE_ID_APPLE_WELLSPRING4A_JIS,
308 HAS_INTEGRATED_BUTTON,
309 0x84, sizeof(struct bt_data),
310 0x81, TYPE2, FINGER_TYPE2, FINGER_TYPE2 + SIZEOF_ALL_FINGERS,
311 { SN_PRESSURE, 0, 300 },
312 { SN_WIDTH, 0, 2048 },
313 { SN_COORD, -4616, 5112 },
314 { SN_COORD, -142, 5234 },
315 { SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION }
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 { SN_PRESSURE, 0, 300 },
325 { SN_WIDTH, 0, 2048 },
326 { SN_COORD, -4415, 5050 },
327 { SN_COORD, -55, 6680 },
328 { SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION }
331 USB_DEVICE_ID_APPLE_WELLSPRING6_ANSI,
332 USB_DEVICE_ID_APPLE_WELLSPRING6_ISO,
333 USB_DEVICE_ID_APPLE_WELLSPRING6_JIS,
334 HAS_INTEGRATED_BUTTON,
335 0x84, sizeof(struct bt_data),
336 0x81, TYPE2, FINGER_TYPE2, FINGER_TYPE2 + SIZEOF_ALL_FINGERS,
337 { SN_PRESSURE, 0, 300 },
338 { SN_WIDTH, 0, 2048 },
339 { SN_COORD, -4620, 5140 },
340 { SN_COORD, -150, 6600 },
341 { SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION }
344 USB_DEVICE_ID_APPLE_WELLSPRING5A_ANSI,
345 USB_DEVICE_ID_APPLE_WELLSPRING5A_ISO,
346 USB_DEVICE_ID_APPLE_WELLSPRING5A_JIS,
347 HAS_INTEGRATED_BUTTON,
348 0x84, sizeof(struct bt_data),
349 0x81, TYPE2, FINGER_TYPE2, FINGER_TYPE2 + SIZEOF_ALL_FINGERS,
350 { SN_PRESSURE, 0, 300 },
351 { SN_WIDTH, 0, 2048 },
352 { SN_COORD, -4750, 5280 },
353 { SN_COORD, -150, 6730 },
354 { SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION }
357 USB_DEVICE_ID_APPLE_WELLSPRING6A_ANSI,
358 USB_DEVICE_ID_APPLE_WELLSPRING6A_ISO,
359 USB_DEVICE_ID_APPLE_WELLSPRING6A_JIS,
360 HAS_INTEGRATED_BUTTON,
361 0x84, sizeof(struct bt_data),
362 0x81, TYPE2, FINGER_TYPE2, FINGER_TYPE2 + SIZEOF_ALL_FINGERS,
363 { SN_PRESSURE, 0, 300 },
364 { SN_WIDTH, 0, 2048 },
365 { SN_COORD, -4620, 5140 },
366 { SN_COORD, -150, 6600 },
367 { SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION }
370 USB_DEVICE_ID_APPLE_WELLSPRING7_ANSI,
371 USB_DEVICE_ID_APPLE_WELLSPRING7_ISO,
372 USB_DEVICE_ID_APPLE_WELLSPRING7_JIS,
373 HAS_INTEGRATED_BUTTON,
374 0x84, sizeof(struct bt_data),
375 0x81, TYPE2, FINGER_TYPE2, FINGER_TYPE2 + SIZEOF_ALL_FINGERS,
376 { SN_PRESSURE, 0, 300 },
377 { SN_WIDTH, 0, 2048 },
378 { SN_COORD, -4750, 5280 },
379 { SN_COORD, -150, 6730 },
380 { SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION }
385 /* return the device-specific configuration by device */
386 static const struct bcm5974_config *bcm5974_get_config(struct usb_device *udev)
388 u16 id = le16_to_cpu(udev->descriptor.idProduct);
389 const struct bcm5974_config *cfg;
391 for (cfg = bcm5974_config_table; cfg->ansi; ++cfg)
392 if (cfg->ansi == id || cfg->iso == id || cfg->jis == id)
395 return bcm5974_config_table;
398 /* convert 16-bit little endian to signed integer */
399 static inline int raw2int(__le16 x)
401 return (signed short)le16_to_cpu(x);
404 static void set_abs(struct input_dev *input, unsigned int code,
405 const struct bcm5974_param *p)
407 int fuzz = p->snratio ? (p->max - p->min) / p->snratio : 0;
408 input_set_abs_params(input, code, p->min, p->max, fuzz, 0);
411 /* setup which logical events to report */
412 static void setup_events_to_report(struct input_dev *input_dev,
413 const struct bcm5974_config *cfg)
415 __set_bit(EV_ABS, input_dev->evbit);
417 /* for synaptics only */
418 input_set_abs_params(input_dev, ABS_PRESSURE, 0, 256, 5, 0);
419 input_set_abs_params(input_dev, ABS_TOOL_WIDTH, 0, 16, 0, 0);
421 /* finger touch area */
422 set_abs(input_dev, ABS_MT_TOUCH_MAJOR, &cfg->w);
423 set_abs(input_dev, ABS_MT_TOUCH_MINOR, &cfg->w);
424 /* finger approach area */
425 set_abs(input_dev, ABS_MT_WIDTH_MAJOR, &cfg->w);
426 set_abs(input_dev, ABS_MT_WIDTH_MINOR, &cfg->w);
427 /* finger orientation */
428 set_abs(input_dev, ABS_MT_ORIENTATION, &cfg->o);
429 /* finger position */
430 set_abs(input_dev, ABS_MT_POSITION_X, &cfg->x);
431 set_abs(input_dev, ABS_MT_POSITION_Y, &cfg->y);
433 __set_bit(EV_KEY, input_dev->evbit);
434 __set_bit(BTN_LEFT, input_dev->keybit);
436 if (cfg->caps & HAS_INTEGRATED_BUTTON)
437 __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
439 input_mt_init_slots(input_dev, MAX_FINGERS,
440 INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED | INPUT_MT_TRACK);
443 /* report button data as logical button state */
444 static int report_bt_state(struct bcm5974 *dev, int size)
446 if (size != sizeof(struct bt_data))
450 "bcm5974: button data: %x %x %x %x\n",
451 dev->bt_data->unknown1, dev->bt_data->button,
452 dev->bt_data->rel_x, dev->bt_data->rel_y);
454 input_report_key(dev->input, BTN_LEFT, dev->bt_data->button);
455 input_sync(dev->input);
460 static void report_finger_data(struct input_dev *input, int slot,
461 const struct input_mt_pos *pos,
462 const struct tp_finger *f)
464 input_mt_slot(input, slot);
465 input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
467 input_report_abs(input, ABS_MT_TOUCH_MAJOR,
468 raw2int(f->touch_major) << 1);
469 input_report_abs(input, ABS_MT_TOUCH_MINOR,
470 raw2int(f->touch_minor) << 1);
471 input_report_abs(input, ABS_MT_WIDTH_MAJOR,
472 raw2int(f->tool_major) << 1);
473 input_report_abs(input, ABS_MT_WIDTH_MINOR,
474 raw2int(f->tool_minor) << 1);
475 input_report_abs(input, ABS_MT_ORIENTATION,
476 MAX_FINGER_ORIENTATION - raw2int(f->orientation));
477 input_report_abs(input, ABS_MT_POSITION_X, pos->x);
478 input_report_abs(input, ABS_MT_POSITION_Y, pos->y);
481 static void report_synaptics_data(struct input_dev *input,
482 const struct bcm5974_config *cfg,
483 const struct tp_finger *f, int raw_n)
485 int abs_p = 0, abs_w = 0;
488 int p = raw2int(f->touch_major);
489 int w = raw2int(f->tool_major);
490 if (p > 0 && raw2int(f->origin)) {
491 abs_p = clamp_val(256 * p / cfg->p.max, 0, 255);
492 abs_w = clamp_val(16 * w / cfg->w.max, 0, 15);
496 input_report_abs(input, ABS_PRESSURE, abs_p);
497 input_report_abs(input, ABS_TOOL_WIDTH, abs_w);
500 /* report trackpad data as logical trackpad state */
501 static int report_tp_state(struct bcm5974 *dev, int size)
503 const struct bcm5974_config *c = &dev->cfg;
504 const struct tp_finger *f;
505 struct input_dev *input = dev->input;
508 if (size < c->tp_offset || (size - c->tp_offset) % SIZEOF_FINGER != 0)
511 /* finger data, le16-aligned */
512 f = (const struct tp_finger *)(dev->tp_data + c->tp_offset);
513 raw_n = (size - c->tp_offset) / SIZEOF_FINGER;
515 for (i = 0; i < raw_n; i++) {
516 if (raw2int(f[i].touch_major) == 0)
518 dev->pos[n].x = raw2int(f[i].abs_x);
519 dev->pos[n].y = c->y.min + c->y.max - raw2int(f[i].abs_y);
520 dev->index[n++] = &f[i];
523 input_mt_assign_slots(input, dev->slots, dev->pos, n);
525 for (i = 0; i < n; i++)
526 report_finger_data(input, dev->slots[i],
527 &dev->pos[i], dev->index[i]);
529 input_mt_sync_frame(input);
531 report_synaptics_data(input, c, f, raw_n);
533 /* type 2 reports button events via ibt only */
534 if (c->tp_type == TYPE2) {
535 int ibt = raw2int(dev->tp_data[BUTTON_TYPE2]);
536 input_report_key(input, BTN_LEFT, ibt);
544 /* Wellspring initialization constants */
545 #define BCM5974_WELLSPRING_MODE_READ_REQUEST_ID 1
546 #define BCM5974_WELLSPRING_MODE_WRITE_REQUEST_ID 9
547 #define BCM5974_WELLSPRING_MODE_REQUEST_VALUE 0x300
548 #define BCM5974_WELLSPRING_MODE_REQUEST_INDEX 0
549 #define BCM5974_WELLSPRING_MODE_VENDOR_VALUE 0x01
550 #define BCM5974_WELLSPRING_MODE_NORMAL_VALUE 0x08
552 static int bcm5974_wellspring_mode(struct bcm5974 *dev, bool on)
554 char *data = kmalloc(8, GFP_KERNEL);
555 int retval = 0, size;
558 dev_err(&dev->intf->dev, "out of memory\n");
563 /* read configuration */
564 size = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
565 BCM5974_WELLSPRING_MODE_READ_REQUEST_ID,
566 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
567 BCM5974_WELLSPRING_MODE_REQUEST_VALUE,
568 BCM5974_WELLSPRING_MODE_REQUEST_INDEX, data, 8, 5000);
571 dev_err(&dev->intf->dev, "could not read from device\n");
576 /* apply the mode switch */
578 BCM5974_WELLSPRING_MODE_VENDOR_VALUE :
579 BCM5974_WELLSPRING_MODE_NORMAL_VALUE;
581 /* write configuration */
582 size = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
583 BCM5974_WELLSPRING_MODE_WRITE_REQUEST_ID,
584 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
585 BCM5974_WELLSPRING_MODE_REQUEST_VALUE,
586 BCM5974_WELLSPRING_MODE_REQUEST_INDEX, data, 8, 5000);
589 dev_err(&dev->intf->dev, "could not write to device\n");
594 dprintk(2, "bcm5974: switched to %s mode.\n",
595 on ? "wellspring" : "normal");
602 static void bcm5974_irq_button(struct urb *urb)
604 struct bcm5974 *dev = urb->context;
605 struct usb_interface *intf = dev->intf;
608 switch (urb->status) {
615 dev_dbg(&intf->dev, "button urb shutting down: %d\n",
619 dev_dbg(&intf->dev, "button urb status: %d\n", urb->status);
623 if (report_bt_state(dev, dev->bt_urb->actual_length))
624 dprintk(1, "bcm5974: bad button package, length: %d\n",
625 dev->bt_urb->actual_length);
628 error = usb_submit_urb(dev->bt_urb, GFP_ATOMIC);
630 dev_err(&intf->dev, "button urb failed: %d\n", error);
633 static void bcm5974_irq_trackpad(struct urb *urb)
635 struct bcm5974 *dev = urb->context;
636 struct usb_interface *intf = dev->intf;
639 switch (urb->status) {
646 dev_dbg(&intf->dev, "trackpad urb shutting down: %d\n",
650 dev_dbg(&intf->dev, "trackpad urb status: %d\n", urb->status);
654 /* control response ignored */
655 if (dev->tp_urb->actual_length == 2)
658 if (report_tp_state(dev, dev->tp_urb->actual_length))
659 dprintk(1, "bcm5974: bad trackpad package, length: %d\n",
660 dev->tp_urb->actual_length);
663 error = usb_submit_urb(dev->tp_urb, GFP_ATOMIC);
665 dev_err(&intf->dev, "trackpad urb failed: %d\n", error);
669 * The Wellspring trackpad, like many recent Apple trackpads, share
670 * the usb device with the keyboard. Since keyboards are usually
671 * handled by the HID system, the device ends up being handled by two
672 * modules. Setting up the device therefore becomes slightly
673 * complicated. To enable multitouch features, a mode switch is
674 * required, which is usually applied via the control interface of the
675 * device. It can be argued where this switch should take place. In
676 * some drivers, like appletouch, the switch is made during
677 * probe. However, the hid module may also alter the state of the
678 * device, resulting in trackpad malfunction under certain
679 * circumstances. To get around this problem, there is at least one
680 * example that utilizes the USB_QUIRK_RESET_RESUME quirk in order to
681 * receive a reset_resume request rather than the normal resume.
682 * Since the implementation of reset_resume is equal to mode switch
683 * plus start_traffic, it seems easier to always do the switch when
684 * starting traffic on the device.
686 static int bcm5974_start_traffic(struct bcm5974 *dev)
690 error = bcm5974_wellspring_mode(dev, true);
692 dprintk(1, "bcm5974: mode switch failed\n");
697 error = usb_submit_urb(dev->bt_urb, GFP_KERNEL);
702 error = usb_submit_urb(dev->tp_urb, GFP_KERNEL);
709 usb_kill_urb(dev->bt_urb);
711 bcm5974_wellspring_mode(dev, false);
716 static void bcm5974_pause_traffic(struct bcm5974 *dev)
718 usb_kill_urb(dev->tp_urb);
719 usb_kill_urb(dev->bt_urb);
720 bcm5974_wellspring_mode(dev, false);
724 * The code below implements open/close and manual suspend/resume.
725 * All functions may be called in random order.
727 * Opening a suspended device fails with EACCES - permission denied.
729 * Failing a resume leaves the device resumed but closed.
731 static int bcm5974_open(struct input_dev *input)
733 struct bcm5974 *dev = input_get_drvdata(input);
736 error = usb_autopm_get_interface(dev->intf);
740 mutex_lock(&dev->pm_mutex);
742 error = bcm5974_start_traffic(dev);
746 mutex_unlock(&dev->pm_mutex);
749 usb_autopm_put_interface(dev->intf);
754 static void bcm5974_close(struct input_dev *input)
756 struct bcm5974 *dev = input_get_drvdata(input);
758 mutex_lock(&dev->pm_mutex);
760 bcm5974_pause_traffic(dev);
763 mutex_unlock(&dev->pm_mutex);
765 usb_autopm_put_interface(dev->intf);
768 static int bcm5974_suspend(struct usb_interface *iface, pm_message_t message)
770 struct bcm5974 *dev = usb_get_intfdata(iface);
772 mutex_lock(&dev->pm_mutex);
775 bcm5974_pause_traffic(dev);
777 mutex_unlock(&dev->pm_mutex);
782 static int bcm5974_resume(struct usb_interface *iface)
784 struct bcm5974 *dev = usb_get_intfdata(iface);
787 mutex_lock(&dev->pm_mutex);
790 error = bcm5974_start_traffic(dev);
792 mutex_unlock(&dev->pm_mutex);
797 static int bcm5974_probe(struct usb_interface *iface,
798 const struct usb_device_id *id)
800 struct usb_device *udev = interface_to_usbdev(iface);
801 const struct bcm5974_config *cfg;
803 struct input_dev *input_dev;
806 /* find the product index */
807 cfg = bcm5974_get_config(udev);
809 /* allocate memory for our device state and initialize it */
810 dev = kzalloc(sizeof(struct bcm5974), GFP_KERNEL);
811 input_dev = input_allocate_device();
812 if (!dev || !input_dev) {
813 dev_err(&iface->dev, "out of memory\n");
819 dev->input = input_dev;
821 mutex_init(&dev->pm_mutex);
824 if (cfg->tp_type == TYPE1) {
825 dev->bt_urb = usb_alloc_urb(0, GFP_KERNEL);
830 dev->tp_urb = usb_alloc_urb(0, GFP_KERNEL);
832 goto err_free_bt_urb;
835 dev->bt_data = usb_alloc_coherent(dev->udev,
836 dev->cfg.bt_datalen, GFP_KERNEL,
837 &dev->bt_urb->transfer_dma);
842 dev->tp_data = usb_alloc_coherent(dev->udev,
843 dev->cfg.tp_datalen, GFP_KERNEL,
844 &dev->tp_urb->transfer_dma);
846 goto err_free_bt_buffer;
849 usb_fill_int_urb(dev->bt_urb, udev,
850 usb_rcvintpipe(udev, cfg->bt_ep),
851 dev->bt_data, dev->cfg.bt_datalen,
852 bcm5974_irq_button, dev, 1);
854 usb_fill_int_urb(dev->tp_urb, udev,
855 usb_rcvintpipe(udev, cfg->tp_ep),
856 dev->tp_data, dev->cfg.tp_datalen,
857 bcm5974_irq_trackpad, dev, 1);
859 /* create bcm5974 device */
860 usb_make_path(udev, dev->phys, sizeof(dev->phys));
861 strlcat(dev->phys, "/input0", sizeof(dev->phys));
863 input_dev->name = "bcm5974";
864 input_dev->phys = dev->phys;
865 usb_to_input_id(dev->udev, &input_dev->id);
866 /* report driver capabilities via the version field */
867 input_dev->id.version = cfg->caps;
868 input_dev->dev.parent = &iface->dev;
870 input_set_drvdata(input_dev, dev);
872 input_dev->open = bcm5974_open;
873 input_dev->close = bcm5974_close;
875 setup_events_to_report(input_dev, cfg);
877 error = input_register_device(dev->input);
879 goto err_free_buffer;
881 /* save our data pointer in this interface device */
882 usb_set_intfdata(iface, dev);
887 usb_free_coherent(dev->udev, dev->cfg.tp_datalen,
888 dev->tp_data, dev->tp_urb->transfer_dma);
891 usb_free_coherent(dev->udev, dev->cfg.bt_datalen,
892 dev->bt_data, dev->bt_urb->transfer_dma);
894 usb_free_urb(dev->tp_urb);
896 usb_free_urb(dev->bt_urb);
898 usb_set_intfdata(iface, NULL);
899 input_free_device(input_dev);
904 static void bcm5974_disconnect(struct usb_interface *iface)
906 struct bcm5974 *dev = usb_get_intfdata(iface);
908 usb_set_intfdata(iface, NULL);
910 input_unregister_device(dev->input);
911 usb_free_coherent(dev->udev, dev->cfg.tp_datalen,
912 dev->tp_data, dev->tp_urb->transfer_dma);
914 usb_free_coherent(dev->udev, dev->cfg.bt_datalen,
915 dev->bt_data, dev->bt_urb->transfer_dma);
916 usb_free_urb(dev->tp_urb);
917 usb_free_urb(dev->bt_urb);
921 static struct usb_driver bcm5974_driver = {
923 .probe = bcm5974_probe,
924 .disconnect = bcm5974_disconnect,
925 .suspend = bcm5974_suspend,
926 .resume = bcm5974_resume,
927 .id_table = bcm5974_table,
928 .supports_autosuspend = 1,
931 module_usb_driver(bcm5974_driver);