2 * Native support for the Aiptek HyperPen USB Tablets
3 * (4000U/5000U/6000U/8000U/12000U)
5 * Copyright (c) 2001 Chris Atenasio <chris@crud.net>
6 * Copyright (c) 2002-2003 Bryan W. Headley <bwheadley@earthlink.net>
9 * Vojtech Pavlik <vojtech@suse.cz>
10 * Andreas Bach Aaen <abach@stofanet.dk>
11 * Clifford Wolf <clifford@clifford.at>
12 * Sam Mosel <sam.mosel@computer.org>
13 * James E. Blair <corvus@gnu.org>
14 * Daniel Egger <egger@suse.de>
16 * Many thanks to Oliver Kuechemann for his support.
19 * v0.1 - Initial release
20 * v0.2 - Hack to get around fake event 28's. (Bryan W. Headley)
21 * v0.3 - Make URB dynamic (Bryan W. Headley, Jun-8-2002)
22 * Released to Linux 2.4.19 and 2.5.x
23 * v0.4 - Rewrote substantial portions of the code to deal with
24 * corrected control sequences, timing, dynamic configuration,
25 * support of 6000U - 12000U, procfs, and macro key support
26 * (Jan-1-2003 - Feb-5-2003, Bryan W. Headley)
27 * v1.0 - Added support for diagnostic messages, count of messages
28 * received from URB - Mar-8-2003, Bryan W. Headley
31 * This kernel driver is augmented by the "Aiptek" XFree86 input
32 * driver for your X server, as well as a GUI Front-end "Tablet Manager".
33 * These three products are highly interactive with one another,
34 * so therefore it's easier to document them all as one subsystem.
35 * Please visit the project's "home page", located at,
36 * http://aiptektablet.sourceforge.net.
41 * This program is free software; you can redistribute it and/or modify
42 * it under the terms of the GNU General Public License as published by
43 * the Free Software Foundation; either version 2 of the License, or
44 * (at your option) any later version.
46 * This program is distributed in the hope that it will be useful,
47 * but WITHOUT ANY WARRANTY; without even the implied warranty of
48 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
49 * GNU General Public License for more details.
51 * You should have received a copy of the GNU General Public License
52 * along with this program; if not, write to the Free Software
53 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
56 #include <linux/kernel.h>
57 #include <linux/slab.h>
58 #include <linux/input.h>
59 #include <linux/module.h>
60 #include <linux/init.h>
61 #include <linux/usb.h>
62 #include <linux/proc_fs.h>
63 #include <asm/uaccess.h>
68 #define DRIVER_VERSION "v1.0 Mar-8-2003"
69 #define DRIVER_AUTHOR "Bryan W. Headley/Chris Atenasio"
70 #define DRIVER_DESC "Aiptek HyperPen USB Tablet Driver (Linux 2.4.x)"
72 MODULE_AUTHOR(DRIVER_AUTHOR);
73 MODULE_DESCRIPTION(DRIVER_DESC);
74 MODULE_LICENSE("GPL");
77 * Aiptek status packet:
78 * (returned as Report 1)
80 * bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0
81 * byte0 0 0 0 0 0 0 1 0
82 * byte1 X7 X6 X5 X4 X3 X2 X1 X0
83 * byte2 X15 X14 X13 X12 X11 X10 X9 X8
84 * byte3 Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0
85 * byte4 Y15 Y14 Y13 Y12 Y11 Y10 Y9 Y8
86 * byte5 * * * BS2 BS1 Tip DV IR
87 * byte6 P7 P6 P5 P4 P3 P2 P1 P0
88 * byte7 P15 P14 P13 P12 P11 P10 P9 P8
90 * IR: In Range = Proximity on
92 * BS = Barrel Switch (as in, macro keys)
93 * BS2 also referred to as Tablet Pick
97 * Use report_type CONTROL (3)
100 * Command/Data Description Return Bytes Return Value
101 * 0x10/0x00 SwitchToMouse 0
102 * 0x10/0x01 SwitchToTablet 0
103 * 0x18/0x04 Resolution500LPI 0
104 * 0x17/0x00 FilterOn 0
105 * 0x12/0xFF AutoGainOn 0
106 * 0x01/0x00 GetXExtension 2 MaxX
107 * 0x01/0x01 GetYExtension 2 MaxY
108 * 0x02/0x00 GetModelCode 2 ModelCode = LOBYTE
109 * 0x03/0x00 GetODMCode 2 ODMCode
110 * 0x08/0x00 GetPressureLevels 2 =512
111 * 0x04/0x00 GetFirmwareVersion 2 Firmware Version
112 * 0x11/0x02 EnableMacroKeys 0
115 * To initialize the tablet:
117 * (1) Send Resolution500LPI (Command)
118 * (2) Query for Model code (Option Report)
119 * (3) Query for ODM code (Option Report)
120 * (4) Query for firmware (Option Report)
121 * (5) Query for GetXExtension (Option Report)
122 * (6) Query for GetYExtension (Option Report)
123 * (7) Query for GetPressureLevels (Option Report)
124 * (8) SwitchToTablet for Absolute coordinates, or
125 * SwitchToMouse for Relative coordinates (Command)
126 * (9) EnableMacroKeys (Command)
127 * (10) FilterOn (Command)
128 * (11) AutoGainOn (Command)
130 * (Step 9 can be omitted, but you'll then have no function keys.)
132 * The procfs interface
133 * --------------------
135 * This driver supports delivering configuration/status reports
136 * through {procfs}/driver/usb/aiptek. ("procfs" is normally mounted
137 * to /proc.) Said file can be found while the driver is active in
138 * memory; it will be removed when the driver is removed, either
139 * through user intervention (rmmod aiptek) or through software
142 * Reading from the Procfs interface
143 * ---------------------------------
145 * The user may determine the status of the tablet by reading the
146 * report in the procfs interface, /proc/driver/usb/aiptek.
147 * The report as of driver version 1.0, looks like,
149 * Aiptek Tablet (3000x2250, 8.00x6.00", 202x152mm)
150 * (USB VendorID 0x08ca, ProductID 0x0020, ODMCode 0x0004
151 * ModelCode: 0x64, FirmwareCode: 0x0400)
152 * on /dev/input/event0
154 * coordinate=absolute
162 * (spurious ", for the benefit of vim's syntax highlighting.)
164 * This report indicates the tablet recognized. (Because Aiptek reuses
165 * the USB 'productID' over several tablets, it's pointless for us to
166 * guess which model you have: we'll instead tell you the size of
167 * the tablet's drawing area, which we indicate in coordinates, inches,
168 * and millimeters.) We also indicate datum read from the USB interface,
169 * such as vendorId, productId, ODMcode, etc. It's there "just in case."
171 * on /dev/input/event0
173 * Linux supports HID-compliant USB devices (such as this tablet) by
174 * transposing their reports to the Linux Input Event System format. Which
175 * means, if you want to data from the tablet, that's where it will be
176 * made available from. For information on the Input Event System, see
177 * the docs in ./Documentation/input, in the kernel source tree.
179 * And yes, depending on the order in which other supported Input Event
180 * devices are recognized and configured, the tablet may be allocated
181 * to a different device driver name: it's all dynamic. Use of the devfs
182 * file system is a help.
184 * The keyword=value part of the report mostly shows what the programmable
185 * parameters have been set to. We describe those below, and how to
186 * program/reprogram them. Note: tablet parameters are to be programmed
187 * while the tablet is attached and active. They are not set as arguments
188 * to the kernel during bootup.
190 * Here are the "read-only" parameters, and what they mean:
192 * diagnostic=stringValue
193 * eventsReceived=numericValue
195 * diagnostic: The tablet driver attempts to explain why things are not
196 * working correctly. (To the best of it's insular abilities)
198 * By default, the tablet boots up in Relative Coordinate
199 * mode. This driver initially attempts to program it in Absolute
200 * Coordinate mode (and of course, the user can subsequently choose
201 * which mode they want.) So, therefore, the situation can arise
202 * where the tablet is in one mode, and the driver believes it
203 * is in the other mode. The driver, however, cannot divine
204 * this mismatch until input events are received.
205 * Two reports indicate such mode-mismatches between the tablet
206 * and the driver, and are,
208 * "tablet sending relative reports"
209 * "tablet sending absolute reports"
211 * The next diagnostic operates in conjunction with the "pointer="
212 * programmable parameter. With it, you can indicate that you want
213 * the tablet to only accept reports from the stylus, or only from the
214 * mouse. (You can also specify to allow reports from either.) What
215 * happens when you specify that you only want mouse reports, yet
216 * the tablet keeps receiving reports from the stylus? Well, first,
217 * it's a "pilot error", but secondly, it tries to diagnose the issue
218 * with the following reports,
220 * "tablet seeing reports from stylus"
221 * "tablet seeing reports from mouse"
223 * What if there is nothing to report? The inference in the diagnostic
224 * reports is that something is happening which shouldn't: when things
225 * appear to be working right, the report is,
229 * The error diagnostic report is dynamic: it only reports issues
230 * that are happening, or have happened as of the last event received.
231 * It will reset following any attempt to reprogram the tablet's mode.
233 * eventsReceived: Occasionally, your movements on the tablet are not being
234 * reported. Usually, this indicates that your tablet is out of sync
235 * with the USB interface driver, or itself is not sending reports
236 * out. To help diagnose this, we keep an active count of events
237 * received from the tablet. So, if you move the stylus, and yet
238 * your client application doesn't notice, make
239 * note of the eventsReceived, and then move the stylus again. If the
240 * event counter's number doesn't change, then the tablet indeed has
243 * We have found that sending the tablet a command sequence often
244 * will clear up "frozen" tablets. Which segues into the section
245 * about how to program your tablet through the procfs interface,
247 * Writing to the procfs interface
248 * -------------------------------
250 * The user may configure the tablet by writing ASCII
251 * commands to the /proc/driver/usb/aiptek file. Commands which are
254 * pointer=stringvalue {stylus|mouse|either}
255 * coordinate=stringvalue {absolute|relative}
256 * tool=stringvalue {mouse|rubber|pen|pencil|brush|airbrush}
257 * xtilt=string_or_numeric {disable|[-128..127]}
258 * ytilt=string_or_numeric {disable|[-128..127]}
259 * jitter=numericvalue {0..xxx}
261 * pointer: you can specify that reports are to be excepted ONLY from the
262 * stylus, or ONLY from the mouse. 'either' allows reports from either
263 * device to be accepted, and is the default.
264 * coordinate: you can specify that either absolute or relative coordinate
265 * reports are issued by the tablet. By default, absolute reports are
267 * tool: The stylus by default prepends TOOL_BTN_PEN events with it's
268 * reports. But you may decide that you want your stylus to behave
269 * like an eraser (named 'rubber', following tablet conventions,)
270 * or a pencil, etc. The behavior is dependent upon the client software
271 * consuming the tablet's events, e.g., the XFree86 tablet driver.
272 * xtilt: By default this is disabled. However, other tablets have a notion
273 * of measuring the angle at which the stylus pen is held against the
274 * drawing surface, along the X axis. Aiptek tablets cannot sense this,
275 * but if you want to send "held-at-angle" reports, specify the value,
276 * an integer between -128 and 127 (inclusive) that you want to send.
277 * This data will be sent along with regular tablet input. Obviously,
278 * the inference here is that your hand does not change angles
279 * while drawing (until you go back to this procfs interface, and
282 * When you consider actual drawing tools (real pens, brushes),
283 * knowing the tools' tip shape and the angle that you hold the tool
284 * becomes important, insofar as calculating the surface of the tip
285 * that actually touches the surface of the paper. Knowledge of what
286 * to do with xtilt reports is solely in the realm of your client
289 * Yes, there is a difference between xtilt=0 and xtilt=disable
290 * settings. The former sends a report that the angle is a 0;
291 * the other indicates that NO xtilt reports are to be sent at all.
292 * ytilt: By default this is disabled. This provides similar functionality
293 * to xtilt, except that we're measuring the angle the stylus pen is
294 * held against the drawing surface, along the Y axis. Same cavaets
295 * apply as for xtilt.
296 * jitter: By default, this is set to 50. When pressing a button on
297 * either the mouse or the stylus pen, you will probably notice that
298 * the tool moves slightly from it's original position, until your
299 * hand steadies it. During that period of time, the pen is "jittering",
300 * sending spurious movement events that perhaps you'd like it not to
301 * send. What we do is set a moratorium, measured in milliseconds,
302 * during which we do not send movement events. So, the default is 50ms;
303 * you obviously can set it to zero or incredibly unreasonable values
304 * (no reports for 4 seconds following the pressing of a stylus button!)
306 * Interesting Side-Note
307 * ---------------------
309 * The tablet has "frozen" and you'd like to send it a command to wake it
310 * up. But you don't want to change how the driver's currently configured.
312 * 1. Send a command to /proc/driver/usb/aiptek with the same setting
313 * already reported by the driver.
314 * 2. Send an illegal string to procfs file ("wakeup=now" is always good)
315 * 3. Because, the driver always attempts to reprogram the tablet to it's
316 * current settings following a write to the procfs interface.
318 * Hmm, still does not work.
319 * -------------------------
321 * This is slightly harder to diagnose. You may be receiving frame errors
322 * from the USB interface driver (see /var/log/messages for any diagnostics).
324 * Alternatively, you may be running something like 'hotplug' that attempts
325 * to match discovered USB devices to it's list of device drivers.
326 * Unfortunately, because this is a tablet that can send relative X,Y events,
327 * it "looks like" a mouse! A usb mouse driver may have possession of
328 * input from the tablet. On the other hand, the tablet also supports
329 * absolute reports from barrel switches, which sounds a lot like a "joystick",
330 * and the software again can be fooled into loading the wrong driver for
331 * the tablet. The distinction is, USB HID devices tell you what they
332 * are capable of, rather than what they are.
334 * Come visit this driver's home page at http://aiptektablet.sourceforge.net
335 * for further assistance.
338 #define USB_VENDOR_ID_AIPTEK 0x08ca
340 #define AIPTEK_POINTER_ONLY_MOUSE_MODE 0
341 #define AIPTEK_POINTER_ONLY_STYLUS_MODE 1
342 #define AIPTEK_POINTER_EITHER_MODE 2
344 #define AIPTEK_POINTER_ALLOW_MOUSE_MODE(a) \
345 (a == AIPTEK_POINTER_ONLY_MOUSE_MODE || \
346 a == AIPTEK_POINTER_EITHER_MODE)
347 #define AIPTEK_POINTER_ALLOW_STYLUS_MODE(a) \
348 (a == AIPTEK_POINTER_ONLY_STYLUS_MODE || \
349 a == AIPTEK_POINTER_EITHER_MODE)
351 #define AIPTEK_COORDINATE_RELATIVE_MODE 0
352 #define AIPTEK_COORDINATE_ABSOLUTE_MODE 1
354 #define AIPTEK_TILT_MIN (-128)
355 #define AIPTEK_TILT_MAX 127
356 #define AIPTEK_TILT_DISABLE (-10101)
358 #define AIPTEK_TOOL_BUTTON_PEN_MODE 0
359 #define AIPTEK_TOOL_BUTTON_PENCIL_MODE 1
360 #define AIPTEK_TOOL_BUTTON_BRUSH_MODE 2
361 #define AIPTEK_TOOL_BUTTON_AIRBRUSH_MODE 3
362 #define AIPTEK_TOOL_BUTTON_RUBBER_MODE 4
363 #define AIPTEK_TOOL_BUTTON_MOUSE_MODE 5
365 #define AIPTEK_DIAGNOSTIC_NA 0
366 #define AIPTEK_DIAGNOSTIC_SENDING_RELATIVE_IN_ABSOLUTE 1
367 #define AIPTEK_DIAGNOSTIC_SENDING_ABSOLUTE_IN_RELATIVE 2
368 #define AIPTEK_DIAGNOSTIC_TOOL_DISALLOWED 3
370 // Time to wait (in ms) to help mask hand jittering
371 // when pressing the stylus buttons.
372 #define AIPTEK_JITTER_DELAY_DEFAULT 50
374 struct aiptek_features {
383 void (*irq) (struct urb * urb);
387 signed char data[10];
388 struct input_dev dev;
389 struct usb_device *usbdev;
391 struct aiptek_features *features;
400 unsigned long eventCount;
402 #ifdef CONFIG_PROC_FS
403 struct proc_dir_entry *usbProcfsEntry;
404 struct proc_dir_entry *aiptekProcfsEntry;
409 * Permit easy lookup of keyboard events to send, versus
410 * the bitmap which comes from the tablet. This hides the
411 * issue that the F_keys are not sequentially numbered.
413 static int macroKeyEvents[] = { KEY_F1, KEY_F2, KEY_F3, KEY_F4, KEY_F5, KEY_F6,
414 KEY_F7, KEY_F8, KEY_F9, KEY_F10, KEY_F11, KEY_F12,
415 KEY_F13, KEY_F14, KEY_F15, KEY_F16, KEY_F17, KEY_F18,
416 KEY_F19, KEY_F20, KEY_F21, KEY_F22, KEY_F23, KEY_F24,
417 KEY_STOP, KEY_AGAIN, KEY_PROPS, KEY_UNDO, KEY_FRONT, KEY_COPY,
418 KEY_OPEN, KEY_PASTE, 0
421 #ifdef CONFIG_PROC_FS
422 extern struct proc_dir_entry *proc_root_driver;
426 aiptek_convert_from_2s_complement(unsigned char c)
438 ret = (negate == 1) ? -ret : ret;
443 * aiptek_irq can receive one of six potential reports.
444 * The documentation for each is in the body of the function.
446 * The tablet reports on several attributes per invocation of
447 * aiptek_irq. Because the Linux Input Event system allows the
448 * transmission of ONE attribute per input_report_xxx() call,
449 * collation has to be done on the other end to reconstitute
450 * a complete tablet report. Further, the number of Input Event reports
451 * submitted varies, depending on what USB report type, and circumstance.
452 * To deal with this, EV_MSC is used to indicate an 'end-of-report'
453 * message. This has been an undocumented convention understood by the kernel
454 * tablet driver and clients such as gpm and XFree86's tablet drivers.
456 * Of the information received from the tablet, the one piece I
457 * cannot transmit is the proximity bit (without resorting to an EV_MSC
458 * convention above.) I therefore have taken over REL_MISC and ABS_MISC
459 * (for relative and absolute reports, respectively) for communicating
460 * Proximity. Why two events? I thought it interesting to know if the
461 * Proximity event occured while the tablet was in absolute or relative
464 * Other tablets use the notion of a certain minimum stylus pressure
465 * to infer proximity. While that could have been done, that is yet
466 * another 'by convention' behavior, the documentation for which
467 * would be spread between two (or more) pieces of software.
469 * EV_MSC usage is terminated in Linux 2.5.x.
473 aiptek_irq(struct urb *urb)
475 struct aiptek *aiptek = urb->context;
476 unsigned char *data = aiptek->data;
477 struct input_dev *dev = &aiptek->dev;
483 aiptek->eventCount++;
485 // Report 1 delivers relative coordinates with either a stylus
486 // or the mouse. You do not know which tool generated the event.
488 if (aiptek->coordinate_mode == AIPTEK_COORDINATE_ABSOLUTE_MODE) {
490 AIPTEK_DIAGNOSTIC_SENDING_RELATIVE_IN_ABSOLUTE;
492 int x, y, left, right, middle;
494 if (aiptek->tool_mode != AIPTEK_TOOL_BUTTON_MOUSE_MODE) {
496 AIPTEK_TOOL_BUTTON_MOUSE_MODE;
497 input_report_key(dev, BTN_TOOL_MOUSE, 1);
499 x = aiptek_convert_from_2s_complement(data[2]);
500 y = aiptek_convert_from_2s_complement(data[3]);
502 left = data[5] & 0x01;
503 right = data[5] & 0x02;
504 middle = data[5] & 0x04;
506 jitterable = left | right | middle;
508 input_report_key(dev, BTN_LEFT, left);
509 input_report_key(dev, BTN_MIDDLE, middle);
510 input_report_key(dev, BTN_RIGHT, right);
511 input_report_rel(dev, REL_X, x);
512 input_report_rel(dev, REL_Y, y);
513 input_report_rel(dev, REL_MISC, 1);
515 input_event(dev, EV_MSC, MSC_SERIAL, 0);
518 // Report 2 is delivered only by the stylus, and delivers
519 // absolute coordinates.
520 else if (data[0] == 2) {
521 if (aiptek->coordinate_mode == AIPTEK_COORDINATE_RELATIVE_MODE) {
523 AIPTEK_DIAGNOSTIC_SENDING_ABSOLUTE_IN_RELATIVE;
525 if (!AIPTEK_POINTER_ALLOW_STYLUS_MODE(aiptek->pointer_mode))
527 aiptek->diagnostic = AIPTEK_DIAGNOSTIC_TOOL_DISALLOWED;
529 int x = ((__u32) data[1]) | ((__u32) data[2] << 8);
530 int y = ((__u32) data[3]) | ((__u32) data[4] << 8);
531 int z = ((__u32) data[6]) | ((__u32) data[7] << 8);
533 int p = data[5] & 0x01;
534 int dv = data[5] & 0x02;
535 int tip = data[5] & 0x04;
536 int bs = data[5] & 0x08;
537 int pck = data[5] & 0x10;
539 // dv indicates 'data valid' (e.g., the tablet is in sync
540 // and has delivered a "correct" report) We will ignore
541 // all 'bad' reports...
543 switch (aiptek->tool_mode) {
544 case AIPTEK_TOOL_BUTTON_PEN_MODE:
546 input_report_key(dev,
552 case AIPTEK_TOOL_BUTTON_PENCIL_MODE:
554 input_report_key(dev,
560 case AIPTEK_TOOL_BUTTON_BRUSH_MODE:
562 input_report_key(dev,
568 case AIPTEK_TOOL_BUTTON_AIRBRUSH_MODE:
570 input_report_key(dev,
576 case AIPTEK_TOOL_BUTTON_RUBBER_MODE:
578 input_report_key(dev,
584 case AIPTEK_TOOL_BUTTON_MOUSE_MODE:
586 input_report_key(dev,
593 input_report_abs(dev, ABS_X, x);
594 input_report_abs(dev, ABS_Y, y);
597 * The user is allowed to switch from one of the
598 * stylus tools to the Mouse using the front-end GUI.
599 * An issue that will arise, however, is what happens
600 * when the user HAS issued a TOOL_BTN_MOUSE, but has not
601 * yet swapped tools. Well, we can "pretend" to be a mouse
602 * by sending overriding tip, barrelswitch and pick.
603 * This stupidity should not be used as an excuse not
604 * to physically move your Aiptek mouse into the tablet's
605 * active area -- it merely provides momentary convenience
606 * during that transition.
608 if (aiptek->tool_mode ==
609 AIPTEK_TOOL_BUTTON_MOUSE_MODE) {
610 input_report_key(dev, BTN_LEFT, tip);
611 input_report_key(dev, BTN_RIGHT, bs);
612 input_report_key(dev, BTN_MIDDLE, pck);
614 jitterable = tip | bs | pck;
616 input_report_abs(dev, ABS_PRESSURE, z);
618 input_report_key(dev, BTN_TOUCH, tip);
619 input_report_key(dev, BTN_STYLUS, bs);
620 input_report_key(dev, BTN_STYLUS2, pck);
622 jitterable = tip | bs | pck;
626 input_report_abs(dev,
631 input_report_abs(dev,
635 input_report_abs(dev, ABS_MISC, p);
636 input_event(dev, EV_MSC, MSC_SERIAL, 0);
640 // Report 3's come from the mouse in absolute mode.
641 else if (data[0] == 3) {
642 if (aiptek->coordinate_mode == AIPTEK_COORDINATE_RELATIVE_MODE) {
644 AIPTEK_DIAGNOSTIC_SENDING_ABSOLUTE_IN_RELATIVE;
646 if (!AIPTEK_POINTER_ALLOW_MOUSE_MODE(aiptek->pointer_mode))
648 aiptek->diagnostic = AIPTEK_DIAGNOSTIC_TOOL_DISALLOWED;
650 int x = ((__u32) data[1]) | ((__u32) data[2] << 8);
651 int y = ((__u32) data[3]) | ((__u32) data[4] << 8);
652 int p = data[5] & 0x01;
653 int dv = data[5] & 0x02;
654 int left = data[5] & 0x04;
655 int right = data[5] & 0x08;
656 int middle = data[5] & 0x10;
659 input_report_key(dev, BTN_TOOL_MOUSE, 1);
660 input_report_abs(dev, ABS_X, x);
661 input_report_abs(dev, ABS_Y, y);
663 input_report_key(dev, BTN_LEFT, left);
664 input_report_key(dev, BTN_MIDDLE, middle);
665 input_report_key(dev, BTN_RIGHT, right);
667 jitterable = left | middle | right;
669 input_report_rel(dev, REL_MISC, p);
670 input_event(dev, EV_MSC, MSC_SERIAL, 0);
674 // Report 4s come from the macro keys when pressed by stylus
675 else if (data[0] == 4) {
676 int p = data[1] & 0x01;
677 int dv = data[1] & 0x02;
678 int tip = data[1] & 0x04;
679 int bs = data[1] & 0x08;
680 int pck = data[1] & 0x10;
683 int z = ((__u32) data[4]) | ((__u32) data[5] << 8);
686 input_report_key(dev, BTN_TOUCH, tip);
687 input_report_key(dev, BTN_STYLUS, bs);
688 input_report_key(dev, BTN_STYLUS2, pck);
690 jitterable = tip | bs | pck;
692 input_report_key(dev, macroKeyEvents[m - 1], 1);
693 input_report_abs(dev, ABS_PRESSURE, z);
694 input_report_abs(dev, ABS_MISC, p);
695 input_event(dev, EV_MSC, MSC_SERIAL, 0);
698 // Report 5s come from the macro keys when pressed by mouse
699 else if (data[0] == 5) {
700 int p = data[1] & 0x01;
701 int dv = data[1] & 0x02;
702 int left = data[1] & 0x04;
703 int right = data[1] & 0x08;
704 int middle = data[1] & 0x10;
708 input_report_key(dev, BTN_LEFT, left);
709 input_report_key(dev, BTN_MIDDLE, middle);
710 input_report_key(dev, BTN_RIGHT, right);
712 jitterable = left | middle | right;
714 input_report_key(dev, macroKeyEvents[macro - 1], 1);
715 input_report_rel(dev, ABS_MISC, p);
716 input_event(dev, EV_MSC, MSC_SERIAL, 0);
719 // We have no idea which tool can generate a report 6. Theoretically,
720 // neither need to, having been given reports 4 & 5 for such use.
721 // However, report 6 is the 'official-looking' report for macroKeys;
722 // reports 4 & 5 supposively are used to support unnamed, unknown
723 // hat switches (which just so happen to be the macroKeys.)
724 else if (data[0] == 6) {
725 int macro = ((__u32) data[1]) | ((__u32) data[2] << 8);
727 input_report_key(dev, macroKeyEvents[macro - 1], 1);
728 input_report_abs(dev, ABS_MISC, 1);
729 input_event(dev, EV_MSC, MSC_SERIAL, 0);
731 dbg("Unknown report %d", data[0]);
734 // Jitter may occur when the user presses a button on the stlyus
735 // or the mouse. What we do to prevent that is wait 'x' milliseconds
736 // following a 'jitterable' event, which should give the hand some time
738 if (jitterable != 0 && aiptek->jitterDelay != 0) {
739 wait_ms(aiptek->jitterDelay);
744 * We are not able to reliably determine the tablet featureset by
745 * asking for the USB productID. Therefore, we will query the
746 * tablet dynamically and populate the struct in aiptek_probe().
749 struct aiptek_features aiptek_features[] = {
750 {"Aiptek", 8, 0, 0, 0, 0, 0, 0, aiptek_irq},
755 * These are the USB id's known so far. We do not identify them to
756 * specific Aiptek model numbers, because there has been overlaps,
757 * use, and reuse of id's in existing models. Certain models have
758 * been known to use more than one ID, indicative perhaps of
759 * manufacturing revisions. In any event, we consider these
760 * IDs to not be model-specific nor unique.
763 struct usb_device_id aiptek_ids[] = {
764 {USB_DEVICE(USB_VENDOR_ID_AIPTEK, 0x01), driver_info:0},
765 {USB_DEVICE(USB_VENDOR_ID_AIPTEK, 0x10), driver_info:0},
766 {USB_DEVICE(USB_VENDOR_ID_AIPTEK, 0x20), driver_info:0},
767 {USB_DEVICE(USB_VENDOR_ID_AIPTEK, 0x21), driver_info:0},
768 {USB_DEVICE(USB_VENDOR_ID_AIPTEK, 0x22), driver_info:0},
769 {USB_DEVICE(USB_VENDOR_ID_AIPTEK, 0x23), driver_info:0},
770 {USB_DEVICE(USB_VENDOR_ID_AIPTEK, 0x24), driver_info:0},
774 MODULE_DEVICE_TABLE(usb, aiptek_ids);
777 aiptek_open(struct input_dev *dev)
779 struct aiptek *aiptek = dev->private;
780 if (aiptek->open_count++)
783 aiptek->irq->dev = aiptek->usbdev;
784 if (usb_submit_urb(aiptek->irq))
791 aiptek_close(struct input_dev *dev)
793 struct aiptek *aiptek = dev->private;
795 if (!--aiptek->open_count)
796 usb_unlink_urb(aiptek->irq);
800 * Send a command to the tablet. No reply is expected.
803 aiptek_command(struct usb_device *dev, unsigned int ifnum,
804 unsigned char command, unsigned char data)
812 if (usb_set_report(dev, ifnum, 3, 2, buf, sizeof (buf)) != sizeof (buf)) {
813 dbg("aiptek_command failed, sending: 0x%02x 0x%02x", command,
819 * Send a query to the tablet. This is done by sending the query stream
820 * first as a command, waiting a few milliseconds, then submitting the
821 * same stream as a query.
824 aiptek_query(struct usb_device *dev, unsigned int ifnum,
825 unsigned char command, unsigned char data)
833 aiptek_command(dev, ifnum, command, data);
836 if (usb_get_report(dev, ifnum, 3, 2, buf, 3) < 3) {
837 dbg("aiptek_query failed: returns 0x%02x 0x%02x 0x%02x",
838 buf[0], buf[1], buf[2]);
841 ret = ((__u32) buf[1]) | ((__u32) buf[2] << 8);
846 * Program the tablet into either absolute or relative mode.
848 * We also get information about the tablet's size.
851 aiptek_program_tablet(struct aiptek *aiptek)
853 int modelCode, odmCode, firmwareCode;
854 int xResolution, yResolution, zResolution;
856 aiptek->diagnostic = AIPTEK_DIAGNOSTIC_NA;
858 // execute Resolution500LPI
859 aiptek_command(aiptek->usbdev, aiptek->ifnum, 0x18, 0x04);
860 // query getModelCode
861 modelCode = aiptek_query(aiptek->usbdev, aiptek->ifnum, 0x02, 0x00);
863 odmCode = aiptek_query(aiptek->usbdev, aiptek->ifnum, 0x03, 0x00);
864 // query getFirmwareCode
865 firmwareCode = aiptek_query(aiptek->usbdev, aiptek->ifnum, 0x04, 0x00);
866 // query getXextension
867 xResolution = aiptek_query(aiptek->usbdev, aiptek->ifnum, 0x01, 0x00);
868 // query getYextension
869 yResolution = aiptek_query(aiptek->usbdev, aiptek->ifnum, 0x01, 0x01);
870 // query getPressureLevels
871 zResolution = aiptek_query(aiptek->usbdev, aiptek->ifnum, 0x08, 0x00);
873 // Depending on whether we are in absolute or relative mode, we will
874 // do a switchToTablet(absolute) or switchToMouse(relative) command.
875 if (aiptek->coordinate_mode == AIPTEK_COORDINATE_ABSOLUTE_MODE) {
876 // execute switchToTablet
877 aiptek_command(aiptek->usbdev, aiptek->ifnum, 0x10, 0x01);
879 // execute switchToMouse
880 aiptek_command(aiptek->usbdev, aiptek->ifnum, 0x10, 0x00);
882 // This command enables the macro keys
883 aiptek_command(aiptek->usbdev, aiptek->ifnum, 0x11, 0x02);
885 aiptek_command(aiptek->usbdev, aiptek->ifnum, 0x17, 0x00);
886 // execute AutoGainOn
887 aiptek_command(aiptek->usbdev, aiptek->ifnum, 0x12, 0xff);
889 aiptek->features->odmCode = odmCode;
890 aiptek->features->modelCode = modelCode & 0xff;
891 aiptek->features->firmwareCode = firmwareCode;
892 aiptek->features->pressure_max = zResolution;
893 aiptek->features->x_max = xResolution;
894 aiptek->features->y_max = yResolution;
896 aiptek->eventCount = 0;
899 #if defined(CONFIG_PROC_FS)
901 * This routine determines keywords and their associated values, and
902 * maps them to supported modes in this driver. It's input comes from
903 * aiptek_procfs_write().
906 aiptek_procfs_parse(struct aiptek *aiptek, char *keyword, char *value)
908 if (strcmp(keyword, "pointer") == 0) {
909 if (strcmp(value, "stylus") == 0) {
910 aiptek->pointer_mode = AIPTEK_POINTER_ONLY_STYLUS_MODE;
911 } else if (strcmp(value, "mouse") == 0) {
912 aiptek->pointer_mode = AIPTEK_POINTER_ONLY_MOUSE_MODE;
913 } else if (strcmp(value, "either") == 0) {
914 aiptek->pointer_mode = AIPTEK_POINTER_EITHER_MODE;
916 } else if (strcmp(keyword, "coordinate") == 0) {
917 if (strcmp(value, "relative") == 0) {
918 aiptek->coordinate_mode =
919 AIPTEK_COORDINATE_RELATIVE_MODE;
920 } else if (strcmp(value, "absolute") == 0) {
921 aiptek->coordinate_mode =
922 AIPTEK_COORDINATE_ABSOLUTE_MODE;
924 } else if (strcmp(keyword, "xtilt") == 0) {
925 if (strcmp(value, "disable") == 0) {
926 aiptek->xTilt = AIPTEK_TILT_DISABLE;
928 int x = (int) simple_strtol(value, 0, 10);
929 if (x >= AIPTEK_TILT_MIN && x <= AIPTEK_TILT_MAX)
932 } else if (strcmp(keyword, "ytilt") == 0) {
933 if (strcmp(value, "disable") == 0) {
934 aiptek->yTilt = AIPTEK_TILT_DISABLE;
936 int y = (int) simple_strtol(value, 0, 10);
937 if (y >= AIPTEK_TILT_MIN && y <= AIPTEK_TILT_MAX)
940 } else if (strcmp(keyword, "jitter") == 0) {
941 aiptek->jitterDelay = (int) simple_strtol(value, 0, 10);
942 } else if (strcmp(keyword, "tool") == 0) {
943 if (strcmp(value, "mouse") == 0) {
944 aiptek->tool_mode = AIPTEK_TOOL_BUTTON_MOUSE_MODE;
945 } else if (strcmp(value, "rubber") == 0) {
946 aiptek->tool_mode = AIPTEK_TOOL_BUTTON_RUBBER_MODE;
947 } else if (strcmp(value, "pencil") == 0) {
948 aiptek->tool_mode = AIPTEK_TOOL_BUTTON_PENCIL_MODE;
949 } else if (strcmp(value, "pen") == 0) {
950 aiptek->tool_mode = AIPTEK_TOOL_BUTTON_PEN_MODE;
951 } else if (strcmp(value, "brush") == 0) {
952 aiptek->tool_mode = AIPTEK_TOOL_BUTTON_BRUSH_MODE;
953 } else if (strcmp(value, "airbrush") == 0) {
954 aiptek->tool_mode = AIPTEK_TOOL_BUTTON_AIRBRUSH_MODE;
960 * This routine reads the status of the aiptek driver, and makes it
961 * available as a procfs file. The description of the procfs file
962 * is at the top of this driver source code.
965 aiptek_procfs_read(char *page, char **start, off_t offset, int count,
966 int *eof, void *data)
970 struct aiptek *aiptek = data;
973 sprintf(out, "Aiptek Tablet (%dx%d)\n",
974 aiptek->features->x_max, aiptek->features->y_max);
978 "(USB VendorID 0x%04x, ProductID 0x%04x, ODMCode 0x%04x\n",
979 aiptek->dev.idvendor, aiptek->dev.idproduct,
980 aiptek->features->odmCode);
982 sprintf(out, " ModelCode: 0x%02x, FirmwareCode: 0x%04x)\n",
983 aiptek->features->modelCode,
984 aiptek->features->firmwareCode);
986 out += sprintf(out, "on /dev/input/event%d\n", aiptek->dev.number);
987 out += sprintf(out, "pointer=%s\n",
988 (aiptek->pointer_mode == AIPTEK_POINTER_ONLY_MOUSE_MODE
990 : (aiptek->pointer_mode ==
991 AIPTEK_POINTER_ONLY_STYLUS_MODE ? "stylus" :
994 sprintf(out, "coordinate=%s\n",
995 (aiptek->coordinate_mode ==
996 AIPTEK_COORDINATE_RELATIVE_MODE ? "relative" :
999 out += sprintf(out, "tool=");
1000 switch (aiptek->tool_mode) {
1001 case AIPTEK_TOOL_BUTTON_MOUSE_MODE:
1002 out += sprintf(out, "mouse\n");
1005 case AIPTEK_TOOL_BUTTON_RUBBER_MODE:
1006 out += sprintf(out, "rubber\n");
1009 case AIPTEK_TOOL_BUTTON_PEN_MODE:
1010 out += sprintf(out, "pen\n");
1013 case AIPTEK_TOOL_BUTTON_PENCIL_MODE:
1014 out += sprintf(out, "pencil\n");
1017 case AIPTEK_TOOL_BUTTON_BRUSH_MODE:
1018 out += sprintf(out, "brush\n");
1021 case AIPTEK_TOOL_BUTTON_AIRBRUSH_MODE:
1022 out += sprintf(out, "airbrush\n");
1026 out += sprintf(out, "xtilt=");
1027 if (aiptek->xTilt == AIPTEK_TILT_DISABLE) {
1028 out += sprintf(out, "disable\n");
1030 out += sprintf(out, "%d\n", aiptek->xTilt);
1033 out += sprintf(out, "ytilt=");
1034 if (aiptek->yTilt == AIPTEK_TILT_DISABLE) {
1035 out += sprintf(out, "disable\n");
1037 out += sprintf(out, "%d\n", aiptek->yTilt);
1040 out += sprintf(out, "jitter=%d\n", aiptek->jitterDelay);
1042 out += sprintf(out, "diagnostic=");
1043 switch (aiptek->diagnostic) {
1044 case AIPTEK_DIAGNOSTIC_NA:
1045 out += sprintf(out, "none\n");
1047 case AIPTEK_DIAGNOSTIC_SENDING_RELATIVE_IN_ABSOLUTE:
1048 out += sprintf(out, "tablet sending relative reports\n");
1050 case AIPTEK_DIAGNOSTIC_SENDING_ABSOLUTE_IN_RELATIVE:
1051 out += sprintf(out, "tablet sending absolute reports\n");
1053 case AIPTEK_DIAGNOSTIC_TOOL_DISALLOWED:
1054 out += sprintf(out, "tablet seeing reports from ");
1055 if (aiptek->pointer_mode == AIPTEK_POINTER_ONLY_MOUSE_MODE)
1056 out += sprintf(out, "stylus\n");
1058 out += sprintf(out, "mouse\n");
1062 out += sprintf(out, "eventsReceived=%lu\n", aiptek->eventCount);
1075 *start = page + offset;
1081 * This routine permits the setting of driver parameters through a
1082 * procfs file. Writing to the procfs file (/proc/driver/usb/aiptek),
1083 * you can program the tablet's behavior. Parameters that can be programmed
1084 * (and their legal values) are described at the top of this driver.
1087 * This parser is order-insensitive, and supports one or many parameters
1088 * to be sent in one write request. As many parameters as you may fit
1089 * in 64 bytes; we only require that you separate them with \n's.
1091 * Any command that is not understood by the parser is silently ignored.
1094 aiptek_procfs_write(struct file *file, const char *buffer, unsigned long count,
1099 char *keyword = NULL;
1101 struct aiptek *aiptek = data;
1104 num = (count < 64) ? count : 64;
1105 if (copy_from_user(buf, buffer, num))
1111 if (*scan == '\n' || *scan == '\0') {
1112 if (*scan == '\n') {
1116 if (keyword && value) {
1117 aiptek_procfs_parse(aiptek, keyword, value);
1124 if (*scan != '=' && keyword == NULL) {
1126 } else if (*scan == '=') {
1132 // We're insensitive as to whether the buffer ended in a \n or not.
1133 if (keyword && value) {
1134 aiptek_procfs_parse(aiptek, keyword, value);
1137 aiptek_program_tablet(aiptek);
1143 * This routine destroys our procfs device interface. This will occur
1144 * when you remove the driver, either through rmmod or the hotplug system.
1147 destroy_procfs_file(struct aiptek *aiptek)
1149 if (aiptek->aiptekProcfsEntry)
1150 remove_proc_entry("aiptek", aiptek->usbProcfsEntry);
1151 if (aiptek->usbProcfsEntry)
1152 remove_proc_entry("usb", proc_root_driver);
1154 aiptek->usbProcfsEntry = NULL;
1155 aiptek->aiptekProcfsEntry = NULL;
1159 * This routine builds the procfs file. The file is located at,
1160 * procfs/driver/usb/aiptek.
1163 create_procfs_file(struct aiptek *aiptek)
1165 // Make procfs/driver/usb directory
1166 aiptek->usbProcfsEntry = create_proc_entry("usb", S_IFDIR,
1168 if (!aiptek->usbProcfsEntry) {
1169 dbg("create_procfs_file failed; no procfs/driver/usb control file.");
1170 destroy_procfs_file(aiptek);
1173 aiptek->usbProcfsEntry->owner = THIS_MODULE;
1175 // Make procfs/driver/usb/aiptek file
1176 aiptek->aiptekProcfsEntry = create_proc_entry("aiptek",
1179 aiptek->usbProcfsEntry);
1180 if (!aiptek->aiptekProcfsEntry) {
1181 dbg("create_procfs_file failed; no procfs/driver/usb control file.");
1182 destroy_procfs_file(aiptek);
1185 aiptek->aiptekProcfsEntry->owner = THIS_MODULE;
1186 aiptek->aiptekProcfsEntry->data = aiptek;
1187 aiptek->aiptekProcfsEntry->read_proc = aiptek_procfs_read;
1188 aiptek->aiptekProcfsEntry->write_proc = aiptek_procfs_write;
1193 aiptek_probe(struct usb_device *dev, unsigned int ifnum,
1194 const struct usb_device_id *id)
1196 struct usb_endpoint_descriptor *endpoint;
1197 struct aiptek *aiptek;
1200 if (!(aiptek = kmalloc(sizeof (struct aiptek), GFP_KERNEL)))
1203 memset(aiptek, 0, sizeof (struct aiptek));
1205 aiptek->irq = usb_alloc_urb(0);
1210 // This used to be meaningful, when we had a matrix of
1211 // different models with statically-assigned different
1212 // features. Now we ask the tablet about everything.
1214 aiptek->features = aiptek_features;
1216 // Reset the tablet. The tablet boots up in 'SwitchtoMouse'
1217 // mode, which indicates relative coordinates. 'SwitchToTablet'
1218 // infers absolute coordinates. (Ergo, mice are inferred to be
1219 // relative-only devices, which is not true. A misnomer.)
1220 // The routine we use, aiptek_program_tablet, has been generalized
1221 // enough such that it's callable through the procfs interface.
1222 // This is why we use struct aiptek throughout.
1223 aiptek->usbdev = dev;
1224 aiptek->ifnum = ifnum;
1225 aiptek->pointer_mode = AIPTEK_POINTER_EITHER_MODE;
1226 aiptek->coordinate_mode = AIPTEK_COORDINATE_ABSOLUTE_MODE;
1227 aiptek->tool_mode = AIPTEK_TOOL_BUTTON_PEN_MODE;
1228 aiptek->xTilt = AIPTEK_TILT_DISABLE;
1229 aiptek->yTilt = AIPTEK_TILT_DISABLE;
1230 aiptek->jitterDelay = AIPTEK_JITTER_DELAY_DEFAULT;
1232 #ifdef CONFIG_PROC_FS
1233 create_procfs_file(aiptek);
1236 aiptek_program_tablet(aiptek);
1238 aiptek->dev.evbit[0] |= BIT(EV_KEY)
1242 aiptek->dev.absbit[0] |= BIT(ABS_X)
1249 aiptek->dev.relbit[0] |= BIT(REL_X)
1253 // Set the macro keys up. They are discontiguous, so it's better
1254 // to set the bitmask this way.
1256 for (i = 0; i < sizeof (macroKeyEvents) / sizeof (macroKeyEvents[0]);
1258 set_bit(macroKeyEvents[i], aiptek->dev.keybit);
1261 aiptek->dev.keybit[LONG(BTN_LEFT)] |= BIT(BTN_LEFT)
1265 aiptek->dev.keybit[LONG(BTN_DIGI)] |= BIT(BTN_TOOL_PEN)
1266 | BIT(BTN_TOOL_RUBBER)
1267 | BIT(BTN_TOOL_PENCIL)
1268 | BIT(BTN_TOOL_AIRBRUSH)
1269 | BIT(BTN_TOOL_BRUSH)
1270 | BIT(BTN_TOOL_MOUSE)
1275 aiptek->dev.mscbit[0] = BIT(MSC_SERIAL);
1277 aiptek->dev.absmax[ABS_X] = aiptek->features->x_max;
1278 aiptek->dev.absmax[ABS_Y] = aiptek->features->y_max;
1279 aiptek->dev.absmax[ABS_PRESSURE] = aiptek->features->pressure_max;
1280 aiptek->dev.absmax[ABS_TILT_X] = AIPTEK_TILT_MAX;
1281 aiptek->dev.absmax[ABS_TILT_Y] = AIPTEK_TILT_MAX;
1282 aiptek->dev.absfuzz[ABS_X] = 0;
1283 aiptek->dev.absfuzz[ABS_Y] = 0;
1285 aiptek->dev.private = aiptek;
1286 aiptek->dev.open = aiptek_open;
1287 aiptek->dev.close = aiptek_close;
1289 aiptek->dev.name = aiptek->features->name;
1290 aiptek->dev.idbus = BUS_USB;
1291 aiptek->dev.idvendor = dev->descriptor.idVendor;
1292 aiptek->dev.idproduct = dev->descriptor.idProduct;
1293 aiptek->dev.idversion = dev->descriptor.bcdDevice;
1294 aiptek->usbdev = dev;
1296 endpoint = dev->config[0].interface[ifnum].altsetting[0].endpoint + 0;
1298 usb_fill_int_urb(aiptek->irq,
1300 usb_rcvintpipe(dev, endpoint->bEndpointAddress),
1302 aiptek->features->pktlen,
1303 aiptek->features->irq, aiptek, endpoint->bInterval);
1305 input_register_device(&aiptek->dev);
1307 printk(KERN_INFO "input%d: %s on usb%d:%d.%d\n",
1309 aiptek->features->name, dev->bus->busnum, dev->devnum, ifnum);
1314 static struct usb_driver aiptek_driver;
1317 aiptek_disconnect(struct usb_device *dev, void *ptr)
1319 struct aiptek *aiptek = ptr;
1320 #ifdef CONFIG_PROC_FS
1321 destroy_procfs_file(aiptek);
1323 usb_unlink_urb(aiptek->irq);
1324 input_unregister_device(&aiptek->dev);
1325 usb_free_urb(aiptek->irq);
1329 static struct usb_driver aiptek_driver = {
1332 disconnect:aiptek_disconnect,
1333 id_table:aiptek_ids,
1339 usb_register(&aiptek_driver);
1340 info(DRIVER_VERSION ": " DRIVER_AUTHOR);
1348 usb_deregister(&aiptek_driver);
1351 module_init(aiptek_init);
1352 module_exit(aiptek_exit);