OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [or1ksim/] [peripheral/] [channels/] [fd.c] - Blame information for rev 201

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 19 jeremybenn
/* fd.c -- Definition of functions and structures for
2
   peripheral to communicate with host through file descriptors
3
 
4
   Copyright (C) 2002 Richard Prescott <rip@step.polymtl.ca>
5
   Copyright (C) 2008 Embecosm Limited
6
 
7
   Contributor Jeremy Bennett <jeremy.bennett@embecosm.com>
8
 
9
   This file is part of Or1ksim, the OpenRISC 1000 Architectural Simulator.
10
 
11
   This program is free software; you can redistribute it and/or modify it
12
   under the terms of the GNU General Public License as published by the Free
13
   Software Foundation; either version 3 of the License, or (at your option)
14
   any later version.
15
 
16
   This program is distributed in the hope that it will be useful, but WITHOUT
17
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18
   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
19
   more details.
20
 
21
   You should have received a copy of the GNU General Public License along
22
   with this program.  If not, see <http://www.gnu.org/licenses/>.  */
23
 
24
/* This program is commented throughout in a fashion suitable for processing
25
   with Doxygen. */
26
 
27
 
28
/* Autoconf and/or portability configuration */
29
#include "config.h"
30
#include "port.h"
31
 
32
/* System includes */
33
#include <stdlib.h>
34
#include <sys/types.h>
35
#include <sys/time.h>
36
#include <unistd.h>
37
#include <errno.h>
38
#include <stdio.h>
39
 
40
/* Package includes */
41
#include "fd.h"
42
#include "channel.h"
43
#include "generic.h"
44
 
45
 
46
/* Forward declarations of static functions */
47
static void *fd_init (const char *args);
48
static int   fd_isok (void *data);
49
static char *fd_status (void *data);
50
 
51
/*! Global data structure representing the operations for communicating
52
  through a file descriptor channel */
53
struct channel_ops  fd_channel_ops = {
54
        .init   = fd_init,
55
        .open   = generic_open,
56
        .close  = generic_close,
57
        .read   = fd_read,
58
        .write  = fd_write,
59
        .free   = generic_free,
60
        .isok   = fd_isok,
61
        .status = fd_status,
62
};
63
 
64
 
65
static void *
66
fd_init (const char *args)
67
{
68
  struct fd_channel *retval;
69
 
70
  retval = (struct fd_channel *) calloc (1, sizeof (struct fd_channel));
71
 
72
  if (!retval)
73
    {
74
      return NULL;
75
    }
76
 
77
 
78
  retval->fdin = atoi (args);   /* so 0 if garbage */
79
  /* TODO: strtoul */
80
 
81
  args = strchr (args, ',');
82
 
83
  if (args)
84
    {
85
      retval->fdout = atoi (args + 1);
86
    }
87
  else
88
    {
89
      retval->fdout = retval->fdin;
90
    }
91
 
92
  return (void *) retval;
93
}
94
 
95
int
96
fd_read (void *data, char *buffer, int size)
97
{
98
  struct fd_channel *fds = (struct fd_channel *) data;
99
  struct timeval timeout = { 0, 0 };
100
  fd_set rfds;
101
  int retval;
102
 
103
  if (!fds)
104
    {
105
      errno = ENODEV;
106
      return -1;
107
    }
108
 
109
  /* Loop, to allow for select being interrupted */
110
  do
111
    {
112
      FD_ZERO (&rfds);
113
      FD_SET (fds->fdin, &rfds);
114
 
115
      retval = select (fds->fdin + 1, &rfds, NULL, NULL, &timeout);
116
    }
117
  while ((retval < 0) && (EINTR == errno));
118
 
119
  if (retval <= 0)
120
    return retval;
121
 
122
  /* Loop, to allow for select being interrupted */
123
  do
124
    {
125
      retval = read (fds->fdin, buffer, size);
126
    }
127
  while ((retval < 0) && (EINTR == errno));
128
 
129
  return retval;
130
}
131
 
132
int
133
fd_write (void *data, const char *buffer, int size)
134
{
135
  struct fd_channel *fds = (struct fd_channel *) data;
136
  if (fds)
137
    {
138
      return write (fds->fdout, buffer, size);
139
    }
140
  errno = ENODEV;
141
  return -1;
142
}
143
 
144
static int
145
fd_isok (void *data)
146
{
147
  struct fd_channel *fds = (struct fd_channel *) data;
148
  if (fds)
149
    {
150
      return fds->fdout != -1 && fds->fdin != -1;
151
    }
152
  return 0;
153
}
154
 
155
static int
156
fd_status_fd (int fd, char *str, int size)
157
{
158
  if (fd == -1)
159
    return snprintf (str, size, "closed");
160
 
161
  return snprintf (str, size, "opened(fd=%d)", fd);
162
}
163
 
164
static char *
165
fd_status (void *data)
166
{
167
  static char retval[256];
168
  int index = 0;
169
 
170
  struct fd_channel *fds = (struct fd_channel *) data;
171
  if (fds)
172
    {
173
      index += snprintf (retval + index, sizeof (retval) - index, "in ");
174
      index +=
175
        fd_status_fd (fds->fdin, retval + index, sizeof (retval) - index);
176
 
177
      index += snprintf (retval + index, sizeof (retval) - index, "out ");
178
      index +=
179
        fd_status_fd (fds->fdout, retval + index, sizeof (retval) - index);
180
    }
181
  else
182
    {
183
      snprintf (retval, sizeof (retval), "(null)");
184
    }
185
  return retval;
186
}
187
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.