2 * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
30 /* Copyright (c) 1997 Apple Computer, Inc. All Rights Reserved */
32 * Copyright (c) 1982, 1986, 1989, 1993
33 * The Regents of the University of California. All rights reserved.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. All advertising materials mentioning features or use of this software
44 * must display the following acknowledgement:
45 * This product includes software developed by the University of
46 * California, Berkeley and its contributors.
47 * 4. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * @(#)tty_pty.c 8.4 (Berkeley) 2/20/95
67 * Pseudo-teletype Driver
68 * (Actually two drivers, requiring two entries in 'cdevsw')
70 #include "pty.h" /* XXX */
72 #include <sys/param.h>
73 #include <sys/systm.h>
74 #include <sys/ioctl.h>
75 #include <sys/proc_internal.h>
76 #include <sys/kauth.h>
79 #include <sys/file_internal.h>
81 #include <sys/kernel.h>
82 #include <sys/vnode.h>
84 #include <sys/signalvar.h>
88 #define FREE_BSDSTATIC static
90 #define FREE_BSDSTATIC __private_extern__
91 #define d_devtotty_t struct tty **
96 typedef void d_stop_t(struct tty
*tp
, int rw
);
100 /* XXX function should be removed??? */
101 int pty_init(int n_ptys
);
104 static void ptyattach(int n
);
106 static void ptsstart(struct tty
*tp
);
107 static void ptcwakeup(struct tty
*tp
, int flag
);
109 FREE_BSDSTATIC d_open_t ptsopen
;
110 FREE_BSDSTATIC d_close_t ptsclose
;
111 FREE_BSDSTATIC d_read_t ptsread
;
112 FREE_BSDSTATIC d_write_t ptswrite
;
113 FREE_BSDSTATIC d_ioctl_t ptyioctl
;
114 FREE_BSDSTATIC d_stop_t ptsstop
;
115 FREE_BSDSTATIC d_devtotty_t ptydevtotty
;
116 FREE_BSDSTATIC d_open_t ptcopen
;
117 FREE_BSDSTATIC d_close_t ptcclose
;
118 FREE_BSDSTATIC d_read_t ptcread
;
119 FREE_BSDSTATIC d_write_t ptcwrite
;
120 FREE_BSDSTATIC d_select_t ptcselect
;
123 #define CDEV_MAJOR_S 5
124 #define CDEV_MAJOR_C 6
125 static struct cdevsw pts_cdevsw
=
126 { ptsopen
, ptsclose
, ptsread
, ptswrite
, /*5*/
127 ptyioctl
, ptsstop
, nullreset
, ptydevtotty
,/* ttyp */
128 ttselect
, nommap
, NULL
, "pts", NULL
, -1 };
130 static struct cdevsw ptc_cdevsw
=
131 { ptcopen
, ptcclose
, ptcread
, ptcwrite
, /*6*/
132 ptyioctl
, nullstop
, nullreset
, ptydevtotty
,/* ptyp */
133 ptcselect
, nommap
, NULL
, "ptc", NULL
, -1 };
139 #define NPTY 32 /* crude XXX */
140 #warning You have only one pty defined, redefining to 32.
145 #define MAXUNITS (8 * 32)
146 static void *devfs_token_pts
[MAXUNITS
];
147 static void *devfs_token_ptc
[MAXUNITS
];
148 static const char jnames
[] = "pqrsPQRS";
151 #define NPTY MAXUNITS
152 #warning Can't have more than 256 pty's with DEVFS defined.
153 #endif /* NPTY > MAXUNITS */
157 #define BUFSIZ 100 /* Chunk size iomoved to/from user */
160 * pts == /dev/tty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
161 * ptc == /dev/pty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
164 FREE_BSDSTATIC
struct tty pt_tty
[NPTY
]; /* XXX */
166 /* NeXT All references to have been changed to indirections in the file */
167 FREE_BSDSTATIC
struct tty
*pt_tty
[NPTY
] = { NULL
};
170 static struct pt_ioctl
{
172 struct selinfo pt_selr
, pt_selw
;
175 } pt_ioctl
[NPTY
]; /* XXX */
176 static int npty
= NPTY
; /* for pstat -t */
178 #define PF_PKT 0x08 /* packet mode */
179 #define PF_STOPPED 0x10 /* user told stopped */
180 #define PF_REMOTE 0x20 /* remote and flow controlled input */
181 #define PF_NOSTOP 0x40
182 #define PF_UCNTL 0x80 /* user control mode */
186 * Establish n (or default if n is 1) ptys in the system.
188 * XXX cdevsw & pstat require the array `pty[]' to be an array
196 #define DEFAULT_NPTY 32
198 /* maybe should allow 0 => none? */
201 ntb
= n
* sizeof(struct tty
);
203 mem
= malloc(ntb
+ ALIGNBYTES
+ n
* sizeof(struct pt_ioctl
),
206 MALLOC(mem
, char *, ntb
+ ALIGNBYTES
+ n
* sizeof(struct pt_ioctl
),
209 pt_tty
= (struct tty
*)mem
;
210 mem
= (char *)ALIGN(mem
+ ntb
);
211 pt_ioctl
= (struct pt_ioctl
*)mem
;
218 pty_init(__unused
int n_ptys
)
223 #include <miscfs/devfs/devfs.h>
224 #define START_CHAR 'p'
232 /* create the pseudo tty device nodes */
233 for (j
= 0; j
< 10; j
++) {
234 for (i
= 0; i
< HEX_BASE
; i
++) {
235 int m
= j
* HEX_BASE
+ i
;
238 (void)devfs_make_node(makedev(4, m
),
239 DEVFS_CHAR
, UID_ROOT
, GID_WHEEL
, 0666,
240 "tty%c%x", j
+ START_CHAR
, i
);
241 (void)devfs_make_node(makedev(5, m
),
242 DEVFS_CHAR
, UID_ROOT
, GID_WHEEL
, 0666,
243 "pty%c%x", j
+ START_CHAR
, i
);
253 ptsopen(dev_t dev
, int flag
, __unused
int devtype
, __unused
struct proc
*p
)
255 register struct tty
*tp
;
257 boolean_t funnel_state
;
259 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
261 tp
= &pt_tty
[minor(dev
)];
264 * You will see this sort of code coming up in diffs later both
265 * the ttymalloc and the tp indirection.
267 if (minor(dev
) >= npty
) {
271 if (!pt_tty
[minor(dev
)]) {
272 tp
= pt_tty
[minor(dev
)] = ttymalloc();
274 tp
= pt_tty
[minor(dev
)];
276 if ((tp
->t_state
& TS_ISOPEN
) == 0) {
277 ttychars(tp
); /* Set up default chars */
278 tp
->t_iflag
= TTYDEF_IFLAG
;
279 tp
->t_oflag
= TTYDEF_OFLAG
;
280 tp
->t_lflag
= TTYDEF_LFLAG
;
281 tp
->t_cflag
= TTYDEF_CFLAG
;
282 tp
->t_ispeed
= tp
->t_ospeed
= TTYDEF_SPEED
;
283 ttsetwater(tp
); /* would be done in xxparam() */
284 } else if (tp
->t_state
&TS_XCLUDE
&& suser(kauth_cred_get(), NULL
)) {
288 if (tp
->t_oproc
) /* Ctrlr still around. */
289 (void)(*linesw
[tp
->t_line
].l_modem
)(tp
, 1);
290 while ((tp
->t_state
& TS_CARR_ON
) == 0) {
293 error
= ttysleep(tp
, TSA_CARR_ON(tp
), TTIPRI
| PCATCH
,
298 error
= (*linesw
[tp
->t_line
].l_open
)(dev
, tp
);
300 ptcwakeup(tp
, FREAD
|FWRITE
);
302 (void) thread_funnel_set(kernel_flock
, funnel_state
);
307 ptsclose(dev_t dev
, int flag
, __unused
int mode
, __unused proc_t p
)
309 register struct tty
*tp
;
311 boolean_t funnel_state
;
313 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
315 tp
= pt_tty
[minor(dev
)];
316 err
= (*linesw
[tp
->t_line
].l_close
)(tp
, flag
);
317 ptsstop(tp
, FREAD
|FWRITE
);
320 (void) thread_funnel_set(kernel_flock
, funnel_state
);
325 ptsread(dev
, uio
, flag
)
331 struct proc
*p
= curproc
;
333 struct proc
*p
= current_proc();
335 register struct tty
*tp
= pt_tty
[minor(dev
)];
336 register struct pt_ioctl
*pti
= &pt_ioctl
[minor(dev
)];
339 boolean_t funnel_state
;
341 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
344 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
346 if (pti
->pt_flags
& PF_REMOTE
) {
347 while (isbackground(p
, tp
)) {
348 if ((p
->p_sigignore
& sigmask(SIGTTIN
)) ||
349 (ut
->uu_sigmask
& sigmask(SIGTTIN
)) ||
350 p
->p_pgrp
->pg_jobc
== 0 ||
351 p
->p_flag
& P_PPWAIT
) {
355 pgsignal(p
->p_pgrp
, SIGTTIN
, 1);
356 error
= ttysleep(tp
, &lbolt
, TTIPRI
| PCATCH
| PTTYBLOCK
, "ptsbg",
361 if (tp
->t_canq
.c_cc
== 0) {
362 if (flag
& IO_NDELAY
)
363 return (EWOULDBLOCK
);
364 error
= ttysleep(tp
, TSA_PTS_READ(tp
), TTIPRI
| PCATCH
,
370 while (tp
->t_canq
.c_cc
> 1 && uio_resid(uio
) > 0) {
374 cc
= min(uio_resid(uio
), BUFSIZ
);
375 // Don't copy the very last byte
376 cc
= min(cc
, tp
->t_canq
.c_cc
- 1);
377 cc
= q_to_b(&tp
->t_canq
, buf
, cc
);
378 error
= uiomove(buf
, cc
, uio
);
382 if (tp
->t_canq
.c_cc
== 1)
383 (void) getc(&tp
->t_canq
);
388 error
= (*linesw
[tp
->t_line
].l_read
)(tp
, uio
, flag
);
389 ptcwakeup(tp
, FWRITE
);
391 (void) thread_funnel_set(kernel_flock
, funnel_state
);
396 * Write to pseudo-tty.
397 * Wakeups of controlling tty will happen
398 * indirectly, when tty driver calls ptsstart.
401 ptswrite(dev
, uio
, flag
)
406 register struct tty
*tp
;
408 boolean_t funnel_state
;
410 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
412 tp
= pt_tty
[minor(dev
)];
413 if (tp
->t_oproc
== 0)
416 error
= (*linesw
[tp
->t_line
].l_write
)(tp
, uio
, flag
);
418 (void) thread_funnel_set(kernel_flock
, funnel_state
);
423 * Start output on pseudo-tty.
424 * Wake up process selecting or sleeping for input from controlling tty.
430 register struct pt_ioctl
*pti
= &pt_ioctl
[minor(tp
->t_dev
)];
431 boolean_t funnel_state
;
433 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
435 if (tp
->t_state
& TS_TTSTOP
)
437 if (pti
->pt_flags
& PF_STOPPED
) {
438 pti
->pt_flags
&= ~PF_STOPPED
;
439 pti
->pt_send
= TIOCPKT_START
;
441 ptcwakeup(tp
, FREAD
);
443 (void) thread_funnel_set(kernel_flock
, funnel_state
);
452 struct pt_ioctl
*pti
= &pt_ioctl
[minor(tp
->t_dev
)];
453 boolean_t funnel_state
;
455 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
458 selwakeup(&pti
->pt_selr
);
459 wakeup(TSA_PTC_READ(tp
));
462 selwakeup(&pti
->pt_selw
);
463 wakeup(TSA_PTC_WRITE(tp
));
465 (void) thread_funnel_set(kernel_flock
, funnel_state
);
469 ptcopen(dev_t dev
, __unused
int flag
, __unused
int devtype
, __unused proc_t p
)
471 register struct tty
*tp
;
472 struct pt_ioctl
*pti
;
474 boolean_t funnel_state
;
476 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
478 if (minor(dev
) >= npty
) {
482 if(!pt_tty
[minor(dev
)]) {
483 tp
= pt_tty
[minor(dev
)] = ttymalloc();
485 tp
= pt_tty
[minor(dev
)];
490 tp
->t_oproc
= ptsstart
;
491 CLR(tp
->t_state
, TS_ZOMBIE
);
493 tp
->t_stop
= ptsstop
;
495 (void)(*linesw
[tp
->t_line
].l_modem
)(tp
, 1);
496 tp
->t_lflag
&= ~EXTPROC
;
497 pti
= &pt_ioctl
[minor(dev
)];
502 (void) thread_funnel_set(kernel_flock
, funnel_state
);
507 ptcclose(dev_t dev
, __unused
int flags
, __unused
int fmt
, __unused proc_t p
)
509 register struct tty
*tp
;
510 boolean_t funnel_state
;
512 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
514 tp
= pt_tty
[minor(dev
)];
515 (void)(*linesw
[tp
->t_line
].l_modem
)(tp
, 0);
518 * XXX MDMBUF makes no sense for ptys but would inhibit the above
519 * l_modem(). CLOCAL makes sense but isn't supported. Special
520 * l_modem()s that ignore carrier drop make no sense for ptys but
521 * may be in use because other parts of the line discipline make
522 * sense for ptys. Recover by doing everything that a normal
523 * ttymodem() would have done except for sending a SIGHUP.
525 if (tp
->t_state
& TS_ISOPEN
) {
526 tp
->t_state
&= ~(TS_CARR_ON
| TS_CONNECTED
);
527 tp
->t_state
|= TS_ZOMBIE
;
528 ttyflush(tp
, FREAD
| FWRITE
);
531 tp
->t_oproc
= 0; /* mark closed */
533 (void) thread_funnel_set(kernel_flock
, funnel_state
);
538 ptcread(dev
, uio
, flag
)
543 register struct tty
*tp
= pt_tty
[minor(dev
)];
544 struct pt_ioctl
*pti
= &pt_ioctl
[minor(dev
)];
547 boolean_t funnel_state
;
549 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
552 * We want to block until the slave
553 * is open, and there's something to read;
554 * but if we lost the slave or we're NBIO,
555 * then return the appropriate error instead.
558 if (tp
->t_state
&TS_ISOPEN
) {
559 if (pti
->pt_flags
&PF_PKT
&& pti
->pt_send
) {
560 error
= ureadc((int)pti
->pt_send
, uio
);
563 if (pti
->pt_send
& TIOCPKT_IOCTL
) {
564 cc
= min(uio_resid(uio
),
565 sizeof(tp
->t_termios
));
566 uiomove((caddr_t
)&tp
->t_termios
, cc
,
572 if (pti
->pt_flags
&PF_UCNTL
&& pti
->pt_ucntl
) {
573 error
= ureadc((int)pti
->pt_ucntl
, uio
);
579 if (tp
->t_outq
.c_cc
&& (tp
->t_state
&TS_TTSTOP
) == 0)
582 if ((tp
->t_state
& TS_CONNECTED
) == 0)
584 if (flag
& IO_NDELAY
) {
588 error
= tsleep(TSA_PTC_READ(tp
), TTIPRI
| PCATCH
, "ptcin", 0);
592 if (pti
->pt_flags
& (PF_PKT
|PF_UCNTL
))
593 error
= ureadc(0, uio
);
594 while (uio_resid(uio
) > 0 && error
== 0) {
595 cc
= q_to_b(&tp
->t_outq
, buf
, min(uio_resid(uio
), BUFSIZ
));
598 error
= uiomove(buf
, cc
, uio
);
600 (*linesw
[tp
->t_line
].l_start
)(tp
);
603 (void) thread_funnel_set(kernel_flock
, funnel_state
);
609 register struct tty
*tp
;
612 struct pt_ioctl
*pti
= &pt_ioctl
[minor(tp
->t_dev
)];
614 boolean_t funnel_state
;
616 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
618 /* note: FLUSHREAD and FLUSHWRITE already ok */
620 flush
= TIOCPKT_STOP
;
621 pti
->pt_flags
|= PF_STOPPED
;
623 pti
->pt_flags
&= ~PF_STOPPED
;
624 pti
->pt_send
|= flush
;
625 /* change of perspective */
633 (void) thread_funnel_set(kernel_flock
, funnel_state
);
637 ptcselect(dev
, rw
, wql
, p
)
643 register struct tty
*tp
= pt_tty
[minor(dev
)];
644 struct pt_ioctl
*pti
= &pt_ioctl
[minor(dev
)];
646 boolean_t funnel_state
;
648 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
650 if ((tp
->t_state
& TS_CONNECTED
) == 0) {
658 * Need to block timeouts (ttrstart).
660 if ((tp
->t_state
&TS_ISOPEN
) &&
661 tp
->t_outq
.c_cc
&& (tp
->t_state
&TS_TTSTOP
) == 0) {
667 case 0: /* exceptional */
668 if ((tp
->t_state
&TS_ISOPEN
) &&
669 ((pti
->pt_flags
&PF_PKT
&& pti
->pt_send
) ||
670 (pti
->pt_flags
&PF_UCNTL
&& pti
->pt_ucntl
))) {
674 selrecord(p
, &pti
->pt_selr
, wql
);
679 if (tp
->t_state
&TS_ISOPEN
) {
680 if (pti
->pt_flags
& PF_REMOTE
) {
681 if (tp
->t_canq
.c_cc
== 0) {
686 if (tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
< TTYHOG
-2) {
690 if (tp
->t_canq
.c_cc
== 0 && (tp
->t_iflag
&ICANON
)) {
696 selrecord(p
, &pti
->pt_selw
, wql
);
701 (void) thread_funnel_set(kernel_flock
, funnel_state
);
706 ptcwrite(dev
, uio
, flag
)
708 register struct uio
*uio
;
711 register struct tty
*tp
= pt_tty
[minor(dev
)];
712 register u_char
*cp
= NULL
;
714 u_char locbuf
[BUFSIZ
];
716 struct pt_ioctl
*pti
= &pt_ioctl
[minor(dev
)];
718 boolean_t funnel_state
;
720 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
723 if ((tp
->t_state
&TS_ISOPEN
) == 0)
725 if (pti
->pt_flags
& PF_REMOTE
) {
728 while ((uio_resid(uio
) > 0 || cc
> 0) &&
729 tp
->t_canq
.c_cc
< TTYHOG
- 1) {
731 cc
= min(uio_resid(uio
), BUFSIZ
);
732 cc
= min(cc
, TTYHOG
- 1 - tp
->t_canq
.c_cc
);
734 error
= uiomove((caddr_t
)cp
, cc
, uio
);
737 /* check again for safety */
738 if ((tp
->t_state
& TS_ISOPEN
) == 0) {
739 /* adjust as usual */
740 uio_setresid(uio
, (uio_resid(uio
) + cc
));
746 cc
= b_to_q((char *)cp
, cc
, &tp
->t_canq
);
748 * XXX we don't guarantee that the canq size
749 * is >= TTYHOG, so the above b_to_q() may
750 * leave some bytes uncopied. However, space
751 * is guaranteed for the null terminator if
752 * we don't fail here since (TTYHOG - 1) is
753 * not a multiple of CBSIZE.
759 /* adjust for data copied in but not written */
760 uio_setresid(uio
, (uio_resid(uio
) + cc
));
761 (void) putc(0, &tp
->t_canq
);
763 wakeup(TSA_PTS_READ(tp
));
766 while (uio_resid(uio
) > 0 || cc
> 0) {
768 cc
= min(uio_resid(uio
), BUFSIZ
);
770 error
= uiomove((caddr_t
)cp
, cc
, uio
);
773 /* check again for safety */
774 if ((tp
->t_state
& TS_ISOPEN
) == 0) {
775 /* adjust for data copied in but not written */
776 uio_setresid(uio
, (uio_resid(uio
) + cc
));
782 if ((tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
) >= TTYHOG
- 2 &&
783 (tp
->t_canq
.c_cc
> 0 || !(tp
->t_iflag
&ICANON
))) {
784 wakeup(TSA_HUP_OR_INPUT(tp
));
787 (*linesw
[tp
->t_line
].l_rint
)(*cp
++, tp
);
794 (void) thread_funnel_set(kernel_flock
, funnel_state
);
798 * Come here to wait for slave to open, for space
799 * in outq, or space in rawq, or an empty canq.
801 if ((tp
->t_state
& TS_CONNECTED
) == 0) {
802 /* adjust for data copied in but not written */
803 uio_setresid(uio
, (uio_resid(uio
) + cc
));
807 if (flag
& IO_NDELAY
) {
808 /* adjust for data copied in but not written */
809 uio_setresid(uio
, (uio_resid(uio
) + cc
));
814 error
= tsleep(TSA_PTC_WRITE(tp
), TTOPRI
| PCATCH
, "ptcout", 0);
816 /* adjust for data copied in but not written */
817 uio_setresid(uio
, (uio_resid(uio
) + cc
));
824 /* XXX we eventually want to go to this model,
825 * but premier can't change the cdevsw */
830 if (minor(dev
) >= npty
)
833 return &pt_tty
[minor(dev
)];
840 ptyioctl(dev
, cmd
, data
, flag
)
846 ptyioctl(dev
, cmd
, data
, flag
, p
)
854 register struct tty
*tp
= pt_tty
[minor(dev
)];
855 register struct pt_ioctl
*pti
= &pt_ioctl
[minor(dev
)];
856 register u_char
*cc
= tp
->t_cc
;
858 boolean_t funnel_state
;
860 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
863 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
864 * ttywflush(tp) will hang if there are characters in the outq.
866 if (cmd
== TIOCEXT
) {
868 * When the EXTPROC bit is being toggled, we need
869 * to send an TIOCPKT_IOCTL if the packet driver
873 if (pti
->pt_flags
& PF_PKT
) {
874 pti
->pt_send
|= TIOCPKT_IOCTL
;
875 ptcwakeup(tp
, FREAD
);
877 tp
->t_lflag
|= EXTPROC
;
879 if ((tp
->t_lflag
& EXTPROC
) &&
880 (pti
->pt_flags
& PF_PKT
)) {
881 pti
->pt_send
|= TIOCPKT_IOCTL
;
882 ptcwakeup(tp
, FREAD
);
884 tp
->t_lflag
&= ~EXTPROC
;
889 if (cdevsw
[major(dev
)]->d_open
== ptcopen
)
891 if (cdevsw
[major(dev
)].d_open
== ptcopen
)
897 * We aviod calling ttioctl on the controller since,
898 * in that case, tp must be the controlling terminal.
900 *(int *)data
= tp
->t_pgrp
? tp
->t_pgrp
->pg_id
: 0;
905 if (pti
->pt_flags
& PF_UCNTL
) {
909 pti
->pt_flags
|= PF_PKT
;
911 pti
->pt_flags
&= ~PF_PKT
;
916 if (pti
->pt_flags
& PF_PKT
) {
920 pti
->pt_flags
|= PF_UCNTL
;
922 pti
->pt_flags
&= ~PF_UCNTL
;
927 pti
->pt_flags
|= PF_REMOTE
;
929 pti
->pt_flags
&= ~PF_REMOTE
;
930 ttyflush(tp
, FREAD
|FWRITE
);
941 ndflush(&tp
->t_outq
, tp
->t_outq
.c_cc
);
945 if (*(unsigned int *)data
>= NSIG
||
946 *(unsigned int *)data
== 0) {
950 if ((tp
->t_lflag
&NOFLSH
) == 0)
951 ttyflush(tp
, FREAD
|FWRITE
);
952 pgsignal(tp
->t_pgrp
, *(unsigned int *)data
, 1);
953 if ((*(unsigned int *)data
== SIGINFO
) &&
954 ((tp
->t_lflag
&NOKERNINFO
) == 0))
958 error
= (*linesw
[tp
->t_line
].l_ioctl
)(tp
, cmd
, data
, flag
, p
);
959 if (error
== ENOTTY
) {
960 error
= ttioctl(tp
, cmd
, data
, flag
, p
);
962 && pti
->pt_flags
& PF_UCNTL
&& (cmd
& ~0xff) == UIOCCMD(0)) {
963 /* Process the UIOCMD ioctl group */
965 pti
->pt_ucntl
= (u_char
)cmd
;
966 ptcwakeup(tp
, FREAD
);
974 * If external processing and packet mode send ioctl packet.
976 if ((tp
->t_lflag
&EXTPROC
) && (pti
->pt_flags
& PF_PKT
)) {
985 #if COMPAT_43_TTY || defined(COMPAT_SUNOS)
992 pti
->pt_send
|= TIOCPKT_IOCTL
;
993 ptcwakeup(tp
, FREAD
);
998 stop
= (tp
->t_iflag
& IXON
) && CCEQ(cc
[VSTOP
], CTRL('s'))
999 && CCEQ(cc
[VSTART
], CTRL('q'));
1000 if (pti
->pt_flags
& PF_NOSTOP
) {
1002 pti
->pt_send
&= ~TIOCPKT_NOSTOP
;
1003 pti
->pt_send
|= TIOCPKT_DOSTOP
;
1004 pti
->pt_flags
&= ~PF_NOSTOP
;
1005 ptcwakeup(tp
, FREAD
);
1009 pti
->pt_send
&= ~TIOCPKT_DOSTOP
;
1010 pti
->pt_send
|= TIOCPKT_NOSTOP
;
1011 pti
->pt_flags
|= PF_NOSTOP
;
1012 ptcwakeup(tp
, FREAD
);
1016 (void) thread_funnel_set(kernel_flock
, funnel_state
);
1021 static ptc_devsw_installed
= 0;
1024 ptc_drvinit(void *unused
)
1031 if( ! ptc_devsw_installed
) {
1032 dev
= makedev(CDEV_MAJOR_S
, 0);
1033 cdevsw_add(&dev
, &pts_cdevsw
, NULL
);
1034 dev
= makedev(CDEV_MAJOR_C
, 0);
1035 cdevsw_add(&dev
, &ptc_cdevsw
, NULL
);
1036 ptc_devsw_installed
= 1;
1038 for ( i
= 0 ; i
<NPTY
; i
++ ) {
1041 devfs_token_pts
[i
] =
1042 devfs_add_devswf(&pts_cdevsw
,i
,
1044 "tty%c%n",jnames
[j
],k
);
1045 devfs_token_ptc
[i
] =
1046 devfs_add_devswf(&ptc_cdevsw
,i
,
1048 "pty%c%n",jnames
[j
],k
);
1054 SYSINIT(ptcdev
,SI_SUB_DRIVERS
,SI_ORDER_MIDDLE
+CDEV_MAJOR_C
,ptc_drvinit
,NULL
)