Home | History | Annotate | Line # | Download | only in boot
console.c revision 1.9
      1 /* $NetBSD: console.c,v 1.9 2009/03/14 21:04:04 dsl 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 int
     75 consinit(void *consptr) {
     76 	struct Console *mc;
     77 
     78 	if (consptr != NULL) {
     79 		/* Check magic? */
     80 		ConsoleBase = consptr;		/* Use existing console */
     81 		return (0);
     82 	}
     83 
     84 	mc = &myConsole;
     85 	IntuitionBase = OpenLibrary("intuition.library", 36L);
     86 	if (IntuitionBase == 0)
     87 		goto err;
     88 
     89 	mc->s = OpenScreenTagList(0, screentags);
     90 	if (!mc->s)
     91 		goto err;
     92 
     93 	windowtags[1] = (u_int32_t)mc->s;
     94 	mc->w = OpenWindowTagList(0, windowtags);
     95 	if (!mc->w)
     96 		goto err;
     97 
     98 	mc->cnmp = CreateMsgPort();
     99 
    100 	if (!mc->cnmp)
    101 		goto err;
    102 
    103 	mc->cnior = (struct AmigaIO *)CreateIORequest(mc->cnmp, sizeof(struct AmigaIO));
    104 	if (!mc->cnior)
    105 		goto err;
    106 
    107 	mc->cnior->buf = (void *)mc->w;
    108 	if (OpenDevice("console.device", 0, mc->cnior, 0))
    109 		goto err;
    110 
    111 	mc->tmior = (struct TimerIO *)CreateIORequest(mc->cnmp, sizeof(struct TimerIO));
    112 	if (!mc->tmior)
    113 		goto err;
    114 
    115 	if (OpenDevice("timer.device", 0, (struct AmigaIO*)mc->tmior, 0))
    116 		goto err;
    117 
    118 	ConsoleBase = mc;
    119 	return 0;
    120 
    121 err:
    122 #ifdef notyet
    123 	if (mc->tmior)
    124 		DeleteIORequest(mc->tmior);
    125 
    126 	if (mc->cnior)
    127 		DeleteIORequest(mc->cnior);
    128 
    129 	if (mc->cnmp)
    130 		DeleteMsgPort(mc->cnmp);
    131 
    132 	if (mc->w)
    133 		CloseWindow(mc->w);
    134 
    135 	if (mc->s)
    136 		CloseScreen(mc->s);
    137 	if (IntuitionBase)
    138 		CloseLibrary(IntuitionBase);
    139 #endif
    140 
    141 	return 1;
    142 }
    143 
    144 #ifdef _PRIMARY_BOOT
    145 int
    146 consclose()
    147 {
    148 	struct Console *mc = ConsoleBase;
    149 
    150 	if (mc == NULL)
    151 		return 0;
    152 	if (mc->tmior) {
    153 		CloseDevice((struct AmigaIO *)mc->tmior);
    154 		DeleteIORequest(mc->tmior);
    155 	}
    156 
    157 	if (mc->cnior) {
    158 		CloseDevice(mc->cnior);
    159 		DeleteIORequest(mc->cnior);
    160 	}
    161 
    162 	if (mc->cnmp)
    163 		DeleteMsgPort(mc->cnmp);
    164 
    165 	if (mc->w)
    166 		CloseWindow(mc->w);
    167 
    168 	if (mc->s)
    169 		CloseScreen(mc->s);
    170 	if (IntuitionBase)
    171 		CloseLibrary(IntuitionBase);
    172 	ConsoleBase = NULL;
    173 	return 0;
    174 }
    175 #endif
    176 
    177 void
    178 putchar(char c)
    179 {
    180 	struct Console *mc = ConsoleBase;
    181 
    182 	mc->cnior->length = 1;
    183 	mc->cnior->buf = &c;
    184 	mc->cnior->cmd = Cmd_Wr;
    185 	(void)DoIO(mc->cnior);
    186 }
    187 
    188 void
    189 puts(char *s)
    190 {
    191 	struct Console *mc = ConsoleBase;
    192 
    193 	mc->cnior->length = -1;
    194 	mc->cnior->buf = s;
    195 	mc->cnior->cmd = Cmd_Wr;
    196 	(void)DoIO(mc->cnior);
    197 }
    198 
    199 int
    200 getchar()
    201 {
    202 	struct AmigaIO *ior;
    203 	char c = -1;
    204 	struct Console *mc = ConsoleBase;
    205 
    206 	mc->cnior->length = 1;
    207 	mc->cnior->buf = &c;
    208 	mc->cnior->cmd = Cmd_Rd;
    209 
    210 	SendIO(mc->cnior);
    211 
    212 	if (timelimit) {
    213 		mc->tmior->cmd = Cmd_Addtimereq;
    214 		mc->tmior->secs = timelimit;
    215 		mc->tmior->usec = 2; /* Paranoid */
    216 		SendIO((struct AmigaIO *)mc->tmior);
    217 
    218 		ior = WaitPort(mc->cnmp);
    219 		if (ior == mc->cnior)
    220 			AbortIO((struct AmigaIO *)mc->tmior);
    221 		else /* if (ior == mc->tmior) */ {
    222 			AbortIO(mc->cnior);
    223 			c = '\n';
    224 		}
    225 		WaitIO((struct AmigaIO *)mc->tmior);
    226 		timelimit = 0;
    227 	}
    228 	(void)WaitIO(mc->cnior);
    229 	return c;
    230 }
    231