Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / sys / dev / misc / nmdm / nmdm.c
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 $
34  * $DragonFly: src/sys/dev/misc/nmdm/nmdm.c,v 1.2 2003/06/17 04:28:28 dillon Exp $
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
56 MALLOC_DEFINE(M_NLMDM, "nullmodem", "nullmodem data structures");
57
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));
62
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;
68
69 #define CDEV_MAJOR      18
70 static struct cdevsw nmdm_cdevsw = {
71         /* open */      nmdmopen,
72         /* close */     nmdmclose,
73         /* read */      nmdmread,
74         /* write */     nmdmwrite,
75         /* ioctl */     nmdmioctl,
76         /* poll */      ttypoll,
77         /* mmap */      nommap,
78         /* strategy */  nostrategy,
79         /* name */      "pts",
80         /* maj */       CDEV_MAJOR,
81         /* dump */      nodump,
82         /* psize */     nopsize,
83         /* flags */     D_TTY,
84         /* bmaj */      -1
85 };
86
87 #define BUFSIZ 100              /* Chunk size iomoved to/from user */
88
89 struct softpart {
90         struct tty nm_tty;
91         dev_t   dev;
92         int     modemsignals;   /* bits defined in sys/ttycom.h */
93         int     gotbreak;
94 };
95
96 struct  nm_softc {
97         int     pt_flags;
98         struct softpart part1, part2;
99         struct  prison *pt_prison;
100 };
101
102 #define PF_STOPPED      0x10            /* user told stopped */
103
104 static void
105 nmdm_crossover(struct nm_softc *pti,
106                 struct softpart *ourpart,
107                 struct softpart *otherpart);
108
109 #define GETPARTS(tp, ourpart, otherpart) \
110 do {    \
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; \
115         } else { \
116                 ourpart = &pti->part2; \
117                 otherpart = &pti->part1; \
118         }  \
119 } while (0)
120
121 /*
122  * This function creates and initializes a pair of ttys.
123  */
124 static void
125 nmdminit(n)
126         int n;
127 {
128         dev_t dev1, dev2;
129         struct nm_softc *pt;
130
131         /* For now we only map the lower 8 bits of the minor */
132         if (n & ~0xff)
133                 return;
134
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);
141
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;
153 }
154
155 /*ARGSUSED*/
156 static  int
157 nmdmopen(dev, flag, devtype, p)
158         dev_t dev;
159         int flag, devtype;
160         struct proc *p;
161 {
162         register struct tty *tp, *tp2;
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
171         /*
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.
175          */
176         minr = lminor(dev);
177         pair = minr >> 1;
178         is_b = minr & 1;
179         
180         if (pair < 127) {
181                 nextdev = makedev(major(dev), (pair+pair) + 1);
182                 if (!nextdev->si_drv1) {
183                         nmdminit(pair + 1);
184                 }
185         }
186         if (!dev->si_drv1)
187                 nmdminit(pair);
188
189         if (!dev->si_drv1)
190                 return(ENXIO);  
191
192         pti = dev->si_drv1;
193         if (is_b) 
194                 tp = &pti->part2.nm_tty;
195         else 
196                 tp = &pti->part1.nm_tty;
197         GETPARTS(tp, ourpart, otherpart);
198         tp2 = &otherpart->nm_tty;
199         ourpart->modemsignals |= TIOCM_LE;
200
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(p)) {
209                 return (EBUSY);
210         } else if (pti->pt_prison != p->p_prison) {
211                 return (EBUSY);
212         }
213
214         /*
215          * If the other side is open we have carrier
216          */
217         if (tp2->t_state & TS_ISOPEN) {
218                 (void)(*linesw[tp->t_line].l_modem)(tp, 1);
219         }
220
221         /*
222          * And the other side gets carrier as we are now open.
223          */
224         (void)(*linesw[tp2->t_line].l_modem)(tp2, 1);
225
226         /* External processing makes no sense here */
227         tp->t_lflag &= ~EXTPROC;
228
229         /* 
230          * Wait here if we don't have carrier.
231          */
232 #if 0
233         while ((tp->t_state & TS_CARR_ON) == 0) {
234                 if (flag & FNONBLOCK)
235                         break;
236                 error = ttysleep(tp, TSA_CARR_ON(tp), TTIPRI | PCATCH,
237                                  "nmdopn", 0);
238                 if (error)
239                         return (error);
240         }
241 #endif
242
243         /*
244          * Give the line disciplin a chance to set this end up.
245          */
246         error = (*linesw[tp->t_line].l_open)(dev, tp);
247
248         /*
249          * Wake up the other side.
250          * Theoretically not needed.
251          */
252         ourpart->modemsignals |= TIOCM_DTR;
253         nmdm_crossover(pti, ourpart, otherpart);
254         if (error == 0)
255                 wakeup_other(tp, FREAD|FWRITE); /* XXX */
256         return (error);
257 }
258
259 static  int
260 nmdmclose(dev, flag, mode, p)
261         dev_t dev;
262         int flag, mode;
263         struct proc *p;
264 {
265         register struct tty *tp, *tp2;
266         int err;
267         struct softpart *ourpart, *otherpart;
268
269         /*
270          * let the other end know that the game is up
271          */
272         tp = dev->si_tty;
273         GETPARTS(tp, ourpart, otherpart);
274         tp2 = &otherpart->nm_tty;
275         (void)(*linesw[tp2->t_line].l_modem)(tp2, 0);
276
277         /*
278          * XXX MDMBUF makes no sense for nmdms but would inhibit the above
279          * l_modem().  CLOCAL makes sense but isn't supported.   Special
280          * l_modem()s that ignore carrier drop make no sense for nmdms but
281          * may be in use because other parts of the line discipline make
282          * sense for nmdms.  Recover by doing everything that a normal
283          * ttymodem() would have done except for sending a SIGHUP.
284          */
285         if (tp2->t_state & TS_ISOPEN) {
286                 tp2->t_state &= ~(TS_CARR_ON | TS_CONNECTED);
287                 tp2->t_state |= TS_ZOMBIE;
288                 ttyflush(tp2, FREAD | FWRITE);
289         }
290
291         err = (*linesw[tp->t_line].l_close)(tp, flag);
292         ourpart->modemsignals &= ~TIOCM_DTR;
293         nmdm_crossover(dev->si_drv1, ourpart, otherpart);
294         nmdmstop(tp, FREAD|FWRITE);
295         (void) ttyclose(tp);
296         return (err);
297 }
298
299 static  int
300 nmdmread(dev, uio, flag)
301         dev_t dev;
302         struct uio *uio;
303         int flag;
304 {
305         int error = 0;
306         struct tty *tp, *tp2;
307         struct softpart *ourpart, *otherpart;
308
309         tp = dev->si_tty;
310         GETPARTS(tp, ourpart, otherpart);
311         tp2 = &otherpart->nm_tty;
312
313 #if 0
314         if (tp2->t_state & TS_ISOPEN) {
315                 error = (*linesw[tp->t_line].l_read)(tp, uio, flag);
316                 wakeup_other(tp, FWRITE);
317         } else {
318                 if (flag & IO_NDELAY) {
319                         return (EWOULDBLOCK);
320                 }
321                 error = tsleep(TSA_PTC_READ(tp),
322                                 TTIPRI | PCATCH, "nmdout", 0);
323                 }
324         }
325 #else
326         if ((error = (*linesw[tp->t_line].l_read)(tp, uio, flag)) == 0)
327                 wakeup_other(tp, FWRITE);
328 #endif
329         return (error);
330 }
331
332 /*
333  * Write to pseudo-tty.
334  * Wakeups of controlling tty will happen
335  * indirectly, when tty driver calls nmdmstart.
336  */
337 static  int
338 nmdmwrite(dev, uio, flag)
339         dev_t dev;
340         struct uio *uio;
341         int flag;
342 {
343         register u_char *cp = 0;
344         register int cc = 0;
345         u_char locbuf[BUFSIZ];
346         int cnt = 0;
347         int error = 0;
348         struct tty *tp1, *tp;
349         struct softpart *ourpart, *otherpart;
350
351         tp1 = dev->si_tty;
352         /*
353          * Get the other tty struct.
354          * basically we are writing into the INPUT side of the other device.
355          */
356         GETPARTS(tp1, ourpart, otherpart);
357         tp = &otherpart->nm_tty;
358
359 again:
360         if ((tp->t_state & TS_ISOPEN) == 0) 
361                 return (EIO);
362         while (uio->uio_resid > 0 || cc > 0) {
363                 /*
364                  * Fill up the buffer if it's empty
365                  */
366                 if (cc == 0) {
367                         cc = min(uio->uio_resid, BUFSIZ);
368                         cp = locbuf;
369                         error = uiomove((caddr_t)cp, cc, uio);
370                         if (error)
371                                 return (error);
372                         /* check again for safety */
373                         if ((tp->t_state & TS_ISOPEN) == 0) {
374                                 /* adjust for data copied in but not written */
375                                 uio->uio_resid += cc;
376                                 return (EIO);
377                         }
378                 }
379                 while (cc > 0) {
380                         if (((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= (TTYHOG-2))
381                         && ((tp->t_canq.c_cc > 0) || !(tp->t_iflag&ICANON))) {
382                                 /*
383                                  * Come here to wait for space in outq,
384                                  * or space in rawq, or an empty canq.
385                                  */
386                                 wakeup(TSA_HUP_OR_INPUT(tp));
387                                 if ((tp->t_state & TS_CONNECTED) == 0) {
388                                         /*
389                                          * Data piled up because not connected.
390                                          * Adjust for data copied in but
391                                          * not written.
392                                          */
393                                         uio->uio_resid += cc;
394                                         return (EIO);
395                                 }
396                                 if (flag & IO_NDELAY) {
397                                         /*
398                                          * Don't wait if asked not to.
399                                          * Adjust for data copied in but
400                                          * not written.
401                                          */
402                                         uio->uio_resid += cc;
403                                         if (cnt == 0)
404                                                 return (EWOULDBLOCK);
405                                         return (0);
406                                 }
407                                 error = tsleep(TSA_PTC_WRITE(tp),
408                                                 TTOPRI | PCATCH, "nmdout", 0);
409                                 if (error) {
410                                         /*
411                                          * Tsleep returned (signal?).
412                                          * Go find out what the user wants.
413                                          * adjust for data copied in but
414                                          * not written
415                                          */
416                                         uio->uio_resid += cc;
417                                         return (error);
418                                 }
419                                 goto again;
420                         }
421                         (*linesw[tp->t_line].l_rint)(*cp++, tp);
422                         cnt++;
423                         cc--;
424                 }
425                 cc = 0;
426         }
427         return (0);
428 }
429
430 /*
431  * Start output on pseudo-tty.
432  * Wake up process selecting or sleeping for input from controlling tty.
433  */
434 static void
435 nmdmstart(tp)
436         struct tty *tp;
437 {
438         register struct nm_softc *pti = tp->t_dev->si_drv1;
439
440         if (tp->t_state & TS_TTSTOP)
441                 return;
442         pti->pt_flags &= ~PF_STOPPED;
443         wakeup_other(tp, FREAD);
444 }
445
446 /* Wakes up the OTHER tty;*/
447 static void
448 wakeup_other(tp, flag)
449         struct tty *tp;
450         int flag;
451 {
452         struct softpart *ourpart, *otherpart;
453
454         GETPARTS(tp, ourpart, otherpart);
455         if (flag & FREAD) {
456                 selwakeup(&otherpart->nm_tty.t_rsel);
457                 wakeup(TSA_PTC_READ((&otherpart->nm_tty)));
458         }
459         if (flag & FWRITE) {
460                 selwakeup(&otherpart->nm_tty.t_wsel);
461                 wakeup(TSA_PTC_WRITE((&otherpart->nm_tty)));
462         }
463 }
464
465 static  void
466 nmdmstop(tp, flush)
467         register struct tty *tp;
468         int flush;
469 {
470         struct nm_softc *pti = tp->t_dev->si_drv1;
471         int flag;
472
473         /* note: FLUSHREAD and FLUSHWRITE already ok */
474         if (flush == 0) {
475                 flush = TIOCPKT_STOP;
476                 pti->pt_flags |= PF_STOPPED;
477         } else
478                 pti->pt_flags &= ~PF_STOPPED;
479         /* change of perspective */
480         flag = 0;
481         if (flush & FREAD)
482                 flag |= FWRITE;
483         if (flush & FWRITE)
484                 flag |= FREAD;
485         wakeup_other(tp, flag);
486 }
487
488 /*ARGSUSED*/
489 static  int
490 nmdmioctl(dev, cmd, data, flag, p)
491         dev_t dev;
492         u_long cmd;
493         caddr_t data;
494         int flag;
495         struct proc *p;
496 {
497         register struct tty *tp = dev->si_tty;
498         struct nm_softc *pti = dev->si_drv1;
499         int error, s;
500         register struct tty *tp2;
501         struct softpart *ourpart, *otherpart;
502
503         s = spltty();
504         GETPARTS(tp, ourpart, otherpart);
505         tp2 = &otherpart->nm_tty;
506
507         error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
508         if (error == ENOIOCTL)
509                  error = ttioctl(tp, cmd, data, flag);
510         if (error == ENOIOCTL) {
511                 switch (cmd) {
512                 case TIOCSBRK:
513                         otherpart->gotbreak = 1;
514                         break;
515                 case TIOCCBRK:
516                         break;
517                 case TIOCSDTR:
518                         ourpart->modemsignals |= TIOCM_DTR;
519                         break;
520                 case TIOCCDTR:
521                         ourpart->modemsignals &= TIOCM_DTR;
522                         break;
523                 case TIOCMSET:
524                         ourpart->modemsignals = *(int *)data;
525                         otherpart->modemsignals = *(int *)data;
526                         break;
527                 case TIOCMBIS:
528                         ourpart->modemsignals |= *(int *)data;
529                         break;
530                 case TIOCMBIC:
531                         ourpart->modemsignals &= ~(*(int *)data);
532                         otherpart->modemsignals &= ~(*(int *)data);
533                         break;
534                 case TIOCMGET:
535                         *(int *)data = ourpart->modemsignals;
536                         break;
537                 case TIOCMSDTRWAIT:
538                         break;
539                 case TIOCMGDTRWAIT:
540                         *(int *)data = 0;
541                         break;
542                 case TIOCTIMESTAMP:
543                 case TIOCDCDTIMESTAMP:
544                 default:
545                         splx(s);
546                         error = ENOTTY;
547                         return (error);
548                 }
549                 error = 0;
550                 nmdm_crossover(pti, ourpart, otherpart);
551         }
552         splx(s);
553         return (error);
554 }
555
556 static void
557 nmdm_crossover(struct nm_softc *pti,
558                 struct softpart *ourpart,
559                 struct softpart *otherpart)
560 {
561         otherpart->modemsignals &= ~(TIOCM_CTS|TIOCM_CAR);
562         if (ourpart->modemsignals & TIOCM_RTS)
563                 otherpart->modemsignals |= TIOCM_CTS;
564         if (ourpart->modemsignals & TIOCM_DTR)
565                 otherpart->modemsignals |= TIOCM_CAR;
566 }
567
568
569
570 static void nmdm_drvinit __P((void *unused));
571
572 static void
573 nmdm_drvinit(unused)
574         void *unused;
575 {
576         cdevsw_add(&nmdm_cdevsw);
577         /* XXX: Gross hack for DEVFS */
578         nmdminit(0);
579 }
580
581 SYSINIT(nmdmdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,nmdm_drvinit,NULL)