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 $
37 * Pseudo-nulmodem Driver
39 #include "opt_compat.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
43 #include <sys/ioctl_compat.h>
48 #include <sys/fcntl.h>
50 #include <sys/kernel.h>
51 #include <sys/vnode.h>
52 #include <sys/signalvar.h>
53 #include <sys/malloc.h>
55 MALLOC_DEFINE(M_NLMDM, "nullmodem", "nullmodem data structures");
57 static void nmdmstart __P((struct tty *tp));
58 static void nmdmstop __P((struct tty *tp, int rw));
59 static void wakeup_other __P((struct tty *tp, int flag));
60 static void nmdminit __P((int n));
62 static d_open_t nmdmopen;
63 static d_close_t nmdmclose;
64 static d_read_t nmdmread;
65 static d_write_t nmdmwrite;
66 static d_ioctl_t nmdmioctl;
69 static struct cdevsw nmdm_cdevsw = {
71 /* close */ nmdmclose,
73 /* write */ nmdmwrite,
74 /* ioctl */ nmdmioctl,
77 /* strategy */ nostrategy,
86 #define BUFSIZ 100 /* Chunk size iomoved to/from user */
91 int modemsignals; /* bits defined in sys/ttycom.h */
97 struct softpart part1, part2;
98 struct prison *pt_prison;
101 #define PF_STOPPED 0x10 /* user told stopped */
104 nmdm_crossover(struct nm_softc *pti,
105 struct softpart *ourpart,
106 struct softpart *otherpart);
108 #define GETPARTS(tp, ourpart, otherpart) \
110 struct nm_softc *pti = tp->t_dev->si_drv1; \
111 if (tp == &pti->part1.nm_tty) { \
112 ourpart = &pti->part1; \
113 otherpart = &pti->part2; \
115 ourpart = &pti->part2; \
116 otherpart = &pti->part1; \
121 * This function creates and initializes a pair of ttys.
130 /* For now we only map the lower 8 bits of the minor */
134 pt = malloc(sizeof(*pt), M_NLMDM, M_WAITOK);
135 bzero(pt, sizeof(*pt));
136 pt->part1.dev = dev1 = make_dev(&nmdm_cdevsw, n+n,
137 0, 0, 0666, "nmdm%dA", n);
138 pt->part2.dev = dev2 = make_dev(&nmdm_cdevsw, n+n+1,
139 0, 0, 0666, "nmdm%dB", n);
141 dev1->si_drv1 = dev2->si_drv1 = pt;
142 dev1->si_tty = &pt->part1.nm_tty;
143 dev2->si_tty = &pt->part2.nm_tty;
144 ttyregister(&pt->part1.nm_tty);
145 ttyregister(&pt->part2.nm_tty);
146 pt->part1.nm_tty.t_oproc = nmdmstart;
147 pt->part2.nm_tty.t_oproc = nmdmstart;
148 pt->part1.nm_tty.t_stop = nmdmstop;
149 pt->part2.nm_tty.t_dev = dev1;
150 pt->part1.nm_tty.t_dev = dev2;
151 pt->part2.nm_tty.t_stop = nmdmstop;
156 nmdmopen(dev, flag, devtype, p)
161 register struct tty *tp, *tp2;
165 struct nm_softc *pti;
168 struct softpart *ourpart, *otherpart;
171 * XXX: Gross hack for DEVFS:
172 * If we openned this device, ensure we have the
173 * next one too, so people can open it.
180 nextdev = makedev(major(dev), (pair+pair) + 1);
181 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 && suser(p)) {
209 } else if (pti->pt_prison != p->p_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), TTIPRI | PCATCH,
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(dev, flag, mode, p)
264 register struct tty *tp, *tp2;
266 struct softpart *ourpart, *otherpart;
269 * let the other end know that the game is up
272 GETPARTS(tp, ourpart, otherpart);
273 tp2 = &otherpart->nm_tty;
274 (void)(*linesw[tp2->t_line].l_modem)(tp2, 0);
277 * XXX MDMBUF makes no sense for nmdms but would inhibit the above
278 * l_modem(). CLOCAL makes sense but isn't supported. Special
279 * l_modem()s that ignore carrier drop make no sense for nmdms but
280 * may be in use because other parts of the line discipline make
281 * sense for nmdms. Recover by doing everything that a normal
282 * ttymodem() would have done except for sending a SIGHUP.
284 if (tp2->t_state & TS_ISOPEN) {
285 tp2->t_state &= ~(TS_CARR_ON | TS_CONNECTED);
286 tp2->t_state |= TS_ZOMBIE;
287 ttyflush(tp2, FREAD | FWRITE);
290 err = (*linesw[tp->t_line].l_close)(tp, flag);
291 ourpart->modemsignals &= ~TIOCM_DTR;
292 nmdm_crossover(dev->si_drv1, ourpart, otherpart);
293 nmdmstop(tp, FREAD|FWRITE);
299 nmdmread(dev, uio, flag)
305 struct tty *tp, *tp2;
306 struct softpart *ourpart, *otherpart;
309 GETPARTS(tp, ourpart, otherpart);
310 tp2 = &otherpart->nm_tty;
313 if (tp2->t_state & TS_ISOPEN) {
314 error = (*linesw[tp->t_line].l_read)(tp, uio, flag);
315 wakeup_other(tp, FWRITE);
317 if (flag & IO_NDELAY) {
318 return (EWOULDBLOCK);
320 error = tsleep(TSA_PTC_READ(tp),
321 TTIPRI | PCATCH, "nmdout", 0);
325 if ((error = (*linesw[tp->t_line].l_read)(tp, uio, flag)) == 0)
326 wakeup_other(tp, FWRITE);
332 * Write to pseudo-tty.
333 * Wakeups of controlling tty will happen
334 * indirectly, when tty driver calls nmdmstart.
337 nmdmwrite(dev, uio, flag)
342 register u_char *cp = 0;
344 u_char locbuf[BUFSIZ];
347 struct tty *tp1, *tp;
348 struct softpart *ourpart, *otherpart;
352 * Get the other tty struct.
353 * basically we are writing into the INPUT side of the other device.
355 GETPARTS(tp1, ourpart, otherpart);
356 tp = &otherpart->nm_tty;
359 if ((tp->t_state & TS_ISOPEN) == 0)
361 while (uio->uio_resid > 0 || cc > 0) {
363 * Fill up the buffer if it's empty
366 cc = min(uio->uio_resid, BUFSIZ);
368 error = uiomove((caddr_t)cp, cc, uio);
371 /* check again for safety */
372 if ((tp->t_state & TS_ISOPEN) == 0) {
373 /* adjust for data copied in but not written */
374 uio->uio_resid += cc;
379 if (((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= (TTYHOG-2))
380 && ((tp->t_canq.c_cc > 0) || !(tp->t_iflag&ICANON))) {
382 * Come here to wait for space in outq,
383 * or space in rawq, or an empty canq.
385 wakeup(TSA_HUP_OR_INPUT(tp));
386 if ((tp->t_state & TS_CONNECTED) == 0) {
388 * Data piled up because not connected.
389 * Adjust for data copied in but
392 uio->uio_resid += cc;
395 if (flag & IO_NDELAY) {
397 * Don't wait if asked not to.
398 * Adjust for data copied in but
401 uio->uio_resid += cc;
403 return (EWOULDBLOCK);
406 error = tsleep(TSA_PTC_WRITE(tp),
407 TTOPRI | PCATCH, "nmdout", 0);
410 * Tsleep returned (signal?).
411 * Go find out what the user wants.
412 * adjust for data copied in but
415 uio->uio_resid += cc;
420 (*linesw[tp->t_line].l_rint)(*cp++, tp);
430 * Start output on pseudo-tty.
431 * Wake up process selecting or sleeping for input from controlling tty.
437 register struct nm_softc *pti = tp->t_dev->si_drv1;
439 if (tp->t_state & TS_TTSTOP)
441 pti->pt_flags &= ~PF_STOPPED;
442 wakeup_other(tp, FREAD);
445 /* Wakes up the OTHER tty;*/
447 wakeup_other(tp, flag)
451 struct softpart *ourpart, *otherpart;
453 GETPARTS(tp, ourpart, otherpart);
455 selwakeup(&otherpart->nm_tty.t_rsel);
456 wakeup(TSA_PTC_READ((&otherpart->nm_tty)));
459 selwakeup(&otherpart->nm_tty.t_wsel);
460 wakeup(TSA_PTC_WRITE((&otherpart->nm_tty)));
466 register struct tty *tp;
469 struct nm_softc *pti = tp->t_dev->si_drv1;
472 /* note: FLUSHREAD and FLUSHWRITE already ok */
474 flush = TIOCPKT_STOP;
475 pti->pt_flags |= PF_STOPPED;
477 pti->pt_flags &= ~PF_STOPPED;
478 /* change of perspective */
484 wakeup_other(tp, flag);
489 nmdmioctl(dev, cmd, data, flag, p)
496 register struct tty *tp = dev->si_tty;
497 struct nm_softc *pti = dev->si_drv1;
499 register struct tty *tp2;
500 struct softpart *ourpart, *otherpart;
503 GETPARTS(tp, ourpart, otherpart);
504 tp2 = &otherpart->nm_tty;
506 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
507 if (error == ENOIOCTL)
508 error = ttioctl(tp, cmd, data, flag);
509 if (error == ENOIOCTL) {
512 otherpart->gotbreak = 1;
517 ourpart->modemsignals |= TIOCM_DTR;
520 ourpart->modemsignals &= TIOCM_DTR;
523 ourpart->modemsignals = *(int *)data;
524 otherpart->modemsignals = *(int *)data;
527 ourpart->modemsignals |= *(int *)data;
530 ourpart->modemsignals &= ~(*(int *)data);
531 otherpart->modemsignals &= ~(*(int *)data);
534 *(int *)data = ourpart->modemsignals;
542 case TIOCDCDTIMESTAMP:
549 nmdm_crossover(pti, ourpart, otherpart);
556 nmdm_crossover(struct nm_softc *pti,
557 struct softpart *ourpart,
558 struct softpart *otherpart)
560 otherpart->modemsignals &= ~(TIOCM_CTS|TIOCM_CAR);
561 if (ourpart->modemsignals & TIOCM_RTS)
562 otherpart->modemsignals |= TIOCM_CTS;
563 if (ourpart->modemsignals & TIOCM_DTR)
564 otherpart->modemsignals |= TIOCM_CAR;
569 static void nmdm_drvinit __P((void *unused));
575 cdevsw_add(&nmdm_cdevsw);
576 /* XXX: Gross hack for DEVFS */
580 SYSINIT(nmdmdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,nmdm_drvinit,NULL)