diff options
author | patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3> | 2007-05-09 00:06:00 +0000 |
---|---|---|
committer | patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3> | 2007-05-09 00:06:00 +0000 |
commit | 404a5f67ab5e862744e0e493ad4743e275600022 (patch) | |
tree | 20ae2ae0199fc9173342f9f1f8a9adc06bfbd0c4 /nuttx/arch | |
parent | 85858bfb91e060906476d4083d71f56e3512bf83 (diff) | |
download | px4-nuttx-404a5f67ab5e862744e0e493ad4743e275600022.tar.gz px4-nuttx-404a5f67ab5e862744e0e493ad4743e275600022.tar.bz2 px4-nuttx-404a5f67ab5e862744e0e493ad4743e275600022.zip |
Add simulated block device
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@208 42af7a65-404d-4744-a932-0658087f49c3
Diffstat (limited to 'nuttx/arch')
-rw-r--r-- | nuttx/arch/sim/src/Makefile | 2 | ||||
-rw-r--r-- | nuttx/arch/sim/src/up_blockdevice.c | 202 | ||||
-rw-r--r-- | nuttx/arch/sim/src/up_devconsole.c | 2 | ||||
-rw-r--r-- | nuttx/arch/sim/src/up_initialize.c | 5 | ||||
-rw-r--r-- | nuttx/arch/sim/src/up_internal.h | 1 |
5 files changed, 208 insertions, 4 deletions
diff --git a/nuttx/arch/sim/src/Makefile b/nuttx/arch/sim/src/Makefile index 5c7ed0d1f..2694c7a8b 100644 --- a/nuttx/arch/sim/src/Makefile +++ b/nuttx/arch/sim/src/Makefile @@ -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) diff --git a/nuttx/arch/sim/src/up_blockdevice.c b/nuttx/arch/sim/src/up_blockdevice.c new file mode 100644 index 000000000..1d4216e01 --- /dev/null +++ b/nuttx/arch/sim/src/up_blockdevice.c @@ -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); +} + + diff --git a/nuttx/arch/sim/src/up_devconsole.c b/nuttx/arch/sim/src/up_devconsole.c index b55df53df..601a13bf3 100644 --- a/nuttx/arch/sim/src/up_devconsole.c +++ b/nuttx/arch/sim/src/up_devconsole.c @@ -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) diff --git a/nuttx/arch/sim/src/up_initialize.c b/nuttx/arch/sim/src/up_initialize.c index 90b0b49e4..805c88b45 100644 --- a/nuttx/arch/sim/src/up_initialize.c +++ b/nuttx/arch/sim/src/up_initialize.c @@ -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 */ } diff --git a/nuttx/arch/sim/src/up_internal.h b/nuttx/arch/sim/src/up_internal.h index 29defe029..5de1d7687 100644 --- a/nuttx/arch/sim/src/up_internal.h +++ b/nuttx/arch/sim/src/up_internal.h @@ -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 */ |