2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: Serial port driver
4 * FILE: drivers/dd/serial/pnp.c
5 * PURPOSE: Serial IRP_MJ_PNP operations
7 * PROGRAMMERS: Hervé Poussineau (hpoussin@reactos.org)
9 /* FIXME: call IoAcquireRemoveLock/IoReleaseRemoveLock around each I/O operation */
15 SerialAddDeviceInternal(
16 IN PDRIVER_OBJECT DriverObject
,
17 IN PDEVICE_OBJECT Pdo
,
18 IN UART_TYPE UartType
,
19 IN PULONG pComPortNumber OPTIONAL
,
20 OUT PDEVICE_OBJECT
* pFdo OPTIONAL
)
22 PDEVICE_OBJECT Fdo
= NULL
;
23 PSERIAL_DEVICE_EXTENSION DeviceExtension
= NULL
;
25 WCHAR DeviceNameBuffer
[32];
26 UNICODE_STRING DeviceName
;
28 TRACE_(SERIAL
, "SerialAddDeviceInternal()\n");
33 /* Create new device object */
34 swprintf(DeviceNameBuffer
, L
"\\Device\\Serial%lu", IoGetConfigurationInformation()->SerialCount
);
35 RtlInitUnicodeString(&DeviceName
, DeviceNameBuffer
);
36 Status
= IoCreateDevice(DriverObject
,
37 sizeof(SERIAL_DEVICE_EXTENSION
),
39 FILE_DEVICE_SERIAL_PORT
,
40 FILE_DEVICE_SECURE_OPEN
,
43 if (!NT_SUCCESS(Status
))
45 WARN_(SERIAL
, "IoCreateDevice() failed with status 0x%08x\n", Status
);
49 DeviceExtension
= (PSERIAL_DEVICE_EXTENSION
)Fdo
->DeviceExtension
;
50 RtlZeroMemory(DeviceExtension
, sizeof(SERIAL_DEVICE_EXTENSION
));
52 /* Register device interface */
53 Status
= IoRegisterDeviceInterface(Pdo
, &GUID_DEVINTERFACE_COMPORT
, NULL
, &DeviceExtension
->SerialInterfaceName
);
54 if (!NT_SUCCESS(Status
))
56 WARN_(SERIAL
, "IoRegisterDeviceInterface() failed with status 0x%08x\n", Status
);
60 DeviceExtension
->SerialPortNumber
= IoGetConfigurationInformation()->SerialCount
++;
61 if (pComPortNumber
== NULL
)
62 DeviceExtension
->ComPort
= DeviceExtension
->SerialPortNumber
+ 1;
64 DeviceExtension
->ComPort
= *pComPortNumber
;
65 DeviceExtension
->Pdo
= Pdo
;
66 DeviceExtension
->PnpState
= dsStopped
;
67 DeviceExtension
->UartType
= UartType
;
68 Status
= InitializeCircularBuffer(&DeviceExtension
->InputBuffer
, 16);
69 if (!NT_SUCCESS(Status
)) goto ByeBye
;
70 Status
= InitializeCircularBuffer(&DeviceExtension
->OutputBuffer
, 16);
71 if (!NT_SUCCESS(Status
)) goto ByeBye
;
72 IoInitializeRemoveLock(&DeviceExtension
->RemoveLock
, SERIAL_TAG
, 0, 0);
73 KeInitializeSpinLock(&DeviceExtension
->InputBufferLock
);
74 KeInitializeSpinLock(&DeviceExtension
->OutputBufferLock
);
75 KeInitializeEvent(&DeviceExtension
->InputBufferNotEmpty
, NotificationEvent
, FALSE
);
76 KeInitializeDpc(&DeviceExtension
->ReceivedByteDpc
, SerialReceiveByte
, DeviceExtension
);
77 KeInitializeDpc(&DeviceExtension
->SendByteDpc
, SerialSendByte
, DeviceExtension
);
78 KeInitializeDpc(&DeviceExtension
->CompleteIrpDpc
, SerialCompleteIrp
, DeviceExtension
);
79 Status
= IoAttachDeviceToDeviceStackSafe(Fdo
, Pdo
, &DeviceExtension
->LowerDevice
);
80 if (!NT_SUCCESS(Status
))
82 WARN_(SERIAL
, "IoAttachDeviceToDeviceStackSafe() failed with status 0x%08x\n", Status
);
85 if (DeviceExtension
->LowerDevice
->Flags
& DO_POWER_PAGABLE
)
86 Fdo
->Flags
|= DO_POWER_PAGABLE
;
87 if (DeviceExtension
->LowerDevice
->Flags
& DO_BUFFERED_IO
)
88 Fdo
->Flags
|= DO_BUFFERED_IO
;
89 if (DeviceExtension
->LowerDevice
->Flags
& DO_DIRECT_IO
)
90 Fdo
->Flags
|= DO_DIRECT_IO
;
92 /* Choose default strategy */
93 if ((Fdo
->Flags
& (DO_BUFFERED_IO
| DO_DIRECT_IO
)) == 0)
94 Fdo
->Flags
|= DO_BUFFERED_IO
;
96 Fdo
->Flags
&= ~DO_DEVICE_INITIALIZING
;
102 return STATUS_SUCCESS
;
107 FreeCircularBuffer(&DeviceExtension
->InputBuffer
);
108 FreeCircularBuffer(&DeviceExtension
->OutputBuffer
);
116 IN PDRIVER_OBJECT DriverObject
,
117 IN PDEVICE_OBJECT Pdo
)
119 /* Serial.sys is a legacy driver. AddDevice is called once
120 * with a NULL Pdo just after the driver initialization.
121 * Detect this case and return success.
124 return STATUS_SUCCESS
;
126 /* We have here a PDO not null. It represents a real serial
127 * port. So call the internal AddDevice function.
129 return SerialAddDeviceInternal(DriverObject
, Pdo
, UartUnknown
, NULL
, NULL
);
133 SerialPnpStartDevice(
134 IN PDEVICE_OBJECT DeviceObject
,
135 IN PCM_RESOURCE_LIST ResourceList
,
136 IN PCM_RESOURCE_LIST ResourceListTranslated
)
138 PSERIAL_DEVICE_EXTENSION DeviceExtension
;
139 WCHAR DeviceNameBuffer
[32];
140 UNICODE_STRING DeviceName
;
141 WCHAR LinkNameBuffer
[32];
142 UNICODE_STRING LinkName
;
143 WCHAR ComPortBuffer
[32];
144 UNICODE_STRING ComPort
;
149 KAFFINITY Affinity
= 0;
150 KINTERRUPT_MODE InterruptMode
= Latched
;
151 BOOLEAN ShareInterrupt
= TRUE
;
152 OBJECT_ATTRIBUTES objectAttributes
;
154 UNICODE_STRING KeyName
;
158 DeviceExtension
= (PSERIAL_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
160 ASSERT(DeviceExtension
);
164 WARN_(SERIAL
, "No allocated resources sent to driver\n");
165 return STATUS_INSUFFICIENT_RESOURCES
;
167 if (ResourceList
->Count
!= 1)
169 WARN_(SERIAL
, "Wrong number of allocated resources sent to driver\n");
170 return STATUS_INSUFFICIENT_RESOURCES
;
172 if (ResourceList
->List
[0].PartialResourceList
.Version
!= 1
173 || ResourceList
->List
[0].PartialResourceList
.Revision
!= 1
174 || ResourceListTranslated
->List
[0].PartialResourceList
.Version
!= 1
175 || ResourceListTranslated
->List
[0].PartialResourceList
.Revision
!= 1)
177 WARN_(SERIAL
, "Revision mismatch: %u.%u != 1.1 or %u.%u != 1.1\n",
178 ResourceList
->List
[0].PartialResourceList
.Version
,
179 ResourceList
->List
[0].PartialResourceList
.Revision
,
180 ResourceListTranslated
->List
[0].PartialResourceList
.Version
,
181 ResourceListTranslated
->List
[0].PartialResourceList
.Revision
);
182 return STATUS_REVISION_MISMATCH
;
185 DeviceExtension
->BaudRate
= 19200;
186 DeviceExtension
->BaseAddress
= 0;
188 for (i
= 0; i
< ResourceList
->List
[0].PartialResourceList
.Count
; i
++)
190 PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialDescriptor
= &ResourceList
->List
[0].PartialResourceList
.PartialDescriptors
[i
];
191 PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialDescriptorTranslated
= &ResourceListTranslated
->List
[0].PartialResourceList
.PartialDescriptors
[i
];
192 switch (PartialDescriptor
->Type
)
194 case CmResourceTypePort
:
195 if (PartialDescriptor
->u
.Port
.Length
< 7)
196 return STATUS_INSUFFICIENT_RESOURCES
;
197 if (DeviceExtension
->BaseAddress
!= 0)
198 return STATUS_UNSUCCESSFUL
;
199 DeviceExtension
->BaseAddress
= PartialDescriptor
->u
.Port
.Start
.u
.LowPart
;
201 case CmResourceTypeInterrupt
:
202 Dirql
= (KIRQL
)PartialDescriptorTranslated
->u
.Interrupt
.Level
;
203 Vector
= PartialDescriptorTranslated
->u
.Interrupt
.Vector
;
204 Affinity
= PartialDescriptorTranslated
->u
.Interrupt
.Affinity
;
205 if (PartialDescriptorTranslated
->Flags
& CM_RESOURCE_INTERRUPT_LATCHED
)
206 InterruptMode
= Latched
;
208 InterruptMode
= LevelSensitive
;
209 ShareInterrupt
= (PartialDescriptorTranslated
->ShareDisposition
== CmResourceShareShared
);
213 INFO_(SERIAL
, "New COM port. Base = 0x%lx, Irql = %u\n",
214 DeviceExtension
->BaseAddress
, Dirql
);
215 if (!DeviceExtension
->BaseAddress
)
216 return STATUS_INSUFFICIENT_RESOURCES
;
218 return STATUS_INSUFFICIENT_RESOURCES
;
219 ComPortBase
= ULongToPtr(DeviceExtension
->BaseAddress
);
221 /* Test if we are trying to start the serial port used for debugging */
222 INFO_(SERIAL
, "Comparing addresses: KdComPortInUse: %p, ComPortBase: %p\n", KdComPortInUse
, ComPortBase
);
223 if (KdComPortInUse
== ComPortBase
)
225 INFO_(SERIAL
, "Failing IRP_MN_START_DEVICE as this serial port is used for debugging\n");
226 return STATUS_INSUFFICIENT_RESOURCES
;
229 if (DeviceExtension
->UartType
== UartUnknown
)
230 DeviceExtension
->UartType
= SerialDetectUartType(ComPortBase
);
232 /* Get current settings */
233 DeviceExtension
->MCR
= READ_PORT_UCHAR(SER_MCR(ComPortBase
));
234 DeviceExtension
->MSR
= READ_PORT_UCHAR(SER_MSR(ComPortBase
));
235 DeviceExtension
->WaitMask
= 0;
238 Status
= SerialSetBaudRate(DeviceExtension
, DeviceExtension
->BaudRate
);
239 if (!NT_SUCCESS(Status
))
241 WARN_(SERIAL
, "SerialSetBaudRate() failed with status 0x%08x\n", Status
);
245 /* Set line control */
246 DeviceExtension
->SerialLineControl
.StopBits
= STOP_BIT_1
;
247 DeviceExtension
->SerialLineControl
.Parity
= NO_PARITY
;
248 DeviceExtension
->SerialLineControl
.WordLength
= 8;
249 Status
= SerialSetLineControl(DeviceExtension
, &DeviceExtension
->SerialLineControl
);
250 if (!NT_SUCCESS(Status
))
252 WARN_(SERIAL
, "SerialSetLineControl() failed with status 0x%08x\n", Status
);
256 /* Clear receive/transmit buffers */
257 if (DeviceExtension
->UartType
>= Uart16550A
)
259 /* 16550 UARTs also have FIFO queues, but they are unusable due to a bug */
260 WRITE_PORT_UCHAR(SER_FCR(ComPortBase
),
261 SR_FCR_CLEAR_RCVR
| SR_FCR_CLEAR_XMIT
);
264 /* Create link \DosDevices\COMX -> \Device\SerialX */
265 swprintf(DeviceNameBuffer
, L
"\\Device\\Serial%lu", DeviceExtension
->SerialPortNumber
);
266 swprintf(LinkNameBuffer
, L
"\\DosDevices\\COM%lu", DeviceExtension
->ComPort
);
267 swprintf(ComPortBuffer
, L
"COM%lu", DeviceExtension
->ComPort
);
268 RtlInitUnicodeString(&DeviceName
, DeviceNameBuffer
);
269 RtlInitUnicodeString(&LinkName
, LinkNameBuffer
);
270 RtlInitUnicodeString(&ComPort
, ComPortBuffer
);
271 Status
= IoCreateSymbolicLink(&LinkName
, &DeviceName
);
272 if (!NT_SUCCESS(Status
))
274 WARN_(SERIAL
, "IoCreateSymbolicLink() failed with status 0x%08x\n", Status
);
278 /* Connect interrupt and enable them */
279 Status
= IoConnectInterrupt(
280 &DeviceExtension
->Interrupt
, SerialInterruptService
,
282 Vector
, Dirql
, Dirql
,
283 InterruptMode
, ShareInterrupt
,
285 if (!NT_SUCCESS(Status
))
287 WARN_(SERIAL
, "IoConnectInterrupt() failed with status 0x%08x\n", Status
);
288 IoSetDeviceInterfaceState(&DeviceExtension
->SerialInterfaceName
, FALSE
);
289 IoDeleteSymbolicLink(&LinkName
);
293 /* Write an entry value under HKLM\HARDWARE\DeviceMap\SERIALCOMM */
294 /* This step is not mandatory, so don't exit in case of error */
295 RtlInitUnicodeString(&KeyName
, L
"\\Registry\\Machine\\HARDWARE\\DeviceMap\\SERIALCOMM");
296 InitializeObjectAttributes(&objectAttributes
, &KeyName
, OBJ_CASE_INSENSITIVE
, NULL
, NULL
);
297 Status
= ZwCreateKey(&hKey
, KEY_SET_VALUE
, &objectAttributes
, 0, NULL
, REG_OPTION_VOLATILE
, NULL
);
298 if (NT_SUCCESS(Status
))
300 /* Key = \Device\Serialx, Value = COMx */
301 ZwSetValueKey(hKey
, &DeviceName
, 0, REG_SZ
, ComPortBuffer
, ComPort
.Length
+ sizeof(WCHAR
));
305 DeviceExtension
->PnpState
= dsStarted
;
307 /* Activate interrupt modes */
308 IER
= READ_PORT_UCHAR(SER_IER(ComPortBase
));
309 IER
|= SR_IER_DATA_RECEIVED
| SR_IER_THR_EMPTY
| SR_IER_LSR_CHANGE
| SR_IER_MSR_CHANGE
;
310 WRITE_PORT_UCHAR(SER_IER(ComPortBase
), IER
);
312 /* Activate DTR, RTS */
313 DeviceExtension
->MCR
|= SR_MCR_DTR
| SR_MCR_RTS
;
314 WRITE_PORT_UCHAR(SER_MCR(ComPortBase
), DeviceExtension
->MCR
);
316 /* Activate serial interface */
317 IoSetDeviceInterfaceState(&DeviceExtension
->SerialInterfaceName
, TRUE
);
318 /* We don't really care if the call succeeded or not... */
320 return STATUS_SUCCESS
;
325 IN PDEVICE_OBJECT DeviceObject
,
329 PIO_STACK_LOCATION Stack
;
330 ULONG_PTR Information
= 0;
333 Stack
= IoGetCurrentIrpStackLocation(Irp
);
334 MinorFunction
= Stack
->MinorFunction
;
336 switch (MinorFunction
)
338 /* FIXME: do all these minor functions
339 IRP_MN_QUERY_REMOVE_DEVICE 0x1
340 IRP_MN_REMOVE_DEVICE 0x2
342 TRACE_(SERIAL, "IRP_MJ_PNP / IRP_MN_REMOVE_DEVICE\n");
344 IoReleaseRemoveLockAndWait
345 pass request to DeviceExtension-LowerDriver
347 IoDeleteDevice(Fdo) and/or IoDetachDevice
350 IRP_MN_CANCEL_REMOVE_DEVICE 0x3
351 IRP_MN_STOP_DEVICE 0x4
352 IRP_MN_QUERY_STOP_DEVICE 0x5
353 IRP_MN_CANCEL_STOP_DEVICE 0x6
354 IRP_MN_QUERY_DEVICE_RELATIONS / BusRelations (optional) 0x7
355 IRP_MN_QUERY_DEVICE_RELATIONS / RemovalRelations (optional) 0x7
356 IRP_MN_QUERY_INTERFACE (optional) 0x8
357 IRP_MN_QUERY_CAPABILITIES (optional) 0x9
358 IRP_MN_FILTER_RESOURCE_REQUIREMENTS (optional) 0xd
359 IRP_MN_QUERY_PNP_DEVICE_STATE (optional) 0x14
360 IRP_MN_DEVICE_USAGE_NOTIFICATION (required or optional) 0x16
361 IRP_MN_SURPRISE_REMOVAL 0x17
363 case IRP_MN_START_DEVICE
: /* 0x0 */
365 TRACE_(SERIAL
, "IRP_MJ_PNP / IRP_MN_START_DEVICE\n");
367 ASSERT(((PSERIAL_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
)->PnpState
== dsStopped
);
369 /* Call lower driver */
370 Status
= ForwardIrpAndWait(DeviceObject
, Irp
);
371 if (NT_SUCCESS(Status
))
372 Status
= SerialPnpStartDevice(
374 Stack
->Parameters
.StartDevice
.AllocatedResources
,
375 Stack
->Parameters
.StartDevice
.AllocatedResourcesTranslated
);
378 case IRP_MN_QUERY_DEVICE_RELATIONS
: /* (optional) 0x7 */
380 switch (Stack
->Parameters
.QueryDeviceRelations
.Type
)
384 TRACE_(SERIAL
, "IRP_MJ_PNP / IRP_MN_QUERY_DEVICE_RELATIONS / BusRelations\n");
385 return ForwardIrpAndForget(DeviceObject
, Irp
);
387 case RemovalRelations
:
389 TRACE_(SERIAL
, "IRP_MJ_PNP / IRP_MN_QUERY_DEVICE_RELATIONS / RemovalRelations\n");
390 return ForwardIrpAndForget(DeviceObject
, Irp
);
393 TRACE_(SERIAL
, "IRP_MJ_PNP / IRP_MN_QUERY_DEVICE_RELATIONS / Unknown type 0x%lx\n",
394 Stack
->Parameters
.QueryDeviceRelations
.Type
);
395 return ForwardIrpAndForget(DeviceObject
, Irp
);
399 case IRP_MN_FILTER_RESOURCE_REQUIREMENTS
: /* (optional) 0xd */
401 TRACE_(SERIAL
, "IRP_MJ_PNP / IRP_MN_FILTER_RESOURCE_REQUIREMENTS\n");
402 return ForwardIrpAndForget(DeviceObject
, Irp
);
406 TRACE_(SERIAL
, "Unknown minor function 0x%x\n", MinorFunction
);
407 return ForwardIrpAndForget(DeviceObject
, Irp
);
411 Irp
->IoStatus
.Information
= Information
;
412 Irp
->IoStatus
.Status
= Status
;
413 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);