| 1 |
786 |
skrzyp |
//==========================================================================
|
| 2 |
|
|
//
|
| 3 |
|
|
// io/iosys.c
|
| 4 |
|
|
//
|
| 5 |
|
|
// I/O Subsystem + Device Table support
|
| 6 |
|
|
//
|
| 7 |
|
|
//==========================================================================
|
| 8 |
|
|
// ####ECOSGPLCOPYRIGHTBEGIN####
|
| 9 |
|
|
// -------------------------------------------
|
| 10 |
|
|
// This file is part of eCos, the Embedded Configurable Operating System.
|
| 11 |
|
|
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
|
| 12 |
|
|
//
|
| 13 |
|
|
// eCos is free software; you can redistribute it and/or modify it under
|
| 14 |
|
|
// the terms of the GNU General Public License as published by the Free
|
| 15 |
|
|
// Software Foundation; either version 2 or (at your option) any later
|
| 16 |
|
|
// version.
|
| 17 |
|
|
//
|
| 18 |
|
|
// eCos is distributed in the hope that it will be useful, but WITHOUT
|
| 19 |
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
| 20 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
| 21 |
|
|
// for more details.
|
| 22 |
|
|
//
|
| 23 |
|
|
// You should have received a copy of the GNU General Public License
|
| 24 |
|
|
// along with eCos; if not, write to the Free Software Foundation, Inc.,
|
| 25 |
|
|
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
| 26 |
|
|
//
|
| 27 |
|
|
// As a special exception, if other files instantiate templates or use
|
| 28 |
|
|
// macros or inline functions from this file, or you compile this file
|
| 29 |
|
|
// and link it with other works to produce a work based on this file,
|
| 30 |
|
|
// this file does not by itself cause the resulting work to be covered by
|
| 31 |
|
|
// the GNU General Public License. However the source code for this file
|
| 32 |
|
|
// must still be made available in accordance with section (3) of the GNU
|
| 33 |
|
|
// General Public License v2.
|
| 34 |
|
|
//
|
| 35 |
|
|
// This exception does not invalidate any other reasons why a work based
|
| 36 |
|
|
// on this file might be covered by the GNU General Public License.
|
| 37 |
|
|
// -------------------------------------------
|
| 38 |
|
|
// ####ECOSGPLCOPYRIGHTEND####
|
| 39 |
|
|
//==========================================================================
|
| 40 |
|
|
//#####DESCRIPTIONBEGIN####
|
| 41 |
|
|
//
|
| 42 |
|
|
// Author(s): gthomas
|
| 43 |
|
|
// Contributors: gthomas
|
| 44 |
|
|
// Date: 1999-02-04
|
| 45 |
|
|
// Purpose: Device I/O Support
|
| 46 |
|
|
// Description:
|
| 47 |
|
|
//
|
| 48 |
|
|
//####DESCRIPTIONEND####
|
| 49 |
|
|
//
|
| 50 |
|
|
//==========================================================================
|
| 51 |
|
|
|
| 52 |
|
|
#include <pkgconf/io.h>
|
| 53 |
|
|
#include <cyg/io/io.h>
|
| 54 |
|
|
#include <cyg/io/devtab.h>
|
| 55 |
|
|
#include <cyg/infra/diag.h>
|
| 56 |
|
|
|
| 57 |
|
|
//extern void cyg_io_init(void) CYGBLD_ATTRIB_CONSTRUCTOR
|
| 58 |
|
|
// CYG_INIT_PRIORITY(CYG_INIT_BEFORE(LIBC));
|
| 59 |
|
|
|
| 60 |
|
|
// Checks that two strings are "equivalent" device names
|
| 61 |
|
|
// 'n1' is a string from the user
|
| 62 |
|
|
// 'n2' is a name in a device table entry
|
| 63 |
|
|
// 'cyg_io_compare()' will return true IFF
|
| 64 |
|
|
// n1 == n2, for all characters
|
| 65 |
|
|
// n2 ends in '/' and matches n1 up to the terminating '/'
|
| 66 |
|
|
// 'ptr' will get a pointer to the residual string.
|
| 67 |
|
|
static bool
|
| 68 |
|
|
cyg_io_compare(const char *n1, const char *n2, const char **ptr)
|
| 69 |
|
|
{
|
| 70 |
|
|
while (*n1 && *n2) {
|
| 71 |
|
|
if (*n1++ != *n2++) {
|
| 72 |
|
|
return false;
|
| 73 |
|
|
}
|
| 74 |
|
|
}
|
| 75 |
|
|
if (*n1) {
|
| 76 |
|
|
// See if the devtab name is is a substring
|
| 77 |
|
|
if (*(n2-1) == '/') {
|
| 78 |
|
|
*ptr = n1;
|
| 79 |
|
|
return true;
|
| 80 |
|
|
}
|
| 81 |
|
|
}
|
| 82 |
|
|
if (*n1 || *n2) {
|
| 83 |
|
|
return false;
|
| 84 |
|
|
}
|
| 85 |
|
|
*ptr = n1;
|
| 86 |
|
|
return true;
|
| 87 |
|
|
}
|
| 88 |
|
|
|
| 89 |
|
|
//
|
| 90 |
|
|
// This function is called during system initialization. The purpose is
|
| 91 |
|
|
// to step through all devices linked into the system, calling their
|
| 92 |
|
|
// "init" entry points.
|
| 93 |
|
|
//
|
| 94 |
|
|
|
| 95 |
|
|
void
|
| 96 |
|
|
cyg_io_init(void)
|
| 97 |
|
|
{
|
| 98 |
|
|
static int _init = false;
|
| 99 |
|
|
cyg_devtab_entry_t *t;
|
| 100 |
|
|
if (_init) return;
|
| 101 |
|
|
for (t = &__DEVTAB__[0]; t != &__DEVTAB_END__; t++) {
|
| 102 |
|
|
#ifdef CYGDBG_IO_INIT
|
| 103 |
|
|
diag_printf("Init device '%s'\n", t->name);
|
| 104 |
|
|
#endif
|
| 105 |
|
|
if (t->init(t)) {
|
| 106 |
|
|
t->status |= CYG_DEVTAB_STATUS_AVAIL;
|
| 107 |
|
|
} else {
|
| 108 |
|
|
// What to do if device init fails?
|
| 109 |
|
|
// Device not [currently] available
|
| 110 |
|
|
t->status &= ~CYG_DEVTAB_STATUS_AVAIL;
|
| 111 |
|
|
}
|
| 112 |
|
|
}
|
| 113 |
|
|
_init = true;
|
| 114 |
|
|
}
|
| 115 |
|
|
|
| 116 |
|
|
//
|
| 117 |
|
|
// Look up the devtab entry for a named device and return its handle.
|
| 118 |
|
|
// If the device is found and it has a "lookup" function, call that
|
| 119 |
|
|
// function to allow the device/driver to perform any necessary
|
| 120 |
|
|
// initializations.
|
| 121 |
|
|
//
|
| 122 |
|
|
|
| 123 |
|
|
Cyg_ErrNo
|
| 124 |
|
|
cyg_io_lookup(const char *name, cyg_io_handle_t *handle)
|
| 125 |
|
|
{
|
| 126 |
|
|
union devtab_entry_handle_union {
|
| 127 |
|
|
cyg_devtab_entry_t *st;
|
| 128 |
|
|
cyg_io_handle_t h;
|
| 129 |
|
|
} stunion;
|
| 130 |
|
|
cyg_devtab_entry_t *t;
|
| 131 |
|
|
Cyg_ErrNo res;
|
| 132 |
|
|
const char *name_ptr;
|
| 133 |
|
|
for (t = &__DEVTAB__[0]; t != &__DEVTAB_END__; t++) {
|
| 134 |
|
|
if (cyg_io_compare(name, t->name, &name_ptr)) {
|
| 135 |
|
|
// FUTURE: Check 'avail'/'online' here
|
| 136 |
|
|
if (t->dep_name) {
|
| 137 |
|
|
res = cyg_io_lookup(t->dep_name, &stunion.h);
|
| 138 |
|
|
if (res != ENOERR) {
|
| 139 |
|
|
return res;
|
| 140 |
|
|
}
|
| 141 |
|
|
} else {
|
| 142 |
|
|
stunion.st = NULL;
|
| 143 |
|
|
}
|
| 144 |
|
|
if (t->lookup) {
|
| 145 |
|
|
// This indirection + the name pointer allows the lookup routine
|
| 146 |
|
|
// to return a different 'devtab' handle. This will provide for
|
| 147 |
|
|
// 'pluggable' devices, file names, etc.
|
| 148 |
|
|
res = (t->lookup)(&t, stunion.st, name_ptr);
|
| 149 |
|
|
if (res != ENOERR) {
|
| 150 |
|
|
return res;
|
| 151 |
|
|
}
|
| 152 |
|
|
}
|
| 153 |
|
|
*handle = (cyg_io_handle_t)t;
|
| 154 |
|
|
return ENOERR;
|
| 155 |
|
|
}
|
| 156 |
|
|
}
|
| 157 |
|
|
return -ENOENT; // Not found
|
| 158 |
|
|
}
|
| 159 |
|
|
|
| 160 |
|
|
//
|
| 161 |
|
|
// 'write' data to a device.
|
| 162 |
|
|
//
|
| 163 |
|
|
|
| 164 |
|
|
Cyg_ErrNo
|
| 165 |
|
|
cyg_io_write(cyg_io_handle_t handle, const void *buf, cyg_uint32 *len)
|
| 166 |
|
|
{
|
| 167 |
|
|
cyg_devtab_entry_t *t = (cyg_devtab_entry_t *)handle;
|
| 168 |
|
|
// Validate request
|
| 169 |
|
|
if (!t->handlers->write) {
|
| 170 |
|
|
return -EDEVNOSUPP;
|
| 171 |
|
|
}
|
| 172 |
|
|
// Special check. If length is zero, this just verifies that the
|
| 173 |
|
|
// 'write' method exists for the given device.
|
| 174 |
|
|
if (NULL != len && 0 == *len) {
|
| 175 |
|
|
return ENOERR;
|
| 176 |
|
|
}
|
| 177 |
|
|
return t->handlers->write(handle, buf, len);
|
| 178 |
|
|
}
|
| 179 |
|
|
|
| 180 |
|
|
//
|
| 181 |
|
|
// 'read' data from a device.
|
| 182 |
|
|
//
|
| 183 |
|
|
|
| 184 |
|
|
Cyg_ErrNo
|
| 185 |
|
|
cyg_io_read(cyg_io_handle_t handle, void *buf, cyg_uint32 *len)
|
| 186 |
|
|
{
|
| 187 |
|
|
cyg_devtab_entry_t *t = (cyg_devtab_entry_t *)handle;
|
| 188 |
|
|
// Validate request
|
| 189 |
|
|
if (!t->handlers->read) {
|
| 190 |
|
|
return -EDEVNOSUPP;
|
| 191 |
|
|
}
|
| 192 |
|
|
// Special check. If length is zero, this just verifies that the
|
| 193 |
|
|
// 'read' method exists for the given device.
|
| 194 |
|
|
if (NULL != len && 0 == *len) {
|
| 195 |
|
|
return ENOERR;
|
| 196 |
|
|
}
|
| 197 |
|
|
return t->handlers->read(handle, buf, len);
|
| 198 |
|
|
}
|
| 199 |
|
|
|
| 200 |
|
|
//
|
| 201 |
|
|
// 'write' blocks to a device. The len and the position are in terms
|
| 202 |
|
|
// of blocks, not bytes like the cyg_io_write.
|
| 203 |
|
|
//
|
| 204 |
|
|
Cyg_ErrNo
|
| 205 |
|
|
cyg_io_bwrite(cyg_io_handle_t handle, const void *buf, cyg_uint32 *len, cyg_uint32 pos)
|
| 206 |
|
|
{
|
| 207 |
|
|
cyg_devtab_entry_t *t = (cyg_devtab_entry_t *)handle;
|
| 208 |
|
|
// Validate request
|
| 209 |
|
|
if (!t->handlers->bwrite) {
|
| 210 |
|
|
return -EDEVNOSUPP;
|
| 211 |
|
|
}
|
| 212 |
|
|
// Special check. If length is zero, this just verifies that the
|
| 213 |
|
|
// 'bwrite' method exists for the given device.
|
| 214 |
|
|
if (NULL != len && 0 == *len) {
|
| 215 |
|
|
return ENOERR;
|
| 216 |
|
|
}
|
| 217 |
|
|
return t->handlers->bwrite(handle, buf, len, pos);
|
| 218 |
|
|
}
|
| 219 |
|
|
|
| 220 |
|
|
//
|
| 221 |
|
|
// 'read' blocks from a device. The len and the position are in terms of
|
| 222 |
|
|
// blocks, not bytes like the cyg_io_read.
|
| 223 |
|
|
//
|
| 224 |
|
|
Cyg_ErrNo
|
| 225 |
|
|
cyg_io_bread(cyg_io_handle_t handle, void *buf, cyg_uint32 *len, cyg_uint32 pos)
|
| 226 |
|
|
{
|
| 227 |
|
|
cyg_devtab_entry_t *t = (cyg_devtab_entry_t *)handle;
|
| 228 |
|
|
// Validate request
|
| 229 |
|
|
if (!t->handlers->bread) {
|
| 230 |
|
|
return -EDEVNOSUPP;
|
| 231 |
|
|
}
|
| 232 |
|
|
// Special check. If length is zero, this just verifies that the
|
| 233 |
|
|
// 'bread' method exists for the given device.
|
| 234 |
|
|
if (NULL != len && 0 == *len) {
|
| 235 |
|
|
return ENOERR;
|
| 236 |
|
|
}
|
| 237 |
|
|
return t->handlers->bread(handle, buf, len, pos);
|
| 238 |
|
|
}
|
| 239 |
|
|
|
| 240 |
|
|
//
|
| 241 |
|
|
// Check device for available input or space for output
|
| 242 |
|
|
//
|
| 243 |
|
|
|
| 244 |
|
|
cyg_bool
|
| 245 |
|
|
cyg_io_select(cyg_io_handle_t handle, cyg_uint32 which, CYG_ADDRWORD info)
|
| 246 |
|
|
{
|
| 247 |
|
|
cyg_devtab_entry_t *t = (cyg_devtab_entry_t *)handle;
|
| 248 |
|
|
// Validate request
|
| 249 |
|
|
if (!t->handlers->select) {
|
| 250 |
|
|
return -EDEVNOSUPP;
|
| 251 |
|
|
}
|
| 252 |
|
|
|
| 253 |
|
|
return t->handlers->select( handle, which, info );
|
| 254 |
|
|
}
|
| 255 |
|
|
|
| 256 |
|
|
//
|
| 257 |
|
|
// Get the configuration of a device.
|
| 258 |
|
|
//
|
| 259 |
|
|
|
| 260 |
|
|
Cyg_ErrNo
|
| 261 |
|
|
cyg_io_get_config(cyg_io_handle_t handle, cyg_uint32 key, void *buf, cyg_uint32 *len)
|
| 262 |
|
|
{
|
| 263 |
|
|
cyg_devtab_entry_t *t = (cyg_devtab_entry_t *)handle;
|
| 264 |
|
|
// Validate request
|
| 265 |
|
|
if (!t->handlers->get_config) {
|
| 266 |
|
|
return -EDEVNOSUPP;
|
| 267 |
|
|
}
|
| 268 |
|
|
// Special check. If length is zero, this just verifies that the
|
| 269 |
|
|
// 'get_config' method exists for the given device.
|
| 270 |
|
|
if (NULL != len && 0 == *len) {
|
| 271 |
|
|
return ENOERR;
|
| 272 |
|
|
}
|
| 273 |
|
|
return t->handlers->get_config(handle, key, buf, len);
|
| 274 |
|
|
}
|
| 275 |
|
|
|
| 276 |
|
|
//
|
| 277 |
|
|
// Change the configuration of a device.
|
| 278 |
|
|
//
|
| 279 |
|
|
|
| 280 |
|
|
Cyg_ErrNo
|
| 281 |
|
|
cyg_io_set_config(cyg_io_handle_t handle, cyg_uint32 key, const void *buf, cyg_uint32 *len)
|
| 282 |
|
|
{
|
| 283 |
|
|
cyg_devtab_entry_t *t = (cyg_devtab_entry_t *)handle;
|
| 284 |
|
|
// Validate request
|
| 285 |
|
|
if (!t->handlers->set_config) {
|
| 286 |
|
|
return -EDEVNOSUPP;
|
| 287 |
|
|
}
|
| 288 |
|
|
// Special check. If length is zero, this just verifies that the
|
| 289 |
|
|
// 'set_config' method exists for the given device.
|
| 290 |
|
|
if (NULL != len && 0 == *len) {
|
| 291 |
|
|
return ENOERR;
|
| 292 |
|
|
}
|
| 293 |
|
|
return t->handlers->set_config(handle, key, buf, len);
|
| 294 |
|
|
}
|
| 295 |
|
|
|
| 296 |
|
|
/*---------------------------------------------------------------------------*/
|
| 297 |
|
|
// Default functions for devio tables
|
| 298 |
|
|
|
| 299 |
|
|
Cyg_ErrNo cyg_devio_cwrite(cyg_io_handle_t handle, const void *buf, cyg_uint32 *len)
|
| 300 |
|
|
{
|
| 301 |
|
|
return -EDEVNOSUPP;
|
| 302 |
|
|
}
|
| 303 |
|
|
|
| 304 |
|
|
Cyg_ErrNo cyg_devio_cread(cyg_io_handle_t handle, void *buf, cyg_uint32 *len)
|
| 305 |
|
|
{
|
| 306 |
|
|
return -EDEVNOSUPP;
|
| 307 |
|
|
}
|
| 308 |
|
|
|
| 309 |
|
|
Cyg_ErrNo cyg_devio_bwrite(cyg_io_handle_t handle, const void *buf,
|
| 310 |
|
|
cyg_uint32 *len, cyg_uint32 pos)
|
| 311 |
|
|
{
|
| 312 |
|
|
return -EDEVNOSUPP;
|
| 313 |
|
|
}
|
| 314 |
|
|
|
| 315 |
|
|
Cyg_ErrNo cyg_devio_bread(cyg_io_handle_t handle, void *buf,
|
| 316 |
|
|
cyg_uint32 *len, cyg_uint32 pos)
|
| 317 |
|
|
{
|
| 318 |
|
|
return -EDEVNOSUPP;
|
| 319 |
|
|
}
|
| 320 |
|
|
|
| 321 |
|
|
Cyg_ErrNo
|
| 322 |
|
|
cyg_devio_select(cyg_io_handle_t handle, cyg_uint32 which, CYG_ADDRWORD info)
|
| 323 |
|
|
{
|
| 324 |
|
|
CYG_UNUSED_PARAM(cyg_io_handle_t, handle);
|
| 325 |
|
|
CYG_UNUSED_PARAM(cyg_uint32, which);
|
| 326 |
|
|
CYG_UNUSED_PARAM(CYG_ADDRWORD, info);
|
| 327 |
|
|
return -EDEVNOSUPP;
|
| 328 |
|
|
}
|
| 329 |
|
|
|
| 330 |
|
|
Cyg_ErrNo
|
| 331 |
|
|
cyg_devio_get_config(cyg_io_handle_t handle, cyg_uint32 key,
|
| 332 |
|
|
void* buf, cyg_uint32* len)
|
| 333 |
|
|
{
|
| 334 |
|
|
CYG_UNUSED_PARAM(cyg_io_handle_t, handle);
|
| 335 |
|
|
CYG_UNUSED_PARAM(cyg_uint32, key);
|
| 336 |
|
|
CYG_UNUSED_PARAM(void*, buf);
|
| 337 |
|
|
CYG_UNUSED_PARAM(cyg_uint32*, len);
|
| 338 |
|
|
return -EDEVNOSUPP;
|
| 339 |
|
|
}
|
| 340 |
|
|
|
| 341 |
|
|
Cyg_ErrNo
|
| 342 |
|
|
cyg_devio_set_config(cyg_io_handle_t handle, cyg_uint32 key,
|
| 343 |
|
|
void* buf, cyg_uint32* len)
|
| 344 |
|
|
{
|
| 345 |
|
|
CYG_UNUSED_PARAM(cyg_io_handle_t, handle);
|
| 346 |
|
|
CYG_UNUSED_PARAM(cyg_uint32, key);
|
| 347 |
|
|
CYG_UNUSED_PARAM(void*, buf);
|
| 348 |
|
|
CYG_UNUSED_PARAM(cyg_uint32*, len);
|
| 349 |
|
|
return -EDEVNOSUPP;
|
| 350 |
|
|
}
|
| 351 |
|
|
|
| 352 |
|
|
/*---------------------------------------------------------------------------*/
|
| 353 |
|
|
/* End of io/iosys.c */
|