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/signalvar.h>
53 #include <sys/malloc.h>
54 #include <sys/taskqueue.h>
56 MALLOC_DEFINE(M_NLMDM, "nullmodem", "nullmodem data structures");
58 static d_close_t nmdmclose;
59 static t_modem_t nmdmmodem;
60 static d_open_t nmdmopen;
61 static t_oproc_t nmdmoproc;
62 static t_param_t nmdmparam;
63 static t_stop_t nmdmstop;
65 static void nmdminit(struct cdev *dev);
68 static struct cdevsw nmdm_cdevsw = {
69 .d_version = D_VERSION,
73 .d_flags = D_TTY | D_PSEUDO | D_NEEDGIANT,
76 #define BUFSIZ 100 /* Chunk size iomoved to/from user */
77 #define NMDM_MAX_NUM 128 /* Artificially limit # devices. */
78 #define PF_STOPPED 0x10 /* user told stopped */
79 #define BFLAG CLONE_FLAG0
86 struct softpart *other;
93 #define QS 8 /* Quota shift */
97 TAILQ_ENTRY(nm_softc) pt_list;
99 struct softpart part1, part2;
100 struct prison *pt_prison;
103 static struct clonedevs *nmdmclones;
104 static TAILQ_HEAD(,nm_softc) nmdmhead = TAILQ_HEAD_INITIALIZER(nmdmhead);
107 nmdm_clone(void *arg, char *name, int nameen, struct cdev **dev)
111 struct cdev *d1, *d2;
115 if (strcmp(name, "nmdm") == 0) {
119 i = dev_stdclone(name, &p, "nmdm", &unit);
122 if (p[0] != '\0' && p[0] != 'A' && p[0] != 'B')
124 else if (p[0] != '\0' && p[1] != '\0')
127 i = clone_create(&nmdmclones, &nmdm_cdevsw, &unit, &d1, 0);
129 d1 = make_dev(&nmdm_cdevsw, unit2minor(unit),
130 0, 0, 0666, "nmdm%dA", unit);
133 d2 = make_dev(&nmdm_cdevsw, unit2minor(unit) | BFLAG,
134 0, 0, 0666, "nmdm%dB", unit);
143 d1->si_flags |= SI_CHEAPCLONE;
144 d2->si_flags |= SI_CHEAPCLONE;
146 if (p != NULL && p[0] == 'B')
153 nmdm_timeout(void *arg)
163 * Do a simple Floyd-Steinberg dither here to avoid FP math.
164 * Wipe out unused quota from last tick.
166 sp->accumulator += sp->credits;
167 sp->quota = sp->accumulator >> QS;
168 sp->accumulator &= ((1 << QS) - 1);
170 taskqueue_enqueue(taskqueue_swi_giant, &sp->pt_task);
171 callout_reset(&sp->co, sp->rate, nmdm_timeout, arg);
175 nmdm_task_tty(void *arg, int pending __unused)
177 struct tty *tp, *otp;
183 otp = sp->other->nm_tty;
184 KASSERT(otp != NULL, ("NULL otp in nmdmstart"));
185 KASSERT(otp != tp, ("NULL otp == tp nmdmstart"));
186 if (sp->other->nm_dcd) {
187 if (!(tp->t_state & TS_ISOPEN)) {
188 sp->other->nm_dcd = 0;
189 (void)ttyld_modem(otp, 0);
192 if (tp->t_state & TS_ISOPEN) {
193 sp->other->nm_dcd = 1;
194 (void)ttyld_modem(otp, 1);
197 if (tp->t_state & TS_TTSTOP)
199 while (tp->t_outq.c_cc != 0) {
200 if (sp->rate && !sp->quota)
202 if (otp->t_state & TS_TBLOCK)
205 c = getc(&tp->t_outq);
206 if (otp->t_state & TS_ISOPEN)
209 if (tp->t_outq.c_cc == 0)
215 * This function creates and initializes a pair of ttys.
218 nmdminit(struct cdev *dev1)
223 dev2 = dev1->si_drv2;
225 dev1->si_flags &= ~SI_CHEAPCLONE;
226 dev2->si_flags &= ~SI_CHEAPCLONE;
228 pt = malloc(sizeof(*pt), M_NLMDM, M_WAITOK | M_ZERO);
229 TAILQ_INSERT_TAIL(&nmdmhead, pt, pt_list);
231 dev1->si_drv1 = dev2->si_drv1 = pt;
233 pt->part1.dev = dev1;
234 pt->part2.dev = dev2;
236 pt->part1.nm_tty = ttymalloc(pt->part1.nm_tty);
237 pt->part1.nm_tty->t_oproc = nmdmoproc;
238 pt->part1.nm_tty->t_stop = nmdmstop;
239 pt->part1.nm_tty->t_modem = nmdmmodem;
240 pt->part1.nm_tty->t_param = nmdmparam;
241 pt->part1.nm_tty->t_dev = dev1;
242 pt->part1.nm_tty->t_sc = &pt->part1;
243 TASK_INIT(&pt->part1.pt_task, 0, nmdm_task_tty, pt->part1.nm_tty);
244 callout_init(&pt->part1.co, 0);
246 pt->part2.nm_tty = ttymalloc(pt->part2.nm_tty);
247 pt->part2.nm_tty->t_oproc = nmdmoproc;
248 pt->part2.nm_tty->t_stop = nmdmstop;
249 pt->part2.nm_tty->t_modem = nmdmmodem;
250 pt->part2.nm_tty->t_param = nmdmparam;
251 pt->part2.nm_tty->t_dev = dev2;
252 pt->part2.nm_tty->t_sc = &pt->part2;
253 TASK_INIT(&pt->part2.pt_task, 0, nmdm_task_tty, pt->part2.nm_tty);
254 callout_init(&pt->part2.co, 0);
256 pt->part1.other = &pt->part2;
257 pt->part2.other = &pt->part1;
259 dev1->si_tty = pt->part1.nm_tty;
262 dev2->si_tty = pt->part2.nm_tty;
267 * Device opened from userland
270 nmdmopen(struct cdev *dev, int flag, int devtype, struct thread *td)
272 struct tty *tp, *tp2;
274 struct nm_softc *pti;
277 if (dev->si_drv1 == NULL)
280 if (pti->pt_prison != td->td_ucred->cr_prison)
285 tp2 = sp->other->nm_tty;
287 if ((tp->t_state & TS_ISOPEN) == 0) {
288 ttyinitmode(tp, 0, 0);
289 ttsetwater(tp); /* XXX ? */
290 } else if (tp->t_state & TS_XCLUDE && suser(td)) {
294 error = ttyld_open(tp, dev);
299 bits_per_char(struct termios *t)
303 bits = 1; /* start bit */
304 switch (t->c_cflag & CSIZE) {
305 case CS5: bits += 5; break;
306 case CS6: bits += 6; break;
307 case CS7: bits += 7; break;
308 case CS8: bits += 8; break;
310 bits++; /* stop bit */
311 if (t->c_cflag & PARENB)
313 if (t->c_cflag & CSTOPB)
319 nmdmparam(struct tty *tp, struct termios *t)
323 int bpc, rate, speed, i;
326 tp2 = sp->other->nm_tty;
328 if (!((t->c_cflag | tp2->t_cflag) & CDSR_OFLOW)) {
335 * DSRFLOW one either side enables rate-simulation for both
337 * NB: the two directions may run at different rates.
340 /* Find the larger of the number of bits transmitted */
341 bpc = imax(bits_per_char(t), bits_per_char(&tp2->t_termios));
343 for (i = 0; i < 2; i++) {
344 /* Use the slower of our receive and their transmit rate */
345 speed = imin(tp2->t_ospeed, t->c_ispeed);
352 speed <<= QS; /* [bit/sec, scaled] */
353 speed /= bpc; /* [char/sec, scaled] */
354 rate = (hz << QS) / speed; /* [hz per callout] */
359 speed /= hz; /* [(char/sec)/tick, scaled */
363 callout_reset(&sp->co, rate, nmdm_timeout, sp);
366 * swap pointers for second pass so the other end gets
377 nmdmmodem(struct tty *tp, int sigon, int sigoff)
383 if (sigon || sigoff) {
385 sp->other->nm_dcd = 1;
386 if (sigoff & SER_DTR)
387 sp->other->nm_dcd = 0;
388 ttyld_modem(sp->other->nm_tty, sp->other->nm_dcd);
394 if (sp->other->nm_dcd)
401 nmdmclose(struct cdev *dev, int flag, int mode, struct thread *td)
404 return (tty_close(dev->si_tty));
408 nmdmoproc(struct tty *tp)
413 taskqueue_enqueue(taskqueue_swi_giant, &pt->pt_task);
417 nmdmstop(struct tty *tp, int flush)
422 taskqueue_enqueue(taskqueue_swi_giant, &pt->pt_task);
429 nmdm_modevent(module_t mod, int type, void *data)
431 static eventhandler_tag tag;
432 struct nm_softc *pt, *tpt;
437 clone_setup(&nmdmclones);
438 tag = EVENTHANDLER_REGISTER(dev_clone, nmdm_clone, 0, 1000);
446 EVENTHANDLER_DEREGISTER(dev_clone, tag);
447 TAILQ_FOREACH_SAFE(pt, &nmdmhead, pt_list, tpt) {
448 destroy_dev(pt->part1.dev);
449 TAILQ_REMOVE(&nmdmhead, pt, pt_list);
452 clone_cleanup(&nmdmclones);
460 DEV_MODULE(nmdm, nmdm_modevent, NULL);