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 $
36 * $DragonFly: src/sys/dev/misc/nmdm/nmdm.c,v 1.16 2008/01/05 14:02:37 swildner Exp $
40 * MPSAFE NOTE: This file acquires the tty_token mainly for linesw access and
41 * tp (struct tty) access.
45 * Pseudo-nulmodem Driver
47 #include "opt_compat.h"
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
51 #include <sys/ioctl_compat.h>
55 #include <sys/thread2.h>
58 #include <sys/fcntl.h>
59 #include <sys/kernel.h>
60 #include <sys/vnode.h>
61 #include <sys/signalvar.h>
62 #include <sys/malloc.h>
64 MALLOC_DEFINE(M_NLMDM, "nullmodem", "nullmodem data structures");
66 static void nmdmstart (struct tty *tp);
67 static void nmdmstop (struct tty *tp, int rw);
68 static void wakeup_other (struct tty *tp, int flag);
69 static void nmdminit (int n);
71 static d_open_t nmdmopen;
72 static d_close_t nmdmclose;
73 static d_read_t nmdmread;
74 static d_write_t nmdmwrite;
75 static d_ioctl_t nmdmioctl;
78 static struct dev_ops nmdm_ops = {
79 { "pts", CDEV_MAJOR, D_TTY },
85 .d_kqfilter = ttykqfilter,
89 #define BUFSIZ 100 /* Chunk size iomoved to/from user */
94 int modemsignals; /* bits defined in sys/ttycom.h */
100 struct softpart part1, part2;
101 struct prison *pt_prison;
104 #define PF_STOPPED 0x10 /* user told stopped */
107 nmdm_crossover(struct nm_softc *pti,
108 struct softpart *ourpart,
109 struct softpart *otherpart);
111 #define GETPARTS(tp, ourpart, otherpart) \
113 struct nm_softc *pti = tp->t_dev->si_drv1; \
114 if (tp == &pti->part1.nm_tty) { \
115 ourpart = &pti->part1; \
116 otherpart = &pti->part2; \
118 ourpart = &pti->part2; \
119 otherpart = &pti->part1; \
124 * This function creates and initializes a pair of ttys.
126 * NOTE: Must be called with tty_token held
135 * Simplified unit number, use low 8 bits of minor number
136 * (remember, the minor number mask is 0xffff00ff).
141 ASSERT_LWKT_TOKEN_HELD(&tty_token);
143 pt = kmalloc(sizeof(*pt), M_NLMDM, M_WAITOK | M_ZERO);
144 pt->part1.dev = dev1 = make_dev(&nmdm_ops, n << 1,
145 0, 0, 0666, "nmdm%dA", n);
146 pt->part2.dev = dev2 = make_dev(&nmdm_ops, (n << 1) + 1,
147 0, 0, 0666, "nmdm%dB", n);
149 dev1->si_drv1 = dev2->si_drv1 = pt;
150 dev1->si_tty = &pt->part1.nm_tty;
151 dev2->si_tty = &pt->part2.nm_tty;
152 ttyregister(&pt->part1.nm_tty);
153 ttyregister(&pt->part2.nm_tty);
154 pt->part1.nm_tty.t_oproc = nmdmstart;
155 pt->part2.nm_tty.t_oproc = nmdmstart;
156 pt->part1.nm_tty.t_stop = nmdmstop;
157 pt->part2.nm_tty.t_dev = dev1;
158 pt->part1.nm_tty.t_dev = dev2;
159 pt->part2.nm_tty.t_stop = nmdmstop;
164 nmdmopen(struct dev_open_args *ap)
166 cdev_t dev = ap->a_head.a_dev;
167 struct tty *tp, *tp2;
173 struct nm_softc *pti;
176 struct softpart *ourpart, *otherpart;
184 * XXX: Gross hack for DEVFS:
185 * If we openned this device, ensure we have the
186 * next one too, so people can open it.
189 nextdev = makedev(major(dev), (pair+pair) + 1);
190 if (!nextdev->si_drv1) {
201 lwkt_gettoken(&tty_token);
204 tp = &pti->part2.nm_tty;
206 tp = &pti->part1.nm_tty;
207 GETPARTS(tp, ourpart, otherpart);
208 tp2 = &otherpart->nm_tty;
209 ourpart->modemsignals |= TIOCM_LE;
211 if ((tp->t_state & TS_ISOPEN) == 0) {
212 ttychars(tp); /* Set up default chars */
213 tp->t_iflag = TTYDEF_IFLAG;
214 tp->t_oflag = TTYDEF_OFLAG;
215 tp->t_lflag = TTYDEF_LFLAG;
216 tp->t_cflag = TTYDEF_CFLAG;
217 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
218 } else if (tp->t_state & TS_XCLUDE && priv_check_cred(ap->a_cred, PRIV_ROOT, 0)) {
219 lwkt_reltoken(&tty_token);
221 } else if (pti->pt_prison != ap->a_cred->cr_prison) {
222 lwkt_reltoken(&tty_token);
227 * If the other side is open we have carrier
229 if (tp2->t_state & TS_ISOPEN) {
230 (void)(*linesw[tp->t_line].l_modem)(tp, 1);
234 * And the other side gets carrier as we are now open.
236 (void)(*linesw[tp2->t_line].l_modem)(tp2, 1);
238 /* External processing makes no sense here */
239 tp->t_lflag &= ~EXTPROC;
242 * Wait here if we don't have carrier.
245 while ((tp->t_state & TS_CARR_ON) == 0) {
246 if (flag & FNONBLOCK)
248 error = ttysleep(tp, TSA_CARR_ON(tp), PCATCH, "nmdopn", 0);
250 lwkt_reltoken(&tty_token);
257 * Give the line disciplin a chance to set this end up.
259 error = (*linesw[tp->t_line].l_open)(dev, tp);
262 * Wake up the other side.
263 * Theoretically not needed.
265 ourpart->modemsignals |= TIOCM_DTR;
266 nmdm_crossover(pti, ourpart, otherpart);
268 wakeup_other(tp, FREAD|FWRITE); /* XXX */
269 lwkt_reltoken(&tty_token);
274 nmdmclose(struct dev_close_args *ap)
276 cdev_t dev = ap->a_head.a_dev;
277 struct tty *tp, *tp2;
279 struct softpart *ourpart, *otherpart;
281 lwkt_gettoken(&tty_token);
283 * let the other end know that the game is up
286 GETPARTS(tp, ourpart, otherpart);
287 tp2 = &otherpart->nm_tty;
288 (void)(*linesw[tp2->t_line].l_modem)(tp2, 0);
291 * XXX MDMBUF makes no sense for nmdms but would inhibit the above
292 * l_modem(). CLOCAL makes sense but isn't supported. Special
293 * l_modem()s that ignore carrier drop make no sense for nmdms but
294 * may be in use because other parts of the line discipline make
295 * sense for nmdms. Recover by doing everything that a normal
296 * ttymodem() would have done except for sending a SIGHUP.
298 if (tp2->t_state & TS_ISOPEN) {
299 tp2->t_state &= ~(TS_CARR_ON | TS_CONNECTED);
300 tp2->t_state |= TS_ZOMBIE;
301 ttyflush(tp2, FREAD | FWRITE);
304 err = (*linesw[tp->t_line].l_close)(tp, ap->a_fflag);
305 ourpart->modemsignals &= ~TIOCM_DTR;
306 nmdm_crossover(dev->si_drv1, ourpart, otherpart);
307 nmdmstop(tp, FREAD|FWRITE);
309 lwkt_reltoken(&tty_token);
314 nmdmread(struct dev_read_args *ap)
316 cdev_t dev = ap->a_head.a_dev;
318 struct tty *tp, *tp2;
319 struct softpart *ourpart, *otherpart;
321 lwkt_gettoken(&tty_token);
323 GETPARTS(tp, ourpart, otherpart);
324 tp2 = &otherpart->nm_tty;
327 if (tp2->t_state & TS_ISOPEN) {
328 error = (*linesw[tp->t_line].l_read)(tp, ap->a_uio, flag);
329 wakeup_other(tp, FWRITE);
331 if (flag & IO_NDELAY) {
332 lwkt_reltoken(&tty_token);
333 return (EWOULDBLOCK);
335 error = tsleep(TSA_PTC_READ(tp), PCATCH, "nmdout", 0);
339 if ((error = (*linesw[tp->t_line].l_read)(tp, ap->a_uio, ap->a_ioflag)) == 0)
340 wakeup_other(tp, FWRITE);
342 lwkt_reltoken(&tty_token);
347 * Write to pseudo-tty.
348 * Wakeups of controlling tty will happen
349 * indirectly, when tty driver calls nmdmstart.
352 nmdmwrite(struct dev_write_args *ap)
354 cdev_t dev = ap->a_head.a_dev;
355 struct uio *uio = ap->a_uio;
358 u_char locbuf[BUFSIZ];
361 struct tty *tp1, *tp;
362 struct softpart *ourpart, *otherpart;
364 lwkt_gettoken(&tty_token);
367 * Get the other tty struct.
368 * basically we are writing into the INPUT side of the other device.
370 GETPARTS(tp1, ourpart, otherpart);
371 tp = &otherpart->nm_tty;
374 if ((tp->t_state & TS_ISOPEN) == 0) {
375 lwkt_reltoken(&tty_token);
378 while (uio->uio_resid > 0 || cc > 0) {
380 * Fill up the buffer if it's empty
383 cc = szmin(uio->uio_resid, BUFSIZ);
385 error = uiomove((caddr_t)cp, cc, uio);
387 lwkt_reltoken(&tty_token);
390 /* check again for safety */
391 if ((tp->t_state & TS_ISOPEN) == 0) {
392 /* adjust for data copied in but not written */
393 uio->uio_resid += cc;
394 lwkt_reltoken(&tty_token);
399 if (((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= (TTYHOG-2))
400 && ((tp->t_canq.c_cc > 0) || !(tp->t_iflag&ICANON))) {
402 * Come here to wait for space in outq,
403 * or space in rawq, or an empty canq.
405 wakeup(TSA_HUP_OR_INPUT(tp));
406 if ((tp->t_state & TS_CONNECTED) == 0) {
408 * Data piled up because not connected.
409 * Adjust for data copied in but
412 uio->uio_resid += cc;
413 lwkt_reltoken(&tty_token);
416 if (ap->a_ioflag & IO_NDELAY) {
418 * Don't wait if asked not to.
419 * Adjust for data copied in but
422 uio->uio_resid += cc;
424 lwkt_reltoken(&tty_token);
425 return (EWOULDBLOCK);
427 lwkt_reltoken(&tty_token);
430 error = tsleep(TSA_PTC_WRITE(tp),
431 PCATCH, "nmdout", 0);
434 * Tsleep returned (signal?).
435 * Go find out what the user wants.
436 * adjust for data copied in but
439 uio->uio_resid += cc;
440 lwkt_reltoken(&tty_token);
445 (*linesw[tp->t_line].l_rint)(*cp++, tp);
451 lwkt_reltoken(&tty_token);
456 * Start output on pseudo-tty.
457 * Wake up process selecting or sleeping for input from controlling tty.
460 nmdmstart(struct tty *tp)
462 struct nm_softc *pti = tp->t_dev->si_drv1;
464 lwkt_gettoken(&tty_token);
465 if (tp->t_state & TS_TTSTOP) {
466 lwkt_reltoken(&tty_token);
469 pti->pt_flags &= ~PF_STOPPED;
470 wakeup_other(tp, FREAD);
471 lwkt_reltoken(&tty_token);
474 /* Wakes up the OTHER tty;*/
476 wakeup_other(struct tty *tp, int flag)
478 struct softpart *ourpart, *otherpart;
480 lwkt_gettoken(&tty_token);
481 GETPARTS(tp, ourpart, otherpart);
483 wakeup(TSA_PTC_READ((&otherpart->nm_tty)));
484 KNOTE(&otherpart->nm_tty.t_rkq.ki_note, 0);
487 wakeup(TSA_PTC_WRITE((&otherpart->nm_tty)));
488 KNOTE(&otherpart->nm_tty.t_wkq.ki_note, 0);
490 lwkt_reltoken(&tty_token);
494 nmdmstop(struct tty *tp, int flush)
496 struct nm_softc *pti = tp->t_dev->si_drv1;
499 lwkt_gettoken(&tty_token);
500 /* note: FLUSHREAD and FLUSHWRITE already ok */
502 flush = TIOCPKT_STOP;
503 pti->pt_flags |= PF_STOPPED;
505 pti->pt_flags &= ~PF_STOPPED;
506 /* change of perspective */
512 wakeup_other(tp, flag);
513 lwkt_reltoken(&tty_token);
518 nmdmioctl(struct dev_ioctl_args *ap)
520 cdev_t dev = ap->a_head.a_dev;
521 struct tty *tp = dev->si_tty;
522 struct nm_softc *pti = dev->si_drv1;
525 struct softpart *ourpart, *otherpart;
528 lwkt_gettoken(&tty_token);
529 GETPARTS(tp, ourpart, otherpart);
530 tp2 = &otherpart->nm_tty;
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)