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 },
83 #define BUFSIZ 100 /* Chunk size iomoved to/from user */
88 int modemsignals; /* bits defined in sys/ttycom.h */
94 struct softpart part1, part2;
95 struct prison *pt_prison;
98 #define PF_STOPPED 0x10 /* user told stopped */
101 nmdm_crossover(struct nm_softc *pti,
102 struct softpart *ourpart,
103 struct softpart *otherpart);
105 #define GETPARTS(tp, ourpart, otherpart) \
107 struct nm_softc *pti = tp->t_dev->si_drv1; \
108 if (tp == &pti->part1.nm_tty) { \
109 ourpart = &pti->part1; \
110 otherpart = &pti->part2; \
112 ourpart = &pti->part2; \
113 otherpart = &pti->part1; \
118 * This function creates and initializes a pair of ttys.
127 * Simplified unit number, use low 8 bits of minor number
128 * (remember, the minor number mask is 0xffff00ff).
133 pt = kmalloc(sizeof(*pt), M_NLMDM, M_WAITOK | M_ZERO);
134 dev_ops_add(&nmdm_ops, ~1, n << 1);
135 pt->part1.dev = dev1 = make_dev(&nmdm_ops, n << 1,
136 0, 0, 0666, "nmdm%dA", n);
137 pt->part2.dev = dev2 = make_dev(&nmdm_ops, (n << 1) + 1,
138 0, 0, 0666, "nmdm%dB", n);
140 dev1->si_drv1 = dev2->si_drv1 = pt;
141 dev1->si_tty = &pt->part1.nm_tty;
142 dev2->si_tty = &pt->part2.nm_tty;
143 ttyregister(&pt->part1.nm_tty);
144 ttyregister(&pt->part2.nm_tty);
145 pt->part1.nm_tty.t_oproc = nmdmstart;
146 pt->part2.nm_tty.t_oproc = nmdmstart;
147 pt->part1.nm_tty.t_stop = nmdmstop;
148 pt->part2.nm_tty.t_dev = dev1;
149 pt->part1.nm_tty.t_dev = dev2;
150 pt->part2.nm_tty.t_stop = nmdmstop;
155 nmdmopen(struct dev_open_args *ap)
157 cdev_t dev = ap->a_head.a_dev;
158 struct tty *tp, *tp2;
164 struct nm_softc *pti;
167 struct softpart *ourpart, *otherpart;
175 * XXX: Gross hack for DEVFS:
176 * If we openned this device, ensure we have the
177 * next one too, so people can open it.
180 nextdev = makedev(major(dev), (pair+pair) + 1);
181 if (!nextdev->si_drv1) {
194 tp = &pti->part2.nm_tty;
196 tp = &pti->part1.nm_tty;
197 GETPARTS(tp, ourpart, otherpart);
198 tp2 = &otherpart->nm_tty;
199 ourpart->modemsignals |= TIOCM_LE;
201 if ((tp->t_state & TS_ISOPEN) == 0) {
202 ttychars(tp); /* Set up default chars */
203 tp->t_iflag = TTYDEF_IFLAG;
204 tp->t_oflag = TTYDEF_OFLAG;
205 tp->t_lflag = TTYDEF_LFLAG;
206 tp->t_cflag = TTYDEF_CFLAG;
207 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
208 } else if (tp->t_state & TS_XCLUDE && priv_check_cred(ap->a_cred, PRIV_ROOT, 0)) {
210 } else if (pti->pt_prison != ap->a_cred->cr_prison) {
215 * If the other side is open we have carrier
217 if (tp2->t_state & TS_ISOPEN) {
218 (void)(*linesw[tp->t_line].l_modem)(tp, 1);
222 * And the other side gets carrier as we are now open.
224 (void)(*linesw[tp2->t_line].l_modem)(tp2, 1);
226 /* External processing makes no sense here */
227 tp->t_lflag &= ~EXTPROC;
230 * Wait here if we don't have carrier.
233 while ((tp->t_state & TS_CARR_ON) == 0) {
234 if (flag & FNONBLOCK)
236 error = ttysleep(tp, TSA_CARR_ON(tp), PCATCH, "nmdopn", 0);
243 * Give the line disciplin a chance to set this end up.
245 error = (*linesw[tp->t_line].l_open)(dev, tp);
248 * Wake up the other side.
249 * Theoretically not needed.
251 ourpart->modemsignals |= TIOCM_DTR;
252 nmdm_crossover(pti, ourpart, otherpart);
254 wakeup_other(tp, FREAD|FWRITE); /* XXX */
259 nmdmclose(struct dev_close_args *ap)
261 cdev_t dev = ap->a_head.a_dev;
262 struct tty *tp, *tp2;
264 struct softpart *ourpart, *otherpart;
267 * let the other end know that the game is up
270 GETPARTS(tp, ourpart, otherpart);
271 tp2 = &otherpart->nm_tty;
272 (void)(*linesw[tp2->t_line].l_modem)(tp2, 0);
275 * XXX MDMBUF makes no sense for nmdms but would inhibit the above
276 * l_modem(). CLOCAL makes sense but isn't supported. Special
277 * l_modem()s that ignore carrier drop make no sense for nmdms but
278 * may be in use because other parts of the line discipline make
279 * sense for nmdms. Recover by doing everything that a normal
280 * ttymodem() would have done except for sending a SIGHUP.
282 if (tp2->t_state & TS_ISOPEN) {
283 tp2->t_state &= ~(TS_CARR_ON | TS_CONNECTED);
284 tp2->t_state |= TS_ZOMBIE;
285 ttyflush(tp2, FREAD | FWRITE);
288 err = (*linesw[tp->t_line].l_close)(tp, ap->a_fflag);
289 ourpart->modemsignals &= ~TIOCM_DTR;
290 nmdm_crossover(dev->si_drv1, ourpart, otherpart);
291 nmdmstop(tp, FREAD|FWRITE);
297 nmdmread(struct dev_read_args *ap)
299 cdev_t dev = ap->a_head.a_dev;
301 struct tty *tp, *tp2;
302 struct softpart *ourpart, *otherpart;
305 GETPARTS(tp, ourpart, otherpart);
306 tp2 = &otherpart->nm_tty;
309 if (tp2->t_state & TS_ISOPEN) {
310 error = (*linesw[tp->t_line].l_read)(tp, ap->a_uio, flag);
311 wakeup_other(tp, FWRITE);
313 if (flag & IO_NDELAY) {
314 return (EWOULDBLOCK);
316 error = tsleep(TSA_PTC_READ(tp), PCATCH, "nmdout", 0);
320 if ((error = (*linesw[tp->t_line].l_read)(tp, ap->a_uio, ap->a_ioflag)) == 0)
321 wakeup_other(tp, FWRITE);
327 * Write to pseudo-tty.
328 * Wakeups of controlling tty will happen
329 * indirectly, when tty driver calls nmdmstart.
332 nmdmwrite(struct dev_write_args *ap)
334 cdev_t dev = ap->a_head.a_dev;
335 struct uio *uio = ap->a_uio;
338 u_char locbuf[BUFSIZ];
341 struct tty *tp1, *tp;
342 struct softpart *ourpart, *otherpart;
346 * Get the other tty struct.
347 * basically we are writing into the INPUT side of the other device.
349 GETPARTS(tp1, ourpart, otherpart);
350 tp = &otherpart->nm_tty;
353 if ((tp->t_state & TS_ISOPEN) == 0)
355 while (uio->uio_resid > 0 || cc > 0) {
357 * Fill up the buffer if it's empty
360 cc = min(uio->uio_resid, BUFSIZ);
362 error = uiomove((caddr_t)cp, cc, uio);
365 /* check again for safety */
366 if ((tp->t_state & TS_ISOPEN) == 0) {
367 /* adjust for data copied in but not written */
368 uio->uio_resid += cc;
373 if (((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= (TTYHOG-2))
374 && ((tp->t_canq.c_cc > 0) || !(tp->t_iflag&ICANON))) {
376 * Come here to wait for space in outq,
377 * or space in rawq, or an empty canq.
379 wakeup(TSA_HUP_OR_INPUT(tp));
380 if ((tp->t_state & TS_CONNECTED) == 0) {
382 * Data piled up because not connected.
383 * Adjust for data copied in but
386 uio->uio_resid += cc;
389 if (ap->a_ioflag & IO_NDELAY) {
391 * Don't wait if asked not to.
392 * Adjust for data copied in but
395 uio->uio_resid += cc;
397 return (EWOULDBLOCK);
400 error = tsleep(TSA_PTC_WRITE(tp),
401 PCATCH, "nmdout", 0);
404 * Tsleep returned (signal?).
405 * Go find out what the user wants.
406 * adjust for data copied in but
409 uio->uio_resid += cc;
414 (*linesw[tp->t_line].l_rint)(*cp++, tp);
424 * Start output on pseudo-tty.
425 * Wake up process selecting or sleeping for input from controlling tty.
428 nmdmstart(struct tty *tp)
430 struct nm_softc *pti = tp->t_dev->si_drv1;
432 if (tp->t_state & TS_TTSTOP)
434 pti->pt_flags &= ~PF_STOPPED;
435 wakeup_other(tp, FREAD);
438 /* Wakes up the OTHER tty;*/
440 wakeup_other(struct tty *tp, int flag)
442 struct softpart *ourpart, *otherpart;
444 GETPARTS(tp, ourpart, otherpart);
446 selwakeup(&otherpart->nm_tty.t_rsel);
447 wakeup(TSA_PTC_READ((&otherpart->nm_tty)));
450 selwakeup(&otherpart->nm_tty.t_wsel);
451 wakeup(TSA_PTC_WRITE((&otherpart->nm_tty)));
456 nmdmstop(struct tty *tp, int flush)
458 struct nm_softc *pti = tp->t_dev->si_drv1;
461 /* note: FLUSHREAD and FLUSHWRITE already ok */
463 flush = TIOCPKT_STOP;
464 pti->pt_flags |= PF_STOPPED;
466 pti->pt_flags &= ~PF_STOPPED;
467 /* change of perspective */
473 wakeup_other(tp, flag);
478 nmdmioctl(struct dev_ioctl_args *ap)
480 cdev_t dev = ap->a_head.a_dev;
481 struct tty *tp = dev->si_tty;
482 struct nm_softc *pti = dev->si_drv1;
485 struct softpart *ourpart, *otherpart;
488 GETPARTS(tp, ourpart, otherpart);
489 tp2 = &otherpart->nm_tty;
491 error = (*linesw[tp->t_line].l_ioctl)(tp, ap->a_cmd, ap->a_data,
492 ap->a_fflag, ap->a_cred);
493 if (error == ENOIOCTL)
494 error = ttioctl(tp, ap->a_cmd, ap->a_data, ap->a_fflag);
495 if (error == ENOIOCTL) {
498 otherpart->gotbreak = 1;
503 ourpart->modemsignals |= TIOCM_DTR;
506 ourpart->modemsignals &= TIOCM_DTR;
509 ourpart->modemsignals = *(int *)ap->a_data;
510 otherpart->modemsignals = *(int *)ap->a_data;
513 ourpart->modemsignals |= *(int *)ap->a_data;
516 ourpart->modemsignals &= ~(*(int *)ap->a_data);
517 otherpart->modemsignals &= ~(*(int *)ap->a_data);
520 *(int *)ap->a_data = ourpart->modemsignals;
525 *(int *)ap->a_data = 0;
528 case TIOCDCDTIMESTAMP:
535 nmdm_crossover(pti, ourpart, otherpart);
542 nmdm_crossover(struct nm_softc *pti,
543 struct softpart *ourpart,
544 struct softpart *otherpart)
546 otherpart->modemsignals &= ~(TIOCM_CTS|TIOCM_CAR);
547 if (ourpart->modemsignals & TIOCM_RTS)
548 otherpart->modemsignals |= TIOCM_CTS;
549 if (ourpart->modemsignals & TIOCM_DTR)
550 otherpart->modemsignals |= TIOCM_CAR;
555 static void nmdm_drvinit (void *unused);
558 nmdm_drvinit(void *unused)
560 /* XXX: Gross hack for DEVFS */
564 SYSINIT(nmdmdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,nmdm_drvinit,NULL)