Sync usage with man page.
[netbsd-mini2440.git] / sys / compat / irix / irix_ioctl.c
blob8c94b4ffc7fa3d9ebdaef7469ff981a7340d9da5
1 /* $NetBSD: irix_ioctl.c,v 1.19 2008/03/21 21:54:58 ad Exp $ */
3 /*-
4 * Copyright (c) 2002, 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Emmanuel Dreyfus
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: irix_ioctl.c,v 1.19 2008/03/21 21:54:58 ad Exp $");
35 #include <sys/param.h>
36 #include <sys/proc.h>
37 #include <sys/systm.h>
38 #include <sys/mount.h>
39 #include <sys/file.h>
40 #include <sys/filio.h>
41 #include <sys/filedesc.h>
42 #include <sys/ioctl.h>
43 #include <sys/vnode.h>
44 #include <sys/types.h>
45 #include <sys/syscallargs.h>
46 #include <sys/conf.h>
48 #include <miscfs/specfs/specdev.h>
50 #include <compat/common/compat_util.h>
52 #include <compat/svr4/svr4_types.h>
53 #include <compat/svr4/svr4_lwp.h>
54 #include <compat/svr4/svr4_ucontext.h>
55 #include <compat/svr4/svr4_signal.h>
56 #include <compat/svr4/svr4_syscall.h>
57 #include <compat/svr4/svr4_syscallargs.h>
59 #include <compat/irix/irix_ioctl.h>
60 #include <compat/irix/irix_usema.h>
61 #include <compat/irix/irix_signal.h>
62 #include <compat/irix/irix_types.h>
63 #include <compat/irix/irix_exec.h>
64 #include <compat/irix/irix_syscallargs.h>
66 int
67 irix_sys_ioctl(struct lwp *l, const struct irix_sys_ioctl_args *uap, register_t *retval)
69 /* {
70 syscallarg(int) fd;
71 syscallarg(u_long) com;
72 syscallarg(void *) data;
73 } */
74 extern const struct cdevsw irix_usema_cdevsw;
75 u_long cmd;
76 void *data;
77 file_t *fp;
78 struct vnode *vp;
79 struct vattr vattr;
80 struct irix_ioctl_usrdata iiu;
81 int error, val, fd;
84 * This duplicates 6 lines from svr4_sys_ioctl()
85 * It would be nice to merge it.
87 fd = SCARG(uap, fd);
88 cmd = SCARG(uap, com);
89 data = SCARG(uap, data);
91 if ((fp = fd_getfile(fd)) == NULL)
92 return EBADF;
94 if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
95 fd_putfile(fd);
96 return EBADF;
100 * A special hook for /dev/usemaclone ioctls. Some of the ioctl
101 * commands need to set the return value, which is normally
102 * impossible in the file methods and lower. We do the job by
103 * copying the retval address and the data argument to a
104 * struct irix_ioctl_usrdata. The data argument
105 * is set to the address of the structure, and the underlying
106 * code will be able to retreive both data and the retval address
107 * from the struct irix_ioctl_usrdata.
109 * We also bypass the checks in sys_ioctl() because theses ioctl
110 * are defined _IO but really are _IOR. XXX need security review.
112 if ((cmd & IRIX_UIOC_MASK) == IRIX_UIOC) {
113 if (fp->f_type != DTYPE_VNODE) {
114 fd_putfile(fd);
115 return ENOTTY;
117 vp = fp->f_data;
118 if (vp->v_type != VCHR ||
119 cdevsw_lookup(vp->v_rdev) != &irix_usema_cdevsw ||
120 minor(vp->v_rdev) != IRIX_USEMACLNDEV_MINOR) {
121 error = ENOTTY;
122 goto out;
125 iiu.iiu_data = data;
126 iiu.iiu_retval = retval;
128 error = (*fp->f_ops->fo_ioctl)(fp, cmd, &iiu);
129 out:
130 fd_putfile(fd);
131 return error;
134 switch (cmd) {
135 case IRIX_SIOCNREAD: /* number of bytes to read */
136 error = (*(fp->f_ops->fo_ioctl))(fp, FIONREAD,
137 SCARG(uap, data));
138 fd_putfile(fd);
139 return error;
141 case IRIX_MTIOCGETBLKSIZE: /* get tape block size in 512B units */
142 if (fp->f_type != DTYPE_VNODE) {
143 fd_putfile(fd);
144 return ENOSYS;
147 vp = fp->f_data;
149 switch (vp->v_type) {
150 case VREG:
151 case VLNK:
152 case VDIR:
153 error = ENOTTY;
154 break;
155 case VCHR:
156 case VFIFO:
157 error = EINVAL;
158 break;
159 case VBLK:
160 error = VOP_GETATTR(vp, &vattr, l->l_cred);
161 if (error == 0) {
162 val = vattr.va_blocksize / 512;
163 error = copyout(&val, data, sizeof(int));
166 default:
167 error = ENOSYS;
168 break;
171 fd_putfile(fd);
172 return error;
174 default: /* Fallback to the standard SVR4 ioctl's */
175 fd_putfile(fd);
176 return svr4_sys_ioctl(l, (const void *)uap, retval);