From c956d6d820f5196fecde268a9334df557ad3c3b6 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Thu, 13 Nov 2014 09:04:42 -0600 Subject: [PATCH 1/2] =?UTF-8?q?Add=20support=20for=20seeking=20in=20BCH.?= =?UTF-8?q?=20=20From=20S=C3=A9bastien=20Lorquet.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- drivers/bch/bchdev_driver.c | 71 +++++++++++++++++++++++++++++++++++-- 1 file changed, 69 insertions(+), 2 deletions(-) diff --git a/drivers/bch/bchdev_driver.c b/drivers/bch/bchdev_driver.c index b4517065237..7e3d2abcb37 100644 --- a/drivers/bch/bchdev_driver.c +++ b/drivers/bch/bchdev_driver.c @@ -70,6 +70,7 @@ static int bch_open(FAR struct file *filep); static int bch_close(FAR struct file *filep); +static off_t bch_seek(FAR struct file *filep, off_t offset, int whence); static ssize_t bch_read(FAR struct file *filep, FAR char *buffer, size_t buflen); static ssize_t bch_write(FAR struct file *filep, FAR const char *buffer, @@ -87,7 +88,7 @@ const struct file_operations bch_fops = bch_close, /* close */ bch_read, /* read */ bch_write, /* write */ - 0, /* seek */ + bch_seek, /* seek */ bch_ioctl /* ioctl */ #ifndef CONFIG_DISABLE_POLL , 0 /* poll */ @@ -168,7 +169,73 @@ static int bch_close(FAR struct file *filep) } /**************************************************************************** - * Name:bch_read + * Name: bch_seek + ****************************************************************************/ + +static off_t bch_seek(FAR struct file *filep, off_t offset, int whence) +{ + FAR struct inode *inode = filep->f_inode; + FAR struct bchlib_s *bch; + off_t newpos; + int ret; + + DEBUGASSERT(inode && inode->i_private); + + bch = (FAR struct bchlib_s *)inode->i_private; + bchlib_semtake(bch); + + /* Determine the new, requested file position */ + + switch (whence) + { + case SEEK_CUR: + newpos = filep->f_pos + offset; + break; + + case SEEK_SET: + newpos = offset; + break; + + case SEEK_END: + newpos = bch->sectsize * bch->nsectors + offset; + break; + + default: + /* Return EINVAL if the whence argument is invalid */ + + bchlib_semgive(bch); + return -EINVAL; + } + + /* Opengroup.org: + * + * "The lseek() function shall allow the file offset to be set beyond the end + * of the existing data in the file. If data is later written at this point, + * subsequent reads of data in the gap shall return bytes with the value 0 + * until data is actually written into the gap." + * + * We can conform to the first part, but not the second. But return EINVAL if + * + * "...the resulting file offset would be negative for a regular file, block + * special file, or directory." + */ + + if (newpos >= 0) + { + filep->f_pos = newpos; + ret = newpos; + } + else + { + ret = -EINVAL; + } + + bchlib_semgive(bch); + return ret; +} + +/**************************************************************************** + * Name: bch_read ****************************************************************************/ static ssize_t bch_read(FAR struct file *filep, FAR char *buffer, size_t len) From e0270c568b3f8605555f0efb115fe8da56fa8895 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Thu, 13 Nov 2014 09:07:27 -0600 Subject: [PATCH 2/2] Update ChangeLog --- ChangeLog | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ChangeLog b/ChangeLog index 546d4cd8221..45534fa9804 100755 --- a/ChangeLog +++ b/ChangeLog @@ -8954,3 +8954,5 @@ * arch/arm/src/efm32/efm32_clockconfig.c, configs/efm32gg-stk3700/README.txt and include/board.h: Fixes to get the EFM32GG-STK3700 basic NSH configuration running. It works! (2014-11-12). + * drivers/bch/bchdev_driver.c: Add support for seeking in BCH. From + Sébastien Lorquet. (2014-11-13).