1*41aa5859Sriastradh /* $NetBSD: linux_fdio.c,v 1.14 2021/09/07 11:43:04 riastradh Exp $ */
2a499e569Sfvdl
3a499e569Sfvdl /*
4a499e569Sfvdl * Copyright (c) 2000 Wasabi Systems, Inc.
5a499e569Sfvdl * All rights reserved.
6a499e569Sfvdl *
7a499e569Sfvdl * Written by Frank van der Linden for Wasabi Systems, Inc.
8a499e569Sfvdl *
9a499e569Sfvdl * Redistribution and use in source and binary forms, with or without
10a499e569Sfvdl * modification, are permitted provided that the following conditions
11a499e569Sfvdl * are met:
12a499e569Sfvdl * 1. Redistributions of source code must retain the above copyright
13a499e569Sfvdl * notice, this list of conditions and the following disclaimer.
14a499e569Sfvdl * 2. Redistributions in binary form must reproduce the above copyright
15a499e569Sfvdl * notice, this list of conditions and the following disclaimer in the
16a499e569Sfvdl * documentation and/or other materials provided with the distribution.
17a499e569Sfvdl * 3. All advertising materials mentioning features or use of this software
18a499e569Sfvdl * must display the following acknowledgement:
19a499e569Sfvdl * This product includes software developed for the NetBSD Project by
20a499e569Sfvdl * Wasabi Systems, Inc.
21a499e569Sfvdl * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22a499e569Sfvdl * or promote products derived from this software without specific prior
23a499e569Sfvdl * written permission.
24a499e569Sfvdl *
25a499e569Sfvdl * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26a499e569Sfvdl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27a499e569Sfvdl * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28a499e569Sfvdl * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29a499e569Sfvdl * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30a499e569Sfvdl * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31a499e569Sfvdl * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32a499e569Sfvdl * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33a499e569Sfvdl * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34a499e569Sfvdl * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35a499e569Sfvdl * POSSIBILITY OF SUCH DAMAGE.
36a499e569Sfvdl */
37a499e569Sfvdl
38dab6ef8bSlukem #include <sys/cdefs.h>
39*41aa5859Sriastradh __KERNEL_RCSID(0, "$NetBSD: linux_fdio.c,v 1.14 2021/09/07 11:43:04 riastradh Exp $");
40dab6ef8bSlukem
41a499e569Sfvdl #include <sys/param.h>
42a499e569Sfvdl #include <sys/systm.h>
43a499e569Sfvdl #include <sys/ioctl.h>
44a499e569Sfvdl #include <sys/file.h>
45a499e569Sfvdl #include <sys/filedesc.h>
46a499e569Sfvdl #include <sys/mount.h>
47a499e569Sfvdl #include <sys/proc.h>
48a499e569Sfvdl #include <sys/disklabel.h>
49a499e569Sfvdl
50a499e569Sfvdl #include <sys/fdio.h>
51a499e569Sfvdl
52a499e569Sfvdl #include <sys/syscallargs.h>
53a499e569Sfvdl
54a499e569Sfvdl #include <dev/isa/fdreg.h>
55a499e569Sfvdl
56a499e569Sfvdl #include <compat/linux/common/linux_types.h>
57a499e569Sfvdl #include <compat/linux/common/linux_ioctl.h>
58a499e569Sfvdl #include <compat/linux/common/linux_signal.h>
59a499e569Sfvdl #include <compat/linux/common/linux_util.h>
60a499e569Sfvdl #include <compat/linux/common/linux_fdio.h>
61a478f23bSnjoly #include <compat/linux/common/linux_ipc.h>
62a478f23bSnjoly #include <compat/linux/common/linux_sem.h>
63a499e569Sfvdl
64a499e569Sfvdl #include <compat/linux/linux_syscallargs.h>
65a499e569Sfvdl
66a499e569Sfvdl int
linux_ioctl_fdio(struct lwp * l,const struct linux_sys_ioctl_args * uap,register_t * retval)677e2790cfSdsl linux_ioctl_fdio(struct lwp *l, const struct linux_sys_ioctl_args *uap,
68a499e569Sfvdl register_t *retval)
69a499e569Sfvdl {
70a9ca7a37Sad file_t *fp;
71a499e569Sfvdl int error;
72a9ca7a37Sad int (*ioctlf)(file_t *, u_long, void *);
73a499e569Sfvdl u_long com;
74a499e569Sfvdl struct fdformat_parms fparams;
75a499e569Sfvdl struct linux_floppy_struct lflop;
76a499e569Sfvdl struct linux_floppy_drive_struct ldrive;
77a499e569Sfvdl
78a499e569Sfvdl com = (u_long)SCARG(uap, data);
79a499e569Sfvdl
80a9ca7a37Sad if ((fp = fd_getfile(SCARG(uap, fd))) == NULL)
81a499e569Sfvdl return (EBADF);
82a499e569Sfvdl
83a499e569Sfvdl com = SCARG(uap, com);
84a499e569Sfvdl ioctlf = fp->f_ops->fo_ioctl;
85a499e569Sfvdl
86a499e569Sfvdl retval[0] = error = 0;
87a499e569Sfvdl
88a499e569Sfvdl switch (com) {
89a499e569Sfvdl case LINUX_FDMSGON:
90a499e569Sfvdl case LINUX_FDMSGOFF:
91a499e569Sfvdl case LINUX_FDTWADDLE:
92a499e569Sfvdl case LINUX_FDCLRPRM:
93a499e569Sfvdl /* whatever you say */
94a499e569Sfvdl break;
95a499e569Sfvdl case LINUX_FDPOLLDRVSTAT:
96a499e569Sfvdl /*
97a499e569Sfvdl * Just fill in some innocent defaults.
98a499e569Sfvdl */
99a499e569Sfvdl memset(&ldrive, 0, sizeof ldrive);
100a499e569Sfvdl ldrive.fd_ref = 1;
101a499e569Sfvdl ldrive.maxblock = 2;
102a499e569Sfvdl ldrive.maxtrack = ldrive.track = 1;
103a499e569Sfvdl ldrive.flags = LINUX_FD_DISK_WRITABLE;
104a499e569Sfvdl error = copyout(&ldrive, SCARG(uap, data), sizeof ldrive);
105a499e569Sfvdl break;
106a499e569Sfvdl case LINUX_FDGETPRM:
107a9ca7a37Sad error = ioctlf(fp, FDIOCGETFORMAT, &fparams);
108a499e569Sfvdl if (error != 0)
109a499e569Sfvdl break;
110*41aa5859Sriastradh memset(&lflop, 0, sizeof(lflop));
111a499e569Sfvdl lflop.size = fparams.ncyl * fparams.nspt * fparams.ntrk;
112a499e569Sfvdl lflop.sect = fparams.nspt;
113a499e569Sfvdl lflop.head = fparams.ntrk;
114a499e569Sfvdl lflop.track = fparams.ncyl;
115a499e569Sfvdl lflop.stretch = fparams.stepspercyl == 2 ? 1 : 0;
116a499e569Sfvdl lflop.fmt_gap = fparams.gaplen;
117a499e569Sfvdl /*
118a499e569Sfvdl * XXXX Use knowledge of floppy for these fields.
119a499e569Sfvdl * FDIOCGETFORMAT does not provide enough information.
120a499e569Sfvdl * It would be better to have FDIOCGETFORMAT return more
121a499e569Sfvdl * information.
122a499e569Sfvdl */
123a499e569Sfvdl switch (fparams.xfer_rate) {
124a499e569Sfvdl case 500 * 1024:
125a499e569Sfvdl lflop.spec1 = 0xcf;
126a499e569Sfvdl lflop.gap = 0x1b;
127a499e569Sfvdl lflop.rate = FDC_500KBPS;
128a499e569Sfvdl break;
129a499e569Sfvdl case 300 * 1024:
130a499e569Sfvdl lflop.spec1 = 0xdf;
131a499e569Sfvdl lflop.gap = 0x23;
132a499e569Sfvdl lflop.rate = FDC_300KBPS;
133a499e569Sfvdl break;
134a499e569Sfvdl case 250 * 1024:
135a499e569Sfvdl lflop.spec1 = 0xdf;
136a499e569Sfvdl lflop.gap = 0x2a;
137a499e569Sfvdl lflop.rate = FDC_250KBPS;
138a499e569Sfvdl break;
139a499e569Sfvdl }
140a499e569Sfvdl error = copyout(&lflop, SCARG(uap, data), sizeof lflop);
141a499e569Sfvdl break;
142a499e569Sfvdl case LINUX_FDSETPRM:
143a499e569Sfvdl /*
144a499e569Sfvdl * Should use FDIOCSETFORMAT here, iff its interface
145a499e569Sfvdl * is extended.
146a499e569Sfvdl */
147a499e569Sfvdl case LINUX_FDDEFPRM:
148a499e569Sfvdl case LINUX_FDFMTBEG:
149a499e569Sfvdl case LINUX_FDFMTTRK:
150a499e569Sfvdl case LINUX_FDFMTEND:
151a499e569Sfvdl case LINUX_FDSETEMSGTRESH:
152a499e569Sfvdl case LINUX_FDFLUSH:
153a499e569Sfvdl case LINUX_FDSETMAXERRS:
154a499e569Sfvdl case LINUX_FDGETMAXERRS:
155a499e569Sfvdl case LINUX_FDGETDRVTYP:
156a499e569Sfvdl case LINUX_FDSETDRVPRM:
157a499e569Sfvdl case LINUX_FDGETDRVPRM:
158a499e569Sfvdl case LINUX_FDGETDRVSTAT:
159a499e569Sfvdl case LINUX_FDRESET:
160a499e569Sfvdl case LINUX_FDGETFDCSTAT:
161a499e569Sfvdl case LINUX_FDWERRORCLR:
162a499e569Sfvdl case LINUX_FDWERRORGET:
163a499e569Sfvdl case LINUX_FDRAWCMD:
164a499e569Sfvdl case LINUX_FDEJECT:
165a499e569Sfvdl default:
166a499e569Sfvdl error = EINVAL;
167a499e569Sfvdl }
168a499e569Sfvdl
169a9ca7a37Sad fd_putfile(SCARG(uap, fd));
170a499e569Sfvdl
171a499e569Sfvdl return 0;
172a499e569Sfvdl }
173