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