Add simulated block device

git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@208 42af7a65-404d-4744-a932-0658087f49c3
This commit is contained in:
patacongo 2007-05-09 00:06:00 +00:00
parent 21a0b97760
commit e4cdccae6c
5 changed files with 208 additions and 4 deletions

View File

@ -45,7 +45,7 @@ CSRCS = up_initialize.c up_idle.c up_interruptcontext.c \
up_releasestack.c up_unblocktask.c up_blocktask.c \
up_releasepending.c up_reprioritizertr.c \
up_exit.c up_schedulesigaction.c up_allocateheap.c \
up_devconsole.c
up_devconsole.c up_blockdevice.c
COBJS = $(CSRCS:.c=$(OBJEXT))
SRCS = $(ASRCS) $(CSRCS)

View File

@ -0,0 +1,202 @@
/****************************************************************************
* up_blockdevice.c
*
* Copyright (C) 2007 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name Gregory Nutt nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <nuttx/fs.h>
#include <errno.h>
#include "up_internal.h"
/****************************************************************************
* Private Definitions
****************************************************************************/
#define NSECTORS 128
#define SECTOR_SIZE 512
/****************************************************************************
* Private Types
****************************************************************************/
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
static int up_open(FAR struct file *filp);
static int up_close(FAR struct file *filp);
static ssize_t up_read(FAR struct file *filp, char *buffer,
size_t start_sector, size_t nsectors);
static ssize_t up_write(FAR struct file *filp, const char *buffer,
size_t start_sector, size_t nsectors);
static size_t up_geometry(FAR struct file *filp, struct geometry *geometry);
/****************************************************************************
* Private Data
****************************************************************************/
static const struct block_operations g_bops =
{
.open = up_open,
.close = up_close,
.read = up_read,
.write = up_write,
.geometry = up_geometry,
};
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: up_open
*
* Description: Open the block device
*
****************************************************************************/
static int up_open(FAR struct file *filp)
{
filp->f_priv = (void*)malloc(NSECTORS*SECTOR_SIZE);
return 0;
}
/****************************************************************************
* Name: up_close
*
* Description: close the block device
*
****************************************************************************/
static int up_close(FAR struct file *filp)
{
if (filp->f_priv)
{
free(filp->f_priv);
filp->f_priv = NULL;
}
return 0;
}
/****************************************************************************
* Name: up_read
*
* Description: Read the specified numer of sectors
*
****************************************************************************/
static ssize_t up_read(FAR struct file *filp, char *buffer,
size_t start_sector, size_t nsectors)
{
char *src = filp->f_priv;
if (src &&
start_sector < NSECTORS &&
start_sector + nsectors < NSECTORS)
{
memcpy(buffer, &src[start_sector*SECTOR_SIZE], nsectors*SECTOR_SIZE);
return OK;
}
else
{
return -EINVAL;
}
}
/****************************************************************************
* Name: up_write
*
* Description: Write the specified number of sectors
*
****************************************************************************/
static ssize_t up_write(FAR struct file *filp, const char *buffer,
size_t start_sector, size_t nsectors)
{
char *dest = filp->f_priv;
if (dest &&
start_sector < NSECTORS &&
start_sector + nsectors < NSECTORS)
{
memcpy(&dest[start_sector*SECTOR_SIZE], buffer, nsectors*SECTOR_SIZE);
return OK;
}
else
{
return -EINVAL;
}
}
/****************************************************************************
* Name: up_geometry
*
* Description: Return device geometry
*
****************************************************************************/
static size_t up_geometry(FAR struct file *filp, struct geometry *geometry)
{
if (geometry)
{
geometry->geo_available = (filp->f_priv != NULL);
geometry->geo_nsectors = NSECTORS;
geometry->geo_sectorsize = SECTOR_SIZE;
return OK;
}
return -EINVAL;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: up_registerblockdevice
*
* Description: Register the simulated block device
****************************************************************************/
void up_registerblockdevice(void)
{
(void)register_blockdriver("/dev/blkdev", &g_bops, 0, NULL);
}

View File

@ -125,7 +125,7 @@ static ssize_t devconsole_write(struct file *filp, const char *buffer, size_t le
void up_devconsole(void)
{
(void)register_inode("/dev/console", &devconsole_fops, 0666, NULL);
(void)register_driver("/dev/console", &devconsole_fops, 0666, NULL);
}
int up_putc(int ch)

View File

@ -83,6 +83,7 @@ void up_initialize(void)
{
/* Register devices */
devnull_register(); /* Standard /dev/null */
up_devconsole(); /* Our private /dev/console */
devnull_register(); /* Standard /dev/null */
up_devconsole(); /* Our private /dev/console */
up_registerblockdevice(); /* Our simulated block device /dev/blkdev */
}

View File

@ -86,6 +86,7 @@ extern void up_longjmp(int *jb, int val) __attribute__ ((noreturn));
/* up_devconsole **********************************************************/
extern void up_devconsole(void);
extern void up_registerblockdevice(void);
#endif /* __ASSEMBLY__ */
#endif /* __ARCH_UP_INTERNAL_H */