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 | 2bd4ef2 | 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 | e0f9063 | 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 | e2712bc | 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 | e2712bc | 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 | 3aa9216 | 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 | 3aa9216 | 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 | 3759e3f | 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 | e2712bc | 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 | a4cb68e | 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 | a4cb68e | 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 | a4cb68e | 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 | a4cb68e | 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 | e2712bc | 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 | 3759e3f | 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 | a4cb68e | 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 | e2712bc | 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 | 6e76206 | 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 | a4cb68e | 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 | a4cb68e | 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 | a4cb68e | 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 | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 97 | int result = -ENOENT; |
Etienne Carriere | 48708f0 | 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 | 6e76206 | 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 | a4cb68e | 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 | 6e76206 | 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 | 6e76206 | 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 | 6e76206 | 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 | a4cb68e | 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 | 6e76206 | 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 | 6e76206 | 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 | a4cb68e | 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 | 6e76206 | 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 | 3aa9216 | 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 | 6e76206 | 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 | a4cb68e | 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 | 6e76206 | 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 | a4cb68e | 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 | a4cb68e | 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 | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 178 | int result = 0; |
Dan Handley | a4cb68e | 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 | a4cb68e | 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 | 6e76206 | 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 | 6e76206 | 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 | |
| 192 | |
| 193 | /* TODO: Consider whether an explicit "shutdown" API should be included */ |
| 194 | |
| 195 | /* Close a connection to a device */ |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 196 | int io_dev_close(uintptr_t dev_handle) |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 197 | { |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 198 | int result = 0; |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 199 | assert(dev_handle != (uintptr_t)NULL); |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 200 | assert(is_valid_dev(dev_handle)); |
| 201 | |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 202 | io_dev_info_t *dev = (io_dev_info_t *)dev_handle; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 203 | |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 204 | /* Absence of registered function implies NOP here */ |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 205 | if (dev->funcs->dev_close != NULL) { |
| 206 | result = dev->funcs->dev_close(dev); |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | return result; |
| 210 | } |
| 211 | |
| 212 | |
| 213 | /* Synchronous operations */ |
| 214 | |
| 215 | |
| 216 | /* Open an IO entity */ |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 217 | 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] | 218 | { |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 219 | int result; |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 220 | assert((spec != (uintptr_t)NULL) && (handle != NULL)); |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 221 | assert(is_valid_dev(dev_handle)); |
| 222 | |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 223 | io_dev_info_t *dev = (io_dev_info_t *)dev_handle; |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 224 | io_entity_t *entity; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 225 | |
| 226 | result = allocate_entity(&entity); |
| 227 | |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 228 | if (result == 0) { |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 229 | assert(dev->funcs->open != NULL); |
| 230 | result = dev->funcs->open(dev, spec, entity); |
| 231 | |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 232 | if (result == 0) { |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 233 | entity->dev_handle = dev; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 234 | set_handle(handle, entity); |
| 235 | } else |
| 236 | free_entity(entity); |
| 237 | } |
| 238 | return result; |
| 239 | } |
| 240 | |
| 241 | |
| 242 | /* Seek to a specific position in an IO entity */ |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 243 | 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] | 244 | { |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 245 | int result = -ENODEV; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 246 | assert(is_valid_entity(handle) && is_valid_seek_mode(mode)); |
| 247 | |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 248 | io_entity_t *entity = (io_entity_t *)handle; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 249 | |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 250 | io_dev_info_t *dev = entity->dev_handle; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 251 | |
| 252 | if (dev->funcs->seek != NULL) |
| 253 | result = dev->funcs->seek(entity, mode, offset); |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 254 | |
| 255 | return result; |
| 256 | } |
| 257 | |
| 258 | |
| 259 | /* Determine the length of an IO entity */ |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 260 | int io_size(uintptr_t handle, size_t *length) |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 261 | { |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 262 | int result = -ENODEV; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 263 | assert(is_valid_entity(handle) && (length != NULL)); |
| 264 | |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 265 | io_entity_t *entity = (io_entity_t *)handle; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 266 | |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 267 | io_dev_info_t *dev = entity->dev_handle; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 268 | |
| 269 | if (dev->funcs->size != NULL) |
| 270 | result = dev->funcs->size(entity, length); |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 271 | |
| 272 | return result; |
| 273 | } |
| 274 | |
| 275 | |
| 276 | /* Read data from an IO entity */ |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 277 | int io_read(uintptr_t handle, |
| 278 | uintptr_t buffer, |
| 279 | size_t length, |
| 280 | size_t *length_read) |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 281 | { |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 282 | int result = -ENODEV; |
Konstantin Porotchkin | bfd0412 | 2018-05-07 12:52:48 +0300 | [diff] [blame] | 283 | assert(is_valid_entity(handle)); |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 284 | |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 285 | io_entity_t *entity = (io_entity_t *)handle; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 286 | |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 287 | io_dev_info_t *dev = entity->dev_handle; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 288 | |
| 289 | if (dev->funcs->read != NULL) |
| 290 | result = dev->funcs->read(entity, buffer, length, length_read); |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 291 | |
| 292 | return result; |
| 293 | } |
| 294 | |
| 295 | |
| 296 | /* Write data to an IO entity */ |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 297 | int io_write(uintptr_t handle, |
| 298 | const uintptr_t buffer, |
| 299 | size_t length, |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 300 | size_t *length_written) |
| 301 | { |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 302 | int result = -ENODEV; |
Konstantin Porotchkin | bfd0412 | 2018-05-07 12:52:48 +0300 | [diff] [blame] | 303 | assert(is_valid_entity(handle)); |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 304 | |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 305 | io_entity_t *entity = (io_entity_t *)handle; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 306 | |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 307 | io_dev_info_t *dev = entity->dev_handle; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 308 | |
| 309 | if (dev->funcs->write != NULL) { |
| 310 | result = dev->funcs->write(entity, buffer, length, |
| 311 | length_written); |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 312 | } |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 313 | |
| 314 | return result; |
| 315 | } |
| 316 | |
| 317 | |
| 318 | /* Close an IO entity */ |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 319 | int io_close(uintptr_t handle) |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 320 | { |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 321 | int result = 0; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 322 | assert(is_valid_entity(handle)); |
| 323 | |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 324 | io_entity_t *entity = (io_entity_t *)handle; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 325 | |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 326 | io_dev_info_t *dev = entity->dev_handle; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 327 | |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 328 | /* Absence of registered function implies NOP here */ |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 329 | if (dev->funcs->close != NULL) |
| 330 | result = dev->funcs->close(entity); |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 331 | |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 332 | /* Ignore improbable free_entity failure */ |
| 333 | (void)free_entity(entity); |
| 334 | |
| 335 | return result; |
| 336 | } |