:pserver:cvsanon@mok.lvcm.com:/CVS/ReactOS reactos
[reactos.git] / drivers / dd / sdisk / sdisk.c
1 /*
2  * COPYRIGHT:        See COPYING in the top level directory
3  * PROJECT:          ReactOS kernel
4  * FILE:             services/sdisk/sdisk.c
5  * PURPOSE:          Disk driver for Bochs
6  * PROGRAMMER:       David Welch (welch@mcmail.com)
7  * UPDATE HISTORY: 
8  */
9
10 /* INCLUDES ****************************************************************/
11
12 #include <ddk/ntddk.h>
13 #include <internal/halio.h>
14
15 #define NDEBUG
16 #include <internal/debug.h>
17
18 /* FUNCTIONS **************************************************************/
19
20 #define PORT      (0x3ec)
21
22 static VOID SdWriteOffset(ULONG Offset)
23 {
24    outl_p(PORT,Offset);
25 }
26
27 NTSTATUS Dispatch(PDEVICE_OBJECT DeviceObject, PIRP Irp)
28 /*
29  * FUNCTION: Handles user mode requests
30  * ARGUMENTS:
31  *           DeviceObject = Device for request
32  *           Irp = I/O request packet describing request
33  * RETURNS: Success or failure
34  */
35 {
36    PIO_STACK_LOCATION Stack = IoGetCurrentIrpStackLocation(Irp);
37    NTSTATUS status;
38    int i;
39    PCH Buffer;
40    ULONG Length;
41    ULONG Information = 0;
42    
43    switch (Stack->MajorFunction)
44      {
45       case IRP_MJ_CREATE:
46         DPRINT("Creating\n",0);
47         status = STATUS_SUCCESS;
48         break;
49         
50       case IRP_MJ_CLOSE:
51         status = STATUS_SUCCESS;
52         break;
53         
54       case IRP_MJ_WRITE:
55         DPRINT("Writing %d bytes\n",
56                Stack->Parameters.Write.Length);
57         Length = Stack->Parameters.Write.Length;
58         if ((Length%512)>0)
59           {
60              Length = Length - (Length%512);
61           }
62         Buffer = MmGetSystemAddressForMdl(Irp->MdlAddress);
63         DPRINT("Buffer %x\n",Buffer);
64         #if 0
65         for (i=0;i<Length;i++)
66           {
67              if ((i%512)==0)
68                {
69                   DPRINT("Offset %x\n",
70                            Stack->Parameters.Write.ByteOffset.LowPart+i);
71                   SdWriteOffset(Stack->Parameters.Write.ByteOffset.LowPart+i);
72                }
73              outb_p(PORT,Buffer[i]);
74              DbgPrint("%c",Buffer[i]);
75           }
76         #endif
77         for (i=0;i<(Length/512);i++)
78           {
79              DPRINT("Offset %x\n",
80                     Stack->Parameters.Write.ByteOffset.LowPart+i);
81                   SdWriteOffset(Stack->Parameters.Write.ByteOffset.LowPart+i);
82              outsb(PORT,Buffer,512);
83           }
84         status = STATUS_SUCCESS;
85         Information = Length;
86         break;
87       
88       case IRP_MJ_READ:
89         DPRINT("Reading %d bytes\n",
90                Stack->Parameters.Write.Length);
91         Length = Stack->Parameters.Write.Length;
92         if ((Length%512)>0)
93           {
94              Length = Length - (Length%512);
95           }
96         Buffer = MmGetSystemAddressForMdl(Irp->MdlAddress);
97         for (i=0;i<Length;i++)
98           {
99              if ((i%512)==0)
100                {
101                   DPRINT("Offset %d\n",
102                            Stack->Parameters.Write.ByteOffset.LowPart+i);
103                   SdWriteOffset(Stack->Parameters.Write.ByteOffset.LowPart+i);
104                }
105              Buffer[i]=inb_p(PORT);
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 = Information;
117    
118    IoCompleteRequest(Irp, IO_NO_INCREMENT);
119    return(status);
120 }
121
122 NTSTATUS DriverEntry(PDRIVER_OBJECT DriverObject, PUNICODE_STRING RegistryPath)
123 /*
124  * FUNCTION: Called by the system to initalize the driver
125  * ARGUMENTS:
126  *           DriverObject = object describing this driver
127  *           RegistryPath = path to our configuration entries
128  * RETURNS: Success or failure
129  */
130 {
131    PDEVICE_OBJECT DeviceObject;
132    NTSTATUS ret;
133    ANSI_STRING astr;
134    UNICODE_STRING ustr;
135    ANSI_STRING asymlink;
136    UNICODE_STRING usymlink;
137    
138    DbgPrint("Simple Disk Driver 0.0.1\n");
139           
140    RtlInitAnsiString(&astr,"\\Device\\SDisk");
141    RtlAnsiStringToUnicodeString(&ustr,&astr,TRUE);
142    ret = IoCreateDevice(DriverObject,0,&ustr,
143                         FILE_DEVICE_DISK,0,FALSE,&DeviceObject);
144    if (ret!=STATUS_SUCCESS)
145      {
146         return(ret);
147      }
148    
149    RtlInitAnsiString(&asymlink,"\\??\\C:");
150    RtlAnsiStringToUnicodeString(&usymlink,&asymlink,TRUE);
151    IoCreateSymbolicLink(&usymlink,&ustr);
152    
153    DeviceObject->Flags=DO_DIRECT_IO;
154    DriverObject->MajorFunction[IRP_MJ_CLOSE] = Dispatch;
155    DriverObject->MajorFunction[IRP_MJ_CREATE] = Dispatch;
156    DriverObject->MajorFunction[IRP_MJ_READ] = Dispatch;
157    DriverObject->MajorFunction[IRP_MJ_WRITE] = Dispatch;
158    DriverObject->DriverUnload = NULL;
159    
160    return(STATUS_SUCCESS);
161 }
162