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