- get mpu401, parallel and ramdrv to work on msvc.
[reactos.git] / reactos / drivers / dd / parallel / parallel.c
1 /* $Id$
2 *
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: services/parallel/parallel.c
6 * PURPOSE: Parallel port driver
7 * PROGRAMMER: David Welch (welch@mcmail.com)
8 * UPDATE HISTORY:
9 * ??/??/??: Created
10 * 18/06/98: Made more NT like
11 */
12
13 /* FUNCTIONS **************************************************************/
14
15 #include <ntddk.h>
16
17 #include "parallel.h"
18
19 #define NDEBUG
20 #include <debug.h>
21
22
23 #define LP_B (0x378)
24 #define LP_S (READ_PORT_UCHAR((PUCHAR)(LP_B+1)))
25 #define LP_C (LP_B+2)
26
27 NTSTATUS STDCALL
28 DriverEntry(PDRIVER_OBJECT DriverObject, PUNICODE_STRING RegistryPath);
29
30 static void Parallel_Reset(void)
31 /*
32 * FUNCTION: Resets the device attached to the parallel port
33 */
34 {
35 int i;
36
37 WRITE_PORT_UCHAR((PUCHAR)LP_C,0);
38 for (i=0;i<LP_DELAY;i++);
39 WRITE_PORT_UCHAR((PUCHAR)LP_C,LP_PSELECP | LP_PINITP);
40 }
41
42 static void Parallel_putchar(unsigned char ch)
43 /*
44 * FUNCTION: Writes a character to the parallel port
45 * ARGUMENTS:
46 * ch = character to write
47 */
48 {
49
50 int count=0;
51 int status;
52 int wait=0;
53
54 do
55 {
56 status=LP_S;
57 count++;
58 }
59 while ( count < 500000 && !(status & LP_PBUSY) );
60
61 if (count==500000)
62 {
63 DPRINT("printer_putchar(): timed out\n");
64 return;
65 }
66
67 WRITE_PORT_UCHAR((PUCHAR)LP_B,ch);
68 while (wait != 10000) { wait++; }
69 WRITE_PORT_UCHAR((PUCHAR)LP_C, (LP_PSELECP | LP_PINITP | LP_PSTROBE ));
70 while (wait) { wait--; }
71 WRITE_PORT_UCHAR((PUCHAR)LP_C, LP_PSELECP | LP_PINITP);
72 }
73
74 static NTSTATUS STDCALL
75 Dispatch(PDEVICE_OBJECT DeviceObject, PIRP Irp)
76 /*
77 * FUNCTION: Handles user mode requests
78 * ARGUMENTS:
79 * DeviceObject = Device for request
80 * Irp = I/O request packet describing request
81 * RETURNS: Success or failure
82 */
83 {
84 PIO_STACK_LOCATION Stack = IoGetCurrentIrpStackLocation(Irp);
85 NTSTATUS status;
86 ULONG i;
87
88 switch (Stack->MajorFunction)
89 {
90 case IRP_MJ_CREATE:
91 DPRINT("(Parallel Port Driver) Creating\n");
92 Parallel_Reset();
93 status = STATUS_SUCCESS;
94 break;
95
96 case IRP_MJ_CLOSE:
97 status = STATUS_SUCCESS;
98 break;
99
100 case IRP_MJ_WRITE:
101 DPRINT("(Parallel Port Driver) Writing %d bytes\n",
102 Stack->Parameters.Write.Length);
103 for (i=0;i<Stack->Parameters.Write.Length;i++)
104 {
105 Parallel_putchar(((char *)Irp->UserBuffer)[i]);
106 }
107 status = STATUS_SUCCESS;
108 break;
109
110 default:
111 status = STATUS_NOT_IMPLEMENTED;
112 break;
113 }
114
115 Irp->IoStatus.Status = status;
116 Irp->IoStatus.Information = 0;
117
118 IoCompleteRequest(Irp, IO_NO_INCREMENT);
119 return(status);
120 }
121
122 NTSTATUS STDCALL
123 DriverEntry(PDRIVER_OBJECT DriverObject, PUNICODE_STRING RegistryPath)
124 /*
125 * FUNCTION: Called by the system to initalize the driver
126 * ARGUMENTS:
127 * DriverObject = object describing this driver
128 * RegistryPath = path to our configuration entries
129 * RETURNS: Success or failure
130 */
131 {
132 PDEVICE_OBJECT DeviceObject;
133 UNICODE_STRING DeviceName = RTL_CONSTANT_STRING(L"\\Device\\Parallel");
134 NTSTATUS Status;
135
136 DPRINT("Parallel Port Driver 0.0.1\n");
137
138 Status = IoCreateDevice(DriverObject,
139 0,
140 &DeviceName,
141 FILE_DEVICE_PARALLEL_PORT,
142 0,
143 FALSE,
144 &DeviceObject);
145 if (!NT_SUCCESS(Status))
146 {
147 return(Status);
148 }
149
150 DeviceObject->Flags=0;
151 DriverObject->MajorFunction[IRP_MJ_CLOSE] = Dispatch;
152 DriverObject->MajorFunction[IRP_MJ_CREATE] = Dispatch;
153 DriverObject->MajorFunction[IRP_MJ_WRITE] = Dispatch;
154 DriverObject->DriverUnload = NULL;
155
156 return(STATUS_SUCCESS);
157 }
158
159 /* EOF */