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