2 * Copyright © 2013 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 /* Include bufio commands */
24 #define BUFIO_COMMANDS 0
27 #include "ao_sdcard.h"
40 static struct ao_bufio ao_bufio[AO_NUM_BUF];
41 static uint8_t ao_buffer[AO_NUM_BUF][AO_BUFSIZ];
42 static int16_t ao_seqno;
43 static uint8_t ao_bufio_mutex;
46 #define DBG(...) printf(__VA_ARGS__)
48 #define DBG(...) (void) 0
54 ao_mutex_get(&ao_bufio_mutex);
60 ao_mutex_put(&ao_bufio_mutex);
64 ao_seqno_age(int16_t s)
76 ao_seqno_older(int16_t a, int16_t b)
78 return ao_seqno_age(a) > ao_seqno_age(b);
82 ao_validate_bufno(int b)
84 if (b < 0 || AO_NUM_BUF <= b)
85 ao_panic(AO_PANIC_BUFIO);
89 ao_buf_to_num(uint8_t *buf)
91 int b = (buf - &ao_buffer[0][0]) / AO_BUFSIZ;
98 ao_bufio_to_num(struct ao_bufio *bufio)
100 int b = (bufio - ao_bufio);
102 ao_validate_bufno(b);
106 static inline struct ao_bufio *
107 ao_buf_to_bufio(uint8_t *buf)
109 int b = ao_buf_to_num(buf);
110 struct ao_bufio *bufio;
112 bufio = &ao_bufio[b];
113 DBG ("buf %08x is %d bufio %08x\n", buf, b, bufio);
117 static inline uint8_t *
118 ao_bufio_to_buf(struct ao_bufio *bufio)
120 int b = ao_bufio_to_num(bufio);
123 buf = &ao_buffer[b][0];
124 DBG ("bufio %08x is %d buf %08x\n", bufio, b, buf);
129 * Write a buffer to storage if it is dirty
132 ao_bufio_write(struct ao_bufio *bufio)
135 ao_sdcard_write_block(bufio->block, ao_bufio_to_buf(bufio));
141 * Read a buffer from storage
144 ao_bufio_read(struct ao_bufio *bufio)
146 uint8_t *buf = ao_bufio_to_buf(bufio);
148 return ao_sdcard_read_block(bufio->block, buf);
152 * Find a buffer containing the specified block
154 static struct ao_bufio *
155 ao_bufio_find_block(uint32_t block)
159 for (b = 0; b < AO_NUM_BUF; b++) {
160 struct ao_bufio *bufio = &ao_bufio[b];
161 if (bufio->block == block) {
162 DBG ("Found existing buffer %d (seqno %d)\n",
163 ao_bufio_to_num(bufio), bufio->seqno);
171 * Find the least recently used idle buffer
173 static struct ao_bufio *
174 ao_bufio_find_idle(void)
177 struct ao_bufio *oldest = NULL;
179 for (b = 0; b < AO_NUM_BUF; b++) {
180 struct ao_bufio *bufio = &ao_bufio[b];
182 if (!oldest || ao_seqno_older(bufio->seqno, oldest->seqno))
186 DBG ("Using idle buffer %d (seqno %d)\n",
187 ao_bufio_to_num(oldest), oldest->seqno);
192 * Return a pointer to a buffer containing
193 * the contents of the specified block
196 ao_bufio_get(uint32_t block)
198 struct ao_bufio *bufio;
202 bufio = ao_bufio_find_block(block);
204 bufio = ao_bufio_find_idle();
206 ao_bufio_write(bufio);
207 bufio->block = block;
208 DBG ("read buffer\n");
209 if (!ao_bufio_read(bufio)) {
210 bufio->block = 0xffffffff;
214 ao_panic(AO_PANIC_BUFIO);
219 ao_panic(AO_PANIC_BUFIO);
220 buf = ao_bufio_to_buf(bufio);
227 * Release a buffer, marking it dirty
228 * if it has been written to
231 ao_bufio_put(uint8_t *buf, uint8_t write)
233 struct ao_bufio *bufio;
236 bufio = ao_buf_to_bufio(buf);
239 ao_panic(AO_PANIC_BUFIO);
241 DBG ("idle buffer %d write %d\n", ao_bufio_to_num(bufio), write);
242 bufio->dirty |= write;
243 if (!--bufio->busy) {
244 bufio->seqno = ao_seqno_next();
245 DBG ("not busy, seqno %d\n", bufio->seqno);
251 * Flush a single buffer immediately. Useful
252 * if write order is important
255 ao_bufio_flush_one(uint8_t *buf)
258 ao_bufio_write(ao_buf_to_bufio(buf));
263 * Flush all buffers to storage
271 for (b = 0; b < AO_NUM_BUF; b++)
272 ao_bufio_write(&ao_bufio[b]);
278 ao_bufio_test_read(void)
282 if (ao_cmd_status != ao_cmd_success)
284 if ((buf = ao_bufio_get(ao_cmd_lex_u32))) {
286 for (i = 0; i < 512; i++) {
287 printf (" %02x", buf[i]);
288 if ((i & 0xf) == 0xf)
291 ao_bufio_put(buf, 0);
295 static const struct ao_cmds ao_bufio_cmds[] = {
296 { ao_bufio_test_read, "q\0Test bufio read" },
306 for (b = 0; b < AO_NUM_BUF; b++) {
307 ao_bufio[b].dirty = 0;
308 ao_bufio[b].busy = 0;
309 ao_bufio[b].block = 0xffffffff;
319 ao_cmd_register(&ao_bufio_cmds[0]);