4 * This file is part of BeRTOS.
6 * Bertos is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * As a special exception, you may use this file as part of a free software
21 * library without restriction. Specifically, if other files instantiate
22 * templates or use macros or inline functions from this file, or you compile
23 * this file and link it with other files to produce an executable, this
24 * file does not by itself cause the resulting executable to be covered by
25 * the GNU General Public License. This exception does not however
26 * invalidate any other reasons why the executable file might be covered by
27 * the GNU General Public License.
29 * Copyright 2009 Develer S.r.l. (http://www.develer.com/)
33 * \author Francesco Sacchi <batt@develer.com>
35 * \brief KBlock interface
37 * $WIZ$ module_name = "kblock"
43 #include <cfg/compiler.h>
44 #include <cfg/debug.h>
45 #include <cfg/macros.h>
47 /** Type for addressing blocks in the device. */
48 typedef uint32_t block_idx_t;
54 * \name Prototypes for KBlock low level access functions.
56 * When writing a driver implementing the KBlock interface you can choose which
57 * function subset to implement, but you have to set to NULL unimplemented
62 typedef size_t (* kblock_read_direct_t) (struct KBlock *b, block_idx_t index, void *buf, size_t offset, size_t size);
63 typedef size_t (* kblock_write_direct_t) (struct KBlock *b, block_idx_t index, const void *buf, size_t offset, size_t size);
65 typedef size_t (* kblock_read_t) (struct KBlock *b, void *buf, size_t offset, size_t size);
66 typedef size_t (* kblock_write_t) (struct KBlock *b, const void *buf, size_t offset, size_t size);
67 typedef int (* kblock_load_t) (struct KBlock *b, block_idx_t index);
68 typedef int (* kblock_store_t) (struct KBlock *b, block_idx_t index);
70 typedef int (* kblock_error_t) (struct KBlock *b);
71 typedef void (* kblock_clearerr_t) (struct KBlock *b);
72 typedef int (* kblock_close_t) (struct KBlock *b);
76 * Table of interface functions for a KBlock device.
78 typedef struct KBlockVTable
80 kblock_read_direct_t readDirect;
81 kblock_write_direct_t writeDirect;
83 kblock_read_t readBuf;
84 kblock_write_t writeBuf;
88 kblock_error_t error; // \sa kblock_error()
89 kblock_clearerr_t clearerr; // \sa kblock_clearerr()
91 kblock_close_t close; // \sa kblock_close()
95 #define KB_BUFFERED BV(0) ///< Internal flag: true if the KBlock has a buffer
96 #define KB_CACHE_DIRTY BV(1) ///< Internal flag: true if the cache is dirty
97 #define KB_PARTIAL_WRITE BV(2) ///< Internal flag: true if the device allows partial block write
101 * KBlock private members.
102 * These are the private members of the KBlock interface, please do not
103 * access these directly, use the KBlock API.
105 typedef struct KBlockPriv
107 DB(id_t type); // Used to keep track, at runtime, of the class type.
108 int flags; // Status and error flags.
109 void *buf; // Pointer to the page buffer for RAM-cached KBlocks.
110 block_idx_t blk_start; // Start block number when the device is trimmed. \sa kblock_trim().
111 block_idx_t curr_blk; // Current cached block number in cached KBlocks.
113 const struct KBlockVTable *vt; // Virtual table of interface functions.
117 * KBlock: interface for a generic block device.
119 * A block device is a device which can only be read/written
120 * with data blocks of constant size: flash memories,
121 * SD cards, hard disks, etc...
123 * This interface is designed to adapt to most block devices and
124 * use peculiar features in order to save CPU time and memory space.
126 * You do not have to use this structure directly, specific implementations
127 * will be supplied in the peripheral drivers.
129 typedef struct KBlock
131 KBlockPriv priv; ///< Interface private data, do not use directly.
133 /* Public access members */
134 size_t blk_size; ///< Block size.
135 block_idx_t blk_cnt; ///< Number of blocks available in the device.
140 * Use a subset of the blocks on the device.
142 * This function is useful for partitioning a device and use it for
143 * different purposes at the same time.
145 * This function will limit the number of blocks used on the device by setting
146 * a start index and a number of blocks to be used counting from that index.
148 * The blocks outside this range are no more accessible.
150 * Logical block indexes will be mapped to physical indexes inside this new
151 * range automatically. Even following calls to kblock_trim() will use logical
152 * indexes, so, once trimmed, access can only be limited further and never
157 * //...init KBlock device dev
158 * kblock_trim(dev, 200, 1500); // Restrict access to the 200-1700 physical block range.
159 * kblock_read(dev, 0, buf, 0, dev->blk_size); // Read from physical block #200.
160 * kblock_trim(dev, 0, 300); // Restrict access to the 200-500 physical block range.
163 * \param b KBlock device.
164 * \param start The index of the start block for the limiting window in logical addressing units.
165 * \param count The number of blocks to be used.
168 INLINE void kblock_trim(struct KBlock *b, block_idx_t start, block_idx_t count)
170 ASSERT(start + count <= b->blk_cnt);
171 b->priv.blk_start += start;
176 #define KB_ASSERT_METHOD(b, method) \
180 ASSERT((b)->priv.vt); \
181 ASSERT((b)->priv.vt->method); \
187 * Get the current errors for the device.
189 * \note Calling this function will not clear the errors.
191 * \param b KBlock device.
193 * \return 0 if no error is present, a driver specific mask of errors otherwise.
195 * \sa kblock_clearerr()
197 INLINE int kblock_error(struct KBlock *b)
199 KB_ASSERT_METHOD(b, error);
200 return b->priv.vt->error(b);
204 * Clear the errors of the device.
206 * \param b KBlock device.
211 INLINE void kblock_clearerr(struct KBlock *b)
213 KB_ASSERT_METHOD(b, clearerr);
214 b->priv.vt->clearerr(b);
219 * Flush the cache (if any) to the device.
221 * This function will write any pending modifications to the device.
222 * If the device does not have a cache, this function will do nothing.
224 * \return 0 if all is OK, EOF on errors.
225 * \sa kblock_read(), kblock_write(), kblock_buffered().
227 int kblock_flush(struct KBlock *b);
232 * \param b KBlock device.
234 * \return 0 on success, EOF on errors.
236 INLINE int kblock_close(struct KBlock *b)
238 KB_ASSERT_METHOD(b, close);
239 return kblock_flush(b) | b->priv.vt->close(b);
243 * \return true if the device \a b is buffered, false otherwise.
244 * \param b KBlock device.
245 * \sa kblock_cachedBlock(), kblock_cacheDirty().
247 INLINE bool kblock_buffered(struct KBlock *b)
250 return (b->priv.flags & KB_BUFFERED);
255 * \return The current cached block number if the device is buffered.
256 * \param b KBlock device.
257 * \note This function will throw an ASSERT if called on a non buffered KBlock.
258 * \sa kblock_buffered(), kblock_cacheDirty().
260 INLINE block_idx_t kblock_cachedBlock(struct KBlock *b)
262 ASSERT(kblock_buffered(b));
263 return b->priv.curr_blk;
268 * Return the status of the internal cache.
270 * \param b KBlock device.
271 * \return If the device supports buffering, returns true if the cache is dirty,
272 * false if the cache is clean and coherent with device content.
273 * \note This function will throw an ASSERT if called on a non buffered KBlock.
274 * \sa kblock_cachedBlock(), kblock_buffered().
276 INLINE bool kblock_cacheDirty(struct KBlock *b)
278 ASSERT(kblock_buffered(b));
279 return kblock_buffered(b) && (b->priv.flags & KB_CACHE_DIRTY);
283 * \return true if the device \a b supports partial block write. That is, you
284 * can call kblock_write() with a size which is lesser than the block
286 * \param b KBlock device.
287 * \sa kblock_write().
289 INLINE bool kblock_partialWrite(struct KBlock *b)
292 return (b->priv.flags & KB_PARTIAL_WRITE);
296 * Read data from the block device.
298 * This function will read \a size bytes from block \a idx starting at
299 * address \a offset inside the block.
301 * Most block devices (almost all flash memories, for instance),
302 * can efficiently read even a part of the block.
304 * \note This function can be slow if you try to partial read a block from
305 * a device which does not support partial block reads and is opened
306 * in unbuffered mode.
308 * \param b KBlock device.
309 * \param idx the block number where you want to read.
310 * \param buf a buffer where the data will be read.
311 * \param offset the offset inside the block from which data reading will start.
312 * \param size the size of data to be read.
314 * \return the number of bytes read.
316 * \sa kblock_write().
318 size_t kblock_read(struct KBlock *b, block_idx_t idx, void *buf, size_t offset, size_t size);
322 * Write data to the block device.
324 * This function will write \a size bytes to block \a idx starting at
325 * address \a offset inside the block.
327 * \note Partial block writes are supported only on certain devices.
328 * You can use kblock_partialWrite() in order to check if the device
329 * has this feature or not.
331 * \note If the device is opened in buffered mode, this function will use
332 * efficiently and trasparently the cache provided.
333 * In order to be sure that all modifications are actually written
334 * to the device you have to call kblock_flush().
336 * \param b KBlock device.
337 * \param idx the block number where you want to write.
338 * \param buf a pointer to the data to be written.
339 * \param offset the offset inside the block from which data writing will start.
340 * \param size the size of data to be written.
342 * \return the number of bytes written.
344 * \sa kblock_read(), kblock_flush(), kblock_buffered(), kblock_partialWrite().
346 size_t kblock_write(struct KBlock *b, block_idx_t idx, const void *buf, size_t offset, size_t size);
349 * Copy one block to another.
351 * This function will copy the content of block \a src to block \a dest.
353 * \note This function is available only on devices which support partial
354 * block write or are opened in buffered mode.
356 * \param b KBlock device.
357 * \param src source block number.
358 * \param dest destination block number.
360 * \return 0 if all is OK, EOF on errors.
362 int kblock_copy(struct KBlock *b, block_idx_t src, block_idx_t dest);
364 int kblock_swLoad(struct KBlock *b, block_idx_t index);
365 int kblock_swStore(struct KBlock *b, block_idx_t index);
366 size_t kblock_swReadBuf(struct KBlock *b, void *buf, size_t offset, size_t size);
367 size_t kblock_swWriteBuf(struct KBlock *b, const void *buf, size_t offset, size_t size);
368 int kblock_swClose(struct KBlock *b);
370 #endif /* IO_KBLOCK_H */