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