comconsole.c revision 40211
1/*
2 * Copyright (c) 1998 Michael Smith (msmith@freebsd.org)
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 * SUCH DAMAGE.
24 *
25 * 	From Id: probe_keyboard.c,v 1.13 1997/06/09 05:10:55 bde Exp
26 *
27 *	$Id: comconsole.c,v 1.3 1998/10/02 16:32:45 msmith Exp $
28 */
29
30#include <stand.h>
31#include <bootstrap.h>
32#include <btxv86.h>
33#include "libi386.h"
34
35static void	comc_probe(struct console *cp);
36static int	comc_init(int arg);
37static void	comc_putchar(int c);
38static int	comc_getchar(void);
39static int	comc_ischar(void);
40
41static int	comc_started;
42
43struct console comconsole = {
44    "comconsole",
45    "BIOS serial port",
46    0,
47    comc_probe,
48    comc_init,
49    comc_putchar,
50    comc_getchar,
51    comc_ischar
52};
53
54#define BIOS_COMPORT	0
55
56static void
57comc_probe(struct console *cp)
58{
59    /* XXX check the BIOS equipment list? */
60    cp->c_flags |= (C_PRESENTIN | C_PRESENTOUT);
61}
62
63static int
64comc_init(int arg)
65{
66    int		i;
67
68    if (comc_started && arg == 0)
69	return 0;
70    comc_started = 1;
71    v86.ctl = 0;
72    v86.addr = 0x14;
73    v86.eax = 0xe3;		/* 9600N81 */
74    v86.edx = BIOS_COMPORT;	/* XXX take as arg, or use env var? */
75    v86int();
76
77    for(i = 0; i < 10 && comc_ischar(); i++)
78        (void)comc_getchar();
79
80    return(0);
81}
82
83static void
84comc_putchar(int c)
85{
86    v86.ctl = 0;
87    v86.addr = 0x14;
88    v86.eax = 0x100 | c;	/* Function 1 = write */
89    v86.edx = BIOS_COMPORT;	/* XXX take as arg, or use env var? */
90    v86int();
91}
92
93static int
94comc_getchar(void)
95{
96    if (comc_ischar()) {
97	v86.ctl = 0;
98	v86.addr = 0x14;
99	v86.eax = 0x200;	/* Function 2 = read */
100	v86.edx = BIOS_COMPORT;	/* XXX take as arg, or use env var? */
101	v86int();
102	return(v86.eax & 0xff);
103    } else {
104	return(-1);
105    }
106}
107
108static int
109comc_ischar(void)
110{
111    v86.ctl = 0;
112    v86.addr = 0x14;
113    v86.eax = 0x300;		/* Function 3 = status */
114    v86.edx = BIOS_COMPORT;	/* XXX take as arg, or use env var? */
115    v86int();
116    return(v86.eax & 0x100);	/* AH bit 1 is "receive data ready" */
117}
118