147ed340e6e13d00e425fc2e8d79f11874d7b185
[powerpc.git] / drivers / input / tablet / aiptek.c
1 /*
2  *  Native support for the Aiptek HyperPen USB Tablets
3  *  (4000U/5000U/6000U/8000U/12000U)
4  *
5  *  Copyright (c) 2001      Chris Atenasio   <chris@crud.net>
6  *  Copyright (c) 2002-2004 Bryan W. Headley <bwheadley@earthlink.net>
7  *
8  *  based on wacom.c by
9  *     Vojtech Pavlik      <vojtech@suse.cz>
10  *     Andreas Bach Aaen   <abach@stofanet.dk>
11  *     Clifford Wolf       <clifford@clifford.at>
12  *     Sam Mosel           <sam.mosel@computer.org>
13  *     James E. Blair      <corvus@gnu.org>
14  *     Daniel Egger        <egger@suse.de>
15  *
16  *  Many thanks to Oliver Kuechemann for his support.
17  *
18  *  ChangeLog:
19  *      v0.1 - Initial release
20  *      v0.2 - Hack to get around fake event 28's. (Bryan W. Headley)
21  *      v0.3 - Make URB dynamic (Bryan W. Headley, Jun-8-2002)
22  *             Released to Linux 2.4.19 and 2.5.x
23  *      v0.4 - Rewrote substantial portions of the code to deal with
24  *             corrected control sequences, timing, dynamic configuration,
25  *             support of 6000U - 12000U, procfs, and macro key support
26  *             (Jan-1-2003 - Feb-5-2003, Bryan W. Headley)
27  *      v1.0 - Added support for diagnostic messages, count of messages
28  *             received from URB - Mar-8-2003, Bryan W. Headley
29  *      v1.1 - added support for tablet resolution, changed DV and proximity
30  *             some corrections - Jun-22-2003, martin schneebacher
31  *           - Added support for the sysfs interface, deprecating the
32  *             procfs interface for 2.5.x kernel. Also added support for
33  *             Wheel command. Bryan W. Headley July-15-2003.
34  *      v1.2 - Reworked jitter timer as a kernel thread.
35  *             Bryan W. Headley November-28-2003/Jan-10-2004.
36  *      v1.3 - Repaired issue of kernel thread going nuts on single-processor
37  *             machines, introduced programmableDelay as a command line
38  *             parameter. Feb 7 2004, Bryan W. Headley.
39  *      v1.4 - Re-wire jitter so it does not require a thread. Courtesy of
40  *             Rene van Paassen. Added reporting of physical pointer device
41  *             (e.g., stylus, mouse in reports 2, 3, 4, 5. We don't know
42  *             for reports 1, 6.)
43  *             what physical device reports for reports 1, 6.) Also enabled
44  *             MOUSE and LENS tool button modes. Renamed "rubber" to "eraser".
45  *             Feb 20, 2004, Bryan W. Headley.
46  *      v1.5 - Added previousJitterable, so we don't do jitter delay when the
47  *             user is holding a button down for periods of time.
48  *
49  * NOTE:
50  *      This kernel driver is augmented by the "Aiptek" XFree86 input
51  *      driver for your X server, as well as the Gaiptek GUI Front-end
52  *      "Tablet Manager".
53  *      These three products are highly interactive with one another,
54  *      so therefore it's easier to document them all as one subsystem.
55  *      Please visit the project's "home page", located at,
56  *      http://aiptektablet.sourceforge.net.
57  *
58  * This program is free software; you can redistribute it and/or modify
59  * it under the terms of the GNU General Public License as published by
60  * the Free Software Foundation; either version 2 of the License, or
61  * (at your option) any later version.
62  *
63  * This program is distributed in the hope that it will be useful,
64  * but WITHOUT ANY WARRANTY; without even the implied warranty of
65  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
66  * GNU General Public License for more details.
67  *
68  * You should have received a copy of the GNU General Public License
69  * along with this program; if not, write to the Free Software
70  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
71  */
72
73 #include <linux/jiffies.h>
74 #include <linux/kernel.h>
75 #include <linux/slab.h>
76 #include <linux/module.h>
77 #include <linux/init.h>
78 #include <linux/usb/input.h>
79 #include <asm/uaccess.h>
80 #include <asm/unaligned.h>
81
82 /*
83  * Version Information
84  */
85 #define DRIVER_VERSION "v1.5 (May-15-2004)"
86 #define DRIVER_AUTHOR  "Bryan W. Headley/Chris Atenasio"
87 #define DRIVER_DESC    "Aiptek HyperPen USB Tablet Driver (Linux 2.6.x)"
88
89 /*
90  * Aiptek status packet:
91  *
92  * (returned as Report 1 - relative coordinates from mouse and stylus)
93  *
94  *        bit7  bit6  bit5  bit4  bit3  bit2  bit1  bit0
95  * byte0   0     0     0     0     0     0     0     1
96  * byte1   0     0     0     0     0    BS2   BS    Tip
97  * byte2  X7    X6    X5    X4    X3    X2    X1    X0
98  * byte3  Y7    Y6    Y5    Y4    Y3    Y2    Y1    Y0
99  *
100  * (returned as Report 2 - absolute coordinates from the stylus)
101  *
102  *        bit7  bit6  bit5  bit4  bit3  bit2  bit1  bit0
103  * byte0   0     0     0     0     0     0     1     0
104  * byte1  X7    X6    X5    X4    X3    X2    X1    X0
105  * byte2  X15   X14   X13   X12   X11   X10   X9    X8
106  * byte3  Y7    Y6    Y5    Y4    Y3    Y2    Y1    Y0
107  * byte4  Y15   Y14   Y13   Y12   Y11   Y10   Y9    Y8
108  * byte5   *     *     *    BS2   BS1   Tip   IR    DV
109  * byte6  P7    P6    P5    P4    P3    P2    P1    P0
110  * byte7  P15   P14   P13   P12   P11   P10   P9    P8
111  *
112  * (returned as Report 3 - absolute coordinates from the mouse)
113  *
114  *        bit7  bit6  bit5  bit4  bit3  bit2  bit1  bit0
115  * byte0   0     0     0     0     0     0     1     1
116  * byte1  X7    X6    X5    X4    X3    X2    X1    X0
117  * byte2  X15   X14   X13   X12   X11   X10   X9    X8
118  * byte3  Y7    Y6    Y5    Y4    Y3    Y2    Y1    Y0
119  * byte4  Y15   Y14   Y13   Y12   Y11   Y10   Y9    Y8
120  * byte5   *     *     *    BS2   BS1   Tip   IR    DV
121  * byte6  P7    P6    P5    P4    P3    P2    P1    P0
122  * byte7  P15   P14   P13   P12   P11   P10   P9    P8
123  *
124  * (returned as Report 4 - macrokeys from the stylus)
125  *
126  *        bit7  bit6  bit5  bit4  bit3  bit2  bit1  bit0
127  * byte0   0     0     0     0     0     1     0     0
128  * byte1   0     0     0    BS2   BS    Tip   IR    DV
129  * byte2   0     0     0     0     0     0     1     0
130  * byte3   0     0     0    K4    K3    K2    K1    K0
131  * byte4  P7    P6    P5    P4    P3    P2    P1    P0
132  * byte5  P15   P14   P13   P12   P11   P10   P9    P8
133  *
134  * (returned as Report 5 - macrokeys from the mouse)
135  *
136  *        bit7  bit6  bit5  bit4  bit3  bit2  bit1  bit0
137  * byte0   0     0     0     0     0     1     0     1
138  * byte1   0     0     0    BS2   BS    Tip   IR    DV
139  * byte2   0     0     0     0     0     0     1     0
140  * byte3   0     0     0    K4    K3    K2    K1    K0
141  * byte4  P7    P6    P5    P4    P3    P2    P1    P0
142  * byte5  P15   P14   P13   P12   P11   P10   P9    P8
143  *
144  * IR: In Range = Proximity on
145  * DV = Data Valid
146  * BS = Barrel Switch (as in, macro keys)
147  * BS2 also referred to as Tablet Pick
148  *
149  * Command Summary:
150  *
151  * Use report_type CONTROL (3)
152  * Use report_id   2
153  *
154  * Command/Data    Description     Return Bytes    Return Value
155  * 0x10/0x00       SwitchToMouse       0
156  * 0x10/0x01       SwitchToTablet      0
157  * 0x18/0x04       SetResolution       0
158  * 0x12/0xFF       AutoGainOn          0
159  * 0x17/0x00       FilterOn            0
160  * 0x01/0x00       GetXExtension       2           MaxX
161  * 0x01/0x01       GetYExtension       2           MaxY
162  * 0x02/0x00       GetModelCode        2           ModelCode = LOBYTE
163  * 0x03/0x00       GetODMCode          2           ODMCode
164  * 0x08/0x00       GetPressureLevels   2           =512
165  * 0x04/0x00       GetFirmwareVersion  2           Firmware Version
166  * 0x11/0x02       EnableMacroKeys     0
167  *
168  * To initialize the tablet:
169  *
170  * (1) Send Resolution500LPI (Command)
171  * (2) Query for Model code (Option Report)
172  * (3) Query for ODM code (Option Report)
173  * (4) Query for firmware (Option Report)
174  * (5) Query for GetXExtension (Option Report)
175  * (6) Query for GetYExtension (Option Report)
176  * (7) Query for GetPressureLevels (Option Report)
177  * (8) SwitchToTablet for Absolute coordinates, or
178  *     SwitchToMouse for Relative coordinates (Command)
179  * (9) EnableMacroKeys (Command)
180  * (10) FilterOn (Command)
181  * (11) AutoGainOn (Command)
182  *
183  * (Step 9 can be omitted, but you'll then have no function keys.)
184  */
185
186 #define USB_VENDOR_ID_AIPTEK                            0x08ca
187 #define USB_REQ_GET_REPORT                              0x01
188 #define USB_REQ_SET_REPORT                              0x09
189
190         /* PointerMode codes
191          */
192 #define AIPTEK_POINTER_ONLY_MOUSE_MODE                  0
193 #define AIPTEK_POINTER_ONLY_STYLUS_MODE                 1
194 #define AIPTEK_POINTER_EITHER_MODE                      2
195
196 #define AIPTEK_POINTER_ALLOW_MOUSE_MODE(a)              \
197         (a == AIPTEK_POINTER_ONLY_MOUSE_MODE ||         \
198          a == AIPTEK_POINTER_EITHER_MODE)
199 #define AIPTEK_POINTER_ALLOW_STYLUS_MODE(a)             \
200         (a == AIPTEK_POINTER_ONLY_STYLUS_MODE ||        \
201          a == AIPTEK_POINTER_EITHER_MODE)
202
203         /* CoordinateMode code
204          */
205 #define AIPTEK_COORDINATE_RELATIVE_MODE                 0
206 #define AIPTEK_COORDINATE_ABSOLUTE_MODE                 1
207
208        /* XTilt and YTilt values
209         */
210 #define AIPTEK_TILT_MIN                                 (-128)
211 #define AIPTEK_TILT_MAX                                 127
212 #define AIPTEK_TILT_DISABLE                             (-10101)
213
214         /* Wheel values
215          */
216 #define AIPTEK_WHEEL_MIN                                0
217 #define AIPTEK_WHEEL_MAX                                1024
218 #define AIPTEK_WHEEL_DISABLE                            (-10101)
219
220         /* ToolCode values, which BTW are 0x140 .. 0x14f
221          * We have things set up such that if TOOL_BUTTON_FIRED_BIT is
222          * not set, we'll send one instance of AIPTEK_TOOL_BUTTON_xxx.
223          *
224          * Whenever the user resets the value, TOOL_BUTTON_FIRED_BIT will
225          * get reset.
226          */
227 #define TOOL_BUTTON(x)                                  ((x) & 0x14f)
228 #define TOOL_BUTTON_FIRED(x)                            ((x) & 0x200)
229 #define TOOL_BUTTON_FIRED_BIT                           0x200
230         /* toolMode codes
231          */
232 #define AIPTEK_TOOL_BUTTON_PEN_MODE                     BTN_TOOL_PEN
233 #define AIPTEK_TOOL_BUTTON_PEN_MODE                     BTN_TOOL_PEN
234 #define AIPTEK_TOOL_BUTTON_PENCIL_MODE                  BTN_TOOL_PENCIL
235 #define AIPTEK_TOOL_BUTTON_BRUSH_MODE                   BTN_TOOL_BRUSH
236 #define AIPTEK_TOOL_BUTTON_AIRBRUSH_MODE                BTN_TOOL_AIRBRUSH
237 #define AIPTEK_TOOL_BUTTON_ERASER_MODE                  BTN_TOOL_RUBBER
238 #define AIPTEK_TOOL_BUTTON_MOUSE_MODE                   BTN_TOOL_MOUSE
239 #define AIPTEK_TOOL_BUTTON_LENS_MODE                    BTN_TOOL_LENS
240
241         /* Diagnostic message codes
242          */
243 #define AIPTEK_DIAGNOSTIC_NA                            0
244 #define AIPTEK_DIAGNOSTIC_SENDING_RELATIVE_IN_ABSOLUTE  1
245 #define AIPTEK_DIAGNOSTIC_SENDING_ABSOLUTE_IN_RELATIVE  2
246 #define AIPTEK_DIAGNOSTIC_TOOL_DISALLOWED               3
247
248         /* Time to wait (in ms) to help mask hand jittering
249          * when pressing the stylus buttons.
250          */
251 #define AIPTEK_JITTER_DELAY_DEFAULT                     50
252
253         /* Time to wait (in ms) in-between sending the tablet
254          * a command and beginning the process of reading the return
255          * sequence from the tablet.
256          */
257 #define AIPTEK_PROGRAMMABLE_DELAY_25            25
258 #define AIPTEK_PROGRAMMABLE_DELAY_50            50
259 #define AIPTEK_PROGRAMMABLE_DELAY_100           100
260 #define AIPTEK_PROGRAMMABLE_DELAY_200           200
261 #define AIPTEK_PROGRAMMABLE_DELAY_300           300
262 #define AIPTEK_PROGRAMMABLE_DELAY_400           400
263 #define AIPTEK_PROGRAMMABLE_DELAY_DEFAULT       AIPTEK_PROGRAMMABLE_DELAY_400
264
265         /* Mouse button programming
266          */
267 #define AIPTEK_MOUSE_LEFT_BUTTON                0x04
268 #define AIPTEK_MOUSE_RIGHT_BUTTON               0x08
269 #define AIPTEK_MOUSE_MIDDLE_BUTTON              0x10
270
271         /* Stylus button programming
272          */
273 #define AIPTEK_STYLUS_LOWER_BUTTON              0x08
274 #define AIPTEK_STYLUS_UPPER_BUTTON              0x10
275
276         /* Length of incoming packet from the tablet
277          */
278 #define AIPTEK_PACKET_LENGTH                    8
279
280         /* We report in EV_MISC both the proximity and
281          * whether the report came from the stylus, tablet mouse
282          * or "unknown" -- Unknown when the tablet is in relative
283          * mode, because we only get report 1's.
284          */
285 #define AIPTEK_REPORT_TOOL_UNKNOWN              0x10
286 #define AIPTEK_REPORT_TOOL_STYLUS               0x20
287 #define AIPTEK_REPORT_TOOL_MOUSE                0x40
288
289 static int programmableDelay = AIPTEK_PROGRAMMABLE_DELAY_DEFAULT;
290 static int jitterDelay = AIPTEK_JITTER_DELAY_DEFAULT;
291
292 struct aiptek_features {
293         int odmCode;            /* Tablet manufacturer code       */
294         int modelCode;          /* Tablet model code (not unique) */
295         int firmwareCode;       /* prom/eeprom version            */
296         char usbPath[64 + 1];   /* device's physical usb path     */
297 };
298
299 struct aiptek_settings {
300         int pointerMode;        /* stylus-, mouse-only or either */
301         int coordinateMode;     /* absolute/relative coords      */
302         int toolMode;           /* pen, pencil, brush, etc. tool */
303         int xTilt;              /* synthetic xTilt amount        */
304         int yTilt;              /* synthetic yTilt amount        */
305         int wheel;              /* synthetic wheel amount        */
306         int stylusButtonUpper;  /* stylus upper btn delivers...  */
307         int stylusButtonLower;  /* stylus lower btn delivers...  */
308         int mouseButtonLeft;    /* mouse left btn delivers...    */
309         int mouseButtonMiddle;  /* mouse middle btn delivers...  */
310         int mouseButtonRight;   /* mouse right btn delivers...   */
311         int programmableDelay;  /* delay for tablet programming  */
312         int jitterDelay;        /* delay for hand jittering      */
313 };
314
315 struct aiptek {
316         struct input_dev *inputdev;             /* input device struct           */
317         struct usb_device *usbdev;              /* usb device struct             */
318         struct urb *urb;                        /* urb for incoming reports      */
319         dma_addr_t data_dma;                    /* our dma stuffage              */
320         struct aiptek_features features;        /* tablet's array of features    */
321         struct aiptek_settings curSetting;      /* tablet's current programmable */
322         struct aiptek_settings newSetting;      /* ... and new param settings    */
323         unsigned int ifnum;                     /* interface number for IO       */
324         int diagnostic;                         /* tablet diagnostic codes       */
325         unsigned long eventCount;               /* event count                   */
326         int inDelay;                            /* jitter: in jitter delay?      */
327         unsigned long endDelay;                 /* jitter: time when delay ends  */
328         int previousJitterable;                 /* jitterable prev value     */
329         unsigned char *data;                    /* incoming packet data          */
330 };
331
332 static const int buttonEvents[] = {
333         BTN_LEFT, BTN_RIGHT, BTN_MIDDLE,
334         BTN_TOOL_PEN, BTN_TOOL_RUBBER, BTN_TOOL_PENCIL, BTN_TOOL_AIRBRUSH,
335         BTN_TOOL_BRUSH, BTN_TOOL_MOUSE, BTN_TOOL_LENS, BTN_TOUCH,
336         BTN_STYLUS, BTN_STYLUS2,
337 };
338
339 /*
340  * Permit easy lookup of keyboard events to send, versus
341  * the bitmap which comes from the tablet. This hides the
342  * issue that the F_keys are not sequentially numbered.
343  */
344 static const int macroKeyEvents[] = {
345         KEY_ESC, KEY_F1, KEY_F2, KEY_F3, KEY_F4, KEY_F5,
346         KEY_F6, KEY_F7, KEY_F8, KEY_F9, KEY_F10, KEY_F11,
347         KEY_F12, KEY_F13, KEY_F14, KEY_F15, KEY_F16, KEY_F17,
348         KEY_F18, KEY_F19, KEY_F20, KEY_F21, KEY_F22, KEY_F23,
349         KEY_F24, KEY_STOP, KEY_AGAIN, KEY_PROPS, KEY_UNDO,
350         KEY_FRONT, KEY_COPY, KEY_OPEN, KEY_PASTE, 0
351 };
352
353 /***********************************************************************
354  * Map values to strings and back. Every map shoudl have the following
355  * as its last element: { NULL, AIPTEK_INVALID_VALUE }.
356  */
357 #define AIPTEK_INVALID_VALUE    -1
358
359 struct aiptek_map {
360         const char *string;
361         int value;
362 };
363
364 static int map_str_to_val(const struct aiptek_map *map, const char *str, size_t count)
365 {
366         const struct aiptek_map *p;
367
368         for (p = map; p->string; p++)
369                 if (!strncmp(str, p->string, count))
370                         return p->value;
371
372         return AIPTEK_INVALID_VALUE;
373 }
374
375 static const char *map_val_to_str(const struct aiptek_map *map, int val)
376 {
377         const struct aiptek_map *p;
378
379         for (p = map; p->value != AIPTEK_INVALID_VALUE; p++)
380                 if (val == p->value)
381                         return p->string;
382
383         return "unknown";
384 }
385
386 /***********************************************************************
387  * aiptek_irq can receive one of six potential reports.
388  * The documentation for each is in the body of the function.
389  *
390  * The tablet reports on several attributes per invocation of
391  * aiptek_irq. Because the Linux Input Event system allows the
392  * transmission of ONE attribute per input_report_xxx() call,
393  * collation has to be done on the other end to reconstitute
394  * a complete tablet report. Further, the number of Input Event reports
395  * submitted varies, depending on what USB report type, and circumstance.
396  * To deal with this, EV_MSC is used to indicate an 'end-of-report'
397  * message. This has been an undocumented convention understood by the kernel
398  * tablet driver and clients such as gpm and XFree86's tablet drivers.
399  *
400  * Of the information received from the tablet, the one piece I
401  * cannot transmit is the proximity bit (without resorting to an EV_MSC
402  * convention above.) I therefore have taken over REL_MISC and ABS_MISC
403  * (for relative and absolute reports, respectively) for communicating
404  * Proximity. Why two events? I thought it interesting to know if the
405  * Proximity event occurred while the tablet was in absolute or relative
406  * mode.
407  *
408  * Other tablets use the notion of a certain minimum stylus pressure
409  * to infer proximity. While that could have been done, that is yet
410  * another 'by convention' behavior, the documentation for which
411  * would be spread between two (or more) pieces of software.
412  *
413  * EV_MSC usage was terminated for this purpose in Linux 2.5.x, and
414  * replaced with the input_sync() method (which emits EV_SYN.)
415  */
416
417 static void aiptek_irq(struct urb *urb)
418 {
419         struct aiptek *aiptek = urb->context;
420         unsigned char *data = aiptek->data;
421         struct input_dev *inputdev = aiptek->inputdev;
422         int jitterable = 0;
423         int retval, macro, x, y, z, left, right, middle, p, dv, tip, bs, pck;
424
425         switch (urb->status) {
426         case 0:
427                 /* Success */
428                 break;
429
430         case -ECONNRESET:
431         case -ENOENT:
432         case -ESHUTDOWN:
433                 /* This urb is terminated, clean up */
434                 dbg("%s - urb shutting down with status: %d",
435                     __FUNCTION__, urb->status);
436                 return;
437
438         default:
439                 dbg("%s - nonzero urb status received: %d",
440                     __FUNCTION__, urb->status);
441                 goto exit;
442         }
443
444         /* See if we are in a delay loop -- throw out report if true.
445          */
446         if (aiptek->inDelay == 1 && time_after(aiptek->endDelay, jiffies)) {
447                 goto exit;
448         }
449
450         aiptek->inDelay = 0;
451         aiptek->eventCount++;
452
453         /* Report 1 delivers relative coordinates with either a stylus
454          * or the mouse. You do not know, however, which input
455          * tool generated the event.
456          */
457         if (data[0] == 1) {
458                 if (aiptek->curSetting.coordinateMode ==
459                     AIPTEK_COORDINATE_ABSOLUTE_MODE) {
460                         aiptek->diagnostic =
461                             AIPTEK_DIAGNOSTIC_SENDING_RELATIVE_IN_ABSOLUTE;
462                 } else {
463                         x = (signed char) data[2];
464                         y = (signed char) data[3];
465
466                         /* jitterable keeps track of whether any button has been pressed.
467                          * We're also using it to remap the physical mouse button mask
468                          * to pseudo-settings. (We don't specifically care about it's
469                          * value after moving/transposing mouse button bitmasks, except
470                          * that a non-zero value indicates that one or more
471                          * mouse button was pressed.)
472                          */
473                         jitterable = data[5] & 0x07;
474
475                         left = (data[5] & aiptek->curSetting.mouseButtonLeft) != 0 ? 1 : 0;
476                         right = (data[5] & aiptek->curSetting.mouseButtonRight) != 0 ? 1 : 0;
477                         middle = (data[5] & aiptek->curSetting.mouseButtonMiddle) != 0 ? 1 : 0;
478
479                         input_report_key(inputdev, BTN_LEFT, left);
480                         input_report_key(inputdev, BTN_MIDDLE, middle);
481                         input_report_key(inputdev, BTN_RIGHT, right);
482                         input_report_rel(inputdev, REL_X, x);
483                         input_report_rel(inputdev, REL_Y, y);
484                         input_report_rel(inputdev, REL_MISC, 1 | AIPTEK_REPORT_TOOL_UNKNOWN);
485
486                         /* Wheel support is in the form of a single-event
487                          * firing.
488                          */
489                         if (aiptek->curSetting.wheel != AIPTEK_WHEEL_DISABLE) {
490                                 input_report_rel(inputdev, REL_WHEEL,
491                                                  aiptek->curSetting.wheel);
492                                 aiptek->curSetting.wheel = AIPTEK_WHEEL_DISABLE;
493                         }
494                         input_sync(inputdev);
495                 }
496         }
497         /* Report 2 is delivered only by the stylus, and delivers
498          * absolute coordinates.
499          */
500         else if (data[0] == 2) {
501                 if (aiptek->curSetting.coordinateMode == AIPTEK_COORDINATE_RELATIVE_MODE) {
502                         aiptek->diagnostic = AIPTEK_DIAGNOSTIC_SENDING_ABSOLUTE_IN_RELATIVE;
503                 } else if (!AIPTEK_POINTER_ALLOW_STYLUS_MODE
504                             (aiptek->curSetting.pointerMode)) {
505                                 aiptek->diagnostic = AIPTEK_DIAGNOSTIC_TOOL_DISALLOWED;
506                 } else {
507                         x = le16_to_cpu(get_unaligned((__le16 *) (data + 1)));
508                         y = le16_to_cpu(get_unaligned((__le16 *) (data + 3)));
509                         z = le16_to_cpu(get_unaligned((__le16 *) (data + 6)));
510
511                         dv = (data[5] & 0x01) != 0 ? 1 : 0;
512                         p = (data[5] & 0x02) != 0 ? 1 : 0;
513                         tip = (data[5] & 0x04) != 0 ? 1 : 0;
514
515                         /* Use jitterable to re-arrange button masks
516                          */
517                         jitterable = data[5] & 0x18;
518
519                         bs = (data[5] & aiptek->curSetting.stylusButtonLower) != 0 ? 1 : 0;
520                         pck = (data[5] & aiptek->curSetting.stylusButtonUpper) != 0 ? 1 : 0;
521
522                         /* dv indicates 'data valid' (e.g., the tablet is in sync
523                          * and has delivered a "correct" report) We will ignore
524                          * all 'bad' reports...
525                          */
526                         if (dv != 0) {
527                                 /* If we've not already sent a tool_button_?? code, do
528                                  * so now. Then set FIRED_BIT so it won't be resent unless
529                                  * the user forces FIRED_BIT off.
530                                  */
531                                 if (TOOL_BUTTON_FIRED
532                                     (aiptek->curSetting.toolMode) == 0) {
533                                         input_report_key(inputdev,
534                                                          TOOL_BUTTON(aiptek->curSetting.toolMode),
535                                                          1);
536                                         aiptek->curSetting.toolMode |= TOOL_BUTTON_FIRED_BIT;
537                                 }
538
539                                 if (p != 0) {
540                                         input_report_abs(inputdev, ABS_X, x);
541                                         input_report_abs(inputdev, ABS_Y, y);
542                                         input_report_abs(inputdev, ABS_PRESSURE, z);
543
544                                         input_report_key(inputdev, BTN_TOUCH, tip);
545                                         input_report_key(inputdev, BTN_STYLUS, bs);
546                                         input_report_key(inputdev, BTN_STYLUS2, pck);
547
548                                         if (aiptek->curSetting.xTilt !=
549                                             AIPTEK_TILT_DISABLE) {
550                                                 input_report_abs(inputdev,
551                                                                  ABS_TILT_X,
552                                                                  aiptek->curSetting.xTilt);
553                                         }
554                                         if (aiptek->curSetting.yTilt != AIPTEK_TILT_DISABLE) {
555                                                 input_report_abs(inputdev,
556                                                                  ABS_TILT_Y,
557                                                                  aiptek->curSetting.yTilt);
558                                         }
559
560                                         /* Wheel support is in the form of a single-event
561                                          * firing.
562                                          */
563                                         if (aiptek->curSetting.wheel !=
564                                             AIPTEK_WHEEL_DISABLE) {
565                                                 input_report_abs(inputdev,
566                                                                  ABS_WHEEL,
567                                                                  aiptek->curSetting.wheel);
568                                                 aiptek->curSetting.wheel = AIPTEK_WHEEL_DISABLE;
569                                         }
570                                 }
571                                 input_report_abs(inputdev, ABS_MISC, p | AIPTEK_REPORT_TOOL_STYLUS);
572                                 input_sync(inputdev);
573                         }
574                 }
575         }
576         /* Report 3's come from the mouse in absolute mode.
577          */
578         else if (data[0] == 3) {
579                 if (aiptek->curSetting.coordinateMode == AIPTEK_COORDINATE_RELATIVE_MODE) {
580                         aiptek->diagnostic = AIPTEK_DIAGNOSTIC_SENDING_ABSOLUTE_IN_RELATIVE;
581                 } else if (!AIPTEK_POINTER_ALLOW_MOUSE_MODE
582                         (aiptek->curSetting.pointerMode)) {
583                         aiptek->diagnostic = AIPTEK_DIAGNOSTIC_TOOL_DISALLOWED;
584                 } else {
585                         x = le16_to_cpu(get_unaligned((__le16 *) (data + 1)));
586                         y = le16_to_cpu(get_unaligned((__le16 *) (data + 3)));
587
588                         jitterable = data[5] & 0x1c;
589
590                         dv = (data[5] & 0x01) != 0 ? 1 : 0;
591                         p = (data[5] & 0x02) != 0 ? 1 : 0;
592                         left = (data[5] & aiptek->curSetting.mouseButtonLeft) != 0 ? 1 : 0;
593                         right = (data[5] & aiptek->curSetting.mouseButtonRight) != 0 ? 1 : 0;
594                         middle = (data[5] & aiptek->curSetting.mouseButtonMiddle) != 0 ? 1 : 0;
595
596                         if (dv != 0) {
597                                 /* If we've not already sent a tool_button_?? code, do
598                                  * so now. Then set FIRED_BIT so it won't be resent unless
599                                  * the user forces FIRED_BIT off.
600                                  */
601                                 if (TOOL_BUTTON_FIRED
602                                     (aiptek->curSetting.toolMode) == 0) {
603                                         input_report_key(inputdev,
604                                                          TOOL_BUTTON(aiptek->curSetting.toolMode),
605                                                          1);
606                                         aiptek->curSetting.toolMode |= TOOL_BUTTON_FIRED_BIT;
607                                 }
608
609                                 if (p != 0) {
610                                         input_report_abs(inputdev, ABS_X, x);
611                                         input_report_abs(inputdev, ABS_Y, y);
612
613                                         input_report_key(inputdev, BTN_LEFT, left);
614                                         input_report_key(inputdev, BTN_MIDDLE, middle);
615                                         input_report_key(inputdev, BTN_RIGHT, right);
616
617                                         /* Wheel support is in the form of a single-event
618                                          * firing.
619                                          */
620                                         if (aiptek->curSetting.wheel != AIPTEK_WHEEL_DISABLE) {
621                                                 input_report_abs(inputdev,
622                                                                  ABS_WHEEL,
623                                                                  aiptek->curSetting.wheel);
624                                                 aiptek->curSetting.wheel = AIPTEK_WHEEL_DISABLE;
625                                         }
626                                 }
627                                 input_report_rel(inputdev, REL_MISC, p | AIPTEK_REPORT_TOOL_MOUSE);
628                                 input_sync(inputdev);
629                         }
630                 }
631         }
632         /* Report 4s come from the macro keys when pressed by stylus
633          */
634         else if (data[0] == 4) {
635                 jitterable = data[1] & 0x18;
636
637                 dv = (data[1] & 0x01) != 0 ? 1 : 0;
638                 p = (data[1] & 0x02) != 0 ? 1 : 0;
639                 tip = (data[1] & 0x04) != 0 ? 1 : 0;
640                 bs = (data[1] & aiptek->curSetting.stylusButtonLower) != 0 ? 1 : 0;
641                 pck = (data[1] & aiptek->curSetting.stylusButtonUpper) != 0 ? 1 : 0;
642
643                 macro = data[3];
644                 z = le16_to_cpu(get_unaligned((__le16 *) (data + 4)));
645
646                 if (dv != 0) {
647                         /* If we've not already sent a tool_button_?? code, do
648                          * so now. Then set FIRED_BIT so it won't be resent unless
649                          * the user forces FIRED_BIT off.
650                          */
651                         if (TOOL_BUTTON_FIRED(aiptek->curSetting.toolMode) == 0) {
652                                 input_report_key(inputdev,
653                                                  TOOL_BUTTON(aiptek->curSetting.toolMode),
654                                                  1);
655                                 aiptek->curSetting.toolMode |= TOOL_BUTTON_FIRED_BIT;
656                         }
657
658                         if (p != 0) {
659                                 input_report_key(inputdev, BTN_TOUCH, tip);
660                                 input_report_key(inputdev, BTN_STYLUS, bs);
661                                 input_report_key(inputdev, BTN_STYLUS2, pck);
662                                 input_report_abs(inputdev, ABS_PRESSURE, z);
663                         }
664
665                         /* For safety, we're sending key 'break' codes for the
666                          * neighboring macro keys.
667                          */
668                         if (macro > 0) {
669                                 input_report_key(inputdev,
670                                                  macroKeyEvents[macro - 1], 0);
671                         }
672                         if (macro < 25) {
673                                 input_report_key(inputdev,
674                                                  macroKeyEvents[macro + 1], 0);
675                         }
676                         input_report_key(inputdev, macroKeyEvents[macro], p);
677                         input_report_abs(inputdev, ABS_MISC,
678                                          p | AIPTEK_REPORT_TOOL_STYLUS);
679                         input_sync(inputdev);
680                 }
681         }
682         /* Report 5s come from the macro keys when pressed by mouse
683          */
684         else if (data[0] == 5) {
685                 jitterable = data[1] & 0x1c;
686
687                 dv = (data[1] & 0x01) != 0 ? 1 : 0;
688                 p = (data[1] & 0x02) != 0 ? 1 : 0;
689                 left = (data[1]& aiptek->curSetting.mouseButtonLeft) != 0 ? 1 : 0;
690                 right = (data[1] & aiptek->curSetting.mouseButtonRight) != 0 ? 1 : 0;
691                 middle = (data[1] & aiptek->curSetting.mouseButtonMiddle) != 0 ? 1 : 0;
692                 macro = data[3];
693
694                 if (dv != 0) {
695                         /* If we've not already sent a tool_button_?? code, do
696                          * so now. Then set FIRED_BIT so it won't be resent unless
697                          * the user forces FIRED_BIT off.
698                          */
699                         if (TOOL_BUTTON_FIRED(aiptek->curSetting.toolMode) == 0) {
700                                 input_report_key(inputdev,
701                                                  TOOL_BUTTON(aiptek->curSetting.toolMode),
702                                                  1);
703                                 aiptek->curSetting.toolMode |= TOOL_BUTTON_FIRED_BIT;
704                         }
705
706                         if (p != 0) {
707                                 input_report_key(inputdev, BTN_LEFT, left);
708                                 input_report_key(inputdev, BTN_MIDDLE, middle);
709                                 input_report_key(inputdev, BTN_RIGHT, right);
710                         }
711
712                         /* For safety, we're sending key 'break' codes for the
713                          * neighboring macro keys.
714                          */
715                         if (macro > 0) {
716                                 input_report_key(inputdev,
717                                                  macroKeyEvents[macro - 1], 0);
718                         }
719                         if (macro < 25) {
720                                 input_report_key(inputdev,
721                                                  macroKeyEvents[macro + 1], 0);
722                         }
723
724                         input_report_key(inputdev, macroKeyEvents[macro], 1);
725                         input_report_rel(inputdev, ABS_MISC,
726                                          p | AIPTEK_REPORT_TOOL_MOUSE);
727                         input_sync(inputdev);
728                 }
729         }
730         /* We have no idea which tool can generate a report 6. Theoretically,
731          * neither need to, having been given reports 4 & 5 for such use.
732          * However, report 6 is the 'official-looking' report for macroKeys;
733          * reports 4 & 5 supposively are used to support unnamed, unknown
734          * hat switches (which just so happen to be the macroKeys.)
735          */
736         else if (data[0] == 6) {
737                 macro = le16_to_cpu(get_unaligned((__le16 *) (data + 1)));
738                 if (macro > 0) {
739                         input_report_key(inputdev, macroKeyEvents[macro - 1],
740                                          0);
741                 }
742                 if (macro < 25) {
743                         input_report_key(inputdev, macroKeyEvents[macro + 1],
744                                          0);
745                 }
746
747                 /* If we've not already sent a tool_button_?? code, do
748                  * so now. Then set FIRED_BIT so it won't be resent unless
749                  * the user forces FIRED_BIT off.
750                  */
751                 if (TOOL_BUTTON_FIRED(aiptek->curSetting.toolMode) == 0) {
752                         input_report_key(inputdev,
753                                          TOOL_BUTTON(aiptek->curSetting.
754                                                      toolMode), 1);
755                         aiptek->curSetting.toolMode |= TOOL_BUTTON_FIRED_BIT;
756                 }
757
758                 input_report_key(inputdev, macroKeyEvents[macro], 1);
759                 input_report_abs(inputdev, ABS_MISC,
760                                  1 | AIPTEK_REPORT_TOOL_UNKNOWN);
761                 input_sync(inputdev);
762         } else {
763                 dbg("Unknown report %d", data[0]);
764         }
765
766         /* Jitter may occur when the user presses a button on the stlyus
767          * or the mouse. What we do to prevent that is wait 'x' milliseconds
768          * following a 'jitterable' event, which should give the hand some time
769          * stabilize itself.
770          *
771          * We just introduced aiptek->previousJitterable to carry forth the
772          * notion that jitter occurs when the button state changes from on to off:
773          * a person drawing, holding a button down is not subject to jittering.
774          * With that in mind, changing from upper button depressed to lower button
775          * WILL transition through a jitter delay.
776          */
777
778         if (aiptek->previousJitterable != jitterable &&
779             aiptek->curSetting.jitterDelay != 0 && aiptek->inDelay != 1) {
780                 aiptek->endDelay = jiffies +
781                     ((aiptek->curSetting.jitterDelay * HZ) / 1000);
782                 aiptek->inDelay = 1;
783         }
784         aiptek->previousJitterable = jitterable;
785
786 exit:
787         retval = usb_submit_urb(urb, GFP_ATOMIC);
788         if (retval != 0) {
789                 err("%s - usb_submit_urb failed with result %d",
790                     __FUNCTION__, retval);
791         }
792 }
793
794 /***********************************************************************
795  * These are the USB id's known so far. We do not identify them to
796  * specific Aiptek model numbers, because there has been overlaps,
797  * use, and reuse of id's in existing models. Certain models have
798  * been known to use more than one ID, indicative perhaps of
799  * manufacturing revisions. In any event, we consider these
800  * IDs to not be model-specific nor unique.
801  */
802 static const struct usb_device_id aiptek_ids[] = {
803         {USB_DEVICE(USB_VENDOR_ID_AIPTEK, 0x01)},
804         {USB_DEVICE(USB_VENDOR_ID_AIPTEK, 0x10)},
805         {USB_DEVICE(USB_VENDOR_ID_AIPTEK, 0x20)},
806         {USB_DEVICE(USB_VENDOR_ID_AIPTEK, 0x21)},
807         {USB_DEVICE(USB_VENDOR_ID_AIPTEK, 0x22)},
808         {USB_DEVICE(USB_VENDOR_ID_AIPTEK, 0x23)},
809         {USB_DEVICE(USB_VENDOR_ID_AIPTEK, 0x24)},
810         {}
811 };
812
813 MODULE_DEVICE_TABLE(usb, aiptek_ids);
814
815 /***********************************************************************
816  * Open an instance of the tablet driver.
817  */
818 static int aiptek_open(struct input_dev *inputdev)
819 {
820         struct aiptek *aiptek = input_get_drvdata(inputdev);
821
822         aiptek->urb->dev = aiptek->usbdev;
823         if (usb_submit_urb(aiptek->urb, GFP_KERNEL) != 0)
824                 return -EIO;
825
826         return 0;
827 }
828
829 /***********************************************************************
830  * Close an instance of the tablet driver.
831  */
832 static void aiptek_close(struct input_dev *inputdev)
833 {
834         struct aiptek *aiptek = input_get_drvdata(inputdev);
835
836         usb_kill_urb(aiptek->urb);
837 }
838
839 /***********************************************************************
840  * aiptek_set_report and aiptek_get_report() are borrowed from Linux 2.4.x,
841  * where they were known as usb_set_report and usb_get_report.
842  */
843 static int
844 aiptek_set_report(struct aiptek *aiptek,
845                   unsigned char report_type,
846                   unsigned char report_id, void *buffer, int size)
847 {
848         return usb_control_msg(aiptek->usbdev,
849                                usb_sndctrlpipe(aiptek->usbdev, 0),
850                                USB_REQ_SET_REPORT,
851                                USB_TYPE_CLASS | USB_RECIP_INTERFACE |
852                                USB_DIR_OUT, (report_type << 8) + report_id,
853                                aiptek->ifnum, buffer, size, 5000);
854 }
855
856 static int
857 aiptek_get_report(struct aiptek *aiptek,
858                   unsigned char report_type,
859                   unsigned char report_id, void *buffer, int size)
860 {
861         return usb_control_msg(aiptek->usbdev,
862                                usb_rcvctrlpipe(aiptek->usbdev, 0),
863                                USB_REQ_GET_REPORT,
864                                USB_TYPE_CLASS | USB_RECIP_INTERFACE |
865                                USB_DIR_IN, (report_type << 8) + report_id,
866                                aiptek->ifnum, buffer, size, 5000);
867 }
868
869 /***********************************************************************
870  * Send a command to the tablet.
871  */
872 static int
873 aiptek_command(struct aiptek *aiptek, unsigned char command, unsigned char data)
874 {
875         const int sizeof_buf = 3 * sizeof(u8);
876         int ret;
877         u8 *buf;
878
879         buf = kmalloc(sizeof_buf, GFP_KERNEL);
880         if (!buf)
881                 return -ENOMEM;
882
883         buf[0] = 2;
884         buf[1] = command;
885         buf[2] = data;
886
887         if ((ret =
888              aiptek_set_report(aiptek, 3, 2, buf, sizeof_buf)) != sizeof_buf) {
889                 dbg("aiptek_program: failed, tried to send: 0x%02x 0x%02x",
890                     command, data);
891         }
892         kfree(buf);
893         return ret < 0 ? ret : 0;
894 }
895
896 /***********************************************************************
897  * Retrieve information from the tablet. Querying info is defined as first
898  * sending the {command,data} sequence as a command, followed by a wait
899  * (aka, "programmaticDelay") and then a "read" request.
900  */
901 static int
902 aiptek_query(struct aiptek *aiptek, unsigned char command, unsigned char data)
903 {
904         const int sizeof_buf = 3 * sizeof(u8);
905         int ret;
906         u8 *buf;
907
908         buf = kmalloc(sizeof_buf, GFP_KERNEL);
909         if (!buf)
910                 return -ENOMEM;
911
912         buf[0] = 2;
913         buf[1] = command;
914         buf[2] = data;
915
916         if (aiptek_command(aiptek, command, data) != 0) {
917                 kfree(buf);
918                 return -EIO;
919         }
920         msleep(aiptek->curSetting.programmableDelay);
921
922         if ((ret =
923              aiptek_get_report(aiptek, 3, 2, buf, sizeof_buf)) != sizeof_buf) {
924                 dbg("aiptek_query failed: returned 0x%02x 0x%02x 0x%02x",
925                     buf[0], buf[1], buf[2]);
926                 ret = -EIO;
927         } else {
928                 ret = le16_to_cpu(get_unaligned((__le16 *) (buf + 1)));
929         }
930         kfree(buf);
931         return ret;
932 }
933
934 /***********************************************************************
935  * Program the tablet into either absolute or relative mode.
936  * We also get information about the tablet's size.
937  */
938 static int aiptek_program_tablet(struct aiptek *aiptek)
939 {
940         int ret;
941         /* Execute Resolution500LPI */
942         if ((ret = aiptek_command(aiptek, 0x18, 0x04)) < 0)
943                 return ret;
944
945         /* Query getModelCode */
946         if ((ret = aiptek_query(aiptek, 0x02, 0x00)) < 0)
947                 return ret;
948         aiptek->features.modelCode = ret & 0xff;
949
950         /* Query getODMCode */
951         if ((ret = aiptek_query(aiptek, 0x03, 0x00)) < 0)
952                 return ret;
953         aiptek->features.odmCode = ret;
954
955         /* Query getFirmwareCode */
956         if ((ret = aiptek_query(aiptek, 0x04, 0x00)) < 0)
957                 return ret;
958         aiptek->features.firmwareCode = ret;
959
960         /* Query getXextension */
961         if ((ret = aiptek_query(aiptek, 0x01, 0x00)) < 0)
962                 return ret;
963         aiptek->inputdev->absmin[ABS_X] = 0;
964         aiptek->inputdev->absmax[ABS_X] = ret - 1;
965
966         /* Query getYextension */
967         if ((ret = aiptek_query(aiptek, 0x01, 0x01)) < 0)
968                 return ret;
969         aiptek->inputdev->absmin[ABS_Y] = 0;
970         aiptek->inputdev->absmax[ABS_Y] = ret - 1;
971
972         /* Query getPressureLevels */
973         if ((ret = aiptek_query(aiptek, 0x08, 0x00)) < 0)
974                 return ret;
975         aiptek->inputdev->absmin[ABS_PRESSURE] = 0;
976         aiptek->inputdev->absmax[ABS_PRESSURE] = ret - 1;
977
978         /* Depending on whether we are in absolute or relative mode, we will
979          * do a switchToTablet(absolute) or switchToMouse(relative) command.
980          */
981         if (aiptek->curSetting.coordinateMode ==
982             AIPTEK_COORDINATE_ABSOLUTE_MODE) {
983                 /* Execute switchToTablet */
984                 if ((ret = aiptek_command(aiptek, 0x10, 0x01)) < 0) {
985                         return ret;
986                 }
987         } else {
988                 /* Execute switchToMouse */
989                 if ((ret = aiptek_command(aiptek, 0x10, 0x00)) < 0) {
990                         return ret;
991                 }
992         }
993
994         /* Enable the macro keys */
995         if ((ret = aiptek_command(aiptek, 0x11, 0x02)) < 0)
996                 return ret;
997 #if 0
998         /* Execute FilterOn */
999         if ((ret = aiptek_command(aiptek, 0x17, 0x00)) < 0)
1000                 return ret;
1001 #endif
1002
1003         /* Execute AutoGainOn */
1004         if ((ret = aiptek_command(aiptek, 0x12, 0xff)) < 0)
1005                 return ret;
1006
1007         /* Reset the eventCount, so we track events from last (re)programming
1008          */
1009         aiptek->diagnostic = AIPTEK_DIAGNOSTIC_NA;
1010         aiptek->eventCount = 0;
1011
1012         return 0;
1013 }
1014
1015 /***********************************************************************
1016  * Sysfs functions. Sysfs prefers that individually-tunable parameters
1017  * exist in their separate pseudo-files. Summary data that is immutable
1018  * may exist in a singular file so long as you don't define a writeable
1019  * interface.
1020  */
1021
1022 /***********************************************************************
1023  * support the 'size' file -- display support
1024  */
1025 static ssize_t show_tabletSize(struct device *dev, struct device_attribute *attr, char *buf)
1026 {
1027         struct aiptek *aiptek = dev_get_drvdata(dev);
1028
1029         return snprintf(buf, PAGE_SIZE, "%dx%d\n",
1030                         aiptek->inputdev->absmax[ABS_X] + 1,
1031                         aiptek->inputdev->absmax[ABS_Y] + 1);
1032 }
1033
1034 /* These structs define the sysfs files, param #1 is the name of the
1035  * file, param 2 is the file permissions, param 3 & 4 are to the
1036  * output generator and input parser routines. Absence of a routine is
1037  * permitted -- it only means can't either 'cat' the file, or send data
1038  * to it.
1039  */
1040 static DEVICE_ATTR(size, S_IRUGO, show_tabletSize, NULL);
1041
1042 /***********************************************************************
1043  * support routines for the 'pointer_mode' file. Note that this file
1044  * both displays current setting and allows reprogramming.
1045  */
1046 static struct aiptek_map pointer_mode_map[] = {
1047         { "stylus",     AIPTEK_POINTER_ONLY_STYLUS_MODE },
1048         { "mouse",      AIPTEK_POINTER_ONLY_MOUSE_MODE },
1049         { "either",     AIPTEK_POINTER_EITHER_MODE },
1050         { NULL,         AIPTEK_INVALID_VALUE }
1051 };
1052
1053 static ssize_t show_tabletPointerMode(struct device *dev, struct device_attribute *attr, char *buf)
1054 {
1055         struct aiptek *aiptek = dev_get_drvdata(dev);
1056
1057         return snprintf(buf, PAGE_SIZE, "%s\n",
1058                         map_val_to_str(pointer_mode_map,
1059                                         aiptek->curSetting.pointerMode));
1060 }
1061
1062 static ssize_t
1063 store_tabletPointerMode(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1064 {
1065         struct aiptek *aiptek = dev_get_drvdata(dev);
1066         int new_mode = map_str_to_val(pointer_mode_map, buf, count);
1067
1068         if (new_mode == AIPTEK_INVALID_VALUE)
1069                 return -EINVAL;
1070
1071         aiptek->newSetting.pointerMode = new_mode;
1072         return count;
1073 }
1074
1075 static DEVICE_ATTR(pointer_mode,
1076                    S_IRUGO | S_IWUGO,
1077                    show_tabletPointerMode, store_tabletPointerMode);
1078
1079 /***********************************************************************
1080  * support routines for the 'coordinate_mode' file. Note that this file
1081  * both displays current setting and allows reprogramming.
1082  */
1083
1084 static struct aiptek_map coordinate_mode_map[] = {
1085         { "absolute",   AIPTEK_COORDINATE_ABSOLUTE_MODE },
1086         { "relative",   AIPTEK_COORDINATE_RELATIVE_MODE },
1087         { NULL,         AIPTEK_INVALID_VALUE }
1088 };
1089
1090 static ssize_t show_tabletCoordinateMode(struct device *dev, struct device_attribute *attr, char *buf)
1091 {
1092         struct aiptek *aiptek = dev_get_drvdata(dev);
1093
1094         return snprintf(buf, PAGE_SIZE, "%s\n",
1095                         map_val_to_str(coordinate_mode_map,
1096                                         aiptek->curSetting.coordinateMode));
1097 }
1098
1099 static ssize_t
1100 store_tabletCoordinateMode(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1101 {
1102         struct aiptek *aiptek = dev_get_drvdata(dev);
1103         int new_mode = map_str_to_val(coordinate_mode_map, buf, count);
1104
1105         if (new_mode == AIPTEK_INVALID_VALUE)
1106                 return -EINVAL;
1107
1108         aiptek->newSetting.coordinateMode = new_mode;
1109         return count;
1110 }
1111
1112 static DEVICE_ATTR(coordinate_mode,
1113                    S_IRUGO | S_IWUGO,
1114                    show_tabletCoordinateMode, store_tabletCoordinateMode);
1115
1116 /***********************************************************************
1117  * support routines for the 'tool_mode' file. Note that this file
1118  * both displays current setting and allows reprogramming.
1119  */
1120
1121 static struct aiptek_map tool_mode_map[] = {
1122         { "mouse",      AIPTEK_TOOL_BUTTON_MOUSE_MODE },
1123         { "eraser",     AIPTEK_TOOL_BUTTON_ERASER_MODE },
1124         { "pencil",     AIPTEK_TOOL_BUTTON_PENCIL_MODE },
1125         { "pen",        AIPTEK_TOOL_BUTTON_PEN_MODE },
1126         { "brush",      AIPTEK_TOOL_BUTTON_BRUSH_MODE },
1127         { "airbrush",   AIPTEK_TOOL_BUTTON_AIRBRUSH_MODE },
1128         { "lens",       AIPTEK_TOOL_BUTTON_LENS_MODE },
1129         { NULL,         AIPTEK_INVALID_VALUE }
1130 };
1131
1132 static ssize_t show_tabletToolMode(struct device *dev, struct device_attribute *attr, char *buf)
1133 {
1134         struct aiptek *aiptek = dev_get_drvdata(dev);
1135
1136         return snprintf(buf, PAGE_SIZE, "%s\n",
1137                         map_val_to_str(tool_mode_map,
1138                                         aiptek->curSetting.toolMode));
1139 }
1140
1141 static ssize_t
1142 store_tabletToolMode(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1143 {
1144         struct aiptek *aiptek = dev_get_drvdata(dev);
1145         int new_mode = map_str_to_val(tool_mode_map, buf, count);
1146
1147         if (new_mode == AIPTEK_INVALID_VALUE)
1148                 return -EINVAL;
1149
1150         aiptek->newSetting.toolMode = new_mode;
1151         return count;
1152 }
1153
1154 static DEVICE_ATTR(tool_mode,
1155                    S_IRUGO | S_IWUGO,
1156                    show_tabletToolMode, store_tabletToolMode);
1157
1158 /***********************************************************************
1159  * support routines for the 'xtilt' file. Note that this file
1160  * both displays current setting and allows reprogramming.
1161  */
1162 static ssize_t show_tabletXtilt(struct device *dev, struct device_attribute *attr, char *buf)
1163 {
1164         struct aiptek *aiptek = dev_get_drvdata(dev);
1165
1166         if (aiptek->curSetting.xTilt == AIPTEK_TILT_DISABLE) {
1167                 return snprintf(buf, PAGE_SIZE, "disable\n");
1168         } else {
1169                 return snprintf(buf, PAGE_SIZE, "%d\n",
1170                                 aiptek->curSetting.xTilt);
1171         }
1172 }
1173
1174 static ssize_t
1175 store_tabletXtilt(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1176 {
1177         struct aiptek *aiptek = dev_get_drvdata(dev);
1178         int x;
1179
1180         if (strcmp(buf, "disable") == 0) {
1181                 aiptek->newSetting.xTilt = AIPTEK_TILT_DISABLE;
1182         } else {
1183                 x = (int)simple_strtol(buf, NULL, 10);
1184                 if (x >= AIPTEK_TILT_MIN && x <= AIPTEK_TILT_MAX) {
1185                         aiptek->newSetting.xTilt = x;
1186                 }
1187         }
1188         return count;
1189 }
1190
1191 static DEVICE_ATTR(xtilt,
1192                    S_IRUGO | S_IWUGO, show_tabletXtilt, store_tabletXtilt);
1193
1194 /***********************************************************************
1195  * support routines for the 'ytilt' file. Note that this file
1196  * both displays current setting and allows reprogramming.
1197  */
1198 static ssize_t show_tabletYtilt(struct device *dev, struct device_attribute *attr, char *buf)
1199 {
1200         struct aiptek *aiptek = dev_get_drvdata(dev);
1201
1202         if (aiptek->curSetting.yTilt == AIPTEK_TILT_DISABLE) {
1203                 return snprintf(buf, PAGE_SIZE, "disable\n");
1204         } else {
1205                 return snprintf(buf, PAGE_SIZE, "%d\n",
1206                                 aiptek->curSetting.yTilt);
1207         }
1208 }
1209
1210 static ssize_t
1211 store_tabletYtilt(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1212 {
1213         struct aiptek *aiptek = dev_get_drvdata(dev);
1214         int y;
1215
1216         if (strcmp(buf, "disable") == 0) {
1217                 aiptek->newSetting.yTilt = AIPTEK_TILT_DISABLE;
1218         } else {
1219                 y = (int)simple_strtol(buf, NULL, 10);
1220                 if (y >= AIPTEK_TILT_MIN && y <= AIPTEK_TILT_MAX) {
1221                         aiptek->newSetting.yTilt = y;
1222                 }
1223         }
1224         return count;
1225 }
1226
1227 static DEVICE_ATTR(ytilt,
1228                    S_IRUGO | S_IWUGO, show_tabletYtilt, store_tabletYtilt);
1229
1230 /***********************************************************************
1231  * support routines for the 'jitter' file. Note that this file
1232  * both displays current setting and allows reprogramming.
1233  */
1234 static ssize_t show_tabletJitterDelay(struct device *dev, struct device_attribute *attr, char *buf)
1235 {
1236         struct aiptek *aiptek = dev_get_drvdata(dev);
1237
1238         return snprintf(buf, PAGE_SIZE, "%d\n", aiptek->curSetting.jitterDelay);
1239 }
1240
1241 static ssize_t
1242 store_tabletJitterDelay(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1243 {
1244         struct aiptek *aiptek = dev_get_drvdata(dev);
1245
1246         aiptek->newSetting.jitterDelay = (int)simple_strtol(buf, NULL, 10);
1247         return count;
1248 }
1249
1250 static DEVICE_ATTR(jitter,
1251                    S_IRUGO | S_IWUGO,
1252                    show_tabletJitterDelay, store_tabletJitterDelay);
1253
1254 /***********************************************************************
1255  * support routines for the 'delay' file. Note that this file
1256  * both displays current setting and allows reprogramming.
1257  */
1258 static ssize_t show_tabletProgrammableDelay(struct device *dev, struct device_attribute *attr, char *buf)
1259 {
1260         struct aiptek *aiptek = dev_get_drvdata(dev);
1261
1262         return snprintf(buf, PAGE_SIZE, "%d\n",
1263                         aiptek->curSetting.programmableDelay);
1264 }
1265
1266 static ssize_t
1267 store_tabletProgrammableDelay(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1268 {
1269         struct aiptek *aiptek = dev_get_drvdata(dev);
1270
1271         aiptek->newSetting.programmableDelay = (int)simple_strtol(buf, NULL, 10);
1272         return count;
1273 }
1274
1275 static DEVICE_ATTR(delay,
1276                    S_IRUGO | S_IWUGO,
1277                    show_tabletProgrammableDelay, store_tabletProgrammableDelay);
1278
1279 /***********************************************************************
1280  * support routines for the 'event_count' file. Note that this file
1281  * only displays current setting.
1282  */
1283 static ssize_t show_tabletEventsReceived(struct device *dev, struct device_attribute *attr, char *buf)
1284 {
1285         struct aiptek *aiptek = dev_get_drvdata(dev);
1286
1287         return snprintf(buf, PAGE_SIZE, "%ld\n", aiptek->eventCount);
1288 }
1289
1290 static DEVICE_ATTR(event_count, S_IRUGO, show_tabletEventsReceived, NULL);
1291
1292 /***********************************************************************
1293  * support routines for the 'diagnostic' file. Note that this file
1294  * only displays current setting.
1295  */
1296 static ssize_t show_tabletDiagnosticMessage(struct device *dev, struct device_attribute *attr, char *buf)
1297 {
1298         struct aiptek *aiptek = dev_get_drvdata(dev);
1299         char *retMsg;
1300
1301         switch (aiptek->diagnostic) {
1302         case AIPTEK_DIAGNOSTIC_NA:
1303                 retMsg = "no errors\n";
1304                 break;
1305
1306         case AIPTEK_DIAGNOSTIC_SENDING_RELATIVE_IN_ABSOLUTE:
1307                 retMsg = "Error: receiving relative reports\n";
1308                 break;
1309
1310         case AIPTEK_DIAGNOSTIC_SENDING_ABSOLUTE_IN_RELATIVE:
1311                 retMsg = "Error: receiving absolute reports\n";
1312                 break;
1313
1314         case AIPTEK_DIAGNOSTIC_TOOL_DISALLOWED:
1315                 if (aiptek->curSetting.pointerMode ==
1316                     AIPTEK_POINTER_ONLY_MOUSE_MODE) {
1317                         retMsg = "Error: receiving stylus reports\n";
1318                 } else {
1319                         retMsg = "Error: receiving mouse reports\n";
1320                 }
1321                 break;
1322
1323         default:
1324                 return 0;
1325         }
1326         return snprintf(buf, PAGE_SIZE, retMsg);
1327 }
1328
1329 static DEVICE_ATTR(diagnostic, S_IRUGO, show_tabletDiagnosticMessage, NULL);
1330
1331 /***********************************************************************
1332  * support routines for the 'stylus_upper' file. Note that this file
1333  * both displays current setting and allows for setting changing.
1334  */
1335
1336 static struct aiptek_map stylus_button_map[] = {
1337         { "upper",      AIPTEK_STYLUS_UPPER_BUTTON },
1338         { "lower",      AIPTEK_STYLUS_LOWER_BUTTON },
1339         { NULL,         AIPTEK_INVALID_VALUE }
1340 };
1341
1342 static ssize_t show_tabletStylusUpper(struct device *dev, struct device_attribute *attr, char *buf)
1343 {
1344         struct aiptek *aiptek = dev_get_drvdata(dev);
1345
1346         return snprintf(buf, PAGE_SIZE, "%s\n",
1347                         map_val_to_str(stylus_button_map,
1348                                         aiptek->curSetting.stylusButtonUpper));
1349 }
1350
1351 static ssize_t
1352 store_tabletStylusUpper(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1353 {
1354         struct aiptek *aiptek = dev_get_drvdata(dev);
1355         int new_button = map_str_to_val(stylus_button_map, buf, count);
1356
1357         if (new_button == AIPTEK_INVALID_VALUE)
1358                 return -EINVAL;
1359
1360         aiptek->newSetting.stylusButtonUpper = new_button;
1361         return count;
1362 }
1363
1364 static DEVICE_ATTR(stylus_upper,
1365                    S_IRUGO | S_IWUGO,
1366                    show_tabletStylusUpper, store_tabletStylusUpper);
1367
1368 /***********************************************************************
1369  * support routines for the 'stylus_lower' file. Note that this file
1370  * both displays current setting and allows for setting changing.
1371  */
1372
1373 static ssize_t show_tabletStylusLower(struct device *dev, struct device_attribute *attr, char *buf)
1374 {
1375         struct aiptek *aiptek = dev_get_drvdata(dev);
1376
1377         return snprintf(buf, PAGE_SIZE, "%s\n",
1378                         map_val_to_str(stylus_button_map,
1379                                         aiptek->curSetting.stylusButtonLower));
1380 }
1381
1382 static ssize_t
1383 store_tabletStylusLower(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1384 {
1385         struct aiptek *aiptek = dev_get_drvdata(dev);
1386         int new_button = map_str_to_val(stylus_button_map, buf, count);
1387
1388         if (new_button == AIPTEK_INVALID_VALUE)
1389                 return -EINVAL;
1390
1391         aiptek->newSetting.stylusButtonLower = new_button;
1392         return count;
1393 }
1394
1395 static DEVICE_ATTR(stylus_lower,
1396                    S_IRUGO | S_IWUGO,
1397                    show_tabletStylusLower, store_tabletStylusLower);
1398
1399 /***********************************************************************
1400  * support routines for the 'mouse_left' file. Note that this file
1401  * both displays current setting and allows for setting changing.
1402  */
1403
1404 static struct aiptek_map mouse_button_map[] = {
1405         { "left",       AIPTEK_MOUSE_LEFT_BUTTON },
1406         { "middle",     AIPTEK_MOUSE_MIDDLE_BUTTON },
1407         { "right",      AIPTEK_MOUSE_RIGHT_BUTTON },
1408         { NULL,         AIPTEK_INVALID_VALUE }
1409 };
1410
1411 static ssize_t show_tabletMouseLeft(struct device *dev, struct device_attribute *attr, char *buf)
1412 {
1413         struct aiptek *aiptek = dev_get_drvdata(dev);
1414
1415         return snprintf(buf, PAGE_SIZE, "%s\n",
1416                         map_val_to_str(mouse_button_map,
1417                                         aiptek->curSetting.mouseButtonLeft));
1418 }
1419
1420 static ssize_t
1421 store_tabletMouseLeft(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1422 {
1423         struct aiptek *aiptek = dev_get_drvdata(dev);
1424         int new_button = map_str_to_val(mouse_button_map, buf, count);
1425
1426         if (new_button == AIPTEK_INVALID_VALUE)
1427                 return -EINVAL;
1428
1429         aiptek->newSetting.mouseButtonLeft = new_button;
1430         return count;
1431 }
1432
1433 static DEVICE_ATTR(mouse_left,
1434                    S_IRUGO | S_IWUGO,
1435                    show_tabletMouseLeft, store_tabletMouseLeft);
1436
1437 /***********************************************************************
1438  * support routines for the 'mouse_middle' file. Note that this file
1439  * both displays current setting and allows for setting changing.
1440  */
1441 static ssize_t show_tabletMouseMiddle(struct device *dev, struct device_attribute *attr, char *buf)
1442 {
1443         struct aiptek *aiptek = dev_get_drvdata(dev);
1444
1445         return snprintf(buf, PAGE_SIZE, "%s\n",
1446                         map_val_to_str(mouse_button_map,
1447                                         aiptek->curSetting.mouseButtonMiddle));
1448 }
1449
1450 static ssize_t
1451 store_tabletMouseMiddle(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1452 {
1453         struct aiptek *aiptek = dev_get_drvdata(dev);
1454         int new_button = map_str_to_val(mouse_button_map, buf, count);
1455
1456         if (new_button == AIPTEK_INVALID_VALUE)
1457                 return -EINVAL;
1458
1459         aiptek->newSetting.mouseButtonMiddle = new_button;
1460         return count;
1461 }
1462
1463 static DEVICE_ATTR(mouse_middle,
1464                    S_IRUGO | S_IWUGO,
1465                    show_tabletMouseMiddle, store_tabletMouseMiddle);
1466
1467 /***********************************************************************
1468  * support routines for the 'mouse_right' file. Note that this file
1469  * both displays current setting and allows for setting changing.
1470  */
1471 static ssize_t show_tabletMouseRight(struct device *dev, struct device_attribute *attr, char *buf)
1472 {
1473         struct aiptek *aiptek = dev_get_drvdata(dev);
1474
1475         return snprintf(buf, PAGE_SIZE, "%s\n",
1476                         map_val_to_str(mouse_button_map,
1477                                         aiptek->curSetting.mouseButtonRight));
1478 }
1479
1480 static ssize_t
1481 store_tabletMouseRight(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1482 {
1483         struct aiptek *aiptek = dev_get_drvdata(dev);
1484         int new_button = map_str_to_val(mouse_button_map, buf, count);
1485
1486         if (new_button == AIPTEK_INVALID_VALUE)
1487                 return -EINVAL;
1488
1489         aiptek->newSetting.mouseButtonRight = new_button;
1490         return count;
1491 }
1492
1493 static DEVICE_ATTR(mouse_right,
1494                    S_IRUGO | S_IWUGO,
1495                    show_tabletMouseRight, store_tabletMouseRight);
1496
1497 /***********************************************************************
1498  * support routines for the 'wheel' file. Note that this file
1499  * both displays current setting and allows for setting changing.
1500  */
1501 static ssize_t show_tabletWheel(struct device *dev, struct device_attribute *attr, char *buf)
1502 {
1503         struct aiptek *aiptek = dev_get_drvdata(dev);
1504
1505         if (aiptek->curSetting.wheel == AIPTEK_WHEEL_DISABLE) {
1506                 return snprintf(buf, PAGE_SIZE, "disable\n");
1507         } else {
1508                 return snprintf(buf, PAGE_SIZE, "%d\n",
1509                                 aiptek->curSetting.wheel);
1510         }
1511 }
1512
1513 static ssize_t
1514 store_tabletWheel(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1515 {
1516         struct aiptek *aiptek = dev_get_drvdata(dev);
1517
1518         aiptek->newSetting.wheel = (int)simple_strtol(buf, NULL, 10);
1519         return count;
1520 }
1521
1522 static DEVICE_ATTR(wheel,
1523                    S_IRUGO | S_IWUGO, show_tabletWheel, store_tabletWheel);
1524
1525 /***********************************************************************
1526  * support routines for the 'execute' file. Note that this file
1527  * both displays current setting and allows for setting changing.
1528  */
1529 static ssize_t show_tabletExecute(struct device *dev, struct device_attribute *attr, char *buf)
1530 {
1531         /* There is nothing useful to display, so a one-line manual
1532          * is in order...
1533          */
1534         return snprintf(buf, PAGE_SIZE,
1535                         "Write anything to this file to program your tablet.\n");
1536 }
1537
1538 static ssize_t
1539 store_tabletExecute(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1540 {
1541         struct aiptek *aiptek = dev_get_drvdata(dev);
1542
1543         /* We do not care what you write to this file. Merely the action
1544          * of writing to this file triggers a tablet reprogramming.
1545          */
1546         memcpy(&aiptek->curSetting, &aiptek->newSetting,
1547                sizeof(struct aiptek_settings));
1548
1549         if (aiptek_program_tablet(aiptek) < 0)
1550                 return -EIO;
1551
1552         return count;
1553 }
1554
1555 static DEVICE_ATTR(execute,
1556                    S_IRUGO | S_IWUGO, show_tabletExecute, store_tabletExecute);
1557
1558 /***********************************************************************
1559  * support routines for the 'odm_code' file. Note that this file
1560  * only displays current setting.
1561  */
1562 static ssize_t show_tabletODMCode(struct device *dev, struct device_attribute *attr, char *buf)
1563 {
1564         struct aiptek *aiptek = dev_get_drvdata(dev);
1565
1566         return snprintf(buf, PAGE_SIZE, "0x%04x\n", aiptek->features.odmCode);
1567 }
1568
1569 static DEVICE_ATTR(odm_code, S_IRUGO, show_tabletODMCode, NULL);
1570
1571 /***********************************************************************
1572  * support routines for the 'model_code' file. Note that this file
1573  * only displays current setting.
1574  */
1575 static ssize_t show_tabletModelCode(struct device *dev, struct device_attribute *attr, char *buf)
1576 {
1577         struct aiptek *aiptek = dev_get_drvdata(dev);
1578
1579         return snprintf(buf, PAGE_SIZE, "0x%04x\n", aiptek->features.modelCode);
1580 }
1581
1582 static DEVICE_ATTR(model_code, S_IRUGO, show_tabletModelCode, NULL);
1583
1584 /***********************************************************************
1585  * support routines for the 'firmware_code' file. Note that this file
1586  * only displays current setting.
1587  */
1588 static ssize_t show_firmwareCode(struct device *dev, struct device_attribute *attr, char *buf)
1589 {
1590         struct aiptek *aiptek = dev_get_drvdata(dev);
1591
1592         return snprintf(buf, PAGE_SIZE, "%04x\n",
1593                         aiptek->features.firmwareCode);
1594 }
1595
1596 static DEVICE_ATTR(firmware_code, S_IRUGO, show_firmwareCode, NULL);
1597
1598 static struct attribute *aiptek_attributes[] = {
1599         &dev_attr_size.attr,
1600         &dev_attr_pointer_mode.attr,
1601         &dev_attr_coordinate_mode.attr,
1602         &dev_attr_tool_mode.attr,
1603         &dev_attr_xtilt.attr,
1604         &dev_attr_ytilt.attr,
1605         &dev_attr_jitter.attr,
1606         &dev_attr_delay.attr,
1607         &dev_attr_event_count.attr,
1608         &dev_attr_diagnostic.attr,
1609         &dev_attr_odm_code.attr,
1610         &dev_attr_model_code.attr,
1611         &dev_attr_firmware_code.attr,
1612         &dev_attr_stylus_lower.attr,
1613         &dev_attr_stylus_upper.attr,
1614         &dev_attr_mouse_left.attr,
1615         &dev_attr_mouse_middle.attr,
1616         &dev_attr_mouse_right.attr,
1617         &dev_attr_wheel.attr,
1618         &dev_attr_execute.attr,
1619         NULL
1620 };
1621
1622 static struct attribute_group aiptek_attribute_group = {
1623         .attrs  = aiptek_attributes,
1624 };
1625
1626 /***********************************************************************
1627  * This routine is called when a tablet has been identified. It basically
1628  * sets up the tablet and the driver's internal structures.
1629  */
1630 static int
1631 aiptek_probe(struct usb_interface *intf, const struct usb_device_id *id)
1632 {
1633         struct usb_device *usbdev = interface_to_usbdev(intf);
1634         struct usb_endpoint_descriptor *endpoint;
1635         struct aiptek *aiptek;
1636         struct input_dev *inputdev;
1637         int i;
1638         int speeds[] = { 0,
1639                 AIPTEK_PROGRAMMABLE_DELAY_50,
1640                 AIPTEK_PROGRAMMABLE_DELAY_400,
1641                 AIPTEK_PROGRAMMABLE_DELAY_25,
1642                 AIPTEK_PROGRAMMABLE_DELAY_100,
1643                 AIPTEK_PROGRAMMABLE_DELAY_200,
1644                 AIPTEK_PROGRAMMABLE_DELAY_300
1645         };
1646         int err = -ENOMEM;
1647
1648         /* programmableDelay is where the command-line specified
1649          * delay is kept. We make it the first element of speeds[],
1650          * so therefore, your override speed is tried first, then the
1651          * remainder. Note that the default value of 400ms will be tried
1652          * if you do not specify any command line parameter.
1653          */
1654         speeds[0] = programmableDelay;
1655
1656         aiptek = kzalloc(sizeof(struct aiptek), GFP_KERNEL);
1657         inputdev = input_allocate_device();
1658         if (!aiptek || !inputdev)
1659                 goto fail1;
1660
1661         aiptek->data = usb_buffer_alloc(usbdev, AIPTEK_PACKET_LENGTH,
1662                                         GFP_ATOMIC, &aiptek->data_dma);
1663         if (!aiptek->data)
1664                 goto fail1;
1665
1666         aiptek->urb = usb_alloc_urb(0, GFP_KERNEL);
1667         if (!aiptek->urb)
1668                 goto fail2;
1669
1670         aiptek->inputdev = inputdev;
1671         aiptek->usbdev = usbdev;
1672         aiptek->ifnum = intf->altsetting[0].desc.bInterfaceNumber;
1673         aiptek->inDelay = 0;
1674         aiptek->endDelay = 0;
1675         aiptek->previousJitterable = 0;
1676
1677         /* Set up the curSettings struct. Said struct contains the current
1678          * programmable parameters. The newSetting struct contains changes
1679          * the user makes to the settings via the sysfs interface. Those
1680          * changes are not "committed" to curSettings until the user
1681          * writes to the sysfs/.../execute file.
1682          */
1683         aiptek->curSetting.pointerMode = AIPTEK_POINTER_EITHER_MODE;
1684         aiptek->curSetting.coordinateMode = AIPTEK_COORDINATE_ABSOLUTE_MODE;
1685         aiptek->curSetting.toolMode = AIPTEK_TOOL_BUTTON_PEN_MODE;
1686         aiptek->curSetting.xTilt = AIPTEK_TILT_DISABLE;
1687         aiptek->curSetting.yTilt = AIPTEK_TILT_DISABLE;
1688         aiptek->curSetting.mouseButtonLeft = AIPTEK_MOUSE_LEFT_BUTTON;
1689         aiptek->curSetting.mouseButtonMiddle = AIPTEK_MOUSE_MIDDLE_BUTTON;
1690         aiptek->curSetting.mouseButtonRight = AIPTEK_MOUSE_RIGHT_BUTTON;
1691         aiptek->curSetting.stylusButtonUpper = AIPTEK_STYLUS_UPPER_BUTTON;
1692         aiptek->curSetting.stylusButtonLower = AIPTEK_STYLUS_LOWER_BUTTON;
1693         aiptek->curSetting.jitterDelay = jitterDelay;
1694         aiptek->curSetting.programmableDelay = programmableDelay;
1695
1696         /* Both structs should have equivalent settings
1697          */
1698         aiptek->newSetting = aiptek->curSetting;
1699
1700         /* Determine the usb devices' physical path.
1701          * Asketh not why we always pretend we're using "../input0",
1702          * but I suspect this will have to be refactored one
1703          * day if a single USB device can be a keyboard & a mouse
1704          * & a tablet, and the inputX number actually will tell
1705          * us something...
1706          */
1707         usb_make_path(usbdev, aiptek->features.usbPath,
1708                         sizeof(aiptek->features.usbPath));
1709         strlcat(aiptek->features.usbPath, "/input0",
1710                 sizeof(aiptek->features.usbPath));
1711
1712         /* Set up client data, pointers to open and close routines
1713          * for the input device.
1714          */
1715         inputdev->name = "Aiptek";
1716         inputdev->phys = aiptek->features.usbPath;
1717         usb_to_input_id(usbdev, &inputdev->id);
1718         inputdev->dev.parent = &intf->dev;
1719
1720         input_set_drvdata(inputdev, aiptek);
1721
1722         inputdev->open = aiptek_open;
1723         inputdev->close = aiptek_close;
1724
1725         /* Now program the capacities of the tablet, in terms of being
1726          * an input device.
1727          */
1728         inputdev->evbit[0] |= BIT(EV_KEY)
1729             | BIT(EV_ABS)
1730             | BIT(EV_REL)
1731             | BIT(EV_MSC);
1732
1733         inputdev->absbit[0] |= BIT(ABS_MISC);
1734
1735         inputdev->relbit[0] |=
1736             (BIT(REL_X) | BIT(REL_Y) | BIT(REL_WHEEL) | BIT(REL_MISC));
1737
1738         inputdev->mscbit[0] = BIT(MSC_SERIAL);
1739
1740         /* Set up key and button codes */
1741         for (i = 0; i < ARRAY_SIZE(buttonEvents); ++i)
1742                 __set_bit(buttonEvents[i], inputdev->keybit);
1743
1744         for (i = 0; i < ARRAY_SIZE(macroKeyEvents); ++i)
1745                 __set_bit(macroKeyEvents[i], inputdev->keybit);
1746
1747         /*
1748          * Program the input device coordinate capacities. We do not yet
1749          * know what maximum X, Y, and Z values are, so we're putting fake
1750          * values in. Later, we'll ask the tablet to put in the correct
1751          * values.
1752          */
1753         input_set_abs_params(inputdev, ABS_X, 0, 2999, 0, 0);
1754         input_set_abs_params(inputdev, ABS_Y, 0, 2249, 0, 0);
1755         input_set_abs_params(inputdev, ABS_PRESSURE, 0, 511, 0, 0);
1756         input_set_abs_params(inputdev, ABS_TILT_X, AIPTEK_TILT_MIN, AIPTEK_TILT_MAX, 0, 0);
1757         input_set_abs_params(inputdev, ABS_TILT_Y, AIPTEK_TILT_MIN, AIPTEK_TILT_MAX, 0, 0);
1758         input_set_abs_params(inputdev, ABS_WHEEL, AIPTEK_WHEEL_MIN, AIPTEK_WHEEL_MAX - 1, 0, 0);
1759
1760         endpoint = &intf->altsetting[0].endpoint[0].desc;
1761
1762         /* Go set up our URB, which is called when the tablet receives
1763          * input.
1764          */
1765         usb_fill_int_urb(aiptek->urb,
1766                          aiptek->usbdev,
1767                          usb_rcvintpipe(aiptek->usbdev,
1768                                         endpoint->bEndpointAddress),
1769                          aiptek->data, 8, aiptek_irq, aiptek,
1770                          endpoint->bInterval);
1771
1772         aiptek->urb->transfer_dma = aiptek->data_dma;
1773         aiptek->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1774
1775         /* Program the tablet. This sets the tablet up in the mode
1776          * specified in newSetting, and also queries the tablet's
1777          * physical capacities.
1778          *
1779          * Sanity check: if a tablet doesn't like the slow programmatic
1780          * delay, we often get sizes of 0x0. Let's use that as an indicator
1781          * to try faster delays, up to 25 ms. If that logic fails, well, you'll
1782          * have to explain to us how your tablet thinks it's 0x0, and yet that's
1783          * not an error :-)
1784          */
1785
1786         for (i = 0; i < ARRAY_SIZE(speeds); ++i) {
1787                 aiptek->curSetting.programmableDelay = speeds[i];
1788                 (void)aiptek_program_tablet(aiptek);
1789                 if (aiptek->inputdev->absmax[ABS_X] > 0) {
1790                         info("input: Aiptek using %d ms programming speed\n",
1791                              aiptek->curSetting.programmableDelay);
1792                         break;
1793                 }
1794         }
1795
1796         /* Associate this driver's struct with the usb interface.
1797          */
1798         usb_set_intfdata(intf, aiptek);
1799
1800         /* Set up the sysfs files
1801          */
1802         err = sysfs_create_group(&intf->dev.kobj, &aiptek_attribute_group);
1803         if (err)
1804                 goto fail3;
1805
1806         /* Register the tablet as an Input Device
1807          */
1808         err = input_register_device(aiptek->inputdev);
1809         if (err)
1810                 goto fail4;
1811
1812         return 0;
1813
1814  fail4: sysfs_remove_group(&intf->dev.kobj, &aiptek_attribute_group);
1815  fail3: usb_free_urb(aiptek->urb);
1816  fail2: usb_buffer_free(usbdev, AIPTEK_PACKET_LENGTH, aiptek->data,
1817                         aiptek->data_dma);
1818  fail1: usb_set_intfdata(intf, NULL);
1819         input_free_device(inputdev);
1820         kfree(aiptek);
1821         return err;
1822 }
1823
1824 /***********************************************************************
1825  * Deal with tablet disconnecting from the system.
1826  */
1827 static void aiptek_disconnect(struct usb_interface *intf)
1828 {
1829         struct aiptek *aiptek = usb_get_intfdata(intf);
1830
1831         /* Disassociate driver's struct with usb interface
1832          */
1833         usb_set_intfdata(intf, NULL);
1834         if (aiptek != NULL) {
1835                 /* Free & unhook everything from the system.
1836                  */
1837                 usb_kill_urb(aiptek->urb);
1838                 input_unregister_device(aiptek->inputdev);
1839                 sysfs_remove_group(&intf->dev.kobj, &aiptek_attribute_group);
1840                 usb_free_urb(aiptek->urb);
1841                 usb_buffer_free(interface_to_usbdev(intf),
1842                                 AIPTEK_PACKET_LENGTH,
1843                                 aiptek->data, aiptek->data_dma);
1844                 kfree(aiptek);
1845         }
1846 }
1847
1848 static struct usb_driver aiptek_driver = {
1849         .name = "aiptek",
1850         .probe = aiptek_probe,
1851         .disconnect = aiptek_disconnect,
1852         .id_table = aiptek_ids,
1853 };
1854
1855 static int __init aiptek_init(void)
1856 {
1857         int result = usb_register(&aiptek_driver);
1858         if (result == 0) {
1859                 info(DRIVER_VERSION ": " DRIVER_AUTHOR);
1860                 info(DRIVER_DESC);
1861         }
1862         return result;
1863 }
1864
1865 static void __exit aiptek_exit(void)
1866 {
1867         usb_deregister(&aiptek_driver);
1868 }
1869
1870 MODULE_AUTHOR(DRIVER_AUTHOR);
1871 MODULE_DESCRIPTION(DRIVER_DESC);
1872 MODULE_LICENSE("GPL");
1873
1874 module_param(programmableDelay, int, 0);
1875 MODULE_PARM_DESC(programmableDelay, "delay used during tablet programming");
1876 module_param(jitterDelay, int, 0);
1877 MODULE_PARM_DESC(jitterDelay, "stylus/mouse settlement delay");
1878
1879 module_init(aiptek_init);
1880 module_exit(aiptek_exit);