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