Home | History | Annotate | Line # | Download | only in lib
pcio.c revision 1.1
      1 /*	$NetBSD: pcio.c,v 1.1 1997/03/14 02:40:33 perry 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 /* console I/O
     36  needs lowlevel routines from conio.S and comio.S
     37 */
     38 
     39 #include <lib/libsa/stand.h>
     40 
     41 #include "libi386.h"
     42 
     43 extern void conputc __P((int));
     44 extern int congetc __P((void));
     45 extern int coniskey __P((void));
     46 
     47 #ifdef SUPPORT_SERIAL
     48 extern void computc __P((int));
     49 extern int comgetc __P((void));
     50 extern int comiskey __P((void));
     51 
     52 static int iodev;
     53 #endif
     54 
     55 static char *iodevname[] = {
     56     "pc",
     57 #ifdef SUPPORT_SERIAL
     58     "com0",
     59     "com1"
     60 #endif
     61 };
     62 
     63 char *initio(dev)
     64 int dev;
     65 {
     66 #ifdef SUPPORT_SERIAL
     67   switch(dev){
     68     case CONSDEV_AUTO:
     69       /* serial console must have hardware handshake!
     70        check: 1. character output without error
     71               2. status bits for modem ready set
     72        (status seems only useful after character output)
     73        */
     74       cominit(0);
     75       if(!(computc(' ', 0) & 0x80) && (comstatus(0) & 0x00b0))
     76 	dev = 1;
     77       else {
     78 	cominit(1);
     79 	if(!(computc(' ', 1) & 0x80) && (comstatus(1) & 0x00b0))
     80 	  dev = 2;
     81 	else
     82 	  dev = 0;
     83       }
     84       break;
     85     case CONSDEV_COM0: cominit(0); break;
     86     case CONSDEV_COM1: cominit(1); break;
     87     default: dev = 0; break;
     88   }
     89   return(iodevname[iodev = dev]);
     90 #else
     91   return(iodevname[0]);
     92 #endif
     93 }
     94 
     95 static inline void internal_putchar(c)
     96 int c;
     97 {
     98 #ifdef SUPPORT_SERIAL
     99   switch(iodev){
    100     case 0:
    101 #endif
    102       conputc(c);
    103 #ifdef SUPPORT_SERIAL
    104       break;
    105     case 1: computc(c, 0); break;
    106     case 2: computc(c, 1); break;
    107   }
    108 #endif
    109 }
    110 
    111 void putchar(c)
    112 int c;
    113 {
    114   if(c=='\n')
    115     internal_putchar('\r');
    116   internal_putchar(c);
    117 }
    118 
    119 int getchar(){
    120 #ifdef SUPPORT_SERIAL
    121   switch(iodev){
    122     case 0:
    123 #endif
    124       return(congetc());
    125 #ifdef SUPPORT_SERIAL
    126     case 1: return(comgetc(0));
    127     case 2: return(comgetc(1));
    128   }
    129 #endif
    130 }
    131 
    132 int iskey(){
    133 #ifdef SUPPORT_SERIAL
    134   switch(iodev){
    135     case 0:
    136 #endif
    137       return(coniskey());
    138 #ifdef SUPPORT_SERIAL
    139     case 1: return(!!(comstatus(0) & 0x0100));
    140     case 2: return(!!(comstatus(1) & 0x0100));
    141   }
    142 #endif
    143 }
    144