diff options
Diffstat (limited to 'drivers/io/io_storage.c')
-rw-r--r-- | drivers/io/io_storage.c | 106 |
1 files changed, 34 insertions, 72 deletions
diff --git a/drivers/io/io_storage.c b/drivers/io/io_storage.c index a3a8186d..0918de0a 100644 --- a/drivers/io/io_storage.c +++ b/drivers/io/io_storage.c @@ -1,31 +1,7 @@ /* - * Copyright (c) 2014, ARM Limited and Contributors. All rights reserved. + * Copyright (c) 2014-2017, ARM Limited and Contributors. All rights reserved. * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * 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. - * - * Neither the name of ARM 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 HOLDER 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. + * SPDX-License-Identifier: BSD-3-Clause */ #include <assert.h> @@ -51,8 +27,8 @@ static const io_dev_info_t *devices[MAX_IO_DEVICES]; /* Number of currently registered devices */ static unsigned int dev_count; - -#if DEBUG /* Extra validation functions only used in debug builds */ +/* Extra validation functions only used when asserts are enabled */ +#if ENABLE_ASSERTIONS /* Return a boolean value indicating whether a device connector is valid */ static int is_valid_dev_connector(const io_dev_connector_t *dev_con) @@ -89,14 +65,15 @@ static int is_valid_seek_mode(io_seek_mode_t mode) return ((mode != IO_SEEK_INVALID) && (mode < IO_SEEK_MAX)); } -#endif /* End of debug-only validation functions */ +#endif /* ENABLE_ASSERTIONS */ +/* End of extra validation functions only used when asserts are enabled */ /* Open a connection to a specific device */ static int dev_open(const io_dev_connector_t *dev_con, const uintptr_t dev_spec, io_dev_info_t **dev_info) { - int result = IO_FAIL; + int result; assert(dev_info != NULL); assert(is_valid_dev_connector(dev_con)); @@ -116,10 +93,10 @@ static void set_handle(uintptr_t *handle, io_entity_t *entity) /* Locate an entity in the pool, specified by address */ static int find_first_entity(const io_entity_t *entity, unsigned int *index_out) { - int result = IO_FAIL; - for (int index = 0; index < MAX_IO_HANDLES; ++index) { + int result = -ENOENT; + for (unsigned int index = 0; index < MAX_IO_HANDLES; ++index) { if (entity_map[index] == entity) { - result = IO_SUCCESS; + result = 0; *index_out = index; break; } @@ -131,17 +108,16 @@ static int find_first_entity(const io_entity_t *entity, unsigned int *index_out) /* Allocate an entity from the pool and return a pointer to it */ static int allocate_entity(io_entity_t **entity) { - int result = IO_FAIL; + int result = -ENOMEM; assert(entity != NULL); if (entity_count < MAX_IO_HANDLES) { unsigned int index = 0; result = find_first_entity(NULL, &index); - assert(result == IO_SUCCESS); + assert(result == 0); *entity = entity_map[index] = &entity_pool[index]; ++entity_count; - } else - result = IO_RESOURCES_EXHAUSTED; + } return result; } @@ -150,12 +126,12 @@ static int allocate_entity(io_entity_t **entity) /* Release an entity back to the pool */ static int free_entity(const io_entity_t *entity) { - int result = IO_FAIL; + int result; unsigned int index = 0; assert(entity != NULL); result = find_first_entity(entity, &index); - if (result == IO_SUCCESS) { + if (result == 0) { entity_map[index] = NULL; --entity_count; } @@ -169,15 +145,13 @@ static int free_entity(const io_entity_t *entity) /* Register a device driver */ int io_register_device(const io_dev_info_t *dev_info) { - int result = IO_FAIL; + int result = -ENOMEM; assert(dev_info != NULL); if (dev_count < MAX_IO_DEVICES) { devices[dev_count] = dev_info; dev_count++; - result = IO_SUCCESS; - } else { - result = IO_RESOURCES_EXHAUSTED; + result = 0; } return result; @@ -188,7 +162,7 @@ int io_register_device(const io_dev_info_t *dev_info) int io_dev_open(const io_dev_connector_t *dev_con, const uintptr_t dev_spec, uintptr_t *handle) { - int result = IO_FAIL; + int result; assert(handle != NULL); result = dev_open(dev_con, dev_spec, (io_dev_info_t **)handle); @@ -200,18 +174,17 @@ int io_dev_open(const io_dev_connector_t *dev_con, const uintptr_t dev_spec, * re-initialisation */ int io_dev_init(uintptr_t dev_handle, const uintptr_t init_params) { - int result = IO_FAIL; + int result = 0; assert(dev_handle != (uintptr_t)NULL); assert(is_valid_dev(dev_handle)); io_dev_info_t *dev = (io_dev_info_t *)dev_handle; + /* Absence of registered function implies NOP here */ if (dev->funcs->dev_init != NULL) { result = dev->funcs->dev_init(dev, init_params); - } else { - /* Absence of registered function implies NOP here */ - result = IO_SUCCESS; } + return result; } @@ -221,17 +194,15 @@ int io_dev_init(uintptr_t dev_handle, const uintptr_t init_params) /* Close a connection to a device */ int io_dev_close(uintptr_t dev_handle) { - int result = IO_FAIL; + int result = 0; assert(dev_handle != (uintptr_t)NULL); assert(is_valid_dev(dev_handle)); io_dev_info_t *dev = (io_dev_info_t *)dev_handle; + /* Absence of registered function implies NOP here */ if (dev->funcs->dev_close != NULL) { result = dev->funcs->dev_close(dev); - } else { - /* Absence of registered function implies NOP here */ - result = IO_SUCCESS; } return result; @@ -244,7 +215,7 @@ int io_dev_close(uintptr_t dev_handle) /* Open an IO entity */ int io_open(uintptr_t dev_handle, const uintptr_t spec, uintptr_t *handle) { - int result = IO_FAIL; + int result; assert((spec != (uintptr_t)NULL) && (handle != NULL)); assert(is_valid_dev(dev_handle)); @@ -253,11 +224,11 @@ int io_open(uintptr_t dev_handle, const uintptr_t spec, uintptr_t *handle) result = allocate_entity(&entity); - if (result == IO_SUCCESS) { + if (result == 0) { assert(dev->funcs->open != NULL); result = dev->funcs->open(dev, spec, entity); - if (result == IO_SUCCESS) { + if (result == 0) { entity->dev_handle = dev; set_handle(handle, entity); } else @@ -270,7 +241,7 @@ int io_open(uintptr_t dev_handle, const uintptr_t spec, uintptr_t *handle) /* Seek to a specific position in an IO entity */ int io_seek(uintptr_t handle, io_seek_mode_t mode, ssize_t offset) { - int result = IO_FAIL; + int result = -ENODEV; assert(is_valid_entity(handle) && is_valid_seek_mode(mode)); io_entity_t *entity = (io_entity_t *)handle; @@ -279,8 +250,6 @@ int io_seek(uintptr_t handle, io_seek_mode_t mode, ssize_t offset) if (dev->funcs->seek != NULL) result = dev->funcs->seek(entity, mode, offset); - else - result = IO_NOT_SUPPORTED; return result; } @@ -289,7 +258,7 @@ int io_seek(uintptr_t handle, io_seek_mode_t mode, ssize_t offset) /* Determine the length of an IO entity */ int io_size(uintptr_t handle, size_t *length) { - int result = IO_FAIL; + int result = -ENODEV; assert(is_valid_entity(handle) && (length != NULL)); io_entity_t *entity = (io_entity_t *)handle; @@ -298,8 +267,6 @@ int io_size(uintptr_t handle, size_t *length) if (dev->funcs->size != NULL) result = dev->funcs->size(entity, length); - else - result = IO_NOT_SUPPORTED; return result; } @@ -311,7 +278,7 @@ int io_read(uintptr_t handle, size_t length, size_t *length_read) { - int result = IO_FAIL; + int result = -ENODEV; assert(is_valid_entity(handle) && (buffer != (uintptr_t)NULL)); io_entity_t *entity = (io_entity_t *)handle; @@ -320,8 +287,6 @@ int io_read(uintptr_t handle, if (dev->funcs->read != NULL) result = dev->funcs->read(entity, buffer, length, length_read); - else - result = IO_NOT_SUPPORTED; return result; } @@ -333,7 +298,7 @@ int io_write(uintptr_t handle, size_t length, size_t *length_written) { - int result = IO_FAIL; + int result = -ENODEV; assert(is_valid_entity(handle) && (buffer != (uintptr_t)NULL)); io_entity_t *entity = (io_entity_t *)handle; @@ -343,8 +308,7 @@ int io_write(uintptr_t handle, if (dev->funcs->write != NULL) { result = dev->funcs->write(entity, buffer, length, length_written); - } else - result = IO_NOT_SUPPORTED; + } return result; } @@ -353,19 +317,17 @@ int io_write(uintptr_t handle, /* Close an IO entity */ int io_close(uintptr_t handle) { - int result = IO_FAIL; + int result = 0; assert(is_valid_entity(handle)); io_entity_t *entity = (io_entity_t *)handle; io_dev_info_t *dev = entity->dev_handle; + /* Absence of registered function implies NOP here */ if (dev->funcs->close != NULL) result = dev->funcs->close(entity); - else { - /* Absence of registered function implies NOP here */ - result = IO_SUCCESS; - } + /* Ignore improbable free_entity failure */ (void)free_entity(entity); |