Clean up code. Add comments. Init module with callback to register all
[bertos.git] / bertos / io / reblock.c
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 2011 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief KBlock block size reducer
34  *
35  * This module allows access to a KBlock device using a smaller block
36  * size than the native one exported by the device.
37  * Note that the device being remapped needs either to be opened in buffered
38  * mode or to support partial writes.
39  *
40  * \author Stefano Fedrigo <aleph@develer.com>
41  *
42  * $WIZ$ module_depends = "kblock"
43  */
44
45 #include "reblock.h"
46 #include <string.h> /* memset */
47
48
49 static size_t reblock_readDirect(struct KBlock *b, block_idx_t idx, void *buf, size_t offset, size_t size)
50 {
51         Reblock *r = REBLOCK_CAST(b);
52
53         offset += idx % (r->native_fd->blk_size / r->fd.blk_size) * r->fd.blk_size;
54         idx    =  idx / (r->native_fd->blk_size / r->fd.blk_size);
55
56         return kblock_read(r->native_fd, idx, buf, offset, size);
57 }
58
59
60 static size_t reblock_writeDirect(struct KBlock *b, block_idx_t idx, const void *buf, size_t offset, size_t size)
61 {
62         Reblock *r = REBLOCK_CAST(b);
63
64         offset += idx % (r->native_fd->blk_size / r->fd.blk_size) * r->fd.blk_size;
65         idx    =  idx / (r->native_fd->blk_size / r->fd.blk_size);
66
67         return kblock_write(r->native_fd, idx, buf, offset, size);
68 }
69
70
71 static int reblock_error(struct KBlock *b)
72 {
73         return kblock_error(REBLOCK_CAST(b)->native_fd);
74 }
75
76 static void reblock_clearerr(struct KBlock *b)
77 {
78         kblock_clearerr(REBLOCK_CAST(b)->native_fd);
79 }
80
81 static int reblock_close(struct KBlock *b)
82 {
83         return kblock_close(REBLOCK_CAST(b)->native_fd);
84 }
85
86
87 static const KBlockVTable reblock_vt =
88 {
89         .readDirect = reblock_readDirect,
90         .writeDirect = reblock_writeDirect,
91
92         .error = reblock_error,
93         .clearerr = reblock_clearerr,
94         .close = reblock_close,
95 };
96
97
98 /*
99  * Initialize reblock device.
100  *
101  * \param rbl           kblock reblock device
102  * \param native_fd     kblock descriptor of the reblocked device
103  * \param new_blk_size  new block size to export
104  *
105  * \note new block size is required to be a submultiple of the
106  *       native device block size.
107  */
108 void reblock_init(Reblock *rbl, KBlock *native_fd, size_t new_blk_size)
109 {
110         ASSERT(new_blk_size);
111         ASSERT(new_blk_size < native_fd->blk_size);
112         ASSERT(native_fd->blk_size % new_blk_size == 0);
113         ASSERT(kblock_buffered(native_fd) || kblock_partialWrite(native_fd));
114
115         memset(rbl, 0, sizeof(Reblock));
116
117         DB(rbl->fd.priv.type = KBT_REBLOCK);
118
119         rbl->fd.blk_size = new_blk_size;
120         rbl->fd.blk_cnt = native_fd->blk_cnt * (native_fd->blk_size / new_blk_size);
121
122         rbl->fd.priv.flags |= KB_PARTIAL_WRITE;
123         rbl->fd.priv.vt = &reblock_vt;
124
125         rbl->native_fd = native_fd;
126 }