/* fd.c -- Definition of functions and structures for
|
/* fd.c -- Definition of functions and structures for
|
peripheral to communicate with host through file descriptors
|
peripheral to communicate with host through file descriptors
|
|
|
Copyright (C) 2002 Richard Prescott <rip@step.polymtl.ca>
|
Copyright (C) 2002 Richard Prescott <rip@step.polymtl.ca>
|
Copyright (C) 2008 Embecosm Limited
|
Copyright (C) 2008 Embecosm Limited
|
|
|
Contributor Jeremy Bennett <jeremy.bennett@embecosm.com>
|
Contributor Jeremy Bennett <jeremy.bennett@embecosm.com>
|
|
|
This file is part of Or1ksim, the OpenRISC 1000 Architectural Simulator.
|
This file is part of Or1ksim, the OpenRISC 1000 Architectural Simulator.
|
|
|
This program is free software; you can redistribute it and/or modify it
|
This program is free software; you can redistribute it and/or modify it
|
under the terms of the GNU General Public License as published by the Free
|
under the terms of the GNU General Public License as published by the Free
|
Software Foundation; either version 3 of the License, or (at your option)
|
Software Foundation; either version 3 of the License, or (at your option)
|
any later version.
|
any later version.
|
|
|
This program is distributed in the hope that it will be useful, but WITHOUT
|
This program is distributed in the hope that it will be useful, but WITHOUT
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
more details.
|
more details.
|
|
|
You should have received a copy of the GNU General Public License along
|
You should have received a copy of the GNU General Public License along
|
with this program. If not, see <http://www.gnu.org/licenses/>. */
|
with this program. If not, see <http://www.gnu.org/licenses/>. */
|
|
|
/* This program is commented throughout in a fashion suitable for processing
|
/* This program is commented throughout in a fashion suitable for processing
|
with Doxygen. */
|
with Doxygen. */
|
|
|
|
|
/* Autoconf and/or portability configuration */
|
/* Autoconf and/or portability configuration */
|
#include "config.h"
|
#include "config.h"
|
#include "port.h"
|
#include "port.h"
|
|
|
/* System includes */
|
/* System includes */
|
#include <stdlib.h>
|
#include <stdlib.h>
|
#include <unistd.h>
|
#include <unistd.h>
|
#include <errno.h>
|
#include <errno.h>
|
#include <stdio.h>
|
#include <stdio.h>
|
|
|
/* Package includes */
|
/* Package includes */
|
#include "fd.h"
|
#include "fd.h"
|
#include "channel.h"
|
#include "channel.h"
|
#include "generic.h"
|
#include "generic.h"
|
|
|
|
|
/* Forward declarations of static functions */
|
/* Forward declarations of static functions */
|
static void *fd_init (const char *args);
|
static void *fd_init (const char *args);
|
static int fd_isok (void *data);
|
static int fd_isok (void *data);
|
static char *fd_status (void *data);
|
static char *fd_status (void *data);
|
|
|
/*! Global data structure representing the operations for communicating
|
/*! Global data structure representing the operations for communicating
|
through a file descriptor channel */
|
through a file descriptor channel */
|
struct channel_ops fd_channel_ops = {
|
struct channel_ops fd_channel_ops = {
|
.init = fd_init,
|
.init = fd_init,
|
.open = generic_open,
|
.open = generic_open,
|
.close = generic_close,
|
.close = generic_close,
|
.read = fd_read,
|
.read = fd_read,
|
.write = fd_write,
|
.write = fd_write,
|
.free = generic_free,
|
.free = generic_free,
|
.isok = fd_isok,
|
.isok = fd_isok,
|
.status = fd_status,
|
.status = fd_status,
|
};
|
};
|
|
|
|
|
static void *
|
static void *
|
fd_init (const char *args)
|
fd_init (const char *args)
|
{
|
{
|
struct fd_channel *retval;
|
struct fd_channel *retval;
|
|
|
retval = (struct fd_channel *) calloc (1, sizeof (struct fd_channel));
|
retval = (struct fd_channel *) calloc (1, sizeof (struct fd_channel));
|
|
|
if (!retval)
|
if (!retval)
|
{
|
{
|
return NULL;
|
return NULL;
|
}
|
}
|
|
|
|
|
retval->fdin = atoi (args); /* so 0 if garbage */
|
retval->fdin = atoi (args); /* so 0 if garbage */
|
/* TODO: strtoul */
|
/* TODO: strtoul */
|
|
|
args = strchr (args, ',');
|
args = strchr (args, ',');
|
|
|
if (args)
|
if (args)
|
{
|
{
|
retval->fdout = atoi (args + 1);
|
retval->fdout = atoi (args + 1);
|
}
|
}
|
else
|
else
|
{
|
{
|
retval->fdout = retval->fdin;
|
retval->fdout = retval->fdin;
|
}
|
}
|
|
|
return (void *) retval;
|
return (void *) retval;
|
}
|
}
|
|
|
int
|
int
|
fd_read (void *data, char *buffer, int size)
|
fd_read (void *data, char *buffer, int size)
|
{
|
{
|
struct fd_channel *fds = (struct fd_channel *) data;
|
struct fd_channel *fds = (struct fd_channel *) data;
|
struct timeval timeout = { 0, 0 };
|
struct timeval timeout = { 0, 0 };
|
fd_set rfds;
|
fd_set rfds;
|
int retval;
|
int retval;
|
|
|
if (!fds)
|
if (!fds)
|
{
|
{
|
errno = ENODEV;
|
errno = ENODEV;
|
return -1;
|
return -1;
|
}
|
}
|
|
|
FD_ZERO (&rfds);
|
FD_ZERO (&rfds);
|
FD_SET (fds->fdin, &rfds);
|
FD_SET (fds->fdin, &rfds);
|
|
|
retval = select (fds->fdin + 1, &rfds, NULL, NULL, &timeout);
|
retval = select (fds->fdin + 1, &rfds, NULL, NULL, &timeout);
|
|
|
if (retval <= 0)
|
if (retval <= 0)
|
return retval;
|
return retval;
|
|
|
return read (fds->fdin, buffer, size);
|
return read (fds->fdin, buffer, size);
|
}
|
}
|
|
|
int
|
int
|
fd_write (void *data, const char *buffer, int size)
|
fd_write (void *data, const char *buffer, int size)
|
{
|
{
|
struct fd_channel *fds = (struct fd_channel *) data;
|
struct fd_channel *fds = (struct fd_channel *) data;
|
if (fds)
|
if (fds)
|
{
|
{
|
return write (fds->fdout, buffer, size);
|
return write (fds->fdout, buffer, size);
|
}
|
}
|
errno = ENODEV;
|
errno = ENODEV;
|
return -1;
|
return -1;
|
}
|
}
|
|
|
static int
|
static int
|
fd_isok (void *data)
|
fd_isok (void *data)
|
{
|
{
|
struct fd_channel *fds = (struct fd_channel *) data;
|
struct fd_channel *fds = (struct fd_channel *) data;
|
if (fds)
|
if (fds)
|
{
|
{
|
return fds->fdout != -1 && fds->fdin != -1;
|
return fds->fdout != -1 && fds->fdin != -1;
|
}
|
}
|
return 0;
|
return 0;
|
}
|
}
|
|
|
static int
|
static int
|
fd_status_fd (int fd, char *str, int size)
|
fd_status_fd (int fd, char *str, int size)
|
{
|
{
|
if (fd == -1)
|
if (fd == -1)
|
return snprintf (str, size, "closed");
|
return snprintf (str, size, "closed");
|
|
|
return snprintf (str, size, "opened(fd=%d)", fd);
|
return snprintf (str, size, "opened(fd=%d)", fd);
|
}
|
}
|
|
|
static char *
|
static char *
|
fd_status (void *data)
|
fd_status (void *data)
|
{
|
{
|
static char retval[256];
|
static char retval[256];
|
int index = 0;
|
int index = 0;
|
|
|
struct fd_channel *fds = (struct fd_channel *) data;
|
struct fd_channel *fds = (struct fd_channel *) data;
|
if (fds)
|
if (fds)
|
{
|
{
|
index += snprintf (retval + index, sizeof (retval) - index, "in ");
|
index += snprintf (retval + index, sizeof (retval) - index, "in ");
|
index +=
|
index +=
|
fd_status_fd (fds->fdin, retval + index, sizeof (retval) - index);
|
fd_status_fd (fds->fdin, retval + index, sizeof (retval) - index);
|
|
|
index += snprintf (retval + index, sizeof (retval) - index, "out ");
|
index += snprintf (retval + index, sizeof (retval) - index, "out ");
|
index +=
|
index +=
|
fd_status_fd (fds->fdout, retval + index, sizeof (retval) - index);
|
fd_status_fd (fds->fdout, retval + index, sizeof (retval) - index);
|
}
|
}
|
else
|
else
|
{
|
{
|
snprintf (retval, sizeof (retval), "(null)");
|
snprintf (retval, sizeof (retval), "(null)");
|
}
|
}
|
return retval;
|
return retval;
|
}
|
}
|
|
|
|
|