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.

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