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