~kris/9p

9hist

ref: 1e9f8fab0a15fa6ab54455d7c64b7391ebbd4af3 9hist/power/devbit.c -rw-r--r-- 3.4 KiB
1e9f8fab — David du Colombier Plan 9 from Bell Labs 1990-03-09 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#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];
}bit;

#define	BITADDR	((Bitmsg**)(KZERO+0x7C))
#define	BITHOLD	((ulong*)(KZERO+0x78))
#define	BITINTR	((char*)(UNCACHED|0x17c12001))

enum
{
	RESET,
	READ,
	WRITE,
};

long	hold, wait, hang;

void
bitsend(Bitmsg *bp, ulong cmd, void *addr, ulong count)
{
	do wait++; while(*BITADDR);
	bp->cmd = cmd;
	bp->addr = (ulong)addr;
	bp->count = count;
/* print("%d %lux %d ", cmd, addr, count); /**/
	*BITADDR = bp;
	wbflush();
	do hold++; while(*BITHOLD);
	*BITINTR = 0x20;
/* print("done\n");  /**/
}

void
bitreset(void)
{
	*BITHOLD = 0;
	*BITADDR = 0;
	qlock(&bit);
	qunlock(&bit);
}

void
bitinit(void)
{
}

Chan*
bitattach(char *spec)
{
	return devattach('b', spec);
}

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

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

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

Chan*
bitopen(Chan *c, int omode)
{
	Bitmsg *bp;

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

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

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

/*
 * 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 bit device are mod 256, so devmnt must use oversize
 * buffers.
 */

long	 
bitread(Chan *c, void *buf, long n)
{
	Bitmsg *bp;
	int docpy;

	bp = &((User*)(u->p->upage->pa|KZERO))->bit;
	bp->rcount = 0;
	switch(c->qid){
	case 1:
		if(n > sizeof bit.buf)
			error(0, Egreg);
		docpy = 0;
		qlock(&bit);
		if((((ulong)buf)&(KSEGM|3)) == KSEG0)
			bitsend(bp, READ, buf, n);
		else{
			bitsend(bp, READ, bit.buf, n);
			docpy = 1;
		}
		qunlock(&bit);
		do{
			n = bp->rcount;
			hang++;
		}while(n == 0);
		if(docpy)
			memcpy(buf, bit.buf, n);
if(0 && n > 512){
	int i;
	char *cp=buf;
	for(i=9; i<n; i++)
		if(cp[i] != cp[i-1]){
			print("r %d %x %x\n", i, cp[i-1], cp[i]);
			break;
		}
}
		return n;
	}
	error(0, Egreg);
	return 0;
}

long	 
bitwrite(Chan *c, void *buf, long n)
{
	Bitmsg *bp;

	bp = &((User*)(u->p->upage->pa|KZERO))->bit;
	switch(c->qid){
	case 1:
		if(n > sizeof bit.buf)
			error(0, Egreg);
if(0 && n > 512){
	int i;
	char *cp=buf;
	for(i=15; i<n; i++)
		if(cp[i] != cp[i-1]){
			print("w %d %x %x\n", i, cp[i-1], cp[i]);
			break;
		}
}
		qlock(&bit);
		if((((ulong)buf)&(KSEGM|3)) == KSEG0)
			bitsend(bp, WRITE, buf, n);
		else{
			memcpy(bit.buf, buf, n);
			bitsend(bp, WRITE, bit.buf, n);
		}
		qunlock(&bit);
		return n;
	}
	error(0, Egreg);
	return 0;
}

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

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

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

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