xref: /dragonfly/sys/dev/misc/nmdm/nmdm.c (revision 279dd846)
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)
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;
318 #if 0
319 	struct tty *tp2;
320 	struct softpart *ourpart, *otherpart;
321 #endif
322 
323 	lwkt_gettoken(&tty_token);
324 	tp = dev->si_tty;
325 #if 0
326 	GETPARTS(tp, ourpart, otherpart);
327 	tp2 = &otherpart->nm_tty;
328 
329 	if (tp2->t_state & TS_ISOPEN) {
330 		error = (*linesw[tp->t_line].l_read)(tp, ap->a_uio, flag);
331 		wakeup_other(tp, FWRITE);
332 	} else {
333 		if (flag & IO_NDELAY) {
334 			lwkt_reltoken(&tty_token);
335 			return (EWOULDBLOCK);
336 		}
337 		error = tsleep(TSA_PTC_READ(tp), PCATCH, "nmdout", 0);
338 		}
339 	}
340 #else
341 	if ((error = (*linesw[tp->t_line].l_read)(tp, ap->a_uio, ap->a_ioflag)) == 0)
342 		wakeup_other(tp, FWRITE);
343 #endif
344 	lwkt_reltoken(&tty_token);
345 	return (error);
346 }
347 
348 /*
349  * Write to pseudo-tty.
350  * Wakeups of controlling tty will happen
351  * indirectly, when tty driver calls nmdmstart.
352  */
353 static	int
354 nmdmwrite(struct dev_write_args *ap)
355 {
356 	cdev_t dev = ap->a_head.a_dev;
357 	struct uio *uio = ap->a_uio;
358 	u_char *cp = NULL;
359 	size_t cc = 0;
360 	u_char locbuf[BUFSIZ];
361 	int cnt = 0;
362 	int error = 0;
363 	struct tty *tp1, *tp;
364 	struct softpart *ourpart, *otherpart;
365 
366 	lwkt_gettoken(&tty_token);
367 	tp1 = dev->si_tty;
368 	/*
369 	 * Get the other tty struct.
370 	 * basically we are writing into the INPUT side of the other device.
371 	 */
372 	GETPARTS(tp1, ourpart, otherpart);
373 	tp = &otherpart->nm_tty;
374 
375 again:
376 	if ((tp->t_state & TS_ISOPEN) == 0) {
377 		lwkt_reltoken(&tty_token);
378 		return (EIO);
379 	}
380 	while (uio->uio_resid > 0 || cc > 0) {
381 		/*
382 		 * Fill up the buffer if it's empty
383 		 */
384 		if (cc == 0) {
385 			cc = szmin(uio->uio_resid, BUFSIZ);
386 			cp = locbuf;
387 			error = uiomove((caddr_t)cp, cc, uio);
388 			if (error) {
389 				lwkt_reltoken(&tty_token);
390 				return (error);
391 			}
392 			/* check again for safety */
393 			if ((tp->t_state & TS_ISOPEN) == 0) {
394 				/* adjust for data copied in but not written */
395 				uio->uio_resid += cc;
396 				lwkt_reltoken(&tty_token);
397 				return (EIO);
398 			}
399 		}
400 		while (cc > 0) {
401 			if (((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= (TTYHOG-2))
402 			&& ((tp->t_canq.c_cc > 0) || !(tp->t_iflag&ICANON))) {
403 				/*
404 	 			 * Come here to wait for space in outq,
405 				 * or space in rawq, or an empty canq.
406 	 			 */
407 				wakeup(TSA_HUP_OR_INPUT(tp));
408 				if ((tp->t_state & TS_CONNECTED) == 0) {
409 					/*
410 					 * Data piled up because not connected.
411 					 * Adjust for data copied in but
412 					 * not written.
413 					 */
414 					uio->uio_resid += cc;
415 					lwkt_reltoken(&tty_token);
416 					return (EIO);
417 				}
418 				if (ap->a_ioflag & IO_NDELAY) {
419 					/*
420 				         * Don't wait if asked not to.
421 					 * Adjust for data copied in but
422 					 * not written.
423 					 */
424 					uio->uio_resid += cc;
425 					if (cnt == 0) {
426 						lwkt_reltoken(&tty_token);
427 						return (EWOULDBLOCK);
428 					}
429 					lwkt_reltoken(&tty_token);
430 					return (0);
431 				}
432 				error = tsleep(TSA_PTC_WRITE(tp),
433 						PCATCH, "nmdout", 0);
434 				if (error) {
435 					/*
436 					 * Tsleep returned (signal?).
437 					 * Go find out what the user wants.
438 					 * adjust for data copied in but
439 					 * not written
440 					 */
441 					uio->uio_resid += cc;
442 					lwkt_reltoken(&tty_token);
443 					return (error);
444 				}
445 				goto again;
446 			}
447 			(*linesw[tp->t_line].l_rint)(*cp++, tp);
448 			cnt++;
449 			cc--;
450 		}
451 		cc = 0;
452 	}
453 	lwkt_reltoken(&tty_token);
454 	return (0);
455 }
456 
457 /*
458  * Start output on pseudo-tty.
459  * Wake up process selecting or sleeping for input from controlling tty.
460  */
461 static void
462 nmdmstart(struct tty *tp)
463 {
464 	struct nm_softc *pti = tp->t_dev->si_drv1;
465 
466 	lwkt_gettoken(&tty_token);
467 	if (tp->t_state & TS_TTSTOP) {
468 		lwkt_reltoken(&tty_token);
469 		return;
470 	}
471 	pti->pt_flags &= ~PF_STOPPED;
472 	wakeup_other(tp, FREAD);
473 	lwkt_reltoken(&tty_token);
474 }
475 
476 /* Wakes up the OTHER tty;*/
477 static void
478 wakeup_other(struct tty *tp, int flag)
479 {
480 	struct softpart *ourpart, *otherpart;
481 
482 	lwkt_gettoken(&tty_token);
483 	GETPARTS(tp, ourpart, otherpart);
484 	if (flag & FREAD) {
485 		wakeup(TSA_PTC_READ((&otherpart->nm_tty)));
486 		KNOTE(&otherpart->nm_tty.t_rkq.ki_note, 0);
487 	}
488 	if (flag & FWRITE) {
489 		wakeup(TSA_PTC_WRITE((&otherpart->nm_tty)));
490 		KNOTE(&otherpart->nm_tty.t_wkq.ki_note, 0);
491 	}
492 	lwkt_reltoken(&tty_token);
493 }
494 
495 static	void
496 nmdmstop(struct tty *tp, int flush)
497 {
498 	struct nm_softc *pti = tp->t_dev->si_drv1;
499 	int flag;
500 
501 	lwkt_gettoken(&tty_token);
502 	/* note: FLUSHREAD and FLUSHWRITE already ok */
503 	if (flush == 0) {
504 		flush = TIOCPKT_STOP;
505 		pti->pt_flags |= PF_STOPPED;
506 	} else
507 		pti->pt_flags &= ~PF_STOPPED;
508 	/* change of perspective */
509 	flag = 0;
510 	if (flush & FREAD)
511 		flag |= FWRITE;
512 	if (flush & FWRITE)
513 		flag |= FREAD;
514 	wakeup_other(tp, flag);
515 	lwkt_reltoken(&tty_token);
516 }
517 
518 /*ARGSUSED*/
519 static	int
520 nmdmioctl(struct dev_ioctl_args *ap)
521 {
522 	cdev_t dev = ap->a_head.a_dev;
523 	struct tty *tp = dev->si_tty;
524 	struct nm_softc *pti = dev->si_drv1;
525 	int error;
526 	struct softpart *ourpart, *otherpart;
527 
528 	crit_enter();
529 	lwkt_gettoken(&tty_token);
530 	GETPARTS(tp, ourpart, otherpart);
531 
532 	error = (*linesw[tp->t_line].l_ioctl)(tp, ap->a_cmd, ap->a_data,
533 					      ap->a_fflag, ap->a_cred);
534 	if (error == ENOIOCTL)
535 		 error = ttioctl(tp, ap->a_cmd, ap->a_data, ap->a_fflag);
536 	if (error == ENOIOCTL) {
537 		switch (ap->a_cmd) {
538 		case TIOCSBRK:
539 			otherpart->gotbreak = 1;
540 			break;
541 		case TIOCCBRK:
542 			break;
543 		case TIOCSDTR:
544 			ourpart->modemsignals |= TIOCM_DTR;
545 			break;
546 		case TIOCCDTR:
547 			ourpart->modemsignals &= TIOCM_DTR;
548 			break;
549 		case TIOCMSET:
550 			ourpart->modemsignals = *(int *)ap->a_data;
551 			otherpart->modemsignals = *(int *)ap->a_data;
552 			break;
553 		case TIOCMBIS:
554 			ourpart->modemsignals |= *(int *)ap->a_data;
555 			break;
556 		case TIOCMBIC:
557 			ourpart->modemsignals &= ~(*(int *)ap->a_data);
558 			otherpart->modemsignals &= ~(*(int *)ap->a_data);
559 			break;
560 		case TIOCMGET:
561 			*(int *)ap->a_data = ourpart->modemsignals;
562 			break;
563 		case TIOCMSDTRWAIT:
564 			break;
565 		case TIOCMGDTRWAIT:
566 			*(int *)ap->a_data = 0;
567 			break;
568 		case TIOCTIMESTAMP:
569 		case TIOCDCDTIMESTAMP:
570 		default:
571 			lwkt_reltoken(&tty_token);
572 			crit_exit();
573 			error = ENOTTY;
574 			return (error);
575 		}
576 		error = 0;
577 		nmdm_crossover(pti, ourpart, otherpart);
578 	}
579 	lwkt_reltoken(&tty_token);
580 	crit_exit();
581 	return (error);
582 }
583 
584 static void
585 nmdm_crossover(struct nm_softc *pti,
586 		struct softpart *ourpart,
587 		struct softpart *otherpart)
588 {
589 	lwkt_gettoken(&tty_token);
590 	otherpart->modemsignals &= ~(TIOCM_CTS|TIOCM_CAR);
591 	if (ourpart->modemsignals & TIOCM_RTS)
592 		otherpart->modemsignals |= TIOCM_CTS;
593 	if (ourpart->modemsignals & TIOCM_DTR)
594 		otherpart->modemsignals |= TIOCM_CAR;
595 	lwkt_reltoken(&tty_token);
596 }
597 
598 
599 
600 static void nmdm_drvinit (void *unused);
601 
602 static void
603 nmdm_drvinit(void *unused)
604 {
605 	/* XXX: Gross hack for DEVFS */
606 	lwkt_gettoken(&tty_token);
607 	nmdminit(0);
608 	lwkt_reltoken(&tty_token);
609 }
610 
611 SYSINIT(nmdmdev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE + CDEV_MAJOR, nmdm_drvinit,
612     NULL);
613