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