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 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * $FreeBSD: src/sys/dev/nmdm/nmdm.c,v 1.5.2.1 2001/08/11 00:54:14 mp Exp $
34 * $DragonFly: src/sys/dev/misc/nmdm/nmdm.c,v 1.16 2008/01/05 14:02:37 swildner Exp $
38 * Pseudo-nulmodem Driver
40 #include "opt_compat.h"
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
44 #include <sys/ioctl_compat.h>
47 #include <sys/thread2.h>
50 #include <sys/fcntl.h>
52 #include <sys/kernel.h>
53 #include <sys/vnode.h>
54 #include <sys/signalvar.h>
55 #include <sys/malloc.h>
57 MALLOC_DEFINE(M_NLMDM, "nullmodem", "nullmodem data structures");
59 static void nmdmstart (struct tty *tp);
60 static void nmdmstop (struct tty *tp, int rw);
61 static void wakeup_other (struct tty *tp, int flag);
62 static void nmdminit (int n);
64 static d_open_t nmdmopen;
65 static d_close_t nmdmclose;
66 static d_read_t nmdmread;
67 static d_write_t nmdmwrite;
68 static d_ioctl_t nmdmioctl;
71 static struct dev_ops nmdm_ops = {
72 { "pts", CDEV_MAJOR, D_TTY },
81 #define BUFSIZ 100 /* Chunk size iomoved to/from user */
86 int modemsignals; /* bits defined in sys/ttycom.h */
92 struct softpart part1, part2;
93 struct prison *pt_prison;
96 #define PF_STOPPED 0x10 /* user told stopped */
99 nmdm_crossover(struct nm_softc *pti,
100 struct softpart *ourpart,
101 struct softpart *otherpart);
103 #define GETPARTS(tp, ourpart, otherpart) \
105 struct nm_softc *pti = tp->t_dev->si_drv1; \
106 if (tp == &pti->part1.nm_tty) { \
107 ourpart = &pti->part1; \
108 otherpart = &pti->part2; \
110 ourpart = &pti->part2; \
111 otherpart = &pti->part1; \
116 * This function creates and initializes a pair of ttys.
125 * Simplified unit number, use low 8 bits of minor number
126 * (remember, the minor number mask is 0xffff00ff).
131 pt = kmalloc(sizeof(*pt), M_NLMDM, M_WAITOK | M_ZERO);
132 dev_ops_add(&nmdm_ops, ~1, n << 1);
133 pt->part1.dev = dev1 = make_dev(&nmdm_ops, n << 1,
134 0, 0, 0666, "nmdm%dA", n);
135 pt->part2.dev = dev2 = make_dev(&nmdm_ops, (n << 1) + 1,
136 0, 0, 0666, "nmdm%dB", n);
138 dev1->si_drv1 = dev2->si_drv1 = pt;
139 dev1->si_tty = &pt->part1.nm_tty;
140 dev2->si_tty = &pt->part2.nm_tty;
141 ttyregister(&pt->part1.nm_tty);
142 ttyregister(&pt->part2.nm_tty);
143 pt->part1.nm_tty.t_oproc = nmdmstart;
144 pt->part2.nm_tty.t_oproc = nmdmstart;
145 pt->part1.nm_tty.t_stop = nmdmstop;
146 pt->part2.nm_tty.t_dev = dev1;
147 pt->part1.nm_tty.t_dev = dev2;
148 pt->part2.nm_tty.t_stop = nmdmstop;
153 nmdmopen(struct dev_open_args *ap)
155 cdev_t dev = ap->a_head.a_dev;
156 struct tty *tp, *tp2;
162 struct nm_softc *pti;
165 struct softpart *ourpart, *otherpart;
173 * XXX: Gross hack for DEVFS:
174 * If we openned this device, ensure we have the
175 * next one too, so people can open it.
178 nextdev = makedev(major(dev), (pair+pair) + 1);
179 if (!nextdev->si_drv1) {
192 tp = &pti->part2.nm_tty;
194 tp = &pti->part1.nm_tty;
195 GETPARTS(tp, ourpart, otherpart);
196 tp2 = &otherpart->nm_tty;
197 ourpart->modemsignals |= TIOCM_LE;
199 if ((tp->t_state & TS_ISOPEN) == 0) {
200 ttychars(tp); /* Set up default chars */
201 tp->t_iflag = TTYDEF_IFLAG;
202 tp->t_oflag = TTYDEF_OFLAG;
203 tp->t_lflag = TTYDEF_LFLAG;
204 tp->t_cflag = TTYDEF_CFLAG;
205 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
206 } else if (tp->t_state & TS_XCLUDE && suser_cred(ap->a_cred, 0)) {
208 } else if (pti->pt_prison != ap->a_cred->cr_prison) {
213 * If the other side is open we have carrier
215 if (tp2->t_state & TS_ISOPEN) {
216 (void)(*linesw[tp->t_line].l_modem)(tp, 1);
220 * And the other side gets carrier as we are now open.
222 (void)(*linesw[tp2->t_line].l_modem)(tp2, 1);
224 /* External processing makes no sense here */
225 tp->t_lflag &= ~EXTPROC;
228 * Wait here if we don't have carrier.
231 while ((tp->t_state & TS_CARR_ON) == 0) {
232 if (flag & FNONBLOCK)
234 error = ttysleep(tp, TSA_CARR_ON(tp), PCATCH, "nmdopn", 0);
241 * Give the line disciplin a chance to set this end up.
243 error = (*linesw[tp->t_line].l_open)(dev, tp);
246 * Wake up the other side.
247 * Theoretically not needed.
249 ourpart->modemsignals |= TIOCM_DTR;
250 nmdm_crossover(pti, ourpart, otherpart);
252 wakeup_other(tp, FREAD|FWRITE); /* XXX */
257 nmdmclose(struct dev_close_args *ap)
259 cdev_t dev = ap->a_head.a_dev;
260 struct tty *tp, *tp2;
262 struct softpart *ourpart, *otherpart;
265 * let the other end know that the game is up
268 GETPARTS(tp, ourpart, otherpart);
269 tp2 = &otherpart->nm_tty;
270 (void)(*linesw[tp2->t_line].l_modem)(tp2, 0);
273 * XXX MDMBUF makes no sense for nmdms but would inhibit the above
274 * l_modem(). CLOCAL makes sense but isn't supported. Special
275 * l_modem()s that ignore carrier drop make no sense for nmdms but
276 * may be in use because other parts of the line discipline make
277 * sense for nmdms. Recover by doing everything that a normal
278 * ttymodem() would have done except for sending a SIGHUP.
280 if (tp2->t_state & TS_ISOPEN) {
281 tp2->t_state &= ~(TS_CARR_ON | TS_CONNECTED);
282 tp2->t_state |= TS_ZOMBIE;
283 ttyflush(tp2, FREAD | FWRITE);
286 err = (*linesw[tp->t_line].l_close)(tp, ap->a_fflag);
287 ourpart->modemsignals &= ~TIOCM_DTR;
288 nmdm_crossover(dev->si_drv1, ourpart, otherpart);
289 nmdmstop(tp, FREAD|FWRITE);
295 nmdmread(struct dev_read_args *ap)
297 cdev_t dev = ap->a_head.a_dev;
299 struct tty *tp, *tp2;
300 struct softpart *ourpart, *otherpart;
303 GETPARTS(tp, ourpart, otherpart);
304 tp2 = &otherpart->nm_tty;
307 if (tp2->t_state & TS_ISOPEN) {
308 error = (*linesw[tp->t_line].l_read)(tp, ap->a_uio, flag);
309 wakeup_other(tp, FWRITE);
311 if (flag & IO_NDELAY) {
312 return (EWOULDBLOCK);
314 error = tsleep(TSA_PTC_READ(tp), PCATCH, "nmdout", 0);
318 if ((error = (*linesw[tp->t_line].l_read)(tp, ap->a_uio, ap->a_ioflag)) == 0)
319 wakeup_other(tp, FWRITE);
325 * Write to pseudo-tty.
326 * Wakeups of controlling tty will happen
327 * indirectly, when tty driver calls nmdmstart.
330 nmdmwrite(struct dev_write_args *ap)
332 cdev_t dev = ap->a_head.a_dev;
333 struct uio *uio = ap->a_uio;
336 u_char locbuf[BUFSIZ];
339 struct tty *tp1, *tp;
340 struct softpart *ourpart, *otherpart;
344 * Get the other tty struct.
345 * basically we are writing into the INPUT side of the other device.
347 GETPARTS(tp1, ourpart, otherpart);
348 tp = &otherpart->nm_tty;
351 if ((tp->t_state & TS_ISOPEN) == 0)
353 while (uio->uio_resid > 0 || cc > 0) {
355 * Fill up the buffer if it's empty
358 cc = min(uio->uio_resid, BUFSIZ);
360 error = uiomove((caddr_t)cp, cc, uio);
363 /* check again for safety */
364 if ((tp->t_state & TS_ISOPEN) == 0) {
365 /* adjust for data copied in but not written */
366 uio->uio_resid += cc;
371 if (((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= (TTYHOG-2))
372 && ((tp->t_canq.c_cc > 0) || !(tp->t_iflag&ICANON))) {
374 * Come here to wait for space in outq,
375 * or space in rawq, or an empty canq.
377 wakeup(TSA_HUP_OR_INPUT(tp));
378 if ((tp->t_state & TS_CONNECTED) == 0) {
380 * Data piled up because not connected.
381 * Adjust for data copied in but
384 uio->uio_resid += cc;
387 if (ap->a_ioflag & IO_NDELAY) {
389 * Don't wait if asked not to.
390 * Adjust for data copied in but
393 uio->uio_resid += cc;
395 return (EWOULDBLOCK);
398 error = tsleep(TSA_PTC_WRITE(tp),
399 PCATCH, "nmdout", 0);
402 * Tsleep returned (signal?).
403 * Go find out what the user wants.
404 * adjust for data copied in but
407 uio->uio_resid += cc;
412 (*linesw[tp->t_line].l_rint)(*cp++, tp);
422 * Start output on pseudo-tty.
423 * Wake up process selecting or sleeping for input from controlling tty.
426 nmdmstart(struct tty *tp)
428 struct nm_softc *pti = tp->t_dev->si_drv1;
430 if (tp->t_state & TS_TTSTOP)
432 pti->pt_flags &= ~PF_STOPPED;
433 wakeup_other(tp, FREAD);
436 /* Wakes up the OTHER tty;*/
438 wakeup_other(struct tty *tp, int flag)
440 struct softpart *ourpart, *otherpart;
442 GETPARTS(tp, ourpart, otherpart);
444 selwakeup(&otherpart->nm_tty.t_rsel);
445 wakeup(TSA_PTC_READ((&otherpart->nm_tty)));
448 selwakeup(&otherpart->nm_tty.t_wsel);
449 wakeup(TSA_PTC_WRITE((&otherpart->nm_tty)));
454 nmdmstop(struct tty *tp, int flush)
456 struct nm_softc *pti = tp->t_dev->si_drv1;
459 /* note: FLUSHREAD and FLUSHWRITE already ok */
461 flush = TIOCPKT_STOP;
462 pti->pt_flags |= PF_STOPPED;
464 pti->pt_flags &= ~PF_STOPPED;
465 /* change of perspective */
471 wakeup_other(tp, flag);
476 nmdmioctl(struct dev_ioctl_args *ap)
478 cdev_t dev = ap->a_head.a_dev;
479 struct tty *tp = dev->si_tty;
480 struct nm_softc *pti = dev->si_drv1;
483 struct softpart *ourpart, *otherpart;
486 GETPARTS(tp, ourpart, otherpart);
487 tp2 = &otherpart->nm_tty;
489 error = (*linesw[tp->t_line].l_ioctl)(tp, ap->a_cmd, ap->a_data,
490 ap->a_fflag, ap->a_cred);
491 if (error == ENOIOCTL)
492 error = ttioctl(tp, ap->a_cmd, ap->a_data, ap->a_fflag);
493 if (error == ENOIOCTL) {
496 otherpart->gotbreak = 1;
501 ourpart->modemsignals |= TIOCM_DTR;
504 ourpart->modemsignals &= TIOCM_DTR;
507 ourpart->modemsignals = *(int *)ap->a_data;
508 otherpart->modemsignals = *(int *)ap->a_data;
511 ourpart->modemsignals |= *(int *)ap->a_data;
514 ourpart->modemsignals &= ~(*(int *)ap->a_data);
515 otherpart->modemsignals &= ~(*(int *)ap->a_data);
518 *(int *)ap->a_data = ourpart->modemsignals;
523 *(int *)ap->a_data = 0;
526 case TIOCDCDTIMESTAMP:
533 nmdm_crossover(pti, ourpart, otherpart);
540 nmdm_crossover(struct nm_softc *pti,
541 struct softpart *ourpart,
542 struct softpart *otherpart)
544 otherpart->modemsignals &= ~(TIOCM_CTS|TIOCM_CAR);
545 if (ourpart->modemsignals & TIOCM_RTS)
546 otherpart->modemsignals |= TIOCM_CTS;
547 if (ourpart->modemsignals & TIOCM_DTR)
548 otherpart->modemsignals |= TIOCM_CAR;
553 static void nmdm_drvinit (void *unused);
556 nmdm_drvinit(void *unused)
558 /* XXX: Gross hack for DEVFS */
562 SYSINIT(nmdmdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,nmdm_drvinit,NULL)