2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS Kernel Streaming
4 * FILE: drivers/ksfilter/ks/factory.c
5 * PURPOSE: KS Allocator functions
6 * PROGRAMMER: Johannes Anderwald
18 KsDispatchQuerySecurity(
19 IN PDEVICE_OBJECT DeviceObject
,
22 PKSOBJECT_CREATE_ITEM CreateItem
;
23 PIO_STACK_LOCATION IoStack
;
27 /* get current irp stack */
28 IoStack
= IoGetCurrentIrpStackLocation(Irp
);
31 CreateItem
= KSCREATE_ITEM_IRP_STORAGE(Irp
);
33 if (!CreateItem
|| !CreateItem
->SecurityDescriptor
)
36 Irp
->IoStatus
.Status
= STATUS_NO_SECURITY_ON_OBJECT
;
37 CompleteRequest(Irp
, IO_NO_INCREMENT
);
38 return STATUS_NO_SECURITY_ON_OBJECT
;
42 /* get input length */
43 Length
= IoStack
->Parameters
.QuerySecurity
.Length
;
45 /* clone the security descriptor */
46 Status
= SeQuerySecurityDescriptorInfo(&IoStack
->Parameters
.QuerySecurity
.SecurityInformation
, (PSECURITY_DESCRIPTOR
)Irp
->UserBuffer
, &Length
, &CreateItem
->SecurityDescriptor
);
48 DPRINT("SeQuerySecurityDescriptorInfo Status %x\n", Status
);
50 Irp
->IoStatus
.Status
= Status
;
51 Irp
->IoStatus
.Information
= Length
;
53 CompleteRequest(Irp
, IO_NO_INCREMENT
);
63 KsDispatchSetSecurity(
64 IN PDEVICE_OBJECT DeviceObject
,
67 PKSOBJECT_CREATE_ITEM CreateItem
;
68 PIO_STACK_LOCATION IoStack
;
69 PGENERIC_MAPPING Mapping
;
70 PSECURITY_DESCRIPTOR Descriptor
;
73 /* get current irp stack */
74 IoStack
= IoGetCurrentIrpStackLocation(Irp
);
77 CreateItem
= KSCREATE_ITEM_IRP_STORAGE(Irp
);
79 if (!CreateItem
|| !CreateItem
->SecurityDescriptor
)
82 Irp
->IoStatus
.Status
= STATUS_NO_SECURITY_ON_OBJECT
;
83 CompleteRequest(Irp
, IO_NO_INCREMENT
);
84 return STATUS_NO_SECURITY_ON_OBJECT
;
87 /* backup old descriptor */
88 Descriptor
= CreateItem
->SecurityDescriptor
;
90 /* get generic mapping */
91 Mapping
= IoGetFileObjectGenericMapping();
93 /* change security descriptor */
94 Status
= SeSetSecurityDescriptorInfo(NULL
, /*FIXME */
95 &IoStack
->Parameters
.SetSecurity
.SecurityInformation
,
96 IoStack
->Parameters
.SetSecurity
.SecurityDescriptor
,
97 &CreateItem
->SecurityDescriptor
,
101 if (NT_SUCCESS(Status
))
103 /* free old descriptor */
104 FreeItem(Descriptor
);
106 /* mark create item as changed */
107 CreateItem
->Flags
|= KSCREATE_ITEM_SECURITYCHANGED
;
111 Irp
->IoStatus
.Status
= Status
;
112 CompleteRequest(Irp
, IO_NO_INCREMENT
);
123 KsDispatchSpecificMethod(
125 IN PFNKSHANDLER Handler
)
128 return STATUS_UNSUCCESSFUL
;
139 IN PFILE_OBJECT FileObject
,
140 IN PKEVENT Event OPTIONAL
,
141 IN PVOID PortContext OPTIONAL
,
142 OUT PIO_STATUS_BLOCK IoStatusBlock
,
145 IN ULONG Key OPTIONAL
,
146 IN KPROCESSOR_MODE RequestorMode
)
148 PDEVICE_OBJECT DeviceObject
;
156 /* make sure event is reset */
160 if (RequestorMode
== UserMode
)
162 /* probe the user buffer */
165 ProbeForWrite(Buffer
, Length
, sizeof(UCHAR
));
166 Status
= STATUS_SUCCESS
;
168 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
170 /* Exception, get the error code */
171 Status
= _SEH2_GetExceptionCode();
175 if (!NT_SUCCESS(Status
))
177 DPRINT1("Invalid user buffer provided\n");
182 /* get corresponding device object */
183 DeviceObject
= IoGetRelatedDeviceObject(FileObject
);
185 /* fast-io read is only available for kernel mode clients */
186 if (RequestorMode
== KernelMode
&& ExGetPreviousMode() == KernelMode
&&
187 DeviceObject
->DriverObject
->FastIoDispatch
->FastIoRead
)
189 /* call fast io write */
190 Result
= DeviceObject
->DriverObject
->FastIoDispatch
->FastIoRead(FileObject
, &FileObject
->CurrentByteOffset
, Length
, TRUE
, Key
, Buffer
, IoStatusBlock
, DeviceObject
);
192 if (Result
&& NT_SUCCESS(IoStatusBlock
->Status
))
194 /* request was handeled and succeeded */
195 return STATUS_SUCCESS
;
199 /* do the slow way */
202 /* initialize temp event */
203 KeInitializeEvent(&LocalEvent
, NotificationEvent
, FALSE
);
207 /* build the irp packet */
208 Irp
= IoBuildSynchronousFsdRequest(IRP_MJ_READ
, DeviceObject
, Buffer
, Length
, &FileObject
->CurrentByteOffset
, Event
, IoStatusBlock
);
211 /* not enough resources */
212 return STATUS_INSUFFICIENT_RESOURCES
;
215 /* send the packet */
216 Status
= IoCallDriver(DeviceObject
, Irp
);
218 if (Status
== STATUS_PENDING
)
220 /* operation is pending, is sync file object */
221 if (FileObject
->Flags
& FO_SYNCHRONOUS_IO
)
224 KeWaitForSingleObject(Event
, Executive
, RequestorMode
, FALSE
, NULL
);
225 Status
= IoStatusBlock
->Status
;
239 IN PFILE_OBJECT FileObject
,
240 IN PKEVENT Event OPTIONAL
,
241 IN PVOID PortContext OPTIONAL
,
242 OUT PIO_STATUS_BLOCK IoStatusBlock
,
245 IN ULONG Key OPTIONAL
,
246 IN KPROCESSOR_MODE RequestorMode
)
248 PDEVICE_OBJECT DeviceObject
;
256 /* make sure event is reset */
260 if (RequestorMode
== UserMode
)
262 /* probe the user buffer */
265 ProbeForRead(Buffer
, Length
, sizeof(UCHAR
));
266 Status
= STATUS_SUCCESS
;
268 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
270 /* Exception, get the error code */
271 Status
= _SEH2_GetExceptionCode();
275 if (!NT_SUCCESS(Status
))
277 DPRINT1("Invalid user buffer provided\n");
282 /* get corresponding device object */
283 DeviceObject
= IoGetRelatedDeviceObject(FileObject
);
285 /* fast-io write is only available for kernel mode clients */
286 if (RequestorMode
== KernelMode
&& ExGetPreviousMode() == KernelMode
&&
287 DeviceObject
->DriverObject
->FastIoDispatch
->FastIoWrite
)
289 /* call fast io write */
290 Result
= DeviceObject
->DriverObject
->FastIoDispatch
->FastIoWrite(FileObject
, &FileObject
->CurrentByteOffset
, Length
, TRUE
, Key
, Buffer
, IoStatusBlock
, DeviceObject
);
292 if (Result
&& NT_SUCCESS(IoStatusBlock
->Status
))
294 /* request was handeled and succeeded */
295 return STATUS_SUCCESS
;
299 /* do the slow way */
302 /* initialize temp event */
303 KeInitializeEvent(&LocalEvent
, NotificationEvent
, FALSE
);
307 /* build the irp packet */
308 Irp
= IoBuildSynchronousFsdRequest(IRP_MJ_WRITE
, DeviceObject
, Buffer
, Length
, &FileObject
->CurrentByteOffset
, Event
, IoStatusBlock
);
311 /* not enough resources */
312 return STATUS_INSUFFICIENT_RESOURCES
;
315 /* send the packet */
316 Status
= IoCallDriver(DeviceObject
, Irp
);
318 if (Status
== STATUS_PENDING
)
320 /* operation is pending, is sync file object */
321 if (FileObject
->Flags
& FO_SYNCHRONOUS_IO
)
324 KeWaitForSingleObject(Event
, Executive
, RequestorMode
, FALSE
, NULL
);
325 Status
= IoStatusBlock
->Status
;
338 KsQueryInformationFile(
339 IN PFILE_OBJECT FileObject
,
340 OUT PVOID FileInformation
,
342 IN FILE_INFORMATION_CLASS FileInformationClass
)
344 PDEVICE_OBJECT DeviceObject
;
345 PFAST_IO_DISPATCH FastIoDispatch
;
347 PIO_STACK_LOCATION IoStack
;
348 IO_STATUS_BLOCK IoStatus
;
350 LARGE_INTEGER Offset
;
351 IO_STATUS_BLOCK StatusBlock
;
354 /* get related file object */
355 DeviceObject
= IoGetRelatedDeviceObject(FileObject
);
357 /* get fast i/o table */
358 FastIoDispatch
= DeviceObject
->DriverObject
->FastIoDispatch
;
360 /* is there a fast table */
363 /* check the class */
364 if (FileInformationClass
== FileBasicInformation
)
366 /* use FastIoQueryBasicInfo routine */
367 if (FastIoDispatch
->FastIoQueryBasicInfo
)
369 return FastIoDispatch
->FastIoQueryBasicInfo(FileObject
, TRUE
, (PFILE_BASIC_INFORMATION
)FileInformation
, &IoStatus
, DeviceObject
);
372 else if (FileInformationClass
== FileStandardInformation
)
374 /* use FastIoQueryBasicInfo routine */
375 if (FastIoDispatch
->FastIoQueryBasicInfo
)
377 return FastIoDispatch
->FastIoQueryStandardInfo(FileObject
, TRUE
, (PFILE_STANDARD_INFORMATION
)FileInformation
, &IoStatus
, DeviceObject
);
382 KeClearEvent(&FileObject
->Event
);
384 /* initialize event */
385 KeInitializeEvent(&Event
, NotificationEvent
, FALSE
);
387 /* set offset to zero */
388 Offset
.QuadPart
= 0L;
390 /* build the request */
391 Irp
= IoBuildSynchronousFsdRequest(IRP_MJ_QUERY_INFORMATION
, IoGetRelatedDeviceObject(FileObject
), NULL
, 0, &Offset
, &Event
, &StatusBlock
);
394 return STATUS_INSUFFICIENT_RESOURCES
;
396 /* get next stack location */
397 IoStack
= IoGetNextIrpStackLocation(Irp
);
399 /* setup parameters */
400 IoStack
->Parameters
.QueryFile
.FileInformationClass
= FileInformationClass
;
401 IoStack
->Parameters
.QueryFile
.Length
= Length
;
402 Irp
->AssociatedIrp
.SystemBuffer
= FileInformation
;
405 /* call the driver */
406 Status
= IoCallDriver(IoGetRelatedDeviceObject(FileObject
), Irp
);
408 if (Status
== STATUS_PENDING
)
410 /* wait for the operation to complete */
411 KeWaitForSingleObject(&Event
, Executive
, KernelMode
, FALSE
, NULL
);
414 if (FileObject
->Flags
& FO_SYNCHRONOUS_IO
)
415 Status
= FileObject
->FinalStatus
;
417 Status
= StatusBlock
.Status
;
430 KsSetInformationFile(
431 IN PFILE_OBJECT FileObject
,
432 IN PVOID FileInformation
,
434 IN FILE_INFORMATION_CLASS FileInformationClass
)
436 PIO_STACK_LOCATION IoStack
;
437 PDEVICE_OBJECT DeviceObject
;
441 LARGE_INTEGER Offset
;
442 IO_STATUS_BLOCK IoStatus
;
445 /* get related device object */
446 DeviceObject
= IoGetRelatedDeviceObject(FileObject
);
448 /* copy file information */
449 Buffer
= AllocateItem(NonPagedPool
, Length
);
451 return STATUS_INSUFFICIENT_RESOURCES
;
455 ProbeForRead(Buffer
, Length
, sizeof(UCHAR
));
456 RtlMoveMemory(Buffer
, FileInformation
, Length
);
457 Status
= STATUS_SUCCESS
;
459 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
461 /* Exception, get the error code */
462 Status
= _SEH2_GetExceptionCode();
466 if (!NT_SUCCESS(Status
))
468 /* invalid user buffer */
473 /* initialize the event */
474 KeInitializeEvent(&Event
, SynchronizationEvent
, FALSE
);
477 Offset
.QuadPart
= 0LL;
480 Irp
= IoBuildSynchronousFsdRequest(IRP_MJ_SET_INFORMATION
, DeviceObject
, NULL
, 0, &Offset
, &Event
, &IoStatus
);
484 /* failed to allocate irp */
486 return STATUS_INSUFFICIENT_RESOURCES
;
489 /* get next stack location */
490 IoStack
= IoGetNextIrpStackLocation(Irp
);
492 /* set irp parameters */
493 IoStack
->Parameters
.SetFile
.FileInformationClass
= FileInformationClass
;
494 IoStack
->Parameters
.SetFile
.Length
= Length
;
495 IoStack
->Parameters
.SetFile
.FileObject
= FileObject
;
496 Irp
->AssociatedIrp
.SystemBuffer
= Buffer
;
497 Irp
->UserBuffer
= FileInformation
;
499 /* dispatch the irp */
500 Status
= IoCallDriver(DeviceObject
, Irp
);
502 if (Status
== STATUS_PENDING
)
504 /* wait untill the operation has completed */
505 KeWaitForSingleObject(&Event
, Executive
, KernelMode
, FALSE
, NULL
);
506 /* is a sync file object */
507 if (FileObject
->Flags
& FO_SYNCHRONOUS_IO
)
508 Status
= FileObject
->FinalStatus
;
510 Status
= IoStatus
.Status
;
523 IN PFILE_OBJECT FileObject
,
524 IN PKEVENT Event OPTIONAL
,
525 IN PVOID PortContext OPTIONAL
,
526 IN PIO_COMPLETION_ROUTINE CompletionRoutine OPTIONAL
,
527 IN PVOID CompletionContext OPTIONAL
,
528 IN KSCOMPLETION_INVOCATION CompletionInvocationFlags OPTIONAL
,
529 OUT PIO_STATUS_BLOCK IoStatusBlock
,
530 IN OUT PVOID StreamHeaders
,
533 IN KPROCESSOR_MODE RequestorMode
)
536 PIO_STACK_LOCATION IoStack
;
537 PDEVICE_OBJECT DeviceObject
;
539 LARGE_INTEGER Offset
;
540 PKSIOBJECT_HEADER ObjectHeader
;
543 /* get related device object */
544 DeviceObject
= IoGetRelatedDeviceObject(FileObject
);
546 ASSERT(DeviceObject
!= NULL
);
548 /* is there a event provided */
555 if (RequestorMode
|| ExGetPreviousMode() == KernelMode
)
557 /* requestor is from kernel land */
558 ObjectHeader
= (PKSIOBJECT_HEADER
)FileObject
->FsContext2
;
562 /* there is a object header */
563 if (Flags
== KSSTREAM_READ
)
565 /* is fast read supported */
566 if (ObjectHeader
->DispatchTable
.FastRead
)
568 /* call fast read dispatch routine */
569 Ret
= ObjectHeader
->DispatchTable
.FastRead(FileObject
, NULL
, Length
, FALSE
, 0, StreamHeaders
, IoStatusBlock
, DeviceObject
);
573 /* the request was handeled */
574 return IoStatusBlock
->Status
;
578 else if (Flags
== KSSTREAM_WRITE
)
580 /* is fast write supported */
581 if (ObjectHeader
->DispatchTable
.FastWrite
)
583 /* call fast write dispatch routine */
584 Ret
= ObjectHeader
->DispatchTable
.FastWrite(FileObject
, NULL
, Length
, FALSE
, 0, StreamHeaders
, IoStatusBlock
, DeviceObject
);
588 /* the request was handeled */
589 return IoStatusBlock
->Status
;
596 /* clear file object event */
597 KeClearEvent(&FileObject
->Event
);
599 /* set the offset to zero */
600 Offset
.QuadPart
= 0LL;
602 /* now build the irp */
603 Irp
= IoBuildSynchronousFsdRequest(IRP_MJ_DEVICE_CONTROL
,
604 DeviceObject
, (PVOID
)StreamHeaders
, Length
, &Offset
, Event
, IoStatusBlock
);
607 /* not enough memory */
608 return STATUS_INSUFFICIENT_RESOURCES
;
611 /* setup irp parameters */
612 Irp
->RequestorMode
= RequestorMode
;
613 Irp
->Overlay
.AsynchronousParameters
.UserApcContext
= PortContext
;
614 Irp
->Tail
.Overlay
.OriginalFileObject
= FileObject
;
615 Irp
->UserBuffer
= StreamHeaders
;
617 /* get next irp stack location */
618 IoStack
= IoGetNextIrpStackLocation(Irp
);
619 /* setup stack parameters */
620 IoStack
->FileObject
= FileObject
;
621 IoStack
->Parameters
.DeviceIoControl
.InputBufferLength
= Length
;
622 IoStack
->Parameters
.DeviceIoControl
.Type3InputBuffer
= StreamHeaders
;
623 IoStack
->Parameters
.DeviceIoControl
.IoControlCode
= (Flags
== KSSTREAM_READ
? IOCTL_KS_READ_STREAM
: IOCTL_KS_WRITE_STREAM
);
625 if (CompletionRoutine
)
627 /* setup completion routine for async processing */
628 IoSetCompletionRoutine(Irp
, CompletionRoutine
, CompletionContext
, (CompletionInvocationFlags
& KsInvokeOnSuccess
), (CompletionInvocationFlags
& KsInvokeOnError
), (CompletionInvocationFlags
& KsInvokeOnCancel
));
631 /* now call the driver */
632 Status
= IoCallDriver(DeviceObject
, Irp
);
650 LOCK_OPERATION Operation
;
651 NTSTATUS Status
= STATUS_SUCCESS
;
652 PKSSTREAM_HEADER StreamHeader
;
653 PIO_STACK_LOCATION IoStack
;
655 //BOOLEAN AllocateMdl = FALSE;
657 /* get current irp stack */
658 IoStack
= IoGetCurrentIrpStackLocation(Irp
);
660 Length
= IoStack
->Parameters
.DeviceIoControl
.OutputBufferLength
;
662 if (Irp
->RequestorMode
== KernelMode
|| Irp
->AssociatedIrp
.SystemBuffer
)
664 if (Irp
->RequestorMode
== KernelMode
)
666 /* no need to allocate stream header */
667 Irp
->AssociatedIrp
.SystemBuffer
= Irp
->UserBuffer
;
670 /* check if alloc mdl flag is passed */
671 if (!(ProbeFlags
& KSPROBE_ALLOCATEMDL
))
673 /* nothing more to do */
674 return STATUS_SUCCESS
;
679 if (ProbeFlags
& KSPROBE_PROBEANDLOCK
)
681 if (Irp
->MdlAddress
->MdlFlags
& (MDL_PAGES_LOCKED
| MDL_SOURCE_IS_NONPAGED_POOL
))
683 if (ProbeFlags
& KSPROBE_SYSTEMADDRESS
)
687 /* loop through all mdls and probe them */
688 Mdl
= Irp
->MdlAddress
;
691 /* the mapping can fail */
692 Mdl
->MdlFlags
|= MDL_MAPPING_CAN_FAIL
;
694 if (Mdl
->MdlFlags
& (MDL_MAPPED_TO_SYSTEM_VA
| MDL_SOURCE_IS_NONPAGED_POOL
))
696 /* no need to probe these pages */
697 Buffer
= Mdl
->MappedSystemVa
;
702 Buffer
= MmMapLockedPages(Mdl
, KernelMode
);
705 /* check if the mapping succeeded */
708 /* raise exception we'll catch */
709 ExRaiseStatus(STATUS_INSUFFICIENT_RESOURCES
);
712 /* iterate to next mdl */
717 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
719 /* Exception, get the error code */
720 Status
= _SEH2_GetExceptionCode();
728 /* loop through all mdls and probe them */
729 Mdl
= Irp
->MdlAddress
;
731 /* determine operation */
732 if (!(ProbeFlags
& KSPROBE_STREAMWRITE
) || (ProbeFlags
& KSPROBE_MODIFY
))
734 /* operation is read / modify stream, need write access */
735 Operation
= IoWriteAccess
;
739 /* operation is write to device, so we need read access */
740 Operation
= IoReadAccess
;
745 /* probe the pages */
746 MmProbeAndLockPages(Mdl
, Irp
->RequestorMode
, Operation
);
748 if (ProbeFlags
& KSPROBE_SYSTEMADDRESS
)
750 /* the mapping can fail */
751 Mdl
->MdlFlags
|= MDL_MAPPING_CAN_FAIL
;
753 if (Mdl
->MdlFlags
& (MDL_MAPPED_TO_SYSTEM_VA
| MDL_SOURCE_IS_NONPAGED_POOL
))
755 /* no need to probe these pages */
756 Buffer
= Mdl
->MappedSystemVa
;
761 Buffer
= MmMapLockedPages(Mdl
, KernelMode
);
764 /* check if the mapping succeeded */
767 /* raise exception we'll catch */
768 ExRaiseStatus(STATUS_INSUFFICIENT_RESOURCES
);
772 /* iterate to next mdl */
777 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
779 /* Exception, get the error code */
780 Status
= _SEH2_GetExceptionCode();
787 /* check all stream headers */
788 StreamHeader
= (PKSSTREAM_HEADER
)Irp
->AssociatedIrp
.SystemBuffer
;
789 ASSERT(StreamHeader
);
796 /* does the supplied header size match stream header size and no type changed */
797 if (StreamHeader
->Size
!= HeaderSize
&& !(StreamHeader
->OptionsFlags
& KSSTREAM_HEADER_OPTIONSF_TYPECHANGED
))
799 /* invalid stream header */
800 ExRaiseStatus(STATUS_INVALID_BUFFER_SIZE
);
805 /* stream must be at least of size KSSTREAM_HEADER and size must be 8-byte block aligned */
806 if (StreamHeader
->Size
< sizeof(KSSTREAM_HEADER
) || (StreamHeader
->Size
& 7))
808 /* invalid stream header */
809 ExRaiseStatus(STATUS_INVALID_BUFFER_SIZE
);
813 if (Length
< StreamHeader
->Size
)
815 /* length is too short */
816 ExRaiseStatus(STATUS_INVALID_BUFFER_SIZE
);
819 if (ProbeFlags
& KSPROBE_STREAMWRITE
)
821 if (StreamHeader
->DataUsed
> StreamHeader
->FrameExtent
)
823 /* frame extend can never be smaller */
824 ExRaiseStatus(STATUS_INVALID_BUFFER_SIZE
);
827 /* is this stream change packet */
828 if (StreamHeader
->OptionsFlags
& KSSTREAM_HEADER_OPTIONSF_TYPECHANGED
)
830 if (Length
!= sizeof(KSSTREAM_HEADER
) || (PVOID
)StreamHeader
!= Irp
->AssociatedIrp
.SystemBuffer
)
832 /* stream changed - must be send in a single packet */
833 ExRaiseStatus(STATUS_INVALID_BUFFER_SIZE
);
836 if (!(ProbeFlags
& KSPROBE_ALLOWFORMATCHANGE
))
838 /* caller does not permit format changes */
839 ExRaiseStatus(STATUS_INVALID_PARAMETER
);
842 if (StreamHeader
->FrameExtent
)
844 /* allocate an mdl */
845 Mdl
= IoAllocateMdl(StreamHeader
->Data
, StreamHeader
->FrameExtent
, FALSE
, TRUE
, Irp
);
849 /* not enough memory */
850 ExRaiseStatus(STATUS_INSUFFICIENT_RESOURCES
);
853 /* break-out to probe for the irp */
860 if (StreamHeader
->DataUsed
)
862 /* DataUsed must be zero for stream read operation */
863 ExRaiseStatus(STATUS_INVALID_BUFFER_SIZE
);
866 if (StreamHeader
->OptionsFlags
)
868 /* no flags supported for reading */
869 ExRaiseStatus(STATUS_INVALID_PARAMETER
);
873 if (StreamHeader
->FrameExtent
)
875 /* allocate an mdl */
876 ASSERT(Irp
->MdlAddress
== NULL
);
877 Mdl
= IoAllocateMdl(StreamHeader
->Data
, StreamHeader
->FrameExtent
, FALSE
, TRUE
, Irp
);
880 /* not enough memory */
881 ExRaiseStatus(STATUS_INSUFFICIENT_RESOURCES
);
885 /* move to next stream header */
886 Length
-= StreamHeader
->Size
;
887 StreamHeader
= (PKSSTREAM_HEADER
)((ULONG_PTR
)StreamHeader
+ StreamHeader
->Size
);
890 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
892 /* Exception, get the error code */
893 Status
= _SEH2_GetExceptionCode();
896 /* now probe the allocated mdl's */
897 if (!NT_SUCCESS(Status
))
899 DPRINT("Status %x\n", Status
);
906 /* probe user mode buffers */
907 if (Length
&& ( (!HeaderSize
) || (Length
% HeaderSize
== 0) || ((ProbeFlags
& KSPROBE_ALLOWFORMATCHANGE
) && (Length
== sizeof(KSSTREAM_HEADER
))) ) )
909 /* allocate stream header buffer */
910 Irp
->AssociatedIrp
.SystemBuffer
= AllocateItem(NonPagedPool
, Length
);
912 if (!Irp
->AssociatedIrp
.SystemBuffer
)
915 return STATUS_INSUFFICIENT_RESOURCES
;
918 /* mark irp as buffered so that changes the stream headers are propagated back */
919 Irp
->Flags
= IRP_DEALLOCATE_BUFFER
| IRP_BUFFERED_IO
;
923 if (ProbeFlags
& KSPROBE_STREAMWRITE
)
925 if (ProbeFlags
& KSPROBE_MODIFY
)
926 ProbeForWrite(Irp
->UserBuffer
, Length
, sizeof(UCHAR
));
928 ProbeForRead(Irp
->UserBuffer
, Length
, sizeof(UCHAR
));
932 /* stream reads means writing */
933 ProbeForWrite(Irp
->UserBuffer
, Length
, sizeof(UCHAR
));
935 /* set input operation flags */
936 Irp
->Flags
|= IRP_INPUT_OPERATION
;
939 /* copy stream buffer */
940 RtlMoveMemory(Irp
->AssociatedIrp
.SystemBuffer
, Irp
->UserBuffer
, Length
);
942 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
944 /* Exception, get the error code */
945 Status
= _SEH2_GetExceptionCode();
948 if (!NT_SUCCESS(Status
))
954 if (ProbeFlags
& KSPROBE_ALLOCATEMDL
)
960 /* check all stream headers */
961 StreamHeader
= (PKSSTREAM_HEADER
)Irp
->AssociatedIrp
.SystemBuffer
;
969 /* does the supplied header size match stream header size and no type changed */
970 if (StreamHeader
->Size
!= HeaderSize
&& !(StreamHeader
->OptionsFlags
& KSSTREAM_HEADER_OPTIONSF_TYPECHANGED
))
972 /* invalid stream header */
973 ExRaiseStatus(STATUS_INVALID_BUFFER_SIZE
);
978 /* stream must be at least of size KSSTREAM_HEADER and size must be 8-byte block aligned */
979 if (StreamHeader
->Size
< sizeof(KSSTREAM_HEADER
) || (StreamHeader
->Size
& 7))
981 /* invalid stream header */
982 ExRaiseStatus(STATUS_INVALID_BUFFER_SIZE
);
986 if (Length
< StreamHeader
->Size
)
988 /* length is too short */
989 ExRaiseStatus(STATUS_INVALID_BUFFER_SIZE
);
992 if (ProbeFlags
& KSPROBE_STREAMWRITE
)
994 if (StreamHeader
->DataUsed
> StreamHeader
->FrameExtent
)
996 /* frame extend can never be smaller */
997 ExRaiseStatus(STATUS_INVALID_BUFFER_SIZE
);
1000 /* is this stream change packet */
1001 if (StreamHeader
->OptionsFlags
& KSSTREAM_HEADER_OPTIONSF_TYPECHANGED
)
1003 if (Length
!= sizeof(KSSTREAM_HEADER
) || (PVOID
)StreamHeader
!= Irp
->AssociatedIrp
.SystemBuffer
)
1005 /* stream changed - must be send in a single packet */
1006 ExRaiseStatus(STATUS_INVALID_BUFFER_SIZE
);
1009 if (!(ProbeFlags
& KSPROBE_ALLOWFORMATCHANGE
))
1011 /* caller does not permit format changes */
1012 ExRaiseStatus(STATUS_INVALID_PARAMETER
);
1015 if (StreamHeader
->FrameExtent
)
1017 /* allocate an mdl */
1018 Mdl
= IoAllocateMdl(StreamHeader
->Data
, StreamHeader
->FrameExtent
, FALSE
, TRUE
, Irp
);
1022 /* not enough memory */
1023 ExRaiseStatus(STATUS_INSUFFICIENT_RESOURCES
);
1026 /* break out to probe for the irp */
1027 //AllocateMdl = TRUE;
1034 if (StreamHeader
->DataUsed
)
1036 /* DataUsed must be zero for stream read operation */
1037 ExRaiseStatus(STATUS_INVALID_BUFFER_SIZE
);
1040 if (StreamHeader
->OptionsFlags
)
1042 /* no flags supported for reading */
1043 ExRaiseStatus(STATUS_INVALID_PARAMETER
);
1047 /* move to next stream header */
1048 Length
-= StreamHeader
->Size
;
1049 StreamHeader
= (PKSSTREAM_HEADER
)((ULONG_PTR
)StreamHeader
+ StreamHeader
->Size
);
1052 }_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1054 /* Exception, get the error code */
1055 Status
= _SEH2_GetExceptionCode();
1058 /* now probe the allocated mdl's */
1059 if (NT_SUCCESS(Status
))
1065 return STATUS_INVALID_BUFFER_SIZE
;
1074 KsAllocateExtraData(
1077 OUT PVOID
* ExtraBuffer
)
1079 PIO_STACK_LOCATION IoStack
;
1081 PUCHAR Buffer
, BufferOrg
;
1082 PKSSTREAM_HEADER Header
;
1083 NTSTATUS Status
= STATUS_SUCCESS
;
1085 /* get current irp stack */
1086 IoStack
= IoGetCurrentIrpStackLocation(Irp
);
1089 ASSERT(IoStack
->Parameters
.DeviceIoControl
.InputBufferLength
>= sizeof(KSSTREAM_HEADER
));
1091 /* get total length */
1092 Count
= IoStack
->Parameters
.DeviceIoControl
.InputBufferLength
/ sizeof(KSSTREAM_HEADER
);
1094 /* allocate buffer */
1095 Buffer
= BufferOrg
= AllocateItem(NonPagedPool
, Count
* (sizeof(KSSTREAM_HEADER
) + ExtraSize
));
1097 return STATUS_INSUFFICIENT_RESOURCES
;
1101 /* get input buffer */
1102 Header
= (PKSSTREAM_HEADER
)IoStack
->Parameters
.DeviceIoControl
.Type3InputBuffer
;
1103 for(Index
= 0; Index
< Count
; Index
++)
1105 /* copy stream header */
1106 RtlMoveMemory(Buffer
, Header
, sizeof(KSSTREAM_HEADER
));
1108 /* move to next header */
1110 /* increment output buffer offset */
1111 Buffer
+= sizeof(KSSTREAM_HEADER
) + ExtraSize
;
1114 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1116 /* Exception, get the error code */
1117 Status
= _SEH2_GetExceptionCode();
1121 if (!NT_SUCCESS(Status
))
1123 /* free buffer on exception */
1129 *ExtraBuffer
= BufferOrg
;
1132 return STATUS_SUCCESS
;
1142 IN PDRIVER_OBJECT DriverObject
)
1152 KsDispatchInvalidDeviceRequest(
1153 IN PDEVICE_OBJECT DeviceObject
,
1156 Irp
->IoStatus
.Status
= STATUS_INVALID_DEVICE_REQUEST
;
1157 CompleteRequest(Irp
, IO_NO_INCREMENT
);
1159 return STATUS_INVALID_DEVICE_REQUEST
;
1168 KsDefaultDeviceIoCompletion(
1169 IN PDEVICE_OBJECT DeviceObject
,
1172 PIO_STACK_LOCATION IoStack
;
1175 /* get current irp stack */
1176 IoStack
= IoGetCurrentIrpStackLocation(Irp
);
1178 if (IoStack
->Parameters
.DeviceIoControl
.IoControlCode
!= IOCTL_KS_PROPERTY
&&
1179 IoStack
->Parameters
.DeviceIoControl
.IoControlCode
!= IOCTL_KS_METHOD
&&
1180 IoStack
->Parameters
.DeviceIoControl
.IoControlCode
!= IOCTL_KS_ENABLE_EVENT
&&
1181 IoStack
->Parameters
.DeviceIoControl
.IoControlCode
!= IOCTL_KS_DISABLE_EVENT
)
1183 if (IoStack
->Parameters
.DeviceIoControl
.IoControlCode
== IOCTL_KS_RESET_STATE
)
1186 Status
= STATUS_SUCCESS
;
1190 /* request unsupported */
1191 Status
= STATUS_INVALID_DEVICE_REQUEST
;
1196 /* property / method / event not found */
1197 Status
= STATUS_PROPSET_NOT_FOUND
;
1200 /* complete request */
1201 Irp
->IoStatus
.Status
= Status
;
1202 CompleteRequest(Irp
, IO_NO_INCREMENT
);
1214 KsDispatchFastIoDeviceControlFailure(
1215 IN PFILE_OBJECT FileObject
,
1217 IN PVOID InputBuffer OPTIONAL
,
1218 IN ULONG InputBufferLength
,
1219 OUT PVOID OutputBuffer OPTIONAL
,
1220 IN ULONG OutputBufferLength
,
1221 IN ULONG IoControlCode
,
1222 OUT PIO_STATUS_BLOCK IoStatus
,
1223 IN PDEVICE_OBJECT DeviceObject
)
1234 KsDispatchFastReadFailure(
1235 IN PFILE_OBJECT FileObject
,
1236 IN PLARGE_INTEGER FileOffset
,
1241 OUT PIO_STATUS_BLOCK IoStatus
,
1242 IN PDEVICE_OBJECT DeviceObject
)
1255 IN OUT PLIST_ENTRY QueueHead
,
1256 IN PKSPIN_LOCK SpinLock
)
1258 PDRIVER_CANCEL OldDriverCancel
;
1259 PIO_STACK_LOCATION IoStack
;
1261 PLIST_ENTRY NextEntry
;
1265 /* acquire spinlock */
1266 KeAcquireSpinLock(SpinLock
, &OldLevel
);
1267 /* point to first entry */
1268 Entry
= QueueHead
->Flink
;
1269 /* loop all items */
1270 while(Entry
!= QueueHead
)
1272 /* get irp offset */
1273 Irp
= (PIRP
)CONTAINING_RECORD(Entry
, IRP
, Tail
.Overlay
.ListEntry
);
1275 /* get next entry */
1276 NextEntry
= Entry
->Flink
;
1278 /* set cancelled bit */
1281 /* now set the cancel routine */
1282 OldDriverCancel
= IoSetCancelRoutine(Irp
, NULL
);
1283 if (OldDriverCancel
)
1285 /* this irp hasnt been yet used, so free to cancel */
1286 KeReleaseSpinLock(SpinLock
, OldLevel
);
1288 /* get current irp stack */
1289 IoStack
= IoGetCurrentIrpStackLocation(Irp
);
1291 /* acquire cancel spinlock */
1292 IoAcquireCancelSpinLock(&Irp
->CancelIrql
);
1294 /* call provided cancel routine */
1295 OldDriverCancel(IoStack
->DeviceObject
, Irp
);
1297 /* re-acquire spinlock */
1298 KeAcquireSpinLock(SpinLock
, &OldLevel
);
1301 /* move on to next entry */
1305 /* the irp has already been canceled */
1306 KeReleaseSpinLock(SpinLock
, OldLevel
);
1316 KsReleaseIrpOnCancelableQueue(
1318 IN PDRIVER_CANCEL DriverCancel OPTIONAL
)
1320 PKSPIN_LOCK SpinLock
;
1321 PDRIVER_CANCEL OldDriverCancel
;
1322 PIO_STACK_LOCATION IoStack
;
1325 /* check for required parameters */
1331 /* default to KsCancelRoutine */
1332 DriverCancel
= KsCancelRoutine
;
1335 /* get current irp stack */
1336 IoStack
= IoGetCurrentIrpStackLocation(Irp
);
1338 /* get internal queue lock */
1339 SpinLock
= KSQUEUE_SPINLOCK_IRP_STORAGE(Irp
);
1341 /* acquire spinlock */
1342 KeAcquireSpinLock(SpinLock
, &OldLevel
);
1344 /* now set the cancel routine */
1345 OldDriverCancel
= IoSetCancelRoutine(Irp
, DriverCancel
);
1347 if (Irp
->Cancel
&& OldDriverCancel
== NULL
)
1349 /* the irp has already been canceled */
1350 KeReleaseSpinLock(SpinLock
, OldLevel
);
1352 /* cancel routine requires that cancel spinlock is held */
1353 IoAcquireCancelSpinLock(&Irp
->CancelIrql
);
1356 DriverCancel(IoStack
->DeviceObject
, Irp
);
1361 KeReleaseSpinLock(SpinLock
, OldLevel
);
1371 KsRemoveIrpFromCancelableQueue(
1372 IN OUT PLIST_ENTRY QueueHead
,
1373 IN PKSPIN_LOCK SpinLock
,
1374 IN KSLIST_ENTRY_LOCATION ListLocation
,
1375 IN KSIRP_REMOVAL_OPERATION RemovalOperation
)
1378 PLIST_ENTRY CurEntry
;
1381 DPRINT("KsRemoveIrpFromCancelableQueue ListHead %p SpinLock %p ListLocation %x RemovalOperation %x\n", QueueHead
, SpinLock
, ListLocation
, RemovalOperation
);
1383 /* check parameters */
1384 if (!QueueHead
|| !SpinLock
)
1387 /* check if parameter ListLocation is valid */
1388 if (ListLocation
!= KsListEntryTail
&& ListLocation
!= KsListEntryHead
)
1391 /* acquire list lock */
1392 KeAcquireSpinLock(SpinLock
, &OldIrql
);
1394 /* point to queue head */
1395 CurEntry
= QueueHead
;
1399 /* reset irp to null */
1402 /* iterate to next entry */
1403 if (ListLocation
== KsListEntryHead
)
1404 CurEntry
= CurEntry
->Flink
;
1406 CurEntry
= CurEntry
->Blink
;
1408 /* is the end of list reached */
1409 if (CurEntry
== QueueHead
)
1411 /* reached end of list */
1415 /* get irp offset */
1416 Irp
= (PIRP
)CONTAINING_RECORD(CurEntry
, IRP
, Tail
.Overlay
.ListEntry
);
1420 /* irp has been canceled */
1424 if (Irp
->CancelRoutine
)
1426 /* remove cancel routine */
1427 Irp
->CancelRoutine
= NULL
;
1429 if (RemovalOperation
== KsAcquireAndRemove
|| RemovalOperation
== KsAcquireAndRemoveOnlySingleItem
)
1431 /* remove irp from list */
1432 RemoveEntryList(&Irp
->Tail
.Overlay
.ListEntry
);
1435 if (RemovalOperation
== KsAcquireAndRemoveOnlySingleItem
|| RemovalOperation
== KsAcquireOnlySingleItem
)
1442 KeReleaseSpinLock(SpinLock
, OldIrql
);
1444 if (!Irp
|| Irp
->CancelRoutine
== NULL
)
1446 /* either an irp has been acquired or nothing found */
1450 /* time to remove the canceled irp */
1451 IoAcquireCancelSpinLock(&OldIrql
);
1452 /* acquire list lock */
1453 KeAcquireSpinLockAtDpcLevel(SpinLock
);
1455 if (RemovalOperation
== KsAcquireAndRemove
|| RemovalOperation
== KsAcquireAndRemoveOnlySingleItem
)
1458 RemoveEntryList(&Irp
->Tail
.Overlay
.ListEntry
);
1461 /* release list lock */
1462 KeReleaseSpinLockFromDpcLevel(SpinLock
);
1464 /* release cancel spinlock */
1465 IoReleaseCancelSpinLock(OldIrql
);
1466 /* no non canceled irp has been found */
1476 KsMoveIrpsOnCancelableQueue(
1477 IN OUT PLIST_ENTRY SourceList
,
1478 IN PKSPIN_LOCK SourceLock
,
1479 IN OUT PLIST_ENTRY DestinationList
,
1480 IN PKSPIN_LOCK DestinationLock OPTIONAL
,
1481 IN KSLIST_ENTRY_LOCATION ListLocation
,
1482 IN PFNKSIRPLISTCALLBACK ListCallback
,
1486 PLIST_ENTRY SrcEntry
;
1488 NTSTATUS Status
= STATUS_SUCCESS
;
1490 if (!DestinationLock
)
1492 /* no destination lock just acquire the source lock */
1493 KeAcquireSpinLock(SourceLock
, &OldLevel
);
1497 /* acquire cancel spinlock */
1498 IoAcquireCancelSpinLock(&OldLevel
);
1500 /* now acquire source lock */
1501 KeAcquireSpinLockAtDpcLevel(SourceLock
);
1503 /* now acquire destination lock */
1504 KeAcquireSpinLockAtDpcLevel(DestinationLock
);
1507 /* point to list head */
1508 SrcEntry
= SourceList
;
1510 /* now move all irps */
1513 if (ListLocation
== KsListEntryTail
)
1515 /* move queue downwards */
1516 SrcEntry
= SrcEntry
->Flink
;
1520 /* move queue upwards */
1521 SrcEntry
= SrcEntry
->Blink
;
1524 if (SrcEntry
== SourceList
)
1526 /* eof list reached */
1530 /* get irp offset */
1531 Irp
= (PIRP
)CONTAINING_RECORD(SrcEntry
, IRP
, Tail
.Overlay
.ListEntry
);
1533 /* now check if irp can be moved */
1534 Status
= ListCallback(Irp
, Context
);
1536 /* check if irp can be moved */
1537 if (Status
== STATUS_SUCCESS
)
1539 /* remove irp from src list */
1540 RemoveEntryList(&Irp
->Tail
.Overlay
.ListEntry
);
1542 if (ListLocation
== KsListEntryTail
)
1544 /* insert irp end of list */
1545 InsertTailList(DestinationList
, &Irp
->Tail
.Overlay
.ListEntry
);
1549 /* insert irp head of list */
1550 InsertHeadList(DestinationList
, &Irp
->Tail
.Overlay
.ListEntry
);
1553 /* do we need to update the irp lock */
1554 if (DestinationLock
)
1556 /* update irp lock */
1557 KSQUEUE_SPINLOCK_IRP_STORAGE(Irp
) = DestinationLock
;
1562 if (Status
!= STATUS_NO_MATCH
)
1564 /* callback decided to stop enumeration */
1568 /* reset return value */
1569 Status
= STATUS_SUCCESS
;
1573 if (!DestinationLock
)
1575 /* release source lock */
1576 KeReleaseSpinLock(SourceLock
, OldLevel
);
1580 /* now release destination lock */
1581 KeReleaseSpinLockFromDpcLevel(DestinationLock
);
1583 /* now release source lock */
1584 KeReleaseSpinLockFromDpcLevel(SourceLock
);
1587 /* now release cancel spinlock */
1588 IoReleaseCancelSpinLock(OldLevel
);
1601 KsRemoveSpecificIrpFromCancelableQueue(
1604 PKSPIN_LOCK SpinLock
;
1607 DPRINT("KsRemoveSpecificIrpFromCancelableQueue %p\n", Irp
);
1609 /* get internal queue lock */
1610 SpinLock
= KSQUEUE_SPINLOCK_IRP_STORAGE(Irp
);
1612 /* acquire spinlock */
1613 KeAcquireSpinLock(SpinLock
, &OldLevel
);
1615 /* remove the irp from the list */
1616 RemoveEntryList(&Irp
->Tail
.Overlay
.ListEntry
);
1618 /* release spinlock */
1619 KeReleaseSpinLock(SpinLock
, OldLevel
);
1629 KsAddIrpToCancelableQueue(
1630 IN OUT PLIST_ENTRY QueueHead
,
1631 IN PKSPIN_LOCK SpinLock
,
1633 IN KSLIST_ENTRY_LOCATION ListLocation
,
1634 IN PDRIVER_CANCEL DriverCancel OPTIONAL
)
1636 PDRIVER_CANCEL OldDriverCancel
;
1637 PIO_STACK_LOCATION IoStack
;
1640 /* check for required parameters */
1641 if (!QueueHead
|| !SpinLock
|| !Irp
)
1644 /* get current irp stack */
1645 IoStack
= IoGetCurrentIrpStackLocation(Irp
);
1647 DPRINT("KsAddIrpToCancelableQueue QueueHead %p SpinLock %p Irp %p ListLocation %x DriverCancel %p\n", QueueHead
, SpinLock
, Irp
, ListLocation
, DriverCancel
);
1649 // HACK for ms portcls
1650 if (IoStack
->MajorFunction
== IRP_MJ_CREATE
)
1652 // complete the request
1653 DPRINT1("MS HACK\n");
1654 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
1655 CompleteRequest(Irp
, IO_NO_INCREMENT
);
1663 /* default to KsCancelRoutine */
1664 DriverCancel
= KsCancelRoutine
;
1668 /* acquire spinlock */
1669 KeAcquireSpinLock(SpinLock
, &OldLevel
);
1671 if (ListLocation
== KsListEntryTail
)
1673 /* insert irp to tail of list */
1674 InsertTailList(QueueHead
, &Irp
->Tail
.Overlay
.ListEntry
);
1678 /* insert irp to head of list */
1679 InsertHeadList(QueueHead
, &Irp
->Tail
.Overlay
.ListEntry
);
1682 /* store internal queue lock */
1683 KSQUEUE_SPINLOCK_IRP_STORAGE(Irp
) = SpinLock
;
1685 /* now set the cancel routine */
1686 OldDriverCancel
= IoSetCancelRoutine(Irp
, DriverCancel
);
1688 if (Irp
->Cancel
&& OldDriverCancel
== NULL
)
1690 /* the irp has already been canceled */
1691 KeReleaseSpinLock(SpinLock
, OldLevel
);
1693 /* cancel routine requires that cancel spinlock is held */
1694 IoAcquireCancelSpinLock(&Irp
->CancelIrql
);
1697 DriverCancel(IoStack
->DeviceObject
, Irp
);
1702 KeReleaseSpinLock(SpinLock
, OldLevel
);
1713 IN PDEVICE_OBJECT DeviceObject
,
1716 PKSPIN_LOCK SpinLock
;
1718 /* get internal queue lock */
1719 SpinLock
= KSQUEUE_SPINLOCK_IRP_STORAGE(Irp
);
1721 /* acquire spinlock */
1722 KeAcquireSpinLockAtDpcLevel(SpinLock
);
1725 ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL
);
1727 /* release cancel spinlock */
1728 IoReleaseCancelSpinLock(Irp
->CancelIrql
);
1730 /* remove the irp from the list */
1731 RemoveEntryList(&Irp
->Tail
.Overlay
.ListEntry
);
1733 /* release spinlock */
1734 KeReleaseSpinLock(SpinLock
, Irp
->CancelIrql
);
1736 /* has the irp already been canceled */
1737 if (Irp
->IoStatus
.Status
!= STATUS_CANCELLED
)
1739 /* let's complete it */
1740 Irp
->IoStatus
.Status
= STATUS_CANCELLED
;
1741 CompleteRequest(Irp
, IO_NO_INCREMENT
);
1746 FindMatchingCreateItem(
1747 PLIST_ENTRY ListHead
,
1748 PUNICODE_STRING String
,
1749 OUT PCREATE_ITEM_ENTRY
*OutCreateItem
)
1752 PCREATE_ITEM_ENTRY CreateItemEntry
;
1753 UNICODE_STRING RefString
;
1757 /* Copy the input string */
1758 RefString
= *String
;
1760 /* Check if the string starts with a backslash */
1761 if (String
->Buffer
[0] == L
'\\')
1763 /* Skip backslash */
1765 RefString
.Length
-= sizeof(WCHAR
);
1769 /* get terminator */
1770 pStr
= String
->Buffer
;
1771 Count
= String
->Length
/ sizeof(WCHAR
);
1772 while ((Count
> 0) && (*pStr
!= L
'\\'))
1781 // request is for pin / node / allocator
1782 RefString
.Length
= (USHORT
)((PCHAR
)pStr
- (PCHAR
)String
->Buffer
);
1785 /* point to first entry */
1786 Entry
= ListHead
->Flink
;
1788 /* loop all device items */
1789 while (Entry
!= ListHead
)
1791 /* get create item entry */
1792 CreateItemEntry
= (PCREATE_ITEM_ENTRY
)CONTAINING_RECORD(Entry
,
1796 ASSERT(CreateItemEntry
->CreateItem
);
1798 if(CreateItemEntry
->CreateItem
->Flags
& KSCREATE_ITEM_WILDCARD
)
1800 /* create item is default */
1801 *OutCreateItem
= CreateItemEntry
;
1802 return STATUS_SUCCESS
;
1805 if (!CreateItemEntry
->CreateItem
->Create
)
1807 /* skip free create item */
1808 Entry
= Entry
->Flink
;
1812 DPRINT("CreateItem %S Length %u Request %wZ %u\n",
1813 CreateItemEntry
->CreateItem
->ObjectClass
.Buffer
,
1814 CreateItemEntry
->CreateItem
->ObjectClass
.Length
,
1818 if (CreateItemEntry
->CreateItem
->ObjectClass
.Length
> RefString
.Length
)
1820 /* create item doesnt match in length */
1821 Entry
= Entry
->Flink
;
1825 /* now check if the object class is the same */
1826 if (!RtlCompareUnicodeString(&CreateItemEntry
->CreateItem
->ObjectClass
,
1830 /* found matching create item */
1831 *OutCreateItem
= CreateItemEntry
;
1832 return STATUS_SUCCESS
;
1834 /* iterate to next */
1835 Entry
= Entry
->Flink
;
1838 return STATUS_NOT_FOUND
;
1844 IN PDEVICE_OBJECT DeviceObject
,
1847 PCREATE_ITEM_ENTRY CreateItemEntry
;
1848 PIO_STACK_LOCATION IoStack
;
1849 PDEVICE_EXTENSION DeviceExtension
;
1850 PKSIDEVICE_HEADER DeviceHeader
;
1851 PKSIOBJECT_HEADER ObjectHeader
;
1854 DPRINT("KS / CREATE\n");
1856 /* get current stack location */
1857 IoStack
= IoGetCurrentIrpStackLocation(Irp
);
1858 /* get device extension */
1859 DeviceExtension
= (PDEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
1860 /* get device header */
1861 DeviceHeader
= DeviceExtension
->DeviceHeader
;
1864 if (IoStack
->FileObject
->FileName
.Buffer
== NULL
)
1866 /* FIXME Pnp-Issue */
1867 DPRINT("Using reference string hack\n");
1868 Irp
->IoStatus
.Information
= 0;
1869 /* set return status */
1870 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
1871 CompleteRequest(Irp
, IO_NO_INCREMENT
);
1872 return STATUS_SUCCESS
;
1875 if (IoStack
->FileObject
->RelatedFileObject
!= NULL
)
1877 /* request is to instantiate a pin / node / clock / allocator */
1878 ObjectHeader
= (PKSIOBJECT_HEADER
)IoStack
->FileObject
->RelatedFileObject
->FsContext2
;
1881 ASSERT(ObjectHeader
);
1883 /* find a matching a create item */
1884 Status
= FindMatchingCreateItem(&ObjectHeader
->ItemList
,
1885 &IoStack
->FileObject
->FileName
,
1890 /* request to create a filter */
1891 Status
= FindMatchingCreateItem(&DeviceHeader
->ItemList
,
1892 &IoStack
->FileObject
->FileName
,
1896 if (NT_SUCCESS(Status
))
1898 /* set object create item */
1899 KSCREATE_ITEM_IRP_STORAGE(Irp
) = CreateItemEntry
->CreateItem
;
1901 /* call create function */
1902 Status
= CreateItemEntry
->CreateItem
->Create(DeviceObject
, Irp
);
1904 if (NT_SUCCESS(Status
))
1906 /* increment create item reference count */
1907 InterlockedIncrement(&CreateItemEntry
->ReferenceCount
);
1912 Irp
->IoStatus
.Information
= 0;
1913 /* set return status */
1914 Irp
->IoStatus
.Status
= STATUS_UNSUCCESSFUL
;
1915 CompleteRequest(Irp
, IO_NO_INCREMENT
);
1916 return STATUS_UNSUCCESSFUL
;
1922 IN PDEVICE_OBJECT DeviceObject
,
1925 PIO_STACK_LOCATION IoStack
;
1926 //PDEVICE_EXTENSION DeviceExtension;
1927 PKSIOBJECT_HEADER ObjectHeader
;
1928 //PKSIDEVICE_HEADER DeviceHeader;
1929 PDRIVER_DISPATCH Dispatch
;
1932 /* get current stack location */
1933 IoStack
= IoGetCurrentIrpStackLocation(Irp
);
1935 /* get device extension */
1936 //DeviceExtension = (PDEVICE_EXTENSION)DeviceObject->DeviceExtension;
1937 /* get device header */
1938 //DeviceHeader = DeviceExtension->DeviceHeader;
1940 ASSERT(IoStack
->FileObject
);
1942 /* get object header */
1943 ObjectHeader
= (PKSIOBJECT_HEADER
) IoStack
->FileObject
->FsContext2
;
1947 /* FIXME Pnp-Issue*/
1948 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
1949 Irp
->IoStatus
.Information
= 0;
1950 /* complete and forget */
1951 CompleteRequest(Irp
, IO_NO_INCREMENT
);
1952 return STATUS_SUCCESS
;
1956 ASSERT(ObjectHeader
);
1957 /* store create item */
1958 //KSCREATE_ITEM_IRP_STORAGE(Irp) = (PKSOBJECT_CREATE_ITEM)0x12345678; //ObjectHeader->CreateItem;
1960 /* retrieve matching dispatch function */
1961 switch(IoStack
->MajorFunction
)
1964 Dispatch
= ObjectHeader
->DispatchTable
.Close
;
1966 case IRP_MJ_DEVICE_CONTROL
:
1967 Dispatch
= ObjectHeader
->DispatchTable
.DeviceIoControl
;
1970 Dispatch
= ObjectHeader
->DispatchTable
.Read
;
1973 Dispatch
= ObjectHeader
->DispatchTable
.Write
;
1975 case IRP_MJ_FLUSH_BUFFERS
:
1976 Dispatch
= ObjectHeader
->DispatchTable
.Flush
;
1978 case IRP_MJ_QUERY_SECURITY
:
1979 Dispatch
= ObjectHeader
->DispatchTable
.QuerySecurity
;
1981 case IRP_MJ_SET_SECURITY
:
1982 Dispatch
= ObjectHeader
->DispatchTable
.SetSecurity
;
1985 Dispatch
= KsDefaultDispatchPnp
;
1991 /* is the request supported */
1994 /* now call the dispatch function */
1995 Status
= Dispatch(DeviceObject
, Irp
);
1999 /* not supported request */
2000 Status
= KsDispatchInvalidDeviceRequest(DeviceObject
, Irp
);
2013 KsSetMajorFunctionHandler(
2014 IN PDRIVER_OBJECT DriverObject
,
2015 IN ULONG MajorFunction
)
2017 DPRINT("KsSetMajorFunctionHandler Function %x\n", MajorFunction
);
2019 switch ( MajorFunction
)
2022 DriverObject
->MajorFunction
[MajorFunction
] = KspCreate
;
2024 case IRP_MJ_DEVICE_CONTROL
:
2028 case IRP_MJ_FLUSH_BUFFERS
:
2029 case IRP_MJ_QUERY_SECURITY
:
2030 case IRP_MJ_SET_SECURITY
:
2031 DriverObject
->MajorFunction
[MajorFunction
] = KspDispatchIrp
;
2034 DPRINT1("NotSupported %x\n", MajorFunction
);
2035 return STATUS_INVALID_PARAMETER
;
2038 return STATUS_SUCCESS
;
2048 IN PDEVICE_OBJECT DeviceObject
,
2051 PIO_STACK_LOCATION IoStack
;
2052 PKSIDEVICE_HEADER DeviceHeader
;
2053 PDEVICE_EXTENSION DeviceExtension
;
2055 DPRINT("KsDispatchIrp DeviceObject %p Irp %p\n", DeviceObject
, Irp
);
2057 /* get device extension */
2058 DeviceExtension
= (PDEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
2060 /* get device header */
2061 DeviceHeader
= DeviceExtension
->DeviceHeader
;
2064 /* get current irp stack */
2065 IoStack
= IoGetCurrentIrpStackLocation(Irp
);
2067 if (IoStack
->MajorFunction
<= IRP_MJ_DEVICE_CONTROL
)
2069 if (IoStack
->MajorFunction
== IRP_MJ_CREATE
)
2071 /* check internal type */
2072 if (DeviceHeader
->BasicHeader
.OuterUnknown
) /* FIXME improve check */
2074 /* AVStream client */
2075 return IKsDevice_Create(DeviceObject
, Irp
);
2079 /* external client (portcls) */
2080 return KspCreate(DeviceObject
, Irp
);
2084 switch (IoStack
->MajorFunction
)
2089 case IRP_MJ_FLUSH_BUFFERS
:
2090 case IRP_MJ_QUERY_SECURITY
:
2091 case IRP_MJ_SET_SECURITY
:
2093 case IRP_MJ_DEVICE_CONTROL
:
2094 return KspDispatchIrp(DeviceObject
, Irp
);
2096 return KsDispatchInvalidDeviceRequest(DeviceObject
, Irp
);
2100 /* dispatch power */
2101 if (IoStack
->MajorFunction
== IRP_MJ_POWER
)
2103 /* check internal type */
2104 if (DeviceHeader
->BasicHeader
.OuterUnknown
) /* FIXME improve check */
2106 /* AVStream client */
2107 return IKsDevice_Power(DeviceObject
, Irp
);
2111 /* external client (portcls) */
2112 return KsDefaultDispatchPower(DeviceObject
, Irp
);
2115 else if (IoStack
->MajorFunction
== IRP_MJ_PNP
) /* dispatch pnp */
2117 /* check internal type */
2118 if (DeviceHeader
->BasicHeader
.OuterUnknown
) /* FIXME improve check */
2120 /* AVStream client */
2121 return IKsDevice_Pnp(DeviceObject
, Irp
);
2125 /* external client (portcls) */
2126 return KsDefaultDispatchPnp(DeviceObject
, Irp
);
2129 else if (IoStack
->MajorFunction
== IRP_MJ_SYSTEM_CONTROL
)
2132 return KsDefaultForwardIrp(DeviceObject
, Irp
);
2137 return KsDispatchInvalidDeviceRequest(DeviceObject
, Irp
);
2151 return KSFILTER_NODE
;