2008-11-15 01:14:05 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/bch/bchlib_setup.c
|
|
|
|
*
|
2021-03-04 07:10:42 +01: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 01:14:05 +01:00
|
|
|
*
|
2021-03-04 07:10:42 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2008-11-15 01:14:05 +01:00
|
|
|
*
|
2021-03-04 07:10:42 +01: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 01:14:05 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
|
2009-12-15 15:25:14 +01:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
2008-11-15 01:14:05 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2011-04-06 18:40:47 +02:00
|
|
|
#include <nuttx/kmalloc.h>
|
2012-03-21 19:01:07 +01:00
|
|
|
#include <nuttx/fs/fs.h>
|
2023-05-30 06:51:09 +02:00
|
|
|
#include <nuttx/drivers/drivers.h>
|
2008-11-15 01:14:05 +01:00
|
|
|
|
2015-11-21 16:15:12 +01:00
|
|
|
#include "bch.h"
|
2008-11-15 01:14:05 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: bchlib_setup
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Setup so that the block driver referenced by 'blkdev' can be accessed
|
|
|
|
* similar to a character device.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2024-08-25 01:21:12 +02:00
|
|
|
int bchlib_setup(FAR const char *blkdev, bool readonly, FAR void **handle)
|
2008-11-15 01:14:05 +01:00
|
|
|
{
|
|
|
|
FAR struct bchlib_s *bch;
|
|
|
|
struct geometry geo;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
DEBUGASSERT(blkdev);
|
|
|
|
|
|
|
|
/* Allocate the BCH state structure */
|
|
|
|
|
2023-08-28 09:39:47 +02:00
|
|
|
bch = kmm_zalloc(sizeof(struct bchlib_s));
|
2008-11-15 01:14:05 +01:00
|
|
|
if (!bch)
|
|
|
|
{
|
2016-06-12 17:26:00 +02:00
|
|
|
ferr("ERROR: Failed to allocate BCH structure\n");
|
2008-11-15 01:14:05 +01:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Open the block driver */
|
|
|
|
|
|
|
|
ret = open_blockdriver(blkdev, readonly ? MS_RDONLY : 0, &bch->inode);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 17:26:00 +02:00
|
|
|
ferr("ERROR: Failed to open driver %s: %d\n", blkdev, -ret);
|
2008-11-15 01:14:05 +01:00
|
|
|
goto errout_with_bch;
|
|
|
|
}
|
|
|
|
|
2020-03-11 06:44:32 +01:00
|
|
|
DEBUGASSERT(bch->inode && bch->inode->u.i_bops &&
|
|
|
|
bch->inode->u.i_bops->geometry);
|
2008-11-15 01:14:05 +01:00
|
|
|
|
|
|
|
ret = bch->inode->u.i_bops->geometry(bch->inode, &geo);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 17:26:00 +02:00
|
|
|
ferr("ERROR: geometry failed: %d\n", -ret);
|
2008-11-15 01:14:05 +01:00
|
|
|
goto errout_with_bch;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!geo.geo_available)
|
|
|
|
{
|
2016-06-12 17:26:00 +02:00
|
|
|
ferr("ERROR: geometry failed: %d\n", -ret);
|
2008-11-15 01:14:05 +01:00
|
|
|
ret = -ENODEV;
|
|
|
|
goto errout_with_bch;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!readonly && (!bch->inode->u.i_bops->write || !geo.geo_writeenabled))
|
|
|
|
{
|
2016-06-12 17:26:00 +02:00
|
|
|
ferr("ERROR: write access not supported\n");
|
2008-11-15 01:14:05 +01:00
|
|
|
ret = -EACCES;
|
|
|
|
goto errout_with_bch;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the geometry info and complete initialization of the structure */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_init(&bch->lock);
|
2008-11-15 01:14:05 +01:00
|
|
|
bch->nsectors = geo.geo_nsectors;
|
|
|
|
bch->sectsize = geo.geo_sectorsize;
|
|
|
|
bch->sector = (size_t)-1;
|
|
|
|
bch->readonly = readonly;
|
|
|
|
*handle = bch;
|
|
|
|
return OK;
|
|
|
|
|
|
|
|
errout_with_bch:
|
2014-09-01 01:04:02 +02:00
|
|
|
kmm_free(bch);
|
2008-11-15 01:14:05 +01:00
|
|
|
return ret;
|
|
|
|
}
|