From be8716dbfb588e820b312c9373cddd41dd034015 Mon Sep 17 00:00:00 2001
From: Chris Bagwell <chris@cnpbagwell.com>
Date: Sun, 14 Feb 2010 13:43:43 -0600
Subject: [PATCH 1/2] Find tablet_id earlier in init sequence.

common->tablet_id is used in wcmInit() to decide things
like defaulting to relative mode for bamboo P&T's but
it is not set until after return from first call to
wcmInit().

Now qeury and store the tablet_id at same time that we are
discoverying its other features.

Signed-off-by: Chris Bagwell <chris@cnpbagwell.com>
---
 src/wcmConfig.c         |    9 +++++++--
 src/wcmUSB.c            |    4 +---
 src/wcmValidateDevice.c |   33 +++++++++++++++++++++++++++++++--
 3 files changed, 39 insertions(+), 7 deletions(-)

diff --git a/src/wcmConfig.c b/src/wcmConfig.c
index 30dafe1..b5d07f2 100644
--- a/src/wcmConfig.c
+++ b/src/wcmConfig.c
@@ -34,7 +34,8 @@ extern int wcmNeedAutoHotplug(LocalDevicePtr local,
 extern int wcmAutoProbeDevice(LocalDevicePtr local);
 extern int wcmParseOptions(LocalDevicePtr local, unsigned long* keys);
 extern void wcmHotplugOthers(LocalDevicePtr local, unsigned long* keys);
-extern int wcmDeviceTypeKeys(LocalDevicePtr local, unsigned long* keys);
+extern int wcmDeviceTypeKeys(LocalDevicePtr local, unsigned long* keys,
+			     int* tablet_id);
 
 static int wcmAllocate(LocalDevicePtr local, char* name, int flag);
 
@@ -323,6 +324,7 @@ static LocalDevicePtr wcmInit(InputDriverPtr drv, IDevPtr dev, int flags)
 	static int	numberWacom = 0;
 	int		need_hotplug = 0;
 	unsigned long   keys[NBITS(KEY_MAX)];
+	int		tablet_id = 0;
 
 	gWacomModule.wcmDrv = drv;
 
@@ -338,7 +340,7 @@ static LocalDevicePtr wcmInit(InputDriverPtr drv, IDevPtr dev, int flags)
 	xf86CollectInputOptions(local, default_options, NULL);
 
 	/* initialize supported keys */
-	wcmDeviceTypeKeys(local, keys);
+	wcmDeviceTypeKeys(local, keys, &tablet_id);
 
 	device = xf86SetStrOption(local->options, "Device", NULL);
 	type = xf86FindOptionValue(local->options, "Type");
@@ -365,6 +367,9 @@ static LocalDevicePtr wcmInit(InputDriverPtr drv, IDevPtr dev, int flags)
 
 	common->wcmDevice = device;
 
+	/* Hardware specific initialization relies on tablet_id */
+	common->tablet_id = tablet_id;
+
 	/* Auto-probe the device if required, otherwise just noop. */
 	if (numberWacom)
 		if (!wcmAutoProbeDevice(local))
diff --git a/src/wcmUSB.c b/src/wcmUSB.c
index 954af06..d243721 100644
--- a/src/wcmUSB.c
+++ b/src/wcmUSB.c
@@ -525,10 +525,8 @@ Bool usbWcmInit(LocalDevicePtr local, char* id, float *version)
 	/* vendor is wacom */
 	if (sID.vendor == WACOM_VENDOR_ID)
 	{
-		common->tablet_id = sID.product;
-
 		for (i = 0; i < sizeof (WacomModelDesc) / sizeof (WacomModelDesc [0]); i++)
-			if (common->tablet_id == WacomModelDesc [i].model_id)
+			if (sID.product == WacomModelDesc [i].model_id)
 			{
 				common->wcmModel = WacomModelDesc [i].model;
 				common->wcmResolX = WacomModelDesc [i].xRes;
diff --git a/src/wcmValidateDevice.c b/src/wcmValidateDevice.c
index 07632de..271f918 100644
--- a/src/wcmValidateDevice.c
+++ b/src/wcmValidateDevice.c
@@ -36,7 +36,8 @@ void wcmHotplugOthers(LocalDevicePtr local, unsigned long* keys);
 int wcmAutoProbeDevice(LocalDevicePtr local);
 int wcmParseOptions(LocalDevicePtr local, unsigned long* keys);
 int wcmIsDuplicate(char* device, LocalDevicePtr local);
-int wcmDeviceTypeKeys(LocalDevicePtr local, unsigned long* keys);
+int wcmDeviceTypeKeys(LocalDevicePtr local, unsigned long* keys,
+		      int* tablet_id);
 
 /* wcmCheckSource - Check if there is another source defined this device
  * before or not: don't add the tool by hal/udev if user has defined at least
@@ -184,7 +185,8 @@ Bool wcmIsAValidType(const char* type, unsigned long* keys)
    device ID. This matching only works for wacom devices (serial ID of
    WACf), all others are simply assumed to be pen + erasor.
  */
-int wcmDeviceTypeKeys(LocalDevicePtr local, unsigned long* keys)
+int wcmDeviceTypeKeys(LocalDevicePtr local, unsigned long* keys,
+		      int* tablet_id)
 {
 	int ret = 1;
 	int fd = -1;
@@ -201,6 +203,8 @@ int wcmDeviceTypeKeys(LocalDevicePtr local, unsigned long* keys)
 		return 0;
 	}
 
+	*tablet_id = 0;
+
 	/* serial ISDV4 devices */
 	if (ioctl(fd, TIOCGSERIAL, &tmp) == 0)
 	{
@@ -248,9 +252,25 @@ int wcmDeviceTypeKeys(LocalDevicePtr local, unsigned long* keys)
 			keys[LONG(BTN_TOOL_PEN)] &= ~BIT(BTN_TOOL_PEN);
 			keys[LONG(BTN_TOOL_RUBBER)] &= ~BIT(BTN_TOOL_RUBBER);
 		}
+
+		/* 0x9a and 0x9f are only detected by communicating
+		 * with device.  This means tablet_id will be updated/refined
+		 * at later stage and true knowledge of capacitive
+		 * support will be delayed until that point.
+		 */
+		if (id >= 0x0 && id <= 0x7)
+			*tablet_id = 0x90;
+		else if (id >= 0x8 && id <= 0xa)
+			*tablet_id = 0x93;
+		else if (id >= 0xb && id <= 0xe)
+			*tablet_id = 0xe3;
+		else if (id == 0x10)
+			*tablet_id = 0xe2;
 	}
 	else /* USB devices */
 	{
+		struct input_id wacom_id;
+
 		if (ioctl(fd, EVIOCGBIT(EV_KEY, (sizeof(unsigned long)
 			 * NBITS(KEY_MAX))), keys) < 0)
 		{
@@ -258,6 +278,15 @@ int wcmDeviceTypeKeys(LocalDevicePtr local, unsigned long* keys)
 				"ioctl USB key bits.\n", local->name);
 			ret = 0;
 		}
+
+		if (ioctl(fd, EVIOCGID, &wacom_id) < 0)
+		{
+			xf86Msg(X_ERROR, "%s: wcmDeviceTypeKeys unable to "
+				"ioctl Device ID.\n", local->name);
+			ret = 0;
+		}
+		else
+			*tablet_id = wacom_id.product;
 	}
 	close(fd);
 	return ret;
-- 
1.6.6

