1 /*        $NetBSD: cons.c,v 1.2 2014/03/26 17:47:10 christos Exp $    */
2 
3 /*-
4  * Copyright (c) 2005 NONAKA Kimihiro
5  * 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  *
16  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <lib/libsa/stand.h>
30 #include <lib/libkern/libkern.h>
31 
32 #include <sys/bootblock.h>
33 
34 #include "boot.h"
35 #include "cons.h"
36 
37 #ifndef   CONSPEED
38 #define   CONSPEED  9600
39 #endif
40 
41 #define   POLL_FREQ 10
42 
43 extern struct landisk_boot_params boot_params;
44 
45 static int consdev = CONSDEV_BIOSCONS;
46 
47 /*ARGSUSED*/
48 int
cninit(int dev)49 cninit(int dev)
50 {
51 
52           switch (dev) {
53           default:
54           case CONSDEV_BIOSCONS:
55                     break;
56 
57           case CONSDEV_SCIF:
58                     switch (boot_params.bp_conspeed) {
59                     default:
60                               scif_init(CONSPEED);
61                               break;
62 
63                     case 9600:
64 #if 0
65                     case 19200:
66                     case 38400:
67                     case 57600:
68                     case 115200:
69 #endif
70                               scif_init(boot_params.bp_conspeed);
71                               break;
72                     }
73                     break;
74           }
75           consdev = dev;
76 
77           return (0);
78 }
79 
80 int
getchar(void)81 getchar(void)
82 {
83 
84           switch (consdev) {
85           default:
86           case CONSDEV_BIOSCONS:
87                     return bioscons_getc();
88 
89           case CONSDEV_SCIF:
90                     return scif_getc();
91           }
92 }
93 
94 void
putchar(int c)95 putchar(int c)
96 {
97 
98           switch (consdev) {
99           default:
100           case CONSDEV_BIOSCONS:
101                     bioscons_putc(c);
102                     break;
103 
104           case CONSDEV_SCIF:
105                     if (c == '\n')
106                               scif_putc('\r');
107                     scif_putc(c);
108                     break;
109           }
110 }
111 
112 /*ARGSUSED*/
113 int
iskey(int intr)114 iskey(int intr)
115 {
116 
117           switch (consdev) {
118           default:
119           case CONSDEV_BIOSCONS:
120                     return scif_status2();
121 
122           case CONSDEV_SCIF:
123                     return scif_status();
124           }
125 }
126 
127 char
awaitkey(int timeout,int tell)128 awaitkey(int timeout, int tell)
129 {
130           int i;
131           char c = 0;
132 
133           i = timeout * POLL_FREQ;
134 
135           for (;;) {
136                     if (tell && (i % POLL_FREQ) == 0) {
137                               char numbuf[20];
138                               int len, j;
139 
140                               len = snprintf(numbuf, sizeof(numbuf),
141                                   "%d ", i / POLL_FREQ);
142                               for (j = 0; j < len; j++)
143                                         numbuf[len + j] = '\b';
144                               numbuf[len + j] = '\0';
145                               printf(numbuf);
146                     }
147                     if (iskey(1)) {
148                               /* flush input buffer */
149                               while (iskey(0))
150                                         c = getchar();
151                               if (c == 0)
152                                         c = -1;
153                               goto out;
154                     }
155                     if (i--) {
156                               delay(1000 / POLL_FREQ);
157                     } else {
158                               break;
159                     }
160           }
161 
162 out:
163           if (tell)
164                     printf("0 \n");
165 
166           return (c);
167 }
168