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
);
891 /* probe user mode buffers */
892 if (Length
&& ( (!HeaderSize
) || (Length
% HeaderSize
== 0) || ((ProbeFlags
& KSPROBE_ALLOWFORMATCHANGE
) && (Length
== sizeof(KSSTREAM_HEADER
))) ) )
894 /* allocate stream header buffer */
895 Irp
->AssociatedIrp
.SystemBuffer
= ExAllocatePool(NonPagedPool
, Length
);
897 if (!Irp
->AssociatedIrp
.SystemBuffer
)
900 return STATUS_INSUFFICIENT_RESOURCES
;
905 if (ProbeFlags
& KSPROBE_STREAMWRITE
)
907 if (ProbeFlags
& KSPROBE_MODIFY
)
908 ProbeForWrite(Irp
->UserBuffer
, Length
, sizeof(UCHAR
));
910 ProbeForRead(Irp
->UserBuffer
, Length
, sizeof(UCHAR
));
914 /* stream reads means writing */
915 ProbeForWrite(Irp
->UserBuffer
, Length
, sizeof(UCHAR
));
918 /* copy stream buffer */
919 RtlMoveMemory(Irp
->AssociatedIrp
.SystemBuffer
, Irp
->UserBuffer
, Length
);
921 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
923 /* Exception, get the error code */
924 Status
= _SEH2_GetExceptionCode();
927 if (!NT_SUCCESS(Status
))
933 if (ProbeFlags
& KSPROBE_ALLOCATEMDL
)
939 /* check all stream headers */
940 StreamHeader
= (PKSSTREAM_HEADER
)Irp
->AssociatedIrp
.SystemBuffer
;
948 /* does the supplied header size match stream header size and no type changed */
949 if (StreamHeader
->Size
!= HeaderSize
&& !(StreamHeader
->OptionsFlags
& KSSTREAM_HEADER_OPTIONSF_TYPECHANGED
))
951 /* invalid stream header */
952 ExRaiseStatus(STATUS_INVALID_BUFFER_SIZE
);
957 /* stream must be at least of size KSSTREAM_HEADER and size must be 8-byte block aligned */
958 if (StreamHeader
->Size
< sizeof(KSSTREAM_HEADER
) || (StreamHeader
->Size
& 7))
960 /* invalid stream header */
961 ExRaiseStatus(STATUS_INVALID_BUFFER_SIZE
);
965 if (Length
< StreamHeader
->Size
)
967 /* length is too short */
968 ExRaiseStatus(STATUS_INVALID_BUFFER_SIZE
);
971 if (ProbeFlags
& KSPROBE_STREAMWRITE
)
973 if (StreamHeader
->DataUsed
> StreamHeader
->FrameExtent
)
975 /* frame extend can never be smaller */
976 ExRaiseStatus(STATUS_INVALID_BUFFER_SIZE
);
979 /* is this stream change packet */
980 if (StreamHeader
->OptionsFlags
& KSSTREAM_HEADER_OPTIONSF_TYPECHANGED
)
982 if (Length
!= sizeof(KSSTREAM_HEADER
) || (PVOID
)StreamHeader
!= Irp
->AssociatedIrp
.SystemBuffer
)
984 /* stream changed - must be send in a single packet */
985 ExRaiseStatus(STATUS_INVALID_BUFFER_SIZE
);
988 if (!(ProbeFlags
& KSPROBE_ALLOWFORMATCHANGE
))
990 /* caller does not permit format changes */
991 ExRaiseStatus(STATUS_INVALID_PARAMETER
);
994 if (StreamHeader
->FrameExtent
)
996 /* allocate an mdl */
997 Mdl
= IoAllocateMdl(StreamHeader
->Data
, StreamHeader
->FrameExtent
, FALSE
, TRUE
, Irp
);
1001 /* not enough memory */
1002 ExRaiseStatus(STATUS_INSUFFICIENT_RESOURCES
);
1005 /* break out to probe for the irp */
1013 if (StreamHeader
->DataUsed
)
1015 /* DataUsed must be zero for stream read operation */
1016 ExRaiseStatus(STATUS_INVALID_BUFFER_SIZE
);
1019 if (StreamHeader
->OptionsFlags
)
1021 /* no flags supported for reading */
1022 ExRaiseStatus(STATUS_INVALID_PARAMETER
);
1026 /* move to next stream header */
1027 Length
-= StreamHeader
->Size
;
1028 StreamHeader
= (PKSSTREAM_HEADER
)((ULONG_PTR
)StreamHeader
+ StreamHeader
->Size
);
1031 }_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1033 /* Exception, get the error code */
1034 Status
= _SEH2_GetExceptionCode();
1037 /* now probe the allocated mdl's */
1038 if (NT_SUCCESS(Status
))
1044 return STATUS_INVALID_BUFFER_SIZE
;
1053 KsAllocateExtraData(
1056 OUT PVOID
* ExtraBuffer
)
1058 PIO_STACK_LOCATION IoStack
;
1060 PUCHAR Buffer
, BufferOrg
;
1061 PKSSTREAM_HEADER Header
;
1062 NTSTATUS Status
= STATUS_SUCCESS
;
1064 /* get current irp stack */
1065 IoStack
= IoGetCurrentIrpStackLocation(Irp
);
1068 ASSERT(IoStack
->Parameters
.DeviceIoControl
.InputBufferLength
>= sizeof(KSSTREAM_HEADER
));
1070 /* get total length */
1071 Count
= IoStack
->Parameters
.DeviceIoControl
.InputBufferLength
/ sizeof(KSSTREAM_HEADER
);
1073 /* allocate buffer */
1074 Buffer
= BufferOrg
= AllocateItem(NonPagedPool
, Count
* (sizeof(KSSTREAM_HEADER
) + ExtraSize
));
1076 return STATUS_INSUFFICIENT_RESOURCES
;
1080 /* get input buffer */
1081 Header
= (PKSSTREAM_HEADER
)IoStack
->Parameters
.DeviceIoControl
.Type3InputBuffer
;
1082 for(Index
= 0; Index
< Count
; Index
++)
1084 /* copy stream header */
1085 RtlMoveMemory(Buffer
, Header
, sizeof(KSSTREAM_HEADER
));
1087 /* move to next header */
1089 /* increment output buffer offset */
1090 Buffer
+= sizeof(KSSTREAM_HEADER
) + ExtraSize
;
1093 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1095 /* Exception, get the error code */
1096 Status
= _SEH2_GetExceptionCode();
1100 if (!NT_SUCCESS(Status
))
1102 /* free buffer on exception */
1108 *ExtraBuffer
= BufferOrg
;
1111 return STATUS_SUCCESS
;
1121 IN PDRIVER_OBJECT DriverObject
)
1131 KsDispatchInvalidDeviceRequest(
1132 IN PDEVICE_OBJECT DeviceObject
,
1135 Irp
->IoStatus
.Status
= STATUS_INVALID_DEVICE_REQUEST
;
1136 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
1138 return STATUS_INVALID_DEVICE_REQUEST
;
1147 KsDefaultDeviceIoCompletion(
1148 IN PDEVICE_OBJECT DeviceObject
,
1151 PIO_STACK_LOCATION IoStack
;
1154 /* get current irp stack */
1155 IoStack
= IoGetCurrentIrpStackLocation(Irp
);
1157 if (IoStack
->Parameters
.DeviceIoControl
.IoControlCode
!= IOCTL_KS_PROPERTY
&&
1158 IoStack
->Parameters
.DeviceIoControl
.IoControlCode
!= IOCTL_KS_METHOD
&&
1159 IoStack
->Parameters
.DeviceIoControl
.IoControlCode
!= IOCTL_KS_PROPERTY
)
1161 if (IoStack
->Parameters
.DeviceIoControl
.IoControlCode
== IOCTL_KS_RESET_STATE
)
1164 Status
= STATUS_SUCCESS
;
1168 /* request unsupported */
1169 Status
= STATUS_INVALID_DEVICE_REQUEST
;
1174 /* property / method / event not found */
1175 Status
= STATUS_PROPSET_NOT_FOUND
;
1178 /* complete request */
1179 Irp
->IoStatus
.Status
= Status
;
1180 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
1192 KsDispatchFastIoDeviceControlFailure(
1193 IN PFILE_OBJECT FileObject
,
1195 IN PVOID InputBuffer OPTIONAL
,
1196 IN ULONG InputBufferLength
,
1197 OUT PVOID OutputBuffer OPTIONAL
,
1198 IN ULONG OutputBufferLength
,
1199 IN ULONG IoControlCode
,
1200 OUT PIO_STATUS_BLOCK IoStatus
,
1201 IN PDEVICE_OBJECT DeviceObject
)
1212 KsDispatchFastReadFailure(
1213 IN PFILE_OBJECT FileObject
,
1214 IN PLARGE_INTEGER FileOffset
,
1219 OUT PIO_STATUS_BLOCK IoStatus
,
1220 IN PDEVICE_OBJECT DeviceObject
)
1233 IN OUT PLIST_ENTRY QueueHead
,
1234 IN PKSPIN_LOCK SpinLock
)
1236 PDRIVER_CANCEL OldDriverCancel
;
1237 PIO_STACK_LOCATION IoStack
;
1242 /* acquire spinlock */
1243 KeAcquireSpinLock(SpinLock
, &OldLevel
);
1244 /* point to first entry */
1245 Entry
= QueueHead
->Flink
;
1246 /* loop all items */
1247 while(Entry
!= QueueHead
)
1249 /* get irp offset */
1250 Irp
= (PIRP
)CONTAINING_RECORD(Entry
, IRP
, Tail
.Overlay
.ListEntry
);
1252 /* set cancelled bit */
1255 /* now set the cancel routine */
1256 OldDriverCancel
= IoSetCancelRoutine(Irp
, NULL
);
1257 if (OldDriverCancel
)
1259 /* this irp hasnt been yet used, so free to cancel */
1260 KeReleaseSpinLock(SpinLock
, OldLevel
);
1262 /* get current irp stack */
1263 IoStack
= IoGetCurrentIrpStackLocation(Irp
);
1265 /* acquire cancel spinlock */
1266 IoAcquireCancelSpinLock(&Irp
->CancelIrql
);
1268 /* call provided cancel routine */
1269 OldDriverCancel(IoStack
->DeviceObject
, Irp
);
1271 /* re-acquire spinlock */
1272 KeAcquireSpinLock(SpinLock
, &OldLevel
);
1274 /* move on to next entry */
1275 Entry
= Entry
->Flink
;
1278 /* the irp has already been canceled */
1279 KeReleaseSpinLock(SpinLock
, OldLevel
);
1289 KsReleaseIrpOnCancelableQueue(
1291 IN PDRIVER_CANCEL DriverCancel OPTIONAL
)
1293 PKSPIN_LOCK SpinLock
;
1294 PDRIVER_CANCEL OldDriverCancel
;
1295 PIO_STACK_LOCATION IoStack
;
1298 /* check for required parameters */
1304 /* default to KsCancelRoutine */
1305 DriverCancel
= KsCancelRoutine
;
1308 /* get current irp stack */
1309 IoStack
= IoGetCurrentIrpStackLocation(Irp
);
1311 /* get internal queue lock */
1312 SpinLock
= KSQUEUE_SPINLOCK_IRP_STORAGE(Irp
);
1314 /* acquire spinlock */
1315 KeAcquireSpinLock(SpinLock
, &OldLevel
);
1317 /* now set the cancel routine */
1318 OldDriverCancel
= IoSetCancelRoutine(Irp
, DriverCancel
);
1320 if (Irp
->Cancel
&& OldDriverCancel
== NULL
)
1322 /* the irp has already been canceled */
1323 KeReleaseSpinLock(SpinLock
, OldLevel
);
1325 /* cancel routine requires that cancel spinlock is held */
1326 IoAcquireCancelSpinLock(&Irp
->CancelIrql
);
1329 DriverCancel(IoStack
->DeviceObject
, Irp
);
1334 KeReleaseSpinLock(SpinLock
, OldLevel
);
1344 KsRemoveIrpFromCancelableQueue(
1345 IN OUT PLIST_ENTRY QueueHead
,
1346 IN PKSPIN_LOCK SpinLock
,
1347 IN KSLIST_ENTRY_LOCATION ListLocation
,
1348 IN KSIRP_REMOVAL_OPERATION RemovalOperation
)
1351 PLIST_ENTRY CurEntry
;
1354 DPRINT("KsRemoveIrpFromCancelableQueue ListHead %p SpinLock %p ListLocation %x RemovalOperation %x\n", QueueHead
, SpinLock
, ListLocation
, RemovalOperation
);
1356 /* check parameters */
1357 if (!QueueHead
|| !SpinLock
)
1360 /* check if parameter ListLocation is valid */
1361 if (ListLocation
!= KsListEntryTail
&& ListLocation
!= KsListEntryHead
)
1364 /* acquire list lock */
1365 KeAcquireSpinLock(SpinLock
, &OldIrql
);
1367 /* point to queue head */
1368 CurEntry
= QueueHead
;
1372 /* reset irp to null */
1375 /* iterate to next entry */
1376 if (ListLocation
== KsListEntryHead
)
1377 CurEntry
= CurEntry
->Flink
;
1379 CurEntry
= CurEntry
->Blink
;
1381 /* is the end of list reached */
1382 if (CurEntry
== QueueHead
)
1384 /* reached end of list */
1388 /* get irp offset */
1389 Irp
= (PIRP
)CONTAINING_RECORD(CurEntry
, IRP
, Tail
.Overlay
.ListEntry
);
1393 /* irp has been canceled */
1397 if (Irp
->CancelRoutine
)
1399 /* remove cancel routine */
1400 Irp
->CancelRoutine
= NULL
;
1402 if (RemovalOperation
== KsAcquireAndRemove
|| RemovalOperation
== KsAcquireAndRemoveOnlySingleItem
)
1404 /* remove irp from list */
1405 RemoveEntryList(&Irp
->Tail
.Overlay
.ListEntry
);
1408 if (RemovalOperation
== KsAcquireAndRemoveOnlySingleItem
|| RemovalOperation
== KsAcquireOnlySingleItem
)
1415 KeReleaseSpinLock(SpinLock
, OldIrql
);
1417 if (!Irp
|| Irp
->CancelRoutine
== NULL
)
1419 /* either an irp has been acquired or nothing found */
1423 /* time to remove the canceled irp */
1424 IoAcquireCancelSpinLock(&OldIrql
);
1425 /* acquire list lock */
1426 KeAcquireSpinLockAtDpcLevel(SpinLock
);
1428 if (RemovalOperation
== KsAcquireAndRemove
|| RemovalOperation
== KsAcquireAndRemoveOnlySingleItem
)
1431 RemoveEntryList(&Irp
->Tail
.Overlay
.ListEntry
);
1434 /* release list lock */
1435 KeReleaseSpinLockFromDpcLevel(SpinLock
);
1437 /* release cancel spinlock */
1438 IoReleaseCancelSpinLock(OldIrql
);
1439 /* no non canceled irp has been found */
1449 KsMoveIrpsOnCancelableQueue(
1450 IN OUT PLIST_ENTRY SourceList
,
1451 IN PKSPIN_LOCK SourceLock
,
1452 IN OUT PLIST_ENTRY DestinationList
,
1453 IN PKSPIN_LOCK DestinationLock OPTIONAL
,
1454 IN KSLIST_ENTRY_LOCATION ListLocation
,
1455 IN PFNKSIRPLISTCALLBACK ListCallback
,
1459 PLIST_ENTRY SrcEntry
;
1461 NTSTATUS Status
= STATUS_SUCCESS
;
1463 if (!DestinationLock
)
1465 /* no destination lock just acquire the source lock */
1466 KeAcquireSpinLock(SourceLock
, &OldLevel
);
1470 /* acquire cancel spinlock */
1471 IoAcquireCancelSpinLock(&OldLevel
);
1473 /* now acquire source lock */
1474 KeAcquireSpinLockAtDpcLevel(SourceLock
);
1476 /* now acquire destination lock */
1477 KeAcquireSpinLockAtDpcLevel(DestinationLock
);
1480 /* point to list head */
1481 SrcEntry
= SourceList
;
1483 /* now move all irps */
1486 if (ListLocation
== KsListEntryTail
)
1488 /* move queue downwards */
1489 SrcEntry
= SrcEntry
->Flink
;
1493 /* move queue upwards */
1494 SrcEntry
= SrcEntry
->Blink
;
1497 if (SrcEntry
== SourceList
)
1499 /* eof list reached */
1503 /* get irp offset */
1504 Irp
= (PIRP
)CONTAINING_RECORD(SrcEntry
, IRP
, Tail
.Overlay
.ListEntry
);
1506 /* now check if irp can be moved */
1507 Status
= ListCallback(Irp
, Context
);
1509 /* check if irp can be moved */
1510 if (Status
== STATUS_SUCCESS
)
1512 /* remove irp from src list */
1513 RemoveEntryList(&Irp
->Tail
.Overlay
.ListEntry
);
1515 if (ListLocation
== KsListEntryTail
)
1517 /* insert irp end of list */
1518 InsertTailList(DestinationList
, &Irp
->Tail
.Overlay
.ListEntry
);
1522 /* insert irp head of list */
1523 InsertHeadList(DestinationList
, &Irp
->Tail
.Overlay
.ListEntry
);
1526 /* do we need to update the irp lock */
1527 if (DestinationLock
)
1529 /* update irp lock */
1530 KSQUEUE_SPINLOCK_IRP_STORAGE(Irp
) = DestinationLock
;
1535 if (Status
!= STATUS_NO_MATCH
)
1537 /* callback decided to stop enumeration */
1541 /* reset return value */
1542 Status
= STATUS_SUCCESS
;
1546 if (!DestinationLock
)
1548 /* release source lock */
1549 KeReleaseSpinLock(SourceLock
, OldLevel
);
1553 /* now release destination lock */
1554 KeReleaseSpinLockFromDpcLevel(DestinationLock
);
1556 /* now release source lock */
1557 KeReleaseSpinLockFromDpcLevel(SourceLock
);
1560 /* now release cancel spinlock */
1561 IoReleaseCancelSpinLock(OldLevel
);
1574 KsRemoveSpecificIrpFromCancelableQueue(
1577 PKSPIN_LOCK SpinLock
;
1580 DPRINT("KsRemoveSpecificIrpFromCancelableQueue %p\n", Irp
);
1582 /* get internal queue lock */
1583 SpinLock
= KSQUEUE_SPINLOCK_IRP_STORAGE(Irp
);
1585 /* acquire spinlock */
1586 KeAcquireSpinLock(SpinLock
, &OldLevel
);
1588 /* remove the irp from the list */
1589 RemoveEntryList(&Irp
->Tail
.Overlay
.ListEntry
);
1591 /* release spinlock */
1592 KeReleaseSpinLock(SpinLock
, OldLevel
);
1602 KsAddIrpToCancelableQueue(
1603 IN OUT PLIST_ENTRY QueueHead
,
1604 IN PKSPIN_LOCK SpinLock
,
1606 IN KSLIST_ENTRY_LOCATION ListLocation
,
1607 IN PDRIVER_CANCEL DriverCancel OPTIONAL
)
1609 PDRIVER_CANCEL OldDriverCancel
;
1610 PIO_STACK_LOCATION IoStack
;
1613 DPRINT("KsAddIrpToCancelableQueue QueueHead %p SpinLock %p Irp %p ListLocation %x DriverCancel %p\n", QueueHead
, SpinLock
, Irp
, ListLocation
, DriverCancel
);
1614 /* check for required parameters */
1615 if (!QueueHead
|| !SpinLock
|| !Irp
)
1620 /* default to KsCancelRoutine */
1621 DriverCancel
= KsCancelRoutine
;
1624 /* get current irp stack */
1625 IoStack
= IoGetCurrentIrpStackLocation(Irp
);
1627 /* acquire spinlock */
1628 KeAcquireSpinLock(SpinLock
, &OldLevel
);
1630 if (ListLocation
== KsListEntryTail
)
1632 /* insert irp to tail of list */
1633 InsertTailList(QueueHead
, &Irp
->Tail
.Overlay
.ListEntry
);
1637 /* insert irp to head of list */
1638 InsertHeadList(QueueHead
, &Irp
->Tail
.Overlay
.ListEntry
);
1641 /* store internal queue lock */
1642 KSQUEUE_SPINLOCK_IRP_STORAGE(Irp
) = SpinLock
;
1644 /* now set the cancel routine */
1645 OldDriverCancel
= IoSetCancelRoutine(Irp
, DriverCancel
);
1647 if (Irp
->Cancel
&& OldDriverCancel
== NULL
)
1649 /* the irp has already been canceled */
1650 KeReleaseSpinLock(SpinLock
, OldLevel
);
1652 /* cancel routine requires that cancel spinlock is held */
1653 IoAcquireCancelSpinLock(&Irp
->CancelIrql
);
1656 DriverCancel(IoStack
->DeviceObject
, Irp
);
1661 KeReleaseSpinLock(SpinLock
, OldLevel
);
1672 IN PDEVICE_OBJECT DeviceObject
,
1675 PKSPIN_LOCK SpinLock
;
1677 /* get internal queue lock */
1678 SpinLock
= KSQUEUE_SPINLOCK_IRP_STORAGE(Irp
);
1680 /* acquire spinlock */
1681 KeAcquireSpinLockAtDpcLevel(SpinLock
);
1684 ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL
);
1686 /* release cancel spinlock */
1687 IoReleaseCancelSpinLock(Irp
->CancelIrql
);
1689 /* remove the irp from the list */
1690 RemoveEntryList(&Irp
->Tail
.Overlay
.ListEntry
);
1692 /* release spinlock */
1693 KeReleaseSpinLockFromDpcLevel(SpinLock
);
1695 /* has the irp already been canceled */
1696 if (Irp
->IoStatus
.Status
!= STATUS_CANCELLED
)
1698 /* let's complete it */
1699 Irp
->IoStatus
.Status
= STATUS_CANCELLED
;
1700 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
1705 FindMatchingCreateItem(
1706 PLIST_ENTRY ListHead
,
1709 OUT PCREATE_ITEM_ENTRY
*OutCreateItem
)
1712 PCREATE_ITEM_ENTRY CreateItemEntry
;
1714 /* remove '\' slash */
1716 BufferSize
-= sizeof(WCHAR
);
1718 /* point to first entry */
1719 Entry
= ListHead
->Flink
;
1721 /* loop all device items */
1722 while(Entry
!= ListHead
)
1724 /* get create item entry */
1725 CreateItemEntry
= (PCREATE_ITEM_ENTRY
)CONTAINING_RECORD(Entry
, CREATE_ITEM_ENTRY
, Entry
);
1727 ASSERT(CreateItemEntry
->CreateItem
);
1729 if(CreateItemEntry
->CreateItem
->Flags
& KSCREATE_ITEM_WILDCARD
)
1731 /* create item is default */
1732 *OutCreateItem
= CreateItemEntry
;
1733 return STATUS_SUCCESS
;
1736 if (!CreateItemEntry
->CreateItem
->Create
)
1738 /* skip free create item */
1739 Entry
= Entry
->Flink
;
1743 ASSERT(CreateItemEntry
->CreateItem
->ObjectClass
.Buffer
);
1745 DPRINT1("CreateItem %S Length %u Request %S %u\n", CreateItemEntry
->CreateItem
->ObjectClass
.Buffer
,
1746 CreateItemEntry
->CreateItem
->ObjectClass
.Length
,
1750 if (CreateItemEntry
->CreateItem
->ObjectClass
.Length
> BufferSize
)
1752 /* create item doesnt match in length */
1753 Entry
= Entry
->Flink
;
1757 /* now check if the object class is the same */
1758 if (RtlCompareMemory(CreateItemEntry
->CreateItem
->ObjectClass
.Buffer
, Buffer
, CreateItemEntry
->CreateItem
->ObjectClass
.Length
) == CreateItemEntry
->CreateItem
->ObjectClass
.Length
)
1760 /* found matching create item */
1761 *OutCreateItem
= CreateItemEntry
;
1762 return STATUS_SUCCESS
;
1764 /* iterate to next */
1765 Entry
= Entry
->Flink
;
1768 return STATUS_NOT_FOUND
;
1774 IN PDEVICE_OBJECT DeviceObject
,
1777 PCREATE_ITEM_ENTRY CreateItemEntry
;
1778 PIO_STACK_LOCATION IoStack
;
1779 PDEVICE_EXTENSION DeviceExtension
;
1780 PKSIDEVICE_HEADER DeviceHeader
;
1781 PKSIOBJECT_HEADER ObjectHeader
;
1784 DPRINT("KS / CREATE\n");
1785 /* get current stack location */
1786 IoStack
= IoGetCurrentIrpStackLocation(Irp
);
1787 /* get device extension */
1788 DeviceExtension
= (PDEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
1789 /* get device header */
1790 DeviceHeader
= DeviceExtension
->DeviceHeader
;
1793 if (IoStack
->FileObject
->FileName
.Buffer
== NULL
)
1795 /* FIXME Pnp-Issue */
1796 DPRINT("Using reference string hack\n");
1797 Irp
->IoStatus
.Information
= 0;
1798 /* set return status */
1799 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
1800 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
1801 return STATUS_SUCCESS
;
1804 if (IoStack
->FileObject
->RelatedFileObject
!= NULL
)
1806 /* request is to instantiate a pin / node / clock / allocator */
1807 ObjectHeader
= (PKSIOBJECT_HEADER
)IoStack
->FileObject
->RelatedFileObject
->FsContext2
;
1810 ASSERT(ObjectHeader
);
1812 /* find a matching a create item */
1813 Status
= FindMatchingCreateItem(&ObjectHeader
->ItemList
, IoStack
->FileObject
->FileName
.Length
, IoStack
->FileObject
->FileName
.Buffer
, &CreateItemEntry
);
1817 /* request to create a filter */
1818 Status
= FindMatchingCreateItem(&DeviceHeader
->ItemList
, IoStack
->FileObject
->FileName
.Length
, IoStack
->FileObject
->FileName
.Buffer
, &CreateItemEntry
);
1821 if (NT_SUCCESS(Status
))
1823 /* set object create item */
1824 KSCREATE_ITEM_IRP_STORAGE(Irp
) = CreateItemEntry
->CreateItem
;
1826 /* call create function */
1827 Status
= CreateItemEntry
->CreateItem
->Create(DeviceObject
, Irp
);
1829 if (NT_SUCCESS(Status
))
1831 /* increment create item reference count */
1832 InterlockedIncrement(&CreateItemEntry
->ReferenceCount
);
1838 Irp
->IoStatus
.Information
= 0;
1839 /* set return status */
1840 Irp
->IoStatus
.Status
= STATUS_UNSUCCESSFUL
;
1841 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
1842 return STATUS_UNSUCCESSFUL
;
1848 IN PDEVICE_OBJECT DeviceObject
,
1851 PIO_STACK_LOCATION IoStack
;
1852 PDEVICE_EXTENSION DeviceExtension
;
1853 PKSIOBJECT_HEADER ObjectHeader
;
1854 PKSIDEVICE_HEADER DeviceHeader
;
1855 PDRIVER_DISPATCH Dispatch
;
1858 /* get current stack location */
1859 IoStack
= IoGetCurrentIrpStackLocation(Irp
);
1861 /* get device extension */
1862 DeviceExtension
= (PDEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
1863 /* get device header */
1864 DeviceHeader
= DeviceExtension
->DeviceHeader
;
1866 ASSERT(IoStack
->FileObject
);
1868 /* get object header */
1869 ObjectHeader
= (PKSIOBJECT_HEADER
) IoStack
->FileObject
->FsContext2
;
1873 /* FIXME Pnp-Issue*/
1874 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
1875 Irp
->IoStatus
.Information
= 0;
1876 /* complete and forget */
1877 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
1878 return STATUS_SUCCESS
;
1882 ASSERT(ObjectHeader
);
1883 /* store create item */
1884 //KSCREATE_ITEM_IRP_STORAGE(Irp) = (PKSOBJECT_CREATE_ITEM)0x12345678; //ObjectHeader->CreateItem;
1886 /* retrieve matching dispatch function */
1887 switch(IoStack
->MajorFunction
)
1890 Dispatch
= ObjectHeader
->DispatchTable
.Close
;
1892 case IRP_MJ_DEVICE_CONTROL
:
1893 Dispatch
= ObjectHeader
->DispatchTable
.DeviceIoControl
;
1896 Dispatch
= ObjectHeader
->DispatchTable
.Read
;
1899 Dispatch
= ObjectHeader
->DispatchTable
.Write
;
1901 case IRP_MJ_FLUSH_BUFFERS
:
1902 Dispatch
= ObjectHeader
->DispatchTable
.Flush
;
1904 case IRP_MJ_QUERY_SECURITY
:
1905 Dispatch
= ObjectHeader
->DispatchTable
.QuerySecurity
;
1907 case IRP_MJ_SET_SECURITY
:
1908 Dispatch
= ObjectHeader
->DispatchTable
.SetSecurity
;
1911 Dispatch
= KsDefaultDispatchPnp
;
1916 /* is the request supported */
1919 /* now call the dispatch function */
1920 Status
= Dispatch(DeviceObject
, Irp
);
1924 /* not supported request */
1925 Status
= KsDispatchInvalidDeviceRequest(DeviceObject
, Irp
);
1938 KsSetMajorFunctionHandler(
1939 IN PDRIVER_OBJECT DriverObject
,
1940 IN ULONG MajorFunction
)
1942 DPRINT("KsSetMajorFunctionHandler Function %x\n", MajorFunction
);
1947 DriverObject
->MajorFunction
[IRP_MJ_CREATE
] = KspCreate
;
1950 switch ( MajorFunction
)
1953 DriverObject
->MajorFunction
[MajorFunction
] = KspCreate
;
1955 case IRP_MJ_DEVICE_CONTROL
:
1959 case IRP_MJ_FLUSH_BUFFERS
:
1960 case IRP_MJ_QUERY_SECURITY
:
1961 case IRP_MJ_SET_SECURITY
:
1962 DriverObject
->MajorFunction
[MajorFunction
] = KspDispatchIrp
;
1965 DPRINT1("NotSupported %x\n", MajorFunction
);
1966 return STATUS_INVALID_PARAMETER
;
1969 return STATUS_SUCCESS
;
1979 IN PDEVICE_OBJECT DeviceObject
,
1982 PIO_STACK_LOCATION IoStack
;
1983 PKSIDEVICE_HEADER DeviceHeader
;
1984 PDEVICE_EXTENSION DeviceExtension
;
1986 DPRINT("KsDispatchIrp DeviceObject %p Irp %p\n", DeviceObject
, Irp
);
1988 /* get device extension */
1989 DeviceExtension
= (PDEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
1990 /* get device header */
1991 DeviceHeader
= DeviceExtension
->DeviceHeader
;
1994 /* get current irp stack */
1995 IoStack
= IoGetCurrentIrpStackLocation(Irp
);
1997 if (IoStack
->MajorFunction
<= IRP_MJ_DEVICE_CONTROL
)
1999 if (IoStack
->MajorFunction
== IRP_MJ_CREATE
)
2001 /* check internal type */
2002 if (DeviceHeader
->lpVtblIKsDevice
) /* FIXME improve check */
2004 /* AVStream client */
2005 return IKsDevice_Create(DeviceObject
, Irp
);
2009 /* external client (portcls) */
2010 return KspCreate(DeviceObject
, Irp
);
2014 switch (IoStack
->MajorFunction
)
2019 case IRP_MJ_FLUSH_BUFFERS
:
2020 case IRP_MJ_QUERY_SECURITY
:
2021 case IRP_MJ_SET_SECURITY
:
2023 case IRP_MJ_DEVICE_CONTROL
:
2024 return KspDispatchIrp(DeviceObject
, Irp
);
2026 return KsDispatchInvalidDeviceRequest(DeviceObject
, Irp
);
2030 /* dispatch power */
2031 if (IoStack
->MajorFunction
== IRP_MJ_POWER
)
2033 /* check internal type */
2034 if (DeviceHeader
->lpVtblIKsDevice
) /* FIXME improve check */
2036 /* AVStream client */
2037 return IKsDevice_Power(DeviceObject
, Irp
);
2041 /* external client (portcls) */
2042 return KsDefaultDispatchPower(DeviceObject
, Irp
);
2045 else if (IoStack
->MajorFunction
== IRP_MJ_PNP
) /* dispatch pnp */
2047 /* check internal type */
2048 if (DeviceHeader
->lpVtblIKsDevice
) /* FIXME improve check */
2050 /* AVStream client */
2051 return IKsDevice_Pnp(DeviceObject
, Irp
);
2055 /* external client (portcls) */
2056 return KsDefaultDispatchPnp(DeviceObject
, Irp
);
2059 else if (IoStack
->MajorFunction
== IRP_MJ_SYSTEM_CONTROL
)
2062 return KsDefaultForwardIrp(DeviceObject
, Irp
);
2067 return KsDispatchInvalidDeviceRequest(DeviceObject
, Irp
);