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
44#define USB_VENDOR_ID_APPLE 0x05ac
45
46
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
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
55#define BCM5974_DEVICE(prod) { \
56 .match_flags = (USB_DEVICE_ID_MATCH_DEVICE | \
57 USB_DEVICE_ID_MATCH_INT_CLASS | \
58 USB_DEVICE_ID_MATCH_INT_PROTOCOL), \
59 .idVendor = USB_VENDOR_ID_APPLE, \
60 .idProduct = (prod), \
61 .bInterfaceClass = USB_INTERFACE_CLASS_HID, \
62 .bInterfaceProtocol = USB_INTERFACE_PROTOCOL_MOUSE \
63}
64
65
66static const struct usb_device_id bcm5974_table[] = {
67
68 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING_ANSI),
69 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING_ISO),
70 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING_JIS),
71
72 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING2_ANSI),
73 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING2_ISO),
74 BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING2_JIS),
75
76 {}
77};
78MODULE_DEVICE_TABLE(usb, bcm5974_table);
79
80MODULE_AUTHOR("Henrik Rydberg");
81MODULE_DESCRIPTION("Apple USB BCM5974 multitouch driver");
82MODULE_LICENSE("GPL");
83
84#define dprintk(level, format, a...)\
85 { if (debug >= level) printk(KERN_DEBUG format, ##a); }
86
87static int debug = 1;
88module_param(debug, int, 0644);
89MODULE_PARM_DESC(debug, "Activate debugging output");
90
91
92struct bt_data {
93 u8 unknown1;
94 u8 button;
95 u8 rel_x;
96 u8 rel_y;
97};
98
99
100struct tp_header {
101 u8 unknown1[16];
102 u8 fingers;
103 u8 unknown2[9];
104};
105
106
107struct tp_finger {
108 __le16 origin;
109 __le16 abs_x;
110 __le16 abs_y;
111 __le16 rel_x;
112 __le16 rel_y;
113 __le16 size_major;
114 __le16 size_minor;
115 __le16 orientation;
116 __le16 force_major;
117 __le16 force_minor;
118 __le16 unused[3];
119 __le16 multi;
120};
121
122
123struct tp_data {
124 struct tp_header header;
125 struct tp_finger finger[16];
126};
127
128
129struct bcm5974_param {
130 int dim;
131 int fuzz;
132 int devmin;
133 int devmax;
134};
135
136
137struct bcm5974_config {
138 int ansi, iso, jis;
139 int bt_ep;
140 int bt_datalen;
141 int tp_ep;
142 int tp_datalen;
143 struct bcm5974_param p;
144 struct bcm5974_param w;
145 struct bcm5974_param x;
146 struct bcm5974_param y;
147};
148
149
150struct bcm5974 {
151 char phys[64];
152 struct usb_device *udev;
153 struct usb_interface *intf;
154 struct input_dev *input;
155 struct bcm5974_config cfg;
156 struct mutex pm_mutex;
157 int opened;
158 struct urb *bt_urb;
159 struct bt_data *bt_data;
160 struct urb *tp_urb;
161 struct tp_data *tp_data;
162 int fingers;
163};
164
165
166#define DIM_PRESSURE 256
167#define DIM_WIDTH 16
168#define DIM_X 1280
169#define DIM_Y 800
170
171
172#define SN_PRESSURE 45
173#define SN_WIDTH 100
174#define SN_COORD 250
175
176
177#define PRESSURE_LOW (2 * DIM_PRESSURE / SN_PRESSURE)
178#define PRESSURE_HIGH (3 * PRESSURE_LOW)
179
180
181static const struct bcm5974_config bcm5974_config_table[] = {
182 {
183 USB_DEVICE_ID_APPLE_WELLSPRING_ANSI,
184 USB_DEVICE_ID_APPLE_WELLSPRING_ISO,
185 USB_DEVICE_ID_APPLE_WELLSPRING_JIS,
186 0x84, sizeof(struct bt_data),
187 0x81, sizeof(struct tp_data),
188 { DIM_PRESSURE, DIM_PRESSURE / SN_PRESSURE, 0, 256 },
189 { DIM_WIDTH, DIM_WIDTH / SN_WIDTH, 0, 2048 },
190 { DIM_X, DIM_X / SN_COORD, -4824, 5342 },
191 { DIM_Y, DIM_Y / SN_COORD, -172, 5820 }
192 },
193 {
194 USB_DEVICE_ID_APPLE_WELLSPRING2_ANSI,
195 USB_DEVICE_ID_APPLE_WELLSPRING2_ISO,
196 USB_DEVICE_ID_APPLE_WELLSPRING2_JIS,
197 0x84, sizeof(struct bt_data),
198 0x81, sizeof(struct tp_data),
199 { DIM_PRESSURE, DIM_PRESSURE / SN_PRESSURE, 0, 256 },
200 { DIM_WIDTH, DIM_WIDTH / SN_WIDTH, 0, 2048 },
201 { DIM_X, DIM_X / SN_COORD, -4824, 4824 },
202 { DIM_Y, DIM_Y / SN_COORD, -172, 4290 }
203 },
204 {}
205};
206
207
208static const struct bcm5974_config *bcm5974_get_config(struct usb_device *udev)
209{
210 u16 id = le16_to_cpu(udev->descriptor.idProduct);
211 const struct bcm5974_config *cfg;
212
213 for (cfg = bcm5974_config_table; cfg->ansi; ++cfg)
214 if (cfg->ansi == id || cfg->iso == id || cfg->jis == id)
215 return cfg;
216
217 return bcm5974_config_table;
218}
219
220
221static inline int raw2int(__le16 x)
222{
223 return (signed short)le16_to_cpu(x);
224}
225
226
227static inline int int2scale(const struct bcm5974_param *p, int x)
228{
229 return x * p->dim / (p->devmax - p->devmin);
230}
231
232
233static inline int int2bound(const struct bcm5974_param *p, int x)
234{
235 int s = int2scale(p, x);
236
237 return clamp_val(s, 0, p->dim - 1);
238}
239
240
241static void setup_events_to_report(struct input_dev *input_dev,
242 const struct bcm5974_config *cfg)
243{
244 __set_bit(EV_ABS, input_dev->evbit);
245
246 input_set_abs_params(input_dev, ABS_PRESSURE,
247 0, cfg->p.dim, cfg->p.fuzz, 0);
248 input_set_abs_params(input_dev, ABS_TOOL_WIDTH,
249 0, cfg->w.dim, cfg->w.fuzz, 0);
250 input_set_abs_params(input_dev, ABS_X,
251 0, cfg->x.dim, cfg->x.fuzz, 0);
252 input_set_abs_params(input_dev, ABS_Y,
253 0, cfg->y.dim, cfg->y.fuzz, 0);
254
255 __set_bit(EV_KEY, input_dev->evbit);
256 __set_bit(BTN_TOUCH, input_dev->keybit);
257 __set_bit(BTN_TOOL_FINGER, input_dev->keybit);
258 __set_bit(BTN_TOOL_DOUBLETAP, input_dev->keybit);
259 __set_bit(BTN_TOOL_TRIPLETAP, input_dev->keybit);
260 __set_bit(BTN_LEFT, input_dev->keybit);
261}
262
263
264static int report_bt_state(struct bcm5974 *dev, int size)
265{
266 if (size != sizeof(struct bt_data))
267 return -EIO;
268
269 input_report_key(dev->input, BTN_LEFT, dev->bt_data->button);
270 input_sync(dev->input);
271
272 return 0;
273}
274
275
276static int report_tp_state(struct bcm5974 *dev, int size)
277{
278 const struct bcm5974_config *c = &dev->cfg;
279 const struct tp_finger *f = dev->tp_data->finger;
280 struct input_dev *input = dev->input;
281 const int fingers = (size - 26) / 28;
282 int raw_p, raw_w, raw_x, raw_y;
283 int ptest = 0, origin = 0, nmin = 0, nmax = 0;
284 int abs_p = 0, abs_w = 0, abs_x = 0, abs_y = 0;
285
286 if (size < 26 || (size - 26) % 28 != 0)
287 return -EIO;
288
289
290 if (fingers) {
291 raw_p = raw2int(f->force_major);
292 raw_w = raw2int(f->size_major);
293 raw_x = raw2int(f->abs_x);
294 raw_y = raw2int(f->abs_y);
295
296 dprintk(9,
297 "bcm5974: raw: p: %+05d w: %+05d x: %+05d y: %+05d\n",
298 raw_p, raw_w, raw_x, raw_y);
299
300 ptest = int2bound(&c->p, raw_p);
301 origin = raw2int(f->origin);
302 }
303
304
305 if (ptest > PRESSURE_LOW && origin) {
306 abs_p = ptest;
307 abs_w = int2bound(&c->w, raw_w);
308 abs_x = int2bound(&c->x, raw_x - c->x.devmin);
309 abs_y = int2bound(&c->y, c->y.devmax - raw_y);
310 for (; f != dev->tp_data->finger + fingers; f++) {
311 ptest = int2bound(&c->p, raw2int(f->force_major));
312 if (ptest > PRESSURE_LOW)
313 nmax++;
314 if (ptest > PRESSURE_HIGH)
315 nmin++;
316 }
317 }
318
319 if (dev->fingers < nmin)
320 dev->fingers = nmin;
321 if (dev->fingers > nmax)
322 dev->fingers = nmax;
323
324 input_report_key(input, BTN_TOUCH, dev->fingers > 0);
325 input_report_key(input, BTN_TOOL_FINGER, dev->fingers == 1);
326 input_report_key(input, BTN_TOOL_DOUBLETAP, dev->fingers == 2);
327 input_report_key(input, BTN_TOOL_TRIPLETAP, dev->fingers > 2);
328
329 input_report_abs(input, ABS_PRESSURE, abs_p);
330 input_report_abs(input, ABS_TOOL_WIDTH, abs_w);
331
332 if (abs_p) {
333 input_report_abs(input, ABS_X, abs_x);
334 input_report_abs(input, ABS_Y, abs_y);
335
336 dprintk(8,
337 "bcm5974: abs: p: %+05d w: %+05d x: %+05d y: %+05d "
338 "nmin: %d nmax: %d n: %d\n",
339 abs_p, abs_w, abs_x, abs_y, nmin, nmax, dev->fingers);
340
341 }
342
343 input_sync(input);
344
345 return 0;
346}
347
348
349#define BCM5974_WELLSPRING_MODE_READ_REQUEST_ID 1
350#define BCM5974_WELLSPRING_MODE_WRITE_REQUEST_ID 9
351#define BCM5974_WELLSPRING_MODE_REQUEST_VALUE 0x300
352#define BCM5974_WELLSPRING_MODE_REQUEST_INDEX 0
353#define BCM5974_WELLSPRING_MODE_VENDOR_VALUE 0x01
354#define BCM5974_WELLSPRING_MODE_NORMAL_VALUE 0x08
355
356static int bcm5974_wellspring_mode(struct bcm5974 *dev, bool on)
357{
358 char *data = kmalloc(8, GFP_KERNEL);
359 int retval = 0, size;
360
361 if (!data) {
362 err("bcm5974: out of memory");
363 retval = -ENOMEM;
364 goto out;
365 }
366
367
368 size = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
369 BCM5974_WELLSPRING_MODE_READ_REQUEST_ID,
370 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
371 BCM5974_WELLSPRING_MODE_REQUEST_VALUE,
372 BCM5974_WELLSPRING_MODE_REQUEST_INDEX, data, 8, 5000);
373
374 if (size != 8) {
375 err("bcm5974: could not read from device");
376 retval = -EIO;
377 goto out;
378 }
379
380
381 data[0] = on ?
382 BCM5974_WELLSPRING_MODE_VENDOR_VALUE :
383 BCM5974_WELLSPRING_MODE_NORMAL_VALUE;
384
385
386 size = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
387 BCM5974_WELLSPRING_MODE_WRITE_REQUEST_ID,
388 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
389 BCM5974_WELLSPRING_MODE_REQUEST_VALUE,
390 BCM5974_WELLSPRING_MODE_REQUEST_INDEX, data, 8, 5000);
391
392 if (size != 8) {
393 err("bcm5974: could not write to device");
394 retval = -EIO;
395 goto out;
396 }
397
398 dprintk(2, "bcm5974: switched to %s mode.\n",
399 on ? "wellspring" : "normal");
400
401 out:
402 kfree(data);
403 return retval;
404}
405
406static void bcm5974_irq_button(struct urb *urb)
407{
408 struct bcm5974 *dev = urb->context;
409 int error;
410
411 switch (urb->status) {
412 case 0:
413 break;
414 case -EOVERFLOW:
415 case -ECONNRESET:
416 case -ENOENT:
417 case -ESHUTDOWN:
418 dbg("bcm5974: button urb shutting down: %d", urb->status);
419 return;
420 default:
421 dbg("bcm5974: button urb status: %d", urb->status);
422 goto exit;
423 }
424
425 if (report_bt_state(dev, dev->bt_urb->actual_length))
426 dprintk(1, "bcm5974: bad button package, length: %d\n",
427 dev->bt_urb->actual_length);
428
429exit:
430 error = usb_submit_urb(dev->bt_urb, GFP_ATOMIC);
431 if (error)
432 err("bcm5974: button urb failed: %d", error);
433}
434
435static void bcm5974_irq_trackpad(struct urb *urb)
436{
437 struct bcm5974 *dev = urb->context;
438 int error;
439
440 switch (urb->status) {
441 case 0:
442 break;
443 case -EOVERFLOW:
444 case -ECONNRESET:
445 case -ENOENT:
446 case -ESHUTDOWN:
447 dbg("bcm5974: trackpad urb shutting down: %d", urb->status);
448 return;
449 default:
450 dbg("bcm5974: trackpad urb status: %d", urb->status);
451 goto exit;
452 }
453
454
455 if (dev->tp_urb->actual_length == 2)
456 goto exit;
457
458 if (report_tp_state(dev, dev->tp_urb->actual_length))
459 dprintk(1, "bcm5974: bad trackpad package, length: %d\n",
460 dev->tp_urb->actual_length);
461
462exit:
463 error = usb_submit_urb(dev->tp_urb, GFP_ATOMIC);
464 if (error)
465 err("bcm5974: trackpad urb failed: %d", error);
466}
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486static int bcm5974_start_traffic(struct bcm5974 *dev)
487{
488 if (bcm5974_wellspring_mode(dev, true)) {
489 dprintk(1, "bcm5974: mode switch failed\n");
490 goto error;
491 }
492
493 if (usb_submit_urb(dev->bt_urb, GFP_KERNEL))
494 goto error;
495
496 if (usb_submit_urb(dev->tp_urb, GFP_KERNEL))
497 goto err_kill_bt;
498
499 return 0;
500
501err_kill_bt:
502 usb_kill_urb(dev->bt_urb);
503error:
504 return -EIO;
505}
506
507static void bcm5974_pause_traffic(struct bcm5974 *dev)
508{
509 usb_kill_urb(dev->tp_urb);
510 usb_kill_urb(dev->bt_urb);
511 bcm5974_wellspring_mode(dev, false);
512}
513
514
515
516
517
518
519
520
521
522static int bcm5974_open(struct input_dev *input)
523{
524 struct bcm5974 *dev = input_get_drvdata(input);
525 int error;
526
527 error = usb_autopm_get_interface(dev->intf);
528 if (error)
529 return error;
530
531 mutex_lock(&dev->pm_mutex);
532
533 error = bcm5974_start_traffic(dev);
534 if (!error)
535 dev->opened = 1;
536
537 mutex_unlock(&dev->pm_mutex);
538
539 if (error)
540 usb_autopm_put_interface(dev->intf);
541
542 return error;
543}
544
545static void bcm5974_close(struct input_dev *input)
546{
547 struct bcm5974 *dev = input_get_drvdata(input);
548
549 mutex_lock(&dev->pm_mutex);
550
551 bcm5974_pause_traffic(dev);
552 dev->opened = 0;
553
554 mutex_unlock(&dev->pm_mutex);
555
556 usb_autopm_put_interface(dev->intf);
557}
558
559static int bcm5974_suspend(struct usb_interface *iface, pm_message_t message)
560{
561 struct bcm5974 *dev = usb_get_intfdata(iface);
562
563 mutex_lock(&dev->pm_mutex);
564
565 if (dev->opened)
566 bcm5974_pause_traffic(dev);
567
568 mutex_unlock(&dev->pm_mutex);
569
570 return 0;
571}
572
573static int bcm5974_resume(struct usb_interface *iface)
574{
575 struct bcm5974 *dev = usb_get_intfdata(iface);
576 int error = 0;
577
578 mutex_lock(&dev->pm_mutex);
579
580 if (dev->opened)
581 error = bcm5974_start_traffic(dev);
582
583 mutex_unlock(&dev->pm_mutex);
584
585 return error;
586}
587
588static int bcm5974_probe(struct usb_interface *iface,
589 const struct usb_device_id *id)
590{
591 struct usb_device *udev = interface_to_usbdev(iface);
592 const struct bcm5974_config *cfg;
593 struct bcm5974 *dev;
594 struct input_dev *input_dev;
595 int error = -ENOMEM;
596
597
598 cfg = bcm5974_get_config(udev);
599
600
601 dev = kzalloc(sizeof(struct bcm5974), GFP_KERNEL);
602 input_dev = input_allocate_device();
603 if (!dev || !input_dev) {
604 err("bcm5974: out of memory");
605 goto err_free_devs;
606 }
607
608 dev->udev = udev;
609 dev->intf = iface;
610 dev->input = input_dev;
611 dev->cfg = *cfg;
612 mutex_init(&dev->pm_mutex);
613
614
615 dev->bt_urb = usb_alloc_urb(0, GFP_KERNEL);
616 if (!dev->bt_urb)
617 goto err_free_devs;
618
619 dev->tp_urb = usb_alloc_urb(0, GFP_KERNEL);
620 if (!dev->tp_urb)
621 goto err_free_bt_urb;
622
623 dev->bt_data = usb_buffer_alloc(dev->udev,
624 dev->cfg.bt_datalen, GFP_KERNEL,
625 &dev->bt_urb->transfer_dma);
626 if (!dev->bt_data)
627 goto err_free_urb;
628
629 dev->tp_data = usb_buffer_alloc(dev->udev,
630 dev->cfg.tp_datalen, GFP_KERNEL,
631 &dev->tp_urb->transfer_dma);
632 if (!dev->tp_data)
633 goto err_free_bt_buffer;
634
635 usb_fill_int_urb(dev->bt_urb, udev,
636 usb_rcvintpipe(udev, cfg->bt_ep),
637 dev->bt_data, dev->cfg.bt_datalen,
638 bcm5974_irq_button, dev, 1);
639
640 usb_fill_int_urb(dev->tp_urb, udev,
641 usb_rcvintpipe(udev, cfg->tp_ep),
642 dev->tp_data, dev->cfg.tp_datalen,
643 bcm5974_irq_trackpad, dev, 1);
644
645
646 usb_make_path(udev, dev->phys, sizeof(dev->phys));
647 strlcat(dev->phys, "/input0", sizeof(dev->phys));
648
649 input_dev->name = "bcm5974";
650 input_dev->phys = dev->phys;
651 usb_to_input_id(dev->udev, &input_dev->id);
652 input_dev->dev.parent = &iface->dev;
653
654 input_set_drvdata(input_dev, dev);
655
656 input_dev->open = bcm5974_open;
657 input_dev->close = bcm5974_close;
658
659 setup_events_to_report(input_dev, cfg);
660
661 error = input_register_device(dev->input);
662 if (error)
663 goto err_free_buffer;
664
665
666 usb_set_intfdata(iface, dev);
667
668 return 0;
669
670err_free_buffer:
671 usb_buffer_free(dev->udev, dev->cfg.tp_datalen,
672 dev->tp_data, dev->tp_urb->transfer_dma);
673err_free_bt_buffer:
674 usb_buffer_free(dev->udev, dev->cfg.bt_datalen,
675 dev->bt_data, dev->bt_urb->transfer_dma);
676err_free_urb:
677 usb_free_urb(dev->tp_urb);
678err_free_bt_urb:
679 usb_free_urb(dev->bt_urb);
680err_free_devs:
681 usb_set_intfdata(iface, NULL);
682 input_free_device(input_dev);
683 kfree(dev);
684 return error;
685}
686
687static void bcm5974_disconnect(struct usb_interface *iface)
688{
689 struct bcm5974 *dev = usb_get_intfdata(iface);
690
691 usb_set_intfdata(iface, NULL);
692
693 input_unregister_device(dev->input);
694 usb_buffer_free(dev->udev, dev->cfg.tp_datalen,
695 dev->tp_data, dev->tp_urb->transfer_dma);
696 usb_buffer_free(dev->udev, dev->cfg.bt_datalen,
697 dev->bt_data, dev->bt_urb->transfer_dma);
698 usb_free_urb(dev->tp_urb);
699 usb_free_urb(dev->bt_urb);
700 kfree(dev);
701}
702
703static struct usb_driver bcm5974_driver = {
704 .name = "bcm5974",
705 .probe = bcm5974_probe,
706 .disconnect = bcm5974_disconnect,
707 .suspend = bcm5974_suspend,
708 .resume = bcm5974_resume,
709 .reset_resume = bcm5974_resume,
710 .id_table = bcm5974_table,
711 .supports_autosuspend = 1,
712};
713
714static int __init bcm5974_init(void)
715{
716 return usb_register(&bcm5974_driver);
717}
718
719static void __exit bcm5974_exit(void)
720{
721 usb_deregister(&bcm5974_driver);
722}
723
724module_init(bcm5974_init);
725module_exit(bcm5974_exit);
726
727