No empty .Rs/.Re
[netbsd-mini2440.git] / sys / arch / macppc / dev / ofcons.c
blob6f77f60b32b6eda35b32c1fc88fe7f6a2aa019e7
1 /* $NetBSD: ofcons.c,v 1.23 2008/06/13 11:54:31 cegger Exp $ */
3 /*
4 * Copyright (C) 1995, 1996 Wolfgang Solfrank.
5 * Copyright (C) 1995, 1996 TooLs GmbH.
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by TooLs GmbH.
19 * 4. The name of TooLs GmbH may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: ofcons.c,v 1.23 2008/06/13 11:54:31 cegger Exp $");
37 #include <sys/param.h>
38 #include <sys/conf.h>
39 #include <sys/device.h>
40 #include <sys/proc.h>
41 #include <sys/systm.h>
42 #include <sys/tty.h>
43 #include <sys/kauth.h>
45 #include <dev/cons.h>
46 #include <dev/ofw/openfirm.h>
48 #include <dev/pci/pcireg.h>
49 #include <dev/pci/pcivar.h>
51 #include <machine/adbsys.h>
53 #include <machine/autoconf.h>
55 #include "adb.h"
57 struct ofcons_softc {
58 struct device of_dev;
59 struct tty *of_tty;
62 #define OFBURSTLEN 128 /* max number of bytes to write in one chunk */
64 static int stdin, stdout;
66 static int ofcmatch(struct device *, struct cfdata *, void *);
67 static void ofcattach(struct device *, struct device *, void *);
69 CFATTACH_DECL(macofcons, sizeof(struct ofcons_softc),
70 ofcmatch, ofcattach, NULL, NULL);
72 extern struct cfdriver macofcons_cd;
74 dev_type_open(ofcopen);
75 dev_type_close(ofcclose);
76 dev_type_read(ofcread);
77 dev_type_write(ofcwrite);
78 dev_type_ioctl(ofcioctl);
79 dev_type_tty(ofctty);
80 dev_type_poll(ofcpoll);
82 const struct cdevsw macofcons_cdevsw = {
83 ofcopen, ofcclose, ofcread, ofcwrite, ofcioctl,
84 nostop, ofctty, ofcpoll, nommap, ttykqfilter, D_TTY
87 /* For polled ADB mode */
88 #if NADB > 0
89 static int polledkey;
90 extern int adb_polling;
91 #endif /* NADB */
93 static void ofcstart(struct tty *);
94 static int ofcparam(struct tty *, struct termios *);
95 static int ofcons_probe(void);
97 static int
98 ofcmatch(struct device *parent, struct cfdata *match, void *aux)
100 struct pci_attach_args *pa = aux;
101 static int attached = 0;
103 if (attached)
104 return 0;
106 if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY)
107 return 0;
109 if (!ofcons_probe())
110 return 0;
112 attached = 1;
113 return 1;
116 static void
117 ofcattach(struct device *parent, struct device *self, void *aux)
119 printf("\n");
123 ofcopen(dev_t dev, int flag, int mode, struct lwp *l)
125 struct ofcons_softc *sc;
126 struct tty *tp;
128 sc = device_lookup_private(&macofcons_cd, minor(dev));
129 if (!sc)
130 return ENXIO;
131 if (!(tp = sc->of_tty))
132 sc->of_tty = tp = ttymalloc();
133 tp->t_oproc = ofcstart;
134 tp->t_param = ofcparam;
135 tp->t_dev = dev;
136 if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
137 return (EBUSY);
138 if (!(tp->t_state & TS_ISOPEN)) {
139 ttychars(tp);
140 tp->t_iflag = TTYDEF_IFLAG;
141 tp->t_oflag = TTYDEF_OFLAG;
142 tp->t_cflag = TTYDEF_CFLAG;
143 tp->t_lflag = TTYDEF_LFLAG;
144 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
145 ofcparam(tp, &tp->t_termios);
146 ttsetwater(tp);
148 tp->t_state |= TS_CARR_ON;
150 return (*tp->t_linesw->l_open)(dev, tp);
154 ofcclose(dev_t dev, int flag, int mode, struct lwp *l)
156 struct ofcons_softc *sc = device_lookup_private(&macofcons_cd, minor(dev));
157 struct tty *tp = sc->of_tty;
159 (*tp->t_linesw->l_close)(tp, flag);
160 ttyclose(tp);
161 return 0;
165 ofcread(dev_t dev, struct uio *uio, int flag)
167 struct ofcons_softc *sc = device_lookup_private(&macofcons_cd, minor(dev));
168 struct tty *tp = sc->of_tty;
170 return (*tp->t_linesw->l_read)(tp, uio, flag);
174 ofcwrite(dev_t dev, struct uio *uio, int flag)
176 struct ofcons_softc *sc = device_lookup_private(&macofcons_cd, minor(dev));
177 struct tty *tp = sc->of_tty;
179 return (*tp->t_linesw->l_write)(tp, uio, flag);
183 ofcpoll(dev_t dev, int events, struct lwp *l)
185 struct ofcons_softc *sc = device_lookup_private(&macofcons_cd, minor(dev));
186 struct tty *tp = sc->of_tty;
188 return ((*tp->t_linesw->l_poll)(tp, events, l));
192 ofcioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
194 struct ofcons_softc *sc = device_lookup_private(&macofcons_cd, minor(dev));
195 struct tty *tp = sc->of_tty;
196 int error;
198 if ((error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l)) != EPASSTHROUGH)
199 return error;
200 return ttioctl(tp, cmd, data, flag, l);
203 struct tty *
204 ofctty(dev_t dev)
206 struct ofcons_softc *sc = device_lookup_private(&macofcons_cd, minor(dev));
208 return sc->of_tty;
211 static void
212 ofcstart(struct tty *tp)
214 struct clist *cl;
215 int s, len;
216 u_char buf[OFBURSTLEN];
218 s = spltty();
219 if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) {
220 splx(s);
221 return;
223 tp->t_state |= TS_BUSY;
224 splx(s);
225 cl = &tp->t_outq;
226 len = q_to_b(cl, buf, OFBURSTLEN);
227 OF_write(stdout, buf, len);
228 s = spltty();
229 tp->t_state &= ~TS_BUSY;
230 if (ttypull(tp)) {
231 tp->t_state |= TS_TIMEOUT;
232 callout_schedule(&tp->t_rstrt_ch, 1);
234 splx(s);
237 static int
238 ofcparam(struct tty *tp, struct termios *t)
240 tp->t_ispeed = t->c_ispeed;
241 tp->t_ospeed = t->c_ospeed;
242 tp->t_cflag = t->c_cflag;
243 return 0;
246 static int
247 ofcons_probe(void)
249 int chosen;
251 if (stdout)
252 return 1;
253 if ((chosen = OF_finddevice("/chosen")) == -1)
254 return 0;
255 if (OF_getprop(chosen, "stdin", &stdin, sizeof(stdin)) !=
256 sizeof(stdin) ||
257 OF_getprop(chosen, "stdout", &stdout, sizeof(stdout)) !=
258 sizeof(stdout))
259 return 0;
261 return 1;
265 * Console support functions
267 static void
268 ofccnprobe(struct consdev *cd)
270 int maj;
272 if (!ofcons_probe())
273 return;
275 maj = cdevsw_lookup_major(&macofcons_cdevsw);
277 printf("major for ofcons: %d\n", maj);
279 cd->cn_dev = makedev(maj, 0);
280 cd->cn_pri = CN_INTERNAL;
283 static void
284 ofccninit(struct consdev *cd)
288 static int
289 ofccngetc(dev_t dev)
291 #if NADB > 0
292 int s;
293 extern void adb_intr_cuda(void); /* in adb_direct.c */
295 s = splhigh();
297 polledkey = -1;
298 adb_polling = 1;
300 while (polledkey == -1)
301 adb_intr_cuda();
303 adb_polling = 0;
305 splx(s);
306 return polledkey;
307 #else
308 unsigned char ch = '\0';
309 int l;
311 while ((l = OF_read(stdin, &ch, 1)) != 1)
312 if (l != -2 && l != 0)
313 return -1;
314 return ch;
315 #endif
318 static void
319 ofccnputc(dev_t dev, int c)
321 char ch = c;
323 OF_write(stdout, &ch, 1);
326 static void
327 ofccnpollc(dev_t dev, int on)
331 struct consdev consdev_ofcons = {
332 ofccnprobe,
333 ofccninit,
334 ofccngetc,
335 ofccnputc,
336 ofccnpollc,
337 NULL,
340 struct consdev *cn_tab = &consdev_ofcons;