2 * Copyright (c) 1982, 1986, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
35 * Pseudo-nulmodem driver
36 * Mighty handy for use with serial console in Vmware
39 #include "opt_compat.h"
42 #include <sys/param.h>
43 #include <sys/systm.h>
47 #include <sys/fcntl.h>
49 #include <sys/kernel.h>
50 #include <sys/module.h>
51 #include <sys/serial.h>
52 #include <sys/vnode.h>
53 #include <sys/signalvar.h>
54 #include <sys/malloc.h>
55 #include <sys/taskqueue.h>
57 MALLOC_DEFINE(M_NLMDM, "nullmodem", "nullmodem data structures");
59 static void nmdmstart(struct tty *tp);
60 static void nmdmstop(struct tty *tp, int rw);
61 static void nmdminit(struct cdev *dev);
62 static t_modem_t nmdmmodem;
64 static d_open_t nmdmopen;
65 static d_close_t nmdmclose;
67 static struct cdevsw nmdm_cdevsw = {
68 .d_version = D_VERSION,
72 .d_flags = D_TTY | D_PSEUDO | D_NEEDGIANT,
75 #define BUFSIZ 100 /* Chunk size iomoved to/from user */
76 #define NMDM_MAX_NUM 128 /* Artificially limit # devices. */
77 #define PF_STOPPED 0x10 /* user told stopped */
78 #define BFLAG CLONE_FLAG0
85 struct softpart *other;
89 TAILQ_ENTRY(nm_softc) pt_list;
91 struct softpart part1, part2;
92 struct prison *pt_prison;
95 static struct clonedevs *nmdmclones;
96 static TAILQ_HEAD(,nm_softc) nmdmhead = TAILQ_HEAD_INITIALIZER(nmdmhead);
99 nmdm_clone(void *arg, char *name, int nameen, struct cdev **dev)
103 struct cdev *d1, *d2;
107 if (strcmp(name, "nmdm") == 0) {
111 i = dev_stdclone(name, &p, "nmdm", &unit);
114 if (p[0] != '\0' && p[0] != 'A' && p[0] != 'B')
116 else if (p[0] != '\0' && p[1] != '\0')
119 i = clone_create(&nmdmclones, &nmdm_cdevsw, &unit, &d1, 0);
121 d1 = make_dev(&nmdm_cdevsw, unit2minor(unit),
122 0, 0, 0666, "nmdm%dA", unit);
125 d2 = make_dev(&nmdm_cdevsw, unit2minor(unit) | BFLAG,
126 0, 0, 0666, "nmdm%dB", unit);
135 d1->si_flags |= SI_CHEAPCLONE;
136 d2->si_flags |= SI_CHEAPCLONE;
138 if (p != NULL && p[0] == 'B')
145 nmdm_task_tty(void *arg, int pending __unused)
147 struct tty *tp, *otp;
153 otp = sp->other->nm_tty;
154 KASSERT(otp != NULL, ("NULL otp in nmdmstart"));
155 KASSERT(otp != tp, ("NULL otp == tp nmdmstart"));
156 if (sp->other->nm_dcd) {
157 if (!(tp->t_state & TS_ISOPEN)) {
158 sp->other->nm_dcd = 0;
159 (void)ttyld_modem(otp, 0);
162 if (tp->t_state & TS_ISOPEN) {
163 sp->other->nm_dcd = 1;
164 (void)ttyld_modem(otp, 1);
167 if (tp->t_state & TS_TTSTOP)
169 while (tp->t_outq.c_cc != 0) {
170 if (otp->t_state & TS_TBLOCK)
172 c = getc(&tp->t_outq);
173 if (otp->t_state & TS_ISOPEN)
176 if (tp->t_outq.c_cc == 0)
181 * This function creates and initializes a pair of ttys.
184 nmdminit(struct cdev *dev1)
189 dev2 = dev1->si_drv2;
191 dev1->si_flags &= ~SI_CHEAPCLONE;
192 dev2->si_flags &= ~SI_CHEAPCLONE;
194 pt = malloc(sizeof(*pt), M_NLMDM, M_WAITOK | M_ZERO);
195 TAILQ_INSERT_TAIL(&nmdmhead, pt, pt_list);
197 dev1->si_drv1 = dev2->si_drv1 = pt;
199 pt->part1.dev = dev1;
200 pt->part2.dev = dev2;
202 pt->part1.nm_tty = ttymalloc(pt->part1.nm_tty);
203 pt->part1.nm_tty->t_oproc = nmdmstart;
204 pt->part1.nm_tty->t_stop = nmdmstop;
205 pt->part1.nm_tty->t_modem = nmdmmodem;
206 pt->part1.nm_tty->t_dev = dev1;
207 pt->part1.nm_tty->t_sc = &pt->part1;
208 TASK_INIT(&pt->part1.pt_task, 0, nmdm_task_tty, pt->part1.nm_tty);
210 pt->part2.nm_tty = ttymalloc(pt->part2.nm_tty);
211 pt->part2.nm_tty->t_oproc = nmdmstart;
212 pt->part2.nm_tty->t_stop = nmdmstop;
213 pt->part2.nm_tty->t_modem = nmdmmodem;
214 pt->part2.nm_tty->t_dev = dev2;
215 pt->part2.nm_tty->t_sc = &pt->part2;
216 TASK_INIT(&pt->part2.pt_task, 0, nmdm_task_tty, pt->part2.nm_tty);
218 pt->part1.other = &pt->part2;
219 pt->part2.other = &pt->part1;
221 dev1->si_tty = pt->part1.nm_tty;
224 dev2->si_tty = pt->part2.nm_tty;
229 * Device opened from userland
232 nmdmopen(struct cdev *dev, int flag, int devtype, struct thread *td)
234 struct tty *tp, *tp2;
236 struct nm_softc *pti;
239 if (dev->si_drv1 == NULL)
242 if (pti->pt_prison != td->td_ucred->cr_prison)
247 tp2 = sp->other->nm_tty;
249 if ((tp->t_state & TS_ISOPEN) == 0) {
250 ttychars(tp); /* Set up default chars */
251 tp->t_iflag = TTYDEF_IFLAG;
252 tp->t_oflag = TTYDEF_OFLAG;
253 tp->t_lflag = TTYDEF_LFLAG;
255 tp->t_cflag = TTYDEF_CFLAG;
256 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
257 ttsetwater(tp); /* XXX ? */
258 } else if (tp->t_state & TS_XCLUDE && suser(td)) {
262 error = ttyld_open(tp, dev);
267 nmdmmodem(struct tty *tp, int sigon, int sigoff)
273 if (sigon || sigoff) {
274 if (sigon & SER_DTR) {
275 sp->other->nm_dcd = 1;
276 ttyld_modem(sp->other->nm_tty, sp->other->nm_dcd);
278 if (sigoff & SER_DTR) {
279 sp->other->nm_dcd = 0;
280 ttyld_modem(sp->other->nm_tty, sp->other->nm_dcd);
287 if (sp->other->nm_dcd)
294 nmdmclose(struct cdev *dev, int flag, int mode, struct thread *td)
297 return (tty_close(dev->si_tty));
301 nmdmstart(struct tty *tp)
306 taskqueue_enqueue(taskqueue_swi_giant, &pt->pt_task);
310 nmdmstop(struct tty *tp, int flush)
315 taskqueue_enqueue(taskqueue_swi_giant, &pt->pt_task);
322 nmdm_modevent(module_t mod, int type, void *data)
324 static eventhandler_tag tag;
325 struct nm_softc *pt, *tpt;
330 clone_setup(&nmdmclones);
331 tag = EVENTHANDLER_REGISTER(dev_clone, nmdm_clone, 0, 1000);
339 EVENTHANDLER_DEREGISTER(dev_clone, tag);
340 TAILQ_FOREACH_SAFE(pt, &nmdmhead, pt_list, tpt) {
341 destroy_dev(pt->part1.dev);
342 TAILQ_REMOVE(&nmdmhead, pt, pt_list);
345 clone_cleanup(&nmdmclones);
353 DEV_MODULE(nmdm, nmdm_modevent, NULL);