Linux Audio

Check our new training course

Loading...
v6.8
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 * Apple USB BCM5974 (Macbook Air and Penryn Macbook Pro) multitouch driver
   4 *
   5 * Copyright (C) 2008	   Henrik Rydberg (rydberg@euromail.se)
   6 * Copyright (C) 2015      John Horan (knasher@gmail.com)
   7 *
   8 * The USB initialization and package decoding was made by
   9 * Scott Shawcroft as part of the touchd user-space driver project:
  10 * Copyright (C) 2008	   Scott Shawcroft (scott.shawcroft@gmail.com)
  11 *
  12 * The BCM5974 driver is based on the appletouch driver:
  13 * Copyright (C) 2001-2004 Greg Kroah-Hartman (greg@kroah.com)
  14 * Copyright (C) 2005      Johannes Berg (johannes@sipsolutions.net)
  15 * Copyright (C) 2005	   Stelian Pop (stelian@popies.net)
  16 * Copyright (C) 2005	   Frank Arnold (frank@scirocco-5v-turbo.de)
  17 * Copyright (C) 2005	   Peter Osterlund (petero2@telia.com)
  18 * Copyright (C) 2005	   Michael Hanselmann (linux-kernel@hansmi.ch)
  19 * Copyright (C) 2006	   Nicolas Boichat (nicolas@boichat.ch)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  20 */
  21
  22#include <linux/kernel.h>
  23#include <linux/errno.h>
 
  24#include <linux/slab.h>
  25#include <linux/module.h>
  26#include <linux/usb/input.h>
  27#include <linux/hid.h>
  28#include <linux/mutex.h>
  29#include <linux/input/mt.h>
  30
  31#define USB_VENDOR_ID_APPLE		0x05ac
  32
  33/* MacbookAir, aka wellspring */
  34#define USB_DEVICE_ID_APPLE_WELLSPRING_ANSI	0x0223
  35#define USB_DEVICE_ID_APPLE_WELLSPRING_ISO	0x0224
  36#define USB_DEVICE_ID_APPLE_WELLSPRING_JIS	0x0225
  37/* MacbookProPenryn, aka wellspring2 */
  38#define USB_DEVICE_ID_APPLE_WELLSPRING2_ANSI	0x0230
  39#define USB_DEVICE_ID_APPLE_WELLSPRING2_ISO	0x0231
  40#define USB_DEVICE_ID_APPLE_WELLSPRING2_JIS	0x0232
  41/* Macbook5,1 (unibody), aka wellspring3 */
  42#define USB_DEVICE_ID_APPLE_WELLSPRING3_ANSI	0x0236
  43#define USB_DEVICE_ID_APPLE_WELLSPRING3_ISO	0x0237
  44#define USB_DEVICE_ID_APPLE_WELLSPRING3_JIS	0x0238
  45/* MacbookAir3,2 (unibody), aka wellspring5 */
  46#define USB_DEVICE_ID_APPLE_WELLSPRING4_ANSI	0x023f
  47#define USB_DEVICE_ID_APPLE_WELLSPRING4_ISO	0x0240
  48#define USB_DEVICE_ID_APPLE_WELLSPRING4_JIS	0x0241
  49/* MacbookAir3,1 (unibody), aka wellspring4 */
  50#define USB_DEVICE_ID_APPLE_WELLSPRING4A_ANSI	0x0242
  51#define USB_DEVICE_ID_APPLE_WELLSPRING4A_ISO	0x0243
  52#define USB_DEVICE_ID_APPLE_WELLSPRING4A_JIS	0x0244
  53/* Macbook8 (unibody, March 2011) */
  54#define USB_DEVICE_ID_APPLE_WELLSPRING5_ANSI	0x0245
  55#define USB_DEVICE_ID_APPLE_WELLSPRING5_ISO	0x0246
  56#define USB_DEVICE_ID_APPLE_WELLSPRING5_JIS	0x0247
  57/* MacbookAir4,1 (unibody, July 2011) */
  58#define USB_DEVICE_ID_APPLE_WELLSPRING6A_ANSI	0x0249
  59#define USB_DEVICE_ID_APPLE_WELLSPRING6A_ISO	0x024a
  60#define USB_DEVICE_ID_APPLE_WELLSPRING6A_JIS	0x024b
  61/* MacbookAir4,2 (unibody, July 2011) */
  62#define USB_DEVICE_ID_APPLE_WELLSPRING6_ANSI	0x024c
  63#define USB_DEVICE_ID_APPLE_WELLSPRING6_ISO	0x024d
  64#define USB_DEVICE_ID_APPLE_WELLSPRING6_JIS	0x024e
  65/* Macbook8,2 (unibody) */
  66#define USB_DEVICE_ID_APPLE_WELLSPRING5A_ANSI	0x0252
  67#define USB_DEVICE_ID_APPLE_WELLSPRING5A_ISO	0x0253
  68#define USB_DEVICE_ID_APPLE_WELLSPRING5A_JIS	0x0254
  69/* MacbookPro10,1 (unibody, June 2012) */
  70#define USB_DEVICE_ID_APPLE_WELLSPRING7_ANSI	0x0262
  71#define USB_DEVICE_ID_APPLE_WELLSPRING7_ISO	0x0263
  72#define USB_DEVICE_ID_APPLE_WELLSPRING7_JIS	0x0264
  73/* MacbookPro10,2 (unibody, October 2012) */
  74#define USB_DEVICE_ID_APPLE_WELLSPRING7A_ANSI	0x0259
  75#define USB_DEVICE_ID_APPLE_WELLSPRING7A_ISO	0x025a
  76#define USB_DEVICE_ID_APPLE_WELLSPRING7A_JIS	0x025b
  77/* MacbookAir6,2 (unibody, June 2013) */
  78#define USB_DEVICE_ID_APPLE_WELLSPRING8_ANSI	0x0290
  79#define USB_DEVICE_ID_APPLE_WELLSPRING8_ISO	0x0291
  80#define USB_DEVICE_ID_APPLE_WELLSPRING8_JIS	0x0292
  81/* MacbookPro12,1 (2015) */
  82#define USB_DEVICE_ID_APPLE_WELLSPRING9_ANSI	0x0272
  83#define USB_DEVICE_ID_APPLE_WELLSPRING9_ISO	0x0273
  84#define USB_DEVICE_ID_APPLE_WELLSPRING9_JIS	0x0274
  85
  86#define BCM5974_DEVICE(prod) {					\
  87	.match_flags = (USB_DEVICE_ID_MATCH_DEVICE |		\
  88			USB_DEVICE_ID_MATCH_INT_CLASS |		\
  89			USB_DEVICE_ID_MATCH_INT_PROTOCOL),	\
  90	.idVendor = USB_VENDOR_ID_APPLE,			\
  91	.idProduct = (prod),					\
  92	.bInterfaceClass = USB_INTERFACE_CLASS_HID,		\
  93	.bInterfaceProtocol = USB_INTERFACE_PROTOCOL_MOUSE	\
  94}
  95
  96/* table of devices that work with this driver */
  97static const struct usb_device_id bcm5974_table[] = {
  98	/* MacbookAir1.1 */
  99	BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING_ANSI),
 100	BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING_ISO),
 101	BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING_JIS),
 102	/* MacbookProPenryn */
 103	BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING2_ANSI),
 104	BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING2_ISO),
 105	BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING2_JIS),
 106	/* Macbook5,1 */
 107	BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING3_ANSI),
 108	BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING3_ISO),
 109	BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING3_JIS),
 110	/* MacbookAir3,2 */
 111	BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING4_ANSI),
 112	BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING4_ISO),
 113	BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING4_JIS),
 114	/* MacbookAir3,1 */
 115	BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING4A_ANSI),
 116	BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING4A_ISO),
 117	BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING4A_JIS),
 118	/* MacbookPro8 */
 119	BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING5_ANSI),
 120	BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING5_ISO),
 121	BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING5_JIS),
 122	/* MacbookAir4,1 */
 123	BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING6A_ANSI),
 124	BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING6A_ISO),
 125	BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING6A_JIS),
 126	/* MacbookAir4,2 */
 127	BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING6_ANSI),
 128	BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING6_ISO),
 129	BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING6_JIS),
 130	/* MacbookPro8,2 */
 131	BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING5A_ANSI),
 132	BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING5A_ISO),
 133	BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING5A_JIS),
 134	/* MacbookPro10,1 */
 135	BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING7_ANSI),
 136	BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING7_ISO),
 137	BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING7_JIS),
 138	/* MacbookPro10,2 */
 139	BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING7A_ANSI),
 140	BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING7A_ISO),
 141	BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING7A_JIS),
 142	/* MacbookAir6,2 */
 143	BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING8_ANSI),
 144	BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING8_ISO),
 145	BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING8_JIS),
 146	/* MacbookPro12,1 */
 147	BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING9_ANSI),
 148	BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING9_ISO),
 149	BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING9_JIS),
 150	/* Terminating entry */
 151	{}
 152};
 153MODULE_DEVICE_TABLE(usb, bcm5974_table);
 154
 155MODULE_AUTHOR("Henrik Rydberg");
 156MODULE_DESCRIPTION("Apple USB BCM5974 multitouch driver");
 157MODULE_LICENSE("GPL");
 158
 159#define dprintk(level, format, a...)\
 160	{ if (debug >= level) printk(KERN_DEBUG format, ##a); }
 161
 162static int debug = 1;
 163module_param(debug, int, 0644);
 164MODULE_PARM_DESC(debug, "Activate debugging output");
 165
 166/* button data structure */
 167struct bt_data {
 168	u8 unknown1;		/* constant */
 169	u8 button;		/* left button */
 170	u8 rel_x;		/* relative x coordinate */
 171	u8 rel_y;		/* relative y coordinate */
 172};
 173
 174/* trackpad header types */
 175enum tp_type {
 176	TYPE1,			/* plain trackpad */
 177	TYPE2,			/* button integrated in trackpad */
 178	TYPE3,			/* additional header fields since June 2013 */
 179	TYPE4			/* additional header field for pressure data */
 180};
 181
 182/* trackpad finger data offsets, le16-aligned */
 183#define HEADER_TYPE1		(13 * sizeof(__le16))
 184#define HEADER_TYPE2		(15 * sizeof(__le16))
 185#define HEADER_TYPE3		(19 * sizeof(__le16))
 186#define HEADER_TYPE4		(23 * sizeof(__le16))
 187
 188/* trackpad button data offsets */
 189#define BUTTON_TYPE1		0
 190#define BUTTON_TYPE2		15
 191#define BUTTON_TYPE3		23
 192#define BUTTON_TYPE4		31
 193
 194/* list of device capability bits */
 195#define HAS_INTEGRATED_BUTTON	1
 196
 197/* trackpad finger data block size */
 198#define FSIZE_TYPE1		(14 * sizeof(__le16))
 199#define FSIZE_TYPE2		(14 * sizeof(__le16))
 200#define FSIZE_TYPE3		(14 * sizeof(__le16))
 201#define FSIZE_TYPE4		(15 * sizeof(__le16))
 202
 203/* offset from header to finger struct */
 204#define DELTA_TYPE1		(0 * sizeof(__le16))
 205#define DELTA_TYPE2		(0 * sizeof(__le16))
 206#define DELTA_TYPE3		(0 * sizeof(__le16))
 207#define DELTA_TYPE4		(1 * sizeof(__le16))
 208
 209/* usb control message mode switch data */
 210#define USBMSG_TYPE1		8, 0x300, 0, 0, 0x1, 0x8
 211#define USBMSG_TYPE2		8, 0x300, 0, 0, 0x1, 0x8
 212#define USBMSG_TYPE3		8, 0x300, 0, 0, 0x1, 0x8
 213#define USBMSG_TYPE4		2, 0x302, 2, 1, 0x1, 0x0
 214
 215/* Wellspring initialization constants */
 216#define BCM5974_WELLSPRING_MODE_READ_REQUEST_ID		1
 217#define BCM5974_WELLSPRING_MODE_WRITE_REQUEST_ID	9
 218
 219/* trackpad finger structure, le16-aligned */
 220struct tp_finger {
 221	__le16 origin;		/* zero when switching track finger */
 222	__le16 abs_x;		/* absolute x coodinate */
 223	__le16 abs_y;		/* absolute y coodinate */
 224	__le16 rel_x;		/* relative x coodinate */
 225	__le16 rel_y;		/* relative y coodinate */
 226	__le16 tool_major;	/* tool area, major axis */
 227	__le16 tool_minor;	/* tool area, minor axis */
 228	__le16 orientation;	/* 16384 when point, else 15 bit angle */
 229	__le16 touch_major;	/* touch area, major axis */
 230	__le16 touch_minor;	/* touch area, minor axis */
 231	__le16 unused[2];	/* zeros */
 232	__le16 pressure;	/* pressure on forcetouch touchpad */
 233	__le16 multi;		/* one finger: varies, more fingers: constant */
 234} __attribute__((packed,aligned(2)));
 235
 236/* trackpad finger data size, empirically at least ten fingers */
 237#define MAX_FINGERS		16
 
 238#define MAX_FINGER_ORIENTATION	16384
 239
 240/* device-specific parameters */
 241struct bcm5974_param {
 242	int snratio;		/* signal-to-noise ratio */
 243	int min;		/* device minimum reading */
 244	int max;		/* device maximum reading */
 
 245};
 246
 247/* device-specific configuration */
 248struct bcm5974_config {
 249	int ansi, iso, jis;	/* the product id of this device */
 250	int caps;		/* device capability bitmask */
 251	int bt_ep;		/* the endpoint of the button interface */
 252	int bt_datalen;		/* data length of the button interface */
 253	int tp_ep;		/* the endpoint of the trackpad interface */
 254	enum tp_type tp_type;	/* type of trackpad interface */
 255	int tp_header;		/* bytes in header block */
 256	int tp_datalen;		/* data length of the trackpad interface */
 257	int tp_button;		/* offset to button data */
 258	int tp_fsize;		/* bytes in single finger block */
 259	int tp_delta;		/* offset from header to finger struct */
 260	int um_size;		/* usb control message length */
 261	int um_req_val;		/* usb control message value */
 262	int um_req_idx;		/* usb control message index */
 263	int um_switch_idx;	/* usb control message mode switch index */
 264	int um_switch_on;	/* usb control message mode switch on */
 265	int um_switch_off;	/* usb control message mode switch off */
 266	struct bcm5974_param p;	/* finger pressure limits */
 267	struct bcm5974_param w;	/* finger width limits */
 268	struct bcm5974_param x;	/* horizontal limits */
 269	struct bcm5974_param y;	/* vertical limits */
 270	struct bcm5974_param o;	/* orientation limits */
 271};
 272
 273/* logical device structure */
 274struct bcm5974 {
 275	char phys[64];
 276	struct usb_device *udev;	/* usb device */
 277	struct usb_interface *intf;	/* our interface */
 278	struct input_dev *input;	/* input dev */
 279	struct bcm5974_config cfg;	/* device configuration */
 280	struct mutex pm_mutex;		/* serialize access to open/suspend */
 281	int opened;			/* 1: opened, 0: closed */
 282	struct urb *bt_urb;		/* button usb request block */
 283	struct bt_data *bt_data;	/* button transferred data */
 284	struct urb *tp_urb;		/* trackpad usb request block */
 285	u8 *tp_data;			/* trackpad transferred data */
 286	const struct tp_finger *index[MAX_FINGERS];	/* finger index data */
 287	struct input_mt_pos pos[MAX_FINGERS];		/* position array */
 288	int slots[MAX_FINGERS];				/* slot assignments */
 289};
 290
 291/* trackpad finger block data, le16-aligned */
 292static const struct tp_finger *get_tp_finger(const struct bcm5974 *dev, int i)
 293{
 294	const struct bcm5974_config *c = &dev->cfg;
 295	u8 *f_base = dev->tp_data + c->tp_header + c->tp_delta;
 296
 297	return (const struct tp_finger *)(f_base + i * c->tp_fsize);
 298}
 299
 300#define DATAFORMAT(type)				\
 301	type,						\
 302	HEADER_##type,					\
 303	HEADER_##type + (MAX_FINGERS) * (FSIZE_##type),	\
 304	BUTTON_##type,					\
 305	FSIZE_##type,					\
 306	DELTA_##type,					\
 307	USBMSG_##type
 308
 309/* logical signal quality */
 310#define SN_PRESSURE	45		/* pressure signal-to-noise ratio */
 311#define SN_WIDTH	25		/* width signal-to-noise ratio */
 312#define SN_COORD	250		/* coordinate signal-to-noise ratio */
 313#define SN_ORIENT	10		/* orientation signal-to-noise ratio */
 
 
 
 314
 315/* device constants */
 316static const struct bcm5974_config bcm5974_config_table[] = {
 317	{
 318		USB_DEVICE_ID_APPLE_WELLSPRING_ANSI,
 319		USB_DEVICE_ID_APPLE_WELLSPRING_ISO,
 320		USB_DEVICE_ID_APPLE_WELLSPRING_JIS,
 321		0,
 322		0x84, sizeof(struct bt_data),
 323		0x81, DATAFORMAT(TYPE1),
 324		{ SN_PRESSURE, 0, 256 },
 325		{ SN_WIDTH, 0, 2048 },
 326		{ SN_COORD, -4824, 5342 },
 327		{ SN_COORD, -172, 5820 },
 328		{ SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION }
 329	},
 330	{
 331		USB_DEVICE_ID_APPLE_WELLSPRING2_ANSI,
 332		USB_DEVICE_ID_APPLE_WELLSPRING2_ISO,
 333		USB_DEVICE_ID_APPLE_WELLSPRING2_JIS,
 334		0,
 335		0x84, sizeof(struct bt_data),
 336		0x81, DATAFORMAT(TYPE1),
 337		{ SN_PRESSURE, 0, 256 },
 338		{ SN_WIDTH, 0, 2048 },
 339		{ SN_COORD, -4824, 4824 },
 340		{ SN_COORD, -172, 4290 },
 341		{ SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION }
 342	},
 343	{
 344		USB_DEVICE_ID_APPLE_WELLSPRING3_ANSI,
 345		USB_DEVICE_ID_APPLE_WELLSPRING3_ISO,
 346		USB_DEVICE_ID_APPLE_WELLSPRING3_JIS,
 347		HAS_INTEGRATED_BUTTON,
 348		0x84, sizeof(struct bt_data),
 349		0x81, DATAFORMAT(TYPE2),
 350		{ SN_PRESSURE, 0, 300 },
 351		{ SN_WIDTH, 0, 2048 },
 352		{ SN_COORD, -4460, 5166 },
 353		{ SN_COORD, -75, 6700 },
 354		{ SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION }
 355	},
 356	{
 357		USB_DEVICE_ID_APPLE_WELLSPRING4_ANSI,
 358		USB_DEVICE_ID_APPLE_WELLSPRING4_ISO,
 359		USB_DEVICE_ID_APPLE_WELLSPRING4_JIS,
 360		HAS_INTEGRATED_BUTTON,
 361		0x84, sizeof(struct bt_data),
 362		0x81, DATAFORMAT(TYPE2),
 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_WELLSPRING4A_ANSI,
 371		USB_DEVICE_ID_APPLE_WELLSPRING4A_ISO,
 372		USB_DEVICE_ID_APPLE_WELLSPRING4A_JIS,
 373		HAS_INTEGRATED_BUTTON,
 374		0x84, sizeof(struct bt_data),
 375		0x81, DATAFORMAT(TYPE2),
 376		{ SN_PRESSURE, 0, 300 },
 377		{ SN_WIDTH, 0, 2048 },
 378		{ SN_COORD, -4616, 5112 },
 379		{ SN_COORD, -142, 5234 },
 380		{ SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION }
 381	},
 382	{
 383		USB_DEVICE_ID_APPLE_WELLSPRING5_ANSI,
 384		USB_DEVICE_ID_APPLE_WELLSPRING5_ISO,
 385		USB_DEVICE_ID_APPLE_WELLSPRING5_JIS,
 386		HAS_INTEGRATED_BUTTON,
 387		0x84, sizeof(struct bt_data),
 388		0x81, DATAFORMAT(TYPE2),
 389		{ SN_PRESSURE, 0, 300 },
 390		{ SN_WIDTH, 0, 2048 },
 391		{ SN_COORD, -4415, 5050 },
 392		{ SN_COORD, -55, 6680 },
 393		{ SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION }
 394	},
 395	{
 396		USB_DEVICE_ID_APPLE_WELLSPRING6_ANSI,
 397		USB_DEVICE_ID_APPLE_WELLSPRING6_ISO,
 398		USB_DEVICE_ID_APPLE_WELLSPRING6_JIS,
 399		HAS_INTEGRATED_BUTTON,
 400		0x84, sizeof(struct bt_data),
 401		0x81, DATAFORMAT(TYPE2),
 402		{ SN_PRESSURE, 0, 300 },
 403		{ SN_WIDTH, 0, 2048 },
 404		{ SN_COORD, -4620, 5140 },
 405		{ SN_COORD, -150, 6600 },
 406		{ SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION }
 407	},
 408	{
 409		USB_DEVICE_ID_APPLE_WELLSPRING5A_ANSI,
 410		USB_DEVICE_ID_APPLE_WELLSPRING5A_ISO,
 411		USB_DEVICE_ID_APPLE_WELLSPRING5A_JIS,
 412		HAS_INTEGRATED_BUTTON,
 413		0x84, sizeof(struct bt_data),
 414		0x81, DATAFORMAT(TYPE2),
 415		{ SN_PRESSURE, 0, 300 },
 416		{ SN_WIDTH, 0, 2048 },
 417		{ SN_COORD, -4750, 5280 },
 418		{ SN_COORD, -150, 6730 },
 419		{ SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION }
 420	},
 421	{
 422		USB_DEVICE_ID_APPLE_WELLSPRING6A_ANSI,
 423		USB_DEVICE_ID_APPLE_WELLSPRING6A_ISO,
 424		USB_DEVICE_ID_APPLE_WELLSPRING6A_JIS,
 425		HAS_INTEGRATED_BUTTON,
 426		0x84, sizeof(struct bt_data),
 427		0x81, DATAFORMAT(TYPE2),
 428		{ SN_PRESSURE, 0, 300 },
 429		{ SN_WIDTH, 0, 2048 },
 430		{ SN_COORD, -4620, 5140 },
 431		{ SN_COORD, -150, 6600 },
 432		{ SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION }
 433	},
 434	{
 435		USB_DEVICE_ID_APPLE_WELLSPRING7_ANSI,
 436		USB_DEVICE_ID_APPLE_WELLSPRING7_ISO,
 437		USB_DEVICE_ID_APPLE_WELLSPRING7_JIS,
 438		HAS_INTEGRATED_BUTTON,
 439		0x84, sizeof(struct bt_data),
 440		0x81, DATAFORMAT(TYPE2),
 441		{ SN_PRESSURE, 0, 300 },
 442		{ SN_WIDTH, 0, 2048 },
 443		{ SN_COORD, -4750, 5280 },
 444		{ SN_COORD, -150, 6730 },
 445		{ SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION }
 446	},
 447	{
 448		USB_DEVICE_ID_APPLE_WELLSPRING7A_ANSI,
 449		USB_DEVICE_ID_APPLE_WELLSPRING7A_ISO,
 450		USB_DEVICE_ID_APPLE_WELLSPRING7A_JIS,
 451		HAS_INTEGRATED_BUTTON,
 452		0x84, sizeof(struct bt_data),
 453		0x81, DATAFORMAT(TYPE2),
 454		{ SN_PRESSURE, 0, 300 },
 455		{ SN_WIDTH, 0, 2048 },
 456		{ SN_COORD, -4750, 5280 },
 457		{ SN_COORD, -150, 6730 },
 458		{ SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION }
 459	},
 460	{
 461		USB_DEVICE_ID_APPLE_WELLSPRING8_ANSI,
 462		USB_DEVICE_ID_APPLE_WELLSPRING8_ISO,
 463		USB_DEVICE_ID_APPLE_WELLSPRING8_JIS,
 464		HAS_INTEGRATED_BUTTON,
 465		0, sizeof(struct bt_data),
 466		0x83, DATAFORMAT(TYPE3),
 467		{ SN_PRESSURE, 0, 300 },
 468		{ SN_WIDTH, 0, 2048 },
 469		{ SN_COORD, -4620, 5140 },
 470		{ SN_COORD, -150, 6600 },
 471		{ SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION }
 472	},
 473	{
 474		USB_DEVICE_ID_APPLE_WELLSPRING9_ANSI,
 475		USB_DEVICE_ID_APPLE_WELLSPRING9_ISO,
 476		USB_DEVICE_ID_APPLE_WELLSPRING9_JIS,
 477		HAS_INTEGRATED_BUTTON,
 478		0, sizeof(struct bt_data),
 479		0x83, DATAFORMAT(TYPE4),
 480		{ SN_PRESSURE, 0, 300 },
 481		{ SN_WIDTH, 0, 2048 },
 482		{ SN_COORD, -4828, 5345 },
 483		{ SN_COORD, -203, 6803 },
 484		{ SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION }
 485	},
 486	{}
 487};
 488
 489/* return the device-specific configuration by device */
 490static const struct bcm5974_config *bcm5974_get_config(struct usb_device *udev)
 491{
 492	u16 id = le16_to_cpu(udev->descriptor.idProduct);
 493	const struct bcm5974_config *cfg;
 494
 495	for (cfg = bcm5974_config_table; cfg->ansi; ++cfg)
 496		if (cfg->ansi == id || cfg->iso == id || cfg->jis == id)
 497			return cfg;
 498
 499	return bcm5974_config_table;
 500}
 501
 502/* convert 16-bit little endian to signed integer */
 503static inline int raw2int(__le16 x)
 504{
 505	return (signed short)le16_to_cpu(x);
 506}
 507
 508static void set_abs(struct input_dev *input, unsigned int code,
 509		    const struct bcm5974_param *p)
 510{
 511	int fuzz = p->snratio ? (p->max - p->min) / p->snratio : 0;
 512	input_set_abs_params(input, code, p->min, p->max, fuzz, 0);
 
 
 
 
 
 
 
 513}
 514
 515/* setup which logical events to report */
 516static void setup_events_to_report(struct input_dev *input_dev,
 517				   const struct bcm5974_config *cfg)
 518{
 519	__set_bit(EV_ABS, input_dev->evbit);
 520
 521	/* for synaptics only */
 522	input_set_abs_params(input_dev, ABS_PRESSURE, 0, 256, 5, 0);
 523	input_set_abs_params(input_dev, ABS_TOOL_WIDTH, 0, 16, 0, 0);
 
 
 
 
 
 524
 525	/* finger touch area */
 526	set_abs(input_dev, ABS_MT_TOUCH_MAJOR, &cfg->w);
 527	set_abs(input_dev, ABS_MT_TOUCH_MINOR, &cfg->w);
 
 
 528	/* finger approach area */
 529	set_abs(input_dev, ABS_MT_WIDTH_MAJOR, &cfg->w);
 530	set_abs(input_dev, ABS_MT_WIDTH_MINOR, &cfg->w);
 
 
 531	/* finger orientation */
 532	set_abs(input_dev, ABS_MT_ORIENTATION, &cfg->o);
 
 
 533	/* finger position */
 534	set_abs(input_dev, ABS_MT_POSITION_X, &cfg->x);
 535	set_abs(input_dev, ABS_MT_POSITION_Y, &cfg->y);
 
 
 536
 537	__set_bit(EV_KEY, input_dev->evbit);
 
 
 
 
 
 538	__set_bit(BTN_LEFT, input_dev->keybit);
 539
 
 540	if (cfg->caps & HAS_INTEGRATED_BUTTON)
 541		__set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
 542
 543	input_mt_init_slots(input_dev, MAX_FINGERS,
 544		INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED | INPUT_MT_TRACK);
 545}
 546
 547/* report button data as logical button state */
 548static int report_bt_state(struct bcm5974 *dev, int size)
 549{
 550	if (size != sizeof(struct bt_data))
 551		return -EIO;
 552
 553	dprintk(7,
 554		"bcm5974: button data: %x %x %x %x\n",
 555		dev->bt_data->unknown1, dev->bt_data->button,
 556		dev->bt_data->rel_x, dev->bt_data->rel_y);
 557
 558	input_report_key(dev->input, BTN_LEFT, dev->bt_data->button);
 559	input_sync(dev->input);
 560
 561	return 0;
 562}
 563
 564static void report_finger_data(struct input_dev *input, int slot,
 565			       const struct input_mt_pos *pos,
 566			       const struct tp_finger *f)
 567{
 568	input_mt_slot(input, slot);
 569	input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
 570
 571	input_report_abs(input, ABS_MT_TOUCH_MAJOR,
 572			 raw2int(f->touch_major) << 1);
 573	input_report_abs(input, ABS_MT_TOUCH_MINOR,
 574			 raw2int(f->touch_minor) << 1);
 575	input_report_abs(input, ABS_MT_WIDTH_MAJOR,
 576			 raw2int(f->tool_major) << 1);
 577	input_report_abs(input, ABS_MT_WIDTH_MINOR,
 578			 raw2int(f->tool_minor) << 1);
 579	input_report_abs(input, ABS_MT_ORIENTATION,
 580			 MAX_FINGER_ORIENTATION - raw2int(f->orientation));
 581	input_report_abs(input, ABS_MT_POSITION_X, pos->x);
 582	input_report_abs(input, ABS_MT_POSITION_Y, pos->y);
 583}
 584
 585static void report_synaptics_data(struct input_dev *input,
 586				  const struct bcm5974_config *cfg,
 587				  const struct tp_finger *f, int raw_n)
 588{
 589	int abs_p = 0, abs_w = 0;
 590
 591	if (raw_n) {
 592		int p = raw2int(f->touch_major);
 593		int w = raw2int(f->tool_major);
 594		if (p > 0 && raw2int(f->origin)) {
 595			abs_p = clamp_val(256 * p / cfg->p.max, 0, 255);
 596			abs_w = clamp_val(16 * w / cfg->w.max, 0, 15);
 597		}
 598	}
 599
 600	input_report_abs(input, ABS_PRESSURE, abs_p);
 601	input_report_abs(input, ABS_TOOL_WIDTH, abs_w);
 602}
 603
 604/* report trackpad data as logical trackpad state */
 605static int report_tp_state(struct bcm5974 *dev, int size)
 606{
 607	const struct bcm5974_config *c = &dev->cfg;
 608	const struct tp_finger *f;
 609	struct input_dev *input = dev->input;
 610	int raw_n, i, n = 0;
 
 
 611
 612	if (size < c->tp_header || (size - c->tp_header) % c->tp_fsize != 0)
 613		return -EIO;
 614
 615	raw_n = (size - c->tp_header) / c->tp_fsize;
 
 
 
 
 
 616
 617	for (i = 0; i < raw_n; i++) {
 618		f = get_tp_finger(dev, i);
 619		if (raw2int(f->touch_major) == 0)
 620			continue;
 621		dev->pos[n].x = raw2int(f->abs_x);
 622		dev->pos[n].y = c->y.min + c->y.max - raw2int(f->abs_y);
 623		dev->index[n++] = f;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 624	}
 625
 626	input_mt_assign_slots(input, dev->slots, dev->pos, n, 0);
 
 
 
 
 
 
 
 
 
 
 
 
 
 627
 628	for (i = 0; i < n; i++)
 629		report_finger_data(input, dev->slots[i],
 630				   &dev->pos[i], dev->index[i]);
 631
 632	input_mt_sync_frame(input);
 
 
 633
 634	report_synaptics_data(input, c, get_tp_finger(dev, 0), raw_n);
 
 
 
 635
 636	/* later types report button events via integrated button only */
 637	if (c->caps & HAS_INTEGRATED_BUTTON) {
 638		int ibt = raw2int(dev->tp_data[c->tp_button]);
 639		input_report_key(input, BTN_LEFT, ibt);
 640	}
 641
 
 
 
 
 642	input_sync(input);
 643
 644	return 0;
 645}
 646
 
 
 
 
 
 
 
 
 647static int bcm5974_wellspring_mode(struct bcm5974 *dev, bool on)
 648{
 649	const struct bcm5974_config *c = &dev->cfg;
 650	int retval = 0, size;
 651	char *data;
 652
 653	/* Type 3 does not require a mode switch */
 654	if (c->tp_type == TYPE3)
 655		return 0;
 656
 657	data = kmalloc(c->um_size, GFP_KERNEL);
 658	if (!data) {
 659		dev_err(&dev->intf->dev, "out of memory\n");
 660		retval = -ENOMEM;
 661		goto out;
 662	}
 663
 664	/* read configuration */
 665	size = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
 666			BCM5974_WELLSPRING_MODE_READ_REQUEST_ID,
 667			USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
 668			c->um_req_val, c->um_req_idx, data, c->um_size, 5000);
 
 669
 670	if (size != c->um_size) {
 671		dev_err(&dev->intf->dev, "could not read from device\n");
 672		retval = -EIO;
 673		goto out;
 674	}
 675
 676	/* apply the mode switch */
 677	data[c->um_switch_idx] = on ? c->um_switch_on : c->um_switch_off;
 
 
 678
 679	/* write configuration */
 680	size = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
 681			BCM5974_WELLSPRING_MODE_WRITE_REQUEST_ID,
 682			USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
 683			c->um_req_val, c->um_req_idx, data, c->um_size, 5000);
 
 684
 685	if (size != c->um_size) {
 686		dev_err(&dev->intf->dev, "could not write to device\n");
 687		retval = -EIO;
 688		goto out;
 689	}
 690
 691	dprintk(2, "bcm5974: switched to %s mode.\n",
 692		on ? "wellspring" : "normal");
 693
 694 out:
 695	kfree(data);
 696	return retval;
 697}
 698
 699static void bcm5974_irq_button(struct urb *urb)
 700{
 701	struct bcm5974 *dev = urb->context;
 702	struct usb_interface *intf = dev->intf;
 703	int error;
 704
 705	switch (urb->status) {
 706	case 0:
 707		break;
 708	case -EOVERFLOW:
 709	case -ECONNRESET:
 710	case -ENOENT:
 711	case -ESHUTDOWN:
 712		dev_dbg(&intf->dev, "button urb shutting down: %d\n",
 713			urb->status);
 714		return;
 715	default:
 716		dev_dbg(&intf->dev, "button urb status: %d\n", urb->status);
 717		goto exit;
 718	}
 719
 720	if (report_bt_state(dev, dev->bt_urb->actual_length))
 721		dprintk(1, "bcm5974: bad button package, length: %d\n",
 722			dev->bt_urb->actual_length);
 723
 724exit:
 725	error = usb_submit_urb(dev->bt_urb, GFP_ATOMIC);
 726	if (error)
 727		dev_err(&intf->dev, "button urb failed: %d\n", error);
 728}
 729
 730static void bcm5974_irq_trackpad(struct urb *urb)
 731{
 732	struct bcm5974 *dev = urb->context;
 733	struct usb_interface *intf = dev->intf;
 734	int error;
 735
 736	switch (urb->status) {
 737	case 0:
 738		break;
 739	case -EOVERFLOW:
 740	case -ECONNRESET:
 741	case -ENOENT:
 742	case -ESHUTDOWN:
 743		dev_dbg(&intf->dev, "trackpad urb shutting down: %d\n",
 744			urb->status);
 745		return;
 746	default:
 747		dev_dbg(&intf->dev, "trackpad urb status: %d\n", urb->status);
 748		goto exit;
 749	}
 750
 751	/* control response ignored */
 752	if (dev->tp_urb->actual_length == 2)
 753		goto exit;
 754
 755	if (report_tp_state(dev, dev->tp_urb->actual_length))
 756		dprintk(1, "bcm5974: bad trackpad package, length: %d\n",
 757			dev->tp_urb->actual_length);
 758
 759exit:
 760	error = usb_submit_urb(dev->tp_urb, GFP_ATOMIC);
 761	if (error)
 762		dev_err(&intf->dev, "trackpad urb failed: %d\n", error);
 763}
 764
 765/*
 766 * The Wellspring trackpad, like many recent Apple trackpads, share
 767 * the usb device with the keyboard. Since keyboards are usually
 768 * handled by the HID system, the device ends up being handled by two
 769 * modules. Setting up the device therefore becomes slightly
 770 * complicated. To enable multitouch features, a mode switch is
 771 * required, which is usually applied via the control interface of the
 772 * device.  It can be argued where this switch should take place. In
 773 * some drivers, like appletouch, the switch is made during
 774 * probe. However, the hid module may also alter the state of the
 775 * device, resulting in trackpad malfunction under certain
 776 * circumstances. To get around this problem, there is at least one
 777 * example that utilizes the USB_QUIRK_RESET_RESUME quirk in order to
 778 * receive a reset_resume request rather than the normal resume.
 779 * Since the implementation of reset_resume is equal to mode switch
 780 * plus start_traffic, it seems easier to always do the switch when
 781 * starting traffic on the device.
 782 */
 783static int bcm5974_start_traffic(struct bcm5974 *dev)
 784{
 785	int error;
 786
 787	error = bcm5974_wellspring_mode(dev, true);
 788	if (error) {
 789		dprintk(1, "bcm5974: mode switch failed\n");
 790		goto err_out;
 791	}
 792
 793	if (dev->bt_urb) {
 794		error = usb_submit_urb(dev->bt_urb, GFP_KERNEL);
 795		if (error)
 796			goto err_reset_mode;
 797	}
 798
 799	error = usb_submit_urb(dev->tp_urb, GFP_KERNEL);
 800	if (error)
 801		goto err_kill_bt;
 802
 803	return 0;
 804
 805err_kill_bt:
 806	usb_kill_urb(dev->bt_urb);
 807err_reset_mode:
 808	bcm5974_wellspring_mode(dev, false);
 809err_out:
 810	return error;
 811}
 812
 813static void bcm5974_pause_traffic(struct bcm5974 *dev)
 814{
 815	usb_kill_urb(dev->tp_urb);
 816	usb_kill_urb(dev->bt_urb);
 817	bcm5974_wellspring_mode(dev, false);
 818}
 819
 820/*
 821 * The code below implements open/close and manual suspend/resume.
 822 * All functions may be called in random order.
 823 *
 824 * Opening a suspended device fails with EACCES - permission denied.
 825 *
 826 * Failing a resume leaves the device resumed but closed.
 827 */
 828static int bcm5974_open(struct input_dev *input)
 829{
 830	struct bcm5974 *dev = input_get_drvdata(input);
 831	int error;
 832
 833	error = usb_autopm_get_interface(dev->intf);
 834	if (error)
 835		return error;
 836
 837	mutex_lock(&dev->pm_mutex);
 838
 839	error = bcm5974_start_traffic(dev);
 840	if (!error)
 841		dev->opened = 1;
 842
 843	mutex_unlock(&dev->pm_mutex);
 844
 845	if (error)
 846		usb_autopm_put_interface(dev->intf);
 847
 848	return error;
 849}
 850
 851static void bcm5974_close(struct input_dev *input)
 852{
 853	struct bcm5974 *dev = input_get_drvdata(input);
 854
 855	mutex_lock(&dev->pm_mutex);
 856
 857	bcm5974_pause_traffic(dev);
 858	dev->opened = 0;
 859
 860	mutex_unlock(&dev->pm_mutex);
 861
 862	usb_autopm_put_interface(dev->intf);
 863}
 864
 865static int bcm5974_suspend(struct usb_interface *iface, pm_message_t message)
 866{
 867	struct bcm5974 *dev = usb_get_intfdata(iface);
 868
 869	mutex_lock(&dev->pm_mutex);
 870
 871	if (dev->opened)
 872		bcm5974_pause_traffic(dev);
 873
 874	mutex_unlock(&dev->pm_mutex);
 875
 876	return 0;
 877}
 878
 879static int bcm5974_resume(struct usb_interface *iface)
 880{
 881	struct bcm5974 *dev = usb_get_intfdata(iface);
 882	int error = 0;
 883
 884	mutex_lock(&dev->pm_mutex);
 885
 886	if (dev->opened)
 887		error = bcm5974_start_traffic(dev);
 888
 889	mutex_unlock(&dev->pm_mutex);
 890
 891	return error;
 892}
 893
 894static int bcm5974_probe(struct usb_interface *iface,
 895			 const struct usb_device_id *id)
 896{
 897	struct usb_device *udev = interface_to_usbdev(iface);
 898	const struct bcm5974_config *cfg;
 899	struct bcm5974 *dev;
 900	struct input_dev *input_dev;
 901	int error = -ENOMEM;
 902
 903	/* find the product index */
 904	cfg = bcm5974_get_config(udev);
 905
 906	/* allocate memory for our device state and initialize it */
 907	dev = kzalloc(sizeof(struct bcm5974), GFP_KERNEL);
 908	input_dev = input_allocate_device();
 909	if (!dev || !input_dev) {
 910		dev_err(&iface->dev, "out of memory\n");
 911		goto err_free_devs;
 912	}
 913
 914	dev->udev = udev;
 915	dev->intf = iface;
 916	dev->input = input_dev;
 917	dev->cfg = *cfg;
 918	mutex_init(&dev->pm_mutex);
 919
 920	/* setup urbs */
 921	if (cfg->tp_type == TYPE1) {
 922		dev->bt_urb = usb_alloc_urb(0, GFP_KERNEL);
 923		if (!dev->bt_urb)
 924			goto err_free_devs;
 925	}
 926
 927	dev->tp_urb = usb_alloc_urb(0, GFP_KERNEL);
 928	if (!dev->tp_urb)
 929		goto err_free_bt_urb;
 930
 931	if (dev->bt_urb) {
 932		dev->bt_data = usb_alloc_coherent(dev->udev,
 933					  dev->cfg.bt_datalen, GFP_KERNEL,
 934					  &dev->bt_urb->transfer_dma);
 935		if (!dev->bt_data)
 936			goto err_free_urb;
 937	}
 938
 939	dev->tp_data = usb_alloc_coherent(dev->udev,
 940					  dev->cfg.tp_datalen, GFP_KERNEL,
 941					  &dev->tp_urb->transfer_dma);
 942	if (!dev->tp_data)
 943		goto err_free_bt_buffer;
 944
 945	if (dev->bt_urb) {
 946		usb_fill_int_urb(dev->bt_urb, udev,
 947				 usb_rcvintpipe(udev, cfg->bt_ep),
 948				 dev->bt_data, dev->cfg.bt_datalen,
 949				 bcm5974_irq_button, dev, 1);
 950
 951		dev->bt_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
 952	}
 953
 954	usb_fill_int_urb(dev->tp_urb, udev,
 955			 usb_rcvintpipe(udev, cfg->tp_ep),
 956			 dev->tp_data, dev->cfg.tp_datalen,
 957			 bcm5974_irq_trackpad, dev, 1);
 958
 959	dev->tp_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
 960
 961	/* create bcm5974 device */
 962	usb_make_path(udev, dev->phys, sizeof(dev->phys));
 963	strlcat(dev->phys, "/input0", sizeof(dev->phys));
 964
 965	input_dev->name = "bcm5974";
 966	input_dev->phys = dev->phys;
 967	usb_to_input_id(dev->udev, &input_dev->id);
 968	/* report driver capabilities via the version field */
 969	input_dev->id.version = cfg->caps;
 970	input_dev->dev.parent = &iface->dev;
 971
 972	input_set_drvdata(input_dev, dev);
 973
 974	input_dev->open = bcm5974_open;
 975	input_dev->close = bcm5974_close;
 976
 977	setup_events_to_report(input_dev, cfg);
 978
 979	error = input_register_device(dev->input);
 980	if (error)
 981		goto err_free_buffer;
 982
 983	/* save our data pointer in this interface device */
 984	usb_set_intfdata(iface, dev);
 985
 986	return 0;
 987
 988err_free_buffer:
 989	usb_free_coherent(dev->udev, dev->cfg.tp_datalen,
 990		dev->tp_data, dev->tp_urb->transfer_dma);
 991err_free_bt_buffer:
 992	if (dev->bt_urb)
 993		usb_free_coherent(dev->udev, dev->cfg.bt_datalen,
 994				  dev->bt_data, dev->bt_urb->transfer_dma);
 995err_free_urb:
 996	usb_free_urb(dev->tp_urb);
 997err_free_bt_urb:
 998	usb_free_urb(dev->bt_urb);
 999err_free_devs:
1000	usb_set_intfdata(iface, NULL);
1001	input_free_device(input_dev);
1002	kfree(dev);
1003	return error;
1004}
1005
1006static void bcm5974_disconnect(struct usb_interface *iface)
1007{
1008	struct bcm5974 *dev = usb_get_intfdata(iface);
1009
1010	usb_set_intfdata(iface, NULL);
1011
1012	input_unregister_device(dev->input);
1013	usb_free_coherent(dev->udev, dev->cfg.tp_datalen,
1014			  dev->tp_data, dev->tp_urb->transfer_dma);
1015	if (dev->bt_urb)
1016		usb_free_coherent(dev->udev, dev->cfg.bt_datalen,
1017				  dev->bt_data, dev->bt_urb->transfer_dma);
1018	usb_free_urb(dev->tp_urb);
1019	usb_free_urb(dev->bt_urb);
1020	kfree(dev);
1021}
1022
1023static struct usb_driver bcm5974_driver = {
1024	.name			= "bcm5974",
1025	.probe			= bcm5974_probe,
1026	.disconnect		= bcm5974_disconnect,
1027	.suspend		= bcm5974_suspend,
1028	.resume			= bcm5974_resume,
1029	.id_table		= bcm5974_table,
1030	.supports_autosuspend	= 1,
1031};
1032
1033module_usb_driver(bcm5974_driver);
v3.5.6
 
  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 */
 98static 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};
142MODULE_DEVICE_TABLE(usb, bcm5974_table);
143
144MODULE_AUTHOR("Henrik Rydberg");
145MODULE_DESCRIPTION("Apple USB BCM5974 multitouch driver");
146MODULE_LICENSE("GPL");
147
148#define dprintk(level, format, a...)\
149	{ if (debug >= level) printk(KERN_DEBUG format, ##a); }
150
151static int debug = 1;
152module_param(debug, int, 0644);
153MODULE_PARM_DESC(debug, "Activate debugging output");
154
155/* button data structure */
156struct 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 */
164enum 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 */
180struct 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 size_major;	/* finger size, major axis? */
187	__le16 size_minor;	/* finger size, minor axis? */
188	__le16 orientation;	/* 16384 when point, else 15 bit angle */
189	__le16 force_major;	/* trackpad force, major axis? */
190	__le16 force_minor;	/* trackpad force, 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 SIZEOF_FINGER		sizeof(struct tp_finger)
197#define SIZEOF_ALL_FINGERS	(16 * SIZEOF_FINGER)
198#define MAX_FINGER_ORIENTATION	16384
199
200/* device-specific parameters */
201struct bcm5974_param {
202	int dim;		/* logical dimension */
203	int fuzz;		/* logical noise value */
204	int devmin;		/* device minimum reading */
205	int devmax;		/* device maximum reading */
206};
207
208/* device-specific configuration */
209struct bcm5974_config {
210	int ansi, iso, jis;	/* the product id of this device */
211	int caps;		/* device capability bitmask */
212	int bt_ep;		/* the endpoint of the button interface */
213	int bt_datalen;		/* data length of the button interface */
214	int tp_ep;		/* the endpoint of the trackpad interface */
215	enum tp_type tp_type;	/* type of trackpad interface */
216	int tp_offset;		/* offset to trackpad finger data */
217	int tp_datalen;		/* data length of the trackpad interface */
 
 
 
 
 
 
 
 
 
218	struct bcm5974_param p;	/* finger pressure limits */
219	struct bcm5974_param w;	/* finger width limits */
220	struct bcm5974_param x;	/* horizontal limits */
221	struct bcm5974_param y;	/* vertical limits */
 
222};
223
224/* logical device structure */
225struct bcm5974 {
226	char phys[64];
227	struct usb_device *udev;	/* usb device */
228	struct usb_interface *intf;	/* our interface */
229	struct input_dev *input;	/* input dev */
230	struct bcm5974_config cfg;	/* device configuration */
231	struct mutex pm_mutex;		/* serialize access to open/suspend */
232	int opened;			/* 1: opened, 0: closed */
233	struct urb *bt_urb;		/* button usb request block */
234	struct bt_data *bt_data;	/* button transferred data */
235	struct urb *tp_urb;		/* trackpad usb request block */
236	u8 *tp_data;			/* trackpad transferred data */
237	int fingers;			/* number of fingers on trackpad */
 
 
238};
239
240/* logical dimensions */
241#define DIM_PRESSURE	256		/* maximum finger pressure */
242#define DIM_WIDTH	16		/* maximum finger width */
243#define DIM_X		1280		/* maximum trackpad x value */
244#define DIM_Y		800		/* maximum trackpad y value */
 
 
 
 
 
 
 
 
 
 
 
 
245
246/* logical signal quality */
247#define SN_PRESSURE	45		/* pressure signal-to-noise ratio */
248#define SN_WIDTH	100		/* width signal-to-noise ratio */
249#define SN_COORD	250		/* coordinate signal-to-noise ratio */
250
251/* pressure thresholds */
252#define PRESSURE_LOW	(2 * DIM_PRESSURE / SN_PRESSURE)
253#define PRESSURE_HIGH	(3 * PRESSURE_LOW)
254
255/* device constants */
256static const struct bcm5974_config bcm5974_config_table[] = {
257	{
258		USB_DEVICE_ID_APPLE_WELLSPRING_ANSI,
259		USB_DEVICE_ID_APPLE_WELLSPRING_ISO,
260		USB_DEVICE_ID_APPLE_WELLSPRING_JIS,
261		0,
262		0x84, sizeof(struct bt_data),
263		0x81, TYPE1, FINGER_TYPE1, FINGER_TYPE1 + SIZEOF_ALL_FINGERS,
264		{ DIM_PRESSURE, DIM_PRESSURE / SN_PRESSURE, 0, 256 },
265		{ DIM_WIDTH, DIM_WIDTH / SN_WIDTH, 0, 2048 },
266		{ DIM_X, DIM_X / SN_COORD, -4824, 5342 },
267		{ DIM_Y, DIM_Y / SN_COORD, -172, 5820 }
 
268	},
269	{
270		USB_DEVICE_ID_APPLE_WELLSPRING2_ANSI,
271		USB_DEVICE_ID_APPLE_WELLSPRING2_ISO,
272		USB_DEVICE_ID_APPLE_WELLSPRING2_JIS,
273		0,
274		0x84, sizeof(struct bt_data),
275		0x81, TYPE1, FINGER_TYPE1, FINGER_TYPE1 + SIZEOF_ALL_FINGERS,
276		{ DIM_PRESSURE, DIM_PRESSURE / SN_PRESSURE, 0, 256 },
277		{ DIM_WIDTH, DIM_WIDTH / SN_WIDTH, 0, 2048 },
278		{ DIM_X, DIM_X / SN_COORD, -4824, 4824 },
279		{ DIM_Y, DIM_Y / SN_COORD, -172, 4290 }
 
280	},
281	{
282		USB_DEVICE_ID_APPLE_WELLSPRING3_ANSI,
283		USB_DEVICE_ID_APPLE_WELLSPRING3_ISO,
284		USB_DEVICE_ID_APPLE_WELLSPRING3_JIS,
285		HAS_INTEGRATED_BUTTON,
286		0x84, sizeof(struct bt_data),
287		0x81, TYPE2, FINGER_TYPE2, FINGER_TYPE2 + SIZEOF_ALL_FINGERS,
288		{ DIM_PRESSURE, DIM_PRESSURE / SN_PRESSURE, 0, 300 },
289		{ DIM_WIDTH, DIM_WIDTH / SN_WIDTH, 0, 2048 },
290		{ DIM_X, DIM_X / SN_COORD, -4460, 5166 },
291		{ DIM_Y, DIM_Y / SN_COORD, -75, 6700 }
 
292	},
293	{
294		USB_DEVICE_ID_APPLE_WELLSPRING4_ANSI,
295		USB_DEVICE_ID_APPLE_WELLSPRING4_ISO,
296		USB_DEVICE_ID_APPLE_WELLSPRING4_JIS,
297		HAS_INTEGRATED_BUTTON,
298		0x84, sizeof(struct bt_data),
299		0x81, TYPE2, FINGER_TYPE2, FINGER_TYPE2 + SIZEOF_ALL_FINGERS,
300		{ DIM_PRESSURE, DIM_PRESSURE / SN_PRESSURE, 0, 300 },
301		{ DIM_WIDTH, DIM_WIDTH / SN_WIDTH, 0, 2048 },
302		{ DIM_X, DIM_X / SN_COORD, -4620, 5140 },
303		{ DIM_Y, DIM_Y / SN_COORD, -150, 6600 }
 
304	},
305	{
306		USB_DEVICE_ID_APPLE_WELLSPRING4A_ANSI,
307		USB_DEVICE_ID_APPLE_WELLSPRING4A_ISO,
308		USB_DEVICE_ID_APPLE_WELLSPRING4A_JIS,
309		HAS_INTEGRATED_BUTTON,
310		0x84, sizeof(struct bt_data),
311		0x81, TYPE2, FINGER_TYPE2, FINGER_TYPE2 + SIZEOF_ALL_FINGERS,
312		{ DIM_PRESSURE, DIM_PRESSURE / SN_PRESSURE, 0, 300 },
313		{ DIM_WIDTH, DIM_WIDTH / SN_WIDTH, 0, 2048 },
314		{ DIM_X, DIM_X / SN_COORD, -4616, 5112 },
315		{ DIM_Y, DIM_Y / SN_COORD, -142, 5234 }
 
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		{ DIM_PRESSURE, DIM_PRESSURE / SN_PRESSURE, 0, 300 },
325		{ DIM_WIDTH, DIM_WIDTH / SN_WIDTH, 0, 2048 },
326		{ DIM_X, DIM_X / SN_COORD, -4415, 5050 },
327		{ DIM_Y, DIM_Y / SN_COORD, -55, 6680 }
 
328	},
329	{
330		USB_DEVICE_ID_APPLE_WELLSPRING6_ANSI,
331		USB_DEVICE_ID_APPLE_WELLSPRING6_ISO,
332		USB_DEVICE_ID_APPLE_WELLSPRING6_JIS,
333		HAS_INTEGRATED_BUTTON,
334		0x84, sizeof(struct bt_data),
335		0x81, TYPE2, FINGER_TYPE2, FINGER_TYPE2 + SIZEOF_ALL_FINGERS,
336		{ DIM_PRESSURE, DIM_PRESSURE / SN_PRESSURE, 0, 300 },
337		{ DIM_WIDTH, DIM_WIDTH / SN_WIDTH, 0, 2048 },
338		{ DIM_X, DIM_X / SN_COORD, -4620, 5140 },
339		{ DIM_Y, DIM_Y / SN_COORD, -150, 6600 }
 
340	},
341	{
342		USB_DEVICE_ID_APPLE_WELLSPRING5A_ANSI,
343		USB_DEVICE_ID_APPLE_WELLSPRING5A_ISO,
344		USB_DEVICE_ID_APPLE_WELLSPRING5A_JIS,
345		HAS_INTEGRATED_BUTTON,
346		0x84, sizeof(struct bt_data),
347		0x81, TYPE2, FINGER_TYPE2, FINGER_TYPE2 + SIZEOF_ALL_FINGERS,
348		{ DIM_PRESSURE, DIM_PRESSURE / SN_PRESSURE, 0, 300 },
349		{ DIM_WIDTH, DIM_WIDTH / SN_WIDTH, 0, 2048 },
350		{ DIM_X, DIM_X / SN_COORD, -4750, 5280 },
351		{ DIM_Y, DIM_Y / SN_COORD, -150, 6730 }
 
352	},
353	{
354		USB_DEVICE_ID_APPLE_WELLSPRING6A_ANSI,
355		USB_DEVICE_ID_APPLE_WELLSPRING6A_ISO,
356		USB_DEVICE_ID_APPLE_WELLSPRING6A_JIS,
357		HAS_INTEGRATED_BUTTON,
358		0x84, sizeof(struct bt_data),
359		0x81, TYPE2, FINGER_TYPE2, FINGER_TYPE2 + SIZEOF_ALL_FINGERS,
360		{ DIM_PRESSURE, DIM_PRESSURE / SN_PRESSURE, 0, 300 },
361		{ DIM_WIDTH, DIM_WIDTH / SN_WIDTH, 0, 2048 },
362		{ DIM_X, DIM_X / SN_COORD, -4620, 5140 },
363		{ DIM_Y, DIM_Y / SN_COORD, -150, 6600 }
 
364	},
365	{
366		USB_DEVICE_ID_APPLE_WELLSPRING7_ANSI,
367		USB_DEVICE_ID_APPLE_WELLSPRING7_ISO,
368		USB_DEVICE_ID_APPLE_WELLSPRING7_JIS,
369		HAS_INTEGRATED_BUTTON,
370		0x84, sizeof(struct bt_data),
371		0x81, TYPE2, FINGER_TYPE2, FINGER_TYPE2 + SIZEOF_ALL_FINGERS,
372		{ DIM_PRESSURE, DIM_PRESSURE / SN_PRESSURE, 0, 300 },
373		{ DIM_WIDTH, DIM_WIDTH / SN_WIDTH, 0, 2048 },
374		{ DIM_X, DIM_X / SN_COORD, -4750, 5280 },
375		{ DIM_Y, DIM_Y / SN_COORD, -150, 6730 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
376	},
377	{}
378};
379
380/* return the device-specific configuration by device */
381static const struct bcm5974_config *bcm5974_get_config(struct usb_device *udev)
382{
383	u16 id = le16_to_cpu(udev->descriptor.idProduct);
384	const struct bcm5974_config *cfg;
385
386	for (cfg = bcm5974_config_table; cfg->ansi; ++cfg)
387		if (cfg->ansi == id || cfg->iso == id || cfg->jis == id)
388			return cfg;
389
390	return bcm5974_config_table;
391}
392
393/* convert 16-bit little endian to signed integer */
394static inline int raw2int(__le16 x)
395{
396	return (signed short)le16_to_cpu(x);
397}
398
399/* scale device data to logical dimensions (asserts devmin < devmax) */
400static inline int int2scale(const struct bcm5974_param *p, int x)
401{
402	return x * p->dim / (p->devmax - p->devmin);
403}
404
405/* all logical value ranges are [0,dim). */
406static inline int int2bound(const struct bcm5974_param *p, int x)
407{
408	int s = int2scale(p, x);
409
410	return clamp_val(s, 0, p->dim - 1);
411}
412
413/* setup which logical events to report */
414static void setup_events_to_report(struct input_dev *input_dev,
415				   const struct bcm5974_config *cfg)
416{
417	__set_bit(EV_ABS, input_dev->evbit);
418
419	input_set_abs_params(input_dev, ABS_PRESSURE,
420				0, cfg->p.dim, cfg->p.fuzz, 0);
421	input_set_abs_params(input_dev, ABS_TOOL_WIDTH,
422				0, cfg->w.dim, cfg->w.fuzz, 0);
423	input_set_abs_params(input_dev, ABS_X,
424				0, cfg->x.dim, cfg->x.fuzz, 0);
425	input_set_abs_params(input_dev, ABS_Y,
426				0, cfg->y.dim, cfg->y.fuzz, 0);
427
428	/* finger touch area */
429	input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
430			     cfg->w.devmin, cfg->w.devmax, 0, 0);
431	input_set_abs_params(input_dev, ABS_MT_TOUCH_MINOR,
432			     cfg->w.devmin, cfg->w.devmax, 0, 0);
433	/* finger approach area */
434	input_set_abs_params(input_dev, ABS_MT_WIDTH_MAJOR,
435			     cfg->w.devmin, cfg->w.devmax, 0, 0);
436	input_set_abs_params(input_dev, ABS_MT_WIDTH_MINOR,
437			     cfg->w.devmin, cfg->w.devmax, 0, 0);
438	/* finger orientation */
439	input_set_abs_params(input_dev, ABS_MT_ORIENTATION,
440			     -MAX_FINGER_ORIENTATION,
441			     MAX_FINGER_ORIENTATION, 0, 0);
442	/* finger position */
443	input_set_abs_params(input_dev, ABS_MT_POSITION_X,
444			     cfg->x.devmin, cfg->x.devmax, 0, 0);
445	input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
446			     cfg->y.devmin, cfg->y.devmax, 0, 0);
447
448	__set_bit(EV_KEY, input_dev->evbit);
449	__set_bit(BTN_TOUCH, input_dev->keybit);
450	__set_bit(BTN_TOOL_FINGER, input_dev->keybit);
451	__set_bit(BTN_TOOL_DOUBLETAP, input_dev->keybit);
452	__set_bit(BTN_TOOL_TRIPLETAP, input_dev->keybit);
453	__set_bit(BTN_TOOL_QUADTAP, input_dev->keybit);
454	__set_bit(BTN_LEFT, input_dev->keybit);
455
456	__set_bit(INPUT_PROP_POINTER, input_dev->propbit);
457	if (cfg->caps & HAS_INTEGRATED_BUTTON)
458		__set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
459
460	input_set_events_per_packet(input_dev, 60);
 
461}
462
463/* report button data as logical button state */
464static int report_bt_state(struct bcm5974 *dev, int size)
465{
466	if (size != sizeof(struct bt_data))
467		return -EIO;
468
469	dprintk(7,
470		"bcm5974: button data: %x %x %x %x\n",
471		dev->bt_data->unknown1, dev->bt_data->button,
472		dev->bt_data->rel_x, dev->bt_data->rel_y);
473
474	input_report_key(dev->input, BTN_LEFT, dev->bt_data->button);
475	input_sync(dev->input);
476
477	return 0;
478}
479
480static void report_finger_data(struct input_dev *input,
481			       const struct bcm5974_config *cfg,
482			       const struct tp_finger *f)
483{
 
 
 
484	input_report_abs(input, ABS_MT_TOUCH_MAJOR,
485			 raw2int(f->force_major) << 1);
486	input_report_abs(input, ABS_MT_TOUCH_MINOR,
487			 raw2int(f->force_minor) << 1);
488	input_report_abs(input, ABS_MT_WIDTH_MAJOR,
489			 raw2int(f->size_major) << 1);
490	input_report_abs(input, ABS_MT_WIDTH_MINOR,
491			 raw2int(f->size_minor) << 1);
492	input_report_abs(input, ABS_MT_ORIENTATION,
493			 MAX_FINGER_ORIENTATION - raw2int(f->orientation));
494	input_report_abs(input, ABS_MT_POSITION_X, raw2int(f->abs_x));
495	input_report_abs(input, ABS_MT_POSITION_Y,
496			 cfg->y.devmin + cfg->y.devmax - raw2int(f->abs_y));
497	input_mt_sync(input);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
498}
499
500/* report trackpad data as logical trackpad state */
501static 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_p, raw_w, raw_x, raw_y, raw_n, i;
507	int ptest, origin, ibt = 0, nmin = 0, nmax = 0;
508	int abs_p = 0, abs_w = 0, abs_x = 0, abs_y = 0;
509
510	if (size < c->tp_offset || (size - c->tp_offset) % SIZEOF_FINGER != 0)
511		return -EIO;
512
513	/* finger data, le16-aligned */
514	f = (const struct tp_finger *)(dev->tp_data + c->tp_offset);
515	raw_n = (size - c->tp_offset) / SIZEOF_FINGER;
516
517	/* always track the first finger; when detached, start over */
518	if (raw_n) {
519
520		/* report raw trackpad data */
521		for (i = 0; i < raw_n; i++)
522			report_finger_data(input, c, &f[i]);
523
524		raw_p = raw2int(f->force_major);
525		raw_w = raw2int(f->size_major);
526		raw_x = raw2int(f->abs_x);
527		raw_y = raw2int(f->abs_y);
528
529		dprintk(9,
530			"bcm5974: "
531			"raw: p: %+05d w: %+05d x: %+05d y: %+05d n: %d\n",
532			raw_p, raw_w, raw_x, raw_y, raw_n);
533
534		ptest = int2bound(&c->p, raw_p);
535		origin = raw2int(f->origin);
536
537		/* while tracking finger still valid, count all fingers */
538		if (ptest > PRESSURE_LOW && origin) {
539			abs_p = ptest;
540			abs_w = int2bound(&c->w, raw_w);
541			abs_x = int2bound(&c->x, raw_x - c->x.devmin);
542			abs_y = int2bound(&c->y, c->y.devmax - raw_y);
543			while (raw_n--) {
544				ptest = int2bound(&c->p,
545						  raw2int(f->force_major));
546				if (ptest > PRESSURE_LOW)
547					nmax++;
548				if (ptest > PRESSURE_HIGH)
549					nmin++;
550				f++;
551			}
552		}
553	}
554
555	/* set the integrated button if applicable */
556	if (c->tp_type == TYPE2)
557		ibt = raw2int(dev->tp_data[BUTTON_TYPE2]);
558
559	if (dev->fingers < nmin)
560		dev->fingers = nmin;
561	if (dev->fingers > nmax)
562		dev->fingers = nmax;
563
564	input_report_key(input, BTN_TOUCH, dev->fingers > 0);
565	input_report_key(input, BTN_TOOL_FINGER, dev->fingers == 1);
566	input_report_key(input, BTN_TOOL_DOUBLETAP, dev->fingers == 2);
567	input_report_key(input, BTN_TOOL_TRIPLETAP, dev->fingers == 3);
568	input_report_key(input, BTN_TOOL_QUADTAP, dev->fingers > 3);
569
570	input_report_abs(input, ABS_PRESSURE, abs_p);
571	input_report_abs(input, ABS_TOOL_WIDTH, abs_w);
 
572
573	if (abs_p) {
574		input_report_abs(input, ABS_X, abs_x);
575		input_report_abs(input, ABS_Y, abs_y);
576
577		dprintk(8,
578			"bcm5974: abs: p: %+05d w: %+05d x: %+05d y: %+05d "
579			"nmin: %d nmax: %d n: %d ibt: %d\n", abs_p, abs_w,
580			abs_x, abs_y, nmin, nmax, dev->fingers, ibt);
581
 
 
 
 
582	}
583
584	/* type 2 reports button events via ibt only */
585	if (c->tp_type == TYPE2)
586		input_report_key(input, BTN_LEFT, ibt);
587
588	input_sync(input);
589
590	return 0;
591}
592
593/* Wellspring initialization constants */
594#define BCM5974_WELLSPRING_MODE_READ_REQUEST_ID		1
595#define BCM5974_WELLSPRING_MODE_WRITE_REQUEST_ID	9
596#define BCM5974_WELLSPRING_MODE_REQUEST_VALUE		0x300
597#define BCM5974_WELLSPRING_MODE_REQUEST_INDEX		0
598#define BCM5974_WELLSPRING_MODE_VENDOR_VALUE		0x01
599#define BCM5974_WELLSPRING_MODE_NORMAL_VALUE		0x08
600
601static int bcm5974_wellspring_mode(struct bcm5974 *dev, bool on)
602{
603	char *data = kmalloc(8, GFP_KERNEL);
604	int retval = 0, size;
 
605
 
 
 
 
 
606	if (!data) {
607		dev_err(&dev->intf->dev, "out of memory\n");
608		retval = -ENOMEM;
609		goto out;
610	}
611
612	/* read configuration */
613	size = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
614			BCM5974_WELLSPRING_MODE_READ_REQUEST_ID,
615			USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
616			BCM5974_WELLSPRING_MODE_REQUEST_VALUE,
617			BCM5974_WELLSPRING_MODE_REQUEST_INDEX, data, 8, 5000);
618
619	if (size != 8) {
620		dev_err(&dev->intf->dev, "could not read from device\n");
621		retval = -EIO;
622		goto out;
623	}
624
625	/* apply the mode switch */
626	data[0] = on ?
627		BCM5974_WELLSPRING_MODE_VENDOR_VALUE :
628		BCM5974_WELLSPRING_MODE_NORMAL_VALUE;
629
630	/* write configuration */
631	size = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
632			BCM5974_WELLSPRING_MODE_WRITE_REQUEST_ID,
633			USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
634			BCM5974_WELLSPRING_MODE_REQUEST_VALUE,
635			BCM5974_WELLSPRING_MODE_REQUEST_INDEX, data, 8, 5000);
636
637	if (size != 8) {
638		dev_err(&dev->intf->dev, "could not write to device\n");
639		retval = -EIO;
640		goto out;
641	}
642
643	dprintk(2, "bcm5974: switched to %s mode.\n",
644		on ? "wellspring" : "normal");
645
646 out:
647	kfree(data);
648	return retval;
649}
650
651static void bcm5974_irq_button(struct urb *urb)
652{
653	struct bcm5974 *dev = urb->context;
654	struct usb_interface *intf = dev->intf;
655	int error;
656
657	switch (urb->status) {
658	case 0:
659		break;
660	case -EOVERFLOW:
661	case -ECONNRESET:
662	case -ENOENT:
663	case -ESHUTDOWN:
664		dev_dbg(&intf->dev, "button urb shutting down: %d\n",
665			urb->status);
666		return;
667	default:
668		dev_dbg(&intf->dev, "button urb status: %d\n", urb->status);
669		goto exit;
670	}
671
672	if (report_bt_state(dev, dev->bt_urb->actual_length))
673		dprintk(1, "bcm5974: bad button package, length: %d\n",
674			dev->bt_urb->actual_length);
675
676exit:
677	error = usb_submit_urb(dev->bt_urb, GFP_ATOMIC);
678	if (error)
679		dev_err(&intf->dev, "button urb failed: %d\n", error);
680}
681
682static void bcm5974_irq_trackpad(struct urb *urb)
683{
684	struct bcm5974 *dev = urb->context;
685	struct usb_interface *intf = dev->intf;
686	int error;
687
688	switch (urb->status) {
689	case 0:
690		break;
691	case -EOVERFLOW:
692	case -ECONNRESET:
693	case -ENOENT:
694	case -ESHUTDOWN:
695		dev_dbg(&intf->dev, "trackpad urb shutting down: %d\n",
696			urb->status);
697		return;
698	default:
699		dev_dbg(&intf->dev, "trackpad urb status: %d\n", urb->status);
700		goto exit;
701	}
702
703	/* control response ignored */
704	if (dev->tp_urb->actual_length == 2)
705		goto exit;
706
707	if (report_tp_state(dev, dev->tp_urb->actual_length))
708		dprintk(1, "bcm5974: bad trackpad package, length: %d\n",
709			dev->tp_urb->actual_length);
710
711exit:
712	error = usb_submit_urb(dev->tp_urb, GFP_ATOMIC);
713	if (error)
714		dev_err(&intf->dev, "trackpad urb failed: %d\n", error);
715}
716
717/*
718 * The Wellspring trackpad, like many recent Apple trackpads, share
719 * the usb device with the keyboard. Since keyboards are usually
720 * handled by the HID system, the device ends up being handled by two
721 * modules. Setting up the device therefore becomes slightly
722 * complicated. To enable multitouch features, a mode switch is
723 * required, which is usually applied via the control interface of the
724 * device.  It can be argued where this switch should take place. In
725 * some drivers, like appletouch, the switch is made during
726 * probe. However, the hid module may also alter the state of the
727 * device, resulting in trackpad malfunction under certain
728 * circumstances. To get around this problem, there is at least one
729 * example that utilizes the USB_QUIRK_RESET_RESUME quirk in order to
730 * receive a reset_resume request rather than the normal resume.
731 * Since the implementation of reset_resume is equal to mode switch
732 * plus start_traffic, it seems easier to always do the switch when
733 * starting traffic on the device.
734 */
735static int bcm5974_start_traffic(struct bcm5974 *dev)
736{
737	int error;
738
739	error = bcm5974_wellspring_mode(dev, true);
740	if (error) {
741		dprintk(1, "bcm5974: mode switch failed\n");
742		goto err_out;
743	}
744
745	error = usb_submit_urb(dev->bt_urb, GFP_KERNEL);
746	if (error)
747		goto err_reset_mode;
 
 
748
749	error = usb_submit_urb(dev->tp_urb, GFP_KERNEL);
750	if (error)
751		goto err_kill_bt;
752
753	return 0;
754
755err_kill_bt:
756	usb_kill_urb(dev->bt_urb);
757err_reset_mode:
758	bcm5974_wellspring_mode(dev, false);
759err_out:
760	return error;
761}
762
763static void bcm5974_pause_traffic(struct bcm5974 *dev)
764{
765	usb_kill_urb(dev->tp_urb);
766	usb_kill_urb(dev->bt_urb);
767	bcm5974_wellspring_mode(dev, false);
768}
769
770/*
771 * The code below implements open/close and manual suspend/resume.
772 * All functions may be called in random order.
773 *
774 * Opening a suspended device fails with EACCES - permission denied.
775 *
776 * Failing a resume leaves the device resumed but closed.
777 */
778static int bcm5974_open(struct input_dev *input)
779{
780	struct bcm5974 *dev = input_get_drvdata(input);
781	int error;
782
783	error = usb_autopm_get_interface(dev->intf);
784	if (error)
785		return error;
786
787	mutex_lock(&dev->pm_mutex);
788
789	error = bcm5974_start_traffic(dev);
790	if (!error)
791		dev->opened = 1;
792
793	mutex_unlock(&dev->pm_mutex);
794
795	if (error)
796		usb_autopm_put_interface(dev->intf);
797
798	return error;
799}
800
801static void bcm5974_close(struct input_dev *input)
802{
803	struct bcm5974 *dev = input_get_drvdata(input);
804
805	mutex_lock(&dev->pm_mutex);
806
807	bcm5974_pause_traffic(dev);
808	dev->opened = 0;
809
810	mutex_unlock(&dev->pm_mutex);
811
812	usb_autopm_put_interface(dev->intf);
813}
814
815static int bcm5974_suspend(struct usb_interface *iface, pm_message_t message)
816{
817	struct bcm5974 *dev = usb_get_intfdata(iface);
818
819	mutex_lock(&dev->pm_mutex);
820
821	if (dev->opened)
822		bcm5974_pause_traffic(dev);
823
824	mutex_unlock(&dev->pm_mutex);
825
826	return 0;
827}
828
829static int bcm5974_resume(struct usb_interface *iface)
830{
831	struct bcm5974 *dev = usb_get_intfdata(iface);
832	int error = 0;
833
834	mutex_lock(&dev->pm_mutex);
835
836	if (dev->opened)
837		error = bcm5974_start_traffic(dev);
838
839	mutex_unlock(&dev->pm_mutex);
840
841	return error;
842}
843
844static int bcm5974_probe(struct usb_interface *iface,
845			 const struct usb_device_id *id)
846{
847	struct usb_device *udev = interface_to_usbdev(iface);
848	const struct bcm5974_config *cfg;
849	struct bcm5974 *dev;
850	struct input_dev *input_dev;
851	int error = -ENOMEM;
852
853	/* find the product index */
854	cfg = bcm5974_get_config(udev);
855
856	/* allocate memory for our device state and initialize it */
857	dev = kzalloc(sizeof(struct bcm5974), GFP_KERNEL);
858	input_dev = input_allocate_device();
859	if (!dev || !input_dev) {
860		dev_err(&iface->dev, "out of memory\n");
861		goto err_free_devs;
862	}
863
864	dev->udev = udev;
865	dev->intf = iface;
866	dev->input = input_dev;
867	dev->cfg = *cfg;
868	mutex_init(&dev->pm_mutex);
869
870	/* setup urbs */
871	dev->bt_urb = usb_alloc_urb(0, GFP_KERNEL);
872	if (!dev->bt_urb)
873		goto err_free_devs;
 
 
874
875	dev->tp_urb = usb_alloc_urb(0, GFP_KERNEL);
876	if (!dev->tp_urb)
877		goto err_free_bt_urb;
878
879	dev->bt_data = usb_alloc_coherent(dev->udev,
 
880					  dev->cfg.bt_datalen, GFP_KERNEL,
881					  &dev->bt_urb->transfer_dma);
882	if (!dev->bt_data)
883		goto err_free_urb;
 
884
885	dev->tp_data = usb_alloc_coherent(dev->udev,
886					  dev->cfg.tp_datalen, GFP_KERNEL,
887					  &dev->tp_urb->transfer_dma);
888	if (!dev->tp_data)
889		goto err_free_bt_buffer;
890
891	usb_fill_int_urb(dev->bt_urb, udev,
892			 usb_rcvintpipe(udev, cfg->bt_ep),
893			 dev->bt_data, dev->cfg.bt_datalen,
894			 bcm5974_irq_button, dev, 1);
 
 
 
 
895
896	usb_fill_int_urb(dev->tp_urb, udev,
897			 usb_rcvintpipe(udev, cfg->tp_ep),
898			 dev->tp_data, dev->cfg.tp_datalen,
899			 bcm5974_irq_trackpad, dev, 1);
900
 
 
901	/* create bcm5974 device */
902	usb_make_path(udev, dev->phys, sizeof(dev->phys));
903	strlcat(dev->phys, "/input0", sizeof(dev->phys));
904
905	input_dev->name = "bcm5974";
906	input_dev->phys = dev->phys;
907	usb_to_input_id(dev->udev, &input_dev->id);
908	/* report driver capabilities via the version field */
909	input_dev->id.version = cfg->caps;
910	input_dev->dev.parent = &iface->dev;
911
912	input_set_drvdata(input_dev, dev);
913
914	input_dev->open = bcm5974_open;
915	input_dev->close = bcm5974_close;
916
917	setup_events_to_report(input_dev, cfg);
918
919	error = input_register_device(dev->input);
920	if (error)
921		goto err_free_buffer;
922
923	/* save our data pointer in this interface device */
924	usb_set_intfdata(iface, dev);
925
926	return 0;
927
928err_free_buffer:
929	usb_free_coherent(dev->udev, dev->cfg.tp_datalen,
930		dev->tp_data, dev->tp_urb->transfer_dma);
931err_free_bt_buffer:
932	usb_free_coherent(dev->udev, dev->cfg.bt_datalen,
933		dev->bt_data, dev->bt_urb->transfer_dma);
 
934err_free_urb:
935	usb_free_urb(dev->tp_urb);
936err_free_bt_urb:
937	usb_free_urb(dev->bt_urb);
938err_free_devs:
939	usb_set_intfdata(iface, NULL);
940	input_free_device(input_dev);
941	kfree(dev);
942	return error;
943}
944
945static void bcm5974_disconnect(struct usb_interface *iface)
946{
947	struct bcm5974 *dev = usb_get_intfdata(iface);
948
949	usb_set_intfdata(iface, NULL);
950
951	input_unregister_device(dev->input);
952	usb_free_coherent(dev->udev, dev->cfg.tp_datalen,
953			  dev->tp_data, dev->tp_urb->transfer_dma);
954	usb_free_coherent(dev->udev, dev->cfg.bt_datalen,
955			  dev->bt_data, dev->bt_urb->transfer_dma);
 
956	usb_free_urb(dev->tp_urb);
957	usb_free_urb(dev->bt_urb);
958	kfree(dev);
959}
960
961static struct usb_driver bcm5974_driver = {
962	.name			= "bcm5974",
963	.probe			= bcm5974_probe,
964	.disconnect		= bcm5974_disconnect,
965	.suspend		= bcm5974_suspend,
966	.resume			= bcm5974_resume,
967	.id_table		= bcm5974_table,
968	.supports_autosuspend	= 1,
969};
970
971module_usb_driver(bcm5974_driver);