aiptek.c 61.9 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3
/*
 *  Native support for the Aiptek HyperPen USB Tablets
 *  (4000U/5000U/6000U/8000U/12000U)
4
 *
Linus Torvalds's avatar
Linus Torvalds committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
 *  Copyright (c) 2001      Chris Atenasio   <chris@crud.net>
 *  Copyright (c) 2002-2004 Bryan W. Headley <bwheadley@earthlink.net>
 *
 *  based on wacom.c by
 *     Vojtech Pavlik      <vojtech@suse.cz>
 *     Andreas Bach Aaen   <abach@stofanet.dk>
 *     Clifford Wolf       <clifford@clifford.at>
 *     Sam Mosel           <sam.mosel@computer.org>
 *     James E. Blair      <corvus@gnu.org>
 *     Daniel Egger        <egger@suse.de>
 *
 *  Many thanks to Oliver Kuechemann for his support.
 *
 *  ChangeLog:
 *      v0.1 - Initial release
 *      v0.2 - Hack to get around fake event 28's. (Bryan W. Headley)
 *      v0.3 - Make URB dynamic (Bryan W. Headley, Jun-8-2002)
 *             Released to Linux 2.4.19 and 2.5.x
 *      v0.4 - Rewrote substantial portions of the code to deal with
 *             corrected control sequences, timing, dynamic configuration,
 *             support of 6000U - 12000U, procfs, and macro key support
 *             (Jan-1-2003 - Feb-5-2003, Bryan W. Headley)
 *      v1.0 - Added support for diagnostic messages, count of messages
 *             received from URB - Mar-8-2003, Bryan W. Headley
 *      v1.1 - added support for tablet resolution, changed DV and proximity
 *             some corrections - Jun-22-2003, martin schneebacher
 *           - Added support for the sysfs interface, deprecating the
 *             procfs interface for 2.5.x kernel. Also added support for
 *             Wheel command. Bryan W. Headley July-15-2003.
34
 *      v1.2 - Reworked jitter timer as a kernel thread.
Linus Torvalds's avatar
Linus Torvalds committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
 *             Bryan W. Headley November-28-2003/Jan-10-2004.
 *      v1.3 - Repaired issue of kernel thread going nuts on single-processor
 *             machines, introduced programmableDelay as a command line
 *             parameter. Feb 7 2004, Bryan W. Headley.
 *      v1.4 - Re-wire jitter so it does not require a thread. Courtesy of
 *             Rene van Paassen. Added reporting of physical pointer device
 *             (e.g., stylus, mouse in reports 2, 3, 4, 5. We don't know
 *             for reports 1, 6.)
 *             what physical device reports for reports 1, 6.) Also enabled
 *             MOUSE and LENS tool button modes. Renamed "rubber" to "eraser".
 *             Feb 20, 2004, Bryan W. Headley.
 *      v1.5 - Added previousJitterable, so we don't do jitter delay when the
 *             user is holding a button down for periods of time.
 *
 * NOTE:
 *      This kernel driver is augmented by the "Aiptek" XFree86 input
 *      driver for your X server, as well as the Gaiptek GUI Front-end
52 53
 *      "Tablet Manager".
 *      These three products are highly interactive with one another,
Linus Torvalds's avatar
Linus Torvalds committed
54
 *      so therefore it's easier to document them all as one subsystem.
55
 *      Please visit the project's "home page", located at,
Linus Torvalds's avatar
Linus Torvalds committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
 *      http://aiptektablet.sourceforge.net.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#include <linux/jiffies.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/module.h>
#include <linux/init.h>
78
#include <linux/usb/input.h>
Linus Torvalds's avatar
Linus Torvalds committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
#include <asm/uaccess.h>
#include <asm/unaligned.h>

/*
 * Version Information
 */
#define DRIVER_VERSION "v1.5 (May-15-2004)"
#define DRIVER_AUTHOR  "Bryan W. Headley/Chris Atenasio"
#define DRIVER_DESC    "Aiptek HyperPen USB Tablet Driver (Linux 2.6.x)"

/*
 * Aiptek status packet:
 *
 * (returned as Report 1 - relative coordinates from mouse and stylus)
 *
 *        bit7  bit6  bit5  bit4  bit3  bit2  bit1  bit0
 * byte0   0     0     0     0     0     0     0     1
 * byte1   0     0     0     0     0    BS2   BS    Tip
 * byte2  X7    X6    X5    X4    X3    X2    X1    X0
 * byte3  Y7    Y6    Y5    Y4    Y3    Y2    Y1    Y0
 *
 * (returned as Report 2 - absolute coordinates from the stylus)
 *
 *        bit7  bit6  bit5  bit4  bit3  bit2  bit1  bit0
 * byte0   0     0     0     0     0     0     1     0
 * byte1  X7    X6    X5    X4    X3    X2    X1    X0
 * byte2  X15   X14   X13   X12   X11   X10   X9    X8
 * byte3  Y7    Y6    Y5    Y4    Y3    Y2    Y1    Y0
 * byte4  Y15   Y14   Y13   Y12   Y11   Y10   Y9    Y8
 * byte5   *     *     *    BS2   BS1   Tip   IR    DV
 * byte6  P7    P6    P5    P4    P3    P2    P1    P0
 * byte7  P15   P14   P13   P12   P11   P10   P9    P8
 *
 * (returned as Report 3 - absolute coordinates from the mouse)
 *
 *        bit7  bit6  bit5  bit4  bit3  bit2  bit1  bit0
115
 * byte0   0     0     0     0     0     0     1     1
Linus Torvalds's avatar
Linus Torvalds committed
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
 * byte1  X7    X6    X5    X4    X3    X2    X1    X0
 * byte2  X15   X14   X13   X12   X11   X10   X9    X8
 * byte3  Y7    Y6    Y5    Y4    Y3    Y2    Y1    Y0
 * byte4  Y15   Y14   Y13   Y12   Y11   Y10   Y9    Y8
 * byte5   *     *     *    BS2   BS1   Tip   IR    DV
 * byte6  P7    P6    P5    P4    P3    P2    P1    P0
 * byte7  P15   P14   P13   P12   P11   P10   P9    P8
 *
 * (returned as Report 4 - macrokeys from the stylus)
 *
 *        bit7  bit6  bit5  bit4  bit3  bit2  bit1  bit0
 * byte0   0     0     0     0     0     1     0     0
 * byte1   0     0     0    BS2   BS    Tip   IR    DV
 * byte2   0     0     0     0     0     0     1     0
 * byte3   0     0     0    K4    K3    K2    K1    K0
 * byte4  P7    P6    P5    P4    P3    P2    P1    P0
 * byte5  P15   P14   P13   P12   P11   P10   P9    P8
 *
 * (returned as Report 5 - macrokeys from the mouse)
 *
 *        bit7  bit6  bit5  bit4  bit3  bit2  bit1  bit0
137
 * byte0   0     0     0     0     0     1     0     1
Linus Torvalds's avatar
Linus Torvalds committed
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
 * byte1   0     0     0    BS2   BS    Tip   IR    DV
 * byte2   0     0     0     0     0     0     1     0
 * byte3   0     0     0    K4    K3    K2    K1    K0
 * byte4  P7    P6    P5    P4    P3    P2    P1    P0
 * byte5  P15   P14   P13   P12   P11   P10   P9    P8
 *
 * IR: In Range = Proximity on
 * DV = Data Valid
 * BS = Barrel Switch (as in, macro keys)
 * BS2 also referred to as Tablet Pick
 *
 * Command Summary:
 *
 * Use report_type CONTROL (3)
 * Use report_id   2
 *
 * Command/Data    Description     Return Bytes    Return Value
 * 0x10/0x00       SwitchToMouse       0
 * 0x10/0x01       SwitchToTablet      0
157
 * 0x18/0x04       SetResolution       0
Linus Torvalds's avatar
Linus Torvalds committed
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
 * 0x12/0xFF       AutoGainOn          0
 * 0x17/0x00       FilterOn            0
 * 0x01/0x00       GetXExtension       2           MaxX
 * 0x01/0x01       GetYExtension       2           MaxY
 * 0x02/0x00       GetModelCode        2           ModelCode = LOBYTE
 * 0x03/0x00       GetODMCode          2           ODMCode
 * 0x08/0x00       GetPressureLevels   2           =512
 * 0x04/0x00       GetFirmwareVersion  2           Firmware Version
 * 0x11/0x02       EnableMacroKeys     0
 *
 * To initialize the tablet:
 *
 * (1) Send Resolution500LPI (Command)
 * (2) Query for Model code (Option Report)
 * (3) Query for ODM code (Option Report)
 * (4) Query for firmware (Option Report)
 * (5) Query for GetXExtension (Option Report)
 * (6) Query for GetYExtension (Option Report)
 * (7) Query for GetPressureLevels (Option Report)
 * (8) SwitchToTablet for Absolute coordinates, or
 *     SwitchToMouse for Relative coordinates (Command)
 * (9) EnableMacroKeys (Command)
 * (10) FilterOn (Command)
 * (11) AutoGainOn (Command)
 *
 * (Step 9 can be omitted, but you'll then have no function keys.)
 */

#define USB_VENDOR_ID_AIPTEK				0x08ca
#define USB_REQ_GET_REPORT				0x01
#define USB_REQ_SET_REPORT				0x09

	/* PointerMode codes
	 */
#define AIPTEK_POINTER_ONLY_MOUSE_MODE			0
#define AIPTEK_POINTER_ONLY_STYLUS_MODE			1
#define AIPTEK_POINTER_EITHER_MODE			2

#define AIPTEK_POINTER_ALLOW_MOUSE_MODE(a)		\
	(a == AIPTEK_POINTER_ONLY_MOUSE_MODE ||		\
	 a == AIPTEK_POINTER_EITHER_MODE)
#define AIPTEK_POINTER_ALLOW_STYLUS_MODE(a)		\
	(a == AIPTEK_POINTER_ONLY_STYLUS_MODE ||	\
	 a == AIPTEK_POINTER_EITHER_MODE)

	/* CoordinateMode code
	 */
#define AIPTEK_COORDINATE_RELATIVE_MODE			0
#define AIPTEK_COORDINATE_ABSOLUTE_MODE			1

       /* XTilt and YTilt values
        */
#define AIPTEK_TILT_MIN					(-128)
#define AIPTEK_TILT_MAX					127
#define AIPTEK_TILT_DISABLE				(-10101)

	/* Wheel values
	 */
#define AIPTEK_WHEEL_MIN				0
#define AIPTEK_WHEEL_MAX				1024
#define AIPTEK_WHEEL_DISABLE				(-10101)

	/* ToolCode values, which BTW are 0x140 .. 0x14f
221 222
	 * We have things set up such that if the tool button has changed,
	 * the tools get reset.
Linus Torvalds's avatar
Linus Torvalds committed
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
	 */
	/* toolMode codes
	 */
#define AIPTEK_TOOL_BUTTON_PEN_MODE			BTN_TOOL_PEN
#define AIPTEK_TOOL_BUTTON_PEN_MODE			BTN_TOOL_PEN
#define AIPTEK_TOOL_BUTTON_PENCIL_MODE			BTN_TOOL_PENCIL
#define AIPTEK_TOOL_BUTTON_BRUSH_MODE			BTN_TOOL_BRUSH
#define AIPTEK_TOOL_BUTTON_AIRBRUSH_MODE		BTN_TOOL_AIRBRUSH
#define AIPTEK_TOOL_BUTTON_ERASER_MODE			BTN_TOOL_RUBBER
#define AIPTEK_TOOL_BUTTON_MOUSE_MODE			BTN_TOOL_MOUSE
#define AIPTEK_TOOL_BUTTON_LENS_MODE			BTN_TOOL_LENS

	/* Diagnostic message codes
	 */
#define AIPTEK_DIAGNOSTIC_NA				0
#define AIPTEK_DIAGNOSTIC_SENDING_RELATIVE_IN_ABSOLUTE	1
#define AIPTEK_DIAGNOSTIC_SENDING_ABSOLUTE_IN_RELATIVE	2
#define AIPTEK_DIAGNOSTIC_TOOL_DISALLOWED		3

242
	/* Time to wait (in ms) to help mask hand jittering
Linus Torvalds's avatar
Linus Torvalds committed
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
	 * when pressing the stylus buttons.
	 */
#define AIPTEK_JITTER_DELAY_DEFAULT			50

	/* Time to wait (in ms) in-between sending the tablet
	 * a command and beginning the process of reading the return
	 * sequence from the tablet.
	 */
#define AIPTEK_PROGRAMMABLE_DELAY_25		25
#define AIPTEK_PROGRAMMABLE_DELAY_50		50
#define AIPTEK_PROGRAMMABLE_DELAY_100		100
#define AIPTEK_PROGRAMMABLE_DELAY_200		200
#define AIPTEK_PROGRAMMABLE_DELAY_300		300
#define AIPTEK_PROGRAMMABLE_DELAY_400		400
#define AIPTEK_PROGRAMMABLE_DELAY_DEFAULT	AIPTEK_PROGRAMMABLE_DELAY_400

	/* Mouse button programming
	 */
261 262 263
#define AIPTEK_MOUSE_LEFT_BUTTON		0x04
#define AIPTEK_MOUSE_RIGHT_BUTTON		0x08
#define AIPTEK_MOUSE_MIDDLE_BUTTON		0x10
Linus Torvalds's avatar
Linus Torvalds committed
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309

	/* Stylus button programming
	 */
#define AIPTEK_STYLUS_LOWER_BUTTON		0x08
#define AIPTEK_STYLUS_UPPER_BUTTON		0x10

	/* Length of incoming packet from the tablet
	 */
#define AIPTEK_PACKET_LENGTH			8

	/* We report in EV_MISC both the proximity and
	 * whether the report came from the stylus, tablet mouse
	 * or "unknown" -- Unknown when the tablet is in relative
	 * mode, because we only get report 1's.
	 */
#define AIPTEK_REPORT_TOOL_UNKNOWN		0x10
#define AIPTEK_REPORT_TOOL_STYLUS		0x20
#define AIPTEK_REPORT_TOOL_MOUSE		0x40

static int programmableDelay = AIPTEK_PROGRAMMABLE_DELAY_DEFAULT;
static int jitterDelay = AIPTEK_JITTER_DELAY_DEFAULT;

struct aiptek_features {
	int odmCode;		/* Tablet manufacturer code       */
	int modelCode;		/* Tablet model code (not unique) */
	int firmwareCode;	/* prom/eeprom version            */
	char usbPath[64 + 1];	/* device's physical usb path     */
};

struct aiptek_settings {
	int pointerMode;	/* stylus-, mouse-only or either */
	int coordinateMode;	/* absolute/relative coords      */
	int toolMode;		/* pen, pencil, brush, etc. tool */
	int xTilt;		/* synthetic xTilt amount        */
	int yTilt;		/* synthetic yTilt amount        */
	int wheel;		/* synthetic wheel amount        */
	int stylusButtonUpper;	/* stylus upper btn delivers...  */
	int stylusButtonLower;	/* stylus lower btn delivers...  */
	int mouseButtonLeft;	/* mouse left btn delivers...    */
	int mouseButtonMiddle;	/* mouse middle btn delivers...  */
	int mouseButtonRight;	/* mouse right btn delivers...   */
	int programmableDelay;	/* delay for tablet programming  */
	int jitterDelay;	/* delay for hand jittering      */
};

struct aiptek {
310
	struct input_dev *inputdev;		/* input device struct           */
Linus Torvalds's avatar
Linus Torvalds committed
311 312 313 314 315 316 317 318 319 320 321 322
	struct usb_device *usbdev;		/* usb device struct             */
	struct urb *urb;			/* urb for incoming reports      */
	dma_addr_t data_dma;			/* our dma stuffage              */
	struct aiptek_features features;	/* tablet's array of features    */
	struct aiptek_settings curSetting;	/* tablet's current programmable */
	struct aiptek_settings newSetting;	/* ... and new param settings    */
	unsigned int ifnum;			/* interface number for IO       */
	int diagnostic;				/* tablet diagnostic codes       */
	unsigned long eventCount;		/* event count                   */
	int inDelay;				/* jitter: in jitter delay?      */
	unsigned long endDelay;			/* jitter: time when delay ends  */
	int previousJitterable;			/* jitterable prev value     */
323 324 325

	int lastMacro;				/* macro key to reset            */
	int previousToolMode;			/* pen, pencil, brush, etc. tool */
Linus Torvalds's avatar
Linus Torvalds committed
326 327 328
	unsigned char *data;			/* incoming packet data          */
};

329 330 331 332 333 334 335 336 337 338 339 340 341
static const int eventTypes[] = {
        EV_KEY, EV_ABS, EV_REL, EV_MSC,
};

static const int absEvents[] = {
        ABS_X, ABS_Y, ABS_PRESSURE, ABS_TILT_X, ABS_TILT_Y,
        ABS_WHEEL, ABS_MISC,
};

static const int relEvents[] = {
        REL_X, REL_Y, REL_WHEEL,
};

342 343 344 345 346 347 348
static const int buttonEvents[] = {
	BTN_LEFT, BTN_RIGHT, BTN_MIDDLE,
	BTN_TOOL_PEN, BTN_TOOL_RUBBER, BTN_TOOL_PENCIL, BTN_TOOL_AIRBRUSH,
	BTN_TOOL_BRUSH, BTN_TOOL_MOUSE, BTN_TOOL_LENS, BTN_TOUCH,
	BTN_STYLUS, BTN_STYLUS2,
};

Linus Torvalds's avatar
Linus Torvalds committed
349 350 351 352 353
/*
 * Permit easy lookup of keyboard events to send, versus
 * the bitmap which comes from the tablet. This hides the
 * issue that the F_keys are not sequentially numbered.
 */
354
static const int macroKeyEvents[] = {
Linus Torvalds's avatar
Linus Torvalds committed
355 356 357 358 359 360 361 362
	KEY_ESC, KEY_F1, KEY_F2, KEY_F3, KEY_F4, KEY_F5,
	KEY_F6, KEY_F7, KEY_F8, KEY_F9, KEY_F10, KEY_F11,
	KEY_F12, KEY_F13, KEY_F14, KEY_F15, KEY_F16, KEY_F17,
	KEY_F18, KEY_F19, KEY_F20, KEY_F21, KEY_F22, KEY_F23,
	KEY_F24, KEY_STOP, KEY_AGAIN, KEY_PROPS, KEY_UNDO,
	KEY_FRONT, KEY_COPY, KEY_OPEN, KEY_PASTE, 0
};

363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
/***********************************************************************
 * Map values to strings and back. Every map shoudl have the following
 * as its last element: { NULL, AIPTEK_INVALID_VALUE }.
 */
#define AIPTEK_INVALID_VALUE	-1

struct aiptek_map {
	const char *string;
	int value;
};

static int map_str_to_val(const struct aiptek_map *map, const char *str, size_t count)
{
	const struct aiptek_map *p;

378 379 380
	if (str[count - 1] == '\n')
		count--;

381
	for (p = map; p->string; p++)
382
	        if (!strncmp(str, p->string, count))
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
			return p->value;

	return AIPTEK_INVALID_VALUE;
}

static const char *map_val_to_str(const struct aiptek_map *map, int val)
{
	const struct aiptek_map *p;

	for (p = map; p->value != AIPTEK_INVALID_VALUE; p++)
		if (val == p->value)
			return p->string;

	return "unknown";
}

Linus Torvalds's avatar
Linus Torvalds committed
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
/***********************************************************************
 * aiptek_irq can receive one of six potential reports.
 * The documentation for each is in the body of the function.
 *
 * The tablet reports on several attributes per invocation of
 * aiptek_irq. Because the Linux Input Event system allows the
 * transmission of ONE attribute per input_report_xxx() call,
 * collation has to be done on the other end to reconstitute
 * a complete tablet report. Further, the number of Input Event reports
 * submitted varies, depending on what USB report type, and circumstance.
 * To deal with this, EV_MSC is used to indicate an 'end-of-report'
 * message. This has been an undocumented convention understood by the kernel
 * tablet driver and clients such as gpm and XFree86's tablet drivers.
 *
 * Of the information received from the tablet, the one piece I
 * cannot transmit is the proximity bit (without resorting to an EV_MSC
 * convention above.) I therefore have taken over REL_MISC and ABS_MISC
 * (for relative and absolute reports, respectively) for communicating
 * Proximity. Why two events? I thought it interesting to know if the
418
 * Proximity event occurred while the tablet was in absolute or relative
Linus Torvalds's avatar
Linus Torvalds committed
419
 * mode.
420 421 422
 * Update: REL_MISC proved not to be such a good idea. With REL_MISC you
 * get an event transmitted each time. ABS_MISC works better, since it
 * can be set and re-set. Thus, only using ABS_MISC from now on.
Linus Torvalds's avatar
Linus Torvalds committed
423 424 425 426 427 428 429 430 431 432
 *
 * Other tablets use the notion of a certain minimum stylus pressure
 * to infer proximity. While that could have been done, that is yet
 * another 'by convention' behavior, the documentation for which
 * would be spread between two (or more) pieces of software.
 *
 * EV_MSC usage was terminated for this purpose in Linux 2.5.x, and
 * replaced with the input_sync() method (which emits EV_SYN.)
 */

433
static void aiptek_irq(struct urb *urb)
Linus Torvalds's avatar
Linus Torvalds committed
434 435 436
{
	struct aiptek *aiptek = urb->context;
	unsigned char *data = aiptek->data;
437
	struct input_dev *inputdev = aiptek->inputdev;
Linus Torvalds's avatar
Linus Torvalds committed
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
	int jitterable = 0;
	int retval, macro, x, y, z, left, right, middle, p, dv, tip, bs, pck;

	switch (urb->status) {
	case 0:
		/* Success */
		break;

	case -ECONNRESET:
	case -ENOENT:
	case -ESHUTDOWN:
		/* This urb is terminated, clean up */
		dbg("%s - urb shutting down with status: %d",
		    __FUNCTION__, urb->status);
		return;

	default:
		dbg("%s - nonzero urb status received: %d",
		    __FUNCTION__, urb->status);
		goto exit;
	}

	/* See if we are in a delay loop -- throw out report if true.
	 */
	if (aiptek->inDelay == 1 && time_after(aiptek->endDelay, jiffies)) {
		goto exit;
	}

	aiptek->inDelay = 0;
	aiptek->eventCount++;

	/* Report 1 delivers relative coordinates with either a stylus
	 * or the mouse. You do not know, however, which input
	 * tool generated the event.
	 */
	if (data[0] == 1) {
		if (aiptek->curSetting.coordinateMode ==
		    AIPTEK_COORDINATE_ABSOLUTE_MODE) {
			aiptek->diagnostic =
			    AIPTEK_DIAGNOSTIC_SENDING_RELATIVE_IN_ABSOLUTE;
		} else {
479 480
			x = (signed char) data[2];
			y = (signed char) data[3];
Linus Torvalds's avatar
Linus Torvalds committed
481 482 483 484 485 486 487 488

			/* jitterable keeps track of whether any button has been pressed.
			 * We're also using it to remap the physical mouse button mask
			 * to pseudo-settings. (We don't specifically care about it's
			 * value after moving/transposing mouse button bitmasks, except
			 * that a non-zero value indicates that one or more
			 * mouse button was pressed.)
			 */
489
			jitterable = data[1] & 0x07;
Linus Torvalds's avatar
Linus Torvalds committed
490

491 492 493
			left = (data[1] & aiptek->curSetting.mouseButtonLeft >> 2) != 0 ? 1 : 0;
			right = (data[1] & aiptek->curSetting.mouseButtonRight >> 2) != 0 ? 1 : 0;
			middle = (data[1] & aiptek->curSetting.mouseButtonMiddle >> 2) != 0 ? 1 : 0;
Linus Torvalds's avatar
Linus Torvalds committed
494 495 496 497

			input_report_key(inputdev, BTN_LEFT, left);
			input_report_key(inputdev, BTN_MIDDLE, middle);
			input_report_key(inputdev, BTN_RIGHT, right);
498 499 500

			input_report_abs(inputdev, ABS_MISC,
					 1 | AIPTEK_REPORT_TOOL_UNKNOWN);
Linus Torvalds's avatar
Linus Torvalds committed
501 502 503 504 505 506 507 508 509 510 511
			input_report_rel(inputdev, REL_X, x);
			input_report_rel(inputdev, REL_Y, y);

			/* Wheel support is in the form of a single-event
			 * firing.
			 */
			if (aiptek->curSetting.wheel != AIPTEK_WHEEL_DISABLE) {
				input_report_rel(inputdev, REL_WHEEL,
						 aiptek->curSetting.wheel);
				aiptek->curSetting.wheel = AIPTEK_WHEEL_DISABLE;
			}
512 513 514 515 516
			if (aiptek->lastMacro != -1) {
			        input_report_key(inputdev,
						 macroKeyEvents[aiptek->lastMacro], 0);
				aiptek->lastMacro = -1;
			}
Linus Torvalds's avatar
Linus Torvalds committed
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
			input_sync(inputdev);
		}
	}
	/* Report 2 is delivered only by the stylus, and delivers
	 * absolute coordinates.
	 */
	else if (data[0] == 2) {
		if (aiptek->curSetting.coordinateMode == AIPTEK_COORDINATE_RELATIVE_MODE) {
			aiptek->diagnostic = AIPTEK_DIAGNOSTIC_SENDING_ABSOLUTE_IN_RELATIVE;
		} else if (!AIPTEK_POINTER_ALLOW_STYLUS_MODE
			    (aiptek->curSetting.pointerMode)) {
				aiptek->diagnostic = AIPTEK_DIAGNOSTIC_TOOL_DISALLOWED;
		} else {
			x = le16_to_cpu(get_unaligned((__le16 *) (data + 1)));
			y = le16_to_cpu(get_unaligned((__le16 *) (data + 3)));
			z = le16_to_cpu(get_unaligned((__le16 *) (data + 6)));

534 535
			dv = (data[5] & 0x01) != 0 ? 1 : 0;
			p = (data[5] & 0x02) != 0 ? 1 : 0;
Linus Torvalds's avatar
Linus Torvalds committed
536 537 538 539 540 541 542 543 544 545 546 547 548 549
			tip = (data[5] & 0x04) != 0 ? 1 : 0;

			/* Use jitterable to re-arrange button masks
			 */
			jitterable = data[5] & 0x18;

			bs = (data[5] & aiptek->curSetting.stylusButtonLower) != 0 ? 1 : 0;
			pck = (data[5] & aiptek->curSetting.stylusButtonUpper) != 0 ? 1 : 0;

			/* dv indicates 'data valid' (e.g., the tablet is in sync
			 * and has delivered a "correct" report) We will ignore
			 * all 'bad' reports...
			 */
			if (dv != 0) {
550 551
				/* If the selected tool changed, reset the old
				 * tool key, and set the new one.
Linus Torvalds's avatar
Linus Torvalds committed
552
				 */
553 554 555 556
				if (aiptek->previousToolMode !=
				    aiptek->curSetting.toolMode) {
				        input_report_key(inputdev,
							 aiptek->previousToolMode, 0);
Linus Torvalds's avatar
Linus Torvalds committed
557
					input_report_key(inputdev,
558
							 aiptek->curSetting.toolMode,
Linus Torvalds's avatar
Linus Torvalds committed
559
							 1);
560 561
					aiptek->previousToolMode =
					          aiptek->curSetting.toolMode;
Linus Torvalds's avatar
Linus Torvalds committed
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
				}

				if (p != 0) {
					input_report_abs(inputdev, ABS_X, x);
					input_report_abs(inputdev, ABS_Y, y);
					input_report_abs(inputdev, ABS_PRESSURE, z);

					input_report_key(inputdev, BTN_TOUCH, tip);
					input_report_key(inputdev, BTN_STYLUS, bs);
					input_report_key(inputdev, BTN_STYLUS2, pck);

					if (aiptek->curSetting.xTilt !=
					    AIPTEK_TILT_DISABLE) {
						input_report_abs(inputdev,
								 ABS_TILT_X,
								 aiptek->curSetting.xTilt);
					}
					if (aiptek->curSetting.yTilt != AIPTEK_TILT_DISABLE) {
						input_report_abs(inputdev,
								 ABS_TILT_Y,
								 aiptek->curSetting.yTilt);
					}

					/* Wheel support is in the form of a single-event
					 * firing.
					 */
					if (aiptek->curSetting.wheel !=
					    AIPTEK_WHEEL_DISABLE) {
						input_report_abs(inputdev,
								 ABS_WHEEL,
								 aiptek->curSetting.wheel);
						aiptek->curSetting.wheel = AIPTEK_WHEEL_DISABLE;
					}
				}
				input_report_abs(inputdev, ABS_MISC, p | AIPTEK_REPORT_TOOL_STYLUS);
597 598 599 600 601
				if (aiptek->lastMacro != -1) {
			                input_report_key(inputdev,
							 macroKeyEvents[aiptek->lastMacro], 0);
					aiptek->lastMacro = -1;
				}
Linus Torvalds's avatar
Linus Torvalds committed
602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
				input_sync(inputdev);
			}
		}
	}
	/* Report 3's come from the mouse in absolute mode.
	 */
	else if (data[0] == 3) {
		if (aiptek->curSetting.coordinateMode == AIPTEK_COORDINATE_RELATIVE_MODE) {
			aiptek->diagnostic = AIPTEK_DIAGNOSTIC_SENDING_ABSOLUTE_IN_RELATIVE;
		} else if (!AIPTEK_POINTER_ALLOW_MOUSE_MODE
			(aiptek->curSetting.pointerMode)) {
			aiptek->diagnostic = AIPTEK_DIAGNOSTIC_TOOL_DISALLOWED;
		} else {
			x = le16_to_cpu(get_unaligned((__le16 *) (data + 1)));
			y = le16_to_cpu(get_unaligned((__le16 *) (data + 3)));

			jitterable = data[5] & 0x1c;

620 621
			dv = (data[5] & 0x01) != 0 ? 1 : 0;
			p = (data[5] & 0x02) != 0 ? 1 : 0;
Linus Torvalds's avatar
Linus Torvalds committed
622 623 624 625 626
			left = (data[5] & aiptek->curSetting.mouseButtonLeft) != 0 ? 1 : 0;
			right = (data[5] & aiptek->curSetting.mouseButtonRight) != 0 ? 1 : 0;
			middle = (data[5] & aiptek->curSetting.mouseButtonMiddle) != 0 ? 1 : 0;

			if (dv != 0) {
627 628
				/* If the selected tool changed, reset the old
				 * tool key, and set the new one.
Linus Torvalds's avatar
Linus Torvalds committed
629
				 */
630 631 632 633
				if (aiptek->previousToolMode !=
				    aiptek->curSetting.toolMode) {
				        input_report_key(inputdev,
							 aiptek->previousToolMode, 0);
Linus Torvalds's avatar
Linus Torvalds committed
634
					input_report_key(inputdev,
635
							 aiptek->curSetting.toolMode,
Linus Torvalds's avatar
Linus Torvalds committed
636
							 1);
637 638
					aiptek->previousToolMode =
					          aiptek->curSetting.toolMode;
Linus Torvalds's avatar
Linus Torvalds committed
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
				}

				if (p != 0) {
					input_report_abs(inputdev, ABS_X, x);
					input_report_abs(inputdev, ABS_Y, y);

					input_report_key(inputdev, BTN_LEFT, left);
					input_report_key(inputdev, BTN_MIDDLE, middle);
					input_report_key(inputdev, BTN_RIGHT, right);

					/* Wheel support is in the form of a single-event
					 * firing.
					 */
					if (aiptek->curSetting.wheel != AIPTEK_WHEEL_DISABLE) {
						input_report_abs(inputdev,
								 ABS_WHEEL,
								 aiptek->curSetting.wheel);
						aiptek->curSetting.wheel = AIPTEK_WHEEL_DISABLE;
					}
				}
659
				input_report_abs(inputdev, ABS_MISC, p | AIPTEK_REPORT_TOOL_MOUSE);
660 661 662 663 664
				if (aiptek->lastMacro != -1) {
			                input_report_key(inputdev,
							 macroKeyEvents[aiptek->lastMacro], 0);
				        aiptek->lastMacro = -1;
				}
Linus Torvalds's avatar
Linus Torvalds committed
665 666 667 668 669 670 671 672 673
				input_sync(inputdev);
			}
		}
	}
	/* Report 4s come from the macro keys when pressed by stylus
	 */
	else if (data[0] == 4) {
		jitterable = data[1] & 0x18;

674 675
		dv = (data[1] & 0x01) != 0 ? 1 : 0;
		p = (data[1] & 0x02) != 0 ? 1 : 0;
Linus Torvalds's avatar
Linus Torvalds committed
676 677 678 679
		tip = (data[1] & 0x04) != 0 ? 1 : 0;
		bs = (data[1] & aiptek->curSetting.stylusButtonLower) != 0 ? 1 : 0;
		pck = (data[1] & aiptek->curSetting.stylusButtonUpper) != 0 ? 1 : 0;

680
		macro = dv && p && tip && !(data[3] & 1) ? (data[3] >> 1) : -1;
Linus Torvalds's avatar
Linus Torvalds committed
681 682
		z = le16_to_cpu(get_unaligned((__le16 *) (data + 4)));

683
		if (dv) {
684 685
		        /* If the selected tool changed, reset the old
			 * tool key, and set the new one.
Linus Torvalds's avatar
Linus Torvalds committed
686
			 */
687 688 689 690
		        if (aiptek->previousToolMode !=
			    aiptek->curSetting.toolMode) {
			        input_report_key(inputdev,
						 aiptek->previousToolMode, 0);
Linus Torvalds's avatar
Linus Torvalds committed
691
				input_report_key(inputdev,
692
						 aiptek->curSetting.toolMode,
Linus Torvalds's avatar
Linus Torvalds committed
693
						 1);
694 695
				aiptek->previousToolMode =
				        aiptek->curSetting.toolMode;
Linus Torvalds's avatar
Linus Torvalds committed
696
			}
697
		}
Linus Torvalds's avatar
Linus Torvalds committed
698

699 700 701 702
		if (aiptek->lastMacro != -1 && aiptek->lastMacro != macro) {
		        input_report_key(inputdev, macroKeyEvents[aiptek->lastMacro], 0);
			aiptek->lastMacro = -1;
		}
Linus Torvalds's avatar
Linus Torvalds committed
703

704 705 706
		if (macro != -1 && macro != aiptek->lastMacro) {
			input_report_key(inputdev, macroKeyEvents[macro], 1);
			aiptek->lastMacro = macro;
Linus Torvalds's avatar
Linus Torvalds committed
707
		}
708 709 710
		input_report_abs(inputdev, ABS_MISC,
				 p | AIPTEK_REPORT_TOOL_STYLUS);
		input_sync(inputdev);
Linus Torvalds's avatar
Linus Torvalds committed
711 712 713 714 715 716
	}
	/* Report 5s come from the macro keys when pressed by mouse
	 */
	else if (data[0] == 5) {
		jitterable = data[1] & 0x1c;

717 718
		dv = (data[1] & 0x01) != 0 ? 1 : 0;
		p = (data[1] & 0x02) != 0 ? 1 : 0;
Linus Torvalds's avatar
Linus Torvalds committed
719 720 721
		left = (data[1]& aiptek->curSetting.mouseButtonLeft) != 0 ? 1 : 0;
		right = (data[1] & aiptek->curSetting.mouseButtonRight) != 0 ? 1 : 0;
		middle = (data[1] & aiptek->curSetting.mouseButtonMiddle) != 0 ? 1 : 0;
722
		macro = dv && p && left && !(data[3] & 1) ? (data[3] >> 1) : 0;
Linus Torvalds's avatar
Linus Torvalds committed
723

724
		if (dv) {
725 726
		        /* If the selected tool changed, reset the old
			 * tool key, and set the new one.
Linus Torvalds's avatar
Linus Torvalds committed
727
			 */
728 729
		        if (aiptek->previousToolMode !=
			    aiptek->curSetting.toolMode) {
730
		                input_report_key(inputdev,
731
						 aiptek->previousToolMode, 0);
732 733 734
			        input_report_key(inputdev,
						 aiptek->curSetting.toolMode, 1);
			        aiptek->previousToolMode = aiptek->curSetting.toolMode;
Linus Torvalds's avatar
Linus Torvalds committed
735
			}
736
		}
Linus Torvalds's avatar
Linus Torvalds committed
737

738 739 740 741
		if (aiptek->lastMacro != -1 && aiptek->lastMacro != macro) {
		        input_report_key(inputdev, macroKeyEvents[aiptek->lastMacro], 0);
			aiptek->lastMacro = -1;
		}
Linus Torvalds's avatar
Linus Torvalds committed
742

743
		if (macro != -1 && macro != aiptek->lastMacro) {
Linus Torvalds's avatar
Linus Torvalds committed
744
			input_report_key(inputdev, macroKeyEvents[macro], 1);
745
			aiptek->lastMacro = macro;
Linus Torvalds's avatar
Linus Torvalds committed
746
		}
747 748 749 750

		input_report_abs(inputdev, ABS_MISC,
				 p | AIPTEK_REPORT_TOOL_MOUSE);
		input_sync(inputdev);
Linus Torvalds's avatar
Linus Torvalds committed
751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768
	}
	/* We have no idea which tool can generate a report 6. Theoretically,
	 * neither need to, having been given reports 4 & 5 for such use.
	 * However, report 6 is the 'official-looking' report for macroKeys;
	 * reports 4 & 5 supposively are used to support unnamed, unknown
	 * hat switches (which just so happen to be the macroKeys.)
	 */
	else if (data[0] == 6) {
		macro = le16_to_cpu(get_unaligned((__le16 *) (data + 1)));
		if (macro > 0) {
			input_report_key(inputdev, macroKeyEvents[macro - 1],
					 0);
		}
		if (macro < 25) {
			input_report_key(inputdev, macroKeyEvents[macro + 1],
					 0);
		}

769 770 771 772 773 774 775
		/* If the selected tool changed, reset the old
		   tool key, and set the new one.
		*/
		if (aiptek->previousToolMode !=
		    aiptek->curSetting.toolMode) {
		        input_report_key(inputdev,
					 aiptek->previousToolMode, 0);
Linus Torvalds's avatar
Linus Torvalds committed
776
			input_report_key(inputdev,
777 778 779 780
					 aiptek->curSetting.toolMode,
					 1);
			aiptek->previousToolMode =
				aiptek->curSetting.toolMode;
Linus Torvalds's avatar
Linus Torvalds committed
781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823
		}

		input_report_key(inputdev, macroKeyEvents[macro], 1);
		input_report_abs(inputdev, ABS_MISC,
				 1 | AIPTEK_REPORT_TOOL_UNKNOWN);
		input_sync(inputdev);
	} else {
		dbg("Unknown report %d", data[0]);
	}

	/* Jitter may occur when the user presses a button on the stlyus
	 * or the mouse. What we do to prevent that is wait 'x' milliseconds
	 * following a 'jitterable' event, which should give the hand some time
	 * stabilize itself.
	 *
	 * We just introduced aiptek->previousJitterable to carry forth the
	 * notion that jitter occurs when the button state changes from on to off:
	 * a person drawing, holding a button down is not subject to jittering.
	 * With that in mind, changing from upper button depressed to lower button
	 * WILL transition through a jitter delay.
	 */

	if (aiptek->previousJitterable != jitterable &&
	    aiptek->curSetting.jitterDelay != 0 && aiptek->inDelay != 1) {
		aiptek->endDelay = jiffies +
		    ((aiptek->curSetting.jitterDelay * HZ) / 1000);
		aiptek->inDelay = 1;
	}
	aiptek->previousJitterable = jitterable;

exit:
	retval = usb_submit_urb(urb, GFP_ATOMIC);
	if (retval != 0) {
		err("%s - usb_submit_urb failed with result %d",
		    __FUNCTION__, retval);
	}
}

/***********************************************************************
 * These are the USB id's known so far. We do not identify them to
 * specific Aiptek model numbers, because there has been overlaps,
 * use, and reuse of id's in existing models. Certain models have
 * been known to use more than one ID, indicative perhaps of
824
 * manufacturing revisions. In any event, we consider these
Linus Torvalds's avatar
Linus Torvalds committed
825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844
 * IDs to not be model-specific nor unique.
 */
static const struct usb_device_id aiptek_ids[] = {
	{USB_DEVICE(USB_VENDOR_ID_AIPTEK, 0x01)},
	{USB_DEVICE(USB_VENDOR_ID_AIPTEK, 0x10)},
	{USB_DEVICE(USB_VENDOR_ID_AIPTEK, 0x20)},
	{USB_DEVICE(USB_VENDOR_ID_AIPTEK, 0x21)},
	{USB_DEVICE(USB_VENDOR_ID_AIPTEK, 0x22)},
	{USB_DEVICE(USB_VENDOR_ID_AIPTEK, 0x23)},
	{USB_DEVICE(USB_VENDOR_ID_AIPTEK, 0x24)},
	{}
};

MODULE_DEVICE_TABLE(usb, aiptek_ids);

/***********************************************************************
 * Open an instance of the tablet driver.
 */
static int aiptek_open(struct input_dev *inputdev)
{
845
	struct aiptek *aiptek = input_get_drvdata(inputdev);
Linus Torvalds's avatar
Linus Torvalds committed
846 847

	aiptek->urb->dev = aiptek->usbdev;
848
	if (usb_submit_urb(aiptek->urb, GFP_KERNEL) != 0)
Linus Torvalds's avatar
Linus Torvalds committed
849 850 851 852 853 854 855 856 857 858
		return -EIO;

	return 0;
}

/***********************************************************************
 * Close an instance of the tablet driver.
 */
static void aiptek_close(struct input_dev *inputdev)
{
859
	struct aiptek *aiptek = input_get_drvdata(inputdev);
Linus Torvalds's avatar
Linus Torvalds committed
860

861
	usb_kill_urb(aiptek->urb);
Linus Torvalds's avatar
Linus Torvalds committed
862 863 864
}

/***********************************************************************
865
 * aiptek_set_report and aiptek_get_report() are borrowed from Linux 2.4.x,
Linus Torvalds's avatar
Linus Torvalds committed
866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987
 * where they were known as usb_set_report and usb_get_report.
 */
static int
aiptek_set_report(struct aiptek *aiptek,
		  unsigned char report_type,
		  unsigned char report_id, void *buffer, int size)
{
	return usb_control_msg(aiptek->usbdev,
			       usb_sndctrlpipe(aiptek->usbdev, 0),
			       USB_REQ_SET_REPORT,
			       USB_TYPE_CLASS | USB_RECIP_INTERFACE |
			       USB_DIR_OUT, (report_type << 8) + report_id,
			       aiptek->ifnum, buffer, size, 5000);
}

static int
aiptek_get_report(struct aiptek *aiptek,
		  unsigned char report_type,
		  unsigned char report_id, void *buffer, int size)
{
	return usb_control_msg(aiptek->usbdev,
			       usb_rcvctrlpipe(aiptek->usbdev, 0),
			       USB_REQ_GET_REPORT,
			       USB_TYPE_CLASS | USB_RECIP_INTERFACE |
			       USB_DIR_IN, (report_type << 8) + report_id,
			       aiptek->ifnum, buffer, size, 5000);
}

/***********************************************************************
 * Send a command to the tablet.
 */
static int
aiptek_command(struct aiptek *aiptek, unsigned char command, unsigned char data)
{
	const int sizeof_buf = 3 * sizeof(u8);
	int ret;
	u8 *buf;

	buf = kmalloc(sizeof_buf, GFP_KERNEL);
	if (!buf)
		return -ENOMEM;

	buf[0] = 2;
	buf[1] = command;
	buf[2] = data;

	if ((ret =
	     aiptek_set_report(aiptek, 3, 2, buf, sizeof_buf)) != sizeof_buf) {
		dbg("aiptek_program: failed, tried to send: 0x%02x 0x%02x",
		    command, data);
	}
	kfree(buf);
	return ret < 0 ? ret : 0;
}

/***********************************************************************
 * Retrieve information from the tablet. Querying info is defined as first
 * sending the {command,data} sequence as a command, followed by a wait
 * (aka, "programmaticDelay") and then a "read" request.
 */
static int
aiptek_query(struct aiptek *aiptek, unsigned char command, unsigned char data)
{
	const int sizeof_buf = 3 * sizeof(u8);
	int ret;
	u8 *buf;

	buf = kmalloc(sizeof_buf, GFP_KERNEL);
	if (!buf)
		return -ENOMEM;

	buf[0] = 2;
	buf[1] = command;
	buf[2] = data;

	if (aiptek_command(aiptek, command, data) != 0) {
		kfree(buf);
		return -EIO;
	}
	msleep(aiptek->curSetting.programmableDelay);

	if ((ret =
	     aiptek_get_report(aiptek, 3, 2, buf, sizeof_buf)) != sizeof_buf) {
		dbg("aiptek_query failed: returned 0x%02x 0x%02x 0x%02x",
		    buf[0], buf[1], buf[2]);
		ret = -EIO;
	} else {
		ret = le16_to_cpu(get_unaligned((__le16 *) (buf + 1)));
	}
	kfree(buf);
	return ret;
}

/***********************************************************************
 * Program the tablet into either absolute or relative mode.
 * We also get information about the tablet's size.
 */
static int aiptek_program_tablet(struct aiptek *aiptek)
{
	int ret;
	/* Execute Resolution500LPI */
	if ((ret = aiptek_command(aiptek, 0x18, 0x04)) < 0)
		return ret;

	/* Query getModelCode */
	if ((ret = aiptek_query(aiptek, 0x02, 0x00)) < 0)
		return ret;
	aiptek->features.modelCode = ret & 0xff;

	/* Query getODMCode */
	if ((ret = aiptek_query(aiptek, 0x03, 0x00)) < 0)
		return ret;
	aiptek->features.odmCode = ret;

	/* Query getFirmwareCode */
	if ((ret = aiptek_query(aiptek, 0x04, 0x00)) < 0)
		return ret;
	aiptek->features.firmwareCode = ret;

	/* Query getXextension */
	if ((ret = aiptek_query(aiptek, 0x01, 0x00)) < 0)
		return ret;
988 989
	aiptek->inputdev->absmin[ABS_X] = 0;
	aiptek->inputdev->absmax[ABS_X] = ret - 1;
Linus Torvalds's avatar
Linus Torvalds committed
990 991 992 993

	/* Query getYextension */
	if ((ret = aiptek_query(aiptek, 0x01, 0x01)) < 0)
		return ret;
994 995
	aiptek->inputdev->absmin[ABS_Y] = 0;
	aiptek->inputdev->absmax[ABS_Y] = ret - 1;
Linus Torvalds's avatar
Linus Torvalds committed
996 997 998 999

	/* Query getPressureLevels */
	if ((ret = aiptek_query(aiptek, 0x08, 0x00)) < 0)
		return ret;
1000 1001
	aiptek->inputdev->absmin[ABS_PRESSURE] = 0;
	aiptek->inputdev->absmax[ABS_PRESSURE] = ret - 1;
Linus Torvalds's avatar
Linus Torvalds committed
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049

	/* Depending on whether we are in absolute or relative mode, we will
	 * do a switchToTablet(absolute) or switchToMouse(relative) command.
	 */
	if (aiptek->curSetting.coordinateMode ==
	    AIPTEK_COORDINATE_ABSOLUTE_MODE) {
		/* Execute switchToTablet */
		if ((ret = aiptek_command(aiptek, 0x10, 0x01)) < 0) {
			return ret;
		}
	} else {
		/* Execute switchToMouse */
		if ((ret = aiptek_command(aiptek, 0x10, 0x00)) < 0) {
			return ret;
		}
	}

	/* Enable the macro keys */
	if ((ret = aiptek_command(aiptek, 0x11, 0x02)) < 0)
		return ret;
#if 0
	/* Execute FilterOn */
	if ((ret = aiptek_command(aiptek, 0x17, 0x00)) < 0)
		return ret;
#endif

	/* Execute AutoGainOn */
	if ((ret = aiptek_command(aiptek, 0x12, 0xff)) < 0)
		return ret;

	/* Reset the eventCount, so we track events from last (re)programming
	 */
	aiptek->diagnostic = AIPTEK_DIAGNOSTIC_NA;
	aiptek->eventCount = 0;

	return 0;
}

/***********************************************************************
 * Sysfs functions. Sysfs prefers that individually-tunable parameters
 * exist in their separate pseudo-files. Summary data that is immutable
 * may exist in a singular file so long as you don't define a writeable
 * interface.
 */

/***********************************************************************
 * support the 'size' file -- display support
 */
1050
static ssize_t show_tabletSize(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds's avatar
Linus Torvalds committed
1051 1052 1053 1054
{
	struct aiptek *aiptek = dev_get_drvdata(dev);

	return snprintf(buf, PAGE_SIZE, "%dx%d\n",
1055 1056
			aiptek->inputdev->absmax[ABS_X] + 1,
			aiptek->inputdev->absmax[ABS_Y] + 1);
Linus Torvalds's avatar
Linus Torvalds committed
1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
}

/* These structs define the sysfs files, param #1 is the name of the
 * file, param 2 is the file permissions, param 3 & 4 are to the
 * output generator and input parser routines. Absence of a routine is
 * permitted -- it only means can't either 'cat' the file, or send data
 * to it.
 */
static DEVICE_ATTR(size, S_IRUGO, show_tabletSize, NULL);

/***********************************************************************
 * support routines for the 'pointer_mode' file. Note that this file
 * both displays current setting and allows reprogramming.
 */
1071 1072 1073 1074 1075 1076 1077
static struct aiptek_map pointer_mode_map[] = {
	{ "stylus",	AIPTEK_POINTER_ONLY_STYLUS_MODE },
	{ "mouse",	AIPTEK_POINTER_ONLY_MOUSE_MODE },
	{ "either",	AIPTEK_POINTER_EITHER_MODE },
	{ NULL,		AIPTEK_INVALID_VALUE }
};

1078
static ssize_t show_tabletPointerMode(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds's avatar
Linus Torvalds committed
1079 1080 1081
{
	struct aiptek *aiptek = dev_get_drvdata(dev);

1082 1083 1084
	return snprintf(buf, PAGE_SIZE, "%s\n",
			map_val_to_str(pointer_mode_map,
					aiptek->curSetting.pointerMode));
Linus Torvalds's avatar
Linus Torvalds committed
1085 1086 1087
}

static ssize_t
1088
store_tabletPointerMode(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds's avatar
Linus Torvalds committed
1089 1090
{
	struct aiptek *aiptek = dev_get_drvdata(dev);
1091
	int new_mode = map_str_to_val(pointer_mode_map, buf, count);
Linus Torvalds's avatar
Linus Torvalds committed
1092

1093 1094 1095 1096
	if (new_mode == AIPTEK_INVALID_VALUE)
		return -EINVAL;

	aiptek->newSetting.pointerMode = new_mode;
Linus Torvalds's avatar
Linus Torvalds committed
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107
	return count;
}

static DEVICE_ATTR(pointer_mode,
		   S_IRUGO | S_IWUGO,
		   show_tabletPointerMode, store_tabletPointerMode);

/***********************************************************************
 * support routines for the 'coordinate_mode' file. Note that this file
 * both displays current setting and allows reprogramming.
 */
1108 1109 1110 1111 1112 1113 1114

static struct aiptek_map coordinate_mode_map[] = {
	{ "absolute",	AIPTEK_COORDINATE_ABSOLUTE_MODE },
	{ "relative",	AIPTEK_COORDINATE_RELATIVE_MODE },
	{ NULL,		AIPTEK_INVALID_VALUE }
};

1115
static ssize_t show_tabletCoordinateMode(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds's avatar
Linus Torvalds committed
1116 1117 1118
{
	struct aiptek *aiptek = dev_get_drvdata(dev);

1119 1120 1121
	return snprintf(buf, PAGE_SIZE, "%s\n",
			map_val_to_str(coordinate_mode_map,
					aiptek->curSetting.coordinateMode));
Linus Torvalds's avatar
Linus Torvalds committed
1122 1123 1124
}

static ssize_t
1125
store_tabletCoordinateMode(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds's avatar
Linus Torvalds committed
1126 1127
{
	struct aiptek *aiptek = dev_get_drvdata(dev);
1128
	int new_mode = map_str_to_val(coordinate_mode_map, buf, count);
Linus Torvalds's avatar
Linus Torvalds committed
1129

1130 1131 1132 1133
	if (new_mode == AIPTEK_INVALID_VALUE)
		return -EINVAL;

	aiptek->newSetting.coordinateMode = new_mode;
Linus Torvalds's avatar
Linus Torvalds committed
1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
	return count;
}

static DEVICE_ATTR(coordinate_mode,
		   S_IRUGO | S_IWUGO,
		   show_tabletCoordinateMode, store_tabletCoordinateMode);

/***********************************************************************
 * support routines for the 'tool_mode' file. Note that this file
 * both displays current setting and allows reprogramming.
 */
1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156

static struct aiptek_map tool_mode_map[] = {
	{ "mouse",	AIPTEK_TOOL_BUTTON_MOUSE_MODE },
	{ "eraser",	AIPTEK_TOOL_BUTTON_ERASER_MODE },
	{ "pencil",	AIPTEK_TOOL_BUTTON_PENCIL_MODE },
	{ "pen",	AIPTEK_TOOL_BUTTON_PEN_MODE },
	{ "brush",	AIPTEK_TOOL_BUTTON_BRUSH_MODE },
	{ "airbrush",	AIPTEK_TOOL_BUTTON_AIRBRUSH_MODE },
	{ "lens",	AIPTEK_TOOL_BUTTON_LENS_MODE },
	{ NULL,		AIPTEK_INVALID_VALUE }
};

1157
static ssize_t show_tabletToolMode(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds's avatar
Linus Torvalds committed
1158 1159 1160
{
	struct aiptek *aiptek = dev_get_drvdata(dev);

1161 1162 1163
	return snprintf(buf, PAGE_SIZE, "%s\n",
			map_val_to_str(tool_mode_map,
					aiptek->curSetting.toolMode));
Linus Torvalds's avatar
Linus Torvalds committed
1164 1165 1166
}

static ssize_t
1167
store_tabletToolMode(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds's avatar
Linus Torvalds committed
1168 1169
{
	struct aiptek *aiptek = dev_get_drvdata(dev);
1170
	int new_mode = map_str_to_val(tool_mode_map, buf, count);
Linus Torvalds's avatar
Linus Torvalds committed
1171

1172 1173
	if (new_mode == AIPTEK_INVALID_VALUE)
		return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
1174

1175
	aiptek->newSetting.toolMode = new_mode;
Linus Torvalds's avatar
Linus Torvalds committed
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
	return count;
}

static DEVICE_ATTR(tool_mode,
		   S_IRUGO | S_IWUGO,
		   show_tabletToolMode, store_tabletToolMode);

/***********************************************************************
 * support routines for the 'xtilt' file. Note that this file
 * both displays current setting and allows reprogramming.
 */
1187
static ssize_t show_tabletXtilt(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds's avatar
Linus Torvalds committed
1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
{
	struct aiptek *aiptek = dev_get_drvdata(dev);

	if (aiptek->curSetting.xTilt == AIPTEK_TILT_DISABLE) {
		return snprintf(buf, PAGE_SIZE, "disable\n");
	} else {
		return snprintf(buf, PAGE_SIZE, "%d\n",
				aiptek->curSetting.xTilt);
	}
}

static ssize_t
1200
store_tabletXtilt(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds's avatar
Linus Torvalds committed
1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222
{
	struct aiptek *aiptek = dev_get_drvdata(dev);
	int x;

	if (strcmp(buf, "disable") == 0) {
		aiptek->newSetting.xTilt = AIPTEK_TILT_DISABLE;
	} else {
		x = (int)simple_strtol(buf, NULL, 10);
		if (x >= AIPTEK_TILT_MIN && x <= AIPTEK_TILT_MAX) {
			aiptek->newSetting.xTilt = x;
		}
	}
	return count;
}

static DEVICE_ATTR(xtilt,
		   S_IRUGO | S_IWUGO, show_tabletXtilt, store_tabletXtilt);

/***********************************************************************
 * support routines for the 'ytilt' file. Note that this file
 * both displays current setting and allows reprogramming.
 */
1223
static ssize_t show_tabletYtilt(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds's avatar
Linus Torvalds committed
1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235
{
	struct aiptek *aiptek = dev_get_drvdata(dev);

	if (aiptek->curSetting.yTilt == AIPTEK_TILT_DISABLE) {
		return snprintf(buf, PAGE_SIZE, "disable\n");
	} else {
		return snprintf(buf, PAGE_SIZE, "%d\n",
				aiptek->curSetting.yTilt);
	}
}

static ssize_t
1236
store_tabletYtilt(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds's avatar
Linus Torvalds committed
1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258
{
	struct aiptek *aiptek = dev_get_drvdata(dev);
	int y;

	if (strcmp(buf, "disable") == 0) {
		aiptek->newSetting.yTilt = AIPTEK_TILT_DISABLE;
	} else {
		y = (int)simple_strtol(buf, NULL, 10);
		if (y >= AIPTEK_TILT_MIN && y <= AIPTEK_TILT_MAX) {
			aiptek->newSetting.yTilt = y;
		}
	}
	return count;
}

static DEVICE_ATTR(ytilt,
		   S_IRUGO | S_IWUGO, show_tabletYtilt, store_tabletYtilt);

/***********************************************************************
 * support routines for the 'jitter' file. Note that this file
 * both displays current setting and allows reprogramming.
 */
1259
static ssize_t show_tabletJitterDelay(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds's avatar
Linus Torvalds committed
1260 1261 1262 1263 1264 1265 1266
{
	struct aiptek *aiptek = dev_get_drvdata(dev);

	return snprintf(buf, PAGE_SIZE, "%d\n", aiptek->curSetting.jitterDelay);
}

static ssize_t
1267
store_tabletJitterDelay(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds's avatar
Linus Torvalds committed
1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282
{
	struct aiptek *aiptek = dev_get_drvdata(dev);

	aiptek->newSetting.jitterDelay = (int)simple_strtol(buf, NULL, 10);
	return count;
}

static DEVICE_ATTR(jitter,
		   S_IRUGO | S_IWUGO,
		   show_tabletJitterDelay, store_tabletJitterDelay);

/***********************************************************************
 * support routines for the 'delay' file. Note that this file
 * both displays current setting and allows reprogramming.
 */
1283
static ssize_t show_tabletProgrammableDelay(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds's avatar
Linus Torvalds committed
1284 1285 1286 1287 1288 1289 1290 1291
{
	struct aiptek *aiptek = dev_get_drvdata(dev);

	return snprintf(buf, PAGE_SIZE, "%d\n",
			aiptek->curSetting.programmableDelay);
}

static ssize_t
1292
store_tabletProgrammableDelay(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds's avatar
Linus Torvalds committed
1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307
{
	struct aiptek *aiptek = dev_get_drvdata(dev);

	aiptek->newSetting.programmableDelay = (int)simple_strtol(buf, NULL, 10);
	return count;
}

static DEVICE_ATTR(delay,
		   S_IRUGO | S_IWUGO,
		   show_tabletProgrammableDelay, store_tabletProgrammableDelay);

/***********************************************************************
 * support routines for the 'event_count' file. Note that this file
 * only displays current setting.
 */
1308
static ssize_t show_tabletEventsReceived(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds's avatar
Linus Torvalds committed
1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320
{
	struct aiptek *aiptek = dev_get_drvdata(dev);

	return snprintf(buf, PAGE_SIZE, "%ld\n", aiptek->eventCount);
}

static DEVICE_ATTR(event_count, S_IRUGO, show_tabletEventsReceived, NULL);

/***********************************************************************
 * support routines for the 'diagnostic' file. Note that this file
 * only displays current setting.
 */
1321
static ssize_t show_tabletDiagnosticMessage(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds's avatar
Linus Torvalds committed
1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359
{
	struct aiptek *aiptek = dev_get_drvdata(dev);
	char *retMsg;

	switch (aiptek->diagnostic) {
	case AIPTEK_DIAGNOSTIC_NA:
		retMsg = "no errors\n";
		break;

	case AIPTEK_DIAGNOSTIC_SENDING_RELATIVE_IN_ABSOLUTE:
		retMsg = "Error: receiving relative reports\n";
		break;

	case AIPTEK_DIAGNOSTIC_SENDING_ABSOLUTE_IN_RELATIVE:
		retMsg = "Error: receiving absolute reports\n";
		break;

	case AIPTEK_DIAGNOSTIC_TOOL_DISALLOWED:
		if (aiptek->curSetting.pointerMode ==
		    AIPTEK_POINTER_ONLY_MOUSE_MODE) {
			retMsg = "Error: receiving stylus reports\n";
		} else {
			retMsg = "Error: receiving mouse reports\n";
		}
		break;

	default:
		return 0;
	}
	return snprintf(buf, PAGE_SIZE, retMsg);
}

static DEVICE_ATTR(diagnostic, S_IRUGO, show_tabletDiagnosticMessage, NULL);

/***********************************************************************
 * support routines for the 'stylus_upper' file. Note that this file
 * both displays current setting and allows for setting changing.
 */
1360 1361 1362 1363 1364 1365 1366

static struct aiptek_map stylus_button_map[] = {
	{ "upper",	AIPTEK_STYLUS_UPPER_BUTTON },
	{ "lower",	AIPTEK_STYLUS_LOWER_BUTTON },
	{ NULL,		AIPTEK_INVALID_VALUE }
};

1367
static ssize_t show_tabletStylusUpper(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds's avatar
Linus Torvalds committed
1368 1369 1370
{
	struct aiptek *aiptek = dev_get_drvdata(dev);

1371 1372 1373
	return snprintf(buf, PAGE_SIZE, "%s\n",
			map_val_to_str(stylus_button_map,
					aiptek->curSetting.stylusButtonUpper));
Linus Torvalds's avatar
Linus Torvalds committed
1374 1375 1376
}

static ssize_t
1377
store_tabletStylusUpper(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds's avatar
Linus Torvalds committed
1378 1379
{
	struct aiptek *aiptek = dev_get_drvdata(dev);
1380
	int new_button = map_str_to_val(stylus_button_map, buf, count);
Linus Torvalds's avatar
Linus Torvalds committed
1381

1382 1383 1384 1385
	if (new_button == AIPTEK_INVALID_VALUE)
		return -EINVAL;

	aiptek->newSetting.stylusButtonUpper = new_button;
Linus Torvalds's avatar
Linus Torvalds committed
1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396
	return count;
}

static DEVICE_ATTR(stylus_upper,
		   S_IRUGO | S_IWUGO,
		   show_tabletStylusUpper, store_tabletStylusUpper);

/***********************************************************************
 * support routines for the 'stylus_lower' file. Note that this file
 * both displays current setting and allows for setting changing.
 */
1397

1398
static ssize_t show_tabletStylusLower(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds's avatar
Linus Torvalds committed
1399 1400 1401
{
	struct aiptek *aiptek = dev_get_drvdata(dev);

1402 1403 1404
	return snprintf(buf, PAGE_SIZE, "%s\n",
			map_val_to_str(stylus_button_map,
					aiptek->curSetting.stylusButtonLower));
Linus Torvalds's avatar
Linus Torvalds committed
1405 1406 1407
}

static ssize_t
1408
store_tabletStylusLower(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds's avatar
Linus Torvalds committed
1409 1410
{
	struct aiptek *aiptek = dev_get_drvdata(dev);
1411
	int new_button = map_str_to_val(stylus_button_map, buf, count);
Linus Torvalds's avatar
Linus Torvalds committed
1412

1413 1414 1415 1416
	if (new_button == AIPTEK_INVALID_VALUE)
		return -EINVAL;

	aiptek->newSetting.stylusButtonLower = new_button;
Linus Torvalds's avatar
Linus Torvalds committed
1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427
	return count;
}

static DEVICE_ATTR(stylus_lower,
		   S_IRUGO | S_IWUGO,
		   show_tabletStylusLower, store_tabletStylusLower);

/***********************************************************************
 * support routines for the 'mouse_left' file. Note that this file
 * both displays current setting and allows for setting changing.
 */
1428 1429 1430 1431 1432 1433 1434 1435

static struct aiptek_map mouse_button_map[] = {
	{ "left",	AIPTEK_MOUSE_LEFT_BUTTON },
	{ "middle",	AIPTEK_MOUSE_MIDDLE_BUTTON },
	{ "right",	AIPTEK_MOUSE_RIGHT_BUTTON },
	{ NULL,		AIPTEK_INVALID_VALUE }
};

1436
static ssize_t show_tabletMouseLeft(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds's avatar
Linus Torvalds committed
1437 1438 1439
{
	struct aiptek *aiptek = dev_get_drvdata(dev);

1440 1441 1442
	return snprintf(buf, PAGE_SIZE, "%s\n",
			map_val_to_str(mouse_button_map,
					aiptek->curSetting.mouseButtonLeft));
Linus Torvalds's avatar
Linus Torvalds committed
1443 1444 1445
}

static ssize_t
1446
store_tabletMouseLeft(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds's avatar
Linus Torvalds committed
1447 1448
{
	struct aiptek *aiptek = dev_get_drvdata(dev);
1449
	int new_button = map_str_to_val(mouse_button_map, buf, count);
Linus Torvalds's avatar
Linus Torvalds committed
1450

1451 1452 1453 1454
	if (new_button == AIPTEK_INVALID_VALUE)
		return -EINVAL;

	aiptek->newSetting.mouseButtonLeft = new_button;
Linus Torvalds's avatar
Linus Torvalds committed
1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465
	return count;
}

static DEVICE_ATTR(mouse_left,
		   S_IRUGO | S_IWUGO,
		   show_tabletMouseLeft, store_tabletMouseLeft);

/***********************************************************************
 * support routines for the 'mouse_middle' file. Note that this file
 * both displays current setting and allows for setting changing.
 */
1466
static ssize_t show_tabletMouseMiddle(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds's avatar
Linus Torvalds committed
1467 1468 1469
{
	struct aiptek *aiptek = dev_get_drvdata(dev);

1470 1471 1472
	return snprintf(buf, PAGE_SIZE, "%s\n",
			map_val_to_str(mouse_button_map,
					aiptek->curSetting.mouseButtonMiddle));
Linus Torvalds's avatar
Linus Torvalds committed
1473 1474 1475
}

static ssize_t
1476
store_tabletMouseMiddle(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds's avatar
Linus Torvalds committed
1477 1478
{
	struct aiptek *aiptek = dev_get_drvdata(dev);
1479
	int new_button = map_str_to_val(mouse_button_map, buf, count);
Linus Torvalds's avatar
Linus Torvalds committed
1480

1481 1482 1483 1484
	if (new_button == AIPTEK_INVALID_VALUE)
		return -EINVAL;

	aiptek->newSetting.mouseButtonMiddle = new_button;
Linus Torvalds's avatar
Linus Torvalds committed
1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495
	return count;
}

static DEVICE_ATTR(mouse_middle,
		   S_IRUGO | S_IWUGO,
		   show_tabletMouseMiddle, store_tabletMouseMiddle);

/***********************************************************************
 * support routines for the 'mouse_right' file. Note that this file
 * both displays current setting and allows for setting changing.
 */
1496
static ssize_t show_tabletMouseRight(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds's avatar
Linus Torvalds committed
1497 1498 1499
{
	struct aiptek *aiptek = dev_get_drvdata(dev);

1500 1501 1502
	return snprintf(buf, PAGE_SIZE, "%s\n",
			map_val_to_str(mouse_button_map,
					aiptek->curSetting.mouseButtonRight));
Linus Torvalds's avatar
Linus Torvalds committed
1503 1504 1505
}

static ssize_t
1506
store_tabletMouseRight(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds's avatar
Linus Torvalds committed
1507 1508
{
	struct aiptek *aiptek = dev_get_drvdata(dev);
1509
	int new_button = map_str_to_val(mouse_button_map, buf, count);
Linus Torvalds's avatar
Linus Torvalds committed
1510

1511 1512 1513 1514
	if (new_button == AIPTEK_INVALID_VALUE)
		return -EINVAL;

	aiptek->newSetting.mouseButtonRight = new_button;
Linus Torvalds's avatar
Linus Torvalds committed
1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525
	return count;
}

static DEVICE_ATTR(mouse_right,
		   S_IRUGO | S_IWUGO,
		   show_tabletMouseRight, store_tabletMouseRight);

/***********************************************************************
 * support routines for the 'wheel' file. Note that this file
 * both displays current setting and allows for setting changing.
 */
1526
static ssize_t show_tabletWheel(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds's avatar
Linus Torvalds committed
1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538
{
	struct aiptek *aiptek = dev_get_drvdata(dev);

	if (aiptek->curSetting.wheel == AIPTEK_WHEEL_DISABLE) {
		return snprintf(buf, PAGE_SIZE, "disable\n");
	} else {
		return snprintf(buf, PAGE_SIZE, "%d\n",
				aiptek->curSetting.wheel);
	}
}

static ssize_t
1539
store_tabletWheel(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds's avatar
Linus Torvalds committed
1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553
{
	struct aiptek *aiptek = dev_get_drvdata(dev);

	aiptek->newSetting.wheel = (int)simple_strtol(buf, NULL, 10);
	return count;
}

static DEVICE_ATTR(wheel,
		   S_IRUGO | S_IWUGO, show_tabletWheel, store_tabletWheel);

/***********************************************************************
 * support routines for the 'execute' file. Note that this file
 * both displays current setting and allows for setting changing.
 */
1554
static ssize_t show_tabletExecute(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds's avatar
Linus Torvalds committed
1555 1556 1557 1558 1559 1560 1561 1562 1563
{
	/* There is nothing useful to display, so a one-line manual
	 * is in order...
	 */
	return snprintf(buf, PAGE_SIZE,
			"Write anything to this file to program your tablet.\n");
}

static ssize_t
1564
store_tabletExecute(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds's avatar
Linus Torvalds committed
1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586
{
	struct aiptek *aiptek = dev_get_drvdata(dev);

	/* We do not care what you write to this file. Merely the action
	 * of writing to this file triggers a tablet reprogramming.
	 */
	memcpy(&aiptek->curSetting, &aiptek->newSetting,
	       sizeof(struct aiptek_settings));

	if (aiptek_program_tablet(aiptek) < 0)
		return -EIO;

	return count;
}

static DEVICE_ATTR(execute,
		   S_IRUGO | S_IWUGO, show_tabletExecute, store_tabletExecute);

/***********************************************************************
 * support routines for the 'odm_code' file. Note that this file
 * only displays current setting.
 */
1587
static ssize_t show_tabletODMCode(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds's avatar
Linus Torvalds committed
1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599
{
	struct aiptek *aiptek = dev_get_drvdata(dev);

	return snprintf(buf, PAGE_SIZE, "0x%04x\n", aiptek->features.odmCode);
}

static DEVICE_ATTR(odm_code, S_IRUGO, show_tabletODMCode, NULL);

/***********************************************************************
 * support routines for the 'model_code' file. Note that this file
 * only displays current setting.
 */
1600
static ssize_t show_tabletModelCode(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds's avatar
Linus Torvalds committed
1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612
{
	struct aiptek *aiptek = dev_get_drvdata(dev);

	return snprintf(buf, PAGE_SIZE, "0x%04x\n", aiptek->features.modelCode);
}

static DEVICE_ATTR(model_code, S_IRUGO, show_tabletModelCode, NULL);

/***********************************************************************
 * support routines for the 'firmware_code' file. Note that this file
 * only displays current setting.
 */
1613
static ssize_t show_firmwareCode(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds's avatar
Linus Torvalds committed
1614 1615 1616 1617 1618 1619 1620 1621 1622
{
	struct aiptek *aiptek = dev_get_drvdata(dev);

	return snprintf(buf, PAGE_SIZE, "%04x\n",
			aiptek->features.firmwareCode);
}

static DEVICE_ATTR(firmware_code, S_IRUGO, show_firmwareCode, NULL);

1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645
static struct attribute *aiptek_attributes[] = {
	&dev_attr_size.attr,
	&dev_attr_pointer_mode.attr,
	&dev_attr_coordinate_mode.attr,
	&dev_attr_tool_mode.attr,
	&dev_attr_xtilt.attr,
	&dev_attr_ytilt.attr,
	&dev_attr_jitter.attr,
	&dev_attr_delay.attr,
	&dev_attr_event_count.attr,
	&dev_attr_diagnostic.attr,
	&dev_attr_odm_code.attr,
	&dev_attr_model_code.attr,
	&dev_attr_firmware_code.attr,
	&dev_attr_stylus_lower.attr,
	&dev_attr_stylus_upper.attr,
	&dev_attr_mouse_left.attr,
	&dev_attr_mouse_middle.attr,
	&dev_attr_mouse_right.attr,
	&dev_attr_wheel.attr,
	&dev_attr_execute.attr,
	NULL
};
Linus Torvalds's avatar
Linus Torvalds committed
1646

1647 1648 1649
static struct attribute_group aiptek_attribute_group = {
	.attrs	= aiptek_attributes,
};
Linus Torvalds's avatar
Linus Torvalds committed
1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670

/***********************************************************************
 * This routine is called when a tablet has been identified. It basically
 * sets up the tablet and the driver's internal structures.
 */
static int
aiptek_probe(struct usb_interface *intf, const struct usb_device_id *id)
{
	struct usb_device *usbdev = interface_to_usbdev(intf);
	struct usb_endpoint_descriptor *endpoint;
	struct aiptek *aiptek;
	struct input_dev *inputdev;
	int i;
	int speeds[] = { 0,
		AIPTEK_PROGRAMMABLE_DELAY_50,
		AIPTEK_PROGRAMMABLE_DELAY_400,
		AIPTEK_PROGRAMMABLE_DELAY_25,
		AIPTEK_PROGRAMMABLE_DELAY_100,
		AIPTEK_PROGRAMMABLE_DELAY_200,
		AIPTEK_PROGRAMMABLE_DELAY_300
	};
1671
	int err = -ENOMEM;
Linus Torvalds's avatar
Linus Torvalds committed
1672 1673 1674 1675 1676 1677 1678 1679 1680

	/* programmableDelay is where the command-line specified
	 * delay is kept. We make it the first element of speeds[],
	 * so therefore, your override speed is tried first, then the
	 * remainder. Note that the default value of 400ms will be tried
	 * if you do not specify any command line parameter.
	 */
	speeds[0] = programmableDelay;

1681 1682
	aiptek = kzalloc(sizeof(struct aiptek), GFP_KERNEL);
	inputdev = input_allocate_device();
1683 1684
	if (!aiptek || !inputdev) {
		warn("aiptek: cannot allocate memory or input device");
1685
		goto fail1;
1686
        }
Linus Torvalds's avatar
Linus Torvalds committed
1687 1688

	aiptek->data = usb_buffer_alloc(usbdev, AIPTEK_PACKET_LENGTH,
1689
					GFP_ATOMIC, &aiptek->data_dma);
1690 1691
        if (!aiptek->data) {
		warn("aiptek: cannot allocate usb buffer");
1692
		goto fail1;
1693
	}
Linus Torvalds's avatar
Linus Torvalds committed
1694 1695

	aiptek->urb = usb_alloc_urb(0, GFP_KERNEL);
1696 1697
	if (!aiptek->urb) {
	        warn("aiptek: cannot allocate urb");
1698
		goto fail2;
1699
	}
1700 1701 1702 1703 1704 1705 1706

	aiptek->inputdev = inputdev;
	aiptek->usbdev = usbdev;
	aiptek->ifnum = intf->altsetting[0].desc.bInterfaceNumber;
	aiptek->inDelay = 0;
	aiptek->endDelay = 0;
	aiptek->previousJitterable = 0;
1707
	aiptek->lastMacro = -1;
Linus Torvalds's avatar
Linus Torvalds committed
1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729

	/* Set up the curSettings struct. Said struct contains the current
	 * programmable parameters. The newSetting struct contains changes
	 * the user makes to the settings via the sysfs interface. Those
	 * changes are not "committed" to curSettings until the user
	 * writes to the sysfs/.../execute file.
	 */
	aiptek->curSetting.pointerMode = AIPTEK_POINTER_EITHER_MODE;
	aiptek->curSetting.coordinateMode = AIPTEK_COORDINATE_ABSOLUTE_MODE;
	aiptek->curSetting.toolMode = AIPTEK_TOOL_BUTTON_PEN_MODE;
	aiptek->curSetting.xTilt = AIPTEK_TILT_DISABLE;
	aiptek->curSetting.yTilt = AIPTEK_TILT_DISABLE;
	aiptek->curSetting.mouseButtonLeft = AIPTEK_MOUSE_LEFT_BUTTON;
	aiptek->curSetting.mouseButtonMiddle = AIPTEK_MOUSE_MIDDLE_BUTTON;
	aiptek->curSetting.mouseButtonRight = AIPTEK_MOUSE_RIGHT_BUTTON;
	aiptek->curSetting.stylusButtonUpper = AIPTEK_STYLUS_UPPER_BUTTON;
	aiptek->curSetting.stylusButtonLower = AIPTEK_STYLUS_LOWER_BUTTON;
	aiptek->curSetting.jitterDelay = jitterDelay;
	aiptek->curSetting.programmableDelay = programmableDelay;

	/* Both structs should have equivalent settings
	 */
1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749
	aiptek->newSetting = aiptek->curSetting;

	/* Determine the usb devices' physical path.
	 * Asketh not why we always pretend we're using "../input0",
	 * but I suspect this will have to be refactored one
	 * day if a single USB device can be a keyboard & a mouse
	 * & a tablet, and the inputX number actually will tell
	 * us something...
	 */
	usb_make_path(usbdev, aiptek->features.usbPath,
			sizeof(aiptek->features.usbPath));
	strlcat(aiptek->features.usbPath, "/input0",
		sizeof(aiptek->features.usbPath));

	/* Set up client data, pointers to open and close routines
	 * for the input device.
	 */
	inputdev->name = "Aiptek";
	inputdev->phys = aiptek->features.usbPath;
	usb_to_input_id(usbdev, &inputdev->id);
1750
	inputdev->dev.parent = &intf->dev;
1751 1752 1753

	input_set_drvdata(inputdev, aiptek);

1754 1755
	inputdev->open = aiptek_open;
	inputdev->close = aiptek_close;
Linus Torvalds's avatar
Linus Torvalds committed
1756 1757 1758 1759

	/* Now program the capacities of the tablet, in terms of being
	 * an input device.
	 */
1760 1761
	for (i = 0; i < ARRAY_SIZE(eventTypes); ++i)
	        __set_bit(eventTypes[i], inputdev->evbit);
Linus Torvalds's avatar
Linus Torvalds committed
1762

1763 1764
	for (i = 0; i < ARRAY_SIZE(absEvents); ++i)
	        __set_bit(absEvents[i], inputdev->absbit);
Linus Torvalds's avatar
Linus Torvalds committed
1765

1766 1767
	for (i = 0; i < ARRAY_SIZE(relEvents); ++i)
	        __set_bit(relEvents[i], inputdev->relbit);
Linus Torvalds's avatar
Linus Torvalds committed
1768

1769
	__set_bit(MSC_SERIAL, inputdev->mscbit);
Linus Torvalds's avatar
Linus Torvalds committed
1770

1771 1772 1773 1774
	/* Set up key and button codes */
	for (i = 0; i < ARRAY_SIZE(buttonEvents); ++i)
		__set_bit(buttonEvents[i], inputdev->keybit);

1775
	for (i = 0; i < ARRAY_SIZE(macroKeyEvents); ++i)
1776
		__set_bit(macroKeyEvents[i], inputdev->keybit);
Linus Torvalds's avatar
Linus Torvalds committed
1777

1778 1779
	/*
	 * Program the input device coordinate capacities. We do not yet
Linus Torvalds's avatar
Linus Torvalds committed
1780 1781 1782 1783
	 * know what maximum X, Y, and Z values are, so we're putting fake
	 * values in. Later, we'll ask the tablet to put in the correct
	 * values.
	 */
1784
	input_set_abs_params(inputdev, ABS_X, 0, 2999, 0, 0);
1785
	input_set_abs_params(inputdev, ABS_Y, 0, 2249, 0, 0);
1786 1787 1788 1789
	input_set_abs_params(inputdev, ABS_PRESSURE, 0, 511, 0, 0);
	input_set_abs_params(inputdev, ABS_TILT_X, AIPTEK_TILT_MIN, AIPTEK_TILT_MAX, 0, 0);
	input_set_abs_params(inputdev, ABS_TILT_Y, AIPTEK_TILT_MIN, AIPTEK_TILT_MAX, 0, 0);
	input_set_abs_params(inputdev, ABS_WHEEL, AIPTEK_WHEEL_MIN, AIPTEK_WHEEL_MAX - 1, 0, 0);
Linus Torvalds's avatar
Linus Torvalds committed
1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816

	endpoint = &intf->altsetting[0].endpoint[0].desc;

	/* Go set up our URB, which is called when the tablet receives
	 * input.
	 */
	usb_fill_int_urb(aiptek->urb,
			 aiptek->usbdev,
			 usb_rcvintpipe(aiptek->usbdev,
					endpoint->bEndpointAddress),
			 aiptek->data, 8, aiptek_irq, aiptek,
			 endpoint->bInterval);

	aiptek->urb->transfer_dma = aiptek->data_dma;
	aiptek->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;

	/* Program the tablet. This sets the tablet up in the mode
	 * specified in newSetting, and also queries the tablet's
	 * physical capacities.
	 *
	 * Sanity check: if a tablet doesn't like the slow programmatic
	 * delay, we often get sizes of 0x0. Let's use that as an indicator
	 * to try faster delays, up to 25 ms. If that logic fails, well, you'll
	 * have to explain to us how your tablet thinks it's 0x0, and yet that's
	 * not an error :-)
	 */

1817
	for (i = 0; i < ARRAY_SIZE(speeds); ++i) {
Linus Torvalds's avatar
Linus Torvalds committed
1818 1819
		aiptek->curSetting.programmableDelay = speeds[i];
		(void)aiptek_program_tablet(aiptek);
1820
		if (aiptek->inputdev->absmax[ABS_X] > 0) {
Linus Torvalds's avatar
Linus Torvalds committed
1821 1822 1823 1824 1825 1826
			info("input: Aiptek using %d ms programming speed\n",
			     aiptek->curSetting.programmableDelay);
			break;
		}
	}

1827 1828 1829 1830 1831 1832 1833
	/* Murphy says that some day someone will have a tablet that fails the
	   above test. That's you, Frederic Rodrigo */
	if (i == ARRAY_SIZE(speeds)) {
		info("input: Aiptek tried all speeds, no sane response");
		goto fail2;
	}

Linus Torvalds's avatar
Linus Torvalds committed
1834 1835 1836 1837 1838 1839
	/* Associate this driver's struct with the usb interface.
	 */
	usb_set_intfdata(intf, aiptek);

	/* Set up the sysfs files
	 */
1840
	err = sysfs_create_group(&intf->dev.kobj, &aiptek_attribute_group);
1841 1842
	if (err) {
		warn("aiptek: cannot create sysfs group err: %d", err);
1843
		goto fail3;
1844
        }
1845 1846 1847 1848

	/* Register the tablet as an Input Device
	 */
	err = input_register_device(aiptek->inputdev);
1849 1850
	if (err) {
		warn("aiptek: input_register_device returned err: %d", err);
1851
		goto fail4;
1852
        }
Linus Torvalds's avatar
Linus Torvalds committed
1853
	return 0;
1854

1855 1856
 fail4:	sysfs_remove_group(&intf->dev.kobj, &aiptek_attribute_group);
 fail3: usb_free_urb(aiptek->urb);
1857
 fail2:	usb_buffer_free(usbdev, AIPTEK_PACKET_LENGTH, aiptek->data,
1858
			aiptek->data_dma);
1859 1860
 fail1: usb_set_intfdata(intf, NULL);
	input_free_device(inputdev);
1861
	kfree(aiptek);
1862
	return err;
Linus Torvalds's avatar
Linus Torvalds committed
1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878
}

/***********************************************************************
 * Deal with tablet disconnecting from the system.
 */
static void aiptek_disconnect(struct usb_interface *intf)
{
	struct aiptek *aiptek = usb_get_intfdata(intf);

	/* Disassociate driver's struct with usb interface
	 */
	usb_set_intfdata(intf, NULL);
	if (aiptek != NULL) {
		/* Free & unhook everything from the system.
		 */
		usb_kill_urb(aiptek->urb);
1879
		input_unregister_device(aiptek->inputdev);
1880
		sysfs_remove_group(&intf->dev.kobj, &aiptek_attribute_group);
Linus Torvalds's avatar
Linus Torvalds committed
1881 1882 1883 1884 1885 1886 1887 1888
		usb_free_urb(aiptek->urb);
		usb_buffer_free(interface_to_usbdev(intf),
				AIPTEK_PACKET_LENGTH,
				aiptek->data, aiptek->data_dma);
		kfree(aiptek);
	}
}

1889 1890 1891 1892 1893 1894 1895
static struct usb_driver aiptek_driver = {
	.name = "aiptek",
	.probe = aiptek_probe,
	.disconnect = aiptek_disconnect,
	.id_table = aiptek_ids,
};

Linus Torvalds's avatar
Linus Torvalds committed
1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921
static int __init aiptek_init(void)
{
	int result = usb_register(&aiptek_driver);
	if (result == 0) {
		info(DRIVER_VERSION ": " DRIVER_AUTHOR);
		info(DRIVER_DESC);
	}
	return result;
}

static void __exit aiptek_exit(void)
{
	usb_deregister(&aiptek_driver);
}

MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_LICENSE("GPL");

module_param(programmableDelay, int, 0);
MODULE_PARM_DESC(programmableDelay, "delay used during tablet programming");
module_param(jitterDelay, int, 0);
MODULE_PARM_DESC(jitterDelay, "stylus/mouse settlement delay");

module_init(aiptek_init);
module_exit(aiptek_exit);