xref: /original-bsd/sys/sparc/sunos/sun_ioctl.c (revision f4532de0)
1 /*
2  * Copyright (c) 1992 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * This software was developed by the Computer Systems Engineering group
6  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
7  * contributed to Berkeley.
8  *
9  * %sccs.include.redist.c%
10  *
11  *	@(#)sun_ioctl.c	7.1 (Berkeley) 07/13/92
12  *
13  * from: $Header: sun_ioctl.c,v 1.5 92/07/10 00:26:51 torek Exp $
14  */
15 
16 #include "param.h"
17 #include "proc.h"
18 #include "file.h"
19 #include "filedesc.h"
20 #include "ioctl.h"
21 #include "termios.h"
22 #include "tty.h"
23 
24 /*
25  * SunOS ioctl calls.
26  * This file is something of a hodge-podge.
27  * Support gets added as things turn up....
28  */
29 
30 struct sun_ttysize {
31 	int	ts_row;
32 	int	ts_col;
33 };
34 
35 struct sun_termio {
36 	u_short	c_iflag;
37 	u_short	c_oflag;
38 	u_short	c_cflag;
39 	u_short	c_lflag;
40 	char	c_line;
41 	unsigned char c_cc[8];
42 };
43 
44 struct sun_ioctl_args {
45 	int	fd;
46 	int	cmd;
47 	caddr_t	data;
48 };
49 sun_ioctl(p, uap, retval)
50 	register struct proc *p;
51 	register struct sun_ioctl_args *uap;
52 	int *retval;
53 {
54 	register struct filedesc *fdp = p->p_fd;
55 	register struct file *fp;
56 	register int (*ctl)();
57 	int error;
58 
59 	if ((unsigned)uap->fd >= fdp->fd_nfiles ||
60 	    (fp = fdp->fd_ofiles[uap->fd]) == NULL)
61 		return (EBADF);
62 	if ((fp->f_flag & (FREAD|FWRITE)) == 0)
63 		return (EBADF);
64 	ctl = fp->f_ops->fo_ioctl;
65 
66 	switch (uap->cmd) {
67 
68 	case _IOR('t', 0, int):
69 		uap->cmd = TIOCGETD;
70 		break;
71 
72 	case _IOW('t', 1, int):
73 		uap->cmd = TIOCSETD;
74 		break;
75 
76 	case _IO('t', 36): {		/* sun TIOCCONS, no parameters */
77 		int on = 1;
78 		return ((*ctl)(fp, TIOCCONS, (caddr_t)&on, p));
79 	    }
80 
81 	case _IOW('t', 37, struct sun_ttysize): {
82 		struct winsize ws;
83 		if ((error = (*ctl)(fp, TIOCGWINSZ, (caddr_t)&ws, p)) != 0)
84 			return (error);
85 		ws.ws_row = ((struct sun_ttysize *)uap->data)->ts_row;
86 		ws.ws_col = ((struct sun_ttysize *)uap->data)->ts_col;
87 		return ((*ctl)(fp, TIOCSWINSZ, (caddr_t)&ws, p));
88 	    }
89 
90 	case _IOW('t', 38, struct sun_ttysize): {
91 		struct winsize ws;
92 		if ((error = (*ctl)(fp, TIOCGWINSZ, (caddr_t)&ws, p)) != 0)
93 			return (error);
94 		((struct sun_ttysize *)uap->data)->ts_row = ws.ws_row;
95 		((struct sun_ttysize *)uap->data)->ts_col = ws.ws_col;
96 		return (0);
97 	    }
98 
99 	case _IOR('t', 130, int):
100 		uap->cmd = TIOCSPGRP;
101 		break;
102 
103 	case _IOR('t', 131, int):
104 		uap->cmd = TIOCGPGRP;
105 		break;
106 
107 	case _IO('t', 132):
108 		uap->cmd = TIOCSCTTY;
109 		break;
110 
111 	case _IOR('T', 1, struct sun_termio): {
112 		struct termios bt;
113 		struct sun_termio st;
114 		int speed;
115 		static struct speedtab sptab[] = {
116 			0,0, 50,1, 75,2, 110,3, 134,4, 135,4, 150,5, 200,6,
117 			300,7, 600,8, 1200,9, 1800,10, 2400,11, 4800,12,
118 			9600,13, 19200,14, 38400,15, -1,-1
119 		};
120 
121 		if ((error = (*ctl)(fp, TIOCGETA, (caddr_t)&bt, p)) != 0)
122 			return (error);
123 		/* most bits match */
124 		st.c_iflag = bt.c_iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK|
125 		    ISTRIP|INLCR|IGNCR|ICRNL|IXANY|IMAXBEL);
126 		if (bt.c_iflag & IXON)
127 			st.c_iflag |= 0x0400;
128 		if (bt.c_iflag & IXOFF)
129 			st.c_iflag |= 0x1000;
130 		st.c_oflag = bt.c_oflag & OPOST;
131 		if (bt.c_oflag & ONLCR)
132 			st.c_oflag |= 0x0004;
133 		if (bt.c_oflag & OXTABS)
134 			st.c_oflag |= 0x1800;
135 		speed = ttspeedtab(bt.c_ospeed, sptab);
136 		st.c_cflag = speed >= 0 ? speed : 0;
137 		st.c_cflag |= (bt.c_cflag & CSIZE) >> 4;
138 		if (bt.c_cflag & CSTOPB)
139 			st.c_cflag |= 0x40;
140 		if (bt.c_cflag & PARENB)
141 			st.c_cflag |= 0x100;
142 		if (bt.c_cflag & PARODD)
143 			st.c_cflag |= 0x200;
144 		if (bt.c_cflag & HUPCL)
145 			st.c_cflag |= 0x400;
146 		if (bt.c_cflag & CLOCAL)
147 			st.c_cflag |= 0x800;
148 		st.c_lflag = 0;
149 		if (bt.c_lflag & (ECHOKE|ECHOE|ECHOK))
150 			st.c_lflag |= 0x0800;
151 		if (bt.c_lflag & ECHO)
152 			st.c_lflag |= 0x0008;
153 		if (bt.c_lflag & ECHONL)
154 			st.c_lflag |= 0x0040;
155 		if (bt.c_lflag & ECHOPRT)
156 			st.c_lflag |= 0x0400;
157 		if (bt.c_lflag & ECHOCTL)
158 			st.c_lflag |= 0x0200;
159 		if (bt.c_lflag & ISIG)
160 			st.c_lflag |= 0x0001;
161 		if (bt.c_lflag & ICANON)
162 			st.c_lflag |= 0x0002;
163 		if (bt.c_lflag & IEXTEN)
164 			st.c_lflag |= 0x8000;
165 		if (bt.c_lflag & NOFLSH)
166 			st.c_lflag |= 0x0080;
167 #define mapcc(x) ((x) == _POSIX_VDISABLE ? 0 : (x))
168 		st.c_cc[0] = mapcc(bt.c_cc[VINTR]);
169 		st.c_cc[1] = mapcc(bt.c_cc[VQUIT]);
170 		st.c_cc[2] = mapcc(bt.c_cc[VERASE]);
171 		st.c_cc[3] = mapcc(bt.c_cc[VKILL]);
172 		st.c_cc[4] = mapcc(bt.c_cc[VEOF]);
173 		st.c_cc[5] = mapcc(bt.c_cc[VEOL]);
174 		st.c_cc[6] = mapcc(bt.c_cc[VEOL2]);
175 		st.c_cc[7] = 0;
176 		return (copyout((caddr_t)&st, uap->data, sizeof(st)));
177 	    }
178 	}
179 	return (ioctl(p, uap, retval));
180 }
181