xref: /linux/drivers/input/tablet/aiptek.c (revision 7a5f1cd22d47f8ca4b760b6334378ae42c1bd24b)
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 		if (macro >= ARRAY_SIZE(macroKeyEvents))
661 			macro = -1;
662 		z = get_unaligned_le16(data + 4);
663 
664 		if (dv) {
665 		        /* If the selected tool changed, reset the old
666 			 * tool key, and set the new one.
667 			 */
668 		        if (aiptek->previousToolMode !=
669 			    aiptek->curSetting.toolMode) {
670 			        input_report_key(inputdev,
671 						 aiptek->previousToolMode, 0);
672 				input_report_key(inputdev,
673 						 aiptek->curSetting.toolMode,
674 						 1);
675 				aiptek->previousToolMode =
676 				        aiptek->curSetting.toolMode;
677 			}
678 		}
679 
680 		if (aiptek->lastMacro != -1 && aiptek->lastMacro != macro) {
681 		        input_report_key(inputdev, macroKeyEvents[aiptek->lastMacro], 0);
682 			aiptek->lastMacro = -1;
683 		}
684 
685 		if (macro != -1 && macro != aiptek->lastMacro) {
686 			input_report_key(inputdev, macroKeyEvents[macro], 1);
687 			aiptek->lastMacro = macro;
688 		}
689 		input_report_abs(inputdev, ABS_MISC,
690 				 p | AIPTEK_REPORT_TOOL_STYLUS);
691 		input_sync(inputdev);
692 	}
693 	/* Report 5s come from the macro keys when pressed by mouse
694 	 */
695 	else if (data[0] == 5) {
696 		jitterable = data[1] & 0x1c;
697 
698 		dv = (data[1] & 0x01) != 0 ? 1 : 0;
699 		p = (data[1] & 0x02) != 0 ? 1 : 0;
700 		left = (data[1]& aiptek->curSetting.mouseButtonLeft) != 0 ? 1 : 0;
701 		right = (data[1] & aiptek->curSetting.mouseButtonRight) != 0 ? 1 : 0;
702 		middle = (data[1] & aiptek->curSetting.mouseButtonMiddle) != 0 ? 1 : 0;
703 		macro = dv && p && left && !(data[3] & 1) ? (data[3] >> 1) : -1;
704 		if (macro >= ARRAY_SIZE(macroKeyEvents))
705 			macro = -1;
706 
707 		if (dv) {
708 		        /* If the selected tool changed, reset the old
709 			 * tool key, and set the new one.
710 			 */
711 		        if (aiptek->previousToolMode !=
712 			    aiptek->curSetting.toolMode) {
713 		                input_report_key(inputdev,
714 						 aiptek->previousToolMode, 0);
715 			        input_report_key(inputdev,
716 						 aiptek->curSetting.toolMode, 1);
717 			        aiptek->previousToolMode = aiptek->curSetting.toolMode;
718 			}
719 		}
720 
721 		if (aiptek->lastMacro != -1 && aiptek->lastMacro != macro) {
722 		        input_report_key(inputdev, macroKeyEvents[aiptek->lastMacro], 0);
723 			aiptek->lastMacro = -1;
724 		}
725 
726 		if (macro != -1 && macro != aiptek->lastMacro) {
727 			input_report_key(inputdev, macroKeyEvents[macro], 1);
728 			aiptek->lastMacro = macro;
729 		}
730 
731 		input_report_abs(inputdev, ABS_MISC,
732 				 p | AIPTEK_REPORT_TOOL_MOUSE);
733 		input_sync(inputdev);
734 	}
735 	/* We have no idea which tool can generate a report 6. Theoretically,
736 	 * neither need to, having been given reports 4 & 5 for such use.
737 	 * However, report 6 is the 'official-looking' report for macroKeys;
738 	 * reports 4 & 5 supposively are used to support unnamed, unknown
739 	 * hat switches (which just so happen to be the macroKeys.)
740 	 */
741 	else if (data[0] == 6) {
742 		macro = get_unaligned_le16(data + 1);
743 		if (macro > 0 && macro - 1 < ARRAY_SIZE(macroKeyEvents)) {
744 			input_report_key(inputdev, macroKeyEvents[macro - 1],
745 					 0);
746 		}
747 		if (macro + 1 < ARRAY_SIZE(macroKeyEvents)) {
748 			input_report_key(inputdev, macroKeyEvents[macro + 1],
749 					 0);
750 		}
751 
752 		/* If the selected tool changed, reset the old
753 		   tool key, and set the new one.
754 		*/
755 		if (aiptek->previousToolMode !=
756 		    aiptek->curSetting.toolMode) {
757 		        input_report_key(inputdev,
758 					 aiptek->previousToolMode, 0);
759 			input_report_key(inputdev,
760 					 aiptek->curSetting.toolMode,
761 					 1);
762 			aiptek->previousToolMode =
763 				aiptek->curSetting.toolMode;
764 		}
765 
766 		if (macro < ARRAY_SIZE(macroKeyEvents))
767 			input_report_key(inputdev, macroKeyEvents[macro], 1);
768 		input_report_abs(inputdev, ABS_MISC,
769 				 1 | AIPTEK_REPORT_TOOL_UNKNOWN);
770 		input_sync(inputdev);
771 	} else {
772 		dev_dbg(&intf->dev, "Unknown report %d\n", data[0]);
773 	}
774 
775 	/* Jitter may occur when the user presses a button on the stlyus
776 	 * or the mouse. What we do to prevent that is wait 'x' milliseconds
777 	 * following a 'jitterable' event, which should give the hand some time
778 	 * stabilize itself.
779 	 *
780 	 * We just introduced aiptek->previousJitterable to carry forth the
781 	 * notion that jitter occurs when the button state changes from on to off:
782 	 * a person drawing, holding a button down is not subject to jittering.
783 	 * With that in mind, changing from upper button depressed to lower button
784 	 * WILL transition through a jitter delay.
785 	 */
786 
787 	if (aiptek->previousJitterable != jitterable &&
788 	    aiptek->curSetting.jitterDelay != 0 && aiptek->inDelay != 1) {
789 		aiptek->endDelay = jiffies +
790 		    ((aiptek->curSetting.jitterDelay * HZ) / 1000);
791 		aiptek->inDelay = 1;
792 	}
793 	aiptek->previousJitterable = jitterable;
794 
795 exit:
796 	retval = usb_submit_urb(urb, GFP_ATOMIC);
797 	if (retval != 0) {
798 		dev_err(&intf->dev,
799 			"%s - usb_submit_urb failed with result %d\n",
800 			__func__, retval);
801 	}
802 }
803 
804 /***********************************************************************
805  * These are the USB id's known so far. We do not identify them to
806  * specific Aiptek model numbers, because there has been overlaps,
807  * use, and reuse of id's in existing models. Certain models have
808  * been known to use more than one ID, indicative perhaps of
809  * manufacturing revisions. In any event, we consider these
810  * IDs to not be model-specific nor unique.
811  */
812 static const struct usb_device_id aiptek_ids[] = {
813 	{USB_DEVICE(USB_VENDOR_ID_AIPTEK, 0x01)},
814 	{USB_DEVICE(USB_VENDOR_ID_AIPTEK, 0x10)},
815 	{USB_DEVICE(USB_VENDOR_ID_AIPTEK, 0x20)},
816 	{USB_DEVICE(USB_VENDOR_ID_AIPTEK, 0x21)},
817 	{USB_DEVICE(USB_VENDOR_ID_AIPTEK, 0x22)},
818 	{USB_DEVICE(USB_VENDOR_ID_AIPTEK, 0x23)},
819 	{USB_DEVICE(USB_VENDOR_ID_AIPTEK, 0x24)},
820 	{USB_DEVICE(USB_VENDOR_ID_KYE, 0x5003)},
821 	{}
822 };
823 
824 MODULE_DEVICE_TABLE(usb, aiptek_ids);
825 
826 /***********************************************************************
827  * Open an instance of the tablet driver.
828  */
829 static int aiptek_open(struct input_dev *inputdev)
830 {
831 	struct aiptek *aiptek = input_get_drvdata(inputdev);
832 
833 	aiptek->urb->dev = interface_to_usbdev(aiptek->intf);
834 	if (usb_submit_urb(aiptek->urb, GFP_KERNEL) != 0)
835 		return -EIO;
836 
837 	return 0;
838 }
839 
840 /***********************************************************************
841  * Close an instance of the tablet driver.
842  */
843 static void aiptek_close(struct input_dev *inputdev)
844 {
845 	struct aiptek *aiptek = input_get_drvdata(inputdev);
846 
847 	usb_kill_urb(aiptek->urb);
848 }
849 
850 /***********************************************************************
851  * aiptek_set_report and aiptek_get_report() are borrowed from Linux 2.4.x,
852  * where they were known as usb_set_report and usb_get_report.
853  */
854 static int
855 aiptek_set_report(struct aiptek *aiptek,
856 		  unsigned char report_type,
857 		  unsigned char report_id, void *buffer, int size)
858 {
859 	struct usb_device *udev = interface_to_usbdev(aiptek->intf);
860 
861 	return usb_control_msg(udev,
862 			       usb_sndctrlpipe(udev, 0),
863 			       HID_REQ_SET_REPORT,
864 			       USB_TYPE_CLASS | USB_RECIP_INTERFACE |
865 			       USB_DIR_OUT, (report_type << 8) + report_id,
866 			       aiptek->ifnum, buffer, size, 5000);
867 }
868 
869 static int
870 aiptek_get_report(struct aiptek *aiptek,
871 		  unsigned char report_type,
872 		  unsigned char report_id, void *buffer, int size)
873 {
874 	struct usb_device *udev = interface_to_usbdev(aiptek->intf);
875 
876 	return usb_control_msg(udev,
877 			       usb_rcvctrlpipe(udev, 0),
878 			       HID_REQ_GET_REPORT,
879 			       USB_TYPE_CLASS | USB_RECIP_INTERFACE |
880 			       USB_DIR_IN, (report_type << 8) + report_id,
881 			       aiptek->ifnum, buffer, size, 5000);
882 }
883 
884 /***********************************************************************
885  * Send a command to the tablet.
886  */
887 static int
888 aiptek_command(struct aiptek *aiptek, unsigned char command, unsigned char data)
889 {
890 	const int sizeof_buf = 3 * sizeof(u8);
891 	int ret;
892 	u8 *buf;
893 
894 	buf = kmalloc(sizeof_buf, GFP_KERNEL);
895 	if (!buf)
896 		return -ENOMEM;
897 
898 	buf[0] = 2;
899 	buf[1] = command;
900 	buf[2] = data;
901 
902 	if ((ret =
903 	     aiptek_set_report(aiptek, 3, 2, buf, sizeof_buf)) != sizeof_buf) {
904 		dev_dbg(&aiptek->intf->dev,
905 			"aiptek_program: failed, tried to send: 0x%02x 0x%02x\n",
906 			command, data);
907 	}
908 	kfree(buf);
909 	return ret < 0 ? ret : 0;
910 }
911 
912 /***********************************************************************
913  * Retrieve information from the tablet. Querying info is defined as first
914  * sending the {command,data} sequence as a command, followed by a wait
915  * (aka, "programmaticDelay") and then a "read" request.
916  */
917 static int
918 aiptek_query(struct aiptek *aiptek, unsigned char command, unsigned char data)
919 {
920 	const int sizeof_buf = 3 * sizeof(u8);
921 	int ret;
922 	u8 *buf;
923 
924 	buf = kmalloc(sizeof_buf, GFP_KERNEL);
925 	if (!buf)
926 		return -ENOMEM;
927 
928 	buf[0] = 2;
929 	buf[1] = command;
930 	buf[2] = data;
931 
932 	if (aiptek_command(aiptek, command, data) != 0) {
933 		kfree(buf);
934 		return -EIO;
935 	}
936 	msleep(aiptek->curSetting.programmableDelay);
937 
938 	if (aiptek_get_report(aiptek, 3, 2, buf, sizeof_buf) != sizeof_buf) {
939 		dev_dbg(&aiptek->intf->dev,
940 			"aiptek_query failed: returned 0x%02x 0x%02x 0x%02x\n",
941 			buf[0], buf[1], buf[2]);
942 		ret = -EIO;
943 	} else {
944 		ret = get_unaligned_le16(buf + 1);
945 	}
946 	kfree(buf);
947 	return ret;
948 }
949 
950 /***********************************************************************
951  * Program the tablet into either absolute or relative mode.
952  * We also get information about the tablet's size.
953  */
954 static int aiptek_program_tablet(struct aiptek *aiptek)
955 {
956 	int ret;
957 	/* Execute Resolution500LPI */
958 	if ((ret = aiptek_command(aiptek, 0x18, 0x04)) < 0)
959 		return ret;
960 
961 	/* Query getModelCode */
962 	if ((ret = aiptek_query(aiptek, 0x02, 0x00)) < 0)
963 		return ret;
964 	aiptek->features.modelCode = ret & 0xff;
965 
966 	/* Query getODMCode */
967 	if ((ret = aiptek_query(aiptek, 0x03, 0x00)) < 0)
968 		return ret;
969 	aiptek->features.odmCode = ret;
970 
971 	/* Query getFirmwareCode */
972 	if ((ret = aiptek_query(aiptek, 0x04, 0x00)) < 0)
973 		return ret;
974 	aiptek->features.firmwareCode = ret;
975 
976 	/* Query getXextension */
977 	if ((ret = aiptek_query(aiptek, 0x01, 0x00)) < 0)
978 		return ret;
979 	input_set_abs_params(aiptek->inputdev, ABS_X, 0, ret - 1, 0, 0);
980 
981 	/* Query getYextension */
982 	if ((ret = aiptek_query(aiptek, 0x01, 0x01)) < 0)
983 		return ret;
984 	input_set_abs_params(aiptek->inputdev, ABS_Y, 0, ret - 1, 0, 0);
985 
986 	/* Query getPressureLevels */
987 	if ((ret = aiptek_query(aiptek, 0x08, 0x00)) < 0)
988 		return ret;
989 	input_set_abs_params(aiptek->inputdev, ABS_PRESSURE, 0, ret - 1, 0, 0);
990 
991 	/* Depending on whether we are in absolute or relative mode, we will
992 	 * do a switchToTablet(absolute) or switchToMouse(relative) command.
993 	 */
994 	if (aiptek->curSetting.coordinateMode ==
995 	    AIPTEK_COORDINATE_ABSOLUTE_MODE) {
996 		/* Execute switchToTablet */
997 		if ((ret = aiptek_command(aiptek, 0x10, 0x01)) < 0) {
998 			return ret;
999 		}
1000 	} else {
1001 		/* Execute switchToMouse */
1002 		if ((ret = aiptek_command(aiptek, 0x10, 0x00)) < 0) {
1003 			return ret;
1004 		}
1005 	}
1006 
1007 	/* Enable the macro keys */
1008 	if ((ret = aiptek_command(aiptek, 0x11, 0x02)) < 0)
1009 		return ret;
1010 #if 0
1011 	/* Execute FilterOn */
1012 	if ((ret = aiptek_command(aiptek, 0x17, 0x00)) < 0)
1013 		return ret;
1014 #endif
1015 
1016 	/* Execute AutoGainOn */
1017 	if ((ret = aiptek_command(aiptek, 0x12, 0xff)) < 0)
1018 		return ret;
1019 
1020 	/* Reset the eventCount, so we track events from last (re)programming
1021 	 */
1022 	aiptek->diagnostic = AIPTEK_DIAGNOSTIC_NA;
1023 	aiptek->eventCount = 0;
1024 
1025 	return 0;
1026 }
1027 
1028 /***********************************************************************
1029  * Sysfs functions. Sysfs prefers that individually-tunable parameters
1030  * exist in their separate pseudo-files. Summary data that is immutable
1031  * may exist in a singular file so long as you don't define a writeable
1032  * interface.
1033  */
1034 
1035 /***********************************************************************
1036  * support the 'size' file -- display support
1037  */
1038 static ssize_t show_tabletSize(struct device *dev, struct device_attribute *attr, char *buf)
1039 {
1040 	struct aiptek *aiptek = dev_get_drvdata(dev);
1041 
1042 	return sysfs_emit(buf, "%dx%d\n",
1043 			  input_abs_get_max(aiptek->inputdev, ABS_X) + 1,
1044 			  input_abs_get_max(aiptek->inputdev, ABS_Y) + 1);
1045 }
1046 
1047 /* These structs define the sysfs files, param #1 is the name of the
1048  * file, param 2 is the file permissions, param 3 & 4 are to the
1049  * output generator and input parser routines. Absence of a routine is
1050  * permitted -- it only means can't either 'cat' the file, or send data
1051  * to it.
1052  */
1053 static DEVICE_ATTR(size, S_IRUGO, show_tabletSize, NULL);
1054 
1055 /***********************************************************************
1056  * support routines for the 'pointer_mode' file. Note that this file
1057  * both displays current setting and allows reprogramming.
1058  */
1059 static struct aiptek_map pointer_mode_map[] = {
1060 	{ "stylus",	AIPTEK_POINTER_ONLY_STYLUS_MODE },
1061 	{ "mouse",	AIPTEK_POINTER_ONLY_MOUSE_MODE },
1062 	{ "either",	AIPTEK_POINTER_EITHER_MODE },
1063 	{ NULL,		AIPTEK_INVALID_VALUE }
1064 };
1065 
1066 static ssize_t show_tabletPointerMode(struct device *dev, struct device_attribute *attr, char *buf)
1067 {
1068 	struct aiptek *aiptek = dev_get_drvdata(dev);
1069 
1070 	return sysfs_emit(buf, "%s\n", map_val_to_str(pointer_mode_map,
1071 						      aiptek->curSetting.pointerMode));
1072 }
1073 
1074 static ssize_t
1075 store_tabletPointerMode(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1076 {
1077 	struct aiptek *aiptek = dev_get_drvdata(dev);
1078 	int new_mode = map_str_to_val(pointer_mode_map, buf, count);
1079 
1080 	if (new_mode == AIPTEK_INVALID_VALUE)
1081 		return -EINVAL;
1082 
1083 	aiptek->newSetting.pointerMode = new_mode;
1084 	return count;
1085 }
1086 
1087 static DEVICE_ATTR(pointer_mode,
1088 		   S_IRUGO | S_IWUSR,
1089 		   show_tabletPointerMode, store_tabletPointerMode);
1090 
1091 /***********************************************************************
1092  * support routines for the 'coordinate_mode' file. Note that this file
1093  * both displays current setting and allows reprogramming.
1094  */
1095 
1096 static struct aiptek_map coordinate_mode_map[] = {
1097 	{ "absolute",	AIPTEK_COORDINATE_ABSOLUTE_MODE },
1098 	{ "relative",	AIPTEK_COORDINATE_RELATIVE_MODE },
1099 	{ NULL,		AIPTEK_INVALID_VALUE }
1100 };
1101 
1102 static ssize_t show_tabletCoordinateMode(struct device *dev, struct device_attribute *attr, char *buf)
1103 {
1104 	struct aiptek *aiptek = dev_get_drvdata(dev);
1105 
1106 	return sysfs_emit(buf, "%s\n", map_val_to_str(coordinate_mode_map,
1107 						      aiptek->curSetting.coordinateMode));
1108 }
1109 
1110 static ssize_t
1111 store_tabletCoordinateMode(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1112 {
1113 	struct aiptek *aiptek = dev_get_drvdata(dev);
1114 	int new_mode = map_str_to_val(coordinate_mode_map, buf, count);
1115 
1116 	if (new_mode == AIPTEK_INVALID_VALUE)
1117 		return -EINVAL;
1118 
1119 	aiptek->newSetting.coordinateMode = new_mode;
1120 	return count;
1121 }
1122 
1123 static DEVICE_ATTR(coordinate_mode,
1124 		   S_IRUGO | S_IWUSR,
1125 		   show_tabletCoordinateMode, store_tabletCoordinateMode);
1126 
1127 /***********************************************************************
1128  * support routines for the 'tool_mode' file. Note that this file
1129  * both displays current setting and allows reprogramming.
1130  */
1131 
1132 static struct aiptek_map tool_mode_map[] = {
1133 	{ "mouse",	AIPTEK_TOOL_BUTTON_MOUSE_MODE },
1134 	{ "eraser",	AIPTEK_TOOL_BUTTON_ERASER_MODE },
1135 	{ "pencil",	AIPTEK_TOOL_BUTTON_PENCIL_MODE },
1136 	{ "pen",	AIPTEK_TOOL_BUTTON_PEN_MODE },
1137 	{ "brush",	AIPTEK_TOOL_BUTTON_BRUSH_MODE },
1138 	{ "airbrush",	AIPTEK_TOOL_BUTTON_AIRBRUSH_MODE },
1139 	{ "lens",	AIPTEK_TOOL_BUTTON_LENS_MODE },
1140 	{ NULL,		AIPTEK_INVALID_VALUE }
1141 };
1142 
1143 static ssize_t show_tabletToolMode(struct device *dev, struct device_attribute *attr, char *buf)
1144 {
1145 	struct aiptek *aiptek = dev_get_drvdata(dev);
1146 
1147 	return sysfs_emit(buf, "%s\n", map_val_to_str(tool_mode_map,
1148 						      aiptek->curSetting.toolMode));
1149 }
1150 
1151 static ssize_t
1152 store_tabletToolMode(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1153 {
1154 	struct aiptek *aiptek = dev_get_drvdata(dev);
1155 	int new_mode = map_str_to_val(tool_mode_map, buf, count);
1156 
1157 	if (new_mode == AIPTEK_INVALID_VALUE)
1158 		return -EINVAL;
1159 
1160 	aiptek->newSetting.toolMode = new_mode;
1161 	return count;
1162 }
1163 
1164 static DEVICE_ATTR(tool_mode,
1165 		   S_IRUGO | S_IWUSR,
1166 		   show_tabletToolMode, store_tabletToolMode);
1167 
1168 /***********************************************************************
1169  * support routines for the 'xtilt' file. Note that this file
1170  * both displays current setting and allows reprogramming.
1171  */
1172 static ssize_t show_tabletXtilt(struct device *dev, struct device_attribute *attr, char *buf)
1173 {
1174 	struct aiptek *aiptek = dev_get_drvdata(dev);
1175 
1176 	if (aiptek->curSetting.xTilt == AIPTEK_TILT_DISABLE) {
1177 		return sysfs_emit(buf, "disable\n");
1178 	} else {
1179 		return sysfs_emit(buf, "%d\n", aiptek->curSetting.xTilt);
1180 	}
1181 }
1182 
1183 static ssize_t
1184 store_tabletXtilt(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1185 {
1186 	struct aiptek *aiptek = dev_get_drvdata(dev);
1187 	int x;
1188 
1189 	if (kstrtoint(buf, 10, &x)) {
1190 		size_t len = buf[count - 1] == '\n' ? count - 1 : count;
1191 
1192 		if (strncmp(buf, "disable", len))
1193 			return -EINVAL;
1194 
1195 		aiptek->newSetting.xTilt = AIPTEK_TILT_DISABLE;
1196 	} else {
1197 		if (x < AIPTEK_TILT_MIN || x > AIPTEK_TILT_MAX)
1198 			return -EINVAL;
1199 
1200 		aiptek->newSetting.xTilt = x;
1201 	}
1202 
1203 	return count;
1204 }
1205 
1206 static DEVICE_ATTR(xtilt,
1207 		   S_IRUGO | S_IWUSR, show_tabletXtilt, store_tabletXtilt);
1208 
1209 /***********************************************************************
1210  * support routines for the 'ytilt' file. Note that this file
1211  * both displays current setting and allows reprogramming.
1212  */
1213 static ssize_t show_tabletYtilt(struct device *dev, struct device_attribute *attr, char *buf)
1214 {
1215 	struct aiptek *aiptek = dev_get_drvdata(dev);
1216 
1217 	if (aiptek->curSetting.yTilt == AIPTEK_TILT_DISABLE) {
1218 		return sysfs_emit(buf, "disable\n");
1219 	} else {
1220 		return sysfs_emit(buf, "%d\n", aiptek->curSetting.yTilt);
1221 	}
1222 }
1223 
1224 static ssize_t
1225 store_tabletYtilt(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1226 {
1227 	struct aiptek *aiptek = dev_get_drvdata(dev);
1228 	int y;
1229 
1230 	if (kstrtoint(buf, 10, &y)) {
1231 		size_t len = buf[count - 1] == '\n' ? count - 1 : count;
1232 
1233 		if (strncmp(buf, "disable", len))
1234 			return -EINVAL;
1235 
1236 		aiptek->newSetting.yTilt = AIPTEK_TILT_DISABLE;
1237 	} else {
1238 		if (y < AIPTEK_TILT_MIN || y > AIPTEK_TILT_MAX)
1239 			return -EINVAL;
1240 
1241 		aiptek->newSetting.yTilt = y;
1242 	}
1243 
1244 	return count;
1245 }
1246 
1247 static DEVICE_ATTR(ytilt,
1248 		   S_IRUGO | S_IWUSR, show_tabletYtilt, store_tabletYtilt);
1249 
1250 /***********************************************************************
1251  * support routines for the 'jitter' file. Note that this file
1252  * both displays current setting and allows reprogramming.
1253  */
1254 static ssize_t show_tabletJitterDelay(struct device *dev, struct device_attribute *attr, char *buf)
1255 {
1256 	struct aiptek *aiptek = dev_get_drvdata(dev);
1257 
1258 	return sysfs_emit(buf, "%d\n", aiptek->curSetting.jitterDelay);
1259 }
1260 
1261 static ssize_t
1262 store_tabletJitterDelay(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1263 {
1264 	struct aiptek *aiptek = dev_get_drvdata(dev);
1265 	int err, j;
1266 
1267 	err = kstrtoint(buf, 10, &j);
1268 	if (err)
1269 		return err;
1270 
1271 	aiptek->newSetting.jitterDelay = j;
1272 	return count;
1273 }
1274 
1275 static DEVICE_ATTR(jitter,
1276 		   S_IRUGO | S_IWUSR,
1277 		   show_tabletJitterDelay, store_tabletJitterDelay);
1278 
1279 /***********************************************************************
1280  * support routines for the 'delay' file. Note that this file
1281  * both displays current setting and allows reprogramming.
1282  */
1283 static ssize_t show_tabletProgrammableDelay(struct device *dev, struct device_attribute *attr, char *buf)
1284 {
1285 	struct aiptek *aiptek = dev_get_drvdata(dev);
1286 
1287 	return sysfs_emit(buf, "%d\n", aiptek->curSetting.programmableDelay);
1288 }
1289 
1290 static ssize_t
1291 store_tabletProgrammableDelay(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1292 {
1293 	struct aiptek *aiptek = dev_get_drvdata(dev);
1294 	int err, d;
1295 
1296 	err = kstrtoint(buf, 10, &d);
1297 	if (err)
1298 		return err;
1299 
1300 	aiptek->newSetting.programmableDelay = d;
1301 	return count;
1302 }
1303 
1304 static DEVICE_ATTR(delay,
1305 		   S_IRUGO | S_IWUSR,
1306 		   show_tabletProgrammableDelay, store_tabletProgrammableDelay);
1307 
1308 /***********************************************************************
1309  * support routines for the 'event_count' file. Note that this file
1310  * only displays current setting.
1311  */
1312 static ssize_t show_tabletEventsReceived(struct device *dev, struct device_attribute *attr, char *buf)
1313 {
1314 	struct aiptek *aiptek = dev_get_drvdata(dev);
1315 
1316 	return sysfs_emit(buf, "%ld\n", aiptek->eventCount);
1317 }
1318 
1319 static DEVICE_ATTR(event_count, S_IRUGO, show_tabletEventsReceived, NULL);
1320 
1321 /***********************************************************************
1322  * support routines for the 'diagnostic' file. Note that this file
1323  * only displays current setting.
1324  */
1325 static ssize_t show_tabletDiagnosticMessage(struct device *dev, struct device_attribute *attr, char *buf)
1326 {
1327 	struct aiptek *aiptek = dev_get_drvdata(dev);
1328 	char *retMsg;
1329 
1330 	switch (aiptek->diagnostic) {
1331 	case AIPTEK_DIAGNOSTIC_NA:
1332 		retMsg = "no errors\n";
1333 		break;
1334 
1335 	case AIPTEK_DIAGNOSTIC_SENDING_RELATIVE_IN_ABSOLUTE:
1336 		retMsg = "Error: receiving relative reports\n";
1337 		break;
1338 
1339 	case AIPTEK_DIAGNOSTIC_SENDING_ABSOLUTE_IN_RELATIVE:
1340 		retMsg = "Error: receiving absolute reports\n";
1341 		break;
1342 
1343 	case AIPTEK_DIAGNOSTIC_TOOL_DISALLOWED:
1344 		if (aiptek->curSetting.pointerMode ==
1345 		    AIPTEK_POINTER_ONLY_MOUSE_MODE) {
1346 			retMsg = "Error: receiving stylus reports\n";
1347 		} else {
1348 			retMsg = "Error: receiving mouse reports\n";
1349 		}
1350 		break;
1351 
1352 	default:
1353 		return 0;
1354 	}
1355 	return sysfs_emit(buf, retMsg);
1356 }
1357 
1358 static DEVICE_ATTR(diagnostic, S_IRUGO, show_tabletDiagnosticMessage, NULL);
1359 
1360 /***********************************************************************
1361  * support routines for the 'stylus_upper' file. Note that this file
1362  * both displays current setting and allows for setting changing.
1363  */
1364 
1365 static struct aiptek_map stylus_button_map[] = {
1366 	{ "upper",	AIPTEK_STYLUS_UPPER_BUTTON },
1367 	{ "lower",	AIPTEK_STYLUS_LOWER_BUTTON },
1368 	{ NULL,		AIPTEK_INVALID_VALUE }
1369 };
1370 
1371 static ssize_t show_tabletStylusUpper(struct device *dev, struct device_attribute *attr, char *buf)
1372 {
1373 	struct aiptek *aiptek = dev_get_drvdata(dev);
1374 
1375 	return sysfs_emit(buf, "%s\n", map_val_to_str(stylus_button_map,
1376 						      aiptek->curSetting.stylusButtonUpper));
1377 }
1378 
1379 static ssize_t
1380 store_tabletStylusUpper(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1381 {
1382 	struct aiptek *aiptek = dev_get_drvdata(dev);
1383 	int new_button = map_str_to_val(stylus_button_map, buf, count);
1384 
1385 	if (new_button == AIPTEK_INVALID_VALUE)
1386 		return -EINVAL;
1387 
1388 	aiptek->newSetting.stylusButtonUpper = new_button;
1389 	return count;
1390 }
1391 
1392 static DEVICE_ATTR(stylus_upper,
1393 		   S_IRUGO | S_IWUSR,
1394 		   show_tabletStylusUpper, store_tabletStylusUpper);
1395 
1396 /***********************************************************************
1397  * support routines for the 'stylus_lower' file. Note that this file
1398  * both displays current setting and allows for setting changing.
1399  */
1400 
1401 static ssize_t show_tabletStylusLower(struct device *dev, struct device_attribute *attr, char *buf)
1402 {
1403 	struct aiptek *aiptek = dev_get_drvdata(dev);
1404 
1405 	return sysfs_emit(buf, "%s\n", map_val_to_str(stylus_button_map,
1406 						      aiptek->curSetting.stylusButtonLower));
1407 }
1408 
1409 static ssize_t
1410 store_tabletStylusLower(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1411 {
1412 	struct aiptek *aiptek = dev_get_drvdata(dev);
1413 	int new_button = map_str_to_val(stylus_button_map, buf, count);
1414 
1415 	if (new_button == AIPTEK_INVALID_VALUE)
1416 		return -EINVAL;
1417 
1418 	aiptek->newSetting.stylusButtonLower = new_button;
1419 	return count;
1420 }
1421 
1422 static DEVICE_ATTR(stylus_lower,
1423 		   S_IRUGO | S_IWUSR,
1424 		   show_tabletStylusLower, store_tabletStylusLower);
1425 
1426 /***********************************************************************
1427  * support routines for the 'mouse_left' file. Note that this file
1428  * both displays current setting and allows for setting changing.
1429  */
1430 
1431 static struct aiptek_map mouse_button_map[] = {
1432 	{ "left",	AIPTEK_MOUSE_LEFT_BUTTON },
1433 	{ "middle",	AIPTEK_MOUSE_MIDDLE_BUTTON },
1434 	{ "right",	AIPTEK_MOUSE_RIGHT_BUTTON },
1435 	{ NULL,		AIPTEK_INVALID_VALUE }
1436 };
1437 
1438 static ssize_t show_tabletMouseLeft(struct device *dev, struct device_attribute *attr, char *buf)
1439 {
1440 	struct aiptek *aiptek = dev_get_drvdata(dev);
1441 
1442 	return sysfs_emit(buf, "%s\n", map_val_to_str(mouse_button_map,
1443 						      aiptek->curSetting.mouseButtonLeft));
1444 }
1445 
1446 static ssize_t
1447 store_tabletMouseLeft(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1448 {
1449 	struct aiptek *aiptek = dev_get_drvdata(dev);
1450 	int new_button = map_str_to_val(mouse_button_map, buf, count);
1451 
1452 	if (new_button == AIPTEK_INVALID_VALUE)
1453 		return -EINVAL;
1454 
1455 	aiptek->newSetting.mouseButtonLeft = new_button;
1456 	return count;
1457 }
1458 
1459 static DEVICE_ATTR(mouse_left,
1460 		   S_IRUGO | S_IWUSR,
1461 		   show_tabletMouseLeft, store_tabletMouseLeft);
1462 
1463 /***********************************************************************
1464  * support routines for the 'mouse_middle' file. Note that this file
1465  * both displays current setting and allows for setting changing.
1466  */
1467 static ssize_t show_tabletMouseMiddle(struct device *dev, struct device_attribute *attr, char *buf)
1468 {
1469 	struct aiptek *aiptek = dev_get_drvdata(dev);
1470 
1471 	return sysfs_emit(buf, "%s\n", map_val_to_str(mouse_button_map,
1472 						      aiptek->curSetting.mouseButtonMiddle));
1473 }
1474 
1475 static ssize_t
1476 store_tabletMouseMiddle(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1477 {
1478 	struct aiptek *aiptek = dev_get_drvdata(dev);
1479 	int new_button = map_str_to_val(mouse_button_map, buf, count);
1480 
1481 	if (new_button == AIPTEK_INVALID_VALUE)
1482 		return -EINVAL;
1483 
1484 	aiptek->newSetting.mouseButtonMiddle = new_button;
1485 	return count;
1486 }
1487 
1488 static DEVICE_ATTR(mouse_middle,
1489 		   S_IRUGO | S_IWUSR,
1490 		   show_tabletMouseMiddle, store_tabletMouseMiddle);
1491 
1492 /***********************************************************************
1493  * support routines for the 'mouse_right' file. Note that this file
1494  * both displays current setting and allows for setting changing.
1495  */
1496 static ssize_t show_tabletMouseRight(struct device *dev, struct device_attribute *attr, char *buf)
1497 {
1498 	struct aiptek *aiptek = dev_get_drvdata(dev);
1499 
1500 	return sysfs_emit(buf, "%s\n", map_val_to_str(mouse_button_map,
1501 						      aiptek->curSetting.mouseButtonRight));
1502 }
1503 
1504 static ssize_t
1505 store_tabletMouseRight(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1506 {
1507 	struct aiptek *aiptek = dev_get_drvdata(dev);
1508 	int new_button = map_str_to_val(mouse_button_map, buf, count);
1509 
1510 	if (new_button == AIPTEK_INVALID_VALUE)
1511 		return -EINVAL;
1512 
1513 	aiptek->newSetting.mouseButtonRight = new_button;
1514 	return count;
1515 }
1516 
1517 static DEVICE_ATTR(mouse_right,
1518 		   S_IRUGO | S_IWUSR,
1519 		   show_tabletMouseRight, store_tabletMouseRight);
1520 
1521 /***********************************************************************
1522  * support routines for the 'wheel' file. Note that this file
1523  * both displays current setting and allows for setting changing.
1524  */
1525 static ssize_t show_tabletWheel(struct device *dev, struct device_attribute *attr, char *buf)
1526 {
1527 	struct aiptek *aiptek = dev_get_drvdata(dev);
1528 
1529 	if (aiptek->curSetting.wheel == AIPTEK_WHEEL_DISABLE) {
1530 		return sysfs_emit(buf, "disable\n");
1531 	} else {
1532 		return sysfs_emit(buf, "%d\n", aiptek->curSetting.wheel);
1533 	}
1534 }
1535 
1536 static ssize_t
1537 store_tabletWheel(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1538 {
1539 	struct aiptek *aiptek = dev_get_drvdata(dev);
1540 	int err, w;
1541 
1542 	err = kstrtoint(buf, 10, &w);
1543 	if (err)
1544 		return err;
1545 
1546 	aiptek->newSetting.wheel = w;
1547 	return count;
1548 }
1549 
1550 static DEVICE_ATTR(wheel,
1551 		   S_IRUGO | S_IWUSR, show_tabletWheel, store_tabletWheel);
1552 
1553 /***********************************************************************
1554  * support routines for the 'execute' file. Note that this file
1555  * both displays current setting and allows for setting changing.
1556  */
1557 static ssize_t show_tabletExecute(struct device *dev, struct device_attribute *attr, char *buf)
1558 {
1559 	/* There is nothing useful to display, so a one-line manual
1560 	 * is in order...
1561 	 */
1562 	return sysfs_emit(buf, "Write anything to this file to program your tablet.\n");
1563 }
1564 
1565 static ssize_t
1566 store_tabletExecute(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1567 {
1568 	struct aiptek *aiptek = dev_get_drvdata(dev);
1569 
1570 	/* We do not care what you write to this file. Merely the action
1571 	 * of writing to this file triggers a tablet reprogramming.
1572 	 */
1573 	memcpy(&aiptek->curSetting, &aiptek->newSetting,
1574 	       sizeof(struct aiptek_settings));
1575 
1576 	if (aiptek_program_tablet(aiptek) < 0)
1577 		return -EIO;
1578 
1579 	return count;
1580 }
1581 
1582 static DEVICE_ATTR(execute,
1583 		   S_IRUGO | S_IWUSR, show_tabletExecute, store_tabletExecute);
1584 
1585 /***********************************************************************
1586  * support routines for the 'odm_code' file. Note that this file
1587  * only displays current setting.
1588  */
1589 static ssize_t show_tabletODMCode(struct device *dev, struct device_attribute *attr, char *buf)
1590 {
1591 	struct aiptek *aiptek = dev_get_drvdata(dev);
1592 
1593 	return sysfs_emit(buf, "0x%04x\n", aiptek->features.odmCode);
1594 }
1595 
1596 static DEVICE_ATTR(odm_code, S_IRUGO, show_tabletODMCode, NULL);
1597 
1598 /***********************************************************************
1599  * support routines for the 'model_code' file. Note that this file
1600  * only displays current setting.
1601  */
1602 static ssize_t show_tabletModelCode(struct device *dev, struct device_attribute *attr, char *buf)
1603 {
1604 	struct aiptek *aiptek = dev_get_drvdata(dev);
1605 
1606 	return sysfs_emit(buf, "0x%04x\n", aiptek->features.modelCode);
1607 }
1608 
1609 static DEVICE_ATTR(model_code, S_IRUGO, show_tabletModelCode, NULL);
1610 
1611 /***********************************************************************
1612  * support routines for the 'firmware_code' file. Note that this file
1613  * only displays current setting.
1614  */
1615 static ssize_t show_firmwareCode(struct device *dev, struct device_attribute *attr, char *buf)
1616 {
1617 	struct aiptek *aiptek = dev_get_drvdata(dev);
1618 
1619 	return sysfs_emit(buf, "%04x\n", aiptek->features.firmwareCode);
1620 }
1621 
1622 static DEVICE_ATTR(firmware_code, S_IRUGO, show_firmwareCode, NULL);
1623 
1624 static struct attribute *aiptek_dev_attrs[] = {
1625 	&dev_attr_size.attr,
1626 	&dev_attr_pointer_mode.attr,
1627 	&dev_attr_coordinate_mode.attr,
1628 	&dev_attr_tool_mode.attr,
1629 	&dev_attr_xtilt.attr,
1630 	&dev_attr_ytilt.attr,
1631 	&dev_attr_jitter.attr,
1632 	&dev_attr_delay.attr,
1633 	&dev_attr_event_count.attr,
1634 	&dev_attr_diagnostic.attr,
1635 	&dev_attr_odm_code.attr,
1636 	&dev_attr_model_code.attr,
1637 	&dev_attr_firmware_code.attr,
1638 	&dev_attr_stylus_lower.attr,
1639 	&dev_attr_stylus_upper.attr,
1640 	&dev_attr_mouse_left.attr,
1641 	&dev_attr_mouse_middle.attr,
1642 	&dev_attr_mouse_right.attr,
1643 	&dev_attr_wheel.attr,
1644 	&dev_attr_execute.attr,
1645 	NULL
1646 };
1647 
1648 ATTRIBUTE_GROUPS(aiptek_dev);
1649 
1650 /***********************************************************************
1651  * This routine is called when a tablet has been identified. It basically
1652  * sets up the tablet and the driver's internal structures.
1653  */
1654 static int
1655 aiptek_probe(struct usb_interface *intf, const struct usb_device_id *id)
1656 {
1657 	struct usb_device *usbdev = interface_to_usbdev(intf);
1658 	struct usb_endpoint_descriptor *endpoint;
1659 	struct aiptek *aiptek;
1660 	struct input_dev *inputdev;
1661 	int i;
1662 	int speeds[] = { 0,
1663 		AIPTEK_PROGRAMMABLE_DELAY_50,
1664 		AIPTEK_PROGRAMMABLE_DELAY_400,
1665 		AIPTEK_PROGRAMMABLE_DELAY_25,
1666 		AIPTEK_PROGRAMMABLE_DELAY_100,
1667 		AIPTEK_PROGRAMMABLE_DELAY_200,
1668 		AIPTEK_PROGRAMMABLE_DELAY_300
1669 	};
1670 	int err = -ENOMEM;
1671 
1672 	/* programmableDelay is where the command-line specified
1673 	 * delay is kept. We make it the first element of speeds[],
1674 	 * so therefore, your override speed is tried first, then the
1675 	 * remainder. Note that the default value of 400ms will be tried
1676 	 * if you do not specify any command line parameter.
1677 	 */
1678 	speeds[0] = programmableDelay;
1679 
1680 	aiptek = kzalloc_obj(*aiptek);
1681 	inputdev = input_allocate_device();
1682 	if (!aiptek || !inputdev) {
1683 		dev_warn(&intf->dev,
1684 			 "cannot allocate memory or input device\n");
1685 		goto fail1;
1686         }
1687 
1688 	aiptek->data = usb_alloc_coherent(usbdev, AIPTEK_PACKET_LENGTH,
1689 					  GFP_KERNEL, &aiptek->data_dma);
1690         if (!aiptek->data) {
1691 		dev_warn(&intf->dev, "cannot allocate usb buffer\n");
1692 		goto fail1;
1693 	}
1694 
1695 	aiptek->urb = usb_alloc_urb(0, GFP_KERNEL);
1696 	if (!aiptek->urb) {
1697 	        dev_warn(&intf->dev, "cannot allocate urb\n");
1698 		goto fail2;
1699 	}
1700 
1701 	aiptek->inputdev = inputdev;
1702 	aiptek->intf = intf;
1703 	aiptek->ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
1704 	aiptek->inDelay = 0;
1705 	aiptek->endDelay = 0;
1706 	aiptek->previousJitterable = 0;
1707 	aiptek->lastMacro = -1;
1708 
1709 	/* Set up the curSettings struct. Said struct contains the current
1710 	 * programmable parameters. The newSetting struct contains changes
1711 	 * the user makes to the settings via the sysfs interface. Those
1712 	 * changes are not "committed" to curSettings until the user
1713 	 * writes to the sysfs/.../execute file.
1714 	 */
1715 	aiptek->curSetting.pointerMode = AIPTEK_POINTER_EITHER_MODE;
1716 	aiptek->curSetting.coordinateMode = AIPTEK_COORDINATE_ABSOLUTE_MODE;
1717 	aiptek->curSetting.toolMode = AIPTEK_TOOL_BUTTON_PEN_MODE;
1718 	aiptek->curSetting.xTilt = AIPTEK_TILT_DISABLE;
1719 	aiptek->curSetting.yTilt = AIPTEK_TILT_DISABLE;
1720 	aiptek->curSetting.mouseButtonLeft = AIPTEK_MOUSE_LEFT_BUTTON;
1721 	aiptek->curSetting.mouseButtonMiddle = AIPTEK_MOUSE_MIDDLE_BUTTON;
1722 	aiptek->curSetting.mouseButtonRight = AIPTEK_MOUSE_RIGHT_BUTTON;
1723 	aiptek->curSetting.stylusButtonUpper = AIPTEK_STYLUS_UPPER_BUTTON;
1724 	aiptek->curSetting.stylusButtonLower = AIPTEK_STYLUS_LOWER_BUTTON;
1725 	aiptek->curSetting.jitterDelay = jitterDelay;
1726 	aiptek->curSetting.programmableDelay = programmableDelay;
1727 
1728 	/* Both structs should have equivalent settings
1729 	 */
1730 	aiptek->newSetting = aiptek->curSetting;
1731 
1732 	/* Determine the usb devices' physical path.
1733 	 * Asketh not why we always pretend we're using "../input0",
1734 	 * but I suspect this will have to be refactored one
1735 	 * day if a single USB device can be a keyboard & a mouse
1736 	 * & a tablet, and the inputX number actually will tell
1737 	 * us something...
1738 	 */
1739 	usb_make_path(usbdev, aiptek->features.usbPath,
1740 			sizeof(aiptek->features.usbPath));
1741 	strlcat(aiptek->features.usbPath, "/input0",
1742 		sizeof(aiptek->features.usbPath));
1743 
1744 	/* Set up client data, pointers to open and close routines
1745 	 * for the input device.
1746 	 */
1747 	inputdev->name = "Aiptek";
1748 	inputdev->phys = aiptek->features.usbPath;
1749 	usb_to_input_id(usbdev, &inputdev->id);
1750 	inputdev->dev.parent = &intf->dev;
1751 
1752 	input_set_drvdata(inputdev, aiptek);
1753 
1754 	inputdev->open = aiptek_open;
1755 	inputdev->close = aiptek_close;
1756 
1757 	/* Now program the capacities of the tablet, in terms of being
1758 	 * an input device.
1759 	 */
1760 	for (i = 0; i < ARRAY_SIZE(eventTypes); ++i)
1761 	        __set_bit(eventTypes[i], inputdev->evbit);
1762 
1763 	for (i = 0; i < ARRAY_SIZE(absEvents); ++i)
1764 	        __set_bit(absEvents[i], inputdev->absbit);
1765 
1766 	for (i = 0; i < ARRAY_SIZE(relEvents); ++i)
1767 	        __set_bit(relEvents[i], inputdev->relbit);
1768 
1769 	__set_bit(MSC_SERIAL, inputdev->mscbit);
1770 
1771 	/* Set up key and button codes */
1772 	for (i = 0; i < ARRAY_SIZE(buttonEvents); ++i)
1773 		__set_bit(buttonEvents[i], inputdev->keybit);
1774 
1775 	for (i = 0; i < ARRAY_SIZE(macroKeyEvents); ++i)
1776 		__set_bit(macroKeyEvents[i], inputdev->keybit);
1777 
1778 	/*
1779 	 * Program the input device coordinate capacities. We do not yet
1780 	 * know what maximum X, Y, and Z values are, so we're putting fake
1781 	 * values in. Later, we'll ask the tablet to put in the correct
1782 	 * values.
1783 	 */
1784 	input_set_abs_params(inputdev, ABS_X, 0, 2999, 0, 0);
1785 	input_set_abs_params(inputdev, ABS_Y, 0, 2249, 0, 0);
1786 	input_set_abs_params(inputdev, ABS_PRESSURE, 0, 511, 0, 0);
1787 	input_set_abs_params(inputdev, ABS_TILT_X, AIPTEK_TILT_MIN, AIPTEK_TILT_MAX, 0, 0);
1788 	input_set_abs_params(inputdev, ABS_TILT_Y, AIPTEK_TILT_MIN, AIPTEK_TILT_MAX, 0, 0);
1789 	input_set_abs_params(inputdev, ABS_WHEEL, AIPTEK_WHEEL_MIN, AIPTEK_WHEEL_MAX - 1, 0, 0);
1790 
1791 	err = usb_find_common_endpoints(intf->cur_altsetting,
1792 					NULL, NULL, &endpoint, NULL);
1793 	if (err) {
1794 		dev_err(&intf->dev,
1795 			"interface has no int in endpoints, but must have minimum 1\n");
1796 		goto fail3;
1797 	}
1798 
1799 	/* Go set up our URB, which is called when the tablet receives
1800 	 * input.
1801 	 */
1802 	usb_fill_int_urb(aiptek->urb,
1803 			 usbdev,
1804 			 usb_rcvintpipe(usbdev,
1805 					endpoint->bEndpointAddress),
1806 			 aiptek->data, 8, aiptek_irq, aiptek,
1807 			 endpoint->bInterval);
1808 
1809 	aiptek->urb->transfer_dma = aiptek->data_dma;
1810 	aiptek->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1811 
1812 	/* Program the tablet. This sets the tablet up in the mode
1813 	 * specified in newSetting, and also queries the tablet's
1814 	 * physical capacities.
1815 	 *
1816 	 * Sanity check: if a tablet doesn't like the slow programmatic
1817 	 * delay, we often get sizes of 0x0. Let's use that as an indicator
1818 	 * to try faster delays, up to 25 ms. If that logic fails, well, you'll
1819 	 * have to explain to us how your tablet thinks it's 0x0, and yet that's
1820 	 * not an error :-)
1821 	 */
1822 
1823 	for (i = 0; i < ARRAY_SIZE(speeds); ++i) {
1824 		aiptek->curSetting.programmableDelay = speeds[i];
1825 		(void)aiptek_program_tablet(aiptek);
1826 		if (input_abs_get_max(aiptek->inputdev, ABS_X) > 0) {
1827 			dev_info(&intf->dev,
1828 				 "Aiptek using %d ms programming speed\n",
1829 				 aiptek->curSetting.programmableDelay);
1830 			break;
1831 		}
1832 	}
1833 
1834 	/* Murphy says that some day someone will have a tablet that fails the
1835 	   above test. That's you, Frederic Rodrigo */
1836 	if (i == ARRAY_SIZE(speeds)) {
1837 		dev_info(&intf->dev,
1838 			 "Aiptek tried all speeds, no sane response\n");
1839 		err = -EINVAL;
1840 		goto fail3;
1841 	}
1842 
1843 	/* Associate this driver's struct with the usb interface.
1844 	 */
1845 	usb_set_intfdata(intf, aiptek);
1846 
1847 	/* Register the tablet as an Input Device
1848 	 */
1849 	err = input_register_device(aiptek->inputdev);
1850 	if (err) {
1851 		dev_warn(&intf->dev,
1852 			 "input_register_device returned err: %d\n", err);
1853 		goto fail3;
1854         }
1855 	return 0;
1856 
1857  fail3: usb_free_urb(aiptek->urb);
1858  fail2:	usb_free_coherent(usbdev, AIPTEK_PACKET_LENGTH, aiptek->data,
1859 			  aiptek->data_dma);
1860  fail1: usb_set_intfdata(intf, NULL);
1861 	input_free_device(inputdev);
1862 	kfree(aiptek);
1863 	return err;
1864 }
1865 
1866 /***********************************************************************
1867  * Deal with tablet disconnecting from the system.
1868  */
1869 static void aiptek_disconnect(struct usb_interface *intf)
1870 {
1871 	struct aiptek *aiptek = usb_get_intfdata(intf);
1872 
1873 	/* Disassociate driver's struct with usb interface
1874 	 */
1875 	usb_set_intfdata(intf, NULL);
1876 	if (aiptek != NULL) {
1877 		/* Free & unhook everything from the system.
1878 		 */
1879 		usb_kill_urb(aiptek->urb);
1880 		input_unregister_device(aiptek->inputdev);
1881 		usb_free_urb(aiptek->urb);
1882 		usb_free_coherent(interface_to_usbdev(intf),
1883 				  AIPTEK_PACKET_LENGTH,
1884 				  aiptek->data, aiptek->data_dma);
1885 		kfree(aiptek);
1886 	}
1887 }
1888 
1889 static struct usb_driver aiptek_driver = {
1890 	.name = "aiptek",
1891 	.probe = aiptek_probe,
1892 	.disconnect = aiptek_disconnect,
1893 	.id_table = aiptek_ids,
1894 	.dev_groups = aiptek_dev_groups,
1895 };
1896 
1897 module_usb_driver(aiptek_driver);
1898 
1899 MODULE_AUTHOR("Bryan W. Headley/Chris Atenasio/Cedric Brun/Rene van Paassen");
1900 MODULE_DESCRIPTION("Aiptek HyperPen USB Tablet Driver");
1901 MODULE_LICENSE("GPL");
1902 
1903 module_param(programmableDelay, int, 0);
1904 MODULE_PARM_DESC(programmableDelay, "delay used during tablet programming");
1905 module_param(jitterDelay, int, 0);
1906 MODULE_PARM_DESC(jitterDelay, "stylus/mouse settlement delay");
1907