Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

7120 lines
207 KiB

  1. /*
  2. Copyright (c) 1996 Microsoft Corporation
  3. Module Name:
  4. upperapi.c
  5. Abstract:
  6. This is the WDM streaming class driver. This module contains code related
  7. to the driver's upper edge api.
  8. Author:
  9. billpa
  10. Environment:
  11. Kernel mode only
  12. Revision History:
  13. --*/
  14. #include "codcls.h"
  15. #include "ksguid.h"
  16. #ifdef ALLOC_PRAGMA
  17. #pragma alloc_text(PAGE, FilterDispatchGlobalCreate)
  18. #pragma alloc_text(PAGE, FilterDispatchIoControl)
  19. #pragma alloc_text(PAGE, FilterDispatchClose)
  20. #pragma alloc_text(PAGE, StreamDispatchCreate)
  21. #pragma alloc_text(PAGE, StreamDispatchIoControl)
  22. #pragma alloc_text(PAGE, StreamDispatchClose)
  23. #pragma alloc_text(PAGE, ClockDispatchCreate)
  24. #pragma alloc_text(PAGE, ClockDispatchIoControl)
  25. #pragma alloc_text(PAGE, ClockDispatchClose)
  26. #pragma alloc_text(PAGE, StreamClassMinidriverDeviceGetProperty)
  27. #pragma alloc_text(PAGE, StreamClassMinidriverDeviceSetProperty)
  28. #pragma alloc_text(PAGE, StreamClassMinidriverStreamGetProperty)
  29. #pragma alloc_text(PAGE, StreamClassMinidriverStreamSetProperty)
  30. #pragma alloc_text(PAGE, StreamClassNull)
  31. #pragma alloc_text(PAGE, SCStreamDeviceState)
  32. #pragma alloc_text(PAGE, SCStreamDeviceRate)
  33. #pragma alloc_text(PAGE, SCFilterPinInstances)
  34. #pragma alloc_text(PAGE, SCFilterPinPropertyHandler)
  35. #pragma alloc_text(PAGE, SCOpenStreamCallback)
  36. #pragma alloc_text(PAGE, SCOpenMasterCallback)
  37. #if ENABLE_MULTIPLE_FILTER_TYPES
  38. #else
  39. #pragma alloc_text(PAGE, SCGlobalInstanceCallback)
  40. #endif
  41. #pragma alloc_text(PAGE, SCSetMasterClock)
  42. #pragma alloc_text(PAGE, SCClockGetTime)
  43. #pragma alloc_text(PAGE, SCGetStreamDeviceState)
  44. #pragma alloc_text(PAGE, SCStreamDeviceRateCapability)
  45. #pragma alloc_text(PAGE, SCStreamProposeNewFormat)
  46. #pragma alloc_text(PAGE, SCStreamSetFormat)
  47. #pragma alloc_text(PAGE, AllocatorDispatchCreate)
  48. #pragma alloc_text(PAGE, SCGetMasterClock)
  49. #pragma alloc_text(PAGE, SCClockGetPhysicalTime)
  50. #pragma alloc_text(PAGE, SCClockGetSynchronizedTime)
  51. #pragma alloc_text(PAGE, SCClockGetFunctionTable)
  52. #pragma alloc_text(PAGE, SCCloseClockCallback)
  53. #pragma alloc_text(PAGE, SCFilterTopologyHandler)
  54. #pragma alloc_text(PAGE, SCFilterPinIntersectionHandler)
  55. #pragma alloc_text(PAGE, SCIntersectHandler)
  56. #pragma alloc_text(PAGE, SCDataIntersectionCallback)
  57. #pragma alloc_text(PAGE, SCGetStreamHeaderSize)
  58. #pragma alloc_text(PAGE, DllUnload)
  59. #ifdef ENABLE_STREAM_CLASS_AS_ALLOCATOR
  60. #pragma alloc_text(PAGE, SCStreamAllocator)
  61. #pragma alloc_text(PAGE, AllocateFrame)
  62. #pragma alloc_text(PAGE, FreeFrame)
  63. #pragma alloc_text(PAGE, PrepareTransfer)
  64. #pragma alloc_text(PAGE, PinCreateHandler)
  65. #endif
  66. #endif
  67. #ifdef ALLOC_DATA_PRAGMA
  68. #pragma const_seg("PAGECONST")
  69. #endif
  70. static const WCHAR ClockTypeName[] = KSSTRING_Clock;
  71. static const WCHAR AllocatorTypeName[] = KSSTRING_Allocator;
  72. //
  73. // this structure is the dispatch table for a filter instance of the device
  74. //
  75. DEFINE_KSDISPATCH_TABLE(
  76. FilterDispatchTable,
  77. FilterDispatchIoControl,
  78. KsDispatchInvalidDeviceRequest,
  79. KsDispatchInvalidDeviceRequest,
  80. KsDispatchInvalidDeviceRequest,
  81. FilterDispatchClose,
  82. NULL,
  83. NULL,
  84. NULL,
  85. NULL,
  86. NULL);
  87. //
  88. // dispatch table for pin properties that we support in the class driver
  89. //
  90. static DEFINE_KSPROPERTY_TABLE(PinPropertyHandlers)
  91. {
  92. DEFINE_KSPROPERTY_ITEM_PIN_CINSTANCES(SCFilterPinInstances),
  93. DEFINE_KSPROPERTY_ITEM_PIN_CTYPES(SCFilterPinPropertyHandler),
  94. DEFINE_KSPROPERTY_ITEM_PIN_DATAFLOW(SCFilterPinPropertyHandler),
  95. DEFINE_KSPROPERTY_ITEM_PIN_DATARANGES(SCFilterPinPropertyHandler),
  96. DEFINE_KSPROPERTY_ITEM_PIN_DATAINTERSECTION(SCFilterPinIntersectionHandler),
  97. DEFINE_KSPROPERTY_ITEM_PIN_INTERFACES(SCFilterPinPropertyHandler),
  98. DEFINE_KSPROPERTY_ITEM_PIN_MEDIUMS(SCFilterPinPropertyHandler),
  99. DEFINE_KSPROPERTY_ITEM_PIN_COMMUNICATION(SCFilterPinPropertyHandler),
  100. // DEFINE_KSPROPERTY_ITEM_PIN_GLOBALCINSTANCES(),
  101. // DEFINE_KSPROPERTY_ITEM_PIN_NECESSARYINSTANCES(),
  102. // DEFINE_KSPROPERTY_ITEM_PIN_PHYSICALCONNECTION(),
  103. DEFINE_KSPROPERTY_ITEM_PIN_CATEGORY(SCFilterPinPropertyHandler),
  104. DEFINE_KSPROPERTY_ITEM_PIN_NAME(SCFilterPinPropertyHandler)
  105. };
  106. static DEFINE_KSPROPERTY_TOPOLOGYSET(
  107. TopologyPropertyHandlers,
  108. SCFilterTopologyHandler);
  109. //
  110. // filter property sets supported by the class driver
  111. //
  112. static DEFINE_KSPROPERTY_SET_TABLE(FilterPropertySets)
  113. {
  114. DEFINE_KSPROPERTY_SET(
  115. &KSPROPSETID_Pin,
  116. SIZEOF_ARRAY(PinPropertyHandlers),
  117. (PKSPROPERTY_ITEM) PinPropertyHandlers,
  118. 0, NULL),
  119. DEFINE_KSPROPERTY_SET(
  120. &KSPROPSETID_Topology,
  121. SIZEOF_ARRAY(TopologyPropertyHandlers),
  122. (PKSPROPERTY_ITEM) TopologyPropertyHandlers,
  123. 0, NULL),
  124. };
  125. //
  126. // handlers for the control properties
  127. //
  128. static DEFINE_KSPROPERTY_TABLE(StreamControlHandlers)
  129. {
  130. DEFINE_KSPROPERTY_ITEM_CONNECTION_STATE(SCGetStreamDeviceState, SCStreamDeviceState),
  131. // DEFINE_KSPROPERTY_ITEM_CONNECTION_PRIORITY(),
  132. // DEFINE_KSPROPERTY_ITEM_CONNECTION_DATAFORMAT(),
  133. // DEFINE_KSPROPERTY_ITEM_CONNECTION_ALLOCATORFRAMING(),
  134. DEFINE_KSPROPERTY_ITEM_CONNECTION_PROPOSEDATAFORMAT(SCStreamProposeNewFormat),
  135. DEFINE_KSPROPERTY_ITEM_CONNECTION_DATAFORMAT(NULL, SCStreamSetFormat),
  136. // DEFINE_KSPROPERTY_ITEM_CONNECTION_ACQUIREORDERING(),
  137. };
  138. DEFINE_KSPROPERTY_TABLE(StreamStreamHandlers)
  139. {
  140. #ifdef ENABLE_STREAM_CLASS_AS_ALLOCATOR
  141. DEFINE_KSPROPERTY_ITEM_STREAM_ALLOCATOR(SCStreamAllocator,SCStreamAllocator),
  142. #else
  143. // DEFINE_KSPROPERTY_ITEM_STREAM_ALLOCATOR(),
  144. #endif
  145. // DEFINE_KSPROPERTY_ITEM_STREAM_QUALITY(),
  146. // DEFINE_KSPROPERTY_ITEM_STREAM_DEGRADATION(),
  147. DEFINE_KSPROPERTY_ITEM_STREAM_MASTERCLOCK(NULL, SCSetMasterClock),
  148. // DEFINE_KSPROPERTY_ITEM_STREAM_TIMEFORMAT(),
  149. // DEFINE_KSPROPERTY_ITEM_STREAM_PRESENTATIONTIME(),
  150. // DEFINE_KSPROPERTY_ITEM_STREAM_PRESENTATIONEXTENT(),
  151. // DEFINE_KSPROPERTY_ITEM_STREAM_FRAMETIME(),
  152. DEFINE_KSPROPERTY_ITEM_STREAM_RATECAPABILITY(SCStreamDeviceRateCapability),
  153. DEFINE_KSPROPERTY_ITEM_STREAM_RATE(NULL, SCStreamDeviceRate),
  154. };
  155. DEFINE_KSPROPERTY_TABLE(StreamInterfaceHandlers)
  156. {
  157. {
  158. KSPROPERTY_STREAMINTERFACE_HEADERSIZE,
  159. SCGetStreamHeaderSize,
  160. 0,
  161. 0,
  162. NULL,
  163. 0,
  164. 0,
  165. NULL
  166. }
  167. };
  168. //
  169. // stream property sets supported by the class driver
  170. //
  171. static DEFINE_KSPROPERTY_SET_TABLE(StreamProperties)
  172. {
  173. DEFINE_KSPROPERTY_SET(
  174. &KSPROPSETID_Connection,
  175. SIZEOF_ARRAY(StreamControlHandlers),
  176. (PVOID) StreamControlHandlers,
  177. 0, NULL),
  178. DEFINE_KSPROPERTY_SET(
  179. &KSPROPSETID_Stream,
  180. SIZEOF_ARRAY(StreamStreamHandlers),
  181. (PVOID) StreamStreamHandlers,
  182. 0, NULL),
  183. DEFINE_KSPROPERTY_SET(
  184. &KSPROPSETID_StreamInterface,
  185. SIZEOF_ARRAY(StreamInterfaceHandlers),
  186. (PVOID) StreamInterfaceHandlers,
  187. 0, NULL),
  188. };
  189. //
  190. // template for on the fly constructed properties
  191. // DO NOT CHANGE without MODIFYING the code that references this set.
  192. //
  193. DEFINE_KSPROPERTY_TABLE(ConstructedStreamHandlers)
  194. {
  195. DEFINE_KSPROPERTY_ITEM_STREAM_MASTERCLOCK(SCGetMasterClock, SCSetMasterClock)
  196. };
  197. //
  198. // template for on-the-fly constructed property sets.
  199. // DO NOT CHANGE without MODIFYING the code that references this set.
  200. //
  201. static DEFINE_KSPROPERTY_SET_TABLE(ConstructedStreamProperties)
  202. {
  203. DEFINE_KSPROPERTY_SET(
  204. &KSPROPSETID_Stream,
  205. SIZEOF_ARRAY(ConstructedStreamHandlers),
  206. (PVOID) ConstructedStreamHandlers,
  207. 0, NULL),
  208. };
  209. static const DEFINE_KSCREATE_DISPATCH_TABLE(StreamDriverDispatch)
  210. {
  211. DEFINE_KSCREATE_ITEM(ClockDispatchCreate,
  212. ClockTypeName,
  213. 0),
  214. DEFINE_KSCREATE_ITEM(AllocatorDispatchCreate,
  215. AllocatorTypeName,
  216. 0),
  217. };
  218. //
  219. // dispatch table for stream functions
  220. //
  221. DEFINE_KSDISPATCH_TABLE(
  222. StreamDispatchTable,
  223. StreamDispatchIoControl,
  224. KsDispatchInvalidDeviceRequest,
  225. KsDispatchInvalidDeviceRequest,
  226. KsDispatchInvalidDeviceRequest,
  227. StreamDispatchClose,
  228. NULL,
  229. NULL,
  230. NULL,
  231. NULL,
  232. NULL);
  233. DEFINE_KSDISPATCH_TABLE(
  234. ClockDispatchTable,
  235. ClockDispatchIoControl,
  236. KsDispatchInvalidDeviceRequest,
  237. KsDispatchInvalidDeviceRequest,
  238. KsDispatchInvalidDeviceRequest,
  239. ClockDispatchClose,
  240. KsDispatchInvalidDeviceRequest,
  241. KsDispatchInvalidDeviceRequest,
  242. KsDispatchFastIoDeviceControlFailure,
  243. KsDispatchFastReadFailure,
  244. KsDispatchFastWriteFailure);
  245. DEFINE_KSPROPERTY_TABLE(ClockPropertyItems)
  246. {
  247. DEFINE_KSPROPERTY_ITEM_CLOCK_TIME(SCClockGetTime),
  248. DEFINE_KSPROPERTY_ITEM_CLOCK_PHYSICALTIME(SCClockGetPhysicalTime),
  249. DEFINE_KSPROPERTY_ITEM_CLOCK_CORRELATEDTIME(SCClockGetSynchronizedTime),
  250. // DEFINE_KSPROPERTY_ITEM_CLOCK_CORRELATEDPHYSICALTIME(),
  251. // DEFINE_KSPROPERTY_ITEM_CLOCK_RESOLUTION(SCClockGetResolution),
  252. // DEFINE_KSPROPERTY_ITEM_CLOCK_STATE(SCClockGetState),
  253. DEFINE_KSPROPERTY_ITEM_CLOCK_FUNCTIONTABLE(SCClockGetFunctionTable)
  254. };
  255. DEFINE_KSPROPERTY_SET_TABLE(ClockPropertySets)
  256. {
  257. DEFINE_KSPROPERTY_SET(
  258. &KSPROPSETID_Clock,
  259. SIZEOF_ARRAY(ClockPropertyItems),
  260. ClockPropertyItems,
  261. 0,
  262. NULL
  263. )
  264. };
  265. #ifdef ALLOC_DATA_PRAGMA
  266. #pragma const_seg()
  267. #endif
  268. #if ENABLE_MULTIPLE_FILTER_TYPES
  269. NTSTATUS
  270. FilterDispatchGlobalCreate(
  271. IN PDEVICE_OBJECT DeviceObject,
  272. IN PIRP Irp)
  273. /*++
  274. Routine Description:
  275. This routine receives global createfile IRP's for the device.
  276. After the Srb_Open_Device_Instance instance we immediate
  277. send Srb_Get_Stream_Info for this filter.
  278. Arguments:
  279. DeviceObject - device object for the device
  280. Irp - probably an IRP, silly
  281. Return Value:
  282. The IRP status is set as appropriate
  283. --*/
  284. {
  285. PDEVICE_EXTENSION DeviceExtension = DeviceObject->DeviceExtension;
  286. PIO_STACK_LOCATION IrpStack;
  287. PFILTER_INSTANCE FilterInstance;
  288. NTSTATUS Status; // = STATUS_TOO_MANY_OPENED_FILES;
  289. IFN_MF( PAGED_CODE());
  290. IrpStack = IoGetCurrentIrpStackLocation(Irp);
  291. DebugPrint((DebugLevelTrace,
  292. "'Closing global filter with Irp %x\n", Irp));
  293. //
  294. // show one more I/O pending & verify that we can actually do I/O.
  295. //
  296. Status = SCShowIoPending(DeviceObject->DeviceExtension, Irp);
  297. if ( !NT_SUCCESS ( Status )) {
  298. //
  299. // the device is currently not accessible, so just return with error
  300. //
  301. return (Status);
  302. } // if !showiopending
  303. //
  304. // if the device is not started, bail out.
  305. // swenum enables device interfaces very early. It should not have
  306. // done that for the pdo. we, the fdo, should be the one to
  307. // enable this. for now, try to work around the problem that we
  308. // come here before device is started.
  309. //
  310. if ( DeviceExtension->RegistryFlags & DRIVER_USES_SWENUM_TO_LOAD ) {
  311. #define OPEN_TIMEOUT -1000*1000 // 100 mili second
  312. #define OPEN_WAIT 50
  313. LARGE_INTEGER liOpenTimeOut;
  314. int i;
  315. liOpenTimeOut.QuadPart = OPEN_TIMEOUT;
  316. for ( i=0; i < OPEN_WAIT; i++ ) {
  317. if ( DeviceExtension->Flags & DEVICE_FLAGS_PNP_STARTED ) {
  318. break;
  319. }
  320. KeDelayExecutionThread( KernelMode, FALSE, &liOpenTimeOut );
  321. }
  322. if ( 0 == (DeviceExtension->Flags & DEVICE_FLAGS_PNP_STARTED) ) {
  323. Status = STATUS_DEVICE_NOT_READY;
  324. Irp->IoStatus.Status = Status;
  325. IoCompleteRequest(Irp, IO_NO_INCREMENT);
  326. DebugPrint((DebugLevelError,
  327. "SWEnum device %p not ready!\n", DeviceObject));
  328. return Status;
  329. }
  330. }
  331. //
  332. // show one more reference to driver.
  333. //
  334. SCReferenceDriver(DeviceExtension);
  335. //
  336. // set the context of createfiles for the filter
  337. //
  338. KeWaitForSingleObject(&DeviceExtension->ControlEvent,
  339. Executive,
  340. KernelMode,
  341. FALSE,// not alertable
  342. NULL);
  343. //
  344. // Make sure adapter is powered on
  345. //
  346. SCCheckPoweredUp(DeviceExtension);
  347. Status = SCOpenMinidriverInstance(DeviceExtension,
  348. &FilterInstance,
  349. NULL, //SCGlobalInstanceCallback,
  350. Irp);
  351. //
  352. // if status != success, we failed so dereference the
  353. // driver.
  354. //
  355. if (!NT_SUCCESS(Status)) {
  356. //
  357. // show one fewer reference to driver.
  358. //
  359. SCDereferenceDriver(DeviceExtension);
  360. }
  361. else {
  362. //
  363. // Open is successul. Fill in the filter dispatch table pointer
  364. //
  365. if ( 0 == DeviceExtension->NumberOfOpenInstances ||
  366. 0 != DeviceExtension->FilterExtensionSize ) {
  367. //
  368. // 1st open of 1x1 or non 1x1 ( i.e. instance opne )
  369. //
  370. // add FilterInstance to DeviceExtension except non-1st open of legacy 1x1
  371. //
  372. PHW_STREAM_DESCRIPTOR StreamDescriptor, StreamDescriptorI;
  373. ULONG nPins;
  374. //
  375. // remeber DO for later
  376. //
  377. FilterInstance->DeviceObject = DeviceObject;
  378. //
  379. // Process stream info for this filterinstance
  380. //
  381. StreamDescriptorI = DeviceExtension->FilterTypeInfos
  382. [FilterInstance->FilterTypeIndex].StreamDescriptor;
  383. nPins = StreamDescriptorI->StreamHeader.NumberOfStreams;
  384. StreamDescriptor =
  385. ExAllocatePool( NonPagedPool,
  386. sizeof(HW_STREAM_HEADER) +
  387. sizeof(HW_STREAM_INFORMATION) * nPins );
  388. if ( NULL != StreamDescriptor ) {
  389. RtlCopyMemory( StreamDescriptor,
  390. StreamDescriptorI,
  391. sizeof(HW_STREAM_HEADER) +
  392. sizeof(HW_STREAM_INFORMATION) * nPins );
  393. Status = SciOnFilterStreamDescriptor(
  394. FilterInstance,
  395. StreamDescriptor);
  396. if ( NT_SUCCESS( Status ) ) {
  397. FilterInstance->StreamDescriptor = StreamDescriptor;
  398. DebugPrint((DebugLevelInfo,
  399. "NumNameExtensions=%x NumopenInstances=%x "
  400. "FilterInstance %x StreamDescriptor %x\n",
  401. DeviceExtension->NumberOfNameExtensions,
  402. DeviceExtension->NumberOfOpenInstances,
  403. FilterInstance,
  404. StreamDescriptor));
  405. }
  406. }
  407. else {
  408. Status = STATUS_INSUFFICIENT_RESOURCES;
  409. }
  410. }
  411. DeviceExtension->NumberOfOpenInstances++;
  412. DebugPrint((DebugLevelVerbose,
  413. "DevExt:%x, Open OpenCount=%x\n",
  414. DeviceExtension,
  415. DeviceExtension->NumberOfOpenInstances));
  416. //
  417. // Make FilterInstance the File Handle Context
  418. //
  419. IrpStack->FileObject->FsContext = FilterInstance;
  420. DebugPrint((DebugLevelVerbose,
  421. "CreateFilterInstance=%x ExtSize=%x\n",
  422. FilterInstance,
  423. DeviceExtension->MinidriverData->HwInitData.FilterInstanceExtensionSize ));
  424. //
  425. // Reference the FDO so that itwon't go away before all handles are closed.
  426. //
  427. ObReferenceObject(DeviceObject);
  428. }
  429. //
  430. // we're done so release the event
  431. //
  432. KeSetEvent(&DeviceExtension->ControlEvent, IO_NO_INCREMENT, FALSE);
  433. ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
  434. return (SCCompleteIrp(Irp, Status, DeviceExtension));
  435. }
  436. #else // ENABLE_MULTIPLE_FILTER_TYPES
  437. #endif // ENABLE_MULTIPLE_FILTER_TYPES
  438. NTSTATUS
  439. StreamDispatchCreate(
  440. IN PDEVICE_OBJECT DeviceObject,
  441. IN PIRP Irp
  442. )
  443. /*++
  444. Routine Description:
  445. This routine receives createfile IRP's for a stream.
  446. Arguments:
  447. DeviceObject - device object for the device
  448. Irp - probably an IRP, silly
  449. Return Value:
  450. The IRP status is set as appropriate
  451. --*/
  452. {
  453. NTSTATUS Status;
  454. PFILTER_INSTANCE FilterInstance;
  455. PIO_STACK_LOCATION IrpStack;
  456. PKSPIN_CONNECT Connect;
  457. PFILE_OBJECT FileObject;
  458. PSTREAM_OBJECT StreamObject;
  459. PDEVICE_EXTENSION DeviceExtension = (PDEVICE_EXTENSION)
  460. DeviceObject->DeviceExtension;
  461. PHW_STREAM_INFORMATION CurrentInfo;
  462. ULONG i;
  463. BOOLEAN RequestIssued;
  464. PADDITIONAL_PIN_INFO AdditionalInfo;
  465. DebugPrint((DebugLevelTrace,
  466. "'Creating stream with Irp %x\n", Irp));
  467. PAGED_CODE();
  468. DebugPrint((DebugLevelTrace,"entering StreamDispatchCreate()\n"));
  469. IrpStack = IoGetCurrentIrpStackLocation(Irp);
  470. DeviceExtension = DeviceObject->DeviceExtension;
  471. //
  472. // show one more I/O pending & verify that we can actually do I/O.
  473. //
  474. Status = SCShowIoPending(DeviceExtension, Irp);
  475. if ( !NT_SUCCESS ( Status )) {
  476. //
  477. // the device is currently not accessible, so just return with error
  478. //
  479. DebugPrint((DebugLevelError,"exiting StreamDispatchCreate():error1\n"));
  480. return (Status);
  481. }
  482. //
  483. // get the parent file object from the child object.
  484. //
  485. FileObject = IrpStack->FileObject->RelatedFileObject;
  486. //
  487. // get the filter instance & additional info pointers
  488. //
  489. FilterInstance =
  490. (PFILTER_INSTANCE) FileObject->FsContext;
  491. AdditionalInfo = FilterInstance->PinInstanceInfo;
  492. DebugPrint((DebugLevelVerbose,
  493. "FilterInstance=%x NumberOfPins=%x PinInfo=%x\n",
  494. FilterInstance,
  495. FilterInstance->NumberOfPins,
  496. FilterInstance->PinInformation));
  497. Status = KsValidateConnectRequest(Irp,
  498. FilterInstance->NumberOfPins,
  499. FilterInstance->PinInformation,
  500. &Connect);
  501. if ( !NT_SUCCESS( Status )) {
  502. DebugPrint((DebugLevelError,
  503. "exiting StreamDispatchCreate():error2\n"));
  504. return (SCCompleteIrp(Irp, Status, DeviceExtension));
  505. }
  506. //
  507. // take the control event to protect the instance counter
  508. //
  509. KeWaitForSingleObject(&DeviceExtension->ControlEvent,
  510. Executive,
  511. KernelMode,
  512. FALSE,// not alertable
  513. NULL);
  514. //
  515. // if the # of instances for this pin is already opened, error the
  516. // request.
  517. //
  518. DebugPrint((DebugLevelVerbose,
  519. "AdditionalInfo@%x PinId=%x CurrentInstances=%x Max=%x\n",
  520. AdditionalInfo, Connect->PinId,
  521. AdditionalInfo[Connect->PinId].CurrentInstances,
  522. AdditionalInfo[Connect->PinId].MaxInstances));
  523. if (AdditionalInfo[Connect->PinId].CurrentInstances ==
  524. AdditionalInfo[Connect->PinId].MaxInstances) {
  525. DebugPrint((DebugLevelWarning,
  526. "StreamDispatchCreate: too many opens "));
  527. KeSetEvent(&DeviceExtension->ControlEvent, IO_NO_INCREMENT, FALSE);
  528. DebugPrint((DebugLevelError,"exiting StreamDispatchCreate():error3\n"));
  529. return (SCCompleteIrp(Irp, STATUS_TOO_MANY_OPENED_FILES, DeviceExtension));
  530. }
  531. //
  532. // initialize the stream object for this instance
  533. //
  534. StreamObject = ExAllocatePool(NonPagedPool,
  535. sizeof(STREAM_OBJECT) +
  536. DeviceExtension->MinidriverData->
  537. HwInitData.PerStreamExtensionSize
  538. );
  539. if (!StreamObject) {
  540. DebugPrint((DebugLevelError,
  541. "StreamDispatchCreate: No pool for stream info"));
  542. KeSetEvent(&DeviceExtension->ControlEvent, IO_NO_INCREMENT, FALSE);
  543. DebugPrint((DebugLevelError,"exiting StreamDispatchCreate():error4\n"));
  544. return (SCCompleteIrp(Irp, STATUS_INSUFFICIENT_RESOURCES, DeviceExtension));
  545. }
  546. RtlZeroMemory(StreamObject,
  547. sizeof(STREAM_OBJECT) +
  548. DeviceExtension->MinidriverData->
  549. HwInitData.PerStreamExtensionSize
  550. );
  551. //
  552. // TODO: Remove this once KS can multiplex CLEANUP requests.
  553. //
  554. StreamObject->ComObj.Cookie = STREAM_OBJECT_COOKIE;
  555. //
  556. // default state to stopped
  557. //
  558. StreamObject->CurrentState = KSSTATE_STOP;
  559. KsAllocateObjectHeader(&StreamObject->ComObj.DeviceHeader,
  560. SIZEOF_ARRAY(StreamDriverDispatch),
  561. (PKSOBJECT_CREATE_ITEM) StreamDriverDispatch,
  562. Irp,
  563. (PKSDISPATCH_TABLE) & StreamDispatchTable);
  564. StreamObject->HwStreamObject.StreamNumber = Connect->PinId;
  565. StreamObject->FilterFileObject = FileObject;
  566. StreamObject->FileObject = IrpStack->FileObject;
  567. StreamObject->FilterInstance = FilterInstance;
  568. StreamObject->DeviceExtension = DeviceExtension;
  569. #ifdef ENABLE_STREAM_CLASS_AS_ALLOCATOR
  570. StreamObject->PinToHandle = Connect->PinToHandle;
  571. #endif
  572. KeInitializeEvent (&StreamObject -> StopEvent, SynchronizationEvent, FALSE);
  573. //
  574. // For potential "source" pins, don't start sourcing standard
  575. // medium/interface stream requests across non-standard medium/interfaces.
  576. //
  577. if (!IsEqualGUIDAligned (&Connect->Medium.Set, &KSMEDIUMSETID_Standard) ||
  578. !IsEqualGUIDAligned (&Connect->Interface.Set, &KSINTERFACESETID_Standard)) {
  579. StreamObject->StandardTransport = FALSE;
  580. } else {
  581. StreamObject -> StandardTransport = TRUE;
  582. }
  583. //
  584. // set the minidriver's parameters in the HwStreamObject struct.
  585. //
  586. StreamObject->HwStreamObject.SizeOfThisPacket = sizeof(HW_STREAM_OBJECT);
  587. StreamObject->HwStreamObject.HwDeviceExtension =
  588. DeviceExtension->HwDeviceExtension;
  589. StreamObject->HwStreamObject.HwStreamExtension =
  590. (PVOID) (StreamObject + 1);
  591. //
  592. // walk the minidriver's stream info structure to find the properties
  593. // for this stream.
  594. //
  595. if ( NULL == FilterInstance->StreamDescriptor ) {
  596. //
  597. // has not reenum, use the global one
  598. //
  599. CurrentInfo = &DeviceExtension->StreamDescriptor->StreamInfo;
  600. }
  601. else {
  602. CurrentInfo = &FilterInstance->StreamDescriptor->StreamInfo;
  603. }
  604. CurrentInfo = CurrentInfo + Connect->PinId;
  605. //
  606. // set the property info in the stream object.
  607. //
  608. StreamObject->PropertyInfo = FilterInstance->
  609. StreamPropEventArray[Connect->PinId].StreamPropertiesArray;
  610. StreamObject->PropInfoSize = CurrentInfo->
  611. NumStreamPropArrayEntries;
  612. //
  613. // set the event info in the stream object
  614. //
  615. StreamObject->EventInfo = FilterInstance->
  616. StreamPropEventArray[Connect->PinId].StreamEventsArray;
  617. StreamObject->EventInfoCount = CurrentInfo->
  618. NumStreamEventArrayEntries;
  619. // moved from callback
  620. InitializeListHead(&StreamObject->NotifyList);
  621. //
  622. // call the minidriver to open the stream. processing will continue
  623. // when the callback procedure is called.
  624. //
  625. Status = SCSubmitRequest(SRB_OPEN_STREAM,
  626. (PVOID) (Connect + 1),
  627. 0,
  628. SCOpenStreamCallback,
  629. DeviceExtension,
  630. FilterInstance->HwInstanceExtension,
  631. &StreamObject->HwStreamObject,
  632. Irp,
  633. &RequestIssued,
  634. &DeviceExtension->PendingQueue,
  635. (PVOID) DeviceExtension->
  636. MinidriverData->HwInitData.
  637. HwReceivePacket
  638. );
  639. if (!RequestIssued) {
  640. //
  641. // failure submitting the request
  642. //
  643. DEBUG_BREAKPOINT();
  644. ExFreePool(StreamObject);
  645. DebugPrint((DebugLevelWarning,
  646. "StreamClassOpen: stream open failed"));
  647. KeSetEvent(&DeviceExtension->ControlEvent, IO_NO_INCREMENT, FALSE);
  648. DebugPrint((DebugLevelError,"exiting StreamDispatchCreate():error6\n"));
  649. return (SCCompleteIrp(Irp, Status, DeviceExtension));
  650. }
  651. ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
  652. DebugPrint((DebugLevelTrace,"exiting StreamDispatchCreate()\n"));
  653. return (Status);
  654. }
  655. NTSTATUS
  656. SCOpenStreamCallback(
  657. IN PSTREAM_REQUEST_BLOCK SRB
  658. )
  659. /*++
  660. Routine Description:
  661. Process the completion of a stream open
  662. Arguments:
  663. SRB - address of the completed SRB
  664. Return Value:
  665. None.
  666. --*/
  667. {
  668. PDEVICE_EXTENSION DeviceExtension =
  669. (PDEVICE_EXTENSION) SRB->HwSRB.HwDeviceExtension - 1;
  670. PSTREAM_OBJECT StreamObject = CONTAINING_RECORD(
  671. SRB->HwSRB.StreamObject,
  672. STREAM_OBJECT,
  673. HwStreamObject
  674. );
  675. PIRP Irp = SRB->HwSRB.Irp;
  676. PIO_STACK_LOCATION IrpStack = IoGetCurrentIrpStackLocation(Irp);
  677. NTSTATUS Status = SRB->HwSRB.Status;
  678. PADDITIONAL_PIN_INFO AdditionalInfo;
  679. PVOID PropertyInfo;
  680. PKSPROPERTY_ITEM PropertyItem;
  681. PHW_STREAM_INFORMATION CurrentInfo;
  682. ULONG i;
  683. PAGED_CODE();
  684. if (NT_SUCCESS(Status)) {
  685. //
  686. // if required parameters have not been filled in, fail the open.
  687. //
  688. if (!StreamObject->HwStreamObject.ReceiveControlPacket) {
  689. DEBUG_BREAKPOINT();
  690. ExFreePool(StreamObject);
  691. SRB->HwSRB.Status = STATUS_ADAPTER_HARDWARE_ERROR;
  692. KeSetEvent(&DeviceExtension->ControlEvent, IO_NO_INCREMENT, FALSE);
  693. return (SCProcessCompletedRequest(SRB));
  694. }
  695. //
  696. // if the minidriver does not accept data, use dummy routine.
  697. //
  698. if (!StreamObject->HwStreamObject.ReceiveDataPacket) {
  699. StreamObject->HwStreamObject.ReceiveDataPacket = SCErrorDataSRB;
  700. }
  701. //
  702. // Save the pointer to our per stream structure in the FsContext
  703. // field of FileObject. Null out the 2nd context param.
  704. //
  705. IrpStack->FileObject->FsContext = StreamObject;
  706. IrpStack->FileObject->FsContext2 = NULL;
  707. //
  708. // Initialize ControlSetMasterClock to serialize the concurrent
  709. // calls of the function on us, and lock the Read/write of the
  710. // MasterLockInfo
  711. //
  712. KeInitializeEvent(&StreamObject->ControlSetMasterClock, SynchronizationEvent, TRUE);
  713. KeInitializeSpinLock(&StreamObject->LockUseMasterClock );
  714. DebugPrint((DebugLevelTrace, "'StreamClassOpen: Stream opened.\n"));
  715. //
  716. // Initialize minidriver timer and timer DPC for this stream
  717. //
  718. KeInitializeTimer(&StreamObject->ComObj.MiniDriverTimer);
  719. KeInitializeDpc(&StreamObject->ComObj.MiniDriverTimerDpc,
  720. SCMinidriverStreamTimerDpc,
  721. StreamObject);
  722. //
  723. // initialize the lists for this stream
  724. //
  725. InitializeListHead(&StreamObject->DataPendingQueue);
  726. InitializeListHead(&StreamObject->ControlPendingQueue);
  727. InitializeListHead(&StreamObject->NextStream);
  728. // a mini driver might start to call GetNextEvent once
  729. // returns from SRB_OPNE_STREAM. Do it earlier than submit.
  730. //InitializeListHead(&StreamObject->NotifyList);
  731. #ifdef ENABLE_STREAM_CLASS_AS_ALLOCATOR
  732. InitializeListHead(&StreamObject->FreeQueue);
  733. KeInitializeSpinLock(&StreamObject->FreeQueueLock );
  734. InitializeListHead(&StreamObject->Queues[READ].ActiveQueue);
  735. KeInitializeSpinLock(&StreamObject->Queues[READ].QueueLock );
  736. InitializeListHead(&StreamObject->Queues[WRITE].ActiveQueue);
  737. KeInitializeSpinLock(&StreamObject->Queues[WRITE].QueueLock );
  738. StreamObject->PinId = StreamObject->HwStreamObject.StreamNumber;
  739. StreamObject->PinType = IrpSink; // assume irp sink
  740. if (StreamObject->PinToHandle) { // if irp source
  741. StreamObject->PinType = IrpSource;
  742. Status = PinCreateHandler( Irp, StreamObject );
  743. if (!NT_SUCCESS(Status)) {
  744. DebugPrint((DebugLevelError,
  745. "\nStreamDispatchCreate: PinCreateHandler() returned ERROR"));
  746. ExFreePool(StreamObject);
  747. SRB->HwSRB.Status = Status;
  748. KeSetEvent(&DeviceExtension->ControlEvent, IO_NO_INCREMENT, FALSE);
  749. return (SCProcessCompletedRequest(SRB));
  750. }
  751. }
  752. #endif
  753. //
  754. // show we're ready for a request. Don't show this for data if the
  755. // minidriver does not want data on this stream.
  756. //
  757. CurrentInfo = &DeviceExtension->StreamDescriptor->StreamInfo;
  758. for (i = 0; i < StreamObject->HwStreamObject.StreamNumber; i++) {
  759. //
  760. // index to next streaminfo structure
  761. //
  762. CurrentInfo++;
  763. }
  764. if (CurrentInfo->DataAccessible) {
  765. StreamObject->ReadyForNextDataReq = TRUE;
  766. }
  767. StreamObject->ReadyForNextControlReq = TRUE;
  768. //
  769. // call locked routine to insert this stream in the list
  770. //
  771. SCInsertStreamInFilter(StreamObject, DeviceExtension);
  772. //
  773. // reference the filter so we won't be called to close the instance
  774. // before all streams are closed.
  775. //
  776. ObReferenceObject(IrpStack->FileObject->RelatedFileObject);
  777. //
  778. // call routine to update the persisted properties for this pin, if
  779. // any.
  780. //
  781. SCUpdatePersistedProperties(StreamObject, DeviceExtension,
  782. IrpStack->FileObject);
  783. //
  784. // show one more instance of this pin opened.
  785. //
  786. AdditionalInfo = ((PFILTER_INSTANCE) IrpStack->FileObject->
  787. RelatedFileObject->FsContext)->PinInstanceInfo;
  788. AdditionalInfo[StreamObject->HwStreamObject.StreamNumber].
  789. CurrentInstances++;
  790. //
  791. // construct on-the-fly properties for the stream, if necessary
  792. //
  793. if (StreamObject->HwStreamObject.HwClockObject.HwClockFunction) {
  794. //
  795. // create a property set describing the characteristics of the
  796. // clock.
  797. //
  798. PropertyInfo = ExAllocatePool(PagedPool,
  799. sizeof(ConstructedStreamHandlers) +
  800. sizeof(ConstructedStreamProperties));
  801. if (PropertyInfo) {
  802. PropertyItem = (PKSPROPERTY_ITEM) ((ULONG_PTR) PropertyInfo +
  803. sizeof(ConstructedStreamProperties));
  804. RtlCopyMemory(PropertyInfo,
  805. &ConstructedStreamProperties,
  806. sizeof(ConstructedStreamProperties));
  807. RtlCopyMemory(PropertyItem,
  808. &ConstructedStreamHandlers,
  809. sizeof(ConstructedStreamHandlers));
  810. //
  811. // patch the address of the handler
  812. //
  813. ((PKSPROPERTY_SET) PropertyInfo)->PropertyItem = PropertyItem;
  814. //
  815. // modify the master clock property based on the support
  816. // level.
  817. //
  818. if (0 == (StreamObject->HwStreamObject.HwClockObject.ClockSupportFlags
  819. & CLOCK_SUPPORT_CAN_RETURN_STREAM_TIME)) {
  820. DEBUG_BREAKPOINT();
  821. PropertyItem->GetPropertyHandler
  822. = NULL;
  823. } // if cannot return stream time
  824. StreamObject->ConstructedPropInfoSize =
  825. SIZEOF_ARRAY(ConstructedStreamProperties);
  826. StreamObject->ConstructedPropertyInfo =
  827. (PKSPROPERTY_SET) PropertyInfo;
  828. } // if property info
  829. } // if clock function
  830. } else {
  831. ExFreePool(StreamObject);
  832. } // if good status
  833. //
  834. // signal the event and complete the IRP.
  835. //
  836. KeSetEvent(&DeviceExtension->ControlEvent, IO_NO_INCREMENT, FALSE);
  837. SCProcessCompletedRequest(SRB);
  838. return (Status);
  839. }
  840. NTSTATUS
  841. SCSetMasterClockWhenDeviceInaccessible(
  842. IN PDEVICE_OBJECT DeviceObject,
  843. IN PIRP Irp )
  844. /*++
  845. Description:
  846. This function look for special case in pin property request when the device
  847. is inaccessible, probably by surprise removal. Yet we need to process the
  848. SetMasterClock(NULL) so that the MC ref'ed by us can be released. The MC could
  849. be on our pin or external.
  850. This function should only be called in StreamDispatchIoControl. We look for the
  851. Stream property.SetMasterClock(NULL). We returned SUCCESS if it is. Otherwise
  852. we return STATUS_UNCESSFUL to indicate that we don't process it.
  853. Arguments:
  854. DeviceObject - Device Object for the device
  855. Irp - the request packet
  856. Return:
  857. SUCCESS : If it is streamproperty.setmasterclock(NULL).
  858. UNSUCCESSFUL : otherwise.
  859. --*/
  860. {
  861. NTSTATUS Status=STATUS_UNSUCCESSFUL;
  862. PIO_STACK_LOCATION IrpStack = IoGetCurrentIrpStackLocation(Irp);
  863. ULONG InputBufferLength = IrpStack->Parameters.DeviceIoControl.InputBufferLength;
  864. ULONG OutputBufferLength = IrpStack->Parameters.DeviceIoControl.OutputBufferLength;
  865. PKSPROPERTY Property;
  866. if ( IOCTL_KS_PROPERTY == IrpStack->Parameters.DeviceIoControl.IoControlCode &&
  867. InputBufferLength >= sizeof(KSPROPERTY) &&
  868. OutputBufferLength >= sizeof( HANDLE )) {
  869. //
  870. // only ksproperty is in our interest.
  871. //
  872. try {
  873. //
  874. // Validate the pointers if the client is not trusted.
  875. //
  876. if (Irp->RequestorMode != KernelMode) {
  877. ProbeForRead(IrpStack->Parameters.DeviceIoControl.Type3InputBuffer,
  878. InputBufferLength,
  879. sizeof(BYTE));
  880. ProbeForRead(Irp->UserBuffer,
  881. OutputBufferLength,
  882. sizeof(DWORD));
  883. }
  884. } except (EXCEPTION_EXECUTE_HANDLER) {
  885. return STATUS_UNSUCCESSFUL;
  886. }
  887. //
  888. // Capture the property request
  889. //
  890. Property = (PKSPROPERTY)IrpStack->Parameters.DeviceIoControl.Type3InputBuffer;
  891. if ( KSPROPERTY_TYPE_SET == Property->Flags &&
  892. KSPROPERTY_STREAM_MASTERCLOCK == Property->Id &&
  893. IsEqualGUIDAligned(&Property->Set, &KSPROPSETID_Stream) &&
  894. NULL == *(PHANDLE) Irp->UserBuffer ) {
  895. //
  896. // All match. Now process it. In theory we should call mini driver.
  897. // But we did not before. To avoid potential regression in mini drivers
  898. // we refrain from sending set_master_clock in this condition.
  899. //
  900. PSTREAM_OBJECT StreamObject = (PSTREAM_OBJECT) IrpStack->FileObject->FsContext;
  901. DebugPrint((DebugLevelInfo, "SCSetMasterClockWhen:Devobj %x Irp %x\n",
  902. DeviceObject, Irp));
  903. if (StreamObject->MasterClockInfo) {
  904. ObDereferenceObject(StreamObject->MasterClockInfo->ClockFileObject);
  905. ExFreePool(StreamObject->MasterClockInfo);
  906. StreamObject->MasterClockInfo = NULL;
  907. }
  908. return STATUS_SUCCESS;
  909. }
  910. }
  911. return Status;
  912. }
  913. NTSTATUS
  914. StreamDispatchIoControl
  915. (
  916. IN PDEVICE_OBJECT DeviceObject,
  917. IN PIRP Irp
  918. )
  919. /*++
  920. Routine Description:
  921. Process an ioctl to the stream.
  922. Arguments:
  923. DeviceObject - device object for the device
  924. Irp - probably an IRP, silly
  925. Return Value:
  926. NTSTATUS returned as appropriate.
  927. --*/
  928. {
  929. NTSTATUS Status;
  930. PIO_STACK_LOCATION IrpStack = IoGetCurrentIrpStackLocation(Irp);
  931. PDEVICE_EXTENSION DeviceExtension;
  932. PSTREAM_OBJECT StreamObject = (PSTREAM_OBJECT)
  933. IrpStack->FileObject->FsContext;
  934. PAGED_CODE();
  935. ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
  936. DeviceExtension = (PDEVICE_EXTENSION) DeviceObject->DeviceExtension;
  937. //
  938. // show one more I/O pending & verify that we can actually do I/O.
  939. //
  940. Status = STATUS_INVALID_DEVICE_REQUEST;
  941. ///Status = SCShowIoPending(DeviceExtension, Irp);
  942. if (DeviceExtension->Flags & DEVICE_FLAGS_DEVICE_INACCESSIBLE) {
  943. ///
  944. // Note. When our device is surprised removed && we have ref on the master clock
  945. // && we receive the stream property to set the master clock to null,
  946. // we need to process it to deref the MC so the MC can be released.
  947. // We will special case it here otherwise there will be big code churn. And
  948. // the perf impact of this special case should be minimum for we get
  949. // in here quite rarely.
  950. //
  951. // (the device is currently not accessible, so just return with error)
  952. //
  953. NTSTATUS StatusProcessed;
  954. StatusProcessed = SCSetMasterClockWhenDeviceInaccessible( DeviceObject, Irp );
  955. if ( NT_SUCCESS( StatusProcessed ) ) {
  956. Status = StatusProcessed;
  957. }
  958. Irp->IoStatus.Status = Status;
  959. IoCompleteRequest(Irp, IO_NO_INCREMENT);
  960. return (Status);
  961. }
  962. //
  963. // show one more IO pending.
  964. //
  965. InterlockedIncrement(&DeviceExtension->OneBasedIoCount);
  966. switch (IrpStack->Parameters.DeviceIoControl.IoControlCode) {
  967. case IOCTL_KS_READ_STREAM:
  968. //
  969. // process read data request
  970. //
  971. DebugPrint((DebugLevelTrace, "'SCReadStream:Irp %x\n", Irp));
  972. Status = SCProcessDataTransfer(DeviceExtension,
  973. Irp,
  974. SRB_READ_DATA);
  975. break;
  976. case IOCTL_KS_WRITE_STREAM:
  977. //
  978. // process write data request
  979. //
  980. DebugPrint((DebugLevelTrace, "'SCWriteStream:Irp %x\n", Irp));
  981. Status = SCProcessDataTransfer(DeviceExtension,
  982. Irp,
  983. SRB_WRITE_DATA);
  984. break;
  985. case IOCTL_KS_RESET_STATE:
  986. {
  987. BOOLEAN RequestIssued;
  988. KSRESET *Reset,
  989. ResetType;
  990. Reset = (KSRESET *) IrpStack->Parameters.DeviceIoControl.Type3InputBuffer;
  991. if (Irp->RequestorMode != KernelMode) {
  992. try {
  993. ProbeForRead(Reset, sizeof(KSRESET), sizeof(ULONG));
  994. ResetType = *Reset;
  995. } except(EXCEPTION_EXECUTE_HANDLER) {
  996. TRAP;
  997. Status = GetExceptionCode();
  998. break;
  999. } // except
  1000. } // if !kernelmode
  1001. else {
  1002. //
  1003. // trusted kernel mode, just use it. #131858 prefixbug 17400
  1004. //
  1005. ResetType = *Reset;
  1006. }
  1007. ASSERT(ResetType == *Reset);
  1008. if (ResetType == KSRESET_BEGIN) {
  1009. StreamObject->InFlush = TRUE;
  1010. Status = SCSubmitRequest(SRB_BEGIN_FLUSH,
  1011. NULL,
  1012. 0,
  1013. SCDequeueAndDeleteSrb,
  1014. DeviceExtension,
  1015. ((PFILTER_INSTANCE)
  1016. (StreamObject->FilterInstance))
  1017. ->HwInstanceExtension,
  1018. &StreamObject->HwStreamObject,
  1019. Irp,
  1020. &RequestIssued,
  1021. &StreamObject->ControlPendingQueue,
  1022. StreamObject->HwStreamObject.
  1023. ReceiveControlPacket
  1024. );
  1025. StreamFlushIo(DeviceExtension, StreamObject);
  1026. } else {
  1027. Status = SCSubmitRequest(SRB_END_FLUSH,
  1028. NULL,
  1029. 0,
  1030. SCDequeueAndDeleteSrb,
  1031. DeviceExtension,
  1032. ((PFILTER_INSTANCE)
  1033. (StreamObject->FilterInstance))
  1034. ->HwInstanceExtension,
  1035. &StreamObject->HwStreamObject,
  1036. Irp,
  1037. &RequestIssued,
  1038. &StreamObject->ControlPendingQueue,
  1039. StreamObject->HwStreamObject.
  1040. ReceiveControlPacket
  1041. );
  1042. StreamObject->InFlush = FALSE;
  1043. } // if begin
  1044. break;
  1045. } // case reset
  1046. case IOCTL_KS_PROPERTY:
  1047. DebugPrint((DebugLevelTrace,
  1048. "'StreamDispatchIO: Property with Irp %x\n", Irp));
  1049. //
  1050. // assume that there are no minidriver properties.
  1051. //
  1052. Status = STATUS_PROPSET_NOT_FOUND;
  1053. //
  1054. // first try the minidriver's properties, giving it a chance to
  1055. // override our built in sets.
  1056. //
  1057. if (StreamObject->PropInfoSize) {
  1058. ASSERT( StreamObject->PropertyInfo );
  1059. Status = KsPropertyHandler(Irp,
  1060. StreamObject->PropInfoSize,
  1061. StreamObject->PropertyInfo);
  1062. } // if minidriver props
  1063. //
  1064. // if the minidriver did not support it, try our on the fly set.
  1065. //
  1066. if ((Status == STATUS_PROPSET_NOT_FOUND) ||
  1067. (Status == STATUS_NOT_FOUND)) {
  1068. if (StreamObject->ConstructedPropertyInfo) {
  1069. Status = KsPropertyHandler(Irp,
  1070. StreamObject->ConstructedPropInfoSize,
  1071. StreamObject->ConstructedPropertyInfo);
  1072. } // if constructed exists
  1073. } // if not found
  1074. //
  1075. // if neither supported it, try our built-in set.
  1076. //
  1077. if ((Status == STATUS_PROPSET_NOT_FOUND) ||
  1078. (Status == STATUS_NOT_FOUND)) {
  1079. Status =
  1080. KsPropertyHandler(Irp,
  1081. SIZEOF_ARRAY(StreamProperties),
  1082. (PKSPROPERTY_SET) StreamProperties);
  1083. } // if property not found
  1084. break;
  1085. case IOCTL_KS_ENABLE_EVENT:
  1086. DebugPrint((DebugLevelTrace,
  1087. "'StreamDispatchIO: Enable event with Irp %x\n", Irp));
  1088. Status = KsEnableEvent(Irp,
  1089. StreamObject->EventInfoCount,
  1090. StreamObject->EventInfo,
  1091. NULL, 0, NULL);
  1092. break;
  1093. case IOCTL_KS_DISABLE_EVENT:
  1094. {
  1095. KSEVENTS_LOCKTYPE LockType;
  1096. PVOID LockObject;
  1097. DebugPrint((DebugLevelTrace,
  1098. "'StreamDispatchIO: Disable event with Irp %x\n", Irp));
  1099. //
  1100. // determine the type of lock necessary based on whether we are
  1101. // using interrupt or spinlock synchronization.
  1102. //
  1103. #if DBG
  1104. if (DeviceExtension->SynchronizeExecution == SCDebugKeSynchronizeExecution) {
  1105. #else
  1106. if (DeviceExtension->SynchronizeExecution == KeSynchronizeExecution) {
  1107. #endif
  1108. LockType = KSEVENTS_INTERRUPT;
  1109. LockObject = DeviceExtension->InterruptObject;
  1110. } else {
  1111. LockType = KSEVENTS_SPINLOCK;
  1112. LockObject = &DeviceExtension->SpinLock;
  1113. }
  1114. Status = KsDisableEvent(Irp,
  1115. &StreamObject->NotifyList,
  1116. LockType,
  1117. LockObject);
  1118. }
  1119. break;
  1120. case IOCTL_KS_METHOD:
  1121. #ifdef ENABLE_KS_METHODS
  1122. DebugPrint((DebugLevelTrace,
  1123. "'StreamDispatchIO: Method in Irp %x\n", Irp));
  1124. //
  1125. // assume that there are no minidriver properties.
  1126. //
  1127. Status = STATUS_PROPSET_NOT_FOUND;
  1128. if ((Status == STATUS_PROPSET_NOT_FOUND) ||
  1129. (Status == STATUS_NOT_FOUND)) {
  1130. if (StreamObject->MethodInfo) {
  1131. Status = KsMethodHandler(Irp,
  1132. StreamObject->MethodInfoSize,
  1133. StreamObject->MethodInfo);
  1134. } // if constructed exists
  1135. } // if not found
  1136. break;
  1137. #else
  1138. Status = STATUS_PROPSET_NOT_FOUND;
  1139. break;
  1140. #endif
  1141. }
  1142. if (Status != STATUS_PENDING) {
  1143. SCCompleteIrp(Irp, Status, DeviceExtension);
  1144. }
  1145. ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
  1146. return Status;
  1147. }
  1148. NTSTATUS
  1149. SCStreamDeviceState
  1150. (
  1151. IN PIRP Irp,
  1152. IN PKSPROPERTY Property,
  1153. IN OUT PKSSTATE DeviceState
  1154. )
  1155. /*++
  1156. Routine Description:
  1157. Process get/set device state to the stream.
  1158. Arguments:
  1159. Irp - pointer to the irp
  1160. Property - pointer to the information for device state property
  1161. DeviceState - state to which the device is to be set
  1162. Return Value:
  1163. NTSTATUS returned as appropriate.
  1164. --*/
  1165. {
  1166. NTSTATUS Status;
  1167. PIO_STACK_LOCATION IrpStack;
  1168. PDEVICE_EXTENSION DeviceExtension;
  1169. PSTREAM_OBJECT StreamObject;
  1170. BOOLEAN RequestIssued;
  1171. #ifdef ENABLE_STREAM_CLASS_AS_ALLOCATOR
  1172. PFILTER_INSTANCE FilterInstance;
  1173. PADDITIONAL_PIN_INFO AdditionalInfo;
  1174. PAGED_CODE();
  1175. DebugPrint((DebugLevelTrace, "'SCStreamDeviceState:Irp %x, State = %x\n",
  1176. Irp, *DeviceState));
  1177. IrpStack = IoGetCurrentIrpStackLocation(Irp);
  1178. DeviceExtension = (PDEVICE_EXTENSION)
  1179. (IrpStack->DeviceObject)->DeviceExtension;
  1180. StreamObject = (PSTREAM_OBJECT) IrpStack->FileObject->FsContext;
  1181. FilterInstance = ((PFILTER_INSTANCE) (StreamObject->FilterInstance));
  1182. AdditionalInfo = FilterInstance->PinInstanceInfo;
  1183. Status = STATUS_SUCCESS;
  1184. //
  1185. // Synchronize pin state changes
  1186. //
  1187. KeWaitForSingleObject(&DeviceExtension->ControlEvent,
  1188. Executive,
  1189. KernelMode,
  1190. FALSE,// not alertable
  1191. NULL);
  1192. if (StreamObject->CurrentState == *DeviceState) {
  1193. KeSetEvent(&DeviceExtension->ControlEvent, IO_NO_INCREMENT, FALSE);
  1194. return STATUS_SUCCESS;
  1195. }
  1196. switch (*DeviceState) {
  1197. case KSSTATE_RUN:
  1198. DebugPrint((DebugLevelTrace, "STREAM: KSSTATE_RUN on stream:%x\n",StreamObject));
  1199. break;
  1200. case KSSTATE_ACQUIRE:
  1201. DebugPrint((DebugLevelTrace, "STREAM: KSSTATE_ACQUIRE on stream:%x\n",StreamObject));
  1202. break;
  1203. case KSSTATE_PAUSE:
  1204. DebugPrint((DebugLevelTrace, "STREAM: KSSTATE_PAUSE on stream:%x\n",StreamObject));
  1205. break;
  1206. case KSSTATE_STOP:
  1207. DebugPrint((DebugLevelTrace, "STREAM: KSSTATE_STOP on stream:%x\n",StreamObject));
  1208. break;
  1209. default:
  1210. DebugPrint((DebugLevelTrace, "STREAM: Invalid Device State\n"));
  1211. break;
  1212. }
  1213. DebugPrint((DebugLevelTrace, "STREAM: Stream->AllocatorFileObject:%x\n",StreamObject->AllocatorFileObject));
  1214. DebugPrint((DebugLevelTrace, "STREAM: Stream->NextFileObject:%x\n",StreamObject->NextFileObject));
  1215. DebugPrint((DebugLevelTrace, "STREAM: Stream->FileObject:%x\n",StreamObject->FileObject));
  1216. DebugPrint((DebugLevelTrace, "STREAM: Stream->PinType:"));
  1217. if (StreamObject->PinType == IrpSource)
  1218. DebugPrint((DebugLevelTrace, "IrpSource\n"));
  1219. else if (StreamObject->PinType == IrpSink)
  1220. DebugPrint((DebugLevelTrace, "IrpSink\n"));
  1221. else {
  1222. DebugPrint((DebugLevelTrace, "neither\n")); // this is a bug.
  1223. }
  1224. //
  1225. // send a set state SRB to the stream.
  1226. //
  1227. //
  1228. // GUBGUB: "we may need to send this if Status == STATUS_SUCCESS only"
  1229. // is a bugus concern since Status is inited to Success.
  1230. //
  1231. Status = SCSubmitRequest(SRB_SET_STREAM_STATE,
  1232. (PVOID) * DeviceState,
  1233. 0,
  1234. SCDequeueAndDeleteSrb,
  1235. DeviceExtension,
  1236. ((PFILTER_INSTANCE)
  1237. (StreamObject->FilterInstance))
  1238. ->HwInstanceExtension,
  1239. &StreamObject->HwStreamObject,
  1240. Irp,
  1241. &RequestIssued,
  1242. &StreamObject->ControlPendingQueue,
  1243. StreamObject->HwStreamObject.
  1244. ReceiveControlPacket
  1245. );
  1246. //
  1247. // if good status, set the new state in the stream object.
  1248. //
  1249. if (NT_SUCCESS(Status)) {
  1250. StreamObject->CurrentState = *DeviceState;
  1251. }
  1252. else {
  1253. DebugPrint((DebugLevelTrace, "STREAM: error sending DeviceState Irp\n"));
  1254. }
  1255. ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
  1256. switch (*DeviceState) {
  1257. //
  1258. // 1. should start sourcing irps at pause
  1259. // 2. worker thread shutdown if pins are connected in certain order.......
  1260. // 3. check MSTEE bugs assigned to dalesat.
  1261. //
  1262. case KSSTATE_RUN:
  1263. if(StreamObject->PinType == IrpSource &&
  1264. StreamObject->StandardTransport)
  1265. {
  1266. Status = BeginTransfer(
  1267. FilterInstance,
  1268. StreamObject);
  1269. }
  1270. break;
  1271. case KSSTATE_ACQUIRE:
  1272. Status = STATUS_SUCCESS;
  1273. break;
  1274. case KSSTATE_PAUSE:
  1275. if (NT_SUCCESS (Status)) {
  1276. if(StreamObject->PinType == IrpSource &&
  1277. StreamObject->StandardTransport)
  1278. {
  1279. Status = PrepareTransfer(
  1280. FilterInstance,
  1281. StreamObject);
  1282. }
  1283. }
  1284. break;
  1285. case KSSTATE_STOP:
  1286. if(StreamObject->PinType == IrpSource &&
  1287. StreamObject->StandardTransport)
  1288. Status = EndTransfer( FilterInstance, StreamObject );
  1289. else
  1290. //
  1291. // cancel any pending I/O on this stream if the state is STOP.
  1292. //
  1293. StreamFlushIo(DeviceExtension, StreamObject);
  1294. break;
  1295. default:
  1296. break;
  1297. }
  1298. KeSetEvent(&DeviceExtension->ControlEvent, IO_NO_INCREMENT, FALSE);
  1299. return (Status);
  1300. }
  1301. #else
  1302. PAGED_CODE();
  1303. DebugPrint((DebugLevelTrace, "'SCStreamDeviceState:Irp %x, State = %x\n",
  1304. Irp, *DeviceState));
  1305. IrpStack = IoGetCurrentIrpStackLocation(Irp);
  1306. DeviceExtension = (PDEVICE_EXTENSION)
  1307. (IrpStack->DeviceObject)->DeviceExtension;
  1308. StreamObject = (PSTREAM_OBJECT) IrpStack->FileObject->FsContext;
  1309. //
  1310. // cancel any pending I/O on this stream if the state is STOP.
  1311. //
  1312. if (*DeviceState == KSSTATE_STOP) {
  1313. StreamFlushIo(DeviceExtension, StreamObject);
  1314. }
  1315. //
  1316. // send a set state SRB to the stream.
  1317. //
  1318. DebugPrint((DebugLevelTrace,
  1319. "'SetStreamState: State %x with Irp %x\n", *DeviceState, Irp));
  1320. Status = SCSubmitRequest(SRB_SET_STREAM_STATE,
  1321. (PVOID) * DeviceState,
  1322. 0,
  1323. SCDequeueAndDeleteSrb,
  1324. DeviceExtension,
  1325. ((PFILTER_INSTANCE)
  1326. (StreamObject->FilterInstance))
  1327. ->HwInstanceExtension,
  1328. &StreamObject->HwStreamObject,
  1329. Irp,
  1330. &RequestIssued,
  1331. &StreamObject->ControlPendingQueue,
  1332. StreamObject->HwStreamObject.
  1333. ReceiveControlPacket
  1334. );
  1335. //
  1336. // if good status, set the new state in the stream object.
  1337. //
  1338. if (NT_SUCCESS(Status)) {
  1339. StreamObject->CurrentState = *DeviceState;
  1340. }
  1341. ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
  1342. return (Status);
  1343. }
  1344. #endif
  1345. NTSTATUS
  1346. SCGetStreamDeviceStateCallback
  1347. (
  1348. IN PSTREAM_REQUEST_BLOCK SRB
  1349. )
  1350. {
  1351. // yep, its a do nothing routine.
  1352. return (SRB->HwSRB.Status);
  1353. }
  1354. NTSTATUS
  1355. SCGetStreamDeviceState
  1356. (
  1357. IN PIRP Irp,
  1358. IN PKSPROPERTY Property,
  1359. IN OUT PKSSTATE DeviceState
  1360. )
  1361. /*++
  1362. Routine Description:
  1363. Process get device state to the stream.
  1364. Arguments:
  1365. Irp - pointer to the irp
  1366. Property - pointer to the information for device state property
  1367. DeviceState - state to which the device is to be set
  1368. Return Value:
  1369. NTSTATUS returned as appropriate.
  1370. --*/
  1371. {
  1372. NTSTATUS Status;
  1373. PIO_STACK_LOCATION IrpStack;
  1374. PDEVICE_EXTENSION DeviceExtension;
  1375. PSTREAM_OBJECT StreamObject;
  1376. BOOLEAN RequestIssued;
  1377. PSTREAM_REQUEST_BLOCK SRB;
  1378. PAGED_CODE();
  1379. IrpStack = IoGetCurrentIrpStackLocation(Irp);
  1380. DeviceExtension = (PDEVICE_EXTENSION)
  1381. (IrpStack->DeviceObject)->DeviceExtension;
  1382. StreamObject = (PSTREAM_OBJECT) IrpStack->FileObject->FsContext;
  1383. //
  1384. // send a get state SRB to the stream.
  1385. //
  1386. #ifdef ENABLE_STREAM_CLASS_AS_ALLOCATOR
  1387. DebugPrint((DebugLevelTrace,
  1388. "'GetStreamState: State with StreamObj:%x\n", StreamObject));
  1389. if (StreamObject->PinType == IrpSource)
  1390. DebugPrint((DebugLevelTrace, "'GetStreamState: Is IrpSource\n"));
  1391. else
  1392. DebugPrint((DebugLevelTrace,"'GetStreamState: Is IrpSink\n"));
  1393. #endif
  1394. //
  1395. // set the returned data size to the correct size regardless of status.
  1396. //
  1397. Irp->IoStatus.Information = sizeof(KSSTATE);
  1398. Status = SCSubmitRequest(SRB_GET_STREAM_STATE,
  1399. (PVOID) DeviceState,
  1400. 0,
  1401. SCGetStreamDeviceStateCallback,
  1402. DeviceExtension,
  1403. ((PFILTER_INSTANCE)
  1404. (StreamObject->FilterInstance))
  1405. ->HwInstanceExtension,
  1406. &StreamObject->HwStreamObject,
  1407. Irp,
  1408. &RequestIssued,
  1409. &StreamObject->ControlPendingQueue,
  1410. StreamObject->HwStreamObject.
  1411. ReceiveControlPacket
  1412. );
  1413. SRB = (PSTREAM_REQUEST_BLOCK) Irp->Tail.Overlay.DriverContext[0];
  1414. *DeviceState = SRB->HwSRB.CommandData.StreamState;
  1415. SCDequeueAndDeleteSrb(SRB);
  1416. //
  1417. // if not supported, return the last known state of the stream.
  1418. //
  1419. if ((Status == STATUS_NOT_SUPPORTED)
  1420. || (Status == STATUS_NOT_IMPLEMENTED)) {
  1421. Status = STATUS_SUCCESS;
  1422. *DeviceState = StreamObject->CurrentState;
  1423. }
  1424. DebugPrint((DebugLevelTrace,
  1425. "'GetStreamState: Returning:%x: DeviceState:", Status));
  1426. switch (*DeviceState) {
  1427. case KSSTATE_RUN:
  1428. DebugPrint((DebugLevelTrace, "KSSTATE_RUN\n"));
  1429. break;
  1430. case KSSTATE_ACQUIRE:
  1431. DebugPrint((DebugLevelTrace, "KSSTATE_AQUIRE\n"));
  1432. break;
  1433. case KSSTATE_PAUSE:
  1434. DebugPrint((DebugLevelTrace, "KSSTATE_PAUSE\n"));
  1435. break;
  1436. case KSSTATE_STOP:
  1437. DebugPrint((DebugLevelTrace, "KSSTATE_STOP\n"));
  1438. break;
  1439. default:
  1440. DebugPrint((DebugLevelTrace, "Invalid Device State\n"));
  1441. break;
  1442. }
  1443. return (Status);
  1444. }
  1445. NTSTATUS
  1446. SCStreamDeviceRate
  1447. (
  1448. IN PIRP Irp,
  1449. IN PKSPROPERTY Property,
  1450. IN OUT PKSRATE DeviceRate
  1451. )
  1452. /*++
  1453. Routine Description:
  1454. Process set device rate to the stream.
  1455. Arguments:
  1456. Irp - pointer to the irp
  1457. Property - pointer to the information for device state property
  1458. DeviceRate - rate at which the device is to be set
  1459. Return Value:
  1460. NTSTATUS returned as appropriate.
  1461. --*/
  1462. {
  1463. NTSTATUS Status;
  1464. PIO_STACK_LOCATION IrpStack;
  1465. PDEVICE_EXTENSION DeviceExtension;
  1466. PSTREAM_OBJECT StreamObject;
  1467. BOOLEAN RequestIssued;
  1468. PAGED_CODE();
  1469. DebugPrint((DebugLevelTrace, "'SCStreamDeviceRate:Irp %x, Rate = %x\n",
  1470. Irp, *DeviceRate));
  1471. IrpStack = IoGetCurrentIrpStackLocation(Irp);
  1472. DeviceExtension = (PDEVICE_EXTENSION)
  1473. (IrpStack->DeviceObject)->DeviceExtension;
  1474. StreamObject = (PSTREAM_OBJECT) IrpStack->FileObject->FsContext;
  1475. //
  1476. // send a set rate SRB to the stream.
  1477. //
  1478. Status = SCSubmitRequest(SRB_SET_STREAM_RATE,
  1479. (PVOID) DeviceRate,
  1480. 0,
  1481. SCDequeueAndDeleteSrb,
  1482. DeviceExtension,
  1483. ((PFILTER_INSTANCE)
  1484. (StreamObject->FilterInstance))
  1485. ->HwInstanceExtension,
  1486. &StreamObject->HwStreamObject,
  1487. Irp,
  1488. &RequestIssued,
  1489. &StreamObject->ControlPendingQueue,
  1490. StreamObject->HwStreamObject.
  1491. ReceiveControlPacket
  1492. );
  1493. //
  1494. // change STATUS_NOT_IMPLEMENTED to STATUS_NOT_FOUND so that the proxy
  1495. // does not get confused (GUBGUB). A necessary mapping between r0 and r3
  1496. // worlds.
  1497. //
  1498. if (Status == STATUS_NOT_IMPLEMENTED) {
  1499. Status = STATUS_NOT_FOUND;
  1500. }
  1501. ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
  1502. return (Status);
  1503. }
  1504. NTSTATUS
  1505. SCStreamDeviceRateCapability
  1506. (
  1507. IN PIRP Irp,
  1508. IN PKSRATE_CAPABILITY RateCap,
  1509. IN OUT PKSRATE DeviceRate
  1510. )
  1511. /*++
  1512. Routine Description:
  1513. Process set device rate to the stream.
  1514. Arguments:
  1515. Irp - pointer to the irp
  1516. RateCap - pointer to the information for device state property
  1517. DeviceRate - rate to which the device was set
  1518. Return Value:
  1519. NTSTATUS returned as appropriate.
  1520. --*/
  1521. {
  1522. NTSTATUS Status;
  1523. PIO_STACK_LOCATION IrpStack;
  1524. PDEVICE_EXTENSION DeviceExtension;
  1525. PSTREAM_OBJECT StreamObject;
  1526. BOOLEAN RequestIssued;
  1527. PAGED_CODE();
  1528. DebugPrint((DebugLevelTrace, "'SCStreamDeviceRate:Irp %x, Rate = %x\n",
  1529. Irp, *DeviceRate));
  1530. IrpStack = IoGetCurrentIrpStackLocation(Irp);
  1531. DeviceExtension = (PDEVICE_EXTENSION)
  1532. (IrpStack->DeviceObject)->DeviceExtension;
  1533. StreamObject = (PSTREAM_OBJECT) IrpStack->FileObject->FsContext;
  1534. //
  1535. // presuppose a successful completion, which means that the minidriver
  1536. // can normalize rate to 1.
  1537. //
  1538. *DeviceRate = RateCap->Rate;
  1539. DeviceRate->Rate = 1000;
  1540. Irp->IoStatus.Information = sizeof(KSRATE);
  1541. //
  1542. // send a set rate SRB to the stream.
  1543. //
  1544. Status = SCSubmitRequest(
  1545. SRB_PROPOSE_STREAM_RATE,
  1546. (PVOID) RateCap,
  1547. 0,
  1548. SCDequeueAndDeleteSrb,
  1549. DeviceExtension,
  1550. ((PFILTER_INSTANCE)(StreamObject->FilterInstance))->HwInstanceExtension,
  1551. &StreamObject->HwStreamObject,
  1552. Irp,
  1553. &RequestIssued,
  1554. &StreamObject->ControlPendingQueue,
  1555. StreamObject->HwStreamObject.ReceiveControlPacket
  1556. );
  1557. ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
  1558. //
  1559. // change STATUS_NOT_IMPLEMENTED to STATUS_NOT_FOUND so that the proxy
  1560. // does not get confused (GUBGUB). A necessary mapping between r0 and r3
  1561. // worlds.
  1562. //
  1563. if (Status == STATUS_NOT_IMPLEMENTED) {
  1564. Status = STATUS_NOT_FOUND;
  1565. }
  1566. return (Status);
  1567. }
  1568. NTSTATUS
  1569. SCStreamProposeNewFormat
  1570. (
  1571. IN PIRP Irp,
  1572. IN PKSPROPERTY Property,
  1573. IN OUT PKSDATAFORMAT Format
  1574. )
  1575. /*++
  1576. Routine Description:
  1577. Process propose data format to the stream.
  1578. Arguments:
  1579. Irp - pointer to the irp
  1580. Property - pointer to the information for propose format property
  1581. DeviceState - state to which the device is to be set
  1582. Return Value:
  1583. NTSTATUS returned as appropriate.
  1584. --*/
  1585. {
  1586. NTSTATUS Status;
  1587. PIO_STACK_LOCATION IrpStack;
  1588. PDEVICE_EXTENSION DeviceExtension;
  1589. PSTREAM_OBJECT StreamObject;
  1590. BOOLEAN RequestIssued;
  1591. PAGED_CODE();
  1592. DebugPrint((DebugLevelTrace, "'SCStreamProposeNewFormat:Irp %x, Format = %x\n",
  1593. Irp, *Format));
  1594. IrpStack = IoGetCurrentIrpStackLocation(Irp);
  1595. DeviceExtension = (PDEVICE_EXTENSION)
  1596. (IrpStack->DeviceObject)->DeviceExtension;
  1597. StreamObject = (PSTREAM_OBJECT) IrpStack->FileObject->FsContext;
  1598. //
  1599. // send a propose format SRB to the stream.
  1600. //
  1601. Status = SCSubmitRequest(SRB_PROPOSE_DATA_FORMAT,
  1602. (PVOID) Format,
  1603. IrpStack->Parameters.DeviceIoControl.OutputBufferLength,
  1604. SCDequeueAndDeleteSrb,
  1605. DeviceExtension,
  1606. ((PFILTER_INSTANCE)
  1607. (StreamObject->FilterInstance))
  1608. ->HwInstanceExtension,
  1609. &StreamObject->HwStreamObject,
  1610. Irp,
  1611. &RequestIssued,
  1612. &StreamObject->ControlPendingQueue,
  1613. StreamObject->HwStreamObject.
  1614. ReceiveControlPacket
  1615. );
  1616. ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
  1617. //
  1618. // change STATUS_NOT_IMPLEMENTED to STATUS_NOT_FOUND so that the proxy
  1619. // does not get confused (GUBGUB). A necessary mapping between r0 and r3
  1620. // worlds.
  1621. //
  1622. if (Status == STATUS_NOT_IMPLEMENTED) {
  1623. Status = STATUS_NOT_FOUND;
  1624. }
  1625. return (Status);
  1626. }
  1627. NTSTATUS
  1628. SCStreamSetFormat
  1629. (
  1630. IN PIRP Irp,
  1631. IN PKSPROPERTY Property,
  1632. IN OUT PKSDATAFORMAT Format
  1633. )
  1634. /*++
  1635. Routine Description:
  1636. Sets the data format on the stream.
  1637. Arguments:
  1638. Irp - pointer to the irp
  1639. Property - pointer to the information for the set format property
  1640. DeviceState - state to which the device is to be set
  1641. Return Value:
  1642. NTSTATUS returned as appropriate.
  1643. --*/
  1644. {
  1645. NTSTATUS Status;
  1646. PIO_STACK_LOCATION IrpStack;
  1647. PDEVICE_EXTENSION DeviceExtension;
  1648. PSTREAM_OBJECT StreamObject;
  1649. BOOLEAN RequestIssued;
  1650. PAGED_CODE();
  1651. DebugPrint((DebugLevelTrace, "'SCStreamSetFormat:Irp %x, Format = %x\n",
  1652. Irp, *Format));
  1653. IrpStack = IoGetCurrentIrpStackLocation(Irp);
  1654. DeviceExtension = (PDEVICE_EXTENSION)
  1655. (IrpStack->DeviceObject)->DeviceExtension;
  1656. StreamObject = (PSTREAM_OBJECT) IrpStack->FileObject->FsContext;
  1657. //
  1658. // send a set format SRB to the stream.
  1659. //
  1660. Status = SCSubmitRequest(SRB_SET_DATA_FORMAT,
  1661. (PVOID) Format,
  1662. IrpStack->Parameters.DeviceIoControl.OutputBufferLength,
  1663. SCDequeueAndDeleteSrb,
  1664. DeviceExtension,
  1665. ((PFILTER_INSTANCE)
  1666. (StreamObject->FilterInstance))
  1667. ->HwInstanceExtension,
  1668. &StreamObject->HwStreamObject,
  1669. Irp,
  1670. &RequestIssued,
  1671. &StreamObject->ControlPendingQueue,
  1672. StreamObject->HwStreamObject.
  1673. ReceiveControlPacket
  1674. );
  1675. ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
  1676. //
  1677. // change STATUS_NOT_IMPLEMENTED to STATUS_NOT_FOUND so that the proxy
  1678. // does not get confused (GUBGUB). A necessary mapping between r0 and r3
  1679. // worlds.
  1680. //
  1681. if (Status == STATUS_NOT_IMPLEMENTED) {
  1682. Status = STATUS_NOT_FOUND;
  1683. }
  1684. return (Status);
  1685. }
  1686. NTSTATUS
  1687. StreamClassMinidriverDeviceGetProperty
  1688. (
  1689. IN PIRP Irp,
  1690. IN PKSPROPERTY Property,
  1691. IN OUT PVOID PropertyInfo
  1692. )
  1693. /*++
  1694. Routine Description:
  1695. Process get property to the device.
  1696. Arguments:
  1697. Irp - pointer to the irp
  1698. Property - pointer to the information for the property
  1699. PropertyInfo - buffer to return the property data to
  1700. Return Value:
  1701. NTSTATUS returned as appropriate.
  1702. --*/
  1703. {
  1704. NTSTATUS Status;
  1705. PAGED_CODE();
  1706. Status = SCMinidriverDevicePropertyHandler(SRB_GET_DEVICE_PROPERTY,
  1707. Irp,
  1708. Property,
  1709. PropertyInfo
  1710. );
  1711. ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
  1712. return (Status);
  1713. }
  1714. NTSTATUS
  1715. StreamClassMinidriverDeviceSetProperty
  1716. (
  1717. IN PIRP Irp,
  1718. IN PKSPROPERTY Property,
  1719. IN OUT PVOID PropertyInfo
  1720. )
  1721. /*++
  1722. Routine Description:
  1723. Process set property to the device.
  1724. Arguments:
  1725. Irp - pointer to the irp
  1726. Property - pointer to the information for the property
  1727. PropertyInfo - buffer that contains the property info
  1728. Return Value:
  1729. NTSTATUS returned as appropriate.
  1730. --*/
  1731. {
  1732. NTSTATUS Status;
  1733. PAGED_CODE();
  1734. Status = SCMinidriverDevicePropertyHandler(SRB_SET_DEVICE_PROPERTY,
  1735. Irp,
  1736. Property,
  1737. PropertyInfo);
  1738. ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
  1739. return (Status);
  1740. }
  1741. NTSTATUS
  1742. StreamClassMinidriverStreamGetProperty
  1743. (
  1744. IN PIRP Irp,
  1745. IN PKSPROPERTY Property,
  1746. IN OUT PVOID PropertyInfo
  1747. )
  1748. /*++
  1749. Routine Description:
  1750. Process get property of a stream.
  1751. Arguments:
  1752. Irp - pointer to the irp
  1753. Property - pointer to the information for the property
  1754. PropertyInfo - buffer to return the property data to
  1755. Return Value:
  1756. NTSTATUS returned as appropriate.
  1757. --*/
  1758. {
  1759. NTSTATUS Status;
  1760. PAGED_CODE();
  1761. Status = SCMinidriverStreamPropertyHandler(SRB_GET_STREAM_PROPERTY,
  1762. Irp,
  1763. Property,
  1764. PropertyInfo
  1765. );
  1766. ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
  1767. return (Status);
  1768. }
  1769. NTSTATUS
  1770. StreamClassMinidriverStreamSetProperty
  1771. (
  1772. IN PIRP Irp,
  1773. IN PKSPROPERTY Property,
  1774. IN OUT PVOID PropertyInfo
  1775. )
  1776. /*++
  1777. Routine Description:
  1778. Process set property to a stream.
  1779. Arguments:
  1780. Irp - pointer to the irp
  1781. Property - pointer to the information for the property
  1782. PropertyInfo - buffer that contains the property info
  1783. Return Value:
  1784. NTSTATUS returned as appropriate.
  1785. --*/
  1786. {
  1787. NTSTATUS Status;
  1788. PAGED_CODE();
  1789. Status = SCMinidriverStreamPropertyHandler(SRB_SET_STREAM_PROPERTY,
  1790. Irp,
  1791. Property,
  1792. PropertyInfo);
  1793. ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
  1794. return (Status);
  1795. }
  1796. #ifdef ENABLE_KS_METHODS
  1797. NTSTATUS
  1798. StreamClassMinidriverStreamMethod(
  1799. IN PIRP Irp,
  1800. IN PKSMETHOD Method,
  1801. IN OUT PVOID MethodInfo)
  1802. /*++
  1803. Routine Description:
  1804. Process get property of a stream.
  1805. Arguments:
  1806. Irp - pointer to the irp
  1807. Property - pointer to the information for the property
  1808. PropertyInfo - buffer to return the property data to
  1809. Return Value:
  1810. NTSTATUS returned as appropriate.
  1811. --*/
  1812. {
  1813. NTSTATUS Status;
  1814. PAGED_CODE();
  1815. Status = SCMinidriverStreamMethodHandler(SRB_STREAM_METHOD,
  1816. Irp,
  1817. Method,
  1818. MethodInfo
  1819. );
  1820. ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
  1821. return (Status);
  1822. }
  1823. NTSTATUS
  1824. StreamClassMinidriverDeviceMethod(
  1825. IN PIRP Irp,
  1826. IN PKSMETHOD Method,
  1827. IN OUT PVOID MethodInfo)
  1828. /*++
  1829. Routine Description:
  1830. Process get property of a device.
  1831. Arguments:
  1832. Irp - pointer to the irp
  1833. Property - pointer to the information for the property
  1834. PropertyInfo - buffer to return the property data to
  1835. Return Value:
  1836. NTSTATUS returned as appropriate.
  1837. --*/
  1838. {
  1839. NTSTATUS Status;
  1840. PAGED_CODE();
  1841. Status = SCMinidriverDeviceMethodHandler(SRB_DEVICE_METHOD,
  1842. Irp,
  1843. Method,
  1844. MethodInfo
  1845. );
  1846. ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
  1847. return (Status);
  1848. }
  1849. #endif
  1850. NTSTATUS
  1851. StreamClassEnableEventHandler(
  1852. IN PIRP Irp,
  1853. IN PKSEVENTDATA EventData,
  1854. IN PKSEVENT_ENTRY EventEntry
  1855. )
  1856. /*++
  1857. Routine Description:
  1858. Process an enable event for the stream.
  1859. Arguments:
  1860. Irp - pointer to the IRP
  1861. EventData - data describing the event
  1862. EventEntry - more info about the event :-)
  1863. Return Value:
  1864. None.
  1865. --*/
  1866. {
  1867. PIO_STACK_LOCATION IrpStack;
  1868. PDEVICE_EXTENSION DeviceExtension;
  1869. PSTREAM_OBJECT StreamObject;
  1870. NTSTATUS Status;
  1871. ULONG EventSetID;
  1872. KIRQL irql;
  1873. HW_EVENT_DESCRIPTOR Event;
  1874. IrpStack = IoGetCurrentIrpStackLocation(Irp);
  1875. DeviceExtension = (PDEVICE_EXTENSION)
  1876. (IrpStack->DeviceObject)->DeviceExtension;
  1877. //
  1878. // clock events are indicated on the pin by the minidriver, for
  1879. // simplicity.
  1880. // but, we will receive clock events on the clock's handle. We need to
  1881. // determine if this file object is the clock's or the pin's.
  1882. //
  1883. StreamObject = IrpStack->FileObject->FsContext;
  1884. if ((PVOID) StreamObject == IrpStack->FileObject->FsContext2) {
  1885. StreamObject = ((PCLOCK_INSTANCE) StreamObject)->StreamObject;
  1886. }
  1887. //
  1888. // compute the index of the event set.
  1889. //
  1890. // this value is calculated by subtracting the base event set
  1891. // pointer from the requested event set pointer.
  1892. //
  1893. //
  1894. EventSetID = (ULONG) ((ULONG_PTR) EventEntry->EventSet -
  1895. (ULONG_PTR) StreamObject->EventInfo)
  1896. / sizeof(KSEVENT_SET);
  1897. //
  1898. // build an event info structure to represent the event to the
  1899. // minidriver.
  1900. //
  1901. Event.EnableEventSetIndex = EventSetID;
  1902. Event.EventEntry = EventEntry;
  1903. Event.StreamObject = &StreamObject->HwStreamObject;
  1904. Event.Enable = TRUE;
  1905. Event.EventData = EventData;
  1906. //
  1907. // acquire the spinlock to protect the interrupt structures
  1908. //
  1909. KeAcquireSpinLock(&DeviceExtension->SpinLock, &irql);
  1910. //
  1911. // call the synchronized routine to add the event to the list
  1912. //
  1913. Status = DeviceExtension->SynchronizeExecution(
  1914. DeviceExtension->InterruptObject,
  1915. (PKSYNCHRONIZE_ROUTINE) SCEnableEventSynchronized,
  1916. &Event);
  1917. KeReleaseSpinLock(&DeviceExtension->SpinLock, irql);
  1918. return (Status);
  1919. }
  1920. VOID
  1921. StreamClassDisableEventHandler(
  1922. IN PFILE_OBJECT FileObject,
  1923. IN PKSEVENT_ENTRY EventEntry
  1924. )
  1925. /*++
  1926. Routine Description:
  1927. Process an event disable for the stream.
  1928. NOTE: we are either at interrupt IRQL or the spinlock is taken on this call!
  1929. Arguments:
  1930. FileObject - file object for the pin
  1931. EventEntry - info about the event
  1932. Return Value:
  1933. None.
  1934. --*/
  1935. {
  1936. PDEVICE_EXTENSION DeviceExtension;
  1937. PSTREAM_OBJECT StreamObject;
  1938. HW_EVENT_DESCRIPTOR Event;
  1939. //
  1940. // clock events are indicated on the pin by the minidriver, for
  1941. // simplicity.
  1942. // but, we will receive clock events on the clock's handle. We need to
  1943. // determine if this file object is the clock's or the pin's.
  1944. //
  1945. StreamObject = FileObject->FsContext;
  1946. if ((PVOID) StreamObject == FileObject->FsContext2) {
  1947. StreamObject = ((PCLOCK_INSTANCE) StreamObject)->StreamObject;
  1948. }
  1949. DeviceExtension = StreamObject->DeviceExtension;
  1950. //
  1951. // build an event info structure to represent the event to the
  1952. // minidriver.
  1953. //
  1954. Event.EventEntry = EventEntry;
  1955. Event.StreamObject = &StreamObject->HwStreamObject;
  1956. Event.Enable = FALSE;
  1957. if (StreamObject->HwStreamObject.HwEventRoutine) {
  1958. //
  1959. // call the minidriver. ignore the status. note that we are
  1960. // already at the correct synchronization level.
  1961. //
  1962. StreamObject->HwStreamObject.HwEventRoutine(&Event);
  1963. } // if eventroutine
  1964. //
  1965. // remove the event from the list.
  1966. //
  1967. RemoveEntryList(&EventEntry->ListEntry);
  1968. }
  1969. NTSTATUS
  1970. StreamClassEnableDeviceEventHandler(
  1971. IN PIRP Irp,
  1972. IN PKSEVENTDATA EventData,
  1973. IN PKSEVENT_ENTRY EventEntry
  1974. )
  1975. /*++
  1976. Routine Description:
  1977. Process an enable event for the device.
  1978. Arguments:
  1979. Irp - pointer to the IRP
  1980. EventData - data describing the event
  1981. EventEntry - more info about the event :-)
  1982. Return Value:
  1983. None.
  1984. --*/
  1985. {
  1986. PIO_STACK_LOCATION IrpStack;
  1987. PDEVICE_EXTENSION DeviceExtension;
  1988. NTSTATUS Status;
  1989. ULONG EventSetID;
  1990. KIRQL irql;
  1991. HW_EVENT_DESCRIPTOR Event;
  1992. PFILTER_INSTANCE FilterInstance;
  1993. IrpStack = IoGetCurrentIrpStackLocation(Irp);
  1994. DeviceExtension = (PDEVICE_EXTENSION)
  1995. (IrpStack->DeviceObject)->DeviceExtension;
  1996. FilterInstance = IrpStack->FileObject->FsContext;
  1997. //
  1998. // compute the index of the event set.
  1999. //
  2000. // this value is calculated by subtracting the base event set
  2001. // pointer from the requested event set pointer.
  2002. //
  2003. //
  2004. EventSetID = (ULONG) ((ULONG_PTR) EventEntry->EventSet -
  2005. (ULONG_PTR) FilterInstance->EventInfo)
  2006. / sizeof(KSEVENT_SET);
  2007. //
  2008. // build an event info structure to represent the event to the
  2009. // minidriver.
  2010. //
  2011. Event.EnableEventSetIndex = EventSetID;
  2012. Event.EventEntry = EventEntry;
  2013. Event.DeviceExtension = DeviceExtension->HwDeviceExtension;
  2014. IF_MF( Event.HwInstanceExtension = FilterInstance->HwInstanceExtension; )
  2015. Event.Enable = TRUE;
  2016. Event.EventData = EventData;
  2017. //
  2018. // acquire the spinlock to protect the interrupt structures
  2019. //
  2020. KeAcquireSpinLock(&DeviceExtension->SpinLock, &irql);
  2021. //
  2022. // call the synchronized routine to add the event to the list
  2023. //
  2024. Status = DeviceExtension->SynchronizeExecution(
  2025. DeviceExtension->InterruptObject,
  2026. (PKSYNCHRONIZE_ROUTINE) SCEnableDeviceEventSynchronized,
  2027. &Event);
  2028. KeReleaseSpinLock(&DeviceExtension->SpinLock, irql);
  2029. return (Status);
  2030. }
  2031. VOID
  2032. StreamClassDisableDeviceEventHandler(
  2033. IN PFILE_OBJECT FileObject,
  2034. IN PKSEVENT_ENTRY EventEntry
  2035. )
  2036. /*++
  2037. Routine Description:
  2038. Process an event disable for the stream.
  2039. NOTE: we are either at interrupt IRQL or the spinlock is taken on this call!
  2040. Arguments:
  2041. FileObject - file object for the pin
  2042. EventEntry - info about the event
  2043. Return Value:
  2044. None.
  2045. --*/
  2046. {
  2047. PDEVICE_EXTENSION DeviceExtension;
  2048. HW_EVENT_DESCRIPTOR Event;
  2049. PFILTER_INSTANCE FilterInstance;
  2050. FilterInstance = (PFILTER_INSTANCE) FileObject->FsContext;
  2051. ASSERT_FILTER_INSTANCE( FilterInstance );
  2052. DeviceExtension = FilterInstance->DeviceExtension;
  2053. //
  2054. // build an event info structure to represent the event to the
  2055. // minidriver.
  2056. //
  2057. Event.EventEntry = EventEntry;
  2058. Event.DeviceExtension = DeviceExtension->HwDeviceExtension;
  2059. Event.Enable = FALSE;
  2060. Event.HwInstanceExtension = FilterInstance->HwInstanceExtension;
  2061. if (FilterInstance->HwEventRoutine) {
  2062. //
  2063. // call the minidriver. ignore the status. note that we are
  2064. // already at the correct synchronization level.
  2065. //
  2066. FilterInstance->HwEventRoutine(&Event);
  2067. }
  2068. //
  2069. // remove the event from the list.
  2070. //
  2071. RemoveEntryList(&EventEntry->ListEntry);
  2072. }
  2073. NTSTATUS
  2074. FilterDispatchIoControl(
  2075. IN PDEVICE_OBJECT DeviceObject,
  2076. IN PIRP Irp)
  2077. /*++
  2078. Routine Description:
  2079. This routine receives control IRP's for the device.
  2080. Arguments:
  2081. DeviceObject - device object for the device
  2082. Irp - probably an IRP, silly
  2083. Return Value:
  2084. The IRP status is set as appropriate
  2085. --*/
  2086. {
  2087. PIO_STACK_LOCATION IrpStack;
  2088. PDEVICE_EXTENSION DeviceExtension = DeviceObject->DeviceExtension;
  2089. PFILTER_INSTANCE FilterInstance;
  2090. NTSTATUS Status;
  2091. PAGED_CODE();
  2092. IrpStack = IoGetCurrentIrpStackLocation(Irp);
  2093. Status = SCShowIoPending(DeviceExtension, Irp);
  2094. if ( !NT_SUCCESS ( Status )) {
  2095. //
  2096. // the device is currently not accessible, so just return.
  2097. //
  2098. return (Status);
  2099. }
  2100. switch (IrpStack->Parameters.DeviceIoControl.IoControlCode) {
  2101. case IOCTL_KS_PROPERTY:
  2102. Status = STATUS_PROPSET_NOT_FOUND;
  2103. FilterInstance = (PFILTER_INSTANCE) IrpStack->FileObject->FsContext;
  2104. ASSERT( FilterInstance );
  2105. if (FilterInstance->StreamDescriptor->
  2106. StreamHeader.NumDevPropArrayEntries) {
  2107. ASSERT( FilterInstance->DevicePropertiesArray );
  2108. Status = KsPropertyHandler(Irp,
  2109. FilterInstance->StreamDescriptor->
  2110. StreamHeader.NumDevPropArrayEntries,
  2111. FilterInstance->DevicePropertiesArray);
  2112. }
  2113. if ((Status == STATUS_PROPSET_NOT_FOUND) ||
  2114. (Status == STATUS_NOT_FOUND)) {
  2115. Status = KsPropertyHandler(Irp,
  2116. SIZEOF_ARRAY(FilterPropertySets),
  2117. (PKSPROPERTY_SET) &FilterPropertySets);
  2118. }
  2119. break;
  2120. case IOCTL_KS_ENABLE_EVENT:
  2121. DebugPrint((DebugLevelTrace,
  2122. "'FilterDispatchIO: Enable event with Irp %x\n", Irp));
  2123. FilterInstance = (PFILTER_INSTANCE) IrpStack->FileObject->FsContext;
  2124. Status = KsEnableEvent(Irp,
  2125. FilterInstance->EventInfoCount,
  2126. FilterInstance->EventInfo,
  2127. NULL, 0, NULL);
  2128. break;
  2129. case IOCTL_KS_DISABLE_EVENT:
  2130. {
  2131. KSEVENTS_LOCKTYPE LockType;
  2132. PVOID LockObject;
  2133. DebugPrint((DebugLevelTrace,
  2134. "'FilterDispatchIO: Disable event with Irp %x\n", Irp));
  2135. //
  2136. // determine the type of lock necessary based on whether we are
  2137. // using interrupt or spinlock synchronization.
  2138. //
  2139. #if DBG
  2140. if (DeviceExtension->SynchronizeExecution == SCDebugKeSynchronizeExecution) {
  2141. #else
  2142. if (DeviceExtension->SynchronizeExecution == KeSynchronizeExecution) {
  2143. #endif
  2144. LockType = KSEVENTS_INTERRUPT;
  2145. LockObject = DeviceExtension->InterruptObject;
  2146. } else {
  2147. LockType = KSEVENTS_SPINLOCK;
  2148. LockObject = &DeviceExtension->SpinLock;
  2149. }
  2150. FilterInstance = (PFILTER_INSTANCE) IrpStack->
  2151. FileObject->FsContext;
  2152. Status = KsDisableEvent(Irp,
  2153. &FilterInstance->NotifyList,
  2154. LockType,
  2155. LockObject);
  2156. }
  2157. break;
  2158. case IOCTL_KS_METHOD:
  2159. Status = STATUS_PROPSET_NOT_FOUND;
  2160. break;
  2161. default:
  2162. Status = STATUS_NOT_SUPPORTED;
  2163. }
  2164. if (Status != STATUS_PENDING) {
  2165. SCCompleteIrp(Irp, Status, DeviceExtension);
  2166. }
  2167. ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
  2168. return (Status);
  2169. }
  2170. NTSTATUS
  2171. ClockDispatchIoControl(
  2172. IN PDEVICE_OBJECT DeviceObject,
  2173. IN PIRP Irp
  2174. )
  2175. /*++
  2176. Routine Description:
  2177. This routine receives control IRP's for the clock.
  2178. Arguments:
  2179. DeviceObject - device object for the device
  2180. Irp - probably an IRP, silly
  2181. Return Value:
  2182. The IRP status is set as appropriate
  2183. --*/
  2184. {
  2185. NTSTATUS Status;
  2186. PIO_STACK_LOCATION IrpStack;
  2187. PDEVICE_EXTENSION DeviceExtension = DeviceObject->DeviceExtension;
  2188. PSTREAM_OBJECT StreamObject;
  2189. PAGED_CODE();
  2190. IrpStack = IoGetCurrentIrpStackLocation(Irp);
  2191. Status = SCShowIoPending(DeviceExtension, Irp);
  2192. if ( !NT_SUCCESS ( Status )) {
  2193. //
  2194. // the device is currently not accessible, so just return.
  2195. //
  2196. return (Status);
  2197. }
  2198. switch (IrpStack->Parameters.DeviceIoControl.IoControlCode) {
  2199. case IOCTL_KS_PROPERTY:
  2200. Status = KsPropertyHandler(Irp,
  2201. SIZEOF_ARRAY(ClockPropertySets),
  2202. (PKSPROPERTY_SET) & ClockPropertySets);
  2203. break;
  2204. case IOCTL_KS_ENABLE_EVENT:
  2205. DebugPrint((DebugLevelTrace,
  2206. "'StreamDispatchIO: Enable event with Irp %x\n", Irp));
  2207. //
  2208. // locate the stream object of the pin for this clock from the IRP.
  2209. // note that we use the event set of the pin for the clock events.
  2210. //
  2211. StreamObject = (PSTREAM_OBJECT) IrpStack->FileObject->RelatedFileObject->
  2212. FsContext;
  2213. ASSERT(StreamObject);
  2214. Status = KsEnableEvent(Irp,
  2215. StreamObject->EventInfoCount,
  2216. StreamObject->EventInfo,
  2217. NULL, 0, NULL);
  2218. break;
  2219. case IOCTL_KS_DISABLE_EVENT:
  2220. {
  2221. KSEVENTS_LOCKTYPE LockType;
  2222. PVOID LockObject;
  2223. //
  2224. // locate the stream object of the pin for this clock from the
  2225. // IRP.
  2226. // note that we use the event set of the pin for the clock
  2227. // events.
  2228. //
  2229. StreamObject = (PSTREAM_OBJECT) IrpStack->FileObject->RelatedFileObject->
  2230. FsContext;
  2231. ASSERT(StreamObject);
  2232. DebugPrint((DebugLevelTrace,
  2233. "'StreamDispatchIO: Disable event with Irp %x\n", Irp));
  2234. //
  2235. // determine the type of lock necessary based on whether we are
  2236. // using interrupt or spinlock synchronization.
  2237. //
  2238. #if DBG
  2239. if (DeviceExtension->SynchronizeExecution == SCDebugKeSynchronizeExecution) {
  2240. #else
  2241. if (DeviceExtension->SynchronizeExecution == KeSynchronizeExecution) {
  2242. #endif
  2243. LockType = KSEVENTS_INTERRUPT;
  2244. LockObject = DeviceExtension->InterruptObject;
  2245. } else {
  2246. LockType = KSEVENTS_SPINLOCK;
  2247. LockObject = &DeviceExtension->SpinLock;
  2248. }
  2249. Status = KsDisableEvent(Irp,
  2250. &StreamObject->NotifyList,
  2251. LockType,
  2252. LockObject);
  2253. }
  2254. break;
  2255. case IOCTL_KS_METHOD:
  2256. #ifdef ENABLE_KS_METHODS
  2257. Status = STATUS_PROPSET_NOT_FOUND;
  2258. {
  2259. PFILTER_INSTANCE FilterInstance;
  2260. PHW_STREAM_DESCRIPTOR StreamDescriptor;
  2261. FilterInstance = (PFILTER_INSTANCE) IrpStack->FileObject->FsContext;
  2262. if ( NULL == FilterInstance->StreamDescriptor ) {
  2263. StreamDescriptor = DeviceExtension->FilterTypeInfos
  2264. [FilterInstance->FilterTypeIndex].StreamDescriptor;
  2265. }
  2266. else {
  2267. StreamDescriptor = FilterInstance->StreamDescriptor;
  2268. }
  2269. Status = KsMethodHandler(Irp,
  2270. StreamDescriptor->
  2271. StreamHeader.NumDevMethodArrayEntries,
  2272. FilterInstance->DeviceMethodsArray);
  2273. }
  2274. break;
  2275. #else
  2276. Status = STATUS_PROPSET_NOT_FOUND;
  2277. break;
  2278. #endif
  2279. default:
  2280. DEBUG_BREAKPOINT();
  2281. Status = STATUS_NOT_SUPPORTED;
  2282. }
  2283. if (Status != STATUS_PENDING) {
  2284. SCCompleteIrp(Irp, Status, DeviceExtension);
  2285. }
  2286. ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
  2287. return (Status);
  2288. }
  2289. NTSTATUS
  2290. FilterDispatchClose(
  2291. IN PDEVICE_OBJECT DeviceObject,
  2292. IN PIRP Irp
  2293. )
  2294. /*++
  2295. Routine Description:
  2296. This routine receives CLOSE IRP's for the device/instance
  2297. Arguments:
  2298. DeviceObject - device object for the device
  2299. Irp - probably an IRP, silly
  2300. Return Value:
  2301. The IRP status is set as appropriate
  2302. --*/
  2303. {
  2304. PIO_STACK_LOCATION IrpStack = IoGetCurrentIrpStackLocation(Irp);
  2305. PFILTER_INSTANCE FilterInstance =
  2306. (PFILTER_INSTANCE) IrpStack->FileObject->FsContext;
  2307. PDEVICE_EXTENSION DeviceExtension =
  2308. (PDEVICE_EXTENSION) DeviceObject->DeviceExtension;
  2309. NTSTATUS Status;
  2310. BOOLEAN IsGlobal;
  2311. BOOLEAN RequestIssued;
  2312. PAGED_CODE();
  2313. InterlockedIncrement(&DeviceExtension->OneBasedIoCount);
  2314. //
  2315. // remove the filter instance structure from our list
  2316. //
  2317. #if DBG
  2318. IFN_MF(
  2319. if (DeviceExtension->NumberOfGlobalInstances == 1) {
  2320. ASSERT(IsListEmpty(&FilterInstance->FirstStream));
  2321. } // if global = 1
  2322. )
  2323. #endif
  2324. //
  2325. // check to see if this is a global instance
  2326. //
  2327. KeWaitForSingleObject(&DeviceExtension->ControlEvent,
  2328. Executive,
  2329. KernelMode,
  2330. FALSE,// not alertable
  2331. NULL);
  2332. DebugPrint(( DebugLevelInfo,
  2333. "Closing FilterInstance %x NeameExts=%x\n",
  2334. FilterInstance,
  2335. DeviceExtension->NumberOfNameExtensions));
  2336. if ( 0 == DeviceExtension->FilterExtensionSize &&
  2337. DeviceExtension->NumberOfOpenInstances > 1) {
  2338. PFILE_OBJECT pFileObject;
  2339. //
  2340. // this is not the last close of the global instance, so just
  2341. // deref this instance and return good status.
  2342. //
  2343. DeviceExtension->NumberOfOpenInstances--;
  2344. DebugPrint(( DebugLevelInfo,
  2345. "DevExt=%x Close OpenCount=%x\n",
  2346. DeviceExtension,
  2347. DeviceExtension->NumberOfOpenInstances));
  2348. IrpStack->FileObject->FsContext = NULL;
  2349. KeSetEvent(&DeviceExtension->ControlEvent, IO_NO_INCREMENT, FALSE);
  2350. ObDereferenceObject(DeviceObject);
  2351. SCDereferenceDriver(DeviceExtension);
  2352. ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
  2353. return (SCCompleteIrp(Irp, STATUS_SUCCESS, DeviceExtension));
  2354. }
  2355. //
  2356. // we now know that this is either a local instance, or the last open of
  2357. // the global instance. process the close.
  2358. //
  2359. if ( 0 != DeviceExtension->FilterExtensionSize ) {
  2360. Status = SCSubmitRequest(SRB_CLOSE_DEVICE_INSTANCE,
  2361. NULL,
  2362. 0,
  2363. SCCloseInstanceCallback,
  2364. DeviceExtension,
  2365. FilterInstance->HwInstanceExtension,
  2366. NULL,
  2367. Irp,
  2368. &RequestIssued,
  2369. &DeviceExtension->PendingQueue,
  2370. (PVOID) DeviceExtension->
  2371. MinidriverData->HwInitData.
  2372. HwReceivePacket);
  2373. if (!RequestIssued) {
  2374. DEBUG_BREAKPOINT();
  2375. KeSetEvent(&DeviceExtension->ControlEvent, IO_NO_INCREMENT, FALSE);
  2376. SCCompleteIrp(Irp, Status, DeviceExtension);
  2377. }
  2378. ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
  2379. return (Status);
  2380. } else { // if instanceextension
  2381. //
  2382. // the minidriver doesn't need to be called as it does not support
  2383. // instancing. dereference the instance now.
  2384. //
  2385. DeviceExtension->NumberOfOpenInstances--;
  2386. DebugPrint(( DebugLevelInfo,
  2387. "DevExt=%x Close OpenCount=%x\n",
  2388. DeviceExtension,
  2389. DeviceExtension->NumberOfOpenInstances));
  2390. //
  2391. // we are ready to free the instance. if it is global, just zero
  2392. // the pointer. if it is local, remove it from the list.
  2393. //
  2394. IrpStack->FileObject->FsContext = NULL;
  2395. DebugPrint((DebugLevelInfo, "FilterCloseInstance=%x\n", FilterInstance));
  2396. if ( !IsListEmpty( &DeviceExtension->FilterInstanceList)) {
  2397. //
  2398. // The list could be emptied at surprise removal
  2399. // where all instances are removed. so when come in here
  2400. // check it first. Event is taken, check is safe.
  2401. //
  2402. RemoveEntryList(&FilterInstance->NextFilterInstance);
  2403. SciFreeFilterInstance( FilterInstance );
  2404. FilterInstance = NULL;
  2405. }
  2406. else {
  2407. //
  2408. // it has been closed by surprise removal. mark it.
  2409. //
  2410. FilterInstance= NULL;
  2411. }
  2412. //
  2413. // if this is the last close of a removed device, detach from
  2414. // the PDO now, since we couldn't do it on the remove. note that
  2415. // we will NOT do this if the NT style surprise remove IRP has been
  2416. // received, since we'll still receive an IRP_REMOVE in that case
  2417. // after
  2418. // this close.
  2419. //
  2420. if ((DeviceExtension->NumberOfOpenInstances == 0) &&
  2421. (DeviceExtension->Flags & DEVICE_FLAGS_DEVICE_INACCESSIBLE) &&
  2422. !(DeviceExtension->Flags & DEVICE_FLAGS_SURPRISE_REMOVE_RECEIVED)) {
  2423. DebugPrint((DebugLevelInfo,
  2424. "SCPNP: detaching %x from %x\n",
  2425. DeviceObject,
  2426. DeviceExtension->AttachedPdo));
  2427. //
  2428. // detach could happen at remove, check before leap.
  2429. // event is taken, check is safe.
  2430. //
  2431. if ( NULL != DeviceExtension->AttachedPdo ) {
  2432. IoDetachDevice(DeviceExtension->AttachedPdo);
  2433. DeviceExtension->AttachedPdo = NULL;
  2434. }
  2435. }
  2436. else {
  2437. //
  2438. // check if we can power down the device.
  2439. //
  2440. SCCheckPowerDown(DeviceExtension);
  2441. } // if inaccessible
  2442. KeSetEvent(&DeviceExtension->ControlEvent, IO_NO_INCREMENT, FALSE);
  2443. if ( NULL != FilterInstance ) {
  2444. DebugPrint(( DebugLevelVerbose,
  2445. "Unregistering ReadWorker %x WriteWorker %x\n",
  2446. FilterInstance->WorkerRead,
  2447. FilterInstance->WorkerWrite));
  2448. KsUnregisterWorker( FilterInstance->WorkerRead );
  2449. KsUnregisterWorker( FilterInstance->WorkerWrite );
  2450. KsFreeObjectHeader(FilterInstance->DeviceHeader);
  2451. ExFreePool(FilterInstance);
  2452. }
  2453. SCDereferenceDriver(DeviceExtension);
  2454. ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
  2455. Status = SCCompleteIrp(Irp, STATUS_SUCCESS, DeviceExtension);
  2456. ObDereferenceObject(DeviceObject);
  2457. return (Status);
  2458. }
  2459. }
  2460. NTSTATUS
  2461. SCCloseInstanceCallback(
  2462. IN PSTREAM_REQUEST_BLOCK SRB
  2463. )
  2464. /*++
  2465. Routine Description:
  2466. Process the completion of an instance close.
  2467. Arguments:
  2468. SRB - address of the completed SRB
  2469. Return Value:
  2470. None.
  2471. --*/
  2472. {
  2473. PDEVICE_EXTENSION DeviceExtension =
  2474. (PDEVICE_EXTENSION) SRB->HwSRB.HwDeviceExtension - 1;
  2475. PIRP Irp = SRB->HwSRB.Irp;
  2476. PIO_STACK_LOCATION IrpStack = IoGetCurrentIrpStackLocation(Irp);
  2477. PFILTER_INSTANCE FilterInstance =
  2478. (PFILTER_INSTANCE) SRB->HwSRB.HwInstanceExtension - 1;
  2479. NTSTATUS Status = SRB->HwSRB.Status;
  2480. KIRQL irql;
  2481. //
  2482. // Close should not fail. If it does, should clean up anyway
  2483. ASSERT( NT_SUCCESS(Status) && "Close Instance failed" );
  2484. ///if (NT_SUCCESS(Status)) {
  2485. //
  2486. // we are ready to free the instance. if it is global, just zero
  2487. // the pointer. if it is local, remove it from the list.
  2488. //
  2489. DeviceExtension->NumberOfOpenInstances--;
  2490. KeAcquireSpinLock(&DeviceExtension->SpinLock, &irql);
  2491. RemoveEntryList(&FilterInstance->NextFilterInstance);
  2492. //
  2493. // free the instance and return success.
  2494. //
  2495. KeReleaseSpinLock(&DeviceExtension->SpinLock, irql);
  2496. //
  2497. // if this is the last close of a removed device, detach from
  2498. // the PDO now, since we couldn't do it on the remove.
  2499. //
  2500. if ((DeviceExtension->NumberOfOpenInstances == 0) &&
  2501. (DeviceExtension->Flags & DEVICE_FLAGS_DEVICE_INACCESSIBLE)) {
  2502. DebugPrint((DebugLevelTrace,
  2503. "'SCPNP: detaching from PDO\n"));
  2504. TRAP;
  2505. IoDetachDevice(DeviceExtension->AttachedPdo);
  2506. DeviceExtension->AttachedPdo = NULL;
  2507. }
  2508. //
  2509. // check if we can power down the device.
  2510. //
  2511. SCCheckPowerDown(DeviceExtension);
  2512. ObDereferenceObject(DeviceExtension->DeviceObject);
  2513. //
  2514. // free the instance and header and dereference the driver
  2515. //
  2516. SciFreeFilterInstance( FilterInstance );
  2517. ///#ifdef ENABLE_STREAM_CLASS_AS_ALLOCATOR
  2518. ///DebugPrint(( DebugLevelVerbose,
  2519. /// "Unregistering ReadWorker %x WriteWorker %x\n",
  2520. /// FilterInstance->WorkerRead,
  2521. /// FilterInstance->WorkerWrite));
  2522. ///
  2523. ///KsUnregisterWorker( FilterInstance->WorkerRead );
  2524. ///KsUnregisterWorker( FilterInstance->WorkerWrite );
  2525. ///#endif
  2526. ///KsFreeObjectHeader(FilterInstance->DeviceHeader);
  2527. ///ExFreePool(FilterInstance);
  2528. SCDereferenceDriver(DeviceExtension);
  2529. ///} // if good status
  2530. //
  2531. // signal the event and complete the IRP.
  2532. //
  2533. KeSetEvent(&DeviceExtension->ControlEvent, IO_NO_INCREMENT, FALSE);
  2534. SCProcessCompletedRequest(SRB);
  2535. return (Status);
  2536. }
  2537. NTSTATUS
  2538. StreamDispatchCleanup
  2539. (
  2540. IN PDEVICE_OBJECT DeviceObject,
  2541. IN PIRP Irp
  2542. )
  2543. /*++
  2544. Routine Description:
  2545. This routine receives CLEANUP IRP's for a stream
  2546. Arguments:
  2547. DeviceObject - device object for the device
  2548. Irp - The CLEANUP Irp
  2549. Return Value:
  2550. The IRP status set as appropriate
  2551. --*/
  2552. {
  2553. PIO_STACK_LOCATION IrpStack = IoGetCurrentIrpStackLocation (Irp);
  2554. PSTREAM_OBJECT StreamObject =
  2555. (PSTREAM_OBJECT) IrpStack -> FileObject -> FsContext;
  2556. PDEVICE_EXTENSION DeviceExtension =
  2557. (PDEVICE_EXTENSION) DeviceObject -> DeviceExtension;
  2558. BOOLEAN BreakClockCycle = FALSE;
  2559. KeWaitForSingleObject (
  2560. &DeviceExtension -> ControlEvent,
  2561. Executive,
  2562. KernelMode,
  2563. FALSE,
  2564. NULL
  2565. );
  2566. //
  2567. // If the stream in question is a source stream and it has not yet
  2568. // stopped the sourcing worker, it must be done at this point in time.
  2569. //
  2570. if (StreamObject -> CurrentState > KSSTATE_STOP &&
  2571. StreamObject -> PinType == IrpSource &&
  2572. StreamObject -> StandardTransport) {
  2573. EndTransfer (StreamObject -> FilterInstance, StreamObject);
  2574. }
  2575. //
  2576. // Check for the clock<->pin cycle and break it if present.
  2577. //
  2578. if (StreamObject -> MasterClockInfo) {
  2579. PFILE_OBJECT ClockFile = StreamObject -> MasterClockInfo ->
  2580. ClockFileObject;
  2581. if (ClockFile &&
  2582. ClockFile -> RelatedFileObject == StreamObject -> FileObject)
  2583. BreakClockCycle = TRUE;
  2584. }
  2585. KeSetEvent(&DeviceExtension->ControlEvent, IO_NO_INCREMENT, FALSE);
  2586. //
  2587. // Synchronously submit an Irp down our own stack to get break the
  2588. // clock<->pin cycle. Otherwise, the stream can't close. The driver should
  2589. // guard against the clock disappearing while running. Stream class does
  2590. // on TOP of that if they do not.
  2591. //
  2592. if (BreakClockCycle) {
  2593. KSPROPERTY Property;
  2594. HANDLE NewClock = NULL;
  2595. ULONG BytesReturned;
  2596. NTSTATUS Status;
  2597. Property.Set = KSPROPSETID_Stream;
  2598. Property.Id = KSPROPERTY_STREAM_MASTERCLOCK;
  2599. Property.Flags = KSPROPERTY_TYPE_SET;
  2600. Status =
  2601. KsSynchronousIoControlDevice (
  2602. StreamObject -> FileObject,
  2603. KernelMode,
  2604. IOCTL_KS_PROPERTY,
  2605. &Property,
  2606. sizeof (KSPROPERTY),
  2607. &NewClock,
  2608. sizeof (HANDLE),
  2609. &BytesReturned
  2610. );
  2611. ASSERT (NT_SUCCESS (Status));
  2612. }
  2613. Irp -> IoStatus.Status = STATUS_SUCCESS;
  2614. IoCompleteRequest (Irp, IO_NO_INCREMENT);
  2615. return STATUS_SUCCESS;
  2616. }
  2617. NTSTATUS
  2618. StreamDispatchClose
  2619. (
  2620. IN PDEVICE_OBJECT DeviceObject,
  2621. IN PIRP Irp
  2622. )
  2623. /*++
  2624. Routine Description:
  2625. This routine receives CLOSE IRP's for a stream
  2626. Arguments:
  2627. DeviceObject - device object for the device
  2628. Irp - probably an IRP, silly
  2629. Return Value:
  2630. The IRP status is set as appropriate
  2631. --*/
  2632. {
  2633. PIO_STACK_LOCATION IrpStack = IoGetCurrentIrpStackLocation(Irp);
  2634. PSTREAM_OBJECT StreamObject =
  2635. (PSTREAM_OBJECT) IrpStack->FileObject->FsContext;
  2636. PDEVICE_EXTENSION DeviceExtension =
  2637. (PDEVICE_EXTENSION) DeviceObject->DeviceExtension;
  2638. NTSTATUS Status;
  2639. BOOLEAN RequestIssued;
  2640. KSEVENTS_LOCKTYPE LockType;
  2641. PVOID LockObject;
  2642. PAGED_CODE();
  2643. InterlockedIncrement(&DeviceExtension->OneBasedIoCount);
  2644. ASSERT(IsListEmpty(&StreamObject->ControlPendingQueue));
  2645. ASSERT(IsListEmpty(&StreamObject->DataPendingQueue));
  2646. //
  2647. // free events associated with this stream. this will cause our remove
  2648. // handler to be called for each, and will hence notify the minidriver.
  2649. //
  2650. //
  2651. // determine the type of lock necessary based on whether we are
  2652. // using interrupt or spinlock synchronization.
  2653. //
  2654. #if DBG
  2655. if (DeviceExtension->SynchronizeExecution == SCDebugKeSynchronizeExecution) {
  2656. #else
  2657. if (DeviceExtension->SynchronizeExecution == KeSynchronizeExecution) {
  2658. #endif
  2659. LockType = KSEVENTS_INTERRUPT;
  2660. LockObject = DeviceExtension->InterruptObject;
  2661. } else {
  2662. LockType = KSEVENTS_SPINLOCK;
  2663. LockObject = &DeviceExtension->SpinLock;
  2664. }
  2665. KsFreeEventList(IrpStack->FileObject,
  2666. &StreamObject->NotifyList,
  2667. LockType,
  2668. LockObject);
  2669. //
  2670. // call the minidriver to close the stream. processing will continue
  2671. // when the callback procedure is called.
  2672. //
  2673. KeWaitForSingleObject(&DeviceExtension->ControlEvent,
  2674. Executive,
  2675. KernelMode,
  2676. FALSE,// not alertable
  2677. NULL);
  2678. Status = SCSubmitRequest(SRB_CLOSE_STREAM,
  2679. NULL,
  2680. 0,
  2681. SCCloseStreamCallback,
  2682. DeviceExtension,
  2683. StreamObject->
  2684. FilterInstance->HwInstanceExtension,
  2685. &StreamObject->HwStreamObject,
  2686. Irp,
  2687. &RequestIssued,
  2688. &DeviceExtension->PendingQueue,
  2689. (PVOID) DeviceExtension->
  2690. MinidriverData->HwInitData.
  2691. HwReceivePacket);
  2692. if (!RequestIssued) {
  2693. DEBUG_BREAKPOINT();
  2694. KeSetEvent(&DeviceExtension->ControlEvent, IO_NO_INCREMENT, FALSE);
  2695. SCCompleteIrp(Irp, Status, DeviceExtension);
  2696. }
  2697. ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
  2698. return (Status);
  2699. }
  2700. NTSTATUS
  2701. SCCloseStreamCallback(
  2702. IN PSTREAM_REQUEST_BLOCK SRB
  2703. )
  2704. /*++
  2705. Routine Description:
  2706. Process the completion of a stream close.
  2707. Arguments:
  2708. SRB - address of the completed SRB
  2709. Return Value:
  2710. None.
  2711. --*/
  2712. {
  2713. PDEVICE_EXTENSION DeviceExtension =
  2714. (PDEVICE_EXTENSION) SRB->HwSRB.HwDeviceExtension - 1;
  2715. PADDITIONAL_PIN_INFO AdditionalInfo;
  2716. PSTREAM_OBJECT StreamObject = CONTAINING_RECORD(
  2717. SRB->HwSRB.StreamObject,
  2718. STREAM_OBJECT,
  2719. HwStreamObject
  2720. );
  2721. PIRP Irp = SRB->HwSRB.Irp;
  2722. PIO_STACK_LOCATION IrpStack = IoGetCurrentIrpStackLocation(Irp);
  2723. KIRQL Irql;
  2724. NTSTATUS Status = SRB->HwSRB.Status;
  2725. ASSERT( NT_SUCCESS(Status) && "CloseStream Failed by Minidriver");
  2726. //
  2727. // Close should not fail. Even it does, we want to clean up.
  2728. //
  2729. // if (NT_SUCCESS(Status)) {
  2730. //
  2731. // show one fewer instance open
  2732. //
  2733. DebugPrint((DebugLevelInfo, "SC Closing StreamObject %x\n", StreamObject));
  2734. AdditionalInfo = ((PFILTER_INSTANCE) IrpStack->FileObject->
  2735. RelatedFileObject->FsContext)->PinInstanceInfo;
  2736. AdditionalInfo[StreamObject->HwStreamObject.StreamNumber].
  2737. CurrentInstances--;
  2738. //
  2739. // free the object header for the stream
  2740. //
  2741. KsFreeObjectHeader(StreamObject->ComObj.DeviceHeader);
  2742. //
  2743. // free the constructed props, if any.
  2744. //
  2745. if (StreamObject->ConstructedPropertyInfo) {
  2746. ExFreePool(StreamObject->ConstructedPropertyInfo);
  2747. }
  2748. //
  2749. // signal the event.
  2750. // signal now so that we won't
  2751. // deadlock when we dereference the object and the filter is closed.
  2752. //
  2753. KeSetEvent(&DeviceExtension->ControlEvent, IO_NO_INCREMENT, FALSE);
  2754. //
  2755. // Zero the pointer to our per stream structure in the FsContext
  2756. // field of
  2757. // of FileObject.
  2758. //
  2759. IrpStack->FileObject->FsContext = 0;
  2760. //
  2761. // remove the stream object from the filter instance list
  2762. //
  2763. KeAcquireSpinLock(&DeviceExtension->SpinLock, &Irql);
  2764. RemoveEntryList(&StreamObject->NextStream);
  2765. KeReleaseSpinLock(&DeviceExtension->SpinLock, Irql);
  2766. //
  2767. // kill the timer, which might have been left dangling by the
  2768. // minidriver.
  2769. //
  2770. KeCancelTimer(&StreamObject->ComObj.MiniDriverTimer);
  2771. //
  2772. // dereference the master clock if any
  2773. //
  2774. if (StreamObject->MasterClockInfo) {
  2775. ObDereferenceObject(StreamObject->MasterClockInfo->ClockFileObject);
  2776. ExFreePool(StreamObject->MasterClockInfo);
  2777. }
  2778. #ifdef ENABLE_STREAM_CLASS_AS_ALLOCATOR
  2779. //
  2780. // dereference the next file object
  2781. //
  2782. if (StreamObject->NextFileObject)
  2783. {
  2784. ObDereferenceObject(StreamObject->NextFileObject);
  2785. StreamObject->NextFileObject = NULL;
  2786. }
  2787. //
  2788. // Dereference the allocator object or stream obj won't be
  2789. // release while it should. Problems would follow particularly
  2790. // with SWEnum loaded driver.
  2791. //
  2792. if ( StreamObject->AllocatorFileObject ) {
  2793. ObDereferenceObject( StreamObject->AllocatorFileObject );
  2794. StreamObject->AllocatorFileObject = NULL;
  2795. }
  2796. #endif
  2797. //
  2798. // dereference the filter
  2799. //
  2800. ObDereferenceObject(StreamObject->FilterFileObject);
  2801. ExFreePool(StreamObject);
  2802. ///} else { // if good status
  2803. /// KeSetEvent(&DeviceExtension->ControlEvent, IO_NO_INCREMENT, FALSE);
  2804. ///} // if good status
  2805. SCProcessCompletedRequest(SRB);
  2806. return (Status);
  2807. }
  2808. BOOLEAN
  2809. StreamClassInterrupt(
  2810. IN PKINTERRUPT Interrupt,
  2811. IN PDEVICE_OBJECT DeviceObject
  2812. )
  2813. /*++
  2814. Routine Description:
  2815. Process interrupt from the device
  2816. Arguments:
  2817. Interrupt - interrupt object
  2818. Device Object - device object which is interrupting
  2819. Return Value:
  2820. Returns TRUE if interrupt expected.
  2821. --*/
  2822. {
  2823. PDEVICE_EXTENSION deviceExtension = DeviceObject->DeviceExtension;
  2824. BOOLEAN returnValue;
  2825. UNREFERENCED_PARAMETER(Interrupt);
  2826. //
  2827. // check if the interrupt cannot currently go down
  2828. //
  2829. if (deviceExtension->DriverInfo->Flags & DRIVER_FLAGS_PAGED_OUT) {
  2830. return (FALSE);
  2831. }
  2832. //
  2833. // call the minidriver's interrupt service routine.
  2834. //
  2835. returnValue = deviceExtension->MinidriverData->
  2836. HwInitData.HwInterrupt(deviceExtension->HwDeviceExtension);
  2837. //
  2838. // Queue up a DPC if needed.
  2839. //
  2840. if ((deviceExtension->NeedyStream) || (deviceExtension->ComObj.
  2841. InterruptData.Flags & INTERRUPT_FLAGS_NOTIFICATION_REQUIRED)) {
  2842. KeInsertQueueDpc(&deviceExtension->WorkDpc, NULL, NULL);
  2843. }
  2844. return (returnValue);
  2845. } // end StreamClassInterrupt()
  2846. NTSTATUS
  2847. StreamClassNull(
  2848. IN PDEVICE_OBJECT DeviceObject,
  2849. IN PIRP Irp
  2850. )
  2851. /*++
  2852. Routine Description:
  2853. This routine fails incoming irps.
  2854. Arguments:
  2855. DeviceObject - device object for the device
  2856. Irp - probably an IRP, silly
  2857. Return Value:
  2858. The IRP status is returned
  2859. --*/
  2860. {
  2861. //
  2862. // complete the IRP with error status
  2863. //
  2864. PAGED_CODE();
  2865. return (SCCompleteIrp(Irp, STATUS_NOT_SUPPORTED, DeviceObject->DeviceExtension));
  2866. }
  2867. NTSTATUS
  2868. SCFilterPinInstances(
  2869. IN PIRP Irp,
  2870. IN PKSPROPERTY Property,
  2871. IN OUT PVOID Data)
  2872. /*++
  2873. Routine Description:
  2874. Returns the # of instances supported by a pin
  2875. Arguments:
  2876. Irp - pointer to the irp
  2877. Property - pointer to the property info
  2878. Data - instance info
  2879. Return Value:
  2880. NTSTATUS returned as appropriate
  2881. --*/
  2882. {
  2883. ULONG Pin;
  2884. PKSPIN_CINSTANCES CInstances;
  2885. PIO_STACK_LOCATION IrpStack;
  2886. PDEVICE_EXTENSION DeviceExtension;
  2887. PFILTER_INSTANCE FilterInstance;
  2888. PADDITIONAL_PIN_INFO AdditionalPinInfo;
  2889. PAGED_CODE();
  2890. IrpStack = IoGetCurrentIrpStackLocation(Irp);
  2891. DeviceExtension = (PDEVICE_EXTENSION) IrpStack->
  2892. DeviceObject->DeviceExtension;
  2893. FilterInstance = IrpStack->FileObject->FsContext;
  2894. //
  2895. // get the pin #
  2896. //
  2897. Pin = ((PKSP_PIN) Property)->PinId;
  2898. //
  2899. // if max pin number exceeded, return error
  2900. //
  2901. IFN_MF(
  2902. if (Pin >= DeviceExtension->NumberOfPins) {
  2903. DEBUG_BREAKPOINT();
  2904. return (STATUS_INVALID_PARAMETER);
  2905. }
  2906. )
  2907. IF_MF(
  2908. if (Pin >= FilterInstance->NumberOfPins) {
  2909. DEBUG_BREAKPOINT();
  2910. return (STATUS_INVALID_PARAMETER);
  2911. }
  2912. )
  2913. CInstances = (PKSPIN_CINSTANCES) Data;
  2914. AdditionalPinInfo = FilterInstance->PinInstanceInfo;
  2915. CInstances->PossibleCount = AdditionalPinInfo[Pin].MaxInstances;
  2916. CInstances->CurrentCount = AdditionalPinInfo[Pin].CurrentInstances;
  2917. Irp->IoStatus.Information = sizeof(KSPIN_CINSTANCES);
  2918. ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
  2919. return (STATUS_SUCCESS);
  2920. }
  2921. NTSTATUS
  2922. SCFilterPinPropertyHandler(
  2923. IN PIRP Irp,
  2924. IN PKSPROPERTY Property,
  2925. IN OUT PVOID Data)
  2926. /*++
  2927. Routine Description:
  2928. Dispatches a pin property request
  2929. Arguments:
  2930. Irp - pointer to the irp
  2931. Property - pointer to the property info
  2932. Data - property specific buffer
  2933. Return Value:
  2934. NTSTATUS returned as appropriate
  2935. --*/
  2936. {
  2937. PIO_STACK_LOCATION IrpStack = IoGetCurrentIrpStackLocation(Irp);
  2938. PDEVICE_EXTENSION DeviceExtension = (PDEVICE_EXTENSION) IrpStack->
  2939. DeviceObject->DeviceExtension;
  2940. PFILTER_INSTANCE FilterInstance= (PFILTER_INSTANCE) IrpStack->
  2941. FileObject->FsContext;
  2942. PAGED_CODE();
  2943. return KsPinPropertyHandler(Irp,
  2944. Property,
  2945. Data,
  2946. FilterInstance->NumberOfPins,
  2947. FilterInstance->PinInformation);
  2948. }
  2949. VOID
  2950. StreamClassTickHandler(
  2951. IN PDEVICE_OBJECT DeviceObject,
  2952. IN PVOID Context
  2953. )
  2954. /*++
  2955. Routine Description:
  2956. Tick handler for device.
  2957. Arguments:
  2958. DeviceObject - pointer to the device object
  2959. Context - unreferenced
  2960. Return Value:
  2961. None.
  2962. --*/
  2963. {
  2964. PDEVICE_EXTENSION DeviceExtension =
  2965. (PDEVICE_EXTENSION) DeviceObject->DeviceExtension;
  2966. PLIST_ENTRY ListEntry;
  2967. PLIST_ENTRY SrbListEntry = ListEntry = &DeviceExtension->OutstandingQueue;
  2968. PSTREAM_REQUEST_BLOCK Srb;
  2969. UNREFERENCED_PARAMETER(Context);
  2970. ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL);
  2971. //
  2972. // acquire the device spinlock to protect the queues.
  2973. //
  2974. KeAcquireSpinLockAtDpcLevel(&DeviceExtension->SpinLock);
  2975. //
  2976. // process any timed out requests on the device
  2977. //
  2978. while (SrbListEntry->Flink != ListEntry) {
  2979. SrbListEntry = SrbListEntry->Flink;
  2980. Srb = CONTAINING_RECORD(SrbListEntry,
  2981. STREAM_REQUEST_BLOCK,
  2982. SRBListEntry);
  2983. //
  2984. // first make sure the request is active, since it could have been
  2985. // called back but not yet removed from the queue.
  2986. //
  2987. if (Srb->Flags & SRB_FLAGS_IS_ACTIVE) {
  2988. //
  2989. // check for a timeout if the counter is currently nonzero.
  2990. //
  2991. if (Srb->HwSRB.TimeoutCounter != 0) {
  2992. if (--Srb->HwSRB.TimeoutCounter == 0) {
  2993. //
  2994. // request timed out. Call the minidriver to process it.
  2995. // first reset the timer in case the minidriver is
  2996. // busted.
  2997. //
  2998. DebugPrint((DebugLevelError, "SCTickHandler: Irp %x timed out! SRB = %x, SRB func = %x, Stream Object = %x\n",
  2999. Srb->HwSRB.Irp, Srb, Srb->HwSRB.Command, Srb->HwSRB.StreamObject));
  3000. Srb->HwSRB.TimeoutCounter = Srb->HwSRB.TimeoutOriginal;
  3001. DeviceExtension = (PDEVICE_EXTENSION)
  3002. Srb->HwSRB.HwDeviceExtension - 1;
  3003. //
  3004. // if we are not synchronizing the minidriver, release
  3005. // and reacquire the spinlock around the call into it.
  3006. //
  3007. if (DeviceExtension->NoSync) {
  3008. //
  3009. // we need to ensure that the SRB memory is valid for
  3010. // the async
  3011. // minidriver, EVEN if it happens to call back the
  3012. // request just
  3013. // before we call it to cancel it! This is done for
  3014. // two reasons:
  3015. // it obviates the need for the minidriver to walk
  3016. // its request
  3017. // queues to find the request, and I failed to pass
  3018. // the dev ext
  3019. // pointer to the minidriver in the below call, which
  3020. // means that
  3021. // the SRB HAS to be valid, and it's too late to
  3022. // change the API.
  3023. //
  3024. // Oh, well. Spinlock is now taken (by caller).
  3025. //
  3026. Srb->DoNotCallBack = TRUE;
  3027. KeReleaseSpinLockFromDpcLevel(&DeviceExtension->SpinLock);
  3028. (DeviceExtension->MinidriverData->HwInitData.HwRequestTimeoutHandler)
  3029. (&Srb->HwSRB);
  3030. KeAcquireSpinLockAtDpcLevel(&DeviceExtension->SpinLock);
  3031. //
  3032. // if the ACTIVE flag is now clear, it indicates that
  3033. // the
  3034. // SRB was completed during the above call into the
  3035. // minidriver.
  3036. // since we blocked the internal completion of the
  3037. // request,
  3038. // we must call it back ourselves in this case.
  3039. //
  3040. Srb->DoNotCallBack = FALSE;
  3041. if (!(Srb->Flags & SRB_FLAGS_IS_ACTIVE)) {
  3042. TRAP;
  3043. KeReleaseSpinLockFromDpcLevel(&DeviceExtension->SpinLock);
  3044. (Srb->Callback) (Srb);
  3045. KeAcquireSpinLockAtDpcLevel(&DeviceExtension->SpinLock);
  3046. } // if ! active
  3047. break;
  3048. } else { // if nosync
  3049. DeviceExtension->SynchronizeExecution(
  3050. DeviceExtension->InterruptObject,
  3051. (PVOID) DeviceExtension->MinidriverData->HwInitData.HwRequestTimeoutHandler,
  3052. &Srb->HwSRB);
  3053. // return now in case the minidriver aborted any
  3054. // other
  3055. // requests that
  3056. // may be timing out now.
  3057. //
  3058. break;
  3059. } // if nosync
  3060. } // if timed out
  3061. } // if counter != 0
  3062. } // if active
  3063. } // while list entry
  3064. //
  3065. // let my people go...
  3066. //
  3067. KeReleaseSpinLockFromDpcLevel(&DeviceExtension->SpinLock);
  3068. ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL);
  3069. return;
  3070. } // end StreamClassTickHandler()
  3071. VOID
  3072. StreamClassCancelPendingIrp(
  3073. IN PDEVICE_OBJECT DeviceObject,
  3074. IN PIRP Irp
  3075. )
  3076. /*++
  3077. Routine Description:
  3078. Cancel routine for pending IRP's.
  3079. Arguments:
  3080. DeviceObject - pointer to the device object
  3081. Irp - pointer to IRP to be cancelled
  3082. Return Value:
  3083. None.
  3084. --*/
  3085. {
  3086. PDEVICE_EXTENSION DeviceExtension = DeviceObject->DeviceExtension;
  3087. PFILTER_INSTANCE FilterInstance;
  3088. PLIST_ENTRY ListHead, ListEntry;
  3089. KIRQL CancelIrql,
  3090. Irql;
  3091. PSTREAM_REQUEST_BLOCK SRB;
  3092. DebugPrint((DebugLevelWarning, "'SCCancelPending: trying to cancel Irp = %x\n",
  3093. Irp));
  3094. //
  3095. // acquire the device spinlock then release the cancel spinlock.
  3096. //
  3097. KeAcquireSpinLock(&DeviceExtension->SpinLock, &Irql);
  3098. CancelIrql = Irp->CancelIrql;
  3099. IoReleaseCancelSpinLock(Irql);
  3100. //
  3101. // there are two possibilities here. 1) the IRP is on the pending queue
  3102. // for the particular stream. 2) the IRP was moved from pending to
  3103. // outstanding and has been submitted to the minidriver.
  3104. // If we are running above an external bus driver, don't
  3105. //
  3106. //
  3107. // now process all streams on the local filter instances.
  3108. //
  3109. ListHead = &DeviceExtension->FilterInstanceList;
  3110. ListEntry = ListHead->Flink;
  3111. while ( ListEntry != ListHead ) {
  3112. //
  3113. // follow the link to the instance
  3114. //
  3115. FilterInstance = CONTAINING_RECORD(ListEntry,
  3116. FILTER_INSTANCE,
  3117. NextFilterInstance);
  3118. //
  3119. // process the streams on this list
  3120. //
  3121. if (SCCheckFilterInstanceStreamsForIrp(FilterInstance, Irp)) {
  3122. goto found;
  3123. }
  3124. ListEntry = ListEntry->Flink;
  3125. }
  3126. //
  3127. // now process any requests on the device itself
  3128. //
  3129. if (SCCheckRequestsForIrp(
  3130. &DeviceExtension->OutstandingQueue, Irp, TRUE, DeviceExtension)) {
  3131. goto found;
  3132. }
  3133. //
  3134. // request is not on pending queue, so call to check the outstanding
  3135. // queue
  3136. //
  3137. SCCancelOutstandingIrp(DeviceExtension, Irp);
  3138. KeReleaseSpinLockFromDpcLevel(&DeviceExtension->SpinLock);
  3139. exit:
  3140. //
  3141. // now call the DPC in case the request was successfully aborted.
  3142. //
  3143. StreamClassDpc(NULL,
  3144. DeviceExtension->DeviceObject,
  3145. NULL,
  3146. NULL);
  3147. KeLowerIrql(CancelIrql);
  3148. return;
  3149. found:
  3150. //
  3151. // the irp is on one of our pending queues. remove it from the queue and
  3152. // complete it.
  3153. //
  3154. RemoveEntryList(&Irp->Tail.Overlay.ListEntry);
  3155. //
  3156. // retrieve the SRB.
  3157. //
  3158. SRB = Irp->Tail.Overlay.DriverContext[0];
  3159. //
  3160. // hack - the completion handlers will try to remove the SRB from the
  3161. // outstanding queue. Point the SRB's queues to itself so this will not
  3162. // cause a problem.
  3163. //
  3164. SRB->SRBListEntry.Flink = &SRB->SRBListEntry;
  3165. SRB->SRBListEntry.Blink = &SRB->SRBListEntry;
  3166. SRB->HwSRB.Status = STATUS_CANCELLED;
  3167. KeReleaseSpinLockFromDpcLevel(&DeviceExtension->SpinLock);
  3168. (SRB->Callback) (SRB);
  3169. goto exit;
  3170. }
  3171. VOID
  3172. StreamClassCancelOutstandingIrp(
  3173. IN PDEVICE_OBJECT DeviceObject,
  3174. IN PIRP Irp
  3175. )
  3176. /*++
  3177. Routine Description:
  3178. Cancel routine for IRP's outstanding in the minidriver
  3179. Arguments:
  3180. DeviceObject - pointer to the device object
  3181. Irp - pointer to IRP to be cancelled
  3182. Return Value:
  3183. None.
  3184. --*/
  3185. {
  3186. PDEVICE_EXTENSION DeviceExtension = DeviceObject->DeviceExtension;
  3187. KIRQL Irql,
  3188. CancelIrql;
  3189. DebugPrint((DebugLevelWarning, "'SCCancelOutstanding: trying to cancel Irp = %x\n",
  3190. Irp));
  3191. //
  3192. // acquire the device spinlock.
  3193. //
  3194. KeAcquireSpinLock(&DeviceExtension->SpinLock, &Irql);
  3195. CancelIrql = Irp->CancelIrql;
  3196. IoReleaseCancelSpinLock(Irql);
  3197. SCCancelOutstandingIrp(DeviceExtension, Irp);
  3198. KeReleaseSpinLockFromDpcLevel(&DeviceExtension->SpinLock);
  3199. //
  3200. // now call the DPC in case the request was successfully aborted.
  3201. //
  3202. StreamClassDpc(NULL,
  3203. DeviceExtension->DeviceObject,
  3204. NULL,
  3205. NULL);
  3206. KeLowerIrql(CancelIrql);
  3207. return;
  3208. }
  3209. VOID
  3210. StreamFlushIo(
  3211. IN PDEVICE_EXTENSION DeviceExtension,
  3212. IN PSTREAM_OBJECT StreamObject
  3213. )
  3214. /*++
  3215. Routine Description:
  3216. Cancel all IRP's on the specified stream.
  3217. Arguments:
  3218. Return Value:
  3219. STATUS_SUCCESS
  3220. --*/
  3221. {
  3222. PLIST_ENTRY IrpEntry;
  3223. KIRQL Irql;
  3224. PSTREAM_REQUEST_BLOCK SRB;
  3225. PIRP Irp;
  3226. //
  3227. // abort all I/O on the specified stream. first acquire the spinlock.
  3228. //
  3229. KeAcquireSpinLock(&DeviceExtension->SpinLock, &Irql);
  3230. //
  3231. // if there is I/O on our pending data queue, abort it.
  3232. //
  3233. while (!IsListEmpty(&StreamObject->DataPendingQueue)) {
  3234. //
  3235. // grab the IRP at the head of the queue and abort it.
  3236. //
  3237. IrpEntry = StreamObject->DataPendingQueue.Flink;
  3238. Irp = CONTAINING_RECORD(IrpEntry,
  3239. IRP,
  3240. Tail.Overlay.ListEntry);
  3241. //
  3242. // remove the IRP from our pending queue and call it back with
  3243. // cancelled status
  3244. //
  3245. RemoveEntryList(&Irp->Tail.Overlay.ListEntry);
  3246. //
  3247. // null out the cancel routine
  3248. //
  3249. IoSetCancelRoutine(Irp, NULL);
  3250. DebugPrint((DebugLevelTrace,
  3251. "'StreamFlush: Canceling Irp %x \n", Irp));
  3252. KeReleaseSpinLock(&DeviceExtension->SpinLock, Irql);
  3253. //
  3254. // The request cannot just be completed or all the resources
  3255. // associated with it will not be freed. Call it back.
  3256. //
  3257. SRB = (PSTREAM_REQUEST_BLOCK)(Irp->Tail.Overlay.DriverContext [0]);
  3258. ASSERT (SRB);
  3259. //
  3260. // hack - the completion handlers will try to remove the SRB from the
  3261. // outstanding queue. Point the SRB's queues to itself so this will not
  3262. // cause a problem.
  3263. //
  3264. SRB->SRBListEntry.Flink = &SRB->SRBListEntry;
  3265. SRB->SRBListEntry.Blink = &SRB->SRBListEntry;
  3266. SRB->HwSRB.Status = STATUS_CANCELLED;
  3267. SCCallBackSrb (SRB, DeviceExtension);
  3268. KeAcquireSpinLock(&DeviceExtension->SpinLock, &Irql);
  3269. }
  3270. //
  3271. // if there is I/O on our pending control queue, abort it.
  3272. //
  3273. while (!IsListEmpty(&StreamObject->ControlPendingQueue)) {
  3274. //
  3275. // grab the IRP at the head of the queue and abort it.
  3276. //
  3277. DEBUG_BREAKPOINT();
  3278. IrpEntry = StreamObject->ControlPendingQueue.Flink;
  3279. Irp = CONTAINING_RECORD(IrpEntry,
  3280. IRP,
  3281. Tail.Overlay.ListEntry);
  3282. //
  3283. // remove the IRP from our pending queue and call it back with
  3284. // cancelled status
  3285. //
  3286. RemoveEntryList(&Irp->Tail.Overlay.ListEntry);
  3287. //
  3288. // null out the cancel routine
  3289. //
  3290. IoSetCancelRoutine(Irp, NULL);
  3291. DebugPrint((DebugLevelTrace,
  3292. "'StreamFlush: Canceling Irp %x \n", Irp));
  3293. KeReleaseSpinLock(&DeviceExtension->SpinLock, Irql);
  3294. //
  3295. // The request cannot just be completed or all the resources
  3296. // associated with it will not be freed. Call it back.
  3297. //
  3298. SRB = (PSTREAM_REQUEST_BLOCK)(Irp->Tail.Overlay.DriverContext [0]);
  3299. ASSERT (SRB);
  3300. //
  3301. // hack - the completion handlers will try to remove the SRB from the
  3302. // outstanding queue. Point the SRB's queues to itself so this will not
  3303. // cause a problem.
  3304. //
  3305. SRB->SRBListEntry.Flink = &SRB->SRBListEntry;
  3306. SRB->SRBListEntry.Blink = &SRB->SRBListEntry;
  3307. SRB->HwSRB.Status = STATUS_CANCELLED;
  3308. SCCallBackSrb (SRB, DeviceExtension);
  3309. KeAcquireSpinLock(&DeviceExtension->SpinLock, &Irql);
  3310. }
  3311. //
  3312. // now cancel any irps for this stream on the outstanding queue.
  3313. // walk the outstanding queue trying to find an SRB for this stream.
  3314. //
  3315. IrpEntry = &DeviceExtension->OutstandingQueue;
  3316. while (IrpEntry->Flink != &DeviceExtension->OutstandingQueue) {
  3317. IrpEntry = IrpEntry->Flink;
  3318. //
  3319. // follow the link to the SRB
  3320. //
  3321. SRB = (PSTREAM_REQUEST_BLOCK) (CONTAINING_RECORD(IrpEntry,
  3322. STREAM_REQUEST_BLOCK,
  3323. SRBListEntry));
  3324. //
  3325. // if this SRB's stream object matches the one we're cancelling for,
  3326. // AND it has not been previously cancelled, AND the IRP itself has
  3327. // not been completed (non-null IRP field), abort this request.
  3328. //
  3329. if ((StreamObject == CONTAINING_RECORD(
  3330. SRB->HwSRB.StreamObject,
  3331. STREAM_OBJECT,
  3332. HwStreamObject)) &&
  3333. (SRB->HwSRB.Irp) &&
  3334. !(SRB->HwSRB.Irp->Cancel)) {
  3335. //
  3336. // The IRP has not been previously cancelled, so cancel it after
  3337. // releasing the spinlock to avoid deadlock with the cancel
  3338. // routine.
  3339. //
  3340. DebugPrint((DebugLevelTrace,
  3341. "'StreamFlush: Canceling Irp %x \n", SRB->HwSRB.Irp));
  3342. KeReleaseSpinLockFromDpcLevel(&DeviceExtension->SpinLock);
  3343. IoCancelIrp(SRB->HwSRB.Irp);
  3344. KeAcquireSpinLockAtDpcLevel(&DeviceExtension->SpinLock);
  3345. //
  3346. // restart at the top of the queue since we released the
  3347. // spinlock.
  3348. // we won't get in an endless loop since we set the cancel flag
  3349. // in the IRP.
  3350. //
  3351. IrpEntry = &DeviceExtension->OutstandingQueue;
  3352. } // if streamobjects match
  3353. } // while entries
  3354. //
  3355. // release the spinlock but remain at DPC level.
  3356. //
  3357. KeReleaseSpinLockFromDpcLevel(&DeviceExtension->SpinLock);
  3358. //
  3359. // now call the DPC in case the request was successfully aborted.
  3360. //
  3361. StreamClassDpc(NULL,
  3362. DeviceExtension->DeviceObject,
  3363. NULL,
  3364. NULL);
  3365. //
  3366. // lower IRQL
  3367. //
  3368. KeLowerIrql(Irql);
  3369. }
  3370. NTSTATUS
  3371. ClockDispatchCreate(
  3372. IN PDEVICE_OBJECT DeviceObject,
  3373. IN PIRP Irp
  3374. )
  3375. {
  3376. NTSTATUS Status;
  3377. PCLOCK_INSTANCE ClockInstance=NULL; //Prefixbug 17399
  3378. PIO_STACK_LOCATION IrpStack;
  3379. PKSCLOCK_CREATE ClockCreate;
  3380. PFILE_OBJECT ParentFileObject;
  3381. PSTREAM_OBJECT StreamObject=NULL; // prefixbug 17399
  3382. BOOLEAN RequestIssued=FALSE; // prefixbug 17398
  3383. PAGED_CODE();
  3384. IrpStack = IoGetCurrentIrpStackLocation(Irp);
  3385. //
  3386. // show one more I/O pending & verify that we can actually do I/O.
  3387. //
  3388. Status = SCShowIoPending(DeviceObject->DeviceExtension, Irp);
  3389. if ( !NT_SUCCESS ( Status )) {
  3390. //
  3391. // the device is currently not accessible, so just return with error
  3392. //
  3393. return (Status);
  3394. }
  3395. Status = KsValidateClockCreateRequest(Irp,
  3396. &ClockCreate);
  3397. ParentFileObject = IrpStack->FileObject->RelatedFileObject;
  3398. DebugPrint((DebugLevelTrace,
  3399. "'ClockCreate: Creating clock with Irp %x \n", Irp));
  3400. if (NT_SUCCESS(Status)) {
  3401. //
  3402. // allocate a clock instance for the clock
  3403. //
  3404. ClockInstance =
  3405. (PCLOCK_INSTANCE)
  3406. ExAllocatePool(NonPagedPool, sizeof(CLOCK_INSTANCE));
  3407. if (ClockInstance) {
  3408. //
  3409. // fill in the clock instance structure and reference it in the
  3410. // file
  3411. // object for the clock
  3412. //
  3413. ClockInstance->ParentFileObject = ParentFileObject;
  3414. #if 0
  3415. ClockInstance->ClockFileObject = IrpStack->FileObject;
  3416. DebugPrint((DebugLevelInfo,
  3417. "++++++++ClockInstance=%x, FileObject=%x\n",
  3418. ClockInstance,
  3419. ClockInstance->ClockFileObject));
  3420. #endif
  3421. KsAllocateObjectHeader(&ClockInstance->DeviceHeader,
  3422. SIZEOF_ARRAY(StreamDriverDispatch),
  3423. (PKSOBJECT_CREATE_ITEM) NULL,
  3424. Irp,
  3425. (PKSDISPATCH_TABLE) & ClockDispatchTable);
  3426. IrpStack->FileObject->FsContext = ClockInstance;
  3427. //
  3428. // set the 2nd context parameter so that we can identify this
  3429. // object as the clock object.
  3430. //
  3431. IrpStack->FileObject->FsContext2 = ClockInstance;
  3432. //
  3433. // call the minidriver to indicate that this stream is the master
  3434. // clock. pass the file object as a handle to the master clock.
  3435. //
  3436. StreamObject = (PSTREAM_OBJECT) ParentFileObject->FsContext;
  3437. StreamObject->ClockInstance = ClockInstance;
  3438. ClockInstance->StreamObject = StreamObject;
  3439. Status = SCSubmitRequest(SRB_OPEN_MASTER_CLOCK,
  3440. (HANDLE) IrpStack->FileObject,
  3441. 0,
  3442. SCOpenMasterCallback,
  3443. StreamObject->DeviceExtension,
  3444. StreamObject->FilterInstance->HwInstanceExtension,
  3445. &StreamObject->HwStreamObject,
  3446. Irp,
  3447. &RequestIssued,
  3448. &StreamObject->ControlPendingQueue,
  3449. (PVOID) StreamObject->HwStreamObject.
  3450. ReceiveControlPacket
  3451. );
  3452. } else { // if clockinstance
  3453. Status = STATUS_INSUFFICIENT_RESOURCES;
  3454. } // if clockinstance
  3455. } // if validate success
  3456. if (!RequestIssued) {
  3457. if ( NULL != StreamObject && NULL != StreamObject->ClockInstance ) {
  3458. ExFreePool(StreamObject->ClockInstance);
  3459. StreamObject->ClockInstance = NULL; // prefixbug 17399
  3460. }
  3461. SCCompleteIrp(Irp,
  3462. STATUS_INSUFFICIENT_RESOURCES,
  3463. DeviceObject->DeviceExtension);
  3464. }
  3465. return (Status);
  3466. }
  3467. NTSTATUS
  3468. AllocatorDispatchCreate(
  3469. IN PDEVICE_OBJECT DeviceObject,
  3470. IN PIRP Irp
  3471. )
  3472. /*++
  3473. Routine Description:
  3474. Processes the allocator create IRP. Currently just uses the default
  3475. allocator.
  3476. Arguments:
  3477. Return Value:
  3478. None.
  3479. --*/
  3480. {
  3481. PIO_STACK_LOCATION IrpStack;
  3482. PFILE_OBJECT ParentFileObject;
  3483. PSTREAM_OBJECT StreamObject;
  3484. NTSTATUS Status;
  3485. PAGED_CODE();
  3486. DebugPrint((DebugLevelTrace,"entering AllocatorDispatchCreate\n"));
  3487. IrpStack = IoGetCurrentIrpStackLocation(Irp);
  3488. ParentFileObject = IrpStack->FileObject->RelatedFileObject;
  3489. StreamObject = (PSTREAM_OBJECT) ParentFileObject->FsContext;
  3490. //
  3491. // show one more I/O pending & verify that we can actually do I/O.
  3492. //
  3493. Status = SCShowIoPending(DeviceObject->DeviceExtension, Irp);
  3494. if ( !NT_SUCCESS ( Status )) {
  3495. //
  3496. // the device is currently not accessible, so just return with error
  3497. //
  3498. DebugPrint((DebugLevelError,"exiting AllocatorDispatchCreate-REMOVED\n"));
  3499. return (Status);
  3500. }
  3501. //
  3502. // if allocator is not needed for this stream, just fail the call.
  3503. //
  3504. if (!StreamObject->HwStreamObject.Allocator) {
  3505. DebugPrint((DebugLevelTrace,"exiting AllocatorDispatchCreate-not implemented\n"));
  3506. return SCCompleteIrp(Irp,
  3507. STATUS_NOT_IMPLEMENTED,
  3508. DeviceObject->DeviceExtension);
  3509. }
  3510. DebugPrint((DebugLevelTrace,"exiting AllocatorDispatchCreate-complete\n"));
  3511. return SCCompleteIrp(Irp,
  3512. KsCreateDefaultAllocator(Irp),
  3513. DeviceObject->DeviceExtension);
  3514. }
  3515. NTSTATUS
  3516. SCOpenMasterCallback(
  3517. IN PSTREAM_REQUEST_BLOCK SRB
  3518. )
  3519. /*++
  3520. Routine Description:
  3521. Process the completion of a master clock open.
  3522. Arguments:
  3523. SRB - address of the completed SRB
  3524. Return Value:
  3525. None.
  3526. --*/
  3527. {
  3528. PSTREAM_OBJECT StreamObject = CONTAINING_RECORD(
  3529. SRB->HwSRB.StreamObject,
  3530. STREAM_OBJECT,
  3531. HwStreamObject
  3532. );
  3533. PIRP Irp = SRB->HwSRB.Irp;
  3534. PIO_STACK_LOCATION IrpStack = IoGetCurrentIrpStackLocation(Irp);
  3535. PAGED_CODE();
  3536. // log 'oMC ', StreamObject, DevExt, Status
  3537. SCLOG( ' CMo', StreamObject, StreamObject->DeviceExtension, SRB->HwSRB.Status);
  3538. if (!NT_SUCCESS(SRB->HwSRB.Status)) {
  3539. //
  3540. // if we could not set the master, free the clock handle and zero
  3541. // the link to the clock.
  3542. //
  3543. ExFreePool(StreamObject->ClockInstance);
  3544. StreamObject->ClockInstance = NULL;
  3545. } else { // if status success
  3546. //
  3547. // reference the pin handle so we won't be called to close the pin
  3548. // before the clock is closed
  3549. //
  3550. ObReferenceObject(IrpStack->FileObject->RelatedFileObject);
  3551. } // if status success
  3552. //
  3553. // complete the SRB
  3554. //
  3555. return (SCProcessCompletedRequest(SRB));
  3556. }
  3557. NTSTATUS
  3558. SCGetMasterClock(
  3559. IN PIRP Irp,
  3560. IN PKSPROPERTY Property,
  3561. IN OUT PHANDLE ClockHandle
  3562. )
  3563. {
  3564. //
  3565. // WorkWork - for now do nothing.
  3566. //
  3567. PAGED_CODE();
  3568. return (STATUS_NOT_SUPPORTED);
  3569. }
  3570. VOID
  3571. SciSetMasterClockInfo(
  3572. IN PSTREAM_OBJECT pStreamObject,
  3573. IN PMASTER_CLOCK_INFO pMasterClockInfo )
  3574. /*++
  3575. Decription:
  3576. This function simply set the new masterclock info for the stream
  3577. with LockUseMasterClock hold. Because of taking a spinlock we need
  3578. this function in lock memory. This function is intended to be called
  3579. by SCSetMasterClockOnly. pStreamObject is assumed valid.
  3580. Parameters:
  3581. pStreamObject: the target stream object to set to the new MasterCLockInfo
  3582. pMasterClockInfo: the new master clock info.
  3583. Return: None.
  3584. --*/
  3585. {
  3586. KIRQL SavedIrql;
  3587. KeAcquireSpinLock( &pStreamObject->LockUseMasterClock, &SavedIrql );
  3588. pStreamObject->MasterClockInfo = pMasterClockInfo;
  3589. KeReleaseSpinLock( &pStreamObject->LockUseMasterClock, SavedIrql );
  3590. return;
  3591. }
  3592. NTSTATUS
  3593. SCSetMasterClock(
  3594. IN PIRP Irp,
  3595. IN PKSPROPERTY Property,
  3596. IN PHANDLE ClockHandle
  3597. )
  3598. /*++
  3599. Description:
  3600. This is a Set property on a the stream. The request may be setting to
  3601. NULL CLockHandle which indicates master clock is revoked. If ClockHandle
  3602. is non-NULL, it is a new Master clock chosen by the graph manager.
  3603. Parameters:
  3604. Irp: the IO request packet to Set the master clock.
  3605. Property: the Set Master clock property
  3606. ClockHanlde: the handle of the clock designated as the new master clcok.
  3607. Return:
  3608. NTSTAUS: depending on the result of processing the request.
  3609. Comments:
  3610. This function must be called at IRQL < DISPATCH_LEVEL
  3611. --*/
  3612. {
  3613. NTSTATUS Status;
  3614. PIO_STACK_LOCATION IrpStack;
  3615. PSTREAM_OBJECT StreamObject;
  3616. KSPROPERTY FuncProperty;
  3617. PMASTER_CLOCK_INFO NewMasterClockInfo=NULL; //prefixbug 17396
  3618. PMASTER_CLOCK_INFO OldMasterClockInfo;
  3619. ULONG BytesReturned;
  3620. PFILE_OBJECT ClockFileObject = NULL;
  3621. BOOLEAN RequestIssued=FALSE;
  3622. PAGED_CODE();
  3623. IrpStack = IoGetCurrentIrpStackLocation(Irp);
  3624. StreamObject = (PSTREAM_OBJECT) IrpStack->FileObject->FsContext;
  3625. //
  3626. // This function can be called from multiple threads. We will serialize
  3627. // this function on the Stream to protect against concurrent accesses.
  3628. //
  3629. KeWaitForSingleObject(&StreamObject->ControlSetMasterClock,
  3630. Executive,
  3631. KernelMode,
  3632. FALSE,// not alertable
  3633. NULL);
  3634. //
  3635. // N.B.
  3636. //
  3637. // If our clock is open, we are potentially the master clock. But this
  3638. // is not guaranteed. Ksproxy opens our clock in attempt to use it as
  3639. // the master clock. But it can change its mind to choose another clock,
  3640. // while keeping our clock open.
  3641. //
  3642. //
  3643. // log 'sMC ', StreamObject, MasterClockInfo, *ClockHandle )
  3644. //
  3645. SCLOG( ' CMs', StreamObject, StreamObject->MasterClockInfo, *ClockHandle );
  3646. /*
  3647. Not so soon. We have not told mini drivers the new master clock yet.
  3648. Mini drivers might think they still have the retiring Master clock and
  3649. can query the clock in the mean time. We would crash on accessing NULL
  3650. MasterClockInfo. We should not nullify it before we notify the mini
  3651. driver first.
  3652. if (StreamObject->MasterClockInfo) {
  3653. ObDereferenceObject(StreamObject->MasterClockInfo->ClockFileObject);
  3654. ExFreePool(StreamObject->MasterClockInfo);
  3655. StreamObject->MasterClockInfo = NULL;
  3656. }
  3657. */
  3658. OldMasterClockInfo = StreamObject->MasterClockInfo;
  3659. //
  3660. // if there is a clock, reference it. If not, we'll send down a null handle.
  3661. //
  3662. if (*ClockHandle) {
  3663. //
  3664. // alloc a structure to represent the master clock
  3665. //
  3666. NewMasterClockInfo = ExAllocatePool(NonPagedPool, sizeof(MASTER_CLOCK_INFO));
  3667. if (!NewMasterClockInfo) {
  3668. Status = STATUS_INSUFFICIENT_RESOURCES;
  3669. goto exit;
  3670. }
  3671. //
  3672. // This is too early to assign. We have not setup MasterClockInfo yet.
  3673. //
  3674. // StreamObject->MasterClockInfo = MasterClockInfo;
  3675. //
  3676. // reference the clock handle, thereby getting the file object for it.
  3677. //
  3678. if (!NT_SUCCESS((Status = ObReferenceObjectByHandle(*ClockHandle,
  3679. FILE_READ_ACCESS | SYNCHRONIZE,
  3680. *IoFileObjectType,
  3681. Irp->RequestorMode,
  3682. &ClockFileObject,
  3683. NULL
  3684. )))) {
  3685. ExFreePool(NewMasterClockInfo);
  3686. NewMasterClockInfo = NULL;
  3687. goto exit;
  3688. } // if Ob succeeded
  3689. NewMasterClockInfo->ClockFileObject = ClockFileObject;
  3690. // check master clock
  3691. #if 0
  3692. {
  3693. if ( StreamObject->ClockInstance ) {
  3694. //
  3695. // we are chosen the master clock
  3696. //
  3697. DebugPrint((DebugLevelInfo,
  3698. "--------ClockInstance=%x, FileObject=%x "
  3699. "Indicated ClockFileObject=%x context=%x\n",
  3700. StreamObject->ClockInstance,
  3701. StreamObject->ClockInstance->ParentFileObject,
  3702. ClockFileObject,
  3703. ClockFileObject->FsContext));
  3704. }
  3705. else {
  3706. DebugPrint((DebugLevelInfo,
  3707. "--------Indicated ClockFileObject=%x context=%x\n",
  3708. ClockFileObject,
  3709. ClockFileObject->FsContext));
  3710. }
  3711. }
  3712. #endif
  3713. //
  3714. // issue the IOCtl to get the function table of the master clock.
  3715. //
  3716. FuncProperty.Id = KSPROPERTY_CLOCK_FUNCTIONTABLE;
  3717. FuncProperty.Flags = KSPROPERTY_TYPE_GET;
  3718. RtlMoveMemory(&FuncProperty.Set, &KSPROPSETID_Clock, sizeof(GUID));
  3719. if (!NT_SUCCESS((Status = KsSynchronousIoControlDevice(
  3720. ClockFileObject,
  3721. KernelMode,
  3722. IOCTL_KS_PROPERTY,
  3723. &FuncProperty,
  3724. sizeof(KSPROPERTY),
  3725. &NewMasterClockInfo->FunctionTable,
  3726. sizeof(KSCLOCK_FUNCTIONTABLE),
  3727. &BytesReturned)))) {
  3728. ObDereferenceObject(NewMasterClockInfo->ClockFileObject);
  3729. ExFreePool(NewMasterClockInfo);
  3730. NewMasterClockInfo = NULL;
  3731. goto exit;
  3732. }
  3733. } // if *ClockHandle
  3734. //
  3735. // call the minidriver to indicate the master clock.
  3736. //
  3737. if ( NULL != NewMasterClockInfo ) {
  3738. //
  3739. // but first, let's put in the MasterClockInfo. When mini driver
  3740. // gets notified with the masterclock, it could fire GetTime right away
  3741. // before the notification returns. Get ready to deal with it. This is
  3742. // critical if oldMasterClockInfo is NULL. Not much so otherwise.
  3743. //
  3744. //
  3745. // Make sure no one is querying master clock when setting the new clock info.
  3746. //
  3747. SciSetMasterClockInfo( StreamObject, NewMasterClockInfo );
  3748. }
  3749. Status = SCSubmitRequest(SRB_INDICATE_MASTER_CLOCK,
  3750. ClockFileObject,
  3751. 0,
  3752. SCDequeueAndDeleteSrb,
  3753. StreamObject->DeviceExtension,
  3754. StreamObject->FilterInstance->HwInstanceExtension,
  3755. &StreamObject->HwStreamObject,
  3756. Irp,
  3757. &RequestIssued,
  3758. &StreamObject->ControlPendingQueue,
  3759. (PVOID) StreamObject->HwStreamObject.
  3760. ReceiveControlPacket);
  3761. ASSERT( RequestIssued );
  3762. ASSERT( NT_SUCCESS( Status ) );
  3763. //
  3764. // SCSubmitRequest is a synch call. When we return here, We can finish our work
  3765. // based on the Status code.
  3766. //
  3767. if ( NT_SUCCESS( Status )) {
  3768. //
  3769. // Everything is cool. Finish up. The assignment is redundent if
  3770. // NewMasterClockInfo is not NULL. Better assign unconditionally than check.
  3771. //
  3772. //
  3773. // Make sure no one is querying master clock when updating MasterClockInfo
  3774. //
  3775. SciSetMasterClockInfo( StreamObject, NewMasterClockInfo );
  3776. if (NULL != OldMasterClockInfo) {
  3777. ObDereferenceObject(OldMasterClockInfo->ClockFileObject);
  3778. ExFreePool(OldMasterClockInfo);
  3779. }
  3780. } else {
  3781. //
  3782. // Failed to tell mini driver the new clock. Clean up shop. But don't update
  3783. // StreamObject->MasterClockInfo. Keep the status quo.
  3784. //
  3785. //
  3786. // Make sure no one is querying master clock when updateing MasterClockInfo.
  3787. //
  3788. SciSetMasterClockInfo( StreamObject, OldMasterClockInfo );
  3789. if (NewMasterClockInfo) {
  3790. ObDereferenceObject(ClockFileObject);
  3791. ExFreePool(NewMasterClockInfo);
  3792. }
  3793. }
  3794. exit:
  3795. KeSetEvent(&StreamObject->ControlSetMasterClock, IO_NO_INCREMENT, FALSE);
  3796. return (Status);
  3797. }
  3798. NTSTATUS
  3799. SCClockGetTime(
  3800. IN PIRP Irp,
  3801. IN PKSPROPERTY Property,
  3802. IN OUT PULONGLONG StreamTime
  3803. )
  3804. {
  3805. PIO_STACK_LOCATION IrpStack = IoGetCurrentIrpStackLocation(Irp);
  3806. PCLOCK_INSTANCE ClockInstance =
  3807. (PCLOCK_INSTANCE) IrpStack->FileObject->FsContext;
  3808. PSTREAM_OBJECT StreamObject = ClockInstance->ParentFileObject->FsContext;
  3809. PAGED_CODE();
  3810. if (StreamObject->HwStreamObject.HwClockObject.ClockSupportFlags &
  3811. CLOCK_SUPPORT_CAN_RETURN_STREAM_TIME) {
  3812. *StreamTime = SCGetStreamTime(IrpStack->FileObject);
  3813. Irp->IoStatus.Information = sizeof(ULONGLONG);
  3814. return STATUS_SUCCESS;
  3815. } else {
  3816. return (STATUS_NOT_SUPPORTED);
  3817. }
  3818. }
  3819. NTSTATUS
  3820. SCClockGetPhysicalTime(
  3821. IN PIRP Irp,
  3822. IN PKSPROPERTY Property,
  3823. IN OUT PULONGLONG PhysicalTime
  3824. )
  3825. {
  3826. PIO_STACK_LOCATION IrpStack = IoGetCurrentIrpStackLocation(Irp);
  3827. PCLOCK_INSTANCE ClockInstance =
  3828. (PCLOCK_INSTANCE) IrpStack->FileObject->FsContext;
  3829. PSTREAM_OBJECT StreamObject = ClockInstance->ParentFileObject->FsContext;
  3830. PAGED_CODE();
  3831. if (StreamObject->HwStreamObject.HwClockObject.ClockSupportFlags &
  3832. CLOCK_SUPPORT_CAN_READ_ONBOARD_CLOCK) {
  3833. *PhysicalTime = SCGetPhysicalTime(IrpStack->FileObject->FsContext);
  3834. Irp->IoStatus.Information = sizeof(ULONGLONG);
  3835. return (STATUS_SUCCESS);
  3836. } else {
  3837. return (STATUS_NOT_SUPPORTED);
  3838. }
  3839. }
  3840. NTSTATUS
  3841. SCClockGetSynchronizedTime(
  3842. IN PIRP Irp,
  3843. IN PKSPROPERTY Property,
  3844. IN OUT PKSCORRELATED_TIME SyncTime
  3845. )
  3846. {
  3847. PIO_STACK_LOCATION IrpStack = IoGetCurrentIrpStackLocation(Irp);
  3848. PCLOCK_INSTANCE ClockInstance =
  3849. (PCLOCK_INSTANCE) IrpStack->FileObject->FsContext;
  3850. PSTREAM_OBJECT StreamObject = ClockInstance->ParentFileObject->FsContext;
  3851. PAGED_CODE();
  3852. if (StreamObject->HwStreamObject.HwClockObject.ClockSupportFlags &
  3853. CLOCK_SUPPORT_CAN_RETURN_STREAM_TIME) {
  3854. SyncTime->Time = SCGetSynchronizedTime(IrpStack->FileObject,
  3855. &SyncTime->SystemTime);
  3856. Irp->IoStatus.Information = sizeof(KSCORRELATED_TIME);
  3857. return (STATUS_SUCCESS);
  3858. } else {
  3859. return (STATUS_NOT_SUPPORTED);
  3860. }
  3861. }
  3862. NTSTATUS
  3863. SCClockGetFunctionTable(
  3864. IN PIRP Irp,
  3865. IN PKSPROPERTY Property,
  3866. IN OUT PKSCLOCK_FUNCTIONTABLE FunctionTable
  3867. )
  3868. {
  3869. PCLOCK_INSTANCE ClockInstance;
  3870. PIO_STACK_LOCATION IrpStack;
  3871. PSTREAM_OBJECT StreamObject;
  3872. PAGED_CODE();
  3873. IrpStack = IoGetCurrentIrpStackLocation(Irp);
  3874. ClockInstance = (PCLOCK_INSTANCE) IrpStack->FileObject->FsContext;
  3875. StreamObject = ClockInstance->ParentFileObject->FsContext;
  3876. RtlZeroMemory(FunctionTable, sizeof(KSCLOCK_FUNCTIONTABLE));
  3877. if (StreamObject->HwStreamObject.HwClockObject.ClockSupportFlags &
  3878. CLOCK_SUPPORT_CAN_RETURN_STREAM_TIME) {
  3879. FunctionTable->GetTime = (PFNKSCLOCK_GETTIME) SCGetStreamTime;
  3880. FunctionTable->GetCorrelatedTime = (PFNKSCLOCK_CORRELATEDTIME) SCGetSynchronizedTime;
  3881. }
  3882. if (StreamObject->HwStreamObject.HwClockObject.ClockSupportFlags &
  3883. CLOCK_SUPPORT_CAN_READ_ONBOARD_CLOCK) {
  3884. FunctionTable->GetPhysicalTime = (PFNKSCLOCK_GETTIME) SCGetPhysicalTime;
  3885. }
  3886. Irp->IoStatus.Information = sizeof(KSCLOCK_FUNCTIONTABLE);
  3887. return STATUS_SUCCESS;
  3888. }
  3889. NTSTATUS
  3890. ClockDispatchClose
  3891. (
  3892. IN PDEVICE_OBJECT DeviceObject,
  3893. IN PIRP Irp
  3894. )
  3895. /*++
  3896. Routine Description:
  3897. This routine receives CLOSE IRP's for a stream
  3898. Arguments:
  3899. DeviceObject - device object for the device
  3900. Irp - probably an IRP, silly
  3901. Return Value:
  3902. The IRP status is set as appropriate
  3903. --*/
  3904. {
  3905. PIO_STACK_LOCATION IrpStack = IoGetCurrentIrpStackLocation(Irp);
  3906. PDEVICE_EXTENSION DeviceExtension =
  3907. (PDEVICE_EXTENSION) DeviceObject->DeviceExtension;
  3908. NTSTATUS Status;
  3909. BOOLEAN RequestIssued;
  3910. PCLOCK_INSTANCE ClockInstance = (PCLOCK_INSTANCE)
  3911. IrpStack->FileObject->FsContext;
  3912. PSTREAM_OBJECT StreamObject = ClockInstance->StreamObject;
  3913. PAGED_CODE();
  3914. InterlockedIncrement(&DeviceExtension->OneBasedIoCount);
  3915. //
  3916. // call the minidriver to indicate that there is no master clock.
  3917. // processing will continue when the callback procedure is called.
  3918. //
  3919. Status = SCSubmitRequest(SRB_CLOSE_MASTER_CLOCK,
  3920. NULL,
  3921. 0,
  3922. SCCloseClockCallback,
  3923. DeviceExtension,
  3924. StreamObject->FilterInstance->HwInstanceExtension,
  3925. &StreamObject->HwStreamObject,
  3926. Irp,
  3927. &RequestIssued,
  3928. &StreamObject->ControlPendingQueue,
  3929. (PVOID) StreamObject->HwStreamObject.
  3930. ReceiveControlPacket
  3931. );
  3932. if (!RequestIssued) {
  3933. DEBUG_BREAKPOINT();
  3934. SCCompleteIrp(Irp, Status, DeviceExtension);
  3935. }
  3936. ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
  3937. return (Status);
  3938. }
  3939. NTSTATUS
  3940. SCCloseClockCallback(
  3941. IN PSTREAM_REQUEST_BLOCK SRB
  3942. )
  3943. /*++
  3944. Routine Description:
  3945. Process the completion of a stream close.
  3946. Arguments:
  3947. SRB - address of the completed SRB
  3948. Return Value:
  3949. None.
  3950. --*/
  3951. {
  3952. PDEVICE_EXTENSION DeviceExtension =
  3953. (PDEVICE_EXTENSION) SRB->HwSRB.HwDeviceExtension - 1;
  3954. PSTREAM_OBJECT StreamObject = CONTAINING_RECORD(
  3955. SRB->HwSRB.StreamObject,
  3956. STREAM_OBJECT,
  3957. HwStreamObject
  3958. );
  3959. PIRP Irp = SRB->HwSRB.Irp;
  3960. PIO_STACK_LOCATION IrpStack = IoGetCurrentIrpStackLocation(Irp);
  3961. NTSTATUS Status = SRB->HwSRB.Status;
  3962. PCLOCK_INSTANCE ClockInstance;
  3963. PAGED_CODE();
  3964. // log 'cMC ', StreamObject, ClockInstance, Status )
  3965. SCLOG( ' CMc', StreamObject, IrpStack->FileObject->FsContext, Status );
  3966. if (NT_SUCCESS(Status)) {
  3967. //
  3968. // free the clock instance structure and the object header
  3969. //
  3970. ClockInstance =
  3971. (PCLOCK_INSTANCE) IrpStack->FileObject->FsContext;
  3972. KsFreeObjectHeader(ClockInstance->DeviceHeader);
  3973. ExFreePool(ClockInstance);
  3974. StreamObject->ClockInstance = NULL;
  3975. //
  3976. // dereference the pin handle
  3977. //
  3978. ObDereferenceObject(IrpStack->FileObject->RelatedFileObject);
  3979. } // if good status
  3980. SCProcessCompletedRequest(SRB);
  3981. return (Status);
  3982. }
  3983. NTSTATUS
  3984. SCFilterTopologyHandler(
  3985. IN PIRP Irp,
  3986. IN PKSPROPERTY Property,
  3987. IN OUT PVOID Data)
  3988. /*++
  3989. Routine Description:
  3990. Dispatches a pin property request
  3991. Arguments:
  3992. Irp - pointer to the irp
  3993. Property - pointer to the property info
  3994. Data - property specific buffer
  3995. Return Value:
  3996. NTSTATUS returned as appropriate
  3997. --*/
  3998. {
  3999. PIO_STACK_LOCATION IrpStack = IoGetCurrentIrpStackLocation(Irp);
  4000. PDEVICE_EXTENSION DeviceExtension = (PDEVICE_EXTENSION) IrpStack->
  4001. DeviceObject->DeviceExtension;
  4002. PAGED_CODE();
  4003. IFN_MF(
  4004. return KsTopologyPropertyHandler(Irp,
  4005. Property,
  4006. Data,
  4007. DeviceExtension->StreamDescriptor->StreamHeader.Topology
  4008. );
  4009. )
  4010. IF_MFS(
  4011. PFILTER_INSTANCE FilterInstance;
  4012. FilterInstance = (PFILTER_INSTANCE) IrpStack->FileObject->FsContext;
  4013. return KsTopologyPropertyHandler(
  4014. Irp,
  4015. Property,
  4016. Data,
  4017. FilterInstance->StreamDescriptor->StreamHeader.Topology);
  4018. )
  4019. }
  4020. NTSTATUS
  4021. SCFilterPinIntersectionHandler(
  4022. IN PIRP Irp,
  4023. IN PKSP_PIN Pin,
  4024. OUT PVOID Data
  4025. )
  4026. /*++
  4027. Routine Description:
  4028. Handles the KSPROPERTY_PIN_DATAINTERSECTION property in the Pin property set.
  4029. Returns the first acceptable data format given a list of data ranges for a specified
  4030. Pin factory. Actually just calls the Intersection Enumeration helper, which then
  4031. calls the IntersectHandler callback with each data range.
  4032. Arguments:
  4033. Irp -
  4034. Device control Irp.
  4035. Pin -
  4036. Specific property request followed by Pin factory identifier, followed by a
  4037. KSMULTIPLE_ITEM structure. This is followed by zero or more data range structures.
  4038. Data -
  4039. The place in which to return the data format selected as the first intersection
  4040. between the list of data ranges passed, and the acceptable formats.
  4041. Return Values:
  4042. returns STATUS_SUCCESS or STATUS_NO_MATCH, else STATUS_INVALID_PARAMETER,
  4043. STATUS_BUFFER_TOO_SMALL, or STATUS_INVALID_BUFFER_SIZE.
  4044. --*/
  4045. {
  4046. PIO_STACK_LOCATION IrpStack = IoGetCurrentIrpStackLocation(Irp);
  4047. PDEVICE_EXTENSION DeviceExtension = (PDEVICE_EXTENSION) IrpStack->
  4048. DeviceObject->DeviceExtension;
  4049. PAGED_CODE();
  4050. IFN_MF(
  4051. return KsPinDataIntersection(
  4052. Irp,
  4053. Pin,
  4054. Data,
  4055. DeviceExtension->NumberOfPins,
  4056. DeviceExtension->PinInformation,
  4057. SCIntersectHandler);
  4058. )
  4059. IF_MFS(
  4060. PSTREAM_OBJECT StreamObject;
  4061. PFILTER_INSTANCE FilterInstance;
  4062. FilterInstance = (PFILTER_INSTANCE) IrpStack->FileObject->FsContext;
  4063. DebugPrint((DebugLevelVerbose,
  4064. "PinIntersection FilterInstance=%p\n", FilterInstance ));
  4065. return KsPinDataIntersection(
  4066. Irp,
  4067. Pin,
  4068. Data,
  4069. FilterInstance->NumberOfPins,
  4070. FilterInstance->PinInformation,
  4071. SCIntersectHandler);
  4072. )
  4073. }
  4074. NTSTATUS
  4075. SCIntersectHandler(
  4076. IN PIRP Irp,
  4077. IN PKSP_PIN Pin,
  4078. IN PKSDATARANGE DataRange,
  4079. OUT PVOID Data
  4080. )
  4081. /*++
  4082. Routine Description:
  4083. This is the data range callback for KsPinDataIntersection, which is called by
  4084. FilterPinIntersection to enumerate the given list of data ranges, looking for
  4085. an acceptable match. If a data range is acceptable, a data format is copied
  4086. into the return buffer. If there is a wave format selected in a current pin
  4087. connection, and it is contained within the data range passed in, it is chosen
  4088. as the data format to return. A STATUS_NO_MATCH continues the enumeration.
  4089. Arguments:
  4090. Irp -
  4091. Device control Irp.
  4092. Pin -
  4093. Specific property request followed by Pin factory identifier, followed by a
  4094. KSMULTIPLE_ITEM structure. This is followed by zero or more data range structures.
  4095. This enumeration callback does not need to look at any of this though. It need
  4096. only look at the specific pin identifier.
  4097. DataRange -
  4098. Contains a specific data range to validate.
  4099. Data -
  4100. The place in which to return the data format selected as the first intersection
  4101. between the list of data ranges passed, and the acceptable formats.
  4102. Return Values:
  4103. returns STATUS_SUCCESS or STATUS_NO_MATCH, else STATUS_INVALID_PARAMETER,
  4104. STATUS_BUFFER_TOO_SMALL, or STATUS_INVALID_BUFFER_SIZE.
  4105. --*/
  4106. {
  4107. PIO_STACK_LOCATION IrpStack;
  4108. NTSTATUS Status;
  4109. PFILTER_INSTANCE FilterInstance;
  4110. STREAM_DATA_INTERSECT_INFO IntersectInfo;
  4111. PDEVICE_EXTENSION DeviceExtension;
  4112. BOOLEAN RequestIssued;
  4113. PAGED_CODE();
  4114. IrpStack = IoGetCurrentIrpStackLocation(Irp);
  4115. FilterInstance = (PFILTER_INSTANCE) IrpStack->FileObject->FsContext;
  4116. DeviceExtension = (PDEVICE_EXTENSION)
  4117. IrpStack->DeviceObject->DeviceExtension;
  4118. ASSERT_FILTER_INSTANCE( FilterInstance );
  4119. ASSERT_DEVICE_EXTENSION( DeviceExtension );
  4120. //
  4121. // fill in the intersect info struct from the input params.
  4122. //
  4123. IntersectInfo.DataRange = DataRange;
  4124. IntersectInfo.DataFormatBuffer = Data;
  4125. IntersectInfo.SizeOfDataFormatBuffer =
  4126. IrpStack->Parameters.DeviceIoControl.OutputBufferLength;
  4127. IntersectInfo.StreamNumber = Pin->PinId;
  4128. //
  4129. // call the minidriver to process the intersection. processing will
  4130. // continue
  4131. // when the callback procedure is called. take the event to ensure that
  4132. // pins don't come and go as we process the intersection.
  4133. //
  4134. KeWaitForSingleObject(&DeviceExtension->ControlEvent,
  4135. Executive,
  4136. KernelMode,
  4137. FALSE,// not alertable
  4138. NULL);
  4139. Status = SCSubmitRequest(SRB_GET_DATA_INTERSECTION,
  4140. &IntersectInfo,
  4141. 0,
  4142. SCDataIntersectionCallback,
  4143. DeviceExtension,
  4144. FilterInstance->HwInstanceExtension,
  4145. NULL,
  4146. Irp,
  4147. &RequestIssued,
  4148. &DeviceExtension->PendingQueue,
  4149. (PVOID) DeviceExtension->
  4150. MinidriverData->HwInitData.
  4151. HwReceivePacket);
  4152. if (!RequestIssued) {
  4153. DEBUG_BREAKPOINT();
  4154. KeSetEvent(&DeviceExtension->ControlEvent, IO_NO_INCREMENT, FALSE);
  4155. }
  4156. return Status;
  4157. }
  4158. NTSTATUS
  4159. SCDataIntersectionCallback(
  4160. IN PSTREAM_REQUEST_BLOCK SRB
  4161. )
  4162. /*++
  4163. Routine Description:
  4164. Process the completion of a data intersection query.
  4165. Arguments:
  4166. SRB - address of the completed SRB
  4167. Return Value:
  4168. None.
  4169. --*/
  4170. {
  4171. PDEVICE_EXTENSION DeviceExtension =
  4172. (PDEVICE_EXTENSION) SRB->HwSRB.HwDeviceExtension - 1;
  4173. PIRP Irp = SRB->HwSRB.Irp;
  4174. NTSTATUS Status = SRB->HwSRB.Status;
  4175. PAGED_CODE();
  4176. Irp->IoStatus.Information = SRB->HwSRB.ActualBytesTransferred;
  4177. //
  4178. // signal the event
  4179. //
  4180. KeSetEvent(&DeviceExtension->ControlEvent, IO_NO_INCREMENT, FALSE);
  4181. SCDequeueAndDeleteSrb(SRB);
  4182. return (Status);
  4183. }
  4184. NTSTATUS
  4185. SCGetStreamHeaderSize(
  4186. IN PIRP Irp,
  4187. IN PKSPROPERTY Property,
  4188. IN OUT PULONG StreamHeaderSize
  4189. )
  4190. /*++
  4191. Routine Description:
  4192. Process the get stream header extension property
  4193. Arguments:
  4194. Return Value:
  4195. None.
  4196. --*/
  4197. {
  4198. PIO_STACK_LOCATION IrpStack = IoGetCurrentIrpStackLocation(Irp);
  4199. PSTREAM_OBJECT StreamObject = (PSTREAM_OBJECT) IrpStack->FileObject->FsContext;
  4200. PAGED_CODE();
  4201. ASSERT(StreamObject);
  4202. *StreamHeaderSize = StreamObject->HwStreamObject.StreamHeaderMediaSpecific;
  4203. Irp->IoStatus.Information = sizeof(ULONG);
  4204. return (STATUS_SUCCESS);
  4205. }
  4206. NTSTATUS
  4207. DllUnload(
  4208. VOID
  4209. )
  4210. {
  4211. NTSTATUS Status=STATUS_SUCCESS;
  4212. #if DBG
  4213. NTSTATUS DbgDllUnload();
  4214. DebugPrint((1, "Stream Class DllUnload: Unloading\n"));
  4215. Status = DbgDllUnload();
  4216. #endif
  4217. return Status;
  4218. }
  4219. #ifdef ENABLE_STREAM_CLASS_AS_ALLOCATOR
  4220. //---------------------------------------------------------------------------
  4221. //---------------------------------------------------------------------------
  4222. NTSTATUS
  4223. SCStreamAllocator(
  4224. IN PIRP Irp,
  4225. IN PKSPROPERTY Property,
  4226. IN OUT PHANDLE AllocatorHandle
  4227. )
  4228. /*++
  4229. Routine Description:
  4230. If KSPROPERTY_TYPE_SET, this function sets the stream allocator
  4231. for this connection by referencing the file handle to obtain
  4232. the file object pointer and stores this pointer in the filter(stream?)
  4233. instance structure.
  4234. Otherwise, a KSPROPERTY_TYPE_GET request returns a NULL handle
  4235. and STATUS_SUCCESS to show that we support allocator creation.
  4236. Arguments:
  4237. IN PIRP Irp -
  4238. pointer to the I/O request packet
  4239. IN PKSPROPERTY Property -
  4240. pointer to the property structure
  4241. IN OUT PHANDLE AllocatorHandle -
  4242. pointer to the handle representing the file object
  4243. Return:
  4244. STATUS_SUCCESS or an appropriate error code
  4245. --*/
  4246. {
  4247. NTSTATUS Status;
  4248. PIO_STACK_LOCATION IrpStack;
  4249. PSTREAM_OBJECT StreamObject;
  4250. PDEVICE_EXTENSION DeviceExtension;
  4251. IrpStack = IoGetCurrentIrpStackLocation( Irp );
  4252. StreamObject = IrpStack->FileObject->FsContext;
  4253. DebugPrint((DebugLevelTrace, "STREAM:entering SCStreamAllocator:Stream:%x\n",StreamObject));
  4254. if (Property->Flags & KSPROPERTY_TYPE_GET) {
  4255. //
  4256. // This is a query to see if we support the creation of
  4257. // allocators. The returned handle is always NULL, but we
  4258. // signal that we support the creation of allocators by
  4259. // returning STATUS_SUCCESS.
  4260. //
  4261. *AllocatorHandle = NULL;
  4262. Status = STATUS_SUCCESS;
  4263. DebugPrint((DebugLevelTrace,"SCStreamAllocator-GET"));
  4264. } else {
  4265. PFILTER_INSTANCE FilterInstance;
  4266. FilterInstance =
  4267. (PFILTER_INSTANCE) StreamObject->FilterFileObject->FsContext;
  4268. DeviceExtension = StreamObject->DeviceExtension;
  4269. DebugPrint((DebugLevelTrace,"SCStreamAllocator-SET"));
  4270. KeWaitForSingleObject(&DeviceExtension->ControlEvent,
  4271. Executive,
  4272. KernelMode,
  4273. FALSE,// not alertable
  4274. NULL);
  4275. //
  4276. // The allocator can only be specified when the device is
  4277. // in KSSTATE_STOP.
  4278. //
  4279. if (StreamObject->CurrentState != KSSTATE_STOP) {
  4280. KeSetEvent(&DeviceExtension->ControlEvent, IO_NO_INCREMENT, FALSE);
  4281. DebugPrint((DebugLevelTrace,"SCStreamAllocator-device not in STOP"));
  4282. return STATUS_INVALID_DEVICE_STATE;
  4283. }
  4284. // if we are in _STOP, the flush was already done.
  4285. // this call may have to be enabled.
  4286. //
  4287. // StreamFlushIo(DeviceExtension, StreamObject);
  4288. //
  4289. // Release the previous allocator, if any.
  4290. //
  4291. if (StreamObject->AllocatorFileObject) {
  4292. ObDereferenceObject( StreamObject->AllocatorFileObject );
  4293. StreamObject->AllocatorFileObject = NULL;
  4294. }
  4295. //
  4296. // Reference this handle and store the resultant pointer
  4297. // in the filter instance. Note that the default allocator
  4298. // does not ObReferenceObject() for its parent
  4299. // (which would be the pin handle). If it did reference
  4300. // the pin handle, we could never close this pin as there
  4301. // would always be a reference to the pin file object held
  4302. // by the allocator and the pin object has a reference to the
  4303. // allocator file object.
  4304. //
  4305. if (*AllocatorHandle != NULL) {
  4306. Status =
  4307. ObReferenceObjectByHandle(
  4308. *AllocatorHandle,
  4309. FILE_READ_DATA | SYNCHRONIZE,
  4310. *IoFileObjectType,
  4311. ExGetPreviousMode(),
  4312. &StreamObject->AllocatorFileObject,
  4313. NULL );
  4314. DebugPrint((DebugLevelTrace, "SCStreamAllocator: got %x as Allocator file object\n",StreamObject->AllocatorFileObject));
  4315. } else {
  4316. Status = STATUS_SUCCESS;
  4317. }
  4318. KeSetEvent(&DeviceExtension->ControlEvent, IO_NO_INCREMENT, FALSE);
  4319. }
  4320. DebugPrint((DebugLevelTrace,"exiting SCStreamAllocator-normal path\n"));
  4321. return Status;
  4322. }
  4323. //---------------------------------------------------------------------------
  4324. BOOLEAN
  4325. HeaderTransfer(
  4326. IN PFILTER_INSTANCE FilterInstance,
  4327. IN PSTREAM_OBJECT StreamObject,
  4328. IN PFILE_OBJECT DestinationFileObject,
  4329. IN OUT PSTREAM_HEADER_EX *StreamHeader
  4330. )
  4331. /*++
  4332. Routine Description:
  4333. Sets up the stream header for a no-copy transfer to the
  4334. opposite pin.
  4335. Arguments:
  4336. IN PFILTER_INSTANCE FilterInstance -
  4337. pointer to the filter instance
  4338. IN PSTREAM_OBJECT StreamObject -
  4339. pointer to the transform instance structure
  4340. IN PSTREAM_OBJECT DestinationInstance -
  4341. pointer to the opposite transform instance structure
  4342. IN OUT PSTREAM_HEADER_EX *StreamHeader -
  4343. pointer containing a pointer to the current stream header,
  4344. this member is updated with a pointer to the next stream
  4345. header to submit to the opposite pin or NULL if there is
  4346. no header to submit.
  4347. Return:
  4348. An indication of whether stop can proceed now or not
  4349. Comments:
  4350. Not pageable, uses SpinLocks.
  4351. --*/
  4352. {
  4353. KIRQL irqlQueue, irqlFree;
  4354. ULONG WhichQueue = (*StreamHeader)->WhichQueue;
  4355. ULONG OppositeQueue = WhichQueue ^ 0x00000001; // 1 to 0, 0 to 1
  4356. BOOLEAN SignalStop = FALSE;
  4357. ASSERT(DestinationFileObject);
  4358. ASSERT( KeGetCurrentIrql() == PASSIVE_LEVEL );
  4359. if (StreamObject->PinState > PinStopPending) { // if normal running case
  4360. //
  4361. // If we are here after submitting an ENDOFSTREAM Irp to the
  4362. // outflow pin, then we have already read the end of stream
  4363. // from the input and there is no need to continue I/O.
  4364. //
  4365. if (DestinationFileObject) {
  4366. ULONG HeaderFlags = (*StreamHeader)->Header.OptionsFlags;
  4367. //
  4368. // Clear the options flags so that we continue
  4369. // reading from where we left off.
  4370. //
  4371. // (*StreamHeader)->Header.OptionsFlags = 0;
  4372. //
  4373. // Reset the stream segment valid data length
  4374. //
  4375. // (*StreamHeader)->Header.DataUsed = 0;
  4376. // (*StreamHeader)->Header.Duration = 0;
  4377. //
  4378. // Check for the end of the stream.
  4379. //
  4380. if ((HeaderFlags & KSSTREAM_HEADER_OPTIONSF_ENDOFSTREAM) ||
  4381. StreamObject->EndOfStream) {
  4382. DebugPrint((DebugLevelTrace,
  4383. "end of stream") );
  4384. //
  4385. // Make sure that this is set for the next time through.
  4386. //
  4387. StreamObject->EndOfStream = TRUE;
  4388. if (!(*StreamHeader)->ReferenceCount) {
  4389. //
  4390. // Put the header back on the free list of the inflow pin.
  4391. //
  4392. KeAcquireSpinLock( &StreamObject->FreeQueueLock, &irqlFree );
  4393. #if (DBG)
  4394. if ((*StreamHeader)->OnFreeList) {
  4395. DebugPrint((DebugLevelTrace,
  4396. "stream header already on free list.") );
  4397. }
  4398. #endif
  4399. DebugPrint((DebugLevelTrace,
  4400. "EOS adding %x to free queue", *StreamHeader) );
  4401. InsertTailList(
  4402. &StreamObject->FreeQueue,
  4403. &(*StreamHeader)->ListEntry );
  4404. if (!InterlockedDecrement (
  4405. &StreamObject -> QueuedFramesPlusOne
  4406. ))
  4407. SignalStop = TRUE;
  4408. #if (DBG)
  4409. (*StreamHeader)->OnFreeList = TRUE;
  4410. if ((*StreamHeader)->OnActiveList) {
  4411. DebugPrint((DebugLevelTrace,
  4412. "stream header on both lists.") );
  4413. }
  4414. #endif
  4415. KeReleaseSpinLock( &StreamObject->FreeQueueLock, irqlFree );
  4416. }
  4417. //
  4418. // No more I/O to opposite pin.
  4419. //
  4420. *StreamHeader = NULL;
  4421. }
  4422. }
  4423. //
  4424. // Grab the spin lock for the other queue, insert this
  4425. // stream header on the queue.
  4426. //
  4427. if (*StreamHeader) {
  4428. KeAcquireSpinLock( &StreamObject->Queues[OppositeQueue].QueueLock, &irqlQueue );
  4429. #if (DBG)
  4430. if ((*StreamHeader)->OnActiveList) {
  4431. DebugPrint((DebugLevelTrace,
  4432. "stream header already on active list.") );
  4433. }
  4434. #endif
  4435. InsertTailList(
  4436. &StreamObject->Queues[OppositeQueue].ActiveQueue,
  4437. &(*StreamHeader)->ListEntry );
  4438. #if (DBG)
  4439. (*StreamHeader)->OnActiveList = TRUE;
  4440. if ((*StreamHeader)->OnFreeList) {
  4441. DebugPrint((DebugLevelTrace,
  4442. "stream header on both lists.") );
  4443. }
  4444. #endif
  4445. KeReleaseSpinLock( &StreamObject->Queues[OppositeQueue].QueueLock, irqlQueue );
  4446. }
  4447. }
  4448. else // pin stop IS pending
  4449. {
  4450. //
  4451. // Location of frames (for this type of transfer, all frames
  4452. // are held on the source pin).
  4453. //
  4454. if (!(*StreamHeader)->ReferenceCount) {
  4455. DebugPrint((DebugLevelTrace,
  4456. "stop: adding %x to free queue.", *StreamHeader) );
  4457. KeAcquireSpinLock( &StreamObject->FreeQueueLock, &irqlFree );
  4458. #if (DBG)
  4459. if ((*StreamHeader)->OnFreeList) {
  4460. DebugPrint((DebugLevelTrace,
  4461. "stream header already on free list.") );
  4462. }
  4463. #endif
  4464. InsertTailList(
  4465. &StreamObject->FreeQueue, &(*StreamHeader)->ListEntry );
  4466. if (!InterlockedDecrement (&StreamObject -> QueuedFramesPlusOne))
  4467. SignalStop = TRUE;
  4468. #if (DBG)
  4469. (*StreamHeader)->OnFreeList = TRUE;
  4470. if ((*StreamHeader)->OnActiveList) {
  4471. DebugPrint((DebugLevelTrace,
  4472. "stream header on both lists.") );
  4473. }
  4474. #endif
  4475. KeReleaseSpinLock( &StreamObject->FreeQueueLock, irqlFree );
  4476. }
  4477. //
  4478. // No I/O to opposite pin this round.
  4479. //
  4480. *StreamHeader = NULL;
  4481. }
  4482. return SignalStop;
  4483. }
  4484. //---------------------------------------------------------------------------
  4485. VOID
  4486. IoWorker(
  4487. PVOID Context,
  4488. ULONG WhichQueue
  4489. )
  4490. /*++
  4491. Routine Description:
  4492. This is the work item for the source pins. Walks the queue
  4493. associated with the stream header looking for sequentially
  4494. completed headers and submits those headers to the opposite
  4495. pin.
  4496. Arguments:
  4497. PVOID Context -
  4498. pointer to the stream header
  4499. Return:
  4500. Nothing.
  4501. Comments:
  4502. Not pageable, uses SpinLocks.
  4503. --*/
  4504. {
  4505. KIRQL irqlOld;
  4506. PFILTER_INSTANCE FilterInstance;
  4507. PADDITIONAL_PIN_INFO AdditionalInfo;
  4508. PLIST_ENTRY Node;
  4509. PSTREAM_OBJECT StreamObject;
  4510. PFILE_OBJECT DestinationFileObject;
  4511. PSTREAM_HEADER_EX StreamHeader;
  4512. NTSTATUS Status;
  4513. ULONG Operation;
  4514. PDEVICE_EXTENSION DeviceExtension;
  4515. BOOLEAN SignalStop = FALSE;
  4516. ASSERT( KeGetCurrentIrql() == PASSIVE_LEVEL );
  4517. StreamObject = (PSTREAM_OBJECT) Context;
  4518. DeviceExtension = StreamObject->DeviceExtension;
  4519. #if (DBG)
  4520. DebugPrint((DebugLevelTrace,
  4521. "entering IoWorker:Source StreamObject:%x\n",StreamObject));
  4522. #endif
  4523. FilterInstance =
  4524. (PFILTER_INSTANCE)
  4525. StreamObject->FilterFileObject->FsContext;
  4526. if (!FilterInstance) {
  4527. //
  4528. // For some reason, the filter instance has gone missing.
  4529. //
  4530. DebugPrint((DebugLevelTrace,
  4531. "error: FilterInstance has gone missing.\n") );
  4532. return;
  4533. }
  4534. AdditionalInfo = FilterInstance->PinInstanceInfo;
  4535. //
  4536. // Synchronize with control changes and protect from reentrancy.
  4537. //
  4538. KeWaitForSingleObject(&DeviceExtension->ControlEvent,
  4539. Executive,
  4540. KernelMode,
  4541. FALSE,// not alertable
  4542. NULL);
  4543. //
  4544. // Synchronize with queues.
  4545. //
  4546. KeAcquireSpinLock( &StreamObject->Queues[WhichQueue].QueueLock, &irqlOld );
  4547. //
  4548. // Loop while there are completed items on the queue.
  4549. //
  4550. while (!IsListEmpty( &StreamObject->Queues[WhichQueue].ActiveQueue )) {
  4551. Node = StreamObject->Queues[WhichQueue].ActiveQueue.Flink;
  4552. StreamHeader =
  4553. CONTAINING_RECORD(
  4554. Node,
  4555. STREAM_HEADER_EX,
  4556. ListEntry );
  4557. #if (DBG)
  4558. DebugPrint((DebugLevelTrace,
  4559. "got StreamHeader:%08x\n", StreamHeader ));
  4560. #endif
  4561. if (StreamHeader->ReferenceCount) {
  4562. DebugPrint((DebugLevelTrace,
  4563. "breaking StreamHeader:%08x\n", StreamHeader ));
  4564. break;
  4565. } else {
  4566. //
  4567. // Remove this header from the current queue.
  4568. //
  4569. RemoveHeadList( &StreamObject->Queues[WhichQueue].ActiveQueue );
  4570. #if (DBG)
  4571. StreamHeader->OnActiveList = FALSE;
  4572. #endif
  4573. KeReleaseSpinLock( &StreamObject->Queues[WhichQueue].QueueLock, irqlOld );
  4574. //
  4575. // Wait for the APC to complete. Note that if an error was
  4576. // returned, the I/O status block is not updated and the
  4577. // event is not signalled.
  4578. //
  4579. DebugPrint((DebugLevelTrace,
  4580. "waiting for StreamHeader (%08x) to complete\n", StreamHeader ));
  4581. KeWaitForSingleObject(
  4582. &StreamHeader->CompletionEvent,
  4583. Executive,
  4584. KernelMode,
  4585. FALSE,
  4586. NULL);
  4587. DebugPrint((DebugLevelTrace,
  4588. "StreamHeader (%08x) completed\n", StreamHeader));
  4589. DestinationFileObject =
  4590. StreamHeader->NextFileObject;
  4591. //
  4592. // At the time this returns TRUE, the loop will be finished.
  4593. //
  4594. SignalStop = HeaderTransfer(
  4595. FilterInstance,
  4596. StreamObject,
  4597. DestinationFileObject,
  4598. &StreamHeader );
  4599. if (StreamHeader)
  4600. {
  4601. DebugPrint((DebugLevelTrace, "IoWorker issuing: "));
  4602. if (DestinationFileObject == StreamObject->NextFileObject)
  4603. {
  4604. DebugPrint((DebugLevelTrace,"KSSTREAM_WRITE:dest=%x\n",DestinationFileObject));
  4605. Operation = KSSTREAM_WRITE;
  4606. StreamHeader->NextFileObject =
  4607. StreamObject->FileObject;
  4608. #if (DBG)
  4609. if (StreamHeader->Id == 7)
  4610. DebugPrint((DebugLevelVerbose,"iw%x\n",StreamHeader->Id));
  4611. else
  4612. DebugPrint((DebugLevelVerbose,"iw%x",StreamHeader->Id));
  4613. #endif
  4614. }
  4615. else
  4616. {
  4617. DebugPrint((DebugLevelTrace,"KSSTREAM_READ:dest=%x\n",DestinationFileObject));
  4618. Operation = KSSTREAM_READ;
  4619. StreamHeader->Header.OptionsFlags = 0;
  4620. //
  4621. // Reset the stream segment valid data length
  4622. //
  4623. StreamHeader->Header.DataUsed = 0;
  4624. StreamHeader->Header.Duration = 0;
  4625. StreamHeader->NextFileObject = StreamObject->NextFileObject;
  4626. #if (DBG)
  4627. if (StreamHeader->Id == 7)
  4628. DebugPrint((DebugLevelVerbose,"ir%x\n",StreamHeader->Id));
  4629. else
  4630. DebugPrint((DebugLevelVerbose,"ir%x",StreamHeader->Id));
  4631. #endif
  4632. }
  4633. InterlockedIncrement( &StreamHeader->ReferenceCount );
  4634. StreamHeader->WhichQueue = WhichQueue ^ 0x00000001;
  4635. Status =
  4636. KsStreamIo(
  4637. DestinationFileObject,
  4638. &StreamHeader->CompletionEvent, // Event
  4639. NULL, // PortContext
  4640. IoCompletionRoutine,
  4641. StreamHeader, // CompletionContext
  4642. KsInvokeOnSuccess |
  4643. KsInvokeOnCancel |
  4644. KsInvokeOnError,
  4645. &StreamHeader->IoStatus,
  4646. &StreamHeader->Header,
  4647. StreamHeader->Header.Size,
  4648. KSSTREAM_SYNCHRONOUS | Operation,
  4649. KernelMode );
  4650. if (Status != STATUS_PENDING) {
  4651. //
  4652. // If this I/O completes immediately (failure or not), the
  4653. // event is not signalled.
  4654. //
  4655. KeSetEvent( &StreamHeader->CompletionEvent, IO_NO_INCREMENT, FALSE );
  4656. }
  4657. }
  4658. KeAcquireSpinLock( &StreamObject->Queues[WhichQueue].QueueLock, &irqlOld );
  4659. }
  4660. //
  4661. // Ok to schedule another work item now.
  4662. //
  4663. } // end while
  4664. InterlockedExchange( &StreamObject->Queues[WhichQueue].WorkItemQueued, FALSE );
  4665. //
  4666. // If a stop needs to be signalled, signal it.
  4667. //
  4668. if (SignalStop) {
  4669. KeSetEvent( &StreamObject->StopEvent,
  4670. IO_NO_INCREMENT,
  4671. FALSE );
  4672. }
  4673. KeReleaseSpinLock( &StreamObject->Queues[WhichQueue].QueueLock, irqlOld );
  4674. //
  4675. // Release the control event
  4676. //
  4677. KeSetEvent(&DeviceExtension->ControlEvent, IO_NO_INCREMENT, FALSE);
  4678. DebugPrint((DebugLevelTrace,"exiting IoWorker\n"));
  4679. }
  4680. //---------------------------------------------------------------------------
  4681. /*++
  4682. Routine Description:
  4683. These are the work items for the source and destination pins.
  4684. Calls the IoWorker code above, passing in READ or WRITE header
  4685. queue information.
  4686. Arguments:
  4687. PVOID Context -
  4688. pointer to the stream header
  4689. Return:
  4690. Nothing.
  4691. Comments:
  4692. --*/
  4693. VOID
  4694. IoWorkerRead(
  4695. PVOID Context
  4696. )
  4697. {
  4698. IoWorker(Context,READ);
  4699. }
  4700. VOID
  4701. IoWorkerWrite(
  4702. PVOID Context
  4703. )
  4704. {
  4705. IoWorker(Context,WRITE);
  4706. }
  4707. //---------------------------------------------------------------------------
  4708. NTSTATUS
  4709. IoCompletionRoutine(
  4710. PDEVICE_OBJECT DeviceObject,
  4711. PIRP Irp,
  4712. PVOID Context
  4713. )
  4714. /*++
  4715. Routine Description:
  4716. Processes the completion of the given Irp by marking the
  4717. associated stream header as completed and scheduling a
  4718. worker item to complete processing if necessary.
  4719. Arguments:
  4720. PDEVICE_OBJECT DeviceObject -
  4721. pointer to the device object
  4722. PIRP Irp -
  4723. pointer to the I/O request packet
  4724. PVOID Context -
  4725. a context pointer (pointer to the associated stream header)
  4726. Return:
  4727. The IoStatus.Status member of the Irp.
  4728. Comments:
  4729. Not pageable, uses SpinLocks and may be called at DISPATCH_LEVEL.
  4730. --*/
  4731. {
  4732. KIRQL irqlOld;
  4733. PSTREAM_HEADER_EX StreamHeader = Context;
  4734. PFILTER_INSTANCE FilterInstance;
  4735. PSTREAM_OBJECT StreamObject;
  4736. ULONG WhichQueue;
  4737. #if (DBG)
  4738. ASSERT( StreamHeader->Data == StreamHeader->Header.Data );
  4739. #endif
  4740. StreamObject =
  4741. (PSTREAM_OBJECT) StreamHeader->OwnerFileObject->FsContext;
  4742. DebugPrint((DebugLevelTrace,
  4743. "IoCompletionRoutine:StreamHeader %08x, StreamObject %08x\n",StreamHeader,StreamObject));
  4744. FilterInstance =
  4745. (PFILTER_INSTANCE)
  4746. StreamHeader->OwnerFileObject->RelatedFileObject->FsContext;
  4747. WhichQueue = StreamHeader->WhichQueue;
  4748. KeAcquireSpinLock( &StreamObject->Queues[WhichQueue].QueueLock, &irqlOld );
  4749. //
  4750. // Remove this reference count on the IRP so that we can continue
  4751. // the loop if this work item is not the head item of the list.
  4752. //
  4753. InterlockedDecrement( &StreamHeader->ReferenceCount );
  4754. //
  4755. // Copy the status block so that we don't have to wait for the APC.
  4756. //
  4757. StreamHeader->IoStatus = Irp->IoStatus;
  4758. //
  4759. // Sweep the active queue in the worker to complete the transfer.
  4760. //
  4761. if (!StreamObject->Queues[WhichQueue].WorkItemQueued) {
  4762. //
  4763. // A work item is not pending, initialize the worker item
  4764. // for the new context and queue it.
  4765. //
  4766. ExInitializeWorkItem(
  4767. &StreamObject->Queues[WhichQueue].WorkItem,
  4768. (WhichQueue == READ) ? IoWorkerRead : IoWorkerWrite,
  4769. StreamObject );
  4770. InterlockedExchange( &StreamObject->Queues[WhichQueue].WorkItemQueued, TRUE );
  4771. KsQueueWorkItem(
  4772. (WhichQueue == READ) ? FilterInstance->WorkerRead :
  4773. FilterInstance->WorkerWrite,
  4774. &StreamObject->Queues[WhichQueue].WorkItem );
  4775. }
  4776. KeReleaseSpinLock( &StreamObject->Queues[WhichQueue].QueueLock, irqlOld );
  4777. DebugPrint((DebugLevelTrace,
  4778. "exiting IoCompletionRoutine:Irp->IoStatus.Status:%x\n",Irp->IoStatus.Status));
  4779. return Irp->IoStatus.Status;
  4780. }
  4781. //---------------------------------------------------------------------------
  4782. NTSTATUS
  4783. PrepareTransfer(
  4784. IN PFILTER_INSTANCE FilterInstance,
  4785. IN PSTREAM_OBJECT StreamObject
  4786. )
  4787. /*++
  4788. Routine Description:
  4789. Prepares for the data transfer by distributing the assigned allocators
  4790. for the source and destination pins.
  4791. Arguments:
  4792. IN PFILTER_INSTANCE FilterInstance,
  4793. pointer to the filter instance
  4794. IN PSTREAM_OBJECT StreamObject -
  4795. pointer to the transform instance
  4796. Return:
  4797. STATUS_SUCCESS or an appropriate error code.
  4798. --*/
  4799. {
  4800. KSPROPERTY Property;
  4801. KSSTREAMALLOCATOR_STATUS AllocatorStatus;
  4802. NTSTATUS Status;
  4803. PSTREAM_HEADER_EX StreamHeader;
  4804. ULONG i, Returned;
  4805. PADDITIONAL_PIN_INFO AdditionalInfo;
  4806. //
  4807. // If the PinState is not PinStopped, then return.
  4808. //
  4809. DebugPrint((DebugLevelTrace,"entering PrepareTransfer\n"));
  4810. if (!StreamObject->AllocatorFileObject) {
  4811. DebugPrint((DebugLevelTrace,"!! AllocatorFileObject is NULL"));
  4812. return STATUS_SUCCESS;
  4813. }
  4814. if (StreamObject->PinState != PinStopped) {
  4815. //
  4816. // We only need to do this work when the pin has been
  4817. // completely stopped. If we were running, just reflect the
  4818. // state.
  4819. //
  4820. DebugPrint((DebugLevelTrace,"PrepareTransfer exiting, PinState != PinStopped\n"));
  4821. StreamObject->PinState = PinPrepared;
  4822. return STATUS_SUCCESS;
  4823. }
  4824. AdditionalInfo = FilterInstance->PinInstanceInfo;
  4825. //
  4826. // Retrieve the allocator framing information for the pin.
  4827. //
  4828. Property.Set = KSPROPSETID_StreamAllocator;
  4829. Property.Id = KSPROPERTY_STREAMALLOCATOR_STATUS;
  4830. Property.Flags = KSPROPERTY_TYPE_GET;
  4831. Status =
  4832. KsSynchronousIoControlDevice(
  4833. StreamObject->AllocatorFileObject,
  4834. KernelMode,
  4835. IOCTL_KS_PROPERTY,
  4836. &Property,
  4837. sizeof( Property ),
  4838. &AllocatorStatus,
  4839. sizeof( AllocatorStatus ),
  4840. &Returned );
  4841. if (!NT_SUCCESS( Status ))
  4842. {
  4843. DebugPrint((DebugLevelTrace,
  4844. "PrepareTransfer exiting, unable to retrieve allocator status\n"));
  4845. return Status;
  4846. }
  4847. //
  4848. // Save the framing information
  4849. //
  4850. StreamObject->Framing = AllocatorStatus.Framing;
  4851. //
  4852. // Allocate the frames from the allocator
  4853. //
  4854. // 1. Always allocate frames when starting the IrpSource.
  4855. //
  4856. // 2. If the allocator is not shared, then allocate the frames when
  4857. // the (each) destination pin is started.
  4858. //
  4859. if (StreamObject->PinType == IrpSource) {
  4860. InterlockedExchange (&StreamObject -> QueuedFramesPlusOne, 1);
  4861. #if (DBG)
  4862. DebugPrint((DebugLevelTrace,"Framing.Frames:%x\n", StreamObject->Framing.Frames));
  4863. DebugPrint((DebugLevelTrace,"Framing.FrameSize:%x\n", StreamObject->Framing.FrameSize));
  4864. #endif
  4865. for (i = 0; i < StreamObject->Framing.Frames; i++) {
  4866. DebugPrint((DebugLevelTrace,"StreamObject->ExtendedHeaderSize:%x\n", StreamObject->HwStreamObject.StreamHeaderMediaSpecific));
  4867. StreamHeader =
  4868. ExAllocatePoolWithTag(
  4869. NonPagedPool,
  4870. sizeof( STREAM_HEADER_EX ) +
  4871. StreamObject->HwStreamObject.StreamHeaderMediaSpecific,
  4872. STREAMCLASS_TAG_STREAMHEADER );
  4873. if (NULL == StreamHeader) {
  4874. DebugPrint((DebugLevelTrace,
  4875. "out of pool while allocating frames\n") );
  4876. Status = STATUS_INSUFFICIENT_RESOURCES;
  4877. } else {
  4878. RtlZeroMemory(
  4879. StreamHeader,
  4880. sizeof( STREAM_HEADER_EX ) +
  4881. StreamObject->HwStreamObject.StreamHeaderMediaSpecific);
  4882. KeInitializeEvent(
  4883. &StreamHeader->CompletionEvent,
  4884. SynchronizationEvent,
  4885. FALSE );
  4886. StreamHeader->Header.Size =
  4887. sizeof( KSSTREAM_HEADER ) +
  4888. StreamObject->HwStreamObject.StreamHeaderMediaSpecific;
  4889. if (StreamObject->HwStreamObject.StreamHeaderMediaSpecific) {
  4890. *(PULONG)((&StreamHeader->Header) + 1) =
  4891. StreamObject->HwStreamObject.StreamHeaderMediaSpecific;
  4892. }
  4893. Status =
  4894. AllocateFrame(
  4895. StreamObject->AllocatorFileObject,
  4896. &StreamHeader->Header.Data );
  4897. #if (DBG)
  4898. //
  4899. // Track who is stomping on the headers...
  4900. //
  4901. StreamHeader->Data = StreamHeader->Header.Data;
  4902. #endif
  4903. StreamHeader->WhichQueue = READ;
  4904. StreamHeader->Id = i;
  4905. if (!NT_SUCCESS( Status )) {
  4906. DebugPrint((DebugLevelTrace,
  4907. "failed to allocate a frame\n") );
  4908. //
  4909. // Free this header here and the routine below will
  4910. // clean up whatever has been added to the queue.
  4911. //
  4912. ExFreePool( StreamHeader );
  4913. } else {
  4914. //
  4915. // Start with the owner file object as this connection,
  4916. // if a no-copy condition exists, this will be adjusted
  4917. // in the transfer function.
  4918. //
  4919. StreamHeader->OwnerFileObject =
  4920. StreamObject->FileObject;
  4921. StreamHeader->Header.DataUsed = 0;
  4922. StreamHeader->Header.FrameExtent =
  4923. StreamObject->Framing.FrameSize;
  4924. #if (DBG)
  4925. if (StreamHeader->OnFreeList) {
  4926. DebugPrint((DebugLevelTrace,"stream header already on free list.\n") );
  4927. }
  4928. #endif
  4929. InsertTailList(
  4930. &StreamObject->FreeQueue,
  4931. &StreamHeader->ListEntry );
  4932. #if (DBG)
  4933. StreamHeader->OnFreeList = TRUE;
  4934. #endif
  4935. }
  4936. }
  4937. }
  4938. //
  4939. // Clean up orphaned frames from the allocator and free headers
  4940. // to the pool if there was a failure.
  4941. //
  4942. if (!NT_SUCCESS( Status )) {
  4943. while (!IsListEmpty( &StreamObject->FreeQueue )) {
  4944. PLIST_ENTRY Node;
  4945. Node = RemoveHeadList( &StreamObject->FreeQueue );
  4946. StreamHeader =
  4947. CONTAINING_RECORD(
  4948. Node,
  4949. STREAM_HEADER_EX,
  4950. ListEntry );
  4951. #if (DBG)
  4952. StreamHeader->OnFreeList = FALSE;
  4953. ASSERT( StreamHeader->Data == StreamHeader->Header.Data );
  4954. #endif
  4955. FreeFrame(
  4956. StreamObject->AllocatorFileObject,
  4957. StreamHeader->Header.Data );
  4958. #if (DBG)
  4959. if (StreamHeader->OnFreeList || StreamHeader->OnActiveList) {
  4960. DebugPrint((DebugLevelTrace,
  4961. "freeing header %x still on list\n", StreamHeader) );
  4962. }
  4963. #endif
  4964. ExFreePool( StreamHeader );
  4965. }
  4966. DebugPrint((DebugLevelTrace,
  4967. "PrepareTransfer exiting, frame allocation failed: %08x\n", Status) );
  4968. return Status;
  4969. }
  4970. }
  4971. StreamObject->PinState = PinPrepared;
  4972. DebugPrint((DebugLevelTrace,"exiting PrepareTransfer\n"));
  4973. return STATUS_SUCCESS;
  4974. }
  4975. //---------------------------------------------------------------------------
  4976. NTSTATUS
  4977. BeginTransfer(
  4978. IN PFILTER_INSTANCE FilterInstance,
  4979. IN PSTREAM_OBJECT StreamObject
  4980. )
  4981. /*++
  4982. Routine Description:
  4983. Begins the data transfer from each pin by initiating stream reads
  4984. from the inflow pin. The completion routine for each read will
  4985. continue the stream processing.
  4986. Arguments:
  4987. IN PFILTER_INSTANCE FilterInstance,
  4988. pointer to the filter instance
  4989. IN PSTREAM_OBJECT StreamObject -
  4990. pointer to the transform instance
  4991. Return:
  4992. STATUS_SUCCESS or an appropriate error code.
  4993. Comments:
  4994. Not pageable, uses SpinLocks.
  4995. --*/
  4996. {
  4997. KIRQL irql0,irqlFree;
  4998. NTSTATUS Status;
  4999. PSTREAM_HEADER_EX StreamHeader;
  5000. PADDITIONAL_PIN_INFO AdditionalInfo;
  5001. DebugPrint((DebugLevelTrace,"entering BeginTransfer\n"));
  5002. //
  5003. // If the PinState is not PinPrepared, then return.
  5004. //
  5005. if (StreamObject->PinState != PinPrepared) {
  5006. DebugPrint((DebugLevelTrace,"BeginTransfer exiting, PinState != PinPrepared\n") );
  5007. return STATUS_INVALID_DEVICE_STATE;
  5008. }
  5009. AdditionalInfo = FilterInstance->PinInstanceInfo;
  5010. StreamObject->PinState = PinRunning;
  5011. //
  5012. // All preparation is complete. If this is the source pin, begin
  5013. // the actual data transfer.
  5014. //
  5015. Status = STATUS_SUCCESS;
  5016. if (StreamObject->PinType == IrpSource) {
  5017. #if (DBG)
  5018. //
  5019. // get the dataflow direction
  5020. //
  5021. DebugPrint((DebugLevelVerbose,
  5022. "BeginTransfer, DataFlow:"));
  5023. if (StreamObject->DeviceExtension->StreamDescriptor->StreamInfo.DataFlow == KSPIN_DATAFLOW_IN)
  5024. DebugPrint((DebugLevelVerbose,
  5025. "KSPIN_DATAFLOW_IN\n"));
  5026. else
  5027. DebugPrint((DebugLevelVerbose,
  5028. "KSPIN_DATAFLOW_OUT\n"));
  5029. #endif
  5030. //
  5031. // Begin the transfer by reading from the inflow pin.
  5032. //
  5033. KeAcquireSpinLock( &StreamObject->Queues[0].QueueLock, &irql0 );
  5034. KeAcquireSpinLock( &StreamObject->FreeQueueLock, &irqlFree );
  5035. while (!IsListEmpty( &StreamObject->FreeQueue )) {
  5036. PLIST_ENTRY Node;
  5037. Node = RemoveHeadList( &StreamObject->FreeQueue );
  5038. StreamHeader =
  5039. CONTAINING_RECORD(
  5040. Node,
  5041. STREAM_HEADER_EX,
  5042. ListEntry );
  5043. #if (DBG)
  5044. StreamHeader->OnFreeList = FALSE;
  5045. if (StreamHeader->OnActiveList) {
  5046. DebugPrint((DebugLevelTrace,"stream header %x already on active list.\n",StreamHeader) );
  5047. }
  5048. #endif
  5049. InterlockedIncrement (&StreamObject -> QueuedFramesPlusOne);
  5050. InsertTailList( &StreamObject->Queues[0].ActiveQueue, Node );
  5051. #if (DBG)
  5052. StreamHeader->OnActiveList = TRUE;
  5053. #endif
  5054. KeReleaseSpinLock( &StreamObject->FreeQueueLock, irqlFree );
  5055. KeReleaseSpinLock( &StreamObject->Queues[0].QueueLock, irql0 );
  5056. DebugPrint((DebugLevelTrace,
  5057. "BeginTransfer, KsStreamIo: %x\n", StreamHeader));
  5058. DebugPrint((DebugLevelTrace,
  5059. "BeginTransfer, KsStreamIo: FileObject:%x\n", StreamObject->FileObject));
  5060. DebugPrint((DebugLevelTrace,
  5061. "BeginTransfer:HeaderSize:=%x\n",StreamHeader->Header.Size));
  5062. InterlockedIncrement( &StreamHeader->ReferenceCount );
  5063. StreamHeader->NextFileObject = StreamObject->NextFileObject;
  5064. //
  5065. // send a data irp to myself, first.
  5066. //
  5067. DebugPrint((DebugLevelTrace,
  5068. "BeginTransfer:Reading:%x\n",StreamHeader->Id));
  5069. Status =
  5070. KsStreamIo(
  5071. StreamObject->FileObject,
  5072. &StreamHeader->CompletionEvent, // Event
  5073. NULL, // PortContext
  5074. IoCompletionRoutine,
  5075. StreamHeader, // CompletionContext
  5076. KsInvokeOnSuccess |
  5077. KsInvokeOnCancel |
  5078. KsInvokeOnError,
  5079. &StreamHeader->IoStatus,
  5080. &StreamHeader->Header,
  5081. StreamHeader->Header.Size,
  5082. KSSTREAM_SYNCHRONOUS | KSSTREAM_READ,
  5083. KernelMode );
  5084. if (Status != STATUS_PENDING) {
  5085. //
  5086. // If this I/O completes immediately (failure or not), the
  5087. // event is not signalled.
  5088. //
  5089. KeSetEvent( &StreamHeader->CompletionEvent, IO_NO_INCREMENT, FALSE );
  5090. }
  5091. if (!NT_SUCCESS( Status )) {
  5092. DebugPrint((DebugLevelTrace, "KsStreamIo returned %08x\n", Status ));
  5093. } else {
  5094. Status = STATUS_SUCCESS;
  5095. }
  5096. KeAcquireSpinLock( &StreamObject->Queues[0].QueueLock, &irql0 );
  5097. KeAcquireSpinLock( &StreamObject->FreeQueueLock, &irqlFree );
  5098. }
  5099. KeReleaseSpinLock( &StreamObject->FreeQueueLock, irqlFree );
  5100. KeReleaseSpinLock( &StreamObject->Queues[0].QueueLock, irql0 );
  5101. }
  5102. DebugPrint((DebugLevelTrace,"exiting BeginTransfer\n"));
  5103. return Status;
  5104. }
  5105. //---------------------------------------------------------------------------
  5106. NTSTATUS
  5107. EndTransfer(
  5108. IN PFILTER_INSTANCE FilterInstance,
  5109. IN PSTREAM_OBJECT StreamObject
  5110. )
  5111. /*++
  5112. Routine Description:
  5113. Ends the data transfer, waits for all Irps to complete
  5114. Arguments:
  5115. IN PFILTER_INSTANCE FilterInstance -
  5116. pointer to the filter instance
  5117. IN PSTREAM_OBJECT StreamObject
  5118. pointer to the Stream object
  5119. Return:
  5120. STATUS_SUCCESS or an appropriate error code.
  5121. Comments:
  5122. Not pageable, uses SpinLocks.
  5123. --*/
  5124. {
  5125. PDEVICE_EXTENSION DeviceExtension;
  5126. KIRQL irqlOld;
  5127. DeviceExtension = StreamObject->DeviceExtension;
  5128. DebugPrint((DebugLevelTrace,"entering EndTransfer!\n"));
  5129. //
  5130. // Set the marker indicating that we stop sourcing frames and then flush
  5131. // to ensure that anything blocked on the output pin at least gets
  5132. // cancelled before we block and deadlock on it.
  5133. //
  5134. StreamObject -> PinState = PinStopPending;
  5135. StreamFlushIo (DeviceExtension, StreamObject);
  5136. if (InterlockedDecrement (&StreamObject -> QueuedFramesPlusOne)) {
  5137. //
  5138. // Release the control mutex to allow the I/O thread to run.
  5139. //
  5140. KeSetEvent(&DeviceExtension->ControlEvent, IO_NO_INCREMENT, FALSE);
  5141. DebugPrint((DebugLevelTrace,
  5142. "waiting for pin %d queue to empty\n", StreamObject->PinId));
  5143. //
  5144. // Wait for the queue to empty
  5145. //
  5146. KeWaitForSingleObject(
  5147. &StreamObject -> StopEvent,
  5148. Executive,
  5149. KernelMode,
  5150. FALSE,
  5151. NULL);
  5152. DebugPrint((DebugLevelTrace,"queue emptied\n") );
  5153. //
  5154. // Re-acquire the control object.
  5155. //
  5156. KeWaitForSingleObject(&DeviceExtension->ControlEvent,
  5157. Executive,
  5158. KernelMode,
  5159. FALSE,// not alertable
  5160. NULL);
  5161. }
  5162. //
  5163. // Free the frames so that we can reprepare for new allocator
  5164. // framing, a new allocator or just general cleanup/shutdown.
  5165. //
  5166. KeAcquireSpinLock( &StreamObject->FreeQueueLock, &irqlOld );
  5167. while (!IsListEmpty( &StreamObject->FreeQueue )) {
  5168. PLIST_ENTRY Node;
  5169. PSTREAM_HEADER_EX StreamHeader;
  5170. Node = RemoveHeadList( &StreamObject->FreeQueue );
  5171. StreamHeader =
  5172. CONTAINING_RECORD(
  5173. Node,
  5174. STREAM_HEADER_EX,
  5175. ListEntry );
  5176. #if (DBG)
  5177. StreamHeader->OnFreeList = FALSE;
  5178. #endif
  5179. KeReleaseSpinLock( &StreamObject->FreeQueueLock, irqlOld );
  5180. #if (DBG)
  5181. ASSERT( StreamHeader->Data == StreamHeader->Header.Data );
  5182. #endif
  5183. FreeFrame(
  5184. StreamObject->AllocatorFileObject,
  5185. StreamHeader->Header.Data );
  5186. DebugPrint((DebugLevelTrace,
  5187. "freeing header: %08x, list: %08x\n", StreamHeader, &StreamObject->FreeQueue) );
  5188. #if (DBG)
  5189. if (StreamHeader->OnFreeList || StreamHeader->OnActiveList) {
  5190. DebugPrint((DebugLevelTrace,
  5191. "freeing header %x still on list\n", StreamHeader) );
  5192. }
  5193. #endif
  5194. ExFreePool( StreamHeader );
  5195. KeAcquireSpinLock( &StreamObject->FreeQueueLock, &irqlOld );
  5196. }
  5197. StreamObject->PinState = PinStopped;
  5198. KeReleaseSpinLock( &StreamObject->FreeQueueLock, irqlOld );
  5199. DebugPrint((DebugLevelTrace,"exiting CleanupTransfer\n"));
  5200. return STATUS_SUCCESS;
  5201. }
  5202. //---------------------------------------------------------------------------
  5203. NTSTATUS
  5204. AllocateFrame(
  5205. PFILE_OBJECT Allocator,
  5206. PVOID *Frame
  5207. )
  5208. /*++
  5209. Routine Description:
  5210. Allocates a frame from the given allocator
  5211. Arguments:
  5212. PFILE_OBJECT Allocator -
  5213. pointer to the allocator's file object
  5214. PVOID *Frame -
  5215. pointer to receive the allocated frame pointer
  5216. Return:
  5217. STATUS_SUCCESS and *Frame contains a pointer to the allocated
  5218. frame, otherwise an appropriate error code.
  5219. --*/
  5220. {
  5221. NTSTATUS Status;
  5222. KSMETHOD Method;
  5223. ULONG Returned;
  5224. DebugPrint((DebugLevelTrace,"entering AllocateFrame\n"));
  5225. Method.Set = KSMETHODSETID_StreamAllocator;
  5226. Method.Id = KSMETHOD_STREAMALLOCATOR_ALLOC;
  5227. Method.Flags = KSMETHOD_TYPE_WRITE;
  5228. Status =
  5229. KsSynchronousIoControlDevice(
  5230. Allocator,
  5231. KernelMode,
  5232. IOCTL_KS_METHOD,
  5233. &Method,
  5234. sizeof( Method ),
  5235. Frame,
  5236. sizeof( PVOID ),
  5237. &Returned );
  5238. DebugPrint((DebugLevelTrace,"exiting AllocateFrame\n"));
  5239. return Status;
  5240. }
  5241. //---------------------------------------------------------------------------
  5242. NTSTATUS
  5243. FreeFrame(
  5244. PFILE_OBJECT Allocator,
  5245. PVOID Frame
  5246. )
  5247. /*++
  5248. Routine Description:
  5249. Frees a frame to the given allocator
  5250. Arguments:
  5251. PFILE_OBJECT Allocator -
  5252. pointer to the allocator's file object
  5253. PVOID Frame -
  5254. pointer to the frame to be freed.
  5255. Return:
  5256. STATUS_SUCCESS or else an appropriate error code.
  5257. --*/
  5258. {
  5259. NTSTATUS Status;
  5260. KSMETHOD Method;
  5261. ULONG Returned;
  5262. DebugPrint((DebugLevelTrace,"entering FreeFrame\n"));
  5263. Method.Set = KSMETHODSETID_StreamAllocator;
  5264. Method.Id = KSMETHOD_STREAMALLOCATOR_FREE;
  5265. Method.Flags = KSMETHOD_TYPE_READ;
  5266. Status =
  5267. KsSynchronousIoControlDevice(
  5268. Allocator,
  5269. KernelMode,
  5270. IOCTL_KS_METHOD,
  5271. &Method,
  5272. sizeof( Method ),
  5273. &Frame,
  5274. sizeof( PVOID ),
  5275. &Returned );
  5276. DebugPrint((DebugLevelTrace,"exiting FreeFrame\n"));
  5277. return Status;
  5278. }
  5279. //---------------------------------------------------------------------------
  5280. NTSTATUS
  5281. PinCreateHandler(
  5282. IN PIRP Irp,
  5283. IN PSTREAM_OBJECT StreamObject
  5284. )
  5285. /*++
  5286. Routine Description:
  5287. This is the pin creation handler which is called by KS when a
  5288. pin create request is submitted to the filter.
  5289. Arguments:
  5290. IN PIRP Irp -
  5291. pointer to the I/O request packet
  5292. Return:
  5293. STATUS_SUCCESS or an appropriate error return code.
  5294. --*/
  5295. {
  5296. NTSTATUS Status;
  5297. PIO_STACK_LOCATION IrpStack;
  5298. PFILTER_INSTANCE FilterInstance;
  5299. PADDITIONAL_PIN_INFO AdditionalInfo;
  5300. PFILE_OBJECT NextFileObject;
  5301. IrpStack = IoGetCurrentIrpStackLocation( Irp );
  5302. DebugPrint((DebugLevelTrace,"entering PinCreateHandler\n"));
  5303. FilterInstance =
  5304. (PFILTER_INSTANCE) IrpStack->FileObject->RelatedFileObject->FsContext;
  5305. AdditionalInfo = FilterInstance->PinInstanceInfo;
  5306. Status = STATUS_SUCCESS;
  5307. StreamObject->NextFileObject = NULL;
  5308. DebugPrint((DebugLevelTrace,"PinCreateHandler:its an IrpSource\n"));
  5309. //
  5310. // Validate that we can handle this connection request
  5311. //
  5312. if (StreamObject->NextFileObject) {
  5313. DebugPrint((DebugLevelTrace,"invalid connection request\n") );
  5314. Status = STATUS_CONNECTION_REFUSED;
  5315. }
  5316. else
  5317. {
  5318. Status =
  5319. ObReferenceObjectByHandle(
  5320. StreamObject->PinToHandle,
  5321. FILE_READ_ACCESS | FILE_WRITE_ACCESS | SYNCHRONIZE,
  5322. *IoFileObjectType,
  5323. KernelMode,
  5324. &NextFileObject,
  5325. NULL );
  5326. if (!NT_SUCCESS(Status)) {
  5327. DebugPrint((DebugLevelTrace,"PinCreateHandler:error referencing PinToHandle\n"));
  5328. }
  5329. else
  5330. {
  5331. // NextFileObject must be per instance
  5332. //AdditionalInfo[ StreamObject->PinId ].NextFileObject = NextFileObject;
  5333. StreamObject->NextFileObject = NextFileObject;
  5334. //
  5335. // Add the pin's target to the list of targets for
  5336. // recalculating stack depth.
  5337. //
  5338. KsSetTargetDeviceObject(
  5339. StreamObject->ComObj.DeviceHeader,
  5340. IoGetRelatedDeviceObject(
  5341. NextFileObject ) );
  5342. }
  5343. }
  5344. DebugPrint((DebugLevelTrace,"PinCreateHandler returning %x\n", Status ));
  5345. return Status;
  5346. }
  5347. #endif