File: buf_io.c

package info (click to toggle)
oskit 0.97.20000202-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 58,008 kB
  • ctags: 172,612
  • sloc: ansic: 832,827; asm: 7,640; sh: 3,920; yacc: 3,664; perl: 1,457; lex: 427; makefile: 337; csh: 141; awk: 78
file content (305 lines) | stat: -rw-r--r-- 6,546 bytes parent folder | download
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
/*
 * Copyright (c) 1997-1999 University of Utah and the Flux Group.
 * All rights reserved.
 * 
 * This file is part of the Flux OSKit.  The OSKit is free software, also known
 * as "open source;" you can redistribute it and/or modify it under the terms
 * of the GNU General Public License (GPL), version 2, as published by the Free
 * Software Foundation (FSF).  To explore alternate licensing terms, contact
 * the University of Utah at csl-dist@cs.utah.edu or +1-801-585-3271.
 * 
 * The OSKit 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 GPL for more details.  You should have
 * received a copy of the GPL along with the OSKit; see the file COPYING.  If
 * not, write to the FSF, 59 Temple Place #330, Boston, MA 02111-1307, USA.
 */
/*
 * Default implementation of buffer routines.
 * Besides providing a useful service,
 * this module serves as an example of how to implement bufio objects.
 */

#include <oskit/io/bufio.h>
#include <oskit/c/string.h>
#include <oskit/c/stdlib.h>
#include <oskit/c/assert.h>
#include <oskit/dev/osenv.h>
#include <oskit/dev/osenv_mem.h>

struct mybufio {
	oskit_bufio_t	ioi;		/* COM I/O Interface */
	unsigned 	count;		/* reference count */
	oskit_size_t	size;		/* size of buffer */
	/* the buffer itself follows... */
};
typedef struct mybufio mybufio_t;


/* 
 * Query a buffer I/O object for it's interfaces.
 *
 */
static OSKIT_COMDECL
bufio_query(oskit_bufio_t *io, const oskit_iid_t *iid, void **out_ihandle) 
{
	struct mybufio *b;

	b = (mybufio_t *)io;
	assert(b != NULL);
	assert(b->count != 0);

	if (memcmp(iid, &oskit_iunknown_iid, sizeof(*iid)) == 0 ||
	    memcmp(iid, &oskit_bufio_iid, sizeof(*iid)) == 0) {
		*out_ihandle = &b->ioi;
		++b->count;
		
		return 0;
	}

	*out_ihandle = NULL;
	return OSKIT_E_NOINTERFACE;
}

/*
 * Clone a reference to a device's block I/O interface.
 */
static OSKIT_COMDECL_U
bufio_addref(oskit_bufio_t *io)
{
	struct mybufio *b;

	b = (struct mybufio *)io;
	assert(b != NULL);
	assert(b->count != 0);

	return ++b->count;
}


static OSKIT_COMDECL_U
bufio_release(oskit_bufio_t *io)
{
	struct mybufio *b;
	unsigned newcount;
#ifndef CPUNITS
	oskit_osenv_t *osenv = 0;
	oskit_osenv_mem_t *osenv_mem = 0;
#endif

	b = (struct mybufio *)io;

	assert(b != NULL);
	assert(b->count != 0);

	if ((newcount = --b->count) == 0) {
#ifdef CPUNITS
		osenv_mem_free(b, OSENV_PHYS_WIRED | OSENV_AUTO_SIZE, 0);
#else /* !CPUNITS */
		/*
		 * Find the osenv, and then the osenv_mem interface.
		 */
		oskit_lookup_first(&oskit_osenv_iid, (void *) &osenv);
		assert(osenv);
		oskit_osenv_lookup(osenv,
				   &oskit_osenv_mem_iid, (void *)&osenv_mem);
		assert(osenv_mem);
		
		oskit_osenv_mem_free(osenv_mem,
				     b, OSENV_PHYS_WIRED | OSENV_AUTO_SIZE, 0);
#endif /* !CPUNITS */
	}

	return newcount;
}

/*
 * Return the block size of this blkio object - always 1.
 */
static OSKIT_COMDECL_U
bufio_getblocksize(oskit_bufio_t *io)
{
	return 1;
}

/*
 * Copy data from a user buffer into kernel's address space.
 */
static OSKIT_COMDECL
bufio_read(oskit_bufio_t *io, void *dest, oskit_off_t offset, oskit_size_t count,
	   oskit_size_t *out_actual)
{
	struct mybufio *b;

	b = (struct mybufio *)io;

	assert(b != NULL);
	assert(b->count != 0);

	if (offset >= b->size)
		return OSKIT_EINVAL;
	if (offset + count > b->size)
		count = b->size - offset;

	memcpy(dest, (void *)(b+1) + offset, count);

	*out_actual = count;
	return 0;
}

/*
 * Copy data from kernel address space to a user buffer.
 */
static OSKIT_COMDECL
bufio_write(oskit_bufio_t *io, const void *src, oskit_off_t offset,
	    oskit_size_t count, oskit_size_t *out_actual)
{
	struct mybufio *b;

	b = (struct mybufio *)io;

	assert(b != NULL);
	assert(b->count != 0);

	if (offset >= b->size)
		return OSKIT_EINVAL;
	if (offset + count > b->size)
		count = b->size - offset;

	memcpy((void *)(b+1) + offset, src, count);

	*out_actual = count;
	return 0;
}

/*
 * Return the size of this buffer.
 */
static OSKIT_COMDECL
bufio_getsize(oskit_bufio_t *io, oskit_off_t *out_size)
{
	struct mybufio *b;

	b = (struct mybufio *)io;
	assert(b != NULL);
	assert(b->count != 0);

	*out_size = b->size;
	return 0;
}

static OSKIT_COMDECL
bufio_setsize(oskit_bufio_t *io, oskit_off_t size)
{
	struct mybufio *b;

	b = (struct mybufio *)io;

	assert(b != NULL);
	assert(b->count != 0);

	return OSKIT_E_NOTIMPL;
}


static OSKIT_COMDECL
bufio_map(oskit_bufio_t *io, void **out_addr,
	  oskit_off_t offset, oskit_size_t count)
{
	struct mybufio *b;

	b = (struct mybufio *)io;

	assert(b != NULL);
	assert(b->count != 0);

	if (offset + count > b->size)
		return OSKIT_EINVAL;

	*out_addr = (void*)(b+1) + offset; 

	return 0;
}

/* 
 * XXX These should probably do checking on their inputs so we could 
 * catch bugs in the code that calls them.
 */

static OSKIT_COMDECL
bufio_unmap(oskit_bufio_t *io, void *addr, oskit_off_t offset, oskit_size_t count)
{
	return 0;
}


static OSKIT_COMDECL 
bufio_wire(oskit_bufio_t *io, oskit_addr_t *out_physaddr,
	   oskit_off_t offset, oskit_size_t count)
{
	return OSKIT_E_NOTIMPL;
}

static OSKIT_COMDECL 
bufio_unwire(oskit_bufio_t *io, oskit_addr_t phys_addr,
	     oskit_off_t offset, oskit_size_t count)
{
	return OSKIT_E_NOTIMPL;
}

static OSKIT_COMDECL
bufio_copy(oskit_bufio_t *io, oskit_off_t offset, oskit_size_t count,
	   oskit_bufio_t **out_io)
{
	return OSKIT_E_NOTIMPL;
}


static struct oskit_bufio_ops bio_ops = {
	bufio_query, bufio_addref, bufio_release,
	bufio_getblocksize,
	bufio_read, bufio_write,
	bufio_getsize, bufio_setsize,
	bufio_map, bufio_unmap,
	bufio_wire, bufio_unwire,
	bufio_copy
};


oskit_bufio_t *
oskit_bufio_create(oskit_size_t size) 
{
	mybufio_t *b;
#ifndef CPUNITS
	oskit_osenv_t *osenv = 0;
	oskit_osenv_mem_t *osenv_mem = 0;
#endif

	/* XXX: must be wired memory for DMA drivers */

#ifdef CPUNITS
	b = (struct mybufio *)osenv_mem_alloc(
				sizeof(*b) + size,
				OSENV_PHYS_WIRED | OSENV_AUTO_SIZE, 0);
#else
	/*
	 * Find the osenv, and then the osenv_mem interface.
	 */
	oskit_lookup_first(&oskit_osenv_iid, (void *) &osenv);
	assert(osenv);
	oskit_osenv_lookup(osenv, &oskit_osenv_mem_iid, (void *)&osenv_mem);
	assert(osenv_mem);
	
	b = (struct mybufio *)oskit_osenv_mem_alloc(osenv_mem, 
				sizeof(*b) + size,
				OSENV_PHYS_WIRED | OSENV_AUTO_SIZE, 0);
#endif /* !CPUNITS */
	if (b == NULL)
		return NULL;

	b->ioi.ops = &bio_ops;
	b->count = 1;
	b->size = size;
	
	return &b->ioi;
}