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.

1948 lines
54 KiB

  1. /*++
  2. Copyright (c) 1991, 1992, 1993 - 1997 Microsoft Corporation
  3. Module Name:
  4. read.c
  5. Abstract:
  6. This module contains the code that is very specific to read
  7. operations in the serial driver
  8. Author:
  9. Anthony V. Ercolano 26-Sep-1991
  10. Environment:
  11. Kernel mode
  12. --*/
  13. #include "precomp.h"
  14. VOID
  15. SerialCancelCurrentRead(
  16. PDEVICE_OBJECT DeviceObject,
  17. PIRP Irp
  18. );
  19. BOOLEAN
  20. SerialGrabReadFromIsr(
  21. IN PVOID Context
  22. );
  23. BOOLEAN
  24. SerialUpdateReadByIsr(
  25. IN PVOID Context
  26. );
  27. ULONG
  28. SerialGetCharsFromIntBuffer(
  29. PSERIAL_DEVICE_EXTENSION Extension
  30. );
  31. BOOLEAN
  32. SerialUpdateInterruptBuffer(
  33. IN PVOID Context
  34. );
  35. BOOLEAN
  36. SerialUpdateAndSwitchToUser(
  37. IN PVOID Context
  38. );
  39. NTSTATUS
  40. SerialResizeBuffer(
  41. IN PSERIAL_DEVICE_EXTENSION Extension
  42. );
  43. ULONG
  44. SerialMoveToNewIntBuffer(
  45. PSERIAL_DEVICE_EXTENSION Extension,
  46. PUCHAR NewBuffer
  47. );
  48. BOOLEAN
  49. SerialUpdateAndSwitchToNew(
  50. IN PVOID Context
  51. );
  52. #ifdef ALLOC_PRAGMA
  53. #pragma alloc_text(PAGESER,SerialRead)
  54. #pragma alloc_text(PAGESER,SerialStartRead)
  55. #pragma alloc_text(PAGESER,SerialCancelCurrentRead)
  56. #pragma alloc_text(PAGESER,SerialGrabReadFromIsr)
  57. #pragma alloc_text(PAGESER,SerialUpdateReadByIsr)
  58. #pragma alloc_text(PAGESER,SerialGetCharsFromIntBuffer)
  59. #pragma alloc_text(PAGESER,SerialUpdateInterruptBuffer)
  60. #pragma alloc_text(PAGESER,SerialUpdateAndSwitchToUser)
  61. #pragma alloc_text(PAGESER,SerialResizeBuffer)
  62. #pragma alloc_text(PAGESER,SerialMoveToNewIntBuffer)
  63. #pragma alloc_text(PAGESER,SerialUpdateAndSwitchToNew)
  64. #endif
  65. NTSTATUS
  66. SerialRead(
  67. IN PDEVICE_OBJECT DeviceObject,
  68. IN PIRP Irp
  69. )
  70. /*++
  71. Routine Description:
  72. This is the dispatch routine for reading. It validates the parameters
  73. for the read request and if all is ok then it places the request
  74. on the work queue.
  75. Arguments:
  76. DeviceObject - Pointer to the device object for this device
  77. Irp - Pointer to the IRP for the current request
  78. Return Value:
  79. If the io is zero length then it will return STATUS_SUCCESS,
  80. otherwise this routine will return the status returned by
  81. the actual start read routine.
  82. --*/
  83. {
  84. PSERIAL_DEVICE_EXTENSION extension = DeviceObject->DeviceExtension;
  85. BOOLEAN acceptingIRPs;
  86. NTSTATUS status;
  87. SERIAL_LOCKED_PAGED_CODE();
  88. SerialDump(SERTRACECALLS, ("SERIAL: Entering SerialRead\n"));
  89. if ((status = SerialIRPPrologue(Irp, extension)) != STATUS_SUCCESS) {
  90. SerialCompleteRequest(extension, Irp, IO_NO_INCREMENT);
  91. SerialDump(SERTRACECALLS, ("SERIAL: Leaving SerialRead (1)\n"));
  92. return status;
  93. }
  94. SerialDump(
  95. SERIRPPATH,
  96. ("SERIAL: Dispatch entry for: %x\n",Irp)
  97. );
  98. if (SerialCompleteIfError(
  99. DeviceObject,
  100. Irp
  101. ) != STATUS_SUCCESS) {
  102. SerialDump(SERTRACECALLS, ("SERIAL: Leaving SerialRead (2)\n"));
  103. return STATUS_CANCELLED;
  104. }
  105. Irp->IoStatus.Information = 0L;
  106. //
  107. // Quick check for a zero length read. If it is zero length
  108. // then we are already done!
  109. //
  110. if (IoGetCurrentIrpStackLocation(Irp)->Parameters.Read.Length) {
  111. //
  112. // Well it looks like we actually have to do some
  113. // work. Put the read on the queue so that we can
  114. // process it when our previous reads are done.
  115. //
  116. SerialDump(SERTRACECALLS, ("SERIAL: Leaving SerialRead (3)\n"));
  117. return SerialStartOrQueue(
  118. extension,
  119. Irp,
  120. &extension->ReadQueue,
  121. &extension->CurrentReadIrp,
  122. SerialStartRead
  123. );
  124. } else {
  125. Irp->IoStatus.Status = STATUS_SUCCESS;
  126. SerialDump(
  127. SERIRPPATH,
  128. ("SERIAL: Complete Irp: %x\n",Irp)
  129. );
  130. SerialCompleteRequest(extension, Irp, 0);
  131. SerialDump(SERTRACECALLS, ("SERIAL: Leaving SerialRead (4)\n"));
  132. return STATUS_SUCCESS;
  133. }
  134. }
  135. NTSTATUS
  136. SerialStartRead(
  137. IN PSERIAL_DEVICE_EXTENSION Extension
  138. )
  139. /*++
  140. Routine Description:
  141. This routine is used to start off any read. It initializes
  142. the Iostatus fields of the irp. It will set up any timers
  143. that are used to control the read. It will attempt to complete
  144. the read from data already in the interrupt buffer. If the
  145. read can be completed quickly it will start off another if
  146. necessary.
  147. Arguments:
  148. Extension - Simply a pointer to the serial device extension.
  149. Return Value:
  150. This routine will return the status of the first read
  151. irp. This is useful in that if we have a read that can
  152. complete right away (AND there had been nothing in the
  153. queue before it) the read could return SUCCESS and the
  154. application won't have to do a wait.
  155. --*/
  156. {
  157. SERIAL_UPDATE_CHAR updateChar;
  158. PIRP newIrp;
  159. KIRQL oldIrql;
  160. KIRQL controlIrql;
  161. BOOLEAN returnWithWhatsPresent;
  162. BOOLEAN os2ssreturn;
  163. BOOLEAN crunchDownToOne;
  164. BOOLEAN useTotalTimer;
  165. BOOLEAN useIntervalTimer;
  166. ULONG multiplierVal;
  167. ULONG constantVal;
  168. LARGE_INTEGER totalTime;
  169. SERIAL_TIMEOUTS timeoutsForIrp;
  170. BOOLEAN setFirstStatus = FALSE;
  171. NTSTATUS firstStatus;
  172. SERIAL_LOCKED_PAGED_CODE();
  173. SerialDump(SERTRACECALLS, ("SERIAL: SerialStartRead\n"));
  174. updateChar.Extension = Extension;
  175. do {
  176. //
  177. // Check to see if this is a resize request. If it is
  178. // then go to a routine that specializes in that.
  179. //
  180. if (IoGetCurrentIrpStackLocation(Extension->CurrentReadIrp)
  181. ->MajorFunction != IRP_MJ_READ) {
  182. NTSTATUS localStatus = SerialResizeBuffer(Extension);
  183. if (!setFirstStatus) {
  184. firstStatus = localStatus;
  185. setFirstStatus = TRUE;
  186. }
  187. } else {
  188. Extension->NumberNeededForRead =
  189. IoGetCurrentIrpStackLocation(Extension->CurrentReadIrp)
  190. ->Parameters.Read.Length;
  191. //
  192. // Calculate the timeout value needed for the
  193. // request. Note that the values stored in the
  194. // timeout record are in milliseconds.
  195. //
  196. useTotalTimer = FALSE;
  197. returnWithWhatsPresent = FALSE;
  198. os2ssreturn = FALSE;
  199. crunchDownToOne = FALSE;
  200. useIntervalTimer = FALSE;
  201. //
  202. //
  203. // CIMEXCIMEX -- this is a lie
  204. //
  205. // Always initialize the timer objects so that the
  206. // completion code can tell when it attempts to
  207. // cancel the timers whether the timers had ever
  208. // been Set.
  209. //
  210. // CIMEXCIMEX -- this is the truth
  211. //
  212. // What we want to do is just make sure the timers are
  213. // cancelled to the best of our ability and move on with
  214. // life.
  215. //
  216. SerialCancelTimer(&Extension->ReadRequestTotalTimer, Extension);
  217. SerialCancelTimer(&Extension->ReadRequestIntervalTimer, Extension);
  218. // KeInitializeTimer(&Extension->ReadRequestTotalTimer);
  219. // KeInitializeTimer(&Extension->ReadRequestIntervalTimer);
  220. //
  221. // We get the *current* timeout values to use for timing
  222. // this read.
  223. //
  224. KeAcquireSpinLock(
  225. &Extension->ControlLock,
  226. &controlIrql
  227. );
  228. timeoutsForIrp = Extension->Timeouts;
  229. KeReleaseSpinLock(
  230. &Extension->ControlLock,
  231. controlIrql
  232. );
  233. //
  234. // Calculate the interval timeout for the read.
  235. //
  236. if (timeoutsForIrp.ReadIntervalTimeout &&
  237. (timeoutsForIrp.ReadIntervalTimeout !=
  238. MAXULONG)) {
  239. useIntervalTimer = TRUE;
  240. Extension->IntervalTime.QuadPart =
  241. UInt32x32To64(
  242. timeoutsForIrp.ReadIntervalTimeout,
  243. 10000
  244. );
  245. if (Extension->IntervalTime.QuadPart >=
  246. Extension->CutOverAmount.QuadPart) {
  247. Extension->IntervalTimeToUse =
  248. &Extension->LongIntervalAmount;
  249. } else {
  250. Extension->IntervalTimeToUse =
  251. &Extension->ShortIntervalAmount;
  252. }
  253. }
  254. if (timeoutsForIrp.ReadIntervalTimeout == MAXULONG) {
  255. //
  256. // We need to do special return quickly stuff here.
  257. //
  258. // 1) If both constant and multiplier are
  259. // 0 then we return immediately with whatever
  260. // we've got, even if it was zero.
  261. //
  262. // 2) If constant and multiplier are not MAXULONG
  263. // then return immediately if any characters
  264. // are present, but if nothing is there, then
  265. // use the timeouts as specified.
  266. //
  267. // 3) If multiplier is MAXULONG then do as in
  268. // "2" but return when the first character
  269. // arrives.
  270. //
  271. if (!timeoutsForIrp.ReadTotalTimeoutConstant &&
  272. !timeoutsForIrp.ReadTotalTimeoutMultiplier) {
  273. returnWithWhatsPresent = TRUE;
  274. } else if ((timeoutsForIrp.ReadTotalTimeoutConstant != MAXULONG)
  275. &&
  276. (timeoutsForIrp.ReadTotalTimeoutMultiplier
  277. != MAXULONG)) {
  278. useTotalTimer = TRUE;
  279. os2ssreturn = TRUE;
  280. multiplierVal = timeoutsForIrp.ReadTotalTimeoutMultiplier;
  281. constantVal = timeoutsForIrp.ReadTotalTimeoutConstant;
  282. } else if ((timeoutsForIrp.ReadTotalTimeoutConstant != MAXULONG)
  283. &&
  284. (timeoutsForIrp.ReadTotalTimeoutMultiplier
  285. == MAXULONG)) {
  286. useTotalTimer = TRUE;
  287. os2ssreturn = TRUE;
  288. crunchDownToOne = TRUE;
  289. multiplierVal = 0;
  290. constantVal = timeoutsForIrp.ReadTotalTimeoutConstant;
  291. }
  292. } else {
  293. //
  294. // If both the multiplier and the constant are
  295. // zero then don't do any total timeout processing.
  296. //
  297. if (timeoutsForIrp.ReadTotalTimeoutMultiplier ||
  298. timeoutsForIrp.ReadTotalTimeoutConstant) {
  299. //
  300. // We have some timer values to calculate.
  301. //
  302. useTotalTimer = TRUE;
  303. multiplierVal = timeoutsForIrp.ReadTotalTimeoutMultiplier;
  304. constantVal = timeoutsForIrp.ReadTotalTimeoutConstant;
  305. }
  306. }
  307. if (useTotalTimer) {
  308. totalTime.QuadPart = ((LONGLONG)(UInt32x32To64(
  309. Extension->NumberNeededForRead,
  310. multiplierVal
  311. )
  312. + constantVal))
  313. * -10000;
  314. }
  315. //
  316. // We do this copy in the hope of getting most (if not
  317. // all) of the characters out of the interrupt buffer.
  318. //
  319. // Note that we need to protect this operation with a
  320. // spinlock since we don't want a purge to hose us.
  321. //
  322. KeAcquireSpinLock(
  323. &Extension->ControlLock,
  324. &controlIrql
  325. );
  326. updateChar.CharsCopied = SerialGetCharsFromIntBuffer(Extension);
  327. //
  328. // See if we have any cause to return immediately.
  329. //
  330. if (returnWithWhatsPresent || (!Extension->NumberNeededForRead) ||
  331. (os2ssreturn &&
  332. Extension->CurrentReadIrp->IoStatus.Information)) {
  333. //
  334. // We got all we needed for this read.
  335. // Update the number of characters in the
  336. // interrupt read buffer.
  337. //
  338. KeSynchronizeExecution(
  339. Extension->Interrupt,
  340. SerialUpdateInterruptBuffer,
  341. &updateChar
  342. );
  343. KeReleaseSpinLock(
  344. &Extension->ControlLock,
  345. controlIrql
  346. );
  347. Extension->CurrentReadIrp->IoStatus.Status = STATUS_SUCCESS;
  348. if (!setFirstStatus) {
  349. firstStatus = STATUS_SUCCESS;
  350. setFirstStatus = TRUE;
  351. }
  352. } else {
  353. //
  354. // The irp might go under control of the isr. It
  355. // won't hurt to initialize the reference count
  356. // right now.
  357. //
  358. SERIAL_INIT_REFERENCE(Extension->CurrentReadIrp);
  359. IoAcquireCancelSpinLock(&oldIrql);
  360. //
  361. // We need to see if this irp should be canceled.
  362. //
  363. if (Extension->CurrentReadIrp->Cancel) {
  364. IoReleaseCancelSpinLock(oldIrql);
  365. KeReleaseSpinLock(
  366. &Extension->ControlLock,
  367. controlIrql
  368. );
  369. Extension->CurrentReadIrp->IoStatus.Status =
  370. STATUS_CANCELLED;
  371. Extension->CurrentReadIrp->IoStatus.Information = 0;
  372. if (!setFirstStatus) {
  373. firstStatus = STATUS_CANCELLED;
  374. setFirstStatus = TRUE;
  375. }
  376. } else {
  377. //
  378. // If we are supposed to crunch the read down to
  379. // one character, then update the read length
  380. // in the irp and truncate the number needed for
  381. // read down to one. Note that if we are doing
  382. // this crunching, then the information must be
  383. // zero (or we would have completed above) and
  384. // the number needed for the read must still be
  385. // equal to the read length.
  386. //
  387. if (crunchDownToOne) {
  388. ASSERT(
  389. (!Extension->CurrentReadIrp->IoStatus.Information)
  390. &&
  391. (Extension->NumberNeededForRead ==
  392. IoGetCurrentIrpStackLocation(
  393. Extension->CurrentReadIrp
  394. )->Parameters.Read.Length)
  395. );
  396. Extension->NumberNeededForRead = 1;
  397. IoGetCurrentIrpStackLocation(
  398. Extension->CurrentReadIrp
  399. )->Parameters.Read.Length = 1;
  400. }
  401. //
  402. // We still need to get more characters for this read.
  403. // synchronize with the isr so that we can update the
  404. // number of characters and if necessary it will have the
  405. // isr switch to copying into the users buffer.
  406. //
  407. KeSynchronizeExecution(
  408. Extension->Interrupt,
  409. SerialUpdateAndSwitchToUser,
  410. &updateChar
  411. );
  412. if (!updateChar.Completed) {
  413. //
  414. // The irp still isn't complete. The
  415. // completion routines will end up reinvoking
  416. // this routine. So we simply leave.
  417. //
  418. // First thought we should start off the total
  419. // timer for the read and increment the reference
  420. // count that the total timer has on the current
  421. // irp. Note that this is safe, because even if
  422. // the io has been satisfied by the isr it can't
  423. // complete yet because we still own the cancel
  424. // spinlock.
  425. //
  426. if (useTotalTimer) {
  427. SERIAL_SET_REFERENCE(
  428. Extension->CurrentReadIrp,
  429. SERIAL_REF_TOTAL_TIMER
  430. );
  431. SerialSetTimer(
  432. &Extension->ReadRequestTotalTimer,
  433. totalTime,
  434. &Extension->TotalReadTimeoutDpc,
  435. Extension
  436. );
  437. }
  438. if (useIntervalTimer) {
  439. SERIAL_SET_REFERENCE(
  440. Extension->CurrentReadIrp,
  441. SERIAL_REF_INT_TIMER
  442. );
  443. KeQuerySystemTime(
  444. &Extension->LastReadTime
  445. );
  446. SerialSetTimer(
  447. &Extension->ReadRequestIntervalTimer,
  448. *Extension->IntervalTimeToUse,
  449. &Extension->IntervalReadTimeoutDpc,
  450. Extension
  451. );
  452. }
  453. IoMarkIrpPending(Extension->CurrentReadIrp);
  454. IoReleaseCancelSpinLock(oldIrql);
  455. KeReleaseSpinLock(
  456. &Extension->ControlLock,
  457. controlIrql
  458. );
  459. if (!setFirstStatus) {
  460. firstStatus = STATUS_PENDING;
  461. }
  462. return firstStatus;
  463. } else {
  464. IoReleaseCancelSpinLock(oldIrql);
  465. KeReleaseSpinLock(
  466. &Extension->ControlLock,
  467. controlIrql
  468. );
  469. Extension->CurrentReadIrp->IoStatus.Status =
  470. STATUS_SUCCESS;
  471. if (!setFirstStatus) {
  472. firstStatus = STATUS_SUCCESS;
  473. setFirstStatus = TRUE;
  474. }
  475. }
  476. }
  477. }
  478. }
  479. //
  480. // Well the operation is complete.
  481. //
  482. SerialGetNextIrp(
  483. &Extension->CurrentReadIrp,
  484. &Extension->ReadQueue,
  485. &newIrp,
  486. TRUE,
  487. Extension
  488. );
  489. } while (newIrp);
  490. return firstStatus;
  491. }
  492. VOID
  493. SerialCompleteRead(
  494. IN PKDPC Dpc,
  495. IN PVOID DeferredContext,
  496. IN PVOID SystemContext1,
  497. IN PVOID SystemContext2
  498. )
  499. /*++
  500. Routine Description:
  501. This routine is merely used to complete any read that
  502. ended up being used by the Isr. It assumes that the
  503. status and the information fields of the irp are already
  504. correctly filled in.
  505. Arguments:
  506. Dpc - Not Used.
  507. DeferredContext - Really points to the device extension.
  508. SystemContext1 - Not Used.
  509. SystemContext2 - Not Used.
  510. Return Value:
  511. None.
  512. --*/
  513. {
  514. PSERIAL_DEVICE_EXTENSION extension = DeferredContext;
  515. KIRQL oldIrql;
  516. UNREFERENCED_PARAMETER(SystemContext1);
  517. UNREFERENCED_PARAMETER(SystemContext2);
  518. SerialDump(SERTRACECALLS, ("SERIAL: SerialCompleteRead\n"));
  519. IoAcquireCancelSpinLock(&oldIrql);
  520. //
  521. // We set this to indicate to the interval timer
  522. // that the read has completed.
  523. //
  524. // Recall that the interval timer dpc can be lurking in some
  525. // DPC queue.
  526. //
  527. extension->CountOnLastRead = SERIAL_COMPLETE_READ_COMPLETE;
  528. SerialTryToCompleteCurrent(
  529. extension,
  530. NULL,
  531. oldIrql,
  532. STATUS_SUCCESS,
  533. &extension->CurrentReadIrp,
  534. &extension->ReadQueue,
  535. &extension->ReadRequestIntervalTimer,
  536. &extension->ReadRequestTotalTimer,
  537. SerialStartRead,
  538. SerialGetNextIrp,
  539. SERIAL_REF_ISR
  540. );
  541. SerialDpcEpilogue(extension, Dpc);
  542. }
  543. VOID
  544. SerialCancelCurrentRead(
  545. PDEVICE_OBJECT DeviceObject,
  546. PIRP Irp
  547. )
  548. /*++
  549. Routine Description:
  550. This routine is used to cancel the current read.
  551. Arguments:
  552. DeviceObject - Pointer to the device object for this device
  553. Irp - Pointer to the IRP to be canceled.
  554. Return Value:
  555. None.
  556. --*/
  557. {
  558. PSERIAL_DEVICE_EXTENSION extension = DeviceObject->DeviceExtension;
  559. SERIAL_LOCKED_PAGED_CODE();
  560. //
  561. // We set this to indicate to the interval timer
  562. // that the read has encountered a cancel.
  563. //
  564. // Recall that the interval timer dpc can be lurking in some
  565. // DPC queue.
  566. //
  567. extension->CountOnLastRead = SERIAL_COMPLETE_READ_CANCEL;
  568. SerialTryToCompleteCurrent(
  569. extension,
  570. SerialGrabReadFromIsr,
  571. Irp->CancelIrql,
  572. STATUS_CANCELLED,
  573. &extension->CurrentReadIrp,
  574. &extension->ReadQueue,
  575. &extension->ReadRequestIntervalTimer,
  576. &extension->ReadRequestTotalTimer,
  577. SerialStartRead,
  578. SerialGetNextIrp,
  579. SERIAL_REF_CANCEL
  580. );
  581. }
  582. BOOLEAN
  583. SerialGrabReadFromIsr(
  584. IN PVOID Context
  585. )
  586. /*++
  587. Routine Description:
  588. This routine is used to grab (if possible) the irp from the
  589. isr. If it finds that the isr still owns the irp it grabs
  590. the ipr away (updating the number of characters copied into the
  591. users buffer). If it grabs it away it also decrements the
  592. reference count on the irp since it no longer belongs to the
  593. isr (and the dpc that would complete it).
  594. NOTE: This routine assumes that if the current buffer that the
  595. ISR is copying characters into is the interrupt buffer then
  596. the dpc has already been queued.
  597. NOTE: This routine is being called from KeSynchronizeExecution.
  598. NOTE: This routine assumes that it is called with the cancel spin
  599. lock held.
  600. Arguments:
  601. Context - Really a pointer to the device extension.
  602. Return Value:
  603. Always false.
  604. --*/
  605. {
  606. PSERIAL_DEVICE_EXTENSION extension = Context;
  607. SERIAL_LOCKED_PAGED_CODE();
  608. if (extension->ReadBufferBase !=
  609. extension->InterruptReadBuffer) {
  610. //
  611. // We need to set the information to the number of characters
  612. // that the read wanted minus the number of characters that
  613. // didn't get read into the interrupt buffer.
  614. //
  615. extension->CurrentReadIrp->IoStatus.Information =
  616. IoGetCurrentIrpStackLocation(
  617. extension->CurrentReadIrp
  618. )->Parameters.Read.Length -
  619. ((extension->LastCharSlot - extension->CurrentCharSlot) + 1);
  620. //
  621. // Switch back to the interrupt buffer.
  622. //
  623. extension->ReadBufferBase = extension->InterruptReadBuffer;
  624. extension->CurrentCharSlot = extension->InterruptReadBuffer;
  625. extension->FirstReadableChar = extension->InterruptReadBuffer;
  626. extension->LastCharSlot = extension->InterruptReadBuffer +
  627. (extension->BufferSize - 1);
  628. extension->CharsInInterruptBuffer = 0;
  629. SERIAL_CLEAR_REFERENCE(
  630. extension->CurrentReadIrp,
  631. SERIAL_REF_ISR
  632. );
  633. }
  634. return FALSE;
  635. }
  636. VOID
  637. SerialReadTimeout(
  638. IN PKDPC Dpc,
  639. IN PVOID DeferredContext,
  640. IN PVOID SystemContext1,
  641. IN PVOID SystemContext2
  642. )
  643. /*++
  644. Routine Description:
  645. This routine is used to complete a read because its total
  646. timer has expired.
  647. Arguments:
  648. Dpc - Not Used.
  649. DeferredContext - Really points to the device extension.
  650. SystemContext1 - Not Used.
  651. SystemContext2 - Not Used.
  652. Return Value:
  653. None.
  654. --*/
  655. {
  656. PSERIAL_DEVICE_EXTENSION extension = DeferredContext;
  657. KIRQL oldIrql;
  658. UNREFERENCED_PARAMETER(SystemContext1);
  659. UNREFERENCED_PARAMETER(SystemContext2);
  660. SerialDump(SERTRACECALLS, ("SERIAL: SerialReadTimeout\n"));
  661. IoAcquireCancelSpinLock(&oldIrql);
  662. //
  663. // We set this to indicate to the interval timer
  664. // that the read has completed due to total timeout.
  665. //
  666. // Recall that the interval timer dpc can be lurking in some
  667. // DPC queue.
  668. //
  669. extension->CountOnLastRead = SERIAL_COMPLETE_READ_TOTAL;
  670. SerialTryToCompleteCurrent(
  671. extension,
  672. SerialGrabReadFromIsr,
  673. oldIrql,
  674. STATUS_TIMEOUT,
  675. &extension->CurrentReadIrp,
  676. &extension->ReadQueue,
  677. &extension->ReadRequestIntervalTimer,
  678. &extension->ReadRequestTotalTimer,
  679. SerialStartRead,
  680. SerialGetNextIrp,
  681. SERIAL_REF_TOTAL_TIMER
  682. );
  683. SerialDpcEpilogue(extension, Dpc);
  684. }
  685. BOOLEAN
  686. SerialUpdateReadByIsr(
  687. IN PVOID Context
  688. )
  689. /*++
  690. Routine Description:
  691. This routine is used to update the count of characters read
  692. by the isr since the last interval timer experation.
  693. NOTE: This routine is being called from KeSynchronizeExecution.
  694. NOTE: This routine assumes that it is called with the cancel spin
  695. lock held.
  696. Arguments:
  697. Context - Really a pointer to the device extension.
  698. Return Value:
  699. Always false.
  700. --*/
  701. {
  702. PSERIAL_DEVICE_EXTENSION extension = Context;
  703. SERIAL_LOCKED_PAGED_CODE();
  704. extension->CountOnLastRead = extension->ReadByIsr;
  705. extension->ReadByIsr = 0;
  706. return FALSE;
  707. }
  708. VOID
  709. SerialIntervalReadTimeout(
  710. IN PKDPC Dpc,
  711. IN PVOID DeferredContext,
  712. IN PVOID SystemContext1,
  713. IN PVOID SystemContext2
  714. )
  715. /*++
  716. Routine Description:
  717. This routine is used timeout the request if the time between
  718. characters exceed the interval time. A global is kept in
  719. the device extension that records the count of characters read
  720. the last the last time this routine was invoked (This dpc
  721. will resubmit the timer if the count has changed). If the
  722. count has not changed then this routine will attempt to complete
  723. the irp. Note the special case of the last count being zero.
  724. The timer isn't really in effect until the first character is
  725. read.
  726. Arguments:
  727. Dpc - Not Used.
  728. DeferredContext - Really points to the device extension.
  729. SystemContext1 - Not Used.
  730. SystemContext2 - Not Used.
  731. Return Value:
  732. None.
  733. --*/
  734. {
  735. PSERIAL_DEVICE_EXTENSION extension = DeferredContext;
  736. KIRQL oldIrql;
  737. UNREFERENCED_PARAMETER(SystemContext1);
  738. UNREFERENCED_PARAMETER(SystemContext2);
  739. IoAcquireCancelSpinLock(&oldIrql);
  740. SerialDump(SERTRACECALLS, ("SERIAL: SerialIntervalReadTimeout\n"));
  741. if (extension->CountOnLastRead == SERIAL_COMPLETE_READ_TOTAL) {
  742. //
  743. // This value is only set by the total
  744. // timer to indicate that it has fired.
  745. // If so, then we should simply try to complete.
  746. //
  747. SerialTryToCompleteCurrent(
  748. extension,
  749. SerialGrabReadFromIsr,
  750. oldIrql,
  751. STATUS_TIMEOUT,
  752. &extension->CurrentReadIrp,
  753. &extension->ReadQueue,
  754. &extension->ReadRequestIntervalTimer,
  755. &extension->ReadRequestTotalTimer,
  756. SerialStartRead,
  757. SerialGetNextIrp,
  758. SERIAL_REF_INT_TIMER
  759. );
  760. } else if (extension->CountOnLastRead == SERIAL_COMPLETE_READ_COMPLETE) {
  761. //
  762. // This value is only set by the regular
  763. // completion routine.
  764. //
  765. // If so, then we should simply try to complete.
  766. //
  767. SerialTryToCompleteCurrent(
  768. extension,
  769. SerialGrabReadFromIsr,
  770. oldIrql,
  771. STATUS_SUCCESS,
  772. &extension->CurrentReadIrp,
  773. &extension->ReadQueue,
  774. &extension->ReadRequestIntervalTimer,
  775. &extension->ReadRequestTotalTimer,
  776. SerialStartRead,
  777. SerialGetNextIrp,
  778. SERIAL_REF_INT_TIMER
  779. );
  780. } else if (extension->CountOnLastRead == SERIAL_COMPLETE_READ_CANCEL) {
  781. //
  782. // This value is only set by the cancel
  783. // read routine.
  784. //
  785. // If so, then we should simply try to complete.
  786. //
  787. SerialTryToCompleteCurrent(
  788. extension,
  789. SerialGrabReadFromIsr,
  790. oldIrql,
  791. STATUS_CANCELLED,
  792. &extension->CurrentReadIrp,
  793. &extension->ReadQueue,
  794. &extension->ReadRequestIntervalTimer,
  795. &extension->ReadRequestTotalTimer,
  796. SerialStartRead,
  797. SerialGetNextIrp,
  798. SERIAL_REF_INT_TIMER
  799. );
  800. } else if (extension->CountOnLastRead || extension->ReadByIsr) {
  801. //
  802. // Something has happened since we last came here. We
  803. // check to see if the ISR has read in any more characters.
  804. // If it did then we should update the isr's read count
  805. // and resubmit the timer.
  806. //
  807. if (extension->ReadByIsr) {
  808. KeSynchronizeExecution(
  809. extension->Interrupt,
  810. SerialUpdateReadByIsr,
  811. extension
  812. );
  813. //
  814. // Save off the "last" time something was read.
  815. // As we come back to this routine we will compare
  816. // the current time to the "last" time. If the
  817. // difference is ever larger then the interval
  818. // requested by the user, then time out the request.
  819. //
  820. KeQuerySystemTime(
  821. &extension->LastReadTime
  822. );
  823. SerialSetTimer(
  824. &extension->ReadRequestIntervalTimer,
  825. *extension->IntervalTimeToUse,
  826. &extension->IntervalReadTimeoutDpc,
  827. extension
  828. );
  829. IoReleaseCancelSpinLock(oldIrql);
  830. } else {
  831. //
  832. // Take the difference between the current time
  833. // and the last time we had characters and
  834. // see if it is greater then the interval time.
  835. // if it is, then time out the request. Otherwise
  836. // go away again for a while.
  837. //
  838. //
  839. // No characters read in the interval time. Kill
  840. // this read.
  841. //
  842. LARGE_INTEGER currentTime;
  843. KeQuerySystemTime(
  844. &currentTime
  845. );
  846. if ((currentTime.QuadPart - extension->LastReadTime.QuadPart) >=
  847. extension->IntervalTime.QuadPart) {
  848. SerialTryToCompleteCurrent(
  849. extension,
  850. SerialGrabReadFromIsr,
  851. oldIrql,
  852. STATUS_TIMEOUT,
  853. &extension->CurrentReadIrp,
  854. &extension->ReadQueue,
  855. &extension->ReadRequestIntervalTimer,
  856. &extension->ReadRequestTotalTimer,
  857. SerialStartRead,
  858. SerialGetNextIrp,
  859. SERIAL_REF_INT_TIMER
  860. );
  861. } else {
  862. SerialSetTimer(
  863. &extension->ReadRequestIntervalTimer,
  864. *extension->IntervalTimeToUse,
  865. &extension->IntervalReadTimeoutDpc,
  866. extension
  867. );
  868. IoReleaseCancelSpinLock(oldIrql);
  869. }
  870. }
  871. } else {
  872. //
  873. // Timer doesn't really start until the first character.
  874. // So we should simply resubmit ourselves.
  875. //
  876. SerialSetTimer(
  877. &extension->ReadRequestIntervalTimer,
  878. *extension->IntervalTimeToUse,
  879. &extension->IntervalReadTimeoutDpc,
  880. extension
  881. );
  882. IoReleaseCancelSpinLock(oldIrql);
  883. }
  884. SerialDpcEpilogue(extension, Dpc);
  885. }
  886. ULONG
  887. SerialGetCharsFromIntBuffer(
  888. PSERIAL_DEVICE_EXTENSION Extension
  889. )
  890. /*++
  891. Routine Description:
  892. This routine is used to copy any characters out of the interrupt
  893. buffer into the users buffer. It will be reading values that
  894. are updated with the ISR but this is safe since this value is
  895. only decremented by synchronization routines. This routine will
  896. return the number of characters copied so some other routine
  897. can call a synchronization routine to update what is seen at
  898. interrupt level.
  899. Arguments:
  900. Extension - A pointer to the device extension.
  901. Return Value:
  902. The number of characters that were copied into the user
  903. buffer.
  904. --*/
  905. {
  906. //
  907. // This value will be the number of characters that this
  908. // routine returns. It will be the minimum of the number
  909. // of characters currently in the buffer or the number of
  910. // characters required for the read.
  911. //
  912. ULONG numberOfCharsToGet;
  913. //
  914. // This holds the number of characters between the first
  915. // readable character and - the last character we will read or
  916. // the real physical end of the buffer (not the last readable
  917. // character).
  918. //
  919. ULONG firstTryNumberToGet;
  920. SERIAL_LOCKED_PAGED_CODE();
  921. //
  922. // The minimum of the number of characters we need and
  923. // the number of characters available
  924. //
  925. numberOfCharsToGet = Extension->CharsInInterruptBuffer;
  926. if (numberOfCharsToGet > Extension->NumberNeededForRead) {
  927. numberOfCharsToGet = Extension->NumberNeededForRead;
  928. }
  929. if (numberOfCharsToGet) {
  930. //
  931. // This will hold the number of characters between the
  932. // first available character and the end of the buffer.
  933. // Note that the buffer could wrap around but for the
  934. // purposes of the first copy we don't care about that.
  935. //
  936. firstTryNumberToGet = (ULONG)(Extension->LastCharSlot -
  937. Extension->FirstReadableChar) + 1;
  938. if (firstTryNumberToGet > numberOfCharsToGet) {
  939. //
  940. // The characters don't wrap. Actually they may wrap but
  941. // we don't care for the purposes of this read since the
  942. // characters we need are available before the wrap.
  943. //
  944. RtlMoveMemory(
  945. ((PUCHAR)(Extension->CurrentReadIrp->AssociatedIrp.SystemBuffer))
  946. + (IoGetCurrentIrpStackLocation(
  947. Extension->CurrentReadIrp
  948. )->Parameters.Read.Length
  949. - Extension->NumberNeededForRead
  950. ),
  951. Extension->FirstReadableChar,
  952. numberOfCharsToGet
  953. );
  954. Extension->NumberNeededForRead -= numberOfCharsToGet;
  955. //
  956. // We now will move the pointer to the first character after
  957. // what we just copied into the users buffer.
  958. //
  959. // We need to check if the stream of readable characters
  960. // is wrapping around to the beginning of the buffer.
  961. //
  962. // Note that we may have just taken the last characters
  963. // at the end of the buffer.
  964. //
  965. if ((Extension->FirstReadableChar + (numberOfCharsToGet - 1)) ==
  966. Extension->LastCharSlot) {
  967. Extension->FirstReadableChar = Extension->InterruptReadBuffer;
  968. } else {
  969. Extension->FirstReadableChar += numberOfCharsToGet;
  970. }
  971. } else {
  972. //
  973. // The characters do wrap. Get up until the end of the buffer.
  974. //
  975. RtlMoveMemory(
  976. ((PUCHAR)(Extension->CurrentReadIrp->AssociatedIrp.SystemBuffer))
  977. + (IoGetCurrentIrpStackLocation(
  978. Extension->CurrentReadIrp
  979. )->Parameters.Read.Length
  980. - Extension->NumberNeededForRead
  981. ),
  982. Extension->FirstReadableChar,
  983. firstTryNumberToGet
  984. );
  985. Extension->NumberNeededForRead -= firstTryNumberToGet;
  986. //
  987. // Now get the rest of the characters from the beginning of the
  988. // buffer.
  989. //
  990. RtlMoveMemory(
  991. ((PUCHAR)(Extension->CurrentReadIrp->AssociatedIrp.SystemBuffer))
  992. + (IoGetCurrentIrpStackLocation(
  993. Extension->CurrentReadIrp
  994. )->Parameters.Read.Length
  995. - Extension->NumberNeededForRead
  996. ),
  997. Extension->InterruptReadBuffer,
  998. numberOfCharsToGet - firstTryNumberToGet
  999. );
  1000. Extension->FirstReadableChar = Extension->InterruptReadBuffer +
  1001. (numberOfCharsToGet -
  1002. firstTryNumberToGet);
  1003. Extension->NumberNeededForRead -= (numberOfCharsToGet -
  1004. firstTryNumberToGet);
  1005. }
  1006. }
  1007. Extension->CurrentReadIrp->IoStatus.Information += numberOfCharsToGet;
  1008. return numberOfCharsToGet;
  1009. }
  1010. BOOLEAN
  1011. SerialUpdateInterruptBuffer(
  1012. IN PVOID Context
  1013. )
  1014. /*++
  1015. Routine Description:
  1016. This routine is used to update the number of characters that
  1017. remain in the interrupt buffer. We need to use this routine
  1018. since the count could be updated during the update by execution
  1019. of the ISR.
  1020. NOTE: This is called by KeSynchronizeExecution.
  1021. Arguments:
  1022. Context - Points to a structure that contains a pointer to the
  1023. device extension and count of the number of characters
  1024. that we previously copied into the users buffer. The
  1025. structure actually has a third field that we don't
  1026. use in this routine.
  1027. Return Value:
  1028. Always FALSE.
  1029. --*/
  1030. {
  1031. PSERIAL_UPDATE_CHAR update = Context;
  1032. PSERIAL_DEVICE_EXTENSION extension = update->Extension;
  1033. SERIAL_LOCKED_PAGED_CODE();
  1034. ASSERT(extension->CharsInInterruptBuffer >= update->CharsCopied);
  1035. extension->CharsInInterruptBuffer -= update->CharsCopied;
  1036. //
  1037. // Deal with flow control if necessary.
  1038. //
  1039. SerialHandleReducedIntBuffer(extension);
  1040. return FALSE;
  1041. }
  1042. BOOLEAN
  1043. SerialUpdateAndSwitchToUser(
  1044. IN PVOID Context
  1045. )
  1046. /*++
  1047. Routine Description:
  1048. This routine gets the (hopefully) few characters that
  1049. remain in the interrupt buffer after the first time we tried
  1050. to get them out. If we still don't have enough characters
  1051. to satisfy the read it will then we set things up so that the
  1052. ISR uses the user buffer copy into.
  1053. This routine is also used to update a count that is maintained
  1054. by the ISR to keep track of the number of characters in its buffer.
  1055. NOTE: This is called by KeSynchronizeExecution.
  1056. Arguments:
  1057. Context - Points to a structure that contains a pointer to the
  1058. device extension, a count of the number of characters
  1059. that we previously copied into the users buffer, and
  1060. a boolean that we will set that defines whether we
  1061. switched the ISR to copy into the users buffer.
  1062. Return Value:
  1063. Always FALSE.
  1064. --*/
  1065. {
  1066. PSERIAL_UPDATE_CHAR updateChar = Context;
  1067. PSERIAL_DEVICE_EXTENSION extension = updateChar->Extension;
  1068. SERIAL_LOCKED_PAGED_CODE();
  1069. SerialUpdateInterruptBuffer(Context);
  1070. //
  1071. // There are more characters to get to satisfy this read.
  1072. // Copy any characters that have arrived since we got
  1073. // the last batch.
  1074. //
  1075. updateChar->CharsCopied = SerialGetCharsFromIntBuffer(extension);
  1076. SerialUpdateInterruptBuffer(Context);
  1077. //
  1078. // No more new characters will be "received" until we exit
  1079. // this routine. We again check to make sure that we
  1080. // haven't satisfied this read, and if we haven't we set things
  1081. // up so that the ISR copies into the user buffer.
  1082. //
  1083. if (extension->NumberNeededForRead) {
  1084. //
  1085. // We shouldn't be switching unless there are no
  1086. // characters left.
  1087. //
  1088. ASSERT(!extension->CharsInInterruptBuffer);
  1089. //
  1090. // We use the following to values to do inteval timing.
  1091. //
  1092. // CountOnLastRead is mostly used to simply prevent
  1093. // the interval timer from timing out before any characters
  1094. // are read. (Interval timing should only be effective
  1095. // after the first character is read.)
  1096. //
  1097. // After the first time the interval timer fires and
  1098. // characters have be read we will simply update with
  1099. // the value of ReadByIsr and then set ReadByIsr to zero.
  1100. // (We do that in a synchronization routine.
  1101. //
  1102. // If the interval timer dpc routine ever encounters
  1103. // ReadByIsr == 0 when CountOnLastRead is non-zero it
  1104. // will timeout the read.
  1105. //
  1106. // (Note that we have a special case of CountOnLastRead
  1107. // < 0. This is done by the read completion routines other
  1108. // than the total timeout dpc to indicate that the total
  1109. // timeout has expired.)
  1110. //
  1111. extension->CountOnLastRead =
  1112. (LONG)extension->CurrentReadIrp->IoStatus.Information;
  1113. extension->ReadByIsr = 0;
  1114. //
  1115. // By compareing the read buffer base address to the
  1116. // the base address of the interrupt buffer the ISR
  1117. // can determine whether we are using the interrupt
  1118. // buffer or the user buffer.
  1119. //
  1120. extension->ReadBufferBase =
  1121. extension->CurrentReadIrp->AssociatedIrp.SystemBuffer;
  1122. //
  1123. // The current char slot is after the last copied in
  1124. // character. We know there is always room since we
  1125. // we wouldn't have gotten here if there wasn't.
  1126. //
  1127. extension->CurrentCharSlot = extension->ReadBufferBase +
  1128. extension->CurrentReadIrp->IoStatus.Information;
  1129. //
  1130. // The last position that a character can go is on the
  1131. // last byte of user buffer. While the actual allocated
  1132. // buffer space may be bigger, we know that there is at
  1133. // least as much as the read length.
  1134. //
  1135. extension->LastCharSlot = extension->ReadBufferBase +
  1136. (IoGetCurrentIrpStackLocation(
  1137. extension->CurrentReadIrp
  1138. )->Parameters.Read.Length
  1139. - 1);
  1140. //
  1141. // Mark the irp as being in a cancelable state.
  1142. //
  1143. IoSetCancelRoutine(
  1144. extension->CurrentReadIrp,
  1145. SerialCancelCurrentRead
  1146. );
  1147. //
  1148. // Increment the reference count twice.
  1149. //
  1150. // Once for the Isr owning the irp and once
  1151. // because the cancel routine has a reference
  1152. // to it.
  1153. //
  1154. SERIAL_SET_REFERENCE(
  1155. extension->CurrentReadIrp,
  1156. SERIAL_REF_ISR
  1157. );
  1158. SERIAL_SET_REFERENCE(
  1159. extension->CurrentReadIrp,
  1160. SERIAL_REF_CANCEL
  1161. );
  1162. updateChar->Completed = FALSE;
  1163. } else {
  1164. updateChar->Completed = TRUE;
  1165. }
  1166. return FALSE;
  1167. }
  1168. //
  1169. // We use this structure only to communicate to the synchronization
  1170. // routine when we are switching to the resized buffer.
  1171. //
  1172. typedef struct _SERIAL_RESIZE_PARAMS {
  1173. PSERIAL_DEVICE_EXTENSION Extension;
  1174. PUCHAR OldBuffer;
  1175. PUCHAR NewBuffer;
  1176. ULONG NewBufferSize;
  1177. ULONG NumberMoved;
  1178. } SERIAL_RESIZE_PARAMS,*PSERIAL_RESIZE_PARAMS;
  1179. NTSTATUS
  1180. SerialResizeBuffer(
  1181. IN PSERIAL_DEVICE_EXTENSION Extension
  1182. )
  1183. /*++
  1184. Routine Description:
  1185. This routine will process the resize buffer request.
  1186. If size requested for the RX buffer is smaller than
  1187. the current buffer then we will simply return
  1188. STATUS_SUCCESS. (We don't want to make buffers smaller.
  1189. If we did that then we all of a sudden have "overrun"
  1190. problems to deal with as well as flow control to deal
  1191. with - very painful.) We ignore the TX buffer size
  1192. request since we don't use a TX buffer.
  1193. Arguments:
  1194. Extension - Pointer to the device extension for the port.
  1195. Return Value:
  1196. STATUS_SUCCESS if everything worked out ok.
  1197. STATUS_INSUFFICIENT_RESOURCES if we couldn't allocate the
  1198. memory for the buffer.
  1199. --*/
  1200. {
  1201. PSERIAL_QUEUE_SIZE rs = Extension->CurrentReadIrp->AssociatedIrp
  1202. .SystemBuffer;
  1203. PIO_STACK_LOCATION irpSp = IoGetCurrentIrpStackLocation(
  1204. Extension->CurrentReadIrp
  1205. );
  1206. PVOID newBuffer = irpSp->Parameters.DeviceIoControl.Type3InputBuffer;
  1207. SERIAL_LOCKED_PAGED_CODE();
  1208. irpSp->Parameters.DeviceIoControl.Type3InputBuffer = NULL;
  1209. Extension->CurrentReadIrp->IoStatus.Information = 0L;
  1210. Extension->CurrentReadIrp->IoStatus.Status = STATUS_SUCCESS;
  1211. if (rs->InSize <= Extension->BufferSize) {
  1212. //
  1213. // Nothing to do. We don't make buffers smaller. Just
  1214. // agree with the user. We must deallocate the memory
  1215. // that was already allocated in the ioctl dispatch routine.
  1216. //
  1217. ExFreePool(newBuffer);
  1218. } else {
  1219. SERIAL_RESIZE_PARAMS rp;
  1220. KIRQL controlIrql;
  1221. //
  1222. // Hmmm, looks like we actually have to go
  1223. // through with this. We need to move all the
  1224. // data that is in the current buffer into this
  1225. // new buffer. We'll do this in two steps.
  1226. //
  1227. // First we go up to dispatch level and try to
  1228. // move as much as we can without stopping the
  1229. // ISR from running. We go up to dispatch level
  1230. // by acquiring the control lock. We do it at
  1231. // dispatch using the control lock so that:
  1232. //
  1233. // 1) We can't be context switched in the middle
  1234. // of the move. Our pointers into the buffer
  1235. // could be *VERY* stale by the time we got back.
  1236. //
  1237. // 2) We use the control lock since we don't want
  1238. // some pesky purge irp to come along while
  1239. // we are trying to move.
  1240. //
  1241. // After the move, but while we still hold the control
  1242. // lock, we synch with the ISR and get those last
  1243. // (hopefully) few characters that have come in since
  1244. // we started the copy. We switch all of our pointers,
  1245. // counters, and such to point to this new buffer. NOTE:
  1246. // we need to be careful. If the buffer we were using
  1247. // was not the default one created when we initialized
  1248. // the device (i.e. it was created via a previous IRP of
  1249. // this type), we should deallocate it.
  1250. //
  1251. rp.Extension = Extension;
  1252. rp.OldBuffer = Extension->InterruptReadBuffer;
  1253. rp.NewBuffer = newBuffer;
  1254. rp.NewBufferSize = rs->InSize;
  1255. KeAcquireSpinLock(
  1256. &Extension->ControlLock,
  1257. &controlIrql
  1258. );
  1259. rp.NumberMoved = SerialMoveToNewIntBuffer(
  1260. Extension,
  1261. newBuffer
  1262. );
  1263. KeSynchronizeExecution(
  1264. Extension->Interrupt,
  1265. SerialUpdateAndSwitchToNew,
  1266. &rp
  1267. );
  1268. KeReleaseSpinLock(
  1269. &Extension->ControlLock,
  1270. controlIrql
  1271. );
  1272. //
  1273. // Free up the memory that the old buffer consumed.
  1274. //
  1275. ExFreePool(rp.OldBuffer);
  1276. }
  1277. return STATUS_SUCCESS;
  1278. }
  1279. ULONG
  1280. SerialMoveToNewIntBuffer(
  1281. PSERIAL_DEVICE_EXTENSION Extension,
  1282. PUCHAR NewBuffer
  1283. )
  1284. /*++
  1285. Routine Description:
  1286. This routine is used to copy any characters out of the interrupt
  1287. buffer into the "new" buffer. It will be reading values that
  1288. are updated with the ISR but this is safe since this value is
  1289. only decremented by synchronization routines. This routine will
  1290. return the number of characters copied so some other routine
  1291. can call a synchronization routine to update what is seen at
  1292. interrupt level.
  1293. Arguments:
  1294. Extension - A pointer to the device extension.
  1295. NewBuffer - Where the characters are to be move to.
  1296. Return Value:
  1297. The number of characters that were copied into the user
  1298. buffer.
  1299. --*/
  1300. {
  1301. ULONG numberOfCharsMoved = Extension->CharsInInterruptBuffer;
  1302. SERIAL_LOCKED_PAGED_CODE();
  1303. if (numberOfCharsMoved) {
  1304. //
  1305. // This holds the number of characters between the first
  1306. // readable character and the last character we will read or
  1307. // the real physical end of the buffer (not the last readable
  1308. // character).
  1309. //
  1310. ULONG firstTryNumberToGet = (ULONG)(Extension->LastCharSlot -
  1311. Extension->FirstReadableChar) + 1;
  1312. if (firstTryNumberToGet >= numberOfCharsMoved) {
  1313. //
  1314. // The characters don't wrap.
  1315. //
  1316. RtlMoveMemory(
  1317. NewBuffer,
  1318. Extension->FirstReadableChar,
  1319. numberOfCharsMoved
  1320. );
  1321. if ((Extension->FirstReadableChar+(numberOfCharsMoved-1)) ==
  1322. Extension->LastCharSlot) {
  1323. Extension->FirstReadableChar = Extension->InterruptReadBuffer;
  1324. } else {
  1325. Extension->FirstReadableChar += numberOfCharsMoved;
  1326. }
  1327. } else {
  1328. //
  1329. // The characters do wrap. Get up until the end of the buffer.
  1330. //
  1331. RtlMoveMemory(
  1332. NewBuffer,
  1333. Extension->FirstReadableChar,
  1334. firstTryNumberToGet
  1335. );
  1336. //
  1337. // Now get the rest of the characters from the beginning of the
  1338. // buffer.
  1339. //
  1340. RtlMoveMemory(
  1341. NewBuffer+firstTryNumberToGet,
  1342. Extension->InterruptReadBuffer,
  1343. numberOfCharsMoved - firstTryNumberToGet
  1344. );
  1345. Extension->FirstReadableChar = Extension->InterruptReadBuffer +
  1346. numberOfCharsMoved - firstTryNumberToGet;
  1347. }
  1348. }
  1349. return numberOfCharsMoved;
  1350. }
  1351. BOOLEAN
  1352. SerialUpdateAndSwitchToNew(
  1353. IN PVOID Context
  1354. )
  1355. /*++
  1356. Routine Description:
  1357. This routine gets the (hopefully) few characters that
  1358. remain in the interrupt buffer after the first time we tried
  1359. to get them out.
  1360. NOTE: This is called by KeSynchronizeExecution.
  1361. Arguments:
  1362. Context - Points to a structure that contains a pointer to the
  1363. device extension, a pointer to the buffer we are moving
  1364. to, and a count of the number of characters
  1365. that we previously copied into the new buffer, and the
  1366. actual size of the new buffer.
  1367. Return Value:
  1368. Always FALSE.
  1369. --*/
  1370. {
  1371. PSERIAL_RESIZE_PARAMS params = Context;
  1372. PSERIAL_DEVICE_EXTENSION extension = params->Extension;
  1373. ULONG tempCharsInInterruptBuffer = extension->CharsInInterruptBuffer;
  1374. SERIAL_LOCKED_PAGED_CODE();
  1375. ASSERT(extension->CharsInInterruptBuffer >= params->NumberMoved);
  1376. //
  1377. // We temporarily reduce the chars in interrupt buffer to
  1378. // "fool" the move routine. We will restore it after the
  1379. // move.
  1380. //
  1381. extension->CharsInInterruptBuffer -= params->NumberMoved;
  1382. if (extension->CharsInInterruptBuffer) {
  1383. SerialMoveToNewIntBuffer(
  1384. extension,
  1385. params->NewBuffer + params->NumberMoved
  1386. );
  1387. }
  1388. extension->CharsInInterruptBuffer = tempCharsInInterruptBuffer;
  1389. extension->LastCharSlot = params->NewBuffer + (params->NewBufferSize - 1);
  1390. extension->FirstReadableChar = params->NewBuffer;
  1391. extension->ReadBufferBase = params->NewBuffer;
  1392. extension->InterruptReadBuffer = params->NewBuffer;
  1393. extension->BufferSize = params->NewBufferSize;
  1394. //
  1395. // We *KNOW* that the new interrupt buffer is larger than the
  1396. // old buffer. We don't need to worry about it being full.
  1397. //
  1398. extension->CurrentCharSlot = extension->InterruptReadBuffer +
  1399. extension->CharsInInterruptBuffer;
  1400. //
  1401. // We set up the default xon/xoff limits.
  1402. //
  1403. extension->HandFlow.XoffLimit = extension->BufferSize >> 3;
  1404. extension->HandFlow.XonLimit = extension->BufferSize >> 1;
  1405. extension->WmiCommData.XoffXmitThreshold = extension->HandFlow.XoffLimit;
  1406. extension->WmiCommData.XonXmitThreshold = extension->HandFlow.XonLimit;
  1407. extension->BufferSizePt8 = ((3*(extension->BufferSize>>2))+
  1408. (extension->BufferSize>>4));
  1409. //
  1410. // Since we (essentially) reduced the percentage of the interrupt
  1411. // buffer being full, we need to handle any flow control.
  1412. //
  1413. SerialHandleReducedIntBuffer(extension);
  1414. return FALSE;
  1415. }