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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * $FreeBSD: src/sys/dev/nmdm/nmdm.c,v 1.5.2.1 2001/08/11 00:54:14 mp Exp $
39 * MPSAFE NOTE: This file acquires the tty_token mainly for linesw access and
40 * tp (struct tty) access.
44 * Pseudo-nulmodem Driver
46 #include "opt_compat.h"
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #if defined(COMPAT_43)
50 #include <sys/ioctl_compat.h>
54 #include <sys/thread2.h>
57 #include <sys/fcntl.h>
58 #include <sys/kernel.h>
59 #include <sys/vnode.h>
60 #include <sys/signalvar.h>
61 #include <sys/malloc.h>
63 MALLOC_DEFINE(M_NLMDM, "nullmodem", "nullmodem data structures");
65 static void nmdmstart (struct tty *tp);
66 static void nmdmstop (struct tty *tp, int rw);
67 static void wakeup_other (struct tty *tp, int flag);
68 static void nmdminit (int n);
70 static d_open_t nmdmopen;
71 static d_close_t nmdmclose;
72 static d_read_t nmdmread;
73 static d_write_t nmdmwrite;
74 static d_ioctl_t nmdmioctl;
77 static struct dev_ops nmdm_ops = {
84 .d_kqfilter = ttykqfilter,
88 #define BUFSIZ 100 /* Chunk size iomoved to/from user */
93 int modemsignals; /* bits defined in sys/ttycom.h */
99 struct softpart part1, part2;
100 struct prison *pt_prison;
103 #define PF_STOPPED 0x10 /* user told stopped */
106 nmdm_crossover(struct nm_softc *pti,
107 struct softpart *ourpart,
108 struct softpart *otherpart);
110 #define GETPARTS(tp, ourpart, otherpart) \
112 struct nm_softc *pti = tp->t_dev->si_drv1; \
113 if (tp == &pti->part1.nm_tty) { \
114 ourpart = &pti->part1; \
115 otherpart = &pti->part2; \
117 ourpart = &pti->part2; \
118 otherpart = &pti->part1; \
123 * This function creates and initializes a pair of ttys.
125 * NOTE: Must be called with tty_token held
134 * Simplified unit number, use low 8 bits of minor number
135 * (remember, the minor number mask is 0xffff00ff).
140 ASSERT_LWKT_TOKEN_HELD(&tty_token);
142 pt = kmalloc(sizeof(*pt), M_NLMDM, M_WAITOK | M_ZERO);
143 pt->part1.dev = dev1 = make_dev(&nmdm_ops, n << 1,
144 0, 0, 0666, "nmdm%dA", n);
145 pt->part2.dev = dev2 = make_dev(&nmdm_ops, (n << 1) + 1,
146 0, 0, 0666, "nmdm%dB", n);
148 dev1->si_drv1 = dev2->si_drv1 = pt;
149 dev1->si_tty = &pt->part1.nm_tty;
150 dev2->si_tty = &pt->part2.nm_tty;
151 ttyregister(&pt->part1.nm_tty);
152 ttyregister(&pt->part2.nm_tty);
153 pt->part1.nm_tty.t_oproc = nmdmstart;
154 pt->part2.nm_tty.t_oproc = nmdmstart;
155 pt->part1.nm_tty.t_stop = nmdmstop;
156 pt->part2.nm_tty.t_dev = dev1;
157 pt->part1.nm_tty.t_dev = dev2;
158 pt->part2.nm_tty.t_stop = nmdmstop;
163 nmdmopen(struct dev_open_args *ap)
165 cdev_t dev = ap->a_head.a_dev;
166 struct tty *tp, *tp2;
172 struct nm_softc *pti;
175 struct softpart *ourpart, *otherpart;
183 * XXX: Gross hack for DEVFS:
184 * If we openned this device, ensure we have the
185 * next one too, so people can open it.
188 nextdev = makedev(major(dev), (pair+pair) + 1);
189 if (!nextdev->si_drv1) {
200 lwkt_gettoken(&tty_token);
203 tp = &pti->part2.nm_tty;
205 tp = &pti->part1.nm_tty;
206 GETPARTS(tp, ourpart, otherpart);
207 tp2 = &otherpart->nm_tty;
208 ourpart->modemsignals |= TIOCM_LE;
210 if ((tp->t_state & TS_ISOPEN) == 0) {
211 ttychars(tp); /* Set up default chars */
212 tp->t_iflag = TTYDEF_IFLAG;
213 tp->t_oflag = TTYDEF_OFLAG;
214 tp->t_lflag = TTYDEF_LFLAG;
215 tp->t_cflag = TTYDEF_CFLAG;
216 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
217 } else if (tp->t_state & TS_XCLUDE && priv_check_cred(ap->a_cred, PRIV_ROOT, 0)) {
218 lwkt_reltoken(&tty_token);
220 } else if (pti->pt_prison != ap->a_cred->cr_prison) {
221 lwkt_reltoken(&tty_token);
226 * If the other side is open we have carrier
228 if (tp2->t_state & TS_ISOPEN) {
229 (void)(*linesw[tp->t_line].l_modem)(tp, 1);
233 * And the other side gets carrier as we are now open.
235 (void)(*linesw[tp2->t_line].l_modem)(tp2, 1);
237 /* External processing makes no sense here */
238 tp->t_lflag &= ~EXTPROC;
241 * Wait here if we don't have carrier.
244 while ((tp->t_state & TS_CARR_ON) == 0) {
245 if (flag & FNONBLOCK)
247 error = ttysleep(tp, TSA_CARR_ON(tp), PCATCH, "nmdopn", 0);
249 lwkt_reltoken(&tty_token);
256 * Give the line disciplin a chance to set this end up.
258 error = (*linesw[tp->t_line].l_open)(dev, tp);
261 * Wake up the other side.
262 * Theoretically not needed.
264 ourpart->modemsignals |= TIOCM_DTR;
265 nmdm_crossover(pti, ourpart, otherpart);
267 wakeup_other(tp, FREAD|FWRITE); /* XXX */
268 lwkt_reltoken(&tty_token);
273 nmdmclose(struct dev_close_args *ap)
275 cdev_t dev = ap->a_head.a_dev;
276 struct tty *tp, *tp2;
278 struct softpart *ourpart, *otherpart;
280 lwkt_gettoken(&tty_token);
282 * let the other end know that the game is up
285 GETPARTS(tp, ourpart, otherpart);
286 tp2 = &otherpart->nm_tty;
287 (void)(*linesw[tp2->t_line].l_modem)(tp2, 0);
290 * XXX MDMBUF makes no sense for nmdms but would inhibit the above
291 * l_modem(). CLOCAL makes sense but isn't supported. Special
292 * l_modem()s that ignore carrier drop make no sense for nmdms but
293 * may be in use because other parts of the line discipline make
294 * sense for nmdms. Recover by doing everything that a normal
295 * ttymodem() would have done except for sending a SIGHUP.
297 if (tp2->t_state & TS_ISOPEN) {
298 tp2->t_state &= ~(TS_CARR_ON | TS_CONNECTED);
299 tp2->t_state |= TS_ZOMBIE;
300 ttyflush(tp2, FREAD | FWRITE);
303 err = (*linesw[tp->t_line].l_close)(tp, ap->a_fflag);
304 ourpart->modemsignals &= ~TIOCM_DTR;
305 nmdm_crossover(dev->si_drv1, ourpart, otherpart);
306 nmdmstop(tp, FREAD|FWRITE);
308 lwkt_reltoken(&tty_token);
313 nmdmread(struct dev_read_args *ap)
315 cdev_t dev = ap->a_head.a_dev;
320 struct softpart *ourpart, *otherpart;
323 lwkt_gettoken(&tty_token);
326 GETPARTS(tp, ourpart, otherpart);
327 tp2 = &otherpart->nm_tty;
329 if (tp2->t_state & TS_ISOPEN) {
330 error = (*linesw[tp->t_line].l_read)(tp, ap->a_uio, flag);
331 wakeup_other(tp, FWRITE);
333 if (flag & IO_NDELAY) {
334 lwkt_reltoken(&tty_token);
335 return (EWOULDBLOCK);
337 error = tsleep(TSA_PTC_READ(tp), PCATCH, "nmdout", 0);
341 if ((error = (*linesw[tp->t_line].l_read)(tp, ap->a_uio, ap->a_ioflag)) == 0)
342 wakeup_other(tp, FWRITE);
344 lwkt_reltoken(&tty_token);
349 * Write to pseudo-tty.
350 * Wakeups of controlling tty will happen
351 * indirectly, when tty driver calls nmdmstart.
354 nmdmwrite(struct dev_write_args *ap)
356 cdev_t dev = ap->a_head.a_dev;
357 struct uio *uio = ap->a_uio;
360 u_char locbuf[BUFSIZ];
363 struct tty *tp1, *tp;
364 struct softpart *ourpart, *otherpart;
366 lwkt_gettoken(&tty_token);
369 * Get the other tty struct.
370 * basically we are writing into the INPUT side of the other device.
372 GETPARTS(tp1, ourpart, otherpart);
373 tp = &otherpart->nm_tty;
376 if ((tp->t_state & TS_ISOPEN) == 0) {
377 lwkt_reltoken(&tty_token);
380 while (uio->uio_resid > 0 || cc > 0) {
382 * Fill up the buffer if it's empty
385 cc = szmin(uio->uio_resid, BUFSIZ);
387 error = uiomove((caddr_t)cp, cc, uio);
389 lwkt_reltoken(&tty_token);
392 /* check again for safety */
393 if ((tp->t_state & TS_ISOPEN) == 0) {
394 /* adjust for data copied in but not written */
395 uio->uio_resid += cc;
396 lwkt_reltoken(&tty_token);
401 if (((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= (TTYHOG-2))
402 && ((tp->t_canq.c_cc > 0) || !(tp->t_iflag&ICANON))) {
404 * Come here to wait for space in outq,
405 * or space in rawq, or an empty canq.
407 wakeup(TSA_HUP_OR_INPUT(tp));
408 if ((tp->t_state & TS_CONNECTED) == 0) {
410 * Data piled up because not connected.
411 * Adjust for data copied in but
414 uio->uio_resid += cc;
415 lwkt_reltoken(&tty_token);
418 if (ap->a_ioflag & IO_NDELAY) {
420 * Don't wait if asked not to.
421 * Adjust for data copied in but
424 uio->uio_resid += cc;
426 lwkt_reltoken(&tty_token);
427 return (EWOULDBLOCK);
429 lwkt_reltoken(&tty_token);
432 error = tsleep(TSA_PTC_WRITE(tp),
433 PCATCH, "nmdout", 0);
436 * Tsleep returned (signal?).
437 * Go find out what the user wants.
438 * adjust for data copied in but
441 uio->uio_resid += cc;
442 lwkt_reltoken(&tty_token);
447 (*linesw[tp->t_line].l_rint)(*cp++, tp);
453 lwkt_reltoken(&tty_token);
458 * Start output on pseudo-tty.
459 * Wake up process selecting or sleeping for input from controlling tty.
462 nmdmstart(struct tty *tp)
464 struct nm_softc *pti = tp->t_dev->si_drv1;
466 lwkt_gettoken(&tty_token);
467 if (tp->t_state & TS_TTSTOP) {
468 lwkt_reltoken(&tty_token);
471 pti->pt_flags &= ~PF_STOPPED;
472 wakeup_other(tp, FREAD);
473 lwkt_reltoken(&tty_token);
476 /* Wakes up the OTHER tty;*/
478 wakeup_other(struct tty *tp, int flag)
480 struct softpart *ourpart, *otherpart;
482 lwkt_gettoken(&tty_token);
483 GETPARTS(tp, ourpart, otherpart);
485 wakeup(TSA_PTC_READ((&otherpart->nm_tty)));
486 KNOTE(&otherpart->nm_tty.t_rkq.ki_note, 0);
489 wakeup(TSA_PTC_WRITE((&otherpart->nm_tty)));
490 KNOTE(&otherpart->nm_tty.t_wkq.ki_note, 0);
492 lwkt_reltoken(&tty_token);
496 nmdmstop(struct tty *tp, int flush)
498 struct nm_softc *pti = tp->t_dev->si_drv1;
501 lwkt_gettoken(&tty_token);
502 /* note: FLUSHREAD and FLUSHWRITE already ok */
504 flush = TIOCPKT_STOP;
505 pti->pt_flags |= PF_STOPPED;
507 pti->pt_flags &= ~PF_STOPPED;
508 /* change of perspective */
514 wakeup_other(tp, flag);
515 lwkt_reltoken(&tty_token);
520 nmdmioctl(struct dev_ioctl_args *ap)
522 cdev_t dev = ap->a_head.a_dev;
523 struct tty *tp = dev->si_tty;
524 struct nm_softc *pti = dev->si_drv1;
526 struct softpart *ourpart, *otherpart;
529 lwkt_gettoken(&tty_token);
530 GETPARTS(tp, ourpart, otherpart);
532 error = (*linesw[tp->t_line].l_ioctl)(tp, ap->a_cmd, ap->a_data,
533 ap->a_fflag, ap->a_cred);
534 if (error == ENOIOCTL)
535 error = ttioctl(tp, ap->a_cmd, ap->a_data, ap->a_fflag);
536 if (error == ENOIOCTL) {
539 otherpart->gotbreak = 1;
544 ourpart->modemsignals |= TIOCM_DTR;
547 ourpart->modemsignals &= TIOCM_DTR;
550 ourpart->modemsignals = *(int *)ap->a_data;
551 otherpart->modemsignals = *(int *)ap->a_data;
554 ourpart->modemsignals |= *(int *)ap->a_data;
557 ourpart->modemsignals &= ~(*(int *)ap->a_data);
558 otherpart->modemsignals &= ~(*(int *)ap->a_data);
561 *(int *)ap->a_data = ourpart->modemsignals;
566 *(int *)ap->a_data = 0;
569 case TIOCDCDTIMESTAMP:
571 lwkt_reltoken(&tty_token);
577 nmdm_crossover(pti, ourpart, otherpart);
579 lwkt_reltoken(&tty_token);
585 nmdm_crossover(struct nm_softc *pti,
586 struct softpart *ourpart,
587 struct softpart *otherpart)
589 lwkt_gettoken(&tty_token);
590 otherpart->modemsignals &= ~(TIOCM_CTS|TIOCM_CAR);
591 if (ourpart->modemsignals & TIOCM_RTS)
592 otherpart->modemsignals |= TIOCM_CTS;
593 if (ourpart->modemsignals & TIOCM_DTR)
594 otherpart->modemsignals |= TIOCM_CAR;
595 lwkt_reltoken(&tty_token);
600 static void nmdm_drvinit (void *unused);
603 nmdm_drvinit(void *unused)
605 /* XXX: Gross hack for DEVFS */
606 lwkt_gettoken(&tty_token);
608 lwkt_reltoken(&tty_token);
611 SYSINIT(nmdmdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,nmdm_drvinit,NULL)