1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
| #include #define NTSTRSAFE_LIB #include
#define CPP_MAX_COM_ID 32 #define DELAY_ONE_MICROSECOND (-10) #define DELAY_ONE_MILLISECOND (DELAY_ONE_MICROSECOND*1000) #define DELAY_ONE_SECOND (DELAY_ONE_MILLISECOND*1000) static PDEVICE_OBJECT s_fltobj[CPP_MAX_COM_ID] = {0}; static PDEVICE_OBJECT s_nextobj[CPP_MAX_COM_ID] = {0};
NTSTATUS ccpAttachDevice( PDRIVER_OBJECT driver , PDEVICE_OBJECT oldobj , PDEVICE_OBJECT *fltobj , PDEVICE_OBJECT *next) { NTSTATUS status; PDEVICE_OBJECT topdev = NULL; status = IoCreateDevice (driver, 0, NULL, oldobj->DeviceType, 0, FALSE, fltobj); if (status != STATUS_SUCCESS) { return status; }
if (oldobj -> Flags & DO_BUFFERED_IO) { (*fltobj)->Flags |= DO_BUFFERED_IO; } if (oldobj->Flags & DO_DIRECT_IO) { (*fltobj)->Flags |= DO_DIRECT_IO; } if (oldobj->Flags & FILE_DEVICE_SECURE_OPEN) { (*fltobj)->Characteristics |= FILE_DEVICE_SECURE_OPEN; } (*fltobj)->Flags |= DO_POWER_PAGABLE; topdev = IoAttachDeviceToDeviceStack (*fltobj, oldobj); if (NULL == topdev) { IoDeleteDevice (*fltobj); *fltobj = NULL; status = STATUS_UNSUCCESSFUL; return status; } *next = topdev;
(*fltobj)->Flags = (*fltobj)->Flags & ~DO_DEVICE_INITIALIZING; return STATUS_SUCCESS; }
PDEVICE_OBJECT cppOpenCom(ULONG id, NTSTATUS *status) { UNICODE_STRING name_str; static WCHAR name[32] = {0}; PFILE_OBJECT fileobj = NULL; PDEVICE_OBJECT devobj = NULL; memset (name, 0, sizeof(WCHAR)*32); RtlStringCchPrintfW(name, 32, L"\\Device\\Serial%d", id); RtlInitUnicodeString (&name_str, name); *status = IoGetDeviceObjectPointer(&name_str, FILE_ALL_ACCESS, &fileobj, &devobj); if (*status == STATUS_SUCCESS) { ObDereferenceObject(fileobj); } return devobj; }
void cppAttachAllCom(PDRIVER_OBJECT driver) { ULONG i; PDEVICE_OBJECT com_ob; NTSTATUS status; for (i = 0; i < CPP_MAX_COM_ID; ++i) { com_ob = cppOpenCom (i, &status); if (NULL == com_ob) { continue; } ccpAttachDevice(driver, com_ob, &s_fltobj[i], &s_nextobj[i]); } }
NTSTATUS ccpDispatch(PDEVICE_OBJECT device, PIRP irp) { PIO_STACK_LOCATION irpsp = IoGetCurrentIrpStackLocation (irp); NTSTATUS status; ULONG i, j; for (i = 0; i < CPP_MAX_COM_ID; ++i) { if (device == s_fltobj[i]) { if (irpsp->MajorFunction == IRP_MJ_POWER) { PoStartNextPowerIrp (irp); IoSkipCurrentIrpStackLocation (irp); return PoCallDriver (s_nextobj[i], irp); } if (irpsp->MajorFunction == IRP_MJ_WRITE) { ULONG len = (irpsp->Parameters.Write.Length); PUCHAR buf = NULL; if (irp->MdlAddress != NULL) { buf = (PUCHAR)MmGetSystemAddressForMdlSafe( irp->MdlAddress, NormalPagePriority); } else { buf = (PUCHAR)irp->UserBuffer; } if (NULL == buf) { buf = (PUCHAR)irp->AssociatedIrp.SystemBuffer; }
for (j = 0; j < len; ++j) { DbgPrint("comcap: Send Data: %2x\r\n", buf[j]); } }
IoSkipCurrentIrpStackLocation (irp); return IoCallDriver (s_nextobj[i], irp); } } irp->IoStatus.Information = 0; irp->IoStatus.Status = STATUS_INVALID_PARAMETER; IoCompleteRequest(irp, IO_NO_INCREMENT); return STATUS_SUCCESS; }
void ccpUnload(PDRIVER_OBJECT drv) { ULONG i; LARGE_INTEGER interval; for (i = 0; i < CPP_MAX_COM_ID; ++i) { if (NULL != s_nextobj[i]) { IoDetachDevice (s_nextobj[i]); } } interval.QuadPart = 5*1000*DELAY_ONE_MILLISECOND; KeDelayExecutionThread (KernelMode, FALSE, &interval); for (i = 0; i < CPP_MAX_COM_ID; ++i) { if (NULL != s_fltobj[i]) { IoDeleteDevice (s_fltobj[i]); } } }
NTSTATUS DriverEntry(PDRIVER_OBJECT driver, PUNICODE_STRING reg_path) { size_t i; #if DBG _asm int 3; #endif for (i = 0; i < IRP_MJ_MAXIMUM_FUNCTION; ++i) { driver->MajorFunction[i] = ccpDispatch; }
driver->DriverUnload = ccpUnload ; cppAttachAllCom (driver); return STATUS_SUCCESS; }
|