This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / drivers / usb / input / appletouch.c
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
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 !!! */
43
44 #define ATP_DRIVER_VERSION      0x0007 /* 00.07 */
45
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
54
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) },
60 #if 0
61         Disabled until someone gives us the real USB id and tests the driver
62         { ATP_DEVICE(ATP_17INCH_ID1) },
63 #endif
64         { }                                     /* Terminating entry */
65 };
66 MODULE_DEVICE_TABLE (usb, atp_table);
67
68 /* size of a USB urb transfer */
69 #define ATP_DATASIZE    81
70
71 /*
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
74  * (vertical) sensors.
75  */
76 #define ATP_XSENSORS    26
77 #define ATP_YSENSORS    16
78
79 /* amount of fuzz this touchpad generates */
80 #define ATP_FUZZ        16
81
82 /* maximum pressure this driver will report */
83 #define ATP_PRESSURE    300
84 /*
85  * multiplication factor for the X and Y coordinates.
86  * We try to keep the touchpad aspect ratio while still doing only simple
87  * arithmetics.
88  * The factors below give coordinates like:
89  *      0 <= x <  960 on 12" and 15" Powerbooks
90  *      0 <= x < 1600 on 17" Powerbooks
91  *      0 <= y <  646
92  */
93 #define ATP_XFACT       64
94 #define ATP_YFACT       43
95
96 /*
97  * Threshold for the touchpad sensors. Any change less than ATP_THRESHOLD is
98  * ignored.
99  */
100 #define ATP_THRESHOLD    5
101
102 /* Structure to hold all of our device specific stuff */
103 struct atp {
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];
118 };
119
120 #define dbg_dump(msg, tab) \
121         if (debug > 1) {                                                \
122                 int i;                                                  \
123                 printk("appletouch: %s %lld", msg, (long long)jiffies); \
124                 for (i = 0; i < ATP_XSENSORS + ATP_YSENSORS; i++)       \
125                         printk("%02x ", tab[i]);                        \
126                 printk("\n");                                           \
127         }
128
129 #define dprintk(format, a...)                                           \
130         do {                                                            \
131                 if (debug) printk(format, ##a);                         \
132         } while (0)
133
134 MODULE_AUTHOR("Johannes Berg, Stelian Pop, Frank Arnold");
135 MODULE_DESCRIPTION("Apple PowerBooks USB touchpad driver");
136 MODULE_LICENSE("GPL");
137
138 static int debug = 1;
139 module_param(debug, int, 0644);
140 MODULE_PARM_DESC(debug, "Activate debugging output");
141
142 static int atp_calculate_abs(int *xy_sensors, int nb_sensors, int fact,
143                              int *z, int *fingers)
144 {
145         int i;
146         /* values to calculate mean */
147         int pcum = 0, psum = 0;
148
149         *fingers = 0;
150
151         for (i = 0; i < nb_sensors; i++) {
152                 if (xy_sensors[i] < ATP_THRESHOLD)
153                         continue;
154                 if ((i - 1 < 0) || (xy_sensors[i - 1] < ATP_THRESHOLD))
155                         (*fingers)++;
156                 pcum += xy_sensors[i] * i;
157                 psum += xy_sensors[i];
158         }
159
160         if (psum > 0) {
161                 *z = psum;
162                 return pcum * fact / psum;
163         }
164
165         return 0;
166 }
167
168 static inline void atp_report_fingers(struct input_dev *input, int fingers)
169 {
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);
173 }
174
175 static void atp_complete(struct urb* urb, struct pt_regs* regs)
176 {
177         int x, y, x_z, y_z, x_f, y_f;
178         int retval, i;
179         struct atp *dev = urb->context;
180
181         switch (urb->status) {
182         case 0:
183                 /* success */
184                 break;
185         case -ECONNRESET:
186         case -ENOENT:
187         case -ESHUTDOWN:
188                 /* This urb is terminated, clean up */
189                 dbg("%s - urb shutting down with status: %d",
190                     __FUNCTION__, urb->status);
191                 return;
192         default:
193                 dbg("%s - nonzero urb status received: %d",
194                     __FUNCTION__, urb->status);
195                 goto exit;
196         }
197
198         /* drop incomplete datasets */
199         if (dev->urb->actual_length != ATP_DATASIZE) {
200                 dprintk("appletouch: incomplete data package.\n");
201                 goto exit;
202         }
203
204         /* reorder the sensors values */
205         for (i = 0; i < 8; i++) {
206                 /* X values */
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];
210                 if (i < 2)
211                         dev->xy_cur[i + 24] = dev->data[5 * i + 44];
212
213                 /* Y values */
214                 dev->xy_cur[i + 26] = dev->data[5 * i +  1];
215                 dev->xy_cur[i + 34] = dev->data[5 * i +  3];
216         }
217
218         dbg_dump("sample", dev->xy_cur);
219
220         if (!dev->valid) {
221                 /* first sample */
222                 dev->valid = 1;
223                 dev->x_old = dev->y_old = -1;
224                 memcpy(dev->xy_old, dev->xy_cur, sizeof(dev->xy_old));
225                 goto exit;
226         }
227
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;
232
233                 /* prevent down drifting */
234                 if (dev->xy_acc[i] < 0)
235                         dev->xy_acc[i] = 0;
236         }
237
238         memcpy(dev->xy_old, dev->xy_cur, sizeof(dev->xy_old));
239
240         dbg_dump("accumulator", dev->xy_acc);
241
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);
246
247         if (x && y) {
248                 if (dev->x_old != -1) {
249                         x = (dev->x_old * 3 + x) >> 2;
250                         y = (dev->y_old * 3 + y) >> 2;
251                         dev->x_old = x;
252                         dev->y_old = y;
253
254                         if (debug > 1)
255                                 printk("appletouch: X: %3d Y: %3d "
256                                        "Xz: %3d Yz: %3d\n",
257                                        x, y, x_z, y_z);
258
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));
265                 }
266                 dev->x_old = x;
267                 dev->y_old = y;
268         }
269         else if (!x && !y) {
270
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);
275
276                 /* reset the accumulator on release */
277                 memset(dev->xy_acc, 0, sizeof(dev->xy_acc));
278         }
279
280         input_report_key(&dev->input, BTN_LEFT, !!dev->data[80]);
281
282         input_sync(&dev->input);
283
284 exit:
285         retval = usb_submit_urb(dev->urb, GFP_ATOMIC);
286         if (retval) {
287                 err("%s - usb_submit_urb failed with result %d",
288                     __FUNCTION__, retval);
289         }
290 }
291
292 static int atp_open(struct input_dev *input)
293 {
294         struct atp *dev = input->private;
295
296         if (usb_submit_urb(dev->urb, GFP_ATOMIC))
297                 return -EIO;
298
299         dev->open = 1;
300         return 0;
301 }
302
303 static void atp_close(struct input_dev *input)
304 {
305         struct atp *dev = input->private;
306
307         usb_kill_urb(dev->urb);
308         dev->open = 0;
309 }
310
311 static int atp_probe(struct usb_interface *iface, const struct usb_device_id *id)
312 {
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;
318
319         /* allocate memory for our device state and initialize it */
320         dev = kmalloc(sizeof(struct atp), GFP_KERNEL);
321         if (dev == NULL) {
322                 err("Out of memory");
323                 goto err_kmalloc;
324         }
325         memset(dev, 0, sizeof(struct atp));
326
327         dev->udev = interface_to_usbdev(iface);
328
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;
340                         break;
341                 }
342         }
343         if (!int_in_endpointAddr) {
344                 retval = -EIO;
345                 err("Could not find int-in endpoint");
346                 goto err_endpoint;
347         }
348
349         /* save our data pointer in this interface device */
350         usb_set_intfdata(iface, dev);
351
352         dev->urb = usb_alloc_urb(0, GFP_KERNEL);
353         if (!dev->urb) {
354                 retval = -ENOMEM;
355                 goto err_usballoc;
356         }
357         dev->data = usb_buffer_alloc(dev->udev, ATP_DATASIZE, GFP_KERNEL,
358                                      &dev->urb->transfer_dma);
359         if (!dev->data) {
360                 retval = -ENOMEM;
361                 goto err_usbbufalloc;
362         }
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);
366
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;
373
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;
378
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,
383                                      ATP_FUZZ, 0);
384         else
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,
388                                      ATP_FUZZ, 0);
389         input_set_abs_params(&dev->input, ABS_Y, 0,
390                              (ATP_YSENSORS - 1) * ATP_YFACT - 1,
391                              ATP_FUZZ, 0);
392         input_set_abs_params(&dev->input, ABS_PRESSURE, 0, ATP_PRESSURE, 0, 0);
393
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);
400
401         input_register_device(&dev->input);
402
403         printk(KERN_INFO "input: appletouch connected\n");
404
405         return 0;
406
407 err_usbbufalloc:
408         usb_free_urb(dev->urb);
409 err_usballoc:
410         usb_set_intfdata(iface, NULL);
411 err_endpoint:
412         kfree(dev);
413 err_kmalloc:
414         return retval;
415 }
416
417 static void atp_disconnect(struct usb_interface *iface)
418 {
419         struct atp *dev = usb_get_intfdata(iface);
420
421         usb_set_intfdata(iface, NULL);
422         if (dev) {
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);
428                 kfree(dev);
429         }
430         printk(KERN_INFO "input: appletouch disconnected\n");
431 }
432
433 static int atp_suspend(struct usb_interface *iface, pm_message_t message)
434 {
435         struct atp *dev = usb_get_intfdata(iface);
436         usb_kill_urb(dev->urb);
437         dev->valid = 0;
438         return 0;
439 }
440
441 static int atp_resume(struct usb_interface *iface)
442 {
443         struct atp *dev = usb_get_intfdata(iface);
444         if (dev->open && usb_submit_urb(dev->urb, GFP_ATOMIC))
445                 return -EIO;
446
447         return 0;
448 }
449
450 static struct usb_driver atp_driver = {
451         .owner          = THIS_MODULE,
452         .name           = "appletouch",
453         .probe          = atp_probe,
454         .disconnect     = atp_disconnect,
455         .suspend        = atp_suspend,
456         .resume         = atp_resume,
457         .id_table       = atp_table,
458 };
459
460 static int __init atp_init(void)
461 {
462         return usb_register(&atp_driver);
463 }
464
465 static void __exit atp_exit(void)
466 {
467         usb_deregister(&atp_driver);
468 }
469
470 module_init(atp_init);
471 module_exit(atp_exit);