1 /*
2  * Copyright 1995-2002 by Frederic Lepied, France. <Lepied@XFree86.org>
3  * Copyright 2002-2013 by Ping Cheng, Wacom. <pingc@wacom.com>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19 
20 #ifndef __XF86_XF86WACOMDEFS_H
21 #define __XF86_XF86WACOMDEFS_H
22 
23 /*****************************************************************************
24  * General Defines
25  ****************************************************************************/
26 #include <wacom-util.h>
27 #ifdef __linux__
28 #include <asm/types.h>
29 #endif
30 #include <linux/input.h>
31 #define MAX_USB_EVENTS 32
32 
33 /* vendor IDs on the kernel device */
34 #define WACOM_VENDOR_ID 0x056a
35 #define WALTOP_VENDOR_ID 0x172F
36 #define NTRIG_VENDOR_ID 0x1b96
37 #define HANWANG_VENDOR_ID 0x0b57
38 #define LENOVO_VENDOR_ID 0x17ef
39 
40 #define DEFAULT_SUPPRESS 2      /* default suppress */
41 #define MAX_SUPPRESS 100        /* max value of suppress */
42 #define BUFFER_SIZE 256         /* size of reception buffer */
43 #define MAXTRY 3                /* max number of try to receive magic number */
44 #define MIN_ROTATION  -900      /* the minimum value of the marker pen rotation */
45 #define MAX_ROTATION_RANGE 1800 /* the maximum range of the marker pen rotation */
46 #define MAX_ABS_WHEEL 1023      /* the maximum value of absolute wheel */
47 
48 #define TILT_RES (180/M_PI)	/* Reported tilt resolution in points/radian
49 				   (1/degree) */
50 #define TILT_MIN -64		/* Minimum reported tilt value */
51 #define TILT_MAX 63		/* Maximum reported tilt value */
52 
53 /* I4 cursor tool has a rotation offset of 175 degrees */
54 #define INTUOS4_CURSOR_ROTATION_OFFSET 175
55 
56 /* Default max distance to the tablet at which a proximity-out event is generated for
57  * cursor device (e.g. mouse).
58  */
59 #define PROXOUT_INTUOS_DISTANCE		10
60 #define PROXOUT_GRAPHIRE_DISTANCE	42
61 
62 /* 2.6.28 */
63 
64 #ifndef BTN_TOOL_DOUBLETAP
65 #define BTN_TOOL_DOUBLETAP 0x14d
66 #endif
67 
68 #ifndef BTN_TOOL_TRIPLETAP
69 #define BTN_TOOL_TRIPLETAP 0x14e
70 #endif
71 
72 /* 2.6.30 */
73 
74 #ifndef ABS_MT_POSITION_X
75 #define ABS_MT_POSITION_X 0x35
76 #endif
77 
78 #ifndef ABS_MT_POSITION_Y
79 #define ABS_MT_POSITION_Y 0x36
80 #endif
81 
82 #ifndef ABS_MT_TRACKING_ID
83 #define ABS_MT_TRACKING_ID 0x39
84 #endif
85 
86 /* 2.6.33 */
87 
88 #ifndef ABS_MT_PRESSURE
89 #define ABS_MT_PRESSURE 0x3a
90 #endif
91 
92 /* 2.6.36 */
93 
94 #ifndef ABS_MT_SLOT
95 #define ABS_MT_SLOT 0x2f
96 #endif
97 
98 /* 4.15 */
99 
100 #ifndef BTN_STYLUS3
101 #define BTN_STYLUS3 0x149
102 #endif
103 
104 /******************************************************************************
105  * Forward Declarations
106  *****************************************************************************/
107 
108 typedef struct _WacomModel WacomModel, *WacomModelPtr;
109 typedef struct _WacomDeviceRec WacomDeviceRec, *WacomDevicePtr;
110 typedef struct _WacomDeviceState WacomDeviceState, *WacomDeviceStatePtr;
111 typedef struct _WacomChannel  WacomChannel, *WacomChannelPtr;
112 typedef struct _WacomCommonRec WacomCommonRec, *WacomCommonPtr;
113 typedef struct _WacomFilterState WacomFilterState, *WacomFilterStatePtr;
114 typedef struct _WacomDeviceClass WacomDeviceClass, *WacomDeviceClassPtr;
115 typedef struct _WacomTool WacomTool, *WacomToolPtr;
116 
117 /******************************************************************************
118  * WacomModel - model-specific device capabilities
119  *****************************************************************************/
120 
121 struct _WacomModel
122 {
123 	const char* name;
124 
125 	void (*Initialize)(WacomCommonPtr common, const char* id, float version);
126 	void (*GetResolution)(InputInfoPtr pInfo);
127 	int (*GetRanges)(InputInfoPtr pInfo);
128 	int (*Start)(InputInfoPtr pInfo);
129 	int (*Parse)(InputInfoPtr pInfo, const unsigned char* data, int len);
130 	int (*DetectConfig)(InputInfoPtr pInfo);
131 };
132 
133 /******************************************************************************
134  * WacomDeviceRec
135  *****************************************************************************/
136 
137 /* these device IDs are reported through ABS_MISC for Protocol 4 devices */
138 #define DEVICE_ID(flags) ((flags) & 0xff)
139 #define STYLUS_DEVICE_ID	0x02
140 #define TOUCH_DEVICE_ID		0x03
141 #define CURSOR_DEVICE_ID	0x06
142 #define ERASER_DEVICE_ID	0x0A
143 #define PAD_DEVICE_ID		0x0F
144 
145 #define STYLUS_ID		0x00000001
146 #define TOUCH_ID		0x00000002
147 #define CURSOR_ID		0x00000004
148 #define ERASER_ID		0x00000008
149 #define PAD_ID			0x00000010
150 
151 /* Each tablet may have one or more of the following
152  * features */
153 #define WCM_PEN			0x00000001 /* Tablet supports pens */
154 #define WCM_PAD			0x00000002 /* Has a pad tool */
155 
156 #define WCM_1FGT		0x00000004 /* One finger touch */
157 #define WCM_2FGT		0x00000008 /* Two finger touch */
158 #define WCM_STRIP		0x00000010 /* Tablet has menu strip (e.g.
159 					      Intuos3) */
160 #define WCM_RING		0x00000020 /* Tablet has touch ring (e.g.
161 					      Intuos4) */
162 #define WCM_DUALINPUT		0x00000040 /* Supports two tools on the
163 					      tablet simultaneously (Intuos
164 					      1 and 2) */
165 #define WCM_ROTATION		0x00000080 /* Needs to convert mouse tool
166 					      tilt to rotation */
167 #define WCM_LCD			0x00000100 /* Cintiqs and other display
168 					      tablets */
169 #define WCM_TPC			(0x00000200 | WCM_LCD) /* TabletPC (special
170 							  button handling,
171 							  always an LCD) */
172 #define WCM_PENTOUCH		0x00000400 /* Tablet supports pen and touch */
173 #define WCM_DUALRING		0x00000800 /* Tablet has two touch rings */
174 #define WCM_LEGACY_IDS		0x00001000 /* Tablet uses legacy device IDs */
175 #define TabletHasFeature(common, feature) MaskIsSet((common)->tablet_type, (feature))
176 #define TabletSetFeature(common, feature) MaskSet((common)->tablet_type, (feature))
177 
178 #define ABSOLUTE_FLAG		0x00000100
179 #define BAUD_19200_FLAG		0x00000400
180 #define BUTTONS_ONLY_FLAG	0x00000800
181 #define SCROLLMODE_FLAG		0x00001000
182 
183 #define IsCursor(priv) (DEVICE_ID((priv)->flags) == CURSOR_ID)
184 #define IsStylus(priv) (DEVICE_ID((priv)->flags) == STYLUS_ID)
185 #define IsTouch(priv)  (DEVICE_ID((priv)->flags) == TOUCH_ID)
186 #define IsEraser(priv) (DEVICE_ID((priv)->flags) == ERASER_ID)
187 #define IsPad(priv)    (DEVICE_ID((priv)->flags) == PAD_ID)
188 #define IsPen(priv)    (IsStylus(priv) || IsEraser(priv))
189 #define IsTablet(priv) (IsPen(priv) || IsCursor(priv))
190 
191 #define IsUSBDevice(common) ((common)->wcmDevCls == &gWacomUSBDevice)
192 
193 #define FILTER_PRESSURE_RES	65536	/* maximum points in pressure curve */
194 /* Tested result for setting the pressure threshold to a reasonable value */
195 #define THRESHOLD_TOLERANCE (0.008f)
196 #define DEFAULT_THRESHOLD (0.013f)
197 
198 #define WCM_MAX_BUTTONS		32	/* maximum number of tablet buttons */
199 #define WCM_MAX_X11BUTTON	127	/* maximum button number X11 can handle */
200 
201 #define AXIS_INVERT  0x01               /* Flag describing an axis which increases "downward" */
202 #define AXIS_BITWISE 0x02               /* Flag describing an axis which changes bitwise */
203 
204 /* Indicies into the wheel/strip default/keys/actions arrays */
205 #define WHEEL_REL_UP      0
206 #define WHEEL_REL_DN      1
207 #define WHEEL_ABS_UP      2
208 #define WHEEL_ABS_DN      3
209 #define WHEEL2_ABS_UP     4
210 #define WHEEL2_ABS_DN     5
211 #define STRIP_LEFT_UP     0
212 #define STRIP_LEFT_DN     1
213 #define STRIP_RIGHT_UP    2
214 #define STRIP_RIGHT_DN    3
215 
216 /******************************************************************************
217  * WacomDeviceState
218  *****************************************************************************/
219 struct _WacomDeviceState
220 {
221 	InputInfoPtr pInfo;
222 	int device_id;		/* tool id reported from the physical device */
223 	int device_type;
224 	unsigned int serial_num;
225 	int x;
226 	int y;
227 	int buttons;
228 	int pressure;
229 	int tiltx;
230 	int tilty;
231 	int stripx;
232 	int stripy;
233 	int rotation;
234 	int abswheel;
235 	int abswheel2;
236 	int relwheel;
237 	int distance;
238 	int throttle;
239 	int proximity;
240 	int sample;	/* wraps every 24 days */
241 	int time;
242 };
243 
244 static const struct _WacomDeviceState OUTPROX_STATE = {
245   .abswheel = INT_MAX,
246   .abswheel2 = INT_MAX
247 };
248 
249 struct _WacomDeviceRec
250 {
251 	char *name;		/* Do not move, same offset as common->device_path. Used by DBG macro */
252 	/* configuration fields */
253 	struct _WacomDeviceRec *next;
254 	InputInfoPtr pInfo;
255 	int debugLevel;
256 
257 	unsigned int flags;	/* various flags (type, abs, touch...) */
258 	int topX;		/* X top in device coordinates */
259 	int topY;		/* Y top in device coordinates */
260 	int bottomX;		/* X bottom in device coordinates */
261 	int bottomY;		/* Y bottom in device coordinates */
262 	int resolX;             /* X resolution */
263 	int resolY;             /* Y resolution */
264 	int minX;	        /* tool physical minX in device coordinates */
265 	int minY;	        /* tool physical minY in device coordinates */
266 	int maxX;	        /* tool physical maxX in device coordinates */
267 	int maxY;	        /* tool physical maxY in device coordinates */
268 	unsigned int serial;	/* device serial number this device takes (if 0, any serial is ok) */
269 	unsigned int cur_serial; /* current serial in prox */
270 	int cur_device_id;	/* current device ID in prox */
271 
272 	/* button mapping information
273 	 *
274 	 * 'button' variables are indexed by physical button number (0..nbuttons)
275 	 * 'strip' variables are indexed by STRIP_* defines
276 	 * 'wheel' variables are indexed by WHEEL_* defines
277 	 */
278 	int button_default[WCM_MAX_BUTTONS]; /* Default mappings set by ourselves (possibly overridden by xorg.conf) */
279 	int strip_default[4];
280 	int wheel_default[6];
281 	unsigned keys[WCM_MAX_BUTTONS][256]; /* Action codes to perform when the associated event occurs */
282 	unsigned strip_keys[4][256];
283 	unsigned wheel_keys[6][256];
284 	Atom btn_actions[WCM_MAX_BUTTONS];   /* Action references so we can update the action codes when a client makes a change */
285 	Atom strip_actions[4];
286 	Atom wheel_actions[6];
287 
288 	int nbuttons;           /* number of buttons for this subdevice */
289 	int naxes;              /* number of axes */
290 				/* FIXME: always 6, and the code relies on that... */
291 
292 	WacomCommonPtr common;  /* common info pointer */
293 
294 	/* state fields in device coordinates */
295 	struct _WacomDeviceState wcmPanscrollState; /* panscroll state tracking */
296 	struct _WacomDeviceState oldState; /* previous state information */
297 	int oldCursorHwProx;	/* previous cursor hardware proximity */
298 
299 	int maxCurve;		/* maximum pressure curve value */
300 	int *pPressCurve;       /* pressure curve */
301 	int nPressCtrl[4];      /* control points for curve */
302 	int minPressure;	/* the minimum pressure a pen may hold */
303 	int oldMinPressure;     /* to record the last minPressure before going out of proximity */
304 	int wcmSurfaceDist;	/* Distance reported by hardware when tool at surface */
305 	int wcmProxoutDist;     /* Distance from surface when proximity-out should be triggered */
306 	unsigned int eventCnt;  /* count number of events while in proximity */
307 	int maxRawPressure;     /* maximum 'raw' pressure seen until first button event */
308 	WacomToolPtr tool;         /* The common tool-structure for this device */
309 
310 	int isParent;		/* set to 1 if the device is not auto-hotplugged */
311 
312 	OsTimerPtr serial_timer; /* timer used for serial number property update */
313 	OsTimerPtr tap_timer;   /* timer used for tap timing */
314 	OsTimerPtr touch_timer; /* timer used for touch switch property update */
315 };
316 
317 #define MAX_SAMPLES	20
318 #define DEFAULT_SAMPLES 4
319 
320 struct _WacomFilterState
321 {
322         int npoints;
323         int x[MAX_SAMPLES];
324         int y[MAX_SAMPLES];
325         int tiltx[MAX_SAMPLES];
326         int tilty[MAX_SAMPLES];
327 };
328 
329 struct _WacomChannel
330 {
331 	/* data stored in this structure is raw data from the tablet, prior
332 	 * to transformation and user-defined filtering. Suppressed values
333 	 * will not be included here, and hardware filtering may occur between
334 	 * the work stage and the valid state. */
335 
336 	WacomDeviceState work;                         /* next state */
337 	Bool dirty;
338 
339 	/* the following union contains the current known state of the
340 	 * device channel, as well as the previous MAX_SAMPLES states
341 	 * for use in detecting hardware defects, jitter, trends, etc. */
342 	union
343 	{
344 		WacomDeviceState state;                /* current state */
345 		WacomDeviceState states[MAX_SAMPLES];  /* states 0..MAX */
346 	} valid;
347 
348 	int nSamples;
349 	WacomFilterState rawFilter;
350 };
351 
352 /******************************************************************************
353  * WacomDeviceClass
354  *****************************************************************************/
355 
356 struct _WacomDeviceClass
357 {
358 	Bool (*Detect)(InputInfoPtr pInfo); /* detect device */
359 	Bool (*ParseOptions)(InputInfoPtr pInfo); /* parse class-specific options */
360 	Bool (*Init)(InputInfoPtr pInfo, char* id, size_t id_len, float *version);   /* initialize device */
361 	int  (*ProbeKeys)(InputInfoPtr pInfo); /* set the bits for the keys supported */
362 };
363 
364 extern WacomDeviceClass gWacomUSBDevice;
365 extern WacomDeviceClass gWacomISDV4Device;
366 
367 /******************************************************************************
368  * WacomCommonRec
369  *****************************************************************************/
370 
371 #define TILT_ENABLED_FLAG       2
372 
373 #define MAX_FINGERS 16
374 #define MAX_CHANNELS (MAX_FINGERS+2) /* one channel for stylus/mouse. The other one for pad */
375 #define PAD_CHANNEL (MAX_CHANNELS-1)
376 
377 typedef struct {
378 	int wcmZoomDistance;	       /* minimum distance for a zoom touch gesture */
379 	int wcmScrollDistance;	       /* minimum motion before sending a scroll gesture */
380 	int wcmScrollDirection;	       /* store the vertical or horizontal bit in use */
381 	int wcmGestureUsed;	       /* retain used gesture count within one in-prox event */
382 	int wcmTapTime;	   	       /* minimum time between taps for a right click */
383 } WacomGesturesParameters;
384 
385 enum WacomProtocol {
386 	WCM_PROTOCOL_GENERIC,
387 	WCM_PROTOCOL_4,
388 	WCM_PROTOCOL_5
389 };
390 
391 struct _WacomDriverRec
392 {
393 	WacomDevicePtr active;     /* Arbitrate motion through this pointer */
394 };
395 extern struct _WacomDriverRec WACOM_DRIVER; // Defined in wcmCommon.c
396 
397 struct _WacomCommonRec
398 {
399 	/* Do not move device_path, same offset as priv->name. Used by DBG macro */
400 	char* device_path;          /* device file name */
401 	dev_t min_maj;               /* minor/major number */
402 	unsigned char wcmFlags;     /* various flags (handle tilt) */
403 	int debugLevel;
404 	int vendor_id;		     /* Vendor ID */
405 	int tablet_id;		     /* USB tablet ID */
406 	int tablet_type;	     /* bitmask of tablet features (WCM_LCD, WCM_PEN, etc) */
407 	int fd;                      /* file descriptor to tablet */
408 	int fd_refs;                 /* number of references to fd; if =0, fd is invalid */
409 	unsigned long wcmKeys[NBITS(KEY_MAX)]; /* supported tool types for the device */
410 	WacomDevicePtr wcmTouchDevice; /* The pointer for pen to access the
411 					  touch tool of the same device id */
412 
413 	Bool wcmHasHWTouchSwitch;    /* Tablet has a touch on/off switch */
414 	int wcmHWTouchSwitchState;   /* touch event disable/enabled by hardware switch */
415 
416 	/* These values are in tablet coordinates */
417 	int wcmMinX;                 /* tablet min X value */
418 	int wcmMinY;                 /* tablet min Y value */
419 	int wcmMaxX;                 /* tablet max X value */
420 	int wcmMaxY;                 /* tablet max Y value */
421 	int wcmMaxZ;                 /* tablet max Z value */
422 	int wcmMaxTouchX;            /* touch panel max X value */
423 	int wcmMaxTouchY;            /* touch panel max Y value */
424 	int wcmResolX;		     /* pen tool X resolution in points/m */
425 	int wcmResolY;		     /* pen tool Y resolution in points/m */
426 	int wcmTouchResolX;	     /* touch X resolution in points/m */
427 	int wcmTouchResolY;	     /* touch Y resolution in points/m */
428 	                             /* tablet Z resolution is equivalent
429 	                              * to wcmMaxZ which is equal to 100% pressure */
430 	int wcmMaxDist;              /* tablet max distance value */
431 	int wcmMaxContacts;          /* MT device max number of contacts */
432 
433 	/*
434 	 * TODO Remove wcmTiltOff*, once the kernel drivers reporting
435 	 * 	non-zero-centered tilt values are no longer in use.
436 	 */
437 	int wcmTiltOffX;	     /* styli tilt offset in X direction */
438 	int wcmTiltOffY;	     /* styli tilt offset in Y direction */
439 	double wcmTiltFactX;	     /* styli tilt factor in X direction */
440 	double wcmTiltFactY;	     /* styli tilt factor in Y direction */
441 	int wcmTiltMinX;	     /* styli min reported tilt in X direction */
442 	int wcmTiltMinY;	     /* styli min reported tilt in Y direction */
443 	int wcmTiltMaxX;	     /* styli max reported tilt in X direction */
444 	int wcmTiltMaxY;	     /* styli max reported tilt in Y direction */
445 
446 	int wcmMaxStripX;            /* Maximum fingerstrip X */
447 	int wcmMaxStripY;            /* Maximum fingerstrip Y */
448 	int wcmMinRing;              /* Minimum touchring value */
449 	int wcmMaxRing;              /* Maximum touchring value */
450 
451 	WacomDevicePtr wcmDevices;   /* list of devices sharing same port */
452 	int wcmPktLength;            /* length of a packet */
453 	int wcmProtocolLevel;        /* Wacom Protocol used */
454 	float wcmVersion;            /* ROM version */
455 	int wcmRotate;               /* rotate screen (for TabletPC) */
456 	int wcmThreshold;            /* Threshold for button pressure */
457 	WacomChannel wcmChannel[MAX_CHANNELS]; /* channel device state */
458 
459 	WacomDeviceClassPtr wcmDevCls; /* device class functions */
460 	WacomModelPtr wcmModel;        /* model-specific functions */
461 	int wcmTPCButton;	     /* set Tablet PC button on/off */
462 	int wcmTouch;	             /* disable/enable touch event */
463 	int wcmTouchDefault;	     /* default to disable when not supported */
464 	int wcmGesture;	     	     /* disable/enable touch gesture */
465 	int wcmGestureMode;	       /* data is in Gesture Mode? */
466 	WacomDeviceState wcmGestureState[MAX_FINGERS]; /* inital state when in gesture mode */
467 	WacomGesturesParameters wcmGestureParameters;
468 	int wcmProxoutDistDefault;   /* Default value for wcmProxoutDist */
469 	int wcmSuppress;        	 /* transmit position on delta > supress */
470 	int wcmRawSample;	     /* Number of raw data used to filter an event */
471 	int wcmPressureRecalibration; /* Determine if pressure recalibration of
472 					 worn pens should be performed */
473 	int wcmPanscrollThreshold;	/* distance pen must move to send a panscroll event */
474 
475 	int bufpos;                        /* position with buffer */
476 	unsigned char buffer[BUFFER_SIZE]; /* data read from device */
477 
478 	void *private;		     /* backend-specific information */
479 
480 	WacomToolPtr wcmTool; /* List of unique tools */
481 	WacomToolPtr serials; /* Serial numbers provided at startup*/
482 
483 	/* DO NOT TOUCH THIS. use wcmRefCommon() instead */
484 	int refcnt;			/* number of devices sharing this struct */
485 
486 #if GET_ABI_MAJOR(ABI_XINPUT_VERSION) >= 16
487 	ValuatorMask *touch_mask;
488 #endif
489 };
490 
491 #define HANDLE_TILT(comm) ((comm)->wcmFlags & TILT_ENABLED_FLAG)
492 
493 /******************************************************************************
494  * WacomTool
495  *****************************************************************************/
496 struct _WacomTool
497 {
498 	WacomToolPtr next; /* Next tool in list */
499 
500 	int typeid; /* Tool type */
501 	unsigned int serial; /* Serial id, 0 == no serial id */
502 	Bool enabled;
503 	char *name;
504 
505 	InputInfoPtr device; /* The InputDevice connected to this tool */
506 };
507 
508 #endif /*__XF86_XF86WACOMDEFS_H */
509 
510 /* vim: set noexpandtab tabstop=8 shiftwidth=8: */
511