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) || defined(COMPAT_SUNOS)
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;
317 struct tty *tp, *tp2;
318 struct softpart *ourpart, *otherpart;
320 lwkt_gettoken(&tty_token);
322 GETPARTS(tp, ourpart, otherpart);
323 tp2 = &otherpart->nm_tty;
326 if (tp2->t_state & TS_ISOPEN) {
327 error = (*linesw[tp->t_line].l_read)(tp, ap->a_uio, flag);
328 wakeup_other(tp, FWRITE);
330 if (flag & IO_NDELAY) {
331 lwkt_reltoken(&tty_token);
332 return (EWOULDBLOCK);
334 error = tsleep(TSA_PTC_READ(tp), PCATCH, "nmdout", 0);
338 if ((error = (*linesw[tp->t_line].l_read)(tp, ap->a_uio, ap->a_ioflag)) == 0)
339 wakeup_other(tp, FWRITE);
341 lwkt_reltoken(&tty_token);
346 * Write to pseudo-tty.
347 * Wakeups of controlling tty will happen
348 * indirectly, when tty driver calls nmdmstart.
351 nmdmwrite(struct dev_write_args *ap)
353 cdev_t dev = ap->a_head.a_dev;
354 struct uio *uio = ap->a_uio;
357 u_char locbuf[BUFSIZ];
360 struct tty *tp1, *tp;
361 struct softpart *ourpart, *otherpart;
363 lwkt_gettoken(&tty_token);
366 * Get the other tty struct.
367 * basically we are writing into the INPUT side of the other device.
369 GETPARTS(tp1, ourpart, otherpart);
370 tp = &otherpart->nm_tty;
373 if ((tp->t_state & TS_ISOPEN) == 0) {
374 lwkt_reltoken(&tty_token);
377 while (uio->uio_resid > 0 || cc > 0) {
379 * Fill up the buffer if it's empty
382 cc = szmin(uio->uio_resid, BUFSIZ);
384 error = uiomove((caddr_t)cp, cc, uio);
386 lwkt_reltoken(&tty_token);
389 /* check again for safety */
390 if ((tp->t_state & TS_ISOPEN) == 0) {
391 /* adjust for data copied in but not written */
392 uio->uio_resid += cc;
393 lwkt_reltoken(&tty_token);
398 if (((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= (TTYHOG-2))
399 && ((tp->t_canq.c_cc > 0) || !(tp->t_iflag&ICANON))) {
401 * Come here to wait for space in outq,
402 * or space in rawq, or an empty canq.
404 wakeup(TSA_HUP_OR_INPUT(tp));
405 if ((tp->t_state & TS_CONNECTED) == 0) {
407 * Data piled up because not connected.
408 * Adjust for data copied in but
411 uio->uio_resid += cc;
412 lwkt_reltoken(&tty_token);
415 if (ap->a_ioflag & IO_NDELAY) {
417 * Don't wait if asked not to.
418 * Adjust for data copied in but
421 uio->uio_resid += cc;
423 lwkt_reltoken(&tty_token);
424 return (EWOULDBLOCK);
426 lwkt_reltoken(&tty_token);
429 error = tsleep(TSA_PTC_WRITE(tp),
430 PCATCH, "nmdout", 0);
433 * Tsleep returned (signal?).
434 * Go find out what the user wants.
435 * adjust for data copied in but
438 uio->uio_resid += cc;
439 lwkt_reltoken(&tty_token);
444 (*linesw[tp->t_line].l_rint)(*cp++, tp);
450 lwkt_reltoken(&tty_token);
455 * Start output on pseudo-tty.
456 * Wake up process selecting or sleeping for input from controlling tty.
459 nmdmstart(struct tty *tp)
461 struct nm_softc *pti = tp->t_dev->si_drv1;
463 lwkt_gettoken(&tty_token);
464 if (tp->t_state & TS_TTSTOP) {
465 lwkt_reltoken(&tty_token);
468 pti->pt_flags &= ~PF_STOPPED;
469 wakeup_other(tp, FREAD);
470 lwkt_reltoken(&tty_token);
473 /* Wakes up the OTHER tty;*/
475 wakeup_other(struct tty *tp, int flag)
477 struct softpart *ourpart, *otherpart;
479 lwkt_gettoken(&tty_token);
480 GETPARTS(tp, ourpart, otherpart);
482 wakeup(TSA_PTC_READ((&otherpart->nm_tty)));
483 KNOTE(&otherpart->nm_tty.t_rkq.ki_note, 0);
486 wakeup(TSA_PTC_WRITE((&otherpart->nm_tty)));
487 KNOTE(&otherpart->nm_tty.t_wkq.ki_note, 0);
489 lwkt_reltoken(&tty_token);
493 nmdmstop(struct tty *tp, int flush)
495 struct nm_softc *pti = tp->t_dev->si_drv1;
498 lwkt_gettoken(&tty_token);
499 /* note: FLUSHREAD and FLUSHWRITE already ok */
501 flush = TIOCPKT_STOP;
502 pti->pt_flags |= PF_STOPPED;
504 pti->pt_flags &= ~PF_STOPPED;
505 /* change of perspective */
511 wakeup_other(tp, flag);
512 lwkt_reltoken(&tty_token);
517 nmdmioctl(struct dev_ioctl_args *ap)
519 cdev_t dev = ap->a_head.a_dev;
520 struct tty *tp = dev->si_tty;
521 struct nm_softc *pti = dev->si_drv1;
524 struct softpart *ourpart, *otherpart;
527 lwkt_gettoken(&tty_token);
528 GETPARTS(tp, ourpart, otherpart);
529 tp2 = &otherpart->nm_tty;
531 error = (*linesw[tp->t_line].l_ioctl)(tp, ap->a_cmd, ap->a_data,
532 ap->a_fflag, ap->a_cred);
533 if (error == ENOIOCTL)
534 error = ttioctl(tp, ap->a_cmd, ap->a_data, ap->a_fflag);
535 if (error == ENOIOCTL) {
538 otherpart->gotbreak = 1;
543 ourpart->modemsignals |= TIOCM_DTR;
546 ourpart->modemsignals &= TIOCM_DTR;
549 ourpart->modemsignals = *(int *)ap->a_data;
550 otherpart->modemsignals = *(int *)ap->a_data;
553 ourpart->modemsignals |= *(int *)ap->a_data;
556 ourpart->modemsignals &= ~(*(int *)ap->a_data);
557 otherpart->modemsignals &= ~(*(int *)ap->a_data);
560 *(int *)ap->a_data = ourpart->modemsignals;
565 *(int *)ap->a_data = 0;
568 case TIOCDCDTIMESTAMP:
570 lwkt_reltoken(&tty_token);
576 nmdm_crossover(pti, ourpart, otherpart);
578 lwkt_reltoken(&tty_token);
584 nmdm_crossover(struct nm_softc *pti,
585 struct softpart *ourpart,
586 struct softpart *otherpart)
588 lwkt_gettoken(&tty_token);
589 otherpart->modemsignals &= ~(TIOCM_CTS|TIOCM_CAR);
590 if (ourpart->modemsignals & TIOCM_RTS)
591 otherpart->modemsignals |= TIOCM_CTS;
592 if (ourpart->modemsignals & TIOCM_DTR)
593 otherpart->modemsignals |= TIOCM_CAR;
594 lwkt_reltoken(&tty_token);
599 static void nmdm_drvinit (void *unused);
602 nmdm_drvinit(void *unused)
604 /* XXX: Gross hack for DEVFS */
605 lwkt_gettoken(&tty_token);
607 lwkt_reltoken(&tty_token);
610 SYSINIT(nmdmdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,nmdm_drvinit,NULL)