xref: /dragonfly/sys/dev/misc/nmdm/nmdm.c (revision 88cfb1f7)
1 /*
2  * (MPSAFE)
3  *
4  * Copyright (c) 1982, 1986, 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  * $FreeBSD: src/sys/dev/nmdm/nmdm.c,v 1.5.2.1 2001/08/11 00:54:14 mp Exp $
36  */
37 
38 /*
39  * MPSAFE NOTE: This file acquires the tty_token mainly for linesw access and
40  *		tp (struct tty) access.
41  */
42 
43 /*
44  * Pseudo-nulmodem Driver
45  */
46 #include "opt_compat.h"
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
50 #include <sys/ioctl_compat.h>
51 #endif
52 #include <sys/proc.h>
53 #include <sys/priv.h>
54 #include <sys/thread2.h>
55 #include <sys/tty.h>
56 #include <sys/conf.h>
57 #include <sys/fcntl.h>
58 #include <sys/kernel.h>
59 #include <sys/vnode.h>
60 #include <sys/signalvar.h>
61 #include <sys/malloc.h>
62 
63 MALLOC_DEFINE(M_NLMDM, "nullmodem", "nullmodem data structures");
64 
65 static void nmdmstart (struct tty *tp);
66 static void nmdmstop (struct tty *tp, int rw);
67 static void wakeup_other (struct tty *tp, int flag);
68 static void nmdminit (int n);
69 
70 static	d_open_t	nmdmopen;
71 static	d_close_t	nmdmclose;
72 static	d_read_t	nmdmread;
73 static	d_write_t	nmdmwrite;
74 static	d_ioctl_t	nmdmioctl;
75 
76 #define	CDEV_MAJOR	18
77 static struct dev_ops nmdm_ops = {
78 	{ "pts", 0, D_TTY },
79 	.d_open =	nmdmopen,
80 	.d_close =	nmdmclose,
81 	.d_read =	nmdmread,
82 	.d_write =	nmdmwrite,
83 	.d_ioctl =	nmdmioctl,
84 	.d_kqfilter = 	ttykqfilter,
85 	.d_revoke =	ttyrevoke
86 };
87 
88 #define BUFSIZ 100		/* Chunk size iomoved to/from user */
89 
90 struct softpart {
91 	struct tty nm_tty;
92 	cdev_t	dev;
93 	int	modemsignals;	/* bits defined in sys/ttycom.h */
94 	int	gotbreak;
95 };
96 
97 struct	nm_softc {
98 	int	pt_flags;
99 	struct softpart part1, part2;
100 	struct	prison *pt_prison;
101 };
102 
103 #define	PF_STOPPED	0x10		/* user told stopped */
104 
105 static void
106 nmdm_crossover(struct nm_softc *pti,
107 		struct softpart *ourpart,
108 		struct softpart *otherpart);
109 
110 #define GETPARTS(tp, ourpart, otherpart) \
111 do {	\
112 	struct nm_softc *pti = tp->t_dev->si_drv1; \
113 	if (tp == &pti->part1.nm_tty) { \
114 		ourpart = &pti->part1; \
115 		otherpart = &pti->part2; \
116 	} else { \
117 		ourpart = &pti->part2; \
118 		otherpart = &pti->part1; \
119 	}  \
120 } while (0)
121 
122 /*
123  * This function creates and initializes a pair of ttys.
124  *
125  * NOTE: Must be called with tty_token held
126  */
127 static void
128 nmdminit(int n)
129 {
130 	cdev_t dev1, dev2;
131 	struct nm_softc *pt;
132 
133 	/*
134 	 * Simplified unit number, use low 8 bits of minor number
135 	 * (remember, the minor number mask is 0xffff00ff).
136 	 */
137 	if (n & ~0x7f)
138 		return;
139 
140 	ASSERT_LWKT_TOKEN_HELD(&tty_token);
141 
142 	pt = kmalloc(sizeof(*pt), M_NLMDM, M_WAITOK | M_ZERO);
143 	pt->part1.dev = dev1 = make_dev(&nmdm_ops, n << 1,
144 					0, 0, 0666, "nmdm%dA", n);
145 	pt->part2.dev = dev2 = make_dev(&nmdm_ops, (n << 1) + 1,
146 					0, 0, 0666, "nmdm%dB", n);
147 
148 	dev1->si_drv1 = dev2->si_drv1 = pt;
149 	dev1->si_tty = &pt->part1.nm_tty;
150 	dev2->si_tty = &pt->part2.nm_tty;
151 	ttyregister(&pt->part1.nm_tty);
152 	ttyregister(&pt->part2.nm_tty);
153 	pt->part1.nm_tty.t_oproc = nmdmstart;
154 	pt->part2.nm_tty.t_oproc = nmdmstart;
155 	pt->part1.nm_tty.t_stop = nmdmstop;
156 	pt->part2.nm_tty.t_dev = dev1;
157 	pt->part1.nm_tty.t_dev = dev2;
158 	pt->part2.nm_tty.t_stop = nmdmstop;
159 }
160 
161 /*ARGSUSED*/
162 static	int
163 nmdmopen(struct dev_open_args *ap)
164 {
165 	cdev_t dev = ap->a_head.a_dev;
166 	struct tty *tp, *tp2;
167 	int error;
168 	int minr;
169 #if 0
170 	cdev_t nextdev;
171 #endif
172 	struct nm_softc *pti;
173 	int is_b;
174 	int	pair;
175 	struct	softpart *ourpart, *otherpart;
176 
177 	minr = lminor(dev);
178 	pair = minr >> 1;
179 	is_b = minr & 1;
180 
181 #if 0
182 	/*
183 	 * XXX: Gross hack for DEVFS:
184 	 * If we openned this device, ensure we have the
185 	 * next one too, so people can open it.
186 	 */
187 	if (pair < 127) {
188 		nextdev = makedev(major(dev), (pair+pair) + 1);
189 		if (!nextdev->si_drv1) {
190 			nmdminit(pair + 1);
191 		}
192 	}
193 #endif
194 	if (!dev->si_drv1)
195 		nmdminit(pair);
196 
197 	if (!dev->si_drv1)
198 		return(ENXIO);
199 
200 	lwkt_gettoken(&tty_token);
201 	pti = dev->si_drv1;
202 	if (is_b)
203 		tp = &pti->part2.nm_tty;
204 	else
205 		tp = &pti->part1.nm_tty;
206 	GETPARTS(tp, ourpart, otherpart);
207 	tp2 = &otherpart->nm_tty;
208 	ourpart->modemsignals |= TIOCM_LE;
209 
210 	if ((tp->t_state & TS_ISOPEN) == 0) {
211 		ttychars(tp);		/* Set up default chars */
212 		tp->t_iflag = TTYDEF_IFLAG;
213 		tp->t_oflag = TTYDEF_OFLAG;
214 		tp->t_lflag = TTYDEF_LFLAG;
215 		tp->t_cflag = TTYDEF_CFLAG;
216 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
217 	} else if (tp->t_state & TS_XCLUDE && priv_check_cred(ap->a_cred, PRIV_ROOT, 0)) {
218 		lwkt_reltoken(&tty_token);
219 		return (EBUSY);
220 	} else if (pti->pt_prison != ap->a_cred->cr_prison) {
221 		lwkt_reltoken(&tty_token);
222 		return (EBUSY);
223 	}
224 
225 	/*
226 	 * If the other side is open we have carrier
227 	 */
228 	if (tp2->t_state & TS_ISOPEN) {
229 		(void)(*linesw[tp->t_line].l_modem)(tp, 1);
230 	}
231 
232 	/*
233 	 * And the other side gets carrier as we are now open.
234 	 */
235 	(void)(*linesw[tp2->t_line].l_modem)(tp2, 1);
236 
237 	/* External processing makes no sense here */
238 	tp->t_lflag &= ~EXTPROC;
239 
240 	/*
241 	 * Wait here if we don't have carrier.
242 	 */
243 #if 0
244 	while ((tp->t_state & TS_CARR_ON) == 0) {
245 		if (flag & FNONBLOCK)
246 			break;
247 		error = ttysleep(tp, TSA_CARR_ON(tp), PCATCH, "nmdopn", 0);
248 		if (error) {
249 			lwkt_reltoken(&tty_token);
250 			return (error);
251 		}
252 	}
253 #endif
254 
255 	/*
256 	 * Give the line disciplin a chance to set this end up.
257 	 */
258 	error = (*linesw[tp->t_line].l_open)(dev, tp);
259 
260 	/*
261 	 * Wake up the other side.
262 	 * Theoretically not needed.
263 	 */
264 	ourpart->modemsignals |= TIOCM_DTR;
265 	nmdm_crossover(pti, ourpart, otherpart);
266 	if (error == 0)
267 		wakeup_other(tp, FREAD|FWRITE); /* XXX */
268 	lwkt_reltoken(&tty_token);
269 	return (error);
270 }
271 
272 static int
273 nmdmclose(struct dev_close_args *ap)
274 {
275 	cdev_t dev = ap->a_head.a_dev;
276 	struct tty *tp, *tp2;
277 	int err;
278 	struct softpart *ourpart, *otherpart;
279 
280 	lwkt_gettoken(&tty_token);
281 	/*
282 	 * let the other end know that the game is up
283 	 */
284 	tp = dev->si_tty;
285 	GETPARTS(tp, ourpart, otherpart);
286 	tp2 = &otherpart->nm_tty;
287 	(void)(*linesw[tp2->t_line].l_modem)(tp2, 0);
288 
289 	/*
290 	 * XXX MDMBUF makes no sense for nmdms but would inhibit the above
291 	 * l_modem().  CLOCAL makes sense but isn't supported.   Special
292 	 * l_modem()s that ignore carrier drop make no sense for nmdms but
293 	 * may be in use because other parts of the line discipline make
294 	 * sense for nmdms.  Recover by doing everything that a normal
295 	 * ttymodem() would have done except for sending a SIGHUP.
296 	 */
297 	if (tp2->t_state & TS_ISOPEN) {
298 		tp2->t_state &= ~(TS_CARR_ON | TS_CONNECTED);
299 		tp2->t_state |= TS_ZOMBIE;
300 		ttyflush(tp2, FREAD | FWRITE);
301 	}
302 
303 	err = (*linesw[tp->t_line].l_close)(tp, ap->a_fflag);
304 	ourpart->modemsignals &= ~TIOCM_DTR;
305 	nmdm_crossover(dev->si_drv1, ourpart, otherpart);
306 	nmdmstop(tp, FREAD|FWRITE);
307 	(void) ttyclose(tp);
308 	lwkt_reltoken(&tty_token);
309 	return (err);
310 }
311 
312 static int
313 nmdmread(struct dev_read_args *ap)
314 {
315 	cdev_t dev = ap->a_head.a_dev;
316 	int error = 0;
317 	struct tty *tp, *tp2;
318 	struct softpart *ourpart, *otherpart;
319 
320 	lwkt_gettoken(&tty_token);
321 	tp = dev->si_tty;
322 	GETPARTS(tp, ourpart, otherpart);
323 	tp2 = &otherpart->nm_tty;
324 
325 #if 0
326 	if (tp2->t_state & TS_ISOPEN) {
327 		error = (*linesw[tp->t_line].l_read)(tp, ap->a_uio, flag);
328 		wakeup_other(tp, FWRITE);
329 	} else {
330 		if (flag & IO_NDELAY) {
331 			lwkt_reltoken(&tty_token);
332 			return (EWOULDBLOCK);
333 		}
334 		error = tsleep(TSA_PTC_READ(tp), PCATCH, "nmdout", 0);
335 		}
336 	}
337 #else
338 	if ((error = (*linesw[tp->t_line].l_read)(tp, ap->a_uio, ap->a_ioflag)) == 0)
339 		wakeup_other(tp, FWRITE);
340 #endif
341 	lwkt_reltoken(&tty_token);
342 	return (error);
343 }
344 
345 /*
346  * Write to pseudo-tty.
347  * Wakeups of controlling tty will happen
348  * indirectly, when tty driver calls nmdmstart.
349  */
350 static	int
351 nmdmwrite(struct dev_write_args *ap)
352 {
353 	cdev_t dev = ap->a_head.a_dev;
354 	struct uio *uio = ap->a_uio;
355 	u_char *cp = 0;
356 	size_t cc = 0;
357 	u_char locbuf[BUFSIZ];
358 	int cnt = 0;
359 	int error = 0;
360 	struct tty *tp1, *tp;
361 	struct softpart *ourpart, *otherpart;
362 
363 	lwkt_gettoken(&tty_token);
364 	tp1 = dev->si_tty;
365 	/*
366 	 * Get the other tty struct.
367 	 * basically we are writing into the INPUT side of the other device.
368 	 */
369 	GETPARTS(tp1, ourpart, otherpart);
370 	tp = &otherpart->nm_tty;
371 
372 again:
373 	if ((tp->t_state & TS_ISOPEN) == 0) {
374 		lwkt_reltoken(&tty_token);
375 		return (EIO);
376 	}
377 	while (uio->uio_resid > 0 || cc > 0) {
378 		/*
379 		 * Fill up the buffer if it's empty
380 		 */
381 		if (cc == 0) {
382 			cc = szmin(uio->uio_resid, BUFSIZ);
383 			cp = locbuf;
384 			error = uiomove((caddr_t)cp, cc, uio);
385 			if (error) {
386 				lwkt_reltoken(&tty_token);
387 				return (error);
388 			}
389 			/* check again for safety */
390 			if ((tp->t_state & TS_ISOPEN) == 0) {
391 				/* adjust for data copied in but not written */
392 				uio->uio_resid += cc;
393 				lwkt_reltoken(&tty_token);
394 				return (EIO);
395 			}
396 		}
397 		while (cc > 0) {
398 			if (((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= (TTYHOG-2))
399 			&& ((tp->t_canq.c_cc > 0) || !(tp->t_iflag&ICANON))) {
400 				/*
401 	 			 * Come here to wait for space in outq,
402 				 * or space in rawq, or an empty canq.
403 	 			 */
404 				wakeup(TSA_HUP_OR_INPUT(tp));
405 				if ((tp->t_state & TS_CONNECTED) == 0) {
406 					/*
407 					 * Data piled up because not connected.
408 					 * Adjust for data copied in but
409 					 * not written.
410 					 */
411 					uio->uio_resid += cc;
412 					lwkt_reltoken(&tty_token);
413 					return (EIO);
414 				}
415 				if (ap->a_ioflag & IO_NDELAY) {
416 					/*
417 				         * Don't wait if asked not to.
418 					 * Adjust for data copied in but
419 					 * not written.
420 					 */
421 					uio->uio_resid += cc;
422 					if (cnt == 0) {
423 						lwkt_reltoken(&tty_token);
424 						return (EWOULDBLOCK);
425 					}
426 					lwkt_reltoken(&tty_token);
427 					return (0);
428 				}
429 				error = tsleep(TSA_PTC_WRITE(tp),
430 						PCATCH, "nmdout", 0);
431 				if (error) {
432 					/*
433 					 * Tsleep returned (signal?).
434 					 * Go find out what the user wants.
435 					 * adjust for data copied in but
436 					 * not written
437 					 */
438 					uio->uio_resid += cc;
439 					lwkt_reltoken(&tty_token);
440 					return (error);
441 				}
442 				goto again;
443 			}
444 			(*linesw[tp->t_line].l_rint)(*cp++, tp);
445 			cnt++;
446 			cc--;
447 		}
448 		cc = 0;
449 	}
450 	lwkt_reltoken(&tty_token);
451 	return (0);
452 }
453 
454 /*
455  * Start output on pseudo-tty.
456  * Wake up process selecting or sleeping for input from controlling tty.
457  */
458 static void
459 nmdmstart(struct tty *tp)
460 {
461 	struct nm_softc *pti = tp->t_dev->si_drv1;
462 
463 	lwkt_gettoken(&tty_token);
464 	if (tp->t_state & TS_TTSTOP) {
465 		lwkt_reltoken(&tty_token);
466 		return;
467 	}
468 	pti->pt_flags &= ~PF_STOPPED;
469 	wakeup_other(tp, FREAD);
470 	lwkt_reltoken(&tty_token);
471 }
472 
473 /* Wakes up the OTHER tty;*/
474 static void
475 wakeup_other(struct tty *tp, int flag)
476 {
477 	struct softpart *ourpart, *otherpart;
478 
479 	lwkt_gettoken(&tty_token);
480 	GETPARTS(tp, ourpart, otherpart);
481 	if (flag & FREAD) {
482 		wakeup(TSA_PTC_READ((&otherpart->nm_tty)));
483 		KNOTE(&otherpart->nm_tty.t_rkq.ki_note, 0);
484 	}
485 	if (flag & FWRITE) {
486 		wakeup(TSA_PTC_WRITE((&otherpart->nm_tty)));
487 		KNOTE(&otherpart->nm_tty.t_wkq.ki_note, 0);
488 	}
489 	lwkt_reltoken(&tty_token);
490 }
491 
492 static	void
493 nmdmstop(struct tty *tp, int flush)
494 {
495 	struct nm_softc *pti = tp->t_dev->si_drv1;
496 	int flag;
497 
498 	lwkt_gettoken(&tty_token);
499 	/* note: FLUSHREAD and FLUSHWRITE already ok */
500 	if (flush == 0) {
501 		flush = TIOCPKT_STOP;
502 		pti->pt_flags |= PF_STOPPED;
503 	} else
504 		pti->pt_flags &= ~PF_STOPPED;
505 	/* change of perspective */
506 	flag = 0;
507 	if (flush & FREAD)
508 		flag |= FWRITE;
509 	if (flush & FWRITE)
510 		flag |= FREAD;
511 	wakeup_other(tp, flag);
512 	lwkt_reltoken(&tty_token);
513 }
514 
515 /*ARGSUSED*/
516 static	int
517 nmdmioctl(struct dev_ioctl_args *ap)
518 {
519 	cdev_t dev = ap->a_head.a_dev;
520 	struct tty *tp = dev->si_tty;
521 	struct nm_softc *pti = dev->si_drv1;
522 	int error;
523 	struct tty *tp2;
524 	struct softpart *ourpart, *otherpart;
525 
526 	crit_enter();
527 	lwkt_gettoken(&tty_token);
528 	GETPARTS(tp, ourpart, otherpart);
529 	tp2 = &otherpart->nm_tty;
530 
531 	error = (*linesw[tp->t_line].l_ioctl)(tp, ap->a_cmd, ap->a_data,
532 					      ap->a_fflag, ap->a_cred);
533 	if (error == ENOIOCTL)
534 		 error = ttioctl(tp, ap->a_cmd, ap->a_data, ap->a_fflag);
535 	if (error == ENOIOCTL) {
536 		switch (ap->a_cmd) {
537 		case TIOCSBRK:
538 			otherpart->gotbreak = 1;
539 			break;
540 		case TIOCCBRK:
541 			break;
542 		case TIOCSDTR:
543 			ourpart->modemsignals |= TIOCM_DTR;
544 			break;
545 		case TIOCCDTR:
546 			ourpart->modemsignals &= TIOCM_DTR;
547 			break;
548 		case TIOCMSET:
549 			ourpart->modemsignals = *(int *)ap->a_data;
550 			otherpart->modemsignals = *(int *)ap->a_data;
551 			break;
552 		case TIOCMBIS:
553 			ourpart->modemsignals |= *(int *)ap->a_data;
554 			break;
555 		case TIOCMBIC:
556 			ourpart->modemsignals &= ~(*(int *)ap->a_data);
557 			otherpart->modemsignals &= ~(*(int *)ap->a_data);
558 			break;
559 		case TIOCMGET:
560 			*(int *)ap->a_data = ourpart->modemsignals;
561 			break;
562 		case TIOCMSDTRWAIT:
563 			break;
564 		case TIOCMGDTRWAIT:
565 			*(int *)ap->a_data = 0;
566 			break;
567 		case TIOCTIMESTAMP:
568 		case TIOCDCDTIMESTAMP:
569 		default:
570 			lwkt_reltoken(&tty_token);
571 			crit_exit();
572 			error = ENOTTY;
573 			return (error);
574 		}
575 		error = 0;
576 		nmdm_crossover(pti, ourpart, otherpart);
577 	}
578 	lwkt_reltoken(&tty_token);
579 	crit_exit();
580 	return (error);
581 }
582 
583 static void
584 nmdm_crossover(struct nm_softc *pti,
585 		struct softpart *ourpart,
586 		struct softpart *otherpart)
587 {
588 	lwkt_gettoken(&tty_token);
589 	otherpart->modemsignals &= ~(TIOCM_CTS|TIOCM_CAR);
590 	if (ourpart->modemsignals & TIOCM_RTS)
591 		otherpart->modemsignals |= TIOCM_CTS;
592 	if (ourpart->modemsignals & TIOCM_DTR)
593 		otherpart->modemsignals |= TIOCM_CAR;
594 	lwkt_reltoken(&tty_token);
595 }
596 
597 
598 
599 static void nmdm_drvinit (void *unused);
600 
601 static void
602 nmdm_drvinit(void *unused)
603 {
604 	/* XXX: Gross hack for DEVFS */
605 	lwkt_gettoken(&tty_token);
606 	nmdminit(0);
607 	lwkt_reltoken(&tty_token);
608 }
609 
610 SYSINIT(nmdmdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,nmdm_drvinit,NULL)
611