Merge 'upstream' branch of rsync://rsync.kernel.org/pub/scm/linux/kernel/git/jgarzik...
[linux-2.6] / 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                 } else {
182                         /* was entered with stylus2 pressed */
183                         if (wacom->tool[1] == BTN_TOOL_RUBBER && !(data[4] & 0x20) ) {
184                                 /* report out proximity for previous tool */
185                                 input_report_key(dev, wacom->tool[1], 0);
186                                 input_sync(dev);
187                                 wacom->tool[1] = BTN_TOOL_PEN;
188                                 goto exit;
189                         }
190                 }
191                 if (wacom->tool[1] != BTN_TOOL_RUBBER) {
192                         /* Unknown tool selected default to pen tool */
193                         wacom->tool[1] = BTN_TOOL_PEN;
194                 }
195                 input_report_key(dev, wacom->tool[1], prox); /* report in proximity for tool */
196                 input_report_abs(dev, ABS_X, data[3] | (data[2] << 7) | ((data[1] & 0x03) << 14));
197                 input_report_abs(dev, ABS_Y, data[6] | (data[5] << 7) | ((data[4] & 0x03) << 14));
198                 input_report_abs(dev, ABS_PRESSURE, pressure);
199
200                 input_report_key(dev, BTN_TOUCH, data[4] & 0x08);
201                 input_report_key(dev, BTN_STYLUS, data[4] & 0x10);
202                 /* Only allow the stylus2 button to be reported for the pen tool. */
203                 input_report_key(dev, BTN_STYLUS2, (wacom->tool[1] == BTN_TOOL_PEN) && (data[4] & 0x20));
204         } else {
205                 /* report proximity-out of a (valid) tool */
206                 if (wacom->tool[1] != BTN_TOOL_RUBBER) {
207                         /* Unknown tool selected default to pen tool */
208                         wacom->tool[1] = BTN_TOOL_PEN;
209                 }
210                 input_report_key(dev, wacom->tool[1], prox);
211         }
212
213         wacom->tool[0] = prox; /* Save proximity state */
214         input_sync(dev);
215
216  exit:
217         retval = usb_submit_urb (urb, GFP_ATOMIC);
218         if (retval)
219                 err ("%s - usb_submit_urb failed with result %d",
220                      __FUNCTION__, retval);
221 }
222
223 static void wacom_ptu_irq(struct urb *urb, struct pt_regs *regs)
224 {
225         struct wacom *wacom = urb->context;
226         unsigned char *data = wacom->data;
227         struct input_dev *dev = &wacom->dev;
228         int retval;
229
230         switch (urb->status) {
231         case 0:
232                 /* success */
233                 break;
234         case -ECONNRESET:
235         case -ENOENT:
236         case -ESHUTDOWN:
237                 /* this urb is terminated, clean up */
238                 dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
239                 return;
240         default:
241                 dbg("%s - nonzero urb status received: %d", __FUNCTION__, urb->status);
242                 goto exit;
243         }
244
245         if (data[0] != 2) {
246                 printk(KERN_INFO "wacom_ptu_irq: received unknown report #%d\n", data[0]);
247                 goto exit;
248         }
249
250         input_regs(dev, regs);
251         if (data[1] & 0x04) {
252                 input_report_key(dev, BTN_TOOL_RUBBER, data[1] & 0x20);
253                 input_report_key(dev, BTN_TOUCH, data[1] & 0x08);
254         } else {
255                 input_report_key(dev, BTN_TOOL_PEN, data[1] & 0x20);
256                 input_report_key(dev, BTN_TOUCH, data[1] & 0x01);
257         }
258         input_report_abs(dev, ABS_X, le16_to_cpu(*(__le16 *) &data[2]));
259         input_report_abs(dev, ABS_Y, le16_to_cpu(*(__le16 *) &data[4]));
260         input_report_abs(dev, ABS_PRESSURE, le16_to_cpu(*(__le16 *) &data[6]));
261         input_report_key(dev, BTN_STYLUS, data[1] & 0x02);
262         input_report_key(dev, BTN_STYLUS2, data[1] & 0x10);
263
264         input_sync(dev);
265
266  exit:
267         retval = usb_submit_urb (urb, GFP_ATOMIC);
268         if (retval)
269                 err ("%s - usb_submit_urb failed with result %d",
270                      __FUNCTION__, retval);
271 }
272
273 static void wacom_penpartner_irq(struct urb *urb, struct pt_regs *regs)
274 {
275         struct wacom *wacom = urb->context;
276         unsigned char *data = wacom->data;
277         struct input_dev *dev = &wacom->dev;
278         int retval;
279
280         switch (urb->status) {
281         case 0:
282                 /* success */
283                 break;
284         case -ECONNRESET:
285         case -ENOENT:
286         case -ESHUTDOWN:
287                 /* this urb is terminated, clean up */
288                 dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
289                 return;
290         default:
291                 dbg("%s - nonzero urb status received: %d", __FUNCTION__, urb->status);
292                 goto exit;
293         }
294
295         if (data[0] != 2) {
296                 printk(KERN_INFO "wacom_penpartner_irq: received unknown report #%d\n", data[0]);
297                 goto exit;
298         }
299
300         input_regs(dev, regs);
301         input_report_key(dev, BTN_TOOL_PEN, 1);
302         input_report_abs(dev, ABS_X, le16_to_cpu(*(__le16 *) &data[1]));
303         input_report_abs(dev, ABS_Y, le16_to_cpu(*(__le16 *) &data[3]));
304         input_report_abs(dev, ABS_PRESSURE, (signed char)data[6] + 127);
305         input_report_key(dev, BTN_TOUCH, ((signed char)data[6] > -80) && !(data[5] & 0x20));
306         input_report_key(dev, BTN_STYLUS, (data[5] & 0x40));
307         input_sync(dev);
308
309  exit:
310         retval = usb_submit_urb (urb, GFP_ATOMIC);
311         if (retval)
312                 err ("%s - usb_submit_urb failed with result %d",
313                      __FUNCTION__, retval);
314 }
315
316 static void wacom_graphire_irq(struct urb *urb, struct pt_regs *regs)
317 {
318         struct wacom *wacom = urb->context;
319         unsigned char *data = wacom->data;
320         struct input_dev *dev = &wacom->dev;
321         int x, y;
322         int retval;
323
324         switch (urb->status) {
325         case 0:
326                 /* success */
327                 break;
328         case -ECONNRESET:
329         case -ENOENT:
330         case -ESHUTDOWN:
331                 /* this urb is terminated, clean up */
332                 dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
333                 return;
334         default:
335                 dbg("%s - nonzero urb status received: %d", __FUNCTION__, urb->status);
336                 goto exit;
337         }
338
339         if (data[0] != 2) {
340                 dbg("wacom_graphire_irq: received unknown report #%d", data[0]);
341                 goto exit;
342         }
343
344         x = le16_to_cpu(*(__le16 *) &data[2]);
345         y = le16_to_cpu(*(__le16 *) &data[4]);
346
347         input_regs(dev, regs);
348
349         if (data[1] & 0x10) { /* in prox */
350
351                 switch ((data[1] >> 5) & 3) {
352
353                         case 0: /* Pen */
354                                 wacom->tool[0] = BTN_TOOL_PEN;
355                                 break;
356
357                         case 1: /* Rubber */
358                                 wacom->tool[0] = BTN_TOOL_RUBBER;
359                                 break;
360
361                         case 2: /* Mouse with wheel */
362                                 input_report_key(dev, BTN_MIDDLE, data[1] & 0x04);
363                                 input_report_rel(dev, REL_WHEEL, (signed char) data[6]);
364                                 /* fall through */
365
366                         case 3: /* Mouse without wheel */
367                                 wacom->tool[0] = BTN_TOOL_MOUSE;
368                                 input_report_key(dev, BTN_LEFT, data[1] & 0x01);
369                                 input_report_key(dev, BTN_RIGHT, data[1] & 0x02);
370                                 input_report_abs(dev, ABS_DISTANCE, data[7]);
371                                 break;
372                 }
373         }
374
375         if (data[1] & 0x80) {
376                 input_report_abs(dev, ABS_X, x);
377                 input_report_abs(dev, ABS_Y, y);
378         }
379         if (wacom->tool[0] != BTN_TOOL_MOUSE) {
380                 input_report_abs(dev, ABS_PRESSURE, le16_to_cpu(*(__le16 *) &data[6]));
381                 input_report_key(dev, BTN_TOUCH, data[1] & 0x01);
382                 input_report_key(dev, BTN_STYLUS, data[1] & 0x02);
383                 input_report_key(dev, BTN_STYLUS2, data[1] & 0x04);
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                 return;
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         wacom->dev.id.bustype = BUS_USB;
827         wacom->dev.id.vendor = le16_to_cpu(dev->descriptor.idVendor);
828         wacom->dev.id.product = le16_to_cpu(dev->descriptor.idProduct);
829         wacom->dev.id.version = le16_to_cpu(dev->descriptor.bcdDevice);
830         wacom->dev.dev = &intf->dev;
831         wacom->usbdev = dev;
832
833         endpoint = &intf->cur_altsetting->endpoint[0].desc;
834
835         if (wacom->features->pktlen > 10)
836                 BUG();
837
838         usb_fill_int_urb(wacom->irq, dev,
839                          usb_rcvintpipe(dev, endpoint->bEndpointAddress),
840                          wacom->data, wacom->features->pktlen,
841                          wacom->features->irq, wacom, endpoint->bInterval);
842         wacom->irq->transfer_dma = wacom->data_dma;
843         wacom->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
844
845         input_register_device(&wacom->dev);
846
847         /* ask the tablet to report tablet data */
848         usb_set_report(intf, 3, 2, rep_data, 2);
849         /* repeat once (not sure why the first call often fails) */
850         usb_set_report(intf, 3, 2, rep_data, 2);
851
852         printk(KERN_INFO "input: %s on %s\n", wacom->features->name, path);
853
854         usb_set_intfdata(intf, wacom);
855
856         return 0;
857 }
858
859 static void wacom_disconnect(struct usb_interface *intf)
860 {
861         struct wacom *wacom = usb_get_intfdata (intf);
862
863         usb_set_intfdata(intf, NULL);
864         if (wacom) {
865                 usb_kill_urb(wacom->irq);
866                 input_unregister_device(&wacom->dev);
867                 usb_free_urb(wacom->irq);
868                 usb_buffer_free(interface_to_usbdev(intf), 10, wacom->data, wacom->data_dma);
869                 kfree(wacom);
870         }
871 }
872
873 static struct usb_driver wacom_driver = {
874         .owner =        THIS_MODULE,
875         .name =         "wacom",
876         .probe =        wacom_probe,
877         .disconnect =   wacom_disconnect,
878         .id_table =     wacom_ids,
879 };
880
881 static int __init wacom_init(void)
882 {
883         int result = usb_register(&wacom_driver);
884         if (result == 0)
885                 info(DRIVER_VERSION ":" DRIVER_DESC);
886         return result;
887 }
888
889 static void __exit wacom_exit(void)
890 {
891         usb_deregister(&wacom_driver);
892 }
893
894 module_init(wacom_init);
895 module_exit(wacom_exit);