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