Change the kernel dev_t, representing a pointer to a specinfo structure,
[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.15 2006/09/10 01:26:34 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/thread2.h>
48 #include <sys/tty.h>
49 #include <sys/conf.h>
50 #include <sys/fcntl.h>
51 #include <sys/poll.h>
52 #include <sys/kernel.h>
53 #include <sys/vnode.h>
54 #include <sys/signalvar.h>
55 #include <sys/malloc.h>
56
57 MALLOC_DEFINE(M_NLMDM, "nullmodem", "nullmodem data structures");
58
59 static void nmdmstart (struct tty *tp);
60 static void nmdmstop (struct tty *tp, int rw);
61 static void wakeup_other (struct tty *tp, int flag);
62 static void nmdminit (int n);
63
64 static  d_open_t        nmdmopen;
65 static  d_close_t       nmdmclose;
66 static  d_read_t        nmdmread;
67 static  d_write_t       nmdmwrite;
68 static  d_ioctl_t       nmdmioctl;
69
70 #define CDEV_MAJOR      18
71 static struct dev_ops nmdm_ops = {
72         { "pts", CDEV_MAJOR, D_TTY },
73         .d_open =       nmdmopen,
74         .d_close =      nmdmclose,
75         .d_read =       nmdmread,
76         .d_write =      nmdmwrite,
77         .d_ioctl =      nmdmioctl,
78         .d_poll =       ttypoll,
79 };
80
81 #define BUFSIZ 100              /* Chunk size iomoved to/from user */
82
83 struct softpart {
84         struct tty nm_tty;
85         cdev_t  dev;
86         int     modemsignals;   /* bits defined in sys/ttycom.h */
87         int     gotbreak;
88 };
89
90 struct  nm_softc {
91         int     pt_flags;
92         struct softpart part1, part2;
93         struct  prison *pt_prison;
94 };
95
96 #define PF_STOPPED      0x10            /* user told stopped */
97
98 static void
99 nmdm_crossover(struct nm_softc *pti,
100                 struct softpart *ourpart,
101                 struct softpart *otherpart);
102
103 #define GETPARTS(tp, ourpart, otherpart) \
104 do {    \
105         struct nm_softc *pti = tp->t_dev->si_drv1; \
106         if (tp == &pti->part1.nm_tty) { \
107                 ourpart = &pti->part1; \
108                 otherpart = &pti->part2; \
109         } else { \
110                 ourpart = &pti->part2; \
111                 otherpart = &pti->part1; \
112         }  \
113 } while (0)
114
115 /*
116  * This function creates and initializes a pair of ttys.
117  */
118 static void
119 nmdminit(int n)
120 {
121         cdev_t dev1, dev2;
122         struct nm_softc *pt;
123
124         /*
125          * Simplified unit number, use low 8 bits of minor number
126          * (remember, the minor number mask is 0xffff00ff).
127          */
128         if (n & ~0x7f)
129                 return;
130
131         pt = kmalloc(sizeof(*pt), M_NLMDM, M_WAITOK);
132         bzero(pt, sizeof(*pt));
133         dev_ops_add(&nmdm_ops, ~1, n << 1);
134         pt->part1.dev = dev1 = make_dev(&nmdm_ops, n << 1,
135             0, 0, 0666, "nmdm%dA", n);
136         pt->part2.dev = dev2 = make_dev(&nmdm_ops, (n << 1) + 1,
137             0, 0, 0666, "nmdm%dB", n);
138
139         dev1->si_drv1 = dev2->si_drv1 = pt;
140         dev1->si_tty = &pt->part1.nm_tty;
141         dev2->si_tty = &pt->part2.nm_tty;
142         ttyregister(&pt->part1.nm_tty);
143         ttyregister(&pt->part2.nm_tty);
144         pt->part1.nm_tty.t_oproc = nmdmstart;
145         pt->part2.nm_tty.t_oproc = nmdmstart;
146         pt->part1.nm_tty.t_stop = nmdmstop;
147         pt->part2.nm_tty.t_dev = dev1;
148         pt->part1.nm_tty.t_dev = dev2;
149         pt->part2.nm_tty.t_stop = nmdmstop;
150 }
151
152 /*ARGSUSED*/
153 static  int
154 nmdmopen(struct dev_open_args *ap)
155 {
156         cdev_t dev = ap->a_head.a_dev;
157         struct tty *tp, *tp2;
158         int error;
159         int minr;
160 #if 0
161         cdev_t nextdev;
162 #endif
163         struct nm_softc *pti;
164         int is_b;
165         int     pair;
166         struct  softpart *ourpart, *otherpart;
167
168         minr = lminor(dev);
169         pair = minr >> 1;
170         is_b = minr & 1;
171         
172 #if 0
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         if (pair < 127) {
179                 nextdev = makedev(major(dev), (pair+pair) + 1);
180                 if (!nextdev->si_drv1) {
181                         nmdminit(pair + 1);
182                 }
183         }
184 #endif
185         if (!dev->si_drv1)
186                 nmdminit(pair);
187
188         if (!dev->si_drv1)
189                 return(ENXIO);  
190
191         pti = dev->si_drv1;
192         if (is_b) 
193                 tp = &pti->part2.nm_tty;
194         else 
195                 tp = &pti->part1.nm_tty;
196         GETPARTS(tp, ourpart, otherpart);
197         tp2 = &otherpart->nm_tty;
198         ourpart->modemsignals |= TIOCM_LE;
199
200         if ((tp->t_state & TS_ISOPEN) == 0) {
201                 ttychars(tp);           /* Set up default chars */
202                 tp->t_iflag = TTYDEF_IFLAG;
203                 tp->t_oflag = TTYDEF_OFLAG;
204                 tp->t_lflag = TTYDEF_LFLAG;
205                 tp->t_cflag = TTYDEF_CFLAG;
206                 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
207         } else if (tp->t_state & TS_XCLUDE && suser_cred(ap->a_cred, 0)) {
208                 return (EBUSY);
209         } else if (pti->pt_prison != ap->a_cred->cr_prison) {
210                 return (EBUSY);
211         }
212
213         /*
214          * If the other side is open we have carrier
215          */
216         if (tp2->t_state & TS_ISOPEN) {
217                 (void)(*linesw[tp->t_line].l_modem)(tp, 1);
218         }
219
220         /*
221          * And the other side gets carrier as we are now open.
222          */
223         (void)(*linesw[tp2->t_line].l_modem)(tp2, 1);
224
225         /* External processing makes no sense here */
226         tp->t_lflag &= ~EXTPROC;
227
228         /* 
229          * Wait here if we don't have carrier.
230          */
231 #if 0
232         while ((tp->t_state & TS_CARR_ON) == 0) {
233                 if (flag & FNONBLOCK)
234                         break;
235                 error = ttysleep(tp, TSA_CARR_ON(tp), PCATCH, "nmdopn", 0);
236                 if (error)
237                         return (error);
238         }
239 #endif
240
241         /*
242          * Give the line disciplin a chance to set this end up.
243          */
244         error = (*linesw[tp->t_line].l_open)(dev, tp);
245
246         /*
247          * Wake up the other side.
248          * Theoretically not needed.
249          */
250         ourpart->modemsignals |= TIOCM_DTR;
251         nmdm_crossover(pti, ourpart, otherpart);
252         if (error == 0)
253                 wakeup_other(tp, FREAD|FWRITE); /* XXX */
254         return (error);
255 }
256
257 static int
258 nmdmclose(struct dev_close_args *ap)
259 {
260         cdev_t dev = ap->a_head.a_dev;
261         struct tty *tp, *tp2;
262         int err;
263         struct softpart *ourpart, *otherpart;
264
265         /*
266          * let the other end know that the game is up
267          */
268         tp = dev->si_tty;
269         GETPARTS(tp, ourpart, otherpart);
270         tp2 = &otherpart->nm_tty;
271         (void)(*linesw[tp2->t_line].l_modem)(tp2, 0);
272
273         /*
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.
280          */
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);
285         }
286
287         err = (*linesw[tp->t_line].l_close)(tp, ap->a_fflag);
288         ourpart->modemsignals &= ~TIOCM_DTR;
289         nmdm_crossover(dev->si_drv1, ourpart, otherpart);
290         nmdmstop(tp, FREAD|FWRITE);
291         (void) ttyclose(tp);
292         return (err);
293 }
294
295 static int
296 nmdmread(struct dev_read_args *ap)
297 {
298         cdev_t dev = ap->a_head.a_dev;
299         int error = 0;
300         struct tty *tp, *tp2;
301         struct softpart *ourpart, *otherpart;
302
303         tp = dev->si_tty;
304         GETPARTS(tp, ourpart, otherpart);
305         tp2 = &otherpart->nm_tty;
306
307 #if 0
308         if (tp2->t_state & TS_ISOPEN) {
309                 error = (*linesw[tp->t_line].l_read)(tp, ap->a_uio, flag);
310                 wakeup_other(tp, FWRITE);
311         } else {
312                 if (flag & IO_NDELAY) {
313                         return (EWOULDBLOCK);
314                 }
315                 error = tsleep(TSA_PTC_READ(tp), PCATCH, "nmdout", 0);
316                 }
317         }
318 #else
319         if ((error = (*linesw[tp->t_line].l_read)(tp, ap->a_uio, ap->a_ioflag)) == 0)
320                 wakeup_other(tp, FWRITE);
321 #endif
322         return (error);
323 }
324
325 /*
326  * Write to pseudo-tty.
327  * Wakeups of controlling tty will happen
328  * indirectly, when tty driver calls nmdmstart.
329  */
330 static  int
331 nmdmwrite(struct dev_write_args *ap)
332 {
333         cdev_t dev = ap->a_head.a_dev;
334         struct uio *uio = ap->a_uio;
335         u_char *cp = 0;
336         int cc = 0;
337         u_char locbuf[BUFSIZ];
338         int cnt = 0;
339         int error = 0;
340         struct tty *tp1, *tp;
341         struct softpart *ourpart, *otherpart;
342
343         tp1 = dev->si_tty;
344         /*
345          * Get the other tty struct.
346          * basically we are writing into the INPUT side of the other device.
347          */
348         GETPARTS(tp1, ourpart, otherpart);
349         tp = &otherpart->nm_tty;
350
351 again:
352         if ((tp->t_state & TS_ISOPEN) == 0) 
353                 return (EIO);
354         while (uio->uio_resid > 0 || cc > 0) {
355                 /*
356                  * Fill up the buffer if it's empty
357                  */
358                 if (cc == 0) {
359                         cc = min(uio->uio_resid, BUFSIZ);
360                         cp = locbuf;
361                         error = uiomove((caddr_t)cp, cc, uio);
362                         if (error)
363                                 return (error);
364                         /* check again for safety */
365                         if ((tp->t_state & TS_ISOPEN) == 0) {
366                                 /* adjust for data copied in but not written */
367                                 uio->uio_resid += cc;
368                                 return (EIO);
369                         }
370                 }
371                 while (cc > 0) {
372                         if (((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= (TTYHOG-2))
373                         && ((tp->t_canq.c_cc > 0) || !(tp->t_iflag&ICANON))) {
374                                 /*
375                                  * Come here to wait for space in outq,
376                                  * or space in rawq, or an empty canq.
377                                  */
378                                 wakeup(TSA_HUP_OR_INPUT(tp));
379                                 if ((tp->t_state & TS_CONNECTED) == 0) {
380                                         /*
381                                          * Data piled up because not connected.
382                                          * Adjust for data copied in but
383                                          * not written.
384                                          */
385                                         uio->uio_resid += cc;
386                                         return (EIO);
387                                 }
388                                 if (ap->a_ioflag & IO_NDELAY) {
389                                         /*
390                                          * Don't wait if asked not to.
391                                          * Adjust for data copied in but
392                                          * not written.
393                                          */
394                                         uio->uio_resid += cc;
395                                         if (cnt == 0)
396                                                 return (EWOULDBLOCK);
397                                         return (0);
398                                 }
399                                 error = tsleep(TSA_PTC_WRITE(tp),
400                                                 PCATCH, "nmdout", 0);
401                                 if (error) {
402                                         /*
403                                          * Tsleep returned (signal?).
404                                          * Go find out what the user wants.
405                                          * adjust for data copied in but
406                                          * not written
407                                          */
408                                         uio->uio_resid += cc;
409                                         return (error);
410                                 }
411                                 goto again;
412                         }
413                         (*linesw[tp->t_line].l_rint)(*cp++, tp);
414                         cnt++;
415                         cc--;
416                 }
417                 cc = 0;
418         }
419         return (0);
420 }
421
422 /*
423  * Start output on pseudo-tty.
424  * Wake up process selecting or sleeping for input from controlling tty.
425  */
426 static void
427 nmdmstart(struct tty *tp)
428 {
429         struct nm_softc *pti = tp->t_dev->si_drv1;
430
431         if (tp->t_state & TS_TTSTOP)
432                 return;
433         pti->pt_flags &= ~PF_STOPPED;
434         wakeup_other(tp, FREAD);
435 }
436
437 /* Wakes up the OTHER tty;*/
438 static void
439 wakeup_other(struct tty *tp, int flag)
440 {
441         struct softpart *ourpart, *otherpart;
442
443         GETPARTS(tp, ourpart, otherpart);
444         if (flag & FREAD) {
445                 selwakeup(&otherpart->nm_tty.t_rsel);
446                 wakeup(TSA_PTC_READ((&otherpart->nm_tty)));
447         }
448         if (flag & FWRITE) {
449                 selwakeup(&otherpart->nm_tty.t_wsel);
450                 wakeup(TSA_PTC_WRITE((&otherpart->nm_tty)));
451         }
452 }
453
454 static  void
455 nmdmstop(struct tty *tp, int flush)
456 {
457         struct nm_softc *pti = tp->t_dev->si_drv1;
458         int flag;
459
460         /* note: FLUSHREAD and FLUSHWRITE already ok */
461         if (flush == 0) {
462                 flush = TIOCPKT_STOP;
463                 pti->pt_flags |= PF_STOPPED;
464         } else
465                 pti->pt_flags &= ~PF_STOPPED;
466         /* change of perspective */
467         flag = 0;
468         if (flush & FREAD)
469                 flag |= FWRITE;
470         if (flush & FWRITE)
471                 flag |= FREAD;
472         wakeup_other(tp, flag);
473 }
474
475 /*ARGSUSED*/
476 static  int
477 nmdmioctl(struct dev_ioctl_args *ap)
478 {
479         cdev_t dev = ap->a_head.a_dev;
480         struct tty *tp = dev->si_tty;
481         struct nm_softc *pti = dev->si_drv1;
482         int error;
483         struct tty *tp2;
484         struct softpart *ourpart, *otherpart;
485
486         crit_enter();
487         GETPARTS(tp, ourpart, otherpart);
488         tp2 = &otherpart->nm_tty;
489
490         error = (*linesw[tp->t_line].l_ioctl)(tp, ap->a_cmd, ap->a_data,
491                                               ap->a_fflag, ap->a_cred);
492         if (error == ENOIOCTL)
493                  error = ttioctl(tp, ap->a_cmd, ap->a_data, ap->a_fflag);
494         if (error == ENOIOCTL) {
495                 switch (ap->a_cmd) {
496                 case TIOCSBRK:
497                         otherpart->gotbreak = 1;
498                         break;
499                 case TIOCCBRK:
500                         break;
501                 case TIOCSDTR:
502                         ourpart->modemsignals |= TIOCM_DTR;
503                         break;
504                 case TIOCCDTR:
505                         ourpart->modemsignals &= TIOCM_DTR;
506                         break;
507                 case TIOCMSET:
508                         ourpart->modemsignals = *(int *)ap->a_data;
509                         otherpart->modemsignals = *(int *)ap->a_data;
510                         break;
511                 case TIOCMBIS:
512                         ourpart->modemsignals |= *(int *)ap->a_data;
513                         break;
514                 case TIOCMBIC:
515                         ourpart->modemsignals &= ~(*(int *)ap->a_data);
516                         otherpart->modemsignals &= ~(*(int *)ap->a_data);
517                         break;
518                 case TIOCMGET:
519                         *(int *)ap->a_data = ourpart->modemsignals;
520                         break;
521                 case TIOCMSDTRWAIT:
522                         break;
523                 case TIOCMGDTRWAIT:
524                         *(int *)ap->a_data = 0;
525                         break;
526                 case TIOCTIMESTAMP:
527                 case TIOCDCDTIMESTAMP:
528                 default:
529                         crit_exit();
530                         error = ENOTTY;
531                         return (error);
532                 }
533                 error = 0;
534                 nmdm_crossover(pti, ourpart, otherpart);
535         }
536         crit_exit();
537         return (error);
538 }
539
540 static void
541 nmdm_crossover(struct nm_softc *pti,
542                 struct softpart *ourpart,
543                 struct softpart *otherpart)
544 {
545         otherpart->modemsignals &= ~(TIOCM_CTS|TIOCM_CAR);
546         if (ourpart->modemsignals & TIOCM_RTS)
547                 otherpart->modemsignals |= TIOCM_CTS;
548         if (ourpart->modemsignals & TIOCM_DTR)
549                 otherpart->modemsignals |= TIOCM_CAR;
550 }
551
552
553
554 static void nmdm_drvinit (void *unused);
555
556 static void
557 nmdm_drvinit(void *unused)
558 {
559         /* XXX: Gross hack for DEVFS */
560         nmdminit(0);
561 }
562
563 SYSINIT(nmdmdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,nmdm_drvinit,NULL)