Sync with trunk.
[reactos.git] / drivers / serial / serial / pnp.c
1 /*
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
6 *
7 * PROGRAMMERS: Hervé Poussineau (hpoussin@reactos.org)
8 */
9 /* FIXME: call IoAcquireRemoveLock/IoReleaseRemoveLock around each I/O operation */
10
11 #define INITGUID
12 #include "serial.h"
13
14 NTSTATUS NTAPI
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)
21 {
22 PDEVICE_OBJECT Fdo = NULL;
23 PSERIAL_DEVICE_EXTENSION DeviceExtension = NULL;
24 NTSTATUS Status;
25 WCHAR DeviceNameBuffer[32];
26 UNICODE_STRING DeviceName;
27
28 TRACE_(SERIAL, "SerialAddDeviceInternal()\n");
29
30 ASSERT(DriverObject);
31 ASSERT(Pdo);
32
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),
38 &DeviceName,
39 FILE_DEVICE_SERIAL_PORT,
40 FILE_DEVICE_SECURE_OPEN,
41 FALSE,
42 &Fdo);
43 if (!NT_SUCCESS(Status))
44 {
45 WARN_(SERIAL, "IoCreateDevice() failed with status 0x%08x\n", Status);
46 Fdo = NULL;
47 goto ByeBye;
48 }
49 DeviceExtension = (PSERIAL_DEVICE_EXTENSION)Fdo->DeviceExtension;
50 RtlZeroMemory(DeviceExtension, sizeof(SERIAL_DEVICE_EXTENSION));
51
52 /* Register device interface */
53 Status = IoRegisterDeviceInterface(Pdo, &GUID_DEVINTERFACE_COMPORT, NULL, &DeviceExtension->SerialInterfaceName);
54 if (!NT_SUCCESS(Status))
55 {
56 WARN_(SERIAL, "IoRegisterDeviceInterface() failed with status 0x%08x\n", Status);
57 goto ByeBye;
58 }
59
60 DeviceExtension->SerialPortNumber = IoGetConfigurationInformation()->SerialCount++;
61 if (pComPortNumber == NULL)
62 DeviceExtension->ComPort = DeviceExtension->SerialPortNumber + 1;
63 else
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))
81 {
82 WARN_(SERIAL, "IoAttachDeviceToDeviceStackSafe() failed with status 0x%08x\n", Status);
83 goto ByeBye;
84 }
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;
91
92 /* Choose default strategy */
93 if ((Fdo->Flags & (DO_BUFFERED_IO | DO_DIRECT_IO)) == 0)
94 Fdo->Flags |= DO_BUFFERED_IO;
95
96 Fdo->Flags &= ~DO_DEVICE_INITIALIZING;
97 if (pFdo)
98 {
99 *pFdo = Fdo;
100 }
101
102 return STATUS_SUCCESS;
103
104 ByeBye:
105 if (Fdo)
106 {
107 FreeCircularBuffer(&DeviceExtension->InputBuffer);
108 FreeCircularBuffer(&DeviceExtension->OutputBuffer);
109 IoDeleteDevice(Fdo);
110 }
111 return Status;
112 }
113
114 NTSTATUS NTAPI
115 SerialAddDevice(
116 IN PDRIVER_OBJECT DriverObject,
117 IN PDEVICE_OBJECT Pdo)
118 {
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.
122 */
123 if (Pdo == NULL)
124 return STATUS_SUCCESS;
125
126 /* We have here a PDO not null. It represents a real serial
127 * port. So call the internal AddDevice function.
128 */
129 return SerialAddDeviceInternal(DriverObject, Pdo, UartUnknown, NULL, NULL);
130 }
131
132 NTSTATUS NTAPI
133 SerialPnpStartDevice(
134 IN PDEVICE_OBJECT DeviceObject,
135 IN PCM_RESOURCE_LIST ResourceList,
136 IN PCM_RESOURCE_LIST ResourceListTranslated)
137 {
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;
145 ULONG Vector = 0;
146 ULONG i;
147 UCHAR IER;
148 KIRQL Dirql;
149 KAFFINITY Affinity = 0;
150 KINTERRUPT_MODE InterruptMode = Latched;
151 BOOLEAN ShareInterrupt = TRUE;
152 OBJECT_ATTRIBUTES objectAttributes;
153 PUCHAR ComPortBase;
154 UNICODE_STRING KeyName;
155 HANDLE hKey;
156 NTSTATUS Status;
157
158 DeviceExtension = (PSERIAL_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
159
160 ASSERT(DeviceExtension);
161
162 if (!ResourceList)
163 {
164 WARN_(SERIAL, "No allocated resources sent to driver\n");
165 return STATUS_INSUFFICIENT_RESOURCES;
166 }
167 if (ResourceList->Count != 1)
168 {
169 WARN_(SERIAL, "Wrong number of allocated resources sent to driver\n");
170 return STATUS_INSUFFICIENT_RESOURCES;
171 }
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)
176 {
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;
183 }
184
185 DeviceExtension->BaudRate = 19200;
186 DeviceExtension->BaseAddress = 0;
187 Dirql = 0;
188 for (i = 0; i < ResourceList->List[0].PartialResourceList.Count; i++)
189 {
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)
193 {
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;
200 break;
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;
207 else
208 InterruptMode = LevelSensitive;
209 ShareInterrupt = (PartialDescriptorTranslated->ShareDisposition == CmResourceShareShared);
210 break;
211 }
212 }
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;
217 if (!Dirql)
218 return STATUS_INSUFFICIENT_RESOURCES;
219 ComPortBase = ULongToPtr(DeviceExtension->BaseAddress);
220
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)
224 {
225 INFO_(SERIAL, "Failing IRP_MN_START_DEVICE as this serial port is used for debugging\n");
226 return STATUS_INSUFFICIENT_RESOURCES;
227 }
228
229 if (DeviceExtension->UartType == UartUnknown)
230 DeviceExtension->UartType = SerialDetectUartType(ComPortBase);
231
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;
236
237 /* Set baud rate */
238 Status = SerialSetBaudRate(DeviceExtension, DeviceExtension->BaudRate);
239 if (!NT_SUCCESS(Status))
240 {
241 WARN_(SERIAL, "SerialSetBaudRate() failed with status 0x%08x\n", Status);
242 return Status;
243 }
244
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))
251 {
252 WARN_(SERIAL, "SerialSetLineControl() failed with status 0x%08x\n", Status);
253 return Status;
254 }
255
256 /* Clear receive/transmit buffers */
257 if (DeviceExtension->UartType >= Uart16550A)
258 {
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);
262 }
263
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))
273 {
274 WARN_(SERIAL, "IoCreateSymbolicLink() failed with status 0x%08x\n", Status);
275 return Status;
276 }
277
278 /* Connect interrupt and enable them */
279 Status = IoConnectInterrupt(
280 &DeviceExtension->Interrupt, SerialInterruptService,
281 DeviceObject, NULL,
282 Vector, Dirql, Dirql,
283 InterruptMode, ShareInterrupt,
284 Affinity, FALSE);
285 if (!NT_SUCCESS(Status))
286 {
287 WARN_(SERIAL, "IoConnectInterrupt() failed with status 0x%08x\n", Status);
288 IoSetDeviceInterfaceState(&DeviceExtension->SerialInterfaceName, FALSE);
289 IoDeleteSymbolicLink(&LinkName);
290 return Status;
291 }
292
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))
299 {
300 /* Key = \Device\Serialx, Value = COMx */
301 ZwSetValueKey(hKey, &DeviceName, 0, REG_SZ, ComPortBuffer, ComPort.Length + sizeof(WCHAR));
302 ZwClose(hKey);
303 }
304
305 DeviceExtension->PnpState = dsStarted;
306
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);
311
312 /* Activate DTR, RTS */
313 DeviceExtension->MCR |= SR_MCR_DTR | SR_MCR_RTS;
314 WRITE_PORT_UCHAR(SER_MCR(ComPortBase), DeviceExtension->MCR);
315
316 /* Activate serial interface */
317 IoSetDeviceInterfaceState(&DeviceExtension->SerialInterfaceName, TRUE);
318 /* We don't really care if the call succeeded or not... */
319
320 return STATUS_SUCCESS;
321 }
322
323 NTSTATUS NTAPI
324 SerialPnp(
325 IN PDEVICE_OBJECT DeviceObject,
326 IN PIRP Irp)
327 {
328 ULONG MinorFunction;
329 PIO_STACK_LOCATION Stack;
330 ULONG_PTR Information = 0;
331 NTSTATUS Status;
332
333 Stack = IoGetCurrentIrpStackLocation(Irp);
334 MinorFunction = Stack->MinorFunction;
335
336 switch (MinorFunction)
337 {
338 /* FIXME: do all these minor functions
339 IRP_MN_QUERY_REMOVE_DEVICE 0x1
340 IRP_MN_REMOVE_DEVICE 0x2
341 {
342 TRACE_(SERIAL, "IRP_MJ_PNP / IRP_MN_REMOVE_DEVICE\n");
343 IoAcquireRemoveLock
344 IoReleaseRemoveLockAndWait
345 pass request to DeviceExtension-LowerDriver
346 disable interface
347 IoDeleteDevice(Fdo) and/or IoDetachDevice
348 break;
349 }
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
362 */
363 case IRP_MN_START_DEVICE: /* 0x0 */
364 {
365 TRACE_(SERIAL, "IRP_MJ_PNP / IRP_MN_START_DEVICE\n");
366
367 ASSERT(((PSERIAL_DEVICE_EXTENSION)DeviceObject->DeviceExtension)->PnpState == dsStopped);
368
369 /* Call lower driver */
370 Status = ForwardIrpAndWait(DeviceObject, Irp);
371 if (NT_SUCCESS(Status))
372 Status = SerialPnpStartDevice(
373 DeviceObject,
374 Stack->Parameters.StartDevice.AllocatedResources,
375 Stack->Parameters.StartDevice.AllocatedResourcesTranslated);
376 break;
377 }
378 case IRP_MN_QUERY_DEVICE_RELATIONS: /* (optional) 0x7 */
379 {
380 switch (Stack->Parameters.QueryDeviceRelations.Type)
381 {
382 case BusRelations:
383 {
384 TRACE_(SERIAL, "IRP_MJ_PNP / IRP_MN_QUERY_DEVICE_RELATIONS / BusRelations\n");
385 return ForwardIrpAndForget(DeviceObject, Irp);
386 }
387 case RemovalRelations:
388 {
389 TRACE_(SERIAL, "IRP_MJ_PNP / IRP_MN_QUERY_DEVICE_RELATIONS / RemovalRelations\n");
390 return ForwardIrpAndForget(DeviceObject, Irp);
391 }
392 default:
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);
396 }
397 break;
398 }
399 case IRP_MN_FILTER_RESOURCE_REQUIREMENTS: /* (optional) 0xd */
400 {
401 TRACE_(SERIAL, "IRP_MJ_PNP / IRP_MN_FILTER_RESOURCE_REQUIREMENTS\n");
402 return ForwardIrpAndForget(DeviceObject, Irp);
403 }
404 default:
405 {
406 TRACE_(SERIAL, "Unknown minor function 0x%x\n", MinorFunction);
407 return ForwardIrpAndForget(DeviceObject, Irp);
408 }
409 }
410
411 Irp->IoStatus.Information = Information;
412 Irp->IoStatus.Status = Status;
413 IoCompleteRequest(Irp, IO_NO_INCREMENT);
414 return Status;
415 }