xref: /netbsd-src/sys/compat/linux/common/linux_hdio.c (revision 8a8f936f250a330d54f8a24ed0e92aadf9743a7b)
1 /*	$NetBSD: linux_hdio.c,v 1.2 2001/06/14 20:32:43 thorpej Exp $	*/
2 
3 /*
4  * Copyright (c) 2000 Wasabi Systems, Inc.
5  * All rights reserved.
6  *
7  * Written by Frank van der Linden for Wasabi Systems, Inc.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *      This product includes software developed for the NetBSD Project by
20  *      Wasabi Systems, Inc.
21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22  *    or promote products derived from this software without specific prior
23  *    written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/ioctl.h>
41 #include <sys/file.h>
42 #include <sys/filedesc.h>
43 #include <sys/mount.h>
44 #include <sys/proc.h>
45 #include <sys/disklabel.h>
46 
47 #include <dev/ata/atareg.h>
48 #include <dev/ic/wdcreg.h>
49 #include <sys/ataio.h>
50 
51 #include <sys/syscallargs.h>
52 
53 #include <compat/linux/common/linux_types.h>
54 #include <compat/linux/common/linux_ioctl.h>
55 #include <compat/linux/common/linux_signal.h>
56 #include <compat/linux/common/linux_util.h>
57 #include <compat/linux/common/linux_hdio.h>
58 
59 #include <compat/linux/linux_syscallargs.h>
60 
61 int
62 linux_ioctl_hdio(struct proc *p, struct linux_sys_ioctl_args *uap,
63 		 register_t *retval)
64 {
65 	u_long com;
66 	int error, error1;
67 	caddr_t sg;
68 	struct filedesc *fdp;
69 	struct file *fp;
70 	int (*ioctlf) __P((struct file *, u_long, caddr_t, struct proc *));
71 	struct ataparams *atap, ata;
72 	struct atareq req;
73 	struct disklabel label, *labp;
74 	struct partinfo partp;
75 	struct linux_hd_geometry hdg;
76 	struct linux_hd_big_geometry hdg_big;
77 
78 	fdp = p->p_fd;
79 	if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
80 		return (EBADF);
81 
82 	FILE_USE(fp);
83 
84 	com = SCARG(uap, com);
85 	ioctlf = fp->f_ops->fo_ioctl;
86 	retval[0] = error = 0;
87 
88 	com = SCARG(uap, com);
89 
90 	switch (com) {
91 	case LINUX_HDIO_OBSOLETE_IDENTITY:
92 	case LINUX_HDIO_GET_IDENTITY:
93 		sg = stackgap_init(p->p_emul);
94 		atap = stackgap_alloc(&sg, DEV_BSIZE);
95 		if (atap == NULL) {
96 			error = ENOMEM;
97 			break;
98 		}
99 
100 		req.flags = ATACMD_READ;
101 		req.command = WDCC_IDENTIFY;
102 		req.databuf = (caddr_t)atap;
103 		req.datalen = DEV_BSIZE;
104 		req.timeout = 1000;
105 		error = ioctlf(fp, ATAIOCCOMMAND, (caddr_t)&req, p);
106 		if (error != 0)
107 			break;
108 		if (req.retsts != ATACMD_OK)
109 			return EIO;
110 		error = copyin(atap, &ata, sizeof ata);
111 		if (error != 0)
112 			break;
113 		/*
114 		 * 142 is the size of the old structure used by Linux,
115 		 * which doesn't seem to be defined anywhere anymore.
116 		 */
117 		error = copyout(&ata, SCARG(uap, data),
118 		    com == LINUX_HDIO_GET_IDENTITY ? sizeof ata : 142);
119 		break;
120 	case LINUX_HDIO_GETGEO:
121 		error = linux_machdepioctl(p, uap, retval);
122 		if (error == 0)
123 			break;
124 		error = ioctlf(fp, DIOCGDEFLABEL, (caddr_t)&label, p);
125 		error1 = ioctlf(fp, DIOCGPART, (caddr_t)&partp, p);
126 		if (error != 0 && error1 != 0) {
127 			error = error1;
128 			break;
129 		}
130 		labp = error != 0 ? &label : partp.disklab;
131 		hdg.start = error1 != 0 ? partp.part->p_offset : 0;
132 		hdg.heads = labp->d_ntracks;
133 		hdg.cylinders = labp->d_ncylinders;
134 		hdg.sectors = labp->d_nsectors;
135 		error = copyout(&hdg, SCARG(uap, data), sizeof hdg);
136 		break;
137 	case LINUX_HDIO_GETGEO_BIG:
138 		error = linux_machdepioctl(p, uap, retval);
139 		if (error == 0)
140 			break;
141 	case LINUX_HDIO_GETGEO_BIG_RAW:
142 		error = ioctlf(fp, DIOCGDEFLABEL, (caddr_t)&label, p);
143 		error1 = ioctlf(fp, DIOCGPART, (caddr_t)&partp, p);
144 		if (error != 0 && error1 != 0) {
145 			error = error1;
146 			break;
147 		}
148 		labp = error != 0 ? &label : partp.disklab;
149 		hdg_big.start = error1 != 0 ? partp.part->p_offset : 0;
150 		hdg_big.heads = labp->d_ntracks;
151 		hdg_big.cylinders = labp->d_ncylinders;
152 		hdg_big.sectors = labp->d_nsectors;
153 		error = copyout(&hdg_big, SCARG(uap, data), sizeof hdg_big);
154 		break;
155 	case LINUX_HDIO_GET_UNMASKINTR:
156 	case LINUX_HDIO_GET_MULTCOUNT:
157 	case LINUX_HDIO_GET_KEEPSETTINGS:
158 	case LINUX_HDIO_GET_32BIT:
159 	case LINUX_HDIO_GET_NOWERR:
160 	case LINUX_HDIO_GET_DMA:
161 	case LINUX_HDIO_GET_NICE:
162 	case LINUX_HDIO_DRIVE_RESET:
163 	case LINUX_HDIO_TRISTATE_HWIF:
164 	case LINUX_HDIO_DRIVE_TASK:
165 	case LINUX_HDIO_DRIVE_CMD:
166 	case LINUX_HDIO_SET_MULTCOUNT:
167 	case LINUX_HDIO_SET_UNMASKINTR:
168 	case LINUX_HDIO_SET_KEEPSETTINGS:
169 	case LINUX_HDIO_SET_32BIT:
170 	case LINUX_HDIO_SET_NOWERR:
171 	case LINUX_HDIO_SET_DMA:
172 	case LINUX_HDIO_SET_PIO_MODE:
173 	case LINUX_HDIO_SCAN_HWIF:
174 	case LINUX_HDIO_SET_NICE:
175 	case LINUX_HDIO_UNREGISTER_HWIF:
176 		error = EINVAL;
177 	}
178 
179 	FILE_UNUSE(fp, p);
180 
181 	return error;
182 }
183