17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
519397407SSherry Moore * Common Development and Distribution License (the "License").
619397407SSherry Moore * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /*
2207d06da5SSurya Prakki * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
24*89b43686SBayard Bell * Copyright (c) 2011 Bayard G. Bell. All rights reserved.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate * MT STREAMS Virtual Console Redirection Device Driver
307c478bd9Sstevel@tonic-gate */
317c478bd9Sstevel@tonic-gate
327c478bd9Sstevel@tonic-gate #include <sys/types.h>
337c478bd9Sstevel@tonic-gate #include <sys/param.h>
347c478bd9Sstevel@tonic-gate #include <sys/errno.h>
357c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
367c478bd9Sstevel@tonic-gate #include <sys/stat.h>
377c478bd9Sstevel@tonic-gate #include <sys/stream.h>
387c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
397c478bd9Sstevel@tonic-gate #include <sys/strsun.h>
407c478bd9Sstevel@tonic-gate #include <sys/debug.h>
417c478bd9Sstevel@tonic-gate #include <sys/thread.h>
427c478bd9Sstevel@tonic-gate #include <sys/conf.h>
437c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
447c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
457c478bd9Sstevel@tonic-gate #include <sys/tty.h>
467c478bd9Sstevel@tonic-gate #include <sys/cvc.h>
477c478bd9Sstevel@tonic-gate #include <sys/conf.h>
487c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
497c478bd9Sstevel@tonic-gate
507c478bd9Sstevel@tonic-gate
517c478bd9Sstevel@tonic-gate /*
527c478bd9Sstevel@tonic-gate * Routine to to register/unregister our queue for console output and pass
537c478bd9Sstevel@tonic-gate * redirected data to the console. The cvc driver will do a putnext using
547c478bd9Sstevel@tonic-gate * our queue, so we will not see the redirected console data.
557c478bd9Sstevel@tonic-gate */
567c478bd9Sstevel@tonic-gate extern int cvc_redir(mblk_t *);
577c478bd9Sstevel@tonic-gate extern int cvc_register(queue_t *);
587c478bd9Sstevel@tonic-gate extern int cvc_unregister(queue_t *);
597c478bd9Sstevel@tonic-gate
607c478bd9Sstevel@tonic-gate static int cvcr_info(dev_info_t *, ddi_info_cmd_t, void *, void **);
617c478bd9Sstevel@tonic-gate static int cvcr_attach(dev_info_t *, ddi_attach_cmd_t);
627c478bd9Sstevel@tonic-gate static int cvcr_detach(dev_info_t *, ddi_detach_cmd_t);
637c478bd9Sstevel@tonic-gate static int cvcr_wput(queue_t *, mblk_t *);
647c478bd9Sstevel@tonic-gate static int cvcr_open(queue_t *, dev_t *, int, int, cred_t *);
657c478bd9Sstevel@tonic-gate static int cvcr_close(queue_t *, int, cred_t *);
667c478bd9Sstevel@tonic-gate static void cvcr_ioctl(queue_t *, mblk_t *);
677c478bd9Sstevel@tonic-gate
687c478bd9Sstevel@tonic-gate static dev_info_t *cvcr_dip;
697c478bd9Sstevel@tonic-gate static int cvcr_suspend = 0;
707c478bd9Sstevel@tonic-gate
717c478bd9Sstevel@tonic-gate static struct module_info minfo = {
727c478bd9Sstevel@tonic-gate 1314, /* mi_idnum Bad luck number +1 ;-) */
737c478bd9Sstevel@tonic-gate "cvcredir", /* mi_idname */
747c478bd9Sstevel@tonic-gate 0, /* mi_minpsz */
757c478bd9Sstevel@tonic-gate INFPSZ, /* mi_maxpsz */
767c478bd9Sstevel@tonic-gate 2048, /* mi_hiwat */
777c478bd9Sstevel@tonic-gate 2048 /* mi_lowat */
787c478bd9Sstevel@tonic-gate };
797c478bd9Sstevel@tonic-gate
807c478bd9Sstevel@tonic-gate static struct qinit cvcr_rinit = {
817c478bd9Sstevel@tonic-gate NULL, /* qi_putp */
827c478bd9Sstevel@tonic-gate NULL, /* qi_srvp */
837c478bd9Sstevel@tonic-gate cvcr_open, /* qi_qopen */
847c478bd9Sstevel@tonic-gate cvcr_close, /* qi_qclose */
857c478bd9Sstevel@tonic-gate NULL, /* qi_qadmin */
867c478bd9Sstevel@tonic-gate &minfo, /* qi_minfo */
877c478bd9Sstevel@tonic-gate NULL /* qi_mstat */
887c478bd9Sstevel@tonic-gate };
897c478bd9Sstevel@tonic-gate
907c478bd9Sstevel@tonic-gate static struct qinit cvcr_winit = {
917c478bd9Sstevel@tonic-gate cvcr_wput, /* qi_putp */
927c478bd9Sstevel@tonic-gate NULL, /* qi_srvp */
937c478bd9Sstevel@tonic-gate cvcr_open, /* qi_qopen */
947c478bd9Sstevel@tonic-gate cvcr_close, /* qi_qclose */
957c478bd9Sstevel@tonic-gate NULL, /* qi_qadmin */
967c478bd9Sstevel@tonic-gate &minfo, /* qi_minfo */
977c478bd9Sstevel@tonic-gate NULL /* qi_mstat */
987c478bd9Sstevel@tonic-gate };
997c478bd9Sstevel@tonic-gate
1007c478bd9Sstevel@tonic-gate struct streamtab cvcrinfo = {
1017c478bd9Sstevel@tonic-gate &cvcr_rinit, /* st_rdinit */
1027c478bd9Sstevel@tonic-gate &cvcr_winit, /* st_wrinit */
1037c478bd9Sstevel@tonic-gate NULL, /* st_muxrinit */
1047c478bd9Sstevel@tonic-gate NULL /* st_muxwrinit */
1057c478bd9Sstevel@tonic-gate };
1067c478bd9Sstevel@tonic-gate
1077c478bd9Sstevel@tonic-gate DDI_DEFINE_STREAM_OPS(cvcrops, nulldev, nulldev, cvcr_attach,
10819397407SSherry Moore cvcr_detach, nodev, cvcr_info, (D_MTPERQ | D_MP), &cvcrinfo,
10919397407SSherry Moore ddi_quiesce_not_supported);
1107c478bd9Sstevel@tonic-gate
1117c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
1127c478bd9Sstevel@tonic-gate &mod_driverops, /* Type of module. This one is a pseudo driver */
11319397407SSherry Moore "CVC redirect driver 'cvcredir'",
1147c478bd9Sstevel@tonic-gate &cvcrops, /* driver ops */
1157c478bd9Sstevel@tonic-gate };
1167c478bd9Sstevel@tonic-gate
1177c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
1187c478bd9Sstevel@tonic-gate MODREV_1,
1197c478bd9Sstevel@tonic-gate &modldrv,
1207c478bd9Sstevel@tonic-gate NULL
1217c478bd9Sstevel@tonic-gate };
1227c478bd9Sstevel@tonic-gate
1237c478bd9Sstevel@tonic-gate int
_init(void)1247c478bd9Sstevel@tonic-gate _init(void)
1257c478bd9Sstevel@tonic-gate {
1267c478bd9Sstevel@tonic-gate return (mod_install(&modlinkage));
1277c478bd9Sstevel@tonic-gate }
1287c478bd9Sstevel@tonic-gate
1297c478bd9Sstevel@tonic-gate int
_fini(void)1307c478bd9Sstevel@tonic-gate _fini(void)
1317c478bd9Sstevel@tonic-gate {
1327c478bd9Sstevel@tonic-gate return (mod_remove(&modlinkage));
1337c478bd9Sstevel@tonic-gate }
1347c478bd9Sstevel@tonic-gate
1357c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)1367c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
1377c478bd9Sstevel@tonic-gate {
1387c478bd9Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop));
1397c478bd9Sstevel@tonic-gate }
1407c478bd9Sstevel@tonic-gate
1417c478bd9Sstevel@tonic-gate static int
cvcr_attach(dev_info_t * devi,ddi_attach_cmd_t cmd)1427c478bd9Sstevel@tonic-gate cvcr_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
1437c478bd9Sstevel@tonic-gate {
1447c478bd9Sstevel@tonic-gate #ifdef lint
1457c478bd9Sstevel@tonic-gate cvcr_suspend = cvcr_suspend;
1467c478bd9Sstevel@tonic-gate #endif
1477c478bd9Sstevel@tonic-gate if (cmd == DDI_RESUME) {
1487c478bd9Sstevel@tonic-gate cvcr_suspend = 0;
1497c478bd9Sstevel@tonic-gate } else {
1507c478bd9Sstevel@tonic-gate if (ddi_create_minor_node(devi, "cvcredir", S_IFCHR,
1517c478bd9Sstevel@tonic-gate 0, DDI_PSEUDO, NULL) == DDI_FAILURE) {
1527c478bd9Sstevel@tonic-gate ddi_remove_minor_node(devi, NULL);
1537c478bd9Sstevel@tonic-gate return (-1);
1547c478bd9Sstevel@tonic-gate }
1557c478bd9Sstevel@tonic-gate cvcr_dip = devi;
1567c478bd9Sstevel@tonic-gate }
1577c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
1587c478bd9Sstevel@tonic-gate }
1597c478bd9Sstevel@tonic-gate
1607c478bd9Sstevel@tonic-gate static int
cvcr_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)1617c478bd9Sstevel@tonic-gate cvcr_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
1627c478bd9Sstevel@tonic-gate {
1637c478bd9Sstevel@tonic-gate if (cmd == DDI_SUSPEND) {
1647c478bd9Sstevel@tonic-gate cvcr_suspend = 1;
1657c478bd9Sstevel@tonic-gate } else {
1667c478bd9Sstevel@tonic-gate if (cmd != DDI_DETACH) {
1677c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
1687c478bd9Sstevel@tonic-gate }
1697c478bd9Sstevel@tonic-gate ddi_remove_minor_node(dip, NULL);
1707c478bd9Sstevel@tonic-gate }
1717c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
1727c478bd9Sstevel@tonic-gate }
1737c478bd9Sstevel@tonic-gate
1747c478bd9Sstevel@tonic-gate /* ARGSUSED */
1757c478bd9Sstevel@tonic-gate static int
cvcr_info(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)1767c478bd9Sstevel@tonic-gate cvcr_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
1777c478bd9Sstevel@tonic-gate {
1787c478bd9Sstevel@tonic-gate register int error;
1797c478bd9Sstevel@tonic-gate
1807c478bd9Sstevel@tonic-gate switch (infocmd) {
1817c478bd9Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO:
1827c478bd9Sstevel@tonic-gate if (cvcr_dip == NULL) {
1837c478bd9Sstevel@tonic-gate error = DDI_FAILURE;
1847c478bd9Sstevel@tonic-gate } else {
1857c478bd9Sstevel@tonic-gate *result = (void *)cvcr_dip;
1867c478bd9Sstevel@tonic-gate error = DDI_SUCCESS;
1877c478bd9Sstevel@tonic-gate }
1887c478bd9Sstevel@tonic-gate break;
1897c478bd9Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE:
1907c478bd9Sstevel@tonic-gate *result = (void *)0;
1917c478bd9Sstevel@tonic-gate error = DDI_SUCCESS;
1927c478bd9Sstevel@tonic-gate break;
1937c478bd9Sstevel@tonic-gate default:
1947c478bd9Sstevel@tonic-gate error = DDI_FAILURE;
1957c478bd9Sstevel@tonic-gate }
1967c478bd9Sstevel@tonic-gate return (error);
1977c478bd9Sstevel@tonic-gate }
1987c478bd9Sstevel@tonic-gate
1997c478bd9Sstevel@tonic-gate /* ARGSUSED */
2007c478bd9Sstevel@tonic-gate static int
cvcr_open(queue_t * q,dev_t * dev,int flag,int sflag,cred_t * cred)2017c478bd9Sstevel@tonic-gate cvcr_open(queue_t *q, dev_t *dev, int flag, int sflag, cred_t *cred)
2027c478bd9Sstevel@tonic-gate {
2037c478bd9Sstevel@tonic-gate WR(q)->q_ptr = q->q_ptr = (char *)2;
2047c478bd9Sstevel@tonic-gate /*
2057c478bd9Sstevel@tonic-gate * call into the cvc driver to register our queue. cvc will use
2067c478bd9Sstevel@tonic-gate * our queue to send console output data upstream (our stream)to
2077c478bd9Sstevel@tonic-gate * cvcd which has us open and is reading console data.
2087c478bd9Sstevel@tonic-gate */
2097c478bd9Sstevel@tonic-gate if (cvc_register(RD(q)) == -1) {
21004580fdfSmathue cmn_err(CE_WARN, "cvcr_open: cvc_register failed for q = 0x%p",
21107d06da5SSurya Prakki (void *)q);
2127c478bd9Sstevel@tonic-gate }
2137c478bd9Sstevel@tonic-gate return (0);
2147c478bd9Sstevel@tonic-gate }
2157c478bd9Sstevel@tonic-gate
2167c478bd9Sstevel@tonic-gate /* ARGSUSED */
2177c478bd9Sstevel@tonic-gate static int
cvcr_close(queue_t * q,int flag,cred_t * cred)2187c478bd9Sstevel@tonic-gate cvcr_close(queue_t *q, int flag, cred_t *cred)
2197c478bd9Sstevel@tonic-gate {
2207c478bd9Sstevel@tonic-gate /*
2217c478bd9Sstevel@tonic-gate * call into the cvc driver to un-register our queue. cvc will
2227c478bd9Sstevel@tonic-gate * no longer use our queue to send console output data upstream.
2237c478bd9Sstevel@tonic-gate */
22407d06da5SSurya Prakki (void) cvc_unregister(RD(q));
2257c478bd9Sstevel@tonic-gate WR(q)->q_ptr = q->q_ptr = NULL;
2267c478bd9Sstevel@tonic-gate return (0);
2277c478bd9Sstevel@tonic-gate }
2287c478bd9Sstevel@tonic-gate
2297c478bd9Sstevel@tonic-gate static int
cvcr_wput(queue_t * q,mblk_t * mp)2307c478bd9Sstevel@tonic-gate cvcr_wput(queue_t *q, mblk_t *mp)
2317c478bd9Sstevel@tonic-gate {
2327c478bd9Sstevel@tonic-gate /*
2337c478bd9Sstevel@tonic-gate * Handle BREAK key for debugger and TIOCSWINSZ.
2347c478bd9Sstevel@tonic-gate */
2357c478bd9Sstevel@tonic-gate if (mp->b_datap->db_type == M_IOCTL) {
2367c478bd9Sstevel@tonic-gate cvcr_ioctl(q, mp);
2377c478bd9Sstevel@tonic-gate return (0);
2387c478bd9Sstevel@tonic-gate }
2397c478bd9Sstevel@tonic-gate /*
2407c478bd9Sstevel@tonic-gate * Call into the cvc driver to put console input data on
2417c478bd9Sstevel@tonic-gate * its upstream queue to be picked up by the console driver.
2427c478bd9Sstevel@tonic-gate */
2437c478bd9Sstevel@tonic-gate if (cvc_redir(mp) != 0)
2447c478bd9Sstevel@tonic-gate freemsg(mp);
2457c478bd9Sstevel@tonic-gate return (0);
2467c478bd9Sstevel@tonic-gate }
2477c478bd9Sstevel@tonic-gate
2487c478bd9Sstevel@tonic-gate static void
cvcr_ioctl(queue_t * q,mblk_t * mp)2497c478bd9Sstevel@tonic-gate cvcr_ioctl(queue_t *q, mblk_t *mp)
2507c478bd9Sstevel@tonic-gate {
2517c478bd9Sstevel@tonic-gate struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
2527c478bd9Sstevel@tonic-gate int error;
2537c478bd9Sstevel@tonic-gate
2547c478bd9Sstevel@tonic-gate switch (iocp->ioc_cmd) {
2557c478bd9Sstevel@tonic-gate case CVC_BREAK:
2567c478bd9Sstevel@tonic-gate abort_sequence_enter(NULL);
2577c478bd9Sstevel@tonic-gate miocack(q, mp, 0, 0);
2587c478bd9Sstevel@tonic-gate break;
2597c478bd9Sstevel@tonic-gate
2607c478bd9Sstevel@tonic-gate case CVC_DISCONNECT:
2617c478bd9Sstevel@tonic-gate case TIOCSWINSZ:
2627c478bd9Sstevel@tonic-gate /*
2637c478bd9Sstevel@tonic-gate * Generate a SIGHUP or SIGWINCH to the console. Note in this
2647c478bd9Sstevel@tonic-gate * case cvc_redir does not free up mp, so we can reuse it for
2657c478bd9Sstevel@tonic-gate * the ACK/NAK.
2667c478bd9Sstevel@tonic-gate */
2677c478bd9Sstevel@tonic-gate error = cvc_redir(mp);
2687c478bd9Sstevel@tonic-gate if (error != 0)
2697c478bd9Sstevel@tonic-gate miocnak(q, mp, 0, error);
2707c478bd9Sstevel@tonic-gate else
2717c478bd9Sstevel@tonic-gate miocack(q, mp, 0, 0);
2727c478bd9Sstevel@tonic-gate break;
2737c478bd9Sstevel@tonic-gate
2747c478bd9Sstevel@tonic-gate default:
2757c478bd9Sstevel@tonic-gate miocnak(q, mp, 0, EINVAL);
2767c478bd9Sstevel@tonic-gate break;
2777c478bd9Sstevel@tonic-gate }
2787c478bd9Sstevel@tonic-gate }
279