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