Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

   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
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
/* $NetBSD: alps.c,v 1.12 2019/05/28 08:59:35 msaitoh Exp $ */

/*-
 * Copyright (c) 2017 Ryo ONODERA <ryo@tetera.org>
 * Copyright (c) 2008 Jared D. McNeill <jmcneill@invisible.ca>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "opt_pms.h"

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: alps.c,v 1.12 2019/05/28 08:59:35 msaitoh Exp $");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/kernel.h>
#include <sys/sysctl.h>
#include <sys/bus.h>
#include <sys/bitops.h>

#include <dev/wscons/wsconsio.h>
#include <dev/wscons/wsmousevar.h>

#include <dev/pckbport/pckbportvar.h>
#include <dev/pckbport/pmsreg.h>
#include <dev/pckbport/pmsvar.h>
#include <dev/pckbport/alpsreg.h>
#include <dev/pckbport/alpsvar.h>

/* #define ALPS_DEBUG */

static int alps_touchpad_xy_unprecision_nodenum;
static int alps_trackstick_xy_precision_nodenum;

static int alps_touchpad_xy_unprecision = 2;
static int alps_trackstick_xy_precision = 1;

static void pms_alps_input_v7(void *, int);
static void pms_alps_input_v2(void *, int);

static int
pms_sysctl_alps_verify(SYSCTLFN_ARGS)
{
	int error, t;
	struct sysctlnode node;

	node = *rnode;
	t = *(int *)rnode->sysctl_data;
	node.sysctl_data = &t;
	error = sysctl_lookup(SYSCTLFN_CALL(&node));
	if (error || newp == NULL)
		return error;

	if (node.sysctl_num == alps_touchpad_xy_unprecision_nodenum ||
		node.sysctl_num == alps_trackstick_xy_precision_nodenum) {
		if (t < 0 || t > 7)
			return EINVAL;
	} else
		return EINVAL;

	*(int *)rnode->sysctl_data = t;

	return 0;

}

static void
pms_sysctl_alps(struct sysctllog **clog)
{
	const struct sysctlnode *node;
	int rc, root_num;

	if ((rc = sysctl_createv(clog, 0, NULL, &node,
		CTLFLAG_PERMANENT, CTLTYPE_NODE, "alps",
		SYSCTL_DESCR("ALPS touchpad controls"),
		NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0)
		goto err;

	root_num = node->sysctl_num;

	if ((rc = sysctl_createv(clog, 0, NULL, &node,
		CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
		CTLTYPE_INT, "touchpad_xy_precision_shift",
		SYSCTL_DESCR("Touchpad X/Y-axis precision shift value"),
		pms_sysctl_alps_verify, 0,
		&alps_touchpad_xy_unprecision,
		0, CTL_HW, root_num, CTL_CREATE,
		CTL_EOL)) != 0)
			goto err;
	alps_touchpad_xy_unprecision_nodenum = node->sysctl_num;

	if ((rc = sysctl_createv(clog, 0, NULL, &node,
		CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
		CTLTYPE_INT, "tackstick_xy_precision_shift",
		SYSCTL_DESCR("Trackstick X/Y-axis precision value"),
		pms_sysctl_alps_verify, 0,
		&alps_trackstick_xy_precision,
		0, CTL_HW, root_num, CTL_CREATE,
		CTL_EOL)) != 0)
			goto err;
	alps_trackstick_xy_precision_nodenum = node->sysctl_num;

	return;

err:
	aprint_error("%s: sysctl_createv failed (rc = %d)\n",
		__func__, rc);
}

/*
 * Publish E6 report command and get E6 signature,
 * then check the signature
 */
static int
pms_alps_e6sig(struct pms_softc *psc, uint8_t *e6sig)
{
	uint8_t cmd[2];
	int res;

	e6sig[0] = 0;
	cmd[0] = PMS_SET_RES; /* E8 */
	cmd[1] = 0;
	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
	    cmd, 2, 0, NULL, 0)) != 0)
		goto err;
	cmd[0] = PMS_SET_SCALE11; /* E6 */
	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
	    cmd, 1, 0, NULL, 0)) != 0)
		goto err;
	cmd[0] = PMS_SET_SCALE11; /* E6 */
	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
	    cmd, 1, 0, NULL, 0)) != 0)
		goto err;
	cmd[0] = PMS_SET_SCALE11; /* E6 */
	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
	    cmd, 1, 0, NULL, 0)) != 0)
		goto err;
	e6sig[0] = e6sig[1] = e6sig[2] = 0;
	/* Get E6 signature */
	cmd[0] = PMS_SEND_DEV_STATUS; /* E9 */
	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
	    cmd, 1, 3, e6sig, 0)) != 0)
		goto err;

	/* ALPS input device returns 00-00-64 as E6 signature */
	if ((e6sig[0] & ~0x07u) != 0x00 || /* ignore buttons */
	    e6sig[1] != 0x00 ||
	    e6sig[2] != 0x64)
	{
		return ENODEV;	/* This is not an ALPS device */
	}

	aprint_debug_dev(psc->sc_dev,
		"ALPS PS/2 E6 signature: 0x%X 0x%X 0x%X\n",
		e6sig[0], e6sig[1], e6sig[2]);

	return 0;
err:
	aprint_error_dev(psc->sc_dev, "Failed to get E6 signature.\n");
	return res;
}

/*
 * Publish E7 report command and get E7 signature
 */
static int
pms_alps_e7sig(struct pms_softc *psc, uint8_t *e7sig)
{
	uint8_t cmd[2];
	int res;

	cmd[0] = PMS_SET_RES; /* E8 */
	cmd[1] = 0;
	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
	    cmd, 2, 0, NULL, 0)) != 0)
		goto err;
	cmd[0] = PMS_SET_SCALE21; /* E7 */
	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
	    cmd, 1, 0, NULL, 0)) != 0)
		goto err;
	cmd[0] = PMS_SET_SCALE21; /* E7 */
	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
	    cmd, 1, 0, NULL, 0)) != 0)
		goto err;
	cmd[0] = PMS_SET_SCALE21; /* E7 */
	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
	    cmd, 1, 0, NULL, 0)) != 0)
		goto err;
	e7sig[0] = e7sig[1] = e7sig[2] = 0;
	cmd[0] = PMS_SEND_DEV_STATUS; /* E9 */
	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
	    cmd, 1, 3, e7sig, 0)) != 0)
		goto err;

	aprint_debug_dev(psc->sc_dev,
		"ALPS PS/2 E7 signature: 0x%X 0x%X 0x%X\n",
		e7sig[0], e7sig[1], e7sig[2]);

	if (e7sig[0] != 0x73)
		return ENODEV;	/* This is not an ALPS device */

	return 0;
err:
	aprint_error_dev(psc->sc_dev, "Failed to get E7 signature.\n");
	return res;
}

/*
 * Publish EC command and get EC signature
 */
static int
pms_alps_ecsig(struct pms_softc *psc, uint8_t *ecsig)
{
	uint8_t cmd[2];
	int res;

	cmd[0] = PMS_SET_RES; /* E8 */
	cmd[1] = 0;
	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
	    cmd, 2, 0, NULL, 0)) != 0)
		goto err;
	cmd[0] = PMS_RESET_WRAP_MODE; /* EC */
	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
	    cmd, 1, 0, NULL, 0)) != 0)
		goto err;
	cmd[0] = PMS_RESET_WRAP_MODE; /* EC */
	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
	    cmd, 1, 0, NULL, 0)) != 0)
		goto err;
	cmd[0] = PMS_RESET_WRAP_MODE; /* EC */
	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
	    cmd, 1, 0, NULL, 0)) != 0)
		goto err;
	ecsig[0] = ecsig[1] = ecsig[2] = 0;
	cmd[0] = PMS_SEND_DEV_STATUS; /* E9 */
	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
	    cmd, 1, 3, ecsig, 0)) != 0)
		goto err;

	aprint_debug_dev(psc->sc_dev,
		"ALPS PS/2 EC signature: 0x%X 0x%X 0x%X\n",
		ecsig[0], ecsig[1], ecsig[2]);

	return 0;

err:
	aprint_debug_dev(psc->sc_dev, "Failed to get EC signature.\n");
	return res;
}

/*
 * Enter to command mode
 */
static int
pms_alps_start_command_mode(struct pms_softc *psc)
{
	uint8_t cmd[1];
	uint8_t resp[3];
	int res;

	cmd[0] = PMS_RESET_WRAP_MODE; /* EC */
	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
	    cmd, 1, 0, NULL, 0)) != 0)
		goto err;
	cmd[0] = PMS_RESET_WRAP_MODE; /* EC */
	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
	    cmd, 1, 0, NULL, 0)) != 0)
		goto err;
	cmd[0] = PMS_RESET_WRAP_MODE; /* EC */
	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
	    cmd, 1, 0, NULL, 0)) != 0)
		goto err;
	resp[0] = resp[1] = resp[2] = 0;
	cmd[0] = PMS_SEND_DEV_STATUS; /* E9 */
	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
	    cmd, 1, 3, resp, 0)) != 0)
		goto err;

	aprint_debug_dev(psc->sc_dev, "ALPS Firmware ID: 0x%x 0x%X 0x%X\n",
		resp[0], resp[1], resp[2]);

	if (resp[0] != 0x88 || (resp[1] & __BITS(7, 4)) != 0xb0)
		return EINVAL;

	return 0;
err:
	aprint_error_dev(psc->sc_dev, "Failed to start command mode.\n");
	return res;
}

/*
 * End command mode
 */
static int
pms_alps_end_command_mode(struct pms_softc *psc)
{
	int res;
	uint8_t cmd[1];

	cmd[0] = PMS_SET_STREAM; /* EA */
	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
	    cmd, 1, 0, NULL, 0)) != 0)
		goto err;

	return res;

err:
	aprint_error_dev(psc->sc_dev, "Failed to end command mode.\n");
	return res;
}

/*
 * Write nibble (4-bit) data
 */
static int
pms_alps_cm_write_nibble(pckbport_tag_t tag, pckbport_slot_t slot, uint8_t nibble)
{
	uint8_t cmd[2];
	uint8_t resp[3];
	int sendparam;
	int receive;
	int res;

	sendparam = alps_v7_nibble_command_data_arr[nibble].sendparam;
	receive= alps_v7_nibble_command_data_arr[nibble].receive;
	cmd[0] = alps_v7_nibble_command_data_arr[nibble].command;
	if (receive) {
		if ((res = pckbport_poll_cmd(tag, slot, cmd, 1, 3, resp, 0)) != 0) {
			aprint_error("send nibble error: %d\n", res);
		}
	} else if (sendparam) {
		cmd[1] = alps_v7_nibble_command_data_arr[nibble].data;
		if ((res = pckbport_poll_cmd(tag, slot, cmd, 2, 0, NULL, 0)) != 0) {
			aprint_error("send nibble error: %d\n", res);
		}
	} else {
		if ((res = pckbport_poll_cmd(tag, slot, cmd, 1, 0, NULL, 0)) != 0) {
			aprint_error("send nibble error: %d\n", res);
		}
	}

	return res;
}

/*
 * Set an register address for read and write
 */
static int
pms_alps_set_address(pckbport_tag_t tag, pckbport_slot_t slot, uint16_t reg)
{
	uint8_t cmd[1];
	uint8_t nibble;
	int res;

	cmd[0] = PMS_RESET_WRAP_MODE;
	if ((res = pckbport_poll_cmd(tag, slot, cmd, 1, 0, NULL, 0)) != 0)
		goto err;

	/* Set address */
	nibble = (reg >> 12) & __BITS(3, 0);
	if ((res = pms_alps_cm_write_nibble(tag, slot, nibble)) != 0) {
		goto err;
	}
	nibble = (reg >> 8) & __BITS(3, 0);
	if ((res = pms_alps_cm_write_nibble(tag, slot, nibble)) != 0) {
		goto err;
	}
	nibble = (reg >> 4) & __BITS(3, 0);
	if ((res = pms_alps_cm_write_nibble(tag, slot, nibble)) != 0) {
		goto err;
	}
	nibble = (reg >> 0) & __BITS(3, 0);
	if ((res = pms_alps_cm_write_nibble(tag, slot, nibble)) != 0) {
		goto err;
	}
	return res;

err:
	aprint_error("Failed to set addess.\n");
	return res;
}

/*
 * Read one byte from register
 */
static int
pms_alps_cm_read_1(pckbport_tag_t tag, pckbport_slot_t slot, uint16_t reg,
	uint8_t *val)
{
	uint8_t cmd[1];
	uint8_t resp[3];
	int res;

	if ((res = pms_alps_set_address(tag, slot, reg)) != 0)
		goto err;

	cmd[0] = PMS_SEND_DEV_STATUS;
	if ((res = pckbport_poll_cmd(tag, slot,
	    cmd, 1, 3, resp, 0)) != 0) {
		goto err;
	}

	if (reg != ((resp[0] << 8) | resp[1])) {
		return EINVAL;
	}

	*val = resp[2];
	return res;

err:
	aprint_error("Failed to read a value.\n");
	*val = 0;
	return res;
}

/*
 * Write one byte to register
 */
static int
pms_alps_cm_write_1(pckbport_tag_t tag, pckbport_slot_t slot, uint16_t reg,
	uint8_t val)
{
	uint8_t nibble;
	int res;

	if ((res = pms_alps_set_address(tag, slot, reg)) != 0)
		goto err;

	nibble = __SHIFTOUT(val, __BITS(7, 4));
	if ((res = pms_alps_cm_write_nibble(tag, slot, nibble)) != 0) {
		goto err;
	}

	nibble = __SHIFTOUT(val, __BITS(3, 0));
	if ((res = pms_alps_cm_write_nibble(tag, slot, nibble)) != 0) {
		goto err;
	}

	return res;
err:
	aprint_error("Failed to write a value.\n");
	return res;
}

/*
 * Not used practically for initialization
 */
static int
pms_alps_get_resolution_v7(struct pms_softc *psc)
{
#if 0
	struct alps_softc *sc = &psc->u.alps;
#endif
	pckbport_tag_t tag = psc->sc_kbctag;
	pckbport_slot_t slot = psc->sc_kbcslot;

	int res;
	uint8_t ret;
#if 0
	uint32_t x_pitch, y_pitch;
	uint32_t x_elec, y_elec;
	uint32_t x_phy, y_phy;
#endif
	/* X/Y pitch */
	if ((res = pms_alps_cm_read_1(tag, slot, 0xc397, &ret)) != 0) {
		goto err;
	}
#if 0
	/* X pitch */
	x_pitch = __SHIFTOUT(ret, __BITS(7, 4)); /* Higher 4-bit */
	x_pitch = x_pitch * 2 + 50; /* Unit = 0.1mm */

	/* Y pitch */
	y_pitch = ret & __BITS(3, 0); /* Lower 4-bit */
	y_pitch = y_pitch * 2 + 36; /* Unit = 0.1mm */

	/* X/Y electrode */
	if ((res = pms_alps_cm_read_1(tag, slot, 0xc397 + 1, &ret)) != 0) {
		goto err;
	}

	/* X electrode */
	x_elec = __SHIFTOUT(ret, __BITS(7, 4)); /* Higher 4-bit */
	x_elec = x_elec + 17;

	/* Y electrode */
	y_elec = ret & __BITS(3, 0); /* Lower 4-bit */
	y_elec = y_elec + 13;

	/* X/Y physical in unit = 0.1mm */
	/* X physical */
	x_phy = (x_elec - 1) * x_pitch;
	y_phy = (y_elec - 1) * y_pitch;

	/* X/Y resolution (unit) */
	sc->res_x = 0xfff * 10 / x_phy;
	sc->res_y = 0x7ff * 10 / y_phy;
#endif
	return res;

err:
	aprint_error("Failed to get resolution.\n");
	return res;
}

/*
 * Enable tap mode for V2 device
 */
static int
pms_alps_enable_tap_mode_v2(struct pms_softc *psc)
{
	uint8_t cmd[2];
	uint8_t resp[3];
	int res;

	resp[0] = resp[1] = resp[2] = 0;
	cmd[0] = PMS_SEND_DEV_STATUS; /* E9 */
	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
	    cmd, 1, 3, resp, 0)) != 0)
		goto err;
	cmd[0] = PMS_DEV_DISABLE; /* F5 */
	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
	    cmd, 1, 0, NULL, 0)) != 0)
		goto err;
	cmd[0] = PMS_DEV_DISABLE; /* F5 */
	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
	    cmd, 1, 0, NULL, 0)) != 0)
		goto err;
	cmd[0] = PMS_SET_SAMPLE;
	cmd[1] = 0x0a; /* argument */
	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
	    cmd, 2, 0, NULL, 0)) != 0)
		goto err;

	/* Get status */
	cmd[0] = PMS_DEV_DISABLE; /* F5 */
	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
	    cmd, 1, 0, NULL, 0)) != 0)
		goto err;
	cmd[0] = PMS_DEV_DISABLE; /* F5 */
	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
	    cmd, 1, 0, NULL, 0)) != 0)
		goto err;
	cmd[0] = PMS_DEV_DISABLE; /* F5 */
	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
	    cmd, 1, 0, NULL, 0)) != 0)
		goto err;
	resp[0] = resp[1] = resp[2] = 0;
	cmd[0] = PMS_SEND_DEV_STATUS; /* E9 */
	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
	    cmd, 1, 3, resp, 0)) != 0)
		goto err;

	aprint_debug_dev(psc->sc_dev, "Tap mode is enabled.\n");

	return 0;
err:
	aprint_error_dev(psc->sc_dev, "Failed to enable tap mode.\n");
	return res;
}

static int
pms_alps_init_v2(struct pms_softc *psc)
{
	uint8_t cmd[1];
	int res;

	if ((res = pms_alps_enable_tap_mode_v2(psc)) != 0)
		goto err;

	/* Enable absolute mode */
	cmd[0] = PMS_DEV_DISABLE; /* F5 */
	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
	    cmd, 1, 0, NULL, 0)) != 0)
		goto err;
	cmd[0] = PMS_DEV_DISABLE; /* F5 */
	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
	    cmd, 1, 0, NULL, 0)) != 0)
		goto err;
	cmd[0] = PMS_DEV_DISABLE; /* F5 */
	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
	    cmd, 1, 0, NULL, 0)) != 0)
		goto err;
	cmd[0] = PMS_DEV_DISABLE; /* F5 */
	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
	    cmd, 1, 0, NULL, 0)) != 0)
		goto err;
	cmd[0] = PMS_DEV_ENABLE; /* F4 */
	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
	    cmd, 1, 0, NULL, 0)) != 0)
		goto err;

	/* Enable remote mode */
	cmd[0] = PMS_SET_REMOTE_MODE; /* F0 */
	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
	    cmd, 1, 0, NULL, 0)) != 0)
		goto err;

	/* Start stream mode to get data */
	cmd[0] = PMS_SET_STREAM; /* EA */
	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
	    cmd, 1, 0, NULL, 0)) != 0)
		goto err;


	return 0;

err:
	aprint_error_dev(psc->sc_dev, "Failed to initialize V2 device.\n");
	return res;
}

static int
pms_alps_init_v7(struct pms_softc *psc)
{
	uint8_t val;
	uint8_t nibble;
	int res;

	/* Start command mode */
	if ((res = pms_alps_start_command_mode(psc)) != 0) {
		goto err;
	}
	if ((res = pms_alps_cm_read_1(psc->sc_kbctag, psc->sc_kbcslot, 0xc2d9, &val)) != 0) {
		goto err;
	}
	if ((res = pms_alps_get_resolution_v7(psc)) != 0) {
		goto err;
	}
	if ((res = pms_alps_cm_write_1(psc->sc_kbctag, psc->sc_kbcslot, 0xc2c9, 0x64)) != 0) {
		goto err;
	}

	/* Start absolute mode */
	if ((res = pms_alps_cm_read_1(psc->sc_kbctag, psc->sc_kbcslot, 0xc2c4, &val)) != 0) {
		goto err;
	}
	/* Do not set address before this, so do not use pms_cm_write_1() */
	val = val | __BIT(1);
	nibble = __SHIFTOUT(val, __BITS(7, 4));
	if ((res = pms_alps_cm_write_nibble(psc->sc_kbctag, psc->sc_kbcslot, nibble)) != 0) {
		goto err;
	}
	nibble = __SHIFTOUT(val, __BITS(3, 0));
	if ((res = pms_alps_cm_write_nibble(psc->sc_kbctag, psc->sc_kbcslot, nibble)) != 0) {
		goto err;
	}

	/* End command mode */
	if ((res = pms_alps_end_command_mode(psc)) != 0)
		goto err;

	return res;

err:
	(void)pms_alps_end_command_mode(psc);
	aprint_error_dev(psc->sc_dev, "Failed to initialize V7 device.\n");
	return res;
}

int
pms_alps_probe_init(void *opaque)
{
	struct pms_softc *psc = opaque;
	struct alps_softc *sc = &psc->u.alps;
	struct sysctllog *clog = NULL;
	uint8_t e6sig[3];
	uint8_t e7sig[3];
	uint8_t ecsig[3];
	int res;
	u_char cmd[1];

	sc->last_x1 = 0;
	sc->last_y1 = 0;
	sc->last_x2 = 0;
	sc->last_y2 = 0;
	sc->last_nfingers = 0;

	pckbport_flush(psc->sc_kbctag, psc->sc_kbcslot);

	if ((res = pms_alps_e6sig(psc, e6sig)) != 0)
		goto err;

	if ((res = pms_alps_e7sig(psc, e7sig)) != 0)
		goto err;

	if ((res = pms_alps_ecsig(psc, ecsig)) != 0)
		goto err;

	/* Determine protocol version */
	if ((ecsig[0] == 0x88) && (__SHIFTOUT(ecsig[1], __BITS(7, 4)) == 0x0b)) {
		/* V7 device in Toshiba dynabook R63/PS */
		sc->version = ALPS_PROTO_V7;
	} else if ((e7sig[0] == 0x73) && (e7sig[1] == 0x02) &&
		((e7sig[2] == 0x14) || (e7sig[2] == 0x0a))) {
		/* 0x14: V2 device in NEC VJ22MF-7 (VersaPro JVF-7) */
		/* 0x0a: V2 devices in Toshiba dynabook satellite B551/D
			 and dynabook SS RX1 */
		sc->version = ALPS_PROTO_V2;
	}

	if (sc->version == ALPS_PROTO_V7) {
		/* Initialize V7 device */
		if ((res = pms_alps_init_v7(psc)) != 0)
			goto err;
		aprint_normal_dev(psc->sc_dev,
			"ALPS PS/2 V7 pointing device\n");
	} else if (sc->version == ALPS_PROTO_V2) {
		/* Initialize V2 pointing device */
		if ((res = pms_alps_init_v2(psc)) != 0)
			goto err;
		aprint_normal_dev(psc->sc_dev,
			"ALPS PS/2 V2 pointing device\n");
	} else {
		res = EINVAL;
		goto err;
	}

	/* From sysctl */
	pms_sysctl_alps(&clog);
	/* Register hundler */
	if (sc->version == ALPS_PROTO_V7) {
		pckbport_set_inputhandler(psc->sc_kbctag, psc->sc_kbcslot,
			pms_alps_input_v7, psc, device_xname(psc->sc_dev));
	} else if (sc->version == ALPS_PROTO_V2) {
		pckbport_set_inputhandler(psc->sc_kbctag, psc->sc_kbcslot,
			pms_alps_input_v2, psc, device_xname(psc->sc_dev));
	} else {
		res = EINVAL;
		goto err;
	}
	/* Palm detection is enabled. */

	return 0;

err:
	cmd[0] = PMS_RESET;
	(void)pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
	    cmd, 1, 2, NULL, 1);
	if (res != ENODEV)
		aprint_error_dev(psc->sc_dev, "Failed to initialize an ALPS device.\n");
	return res;
}

void
pms_alps_enable(void *opaque)
{
	struct pms_softc *psc = opaque;
	struct alps_softc *sc = &psc->u.alps;

	sc->initializing = true;
}

void
pms_alps_resume(void *opaque)
{
	struct pms_softc *psc = opaque;
	struct alps_softc *sc = &psc->u.alps;
	uint8_t cmd, resp[2];
	int res;

	cmd = PMS_RESET;
	res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot, &cmd,
		1, 2, resp, 1);
	if (res)
	aprint_error_dev(psc->sc_dev,
		"ALPS reset on resume failed\n");
	else {
		if (sc->version == ALPS_PROTO_V7) {
			(void)pms_alps_init_v7(psc);
		} else if (sc->version == ALPS_PROTO_V2) {
			(void)pms_alps_init_v2(psc);
		} else {
			/* Not supported */
		}
		pms_alps_enable(psc);
	}
}

static void
pms_alps_decode_trackstick_packet_v7(struct pms_softc *psc)
{
	int s;

	int x, y, z;
	int dx, dy, dz;
	int left, middle, right;
	u_int buttons;

	x = (int8_t)((psc->packet[2] & 0xbf) |
		((psc->packet[3] & 0x10) << 2));
	y = (int8_t)((psc->packet[3] & 0x07) | (psc->packet[4] & 0xb8) |
		((psc->packet[3] & 0x20) << 1));
	z = (int8_t)((psc->packet[5] & 0x3f) |
		((psc->packet[3] & 0x80) >> 1));

	dx = x * alps_trackstick_xy_precision;
	dy = y * alps_trackstick_xy_precision;
	dz = z * 1;

	left = psc->packet[1] & 0x01;
	middle = (psc->packet[1] & 0x04) >> 2;
	right = (psc->packet[1] & 0x02) >> 1;
	buttons = 0;
	buttons = (u_int)((left << 0) | (middle << 1) | (right << 2));

	s = spltty();
	wsmouse_input(psc->sc_wsmousedev,
		buttons,
		dx, dy, dz, 0,
		WSMOUSE_INPUT_DELTA);
	splx(s);
}

static uint8_t
pms_alps_decode_packetid_v7(struct pms_softc *psc)
{
	if (psc->packet[4] & 0x40)
		return ALPS_V7_PACKETID_TWOFINGER;
	else if (psc->packet[4] & 0x01)
		return ALPS_V7_PACKETID_MULTIFINGER;
	else if ((psc->packet[0] & 0x10) && !(psc->packet[4] & 0x43))
		return ALPS_V7_PACKETID_NEWPACKET;
	else if ((psc->packet[1] == 0x00) && (psc->packet[4] == 0x00))
		return ALPS_V7_PACKETID_IDLE;
	else
		return ALPS_V7_PACKETID_UNKNOWN;
}

static void
pms_alps_decode_touchpad_packet_v7(struct pms_softc *psc)
{
	int s;
	struct alps_softc *sc = &psc->u.alps;
	uint8_t packetid;

	uint16_t cur_x1, cur_y1;
	uint16_t cur_x2, cur_y2;
	int dx1, dy1;
	int button;
	u_int buttons;

	packetid = pms_alps_decode_packetid_v7(psc);
	switch (packetid) {
	case ALPS_V7_PACKETID_IDLE:
		/* Accept meaningful packets only */
		return;
	case ALPS_V7_PACKETID_UNKNOWN:
		/* Accept meaningful packets only */
		return;
	case ALPS_V7_PACKETID_NEWPACKET:
		/* Sent new packet ID to reset status and not decoded */
		sc->initializing = true;
		return;
	}

	/* Decode a number of fingers and locations */
	/* X0-11 ... X0-0 */
	cur_x1 = (psc->packet[2] & 0x80) << 4; /* X0-11 */
	cur_x1 |= (psc->packet[2] & 0x3f) << 5; /* X0-10 ... X0-5 */
	cur_x1 |= (psc->packet[3] & 0x30) >> 1; /* X0-4, X0-3 */
	cur_x1 |= psc->packet[3] & 0x07; /* X0-2 ... X0-0 */

	/* Y0-10 ... Y0-0 */
	cur_y1 = psc->packet[1] << 3; /* Y0-10 ... Y0-3 */
	cur_y1 |= psc->packet[0] & 0x07; /* Y0-2 ... Y0-0 */

	/* X1-11 ... X1-3 */
	cur_x2 = (psc->packet[3] & 0x80) << 4; /* X1-11 */
	cur_x2 |= (psc->packet[4] & 0x80) << 3; /* X1-10 */
	cur_x2 |= (psc->packet[4] & 0x3f) << 4; /* X1-9 ... X1-4 */

	/* Y1-10 ... Y1-4 */
	cur_y2 = (psc->packet[5] & 0x80) << 3; /* Y1-10 */
	cur_y2 |= (psc->packet[5] & 0x3f) << 4; /* Y1-9 .. Y1-4 */

	switch (packetid) {
	case ALPS_V7_PACKETID_TWOFINGER:
		cur_x2 &= ~__BITS(3, 0); /* Clear undefined locations */
		cur_y2 |= __BITS(3, 0); /* Fill undefined locations */
		break;
	case ALPS_V7_PACKETID_MULTIFINGER:
		cur_x2 &= ~__BITS(5, 0); /* Clear undefined locations */
		cur_y2 &= ~__BIT(5); /* Clear duplicate locations */
		cur_y2 |= (psc->packet[4] & __BIT(1)) << 4; /* Set another */
		cur_y2 |= __BITS(4, 0); /* Fill undefined locations */
		break;
	}

	cur_y1 = 0x7ff - cur_y1;
	cur_y2 = 0x7ff - cur_y2;

	/* Handle finger touch reported in cur_x2/y2. only */
	if (cur_x1 == 0 && cur_y1 == 0 && cur_x2 != 0 && cur_y2 != 0) {
		cur_x1 = cur_x2;
		cur_y1 = cur_y2;
		cur_x2 = 0;
		cur_y2 = 0;
	}

	switch (packetid) {
	case ALPS_V7_PACKETID_TWOFINGER:
		if ((cur_x2 == 0) && (cur_y2 == 0))
			sc->nfingers = 1;
		else
			sc->nfingers = 2;
		break;
	case ALPS_V7_PACKETID_MULTIFINGER:
		sc->nfingers = 3 + (psc->packet[5] & 0x03);
		break;
	}

	button = (psc->packet[0] & 0x80) >> 7;
	buttons = 0;
	if (sc->nfingers == 1) {
		if (button && (cur_y1 > 1700) && (cur_x1 < 1700))
			buttons |= button << 0; /* Left button */
		else if (button && (cur_y1 > 1700)
				&& (1700 <= cur_x1) && (cur_x1 <= 2700))
			buttons |= button << 1; /* Middle button */
		else if (button && (cur_y1 > 1700) && (2700 < cur_x1))
			buttons |= button << 2; /* Right button */
	} else if (sc->nfingers > 1) {
		if (button && (cur_y2 > 1700) && (cur_x2 < 1700))
			buttons |= button << 0; /* Left button */
		else if (button && (cur_y2 > 1700)
				&& (1700 <= cur_x2) && (cur_x2 <= 2700))
			buttons |= button << 1; /* Middle button */
		else if (button && (cur_y2 > 1700) && (2700 < cur_x2))
			buttons |= button << 2; /* Right button */
	}

	/* New touch */
	if (sc->nfingers == 0 || sc->nfingers != sc->last_nfingers)
		sc->initializing = true;

	if (sc->initializing == true) {
		dx1 = 0;
		dy1 = 0;
	} else {
		dx1 = (int16_t)(cur_x1 - sc->last_x1);
		dy1 = (int16_t)(sc->last_y1 - cur_y1);

		dx1 = dx1 >> alps_touchpad_xy_unprecision;
		dy1 = dy1 >> alps_touchpad_xy_unprecision;
	}

	/* Allow finger detouch during drag and drop */
	if ((sc->nfingers < sc->last_nfingers)
		&& (cur_x2 == sc->last_x1) && (cur_y2 == sc->last_y1)) {
		sc->last_x1 = sc->last_x2;
		sc->last_y1 = sc->last_y2;
		dx1 = 0;
		dy1 = 0;
	}

	s = spltty();
	wsmouse_input(psc->sc_wsmousedev,
		buttons,
		dx1, dy1, 0, 0,
		WSMOUSE_INPUT_DELTA);
	splx(s);

	if (sc->initializing == true || (dx1 != 0))
		sc->last_x1 = cur_x1;
	if (sc->initializing == true || (dy1 != 0))
		sc->last_y1 = cur_y1;

	if (sc->nfingers > 0)
		sc->initializing = false;
	sc->last_nfingers = sc->nfingers;
}

static void
pms_alps_dispatch_packet_v7(struct pms_softc *psc)
{
	if ((psc->packet[0] == 0x48) && ((psc->packet[4] & 0x47) == 0x06))
		pms_alps_decode_trackstick_packet_v7(psc);
	else
		pms_alps_decode_touchpad_packet_v7(psc);
}

static void
pms_alps_decode_touchpad_packet_v2(struct pms_softc *psc)
{
	int s;
	struct alps_softc *sc = &psc->u.alps;
	uint16_t cur_x, cur_y;
	int16_t dx, dy;
	u_int left, middle, right;
	u_int forward, back;
	u_int buttons;
	uint8_t ges;

	sc->nfingers = (psc->packet[2] & 0x02) >> 1;
	if (sc->last_nfingers == 0)
		sc->initializing = true;

	left = psc->packet[3] & 0x01;
	right = (psc->packet[3] & 0x02) >> 1;
	middle = (psc->packet[3] & 0x04) >> 2;

	cur_x = psc->packet[1];
	cur_x |= (psc->packet[2] & 0x78) << 4;

	cur_y = psc->packet[4];
	cur_y |= (psc->packet[3] & 0x70) << 3;

#if 0
	cur_z = psc->packet[5];
#endif

	forward = (psc->packet[2] & 0x04) >> 2;
	back = (psc->packet[3] & 0x04) >> 2;
	ges = psc->packet[2] & 0x01;

	buttons = (left | ges) << 0;
	buttons |= (middle | forward | back) << 1;
	buttons |= right << 2;

	if (sc->initializing == true) {
		dx = 0;
		dy = 0;
	} else {
		dx = (cur_x - sc->last_x1);
		dy = (sc->last_y1 - cur_y);

		dx = dx >> alps_touchpad_xy_unprecision;
		dy = dy >> alps_touchpad_xy_unprecision;
	}

	s = spltty();
	wsmouse_input(psc->sc_wsmousedev,
		buttons,
		dx, dy, 0, 0,
		WSMOUSE_INPUT_DELTA);
	splx(s);

	if (sc->initializing == true || (dx != 0))
		sc->last_x1 = cur_x;
	if (sc->initializing == true || (dy != 0))
		sc->last_y1 = cur_y;

	if (sc->nfingers > 0)
		sc->initializing = false;
	sc->last_nfingers = sc->nfingers;
}

static void
pms_alps_input_v2(void *opaque, int data)
{
	struct pms_softc *psc = opaque;

	if (!psc->sc_enabled)
		return;

	psc->packet[psc->inputstate++] = data & 0xff;
	if (psc->inputstate < 6)
		return;

	pms_alps_decode_touchpad_packet_v2(psc);

	psc->inputstate = 0;
}

static void
pms_alps_input_v7(void *opaque, int data)
{
	struct pms_softc *psc = opaque;

	if (!psc->sc_enabled)
		return;

	psc->packet[psc->inputstate++] = data & 0xff;
	if (psc->inputstate < 6)
		return;

	pms_alps_dispatch_packet_v7(psc);

	psc->inputstate = 0;
}