2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1982, 1986, 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
37 * Pseudo-nulmodem driver
38 * Mighty handy for use with serial console in Vmware
41 #include <sys/param.h>
42 #include <sys/systm.h>
47 #include <sys/eventhandler.h>
48 #include <sys/fcntl.h>
50 #include <sys/kernel.h>
51 #include <sys/limits.h>
52 #include <sys/module.h>
53 #include <sys/serial.h>
54 #include <sys/signalvar.h>
55 #include <sys/malloc.h>
56 #include <sys/taskqueue.h>
58 static MALLOC_DEFINE(M_NMDM, "nullmodem", "nullmodem data structures");
60 static tsw_inwakeup_t nmdm_outwakeup;
61 static tsw_outwakeup_t nmdm_inwakeup;
62 static tsw_param_t nmdm_param;
63 static tsw_modem_t nmdm_modem;
64 static tsw_close_t nmdm_close;
65 static tsw_free_t nmdm_free;
67 static struct ttydevsw nmdm_class = {
68 .tsw_flags = TF_NOPREFIX,
69 .tsw_inwakeup = nmdm_inwakeup,
70 .tsw_outwakeup = nmdm_outwakeup,
71 .tsw_param = nmdm_param,
72 .tsw_modem = nmdm_modem,
73 .tsw_close = nmdm_close,
74 .tsw_free = nmdm_free,
77 static void nmdm_task_tty(void *, int);
85 struct nmdmpart *np_other;
86 struct nmdmsoftc *np_pair;
87 struct callout np_callout;
89 u_long np_accumulator;
93 #define QS 8 /* Quota shift */
97 struct nmdmpart ns_part1;
98 struct nmdmpart ns_part2;
102 static int nmdm_count = 0;
105 nmdm_close(struct tty *tp)
108 struct nmdmpart *onp;
115 /* If second part is opened, do not destroy ourselves. */
119 /* Shut down self. */
122 /* Shut down second part. */
133 nmdm_free(void *softc)
135 struct nmdmpart *np = (struct nmdmpart *)softc;
136 struct nmdmsoftc *ns = np->np_pair;
138 callout_drain(&np->np_callout);
139 taskqueue_drain(taskqueue_swi, &np->np_task);
142 * The function is called on both parts simultaneously. We serialize
143 * with help of ns_mtx. The first invocation should return and
144 * delegate freeing of resources to the second.
146 mtx_lock(&ns->ns_mtx);
147 if (np->np_other != NULL) {
148 np->np_other->np_other = NULL;
149 mtx_unlock(&ns->ns_mtx);
152 mtx_destroy(&ns->ns_mtx);
154 atomic_subtract_int(&nmdm_count, 1);
158 nmdm_clone(void *arg, struct ucred *cred, char *name, int nameen,
161 struct nmdmsoftc *ns;
169 if (strncmp(name, "nmdm", 4) != 0)
171 if (strlen(name) <= strlen("nmdmX"))
174 /* Device name must be "nmdm%s%c", where %c is 'A' or 'B'. */
175 end = name + strlen(name) - 1;
177 if (endc != 'A' && endc != 'B')
180 ns = malloc(sizeof(*ns), M_NMDM, M_WAITOK | M_ZERO);
181 mtx_init(&ns->ns_mtx, "nmdm", NULL, MTX_DEF);
183 /* Hook the pairs together. */
184 ns->ns_part1.np_pair = ns;
185 ns->ns_part1.np_other = &ns->ns_part2;
186 TASK_INIT(&ns->ns_part1.np_task, 0, nmdm_task_tty, &ns->ns_part1);
187 callout_init_mtx(&ns->ns_part1.np_callout, &ns->ns_mtx, 0);
189 ns->ns_part2.np_pair = ns;
190 ns->ns_part2.np_other = &ns->ns_part1;
191 TASK_INIT(&ns->ns_part2.np_task, 0, nmdm_task_tty, &ns->ns_part2);
192 callout_init_mtx(&ns->ns_part2.np_callout, &ns->ns_mtx, 0);
194 /* Create device nodes. */
195 tp = ns->ns_part1.np_tty = tty_alloc_mutex(&nmdm_class, &ns->ns_part1,
198 error = tty_makedevf(tp, cred, endc == 'A' ? TTYMK_CLONING : 0,
202 mtx_destroy(&ns->ns_mtx);
207 tp = ns->ns_part2.np_tty = tty_alloc_mutex(&nmdm_class, &ns->ns_part2,
210 error = tty_makedevf(tp, cred, endc == 'B' ? TTYMK_CLONING : 0,
214 mtx_lock(&ns->ns_mtx);
215 /* see nmdm_free() */
216 ns->ns_part1.np_other = NULL;
217 atomic_add_int(&nmdm_count, 1);
218 tty_rel_gone(ns->ns_part1.np_tty);
223 *dev = ns->ns_part1.np_tty->t_dev;
225 *dev = ns->ns_part2.np_tty->t_dev;
228 atomic_add_int(&nmdm_count, 1);
232 nmdm_timeout(void *arg)
234 struct nmdmpart *np = arg;
236 if (np->np_rate == 0)
240 * Do a simple Floyd-Steinberg dither here to avoid FP math.
241 * Wipe out unused quota from last tick.
243 np->np_accumulator += np->np_credits;
244 np->np_quota = np->np_accumulator >> QS;
245 np->np_accumulator &= ((1 << QS) - 1);
247 taskqueue_enqueue(taskqueue_swi, &np->np_task);
248 callout_reset(&np->np_callout, np->np_rate, nmdm_timeout, np);
252 nmdm_task_tty(void *arg, int pending __unused)
254 struct tty *tp, *otp;
255 struct nmdmpart *np = arg;
265 otp = np->np_other->np_tty;
266 KASSERT(otp != NULL, ("NULL otp in nmdmstart"));
267 KASSERT(otp != tp, ("NULL otp == tp nmdmstart"));
268 if (np->np_other->np_dcd) {
269 if (!tty_opened(tp)) {
270 np->np_other->np_dcd = 0;
271 ttydisc_modem(otp, 0);
274 if (tty_opened(tp)) {
275 np->np_other->np_dcd = 1;
276 ttydisc_modem(otp, 1);
280 /* This may happen when we are in detach process. */
286 while (ttydisc_rint_poll(otp) > 0) {
287 if (np->np_rate && !np->np_quota)
289 if (ttydisc_getc(tp, &c, 1) != 1)
292 ttydisc_rint(otp, c, 0);
295 ttydisc_rint_done(otp);
301 bits_per_char(struct termios *t)
305 bits = 1; /* start bit */
306 switch (t->c_cflag & CSIZE) {
307 case CS5: bits += 5; break;
308 case CS6: bits += 6; break;
309 case CS7: bits += 7; break;
310 case CS8: bits += 8; break;
312 bits++; /* stop bit */
313 if (t->c_cflag & PARENB)
315 if (t->c_cflag & CSTOPB)
321 nmdm_param(struct tty *tp, struct termios *t)
323 struct nmdmpart *np = tty_softc(tp);
325 int bpc, rate, speed, i;
327 tp2 = np->np_other->np_tty;
329 if (!((t->c_cflag | tp2->t_termios.c_cflag) & CDSR_OFLOW)) {
331 np->np_other->np_rate = 0;
336 * DSRFLOW one either side enables rate-simulation for both
338 * NB: the two directions may run at different rates.
341 /* Find the larger of the number of bits transmitted */
342 bpc = imax(bits_per_char(t), bits_per_char(&tp2->t_termios));
344 for (i = 0; i < 2; i++) {
345 /* Use the slower of our receive and their transmit rate */
346 speed = imin(tp2->t_termios.c_ospeed, t->c_ispeed);
349 np->np_other->np_rate = 0;
353 speed <<= QS; /* [bit/sec, scaled] */
354 speed /= bpc; /* [char/sec, scaled] */
355 rate = (hz << QS) / speed; /* [hz per callout] */
360 speed /= hz; /* [(char/sec)/tick, scaled */
362 np->np_credits = speed;
364 callout_reset(&np->np_callout, rate, nmdm_timeout, np);
367 * swap pointers for second pass so the other end gets
379 nmdm_modem(struct tty *tp, int sigon, int sigoff)
381 struct nmdmpart *np = tty_softc(tp);
384 if (sigon || sigoff) {
386 np->np_other->np_dcd = 1;
387 if (sigoff & SER_DTR)
388 np->np_other->np_dcd = 0;
390 ttydisc_modem(np->np_other->np_tty, np->np_other->np_dcd);
396 if (np->np_other->np_dcd)
404 nmdm_inwakeup(struct tty *tp)
406 struct nmdmpart *np = tty_softc(tp);
408 /* We can receive again, so wake up the other side. */
409 taskqueue_enqueue(taskqueue_swi, &np->np_other->np_task);
413 nmdm_outwakeup(struct tty *tp)
415 struct nmdmpart *np = tty_softc(tp);
417 /* We can transmit again, so wake up our side. */
418 taskqueue_enqueue(taskqueue_swi, &np->np_task);
425 nmdm_modevent(module_t mod, int type, void *data)
427 static eventhandler_tag tag;
431 tag = EVENTHANDLER_REGISTER(dev_clone, nmdm_clone, 0, 1000);
442 EVENTHANDLER_DEREGISTER(dev_clone, tag);
452 DEV_MODULE(nmdm, nmdm_modevent, NULL);