2 * ReactOS Floppy Driver
3 * Copyright (C) 2004, Vizzini (vizzini@plasmic.com)
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.
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.
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.
19 * PROJECT: ReactOS Floppy Driver
21 * PURPOSE: Read/Write handler routines
22 * PROGRAMMER: Vizzini (vizzini@plasmic.com)
24 * 15-Feb-2004 vizzini - Created
29 * This process is extracted from the Intel datasheet for the floppy controller.
31 * - Turn on the motor and set turnoff time
32 * - Program the drive's data rate
36 * - Send read/write command to FDC
39 * This is mostly implemented in one big function, which watis on the SynchEvent
40 * as many times as necessary to get through the process. See ReadWritePassive() for
44 * - Currently doesn't support partial-sector transfers, which is really just a failing
45 * of RWComputeCHS. I've never seen Windows send a partial-sector request, though, so
46 * this may not be a bad thing. Should be looked into, regardless.
48 * TODO: Break up ReadWritePassive and handle errors better
49 * TODO: Figure out data rate issues
50 * TODO: Media type detection
51 * TODO: Figure out perf issue - waiting after call to read/write for about a second each time
52 * TODO: Figure out specify timings
59 static IO_ALLOCATION_ACTION NTAPI
60 MapRegisterCallback(PDEVICE_OBJECT DeviceObject
,
62 PVOID MapRegisterBase
,
65 * FUNCTION: Acquire map registers in prep for DMA
67 * DeviceObject: unused
69 * MapRegisterBase: returned to blocked thread via a member var
70 * Context: contains a pointer to the right ControllerInfo
73 * KeepObject, because that's what the DDK says to do
76 PCONTROLLER_INFO ControllerInfo
= (PCONTROLLER_INFO
)Context
;
77 UNREFERENCED_PARAMETER(DeviceObject
);
78 UNREFERENCED_PARAMETER(Irp
);
80 TRACE_(FLOPPY
, "MapRegisterCallback Called\n");
82 ControllerInfo
->MapRegisterBase
= MapRegisterBase
;
83 KeSetEvent(&ControllerInfo
->SynchEvent
, 0, FALSE
);
90 ReadWrite(PDEVICE_OBJECT DeviceObject
, PIRP Irp
)
92 * FUNCTION: Dispatch routine called for read or write IRPs
95 * STATUS_PENDING if the IRP is queued
96 * STATUS_INVALID_PARAMETER if IRP is set up wrong
98 * - This function validates arguments to the IRP and then queues it
99 * - Note that this function is implicitly serialized by the queue logic. Only
100 * one of these at a time is active in the system, no matter how many processors
101 * and threads we have.
102 * - This function stores the DeviceObject in the IRP's context area before dropping
103 * it onto the irp queue
106 TRACE_(FLOPPY
, "ReadWrite called\n");
108 ASSERT(DeviceObject
);
113 WARN_(FLOPPY
, "ReadWrite(): MDL not found in IRP - Completing with STATUS_INVALID_PARAMETER\n");
114 Irp
->IoStatus
.Status
= STATUS_INVALID_PARAMETER
;
115 Irp
->IoStatus
.Information
= 0;
116 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
117 return STATUS_INVALID_PARAMETER
;
121 * Queue the irp to the thread.
122 * The de-queue thread will look in DriverContext[0] for the Device Object.
124 Irp
->Tail
.Overlay
.DriverContext
[0] = DeviceObject
;
125 IoCsqInsertIrp(&Csq
, Irp
, NULL
);
127 return STATUS_PENDING
;
132 RWFreeAdapterChannel(PADAPTER_OBJECT AdapterObject
)
134 * FUNCTION: Free the adapter DMA channel that we allocated
136 * AdapterObject: the object with the map registers to free
138 * - This function is primarily needed because IoFreeAdapterChannel wants to
139 * be called at DISPATCH_LEVEL
144 ASSERT(KeGetCurrentIrql() <= DISPATCH_LEVEL
);
146 KeRaiseIrql(DISPATCH_LEVEL
, &Irql
);
147 IoFreeAdapterChannel(AdapterObject
);
153 RWDetermineMediaType(PDRIVE_INFO DriveInfo
, BOOLEAN OneShot
)
155 * FUNCTION: Determine the media type of the disk in the drive and fill in the geometry
157 * DriveInfo: drive to look at
159 * STATUS_SUCCESS if the media was recognized and the geometry struct was filled in
160 * STATUS_UNRECOGNIZED_MEDIA if not
161 * STATUS_UNSUCCESSFUL if the controller can't be talked to
163 * - Expects the motor to already be running
164 * - Currently only supports 1.44MB 3.5" disks
165 * - PAGED_CODE because it waits
167 * - Support more disk types
171 UCHAR HeadUnloadTime
;
173 LARGE_INTEGER Timeout
;
177 TRACE_(FLOPPY
, "RWDetermineMediaType called\n");
180 * This algorithm assumes that a 1.44MB floppy is in the drive. If it's not,
181 * it works backwards until the read works unless OneShot try is asked.
182 * Note that only 1.44 has been tested at all.
185 Timeout
.QuadPart
= -10000000; /* 1 second. Is that enough? */
192 /* Program data rate */
193 if(HwSetDataRate(DriveInfo
->ControllerInfo
, DRSR_DSEL_500KBPS
) != STATUS_SUCCESS
)
195 WARN_(FLOPPY
, "RWDetermineMediaType(): unable to set data rate\n");
196 return STATUS_UNSUCCESSFUL
;
200 HeadLoadTime
= SPECIFY_HLT_500K
;
201 HeadUnloadTime
= SPECIFY_HUT_500K
;
202 StepRateTime
= SPECIFY_SRT_500K
;
204 /* Don't disable DMA --> enable dma (dumb & confusing) */
205 if(HwSpecify(DriveInfo
->ControllerInfo
, HeadLoadTime
, HeadUnloadTime
, StepRateTime
, FALSE
) != STATUS_SUCCESS
)
207 WARN_(FLOPPY
, "RWDetermineMediaType(): specify failed\n");
208 return STATUS_UNSUCCESSFUL
;
211 /* clear any spurious interrupts in preparation for recalibrate */
212 KeClearEvent(&DriveInfo
->ControllerInfo
->SynchEvent
);
214 /* Recalibrate --> head over first track */
217 NTSTATUS RecalStatus
;
219 if(HwRecalibrate(DriveInfo
) != STATUS_SUCCESS
)
221 WARN_(FLOPPY
, "RWDetermineMediaType(): Recalibrate failed\n");
222 return STATUS_UNSUCCESSFUL
;
225 /* Wait for the recalibrate to finish */
226 WaitForControllerInterrupt(DriveInfo
->ControllerInfo
, NULL
);
228 RecalStatus
= HwRecalibrateResult(DriveInfo
->ControllerInfo
);
230 if(RecalStatus
== STATUS_SUCCESS
)
233 if(i
== 1) /* failed for 2nd time */
235 WARN_(FLOPPY
, "RWDetermineMediaType(): RecalibrateResult failed\n");
236 return STATUS_UNSUCCESSFUL
;
240 /* clear any spurious interrupts */
241 KeClearEvent(&DriveInfo
->ControllerInfo
->SynchEvent
);
243 /* Try to read an ID */
244 if(HwReadId(DriveInfo
, 0) != STATUS_SUCCESS
) /* read the first ID we find, from head 0 */
246 WARN_(FLOPPY
, "RWDetermineMediaType(): ReadId failed\n");
247 return STATUS_UNSUCCESSFUL
; /* if we can't even write to the controller, it's hopeless */
250 /* Wait for the ReadID to finish */
251 Status
= WaitForControllerInterrupt(DriveInfo
->ControllerInfo
, &Timeout
);
253 if(Status
== STATUS_TIMEOUT
|| HwReadIdResult(DriveInfo
->ControllerInfo
, NULL
, NULL
) != STATUS_SUCCESS
)
255 WARN_(FLOPPY
, "RWDetermineMediaType(): ReadIdResult failed; continuing\n");
262 /* Found the media; populate the geometry now */
263 WARN_(FLOPPY
, "Hardcoded media type!\n");
264 INFO_(FLOPPY
, "RWDetermineMediaType(): Found 1.44 media; returning success\n");
265 DriveInfo
->DiskGeometry
.MediaType
= GEOMETRY_144_MEDIATYPE
;
266 DriveInfo
->DiskGeometry
.Cylinders
.QuadPart
= GEOMETRY_144_CYLINDERS
;
267 DriveInfo
->DiskGeometry
.TracksPerCylinder
= GEOMETRY_144_TRACKSPERCYLINDER
;
268 DriveInfo
->DiskGeometry
.SectorsPerTrack
= GEOMETRY_144_SECTORSPERTRACK
;
269 DriveInfo
->DiskGeometry
.BytesPerSector
= GEOMETRY_144_BYTESPERSECTOR
;
270 DriveInfo
->BytesPerSectorCode
= HW_512_BYTES_PER_SECTOR
;
271 return STATUS_SUCCESS
;
275 TRACE_(FLOPPY
, "RWDetermineMediaType(): failed to find media\n");
276 return STATUS_UNRECOGNIZED_MEDIA
;
280 static NTSTATUS NTAPI
281 RWSeekToCylinder(PDRIVE_INFO DriveInfo
, UCHAR Cylinder
)
283 * FUNCTION: Seek a particular drive to a particular track
285 * DriveInfo: Drive to seek
286 * Cylinder: track to seek to
288 * STATUS_SUCCESS if the head was successfully seeked
289 * STATUS_UNSUCCESSFUL if not
291 * - PAGED_CODE because it blocks
298 TRACE_(FLOPPY
, "RWSeekToCylinder called drive 0x%p cylinder %d\n", DriveInfo
, Cylinder
);
300 /* Clear any spurious interrupts */
301 KeClearEvent(&DriveInfo
->ControllerInfo
->SynchEvent
);
303 /* queue seek command */
304 if(HwSeek(DriveInfo
, Cylinder
) != STATUS_SUCCESS
)
306 WARN_(FLOPPY
, "RWSeekToTrack(): unable to seek\n");
307 return STATUS_UNSUCCESSFUL
;
310 WaitForControllerInterrupt(DriveInfo
->ControllerInfo
, NULL
);
312 if(HwSenseInterruptStatus(DriveInfo
->ControllerInfo
) != STATUS_SUCCESS
)
314 WARN_(FLOPPY
, "RWSeekToTrack(): unable to get seek results\n");
315 return STATUS_UNSUCCESSFUL
;
318 /* read ID mark from head 0 to verify */
319 if(HwReadId(DriveInfo
, 0) != STATUS_SUCCESS
)
321 WARN_(FLOPPY
, "RWSeekToTrack(): unable to queue ReadId\n");
322 return STATUS_UNSUCCESSFUL
;
325 WaitForControllerInterrupt(DriveInfo
->ControllerInfo
, NULL
);
327 if(HwReadIdResult(DriveInfo
->ControllerInfo
, &CurCylinder
, NULL
) != STATUS_SUCCESS
)
329 WARN_(FLOPPY
, "RWSeekToTrack(): unable to get ReadId result\n");
330 return STATUS_UNSUCCESSFUL
;
333 if(CurCylinder
!= Cylinder
)
335 WARN_(FLOPPY
, "RWSeekToTrack(): Seek to track failed; current cylinder is 0x%x\n", CurCylinder
);
336 return STATUS_UNSUCCESSFUL
;
339 INFO_(FLOPPY
, "RWSeekToCylinder: returning successfully, now on cyl %d\n", Cylinder
);
341 return STATUS_SUCCESS
;
345 static NTSTATUS NTAPI
346 RWComputeCHS(PDRIVE_INFO IN DriveInfo
,
347 ULONG IN DiskByteOffset
,
352 * FUNCTION: Compute the CHS from the absolute byte offset on disk
354 * DriveInfo: Drive to compute on
355 * DiskByteOffset: Absolute offset on disk of the starting byte
356 * Cylinder: Cylinder that the byte is on
357 * Head: Head that the byte is on
358 * Sector: Sector that the byte is on
360 * STATUS_SUCCESS if CHS are determined correctly
361 * STATUS_UNSUCCESSFUL otherwise
363 * - Lots of ugly typecasts here
364 * - Sectors are 1-based!
365 * - This is really crummy code. Please FIXME.
368 ULONG AbsoluteSector
;
369 UCHAR SectorsPerCylinder
= (UCHAR
)DriveInfo
->DiskGeometry
.SectorsPerTrack
* (UCHAR
)DriveInfo
->DiskGeometry
.TracksPerCylinder
;
371 TRACE_(FLOPPY
, "RWComputeCHS: Called with offset 0x%x\n", DiskByteOffset
);
373 /* First calculate the 1-based "absolute sector" based on the byte offset */
374 ASSERT(!(DiskByteOffset
% DriveInfo
->DiskGeometry
.BytesPerSector
)); /* FIXME: Only handle full sector transfers atm */
376 /* AbsoluteSector is zero-based to make the math a little easier */
377 AbsoluteSector
= DiskByteOffset
/ DriveInfo
->DiskGeometry
.BytesPerSector
; /* Num full sectors */
379 /* Cylinder number is floor(AbsoluteSector / SectorsPerCylinder) */
380 *Cylinder
= (CHAR
)(AbsoluteSector
/ SectorsPerCylinder
);
382 /* Head number is 0 if the sector within the cylinder < SectorsPerTrack; 1 otherwise */
383 *Head
= AbsoluteSector
% SectorsPerCylinder
< DriveInfo
->DiskGeometry
.SectorsPerTrack
? 0 : 1;
386 * Sector number is the sector within the cylinder if on head 0; that minus SectorsPerTrack if it's on head 1
387 * (lots of casts to placate msvc). 1-based!
389 *Sector
= ((UCHAR
)(AbsoluteSector
% SectorsPerCylinder
) + 1) - ((*Head
) * (UCHAR
)DriveInfo
->DiskGeometry
.SectorsPerTrack
);
391 INFO_(FLOPPY
, "RWComputeCHS: offset 0x%x is c:0x%x h:0x%x s:0x%x\n", DiskByteOffset
, *Cylinder
, *Head
, *Sector
);
393 /* Sanity checking */
394 ASSERT(*Cylinder
<= DriveInfo
->DiskGeometry
.Cylinders
.QuadPart
);
395 ASSERT(*Head
<= DriveInfo
->DiskGeometry
.TracksPerCylinder
);
396 ASSERT(*Sector
<= DriveInfo
->DiskGeometry
.SectorsPerTrack
);
398 return STATUS_SUCCESS
;
403 ReadWritePassive(PDRIVE_INFO DriveInfo
, PIRP Irp
)
405 * FUNCTION: Handle the first phase of a read or write IRP
407 * DeviceObject: DeviceObject that is the target of the IRP
408 * Irp: IRP to process
410 * STATUS_VERIFY_REQUIRED if the media has changed and we need the filesystems to re-synch
411 * STATUS_SUCCESS otherwise
413 * - Must be called at PASSIVE_LEVEL
414 * - This function is about 250 lines longer than I wanted it to be. Sorry.
417 * This routine manages the whole process of servicing a read or write request. It goes like this:
418 * 1) Check the DO_VERIFY_VOLUME flag and return if it's set
419 * 2) Check the disk change line and notify the OS if it's set and return
420 * 3) Detect the media if we haven't already
421 * 4) Set up DiskByteOffset, Length, and WriteToDevice parameters
422 * 5) Get DMA map registers
423 * 6) Then, in a loop for each track, until all bytes are transferred:
424 * a) Compute the current CHS to set the read/write head to
425 * b) Seek to that spot
426 * c) Compute the last sector to transfer on that track
427 * d) Map the transfer through DMA
428 * e) Send the read or write command to the controller
429 * f) Read the results of the command
432 PDEVICE_OBJECT DeviceObject
= DriveInfo
->DeviceObject
;
433 PIO_STACK_LOCATION Stack
= IoGetCurrentIrpStackLocation(Irp
);
434 BOOLEAN WriteToDevice
;
436 ULONG DiskByteOffset
;
440 ULONG_PTR TransferByteOffset
;
445 TRACE_(FLOPPY
, "ReadWritePassive called to %s 0x%x bytes from offset 0x%x\n",
446 (Stack
->MajorFunction
== IRP_MJ_READ
? "read" : "write"),
447 (Stack
->MajorFunction
== IRP_MJ_READ
? Stack
->Parameters
.Read
.Length
: Stack
->Parameters
.Write
.Length
),
448 (Stack
->MajorFunction
== IRP_MJ_READ
? Stack
->Parameters
.Read
.ByteOffset
.u
.LowPart
:
449 Stack
->Parameters
.Write
.ByteOffset
.u
.LowPart
));
451 /* Default return codes */
452 Irp
->IoStatus
.Status
= STATUS_UNSUCCESSFUL
;
453 Irp
->IoStatus
.Information
= 0;
456 * Check to see if the volume needs to be verified. If so,
457 * we can get out of here quickly.
459 if(DeviceObject
->Flags
& DO_VERIFY_VOLUME
&& !(Stack
->Flags
& SL_OVERRIDE_VERIFY_VOLUME
))
461 INFO_(FLOPPY
, "ReadWritePassive(): DO_VERIFY_VOLUME set; Completing with STATUS_VERIFY_REQUIRED\n");
462 Irp
->IoStatus
.Status
= STATUS_VERIFY_REQUIRED
;
463 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
468 * Check the change line, and if it's set, return
470 StartMotor(DriveInfo
);
471 if(HwDiskChanged(DeviceObject
->DeviceExtension
, &DiskChanged
) != STATUS_SUCCESS
)
473 WARN_(FLOPPY
, "ReadWritePassive(): unable to detect disk change; Completing with STATUS_UNSUCCESSFUL\n");
474 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
475 StopMotor(DriveInfo
->ControllerInfo
);
481 INFO_(FLOPPY
, "ReadWritePhase1(): signalling media changed; Completing with STATUS_MEDIA_CHANGED\n");
483 /* The following call sets IoStatus.Status and IoStatus.Information */
484 SignalMediaChanged(DeviceObject
, Irp
);
487 * Guessing at something... see ioctl.c for more info
489 if(ResetChangeFlag(DriveInfo
) == STATUS_NO_MEDIA_IN_DEVICE
)
490 Irp
->IoStatus
.Status
= STATUS_NO_MEDIA_IN_DEVICE
;
492 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
493 StopMotor(DriveInfo
->ControllerInfo
);
498 * Figure out the media type, if we don't know it already
500 if(DriveInfo
->DiskGeometry
.MediaType
== Unknown
)
502 if(RWDetermineMediaType(DriveInfo
, FALSE
) != STATUS_SUCCESS
)
504 WARN_(FLOPPY
, "ReadWritePassive(): unable to determine media type; completing with STATUS_UNSUCCESSFUL\n");
505 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
506 StopMotor(DriveInfo
->ControllerInfo
);
510 if(DriveInfo
->DiskGeometry
.MediaType
== Unknown
)
512 WARN_(FLOPPY
, "ReadWritePassive(): Unknown media in drive; completing with STATUS_UNRECOGNIZED_MEDIA\n");
513 Irp
->IoStatus
.Status
= STATUS_UNRECOGNIZED_MEDIA
;
514 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
515 StopMotor(DriveInfo
->ControllerInfo
);
520 /* Set up parameters for read or write */
521 if(Stack
->MajorFunction
== IRP_MJ_READ
)
523 Length
= Stack
->Parameters
.Read
.Length
;
524 DiskByteOffset
= Stack
->Parameters
.Read
.ByteOffset
.u
.LowPart
;
525 WriteToDevice
= FALSE
;
529 Length
= Stack
->Parameters
.Write
.Length
;
530 DiskByteOffset
= Stack
->Parameters
.Write
.ByteOffset
.u
.LowPart
;
531 WriteToDevice
= TRUE
;
536 * FloppyDeviceData.ReadWriteGapLength specify the value for the physical drive.
537 * We should set this value depend on the format of the inserted disk and possible
538 * depend on the request (read or write). A value of 0 results in one rotation
539 * between the sectors (7.2sec for reading a track).
541 Gap
= DriveInfo
->FloppyDeviceData
.ReadWriteGapLength
;
544 * Set up DMA transfer
546 * This is as good of a place as any to document something that used to confuse me
547 * greatly (and I even wrote some of the kernel's DMA code, so if it confuses me, it
548 * probably confuses at least a couple of other people too).
550 * MmGetMdlVirtualAddress() returns the virtual address, as mapped in the buffer's original
551 * process context, of the MDL. In other words: say you start with a buffer at address X, then
552 * you build an MDL out of that buffer called Mdl. If you call MmGetMdlVirtualAddress(Mdl), it
555 * There are two parameters that the function looks at to produce X again, given the MDL: the
556 * first is the StartVa, which is the base virtual address of the page that the buffer starts
557 * in. If your buffer's virtual address is 0x12345678, StartVa will be 0x12345000, assuming 4K pages
558 * (which is (almost) always the case on x86). Note well: this address is only valid in the
559 * process context that you initially built the MDL from. The physical pages that make up
560 * the MDL might perhaps be mapped in other process contexts too (or even in the system space,
561 * above 0x80000000 (default; 0xc0000000 on current ReactOS or /3GB Windows)), but it will
562 * (possibly) be mapped at a different address.
564 * The second parameter is the ByteOffset. Given an original buffer address of 0x12345678,
565 * the ByteOffset would be 0x678. Because MDLs can only describe full pages (and therefore
566 * StartVa always points to the start address of a page), the ByteOffset must be used to
567 * find the real start of the buffer.
569 * In general, if you add the StartVa and ByteOffset together, you get back your original
570 * buffer pointer, which you are free to use if you're sure you're in the right process
571 * context. You could tell by accessing the (hidden and not-to-be-used) Process member of
572 * the MDL, but in general, if you have to ask whether or not you are in the right context,
573 * then you shouldn't be using this address for anything anyway. There are also security implications
574 * (big ones, really, I wouldn't kid about this) to directly accessing a user's buffer by VA, so
577 * There is a somewhat weird but very common use of the virtual address associated with a MDL
578 * that pops up often in the context of DMA. DMA APIs (particularly MapTransfer()) need to
579 * know where the memory is that they should DMA into and out of. This memory is described
580 * by a MDL. The controller eventually needs to know a physical address on the host side,
581 * which is generally a 32-bit linear address (on x86), and not just a page address. Therefore,
582 * the DMA APIs look at the ByteOffset field of the MDL to reconstruct the real address that
583 * should be programmed into the DMA controller.
585 * It is often the case that a transfer needs to be broken down over more than one DMA operation,
586 * particularly when it is a big transfer and the HAL doesn't give you enough map registers
587 * to map the whole thing at once. Therefore, the APIs need a way to tell how far into the MDL
588 * they should look to transfer the next chunk of bytes. Now, Microsoft could have designed
589 * MapTransfer to take a "MDL offset" argument, starting with 0, for how far into the buffer to
590 * start, but it didn't. Instead, MapTransfer asks for the virtual address of the MDL as an "index" into
591 * the MDL. The way it computes how far into the page to start the transfer is by masking off all but
592 * the bottom 12 bits (on x86) of the number you supply as the CurrentVa and using *that* as the
593 * ByteOffset instead of the one in the MDL. (OK, this varies a bit by OS and version, but this
596 * In other words, you get a number back from MmGetMdlVirtualAddress that represents the start of your
597 * buffer, and you pass it to the first MapTransfer call. Then, for each successive operation
598 * on the same buffer, you increment that address to point to the next spot in the MDL that
599 * you want to DMA to/from. The fact that the virtual address you're manipulating is probably not
600 * mapped into the process context that you're running in is irrelevant, since it's only being
601 * used to index into the MDL.
604 /* Get map registers for DMA */
605 KeRaiseIrql(DISPATCH_LEVEL
, &OldIrql
);
606 Status
= IoAllocateAdapterChannel(DriveInfo
->ControllerInfo
->AdapterObject
, DeviceObject
,
607 DriveInfo
->ControllerInfo
->MapRegisters
, MapRegisterCallback
, DriveInfo
->ControllerInfo
);
608 KeLowerIrql(OldIrql
);
610 if(Status
!= STATUS_SUCCESS
)
612 WARN_(FLOPPY
, "ReadWritePassive(): unable allocate an adapter channel; completing with STATUS_UNSUCCESSFUL\n");
613 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
614 StopMotor(DriveInfo
->ControllerInfo
);
620 * Read from (or write to) the device
622 * This has to be called in a loop, as you can only transfer data to/from a single track at
625 TransferByteOffset
= 0;
626 while(TransferByteOffset
< Length
)
631 ULONG CurrentTransferBytes
;
632 UCHAR CurrentTransferSectors
;
634 INFO_(FLOPPY
, "ReadWritePassive(): iterating in while (TransferByteOffset = 0x%x of 0x%x total) - allocating %d registers\n",
635 TransferByteOffset
, Length
, DriveInfo
->ControllerInfo
->MapRegisters
);
637 KeClearEvent(&DriveInfo
->ControllerInfo
->SynchEvent
);
640 * Compute starting CHS
642 if(RWComputeCHS(DriveInfo
, DiskByteOffset
+TransferByteOffset
, &Cylinder
, &Head
, &StartSector
) != STATUS_SUCCESS
)
644 WARN_(FLOPPY
, "ReadWritePassive(): unable to compute CHS; completing with STATUS_UNSUCCESSFUL\n");
645 RWFreeAdapterChannel(DriveInfo
->ControllerInfo
->AdapterObject
);
646 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
647 StopMotor(DriveInfo
->ControllerInfo
);
652 * Seek to the right track
654 if(!DriveInfo
->ControllerInfo
->ImpliedSeeks
)
656 if(RWSeekToCylinder(DriveInfo
, Cylinder
) != STATUS_SUCCESS
)
658 WARN_(FLOPPY
, "ReadWritePassive(): unable to seek; completing with STATUS_UNSUCCESSFUL\n");
659 RWFreeAdapterChannel(DriveInfo
->ControllerInfo
->AdapterObject
);
660 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
661 StopMotor(DriveInfo
->ControllerInfo
);
667 * Compute last sector
669 * We can only ask for a transfer up to the end of the track. Then we have to re-seek and do more.
670 * TODO: Support the MT bit
672 INFO_(FLOPPY
, "ReadWritePassive(): computing number of sectors to transfer (StartSector 0x%x): ", StartSector
);
674 /* 1-based sector number */
675 if( (((DriveInfo
->DiskGeometry
.TracksPerCylinder
- Head
) * DriveInfo
->DiskGeometry
.SectorsPerTrack
- StartSector
) + 1 ) <
676 (Length
- TransferByteOffset
) / DriveInfo
->DiskGeometry
.BytesPerSector
)
678 CurrentTransferSectors
= (UCHAR
)((DriveInfo
->DiskGeometry
.TracksPerCylinder
- Head
) * DriveInfo
->DiskGeometry
.SectorsPerTrack
- StartSector
) + 1;
682 CurrentTransferSectors
= (UCHAR
)((Length
- TransferByteOffset
) / DriveInfo
->DiskGeometry
.BytesPerSector
);
685 INFO_(FLOPPY
, "0x%x\n", CurrentTransferSectors
);
687 CurrentTransferBytes
= CurrentTransferSectors
* DriveInfo
->DiskGeometry
.BytesPerSector
;
690 * Adjust to map registers
691 * BUG: Does this take into account page crossings?
693 INFO_(FLOPPY
, "ReadWritePassive(): Trying to transfer 0x%x bytes\n", CurrentTransferBytes
);
695 ASSERT(CurrentTransferBytes
);
697 if(BYTES_TO_PAGES(CurrentTransferBytes
) > DriveInfo
->ControllerInfo
->MapRegisters
)
699 CurrentTransferSectors
= (UCHAR
)((DriveInfo
->ControllerInfo
->MapRegisters
* PAGE_SIZE
) /
700 DriveInfo
->DiskGeometry
.BytesPerSector
);
702 CurrentTransferBytes
= CurrentTransferSectors
* DriveInfo
->DiskGeometry
.BytesPerSector
;
704 INFO_(FLOPPY
, "ReadWritePassive: limiting transfer to 0x%x bytes (0x%x sectors) due to map registers\n",
705 CurrentTransferBytes
, CurrentTransferSectors
);
708 /* set up this round's dma operation */
709 /* param 2 is ReadOperation --> opposite of WriteToDevice that IoMapTransfer takes. BAD MS. */
710 KeFlushIoBuffers(Irp
->MdlAddress
, !WriteToDevice
, TRUE
);
712 IoMapTransfer(DriveInfo
->ControllerInfo
->AdapterObject
, Irp
->MdlAddress
,
713 DriveInfo
->ControllerInfo
->MapRegisterBase
,
714 (PVOID
)((ULONG_PTR
)MmGetMdlVirtualAddress(Irp
->MdlAddress
) + TransferByteOffset
),
715 &CurrentTransferBytes
, WriteToDevice
);
720 KeClearEvent(&DriveInfo
->ControllerInfo
->SynchEvent
);
722 /* Issue the read/write command to the controller. Note that it expects the opposite of WriteToDevice. */
723 if(HwReadWriteData(DriveInfo
->ControllerInfo
, !WriteToDevice
, DriveInfo
->UnitNumber
, Cylinder
, Head
, StartSector
,
724 DriveInfo
->BytesPerSectorCode
, DriveInfo
->DiskGeometry
.SectorsPerTrack
, Gap
, 0xff) != STATUS_SUCCESS
)
726 WARN_(FLOPPY
, "ReadWritePassive(): HwReadWriteData returned failure; unable to read; completing with STATUS_UNSUCCESSFUL\n");
727 RWFreeAdapterChannel(DriveInfo
->ControllerInfo
->AdapterObject
);
728 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
729 StopMotor(DriveInfo
->ControllerInfo
);
733 INFO_(FLOPPY
, "ReadWritePassive(): HwReadWriteData returned -- waiting on event\n");
736 * At this point, we block and wait for an interrupt
737 * FIXME: this seems to take too long
739 WaitForControllerInterrupt(DriveInfo
->ControllerInfo
, NULL
);
741 /* Read is complete; flush & free adapter channel */
742 IoFlushAdapterBuffers(DriveInfo
->ControllerInfo
->AdapterObject
, Irp
->MdlAddress
,
743 DriveInfo
->ControllerInfo
->MapRegisterBase
,
744 (PVOID
)((ULONG_PTR
)MmGetMdlVirtualAddress(Irp
->MdlAddress
) + TransferByteOffset
),
745 CurrentTransferBytes
, WriteToDevice
);
747 /* Read the results from the drive */
748 if(HwReadWriteResult(DriveInfo
->ControllerInfo
) != STATUS_SUCCESS
)
750 WARN_(FLOPPY
, "ReadWritePassive(): HwReadWriteResult returned failure; unable to read; completing with STATUS_UNSUCCESSFUL\n");
751 HwDumpRegisters(DriveInfo
->ControllerInfo
);
752 RWFreeAdapterChannel(DriveInfo
->ControllerInfo
->AdapterObject
);
753 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
754 StopMotor(DriveInfo
->ControllerInfo
);
758 TransferByteOffset
+= CurrentTransferBytes
;
761 RWFreeAdapterChannel(DriveInfo
->ControllerInfo
->AdapterObject
);
763 /* That's all folks! */
764 INFO_(FLOPPY
, "ReadWritePassive(): success; Completing with STATUS_SUCCESS\n");
765 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
766 Irp
->IoStatus
.Information
= Length
;
767 IoCompleteRequest(Irp
, IO_DISK_INCREMENT
);
768 StopMotor(DriveInfo
->ControllerInfo
);