DEV messaging stage 1/4: Rearrange struct cdevsw and add a message port
[dragonfly.git] / sys / kern / tty_cons.c
1 /*
2  * Copyright (c) 1988 University of Utah.
3  * Copyright (c) 1991 The Regents of the University of California.
4  * All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * the Systems Programming Group of the University of Utah Computer
8  * Science Department.
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  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *      This product includes software developed by the University of
21  *      California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *      from: @(#)cons.c        7.2 (Berkeley) 5/9/91
39  * $FreeBSD: src/sys/kern/tty_cons.c,v 1.81.2.4 2001/12/17 18:44:41 guido Exp $
40  * $DragonFly: src/sys/kern/tty_cons.c,v 1.5 2003/07/21 05:50:43 dillon Exp $
41  */
42
43 #include "opt_ddb.h"
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/conf.h>
48 #include <sys/cons.h>
49 #include <sys/kernel.h>
50 #include <sys/proc.h>
51 #include <sys/reboot.h>
52 #include <sys/sysctl.h>
53 #include <sys/tty.h>
54 #include <sys/uio.h>
55
56 #include <ddb/ddb.h>
57
58 #include <machine/cpu.h>
59
60 static  d_open_t        cnopen;
61 static  d_close_t       cnclose;
62 static  d_read_t        cnread;
63 static  d_write_t       cnwrite;
64 static  d_ioctl_t       cnioctl;
65 static  d_poll_t        cnpoll;
66 static  d_kqfilter_t    cnkqfilter;
67
68 #define CDEV_MAJOR      0
69 static struct cdevsw cn_cdevsw = {
70         /* name */      "console",
71         /* maj */       CDEV_MAJOR,
72         /* flags */     D_TTY | D_KQFILTER,
73         /* port */      NULL,
74         /* autoq */     0,
75
76         /* open */      cnopen,
77         /* close */     cnclose,
78         /* read */      cnread,
79         /* write */     cnwrite,
80         /* ioctl */     cnioctl,
81         /* poll */      cnpoll,
82         /* mmap */      nommap,
83         /* strategy */  nostrategy,
84         /* dump */      nodump,
85         /* psize */     nopsize,
86         /* kqfilter */  cnkqfilter
87 };
88
89 static dev_t    cn_dev_t;       /* seems to be never really used */
90 static udev_t   cn_udev_t;
91 SYSCTL_OPAQUE(_machdep, CPU_CONSDEV, consdev, CTLFLAG_RD,
92         &cn_udev_t, sizeof cn_udev_t, "T,dev_t", "");
93
94 static int cn_mute;
95
96 int     cons_unavail = 0;       /* XXX:
97                                  * physical console not available for
98                                  * input (i.e., it is in graphics mode)
99                                  */
100
101 static u_char cn_is_open;               /* nonzero if logical console is open */
102 static int openmode, openflag;          /* how /dev/console was openned */
103 static dev_t cn_devfsdev;               /* represents the device private info */
104 static u_char cn_phys_is_open;          /* nonzero if physical device is open */
105 static d_close_t *cn_phys_close;        /* physical device close function */
106 static d_open_t *cn_phys_open;          /* physical device open function */
107        struct consdev *cn_tab;          /* physical console device info */
108 static u_char console_pausing;          /* pause after each line during probe */
109 static char *console_pausestr=
110 "<pause; press any key to proceed to next line or '.' to end pause mode>";
111
112 CONS_DRIVER(cons, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
113
114 void
115 cninit()
116 {
117         struct consdev *best_cp, *cp, **list;
118
119         /*
120          * Find the first console with the highest priority.
121          */
122         best_cp = NULL;
123         list = (struct consdev **)cons_set.ls_items;
124         while ((cp = *list++) != NULL) {
125                 if (cp->cn_probe == NULL)
126                         continue;
127                 (*cp->cn_probe)(cp);
128                 if (cp->cn_pri > CN_DEAD &&
129                     (best_cp == NULL || cp->cn_pri > best_cp->cn_pri))
130                         best_cp = cp;
131         }
132
133         /*
134          * Check if we should mute the console (for security reasons perhaps)
135          * It can be changes dynamically using sysctl kern.consmute
136          * once we are up and going.
137          * 
138          */
139         cn_mute = ((boothowto & (RB_MUTE
140                         |RB_SINGLE
141                         |RB_VERBOSE
142                         |RB_ASKNAME
143                         |RB_CONFIG)) == RB_MUTE);
144         
145         /*
146          * If no console, give up.
147          */
148         if (best_cp == NULL) {
149                 if (cn_tab != NULL && cn_tab->cn_term != NULL)
150                         (*cn_tab->cn_term)(cn_tab);
151                 cn_tab = best_cp;
152                 return;
153         }
154
155         /*
156          * Initialize console, then attach to it.  This ordering allows
157          * debugging using the previous console, if any.
158          */
159         (*best_cp->cn_init)(best_cp);
160         if (cn_tab != NULL && cn_tab != best_cp) {
161                 /* Turn off the previous console.  */
162                 if (cn_tab->cn_term != NULL)
163                         (*cn_tab->cn_term)(cn_tab);
164         }
165         if (boothowto & RB_PAUSE)
166                 console_pausing = 1;
167         cn_tab = best_cp;
168 }
169
170 void
171 cninit_finish()
172 {
173         struct cdevsw *cdp;
174
175         if ((cn_tab == NULL) || cn_mute)
176                 return;
177
178         /*
179          * Hook the open and close functions.
180          */
181         cdp = devsw(cn_tab->cn_dev);
182         if (cdp != NULL) {
183                 cn_phys_close = cdp->d_close;
184                 cdp->d_close = cnclose;
185                 cn_phys_open = cdp->d_open;
186                 cdp->d_open = cnopen;
187         }
188         cn_dev_t = cn_tab->cn_dev;
189         cn_udev_t = dev2udev(cn_dev_t);
190         console_pausing = 0;
191 }
192
193 static void
194 cnuninit(void)
195 {
196         struct cdevsw *cdp;
197
198         if (cn_tab == NULL)
199                 return;
200
201         /*
202          * Unhook the open and close functions.
203          */
204         cdp = devsw(cn_tab->cn_dev);
205         if (cdp != NULL) {
206                 cdp->d_close = cn_phys_close;
207                 cdp->d_open = cn_phys_open;
208         }
209         cn_phys_close = NULL;
210         cn_phys_open = NULL;
211         cn_dev_t = NODEV;
212         cn_udev_t = NOUDEV;
213 }
214
215 /*
216  * User has changed the state of the console muting.
217  * This may require us to open or close the device in question.
218  */
219 static int
220 sysctl_kern_consmute(SYSCTL_HANDLER_ARGS)
221 {
222         int error;
223         int ocn_mute;
224
225         ocn_mute = cn_mute;
226         error = sysctl_handle_int(oidp, &cn_mute, 0, req);
227         if((error == 0) && (cn_tab != NULL) && (req->newptr != NULL)) {
228                 if(ocn_mute && !cn_mute) {
229                         /*
230                          * going from muted to unmuted.. open the physical dev 
231                          * if the console has been openned
232                          */
233                         cninit_finish();
234                         if(cn_is_open)
235                                 /* XXX curproc is not what we want really */
236                                 error = cnopen(cn_dev_t, openflag,
237                                         openmode, curthread);
238                         /* if it failed, back it out */
239                         if ( error != 0) cnuninit();
240                 } else if (!ocn_mute && cn_mute) {
241                         /*
242                          * going from unmuted to muted.. close the physical dev 
243                          * if it's only open via /dev/console
244                          */
245                         if(cn_is_open)
246                                 error = cnclose(cn_dev_t, openflag,
247                                         openmode, curthread);
248                         if ( error == 0) cnuninit();
249                 }
250                 if (error != 0) {
251                         /* 
252                          * back out the change if there was an error
253                          */
254                         cn_mute = ocn_mute;
255                 }
256         }
257         return (error);
258 }
259
260 SYSCTL_PROC(_kern, OID_AUTO, consmute, CTLTYPE_INT|CTLFLAG_RW,
261         0, sizeof cn_mute, sysctl_kern_consmute, "I", "");
262
263 static int
264 cnopen(dev, flag, mode, td)
265         dev_t dev;
266         int flag, mode;
267         struct thread *td;
268 {
269         dev_t cndev, physdev;
270         int retval = 0;
271
272         if (cn_tab == NULL || cn_phys_open == NULL)
273                 return (0);
274         cndev = cn_tab->cn_dev;
275         physdev = (major(dev) == major(cndev) ? dev : cndev);
276         /*
277          * If mute is active, then non console opens don't get here
278          * so we don't need to check for that. They 
279          * bypass this and go straight to the device.
280          */
281         if(!cn_mute)
282                 retval = (*cn_phys_open)(physdev, flag, mode, td);
283         if (retval == 0) {
284                 /* 
285                  * check if we openned it via /dev/console or 
286                  * via the physical entry (e.g. /dev/sio0).
287                  */
288                 if (dev == cndev)
289                         cn_phys_is_open = 1;
290                 else if (physdev == cndev) {
291                         openmode = mode;
292                         openflag = flag;
293                         cn_is_open = 1;
294                 }
295                 dev->si_tty = physdev->si_tty;
296         }
297         return (retval);
298 }
299
300 static int
301 cnclose(dev, flag, mode, td)
302         dev_t dev;
303         int flag, mode;
304         struct thread *td;
305 {
306         dev_t cndev;
307         struct tty *cn_tp;
308
309         if (cn_tab == NULL || cn_phys_open == NULL)
310                 return (0);
311         cndev = cn_tab->cn_dev;
312         cn_tp = cndev->si_tty;
313         /*
314          * act appropriatly depending on whether it's /dev/console
315          * or the pysical device (e.g. /dev/sio) that's being closed.
316          * in either case, don't actually close the device unless
317          * both are closed.
318          */
319         if (dev == cndev) {
320                 /* the physical device is about to be closed */
321                 cn_phys_is_open = 0;
322                 if (cn_is_open) {
323                         if (cn_tp) {
324                                 /* perform a ttyhalfclose() */
325                                 /* reset session and proc group */
326                                 cn_tp->t_pgrp = NULL;
327                                 cn_tp->t_session = NULL;
328                         }
329                         return (0);
330                 }
331         } else if (major(dev) != major(cndev)) {
332                 /* the logical console is about to be closed */
333                 cn_is_open = 0;
334                 if (cn_phys_is_open)
335                         return (0);
336                 dev = cndev;
337         }
338         if(cn_phys_close)
339                 return ((*cn_phys_close)(dev, flag, mode, td));
340         return (0);
341 }
342
343 static int
344 cnread(dev, uio, flag)
345         dev_t dev;
346         struct uio *uio;
347         int flag;
348 {
349
350         if (cn_tab == NULL || cn_phys_open == NULL)
351                 return (0);
352         dev = cn_tab->cn_dev;
353         return ((*devsw(dev)->d_read)(dev, uio, flag));
354 }
355
356 static int
357 cnwrite(dev, uio, flag)
358         dev_t dev;
359         struct uio *uio;
360         int flag;
361 {
362
363         if (cn_tab == NULL || cn_phys_open == NULL) {
364                 uio->uio_resid = 0; /* dump the data */
365                 return (0);
366         }
367         if (constty)
368                 dev = constty->t_dev;
369         else
370                 dev = cn_tab->cn_dev;
371         log_console(uio);
372         return ((*devsw(dev)->d_write)(dev, uio, flag));
373 }
374
375 static int
376 cnioctl(dev, cmd, data, flag, td)
377         dev_t dev;
378         u_long cmd;
379         caddr_t data;
380         int flag;
381         struct thread *td;
382 {
383         int error;
384
385         if (cn_tab == NULL || cn_phys_open == NULL)
386                 return (0);
387         KKASSERT(td->td_proc != NULL);
388         /*
389          * Superuser can always use this to wrest control of console
390          * output from the "virtual" console.
391          */
392         if (cmd == TIOCCONS && constty) {
393                 error = suser(td);
394                 if (error)
395                         return (error);
396                 constty = NULL;
397                 return (0);
398         }
399         dev = cn_tab->cn_dev;
400         return ((*devsw(dev)->d_ioctl)(dev, cmd, data, flag, td));
401 }
402
403 static int
404 cnpoll(dev, events, td)
405         dev_t dev;
406         int events;
407         struct thread *td;
408 {
409         if ((cn_tab == NULL) || cn_mute)
410                 return (1);
411
412         dev = cn_tab->cn_dev;
413
414         return ((*devsw(dev)->d_poll)(dev, events, td));
415 }
416
417 static int
418 cnkqfilter(dev, kn)
419         dev_t dev;
420         struct knote *kn;
421 {
422         if ((cn_tab == NULL) || cn_mute)
423                 return (1);
424
425         dev = cn_tab->cn_dev;
426         if (devsw(dev)->d_flags & D_KQFILTER)
427                 return ((*devsw(dev)->d_kqfilter)(dev, kn));
428         return (1);
429 }
430
431 int
432 cngetc()
433 {
434         int c;
435         if ((cn_tab == NULL) || cn_mute)
436                 return (-1);
437         c = (*cn_tab->cn_getc)(cn_tab->cn_dev);
438         if (c == '\r') c = '\n'; /* console input is always ICRNL */
439         return (c);
440 }
441
442 int
443 cncheckc()
444 {
445         if ((cn_tab == NULL) || cn_mute)
446                 return (-1);
447         return ((*cn_tab->cn_checkc)(cn_tab->cn_dev));
448 }
449
450 void
451 cnputc(c)
452         register int c;
453 {
454         char *cp;
455
456         if ((cn_tab == NULL) || cn_mute)
457                 return;
458         if (c) {
459                 if (c == '\n')
460                         (*cn_tab->cn_putc)(cn_tab->cn_dev, '\r');
461                 (*cn_tab->cn_putc)(cn_tab->cn_dev, c);
462 #ifdef DDB
463                 if (console_pausing && !db_active && (c == '\n')) {
464 #else
465                 if (console_pausing && (c == '\n')) {
466 #endif
467                         for(cp=console_pausestr; *cp != '\0'; cp++)
468                             (*cn_tab->cn_putc)(cn_tab->cn_dev, *cp);
469                         if (cngetc() == '.')
470                                 console_pausing = 0;
471                         (*cn_tab->cn_putc)(cn_tab->cn_dev, '\r');
472                         for(cp=console_pausestr; *cp != '\0'; cp++)
473                             (*cn_tab->cn_putc)(cn_tab->cn_dev, ' ');
474                         (*cn_tab->cn_putc)(cn_tab->cn_dev, '\r');
475                 }
476         }
477 }
478
479 void
480 cndbctl(on)
481         int on;
482 {
483         static int refcount;
484
485         if (cn_tab == NULL)
486                 return;
487         if (!on)
488                 refcount--;
489         if (refcount == 0 && cn_tab->cn_dbctl != NULL)
490                 (*cn_tab->cn_dbctl)(cn_tab->cn_dev, on);
491         if (on)
492                 refcount++;
493 }
494
495 static void
496 cn_drvinit(void *unused)
497 {
498
499         cn_devfsdev = make_dev(&cn_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600,
500             "console");
501 }
502
503 SYSINIT(cndev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,cn_drvinit,NULL)