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.5 2003/07/19 21:14:25 dillon 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>
49 #include <sys/fcntl.h>
51 #include <sys/kernel.h>
52 #include <sys/vnode.h>
53 #include <sys/signalvar.h>
54 #include <sys/malloc.h>
56 MALLOC_DEFINE(M_NLMDM, "nullmodem", "nullmodem data structures");
58 static void nmdmstart __P((struct tty *tp));
59 static void nmdmstop __P((struct tty *tp, int rw));
60 static void wakeup_other __P((struct tty *tp, int flag));
61 static void nmdminit __P((int n));
63 static d_open_t nmdmopen;
64 static d_close_t nmdmclose;
65 static d_read_t nmdmread;
66 static d_write_t nmdmwrite;
67 static d_ioctl_t nmdmioctl;
70 static struct cdevsw nmdm_cdevsw = {
72 /* close */ nmdmclose,
74 /* write */ nmdmwrite,
75 /* ioctl */ nmdmioctl,
78 /* strategy */ nostrategy,
87 #define BUFSIZ 100 /* Chunk size iomoved to/from user */
92 int modemsignals; /* bits defined in sys/ttycom.h */
98 struct softpart part1, part2;
99 struct prison *pt_prison;
102 #define PF_STOPPED 0x10 /* user told stopped */
105 nmdm_crossover(struct nm_softc *pti,
106 struct softpart *ourpart,
107 struct softpart *otherpart);
109 #define GETPARTS(tp, ourpart, otherpart) \
111 struct nm_softc *pti = tp->t_dev->si_drv1; \
112 if (tp == &pti->part1.nm_tty) { \
113 ourpart = &pti->part1; \
114 otherpart = &pti->part2; \
116 ourpart = &pti->part2; \
117 otherpart = &pti->part1; \
122 * This function creates and initializes a pair of ttys.
131 /* For now we only map the lower 8 bits of the minor */
135 pt = malloc(sizeof(*pt), M_NLMDM, M_WAITOK);
136 bzero(pt, sizeof(*pt));
137 pt->part1.dev = dev1 = make_dev(&nmdm_cdevsw, n+n,
138 0, 0, 0666, "nmdm%dA", n);
139 pt->part2.dev = dev2 = make_dev(&nmdm_cdevsw, n+n+1,
140 0, 0, 0666, "nmdm%dB", n);
142 dev1->si_drv1 = dev2->si_drv1 = pt;
143 dev1->si_tty = &pt->part1.nm_tty;
144 dev2->si_tty = &pt->part2.nm_tty;
145 ttyregister(&pt->part1.nm_tty);
146 ttyregister(&pt->part2.nm_tty);
147 pt->part1.nm_tty.t_oproc = nmdmstart;
148 pt->part2.nm_tty.t_oproc = nmdmstart;
149 pt->part1.nm_tty.t_stop = nmdmstop;
150 pt->part2.nm_tty.t_dev = dev1;
151 pt->part1.nm_tty.t_dev = dev2;
152 pt->part2.nm_tty.t_stop = nmdmstop;
157 nmdmopen(dev_t dev, int flag, int devtype, struct thread *td)
159 struct proc *p = td->td_proc;
160 struct tty *tp, *tp2;
164 struct nm_softc *pti;
167 struct softpart *ourpart, *otherpart;
172 * XXX: Gross hack for DEVFS:
173 * If we openned this device, ensure we have the
174 * next one too, so people can open it.
181 nextdev = makedev(major(dev), (pair+pair) + 1);
182 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 && suser(td)) {
210 } else if (pti->pt_prison != p->p_ucred->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(dev_t dev, int flag, int mode, struct thread *td)
261 register 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, flag);
288 ourpart->modemsignals &= ~TIOCM_DTR;
289 nmdm_crossover(dev->si_drv1, ourpart, otherpart);
290 nmdmstop(tp, FREAD|FWRITE);
296 nmdmread(dev, uio, flag)
302 struct tty *tp, *tp2;
303 struct softpart *ourpart, *otherpart;
306 GETPARTS(tp, ourpart, otherpart);
307 tp2 = &otherpart->nm_tty;
310 if (tp2->t_state & TS_ISOPEN) {
311 error = (*linesw[tp->t_line].l_read)(tp, uio, flag);
312 wakeup_other(tp, FWRITE);
314 if (flag & IO_NDELAY) {
315 return (EWOULDBLOCK);
317 error = tsleep(TSA_PTC_READ(tp), PCATCH, "nmdout", 0);
321 if ((error = (*linesw[tp->t_line].l_read)(tp, uio, flag)) == 0)
322 wakeup_other(tp, FWRITE);
328 * Write to pseudo-tty.
329 * Wakeups of controlling tty will happen
330 * indirectly, when tty driver calls nmdmstart.
333 nmdmwrite(dev, uio, flag)
338 register u_char *cp = 0;
340 u_char locbuf[BUFSIZ];
343 struct tty *tp1, *tp;
344 struct softpart *ourpart, *otherpart;
348 * Get the other tty struct.
349 * basically we are writing into the INPUT side of the other device.
351 GETPARTS(tp1, ourpart, otherpart);
352 tp = &otherpart->nm_tty;
355 if ((tp->t_state & TS_ISOPEN) == 0)
357 while (uio->uio_resid > 0 || cc > 0) {
359 * Fill up the buffer if it's empty
362 cc = min(uio->uio_resid, BUFSIZ);
364 error = uiomove((caddr_t)cp, cc, uio);
367 /* check again for safety */
368 if ((tp->t_state & TS_ISOPEN) == 0) {
369 /* adjust for data copied in but not written */
370 uio->uio_resid += cc;
375 if (((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= (TTYHOG-2))
376 && ((tp->t_canq.c_cc > 0) || !(tp->t_iflag&ICANON))) {
378 * Come here to wait for space in outq,
379 * or space in rawq, or an empty canq.
381 wakeup(TSA_HUP_OR_INPUT(tp));
382 if ((tp->t_state & TS_CONNECTED) == 0) {
384 * Data piled up because not connected.
385 * Adjust for data copied in but
388 uio->uio_resid += cc;
391 if (flag & IO_NDELAY) {
393 * Don't wait if asked not to.
394 * Adjust for data copied in but
397 uio->uio_resid += cc;
399 return (EWOULDBLOCK);
402 error = tsleep(TSA_PTC_WRITE(tp),
403 PCATCH, "nmdout", 0);
406 * Tsleep returned (signal?).
407 * Go find out what the user wants.
408 * adjust for data copied in but
411 uio->uio_resid += cc;
416 (*linesw[tp->t_line].l_rint)(*cp++, tp);
426 * Start output on pseudo-tty.
427 * Wake up process selecting or sleeping for input from controlling tty.
433 register struct nm_softc *pti = tp->t_dev->si_drv1;
435 if (tp->t_state & TS_TTSTOP)
437 pti->pt_flags &= ~PF_STOPPED;
438 wakeup_other(tp, FREAD);
441 /* Wakes up the OTHER tty;*/
443 wakeup_other(tp, flag)
447 struct softpart *ourpart, *otherpart;
449 GETPARTS(tp, ourpart, otherpart);
451 selwakeup(&otherpart->nm_tty.t_rsel);
452 wakeup(TSA_PTC_READ((&otherpart->nm_tty)));
455 selwakeup(&otherpart->nm_tty.t_wsel);
456 wakeup(TSA_PTC_WRITE((&otherpart->nm_tty)));
462 register struct tty *tp;
465 struct nm_softc *pti = tp->t_dev->si_drv1;
468 /* note: FLUSHREAD and FLUSHWRITE already ok */
470 flush = TIOCPKT_STOP;
471 pti->pt_flags |= PF_STOPPED;
473 pti->pt_flags &= ~PF_STOPPED;
474 /* change of perspective */
480 wakeup_other(tp, flag);
485 nmdmioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td)
487 register struct tty *tp = dev->si_tty;
488 struct nm_softc *pti = dev->si_drv1;
490 register struct tty *tp2;
491 struct softpart *ourpart, *otherpart;
494 GETPARTS(tp, ourpart, otherpart);
495 tp2 = &otherpart->nm_tty;
497 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, td);
498 if (error == ENOIOCTL)
499 error = ttioctl(tp, cmd, data, flag);
500 if (error == ENOIOCTL) {
503 otherpart->gotbreak = 1;
508 ourpart->modemsignals |= TIOCM_DTR;
511 ourpart->modemsignals &= TIOCM_DTR;
514 ourpart->modemsignals = *(int *)data;
515 otherpart->modemsignals = *(int *)data;
518 ourpart->modemsignals |= *(int *)data;
521 ourpart->modemsignals &= ~(*(int *)data);
522 otherpart->modemsignals &= ~(*(int *)data);
525 *(int *)data = ourpart->modemsignals;
533 case TIOCDCDTIMESTAMP:
540 nmdm_crossover(pti, ourpart, otherpart);
547 nmdm_crossover(struct nm_softc *pti,
548 struct softpart *ourpart,
549 struct softpart *otherpart)
551 otherpart->modemsignals &= ~(TIOCM_CTS|TIOCM_CAR);
552 if (ourpart->modemsignals & TIOCM_RTS)
553 otherpart->modemsignals |= TIOCM_CTS;
554 if (ourpart->modemsignals & TIOCM_DTR)
555 otherpart->modemsignals |= TIOCM_CAR;
560 static void nmdm_drvinit __P((void *unused));
566 cdevsw_add(&nmdm_cdevsw);
567 /* XXX: Gross hack for DEVFS */
571 SYSINIT(nmdmdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,nmdm_drvinit,NULL)