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

Subversion Repositories or1k

[/] [or1k/] [tags/] [rel-0-3-0-rc3/] [or1ksim/] [peripheral/] [channels/] [fd.c] - Blame information for rev 1758

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

Line No. Rev Author Line
1 1070 rprescott
/* 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 1748 jeremybenn
   Copyright (C) 2008 Embecosm Limited
6 1070 rprescott
 
7 1748 jeremybenn
   Contributor Jeremy Bennett <jeremy.bennett@embecosm.com>
8 1070 rprescott
 
9 1748 jeremybenn
   This file is part of Or1ksim, the OpenRISC 1000 Architectural Simulator.
10 1070 rprescott
 
11 1748 jeremybenn
   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 1070 rprescott
 
16 1748 jeremybenn
   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 1070 rprescott
 
21 1748 jeremybenn
   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 1244 hpanther
 
24 1748 jeremybenn
/* This program is commented throughout in a fashion suitable for processing
25
   with Doxygen. */
26 1070 rprescott
 
27 1748 jeremybenn
 
28
/* Autoconf and/or portability configuration */
29
#include "config.h"
30
#include "port.h"
31
 
32
/* System includes */
33
#include <stdlib.h>
34 1751 jeremybenn
#include <sys/types.h>
35
#include <sys/time.h>
36 1748 jeremybenn
#include <unistd.h>
37
#include <errno.h>
38
#include <stdio.h>
39
 
40
/* Package includes */
41
#include "fd.h"
42 1070 rprescott
#include "channel.h"
43
#include "generic.h"
44
 
45 1748 jeremybenn
 
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 1070 rprescott
{
68 1748 jeremybenn
  struct fd_channel *retval;
69 1070 rprescott
 
70 1748 jeremybenn
  retval = (struct fd_channel *) calloc (1, sizeof (struct fd_channel));
71 1070 rprescott
 
72 1748 jeremybenn
  if (!retval)
73
    {
74
      return NULL;
75
    }
76 1070 rprescott
 
77
 
78 1748 jeremybenn
  retval->fdin = atoi (args);   /* so 0 if garbage */
79
  /* TODO: strtoul */
80 1070 rprescott
 
81 1748 jeremybenn
  args = strchr (args, ',');
82 1070 rprescott
 
83 1748 jeremybenn
  if (args)
84
    {
85
      retval->fdout = atoi (args + 1);
86
    }
87
  else
88
    {
89
      retval->fdout = retval->fdin;
90
    }
91 1070 rprescott
 
92 1748 jeremybenn
  return (void *) retval;
93 1070 rprescott
}
94
 
95 1748 jeremybenn
int
96
fd_read (void *data, char *buffer, int size)
97 1070 rprescott
{
98 1748 jeremybenn
  struct fd_channel *fds = (struct fd_channel *) data;
99
  struct timeval timeout = { 0, 0 };
100
  fd_set rfds;
101
  int retval;
102 1070 rprescott
 
103 1748 jeremybenn
  if (!fds)
104
    {
105
      errno = ENODEV;
106
      return -1;
107
    }
108 1070 rprescott
 
109 1756 jeremybenn
  /* Loop, to allow for select being interrupted */
110
  do
111
    {
112
      FD_ZERO (&rfds);
113
      FD_SET (fds->fdin, &rfds);
114 1070 rprescott
 
115 1756 jeremybenn
      retval = select (fds->fdin + 1, &rfds, NULL, NULL, &timeout);
116
    }
117
  while ((retval < 0) && (EINTR == errno));
118 1070 rprescott
 
119 1748 jeremybenn
  if (retval <= 0)
120
    return retval;
121 1070 rprescott
 
122 1756 jeremybenn
  /* 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 1070 rprescott
}
131
 
132 1748 jeremybenn
int
133
fd_write (void *data, const char *buffer, int size)
134 1070 rprescott
{
135 1748 jeremybenn
  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 1070 rprescott
}
143
 
144 1748 jeremybenn
static int
145
fd_isok (void *data)
146 1070 rprescott
{
147 1748 jeremybenn
  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 1070 rprescott
}
154
 
155 1748 jeremybenn
static int
156
fd_status_fd (int fd, char *str, int size)
157 1070 rprescott
{
158 1748 jeremybenn
  if (fd == -1)
159
    return snprintf (str, size, "closed");
160 1070 rprescott
 
161 1748 jeremybenn
  return snprintf (str, size, "opened(fd=%d)", fd);
162 1070 rprescott
}
163
 
164 1748 jeremybenn
static char *
165
fd_status (void *data)
166 1070 rprescott
{
167 1748 jeremybenn
  static char retval[256];
168
  int index = 0;
169 1070 rprescott
 
170 1748 jeremybenn
  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 1070 rprescott
 
177 1748 jeremybenn
      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 1070 rprescott
}
187
 

powered by: WebSVN 2.1.0

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