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/input.h>
38 /* Apple has powerbooks which have the keyboard with different Product IDs */
39 #define APPLE_VENDOR_ID 0x05AC
41 /* These names come from Info.plist in AppleUSBTrackpad.kext */
42 #define GEYSER_ANSI_PRODUCT_ID 0x0214
43 #define GEYSER_ISO_PRODUCT_ID 0x0215
44 #define GEYSER_JIS_PRODUCT_ID 0x0216
47 #define GEYSER3_ANSI_PRODUCT_ID 0x0217
48 #define GEYSER3_ISO_PRODUCT_ID 0x0218
49 #define GEYSER3_JIS_PRODUCT_ID 0x0219
51 #define ATP_DEVICE(prod) \
52 .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \
53 USB_DEVICE_ID_MATCH_INT_CLASS | \
54 USB_DEVICE_ID_MATCH_INT_PROTOCOL, \
55 .idVendor = APPLE_VENDOR_ID, \
56 .idProduct = (prod), \
57 .bInterfaceClass = 0x03, \
58 .bInterfaceProtocol = 0x02
60 /* table of devices that work with this driver */
61 static struct usb_device_id atp_table [] = {
62 { ATP_DEVICE(0x020E) },
63 { ATP_DEVICE(0x020F) },
64 { ATP_DEVICE(0x030A) },
65 { ATP_DEVICE(0x030B) },
67 /* PowerBooks Oct 2005 */
68 { ATP_DEVICE(GEYSER_ANSI_PRODUCT_ID) },
69 { ATP_DEVICE(GEYSER_ISO_PRODUCT_ID) },
70 { ATP_DEVICE(GEYSER_JIS_PRODUCT_ID) },
72 { ATP_DEVICE(GEYSER3_ANSI_PRODUCT_ID) },
73 { ATP_DEVICE(GEYSER3_ISO_PRODUCT_ID) },
74 { ATP_DEVICE(GEYSER3_JIS_PRODUCT_ID) },
76 /* Terminating entry */
79 MODULE_DEVICE_TABLE (usb, atp_table);
82 * number of sensors. Note that only 16 instead of 26 X (horizontal)
83 * sensors exist on 12" and 15" PowerBooks. All models have 16 Y
86 #define ATP_XSENSORS 26
87 #define ATP_YSENSORS 16
89 /* amount of fuzz this touchpad generates */
92 /* maximum pressure this driver will report */
93 #define ATP_PRESSURE 300
95 * multiplication factor for the X and Y coordinates.
96 * We try to keep the touchpad aspect ratio while still doing only simple
98 * The factors below give coordinates like:
99 * 0 <= x < 960 on 12" and 15" Powerbooks
100 * 0 <= x < 1600 on 17" Powerbooks
107 * Threshold for the touchpad sensors. Any change less than ATP_THRESHOLD is
110 #define ATP_THRESHOLD 5
112 /* MacBook Pro (Geyser 3) initialization constants */
113 #define ATP_GEYSER3_MODE_READ_REQUEST_ID 1
114 #define ATP_GEYSER3_MODE_WRITE_REQUEST_ID 9
115 #define ATP_GEYSER3_MODE_REQUEST_VALUE 0x300
116 #define ATP_GEYSER3_MODE_REQUEST_INDEX 0
117 #define ATP_GEYSER3_MODE_VENDOR_VALUE 0x04
119 /* Structure to hold all of our device specific stuff */
122 struct usb_device * udev; /* usb device */
123 struct urb * urb; /* usb request block */
124 signed char * data; /* transferred data */
125 int open; /* non-zero if opened */
126 struct input_dev *input; /* input dev */
127 int valid; /* are the sensors valid ? */
128 int x_old; /* last reported x/y, */
129 int y_old; /* used for smoothing */
130 /* current value of the sensors */
131 signed char xy_cur[ATP_XSENSORS + ATP_YSENSORS];
132 /* last value of the sensors */
133 signed char xy_old[ATP_XSENSORS + ATP_YSENSORS];
134 /* accumulated sensors */
135 int xy_acc[ATP_XSENSORS + ATP_YSENSORS];
136 int overflowwarn; /* overflow warning printed? */
137 int datalen; /* size of an USB urb transfer */
140 #define dbg_dump(msg, tab) \
143 printk("appletouch: %s %lld", msg, (long long)jiffies); \
144 for (i = 0; i < ATP_XSENSORS + ATP_YSENSORS; i++) \
145 printk(" %02x", tab[i]); \
149 #define dprintk(format, a...) \
151 if (debug) printk(format, ##a); \
154 MODULE_AUTHOR("Johannes Berg, Stelian Pop, Frank Arnold, Michael Hanselmann");
155 MODULE_DESCRIPTION("Apple PowerBooks USB touchpad driver");
156 MODULE_LICENSE("GPL");
158 static int debug = 1;
159 module_param(debug, int, 0644);
160 MODULE_PARM_DESC(debug, "Activate debugging output");
162 /* Checks if the device a Geyser 2 (ANSI, ISO, JIS) */
163 static inline int atp_is_geyser_2(struct atp *dev)
165 u16 productId = le16_to_cpu(dev->udev->descriptor.idProduct);
167 return (productId == GEYSER_ANSI_PRODUCT_ID) ||
168 (productId == GEYSER_ISO_PRODUCT_ID) ||
169 (productId == GEYSER_JIS_PRODUCT_ID);
172 static inline int atp_is_geyser_3(struct atp *dev)
174 u16 productId = le16_to_cpu(dev->udev->descriptor.idProduct);
176 return (productId == GEYSER3_ANSI_PRODUCT_ID) ||
177 (productId == GEYSER3_ISO_PRODUCT_ID) ||
178 (productId == GEYSER3_JIS_PRODUCT_ID);
181 static int atp_calculate_abs(int *xy_sensors, int nb_sensors, int fact,
182 int *z, int *fingers)
185 /* values to calculate mean */
186 int pcum = 0, psum = 0;
190 for (i = 0; i < nb_sensors; i++) {
191 if (xy_sensors[i] < ATP_THRESHOLD)
193 if ((i - 1 < 0) || (xy_sensors[i - 1] < ATP_THRESHOLD))
195 pcum += xy_sensors[i] * i;
196 psum += xy_sensors[i];
201 return pcum * fact / psum;
207 static inline void atp_report_fingers(struct input_dev *input, int fingers)
209 input_report_key(input, BTN_TOOL_FINGER, fingers == 1);
210 input_report_key(input, BTN_TOOL_DOUBLETAP, fingers == 2);
211 input_report_key(input, BTN_TOOL_TRIPLETAP, fingers > 2);
214 static void atp_complete(struct urb* urb, struct pt_regs* regs)
216 int x, y, x_z, y_z, x_f, y_f;
218 struct atp *dev = urb->context;
220 switch (urb->status) {
225 if(!dev->overflowwarn) {
226 printk("appletouch: OVERFLOW with data "
227 "length %d, actual length is %d\n",
228 dev->datalen, dev->urb->actual_length);
229 dev->overflowwarn = 1;
234 /* This urb is terminated, clean up */
235 dbg("%s - urb shutting down with status: %d",
236 __FUNCTION__, urb->status);
239 dbg("%s - nonzero urb status received: %d",
240 __FUNCTION__, urb->status);
244 /* drop incomplete datasets */
245 if (dev->urb->actual_length != dev->datalen) {
246 dprintk("appletouch: incomplete data package"
247 " (first byte: %d, length: %d).\n",
248 dev->data[0], dev->urb->actual_length);
252 /* reorder the sensors values */
253 if (atp_is_geyser_3(dev)) {
254 memset(dev->xy_cur, 0, sizeof(dev->xy_cur));
257 * The values are laid out like this:
258 * -, Y1, Y2, -, Y3, Y4, -, ..., -, X1, X2, -, X3, X4, ...
259 * '-' is an unused value.
263 for (i = 0, j = 19; i < 20; i += 2, j += 3) {
264 dev->xy_cur[i] = dev->data[j + 1];
265 dev->xy_cur[i + 1] = dev->data[j + 2];
268 for (i = 0, j = 1; i < 9; i += 2, j += 3) {
269 dev->xy_cur[ATP_XSENSORS + i] = dev->data[j + 1];
270 dev->xy_cur[ATP_XSENSORS + i + 1] = dev->data[j + 2];
272 } else if (atp_is_geyser_2(dev)) {
273 memset(dev->xy_cur, 0, sizeof(dev->xy_cur));
276 * The values are laid out like this:
277 * Y1, Y2, -, Y3, Y4, -, ..., X1, X2, -, X3, X4, -, ...
278 * '-' is an unused value.
282 for (i = 0, j = 19; i < 20; i += 2, j += 3) {
283 dev->xy_cur[i] = dev->data[j];
284 dev->xy_cur[i + 1] = dev->data[j + 1];
288 for (i = 0, j = 1; i < 9; i += 2, j += 3) {
289 dev->xy_cur[ATP_XSENSORS + i] = dev->data[j];
290 dev->xy_cur[ATP_XSENSORS + i + 1] = dev->data[j + 1];
293 for (i = 0; i < 8; i++) {
295 dev->xy_cur[i ] = dev->data[5 * i + 2];
296 dev->xy_cur[i + 8] = dev->data[5 * i + 4];
297 dev->xy_cur[i + 16] = dev->data[5 * i + 42];
299 dev->xy_cur[i + 24] = dev->data[5 * i + 44];
302 dev->xy_cur[i + 26] = dev->data[5 * i + 1];
303 dev->xy_cur[i + 34] = dev->data[5 * i + 3];
307 dbg_dump("sample", dev->xy_cur);
312 dev->x_old = dev->y_old = -1;
313 memcpy(dev->xy_old, dev->xy_cur, sizeof(dev->xy_old));
315 if (atp_is_geyser_3(dev)) /* No 17" Macbooks (yet) */
318 /* 17" Powerbooks have extra X sensors */
319 for (i = (atp_is_geyser_2(dev)?15:16); i < ATP_XSENSORS; i++) {
320 if (!dev->xy_cur[i]) continue;
322 printk("appletouch: 17\" model detected.\n");
323 if(atp_is_geyser_2(dev))
324 input_set_abs_params(dev->input, ABS_X, 0,
329 input_set_abs_params(dev->input, ABS_X, 0,
340 for (i = 0; i < ATP_XSENSORS + ATP_YSENSORS; i++) {
341 /* accumulate the change */
342 signed char change = dev->xy_old[i] - dev->xy_cur[i];
343 dev->xy_acc[i] -= change;
345 /* prevent down drifting */
346 if (dev->xy_acc[i] < 0)
350 memcpy(dev->xy_old, dev->xy_cur, sizeof(dev->xy_old));
352 dbg_dump("accumulator", dev->xy_acc);
354 x = atp_calculate_abs(dev->xy_acc, ATP_XSENSORS,
355 ATP_XFACT, &x_z, &x_f);
356 y = atp_calculate_abs(dev->xy_acc + ATP_XSENSORS, ATP_YSENSORS,
357 ATP_YFACT, &y_z, &y_f);
360 if (dev->x_old != -1) {
361 x = (dev->x_old * 3 + x) >> 2;
362 y = (dev->y_old * 3 + y) >> 2;
367 printk("appletouch: X: %3d Y: %3d "
371 input_report_key(dev->input, BTN_TOUCH, 1);
372 input_report_abs(dev->input, ABS_X, x);
373 input_report_abs(dev->input, ABS_Y, y);
374 input_report_abs(dev->input, ABS_PRESSURE,
375 min(ATP_PRESSURE, x_z + y_z));
376 atp_report_fingers(dev->input, max(x_f, y_f));
383 dev->x_old = dev->y_old = -1;
384 input_report_key(dev->input, BTN_TOUCH, 0);
385 input_report_abs(dev->input, ABS_PRESSURE, 0);
386 atp_report_fingers(dev->input, 0);
388 /* reset the accumulator on release */
389 memset(dev->xy_acc, 0, sizeof(dev->xy_acc));
392 input_report_key(dev->input, BTN_LEFT,
393 !!dev->data[dev->datalen - 1]);
395 input_sync(dev->input);
398 retval = usb_submit_urb(dev->urb, GFP_ATOMIC);
400 err("%s - usb_submit_urb failed with result %d",
401 __FUNCTION__, retval);
405 static int atp_open(struct input_dev *input)
407 struct atp *dev = input->private;
409 if (usb_submit_urb(dev->urb, GFP_ATOMIC))
416 static void atp_close(struct input_dev *input)
418 struct atp *dev = input->private;
420 usb_kill_urb(dev->urb);
424 static int atp_probe(struct usb_interface *iface, const struct usb_device_id *id)
427 struct input_dev *input_dev;
428 struct usb_device *udev = interface_to_usbdev(iface);
429 struct usb_host_interface *iface_desc;
430 struct usb_endpoint_descriptor *endpoint;
431 int int_in_endpointAddr = 0;
432 int i, retval = -ENOMEM;
435 /* set up the endpoint information */
436 /* use only the first interrupt-in endpoint */
437 iface_desc = iface->cur_altsetting;
438 for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
439 endpoint = &iface_desc->endpoint[i].desc;
440 if (!int_in_endpointAddr &&
441 (endpoint->bEndpointAddress & USB_DIR_IN) &&
442 ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
443 == USB_ENDPOINT_XFER_INT)) {
444 /* we found an interrupt in endpoint */
445 int_in_endpointAddr = endpoint->bEndpointAddress;
449 if (!int_in_endpointAddr) {
450 err("Could not find int-in endpoint");
454 /* allocate memory for our device state and initialize it */
455 dev = kzalloc(sizeof(struct atp), GFP_KERNEL);
456 input_dev = input_allocate_device();
457 if (!dev || !input_dev) {
458 err("Out of memory");
463 dev->input = input_dev;
464 dev->overflowwarn = 0;
465 if (atp_is_geyser_3(dev))
467 else if (atp_is_geyser_2(dev))
472 if (atp_is_geyser_3(dev)) {
474 * By default Geyser 3 device sends standard USB HID mouse
475 * packets (Report ID 2). This code changes device mode, so it
476 * sends raw sensor reports (Report ID 5).
481 size = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
482 ATP_GEYSER3_MODE_READ_REQUEST_ID,
483 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
484 ATP_GEYSER3_MODE_REQUEST_VALUE,
485 ATP_GEYSER3_MODE_REQUEST_INDEX, &data, 8, 5000);
488 err("Could not do mode read request from device"
493 /* Apply the mode switch */
494 data[0] = ATP_GEYSER3_MODE_VENDOR_VALUE;
496 size = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
497 ATP_GEYSER3_MODE_WRITE_REQUEST_ID,
498 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
499 ATP_GEYSER3_MODE_REQUEST_VALUE,
500 ATP_GEYSER3_MODE_REQUEST_INDEX, &data, 8, 5000);
503 err("Could not do mode write request to device"
507 printk("appletouch Geyser 3 inited.\n");
510 dev->urb = usb_alloc_urb(0, GFP_KERNEL);
516 dev->data = usb_buffer_alloc(dev->udev, dev->datalen, GFP_KERNEL,
517 &dev->urb->transfer_dma);
523 usb_fill_int_urb(dev->urb, udev,
524 usb_rcvintpipe(udev, int_in_endpointAddr),
525 dev->data, dev->datalen, atp_complete, dev, 1);
527 usb_make_path(udev, dev->phys, sizeof(dev->phys));
528 strlcat(dev->phys, "/input0", sizeof(dev->phys));
530 input_dev->name = "appletouch";
531 input_dev->phys = dev->phys;
532 usb_to_input_id(dev->udev, &input_dev->id);
533 input_dev->cdev.dev = &iface->dev;
535 input_dev->private = dev;
536 input_dev->open = atp_open;
537 input_dev->close = atp_close;
539 set_bit(EV_ABS, input_dev->evbit);
541 if (atp_is_geyser_3(dev)) {
543 * MacBook have 20 X sensors, 10 Y sensors
545 input_set_abs_params(input_dev, ABS_X, 0,
546 ((20 - 1) * ATP_XFACT) - 1, ATP_FUZZ, 0);
547 input_set_abs_params(input_dev, ABS_Y, 0,
548 ((10 - 1) * ATP_YFACT) - 1, ATP_FUZZ, 0);
549 } else if (atp_is_geyser_2(dev)) {
551 * Oct 2005 15" PowerBooks have 15 X sensors, 17" are detected
554 input_set_abs_params(input_dev, ABS_X, 0,
555 ((15 - 1) * ATP_XFACT) - 1, ATP_FUZZ, 0);
556 input_set_abs_params(input_dev, ABS_Y, 0,
557 ((9 - 1) * ATP_YFACT) - 1, ATP_FUZZ, 0);
560 * 12" and 15" Powerbooks only have 16 x sensors,
561 * 17" models are detected later.
563 input_set_abs_params(input_dev, ABS_X, 0,
564 (16 - 1) * ATP_XFACT - 1, ATP_FUZZ, 0);
565 input_set_abs_params(input_dev, ABS_Y, 0,
566 (ATP_YSENSORS - 1) * ATP_YFACT - 1, ATP_FUZZ, 0);
568 input_set_abs_params(input_dev, ABS_PRESSURE, 0, ATP_PRESSURE, 0, 0);
570 set_bit(EV_KEY, input_dev->evbit);
571 set_bit(BTN_TOUCH, input_dev->keybit);
572 set_bit(BTN_TOOL_FINGER, input_dev->keybit);
573 set_bit(BTN_TOOL_DOUBLETAP, input_dev->keybit);
574 set_bit(BTN_TOOL_TRIPLETAP, input_dev->keybit);
575 set_bit(BTN_LEFT, input_dev->keybit);
577 input_register_device(dev->input);
579 /* save our data pointer in this interface device */
580 usb_set_intfdata(iface, dev);
585 usb_free_urb(dev->urb);
587 usb_set_intfdata(iface, NULL);
589 input_free_device(input_dev);
593 static void atp_disconnect(struct usb_interface *iface)
595 struct atp *dev = usb_get_intfdata(iface);
597 usb_set_intfdata(iface, NULL);
599 usb_kill_urb(dev->urb);
600 input_unregister_device(dev->input);
601 usb_free_urb(dev->urb);
602 usb_buffer_free(dev->udev, dev->datalen,
603 dev->data, dev->urb->transfer_dma);
606 printk(KERN_INFO "input: appletouch disconnected\n");
609 static int atp_suspend(struct usb_interface *iface, pm_message_t message)
611 struct atp *dev = usb_get_intfdata(iface);
612 usb_kill_urb(dev->urb);
617 static int atp_resume(struct usb_interface *iface)
619 struct atp *dev = usb_get_intfdata(iface);
620 if (dev->open && usb_submit_urb(dev->urb, GFP_ATOMIC))
626 static struct usb_driver atp_driver = {
627 .name = "appletouch",
629 .disconnect = atp_disconnect,
630 .suspend = atp_suspend,
631 .resume = atp_resume,
632 .id_table = atp_table,
635 static int __init atp_init(void)
637 return usb_register(&atp_driver);
640 static void __exit atp_exit(void)
642 usb_deregister(&atp_driver);
645 module_init(atp_init);
646 module_exit(atp_exit);