1 /* $NetBSD: cons.c,v 1.3 2011/02/08 20:20:23 rmind Exp $ */
2
3 /*
4 * Copyright (c) 1988 University of Utah.
5 * Copyright (c) 1990, 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * the Systems Programming Group of the University of Utah Computer
10 * Science Department.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * from: Utah Hdr: cons.c 1.7 92/02/28
37 *
38 * @(#)cons.c 8.1 (Berkeley) 6/10/93
39 */
40
41 #include <lib/libsa/stand.h>
42
43 #include "boot.h"
44 #include "cons.h"
45
46 #ifdef CONS_SERIAL
47 void siocnprobe(struct consdev *);
48 void siocninit(struct consdev *);
49 void siocnputchar(void *, int);
50 int siocngetchar(void *);
51 int siocnscan(void *);
52 # include "ns16550.h"
53 # ifndef COMPORT
54 # define COMPORT COM1
55 # endif
56 # ifndef COMSPEED
57 # define COMSPEED 9600
58 # endif
59 #endif
60
61 struct consdev constab[] = {
62 #ifdef CONS_SERIAL
63 { "com", COMPORT, COMSPEED,
64 siocnprobe, siocninit, siocngetchar, siocnputchar, siocnscan },
65 #endif
66 { 0 }
67 };
68
69 struct consdev *cn_tab;
70
71 char *
cninit(int * addr,int * speed)72 cninit(int *addr, int *speed)
73 {
74 register struct consdev *cp;
75
76 cn_tab = NULL;
77 for (cp = constab; cp->cn_probe; cp++) {
78 (*cp->cn_probe)(cp);
79 if (cp->cn_pri > CN_DEAD &&
80 (cn_tab == NULL || cp->cn_pri > cn_tab->cn_pri))
81 cn_tab = cp;
82 }
83 if (cn_tab) {
84 (*cn_tab->cn_init)(cn_tab);
85 *addr = cn_tab->address;
86 *speed = cn_tab->speed;
87 return (cn_tab->cn_name);
88 }
89
90 return (NULL);
91 }
92
93 int
cngetc(void)94 cngetc(void)
95 {
96
97 if (cn_tab)
98 return ((*cn_tab->cn_getc)(cn_tab->cn_dev));
99 return (0);
100 }
101
102 void
cnputc(int c)103 cnputc(int c)
104 {
105
106 if (cn_tab)
107 (*cn_tab->cn_putc)(cn_tab->cn_dev, c);
108 }
109
110 int
cnscan(void)111 cnscan(void)
112 {
113
114 if (cn_tab)
115 return ((*cn_tab->cn_scan)(cn_tab->cn_dev));
116 return -1;
117 }
118
119 #ifdef CONS_SERIAL
120 /*
121 * serial console
122 */
123 void
siocnprobe(struct consdev * cp)124 siocnprobe(struct consdev *cp)
125 {
126
127 cp->cn_pri = CN_REMOTE;
128 }
129
130 void
siocninit(struct consdev * cp)131 siocninit(struct consdev *cp)
132 {
133
134 cp->cn_dev = (void *)NS16550_init(cp->address, cp->speed);
135 }
136
137 int
siocngetchar(void * dev)138 siocngetchar(void *dev)
139 {
140
141 return (NS16550_getc((struct NS16550 *)dev));
142 }
143
144 void
siocnputchar(void * dev,register int c)145 siocnputchar(void *dev, register int c)
146 {
147
148 if (c == '\n')
149 NS16550_putc((struct NS16550 *)dev, '\r');
150 NS16550_putc((struct NS16550 *)dev, c);
151 }
152
153 int
siocnscan(void * dev)154 siocnscan(void *dev)
155 {
156
157 return (NS16550_scankbd((struct NS16550 *)dev));
158 }
159 #endif /* CONS_SERIAL */
160