James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 1 | /* |
Antonio Nino Diaz | aa61368 | 2017-03-22 15:48:51 +0000 | [diff] [blame] | 2 | * Copyright (c) 2014-2017, ARM Limited and Contributors. All rights reserved. |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 3 | * |
dp-arm | 82cb2c1 | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 7 | #include <assert.h> |
Dan Handley | 97043ac | 2014-04-09 13:14:54 +0100 | [diff] [blame] | 8 | #include <stddef.h> |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 9 | |
Antonio Nino Diaz | 09d40e0 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 10 | #include <platform_def.h> |
| 11 | |
| 12 | #include <drivers/io/io_driver.h> |
| 13 | #include <drivers/io/io_storage.h> |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 14 | |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 15 | /* Storage for a fixed maximum number of IO entities, definable by platform */ |
Dan Handley | fb037bf | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 16 | static io_entity_t entity_pool[MAX_IO_HANDLES]; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 17 | |
| 18 | /* Simple way of tracking used storage - each entry is NULL or a pointer to an |
| 19 | * entity */ |
Dan Handley | fb037bf | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 20 | static io_entity_t *entity_map[MAX_IO_HANDLES]; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 21 | |
| 22 | /* Track number of allocated entities */ |
| 23 | static unsigned int entity_count; |
| 24 | |
Dan Handley | 6d16ce0 | 2014-08-04 18:31:43 +0100 | [diff] [blame] | 25 | /* Array of fixed maximum of registered devices, definable by platform */ |
| 26 | static const io_dev_info_t *devices[MAX_IO_DEVICES]; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 27 | |
Dan Handley | 6d16ce0 | 2014-08-04 18:31:43 +0100 | [diff] [blame] | 28 | /* Number of currently registered devices */ |
| 29 | static unsigned int dev_count; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 30 | |
Antonio Nino Diaz | aa61368 | 2017-03-22 15:48:51 +0000 | [diff] [blame] | 31 | /* Extra validation functions only used when asserts are enabled */ |
| 32 | #if ENABLE_ASSERTIONS |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 33 | |
| 34 | /* Return a boolean value indicating whether a device connector is valid */ |
Dan Handley | fb037bf | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 35 | static int is_valid_dev_connector(const io_dev_connector_t *dev_con) |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 36 | { |
| 37 | int result = (dev_con != NULL) && (dev_con->dev_open != NULL); |
| 38 | return result; |
| 39 | } |
| 40 | |
| 41 | |
| 42 | /* Return a boolean value indicating whether a device handle is valid */ |
Dan Handley | 625de1d | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 43 | static int is_valid_dev(const uintptr_t dev_handle) |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 44 | { |
Dan Handley | 625de1d | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 45 | const io_dev_info_t *dev = (io_dev_info_t *)dev_handle; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 46 | int result = (dev != NULL) && (dev->funcs != NULL) && |
| 47 | (dev->funcs->type != NULL) && |
| 48 | (dev->funcs->type() < IO_TYPE_MAX); |
| 49 | return result; |
| 50 | } |
| 51 | |
| 52 | |
| 53 | /* Return a boolean value indicating whether an IO entity is valid */ |
Dan Handley | 625de1d | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 54 | static int is_valid_entity(const uintptr_t handle) |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 55 | { |
Dan Handley | 625de1d | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 56 | const io_entity_t *entity = (io_entity_t *)handle; |
| 57 | int result = (entity != NULL) && |
| 58 | (is_valid_dev((uintptr_t)entity->dev_handle)); |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 59 | return result; |
| 60 | } |
| 61 | |
| 62 | |
| 63 | /* Return a boolean value indicating whether a seek mode is valid */ |
Dan Handley | fb037bf | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 64 | static int is_valid_seek_mode(io_seek_mode_t mode) |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 65 | { |
| 66 | return ((mode != IO_SEEK_INVALID) && (mode < IO_SEEK_MAX)); |
| 67 | } |
| 68 | |
Antonio Nino Diaz | aa61368 | 2017-03-22 15:48:51 +0000 | [diff] [blame] | 69 | #endif /* ENABLE_ASSERTIONS */ |
| 70 | /* End of extra validation functions only used when asserts are enabled */ |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 71 | |
| 72 | |
| 73 | /* Open a connection to a specific device */ |
Dan Handley | 625de1d | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 74 | static int dev_open(const io_dev_connector_t *dev_con, const uintptr_t dev_spec, |
Dan Handley | fb037bf | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 75 | io_dev_info_t **dev_info) |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 76 | { |
Juan Castillo | e098e24 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 77 | int result; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 78 | assert(dev_info != NULL); |
| 79 | assert(is_valid_dev_connector(dev_con)); |
| 80 | |
| 81 | result = dev_con->dev_open(dev_spec, dev_info); |
| 82 | return result; |
| 83 | } |
| 84 | |
| 85 | |
| 86 | /* Set a handle to track an entity */ |
Dan Handley | 625de1d | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 87 | static void set_handle(uintptr_t *handle, io_entity_t *entity) |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 88 | { |
| 89 | assert(handle != NULL); |
Dan Handley | 625de1d | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 90 | *handle = (uintptr_t)entity; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | |
| 94 | /* Locate an entity in the pool, specified by address */ |
Dan Handley | 625de1d | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 95 | static int find_first_entity(const io_entity_t *entity, unsigned int *index_out) |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 96 | { |
Juan Castillo | e098e24 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 97 | int result = -ENOENT; |
Etienne Carriere | 5a8e2ae | 2017-06-07 16:43:19 +0200 | [diff] [blame] | 98 | for (unsigned int index = 0; index < MAX_IO_HANDLES; ++index) { |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 99 | if (entity_map[index] == entity) { |
Juan Castillo | e098e24 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 100 | result = 0; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 101 | *index_out = index; |
| 102 | break; |
| 103 | } |
| 104 | } |
| 105 | return result; |
| 106 | } |
| 107 | |
| 108 | |
| 109 | /* Allocate an entity from the pool and return a pointer to it */ |
Dan Handley | 625de1d | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 110 | static int allocate_entity(io_entity_t **entity) |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 111 | { |
Juan Castillo | e098e24 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 112 | int result = -ENOMEM; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 113 | assert(entity != NULL); |
| 114 | |
| 115 | if (entity_count < MAX_IO_HANDLES) { |
| 116 | unsigned int index = 0; |
| 117 | result = find_first_entity(NULL, &index); |
Juan Castillo | e098e24 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 118 | assert(result == 0); |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 119 | *entity = entity_map[index] = &entity_pool[index]; |
| 120 | ++entity_count; |
Juan Castillo | e098e24 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 121 | } |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 122 | |
| 123 | return result; |
| 124 | } |
| 125 | |
| 126 | |
| 127 | /* Release an entity back to the pool */ |
Dan Handley | 625de1d | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 128 | static int free_entity(const io_entity_t *entity) |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 129 | { |
Juan Castillo | e098e24 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 130 | int result; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 131 | unsigned int index = 0; |
| 132 | assert(entity != NULL); |
| 133 | |
| 134 | result = find_first_entity(entity, &index); |
Juan Castillo | e098e24 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 135 | if (result == 0) { |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 136 | entity_map[index] = NULL; |
| 137 | --entity_count; |
| 138 | } |
| 139 | |
| 140 | return result; |
| 141 | } |
| 142 | |
| 143 | |
| 144 | /* Exported API */ |
| 145 | |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 146 | /* Register a device driver */ |
Dan Handley | 625de1d | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 147 | int io_register_device(const io_dev_info_t *dev_info) |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 148 | { |
Juan Castillo | e098e24 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 149 | int result = -ENOMEM; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 150 | assert(dev_info != NULL); |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 151 | |
Dan Handley | 6d16ce0 | 2014-08-04 18:31:43 +0100 | [diff] [blame] | 152 | if (dev_count < MAX_IO_DEVICES) { |
| 153 | devices[dev_count] = dev_info; |
| 154 | dev_count++; |
Juan Castillo | e098e24 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 155 | result = 0; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | return result; |
| 159 | } |
| 160 | |
| 161 | |
| 162 | /* Open a connection to an IO device */ |
Dan Handley | 625de1d | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 163 | int io_dev_open(const io_dev_connector_t *dev_con, const uintptr_t dev_spec, |
| 164 | uintptr_t *handle) |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 165 | { |
Juan Castillo | e098e24 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 166 | int result; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 167 | assert(handle != NULL); |
| 168 | |
Dan Handley | 625de1d | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 169 | result = dev_open(dev_con, dev_spec, (io_dev_info_t **)handle); |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 170 | return result; |
| 171 | } |
| 172 | |
| 173 | |
| 174 | /* Initialise an IO device explicitly - to permit lazy initialisation or |
| 175 | * re-initialisation */ |
Dan Handley | 625de1d | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 176 | int io_dev_init(uintptr_t dev_handle, const uintptr_t init_params) |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 177 | { |
Juan Castillo | e098e24 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 178 | int result = 0; |
Dan Handley | 625de1d | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 179 | assert(dev_handle != (uintptr_t)NULL); |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 180 | assert(is_valid_dev(dev_handle)); |
| 181 | |
Dan Handley | 625de1d | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 182 | io_dev_info_t *dev = (io_dev_info_t *)dev_handle; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 183 | |
Juan Castillo | e098e24 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 184 | /* Absence of registered function implies NOP here */ |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 185 | if (dev->funcs->dev_init != NULL) { |
| 186 | result = dev->funcs->dev_init(dev, init_params); |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 187 | } |
Juan Castillo | e098e24 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 188 | |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 189 | return result; |
| 190 | } |
| 191 | |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 192 | /* Close a connection to a device */ |
Dan Handley | 625de1d | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 193 | int io_dev_close(uintptr_t dev_handle) |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 194 | { |
Juan Castillo | e098e24 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 195 | int result = 0; |
Dan Handley | 625de1d | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 196 | assert(dev_handle != (uintptr_t)NULL); |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 197 | assert(is_valid_dev(dev_handle)); |
| 198 | |
Dan Handley | 625de1d | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 199 | io_dev_info_t *dev = (io_dev_info_t *)dev_handle; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 200 | |
Juan Castillo | e098e24 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 201 | /* Absence of registered function implies NOP here */ |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 202 | if (dev->funcs->dev_close != NULL) { |
| 203 | result = dev->funcs->dev_close(dev); |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | return result; |
| 207 | } |
| 208 | |
| 209 | |
| 210 | /* Synchronous operations */ |
| 211 | |
| 212 | |
| 213 | /* Open an IO entity */ |
Dan Handley | 625de1d | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 214 | int io_open(uintptr_t dev_handle, const uintptr_t spec, uintptr_t *handle) |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 215 | { |
Juan Castillo | e098e24 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 216 | int result; |
Dan Handley | 625de1d | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 217 | assert((spec != (uintptr_t)NULL) && (handle != NULL)); |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 218 | assert(is_valid_dev(dev_handle)); |
| 219 | |
Dan Handley | 625de1d | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 220 | io_dev_info_t *dev = (io_dev_info_t *)dev_handle; |
Dan Handley | fb037bf | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 221 | io_entity_t *entity; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 222 | |
| 223 | result = allocate_entity(&entity); |
| 224 | |
Juan Castillo | e098e24 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 225 | if (result == 0) { |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 226 | assert(dev->funcs->open != NULL); |
| 227 | result = dev->funcs->open(dev, spec, entity); |
| 228 | |
Juan Castillo | e098e24 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 229 | if (result == 0) { |
Dan Handley | 625de1d | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 230 | entity->dev_handle = dev; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 231 | set_handle(handle, entity); |
| 232 | } else |
| 233 | free_entity(entity); |
| 234 | } |
| 235 | return result; |
| 236 | } |
| 237 | |
| 238 | |
| 239 | /* Seek to a specific position in an IO entity */ |
Dan Handley | 625de1d | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 240 | int io_seek(uintptr_t handle, io_seek_mode_t mode, ssize_t offset) |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 241 | { |
Juan Castillo | e098e24 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 242 | int result = -ENODEV; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 243 | assert(is_valid_entity(handle) && is_valid_seek_mode(mode)); |
| 244 | |
Dan Handley | 625de1d | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 245 | io_entity_t *entity = (io_entity_t *)handle; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 246 | |
Dan Handley | fb037bf | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 247 | io_dev_info_t *dev = entity->dev_handle; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 248 | |
| 249 | if (dev->funcs->seek != NULL) |
| 250 | result = dev->funcs->seek(entity, mode, offset); |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 251 | |
| 252 | return result; |
| 253 | } |
| 254 | |
| 255 | |
| 256 | /* Determine the length of an IO entity */ |
Dan Handley | 625de1d | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 257 | int io_size(uintptr_t handle, size_t *length) |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 258 | { |
Juan Castillo | e098e24 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 259 | int result = -ENODEV; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 260 | assert(is_valid_entity(handle) && (length != NULL)); |
| 261 | |
Dan Handley | 625de1d | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 262 | io_entity_t *entity = (io_entity_t *)handle; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 263 | |
Dan Handley | fb037bf | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 264 | io_dev_info_t *dev = entity->dev_handle; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 265 | |
| 266 | if (dev->funcs->size != NULL) |
| 267 | result = dev->funcs->size(entity, length); |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 268 | |
| 269 | return result; |
| 270 | } |
| 271 | |
| 272 | |
| 273 | /* Read data from an IO entity */ |
Dan Handley | 625de1d | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 274 | int io_read(uintptr_t handle, |
| 275 | uintptr_t buffer, |
| 276 | size_t length, |
| 277 | size_t *length_read) |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 278 | { |
Juan Castillo | e098e24 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 279 | int result = -ENODEV; |
Konstantin Porotchkin | 255f5c8 | 2018-05-07 12:52:48 +0300 | [diff] [blame] | 280 | assert(is_valid_entity(handle)); |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 281 | |
Dan Handley | 625de1d | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 282 | io_entity_t *entity = (io_entity_t *)handle; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 283 | |
Dan Handley | fb037bf | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 284 | io_dev_info_t *dev = entity->dev_handle; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 285 | |
| 286 | if (dev->funcs->read != NULL) |
| 287 | result = dev->funcs->read(entity, buffer, length, length_read); |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 288 | |
| 289 | return result; |
| 290 | } |
| 291 | |
| 292 | |
| 293 | /* Write data to an IO entity */ |
Dan Handley | 625de1d | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 294 | int io_write(uintptr_t handle, |
| 295 | const uintptr_t buffer, |
| 296 | size_t length, |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 297 | size_t *length_written) |
| 298 | { |
Juan Castillo | e098e24 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 299 | int result = -ENODEV; |
Konstantin Porotchkin | 255f5c8 | 2018-05-07 12:52:48 +0300 | [diff] [blame] | 300 | assert(is_valid_entity(handle)); |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 301 | |
Dan Handley | 625de1d | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 302 | io_entity_t *entity = (io_entity_t *)handle; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 303 | |
Dan Handley | fb037bf | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 304 | io_dev_info_t *dev = entity->dev_handle; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 305 | |
| 306 | if (dev->funcs->write != NULL) { |
| 307 | result = dev->funcs->write(entity, buffer, length, |
| 308 | length_written); |
Juan Castillo | e098e24 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 309 | } |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 310 | |
| 311 | return result; |
| 312 | } |
| 313 | |
| 314 | |
| 315 | /* Close an IO entity */ |
Dan Handley | 625de1d | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 316 | int io_close(uintptr_t handle) |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 317 | { |
Juan Castillo | e098e24 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 318 | int result = 0; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 319 | assert(is_valid_entity(handle)); |
| 320 | |
Dan Handley | 625de1d | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 321 | io_entity_t *entity = (io_entity_t *)handle; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 322 | |
Dan Handley | fb037bf | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 323 | io_dev_info_t *dev = entity->dev_handle; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 324 | |
Juan Castillo | e098e24 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 325 | /* Absence of registered function implies NOP here */ |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 326 | if (dev->funcs->close != NULL) |
| 327 | result = dev->funcs->close(entity); |
Juan Castillo | e098e24 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 328 | |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 329 | /* Ignore improbable free_entity failure */ |
| 330 | (void)free_entity(entity); |
| 331 | |
| 332 | return result; |
| 333 | } |