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

Subversion Repositories or1k

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

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
#include <unistd.h>
35
#include <errno.h>
36
#include <stdio.h>
37
 
38
/* Package includes */
39
#include "fd.h"
40 1070 rprescott
#include "channel.h"
41
#include "generic.h"
42
 
43 1748 jeremybenn
 
44
/* Forward declarations of static functions */
45
static void *fd_init (const char *args);
46
static int   fd_isok (void *data);
47
static char *fd_status (void *data);
48
 
49
/*! Global data structure representing the operations for communicating
50
  through a file descriptor channel */
51
struct channel_ops  fd_channel_ops = {
52
        .init   = fd_init,
53
        .open   = generic_open,
54
        .close  = generic_close,
55
        .read   = fd_read,
56
        .write  = fd_write,
57
        .free   = generic_free,
58
        .isok   = fd_isok,
59
        .status = fd_status,
60
};
61
 
62
 
63
static void *
64
fd_init (const char *args)
65 1070 rprescott
{
66 1748 jeremybenn
  struct fd_channel *retval;
67 1070 rprescott
 
68 1748 jeremybenn
  retval = (struct fd_channel *) calloc (1, sizeof (struct fd_channel));
69 1070 rprescott
 
70 1748 jeremybenn
  if (!retval)
71
    {
72
      return NULL;
73
    }
74 1070 rprescott
 
75
 
76 1748 jeremybenn
  retval->fdin = atoi (args);   /* so 0 if garbage */
77
  /* TODO: strtoul */
78 1070 rprescott
 
79 1748 jeremybenn
  args = strchr (args, ',');
80 1070 rprescott
 
81 1748 jeremybenn
  if (args)
82
    {
83
      retval->fdout = atoi (args + 1);
84
    }
85
  else
86
    {
87
      retval->fdout = retval->fdin;
88
    }
89 1070 rprescott
 
90 1748 jeremybenn
  return (void *) retval;
91 1070 rprescott
}
92
 
93 1748 jeremybenn
int
94
fd_read (void *data, char *buffer, int size)
95 1070 rprescott
{
96 1748 jeremybenn
  struct fd_channel *fds = (struct fd_channel *) data;
97
  struct timeval timeout = { 0, 0 };
98
  fd_set rfds;
99
  int retval;
100 1070 rprescott
 
101 1748 jeremybenn
  if (!fds)
102
    {
103
      errno = ENODEV;
104
      return -1;
105
    }
106 1070 rprescott
 
107 1748 jeremybenn
  FD_ZERO (&rfds);
108
  FD_SET (fds->fdin, &rfds);
109 1070 rprescott
 
110 1748 jeremybenn
  retval = select (fds->fdin + 1, &rfds, NULL, NULL, &timeout);
111 1070 rprescott
 
112 1748 jeremybenn
  if (retval <= 0)
113
    return retval;
114 1070 rprescott
 
115 1748 jeremybenn
  return read (fds->fdin, buffer, size);
116 1070 rprescott
}
117
 
118 1748 jeremybenn
int
119
fd_write (void *data, const char *buffer, int size)
120 1070 rprescott
{
121 1748 jeremybenn
  struct fd_channel *fds = (struct fd_channel *) data;
122
  if (fds)
123
    {
124
      return write (fds->fdout, buffer, size);
125
    }
126
  errno = ENODEV;
127
  return -1;
128 1070 rprescott
}
129
 
130 1748 jeremybenn
static int
131
fd_isok (void *data)
132 1070 rprescott
{
133 1748 jeremybenn
  struct fd_channel *fds = (struct fd_channel *) data;
134
  if (fds)
135
    {
136
      return fds->fdout != -1 && fds->fdin != -1;
137
    }
138
  return 0;
139 1070 rprescott
}
140
 
141 1748 jeremybenn
static int
142
fd_status_fd (int fd, char *str, int size)
143 1070 rprescott
{
144 1748 jeremybenn
  if (fd == -1)
145
    return snprintf (str, size, "closed");
146 1070 rprescott
 
147 1748 jeremybenn
  return snprintf (str, size, "opened(fd=%d)", fd);
148 1070 rprescott
}
149
 
150 1748 jeremybenn
static char *
151
fd_status (void *data)
152 1070 rprescott
{
153 1748 jeremybenn
  static char retval[256];
154
  int index = 0;
155 1070 rprescott
 
156 1748 jeremybenn
  struct fd_channel *fds = (struct fd_channel *) data;
157
  if (fds)
158
    {
159
      index += snprintf (retval + index, sizeof (retval) - index, "in ");
160
      index +=
161
        fd_status_fd (fds->fdin, retval + index, sizeof (retval) - index);
162 1070 rprescott
 
163 1748 jeremybenn
      index += snprintf (retval + index, sizeof (retval) - index, "out ");
164
      index +=
165
        fd_status_fd (fds->fdout, retval + index, sizeof (retval) - index);
166
    }
167
  else
168
    {
169
      snprintf (retval, sizeof (retval), "(null)");
170
    }
171
  return retval;
172 1070 rprescott
}
173
 

powered by: WebSVN 2.1.0

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