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