summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpatacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3>2008-11-15 00:14:05 +0000
committerpatacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3>2008-11-15 00:14:05 +0000
commitcde81697d12417a8d23655997fdf209a3d6bc417 (patch)
tree886cd728935bde3be18e8d5d99e0c3d6052706b5
parent33750aac935ce4f0f9c2e44ab1e43cd196c95ce9 (diff)
downloadnuttx-cde81697d12417a8d23655997fdf209a3d6bc417.tar.gz
nuttx-cde81697d12417a8d23655997fdf209a3d6bc417.tar.bz2
nuttx-cde81697d12417a8d23655997fdf209a3d6bc417.zip
Block-to-character driver
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@1235 42af7a65-404d-4744-a932-0658087f49c3
-rw-r--r--nuttx/drivers/bch/Make.defs40
-rw-r--r--nuttx/drivers/bch/bch_internal.h101
-rw-r--r--nuttx/drivers/bch/bchdev_driver.c211
-rw-r--r--nuttx/drivers/bch/bchdev_register.c108
-rw-r--r--nuttx/drivers/bch/bchdev_unregister.c155
-rw-r--r--nuttx/drivers/bch/bchlib_cache.c131
-rw-r--r--nuttx/drivers/bch/bchlib_read.c336
-rw-r--r--nuttx/drivers/bch/bchlib_refs.c123
-rw-r--r--nuttx/drivers/bch/bchlib_sem.c90
-rw-r--r--nuttx/drivers/bch/bchlib_setup.c160
-rw-r--r--nuttx/drivers/bch/bchlib_teardown.c118
-rw-r--r--nuttx/drivers/bch/bchlib_write.c207
12 files changed, 1780 insertions, 0 deletions
diff --git a/nuttx/drivers/bch/Make.defs b/nuttx/drivers/bch/Make.defs
new file mode 100644
index 000000000..d22d5a2dc
--- /dev/null
+++ b/nuttx/drivers/bch/Make.defs
@@ -0,0 +1,40 @@
+############################################################################
+# drivers/bch/Make.defs
+#
+# Copyright (C) 2008 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 NuttX 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.
+#
+############################################################################
+
+BCH_ASRCS =
+BCH_CSRCS = bchlib_setup.c bchlib_teardown.c bchlib_read.c bchlib_write.c \
+ bchlib_cache.c bchlib_refs.c bchlib_sem.c bchdev_register.c \
+ bchdev_unregister.c bchdev_driver.c
+
diff --git a/nuttx/drivers/bch/bch_internal.h b/nuttx/drivers/bch/bch_internal.h
new file mode 100644
index 000000000..5e1e6d0ab
--- /dev/null
+++ b/nuttx/drivers/bch/bch_internal.h
@@ -0,0 +1,101 @@
+/****************************************************************************
+ * drivers/bch/bch_internal.h
+ *
+ * Copyright (C) 2008 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.
+ *
+ ****************************************************************************/
+
+#ifndef __FS_BCH_INTERNAL_H
+#define __FS_BCH_INTERNAL_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <semaphore.h>
+#include <nuttx/fs.h>
+
+/****************************************************************************
+ * Definitions
+ ****************************************************************************/
+
+#define bchlib_semgive(d) sem_post(&(d)->sem) /* To match bchlib_semtake */
+#define MAX_OPENCNT (255) /* Limit of ubyte */
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+struct bchlib_s
+{
+ struct inode *inode; /* I-node of the block driver */
+ sem_t sem; /* For atomic accesses to this structure */
+ size_t nsectors; /* Number of sectors supported by the device */
+ size_t sector; /* The current sector in the buffer */
+ uint16 sectsize; /* The size of one sector on the device */
+ ubyte refs; /* Number of references */
+ boolean dirty; /* Data has been written to the buffer */
+ boolean readonly; /* TRUE: Only read operations are supported */
+ FAR ubyte *buffer; /* One sector buffer */
+};
+
+/****************************************************************************
+ * Global Variables
+ ****************************************************************************/
+
+#undef EXTERN
+#if defined(__cplusplus)
+#define EXTERN extern "C"
+extern "C" {
+#else
+#define EXTERN extern
+#endif
+
+EXTERN struct file_operations bch_fops;
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+EXTERN void bchlib_semtake(FAR struct bchlib_s *bch);
+EXTERN int bchlib_incref(FAR struct bchlib_s *bch);
+EXTERN int bchlib_decref(FAR struct bchlib_s *bch);
+EXTERN int bchlib_flushsector(FAR struct bchlib_s *bch);
+EXTERN int bchlib_readsector(FAR struct bchlib_s *bch, size_t sector);
+
+#undef EXTERN
+#if defined(__cplusplus)
+}
+#endif
+
+#endif /* __FS_BCH_INTERNAL_H */
diff --git a/nuttx/drivers/bch/bchdev_driver.c b/nuttx/drivers/bch/bchdev_driver.c
new file mode 100644
index 000000000..33e26032f
--- /dev/null
+++ b/nuttx/drivers/bch/bchdev_driver.c
@@ -0,0 +1,211 @@
+/****************************************************************************
+ * drivers/bch/bchdev_driver.c
+ *
+ * Copyright (C) 2008 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 NuttX 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Compilation Switches
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/ioctl.h>
+
+#include <unistd.h>
+#include <string.h>
+#include <fcntl.h>
+#include <sched.h>
+#include <errno.h>
+#include <assert.h>
+#include <debug.h>
+
+#include <nuttx/fs.h>
+#include <nuttx/ioctl.h>
+
+#include "bch_internal.h"
+
+/****************************************************************************
+ * Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int bch_open(FAR struct file *filp);
+static int bch_close(FAR struct file *filp);
+static ssize_t bch_read(FAR struct file *, FAR char *, size_t);
+static ssize_t bch_write(FAR struct file *, FAR const char *, size_t);
+static int bch_ioctl(FAR struct file *filp, int cmd, unsigned long arg);
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+struct file_operations bch_fops =
+{
+ bch_open, /* open */
+ bch_close, /* close */
+ bch_read, /* read */
+ bch_write, /* write */
+ 0, /* seek */
+ bch_ioctl /* ioctl */
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: bch_open
+ *
+ * Description: Open the block device
+ *
+ ****************************************************************************/
+
+static int bch_open(FAR struct file *filp)
+{
+ FAR struct inode *inode = filp->f_inode;
+
+ DEBUGASSERT(inode && inode->i_private);
+ return bchlib_incref((FAR struct bchlib_s *)inode->i_private);
+}
+
+/****************************************************************************
+ * Name: bch_close
+ *
+ * Description: close the block device
+ *
+ ****************************************************************************/
+
+static int bch_close(FAR struct file *filp)
+{
+ FAR struct inode *inode = filp->f_inode;
+
+ DEBUGASSERT(inode && inode->i_private);
+ return bchlib_decref((FAR struct bchlib_s *)inode->i_private);
+}
+
+/****************************************************************************
+ * Name:bch_read
+ ****************************************************************************/
+
+static ssize_t bch_read(FAR struct file *filp, FAR char *buffer, size_t len)
+{
+ FAR struct inode *inode = filp->f_inode;
+ FAR struct bchlib_s *bch;
+ int ret;
+
+ DEBUGASSERT(inode && inode->i_private);
+ bch = (FAR struct bchlib_s *)inode->i_private;
+
+ bchlib_semtake(bch);
+ ret = bchlib_read(bch, buffer, filp->f_pos, len);
+ if (ret > 0)
+ {
+ filp->f_pos += len;
+ }
+ bchlib_semgive(bch);
+ return ret;
+}
+
+/****************************************************************************
+ * Name:bch_write
+ ****************************************************************************/
+
+static ssize_t bch_write(FAR struct file *filp, FAR const char *buffer, size_t len)
+{
+ FAR struct inode *inode = filp->f_inode;
+ FAR struct bchlib_s *bch;
+ int ret = -EACCES;
+
+ DEBUGASSERT(inode && inode->i_private);
+ bch = (FAR struct bchlib_s *)inode->i_private;
+
+ if (!bch->readonly)
+ {
+ bchlib_semtake(bch);
+ ret = bchlib_write(bch, buffer, filp->f_pos, len);
+ if (ret > 0)
+ {
+ filp->f_pos += len;
+ }
+ bchlib_semgive(bch);
+ }
+
+ return ret;
+}
+
+/****************************************************************************
+ * Name: bch_ioctl
+ *
+ * Description: Return device geometry
+ *
+ ****************************************************************************/
+
+static int bch_ioctl(FAR struct file *filp, int cmd, unsigned long arg)
+{
+ FAR struct inode *inode = filp->f_inode;
+ FAR struct bchlib_s *bch;
+ int ret = -ENOTTY;
+
+ DEBUGASSERT(inode && inode->i_private);
+ bch = (FAR struct bchlib_s *)inode->i_private;
+
+ if (cmd == DIOC_GETPRIV)
+ {
+ FAR struct bchlib_s **bchr = (FAR struct bchlib_s **)arg;
+
+ if (!bchr)
+ {
+ ret = -EINVAL;
+ }
+ else
+ {
+ ret = bchlib_incref(bch);
+ *bchr = bch;
+ }
+ }
+
+ return ret;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
diff --git a/nuttx/drivers/bch/bchdev_register.c b/nuttx/drivers/bch/bchdev_register.c
new file mode 100644
index 000000000..f4d55dfa8
--- /dev/null
+++ b/nuttx/drivers/bch/bchdev_register.c
@@ -0,0 +1,108 @@
+/****************************************************************************
+ * drivers/bch/bchdev_register.c
+ *
+ * Copyright (C) 2008 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 NuttX 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Compilation Switches
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <sys/types.h>
+
+#include <errno.h>
+#include <assert.h>
+#include <debug.h>
+
+#include "bch_internal.h"
+
+/****************************************************************************
+ * Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: bchdev_register
+ *
+ * Description:
+ * Setup so that it exports the block driver referenced by
+ * 'blkdev' as a character device 'chardev'
+ *
+ ****************************************************************************/
+
+int bchdev_register(const char *blkdev, const char *chardev, boolean readonly)
+{
+ FAR void *handle;
+ int ret;
+
+ /* Setup the BCH lib functions */
+
+ ret = bchlib_setup(blkdev, readonly, &handle);
+ if (ret < 0)
+ {
+ fdbg("bchlib_setup failed: %d\n", -ret);
+ return ret;
+ }
+
+ /* Then setup the character device */
+
+ ret = register_driver(chardev, &bch_fops, 0666, handle);
+ if (ret < 0)
+ {
+ fdbg("register_driver failed: %d\n", -ret);
+ bchlib_teardown(handle);
+ handle = NULL;
+ }
+
+ return ret;
+}
diff --git a/nuttx/drivers/bch/bchdev_unregister.c b/nuttx/drivers/bch/bchdev_unregister.c
new file mode 100644
index 000000000..b71dfd385
--- /dev/null
+++ b/nuttx/drivers/bch/bchdev_unregister.c
@@ -0,0 +1,155 @@
+/****************************************************************************
+ * drivers/bch/bchdev_unregister.c
+ *
+ * Copyright (C) 2008 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 NuttX 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Compilation Switches
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/ioctl.h>
+
+#include <unistd.h>
+#include <fcntl.h>
+#include <sched.h>
+#include <errno.h>
+#include <assert.h>
+#include <debug.h>
+
+#include <nuttx/fs.h>
+#include <nuttx/ioctl.h>
+
+#include "bch_internal.h"
+
+/****************************************************************************
+ * Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: bchdev_unregister
+ *
+ * Description:
+ * Undo the setup performed by losetup
+ *
+ ****************************************************************************/
+
+int bchdev_unregister(const char *chardev)
+{
+ FAR struct bchlib_s *bch;
+ int fd;
+ int ret;
+
+ /* Sanity check */
+
+#ifdef CONFIG_DEBUG
+ if (!chardev)
+ {
+ return -EINVAL;
+ }
+#endif
+
+ /* Open the character driver associated with chardev */
+
+ fd = open(chardev, O_RDONLY);
+ if (ret < 0)
+ {
+ dbg("Failed to open %s: %d\n", chardev, -ret);
+ return ret;
+ }
+
+ /* Get a reference to the internal data structure */
+
+ ret = ioctl(fd, DIOC_GETPRIV, (unsigned long)&bch);
+ (void)close(fd);
+
+ if (ret < 0)
+ {
+ dbg("ioctl failed: %d\n", errno);
+ return -errno;
+ }
+
+ /* Lock out context switches */
+
+ sched_lock();
+
+ /* Check if the internal structure is non-busy (we hold one reference) */
+
+ if (bch->refs > 1)
+ {
+ ret = -EBUSY;
+ goto errout_with_lock;
+ }
+
+ /* Unregister the driver (this cannot suspend) */
+
+ ret = unregister_driver(chardev);
+ if (ret < 0)
+ {
+ goto errout_with_lock;
+ }
+ sched_unlock();
+
+ /* Release the internal structure */
+
+ bch->refs = 0;
+ return bchlib_teardown(bch);
+
+errout_with_lock:
+ sched_unlock();
+ bchlib_decref(bch);
+ return ret;
+}
diff --git a/nuttx/drivers/bch/bchlib_cache.c b/nuttx/drivers/bch/bchlib_cache.c
new file mode 100644
index 000000000..48b3c80f1
--- /dev/null
+++ b/nuttx/drivers/bch/bchlib_cache.c
@@ -0,0 +1,131 @@
+/****************************************************************************
+ * drivers/bch/bchlib_cache.c
+ *
+ * Copyright (C) 2008 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 NuttX 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Compilation Switches
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <sys/types.h>
+
+#include <errno.h>
+#include <assert.h>
+#include <debug.h>
+
+#include <nuttx/fs.h>
+
+#include "bch_internal.h"
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: bchlib_flushsector
+ *
+ * Description:
+ * Flush the current contents of the sector buffer (if dirty)
+ *
+ ****************************************************************************/
+
+int bchlib_flushsector(FAR struct bchlib_s *bch)
+{
+ FAR struct inode *inode;
+ ssize_t ret = OK;
+
+ if (bch->dirty)
+ {
+ inode = bch->inode;
+ ret = inode->u.i_bops->write(inode, bch->buffer, bch->sector, 1);
+ if (ret < 0)
+ {
+ fdbg("Write failed: %d\n");
+ }
+ bch->dirty = FALSE;
+ }
+ return (int)ret;
+}
+
+/****************************************************************************
+ * Name: bchlib_readsector
+ *
+ * Description:
+ * Flush the current contents of the sector buffer (if dirty)
+ *
+ ****************************************************************************/
+
+int bchlib_readsector(FAR struct bchlib_s *bch, size_t sector)
+{
+ FAR struct inode *inode;
+ ssize_t ret = OK;
+
+ if (bch->sector != sector)
+ {
+ inode = bch->inode;
+
+ (void)bchlib_flushsector(bch);
+ bch->sector = (size_t)-1;
+
+ ret = inode->u.i_bops->read(inode, bch->buffer, sector, 1);
+ if (ret < 0)
+ {
+ fdbg("Read failed: %d\n");
+ }
+ bch->sector = sector;
+ }
+ return (int)ret;
+}
+
diff --git a/nuttx/drivers/bch/bchlib_read.c b/nuttx/drivers/bch/bchlib_read.c
new file mode 100644
index 000000000..7af72ce8a
--- /dev/null
+++ b/nuttx/drivers/bch/bchlib_read.c
@@ -0,0 +1,336 @@
+/****************************************************************************
+ * drivers/bch/bchlib_read.c
+ *
+ * Copyright (C) 2008 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 NuttX 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Compilation Switches
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <sys/types.h>
+
+#include <string.h>
+#include <errno.h>
+#include <assert.h>
+#include <debug.h>
+
+#include <nuttx/fs.h>
+
+#include "bch_internal.h"
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: bchlib_read
+ *
+ * Description:
+ * Read from the block device set-up by bchlib_setup as if it were a character
+ * device.
+ *
+ ****************************************************************************/
+
+ssize_t bchlib_read(FAR void *handle, FAR char *buffer, size_t offset, size_t len)
+{
+ FAR struct bchlib_s *bch;
+ size_t nsectors;
+ size_t sector;
+ uint16 sectoffset;
+ size_t nbytes;
+ size_t bytesread;
+ int ret;
+
+ /* Get rid of this special case right away */
+
+ if (len < 1)
+ {
+ return 0;
+ }
+
+ /* Convert the file position into a sector number an offset. */
+
+ sector = offset / bch->sectsize;
+ sectoffset = offset - sector * bch->sectsize;
+
+ if (sector >= bch->nsectors)
+ {
+ /* Return end-of-file */
+
+ return 0;
+ }
+
+ /* Read the initial partial sector */
+
+ bytesread = 0;
+ if (sectoffset > 0)
+ {
+ /* Read the sector into the sector buffer */
+
+ bchlib_readsector(bch, sector);
+
+ /* Copy the tail end of the sector to the user buffer */
+
+ if (sectoffset + len > bch->sectsize)
+ {
+ nbytes = bch->sectsize - sectoffset;
+ }
+ else
+ {
+ nbytes = len;
+ }
+
+ memcpy(buffer, &bch->buffer[sectoffset], nbytes);
+
+ /* Adjust pointers and counts */
+
+ sectoffset = 0;
+ sector++;
+
+ if (sector >= bch->nsectors)
+ {
+ return nbytes;
+ }
+
+ bytesread = nbytes;
+ buffer += nbytes;
+ len -= nbytes;
+ }
+
+ /* Then read all of the full sectors following the partial sector */
+
+ if (len >= bch->sectsize )
+ {
+ nsectors = len / bch->sectsize;
+ if (sector + nsectors > bch->nsectors)
+ {
+ nsectors = bch->nsectors - sector;
+ }
+
+ ret = bch->inode->u.i_bops->read(bch->inode, bch->buffer, sector, nsectors);
+ if (ret < 0)
+ {
+ fdbg("Read failed: %d\n");
+ return ret;
+ }
+
+ /* Adjust pointers and counts */
+
+ sectoffset = 0;
+ sector += nsectors;
+
+ nbytes = nsectors * bch->sectsize;
+ bytesread += nbytes;
+
+ if (sector >= bch->nsectors)
+ {
+ return bytesread;
+ }
+
+ buffer += nbytes;
+ len -= nbytes;
+ }
+
+ /* Then read any partial final sector */
+
+ if (len > 0)
+ {
+ /* Read the sector into the sector buffer */
+
+ bchlib_readsector(bch, sector);
+
+ /* Copy the head end of the sector to the user buffer */
+
+ memcpy(buffer, bch->buffer, len);
+
+ /* Adjust counts */
+
+ bytesread += len;
+ }
+
+ return bytesread;
+}
+
+/****************************************************************************
+ * Name: bchlib_write
+ *
+ * Description:
+ * Write to the block device set-up by bchlib_setup as if it were a character
+ * device.
+ *
+ ****************************************************************************/
+
+ssize_t bchlib_write(FAR void *handle, FAR const char *buffer, size_t offset, size_t len)
+{
+ FAR struct bchlib_s *bch;
+ size_t nsectors;
+ size_t sector;
+ uint16 sectoffset;
+ size_t nbytes;
+ size_t byteswritten;
+ int ret;
+
+ /* Get rid of this special case right away */
+
+ if (len < 1)
+ {
+ return 0;
+ }
+
+ /* Convert the file position into a sector number an offset. */
+
+ sector = offset / bch->sectsize;
+ sectoffset = offset - sector * bch->sectsize;
+
+ if (sector >= bch->nsectors)
+ {
+ return -EFBIG;
+ }
+
+ /* Write the initial partial sector */
+
+ byteswritten = 0;
+ if (sectoffset > 0)
+ {
+ /* Read the full sector into the sector buffer */
+
+ bchlib_readsector(bch, sector);
+
+ /* Copy the tail end of the sector from the user buffer */
+
+ if (sectoffset + len > bch->sectsize)
+ {
+ nbytes = bch->sectsize - sectoffset;
+ }
+ else
+ {
+ nbytes = len;
+ }
+
+ memcpy(&bch->buffer[sectoffset], buffer, nbytes);
+ bch->dirty = TRUE;
+
+ /* Adjust pointers and counts */
+
+ sectoffset = 0;
+ sector++;
+
+ if (sector >= bch->nsectors)
+ {
+ return nbytes;
+ }
+
+ byteswritten = nbytes;
+ buffer += nbytes;
+ len -= nbytes;
+ }
+
+ /* Then write all of the full sectors following the partial sector */
+
+ if (len >= bch->sectsize )
+ {
+ nsectors = len / bch->sectsize;
+ if (sector + nsectors > bch->nsectors)
+ {
+ nsectors = bch->nsectors - sector;
+ }
+
+ /* Write the contiguous sectors */
+
+ ret = bch->inode->u.i_bops->write(bch->inode, bch->buffer, sector, nsectors);
+ if (ret < 0)
+ {
+ fdbg("Write failed: %d\n");
+ return ret;
+ }
+
+ /* Adjust pointers and counts */
+
+ sectoffset = 0;
+ sector += nsectors;
+
+ nbytes = nsectors * bch->sectsize;
+ byteswritten += nbytes;
+
+ if (sector >= bch->nsectors)
+ {
+ return byteswritten;
+ }
+
+ buffer += nbytes;
+ len -= nbytes;
+ }
+
+ /* Then write any partial final sector */
+
+ if (len > 0)
+ {
+ /* Read the sector into the sector buffer */
+
+ bchlib_readsector(bch, sector);
+
+ /* Copy the head end of the sector from the user buffer */
+
+ memcpy(bch->buffer, buffer, len);
+ bch->dirty = TRUE;
+
+ /* Adjust counts */
+
+ byteswritten += len;
+ }
+
+ return byteswritten;
+}
+
diff --git a/nuttx/drivers/bch/bchlib_refs.c b/nuttx/drivers/bch/bchlib_refs.c
new file mode 100644
index 000000000..43ecf061a
--- /dev/null
+++ b/nuttx/drivers/bch/bchlib_refs.c
@@ -0,0 +1,123 @@
+/****************************************************************************
+ * drivers/bch/bchlib_refs.c
+ *
+ * Copyright (C) 2008 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 NuttX 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Compilation Switches
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <sys/types.h>
+#if 0
+#include <sys/stat.h>
+#include <sys/ioctl.h>
+
+#include <unistd.h>
+#include <string.h>
+#include <fcntl.h>
+#include <sched.h>
+
+#include <nuttx/fs.h>
+#include <nuttx/ioctl.h>
+#endif
+
+#include <semaphore.h>
+#include <errno.h>
+#include <assert.h>
+#include <debug.h>
+
+#include "bch_internal.h"
+
+/****************************************************************************
+ * Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: bchlib_incref
+ ****************************************************************************/
+
+int bchlib_incref(FAR struct bchlib_s *bch)
+{
+ bchlib_semtake(bch);
+ if (bch->refs == MAX_OPENCNT)
+ {
+ return -EMFILE;
+ }
+ else
+ {
+ bch->refs++;
+ }
+ bchlib_semgive(bch);
+ return OK;
+}
+
+/****************************************************************************
+ * Name: bchlib_decref
+ ****************************************************************************/
+
+int bchlib_decref(FAR struct bchlib_s *bch)
+{
+ bchlib_semtake(bch);
+ if (bch->refs == 0)
+ {
+ return -EIO;
+ }
+ else
+ {
+ bch->refs--;
+ }
+ bchlib_semgive(bch);
+ return OK;
+}
diff --git a/nuttx/drivers/bch/bchlib_sem.c b/nuttx/drivers/bch/bchlib_sem.c
new file mode 100644
index 000000000..c7b0f39f7
--- /dev/null
+++ b/nuttx/drivers/bch/bchlib_sem.c
@@ -0,0 +1,90 @@
+/****************************************************************************
+ * drivers/bch/bchlib_sem.c
+ *
+ * Copyright (C) 2008 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 NuttX 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Compilation Switches
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <sys/types.h>
+
+#include <errno.h>
+#include <assert.h>
+#include <debug.h>
+
+#include "bch_internal.h"
+
+/****************************************************************************
+ * Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: bch_semtake
+ ****************************************************************************/
+
+void bchlib_semtake(FAR struct bchlib_s *bch)
+{
+ /* Take the semaphore (perhaps waiting) */
+
+ while (sem_wait(&bch->sem) != 0)
+ {
+ /* The only case that an error should occur here is if
+ * the wait was awakened by a signal.
+ */
+
+ ASSERT(errno == EINTR);
+ }
+}
diff --git a/nuttx/drivers/bch/bchlib_setup.c b/nuttx/drivers/bch/bchlib_setup.c
new file mode 100644
index 000000000..35520ad3b
--- /dev/null
+++ b/nuttx/drivers/bch/bchlib_setup.c
@@ -0,0 +1,160 @@
+/****************************************************************************
+ * drivers/bch/bchlib_setup.c
+ *
+ * Copyright (C) 2008 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 NuttX 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Compilation Switches
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <sys/types.h>
+#include <sys/mount.h>
+
+#include <stdlib.h>
+#include <errno.h>
+#include <assert.h>
+#include <debug.h>
+
+#include <nuttx/fs.h>
+
+#include "bch_internal.h"
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: bchlib_setup
+ *
+ * Description:
+ * Setup so that the block driver referenced by 'blkdev' can be accessed
+ * similar to a character device.
+ *
+ ****************************************************************************/
+
+int bchlib_setup(const char *blkdev, boolean readonly, FAR void **handle)
+{
+ FAR struct bchlib_s *bch;
+ struct geometry geo;
+ int ret;
+
+ DEBUGASSERT(blkdev);
+
+ /* Allocate the BCH state structure */
+
+ bch = (FAR struct bchlib_s*)zalloc(sizeof(struct bchlib_s));
+ if (!bch)
+ {
+ fdbg("Failed to allocate BCH structure\n");
+ return -ENOMEM;
+ }
+
+ /* Open the block driver */
+
+ ret = open_blockdriver(blkdev, readonly ? MS_RDONLY : 0, &bch->inode);
+ if (ret < 0)
+ {
+ fdbg("Failed to open driver %s: %d\n", blkdev, -ret);
+ goto errout_with_bch;
+ }
+
+ DEBUGASSERT(bch->inode && bch->inode->u.i_bops && bch->inode->u.i_bops->geometry);
+
+ ret = bch->inode->u.i_bops->geometry(bch->inode, &geo);
+ if (ret < 0)
+ {
+ fdbg("geometry failed: %d\n", -ret);
+ goto errout_with_bch;
+ }
+
+ if (!geo.geo_available)
+ {
+ fdbg("geometry failed: %d\n", -ret);
+ ret = -ENODEV;
+ goto errout_with_bch;
+ }
+
+ if (!readonly && (!bch->inode->u.i_bops->write || !geo.geo_writeenabled))
+ {
+ fdbg("write access not supported\n");
+ ret = -EACCES;
+ goto errout_with_bch;
+ }
+
+ /* Save the geometry info and complete initialization of the structure */
+
+ sem_init(&bch->sem, 0, 1);
+ bch->nsectors = geo.geo_nsectors;
+ bch->sectsize = geo.geo_sectorsize;
+ bch->sector = (size_t)-1;
+ bch->readonly = readonly;
+
+ /* Allocate the sector I/O buffer */
+
+ bch->buffer = (FAR ubyte *)malloc(bch->sectsize);
+ if (!bch->buffer)
+ {
+ fdbg("Failed to allocate sector buffer\n");
+ ret = -ENOMEM;
+ goto errout_with_bch;
+ }
+
+ *handle = bch;
+ return OK;
+
+errout_with_bch:
+ free(bch);
+ return ret;
+}
diff --git a/nuttx/drivers/bch/bchlib_teardown.c b/nuttx/drivers/bch/bchlib_teardown.c
new file mode 100644
index 000000000..9580d24e4
--- /dev/null
+++ b/nuttx/drivers/bch/bchlib_teardown.c
@@ -0,0 +1,118 @@
+/****************************************************************************
+ * drivers/bch/bchlib_teardown.c
+ *
+ * Copyright (C) 2008 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 NuttX 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Compilation Switches
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <sys/types.h>
+
+#include <stdlib.h>
+#include <errno.h>
+#include <assert.h>
+#include <debug.h>
+
+#include <nuttx/fs.h>
+
+#include "bch_internal.h"
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: bchlib_teardown
+ *
+ * Description:
+ * Setup so that the block driver referenced by 'blkdev' can be accessed
+ * similar to a character device.
+ *
+ ****************************************************************************/
+
+int bchlib_teardown(FAR void *handle)
+{
+ FAR struct bchlib_s *bch = (FAR struct bchlib_s *)handle;
+
+ DEBUGASSERT(handle);
+
+ /* Check that there are not outstanding reference counts on the object */
+
+ if (bch->refs > 0)
+ {
+ return -EBUSY;
+ }
+
+ /* Flush any pending data to the block driver */
+
+ bchlib_flushsector(bch);
+
+ /* Close the block driver */
+
+ (void)close_blockdriver(bch->inode);
+
+ /* Free the BCH state structure */
+
+ if (bch->buffer)
+ {
+ free(bch->buffer);
+ }
+
+ sem_destroy(&bch->sem);
+ free(bch);
+ return OK;
+}
+
diff --git a/nuttx/drivers/bch/bchlib_write.c b/nuttx/drivers/bch/bchlib_write.c
new file mode 100644
index 000000000..a897215ea
--- /dev/null
+++ b/nuttx/drivers/bch/bchlib_write.c
@@ -0,0 +1,207 @@
+/****************************************************************************
+ * drivers/bch/bchlib_write.c
+ *
+ * Copyright (C) 2008 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 NuttX 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Compilation Switches
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <sys/types.h>
+
+#include <string.h>
+#include <errno.h>
+#include <assert.h>
+#include <debug.h>
+
+#include <nuttx/fs.h>
+
+#include "bch_internal.h"
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: bchlib_write
+ *
+ * Description:
+ * Write to the block device set-up by bchlib_setup as if it were a character
+ * device.
+ *
+ ****************************************************************************/
+
+ssize_t bchlib_write(FAR void *handle, FAR const char *buffer, size_t offset, size_t len)
+{
+ FAR struct bchlib_s *bch;
+ size_t nsectors;
+ size_t sector;
+ uint16 sectoffset;
+ size_t nbytes;
+ size_t byteswritten;
+ int ret;
+
+ /* Get rid of this special case right away */
+
+ if (len < 1)
+ {
+ return 0;
+ }
+
+ /* Convert the file position into a sector number an offset. */
+
+ sector = offset / bch->sectsize;
+ sectoffset = offset - sector * bch->sectsize;
+
+ if (sector >= bch->nsectors)
+ {
+ return -EFBIG;
+ }
+
+ /* Write the initial partial sector */
+
+ byteswritten = 0;
+ if (sectoffset > 0)
+ {
+ /* Read the full sector into the sector buffer */
+
+ bchlib_readsector(bch, sector);
+
+ /* Copy the tail end of the sector from the user buffer */
+
+ if (sectoffset + len > bch->sectsize)
+ {
+ nbytes = bch->sectsize - sectoffset;
+ }
+ else
+ {
+ nbytes = len;
+ }
+
+ memcpy(&bch->buffer[sectoffset], buffer, nbytes);
+ bch->dirty = TRUE;
+
+ /* Adjust pointers and counts */
+
+ sectoffset = 0;
+ sector++;
+
+ if (sector >= bch->nsectors)
+ {
+ return nbytes;
+ }
+
+ byteswritten = nbytes;
+ buffer += nbytes;
+ len -= nbytes;
+ }
+
+ /* Then write all of the full sectors following the partial sector */
+
+ if (len >= bch->sectsize )
+ {
+ nsectors = len / bch->sectsize;
+ if (sector + nsectors > bch->nsectors)
+ {
+ nsectors = bch->nsectors - sector;
+ }
+
+ /* Write the contiguous sectors */
+
+ ret = bch->inode->u.i_bops->write(bch->inode, bch->buffer, sector, nsectors);
+ if (ret < 0)
+ {
+ fdbg("Write failed: %d\n");
+ return ret;
+ }
+
+ /* Adjust pointers and counts */
+
+ sectoffset = 0;
+ sector += nsectors;
+
+ nbytes = nsectors * bch->sectsize;
+ byteswritten += nbytes;
+
+ if (sector >= bch->nsectors)
+ {
+ return byteswritten;
+ }
+
+ buffer += nbytes;
+ len -= nbytes;
+ }
+
+ /* Then write any partial final sector */
+
+ if (len > 0)
+ {
+ /* Read the sector into the sector buffer */
+
+ bchlib_readsector(bch, sector);
+
+ /* Copy the head end of the sector from the user buffer */
+
+ memcpy(bch->buffer, buffer, len);
+ bch->dirty = TRUE;
+
+ /* Adjust counts */
+
+ byteswritten += len;
+ }
+
+ return byteswritten;
+}
+