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 */
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_PROPERTY
)
1182 if (IoStack
->Parameters
.DeviceIoControl
.IoControlCode
== IOCTL_KS_RESET_STATE
)
1185 Status
= STATUS_SUCCESS
;
1189 /* request unsupported */
1190 Status
= STATUS_INVALID_DEVICE_REQUEST
;
1195 /* property / method / event not found */
1196 Status
= STATUS_PROPSET_NOT_FOUND
;
1199 /* complete request */
1200 Irp
->IoStatus
.Status
= Status
;
1201 CompleteRequest(Irp
, IO_NO_INCREMENT
);
1213 KsDispatchFastIoDeviceControlFailure(
1214 IN PFILE_OBJECT FileObject
,
1216 IN PVOID InputBuffer OPTIONAL
,
1217 IN ULONG InputBufferLength
,
1218 OUT PVOID OutputBuffer OPTIONAL
,
1219 IN ULONG OutputBufferLength
,
1220 IN ULONG IoControlCode
,
1221 OUT PIO_STATUS_BLOCK IoStatus
,
1222 IN PDEVICE_OBJECT DeviceObject
)
1233 KsDispatchFastReadFailure(
1234 IN PFILE_OBJECT FileObject
,
1235 IN PLARGE_INTEGER FileOffset
,
1240 OUT PIO_STATUS_BLOCK IoStatus
,
1241 IN PDEVICE_OBJECT DeviceObject
)
1254 IN OUT PLIST_ENTRY QueueHead
,
1255 IN PKSPIN_LOCK SpinLock
)
1257 PDRIVER_CANCEL OldDriverCancel
;
1258 PIO_STACK_LOCATION IoStack
;
1260 PLIST_ENTRY NextEntry
;
1264 /* acquire spinlock */
1265 KeAcquireSpinLock(SpinLock
, &OldLevel
);
1266 /* point to first entry */
1267 Entry
= QueueHead
->Flink
;
1268 /* loop all items */
1269 while(Entry
!= QueueHead
)
1271 /* get irp offset */
1272 Irp
= (PIRP
)CONTAINING_RECORD(Entry
, IRP
, Tail
.Overlay
.ListEntry
);
1274 /* get next entry */
1275 NextEntry
= Entry
->Flink
;
1277 /* set cancelled bit */
1280 /* now set the cancel routine */
1281 OldDriverCancel
= IoSetCancelRoutine(Irp
, NULL
);
1282 if (OldDriverCancel
)
1284 /* this irp hasnt been yet used, so free to cancel */
1285 KeReleaseSpinLock(SpinLock
, OldLevel
);
1287 /* get current irp stack */
1288 IoStack
= IoGetCurrentIrpStackLocation(Irp
);
1290 /* acquire cancel spinlock */
1291 IoAcquireCancelSpinLock(&Irp
->CancelIrql
);
1293 /* call provided cancel routine */
1294 OldDriverCancel(IoStack
->DeviceObject
, Irp
);
1296 /* re-acquire spinlock */
1297 KeAcquireSpinLock(SpinLock
, &OldLevel
);
1300 /* move on to next entry */
1304 /* the irp has already been canceled */
1305 KeReleaseSpinLock(SpinLock
, OldLevel
);
1315 KsReleaseIrpOnCancelableQueue(
1317 IN PDRIVER_CANCEL DriverCancel OPTIONAL
)
1319 PKSPIN_LOCK SpinLock
;
1320 PDRIVER_CANCEL OldDriverCancel
;
1321 PIO_STACK_LOCATION IoStack
;
1324 /* check for required parameters */
1330 /* default to KsCancelRoutine */
1331 DriverCancel
= KsCancelRoutine
;
1334 /* get current irp stack */
1335 IoStack
= IoGetCurrentIrpStackLocation(Irp
);
1337 /* get internal queue lock */
1338 SpinLock
= KSQUEUE_SPINLOCK_IRP_STORAGE(Irp
);
1340 /* acquire spinlock */
1341 KeAcquireSpinLock(SpinLock
, &OldLevel
);
1343 /* now set the cancel routine */
1344 OldDriverCancel
= IoSetCancelRoutine(Irp
, DriverCancel
);
1346 if (Irp
->Cancel
&& OldDriverCancel
== NULL
)
1348 /* the irp has already been canceled */
1349 KeReleaseSpinLock(SpinLock
, OldLevel
);
1351 /* cancel routine requires that cancel spinlock is held */
1352 IoAcquireCancelSpinLock(&Irp
->CancelIrql
);
1355 DriverCancel(IoStack
->DeviceObject
, Irp
);
1360 KeReleaseSpinLock(SpinLock
, OldLevel
);
1370 KsRemoveIrpFromCancelableQueue(
1371 IN OUT PLIST_ENTRY QueueHead
,
1372 IN PKSPIN_LOCK SpinLock
,
1373 IN KSLIST_ENTRY_LOCATION ListLocation
,
1374 IN KSIRP_REMOVAL_OPERATION RemovalOperation
)
1377 PLIST_ENTRY CurEntry
;
1380 DPRINT("KsRemoveIrpFromCancelableQueue ListHead %p SpinLock %p ListLocation %x RemovalOperation %x\n", QueueHead
, SpinLock
, ListLocation
, RemovalOperation
);
1382 /* check parameters */
1383 if (!QueueHead
|| !SpinLock
)
1386 /* check if parameter ListLocation is valid */
1387 if (ListLocation
!= KsListEntryTail
&& ListLocation
!= KsListEntryHead
)
1390 /* acquire list lock */
1391 KeAcquireSpinLock(SpinLock
, &OldIrql
);
1393 /* point to queue head */
1394 CurEntry
= QueueHead
;
1398 /* reset irp to null */
1401 /* iterate to next entry */
1402 if (ListLocation
== KsListEntryHead
)
1403 CurEntry
= CurEntry
->Flink
;
1405 CurEntry
= CurEntry
->Blink
;
1407 /* is the end of list reached */
1408 if (CurEntry
== QueueHead
)
1410 /* reached end of list */
1414 /* get irp offset */
1415 Irp
= (PIRP
)CONTAINING_RECORD(CurEntry
, IRP
, Tail
.Overlay
.ListEntry
);
1419 /* irp has been canceled */
1423 if (Irp
->CancelRoutine
)
1425 /* remove cancel routine */
1426 Irp
->CancelRoutine
= NULL
;
1428 if (RemovalOperation
== KsAcquireAndRemove
|| RemovalOperation
== KsAcquireAndRemoveOnlySingleItem
)
1430 /* remove irp from list */
1431 RemoveEntryList(&Irp
->Tail
.Overlay
.ListEntry
);
1434 if (RemovalOperation
== KsAcquireAndRemoveOnlySingleItem
|| RemovalOperation
== KsAcquireOnlySingleItem
)
1441 KeReleaseSpinLock(SpinLock
, OldIrql
);
1443 if (!Irp
|| Irp
->CancelRoutine
== NULL
)
1445 /* either an irp has been acquired or nothing found */
1449 /* time to remove the canceled irp */
1450 IoAcquireCancelSpinLock(&OldIrql
);
1451 /* acquire list lock */
1452 KeAcquireSpinLockAtDpcLevel(SpinLock
);
1454 if (RemovalOperation
== KsAcquireAndRemove
|| RemovalOperation
== KsAcquireAndRemoveOnlySingleItem
)
1457 RemoveEntryList(&Irp
->Tail
.Overlay
.ListEntry
);
1460 /* release list lock */
1461 KeReleaseSpinLockFromDpcLevel(SpinLock
);
1463 /* release cancel spinlock */
1464 IoReleaseCancelSpinLock(OldIrql
);
1465 /* no non canceled irp has been found */
1475 KsMoveIrpsOnCancelableQueue(
1476 IN OUT PLIST_ENTRY SourceList
,
1477 IN PKSPIN_LOCK SourceLock
,
1478 IN OUT PLIST_ENTRY DestinationList
,
1479 IN PKSPIN_LOCK DestinationLock OPTIONAL
,
1480 IN KSLIST_ENTRY_LOCATION ListLocation
,
1481 IN PFNKSIRPLISTCALLBACK ListCallback
,
1485 PLIST_ENTRY SrcEntry
;
1487 NTSTATUS Status
= STATUS_SUCCESS
;
1489 if (!DestinationLock
)
1491 /* no destination lock just acquire the source lock */
1492 KeAcquireSpinLock(SourceLock
, &OldLevel
);
1496 /* acquire cancel spinlock */
1497 IoAcquireCancelSpinLock(&OldLevel
);
1499 /* now acquire source lock */
1500 KeAcquireSpinLockAtDpcLevel(SourceLock
);
1502 /* now acquire destination lock */
1503 KeAcquireSpinLockAtDpcLevel(DestinationLock
);
1506 /* point to list head */
1507 SrcEntry
= SourceList
;
1509 /* now move all irps */
1512 if (ListLocation
== KsListEntryTail
)
1514 /* move queue downwards */
1515 SrcEntry
= SrcEntry
->Flink
;
1519 /* move queue upwards */
1520 SrcEntry
= SrcEntry
->Blink
;
1523 if (SrcEntry
== SourceList
)
1525 /* eof list reached */
1529 /* get irp offset */
1530 Irp
= (PIRP
)CONTAINING_RECORD(SrcEntry
, IRP
, Tail
.Overlay
.ListEntry
);
1532 /* now check if irp can be moved */
1533 Status
= ListCallback(Irp
, Context
);
1535 /* check if irp can be moved */
1536 if (Status
== STATUS_SUCCESS
)
1538 /* remove irp from src list */
1539 RemoveEntryList(&Irp
->Tail
.Overlay
.ListEntry
);
1541 if (ListLocation
== KsListEntryTail
)
1543 /* insert irp end of list */
1544 InsertTailList(DestinationList
, &Irp
->Tail
.Overlay
.ListEntry
);
1548 /* insert irp head of list */
1549 InsertHeadList(DestinationList
, &Irp
->Tail
.Overlay
.ListEntry
);
1552 /* do we need to update the irp lock */
1553 if (DestinationLock
)
1555 /* update irp lock */
1556 KSQUEUE_SPINLOCK_IRP_STORAGE(Irp
) = DestinationLock
;
1561 if (Status
!= STATUS_NO_MATCH
)
1563 /* callback decided to stop enumeration */
1567 /* reset return value */
1568 Status
= STATUS_SUCCESS
;
1572 if (!DestinationLock
)
1574 /* release source lock */
1575 KeReleaseSpinLock(SourceLock
, OldLevel
);
1579 /* now release destination lock */
1580 KeReleaseSpinLockFromDpcLevel(DestinationLock
);
1582 /* now release source lock */
1583 KeReleaseSpinLockFromDpcLevel(SourceLock
);
1586 /* now release cancel spinlock */
1587 IoReleaseCancelSpinLock(OldLevel
);
1600 KsRemoveSpecificIrpFromCancelableQueue(
1603 PKSPIN_LOCK SpinLock
;
1606 DPRINT("KsRemoveSpecificIrpFromCancelableQueue %p\n", Irp
);
1608 /* get internal queue lock */
1609 SpinLock
= KSQUEUE_SPINLOCK_IRP_STORAGE(Irp
);
1611 /* acquire spinlock */
1612 KeAcquireSpinLock(SpinLock
, &OldLevel
);
1614 /* remove the irp from the list */
1615 RemoveEntryList(&Irp
->Tail
.Overlay
.ListEntry
);
1617 /* release spinlock */
1618 KeReleaseSpinLock(SpinLock
, OldLevel
);
1628 KsAddIrpToCancelableQueue(
1629 IN OUT PLIST_ENTRY QueueHead
,
1630 IN PKSPIN_LOCK SpinLock
,
1632 IN KSLIST_ENTRY_LOCATION ListLocation
,
1633 IN PDRIVER_CANCEL DriverCancel OPTIONAL
)
1635 PDRIVER_CANCEL OldDriverCancel
;
1636 PIO_STACK_LOCATION IoStack
;
1639 /* check for required parameters */
1640 if (!QueueHead
|| !SpinLock
|| !Irp
)
1643 /* get current irp stack */
1644 IoStack
= IoGetCurrentIrpStackLocation(Irp
);
1646 DPRINT("KsAddIrpToCancelableQueue QueueHead %p SpinLock %p Irp %p ListLocation %x DriverCancel %p\n", QueueHead
, SpinLock
, Irp
, ListLocation
, DriverCancel
);
1648 // HACK for ms portcls
1649 if (IoStack
->MajorFunction
== IRP_MJ_CREATE
)
1651 // complete the request
1652 DPRINT1("MS HACK\n");
1653 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
1654 CompleteRequest(Irp
, IO_NO_INCREMENT
);
1662 /* default to KsCancelRoutine */
1663 DriverCancel
= KsCancelRoutine
;
1667 /* acquire spinlock */
1668 KeAcquireSpinLock(SpinLock
, &OldLevel
);
1670 if (ListLocation
== KsListEntryTail
)
1672 /* insert irp to tail of list */
1673 InsertTailList(QueueHead
, &Irp
->Tail
.Overlay
.ListEntry
);
1677 /* insert irp to head of list */
1678 InsertHeadList(QueueHead
, &Irp
->Tail
.Overlay
.ListEntry
);
1681 /* store internal queue lock */
1682 KSQUEUE_SPINLOCK_IRP_STORAGE(Irp
) = SpinLock
;
1684 /* now set the cancel routine */
1685 OldDriverCancel
= IoSetCancelRoutine(Irp
, DriverCancel
);
1687 if (Irp
->Cancel
&& OldDriverCancel
== NULL
)
1689 /* the irp has already been canceled */
1690 KeReleaseSpinLock(SpinLock
, OldLevel
);
1692 /* cancel routine requires that cancel spinlock is held */
1693 IoAcquireCancelSpinLock(&Irp
->CancelIrql
);
1696 DriverCancel(IoStack
->DeviceObject
, Irp
);
1701 KeReleaseSpinLock(SpinLock
, OldLevel
);
1712 IN PDEVICE_OBJECT DeviceObject
,
1715 PKSPIN_LOCK SpinLock
;
1717 /* get internal queue lock */
1718 SpinLock
= KSQUEUE_SPINLOCK_IRP_STORAGE(Irp
);
1720 /* acquire spinlock */
1721 KeAcquireSpinLockAtDpcLevel(SpinLock
);
1724 ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL
);
1726 /* release cancel spinlock */
1727 IoReleaseCancelSpinLock(Irp
->CancelIrql
);
1729 /* remove the irp from the list */
1730 RemoveEntryList(&Irp
->Tail
.Overlay
.ListEntry
);
1732 /* release spinlock */
1733 KeReleaseSpinLock(SpinLock
, Irp
->CancelIrql
);
1735 /* has the irp already been canceled */
1736 if (Irp
->IoStatus
.Status
!= STATUS_CANCELLED
)
1738 /* let's complete it */
1739 Irp
->IoStatus
.Status
= STATUS_CANCELLED
;
1740 CompleteRequest(Irp
, IO_NO_INCREMENT
);
1745 FindMatchingCreateItem(
1746 PLIST_ENTRY ListHead
,
1749 OUT PCREATE_ITEM_ENTRY
*OutCreateItem
)
1752 PCREATE_ITEM_ENTRY CreateItemEntry
;
1753 UNICODE_STRING RefString
;
1756 /* get terminator */
1757 pStr
= wcschr(Buffer
, L
'\\');
1760 ASSERT(pStr
!= NULL
);
1765 RtlInitUnicodeString(&RefString
, ++pStr
);
1769 // request is for pin / node / allocator
1770 RefString
.Buffer
= Buffer
;
1771 RefString
.Length
= BufferSize
= RefString
.MaximumLength
= ((ULONG_PTR
)pStr
- (ULONG_PTR
)Buffer
);
1774 /* point to first entry */
1775 Entry
= ListHead
->Flink
;
1777 /* loop all device items */
1778 while(Entry
!= ListHead
)
1780 /* get create item entry */
1781 CreateItemEntry
= (PCREATE_ITEM_ENTRY
)CONTAINING_RECORD(Entry
, CREATE_ITEM_ENTRY
, Entry
);
1783 ASSERT(CreateItemEntry
->CreateItem
);
1785 if(CreateItemEntry
->CreateItem
->Flags
& KSCREATE_ITEM_WILDCARD
)
1787 /* create item is default */
1788 *OutCreateItem
= CreateItemEntry
;
1789 return STATUS_SUCCESS
;
1792 if (!CreateItemEntry
->CreateItem
->Create
)
1794 /* skip free create item */
1795 Entry
= Entry
->Flink
;
1799 DPRINT("CreateItem %S Length %u Request %wZ %u\n", CreateItemEntry
->CreateItem
->ObjectClass
.Buffer
,
1800 CreateItemEntry
->CreateItem
->ObjectClass
.Length
,
1804 if (CreateItemEntry
->CreateItem
->ObjectClass
.Length
> RefString
.Length
)
1806 /* create item doesnt match in length */
1807 Entry
= Entry
->Flink
;
1811 /* now check if the object class is the same */
1812 if (!RtlCompareUnicodeString(&CreateItemEntry
->CreateItem
->ObjectClass
, &RefString
, TRUE
))
1814 /* found matching create item */
1815 *OutCreateItem
= CreateItemEntry
;
1816 return STATUS_SUCCESS
;
1818 /* iterate to next */
1819 Entry
= Entry
->Flink
;
1822 return STATUS_NOT_FOUND
;
1828 IN PDEVICE_OBJECT DeviceObject
,
1831 PCREATE_ITEM_ENTRY CreateItemEntry
;
1832 PIO_STACK_LOCATION IoStack
;
1833 PDEVICE_EXTENSION DeviceExtension
;
1834 PKSIDEVICE_HEADER DeviceHeader
;
1835 PKSIOBJECT_HEADER ObjectHeader
;
1838 DPRINT("KS / CREATE\n");
1840 /* get current stack location */
1841 IoStack
= IoGetCurrentIrpStackLocation(Irp
);
1842 /* get device extension */
1843 DeviceExtension
= (PDEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
1844 /* get device header */
1845 DeviceHeader
= DeviceExtension
->DeviceHeader
;
1848 if (IoStack
->FileObject
->FileName
.Buffer
== NULL
)
1850 /* FIXME Pnp-Issue */
1851 DPRINT("Using reference string hack\n");
1852 Irp
->IoStatus
.Information
= 0;
1853 /* set return status */
1854 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
1855 CompleteRequest(Irp
, IO_NO_INCREMENT
);
1856 return STATUS_SUCCESS
;
1859 if (IoStack
->FileObject
->RelatedFileObject
!= NULL
)
1861 /* request is to instantiate a pin / node / clock / allocator */
1862 ObjectHeader
= (PKSIOBJECT_HEADER
)IoStack
->FileObject
->RelatedFileObject
->FsContext2
;
1865 ASSERT(ObjectHeader
);
1867 /* find a matching a create item */
1868 Status
= FindMatchingCreateItem(&ObjectHeader
->ItemList
, IoStack
->FileObject
->FileName
.Length
, IoStack
->FileObject
->FileName
.Buffer
, &CreateItemEntry
);
1872 /* request to create a filter */
1873 Status
= FindMatchingCreateItem(&DeviceHeader
->ItemList
, IoStack
->FileObject
->FileName
.Length
, IoStack
->FileObject
->FileName
.Buffer
, &CreateItemEntry
);
1876 if (NT_SUCCESS(Status
))
1878 /* set object create item */
1879 KSCREATE_ITEM_IRP_STORAGE(Irp
) = CreateItemEntry
->CreateItem
;
1881 /* call create function */
1882 Status
= CreateItemEntry
->CreateItem
->Create(DeviceObject
, Irp
);
1884 if (NT_SUCCESS(Status
))
1886 /* increment create item reference count */
1887 InterlockedIncrement(&CreateItemEntry
->ReferenceCount
);
1892 Irp
->IoStatus
.Information
= 0;
1893 /* set return status */
1894 Irp
->IoStatus
.Status
= STATUS_UNSUCCESSFUL
;
1895 CompleteRequest(Irp
, IO_NO_INCREMENT
);
1896 return STATUS_UNSUCCESSFUL
;
1902 IN PDEVICE_OBJECT DeviceObject
,
1905 PIO_STACK_LOCATION IoStack
;
1906 PDEVICE_EXTENSION DeviceExtension
;
1907 PKSIOBJECT_HEADER ObjectHeader
;
1908 PKSIDEVICE_HEADER DeviceHeader
;
1909 PDRIVER_DISPATCH Dispatch
;
1912 /* get current stack location */
1913 IoStack
= IoGetCurrentIrpStackLocation(Irp
);
1915 /* get device extension */
1916 DeviceExtension
= (PDEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
1917 /* get device header */
1918 DeviceHeader
= DeviceExtension
->DeviceHeader
;
1920 ASSERT(IoStack
->FileObject
);
1922 /* get object header */
1923 ObjectHeader
= (PKSIOBJECT_HEADER
) IoStack
->FileObject
->FsContext2
;
1927 /* FIXME Pnp-Issue*/
1928 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
1929 Irp
->IoStatus
.Information
= 0;
1930 /* complete and forget */
1931 CompleteRequest(Irp
, IO_NO_INCREMENT
);
1932 return STATUS_SUCCESS
;
1936 ASSERT(ObjectHeader
);
1937 /* store create item */
1938 //KSCREATE_ITEM_IRP_STORAGE(Irp) = (PKSOBJECT_CREATE_ITEM)0x12345678; //ObjectHeader->CreateItem;
1940 /* retrieve matching dispatch function */
1941 switch(IoStack
->MajorFunction
)
1944 Dispatch
= ObjectHeader
->DispatchTable
.Close
;
1946 case IRP_MJ_DEVICE_CONTROL
:
1947 Dispatch
= ObjectHeader
->DispatchTable
.DeviceIoControl
;
1950 Dispatch
= ObjectHeader
->DispatchTable
.Read
;
1953 Dispatch
= ObjectHeader
->DispatchTable
.Write
;
1955 case IRP_MJ_FLUSH_BUFFERS
:
1956 Dispatch
= ObjectHeader
->DispatchTable
.Flush
;
1958 case IRP_MJ_QUERY_SECURITY
:
1959 Dispatch
= ObjectHeader
->DispatchTable
.QuerySecurity
;
1961 case IRP_MJ_SET_SECURITY
:
1962 Dispatch
= ObjectHeader
->DispatchTable
.SetSecurity
;
1965 Dispatch
= KsDefaultDispatchPnp
;
1970 /* is the request supported */
1973 /* now call the dispatch function */
1974 Status
= Dispatch(DeviceObject
, Irp
);
1978 /* not supported request */
1979 Status
= KsDispatchInvalidDeviceRequest(DeviceObject
, Irp
);
1992 KsSetMajorFunctionHandler(
1993 IN PDRIVER_OBJECT DriverObject
,
1994 IN ULONG MajorFunction
)
1996 DPRINT("KsSetMajorFunctionHandler Function %x\n", MajorFunction
);
1998 switch ( MajorFunction
)
2001 DriverObject
->MajorFunction
[MajorFunction
] = KspCreate
;
2003 case IRP_MJ_DEVICE_CONTROL
:
2007 case IRP_MJ_FLUSH_BUFFERS
:
2008 case IRP_MJ_QUERY_SECURITY
:
2009 case IRP_MJ_SET_SECURITY
:
2010 DriverObject
->MajorFunction
[MajorFunction
] = KspDispatchIrp
;
2013 DPRINT1("NotSupported %x\n", MajorFunction
);
2014 return STATUS_INVALID_PARAMETER
;
2017 return STATUS_SUCCESS
;
2027 IN PDEVICE_OBJECT DeviceObject
,
2030 PIO_STACK_LOCATION IoStack
;
2031 PKSIDEVICE_HEADER DeviceHeader
;
2032 PDEVICE_EXTENSION DeviceExtension
;
2034 DPRINT("KsDispatchIrp DeviceObject %p Irp %p\n", DeviceObject
, Irp
);
2036 /* get device extension */
2037 DeviceExtension
= (PDEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
2039 /* get device header */
2040 DeviceHeader
= DeviceExtension
->DeviceHeader
;
2043 /* get current irp stack */
2044 IoStack
= IoGetCurrentIrpStackLocation(Irp
);
2046 if (IoStack
->MajorFunction
<= IRP_MJ_DEVICE_CONTROL
)
2048 if (IoStack
->MajorFunction
== IRP_MJ_CREATE
)
2050 /* check internal type */
2051 if (DeviceHeader
->BasicHeader
.OuterUnknown
) /* FIXME improve check */
2053 /* AVStream client */
2054 return IKsDevice_Create(DeviceObject
, Irp
);
2058 /* external client (portcls) */
2059 return KspCreate(DeviceObject
, Irp
);
2063 switch (IoStack
->MajorFunction
)
2068 case IRP_MJ_FLUSH_BUFFERS
:
2069 case IRP_MJ_QUERY_SECURITY
:
2070 case IRP_MJ_SET_SECURITY
:
2072 case IRP_MJ_DEVICE_CONTROL
:
2073 return KspDispatchIrp(DeviceObject
, Irp
);
2075 return KsDispatchInvalidDeviceRequest(DeviceObject
, Irp
);
2079 /* dispatch power */
2080 if (IoStack
->MajorFunction
== IRP_MJ_POWER
)
2082 /* check internal type */
2083 if (DeviceHeader
->BasicHeader
.OuterUnknown
) /* FIXME improve check */
2085 /* AVStream client */
2086 return IKsDevice_Power(DeviceObject
, Irp
);
2090 /* external client (portcls) */
2091 return KsDefaultDispatchPower(DeviceObject
, Irp
);
2094 else if (IoStack
->MajorFunction
== IRP_MJ_PNP
) /* dispatch pnp */
2096 /* check internal type */
2097 if (DeviceHeader
->BasicHeader
.OuterUnknown
) /* FIXME improve check */
2099 /* AVStream client */
2100 return IKsDevice_Pnp(DeviceObject
, Irp
);
2104 /* external client (portcls) */
2105 return KsDefaultDispatchPnp(DeviceObject
, Irp
);
2108 else if (IoStack
->MajorFunction
== IRP_MJ_SYSTEM_CONTROL
)
2111 return KsDefaultForwardIrp(DeviceObject
, Irp
);
2116 return KsDispatchInvalidDeviceRequest(DeviceObject
, Irp
);
2130 return KSFILTER_NODE
;