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>
37 /* Apple has powerbooks which have the keyboard with different Product IDs */
38 #define APPLE_VENDOR_ID 0x05AC
39 #define ATP_12INCH_ID1 0x030A
40 #define ATP_15INCH_ID1 0x020E
41 #define ATP_15INCH_ID2 0x020F
42 #define ATP_17INCH_ID1 0xFFFF /* XXX need a tester !!! */
44 #define ATP_DRIVER_VERSION 0x0007 /* 00.07 */
46 #define ATP_DEVICE(prod) \
47 .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \
48 USB_DEVICE_ID_MATCH_INT_CLASS | \
49 USB_DEVICE_ID_MATCH_INT_PROTOCOL, \
50 .idVendor = APPLE_VENDOR_ID, \
51 .idProduct = (prod), \
52 .bInterfaceClass = 0x03, \
53 .bInterfaceProtocol = 0x02
55 /* table of devices that work with this driver */
56 static struct usb_device_id atp_table [] = {
57 { ATP_DEVICE(ATP_12INCH_ID1) },
58 { ATP_DEVICE(ATP_15INCH_ID1) },
59 { ATP_DEVICE(ATP_15INCH_ID2) },
61 Disabled until someone gives us the real USB id and tests the driver
62 { ATP_DEVICE(ATP_17INCH_ID1) },
64 { } /* Terminating entry */
66 MODULE_DEVICE_TABLE (usb, atp_table);
68 /* size of a USB urb transfer */
69 #define ATP_DATASIZE 81
72 * number of sensors. Note that only 16 instead of 26 X (horizontal)
73 * sensors exist on 12" and 15" PowerBooks. All models have 16 Y
76 #define ATP_XSENSORS 26
77 #define ATP_YSENSORS 16
79 /* amount of fuzz this touchpad generates */
82 /* maximum pressure this driver will report */
83 #define ATP_PRESSURE 300
85 * multiplication factor for the X and Y coordinates.
86 * We try to keep the touchpad aspect ratio while still doing only simple
88 * The factors below give coordinates like:
89 * 0 <= x < 960 on 12" and 15" Powerbooks
90 * 0 <= x < 1600 on 17" Powerbooks
97 * Threshold for the touchpad sensors. Any change less than ATP_THRESHOLD is
100 #define ATP_THRESHOLD 5
102 /* Structure to hold all of our device specific stuff */
104 struct usb_device * udev; /* usb device */
105 struct urb * urb; /* usb request block */
106 signed char * data; /* transferred data */
107 int open; /* non-zero if opened */
108 struct input_dev input; /* input dev */
109 int valid; /* are the sensors valid ? */
110 int x_old; /* last reported x/y, */
111 int y_old; /* used for smoothing */
112 /* current value of the sensors */
113 signed char xy_cur[ATP_XSENSORS + ATP_YSENSORS];
114 /* last value of the sensors */
115 signed char xy_old[ATP_XSENSORS + ATP_YSENSORS];
116 /* accumulated sensors */
117 int xy_acc[ATP_XSENSORS + ATP_YSENSORS];
120 #define dbg_dump(msg, tab) \
123 printk("appletouch: %s %lld", msg, (long long)jiffies); \
124 for (i = 0; i < ATP_XSENSORS + ATP_YSENSORS; i++) \
125 printk("%02x ", tab[i]); \
129 #define dprintk(format, a...) \
131 if (debug) printk(format, ##a); \
134 MODULE_AUTHOR("Johannes Berg, Stelian Pop, Frank Arnold");
135 MODULE_DESCRIPTION("Apple PowerBooks USB touchpad driver");
136 MODULE_LICENSE("GPL");
138 static int debug = 1;
139 module_param(debug, int, 0644);
140 MODULE_PARM_DESC(debug, "Activate debugging output");
142 static int atp_calculate_abs(int *xy_sensors, int nb_sensors, int fact,
143 int *z, int *fingers)
146 /* values to calculate mean */
147 int pcum = 0, psum = 0;
151 for (i = 0; i < nb_sensors; i++) {
152 if (xy_sensors[i] < ATP_THRESHOLD)
154 if ((i - 1 < 0) || (xy_sensors[i - 1] < ATP_THRESHOLD))
156 pcum += xy_sensors[i] * i;
157 psum += xy_sensors[i];
162 return pcum * fact / psum;
168 static inline void atp_report_fingers(struct input_dev *input, int fingers)
170 input_report_key(input, BTN_TOOL_FINGER, fingers == 1);
171 input_report_key(input, BTN_TOOL_DOUBLETAP, fingers == 2);
172 input_report_key(input, BTN_TOOL_TRIPLETAP, fingers > 2);
175 static void atp_complete(struct urb* urb, struct pt_regs* regs)
177 int x, y, x_z, y_z, x_f, y_f;
179 struct atp *dev = urb->context;
181 switch (urb->status) {
188 /* This urb is terminated, clean up */
189 dbg("%s - urb shutting down with status: %d",
190 __FUNCTION__, urb->status);
193 dbg("%s - nonzero urb status received: %d",
194 __FUNCTION__, urb->status);
198 /* drop incomplete datasets */
199 if (dev->urb->actual_length != ATP_DATASIZE) {
200 dprintk("appletouch: incomplete data package.\n");
204 /* reorder the sensors values */
205 for (i = 0; i < 8; i++) {
207 dev->xy_cur[i ] = dev->data[5 * i + 2];
208 dev->xy_cur[i + 8] = dev->data[5 * i + 4];
209 dev->xy_cur[i + 16] = dev->data[5 * i + 42];
211 dev->xy_cur[i + 24] = dev->data[5 * i + 44];
214 dev->xy_cur[i + 26] = dev->data[5 * i + 1];
215 dev->xy_cur[i + 34] = dev->data[5 * i + 3];
218 dbg_dump("sample", dev->xy_cur);
223 dev->x_old = dev->y_old = -1;
224 memcpy(dev->xy_old, dev->xy_cur, sizeof(dev->xy_old));
228 for (i = 0; i < ATP_XSENSORS + ATP_YSENSORS; i++) {
229 /* accumulate the change */
230 signed char change = dev->xy_old[i] - dev->xy_cur[i];
231 dev->xy_acc[i] -= change;
233 /* prevent down drifting */
234 if (dev->xy_acc[i] < 0)
238 memcpy(dev->xy_old, dev->xy_cur, sizeof(dev->xy_old));
240 dbg_dump("accumulator", dev->xy_acc);
242 x = atp_calculate_abs(dev->xy_acc, ATP_XSENSORS,
243 ATP_XFACT, &x_z, &x_f);
244 y = atp_calculate_abs(dev->xy_acc + ATP_XSENSORS, ATP_YSENSORS,
245 ATP_YFACT, &y_z, &y_f);
248 if (dev->x_old != -1) {
249 x = (dev->x_old * 3 + x) >> 2;
250 y = (dev->y_old * 3 + y) >> 2;
255 printk("appletouch: X: %3d Y: %3d "
259 input_report_key(&dev->input, BTN_TOUCH, 1);
260 input_report_abs(&dev->input, ABS_X, x);
261 input_report_abs(&dev->input, ABS_Y, y);
262 input_report_abs(&dev->input, ABS_PRESSURE,
263 min(ATP_PRESSURE, x_z + y_z));
264 atp_report_fingers(&dev->input, max(x_f, y_f));
271 dev->x_old = dev->y_old = -1;
272 input_report_key(&dev->input, BTN_TOUCH, 0);
273 input_report_abs(&dev->input, ABS_PRESSURE, 0);
274 atp_report_fingers(&dev->input, 0);
276 /* reset the accumulator on release */
277 memset(dev->xy_acc, 0, sizeof(dev->xy_acc));
280 input_report_key(&dev->input, BTN_LEFT, !!dev->data[80]);
282 input_sync(&dev->input);
285 retval = usb_submit_urb(dev->urb, GFP_ATOMIC);
287 err("%s - usb_submit_urb failed with result %d",
288 __FUNCTION__, retval);
292 static int atp_open(struct input_dev *input)
294 struct atp *dev = input->private;
296 if (usb_submit_urb(dev->urb, GFP_ATOMIC))
303 static void atp_close(struct input_dev *input)
305 struct atp *dev = input->private;
307 usb_kill_urb(dev->urb);
311 static int atp_probe(struct usb_interface *iface, const struct usb_device_id *id)
313 struct atp *dev = NULL;
314 struct usb_host_interface *iface_desc;
315 struct usb_endpoint_descriptor *endpoint;
316 int int_in_endpointAddr = 0;
317 int i, retval = -ENOMEM;
319 /* allocate memory for our device state and initialize it */
320 dev = kmalloc(sizeof(struct atp), GFP_KERNEL);
322 err("Out of memory");
325 memset(dev, 0, sizeof(struct atp));
327 dev->udev = interface_to_usbdev(iface);
329 /* set up the endpoint information */
330 /* use only the first interrupt-in endpoint */
331 iface_desc = iface->cur_altsetting;
332 for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
333 endpoint = &iface_desc->endpoint[i].desc;
334 if (!int_in_endpointAddr &&
335 (endpoint->bEndpointAddress & USB_DIR_IN) &&
336 ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
337 == USB_ENDPOINT_XFER_INT)) {
338 /* we found an interrupt in endpoint */
339 int_in_endpointAddr = endpoint->bEndpointAddress;
343 if (!int_in_endpointAddr) {
345 err("Could not find int-in endpoint");
349 /* save our data pointer in this interface device */
350 usb_set_intfdata(iface, dev);
352 dev->urb = usb_alloc_urb(0, GFP_KERNEL);
357 dev->data = usb_buffer_alloc(dev->udev, ATP_DATASIZE, GFP_KERNEL,
358 &dev->urb->transfer_dma);
361 goto err_usbbufalloc;
363 usb_fill_int_urb(dev->urb, dev->udev,
364 usb_rcvintpipe(dev->udev, int_in_endpointAddr),
365 dev->data, ATP_DATASIZE, atp_complete, dev, 1);
367 init_input_dev(&dev->input);
368 dev->input.name = "appletouch";
369 dev->input.dev = &iface->dev;
370 dev->input.private = dev;
371 dev->input.open = atp_open;
372 dev->input.close = atp_close;
374 dev->input.id.bustype = BUS_USB;
375 dev->input.id.vendor = id->idVendor;
376 dev->input.id.product = id->idProduct;
377 dev->input.id.version = ATP_DRIVER_VERSION;
379 set_bit(EV_ABS, dev->input.evbit);
380 if (id->idProduct == ATP_17INCH_ID1)
381 input_set_abs_params(&dev->input, ABS_X, 0,
382 (ATP_XSENSORS - 1) * ATP_XFACT - 1,
385 /* 12" and 15" Powerbooks only have 16 x sensors */
386 input_set_abs_params(&dev->input, ABS_X, 0,
387 (16 - 1) * ATP_XFACT - 1,
389 input_set_abs_params(&dev->input, ABS_Y, 0,
390 (ATP_YSENSORS - 1) * ATP_YFACT - 1,
392 input_set_abs_params(&dev->input, ABS_PRESSURE, 0, ATP_PRESSURE, 0, 0);
394 set_bit(EV_KEY, dev->input.evbit);
395 set_bit(BTN_TOUCH, dev->input.keybit);
396 set_bit(BTN_TOOL_FINGER, dev->input.keybit);
397 set_bit(BTN_TOOL_DOUBLETAP, dev->input.keybit);
398 set_bit(BTN_TOOL_TRIPLETAP, dev->input.keybit);
399 set_bit(BTN_LEFT, dev->input.keybit);
401 input_register_device(&dev->input);
403 printk(KERN_INFO "input: appletouch connected\n");
408 usb_free_urb(dev->urb);
410 usb_set_intfdata(iface, NULL);
417 static void atp_disconnect(struct usb_interface *iface)
419 struct atp *dev = usb_get_intfdata(iface);
421 usb_set_intfdata(iface, NULL);
423 usb_kill_urb(dev->urb);
424 input_unregister_device(&dev->input);
425 usb_free_urb(dev->urb);
426 usb_buffer_free(dev->udev, ATP_DATASIZE,
427 dev->data, dev->urb->transfer_dma);
430 printk(KERN_INFO "input: appletouch disconnected\n");
433 static int atp_suspend(struct usb_interface *iface, pm_message_t message)
435 struct atp *dev = usb_get_intfdata(iface);
436 usb_kill_urb(dev->urb);
441 static int atp_resume(struct usb_interface *iface)
443 struct atp *dev = usb_get_intfdata(iface);
444 if (dev->open && usb_submit_urb(dev->urb, GFP_ATOMIC))
450 static struct usb_driver atp_driver = {
451 .owner = THIS_MODULE,
452 .name = "appletouch",
454 .disconnect = atp_disconnect,
455 .suspend = atp_suspend,
456 .resume = atp_resume,
457 .id_table = atp_table,
460 static int __init atp_init(void)
462 return usb_register(&atp_driver);
465 static void __exit atp_exit(void)
467 usb_deregister(&atp_driver);
470 module_init(atp_init);
471 module_exit(atp_exit);