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: Main floppy driver routines
22 * PROGRAMMER: Vizzini (vizzini@plasmic.com)
24 * 15-Feb-2004 vizzini - Created
26 * - This driver is only designed to work with ISA-bus floppy controllers. This
27 * won't work on PCI-based controllers or on anything else with level-sensitive
28 * interrupts without modification. I don't think these controllers exist.
30 * ---- General to-do items ----
31 * TODO: Figure out why CreateClose isn't called any more. Seems to correspond
32 * with the driver not being unloadable.
33 * TODO: Think about StopDpcQueued -- could be a race; too tired atm to tell
34 * TODO: Clean up drive start/stop responsibilities (currently a mess...)
36 * ---- Support for proper media detection ----
37 * TODO: Handle MFM flag
38 * TODO: Un-hardcode the data rate from various places
39 * TODO: Proper media detection (right now we're hardcoded to 1.44)
40 * TODO: Media detection based on sector 1
49 #include "readwrite.h"
52 * Global controller info structures. Each controller gets one. Since the system
53 * will probably have only one, with four being a very unlikely maximum, a static
54 * global array is easiest to deal with.
56 static CONTROLLER_INFO gControllerInfo
[MAX_CONTROLLERS
];
57 static ULONG gNumberOfControllers
= 0;
59 /* Queue thread management */
60 static KEVENT QueueThreadTerminate
;
61 static PVOID QueueThreadObject
;
65 MotorStopDpcFunc(PKDPC UnusedDpc
, PVOID DeferredContext
, PVOID SystemArgument1
, PVOID SystemArgument2
)
67 * FUNCTION: Stop the floppy motor
69 * UnusedDpc: DPC object that's going off
70 * DeferredContext: called with DRIVE_INFO for drive to turn off
71 * SystemArgument1: unused
72 * SystemArgument2: unused
74 * - Must set an event to let other threads know we're done turning off the motor
75 * - Called back at DISPATCH_LEVEL
78 PCONTROLLER_INFO ControllerInfo
= (PCONTROLLER_INFO
)DeferredContext
;
80 UNREFERENCED_PARAMETER(SystemArgument1
);
81 UNREFERENCED_PARAMETER(SystemArgument2
);
82 UNREFERENCED_PARAMETER(UnusedDpc
);
84 ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL
);
85 ASSERT(ControllerInfo
);
87 TRACE_(FLOPPY
, "MotorStopDpcFunc called\n");
89 HwTurnOffMotor(ControllerInfo
);
90 ControllerInfo
->StopDpcQueued
= FALSE
;
91 KeSetEvent(&ControllerInfo
->MotorStoppedEvent
, EVENT_INCREMENT
, FALSE
);
96 StartMotor(PDRIVE_INFO DriveInfo
)
98 * FUNCTION: Start the motor, taking into account proper handling of the timer race
100 * DriveInfo: drive to start
102 * - Never call HwTurnOnMotor() directly
103 * - This protocol manages a race between the cancel timer and the requesting thread.
104 * You wouldn't want to turn on the motor and then cancel the timer, because the
105 * cancel dpc might fire in the meantime, and that'd un-do what you just did. If you
106 * cancel the timer first, but KeCancelTimer returns false, the dpc is already running,
107 * so you have to wait until the dpc is completely done running, or else you'll race
108 * with the turner-offer
109 * - PAGED_CODE because we wait
115 TRACE_(FLOPPY
, "StartMotor called\n");
117 if(DriveInfo
->ControllerInfo
->StopDpcQueued
&& !KeCancelTimer(&DriveInfo
->ControllerInfo
->MotorTimer
))
119 /* Motor turner-offer is already running; wait for it to finish */
120 INFO_(FLOPPY
, "StartMotor: motor turner-offer is already running; waiting for it\n");
121 KeWaitForSingleObject(&DriveInfo
->ControllerInfo
->MotorStoppedEvent
, Executive
, KernelMode
, FALSE
, NULL
);
122 INFO_(FLOPPY
, "StartMotor: wait satisfied\n");
125 DriveInfo
->ControllerInfo
->StopDpcQueued
= FALSE
;
127 if(HwTurnOnMotor(DriveInfo
) != STATUS_SUCCESS
)
129 WARN_(FLOPPY
, "StartMotor(): warning: HwTurnOnMotor failed\n");
135 StopMotor(PCONTROLLER_INFO ControllerInfo
)
137 * FUNCTION: Stop all motors on the controller
139 * DriveInfo: Drive to stop
141 * - Never call HwTurnOffMotor() directly
142 * - This manages the timer cancelation race (see StartMotor for details).
143 * All we have to do is set up a timer.
146 LARGE_INTEGER StopTime
;
148 ASSERT(ControllerInfo
);
150 TRACE_(FLOPPY
, "StopMotor called\n");
152 /* one relative second, in 100-ns units */
153 StopTime
.QuadPart
= 10000000;
154 StopTime
.QuadPart
*= -1;
156 KeClearEvent(&ControllerInfo
->MotorStoppedEvent
);
157 KeSetTimer(&ControllerInfo
->MotorTimer
, StopTime
, &ControllerInfo
->MotorStopDpc
);
158 ControllerInfo
->StopDpcQueued
= TRUE
;
163 WaitForControllerInterrupt(PCONTROLLER_INFO ControllerInfo
, PLARGE_INTEGER Timeout
)
165 * FUNCTION: Wait for the controller to interrupt, and then clear the event
167 * ControllerInfo: Controller to wait for
168 * Timeout: How long to wait for
170 * - There is a small chance that an unexpected or spurious interrupt could
171 * be lost with this clear/wait/clear scheme used in this driver. This is
172 * deemed to be an acceptable risk due to the unlikeliness of the scenario,
173 * and the fact that it'll probably work fine next time.
174 * - PAGED_CODE because it waits
180 ASSERT(ControllerInfo
);
182 Status
= KeWaitForSingleObject(&ControllerInfo
->SynchEvent
, Executive
, KernelMode
, FALSE
, Timeout
);
183 KeClearEvent(&ControllerInfo
->SynchEvent
);
188 static DRIVER_DISPATCH CreateClose
;
189 static NTSTATUS NTAPI
CreateClose(PDEVICE_OBJECT DeviceObject
,
192 * FUNCTION: Dispatch function called for Create and Close IRPs
194 * DeviceObject: DeviceObject that is the target of the IRP
195 * Irp: IRP to process
197 * STATUS_SUCCESS in all cases
199 * - The Microsoft sample drivers tend to return FILE_OPENED in Information, so I do too.
200 * - No reason to fail the device open
201 * - No state to track, so this routine is easy
202 * - Can be called <= DISPATCH_LEVEL
204 * TODO: Figure out why this isn't getting called
207 UNREFERENCED_PARAMETER(DeviceObject
);
209 TRACE_(FLOPPY
, "CreateClose called\n");
211 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
212 Irp
->IoStatus
.Information
= FILE_OPENED
;
214 IoCompleteRequest(Irp
, IO_DISK_INCREMENT
);
216 return STATUS_SUCCESS
;
220 static NTSTATUS NTAPI
221 Recalibrate(PDRIVE_INFO DriveInfo
)
223 * FUNCTION: Start the recalibration process
225 * DriveInfo: Pointer to the driveinfo struct associated with the targeted drive
227 * STATUS_SUCCESS on successful starting of the process
228 * STATUS_IO_DEVICE_ERROR if it fails
230 * - Sometimes you have to do two recalibrations, particularly if the disk has <80 tracks.
231 * - PAGED_CODE because we wait
240 /* first turn on the motor */
241 /* Must stop after every start, prior to return */
242 StartMotor(DriveInfo
);
244 /* set the data rate */
245 WARN_(FLOPPY
, "FIXME: UN-HARDCODE DATA RATE\n");
246 if(HwSetDataRate(DriveInfo
->ControllerInfo
, 0) != STATUS_SUCCESS
)
248 WARN_(FLOPPY
, "Recalibrate: HwSetDataRate failed\n");
249 StopMotor(DriveInfo
->ControllerInfo
);
250 return STATUS_IO_DEVICE_ERROR
;
253 /* clear the event just in case the last call forgot */
254 KeClearEvent(&DriveInfo
->ControllerInfo
->SynchEvent
);
256 /* sometimes you have to do this twice; we'll just do it twice all the time since
257 * we don't know if the people calling this Recalibrate routine expect a disk to
258 * even be in the drive, and if so, if that disk is formatted.
260 for(i
= 0; i
< 2; i
++)
262 /* Send the command */
263 Status
= HwRecalibrate(DriveInfo
);
264 if(Status
!= STATUS_SUCCESS
)
266 WARN_(FLOPPY
, "Recalibrate: HwRecalibrate returned error\n");
270 WaitForControllerInterrupt(DriveInfo
->ControllerInfo
, NULL
);
272 /* Get the results */
273 Status
= HwRecalibrateResult(DriveInfo
->ControllerInfo
);
274 if(Status
!= STATUS_SUCCESS
)
276 WARN_(FLOPPY
, "Recalibrate: HwRecalibrateResult returned error\n");
281 KeClearEvent(&DriveInfo
->ControllerInfo
->SynchEvent
);
283 /* Must stop after every start, prior to return */
284 StopMotor(DriveInfo
->ControllerInfo
);
291 ResetChangeFlag(PDRIVE_INFO DriveInfo
)
293 * FUNCTION: Reset the drive's change flag (as reflected in the DIR)
295 * DriveInfo: the drive to reset
297 * STATUS_SUCCESS if the changeline is cleared
298 * STATUS_NO_MEDIA_IN_DEVICE if the changeline cannot be cleared
299 * STATUS_IO_DEVICE_ERROR if the controller cannot be communicated with
301 * - Change reset procedure: recalibrate, seek 1, seek 0
302 * - If the line is still set after that, there's clearly no disk in the
303 * drive, so we return STATUS_NO_MEDIA_IN_DEVICE
304 * - PAGED_CODE because we wait
312 TRACE_(FLOPPY
, "ResetChangeFlag called\n");
314 /* Try to recalibrate. We don't care if it works. */
315 Recalibrate(DriveInfo
);
317 /* clear spurious interrupts in prep for seeks */
318 KeClearEvent(&DriveInfo
->ControllerInfo
->SynchEvent
);
320 /* must re-start the drive because Recalibrate() stops it */
321 StartMotor(DriveInfo
);
324 if(HwSeek(DriveInfo
, 1) != STATUS_SUCCESS
)
326 WARN_(FLOPPY
, "ResetChangeFlag(): HwSeek failed; returning STATUS_IO_DEVICE_ERROR\n");
327 StopMotor(DriveInfo
->ControllerInfo
);
328 return STATUS_IO_DEVICE_ERROR
;
331 WaitForControllerInterrupt(DriveInfo
->ControllerInfo
, NULL
);
333 if(HwSenseInterruptStatus(DriveInfo
->ControllerInfo
) != STATUS_SUCCESS
)
335 WARN_(FLOPPY
, "ResetChangeFlag(): HwSenseInterruptStatus failed; bailing out\n");
336 StopMotor(DriveInfo
->ControllerInfo
);
337 return STATUS_IO_DEVICE_ERROR
;
341 if(HwSeek(DriveInfo
, 0) != STATUS_SUCCESS
)
343 WARN_(FLOPPY
, "ResetChangeFlag(): HwSeek failed; returning STATUS_IO_DEVICE_ERROR\n");
344 StopMotor(DriveInfo
->ControllerInfo
);
345 return STATUS_IO_DEVICE_ERROR
;
348 WaitForControllerInterrupt(DriveInfo
->ControllerInfo
, NULL
);
350 if(HwSenseInterruptStatus(DriveInfo
->ControllerInfo
) != STATUS_SUCCESS
)
352 WARN_(FLOPPY
, "ResetChangeFlag(): HwSenseInterruptStatus #2 failed; bailing\n");
353 StopMotor(DriveInfo
->ControllerInfo
);
354 return STATUS_IO_DEVICE_ERROR
;
357 /* Check the change bit */
358 if(HwDiskChanged(DriveInfo
, &DiskChanged
) != STATUS_SUCCESS
)
360 WARN_(FLOPPY
, "ResetChangeFlag(): HwDiskChanged failed; returning STATUS_IO_DEVICE_ERROR\n");
361 StopMotor(DriveInfo
->ControllerInfo
);
362 return STATUS_IO_DEVICE_ERROR
;
365 StopMotor(DriveInfo
->ControllerInfo
);
367 /* if the change flag is still set, there's probably no media in the drive. */
369 return STATUS_NO_MEDIA_IN_DEVICE
;
371 /* else we're done! */
372 return STATUS_SUCCESS
;
377 Unload(PDRIVER_OBJECT DriverObject
)
379 * FUNCTION: Unload the driver from memory
381 * DriverObject - The driver that is being unloaded
387 UNREFERENCED_PARAMETER(DriverObject
);
389 TRACE_(FLOPPY
, "unloading\n");
391 KeSetEvent(&QueueThreadTerminate
, 0, FALSE
);
392 KeWaitForSingleObject(QueueThreadObject
, Executive
, KernelMode
, FALSE
, 0);
393 ObDereferenceObject(QueueThreadObject
);
395 for(i
= 0; i
< gNumberOfControllers
; i
++)
397 if(!gControllerInfo
[i
].Initialized
)
400 for(j
= 0; j
< gControllerInfo
[i
].NumberOfDrives
; j
++)
402 if(!gControllerInfo
[i
].DriveInfo
[j
].Initialized
)
405 if(gControllerInfo
[i
].DriveInfo
[j
].DeviceObject
)
409 RtlInitUnicodeString(&Link
, gControllerInfo
[i
].DriveInfo
[j
].SymLinkBuffer
);
410 IoDeleteSymbolicLink(&Link
);
412 RtlInitUnicodeString(&Link
, gControllerInfo
[i
].DriveInfo
[j
].ArcPathBuffer
);
413 IoDeassignArcName(&Link
);
415 IoDeleteDevice(gControllerInfo
[i
].DriveInfo
[j
].DeviceObject
);
419 IoDisconnectInterrupt(gControllerInfo
[i
].InterruptObject
);
421 /* Power down the controller */
422 if(HwPowerOff(&gControllerInfo
[i
]) != STATUS_SUCCESS
)
424 WARN_(FLOPPY
, "unload: warning: HwPowerOff failed\n");
430 static NTSTATUS NTAPI
431 ConfigCallback(PVOID Context
,
432 PUNICODE_STRING PathName
,
433 INTERFACE_TYPE BusType
,
435 PKEY_VALUE_FULL_INFORMATION
*BusInformation
,
436 CONFIGURATION_TYPE ControllerType
,
437 ULONG ControllerNumber
,
438 PKEY_VALUE_FULL_INFORMATION
*ControllerInformation
,
439 CONFIGURATION_TYPE PeripheralType
,
440 ULONG PeripheralNumber
,
441 PKEY_VALUE_FULL_INFORMATION
*PeripheralInformation
)
443 * FUNCTION: Callback to IoQueryDeviceDescription, which tells us about our controllers
447 * BusType: Type of the bus that our controller is on
448 * BusNumber: Number of the bus that our controller is on
449 * BusInformation: Unused
450 * ControllerType: Unused
451 * ControllerNumber: Number of the controller that we're adding
452 * ControllerInformation: Full configuration information for our controller
453 * PeripheralType: Unused
454 * PeripheralNumber: Unused
455 * PeripheralInformation: Full configuration information for each drive on our controller
457 * STATUS_SUCCESS in all cases
459 * - The only documentation I've found about the contents of these structures is
460 * from the various Microsoft floppy samples and from the DDK headers. They're
461 * very vague, though, so I'm only mostly sure that this stuff is correct, as
462 * the MS samples do things completely differently than I have done them. Seems
463 * to work in my VMWare, though.
464 * - Basically, the function gets all of the information (port, dma, irq) about the
465 * controller, and then loops through all of the drives presented in PeripheralInformation.
466 * - Each controller has a CONTROLLER_INFO created for it, and each drive has a DRIVE_INFO.
467 * - Device objects are created for each drive (not controller), as that's the targeted
468 * device in the eyes of the rest of the OS. Each DRIVE_INFO points to a single CONTROLLER_INFO.
469 * - We only support up to four controllers in the whole system, each of which supports up to four
473 PKEY_VALUE_FULL_INFORMATION ControllerFullDescriptor
= ControllerInformation
[IoQueryDeviceConfigurationData
];
474 PCM_FULL_RESOURCE_DESCRIPTOR ControllerResourceDescriptor
= (PCM_FULL_RESOURCE_DESCRIPTOR
)((PCHAR
)ControllerFullDescriptor
+
475 ControllerFullDescriptor
->DataOffset
);
477 PKEY_VALUE_FULL_INFORMATION PeripheralFullDescriptor
= PeripheralInformation
[IoQueryDeviceConfigurationData
];
478 PCM_FULL_RESOURCE_DESCRIPTOR PeripheralResourceDescriptor
= (PCM_FULL_RESOURCE_DESCRIPTOR
)((PCHAR
)PeripheralFullDescriptor
+
479 PeripheralFullDescriptor
->DataOffset
);
481 PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialDescriptor
;
482 PCM_FLOPPY_DEVICE_DATA FloppyDeviceData
;
486 UNREFERENCED_PARAMETER(PeripheralType
);
487 UNREFERENCED_PARAMETER(PeripheralNumber
);
488 UNREFERENCED_PARAMETER(BusInformation
);
489 UNREFERENCED_PARAMETER(Context
);
490 UNREFERENCED_PARAMETER(ControllerType
);
491 UNREFERENCED_PARAMETER(PathName
);
494 TRACE_(FLOPPY
, "ConfigCallback called with ControllerNumber %d\n", ControllerNumber
);
496 gControllerInfo
[gNumberOfControllers
].ControllerNumber
= ControllerNumber
;
497 gControllerInfo
[gNumberOfControllers
].InterfaceType
= BusType
;
498 gControllerInfo
[gNumberOfControllers
].BusNumber
= BusNumber
;
500 /* Get controller interrupt level/vector, dma channel, and port base */
501 for(i
= 0; i
< ControllerResourceDescriptor
->PartialResourceList
.Count
; i
++)
503 KeInitializeEvent(&gControllerInfo
[gNumberOfControllers
].SynchEvent
, NotificationEvent
, FALSE
);
505 PartialDescriptor
= &ControllerResourceDescriptor
->PartialResourceList
.PartialDescriptors
[i
];
507 if(PartialDescriptor
->Type
== CmResourceTypeInterrupt
)
509 gControllerInfo
[gNumberOfControllers
].Level
= PartialDescriptor
->u
.Interrupt
.Level
;
510 gControllerInfo
[gNumberOfControllers
].Vector
= PartialDescriptor
->u
.Interrupt
.Vector
;
512 if(PartialDescriptor
->Flags
& CM_RESOURCE_INTERRUPT_LATCHED
)
513 gControllerInfo
[gNumberOfControllers
].InterruptMode
= Latched
;
515 gControllerInfo
[gNumberOfControllers
].InterruptMode
= LevelSensitive
;
518 else if(PartialDescriptor
->Type
== CmResourceTypePort
)
520 PHYSICAL_ADDRESS TranslatedAddress
;
521 ULONG AddressSpace
= 0x1; /* I/O Port Range */
523 if(!HalTranslateBusAddress(BusType
, BusNumber
, PartialDescriptor
->u
.Port
.Start
, &AddressSpace
, &TranslatedAddress
))
525 WARN_(FLOPPY
, "HalTranslateBusAddress failed; returning\n");
526 return STATUS_IO_DEVICE_ERROR
;
529 if(AddressSpace
== 0)
530 gControllerInfo
[gNumberOfControllers
].BaseAddress
= MmMapIoSpace(TranslatedAddress
, FDC_PORT_BYTES
, MmNonCached
);
532 gControllerInfo
[gNumberOfControllers
].BaseAddress
= (PUCHAR
)(ULONG_PTR
)TranslatedAddress
.QuadPart
;
535 else if(PartialDescriptor
->Type
== CmResourceTypeDma
)
536 gControllerInfo
[gNumberOfControllers
].Dma
= PartialDescriptor
->u
.Dma
.Channel
;
539 /* Start with 0 drives, then go looking */
540 gControllerInfo
[gNumberOfControllers
].NumberOfDrives
= 0;
542 /* learn about drives attached to controller */
543 for(i
= 0; i
< PeripheralResourceDescriptor
->PartialResourceList
.Count
; i
++)
545 PDRIVE_INFO DriveInfo
= &gControllerInfo
[gNumberOfControllers
].DriveInfo
[i
];
547 PartialDescriptor
= &PeripheralResourceDescriptor
->PartialResourceList
.PartialDescriptors
[i
];
549 if(PartialDescriptor
->Type
!= CmResourceTypeDeviceSpecific
)
552 FloppyDeviceData
= (PCM_FLOPPY_DEVICE_DATA
)(PartialDescriptor
+ 1);
554 DriveInfo
->ControllerInfo
= &gControllerInfo
[gNumberOfControllers
];
555 DriveInfo
->UnitNumber
= i
;
557 DriveInfo
->FloppyDeviceData
.MaxDensity
= FloppyDeviceData
->MaxDensity
;
558 DriveInfo
->FloppyDeviceData
.MountDensity
= FloppyDeviceData
->MountDensity
;
559 DriveInfo
->FloppyDeviceData
.StepRateHeadUnloadTime
= FloppyDeviceData
->StepRateHeadUnloadTime
;
560 DriveInfo
->FloppyDeviceData
.HeadLoadTime
= FloppyDeviceData
->HeadLoadTime
;
561 DriveInfo
->FloppyDeviceData
.MotorOffTime
= FloppyDeviceData
->MotorOffTime
;
562 DriveInfo
->FloppyDeviceData
.SectorLengthCode
= FloppyDeviceData
->SectorLengthCode
;
563 DriveInfo
->FloppyDeviceData
.SectorPerTrack
= FloppyDeviceData
->SectorPerTrack
;
564 DriveInfo
->FloppyDeviceData
.ReadWriteGapLength
= FloppyDeviceData
->ReadWriteGapLength
;
565 DriveInfo
->FloppyDeviceData
.FormatGapLength
= FloppyDeviceData
->FormatGapLength
;
566 DriveInfo
->FloppyDeviceData
.FormatFillCharacter
= FloppyDeviceData
->FormatFillCharacter
;
567 DriveInfo
->FloppyDeviceData
.HeadSettleTime
= FloppyDeviceData
->HeadSettleTime
;
568 DriveInfo
->FloppyDeviceData
.MotorSettleTime
= FloppyDeviceData
->MotorSettleTime
;
569 DriveInfo
->FloppyDeviceData
.MaximumTrackValue
= FloppyDeviceData
->MaximumTrackValue
;
570 DriveInfo
->FloppyDeviceData
.DataTransferLength
= FloppyDeviceData
->DataTransferLength
;
572 /* Once it's all set up, acknowledge its existence in the controller info object */
573 gControllerInfo
[gNumberOfControllers
].NumberOfDrives
++;
576 gControllerInfo
[gNumberOfControllers
].Populated
= TRUE
;
577 gNumberOfControllers
++;
579 return STATUS_SUCCESS
;
584 Isr(PKINTERRUPT Interrupt
, PVOID ServiceContext
)
586 * FUNCTION: Interrupt service routine for the controllers
588 * Interrupt: Interrupt object representing the interrupt that occured
589 * ServiceContext: Pointer to the ControllerInfo object that caused the interrupt
591 * TRUE in all cases (see notes)
593 * - We should always be the target of the interrupt, being an edge-triggered ISA interrupt, but
594 * this won't be the case with a level-sensitive system like PCI
595 * - Note that it probably doesn't matter if the interrupt isn't dismissed, as it's edge-triggered.
596 * It probably won't keep re-interrupting.
597 * - There are two different ways to dismiss a floppy interrupt. If the command has a result phase
598 * (see intel datasheet), you dismiss the interrupt by reading the first data byte. If it does
599 * not, you dismiss the interrupt by doing a Sense Interrupt command. Again, because it's edge-
600 * triggered, this is safe to not do here, as we can just wait for the DPC.
601 * - Either way, we don't want to do this here. The controller shouldn't interrupt again, so we'll
602 * schedule a DPC to take care of it.
603 * - This driver really cannot share interrupts, as I don't know how to conclusively say
604 * whether it was our controller that interrupted or not. I just have to assume that any time
605 * my ISR gets called, it was my board that called it. Dumb design, yes, but it goes back to
606 * the semantics of ISA buses. That, and I don't know much about ISA drivers. :-)
607 * UPDATE: The high bit of Status Register A seems to work on non-AT controllers.
611 PCONTROLLER_INFO ControllerInfo
= (PCONTROLLER_INFO
)ServiceContext
;
613 UNREFERENCED_PARAMETER(Interrupt
);
615 ASSERT(ControllerInfo
);
617 TRACE_(FLOPPY
, "ISR called\n");
620 * Due to the stupidity of the drive/controller relationship on the floppy drive, only one device object
621 * can have an active interrupt pending. Due to the nature of these IRPs, though, there will only ever
622 * be one thread expecting an interrupt at a time, and furthermore, Interrupts (outside of spurious ones)
623 * won't ever happen unless a thread is expecting them. Therefore, all we have to do is signal an event
624 * and we're done. Queue a DPC and leave.
626 KeInsertQueueDpc(&ControllerInfo
->Dpc
, NULL
, NULL
);
633 DpcForIsr(PKDPC UnusedDpc
, PVOID Context
, PVOID SystemArgument1
, PVOID SystemArgument2
)
635 * FUNCTION: This DPC gets queued by every ISR. Does the real per-interrupt work.
637 * UnusedDpc: Pointer to the DPC object that represents our function
638 * DeviceObject: Device that this DPC is running for
640 * Context: Pointer to our ControllerInfo struct
642 * - This function just kicks off whatever the SynchEvent is and returns. We depend on
643 * the thing that caused the drive to interrupt to handle the work of clearing the interrupt.
644 * This enables us to get back to PASSIVE_LEVEL and not hog system time on a really stupid,
645 * slow, screwed-up piece of hardware.
646 * - If nothing is waiting for us to set the event, the interrupt is effectively lost and will
647 * never be dismissed. I wonder if this will become a problem.
648 * - Called at DISPATCH_LEVEL
651 PCONTROLLER_INFO ControllerInfo
= (PCONTROLLER_INFO
)Context
;
653 UNREFERENCED_PARAMETER(UnusedDpc
);
654 UNREFERENCED_PARAMETER(SystemArgument1
);
655 UNREFERENCED_PARAMETER(SystemArgument2
);
657 ASSERT(ControllerInfo
);
659 TRACE_(FLOPPY
, "DpcForIsr called\n");
661 KeSetEvent(&ControllerInfo
->SynchEvent
, EVENT_INCREMENT
, FALSE
);
665 static NTSTATUS NTAPI
666 InitController(PCONTROLLER_INFO ControllerInfo
)
668 * FUNCTION: Initialize a newly-found controller
670 * ControllerInfo: pointer to the controller to be initialized
672 * STATUS_SUCCESS if the controller is successfully initialized
673 * STATUS_IO_DEVICE_ERROR otherwise
678 UCHAR HeadUnloadTime
;
680 UCHAR ControllerVersion
;
683 ASSERT(ControllerInfo
);
685 TRACE_(FLOPPY
, "InitController called with Controller 0x%p\n", ControllerInfo
);
687 /* Get controller in a known state */
688 if(HwConfigure(ControllerInfo
, FALSE
, TRUE
, TRUE
, 0, 0) != STATUS_SUCCESS
)
690 WARN_(FLOPPY
, "InitController: unable to configure controller\n");
691 return STATUS_IO_DEVICE_ERROR
;
694 /* Get the controller version */
695 ControllerVersion
= HwGetVersion(ControllerInfo
);
697 KeClearEvent(&ControllerInfo
->SynchEvent
);
699 /* Reset the controller */
700 if(HwReset(ControllerInfo
) != STATUS_SUCCESS
)
702 WARN_(FLOPPY
, "InitController: unable to reset controller\n");
703 return STATUS_IO_DEVICE_ERROR
;
706 INFO_(FLOPPY
, "InitController: waiting for initial interrupt\n");
708 /* Wait for an interrupt */
709 WaitForControllerInterrupt(ControllerInfo
, NULL
);
711 /* Reset means you have to clear each of the four interrupts (one per drive) */
712 for(i
= 0; i
< MAX_DRIVES_PER_CONTROLLER
; i
++)
714 INFO_(FLOPPY
, "InitController: Sensing interrupt %d\n", i
);
716 if(HwSenseInterruptStatus(ControllerInfo
) != STATUS_SUCCESS
)
718 WARN_(FLOPPY
, "InitController: Unable to clear interrupt 0x%x\n", i
);
719 return STATUS_IO_DEVICE_ERROR
;
723 INFO_(FLOPPY
, "InitController: done sensing interrupts\n");
725 /* Next, see if we have the right version to do implied seek */
726 if(ControllerVersion
== VERSION_ENHANCED
)
728 /* If so, set that up -- all defaults below except first TRUE for EIS */
729 if(HwConfigure(ControllerInfo
, TRUE
, TRUE
, TRUE
, 0, 0) != STATUS_SUCCESS
)
731 WARN_(FLOPPY
, "InitController: unable to set up implied seek\n");
732 ControllerInfo
->ImpliedSeeks
= FALSE
;
736 INFO_(FLOPPY
, "InitController: implied seeks set!\n");
737 ControllerInfo
->ImpliedSeeks
= TRUE
;
741 * FIXME: Figure out the answer to the below
743 * I must admit that I'm really confused about the Model 30 issue. At least one
744 * important bit (the disk change bit in the DIR) is flipped if this is a Model 30
745 * controller. However, at least one other floppy driver believes that there are only
746 * two computers that are guaranteed to have a Model 30 controller:
750 * ...and another driver only lists a config option for "thinkpad", that flips
751 * the change line. A third driver doesn't mention the Model 30 issue at all.
753 * What I can't tell is whether or not the average, run-of-the-mill computer now has
754 * a Model 30 controller. For the time being, I'm going to wire this to FALSE,
755 * and just not support the computers mentioned above, while I try to figure out
756 * how ubiquitous these newfangled 30 thingies are.
758 //ControllerInfo->Model30 = TRUE;
759 ControllerInfo
->Model30
= FALSE
;
763 INFO_(FLOPPY
, "InitController: enhanced version not supported; disabling implied seeks\n");
764 ControllerInfo
->ImpliedSeeks
= FALSE
;
765 ControllerInfo
->Model30
= FALSE
;
769 WARN_(FLOPPY
, "FIXME: Figure out speed\n");
770 HeadLoadTime
= SPECIFY_HLT_500K
;
771 HeadUnloadTime
= SPECIFY_HUT_500K
;
772 StepRateTime
= SPECIFY_SRT_500K
;
774 INFO_(FLOPPY
, "InitController: setting data rate\n");
777 if(HwSetDataRate(ControllerInfo
, DRSR_DSEL_500KBPS
) != STATUS_SUCCESS
)
779 WARN_(FLOPPY
, "InitController: unable to set data rate\n");
780 return STATUS_IO_DEVICE_ERROR
;
783 INFO_(FLOPPY
, "InitController: issuing specify command to controller\n");
785 /* Don't disable DMA --> enable dma (dumb & confusing) */
786 if(HwSpecify(ControllerInfo
, HeadLoadTime
, HeadUnloadTime
, StepRateTime
, FALSE
) != STATUS_SUCCESS
)
788 WARN_(FLOPPY
, "InitController: unable to specify options\n");
789 return STATUS_IO_DEVICE_ERROR
;
792 /* Init the stop stuff */
793 KeInitializeDpc(&ControllerInfo
->MotorStopDpc
, MotorStopDpcFunc
, ControllerInfo
);
794 KeInitializeTimer(&ControllerInfo
->MotorTimer
);
795 KeInitializeEvent(&ControllerInfo
->MotorStoppedEvent
, NotificationEvent
, FALSE
);
796 ControllerInfo
->StopDpcQueued
= FALSE
;
799 * Recalibrate each drive on the controller (depends on StartMotor, which depends on the timer stuff above)
800 * We don't even know if there is a disk in the drive, so this may not work, but that's OK.
802 for(i
= 0; i
< ControllerInfo
->NumberOfDrives
; i
++)
804 INFO_(FLOPPY
, "InitController: recalibrating drive 0x%x on controller 0x%p\n", i
, ControllerInfo
);
805 Recalibrate(&ControllerInfo
->DriveInfo
[i
]);
808 INFO_(FLOPPY
, "InitController: done initializing; returning STATUS_SUCCESS\n");
810 return STATUS_SUCCESS
;
815 AddControllers(PDRIVER_OBJECT DriverObject
)
817 * FUNCTION: Called on initialization to find our controllers and build device and controller objects for them
819 * DriverObject: Our driver's DriverObject (so we can create devices against it)
821 * FALSE if we can't allocate a device, adapter, or interrupt object, or if we fail to find any controllers
822 * TRUE otherwise (i.e. we have at least one fully-configured controller)
824 * - Currently we only support ISA buses.
825 * - BUG: Windows 2000 seems to clobber the response from the IoQueryDeviceDescription callback, so now we
826 * just test a boolean value in the first object to see if it was completely populated. The same value
827 * is tested for each controller before we build device objects for it.
829 * - Report resource usage to the HAL
832 INTERFACE_TYPE InterfaceType
= Isa
;
833 CONFIGURATION_TYPE ControllerType
= DiskController
;
834 CONFIGURATION_TYPE PeripheralType
= FloppyDiskPeripheral
;
836 DEVICE_DESCRIPTION DeviceDescription
;
842 /* Find our controllers on all ISA buses */
843 IoQueryDeviceDescription(&InterfaceType
, 0, &ControllerType
, 0, &PeripheralType
, 0, ConfigCallback
, 0);
846 * w2k breaks the return val from ConfigCallback, so we have to hack around it, rather than just
847 * looking for a return value from ConfigCallback. We expect at least one controller.
849 if(!gControllerInfo
[0].Populated
)
851 WARN_(FLOPPY
, "AddControllers: failed to get controller info from registry\n");
855 /* Now that we have a controller, set it up with the system */
856 for(i
= 0; i
< gNumberOfControllers
&& gControllerInfo
[i
].NumberOfDrives
> 0; i
++)
858 /* 0: Report resource usage to the kernel, to make sure they aren't assigned to anyone else */
859 /* FIXME: Implement me. */
861 /* 1: Set up interrupt */
862 gControllerInfo
[i
].MappedVector
= HalGetInterruptVector(gControllerInfo
[i
].InterfaceType
, gControllerInfo
[i
].BusNumber
,
863 gControllerInfo
[i
].Level
, gControllerInfo
[i
].Vector
,
864 &gControllerInfo
[i
].MappedLevel
, &Affinity
);
866 /* Must set up the DPC before we connect the interrupt */
867 KeInitializeDpc(&gControllerInfo
[i
].Dpc
, DpcForIsr
, &gControllerInfo
[i
]);
869 INFO_(FLOPPY
, "Connecting interrupt %d to controller%d (object 0x%p)\n", gControllerInfo
[i
].MappedVector
,
870 i
, &gControllerInfo
[i
]);
872 /* NOTE: We cannot share our interrupt, even on level-triggered buses. See Isr() for details. */
873 if(IoConnectInterrupt(&gControllerInfo
[i
].InterruptObject
, Isr
, &gControllerInfo
[i
], 0, gControllerInfo
[i
].MappedVector
,
874 gControllerInfo
[i
].MappedLevel
, gControllerInfo
[i
].MappedLevel
, gControllerInfo
[i
].InterruptMode
,
875 FALSE
, Affinity
, 0) != STATUS_SUCCESS
)
877 WARN_(FLOPPY
, "AddControllers: unable to connect interrupt\n");
882 memset(&DeviceDescription
, 0, sizeof(DeviceDescription
));
883 DeviceDescription
.Version
= DEVICE_DESCRIPTION_VERSION
;
884 DeviceDescription
.DmaChannel
= gControllerInfo
[i
].Dma
;
885 DeviceDescription
.InterfaceType
= gControllerInfo
[i
].InterfaceType
;
886 DeviceDescription
.BusNumber
= gControllerInfo
[i
].BusNumber
;
887 DeviceDescription
.MaximumLength
= 2*18*512; /* based on a 1.44MB floppy */
889 /* DMA 0,1,2,3 are 8-bit; 4,5,6,7 are 16-bit (4 is chain i think) */
890 DeviceDescription
.DmaWidth
= gControllerInfo
[i
].Dma
> 3 ? Width16Bits
: Width8Bits
;
892 gControllerInfo
[i
].AdapterObject
= HalGetAdapter(&DeviceDescription
, &gControllerInfo
[i
].MapRegisters
);
894 if(!gControllerInfo
[i
].AdapterObject
)
896 WARN_(FLOPPY
, "AddControllers: unable to allocate an adapter object\n");
897 IoDisconnectInterrupt(gControllerInfo
[i
].InterruptObject
);
901 /* 2b: Initialize the new controller */
902 if(InitController(&gControllerInfo
[i
]) != STATUS_SUCCESS
)
904 WARN_(FLOPPY
, "AddControllers(): Unable to set up controller %d - initialization failed\n", i
);
905 IoDisconnectInterrupt(gControllerInfo
[i
].InterruptObject
);
909 /* 2c: Set the controller's initialized flag so we know to release stuff in Unload */
910 gControllerInfo
[i
].Initialized
= TRUE
;
912 /* 3: per-drive setup */
913 for(j
= 0; j
< gControllerInfo
[i
].NumberOfDrives
; j
++)
915 WCHAR DeviceNameBuf
[MAX_DEVICE_NAME
];
916 UNICODE_STRING DeviceName
;
917 UNICODE_STRING LinkName
;
918 UNICODE_STRING ArcPath
;
921 INFO_(FLOPPY
, "AddControllers(): Configuring drive %d on controller %d\n", i
, j
);
924 * 3a: create a device object for the drive
925 * Controllers and drives are 0-based, so the combos are:
937 DriveNumber
= (UCHAR
)(i
*4 + j
); /* loss of precision is OK; there are only 16 of 'em */
939 RtlZeroMemory(&DeviceNameBuf
, MAX_DEVICE_NAME
* sizeof(WCHAR
));
940 swprintf(DeviceNameBuf
, L
"\\Device\\Floppy%d", DriveNumber
);
941 RtlInitUnicodeString(&DeviceName
, DeviceNameBuf
);
943 if(IoCreateDevice(DriverObject
, sizeof(PVOID
), &DeviceName
,
944 FILE_DEVICE_DISK
, FILE_REMOVABLE_MEDIA
| FILE_FLOPPY_DISKETTE
, FALSE
,
945 &gControllerInfo
[i
].DriveInfo
[j
].DeviceObject
) != STATUS_SUCCESS
)
947 WARN_(FLOPPY
, "AddControllers: unable to register a Device object\n");
948 IoDisconnectInterrupt(gControllerInfo
[i
].InterruptObject
);
949 continue; /* continue on to next drive */
952 INFO_(FLOPPY
, "AddControllers: New device: %S (0x%p)\n", DeviceNameBuf
, gControllerInfo
[i
].DriveInfo
[j
].DeviceObject
);
954 /* 3b.5: Create an ARC path in case we're booting from this drive */
955 swprintf(gControllerInfo
[i
].DriveInfo
[j
].ArcPathBuffer
,
956 L
"\\ArcName\\multi(%d)disk(%d)fdisk(%d)", gControllerInfo
[i
].BusNumber
, i
, DriveNumber
);
958 RtlInitUnicodeString(&ArcPath
, gControllerInfo
[i
].DriveInfo
[j
].ArcPathBuffer
);
959 IoAssignArcName(&ArcPath
, &DeviceName
);
961 /* 3c: Set flags up */
962 gControllerInfo
[i
].DriveInfo
[j
].DeviceObject
->Flags
|= DO_DIRECT_IO
;
964 /* 3d: Create a symlink */
965 swprintf(gControllerInfo
[i
].DriveInfo
[j
].SymLinkBuffer
, L
"\\DosDevices\\%c:", DriveNumber
+ 'A');
966 RtlInitUnicodeString(&LinkName
, gControllerInfo
[i
].DriveInfo
[j
].SymLinkBuffer
);
967 if(IoCreateSymbolicLink(&LinkName
, &DeviceName
) != STATUS_SUCCESS
)
969 WARN_(FLOPPY
, "AddControllers: Unable to create a symlink for drive %d\n", DriveNumber
);
970 IoDisconnectInterrupt(gControllerInfo
[i
].InterruptObject
);
971 IoDeassignArcName(&ArcPath
);
972 continue; /* continue to next drive */
975 /* 3e: Increase global floppy drives count */
976 IoGetConfigurationInformation()->FloppyCount
++;
978 /* 3f: Set up the DPC */
979 IoInitializeDpcRequest(gControllerInfo
[i
].DriveInfo
[j
].DeviceObject
, (PIO_DPC_ROUTINE
)DpcForIsr
);
981 /* 3g: Point the device extension at our DriveInfo struct */
982 gControllerInfo
[i
].DriveInfo
[j
].DeviceObject
->DeviceExtension
= &gControllerInfo
[i
].DriveInfo
[j
];
984 /* 3h: neat comic strip */
986 /* 3i: set the initial media type to unknown */
987 memset(&gControllerInfo
[i
].DriveInfo
[j
].DiskGeometry
, 0, sizeof(DISK_GEOMETRY
));
988 gControllerInfo
[i
].DriveInfo
[j
].DiskGeometry
.MediaType
= Unknown
;
990 /* 3j: Now that we're done, set the Initialized flag so we know to free this in Unload */
991 gControllerInfo
[i
].DriveInfo
[j
].Initialized
= TRUE
;
993 /* 3k: Clear the DO_DEVICE_INITIALIZING flag */
994 gControllerInfo
[i
].DriveInfo
[j
].DeviceObject
->Flags
&= ~DO_DEVICE_INITIALIZING
;
996 /* 3l: Attempt to get drive info - if a floppy is already present */
997 StartMotor(&gControllerInfo
[i
].DriveInfo
[j
]);
998 RWDetermineMediaType(&gControllerInfo
[i
].DriveInfo
[j
], TRUE
);
999 StopMotor(gControllerInfo
[i
].DriveInfo
[j
].ControllerInfo
);
1003 INFO_(FLOPPY
, "AddControllers: --------------------------------------------> finished adding controllers\n");
1005 return (IoGetConfigurationInformation()->FloppyCount
!= 0);
1010 SignalMediaChanged(PDEVICE_OBJECT DeviceObject
, PIRP Irp
)
1012 * FUNCTION: Process an IRP when the media has changed, and possibly notify the user
1014 * DeviceObject: DeviceObject associated with the IRP
1015 * Irp: IRP that we're failing due to change
1017 * - This procedure is documented in the DDK by "Notifying the File System of Possible Media Changes",
1018 * "IoSetHardErrorOrVerifyDevice", and by "Responding to Check-Verify Requests from the File System".
1019 * - Callable at <= DISPATCH_LEVEL
1022 PDRIVE_INFO DriveInfo
= DeviceObject
->DeviceExtension
;
1024 TRACE_(FLOPPY
, "SignalMediaChanged called\n");
1026 DriveInfo
->DiskChangeCount
++;
1028 /* If volume is not mounted, do NOT set verify and return STATUS_IO_DEVICE_ERROR */
1029 if(!(DeviceObject
->Vpb
->Flags
& VPB_MOUNTED
))
1031 Irp
->IoStatus
.Status
= STATUS_IO_DEVICE_ERROR
;
1032 Irp
->IoStatus
.Information
= 0;
1036 /* Notify the filesystem that it will need to verify the volume */
1037 DeviceObject
->Flags
|= DO_VERIFY_VOLUME
;
1038 Irp
->IoStatus
.Status
= STATUS_VERIFY_REQUIRED
;
1039 Irp
->IoStatus
.Information
= 0;
1042 * If this is a user-based, threaded request, let the IO manager know to pop up a box asking
1043 * the user to supply the correct media, but only if the error (which we just picked out above)
1044 * is deemed by the IO manager to be "user induced". The reason we don't just unconditionally
1045 * call IoSetHardError... is because MS might change the definition of "user induced" some day,
1046 * and we don't want to have to remember to re-code this.
1048 if(Irp
->Tail
.Overlay
.Thread
&& IoIsErrorUserInduced(Irp
->IoStatus
.Status
))
1049 IoSetHardErrorOrVerifyDevice(Irp
, DeviceObject
);
1054 QueueThread(PVOID Context
)
1056 * FUNCTION: Thread that manages the queue and dispatches any queued requests
1062 PIO_STACK_LOCATION Stack
;
1063 PDEVICE_OBJECT DeviceObject
;
1067 UNREFERENCED_PARAMETER(Context
);
1069 Objects
[0] = &QueueSemaphore
;
1070 Objects
[1] = &QueueThreadTerminate
;
1074 KeWaitForMultipleObjects(2, Objects
, WaitAny
, Executive
, KernelMode
, FALSE
, NULL
, NULL
);
1076 if(KeReadStateEvent(&QueueThreadTerminate
))
1078 INFO_(FLOPPY
, "QueueThread terminating\n");
1082 INFO_(FLOPPY
, "QueueThread: servicing an IRP\n");
1084 Irp
= IoCsqRemoveNextIrp(&Csq
, 0);
1086 /* we won't get an irp if it was canceled */
1089 INFO_(FLOPPY
, "QueueThread: IRP queue empty\n");
1093 DeviceObject
= (PDEVICE_OBJECT
)Irp
->Tail
.Overlay
.DriverContext
[0];
1095 ASSERT(DeviceObject
);
1097 Stack
= IoGetCurrentIrpStackLocation(Irp
);
1099 /* Decide what to do with the IRP */
1100 switch(Stack
->MajorFunction
)
1104 ReadWritePassive(DeviceObject
->DeviceExtension
, Irp
);
1107 case IRP_MJ_DEVICE_CONTROL
:
1108 DeviceIoctlPassive(DeviceObject
->DeviceExtension
, Irp
);
1112 WARN_(FLOPPY
, "QueueThread(): Unrecognized irp: mj: 0x%x\n", Stack
->MajorFunction
);
1113 Irp
->IoStatus
.Status
= STATUS_NOT_SUPPORTED
;
1114 Irp
->IoStatus
.Information
= 0;
1115 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
1122 DriverEntry(PDRIVER_OBJECT DriverObject
, PUNICODE_STRING RegistryPath
)
1124 * FUNCTION: Entry-point for the driver
1126 * DriverObject: Our driver object
1127 * RegistryPath: Unused
1129 * STATUS_SUCCESS on successful initialization of at least one drive
1130 * STATUS_NO_SUCH_DEVICE if we didn't find even one drive
1131 * STATUS_UNSUCCESSFUL otherwise
1134 HANDLE ThreadHandle
;
1136 UNREFERENCED_PARAMETER(RegistryPath
);
1139 * Set up dispatch routines
1141 DriverObject
->MajorFunction
[IRP_MJ_CREATE
] = (PDRIVER_DISPATCH
)CreateClose
;
1142 DriverObject
->MajorFunction
[IRP_MJ_CLOSE
] = (PDRIVER_DISPATCH
)CreateClose
;
1143 DriverObject
->MajorFunction
[IRP_MJ_READ
] = (PDRIVER_DISPATCH
)ReadWrite
;
1144 DriverObject
->MajorFunction
[IRP_MJ_WRITE
] = (PDRIVER_DISPATCH
)ReadWrite
;
1145 DriverObject
->MajorFunction
[IRP_MJ_DEVICE_CONTROL
] = (PDRIVER_DISPATCH
)DeviceIoctl
;
1147 DriverObject
->DriverUnload
= Unload
;
1150 * We depend on some zeroes in these structures. I know this is supposed to be
1151 * initialized to 0 by the complier but this makes me feel beter.
1153 memset(&gControllerInfo
, 0, sizeof(gControllerInfo
));
1156 * Set up queue. This routine cannot fail (trust me, I wrote it).
1158 IoCsqInitialize(&Csq
, CsqInsertIrp
, CsqRemoveIrp
, CsqPeekNextIrp
,
1159 CsqAcquireLock
, CsqReleaseLock
, CsqCompleteCanceledIrp
);
1164 KeInitializeSpinLock(&IrpQueueLock
);
1167 * ...and the queue list itself
1169 InitializeListHead(&IrpQueue
);
1172 * The queue is counted by a semaphore. The queue management thread
1173 * blocks on this semaphore, so if requests come in faster than the queue
1174 * thread can handle them, the semaphore count goes up.
1176 KeInitializeSemaphore(&QueueSemaphore
, 0, 0x7fffffff);
1179 * Event to terminate that thread
1181 KeInitializeEvent(&QueueThreadTerminate
, NotificationEvent
, FALSE
);
1184 * Create the queue processing thread. Save its handle in the global variable
1185 * ThreadHandle so we can wait on its termination during Unload.
1187 if(PsCreateSystemThread(&ThreadHandle
, THREAD_ALL_ACCESS
, 0, 0, 0, QueueThread
, 0) != STATUS_SUCCESS
)
1189 WARN_(FLOPPY
, "Unable to create system thread; failing init\n");
1190 return STATUS_INSUFFICIENT_RESOURCES
;
1193 if(ObReferenceObjectByHandle(ThreadHandle
, STANDARD_RIGHTS_ALL
, *PsThreadType
, KernelMode
, &QueueThreadObject
, NULL
) != STATUS_SUCCESS
)
1195 WARN_(FLOPPY
, "Unable to reference returned thread handle; failing init\n");
1196 return STATUS_UNSUCCESSFUL
;
1200 * Close the handle, now that we have the object pointer and a reference of our own.
1201 * The handle will certainly not be valid in the context of the caller next time we
1202 * need it, as handles are process-specific.
1204 ZwClose(ThreadHandle
);
1207 * Start the device discovery process. Returns STATUS_SUCCESS if
1208 * it finds even one drive attached to one controller.
1210 if(!AddControllers(DriverObject
))
1211 return STATUS_NO_SUCH_DEVICE
;
1213 return STATUS_SUCCESS
;