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.

2523 lines
66 KiB

  1. /*++
  2. Copyright (c) 1991, 1992, 1993 Microsoft Corporation
  3. Module Name:
  4. ioctl.c
  5. Abstract:
  6. This module contains the ioctl dispatcher as well as a couple
  7. of routines that are generally just called in response to
  8. ioctl calls.
  9. Author:
  10. Anthony V. Ercolano 26-Sep-1991
  11. Environment:
  12. Kernel mode
  13. --*/
  14. #include "precomp.h"
  15. BOOLEAN
  16. SerialGetModemUpdate(
  17. IN PVOID Context
  18. );
  19. BOOLEAN
  20. SerialGetCommStatus(
  21. IN PVOID Context
  22. );
  23. VOID
  24. SerialGetProperties(
  25. IN PSERIAL_DEVICE_EXTENSION Extension,
  26. IN PSERIAL_COMMPROP Properties
  27. );
  28. BOOLEAN
  29. SerialSetEscapeChar(
  30. IN PVOID Context
  31. );
  32. #ifdef ALLOC_PRAGMA
  33. //
  34. // Locked during PnP operations and while open
  35. //
  36. #pragma alloc_text(PAGESER,SerialSetBaud)
  37. #pragma alloc_text(PAGESER,SerialSetLineControl)
  38. #pragma alloc_text(PAGESER,SerialIoControl)
  39. #pragma alloc_text(PAGESER,SerialSetChars)
  40. #pragma alloc_text(PAGESER,SerialGetModemUpdate)
  41. #pragma alloc_text(PAGESER,SerialGetCommStatus)
  42. #pragma alloc_text(PAGESER,SerialGetProperties)
  43. #pragma alloc_text(PAGESER,SerialSetEscapeChar)
  44. #pragma alloc_text(PAGESER,SerialGetStats)
  45. #pragma alloc_text(PAGESER,SerialClearStats)
  46. #pragma alloc_text(PAGESER, SerialSetMCRContents)
  47. #pragma alloc_text(PAGESER, SerialGetMCRContents)
  48. #pragma alloc_text(PAGESER, SerialSetFCRContents)
  49. #pragma alloc_text(PAGESER, SerialInternalIoControl)
  50. #endif
  51. BOOLEAN
  52. SerialGetStats(
  53. IN PVOID Context
  54. )
  55. /*++
  56. Routine Description:
  57. In sync with the interrpt service routine (which sets the perf stats)
  58. return the perf stats to the caller.
  59. Arguments:
  60. Context - Pointer to a the irp.
  61. Return Value:
  62. This routine always returns FALSE.
  63. --*/
  64. {
  65. PIO_STACK_LOCATION irpSp = IoGetCurrentIrpStackLocation((PIRP)Context);
  66. PSERIAL_DEVICE_EXTENSION extension = irpSp->DeviceObject->DeviceExtension;
  67. PSERIALPERF_STATS sp = ((PIRP)Context)->AssociatedIrp.SystemBuffer;
  68. SERIAL_LOCKED_PAGED_CODE();
  69. *sp = extension->PerfStats;
  70. return FALSE;
  71. }
  72. BOOLEAN
  73. SerialClearStats(
  74. IN PVOID Context
  75. )
  76. /*++
  77. Routine Description:
  78. In sync with the interrpt service routine (which sets the perf stats)
  79. clear the perf stats.
  80. Arguments:
  81. Context - Pointer to a the extension.
  82. Return Value:
  83. This routine always returns FALSE.
  84. --*/
  85. {
  86. SERIAL_LOCKED_PAGED_CODE();
  87. RtlZeroMemory(
  88. &((PSERIAL_DEVICE_EXTENSION)Context)->PerfStats,
  89. sizeof(SERIALPERF_STATS)
  90. );
  91. RtlZeroMemory(&((PSERIAL_DEVICE_EXTENSION)Context)->WmiPerfData,
  92. sizeof(SERIAL_WMI_PERF_DATA));
  93. return FALSE;
  94. }
  95. BOOLEAN
  96. SerialSetChars(
  97. IN PVOID Context
  98. )
  99. /*++
  100. Routine Description:
  101. This routine is used to set the special characters for the
  102. driver.
  103. Arguments:
  104. Context - Pointer to a structure that contains a pointer to
  105. the device extension and a pointer to a special characters
  106. structure.
  107. Return Value:
  108. This routine always returns FALSE.
  109. --*/
  110. {
  111. ((PSERIAL_IOCTL_SYNC)Context)->Extension->SpecialChars =
  112. *((PSERIAL_CHARS)(((PSERIAL_IOCTL_SYNC)Context)->Data));
  113. SERIAL_LOCKED_PAGED_CODE();
  114. return FALSE;
  115. }
  116. BOOLEAN
  117. SerialSetBaud(
  118. IN PVOID Context
  119. )
  120. /*++
  121. Routine Description:
  122. This routine is used to set the baud rate of the device.
  123. Arguments:
  124. Context - Pointer to a structure that contains a pointer to
  125. the device extension and what should be the current
  126. baud rate.
  127. Return Value:
  128. This routine always returns FALSE.
  129. --*/
  130. {
  131. PSERIAL_DEVICE_EXTENSION Extension = ((PSERIAL_IOCTL_SYNC)Context)->Extension;
  132. USHORT Appropriate = PtrToUshort(((PSERIAL_IOCTL_SYNC)Context)->Data);
  133. SERIAL_LOCKED_PAGED_CODE();
  134. WRITE_DIVISOR_LATCH(
  135. Extension->Controller,
  136. Appropriate
  137. );
  138. return FALSE;
  139. }
  140. BOOLEAN
  141. SerialSetLineControl(
  142. IN PVOID Context
  143. )
  144. /*++
  145. Routine Description:
  146. This routine is used to set the buad rate of the device.
  147. Arguments:
  148. Context - Pointer to the device extension.
  149. Return Value:
  150. This routine always returns FALSE.
  151. --*/
  152. {
  153. PSERIAL_DEVICE_EXTENSION Extension = Context;
  154. SERIAL_LOCKED_PAGED_CODE();
  155. WRITE_LINE_CONTROL(
  156. Extension->Controller,
  157. Extension->LineControl
  158. );
  159. return FALSE;
  160. }
  161. BOOLEAN
  162. SerialGetModemUpdate(
  163. IN PVOID Context
  164. )
  165. /*++
  166. Routine Description:
  167. This routine is simply used to call the interrupt level routine
  168. that handles modem status update.
  169. Arguments:
  170. Context - Pointer to a structure that contains a pointer to
  171. the device extension and a pointer to a ulong.
  172. Return Value:
  173. This routine always returns FALSE.
  174. --*/
  175. {
  176. PSERIAL_DEVICE_EXTENSION Extension = ((PSERIAL_IOCTL_SYNC)Context)->Extension;
  177. ULONG *Result = (ULONG *)(((PSERIAL_IOCTL_SYNC)Context)->Data);
  178. SERIAL_LOCKED_PAGED_CODE();
  179. *Result = SerialHandleModemUpdate(
  180. Extension,
  181. FALSE
  182. );
  183. return FALSE;
  184. }
  185. BOOLEAN
  186. SerialSetMCRContents(IN PVOID Context)
  187. /*++
  188. Routine Description:
  189. This routine is simply used to set the contents of the MCR
  190. Arguments:
  191. Context - Pointer to a structure that contains a pointer to
  192. the device extension and a pointer to a ulong.
  193. Return Value:
  194. This routine always returns FALSE.
  195. --*/
  196. {
  197. PSERIAL_DEVICE_EXTENSION Extension = ((PSERIAL_IOCTL_SYNC)Context)->Extension;
  198. ULONG *Result = (ULONG *)(((PSERIAL_IOCTL_SYNC)Context)->Data);
  199. SERIAL_LOCKED_PAGED_CODE();
  200. //
  201. // This is severe casting abuse!!!
  202. //
  203. WRITE_MODEM_CONTROL(Extension->Controller, (UCHAR)PtrToUlong(Result));
  204. return FALSE;
  205. }
  206. BOOLEAN
  207. SerialGetMCRContents(IN PVOID Context)
  208. /*++
  209. Routine Description:
  210. This routine is simply used to get the contents of the MCR
  211. Arguments:
  212. Context - Pointer to a structure that contains a pointer to
  213. the device extension and a pointer to a ulong.
  214. Return Value:
  215. This routine always returns FALSE.
  216. --*/
  217. {
  218. PSERIAL_DEVICE_EXTENSION Extension = ((PSERIAL_IOCTL_SYNC)Context)->Extension;
  219. ULONG *Result = (ULONG *)(((PSERIAL_IOCTL_SYNC)Context)->Data);
  220. SERIAL_LOCKED_PAGED_CODE();
  221. *Result = READ_MODEM_CONTROL(Extension->Controller);
  222. return FALSE;
  223. }
  224. BOOLEAN
  225. SerialSetFCRContents(IN PVOID Context)
  226. /*++
  227. Routine Description:
  228. This routine is simply used to set the contents of the FCR
  229. Arguments:
  230. Context - Pointer to a structure that contains a pointer to
  231. the device extension and a pointer to a ulong.
  232. Return Value:
  233. This routine always returns FALSE.
  234. --*/
  235. {
  236. PSERIAL_DEVICE_EXTENSION Extension = ((PSERIAL_IOCTL_SYNC)Context)->Extension;
  237. ULONG *Result = (ULONG *)(((PSERIAL_IOCTL_SYNC)Context)->Data);
  238. SERIAL_LOCKED_PAGED_CODE();
  239. //
  240. // This is severe casting abuse!!!
  241. //
  242. WRITE_FIFO_CONTROL(Extension->Controller, (UCHAR)PtrToUlong(Result));
  243. return FALSE;
  244. }
  245. BOOLEAN
  246. SerialGetCommStatus(
  247. IN PVOID Context
  248. )
  249. /*++
  250. Routine Description:
  251. This is used to get the current state of the serial driver.
  252. Arguments:
  253. Context - Pointer to a structure that contains a pointer to
  254. the device extension and a pointer to a serial status
  255. record.
  256. Return Value:
  257. This routine always returns FALSE.
  258. --*/
  259. {
  260. PSERIAL_DEVICE_EXTENSION Extension = ((PSERIAL_IOCTL_SYNC)Context)->Extension;
  261. PSERIAL_STATUS Stat = ((PSERIAL_IOCTL_SYNC)Context)->Data;
  262. SERIAL_LOCKED_PAGED_CODE();
  263. Stat->Errors = Extension->ErrorWord;
  264. Extension->ErrorWord = 0;
  265. //
  266. // BUG BUG We need to do something about eof (binary mode).
  267. //
  268. Stat->EofReceived = FALSE;
  269. Stat->AmountInInQueue = Extension->CharsInInterruptBuffer;
  270. Stat->AmountInOutQueue = Extension->TotalCharsQueued;
  271. if (Extension->WriteLength) {
  272. //
  273. // By definition if we have a writelength the we have
  274. // a current write irp.
  275. //
  276. ASSERT(Extension->CurrentWriteIrp);
  277. ASSERT(Stat->AmountInOutQueue >= Extension->WriteLength);
  278. ASSERT((IoGetCurrentIrpStackLocation(Extension->CurrentWriteIrp)->
  279. Parameters.Write.Length) >=
  280. Extension->WriteLength);
  281. Stat->AmountInOutQueue -=
  282. IoGetCurrentIrpStackLocation(Extension->CurrentWriteIrp)
  283. ->Parameters.Write.Length - (Extension->WriteLength);
  284. }
  285. Stat->WaitForImmediate = Extension->TransmitImmediate;
  286. Stat->HoldReasons = 0;
  287. if (Extension->TXHolding) {
  288. if (Extension->TXHolding & SERIAL_TX_CTS) {
  289. Stat->HoldReasons |= SERIAL_TX_WAITING_FOR_CTS;
  290. }
  291. if (Extension->TXHolding & SERIAL_TX_DSR) {
  292. Stat->HoldReasons |= SERIAL_TX_WAITING_FOR_DSR;
  293. }
  294. if (Extension->TXHolding & SERIAL_TX_DCD) {
  295. Stat->HoldReasons |= SERIAL_TX_WAITING_FOR_DCD;
  296. }
  297. if (Extension->TXHolding & SERIAL_TX_XOFF) {
  298. Stat->HoldReasons |= SERIAL_TX_WAITING_FOR_XON;
  299. }
  300. if (Extension->TXHolding & SERIAL_TX_BREAK) {
  301. Stat->HoldReasons |= SERIAL_TX_WAITING_ON_BREAK;
  302. }
  303. }
  304. if (Extension->RXHolding & SERIAL_RX_DSR) {
  305. Stat->HoldReasons |= SERIAL_RX_WAITING_FOR_DSR;
  306. }
  307. if (Extension->RXHolding & SERIAL_RX_XOFF) {
  308. Stat->HoldReasons |= SERIAL_TX_WAITING_XOFF_SENT;
  309. }
  310. return FALSE;
  311. }
  312. BOOLEAN
  313. SerialSetEscapeChar(
  314. IN PVOID Context
  315. )
  316. /*++
  317. Routine Description:
  318. This is used to set the character that will be used to escape
  319. line status and modem status information when the application
  320. has set up that line status and modem status should be passed
  321. back in the data stream.
  322. Arguments:
  323. Context - Pointer to the irp that is specify the escape character.
  324. Implicitly - An escape character of 0 means no escaping
  325. will occur.
  326. Return Value:
  327. This routine always returns FALSE.
  328. --*/
  329. {
  330. PSERIAL_DEVICE_EXTENSION extension =
  331. IoGetCurrentIrpStackLocation((PIRP)Context)
  332. ->DeviceObject->DeviceExtension;
  333. extension->EscapeChar =
  334. *(PUCHAR)((PIRP)Context)->AssociatedIrp.SystemBuffer;
  335. return FALSE;
  336. }
  337. NTSTATUS
  338. SerialIoControl(
  339. IN PDEVICE_OBJECT DeviceObject,
  340. IN PIRP Irp
  341. )
  342. /*++
  343. Routine Description:
  344. This routine provides the initial processing for all of the
  345. Ioctrls for the serial device.
  346. Arguments:
  347. DeviceObject - Pointer to the device object for this device
  348. Irp - Pointer to the IRP for the current request
  349. Return Value:
  350. The function value is the final status of the call
  351. --*/
  352. {
  353. //
  354. // The status that gets returned to the caller and
  355. // set in the Irp.
  356. //
  357. NTSTATUS Status;
  358. //
  359. // The current stack location. This contains all of the
  360. // information we need to process this particular request.
  361. //
  362. PIO_STACK_LOCATION IrpSp;
  363. //
  364. // Just what it says. This is the serial specific device
  365. // extension of the device object create for the serial driver.
  366. //
  367. PSERIAL_DEVICE_EXTENSION Extension = DeviceObject->DeviceExtension;
  368. //
  369. // A temporary to hold the old IRQL so that it can be
  370. // restored once we complete/validate this request.
  371. //
  372. KIRQL OldIrql;
  373. NTSTATUS prologueStatus;
  374. SERIAL_LOCKED_PAGED_CODE();
  375. //
  376. // We expect to be open so all our pages are locked down. This is, after
  377. // all, an IO operation, so the device should be open first.
  378. //
  379. if (Extension->DeviceIsOpened != TRUE) {
  380. Irp->IoStatus.Status = STATUS_INVALID_DEVICE_REQUEST;
  381. IoCompleteRequest(Irp, IO_NO_INCREMENT);
  382. return STATUS_INVALID_DEVICE_REQUEST;
  383. }
  384. if ((prologueStatus = SerialIRPPrologue(Irp, Extension))
  385. != STATUS_SUCCESS) {
  386. Irp->IoStatus.Status = prologueStatus;
  387. SerialCompleteRequest(Extension, Irp, IO_NO_INCREMENT);
  388. return prologueStatus;
  389. }
  390. SerialDump(
  391. SERIRPPATH,
  392. ("SERIAL: Dispatch entry for: %x\n",Irp)
  393. );
  394. if (SerialCompleteIfError(
  395. DeviceObject,
  396. Irp
  397. ) != STATUS_SUCCESS) {
  398. return STATUS_CANCELLED;
  399. }
  400. IrpSp = IoGetCurrentIrpStackLocation(Irp);
  401. Irp->IoStatus.Information = 0L;
  402. Status = STATUS_SUCCESS;
  403. switch (IrpSp->Parameters.DeviceIoControl.IoControlCode) {
  404. case IOCTL_SERIAL_SET_BAUD_RATE : {
  405. ULONG BaudRate;
  406. //
  407. // Will hold the value of the appropriate divisor for
  408. // the requested baud rate. If the baudrate is invalid
  409. // (because the device won't support that baud rate) then
  410. // this value is undefined.
  411. //
  412. // Note: in one sense the concept of a valid baud rate
  413. // is cloudy. We could allow the user to request any
  414. // baud rate. We could then calculate the divisor needed
  415. // for that baud rate. As long as the divisor wasn't less
  416. // than one we would be "ok". (The percentage difference
  417. // between the "true" divisor and the "rounded" value given
  418. // to the hardware might make it unusable, but... ) It would
  419. // really be up to the user to "Know" whether the baud rate
  420. // is suitable. So much for theory, *We* only support a given
  421. // set of baud rates.
  422. //
  423. SHORT AppropriateDivisor;
  424. if (IrpSp->Parameters.DeviceIoControl.InputBufferLength <
  425. sizeof(SERIAL_BAUD_RATE)) {
  426. Status = STATUS_BUFFER_TOO_SMALL;
  427. break;
  428. } else {
  429. BaudRate = ((PSERIAL_BAUD_RATE)(Irp->AssociatedIrp.SystemBuffer))->BaudRate;
  430. }
  431. //
  432. // Get the baud rate from the irp. We pass it
  433. // to a routine which will set the correct divisor.
  434. //
  435. Status = SerialGetDivisorFromBaud(
  436. Extension->ClockRate,
  437. BaudRate,
  438. &AppropriateDivisor
  439. );
  440. //
  441. // Make sure we are at power D0
  442. //
  443. if (NT_SUCCESS(Status)) {
  444. if (Extension->PowerState != PowerDeviceD0) {
  445. Status = SerialGotoPowerState(Extension->Pdo, Extension,
  446. PowerDeviceD0);
  447. if (!NT_SUCCESS(Status)) {
  448. break;
  449. }
  450. }
  451. }
  452. KeAcquireSpinLock(
  453. &Extension->ControlLock,
  454. &OldIrql
  455. );
  456. if (NT_SUCCESS(Status)) {
  457. SERIAL_IOCTL_SYNC S;
  458. Extension->CurrentBaud = BaudRate;
  459. Extension->WmiCommData.BaudRate = BaudRate;
  460. S.Extension = Extension;
  461. S.Data = (PVOID)AppropriateDivisor;
  462. KeSynchronizeExecution(
  463. Extension->Interrupt,
  464. SerialSetBaud,
  465. &S
  466. );
  467. }
  468. KeReleaseSpinLock(
  469. &Extension->ControlLock,
  470. OldIrql
  471. );
  472. break;
  473. }
  474. case IOCTL_SERIAL_GET_BAUD_RATE: {
  475. PSERIAL_BAUD_RATE Br = (PSERIAL_BAUD_RATE)Irp->AssociatedIrp.SystemBuffer;
  476. if (IrpSp->Parameters.DeviceIoControl.OutputBufferLength <
  477. sizeof(SERIAL_BAUD_RATE)) {
  478. Status = STATUS_BUFFER_TOO_SMALL;
  479. break;
  480. }
  481. KeAcquireSpinLock(
  482. &Extension->ControlLock,
  483. &OldIrql
  484. );
  485. Br->BaudRate = Extension->CurrentBaud;
  486. KeReleaseSpinLock(
  487. &Extension->ControlLock,
  488. OldIrql
  489. );
  490. Irp->IoStatus.Information = sizeof(SERIAL_BAUD_RATE);
  491. break;
  492. }
  493. case IOCTL_SERIAL_GET_MODEM_CONTROL: {
  494. SERIAL_IOCTL_SYNC S;
  495. if (IrpSp->Parameters.DeviceIoControl.OutputBufferLength <
  496. sizeof(ULONG)) {
  497. Status = STATUS_BUFFER_TOO_SMALL;
  498. break;
  499. }
  500. Irp->IoStatus.Information = sizeof(ULONG);
  501. S.Extension = Extension;
  502. S.Data = Irp->AssociatedIrp.SystemBuffer;
  503. KeAcquireSpinLock(
  504. &Extension->ControlLock,
  505. &OldIrql
  506. );
  507. KeSynchronizeExecution(
  508. Extension->Interrupt,
  509. SerialGetMCRContents,
  510. &S
  511. );
  512. KeReleaseSpinLock(
  513. &Extension->ControlLock,
  514. OldIrql
  515. );
  516. break;
  517. }
  518. case IOCTL_SERIAL_SET_MODEM_CONTROL: {
  519. SERIAL_IOCTL_SYNC S;
  520. if (IrpSp->Parameters.DeviceIoControl.InputBufferLength <
  521. sizeof(ULONG)) {
  522. Status = STATUS_BUFFER_TOO_SMALL;
  523. break;
  524. }
  525. S.Extension = Extension;
  526. S.Data = Irp->AssociatedIrp.SystemBuffer;
  527. //
  528. // Make sure we are at power D0
  529. //
  530. if (Extension->PowerState != PowerDeviceD0) {
  531. Status = SerialGotoPowerState(Extension->Pdo, Extension,
  532. PowerDeviceD0);
  533. if (!NT_SUCCESS(Status)) {
  534. break;
  535. }
  536. }
  537. KeAcquireSpinLock(
  538. &Extension->ControlLock,
  539. &OldIrql
  540. );
  541. KeSynchronizeExecution(
  542. Extension->Interrupt,
  543. SerialSetMCRContents,
  544. &S
  545. );
  546. KeReleaseSpinLock(
  547. &Extension->ControlLock,
  548. OldIrql
  549. );
  550. break;
  551. }
  552. case IOCTL_SERIAL_SET_FIFO_CONTROL: {
  553. SERIAL_IOCTL_SYNC S;
  554. if (IrpSp->Parameters.DeviceIoControl.InputBufferLength <
  555. sizeof(ULONG)) {
  556. Status = STATUS_BUFFER_TOO_SMALL;
  557. break;
  558. }
  559. S.Extension = Extension;
  560. S.Data = Irp->AssociatedIrp.SystemBuffer;
  561. //
  562. // Make sure we are at power D0
  563. //
  564. if (Extension->PowerState != PowerDeviceD0) {
  565. Status = SerialGotoPowerState(Extension->Pdo, Extension,
  566. PowerDeviceD0);
  567. if (!NT_SUCCESS(Status)) {
  568. break;
  569. }
  570. }
  571. KeAcquireSpinLock(
  572. &Extension->ControlLock,
  573. &OldIrql
  574. );
  575. KeSynchronizeExecution(
  576. Extension->Interrupt,
  577. SerialSetFCRContents,
  578. &S
  579. );
  580. KeReleaseSpinLock(
  581. &Extension->ControlLock,
  582. OldIrql
  583. );
  584. break;
  585. }
  586. case IOCTL_SERIAL_SET_LINE_CONTROL: {
  587. //
  588. // Points to the line control record in the Irp.
  589. //
  590. PSERIAL_LINE_CONTROL Lc =
  591. ((PSERIAL_LINE_CONTROL)(Irp->AssociatedIrp.SystemBuffer));
  592. UCHAR LData;
  593. UCHAR LStop;
  594. UCHAR LParity;
  595. UCHAR Mask = 0xff;
  596. if (IrpSp->Parameters.DeviceIoControl.InputBufferLength <
  597. sizeof(SERIAL_LINE_CONTROL)) {
  598. Status = STATUS_BUFFER_TOO_SMALL;
  599. break;
  600. }
  601. //
  602. // Make sure we are at power D0
  603. //
  604. if (Extension->PowerState != PowerDeviceD0) {
  605. Status = SerialGotoPowerState(Extension->Pdo, Extension,
  606. PowerDeviceD0);
  607. if (!NT_SUCCESS(Status)) {
  608. break;
  609. }
  610. }
  611. switch (Lc->WordLength) {
  612. case 5: {
  613. LData = SERIAL_5_DATA;
  614. Mask = 0x1f;
  615. break;
  616. }
  617. case 6: {
  618. LData = SERIAL_6_DATA;
  619. Mask = 0x3f;
  620. break;
  621. }
  622. case 7: {
  623. LData = SERIAL_7_DATA;
  624. Mask = 0x7f;
  625. break;
  626. }
  627. case 8: {
  628. LData = SERIAL_8_DATA;
  629. break;
  630. }
  631. default: {
  632. Status = STATUS_INVALID_PARAMETER;
  633. goto DoneWithIoctl;
  634. }
  635. }
  636. Extension->WmiCommData.BitsPerByte = Lc->WordLength;
  637. switch (Lc->Parity) {
  638. case NO_PARITY: {
  639. Extension->WmiCommData.Parity = SERIAL_WMI_PARITY_NONE;
  640. LParity = SERIAL_NONE_PARITY;
  641. break;
  642. }
  643. case EVEN_PARITY: {
  644. Extension->WmiCommData.Parity = SERIAL_WMI_PARITY_EVEN;
  645. LParity = SERIAL_EVEN_PARITY;
  646. break;
  647. }
  648. case ODD_PARITY: {
  649. Extension->WmiCommData.Parity = SERIAL_WMI_PARITY_ODD;
  650. LParity = SERIAL_ODD_PARITY;
  651. break;
  652. }
  653. #if defined(NEC_98)
  654. //
  655. // COM1 of PC-9800 series is not support MarkParity and SpaceParity.
  656. //
  657. #else
  658. case SPACE_PARITY: {
  659. Extension->WmiCommData.Parity = SERIAL_WMI_PARITY_SPACE;
  660. LParity = SERIAL_SPACE_PARITY;
  661. break;
  662. }
  663. case MARK_PARITY: {
  664. Extension->WmiCommData.Parity = SERIAL_WMI_PARITY_MARK;
  665. LParity = SERIAL_MARK_PARITY;
  666. break;
  667. }
  668. #endif //defined(NEC_98)
  669. default: {
  670. Status = STATUS_INVALID_PARAMETER;
  671. goto DoneWithIoctl;
  672. break;
  673. }
  674. }
  675. switch (Lc->StopBits) {
  676. case STOP_BIT_1: {
  677. Extension->WmiCommData.StopBits = SERIAL_WMI_STOP_1;
  678. LStop = SERIAL_1_STOP;
  679. break;
  680. }
  681. case STOP_BITS_1_5: {
  682. if (LData != SERIAL_5_DATA) {
  683. Status = STATUS_INVALID_PARAMETER;
  684. goto DoneWithIoctl;
  685. }
  686. Extension->WmiCommData.StopBits = SERIAL_WMI_STOP_1_5;
  687. LStop = SERIAL_1_5_STOP;
  688. break;
  689. }
  690. case STOP_BITS_2: {
  691. if (LData == SERIAL_5_DATA) {
  692. Status = STATUS_INVALID_PARAMETER;
  693. goto DoneWithIoctl;
  694. }
  695. Extension->WmiCommData.StopBits = SERIAL_WMI_STOP_2;
  696. LStop = SERIAL_2_STOP;
  697. break;
  698. }
  699. default: {
  700. Status = STATUS_INVALID_PARAMETER;
  701. goto DoneWithIoctl;
  702. }
  703. }
  704. KeAcquireSpinLock(
  705. &Extension->ControlLock,
  706. &OldIrql
  707. );
  708. Extension->LineControl =
  709. (UCHAR)((Extension->LineControl & SERIAL_LCR_BREAK) |
  710. (LData | LParity | LStop));
  711. Extension->ValidDataMask = Mask;
  712. KeSynchronizeExecution(
  713. Extension->Interrupt,
  714. SerialSetLineControl,
  715. Extension
  716. );
  717. KeReleaseSpinLock(
  718. &Extension->ControlLock,
  719. OldIrql
  720. );
  721. break;
  722. }
  723. case IOCTL_SERIAL_GET_LINE_CONTROL: {
  724. PSERIAL_LINE_CONTROL Lc = (PSERIAL_LINE_CONTROL)Irp->AssociatedIrp.SystemBuffer;
  725. if (IrpSp->Parameters.DeviceIoControl.OutputBufferLength <
  726. sizeof(SERIAL_LINE_CONTROL)) {
  727. Status = STATUS_BUFFER_TOO_SMALL;
  728. break;
  729. }
  730. KeAcquireSpinLock(
  731. &Extension->ControlLock,
  732. &OldIrql
  733. );
  734. if ((Extension->LineControl & SERIAL_DATA_MASK) == SERIAL_5_DATA) {
  735. Lc->WordLength = 5;
  736. } else if ((Extension->LineControl & SERIAL_DATA_MASK)
  737. == SERIAL_6_DATA) {
  738. Lc->WordLength = 6;
  739. } else if ((Extension->LineControl & SERIAL_DATA_MASK)
  740. == SERIAL_7_DATA) {
  741. Lc->WordLength = 7;
  742. } else if ((Extension->LineControl & SERIAL_DATA_MASK)
  743. == SERIAL_8_DATA) {
  744. Lc->WordLength = 8;
  745. }
  746. if ((Extension->LineControl & SERIAL_PARITY_MASK)
  747. == SERIAL_NONE_PARITY) {
  748. Lc->Parity = NO_PARITY;
  749. } else if ((Extension->LineControl & SERIAL_PARITY_MASK)
  750. == SERIAL_ODD_PARITY) {
  751. Lc->Parity = ODD_PARITY;
  752. } else if ((Extension->LineControl & SERIAL_PARITY_MASK)
  753. == SERIAL_EVEN_PARITY) {
  754. Lc->Parity = EVEN_PARITY;
  755. } else if ((Extension->LineControl & SERIAL_PARITY_MASK)
  756. == SERIAL_MARK_PARITY) {
  757. Lc->Parity = MARK_PARITY;
  758. } else if ((Extension->LineControl & SERIAL_PARITY_MASK)
  759. == SERIAL_SPACE_PARITY) {
  760. Lc->Parity = SPACE_PARITY;
  761. }
  762. if (Extension->LineControl & SERIAL_2_STOP) {
  763. if (Lc->WordLength == 5) {
  764. Lc->StopBits = STOP_BITS_1_5;
  765. } else {
  766. Lc->StopBits = STOP_BITS_2;
  767. }
  768. } else {
  769. Lc->StopBits = STOP_BIT_1;
  770. }
  771. Irp->IoStatus.Information = sizeof(SERIAL_LINE_CONTROL);
  772. KeReleaseSpinLock(
  773. &Extension->ControlLock,
  774. OldIrql
  775. );
  776. break;
  777. }
  778. case IOCTL_SERIAL_SET_TIMEOUTS: {
  779. PSERIAL_TIMEOUTS NewTimeouts =
  780. ((PSERIAL_TIMEOUTS)(Irp->AssociatedIrp.SystemBuffer));
  781. if (IrpSp->Parameters.DeviceIoControl.InputBufferLength <
  782. sizeof(SERIAL_TIMEOUTS)) {
  783. Status = STATUS_BUFFER_TOO_SMALL;
  784. break;
  785. }
  786. if ((NewTimeouts->ReadIntervalTimeout == MAXULONG) &&
  787. (NewTimeouts->ReadTotalTimeoutMultiplier == MAXULONG) &&
  788. (NewTimeouts->ReadTotalTimeoutConstant == MAXULONG)) {
  789. Status = STATUS_INVALID_PARAMETER;
  790. break;
  791. }
  792. KeAcquireSpinLock(
  793. &Extension->ControlLock,
  794. &OldIrql
  795. );
  796. Extension->Timeouts.ReadIntervalTimeout =
  797. NewTimeouts->ReadIntervalTimeout;
  798. Extension->Timeouts.ReadTotalTimeoutMultiplier =
  799. NewTimeouts->ReadTotalTimeoutMultiplier;
  800. Extension->Timeouts.ReadTotalTimeoutConstant =
  801. NewTimeouts->ReadTotalTimeoutConstant;
  802. Extension->Timeouts.WriteTotalTimeoutMultiplier =
  803. NewTimeouts->WriteTotalTimeoutMultiplier;
  804. Extension->Timeouts.WriteTotalTimeoutConstant =
  805. NewTimeouts->WriteTotalTimeoutConstant;
  806. KeReleaseSpinLock(
  807. &Extension->ControlLock,
  808. OldIrql
  809. );
  810. break;
  811. }
  812. case IOCTL_SERIAL_GET_TIMEOUTS: {
  813. if (IrpSp->Parameters.DeviceIoControl.OutputBufferLength <
  814. sizeof(SERIAL_TIMEOUTS)) {
  815. Status = STATUS_BUFFER_TOO_SMALL;
  816. break;
  817. }
  818. KeAcquireSpinLock(
  819. &Extension->ControlLock,
  820. &OldIrql
  821. );
  822. *((PSERIAL_TIMEOUTS)Irp->AssociatedIrp.SystemBuffer) = Extension->Timeouts;
  823. Irp->IoStatus.Information = sizeof(SERIAL_TIMEOUTS);
  824. KeReleaseSpinLock(
  825. &Extension->ControlLock,
  826. OldIrql
  827. );
  828. break;
  829. }
  830. case IOCTL_SERIAL_SET_CHARS: {
  831. SERIAL_IOCTL_SYNC S;
  832. PSERIAL_CHARS NewChars =
  833. ((PSERIAL_CHARS)(Irp->AssociatedIrp.SystemBuffer));
  834. if (IrpSp->Parameters.DeviceIoControl.InputBufferLength <
  835. sizeof(SERIAL_CHARS)) {
  836. Status = STATUS_BUFFER_TOO_SMALL;
  837. break;
  838. }
  839. //
  840. // The only thing that can be wrong with the chars
  841. // is that the xon and xoff characters are the
  842. // same.
  843. //
  844. #if 0
  845. if (NewChars->XonChar == NewChars->XoffChar) {
  846. Status = STATUS_INVALID_PARAMETER;
  847. break;
  848. }
  849. #endif
  850. //
  851. // We acquire the control lock so that only
  852. // one request can GET or SET the characters
  853. // at a time. The sets could be synchronized
  854. // by the interrupt spinlock, but that wouldn't
  855. // prevent multiple gets at the same time.
  856. //
  857. S.Extension = Extension;
  858. S.Data = NewChars;
  859. KeAcquireSpinLock(
  860. &Extension->ControlLock,
  861. &OldIrql
  862. );
  863. //
  864. // Under the protection of the lock, make sure that
  865. // the xon and xoff characters aren't the same as
  866. // the escape character.
  867. //
  868. if (Extension->EscapeChar) {
  869. if ((Extension->EscapeChar == NewChars->XonChar) ||
  870. (Extension->EscapeChar == NewChars->XoffChar)) {
  871. Status = STATUS_INVALID_PARAMETER;
  872. KeReleaseSpinLock(
  873. &Extension->ControlLock,
  874. OldIrql
  875. );
  876. break;
  877. }
  878. }
  879. Extension->WmiCommData.XonCharacter = NewChars->XonChar;
  880. Extension->WmiCommData.XoffCharacter = NewChars->XoffChar;
  881. KeSynchronizeExecution(
  882. Extension->Interrupt,
  883. SerialSetChars,
  884. &S
  885. );
  886. KeReleaseSpinLock(
  887. &Extension->ControlLock,
  888. OldIrql
  889. );
  890. break;
  891. }
  892. case IOCTL_SERIAL_GET_CHARS: {
  893. if (IrpSp->Parameters.DeviceIoControl.OutputBufferLength <
  894. sizeof(SERIAL_CHARS)) {
  895. Status = STATUS_BUFFER_TOO_SMALL;
  896. break;
  897. }
  898. KeAcquireSpinLock(
  899. &Extension->ControlLock,
  900. &OldIrql
  901. );
  902. *((PSERIAL_CHARS)Irp->AssociatedIrp.SystemBuffer) = Extension->SpecialChars;
  903. Irp->IoStatus.Information = sizeof(SERIAL_CHARS);
  904. KeReleaseSpinLock(
  905. &Extension->ControlLock,
  906. OldIrql
  907. );
  908. break;
  909. }
  910. case IOCTL_SERIAL_SET_DTR:
  911. case IOCTL_SERIAL_CLR_DTR: {
  912. //
  913. // Make sure we are at power D0
  914. //
  915. if (Extension->PowerState != PowerDeviceD0) {
  916. Status = SerialGotoPowerState(Extension->Pdo, Extension,
  917. PowerDeviceD0);
  918. if (!NT_SUCCESS(Status)) {
  919. break;
  920. }
  921. }
  922. //
  923. // We acquire the lock so that we can check whether
  924. // automatic dtr flow control is enabled. If it is
  925. // then we return an error since the app is not allowed
  926. // to touch this if it is automatic.
  927. //
  928. KeAcquireSpinLock(
  929. &Extension->ControlLock,
  930. &OldIrql
  931. );
  932. if ((Extension->HandFlow.ControlHandShake & SERIAL_DTR_MASK)
  933. == SERIAL_DTR_HANDSHAKE) {
  934. Status = STATUS_INVALID_PARAMETER;
  935. } else {
  936. KeSynchronizeExecution(
  937. Extension->Interrupt,
  938. ((IrpSp->Parameters.DeviceIoControl.IoControlCode ==
  939. IOCTL_SERIAL_SET_DTR)?
  940. (SerialSetDTR):(SerialClrDTR)),
  941. Extension
  942. );
  943. }
  944. KeReleaseSpinLock(
  945. &Extension->ControlLock,
  946. OldIrql
  947. );
  948. break;
  949. }
  950. case IOCTL_SERIAL_RESET_DEVICE: {
  951. break;
  952. }
  953. case IOCTL_SERIAL_SET_RTS:
  954. case IOCTL_SERIAL_CLR_RTS: {
  955. //
  956. // Make sure we are at power D0
  957. //
  958. if (Extension->PowerState != PowerDeviceD0) {
  959. Status = SerialGotoPowerState(Extension->Pdo, Extension,
  960. PowerDeviceD0);
  961. if (!NT_SUCCESS(Status)) {
  962. break;
  963. }
  964. }
  965. //
  966. // We acquire the lock so that we can check whether
  967. // automatic rts flow control or transmit toggleing
  968. // is enabled. If it is then we return an error since
  969. // the app is not allowed to touch this if it is automatic
  970. // or toggling.
  971. //
  972. KeAcquireSpinLock(
  973. &Extension->ControlLock,
  974. &OldIrql
  975. );
  976. if (((Extension->HandFlow.FlowReplace & SERIAL_RTS_MASK)
  977. == SERIAL_RTS_HANDSHAKE) ||
  978. ((Extension->HandFlow.FlowReplace & SERIAL_RTS_MASK)
  979. == SERIAL_TRANSMIT_TOGGLE)) {
  980. Status = STATUS_INVALID_PARAMETER;
  981. } else {
  982. KeSynchronizeExecution(
  983. Extension->Interrupt,
  984. ((IrpSp->Parameters.DeviceIoControl.IoControlCode ==
  985. IOCTL_SERIAL_SET_RTS)?
  986. (SerialSetRTS):(SerialClrRTS)),
  987. Extension
  988. );
  989. }
  990. KeReleaseSpinLock(
  991. &Extension->ControlLock,
  992. OldIrql
  993. );
  994. break;
  995. }
  996. case IOCTL_SERIAL_SET_XOFF: {
  997. KeSynchronizeExecution(
  998. Extension->Interrupt,
  999. SerialPretendXoff,
  1000. Extension
  1001. );
  1002. break;
  1003. }
  1004. case IOCTL_SERIAL_SET_XON: {
  1005. KeSynchronizeExecution(
  1006. Extension->Interrupt,
  1007. SerialPretendXon,
  1008. Extension
  1009. );
  1010. break;
  1011. }
  1012. case IOCTL_SERIAL_SET_BREAK_ON: {
  1013. //
  1014. // Make sure we are at power D0
  1015. //
  1016. if (Extension->PowerState != PowerDeviceD0) {
  1017. Status = SerialGotoPowerState(Extension->Pdo, Extension,
  1018. PowerDeviceD0);
  1019. if (!NT_SUCCESS(Status)) {
  1020. break;
  1021. }
  1022. }
  1023. KeSynchronizeExecution(
  1024. Extension->Interrupt,
  1025. SerialTurnOnBreak,
  1026. Extension
  1027. );
  1028. break;
  1029. }
  1030. case IOCTL_SERIAL_SET_BREAK_OFF: {
  1031. //
  1032. // Make sure we are at power D0
  1033. //
  1034. if (Extension->PowerState != PowerDeviceD0) {
  1035. Status = SerialGotoPowerState(Extension->Pdo, Extension,
  1036. PowerDeviceD0);
  1037. if (!NT_SUCCESS(Status)) {
  1038. break;
  1039. }
  1040. }
  1041. KeSynchronizeExecution(
  1042. Extension->Interrupt,
  1043. SerialTurnOffBreak,
  1044. Extension
  1045. );
  1046. break;
  1047. }
  1048. case IOCTL_SERIAL_SET_QUEUE_SIZE: {
  1049. //
  1050. // Type ahead buffer is fixed, so we just validate
  1051. // the the users request is not bigger that our
  1052. // own internal buffer size.
  1053. //
  1054. PSERIAL_QUEUE_SIZE Rs =
  1055. ((PSERIAL_QUEUE_SIZE)(Irp->AssociatedIrp.SystemBuffer));
  1056. if (IrpSp->Parameters.DeviceIoControl.InputBufferLength <
  1057. sizeof(SERIAL_QUEUE_SIZE)) {
  1058. Status = STATUS_BUFFER_TOO_SMALL;
  1059. break;
  1060. }
  1061. //
  1062. // We have to allocate the memory for the new
  1063. // buffer while we're still in the context of the
  1064. // caller. We don't even try to protect this
  1065. // with a lock because the value could be stale
  1066. // as soon as we release the lock - The only time
  1067. // we will know for sure is when we actually try
  1068. // to do the resize.
  1069. //
  1070. if (Rs->InSize <= Extension->BufferSize) {
  1071. Status = STATUS_SUCCESS;
  1072. break;
  1073. }
  1074. try {
  1075. IrpSp->Parameters.DeviceIoControl.Type3InputBuffer =
  1076. ExAllocatePoolWithQuota(
  1077. NonPagedPool,
  1078. Rs->InSize
  1079. );
  1080. } except (EXCEPTION_EXECUTE_HANDLER) {
  1081. IrpSp->Parameters.DeviceIoControl.Type3InputBuffer = NULL;
  1082. Status = GetExceptionCode();
  1083. }
  1084. if (!IrpSp->Parameters.DeviceIoControl.Type3InputBuffer) {
  1085. break;
  1086. }
  1087. //
  1088. // Well the data passed was big enough. Do the request.
  1089. //
  1090. // There are two reason we place it in the read queue:
  1091. //
  1092. // 1) We want to serialize these resize requests so that
  1093. // they don't contend with each other.
  1094. //
  1095. // 2) We want to serialize these requests with reads since
  1096. // we don't want reads and resizes contending over the
  1097. // read buffer.
  1098. //
  1099. return SerialStartOrQueue(
  1100. Extension,
  1101. Irp,
  1102. &Extension->ReadQueue,
  1103. &Extension->CurrentReadIrp,
  1104. SerialStartRead
  1105. );
  1106. break;
  1107. }
  1108. case IOCTL_SERIAL_GET_WAIT_MASK: {
  1109. if (IrpSp->Parameters.DeviceIoControl.OutputBufferLength <
  1110. sizeof(ULONG)) {
  1111. Status = STATUS_BUFFER_TOO_SMALL;
  1112. break;
  1113. }
  1114. //
  1115. // Simple scalar read. No reason to acquire a lock.
  1116. //
  1117. Irp->IoStatus.Information = sizeof(ULONG);
  1118. *((ULONG *)Irp->AssociatedIrp.SystemBuffer) = Extension->IsrWaitMask;
  1119. break;
  1120. }
  1121. case IOCTL_SERIAL_SET_WAIT_MASK: {
  1122. ULONG NewMask;
  1123. SerialDump(
  1124. SERDIAG3 | SERIRPPATH,
  1125. ("SERIAL: In Ioctl processing for set mask\n")
  1126. );
  1127. if (IrpSp->Parameters.DeviceIoControl.InputBufferLength <
  1128. sizeof(ULONG)) {
  1129. SerialDump(
  1130. SERDIAG3,
  1131. ("SERIAL: Invalid size fo the buffer %d\n",
  1132. IrpSp->Parameters.DeviceIoControl.InputBufferLength)
  1133. );
  1134. Status = STATUS_BUFFER_TOO_SMALL;
  1135. break;
  1136. } else {
  1137. NewMask = *((ULONG *)Irp->AssociatedIrp.SystemBuffer);
  1138. }
  1139. //
  1140. // Make sure that the mask only contains valid
  1141. // waitable events.
  1142. //
  1143. if (NewMask & ~(SERIAL_EV_RXCHAR |
  1144. SERIAL_EV_RXFLAG |
  1145. SERIAL_EV_TXEMPTY |
  1146. SERIAL_EV_CTS |
  1147. SERIAL_EV_DSR |
  1148. SERIAL_EV_RLSD |
  1149. SERIAL_EV_BREAK |
  1150. SERIAL_EV_ERR |
  1151. SERIAL_EV_RING |
  1152. SERIAL_EV_PERR |
  1153. SERIAL_EV_RX80FULL |
  1154. SERIAL_EV_EVENT1 |
  1155. SERIAL_EV_EVENT2)) {
  1156. SerialDump(
  1157. SERDIAG3,
  1158. ("SERIAL: Unknown mask %x\n",NewMask)
  1159. );
  1160. Status = STATUS_INVALID_PARAMETER;
  1161. break;
  1162. }
  1163. //
  1164. // Either start this irp or put it on the
  1165. // queue.
  1166. //
  1167. SerialDump(
  1168. SERDIAG3 | SERIRPPATH,
  1169. ("SERIAL: Starting or queuing set mask irp %x\n",Irp)
  1170. );
  1171. return SerialStartOrQueue(
  1172. Extension,
  1173. Irp,
  1174. &Extension->MaskQueue,
  1175. &Extension->CurrentMaskIrp,
  1176. SerialStartMask
  1177. );
  1178. }
  1179. case IOCTL_SERIAL_WAIT_ON_MASK: {
  1180. SerialDump(
  1181. SERDIAG3 | SERIRPPATH,
  1182. ("SERIAL: In Ioctl processing for wait mask\n")
  1183. );
  1184. if (IrpSp->Parameters.DeviceIoControl.OutputBufferLength <
  1185. sizeof(ULONG)) {
  1186. SerialDump(
  1187. SERDIAG3,
  1188. ("SERIAL: Invalid size fo the buffer %d\n",
  1189. IrpSp->Parameters.DeviceIoControl.InputBufferLength)
  1190. );
  1191. Status = STATUS_BUFFER_TOO_SMALL;
  1192. break;
  1193. }
  1194. //
  1195. // Either start this irp or put it on the
  1196. // queue.
  1197. //
  1198. SerialDump(
  1199. SERDIAG3 | SERIRPPATH,
  1200. ("SERIAL: Starting or queuing wait mask irp %x\n",Irp)
  1201. );
  1202. return SerialStartOrQueue(
  1203. Extension,
  1204. Irp,
  1205. &Extension->MaskQueue,
  1206. &Extension->CurrentMaskIrp,
  1207. SerialStartMask
  1208. );
  1209. }
  1210. case IOCTL_SERIAL_IMMEDIATE_CHAR: {
  1211. KIRQL OldIrql;
  1212. if (IrpSp->Parameters.DeviceIoControl.InputBufferLength <
  1213. sizeof(UCHAR)) {
  1214. Status = STATUS_BUFFER_TOO_SMALL;
  1215. break;
  1216. }
  1217. IoAcquireCancelSpinLock(&OldIrql);
  1218. if (Extension->CurrentImmediateIrp) {
  1219. Status = STATUS_INVALID_PARAMETER;
  1220. IoReleaseCancelSpinLock(OldIrql);
  1221. } else {
  1222. //
  1223. // We can queue the char. We need to set
  1224. // a cancel routine because flow control could
  1225. // keep the char from transmitting. Make sure
  1226. // that the irp hasn't already been canceled.
  1227. //
  1228. if (Irp->Cancel) {
  1229. IoReleaseCancelSpinLock(OldIrql);
  1230. Status = STATUS_CANCELLED;
  1231. } else {
  1232. Extension->CurrentImmediateIrp = Irp;
  1233. Extension->TotalCharsQueued++;
  1234. IoReleaseCancelSpinLock(OldIrql);
  1235. SerialStartImmediate(Extension);
  1236. return STATUS_PENDING;
  1237. }
  1238. }
  1239. break;
  1240. }
  1241. case IOCTL_SERIAL_PURGE: {
  1242. ULONG Mask;
  1243. if (IrpSp->Parameters.DeviceIoControl.InputBufferLength <
  1244. sizeof(ULONG)) {
  1245. Status = STATUS_BUFFER_TOO_SMALL;
  1246. break;
  1247. }
  1248. //
  1249. // Check to make sure that the mask only has
  1250. // 0 or the other appropriate values.
  1251. //
  1252. Mask = *((ULONG *)(Irp->AssociatedIrp.SystemBuffer));
  1253. if ((!Mask) || (Mask & (~(SERIAL_PURGE_TXABORT |
  1254. SERIAL_PURGE_RXABORT |
  1255. SERIAL_PURGE_TXCLEAR |
  1256. SERIAL_PURGE_RXCLEAR
  1257. )
  1258. )
  1259. )) {
  1260. Status = STATUS_INVALID_PARAMETER;
  1261. break;
  1262. }
  1263. //
  1264. // Either start this irp or put it on the
  1265. // queue.
  1266. //
  1267. return SerialStartOrQueue(
  1268. Extension,
  1269. Irp,
  1270. &Extension->PurgeQueue,
  1271. &Extension->CurrentPurgeIrp,
  1272. SerialStartPurge
  1273. );
  1274. }
  1275. case IOCTL_SERIAL_GET_HANDFLOW: {
  1276. if (IrpSp->Parameters.DeviceIoControl.OutputBufferLength <
  1277. sizeof(SERIAL_HANDFLOW)) {
  1278. Status = STATUS_BUFFER_TOO_SMALL;
  1279. break;
  1280. }
  1281. Irp->IoStatus.Information = sizeof(SERIAL_HANDFLOW);
  1282. KeAcquireSpinLock(
  1283. &Extension->ControlLock,
  1284. &OldIrql
  1285. );
  1286. *((PSERIAL_HANDFLOW)Irp->AssociatedIrp.SystemBuffer) =
  1287. Extension->HandFlow;
  1288. KeReleaseSpinLock(
  1289. &Extension->ControlLock,
  1290. OldIrql
  1291. );
  1292. break;
  1293. }
  1294. case IOCTL_SERIAL_SET_HANDFLOW: {
  1295. SERIAL_IOCTL_SYNC S;
  1296. PSERIAL_HANDFLOW HandFlow = Irp->AssociatedIrp.SystemBuffer;
  1297. //
  1298. // Make sure that the hand shake and control is the
  1299. // right size.
  1300. //
  1301. if (IrpSp->Parameters.DeviceIoControl.InputBufferLength <
  1302. sizeof(SERIAL_HANDFLOW)) {
  1303. Status = STATUS_BUFFER_TOO_SMALL;
  1304. break;
  1305. }
  1306. //
  1307. // Make sure that there are no invalid bits set in
  1308. // the control and handshake.
  1309. //
  1310. if (HandFlow->ControlHandShake & SERIAL_CONTROL_INVALID) {
  1311. Status = STATUS_INVALID_PARAMETER;
  1312. break;
  1313. }
  1314. if (HandFlow->FlowReplace & SERIAL_FLOW_INVALID) {
  1315. Status = STATUS_INVALID_PARAMETER;
  1316. break;
  1317. }
  1318. //
  1319. // Make sure that the app hasn't set an invlid DTR mode.
  1320. //
  1321. if ((HandFlow->ControlHandShake & SERIAL_DTR_MASK) ==
  1322. SERIAL_DTR_MASK) {
  1323. Status = STATUS_INVALID_PARAMETER;
  1324. break;
  1325. }
  1326. //
  1327. // Make sure that haven't set totally invalid xon/xoff
  1328. // limits.
  1329. //
  1330. if ((HandFlow->XonLimit < 0) ||
  1331. ((ULONG)HandFlow->XonLimit > Extension->BufferSize)) {
  1332. Status = STATUS_INVALID_PARAMETER;
  1333. break;
  1334. }
  1335. if ((HandFlow->XoffLimit < 0) ||
  1336. ((ULONG)HandFlow->XoffLimit > Extension->BufferSize)) {
  1337. Status = STATUS_INVALID_PARAMETER;
  1338. break;
  1339. }
  1340. S.Extension = Extension;
  1341. S.Data = HandFlow;
  1342. KeAcquireSpinLock(
  1343. &Extension->ControlLock,
  1344. &OldIrql
  1345. );
  1346. //
  1347. // Under the protection of the lock, make sure that
  1348. // we aren't turning on error replacement when we
  1349. // are doing line status/modem status insertion.
  1350. //
  1351. if (Extension->EscapeChar) {
  1352. if (HandFlow->FlowReplace & SERIAL_ERROR_CHAR) {
  1353. Status = STATUS_INVALID_PARAMETER;
  1354. KeReleaseSpinLock(
  1355. &Extension->ControlLock,
  1356. OldIrql
  1357. );
  1358. break;
  1359. }
  1360. }
  1361. KeSynchronizeExecution(
  1362. Extension->Interrupt,
  1363. SerialSetHandFlow,
  1364. &S
  1365. );
  1366. KeReleaseSpinLock(
  1367. &Extension->ControlLock,
  1368. OldIrql
  1369. );
  1370. break;
  1371. }
  1372. case IOCTL_SERIAL_GET_MODEMSTATUS: {
  1373. SERIAL_IOCTL_SYNC S;
  1374. if (IrpSp->Parameters.DeviceIoControl.OutputBufferLength <
  1375. sizeof(ULONG)) {
  1376. Status = STATUS_BUFFER_TOO_SMALL;
  1377. break;
  1378. }
  1379. Irp->IoStatus.Information = sizeof(ULONG);
  1380. S.Extension = Extension;
  1381. S.Data = Irp->AssociatedIrp.SystemBuffer;
  1382. KeAcquireSpinLock(
  1383. &Extension->ControlLock,
  1384. &OldIrql
  1385. );
  1386. KeSynchronizeExecution(
  1387. Extension->Interrupt,
  1388. SerialGetModemUpdate,
  1389. &S
  1390. );
  1391. KeReleaseSpinLock(
  1392. &Extension->ControlLock,
  1393. OldIrql
  1394. );
  1395. break;
  1396. }
  1397. case IOCTL_SERIAL_GET_DTRRTS: {
  1398. ULONG ModemControl;
  1399. if (IrpSp->Parameters.DeviceIoControl.OutputBufferLength <
  1400. sizeof(ULONG)) {
  1401. Status = STATUS_BUFFER_TOO_SMALL;
  1402. break;
  1403. }
  1404. Irp->IoStatus.Information = sizeof(ULONG);
  1405. Irp->IoStatus.Status = STATUS_SUCCESS;
  1406. //
  1407. // Reading this hardware has no effect on the device.
  1408. //
  1409. ModemControl = READ_MODEM_CONTROL(Extension->Controller);
  1410. ModemControl &= SERIAL_DTR_STATE | SERIAL_RTS_STATE;
  1411. *(PULONG)Irp->AssociatedIrp.SystemBuffer = ModemControl;
  1412. break;
  1413. }
  1414. case IOCTL_SERIAL_GET_COMMSTATUS: {
  1415. SERIAL_IOCTL_SYNC S;
  1416. if (IrpSp->Parameters.DeviceIoControl.OutputBufferLength <
  1417. sizeof(SERIAL_STATUS)) {
  1418. Status = STATUS_BUFFER_TOO_SMALL;
  1419. break;
  1420. }
  1421. Irp->IoStatus.Information = sizeof(SERIAL_STATUS);
  1422. S.Extension = Extension;
  1423. S.Data = Irp->AssociatedIrp.SystemBuffer;
  1424. //
  1425. // Acquire the cancel spin lock so nothing much
  1426. // changes while were getting the state.
  1427. //
  1428. IoAcquireCancelSpinLock(&OldIrql);
  1429. KeSynchronizeExecution(
  1430. Extension->Interrupt,
  1431. SerialGetCommStatus,
  1432. &S
  1433. );
  1434. IoReleaseCancelSpinLock(OldIrql);
  1435. break;
  1436. }
  1437. case IOCTL_SERIAL_GET_PROPERTIES: {
  1438. if (IrpSp->Parameters.DeviceIoControl.OutputBufferLength <
  1439. sizeof(SERIAL_COMMPROP)) {
  1440. Status = STATUS_BUFFER_TOO_SMALL;
  1441. break;
  1442. }
  1443. //
  1444. // No synchronization is required since this information
  1445. // is "static".
  1446. //
  1447. SerialGetProperties(
  1448. Extension,
  1449. Irp->AssociatedIrp.SystemBuffer
  1450. );
  1451. Irp->IoStatus.Information = sizeof(SERIAL_COMMPROP);
  1452. Irp->IoStatus.Status = STATUS_SUCCESS;
  1453. break;
  1454. }
  1455. case IOCTL_SERIAL_XOFF_COUNTER: {
  1456. PSERIAL_XOFF_COUNTER Xc = Irp->AssociatedIrp.SystemBuffer;
  1457. if (IrpSp->Parameters.DeviceIoControl.InputBufferLength <
  1458. sizeof(SERIAL_XOFF_COUNTER)) {
  1459. Status = STATUS_BUFFER_TOO_SMALL;
  1460. break;
  1461. }
  1462. if (Xc->Counter <= 0) {
  1463. Status = STATUS_INVALID_PARAMETER;
  1464. break;
  1465. }
  1466. //
  1467. // So far so good. Put the irp onto the write queue.
  1468. //
  1469. return SerialStartOrQueue(
  1470. Extension,
  1471. Irp,
  1472. &Extension->WriteQueue,
  1473. &Extension->CurrentWriteIrp,
  1474. SerialStartWrite
  1475. );
  1476. }
  1477. case IOCTL_SERIAL_LSRMST_INSERT: {
  1478. PUCHAR escapeChar = Irp->AssociatedIrp.SystemBuffer;
  1479. SERIAL_IOCTL_SYNC S;
  1480. //
  1481. // Make sure we get a byte.
  1482. //
  1483. if (IrpSp->Parameters.DeviceIoControl.InputBufferLength <
  1484. sizeof(UCHAR)) {
  1485. Status = STATUS_BUFFER_TOO_SMALL;
  1486. break;
  1487. }
  1488. KeAcquireSpinLock(
  1489. &Extension->ControlLock,
  1490. &OldIrql
  1491. );
  1492. if (*escapeChar) {
  1493. //
  1494. // We've got some escape work to do. We will make sure that
  1495. // the character is not the same as the Xon or Xoff character,
  1496. // or that we are already doing error replacement.
  1497. //
  1498. if ((*escapeChar == Extension->SpecialChars.XoffChar) ||
  1499. (*escapeChar == Extension->SpecialChars.XonChar) ||
  1500. (Extension->HandFlow.FlowReplace & SERIAL_ERROR_CHAR)) {
  1501. Status = STATUS_INVALID_PARAMETER;
  1502. KeReleaseSpinLock(
  1503. &Extension->ControlLock,
  1504. OldIrql
  1505. );
  1506. break;
  1507. }
  1508. }
  1509. S.Extension = Extension;
  1510. S.Data = Irp->AssociatedIrp.SystemBuffer;
  1511. KeSynchronizeExecution(
  1512. Extension->Interrupt,
  1513. SerialSetEscapeChar,
  1514. Irp
  1515. );
  1516. KeReleaseSpinLock(
  1517. &Extension->ControlLock,
  1518. OldIrql
  1519. );
  1520. break;
  1521. }
  1522. case IOCTL_SERIAL_CONFIG_SIZE: {
  1523. if (IrpSp->Parameters.DeviceIoControl.OutputBufferLength <
  1524. sizeof(ULONG)) {
  1525. Status = STATUS_BUFFER_TOO_SMALL;
  1526. break;
  1527. }
  1528. Irp->IoStatus.Information = sizeof(ULONG);
  1529. Irp->IoStatus.Status = STATUS_SUCCESS;
  1530. *(PULONG)Irp->AssociatedIrp.SystemBuffer = 0;
  1531. break;
  1532. }
  1533. case IOCTL_SERIAL_GET_STATS: {
  1534. if (IrpSp->Parameters.DeviceIoControl.OutputBufferLength <
  1535. sizeof(SERIALPERF_STATS)) {
  1536. Status = STATUS_BUFFER_TOO_SMALL;
  1537. break;
  1538. }
  1539. Irp->IoStatus.Information = sizeof(SERIALPERF_STATS);
  1540. Irp->IoStatus.Status = STATUS_SUCCESS;
  1541. KeSynchronizeExecution(
  1542. Extension->Interrupt,
  1543. SerialGetStats,
  1544. Irp
  1545. );
  1546. break;
  1547. }
  1548. case IOCTL_SERIAL_CLEAR_STATS: {
  1549. KeSynchronizeExecution(
  1550. Extension->Interrupt,
  1551. SerialClearStats,
  1552. Extension
  1553. );
  1554. break;
  1555. }
  1556. default: {
  1557. Status = STATUS_INVALID_PARAMETER;
  1558. break;
  1559. }
  1560. }
  1561. DoneWithIoctl:;
  1562. Irp->IoStatus.Status = Status;
  1563. SerialDump(
  1564. SERIRPPATH,
  1565. ("SERIAL: Complete Irp: %x\n",Irp)
  1566. );
  1567. SerialCompleteRequest(Extension, Irp, 0);
  1568. return Status;
  1569. }
  1570. VOID
  1571. SerialGetProperties(
  1572. IN PSERIAL_DEVICE_EXTENSION Extension,
  1573. IN PSERIAL_COMMPROP Properties
  1574. )
  1575. /*++
  1576. Routine Description:
  1577. This function returns the capabilities of this particular
  1578. serial device.
  1579. Arguments:
  1580. Extension - The serial device extension.
  1581. Properties - The structure used to return the properties
  1582. Return Value:
  1583. None.
  1584. --*/
  1585. {
  1586. SERIAL_LOCKED_PAGED_CODE();
  1587. RtlZeroMemory(
  1588. Properties,
  1589. sizeof(SERIAL_COMMPROP)
  1590. );
  1591. Properties->PacketLength = sizeof(SERIAL_COMMPROP);
  1592. Properties->PacketVersion = 2;
  1593. Properties->ServiceMask = SERIAL_SP_SERIALCOMM;
  1594. Properties->MaxTxQueue = 0;
  1595. Properties->MaxRxQueue = 0;
  1596. Properties->MaxBaud = SERIAL_BAUD_USER;
  1597. Properties->SettableBaud = Extension->SupportedBauds;
  1598. Properties->ProvSubType = SERIAL_SP_RS232;
  1599. Properties->ProvCapabilities = SERIAL_PCF_DTRDSR |
  1600. SERIAL_PCF_RTSCTS |
  1601. SERIAL_PCF_CD |
  1602. SERIAL_PCF_PARITY_CHECK |
  1603. SERIAL_PCF_XONXOFF |
  1604. SERIAL_PCF_SETXCHAR |
  1605. SERIAL_PCF_TOTALTIMEOUTS |
  1606. SERIAL_PCF_INTTIMEOUTS;
  1607. Properties->SettableParams = SERIAL_SP_PARITY |
  1608. SERIAL_SP_BAUD |
  1609. SERIAL_SP_DATABITS |
  1610. SERIAL_SP_STOPBITS |
  1611. SERIAL_SP_HANDSHAKING |
  1612. SERIAL_SP_PARITY_CHECK |
  1613. SERIAL_SP_CARRIER_DETECT;
  1614. Properties->SettableData = SERIAL_DATABITS_5 |
  1615. SERIAL_DATABITS_6 |
  1616. SERIAL_DATABITS_7 |
  1617. SERIAL_DATABITS_8;
  1618. #if defined(NEC_98)
  1619. //
  1620. // COM1 of PC-9800 series is not support MarkParity and SpaceParity.
  1621. //
  1622. Properties->SettableStopParity = SERIAL_STOPBITS_10 |
  1623. SERIAL_STOPBITS_15 |
  1624. SERIAL_STOPBITS_20 |
  1625. SERIAL_PARITY_NONE |
  1626. SERIAL_PARITY_ODD |
  1627. SERIAL_PARITY_EVEN;
  1628. #else
  1629. Properties->SettableStopParity = SERIAL_STOPBITS_10 |
  1630. SERIAL_STOPBITS_15 |
  1631. SERIAL_STOPBITS_20 |
  1632. SERIAL_PARITY_NONE |
  1633. SERIAL_PARITY_ODD |
  1634. SERIAL_PARITY_EVEN |
  1635. SERIAL_PARITY_MARK |
  1636. SERIAL_PARITY_SPACE;
  1637. #endif //defined(NEC_98)
  1638. Properties->CurrentTxQueue = 0;
  1639. Properties->CurrentRxQueue = Extension->BufferSize;
  1640. }
  1641. NTSTATUS
  1642. SerialInternalIoControl(IN PDEVICE_OBJECT PDevObj, IN PIRP PIrp)
  1643. /*++
  1644. Routine Description:
  1645. This routine provides the initial processing for all of the
  1646. internal Ioctrls for the serial device.
  1647. Arguments:
  1648. PDevObj - Pointer to the device object for this device
  1649. PIrp - Pointer to the IRP for the current request
  1650. Return Value:
  1651. The function value is the final status of the call
  1652. --*/
  1653. {
  1654. //
  1655. // The status that gets returned to the caller and
  1656. // set in the Irp.
  1657. //
  1658. NTSTATUS status;
  1659. //
  1660. // The current stack location. This contains all of the
  1661. // information we need to process this particular request.
  1662. //
  1663. PIO_STACK_LOCATION pIrpStack;
  1664. //
  1665. // Just what it says. This is the serial specific device
  1666. // extension of the device object create for the serial driver.
  1667. //
  1668. PSERIAL_DEVICE_EXTENSION pDevExt = PDevObj->DeviceExtension;
  1669. //
  1670. // A temporary to hold the old IRQL so that it can be
  1671. // restored once we complete/validate this request.
  1672. //
  1673. KIRQL OldIrql;
  1674. NTSTATUS prologueStatus;
  1675. SERIAL_LOCKED_PAGED_CODE();
  1676. if ((prologueStatus = SerialIRPPrologue(PIrp, pDevExt))
  1677. != STATUS_SUCCESS) {
  1678. SerialCompleteRequest(pDevExt, PIrp, IO_NO_INCREMENT);
  1679. return prologueStatus;
  1680. }
  1681. SerialDump(SERIRPPATH, ("SERIAL: Dispatch entry for: %x\n", PIrp));
  1682. if (SerialCompleteIfError(PDevObj, PIrp) != STATUS_SUCCESS) {
  1683. return STATUS_CANCELLED;
  1684. }
  1685. pIrpStack = IoGetCurrentIrpStackLocation(PIrp);
  1686. PIrp->IoStatus.Information = 0L;
  1687. status = STATUS_SUCCESS;
  1688. switch (pIrpStack->Parameters.DeviceIoControl.IoControlCode) {
  1689. //
  1690. // Send a wait-wake IRP
  1691. //
  1692. case IOCTL_SERIAL_INTERNAL_DO_WAIT_WAKE:
  1693. pDevExt->SendWaitWake = TRUE;
  1694. status = STATUS_SUCCESS;
  1695. break;
  1696. case IOCTL_SERIAL_INTERNAL_CANCEL_WAIT_WAKE:
  1697. pDevExt->SendWaitWake = FALSE;
  1698. if (pDevExt->PendingWakeIrp != NULL) {
  1699. IoCancelIrp(pDevExt->PendingWakeIrp);
  1700. }
  1701. status = STATUS_SUCCESS;
  1702. break;
  1703. //
  1704. // Put the serial port in a "filter-driver" appropriate state
  1705. //
  1706. // WARNING: This code assumes it is being called by a trusted kernel
  1707. // entity and no checking is done on the validity of the settings
  1708. // passed to IOCTL_SERIAL_INTERNAL_RESTORE_SETTINGS
  1709. //
  1710. // If validity checking is desired, the regular ioctl's should be used
  1711. //
  1712. case IOCTL_SERIAL_INTERNAL_BASIC_SETTINGS:
  1713. case IOCTL_SERIAL_INTERNAL_RESTORE_SETTINGS: {
  1714. SERIAL_BASIC_SETTINGS basic;
  1715. PSERIAL_BASIC_SETTINGS pBasic;
  1716. SHORT AppropriateDivisor;
  1717. SERIAL_IOCTL_SYNC S;
  1718. if (pIrpStack->Parameters.DeviceIoControl.IoControlCode
  1719. == IOCTL_SERIAL_INTERNAL_BASIC_SETTINGS) {
  1720. //
  1721. // Check the buffer size
  1722. //
  1723. if (pIrpStack->Parameters.DeviceIoControl.OutputBufferLength <
  1724. sizeof(SERIAL_BASIC_SETTINGS)) {
  1725. status = STATUS_BUFFER_TOO_SMALL;
  1726. break;
  1727. }
  1728. //
  1729. // Everything is 0 -- timeouts and flow control and fifos. If
  1730. // We add additional features, this zero memory method
  1731. // may not work.
  1732. //
  1733. RtlZeroMemory(&basic, sizeof(SERIAL_BASIC_SETTINGS));
  1734. basic.TxFifo = 1;
  1735. basic.RxFifo = SERIAL_1_BYTE_HIGH_WATER;
  1736. PIrp->IoStatus.Information = sizeof(SERIAL_BASIC_SETTINGS);
  1737. pBasic = (PSERIAL_BASIC_SETTINGS)PIrp->AssociatedIrp.SystemBuffer;
  1738. //
  1739. // Save off the old settings
  1740. //
  1741. RtlCopyMemory(&pBasic->Timeouts, &pDevExt->Timeouts,
  1742. sizeof(SERIAL_TIMEOUTS));
  1743. RtlCopyMemory(&pBasic->HandFlow, &pDevExt->HandFlow,
  1744. sizeof(SERIAL_HANDFLOW));
  1745. pBasic->RxFifo = pDevExt->RxFifoTrigger;
  1746. pBasic->TxFifo = pDevExt->TxFifoAmount;
  1747. //
  1748. // Point to our new settings
  1749. //
  1750. pBasic = &basic;
  1751. } else { // restoring settings
  1752. if (pIrpStack->Parameters.DeviceIoControl.InputBufferLength
  1753. < sizeof(SERIAL_BASIC_SETTINGS)) {
  1754. status = STATUS_BUFFER_TOO_SMALL;
  1755. break;
  1756. }
  1757. pBasic = (PSERIAL_BASIC_SETTINGS)PIrp->AssociatedIrp.SystemBuffer;
  1758. }
  1759. KeAcquireSpinLock(&pDevExt->ControlLock, &OldIrql);
  1760. //
  1761. // Set the timeouts
  1762. //
  1763. RtlCopyMemory(&pDevExt->Timeouts, &pBasic->Timeouts,
  1764. sizeof(SERIAL_TIMEOUTS));
  1765. //
  1766. // Set flowcontrol
  1767. //
  1768. S.Extension = pDevExt;
  1769. S.Data = &pBasic->HandFlow;
  1770. KeSynchronizeExecution(pDevExt->Interrupt, SerialSetHandFlow, &S);
  1771. if (pDevExt->FifoPresent) {
  1772. pDevExt->TxFifoAmount = pBasic->TxFifo;
  1773. pDevExt->RxFifoTrigger = (UCHAR)pBasic->RxFifo;
  1774. WRITE_FIFO_CONTROL(pDevExt->Controller, (UCHAR)0);
  1775. READ_RECEIVE_BUFFER(pDevExt->Controller);
  1776. WRITE_FIFO_CONTROL(pDevExt->Controller,
  1777. (UCHAR)(SERIAL_FCR_ENABLE | pDevExt->RxFifoTrigger
  1778. | SERIAL_FCR_RCVR_RESET
  1779. | SERIAL_FCR_TXMT_RESET));
  1780. } else {
  1781. pDevExt->TxFifoAmount = pDevExt->RxFifoTrigger = 0;
  1782. WRITE_FIFO_CONTROL(pDevExt->Controller, (UCHAR)0);
  1783. }
  1784. KeReleaseSpinLock(&pDevExt->ControlLock, OldIrql);
  1785. break;
  1786. }
  1787. default:
  1788. status = STATUS_INVALID_PARAMETER;
  1789. break;
  1790. }
  1791. PIrp->IoStatus.Status = status;
  1792. SerialDump(SERIRPPATH, ("SERIAL: Complete Irp: %x\n", PIrp));
  1793. SerialCompleteRequest(pDevExt, PIrp, 0);
  1794. return status;
  1795. }