~kris/9p

9hist

ref: 0f3dcc9033d70b17140ee99e64444889e2dfa0f1 9hist/power/devbit3.c -rw-r--r-- 3.0 KiB
0f3dcc90 — David du Colombier Plan 9 from Bell Labs 1990-06-05 36 years ago
                                                                                
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
#include	"u.h"
#include	"lib.h"
#include	"mem.h"
#include	"dat.h"
#include	"fns.h"
#include	"errno.h"
#include	"devtab.h"

#include	"io.h"

static struct
{
	QLock;
	int	open;
	char	buf[10*1024];
}bit3;

#define	BIT3ADDR	((Bit3msg**)(KZERO+0x7C))
#define	BIT3HOLD	((ulong*)(KZERO+0x78))
#define	BIT3INTR	((char*)(UNCACHED|0x17c12001))

enum
{
	RESET,
	READ,
	WRITE,
};

void
bit3send(Bit3msg *bp, ulong cmd, void *addr, ulong count)
{
	do; while(*BIT3ADDR);
	bp->cmd = cmd;
	bp->addr = (ulong)addr;
	bp->count = count;
	*BIT3ADDR = bp;
	wbflush();
	do; while(*BIT3HOLD);
	*BIT3INTR = 0x20;
}

void
bit3reset(void)
{
	*BIT3HOLD = 0;
	*BIT3ADDR = 0;
	qlock(&bit3);
	qunlock(&bit3);
}

void
bit3init(void)
{
}

Chan*
bit3attach(char *spec)
{
	return devattach('3', spec);
}

Chan*
bit3clone(Chan *c, Chan *nc)
{
	return devclone(c, nc);
}

int	 
bit3walk(Chan *c, char *name)
{
	if(c->qid != CHDIR)
		return 0;
	if(strcmp(name, "bit3") == 0){
		c->qid = 1;
		return 1;
	}
	return 0;
}

void	 
bit3stat(Chan *c, char *dp)
{
	print("bit3stat\n");
	error(0, Egreg);
}

Chan*
bit3open(Chan *c, int omode)
{
	Bit3msg *bp;

	bp = &((User*)(u->p->upage->pa|KZERO))->bit3;
	if(c->qid!=1 || omode!=ORDWR)
		error(0, Eperm);
	qlock(&bit3);
	if(bit3.open){
		qunlock(&bit3);
		error(0, Einuse);
	}
	bit3send(bp, RESET, 0, 0);
	bit3.open = 1;
	qunlock(&bit3);
	c->mode = openmode(omode);
	c->flag |= COPEN;
	c->offset = 0;
	return c;
}

void	 
bit3create(Chan *c, char *name, int omode, ulong perm)
{
	error(0, Eperm);
}

void	 
bit3close(Chan *c)
{
	qlock(&bit3);
	bit3.open = 0;
	qunlock(&bit3);
}

/*
 * Read and write use physical addresses if they can, which they usually can.
 * Most I/O is from devmnt, which has local buffers.  Therefore just check
 * that buf is in KSEG0 and is at an even address.  The only killer is that
 * DMA counts from the bit3 device are mod 256, so devmnt must use oversize
 * buffers.
 */

long	 
bit3read(Chan *c, void *buf, long n)
{
	Bit3msg *bp;
	int docpy;

	bp = &((User*)(u->p->upage->pa|KZERO))->bit3;
	bp->rcount = 0;
	switch(c->qid){
	case 1:
		if(n > sizeof bit3.buf)
			error(0, Egreg);
		docpy = 0;
		qlock(&bit3);
		if((((ulong)buf)&(KSEGM|3)) == KSEG0)
			bit3send(bp, READ, buf, n);
		else{
			bit3send(bp, READ, bit3.buf, n);
			docpy = 1;
		}
		qunlock(&bit3);
		do
			n = bp->rcount;
		while(n == 0);
		if(docpy)
			memcpy(buf, bit3.buf, n);
		return n;
	}
	error(0, Egreg);
	return 0;
}

long	 
bit3write(Chan *c, void *buf, long n)
{
	Bit3msg *bp;

	bp = &((User*)(u->p->upage->pa|KZERO))->bit3;
	switch(c->qid){
	case 1:
		if(n > sizeof bit3.buf)
			error(0, Egreg);
		qlock(&bit3);
		if((((ulong)buf)&(KSEGM|3)) == KSEG0)
			bit3send(bp, WRITE, buf, n);
		else{
			memcpy(bit3.buf, buf, n);
			bit3send(bp, WRITE, bit3.buf, n);
		}
		qunlock(&bit3);
		return n;
	}
	error(0, Egreg);
	return 0;
}

void	 
bit3remove(Chan *c)
{
	error(0, Eperm);
}

void	 
bit3wstat(Chan *c, char *dp)
{
	error(0, Eperm);
}

void
bit3userstr(Error *e, char *buf)
{
	consuserstr(e, buf);
}

void	 
bit3errstr(Error *e, char *buf)
{
	rooterrstr(e, buf);
}