1 /* $NetBSD: ofcons.c,v 1.23 2008/06/13 11:54:31 cegger Exp $ */
4 * Copyright (C) 1995, 1996 Wolfgang Solfrank.
5 * Copyright (C) 1995, 1996 TooLs GmbH.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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>
39 #include <sys/device.h>
41 #include <sys/systm.h>
43 #include <sys/kauth.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>
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
);
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 */
90 extern int adb_polling
;
93 static void ofcstart(struct tty
*);
94 static int ofcparam(struct tty
*, struct termios
*);
95 static int ofcons_probe(void);
98 ofcmatch(struct device
*parent
, struct cfdata
*match
, void *aux
)
100 struct pci_attach_args
*pa
= aux
;
101 static int attached
= 0;
106 if (PCI_CLASS(pa
->pa_class
) != PCI_CLASS_DISPLAY
)
117 ofcattach(struct device
*parent
, struct device
*self
, void *aux
)
123 ofcopen(dev_t dev
, int flag
, int mode
, struct lwp
*l
)
125 struct ofcons_softc
*sc
;
128 sc
= device_lookup_private(&macofcons_cd
, minor(dev
));
131 if (!(tp
= sc
->of_tty
))
132 sc
->of_tty
= tp
= ttymalloc();
133 tp
->t_oproc
= ofcstart
;
134 tp
->t_param
= ofcparam
;
136 if (kauth_authorize_device_tty(l
->l_cred
, KAUTH_DEVICE_TTY_OPEN
, tp
))
138 if (!(tp
->t_state
& TS_ISOPEN
)) {
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
);
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
);
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
;
198 if ((error
= (*tp
->t_linesw
->l_ioctl
)(tp
, cmd
, data
, flag
, l
)) != EPASSTHROUGH
)
200 return ttioctl(tp
, cmd
, data
, flag
, l
);
206 struct ofcons_softc
*sc
= device_lookup_private(&macofcons_cd
, minor(dev
));
212 ofcstart(struct tty
*tp
)
216 u_char buf
[OFBURSTLEN
];
219 if (tp
->t_state
& (TS_TIMEOUT
| TS_BUSY
| TS_TTSTOP
)) {
223 tp
->t_state
|= TS_BUSY
;
226 len
= q_to_b(cl
, buf
, OFBURSTLEN
);
227 OF_write(stdout
, buf
, len
);
229 tp
->t_state
&= ~TS_BUSY
;
231 tp
->t_state
|= TS_TIMEOUT
;
232 callout_schedule(&tp
->t_rstrt_ch
, 1);
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
;
253 if ((chosen
= OF_finddevice("/chosen")) == -1)
255 if (OF_getprop(chosen
, "stdin", &stdin
, sizeof(stdin
)) !=
257 OF_getprop(chosen
, "stdout", &stdout
, sizeof(stdout
)) !=
265 * Console support functions
268 ofccnprobe(struct consdev
*cd
)
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
;
284 ofccninit(struct consdev
*cd
)
293 extern void adb_intr_cuda(void); /* in adb_direct.c */
300 while (polledkey
== -1)
308 unsigned char ch
= '\0';
311 while ((l
= OF_read(stdin
, &ch
, 1)) != 1)
312 if (l
!= -2 && l
!= 0)
319 ofccnputc(dev_t dev
, int c
)
323 OF_write(stdout
, &ch
, 1);
327 ofccnpollc(dev_t dev
, int on
)
331 struct consdev consdev_ofcons
= {
340 struct consdev
*cn_tab
= &consdev_ofcons
;