[0.4.14] Sledgehammer-revert of the work towards MountMgr
[reactos.git] / drivers / storage / floppy / floppy / ioctl.c
1 /*
2 * ReactOS Floppy Driver
3 * Copyright (C) 2004, Vizzini (vizzini@plasmic.com)
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * PROJECT: ReactOS Floppy Driver
20 * FILE: ioctl.c
21 * PURPOSE: IOCTL Routines
22 * PROGRAMMER: Vizzini (vizzini@plasmic.com)
23 * REVISIONS:
24 * 15-Feb-2004 vizzini - Created
25 * NOTES:
26 * - All IOCTL documentation taken from the DDK
27 * - This driver tries to support all of the IOCTLs that the DDK floppy
28 * sample does
29 *
30 * TODO: Add support to GET_MEDIA_TYPES for non-1.44 disks
31 * TODO: Implement format
32 */
33
34 #include "precomp.h"
35
36 #include <debug.h>
37
38 NTSTATUS NTAPI
39 DeviceIoctl(PDEVICE_OBJECT DeviceObject, PIRP Irp)
40 /*
41 * FUNCTION: Queue IOCTL IRPs
42 * ARGUMENTS:
43 * DeviceObject: DeviceObject that is the target of the IRP
44 * Irp: IRP to process
45 * RETURNS:
46 * STATUS_SUCCESS in all cases, so far
47 * NOTES:
48 * - We can't just service these immediately because, even though some
49 * are able to run at DISPATCH, they'll get out of sync with other
50 * read/write or ioctl irps.
51 */
52 {
53 ASSERT(DeviceObject);
54 ASSERT(Irp);
55
56 Irp->Tail.Overlay.DriverContext[0] = DeviceObject;
57 IoCsqInsertIrp(&Csq, Irp, NULL);
58
59 return STATUS_PENDING;
60 }
61
62
63 VOID NTAPI
64 DeviceIoctlPassive(PDRIVE_INFO DriveInfo, PIRP Irp)
65 /*
66 * FUNCTION: Handles IOCTL requests at PASSIVE_LEVEL
67 * ARGUMENTS:
68 * DriveInfo: Drive that the IOCTL is directed at
69 * Irp: IRP with the request in it
70 */
71 {
72 PIO_STACK_LOCATION Stack = IoGetCurrentIrpStackLocation(Irp);
73 ULONG OutputLength = Stack->Parameters.DeviceIoControl.OutputBufferLength;
74 PVOID OutputBuffer = Irp->AssociatedIrp.SystemBuffer;
75 ULONG Code = Stack->Parameters.DeviceIoControl.IoControlCode;
76 BOOLEAN DiskChanged;
77 PMOUNTDEV_NAME Name;
78
79 TRACE_(FLOPPY, "DeviceIoctl called\n");
80 Irp->IoStatus.Status = STATUS_SUCCESS;
81 Irp->IoStatus.Information = 0;
82
83 /*
84 * First the non-change-sensitive ioctls
85 */
86 if(Code == IOCTL_DISK_GET_MEDIA_TYPES)
87 {
88 PDISK_GEOMETRY Geometry = OutputBuffer;
89 INFO_(FLOPPY, "IOCTL_DISK_GET_MEDIA_TYPES Called\n");
90
91 if(OutputLength < sizeof(DISK_GEOMETRY))
92 {
93 INFO_(FLOPPY, "IOCTL_DISK_GET_MEDIA_TYPES: insufficient buffer; returning STATUS_INVALID_PARAMETER\n");
94 Irp->IoStatus.Status = STATUS_INVALID_PARAMETER;
95 IoCompleteRequest(Irp, IO_NO_INCREMENT);
96 return;
97 }
98
99 /*
100 * for now, this driver only supports 3.5" HD media
101 */
102 Geometry->MediaType = F3_1Pt44_512;
103 Geometry->Cylinders.QuadPart = 80;
104 Geometry->TracksPerCylinder = 2 * 18;
105 Geometry->SectorsPerTrack = 18;
106 Geometry->BytesPerSector = 512;
107
108 Irp->IoStatus.Status = STATUS_SUCCESS;
109 Irp->IoStatus.Information = sizeof(DISK_GEOMETRY);
110 INFO_(FLOPPY, "Ioctl: completing with STATUS_SUCCESS\n");
111 IoCompleteRequest(Irp, IO_NO_INCREMENT);
112
113 return;
114 }
115
116 /*
117 * Now, check to see if the volume needs to be verified. If so,
118 * return STATUS_VERIFY_REQUIRED.
119 *
120 * NOTE: This code, which is outside of the switch and if/else blocks,
121 * will implicity catch and correctly service IOCTL_DISK_CHECK_VERIFY.
122 * Therefore if we see one below in the switch, we can return STATUS_SUCCESS
123 * immediately.
124 */
125 if(DriveInfo->DeviceObject->Flags & DO_VERIFY_VOLUME && !(Stack->Flags & SL_OVERRIDE_VERIFY_VOLUME))
126 {
127 INFO_(FLOPPY, "DeviceIoctl(): completing with STATUS_VERIFY_REQUIRED\n");
128 Irp->IoStatus.Status = STATUS_VERIFY_REQUIRED;
129 Irp->IoStatus.Information = 0;
130 IoCompleteRequest(Irp, IO_NO_INCREMENT);
131 return;
132 }
133
134 /*
135 * Start the drive to see if the disk has changed
136 */
137 StartMotor(DriveInfo);
138
139 /*
140 * Check the change line, and if it's set, return
141 */
142 if(HwDiskChanged(DriveInfo, &DiskChanged) != STATUS_SUCCESS)
143 {
144 WARN_(FLOPPY, "DeviceIoctl(): unable to sense disk change; completing with STATUS_UNSUCCESSFUL\n");
145 Irp->IoStatus.Status = STATUS_UNSUCCESSFUL;
146 Irp->IoStatus.Information = 0;
147 IoCompleteRequest(Irp, IO_NO_INCREMENT);
148 StopMotor(DriveInfo->ControllerInfo);
149 return;
150 }
151
152 if(DiskChanged)
153 {
154 INFO_(FLOPPY, "DeviceIoctl(): detected disk changed; signalling media change and completing\n");
155 SignalMediaChanged(DriveInfo->DeviceObject, Irp);
156
157 /*
158 * Just guessing here - I have a choice of returning NO_MEDIA or VERIFY_REQUIRED. If there's
159 * really no disk in the drive, I'm thinking I can save time by just reporting that fact, rather
160 * than forcing windows to ask me twice. If this doesn't work, we'll need to split this up and
161 * handle the CHECK_VERIFY IOCTL separately.
162 */
163 if(ResetChangeFlag(DriveInfo) == STATUS_NO_MEDIA_IN_DEVICE)
164 Irp->IoStatus.Status = STATUS_NO_MEDIA_IN_DEVICE;
165
166 IoCompleteRequest(Irp, IO_NO_INCREMENT);
167 StopMotor(DriveInfo->ControllerInfo);
168 return;
169 }
170
171 switch(Code)
172 {
173 case IOCTL_DISK_IS_WRITABLE:
174 {
175 UCHAR Status;
176
177 INFO_(FLOPPY, "IOCTL_DISK_IS_WRITABLE Called\n");
178
179 /* This IRP always has 0 information */
180 Irp->IoStatus.Information = 0;
181
182 if(HwSenseDriveStatus(DriveInfo) != STATUS_SUCCESS)
183 {
184 WARN_(FLOPPY, "IoctlDiskIsWritable(): unable to sense drive status\n");
185 Irp->IoStatus.Status = STATUS_IO_DEVICE_ERROR;
186 break;
187 }
188
189 /* Now, read the drive's status back */
190 if(HwSenseDriveStatusResult(DriveInfo->ControllerInfo, &Status) != STATUS_SUCCESS)
191 {
192 WARN_(FLOPPY, "IoctlDiskIsWritable(): unable to read drive status result\n");
193 Irp->IoStatus.Status = STATUS_IO_DEVICE_ERROR;
194 break;
195 }
196
197 /* Check to see if the write flag is set. */
198 if(Status & SR3_WRITE_PROTECT_STATUS_SIGNAL)
199 {
200 INFO_(FLOPPY, "IOCTL_DISK_IS_WRITABLE: disk is write protected\n");
201 Irp->IoStatus.Status = STATUS_MEDIA_WRITE_PROTECTED;
202 }
203 else
204 Irp->IoStatus.Status = STATUS_SUCCESS;
205 }
206 break;
207
208 case IOCTL_DISK_CHECK_VERIFY:
209 INFO_(FLOPPY, "IOCTL_DISK_CHECK_VERIFY called\n");
210 if (OutputLength != 0)
211 {
212 if (OutputLength < sizeof(ULONG))
213 {
214 Irp->IoStatus.Status = STATUS_BUFFER_TOO_SMALL;
215 Irp->IoStatus.Information = 0;
216 }
217 else
218 {
219 *((PULONG)OutputBuffer) = DriveInfo->DiskChangeCount;
220 Irp->IoStatus.Status = STATUS_SUCCESS;
221 Irp->IoStatus.Information = sizeof(ULONG);
222 }
223 }
224 else
225 {
226 Irp->IoStatus.Status = STATUS_SUCCESS;
227 Irp->IoStatus.Information = 0;
228 }
229 break;
230
231 case IOCTL_DISK_GET_DRIVE_GEOMETRY:
232 {
233 INFO_(FLOPPY, "IOCTL_DISK_GET_DRIVE_GEOMETRY Called\n");
234 if(OutputLength < sizeof(DISK_GEOMETRY))
235 {
236 Irp->IoStatus.Status = STATUS_INVALID_PARAMETER;
237 break;
238 }
239
240 /* This still works right even if DriveInfo->DiskGeometry->MediaType = Unknown */
241 memcpy(OutputBuffer, &DriveInfo->DiskGeometry, sizeof(DISK_GEOMETRY));
242 Irp->IoStatus.Information = sizeof(DISK_GEOMETRY);
243 break;
244 }
245
246 case IOCTL_DISK_FORMAT_TRACKS:
247 case IOCTL_DISK_FORMAT_TRACKS_EX:
248 ERR_(FLOPPY, "Format called; not supported yet\n");
249 Irp->IoStatus.Status = STATUS_NOT_IMPLEMENTED;
250 Irp->IoStatus.Information = 0;
251 break;
252
253 case IOCTL_DISK_GET_PARTITION_INFO:
254 INFO_(FLOPPY, "IOCTL_DISK_GET_PARTITION_INFO Called; not supported by a floppy driver\n");
255 Irp->IoStatus.Status = STATUS_INVALID_DEVICE_REQUEST;
256 Irp->IoStatus.Information = 0;
257 break;
258
259 case IOCTL_MOUNTDEV_QUERY_DEVICE_NAME:
260 if (OutputLength < sizeof(MOUNTDEV_NAME)) {
261 Irp->IoStatus.Status = STATUS_BUFFER_TOO_SMALL;
262 Irp->IoStatus.Information = sizeof(MOUNTDEV_NAME);
263 break;
264 }
265
266 Name = Irp->AssociatedIrp.SystemBuffer;
267 Name->NameLength = wcslen(&DriveInfo->SymLinkBuffer[0]) * sizeof(WCHAR);
268
269 if (OutputLength < sizeof(USHORT) + Name->NameLength) {
270 Irp->IoStatus.Status = STATUS_BUFFER_OVERFLOW;
271 Irp->IoStatus.Information = sizeof(MOUNTDEV_NAME);
272 break;
273 }
274
275 RtlCopyMemory(Name->Name, &DriveInfo->SymLinkBuffer[0],
276 Name->NameLength);
277
278 Irp->IoStatus.Status = STATUS_SUCCESS;
279 Irp->IoStatus.Information = sizeof(USHORT) + Name->NameLength;
280 break;
281
282 default:
283 ERR_(FLOPPY, "UNKNOWN IOCTL CODE: 0x%x\n", Code);
284 Irp->IoStatus.Status = STATUS_NOT_SUPPORTED;
285 Irp->IoStatus.Information = 0;
286 break;
287 }
288
289 INFO_(FLOPPY, "ioctl: completing with status 0x%x\n", Irp->IoStatus.Status);
290 IoCompleteRequest(Irp, IO_NO_INCREMENT);
291
292 StopMotor(DriveInfo->ControllerInfo);
293 return;
294 }