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