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

4.104.114.84.95
Last change on this file since b2b143f4 was b2b143f4, checked in by Joel Sherrill <joel.sherrill@…>, on 03/05/04 at 17:58:51

2004-03-05 Joel Sherrill <joel@…>

  • libblock/src/bdbuf.c, libblock/src/ramdisk.c, libcsupport/src/newlibc.c, libcsupport/src/sync.c, libmisc/cpuuse/cpuuse.c, libmisc/monitor/mon-symbols.c, libmisc/shell/cmds.c, libmisc/shell/shell.c, libnetworking/kern/kern_sysctl.c, libnetworking/lib/ftpfs.c, libnetworking/lib/tftpDriver.c, libnetworking/libc/gethostbydns.c, libnetworking/libc/gethostbyht.c, libnetworking/libc/gethostnamadr.c, libnetworking/libc/getnetbyht.c, libnetworking/libc/getnetnamadr.c, libnetworking/libc/inet_addr.c, libnetworking/libc/linkaddr.c, libnetworking/libc/map_v4v6.c, libnetworking/libc/ns_print.c, libnetworking/libc/ns_ttl.c, libnetworking/libc/nsap_addr.c, libnetworking/libc/rcmd.c, libnetworking/libc/res_debug.c, libnetworking/libc/res_mkupdate.c, libnetworking/libc/res_query.c, libnetworking/libc/res_send.c, libnetworking/libc/res_update.c, libnetworking/net/radix.c, libnetworking/rtems/mkrootfs.c, librpc/src/rpc/clnt_perror.c, librpc/src/rpc/rtems_rpc.c, librpc/src/rpc/svc.c, sapi/include/confdefs.h, score/macros/rtems/score/chain.inl, score/src/objectidtoname.c:
  • Property mode set to 100644
File size: 6.1 KB
Line 
1/* ramdisk.c -- RAM disk block device implementation
2 *
3 * Copyright (C) 2001 OKTET Ltd., St.-Petersburg, Russia
4 * Author: Victor V. Vengerov <vvv@oktet.ru>
5 *
6 * @(#) $Id$
7 */
8
9#if HAVE_CONFIG_H
10#include "config.h"
11#endif
12
13#include <rtems.h>
14#include <rtems/libio.h>
15
16/* Since we compile with strict ANSI we need to undef it to get
17 * prototypes for extensions
18 */
19#undef __STRICT_ANSI__
20
21#include <errno.h>
22#include <stdlib.h>
23#include <stdio.h>
24#include <string.h>
25
26#include "rtems/blkdev.h"
27#include "rtems/diskdevs.h"
28#include "rtems/ramdisk.h"
29
30#define RAMDISK_DEVICE_BASE_NAME "/dev/ramdisk"
31
32/* Internal RAM disk descriptor */
33struct ramdisk {
34    int           block_size; /* RAM disk block size */
35    int           block_num;  /* Number of blocks on this RAM disk */
36    void         *area;       /* RAM disk memory area */
37    rtems_boolean initialized;/* RAM disk is initialized */
38    rtems_boolean malloced;   /* != 0, if memory allocated by malloc for this
39                                 RAM disk */
40};
41
42static struct ramdisk *ramdisk;
43static int nramdisks;
44
45/* ramdisk_read --
46 *     RAM disk READ request handler. This primitive copies data from RAM
47 *     disk to supplied buffer and invoke the callout function to inform
48 *     upper layer that reading is completed.
49 *
50 * PARAMETERS:
51 *     req - pointer to the READ block device request info
52 *
53 * RETURNS:
54 *     ioctl return value
55 */
56static int
57ramdisk_read(struct ramdisk *rd, blkdev_request *req)
58{
59    char *from;
60    uint32_t   i;
61    blkdev_sg_buffer *sg;
62    uint32_t   remains;
63   
64    from = (char *)rd->area + (req->start * rd->block_size);
65    remains = rd->block_size * req->count;
66    sg = req->bufs;
67    for (i = 0; (remains > 0) && (i < req->bufnum); i++, sg++)
68    {
69        int count = sg->length;
70        if (count > remains)
71            count = remains;
72        memcpy(sg->buffer, from, count);
73        remains -= count;
74        from += count;
75    }
76    req->req_done(req->done_arg, RTEMS_SUCCESSFUL, 0);
77    return 0;
78}
79
80/* ramdisk_write --
81 *     RAM disk WRITE request handler. This primitive copies data from
82 *     supplied buffer to RAM disk and invoke the callout function to inform
83 *     upper layer that writing is completed.
84 *
85 * PARAMETERS:
86 *     req - pointer to the WRITE block device request info
87 *
88 * RETURNS:
89 *     ioctl return value
90 */
91static int
92ramdisk_write(struct ramdisk *rd, blkdev_request *req)
93{
94    char *to;
95    uint32_t   i;
96    blkdev_sg_buffer *sg;
97    uint32_t   remains;
98   
99    to = (char *)rd->area + (req->start * rd->block_size);
100    remains = rd->block_size * req->count;
101    sg = req->bufs;
102    for (i = 0; (remains > 0) && (i < req->bufnum); i++, sg++)
103    {
104        int count = sg->length;
105        if (count > remains)
106            count = remains;
107        memcpy(to, sg->buffer, count);
108        remains -= count;
109        to += count;
110    }
111    req->req_done(req->done_arg, RTEMS_SUCCESSFUL, 0);
112    return 0;
113}
114
115/* ramdisk_ioctl --
116 *     IOCTL handler for RAM disk device.
117 *
118 * PARAMETERS:
119 *      dev  - device number (major, minor number)
120 *      req  - IOCTL request code
121 *      argp - IOCTL argument
122 *
123 * RETURNS:
124 *     IOCTL return value
125 */
126static int
127ramdisk_ioctl(dev_t dev, int req, void *argp)
128{
129    switch (req)
130    {
131        case BLKIO_REQUEST:
132        {
133            rtems_device_minor_number minor;
134            blkdev_request *r = argp;
135            struct ramdisk *rd;
136
137            minor = rtems_filesystem_dev_minor_t(dev);
138            if ((minor >= nramdisks) || !ramdisk[minor].initialized)
139            {
140                errno = ENODEV;
141                return -1;
142            }
143           
144            rd = ramdisk + minor;
145           
146            switch (r->req)
147            {
148                case BLKDEV_REQ_READ:
149                    return ramdisk_read(rd, r);
150
151                case BLKDEV_REQ_WRITE:
152                    return ramdisk_write(rd, r);
153                   
154                default:
155                    errno = EBADRQC;
156                    return -1;
157            }
158            break;
159        }
160       
161        default:
162            errno = EBADRQC;
163            return -1;
164    }
165}
166
167/* ramdisk_initialize --
168 *     RAM disk device driver initialization. Run through RAM disk
169 *     configuration information and configure appropriate RAM disks.
170 *
171 * PARAMETERS:
172 *     major - RAM disk major device number
173 *     minor - minor device number, not applicable
174 *     arg   - initialization argument, not applicable
175 *
176 * RETURNS:
177 *     none
178 */
179rtems_device_driver
180ramdisk_initialize(
181    rtems_device_major_number major,
182    rtems_device_minor_number minor,
183    void *arg)
184{
185    rtems_device_minor_number i;
186    rtems_ramdisk_config *c = rtems_ramdisk_configuration;
187    struct ramdisk *r;
188    rtems_status_code rc;
189
190    rc = rtems_disk_io_initialize();
191    if (rc != RTEMS_SUCCESSFUL)
192        return rc;
193       
194    r = ramdisk = calloc(rtems_ramdisk_configuration_size,
195                         sizeof(struct ramdisk));
196   
197    for (i = 0; i < rtems_ramdisk_configuration_size; i++, c++, r++)
198    {
199        dev_t dev = rtems_filesystem_make_dev_t(major, i);
200        char name[sizeof(RAMDISK_DEVICE_BASE_NAME "0123456789")];
201        snprintf(name, sizeof(name), RAMDISK_DEVICE_BASE_NAME "%d", i);
202        r->block_size = c->block_size;
203        r->block_num = c->block_num;
204        if (c->location == NULL)
205        {
206            r->malloced = TRUE;
207            r->area = malloc(r->block_size * r->block_num);
208            if (r->area == NULL) /* No enough memory for this disk */
209            {
210                r->initialized = FALSE;
211                continue;
212            }
213            else
214            {
215                r->initialized = TRUE;
216            }
217        }
218        else
219        {
220            r->malloced = FALSE;
221            r->initialized = TRUE;
222            r->area = c->location;
223        }
224        rc = rtems_disk_create_phys(dev, c->block_size, c->block_num,
225                                    ramdisk_ioctl, name);
226        if (rc != RTEMS_SUCCESSFUL)
227        {
228            if (r->malloced)
229            {
230                free(r->area);
231            }
232            r->initialized = FALSE;
233        }
234    }
235    nramdisks = rtems_ramdisk_configuration_size;
236    return RTEMS_SUCCESSFUL;
237}
Note: See TracBrowser for help on using the repository browser.