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