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 /* 22*07d06da5SSurya Prakki * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate /* 287c478bd9Sstevel@tonic-gate * MT STREAMS Virtual Console Redirection Device Driver 297c478bd9Sstevel@tonic-gate */ 307c478bd9Sstevel@tonic-gate 317c478bd9Sstevel@tonic-gate #include <sys/types.h> 327c478bd9Sstevel@tonic-gate #include <sys/param.h> 337c478bd9Sstevel@tonic-gate #include <sys/errno.h> 347c478bd9Sstevel@tonic-gate #include <sys/kmem.h> 357c478bd9Sstevel@tonic-gate #include <sys/stat.h> 367c478bd9Sstevel@tonic-gate #include <sys/stream.h> 377c478bd9Sstevel@tonic-gate #include <sys/stropts.h> 387c478bd9Sstevel@tonic-gate #include <sys/strsun.h> 397c478bd9Sstevel@tonic-gate #include <sys/debug.h> 407c478bd9Sstevel@tonic-gate #include <sys/thread.h> 417c478bd9Sstevel@tonic-gate #include <sys/conf.h> 427c478bd9Sstevel@tonic-gate #include <sys/ddi.h> 437c478bd9Sstevel@tonic-gate #include <sys/sunddi.h> 447c478bd9Sstevel@tonic-gate #include <sys/tty.h> 457c478bd9Sstevel@tonic-gate #include <sys/sc_cvcio.h> 467c478bd9Sstevel@tonic-gate #include <sys/conf.h> 477c478bd9Sstevel@tonic-gate #include <sys/modctl.h> 487c478bd9Sstevel@tonic-gate 497c478bd9Sstevel@tonic-gate 507c478bd9Sstevel@tonic-gate /* 517c478bd9Sstevel@tonic-gate * Routine to to register/unregister our queue for console output and pass 527c478bd9Sstevel@tonic-gate * redirected data to the console. The cvc driver will do a putnext using 537c478bd9Sstevel@tonic-gate * our queue, so we will not see the redirected console data. 547c478bd9Sstevel@tonic-gate */ 557c478bd9Sstevel@tonic-gate extern int cvc_redir(mblk_t *); 567c478bd9Sstevel@tonic-gate extern int cvc_register(queue_t *); 577c478bd9Sstevel@tonic-gate extern int cvc_unregister(queue_t *); 587c478bd9Sstevel@tonic-gate 597c478bd9Sstevel@tonic-gate static int cvcr_info(dev_info_t *, ddi_info_cmd_t, void *, void **); 607c478bd9Sstevel@tonic-gate static int cvcr_attach(dev_info_t *, ddi_attach_cmd_t); 617c478bd9Sstevel@tonic-gate static int cvcr_detach(dev_info_t *, ddi_detach_cmd_t); 627c478bd9Sstevel@tonic-gate static int cvcr_wput(queue_t *, mblk_t *); 637c478bd9Sstevel@tonic-gate static int cvcr_open(queue_t *, dev_t *, int, int, cred_t *); 647c478bd9Sstevel@tonic-gate static int cvcr_close(queue_t *, int, cred_t *); 657c478bd9Sstevel@tonic-gate static void cvcr_ioctl(queue_t *, mblk_t *); 667c478bd9Sstevel@tonic-gate 677c478bd9Sstevel@tonic-gate static dev_info_t *cvcr_dip; 687c478bd9Sstevel@tonic-gate static int cvcr_suspend = 0; 697c478bd9Sstevel@tonic-gate 707c478bd9Sstevel@tonic-gate static struct module_info minfo = { 717c478bd9Sstevel@tonic-gate 1314, /* mi_idnum Bad luck number +1 ;-) */ 727c478bd9Sstevel@tonic-gate "cvcredir", /* mi_idname */ 737c478bd9Sstevel@tonic-gate 0, /* mi_minpsz */ 747c478bd9Sstevel@tonic-gate INFPSZ, /* mi_maxpsz */ 757c478bd9Sstevel@tonic-gate 2048, /* mi_hiwat */ 767c478bd9Sstevel@tonic-gate 2048 /* mi_lowat */ 777c478bd9Sstevel@tonic-gate }; 787c478bd9Sstevel@tonic-gate 797c478bd9Sstevel@tonic-gate static struct qinit cvcr_rinit = { 807c478bd9Sstevel@tonic-gate NULL, /* qi_putp */ 817c478bd9Sstevel@tonic-gate NULL, /* qi_srvp */ 827c478bd9Sstevel@tonic-gate cvcr_open, /* qi_qopen */ 837c478bd9Sstevel@tonic-gate cvcr_close, /* qi_qclose */ 847c478bd9Sstevel@tonic-gate NULL, /* qi_qadmin */ 857c478bd9Sstevel@tonic-gate &minfo, /* qi_minfo */ 867c478bd9Sstevel@tonic-gate NULL /* qi_mstat */ 877c478bd9Sstevel@tonic-gate }; 887c478bd9Sstevel@tonic-gate 897c478bd9Sstevel@tonic-gate static struct qinit cvcr_winit = { 907c478bd9Sstevel@tonic-gate cvcr_wput, /* qi_putp */ 917c478bd9Sstevel@tonic-gate NULL, /* qi_srvp */ 927c478bd9Sstevel@tonic-gate cvcr_open, /* qi_qopen */ 937c478bd9Sstevel@tonic-gate cvcr_close, /* qi_qclose */ 947c478bd9Sstevel@tonic-gate NULL, /* qi_qadmin */ 957c478bd9Sstevel@tonic-gate &minfo, /* qi_minfo */ 967c478bd9Sstevel@tonic-gate NULL /* qi_mstat */ 977c478bd9Sstevel@tonic-gate }; 987c478bd9Sstevel@tonic-gate 997c478bd9Sstevel@tonic-gate struct streamtab cvcrinfo = { 1007c478bd9Sstevel@tonic-gate &cvcr_rinit, /* st_rdinit */ 1017c478bd9Sstevel@tonic-gate &cvcr_winit, /* st_wrinit */ 1027c478bd9Sstevel@tonic-gate NULL, /* st_muxrinit */ 1037c478bd9Sstevel@tonic-gate NULL /* st_muxwrinit */ 1047c478bd9Sstevel@tonic-gate }; 1057c478bd9Sstevel@tonic-gate 1067c478bd9Sstevel@tonic-gate DDI_DEFINE_STREAM_OPS(cvcrops, nulldev, nulldev, cvcr_attach, 10719397407SSherry Moore cvcr_detach, nodev, cvcr_info, (D_MTPERQ|D_MP), &cvcrinfo, 10819397407SSherry Moore ddi_quiesce_not_supported); 1097c478bd9Sstevel@tonic-gate 1107c478bd9Sstevel@tonic-gate extern struct mod_ops mod_driverops; 1117c478bd9Sstevel@tonic-gate 1127c478bd9Sstevel@tonic-gate static struct modldrv modldrv = { 1137c478bd9Sstevel@tonic-gate &mod_driverops, /* Type of module. This one is a pseudo driver */ 11419397407SSherry Moore "CVC redirect driver 'cvcredir'", 1157c478bd9Sstevel@tonic-gate &cvcrops, /* driver ops */ 1167c478bd9Sstevel@tonic-gate }; 1177c478bd9Sstevel@tonic-gate 1187c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = { 1197c478bd9Sstevel@tonic-gate MODREV_1, 1207c478bd9Sstevel@tonic-gate &modldrv, 1217c478bd9Sstevel@tonic-gate NULL 1227c478bd9Sstevel@tonic-gate }; 1237c478bd9Sstevel@tonic-gate 1247c478bd9Sstevel@tonic-gate 1257c478bd9Sstevel@tonic-gate int 1267c478bd9Sstevel@tonic-gate _init() 1277c478bd9Sstevel@tonic-gate { 1287c478bd9Sstevel@tonic-gate return (mod_install(&modlinkage)); 1297c478bd9Sstevel@tonic-gate } 1307c478bd9Sstevel@tonic-gate 1317c478bd9Sstevel@tonic-gate int 1327c478bd9Sstevel@tonic-gate _fini() 1337c478bd9Sstevel@tonic-gate { 1347c478bd9Sstevel@tonic-gate return (mod_remove(&modlinkage)); 1357c478bd9Sstevel@tonic-gate } 1367c478bd9Sstevel@tonic-gate 1377c478bd9Sstevel@tonic-gate int 1387c478bd9Sstevel@tonic-gate _info(modinfop) 1397c478bd9Sstevel@tonic-gate struct modinfo *modinfop; 1407c478bd9Sstevel@tonic-gate { 1417c478bd9Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop)); 1427c478bd9Sstevel@tonic-gate } 1437c478bd9Sstevel@tonic-gate 1447c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 1457c478bd9Sstevel@tonic-gate static int 1467c478bd9Sstevel@tonic-gate cvcr_attach(dev_info_t *devi, ddi_attach_cmd_t cmd) 1477c478bd9Sstevel@tonic-gate { 1487c478bd9Sstevel@tonic-gate if (cmd == DDI_RESUME) { 1497c478bd9Sstevel@tonic-gate if (cvcr_suspend) { 1507c478bd9Sstevel@tonic-gate cvcr_suspend = 0; 1517c478bd9Sstevel@tonic-gate } 1527c478bd9Sstevel@tonic-gate } else { 1537c478bd9Sstevel@tonic-gate if (ddi_create_minor_node(devi, "cvcredir", S_IFCHR, 1547c478bd9Sstevel@tonic-gate 0, NULL, NULL) == DDI_FAILURE) { 1557c478bd9Sstevel@tonic-gate ddi_remove_minor_node(devi, NULL); 1567c478bd9Sstevel@tonic-gate return (-1); 1577c478bd9Sstevel@tonic-gate } 1587c478bd9Sstevel@tonic-gate cvcr_dip = devi; 1597c478bd9Sstevel@tonic-gate } 1607c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 1617c478bd9Sstevel@tonic-gate } 1627c478bd9Sstevel@tonic-gate 1637c478bd9Sstevel@tonic-gate static int 1647c478bd9Sstevel@tonic-gate cvcr_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 1657c478bd9Sstevel@tonic-gate { 1667c478bd9Sstevel@tonic-gate if (cmd == DDI_SUSPEND) { 1677c478bd9Sstevel@tonic-gate if (!cvcr_suspend) { 1687c478bd9Sstevel@tonic-gate cvcr_suspend = 1; 1697c478bd9Sstevel@tonic-gate } 1707c478bd9Sstevel@tonic-gate } else { 1717c478bd9Sstevel@tonic-gate if (cmd != DDI_DETACH) { 1727c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 1737c478bd9Sstevel@tonic-gate } 1747c478bd9Sstevel@tonic-gate ddi_remove_minor_node(dip, NULL); 1757c478bd9Sstevel@tonic-gate } 1767c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 1777c478bd9Sstevel@tonic-gate } 1787c478bd9Sstevel@tonic-gate 1797c478bd9Sstevel@tonic-gate /* ARGSUSED */ 1807c478bd9Sstevel@tonic-gate static int 1817c478bd9Sstevel@tonic-gate cvcr_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 1827c478bd9Sstevel@tonic-gate { 1837c478bd9Sstevel@tonic-gate register int error; 1847c478bd9Sstevel@tonic-gate 1857c478bd9Sstevel@tonic-gate switch (infocmd) { 1867c478bd9Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO: 1877c478bd9Sstevel@tonic-gate if (cvcr_dip == NULL) { 1887c478bd9Sstevel@tonic-gate error = DDI_FAILURE; 1897c478bd9Sstevel@tonic-gate } else { 1907c478bd9Sstevel@tonic-gate *result = (void *)cvcr_dip; 1917c478bd9Sstevel@tonic-gate error = DDI_SUCCESS; 1927c478bd9Sstevel@tonic-gate } 1937c478bd9Sstevel@tonic-gate break; 1947c478bd9Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE: 1957c478bd9Sstevel@tonic-gate *result = (void *)0; 1967c478bd9Sstevel@tonic-gate error = DDI_SUCCESS; 1977c478bd9Sstevel@tonic-gate break; 1987c478bd9Sstevel@tonic-gate default: 1997c478bd9Sstevel@tonic-gate error = DDI_FAILURE; 2007c478bd9Sstevel@tonic-gate } 2017c478bd9Sstevel@tonic-gate return (error); 2027c478bd9Sstevel@tonic-gate } 2037c478bd9Sstevel@tonic-gate 2047c478bd9Sstevel@tonic-gate /* ARGSUSED */ 2057c478bd9Sstevel@tonic-gate static int 2067c478bd9Sstevel@tonic-gate cvcr_open(queue_t *q, dev_t *dev, int flag, int sflag, cred_t *cred) 2077c478bd9Sstevel@tonic-gate { 2087c478bd9Sstevel@tonic-gate WR(q)->q_ptr = q->q_ptr = (char *)2; 2097c478bd9Sstevel@tonic-gate /* 2107c478bd9Sstevel@tonic-gate * call into the cvc driver to register our queue. cvc will use 2117c478bd9Sstevel@tonic-gate * our queue to send console output data upstream (our stream)to 2127c478bd9Sstevel@tonic-gate * cvcd which has us open and is reading console data. 2137c478bd9Sstevel@tonic-gate */ 2147c478bd9Sstevel@tonic-gate if (cvc_register(RD(q)) == -1) { 21504580fdfSmathue cmn_err(CE_WARN, "cvcr_open: cvc_register failed for q = 0x%p", 216*07d06da5SSurya Prakki (void *)q); 2177c478bd9Sstevel@tonic-gate } 2187c478bd9Sstevel@tonic-gate return (0); 2197c478bd9Sstevel@tonic-gate } 2207c478bd9Sstevel@tonic-gate 2217c478bd9Sstevel@tonic-gate /* ARGSUSED */ 2227c478bd9Sstevel@tonic-gate static int 2237c478bd9Sstevel@tonic-gate cvcr_close(queue_t *q, int flag, cred_t *cred) 2247c478bd9Sstevel@tonic-gate { 2257c478bd9Sstevel@tonic-gate /* 2267c478bd9Sstevel@tonic-gate * call into the cvc driver to un-register our queue. cvc will 2277c478bd9Sstevel@tonic-gate * no longer use our queue to send console output data upstream. 2287c478bd9Sstevel@tonic-gate */ 229*07d06da5SSurya Prakki (void) cvc_unregister(RD(q)); 2307c478bd9Sstevel@tonic-gate WR(q)->q_ptr = q->q_ptr = NULL; 2317c478bd9Sstevel@tonic-gate return (0); 2327c478bd9Sstevel@tonic-gate } 2337c478bd9Sstevel@tonic-gate 2347c478bd9Sstevel@tonic-gate static int 2357c478bd9Sstevel@tonic-gate cvcr_wput(queue_t *q, mblk_t *mp) 2367c478bd9Sstevel@tonic-gate { 2377c478bd9Sstevel@tonic-gate /* 2387c478bd9Sstevel@tonic-gate * Handle network-transmitted control messages 2397c478bd9Sstevel@tonic-gate */ 2407c478bd9Sstevel@tonic-gate if (mp->b_datap->db_type == M_IOCTL) { 2417c478bd9Sstevel@tonic-gate cvcr_ioctl(q, mp); 2427c478bd9Sstevel@tonic-gate return (0); 2437c478bd9Sstevel@tonic-gate } 2447c478bd9Sstevel@tonic-gate /* 2457c478bd9Sstevel@tonic-gate * Call into the cvc driver to put console input data on 2467c478bd9Sstevel@tonic-gate * its upstream queue to be picked up by the console driver. 2477c478bd9Sstevel@tonic-gate */ 2487c478bd9Sstevel@tonic-gate if (!cvc_redir(mp)) 2497c478bd9Sstevel@tonic-gate freemsg(mp); 2507c478bd9Sstevel@tonic-gate return (0); 2517c478bd9Sstevel@tonic-gate } 2527c478bd9Sstevel@tonic-gate 2537c478bd9Sstevel@tonic-gate static void 2547c478bd9Sstevel@tonic-gate cvcr_ioctl(queue_t *q, mblk_t *mp) 2557c478bd9Sstevel@tonic-gate { 2567c478bd9Sstevel@tonic-gate struct iocblk *iocp = (struct iocblk *)mp->b_rptr; 2577c478bd9Sstevel@tonic-gate 2587c478bd9Sstevel@tonic-gate /* 2597c478bd9Sstevel@tonic-gate * If this is a CVC_BREAK, drop to OBP/kmdb from here rather than 2607c478bd9Sstevel@tonic-gate * passing the mblk on to cvc. If it is a disconnect, pass it on to 2617c478bd9Sstevel@tonic-gate * cvc. Nothing else is currently supported, so NAK all others. 2627c478bd9Sstevel@tonic-gate * Note that cvc_redir doesn't free the mblk passed in, so we can 2637c478bd9Sstevel@tonic-gate * reuse it for ACKing. 2647c478bd9Sstevel@tonic-gate */ 2657c478bd9Sstevel@tonic-gate if (iocp->ioc_cmd == CVC_BREAK) { 2667c478bd9Sstevel@tonic-gate abort_sequence_enter((char *)NULL); 2677c478bd9Sstevel@tonic-gate miocack(q, mp, 0, 0); 2687c478bd9Sstevel@tonic-gate } else if (iocp->ioc_cmd == CVC_DISCONNECT) { 2697c478bd9Sstevel@tonic-gate (void) cvc_redir(mp); 2707c478bd9Sstevel@tonic-gate miocack(q, mp, 0, 0); 2717c478bd9Sstevel@tonic-gate } else { 2727c478bd9Sstevel@tonic-gate miocnak(q, mp, 0, EINVAL); 2737c478bd9Sstevel@tonic-gate } 2747c478bd9Sstevel@tonic-gate } 275