]> git.karo-electronics.de Git - karo-tx-redboot.git/blob - packages/services/blib/v2_0/include/blib.h
Initial revision
[karo-tx-redboot.git] / packages / services / blib / v2_0 / include / blib.h
1 #ifndef CYGONCE_BLIB_H
2 #define CYGONCE_BLIB_H
3 //==========================================================================
4 //
5 //      blib.h
6 //
7 //      Block cache and access library  
8 //
9 //==========================================================================
10 //####ECOSGPLCOPYRIGHTBEGIN####
11 // -------------------------------------------
12 // This file is part of eCos, the Embedded Configurable Operating System.
13 // Copyright (C) 2003 Savin Zlobec 
14 //
15 // eCos is free software; you can redistribute it and/or modify it under
16 // the terms of the GNU General Public License as published by the Free
17 // Software Foundation; either version 2 or (at your option) any later version.
18 //
19 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
20 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
22 // for more details.
23 //
24 // You should have received a copy of the GNU General Public License along
25 // with eCos; if not, write to the Free Software Foundation, Inc.,
26 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
27 //
28 // As a special exception, if other files instantiate templates or use macros
29 // or inline functions from this file, or you compile this file and link it
30 // with other works to produce a work based on this file, this file does not
31 // by itself cause the resulting work to be covered by the GNU General Public
32 // License. However the source code for this file must still be made available
33 // in accordance with section (3) of the GNU General Public License.
34 //
35 // This exception does not invalidate any other reasons why a work based on
36 // this file might be covered by the GNU General Public License.
37 //
38 // -------------------------------------------
39 //####ECOSGPLCOPYRIGHTEND####
40 //==========================================================================
41 //#####DESCRIPTIONBEGIN####
42 //
43 // Author(s):     savin 
44 // Date:          2003-08-29
45 // Description: 
46 //
47 //####DESCRIPTIONEND####
48 //
49 //==========================================================================
50
51 #include <cyg/infra/cyg_type.h> 
52 #include <cyg/io/io.h> 
53
54 #include <linux/rbtree.h>
55 #include <linux/list.h>
56
57 // --------------------------------------------------------------------
58
59 typedef int (*cyg_blib_bread_fn) (
60     void*,       // private data
61     void*,       // block buffer
62     cyg_uint32*, // number of blocks to read
63     cyg_uint32   // starting block number
64 );
65
66 typedef int (*cyg_blib_bwrite_fn) (
67     void*,       // private data
68     const void*, // block buffer
69     cyg_uint32*, // number of blocks to write 
70     cyg_uint32   // starting block number
71 );
72
73 typedef struct {
74     cyg_uint32  n_gets;     // number of block gets
75     cyg_uint32  n_reads;    // number of block reads
76     cyg_uint32  n_writes;   // number of block writes
77 } cyg_blib_stat_t;
78
79 typedef struct {
80     void                 *priv;            // private data
81     struct list_head      list_head;       // head of block list 
82     struct rb_root        rb_root;         // block red-black tree root
83     cyg_uint32            block_size;      // block size
84     cyg_uint32            block_size_log2; // block size log2
85     cyg_uint8            *mem_base;        // memory base
86     cyg_uint32            mem_size;        // memory size
87     struct list_head      free_list_head;  // list of free blocks
88     cyg_blib_bread_fn     bread_fn;        // block read function
89     cyg_blib_bwrite_fn    bwrite_fn;       // block write function
90 #ifdef CYGIMP_BLOCK_LIB_STATISTICS
91     cyg_blib_stat_t       stat;            // statistics
92 #endif
93 } cyg_blib_t;
94
95 // --------------------------------------------------------------------
96
97 // --------------------------------------------------------------------
98 // Creates a block lib instance
99 // 
100 //   priv_data  - private data to pass to bread_fn and bwrite_fn
101 //   mem_base   - block cache memory base
102 //   mem_size   - block cache memory size
103 //   block_size - block size
104 //   bread_fn   - function which reads blocks
105 //   bwrite_fn  - function which writes blocks
106 //   bl         - block lib instance space holder
107 //   
108 //   returns ENOERR if create succeded
109 //
110
111 int cyg_blib_create(void               *priv_data,
112                     void               *mem_base,
113                     cyg_uint32          mem_size,
114                     cyg_uint32          block_size,
115                     cyg_blib_bread_fn   bread_fn,
116                     cyg_blib_bwrite_fn  bwrite_fn,
117                     cyg_blib_t         *bl);
118
119 // --------------------------------------------------------------------
120 // Creates a block lib instance on top of IO system 
121 //   (cyg_io_bread and cyg_io_bwrite)
122 // 
123 //   handle     - cyg_io_handle_t
124 //   mem_base   - block cache memory base
125 //   mem_size   - block cache memory size
126 //   block_size - block size
127 //   bl         - block lib instance space holder
128 //   
129 //   returns ENOERR if create succeded
130 //
131
132 int cyg_blib_io_create(cyg_io_handle_t     handle,
133                        void               *mem_base,
134                        cyg_uint32          mem_size,
135                        cyg_uint32          block_size,
136                        cyg_blib_t         *bl);
137
138 // --------------------------------------------------------------------
139 // Deletes a block lib instance
140 //   
141 //   bl - block lib instance
142 //
143 //   The block cache is synced before
144 //
145 //   returns ENOERR if delete succeded
146 //
147
148 int cyg_blib_delete(cyg_blib_t *bl);
149
150 // --------------------------------------------------------------------
151 // Reads a number of blocks
152 //
153 //   bl  - block lib instance
154 //   buf - block buffer ptr 
155 //   len - number of blocks to read
156 //   pos - starting block number
157 //       
158 //   returns ENOERR if read succeded
159 //   
160         
161 int cyg_blib_bread(cyg_blib_t *bl,
162                    void       *buf,
163                    cyg_uint32 *len,
164                    cyg_uint32  pos);
165
166 // --------------------------------------------------------------------
167 // Writes a number of blocks
168 //
169 //   bl  - block lib instance
170 //   buf - block buffer ptr 
171 //   len - number of blocks to write 
172 //   pos - starting block number
173 //       
174 //   returns ENOERR if write succeded
175 //   
176  
177 int cyg_blib_bwrite(cyg_blib_t *bl,
178                     const void *buf,
179                     cyg_uint32 *len,
180                     cyg_uint32  pos);
181
182 // --------------------------------------------------------------------
183 // Reads data
184 //
185 //   bl   - block lib instance
186 //   buf  - data buffer ptr 
187 //   len  - number of bytes to read
188 //   bnum - starting block number 
189 //   pos  - starting position inside starting block
190 //       
191 //   returns ENOERR if read succeded
192 //   
193 //   The block number is automatically adjusted if
194 //   position is greater than block size
195 //
196  
197 int cyg_blib_read(cyg_blib_t *bl,
198                   void       *buf,
199                   cyg_uint32 *len,
200                   cyg_uint32  bnum,
201                   cyg_uint32  pos);
202
203 // --------------------------------------------------------------------
204 // Writes data
205 //
206 //   bl   - block lib instance
207 //   buf  - data buffer ptr 
208 //   len  - number of bytes to write 
209 //   bnum - starting block number 
210 //   pos  - starting position inside starting block
211 //       
212 //   returns ENOERR if write succeded
213 //
214 //   The block number is automatically adjusted if
215 //   position is greater than block size
216 //    
217  
218 int cyg_blib_write(cyg_blib_t *bl,
219                    const void *buf,
220                    cyg_uint32 *len,
221                    cyg_uint32  bnum,
222                    cyg_uint32  pos);
223
224 // --------------------------------------------------------------------
225 // Syncs block cache - ie write modified blocks
226 //
227 //   bl - block lib instance
228 //
229 //   returns ENOERR if sync succeded
230 //
231
232 int cyg_blib_sync(cyg_blib_t *bl);
233
234 // --------------------------------------------------------------------
235 // Syncs block - ie write if modified
236 //
237 //   bl  - block lib instance
238 //   num - block number to sync
239 //
240 //   returns ENOERR if sync succeded
241 //
242
243 int cyg_blib_sync_block(cyg_blib_t *bl, cyg_uint32 num);
244
245 // --------------------------------------------------------------------
246 // Flushes block cache 
247 //
248 //   bl  - block lib instance
249 //
250 //   returns ENOERR if flush succeded
251 //
252 //   The block cache is synced before
253 //
254
255 int cyg_blib_flush(cyg_blib_t *bl);
256
257 // --------------------------------------------------------------------
258 // Sets block size 
259 //
260 //   bl         - block lib instance
261 //   block_size - new block size
262 //
263 //   returns ENOERR if set succeded
264 //
265 //   The block cache is synced before
266 //
267
268 int cyg_blib_set_block_size(cyg_blib_t *bl, cyg_uint32 block_size);
269
270 // --------------------------------------------------------------------
271 // Gets block size 
272 //
273 //   bl  - block lib instance
274 //
275 //   returns the current block size
276
277 static inline cyg_uint32 cyg_blib_get_block_size(cyg_blib_t *bl)
278 {
279     return bl->block_size;
280 }
281
282 // --------------------------------------------------------------------
283 // Gets log2 of block size 
284 //
285 //   bl  - block lib instance
286 //
287 //   returns log2 of the current block size 
288
289 static inline cyg_uint32 cyg_blib_get_block_size_log2(cyg_blib_t *bl)
290 {
291     return bl->block_size_log2;
292 }
293
294 // --------------------------------------------------------------------
295 // Gets block cache statistics 
296 //
297 //   bl - block lib instance
298 //
299 //   returns ENOERR if get succeded
300 //
301
302 int cyg_blib_get_stat(cyg_blib_t *bl, cyg_blib_stat_t *stat);
303
304 #endif // CYGONCE_BLIB_H
305
306 // --------------------------------------------------------------------
307 // EOF blib.h