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 "ao_sdcard.h"
35 static struct ao_bufio ao_bufio[AO_NUM_BUF];
36 static uint8_t ao_buffer[AO_NUM_BUF][AO_BUFSIZ];
37 static int16_t ao_seqno;
38 static uint8_t ao_bufio_mutex;
41 #define DBG(...) printf(__VA_ARGS__)
49 ao_mutex_get(&ao_bufio_mutex);
55 ao_mutex_put(&ao_bufio_mutex);
59 ao_seqno_age(int16_t s)
71 ao_seqno_older(int16_t a, int16_t b)
73 return ao_seqno_age(a) > ao_seqno_age(b);
77 ao_validate_bufno(int b)
79 if (b < 0 || AO_NUM_BUF <= b)
80 ao_panic(AO_PANIC_BUFIO);
84 ao_buf_to_num(uint8_t *buf)
86 int b = (buf - &ao_buffer[0][0]) / AO_BUFSIZ;
93 ao_bufio_to_num(struct ao_bufio *bufio)
95 int b = (bufio - ao_bufio);
101 static inline struct ao_bufio *
102 ao_buf_to_bufio(uint8_t *buf)
104 int b = ao_buf_to_num(buf);
105 struct ao_bufio *bufio;
107 bufio = &ao_bufio[b];
108 DBG ("buf %08x is %d bufio %08x\n", buf, b, bufio);
112 static inline uint8_t *
113 ao_bufio_to_buf(struct ao_bufio *bufio)
115 int b = ao_bufio_to_num(bufio);
118 buf = &ao_buffer[b][0];
119 DBG ("bufio %08x is %d buf %08x\n", bufio, b, buf);
124 * Write a buffer to storage if it is dirty
127 ao_bufio_write(struct ao_bufio *bufio)
130 ao_sdcard_write_block(bufio->block, ao_bufio_to_buf(bufio));
136 * Read a buffer from storage
139 ao_bufio_read(struct ao_bufio *bufio)
141 uint8_t *buf = ao_bufio_to_buf(bufio);
143 return ao_sdcard_read_block(bufio->block, buf);
147 * Find a buffer containing the specified block
149 static struct ao_bufio *
150 ao_bufio_find_block(uint32_t block)
154 for (b = 0; b < AO_NUM_BUF; b++) {
155 struct ao_bufio *bufio = &ao_bufio[b];
156 if (bufio->block == block) {
157 DBG ("Found existing buffer %d (seqno %d)\n",
158 ao_bufio_to_num(bufio), bufio->seqno);
166 * Find the least recently used idle buffer
168 static struct ao_bufio *
169 ao_bufio_find_idle(void)
172 struct ao_bufio *oldest = NULL;
174 for (b = 0; b < AO_NUM_BUF; b++) {
175 struct ao_bufio *bufio = &ao_bufio[b];
177 if (!oldest || ao_seqno_older(bufio->seqno, oldest->seqno))
181 DBG ("Using idle buffer %d (seqno %d)\n",
182 ao_bufio_to_num(oldest), oldest->seqno);
187 * Return a pointer to a buffer containing
188 * the contents of the specified block
191 ao_bufio_get(uint32_t block)
193 struct ao_bufio *bufio;
197 bufio = ao_bufio_find_block(block);
199 bufio = ao_bufio_find_idle();
201 ao_bufio_write(bufio);
202 bufio->block = block;
203 DBG ("read buffer\n");
204 if (!ao_bufio_read(bufio)) {
205 bufio->block = 0xffffffff;
209 ao_panic(AO_PANIC_BUFIO);
214 ao_panic(AO_PANIC_BUFIO);
215 buf = ao_bufio_to_buf(bufio);
222 * Release a buffer, marking it dirty
223 * if it has been written to
226 ao_bufio_put(uint8_t *buf, uint8_t write)
228 struct ao_bufio *bufio;
231 bufio = ao_buf_to_bufio(buf);
234 ao_panic(AO_PANIC_BUFIO);
236 DBG ("idle buffer %d write %d\n", ao_bufio_to_num(bufio), write);
237 bufio->dirty |= write;
238 if (!--bufio->busy) {
239 bufio->seqno = ao_seqno_next();
240 DBG ("not busy, seqno %d\n", bufio->seqno);
246 * Flush a single buffer immediately. Useful
247 * if write order is important
250 ao_bufio_flush_one(uint8_t *buf)
253 ao_bufio_write(ao_buf_to_bufio(buf));
258 * Flush all buffers to storage
266 for (b = 0; b < AO_NUM_BUF; b++)
267 ao_bufio_write(&ao_bufio[b]);
272 ao_bufio_test_read(void)
276 if (ao_cmd_status != ao_cmd_success)
278 if ((buf = ao_bufio_get(ao_cmd_lex_u32))) {
280 for (i = 0; i < 512; i++) {
281 printf (" %02x", buf[i]);
282 if ((i & 0xf) == 0xf)
285 ao_bufio_put(buf, 0);
289 static const struct ao_cmds ao_bufio_cmds[] = {
290 { ao_bufio_test_read, "q\0Test bufio read" },
299 for (b = 0; b < AO_NUM_BUF; b++)
300 ao_bufio[b].block = 0xffffffff;
303 ao_cmd_register(&ao_bufio_cmds[0]);