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> |
| 34 | #include <stddef.h> |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 35 | |
| 36 | |
| 37 | #define MAX_DEVICES(plat_data) \ |
| 38 | (sizeof((plat_data)->devices)/sizeof((plat_data)->devices[0])) |
| 39 | |
| 40 | |
| 41 | /* Storage for a fixed maximum number of IO entities, definable by platform */ |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 42 | static io_entity_t entity_pool[MAX_IO_HANDLES]; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 43 | |
| 44 | /* Simple way of tracking used storage - each entry is NULL or a pointer to an |
| 45 | * entity */ |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 46 | static io_entity_t *entity_map[MAX_IO_HANDLES]; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 47 | |
| 48 | /* Track number of allocated entities */ |
| 49 | static unsigned int entity_count; |
| 50 | |
| 51 | |
| 52 | /* Used to keep a reference to platform-specific data */ |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 53 | static io_plat_data_t *platform_data; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 54 | |
| 55 | |
| 56 | #if DEBUG /* Extra validation functions only used in debug builds */ |
| 57 | |
| 58 | /* Return a boolean value indicating whether a device connector is valid */ |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 59 | 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] | 60 | { |
| 61 | int result = (dev_con != NULL) && (dev_con->dev_open != NULL); |
| 62 | return result; |
| 63 | } |
| 64 | |
| 65 | |
| 66 | /* Return a boolean value indicating whether a device handle is valid */ |
| 67 | static int is_valid_dev(io_dev_handle handle) |
| 68 | { |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 69 | const io_dev_info_t *dev = handle; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 70 | int result = (dev != NULL) && (dev->funcs != NULL) && |
| 71 | (dev->funcs->type != NULL) && |
| 72 | (dev->funcs->type() < IO_TYPE_MAX); |
| 73 | return result; |
| 74 | } |
| 75 | |
| 76 | |
| 77 | /* Return a boolean value indicating whether an IO entity is valid */ |
| 78 | static int is_valid_entity(io_handle handle) |
| 79 | { |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 80 | const io_entity_t *entity = handle; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 81 | int result = (entity != NULL) && (is_valid_dev(entity->dev_handle)); |
| 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 | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 96 | static int dev_open(const io_dev_connector_t *dev_con, void *dev_spec, |
| 97 | io_dev_info_t **dev_info) |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 98 | { |
| 99 | int result = IO_FAIL; |
| 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 | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 109 | static void set_handle(io_handle *handle, io_entity_t *entity) |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 110 | { |
| 111 | assert(handle != NULL); |
| 112 | *handle = entity; |
| 113 | } |
| 114 | |
| 115 | |
| 116 | /* Locate an entity in the pool, specified by address */ |
| 117 | static int find_first_entity(struct io_entity *entity, unsigned int *index_out) |
| 118 | { |
| 119 | int result = IO_FAIL; |
| 120 | for (int index = 0; index < MAX_IO_HANDLES; ++index) { |
| 121 | if (entity_map[index] == entity) { |
| 122 | result = IO_SUCCESS; |
| 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 */ |
| 132 | static int allocate_entity(struct io_entity **entity) |
| 133 | { |
| 134 | int result = IO_FAIL; |
| 135 | assert(entity != NULL); |
| 136 | |
| 137 | if (entity_count < MAX_IO_HANDLES) { |
| 138 | unsigned int index = 0; |
| 139 | result = find_first_entity(NULL, &index); |
| 140 | assert(result == IO_SUCCESS); |
| 141 | *entity = entity_map[index] = &entity_pool[index]; |
| 142 | ++entity_count; |
| 143 | } else |
| 144 | result = IO_RESOURCES_EXHAUSTED; |
| 145 | |
| 146 | return result; |
| 147 | } |
| 148 | |
| 149 | |
| 150 | /* Release an entity back to the pool */ |
| 151 | static int free_entity(struct io_entity *entity) |
| 152 | { |
| 153 | int result = IO_FAIL; |
| 154 | unsigned int index = 0; |
| 155 | assert(entity != NULL); |
| 156 | |
| 157 | result = find_first_entity(entity, &index); |
| 158 | if (result == IO_SUCCESS) { |
| 159 | entity_map[index] = NULL; |
| 160 | --entity_count; |
| 161 | } |
| 162 | |
| 163 | return result; |
| 164 | } |
| 165 | |
| 166 | |
| 167 | /* Exported API */ |
| 168 | |
| 169 | |
| 170 | /* Initialise the IO layer */ |
| 171 | void io_init(struct io_plat_data *data) |
| 172 | { |
| 173 | assert(data != NULL); |
| 174 | platform_data = data; |
| 175 | } |
| 176 | |
| 177 | |
| 178 | /* Register a device driver */ |
| 179 | int io_register_device(struct io_dev_info *dev_info) |
| 180 | { |
| 181 | int result = IO_FAIL; |
| 182 | assert(dev_info != NULL); |
| 183 | assert(platform_data != NULL); |
| 184 | |
| 185 | unsigned int dev_count = platform_data->dev_count; |
| 186 | |
| 187 | if (dev_count < MAX_DEVICES(platform_data)) { |
| 188 | platform_data->devices[dev_count] = dev_info; |
| 189 | platform_data->dev_count++; |
| 190 | result = IO_SUCCESS; |
| 191 | } else { |
| 192 | result = IO_RESOURCES_EXHAUSTED; |
| 193 | } |
| 194 | |
| 195 | return result; |
| 196 | } |
| 197 | |
| 198 | |
| 199 | /* Open a connection to an IO device */ |
| 200 | int io_dev_open(struct io_dev_connector *dev_con, void *dev_spec, |
| 201 | io_dev_handle *handle) |
| 202 | { |
| 203 | int result = IO_FAIL; |
| 204 | assert(handle != NULL); |
| 205 | |
| 206 | result = dev_open(dev_con, dev_spec, handle); |
| 207 | return result; |
| 208 | } |
| 209 | |
| 210 | |
| 211 | /* Initialise an IO device explicitly - to permit lazy initialisation or |
| 212 | * re-initialisation */ |
| 213 | int io_dev_init(struct io_dev_info *dev_handle, const void *init_params) |
| 214 | { |
| 215 | int result = IO_FAIL; |
| 216 | assert(dev_handle != NULL); |
| 217 | assert(is_valid_dev(dev_handle)); |
| 218 | |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 219 | io_dev_info_t *dev = dev_handle; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 220 | |
| 221 | if (dev->funcs->dev_init != NULL) { |
| 222 | result = dev->funcs->dev_init(dev, init_params); |
| 223 | } else { |
| 224 | /* Absence of registered function implies NOP here */ |
| 225 | result = IO_SUCCESS; |
| 226 | } |
| 227 | return result; |
| 228 | } |
| 229 | |
| 230 | |
| 231 | /* TODO: Consider whether an explicit "shutdown" API should be included */ |
| 232 | |
| 233 | /* Close a connection to a device */ |
| 234 | int io_dev_close(io_dev_handle dev_handle) |
| 235 | { |
| 236 | int result = IO_FAIL; |
| 237 | assert(dev_handle != NULL); |
| 238 | assert(is_valid_dev(dev_handle)); |
| 239 | |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 240 | io_dev_info_t *dev = dev_handle; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 241 | |
| 242 | if (dev->funcs->dev_close != NULL) { |
| 243 | result = dev->funcs->dev_close(dev); |
| 244 | } else { |
| 245 | /* Absence of registered function implies NOP here */ |
| 246 | result = IO_SUCCESS; |
| 247 | } |
| 248 | |
| 249 | return result; |
| 250 | } |
| 251 | |
| 252 | |
| 253 | /* Synchronous operations */ |
| 254 | |
| 255 | |
| 256 | /* Open an IO entity */ |
| 257 | int io_open(io_dev_handle dev_handle, const void *spec, io_handle *handle) |
| 258 | { |
| 259 | int result = IO_FAIL; |
| 260 | assert((spec != NULL) && (handle != NULL)); |
| 261 | assert(is_valid_dev(dev_handle)); |
| 262 | |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 263 | io_dev_info_t *dev = dev_handle; |
| 264 | io_entity_t *entity; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 265 | |
| 266 | result = allocate_entity(&entity); |
| 267 | |
| 268 | if (result == IO_SUCCESS) { |
| 269 | assert(dev->funcs->open != NULL); |
| 270 | result = dev->funcs->open(dev, spec, entity); |
| 271 | |
| 272 | if (result == IO_SUCCESS) { |
| 273 | entity->dev_handle = dev_handle; |
| 274 | set_handle(handle, entity); |
| 275 | } else |
| 276 | free_entity(entity); |
| 277 | } |
| 278 | return result; |
| 279 | } |
| 280 | |
| 281 | |
| 282 | /* Seek to a specific position in an IO entity */ |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 283 | int io_seek(io_handle handle, io_seek_mode_t mode, ssize_t offset) |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 284 | { |
| 285 | int result = IO_FAIL; |
| 286 | assert(is_valid_entity(handle) && is_valid_seek_mode(mode)); |
| 287 | |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 288 | io_entity_t *entity = handle; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 289 | |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 290 | io_dev_info_t *dev = entity->dev_handle; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 291 | |
| 292 | if (dev->funcs->seek != NULL) |
| 293 | result = dev->funcs->seek(entity, mode, offset); |
| 294 | else |
| 295 | result = IO_NOT_SUPPORTED; |
| 296 | |
| 297 | return result; |
| 298 | } |
| 299 | |
| 300 | |
| 301 | /* Determine the length of an IO entity */ |
| 302 | int io_size(io_handle handle, size_t *length) |
| 303 | { |
| 304 | int result = IO_FAIL; |
| 305 | assert(is_valid_entity(handle) && (length != NULL)); |
| 306 | |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 307 | io_entity_t *entity = 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->size != NULL) |
| 312 | result = dev->funcs->size(entity, length); |
| 313 | else |
| 314 | result = IO_NOT_SUPPORTED; |
| 315 | |
| 316 | return result; |
| 317 | } |
| 318 | |
| 319 | |
| 320 | /* Read data from an IO entity */ |
| 321 | int io_read(io_handle handle, void *buffer, size_t length, size_t *length_read) |
| 322 | { |
| 323 | int result = IO_FAIL; |
| 324 | assert(is_valid_entity(handle) && (buffer != NULL)); |
| 325 | |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 326 | io_entity_t *entity = handle; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 327 | |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 328 | io_dev_info_t *dev = entity->dev_handle; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 329 | |
| 330 | if (dev->funcs->read != NULL) |
| 331 | result = dev->funcs->read(entity, buffer, length, length_read); |
| 332 | else |
| 333 | result = IO_NOT_SUPPORTED; |
| 334 | |
| 335 | return result; |
| 336 | } |
| 337 | |
| 338 | |
| 339 | /* Write data to an IO entity */ |
| 340 | int io_write(io_handle handle, const void *buffer, size_t length, |
| 341 | size_t *length_written) |
| 342 | { |
| 343 | int result = IO_FAIL; |
| 344 | assert(is_valid_entity(handle) && (buffer != NULL)); |
| 345 | |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 346 | io_entity_t *entity = 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 | |
| 350 | if (dev->funcs->write != NULL) { |
| 351 | result = dev->funcs->write(entity, buffer, length, |
| 352 | length_written); |
| 353 | } else |
| 354 | result = IO_NOT_SUPPORTED; |
| 355 | |
| 356 | return result; |
| 357 | } |
| 358 | |
| 359 | |
| 360 | /* Close an IO entity */ |
| 361 | int io_close(io_handle handle) |
| 362 | { |
| 363 | int result = IO_FAIL; |
| 364 | assert(is_valid_entity(handle)); |
| 365 | |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 366 | io_entity_t *entity = handle; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 367 | |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 368 | io_dev_info_t *dev = entity->dev_handle; |
James Morrissey | f2f9bb5 | 2014-02-10 16:18:59 +0000 | [diff] [blame] | 369 | |
| 370 | if (dev->funcs->close != NULL) |
| 371 | result = dev->funcs->close(entity); |
| 372 | else { |
| 373 | /* Absence of registered function implies NOP here */ |
| 374 | result = IO_SUCCESS; |
| 375 | } |
| 376 | /* Ignore improbable free_entity failure */ |
| 377 | (void)free_entity(entity); |
| 378 | |
| 379 | return result; |
| 380 | } |