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.

562 lines
12 KiB

  1. /*--------------------------------------------------------------------------
  2. *
  3. * Copyright (C) Cyclades Corporation, 1996-2001.
  4. * All rights reserved.
  5. *
  6. * Cyclom-Y Port Driver
  7. *
  8. * This file: cyyimmed.c
  9. *
  10. * Description: This module contains the code related to transmit
  11. * immediate character operations in the Cyclom-Y Port
  12. * driver.
  13. *
  14. * Notes: This code supports Windows 2000 and Windows XP,
  15. * x86 and ia64 processors.
  16. *
  17. * Complies with Cyclades SW Coding Standard rev 1.3.
  18. *
  19. *--------------------------------------------------------------------------
  20. */
  21. /*-------------------------------------------------------------------------
  22. *
  23. * Change History
  24. *
  25. *--------------------------------------------------------------------------
  26. *
  27. *
  28. *--------------------------------------------------------------------------
  29. */
  30. #include "precomp.h"
  31. VOID
  32. CyyGetNextImmediate(
  33. IN PIRP *CurrentOpIrp,
  34. IN PLIST_ENTRY QueueToProcess,
  35. IN PIRP *NewIrp,
  36. IN BOOLEAN CompleteCurrent,
  37. IN PCYY_DEVICE_EXTENSION Extension
  38. );
  39. VOID
  40. CyyCancelImmediate(
  41. IN PDEVICE_OBJECT DeviceObject,
  42. IN PIRP Irp
  43. );
  44. BOOLEAN
  45. CyyGiveImmediateToIsr(
  46. IN PVOID Context
  47. );
  48. BOOLEAN
  49. CyyGrabImmediateFromIsr(
  50. IN PVOID Context
  51. );
  52. BOOLEAN
  53. CyyGiveImmediateToIsr(
  54. IN PVOID Context
  55. );
  56. BOOLEAN
  57. CyyGrabImmediateFromIsr(
  58. IN PVOID Context
  59. );
  60. #ifdef ALLOC_PRAGMA
  61. #pragma alloc_text(PAGESER,CyyStartImmediate)
  62. #pragma alloc_text(PAGESER,CyyGetNextImmediate)
  63. #pragma alloc_text(PAGESER,CyyCancelImmediate)
  64. #pragma alloc_text(PAGESER,CyyGiveImmediateToIsr)
  65. #pragma alloc_text(PAGESER,CyyGrabImmediateFromIsr)
  66. #endif
  67. VOID
  68. CyyStartImmediate(
  69. IN PCYY_DEVICE_EXTENSION Extension
  70. )
  71. /*++
  72. Routine Description:
  73. This routine will calculate the timeouts needed for the
  74. write. It will then hand the irp off to the isr. It
  75. will need to be careful incase the irp has been canceled.
  76. Arguments:
  77. Extension - A pointer to the serial device extension.
  78. Return Value:
  79. None.
  80. --*/
  81. {
  82. KIRQL OldIrql;
  83. LARGE_INTEGER TotalTime;
  84. BOOLEAN UseATimer;
  85. SERIAL_TIMEOUTS Timeouts;
  86. CYY_LOCKED_PAGED_CODE();
  87. CyyDbgPrintEx(DPFLTR_TRACE_LEVEL, ">CyyStartImmediate(%X)\n",
  88. Extension);
  89. UseATimer = FALSE;
  90. Extension->CurrentImmediateIrp->IoStatus.Status = STATUS_PENDING;
  91. IoMarkIrpPending(Extension->CurrentImmediateIrp);
  92. //
  93. // Calculate the timeout value needed for the
  94. // request. Note that the values stored in the
  95. // timeout record are in milliseconds. Note that
  96. // if the timeout values are zero then we won't start
  97. // the timer.
  98. //
  99. KeAcquireSpinLock(
  100. &Extension->ControlLock,
  101. &OldIrql
  102. );
  103. Timeouts = Extension->Timeouts;
  104. KeReleaseSpinLock(
  105. &Extension->ControlLock,
  106. OldIrql
  107. );
  108. if (Timeouts.WriteTotalTimeoutConstant ||
  109. Timeouts.WriteTotalTimeoutMultiplier) {
  110. UseATimer = TRUE;
  111. //
  112. // We have some timer values to calculate.
  113. //
  114. TotalTime.QuadPart
  115. = (LONGLONG)((ULONG)Timeouts.WriteTotalTimeoutMultiplier);
  116. TotalTime.QuadPart += Timeouts.WriteTotalTimeoutConstant;
  117. TotalTime.QuadPart *= -10000;
  118. }
  119. //
  120. // As the irp might be going to the isr, this is a good time
  121. // to initialize the reference count.
  122. //
  123. SERIAL_INIT_REFERENCE(Extension->CurrentImmediateIrp);
  124. //
  125. // We need to see if this irp should be canceled.
  126. //
  127. IoAcquireCancelSpinLock(&OldIrql);
  128. if (Extension->CurrentImmediateIrp->Cancel) {
  129. PIRP OldIrp = Extension->CurrentImmediateIrp;
  130. Extension->CurrentImmediateIrp = NULL;
  131. IoReleaseCancelSpinLock(OldIrql);
  132. OldIrp->IoStatus.Status = STATUS_CANCELLED;
  133. OldIrp->IoStatus.Information = 0;
  134. CyyCompleteRequest(Extension, OldIrp, 0);
  135. } else {
  136. //
  137. // We give the irp to to the isr to write out.
  138. // We set a cancel routine that knows how to
  139. // grab the current write away from the isr.
  140. //
  141. IoSetCancelRoutine(
  142. Extension->CurrentImmediateIrp,
  143. CyyCancelImmediate
  144. );
  145. //
  146. // Since the cancel routine knows about the irp we
  147. // increment the reference count.
  148. //
  149. SERIAL_SET_REFERENCE(
  150. Extension->CurrentImmediateIrp,
  151. SERIAL_REF_CANCEL
  152. );
  153. if (UseATimer) {
  154. CyySetTimer(
  155. &Extension->ImmediateTotalTimer,
  156. TotalTime,
  157. &Extension->TotalImmediateTimeoutDpc,
  158. Extension
  159. );
  160. //
  161. // Since the timer knows about the irp we increment
  162. // the reference count.
  163. //
  164. SERIAL_SET_REFERENCE(
  165. Extension->CurrentImmediateIrp,
  166. SERIAL_REF_TOTAL_TIMER
  167. );
  168. }
  169. KeSynchronizeExecution(
  170. Extension->Interrupt,
  171. CyyGiveImmediateToIsr,
  172. Extension
  173. );
  174. IoReleaseCancelSpinLock(OldIrql);
  175. }
  176. CyyDbgPrintEx(DPFLTR_TRACE_LEVEL, "<CyyStartImmediate\n");
  177. }
  178. VOID
  179. CyyCompleteImmediate(
  180. IN PKDPC Dpc,
  181. IN PVOID DeferredContext,
  182. IN PVOID SystemContext1,
  183. IN PVOID SystemContext2
  184. )
  185. {
  186. PCYY_DEVICE_EXTENSION Extension = DeferredContext;
  187. KIRQL OldIrql;
  188. UNREFERENCED_PARAMETER(SystemContext1);
  189. UNREFERENCED_PARAMETER(SystemContext2);
  190. CyyDbgPrintEx(DPFLTR_TRACE_LEVEL, ">CyyCompleteImmediate(%X)\n",
  191. Extension);
  192. IoAcquireCancelSpinLock(&OldIrql);
  193. CyyTryToCompleteCurrent(
  194. Extension,
  195. NULL,
  196. OldIrql,
  197. STATUS_SUCCESS,
  198. &Extension->CurrentImmediateIrp,
  199. NULL,
  200. NULL,
  201. &Extension->ImmediateTotalTimer,
  202. NULL,
  203. CyyGetNextImmediate,
  204. SERIAL_REF_ISR
  205. );
  206. CyyDpcEpilogue(Extension, Dpc);
  207. CyyDbgPrintEx(DPFLTR_TRACE_LEVEL, "<CyyCompleteImmediate\n");
  208. }
  209. VOID
  210. CyyTimeoutImmediate(
  211. IN PKDPC Dpc,
  212. IN PVOID DeferredContext,
  213. IN PVOID SystemContext1,
  214. IN PVOID SystemContext2
  215. )
  216. {
  217. PCYY_DEVICE_EXTENSION Extension = DeferredContext;
  218. KIRQL OldIrql;
  219. UNREFERENCED_PARAMETER(SystemContext1);
  220. UNREFERENCED_PARAMETER(SystemContext2);
  221. CyyDbgPrintEx(DPFLTR_TRACE_LEVEL, ">CyyTimeoutImmediate(%X)\n",
  222. Extension);
  223. IoAcquireCancelSpinLock(&OldIrql);
  224. CyyTryToCompleteCurrent(
  225. Extension,
  226. CyyGrabImmediateFromIsr,
  227. OldIrql,
  228. STATUS_TIMEOUT,
  229. &Extension->CurrentImmediateIrp,
  230. NULL,
  231. NULL,
  232. &Extension->ImmediateTotalTimer,
  233. NULL,
  234. CyyGetNextImmediate,
  235. SERIAL_REF_TOTAL_TIMER
  236. );
  237. CyyDpcEpilogue(Extension, Dpc);
  238. CyyDbgPrintEx(DPFLTR_TRACE_LEVEL, "<CyyTimeoutImmediate\n");
  239. }
  240. VOID
  241. CyyGetNextImmediate(
  242. IN PIRP *CurrentOpIrp,
  243. IN PLIST_ENTRY QueueToProcess,
  244. IN PIRP *NewIrp,
  245. IN BOOLEAN CompleteCurrent,
  246. IN PCYY_DEVICE_EXTENSION Extension
  247. )
  248. /*++
  249. Routine Description:
  250. This routine is used to complete the current immediate
  251. irp. Even though the current immediate will always
  252. be completed and there is no queue associated with it,
  253. we use this routine so that we can try to satisfy
  254. a wait for transmit queue empty event.
  255. Arguments:
  256. CurrentOpIrp - Pointer to the pointer that points to the
  257. current write irp. This should point
  258. to CurrentImmediateIrp.
  259. QueueToProcess - Always NULL.
  260. NewIrp - Always NULL on exit to this routine.
  261. CompleteCurrent - Should always be true for this routine.
  262. Return Value:
  263. None.
  264. --*/
  265. {
  266. KIRQL OldIrql;
  267. PIRP OldIrp = *CurrentOpIrp;
  268. UNREFERENCED_PARAMETER(QueueToProcess);
  269. UNREFERENCED_PARAMETER(CompleteCurrent);
  270. CYY_LOCKED_PAGED_CODE();
  271. IoAcquireCancelSpinLock(&OldIrql);
  272. ASSERT(Extension->TotalCharsQueued >= 1);
  273. Extension->TotalCharsQueued--;
  274. *CurrentOpIrp = NULL;
  275. *NewIrp = NULL;
  276. KeSynchronizeExecution(
  277. Extension->Interrupt,
  278. CyyProcessEmptyTransmit,
  279. Extension
  280. );
  281. IoReleaseCancelSpinLock(OldIrql);
  282. CyyCompleteRequest(Extension, OldIrp, IO_SERIAL_INCREMENT);
  283. }
  284. VOID
  285. CyyCancelImmediate(
  286. IN PDEVICE_OBJECT DeviceObject,
  287. IN PIRP Irp
  288. )
  289. /*++
  290. Routine Description:
  291. This routine is used to cancel a irp that is waiting on
  292. a comm event.
  293. Arguments:
  294. DeviceObject - Pointer to the device object for this device
  295. Irp - Pointer to the IRP for the current request
  296. Return Value:
  297. None.
  298. --*/
  299. {
  300. PCYY_DEVICE_EXTENSION Extension = DeviceObject->DeviceExtension;
  301. CYY_LOCKED_PAGED_CODE();
  302. CyyTryToCompleteCurrent(
  303. Extension,
  304. CyyGrabImmediateFromIsr,
  305. Irp->CancelIrql,
  306. STATUS_CANCELLED,
  307. &Extension->CurrentImmediateIrp,
  308. NULL,
  309. NULL,
  310. &Extension->ImmediateTotalTimer,
  311. NULL,
  312. CyyGetNextImmediate,
  313. SERIAL_REF_CANCEL
  314. );
  315. }
  316. BOOLEAN
  317. CyyGiveImmediateToIsr(
  318. IN PVOID Context
  319. )
  320. /*++
  321. Routine Description:
  322. Try to start off the write by slipping it in behind
  323. a transmit immediate char, or if that isn't available
  324. and the transmit holding register is empty, "tickle"
  325. the UART into interrupting with a transmit buffer
  326. empty.
  327. NOTE: This routine is called by KeSynchronizeExecution.
  328. NOTE: This routine assumes that it is called with the
  329. cancel spin lock held.
  330. Arguments:
  331. Context - Really a pointer to the device extension.
  332. Return Value:
  333. This routine always returns FALSE.
  334. --*/
  335. {
  336. PCYY_DEVICE_EXTENSION Extension = Context;
  337. CYY_LOCKED_PAGED_CODE();
  338. Extension->TransmitImmediate = TRUE;
  339. Extension->ImmediateChar =
  340. *((UCHAR *)
  341. (Extension->CurrentImmediateIrp->AssociatedIrp.SystemBuffer));
  342. //
  343. // The isr now has a reference to the irp.
  344. //
  345. SERIAL_SET_REFERENCE(
  346. Extension->CurrentImmediateIrp,
  347. SERIAL_REF_ISR
  348. );
  349. //
  350. // Check first to see if a write is going on. If
  351. // there is then we'll just slip in during the write.
  352. //
  353. if (!Extension->WriteLength) {
  354. //
  355. // If there is no normal write transmitting then we
  356. // will "re-enable" the transmit holding register empty
  357. // interrupt. The 8250 family of devices will always
  358. // signal a transmit holding register empty interrupt
  359. // *ANY* time this bit is set to one. By doing things
  360. // this way we can simply use the normal interrupt code
  361. // to start off this write.
  362. //
  363. // We've been keeping track of whether the transmit holding
  364. // register is empty so it we only need to do this
  365. // if the register is empty.
  366. //
  367. if (Extension->HoldingEmpty) {
  368. CyyTxStart(Extension);
  369. }
  370. }
  371. return FALSE;
  372. }
  373. BOOLEAN
  374. CyyGrabImmediateFromIsr(
  375. IN PVOID Context
  376. )
  377. /*++
  378. Routine Description:
  379. This routine is used to grab the current irp, which could be timing
  380. out or canceling, from the ISR
  381. NOTE: This routine is being called from KeSynchronizeExecution.
  382. NOTE: This routine assumes that the cancel spin lock is held
  383. when this routine is called.
  384. Arguments:
  385. Context - Really a pointer to the device extension.
  386. Return Value:
  387. Always false.
  388. --*/
  389. {
  390. PCYY_DEVICE_EXTENSION Extension = Context;
  391. CYY_LOCKED_PAGED_CODE();
  392. if (Extension->TransmitImmediate) {
  393. Extension->TransmitImmediate = FALSE;
  394. //
  395. // Since the isr no longer references this irp, we can
  396. // decrement it's reference count.
  397. //
  398. SERIAL_CLEAR_REFERENCE(
  399. Extension->CurrentImmediateIrp,
  400. SERIAL_REF_ISR
  401. );
  402. }
  403. return FALSE;
  404. }