2008-11-15 01:14:05 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/bch/bchlib_teardown.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 <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_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 */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
close_blockdriver(bch->inode);
|
2008-11-15 01:14:05 +01:00
|
|
|
|
|
|
|
/* Free the BCH state structure */
|
|
|
|
|
|
|
|
if (bch->buffer)
|
|
|
|
{
|
2014-09-01 01:04:02 +02:00
|
|
|
kmm_free(bch->buffer);
|
2008-11-15 01:14:05 +01:00
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_destroy(&bch->lock);
|
2014-09-01 01:04:02 +02:00
|
|
|
kmm_free(bch);
|
2008-11-15 01:14:05 +01:00
|
|
|
return OK;
|
|
|
|
}
|