]>
Commit | Line | Data |
---|---|---|
f7214ff4 SP |
1 | /* |
2 | * Apple USB Touchpad (for post-February 2005 PowerBooks) driver | |
3 | * | |
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 | * | |
10 | * Thanks to Alex Harper <basilisk@foobox.net> for his inputs. | |
11 | * | |
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. | |
16 | * | |
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. | |
21 | * | |
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. | |
25 | * | |
26 | */ | |
27 | ||
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> | |
37 | ||
38 | /* Apple has powerbooks which have the keyboard with different Product IDs */ | |
39 | #define APPLE_VENDOR_ID 0x05AC | |
40 | ||
41 | #define ATP_DEVICE(prod) \ | |
c5b7c7c3 | 42 | .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \ |
f7214ff4 SP |
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 | |
49 | ||
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 */ | |
57 | }; | |
58 | MODULE_DEVICE_TABLE (usb, atp_table); | |
59 | ||
60 | /* size of a USB urb transfer */ | |
61 | #define ATP_DATASIZE 81 | |
62 | ||
63 | /* | |
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 | |
66 | * (vertical) sensors. | |
67 | */ | |
68 | #define ATP_XSENSORS 26 | |
69 | #define ATP_YSENSORS 16 | |
70 | ||
71 | /* amount of fuzz this touchpad generates */ | |
72 | #define ATP_FUZZ 16 | |
73 | ||
74 | /* maximum pressure this driver will report */ | |
75 | #define ATP_PRESSURE 300 | |
76 | /* | |
77 | * multiplication factor for the X and Y coordinates. | |
78 | * We try to keep the touchpad aspect ratio while still doing only simple | |
79 | * arithmetics. | |
80 | * The factors below give coordinates like: | |
c5b7c7c3 DT |
81 | * 0 <= x < 960 on 12" and 15" Powerbooks |
82 | * 0 <= x < 1600 on 17" Powerbooks | |
83 | * 0 <= y < 646 | |
f7214ff4 SP |
84 | */ |
85 | #define ATP_XFACT 64 | |
86 | #define ATP_YFACT 43 | |
87 | ||
88 | /* | |
89 | * Threshold for the touchpad sensors. Any change less than ATP_THRESHOLD is | |
90 | * ignored. | |
91 | */ | |
92 | #define ATP_THRESHOLD 5 | |
93 | ||
94 | /* Structure to hold all of our device specific stuff */ | |
95 | struct atp { | |
c5b7c7c3 | 96 | char phys[64]; |
f7214ff4 SP |
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 */ | |
c5b7c7c3 | 101 | struct input_dev *input; /* input dev */ |
f7214ff4 SP |
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]; | |
111 | }; | |
112 | ||
113 | #define dbg_dump(msg, tab) \ | |
114 | if (debug > 1) { \ | |
115 | int i; \ | |
116 | printk("appletouch: %s %lld", msg, (long long)jiffies); \ | |
117 | for (i = 0; i < ATP_XSENSORS + ATP_YSENSORS; i++) \ | |
c5b7c7c3 DT |
118 | printk(" %02x", tab[i]); \ |
119 | printk("\n"); \ | |
f7214ff4 SP |
120 | } |
121 | ||
c5b7c7c3 | 122 | #define dprintk(format, a...) \ |
f7214ff4 SP |
123 | do { \ |
124 | if (debug) printk(format, ##a); \ | |
125 | } while (0) | |
126 | ||
127 | MODULE_AUTHOR("Johannes Berg, Stelian Pop, Frank Arnold"); | |
128 | MODULE_DESCRIPTION("Apple PowerBooks USB touchpad driver"); | |
129 | MODULE_LICENSE("GPL"); | |
130 | ||
131 | static int debug = 1; | |
132 | module_param(debug, int, 0644); | |
133 | MODULE_PARM_DESC(debug, "Activate debugging output"); | |
134 | ||
135 | static int atp_calculate_abs(int *xy_sensors, int nb_sensors, int fact, | |
136 | int *z, int *fingers) | |
137 | { | |
138 | int i; | |
139 | /* values to calculate mean */ | |
140 | int pcum = 0, psum = 0; | |
141 | ||
142 | *fingers = 0; | |
143 | ||
144 | for (i = 0; i < nb_sensors; i++) { | |
145 | if (xy_sensors[i] < ATP_THRESHOLD) | |
146 | continue; | |
147 | if ((i - 1 < 0) || (xy_sensors[i - 1] < ATP_THRESHOLD)) | |
148 | (*fingers)++; | |
149 | pcum += xy_sensors[i] * i; | |
150 | psum += xy_sensors[i]; | |
151 | } | |
152 | ||
153 | if (psum > 0) { | |
154 | *z = psum; | |
155 | return pcum * fact / psum; | |
156 | } | |
157 | ||
158 | return 0; | |
159 | } | |
160 | ||
161 | static inline void atp_report_fingers(struct input_dev *input, int fingers) | |
162 | { | |
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); | |
166 | } | |
167 | ||
168 | static void atp_complete(struct urb* urb, struct pt_regs* regs) | |
169 | { | |
170 | int x, y, x_z, y_z, x_f, y_f; | |
171 | int retval, i; | |
172 | struct atp *dev = urb->context; | |
173 | ||
174 | switch (urb->status) { | |
175 | case 0: | |
176 | /* success */ | |
177 | break; | |
178 | case -ECONNRESET: | |
179 | case -ENOENT: | |
180 | case -ESHUTDOWN: | |
181 | /* This urb is terminated, clean up */ | |
182 | dbg("%s - urb shutting down with status: %d", | |
183 | __FUNCTION__, urb->status); | |
184 | return; | |
185 | default: | |
186 | dbg("%s - nonzero urb status received: %d", | |
187 | __FUNCTION__, urb->status); | |
188 | goto exit; | |
189 | } | |
190 | ||
191 | /* drop incomplete datasets */ | |
192 | if (dev->urb->actual_length != ATP_DATASIZE) { | |
193 | dprintk("appletouch: incomplete data package.\n"); | |
194 | goto exit; | |
195 | } | |
196 | ||
197 | /* reorder the sensors values */ | |
198 | for (i = 0; i < 8; i++) { | |
199 | /* X values */ | |
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]; | |
203 | if (i < 2) | |
204 | dev->xy_cur[i + 24] = dev->data[5 * i + 44]; | |
205 | ||
206 | /* Y values */ | |
207 | dev->xy_cur[i + 26] = dev->data[5 * i + 1]; | |
208 | dev->xy_cur[i + 34] = dev->data[5 * i + 3]; | |
209 | } | |
210 | ||
211 | dbg_dump("sample", dev->xy_cur); | |
212 | ||
213 | if (!dev->valid) { | |
214 | /* first sample */ | |
215 | dev->valid = 1; | |
216 | dev->x_old = dev->y_old = -1; | |
217 | memcpy(dev->xy_old, dev->xy_cur, sizeof(dev->xy_old)); | |
218 | ||
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"); | |
c5b7c7c3 DT |
223 | input_set_abs_params(dev->input, ABS_X, 0, |
224 | (ATP_XSENSORS - 1) * | |
f7214ff4 SP |
225 | ATP_XFACT - 1, |
226 | ATP_FUZZ, 0); | |
227 | break; | |
228 | } | |
229 | ||
230 | goto exit; | |
231 | } | |
232 | ||
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; | |
237 | ||
238 | /* prevent down drifting */ | |
239 | if (dev->xy_acc[i] < 0) | |
240 | dev->xy_acc[i] = 0; | |
241 | } | |
242 | ||
243 | memcpy(dev->xy_old, dev->xy_cur, sizeof(dev->xy_old)); | |
244 | ||
245 | dbg_dump("accumulator", dev->xy_acc); | |
246 | ||
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); | |
251 | ||
252 | if (x && y) { | |
253 | if (dev->x_old != -1) { | |
254 | x = (dev->x_old * 3 + x) >> 2; | |
255 | y = (dev->y_old * 3 + y) >> 2; | |
256 | dev->x_old = x; | |
257 | dev->y_old = y; | |
258 | ||
259 | if (debug > 1) | |
260 | printk("appletouch: X: %3d Y: %3d " | |
261 | "Xz: %3d Yz: %3d\n", | |
262 | x, y, x_z, y_z); | |
263 | ||
c5b7c7c3 DT |
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, | |
f7214ff4 | 268 | min(ATP_PRESSURE, x_z + y_z)); |
c5b7c7c3 | 269 | atp_report_fingers(dev->input, max(x_f, y_f)); |
f7214ff4 SP |
270 | } |
271 | dev->x_old = x; | |
272 | dev->y_old = y; | |
273 | } | |
274 | else if (!x && !y) { | |
275 | ||
276 | dev->x_old = dev->y_old = -1; | |
c5b7c7c3 DT |
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); | |
f7214ff4 SP |
280 | |
281 | /* reset the accumulator on release */ | |
282 | memset(dev->xy_acc, 0, sizeof(dev->xy_acc)); | |
283 | } | |
284 | ||
c5b7c7c3 | 285 | input_report_key(dev->input, BTN_LEFT, !!dev->data[80]); |
f7214ff4 | 286 | |
c5b7c7c3 | 287 | input_sync(dev->input); |
f7214ff4 SP |
288 | |
289 | exit: | |
290 | retval = usb_submit_urb(dev->urb, GFP_ATOMIC); | |
291 | if (retval) { | |
292 | err("%s - usb_submit_urb failed with result %d", | |
293 | __FUNCTION__, retval); | |
294 | } | |
295 | } | |
296 | ||
297 | static int atp_open(struct input_dev *input) | |
298 | { | |
299 | struct atp *dev = input->private; | |
300 | ||
301 | if (usb_submit_urb(dev->urb, GFP_ATOMIC)) | |
302 | return -EIO; | |
303 | ||
304 | dev->open = 1; | |
305 | return 0; | |
306 | } | |
307 | ||
308 | static void atp_close(struct input_dev *input) | |
309 | { | |
310 | struct atp *dev = input->private; | |
311 | ||
312 | usb_kill_urb(dev->urb); | |
313 | dev->open = 0; | |
314 | } | |
315 | ||
316 | static int atp_probe(struct usb_interface *iface, const struct usb_device_id *id) | |
317 | { | |
c5b7c7c3 DT |
318 | struct atp *dev; |
319 | struct input_dev *input_dev; | |
320 | struct usb_device *udev = interface_to_usbdev(iface); | |
f7214ff4 SP |
321 | struct usb_host_interface *iface_desc; |
322 | struct usb_endpoint_descriptor *endpoint; | |
323 | int int_in_endpointAddr = 0; | |
324 | int i, retval = -ENOMEM; | |
325 | ||
f7214ff4 SP |
326 | |
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; | |
338 | break; | |
339 | } | |
340 | } | |
341 | if (!int_in_endpointAddr) { | |
f7214ff4 | 342 | err("Could not find int-in endpoint"); |
c5b7c7c3 | 343 | return -EIO; |
f7214ff4 SP |
344 | } |
345 | ||
c5b7c7c3 DT |
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"); | |
351 | goto err_free_devs; | |
352 | } | |
353 | ||
354 | dev->udev = udev; | |
355 | dev->input = input_dev; | |
f7214ff4 SP |
356 | |
357 | dev->urb = usb_alloc_urb(0, GFP_KERNEL); | |
358 | if (!dev->urb) { | |
359 | retval = -ENOMEM; | |
c5b7c7c3 | 360 | goto err_free_devs; |
f7214ff4 | 361 | } |
c5b7c7c3 | 362 | |
f7214ff4 SP |
363 | dev->data = usb_buffer_alloc(dev->udev, ATP_DATASIZE, GFP_KERNEL, |
364 | &dev->urb->transfer_dma); | |
365 | if (!dev->data) { | |
366 | retval = -ENOMEM; | |
c5b7c7c3 | 367 | goto err_free_urb; |
f7214ff4 | 368 | } |
c5b7c7c3 DT |
369 | |
370 | usb_fill_int_urb(dev->urb, udev, | |
371 | usb_rcvintpipe(udev, int_in_endpointAddr), | |
f7214ff4 SP |
372 | dev->data, ATP_DATASIZE, atp_complete, dev, 1); |
373 | ||
c5b7c7c3 DT |
374 | usb_make_path(udev, dev->phys, sizeof(dev->phys)); |
375 | strlcat(dev->phys, "/input0", sizeof(dev->phys)); | |
376 | ||
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; | |
f7214ff4 | 381 | |
c5b7c7c3 DT |
382 | input_dev->private = dev; |
383 | input_dev->open = atp_open; | |
384 | input_dev->close = atp_close; | |
f7214ff4 | 385 | |
c5b7c7c3 | 386 | set_bit(EV_ABS, input_dev->evbit); |
f7214ff4 SP |
387 | |
388 | /* | |
389 | * 12" and 15" Powerbooks only have 16 x sensors, | |
390 | * 17" models are detected later. | |
391 | */ | |
c5b7c7c3 | 392 | input_set_abs_params(input_dev, ABS_X, 0, |
f7214ff4 | 393 | (16 - 1) * ATP_XFACT - 1, ATP_FUZZ, 0); |
c5b7c7c3 | 394 | input_set_abs_params(input_dev, ABS_Y, 0, |
f7214ff4 | 395 | (ATP_YSENSORS - 1) * ATP_YFACT - 1, ATP_FUZZ, 0); |
c5b7c7c3 | 396 | input_set_abs_params(input_dev, ABS_PRESSURE, 0, ATP_PRESSURE, 0, 0); |
f7214ff4 | 397 | |
c5b7c7c3 DT |
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); | |
f7214ff4 | 404 | |
c5b7c7c3 | 405 | input_register_device(dev->input); |
f7214ff4 | 406 | |
c5b7c7c3 DT |
407 | /* save our data pointer in this interface device */ |
408 | usb_set_intfdata(iface, dev); | |
f7214ff4 SP |
409 | |
410 | return 0; | |
411 | ||
c5b7c7c3 | 412 | err_free_urb: |
f7214ff4 | 413 | usb_free_urb(dev->urb); |
c5b7c7c3 | 414 | err_free_devs: |
f7214ff4 | 415 | usb_set_intfdata(iface, NULL); |
f7214ff4 | 416 | kfree(dev); |
c5b7c7c3 | 417 | input_free_device(input_dev); |
f7214ff4 SP |
418 | return retval; |
419 | } | |
420 | ||
421 | static void atp_disconnect(struct usb_interface *iface) | |
422 | { | |
423 | struct atp *dev = usb_get_intfdata(iface); | |
424 | ||
425 | usb_set_intfdata(iface, NULL); | |
426 | if (dev) { | |
427 | usb_kill_urb(dev->urb); | |
c5b7c7c3 | 428 | input_unregister_device(dev->input); |
f7214ff4 SP |
429 | usb_free_urb(dev->urb); |
430 | usb_buffer_free(dev->udev, ATP_DATASIZE, | |
431 | dev->data, dev->urb->transfer_dma); | |
432 | kfree(dev); | |
433 | } | |
434 | printk(KERN_INFO "input: appletouch disconnected\n"); | |
435 | } | |
436 | ||
437 | static int atp_suspend(struct usb_interface *iface, pm_message_t message) | |
438 | { | |
439 | struct atp *dev = usb_get_intfdata(iface); | |
440 | usb_kill_urb(dev->urb); | |
441 | dev->valid = 0; | |
442 | return 0; | |
443 | } | |
444 | ||
445 | static int atp_resume(struct usb_interface *iface) | |
446 | { | |
447 | struct atp *dev = usb_get_intfdata(iface); | |
448 | if (dev->open && usb_submit_urb(dev->urb, GFP_ATOMIC)) | |
449 | return -EIO; | |
450 | ||
451 | return 0; | |
452 | } | |
453 | ||
454 | static struct usb_driver atp_driver = { | |
455 | .owner = THIS_MODULE, | |
456 | .name = "appletouch", | |
457 | .probe = atp_probe, | |
458 | .disconnect = atp_disconnect, | |
459 | .suspend = atp_suspend, | |
460 | .resume = atp_resume, | |
461 | .id_table = atp_table, | |
462 | }; | |
463 | ||
464 | static int __init atp_init(void) | |
465 | { | |
466 | return usb_register(&atp_driver); | |
467 | } | |
468 | ||
469 | static void __exit atp_exit(void) | |
470 | { | |
471 | usb_deregister(&atp_driver); | |
472 | } | |
473 | ||
474 | module_init(atp_init); | |
475 | module_exit(atp_exit); |