1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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#include <linux/input/mt.h>
44
45#define USB_VENDOR_ID_APPLE 0x05ac
46
47
48#define USB_DEVICE_ID_APPLE_WELLSPRING_ANSI 0x0223
49#define USB_DEVICE_ID_APPLE_WELLSPRING_ISO 0x0224
50#define USB_DEVICE_ID_APPLE_WELLSPRING_JIS 0x0225
51
52#define USB_DEVICE_ID_APPLE_WELLSPRING2_ANSI 0x0230
53#define USB_DEVICE_ID_APPLE_WELLSPRING2_ISO 0x0231
54#define USB_DEVICE_ID_APPLE_WELLSPRING2_JIS 0x0232
55
56#define USB_DEVICE_ID_APPLE_WELLSPRING3_ANSI 0x0236
57#define USB_DEVICE_ID_APPLE_WELLSPRING3_ISO 0x0237
58#define USB_DEVICE_ID_APPLE_WELLSPRING3_JIS 0x0238
59
60#define USB_DEVICE_ID_APPLE_WELLSPRING4_ANSI 0x023f
61#define USB_DEVICE_ID_APPLE_WELLSPRING4_ISO 0x0240
62#define USB_DEVICE_ID_APPLE_WELLSPRING4_JIS 0x0241
63
64#define USB_DEVICE_ID_APPLE_WELLSPRING4A_ANSI 0x0242
65#define USB_DEVICE_ID_APPLE_WELLSPRING4A_ISO 0x0243
66#define USB_DEVICE_ID_APPLE_WELLSPRING4A_JIS 0x0244
67
68#define USB_DEVICE_ID_APPLE_WELLSPRING5_ANSI 0x0245
69#define USB_DEVICE_ID_APPLE_WELLSPRING5_ISO 0x0246
70#define USB_DEVICE_ID_APPLE_WELLSPRING5_JIS 0x0247
71
72#define USB_DEVICE_ID_APPLE_WELLSPRING6A_ANSI 0x0249
73#define USB_DEVICE_ID_APPLE_WELLSPRING6A_ISO 0x024a
74#define USB_DEVICE_ID_APPLE_WELLSPRING6A_JIS 0x024b
75
76#define USB_DEVICE_ID_APPLE_WELLSPRING6_ANSI 0x024c
77#define USB_DEVICE_ID_APPLE_WELLSPRING6_ISO 0x024d
78#define USB_DEVICE_ID_APPLE_WELLSPRING6_JIS 0x024e
79
80#define USB_DEVICE_ID_APPLE_WELLSPRING5A_ANSI 0x0252
81#define USB_DEVICE_ID_APPLE_WELLSPRING5A_ISO 0x0253
82#define USB_DEVICE_ID_APPLE_WELLSPRING5A_JIS 0x0254
83
84#define USB_DEVICE_ID_APPLE_WELLSPRING7_ANSI 0x0262
85#define USB_DEVICE_ID_APPLE_WELLSPRING7_ISO 0x0263
86#define USB_DEVICE_ID_APPLE_WELLSPRING7_JIS 0x0264
87
88#define USB_DEVICE_ID_APPLE_WELLSPRING7A_ANSI 0x0259
89#define USB_DEVICE_ID_APPLE_WELLSPRING7A_ISO 0x025a
90#define USB_DEVICE_ID_APPLE_WELLSPRING7A_JIS 0x025b
91
92#define BCM5974_DEVICE(prod) { \
93 .match_flags = (USB_DEVICE_ID_MATCH_DEVICE | \
94 USB_DEVICE_ID_MATCH_INT_CLASS | \
95 USB_DEVICE_ID_MATCH_INT_PROTOCOL), \
96 .idVendor = USB_VENDOR_ID_APPLE, \
97 .idProduct = (prod), \
98 .bInterfaceClass = USB_INTERFACE_CLASS_HID, \
99 .bInterfaceProtocol = USB_INTERFACE_PROTOCOL_MOUSE \
100}
101
102
103static const struct usb_device_id bcm5974_table[] = {
104
105 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING_ANSI),
106 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING_ISO),
107 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING_JIS),
108
109 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING2_ANSI),
110 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING2_ISO),
111 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING2_JIS),
112
113 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING3_ANSI),
114 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING3_ISO),
115 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING3_JIS),
116
117 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING4_ANSI),
118 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING4_ISO),
119 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING4_JIS),
120
121 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING4A_ANSI),
122 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING4A_ISO),
123 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING4A_JIS),
124
125 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING5_ANSI),
126 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING5_ISO),
127 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING5_JIS),
128
129 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING6A_ANSI),
130 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING6A_ISO),
131 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING6A_JIS),
132
133 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING6_ANSI),
134 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING6_ISO),
135 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING6_JIS),
136
137 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING5A_ANSI),
138 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING5A_ISO),
139 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING5A_JIS),
140
141 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING7_ANSI),
142 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING7_ISO),
143 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING7_JIS),
144
145 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING7A_ANSI),
146 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING7A_ISO),
147 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING7A_JIS),
148
149 {}
150};
151MODULE_DEVICE_TABLE(usb, bcm5974_table);
152
153MODULE_AUTHOR("Henrik Rydberg");
154MODULE_DESCRIPTION("Apple USB BCM5974 multitouch driver");
155MODULE_LICENSE("GPL");
156
157#define dprintk(level, format, a...)\
158 { if (debug >= level) printk(KERN_DEBUG format, ##a); }
159
160static int debug = 1;
161module_param(debug, int, 0644);
162MODULE_PARM_DESC(debug, "Activate debugging output");
163
164
165struct bt_data {
166 u8 unknown1;
167 u8 button;
168 u8 rel_x;
169 u8 rel_y;
170};
171
172
173enum tp_type {
174 TYPE1,
175 TYPE2
176};
177
178
179#define FINGER_TYPE1 (13 * sizeof(__le16))
180#define FINGER_TYPE2 (15 * sizeof(__le16))
181
182
183#define BUTTON_TYPE2 15
184
185
186#define HAS_INTEGRATED_BUTTON 1
187
188
189struct tp_finger {
190 __le16 origin;
191 __le16 abs_x;
192 __le16 abs_y;
193 __le16 rel_x;
194 __le16 rel_y;
195 __le16 tool_major;
196 __le16 tool_minor;
197 __le16 orientation;
198 __le16 touch_major;
199 __le16 touch_minor;
200 __le16 unused[3];
201 __le16 multi;
202} __attribute__((packed,aligned(2)));
203
204
205#define MAX_FINGERS 16
206#define SIZEOF_FINGER sizeof(struct tp_finger)
207#define SIZEOF_ALL_FINGERS (MAX_FINGERS * SIZEOF_FINGER)
208#define MAX_FINGER_ORIENTATION 16384
209
210
211struct bcm5974_param {
212 int snratio;
213 int min;
214 int max;
215};
216
217
218struct bcm5974_config {
219 int ansi, iso, jis;
220 int caps;
221 int bt_ep;
222 int bt_datalen;
223 int tp_ep;
224 enum tp_type tp_type;
225 int tp_offset;
226 int tp_datalen;
227 struct bcm5974_param p;
228 struct bcm5974_param w;
229 struct bcm5974_param x;
230 struct bcm5974_param y;
231 struct bcm5974_param o;
232};
233
234
235struct bcm5974 {
236 char phys[64];
237 struct usb_device *udev;
238 struct usb_interface *intf;
239 struct input_dev *input;
240 struct bcm5974_config cfg;
241 struct mutex pm_mutex;
242 int opened;
243 struct urb *bt_urb;
244 struct bt_data *bt_data;
245 struct urb *tp_urb;
246 u8 *tp_data;
247 const struct tp_finger *index[MAX_FINGERS];
248 struct input_mt_pos pos[MAX_FINGERS];
249 int slots[MAX_FINGERS];
250};
251
252
253#define SN_PRESSURE 45
254#define SN_WIDTH 25
255#define SN_COORD 250
256#define SN_ORIENT 10
257
258
259static const struct bcm5974_config bcm5974_config_table[] = {
260 {
261 USB_DEVICE_ID_APPLE_WELLSPRING_ANSI,
262 USB_DEVICE_ID_APPLE_WELLSPRING_ISO,
263 USB_DEVICE_ID_APPLE_WELLSPRING_JIS,
264 0,
265 0x84, sizeof(struct bt_data),
266 0x81, TYPE1, FINGER_TYPE1, FINGER_TYPE1 + SIZEOF_ALL_FINGERS,
267 { SN_PRESSURE, 0, 256 },
268 { SN_WIDTH, 0, 2048 },
269 { SN_COORD, -4824, 5342 },
270 { SN_COORD, -172, 5820 },
271 { SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION }
272 },
273 {
274 USB_DEVICE_ID_APPLE_WELLSPRING2_ANSI,
275 USB_DEVICE_ID_APPLE_WELLSPRING2_ISO,
276 USB_DEVICE_ID_APPLE_WELLSPRING2_JIS,
277 0,
278 0x84, sizeof(struct bt_data),
279 0x81, TYPE1, FINGER_TYPE1, FINGER_TYPE1 + SIZEOF_ALL_FINGERS,
280 { SN_PRESSURE, 0, 256 },
281 { SN_WIDTH, 0, 2048 },
282 { SN_COORD, -4824, 4824 },
283 { SN_COORD, -172, 4290 },
284 { SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION }
285 },
286 {
287 USB_DEVICE_ID_APPLE_WELLSPRING3_ANSI,
288 USB_DEVICE_ID_APPLE_WELLSPRING3_ISO,
289 USB_DEVICE_ID_APPLE_WELLSPRING3_JIS,
290 HAS_INTEGRATED_BUTTON,
291 0x84, sizeof(struct bt_data),
292 0x81, TYPE2, FINGER_TYPE2, FINGER_TYPE2 + SIZEOF_ALL_FINGERS,
293 { SN_PRESSURE, 0, 300 },
294 { SN_WIDTH, 0, 2048 },
295 { SN_COORD, -4460, 5166 },
296 { SN_COORD, -75, 6700 },
297 { SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION }
298 },
299 {
300 USB_DEVICE_ID_APPLE_WELLSPRING4_ANSI,
301 USB_DEVICE_ID_APPLE_WELLSPRING4_ISO,
302 USB_DEVICE_ID_APPLE_WELLSPRING4_JIS,
303 HAS_INTEGRATED_BUTTON,
304 0x84, sizeof(struct bt_data),
305 0x81, TYPE2, FINGER_TYPE2, FINGER_TYPE2 + SIZEOF_ALL_FINGERS,
306 { SN_PRESSURE, 0, 300 },
307 { SN_WIDTH, 0, 2048 },
308 { SN_COORD, -4620, 5140 },
309 { SN_COORD, -150, 6600 },
310 { SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION }
311 },
312 {
313 USB_DEVICE_ID_APPLE_WELLSPRING4A_ANSI,
314 USB_DEVICE_ID_APPLE_WELLSPRING4A_ISO,
315 USB_DEVICE_ID_APPLE_WELLSPRING4A_JIS,
316 HAS_INTEGRATED_BUTTON,
317 0x84, sizeof(struct bt_data),
318 0x81, TYPE2, FINGER_TYPE2, FINGER_TYPE2 + SIZEOF_ALL_FINGERS,
319 { SN_PRESSURE, 0, 300 },
320 { SN_WIDTH, 0, 2048 },
321 { SN_COORD, -4616, 5112 },
322 { SN_COORD, -142, 5234 },
323 { SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION }
324 },
325 {
326 USB_DEVICE_ID_APPLE_WELLSPRING5_ANSI,
327 USB_DEVICE_ID_APPLE_WELLSPRING5_ISO,
328 USB_DEVICE_ID_APPLE_WELLSPRING5_JIS,
329 HAS_INTEGRATED_BUTTON,
330 0x84, sizeof(struct bt_data),
331 0x81, TYPE2, FINGER_TYPE2, FINGER_TYPE2 + SIZEOF_ALL_FINGERS,
332 { SN_PRESSURE, 0, 300 },
333 { SN_WIDTH, 0, 2048 },
334 { SN_COORD, -4415, 5050 },
335 { SN_COORD, -55, 6680 },
336 { SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION }
337 },
338 {
339 USB_DEVICE_ID_APPLE_WELLSPRING6_ANSI,
340 USB_DEVICE_ID_APPLE_WELLSPRING6_ISO,
341 USB_DEVICE_ID_APPLE_WELLSPRING6_JIS,
342 HAS_INTEGRATED_BUTTON,
343 0x84, sizeof(struct bt_data),
344 0x81, TYPE2, FINGER_TYPE2, FINGER_TYPE2 + SIZEOF_ALL_FINGERS,
345 { SN_PRESSURE, 0, 300 },
346 { SN_WIDTH, 0, 2048 },
347 { SN_COORD, -4620, 5140 },
348 { SN_COORD, -150, 6600 },
349 { SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION }
350 },
351 {
352 USB_DEVICE_ID_APPLE_WELLSPRING5A_ANSI,
353 USB_DEVICE_ID_APPLE_WELLSPRING5A_ISO,
354 USB_DEVICE_ID_APPLE_WELLSPRING5A_JIS,
355 HAS_INTEGRATED_BUTTON,
356 0x84, sizeof(struct bt_data),
357 0x81, TYPE2, FINGER_TYPE2, FINGER_TYPE2 + SIZEOF_ALL_FINGERS,
358 { SN_PRESSURE, 0, 300 },
359 { SN_WIDTH, 0, 2048 },
360 { SN_COORD, -4750, 5280 },
361 { SN_COORD, -150, 6730 },
362 { SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION }
363 },
364 {
365 USB_DEVICE_ID_APPLE_WELLSPRING6A_ANSI,
366 USB_DEVICE_ID_APPLE_WELLSPRING6A_ISO,
367 USB_DEVICE_ID_APPLE_WELLSPRING6A_JIS,
368 HAS_INTEGRATED_BUTTON,
369 0x84, sizeof(struct bt_data),
370 0x81, TYPE2, FINGER_TYPE2, FINGER_TYPE2 + SIZEOF_ALL_FINGERS,
371 { SN_PRESSURE, 0, 300 },
372 { SN_WIDTH, 0, 2048 },
373 { SN_COORD, -4620, 5140 },
374 { SN_COORD, -150, 6600 },
375 { SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION }
376 },
377 {
378 USB_DEVICE_ID_APPLE_WELLSPRING7_ANSI,
379 USB_DEVICE_ID_APPLE_WELLSPRING7_ISO,
380 USB_DEVICE_ID_APPLE_WELLSPRING7_JIS,
381 HAS_INTEGRATED_BUTTON,
382 0x84, sizeof(struct bt_data),
383 0x81, TYPE2, FINGER_TYPE2, FINGER_TYPE2 + SIZEOF_ALL_FINGERS,
384 { SN_PRESSURE, 0, 300 },
385 { SN_WIDTH, 0, 2048 },
386 { SN_COORD, -4750, 5280 },
387 { SN_COORD, -150, 6730 },
388 { SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION }
389 },
390 {
391 USB_DEVICE_ID_APPLE_WELLSPRING7A_ANSI,
392 USB_DEVICE_ID_APPLE_WELLSPRING7A_ISO,
393 USB_DEVICE_ID_APPLE_WELLSPRING7A_JIS,
394 HAS_INTEGRATED_BUTTON,
395 0x84, sizeof(struct bt_data),
396 0x81, TYPE2, FINGER_TYPE2, FINGER_TYPE2 + SIZEOF_ALL_FINGERS,
397 { SN_PRESSURE, 0, 300 },
398 { SN_WIDTH, 0, 2048 },
399 { SN_COORD, -4750, 5280 },
400 { SN_COORD, -150, 6730 },
401 { SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION }
402 },
403 {}
404};
405
406
407static const struct bcm5974_config *bcm5974_get_config(struct usb_device *udev)
408{
409 u16 id = le16_to_cpu(udev->descriptor.idProduct);
410 const struct bcm5974_config *cfg;
411
412 for (cfg = bcm5974_config_table; cfg->ansi; ++cfg)
413 if (cfg->ansi == id || cfg->iso == id || cfg->jis == id)
414 return cfg;
415
416 return bcm5974_config_table;
417}
418
419
420static inline int raw2int(__le16 x)
421{
422 return (signed short)le16_to_cpu(x);
423}
424
425static void set_abs(struct input_dev *input, unsigned int code,
426 const struct bcm5974_param *p)
427{
428 int fuzz = p->snratio ? (p->max - p->min) / p->snratio : 0;
429 input_set_abs_params(input, code, p->min, p->max, fuzz, 0);
430}
431
432
433static void setup_events_to_report(struct input_dev *input_dev,
434 const struct bcm5974_config *cfg)
435{
436 __set_bit(EV_ABS, input_dev->evbit);
437
438
439 input_set_abs_params(input_dev, ABS_PRESSURE, 0, 256, 5, 0);
440 input_set_abs_params(input_dev, ABS_TOOL_WIDTH, 0, 16, 0, 0);
441
442
443 set_abs(input_dev, ABS_MT_TOUCH_MAJOR, &cfg->w);
444 set_abs(input_dev, ABS_MT_TOUCH_MINOR, &cfg->w);
445
446 set_abs(input_dev, ABS_MT_WIDTH_MAJOR, &cfg->w);
447 set_abs(input_dev, ABS_MT_WIDTH_MINOR, &cfg->w);
448
449 set_abs(input_dev, ABS_MT_ORIENTATION, &cfg->o);
450
451 set_abs(input_dev, ABS_MT_POSITION_X, &cfg->x);
452 set_abs(input_dev, ABS_MT_POSITION_Y, &cfg->y);
453
454 __set_bit(EV_KEY, input_dev->evbit);
455 __set_bit(BTN_LEFT, input_dev->keybit);
456
457 if (cfg->caps & HAS_INTEGRATED_BUTTON)
458 __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
459
460 input_mt_init_slots(input_dev, MAX_FINGERS,
461 INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED | INPUT_MT_TRACK);
462}
463
464
465static int report_bt_state(struct bcm5974 *dev, int size)
466{
467 if (size != sizeof(struct bt_data))
468 return -EIO;
469
470 dprintk(7,
471 "bcm5974: button data: %x %x %x %x\n",
472 dev->bt_data->unknown1, dev->bt_data->button,
473 dev->bt_data->rel_x, dev->bt_data->rel_y);
474
475 input_report_key(dev->input, BTN_LEFT, dev->bt_data->button);
476 input_sync(dev->input);
477
478 return 0;
479}
480
481static void report_finger_data(struct input_dev *input, int slot,
482 const struct input_mt_pos *pos,
483 const struct tp_finger *f)
484{
485 input_mt_slot(input, slot);
486 input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
487
488 input_report_abs(input, ABS_MT_TOUCH_MAJOR,
489 raw2int(f->touch_major) << 1);
490 input_report_abs(input, ABS_MT_TOUCH_MINOR,
491 raw2int(f->touch_minor) << 1);
492 input_report_abs(input, ABS_MT_WIDTH_MAJOR,
493 raw2int(f->tool_major) << 1);
494 input_report_abs(input, ABS_MT_WIDTH_MINOR,
495 raw2int(f->tool_minor) << 1);
496 input_report_abs(input, ABS_MT_ORIENTATION,
497 MAX_FINGER_ORIENTATION - raw2int(f->orientation));
498 input_report_abs(input, ABS_MT_POSITION_X, pos->x);
499 input_report_abs(input, ABS_MT_POSITION_Y, pos->y);
500}
501
502static void report_synaptics_data(struct input_dev *input,
503 const struct bcm5974_config *cfg,
504 const struct tp_finger *f, int raw_n)
505{
506 int abs_p = 0, abs_w = 0;
507
508 if (raw_n) {
509 int p = raw2int(f->touch_major);
510 int w = raw2int(f->tool_major);
511 if (p > 0 && raw2int(f->origin)) {
512 abs_p = clamp_val(256 * p / cfg->p.max, 0, 255);
513 abs_w = clamp_val(16 * w / cfg->w.max, 0, 15);
514 }
515 }
516
517 input_report_abs(input, ABS_PRESSURE, abs_p);
518 input_report_abs(input, ABS_TOOL_WIDTH, abs_w);
519}
520
521
522static int report_tp_state(struct bcm5974 *dev, int size)
523{
524 const struct bcm5974_config *c = &dev->cfg;
525 const struct tp_finger *f;
526 struct input_dev *input = dev->input;
527 int raw_n, i, n = 0;
528
529 if (size < c->tp_offset || (size - c->tp_offset) % SIZEOF_FINGER != 0)
530 return -EIO;
531
532
533 f = (const struct tp_finger *)(dev->tp_data + c->tp_offset);
534 raw_n = (size - c->tp_offset) / SIZEOF_FINGER;
535
536 for (i = 0; i < raw_n; i++) {
537 if (raw2int(f[i].touch_major) == 0)
538 continue;
539 dev->pos[n].x = raw2int(f[i].abs_x);
540 dev->pos[n].y = c->y.min + c->y.max - raw2int(f[i].abs_y);
541 dev->index[n++] = &f[i];
542 }
543
544 input_mt_assign_slots(input, dev->slots, dev->pos, n);
545
546 for (i = 0; i < n; i++)
547 report_finger_data(input, dev->slots[i],
548 &dev->pos[i], dev->index[i]);
549
550 input_mt_sync_frame(input);
551
552 report_synaptics_data(input, c, f, raw_n);
553
554
555 if (c->tp_type == TYPE2) {
556 int ibt = raw2int(dev->tp_data[BUTTON_TYPE2]);
557 input_report_key(input, BTN_LEFT, ibt);
558 }
559
560 input_sync(input);
561
562 return 0;
563}
564
565
566#define BCM5974_WELLSPRING_MODE_READ_REQUEST_ID 1
567#define BCM5974_WELLSPRING_MODE_WRITE_REQUEST_ID 9
568#define BCM5974_WELLSPRING_MODE_REQUEST_VALUE 0x300
569#define BCM5974_WELLSPRING_MODE_REQUEST_INDEX 0
570#define BCM5974_WELLSPRING_MODE_VENDOR_VALUE 0x01
571#define BCM5974_WELLSPRING_MODE_NORMAL_VALUE 0x08
572
573static int bcm5974_wellspring_mode(struct bcm5974 *dev, bool on)
574{
575 char *data = kmalloc(8, GFP_KERNEL);
576 int retval = 0, size;
577
578 if (!data) {
579 dev_err(&dev->intf->dev, "out of memory\n");
580 retval = -ENOMEM;
581 goto out;
582 }
583
584
585 size = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
586 BCM5974_WELLSPRING_MODE_READ_REQUEST_ID,
587 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
588 BCM5974_WELLSPRING_MODE_REQUEST_VALUE,
589 BCM5974_WELLSPRING_MODE_REQUEST_INDEX, data, 8, 5000);
590
591 if (size != 8) {
592 dev_err(&dev->intf->dev, "could not read from device\n");
593 retval = -EIO;
594 goto out;
595 }
596
597
598 data[0] = on ?
599 BCM5974_WELLSPRING_MODE_VENDOR_VALUE :
600 BCM5974_WELLSPRING_MODE_NORMAL_VALUE;
601
602
603 size = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
604 BCM5974_WELLSPRING_MODE_WRITE_REQUEST_ID,
605 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
606 BCM5974_WELLSPRING_MODE_REQUEST_VALUE,
607 BCM5974_WELLSPRING_MODE_REQUEST_INDEX, data, 8, 5000);
608
609 if (size != 8) {
610 dev_err(&dev->intf->dev, "could not write to device\n");
611 retval = -EIO;
612 goto out;
613 }
614
615 dprintk(2, "bcm5974: switched to %s mode.\n",
616 on ? "wellspring" : "normal");
617
618 out:
619 kfree(data);
620 return retval;
621}
622
623static void bcm5974_irq_button(struct urb *urb)
624{
625 struct bcm5974 *dev = urb->context;
626 struct usb_interface *intf = dev->intf;
627 int error;
628
629 switch (urb->status) {
630 case 0:
631 break;
632 case -EOVERFLOW:
633 case -ECONNRESET:
634 case -ENOENT:
635 case -ESHUTDOWN:
636 dev_dbg(&intf->dev, "button urb shutting down: %d\n",
637 urb->status);
638 return;
639 default:
640 dev_dbg(&intf->dev, "button urb status: %d\n", urb->status);
641 goto exit;
642 }
643
644 if (report_bt_state(dev, dev->bt_urb->actual_length))
645 dprintk(1, "bcm5974: bad button package, length: %d\n",
646 dev->bt_urb->actual_length);
647
648exit:
649 error = usb_submit_urb(dev->bt_urb, GFP_ATOMIC);
650 if (error)
651 dev_err(&intf->dev, "button urb failed: %d\n", error);
652}
653
654static void bcm5974_irq_trackpad(struct urb *urb)
655{
656 struct bcm5974 *dev = urb->context;
657 struct usb_interface *intf = dev->intf;
658 int error;
659
660 switch (urb->status) {
661 case 0:
662 break;
663 case -EOVERFLOW:
664 case -ECONNRESET:
665 case -ENOENT:
666 case -ESHUTDOWN:
667 dev_dbg(&intf->dev, "trackpad urb shutting down: %d\n",
668 urb->status);
669 return;
670 default:
671 dev_dbg(&intf->dev, "trackpad urb status: %d\n", urb->status);
672 goto exit;
673 }
674
675
676 if (dev->tp_urb->actual_length == 2)
677 goto exit;
678
679 if (report_tp_state(dev, dev->tp_urb->actual_length))
680 dprintk(1, "bcm5974: bad trackpad package, length: %d\n",
681 dev->tp_urb->actual_length);
682
683exit:
684 error = usb_submit_urb(dev->tp_urb, GFP_ATOMIC);
685 if (error)
686 dev_err(&intf->dev, "trackpad urb failed: %d\n", error);
687}
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707static int bcm5974_start_traffic(struct bcm5974 *dev)
708{
709 int error;
710
711 error = bcm5974_wellspring_mode(dev, true);
712 if (error) {
713 dprintk(1, "bcm5974: mode switch failed\n");
714 goto err_out;
715 }
716
717 if (dev->bt_urb) {
718 error = usb_submit_urb(dev->bt_urb, GFP_KERNEL);
719 if (error)
720 goto err_reset_mode;
721 }
722
723 error = usb_submit_urb(dev->tp_urb, GFP_KERNEL);
724 if (error)
725 goto err_kill_bt;
726
727 return 0;
728
729err_kill_bt:
730 usb_kill_urb(dev->bt_urb);
731err_reset_mode:
732 bcm5974_wellspring_mode(dev, false);
733err_out:
734 return error;
735}
736
737static void bcm5974_pause_traffic(struct bcm5974 *dev)
738{
739 usb_kill_urb(dev->tp_urb);
740 usb_kill_urb(dev->bt_urb);
741 bcm5974_wellspring_mode(dev, false);
742}
743
744
745
746
747
748
749
750
751
752static int bcm5974_open(struct input_dev *input)
753{
754 struct bcm5974 *dev = input_get_drvdata(input);
755 int error;
756
757 error = usb_autopm_get_interface(dev->intf);
758 if (error)
759 return error;
760
761 mutex_lock(&dev->pm_mutex);
762
763 error = bcm5974_start_traffic(dev);
764 if (!error)
765 dev->opened = 1;
766
767 mutex_unlock(&dev->pm_mutex);
768
769 if (error)
770 usb_autopm_put_interface(dev->intf);
771
772 return error;
773}
774
775static void bcm5974_close(struct input_dev *input)
776{
777 struct bcm5974 *dev = input_get_drvdata(input);
778
779 mutex_lock(&dev->pm_mutex);
780
781 bcm5974_pause_traffic(dev);
782 dev->opened = 0;
783
784 mutex_unlock(&dev->pm_mutex);
785
786 usb_autopm_put_interface(dev->intf);
787}
788
789static int bcm5974_suspend(struct usb_interface *iface, pm_message_t message)
790{
791 struct bcm5974 *dev = usb_get_intfdata(iface);
792
793 mutex_lock(&dev->pm_mutex);
794
795 if (dev->opened)
796 bcm5974_pause_traffic(dev);
797
798 mutex_unlock(&dev->pm_mutex);
799
800 return 0;
801}
802
803static int bcm5974_resume(struct usb_interface *iface)
804{
805 struct bcm5974 *dev = usb_get_intfdata(iface);
806 int error = 0;
807
808 mutex_lock(&dev->pm_mutex);
809
810 if (dev->opened)
811 error = bcm5974_start_traffic(dev);
812
813 mutex_unlock(&dev->pm_mutex);
814
815 return error;
816}
817
818static int bcm5974_probe(struct usb_interface *iface,
819 const struct usb_device_id *id)
820{
821 struct usb_device *udev = interface_to_usbdev(iface);
822 const struct bcm5974_config *cfg;
823 struct bcm5974 *dev;
824 struct input_dev *input_dev;
825 int error = -ENOMEM;
826
827
828 cfg = bcm5974_get_config(udev);
829
830
831 dev = kzalloc(sizeof(struct bcm5974), GFP_KERNEL);
832 input_dev = input_allocate_device();
833 if (!dev || !input_dev) {
834 dev_err(&iface->dev, "out of memory\n");
835 goto err_free_devs;
836 }
837
838 dev->udev = udev;
839 dev->intf = iface;
840 dev->input = input_dev;
841 dev->cfg = *cfg;
842 mutex_init(&dev->pm_mutex);
843
844
845 if (cfg->tp_type == TYPE1) {
846 dev->bt_urb = usb_alloc_urb(0, GFP_KERNEL);
847 if (!dev->bt_urb)
848 goto err_free_devs;
849 }
850
851 dev->tp_urb = usb_alloc_urb(0, GFP_KERNEL);
852 if (!dev->tp_urb)
853 goto err_free_bt_urb;
854
855 if (dev->bt_urb) {
856 dev->bt_data = usb_alloc_coherent(dev->udev,
857 dev->cfg.bt_datalen, GFP_KERNEL,
858 &dev->bt_urb->transfer_dma);
859 if (!dev->bt_data)
860 goto err_free_urb;
861 }
862
863 dev->tp_data = usb_alloc_coherent(dev->udev,
864 dev->cfg.tp_datalen, GFP_KERNEL,
865 &dev->tp_urb->transfer_dma);
866 if (!dev->tp_data)
867 goto err_free_bt_buffer;
868
869 if (dev->bt_urb)
870 usb_fill_int_urb(dev->bt_urb, udev,
871 usb_rcvintpipe(udev, cfg->bt_ep),
872 dev->bt_data, dev->cfg.bt_datalen,
873 bcm5974_irq_button, dev, 1);
874
875 usb_fill_int_urb(dev->tp_urb, udev,
876 usb_rcvintpipe(udev, cfg->tp_ep),
877 dev->tp_data, dev->cfg.tp_datalen,
878 bcm5974_irq_trackpad, dev, 1);
879
880
881 usb_make_path(udev, dev->phys, sizeof(dev->phys));
882 strlcat(dev->phys, "/input0", sizeof(dev->phys));
883
884 input_dev->name = "bcm5974";
885 input_dev->phys = dev->phys;
886 usb_to_input_id(dev->udev, &input_dev->id);
887
888 input_dev->id.version = cfg->caps;
889 input_dev->dev.parent = &iface->dev;
890
891 input_set_drvdata(input_dev, dev);
892
893 input_dev->open = bcm5974_open;
894 input_dev->close = bcm5974_close;
895
896 setup_events_to_report(input_dev, cfg);
897
898 error = input_register_device(dev->input);
899 if (error)
900 goto err_free_buffer;
901
902
903 usb_set_intfdata(iface, dev);
904
905 return 0;
906
907err_free_buffer:
908 usb_free_coherent(dev->udev, dev->cfg.tp_datalen,
909 dev->tp_data, dev->tp_urb->transfer_dma);
910err_free_bt_buffer:
911 if (dev->bt_urb)
912 usb_free_coherent(dev->udev, dev->cfg.bt_datalen,
913 dev->bt_data, dev->bt_urb->transfer_dma);
914err_free_urb:
915 usb_free_urb(dev->tp_urb);
916err_free_bt_urb:
917 usb_free_urb(dev->bt_urb);
918err_free_devs:
919 usb_set_intfdata(iface, NULL);
920 input_free_device(input_dev);
921 kfree(dev);
922 return error;
923}
924
925static void bcm5974_disconnect(struct usb_interface *iface)
926{
927 struct bcm5974 *dev = usb_get_intfdata(iface);
928
929 usb_set_intfdata(iface, NULL);
930
931 input_unregister_device(dev->input);
932 usb_free_coherent(dev->udev, dev->cfg.tp_datalen,
933 dev->tp_data, dev->tp_urb->transfer_dma);
934 if (dev->bt_urb)
935 usb_free_coherent(dev->udev, dev->cfg.bt_datalen,
936 dev->bt_data, dev->bt_urb->transfer_dma);
937 usb_free_urb(dev->tp_urb);
938 usb_free_urb(dev->bt_urb);
939 kfree(dev);
940}
941
942static struct usb_driver bcm5974_driver = {
943 .name = "bcm5974",
944 .probe = bcm5974_probe,
945 .disconnect = bcm5974_disconnect,
946 .suspend = bcm5974_suspend,
947 .resume = bcm5974_resume,
948 .id_table = bcm5974_table,
949 .supports_autosuspend = 1,
950};
951
952module_usb_driver(bcm5974_driver);
953