xref: /netbsd-src/sys/arch/hppa/stand/common/dev_hppa.c (revision 1c9d295ac50e01b09df0d79bf02aee4de7cc31dd)
1 /*	$NetBSD: dev_hppa.c,v 1.2 2014/03/26 17:57:17 christos Exp $	*/
2 
3 /*	$OpenBSD: dev_hppa.c,v 1.5 1999/04/20 20:01:01 mickey Exp $	*/
4 
5 /*
6  * Copyright (c) 1998-2004 Michael Shalayeff
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
22  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24  * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28  * THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  */
31 
32 #include "libsa.h"
33 #include <sys/param.h>
34 #include <sys/disklabel.h>
35 #include <sys/reboot.h>
36 #include <dev/cons.h>
37 
38 #include <machine/iomod.h>
39 
40 #include "dev_hppa.h"
41 
42 extern int debug;
43 
44 const char cdevs[][4] = {
45 	"ite", "", "", "", "", "", "", "",
46 	"", "", "", "", ""
47 };
48 const int ncdevs = NENTS(cdevs);
49 
50 const struct pdc_devs {
51 	char	name[3];
52 	int	dev_type;
53 } pdc_devs[] = {
54 	{ "dk",  0 },
55 	{ "ct",  1 },
56 	{ "lf",  2 },
57 	{ "",   -1 },
58 	{ "rd", -1 },
59 	{ "sw", -1 },
60 	{ "fl", -1 },
61 };
62 
63 /* pass dev_t to the open routines */
64 int
devopen(struct open_file * f,const char * fname,char ** file)65 devopen(struct open_file *f, const char *fname, char **file)
66 {
67 	struct hppa_dev *hpd;
68 	const struct pdc_devs *dp = pdc_devs;
69 	int bdev, badapt, bctlr, bunit, bpart;
70 	unsigned long n;
71 	char *p;
72 	int rc = 1;
73 
74 	if (!(*file = strchr(fname, ':')))
75 		return ENODEV;
76 	else
77 		(*file)++;
78 
79 #ifdef DEBUGBUG
80 	if (debug)
81 		printf("devopen: ");
82 #endif
83 
84 	for (dp = pdc_devs; dp < &pdc_devs[NENTS(pdc_devs)]; dp++)
85 		if (!strncmp(fname, dp->name, sizeof(dp->name)-1))
86 			break;
87 
88 	if (dp >= &pdc_devs[NENTS(pdc_devs)] || dp->dev_type < 0)
89 		return ENODEV;
90 	bdev = dp->dev_type;
91 	n = strtoul(fname + sizeof(dp->name)-1, &p, 10);
92 	if (n == ULONG_MAX)
93 		return ENODEV;
94 	bunit = n & 0xf;
95 	bctlr = (n >> 4) & 0xf;
96 	badapt = (n >> 8) & 0xf;
97 	if (*p >= 'a' && *p < 'a' + MAXPARTITIONS) {
98 	        bpart = *p - 'a';
99 	} else {
100 		bpart = 0;
101 	}
102 	bootdev = MAKEBOOTDEV(bdev, badapt, bctlr, bunit, bpart);
103 
104 #ifdef DEBUGBUG
105 	if (debug)
106 		printf("%s\n", dp->name);
107 #endif
108 
109 	if (!(hpd = alloc(sizeof *hpd))) {
110 #ifdef DEBUG
111 		printf ("devopen: no mem\n");
112 #endif
113 	} else {
114 		memset(hpd, 0, sizeof *hpd);
115 		hpd->bootdev = bootdev;
116 		hpd->buf = (char *)(((u_int)hpd->ua_buf + IODC_MINIOSIZ-1) &
117 			~(IODC_MINIOSIZ-1));
118 		f->f_devdata = hpd;
119 		if ((rc = (*devsw[dp->dev_type].dv_open)(f, file)) == 0) {
120 			f->f_dev = &devsw[dp->dev_type];
121 			return 0;
122 		}
123 		dealloc (hpd, 0);
124 		f->f_devdata = NULL;
125 	}
126 
127 	if (!(f->f_flags & F_NODEV))
128 		f->f_dev = &devsw[dp->dev_type];
129 
130 	if (!f->f_devdata)
131 		*file = NULL;
132 
133 	return rc;
134 }
135 
136 void
devboot(btdev_t dev,char * p)137 devboot(btdev_t dev, char *p)
138 {
139 	const char *q;
140 	if (!dev) {
141 		int type, unit;
142 
143 		switch (PAGE0->mem_boot.pz_class) {
144 		case PCL_RANDOM:
145 			type = 0;
146 			unit = PAGE0->mem_boot.pz_layers[0];
147 			break;
148 		case PCL_SEQU:
149 			type = 1;
150 			unit = PAGE0->mem_boot.pz_layers[0];
151 			break;
152 		case PCL_NET_MASK|PCL_SEQU:
153 			type = 2;
154 			unit = 0;
155 			break;
156 		default:
157 			type = 0;
158 			unit = 0;
159 			break;
160 		}
161 		dev = bootdev = MAKEBOOTDEV(type, 0, 0, unit, 0);
162 	}
163 #ifdef _TEST
164 	*p++ = '/';
165 	*p++ = 'd';
166 	*p++ = 'e';
167 	*p++ = 'v';
168 	*p++ = '/';
169 	*p++ = 'r';
170 #endif
171 	/* quick copy device name */
172 	for (q = pdc_devs[B_TYPE(dev)].name; (*p++ = *q++););
173 	p[-1] = '0' + B_UNIT(dev);
174 	*p++ = 'a' + B_PARTITION(dev);
175 	*p = '\0';
176 }
177 
178 int pch_pos;
179 
180 void
putchar(int c)181 putchar(int c)
182 {
183 	switch(c) {
184 	case '\177':	/* DEL erases */
185 		cnputc('\b');
186 		cnputc(' ');
187 	case '\b':
188 		cnputc('\b');
189 		if (pch_pos)
190 			pch_pos--;
191 		break;
192 	case '\t':
193 		do
194 			cnputc(' ');
195 		while(++pch_pos % 8);
196 		break;
197 	case '\n':
198 		/*
199 		* XXX fredette - probably only necessary
200 		* when using a serial console?
201 		*/
202 		cnputc(c);
203 		c = '\r';
204 		/* FALLTHROUGH */
205 	case '\r':
206 		cnputc(c);
207 		pch_pos=0;
208 		break;
209 	default:
210 		cnputc(c);
211 		pch_pos++;
212 		break;
213 	}
214 }
215 
216 int
getchar(void)217 getchar(void)
218 {
219 	int c = cngetc();
220 
221 	if (c == '\r')
222 		c = '\n';
223 
224 	if ((c < ' ' && c != '\n') || c == '\177')
225 		return(c);
226 
227 	/*
228 	 * XXX fredette - probably only unnecessary
229 	 * when using a serial console?
230 	 */
231 #if 0
232 	putchar(c);
233 #endif
234 
235 	return(c);
236 }
237 
238 int
tgetchar(void)239 tgetchar(void)
240 {
241 	int c;
242 
243 	if ((c = tcngetc()) == 0)
244 		return(0);
245 	return(getchar());
246 }
247 
248 #if 0
249 char ttyname_buf[8];
250 char *
251 ttyname(int fd)
252 {
253 	snprintf(ttyname_buf, sizeof(ttyname_buf), "%s%d",
254 	    cdevs[major(cn_tab->cn_dev)],
255 	    minor(cn_tab->cn_dev));
256 	return (ttyname_buf);
257 }
258 
259 dev_t
260 ttydev(char *name)
261 {
262 	int i, unit = -1;
263 	char *no = name + strlen(name) - 1;
264 
265 	while (no >= name && *no >= '0' && *no <= '9')
266 		unit = (unit < 0 ? 0 : (unit * 10)) + *no-- - '0';
267 	if (no < name || unit < 0)
268 		return (NODEV);
269 	for (i = 0; i < ncdevs; i++)
270 		if (strncmp(name, cdevs[i], no - name + 1) == 0)
271 			return (makedev(i, unit));
272 	return (NODEV);
273 }
274 #endif
275