Create a branch for network fixes.
[reactos.git] / drivers / serial / serial / serial.h
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: Serial driver
4 * FILE: drivers/dd/serial/serial.h
5 * PURPOSE: Serial driver header
6 *
7 * PROGRAMMERS: Hervé Poussineau (hpoussin@reactos.org)
8 */
9
10 #include <ntddk.h>
11 #include <ndk/haltypes.h>
12 #include <ntddser.h>
13 #include <stdio.h>
14 #include <debug.h>
15
16 #define TAG(A, B, C, D) (ULONG)(((A)<<0) + ((B)<<8) + ((C)<<16) + ((D)<<24))
17
18 /* See winbase.h */
19 #define PST_RS232 1
20 #define COMMPROP_INITIALIZED 0xE73CF52E
21
22 #ifndef _NTIFS_
23 /* Why is it only defined in ntifs.h file? */
24 NTSTATUS NTAPI
25 IoAttachDeviceToDeviceStackSafe(
26 IN PDEVICE_OBJECT SourceDevice,
27 IN PDEVICE_OBJECT TargetDevice,
28 OUT PDEVICE_OBJECT *AttachedToDeviceObject);
29 #endif
30
31 typedef enum
32 {
33 dsStopped,
34 dsStarted,
35 dsPaused,
36 dsRemoved,
37 dsSurpriseRemoved
38 } SERIAL_DEVICE_STATE;
39
40 typedef enum
41 {
42 UartUnknown,
43 Uart8250, /* initial version */
44 Uart16450, /* + 38.4 Kbps */
45 Uart16550, /* + 115 Kbps */
46 Uart16550A,/* + FIFO 16 bytes */
47 Uart16650, /* + FIFO 32 bytes, 230 Kbps, power management, auto-flow */
48 Uart16750 /* + FIFO 64 bytes, 460 Kbps */
49 } UART_TYPE;
50
51 typedef struct _CIRCULAR_BUFFER
52 {
53 PUCHAR Buffer;
54 ULONG Length;
55 ULONG ReadPosition;
56 ULONG WritePosition;
57 } CIRCULAR_BUFFER, *PCIRCULAR_BUFFER;
58
59 typedef struct _SERIAL_DEVICE_EXTENSION
60 {
61 PDEVICE_OBJECT Pdo;
62 PDEVICE_OBJECT LowerDevice;
63 SERIAL_DEVICE_STATE PnpState;
64 IO_REMOVE_LOCK RemoveLock;
65
66 ULONG SerialPortNumber;
67
68 ULONG ComPort;
69 ULONG BaudRate;
70 ULONG BaseAddress;
71 PKINTERRUPT Interrupt;
72 KDPC ReceivedByteDpc;
73 KDPC SendByteDpc;
74 KDPC CompleteIrpDpc;
75
76 SERIAL_LINE_CONTROL SerialLineControl;
77 UART_TYPE UartType;
78 ULONG WaitMask;
79 PIRP WaitOnMaskIrp;
80
81 ULONG BreakInterruptErrorCount;
82 SERIALPERF_STATS SerialPerfStats;
83 SERIAL_TIMEOUTS SerialTimeOuts;
84 BOOLEAN IsOpened;
85 KEVENT InputBufferNotEmpty;
86 CIRCULAR_BUFFER InputBuffer;
87 KSPIN_LOCK InputBufferLock;
88 CIRCULAR_BUFFER OutputBuffer;
89 KSPIN_LOCK OutputBufferLock;
90
91 UNICODE_STRING SerialInterfaceName;
92
93 /* Current values */
94 UCHAR MCR; /* Base+4, Modem Control Register */
95 UCHAR MSR; /* Base+6, Modem Status Register */
96 } SERIAL_DEVICE_EXTENSION, *PSERIAL_DEVICE_EXTENSION;
97
98 typedef struct _WORKITEM_DATA
99 {
100 PIRP Irp;
101 PIO_WORKITEM IoWorkItem;
102
103 BOOLEAN UseIntervalTimeout;
104 BOOLEAN UseTotalTimeout;
105 LARGE_INTEGER IntervalTimeout;
106 LARGE_INTEGER TotalTimeoutTime;
107 BOOLEAN DontWait;
108 BOOLEAN ReadAtLeastOneByte;
109 } WORKITEM_DATA, *PWORKITEM_DATA;
110
111 #define SERIAL_TAG TAG('S', 'e', 'r', 'l')
112
113 #define INFINITE ((ULONG)-1)
114
115 /* Baud master clock */
116 #define BAUD_CLOCK 1843200
117 #define CLOCKS_PER_BIT 16
118
119 /* UART registers and bits */
120 #define SER_RBR(x) ((x)+0) /* Receive Register */
121 #define SER_THR(x) ((x)+0) /* Transmit Register */
122 #define SER_DLL(x) ((x)+0) /* Baud Rate Divisor LSB */
123 #define SER_IER(x) ((x)+1) /* Interrupt Enable Register */
124 #define SR_IER_DATA_RECEIVED 0x01
125 #define SR_IER_THR_EMPTY 0x02
126 #define SR_IER_LSR_CHANGE 0x04
127 #define SR_IER_MSR_CHANGE 0x08
128 #define SR_IER_SLEEP_MODE 0x10 /* Uart >= 16750 */
129 #define SR_IER_LOW_POWER 0x20 /* Uart >= 16750 */
130 #define SER_DLM(x) ((x)+1) /* Baud Rate Divisor MSB */
131 #define SER_IIR(x) ((x)+2) /* Interrupt Identification Register */
132 #define SR_IIR_SELF 0x00
133 #define SR_IIR_ID_MASK 0x07
134 #define SR_IIR_MSR_CHANGE SR_IIR_SELF
135 #define SR_IIR_THR_EMPTY (SR_IIR_SELF | 2)
136 #define SR_IIR_DATA_RECEIVED (SR_IIR_SELF | 4)
137 #define SR_IIR_ERROR (SR_IIR_SELF | 6)
138 #define SER_FCR(x) ((x)+2) /* FIFO Control Register (Uart >= 16550A) */
139 #define SR_FCR_ENABLE_FIFO 0x01
140 #define SR_FCR_CLEAR_RCVR (0x02 | SR_FCR_ENABLE_FIFO)
141 #define SR_FCR_CLEAR_XMIT (0x04 | SR_FCR_ENABLE_FIFO)
142 #define SR_FCR_1_BYTE (0x00 | SR_FCR_ENABLE_FIFO)
143 #define SR_FCR_4_BYTES (0x40 | SR_FCR_ENABLE_FIFO)
144 #define SR_FCR_8_BYTES (0x80 | SR_FCR_ENABLE_FIFO)
145 #define SR_FCR_14_BYTES (0xC0 | SR_FCR_ENABLE_FIFO)
146 #define SER_LCR(x) ((x)+3) /* Line Control Register */
147 #define SR_LCR_CS5 0x00
148 #define SR_LCR_CS6 0x01
149 #define SR_LCR_CS7 0x02
150 #define SR_LCR_CS8 0x03
151 #define SR_LCR_ST1 0x00
152 #define SR_LCR_ST2 0x04
153 #define SR_LCR_PNO 0x00
154 #define SR_LCR_POD 0x08
155 #define SR_LCR_PEV 0x18
156 #define SR_LCR_PMK 0x28
157 #define SR_LCR_PSP 0x38
158 #define SR_LCR_BRK 0x40
159 #define SR_LCR_DLAB 0x80
160 #define SER_MCR(x) ((x)+4) /* Modem Control Register */
161 #define SR_MCR_DTR SERIAL_DTR_STATE
162 #define SR_MCR_RTS SERIAL_RTS_STATE
163 #define SER_LSR(x) ((x)+5) /* Line Status Register */
164 #define SR_LSR_DATA_RECEIVED 0x01
165 #define SR_LSR_OVERRUN_ERROR 0x02
166 #define SR_LSR_PARITY_ERROR 0x04
167 #define SR_LSR_FRAMING_ERROR 0x08
168 #define SR_LSR_BREAK_INT 0x10
169 #define SR_LSR_THR_EMPTY 0x20
170 #define SR_LSR_TSR_EMPTY 0x40
171 #define SR_LSR_ERROR_IN_FIFO 0x80 /* Uart >= 16550A */
172 #define SER_MSR(x) ((x)+6) /* Modem Status Register */
173 #define SR_MSR_CTS_CHANGED 0x01
174 #define SR_MSR_DSR_CHANGED 0x02
175 #define SR_MSR_RI_CHANGED 0x04
176 #define SR_MSR_DCD_CHANGED 0x08
177 #define SR_MSR_CTS SERIAL_CTS_STATE /* Clear To Send */
178 #define SR_MSR_DSR SERIAL_DSR_STATE /* Data Set Ready */
179 #define SI_MSR_RI SERIAL_RI_STATE /* Ring Indicator */
180 #define SR_MSR_DCD SERIAL_DCD_STATE /* Data Carrier Detect */
181 #define SER_SCR(x) ((x)+7) /* Scratch Pad Register, Uart >= Uart16450 */
182
183 /************************************ circularbuffer.c */
184
185 /* FIXME: transform these functions into #define? */
186 NTSTATUS
187 InitializeCircularBuffer(
188 IN PCIRCULAR_BUFFER pBuffer,
189 IN ULONG BufferSize);
190
191 NTSTATUS
192 FreeCircularBuffer(
193 IN PCIRCULAR_BUFFER pBuffer);
194
195 BOOLEAN
196 IsCircularBufferEmpty(
197 IN PCIRCULAR_BUFFER pBuffer);
198
199 ULONG
200 GetNumberOfElementsInCircularBuffer(
201 IN PCIRCULAR_BUFFER pBuffer);
202
203 NTSTATUS
204 PushCircularBufferEntry(
205 IN PCIRCULAR_BUFFER pBuffer,
206 IN UCHAR Entry);
207
208 NTSTATUS
209 PopCircularBufferEntry(
210 IN PCIRCULAR_BUFFER pBuffer,
211 OUT PUCHAR Entry);
212
213 NTSTATUS
214 IncreaseCircularBufferSize(
215 IN PCIRCULAR_BUFFER pBuffer,
216 IN ULONG NewBufferSize);
217
218 /************************************ cleanup.c */
219
220 DRIVER_DISPATCH SerialCleanup;
221
222 /************************************ close.c */
223
224 DRIVER_DISPATCH SerialClose;
225
226 /************************************ create.c */
227
228 DRIVER_DISPATCH SerialCreate;
229
230 /************************************ devctrl.c */
231
232 DRIVER_DISPATCH SerialDeviceControl;
233
234 NTSTATUS NTAPI
235 SerialSetBaudRate(
236 IN PSERIAL_DEVICE_EXTENSION DeviceExtension,
237 IN ULONG NewBaudRate);
238
239 NTSTATUS NTAPI
240 SerialSetLineControl(
241 IN PSERIAL_DEVICE_EXTENSION DeviceExtension,
242 IN PSERIAL_LINE_CONTROL NewSettings);
243
244 /************************************ info.c */
245
246 DRIVER_DISPATCH SerialQueryInformation;
247
248 /************************************ legacy.c */
249
250 UART_TYPE
251 SerialDetectUartType(
252 IN PUCHAR ComPortBase);
253
254 /************************************ misc.c */
255
256 NTSTATUS
257 ForwardIrpAndWait(
258 IN PDEVICE_OBJECT DeviceObject,
259 IN PIRP Irp);
260
261 DRIVER_DISPATCH ForwardIrpAndForget;
262
263 VOID NTAPI
264 SerialReceiveByte(
265 IN PKDPC Dpc,
266 IN PVOID pDeviceExtension, // real type PSERIAL_DEVICE_EXTENSION
267 IN PVOID Unused1,
268 IN PVOID Unused2);
269
270 VOID NTAPI
271 SerialSendByte(
272 IN PKDPC Dpc,
273 IN PVOID pDeviceExtension, // real type PSERIAL_DEVICE_EXTENSION
274 IN PVOID Unused1,
275 IN PVOID Unused2);
276
277 VOID NTAPI
278 SerialCompleteIrp(
279 IN PKDPC Dpc,
280 IN PVOID pDeviceExtension, // real type PSERIAL_DEVICE_EXTENSION
281 IN PVOID pIrp, // real type PIRP
282 IN PVOID Unused);
283
284 KSERVICE_ROUTINE SerialInterruptService;
285
286 /************************************ pnp.c */
287
288 NTSTATUS NTAPI
289 SerialAddDeviceInternal(
290 IN PDRIVER_OBJECT DriverObject,
291 IN PDEVICE_OBJECT Pdo,
292 IN UART_TYPE UartType,
293 IN PULONG pComPortNumber OPTIONAL,
294 OUT PDEVICE_OBJECT* pFdo OPTIONAL);
295
296 DRIVER_ADD_DEVICE SerialAddDevice;
297
298 NTSTATUS NTAPI
299 SerialPnpStartDevice(
300 IN PDEVICE_OBJECT DeviceObject,
301 IN PCM_RESOURCE_LIST ResourceList,
302 IN PCM_RESOURCE_LIST ResourceListTranslated);
303
304 DRIVER_DISPATCH SerialPnp;
305
306 /************************************ power.c */
307
308 DRIVER_DISPATCH SerialPower;
309
310 /************************************ rw.c */
311
312 DRIVER_DISPATCH SerialRead;
313 DRIVER_DISPATCH SerialWrite;