James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014, ARM Limited and Contributors. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are met: |
| 6 | * |
| 7 | * Redistributions of source code must retain the above copyright notice, this |
| 8 | * list of conditions and the following disclaimer. |
| 9 | * |
| 10 | * Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * |
| 14 | * Neither the name of ARM nor the names of its contributors may be used |
| 15 | * to endorse or promote products derived from this software without specific |
| 16 | * prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 | * POSSIBILITY OF SUCH DAMAGE. |
| 29 | */ |
| 30 | |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 31 | #include <assert.h> |
Dan Handley | 714a0d2 | 2014-04-09 13:13:04 +0100 | [diff] [blame] | 32 | #include <io_driver.h> |
Dan Handley | 2bd4ef2 | 2014-04-09 13:14:54 +0100 | [diff] [blame] | 33 | #include <io_storage.h> |
Dan Handley | 3aa9216 | 2014-08-04 18:31:43 +0100 | [diff] [blame] | 34 | #include <platform_def.h> |
Dan Handley | 2bd4ef2 | 2014-04-09 13:14:54 +0100 | [diff] [blame] | 35 | #include <stddef.h> |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 36 | |
| 37 | |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 38 | /* Storage for a fixed maximum number of IO entities, definable by platform */ |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 39 | static io_entity_t entity_pool[MAX_IO_HANDLES]; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 40 | |
| 41 | /* Simple way of tracking used storage - each entry is NULL or a pointer to an |
| 42 | * entity */ |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 43 | static io_entity_t *entity_map[MAX_IO_HANDLES]; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 44 | |
| 45 | /* Track number of allocated entities */ |
| 46 | static unsigned int entity_count; |
| 47 | |
Dan Handley | 3aa9216 | 2014-08-04 18:31:43 +0100 | [diff] [blame] | 48 | /* Array of fixed maximum of registered devices, definable by platform */ |
| 49 | static const io_dev_info_t *devices[MAX_IO_DEVICES]; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 50 | |
Dan Handley | 3aa9216 | 2014-08-04 18:31:43 +0100 | [diff] [blame] | 51 | /* Number of currently registered devices */ |
| 52 | static unsigned int dev_count; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 53 | |
| 54 | |
| 55 | #if DEBUG /* Extra validation functions only used in debug builds */ |
| 56 | |
| 57 | /* Return a boolean value indicating whether a device connector is valid */ |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 58 | 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] | 59 | { |
| 60 | int result = (dev_con != NULL) && (dev_con->dev_open != NULL); |
| 61 | return result; |
| 62 | } |
| 63 | |
| 64 | |
| 65 | /* Return a boolean value indicating whether a device handle is valid */ |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 66 | static int is_valid_dev(const uintptr_t dev_handle) |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 67 | { |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 68 | const io_dev_info_t *dev = (io_dev_info_t *)dev_handle; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 69 | int result = (dev != NULL) && (dev->funcs != NULL) && |
| 70 | (dev->funcs->type != NULL) && |
| 71 | (dev->funcs->type() < IO_TYPE_MAX); |
| 72 | return result; |
| 73 | } |
| 74 | |
| 75 | |
| 76 | /* Return a boolean value indicating whether an IO entity is valid */ |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 77 | static int is_valid_entity(const uintptr_t handle) |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 78 | { |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 79 | const io_entity_t *entity = (io_entity_t *)handle; |
| 80 | int result = (entity != NULL) && |
| 81 | (is_valid_dev((uintptr_t)entity->dev_handle)); |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 82 | return result; |
| 83 | } |
| 84 | |
| 85 | |
| 86 | /* Return a boolean value indicating whether a seek mode is valid */ |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 87 | static int is_valid_seek_mode(io_seek_mode_t mode) |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 88 | { |
| 89 | return ((mode != IO_SEEK_INVALID) && (mode < IO_SEEK_MAX)); |
| 90 | } |
| 91 | |
| 92 | #endif /* End of debug-only validation functions */ |
| 93 | |
| 94 | |
| 95 | /* Open a connection to a specific device */ |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 96 | static int dev_open(const io_dev_connector_t *dev_con, const uintptr_t dev_spec, |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 97 | io_dev_info_t **dev_info) |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 98 | { |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 99 | int result; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 100 | assert(dev_info != NULL); |
| 101 | assert(is_valid_dev_connector(dev_con)); |
| 102 | |
| 103 | result = dev_con->dev_open(dev_spec, dev_info); |
| 104 | return result; |
| 105 | } |
| 106 | |
| 107 | |
| 108 | /* Set a handle to track an entity */ |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 109 | static void set_handle(uintptr_t *handle, io_entity_t *entity) |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 110 | { |
| 111 | assert(handle != NULL); |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 112 | *handle = (uintptr_t)entity; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | |
| 116 | /* Locate an entity in the pool, specified by address */ |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 117 | 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] | 118 | { |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 119 | int result = -ENOENT; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 120 | for (int index = 0; index < MAX_IO_HANDLES; ++index) { |
| 121 | if (entity_map[index] == entity) { |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 122 | result = 0; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 123 | *index_out = index; |
| 124 | break; |
| 125 | } |
| 126 | } |
| 127 | return result; |
| 128 | } |
| 129 | |
| 130 | |
| 131 | /* Allocate an entity from the pool and return a pointer to it */ |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 132 | static int allocate_entity(io_entity_t **entity) |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 133 | { |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 134 | int result = -ENOMEM; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 135 | assert(entity != NULL); |
| 136 | |
| 137 | if (entity_count < MAX_IO_HANDLES) { |
| 138 | unsigned int index = 0; |
| 139 | result = find_first_entity(NULL, &index); |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 140 | assert(result == 0); |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 141 | *entity = entity_map[index] = &entity_pool[index]; |
| 142 | ++entity_count; |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 143 | } |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 144 | |
| 145 | return result; |
| 146 | } |
| 147 | |
| 148 | |
| 149 | /* Release an entity back to the pool */ |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 150 | static int free_entity(const io_entity_t *entity) |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 151 | { |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 152 | int result; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 153 | unsigned int index = 0; |
| 154 | assert(entity != NULL); |
| 155 | |
| 156 | result = find_first_entity(entity, &index); |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 157 | if (result == 0) { |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 158 | entity_map[index] = NULL; |
| 159 | --entity_count; |
| 160 | } |
| 161 | |
| 162 | return result; |
| 163 | } |
| 164 | |
| 165 | |
| 166 | /* Exported API */ |
| 167 | |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 168 | /* Register a device driver */ |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 169 | int io_register_device(const io_dev_info_t *dev_info) |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 170 | { |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 171 | int result = -ENOMEM; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 172 | assert(dev_info != NULL); |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 173 | |
Dan Handley | 3aa9216 | 2014-08-04 18:31:43 +0100 | [diff] [blame] | 174 | if (dev_count < MAX_IO_DEVICES) { |
| 175 | devices[dev_count] = dev_info; |
| 176 | dev_count++; |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 177 | result = 0; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | return result; |
| 181 | } |
| 182 | |
| 183 | |
| 184 | /* Open a connection to an IO device */ |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 185 | int io_dev_open(const io_dev_connector_t *dev_con, const uintptr_t dev_spec, |
| 186 | uintptr_t *handle) |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 187 | { |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 188 | int result; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 189 | assert(handle != NULL); |
| 190 | |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 191 | result = dev_open(dev_con, dev_spec, (io_dev_info_t **)handle); |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 192 | return result; |
| 193 | } |
| 194 | |
| 195 | |
| 196 | /* Initialise an IO device explicitly - to permit lazy initialisation or |
| 197 | * re-initialisation */ |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 198 | 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] | 199 | { |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 200 | int result = 0; |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 201 | assert(dev_handle != (uintptr_t)NULL); |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 202 | assert(is_valid_dev(dev_handle)); |
| 203 | |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 204 | io_dev_info_t *dev = (io_dev_info_t *)dev_handle; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 205 | |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 206 | /* Absence of registered function implies NOP here */ |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 207 | if (dev->funcs->dev_init != NULL) { |
| 208 | result = dev->funcs->dev_init(dev, init_params); |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 209 | } |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 210 | |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 211 | return result; |
| 212 | } |
| 213 | |
| 214 | |
| 215 | /* TODO: Consider whether an explicit "shutdown" API should be included */ |
| 216 | |
| 217 | /* Close a connection to a device */ |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 218 | int io_dev_close(uintptr_t dev_handle) |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 219 | { |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 220 | int result = 0; |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 221 | assert(dev_handle != (uintptr_t)NULL); |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 222 | assert(is_valid_dev(dev_handle)); |
| 223 | |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 224 | io_dev_info_t *dev = (io_dev_info_t *)dev_handle; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 225 | |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 226 | /* Absence of registered function implies NOP here */ |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 227 | if (dev->funcs->dev_close != NULL) { |
| 228 | result = dev->funcs->dev_close(dev); |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | return result; |
| 232 | } |
| 233 | |
| 234 | |
| 235 | /* Synchronous operations */ |
| 236 | |
| 237 | |
| 238 | /* Open an IO entity */ |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 239 | 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] | 240 | { |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 241 | int result; |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 242 | assert((spec != (uintptr_t)NULL) && (handle != NULL)); |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 243 | assert(is_valid_dev(dev_handle)); |
| 244 | |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 245 | io_dev_info_t *dev = (io_dev_info_t *)dev_handle; |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 246 | io_entity_t *entity; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 247 | |
| 248 | result = allocate_entity(&entity); |
| 249 | |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 250 | if (result == 0) { |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 251 | assert(dev->funcs->open != NULL); |
| 252 | result = dev->funcs->open(dev, spec, entity); |
| 253 | |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 254 | if (result == 0) { |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 255 | entity->dev_handle = dev; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 256 | set_handle(handle, entity); |
| 257 | } else |
| 258 | free_entity(entity); |
| 259 | } |
| 260 | return result; |
| 261 | } |
| 262 | |
| 263 | |
| 264 | /* Seek to a specific position in an IO entity */ |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 265 | 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] | 266 | { |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 267 | int result = -ENODEV; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 268 | assert(is_valid_entity(handle) && is_valid_seek_mode(mode)); |
| 269 | |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 270 | io_entity_t *entity = (io_entity_t *)handle; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 271 | |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 272 | io_dev_info_t *dev = entity->dev_handle; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 273 | |
| 274 | if (dev->funcs->seek != NULL) |
| 275 | result = dev->funcs->seek(entity, mode, offset); |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 276 | |
| 277 | return result; |
| 278 | } |
| 279 | |
| 280 | |
| 281 | /* Determine the length of an IO entity */ |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 282 | int io_size(uintptr_t handle, size_t *length) |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 283 | { |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 284 | int result = -ENODEV; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 285 | assert(is_valid_entity(handle) && (length != NULL)); |
| 286 | |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 287 | io_entity_t *entity = (io_entity_t *)handle; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 288 | |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 289 | io_dev_info_t *dev = entity->dev_handle; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 290 | |
| 291 | if (dev->funcs->size != NULL) |
| 292 | result = dev->funcs->size(entity, length); |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 293 | |
| 294 | return result; |
| 295 | } |
| 296 | |
| 297 | |
| 298 | /* Read data from an IO entity */ |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 299 | int io_read(uintptr_t handle, |
| 300 | uintptr_t buffer, |
| 301 | size_t length, |
| 302 | size_t *length_read) |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 303 | { |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 304 | int result = -ENODEV; |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 305 | assert(is_valid_entity(handle) && (buffer != (uintptr_t)NULL)); |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 306 | |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 307 | io_entity_t *entity = (io_entity_t *)handle; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 308 | |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 309 | io_dev_info_t *dev = entity->dev_handle; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 310 | |
| 311 | if (dev->funcs->read != NULL) |
| 312 | result = dev->funcs->read(entity, buffer, length, length_read); |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 313 | |
| 314 | return result; |
| 315 | } |
| 316 | |
| 317 | |
| 318 | /* Write data to an IO entity */ |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 319 | int io_write(uintptr_t handle, |
| 320 | const uintptr_t buffer, |
| 321 | size_t length, |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 322 | size_t *length_written) |
| 323 | { |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 324 | int result = -ENODEV; |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 325 | assert(is_valid_entity(handle) && (buffer != (uintptr_t)NULL)); |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 326 | |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 327 | io_entity_t *entity = (io_entity_t *)handle; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 328 | |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 329 | io_dev_info_t *dev = entity->dev_handle; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 330 | |
| 331 | if (dev->funcs->write != NULL) { |
| 332 | result = dev->funcs->write(entity, buffer, length, |
| 333 | length_written); |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 334 | } |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 335 | |
| 336 | return result; |
| 337 | } |
| 338 | |
| 339 | |
| 340 | /* Close an IO entity */ |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 341 | int io_close(uintptr_t handle) |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 342 | { |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 343 | int result = 0; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 344 | assert(is_valid_entity(handle)); |
| 345 | |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 346 | io_entity_t *entity = (io_entity_t *)handle; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 347 | |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 348 | io_dev_info_t *dev = entity->dev_handle; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 349 | |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 350 | /* Absence of registered function implies NOP here */ |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 351 | if (dev->funcs->close != NULL) |
| 352 | result = dev->funcs->close(entity); |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 353 | |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 354 | /* Ignore improbable free_entity failure */ |
| 355 | (void)free_entity(entity); |
| 356 | |
| 357 | return result; |
| 358 | } |