2 * Apple USB Touchpad (for post-February 2005 PowerBooks and MacBooks) driver
4 * Copyright (C) 2001-2004 Greg Kroah-Hartman (greg@kroah.com)
5 * Copyright (C) 2005 Johannes Berg (johannes@sipsolutions.net)
6 * Copyright (C) 2005 Stelian Pop (stelian@popies.net)
7 * Copyright (C) 2005 Frank Arnold (frank@scirocco-5v-turbo.de)
8 * Copyright (C) 2005 Peter Osterlund (petero2@telia.com)
9 * Copyright (C) 2005 Michael Hanselmann (linux-kernel@hansmi.ch)
10 * Copyright (C) 2006 Nicolas Boichat (nicolas@boichat.ch)
12 * Thanks to Alex Harper <basilisk@foobox.net> for his inputs.
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 #include <linux/config.h>
31 #include <linux/kernel.h>
32 #include <linux/errno.h>
33 #include <linux/init.h>
34 #include <linux/slab.h>
35 #include <linux/module.h>
36 #include <linux/usb.h>
37 #include <linux/input.h>
38 #include <linux/usb_input.h>
40 /* Apple has powerbooks which have the keyboard with different Product IDs */
41 #define APPLE_VENDOR_ID 0x05AC
43 /* These names come from Info.plist in AppleUSBTrackpad.kext */
44 #define GEYSER_ANSI_PRODUCT_ID 0x0214
45 #define GEYSER_ISO_PRODUCT_ID 0x0215
46 #define GEYSER_JIS_PRODUCT_ID 0x0216
49 #define GEYSER3_ANSI_PRODUCT_ID 0x0217
50 #define GEYSER3_ISO_PRODUCT_ID 0x0218
51 #define GEYSER3_JIS_PRODUCT_ID 0x0219
53 #define ATP_DEVICE(prod) \
54 .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \
55 USB_DEVICE_ID_MATCH_INT_CLASS | \
56 USB_DEVICE_ID_MATCH_INT_PROTOCOL, \
57 .idVendor = APPLE_VENDOR_ID, \
58 .idProduct = (prod), \
59 .bInterfaceClass = 0x03, \
60 .bInterfaceProtocol = 0x02
62 /* table of devices that work with this driver */
63 static struct usb_device_id atp_table
[] = {
64 { ATP_DEVICE(0x020E) },
65 { ATP_DEVICE(0x020F) },
66 { ATP_DEVICE(0x030A) },
67 { ATP_DEVICE(0x030B) },
69 /* PowerBooks Oct 2005 */
70 { ATP_DEVICE(GEYSER_ANSI_PRODUCT_ID
) },
71 { ATP_DEVICE(GEYSER_ISO_PRODUCT_ID
) },
72 { ATP_DEVICE(GEYSER_JIS_PRODUCT_ID
) },
74 { ATP_DEVICE(GEYSER3_ANSI_PRODUCT_ID
) },
75 { ATP_DEVICE(GEYSER3_ISO_PRODUCT_ID
) },
76 { ATP_DEVICE(GEYSER3_JIS_PRODUCT_ID
) },
78 /* Terminating entry */
81 MODULE_DEVICE_TABLE (usb
, atp_table
);
84 * number of sensors. Note that only 16 instead of 26 X (horizontal)
85 * sensors exist on 12" and 15" PowerBooks. All models have 16 Y
88 #define ATP_XSENSORS 26
89 #define ATP_YSENSORS 16
91 /* amount of fuzz this touchpad generates */
94 /* maximum pressure this driver will report */
95 #define ATP_PRESSURE 300
97 * multiplication factor for the X and Y coordinates.
98 * We try to keep the touchpad aspect ratio while still doing only simple
100 * The factors below give coordinates like:
101 * 0 <= x < 960 on 12" and 15" Powerbooks
102 * 0 <= x < 1600 on 17" Powerbooks
109 * Threshold for the touchpad sensors. Any change less than ATP_THRESHOLD is
112 #define ATP_THRESHOLD 5
114 /* MacBook Pro (Geyser 3) initialization constants */
115 #define ATP_GEYSER3_MODE_READ_REQUEST_ID 1
116 #define ATP_GEYSER3_MODE_WRITE_REQUEST_ID 9
117 #define ATP_GEYSER3_MODE_REQUEST_VALUE 0x300
118 #define ATP_GEYSER3_MODE_REQUEST_INDEX 0
119 #define ATP_GEYSER3_MODE_VENDOR_VALUE 0x04
121 /* Structure to hold all of our device specific stuff */
124 struct usb_device
* udev
; /* usb device */
125 struct urb
* urb
; /* usb request block */
126 signed char * data
; /* transferred data */
127 int open
; /* non-zero if opened */
128 struct input_dev
*input
; /* input dev */
129 int valid
; /* are the sensors valid ? */
130 int x_old
; /* last reported x/y, */
131 int y_old
; /* used for smoothing */
132 /* current value of the sensors */
133 signed char xy_cur
[ATP_XSENSORS
+ ATP_YSENSORS
];
134 /* last value of the sensors */
135 signed char xy_old
[ATP_XSENSORS
+ ATP_YSENSORS
];
136 /* accumulated sensors */
137 int xy_acc
[ATP_XSENSORS
+ ATP_YSENSORS
];
138 int overflowwarn
; /* overflow warning printed? */
139 int datalen
; /* size of an USB urb transfer */
142 #define dbg_dump(msg, tab) \
145 printk("appletouch: %s %lld", msg, (long long)jiffies); \
146 for (i = 0; i < ATP_XSENSORS + ATP_YSENSORS; i++) \
147 printk(" %02x", tab[i]); \
151 #define dprintk(format, a...) \
153 if (debug) printk(format, ##a); \
156 MODULE_AUTHOR("Johannes Berg, Stelian Pop, Frank Arnold, Michael Hanselmann");
157 MODULE_DESCRIPTION("Apple PowerBooks USB touchpad driver");
158 MODULE_LICENSE("GPL");
160 static int debug
= 1;
161 module_param(debug
, int, 0644);
162 MODULE_PARM_DESC(debug
, "Activate debugging output");
164 /* Checks if the device a Geyser 2 (ANSI, ISO, JIS) */
165 static inline int atp_is_geyser_2(struct atp
*dev
)
167 u16 productId
= le16_to_cpu(dev
->udev
->descriptor
.idProduct
);
169 return (productId
== GEYSER_ANSI_PRODUCT_ID
) ||
170 (productId
== GEYSER_ISO_PRODUCT_ID
) ||
171 (productId
== GEYSER_JIS_PRODUCT_ID
);
174 static inline int atp_is_geyser_3(struct atp
*dev
)
176 u16 productId
= le16_to_cpu(dev
->udev
->descriptor
.idProduct
);
178 return (productId
== GEYSER3_ANSI_PRODUCT_ID
) ||
179 (productId
== GEYSER3_ISO_PRODUCT_ID
) ||
180 (productId
== GEYSER3_JIS_PRODUCT_ID
);
183 static int atp_calculate_abs(int *xy_sensors
, int nb_sensors
, int fact
,
184 int *z
, int *fingers
)
187 /* values to calculate mean */
188 int pcum
= 0, psum
= 0;
192 for (i
= 0; i
< nb_sensors
; i
++) {
193 if (xy_sensors
[i
] < ATP_THRESHOLD
)
195 if ((i
- 1 < 0) || (xy_sensors
[i
- 1] < ATP_THRESHOLD
))
197 pcum
+= xy_sensors
[i
] * i
;
198 psum
+= xy_sensors
[i
];
203 return pcum
* fact
/ psum
;
209 static inline void atp_report_fingers(struct input_dev
*input
, int fingers
)
211 input_report_key(input
, BTN_TOOL_FINGER
, fingers
== 1);
212 input_report_key(input
, BTN_TOOL_DOUBLETAP
, fingers
== 2);
213 input_report_key(input
, BTN_TOOL_TRIPLETAP
, fingers
> 2);
216 static void atp_complete(struct urb
* urb
, struct pt_regs
* regs
)
218 int x
, y
, x_z
, y_z
, x_f
, y_f
;
220 struct atp
*dev
= urb
->context
;
222 switch (urb
->status
) {
227 if(!dev
->overflowwarn
) {
228 printk("appletouch: OVERFLOW with data "
229 "length %d, actual length is %d\n",
230 dev
->datalen
, dev
->urb
->actual_length
);
231 dev
->overflowwarn
= 1;
236 /* This urb is terminated, clean up */
237 dbg("%s - urb shutting down with status: %d",
238 __FUNCTION__
, urb
->status
);
241 dbg("%s - nonzero urb status received: %d",
242 __FUNCTION__
, urb
->status
);
246 /* drop incomplete datasets */
247 if (dev
->urb
->actual_length
!= dev
->datalen
) {
248 dprintk("appletouch: incomplete data package"
249 " (first byte: %d, length: %d).\n",
250 dev
->data
[0], dev
->urb
->actual_length
);
254 /* reorder the sensors values */
255 if (atp_is_geyser_3(dev
)) {
256 memset(dev
->xy_cur
, 0, sizeof(dev
->xy_cur
));
259 * The values are laid out like this:
260 * -, Y1, Y2, -, Y3, Y4, -, ..., -, X1, X2, -, X3, X4, ...
261 * '-' is an unused value.
265 for (i
= 0, j
= 19; i
< 20; i
+= 2, j
+= 3) {
266 dev
->xy_cur
[i
] = dev
->data
[j
+ 1];
267 dev
->xy_cur
[i
+ 1] = dev
->data
[j
+ 2];
270 for (i
= 0, j
= 1; i
< 9; i
+= 2, j
+= 3) {
271 dev
->xy_cur
[ATP_XSENSORS
+ i
] = dev
->data
[j
+ 1];
272 dev
->xy_cur
[ATP_XSENSORS
+ i
+ 1] = dev
->data
[j
+ 2];
274 } else if (atp_is_geyser_2(dev
)) {
275 memset(dev
->xy_cur
, 0, sizeof(dev
->xy_cur
));
278 * The values are laid out like this:
279 * Y1, Y2, -, Y3, Y4, -, ..., X1, X2, -, X3, X4, -, ...
280 * '-' is an unused value.
284 for (i
= 0, j
= 19; i
< 20; i
+= 2, j
+= 3) {
285 dev
->xy_cur
[i
] = dev
->data
[j
];
286 dev
->xy_cur
[i
+ 1] = dev
->data
[j
+ 1];
290 for (i
= 0, j
= 1; i
< 9; i
+= 2, j
+= 3) {
291 dev
->xy_cur
[ATP_XSENSORS
+ i
] = dev
->data
[j
];
292 dev
->xy_cur
[ATP_XSENSORS
+ i
+ 1] = dev
->data
[j
+ 1];
295 for (i
= 0; i
< 8; i
++) {
297 dev
->xy_cur
[i
] = dev
->data
[5 * i
+ 2];
298 dev
->xy_cur
[i
+ 8] = dev
->data
[5 * i
+ 4];
299 dev
->xy_cur
[i
+ 16] = dev
->data
[5 * i
+ 42];
301 dev
->xy_cur
[i
+ 24] = dev
->data
[5 * i
+ 44];
304 dev
->xy_cur
[i
+ 26] = dev
->data
[5 * i
+ 1];
305 dev
->xy_cur
[i
+ 34] = dev
->data
[5 * i
+ 3];
309 dbg_dump("sample", dev
->xy_cur
);
314 dev
->x_old
= dev
->y_old
= -1;
315 memcpy(dev
->xy_old
, dev
->xy_cur
, sizeof(dev
->xy_old
));
317 if (atp_is_geyser_3(dev
)) /* No 17" Macbooks (yet) */
320 /* 17" Powerbooks have extra X sensors */
321 for (i
= (atp_is_geyser_2(dev
)?15:16); i
< ATP_XSENSORS
; i
++) {
322 if (!dev
->xy_cur
[i
]) continue;
324 printk("appletouch: 17\" model detected.\n");
325 if(atp_is_geyser_2(dev
))
326 input_set_abs_params(dev
->input
, ABS_X
, 0,
331 input_set_abs_params(dev
->input
, ABS_X
, 0,
342 for (i
= 0; i
< ATP_XSENSORS
+ ATP_YSENSORS
; i
++) {
343 /* accumulate the change */
344 signed char change
= dev
->xy_old
[i
] - dev
->xy_cur
[i
];
345 dev
->xy_acc
[i
] -= change
;
347 /* prevent down drifting */
348 if (dev
->xy_acc
[i
] < 0)
352 memcpy(dev
->xy_old
, dev
->xy_cur
, sizeof(dev
->xy_old
));
354 dbg_dump("accumulator", dev
->xy_acc
);
356 x
= atp_calculate_abs(dev
->xy_acc
, ATP_XSENSORS
,
357 ATP_XFACT
, &x_z
, &x_f
);
358 y
= atp_calculate_abs(dev
->xy_acc
+ ATP_XSENSORS
, ATP_YSENSORS
,
359 ATP_YFACT
, &y_z
, &y_f
);
362 if (dev
->x_old
!= -1) {
363 x
= (dev
->x_old
* 3 + x
) >> 2;
364 y
= (dev
->y_old
* 3 + y
) >> 2;
369 printk("appletouch: X: %3d Y: %3d "
373 input_report_key(dev
->input
, BTN_TOUCH
, 1);
374 input_report_abs(dev
->input
, ABS_X
, x
);
375 input_report_abs(dev
->input
, ABS_Y
, y
);
376 input_report_abs(dev
->input
, ABS_PRESSURE
,
377 min(ATP_PRESSURE
, x_z
+ y_z
));
378 atp_report_fingers(dev
->input
, max(x_f
, y_f
));
385 dev
->x_old
= dev
->y_old
= -1;
386 input_report_key(dev
->input
, BTN_TOUCH
, 0);
387 input_report_abs(dev
->input
, ABS_PRESSURE
, 0);
388 atp_report_fingers(dev
->input
, 0);
390 /* reset the accumulator on release */
391 memset(dev
->xy_acc
, 0, sizeof(dev
->xy_acc
));
394 input_report_key(dev
->input
, BTN_LEFT
,
395 !!dev
->data
[dev
->datalen
- 1]);
397 input_sync(dev
->input
);
400 retval
= usb_submit_urb(dev
->urb
, GFP_ATOMIC
);
402 err("%s - usb_submit_urb failed with result %d",
403 __FUNCTION__
, retval
);
407 static int atp_open(struct input_dev
*input
)
409 struct atp
*dev
= input
->private;
411 if (usb_submit_urb(dev
->urb
, GFP_ATOMIC
))
418 static void atp_close(struct input_dev
*input
)
420 struct atp
*dev
= input
->private;
422 usb_kill_urb(dev
->urb
);
426 static int atp_probe(struct usb_interface
*iface
, const struct usb_device_id
*id
)
429 struct input_dev
*input_dev
;
430 struct usb_device
*udev
= interface_to_usbdev(iface
);
431 struct usb_host_interface
*iface_desc
;
432 struct usb_endpoint_descriptor
*endpoint
;
433 int int_in_endpointAddr
= 0;
434 int i
, retval
= -ENOMEM
;
437 /* set up the endpoint information */
438 /* use only the first interrupt-in endpoint */
439 iface_desc
= iface
->cur_altsetting
;
440 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; i
++) {
441 endpoint
= &iface_desc
->endpoint
[i
].desc
;
442 if (!int_in_endpointAddr
&&
443 (endpoint
->bEndpointAddress
& USB_DIR_IN
) &&
444 ((endpoint
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
)
445 == USB_ENDPOINT_XFER_INT
)) {
446 /* we found an interrupt in endpoint */
447 int_in_endpointAddr
= endpoint
->bEndpointAddress
;
451 if (!int_in_endpointAddr
) {
452 err("Could not find int-in endpoint");
456 /* allocate memory for our device state and initialize it */
457 dev
= kzalloc(sizeof(struct atp
), GFP_KERNEL
);
458 input_dev
= input_allocate_device();
459 if (!dev
|| !input_dev
) {
460 err("Out of memory");
465 dev
->input
= input_dev
;
466 dev
->overflowwarn
= 0;
467 if (atp_is_geyser_3(dev
))
469 else if (atp_is_geyser_2(dev
))
474 if (atp_is_geyser_3(dev
)) {
476 * By default Geyser 3 device sends standard USB HID mouse
477 * packets (Report ID 2). This code changes device mode, so it
478 * sends raw sensor reports (Report ID 5).
483 size
= usb_control_msg(udev
, usb_rcvctrlpipe(udev
, 0),
484 ATP_GEYSER3_MODE_READ_REQUEST_ID
,
485 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
486 ATP_GEYSER3_MODE_REQUEST_VALUE
,
487 ATP_GEYSER3_MODE_REQUEST_INDEX
, &data
, 8, 5000);
490 err("Could not do mode read request from device"
495 /* Apply the mode switch */
496 data
[0] = ATP_GEYSER3_MODE_VENDOR_VALUE
;
498 size
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
499 ATP_GEYSER3_MODE_WRITE_REQUEST_ID
,
500 USB_DIR_OUT
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
501 ATP_GEYSER3_MODE_REQUEST_VALUE
,
502 ATP_GEYSER3_MODE_REQUEST_INDEX
, &data
, 8, 5000);
505 err("Could not do mode write request to device"
509 printk("appletouch Geyser 3 inited.\n");
512 dev
->urb
= usb_alloc_urb(0, GFP_KERNEL
);
518 dev
->data
= usb_buffer_alloc(dev
->udev
, dev
->datalen
, GFP_KERNEL
,
519 &dev
->urb
->transfer_dma
);
525 usb_fill_int_urb(dev
->urb
, udev
,
526 usb_rcvintpipe(udev
, int_in_endpointAddr
),
527 dev
->data
, dev
->datalen
, atp_complete
, dev
, 1);
529 usb_make_path(udev
, dev
->phys
, sizeof(dev
->phys
));
530 strlcat(dev
->phys
, "/input0", sizeof(dev
->phys
));
532 input_dev
->name
= "appletouch";
533 input_dev
->phys
= dev
->phys
;
534 usb_to_input_id(dev
->udev
, &input_dev
->id
);
535 input_dev
->cdev
.dev
= &iface
->dev
;
537 input_dev
->private = dev
;
538 input_dev
->open
= atp_open
;
539 input_dev
->close
= atp_close
;
541 set_bit(EV_ABS
, input_dev
->evbit
);
543 if (atp_is_geyser_3(dev
)) {
545 * MacBook have 20 X sensors, 10 Y sensors
547 input_set_abs_params(input_dev
, ABS_X
, 0,
548 ((20 - 1) * ATP_XFACT
) - 1, ATP_FUZZ
, 0);
549 input_set_abs_params(input_dev
, ABS_Y
, 0,
550 ((10 - 1) * ATP_YFACT
) - 1, ATP_FUZZ
, 0);
551 } else if (atp_is_geyser_2(dev
)) {
553 * Oct 2005 15" PowerBooks have 15 X sensors, 17" are detected
556 input_set_abs_params(input_dev
, ABS_X
, 0,
557 ((15 - 1) * ATP_XFACT
) - 1, ATP_FUZZ
, 0);
558 input_set_abs_params(input_dev
, ABS_Y
, 0,
559 ((9 - 1) * ATP_YFACT
) - 1, ATP_FUZZ
, 0);
562 * 12" and 15" Powerbooks only have 16 x sensors,
563 * 17" models are detected later.
565 input_set_abs_params(input_dev
, ABS_X
, 0,
566 (16 - 1) * ATP_XFACT
- 1, ATP_FUZZ
, 0);
567 input_set_abs_params(input_dev
, ABS_Y
, 0,
568 (ATP_YSENSORS
- 1) * ATP_YFACT
- 1, ATP_FUZZ
, 0);
570 input_set_abs_params(input_dev
, ABS_PRESSURE
, 0, ATP_PRESSURE
, 0, 0);
572 set_bit(EV_KEY
, input_dev
->evbit
);
573 set_bit(BTN_TOUCH
, input_dev
->keybit
);
574 set_bit(BTN_TOOL_FINGER
, input_dev
->keybit
);
575 set_bit(BTN_TOOL_DOUBLETAP
, input_dev
->keybit
);
576 set_bit(BTN_TOOL_TRIPLETAP
, input_dev
->keybit
);
577 set_bit(BTN_LEFT
, input_dev
->keybit
);
579 input_register_device(dev
->input
);
581 /* save our data pointer in this interface device */
582 usb_set_intfdata(iface
, dev
);
587 usb_free_urb(dev
->urb
);
589 usb_set_intfdata(iface
, NULL
);
591 input_free_device(input_dev
);
595 static void atp_disconnect(struct usb_interface
*iface
)
597 struct atp
*dev
= usb_get_intfdata(iface
);
599 usb_set_intfdata(iface
, NULL
);
601 usb_kill_urb(dev
->urb
);
602 input_unregister_device(dev
->input
);
603 usb_free_urb(dev
->urb
);
604 usb_buffer_free(dev
->udev
, dev
->datalen
,
605 dev
->data
, dev
->urb
->transfer_dma
);
608 printk(KERN_INFO
"input: appletouch disconnected\n");
611 static int atp_suspend(struct usb_interface
*iface
, pm_message_t message
)
613 struct atp
*dev
= usb_get_intfdata(iface
);
614 usb_kill_urb(dev
->urb
);
619 static int atp_resume(struct usb_interface
*iface
)
621 struct atp
*dev
= usb_get_intfdata(iface
);
622 if (dev
->open
&& usb_submit_urb(dev
->urb
, GFP_ATOMIC
))
628 static struct usb_driver atp_driver
= {
629 .name
= "appletouch",
631 .disconnect
= atp_disconnect
,
632 .suspend
= atp_suspend
,
633 .resume
= atp_resume
,
634 .id_table
= atp_table
,
637 static int __init
atp_init(void)
639 return usb_register(&atp_driver
);
642 static void __exit
atp_exit(void)
644 usb_deregister(&atp_driver
);
647 module_init(atp_init
);
648 module_exit(atp_exit
);