sched: Remove double_rq_lock() from __migrate_task()
[linux/fpc-iii.git] / drivers / input / mouse / bcm5974.c
blobc329cdb0b91aa819f4b4f36dda820ed7a55d97e2
1 /*
2 * Apple USB BCM5974 (Macbook Air and Penryn Macbook Pro) multitouch driver
4 * Copyright (C) 2008 Henrik Rydberg (rydberg@euromail.se)
6 * The USB initialization and package decoding was made by
7 * Scott Shawcroft as part of the touchd user-space driver project:
8 * Copyright (C) 2008 Scott Shawcroft (scott.shawcroft@gmail.com)
10 * The BCM5974 driver is based on the appletouch driver:
11 * Copyright (C) 2001-2004 Greg Kroah-Hartman (greg@kroah.com)
12 * Copyright (C) 2005 Johannes Berg (johannes@sipsolutions.net)
13 * Copyright (C) 2005 Stelian Pop (stelian@popies.net)
14 * Copyright (C) 2005 Frank Arnold (frank@scirocco-5v-turbo.de)
15 * Copyright (C) 2005 Peter Osterlund (petero2@telia.com)
16 * Copyright (C) 2005 Michael Hanselmann (linux-kernel@hansmi.ch)
17 * Copyright (C) 2006 Nicolas Boichat (nicolas@boichat.ch)
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 #include <linux/kernel.h>
36 #include <linux/errno.h>
37 #include <linux/slab.h>
38 #include <linux/module.h>
39 #include <linux/usb/input.h>
40 #include <linux/hid.h>
41 #include <linux/mutex.h>
42 #include <linux/input/mt.h>
44 #define USB_VENDOR_ID_APPLE 0x05ac
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 /* MacbookPro10,2 (unibody, October 2012) */
87 #define USB_DEVICE_ID_APPLE_WELLSPRING7A_ANSI 0x0259
88 #define USB_DEVICE_ID_APPLE_WELLSPRING7A_ISO 0x025a
89 #define USB_DEVICE_ID_APPLE_WELLSPRING7A_JIS 0x025b
90 /* MacbookAir6,2 (unibody, June 2013) */
91 #define USB_DEVICE_ID_APPLE_WELLSPRING8_ANSI 0x0290
92 #define USB_DEVICE_ID_APPLE_WELLSPRING8_ISO 0x0291
93 #define USB_DEVICE_ID_APPLE_WELLSPRING8_JIS 0x0292
95 #define BCM5974_DEVICE(prod) { \
96 .match_flags = (USB_DEVICE_ID_MATCH_DEVICE | \
97 USB_DEVICE_ID_MATCH_INT_CLASS | \
98 USB_DEVICE_ID_MATCH_INT_PROTOCOL), \
99 .idVendor = USB_VENDOR_ID_APPLE, \
100 .idProduct = (prod), \
101 .bInterfaceClass = USB_INTERFACE_CLASS_HID, \
102 .bInterfaceProtocol = USB_INTERFACE_PROTOCOL_MOUSE \
105 /* table of devices that work with this driver */
106 static const struct usb_device_id bcm5974_table[] = {
107 /* MacbookAir1.1 */
108 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING_ANSI),
109 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING_ISO),
110 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING_JIS),
111 /* MacbookProPenryn */
112 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING2_ANSI),
113 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING2_ISO),
114 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING2_JIS),
115 /* Macbook5,1 */
116 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING3_ANSI),
117 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING3_ISO),
118 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING3_JIS),
119 /* MacbookAir3,2 */
120 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING4_ANSI),
121 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING4_ISO),
122 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING4_JIS),
123 /* MacbookAir3,1 */
124 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING4A_ANSI),
125 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING4A_ISO),
126 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING4A_JIS),
127 /* MacbookPro8 */
128 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING5_ANSI),
129 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING5_ISO),
130 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING5_JIS),
131 /* MacbookAir4,1 */
132 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING6A_ANSI),
133 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING6A_ISO),
134 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING6A_JIS),
135 /* MacbookAir4,2 */
136 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING6_ANSI),
137 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING6_ISO),
138 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING6_JIS),
139 /* MacbookPro8,2 */
140 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING5A_ANSI),
141 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING5A_ISO),
142 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING5A_JIS),
143 /* MacbookPro10,1 */
144 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING7_ANSI),
145 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING7_ISO),
146 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING7_JIS),
147 /* MacbookPro10,2 */
148 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING7A_ANSI),
149 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING7A_ISO),
150 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING7A_JIS),
151 /* MacbookAir6,2 */
152 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING8_ANSI),
153 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING8_ISO),
154 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING8_JIS),
155 /* Terminating entry */
158 MODULE_DEVICE_TABLE(usb, bcm5974_table);
160 MODULE_AUTHOR("Henrik Rydberg");
161 MODULE_DESCRIPTION("Apple USB BCM5974 multitouch driver");
162 MODULE_LICENSE("GPL");
164 #define dprintk(level, format, a...)\
165 { if (debug >= level) printk(KERN_DEBUG format, ##a); }
167 static int debug = 1;
168 module_param(debug, int, 0644);
169 MODULE_PARM_DESC(debug, "Activate debugging output");
171 /* button data structure */
172 struct bt_data {
173 u8 unknown1; /* constant */
174 u8 button; /* left button */
175 u8 rel_x; /* relative x coordinate */
176 u8 rel_y; /* relative y coordinate */
179 /* trackpad header types */
180 enum tp_type {
181 TYPE1, /* plain trackpad */
182 TYPE2, /* button integrated in trackpad */
183 TYPE3 /* additional header fields since June 2013 */
186 /* trackpad finger data offsets, le16-aligned */
187 #define FINGER_TYPE1 (13 * sizeof(__le16))
188 #define FINGER_TYPE2 (15 * sizeof(__le16))
189 #define FINGER_TYPE3 (19 * sizeof(__le16))
191 /* trackpad button data offsets */
192 #define BUTTON_TYPE2 15
193 #define BUTTON_TYPE3 23
195 /* list of device capability bits */
196 #define HAS_INTEGRATED_BUTTON 1
198 /* trackpad finger structure, le16-aligned */
199 struct tp_finger {
200 __le16 origin; /* zero when switching track finger */
201 __le16 abs_x; /* absolute x coodinate */
202 __le16 abs_y; /* absolute y coodinate */
203 __le16 rel_x; /* relative x coodinate */
204 __le16 rel_y; /* relative y coodinate */
205 __le16 tool_major; /* tool area, major axis */
206 __le16 tool_minor; /* tool area, minor axis */
207 __le16 orientation; /* 16384 when point, else 15 bit angle */
208 __le16 touch_major; /* touch area, major axis */
209 __le16 touch_minor; /* touch area, minor axis */
210 __le16 unused[3]; /* zeros */
211 __le16 multi; /* one finger: varies, more fingers: constant */
212 } __attribute__((packed,aligned(2)));
214 /* trackpad finger data size, empirically at least ten fingers */
215 #define MAX_FINGERS 16
216 #define SIZEOF_FINGER sizeof(struct tp_finger)
217 #define SIZEOF_ALL_FINGERS (MAX_FINGERS * SIZEOF_FINGER)
218 #define MAX_FINGER_ORIENTATION 16384
220 /* device-specific parameters */
221 struct bcm5974_param {
222 int snratio; /* signal-to-noise ratio */
223 int min; /* device minimum reading */
224 int max; /* device maximum reading */
227 /* device-specific configuration */
228 struct bcm5974_config {
229 int ansi, iso, jis; /* the product id of this device */
230 int caps; /* device capability bitmask */
231 int bt_ep; /* the endpoint of the button interface */
232 int bt_datalen; /* data length of the button interface */
233 int tp_ep; /* the endpoint of the trackpad interface */
234 enum tp_type tp_type; /* type of trackpad interface */
235 int tp_offset; /* offset to trackpad finger data */
236 int tp_datalen; /* data length of the trackpad interface */
237 struct bcm5974_param p; /* finger pressure limits */
238 struct bcm5974_param w; /* finger width limits */
239 struct bcm5974_param x; /* horizontal limits */
240 struct bcm5974_param y; /* vertical limits */
241 struct bcm5974_param o; /* orientation limits */
244 /* logical device structure */
245 struct bcm5974 {
246 char phys[64];
247 struct usb_device *udev; /* usb device */
248 struct usb_interface *intf; /* our interface */
249 struct input_dev *input; /* input dev */
250 struct bcm5974_config cfg; /* device configuration */
251 struct mutex pm_mutex; /* serialize access to open/suspend */
252 int opened; /* 1: opened, 0: closed */
253 struct urb *bt_urb; /* button usb request block */
254 struct bt_data *bt_data; /* button transferred data */
255 struct urb *tp_urb; /* trackpad usb request block */
256 u8 *tp_data; /* trackpad transferred data */
257 const struct tp_finger *index[MAX_FINGERS]; /* finger index data */
258 struct input_mt_pos pos[MAX_FINGERS]; /* position array */
259 int slots[MAX_FINGERS]; /* slot assignments */
262 /* logical signal quality */
263 #define SN_PRESSURE 45 /* pressure signal-to-noise ratio */
264 #define SN_WIDTH 25 /* width signal-to-noise ratio */
265 #define SN_COORD 250 /* coordinate signal-to-noise ratio */
266 #define SN_ORIENT 10 /* orientation signal-to-noise ratio */
268 /* device constants */
269 static const struct bcm5974_config bcm5974_config_table[] = {
271 USB_DEVICE_ID_APPLE_WELLSPRING_ANSI,
272 USB_DEVICE_ID_APPLE_WELLSPRING_ISO,
273 USB_DEVICE_ID_APPLE_WELLSPRING_JIS,
275 0x84, sizeof(struct bt_data),
276 0x81, TYPE1, FINGER_TYPE1, FINGER_TYPE1 + SIZEOF_ALL_FINGERS,
277 { SN_PRESSURE, 0, 256 },
278 { SN_WIDTH, 0, 2048 },
279 { SN_COORD, -4824, 5342 },
280 { SN_COORD, -172, 5820 },
281 { SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION }
284 USB_DEVICE_ID_APPLE_WELLSPRING2_ANSI,
285 USB_DEVICE_ID_APPLE_WELLSPRING2_ISO,
286 USB_DEVICE_ID_APPLE_WELLSPRING2_JIS,
288 0x84, sizeof(struct bt_data),
289 0x81, TYPE1, FINGER_TYPE1, FINGER_TYPE1 + SIZEOF_ALL_FINGERS,
290 { SN_PRESSURE, 0, 256 },
291 { SN_WIDTH, 0, 2048 },
292 { SN_COORD, -4824, 4824 },
293 { SN_COORD, -172, 4290 },
294 { SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION }
297 USB_DEVICE_ID_APPLE_WELLSPRING3_ANSI,
298 USB_DEVICE_ID_APPLE_WELLSPRING3_ISO,
299 USB_DEVICE_ID_APPLE_WELLSPRING3_JIS,
300 HAS_INTEGRATED_BUTTON,
301 0x84, sizeof(struct bt_data),
302 0x81, TYPE2, FINGER_TYPE2, FINGER_TYPE2 + SIZEOF_ALL_FINGERS,
303 { SN_PRESSURE, 0, 300 },
304 { SN_WIDTH, 0, 2048 },
305 { SN_COORD, -4460, 5166 },
306 { SN_COORD, -75, 6700 },
307 { SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION }
310 USB_DEVICE_ID_APPLE_WELLSPRING4_ANSI,
311 USB_DEVICE_ID_APPLE_WELLSPRING4_ISO,
312 USB_DEVICE_ID_APPLE_WELLSPRING4_JIS,
313 HAS_INTEGRATED_BUTTON,
314 0x84, sizeof(struct bt_data),
315 0x81, TYPE2, FINGER_TYPE2, FINGER_TYPE2 + SIZEOF_ALL_FINGERS,
316 { SN_PRESSURE, 0, 300 },
317 { SN_WIDTH, 0, 2048 },
318 { SN_COORD, -4620, 5140 },
319 { SN_COORD, -150, 6600 },
320 { SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION }
323 USB_DEVICE_ID_APPLE_WELLSPRING4A_ANSI,
324 USB_DEVICE_ID_APPLE_WELLSPRING4A_ISO,
325 USB_DEVICE_ID_APPLE_WELLSPRING4A_JIS,
326 HAS_INTEGRATED_BUTTON,
327 0x84, sizeof(struct bt_data),
328 0x81, TYPE2, FINGER_TYPE2, FINGER_TYPE2 + SIZEOF_ALL_FINGERS,
329 { SN_PRESSURE, 0, 300 },
330 { SN_WIDTH, 0, 2048 },
331 { SN_COORD, -4616, 5112 },
332 { SN_COORD, -142, 5234 },
333 { SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION }
336 USB_DEVICE_ID_APPLE_WELLSPRING5_ANSI,
337 USB_DEVICE_ID_APPLE_WELLSPRING5_ISO,
338 USB_DEVICE_ID_APPLE_WELLSPRING5_JIS,
339 HAS_INTEGRATED_BUTTON,
340 0x84, sizeof(struct bt_data),
341 0x81, TYPE2, FINGER_TYPE2, FINGER_TYPE2 + SIZEOF_ALL_FINGERS,
342 { SN_PRESSURE, 0, 300 },
343 { SN_WIDTH, 0, 2048 },
344 { SN_COORD, -4415, 5050 },
345 { SN_COORD, -55, 6680 },
346 { SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION }
349 USB_DEVICE_ID_APPLE_WELLSPRING6_ANSI,
350 USB_DEVICE_ID_APPLE_WELLSPRING6_ISO,
351 USB_DEVICE_ID_APPLE_WELLSPRING6_JIS,
352 HAS_INTEGRATED_BUTTON,
353 0x84, sizeof(struct bt_data),
354 0x81, TYPE2, FINGER_TYPE2, FINGER_TYPE2 + SIZEOF_ALL_FINGERS,
355 { SN_PRESSURE, 0, 300 },
356 { SN_WIDTH, 0, 2048 },
357 { SN_COORD, -4620, 5140 },
358 { SN_COORD, -150, 6600 },
359 { SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION }
362 USB_DEVICE_ID_APPLE_WELLSPRING5A_ANSI,
363 USB_DEVICE_ID_APPLE_WELLSPRING5A_ISO,
364 USB_DEVICE_ID_APPLE_WELLSPRING5A_JIS,
365 HAS_INTEGRATED_BUTTON,
366 0x84, sizeof(struct bt_data),
367 0x81, TYPE2, FINGER_TYPE2, FINGER_TYPE2 + SIZEOF_ALL_FINGERS,
368 { SN_PRESSURE, 0, 300 },
369 { SN_WIDTH, 0, 2048 },
370 { SN_COORD, -4750, 5280 },
371 { SN_COORD, -150, 6730 },
372 { SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION }
375 USB_DEVICE_ID_APPLE_WELLSPRING6A_ANSI,
376 USB_DEVICE_ID_APPLE_WELLSPRING6A_ISO,
377 USB_DEVICE_ID_APPLE_WELLSPRING6A_JIS,
378 HAS_INTEGRATED_BUTTON,
379 0x84, sizeof(struct bt_data),
380 0x81, TYPE2, FINGER_TYPE2, FINGER_TYPE2 + SIZEOF_ALL_FINGERS,
381 { SN_PRESSURE, 0, 300 },
382 { SN_WIDTH, 0, 2048 },
383 { SN_COORD, -4620, 5140 },
384 { SN_COORD, -150, 6600 },
385 { SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION }
388 USB_DEVICE_ID_APPLE_WELLSPRING7_ANSI,
389 USB_DEVICE_ID_APPLE_WELLSPRING7_ISO,
390 USB_DEVICE_ID_APPLE_WELLSPRING7_JIS,
391 HAS_INTEGRATED_BUTTON,
392 0x84, sizeof(struct bt_data),
393 0x81, TYPE2, FINGER_TYPE2, FINGER_TYPE2 + SIZEOF_ALL_FINGERS,
394 { SN_PRESSURE, 0, 300 },
395 { SN_WIDTH, 0, 2048 },
396 { SN_COORD, -4750, 5280 },
397 { SN_COORD, -150, 6730 },
398 { SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION }
401 USB_DEVICE_ID_APPLE_WELLSPRING7A_ANSI,
402 USB_DEVICE_ID_APPLE_WELLSPRING7A_ISO,
403 USB_DEVICE_ID_APPLE_WELLSPRING7A_JIS,
404 HAS_INTEGRATED_BUTTON,
405 0x84, sizeof(struct bt_data),
406 0x81, TYPE2, FINGER_TYPE2, FINGER_TYPE2 + SIZEOF_ALL_FINGERS,
407 { SN_PRESSURE, 0, 300 },
408 { SN_WIDTH, 0, 2048 },
409 { SN_COORD, -4750, 5280 },
410 { SN_COORD, -150, 6730 },
411 { SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION }
414 USB_DEVICE_ID_APPLE_WELLSPRING8_ANSI,
415 USB_DEVICE_ID_APPLE_WELLSPRING8_ISO,
416 USB_DEVICE_ID_APPLE_WELLSPRING8_JIS,
417 HAS_INTEGRATED_BUTTON,
418 0, sizeof(struct bt_data),
419 0x83, TYPE3, FINGER_TYPE3, FINGER_TYPE3 + SIZEOF_ALL_FINGERS,
420 { SN_PRESSURE, 0, 300 },
421 { SN_WIDTH, 0, 2048 },
422 { SN_COORD, -4620, 5140 },
423 { SN_COORD, -150, 6600 },
424 { SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION }
429 /* return the device-specific configuration by device */
430 static const struct bcm5974_config *bcm5974_get_config(struct usb_device *udev)
432 u16 id = le16_to_cpu(udev->descriptor.idProduct);
433 const struct bcm5974_config *cfg;
435 for (cfg = bcm5974_config_table; cfg->ansi; ++cfg)
436 if (cfg->ansi == id || cfg->iso == id || cfg->jis == id)
437 return cfg;
439 return bcm5974_config_table;
442 /* convert 16-bit little endian to signed integer */
443 static inline int raw2int(__le16 x)
445 return (signed short)le16_to_cpu(x);
448 static void set_abs(struct input_dev *input, unsigned int code,
449 const struct bcm5974_param *p)
451 int fuzz = p->snratio ? (p->max - p->min) / p->snratio : 0;
452 input_set_abs_params(input, code, p->min, p->max, fuzz, 0);
455 /* setup which logical events to report */
456 static void setup_events_to_report(struct input_dev *input_dev,
457 const struct bcm5974_config *cfg)
459 __set_bit(EV_ABS, input_dev->evbit);
461 /* for synaptics only */
462 input_set_abs_params(input_dev, ABS_PRESSURE, 0, 256, 5, 0);
463 input_set_abs_params(input_dev, ABS_TOOL_WIDTH, 0, 16, 0, 0);
465 /* finger touch area */
466 set_abs(input_dev, ABS_MT_TOUCH_MAJOR, &cfg->w);
467 set_abs(input_dev, ABS_MT_TOUCH_MINOR, &cfg->w);
468 /* finger approach area */
469 set_abs(input_dev, ABS_MT_WIDTH_MAJOR, &cfg->w);
470 set_abs(input_dev, ABS_MT_WIDTH_MINOR, &cfg->w);
471 /* finger orientation */
472 set_abs(input_dev, ABS_MT_ORIENTATION, &cfg->o);
473 /* finger position */
474 set_abs(input_dev, ABS_MT_POSITION_X, &cfg->x);
475 set_abs(input_dev, ABS_MT_POSITION_Y, &cfg->y);
477 __set_bit(EV_KEY, input_dev->evbit);
478 __set_bit(BTN_LEFT, input_dev->keybit);
480 if (cfg->caps & HAS_INTEGRATED_BUTTON)
481 __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
483 input_mt_init_slots(input_dev, MAX_FINGERS,
484 INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED | INPUT_MT_TRACK);
487 /* report button data as logical button state */
488 static int report_bt_state(struct bcm5974 *dev, int size)
490 if (size != sizeof(struct bt_data))
491 return -EIO;
493 dprintk(7,
494 "bcm5974: button data: %x %x %x %x\n",
495 dev->bt_data->unknown1, dev->bt_data->button,
496 dev->bt_data->rel_x, dev->bt_data->rel_y);
498 input_report_key(dev->input, BTN_LEFT, dev->bt_data->button);
499 input_sync(dev->input);
501 return 0;
504 static void report_finger_data(struct input_dev *input, int slot,
505 const struct input_mt_pos *pos,
506 const struct tp_finger *f)
508 input_mt_slot(input, slot);
509 input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
511 input_report_abs(input, ABS_MT_TOUCH_MAJOR,
512 raw2int(f->touch_major) << 1);
513 input_report_abs(input, ABS_MT_TOUCH_MINOR,
514 raw2int(f->touch_minor) << 1);
515 input_report_abs(input, ABS_MT_WIDTH_MAJOR,
516 raw2int(f->tool_major) << 1);
517 input_report_abs(input, ABS_MT_WIDTH_MINOR,
518 raw2int(f->tool_minor) << 1);
519 input_report_abs(input, ABS_MT_ORIENTATION,
520 MAX_FINGER_ORIENTATION - raw2int(f->orientation));
521 input_report_abs(input, ABS_MT_POSITION_X, pos->x);
522 input_report_abs(input, ABS_MT_POSITION_Y, pos->y);
525 static void report_synaptics_data(struct input_dev *input,
526 const struct bcm5974_config *cfg,
527 const struct tp_finger *f, int raw_n)
529 int abs_p = 0, abs_w = 0;
531 if (raw_n) {
532 int p = raw2int(f->touch_major);
533 int w = raw2int(f->tool_major);
534 if (p > 0 && raw2int(f->origin)) {
535 abs_p = clamp_val(256 * p / cfg->p.max, 0, 255);
536 abs_w = clamp_val(16 * w / cfg->w.max, 0, 15);
540 input_report_abs(input, ABS_PRESSURE, abs_p);
541 input_report_abs(input, ABS_TOOL_WIDTH, abs_w);
544 /* report trackpad data as logical trackpad state */
545 static int report_tp_state(struct bcm5974 *dev, int size)
547 const struct bcm5974_config *c = &dev->cfg;
548 const struct tp_finger *f;
549 struct input_dev *input = dev->input;
550 int raw_n, i, n = 0;
552 if (size < c->tp_offset || (size - c->tp_offset) % SIZEOF_FINGER != 0)
553 return -EIO;
555 /* finger data, le16-aligned */
556 f = (const struct tp_finger *)(dev->tp_data + c->tp_offset);
557 raw_n = (size - c->tp_offset) / SIZEOF_FINGER;
559 for (i = 0; i < raw_n; i++) {
560 if (raw2int(f[i].touch_major) == 0)
561 continue;
562 dev->pos[n].x = raw2int(f[i].abs_x);
563 dev->pos[n].y = c->y.min + c->y.max - raw2int(f[i].abs_y);
564 dev->index[n++] = &f[i];
567 input_mt_assign_slots(input, dev->slots, dev->pos, n);
569 for (i = 0; i < n; i++)
570 report_finger_data(input, dev->slots[i],
571 &dev->pos[i], dev->index[i]);
573 input_mt_sync_frame(input);
575 report_synaptics_data(input, c, f, raw_n);
577 /* type 2 reports button events via ibt only */
578 if (c->tp_type == TYPE2) {
579 int ibt = raw2int(dev->tp_data[BUTTON_TYPE2]);
580 input_report_key(input, BTN_LEFT, ibt);
583 if (c->tp_type == TYPE3)
584 input_report_key(input, BTN_LEFT, dev->tp_data[BUTTON_TYPE3]);
586 input_sync(input);
588 return 0;
591 /* Wellspring initialization constants */
592 #define BCM5974_WELLSPRING_MODE_READ_REQUEST_ID 1
593 #define BCM5974_WELLSPRING_MODE_WRITE_REQUEST_ID 9
594 #define BCM5974_WELLSPRING_MODE_REQUEST_VALUE 0x300
595 #define BCM5974_WELLSPRING_MODE_REQUEST_INDEX 0
596 #define BCM5974_WELLSPRING_MODE_VENDOR_VALUE 0x01
597 #define BCM5974_WELLSPRING_MODE_NORMAL_VALUE 0x08
599 static int bcm5974_wellspring_mode(struct bcm5974 *dev, bool on)
601 int retval = 0, size;
602 char *data;
604 /* Type 3 does not require a mode switch */
605 if (dev->cfg.tp_type == TYPE3)
606 return 0;
608 data = kmalloc(8, GFP_KERNEL);
609 if (!data) {
610 dev_err(&dev->intf->dev, "out of memory\n");
611 retval = -ENOMEM;
612 goto out;
615 /* read configuration */
616 size = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
617 BCM5974_WELLSPRING_MODE_READ_REQUEST_ID,
618 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
619 BCM5974_WELLSPRING_MODE_REQUEST_VALUE,
620 BCM5974_WELLSPRING_MODE_REQUEST_INDEX, data, 8, 5000);
622 if (size != 8) {
623 dev_err(&dev->intf->dev, "could not read from device\n");
624 retval = -EIO;
625 goto out;
628 /* apply the mode switch */
629 data[0] = on ?
630 BCM5974_WELLSPRING_MODE_VENDOR_VALUE :
631 BCM5974_WELLSPRING_MODE_NORMAL_VALUE;
633 /* write configuration */
634 size = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
635 BCM5974_WELLSPRING_MODE_WRITE_REQUEST_ID,
636 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
637 BCM5974_WELLSPRING_MODE_REQUEST_VALUE,
638 BCM5974_WELLSPRING_MODE_REQUEST_INDEX, data, 8, 5000);
640 if (size != 8) {
641 dev_err(&dev->intf->dev, "could not write to device\n");
642 retval = -EIO;
643 goto out;
646 dprintk(2, "bcm5974: switched to %s mode.\n",
647 on ? "wellspring" : "normal");
649 out:
650 kfree(data);
651 return retval;
654 static void bcm5974_irq_button(struct urb *urb)
656 struct bcm5974 *dev = urb->context;
657 struct usb_interface *intf = dev->intf;
658 int error;
660 switch (urb->status) {
661 case 0:
662 break;
663 case -EOVERFLOW:
664 case -ECONNRESET:
665 case -ENOENT:
666 case -ESHUTDOWN:
667 dev_dbg(&intf->dev, "button urb shutting down: %d\n",
668 urb->status);
669 return;
670 default:
671 dev_dbg(&intf->dev, "button urb status: %d\n", urb->status);
672 goto exit;
675 if (report_bt_state(dev, dev->bt_urb->actual_length))
676 dprintk(1, "bcm5974: bad button package, length: %d\n",
677 dev->bt_urb->actual_length);
679 exit:
680 error = usb_submit_urb(dev->bt_urb, GFP_ATOMIC);
681 if (error)
682 dev_err(&intf->dev, "button urb failed: %d\n", error);
685 static void bcm5974_irq_trackpad(struct urb *urb)
687 struct bcm5974 *dev = urb->context;
688 struct usb_interface *intf = dev->intf;
689 int error;
691 switch (urb->status) {
692 case 0:
693 break;
694 case -EOVERFLOW:
695 case -ECONNRESET:
696 case -ENOENT:
697 case -ESHUTDOWN:
698 dev_dbg(&intf->dev, "trackpad urb shutting down: %d\n",
699 urb->status);
700 return;
701 default:
702 dev_dbg(&intf->dev, "trackpad urb status: %d\n", urb->status);
703 goto exit;
706 /* control response ignored */
707 if (dev->tp_urb->actual_length == 2)
708 goto exit;
710 if (report_tp_state(dev, dev->tp_urb->actual_length))
711 dprintk(1, "bcm5974: bad trackpad package, length: %d\n",
712 dev->tp_urb->actual_length);
714 exit:
715 error = usb_submit_urb(dev->tp_urb, GFP_ATOMIC);
716 if (error)
717 dev_err(&intf->dev, "trackpad urb failed: %d\n", error);
721 * The Wellspring trackpad, like many recent Apple trackpads, share
722 * the usb device with the keyboard. Since keyboards are usually
723 * handled by the HID system, the device ends up being handled by two
724 * modules. Setting up the device therefore becomes slightly
725 * complicated. To enable multitouch features, a mode switch is
726 * required, which is usually applied via the control interface of the
727 * device. It can be argued where this switch should take place. In
728 * some drivers, like appletouch, the switch is made during
729 * probe. However, the hid module may also alter the state of the
730 * device, resulting in trackpad malfunction under certain
731 * circumstances. To get around this problem, there is at least one
732 * example that utilizes the USB_QUIRK_RESET_RESUME quirk in order to
733 * receive a reset_resume request rather than the normal resume.
734 * Since the implementation of reset_resume is equal to mode switch
735 * plus start_traffic, it seems easier to always do the switch when
736 * starting traffic on the device.
738 static int bcm5974_start_traffic(struct bcm5974 *dev)
740 int error;
742 error = bcm5974_wellspring_mode(dev, true);
743 if (error) {
744 dprintk(1, "bcm5974: mode switch failed\n");
745 goto err_out;
748 if (dev->bt_urb) {
749 error = usb_submit_urb(dev->bt_urb, GFP_KERNEL);
750 if (error)
751 goto err_reset_mode;
754 error = usb_submit_urb(dev->tp_urb, GFP_KERNEL);
755 if (error)
756 goto err_kill_bt;
758 return 0;
760 err_kill_bt:
761 usb_kill_urb(dev->bt_urb);
762 err_reset_mode:
763 bcm5974_wellspring_mode(dev, false);
764 err_out:
765 return error;
768 static void bcm5974_pause_traffic(struct bcm5974 *dev)
770 usb_kill_urb(dev->tp_urb);
771 usb_kill_urb(dev->bt_urb);
772 bcm5974_wellspring_mode(dev, false);
776 * The code below implements open/close and manual suspend/resume.
777 * All functions may be called in random order.
779 * Opening a suspended device fails with EACCES - permission denied.
781 * Failing a resume leaves the device resumed but closed.
783 static int bcm5974_open(struct input_dev *input)
785 struct bcm5974 *dev = input_get_drvdata(input);
786 int error;
788 error = usb_autopm_get_interface(dev->intf);
789 if (error)
790 return error;
792 mutex_lock(&dev->pm_mutex);
794 error = bcm5974_start_traffic(dev);
795 if (!error)
796 dev->opened = 1;
798 mutex_unlock(&dev->pm_mutex);
800 if (error)
801 usb_autopm_put_interface(dev->intf);
803 return error;
806 static void bcm5974_close(struct input_dev *input)
808 struct bcm5974 *dev = input_get_drvdata(input);
810 mutex_lock(&dev->pm_mutex);
812 bcm5974_pause_traffic(dev);
813 dev->opened = 0;
815 mutex_unlock(&dev->pm_mutex);
817 usb_autopm_put_interface(dev->intf);
820 static int bcm5974_suspend(struct usb_interface *iface, pm_message_t message)
822 struct bcm5974 *dev = usb_get_intfdata(iface);
824 mutex_lock(&dev->pm_mutex);
826 if (dev->opened)
827 bcm5974_pause_traffic(dev);
829 mutex_unlock(&dev->pm_mutex);
831 return 0;
834 static int bcm5974_resume(struct usb_interface *iface)
836 struct bcm5974 *dev = usb_get_intfdata(iface);
837 int error = 0;
839 mutex_lock(&dev->pm_mutex);
841 if (dev->opened)
842 error = bcm5974_start_traffic(dev);
844 mutex_unlock(&dev->pm_mutex);
846 return error;
849 static int bcm5974_probe(struct usb_interface *iface,
850 const struct usb_device_id *id)
852 struct usb_device *udev = interface_to_usbdev(iface);
853 const struct bcm5974_config *cfg;
854 struct bcm5974 *dev;
855 struct input_dev *input_dev;
856 int error = -ENOMEM;
858 /* find the product index */
859 cfg = bcm5974_get_config(udev);
861 /* allocate memory for our device state and initialize it */
862 dev = kzalloc(sizeof(struct bcm5974), GFP_KERNEL);
863 input_dev = input_allocate_device();
864 if (!dev || !input_dev) {
865 dev_err(&iface->dev, "out of memory\n");
866 goto err_free_devs;
869 dev->udev = udev;
870 dev->intf = iface;
871 dev->input = input_dev;
872 dev->cfg = *cfg;
873 mutex_init(&dev->pm_mutex);
875 /* setup urbs */
876 if (cfg->tp_type == TYPE1) {
877 dev->bt_urb = usb_alloc_urb(0, GFP_KERNEL);
878 if (!dev->bt_urb)
879 goto err_free_devs;
882 dev->tp_urb = usb_alloc_urb(0, GFP_KERNEL);
883 if (!dev->tp_urb)
884 goto err_free_bt_urb;
886 if (dev->bt_urb) {
887 dev->bt_data = usb_alloc_coherent(dev->udev,
888 dev->cfg.bt_datalen, GFP_KERNEL,
889 &dev->bt_urb->transfer_dma);
890 if (!dev->bt_data)
891 goto err_free_urb;
894 dev->tp_data = usb_alloc_coherent(dev->udev,
895 dev->cfg.tp_datalen, GFP_KERNEL,
896 &dev->tp_urb->transfer_dma);
897 if (!dev->tp_data)
898 goto err_free_bt_buffer;
900 if (dev->bt_urb)
901 usb_fill_int_urb(dev->bt_urb, udev,
902 usb_rcvintpipe(udev, cfg->bt_ep),
903 dev->bt_data, dev->cfg.bt_datalen,
904 bcm5974_irq_button, dev, 1);
906 usb_fill_int_urb(dev->tp_urb, udev,
907 usb_rcvintpipe(udev, cfg->tp_ep),
908 dev->tp_data, dev->cfg.tp_datalen,
909 bcm5974_irq_trackpad, dev, 1);
911 /* create bcm5974 device */
912 usb_make_path(udev, dev->phys, sizeof(dev->phys));
913 strlcat(dev->phys, "/input0", sizeof(dev->phys));
915 input_dev->name = "bcm5974";
916 input_dev->phys = dev->phys;
917 usb_to_input_id(dev->udev, &input_dev->id);
918 /* report driver capabilities via the version field */
919 input_dev->id.version = cfg->caps;
920 input_dev->dev.parent = &iface->dev;
922 input_set_drvdata(input_dev, dev);
924 input_dev->open = bcm5974_open;
925 input_dev->close = bcm5974_close;
927 setup_events_to_report(input_dev, cfg);
929 error = input_register_device(dev->input);
930 if (error)
931 goto err_free_buffer;
933 /* save our data pointer in this interface device */
934 usb_set_intfdata(iface, dev);
936 return 0;
938 err_free_buffer:
939 usb_free_coherent(dev->udev, dev->cfg.tp_datalen,
940 dev->tp_data, dev->tp_urb->transfer_dma);
941 err_free_bt_buffer:
942 if (dev->bt_urb)
943 usb_free_coherent(dev->udev, dev->cfg.bt_datalen,
944 dev->bt_data, dev->bt_urb->transfer_dma);
945 err_free_urb:
946 usb_free_urb(dev->tp_urb);
947 err_free_bt_urb:
948 usb_free_urb(dev->bt_urb);
949 err_free_devs:
950 usb_set_intfdata(iface, NULL);
951 input_free_device(input_dev);
952 kfree(dev);
953 return error;
956 static void bcm5974_disconnect(struct usb_interface *iface)
958 struct bcm5974 *dev = usb_get_intfdata(iface);
960 usb_set_intfdata(iface, NULL);
962 input_unregister_device(dev->input);
963 usb_free_coherent(dev->udev, dev->cfg.tp_datalen,
964 dev->tp_data, dev->tp_urb->transfer_dma);
965 if (dev->bt_urb)
966 usb_free_coherent(dev->udev, dev->cfg.bt_datalen,
967 dev->bt_data, dev->bt_urb->transfer_dma);
968 usb_free_urb(dev->tp_urb);
969 usb_free_urb(dev->bt_urb);
970 kfree(dev);
973 static struct usb_driver bcm5974_driver = {
974 .name = "bcm5974",
975 .probe = bcm5974_probe,
976 .disconnect = bcm5974_disconnect,
977 .suspend = bcm5974_suspend,
978 .resume = bcm5974_resume,
979 .id_table = bcm5974_table,
980 .supports_autosuspend = 1,
983 module_usb_driver(bcm5974_driver);