Automatic merge of rsync://rsync.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[linux-2.6.git] / drivers / usb / input / wacom.c
1 /*
2  *  USB Wacom Graphire and Wacom Intuos tablet support
3  *
4  *  Copyright (c) 2000-2004 Vojtech Pavlik      <vojtech@ucw.cz>
5  *  Copyright (c) 2000 Andreas Bach Aaen        <abach@stofanet.dk>
6  *  Copyright (c) 2000 Clifford Wolf            <clifford@clifford.at>
7  *  Copyright (c) 2000 Sam Mosel                <sam.mosel@computer.org>
8  *  Copyright (c) 2000 James E. Blair           <corvus@gnu.org>
9  *  Copyright (c) 2000 Daniel Egger             <egger@suse.de>
10  *  Copyright (c) 2001 Frederic Lepied          <flepied@mandrakesoft.com>
11  *  Copyright (c) 2004 Panagiotis Issaris       <panagiotis.issaris@mech.kuleuven.ac.be>
12  *  Copyright (c) 2002-2004 Ping Cheng          <pingc@wacom.com>
13  *
14  *  ChangeLog:
15  *      v0.1 (vp)  - Initial release
16  *      v0.2 (aba) - Support for all buttons / combinations
17  *      v0.3 (vp)  - Support for Intuos added
18  *      v0.4 (sm)  - Support for more Intuos models, menustrip
19  *                      relative mode, proximity.
20  *      v0.5 (vp)  - Big cleanup, nifty features removed,
21  *                      they belong in userspace
22  *      v1.8 (vp)  - Submit URB only when operating, moved to CVS,
23  *                      use input_report_key instead of report_btn and
24  *                      other cleanups
25  *      v1.11 (vp) - Add URB ->dev setting for new kernels
26  *      v1.11 (jb) - Add support for the 4D Mouse & Lens
27  *      v1.12 (de) - Add support for two more inking pen IDs
28  *      v1.14 (vp) - Use new USB device id probing scheme.
29  *                   Fix Wacom Graphire mouse wheel
30  *      v1.18 (vp) - Fix mouse wheel direction
31  *                   Make mouse relative
32  *      v1.20 (fl) - Report tool id for Intuos devices
33  *                 - Multi tools support
34  *                 - Corrected Intuos protocol decoding (airbrush, 4D mouse, lens cursor...)
35  *                 - Add PL models support
36  *                 - Fix Wacom Graphire mouse wheel again
37  *      v1.21 (vp) - Removed protocol descriptions
38  *                 - Added MISC_SERIAL for tool serial numbers
39  *            (gb) - Identify version on module load.
40  *    v1.21.1 (fl) - added Graphire2 support
41  *    v1.21.2 (fl) - added Intuos2 support
42  *                 - added all the PL ids
43  *    v1.21.3 (fl) - added another eraser id from Neil Okamoto
44  *                 - added smooth filter for Graphire from Peri Hankey
45  *                 - added PenPartner support from Olaf van Es
46  *                 - new tool ids from Ole Martin Bjoerndalen
47  *      v1.29 (pc) - Add support for more tablets
48  *                 - Fix pressure reporting
49  *      v1.30 (vp) - Merge 2.4 and 2.5 drivers
50  *                 - Since 2.5 now has input_sync(), remove MSC_SERIAL abuse
51  *                 - Cleanups here and there
52  *    v1.30.1 (pi) - Added Graphire3 support
53  *      v1.40 (pc) - Add support for several new devices, fix eraser reporting, ...
54  */
55
56 /*
57  * This program is free software; you can redistribute it and/or modify
58  * it under the terms of the GNU General Public License as published by
59  * the Free Software Foundation; either version 2 of the License, or
60  * (at your option) any later version.
61  */
62
63 #include <linux/kernel.h>
64 #include <linux/slab.h>
65 #include <linux/input.h>
66 #include <linux/module.h>
67 #include <linux/init.h>
68 #include <linux/usb.h>
69 #include <asm/unaligned.h>
70 #include <asm/byteorder.h>
71
72 /*
73  * Version Information
74  */
75 #define DRIVER_VERSION "v1.40"
76 #define DRIVER_AUTHOR "Vojtech Pavlik <vojtech@ucw.cz>"
77 #define DRIVER_DESC "USB Wacom Graphire and Wacom Intuos tablet driver"
78 #define DRIVER_LICENSE "GPL"
79
80 MODULE_AUTHOR(DRIVER_AUTHOR);
81 MODULE_DESCRIPTION(DRIVER_DESC);
82 MODULE_LICENSE(DRIVER_LICENSE);
83
84 #define USB_VENDOR_ID_WACOM     0x056a
85
86 struct wacom_features {
87         char *name;
88         int pktlen;
89         int x_max;
90         int y_max;
91         int pressure_max;
92         int distance_max;
93         int type;
94         usb_complete_t irq;
95 };
96
97 struct wacom {
98         signed char *data;
99         dma_addr_t data_dma;
100         struct input_dev dev;
101         struct usb_device *usbdev;
102         struct urb *irq;
103         struct wacom_features *features;
104         int tool[2];
105         __u32 serial[2];
106         char phys[32];
107 };
108
109 #define USB_REQ_SET_REPORT      0x09
110 static int usb_set_report(struct usb_interface *intf, unsigned char type,
111                                 unsigned char id, void *buf, int size)
112 {
113         return usb_control_msg(interface_to_usbdev(intf),
114                 usb_sndctrlpipe(interface_to_usbdev(intf), 0),
115                 USB_REQ_SET_REPORT, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
116                 (type << 8) + id, intf->altsetting[0].desc.bInterfaceNumber,
117                 buf, size, 1000);
118 }
119
120 static void wacom_pl_irq(struct urb *urb, struct pt_regs *regs)
121 {
122         struct wacom *wacom = urb->context;
123         unsigned char *data = wacom->data;
124         struct input_dev *dev = &wacom->dev;
125         int prox, pressure;
126         int retval;
127
128         switch (urb->status) {
129         case 0:
130                 /* success */
131                 break;
132         case -ECONNRESET:
133         case -ENOENT:
134         case -ESHUTDOWN:
135                 /* this urb is terminated, clean up */
136                 dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
137                 return;
138         default:
139                 dbg("%s - nonzero urb status received: %d", __FUNCTION__, urb->status);
140                 goto exit;
141         }
142
143         if (data[0] != 2) {
144                 dbg("wacom_pl_irq: received unknown report #%d", data[0]);
145                 goto exit;
146         }
147
148         prox = data[1] & 0x40;
149
150         input_regs(dev, regs);
151
152         if (prox) {
153
154                 pressure = (signed char)((data[7] << 1) | ((data[4] >> 2) & 1));
155                 if (wacom->features->pressure_max > 255)
156                         pressure = (pressure << 1) | ((data[4] >> 6) & 1);
157                 pressure += (wacom->features->pressure_max + 1) / 2;
158
159                 /*
160                  * if going from out of proximity into proximity select between the eraser
161                  * and the pen based on the state of the stylus2 button, choose eraser if
162                  * pressed else choose pen. if not a proximity change from out to in, send
163                  * an out of proximity for previous tool then a in for new tool.
164                  */
165                 if (!wacom->tool[0]) {
166                         /* Going into proximity select tool */
167                         wacom->tool[1] = (data[4] & 0x20)? BTN_TOOL_RUBBER : BTN_TOOL_PEN;
168                 }
169                 else {
170                         /* was entered with stylus2 pressed */
171                         if (wacom->tool[1] == BTN_TOOL_RUBBER && !(data[4] & 0x20) ) {
172                                 /* report out proximity for previous tool */
173                                 input_report_key(dev, wacom->tool[1], 0);
174                                 input_sync(dev);
175                                 wacom->tool[1] = BTN_TOOL_PEN;
176                                 goto exit;
177                         }
178                 }
179                 if (wacom->tool[1] != BTN_TOOL_RUBBER) {
180                         /* Unknown tool selected default to pen tool */
181                         wacom->tool[1] = BTN_TOOL_PEN;
182                 }
183                 input_report_key(dev, wacom->tool[1], prox); /* report in proximity for tool */
184                 input_report_abs(dev, ABS_X, data[3] | ((__u32)data[2] << 7) | ((__u32)(data[1] & 0x03) << 14));
185                 input_report_abs(dev, ABS_Y, data[6] | ((__u32)data[5] << 7) | ((__u32)(data[4] & 0x03) << 14));
186                 input_report_abs(dev, ABS_PRESSURE, pressure);
187
188                 input_report_key(dev, BTN_TOUCH, data[4] & 0x08);
189                 input_report_key(dev, BTN_STYLUS, data[4] & 0x10);
190                 /* Only allow the stylus2 button to be reported for the pen tool. */
191                 input_report_key(dev, BTN_STYLUS2, (wacom->tool[1] == BTN_TOOL_PEN) && (data[4] & 0x20));
192         }
193         else {
194                 /* report proximity-out of a (valid) tool */
195                 if (wacom->tool[1] != BTN_TOOL_RUBBER) {
196                         /* Unknown tool selected default to pen tool */
197                         wacom->tool[1] = BTN_TOOL_PEN;
198                 }
199                 input_report_key(dev, wacom->tool[1], prox);
200         }
201
202         wacom->tool[0] = prox; /* Save proximity state */
203         input_sync(dev);
204
205 exit:
206         retval = usb_submit_urb (urb, GFP_ATOMIC);
207         if (retval)
208                 err ("%s - usb_submit_urb failed with result %d",
209                      __FUNCTION__, retval);
210 }
211
212 static void wacom_ptu_irq(struct urb *urb, struct pt_regs *regs)
213 {
214         struct wacom *wacom = urb->context;
215         unsigned char *data = wacom->data;
216         struct input_dev *dev = &wacom->dev;
217         int retval;
218
219         switch (urb->status) {
220         case 0:
221                 /* success */
222                 break;
223         case -ECONNRESET:
224         case -ENOENT:
225         case -ESHUTDOWN:
226                 /* this urb is terminated, clean up */
227                 dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
228                 return;
229         default:
230                 dbg("%s - nonzero urb status received: %d", __FUNCTION__, urb->status);
231                 goto exit;
232         }
233
234         if (data[0] != 2)
235         {
236                 printk(KERN_INFO "wacom_ptu_irq: received unknown report #%d\n", data[0]);
237                 goto exit;
238         }
239
240         input_regs(dev, regs);
241         if (data[1] & 0x04)
242         {
243                 input_report_key(dev, BTN_TOOL_RUBBER, data[1] & 0x20);
244                 input_report_key(dev, BTN_TOUCH, data[1] & 0x08);
245         }
246         else
247         {
248                 input_report_key(dev, BTN_TOOL_PEN, data[1] & 0x20);
249                 input_report_key(dev, BTN_TOUCH, data[1] & 0x01);
250         }
251         input_report_abs(dev, ABS_X, le16_to_cpu(*(__le16 *) &data[2]));
252         input_report_abs(dev, ABS_Y, le16_to_cpu(*(__le16 *) &data[4]));
253         input_report_abs(dev, ABS_PRESSURE, le16_to_cpu(*(__le16 *) &data[6]));
254         input_report_key(dev, BTN_STYLUS, data[1] & 0x02);
255         input_report_key(dev, BTN_STYLUS2, data[1] & 0x10);
256
257         input_sync(dev);
258
259 exit:
260         retval = usb_submit_urb (urb, GFP_ATOMIC);
261         if (retval)
262                 err ("%s - usb_submit_urb failed with result %d",
263                      __FUNCTION__, retval);
264 }
265
266 static void wacom_penpartner_irq(struct urb *urb, struct pt_regs *regs)
267 {
268         struct wacom *wacom = urb->context;
269         unsigned char *data = wacom->data;
270         struct input_dev *dev = &wacom->dev;
271         int retval;
272
273         switch (urb->status) {
274         case 0:
275                 /* success */
276                 break;
277         case -ECONNRESET:
278         case -ENOENT:
279         case -ESHUTDOWN:
280                 /* this urb is terminated, clean up */
281                 dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
282                 return;
283         default:
284                 dbg("%s - nonzero urb status received: %d", __FUNCTION__, urb->status);
285                 goto exit;
286         }
287
288         if (data[0] != 2) {
289                 printk(KERN_INFO "wacom_penpartner_irq: received unknown report #%d\n", data[0]);
290                 goto exit;
291         }
292
293         input_regs(dev, regs);
294         input_report_key(dev, BTN_TOOL_PEN, 1);
295         input_report_abs(dev, ABS_X, le16_to_cpu(*(__le16 *) &data[1]));
296         input_report_abs(dev, ABS_Y, le16_to_cpu(*(__le16 *) &data[3]));
297         input_report_abs(dev, ABS_PRESSURE, (signed char)data[6] + 127);
298         input_report_key(dev, BTN_TOUCH, ((signed char)data[6] > -80) && !(data[5] & 0x20));
299         input_report_key(dev, BTN_STYLUS, (data[5] & 0x40));
300         input_sync(dev);
301
302 exit:
303         retval = usb_submit_urb (urb, GFP_ATOMIC);
304         if (retval)
305                 err ("%s - usb_submit_urb failed with result %d",
306                      __FUNCTION__, retval);
307 }
308
309 static void wacom_graphire_irq(struct urb *urb, struct pt_regs *regs)
310 {
311         struct wacom *wacom = urb->context;
312         unsigned char *data = wacom->data;
313         struct input_dev *dev = &wacom->dev;
314         int x, y;
315         int retval;
316
317         switch (urb->status) {
318         case 0:
319                 /* success */
320                 break;
321         case -ECONNRESET:
322         case -ENOENT:
323         case -ESHUTDOWN:
324                 /* this urb is terminated, clean up */
325                 dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
326                 return;
327         default:
328                 dbg("%s - nonzero urb status received: %d", __FUNCTION__, urb->status);
329                 goto exit;
330         }
331
332         if (data[0] != 2) {
333                 dbg("wacom_graphire_irq: received unknown report #%d", data[0]);
334                 goto exit;
335         }
336
337         x = le16_to_cpu(*(__le16 *) &data[2]);
338         y = le16_to_cpu(*(__le16 *) &data[4]);
339
340         input_regs(dev, regs);
341
342         switch ((data[1] >> 5) & 3) {
343
344                 case 0: /* Pen */
345                         input_report_key(dev, BTN_TOOL_PEN, data[1] & 0x80);
346                         break;
347
348                 case 1: /* Rubber */
349                         input_report_key(dev, BTN_TOOL_RUBBER, data[1] & 0x80);
350                         break;
351
352                 case 2: /* Mouse with wheel */
353                         input_report_key(dev, BTN_MIDDLE, data[1] & 0x04);
354                         input_report_rel(dev, REL_WHEEL, (signed char) data[6]);
355                         /* fall through */
356
357                 case 3: /* Mouse without wheel */
358                         input_report_key(dev, BTN_TOOL_MOUSE, data[7] > 24);
359                         input_report_key(dev, BTN_LEFT, data[1] & 0x01);
360                         input_report_key(dev, BTN_RIGHT, data[1] & 0x02);
361                         input_report_abs(dev, ABS_DISTANCE, data[7]);
362
363                         input_report_abs(dev, ABS_X, x);
364                         input_report_abs(dev, ABS_Y, y);
365
366                         input_sync(dev);
367                         goto exit;
368         }
369
370         if (data[1] & 0x80) {
371                 input_report_abs(dev, ABS_X, x);
372                 input_report_abs(dev, ABS_Y, y);
373         }
374
375         input_report_abs(dev, ABS_PRESSURE, le16_to_cpu(*(__le16 *) &data[6]));
376         input_report_key(dev, BTN_TOUCH, data[1] & 0x01);
377         input_report_key(dev, BTN_STYLUS, data[1] & 0x02);
378         input_report_key(dev, BTN_STYLUS2, data[1] & 0x04);
379
380         input_sync(dev);
381
382 exit:
383         retval = usb_submit_urb (urb, GFP_ATOMIC);
384         if (retval)
385                 err ("%s - usb_submit_urb failed with result %d",
386                      __FUNCTION__, retval);
387 }
388
389 static int wacom_intuos_inout(struct urb *urb)
390 {
391         struct wacom *wacom = urb->context;
392         unsigned char *data = wacom->data;
393         struct input_dev *dev = &wacom->dev;
394         int idx;
395
396         /* tool number */
397         idx = data[1] & 0x01;
398
399         /* Enter report */
400         if ((data[1] & 0xfc) == 0xc0)
401         {
402                 /* serial number of the tool */
403                 wacom->serial[idx] = ((__u32)(data[3] & 0x0f) << 28) +
404                         ((__u32)data[4] << 20) + ((__u32)data[5] << 12) +
405                         ((__u32)data[6] << 4) + (data[7] >> 4);
406
407                 switch (((__u32)data[2] << 4) | (data[3] >> 4)) {
408                         case 0x812: /* Inking pen */
409                         case 0x801: /* Intuos3 Inking pen */
410                         case 0x012:
411                                 wacom->tool[idx] = BTN_TOOL_PENCIL;
412                                 break;
413                         case 0x822: /* Pen */
414                         case 0x842:
415                         case 0x852:
416                         case 0x823: /* Intuos3 Grip Pen */
417                         case 0x813: /* Intuos3 Classic Pen */
418                         case 0x885: /* Intuos3 Marker Pen */
419                         case 0x022:
420                                 wacom->tool[idx] = BTN_TOOL_PEN;
421                                 break;
422                         case 0x832: /* Stroke pen */
423                         case 0x032:
424                                 wacom->tool[idx] = BTN_TOOL_BRUSH;
425                                 break;
426                         case 0x007: /* Mouse 4D and 2D */
427                         case 0x09c:
428                         case 0x094:
429                         case 0x017: /* Intuos3 2D Mouse */
430                                 wacom->tool[idx] = BTN_TOOL_MOUSE;
431                                 break;
432                         case 0x096: /* Lens cursor */
433                         case 0x097: /* Intuos3 Lens cursor */
434                                 wacom->tool[idx] = BTN_TOOL_LENS;
435                                 break;
436                         case 0x82a: /* Eraser */
437                         case 0x85a:
438                         case 0x91a:
439                         case 0xd1a:
440                         case 0x0fa:
441                         case 0x82b: /* Intuos3 Grip Pen Eraser */
442                         case 0x81b: /* Intuos3 Classic Pen Eraser */
443                         case 0x91b: /* Intuos3 Airbrush Eraser */
444                                 wacom->tool[idx] = BTN_TOOL_RUBBER;
445                                 break;
446                         case 0xd12:
447                         case 0x912:
448                         case 0x112:
449                         case 0x913: /* Intuos3 Airbrush */
450                                 wacom->tool[idx] = BTN_TOOL_AIRBRUSH;
451                                 break;  /* Airbrush */
452                         default: /* Unknown tool */
453                                 wacom->tool[idx] = BTN_TOOL_PEN;
454                 }
455                 input_report_key(dev, wacom->tool[idx], 1);
456                 input_event(dev, EV_MSC, MSC_SERIAL, wacom->serial[idx]);
457                 input_sync(dev);
458                 return 1;
459         }
460
461         /* Exit report */
462         if ((data[1] & 0xfe) == 0x80) {
463                 input_report_key(dev, wacom->tool[idx], 0);
464                 input_event(dev, EV_MSC, MSC_SERIAL, wacom->serial[idx]);
465                 input_sync(dev);
466                 return 1;
467         }
468
469         return 0;
470 }
471
472 static void wacom_intuos_general(struct urb *urb)
473 {
474         struct wacom *wacom = urb->context;
475         unsigned char *data = wacom->data;
476         struct input_dev *dev = &wacom->dev;
477         unsigned int t;
478
479         /* general pen packet */
480         if ((data[1] & 0xb8) == 0xa0)
481         {
482                 t = ((__u32)data[6] << 2) | ((data[7] >> 6) & 3);
483                 input_report_abs(dev, ABS_PRESSURE, t);
484                 input_report_abs(dev, ABS_TILT_X,
485                                 ((data[7] << 1) & 0x7e) | (data[8] >> 7));
486                 input_report_abs(dev, ABS_TILT_Y, data[8] & 0x7f);
487                 input_report_key(dev, BTN_STYLUS, data[1] & 2);
488                 input_report_key(dev, BTN_STYLUS2, data[1] & 4);
489                 input_report_key(dev, BTN_TOUCH, t > 10);
490         }
491
492         /* airbrush second packet */
493         if ((data[1] & 0xbc) == 0xb4)
494         {
495                 input_report_abs(dev, ABS_WHEEL,
496                                 ((__u32)data[6] << 2) | ((data[7] >> 6) & 3));
497                 input_report_abs(dev, ABS_TILT_X,
498                                 ((data[7] << 1) & 0x7e) | (data[8] >> 7));
499                 input_report_abs(dev, ABS_TILT_Y, data[8] & 0x7f);
500         }
501         return;
502 }
503
504 static void wacom_intuos_irq(struct urb *urb, struct pt_regs *regs)
505 {
506         struct wacom *wacom = urb->context;
507         unsigned char *data = wacom->data;
508         struct input_dev *dev = &wacom->dev;
509         unsigned int t;
510         int idx;
511         int retval;
512
513         switch (urb->status) {
514         case 0:
515                 /* success */
516                 break;
517         case -ECONNRESET:
518         case -ENOENT:
519         case -ESHUTDOWN:
520                 /* this urb is terminated, clean up */
521                 dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
522                 return;
523         default:
524                 dbg("%s - nonzero urb status received: %d", __FUNCTION__, urb->status);
525                 goto exit;
526         }
527
528         if (data[0] != 2 && data[0] != 5 && data[0] != 6) {
529                 dbg("wacom_intuos_irq: received unknown report #%d", data[0]);
530                 goto exit;
531         }
532
533         input_regs(dev, regs);
534
535         /* tool number */
536         idx = data[1] & 0x01;
537
538         /* process in/out prox events */
539         if (wacom_intuos_inout(urb)) goto exit;
540
541         input_report_abs(dev, ABS_X, be16_to_cpu(*(__be16 *) &data[2]));
542         input_report_abs(dev, ABS_Y, be16_to_cpu(*(__be16 *) &data[4]));
543         input_report_abs(dev, ABS_DISTANCE, data[9]);
544
545         /* process general packets */
546         wacom_intuos_general(urb);
547
548         if ((data[1] & 0xbc) == 0xa8 || (data[1] & 0xbe) == 0xb0) {             /* 4D mouse or Lens cursor packets */
549
550                 if (data[1] & 0x02) {                                           /* Rotation packet */
551
552                         t = ((__u32)data[6] << 3) | ((data[7] >> 5) & 7);
553                         input_report_abs(dev, ABS_RZ, (data[7] & 0x20) ? ((t - 1) / 2) : -t / 2);
554
555                 } else {
556
557                         if ((data[1] & 0x10) == 0) {                            /* 4D mouse packets */
558
559                                 input_report_key(dev, BTN_LEFT,   data[8] & 0x01);
560                                 input_report_key(dev, BTN_MIDDLE, data[8] & 0x02);
561                                 input_report_key(dev, BTN_RIGHT,  data[8] & 0x04);
562
563                                 input_report_key(dev, BTN_SIDE,   data[8] & 0x20);
564                                 input_report_key(dev, BTN_EXTRA,  data[8] & 0x10);
565                                 t = ((__u32)data[6] << 2) | ((data[7] >> 6) & 3);
566                                 input_report_abs(dev, ABS_THROTTLE, (data[8] & 0x08) ? -t : t);
567
568                         } else {
569                                 if (wacom->tool[idx] == BTN_TOOL_MOUSE) {       /* 2D mouse packets */
570                                         input_report_key(dev, BTN_LEFT,   data[8] & 0x04);
571                                         input_report_key(dev, BTN_MIDDLE, data[8] & 0x08);
572                                         input_report_key(dev, BTN_RIGHT,  data[8] & 0x10);
573                                         input_report_rel(dev, REL_WHEEL,
574                                             (-(__u32)(data[8] & 0x01) + (__u32)((data[8] & 0x02) >> 1)));
575                                 }
576                                 else {     /* Lens cursor packets */
577                                         input_report_key(dev, BTN_LEFT,   data[8] & 0x01);
578                                         input_report_key(dev, BTN_MIDDLE, data[8] & 0x02);
579                                         input_report_key(dev, BTN_RIGHT,  data[8] & 0x04);
580                                         input_report_key(dev, BTN_SIDE,   data[8] & 0x10);
581                                         input_report_key(dev, BTN_EXTRA,  data[8] & 0x08);
582                                 }
583                         }
584                 }
585         }
586
587         input_report_key(dev, wacom->tool[idx], 1);
588         input_event(dev, EV_MSC, MSC_SERIAL, wacom->serial[idx]);
589         input_sync(dev);
590
591 exit:
592         retval = usb_submit_urb (urb, GFP_ATOMIC);
593         if (retval)
594                 err ("%s - usb_submit_urb failed with result %d",
595                      __FUNCTION__, retval);
596 }
597
598 static void wacom_intuos3_irq(struct urb *urb, struct pt_regs *regs)
599 {
600         struct wacom *wacom = urb->context;
601         unsigned char *data = wacom->data;
602         struct input_dev *dev = &wacom->dev;
603         unsigned int t;
604         int idx, retval;
605
606         switch (urb->status) {
607         case 0:
608                 /* success */
609                 break;
610         case -ECONNRESET:
611         case -ENOENT:
612         case -ESHUTDOWN:
613                 /* this urb is terminated, clean up */
614                 dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
615                 return;
616         default:
617                 dbg("%s - nonzero urb status received: %d", __FUNCTION__, urb->status);
618                 goto exit;
619         }
620
621         /* check for valid report */
622         if (data[0] != 2 && data[0] != 5 && data[0] != 12)
623         {
624                 printk(KERN_INFO "wacom_intuos3_irq: received unknown report #%d\n", data[0]);
625                 goto exit;
626         }
627
628         input_regs(dev, regs);
629
630         /* tool index is always 0 here since there is no dual input tool */
631         idx = data[1] & 0x01;
632
633         /* pad packets. Works as a second tool and is always in prox */
634         if (data[0] == 12)
635         {
636                 /* initiate the pad as a device */
637                 if (wacom->tool[1] != BTN_TOOL_FINGER)
638                 {
639                         wacom->tool[1] = BTN_TOOL_FINGER;
640                         input_report_key(dev, wacom->tool[1], 1);
641                 }
642                 input_report_key(dev, BTN_0, (data[5] & 0x01));
643                 input_report_key(dev, BTN_1, (data[5] & 0x02));
644                 input_report_key(dev, BTN_2, (data[5] & 0x04));
645                 input_report_key(dev, BTN_3, (data[5] & 0x08));
646                 input_report_key(dev, BTN_4, (data[6] & 0x01));
647                 input_report_key(dev, BTN_5, (data[6] & 0x02));
648                 input_report_key(dev, BTN_6, (data[6] & 0x04));
649                 input_report_key(dev, BTN_7, (data[6] & 0x08));
650                 input_report_abs(dev, ABS_RX, ((data[1] & 0x1f) << 8) | data[2]);
651                 input_report_abs(dev, ABS_RY, ((data[3] & 0x1f) << 8) | data[4]);
652                 input_event(dev, EV_MSC, MSC_SERIAL, 0xffffffff);
653                 input_sync(dev);
654                 goto exit;
655         }
656
657         /* process in/out prox events */
658         if (wacom_intuos_inout(urb)) goto exit;
659
660         input_report_abs(dev, ABS_X, ((__u32)data[2] << 9) | ((__u32)data[3] << 1) | ((data[9] >> 1) & 1));
661         input_report_abs(dev, ABS_Y, ((__u32)data[4] << 9) | ((__u32)data[5] << 1) | (data[9] & 1));
662         input_report_abs(dev, ABS_DISTANCE, ((data[9] >> 2) & 0x3f));
663
664         /* process general packets */
665         wacom_intuos_general(urb);
666
667         if ((data[1] & 0xbc) == 0xa8 || (data[1] & 0xbe) == 0xb0)
668         {
669                 /* Marker pen rotation packet. Reported as wheel due to valuator limitation */
670                 if (data[1] & 0x02)
671                 {
672                         t = ((__u32)data[6] << 3) | ((data[7] >> 5) & 7);
673                         t = (data[7] & 0x20) ? ((t > 900) ? ((t-1) / 2 - 1350) :
674                                 ((t-1) / 2 + 450)) : (450 - t / 2) ;
675                         input_report_abs(dev, ABS_WHEEL, t);
676                 }
677
678                 /* 2D mouse packets */
679                 if (wacom->tool[idx] == BTN_TOOL_MOUSE)
680                 {
681                         input_report_key(dev, BTN_LEFT,   data[8] & 0x04);
682                         input_report_key(dev, BTN_MIDDLE, data[8] & 0x08);
683                         input_report_key(dev, BTN_RIGHT,  data[8] & 0x10);
684                         input_report_key(dev, BTN_SIDE,   data[8] & 0x40);
685                         input_report_key(dev, BTN_EXTRA,  data[8] & 0x20);
686                         /* mouse wheel is positive when rolled backwards */
687                         input_report_rel(dev, REL_WHEEL,  ((__u32)((data[8] & 0x02) >> 1)
688                                          - (__u32)(data[8] & 0x01)));
689                 }
690         }
691
692         input_report_key(dev, wacom->tool[idx], 1);
693         input_event(dev, EV_MSC, MSC_SERIAL, wacom->serial[idx]);
694         input_sync(dev);
695
696 exit:
697         retval = usb_submit_urb (urb, GFP_ATOMIC);
698         if (retval)
699                 err ("%s - usb_submit_urb failed with result %d",
700                     __FUNCTION__, retval);
701 }
702
703 static struct wacom_features wacom_features[] = {
704         { "Wacom Penpartner",    7,   5040,  3780,  255, 32, 0, wacom_penpartner_irq },
705         { "Wacom Graphire",      8,  10206,  7422,  511, 32, 1, wacom_graphire_irq },
706         { "Wacom Graphire2 4x5", 8,  10206,  7422,  511, 32, 1, wacom_graphire_irq },
707         { "Wacom Graphire2 5x7", 8,  13918, 10206,  511, 32, 1, wacom_graphire_irq },
708         { "Wacom Graphire3",     8,  10208,  7424,  511, 32, 1, wacom_graphire_irq },
709         { "Wacom Graphire3 6x8", 8,  16704, 12064,  511, 32, 1, wacom_graphire_irq },
710         { "Wacom Intuos 4x5",   10,  12700, 10600, 1023, 15, 2, wacom_intuos_irq },
711         { "Wacom Intuos 6x8",   10,  20320, 16240, 1023, 15, 2, wacom_intuos_irq },
712         { "Wacom Intuos 9x12",  10,  30480, 24060, 1023, 15, 2, wacom_intuos_irq },
713         { "Wacom Intuos 12x12", 10,  30480, 31680, 1023, 15, 2, wacom_intuos_irq },
714         { "Wacom Intuos 12x18", 10,  45720, 31680, 1023, 15, 2, wacom_intuos_irq },
715         { "Wacom PL400",         8,   5408,  4056,  255, 32, 3, wacom_pl_irq },
716         { "Wacom PL500",         8,   6144,  4608,  255, 32, 3, wacom_pl_irq },
717         { "Wacom PL600",         8,   6126,  4604,  255, 32, 3, wacom_pl_irq },
718         { "Wacom PL600SX",       8,   6260,  5016,  255, 32, 3, wacom_pl_irq },
719         { "Wacom PL550",         8,   6144,  4608,  511, 32, 3, wacom_pl_irq },
720         { "Wacom PL800",         8,   7220,  5780,  511, 32, 3, wacom_pl_irq },
721         { "Wacom Intuos2 4x5",   10, 12700, 10600, 1023, 15, 2, wacom_intuos_irq },
722         { "Wacom Intuos2 6x8",   10, 20320, 16240, 1023, 15, 2, wacom_intuos_irq },
723         { "Wacom Intuos2 9x12",  10, 30480, 24060, 1023, 15, 2, wacom_intuos_irq },
724         { "Wacom Intuos2 12x12", 10, 30480, 31680, 1023, 15, 2, wacom_intuos_irq },
725         { "Wacom Intuos2 12x18", 10, 45720, 31680, 1023, 15, 2, wacom_intuos_irq },
726         { "Wacom Volito",        8,   5104,  3712,  511, 32, 1, wacom_graphire_irq },
727         { "Wacom Cintiq Partner",8,  20480, 15360,  511, 32, 3, wacom_ptu_irq },
728         { "Wacom Intuos3 4x5",   10, 25400, 20320, 1023, 15, 4, wacom_intuos3_irq },
729         { "Wacom Intuos3 6x8",   10, 40640, 30480, 1023, 15, 4, wacom_intuos3_irq },
730         { "Wacom Intuos3 9x12",  10, 60960, 45720, 1023, 15, 4, wacom_intuos3_irq },
731         { "Wacom Intuos2 6x8",   10, 20320, 16240, 1023, 15, 2, wacom_intuos_irq },
732         { }
733 };
734
735 static struct usb_device_id wacom_ids[] = {
736         { USB_DEVICE(USB_VENDOR_ID_WACOM, 0x00) },
737         { USB_DEVICE(USB_VENDOR_ID_WACOM, 0x10) },
738         { USB_DEVICE(USB_VENDOR_ID_WACOM, 0x11) },
739         { USB_DEVICE(USB_VENDOR_ID_WACOM, 0x12) },
740         { USB_DEVICE(USB_VENDOR_ID_WACOM, 0x13) },
741         { USB_DEVICE(USB_VENDOR_ID_WACOM, 0x14) },
742         { USB_DEVICE(USB_VENDOR_ID_WACOM, 0x20) },
743         { USB_DEVICE(USB_VENDOR_ID_WACOM, 0x21) },
744         { USB_DEVICE(USB_VENDOR_ID_WACOM, 0x22) },
745         { USB_DEVICE(USB_VENDOR_ID_WACOM, 0x23) },
746         { USB_DEVICE(USB_VENDOR_ID_WACOM, 0x24) },
747         { USB_DEVICE(USB_VENDOR_ID_WACOM, 0x30) },
748         { USB_DEVICE(USB_VENDOR_ID_WACOM, 0x31) },
749         { USB_DEVICE(USB_VENDOR_ID_WACOM, 0x32) },
750         { USB_DEVICE(USB_VENDOR_ID_WACOM, 0x33) },
751         { USB_DEVICE(USB_VENDOR_ID_WACOM, 0x34) },
752         { USB_DEVICE(USB_VENDOR_ID_WACOM, 0x35) },
753         { USB_DEVICE(USB_VENDOR_ID_WACOM, 0x41) },
754         { USB_DEVICE(USB_VENDOR_ID_WACOM, 0x42) },
755         { USB_DEVICE(USB_VENDOR_ID_WACOM, 0x43) },
756         { USB_DEVICE(USB_VENDOR_ID_WACOM, 0x44) },
757         { USB_DEVICE(USB_VENDOR_ID_WACOM, 0x45) },
758         { USB_DEVICE(USB_VENDOR_ID_WACOM, 0x60) },
759         { USB_DEVICE(USB_VENDOR_ID_WACOM, 0x03) },
760         { USB_DEVICE(USB_VENDOR_ID_WACOM, 0xB0) },
761         { USB_DEVICE(USB_VENDOR_ID_WACOM, 0xB1) },
762         { USB_DEVICE(USB_VENDOR_ID_WACOM, 0xB2) },
763         { USB_DEVICE(USB_VENDOR_ID_WACOM, 0x47) },
764         { }
765 };
766
767 MODULE_DEVICE_TABLE(usb, wacom_ids);
768
769 static int wacom_open(struct input_dev *dev)
770 {
771         struct wacom *wacom = dev->private;
772
773         wacom->irq->dev = wacom->usbdev;
774         if (usb_submit_urb(wacom->irq, GFP_KERNEL))
775                 return -EIO;
776
777         return 0;
778 }
779
780 static void wacom_close(struct input_dev *dev)
781 {
782         struct wacom *wacom = dev->private;
783
784         usb_kill_urb(wacom->irq);
785 }
786
787 static int wacom_probe(struct usb_interface *intf, const struct usb_device_id *id)
788 {
789         struct usb_device *dev = interface_to_usbdev(intf);
790         struct usb_endpoint_descriptor *endpoint;
791         char rep_data[2] = {0x02, 0x02};
792         struct wacom *wacom;
793         char path[64];
794
795         if (!(wacom = kmalloc(sizeof(struct wacom), GFP_KERNEL)))
796                 return -ENOMEM;
797         memset(wacom, 0, sizeof(struct wacom));
798
799         wacom->data = usb_buffer_alloc(dev, 10, GFP_KERNEL, &wacom->data_dma);
800         if (!wacom->data) {
801                 kfree(wacom);
802                 return -ENOMEM;
803         }
804
805         wacom->irq = usb_alloc_urb(0, GFP_KERNEL);
806         if (!wacom->irq) {
807                 usb_buffer_free(dev, 10, wacom->data, wacom->data_dma);
808                 kfree(wacom);
809                 return -ENOMEM;
810         }
811
812         wacom->features = wacom_features + (id - wacom_ids);
813
814         wacom->dev.evbit[0] |= BIT(EV_KEY) | BIT(EV_ABS);
815         wacom->dev.absbit[0] |= BIT(ABS_X) | BIT(ABS_Y) | BIT(ABS_PRESSURE);
816         wacom->dev.keybit[LONG(BTN_DIGI)] |= BIT(BTN_TOOL_PEN) | BIT(BTN_TOUCH) | BIT(BTN_STYLUS);
817
818         switch (wacom->features->type) {
819                 case 1:
820                         wacom->dev.evbit[0] |= BIT(EV_REL);
821                         wacom->dev.relbit[0] |= BIT(REL_WHEEL);
822                         wacom->dev.absbit[0] |= BIT(ABS_DISTANCE);
823                         wacom->dev.keybit[LONG(BTN_LEFT)] |= BIT(BTN_LEFT) | BIT(BTN_RIGHT) | BIT(BTN_MIDDLE);
824                         wacom->dev.keybit[LONG(BTN_DIGI)] |= BIT(BTN_TOOL_RUBBER) | BIT(BTN_TOOL_MOUSE) | BIT(BTN_STYLUS2);
825                         break;
826
827                 case 4: /* new functions for Intuos3 */
828                         wacom->dev.keybit[LONG(BTN_DIGI)] |= BIT(BTN_TOOL_FINGER);
829                         wacom->dev.keybit[LONG(BTN_LEFT)] |= BIT(BTN_0) | BIT(BTN_1) | BIT(BTN_2) | BIT(BTN_3) | BIT(BTN_4) | BIT(BTN_5) | BIT(BTN_6) | BIT(BTN_7);
830                         wacom->dev.absbit[0] |= BIT(ABS_RX) | BIT(ABS_RY);
831                         /* fall through */
832
833                 case 2:
834                         wacom->dev.evbit[0] |= BIT(EV_MSC) | BIT(EV_REL);
835                         wacom->dev.mscbit[0] |= BIT(MSC_SERIAL);
836                         wacom->dev.relbit[0] |= BIT(REL_WHEEL);
837                         wacom->dev.keybit[LONG(BTN_LEFT)] |= BIT(BTN_LEFT) | BIT(BTN_RIGHT) | BIT(BTN_MIDDLE) | BIT(BTN_SIDE) | BIT(BTN_EXTRA);
838                         wacom->dev.keybit[LONG(BTN_DIGI)] |= BIT(BTN_TOOL_RUBBER) | BIT(BTN_TOOL_MOUSE) | BIT(BTN_TOOL_BRUSH)
839                                                           | BIT(BTN_TOOL_PENCIL) | BIT(BTN_TOOL_AIRBRUSH) | BIT(BTN_TOOL_LENS) | BIT(BTN_STYLUS2);
840                         wacom->dev.absbit[0] |= BIT(ABS_DISTANCE) | BIT(ABS_WHEEL) | BIT(ABS_TILT_X) | BIT(ABS_TILT_Y) | BIT(ABS_RZ) | BIT(ABS_THROTTLE);
841                         break;
842
843                 case 3:
844                         wacom->dev.keybit[LONG(BTN_DIGI)] |= BIT(BTN_STYLUS2) | BIT(BTN_TOOL_RUBBER);
845                         break;
846         }
847
848         wacom->dev.absmax[ABS_X] = wacom->features->x_max;
849         wacom->dev.absmax[ABS_Y] = wacom->features->y_max;
850         wacom->dev.absmax[ABS_PRESSURE] = wacom->features->pressure_max;
851         wacom->dev.absmax[ABS_DISTANCE] = wacom->features->distance_max;
852         wacom->dev.absmax[ABS_TILT_X] = 127;
853         wacom->dev.absmax[ABS_TILT_Y] = 127;
854         wacom->dev.absmax[ABS_WHEEL] = 1023;
855
856         wacom->dev.absmax[ABS_RX] = 4097;
857         wacom->dev.absmax[ABS_RY] = 4097;
858         wacom->dev.absmin[ABS_RZ] = -900;
859         wacom->dev.absmax[ABS_RZ] = 899;
860         wacom->dev.absmin[ABS_THROTTLE] = -1023;
861         wacom->dev.absmax[ABS_THROTTLE] = 1023;
862
863         wacom->dev.absfuzz[ABS_X] = 4;
864         wacom->dev.absfuzz[ABS_Y] = 4;
865
866         wacom->dev.private = wacom;
867         wacom->dev.open = wacom_open;
868         wacom->dev.close = wacom_close;
869
870         usb_make_path(dev, path, 64);
871         sprintf(wacom->phys, "%s/input0", path);
872
873         wacom->dev.name = wacom->features->name;
874         wacom->dev.phys = wacom->phys;
875         wacom->dev.id.bustype = BUS_USB;
876         wacom->dev.id.vendor = le16_to_cpu(dev->descriptor.idVendor);
877         wacom->dev.id.product = le16_to_cpu(dev->descriptor.idProduct);
878         wacom->dev.id.version = le16_to_cpu(dev->descriptor.bcdDevice);
879         wacom->dev.dev = &intf->dev;
880         wacom->usbdev = dev;
881
882         endpoint = &intf->cur_altsetting->endpoint[0].desc;
883
884         if (wacom->features->pktlen > 10)
885                 BUG();
886
887         usb_fill_int_urb(wacom->irq, dev,
888                          usb_rcvintpipe(dev, endpoint->bEndpointAddress),
889                          wacom->data, wacom->features->pktlen,
890                          wacom->features->irq, wacom, endpoint->bInterval);
891         wacom->irq->transfer_dma = wacom->data_dma;
892         wacom->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
893
894         input_register_device(&wacom->dev);
895
896         /* ask the tablet to report tablet data */
897         usb_set_report(intf, 3, 2, rep_data, 2);
898         /* repeat once (not sure why the first call often fails) */
899         usb_set_report(intf, 3, 2, rep_data, 2);
900
901         printk(KERN_INFO "input: %s on %s\n", wacom->features->name, path);
902
903         usb_set_intfdata(intf, wacom);
904
905         return 0;
906 }
907
908 static void wacom_disconnect(struct usb_interface *intf)
909 {
910         struct wacom *wacom = usb_get_intfdata (intf);
911
912         usb_set_intfdata(intf, NULL);
913         if (wacom) {
914                 usb_kill_urb(wacom->irq);
915                 input_unregister_device(&wacom->dev);
916                 usb_free_urb(wacom->irq);
917                 usb_buffer_free(interface_to_usbdev(intf), 10, wacom->data, wacom->data_dma);
918                 kfree(wacom);
919         }
920 }
921
922 static struct usb_driver wacom_driver = {
923         .owner =        THIS_MODULE,
924         .name =         "wacom",
925         .probe =        wacom_probe,
926         .disconnect =   wacom_disconnect,
927         .id_table =     wacom_ids,
928 };
929
930 static int __init wacom_init(void)
931 {
932         int result = usb_register(&wacom_driver);
933         if (result == 0)
934                 info(DRIVER_VERSION ":" DRIVER_DESC);
935         return result;
936 }
937
938 static void __exit wacom_exit(void)
939 {
940         usb_deregister(&wacom_driver);
941 }
942
943 module_init(wacom_init);
944 module_exit(wacom_exit);