Pull ngam-maule-steiner into release branch
[sfrench/cifs-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         input_regs(dev, regs);
346
347         if (data[1] & 0x10) { /* in prox */
348
349                 switch ((data[1] >> 5) & 3) {
350
351                         case 0: /* Pen */
352                                 wacom->tool[0] = BTN_TOOL_PEN;
353                                 break;
354
355                         case 1: /* Rubber */
356                                 wacom->tool[0] = BTN_TOOL_RUBBER;
357                                 break;
358
359                         case 2: /* Mouse with wheel */
360                                 input_report_key(dev, BTN_MIDDLE, data[1] & 0x04);
361                                 input_report_rel(dev, REL_WHEEL, (signed char) data[6]);
362                                 /* fall through */
363
364                         case 3: /* Mouse without wheel */
365                                 wacom->tool[0] = BTN_TOOL_MOUSE;
366                                 input_report_key(dev, BTN_LEFT, data[1] & 0x01);
367                                 input_report_key(dev, BTN_RIGHT, data[1] & 0x02);
368                                 input_report_abs(dev, ABS_DISTANCE, data[7]);
369                                 break;
370                 }
371         }
372
373         if (data[1] & 0x90) {
374                 x = le16_to_cpu(*(__le16 *) &data[2]);
375                 y = le16_to_cpu(*(__le16 *) &data[4]);
376                 input_report_abs(dev, ABS_X, x);
377                 input_report_abs(dev, ABS_Y, y);
378                 if (wacom->tool[0] != BTN_TOOL_MOUSE) {
379                         input_report_abs(dev, ABS_PRESSURE, le16_to_cpu(*(__le16 *) &data[6]));
380                         input_report_key(dev, BTN_TOUCH, data[1] & 0x01);
381                         input_report_key(dev, BTN_STYLUS, data[1] & 0x02);
382                         input_report_key(dev, BTN_STYLUS2, data[1] & 0x04);
383                 }
384         }
385
386         input_report_key(dev, wacom->tool[0], data[1] & 0x10);
387         input_sync(dev);
388
389  exit:
390         retval = usb_submit_urb (urb, GFP_ATOMIC);
391         if (retval)
392                 err ("%s - usb_submit_urb failed with result %d",
393                      __FUNCTION__, retval);
394 }
395
396 static int wacom_intuos_inout(struct urb *urb)
397 {
398         struct wacom *wacom = urb->context;
399         unsigned char *data = wacom->data;
400         struct input_dev *dev = &wacom->dev;
401         int idx;
402
403         /* tool number */
404         idx = data[1] & 0x01;
405
406         /* Enter report */
407         if ((data[1] & 0xfc) == 0xc0) {
408                 /* serial number of the tool */
409                 wacom->serial[idx] = ((data[3] & 0x0f) << 28) +
410                         (data[4] << 20) + (data[5] << 12) +
411                         (data[6] << 4) + (data[7] >> 4);
412
413                 switch ((data[2] << 4) | (data[3] >> 4)) {
414                         case 0x812: /* Inking pen */
415                         case 0x801: /* Intuos3 Inking pen */
416                         case 0x012:
417                                 wacom->tool[idx] = BTN_TOOL_PENCIL;
418                                 break;
419                         case 0x822: /* Pen */
420                         case 0x842:
421                         case 0x852:
422                         case 0x823: /* Intuos3 Grip Pen */
423                         case 0x813: /* Intuos3 Classic Pen */
424                         case 0x885: /* Intuos3 Marker Pen */
425                         case 0x022:
426                                 wacom->tool[idx] = BTN_TOOL_PEN;
427                                 break;
428                         case 0x832: /* Stroke pen */
429                         case 0x032:
430                                 wacom->tool[idx] = BTN_TOOL_BRUSH;
431                                 break;
432                         case 0x007: /* Mouse 4D and 2D */
433                         case 0x09c:
434                         case 0x094:
435                         case 0x017: /* Intuos3 2D Mouse */
436                                 wacom->tool[idx] = BTN_TOOL_MOUSE;
437                                 break;
438                         case 0x096: /* Lens cursor */
439                         case 0x097: /* Intuos3 Lens cursor */
440                                 wacom->tool[idx] = BTN_TOOL_LENS;
441                                 break;
442                         case 0x82a: /* Eraser */
443                         case 0x85a:
444                         case 0x91a:
445                         case 0xd1a:
446                         case 0x0fa:
447                         case 0x82b: /* Intuos3 Grip Pen Eraser */
448                         case 0x81b: /* Intuos3 Classic Pen Eraser */
449                         case 0x91b: /* Intuos3 Airbrush Eraser */
450                                 wacom->tool[idx] = BTN_TOOL_RUBBER;
451                                 break;
452                         case 0xd12:
453                         case 0x912:
454                         case 0x112:
455                         case 0x913: /* Intuos3 Airbrush */
456                                 wacom->tool[idx] = BTN_TOOL_AIRBRUSH;
457                                 break;
458                         default: /* Unknown tool */
459                                 wacom->tool[idx] = BTN_TOOL_PEN;
460                 }
461                 input_report_key(dev, wacom->tool[idx], 1);
462                 input_event(dev, EV_MSC, MSC_SERIAL, wacom->serial[idx]);
463                 input_sync(dev);
464                 return 1;
465         }
466
467         /* Exit report */
468         if ((data[1] & 0xfe) == 0x80) {
469                 input_report_key(dev, wacom->tool[idx], 0);
470                 input_event(dev, EV_MSC, MSC_SERIAL, wacom->serial[idx]);
471                 input_sync(dev);
472                 return 1;
473         }
474
475         return 0;
476 }
477
478 static void wacom_intuos_general(struct urb *urb)
479 {
480         struct wacom *wacom = urb->context;
481         unsigned char *data = wacom->data;
482         struct input_dev *dev = &wacom->dev;
483         unsigned int t;
484
485         /* general pen packet */
486         if ((data[1] & 0xb8) == 0xa0) {
487                 t = (data[6] << 2) | ((data[7] >> 6) & 3);
488                 input_report_abs(dev, ABS_PRESSURE, t);
489                 input_report_abs(dev, ABS_TILT_X,
490                                 ((data[7] << 1) & 0x7e) | (data[8] >> 7));
491                 input_report_abs(dev, ABS_TILT_Y, data[8] & 0x7f);
492                 input_report_key(dev, BTN_STYLUS, data[1] & 2);
493                 input_report_key(dev, BTN_STYLUS2, data[1] & 4);
494                 input_report_key(dev, BTN_TOUCH, t > 10);
495         }
496
497         /* airbrush second packet */
498         if ((data[1] & 0xbc) == 0xb4) {
499                 input_report_abs(dev, ABS_WHEEL,
500                                 (data[6] << 2) | ((data[7] >> 6) & 3));
501                 input_report_abs(dev, ABS_TILT_X,
502                                 ((data[7] << 1) & 0x7e) | (data[8] >> 7));
503                 input_report_abs(dev, ABS_TILT_Y, data[8] & 0x7f);
504         }
505         return;
506 }
507
508 static void wacom_intuos_irq(struct urb *urb, struct pt_regs *regs)
509 {
510         struct wacom *wacom = urb->context;
511         unsigned char *data = wacom->data;
512         struct input_dev *dev = &wacom->dev;
513         unsigned int t;
514         int idx;
515         int retval;
516
517         switch (urb->status) {
518         case 0:
519                 /* success */
520                 break;
521         case -ECONNRESET:
522         case -ENOENT:
523         case -ESHUTDOWN:
524                 /* this urb is terminated, clean up */
525                 dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
526                 return;
527         default:
528                 dbg("%s - nonzero urb status received: %d", __FUNCTION__, urb->status);
529                 goto exit;
530         }
531
532         if (data[0] != 2 && data[0] != 5 && data[0] != 6 && data[0] != 12) {
533                 dbg("wacom_intuos_irq: received unknown report #%d", data[0]);
534                 goto exit;
535         }
536
537         input_regs(dev, regs);
538
539         /* tool number */
540         idx = data[1] & 0x01;
541
542         /* pad packets. Works as a second tool and is always in prox */
543         if (data[0] == 12) {
544                 /* initiate the pad as a device */
545                 if (wacom->tool[1] != BTN_TOOL_FINGER) {
546                         wacom->tool[1] = BTN_TOOL_FINGER;
547                         input_report_key(dev, wacom->tool[1], 1);
548                 }
549                 input_report_key(dev, BTN_0, (data[5] & 0x01));
550                 input_report_key(dev, BTN_1, (data[5] & 0x02));
551                 input_report_key(dev, BTN_2, (data[5] & 0x04));
552                 input_report_key(dev, BTN_3, (data[5] & 0x08));
553                 input_report_key(dev, BTN_4, (data[6] & 0x01));
554                 input_report_key(dev, BTN_5, (data[6] & 0x02));
555                 input_report_key(dev, BTN_6, (data[6] & 0x04));
556                 input_report_key(dev, BTN_7, (data[6] & 0x08));
557                 input_report_abs(dev, ABS_RX, ((data[1] & 0x1f) << 8) | data[2]);
558                 input_report_abs(dev, ABS_RY, ((data[3] & 0x1f) << 8) | data[4]);
559                 input_event(dev, EV_MSC, MSC_SERIAL, 0xffffffff);
560                 input_sync(dev);
561                 goto exit;
562         }
563
564         /* process in/out prox events */
565         if (wacom_intuos_inout(urb))
566                 goto exit;
567
568         /* Cintiq doesn't send data when RDY bit isn't set */
569         if ((wacom->features->type == CINTIQ) && !(data[1] & 0x40))
570                 goto exit;
571
572         if (wacom->features->type >= INTUOS3) {
573                 input_report_abs(dev, ABS_X, (data[2] << 9) | (data[3] << 1) | ((data[9] >> 1) & 1));
574                 input_report_abs(dev, ABS_Y, (data[4] << 9) | (data[5] << 1) | (data[9] & 1));
575                 input_report_abs(dev, ABS_DISTANCE, ((data[9] >> 2) & 0x3f));
576         } else {
577                 input_report_abs(dev, ABS_X, be16_to_cpu(*(__be16 *) &data[2]));
578                 input_report_abs(dev, ABS_Y, be16_to_cpu(*(__be16 *) &data[4]));
579                 input_report_abs(dev, ABS_DISTANCE, ((data[9] >> 3) & 0x1f));
580         }
581
582         /* process general packets */
583         wacom_intuos_general(urb);
584
585         /* 4D mouse, 2D mouse, marker pen rotation, or Lens cursor packets */
586         if ((data[1] & 0xbc) == 0xa8 || (data[1] & 0xbe) == 0xb0) {
587
588                 if (data[1] & 0x02) {
589                         /* Rotation packet */
590                         if (wacom->features->type >= INTUOS3) {
591                                 /* I3 marker pen rotation reported as wheel
592                                  * due to valuator limitation
593                                  */
594                                 t = (data[6] << 3) | ((data[7] >> 5) & 7);
595                                 t = (data[7] & 0x20) ? ((t > 900) ? ((t-1) / 2 - 1350) :
596                                         ((t-1) / 2 + 450)) : (450 - t / 2) ;
597                                 input_report_abs(dev, ABS_WHEEL, t);
598                         } else {
599                                 /* 4D mouse rotation packet */
600                                 t = (data[6] << 3) | ((data[7] >> 5) & 7);
601                                 input_report_abs(dev, ABS_RZ, (data[7] & 0x20) ?
602                                         ((t - 1) / 2) : -t / 2);
603                         }
604
605                 } else if (!(data[1] & 0x10) && wacom->features->type < INTUOS3) {
606                         /* 4D mouse packet */
607                         input_report_key(dev, BTN_LEFT,   data[8] & 0x01);
608                         input_report_key(dev, BTN_MIDDLE, data[8] & 0x02);
609                         input_report_key(dev, BTN_RIGHT,  data[8] & 0x04);
610
611                         input_report_key(dev, BTN_SIDE,   data[8] & 0x20);
612                         input_report_key(dev, BTN_EXTRA,  data[8] & 0x10);
613                         t = (data[6] << 2) | ((data[7] >> 6) & 3);
614                         input_report_abs(dev, ABS_THROTTLE, (data[8] & 0x08) ? -t : t);
615
616                 } else if (wacom->tool[idx] == BTN_TOOL_MOUSE) {
617                         /* 2D mouse packet */
618                         input_report_key(dev, BTN_LEFT,   data[8] & 0x04);
619                         input_report_key(dev, BTN_MIDDLE, data[8] & 0x08);
620                         input_report_key(dev, BTN_RIGHT,  data[8] & 0x10);
621                         input_report_rel(dev, REL_WHEEL, ((data[8] & 0x02) >> 1)
622                                                  - (data[8] & 0x01));
623
624                         /* I3 2D mouse side buttons */
625                         if (wacom->features->type == INTUOS3) {
626                                 input_report_key(dev, BTN_SIDE,   data[8] & 0x40);
627                                 input_report_key(dev, BTN_EXTRA,  data[8] & 0x20);
628                         }
629
630                 } else if (wacom->features->type < INTUOS3) {
631                         /* Lens cursor packets */
632                         input_report_key(dev, BTN_LEFT,   data[8] & 0x01);
633                         input_report_key(dev, BTN_MIDDLE, data[8] & 0x02);
634                         input_report_key(dev, BTN_RIGHT,  data[8] & 0x04);
635                         input_report_key(dev, BTN_SIDE,   data[8] & 0x10);
636                         input_report_key(dev, BTN_EXTRA,  data[8] & 0x08);
637                 }
638         }
639
640         input_report_key(dev, wacom->tool[idx], 1);
641         input_event(dev, EV_MSC, MSC_SERIAL, wacom->serial[idx]);
642         input_sync(dev);
643
644 exit:
645         retval = usb_submit_urb (urb, GFP_ATOMIC);
646         if (retval)
647                 err ("%s - usb_submit_urb failed with result %d",
648                     __FUNCTION__, retval);
649 }
650
651 static struct wacom_features wacom_features[] = {
652         { "Wacom Penpartner",    7,   5040,  3780,  255, 32, PENPARTNER, wacom_penpartner_irq },
653         { "Wacom Graphire",      8,  10206,  7422,  511, 32, GRAPHIRE,   wacom_graphire_irq },
654         { "Wacom Graphire2 4x5", 8,  10206,  7422,  511, 32, GRAPHIRE,   wacom_graphire_irq },
655         { "Wacom Graphire2 5x7", 8,  13918, 10206,  511, 32, GRAPHIRE,   wacom_graphire_irq },
656         { "Wacom Graphire3",     8,  10208,  7424,  511, 32, GRAPHIRE,   wacom_graphire_irq },
657         { "Wacom Graphire3 6x8", 8,  16704, 12064,  511, 32, GRAPHIRE,   wacom_graphire_irq },
658         { "Wacom Intuos 4x5",   10,  12700, 10600, 1023, 15, INTUOS,     wacom_intuos_irq },
659         { "Wacom Intuos 6x8",   10,  20320, 16240, 1023, 15, INTUOS,     wacom_intuos_irq },
660         { "Wacom Intuos 9x12",  10,  30480, 24060, 1023, 15, INTUOS,     wacom_intuos_irq },
661         { "Wacom Intuos 12x12", 10,  30480, 31680, 1023, 15, INTUOS,     wacom_intuos_irq },
662         { "Wacom Intuos 12x18", 10,  45720, 31680, 1023, 15, INTUOS,     wacom_intuos_irq },
663         { "Wacom PL400",         8,   5408,  4056,  255, 32, PL,         wacom_pl_irq },
664         { "Wacom PL500",         8,   6144,  4608,  255, 32, PL,         wacom_pl_irq },
665         { "Wacom PL600",         8,   6126,  4604,  255, 32, PL,         wacom_pl_irq },
666         { "Wacom PL600SX",       8,   6260,  5016,  255, 32, PL,         wacom_pl_irq },
667         { "Wacom PL550",         8,   6144,  4608,  511, 32, PL,         wacom_pl_irq },
668         { "Wacom PL800",         8,   7220,  5780,  511, 32, PL,         wacom_pl_irq },
669         { "Wacom Intuos2 4x5",   10, 12700, 10600, 1023, 15, INTUOS,     wacom_intuos_irq },
670         { "Wacom Intuos2 6x8",   10, 20320, 16240, 1023, 15, INTUOS,     wacom_intuos_irq },
671         { "Wacom Intuos2 9x12",  10, 30480, 24060, 1023, 15, INTUOS,     wacom_intuos_irq },
672         { "Wacom Intuos2 12x12", 10, 30480, 31680, 1023, 15, INTUOS,     wacom_intuos_irq },
673         { "Wacom Intuos2 12x18", 10, 45720, 31680, 1023, 15, INTUOS,     wacom_intuos_irq },
674         { "Wacom Volito",        8,   5104,  3712,  511, 32, GRAPHIRE,   wacom_graphire_irq },
675         { "Wacom Cintiq Partner",8,  20480, 15360,  511, 32, PL,         wacom_ptu_irq },
676         { "Wacom Intuos3 4x5",   10, 25400, 20320, 1023, 15, INTUOS3,    wacom_intuos_irq },
677         { "Wacom Intuos3 6x8",   10, 40640, 30480, 1023, 15, INTUOS3,    wacom_intuos_irq },
678         { "Wacom Intuos3 9x12",  10, 60960, 45720, 1023, 15, INTUOS3,    wacom_intuos_irq },
679         { "Wacom Cintiq 21UX",   10, 87200, 65600, 1023, 15, CINTIQ,     wacom_intuos_irq },
680         { "Wacom Intuos2 6x8",   10, 20320, 16240, 1023, 15, INTUOS,     wacom_intuos_irq },
681         { }
682 };
683
684 static struct usb_device_id wacom_ids[] = {
685         { USB_DEVICE(USB_VENDOR_ID_WACOM, 0x00) },
686         { USB_DEVICE(USB_VENDOR_ID_WACOM, 0x10) },
687         { USB_DEVICE(USB_VENDOR_ID_WACOM, 0x11) },
688         { USB_DEVICE(USB_VENDOR_ID_WACOM, 0x12) },
689         { USB_DEVICE(USB_VENDOR_ID_WACOM, 0x13) },
690         { USB_DEVICE(USB_VENDOR_ID_WACOM, 0x14) },
691         { USB_DEVICE(USB_VENDOR_ID_WACOM, 0x20) },
692         { USB_DEVICE(USB_VENDOR_ID_WACOM, 0x21) },
693         { USB_DEVICE(USB_VENDOR_ID_WACOM, 0x22) },
694         { USB_DEVICE(USB_VENDOR_ID_WACOM, 0x23) },
695         { USB_DEVICE(USB_VENDOR_ID_WACOM, 0x24) },
696         { USB_DEVICE(USB_VENDOR_ID_WACOM, 0x30) },
697         { USB_DEVICE(USB_VENDOR_ID_WACOM, 0x31) },
698         { USB_DEVICE(USB_VENDOR_ID_WACOM, 0x32) },
699         { USB_DEVICE(USB_VENDOR_ID_WACOM, 0x33) },
700         { USB_DEVICE(USB_VENDOR_ID_WACOM, 0x34) },
701         { USB_DEVICE(USB_VENDOR_ID_WACOM, 0x35) },
702         { USB_DEVICE(USB_VENDOR_ID_WACOM, 0x41) },
703         { USB_DEVICE(USB_VENDOR_ID_WACOM, 0x42) },
704         { USB_DEVICE(USB_VENDOR_ID_WACOM, 0x43) },
705         { USB_DEVICE(USB_VENDOR_ID_WACOM, 0x44) },
706         { USB_DEVICE(USB_VENDOR_ID_WACOM, 0x45) },
707         { USB_DEVICE(USB_VENDOR_ID_WACOM, 0x60) },
708         { USB_DEVICE(USB_VENDOR_ID_WACOM, 0x03) },
709         { USB_DEVICE(USB_VENDOR_ID_WACOM, 0xB0) },
710         { USB_DEVICE(USB_VENDOR_ID_WACOM, 0xB1) },
711         { USB_DEVICE(USB_VENDOR_ID_WACOM, 0xB2) },
712         { USB_DEVICE(USB_VENDOR_ID_WACOM, 0x3F) },
713         { USB_DEVICE(USB_VENDOR_ID_WACOM, 0x47) },
714         { }
715 };
716
717 MODULE_DEVICE_TABLE(usb, wacom_ids);
718
719 static int wacom_open(struct input_dev *dev)
720 {
721         struct wacom *wacom = dev->private;
722
723         wacom->irq->dev = wacom->usbdev;
724         if (usb_submit_urb(wacom->irq, GFP_KERNEL))
725                 return -EIO;
726
727         return 0;
728 }
729
730 static void wacom_close(struct input_dev *dev)
731 {
732         struct wacom *wacom = dev->private;
733
734         usb_kill_urb(wacom->irq);
735 }
736
737 static int wacom_probe(struct usb_interface *intf, const struct usb_device_id *id)
738 {
739         struct usb_device *dev = interface_to_usbdev(intf);
740         struct usb_endpoint_descriptor *endpoint;
741         char rep_data[2] = {0x02, 0x02};
742         struct wacom *wacom;
743         char path[64];
744
745         if (!(wacom = kmalloc(sizeof(struct wacom), GFP_KERNEL)))
746                 return -ENOMEM;
747         memset(wacom, 0, sizeof(struct wacom));
748
749         wacom->data = usb_buffer_alloc(dev, 10, GFP_KERNEL, &wacom->data_dma);
750         if (!wacom->data) {
751                 kfree(wacom);
752                 return -ENOMEM;
753         }
754
755         wacom->irq = usb_alloc_urb(0, GFP_KERNEL);
756         if (!wacom->irq) {
757                 usb_buffer_free(dev, 10, wacom->data, wacom->data_dma);
758                 kfree(wacom);
759                 return -ENOMEM;
760         }
761
762         wacom->features = wacom_features + (id - wacom_ids);
763
764         wacom->dev.evbit[0] |= BIT(EV_KEY) | BIT(EV_ABS);
765         wacom->dev.absbit[0] |= BIT(ABS_X) | BIT(ABS_Y) | BIT(ABS_PRESSURE);
766         wacom->dev.keybit[LONG(BTN_DIGI)] |= BIT(BTN_TOOL_PEN) | BIT(BTN_TOUCH) | BIT(BTN_STYLUS);
767
768         switch (wacom->features->type) {
769                 case GRAPHIRE:
770                         wacom->dev.evbit[0] |= BIT(EV_REL);
771                         wacom->dev.relbit[0] |= BIT(REL_WHEEL);
772                         wacom->dev.absbit[0] |= BIT(ABS_DISTANCE);
773                         wacom->dev.keybit[LONG(BTN_LEFT)] |= BIT(BTN_LEFT) | BIT(BTN_RIGHT) | BIT(BTN_MIDDLE);
774                         wacom->dev.keybit[LONG(BTN_DIGI)] |= BIT(BTN_TOOL_RUBBER) | BIT(BTN_TOOL_MOUSE) | BIT(BTN_STYLUS2);
775                         break;
776
777                 case INTUOS3:
778                 case CINTIQ:
779                         wacom->dev.keybit[LONG(BTN_DIGI)] |= BIT(BTN_TOOL_FINGER);
780                         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);
781                         wacom->dev.absbit[0] |= BIT(ABS_RX) | BIT(ABS_RY);
782                         /* fall through */
783
784                 case INTUOS:
785                         wacom->dev.evbit[0] |= BIT(EV_MSC) | BIT(EV_REL);
786                         wacom->dev.mscbit[0] |= BIT(MSC_SERIAL);
787                         wacom->dev.relbit[0] |= BIT(REL_WHEEL);
788                         wacom->dev.keybit[LONG(BTN_LEFT)] |= BIT(BTN_LEFT) | BIT(BTN_RIGHT) | BIT(BTN_MIDDLE) | BIT(BTN_SIDE) | BIT(BTN_EXTRA);
789                         wacom->dev.keybit[LONG(BTN_DIGI)] |= BIT(BTN_TOOL_RUBBER) | BIT(BTN_TOOL_MOUSE) | BIT(BTN_TOOL_BRUSH)
790                                                           | BIT(BTN_TOOL_PENCIL) | BIT(BTN_TOOL_AIRBRUSH) | BIT(BTN_TOOL_LENS) | BIT(BTN_STYLUS2);
791                         wacom->dev.absbit[0] |= BIT(ABS_DISTANCE) | BIT(ABS_WHEEL) | BIT(ABS_TILT_X) | BIT(ABS_TILT_Y) | BIT(ABS_RZ) | BIT(ABS_THROTTLE);
792                         break;
793
794                 case PL:
795                         wacom->dev.keybit[LONG(BTN_DIGI)] |= BIT(BTN_STYLUS2) | BIT(BTN_TOOL_RUBBER);
796                         break;
797         }
798
799         wacom->dev.absmax[ABS_X] = wacom->features->x_max;
800         wacom->dev.absmax[ABS_Y] = wacom->features->y_max;
801         wacom->dev.absmax[ABS_PRESSURE] = wacom->features->pressure_max;
802         wacom->dev.absmax[ABS_DISTANCE] = wacom->features->distance_max;
803         wacom->dev.absmax[ABS_TILT_X] = 127;
804         wacom->dev.absmax[ABS_TILT_Y] = 127;
805         wacom->dev.absmax[ABS_WHEEL] = 1023;
806
807         wacom->dev.absmax[ABS_RX] = 4097;
808         wacom->dev.absmax[ABS_RY] = 4097;
809         wacom->dev.absmin[ABS_RZ] = -900;
810         wacom->dev.absmax[ABS_RZ] = 899;
811         wacom->dev.absmin[ABS_THROTTLE] = -1023;
812         wacom->dev.absmax[ABS_THROTTLE] = 1023;
813
814         wacom->dev.absfuzz[ABS_X] = 4;
815         wacom->dev.absfuzz[ABS_Y] = 4;
816
817         wacom->dev.private = wacom;
818         wacom->dev.open = wacom_open;
819         wacom->dev.close = wacom_close;
820
821         usb_make_path(dev, path, 64);
822         sprintf(wacom->phys, "%s/input0", path);
823
824         wacom->dev.name = wacom->features->name;
825         wacom->dev.phys = wacom->phys;
826         usb_to_input_id(dev, &wacom->dev.id);
827         wacom->dev.dev = &intf->dev;
828         wacom->usbdev = dev;
829
830         endpoint = &intf->cur_altsetting->endpoint[0].desc;
831
832         if (wacom->features->pktlen > 10)
833                 BUG();
834
835         usb_fill_int_urb(wacom->irq, dev,
836                          usb_rcvintpipe(dev, endpoint->bEndpointAddress),
837                          wacom->data, wacom->features->pktlen,
838                          wacom->features->irq, wacom, endpoint->bInterval);
839         wacom->irq->transfer_dma = wacom->data_dma;
840         wacom->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
841
842         input_register_device(&wacom->dev);
843
844         /* ask the tablet to report tablet data */
845         usb_set_report(intf, 3, 2, rep_data, 2);
846         /* repeat once (not sure why the first call often fails) */
847         usb_set_report(intf, 3, 2, rep_data, 2);
848
849         printk(KERN_INFO "input: %s on %s\n", wacom->features->name, path);
850
851         usb_set_intfdata(intf, wacom);
852
853         return 0;
854 }
855
856 static void wacom_disconnect(struct usb_interface *intf)
857 {
858         struct wacom *wacom = usb_get_intfdata (intf);
859
860         usb_set_intfdata(intf, NULL);
861         if (wacom) {
862                 usb_kill_urb(wacom->irq);
863                 input_unregister_device(&wacom->dev);
864                 usb_free_urb(wacom->irq);
865                 usb_buffer_free(interface_to_usbdev(intf), 10, wacom->data, wacom->data_dma);
866                 kfree(wacom);
867         }
868 }
869
870 static struct usb_driver wacom_driver = {
871         .owner =        THIS_MODULE,
872         .name =         "wacom",
873         .probe =        wacom_probe,
874         .disconnect =   wacom_disconnect,
875         .id_table =     wacom_ids,
876 };
877
878 static int __init wacom_init(void)
879 {
880         int result = usb_register(&wacom_driver);
881         if (result == 0)
882                 info(DRIVER_VERSION ":" DRIVER_DESC);
883         return result;
884 }
885
886 static void __exit wacom_exit(void)
887 {
888         usb_deregister(&wacom_driver);
889 }
890
891 module_init(wacom_init);
892 module_exit(wacom_exit);