Commit | Line | Data |
---|---|---|
f7214ff4 | 1 | /* |
9effa978 | 2 | * Apple USB Touchpad (for post-February 2005 PowerBooks and MacBooks) driver |
f7214ff4 SP |
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) | |
e1e02c9f | 9 | * Copyright (C) 2005 Michael Hanselmann (linux-kernel@hansmi.ch) |
9effa978 | 10 | * Copyright (C) 2006 Nicolas Boichat (nicolas@boichat.ch) |
f7214ff4 SP |
11 | * |
12 | * Thanks to Alex Harper <basilisk@foobox.net> for his inputs. | |
13 | * | |
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. | |
18 | * | |
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. | |
23 | * | |
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. | |
27 | * | |
28 | */ | |
29 | ||
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> | |
39 | ||
40 | /* Apple has powerbooks which have the keyboard with different Product IDs */ | |
41 | #define APPLE_VENDOR_ID 0x05AC | |
42 | ||
e1e02c9f MH |
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 | |
47 | ||
9effa978 NB |
48 | /* MacBook devices */ |
49 | #define GEYSER3_ANSI_PRODUCT_ID 0x0217 | |
50 | #define GEYSER3_ISO_PRODUCT_ID 0x0218 | |
51 | #define GEYSER3_JIS_PRODUCT_ID 0x0219 | |
52 | ||
f7214ff4 | 53 | #define ATP_DEVICE(prod) \ |
c5b7c7c3 | 54 | .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \ |
f7214ff4 SP |
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 | |
61 | ||
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) }, | |
e1e02c9f MH |
68 | |
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) }, | |
73 | ||
9effa978 NB |
74 | { ATP_DEVICE(GEYSER3_ANSI_PRODUCT_ID) }, |
75 | { ATP_DEVICE(GEYSER3_ISO_PRODUCT_ID) }, | |
76 | { ATP_DEVICE(GEYSER3_JIS_PRODUCT_ID) }, | |
77 | ||
e1e02c9f MH |
78 | /* Terminating entry */ |
79 | { } | |
f7214ff4 SP |
80 | }; |
81 | MODULE_DEVICE_TABLE (usb, atp_table); | |
82 | ||
f7214ff4 SP |
83 | /* |
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 | |
86 | * (vertical) sensors. | |
87 | */ | |
88 | #define ATP_XSENSORS 26 | |
89 | #define ATP_YSENSORS 16 | |
90 | ||
91 | /* amount of fuzz this touchpad generates */ | |
92 | #define ATP_FUZZ 16 | |
93 | ||
94 | /* maximum pressure this driver will report */ | |
95 | #define ATP_PRESSURE 300 | |
96 | /* | |
97 | * multiplication factor for the X and Y coordinates. | |
98 | * We try to keep the touchpad aspect ratio while still doing only simple | |
99 | * arithmetics. | |
100 | * The factors below give coordinates like: | |
c5b7c7c3 DT |
101 | * 0 <= x < 960 on 12" and 15" Powerbooks |
102 | * 0 <= x < 1600 on 17" Powerbooks | |
103 | * 0 <= y < 646 | |
f7214ff4 SP |
104 | */ |
105 | #define ATP_XFACT 64 | |
106 | #define ATP_YFACT 43 | |
107 | ||
108 | /* | |
109 | * Threshold for the touchpad sensors. Any change less than ATP_THRESHOLD is | |
110 | * ignored. | |
111 | */ | |
112 | #define ATP_THRESHOLD 5 | |
113 | ||
9effa978 NB |
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 | |
120 | ||
f7214ff4 SP |
121 | /* Structure to hold all of our device specific stuff */ |
122 | struct atp { | |
c5b7c7c3 | 123 | char phys[64]; |
f7214ff4 SP |
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 */ | |
c5b7c7c3 | 128 | struct input_dev *input; /* input dev */ |
f7214ff4 SP |
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]; | |
e1e02c9f MH |
138 | int overflowwarn; /* overflow warning printed? */ |
139 | int datalen; /* size of an USB urb transfer */ | |
f7214ff4 SP |
140 | }; |
141 | ||
142 | #define dbg_dump(msg, tab) \ | |
143 | if (debug > 1) { \ | |
144 | int i; \ | |
145 | printk("appletouch: %s %lld", msg, (long long)jiffies); \ | |
146 | for (i = 0; i < ATP_XSENSORS + ATP_YSENSORS; i++) \ | |
c5b7c7c3 DT |
147 | printk(" %02x", tab[i]); \ |
148 | printk("\n"); \ | |
f7214ff4 SP |
149 | } |
150 | ||
c5b7c7c3 | 151 | #define dprintk(format, a...) \ |
f7214ff4 SP |
152 | do { \ |
153 | if (debug) printk(format, ##a); \ | |
154 | } while (0) | |
155 | ||
e1e02c9f | 156 | MODULE_AUTHOR("Johannes Berg, Stelian Pop, Frank Arnold, Michael Hanselmann"); |
f7214ff4 SP |
157 | MODULE_DESCRIPTION("Apple PowerBooks USB touchpad driver"); |
158 | MODULE_LICENSE("GPL"); | |
159 | ||
160 | static int debug = 1; | |
161 | module_param(debug, int, 0644); | |
162 | MODULE_PARM_DESC(debug, "Activate debugging output"); | |
163 | ||
e1e02c9f MH |
164 | /* Checks if the device a Geyser 2 (ANSI, ISO, JIS) */ |
165 | static inline int atp_is_geyser_2(struct atp *dev) | |
166 | { | |
9effa978 | 167 | u16 productId = le16_to_cpu(dev->udev->descriptor.idProduct); |
e1e02c9f MH |
168 | |
169 | return (productId == GEYSER_ANSI_PRODUCT_ID) || | |
170 | (productId == GEYSER_ISO_PRODUCT_ID) || | |
171 | (productId == GEYSER_JIS_PRODUCT_ID); | |
172 | } | |
173 | ||
9effa978 NB |
174 | static inline int atp_is_geyser_3(struct atp *dev) |
175 | { | |
176 | u16 productId = le16_to_cpu(dev->udev->descriptor.idProduct); | |
177 | ||
178 | return (productId == GEYSER3_ANSI_PRODUCT_ID) || | |
179 | (productId == GEYSER3_ISO_PRODUCT_ID) || | |
180 | (productId == GEYSER3_JIS_PRODUCT_ID); | |
181 | } | |
182 | ||
f7214ff4 SP |
183 | static int atp_calculate_abs(int *xy_sensors, int nb_sensors, int fact, |
184 | int *z, int *fingers) | |
185 | { | |
186 | int i; | |
187 | /* values to calculate mean */ | |
188 | int pcum = 0, psum = 0; | |
189 | ||
190 | *fingers = 0; | |
191 | ||
192 | for (i = 0; i < nb_sensors; i++) { | |
193 | if (xy_sensors[i] < ATP_THRESHOLD) | |
194 | continue; | |
195 | if ((i - 1 < 0) || (xy_sensors[i - 1] < ATP_THRESHOLD)) | |
196 | (*fingers)++; | |
197 | pcum += xy_sensors[i] * i; | |
198 | psum += xy_sensors[i]; | |
199 | } | |
200 | ||
201 | if (psum > 0) { | |
202 | *z = psum; | |
203 | return pcum * fact / psum; | |
204 | } | |
205 | ||
206 | return 0; | |
207 | } | |
208 | ||
209 | static inline void atp_report_fingers(struct input_dev *input, int fingers) | |
210 | { | |
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); | |
214 | } | |
215 | ||
216 | static void atp_complete(struct urb* urb, struct pt_regs* regs) | |
217 | { | |
218 | int x, y, x_z, y_z, x_f, y_f; | |
e1e02c9f | 219 | int retval, i, j; |
f7214ff4 SP |
220 | struct atp *dev = urb->context; |
221 | ||
222 | switch (urb->status) { | |
223 | case 0: | |
224 | /* success */ | |
225 | break; | |
e1e02c9f MH |
226 | case -EOVERFLOW: |
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; | |
232 | } | |
f7214ff4 SP |
233 | case -ECONNRESET: |
234 | case -ENOENT: | |
235 | case -ESHUTDOWN: | |
236 | /* This urb is terminated, clean up */ | |
237 | dbg("%s - urb shutting down with status: %d", | |
238 | __FUNCTION__, urb->status); | |
239 | return; | |
240 | default: | |
241 | dbg("%s - nonzero urb status received: %d", | |
242 | __FUNCTION__, urb->status); | |
243 | goto exit; | |
244 | } | |
245 | ||
246 | /* drop incomplete datasets */ | |
e1e02c9f | 247 | if (dev->urb->actual_length != dev->datalen) { |
9effa978 NB |
248 | dprintk("appletouch: incomplete data package" |
249 | " (first byte: %d, length: %d).\n", | |
250 | dev->data[0], dev->urb->actual_length); | |
f7214ff4 SP |
251 | goto exit; |
252 | } | |
253 | ||
254 | /* reorder the sensors values */ | |
9effa978 NB |
255 | if (atp_is_geyser_3(dev)) { |
256 | memset(dev->xy_cur, 0, sizeof(dev->xy_cur)); | |
257 | ||
258 | /* | |
259 | * The values are laid out like this: | |
260 | * -, Y1, Y2, -, Y3, Y4, -, ..., -, X1, X2, -, X3, X4, ... | |
261 | * '-' is an unused value. | |
262 | */ | |
263 | ||
264 | /* read X values */ | |
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]; | |
268 | } | |
269 | /* read Y values */ | |
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]; | |
273 | } | |
274 | } else if (atp_is_geyser_2(dev)) { | |
e1e02c9f MH |
275 | memset(dev->xy_cur, 0, sizeof(dev->xy_cur)); |
276 | ||
277 | /* | |
278 | * The values are laid out like this: | |
279 | * Y1, Y2, -, Y3, Y4, -, ..., X1, X2, -, X3, X4, -, ... | |
280 | * '-' is an unused value. | |
281 | */ | |
282 | ||
283 | /* read X values */ | |
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]; | |
287 | } | |
288 | ||
289 | /* read Y values */ | |
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]; | |
293 | } | |
294 | } else { | |
295 | for (i = 0; i < 8; i++) { | |
296 | /* X values */ | |
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]; | |
300 | if (i < 2) | |
301 | dev->xy_cur[i + 24] = dev->data[5 * i + 44]; | |
302 | ||
303 | /* Y values */ | |
304 | dev->xy_cur[i + 26] = dev->data[5 * i + 1]; | |
305 | dev->xy_cur[i + 34] = dev->data[5 * i + 3]; | |
306 | } | |
f7214ff4 SP |
307 | } |
308 | ||
309 | dbg_dump("sample", dev->xy_cur); | |
310 | ||
311 | if (!dev->valid) { | |
312 | /* first sample */ | |
313 | dev->valid = 1; | |
314 | dev->x_old = dev->y_old = -1; | |
315 | memcpy(dev->xy_old, dev->xy_cur, sizeof(dev->xy_old)); | |
316 | ||
9effa978 NB |
317 | if (atp_is_geyser_3(dev)) /* No 17" Macbooks (yet) */ |
318 | goto exit; | |
319 | ||
e1e02c9f MH |
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; | |
323 | ||
324 | printk("appletouch: 17\" model detected.\n"); | |
325 | if(atp_is_geyser_2(dev)) | |
326 | input_set_abs_params(dev->input, ABS_X, 0, | |
327 | (20 - 1) * | |
328 | ATP_XFACT - 1, | |
329 | ATP_FUZZ, 0); | |
330 | else | |
c5b7c7c3 DT |
331 | input_set_abs_params(dev->input, ABS_X, 0, |
332 | (ATP_XSENSORS - 1) * | |
f7214ff4 SP |
333 | ATP_XFACT - 1, |
334 | ATP_FUZZ, 0); | |
e1e02c9f MH |
335 | |
336 | break; | |
337 | } | |
f7214ff4 SP |
338 | |
339 | goto exit; | |
340 | } | |
341 | ||
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; | |
346 | ||
347 | /* prevent down drifting */ | |
348 | if (dev->xy_acc[i] < 0) | |
349 | dev->xy_acc[i] = 0; | |
350 | } | |
351 | ||
352 | memcpy(dev->xy_old, dev->xy_cur, sizeof(dev->xy_old)); | |
353 | ||
354 | dbg_dump("accumulator", dev->xy_acc); | |
355 | ||
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); | |
360 | ||
361 | if (x && y) { | |
362 | if (dev->x_old != -1) { | |
363 | x = (dev->x_old * 3 + x) >> 2; | |
364 | y = (dev->y_old * 3 + y) >> 2; | |
365 | dev->x_old = x; | |
366 | dev->y_old = y; | |
367 | ||
368 | if (debug > 1) | |
369 | printk("appletouch: X: %3d Y: %3d " | |
370 | "Xz: %3d Yz: %3d\n", | |
371 | x, y, x_z, y_z); | |
372 | ||
c5b7c7c3 DT |
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, | |
f7214ff4 | 377 | min(ATP_PRESSURE, x_z + y_z)); |
c5b7c7c3 | 378 | atp_report_fingers(dev->input, max(x_f, y_f)); |
f7214ff4 SP |
379 | } |
380 | dev->x_old = x; | |
381 | dev->y_old = y; | |
382 | } | |
383 | else if (!x && !y) { | |
384 | ||
385 | dev->x_old = dev->y_old = -1; | |
c5b7c7c3 DT |
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); | |
f7214ff4 SP |
389 | |
390 | /* reset the accumulator on release */ | |
391 | memset(dev->xy_acc, 0, sizeof(dev->xy_acc)); | |
392 | } | |
393 | ||
e1e02c9f MH |
394 | input_report_key(dev->input, BTN_LEFT, |
395 | !!dev->data[dev->datalen - 1]); | |
f7214ff4 | 396 | |
c5b7c7c3 | 397 | input_sync(dev->input); |
f7214ff4 SP |
398 | |
399 | exit: | |
400 | retval = usb_submit_urb(dev->urb, GFP_ATOMIC); | |
401 | if (retval) { | |
402 | err("%s - usb_submit_urb failed with result %d", | |
403 | __FUNCTION__, retval); | |
404 | } | |
405 | } | |
406 | ||
407 | static int atp_open(struct input_dev *input) | |
408 | { | |
409 | struct atp *dev = input->private; | |
410 | ||
411 | if (usb_submit_urb(dev->urb, GFP_ATOMIC)) | |
412 | return -EIO; | |
413 | ||
414 | dev->open = 1; | |
415 | return 0; | |
416 | } | |
417 | ||
418 | static void atp_close(struct input_dev *input) | |
419 | { | |
420 | struct atp *dev = input->private; | |
421 | ||
422 | usb_kill_urb(dev->urb); | |
423 | dev->open = 0; | |
424 | } | |
425 | ||
426 | static int atp_probe(struct usb_interface *iface, const struct usb_device_id *id) | |
427 | { | |
c5b7c7c3 DT |
428 | struct atp *dev; |
429 | struct input_dev *input_dev; | |
430 | struct usb_device *udev = interface_to_usbdev(iface); | |
f7214ff4 SP |
431 | struct usb_host_interface *iface_desc; |
432 | struct usb_endpoint_descriptor *endpoint; | |
433 | int int_in_endpointAddr = 0; | |
434 | int i, retval = -ENOMEM; | |
435 | ||
f7214ff4 SP |
436 | |
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; | |
448 | break; | |
449 | } | |
450 | } | |
451 | if (!int_in_endpointAddr) { | |
f7214ff4 | 452 | err("Could not find int-in endpoint"); |
c5b7c7c3 | 453 | return -EIO; |
f7214ff4 SP |
454 | } |
455 | ||
c5b7c7c3 DT |
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"); | |
461 | goto err_free_devs; | |
462 | } | |
463 | ||
464 | dev->udev = udev; | |
465 | dev->input = input_dev; | |
e1e02c9f | 466 | dev->overflowwarn = 0; |
9effa978 NB |
467 | if (atp_is_geyser_3(dev)) |
468 | dev->datalen = 64; | |
469 | else if (atp_is_geyser_2(dev)) | |
470 | dev->datalen = 64; | |
471 | else | |
472 | dev->datalen = 81; | |
473 | ||
474 | if (atp_is_geyser_3(dev)) { | |
475 | /* | |
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). | |
479 | */ | |
480 | char data[8]; | |
481 | int size; | |
482 | ||
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); | |
488 | ||
489 | if (size != 8) { | |
490 | err("Could not do mode read request from device" | |
491 | " (Geyser 3 mode)"); | |
492 | goto err_free_devs; | |
493 | } | |
494 | ||
495 | /* Apply the mode switch */ | |
496 | data[0] = ATP_GEYSER3_MODE_VENDOR_VALUE; | |
497 | ||
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); | |
503 | ||
504 | if (size != 8) { | |
505 | err("Could not do mode write request to device" | |
506 | " (Geyser 3 mode)"); | |
507 | goto err_free_devs; | |
508 | } | |
509 | printk("appletouch Geyser 3 inited.\n"); | |
510 | } | |
f7214ff4 SP |
511 | |
512 | dev->urb = usb_alloc_urb(0, GFP_KERNEL); | |
513 | if (!dev->urb) { | |
514 | retval = -ENOMEM; | |
c5b7c7c3 | 515 | goto err_free_devs; |
f7214ff4 | 516 | } |
c5b7c7c3 | 517 | |
e1e02c9f | 518 | dev->data = usb_buffer_alloc(dev->udev, dev->datalen, GFP_KERNEL, |
f7214ff4 SP |
519 | &dev->urb->transfer_dma); |
520 | if (!dev->data) { | |
521 | retval = -ENOMEM; | |
c5b7c7c3 | 522 | goto err_free_urb; |
f7214ff4 | 523 | } |
c5b7c7c3 DT |
524 | |
525 | usb_fill_int_urb(dev->urb, udev, | |
526 | usb_rcvintpipe(udev, int_in_endpointAddr), | |
e1e02c9f | 527 | dev->data, dev->datalen, atp_complete, dev, 1); |
f7214ff4 | 528 | |
c5b7c7c3 DT |
529 | usb_make_path(udev, dev->phys, sizeof(dev->phys)); |
530 | strlcat(dev->phys, "/input0", sizeof(dev->phys)); | |
531 | ||
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; | |
f7214ff4 | 536 | |
c5b7c7c3 DT |
537 | input_dev->private = dev; |
538 | input_dev->open = atp_open; | |
539 | input_dev->close = atp_close; | |
f7214ff4 | 540 | |
c5b7c7c3 | 541 | set_bit(EV_ABS, input_dev->evbit); |
f7214ff4 | 542 | |
9effa978 NB |
543 | if (atp_is_geyser_3(dev)) { |
544 | /* | |
545 | * MacBook have 20 X sensors, 10 Y sensors | |
546 | */ | |
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)) { | |
e1e02c9f MH |
552 | /* |
553 | * Oct 2005 15" PowerBooks have 15 X sensors, 17" are detected | |
554 | * later. | |
555 | */ | |
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); | |
560 | } else { | |
561 | /* | |
562 | * 12" and 15" Powerbooks only have 16 x sensors, | |
563 | * 17" models are detected later. | |
564 | */ | |
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); | |
569 | } | |
c5b7c7c3 | 570 | input_set_abs_params(input_dev, ABS_PRESSURE, 0, ATP_PRESSURE, 0, 0); |
f7214ff4 | 571 | |
c5b7c7c3 DT |
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); | |
f7214ff4 | 578 | |
c5b7c7c3 | 579 | input_register_device(dev->input); |
f7214ff4 | 580 | |
c5b7c7c3 DT |
581 | /* save our data pointer in this interface device */ |
582 | usb_set_intfdata(iface, dev); | |
f7214ff4 SP |
583 | |
584 | return 0; | |
585 | ||
c5b7c7c3 | 586 | err_free_urb: |
f7214ff4 | 587 | usb_free_urb(dev->urb); |
c5b7c7c3 | 588 | err_free_devs: |
f7214ff4 | 589 | usb_set_intfdata(iface, NULL); |
f7214ff4 | 590 | kfree(dev); |
c5b7c7c3 | 591 | input_free_device(input_dev); |
f7214ff4 SP |
592 | return retval; |
593 | } | |
594 | ||
595 | static void atp_disconnect(struct usb_interface *iface) | |
596 | { | |
597 | struct atp *dev = usb_get_intfdata(iface); | |
598 | ||
599 | usb_set_intfdata(iface, NULL); | |
600 | if (dev) { | |
601 | usb_kill_urb(dev->urb); | |
c5b7c7c3 | 602 | input_unregister_device(dev->input); |
f7214ff4 | 603 | usb_free_urb(dev->urb); |
e1e02c9f | 604 | usb_buffer_free(dev->udev, dev->datalen, |
f7214ff4 SP |
605 | dev->data, dev->urb->transfer_dma); |
606 | kfree(dev); | |
607 | } | |
608 | printk(KERN_INFO "input: appletouch disconnected\n"); | |
609 | } | |
610 | ||
611 | static int atp_suspend(struct usb_interface *iface, pm_message_t message) | |
612 | { | |
613 | struct atp *dev = usb_get_intfdata(iface); | |
614 | usb_kill_urb(dev->urb); | |
615 | dev->valid = 0; | |
616 | return 0; | |
617 | } | |
618 | ||
619 | static int atp_resume(struct usb_interface *iface) | |
620 | { | |
621 | struct atp *dev = usb_get_intfdata(iface); | |
622 | if (dev->open && usb_submit_urb(dev->urb, GFP_ATOMIC)) | |
623 | return -EIO; | |
624 | ||
625 | return 0; | |
626 | } | |
627 | ||
628 | static struct usb_driver atp_driver = { | |
f7214ff4 SP |
629 | .name = "appletouch", |
630 | .probe = atp_probe, | |
631 | .disconnect = atp_disconnect, | |
632 | .suspend = atp_suspend, | |
633 | .resume = atp_resume, | |
634 | .id_table = atp_table, | |
635 | }; | |
636 | ||
637 | static int __init atp_init(void) | |
638 | { | |
639 | return usb_register(&atp_driver); | |
640 | } | |
641 | ||
642 | static void __exit atp_exit(void) | |
643 | { | |
644 | usb_deregister(&atp_driver); | |
645 | } | |
646 | ||
647 | module_init(atp_init); | |
648 | module_exit(atp_exit); |