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 <sys/param.h>
40 #include <sys/systm.h>
45 #include <sys/fcntl.h>
47 #include <sys/kernel.h>
48 #include <sys/limits.h>
49 #include <sys/module.h>
50 #include <sys/serial.h>
51 #include <sys/signalvar.h>
52 #include <sys/malloc.h>
53 #include <sys/taskqueue.h>
55 static MALLOC_DEFINE(M_NMDM, "nullmodem", "nullmodem data structures");
57 static tsw_inwakeup_t nmdm_outwakeup;
58 static tsw_outwakeup_t nmdm_inwakeup;
59 static tsw_param_t nmdm_param;
60 static tsw_modem_t nmdm_modem;
62 static struct ttydevsw nmdm_class = {
63 .tsw_flags = TF_NOPREFIX,
64 .tsw_inwakeup = nmdm_inwakeup,
65 .tsw_outwakeup = nmdm_outwakeup,
66 .tsw_param = nmdm_param,
67 .tsw_modem = nmdm_modem,
70 static void nmdm_task_tty(void *, int);
78 struct nmdmpart *np_other;
79 struct nmdmsoftc *np_pair;
80 struct callout np_callout;
82 u_long np_accumulator;
86 #define QS 8 /* Quota shift */
90 struct nmdmpart ns_part1;
91 struct nmdmpart ns_part2;
95 static int nmdm_count = 0;
97 static struct nmdmsoftc *
98 nmdm_alloc(unsigned long unit)
100 struct nmdmsoftc *ns;
103 atomic_add_int(&nmdm_count, 1);
105 ns = malloc(sizeof(*ns), M_NMDM, M_WAITOK|M_ZERO);
106 mtx_init(&ns->ns_mtx, "nmdm", NULL, MTX_DEF);
108 /* Hook the pairs together. */
109 ns->ns_part1.np_pair = ns;
110 ns->ns_part1.np_other = &ns->ns_part2;
111 TASK_INIT(&ns->ns_part1.np_task, 0, nmdm_task_tty, &ns->ns_part1);
112 callout_init_mtx(&ns->ns_part1.np_callout, &ns->ns_mtx, 0);
114 ns->ns_part2.np_pair = ns;
115 ns->ns_part2.np_other = &ns->ns_part1;
116 TASK_INIT(&ns->ns_part2.np_task, 0, nmdm_task_tty, &ns->ns_part2);
117 callout_init_mtx(&ns->ns_part2.np_callout, &ns->ns_mtx, 0);
119 /* Create device nodes. */
120 tp = ns->ns_part1.np_tty = tty_alloc_mutex(&nmdm_class, &ns->ns_part1,
122 tty_makedev(tp, NULL, "nmdm%luA", unit);
124 tp = ns->ns_part2.np_tty = tty_alloc_mutex(&nmdm_class, &ns->ns_part2,
126 tty_makedev(tp, NULL, "nmdm%luB", unit);
132 nmdm_clone(void *arg, struct ucred *cred, char *name, int nameen,
137 struct nmdmsoftc *ns;
141 if (strncmp(name, "nmdm", 4) != 0)
144 /* Device name must be "nmdm%lu%c", where %c is 'A' or 'B'. */
146 unit = strtoul(name, &end, 10);
147 if (unit == ULONG_MAX || name == end)
149 if ((end[0] != 'A' && end[0] != 'B') || end[1] != '\0')
152 /* XXX: pass privileges? */
153 ns = nmdm_alloc(unit);
156 *dev = ns->ns_part1.np_tty->t_dev;
158 *dev = ns->ns_part2.np_tty->t_dev;
162 nmdm_timeout(void *arg)
164 struct nmdmpart *np = arg;
166 if (np->np_rate == 0)
170 * Do a simple Floyd-Steinberg dither here to avoid FP math.
171 * Wipe out unused quota from last tick.
173 np->np_accumulator += np->np_credits;
174 np->np_quota = np->np_accumulator >> QS;
175 np->np_accumulator &= ((1 << QS) - 1);
177 taskqueue_enqueue(taskqueue_swi, &np->np_task);
178 callout_reset(&np->np_callout, np->np_rate, nmdm_timeout, np);
182 nmdm_task_tty(void *arg, int pending __unused)
184 struct tty *tp, *otp;
185 struct nmdmpart *np = arg;
191 otp = np->np_other->np_tty;
192 KASSERT(otp != NULL, ("NULL otp in nmdmstart"));
193 KASSERT(otp != tp, ("NULL otp == tp nmdmstart"));
194 if (np->np_other->np_dcd) {
195 if (!tty_opened(tp)) {
196 np->np_other->np_dcd = 0;
197 ttydisc_modem(otp, 0);
200 if (tty_opened(tp)) {
201 np->np_other->np_dcd = 1;
202 ttydisc_modem(otp, 1);
206 while (ttydisc_rint_poll(otp) > 0) {
207 if (np->np_rate && !np->np_quota)
209 if (ttydisc_getc(tp, &c, 1) != 1)
212 ttydisc_rint(otp, c, 0);
215 ttydisc_rint_done(otp);
221 bits_per_char(struct termios *t)
225 bits = 1; /* start bit */
226 switch (t->c_cflag & CSIZE) {
227 case CS5: bits += 5; break;
228 case CS6: bits += 6; break;
229 case CS7: bits += 7; break;
230 case CS8: bits += 8; break;
232 bits++; /* stop bit */
233 if (t->c_cflag & PARENB)
235 if (t->c_cflag & CSTOPB)
241 nmdm_param(struct tty *tp, struct termios *t)
243 struct nmdmpart *np = tty_softc(tp);
245 int bpc, rate, speed, i;
247 tp2 = np->np_other->np_tty;
249 if (!((t->c_cflag | tp2->t_termios.c_cflag) & CDSR_OFLOW)) {
251 np->np_other->np_rate = 0;
256 * DSRFLOW one either side enables rate-simulation for both
258 * NB: the two directions may run at different rates.
261 /* Find the larger of the number of bits transmitted */
262 bpc = imax(bits_per_char(t), bits_per_char(&tp2->t_termios));
264 for (i = 0; i < 2; i++) {
265 /* Use the slower of our receive and their transmit rate */
266 speed = imin(tp2->t_termios.c_ospeed, t->c_ispeed);
269 np->np_other->np_rate = 0;
273 speed <<= QS; /* [bit/sec, scaled] */
274 speed /= bpc; /* [char/sec, scaled] */
275 rate = (hz << QS) / speed; /* [hz per callout] */
280 speed /= hz; /* [(char/sec)/tick, scaled */
282 np->np_credits = speed;
284 callout_reset(&np->np_callout, rate, nmdm_timeout, np);
287 * swap pointers for second pass so the other end gets
299 nmdm_modem(struct tty *tp, int sigon, int sigoff)
301 struct nmdmpart *np = tty_softc(tp);
304 if (sigon || sigoff) {
306 np->np_other->np_dcd = 1;
307 if (sigoff & SER_DTR)
308 np->np_other->np_dcd = 0;
310 ttydisc_modem(np->np_other->np_tty, np->np_other->np_dcd);
316 if (np->np_other->np_dcd)
324 nmdm_inwakeup(struct tty *tp)
326 struct nmdmpart *np = tty_softc(tp);
328 /* We can receive again, so wake up the other side. */
329 taskqueue_enqueue(taskqueue_swi, &np->np_other->np_task);
333 nmdm_outwakeup(struct tty *tp)
335 struct nmdmpart *np = tty_softc(tp);
337 /* We can transmit again, so wake up our side. */
338 taskqueue_enqueue(taskqueue_swi, &np->np_task);
345 nmdm_modevent(module_t mod, int type, void *data)
347 static eventhandler_tag tag;
351 tag = EVENTHANDLER_REGISTER(dev_clone, nmdm_clone, 0, 1000);
362 EVENTHANDLER_DEREGISTER(dev_clone, tag);
372 DEV_MODULE(nmdm, nmdm_modevent, NULL);