doc: Added group definitions for most common modules.
[bertos.git] / bertos / io / kblock.h
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
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.
10  *
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.
15  *
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
19  *
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.
28  *
29  * Copyright 2009 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \defgroup io_kblock KBlock interface
34  * \ingroup core
35  * \{
36  *
37  * \brief KBlock interface
38  *
39  * A block device is a device which can only be read/written
40  * with data blocks of constant size: flash memories,
41  * SD cards, hard disks, etc...
42  * This interface is designed to adapt to most block devices and
43  * use peculiar features in order to save CPU time and memory space.
44  *
45  * There is no init function because you do not have to use this
46  * structure directly, specific implementations will supply their own init
47  * functions.
48  *
49  * Error handling is done in a way similar to standard C library: whenever a
50  * function (eg. kblock_flush()) returns error, you need to check the error
51  * code, which is implementation specific.
52  *
53  * Example of code flow:
54  * \code
55  * // init a KBlock-derived class
56  * Flash fls;
57  * flash_init(&fls.blk, 0);
58  *
59  * // use kblock_* functions to access the derived class
60  * kblock_write(&fls.blk, ...);
61  * if (kblock_flush(&fls.blk) == EOF)
62  * {
63  *     // oops, error occurred!
64  *     int err = kblock_error(&fls.blk);
65  *     // handle Flash specific error conditions
66  *     // ...
67  *     // clear error condition
68  *     kblock_clearerr(&fls.blk);
69  * }
70  * \endcode
71  *
72  * \author Francesco Sacchi <batt@develer.com>
73  *
74  * $WIZ$ module_name = "kblock"
75  */
76
77 #ifndef IO_KBLOCK_H
78 #define IO_KBLOCK_H
79
80 #include <cfg/compiler.h>
81 #include <cfg/debug.h>
82 #include <cfg/macros.h>
83
84 /** Type for addressing blocks in the device. */
85 typedef uint32_t block_idx_t;
86
87 // Fwd Declaration
88 struct KBlock;
89
90 /**
91  * \name Prototypes for KBlock low level access functions.
92  *
93  * When writing a driver implementing the KBlock interface you can choose which
94  * function subset to implement, but you have to set to NULL unimplemented
95  * features.
96  *
97  *  \{
98  */
99 typedef size_t (* kblock_read_direct_t)  (struct KBlock *b, block_idx_t index, void *buf, size_t offset, size_t size);
100 typedef size_t (* kblock_write_direct_t) (struct KBlock *b, block_idx_t index, const void *buf, size_t offset, size_t size);
101
102 typedef size_t (* kblock_read_t)        (struct KBlock *b, void *buf, size_t offset, size_t size);
103 typedef size_t (* kblock_write_t)       (struct KBlock *b, const void *buf, size_t offset, size_t size);
104 typedef int    (* kblock_load_t)        (struct KBlock *b, block_idx_t index);
105 typedef int    (* kblock_store_t)       (struct KBlock *b, block_idx_t index);
106
107 typedef int    (* kblock_error_t)       (struct KBlock *b);
108 typedef void   (* kblock_clearerr_t)    (struct KBlock *b);
109 typedef int    (* kblock_close_t)       (struct KBlock *b);
110 /* \} */
111
112 /*
113  * Table of interface functions for a KBlock device.
114  */
115 typedef struct KBlockVTable
116 {
117         kblock_read_direct_t readDirect;
118         kblock_write_direct_t writeDirect;
119
120         kblock_read_t  readBuf;
121         kblock_write_t writeBuf;
122         kblock_load_t  load;
123         kblock_store_t store;
124
125         kblock_error_t    error;    // \sa kblock_error()
126         kblock_clearerr_t clearerr; // \sa kblock_clearerr()
127
128         kblock_close_t  close; // \sa kblock_close()
129 } KBlockVTable;
130
131
132 #define KB_BUFFERED        BV(0) ///< Internal flag: true if the KBlock has a buffer
133 #define KB_CACHE_DIRTY     BV(1) ///< Internal flag: true if the cache is dirty
134 #define KB_PARTIAL_WRITE   BV(2) ///< Internal flag: true if the device allows partial block write
135
136
137 /*
138  * KBlock private members.
139  * These are the private members of the KBlock interface, please do not
140  * access these directly, use the KBlock API.
141  */
142 typedef struct KBlockPriv
143 {
144         DB(id_t type);         // Used to keep track, at runtime, of the class type.
145         int flags;             // Status and error flags.
146         void *buf;             // Pointer to the page buffer for RAM-cached KBlocks.
147         block_idx_t blk_start; // Start block number when the device is trimmed. \sa kblock_trim().
148         block_idx_t curr_blk;  // Current cached block number in cached KBlocks.
149
150         const struct KBlockVTable *vt; // Virtual table of interface functions.
151 } KBlockPriv;
152
153 /**
154  * KBlock: interface for a generic block device.
155  *
156  */
157 typedef struct KBlock
158 {
159         KBlockPriv priv;         ///< Interface private data, do not use directly.
160
161         /* Public access members */
162         size_t blk_size;         ///< Block size.
163         block_idx_t blk_cnt;     ///< Number of blocks available in the device.
164 } KBlock;
165
166
167 /**
168  * Use a subset of the blocks on the device.
169  *
170  * This function is useful for partitioning a device and use it for
171  * different purposes at the same time.
172  *
173  * This function will limit the number of blocks used on the device by setting
174  * a start index and a number of blocks to be used counting from that index.
175  *
176  * The blocks outside this range are no more accessible.
177  *
178  * Logical block indexes will be mapped to physical indexes inside this new
179  * range automatically. Even following calls to kblock_trim() will use logical
180  * indexes, so, once trimmed, access can only be limited further and never
181  * expanded back.
182  *
183  * Example:
184  * \code
185  * //...init KBlock device dev
186  * kblock_trim(dev, 200, 1500); // Restrict access to the 200-1700 physical block range.
187  * kblock_read(dev, 0, buf, 0, dev->blk_size);  // Read from physical block #200.
188  * kblock_trim(dev, 0, 300); // Restrict access to the 200-500 physical block range.
189  * \endcode
190  *
191  * \param b KBlock device.
192  * \param start The index of the start block for the limiting window in logical addressing units.
193  * \param count The number of blocks to be used.
194  *
195  */
196 INLINE void kblock_trim(struct KBlock *b, block_idx_t start, block_idx_t count)
197 {
198         ASSERT(start + count <= b->blk_cnt);
199         b->priv.blk_start += start;
200         b->blk_cnt = count;
201 }
202
203
204 #define KB_ASSERT_METHOD(b, method) \
205         do \
206         { \
207                 ASSERT(b); \
208                 ASSERT((b)->priv.vt); \
209                 ASSERT((b)->priv.vt->method); \
210         } \
211         while (0)
212
213
214 /**
215  * Get the current errors for the device.
216  *
217  * \note Calling this function will not clear the errors.
218  *
219  * \param b KBlock device.
220  *
221  * \return 0 if no error is present, a driver specific mask of errors otherwise.
222  *
223  * \sa kblock_clearerr()
224  */
225 INLINE int kblock_error(struct KBlock *b)
226 {
227         KB_ASSERT_METHOD(b, error);
228         return b->priv.vt->error(b);
229 }
230
231 /**
232  * Clear the errors of the device.
233  *
234  * \param b KBlock device.
235  *
236  *
237  * \sa kblock_error()
238  */
239 INLINE void kblock_clearerr(struct KBlock *b)
240 {
241         KB_ASSERT_METHOD(b, clearerr);
242         b->priv.vt->clearerr(b);
243 }
244
245
246 /**
247  * Flush the cache (if any) to the device.
248  *
249  * This function will write any pending modifications to the device.
250  * If the device does not have a cache, this function will do nothing.
251  *
252  * \return 0 if all is OK, EOF on errors.
253  * \sa kblock_read(), kblock_write(), kblock_buffered().
254  */
255 int kblock_flush(struct KBlock *b);
256
257 /**
258  * Close the device.
259  *
260  * \param b KBlock device.
261  *
262  * \return 0 on success, EOF on errors.
263  */
264 INLINE int kblock_close(struct KBlock *b)
265 {
266         KB_ASSERT_METHOD(b, close);
267         return kblock_flush(b) | b->priv.vt->close(b);
268 }
269
270 /**
271  * \return true if the device \a b is buffered, false otherwise.
272  * \param b KBlock device.
273  * \sa kblock_cachedBlock(), kblock_cacheDirty().
274  */
275 INLINE bool kblock_buffered(struct KBlock *b)
276 {
277         ASSERT(b);
278         return (b->priv.flags & KB_BUFFERED);
279 }
280
281
282 /**
283  * \return The current cached block number if the device is buffered.
284  * \param b KBlock device.
285  * \note   This function will throw an ASSERT if called on a non buffered KBlock.
286  * \sa kblock_buffered(), kblock_cacheDirty().
287  */
288 INLINE block_idx_t kblock_cachedBlock(struct KBlock *b)
289 {
290         ASSERT(kblock_buffered(b));
291         return b->priv.curr_blk;
292 }
293
294
295 /**
296  * Return the status of the internal cache.
297  *
298  * \param b KBlock device.
299  * \return If the device supports buffering, returns true if the cache is dirty,
300  *         false if the cache is clean and coherent with device content.
301  * \note   This function will throw an ASSERT if called on a non buffered KBlock.
302  * \sa kblock_cachedBlock(), kblock_buffered().
303  */
304 INLINE bool kblock_cacheDirty(struct KBlock *b)
305 {
306         ASSERT(kblock_buffered(b));
307         return kblock_buffered(b) && (b->priv.flags & KB_CACHE_DIRTY);
308 }
309
310 /**
311  * \return true if the device \a b supports partial block write. That is, you
312  *         can call kblock_write() with a size which is lesser than the block
313  *         size.
314  * \param b KBlock device.
315  * \sa kblock_write().
316  */
317 INLINE bool kblock_partialWrite(struct KBlock *b)
318 {
319         ASSERT(b);
320         return (b->priv.flags & KB_PARTIAL_WRITE);
321 }
322
323 /**
324  * Read data from the block device.
325  *
326  * This function will read \a size bytes from block \a idx starting at
327  * address \a offset inside the block.
328  *
329  * Most block devices (almost all flash memories, for instance),
330  * can efficiently read even a part of the block.
331  *
332  * \note This function can be slow if you try to partial read a block from
333  *       a device which does not support partial block reads and is opened
334  *       in unbuffered mode.
335  *
336  * \param b KBlock device.
337  * \param idx the block number where you want to read.
338  * \param buf a buffer where the data will be read.
339  * \param offset the offset inside the block from which data reading will start.
340  * \param size the size of data to be read.
341  *
342  * \return the number of bytes read.
343  *
344  * \sa kblock_write().
345  */
346 size_t kblock_read(struct KBlock *b, block_idx_t idx, void *buf, size_t offset, size_t size);
347
348
349 /**
350  * Write data to the block device.
351  *
352  * This function will write \a size bytes to block \a idx starting at
353  * address \a offset inside the block.
354  *
355  * \note Partial block writes are supported only on certain devices.
356  *       You can use kblock_partialWrite() in order to check if the device
357  *       has this feature or not.
358  *
359  * \note If the device is opened in buffered mode, this function will use
360  *       efficiently and trasparently the cache provided.
361  *       In order to be sure that all modifications are actually written
362  *       to the device you have to call kblock_flush().
363  *
364  * \param b KBlock device.
365  * \param idx the block number where you want to write.
366  * \param buf a pointer to the data to be written.
367  * \param offset the offset inside the block from which data writing will start.
368  * \param size the size of data to be written.
369  *
370  * \return the number of bytes written.
371  *
372  * \sa kblock_read(), kblock_flush(), kblock_buffered(), kblock_partialWrite().
373  */
374 size_t kblock_write(struct KBlock *b, block_idx_t idx, const void *buf, size_t offset, size_t size);
375
376 /**
377  * Copy one block to another.
378  *
379  * This function will copy the content of block \a src to block \a dest.
380  *
381  * \note This function is available only on devices which support partial
382  *       block write or are opened in buffered mode.
383  *
384  * \param b KBlock device.
385  * \param src source block number.
386  * \param dest destination block number.
387  *
388  * \return 0 if all is OK, EOF on errors.
389  */
390 int kblock_copy(struct KBlock *b, block_idx_t src, block_idx_t dest);
391
392 int kblock_swLoad(struct KBlock *b, block_idx_t index);
393 int kblock_swStore(struct KBlock *b, block_idx_t index);
394 size_t kblock_swReadBuf(struct KBlock *b, void *buf, size_t offset, size_t size);
395 size_t kblock_swWriteBuf(struct KBlock *b, const void *buf, size_t offset, size_t size);
396 int kblock_swClose(struct KBlock *b);
397
398 /** \} */ //defgroup io_kblock
399
400
401 #endif /* IO_KBLOCK_H */