2 * Apple USB Touchpad (for post-February 2005 PowerBooks) 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)
10 * Thanks to Alex Harper <basilisk@foobox.net> for his inputs.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include <linux/config.h>
29 #include <linux/kernel.h>
30 #include <linux/errno.h>
31 #include <linux/init.h>
32 #include <linux/slab.h>
33 #include <linux/module.h>
34 #include <linux/usb.h>
35 #include <linux/input.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 #define ATP_DEVICE(prod) \
42 .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \
43 USB_DEVICE_ID_MATCH_INT_CLASS | \
44 USB_DEVICE_ID_MATCH_INT_PROTOCOL, \
45 .idVendor = APPLE_VENDOR_ID, \
46 .idProduct = (prod), \
47 .bInterfaceClass = 0x03, \
48 .bInterfaceProtocol = 0x02
50 /* table of devices that work with this driver */
51 static struct usb_device_id atp_table [] = {
52 { ATP_DEVICE(0x020E) },
53 { ATP_DEVICE(0x020F) },
54 { ATP_DEVICE(0x030A) },
55 { ATP_DEVICE(0x030B) },
56 { } /* Terminating entry */
58 MODULE_DEVICE_TABLE (usb, atp_table);
60 /* size of a USB urb transfer */
61 #define ATP_DATASIZE 81
64 * number of sensors. Note that only 16 instead of 26 X (horizontal)
65 * sensors exist on 12" and 15" PowerBooks. All models have 16 Y
68 #define ATP_XSENSORS 26
69 #define ATP_YSENSORS 16
71 /* amount of fuzz this touchpad generates */
74 /* maximum pressure this driver will report */
75 #define ATP_PRESSURE 300
77 * multiplication factor for the X and Y coordinates.
78 * We try to keep the touchpad aspect ratio while still doing only simple
80 * The factors below give coordinates like:
81 * 0 <= x < 960 on 12" and 15" Powerbooks
82 * 0 <= x < 1600 on 17" Powerbooks
89 * Threshold for the touchpad sensors. Any change less than ATP_THRESHOLD is
92 #define ATP_THRESHOLD 5
94 /* Structure to hold all of our device specific stuff */
97 struct usb_device * udev; /* usb device */
98 struct urb * urb; /* usb request block */
99 signed char * data; /* transferred data */
100 int open; /* non-zero if opened */
101 struct input_dev *input; /* input dev */
102 int valid; /* are the sensors valid ? */
103 int x_old; /* last reported x/y, */
104 int y_old; /* used for smoothing */
105 /* current value of the sensors */
106 signed char xy_cur[ATP_XSENSORS + ATP_YSENSORS];
107 /* last value of the sensors */
108 signed char xy_old[ATP_XSENSORS + ATP_YSENSORS];
109 /* accumulated sensors */
110 int xy_acc[ATP_XSENSORS + ATP_YSENSORS];
113 #define dbg_dump(msg, tab) \
116 printk("appletouch: %s %lld", msg, (long long)jiffies); \
117 for (i = 0; i < ATP_XSENSORS + ATP_YSENSORS; i++) \
118 printk(" %02x", tab[i]); \
122 #define dprintk(format, a...) \
124 if (debug) printk(format, ##a); \
127 MODULE_AUTHOR("Johannes Berg, Stelian Pop, Frank Arnold");
128 MODULE_DESCRIPTION("Apple PowerBooks USB touchpad driver");
129 MODULE_LICENSE("GPL");
131 static int debug = 1;
132 module_param(debug, int, 0644);
133 MODULE_PARM_DESC(debug, "Activate debugging output");
135 static int atp_calculate_abs(int *xy_sensors, int nb_sensors, int fact,
136 int *z, int *fingers)
139 /* values to calculate mean */
140 int pcum = 0, psum = 0;
144 for (i = 0; i < nb_sensors; i++) {
145 if (xy_sensors[i] < ATP_THRESHOLD)
147 if ((i - 1 < 0) || (xy_sensors[i - 1] < ATP_THRESHOLD))
149 pcum += xy_sensors[i] * i;
150 psum += xy_sensors[i];
155 return pcum * fact / psum;
161 static inline void atp_report_fingers(struct input_dev *input, int fingers)
163 input_report_key(input, BTN_TOOL_FINGER, fingers == 1);
164 input_report_key(input, BTN_TOOL_DOUBLETAP, fingers == 2);
165 input_report_key(input, BTN_TOOL_TRIPLETAP, fingers > 2);
168 static void atp_complete(struct urb* urb, struct pt_regs* regs)
170 int x, y, x_z, y_z, x_f, y_f;
172 struct atp *dev = urb->context;
174 switch (urb->status) {
181 /* This urb is terminated, clean up */
182 dbg("%s - urb shutting down with status: %d",
183 __FUNCTION__, urb->status);
186 dbg("%s - nonzero urb status received: %d",
187 __FUNCTION__, urb->status);
191 /* drop incomplete datasets */
192 if (dev->urb->actual_length != ATP_DATASIZE) {
193 dprintk("appletouch: incomplete data package.\n");
197 /* reorder the sensors values */
198 for (i = 0; i < 8; i++) {
200 dev->xy_cur[i ] = dev->data[5 * i + 2];
201 dev->xy_cur[i + 8] = dev->data[5 * i + 4];
202 dev->xy_cur[i + 16] = dev->data[5 * i + 42];
204 dev->xy_cur[i + 24] = dev->data[5 * i + 44];
207 dev->xy_cur[i + 26] = dev->data[5 * i + 1];
208 dev->xy_cur[i + 34] = dev->data[5 * i + 3];
211 dbg_dump("sample", dev->xy_cur);
216 dev->x_old = dev->y_old = -1;
217 memcpy(dev->xy_old, dev->xy_cur, sizeof(dev->xy_old));
219 /* 17" Powerbooks have 10 extra X sensors */
220 for (i = 16; i < ATP_XSENSORS; i++)
221 if (dev->xy_cur[i]) {
222 printk("appletouch: 17\" model detected.\n");
223 input_set_abs_params(dev->input, ABS_X, 0,
233 for (i = 0; i < ATP_XSENSORS + ATP_YSENSORS; i++) {
234 /* accumulate the change */
235 signed char change = dev->xy_old[i] - dev->xy_cur[i];
236 dev->xy_acc[i] -= change;
238 /* prevent down drifting */
239 if (dev->xy_acc[i] < 0)
243 memcpy(dev->xy_old, dev->xy_cur, sizeof(dev->xy_old));
245 dbg_dump("accumulator", dev->xy_acc);
247 x = atp_calculate_abs(dev->xy_acc, ATP_XSENSORS,
248 ATP_XFACT, &x_z, &x_f);
249 y = atp_calculate_abs(dev->xy_acc + ATP_XSENSORS, ATP_YSENSORS,
250 ATP_YFACT, &y_z, &y_f);
253 if (dev->x_old != -1) {
254 x = (dev->x_old * 3 + x) >> 2;
255 y = (dev->y_old * 3 + y) >> 2;
260 printk("appletouch: X: %3d Y: %3d "
264 input_report_key(dev->input, BTN_TOUCH, 1);
265 input_report_abs(dev->input, ABS_X, x);
266 input_report_abs(dev->input, ABS_Y, y);
267 input_report_abs(dev->input, ABS_PRESSURE,
268 min(ATP_PRESSURE, x_z + y_z));
269 atp_report_fingers(dev->input, max(x_f, y_f));
276 dev->x_old = dev->y_old = -1;
277 input_report_key(dev->input, BTN_TOUCH, 0);
278 input_report_abs(dev->input, ABS_PRESSURE, 0);
279 atp_report_fingers(dev->input, 0);
281 /* reset the accumulator on release */
282 memset(dev->xy_acc, 0, sizeof(dev->xy_acc));
285 input_report_key(dev->input, BTN_LEFT, !!dev->data[80]);
287 input_sync(dev->input);
290 retval = usb_submit_urb(dev->urb, GFP_ATOMIC);
292 err("%s - usb_submit_urb failed with result %d",
293 __FUNCTION__, retval);
297 static int atp_open(struct input_dev *input)
299 struct atp *dev = input->private;
301 if (usb_submit_urb(dev->urb, GFP_ATOMIC))
308 static void atp_close(struct input_dev *input)
310 struct atp *dev = input->private;
312 usb_kill_urb(dev->urb);
316 static int atp_probe(struct usb_interface *iface, const struct usb_device_id *id)
319 struct input_dev *input_dev;
320 struct usb_device *udev = interface_to_usbdev(iface);
321 struct usb_host_interface *iface_desc;
322 struct usb_endpoint_descriptor *endpoint;
323 int int_in_endpointAddr = 0;
324 int i, retval = -ENOMEM;
327 /* set up the endpoint information */
328 /* use only the first interrupt-in endpoint */
329 iface_desc = iface->cur_altsetting;
330 for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
331 endpoint = &iface_desc->endpoint[i].desc;
332 if (!int_in_endpointAddr &&
333 (endpoint->bEndpointAddress & USB_DIR_IN) &&
334 ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
335 == USB_ENDPOINT_XFER_INT)) {
336 /* we found an interrupt in endpoint */
337 int_in_endpointAddr = endpoint->bEndpointAddress;
341 if (!int_in_endpointAddr) {
342 err("Could not find int-in endpoint");
346 /* allocate memory for our device state and initialize it */
347 dev = kzalloc(sizeof(struct atp), GFP_KERNEL);
348 input_dev = input_allocate_device();
349 if (!dev || !input_dev) {
350 err("Out of memory");
355 dev->input = input_dev;
357 dev->urb = usb_alloc_urb(0, GFP_KERNEL);
363 dev->data = usb_buffer_alloc(dev->udev, ATP_DATASIZE, GFP_KERNEL,
364 &dev->urb->transfer_dma);
370 usb_fill_int_urb(dev->urb, udev,
371 usb_rcvintpipe(udev, int_in_endpointAddr),
372 dev->data, ATP_DATASIZE, atp_complete, dev, 1);
374 usb_make_path(udev, dev->phys, sizeof(dev->phys));
375 strlcat(dev->phys, "/input0", sizeof(dev->phys));
377 input_dev->name = "appletouch";
378 input_dev->phys = dev->phys;
379 usb_to_input_id(dev->udev, &input_dev->id);
380 input_dev->cdev.dev = &iface->dev;
382 input_dev->private = dev;
383 input_dev->open = atp_open;
384 input_dev->close = atp_close;
386 set_bit(EV_ABS, input_dev->evbit);
389 * 12" and 15" Powerbooks only have 16 x sensors,
390 * 17" models are detected later.
392 input_set_abs_params(input_dev, ABS_X, 0,
393 (16 - 1) * ATP_XFACT - 1, ATP_FUZZ, 0);
394 input_set_abs_params(input_dev, ABS_Y, 0,
395 (ATP_YSENSORS - 1) * ATP_YFACT - 1, ATP_FUZZ, 0);
396 input_set_abs_params(input_dev, ABS_PRESSURE, 0, ATP_PRESSURE, 0, 0);
398 set_bit(EV_KEY, input_dev->evbit);
399 set_bit(BTN_TOUCH, input_dev->keybit);
400 set_bit(BTN_TOOL_FINGER, input_dev->keybit);
401 set_bit(BTN_TOOL_DOUBLETAP, input_dev->keybit);
402 set_bit(BTN_TOOL_TRIPLETAP, input_dev->keybit);
403 set_bit(BTN_LEFT, input_dev->keybit);
405 input_register_device(dev->input);
407 /* save our data pointer in this interface device */
408 usb_set_intfdata(iface, dev);
413 usb_free_urb(dev->urb);
415 usb_set_intfdata(iface, NULL);
417 input_free_device(input_dev);
421 static void atp_disconnect(struct usb_interface *iface)
423 struct atp *dev = usb_get_intfdata(iface);
425 usb_set_intfdata(iface, NULL);
427 usb_kill_urb(dev->urb);
428 input_unregister_device(dev->input);
429 usb_free_urb(dev->urb);
430 usb_buffer_free(dev->udev, ATP_DATASIZE,
431 dev->data, dev->urb->transfer_dma);
434 printk(KERN_INFO "input: appletouch disconnected\n");
437 static int atp_suspend(struct usb_interface *iface, pm_message_t message)
439 struct atp *dev = usb_get_intfdata(iface);
440 usb_kill_urb(dev->urb);
445 static int atp_resume(struct usb_interface *iface)
447 struct atp *dev = usb_get_intfdata(iface);
448 if (dev->open && usb_submit_urb(dev->urb, GFP_ATOMIC))
454 static struct usb_driver atp_driver = {
455 .owner = THIS_MODULE,
456 .name = "appletouch",
458 .disconnect = atp_disconnect,
459 .suspend = atp_suspend,
460 .resume = atp_resume,
461 .id_table = atp_table,
464 static int __init atp_init(void)
466 return usb_register(&atp_driver);
469 static void __exit atp_exit(void)
471 usb_deregister(&atp_driver);
474 module_init(atp_init);
475 module_exit(atp_exit);