Source code of Windows XP (NT5)
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.

897 lines
28 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1998 - 1999
  6. //
  7. // File: readwrit.c
  8. //
  9. //--------------------------------------------------------------------------
  10. //
  11. // This file contains functions associated with handling Read and Write requests
  12. //
  13. #include "pch.h"
  14. NTSTATUS
  15. ParForwardToReverse(
  16. IN PPDO_EXTENSION Pdx
  17. )
  18. /*++
  19. Routine Description:
  20. This routine flips the bus from Forward to Reverse direction.
  21. Arguments:
  22. Pdx - Supplies the device extension.
  23. Return Value:
  24. None.
  25. --*/
  26. {
  27. NTSTATUS Status = STATUS_SUCCESS;
  28. // Do a quick check to see if we are where we want to be.
  29. // Happy punt if everything is ok.
  30. if( Pdx->Connected &&
  31. ( Pdx->CurrentPhase == PHASE_REVERSE_IDLE || Pdx->CurrentPhase == PHASE_REVERSE_XFER) ) {
  32. DD((PCE)Pdx,DDT,"ParForwardToReverse - already in reverse mode\n");
  33. return Status;
  34. }
  35. if (Pdx->Connected) {
  36. if (Pdx->CurrentPhase != PHASE_REVERSE_IDLE &&
  37. Pdx->CurrentPhase != PHASE_REVERSE_XFER) {
  38. if (afpForward[Pdx->IdxForwardProtocol].ProtocolFamily ==
  39. arpReverse[Pdx->IdxReverseProtocol].ProtocolFamily) {
  40. // Protocol Families match and we are in Fwd. Exit Fwd to cleanup the state
  41. // machine, fifo, etc. We will call EnterReverse later to
  42. // actually bus flip. Also only do this if in safe mode
  43. if ( (afpForward[Pdx->IdxForwardProtocol].fnExitForward) ) {
  44. Status = afpForward[Pdx->IdxForwardProtocol].fnExitForward(Pdx);
  45. }
  46. } else {
  47. //
  48. // Protocol Families don't match...need to terminate from the forward mode
  49. //
  50. if (afpForward[Pdx->IdxForwardProtocol].fnDisconnect) {
  51. afpForward[Pdx->IdxForwardProtocol].fnDisconnect (Pdx);
  52. }
  53. if ((Pdx->ForwardInterfaceAddress != DEFAULT_ECP_CHANNEL) &&
  54. (afpForward[Pdx->IdxForwardProtocol].fnSetInterfaceAddress))
  55. Pdx->SetForwardAddress = TRUE;
  56. }
  57. }
  58. }
  59. if( (!Pdx->Connected) && (arpReverse[Pdx->IdxReverseProtocol].fnConnect) ) {
  60. //
  61. // If we are still connected the protocol families match...
  62. //
  63. Status = arpReverse[Pdx->IdxReverseProtocol].fnConnect(Pdx, FALSE);
  64. //
  65. // Makes the assumption that the connected address is always 0
  66. //
  67. if ((NT_SUCCESS(Status)) &&
  68. (arpReverse[Pdx->IdxReverseProtocol].fnSetInterfaceAddress) &&
  69. (Pdx->ReverseInterfaceAddress != DEFAULT_ECP_CHANNEL)) {
  70. Pdx->SetReverseAddress = TRUE;
  71. }
  72. }
  73. //
  74. // Set the channel address if we need to.
  75. //
  76. if (NT_SUCCESS(Status) && Pdx->SetReverseAddress &&
  77. (arpReverse[Pdx->IdxReverseProtocol].fnSetInterfaceAddress)) {
  78. Status = arpReverse[Pdx->IdxReverseProtocol].fnSetInterfaceAddress (
  79. Pdx,
  80. Pdx->ReverseInterfaceAddress);
  81. if (NT_SUCCESS(Status))
  82. Pdx->SetReverseAddress = FALSE;
  83. else
  84. Pdx->SetReverseAddress = TRUE;
  85. }
  86. //
  87. // Do we need to reverse?
  88. //
  89. if ( (NT_SUCCESS(Status)) &&
  90. ((Pdx->CurrentPhase != PHASE_REVERSE_IDLE) &&
  91. (Pdx->CurrentPhase != PHASE_REVERSE_XFER)) ) {
  92. if ((arpReverse[Pdx->IdxReverseProtocol].fnEnterReverse))
  93. Status = arpReverse[Pdx->IdxReverseProtocol].fnEnterReverse(Pdx);
  94. }
  95. DD((PCE)Pdx,DDT,"ParForwardToReverse - exit w/status=%x\n",Status);
  96. return Status;
  97. }
  98. BOOLEAN
  99. ParHaveReadData(
  100. IN PPDO_EXTENSION Pdx
  101. )
  102. /*++
  103. Routine Description:
  104. This method determines if the dot4 peripheral has any data ready
  105. to send to the host.
  106. Arguments:
  107. Pdx - Supplies the device EXTENSION.
  108. Return Value:
  109. TRUE - Either the peripheral has data
  110. FALSE - No data
  111. --*/
  112. {
  113. NTSTATUS status;
  114. BOOLEAN justAcquiredPort = FALSE;
  115. if( Pdx->CurrentPhase != PHASE_TERMINATE &&
  116. Pdx->CurrentPhase != PHASE_REVERSE_IDLE &&
  117. Pdx->CurrentPhase != PHASE_REVERSE_XFER &&
  118. Pdx->CurrentPhase != PHASE_FORWARD_IDLE &&
  119. Pdx->CurrentPhase != PHASE_FORWARD_XFER ) {
  120. // unexpected phase - no idea what to do here - pretend that
  121. // there is no data avail and return
  122. DD((PCE)Pdx,DDE,"ParHaveReadData - unexpected CurrentPhase %x\n",Pdx->CurrentPhase);
  123. PptAssertMsg("ParHaveReadData - unexpected CurrentPhase",FALSE);
  124. return FALSE;
  125. }
  126. if( PHASE_TERMINATE == Pdx->CurrentPhase ) {
  127. //
  128. // we're not currently talking with the peripheral and we
  129. // likely don't have access to the port - try to acquire the
  130. // port and establish communication with the peripheral so
  131. // that we can check if the peripheral has data for us
  132. //
  133. // CurrentPhase indicates !Connected - do a check for consistency
  134. PptAssert( !Pdx->Connected );
  135. DD((PCE)Pdx,DDE,"ParHaveReadData - PHASE_TERMINATE\n");
  136. if( !Pdx->bAllocated ) {
  137. // we don't have the port - try to acquire port
  138. DD((PCE)Pdx,DDE,"ParHaveReadData - PHASE_TERMINATE - don't have port\n");
  139. status = PptAcquirePortViaIoctl( Pdx->Fdo, NULL );
  140. if( STATUS_SUCCESS == status ) {
  141. // we now have the port
  142. DD((PCE)Pdx,DDE,"ParHaveReadData - PHASE_TERMINATE - port acquired\n");
  143. // note that we have just now acquired the port so
  144. // that we can release the port below if we are unable
  145. // to establish communication with the peripheral
  146. justAcquiredPort = TRUE;
  147. Pdx->bAllocated = TRUE;
  148. } else {
  149. // we couldn't get the port - bail out
  150. DD((PCE)Pdx,DDE,"ParHaveReadData - PHASE_TERMINATE - don't have port - acquire failed\n");
  151. return FALSE;
  152. }
  153. } // endif !Pdx->bAllocated
  154. //
  155. // we now have the port - try to negotiate into a forward
  156. // mode since we believe that the check for periph data
  157. // avail is more robust in forward modes
  158. //
  159. DD((PCE)Pdx,DDE,"ParHaveReadData - PHASE_TERMINATE - we have the port - try to Connect\n");
  160. DD((PCE)Pdx,DDE,"ParHaveReadData - we have the port - try to Connect - calling ParReverseToForward\n");
  161. //
  162. // ParReverseToForward:
  163. //
  164. // 1) tries to negotiate the peripheral into the forward mode
  165. // specified by a combination of the device specific
  166. // Pdx->IdxForwardProtocol and the driver global afpForward
  167. // array.
  168. //
  169. // 2) sets up our internal state machine, Pdx->CurrentPhase
  170. //
  171. // 3) as a side effect - sets Pdx->SetForwardAddress if we
  172. // need to use a non-Zero ECP (or EPP) address.
  173. //
  174. status = ParReverseToForward( Pdx );
  175. if( STATUS_SUCCESS == status ) {
  176. //
  177. // We are in communication with the peripheral
  178. //
  179. DD((PCE)Pdx,DDE,"ParHaveReadData - we have the port - connected - ParReverseToForward SUCCESS\n");
  180. // Set the channel address if we need to - use the side effect from ParReverseToForward here
  181. if( Pdx->SetForwardAddress ) {
  182. DD((PCE)Pdx,DDE,"ParHaveReadData - we have the port - connected - try to set Forward Address\n");
  183. if( afpForward[Pdx->IdxForwardProtocol].fnSetInterfaceAddress ) {
  184. status = afpForward[Pdx->IdxForwardProtocol].fnSetInterfaceAddress ( Pdx, Pdx->ForwardInterfaceAddress );
  185. if( STATUS_SUCCESS == status ) {
  186. // success - set flag to indicate that we don't need to set the address again
  187. DD((PCE)Pdx,DDE,"ParHaveReadData - we have the port - connected - set Forward Address - SUCCESS\n");
  188. Pdx->SetForwardAddress = FALSE;
  189. } else {
  190. // couldn't set address - clean up and bail out - report no peripheral data avail
  191. DD((PCE)Pdx,DDE,"ParHaveReadData - we have the port - connected - set Forward Address - FAIL\n");
  192. Pdx->SetForwardAddress = TRUE;
  193. // Return peripheral to quiescent state
  194. // (Compatibility Mode Forward Idle) and set
  195. // our state machine accordingly
  196. ParTerminate( Pdx );
  197. // if we just acquired the port in this function then give
  198. // up the port, otherwise keep it for now
  199. if( justAcquiredPort ) {
  200. DD((PCE)Pdx,DDE,"ParHaveReadData - set address failed - giving up port\n");
  201. ParFreePort( Pdx );
  202. }
  203. return FALSE;
  204. }
  205. }
  206. } else {
  207. DD((PCE)Pdx,DDE,"ParHaveReadData - we have the port - connected - no need to set Forward Address\n");
  208. }
  209. } else {
  210. // unable to establish communication with peripheral
  211. DD((PCE)Pdx,DDE,"ParHaveReadData - we have the port - try to Connect - ParReverseToForward FAILED\n");
  212. // if we just acquired the port in this function then give
  213. // up the port, otherwise keep it for now
  214. if( justAcquiredPort ) {
  215. DD((PCE)Pdx,DDE,"ParHaveReadData - connect failed - giving up port\n");
  216. ParFreePort( Pdx );
  217. }
  218. return FALSE;
  219. }
  220. // we're communicating with the peripheral - fall through to below to check for data avail
  221. } // endif PHASE_TERMINATE == CurrentPhase
  222. if( Pdx->CurrentPhase == PHASE_REVERSE_IDLE ||
  223. Pdx->CurrentPhase == PHASE_REVERSE_XFER ) {
  224. DD((PCE)Pdx,DDE,"ParHaveReadData - PHASE_REVERSE_*\n");
  225. if( arpReverse[Pdx->IdxReverseProtocol].fnHaveReadData ) {
  226. if( arpReverse[Pdx->IdxReverseProtocol].fnHaveReadData( Pdx ) ) {
  227. DD((PCE)Pdx,DDE,"ParHaveReadData - PHASE_REVERSE_* - we have data\n");
  228. return TRUE;
  229. }
  230. }
  231. DD((PCE)Pdx,DDE,"ParHaveReadData - PHASE_REVERSE_* - no data - flip bus to forward\n");
  232. // Don't have data. This could be a fluke. Let's flip the bus
  233. // and try again in Fwd mode since some peripherals reportedly
  234. // have broken firmware that does not properly signal that
  235. // they have data avail when in some reverse modes.
  236. ParReverseToForward( Pdx );
  237. }
  238. if( Pdx->CurrentPhase == PHASE_FORWARD_IDLE ||
  239. Pdx->CurrentPhase == PHASE_FORWARD_XFER ) {
  240. DD((PCE)Pdx,DDE,"ParHaveReadData - PHASE_FORWARD_*\n");
  241. if( afpForward[Pdx->IdxForwardProtocol].ProtocolFamily == FAMILY_BECP ||
  242. afpForward[Pdx->IdxForwardProtocol].Protocol & ECP_HW_NOIRQ ||
  243. afpForward[Pdx->IdxForwardProtocol].Protocol & ECP_HW_IRQ) {
  244. if( PptEcpHwHaveReadData( Pdx ) ) {
  245. DD((PCE)Pdx,DDE,"ParHaveReadData - PHASE_FORWARD_* - ECP HW - have data\n");
  246. return TRUE;
  247. }
  248. // Hmmm. No data. Is the chip stuck?
  249. DD((PCE)Pdx,DDE,"ParHaveReadData - PHASE_FORWARD_* - ECP HW - no data\n");
  250. return FALSE;
  251. } else {
  252. if( afpForward[Pdx->IdxForwardProtocol].Protocol & ECP_SW ) {
  253. DD((PCE)Pdx,DDE,"ParHaveReadData - PHASE_FORWARD_* - ECP SW - checking for data\n");
  254. return ParEcpHaveReadData( Pdx );
  255. }
  256. }
  257. }
  258. // DVRH RMT
  259. // We got here because the protocol doesn't support peeking.
  260. // - pretend there is data avail
  261. DD((PCE)Pdx,DDE,"ParHaveReadData - exit - returning TRUE\n");
  262. return TRUE;
  263. }
  264. NTSTATUS
  265. ParPing(
  266. IN PPDO_EXTENSION Pdx
  267. )
  268. /*++
  269. Routine Description:
  270. This method was intended to ping the device, but it is currently a NOOP.
  271. Arguments:
  272. Pdx - Supplies the device EXTENSION.
  273. Return Value:
  274. none
  275. --*/
  276. {
  277. NTSTATUS NtStatus = STATUS_SUCCESS;
  278. UNREFERENCED_PARAMETER( Pdx );
  279. return NtStatus;
  280. }
  281. NTSTATUS
  282. PptPdoReadWrite(
  283. IN PDEVICE_OBJECT DeviceObject,
  284. IN PIRP Irp
  285. )
  286. /*++
  287. Routine Description:
  288. This is the dispatch routine for READ and WRITE requests.
  289. Arguments:
  290. DeviceObject - Supplies the device object.
  291. Irp - Supplies the I/O request packet.
  292. Return Value:
  293. STATUS_PENDING - Request pending - a worker thread will carry
  294. out the request at PASSIVE_LEVEL IRQL
  295. STATUS_SUCCESS - Success - asked for a read or write of
  296. length zero.
  297. STATUS_INVALID_PARAMETER - Invalid parameter.
  298. STATUS_DELETE_PENDING - This device object is being deleted.
  299. --*/
  300. {
  301. PIO_STACK_LOCATION IrpSp;
  302. PPDO_EXTENSION Pdx;
  303. Irp->IoStatus.Information = 0;
  304. IrpSp = IoGetCurrentIrpStackLocation(Irp);
  305. Pdx = DeviceObject->DeviceExtension;
  306. //
  307. // bail out if a delete is pending for this device object
  308. //
  309. if(Pdx->DeviceStateFlags & PPT_DEVICE_DELETE_PENDING) {
  310. return P4CompleteRequest( Irp, STATUS_DELETE_PENDING, Irp->IoStatus.Information );
  311. }
  312. //
  313. // bail out if a remove is pending for our ParPort device object
  314. //
  315. if(Pdx->DeviceStateFlags & PAR_DEVICE_PORT_REMOVE_PENDING) {
  316. return P4CompleteRequest( Irp, STATUS_DELETE_PENDING, Irp->IoStatus.Information );
  317. }
  318. //
  319. // bail out if device has been removed
  320. //
  321. if(Pdx->DeviceStateFlags & (PPT_DEVICE_REMOVED|PPT_DEVICE_SURPRISE_REMOVED) ) {
  322. return P4CompleteRequest( Irp, STATUS_DEVICE_REMOVED, Irp->IoStatus.Information );
  323. }
  324. //
  325. // Note that checks of the Write IRP parameters also handles Read IRPs
  326. // because the Write and Read structures are identical in the
  327. // IO_STACK_LOCATION.Parameters union
  328. //
  329. //
  330. // bail out on nonzero offset
  331. //
  332. if( (IrpSp->Parameters.Write.ByteOffset.HighPart != 0) || (IrpSp->Parameters.Write.ByteOffset.LowPart != 0) ) {
  333. return P4CompleteRequest( Irp, STATUS_INVALID_PARAMETER, Irp->IoStatus.Information );
  334. }
  335. //
  336. // immediately succeed read or write request of length zero
  337. //
  338. if (IrpSp->Parameters.Write.Length == 0) {
  339. return P4CompleteRequest( Irp, STATUS_SUCCESS, Irp->IoStatus.Information );
  340. }
  341. //
  342. // Request appears to be valid, queue it for our worker thread to handle at
  343. // PASSIVE_LEVEL IRQL and wake up the thread to do the work
  344. //
  345. {
  346. KIRQL OldIrql;
  347. // make sure IRP isn't cancelled out from under us
  348. IoAcquireCancelSpinLock(&OldIrql);
  349. if (Irp->Cancel) {
  350. // IRP has been cancelled, bail out
  351. IoReleaseCancelSpinLock(OldIrql);
  352. return STATUS_CANCELLED;
  353. } else {
  354. BOOLEAN needToSignalSemaphore = IsListEmpty( &Pdx->WorkQueue ) ? TRUE : FALSE;
  355. #pragma warning( push )
  356. #pragma warning( disable : 4054 4055 )
  357. IoSetCancelRoutine(Irp, ParCancelRequest);
  358. #pragma warning( pop )
  359. IoMarkIrpPending(Irp);
  360. InsertTailList(&Pdx->WorkQueue, &Irp->Tail.Overlay.ListEntry);
  361. IoReleaseCancelSpinLock(OldIrql);
  362. if( needToSignalSemaphore ) {
  363. KeReleaseSemaphore(&Pdx->RequestSemaphore, 0, 1, FALSE);
  364. }
  365. return STATUS_PENDING;
  366. }
  367. }
  368. }
  369. NTSTATUS
  370. ParRead(
  371. IN PPDO_EXTENSION Pdx,
  372. OUT PVOID Buffer,
  373. IN ULONG NumBytesToRead,
  374. OUT PULONG NumBytesRead
  375. )
  376. {
  377. NTSTATUS Status = STATUS_SUCCESS;
  378. PUCHAR lpsBufPtr = (PUCHAR)Buffer; // Pointer to buffer cast to desired data type
  379. ULONG Bytes = 0;
  380. *NumBytesRead = Bytes;
  381. // only do this if we are in safe mode
  382. if ( Pdx->ModeSafety == SAFE_MODE ) {
  383. if (arpReverse[Pdx->IdxReverseProtocol].fnReadShadow) {
  384. Queue *pQueue;
  385. pQueue = &(Pdx->ShadowBuffer);
  386. arpReverse[Pdx->IdxReverseProtocol].fnReadShadow( pQueue, lpsBufPtr, NumBytesToRead, &Bytes );
  387. NumBytesToRead -= Bytes;
  388. *NumBytesRead += Bytes;
  389. lpsBufPtr += Bytes;
  390. if ( 0 == NumBytesToRead ) {
  391. Status = STATUS_SUCCESS;
  392. if ((!Queue_IsEmpty(pQueue)) &&
  393. (TRUE == Pdx->P12843DL.bEventActive) ) {
  394. KeSetEvent(Pdx->P12843DL.Event, 0, FALSE);
  395. }
  396. goto ParRead_ExitLabel;
  397. }
  398. }
  399. if (arpReverse[Pdx->IdxReverseProtocol].fnHaveReadData) {
  400. if (!arpReverse[Pdx->IdxReverseProtocol].fnHaveReadData(Pdx)) {
  401. DD((PCE)Pdx,DDT,"ParRead - periph doesn't have data - give cycles to someone else\n");
  402. Status = STATUS_SUCCESS;
  403. goto ParRead_ExitLabel;
  404. }
  405. }
  406. }
  407. // Go ahead and flip the bus if need be. The proc will just make sure we're properly
  408. // connected and pointing in the right direction.
  409. Status = ParForwardToReverse( Pdx );
  410. //
  411. // The read mode will vary depending upon the currently negotiated mode.
  412. // Default: Nibble
  413. //
  414. if (NT_SUCCESS(Status)) {
  415. if (Pdx->fnRead || arpReverse[Pdx->IdxReverseProtocol].fnRead) {
  416. //
  417. // Do the read...
  418. //
  419. if(Pdx->fnRead) {
  420. Status = ((PPROTOCOL_READ_ROUTINE)Pdx->fnRead)( Pdx, (PVOID)lpsBufPtr, NumBytesToRead, &Bytes );
  421. } else {
  422. Status = arpReverse[Pdx->IdxReverseProtocol].fnRead( Pdx, (PVOID)lpsBufPtr, NumBytesToRead, &Bytes );
  423. }
  424. *NumBytesRead += Bytes;
  425. NumBytesToRead -= Bytes;
  426. #if DVRH_SHOW_BYTE_LOG
  427. {
  428. ULONG i=0;
  429. DD((PCE)Pdx,DDT,"Parallel:Read: ");
  430. for (i=0; i<*NumBytesRead; ++i) {
  431. DD((PCE)Pdx,DDT," %02x",((PUCHAR)lpsBufPtr)[i]);
  432. }
  433. DD((PCE)Pdx,DDT,"\n");
  434. }
  435. #endif
  436. } else {
  437. // If you are here, you've got a bug somewhere else
  438. DD((PCE)Pdx,DDE,"ParRead - you're hosed man - no fnRead\n");
  439. PptAssertMsg("ParRead - don't have a fnRead! Can't Read!\n",FALSE);
  440. }
  441. } else {
  442. DD((PCE)Pdx,DDE,"ParRead - Bus Flip Forward->Reverse FAILED - can't read\n");
  443. }
  444. ParRead_ExitLabel:
  445. return Status;
  446. }
  447. VOID
  448. ParReadIrp(
  449. IN PPDO_EXTENSION Pdx
  450. )
  451. /*++
  452. Routine Description:
  453. This routine implements a READ request with the extension's current irp.
  454. Arguments:
  455. Pdx - Supplies the device extension.
  456. Return Value:
  457. None.
  458. --*/
  459. {
  460. PIRP Irp = Pdx->CurrentOpIrp;
  461. PIO_STACK_LOCATION IrpSp = IoGetCurrentIrpStackLocation(Irp);
  462. ULONG bytesRead;
  463. NTSTATUS status;
  464. status = ParRead( Pdx, Irp->AssociatedIrp.SystemBuffer, IrpSp->Parameters.Read.Length, &bytesRead );
  465. Irp->IoStatus.Status = status;
  466. Irp->IoStatus.Information = bytesRead;
  467. DD((PCE)Pdx,DDT,"ParReadIrp - status = %x, bytesRead=%d\n", status, bytesRead);
  468. return;
  469. }
  470. NTSTATUS
  471. ParReverseToForward(
  472. IN PPDO_EXTENSION Pdx
  473. )
  474. /*++
  475. Routine Description:
  476. This routine flips the bus from Reverse to Forward direction.
  477. Arguments:
  478. Pdx - Supplies the device extension.
  479. Return Value:
  480. None.
  481. --*/
  482. {
  483. NTSTATUS Status = STATUS_SUCCESS;
  484. // dvdr
  485. if (Pdx->Connected) {
  486. // Do a quick check to see if we are where we want to be.
  487. // Happy punt if everything is ok.
  488. if( Pdx->CurrentPhase == PHASE_FORWARD_IDLE || Pdx->CurrentPhase == PHASE_FORWARD_XFER ) {
  489. DD((PCE)Pdx,DDT,"ParReverseToForward: Already in Fwd. Exit STATUS_SUCCESS\n");
  490. return Status;
  491. } else {
  492. if (afpForward[Pdx->IdxForwardProtocol].ProtocolFamily !=
  493. arpReverse[Pdx->IdxReverseProtocol].ProtocolFamily) {
  494. //
  495. // Protocol Families don't match...need to terminate from the forward mode
  496. //
  497. if (arpReverse[Pdx->IdxReverseProtocol].fnDisconnect) {
  498. arpReverse[Pdx->IdxReverseProtocol].fnDisconnect (Pdx);
  499. }
  500. if ((Pdx->ReverseInterfaceAddress != DEFAULT_ECP_CHANNEL) &&
  501. (arpReverse[Pdx->IdxReverseProtocol].fnSetInterfaceAddress)) {
  502. Pdx->SetReverseAddress = TRUE;
  503. }
  504. } else if((Pdx->CurrentPhase == PHASE_REVERSE_IDLE) || (Pdx->CurrentPhase == PHASE_REVERSE_XFER)) {
  505. if ( (arpReverse[Pdx->IdxReverseProtocol].fnExitReverse) ) {
  506. Status = arpReverse[Pdx->IdxReverseProtocol].fnExitReverse(Pdx);
  507. }
  508. } else {
  509. // We are in a screwy state.
  510. DD((PCE)Pdx,DDE,"ParReverseToForward: We're lost! Unknown state - Gonna start spewing!\n");
  511. Status = STATUS_IO_TIMEOUT; // I picked a RetVal from thin air!
  512. }
  513. }
  514. }
  515. // Yes, we still want to check for connection since we might have
  516. // terminated in the previous code block!
  517. if (!Pdx->Connected && afpForward[Pdx->IdxForwardProtocol].fnConnect) {
  518. Status = afpForward[Pdx->IdxForwardProtocol].fnConnect( Pdx, FALSE );
  519. //
  520. // Makes the assumption that the connected address is always 0
  521. //
  522. if ((NT_SUCCESS(Status)) && (Pdx->ForwardInterfaceAddress != DEFAULT_ECP_CHANNEL)) {
  523. Pdx->SetForwardAddress = TRUE;
  524. }
  525. }
  526. //
  527. // Do we need to enter a forward mode?
  528. //
  529. if ( (NT_SUCCESS(Status)) &&
  530. (Pdx->CurrentPhase != PHASE_FORWARD_IDLE) &&
  531. (Pdx->CurrentPhase != PHASE_FORWARD_XFER) &&
  532. (afpForward[Pdx->IdxForwardProtocol].fnEnterForward) ) {
  533. Status = afpForward[Pdx->IdxForwardProtocol].fnEnterForward(Pdx);
  534. }
  535. DD((PCE)Pdx,DDT,"ParReverseToForward - exit w/status= %x\n", Status);
  536. return Status;
  537. }
  538. NTSTATUS
  539. ParSetFwdAddress(
  540. IN PPDO_EXTENSION Pdx
  541. )
  542. {
  543. NTSTATUS Status = STATUS_SUCCESS;
  544. DD((PCE)Pdx,DDT,"ParSetFwdAddress: Start: Channel [%x]\n", Pdx->ForwardInterfaceAddress);
  545. if (afpForward[Pdx->IdxForwardProtocol].fnSetInterfaceAddress) {
  546. Status = ParReverseToForward(Pdx);
  547. if (!NT_SUCCESS(Status)) {
  548. DD((PCE)Pdx,DDE,"ParSetFwdAddress: FAIL. Couldn't flip the bus for Set ECP/EPP Channel failed.\n");
  549. goto ParSetFwdAddress_ExitLabel;
  550. }
  551. Status = afpForward[Pdx->IdxForwardProtocol].fnSetInterfaceAddress (
  552. Pdx,
  553. Pdx->ForwardInterfaceAddress);
  554. if (NT_SUCCESS(Status)) {
  555. Pdx->SetForwardAddress = FALSE;
  556. } else {
  557. DD((PCE)Pdx,DDE,"ParSetFwdAddress: FAIL. Set ECP/EPP Channel failed.\n");
  558. goto ParSetFwdAddress_ExitLabel;
  559. }
  560. } else {
  561. DD((PCE)Pdx,DDE,"ParSetFwdAddress: FAIL. Protocol doesn't support SetECP/EPP Channel\n");
  562. Status = STATUS_UNSUCCESSFUL;
  563. goto ParSetFwdAddress_ExitLabel;
  564. }
  565. ParSetFwdAddress_ExitLabel:
  566. return Status;
  567. }
  568. VOID
  569. ParTerminate(
  570. IN PPDO_EXTENSION Pdx
  571. )
  572. {
  573. if (!Pdx->Connected) {
  574. return;
  575. }
  576. if (Pdx->CurrentPhase == PHASE_REVERSE_IDLE || Pdx->CurrentPhase == PHASE_REVERSE_XFER) {
  577. if (afpForward[Pdx->IdxForwardProtocol].ProtocolFamily !=
  578. arpReverse[Pdx->IdxReverseProtocol].ProtocolFamily) {
  579. if (arpReverse[Pdx->IdxReverseProtocol].fnDisconnect) {
  580. DD((PCE)Pdx,DDT,"ParTerminate: Calling arpReverse.fnDisconnect\r\n");
  581. arpReverse[Pdx->IdxReverseProtocol].fnDisconnect (Pdx);
  582. }
  583. return;
  584. }
  585. ParReverseToForward(Pdx);
  586. }
  587. if (afpForward[Pdx->IdxForwardProtocol].fnDisconnect) {
  588. DD((PCE)Pdx,DDT,"ParTerminate: Calling afpForward.fnDisconnect\r\n");
  589. afpForward[Pdx->IdxForwardProtocol].fnDisconnect (Pdx);
  590. }
  591. }
  592. NTSTATUS
  593. ParWrite(
  594. IN PPDO_EXTENSION Pdx,
  595. OUT PVOID Buffer,
  596. IN ULONG NumBytesToWrite,
  597. OUT PULONG NumBytesWritten
  598. )
  599. {
  600. NTSTATUS Status = STATUS_SUCCESS;
  601. //
  602. // The routine which performs the write varies depending upon the currently
  603. // negotiated mode. Start I/O moves the IRP into the Pdx (CurrentOpIrp)
  604. //
  605. // Default mode: Centronics
  606. //
  607. // Go ahead and flip the bus if need be. The proc will just make sure we're properly
  608. // connected and pointing in the right direction.
  609. Status = ParReverseToForward( Pdx );
  610. // only do this if we are in safe mode
  611. if ( Pdx->ModeSafety == SAFE_MODE ) {
  612. //
  613. // Set the channel address if we need to.
  614. //
  615. if (NT_SUCCESS(Status) && Pdx->SetForwardAddress &&
  616. (afpForward[Pdx->IdxForwardProtocol].fnSetInterfaceAddress))
  617. {
  618. Status = afpForward[Pdx->IdxForwardProtocol].fnSetInterfaceAddress (
  619. Pdx,
  620. Pdx->ForwardInterfaceAddress);
  621. if (NT_SUCCESS(Status))
  622. Pdx->SetForwardAddress = FALSE;
  623. else
  624. Pdx->SetForwardAddress = TRUE;
  625. }
  626. }
  627. if (NT_SUCCESS(Status)) {
  628. if (Pdx->fnWrite || afpForward[Pdx->IdxForwardProtocol].fnWrite) {
  629. *NumBytesWritten = 0;
  630. #if DVRH_SHOW_BYTE_LOG
  631. {
  632. ULONG i=0;
  633. DD((PCE)Pdx,DDT,"Parallel:Write: ");
  634. for (i=0; i<NumBytesToWrite; ++i) {
  635. DD((PCE)Pdx,DDT," %02x",*((PUCHAR)Buffer+i));
  636. }
  637. DD((PCE)Pdx,DDT,"\n");
  638. }
  639. #endif
  640. if( Pdx->fnWrite) {
  641. Status = ((PPROTOCOL_WRITE_ROUTINE)Pdx->fnWrite)(Pdx,
  642. Buffer,
  643. NumBytesToWrite,
  644. NumBytesWritten);
  645. } else {
  646. Status = afpForward[Pdx->IdxForwardProtocol].fnWrite(Pdx,
  647. Buffer,
  648. NumBytesToWrite,
  649. NumBytesWritten);
  650. }
  651. }
  652. }
  653. return Status;
  654. }
  655. VOID
  656. ParWriteIrp(
  657. IN PPDO_EXTENSION Pdx
  658. )
  659. /*++
  660. Routine Description:
  661. This routine implements a WRITE request with the extension's current irp.
  662. Arguments:
  663. Pdx - Supplies the device extension.
  664. Return Value:
  665. None.
  666. --*/
  667. {
  668. PIRP Irp;
  669. PIO_STACK_LOCATION IrpSp;
  670. ULONG NumBytesWritten = 0;
  671. Irp = Pdx->CurrentOpIrp;
  672. IrpSp = IoGetCurrentIrpStackLocation(Irp);
  673. Irp->IoStatus.Status = ParWrite(Pdx,
  674. Irp->AssociatedIrp.SystemBuffer,
  675. IrpSp->Parameters.Write.Length,
  676. &NumBytesWritten);
  677. Irp->IoStatus.Information = NumBytesWritten;
  678. }