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