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