xref: /linux/drivers/input/mouse/bcm5974.c (revision f49f4ab95c301dbccad0efe85296d908b8ae7ad4)
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 #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 
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	\
96 }
97 
98 /* table of devices that work with this driver */
99 static const struct usb_device_id bcm5974_table[] = {
100 	/* MacbookAir1.1 */
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),
108 	/* Macbook5,1 */
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),
112 	/* MacbookAir3,2 */
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),
116 	/* MacbookAir3,1 */
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),
120 	/* MacbookPro8 */
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),
124 	/* MacbookAir4,1 */
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),
128 	/* MacbookAir4,2 */
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),
132 	/* MacbookPro8,2 */
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),
136 	/* MacbookPro10,1 */
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 */
141 	{}
142 };
143 MODULE_DEVICE_TABLE(usb, bcm5974_table);
144 
145 MODULE_AUTHOR("Henrik Rydberg");
146 MODULE_DESCRIPTION("Apple USB BCM5974 multitouch driver");
147 MODULE_LICENSE("GPL");
148 
149 #define dprintk(level, format, a...)\
150 	{ if (debug >= level) printk(KERN_DEBUG format, ##a); }
151 
152 static int debug = 1;
153 module_param(debug, int, 0644);
154 MODULE_PARM_DESC(debug, "Activate debugging output");
155 
156 /* button data structure */
157 struct bt_data {
158 	u8 unknown1;		/* constant */
159 	u8 button;		/* left button */
160 	u8 rel_x;		/* relative x coordinate */
161 	u8 rel_y;		/* relative y coordinate */
162 };
163 
164 /* trackpad header types */
165 enum tp_type {
166 	TYPE1,			/* plain trackpad */
167 	TYPE2			/* button integrated in trackpad */
168 };
169 
170 /* trackpad finger data offsets, le16-aligned */
171 #define FINGER_TYPE1		(13 * sizeof(__le16))
172 #define FINGER_TYPE2		(15 * sizeof(__le16))
173 
174 /* trackpad button data offsets */
175 #define BUTTON_TYPE2		15
176 
177 /* list of device capability bits */
178 #define HAS_INTEGRATED_BUTTON	1
179 
180 /* trackpad finger structure, le16-aligned */
181 struct tp_finger {
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)));
195 
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
201 
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 */
207 };
208 
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 */
224 };
225 
226 /* logical device structure */
227 struct bcm5974 {
228 	char phys[64];
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 */
242 };
243 
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 */
249 
250 /* device constants */
251 static const struct bcm5974_config bcm5974_config_table[] = {
252 	{
253 		USB_DEVICE_ID_APPLE_WELLSPRING_ANSI,
254 		USB_DEVICE_ID_APPLE_WELLSPRING_ISO,
255 		USB_DEVICE_ID_APPLE_WELLSPRING_JIS,
256 		0,
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 }
264 	},
265 	{
266 		USB_DEVICE_ID_APPLE_WELLSPRING2_ANSI,
267 		USB_DEVICE_ID_APPLE_WELLSPRING2_ISO,
268 		USB_DEVICE_ID_APPLE_WELLSPRING2_JIS,
269 		0,
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 }
277 	},
278 	{
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 }
290 	},
291 	{
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 }
303 	},
304 	{
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 }
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 		{ 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 }
329 	},
330 	{
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 }
342 	},
343 	{
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 }
355 	},
356 	{
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 }
368 	},
369 	{
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 }
381 	},
382 	{}
383 };
384 
385 /* return the device-specific configuration by device */
386 static const struct bcm5974_config *bcm5974_get_config(struct usb_device *udev)
387 {
388 	u16 id = le16_to_cpu(udev->descriptor.idProduct);
389 	const struct bcm5974_config *cfg;
390 
391 	for (cfg = bcm5974_config_table; cfg->ansi; ++cfg)
392 		if (cfg->ansi == id || cfg->iso == id || cfg->jis == id)
393 			return cfg;
394 
395 	return bcm5974_config_table;
396 }
397 
398 /* convert 16-bit little endian to signed integer */
399 static inline int raw2int(__le16 x)
400 {
401 	return (signed short)le16_to_cpu(x);
402 }
403 
404 static void set_abs(struct input_dev *input, unsigned int code,
405 		    const struct bcm5974_param *p)
406 {
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);
409 }
410 
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)
414 {
415 	__set_bit(EV_ABS, input_dev->evbit);
416 
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);
420 
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);
432 
433 	__set_bit(EV_KEY, input_dev->evbit);
434 	__set_bit(BTN_LEFT, input_dev->keybit);
435 
436 	if (cfg->caps & HAS_INTEGRATED_BUTTON)
437 		__set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
438 
439 	input_mt_init_slots(input_dev, MAX_FINGERS,
440 		INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED | INPUT_MT_TRACK);
441 }
442 
443 /* report button data as logical button state */
444 static int report_bt_state(struct bcm5974 *dev, int size)
445 {
446 	if (size != sizeof(struct bt_data))
447 		return -EIO;
448 
449 	dprintk(7,
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);
453 
454 	input_report_key(dev->input, BTN_LEFT, dev->bt_data->button);
455 	input_sync(dev->input);
456 
457 	return 0;
458 }
459 
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)
463 {
464 	input_mt_slot(input, slot);
465 	input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
466 
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);
479 }
480 
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)
484 {
485 	int abs_p = 0, abs_w = 0;
486 
487 	if (raw_n) {
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);
493 		}
494 	}
495 
496 	input_report_abs(input, ABS_PRESSURE, abs_p);
497 	input_report_abs(input, ABS_TOOL_WIDTH, abs_w);
498 }
499 
500 /* report trackpad data as logical trackpad state */
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_n, i, n = 0;
507 
508 	if (size < c->tp_offset || (size - c->tp_offset) % SIZEOF_FINGER != 0)
509 		return -EIO;
510 
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;
514 
515 	for (i = 0; i < raw_n; i++) {
516 		if (raw2int(f[i].touch_major) == 0)
517 			continue;
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];
521 	}
522 
523 	input_mt_assign_slots(input, dev->slots, dev->pos, n);
524 
525 	for (i = 0; i < n; i++)
526 		report_finger_data(input, dev->slots[i],
527 				   &dev->pos[i], dev->index[i]);
528 
529 	input_mt_sync_frame(input);
530 
531 	report_synaptics_data(input, c, f, raw_n);
532 
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);
537 	}
538 
539 	input_sync(input);
540 
541 	return 0;
542 }
543 
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
551 
552 static int bcm5974_wellspring_mode(struct bcm5974 *dev, bool on)
553 {
554 	char *data = kmalloc(8, GFP_KERNEL);
555 	int retval = 0, size;
556 
557 	if (!data) {
558 		dev_err(&dev->intf->dev, "out of memory\n");
559 		retval = -ENOMEM;
560 		goto out;
561 	}
562 
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);
569 
570 	if (size != 8) {
571 		dev_err(&dev->intf->dev, "could not read from device\n");
572 		retval = -EIO;
573 		goto out;
574 	}
575 
576 	/* apply the mode switch */
577 	data[0] = on ?
578 		BCM5974_WELLSPRING_MODE_VENDOR_VALUE :
579 		BCM5974_WELLSPRING_MODE_NORMAL_VALUE;
580 
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);
587 
588 	if (size != 8) {
589 		dev_err(&dev->intf->dev, "could not write to device\n");
590 		retval = -EIO;
591 		goto out;
592 	}
593 
594 	dprintk(2, "bcm5974: switched to %s mode.\n",
595 		on ? "wellspring" : "normal");
596 
597  out:
598 	kfree(data);
599 	return retval;
600 }
601 
602 static void bcm5974_irq_button(struct urb *urb)
603 {
604 	struct bcm5974 *dev = urb->context;
605 	struct usb_interface *intf = dev->intf;
606 	int error;
607 
608 	switch (urb->status) {
609 	case 0:
610 		break;
611 	case -EOVERFLOW:
612 	case -ECONNRESET:
613 	case -ENOENT:
614 	case -ESHUTDOWN:
615 		dev_dbg(&intf->dev, "button urb shutting down: %d\n",
616 			urb->status);
617 		return;
618 	default:
619 		dev_dbg(&intf->dev, "button urb status: %d\n", urb->status);
620 		goto exit;
621 	}
622 
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);
626 
627 exit:
628 	error = usb_submit_urb(dev->bt_urb, GFP_ATOMIC);
629 	if (error)
630 		dev_err(&intf->dev, "button urb failed: %d\n", error);
631 }
632 
633 static void bcm5974_irq_trackpad(struct urb *urb)
634 {
635 	struct bcm5974 *dev = urb->context;
636 	struct usb_interface *intf = dev->intf;
637 	int error;
638 
639 	switch (urb->status) {
640 	case 0:
641 		break;
642 	case -EOVERFLOW:
643 	case -ECONNRESET:
644 	case -ENOENT:
645 	case -ESHUTDOWN:
646 		dev_dbg(&intf->dev, "trackpad urb shutting down: %d\n",
647 			urb->status);
648 		return;
649 	default:
650 		dev_dbg(&intf->dev, "trackpad urb status: %d\n", urb->status);
651 		goto exit;
652 	}
653 
654 	/* control response ignored */
655 	if (dev->tp_urb->actual_length == 2)
656 		goto exit;
657 
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);
661 
662 exit:
663 	error = usb_submit_urb(dev->tp_urb, GFP_ATOMIC);
664 	if (error)
665 		dev_err(&intf->dev, "trackpad urb failed: %d\n", error);
666 }
667 
668 /*
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.
685  */
686 static int bcm5974_start_traffic(struct bcm5974 *dev)
687 {
688 	int error;
689 
690 	error = bcm5974_wellspring_mode(dev, true);
691 	if (error) {
692 		dprintk(1, "bcm5974: mode switch failed\n");
693 		goto err_out;
694 	}
695 
696 	if (dev->bt_urb) {
697 		error = usb_submit_urb(dev->bt_urb, GFP_KERNEL);
698 		if (error)
699 			goto err_reset_mode;
700 	}
701 
702 	error = usb_submit_urb(dev->tp_urb, GFP_KERNEL);
703 	if (error)
704 		goto err_kill_bt;
705 
706 	return 0;
707 
708 err_kill_bt:
709 	usb_kill_urb(dev->bt_urb);
710 err_reset_mode:
711 	bcm5974_wellspring_mode(dev, false);
712 err_out:
713 	return error;
714 }
715 
716 static void bcm5974_pause_traffic(struct bcm5974 *dev)
717 {
718 	usb_kill_urb(dev->tp_urb);
719 	usb_kill_urb(dev->bt_urb);
720 	bcm5974_wellspring_mode(dev, false);
721 }
722 
723 /*
724  * The code below implements open/close and manual suspend/resume.
725  * All functions may be called in random order.
726  *
727  * Opening a suspended device fails with EACCES - permission denied.
728  *
729  * Failing a resume leaves the device resumed but closed.
730  */
731 static int bcm5974_open(struct input_dev *input)
732 {
733 	struct bcm5974 *dev = input_get_drvdata(input);
734 	int error;
735 
736 	error = usb_autopm_get_interface(dev->intf);
737 	if (error)
738 		return error;
739 
740 	mutex_lock(&dev->pm_mutex);
741 
742 	error = bcm5974_start_traffic(dev);
743 	if (!error)
744 		dev->opened = 1;
745 
746 	mutex_unlock(&dev->pm_mutex);
747 
748 	if (error)
749 		usb_autopm_put_interface(dev->intf);
750 
751 	return error;
752 }
753 
754 static void bcm5974_close(struct input_dev *input)
755 {
756 	struct bcm5974 *dev = input_get_drvdata(input);
757 
758 	mutex_lock(&dev->pm_mutex);
759 
760 	bcm5974_pause_traffic(dev);
761 	dev->opened = 0;
762 
763 	mutex_unlock(&dev->pm_mutex);
764 
765 	usb_autopm_put_interface(dev->intf);
766 }
767 
768 static int bcm5974_suspend(struct usb_interface *iface, pm_message_t message)
769 {
770 	struct bcm5974 *dev = usb_get_intfdata(iface);
771 
772 	mutex_lock(&dev->pm_mutex);
773 
774 	if (dev->opened)
775 		bcm5974_pause_traffic(dev);
776 
777 	mutex_unlock(&dev->pm_mutex);
778 
779 	return 0;
780 }
781 
782 static int bcm5974_resume(struct usb_interface *iface)
783 {
784 	struct bcm5974 *dev = usb_get_intfdata(iface);
785 	int error = 0;
786 
787 	mutex_lock(&dev->pm_mutex);
788 
789 	if (dev->opened)
790 		error = bcm5974_start_traffic(dev);
791 
792 	mutex_unlock(&dev->pm_mutex);
793 
794 	return error;
795 }
796 
797 static int bcm5974_probe(struct usb_interface *iface,
798 			 const struct usb_device_id *id)
799 {
800 	struct usb_device *udev = interface_to_usbdev(iface);
801 	const struct bcm5974_config *cfg;
802 	struct bcm5974 *dev;
803 	struct input_dev *input_dev;
804 	int error = -ENOMEM;
805 
806 	/* find the product index */
807 	cfg = bcm5974_get_config(udev);
808 
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");
814 		goto err_free_devs;
815 	}
816 
817 	dev->udev = udev;
818 	dev->intf = iface;
819 	dev->input = input_dev;
820 	dev->cfg = *cfg;
821 	mutex_init(&dev->pm_mutex);
822 
823 	/* setup urbs */
824 	if (cfg->tp_type == TYPE1) {
825 		dev->bt_urb = usb_alloc_urb(0, GFP_KERNEL);
826 		if (!dev->bt_urb)
827 			goto err_free_devs;
828 	}
829 
830 	dev->tp_urb = usb_alloc_urb(0, GFP_KERNEL);
831 	if (!dev->tp_urb)
832 		goto err_free_bt_urb;
833 
834 	if (dev->bt_urb) {
835 		dev->bt_data = usb_alloc_coherent(dev->udev,
836 					  dev->cfg.bt_datalen, GFP_KERNEL,
837 					  &dev->bt_urb->transfer_dma);
838 		if (!dev->bt_data)
839 			goto err_free_urb;
840 	}
841 
842 	dev->tp_data = usb_alloc_coherent(dev->udev,
843 					  dev->cfg.tp_datalen, GFP_KERNEL,
844 					  &dev->tp_urb->transfer_dma);
845 	if (!dev->tp_data)
846 		goto err_free_bt_buffer;
847 
848 	if (dev->bt_urb)
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);
853 
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);
858 
859 	/* create bcm5974 device */
860 	usb_make_path(udev, dev->phys, sizeof(dev->phys));
861 	strlcat(dev->phys, "/input0", sizeof(dev->phys));
862 
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;
869 
870 	input_set_drvdata(input_dev, dev);
871 
872 	input_dev->open = bcm5974_open;
873 	input_dev->close = bcm5974_close;
874 
875 	setup_events_to_report(input_dev, cfg);
876 
877 	error = input_register_device(dev->input);
878 	if (error)
879 		goto err_free_buffer;
880 
881 	/* save our data pointer in this interface device */
882 	usb_set_intfdata(iface, dev);
883 
884 	return 0;
885 
886 err_free_buffer:
887 	usb_free_coherent(dev->udev, dev->cfg.tp_datalen,
888 		dev->tp_data, dev->tp_urb->transfer_dma);
889 err_free_bt_buffer:
890 	if (dev->bt_urb)
891 		usb_free_coherent(dev->udev, dev->cfg.bt_datalen,
892 				  dev->bt_data, dev->bt_urb->transfer_dma);
893 err_free_urb:
894 	usb_free_urb(dev->tp_urb);
895 err_free_bt_urb:
896 	usb_free_urb(dev->bt_urb);
897 err_free_devs:
898 	usb_set_intfdata(iface, NULL);
899 	input_free_device(input_dev);
900 	kfree(dev);
901 	return error;
902 }
903 
904 static void bcm5974_disconnect(struct usb_interface *iface)
905 {
906 	struct bcm5974 *dev = usb_get_intfdata(iface);
907 
908 	usb_set_intfdata(iface, NULL);
909 
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);
913 	if (dev->bt_urb)
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);
918 	kfree(dev);
919 }
920 
921 static struct usb_driver bcm5974_driver = {
922 	.name			= "bcm5974",
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,
929 };
930 
931 module_usb_driver(bcm5974_driver);
932