1 /*
2  * irixioctl.c: A fucking mess...
3  *
4  * Copyright (C) 1996 David S. Miller (dm@engr.sgi.com)
5  */
6 
7 #include <linux/kernel.h>
8 #include <linux/sched.h>
9 #include <linux/fs.h>
10 #include <linux/mm.h>
11 #include <linux/smp.h>
12 #include <linux/smp_lock.h>
13 #include <linux/tty.h>
14 #include <linux/file.h>
15 
16 #include <asm/uaccess.h>
17 #include <asm/ioctl.h>
18 #include <asm/ioctls.h>
19 
20 #undef DEBUG_IOCTLS
21 #undef DEBUG_MISSING_IOCTL
22 
23 struct irix_termios {
24 	tcflag_t c_iflag, c_oflag, c_cflag, c_lflag;
25 	cc_t c_cc[NCCS];
26 };
27 
28 extern asmlinkage int sys_ioctl(unsigned int fd, unsigned int cmd,
29 				unsigned long arg);
30 extern asmlinkage int sys_write(unsigned int fd,char * buf,unsigned int count);
31 extern void start_tty(struct tty_struct *tty);
get_tty(int fd)32 static struct tty_struct *get_tty(int fd)
33 {
34 	struct file *filp;
35 	struct tty_struct *ttyp = NULL;
36 
37 	read_lock(&current->files->file_lock);
38 	filp = fcheck(fd);
39 	if(filp && filp->private_data) {
40 		ttyp = (struct tty_struct *) filp->private_data;
41 
42 		if(ttyp->magic != TTY_MAGIC)
43 			ttyp =NULL;
44 	}
45 	read_unlock(&current->files->file_lock);
46 	return ttyp;
47 }
48 
get_real_tty(struct tty_struct * tp)49 static struct tty_struct *get_real_tty(struct tty_struct *tp)
50 {
51 	if (tp->driver.type == TTY_DRIVER_TYPE_PTY &&
52 	   tp->driver.subtype == PTY_TYPE_MASTER)
53 		return tp->link;
54 	else
55 		return tp;
56 }
57 
irix_ioctl(int fd,unsigned long cmd,unsigned long arg)58 asmlinkage int irix_ioctl(int fd, unsigned long cmd, unsigned long arg)
59 {
60 	struct tty_struct *tp, *rtp;
61 	mm_segment_t old_fs;
62 	int error = 0;
63 
64 #ifdef DEBUG_IOCTLS
65 	printk("[%s:%d] irix_ioctl(%d, ", current->comm, current->pid, fd);
66 #endif
67 	switch(cmd) {
68 	case 0x00005401:
69 #ifdef DEBUG_IOCTLS
70 		printk("TCGETA, %08lx) ", arg);
71 #endif
72 		error = sys_ioctl(fd, TCGETA, arg);
73 		break;
74 
75 	case 0x0000540d: {
76 		struct termios kt;
77 		struct irix_termios *it = (struct irix_termios *) arg;
78 
79 #ifdef DEBUG_IOCTLS
80 		printk("TCGETS, %08lx) ", arg);
81 #endif
82 		if(!access_ok(VERIFY_WRITE, it, sizeof(*it))) {
83 			error = -EFAULT;
84 			break;
85 		}
86 		old_fs = get_fs(); set_fs(get_ds());
87 		error = sys_ioctl(fd, TCGETS, (unsigned long) &kt);
88 		set_fs(old_fs);
89 		if (error)
90 			break;
91 		__put_user(kt.c_iflag, &it->c_iflag);
92 		__put_user(kt.c_oflag, &it->c_oflag);
93 		__put_user(kt.c_cflag, &it->c_cflag);
94 		__put_user(kt.c_lflag, &it->c_lflag);
95 		for(error = 0; error < NCCS; error++)
96 			__put_user(kt.c_cc[error], &it->c_cc[error]);
97 		error = 0;
98 		break;
99 	}
100 
101 	case 0x0000540e: {
102 		struct termios kt;
103 		struct irix_termios *it = (struct irix_termios *) arg;
104 
105 #ifdef DEBUG_IOCTLS
106 		printk("TCSETS, %08lx) ", arg);
107 #endif
108 		if (!access_ok(VERIFY_READ, it, sizeof(*it))) {
109 			error = -EFAULT;
110 			break;
111 		}
112 		old_fs = get_fs(); set_fs(get_ds());
113 		error = sys_ioctl(fd, TCGETS, (unsigned long) &kt);
114 		set_fs(old_fs);
115 		if(error)
116 			break;
117 		__get_user(kt.c_iflag, &it->c_iflag);
118 		__get_user(kt.c_oflag, &it->c_oflag);
119 		__get_user(kt.c_cflag, &it->c_cflag);
120 		__get_user(kt.c_lflag, &it->c_lflag);
121 		for(error = 0; error < NCCS; error++)
122 			__get_user(kt.c_cc[error], &it->c_cc[error]);
123 		old_fs = get_fs(); set_fs(get_ds());
124 		error = sys_ioctl(fd, TCSETS, (unsigned long) &kt);
125 		set_fs(old_fs);
126 		break;
127 	}
128 
129 	case 0x0000540f:
130 #ifdef DEBUG_IOCTLS
131 		printk("TCSETSW, %08lx) ", arg);
132 #endif
133 		error = sys_ioctl(fd, TCSETSW, arg);
134 		break;
135 
136 	case 0x00005471:
137 #ifdef DEBUG_IOCTLS
138 		printk("TIOCNOTTY, %08lx) ", arg);
139 #endif
140 		error = sys_ioctl(fd, TIOCNOTTY, arg);
141 		break;
142 
143 	case 0x00007416:
144 #ifdef DEBUG_IOCTLS
145 		printk("TIOCGSID, %08lx) ", arg);
146 #endif
147 		tp = get_tty(fd);
148 		if(!tp) {
149 			error = -EINVAL;
150 			break;
151 		}
152 		rtp = get_real_tty(tp);
153 #ifdef DEBUG_IOCTLS
154 		printk("rtp->session=%d ", rtp->session);
155 #endif
156 		error = put_user(rtp->session, (unsigned long *) arg);
157 		break;
158 
159 	case 0x746e:
160 		/* TIOCSTART, same effect as hitting ^Q */
161 #ifdef DEBUG_IOCTLS
162 		printk("TIOCSTART, %08lx) ", arg);
163 #endif
164 		tp = get_tty(fd);
165 		if(!tp) {
166 			error = -EINVAL;
167 			break;
168 		}
169 		rtp = get_real_tty(tp);
170 		start_tty(rtp);
171 		break;
172 
173 	case 0x20006968:
174 #ifdef DEBUG_IOCTLS
175 		printk("SIOCGETLABEL, %08lx) ", arg);
176 #endif
177 		error = -ENOPKG;
178 		break;
179 
180 	case 0x40047477:
181 #ifdef DEBUG_IOCTLS
182 		printk("TIOCGPGRP, %08lx) ", arg);
183 #endif
184 		error = sys_ioctl(fd, TIOCGPGRP, arg);
185 #ifdef DEBUG_IOCTLS
186 		printk("arg=%d ", *(int *)arg);
187 #endif
188 		break;
189 
190 	case 0x40087468:
191 #ifdef DEBUG_IOCTLS
192 		printk("TIOCGWINSZ, %08lx) ", arg);
193 #endif
194 		error = sys_ioctl(fd, TIOCGWINSZ, arg);
195 		break;
196 
197 	case 0x8004667e:
198 #ifdef DEBUG_IOCTLS
199 		printk("FIONBIO, %08lx) arg=%d ", arg, *(int *)arg);
200 #endif
201 		error = sys_ioctl(fd, FIONBIO, arg);
202 		break;
203 
204 	case 0x80047476:
205 #ifdef DEBUG_IOCTLS
206 		printk("TIOCSPGRP, %08lx) arg=%d ", arg, *(int *)arg);
207 #endif
208 		error = sys_ioctl(fd, TIOCSPGRP, arg);
209 		break;
210 
211 	case 0x8020690c:
212 #ifdef DEBUG_IOCTLS
213 		printk("SIOCSIFADDR, %08lx) arg=%d ", arg, *(int *)arg);
214 #endif
215 		error = sys_ioctl(fd, SIOCSIFADDR, arg);
216 		break;
217 
218 	case 0x80206910:
219 #ifdef DEBUG_IOCTLS
220 		printk("SIOCSIFFLAGS, %08lx) arg=%d ", arg, *(int *)arg);
221 #endif
222 		error = sys_ioctl(fd, SIOCSIFFLAGS, arg);
223 		break;
224 
225 	case 0xc0206911:
226 #ifdef DEBUG_IOCTLS
227 		printk("SIOCGIFFLAGS, %08lx) arg=%d ", arg, *(int *)arg);
228 #endif
229 		error = sys_ioctl(fd, SIOCGIFFLAGS, arg);
230 		break;
231 
232 	case 0xc020691b:
233 #ifdef DEBUG_IOCTLS
234 		printk("SIOCGIFMETRIC, %08lx) arg=%d ", arg, *(int *)arg);
235 #endif
236 		error = sys_ioctl(fd, SIOCGIFMETRIC, arg);
237 		break;
238 
239 	default: {
240 #ifdef DEBUG_MISSING_IOCTL
241 		char *msg = "Unimplemented IOCTL cmd tell linux@engr.sgi.com\n";
242 
243 #ifdef DEBUG_IOCTLS
244 		printk("UNIMP_IOCTL, %08lx)\n", arg);
245 #endif
246 		old_fs = get_fs(); set_fs(get_ds());
247 		sys_write(2, msg, strlen(msg));
248 		set_fs(old_fs);
249 		printk("[%s:%d] Does unimplemented IRIX ioctl cmd %08lx\n",
250 		       current->comm, current->pid, cmd);
251 		do_exit(255);
252 #else
253 		error = sys_ioctl (fd, cmd, arg);
254 #endif
255 	}
256 
257 	};
258 #ifdef DEBUG_IOCTLS
259 	printk("error=%d\n", error);
260 #endif
261 	return error;
262 }
263