/* tty.c -- Definition of functions for peripheral to
|
/* tty.c -- Definition of functions for peripheral to
|
* communicate with host via a tty.
|
* communicate with host via a tty.
|
|
|
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 <termios.h>
|
#include <termios.h>
|
#include <stdio.h>
|
#include <stdio.h>
|
#include <fcntl.h>
|
#include <fcntl.h>
|
|
|
/* Package includes */
|
/* Package includes */
|
#include "channel.h"
|
#include "channel.h"
|
#include "generic.h"
|
#include "generic.h"
|
#include "fd.h"
|
#include "fd.h"
|
|
|
/* Default parameters if not specified in config file */
|
/* Default parameters if not specified in config file */
|
#define DEFAULT_BAUD B19200
|
#define DEFAULT_BAUD B19200
|
#define DEFAULT_TTY_DEVICE "/dev/ttyS0"
|
#define DEFAULT_TTY_DEVICE "/dev/ttyS0"
|
|
|
/*! Data structure representing a TTY channel */
|
/*! Data structure representing a TTY channel */
|
struct tty_channel
|
struct tty_channel
|
{
|
{
|
struct fd_channel fds;
|
struct fd_channel fds;
|
};
|
};
|
|
|
/*! Table of Baud rates */
|
/*! Table of Baud rates */
|
static const struct
|
static const struct
|
{
|
{
|
char *name;
|
char *name;
|
int value;
|
int value;
|
} baud_table[] =
|
} baud_table[] =
|
{
|
{
|
{
|
{
|
"50", B50},
|
"50", B50},
|
{
|
{
|
"2400", B2400},
|
"2400", B2400},
|
{
|
{
|
"4800", B4800},
|
"4800", B4800},
|
{
|
{
|
"9600", B9600},
|
"9600", B9600},
|
{
|
{
|
"19200", B19200},
|
"19200", B19200},
|
{
|
{
|
"38400", B38400},
|
"38400", B38400},
|
{
|
{
|
"115200", B115200},
|
"115200", B115200},
|
{
|
{
|
"230400", B230400},
|
"230400", B230400},
|
{
|
{
|
0, 0}
|
0, 0}
|
};
|
};
|
|
|
/* Forward declaration of static functions */
|
/* Forward declaration of static functions */
|
static void *tty_init (const char *input);
|
static void *tty_init (const char *input);
|
static int tty_open (void *data);
|
static int tty_open (void *data);
|
|
|
/*! Global data structure representing the operations on a TTY channel */
|
/*! Global data structure representing the operations on a TTY channel */
|
struct channel_ops tty_channel_ops = {
|
struct channel_ops tty_channel_ops = {
|
.init = tty_init,
|
.init = tty_init,
|
.open = tty_open,
|
.open = tty_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,
|
};
|
};
|
|
|
|
|
/* Convert baud rate string to termio baud rate constant */
|
/* Convert baud rate string to termio baud rate constant */
|
static int
|
static int
|
parse_baud (char *baud_string)
|
parse_baud (char *baud_string)
|
{
|
{
|
int i;
|
int i;
|
for (i = 0; baud_table[i].name; i++)
|
for (i = 0; baud_table[i].name; i++)
|
{
|
{
|
if (!strcmp (baud_table[i].name, baud_string))
|
if (!strcmp (baud_table[i].name, baud_string))
|
return baud_table[i].value;
|
return baud_table[i].value;
|
}
|
}
|
|
|
fprintf (stderr, "Error: unknown baud rate: %s\n", baud_string);
|
fprintf (stderr, "Error: unknown baud rate: %s\n", baud_string);
|
fprintf (stderr, " Known baud rates: ");
|
fprintf (stderr, " Known baud rates: ");
|
|
|
for (i = 0; baud_table[i].name; i++)
|
for (i = 0; baud_table[i].name; i++)
|
{
|
{
|
fprintf (stderr, "%s%s", baud_table[i].name,
|
fprintf (stderr, "%s%s", baud_table[i].name,
|
baud_table[i + 1].name ? ", " : "\n");
|
baud_table[i + 1].name ? ", " : "\n");
|
}
|
}
|
return B0;
|
return B0;
|
}
|
}
|
|
|
static void *
|
static void *
|
tty_init (const char *input)
|
tty_init (const char *input)
|
{
|
{
|
int fd = 0, baud;
|
int fd = 0, baud;
|
char *param_name, *param_value, *device;
|
char *param_name, *param_value, *device;
|
struct termios options;
|
struct termios options;
|
struct tty_channel *channel;
|
struct tty_channel *channel;
|
|
|
channel = (struct tty_channel *) malloc (sizeof (struct tty_channel));
|
channel = (struct tty_channel *) malloc (sizeof (struct tty_channel));
|
if (!channel)
|
if (!channel)
|
return NULL;
|
return NULL;
|
|
|
/* Make a copy of config string, because we're about to mutate it */
|
/* Make a copy of config string, because we're about to mutate it */
|
input = strdup (input);
|
input = strdup (input);
|
if (!input)
|
if (!input)
|
goto error;
|
goto error;
|
|
|
baud = DEFAULT_BAUD;
|
baud = DEFAULT_BAUD;
|
device = DEFAULT_TTY_DEVICE;
|
device = DEFAULT_TTY_DEVICE;
|
|
|
/* Parse command-line parameters
|
/* Parse command-line parameters
|
Command line looks like name1=value1,name2,name3=value3,... */
|
Command line looks like name1=value1,name2,name3=value3,... */
|
while ((param_name = strtok ((char *) input, ",")))
|
while ((param_name = strtok ((char *) input, ",")))
|
{
|
{
|
|
|
input = NULL;
|
input = NULL;
|
|
|
/* Parse a parameter's name and value */
|
/* Parse a parameter's name and value */
|
param_value = strchr (param_name, '=');
|
param_value = strchr (param_name, '=');
|
if (param_value != NULL)
|
if (param_value != NULL)
|
{
|
{
|
*param_value = '\0';
|
*param_value = '\0';
|
param_value++; /* Advance past '=' character */
|
param_value++; /* Advance past '=' character */
|
}
|
}
|
|
|
if (!strcmp (param_name, "baud") && param_value)
|
if (!strcmp (param_name, "baud") && param_value)
|
{
|
{
|
baud = parse_baud (param_value);
|
baud = parse_baud (param_value);
|
if (baud == B0)
|
if (baud == B0)
|
{
|
{
|
goto error;
|
goto error;
|
}
|
}
|
}
|
}
|
else if (!strcmp (param_name, "device"))
|
else if (!strcmp (param_name, "device"))
|
{
|
{
|
device = param_value;
|
device = param_value;
|
}
|
}
|
else
|
else
|
{
|
{
|
fprintf (stderr, "error: unknown tty channel parameter \"%s\"\n",
|
fprintf (stderr, "error: unknown tty channel parameter \"%s\"\n",
|
param_name);
|
param_name);
|
goto error;
|
goto error;
|
}
|
}
|
}
|
}
|
|
|
fd = open (device, O_RDWR);
|
fd = open (device, O_RDWR);
|
if (fd < 0)
|
if (fd < 0)
|
goto error;
|
goto error;
|
|
|
/* Get the current options for the port... */
|
/* Get the current options for the port... */
|
if (tcgetattr (fd, &options) < 0)
|
if (tcgetattr (fd, &options) < 0)
|
goto error;
|
goto error;
|
|
|
/* Set the serial baud rate */
|
/* Set the serial baud rate */
|
cfsetispeed (&options, baud);
|
cfsetispeed (&options, baud);
|
cfsetospeed (&options, baud);
|
cfsetospeed (&options, baud);
|
|
|
/* Enable the receiver and set local mode... */
|
/* Enable the receiver and set local mode... */
|
|
|
/* cfmakeraw(&options);
|
/* cfmakeraw(&options);
|
*
|
*
|
* cygwin lacks cfmakeraw(), just do it explicitly
|
* cygwin lacks cfmakeraw(), just do it explicitly
|
*/
|
*/
|
options.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP
|
options.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP
|
| INLCR | IGNCR | ICRNL | IXON);
|
| INLCR | IGNCR | ICRNL | IXON);
|
options.c_oflag &= ~OPOST;
|
options.c_oflag &= ~OPOST;
|
options.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
|
options.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
|
options.c_cflag &= ~(CSIZE | PARENB);
|
options.c_cflag &= ~(CSIZE | PARENB);
|
options.c_cflag |= CS8;
|
options.c_cflag |= CS8;
|
|
|
options.c_cflag |= (CLOCAL | CREAD);
|
options.c_cflag |= (CLOCAL | CREAD);
|
|
|
|
|
/* Set the new options for the port... */
|
/* Set the new options for the port... */
|
if (tcsetattr (fd, TCSANOW, &options) < 0)
|
if (tcsetattr (fd, TCSANOW, &options) < 0)
|
goto error;
|
goto error;
|
|
|
channel->fds.fdin = channel->fds.fdout = fd;
|
channel->fds.fdin = channel->fds.fdout = fd;
|
free ((void *) input);
|
free ((void *) input);
|
return channel;
|
return channel;
|
|
|
error:
|
error:
|
if (fd > 0)
|
if (fd > 0)
|
close (fd);
|
close (fd);
|
free (channel);
|
free (channel);
|
if (input)
|
if (input)
|
free ((void *) input);
|
free ((void *) input);
|
return NULL;
|
return NULL;
|
}
|
}
|
|
|
static int
|
static int
|
tty_open (void *data)
|
tty_open (void *data)
|
{
|
{
|
return 0;
|
return 0;
|
}
|
}
|
|
|