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 IoCompleteRequest(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 IoCompleteRequest(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 IoCompleteRequest(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 ExFreePool(Descriptor
);
106 /* mark create item as changed */
107 CreateItem
->Flags
|= KSCREATE_ITEM_SECURITYCHANGED
;
111 Irp
->IoStatus
.Status
= Status
;
112 IoCompleteRequest(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
)
665 /* check if alloc mdl flag is passed */
666 if (!(ProbeFlags
& KSPROBE_ALLOCATEMDL
))
668 /* nothing more to do */
669 return STATUS_SUCCESS
;
674 if (ProbeFlags
& KSPROBE_PROBEANDLOCK
)
676 if (Irp
->MdlAddress
->MdlFlags
& (MDL_PAGES_LOCKED
| MDL_SOURCE_IS_NONPAGED_POOL
))
678 if (ProbeFlags
& KSPROBE_SYSTEMADDRESS
)
682 /* loop through all mdls and probe them */
683 Mdl
= Irp
->MdlAddress
;
686 /* the mapping can fail */
687 Mdl
->MdlFlags
|= MDL_MAPPING_CAN_FAIL
;
689 if (Mdl
->MdlFlags
& (MDL_MAPPED_TO_SYSTEM_VA
| MDL_SOURCE_IS_NONPAGED_POOL
))
691 /* no need to probe these pages */
692 Buffer
= Mdl
->MappedSystemVa
;
697 Buffer
= MmMapLockedPages(Mdl
, KernelMode
);
700 /* check if the mapping succeeded */
703 /* raise exception we'll catch */
704 ExRaiseStatus(STATUS_INSUFFICIENT_RESOURCES
);
707 /* iterate to next mdl */
712 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
714 /* Exception, get the error code */
715 Status
= _SEH2_GetExceptionCode();
723 /* loop through all mdls and probe them */
724 Mdl
= Irp
->MdlAddress
;
726 /* determine operation */
727 Operation
= (ProbeFlags
& KSPROBE_STREAMWRITE
) ? IoWriteAccess
: IoReadAccess
;
731 /* probe the pages */
732 MmProbeAndLockPages(Mdl
, Irp
->RequestorMode
, Operation
);
734 if (ProbeFlags
& KSPROBE_SYSTEMADDRESS
)
736 /* the mapping can fail */
737 Mdl
->MdlFlags
|= MDL_MAPPING_CAN_FAIL
;
739 if (Mdl
->MdlFlags
& (MDL_MAPPED_TO_SYSTEM_VA
| MDL_SOURCE_IS_NONPAGED_POOL
))
741 /* no need to probe these pages */
742 Buffer
= Mdl
->MappedSystemVa
;
747 Buffer
= MmMapLockedPages(Mdl
, KernelMode
);
750 /* check if the mapping succeeded */
753 /* raise exception we'll catch */
754 ExRaiseStatus(STATUS_INSUFFICIENT_RESOURCES
);
758 /* iterate to next mdl */
763 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
765 /* Exception, get the error code */
766 Status
= _SEH2_GetExceptionCode();
773 /* check all stream headers */
774 StreamHeader
= (PKSSTREAM_HEADER
)Irp
->AssociatedIrp
.SystemBuffer
;
775 ASSERT(StreamHeader
);
782 /* does the supplied header size match stream header size and no type changed */
783 if (StreamHeader
->Size
!= HeaderSize
&& !(StreamHeader
->OptionsFlags
& KSSTREAM_HEADER_OPTIONSF_TYPECHANGED
))
785 /* invalid stream header */
786 ExRaiseStatus(STATUS_INVALID_BUFFER_SIZE
);
791 /* stream must be at least of size KSSTREAM_HEADER and size must be 8-byte block aligned */
792 if (StreamHeader
->Size
< sizeof(KSSTREAM_HEADER
) || (StreamHeader
->Size
& 7))
794 /* invalid stream header */
795 ExRaiseStatus(STATUS_INVALID_BUFFER_SIZE
);
799 if (Length
< StreamHeader
->Size
)
801 /* length is too short */
802 ExRaiseStatus(STATUS_INVALID_BUFFER_SIZE
);
805 if (ProbeFlags
& KSPROBE_STREAMWRITE
)
807 if (StreamHeader
->DataUsed
> StreamHeader
->FrameExtent
)
809 /* frame extend can never be smaller */
810 ExRaiseStatus(STATUS_INVALID_BUFFER_SIZE
);
813 /* is this stream change packet */
814 if (StreamHeader
->OptionsFlags
& KSSTREAM_HEADER_OPTIONSF_TYPECHANGED
)
816 if (Length
!= sizeof(KSSTREAM_HEADER
) || (PVOID
)StreamHeader
!= Irp
->AssociatedIrp
.SystemBuffer
)
818 /* stream changed - must be send in a single packet */
819 ExRaiseStatus(STATUS_INVALID_BUFFER_SIZE
);
822 if (!(ProbeFlags
& KSPROBE_ALLOWFORMATCHANGE
))
824 /* caller does not permit format changes */
825 ExRaiseStatus(STATUS_INVALID_PARAMETER
);
828 if (StreamHeader
->FrameExtent
)
830 /* allocate an mdl */
831 Mdl
= IoAllocateMdl(StreamHeader
->Data
, StreamHeader
->FrameExtent
, FALSE
, TRUE
, Irp
);
835 /* not enough memory */
836 ExRaiseStatus(STATUS_INSUFFICIENT_RESOURCES
);
839 /* break-out to probe for the irp */
846 if (StreamHeader
->DataUsed
)
848 /* DataUsed must be zero for stream read operation */
849 ExRaiseStatus(STATUS_INVALID_BUFFER_SIZE
);
852 if (StreamHeader
->OptionsFlags
)
854 /* no flags supported for reading */
855 ExRaiseStatus(STATUS_INVALID_PARAMETER
);
859 if (StreamHeader
->FrameExtent
)
861 /* allocate an mdl */
862 Mdl
= IoAllocateMdl(StreamHeader
->Data
, StreamHeader
->FrameExtent
, Irp
->MdlAddress
!= NULL
, TRUE
, Irp
);
865 /* not enough memory */
866 ExRaiseStatus(STATUS_INSUFFICIENT_RESOURCES
);
870 /* move to next stream header */
871 Length
-= StreamHeader
->Size
;
872 StreamHeader
= (PKSSTREAM_HEADER
)((ULONG_PTR
)StreamHeader
+ StreamHeader
->Size
);
875 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
877 /* Exception, get the error code */
878 Status
= _SEH2_GetExceptionCode();
881 /* now probe the allocated mdl's */
882 if (!NT_SUCCESS(Status
))
884 DPRINT("Status %x\n", Status
);
892 // HACK for MS PORTCLS
895 /* probe user mode buffers */
896 if (Length
&& ( (!HeaderSize
) || (Length
% HeaderSize
== 0) || ((ProbeFlags
& KSPROBE_ALLOWFORMATCHANGE
) && (Length
== sizeof(KSSTREAM_HEADER
))) ) )
898 /* allocate stream header buffer */
899 Irp
->AssociatedIrp
.SystemBuffer
= ExAllocatePool(NonPagedPool
, Length
);
901 if (!Irp
->AssociatedIrp
.SystemBuffer
)
904 return STATUS_INSUFFICIENT_RESOURCES
;
909 if (ProbeFlags
& KSPROBE_STREAMWRITE
)
911 if (ProbeFlags
& KSPROBE_MODIFY
)
912 ProbeForWrite(Irp
->UserBuffer
, Length
, sizeof(UCHAR
));
914 ProbeForRead(Irp
->UserBuffer
, Length
, sizeof(UCHAR
));
918 /* stream reads means writing */
919 ProbeForWrite(Irp
->UserBuffer
, Length
, sizeof(UCHAR
));
922 /* copy stream buffer */
923 RtlMoveMemory(Irp
->AssociatedIrp
.SystemBuffer
, Irp
->UserBuffer
, Length
);
925 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
927 /* Exception, get the error code */
928 Status
= _SEH2_GetExceptionCode();
931 if (!NT_SUCCESS(Status
))
937 if (ProbeFlags
& KSPROBE_ALLOCATEMDL
)
943 /* check all stream headers */
944 StreamHeader
= (PKSSTREAM_HEADER
)Irp
->AssociatedIrp
.SystemBuffer
;
952 /* does the supplied header size match stream header size and no type changed */
953 if (StreamHeader
->Size
!= HeaderSize
&& !(StreamHeader
->OptionsFlags
& KSSTREAM_HEADER_OPTIONSF_TYPECHANGED
))
955 /* invalid stream header */
956 ExRaiseStatus(STATUS_INVALID_BUFFER_SIZE
);
961 /* stream must be at least of size KSSTREAM_HEADER and size must be 8-byte block aligned */
962 if (StreamHeader
->Size
< sizeof(KSSTREAM_HEADER
) || (StreamHeader
->Size
& 7))
964 /* invalid stream header */
965 ExRaiseStatus(STATUS_INVALID_BUFFER_SIZE
);
969 if (Length
< StreamHeader
->Size
)
971 /* length is too short */
972 ExRaiseStatus(STATUS_INVALID_BUFFER_SIZE
);
975 if (ProbeFlags
& KSPROBE_STREAMWRITE
)
977 if (StreamHeader
->DataUsed
> StreamHeader
->FrameExtent
)
979 /* frame extend can never be smaller */
980 ExRaiseStatus(STATUS_INVALID_BUFFER_SIZE
);
983 /* is this stream change packet */
984 if (StreamHeader
->OptionsFlags
& KSSTREAM_HEADER_OPTIONSF_TYPECHANGED
)
986 if (Length
!= sizeof(KSSTREAM_HEADER
) || (PVOID
)StreamHeader
!= Irp
->AssociatedIrp
.SystemBuffer
)
988 /* stream changed - must be send in a single packet */
989 ExRaiseStatus(STATUS_INVALID_BUFFER_SIZE
);
992 if (!(ProbeFlags
& KSPROBE_ALLOWFORMATCHANGE
))
994 /* caller does not permit format changes */
995 ExRaiseStatus(STATUS_INVALID_PARAMETER
);
998 if (StreamHeader
->FrameExtent
)
1000 /* allocate an mdl */
1001 Mdl
= IoAllocateMdl(StreamHeader
->Data
, StreamHeader
->FrameExtent
, FALSE
, TRUE
, Irp
);
1005 /* not enough memory */
1006 ExRaiseStatus(STATUS_INSUFFICIENT_RESOURCES
);
1009 /* break out to probe for the irp */
1017 if (StreamHeader
->DataUsed
)
1019 /* DataUsed must be zero for stream read operation */
1020 ExRaiseStatus(STATUS_INVALID_BUFFER_SIZE
);
1023 if (StreamHeader
->OptionsFlags
)
1025 /* no flags supported for reading */
1026 ExRaiseStatus(STATUS_INVALID_PARAMETER
);
1030 /* move to next stream header */
1031 Length
-= StreamHeader
->Size
;
1032 StreamHeader
= (PKSSTREAM_HEADER
)((ULONG_PTR
)StreamHeader
+ StreamHeader
->Size
);
1035 }_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1037 /* Exception, get the error code */
1038 Status
= _SEH2_GetExceptionCode();
1041 /* now probe the allocated mdl's */
1042 if (NT_SUCCESS(Status
))
1048 return STATUS_INVALID_BUFFER_SIZE
;
1057 KsAllocateExtraData(
1060 OUT PVOID
* ExtraBuffer
)
1062 PIO_STACK_LOCATION IoStack
;
1064 PUCHAR Buffer
, BufferOrg
;
1065 PKSSTREAM_HEADER Header
;
1066 NTSTATUS Status
= STATUS_SUCCESS
;
1068 /* get current irp stack */
1069 IoStack
= IoGetCurrentIrpStackLocation(Irp
);
1072 ASSERT(IoStack
->Parameters
.DeviceIoControl
.InputBufferLength
>= sizeof(KSSTREAM_HEADER
));
1074 /* get total length */
1075 Count
= IoStack
->Parameters
.DeviceIoControl
.InputBufferLength
/ sizeof(KSSTREAM_HEADER
);
1077 /* allocate buffer */
1078 Buffer
= BufferOrg
= AllocateItem(NonPagedPool
, Count
* (sizeof(KSSTREAM_HEADER
) + ExtraSize
));
1080 return STATUS_INSUFFICIENT_RESOURCES
;
1084 /* get input buffer */
1085 Header
= (PKSSTREAM_HEADER
)IoStack
->Parameters
.DeviceIoControl
.Type3InputBuffer
;
1086 for(Index
= 0; Index
< Count
; Index
++)
1088 /* copy stream header */
1089 RtlMoveMemory(Buffer
, Header
, sizeof(KSSTREAM_HEADER
));
1091 /* move to next header */
1093 /* increment output buffer offset */
1094 Buffer
+= sizeof(KSSTREAM_HEADER
) + ExtraSize
;
1097 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1099 /* Exception, get the error code */
1100 Status
= _SEH2_GetExceptionCode();
1104 if (!NT_SUCCESS(Status
))
1106 /* free buffer on exception */
1112 *ExtraBuffer
= BufferOrg
;
1115 return STATUS_SUCCESS
;
1125 IN PDRIVER_OBJECT DriverObject
)
1135 KsDispatchInvalidDeviceRequest(
1136 IN PDEVICE_OBJECT DeviceObject
,
1139 Irp
->IoStatus
.Status
= STATUS_INVALID_DEVICE_REQUEST
;
1140 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
1142 return STATUS_INVALID_DEVICE_REQUEST
;
1151 KsDefaultDeviceIoCompletion(
1152 IN PDEVICE_OBJECT DeviceObject
,
1155 PIO_STACK_LOCATION IoStack
;
1158 /* get current irp stack */
1159 IoStack
= IoGetCurrentIrpStackLocation(Irp
);
1161 if (IoStack
->Parameters
.DeviceIoControl
.IoControlCode
!= IOCTL_KS_PROPERTY
&&
1162 IoStack
->Parameters
.DeviceIoControl
.IoControlCode
!= IOCTL_KS_METHOD
&&
1163 IoStack
->Parameters
.DeviceIoControl
.IoControlCode
!= IOCTL_KS_PROPERTY
)
1165 if (IoStack
->Parameters
.DeviceIoControl
.IoControlCode
== IOCTL_KS_RESET_STATE
)
1168 Status
= STATUS_SUCCESS
;
1172 /* request unsupported */
1173 Status
= STATUS_INVALID_DEVICE_REQUEST
;
1178 /* property / method / event not found */
1179 Status
= STATUS_PROPSET_NOT_FOUND
;
1182 /* complete request */
1183 Irp
->IoStatus
.Status
= Status
;
1184 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
1196 KsDispatchFastIoDeviceControlFailure(
1197 IN PFILE_OBJECT FileObject
,
1199 IN PVOID InputBuffer OPTIONAL
,
1200 IN ULONG InputBufferLength
,
1201 OUT PVOID OutputBuffer OPTIONAL
,
1202 IN ULONG OutputBufferLength
,
1203 IN ULONG IoControlCode
,
1204 OUT PIO_STATUS_BLOCK IoStatus
,
1205 IN PDEVICE_OBJECT DeviceObject
)
1216 KsDispatchFastReadFailure(
1217 IN PFILE_OBJECT FileObject
,
1218 IN PLARGE_INTEGER FileOffset
,
1223 OUT PIO_STATUS_BLOCK IoStatus
,
1224 IN PDEVICE_OBJECT DeviceObject
)
1237 IN OUT PLIST_ENTRY QueueHead
,
1238 IN PKSPIN_LOCK SpinLock
)
1240 PDRIVER_CANCEL OldDriverCancel
;
1241 PIO_STACK_LOCATION IoStack
;
1243 PLIST_ENTRY NextEntry
;
1247 /* acquire spinlock */
1248 KeAcquireSpinLock(SpinLock
, &OldLevel
);
1249 /* point to first entry */
1250 Entry
= QueueHead
->Flink
;
1251 /* loop all items */
1252 while(Entry
!= QueueHead
)
1254 /* get irp offset */
1255 Irp
= (PIRP
)CONTAINING_RECORD(Entry
, IRP
, Tail
.Overlay
.ListEntry
);
1257 /* get next entry */
1258 NextEntry
= Entry
->Flink
;
1260 /* set cancelled bit */
1263 /* now set the cancel routine */
1264 OldDriverCancel
= IoSetCancelRoutine(Irp
, NULL
);
1265 if (OldDriverCancel
)
1267 /* this irp hasnt been yet used, so free to cancel */
1268 KeReleaseSpinLock(SpinLock
, OldLevel
);
1270 /* get current irp stack */
1271 IoStack
= IoGetCurrentIrpStackLocation(Irp
);
1273 /* acquire cancel spinlock */
1274 IoAcquireCancelSpinLock(&Irp
->CancelIrql
);
1276 /* call provided cancel routine */
1277 OldDriverCancel(IoStack
->DeviceObject
, Irp
);
1279 /* re-acquire spinlock */
1280 KeAcquireSpinLock(SpinLock
, &OldLevel
);
1283 /* move on to next entry */
1287 /* the irp has already been canceled */
1288 KeReleaseSpinLock(SpinLock
, OldLevel
);
1298 KsReleaseIrpOnCancelableQueue(
1300 IN PDRIVER_CANCEL DriverCancel OPTIONAL
)
1302 PKSPIN_LOCK SpinLock
;
1303 PDRIVER_CANCEL OldDriverCancel
;
1304 PIO_STACK_LOCATION IoStack
;
1307 /* check for required parameters */
1313 /* default to KsCancelRoutine */
1314 DriverCancel
= KsCancelRoutine
;
1317 /* get current irp stack */
1318 IoStack
= IoGetCurrentIrpStackLocation(Irp
);
1320 /* get internal queue lock */
1321 SpinLock
= KSQUEUE_SPINLOCK_IRP_STORAGE(Irp
);
1323 /* acquire spinlock */
1324 KeAcquireSpinLock(SpinLock
, &OldLevel
);
1326 /* now set the cancel routine */
1327 OldDriverCancel
= IoSetCancelRoutine(Irp
, DriverCancel
);
1329 if (Irp
->Cancel
&& OldDriverCancel
== NULL
)
1331 /* the irp has already been canceled */
1332 KeReleaseSpinLock(SpinLock
, OldLevel
);
1334 /* cancel routine requires that cancel spinlock is held */
1335 IoAcquireCancelSpinLock(&Irp
->CancelIrql
);
1338 DriverCancel(IoStack
->DeviceObject
, Irp
);
1343 KeReleaseSpinLock(SpinLock
, OldLevel
);
1353 KsRemoveIrpFromCancelableQueue(
1354 IN OUT PLIST_ENTRY QueueHead
,
1355 IN PKSPIN_LOCK SpinLock
,
1356 IN KSLIST_ENTRY_LOCATION ListLocation
,
1357 IN KSIRP_REMOVAL_OPERATION RemovalOperation
)
1360 PLIST_ENTRY CurEntry
;
1363 //DPRINT("KsRemoveIrpFromCancelableQueue ListHead %p SpinLock %p ListLocation %x RemovalOperation %x\n", QueueHead, SpinLock, ListLocation, RemovalOperation);
1365 /* check parameters */
1366 if (!QueueHead
|| !SpinLock
)
1369 /* check if parameter ListLocation is valid */
1370 if (ListLocation
!= KsListEntryTail
&& ListLocation
!= KsListEntryHead
)
1373 /* acquire list lock */
1374 KeAcquireSpinLock(SpinLock
, &OldIrql
);
1376 /* point to queue head */
1377 CurEntry
= QueueHead
;
1381 /* reset irp to null */
1384 /* iterate to next entry */
1385 if (ListLocation
== KsListEntryHead
)
1386 CurEntry
= CurEntry
->Flink
;
1388 CurEntry
= CurEntry
->Blink
;
1390 /* is the end of list reached */
1391 if (CurEntry
== QueueHead
)
1393 /* reached end of list */
1397 /* get irp offset */
1398 Irp
= (PIRP
)CONTAINING_RECORD(CurEntry
, IRP
, Tail
.Overlay
.ListEntry
);
1402 /* irp has been canceled */
1406 if (Irp
->CancelRoutine
)
1408 /* remove cancel routine */
1409 Irp
->CancelRoutine
= NULL
;
1411 if (RemovalOperation
== KsAcquireAndRemove
|| RemovalOperation
== KsAcquireAndRemoveOnlySingleItem
)
1413 /* remove irp from list */
1414 RemoveEntryList(&Irp
->Tail
.Overlay
.ListEntry
);
1417 if (RemovalOperation
== KsAcquireAndRemoveOnlySingleItem
|| RemovalOperation
== KsAcquireOnlySingleItem
)
1424 KeReleaseSpinLock(SpinLock
, OldIrql
);
1426 if (!Irp
|| Irp
->CancelRoutine
== NULL
)
1428 /* either an irp has been acquired or nothing found */
1432 /* time to remove the canceled irp */
1433 IoAcquireCancelSpinLock(&OldIrql
);
1434 /* acquire list lock */
1435 KeAcquireSpinLockAtDpcLevel(SpinLock
);
1437 if (RemovalOperation
== KsAcquireAndRemove
|| RemovalOperation
== KsAcquireAndRemoveOnlySingleItem
)
1440 RemoveEntryList(&Irp
->Tail
.Overlay
.ListEntry
);
1443 /* release list lock */
1444 KeReleaseSpinLockFromDpcLevel(SpinLock
);
1446 /* release cancel spinlock */
1447 IoReleaseCancelSpinLock(OldIrql
);
1448 /* no non canceled irp has been found */
1458 KsMoveIrpsOnCancelableQueue(
1459 IN OUT PLIST_ENTRY SourceList
,
1460 IN PKSPIN_LOCK SourceLock
,
1461 IN OUT PLIST_ENTRY DestinationList
,
1462 IN PKSPIN_LOCK DestinationLock OPTIONAL
,
1463 IN KSLIST_ENTRY_LOCATION ListLocation
,
1464 IN PFNKSIRPLISTCALLBACK ListCallback
,
1468 PLIST_ENTRY SrcEntry
;
1470 NTSTATUS Status
= STATUS_SUCCESS
;
1472 if (!DestinationLock
)
1474 /* no destination lock just acquire the source lock */
1475 KeAcquireSpinLock(SourceLock
, &OldLevel
);
1479 /* acquire cancel spinlock */
1480 IoAcquireCancelSpinLock(&OldLevel
);
1482 /* now acquire source lock */
1483 KeAcquireSpinLockAtDpcLevel(SourceLock
);
1485 /* now acquire destination lock */
1486 KeAcquireSpinLockAtDpcLevel(DestinationLock
);
1489 /* point to list head */
1490 SrcEntry
= SourceList
;
1492 /* now move all irps */
1495 if (ListLocation
== KsListEntryTail
)
1497 /* move queue downwards */
1498 SrcEntry
= SrcEntry
->Flink
;
1502 /* move queue upwards */
1503 SrcEntry
= SrcEntry
->Blink
;
1506 if (SrcEntry
== SourceList
)
1508 /* eof list reached */
1512 /* get irp offset */
1513 Irp
= (PIRP
)CONTAINING_RECORD(SrcEntry
, IRP
, Tail
.Overlay
.ListEntry
);
1515 /* now check if irp can be moved */
1516 Status
= ListCallback(Irp
, Context
);
1518 /* check if irp can be moved */
1519 if (Status
== STATUS_SUCCESS
)
1521 /* remove irp from src list */
1522 RemoveEntryList(&Irp
->Tail
.Overlay
.ListEntry
);
1524 if (ListLocation
== KsListEntryTail
)
1526 /* insert irp end of list */
1527 InsertTailList(DestinationList
, &Irp
->Tail
.Overlay
.ListEntry
);
1531 /* insert irp head of list */
1532 InsertHeadList(DestinationList
, &Irp
->Tail
.Overlay
.ListEntry
);
1535 /* do we need to update the irp lock */
1536 if (DestinationLock
)
1538 /* update irp lock */
1539 KSQUEUE_SPINLOCK_IRP_STORAGE(Irp
) = DestinationLock
;
1544 if (Status
!= STATUS_NO_MATCH
)
1546 /* callback decided to stop enumeration */
1550 /* reset return value */
1551 Status
= STATUS_SUCCESS
;
1555 if (!DestinationLock
)
1557 /* release source lock */
1558 KeReleaseSpinLock(SourceLock
, OldLevel
);
1562 /* now release destination lock */
1563 KeReleaseSpinLockFromDpcLevel(DestinationLock
);
1565 /* now release source lock */
1566 KeReleaseSpinLockFromDpcLevel(SourceLock
);
1569 /* now release cancel spinlock */
1570 IoReleaseCancelSpinLock(OldLevel
);
1583 KsRemoveSpecificIrpFromCancelableQueue(
1586 PKSPIN_LOCK SpinLock
;
1589 DPRINT("KsRemoveSpecificIrpFromCancelableQueue %p\n", Irp
);
1591 /* get internal queue lock */
1592 SpinLock
= KSQUEUE_SPINLOCK_IRP_STORAGE(Irp
);
1594 /* acquire spinlock */
1595 KeAcquireSpinLock(SpinLock
, &OldLevel
);
1597 /* remove the irp from the list */
1598 RemoveEntryList(&Irp
->Tail
.Overlay
.ListEntry
);
1600 /* release spinlock */
1601 KeReleaseSpinLock(SpinLock
, OldLevel
);
1611 KsAddIrpToCancelableQueue(
1612 IN OUT PLIST_ENTRY QueueHead
,
1613 IN PKSPIN_LOCK SpinLock
,
1615 IN KSLIST_ENTRY_LOCATION ListLocation
,
1616 IN PDRIVER_CANCEL DriverCancel OPTIONAL
)
1618 PDRIVER_CANCEL OldDriverCancel
;
1619 PIO_STACK_LOCATION IoStack
;
1622 DPRINT("KsAddIrpToCancelableQueue QueueHead %p SpinLock %p Irp %p ListLocation %x DriverCancel %p\n", QueueHead
, SpinLock
, Irp
, ListLocation
, DriverCancel
);
1623 /* check for required parameters */
1624 if (!QueueHead
|| !SpinLock
|| !Irp
)
1629 /* default to KsCancelRoutine */
1630 DriverCancel
= KsCancelRoutine
;
1633 /* get current irp stack */
1634 IoStack
= IoGetCurrentIrpStackLocation(Irp
);
1636 /* acquire spinlock */
1637 KeAcquireSpinLock(SpinLock
, &OldLevel
);
1639 if (ListLocation
== KsListEntryTail
)
1641 /* insert irp to tail of list */
1642 InsertTailList(QueueHead
, &Irp
->Tail
.Overlay
.ListEntry
);
1646 /* insert irp to head of list */
1647 InsertHeadList(QueueHead
, &Irp
->Tail
.Overlay
.ListEntry
);
1650 /* store internal queue lock */
1651 KSQUEUE_SPINLOCK_IRP_STORAGE(Irp
) = SpinLock
;
1653 /* now set the cancel routine */
1654 OldDriverCancel
= IoSetCancelRoutine(Irp
, DriverCancel
);
1656 if (Irp
->Cancel
&& OldDriverCancel
== NULL
)
1658 /* the irp has already been canceled */
1659 KeReleaseSpinLock(SpinLock
, OldLevel
);
1661 /* cancel routine requires that cancel spinlock is held */
1662 IoAcquireCancelSpinLock(&Irp
->CancelIrql
);
1665 DriverCancel(IoStack
->DeviceObject
, Irp
);
1670 KeReleaseSpinLock(SpinLock
, OldLevel
);
1681 IN PDEVICE_OBJECT DeviceObject
,
1684 PKSPIN_LOCK SpinLock
;
1686 /* get internal queue lock */
1687 SpinLock
= KSQUEUE_SPINLOCK_IRP_STORAGE(Irp
);
1689 /* acquire spinlock */
1690 KeAcquireSpinLockAtDpcLevel(SpinLock
);
1693 ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL
);
1695 /* release cancel spinlock */
1696 IoReleaseCancelSpinLock(Irp
->CancelIrql
);
1698 /* remove the irp from the list */
1699 RemoveEntryList(&Irp
->Tail
.Overlay
.ListEntry
);
1701 /* release spinlock */
1702 KeReleaseSpinLock(SpinLock
, Irp
->CancelIrql
);
1704 /* has the irp already been canceled */
1705 if (Irp
->IoStatus
.Status
!= STATUS_CANCELLED
)
1707 /* let's complete it */
1708 Irp
->IoStatus
.Status
= STATUS_CANCELLED
;
1709 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
1714 FindMatchingCreateItem(
1715 PLIST_ENTRY ListHead
,
1718 OUT PCREATE_ITEM_ENTRY
*OutCreateItem
)
1721 PCREATE_ITEM_ENTRY CreateItemEntry
;
1722 UNICODE_STRING RefString
;
1726 /* remove '\' slash */
1728 BufferSize
-= sizeof(WCHAR
);
1731 if (!wcschr(Buffer
, L
'\\'))
1733 RtlInitUnicodeString(&RefString
, Buffer
);
1737 RefString
.Buffer
= Buffer
;
1738 RefString
.Length
= RefString
.MaximumLength
= ((ULONG_PTR
)wcschr(Buffer
, L
'\\') - (ULONG_PTR
)Buffer
);
1741 /* point to first entry */
1742 Entry
= ListHead
->Flink
;
1744 /* loop all device items */
1745 while(Entry
!= ListHead
)
1747 /* get create item entry */
1748 CreateItemEntry
= (PCREATE_ITEM_ENTRY
)CONTAINING_RECORD(Entry
, CREATE_ITEM_ENTRY
, Entry
);
1750 ASSERT(CreateItemEntry
->CreateItem
);
1752 if(CreateItemEntry
->CreateItem
->Flags
& KSCREATE_ITEM_WILDCARD
)
1754 /* create item is default */
1755 *OutCreateItem
= CreateItemEntry
;
1756 return STATUS_SUCCESS
;
1759 if (!CreateItemEntry
->CreateItem
->Create
)
1761 /* skip free create item */
1762 Entry
= Entry
->Flink
;
1766 ASSERT(CreateItemEntry
->CreateItem
->ObjectClass
.Buffer
);
1768 DPRINT("CreateItem %S Length %u Request %wZ %u\n", CreateItemEntry
->CreateItem
->ObjectClass
.Buffer
,
1769 CreateItemEntry
->CreateItem
->ObjectClass
.Length
,
1773 if (CreateItemEntry
->CreateItem
->ObjectClass
.Length
> BufferSize
)
1775 /* create item doesnt match in length */
1776 Entry
= Entry
->Flink
;
1780 /* now check if the object class is the same */
1781 if (!RtlCompareUnicodeString(&CreateItemEntry
->CreateItem
->ObjectClass
, &RefString
, TRUE
))
1783 /* found matching create item */
1784 *OutCreateItem
= CreateItemEntry
;
1785 return STATUS_SUCCESS
;
1787 /* iterate to next */
1788 Entry
= Entry
->Flink
;
1791 return STATUS_NOT_FOUND
;
1797 IN PDEVICE_OBJECT DeviceObject
,
1800 PCREATE_ITEM_ENTRY CreateItemEntry
;
1801 PIO_STACK_LOCATION IoStack
;
1802 PDEVICE_EXTENSION DeviceExtension
;
1803 PKSIDEVICE_HEADER DeviceHeader
;
1804 PKSIOBJECT_HEADER ObjectHeader
;
1807 DPRINT("KS / CREATE\n");
1809 /* get current stack location */
1810 IoStack
= IoGetCurrentIrpStackLocation(Irp
);
1811 /* get device extension */
1812 DeviceExtension
= (PDEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
1813 /* get device header */
1814 DeviceHeader
= DeviceExtension
->DeviceHeader
;
1817 if (IoStack
->FileObject
->FileName
.Buffer
== NULL
)
1819 /* FIXME Pnp-Issue */
1820 DPRINT("Using reference string hack\n");
1821 Irp
->IoStatus
.Information
= 0;
1822 /* set return status */
1823 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
1824 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
1825 return STATUS_SUCCESS
;
1828 if (IoStack
->FileObject
->RelatedFileObject
!= NULL
)
1830 /* request is to instantiate a pin / node / clock / allocator */
1831 ObjectHeader
= (PKSIOBJECT_HEADER
)IoStack
->FileObject
->RelatedFileObject
->FsContext2
;
1834 ASSERT(ObjectHeader
);
1836 /* find a matching a create item */
1837 Status
= FindMatchingCreateItem(&ObjectHeader
->ItemList
, IoStack
->FileObject
->FileName
.Length
, IoStack
->FileObject
->FileName
.Buffer
, &CreateItemEntry
);
1841 /* request to create a filter */
1842 Status
= FindMatchingCreateItem(&DeviceHeader
->ItemList
, IoStack
->FileObject
->FileName
.Length
, IoStack
->FileObject
->FileName
.Buffer
, &CreateItemEntry
);
1845 if (NT_SUCCESS(Status
))
1847 /* set object create item */
1848 KSCREATE_ITEM_IRP_STORAGE(Irp
) = CreateItemEntry
->CreateItem
;
1850 /* call create function */
1851 Status
= CreateItemEntry
->CreateItem
->Create(DeviceObject
, Irp
);
1853 if (NT_SUCCESS(Status
))
1855 /* increment create item reference count */
1856 InterlockedIncrement(&CreateItemEntry
->ReferenceCount
);
1861 Irp
->IoStatus
.Information
= 0;
1862 /* set return status */
1863 Irp
->IoStatus
.Status
= STATUS_UNSUCCESSFUL
;
1864 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
1865 return STATUS_UNSUCCESSFUL
;
1871 IN PDEVICE_OBJECT DeviceObject
,
1874 PIO_STACK_LOCATION IoStack
;
1875 PDEVICE_EXTENSION DeviceExtension
;
1876 PKSIOBJECT_HEADER ObjectHeader
;
1877 PKSIDEVICE_HEADER DeviceHeader
;
1878 PDRIVER_DISPATCH Dispatch
;
1881 /* get current stack location */
1882 IoStack
= IoGetCurrentIrpStackLocation(Irp
);
1884 /* get device extension */
1885 DeviceExtension
= (PDEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
1886 /* get device header */
1887 DeviceHeader
= DeviceExtension
->DeviceHeader
;
1889 ASSERT(IoStack
->FileObject
);
1891 /* get object header */
1892 ObjectHeader
= (PKSIOBJECT_HEADER
) IoStack
->FileObject
->FsContext2
;
1896 /* FIXME Pnp-Issue*/
1897 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
1898 Irp
->IoStatus
.Information
= 0;
1899 /* complete and forget */
1900 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
1901 return STATUS_SUCCESS
;
1905 ASSERT(ObjectHeader
);
1906 /* store create item */
1907 //KSCREATE_ITEM_IRP_STORAGE(Irp) = (PKSOBJECT_CREATE_ITEM)0x12345678; //ObjectHeader->CreateItem;
1909 /* retrieve matching dispatch function */
1910 switch(IoStack
->MajorFunction
)
1913 Dispatch
= ObjectHeader
->DispatchTable
.Close
;
1915 case IRP_MJ_DEVICE_CONTROL
:
1916 Dispatch
= ObjectHeader
->DispatchTable
.DeviceIoControl
;
1919 Dispatch
= ObjectHeader
->DispatchTable
.Read
;
1922 Dispatch
= ObjectHeader
->DispatchTable
.Write
;
1924 case IRP_MJ_FLUSH_BUFFERS
:
1925 Dispatch
= ObjectHeader
->DispatchTable
.Flush
;
1927 case IRP_MJ_QUERY_SECURITY
:
1928 Dispatch
= ObjectHeader
->DispatchTable
.QuerySecurity
;
1930 case IRP_MJ_SET_SECURITY
:
1931 Dispatch
= ObjectHeader
->DispatchTable
.SetSecurity
;
1934 Dispatch
= KsDefaultDispatchPnp
;
1939 /* is the request supported */
1942 /* now call the dispatch function */
1943 Status
= Dispatch(DeviceObject
, Irp
);
1947 /* not supported request */
1948 Status
= KsDispatchInvalidDeviceRequest(DeviceObject
, Irp
);
1961 KsSetMajorFunctionHandler(
1962 IN PDRIVER_OBJECT DriverObject
,
1963 IN ULONG MajorFunction
)
1965 DPRINT("KsSetMajorFunctionHandler Function %x\n", MajorFunction
);
1970 DriverObject
->MajorFunction
[IRP_MJ_CREATE
] = KspCreate
;
1973 switch ( MajorFunction
)
1976 DriverObject
->MajorFunction
[MajorFunction
] = KspCreate
;
1978 case IRP_MJ_DEVICE_CONTROL
:
1982 case IRP_MJ_FLUSH_BUFFERS
:
1983 case IRP_MJ_QUERY_SECURITY
:
1984 case IRP_MJ_SET_SECURITY
:
1985 DriverObject
->MajorFunction
[MajorFunction
] = KspDispatchIrp
;
1988 DPRINT1("NotSupported %x\n", MajorFunction
);
1989 return STATUS_INVALID_PARAMETER
;
1992 return STATUS_SUCCESS
;
2002 IN PDEVICE_OBJECT DeviceObject
,
2005 PIO_STACK_LOCATION IoStack
;
2006 PKSIDEVICE_HEADER DeviceHeader
;
2007 PDEVICE_EXTENSION DeviceExtension
;
2009 //DPRINT("KsDispatchIrp DeviceObject %p Irp %p\n", DeviceObject, Irp);
2011 /* get device extension */
2012 DeviceExtension
= (PDEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
2013 /* get device header */
2014 DeviceHeader
= DeviceExtension
->DeviceHeader
;
2017 /* get current irp stack */
2018 IoStack
= IoGetCurrentIrpStackLocation(Irp
);
2020 if (IoStack
->MajorFunction
<= IRP_MJ_DEVICE_CONTROL
)
2022 if (IoStack
->MajorFunction
== IRP_MJ_CREATE
)
2024 /* check internal type */
2025 if (DeviceHeader
->BasicHeader
.OuterUnknown
) /* FIXME improve check */
2027 /* AVStream client */
2028 return IKsDevice_Create(DeviceObject
, Irp
);
2032 /* external client (portcls) */
2033 return KspCreate(DeviceObject
, Irp
);
2037 switch (IoStack
->MajorFunction
)
2042 case IRP_MJ_FLUSH_BUFFERS
:
2043 case IRP_MJ_QUERY_SECURITY
:
2044 case IRP_MJ_SET_SECURITY
:
2046 case IRP_MJ_DEVICE_CONTROL
:
2047 return KspDispatchIrp(DeviceObject
, Irp
);
2049 return KsDispatchInvalidDeviceRequest(DeviceObject
, Irp
);
2053 /* dispatch power */
2054 if (IoStack
->MajorFunction
== IRP_MJ_POWER
)
2056 /* check internal type */
2057 if (DeviceHeader
->BasicHeader
.OuterUnknown
) /* FIXME improve check */
2059 /* AVStream client */
2060 return IKsDevice_Power(DeviceObject
, Irp
);
2064 /* external client (portcls) */
2065 return KsDefaultDispatchPower(DeviceObject
, Irp
);
2068 else if (IoStack
->MajorFunction
== IRP_MJ_PNP
) /* dispatch pnp */
2070 /* check internal type */
2071 if (DeviceHeader
->BasicHeader
.OuterUnknown
) /* FIXME improve check */
2073 /* AVStream client */
2074 return IKsDevice_Pnp(DeviceObject
, Irp
);
2078 /* external client (portcls) */
2079 return KsDefaultDispatchPnp(DeviceObject
, Irp
);
2082 else if (IoStack
->MajorFunction
== IRP_MJ_SYSTEM_CONTROL
)
2085 return KsDefaultForwardIrp(DeviceObject
, Irp
);
2090 return KsDispatchInvalidDeviceRequest(DeviceObject
, Irp
);
2104 return KSFILTER_NODE
;