Style(9) cleanup to src/sys/vfs, stage 20/21: umapfs.
[dragonfly.git] / sys / dev / misc / nmdm / nmdm.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1982, 1986, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
20 *
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
31 * SUCH DAMAGE.
32 *
33 * $FreeBSD: src/sys/dev/nmdm/nmdm.c,v 1.5.2.1 2001/08/11 00:54:14 mp Exp $
455fcd7e 34 * $DragonFly: src/sys/dev/misc/nmdm/nmdm.c,v 1.9 2004/05/13 23:49:16 dillon Exp $
984263bc
MD
35 */
36
37/*
38 * Pseudo-nulmodem Driver
39 */
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>
45#endif
46#include <sys/proc.h>
47#include <sys/tty.h>
48#include <sys/conf.h>
49#include <sys/fcntl.h>
50#include <sys/poll.h>
51#include <sys/kernel.h>
52#include <sys/vnode.h>
53#include <sys/signalvar.h>
54#include <sys/malloc.h>
55
56MALLOC_DEFINE(M_NLMDM, "nullmodem", "nullmodem data structures");
57
38e94a25
RG
58static void nmdmstart (struct tty *tp);
59static void nmdmstop (struct tty *tp, int rw);
60static void wakeup_other (struct tty *tp, int flag);
61static void nmdminit (int n);
984263bc
MD
62
63static d_open_t nmdmopen;
64static d_close_t nmdmclose;
65static d_read_t nmdmread;
66static d_write_t nmdmwrite;
67static d_ioctl_t nmdmioctl;
68
69#define CDEV_MAJOR 18
70static struct cdevsw nmdm_cdevsw = {
fabb8ceb
MD
71 /* name */ "pts",
72 /* maj */ CDEV_MAJOR,
73 /* flags */ D_TTY,
74 /* port */ NULL,
455fcd7e 75 /* clone */ NULL,
fabb8ceb 76
984263bc
MD
77 /* open */ nmdmopen,
78 /* close */ nmdmclose,
79 /* read */ nmdmread,
80 /* write */ nmdmwrite,
81 /* ioctl */ nmdmioctl,
82 /* poll */ ttypoll,
83 /* mmap */ nommap,
84 /* strategy */ nostrategy,
984263bc 85 /* dump */ nodump,
fabb8ceb 86 /* psize */ nopsize
984263bc
MD
87};
88
89#define BUFSIZ 100 /* Chunk size iomoved to/from user */
90
91struct softpart {
92 struct tty nm_tty;
93 dev_t dev;
94 int modemsignals; /* bits defined in sys/ttycom.h */
95 int gotbreak;
96};
97
98struct nm_softc {
99 int pt_flags;
100 struct softpart part1, part2;
101 struct prison *pt_prison;
102};
103
104#define PF_STOPPED 0x10 /* user told stopped */
105
106static void
107nmdm_crossover(struct nm_softc *pti,
108 struct softpart *ourpart,
109 struct softpart *otherpart);
110
111#define GETPARTS(tp, ourpart, otherpart) \
112do { \
113 struct nm_softc *pti = tp->t_dev->si_drv1; \
114 if (tp == &pti->part1.nm_tty) { \
115 ourpart = &pti->part1; \
116 otherpart = &pti->part2; \
117 } else { \
118 ourpart = &pti->part2; \
119 otherpart = &pti->part1; \
120 } \
121} while (0)
122
123/*
124 * This function creates and initializes a pair of ttys.
125 */
126static void
127nmdminit(n)
128 int n;
129{
130 dev_t dev1, dev2;
131 struct nm_softc *pt;
132
133 /* For now we only map the lower 8 bits of the minor */
134 if (n & ~0xff)
135 return;
136
137 pt = malloc(sizeof(*pt), M_NLMDM, M_WAITOK);
138 bzero(pt, sizeof(*pt));
139 pt->part1.dev = dev1 = make_dev(&nmdm_cdevsw, n+n,
140 0, 0, 0666, "nmdm%dA", n);
141 pt->part2.dev = dev2 = make_dev(&nmdm_cdevsw, n+n+1,
142 0, 0, 0666, "nmdm%dB", n);
143
144 dev1->si_drv1 = dev2->si_drv1 = pt;
145 dev1->si_tty = &pt->part1.nm_tty;
146 dev2->si_tty = &pt->part2.nm_tty;
147 ttyregister(&pt->part1.nm_tty);
148 ttyregister(&pt->part2.nm_tty);
149 pt->part1.nm_tty.t_oproc = nmdmstart;
150 pt->part2.nm_tty.t_oproc = nmdmstart;
151 pt->part1.nm_tty.t_stop = nmdmstop;
152 pt->part2.nm_tty.t_dev = dev1;
153 pt->part1.nm_tty.t_dev = dev2;
154 pt->part2.nm_tty.t_stop = nmdmstop;
155}
156
157/*ARGSUSED*/
158static int
41c20dac 159nmdmopen(dev_t dev, int flag, int devtype, struct thread *td)
984263bc 160{
41c20dac
MD
161 struct proc *p = td->td_proc;
162 struct tty *tp, *tp2;
984263bc
MD
163 int error;
164 int minr;
165 dev_t nextdev;
166 struct nm_softc *pti;
167 int is_b;
168 int pair;
169 struct softpart *ourpart, *otherpart;
170
41c20dac
MD
171 KKASSERT(p != NULL);
172
984263bc
MD
173 /*
174 * XXX: Gross hack for DEVFS:
175 * If we openned this device, ensure we have the
176 * next one too, so people can open it.
177 */
178 minr = lminor(dev);
179 pair = minr >> 1;
180 is_b = minr & 1;
181
182 if (pair < 127) {
183 nextdev = makedev(major(dev), (pair+pair) + 1);
184 if (!nextdev->si_drv1) {
185 nmdminit(pair + 1);
186 }
187 }
188 if (!dev->si_drv1)
189 nmdminit(pair);
190
191 if (!dev->si_drv1)
192 return(ENXIO);
193
194 pti = dev->si_drv1;
195 if (is_b)
196 tp = &pti->part2.nm_tty;
197 else
198 tp = &pti->part1.nm_tty;
199 GETPARTS(tp, ourpart, otherpart);
200 tp2 = &otherpart->nm_tty;
201 ourpart->modemsignals |= TIOCM_LE;
202
203 if ((tp->t_state & TS_ISOPEN) == 0) {
204 ttychars(tp); /* Set up default chars */
205 tp->t_iflag = TTYDEF_IFLAG;
206 tp->t_oflag = TTYDEF_OFLAG;
207 tp->t_lflag = TTYDEF_LFLAG;
208 tp->t_cflag = TTYDEF_CFLAG;
209 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
dadab5e9 210 } else if (tp->t_state & TS_XCLUDE && suser(td)) {
984263bc 211 return (EBUSY);
41c20dac 212 } else if (pti->pt_prison != p->p_ucred->cr_prison) {
984263bc
MD
213 return (EBUSY);
214 }
215
216 /*
217 * If the other side is open we have carrier
218 */
219 if (tp2->t_state & TS_ISOPEN) {
220 (void)(*linesw[tp->t_line].l_modem)(tp, 1);
221 }
222
223 /*
224 * And the other side gets carrier as we are now open.
225 */
226 (void)(*linesw[tp2->t_line].l_modem)(tp2, 1);
227
228 /* External processing makes no sense here */
229 tp->t_lflag &= ~EXTPROC;
230
231 /*
232 * Wait here if we don't have carrier.
233 */
234#if 0
235 while ((tp->t_state & TS_CARR_ON) == 0) {
236 if (flag & FNONBLOCK)
237 break;
377d4740 238 error = ttysleep(tp, TSA_CARR_ON(tp), PCATCH, "nmdopn", 0);
984263bc
MD
239 if (error)
240 return (error);
241 }
242#endif
243
244 /*
245 * Give the line disciplin a chance to set this end up.
246 */
247 error = (*linesw[tp->t_line].l_open)(dev, tp);
248
249 /*
250 * Wake up the other side.
251 * Theoretically not needed.
252 */
253 ourpart->modemsignals |= TIOCM_DTR;
254 nmdm_crossover(pti, ourpart, otherpart);
255 if (error == 0)
256 wakeup_other(tp, FREAD|FWRITE); /* XXX */
257 return (error);
258}
259
260static int
41c20dac 261nmdmclose(dev_t dev, int flag, int mode, struct thread *td)
984263bc 262{
f96d6c88 263 struct tty *tp, *tp2;
984263bc
MD
264 int err;
265 struct softpart *ourpart, *otherpart;
266
267 /*
268 * let the other end know that the game is up
269 */
270 tp = dev->si_tty;
271 GETPARTS(tp, ourpart, otherpart);
272 tp2 = &otherpart->nm_tty;
273 (void)(*linesw[tp2->t_line].l_modem)(tp2, 0);
274
275 /*
276 * XXX MDMBUF makes no sense for nmdms but would inhibit the above
277 * l_modem(). CLOCAL makes sense but isn't supported. Special
278 * l_modem()s that ignore carrier drop make no sense for nmdms but
279 * may be in use because other parts of the line discipline make
280 * sense for nmdms. Recover by doing everything that a normal
281 * ttymodem() would have done except for sending a SIGHUP.
282 */
283 if (tp2->t_state & TS_ISOPEN) {
284 tp2->t_state &= ~(TS_CARR_ON | TS_CONNECTED);
285 tp2->t_state |= TS_ZOMBIE;
286 ttyflush(tp2, FREAD | FWRITE);
287 }
288
289 err = (*linesw[tp->t_line].l_close)(tp, flag);
290 ourpart->modemsignals &= ~TIOCM_DTR;
291 nmdm_crossover(dev->si_drv1, ourpart, otherpart);
292 nmdmstop(tp, FREAD|FWRITE);
293 (void) ttyclose(tp);
294 return (err);
295}
296
297static int
298nmdmread(dev, uio, flag)
299 dev_t dev;
300 struct uio *uio;
301 int flag;
302{
303 int error = 0;
304 struct tty *tp, *tp2;
305 struct softpart *ourpart, *otherpart;
306
307 tp = dev->si_tty;
308 GETPARTS(tp, ourpart, otherpart);
309 tp2 = &otherpart->nm_tty;
310
311#if 0
312 if (tp2->t_state & TS_ISOPEN) {
313 error = (*linesw[tp->t_line].l_read)(tp, uio, flag);
314 wakeup_other(tp, FWRITE);
315 } else {
316 if (flag & IO_NDELAY) {
317 return (EWOULDBLOCK);
318 }
377d4740 319 error = tsleep(TSA_PTC_READ(tp), PCATCH, "nmdout", 0);
984263bc
MD
320 }
321 }
322#else
323 if ((error = (*linesw[tp->t_line].l_read)(tp, uio, flag)) == 0)
324 wakeup_other(tp, FWRITE);
325#endif
326 return (error);
327}
328
329/*
330 * Write to pseudo-tty.
331 * Wakeups of controlling tty will happen
332 * indirectly, when tty driver calls nmdmstart.
333 */
334static int
335nmdmwrite(dev, uio, flag)
336 dev_t dev;
337 struct uio *uio;
338 int flag;
339{
f96d6c88
RG
340 u_char *cp = 0;
341 int cc = 0;
984263bc
MD
342 u_char locbuf[BUFSIZ];
343 int cnt = 0;
344 int error = 0;
345 struct tty *tp1, *tp;
346 struct softpart *ourpart, *otherpart;
347
348 tp1 = dev->si_tty;
349 /*
350 * Get the other tty struct.
351 * basically we are writing into the INPUT side of the other device.
352 */
353 GETPARTS(tp1, ourpart, otherpart);
354 tp = &otherpart->nm_tty;
355
356again:
357 if ((tp->t_state & TS_ISOPEN) == 0)
358 return (EIO);
359 while (uio->uio_resid > 0 || cc > 0) {
360 /*
361 * Fill up the buffer if it's empty
362 */
363 if (cc == 0) {
364 cc = min(uio->uio_resid, BUFSIZ);
365 cp = locbuf;
366 error = uiomove((caddr_t)cp, cc, uio);
367 if (error)
368 return (error);
369 /* check again for safety */
370 if ((tp->t_state & TS_ISOPEN) == 0) {
371 /* adjust for data copied in but not written */
372 uio->uio_resid += cc;
373 return (EIO);
374 }
375 }
376 while (cc > 0) {
377 if (((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= (TTYHOG-2))
378 && ((tp->t_canq.c_cc > 0) || !(tp->t_iflag&ICANON))) {
379 /*
380 * Come here to wait for space in outq,
381 * or space in rawq, or an empty canq.
382 */
383 wakeup(TSA_HUP_OR_INPUT(tp));
384 if ((tp->t_state & TS_CONNECTED) == 0) {
385 /*
386 * Data piled up because not connected.
387 * Adjust for data copied in but
388 * not written.
389 */
390 uio->uio_resid += cc;
391 return (EIO);
392 }
393 if (flag & IO_NDELAY) {
394 /*
395 * Don't wait if asked not to.
396 * Adjust for data copied in but
397 * not written.
398 */
399 uio->uio_resid += cc;
400 if (cnt == 0)
401 return (EWOULDBLOCK);
402 return (0);
403 }
404 error = tsleep(TSA_PTC_WRITE(tp),
377d4740 405 PCATCH, "nmdout", 0);
984263bc
MD
406 if (error) {
407 /*
408 * Tsleep returned (signal?).
409 * Go find out what the user wants.
410 * adjust for data copied in but
411 * not written
412 */
413 uio->uio_resid += cc;
414 return (error);
415 }
416 goto again;
417 }
418 (*linesw[tp->t_line].l_rint)(*cp++, tp);
419 cnt++;
420 cc--;
421 }
422 cc = 0;
423 }
424 return (0);
425}
426
427/*
428 * Start output on pseudo-tty.
429 * Wake up process selecting or sleeping for input from controlling tty.
430 */
431static void
432nmdmstart(tp)
433 struct tty *tp;
434{
f96d6c88 435 struct nm_softc *pti = tp->t_dev->si_drv1;
984263bc
MD
436
437 if (tp->t_state & TS_TTSTOP)
438 return;
439 pti->pt_flags &= ~PF_STOPPED;
440 wakeup_other(tp, FREAD);
441}
442
443/* Wakes up the OTHER tty;*/
444static void
445wakeup_other(tp, flag)
446 struct tty *tp;
447 int flag;
448{
449 struct softpart *ourpart, *otherpart;
450
451 GETPARTS(tp, ourpart, otherpart);
452 if (flag & FREAD) {
453 selwakeup(&otherpart->nm_tty.t_rsel);
454 wakeup(TSA_PTC_READ((&otherpart->nm_tty)));
455 }
456 if (flag & FWRITE) {
457 selwakeup(&otherpart->nm_tty.t_wsel);
458 wakeup(TSA_PTC_WRITE((&otherpart->nm_tty)));
459 }
460}
461
462static void
463nmdmstop(tp, flush)
f96d6c88 464 struct tty *tp;
984263bc
MD
465 int flush;
466{
467 struct nm_softc *pti = tp->t_dev->si_drv1;
468 int flag;
469
470 /* note: FLUSHREAD and FLUSHWRITE already ok */
471 if (flush == 0) {
472 flush = TIOCPKT_STOP;
473 pti->pt_flags |= PF_STOPPED;
474 } else
475 pti->pt_flags &= ~PF_STOPPED;
476 /* change of perspective */
477 flag = 0;
478 if (flush & FREAD)
479 flag |= FWRITE;
480 if (flush & FWRITE)
481 flag |= FREAD;
482 wakeup_other(tp, flag);
483}
484
485/*ARGSUSED*/
486static int
41c20dac 487nmdmioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td)
984263bc 488{
f96d6c88 489 struct tty *tp = dev->si_tty;
984263bc
MD
490 struct nm_softc *pti = dev->si_drv1;
491 int error, s;
f96d6c88 492 struct tty *tp2;
984263bc
MD
493 struct softpart *ourpart, *otherpart;
494
495 s = spltty();
496 GETPARTS(tp, ourpart, otherpart);
497 tp2 = &otherpart->nm_tty;
498
41c20dac 499 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, td);
984263bc
MD
500 if (error == ENOIOCTL)
501 error = ttioctl(tp, cmd, data, flag);
502 if (error == ENOIOCTL) {
503 switch (cmd) {
504 case TIOCSBRK:
505 otherpart->gotbreak = 1;
506 break;
507 case TIOCCBRK:
508 break;
509 case TIOCSDTR:
510 ourpart->modemsignals |= TIOCM_DTR;
511 break;
512 case TIOCCDTR:
513 ourpart->modemsignals &= TIOCM_DTR;
514 break;
515 case TIOCMSET:
516 ourpart->modemsignals = *(int *)data;
517 otherpart->modemsignals = *(int *)data;
518 break;
519 case TIOCMBIS:
520 ourpart->modemsignals |= *(int *)data;
521 break;
522 case TIOCMBIC:
523 ourpart->modemsignals &= ~(*(int *)data);
524 otherpart->modemsignals &= ~(*(int *)data);
525 break;
526 case TIOCMGET:
527 *(int *)data = ourpart->modemsignals;
528 break;
529 case TIOCMSDTRWAIT:
530 break;
531 case TIOCMGDTRWAIT:
532 *(int *)data = 0;
533 break;
534 case TIOCTIMESTAMP:
535 case TIOCDCDTIMESTAMP:
536 default:
537 splx(s);
538 error = ENOTTY;
539 return (error);
540 }
541 error = 0;
542 nmdm_crossover(pti, ourpart, otherpart);
543 }
544 splx(s);
545 return (error);
546}
547
548static void
549nmdm_crossover(struct nm_softc *pti,
550 struct softpart *ourpart,
551 struct softpart *otherpart)
552{
553 otherpart->modemsignals &= ~(TIOCM_CTS|TIOCM_CAR);
554 if (ourpart->modemsignals & TIOCM_RTS)
555 otherpart->modemsignals |= TIOCM_CTS;
556 if (ourpart->modemsignals & TIOCM_DTR)
557 otherpart->modemsignals |= TIOCM_CAR;
558}
559
560
561
38e94a25 562static void nmdm_drvinit (void *unused);
984263bc
MD
563
564static void
565nmdm_drvinit(unused)
566 void *unused;
567{
568 cdevsw_add(&nmdm_cdevsw);
569 /* XXX: Gross hack for DEVFS */
570 nmdminit(0);
571}
572
573SYSINIT(nmdmdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,nmdm_drvinit,NULL)