Home | History | Annotate | Line # | Download | only in boot
console.c revision 1.14
      1 /* $NetBSD: console.c,v 1.14 2016/01/15 08:27:04 mlelstv Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1996 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Ignatios Souvatzis.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Bootblock support routines for Intuition console support.
     34  */
     35 
     36 #include <sys/types.h>
     37 
     38 #include <lib/libsa/stand.h>
     39 #include "samachdep.h"
     40 
     41 #include "amigatypes.h"
     42 #include "amigagraph.h"
     43 #include "amigaio.h"
     44 #include "libstubs.h"
     45 
     46 const u_int32_t screentags[] = {
     47 	SA_Type, CUSTOMSCREEN,
     48 	SA_DisplayID, 0x8000,
     49 	SA_ShowTitle, 0,
     50 	SA_Quiet, 1,
     51 	0
     52 };
     53 
     54 u_int32_t windowtags[] = {
     55 	WA_CustomScreen, 0L,
     56 	WA_Borderless, 1L,
     57 	WA_Backdrop, 1L,
     58 	WA_Activate, 1L,
     59 	0
     60 };
     61 
     62 struct Console {
     63 	int magic;
     64 	struct AmigaIO *cnior;
     65 	struct TimerIO *tmior;
     66 	struct MsgPort *cnmp;
     67 	struct Screen *s;
     68 	struct Window *w;
     69 } *ConsoleBase;
     70 static struct Console myConsole;
     71 
     72 u_int16_t timelimit;
     73 
     74 #ifdef SERCONSOLE
     75 static int use_serconsole;
     76 extern char default_command[];
     77 
     78 static void
     79 conspreinit(void)
     80 {
     81 	char *p = default_command;
     82 	char c;
     83 
     84 	/*
     85 	 * preparse the default command to check for -C option
     86 	 * that selects the serial console
     87 	 */
     88 	while ((c = *p)) {
     89 		while (c == ' ')
     90 			c = *++p;
     91 		if (c == '-') {
     92 			while ((c = *++p) && c != ' ') {
     93 				switch (c) {
     94 				case 'C':
     95 					use_serconsole = 1;
     96 					break;
     97 				}
     98 			}
     99 		} else {
    100 			while ((c = *++p) && c != ' ')
    101 				;
    102 		}
    103 	}
    104 }
    105 #endif
    106 
    107 int
    108 consinit(void *consptr) {
    109 	struct Console *mc;
    110 
    111 	if (consptr != NULL) {
    112 		/* Check magic? */
    113 		ConsoleBase = consptr;		/* Use existing console */
    114 		return (0);
    115 	}
    116 
    117 	mc = &myConsole;
    118 	IntuitionBase = OpenLibrary("intuition.library", 36L);
    119 	if (IntuitionBase == 0)
    120 		goto err;
    121 
    122 	mc->s = OpenScreenTagList(0, screentags);
    123 	if (!mc->s)
    124 		goto err;
    125 
    126 	windowtags[1] = (u_int32_t)mc->s;
    127 	mc->w = OpenWindowTagList(0, windowtags);
    128 	if (!mc->w)
    129 		goto err;
    130 
    131 	mc->cnmp = CreateMsgPort();
    132 
    133 	if (!mc->cnmp)
    134 		goto err;
    135 
    136 	mc->cnior = (struct AmigaIO *)CreateIORequest(mc->cnmp, sizeof(struct AmigaIO));
    137 	if (!mc->cnior)
    138 		goto err;
    139 
    140 	mc->cnior->buf = (void *)mc->w;
    141 	if (OpenDevice("console.device", 0, mc->cnior, 0))
    142 		goto err;
    143 
    144 	mc->tmior = (struct TimerIO *)CreateIORequest(mc->cnmp, sizeof(struct TimerIO));
    145 	if (!mc->tmior)
    146 		goto err;
    147 
    148 	if (OpenDevice("timer.device", 0, (struct AmigaIO*)mc->tmior, 0))
    149 		goto err;
    150 
    151 #ifdef SERCONSOLE
    152 	conspreinit();
    153 	if (use_serconsole)
    154 		RawIOInit();
    155 #endif
    156 
    157 	ConsoleBase = mc;
    158 	return 0;
    159 
    160 err:
    161 #ifdef notyet
    162 	if (mc->tmior)
    163 		DeleteIORequest(mc->tmior);
    164 
    165 	if (mc->cnior)
    166 		DeleteIORequest(mc->cnior);
    167 
    168 	if (mc->cnmp)
    169 		DeleteMsgPort(mc->cnmp);
    170 
    171 	if (mc->w)
    172 		CloseWindow(mc->w);
    173 
    174 	if (mc->s)
    175 		CloseScreen(mc->s);
    176 	if (IntuitionBase)
    177 		CloseLibrary(IntuitionBase);
    178 #endif
    179 
    180 	return 1;
    181 }
    182 
    183 #ifdef _PRIMARY_BOOT
    184 int
    185 consclose(void)
    186 {
    187 	struct Console *mc = ConsoleBase;
    188 
    189 	if (mc == NULL)
    190 		return 0;
    191 	if (mc->tmior) {
    192 		CloseDevice((struct AmigaIO *)mc->tmior);
    193 		DeleteIORequest(mc->tmior);
    194 	}
    195 
    196 	if (mc->cnior) {
    197 		CloseDevice(mc->cnior);
    198 		DeleteIORequest(mc->cnior);
    199 	}
    200 
    201 	if (mc->cnmp)
    202 		DeleteMsgPort(mc->cnmp);
    203 
    204 	if (mc->w)
    205 		CloseWindow(mc->w);
    206 
    207 	if (mc->s)
    208 		CloseScreen(mc->s);
    209 	if (IntuitionBase)
    210 		CloseLibrary(IntuitionBase);
    211 	ConsoleBase = NULL;
    212 	return 0;
    213 }
    214 #endif
    215 
    216 void
    217 putchar(int c)
    218 {
    219 	struct Console *mc = ConsoleBase;
    220 	char buf = c;
    221 
    222 	mc->cnior->length = 1;
    223 	mc->cnior->buf = &buf;
    224 	mc->cnior->cmd = Cmd_Wr;
    225 
    226 #ifdef SERCONSOLE
    227 	if (use_serconsole)
    228 		RawPutChar((int32_t)c);
    229 #endif
    230 
    231 	(void)DoIO(mc->cnior);
    232 }
    233 
    234 void
    235 puts(char *s)
    236 {
    237 	struct Console *mc = ConsoleBase;
    238 
    239 	mc->cnior->length = -1;
    240 	mc->cnior->buf = s;
    241 	mc->cnior->cmd = Cmd_Wr;
    242 
    243 #ifdef SERCONSOLE
    244 	if (use_serconsole) {
    245 		while (*s)
    246 			RawPutChar(*s++);
    247 	}
    248 #endif
    249 
    250 	(void)DoIO(mc->cnior);
    251 }
    252 
    253 int
    254 getchar(void)
    255 {
    256 	struct AmigaIO *ior;
    257 	char c = '\n';
    258 	struct Console *mc = ConsoleBase;
    259 	unsigned long ticks;
    260 #ifdef SERCONSOLE
    261 	int32_t r;
    262 #endif
    263 
    264 	mc->cnior->length = 1;
    265 	mc->cnior->buf = &c;
    266 	mc->cnior->cmd = Cmd_Rd;
    267 
    268 	SendIO(mc->cnior);
    269 
    270 	ticks = 10 * timelimit;
    271 	do {
    272 		if (timelimit == 0)
    273 			ticks = 2;
    274 
    275 		mc->tmior->cmd = Cmd_Addtimereq;
    276 		mc->tmior->secs = 0;
    277 		mc->tmior->usec = 100000;
    278 		SendIO((struct AmigaIO *)mc->tmior);
    279 
    280 		ior = WaitPort(mc->cnmp);
    281 		if (ior == mc->cnior) {
    282 			AbortIO((struct AmigaIO *)mc->tmior);
    283 			ticks = 1;
    284 		} else /* if (ior == mc->tmior) */ {
    285 #ifdef SERCONSOLE
    286 			if (use_serconsole) {
    287 				r = RawMayGetChar();
    288 				if (r != -1) {
    289 					c = r;
    290 					ticks = 1;
    291 				}
    292 			}
    293 #endif
    294 			if (ticks == 1)
    295 				AbortIO((struct AmigaIO *)mc->cnior);
    296 		}
    297 		WaitIO((struct AmigaIO *)mc->tmior);
    298 
    299 		--ticks;
    300 	} while (ticks != 0);
    301 	timelimit = 0;
    302 
    303 	(void)WaitIO(mc->cnior);
    304 	return c;
    305 }
    306