summaryrefslogtreecommitdiff
path: root/src/wcmValidateDevice.c
blob: d5a7b2b8d54339fec2c49d07ded4940c73187116 (plain)
1
2
3
4
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
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
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
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
/*
 * Copyright 2009 - 2013 by Ping Cheng, Wacom. <pingc@wacom.com>
 *
 * 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 <config.h>

#include "xf86Wacom.h"
#include "wcmFilter.h"
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

struct checkData {
	dev_t min_maj;
	const char *source;
};

static int checkSource(WacomDevicePtr other, void *data)
{
	char *device = wcmOptCheckStr(other, "Device", NULL);
	WacomCommonPtr pCommon;
	struct checkData *check = data;
	Bool match = FALSE;

	/* device can be NULL on some distros */
	if (!device)
		return -ENODEV;

	free(device);

	pCommon = other->common;
	if (pCommon->min_maj && pCommon->min_maj == check->min_maj)
	{
		char* source = wcmOptCheckStr(other, "_source", "");
		/* only add the new tool if the matching major/minor
		 * was from the same source */
		if (strcmp(check->source, source))
			match = TRUE;
		free(source);
	}

	return match ? 0 : -ENODEV;
}

/* 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
 * one tool for the device in xorg.conf. One device can have multiple tools
 * with the same type to individualize tools with serial number or areas */
static Bool wcmCheckSource(WacomDevicePtr priv, dev_t min_maj)
{
	int nmatch;
	struct checkData check = {
		.min_maj = min_maj,
		.source = wcmOptCheckStr(priv, "_source", ""),
	};

	nmatch = wcmForeachDevice(priv, checkSource, &check);
	if (nmatch > 0)
		wcmLog(priv, W_WARNING,
			    "device file already in use. Ignoring.\n");
	free((char*)check.source);
	return nmatch;
}

/* check if the device has been added.
 * Open the device and check it's major/minor, then compare this with every
 * other wacom device listed in the config. If they share the same
 * major/minor and the same source/type, fail.
 * This is to detect duplicate devices if a device was added once through
 * the xorg.conf and is then hotplugged through the server backend (HAL,
 * udev). In this case, the hotplugged one fails.
 */
int wcmIsDuplicate(const char* device, WacomDevicePtr priv)
{
	struct stat st;
	int isInUse = 0;
	char* lsource = wcmOptCheckStr(priv, "_source", NULL);

	/* always allow xorg.conf defined tools to be added */
	if (!lsource || !strlen(lsource)) goto ret;

	if (stat(device, &st) == -1)
	{
		/* can not access major/minor to check device duplication */
		wcmLog(priv, W_ERROR,
			    "stat failed (%s). cannot check for duplicates.\n",
			    strerror(errno));

		/* older systems don't support the required ioctl.  let it pass */
		goto ret;
	}

	if (st.st_rdev)
	{
		/* device matches with another added port */
		if (wcmCheckSource(priv, st.st_rdev))
		{
			isInUse = 3;
			goto ret;
		}
	}
	else
	{
		/* major/minor can never be 0, right? */
		wcmLog(priv, W_ERROR,
			    "device opened with a major/minor of 0. Something was wrong.\n");
		isInUse = 4;
	}
ret:
	free(lsource);
	return isInUse;
}

static struct
{
	const char* type;
	__u16 tool[5]; /* tool array is terminated by 0 */
} wcmType [] =
{
	{ "stylus", { BTN_TOOL_PEN,       0                  } },
	{ "eraser", { BTN_TOOL_RUBBER,    0                  } },
	{ "cursor", { BTN_TOOL_MOUSE,     0                  } },
	{ "touch",  { BTN_TOOL_DOUBLETAP, BTN_TOOL_FINGER, 0 } },
	{ "pad",    { BTN_FORWARD,        BTN_0,           KEY_CONTROLPANEL, KEY_ONSCREEN_KEYBOARD, 0 } }
};

/* validate tool type for device/product */
Bool wcmIsAValidType(WacomDevicePtr priv, const char* type)
{
	WacomCommonPtr common = priv->common;
	size_t i, j;
	char* dsource;
	Bool user_defined;

	if (!type)
	{
		wcmLog(priv, W_ERROR, "No type specified\n");
		return FALSE;
	}

	dsource = wcmOptCheckStr(priv, "_source", NULL);
	user_defined = !dsource || !strlen(dsource);
	free(dsource);

	for (i = 0; i < ARRAY_SIZE(wcmType); i++)
	{
		if (strcmp(wcmType[i].type, type) == 0)
		{
			break;
		}
	}

	if (i >= ARRAY_SIZE(wcmType))
	{
		/* No type with the given name was found. */
		wcmLog(priv, W_ERROR, "type '%s' is not known to the driver\n", type);
		return FALSE;
	}

	for (j = 0; wcmType[i].tool[j]; j++)
	{
		/* Check if the device has this tool among its declared
		 * keys. If it does, then this is (generally) a valid
		 * type.
		 *
		 * Non-generic devices are an exception to this rule.
		 * In particular, they set BTN_TOOL_FINGER on the
		 * pad device. This means that we need to ignore
		 * BTN_TOOL_FINGER when checking if "touch" is a
		 * valid type since that would be a false-positive.
		 * Note that "touch" can still be detected by another
		 * key, e.g. BTN_TOOL_DOUBLETAP.
		 */
		Bool valid = ISBITSET (common->wcmKeys, wcmType[i].tool[j]);
		Bool bypass = common->wcmProtocolLevel != WCM_PROTOCOL_GENERIC &&
			      wcmType[i].tool[j] == BTN_TOOL_FINGER &&
		              strcmp(type, "touch") == 0;

		if (valid && !bypass) {
			return TRUE;
		}
	}

	if (user_defined)
	{
		/* This device does not appear to be the right type, but
		 * maybe the user knows better than us...
		 */
		SETBIT(common->wcmKeys, wcmType[i].tool[0]);
		wcmLog(priv, W_WARNING, "user-defined type '%s' may not be valid\n", type);
		return TRUE;
	}

	/* The driver is probably probing to see if this is a valid
	 * type to associate with this device for hotplug. Let the
	 * caller know it is invalid, but don't complain in the logs.
	 */
	return FALSE;
}

/* Choose valid types according to device ID. */
int wcmDeviceTypeKeys(WacomDevicePtr priv)
{
	int ret = 1;
	WacomCommonPtr common = priv->common;

	priv->common->tablet_id = common->wcmDevCls->ProbeKeys(priv);

	switch (priv->common->tablet_id)
	{
		case 0xF8:  /* Cintiq 24HDT */
		case 0xF4:  /* Cintiq 24HD */
			TabletSetFeature(priv->common, WCM_DUALRING);
			_fallthrough_;
		case 0x34D: /* MobileStudio Pro 13 */
		case 0x34E: /* MobileStudio Pro 16 */
		case 0x398: /* MobileStudio Pro 13 */
		case 0x399: /* MobileStudio Pro 16 */
		case 0x3AA: /* MobileStudio Pro 16 */
			TabletSetFeature(priv->common, WCM_LCD);
			_fallthrough_;
		case 0x357: /* Intuos Pro 2 M */
		case 0x358: /* Intuos Pro 2 L */
		case 0x360: /* Intuos Pro 2 M (Bluetooth) */
		case 0x36a: /* Intuos Pro 2 L (Bluetooth) */
		case 0x392: /* Intuos Pro 2 S */
		case 0x393: /* Intuos Pro 2 S (Bluetooth) */
		case 0x314: /* Intuos Pro S */
		case 0x315: /* Intuos Pro M */
		case 0x317: /* Intuos Pro L */
		case 0x26:  /* I5 */
		case 0x27:  /* I5 */
		case 0x28:  /* I5 */
		case 0x29:  /* I5 */
		case 0x2A:  /* I5 */
		case 0xB8:  /* I4 */
		case 0xB9:  /* I4 */
		case 0xBA:  /* I4 */
		case 0xBB:  /* I4 */
		case 0xBC:  /* I4 */
		case 0xBD:  /* I4 */
			TabletSetFeature(priv->common, WCM_ROTATION);
			_fallthrough_;
		/* tablets with touch ring */
		case 0x17:  /* BambooFun */
		case 0x18:  /* BambooFun */
			TabletSetFeature(priv->common, WCM_RING);
			break;

		/* tablets support dual input */
		case 0x20:  /* I1 */
		case 0x21:  /* I1 */
		case 0x22:  /* I1 */
		case 0x23:  /* I1 */
		case 0x24:  /* I1 */
		case 0x41:  /* I2 */
		case 0x42:  /* I2 */
		case 0x43:  /* I2 */
		case 0x44:  /* I2 */
		case 0x45:  /* I2 */
		case 0x47:  /* I2 */
			TabletSetFeature(priv->common, WCM_DUALINPUT);
			break;

		/* P4 display tablets */
		case 0x30:  /* PL400 */
		case 0x31:  /* PL500 */
		case 0x32:  /* PL600 */
		case 0x33:  /* PL600SX */
		case 0x34:  /* PL550 */
		case 0x35:  /* PL800 */
		case 0x37:  /* PL700 */
		case 0x38:  /* PL510 */
		case 0x39:  /* PL710 */
		case 0x3A:  /* DTI520 */
		case 0xC0:  /* DTF720 */
		case 0xC2:  /* DTF720a */
		case 0xC4:  /* DTF521 */
		case 0xC7:  /* DTU1931 */
		case 0xCE:  /* DTU2231 */
		case 0xF0:  /* DTU1631 */

		/* Wacom One display tablet */
		case 0x3A6: /* DTC133 */
			TabletSetFeature(priv->common, WCM_LCD);
			break;

		/* tablets support menu strips */
		case 0x3F:  /* CintiqV5 */
		case 0xC5:  /* CintiqV5 */
		case 0xC6:  /* CintiqV5 */
		case 0xCC:  /* CinitqV5 */
		case 0xFA:  /* Cintiq 22HD */
		case 0x5B:  /* Cintiq 22HDT Pen */
			TabletSetFeature(priv->common, WCM_LCD);
			_fallthrough_;
		case 0xB0:  /* I3 */
		case 0xB1:  /* I3 */
		case 0xB2:  /* I3 */
		case 0xB3:  /* I3 */
		case 0xB4:  /* I3 */
		case 0xB5:  /* I3 */
		case 0xB7:  /* I3 */
			TabletSetFeature(priv->common, WCM_STRIP | WCM_ROTATION);
			break;

		case 0x100: /* TPC with MT */
		case 0x101: /* TPC with MT */
		case 0x10D: /* TPC with MT */
		case 0x116: /* TPC with 1FGT */
		case 0x12C: /* TPC */
		case 0x4001: /* TPC with MT */
		case 0x4004: /* TPC with MT (no pen on Motion) */
		case 0x5000: /* TPC with MT */
		case 0x5002: /* TPC with MT */
		case 0xE2: /* TPC with 2FGT */
		case 0xE3: /* TPC with 2FGT */
		case 0xE5: /* TPC with MT */
		case 0xE6: /* TPC with 2FGT */
		case 0x93: /* TPC with 1FGT */
		case 0x9A: /* TPC with 1FGT */
		case 0xEC: /* TPC with 1FGT */
		case 0xED: /* TPC with 1FGT */
		case 0x90: /* TPC */
		case 0x97: /* TPC */
		case 0x9F: /* TPC */
		case 0xEF: /* TPC */
			TabletSetFeature(priv->common, WCM_TPC);
			break;

		case 0x304:/* Cintiq 13HD */
		case 0x307:/* Cintiq Companion Hybrid */
		case 0x30A:/* Cintiq Companion */
		case 0x325:/* Cintiq Companion 2 */
		case 0x32A:/* Cintiq 27QHD */
		case 0x32B:/* Cintiq 27QHDT Pen */
		case 0x333:/* Cintiq 13HDT Pen */
		case 0x34F:/* Cintiq Pro 13 FHD */
		case 0x350:/* Cintiq Pro 16 UHD */
		case 0x351:/* Cintiq Pro 24 */
		case 0x352:/* Cintiq Pro 32 */
		case 0x37C:/* Cintiq Pro 24 Pen-Only */
		case 0x390:/* Cintiq 16 */
		case 0x391:/* Cintiq 22 */
		case 0x396:/* DTK-1660E */
		case 0x3AE:/* Cintiq 16 */
		case 0x3B0:/* DTK-1660E */
			TabletSetFeature(priv->common, WCM_ROTATION);
			_fallthrough_;
		case 0xF6: /* Cintiq 24HDT Touch */
		case 0x57: /* DTK2241 */
		case 0x59: /* DTH2242 Pen */
		case 0x5D: /* DTH2242 Touch */
		case 0x5E: /* Cintiq 22HDT Touch */
		case 0x309:/* Cintiq Companion Hybrid Touch */
		case 0x30C:/* Cintiq Companion Touch */
		case 0x326:/* Cintiq Companion 2 Touch */
		case 0x32C:/* Cintiq 27QHDT Touch */
		case 0x32F:/* DTU-1031X */
		case 0x335:/* Cintiq 13HDT Touch */
		case 0x336:/* DTU-1141 */
		case 0x343:/* DTK-1651 */
		case 0x34A:/* MobileStudio Pro 13 Touch */
		case 0x34B:/* MobileStudio Pro 16 Touch */
		case 0x353:/* Cintiq Pro 13 FHD Touch */
		case 0x354:/* Cintiq Pro 13 UHD Touch */
		case 0x355:/* Cintiq Pro 24 Touch */
		case 0x356:/* Cintiq Pro 32 Touch */
		case 0x359:/* DTU-1141B */
		case 0x35A:/* DTH-1152*/
		case 0x368:/* DTH-1152 Touch */
		case 0x382:/* DTK-2451 */
		case 0x37D:/* DTH-2452 */
		case 0x37E:/* DTH-2452 Touch */
		case 0x39A:/* MobileStudio Pro 13 Touch */
		case 0x39B:/* MobileStudio Pro 16 Touch */
		case 0x3AC:/* MobileStudio Pro 16 Touch */
			TabletSetFeature(priv->common, WCM_LCD);
			break;
	}

	if (ISBITSET(common->wcmInputProps, INPUT_PROP_DIRECT))
		TabletSetFeature(priv->common, WCM_LCD);

	if (ISBITSET(common->wcmKeys, BTN_TOOL_PEN))
		TabletSetFeature(priv->common, WCM_PEN);

	if (ISBITSET (common->wcmKeys, BTN_0) ||
			ISBITSET (common->wcmKeys, BTN_FORWARD))
	{
		TabletSetFeature(priv->common, WCM_PAD);
	}

	/* This handles both protocol 4 and 5 meanings of wcmKeys */
	if (common->wcmProtocolLevel == WCM_PROTOCOL_4)
	{
		/* TRIPLETAP means 2 finger touch */
		/* DOUBLETAP without TRIPLETAP means 1 finger touch */
		if (ISBITSET(common->wcmKeys, BTN_TOOL_TRIPLETAP))
			TabletSetFeature(priv->common, WCM_2FGT);
		else if (ISBITSET(common->wcmKeys, BTN_TOOL_DOUBLETAP))
			TabletSetFeature(priv->common, WCM_1FGT);
	}

	if (common->wcmProtocolLevel == WCM_PROTOCOL_GENERIC)
	{
		/* DOUBLETAP means 2 finger touch */
		/* FINGER without DOUBLETAP means 1 finger touch */
		if (ISBITSET(common->wcmKeys, BTN_TOOL_DOUBLETAP))
			TabletSetFeature(priv->common, WCM_2FGT);
		else if (ISBITSET(common->wcmKeys, BTN_TOOL_FINGER))
			TabletSetFeature(priv->common, WCM_1FGT);
	}

	return ret;
}

static void
wcmAddHotpluggedDevice(WacomDevicePtr priv, const char *basename, const char *type,
		       WacomToolPtr ser)
{
	char *name;
	int rc;

	if (ser == NULL)
		rc = asprintf(&name, "%s %s", basename, type);
	else if (strlen(ser->name) > 0)
		rc = asprintf(&name, "%s %s %s", basename, ser->name, type);
	else
		rc = asprintf(&name, "%s %u %s", basename, ser->serial, type);

	if (rc == -1)
		return;

	wcmQueueHotplug(priv, name, type, ser ? ser->serial : UINT_MAX);

	free(name);
}

/**
 * Attempt to hotplug a tool with a given type.
 *
 * If the tool does not claim to support the given type, ignore the
 * request. Otherwise, verify that it is actually valid and then
 * queue the hotplug.
 *
 * @param priv The parent device
 * @param ser The tool pointer
 * @param basename The kernel device name
 * @param idflag Tool ID_XXXX flag to try to hotplug
 * @param type Type name for this tool
 */
static void wcmTryHotplugSerialType(WacomDevicePtr priv, WacomToolPtr ser, const char *basename, int idflag, const char *type)
{
	if (!(ser->typeid & idflag)) {
		// No need to print an error message. The device doesn't
		// claim to support this type so there's no problem.
		return;
	}

	if (!wcmIsAValidType(priv, type)) {
		wcmLog(priv, W_ERROR, "invalid device type '%s'.\n", type);
		return;
	}

	wcmAddHotpluggedDevice(priv, basename, type, ser);
}

/**
 * Hotplug all serial numbers configured on this device.
 *
 * @param priv The parent device
 * @param basename The kernel device name
 */
static void wcmHotplugSerials(WacomDevicePtr priv, const char *basename)
{
	WacomCommonPtr  common = priv->common;
	WacomToolPtr    ser = common->serials;

	while (ser)
	{
		wcmLog(priv, W_INFO, "hotplugging serial %u.\n", ser->serial);
		wcmTryHotplugSerialType(priv, ser, basename, STYLUS_ID, "stylus");
		wcmTryHotplugSerialType(priv, ser, basename, ERASER_ID, "eraser");
		wcmTryHotplugSerialType(priv, ser, basename, CURSOR_ID, "cursor");

		ser = ser->next;
	}
}

void wcmHotplugOthers(WacomDevicePtr priv, const char *basename)
{
	Bool skip = TRUE;

	wcmLog(priv, W_INFO, "hotplugging dependent devices.\n");

        /* same loop is used to init the first device, if we get here we
         * need to start at the second one */
	for (size_t i = 0; i < ARRAY_SIZE(wcmType); i++)
	{
		if (wcmIsAValidType(priv, wcmType[i].type))
		{
			if (skip)
				skip = FALSE;
			else
				wcmAddHotpluggedDevice(priv, basename, wcmType[i].type, NULL);
		}
	}

	wcmHotplugSerials(priv, basename);

	wcmLog(priv, W_INFO, "hotplugging completed.\n");
}

/**
 * Return 1 if the device needs auto-hotplugging from within the driver.
 * This is the case if we don't get passed a "type" option (invalid in
 * xorg.conf configurations) and we come from HAL, udev or whatever future
 * config backend.
 *
 * This changes the source to _driver/wacom, all auto-hotplugged devices
 * will have the same source.
 */
int wcmNeedAutoHotplug(WacomDevicePtr priv, char **type)
{
	char *source = wcmOptCheckStr(priv, "_source", NULL);
	int rc = 0;

	if (*type) /* type specified, don't hotplug */
		goto out;

	if (!source) /* xorg.conf device, don't auto-pick type */
		goto out;

	if (source && strcmp(source, "server/hal") && strcmp(source, "server/udev"))
		goto out;

	/* no type specified, so we need to pick the first one applicable
	 * for our device */
	for (size_t i = 0; i < ARRAY_SIZE(wcmType); i++)
	{
		if (wcmIsAValidType(priv, wcmType[i].type))
		{
			free(*type);
			*type = strdup(wcmType[i].type);
			break;
		}
	}

	if (!*type) {
		wcmLog(priv, W_ERROR, "No valid type found for this device.\n");
		goto out;
	}

	wcmLog(priv, W_INFO, "type not specified, assuming '%s'.\n", *type);
	wcmLog(priv, W_INFO, "other types will be automatically added.\n");

	/* Note: wcmIsHotpluggedDevice() relies on this */
	wcmOptSetStr(priv, "Type", *type);
	wcmOptSetStr(priv, "_source", "_driver/wacom");

	rc = 1;

	free(source);
out:
	return rc;
}

int wcmParseSerials (WacomDevicePtr priv)
{
	WacomCommonPtr  common = priv->common;
	char            *s;

	if (common->serials)
	{
		return 0; /*Parse has been already done*/
	}

	s = wcmOptGetStr(priv, "ToolSerials", NULL);
	if (s) /*Dont parse again, if the commons have values already*/
	{
		char* tok = strtok(s, ";");
		while (tok != NULL)
		{
			int serial, nmatch;
			char type[strlen(tok) + 1];
			char name[strlen(tok) + 1];
			WacomToolPtr ser = calloc(1, sizeof(WacomTool));

			if (ser == NULL)
				return 1;

			nmatch = sscanf(tok,"%d,%[a-z],%[A-Za-z ]",&serial, type, name);

			if (nmatch < 1)
			{
				wcmLog(priv, W_ERROR, "%s is invalid serial string.\n", tok);
				free(ser);
				return 1;
			}

			if (nmatch >= 1)
			{
				wcmLog(priv, W_CONFIG, "Tool serial %d found.\n", serial);

				ser->serial = serial;

				ser->typeid = STYLUS_ID | ERASER_ID; /*Default to both tools*/
			}

			if (nmatch >= 2)
			{
				wcmLog(priv, W_CONFIG, "Tool %d has type %s.\n", serial, type);
				if ((strcmp(type, "pen") == 0) || (strcmp(type, "airbrush") == 0))
					ser->typeid = STYLUS_ID | ERASER_ID;
				else if (strcmp(type, "artpen") == 0)
					ser->typeid = STYLUS_ID;
				else if (strcmp(type, "cursor") == 0)
					ser->typeid = CURSOR_ID;
				else    wcmLog(priv, W_CONFIG, "Invalid type %s, defaulting to pen.\n", type);
			}

			if (nmatch == 3)
			{
				wcmLog(priv, W_CONFIG, "Tool %d is named %s.\n", serial, name);
				ser->name = strdup(name);
			}
			else ser->name = strdup(""); /*no name yet*/

			if (common->serials == NULL)
				common->serials = ser;
			else
			{
				WacomToolPtr tool = common->serials;
				while (tool->next)
					tool = tool->next;
				tool->next = ser;
			}

			tok = strtok(NULL,";");
		}
	}
	return 0;
}

/**
 * Parse the pre-init options for this device. Most useful for options
 * needed to properly init a device (baud rate for example).
 *
 * Note that parameters is_primary and is_dependent are mutually exclusive,
 * though both may be false in the case of an xorg.conf device.
 *
 * @param is_primary True if the device is the parent device for
 * hotplugging, False if the device is a depent or xorg.conf device.
 * @param is_hotplugged True if the device is a dependent device, FALSE
 * otherwise.
 * @retval True on success or False otherwise.
 */
Bool wcmPreInitParseOptions(WacomDevicePtr priv, Bool is_primary,
			    Bool is_dependent)
{
	WacomCommonPtr  common = priv->common;
	char            *s;
	int		i;
	WacomToolPtr    tool = NULL;
	int		tpc_button_is_on;

	/* Optional configuration */
	s = wcmOptGetStr(priv, "Mode", NULL);

	if (s && (strcasecmp(s, "absolute") == 0))
		set_absolute(priv, TRUE);
	else if (s && (strcasecmp(s, "relative") == 0))
		set_absolute(priv, FALSE);
	else
	{
		if (s)
			wcmLog(priv, W_ERROR,
				"invalid Mode (should be absolute"
				" or relative). Using default.\n");

		/* If Mode not specified or is invalid then rely on
		 * Type specific defaults from initialization.
		 */
	}

	free(s);

	/* Pad is always in absolute mode.
	 * The pad also defaults to wheel scrolling, unlike the pens
	 * (interesting effects happen on ArtPen and others with build-in
	 * wheels)
	 */
	if (IsPad(priv))
	{
		priv->wheel_default[WHEEL_ABS_UP] = priv->wheel_default[WHEEL2_ABS_UP] = 4;
		priv->wheel_default[WHEEL_ABS_DN] = priv->wheel_default[WHEEL2_ABS_DN] = 5;
		set_absolute(priv, TRUE);
	}

	s = wcmOptGetStr(priv, "Rotate", NULL);

	if (s)
	{
		int rotation = ROTATE_NONE;

		if (strcasecmp(s, "CW") == 0)
			rotation = ROTATE_CW;
		else if (strcasecmp(s, "CCW") ==0)
			rotation = ROTATE_CCW;
		else if (strcasecmp(s, "HALF") ==0)
			rotation = ROTATE_HALF;
		else if (strcasecmp(s, "NONE") !=0)
		{
			wcmLog(priv, W_ERROR, "invalid Rotate option '%s'.\n", s);
			goto error;
		}

		if (is_dependent && rotation != common->wcmRotate)
			wcmLog(priv, W_INFO, "ignoring rotation of dependent device\n");
		else
			wcmRotateTablet(priv, rotation);
		free(s);
	}

	common->wcmRawSample = wcmOptGetInt(priv, "RawSample",
			common->wcmRawSample);
	if (common->wcmRawSample < 1 || common->wcmRawSample > MAX_SAMPLES)
	{
		wcmLog(priv, W_ERROR,
			    "RawSample setting '%d' out of range [1..%d]. Using default.\n",
			    common->wcmRawSample, MAX_SAMPLES);
		common->wcmRawSample = DEFAULT_SAMPLES;
	}

	common->wcmSuppress = wcmOptGetInt(priv, "Suppress",
			common->wcmSuppress);
	if (common->wcmSuppress != 0) /* 0 disables suppression */
	{
		if (common->wcmSuppress > MAX_SUPPRESS)
			common->wcmSuppress = MAX_SUPPRESS;
		if (common->wcmSuppress < DEFAULT_SUPPRESS)
			common->wcmSuppress = DEFAULT_SUPPRESS;
	}

	/* pressure curve takes control points x1,y1,x2,y2
	 * values in range from 0..100.
	 * Linear curve is 0,0,100,100
	 * Slightly depressed curve might be 5,0,100,95
	 * Slightly raised curve might be 0,5,95,100
	 */
	s = wcmOptGetStr(priv, "PressCurve", "0,0,100,100");
	if (s && (IsPen(priv) || IsTouch(priv)))
	{
		int a,b,c,d;
		if ((sscanf(s,"%d,%d,%d,%d",&a,&b,&c,&d) != 4) ||
				!wcmCheckPressureCurveValues(a, b, c, d))
			wcmLog(priv, W_CONFIG, "PressCurve not valid\n");
		else
			wcmSetPressureCurve(priv,a,b,c,d);
	}
	free(s);

	if (wcmOptGetBool(priv, "Pressure2K", 0)) {
		wcmLog(priv, W_CONFIG, "Using 2K pressure levels\n");
		priv->maxCurve = 2048;
	}

	/*Serials of tools we want hotpluged*/
	if (wcmParseSerials (priv) != 0)
		goto error;

	if (IsTablet(priv))
	{
		const char *prop = IsCursor(priv) ? "CursorProx" : "StylusProx";
		priv->wcmProxoutDist = wcmOptGetInt(priv, prop, 0);
		if (priv->wcmProxoutDist < 0 ||
				priv->wcmProxoutDist > common->wcmMaxDist)
			wcmLog(priv, W_CONFIG, "%s invalid %d \n",
				    prop, priv->wcmProxoutDist);
		priv->wcmSurfaceDist = -1;
	}

	priv->topX = wcmOptGetInt(priv, "TopX", 0);
	priv->topY = wcmOptGetInt(priv, "TopY", 0);
	priv->bottomX = wcmOptGetInt(priv, "BottomX", 0);
	priv->bottomY = wcmOptGetInt(priv, "BottomY", 0);
	priv->serial = wcmOptGetInt(priv, "Serial", 0);

	tool = priv->tool;
	tool->serial = priv->serial;

	common->wcmPanscrollThreshold = wcmOptGetInt(priv, "PanScrollThreshold",
			common->wcmPanscrollThreshold);

	/* The first device doesn't need to add any tools/areas as it
	 * will be the first anyway. So if different, add tool
	 * and/or area to the existing lists
	 */
	if(tool != common->wcmTool)
	{
		WacomToolPtr toollist = NULL;
		for(toollist = common->wcmTool; toollist; toollist = toollist->next)
			if(tool->typeid == toollist->typeid && tool->serial == toollist->serial)
				break;

		if(toollist) /* Already have a tool with the same type/serial */
		{
			wcmLog(priv, W_ERROR,
				    "already have a tool with type/serial %d/%u.\n",
				    tool->typeid, tool->serial);
			goto error;
		} else /* No match on existing tool/serial, add tool to the end of the list */
		{
			assert(common->wcmTool != NULL);
			toollist = common->wcmTool;
			while(toollist->next)
				toollist = toollist->next;
			toollist->next = tool;
		}
	}

	common->wcmThreshold = wcmOptGetInt(priv, "Threshold",
			common->wcmThreshold);

	if (wcmOptGetBool(priv, "ButtonsOnly", 0))
		priv->flags |= BUTTONS_ONLY_FLAG;

	/* TPCButton on for Tablet PC by default */
	tpc_button_is_on = wcmOptGetBool(priv, "TPCButton",
					TabletHasFeature(common, WCM_TPC));

	if (is_primary || IsStylus(priv))
		common->wcmTPCButton = tpc_button_is_on;
	else if (tpc_button_is_on != common->wcmTPCButton)
		wcmLog(priv, W_WARNING, "TPCButton option can only be set by stylus.\n");

	/* a single or double touch device */
	if (TabletHasFeature(common, WCM_1FGT) ||
	    TabletHasFeature(common, WCM_2FGT))
	{
		int touch_is_on;

		/* TouchDefault was off for all devices
		 * except when touch is supported */
		common->wcmTouchDefault = 1;

		touch_is_on = wcmOptGetBool(priv, "Touch",
						common->wcmTouchDefault);

		if (is_primary || IsTouch(priv))
			common->wcmTouch = touch_is_on;
		else if (touch_is_on != common->wcmTouch)
			wcmLog(priv, W_WARNING,
				    "Touch option can only be set by a touch tool.\n");

		if (TabletHasFeature(common, WCM_1FGT))
			common->wcmMaxContacts = 1;
		else
			common->wcmMaxContacts = 2;
	}

	/* 2FG touch device */
	if (TabletHasFeature(common, WCM_2FGT))
	{
		int gesture_is_on;
		Bool gesture_default = TabletHasFeature(priv->common, WCM_LCD) ? FALSE : TRUE;

		gesture_is_on = wcmOptGetBool(priv, "Gesture",
					    gesture_default);

		if (is_primary || IsTouch(priv))
			common->wcmGesture = gesture_is_on;
		else if (gesture_is_on != common->wcmGesture)
			wcmLog(priv, W_WARNING,
				    "Touch gesture option can only be set by a touch tool.\n");

		common->wcmGestureParameters.wcmTapTime =
			wcmOptGetInt(priv, "TapTime",
			common->wcmGestureParameters.wcmTapTime);
	}

	if (IsStylus(priv) || IsEraser(priv)) {
		common->wcmPressureRecalibration
			= wcmOptGetBool(priv,
					    "PressureRecalibration", 1);
	}

	/* Swap stylus buttons 2 and 3 for Tablet PCs */
	if (TabletHasFeature(common, WCM_TPC) && IsStylus(priv))
	{
		priv->button_default[1] = 3;
		priv->button_default[2] = 2;
	}

	for (i=0; i<WCM_MAX_BUTTONS; i++)
	{
		char b[20];
		sprintf(b, "Button%d", i+1);
		priv->button_default[i] = wcmOptGetInt(priv, b, priv->button_default[i]);
	}

	/* Now parse class-specific options */
	if (common->wcmDevCls->ParseOptions &&
	    !common->wcmDevCls->ParseOptions(priv))
		goto error;

	return TRUE;
error:
	return FALSE;
}

/* The values were based on trial and error with a 3rd-gen Bamboo */
#define WCM_DEFAULT_MM_XRES           (27.8 * 1000)
#define WCM_DEFAULT_MM_YRES           (44.5 * 1000)
#define WCM_ZOOM_DISTANCE_MM          6.5
#define WCM_SCROLL_DISTANCE_MM        1.8

/**
 * Parse post-init options for this device. Useful for overriding HW
 * specific options computed during init phase (HW distances for example).
 *
 * Note that parameters is_primary and is_dependent are mutually exclusive,
 * though both may be false in the case of an xorg.conf device.
 *
 * @param is_primary True if the device is the parent device for
 * hotplugging, False if the device is a depent or xorg.conf device.
 * @param is_hotplugged True if the device is a dependent device, FALSE
 * otherwise.
 * @retval True on success or False otherwise.
 */
Bool wcmPostInitParseOptions(WacomDevicePtr priv, Bool is_primary,
			     Bool is_dependent)
{
	WacomCommonPtr  common = priv->common;

	common->wcmMaxZ = wcmOptGetInt(priv, "MaxZ",
					   common->wcmMaxZ);

	/* 2FG touch device */
	if (TabletHasFeature(common, WCM_2FGT) && IsTouch(priv))
	{
		int x_res = common->wcmTouchResolX ? common->wcmTouchResolX : WCM_DEFAULT_MM_XRES;
		int y_res = common->wcmTouchResolY ? common->wcmTouchResolY : WCM_DEFAULT_MM_YRES;
		int zoom_distance = WCM_ZOOM_DISTANCE_MM * x_res / 1000;
		int scroll_distance = WCM_SCROLL_DISTANCE_MM * y_res / 1000;

		common->wcmGestureParameters.wcmZoomDistance =
			wcmOptGetInt(priv, "ZoomDistance",
					 zoom_distance);

		common->wcmGestureParameters.wcmScrollDistance =
			wcmOptGetInt(priv, "ScrollDistance",
					 scroll_distance);
	}


	return TRUE;
}

/* vim: set noexpandtab tabstop=8 shiftwidth=8: */