summaryrefslogtreecommitdiff
path: root/test/test-pad.c
blob: f4c27e9322231236c5ae92d745cd3aeb333509ca (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
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

int
getbyte(int fd, unsigned char *b)
{
    return(read(fd, b, 1) == 1);
}

int
putbyte(int fd, unsigned char b)
{
    unsigned char ack;

    printf("write %02X\n", b);
    if (write(fd, &b, 1) != 1) {
	fprintf(stderr, "error write: %s\n", strerror(errno));
	return 0;
    }

    if (!getbyte(fd, &ack)) {
	fprintf(stderr, "error read: %s\n", strerror(errno));
	return 0;
    }
    printf("read %02X\n", ack);

    if (ack != 0xFA) {
	fprintf(stderr, "error ack\n");
	return 0;
    }

    return 1;
}

int
special_cmd(int fd, unsigned char cmd)
{
    int i;

    if (putbyte(fd, 0xE6))
	for (i = 0; i < 4; i++) {
	    printf("special_cmd %i\n", i);
	    if ((!putbyte(fd, 0xE8)) || (!putbyte(fd, (cmd>>6)&0x3)))
		return 0;
	    cmd<<=2;
	}
    else
	return 0;
    return 1;
}

int
send_cmd(int fd, unsigned char cmd)
{
    return (special_cmd(fd, cmd) &&
	    putbyte(fd, 0xE9));
}

int
identify(int fd, unsigned long int *ident)
{
    unsigned char id[3];

    if (send_cmd(fd, 0x00) &&
	getbyte(fd, &id[0]) &&
	getbyte(fd, &id[1]) &&
	getbyte(fd, &id[2])) {
	*ident = (id[0]<<16)|(id[1]<<8)|id[2];
	printf("ident %06X\n", *ident);
	return 1;
    } else {
	fprintf(stderr, "error identify\n");
	return 0;
    }
}

int
reset(int fd)
{
    unsigned char r[2];

    if (!putbyte(fd, 0xFF)) {
	fprintf(stderr, "error reset\n");
	return 0;
    }

    sleep(5);

    if (getbyte(fd, &r[0]) && getbyte(fd, &r[1]))
	if (r[0] == 0xAA && r[1] == 0x00) {
	    fprintf(stderr, "reset done\n");
	    return 1;
	}
    fprintf(stderr, "error reset ack\n");
    return 0;
}

int
main(int argc, char* argv[])
{
    int fd;
    unsigned long int ident;

    fd = open("/dev/psaux", O_RDWR);
    if (fd == -1) {
	fprintf(stderr, "error open: %s\n", strerror(errno));
	exit(0);
    }

    reset(fd);
    identify(fd, &ident);

    close(fd);

    exit(0);
}