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>
48 #include <sys/thread2.h>
51 #include <sys/fcntl.h>
53 #include <sys/kernel.h>
54 #include <sys/vnode.h>
55 #include <sys/signalvar.h>
56 #include <sys/malloc.h>
58 MALLOC_DEFINE(M_NLMDM, "nullmodem", "nullmodem data structures");
60 static void nmdmstart (struct tty *tp);
61 static void nmdmstop (struct tty *tp, int rw);
62 static void wakeup_other (struct tty *tp, int flag);
63 static void nmdminit (int n);
65 static d_open_t nmdmopen;
66 static d_close_t nmdmclose;
67 static d_read_t nmdmread;
68 static d_write_t nmdmwrite;
69 static d_ioctl_t nmdmioctl;
72 static struct dev_ops nmdm_ops = {
73 { "pts", CDEV_MAJOR, D_TTY },
82 #define BUFSIZ 100 /* Chunk size iomoved to/from user */
87 int modemsignals; /* bits defined in sys/ttycom.h */
93 struct softpart part1, part2;
94 struct prison *pt_prison;
97 #define PF_STOPPED 0x10 /* user told stopped */
100 nmdm_crossover(struct nm_softc *pti,
101 struct softpart *ourpart,
102 struct softpart *otherpart);
104 #define GETPARTS(tp, ourpart, otherpart) \
106 struct nm_softc *pti = tp->t_dev->si_drv1; \
107 if (tp == &pti->part1.nm_tty) { \
108 ourpart = &pti->part1; \
109 otherpart = &pti->part2; \
111 ourpart = &pti->part2; \
112 otherpart = &pti->part1; \
117 * This function creates and initializes a pair of ttys.
126 * Simplified unit number, use low 8 bits of minor number
127 * (remember, the minor number mask is 0xffff00ff).
132 pt = kmalloc(sizeof(*pt), M_NLMDM, M_WAITOK | M_ZERO);
133 dev_ops_add(&nmdm_ops, ~1, n << 1);
134 pt->part1.dev = dev1 = make_dev(&nmdm_ops, n << 1,
135 0, 0, 0666, "nmdm%dA", n);
136 pt->part2.dev = dev2 = make_dev(&nmdm_ops, (n << 1) + 1,
137 0, 0, 0666, "nmdm%dB", n);
139 dev1->si_drv1 = dev2->si_drv1 = pt;
140 dev1->si_tty = &pt->part1.nm_tty;
141 dev2->si_tty = &pt->part2.nm_tty;
142 ttyregister(&pt->part1.nm_tty);
143 ttyregister(&pt->part2.nm_tty);
144 pt->part1.nm_tty.t_oproc = nmdmstart;
145 pt->part2.nm_tty.t_oproc = nmdmstart;
146 pt->part1.nm_tty.t_stop = nmdmstop;
147 pt->part2.nm_tty.t_dev = dev1;
148 pt->part1.nm_tty.t_dev = dev2;
149 pt->part2.nm_tty.t_stop = nmdmstop;
154 nmdmopen(struct dev_open_args *ap)
156 cdev_t dev = ap->a_head.a_dev;
157 struct tty *tp, *tp2;
163 struct nm_softc *pti;
166 struct softpart *ourpart, *otherpart;
174 * XXX: Gross hack for DEVFS:
175 * If we openned this device, ensure we have the
176 * next one too, so people can open it.
179 nextdev = makedev(major(dev), (pair+pair) + 1);
180 if (!nextdev->si_drv1) {
193 tp = &pti->part2.nm_tty;
195 tp = &pti->part1.nm_tty;
196 GETPARTS(tp, ourpart, otherpart);
197 tp2 = &otherpart->nm_tty;
198 ourpart->modemsignals |= TIOCM_LE;
200 if ((tp->t_state & TS_ISOPEN) == 0) {
201 ttychars(tp); /* Set up default chars */
202 tp->t_iflag = TTYDEF_IFLAG;
203 tp->t_oflag = TTYDEF_OFLAG;
204 tp->t_lflag = TTYDEF_LFLAG;
205 tp->t_cflag = TTYDEF_CFLAG;
206 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
207 } else if (tp->t_state & TS_XCLUDE && priv_check_cred(ap->a_cred, PRIV_ROOT, 0)) {
209 } else if (pti->pt_prison != ap->a_cred->cr_prison) {
214 * If the other side is open we have carrier
216 if (tp2->t_state & TS_ISOPEN) {
217 (void)(*linesw[tp->t_line].l_modem)(tp, 1);
221 * And the other side gets carrier as we are now open.
223 (void)(*linesw[tp2->t_line].l_modem)(tp2, 1);
225 /* External processing makes no sense here */
226 tp->t_lflag &= ~EXTPROC;
229 * Wait here if we don't have carrier.
232 while ((tp->t_state & TS_CARR_ON) == 0) {
233 if (flag & FNONBLOCK)
235 error = ttysleep(tp, TSA_CARR_ON(tp), PCATCH, "nmdopn", 0);
242 * Give the line disciplin a chance to set this end up.
244 error = (*linesw[tp->t_line].l_open)(dev, tp);
247 * Wake up the other side.
248 * Theoretically not needed.
250 ourpart->modemsignals |= TIOCM_DTR;
251 nmdm_crossover(pti, ourpart, otherpart);
253 wakeup_other(tp, FREAD|FWRITE); /* XXX */
258 nmdmclose(struct dev_close_args *ap)
260 cdev_t dev = ap->a_head.a_dev;
261 struct tty *tp, *tp2;
263 struct softpart *ourpart, *otherpart;
266 * let the other end know that the game is up
269 GETPARTS(tp, ourpart, otherpart);
270 tp2 = &otherpart->nm_tty;
271 (void)(*linesw[tp2->t_line].l_modem)(tp2, 0);
274 * XXX MDMBUF makes no sense for nmdms but would inhibit the above
275 * l_modem(). CLOCAL makes sense but isn't supported. Special
276 * l_modem()s that ignore carrier drop make no sense for nmdms but
277 * may be in use because other parts of the line discipline make
278 * sense for nmdms. Recover by doing everything that a normal
279 * ttymodem() would have done except for sending a SIGHUP.
281 if (tp2->t_state & TS_ISOPEN) {
282 tp2->t_state &= ~(TS_CARR_ON | TS_CONNECTED);
283 tp2->t_state |= TS_ZOMBIE;
284 ttyflush(tp2, FREAD | FWRITE);
287 err = (*linesw[tp->t_line].l_close)(tp, ap->a_fflag);
288 ourpart->modemsignals &= ~TIOCM_DTR;
289 nmdm_crossover(dev->si_drv1, ourpart, otherpart);
290 nmdmstop(tp, FREAD|FWRITE);
296 nmdmread(struct dev_read_args *ap)
298 cdev_t dev = ap->a_head.a_dev;
300 struct tty *tp, *tp2;
301 struct softpart *ourpart, *otherpart;
304 GETPARTS(tp, ourpart, otherpart);
305 tp2 = &otherpart->nm_tty;
308 if (tp2->t_state & TS_ISOPEN) {
309 error = (*linesw[tp->t_line].l_read)(tp, ap->a_uio, flag);
310 wakeup_other(tp, FWRITE);
312 if (flag & IO_NDELAY) {
313 return (EWOULDBLOCK);
315 error = tsleep(TSA_PTC_READ(tp), PCATCH, "nmdout", 0);
319 if ((error = (*linesw[tp->t_line].l_read)(tp, ap->a_uio, ap->a_ioflag)) == 0)
320 wakeup_other(tp, FWRITE);
326 * Write to pseudo-tty.
327 * Wakeups of controlling tty will happen
328 * indirectly, when tty driver calls nmdmstart.
331 nmdmwrite(struct dev_write_args *ap)
333 cdev_t dev = ap->a_head.a_dev;
334 struct uio *uio = ap->a_uio;
337 u_char locbuf[BUFSIZ];
340 struct tty *tp1, *tp;
341 struct softpart *ourpart, *otherpart;
345 * Get the other tty struct.
346 * basically we are writing into the INPUT side of the other device.
348 GETPARTS(tp1, ourpart, otherpart);
349 tp = &otherpart->nm_tty;
352 if ((tp->t_state & TS_ISOPEN) == 0)
354 while (uio->uio_resid > 0 || cc > 0) {
356 * Fill up the buffer if it's empty
359 cc = min(uio->uio_resid, BUFSIZ);
361 error = uiomove((caddr_t)cp, cc, uio);
364 /* check again for safety */
365 if ((tp->t_state & TS_ISOPEN) == 0) {
366 /* adjust for data copied in but not written */
367 uio->uio_resid += cc;
372 if (((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= (TTYHOG-2))
373 && ((tp->t_canq.c_cc > 0) || !(tp->t_iflag&ICANON))) {
375 * Come here to wait for space in outq,
376 * or space in rawq, or an empty canq.
378 wakeup(TSA_HUP_OR_INPUT(tp));
379 if ((tp->t_state & TS_CONNECTED) == 0) {
381 * Data piled up because not connected.
382 * Adjust for data copied in but
385 uio->uio_resid += cc;
388 if (ap->a_ioflag & IO_NDELAY) {
390 * Don't wait if asked not to.
391 * Adjust for data copied in but
394 uio->uio_resid += cc;
396 return (EWOULDBLOCK);
399 error = tsleep(TSA_PTC_WRITE(tp),
400 PCATCH, "nmdout", 0);
403 * Tsleep returned (signal?).
404 * Go find out what the user wants.
405 * adjust for data copied in but
408 uio->uio_resid += cc;
413 (*linesw[tp->t_line].l_rint)(*cp++, tp);
423 * Start output on pseudo-tty.
424 * Wake up process selecting or sleeping for input from controlling tty.
427 nmdmstart(struct tty *tp)
429 struct nm_softc *pti = tp->t_dev->si_drv1;
431 if (tp->t_state & TS_TTSTOP)
433 pti->pt_flags &= ~PF_STOPPED;
434 wakeup_other(tp, FREAD);
437 /* Wakes up the OTHER tty;*/
439 wakeup_other(struct tty *tp, int flag)
441 struct softpart *ourpart, *otherpart;
443 GETPARTS(tp, ourpart, otherpart);
445 selwakeup(&otherpart->nm_tty.t_rsel);
446 wakeup(TSA_PTC_READ((&otherpart->nm_tty)));
449 selwakeup(&otherpart->nm_tty.t_wsel);
450 wakeup(TSA_PTC_WRITE((&otherpart->nm_tty)));
455 nmdmstop(struct tty *tp, int flush)
457 struct nm_softc *pti = tp->t_dev->si_drv1;
460 /* note: FLUSHREAD and FLUSHWRITE already ok */
462 flush = TIOCPKT_STOP;
463 pti->pt_flags |= PF_STOPPED;
465 pti->pt_flags &= ~PF_STOPPED;
466 /* change of perspective */
472 wakeup_other(tp, flag);
477 nmdmioctl(struct dev_ioctl_args *ap)
479 cdev_t dev = ap->a_head.a_dev;
480 struct tty *tp = dev->si_tty;
481 struct nm_softc *pti = dev->si_drv1;
484 struct softpart *ourpart, *otherpart;
487 GETPARTS(tp, ourpart, otherpart);
488 tp2 = &otherpart->nm_tty;
490 error = (*linesw[tp->t_line].l_ioctl)(tp, ap->a_cmd, ap->a_data,
491 ap->a_fflag, ap->a_cred);
492 if (error == ENOIOCTL)
493 error = ttioctl(tp, ap->a_cmd, ap->a_data, ap->a_fflag);
494 if (error == ENOIOCTL) {
497 otherpart->gotbreak = 1;
502 ourpart->modemsignals |= TIOCM_DTR;
505 ourpart->modemsignals &= TIOCM_DTR;
508 ourpart->modemsignals = *(int *)ap->a_data;
509 otherpart->modemsignals = *(int *)ap->a_data;
512 ourpart->modemsignals |= *(int *)ap->a_data;
515 ourpart->modemsignals &= ~(*(int *)ap->a_data);
516 otherpart->modemsignals &= ~(*(int *)ap->a_data);
519 *(int *)ap->a_data = ourpart->modemsignals;
524 *(int *)ap->a_data = 0;
527 case TIOCDCDTIMESTAMP:
534 nmdm_crossover(pti, ourpart, otherpart);
541 nmdm_crossover(struct nm_softc *pti,
542 struct softpart *ourpart,
543 struct softpart *otherpart)
545 otherpart->modemsignals &= ~(TIOCM_CTS|TIOCM_CAR);
546 if (ourpart->modemsignals & TIOCM_RTS)
547 otherpart->modemsignals |= TIOCM_CTS;
548 if (ourpart->modemsignals & TIOCM_DTR)
549 otherpart->modemsignals |= TIOCM_CAR;
554 static void nmdm_drvinit (void *unused);
557 nmdm_drvinit(void *unused)
559 /* XXX: Gross hack for DEVFS */
563 SYSINIT(nmdmdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,nmdm_drvinit,NULL)