Home | History | Annotate | Line # | Download | only in lib
pcio.c revision 1.4
      1 /*	$NetBSD: pcio.c,v 1.4 1997/06/13 13:41:20 drochner Exp $	 */
      2 
      3 /*
      4  * Copyright (c) 1996
      5  *	Matthias Drochner.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed for the NetBSD Project
     18  *	by Matthias Drochner.
     19  * 4. The name of the author may not be used to endorse or promote products
     20  *    derived from this software without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  *
     33  */
     34 
     35 /*
     36  * console I/O
     37  * needs lowlevel routines from conio.S and comio.S
     38  */
     39 
     40 #include <lib/libsa/stand.h>
     41 
     42 #include "libi386.h"
     43 
     44 extern void conputc __P((int));
     45 extern int congetc __P((void));
     46 extern int coniskey __P((void));
     47 
     48 #ifdef SUPPORT_SERIAL
     49 extern void cominit __P((int));
     50 extern int computc __P((int, int));
     51 extern int comgetc __P((int));
     52 extern int comstatus __P((int));
     53 
     54 static int      iodev;
     55 #endif
     56 
     57 static char    *iodevname[] = {
     58 	"pc",
     59 #ifdef SUPPORT_SERIAL
     60 	"com0",
     61 	"com1"
     62 #endif
     63 };
     64 
     65 char           *
     66 initio(dev)
     67 	int             dev;
     68 {
     69 #ifdef SUPPORT_SERIAL
     70 	switch (dev) {
     71 	case CONSDEV_AUTO:
     72 		/*
     73 		 * serial console must have hardware handshake! check: 1.
     74 		 * character output without error 2. status bits for modem
     75 		 * ready set (status seems only useful after character
     76 		 * output)
     77 		 */
     78 		cominit(0);
     79 		if (!(computc(' ', 0) & 0x80) && (comstatus(0) & 0x00b0))
     80 			dev = CONSDEV_COM0;
     81 		else {
     82 			cominit(1);
     83 			if (!(computc(' ', 1) & 0x80) && (comstatus(1) & 0x00b0))
     84 				dev = CONSDEV_COM1;
     85 			else
     86 				dev = CONSDEV_PC;
     87 		}
     88 		break;
     89 	case CONSDEV_COM0:
     90 		cominit(0);
     91 		break;
     92 	case CONSDEV_COM1:
     93 		cominit(1);
     94 		break;
     95 	default:
     96 		dev = CONSDEV_PC;
     97 		break;
     98 	}
     99 	return (iodevname[iodev = dev]);
    100 #else
    101 	return (iodevname[0]);
    102 #endif
    103 }
    104 
    105 static inline void
    106 internal_putchar(c)
    107 	int             c;
    108 {
    109 #ifdef SUPPORT_SERIAL
    110 	switch (iodev) {
    111 	case CONSDEV_PC:
    112 #endif
    113 		conputc(c);
    114 #ifdef SUPPORT_SERIAL
    115 		break;
    116 	case CONSDEV_COM0:
    117 		computc(c, 0);
    118 		break;
    119 	case CONSDEV_COM1:
    120 		computc(c, 1);
    121 		break;
    122 	}
    123 #endif
    124 }
    125 
    126 void
    127 putchar(c)
    128 	int             c;
    129 {
    130 	if (c == '\n')
    131 		internal_putchar('\r');
    132 	internal_putchar(c);
    133 }
    134 
    135 int
    136 getchar()
    137 {
    138 #ifdef SUPPORT_SERIAL
    139 	switch (iodev) {
    140 		default: /* to make gcc -Wall happy... */
    141 		case CONSDEV_PC:
    142 #endif
    143 		return (congetc());
    144 #ifdef SUPPORT_SERIAL
    145 	case CONSDEV_COM0:
    146 		return (comgetc(0));
    147 	case CONSDEV_COM1:
    148 		return (comgetc(1));
    149 	}
    150 #endif
    151 }
    152 
    153 int
    154 iskey()
    155 {
    156 #ifdef SUPPORT_SERIAL
    157 	switch (iodev) {
    158 		default: /* to make gcc -Wall happy... */
    159 		case CONSDEV_PC:
    160 #endif
    161 		return (coniskey());
    162 #ifdef SUPPORT_SERIAL
    163 	case CONSDEV_COM0:
    164 		return (!!(comstatus(0) & 0x0100));
    165 	case CONSDEV_COM1:
    166 		return (!!(comstatus(1) & 0x0100));
    167 	}
    168 #endif
    169 }
    170