1997-10-27 22:53:20 +03:00
|
|
|
/* $NetBSD: pcio.c,v 1.6 1997/10/27 19:53:20 drochner Exp $ */
|
1997-03-14 05:40:32 +03:00
|
|
|
|
|
|
|
/*
|
1997-09-17 21:48:00 +04:00
|
|
|
* Copyright (c) 1996, 1997
|
1997-03-14 05:40:32 +03:00
|
|
|
* Matthias Drochner. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed for the NetBSD Project
|
|
|
|
* by Matthias Drochner.
|
|
|
|
* 4. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
1997-03-22 04:48:27 +03:00
|
|
|
/*
|
1997-06-13 17:41:20 +04:00
|
|
|
* console I/O
|
|
|
|
* needs lowlevel routines from conio.S and comio.S
|
1997-03-22 04:48:27 +03:00
|
|
|
*/
|
1997-03-14 05:40:32 +03:00
|
|
|
|
|
|
|
#include <lib/libsa/stand.h>
|
1997-09-17 21:48:00 +04:00
|
|
|
#include <lib/libkern/libkern.h>
|
1997-03-14 05:40:32 +03:00
|
|
|
|
|
|
|
#include "libi386.h"
|
1997-09-17 21:48:00 +04:00
|
|
|
#include "bootinfo.h"
|
1997-03-14 05:40:32 +03:00
|
|
|
|
|
|
|
extern void conputc __P((int));
|
|
|
|
extern int congetc __P((void));
|
|
|
|
extern int coniskey __P((void));
|
|
|
|
|
1997-09-17 21:48:00 +04:00
|
|
|
struct btinfo_console btinfo_console;
|
|
|
|
|
1997-03-14 05:40:32 +03:00
|
|
|
#ifdef SUPPORT_SERIAL
|
1997-09-17 21:48:00 +04:00
|
|
|
static int iodev;
|
|
|
|
|
|
|
|
#ifdef DIRECT_SERIAL
|
|
|
|
#include "comio_direct.h"
|
|
|
|
|
|
|
|
#define cominit cominit_d
|
|
|
|
#define computc computc_d
|
|
|
|
#define comgetc comgetc_d
|
|
|
|
/* comstatus() is different */
|
|
|
|
|
|
|
|
#define SERIAL_ARG btinfo_console.addr
|
|
|
|
#else
|
1997-06-13 17:41:20 +04:00
|
|
|
extern void cominit __P((int));
|
1997-03-16 01:15:49 +03:00
|
|
|
extern int computc __P((int, int));
|
|
|
|
extern int comgetc __P((int));
|
|
|
|
extern int comstatus __P((int));
|
1997-03-14 05:40:32 +03:00
|
|
|
|
1997-09-17 21:48:00 +04:00
|
|
|
#define SERIAL_ARG (iodev - CONSDEV_COM0)
|
|
|
|
#endif /* DIRECT_SERIAL */
|
|
|
|
|
|
|
|
static int getcomaddr __P((int));
|
|
|
|
#endif /* SUPPORT_SERIAL */
|
|
|
|
|
|
|
|
#define POLL_FREQ 10
|
1997-03-14 05:40:32 +03:00
|
|
|
|
|
|
|
#ifdef SUPPORT_SERIAL
|
1997-09-17 21:48:00 +04:00
|
|
|
static int
|
|
|
|
getcomaddr(idx)
|
|
|
|
int idx;
|
|
|
|
{
|
|
|
|
short addr;
|
|
|
|
/* read in BIOS data area */
|
|
|
|
pvbcopy(0x400 + 2 * idx, &addr, 2);
|
|
|
|
return(addr);
|
|
|
|
}
|
1997-03-14 05:40:32 +03:00
|
|
|
#endif
|
|
|
|
|
1997-09-17 21:48:00 +04:00
|
|
|
void
|
1997-03-22 04:48:27 +03:00
|
|
|
initio(dev)
|
|
|
|
int dev;
|
1997-03-14 05:40:32 +03:00
|
|
|
{
|
|
|
|
#ifdef SUPPORT_SERIAL
|
1997-09-17 21:48:00 +04:00
|
|
|
int i;
|
|
|
|
|
1997-03-22 04:48:27 +03:00
|
|
|
switch (dev) {
|
1997-09-17 21:48:00 +04:00
|
|
|
case CONSDEV_AUTO:
|
|
|
|
for(i = 0; i < 3; i++) {
|
|
|
|
iodev = CONSDEV_COM0 + i;
|
|
|
|
btinfo_console.addr = getcomaddr(i);
|
|
|
|
if(!btinfo_console.addr) break;
|
|
|
|
conputc('0' + i); /* to tell user what happens */
|
|
|
|
cominit(SERIAL_ARG);
|
|
|
|
#ifdef DIRECT_SERIAL
|
|
|
|
/* check for:
|
|
|
|
* 1. successful output
|
|
|
|
* 2. optionally, keypress within 1s
|
|
|
|
*/
|
|
|
|
if(computc(' ', SERIAL_ARG)
|
|
|
|
#ifdef COMCONS_KEYPRESS
|
|
|
|
&& awaitkey(1, 0)
|
|
|
|
#endif
|
|
|
|
)
|
|
|
|
goto ok;
|
|
|
|
#else
|
|
|
|
/*
|
|
|
|
* serial console must have hardware handshake!
|
|
|
|
* check:
|
|
|
|
* 1. character output without error
|
|
|
|
* 2. status bits for modem ready set
|
|
|
|
* (status seems only useful after character output)
|
|
|
|
* 3. optionally, keypress within 1s
|
|
|
|
*/
|
|
|
|
if (!(computc(' ', SERIAL_ARG) & 0x80)
|
|
|
|
&& (comstatus(SERIAL_ARG) & 0x00b0)
|
|
|
|
#ifdef COMCONS_KEYPRESS
|
|
|
|
&& awaitkey(1, 0)
|
|
|
|
#endif
|
|
|
|
)
|
|
|
|
goto ok;
|
|
|
|
#endif
|
1997-03-22 04:48:27 +03:00
|
|
|
}
|
1997-09-17 21:48:00 +04:00
|
|
|
iodev = CONSDEV_PC;
|
|
|
|
ok:
|
1997-03-22 04:48:27 +03:00
|
|
|
break;
|
1997-09-17 21:48:00 +04:00
|
|
|
case CONSDEV_COM0:
|
|
|
|
case CONSDEV_COM1:
|
|
|
|
case CONSDEV_COM2:
|
|
|
|
case CONSDEV_COM3:
|
|
|
|
iodev = dev;
|
|
|
|
btinfo_console.addr = getcomaddr(iodev - CONSDEV_COM0);
|
|
|
|
if(!btinfo_console.addr) goto nocom;
|
|
|
|
cominit(SERIAL_ARG);
|
1997-03-22 04:48:27 +03:00
|
|
|
break;
|
1997-09-17 21:48:00 +04:00
|
|
|
default:
|
|
|
|
nocom:
|
|
|
|
iodev = CONSDEV_PC;
|
1997-03-22 04:48:27 +03:00
|
|
|
break;
|
|
|
|
}
|
1997-09-17 21:48:00 +04:00
|
|
|
strncpy(btinfo_console.devname, iodev == CONSDEV_PC ? "pc" : "com", 16);
|
|
|
|
#if defined(DIRECT_SERIAL) && defined(CONSPEED)
|
|
|
|
btinfo_console.speed = CONSPEED;
|
1997-03-14 05:40:32 +03:00
|
|
|
#else
|
1997-09-17 21:48:00 +04:00
|
|
|
btinfo_console.speed = 9600;
|
1997-03-14 05:40:32 +03:00
|
|
|
#endif
|
1997-09-17 21:48:00 +04:00
|
|
|
#else /* !SUPPORT_SERIAL */
|
|
|
|
strncpy(btinfo_console.devname, "pc", 16);
|
|
|
|
#endif /* SUPPORT_SERIAL */
|
1997-03-14 05:40:32 +03:00
|
|
|
}
|
|
|
|
|
1997-03-22 04:48:27 +03:00
|
|
|
static inline void
|
|
|
|
internal_putchar(c)
|
|
|
|
int c;
|
1997-03-14 05:40:32 +03:00
|
|
|
{
|
|
|
|
#ifdef SUPPORT_SERIAL
|
1997-03-22 04:48:27 +03:00
|
|
|
switch (iodev) {
|
1997-09-17 21:48:00 +04:00
|
|
|
case CONSDEV_PC:
|
1997-03-14 05:40:32 +03:00
|
|
|
#endif
|
1997-03-22 04:48:27 +03:00
|
|
|
conputc(c);
|
1997-03-14 05:40:32 +03:00
|
|
|
#ifdef SUPPORT_SERIAL
|
1997-03-22 04:48:27 +03:00
|
|
|
break;
|
1997-09-17 21:48:00 +04:00
|
|
|
case CONSDEV_COM0:
|
|
|
|
case CONSDEV_COM1:
|
|
|
|
case CONSDEV_COM2:
|
|
|
|
case CONSDEV_COM3:
|
|
|
|
computc(c, SERIAL_ARG);
|
1997-03-22 04:48:27 +03:00
|
|
|
break;
|
|
|
|
}
|
1997-03-14 05:40:32 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
1997-03-22 04:48:27 +03:00
|
|
|
void
|
|
|
|
putchar(c)
|
|
|
|
int c;
|
1997-03-14 05:40:32 +03:00
|
|
|
{
|
1997-03-22 04:48:27 +03:00
|
|
|
if (c == '\n')
|
|
|
|
internal_putchar('\r');
|
|
|
|
internal_putchar(c);
|
1997-03-14 05:40:32 +03:00
|
|
|
}
|
|
|
|
|
1997-03-22 04:48:27 +03:00
|
|
|
int
|
|
|
|
getchar()
|
|
|
|
{
|
1997-03-14 05:40:32 +03:00
|
|
|
#ifdef SUPPORT_SERIAL
|
1997-10-27 22:53:20 +03:00
|
|
|
int c;
|
1997-03-22 04:48:27 +03:00
|
|
|
switch (iodev) {
|
1997-09-17 21:48:00 +04:00
|
|
|
default: /* to make gcc -Wall happy... */
|
|
|
|
case CONSDEV_PC:
|
1997-03-14 05:40:32 +03:00
|
|
|
#endif
|
1997-03-22 04:48:27 +03:00
|
|
|
return (congetc());
|
1997-03-14 05:40:32 +03:00
|
|
|
#ifdef SUPPORT_SERIAL
|
1997-09-17 21:48:00 +04:00
|
|
|
case CONSDEV_COM0:
|
|
|
|
case CONSDEV_COM1:
|
|
|
|
case CONSDEV_COM2:
|
|
|
|
case CONSDEV_COM3:
|
1997-10-27 22:53:20 +03:00
|
|
|
#ifdef DIRECT_SERIAL
|
|
|
|
c = comgetc(SERIAL_ARG);
|
|
|
|
#else
|
|
|
|
do {
|
|
|
|
c = comgetc(SERIAL_ARG);
|
|
|
|
} while ((c >> 8) == 0xe0); /* catch timeout */
|
|
|
|
#ifdef COMDEBUG
|
|
|
|
if (c & 0x8000) {
|
|
|
|
printf("com input %x, status %x\n",
|
|
|
|
c, comstatus(SERIAL_ARG));
|
|
|
|
}
|
1997-03-14 05:40:32 +03:00
|
|
|
#endif
|
1997-10-27 22:53:20 +03:00
|
|
|
c &= 0xff;
|
|
|
|
#endif /* DIRECT_SERIAL */
|
|
|
|
return (c);
|
|
|
|
}
|
|
|
|
#endif /* SUPPORT_SERIAL */
|
1997-03-14 05:40:32 +03:00
|
|
|
}
|
|
|
|
|
1997-03-22 04:48:27 +03:00
|
|
|
int
|
|
|
|
iskey()
|
|
|
|
{
|
1997-03-14 05:40:32 +03:00
|
|
|
#ifdef SUPPORT_SERIAL
|
1997-03-22 04:48:27 +03:00
|
|
|
switch (iodev) {
|
1997-09-17 21:48:00 +04:00
|
|
|
default: /* to make gcc -Wall happy... */
|
|
|
|
case CONSDEV_PC:
|
1997-03-14 05:40:32 +03:00
|
|
|
#endif
|
1997-03-22 04:48:27 +03:00
|
|
|
return (coniskey());
|
1997-03-14 05:40:32 +03:00
|
|
|
#ifdef SUPPORT_SERIAL
|
1997-09-17 21:48:00 +04:00
|
|
|
case CONSDEV_COM0:
|
|
|
|
case CONSDEV_COM1:
|
|
|
|
case CONSDEV_COM2:
|
|
|
|
case CONSDEV_COM3:
|
|
|
|
#ifdef DIRECT_SERIAL
|
|
|
|
return(!!comstatus_d(SERIAL_ARG));
|
|
|
|
#else
|
|
|
|
return (!!(comstatus(SERIAL_ARG) & 0x0100));
|
1997-03-14 05:40:32 +03:00
|
|
|
#endif
|
1997-09-17 21:48:00 +04:00
|
|
|
}
|
|
|
|
#endif /* SUPPORT_SERIAL */
|
|
|
|
}
|
|
|
|
|
|
|
|
char
|
|
|
|
awaitkey(timeout, tell)
|
|
|
|
int timeout, tell;
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char c = 0;
|
|
|
|
|
|
|
|
i = timeout * POLL_FREQ;
|
|
|
|
|
|
|
|
while (i) {
|
|
|
|
if (tell && (i % POLL_FREQ) == 0)
|
|
|
|
printf("%d\b", i/POLL_FREQ);
|
|
|
|
if (iskey()) {
|
|
|
|
/* flush input buffer */
|
|
|
|
while (iskey())
|
|
|
|
c = getchar();
|
|
|
|
goto out; /* XXX what happens if c == 0? */
|
|
|
|
}
|
|
|
|
delay(1000000 / POLL_FREQ);
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
if (tell)
|
|
|
|
printf("0\n");
|
|
|
|
|
|
|
|
return(c);
|
1997-03-14 05:40:32 +03:00
|
|
|
}
|