source: rtems/cpukit/libblock/src/ramdisk.c @ b96e09c

4.104.11
Last change on this file since b96e09c was b96e09c, checked in by Thomas Doerfler <Thomas.Doerfler@…>, on Oct 13, 2009 at 7:58:33 AM
  • libblock/include/rtems/diskdevs.h: Added driver data pointer to IO control function. The IO control handler takes now the disk device as first parameter instead of the physical device number.
  • cpukit/libblock/include/rtems/blkdev.h, libblock/src/bdbuf.c, libblock/src/blkdev.c, libblock/src/diskdevs.c, libblock/src/nvdisk.c, libblock/src/flashdisk.c, libblock/src/ramdisk.c: Update for block device API change.
  • Property mode set to 100644
File size: 7.1 KB
Line 
1/**
2 * @file
3 *
4 * @ingroup rtems_ramdisk
5 *
6 * RAM disk block device.
7 */
8
9/*
10 * Copyright (C) 2001 OKTET Ltd., St.-Petersburg, Russia
11 * Author: Victor V. Vengerov <vvv@oktet.ru>
12 *
13 * @(#) $Id$
14 */
15
16#if HAVE_CONFIG_H
17#include "config.h"
18#endif
19
20#include <rtems.h>
21#include <rtems/libio.h>
22#include <errno.h>
23#include <stdlib.h>
24#include <stdio.h>
25#include <string.h>
26#include <inttypes.h>
27
28#include <rtems/blkdev.h>
29#include <rtems/diskdevs.h>
30#include <rtems/ramdisk.h>
31
32/**
33 * Control tracing. It can be compiled out of the code for small
34 * footprint targets. Leave in by default.
35 */
36#if !defined (RTEMS_RAMDISK_TRACE)
37#define RTEMS_RAMDISK_TRACE 0
38#endif
39
40#define RAMDISK_DEVICE_BASE_NAME "/dev/rd"
41
42/* Internal RAM disk descriptor */
43struct ramdisk {
44  uint32_t block_size; /* RAM disk block size, the media size */
45  rtems_blkdev_bnum block_num; /* Number of blocks on this RAM disk */
46  void *area; /* RAM disk memory area */
47  bool initialized; /* RAM disk is initialized */
48  bool malloced; /* != 0, if memory allocated by malloc for this RAM disk */
49#if RTEMS_RAMDISK_TRACE
50  int info_level; /* Trace level */
51#endif
52};
53
54static struct ramdisk *ramdisk;
55static uint32_t nramdisks;
56
57#if RTEMS_RAMDISK_TRACE
58/**
59 * Print a message to the ramdisk output and flush it.
60 *
61 * @param rd The ramdisk control structure.
62 * @param format The format string. See printf for details.
63 * @param ... The arguments for the format text.
64 * @return int The number of bytes written to the output.
65 */
66static int
67rtems_ramdisk_printf (struct ramdisk *rd, const char *format, ...)
68{
69  int ret = 0;
70  if (rd->info_level >= 1)
71  {
72    va_list args;
73    va_start (args, format);
74    fprintf (stdout, "ramdisk:");
75    ret =  vfprintf (stdout, format, args);
76    fprintf (stdout, "\n");
77    fflush (stdout);
78  }
79  return ret;
80}
81#endif
82
83/* ramdisk_read --
84 *     RAM disk READ request handler. This primitive copies data from RAM
85 *     disk to supplied buffer and invoke the callout function to inform
86 *     upper layer that reading is completed.
87 *
88 * PARAMETERS:
89 *     req - pointer to the READ block device request info
90 *
91 * RETURNS:
92 *     ioctl return value
93 */
94static int
95ramdisk_read(struct ramdisk *rd, rtems_blkdev_request *req)
96{
97    uint8_t *from = rd->area;
98    uint32_t   i;
99    rtems_blkdev_sg_buffer *sg;
100
101#if RTEMS_RAMDISK_TRACE
102    rtems_ramdisk_printf (rd, "ramdisk read: start=%d, blocks=%d",
103                          req->bufs[0].block, req->bufnum);
104#endif
105
106    for (i = 0, sg = req->bufs; i < req->bufnum; i++, sg++)
107    {
108#if RTEMS_RAMDISK_TRACE
109        rtems_ramdisk_printf (rd, "ramdisk read: buf=%d block=%d length=%d off=%d addr=%p",
110                              i, sg->block, sg->length, sg->block * rd->block_size,
111                              from + (sg->block * rd->block_size));
112#endif
113        memcpy(sg->buffer, from + (sg->block * rd->block_size), sg->length);
114    }
115    req->req_done(req->done_arg, RTEMS_SUCCESSFUL, 0);
116    return 0;
117}
118
119/* ramdisk_write --
120 *     RAM disk WRITE request handler. This primitive copies data from
121 *     supplied buffer to RAM disk and invoke the callout function to inform
122 *     upper layer that writing is completed.
123 *
124 * PARAMETERS:
125 *     req - pointer to the WRITE block device request info
126 *
127 * RETURNS:
128 *     ioctl return value
129 */
130static int
131ramdisk_write(struct ramdisk *rd, rtems_blkdev_request *req)
132{
133    uint8_t *to = rd->area;
134    uint32_t   i;
135    rtems_blkdev_sg_buffer *sg;
136
137#if RTEMS_RAMDISK_TRACE
138    rtems_ramdisk_printf (rd, "ramdisk write: start=%d, blocks=%d",
139                          req->bufs[0].block, req->bufnum);
140#endif
141    for (i = 0, sg = req->bufs; i < req->bufnum; i++, sg++)
142    {
143#if RTEMS_RAMDISK_TRACE
144        rtems_ramdisk_printf (rd, "ramdisk write: buf=%d block=%d length=%d off=%d addr=%p",
145                              i, sg->block, sg->length, sg->block * rd->block_size,
146                              to + (sg->block * rd->block_size));
147#endif
148        memcpy(to + (sg->block * rd->block_size), sg->buffer, sg->length);
149    }
150    req->req_done(req->done_arg, RTEMS_SUCCESSFUL, 0);
151    return 0;
152}
153
154/* ramdisk_ioctl --
155 *     IOCTL handler for RAM disk device.
156 *
157 * PARAMETERS:
158 *      dev  - device number (major, minor number)
159 *      req  - IOCTL request code
160 *      argp - IOCTL argument
161 *
162 * RETURNS:
163 *     IOCTL return value
164 */
165static int
166ramdisk_ioctl(rtems_disk_device *dd, uint32_t req, void *argp)
167{
168    switch (req)
169    {
170        case RTEMS_BLKIO_REQUEST:
171        {
172            rtems_blkdev_request *r = argp;
173            struct ramdisk *rd = rtems_disk_driver_data(dd);
174
175            switch (r->req)
176            {
177                case RTEMS_BLKDEV_REQ_READ:
178                    return ramdisk_read(rd, r);
179
180                case RTEMS_BLKDEV_REQ_WRITE:
181                    return ramdisk_write(rd, r);
182
183                default:
184                    errno = EINVAL;
185                    return -1;
186            }
187            break;
188        }
189 
190        default:
191            return rtems_blkdev_ioctl (dd, req, argp);
192            break;
193    }
194
195    errno = EINVAL;
196    return -1;
197}
198
199/* ramdisk_initialize --
200 *     RAM disk device driver initialization. Run through RAM disk
201 *     configuration information and configure appropriate RAM disks.
202 *
203 * PARAMETERS:
204 *     major - RAM disk major device number
205 *     minor - minor device number, not applicable
206 *     arg   - initialization argument, not applicable
207 *
208 * RETURNS:
209 *     none
210 */
211rtems_device_driver
212ramdisk_initialize(
213    rtems_device_major_number major,
214    rtems_device_minor_number minor __attribute__((unused)),
215    void *arg __attribute__((unused)))
216{
217    rtems_device_minor_number i;
218    rtems_ramdisk_config *c = rtems_ramdisk_configuration;
219    struct ramdisk *r;
220    rtems_status_code rc;
221
222    rc = rtems_disk_io_initialize();
223    if (rc != RTEMS_SUCCESSFUL)
224        return rc;
225
226    r = ramdisk = calloc(rtems_ramdisk_configuration_size,
227                         sizeof(struct ramdisk));
228#if RTEMS_RAMDISK_TRACE
229    r->info_level = 1;
230#endif   
231    for (i = 0; i < rtems_ramdisk_configuration_size; i++, c++, r++)
232    {
233        dev_t dev = rtems_filesystem_make_dev_t(major, i);
234        char name [] = RAMDISK_DEVICE_BASE_NAME "a";
235        name [sizeof(RAMDISK_DEVICE_BASE_NAME)] += i;
236        r->block_size = c->block_size;
237        r->block_num = c->block_num;
238        if (c->location == NULL)
239        {
240            r->malloced = true;
241            r->area = malloc(r->block_size * r->block_num);
242            if (r->area == NULL) /* No enough memory for this disk */
243            {
244                r->initialized = false;
245                continue;
246            }
247            else
248            {
249                r->initialized = true;
250            }
251        }
252        else
253        {
254            r->malloced = false;
255            r->initialized = true;
256            r->area = c->location;
257        }
258        rc = rtems_disk_create_phys(dev, c->block_size, c->block_num,
259                                    ramdisk_ioctl, r, name);
260        if (rc != RTEMS_SUCCESSFUL)
261        {
262            if (r->malloced)
263            {
264                free(r->area);
265            }
266            r->initialized = false;
267        }
268    }
269    nramdisks = rtems_ramdisk_configuration_size;
270    return RTEMS_SUCCESSFUL;
271}
Note: See TracBrowser for help on using the repository browser.