2008-11-15 08:14:05 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/bch/bchlib_setup.c
|
|
|
|
*
|
2021-03-04 14:10:42 +08:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2008-11-15 08:14:05 +08:00
|
|
|
*
|
2021-03-04 14:10:42 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2008-11-15 08:14:05 +08:00
|
|
|
*
|
2021-03-04 14:10:42 +08:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2008-11-15 08:14:05 +08:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
|
2009-12-15 22:25:14 +08:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
2008-11-15 08:14:05 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2011-04-07 00:40:47 +08:00
|
|
|
#include <nuttx/kmalloc.h>
|
2012-03-22 02:01:07 +08:00
|
|
|
#include <nuttx/fs/fs.h>
|
2023-05-30 12:51:09 +08:00
|
|
|
#include <nuttx/drivers/drivers.h>
|
2008-11-15 08:14:05 +08:00
|
|
|
|
2015-11-21 23:15:12 +08:00
|
|
|
#include "bch.h"
|
2008-11-15 08:14:05 +08:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: bchlib_setup
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Setup so that the block driver referenced by 'blkdev' can be accessed
|
|
|
|
* similar to a character device.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2009-12-15 22:25:14 +08:00
|
|
|
int bchlib_setup(const char *blkdev, bool readonly, FAR void **handle)
|
2008-11-15 08:14:05 +08:00
|
|
|
{
|
|
|
|
FAR struct bchlib_s *bch;
|
|
|
|
struct geometry geo;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
DEBUGASSERT(blkdev);
|
|
|
|
|
|
|
|
/* Allocate the BCH state structure */
|
|
|
|
|
2023-08-28 15:39:47 +08:00
|
|
|
bch = kmm_zalloc(sizeof(struct bchlib_s));
|
2008-11-15 08:14:05 +08:00
|
|
|
if (!bch)
|
|
|
|
{
|
2016-06-12 23:26:00 +08:00
|
|
|
ferr("ERROR: Failed to allocate BCH structure\n");
|
2008-11-15 08:14:05 +08:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Open the block driver */
|
|
|
|
|
|
|
|
ret = open_blockdriver(blkdev, readonly ? MS_RDONLY : 0, &bch->inode);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 23:26:00 +08:00
|
|
|
ferr("ERROR: Failed to open driver %s: %d\n", blkdev, -ret);
|
2008-11-15 08:14:05 +08:00
|
|
|
goto errout_with_bch;
|
|
|
|
}
|
|
|
|
|
2020-03-11 13:44:32 +08:00
|
|
|
DEBUGASSERT(bch->inode && bch->inode->u.i_bops &&
|
|
|
|
bch->inode->u.i_bops->geometry);
|
2008-11-15 08:14:05 +08:00
|
|
|
|
|
|
|
ret = bch->inode->u.i_bops->geometry(bch->inode, &geo);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 23:26:00 +08:00
|
|
|
ferr("ERROR: geometry failed: %d\n", -ret);
|
2008-11-15 08:14:05 +08:00
|
|
|
goto errout_with_bch;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!geo.geo_available)
|
|
|
|
{
|
2016-06-12 23:26:00 +08:00
|
|
|
ferr("ERROR: geometry failed: %d\n", -ret);
|
2008-11-15 08:14:05 +08:00
|
|
|
ret = -ENODEV;
|
|
|
|
goto errout_with_bch;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!readonly && (!bch->inode->u.i_bops->write || !geo.geo_writeenabled))
|
|
|
|
{
|
2016-06-12 23:26:00 +08:00
|
|
|
ferr("ERROR: write access not supported\n");
|
2008-11-15 08:14:05 +08:00
|
|
|
ret = -EACCES;
|
|
|
|
goto errout_with_bch;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the geometry info and complete initialization of the structure */
|
|
|
|
|
2022-09-06 14:18:45 +08:00
|
|
|
nxmutex_init(&bch->lock);
|
2008-11-15 08:14:05 +08:00
|
|
|
bch->nsectors = geo.geo_nsectors;
|
|
|
|
bch->sectsize = geo.geo_sectorsize;
|
|
|
|
bch->sector = (size_t)-1;
|
|
|
|
bch->readonly = readonly;
|
|
|
|
|
|
|
|
/* Allocate the sector I/O buffer */
|
|
|
|
|
2021-12-06 22:53:21 +08:00
|
|
|
#if CONFIG_BCH_BUFFER_ALIGNMENT != 0
|
|
|
|
bch->buffer = kmm_memalign(CONFIG_BCH_BUFFER_ALIGNMENT, bch->sectsize);
|
|
|
|
#else
|
|
|
|
bch->buffer = kmm_malloc(bch->sectsize);
|
|
|
|
#endif
|
2008-11-15 08:14:05 +08:00
|
|
|
if (!bch->buffer)
|
|
|
|
{
|
2016-06-12 23:26:00 +08:00
|
|
|
ferr("ERROR: Failed to allocate sector buffer\n");
|
2008-11-15 08:14:05 +08:00
|
|
|
ret = -ENOMEM;
|
|
|
|
goto errout_with_bch;
|
|
|
|
}
|
|
|
|
|
|
|
|
*handle = bch;
|
|
|
|
return OK;
|
|
|
|
|
|
|
|
errout_with_bch:
|
2014-09-01 07:04:02 +08:00
|
|
|
kmm_free(bch);
|
2008-11-15 08:14:05 +08:00
|
|
|
return ret;
|
|
|
|
}
|