xref: /openbmc/linux/drivers/input/tablet/aiptek.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
24104d13fSDmitry Torokhov /*
34104d13fSDmitry Torokhov  *  Native support for the Aiptek HyperPen USB Tablets
44104d13fSDmitry Torokhov  *  (4000U/5000U/6000U/8000U/12000U)
54104d13fSDmitry Torokhov  *
64104d13fSDmitry Torokhov  *  Copyright (c) 2001      Chris Atenasio   <chris@crud.net>
74104d13fSDmitry Torokhov  *  Copyright (c) 2002-2004 Bryan W. Headley <bwheadley@earthlink.net>
84104d13fSDmitry Torokhov  *
94104d13fSDmitry Torokhov  *  based on wacom.c by
104104d13fSDmitry Torokhov  *     Vojtech Pavlik      <vojtech@suse.cz>
114104d13fSDmitry Torokhov  *     Andreas Bach Aaen   <abach@stofanet.dk>
124104d13fSDmitry Torokhov  *     Clifford Wolf       <clifford@clifford.at>
134104d13fSDmitry Torokhov  *     Sam Mosel           <sam.mosel@computer.org>
144104d13fSDmitry Torokhov  *     James E. Blair      <corvus@gnu.org>
154104d13fSDmitry Torokhov  *     Daniel Egger        <egger@suse.de>
164104d13fSDmitry Torokhov  *
174104d13fSDmitry Torokhov  *  Many thanks to Oliver Kuechemann for his support.
184104d13fSDmitry Torokhov  *
194104d13fSDmitry Torokhov  *  ChangeLog:
204104d13fSDmitry Torokhov  *      v0.1 - Initial release
214104d13fSDmitry Torokhov  *      v0.2 - Hack to get around fake event 28's. (Bryan W. Headley)
224104d13fSDmitry Torokhov  *      v0.3 - Make URB dynamic (Bryan W. Headley, Jun-8-2002)
234104d13fSDmitry Torokhov  *             Released to Linux 2.4.19 and 2.5.x
244104d13fSDmitry Torokhov  *      v0.4 - Rewrote substantial portions of the code to deal with
254104d13fSDmitry Torokhov  *             corrected control sequences, timing, dynamic configuration,
264104d13fSDmitry Torokhov  *             support of 6000U - 12000U, procfs, and macro key support
274104d13fSDmitry Torokhov  *             (Jan-1-2003 - Feb-5-2003, Bryan W. Headley)
284104d13fSDmitry Torokhov  *      v1.0 - Added support for diagnostic messages, count of messages
294104d13fSDmitry Torokhov  *             received from URB - Mar-8-2003, Bryan W. Headley
304104d13fSDmitry Torokhov  *      v1.1 - added support for tablet resolution, changed DV and proximity
314104d13fSDmitry Torokhov  *             some corrections - Jun-22-2003, martin schneebacher
324104d13fSDmitry Torokhov  *           - Added support for the sysfs interface, deprecating the
334104d13fSDmitry Torokhov  *             procfs interface for 2.5.x kernel. Also added support for
344104d13fSDmitry Torokhov  *             Wheel command. Bryan W. Headley July-15-2003.
354104d13fSDmitry Torokhov  *      v1.2 - Reworked jitter timer as a kernel thread.
364104d13fSDmitry Torokhov  *             Bryan W. Headley November-28-2003/Jan-10-2004.
374104d13fSDmitry Torokhov  *      v1.3 - Repaired issue of kernel thread going nuts on single-processor
384104d13fSDmitry Torokhov  *             machines, introduced programmableDelay as a command line
394104d13fSDmitry Torokhov  *             parameter. Feb 7 2004, Bryan W. Headley.
404104d13fSDmitry Torokhov  *      v1.4 - Re-wire jitter so it does not require a thread. Courtesy of
414104d13fSDmitry Torokhov  *             Rene van Paassen. Added reporting of physical pointer device
424104d13fSDmitry Torokhov  *             (e.g., stylus, mouse in reports 2, 3, 4, 5. We don't know
434104d13fSDmitry Torokhov  *             for reports 1, 6.)
444104d13fSDmitry Torokhov  *             what physical device reports for reports 1, 6.) Also enabled
454104d13fSDmitry Torokhov  *             MOUSE and LENS tool button modes. Renamed "rubber" to "eraser".
464104d13fSDmitry Torokhov  *             Feb 20, 2004, Bryan W. Headley.
474104d13fSDmitry Torokhov  *      v1.5 - Added previousJitterable, so we don't do jitter delay when the
484104d13fSDmitry Torokhov  *             user is holding a button down for periods of time.
494104d13fSDmitry Torokhov  *
504104d13fSDmitry Torokhov  * NOTE:
514104d13fSDmitry Torokhov  *      This kernel driver is augmented by the "Aiptek" XFree86 input
524104d13fSDmitry Torokhov  *      driver for your X server, as well as the Gaiptek GUI Front-end
534104d13fSDmitry Torokhov  *      "Tablet Manager".
544104d13fSDmitry Torokhov  *      These three products are highly interactive with one another,
554104d13fSDmitry Torokhov  *      so therefore it's easier to document them all as one subsystem.
564104d13fSDmitry Torokhov  *      Please visit the project's "home page", located at,
574104d13fSDmitry Torokhov  *      http://aiptektablet.sourceforge.net.
584104d13fSDmitry Torokhov  */
594104d13fSDmitry Torokhov 
604104d13fSDmitry Torokhov #include <linux/jiffies.h>
614104d13fSDmitry Torokhov #include <linux/kernel.h>
624104d13fSDmitry Torokhov #include <linux/slab.h>
634104d13fSDmitry Torokhov #include <linux/module.h>
644104d13fSDmitry Torokhov #include <linux/usb/input.h>
657c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
664104d13fSDmitry Torokhov #include <asm/unaligned.h>
674104d13fSDmitry Torokhov 
684104d13fSDmitry Torokhov /*
694104d13fSDmitry Torokhov  * Aiptek status packet:
704104d13fSDmitry Torokhov  *
714104d13fSDmitry Torokhov  * (returned as Report 1 - relative coordinates from mouse and stylus)
724104d13fSDmitry Torokhov  *
734104d13fSDmitry Torokhov  *        bit7  bit6  bit5  bit4  bit3  bit2  bit1  bit0
744104d13fSDmitry Torokhov  * byte0   0     0     0     0     0     0     0     1
754104d13fSDmitry Torokhov  * byte1   0     0     0     0     0    BS2   BS    Tip
764104d13fSDmitry Torokhov  * byte2  X7    X6    X5    X4    X3    X2    X1    X0
774104d13fSDmitry Torokhov  * byte3  Y7    Y6    Y5    Y4    Y3    Y2    Y1    Y0
784104d13fSDmitry Torokhov  *
794104d13fSDmitry Torokhov  * (returned as Report 2 - absolute coordinates from the stylus)
804104d13fSDmitry Torokhov  *
814104d13fSDmitry Torokhov  *        bit7  bit6  bit5  bit4  bit3  bit2  bit1  bit0
824104d13fSDmitry Torokhov  * byte0   0     0     0     0     0     0     1     0
834104d13fSDmitry Torokhov  * byte1  X7    X6    X5    X4    X3    X2    X1    X0
844104d13fSDmitry Torokhov  * byte2  X15   X14   X13   X12   X11   X10   X9    X8
854104d13fSDmitry Torokhov  * byte3  Y7    Y6    Y5    Y4    Y3    Y2    Y1    Y0
864104d13fSDmitry Torokhov  * byte4  Y15   Y14   Y13   Y12   Y11   Y10   Y9    Y8
874104d13fSDmitry Torokhov  * byte5   *     *     *    BS2   BS1   Tip   IR    DV
884104d13fSDmitry Torokhov  * byte6  P7    P6    P5    P4    P3    P2    P1    P0
894104d13fSDmitry Torokhov  * byte7  P15   P14   P13   P12   P11   P10   P9    P8
904104d13fSDmitry Torokhov  *
914104d13fSDmitry Torokhov  * (returned as Report 3 - absolute coordinates from the mouse)
924104d13fSDmitry Torokhov  *
934104d13fSDmitry Torokhov  *        bit7  bit6  bit5  bit4  bit3  bit2  bit1  bit0
942fe57416SRene van Paassen  * byte0   0     0     0     0     0     0     1     1
954104d13fSDmitry Torokhov  * byte1  X7    X6    X5    X4    X3    X2    X1    X0
964104d13fSDmitry Torokhov  * byte2  X15   X14   X13   X12   X11   X10   X9    X8
974104d13fSDmitry Torokhov  * byte3  Y7    Y6    Y5    Y4    Y3    Y2    Y1    Y0
984104d13fSDmitry Torokhov  * byte4  Y15   Y14   Y13   Y12   Y11   Y10   Y9    Y8
994104d13fSDmitry Torokhov  * byte5   *     *     *    BS2   BS1   Tip   IR    DV
1004104d13fSDmitry Torokhov  * byte6  P7    P6    P5    P4    P3    P2    P1    P0
1014104d13fSDmitry Torokhov  * byte7  P15   P14   P13   P12   P11   P10   P9    P8
1024104d13fSDmitry Torokhov  *
1034104d13fSDmitry Torokhov  * (returned as Report 4 - macrokeys from the stylus)
1044104d13fSDmitry Torokhov  *
1054104d13fSDmitry Torokhov  *        bit7  bit6  bit5  bit4  bit3  bit2  bit1  bit0
1064104d13fSDmitry Torokhov  * byte0   0     0     0     0     0     1     0     0
1074104d13fSDmitry Torokhov  * byte1   0     0     0    BS2   BS    Tip   IR    DV
1084104d13fSDmitry Torokhov  * byte2   0     0     0     0     0     0     1     0
1094104d13fSDmitry Torokhov  * byte3   0     0     0    K4    K3    K2    K1    K0
1104104d13fSDmitry Torokhov  * byte4  P7    P6    P5    P4    P3    P2    P1    P0
1114104d13fSDmitry Torokhov  * byte5  P15   P14   P13   P12   P11   P10   P9    P8
1124104d13fSDmitry Torokhov  *
1134104d13fSDmitry Torokhov  * (returned as Report 5 - macrokeys from the mouse)
1144104d13fSDmitry Torokhov  *
1154104d13fSDmitry Torokhov  *        bit7  bit6  bit5  bit4  bit3  bit2  bit1  bit0
1162fe57416SRene van Paassen  * byte0   0     0     0     0     0     1     0     1
1174104d13fSDmitry Torokhov  * byte1   0     0     0    BS2   BS    Tip   IR    DV
1184104d13fSDmitry Torokhov  * byte2   0     0     0     0     0     0     1     0
1194104d13fSDmitry Torokhov  * byte3   0     0     0    K4    K3    K2    K1    K0
1204104d13fSDmitry Torokhov  * byte4  P7    P6    P5    P4    P3    P2    P1    P0
1214104d13fSDmitry Torokhov  * byte5  P15   P14   P13   P12   P11   P10   P9    P8
1224104d13fSDmitry Torokhov  *
1234104d13fSDmitry Torokhov  * IR: In Range = Proximity on
1244104d13fSDmitry Torokhov  * DV = Data Valid
1254104d13fSDmitry Torokhov  * BS = Barrel Switch (as in, macro keys)
1264104d13fSDmitry Torokhov  * BS2 also referred to as Tablet Pick
1274104d13fSDmitry Torokhov  *
1284104d13fSDmitry Torokhov  * Command Summary:
1294104d13fSDmitry Torokhov  *
1304104d13fSDmitry Torokhov  * Use report_type CONTROL (3)
1314104d13fSDmitry Torokhov  * Use report_id   2
1324104d13fSDmitry Torokhov  *
1334104d13fSDmitry Torokhov  * Command/Data    Description     Return Bytes    Return Value
1344104d13fSDmitry Torokhov  * 0x10/0x00       SwitchToMouse       0
1354104d13fSDmitry Torokhov  * 0x10/0x01       SwitchToTablet      0
1364104d13fSDmitry Torokhov  * 0x18/0x04       SetResolution       0
1374104d13fSDmitry Torokhov  * 0x12/0xFF       AutoGainOn          0
1384104d13fSDmitry Torokhov  * 0x17/0x00       FilterOn            0
1394104d13fSDmitry Torokhov  * 0x01/0x00       GetXExtension       2           MaxX
1404104d13fSDmitry Torokhov  * 0x01/0x01       GetYExtension       2           MaxY
1414104d13fSDmitry Torokhov  * 0x02/0x00       GetModelCode        2           ModelCode = LOBYTE
1424104d13fSDmitry Torokhov  * 0x03/0x00       GetODMCode          2           ODMCode
1434104d13fSDmitry Torokhov  * 0x08/0x00       GetPressureLevels   2           =512
1444104d13fSDmitry Torokhov  * 0x04/0x00       GetFirmwareVersion  2           Firmware Version
1454104d13fSDmitry Torokhov  * 0x11/0x02       EnableMacroKeys     0
1464104d13fSDmitry Torokhov  *
1474104d13fSDmitry Torokhov  * To initialize the tablet:
1484104d13fSDmitry Torokhov  *
1494104d13fSDmitry Torokhov  * (1) Send Resolution500LPI (Command)
1504104d13fSDmitry Torokhov  * (2) Query for Model code (Option Report)
1514104d13fSDmitry Torokhov  * (3) Query for ODM code (Option Report)
1524104d13fSDmitry Torokhov  * (4) Query for firmware (Option Report)
1534104d13fSDmitry Torokhov  * (5) Query for GetXExtension (Option Report)
1544104d13fSDmitry Torokhov  * (6) Query for GetYExtension (Option Report)
1554104d13fSDmitry Torokhov  * (7) Query for GetPressureLevels (Option Report)
1564104d13fSDmitry Torokhov  * (8) SwitchToTablet for Absolute coordinates, or
1574104d13fSDmitry Torokhov  *     SwitchToMouse for Relative coordinates (Command)
1584104d13fSDmitry Torokhov  * (9) EnableMacroKeys (Command)
1594104d13fSDmitry Torokhov  * (10) FilterOn (Command)
1604104d13fSDmitry Torokhov  * (11) AutoGainOn (Command)
1614104d13fSDmitry Torokhov  *
1624104d13fSDmitry Torokhov  * (Step 9 can be omitted, but you'll then have no function keys.)
1634104d13fSDmitry Torokhov  */
1644104d13fSDmitry Torokhov 
1654104d13fSDmitry Torokhov #define USB_VENDOR_ID_AIPTEK				0x08ca
166a32bcc45SGuryanov Dmitry #define USB_VENDOR_ID_KYE				0x0458
1674104d13fSDmitry Torokhov #define USB_REQ_GET_REPORT				0x01
1684104d13fSDmitry Torokhov #define USB_REQ_SET_REPORT				0x09
1694104d13fSDmitry Torokhov 
1704104d13fSDmitry Torokhov 	/* PointerMode codes
1714104d13fSDmitry Torokhov 	 */
1724104d13fSDmitry Torokhov #define AIPTEK_POINTER_ONLY_MOUSE_MODE			0
1734104d13fSDmitry Torokhov #define AIPTEK_POINTER_ONLY_STYLUS_MODE			1
1744104d13fSDmitry Torokhov #define AIPTEK_POINTER_EITHER_MODE			2
1754104d13fSDmitry Torokhov 
1764104d13fSDmitry Torokhov #define AIPTEK_POINTER_ALLOW_MOUSE_MODE(a)		\
1774104d13fSDmitry Torokhov 	(a == AIPTEK_POINTER_ONLY_MOUSE_MODE ||		\
1784104d13fSDmitry Torokhov 	 a == AIPTEK_POINTER_EITHER_MODE)
1794104d13fSDmitry Torokhov #define AIPTEK_POINTER_ALLOW_STYLUS_MODE(a)		\
1804104d13fSDmitry Torokhov 	(a == AIPTEK_POINTER_ONLY_STYLUS_MODE ||	\
1814104d13fSDmitry Torokhov 	 a == AIPTEK_POINTER_EITHER_MODE)
1824104d13fSDmitry Torokhov 
1834104d13fSDmitry Torokhov 	/* CoordinateMode code
1844104d13fSDmitry Torokhov 	 */
1854104d13fSDmitry Torokhov #define AIPTEK_COORDINATE_RELATIVE_MODE			0
1864104d13fSDmitry Torokhov #define AIPTEK_COORDINATE_ABSOLUTE_MODE			1
1874104d13fSDmitry Torokhov 
1884104d13fSDmitry Torokhov        /* XTilt and YTilt values
1894104d13fSDmitry Torokhov         */
1904104d13fSDmitry Torokhov #define AIPTEK_TILT_MIN					(-128)
1914104d13fSDmitry Torokhov #define AIPTEK_TILT_MAX					127
1924104d13fSDmitry Torokhov #define AIPTEK_TILT_DISABLE				(-10101)
1934104d13fSDmitry Torokhov 
1944104d13fSDmitry Torokhov 	/* Wheel values
1954104d13fSDmitry Torokhov 	 */
1964104d13fSDmitry Torokhov #define AIPTEK_WHEEL_MIN				0
1974104d13fSDmitry Torokhov #define AIPTEK_WHEEL_MAX				1024
1984104d13fSDmitry Torokhov #define AIPTEK_WHEEL_DISABLE				(-10101)
1994104d13fSDmitry Torokhov 
2004104d13fSDmitry Torokhov 	/* ToolCode values, which BTW are 0x140 .. 0x14f
201b3b6cf1dSRene van Paassen 	 * We have things set up such that if the tool button has changed,
202b3b6cf1dSRene van Paassen 	 * the tools get reset.
2034104d13fSDmitry Torokhov 	 */
2044104d13fSDmitry Torokhov 	/* toolMode codes
2054104d13fSDmitry Torokhov 	 */
2064104d13fSDmitry Torokhov #define AIPTEK_TOOL_BUTTON_PEN_MODE			BTN_TOOL_PEN
2074104d13fSDmitry Torokhov #define AIPTEK_TOOL_BUTTON_PENCIL_MODE			BTN_TOOL_PENCIL
2084104d13fSDmitry Torokhov #define AIPTEK_TOOL_BUTTON_BRUSH_MODE			BTN_TOOL_BRUSH
2094104d13fSDmitry Torokhov #define AIPTEK_TOOL_BUTTON_AIRBRUSH_MODE		BTN_TOOL_AIRBRUSH
2104104d13fSDmitry Torokhov #define AIPTEK_TOOL_BUTTON_ERASER_MODE			BTN_TOOL_RUBBER
2114104d13fSDmitry Torokhov #define AIPTEK_TOOL_BUTTON_MOUSE_MODE			BTN_TOOL_MOUSE
2124104d13fSDmitry Torokhov #define AIPTEK_TOOL_BUTTON_LENS_MODE			BTN_TOOL_LENS
2134104d13fSDmitry Torokhov 
2144104d13fSDmitry Torokhov 	/* Diagnostic message codes
2154104d13fSDmitry Torokhov 	 */
2164104d13fSDmitry Torokhov #define AIPTEK_DIAGNOSTIC_NA				0
2174104d13fSDmitry Torokhov #define AIPTEK_DIAGNOSTIC_SENDING_RELATIVE_IN_ABSOLUTE	1
2184104d13fSDmitry Torokhov #define AIPTEK_DIAGNOSTIC_SENDING_ABSOLUTE_IN_RELATIVE	2
2194104d13fSDmitry Torokhov #define AIPTEK_DIAGNOSTIC_TOOL_DISALLOWED		3
2204104d13fSDmitry Torokhov 
2214104d13fSDmitry Torokhov 	/* Time to wait (in ms) to help mask hand jittering
2224104d13fSDmitry Torokhov 	 * when pressing the stylus buttons.
2234104d13fSDmitry Torokhov 	 */
2244104d13fSDmitry Torokhov #define AIPTEK_JITTER_DELAY_DEFAULT			50
2254104d13fSDmitry Torokhov 
2264104d13fSDmitry Torokhov 	/* Time to wait (in ms) in-between sending the tablet
2274104d13fSDmitry Torokhov 	 * a command and beginning the process of reading the return
2284104d13fSDmitry Torokhov 	 * sequence from the tablet.
2294104d13fSDmitry Torokhov 	 */
2304104d13fSDmitry Torokhov #define AIPTEK_PROGRAMMABLE_DELAY_25		25
2314104d13fSDmitry Torokhov #define AIPTEK_PROGRAMMABLE_DELAY_50		50
2324104d13fSDmitry Torokhov #define AIPTEK_PROGRAMMABLE_DELAY_100		100
2334104d13fSDmitry Torokhov #define AIPTEK_PROGRAMMABLE_DELAY_200		200
2344104d13fSDmitry Torokhov #define AIPTEK_PROGRAMMABLE_DELAY_300		300
2354104d13fSDmitry Torokhov #define AIPTEK_PROGRAMMABLE_DELAY_400		400
2364104d13fSDmitry Torokhov #define AIPTEK_PROGRAMMABLE_DELAY_DEFAULT	AIPTEK_PROGRAMMABLE_DELAY_400
2374104d13fSDmitry Torokhov 
2384104d13fSDmitry Torokhov 	/* Mouse button programming
2394104d13fSDmitry Torokhov 	 */
240ce0982edSRene van Paassen #define AIPTEK_MOUSE_LEFT_BUTTON		0x04
241ce0982edSRene van Paassen #define AIPTEK_MOUSE_RIGHT_BUTTON		0x08
242ce0982edSRene van Paassen #define AIPTEK_MOUSE_MIDDLE_BUTTON		0x10
2434104d13fSDmitry Torokhov 
2444104d13fSDmitry Torokhov 	/* Stylus button programming
2454104d13fSDmitry Torokhov 	 */
2464104d13fSDmitry Torokhov #define AIPTEK_STYLUS_LOWER_BUTTON		0x08
2474104d13fSDmitry Torokhov #define AIPTEK_STYLUS_UPPER_BUTTON		0x10
2484104d13fSDmitry Torokhov 
2494104d13fSDmitry Torokhov 	/* Length of incoming packet from the tablet
2504104d13fSDmitry Torokhov 	 */
2514104d13fSDmitry Torokhov #define AIPTEK_PACKET_LENGTH			8
2524104d13fSDmitry Torokhov 
2534104d13fSDmitry Torokhov 	/* We report in EV_MISC both the proximity and
2544104d13fSDmitry Torokhov 	 * whether the report came from the stylus, tablet mouse
2554104d13fSDmitry Torokhov 	 * or "unknown" -- Unknown when the tablet is in relative
2564104d13fSDmitry Torokhov 	 * mode, because we only get report 1's.
2574104d13fSDmitry Torokhov 	 */
2584104d13fSDmitry Torokhov #define AIPTEK_REPORT_TOOL_UNKNOWN		0x10
2594104d13fSDmitry Torokhov #define AIPTEK_REPORT_TOOL_STYLUS		0x20
2604104d13fSDmitry Torokhov #define AIPTEK_REPORT_TOOL_MOUSE		0x40
2614104d13fSDmitry Torokhov 
2624104d13fSDmitry Torokhov static int programmableDelay = AIPTEK_PROGRAMMABLE_DELAY_DEFAULT;
2634104d13fSDmitry Torokhov static int jitterDelay = AIPTEK_JITTER_DELAY_DEFAULT;
2644104d13fSDmitry Torokhov 
2654104d13fSDmitry Torokhov struct aiptek_features {
2664104d13fSDmitry Torokhov 	int odmCode;		/* Tablet manufacturer code       */
2674104d13fSDmitry Torokhov 	int modelCode;		/* Tablet model code (not unique) */
2684104d13fSDmitry Torokhov 	int firmwareCode;	/* prom/eeprom version            */
2694104d13fSDmitry Torokhov 	char usbPath[64 + 1];	/* device's physical usb path     */
2704104d13fSDmitry Torokhov };
2714104d13fSDmitry Torokhov 
2724104d13fSDmitry Torokhov struct aiptek_settings {
2734104d13fSDmitry Torokhov 	int pointerMode;	/* stylus-, mouse-only or either */
2744104d13fSDmitry Torokhov 	int coordinateMode;	/* absolute/relative coords      */
2754104d13fSDmitry Torokhov 	int toolMode;		/* pen, pencil, brush, etc. tool */
2764104d13fSDmitry Torokhov 	int xTilt;		/* synthetic xTilt amount        */
2774104d13fSDmitry Torokhov 	int yTilt;		/* synthetic yTilt amount        */
2784104d13fSDmitry Torokhov 	int wheel;		/* synthetic wheel amount        */
2794104d13fSDmitry Torokhov 	int stylusButtonUpper;	/* stylus upper btn delivers...  */
2804104d13fSDmitry Torokhov 	int stylusButtonLower;	/* stylus lower btn delivers...  */
2814104d13fSDmitry Torokhov 	int mouseButtonLeft;	/* mouse left btn delivers...    */
2824104d13fSDmitry Torokhov 	int mouseButtonMiddle;	/* mouse middle btn delivers...  */
2834104d13fSDmitry Torokhov 	int mouseButtonRight;	/* mouse right btn delivers...   */
2844104d13fSDmitry Torokhov 	int programmableDelay;	/* delay for tablet programming  */
2854104d13fSDmitry Torokhov 	int jitterDelay;	/* delay for hand jittering      */
2864104d13fSDmitry Torokhov };
2874104d13fSDmitry Torokhov 
2884104d13fSDmitry Torokhov struct aiptek {
2894104d13fSDmitry Torokhov 	struct input_dev *inputdev;		/* input device struct           */
290871ba51cSGreg Kroah-Hartman 	struct usb_interface *intf;		/* usb interface struct          */
2914104d13fSDmitry Torokhov 	struct urb *urb;			/* urb for incoming reports      */
2924104d13fSDmitry Torokhov 	dma_addr_t data_dma;			/* our dma stuffage              */
2934104d13fSDmitry Torokhov 	struct aiptek_features features;	/* tablet's array of features    */
2944104d13fSDmitry Torokhov 	struct aiptek_settings curSetting;	/* tablet's current programmable */
2954104d13fSDmitry Torokhov 	struct aiptek_settings newSetting;	/* ... and new param settings    */
2964104d13fSDmitry Torokhov 	unsigned int ifnum;			/* interface number for IO       */
2974104d13fSDmitry Torokhov 	int diagnostic;				/* tablet diagnostic codes       */
2984104d13fSDmitry Torokhov 	unsigned long eventCount;		/* event count                   */
2994104d13fSDmitry Torokhov 	int inDelay;				/* jitter: in jitter delay?      */
3004104d13fSDmitry Torokhov 	unsigned long endDelay;			/* jitter: time when delay ends  */
3014104d13fSDmitry Torokhov 	int previousJitterable;			/* jitterable prev value     */
302b3b6cf1dSRene van Paassen 
303b3b6cf1dSRene van Paassen 	int lastMacro;				/* macro key to reset            */
304b3b6cf1dSRene van Paassen 	int previousToolMode;			/* pen, pencil, brush, etc. tool */
3054104d13fSDmitry Torokhov 	unsigned char *data;			/* incoming packet data          */
3064104d13fSDmitry Torokhov };
3074104d13fSDmitry Torokhov 
3081a54f49eSRene van Paassen static const int eventTypes[] = {
3091a54f49eSRene van Paassen         EV_KEY, EV_ABS, EV_REL, EV_MSC,
3101a54f49eSRene van Paassen };
3111a54f49eSRene van Paassen 
3121a54f49eSRene van Paassen static const int absEvents[] = {
3131a54f49eSRene van Paassen         ABS_X, ABS_Y, ABS_PRESSURE, ABS_TILT_X, ABS_TILT_Y,
3141a54f49eSRene van Paassen         ABS_WHEEL, ABS_MISC,
3151a54f49eSRene van Paassen };
3161a54f49eSRene van Paassen 
3171a54f49eSRene van Paassen static const int relEvents[] = {
3181a54f49eSRene van Paassen         REL_X, REL_Y, REL_WHEEL,
3191a54f49eSRene van Paassen };
3201a54f49eSRene van Paassen 
32133936fa6SDmitry Torokhov static const int buttonEvents[] = {
32233936fa6SDmitry Torokhov 	BTN_LEFT, BTN_RIGHT, BTN_MIDDLE,
32333936fa6SDmitry Torokhov 	BTN_TOOL_PEN, BTN_TOOL_RUBBER, BTN_TOOL_PENCIL, BTN_TOOL_AIRBRUSH,
32433936fa6SDmitry Torokhov 	BTN_TOOL_BRUSH, BTN_TOOL_MOUSE, BTN_TOOL_LENS, BTN_TOUCH,
32533936fa6SDmitry Torokhov 	BTN_STYLUS, BTN_STYLUS2,
32633936fa6SDmitry Torokhov };
32733936fa6SDmitry Torokhov 
3284104d13fSDmitry Torokhov /*
3294104d13fSDmitry Torokhov  * Permit easy lookup of keyboard events to send, versus
3304104d13fSDmitry Torokhov  * the bitmap which comes from the tablet. This hides the
3314104d13fSDmitry Torokhov  * issue that the F_keys are not sequentially numbered.
3324104d13fSDmitry Torokhov  */
3334104d13fSDmitry Torokhov static const int macroKeyEvents[] = {
3344104d13fSDmitry Torokhov 	KEY_ESC, KEY_F1, KEY_F2, KEY_F3, KEY_F4, KEY_F5,
3354104d13fSDmitry Torokhov 	KEY_F6, KEY_F7, KEY_F8, KEY_F9, KEY_F10, KEY_F11,
3364104d13fSDmitry Torokhov 	KEY_F12, KEY_F13, KEY_F14, KEY_F15, KEY_F16, KEY_F17,
3374104d13fSDmitry Torokhov 	KEY_F18, KEY_F19, KEY_F20, KEY_F21, KEY_F22, KEY_F23,
3384104d13fSDmitry Torokhov 	KEY_F24, KEY_STOP, KEY_AGAIN, KEY_PROPS, KEY_UNDO,
3394104d13fSDmitry Torokhov 	KEY_FRONT, KEY_COPY, KEY_OPEN, KEY_PASTE, 0
3404104d13fSDmitry Torokhov };
3414104d13fSDmitry Torokhov 
3424104d13fSDmitry Torokhov /***********************************************************************
343c9404c9cSAdam Buchbinder  * Map values to strings and back. Every map should have the following
344cd438a58SDmitry Torokhov  * as its last element: { NULL, AIPTEK_INVALID_VALUE }.
345cd438a58SDmitry Torokhov  */
346cd438a58SDmitry Torokhov #define AIPTEK_INVALID_VALUE	-1
347cd438a58SDmitry Torokhov 
348cd438a58SDmitry Torokhov struct aiptek_map {
349cd438a58SDmitry Torokhov 	const char *string;
350cd438a58SDmitry Torokhov 	int value;
351cd438a58SDmitry Torokhov };
352cd438a58SDmitry Torokhov 
map_str_to_val(const struct aiptek_map * map,const char * str,size_t count)353cd438a58SDmitry Torokhov static int map_str_to_val(const struct aiptek_map *map, const char *str, size_t count)
354cd438a58SDmitry Torokhov {
355cd438a58SDmitry Torokhov 	const struct aiptek_map *p;
356cd438a58SDmitry Torokhov 
3570112db36SRene van Paassen 	if (str[count - 1] == '\n')
3580112db36SRene van Paassen 		count--;
3590112db36SRene van Paassen 
360cd438a58SDmitry Torokhov 	for (p = map; p->string; p++)
361cd438a58SDmitry Torokhov 	        if (!strncmp(str, p->string, count))
362cd438a58SDmitry Torokhov 			return p->value;
363cd438a58SDmitry Torokhov 
364cd438a58SDmitry Torokhov 	return AIPTEK_INVALID_VALUE;
365cd438a58SDmitry Torokhov }
366cd438a58SDmitry Torokhov 
map_val_to_str(const struct aiptek_map * map,int val)367cd438a58SDmitry Torokhov static const char *map_val_to_str(const struct aiptek_map *map, int val)
368cd438a58SDmitry Torokhov {
369cd438a58SDmitry Torokhov 	const struct aiptek_map *p;
370cd438a58SDmitry Torokhov 
371cd438a58SDmitry Torokhov 	for (p = map; p->value != AIPTEK_INVALID_VALUE; p++)
372cd438a58SDmitry Torokhov 		if (val == p->value)
373cd438a58SDmitry Torokhov 			return p->string;
374cd438a58SDmitry Torokhov 
375cd438a58SDmitry Torokhov 	return "unknown";
376cd438a58SDmitry Torokhov }
377cd438a58SDmitry Torokhov 
378cd438a58SDmitry Torokhov /***********************************************************************
3794104d13fSDmitry Torokhov  * aiptek_irq can receive one of six potential reports.
3804104d13fSDmitry Torokhov  * The documentation for each is in the body of the function.
3814104d13fSDmitry Torokhov  *
3824104d13fSDmitry Torokhov  * The tablet reports on several attributes per invocation of
3834104d13fSDmitry Torokhov  * aiptek_irq. Because the Linux Input Event system allows the
3844104d13fSDmitry Torokhov  * transmission of ONE attribute per input_report_xxx() call,
3854104d13fSDmitry Torokhov  * collation has to be done on the other end to reconstitute
3864104d13fSDmitry Torokhov  * a complete tablet report. Further, the number of Input Event reports
3874104d13fSDmitry Torokhov  * submitted varies, depending on what USB report type, and circumstance.
3884104d13fSDmitry Torokhov  * To deal with this, EV_MSC is used to indicate an 'end-of-report'
3894104d13fSDmitry Torokhov  * message. This has been an undocumented convention understood by the kernel
3904104d13fSDmitry Torokhov  * tablet driver and clients such as gpm and XFree86's tablet drivers.
3914104d13fSDmitry Torokhov  *
3924104d13fSDmitry Torokhov  * Of the information received from the tablet, the one piece I
3934104d13fSDmitry Torokhov  * cannot transmit is the proximity bit (without resorting to an EV_MSC
3944104d13fSDmitry Torokhov  * convention above.) I therefore have taken over REL_MISC and ABS_MISC
3954104d13fSDmitry Torokhov  * (for relative and absolute reports, respectively) for communicating
3964104d13fSDmitry Torokhov  * Proximity. Why two events? I thought it interesting to know if the
3974104d13fSDmitry Torokhov  * Proximity event occurred while the tablet was in absolute or relative
3984104d13fSDmitry Torokhov  * mode.
399da9fda43SRene van Paassen  * Update: REL_MISC proved not to be such a good idea. With REL_MISC you
400da9fda43SRene van Paassen  * get an event transmitted each time. ABS_MISC works better, since it
401da9fda43SRene van Paassen  * can be set and re-set. Thus, only using ABS_MISC from now on.
4024104d13fSDmitry Torokhov  *
4034104d13fSDmitry Torokhov  * Other tablets use the notion of a certain minimum stylus pressure
4044104d13fSDmitry Torokhov  * to infer proximity. While that could have been done, that is yet
4054104d13fSDmitry Torokhov  * another 'by convention' behavior, the documentation for which
4064104d13fSDmitry Torokhov  * would be spread between two (or more) pieces of software.
4074104d13fSDmitry Torokhov  *
4084104d13fSDmitry Torokhov  * EV_MSC usage was terminated for this purpose in Linux 2.5.x, and
4094104d13fSDmitry Torokhov  * replaced with the input_sync() method (which emits EV_SYN.)
4104104d13fSDmitry Torokhov  */
4114104d13fSDmitry Torokhov 
aiptek_irq(struct urb * urb)4124104d13fSDmitry Torokhov static void aiptek_irq(struct urb *urb)
4134104d13fSDmitry Torokhov {
4144104d13fSDmitry Torokhov 	struct aiptek *aiptek = urb->context;
4154104d13fSDmitry Torokhov 	unsigned char *data = aiptek->data;
4164104d13fSDmitry Torokhov 	struct input_dev *inputdev = aiptek->inputdev;
417871ba51cSGreg Kroah-Hartman 	struct usb_interface *intf = aiptek->intf;
4184104d13fSDmitry Torokhov 	int jitterable = 0;
4194104d13fSDmitry Torokhov 	int retval, macro, x, y, z, left, right, middle, p, dv, tip, bs, pck;
4204104d13fSDmitry Torokhov 
4214104d13fSDmitry Torokhov 	switch (urb->status) {
4224104d13fSDmitry Torokhov 	case 0:
4234104d13fSDmitry Torokhov 		/* Success */
4244104d13fSDmitry Torokhov 		break;
4254104d13fSDmitry Torokhov 
4264104d13fSDmitry Torokhov 	case -ECONNRESET:
4274104d13fSDmitry Torokhov 	case -ENOENT:
4284104d13fSDmitry Torokhov 	case -ESHUTDOWN:
4294104d13fSDmitry Torokhov 		/* This urb is terminated, clean up */
430871ba51cSGreg Kroah-Hartman 		dev_dbg(&intf->dev, "%s - urb shutting down with status: %d\n",
431ea3e6c59SHarvey Harrison 			__func__, urb->status);
4324104d13fSDmitry Torokhov 		return;
4334104d13fSDmitry Torokhov 
4344104d13fSDmitry Torokhov 	default:
435871ba51cSGreg Kroah-Hartman 		dev_dbg(&intf->dev, "%s - nonzero urb status received: %d\n",
436ea3e6c59SHarvey Harrison 			__func__, urb->status);
4374104d13fSDmitry Torokhov 		goto exit;
4384104d13fSDmitry Torokhov 	}
4394104d13fSDmitry Torokhov 
4404104d13fSDmitry Torokhov 	/* See if we are in a delay loop -- throw out report if true.
4414104d13fSDmitry Torokhov 	 */
4424104d13fSDmitry Torokhov 	if (aiptek->inDelay == 1 && time_after(aiptek->endDelay, jiffies)) {
4434104d13fSDmitry Torokhov 		goto exit;
4444104d13fSDmitry Torokhov 	}
4454104d13fSDmitry Torokhov 
4464104d13fSDmitry Torokhov 	aiptek->inDelay = 0;
4474104d13fSDmitry Torokhov 	aiptek->eventCount++;
4484104d13fSDmitry Torokhov 
4494104d13fSDmitry Torokhov 	/* Report 1 delivers relative coordinates with either a stylus
4504104d13fSDmitry Torokhov 	 * or the mouse. You do not know, however, which input
4514104d13fSDmitry Torokhov 	 * tool generated the event.
4524104d13fSDmitry Torokhov 	 */
4534104d13fSDmitry Torokhov 	if (data[0] == 1) {
4544104d13fSDmitry Torokhov 		if (aiptek->curSetting.coordinateMode ==
4554104d13fSDmitry Torokhov 		    AIPTEK_COORDINATE_ABSOLUTE_MODE) {
4564104d13fSDmitry Torokhov 			aiptek->diagnostic =
4574104d13fSDmitry Torokhov 			    AIPTEK_DIAGNOSTIC_SENDING_RELATIVE_IN_ABSOLUTE;
4584104d13fSDmitry Torokhov 		} else {
45937767b66SDmitry Torokhov 			x = (signed char) data[2];
46037767b66SDmitry Torokhov 			y = (signed char) data[3];
4614104d13fSDmitry Torokhov 
4624104d13fSDmitry Torokhov 			/* jitterable keeps track of whether any button has been pressed.
4634104d13fSDmitry Torokhov 			 * We're also using it to remap the physical mouse button mask
4644104d13fSDmitry Torokhov 			 * to pseudo-settings. (We don't specifically care about it's
4654104d13fSDmitry Torokhov 			 * value after moving/transposing mouse button bitmasks, except
4664104d13fSDmitry Torokhov 			 * that a non-zero value indicates that one or more
4674104d13fSDmitry Torokhov 			 * mouse button was pressed.)
4684104d13fSDmitry Torokhov 			 */
4690038cae0SMark Vytlacil 			jitterable = data[1] & 0x07;
4704104d13fSDmitry Torokhov 
4710038cae0SMark Vytlacil 			left = (data[1] & aiptek->curSetting.mouseButtonLeft >> 2) != 0 ? 1 : 0;
4720038cae0SMark Vytlacil 			right = (data[1] & aiptek->curSetting.mouseButtonRight >> 2) != 0 ? 1 : 0;
4730038cae0SMark Vytlacil 			middle = (data[1] & aiptek->curSetting.mouseButtonMiddle >> 2) != 0 ? 1 : 0;
4744104d13fSDmitry Torokhov 
4754104d13fSDmitry Torokhov 			input_report_key(inputdev, BTN_LEFT, left);
4764104d13fSDmitry Torokhov 			input_report_key(inputdev, BTN_MIDDLE, middle);
4774104d13fSDmitry Torokhov 			input_report_key(inputdev, BTN_RIGHT, right);
4780038cae0SMark Vytlacil 
4790038cae0SMark Vytlacil 			input_report_abs(inputdev, ABS_MISC,
4800038cae0SMark Vytlacil 					 1 | AIPTEK_REPORT_TOOL_UNKNOWN);
4814104d13fSDmitry Torokhov 			input_report_rel(inputdev, REL_X, x);
4824104d13fSDmitry Torokhov 			input_report_rel(inputdev, REL_Y, y);
4834104d13fSDmitry Torokhov 
4844104d13fSDmitry Torokhov 			/* Wheel support is in the form of a single-event
4854104d13fSDmitry Torokhov 			 * firing.
4864104d13fSDmitry Torokhov 			 */
4874104d13fSDmitry Torokhov 			if (aiptek->curSetting.wheel != AIPTEK_WHEEL_DISABLE) {
4884104d13fSDmitry Torokhov 				input_report_rel(inputdev, REL_WHEEL,
4894104d13fSDmitry Torokhov 						 aiptek->curSetting.wheel);
4904104d13fSDmitry Torokhov 				aiptek->curSetting.wheel = AIPTEK_WHEEL_DISABLE;
4914104d13fSDmitry Torokhov 			}
4921e7b3faeSRene van Paassen 			if (aiptek->lastMacro != -1) {
4931e7b3faeSRene van Paassen 			        input_report_key(inputdev,
4941e7b3faeSRene van Paassen 						 macroKeyEvents[aiptek->lastMacro], 0);
4951e7b3faeSRene van Paassen 				aiptek->lastMacro = -1;
4961e7b3faeSRene van Paassen 			}
4974104d13fSDmitry Torokhov 			input_sync(inputdev);
4984104d13fSDmitry Torokhov 		}
4994104d13fSDmitry Torokhov 	}
5004104d13fSDmitry Torokhov 	/* Report 2 is delivered only by the stylus, and delivers
5014104d13fSDmitry Torokhov 	 * absolute coordinates.
5024104d13fSDmitry Torokhov 	 */
5034104d13fSDmitry Torokhov 	else if (data[0] == 2) {
5044104d13fSDmitry Torokhov 		if (aiptek->curSetting.coordinateMode == AIPTEK_COORDINATE_RELATIVE_MODE) {
5054104d13fSDmitry Torokhov 			aiptek->diagnostic = AIPTEK_DIAGNOSTIC_SENDING_ABSOLUTE_IN_RELATIVE;
5064104d13fSDmitry Torokhov 		} else if (!AIPTEK_POINTER_ALLOW_STYLUS_MODE
5074104d13fSDmitry Torokhov 			    (aiptek->curSetting.pointerMode)) {
5084104d13fSDmitry Torokhov 				aiptek->diagnostic = AIPTEK_DIAGNOSTIC_TOOL_DISALLOWED;
5094104d13fSDmitry Torokhov 		} else {
510858ad08cSHarvey Harrison 			x = get_unaligned_le16(data + 1);
511858ad08cSHarvey Harrison 			y = get_unaligned_le16(data + 3);
512858ad08cSHarvey Harrison 			z = get_unaligned_le16(data + 6);
5134104d13fSDmitry Torokhov 
514fe981f23SRene van Paassen 			dv = (data[5] & 0x01) != 0 ? 1 : 0;
515fe981f23SRene van Paassen 			p = (data[5] & 0x02) != 0 ? 1 : 0;
5164104d13fSDmitry Torokhov 			tip = (data[5] & 0x04) != 0 ? 1 : 0;
5174104d13fSDmitry Torokhov 
5184104d13fSDmitry Torokhov 			/* Use jitterable to re-arrange button masks
5194104d13fSDmitry Torokhov 			 */
5204104d13fSDmitry Torokhov 			jitterable = data[5] & 0x18;
5214104d13fSDmitry Torokhov 
5224104d13fSDmitry Torokhov 			bs = (data[5] & aiptek->curSetting.stylusButtonLower) != 0 ? 1 : 0;
5234104d13fSDmitry Torokhov 			pck = (data[5] & aiptek->curSetting.stylusButtonUpper) != 0 ? 1 : 0;
5244104d13fSDmitry Torokhov 
5254104d13fSDmitry Torokhov 			/* dv indicates 'data valid' (e.g., the tablet is in sync
5264104d13fSDmitry Torokhov 			 * and has delivered a "correct" report) We will ignore
5274104d13fSDmitry Torokhov 			 * all 'bad' reports...
5284104d13fSDmitry Torokhov 			 */
5294104d13fSDmitry Torokhov 			if (dv != 0) {
530b3b6cf1dSRene van Paassen 				/* If the selected tool changed, reset the old
531b3b6cf1dSRene van Paassen 				 * tool key, and set the new one.
5324104d13fSDmitry Torokhov 				 */
533b3b6cf1dSRene van Paassen 				if (aiptek->previousToolMode !=
534b3b6cf1dSRene van Paassen 				    aiptek->curSetting.toolMode) {
5354104d13fSDmitry Torokhov 				        input_report_key(inputdev,
536b3b6cf1dSRene van Paassen 							 aiptek->previousToolMode, 0);
537b3b6cf1dSRene van Paassen 					input_report_key(inputdev,
538b3b6cf1dSRene van Paassen 							 aiptek->curSetting.toolMode,
5394104d13fSDmitry Torokhov 							 1);
540b3b6cf1dSRene van Paassen 					aiptek->previousToolMode =
541b3b6cf1dSRene van Paassen 					          aiptek->curSetting.toolMode;
5424104d13fSDmitry Torokhov 				}
5434104d13fSDmitry Torokhov 
5444104d13fSDmitry Torokhov 				if (p != 0) {
5454104d13fSDmitry Torokhov 					input_report_abs(inputdev, ABS_X, x);
5464104d13fSDmitry Torokhov 					input_report_abs(inputdev, ABS_Y, y);
5474104d13fSDmitry Torokhov 					input_report_abs(inputdev, ABS_PRESSURE, z);
5484104d13fSDmitry Torokhov 
5494104d13fSDmitry Torokhov 					input_report_key(inputdev, BTN_TOUCH, tip);
5504104d13fSDmitry Torokhov 					input_report_key(inputdev, BTN_STYLUS, bs);
5514104d13fSDmitry Torokhov 					input_report_key(inputdev, BTN_STYLUS2, pck);
5524104d13fSDmitry Torokhov 
5534104d13fSDmitry Torokhov 					if (aiptek->curSetting.xTilt !=
5544104d13fSDmitry Torokhov 					    AIPTEK_TILT_DISABLE) {
5554104d13fSDmitry Torokhov 						input_report_abs(inputdev,
5564104d13fSDmitry Torokhov 								 ABS_TILT_X,
5574104d13fSDmitry Torokhov 								 aiptek->curSetting.xTilt);
5584104d13fSDmitry Torokhov 					}
5594104d13fSDmitry Torokhov 					if (aiptek->curSetting.yTilt != AIPTEK_TILT_DISABLE) {
5604104d13fSDmitry Torokhov 						input_report_abs(inputdev,
5614104d13fSDmitry Torokhov 								 ABS_TILT_Y,
5624104d13fSDmitry Torokhov 								 aiptek->curSetting.yTilt);
5634104d13fSDmitry Torokhov 					}
5644104d13fSDmitry Torokhov 
5654104d13fSDmitry Torokhov 					/* Wheel support is in the form of a single-event
5664104d13fSDmitry Torokhov 					 * firing.
5674104d13fSDmitry Torokhov 					 */
5684104d13fSDmitry Torokhov 					if (aiptek->curSetting.wheel !=
5694104d13fSDmitry Torokhov 					    AIPTEK_WHEEL_DISABLE) {
5704104d13fSDmitry Torokhov 						input_report_abs(inputdev,
5714104d13fSDmitry Torokhov 								 ABS_WHEEL,
5724104d13fSDmitry Torokhov 								 aiptek->curSetting.wheel);
5734104d13fSDmitry Torokhov 						aiptek->curSetting.wheel = AIPTEK_WHEEL_DISABLE;
5744104d13fSDmitry Torokhov 					}
5754104d13fSDmitry Torokhov 				}
5764104d13fSDmitry Torokhov 				input_report_abs(inputdev, ABS_MISC, p | AIPTEK_REPORT_TOOL_STYLUS);
5771e7b3faeSRene van Paassen 				if (aiptek->lastMacro != -1) {
5781e7b3faeSRene van Paassen 			                input_report_key(inputdev,
5791e7b3faeSRene van Paassen 							 macroKeyEvents[aiptek->lastMacro], 0);
5801e7b3faeSRene van Paassen 					aiptek->lastMacro = -1;
5811e7b3faeSRene van Paassen 				}
5824104d13fSDmitry Torokhov 				input_sync(inputdev);
5834104d13fSDmitry Torokhov 			}
5844104d13fSDmitry Torokhov 		}
5854104d13fSDmitry Torokhov 	}
5864104d13fSDmitry Torokhov 	/* Report 3's come from the mouse in absolute mode.
5874104d13fSDmitry Torokhov 	 */
5884104d13fSDmitry Torokhov 	else if (data[0] == 3) {
5894104d13fSDmitry Torokhov 		if (aiptek->curSetting.coordinateMode == AIPTEK_COORDINATE_RELATIVE_MODE) {
5904104d13fSDmitry Torokhov 			aiptek->diagnostic = AIPTEK_DIAGNOSTIC_SENDING_ABSOLUTE_IN_RELATIVE;
5914104d13fSDmitry Torokhov 		} else if (!AIPTEK_POINTER_ALLOW_MOUSE_MODE
5924104d13fSDmitry Torokhov 			(aiptek->curSetting.pointerMode)) {
5934104d13fSDmitry Torokhov 			aiptek->diagnostic = AIPTEK_DIAGNOSTIC_TOOL_DISALLOWED;
5944104d13fSDmitry Torokhov 		} else {
595858ad08cSHarvey Harrison 			x = get_unaligned_le16(data + 1);
596858ad08cSHarvey Harrison 			y = get_unaligned_le16(data + 3);
5974104d13fSDmitry Torokhov 
5984104d13fSDmitry Torokhov 			jitterable = data[5] & 0x1c;
5994104d13fSDmitry Torokhov 
600fe981f23SRene van Paassen 			dv = (data[5] & 0x01) != 0 ? 1 : 0;
601fe981f23SRene van Paassen 			p = (data[5] & 0x02) != 0 ? 1 : 0;
6024104d13fSDmitry Torokhov 			left = (data[5] & aiptek->curSetting.mouseButtonLeft) != 0 ? 1 : 0;
6034104d13fSDmitry Torokhov 			right = (data[5] & aiptek->curSetting.mouseButtonRight) != 0 ? 1 : 0;
6044104d13fSDmitry Torokhov 			middle = (data[5] & aiptek->curSetting.mouseButtonMiddle) != 0 ? 1 : 0;
6054104d13fSDmitry Torokhov 
6064104d13fSDmitry Torokhov 			if (dv != 0) {
607b3b6cf1dSRene van Paassen 				/* If the selected tool changed, reset the old
608b3b6cf1dSRene van Paassen 				 * tool key, and set the new one.
6094104d13fSDmitry Torokhov 				 */
610b3b6cf1dSRene van Paassen 				if (aiptek->previousToolMode !=
611b3b6cf1dSRene van Paassen 				    aiptek->curSetting.toolMode) {
6124104d13fSDmitry Torokhov 				        input_report_key(inputdev,
613b3b6cf1dSRene van Paassen 							 aiptek->previousToolMode, 0);
614b3b6cf1dSRene van Paassen 					input_report_key(inputdev,
615b3b6cf1dSRene van Paassen 							 aiptek->curSetting.toolMode,
6164104d13fSDmitry Torokhov 							 1);
617b3b6cf1dSRene van Paassen 					aiptek->previousToolMode =
618b3b6cf1dSRene van Paassen 					          aiptek->curSetting.toolMode;
6194104d13fSDmitry Torokhov 				}
6204104d13fSDmitry Torokhov 
6214104d13fSDmitry Torokhov 				if (p != 0) {
6224104d13fSDmitry Torokhov 					input_report_abs(inputdev, ABS_X, x);
6234104d13fSDmitry Torokhov 					input_report_abs(inputdev, ABS_Y, y);
6244104d13fSDmitry Torokhov 
6254104d13fSDmitry Torokhov 					input_report_key(inputdev, BTN_LEFT, left);
6264104d13fSDmitry Torokhov 					input_report_key(inputdev, BTN_MIDDLE, middle);
6274104d13fSDmitry Torokhov 					input_report_key(inputdev, BTN_RIGHT, right);
6284104d13fSDmitry Torokhov 
6294104d13fSDmitry Torokhov 					/* Wheel support is in the form of a single-event
6304104d13fSDmitry Torokhov 					 * firing.
6314104d13fSDmitry Torokhov 					 */
6324104d13fSDmitry Torokhov 					if (aiptek->curSetting.wheel != AIPTEK_WHEEL_DISABLE) {
6334104d13fSDmitry Torokhov 						input_report_abs(inputdev,
6344104d13fSDmitry Torokhov 								 ABS_WHEEL,
6354104d13fSDmitry Torokhov 								 aiptek->curSetting.wheel);
6364104d13fSDmitry Torokhov 						aiptek->curSetting.wheel = AIPTEK_WHEEL_DISABLE;
6374104d13fSDmitry Torokhov 					}
6384104d13fSDmitry Torokhov 				}
639da9fda43SRene van Paassen 				input_report_abs(inputdev, ABS_MISC, p | AIPTEK_REPORT_TOOL_MOUSE);
6401e7b3faeSRene van Paassen 				if (aiptek->lastMacro != -1) {
6411e7b3faeSRene van Paassen 			                input_report_key(inputdev,
6421e7b3faeSRene van Paassen 							 macroKeyEvents[aiptek->lastMacro], 0);
6431e7b3faeSRene van Paassen 				        aiptek->lastMacro = -1;
6441e7b3faeSRene van Paassen 				}
6454104d13fSDmitry Torokhov 				input_sync(inputdev);
6464104d13fSDmitry Torokhov 			}
6474104d13fSDmitry Torokhov 		}
6484104d13fSDmitry Torokhov 	}
6494104d13fSDmitry Torokhov 	/* Report 4s come from the macro keys when pressed by stylus
6504104d13fSDmitry Torokhov 	 */
6514104d13fSDmitry Torokhov 	else if (data[0] == 4) {
6524104d13fSDmitry Torokhov 		jitterable = data[1] & 0x18;
6534104d13fSDmitry Torokhov 
654fe981f23SRene van Paassen 		dv = (data[1] & 0x01) != 0 ? 1 : 0;
655fe981f23SRene van Paassen 		p = (data[1] & 0x02) != 0 ? 1 : 0;
6564104d13fSDmitry Torokhov 		tip = (data[1] & 0x04) != 0 ? 1 : 0;
6574104d13fSDmitry Torokhov 		bs = (data[1] & aiptek->curSetting.stylusButtonLower) != 0 ? 1 : 0;
6584104d13fSDmitry Torokhov 		pck = (data[1] & aiptek->curSetting.stylusButtonUpper) != 0 ? 1 : 0;
6594104d13fSDmitry Torokhov 
6601e7b3faeSRene van Paassen 		macro = dv && p && tip && !(data[3] & 1) ? (data[3] >> 1) : -1;
661858ad08cSHarvey Harrison 		z = get_unaligned_le16(data + 4);
6624104d13fSDmitry Torokhov 
6631e7b3faeSRene van Paassen 		if (dv) {
664b3b6cf1dSRene van Paassen 		        /* If the selected tool changed, reset the old
665b3b6cf1dSRene van Paassen 			 * tool key, and set the new one.
6664104d13fSDmitry Torokhov 			 */
667b3b6cf1dSRene van Paassen 		        if (aiptek->previousToolMode !=
668b3b6cf1dSRene van Paassen 			    aiptek->curSetting.toolMode) {
6694104d13fSDmitry Torokhov 			        input_report_key(inputdev,
670b3b6cf1dSRene van Paassen 						 aiptek->previousToolMode, 0);
671b3b6cf1dSRene van Paassen 				input_report_key(inputdev,
672b3b6cf1dSRene van Paassen 						 aiptek->curSetting.toolMode,
6734104d13fSDmitry Torokhov 						 1);
674b3b6cf1dSRene van Paassen 				aiptek->previousToolMode =
675b3b6cf1dSRene van Paassen 				        aiptek->curSetting.toolMode;
6764104d13fSDmitry Torokhov 			}
6774104d13fSDmitry Torokhov 		}
6784104d13fSDmitry Torokhov 
6791e7b3faeSRene van Paassen 		if (aiptek->lastMacro != -1 && aiptek->lastMacro != macro) {
6801e7b3faeSRene van Paassen 		        input_report_key(inputdev, macroKeyEvents[aiptek->lastMacro], 0);
6811e7b3faeSRene van Paassen 			aiptek->lastMacro = -1;
6824104d13fSDmitry Torokhov 		}
6831e7b3faeSRene van Paassen 
6841e7b3faeSRene van Paassen 		if (macro != -1 && macro != aiptek->lastMacro) {
6851e7b3faeSRene van Paassen 			input_report_key(inputdev, macroKeyEvents[macro], 1);
6861e7b3faeSRene van Paassen 			aiptek->lastMacro = macro;
6874104d13fSDmitry Torokhov 		}
6884104d13fSDmitry Torokhov 		input_report_abs(inputdev, ABS_MISC,
6894104d13fSDmitry Torokhov 				 p | AIPTEK_REPORT_TOOL_STYLUS);
6904104d13fSDmitry Torokhov 		input_sync(inputdev);
6914104d13fSDmitry Torokhov 	}
6924104d13fSDmitry Torokhov 	/* Report 5s come from the macro keys when pressed by mouse
6934104d13fSDmitry Torokhov 	 */
6944104d13fSDmitry Torokhov 	else if (data[0] == 5) {
6954104d13fSDmitry Torokhov 		jitterable = data[1] & 0x1c;
6964104d13fSDmitry Torokhov 
697fe981f23SRene van Paassen 		dv = (data[1] & 0x01) != 0 ? 1 : 0;
698fe981f23SRene van Paassen 		p = (data[1] & 0x02) != 0 ? 1 : 0;
6994104d13fSDmitry Torokhov 		left = (data[1]& aiptek->curSetting.mouseButtonLeft) != 0 ? 1 : 0;
7004104d13fSDmitry Torokhov 		right = (data[1] & aiptek->curSetting.mouseButtonRight) != 0 ? 1 : 0;
7014104d13fSDmitry Torokhov 		middle = (data[1] & aiptek->curSetting.mouseButtonMiddle) != 0 ? 1 : 0;
7021e7b3faeSRene van Paassen 		macro = dv && p && left && !(data[3] & 1) ? (data[3] >> 1) : 0;
7034104d13fSDmitry Torokhov 
7041e7b3faeSRene van Paassen 		if (dv) {
705b3b6cf1dSRene van Paassen 		        /* If the selected tool changed, reset the old
706b3b6cf1dSRene van Paassen 			 * tool key, and set the new one.
7074104d13fSDmitry Torokhov 			 */
708b3b6cf1dSRene van Paassen 		        if (aiptek->previousToolMode !=
709b3b6cf1dSRene van Paassen 			    aiptek->curSetting.toolMode) {
7104104d13fSDmitry Torokhov 		                input_report_key(inputdev,
711b3b6cf1dSRene van Paassen 						 aiptek->previousToolMode, 0);
712b3b6cf1dSRene van Paassen 			        input_report_key(inputdev,
7131e7b3faeSRene van Paassen 						 aiptek->curSetting.toolMode, 1);
7141e7b3faeSRene van Paassen 			        aiptek->previousToolMode = aiptek->curSetting.toolMode;
7151e7b3faeSRene van Paassen 			}
7164104d13fSDmitry Torokhov 		}
7174104d13fSDmitry Torokhov 
7181e7b3faeSRene van Paassen 		if (aiptek->lastMacro != -1 && aiptek->lastMacro != macro) {
7191e7b3faeSRene van Paassen 		        input_report_key(inputdev, macroKeyEvents[aiptek->lastMacro], 0);
7201e7b3faeSRene van Paassen 			aiptek->lastMacro = -1;
7214104d13fSDmitry Torokhov 		}
7224104d13fSDmitry Torokhov 
7231e7b3faeSRene van Paassen 		if (macro != -1 && macro != aiptek->lastMacro) {
7244104d13fSDmitry Torokhov 			input_report_key(inputdev, macroKeyEvents[macro], 1);
7251e7b3faeSRene van Paassen 			aiptek->lastMacro = macro;
7261e7b3faeSRene van Paassen 		}
7271e7b3faeSRene van Paassen 
7281e7b3faeSRene van Paassen 		input_report_abs(inputdev, ABS_MISC,
7294104d13fSDmitry Torokhov 				 p | AIPTEK_REPORT_TOOL_MOUSE);
7304104d13fSDmitry Torokhov 		input_sync(inputdev);
7314104d13fSDmitry Torokhov 	}
7324104d13fSDmitry Torokhov 	/* We have no idea which tool can generate a report 6. Theoretically,
7334104d13fSDmitry Torokhov 	 * neither need to, having been given reports 4 & 5 for such use.
7344104d13fSDmitry Torokhov 	 * However, report 6 is the 'official-looking' report for macroKeys;
7354104d13fSDmitry Torokhov 	 * reports 4 & 5 supposively are used to support unnamed, unknown
7364104d13fSDmitry Torokhov 	 * hat switches (which just so happen to be the macroKeys.)
7374104d13fSDmitry Torokhov 	 */
7384104d13fSDmitry Torokhov 	else if (data[0] == 6) {
739858ad08cSHarvey Harrison 		macro = get_unaligned_le16(data + 1);
7404104d13fSDmitry Torokhov 		if (macro > 0) {
7414104d13fSDmitry Torokhov 			input_report_key(inputdev, macroKeyEvents[macro - 1],
7424104d13fSDmitry Torokhov 					 0);
7434104d13fSDmitry Torokhov 		}
7444104d13fSDmitry Torokhov 		if (macro < 25) {
7454104d13fSDmitry Torokhov 			input_report_key(inputdev, macroKeyEvents[macro + 1],
7464104d13fSDmitry Torokhov 					 0);
7474104d13fSDmitry Torokhov 		}
7484104d13fSDmitry Torokhov 
749b3b6cf1dSRene van Paassen 		/* If the selected tool changed, reset the old
750b3b6cf1dSRene van Paassen 		   tool key, and set the new one.
7514104d13fSDmitry Torokhov 		*/
752b3b6cf1dSRene van Paassen 		if (aiptek->previousToolMode !=
753b3b6cf1dSRene van Paassen 		    aiptek->curSetting.toolMode) {
7544104d13fSDmitry Torokhov 		        input_report_key(inputdev,
755b3b6cf1dSRene van Paassen 					 aiptek->previousToolMode, 0);
756b3b6cf1dSRene van Paassen 			input_report_key(inputdev,
757b3b6cf1dSRene van Paassen 					 aiptek->curSetting.toolMode,
758b3b6cf1dSRene van Paassen 					 1);
759b3b6cf1dSRene van Paassen 			aiptek->previousToolMode =
760b3b6cf1dSRene van Paassen 				aiptek->curSetting.toolMode;
7614104d13fSDmitry Torokhov 		}
7624104d13fSDmitry Torokhov 
7634104d13fSDmitry Torokhov 		input_report_key(inputdev, macroKeyEvents[macro], 1);
7644104d13fSDmitry Torokhov 		input_report_abs(inputdev, ABS_MISC,
7654104d13fSDmitry Torokhov 				 1 | AIPTEK_REPORT_TOOL_UNKNOWN);
7664104d13fSDmitry Torokhov 		input_sync(inputdev);
7674104d13fSDmitry Torokhov 	} else {
768871ba51cSGreg Kroah-Hartman 		dev_dbg(&intf->dev, "Unknown report %d\n", data[0]);
7694104d13fSDmitry Torokhov 	}
7704104d13fSDmitry Torokhov 
7714104d13fSDmitry Torokhov 	/* Jitter may occur when the user presses a button on the stlyus
7724104d13fSDmitry Torokhov 	 * or the mouse. What we do to prevent that is wait 'x' milliseconds
7734104d13fSDmitry Torokhov 	 * following a 'jitterable' event, which should give the hand some time
7744104d13fSDmitry Torokhov 	 * stabilize itself.
7754104d13fSDmitry Torokhov 	 *
7764104d13fSDmitry Torokhov 	 * We just introduced aiptek->previousJitterable to carry forth the
7774104d13fSDmitry Torokhov 	 * notion that jitter occurs when the button state changes from on to off:
7784104d13fSDmitry Torokhov 	 * a person drawing, holding a button down is not subject to jittering.
7794104d13fSDmitry Torokhov 	 * With that in mind, changing from upper button depressed to lower button
7804104d13fSDmitry Torokhov 	 * WILL transition through a jitter delay.
7814104d13fSDmitry Torokhov 	 */
7824104d13fSDmitry Torokhov 
7834104d13fSDmitry Torokhov 	if (aiptek->previousJitterable != jitterable &&
7844104d13fSDmitry Torokhov 	    aiptek->curSetting.jitterDelay != 0 && aiptek->inDelay != 1) {
7854104d13fSDmitry Torokhov 		aiptek->endDelay = jiffies +
7864104d13fSDmitry Torokhov 		    ((aiptek->curSetting.jitterDelay * HZ) / 1000);
7874104d13fSDmitry Torokhov 		aiptek->inDelay = 1;
7884104d13fSDmitry Torokhov 	}
7894104d13fSDmitry Torokhov 	aiptek->previousJitterable = jitterable;
7904104d13fSDmitry Torokhov 
7914104d13fSDmitry Torokhov exit:
7924104d13fSDmitry Torokhov 	retval = usb_submit_urb(urb, GFP_ATOMIC);
7934104d13fSDmitry Torokhov 	if (retval != 0) {
794871ba51cSGreg Kroah-Hartman 		dev_err(&intf->dev,
7951f80bb94SGreg Kroah-Hartman 			"%s - usb_submit_urb failed with result %d\n",
796ea3e6c59SHarvey Harrison 			__func__, retval);
7974104d13fSDmitry Torokhov 	}
7984104d13fSDmitry Torokhov }
7994104d13fSDmitry Torokhov 
8004104d13fSDmitry Torokhov /***********************************************************************
8014104d13fSDmitry Torokhov  * These are the USB id's known so far. We do not identify them to
8024104d13fSDmitry Torokhov  * specific Aiptek model numbers, because there has been overlaps,
8034104d13fSDmitry Torokhov  * use, and reuse of id's in existing models. Certain models have
8044104d13fSDmitry Torokhov  * been known to use more than one ID, indicative perhaps of
8054104d13fSDmitry Torokhov  * manufacturing revisions. In any event, we consider these
8064104d13fSDmitry Torokhov  * IDs to not be model-specific nor unique.
8074104d13fSDmitry Torokhov  */
8084104d13fSDmitry Torokhov static const struct usb_device_id aiptek_ids[] = {
8094104d13fSDmitry Torokhov 	{USB_DEVICE(USB_VENDOR_ID_AIPTEK, 0x01)},
8104104d13fSDmitry Torokhov 	{USB_DEVICE(USB_VENDOR_ID_AIPTEK, 0x10)},
8114104d13fSDmitry Torokhov 	{USB_DEVICE(USB_VENDOR_ID_AIPTEK, 0x20)},
8124104d13fSDmitry Torokhov 	{USB_DEVICE(USB_VENDOR_ID_AIPTEK, 0x21)},
8134104d13fSDmitry Torokhov 	{USB_DEVICE(USB_VENDOR_ID_AIPTEK, 0x22)},
8144104d13fSDmitry Torokhov 	{USB_DEVICE(USB_VENDOR_ID_AIPTEK, 0x23)},
8154104d13fSDmitry Torokhov 	{USB_DEVICE(USB_VENDOR_ID_AIPTEK, 0x24)},
816a32bcc45SGuryanov Dmitry 	{USB_DEVICE(USB_VENDOR_ID_KYE, 0x5003)},
8174104d13fSDmitry Torokhov 	{}
8184104d13fSDmitry Torokhov };
8194104d13fSDmitry Torokhov 
8204104d13fSDmitry Torokhov MODULE_DEVICE_TABLE(usb, aiptek_ids);
8214104d13fSDmitry Torokhov 
8224104d13fSDmitry Torokhov /***********************************************************************
8234104d13fSDmitry Torokhov  * Open an instance of the tablet driver.
8244104d13fSDmitry Torokhov  */
aiptek_open(struct input_dev * inputdev)8254104d13fSDmitry Torokhov static int aiptek_open(struct input_dev *inputdev)
8264104d13fSDmitry Torokhov {
8274104d13fSDmitry Torokhov 	struct aiptek *aiptek = input_get_drvdata(inputdev);
8284104d13fSDmitry Torokhov 
829c6309018SOliver Neukum 	aiptek->urb->dev = interface_to_usbdev(aiptek->intf);
8304104d13fSDmitry Torokhov 	if (usb_submit_urb(aiptek->urb, GFP_KERNEL) != 0)
8314104d13fSDmitry Torokhov 		return -EIO;
8324104d13fSDmitry Torokhov 
8334104d13fSDmitry Torokhov 	return 0;
8344104d13fSDmitry Torokhov }
8354104d13fSDmitry Torokhov 
8364104d13fSDmitry Torokhov /***********************************************************************
8374104d13fSDmitry Torokhov  * Close an instance of the tablet driver.
8384104d13fSDmitry Torokhov  */
aiptek_close(struct input_dev * inputdev)8394104d13fSDmitry Torokhov static void aiptek_close(struct input_dev *inputdev)
8404104d13fSDmitry Torokhov {
8414104d13fSDmitry Torokhov 	struct aiptek *aiptek = input_get_drvdata(inputdev);
8424104d13fSDmitry Torokhov 
8434104d13fSDmitry Torokhov 	usb_kill_urb(aiptek->urb);
8444104d13fSDmitry Torokhov }
8454104d13fSDmitry Torokhov 
8464104d13fSDmitry Torokhov /***********************************************************************
8474104d13fSDmitry Torokhov  * aiptek_set_report and aiptek_get_report() are borrowed from Linux 2.4.x,
8484104d13fSDmitry Torokhov  * where they were known as usb_set_report and usb_get_report.
8494104d13fSDmitry Torokhov  */
8504104d13fSDmitry Torokhov static int
aiptek_set_report(struct aiptek * aiptek,unsigned char report_type,unsigned char report_id,void * buffer,int size)8514104d13fSDmitry Torokhov aiptek_set_report(struct aiptek *aiptek,
8524104d13fSDmitry Torokhov 		  unsigned char report_type,
8534104d13fSDmitry Torokhov 		  unsigned char report_id, void *buffer, int size)
8544104d13fSDmitry Torokhov {
855c6309018SOliver Neukum 	struct usb_device *udev = interface_to_usbdev(aiptek->intf);
856c6309018SOliver Neukum 
857c6309018SOliver Neukum 	return usb_control_msg(udev,
858c6309018SOliver Neukum 			       usb_sndctrlpipe(udev, 0),
8594104d13fSDmitry Torokhov 			       USB_REQ_SET_REPORT,
8604104d13fSDmitry Torokhov 			       USB_TYPE_CLASS | USB_RECIP_INTERFACE |
8614104d13fSDmitry Torokhov 			       USB_DIR_OUT, (report_type << 8) + report_id,
8624104d13fSDmitry Torokhov 			       aiptek->ifnum, buffer, size, 5000);
8634104d13fSDmitry Torokhov }
8644104d13fSDmitry Torokhov 
8654104d13fSDmitry Torokhov static int
aiptek_get_report(struct aiptek * aiptek,unsigned char report_type,unsigned char report_id,void * buffer,int size)8664104d13fSDmitry Torokhov aiptek_get_report(struct aiptek *aiptek,
8674104d13fSDmitry Torokhov 		  unsigned char report_type,
8684104d13fSDmitry Torokhov 		  unsigned char report_id, void *buffer, int size)
8694104d13fSDmitry Torokhov {
870c6309018SOliver Neukum 	struct usb_device *udev = interface_to_usbdev(aiptek->intf);
871c6309018SOliver Neukum 
872c6309018SOliver Neukum 	return usb_control_msg(udev,
873c6309018SOliver Neukum 			       usb_rcvctrlpipe(udev, 0),
8744104d13fSDmitry Torokhov 			       USB_REQ_GET_REPORT,
8754104d13fSDmitry Torokhov 			       USB_TYPE_CLASS | USB_RECIP_INTERFACE |
8764104d13fSDmitry Torokhov 			       USB_DIR_IN, (report_type << 8) + report_id,
8774104d13fSDmitry Torokhov 			       aiptek->ifnum, buffer, size, 5000);
8784104d13fSDmitry Torokhov }
8794104d13fSDmitry Torokhov 
8804104d13fSDmitry Torokhov /***********************************************************************
8814104d13fSDmitry Torokhov  * Send a command to the tablet.
8824104d13fSDmitry Torokhov  */
8834104d13fSDmitry Torokhov static int
aiptek_command(struct aiptek * aiptek,unsigned char command,unsigned char data)8844104d13fSDmitry Torokhov aiptek_command(struct aiptek *aiptek, unsigned char command, unsigned char data)
8854104d13fSDmitry Torokhov {
8864104d13fSDmitry Torokhov 	const int sizeof_buf = 3 * sizeof(u8);
8874104d13fSDmitry Torokhov 	int ret;
8884104d13fSDmitry Torokhov 	u8 *buf;
8894104d13fSDmitry Torokhov 
8904104d13fSDmitry Torokhov 	buf = kmalloc(sizeof_buf, GFP_KERNEL);
8914104d13fSDmitry Torokhov 	if (!buf)
8924104d13fSDmitry Torokhov 		return -ENOMEM;
8934104d13fSDmitry Torokhov 
8944104d13fSDmitry Torokhov 	buf[0] = 2;
8954104d13fSDmitry Torokhov 	buf[1] = command;
8964104d13fSDmitry Torokhov 	buf[2] = data;
8974104d13fSDmitry Torokhov 
8984104d13fSDmitry Torokhov 	if ((ret =
8994104d13fSDmitry Torokhov 	     aiptek_set_report(aiptek, 3, 2, buf, sizeof_buf)) != sizeof_buf) {
900871ba51cSGreg Kroah-Hartman 		dev_dbg(&aiptek->intf->dev,
9018fb6321bSGreg Kroah-Hartman 			"aiptek_program: failed, tried to send: 0x%02x 0x%02x\n",
9024104d13fSDmitry Torokhov 			command, data);
9034104d13fSDmitry Torokhov 	}
9044104d13fSDmitry Torokhov 	kfree(buf);
9054104d13fSDmitry Torokhov 	return ret < 0 ? ret : 0;
9064104d13fSDmitry Torokhov }
9074104d13fSDmitry Torokhov 
9084104d13fSDmitry Torokhov /***********************************************************************
9094104d13fSDmitry Torokhov  * Retrieve information from the tablet. Querying info is defined as first
9104104d13fSDmitry Torokhov  * sending the {command,data} sequence as a command, followed by a wait
9114104d13fSDmitry Torokhov  * (aka, "programmaticDelay") and then a "read" request.
9124104d13fSDmitry Torokhov  */
9134104d13fSDmitry Torokhov static int
aiptek_query(struct aiptek * aiptek,unsigned char command,unsigned char data)9144104d13fSDmitry Torokhov aiptek_query(struct aiptek *aiptek, unsigned char command, unsigned char data)
9154104d13fSDmitry Torokhov {
9164104d13fSDmitry Torokhov 	const int sizeof_buf = 3 * sizeof(u8);
9174104d13fSDmitry Torokhov 	int ret;
9184104d13fSDmitry Torokhov 	u8 *buf;
9194104d13fSDmitry Torokhov 
9204104d13fSDmitry Torokhov 	buf = kmalloc(sizeof_buf, GFP_KERNEL);
9214104d13fSDmitry Torokhov 	if (!buf)
9224104d13fSDmitry Torokhov 		return -ENOMEM;
9234104d13fSDmitry Torokhov 
9244104d13fSDmitry Torokhov 	buf[0] = 2;
9254104d13fSDmitry Torokhov 	buf[1] = command;
9264104d13fSDmitry Torokhov 	buf[2] = data;
9274104d13fSDmitry Torokhov 
9284104d13fSDmitry Torokhov 	if (aiptek_command(aiptek, command, data) != 0) {
9294104d13fSDmitry Torokhov 		kfree(buf);
9304104d13fSDmitry Torokhov 		return -EIO;
9314104d13fSDmitry Torokhov 	}
9324104d13fSDmitry Torokhov 	msleep(aiptek->curSetting.programmableDelay);
9334104d13fSDmitry Torokhov 
934c8eefa0fSColin Ian King 	if (aiptek_get_report(aiptek, 3, 2, buf, sizeof_buf) != sizeof_buf) {
935871ba51cSGreg Kroah-Hartman 		dev_dbg(&aiptek->intf->dev,
9368fb6321bSGreg Kroah-Hartman 			"aiptek_query failed: returned 0x%02x 0x%02x 0x%02x\n",
9374104d13fSDmitry Torokhov 			buf[0], buf[1], buf[2]);
9384104d13fSDmitry Torokhov 		ret = -EIO;
9394104d13fSDmitry Torokhov 	} else {
940858ad08cSHarvey Harrison 		ret = get_unaligned_le16(buf + 1);
9414104d13fSDmitry Torokhov 	}
9424104d13fSDmitry Torokhov 	kfree(buf);
9434104d13fSDmitry Torokhov 	return ret;
9444104d13fSDmitry Torokhov }
9454104d13fSDmitry Torokhov 
9464104d13fSDmitry Torokhov /***********************************************************************
9474104d13fSDmitry Torokhov  * Program the tablet into either absolute or relative mode.
9484104d13fSDmitry Torokhov  * We also get information about the tablet's size.
9494104d13fSDmitry Torokhov  */
aiptek_program_tablet(struct aiptek * aiptek)9504104d13fSDmitry Torokhov static int aiptek_program_tablet(struct aiptek *aiptek)
9514104d13fSDmitry Torokhov {
9524104d13fSDmitry Torokhov 	int ret;
9534104d13fSDmitry Torokhov 	/* Execute Resolution500LPI */
9544104d13fSDmitry Torokhov 	if ((ret = aiptek_command(aiptek, 0x18, 0x04)) < 0)
9554104d13fSDmitry Torokhov 		return ret;
9564104d13fSDmitry Torokhov 
9574104d13fSDmitry Torokhov 	/* Query getModelCode */
9584104d13fSDmitry Torokhov 	if ((ret = aiptek_query(aiptek, 0x02, 0x00)) < 0)
9594104d13fSDmitry Torokhov 		return ret;
9604104d13fSDmitry Torokhov 	aiptek->features.modelCode = ret & 0xff;
9614104d13fSDmitry Torokhov 
9624104d13fSDmitry Torokhov 	/* Query getODMCode */
9634104d13fSDmitry Torokhov 	if ((ret = aiptek_query(aiptek, 0x03, 0x00)) < 0)
9644104d13fSDmitry Torokhov 		return ret;
9654104d13fSDmitry Torokhov 	aiptek->features.odmCode = ret;
9664104d13fSDmitry Torokhov 
9674104d13fSDmitry Torokhov 	/* Query getFirmwareCode */
9684104d13fSDmitry Torokhov 	if ((ret = aiptek_query(aiptek, 0x04, 0x00)) < 0)
9694104d13fSDmitry Torokhov 		return ret;
9704104d13fSDmitry Torokhov 	aiptek->features.firmwareCode = ret;
9714104d13fSDmitry Torokhov 
9724104d13fSDmitry Torokhov 	/* Query getXextension */
9734104d13fSDmitry Torokhov 	if ((ret = aiptek_query(aiptek, 0x01, 0x00)) < 0)
9744104d13fSDmitry Torokhov 		return ret;
975987a6c02SDaniel Mack 	input_set_abs_params(aiptek->inputdev, ABS_X, 0, ret - 1, 0, 0);
9764104d13fSDmitry Torokhov 
9774104d13fSDmitry Torokhov 	/* Query getYextension */
9784104d13fSDmitry Torokhov 	if ((ret = aiptek_query(aiptek, 0x01, 0x01)) < 0)
9794104d13fSDmitry Torokhov 		return ret;
980987a6c02SDaniel Mack 	input_set_abs_params(aiptek->inputdev, ABS_Y, 0, ret - 1, 0, 0);
9814104d13fSDmitry Torokhov 
9824104d13fSDmitry Torokhov 	/* Query getPressureLevels */
9834104d13fSDmitry Torokhov 	if ((ret = aiptek_query(aiptek, 0x08, 0x00)) < 0)
9844104d13fSDmitry Torokhov 		return ret;
985987a6c02SDaniel Mack 	input_set_abs_params(aiptek->inputdev, ABS_PRESSURE, 0, ret - 1, 0, 0);
9864104d13fSDmitry Torokhov 
9874104d13fSDmitry Torokhov 	/* Depending on whether we are in absolute or relative mode, we will
9884104d13fSDmitry Torokhov 	 * do a switchToTablet(absolute) or switchToMouse(relative) command.
9894104d13fSDmitry Torokhov 	 */
9904104d13fSDmitry Torokhov 	if (aiptek->curSetting.coordinateMode ==
9914104d13fSDmitry Torokhov 	    AIPTEK_COORDINATE_ABSOLUTE_MODE) {
9924104d13fSDmitry Torokhov 		/* Execute switchToTablet */
9934104d13fSDmitry Torokhov 		if ((ret = aiptek_command(aiptek, 0x10, 0x01)) < 0) {
9944104d13fSDmitry Torokhov 			return ret;
9954104d13fSDmitry Torokhov 		}
9964104d13fSDmitry Torokhov 	} else {
9974104d13fSDmitry Torokhov 		/* Execute switchToMouse */
9984104d13fSDmitry Torokhov 		if ((ret = aiptek_command(aiptek, 0x10, 0x00)) < 0) {
9994104d13fSDmitry Torokhov 			return ret;
10004104d13fSDmitry Torokhov 		}
10014104d13fSDmitry Torokhov 	}
10024104d13fSDmitry Torokhov 
10034104d13fSDmitry Torokhov 	/* Enable the macro keys */
10044104d13fSDmitry Torokhov 	if ((ret = aiptek_command(aiptek, 0x11, 0x02)) < 0)
10054104d13fSDmitry Torokhov 		return ret;
10064104d13fSDmitry Torokhov #if 0
10074104d13fSDmitry Torokhov 	/* Execute FilterOn */
10084104d13fSDmitry Torokhov 	if ((ret = aiptek_command(aiptek, 0x17, 0x00)) < 0)
10094104d13fSDmitry Torokhov 		return ret;
10104104d13fSDmitry Torokhov #endif
10114104d13fSDmitry Torokhov 
10124104d13fSDmitry Torokhov 	/* Execute AutoGainOn */
10134104d13fSDmitry Torokhov 	if ((ret = aiptek_command(aiptek, 0x12, 0xff)) < 0)
10144104d13fSDmitry Torokhov 		return ret;
10154104d13fSDmitry Torokhov 
10164104d13fSDmitry Torokhov 	/* Reset the eventCount, so we track events from last (re)programming
10174104d13fSDmitry Torokhov 	 */
10184104d13fSDmitry Torokhov 	aiptek->diagnostic = AIPTEK_DIAGNOSTIC_NA;
10194104d13fSDmitry Torokhov 	aiptek->eventCount = 0;
10204104d13fSDmitry Torokhov 
10214104d13fSDmitry Torokhov 	return 0;
10224104d13fSDmitry Torokhov }
10234104d13fSDmitry Torokhov 
10244104d13fSDmitry Torokhov /***********************************************************************
10254104d13fSDmitry Torokhov  * Sysfs functions. Sysfs prefers that individually-tunable parameters
10264104d13fSDmitry Torokhov  * exist in their separate pseudo-files. Summary data that is immutable
10274104d13fSDmitry Torokhov  * may exist in a singular file so long as you don't define a writeable
10284104d13fSDmitry Torokhov  * interface.
10294104d13fSDmitry Torokhov  */
10304104d13fSDmitry Torokhov 
10314104d13fSDmitry Torokhov /***********************************************************************
10324104d13fSDmitry Torokhov  * support the 'size' file -- display support
10334104d13fSDmitry Torokhov  */
show_tabletSize(struct device * dev,struct device_attribute * attr,char * buf)10344104d13fSDmitry Torokhov static ssize_t show_tabletSize(struct device *dev, struct device_attribute *attr, char *buf)
10354104d13fSDmitry Torokhov {
10364104d13fSDmitry Torokhov 	struct aiptek *aiptek = dev_get_drvdata(dev);
10374104d13fSDmitry Torokhov 
10384c47097fSJiapeng Chong 	return sysfs_emit(buf, "%dx%d\n",
1039987a6c02SDaniel Mack 			  input_abs_get_max(aiptek->inputdev, ABS_X) + 1,
1040987a6c02SDaniel Mack 			  input_abs_get_max(aiptek->inputdev, ABS_Y) + 1);
10414104d13fSDmitry Torokhov }
10424104d13fSDmitry Torokhov 
10434104d13fSDmitry Torokhov /* These structs define the sysfs files, param #1 is the name of the
10444104d13fSDmitry Torokhov  * file, param 2 is the file permissions, param 3 & 4 are to the
10454104d13fSDmitry Torokhov  * output generator and input parser routines. Absence of a routine is
10464104d13fSDmitry Torokhov  * permitted -- it only means can't either 'cat' the file, or send data
10474104d13fSDmitry Torokhov  * to it.
10484104d13fSDmitry Torokhov  */
10494104d13fSDmitry Torokhov static DEVICE_ATTR(size, S_IRUGO, show_tabletSize, NULL);
10504104d13fSDmitry Torokhov 
10514104d13fSDmitry Torokhov /***********************************************************************
10524104d13fSDmitry Torokhov  * support routines for the 'pointer_mode' file. Note that this file
10534104d13fSDmitry Torokhov  * both displays current setting and allows reprogramming.
10544104d13fSDmitry Torokhov  */
1055cd438a58SDmitry Torokhov static struct aiptek_map pointer_mode_map[] = {
1056cd438a58SDmitry Torokhov 	{ "stylus",	AIPTEK_POINTER_ONLY_STYLUS_MODE },
1057cd438a58SDmitry Torokhov 	{ "mouse",	AIPTEK_POINTER_ONLY_MOUSE_MODE },
1058cd438a58SDmitry Torokhov 	{ "either",	AIPTEK_POINTER_EITHER_MODE },
1059cd438a58SDmitry Torokhov 	{ NULL,		AIPTEK_INVALID_VALUE }
1060cd438a58SDmitry Torokhov };
1061cd438a58SDmitry Torokhov 
show_tabletPointerMode(struct device * dev,struct device_attribute * attr,char * buf)10624104d13fSDmitry Torokhov static ssize_t show_tabletPointerMode(struct device *dev, struct device_attribute *attr, char *buf)
10634104d13fSDmitry Torokhov {
10644104d13fSDmitry Torokhov 	struct aiptek *aiptek = dev_get_drvdata(dev);
10654104d13fSDmitry Torokhov 
10664c47097fSJiapeng Chong 	return sysfs_emit(buf, "%s\n", map_val_to_str(pointer_mode_map,
1067cd438a58SDmitry Torokhov 						      aiptek->curSetting.pointerMode));
10684104d13fSDmitry Torokhov }
10694104d13fSDmitry Torokhov 
10704104d13fSDmitry Torokhov static ssize_t
store_tabletPointerMode(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)10714104d13fSDmitry Torokhov store_tabletPointerMode(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
10724104d13fSDmitry Torokhov {
10734104d13fSDmitry Torokhov 	struct aiptek *aiptek = dev_get_drvdata(dev);
1074cd438a58SDmitry Torokhov 	int new_mode = map_str_to_val(pointer_mode_map, buf, count);
10754104d13fSDmitry Torokhov 
1076cd438a58SDmitry Torokhov 	if (new_mode == AIPTEK_INVALID_VALUE)
1077cd438a58SDmitry Torokhov 		return -EINVAL;
1078cd438a58SDmitry Torokhov 
1079cd438a58SDmitry Torokhov 	aiptek->newSetting.pointerMode = new_mode;
10804104d13fSDmitry Torokhov 	return count;
10814104d13fSDmitry Torokhov }
10824104d13fSDmitry Torokhov 
10834104d13fSDmitry Torokhov static DEVICE_ATTR(pointer_mode,
10846f07d31eSDmitry Torokhov 		   S_IRUGO | S_IWUSR,
10854104d13fSDmitry Torokhov 		   show_tabletPointerMode, store_tabletPointerMode);
10864104d13fSDmitry Torokhov 
10874104d13fSDmitry Torokhov /***********************************************************************
10884104d13fSDmitry Torokhov  * support routines for the 'coordinate_mode' file. Note that this file
10894104d13fSDmitry Torokhov  * both displays current setting and allows reprogramming.
10904104d13fSDmitry Torokhov  */
1091cd438a58SDmitry Torokhov 
1092cd438a58SDmitry Torokhov static struct aiptek_map coordinate_mode_map[] = {
1093cd438a58SDmitry Torokhov 	{ "absolute",	AIPTEK_COORDINATE_ABSOLUTE_MODE },
1094cd438a58SDmitry Torokhov 	{ "relative",	AIPTEK_COORDINATE_RELATIVE_MODE },
1095cd438a58SDmitry Torokhov 	{ NULL,		AIPTEK_INVALID_VALUE }
1096cd438a58SDmitry Torokhov };
1097cd438a58SDmitry Torokhov 
show_tabletCoordinateMode(struct device * dev,struct device_attribute * attr,char * buf)10984104d13fSDmitry Torokhov static ssize_t show_tabletCoordinateMode(struct device *dev, struct device_attribute *attr, char *buf)
10994104d13fSDmitry Torokhov {
11004104d13fSDmitry Torokhov 	struct aiptek *aiptek = dev_get_drvdata(dev);
11014104d13fSDmitry Torokhov 
11024c47097fSJiapeng Chong 	return sysfs_emit(buf, "%s\n", map_val_to_str(coordinate_mode_map,
1103cd438a58SDmitry Torokhov 						      aiptek->curSetting.coordinateMode));
11044104d13fSDmitry Torokhov }
11054104d13fSDmitry Torokhov 
11064104d13fSDmitry Torokhov static ssize_t
store_tabletCoordinateMode(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)11074104d13fSDmitry Torokhov store_tabletCoordinateMode(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
11084104d13fSDmitry Torokhov {
11094104d13fSDmitry Torokhov 	struct aiptek *aiptek = dev_get_drvdata(dev);
1110cd438a58SDmitry Torokhov 	int new_mode = map_str_to_val(coordinate_mode_map, buf, count);
11114104d13fSDmitry Torokhov 
1112cd438a58SDmitry Torokhov 	if (new_mode == AIPTEK_INVALID_VALUE)
1113cd438a58SDmitry Torokhov 		return -EINVAL;
1114cd438a58SDmitry Torokhov 
1115cd438a58SDmitry Torokhov 	aiptek->newSetting.coordinateMode = new_mode;
11164104d13fSDmitry Torokhov 	return count;
11174104d13fSDmitry Torokhov }
11184104d13fSDmitry Torokhov 
11194104d13fSDmitry Torokhov static DEVICE_ATTR(coordinate_mode,
11206f07d31eSDmitry Torokhov 		   S_IRUGO | S_IWUSR,
11214104d13fSDmitry Torokhov 		   show_tabletCoordinateMode, store_tabletCoordinateMode);
11224104d13fSDmitry Torokhov 
11234104d13fSDmitry Torokhov /***********************************************************************
11244104d13fSDmitry Torokhov  * support routines for the 'tool_mode' file. Note that this file
11254104d13fSDmitry Torokhov  * both displays current setting and allows reprogramming.
11264104d13fSDmitry Torokhov  */
1127cd438a58SDmitry Torokhov 
1128cd438a58SDmitry Torokhov static struct aiptek_map tool_mode_map[] = {
1129cd438a58SDmitry Torokhov 	{ "mouse",	AIPTEK_TOOL_BUTTON_MOUSE_MODE },
1130cd438a58SDmitry Torokhov 	{ "eraser",	AIPTEK_TOOL_BUTTON_ERASER_MODE },
1131cd438a58SDmitry Torokhov 	{ "pencil",	AIPTEK_TOOL_BUTTON_PENCIL_MODE },
1132cd438a58SDmitry Torokhov 	{ "pen",	AIPTEK_TOOL_BUTTON_PEN_MODE },
1133cd438a58SDmitry Torokhov 	{ "brush",	AIPTEK_TOOL_BUTTON_BRUSH_MODE },
1134cd438a58SDmitry Torokhov 	{ "airbrush",	AIPTEK_TOOL_BUTTON_AIRBRUSH_MODE },
1135cd438a58SDmitry Torokhov 	{ "lens",	AIPTEK_TOOL_BUTTON_LENS_MODE },
1136cd438a58SDmitry Torokhov 	{ NULL,		AIPTEK_INVALID_VALUE }
1137cd438a58SDmitry Torokhov };
1138cd438a58SDmitry Torokhov 
show_tabletToolMode(struct device * dev,struct device_attribute * attr,char * buf)11394104d13fSDmitry Torokhov static ssize_t show_tabletToolMode(struct device *dev, struct device_attribute *attr, char *buf)
11404104d13fSDmitry Torokhov {
11414104d13fSDmitry Torokhov 	struct aiptek *aiptek = dev_get_drvdata(dev);
11424104d13fSDmitry Torokhov 
11434c47097fSJiapeng Chong 	return sysfs_emit(buf, "%s\n", map_val_to_str(tool_mode_map,
1144cd438a58SDmitry Torokhov 						      aiptek->curSetting.toolMode));
11454104d13fSDmitry Torokhov }
11464104d13fSDmitry Torokhov 
11474104d13fSDmitry Torokhov static ssize_t
store_tabletToolMode(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)11484104d13fSDmitry Torokhov store_tabletToolMode(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
11494104d13fSDmitry Torokhov {
11504104d13fSDmitry Torokhov 	struct aiptek *aiptek = dev_get_drvdata(dev);
1151cd438a58SDmitry Torokhov 	int new_mode = map_str_to_val(tool_mode_map, buf, count);
11524104d13fSDmitry Torokhov 
1153cd438a58SDmitry Torokhov 	if (new_mode == AIPTEK_INVALID_VALUE)
1154cd438a58SDmitry Torokhov 		return -EINVAL;
11554104d13fSDmitry Torokhov 
1156cd438a58SDmitry Torokhov 	aiptek->newSetting.toolMode = new_mode;
11574104d13fSDmitry Torokhov 	return count;
11584104d13fSDmitry Torokhov }
11594104d13fSDmitry Torokhov 
11604104d13fSDmitry Torokhov static DEVICE_ATTR(tool_mode,
11616f07d31eSDmitry Torokhov 		   S_IRUGO | S_IWUSR,
11624104d13fSDmitry Torokhov 		   show_tabletToolMode, store_tabletToolMode);
11634104d13fSDmitry Torokhov 
11644104d13fSDmitry Torokhov /***********************************************************************
11654104d13fSDmitry Torokhov  * support routines for the 'xtilt' file. Note that this file
11664104d13fSDmitry Torokhov  * both displays current setting and allows reprogramming.
11674104d13fSDmitry Torokhov  */
show_tabletXtilt(struct device * dev,struct device_attribute * attr,char * buf)11684104d13fSDmitry Torokhov static ssize_t show_tabletXtilt(struct device *dev, struct device_attribute *attr, char *buf)
11694104d13fSDmitry Torokhov {
11704104d13fSDmitry Torokhov 	struct aiptek *aiptek = dev_get_drvdata(dev);
11714104d13fSDmitry Torokhov 
11724104d13fSDmitry Torokhov 	if (aiptek->curSetting.xTilt == AIPTEK_TILT_DISABLE) {
11734c47097fSJiapeng Chong 		return sysfs_emit(buf, "disable\n");
11744104d13fSDmitry Torokhov 	} else {
11754c47097fSJiapeng Chong 		return sysfs_emit(buf, "%d\n", aiptek->curSetting.xTilt);
11764104d13fSDmitry Torokhov 	}
11774104d13fSDmitry Torokhov }
11784104d13fSDmitry Torokhov 
11794104d13fSDmitry Torokhov static ssize_t
store_tabletXtilt(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)11804104d13fSDmitry Torokhov store_tabletXtilt(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
11814104d13fSDmitry Torokhov {
11824104d13fSDmitry Torokhov 	struct aiptek *aiptek = dev_get_drvdata(dev);
118376496e7aSJJ Ding 	int x;
11844104d13fSDmitry Torokhov 
118576496e7aSJJ Ding 	if (kstrtoint(buf, 10, &x)) {
1186160f1fefSJoe Rouvier 		size_t len = buf[count - 1] == '\n' ? count - 1 : count;
1187160f1fefSJoe Rouvier 
1188160f1fefSJoe Rouvier 		if (strncmp(buf, "disable", len))
1189160f1fefSJoe Rouvier 			return -EINVAL;
1190160f1fefSJoe Rouvier 
11914104d13fSDmitry Torokhov 		aiptek->newSetting.xTilt = AIPTEK_TILT_DISABLE;
11924104d13fSDmitry Torokhov 	} else {
1193160f1fefSJoe Rouvier 		if (x < AIPTEK_TILT_MIN || x > AIPTEK_TILT_MAX)
1194160f1fefSJoe Rouvier 			return -EINVAL;
1195160f1fefSJoe Rouvier 
11964104d13fSDmitry Torokhov 		aiptek->newSetting.xTilt = x;
11974104d13fSDmitry Torokhov 	}
1198160f1fefSJoe Rouvier 
11994104d13fSDmitry Torokhov 	return count;
12004104d13fSDmitry Torokhov }
12014104d13fSDmitry Torokhov 
12024104d13fSDmitry Torokhov static DEVICE_ATTR(xtilt,
12036f07d31eSDmitry Torokhov 		   S_IRUGO | S_IWUSR, show_tabletXtilt, store_tabletXtilt);
12044104d13fSDmitry Torokhov 
12054104d13fSDmitry Torokhov /***********************************************************************
12064104d13fSDmitry Torokhov  * support routines for the 'ytilt' file. Note that this file
12074104d13fSDmitry Torokhov  * both displays current setting and allows reprogramming.
12084104d13fSDmitry Torokhov  */
show_tabletYtilt(struct device * dev,struct device_attribute * attr,char * buf)12094104d13fSDmitry Torokhov static ssize_t show_tabletYtilt(struct device *dev, struct device_attribute *attr, char *buf)
12104104d13fSDmitry Torokhov {
12114104d13fSDmitry Torokhov 	struct aiptek *aiptek = dev_get_drvdata(dev);
12124104d13fSDmitry Torokhov 
12134104d13fSDmitry Torokhov 	if (aiptek->curSetting.yTilt == AIPTEK_TILT_DISABLE) {
12144c47097fSJiapeng Chong 		return sysfs_emit(buf, "disable\n");
12154104d13fSDmitry Torokhov 	} else {
12164c47097fSJiapeng Chong 		return sysfs_emit(buf, "%d\n", aiptek->curSetting.yTilt);
12174104d13fSDmitry Torokhov 	}
12184104d13fSDmitry Torokhov }
12194104d13fSDmitry Torokhov 
12204104d13fSDmitry Torokhov static ssize_t
store_tabletYtilt(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)12214104d13fSDmitry Torokhov store_tabletYtilt(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
12224104d13fSDmitry Torokhov {
12234104d13fSDmitry Torokhov 	struct aiptek *aiptek = dev_get_drvdata(dev);
122476496e7aSJJ Ding 	int y;
12254104d13fSDmitry Torokhov 
122676496e7aSJJ Ding 	if (kstrtoint(buf, 10, &y)) {
1227160f1fefSJoe Rouvier 		size_t len = buf[count - 1] == '\n' ? count - 1 : count;
1228160f1fefSJoe Rouvier 
1229160f1fefSJoe Rouvier 		if (strncmp(buf, "disable", len))
1230160f1fefSJoe Rouvier 			return -EINVAL;
1231160f1fefSJoe Rouvier 
12324104d13fSDmitry Torokhov 		aiptek->newSetting.yTilt = AIPTEK_TILT_DISABLE;
12334104d13fSDmitry Torokhov 	} else {
1234160f1fefSJoe Rouvier 		if (y < AIPTEK_TILT_MIN || y > AIPTEK_TILT_MAX)
1235160f1fefSJoe Rouvier 			return -EINVAL;
1236160f1fefSJoe Rouvier 
12374104d13fSDmitry Torokhov 		aiptek->newSetting.yTilt = y;
12384104d13fSDmitry Torokhov 	}
1239160f1fefSJoe Rouvier 
12404104d13fSDmitry Torokhov 	return count;
12414104d13fSDmitry Torokhov }
12424104d13fSDmitry Torokhov 
12434104d13fSDmitry Torokhov static DEVICE_ATTR(ytilt,
12446f07d31eSDmitry Torokhov 		   S_IRUGO | S_IWUSR, show_tabletYtilt, store_tabletYtilt);
12454104d13fSDmitry Torokhov 
12464104d13fSDmitry Torokhov /***********************************************************************
12474104d13fSDmitry Torokhov  * support routines for the 'jitter' file. Note that this file
12484104d13fSDmitry Torokhov  * both displays current setting and allows reprogramming.
12494104d13fSDmitry Torokhov  */
show_tabletJitterDelay(struct device * dev,struct device_attribute * attr,char * buf)12504104d13fSDmitry Torokhov static ssize_t show_tabletJitterDelay(struct device *dev, struct device_attribute *attr, char *buf)
12514104d13fSDmitry Torokhov {
12524104d13fSDmitry Torokhov 	struct aiptek *aiptek = dev_get_drvdata(dev);
12534104d13fSDmitry Torokhov 
12544c47097fSJiapeng Chong 	return sysfs_emit(buf, "%d\n", aiptek->curSetting.jitterDelay);
12554104d13fSDmitry Torokhov }
12564104d13fSDmitry Torokhov 
12574104d13fSDmitry Torokhov static ssize_t
store_tabletJitterDelay(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)12584104d13fSDmitry Torokhov store_tabletJitterDelay(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
12594104d13fSDmitry Torokhov {
12604104d13fSDmitry Torokhov 	struct aiptek *aiptek = dev_get_drvdata(dev);
126176496e7aSJJ Ding 	int err, j;
12624104d13fSDmitry Torokhov 
126376496e7aSJJ Ding 	err = kstrtoint(buf, 10, &j);
126476496e7aSJJ Ding 	if (err)
126576496e7aSJJ Ding 		return err;
1266160f1fefSJoe Rouvier 
126776496e7aSJJ Ding 	aiptek->newSetting.jitterDelay = j;
12684104d13fSDmitry Torokhov 	return count;
12694104d13fSDmitry Torokhov }
12704104d13fSDmitry Torokhov 
12714104d13fSDmitry Torokhov static DEVICE_ATTR(jitter,
12726f07d31eSDmitry Torokhov 		   S_IRUGO | S_IWUSR,
12734104d13fSDmitry Torokhov 		   show_tabletJitterDelay, store_tabletJitterDelay);
12744104d13fSDmitry Torokhov 
12754104d13fSDmitry Torokhov /***********************************************************************
12764104d13fSDmitry Torokhov  * support routines for the 'delay' file. Note that this file
12774104d13fSDmitry Torokhov  * both displays current setting and allows reprogramming.
12784104d13fSDmitry Torokhov  */
show_tabletProgrammableDelay(struct device * dev,struct device_attribute * attr,char * buf)12794104d13fSDmitry Torokhov static ssize_t show_tabletProgrammableDelay(struct device *dev, struct device_attribute *attr, char *buf)
12804104d13fSDmitry Torokhov {
12814104d13fSDmitry Torokhov 	struct aiptek *aiptek = dev_get_drvdata(dev);
12824104d13fSDmitry Torokhov 
12834c47097fSJiapeng Chong 	return sysfs_emit(buf, "%d\n", aiptek->curSetting.programmableDelay);
12844104d13fSDmitry Torokhov }
12854104d13fSDmitry Torokhov 
12864104d13fSDmitry Torokhov static ssize_t
store_tabletProgrammableDelay(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)12874104d13fSDmitry Torokhov store_tabletProgrammableDelay(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
12884104d13fSDmitry Torokhov {
12894104d13fSDmitry Torokhov 	struct aiptek *aiptek = dev_get_drvdata(dev);
129076496e7aSJJ Ding 	int err, d;
12914104d13fSDmitry Torokhov 
129276496e7aSJJ Ding 	err = kstrtoint(buf, 10, &d);
129376496e7aSJJ Ding 	if (err)
129476496e7aSJJ Ding 		return err;
1295160f1fefSJoe Rouvier 
129676496e7aSJJ Ding 	aiptek->newSetting.programmableDelay = d;
12974104d13fSDmitry Torokhov 	return count;
12984104d13fSDmitry Torokhov }
12994104d13fSDmitry Torokhov 
13004104d13fSDmitry Torokhov static DEVICE_ATTR(delay,
13016f07d31eSDmitry Torokhov 		   S_IRUGO | S_IWUSR,
13024104d13fSDmitry Torokhov 		   show_tabletProgrammableDelay, store_tabletProgrammableDelay);
13034104d13fSDmitry Torokhov 
13044104d13fSDmitry Torokhov /***********************************************************************
13054104d13fSDmitry Torokhov  * support routines for the 'event_count' file. Note that this file
13064104d13fSDmitry Torokhov  * only displays current setting.
13074104d13fSDmitry Torokhov  */
show_tabletEventsReceived(struct device * dev,struct device_attribute * attr,char * buf)13084104d13fSDmitry Torokhov static ssize_t show_tabletEventsReceived(struct device *dev, struct device_attribute *attr, char *buf)
13094104d13fSDmitry Torokhov {
13104104d13fSDmitry Torokhov 	struct aiptek *aiptek = dev_get_drvdata(dev);
13114104d13fSDmitry Torokhov 
13124c47097fSJiapeng Chong 	return sysfs_emit(buf, "%ld\n", aiptek->eventCount);
13134104d13fSDmitry Torokhov }
13144104d13fSDmitry Torokhov 
13154104d13fSDmitry Torokhov static DEVICE_ATTR(event_count, S_IRUGO, show_tabletEventsReceived, NULL);
13164104d13fSDmitry Torokhov 
13174104d13fSDmitry Torokhov /***********************************************************************
13184104d13fSDmitry Torokhov  * support routines for the 'diagnostic' file. Note that this file
13194104d13fSDmitry Torokhov  * only displays current setting.
13204104d13fSDmitry Torokhov  */
show_tabletDiagnosticMessage(struct device * dev,struct device_attribute * attr,char * buf)13214104d13fSDmitry Torokhov static ssize_t show_tabletDiagnosticMessage(struct device *dev, struct device_attribute *attr, char *buf)
13224104d13fSDmitry Torokhov {
13234104d13fSDmitry Torokhov 	struct aiptek *aiptek = dev_get_drvdata(dev);
13244104d13fSDmitry Torokhov 	char *retMsg;
13254104d13fSDmitry Torokhov 
13264104d13fSDmitry Torokhov 	switch (aiptek->diagnostic) {
13274104d13fSDmitry Torokhov 	case AIPTEK_DIAGNOSTIC_NA:
13284104d13fSDmitry Torokhov 		retMsg = "no errors\n";
13294104d13fSDmitry Torokhov 		break;
13304104d13fSDmitry Torokhov 
13314104d13fSDmitry Torokhov 	case AIPTEK_DIAGNOSTIC_SENDING_RELATIVE_IN_ABSOLUTE:
13324104d13fSDmitry Torokhov 		retMsg = "Error: receiving relative reports\n";
13334104d13fSDmitry Torokhov 		break;
13344104d13fSDmitry Torokhov 
13354104d13fSDmitry Torokhov 	case AIPTEK_DIAGNOSTIC_SENDING_ABSOLUTE_IN_RELATIVE:
13364104d13fSDmitry Torokhov 		retMsg = "Error: receiving absolute reports\n";
13374104d13fSDmitry Torokhov 		break;
13384104d13fSDmitry Torokhov 
13394104d13fSDmitry Torokhov 	case AIPTEK_DIAGNOSTIC_TOOL_DISALLOWED:
13404104d13fSDmitry Torokhov 		if (aiptek->curSetting.pointerMode ==
13414104d13fSDmitry Torokhov 		    AIPTEK_POINTER_ONLY_MOUSE_MODE) {
13424104d13fSDmitry Torokhov 			retMsg = "Error: receiving stylus reports\n";
13434104d13fSDmitry Torokhov 		} else {
13444104d13fSDmitry Torokhov 			retMsg = "Error: receiving mouse reports\n";
13454104d13fSDmitry Torokhov 		}
13464104d13fSDmitry Torokhov 		break;
13474104d13fSDmitry Torokhov 
13484104d13fSDmitry Torokhov 	default:
13494104d13fSDmitry Torokhov 		return 0;
13504104d13fSDmitry Torokhov 	}
13514c47097fSJiapeng Chong 	return sysfs_emit(buf, retMsg);
13524104d13fSDmitry Torokhov }
13534104d13fSDmitry Torokhov 
13544104d13fSDmitry Torokhov static DEVICE_ATTR(diagnostic, S_IRUGO, show_tabletDiagnosticMessage, NULL);
13554104d13fSDmitry Torokhov 
13564104d13fSDmitry Torokhov /***********************************************************************
13574104d13fSDmitry Torokhov  * support routines for the 'stylus_upper' file. Note that this file
13584104d13fSDmitry Torokhov  * both displays current setting and allows for setting changing.
13594104d13fSDmitry Torokhov  */
1360cd438a58SDmitry Torokhov 
1361cd438a58SDmitry Torokhov static struct aiptek_map stylus_button_map[] = {
1362cd438a58SDmitry Torokhov 	{ "upper",	AIPTEK_STYLUS_UPPER_BUTTON },
1363cd438a58SDmitry Torokhov 	{ "lower",	AIPTEK_STYLUS_LOWER_BUTTON },
1364cd438a58SDmitry Torokhov 	{ NULL,		AIPTEK_INVALID_VALUE }
1365cd438a58SDmitry Torokhov };
1366cd438a58SDmitry Torokhov 
show_tabletStylusUpper(struct device * dev,struct device_attribute * attr,char * buf)13674104d13fSDmitry Torokhov static ssize_t show_tabletStylusUpper(struct device *dev, struct device_attribute *attr, char *buf)
13684104d13fSDmitry Torokhov {
13694104d13fSDmitry Torokhov 	struct aiptek *aiptek = dev_get_drvdata(dev);
13704104d13fSDmitry Torokhov 
13714c47097fSJiapeng Chong 	return sysfs_emit(buf, "%s\n", map_val_to_str(stylus_button_map,
1372cd438a58SDmitry Torokhov 						      aiptek->curSetting.stylusButtonUpper));
13734104d13fSDmitry Torokhov }
13744104d13fSDmitry Torokhov 
13754104d13fSDmitry Torokhov static ssize_t
store_tabletStylusUpper(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)13764104d13fSDmitry Torokhov store_tabletStylusUpper(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
13774104d13fSDmitry Torokhov {
13784104d13fSDmitry Torokhov 	struct aiptek *aiptek = dev_get_drvdata(dev);
1379cd438a58SDmitry Torokhov 	int new_button = map_str_to_val(stylus_button_map, buf, count);
13804104d13fSDmitry Torokhov 
1381cd438a58SDmitry Torokhov 	if (new_button == AIPTEK_INVALID_VALUE)
1382cd438a58SDmitry Torokhov 		return -EINVAL;
1383cd438a58SDmitry Torokhov 
1384cd438a58SDmitry Torokhov 	aiptek->newSetting.stylusButtonUpper = new_button;
13854104d13fSDmitry Torokhov 	return count;
13864104d13fSDmitry Torokhov }
13874104d13fSDmitry Torokhov 
13884104d13fSDmitry Torokhov static DEVICE_ATTR(stylus_upper,
13896f07d31eSDmitry Torokhov 		   S_IRUGO | S_IWUSR,
13904104d13fSDmitry Torokhov 		   show_tabletStylusUpper, store_tabletStylusUpper);
13914104d13fSDmitry Torokhov 
13924104d13fSDmitry Torokhov /***********************************************************************
13934104d13fSDmitry Torokhov  * support routines for the 'stylus_lower' file. Note that this file
13944104d13fSDmitry Torokhov  * both displays current setting and allows for setting changing.
13954104d13fSDmitry Torokhov  */
1396cd438a58SDmitry Torokhov 
show_tabletStylusLower(struct device * dev,struct device_attribute * attr,char * buf)13974104d13fSDmitry Torokhov static ssize_t show_tabletStylusLower(struct device *dev, struct device_attribute *attr, char *buf)
13984104d13fSDmitry Torokhov {
13994104d13fSDmitry Torokhov 	struct aiptek *aiptek = dev_get_drvdata(dev);
14004104d13fSDmitry Torokhov 
14014c47097fSJiapeng Chong 	return sysfs_emit(buf, "%s\n", map_val_to_str(stylus_button_map,
1402cd438a58SDmitry Torokhov 						      aiptek->curSetting.stylusButtonLower));
14034104d13fSDmitry Torokhov }
14044104d13fSDmitry Torokhov 
14054104d13fSDmitry Torokhov static ssize_t
store_tabletStylusLower(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)14064104d13fSDmitry Torokhov store_tabletStylusLower(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
14074104d13fSDmitry Torokhov {
14084104d13fSDmitry Torokhov 	struct aiptek *aiptek = dev_get_drvdata(dev);
1409cd438a58SDmitry Torokhov 	int new_button = map_str_to_val(stylus_button_map, buf, count);
14104104d13fSDmitry Torokhov 
1411cd438a58SDmitry Torokhov 	if (new_button == AIPTEK_INVALID_VALUE)
1412cd438a58SDmitry Torokhov 		return -EINVAL;
1413cd438a58SDmitry Torokhov 
1414cd438a58SDmitry Torokhov 	aiptek->newSetting.stylusButtonLower = new_button;
14154104d13fSDmitry Torokhov 	return count;
14164104d13fSDmitry Torokhov }
14174104d13fSDmitry Torokhov 
14184104d13fSDmitry Torokhov static DEVICE_ATTR(stylus_lower,
14196f07d31eSDmitry Torokhov 		   S_IRUGO | S_IWUSR,
14204104d13fSDmitry Torokhov 		   show_tabletStylusLower, store_tabletStylusLower);
14214104d13fSDmitry Torokhov 
14224104d13fSDmitry Torokhov /***********************************************************************
14234104d13fSDmitry Torokhov  * support routines for the 'mouse_left' file. Note that this file
14244104d13fSDmitry Torokhov  * both displays current setting and allows for setting changing.
14254104d13fSDmitry Torokhov  */
1426cd438a58SDmitry Torokhov 
1427cd438a58SDmitry Torokhov static struct aiptek_map mouse_button_map[] = {
1428cd438a58SDmitry Torokhov 	{ "left",	AIPTEK_MOUSE_LEFT_BUTTON },
1429cd438a58SDmitry Torokhov 	{ "middle",	AIPTEK_MOUSE_MIDDLE_BUTTON },
1430cd438a58SDmitry Torokhov 	{ "right",	AIPTEK_MOUSE_RIGHT_BUTTON },
1431cd438a58SDmitry Torokhov 	{ NULL,		AIPTEK_INVALID_VALUE }
1432cd438a58SDmitry Torokhov };
1433cd438a58SDmitry Torokhov 
show_tabletMouseLeft(struct device * dev,struct device_attribute * attr,char * buf)14344104d13fSDmitry Torokhov static ssize_t show_tabletMouseLeft(struct device *dev, struct device_attribute *attr, char *buf)
14354104d13fSDmitry Torokhov {
14364104d13fSDmitry Torokhov 	struct aiptek *aiptek = dev_get_drvdata(dev);
14374104d13fSDmitry Torokhov 
14384c47097fSJiapeng Chong 	return sysfs_emit(buf, "%s\n", map_val_to_str(mouse_button_map,
1439cd438a58SDmitry Torokhov 						      aiptek->curSetting.mouseButtonLeft));
14404104d13fSDmitry Torokhov }
14414104d13fSDmitry Torokhov 
14424104d13fSDmitry Torokhov static ssize_t
store_tabletMouseLeft(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)14434104d13fSDmitry Torokhov store_tabletMouseLeft(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
14444104d13fSDmitry Torokhov {
14454104d13fSDmitry Torokhov 	struct aiptek *aiptek = dev_get_drvdata(dev);
1446cd438a58SDmitry Torokhov 	int new_button = map_str_to_val(mouse_button_map, buf, count);
14474104d13fSDmitry Torokhov 
1448cd438a58SDmitry Torokhov 	if (new_button == AIPTEK_INVALID_VALUE)
1449cd438a58SDmitry Torokhov 		return -EINVAL;
1450cd438a58SDmitry Torokhov 
1451cd438a58SDmitry Torokhov 	aiptek->newSetting.mouseButtonLeft = new_button;
14524104d13fSDmitry Torokhov 	return count;
14534104d13fSDmitry Torokhov }
14544104d13fSDmitry Torokhov 
14554104d13fSDmitry Torokhov static DEVICE_ATTR(mouse_left,
14566f07d31eSDmitry Torokhov 		   S_IRUGO | S_IWUSR,
14574104d13fSDmitry Torokhov 		   show_tabletMouseLeft, store_tabletMouseLeft);
14584104d13fSDmitry Torokhov 
14594104d13fSDmitry Torokhov /***********************************************************************
14604104d13fSDmitry Torokhov  * support routines for the 'mouse_middle' file. Note that this file
14614104d13fSDmitry Torokhov  * both displays current setting and allows for setting changing.
14624104d13fSDmitry Torokhov  */
show_tabletMouseMiddle(struct device * dev,struct device_attribute * attr,char * buf)14634104d13fSDmitry Torokhov static ssize_t show_tabletMouseMiddle(struct device *dev, struct device_attribute *attr, char *buf)
14644104d13fSDmitry Torokhov {
14654104d13fSDmitry Torokhov 	struct aiptek *aiptek = dev_get_drvdata(dev);
14664104d13fSDmitry Torokhov 
14674c47097fSJiapeng Chong 	return sysfs_emit(buf, "%s\n", map_val_to_str(mouse_button_map,
1468cd438a58SDmitry Torokhov 						      aiptek->curSetting.mouseButtonMiddle));
14694104d13fSDmitry Torokhov }
14704104d13fSDmitry Torokhov 
14714104d13fSDmitry Torokhov static ssize_t
store_tabletMouseMiddle(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)14724104d13fSDmitry Torokhov store_tabletMouseMiddle(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
14734104d13fSDmitry Torokhov {
14744104d13fSDmitry Torokhov 	struct aiptek *aiptek = dev_get_drvdata(dev);
1475cd438a58SDmitry Torokhov 	int new_button = map_str_to_val(mouse_button_map, buf, count);
14764104d13fSDmitry Torokhov 
1477cd438a58SDmitry Torokhov 	if (new_button == AIPTEK_INVALID_VALUE)
1478cd438a58SDmitry Torokhov 		return -EINVAL;
1479cd438a58SDmitry Torokhov 
1480cd438a58SDmitry Torokhov 	aiptek->newSetting.mouseButtonMiddle = new_button;
14814104d13fSDmitry Torokhov 	return count;
14824104d13fSDmitry Torokhov }
14834104d13fSDmitry Torokhov 
14844104d13fSDmitry Torokhov static DEVICE_ATTR(mouse_middle,
14856f07d31eSDmitry Torokhov 		   S_IRUGO | S_IWUSR,
14864104d13fSDmitry Torokhov 		   show_tabletMouseMiddle, store_tabletMouseMiddle);
14874104d13fSDmitry Torokhov 
14884104d13fSDmitry Torokhov /***********************************************************************
14894104d13fSDmitry Torokhov  * support routines for the 'mouse_right' file. Note that this file
14904104d13fSDmitry Torokhov  * both displays current setting and allows for setting changing.
14914104d13fSDmitry Torokhov  */
show_tabletMouseRight(struct device * dev,struct device_attribute * attr,char * buf)14924104d13fSDmitry Torokhov static ssize_t show_tabletMouseRight(struct device *dev, struct device_attribute *attr, char *buf)
14934104d13fSDmitry Torokhov {
14944104d13fSDmitry Torokhov 	struct aiptek *aiptek = dev_get_drvdata(dev);
14954104d13fSDmitry Torokhov 
14964c47097fSJiapeng Chong 	return sysfs_emit(buf, "%s\n", map_val_to_str(mouse_button_map,
1497cd438a58SDmitry Torokhov 						      aiptek->curSetting.mouseButtonRight));
14984104d13fSDmitry Torokhov }
14994104d13fSDmitry Torokhov 
15004104d13fSDmitry Torokhov static ssize_t
store_tabletMouseRight(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)15014104d13fSDmitry Torokhov store_tabletMouseRight(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
15024104d13fSDmitry Torokhov {
15034104d13fSDmitry Torokhov 	struct aiptek *aiptek = dev_get_drvdata(dev);
1504cd438a58SDmitry Torokhov 	int new_button = map_str_to_val(mouse_button_map, buf, count);
15054104d13fSDmitry Torokhov 
1506cd438a58SDmitry Torokhov 	if (new_button == AIPTEK_INVALID_VALUE)
1507cd438a58SDmitry Torokhov 		return -EINVAL;
1508cd438a58SDmitry Torokhov 
1509cd438a58SDmitry Torokhov 	aiptek->newSetting.mouseButtonRight = new_button;
15104104d13fSDmitry Torokhov 	return count;
15114104d13fSDmitry Torokhov }
15124104d13fSDmitry Torokhov 
15134104d13fSDmitry Torokhov static DEVICE_ATTR(mouse_right,
15146f07d31eSDmitry Torokhov 		   S_IRUGO | S_IWUSR,
15154104d13fSDmitry Torokhov 		   show_tabletMouseRight, store_tabletMouseRight);
15164104d13fSDmitry Torokhov 
15174104d13fSDmitry Torokhov /***********************************************************************
15184104d13fSDmitry Torokhov  * support routines for the 'wheel' file. Note that this file
15194104d13fSDmitry Torokhov  * both displays current setting and allows for setting changing.
15204104d13fSDmitry Torokhov  */
show_tabletWheel(struct device * dev,struct device_attribute * attr,char * buf)15214104d13fSDmitry Torokhov static ssize_t show_tabletWheel(struct device *dev, struct device_attribute *attr, char *buf)
15224104d13fSDmitry Torokhov {
15234104d13fSDmitry Torokhov 	struct aiptek *aiptek = dev_get_drvdata(dev);
15244104d13fSDmitry Torokhov 
15254104d13fSDmitry Torokhov 	if (aiptek->curSetting.wheel == AIPTEK_WHEEL_DISABLE) {
15264c47097fSJiapeng Chong 		return sysfs_emit(buf, "disable\n");
15274104d13fSDmitry Torokhov 	} else {
15284c47097fSJiapeng Chong 		return sysfs_emit(buf, "%d\n", aiptek->curSetting.wheel);
15294104d13fSDmitry Torokhov 	}
15304104d13fSDmitry Torokhov }
15314104d13fSDmitry Torokhov 
15324104d13fSDmitry Torokhov static ssize_t
store_tabletWheel(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)15334104d13fSDmitry Torokhov store_tabletWheel(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
15344104d13fSDmitry Torokhov {
15354104d13fSDmitry Torokhov 	struct aiptek *aiptek = dev_get_drvdata(dev);
153676496e7aSJJ Ding 	int err, w;
15374104d13fSDmitry Torokhov 
153876496e7aSJJ Ding 	err = kstrtoint(buf, 10, &w);
153976496e7aSJJ Ding 	if (err)
154076496e7aSJJ Ding 		return err;
1541160f1fefSJoe Rouvier 
154276496e7aSJJ Ding 	aiptek->newSetting.wheel = w;
15434104d13fSDmitry Torokhov 	return count;
15444104d13fSDmitry Torokhov }
15454104d13fSDmitry Torokhov 
15464104d13fSDmitry Torokhov static DEVICE_ATTR(wheel,
15476f07d31eSDmitry Torokhov 		   S_IRUGO | S_IWUSR, show_tabletWheel, store_tabletWheel);
15484104d13fSDmitry Torokhov 
15494104d13fSDmitry Torokhov /***********************************************************************
15504104d13fSDmitry Torokhov  * support routines for the 'execute' file. Note that this file
15514104d13fSDmitry Torokhov  * both displays current setting and allows for setting changing.
15524104d13fSDmitry Torokhov  */
show_tabletExecute(struct device * dev,struct device_attribute * attr,char * buf)15534104d13fSDmitry Torokhov static ssize_t show_tabletExecute(struct device *dev, struct device_attribute *attr, char *buf)
15544104d13fSDmitry Torokhov {
15554104d13fSDmitry Torokhov 	/* There is nothing useful to display, so a one-line manual
15564104d13fSDmitry Torokhov 	 * is in order...
15574104d13fSDmitry Torokhov 	 */
15584c47097fSJiapeng Chong 	return sysfs_emit(buf, "Write anything to this file to program your tablet.\n");
15594104d13fSDmitry Torokhov }
15604104d13fSDmitry Torokhov 
15614104d13fSDmitry Torokhov static ssize_t
store_tabletExecute(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)15624104d13fSDmitry Torokhov store_tabletExecute(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
15634104d13fSDmitry Torokhov {
15644104d13fSDmitry Torokhov 	struct aiptek *aiptek = dev_get_drvdata(dev);
15654104d13fSDmitry Torokhov 
15664104d13fSDmitry Torokhov 	/* We do not care what you write to this file. Merely the action
15674104d13fSDmitry Torokhov 	 * of writing to this file triggers a tablet reprogramming.
15684104d13fSDmitry Torokhov 	 */
15694104d13fSDmitry Torokhov 	memcpy(&aiptek->curSetting, &aiptek->newSetting,
15704104d13fSDmitry Torokhov 	       sizeof(struct aiptek_settings));
15714104d13fSDmitry Torokhov 
15724104d13fSDmitry Torokhov 	if (aiptek_program_tablet(aiptek) < 0)
15734104d13fSDmitry Torokhov 		return -EIO;
15744104d13fSDmitry Torokhov 
15754104d13fSDmitry Torokhov 	return count;
15764104d13fSDmitry Torokhov }
15774104d13fSDmitry Torokhov 
15784104d13fSDmitry Torokhov static DEVICE_ATTR(execute,
15796f07d31eSDmitry Torokhov 		   S_IRUGO | S_IWUSR, show_tabletExecute, store_tabletExecute);
15804104d13fSDmitry Torokhov 
15814104d13fSDmitry Torokhov /***********************************************************************
15824104d13fSDmitry Torokhov  * support routines for the 'odm_code' file. Note that this file
15834104d13fSDmitry Torokhov  * only displays current setting.
15844104d13fSDmitry Torokhov  */
show_tabletODMCode(struct device * dev,struct device_attribute * attr,char * buf)15854104d13fSDmitry Torokhov static ssize_t show_tabletODMCode(struct device *dev, struct device_attribute *attr, char *buf)
15864104d13fSDmitry Torokhov {
15874104d13fSDmitry Torokhov 	struct aiptek *aiptek = dev_get_drvdata(dev);
15884104d13fSDmitry Torokhov 
15894c47097fSJiapeng Chong 	return sysfs_emit(buf, "0x%04x\n", aiptek->features.odmCode);
15904104d13fSDmitry Torokhov }
15914104d13fSDmitry Torokhov 
15924104d13fSDmitry Torokhov static DEVICE_ATTR(odm_code, S_IRUGO, show_tabletODMCode, NULL);
15934104d13fSDmitry Torokhov 
15944104d13fSDmitry Torokhov /***********************************************************************
15954104d13fSDmitry Torokhov  * support routines for the 'model_code' file. Note that this file
15964104d13fSDmitry Torokhov  * only displays current setting.
15974104d13fSDmitry Torokhov  */
show_tabletModelCode(struct device * dev,struct device_attribute * attr,char * buf)15984104d13fSDmitry Torokhov static ssize_t show_tabletModelCode(struct device *dev, struct device_attribute *attr, char *buf)
15994104d13fSDmitry Torokhov {
16004104d13fSDmitry Torokhov 	struct aiptek *aiptek = dev_get_drvdata(dev);
16014104d13fSDmitry Torokhov 
16024c47097fSJiapeng Chong 	return sysfs_emit(buf, "0x%04x\n", aiptek->features.modelCode);
16034104d13fSDmitry Torokhov }
16044104d13fSDmitry Torokhov 
16054104d13fSDmitry Torokhov static DEVICE_ATTR(model_code, S_IRUGO, show_tabletModelCode, NULL);
16064104d13fSDmitry Torokhov 
16074104d13fSDmitry Torokhov /***********************************************************************
16084104d13fSDmitry Torokhov  * support routines for the 'firmware_code' file. Note that this file
16094104d13fSDmitry Torokhov  * only displays current setting.
16104104d13fSDmitry Torokhov  */
show_firmwareCode(struct device * dev,struct device_attribute * attr,char * buf)16114104d13fSDmitry Torokhov static ssize_t show_firmwareCode(struct device *dev, struct device_attribute *attr, char *buf)
16124104d13fSDmitry Torokhov {
16134104d13fSDmitry Torokhov 	struct aiptek *aiptek = dev_get_drvdata(dev);
16144104d13fSDmitry Torokhov 
16154c47097fSJiapeng Chong 	return sysfs_emit(buf, "%04x\n", aiptek->features.firmwareCode);
16164104d13fSDmitry Torokhov }
16174104d13fSDmitry Torokhov 
16184104d13fSDmitry Torokhov static DEVICE_ATTR(firmware_code, S_IRUGO, show_firmwareCode, NULL);
16194104d13fSDmitry Torokhov 
1620*f4e7a254SDmitry Torokhov static struct attribute *aiptek_dev_attrs[] = {
1621b087e1f3SDmitry Torokhov 	&dev_attr_size.attr,
1622b087e1f3SDmitry Torokhov 	&dev_attr_pointer_mode.attr,
1623b087e1f3SDmitry Torokhov 	&dev_attr_coordinate_mode.attr,
1624b087e1f3SDmitry Torokhov 	&dev_attr_tool_mode.attr,
1625b087e1f3SDmitry Torokhov 	&dev_attr_xtilt.attr,
1626b087e1f3SDmitry Torokhov 	&dev_attr_ytilt.attr,
1627b087e1f3SDmitry Torokhov 	&dev_attr_jitter.attr,
1628b087e1f3SDmitry Torokhov 	&dev_attr_delay.attr,
1629b087e1f3SDmitry Torokhov 	&dev_attr_event_count.attr,
1630b087e1f3SDmitry Torokhov 	&dev_attr_diagnostic.attr,
1631b087e1f3SDmitry Torokhov 	&dev_attr_odm_code.attr,
1632b087e1f3SDmitry Torokhov 	&dev_attr_model_code.attr,
1633b087e1f3SDmitry Torokhov 	&dev_attr_firmware_code.attr,
1634b087e1f3SDmitry Torokhov 	&dev_attr_stylus_lower.attr,
1635b087e1f3SDmitry Torokhov 	&dev_attr_stylus_upper.attr,
1636b087e1f3SDmitry Torokhov 	&dev_attr_mouse_left.attr,
1637b087e1f3SDmitry Torokhov 	&dev_attr_mouse_middle.attr,
1638b087e1f3SDmitry Torokhov 	&dev_attr_mouse_right.attr,
1639b087e1f3SDmitry Torokhov 	&dev_attr_wheel.attr,
1640b087e1f3SDmitry Torokhov 	&dev_attr_execute.attr,
1641b087e1f3SDmitry Torokhov 	NULL
1642b087e1f3SDmitry Torokhov };
16434104d13fSDmitry Torokhov 
1644*f4e7a254SDmitry Torokhov ATTRIBUTE_GROUPS(aiptek_dev);
16454104d13fSDmitry Torokhov 
16464104d13fSDmitry Torokhov /***********************************************************************
16474104d13fSDmitry Torokhov  * This routine is called when a tablet has been identified. It basically
16484104d13fSDmitry Torokhov  * sets up the tablet and the driver's internal structures.
16494104d13fSDmitry Torokhov  */
16504104d13fSDmitry Torokhov static int
aiptek_probe(struct usb_interface * intf,const struct usb_device_id * id)16514104d13fSDmitry Torokhov aiptek_probe(struct usb_interface *intf, const struct usb_device_id *id)
16524104d13fSDmitry Torokhov {
16534104d13fSDmitry Torokhov 	struct usb_device *usbdev = interface_to_usbdev(intf);
16544104d13fSDmitry Torokhov 	struct usb_endpoint_descriptor *endpoint;
16554104d13fSDmitry Torokhov 	struct aiptek *aiptek;
16564104d13fSDmitry Torokhov 	struct input_dev *inputdev;
16574104d13fSDmitry Torokhov 	int i;
16584104d13fSDmitry Torokhov 	int speeds[] = { 0,
16594104d13fSDmitry Torokhov 		AIPTEK_PROGRAMMABLE_DELAY_50,
16604104d13fSDmitry Torokhov 		AIPTEK_PROGRAMMABLE_DELAY_400,
16614104d13fSDmitry Torokhov 		AIPTEK_PROGRAMMABLE_DELAY_25,
16624104d13fSDmitry Torokhov 		AIPTEK_PROGRAMMABLE_DELAY_100,
16634104d13fSDmitry Torokhov 		AIPTEK_PROGRAMMABLE_DELAY_200,
16644104d13fSDmitry Torokhov 		AIPTEK_PROGRAMMABLE_DELAY_300
16654104d13fSDmitry Torokhov 	};
16664104d13fSDmitry Torokhov 	int err = -ENOMEM;
16674104d13fSDmitry Torokhov 
16684104d13fSDmitry Torokhov 	/* programmableDelay is where the command-line specified
16694104d13fSDmitry Torokhov 	 * delay is kept. We make it the first element of speeds[],
16704104d13fSDmitry Torokhov 	 * so therefore, your override speed is tried first, then the
16714104d13fSDmitry Torokhov 	 * remainder. Note that the default value of 400ms will be tried
16724104d13fSDmitry Torokhov 	 * if you do not specify any command line parameter.
16734104d13fSDmitry Torokhov 	 */
16744104d13fSDmitry Torokhov 	speeds[0] = programmableDelay;
16754104d13fSDmitry Torokhov 
16764104d13fSDmitry Torokhov 	aiptek = kzalloc(sizeof(struct aiptek), GFP_KERNEL);
16774104d13fSDmitry Torokhov 	inputdev = input_allocate_device();
16786125a400SRene van Paassen 	if (!aiptek || !inputdev) {
16791817b169SGreg Kroah-Hartman 		dev_warn(&intf->dev,
16801817b169SGreg Kroah-Hartman 			 "cannot allocate memory or input device\n");
16814104d13fSDmitry Torokhov 		goto fail1;
16826125a400SRene van Paassen         }
16834104d13fSDmitry Torokhov 
1684997ea58eSDaniel Mack 	aiptek->data = usb_alloc_coherent(usbdev, AIPTEK_PACKET_LENGTH,
168553fddb66SJia-Ju Bai 					  GFP_KERNEL, &aiptek->data_dma);
16866125a400SRene van Paassen         if (!aiptek->data) {
16871817b169SGreg Kroah-Hartman 		dev_warn(&intf->dev, "cannot allocate usb buffer\n");
16884104d13fSDmitry Torokhov 		goto fail1;
16896125a400SRene van Paassen 	}
16904104d13fSDmitry Torokhov 
16914104d13fSDmitry Torokhov 	aiptek->urb = usb_alloc_urb(0, GFP_KERNEL);
16926125a400SRene van Paassen 	if (!aiptek->urb) {
16931817b169SGreg Kroah-Hartman 	        dev_warn(&intf->dev, "cannot allocate urb\n");
16944104d13fSDmitry Torokhov 		goto fail2;
16956125a400SRene van Paassen 	}
16964104d13fSDmitry Torokhov 
16974104d13fSDmitry Torokhov 	aiptek->inputdev = inputdev;
1698871ba51cSGreg Kroah-Hartman 	aiptek->intf = intf;
1699cfa4f6a9SJohan Hovold 	aiptek->ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
17004104d13fSDmitry Torokhov 	aiptek->inDelay = 0;
17014104d13fSDmitry Torokhov 	aiptek->endDelay = 0;
17024104d13fSDmitry Torokhov 	aiptek->previousJitterable = 0;
17031e7b3faeSRene van Paassen 	aiptek->lastMacro = -1;
17044104d13fSDmitry Torokhov 
17054104d13fSDmitry Torokhov 	/* Set up the curSettings struct. Said struct contains the current
17064104d13fSDmitry Torokhov 	 * programmable parameters. The newSetting struct contains changes
17074104d13fSDmitry Torokhov 	 * the user makes to the settings via the sysfs interface. Those
17084104d13fSDmitry Torokhov 	 * changes are not "committed" to curSettings until the user
17094104d13fSDmitry Torokhov 	 * writes to the sysfs/.../execute file.
17104104d13fSDmitry Torokhov 	 */
17114104d13fSDmitry Torokhov 	aiptek->curSetting.pointerMode = AIPTEK_POINTER_EITHER_MODE;
17124104d13fSDmitry Torokhov 	aiptek->curSetting.coordinateMode = AIPTEK_COORDINATE_ABSOLUTE_MODE;
17134104d13fSDmitry Torokhov 	aiptek->curSetting.toolMode = AIPTEK_TOOL_BUTTON_PEN_MODE;
17144104d13fSDmitry Torokhov 	aiptek->curSetting.xTilt = AIPTEK_TILT_DISABLE;
17154104d13fSDmitry Torokhov 	aiptek->curSetting.yTilt = AIPTEK_TILT_DISABLE;
17164104d13fSDmitry Torokhov 	aiptek->curSetting.mouseButtonLeft = AIPTEK_MOUSE_LEFT_BUTTON;
17174104d13fSDmitry Torokhov 	aiptek->curSetting.mouseButtonMiddle = AIPTEK_MOUSE_MIDDLE_BUTTON;
17184104d13fSDmitry Torokhov 	aiptek->curSetting.mouseButtonRight = AIPTEK_MOUSE_RIGHT_BUTTON;
17194104d13fSDmitry Torokhov 	aiptek->curSetting.stylusButtonUpper = AIPTEK_STYLUS_UPPER_BUTTON;
17204104d13fSDmitry Torokhov 	aiptek->curSetting.stylusButtonLower = AIPTEK_STYLUS_LOWER_BUTTON;
17214104d13fSDmitry Torokhov 	aiptek->curSetting.jitterDelay = jitterDelay;
17224104d13fSDmitry Torokhov 	aiptek->curSetting.programmableDelay = programmableDelay;
17234104d13fSDmitry Torokhov 
17244104d13fSDmitry Torokhov 	/* Both structs should have equivalent settings
17254104d13fSDmitry Torokhov 	 */
17264104d13fSDmitry Torokhov 	aiptek->newSetting = aiptek->curSetting;
17274104d13fSDmitry Torokhov 
17284104d13fSDmitry Torokhov 	/* Determine the usb devices' physical path.
17294104d13fSDmitry Torokhov 	 * Asketh not why we always pretend we're using "../input0",
17304104d13fSDmitry Torokhov 	 * but I suspect this will have to be refactored one
17314104d13fSDmitry Torokhov 	 * day if a single USB device can be a keyboard & a mouse
17324104d13fSDmitry Torokhov 	 * & a tablet, and the inputX number actually will tell
17334104d13fSDmitry Torokhov 	 * us something...
17344104d13fSDmitry Torokhov 	 */
17354104d13fSDmitry Torokhov 	usb_make_path(usbdev, aiptek->features.usbPath,
17364104d13fSDmitry Torokhov 			sizeof(aiptek->features.usbPath));
17374104d13fSDmitry Torokhov 	strlcat(aiptek->features.usbPath, "/input0",
17384104d13fSDmitry Torokhov 		sizeof(aiptek->features.usbPath));
17394104d13fSDmitry Torokhov 
17404104d13fSDmitry Torokhov 	/* Set up client data, pointers to open and close routines
17414104d13fSDmitry Torokhov 	 * for the input device.
17424104d13fSDmitry Torokhov 	 */
17434104d13fSDmitry Torokhov 	inputdev->name = "Aiptek";
17444104d13fSDmitry Torokhov 	inputdev->phys = aiptek->features.usbPath;
17454104d13fSDmitry Torokhov 	usb_to_input_id(usbdev, &inputdev->id);
17464104d13fSDmitry Torokhov 	inputdev->dev.parent = &intf->dev;
17474104d13fSDmitry Torokhov 
17484104d13fSDmitry Torokhov 	input_set_drvdata(inputdev, aiptek);
17494104d13fSDmitry Torokhov 
17504104d13fSDmitry Torokhov 	inputdev->open = aiptek_open;
17514104d13fSDmitry Torokhov 	inputdev->close = aiptek_close;
17524104d13fSDmitry Torokhov 
17534104d13fSDmitry Torokhov 	/* Now program the capacities of the tablet, in terms of being
17544104d13fSDmitry Torokhov 	 * an input device.
17554104d13fSDmitry Torokhov 	 */
17561a54f49eSRene van Paassen 	for (i = 0; i < ARRAY_SIZE(eventTypes); ++i)
17571a54f49eSRene van Paassen 	        __set_bit(eventTypes[i], inputdev->evbit);
17584104d13fSDmitry Torokhov 
17591a54f49eSRene van Paassen 	for (i = 0; i < ARRAY_SIZE(absEvents); ++i)
17601a54f49eSRene van Paassen 	        __set_bit(absEvents[i], inputdev->absbit);
17614104d13fSDmitry Torokhov 
17621a54f49eSRene van Paassen 	for (i = 0; i < ARRAY_SIZE(relEvents); ++i)
17631a54f49eSRene van Paassen 	        __set_bit(relEvents[i], inputdev->relbit);
17644104d13fSDmitry Torokhov 
17651a54f49eSRene van Paassen 	__set_bit(MSC_SERIAL, inputdev->mscbit);
17664104d13fSDmitry Torokhov 
176733936fa6SDmitry Torokhov 	/* Set up key and button codes */
176833936fa6SDmitry Torokhov 	for (i = 0; i < ARRAY_SIZE(buttonEvents); ++i)
176933936fa6SDmitry Torokhov 		__set_bit(buttonEvents[i], inputdev->keybit);
177033936fa6SDmitry Torokhov 
17714104d13fSDmitry Torokhov 	for (i = 0; i < ARRAY_SIZE(macroKeyEvents); ++i)
177233936fa6SDmitry Torokhov 		__set_bit(macroKeyEvents[i], inputdev->keybit);
17734104d13fSDmitry Torokhov 
17744104d13fSDmitry Torokhov 	/*
17754104d13fSDmitry Torokhov 	 * Program the input device coordinate capacities. We do not yet
17764104d13fSDmitry Torokhov 	 * know what maximum X, Y, and Z values are, so we're putting fake
17774104d13fSDmitry Torokhov 	 * values in. Later, we'll ask the tablet to put in the correct
17784104d13fSDmitry Torokhov 	 * values.
17794104d13fSDmitry Torokhov 	 */
17804104d13fSDmitry Torokhov 	input_set_abs_params(inputdev, ABS_X, 0, 2999, 0, 0);
17814104d13fSDmitry Torokhov 	input_set_abs_params(inputdev, ABS_Y, 0, 2249, 0, 0);
17824104d13fSDmitry Torokhov 	input_set_abs_params(inputdev, ABS_PRESSURE, 0, 511, 0, 0);
17834104d13fSDmitry Torokhov 	input_set_abs_params(inputdev, ABS_TILT_X, AIPTEK_TILT_MIN, AIPTEK_TILT_MAX, 0, 0);
17844104d13fSDmitry Torokhov 	input_set_abs_params(inputdev, ABS_TILT_Y, AIPTEK_TILT_MIN, AIPTEK_TILT_MAX, 0, 0);
17854104d13fSDmitry Torokhov 	input_set_abs_params(inputdev, ABS_WHEEL, AIPTEK_WHEEL_MIN, AIPTEK_WHEEL_MAX - 1, 0, 0);
17864104d13fSDmitry Torokhov 
17875600f698SPavel Skripkin 	err = usb_find_common_endpoints(intf->cur_altsetting,
17885600f698SPavel Skripkin 					NULL, NULL, &endpoint, NULL);
17895600f698SPavel Skripkin 	if (err) {
17908e20cf2bSVladis Dronov 		dev_err(&intf->dev,
17915600f698SPavel Skripkin 			"interface has no int in endpoints, but must have minimum 1\n");
17928e20cf2bSVladis Dronov 		goto fail3;
17938e20cf2bSVladis Dronov 	}
17944104d13fSDmitry Torokhov 
17954104d13fSDmitry Torokhov 	/* Go set up our URB, which is called when the tablet receives
17964104d13fSDmitry Torokhov 	 * input.
17974104d13fSDmitry Torokhov 	 */
17984104d13fSDmitry Torokhov 	usb_fill_int_urb(aiptek->urb,
1799c6309018SOliver Neukum 			 usbdev,
1800c6309018SOliver Neukum 			 usb_rcvintpipe(usbdev,
18014104d13fSDmitry Torokhov 					endpoint->bEndpointAddress),
18024104d13fSDmitry Torokhov 			 aiptek->data, 8, aiptek_irq, aiptek,
18034104d13fSDmitry Torokhov 			 endpoint->bInterval);
18044104d13fSDmitry Torokhov 
18054104d13fSDmitry Torokhov 	aiptek->urb->transfer_dma = aiptek->data_dma;
18064104d13fSDmitry Torokhov 	aiptek->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
18074104d13fSDmitry Torokhov 
18084104d13fSDmitry Torokhov 	/* Program the tablet. This sets the tablet up in the mode
18094104d13fSDmitry Torokhov 	 * specified in newSetting, and also queries the tablet's
18104104d13fSDmitry Torokhov 	 * physical capacities.
18114104d13fSDmitry Torokhov 	 *
18124104d13fSDmitry Torokhov 	 * Sanity check: if a tablet doesn't like the slow programmatic
18134104d13fSDmitry Torokhov 	 * delay, we often get sizes of 0x0. Let's use that as an indicator
18144104d13fSDmitry Torokhov 	 * to try faster delays, up to 25 ms. If that logic fails, well, you'll
18154104d13fSDmitry Torokhov 	 * have to explain to us how your tablet thinks it's 0x0, and yet that's
18164104d13fSDmitry Torokhov 	 * not an error :-)
18174104d13fSDmitry Torokhov 	 */
18184104d13fSDmitry Torokhov 
18194104d13fSDmitry Torokhov 	for (i = 0; i < ARRAY_SIZE(speeds); ++i) {
18204104d13fSDmitry Torokhov 		aiptek->curSetting.programmableDelay = speeds[i];
18214104d13fSDmitry Torokhov 		(void)aiptek_program_tablet(aiptek);
1822987a6c02SDaniel Mack 		if (input_abs_get_max(aiptek->inputdev, ABS_X) > 0) {
1823899ef6e7SGreg Kroah-Hartman 			dev_info(&intf->dev,
1824899ef6e7SGreg Kroah-Hartman 				 "Aiptek using %d ms programming speed\n",
18254104d13fSDmitry Torokhov 				 aiptek->curSetting.programmableDelay);
18264104d13fSDmitry Torokhov 			break;
18274104d13fSDmitry Torokhov 		}
18284104d13fSDmitry Torokhov 	}
18294104d13fSDmitry Torokhov 
18306125a400SRene van Paassen 	/* Murphy says that some day someone will have a tablet that fails the
18316125a400SRene van Paassen 	   above test. That's you, Frederic Rodrigo */
18326125a400SRene van Paassen 	if (i == ARRAY_SIZE(speeds)) {
1833899ef6e7SGreg Kroah-Hartman 		dev_info(&intf->dev,
1834899ef6e7SGreg Kroah-Hartman 			 "Aiptek tried all speeds, no sane response\n");
18358e20cf2bSVladis Dronov 		err = -EINVAL;
1836482d74ceSJulia Lawall 		goto fail3;
18376125a400SRene van Paassen 	}
18386125a400SRene van Paassen 
18394104d13fSDmitry Torokhov 	/* Associate this driver's struct with the usb interface.
18404104d13fSDmitry Torokhov 	 */
18414104d13fSDmitry Torokhov 	usb_set_intfdata(intf, aiptek);
18424104d13fSDmitry Torokhov 
1843b087e1f3SDmitry Torokhov 	/* Register the tablet as an Input Device
1844b087e1f3SDmitry Torokhov 	 */
1845b087e1f3SDmitry Torokhov 	err = input_register_device(aiptek->inputdev);
18466125a400SRene van Paassen 	if (err) {
18471817b169SGreg Kroah-Hartman 		dev_warn(&intf->dev,
18481817b169SGreg Kroah-Hartman 			 "input_register_device returned err: %d\n", err);
1849*f4e7a254SDmitry Torokhov 		goto fail3;
18506125a400SRene van Paassen         }
18514104d13fSDmitry Torokhov 	return 0;
18524104d13fSDmitry Torokhov 
1853b087e1f3SDmitry Torokhov  fail3: usb_free_urb(aiptek->urb);
1854997ea58eSDaniel Mack  fail2:	usb_free_coherent(usbdev, AIPTEK_PACKET_LENGTH, aiptek->data,
18554104d13fSDmitry Torokhov 			  aiptek->data_dma);
1856b087e1f3SDmitry Torokhov  fail1: usb_set_intfdata(intf, NULL);
1857b087e1f3SDmitry Torokhov 	input_free_device(inputdev);
18584104d13fSDmitry Torokhov 	kfree(aiptek);
18594104d13fSDmitry Torokhov 	return err;
18604104d13fSDmitry Torokhov }
18614104d13fSDmitry Torokhov 
18624104d13fSDmitry Torokhov /***********************************************************************
18634104d13fSDmitry Torokhov  * Deal with tablet disconnecting from the system.
18644104d13fSDmitry Torokhov  */
aiptek_disconnect(struct usb_interface * intf)18654104d13fSDmitry Torokhov static void aiptek_disconnect(struct usb_interface *intf)
18664104d13fSDmitry Torokhov {
18674104d13fSDmitry Torokhov 	struct aiptek *aiptek = usb_get_intfdata(intf);
18684104d13fSDmitry Torokhov 
18694104d13fSDmitry Torokhov 	/* Disassociate driver's struct with usb interface
18704104d13fSDmitry Torokhov 	 */
18714104d13fSDmitry Torokhov 	usb_set_intfdata(intf, NULL);
18724104d13fSDmitry Torokhov 	if (aiptek != NULL) {
18734104d13fSDmitry Torokhov 		/* Free & unhook everything from the system.
18744104d13fSDmitry Torokhov 		 */
18754104d13fSDmitry Torokhov 		usb_kill_urb(aiptek->urb);
18764104d13fSDmitry Torokhov 		input_unregister_device(aiptek->inputdev);
18774104d13fSDmitry Torokhov 		usb_free_urb(aiptek->urb);
1878997ea58eSDaniel Mack 		usb_free_coherent(interface_to_usbdev(intf),
18794104d13fSDmitry Torokhov 				  AIPTEK_PACKET_LENGTH,
18804104d13fSDmitry Torokhov 				  aiptek->data, aiptek->data_dma);
18814104d13fSDmitry Torokhov 		kfree(aiptek);
18824104d13fSDmitry Torokhov 	}
18834104d13fSDmitry Torokhov }
18844104d13fSDmitry Torokhov 
18854104d13fSDmitry Torokhov static struct usb_driver aiptek_driver = {
18864104d13fSDmitry Torokhov 	.name = "aiptek",
18874104d13fSDmitry Torokhov 	.probe = aiptek_probe,
18884104d13fSDmitry Torokhov 	.disconnect = aiptek_disconnect,
18894104d13fSDmitry Torokhov 	.id_table = aiptek_ids,
1890*f4e7a254SDmitry Torokhov 	.dev_groups = aiptek_dev_groups,
18914104d13fSDmitry Torokhov };
18924104d13fSDmitry Torokhov 
189308642e7cSGreg Kroah-Hartman module_usb_driver(aiptek_driver);
18944104d13fSDmitry Torokhov 
1895698c03b4SJulia Lawall MODULE_AUTHOR("Bryan W. Headley/Chris Atenasio/Cedric Brun/Rene van Paassen");
1896698c03b4SJulia Lawall MODULE_DESCRIPTION("Aiptek HyperPen USB Tablet Driver");
18974104d13fSDmitry Torokhov MODULE_LICENSE("GPL");
18984104d13fSDmitry Torokhov 
18994104d13fSDmitry Torokhov module_param(programmableDelay, int, 0);
19004104d13fSDmitry Torokhov MODULE_PARM_DESC(programmableDelay, "delay used during tablet programming");
19014104d13fSDmitry Torokhov module_param(jitterDelay, int, 0);
19024104d13fSDmitry Torokhov MODULE_PARM_DESC(jitterDelay, "stylus/mouse settlement delay");
1903