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.

656 lines
13 KiB

  1. /*++
  2. Copyright (c) 1997-1998 Microsoft Corporation, All Rights Reserved
  3. Module Name:
  4. wrapper.c
  5. Abstract:
  6. Wraps all IOCTL based requests into nice self contained functions
  7. Environment:
  8. Kernel mode only.
  9. Notes:
  10. Revision History:
  11. --*/
  12. #include "mouser.h"
  13. #include "debug.h"
  14. #ifdef ALLOC_PRAGMA
  15. #pragma alloc_text(PAGE,SerialMouseSetFifo)
  16. #pragma alloc_text(PAGE,SerialMouseGetLineCtrl)
  17. #pragma alloc_text(PAGE,SerialMouseSetLineCtrl)
  18. #pragma alloc_text(PAGE,SerialMouseGetModemCtrl)
  19. #pragma alloc_text(PAGE,SerialMouseSetModemCtrl)
  20. #pragma alloc_text(PAGE,SerialMouseGetBaudRate)
  21. #pragma alloc_text(PAGE,SerialMouseSetBaudRate)
  22. #pragma alloc_text(PAGE,SerialMouseReadChar)
  23. #pragma alloc_text(PAGE,SerialMouseWriteChar)
  24. #pragma alloc_text(PAGE,SerialMouseWriteString)
  25. #endif // ALLOC_PRAGMA
  26. //
  27. // Constants
  28. //
  29. //
  30. // unknown
  31. //
  32. NTSTATUS
  33. SerialMouseSetFifo(
  34. PDEVICE_EXTENSION DeviceExtension,
  35. UCHAR Value
  36. )
  37. /*++
  38. Routine Description:
  39. Set the FIFO register.
  40. Arguments:
  41. Port - Pointer to the serial port.
  42. Value - The FIFO control mask.
  43. Return Value:
  44. None.
  45. --*/
  46. {
  47. ULONG fifo = Value;
  48. IO_STATUS_BLOCK iosb;
  49. KEVENT event;
  50. NTSTATUS status;
  51. Print(DeviceExtension, DBG_UART_TRACE, ("Fifo, enter\n"));
  52. KeInitializeEvent(&event,
  53. NotificationEvent,
  54. FALSE);
  55. status = SerialMouseIoSyncIoctlEx(
  56. IOCTL_SERIAL_SET_FIFO_CONTROL,
  57. DeviceExtension->TopOfStack,
  58. &event,
  59. &iosb,
  60. &fifo,
  61. sizeof(ULONG),
  62. NULL,
  63. 0);
  64. if (!NT_SUCCESS(iosb.Status)) {
  65. status = iosb.Status;
  66. }
  67. if (!NT_SUCCESS(status)) {
  68. Print(DeviceExtension, DBG_UART_ERROR, ("Fifo failed (%x)\n", status));
  69. }
  70. return status;
  71. }
  72. NTSTATUS
  73. SerialMouseGetLineCtrl(
  74. PDEVICE_EXTENSION DeviceExtension,
  75. PSERIAL_LINE_CONTROL SerialLineControl
  76. )
  77. /*++
  78. Routine Description:
  79. Get the serial port line control register.
  80. Arguments:
  81. Port - Pointer to the serial port.
  82. Return Value:
  83. Serial port line control value.
  84. --*/
  85. {
  86. IO_STATUS_BLOCK iosb;
  87. KEVENT event;
  88. NTSTATUS status;
  89. Print(DeviceExtension, DBG_UART_TRACE, ("GetLineCtrl enter\n"));
  90. KeInitializeEvent(&event,
  91. NotificationEvent,
  92. FALSE);
  93. status = SerialMouseIoSyncIoctlEx(
  94. IOCTL_SERIAL_GET_LINE_CONTROL,
  95. DeviceExtension->TopOfStack,
  96. &event,
  97. &iosb,
  98. NULL,
  99. 0,
  100. SerialLineControl,
  101. sizeof(SERIAL_LINE_CONTROL));
  102. if (!NT_SUCCESS(iosb.Status)) {
  103. status = iosb.Status;
  104. }
  105. if (!NT_SUCCESS(status)) {
  106. Print(DeviceExtension, DBG_UART_ERROR,
  107. ("GetLineCtrl failed! (%x)\n", status));
  108. }
  109. Print(DeviceExtension, DBG_UART_TRACE,
  110. ("GetLineCtrl exit (%x)\n", status));
  111. return status;
  112. }
  113. NTSTATUS
  114. SerialMouseSetLineCtrl(
  115. PDEVICE_EXTENSION DeviceExtension,
  116. PSERIAL_LINE_CONTROL SerialLineControl
  117. )
  118. /*++
  119. Routine Description:
  120. Set the serial port line control register.
  121. Arguments:
  122. Port - Pointer to the serial port.
  123. Value - New line control value.
  124. Return Value:
  125. Previous serial line control register value.
  126. --*/
  127. {
  128. IO_STATUS_BLOCK iosb;
  129. KEVENT event;
  130. NTSTATUS status;
  131. KeInitializeEvent(&event,
  132. NotificationEvent,
  133. FALSE);
  134. status = SerialMouseIoSyncIoctlEx(
  135. IOCTL_SERIAL_SET_LINE_CONTROL,
  136. DeviceExtension->TopOfStack,
  137. &event,
  138. &iosb,
  139. SerialLineControl,
  140. sizeof(SERIAL_LINE_CONTROL),
  141. NULL,
  142. 0);
  143. if (!NT_SUCCESS(iosb.Status)) {
  144. status = iosb.Status;
  145. }
  146. if (!NT_SUCCESS(status)) {
  147. Print(DeviceExtension, DBG_UART_ERROR,
  148. ("SetLineCtrl failed (%x)\n", status));
  149. }
  150. Print(DeviceExtension, DBG_UART_TRACE,
  151. ("SetLineCtrl exit (%x)\n", status));
  152. return status;
  153. }
  154. NTSTATUS
  155. SerialMouseGetModemCtrl(
  156. PDEVICE_EXTENSION DeviceExtension,
  157. PULONG ModemCtrl
  158. )
  159. /*++
  160. Routine Description:
  161. Get the serial port modem control register.
  162. Arguments:
  163. Port - Pointer to the serial port.
  164. Return Value:
  165. Serial port modem control register value.
  166. --*/
  167. {
  168. IO_STATUS_BLOCK iosb;
  169. KEVENT event;
  170. NTSTATUS status;
  171. KeInitializeEvent(&event,
  172. NotificationEvent,
  173. FALSE);
  174. status = SerialMouseIoSyncIoctlEx(
  175. IOCTL_SERIAL_GET_MODEM_CONTROL,
  176. DeviceExtension->TopOfStack,
  177. &event,
  178. &iosb,
  179. NULL,
  180. 0,
  181. ModemCtrl,
  182. sizeof(ULONG));
  183. if (!NT_SUCCESS(iosb.Status)) {
  184. status = iosb.Status;
  185. }
  186. if (!NT_SUCCESS(status)) {
  187. Print(DeviceExtension, DBG_UART_ERROR,
  188. ("GetModemCtrl failed! (%x)\n", status));
  189. }
  190. Print(DeviceExtension, DBG_UART_TRACE,
  191. ("GetModemCtrl exit (%x)\n", status));
  192. return status;
  193. }
  194. //
  195. // unknown
  196. //
  197. NTSTATUS
  198. SerialMouseSetModemCtrl(
  199. PDEVICE_EXTENSION DeviceExtension,
  200. ULONG Value,
  201. PULONG OldValue OPTIONAL
  202. )
  203. /*++
  204. Routine Description:
  205. Set the serial port modem control register.
  206. Arguments:
  207. Port - Pointer to the serial port.
  208. Return Value:
  209. Previous modem control register value.
  210. --*/
  211. {
  212. IO_STATUS_BLOCK iosb;
  213. KEVENT event;
  214. NTSTATUS status;
  215. Print(DeviceExtension, DBG_UART_TRACE, ("SetModemCtrl enter\n"));
  216. if (ARGUMENT_PRESENT(OldValue)) {
  217. SerialMouseGetModemCtrl(DeviceExtension, OldValue);
  218. }
  219. KeInitializeEvent(&event,
  220. NotificationEvent,
  221. FALSE);
  222. status = SerialMouseIoSyncIoctlEx(
  223. IOCTL_SERIAL_SET_MODEM_CONTROL,
  224. DeviceExtension->TopOfStack,
  225. &event,
  226. &iosb,
  227. &Value,
  228. sizeof(ULONG),
  229. NULL,
  230. 0);
  231. if (!NT_SUCCESS(iosb.Status)) {
  232. status = iosb.Status;
  233. }
  234. if (!NT_SUCCESS(status)) {
  235. Print(DeviceExtension, DBG_UART_ERROR,
  236. ("SetModemCtrl failed! (%x)\n", status));
  237. }
  238. Print(DeviceExtension, DBG_UART_TRACE,
  239. ("SetModemCtrl exit (%x)\n", status));
  240. return status;
  241. }
  242. NTSTATUS
  243. SerialMouseGetBaudRate(
  244. PDEVICE_EXTENSION DeviceExtension,
  245. PULONG BaudRate
  246. )
  247. /*++
  248. Routine Description:
  249. Get the serial port baud rate setting.
  250. Arguments:
  251. Port - Pointer to the serial port.
  252. BaudClock - The external frequency driving the serial chip.
  253. Return Value:
  254. Serial port baud rate.
  255. --*/
  256. {
  257. SERIAL_BAUD_RATE sbr;
  258. IO_STATUS_BLOCK iosb;
  259. KEVENT event;
  260. NTSTATUS status;
  261. Print(DeviceExtension, DBG_UART_TRACE, ("GetBaud enter\n"));
  262. KeInitializeEvent(&event,
  263. NotificationEvent,
  264. FALSE);
  265. status = SerialMouseIoSyncIoctlEx(
  266. IOCTL_SERIAL_GET_BAUD_RATE,
  267. DeviceExtension->TopOfStack,
  268. &event,
  269. &iosb,
  270. NULL,
  271. 0,
  272. &sbr,
  273. sizeof(SERIAL_BAUD_RATE));
  274. if (!NT_SUCCESS(iosb.Status)) {
  275. status = iosb.Status;
  276. }
  277. if (!NT_SUCCESS(status)) {
  278. Print(DeviceExtension, DBG_UART_ERROR,
  279. ("GetBaud failed (%x)\n", status));
  280. }
  281. else {
  282. *BaudRate = sbr.BaudRate;
  283. }
  284. Print(DeviceExtension, DBG_UART_TRACE,
  285. ("GetBaud exit (%x)\n", status));
  286. return status;
  287. }
  288. NTSTATUS
  289. SerialMouseSetBaudRate(
  290. PDEVICE_EXTENSION DeviceExtension,
  291. ULONG BaudRate
  292. )
  293. /*++
  294. Routine Description:
  295. Set the serial port baud rate.
  296. Arguments:
  297. Port - Pointer to the serial port.
  298. BaudRate - New serial port baud rate.
  299. BaudClock - The external frequency driving the serial chip.
  300. Return Value:
  301. None.
  302. --*/
  303. {
  304. SERIAL_BAUD_RATE sbr;
  305. IO_STATUS_BLOCK iosb;
  306. KEVENT event;
  307. NTSTATUS status;
  308. KeInitializeEvent(&event,
  309. NotificationEvent,
  310. FALSE);
  311. sbr.BaudRate = BaudRate;
  312. status = SerialMouseIoSyncIoctlEx(
  313. IOCTL_SERIAL_SET_BAUD_RATE,
  314. DeviceExtension->TopOfStack,
  315. &event,
  316. &iosb,
  317. &sbr,
  318. sizeof(SERIAL_BAUD_RATE),
  319. NULL,
  320. 0);
  321. if (!NT_SUCCESS(iosb.Status)) {
  322. status = iosb.Status;
  323. }
  324. if (!NT_SUCCESS(status)) {
  325. Print(DeviceExtension, DBG_UART_ERROR,
  326. ("SetBaud failed! (%x)\n", status));
  327. }
  328. else {
  329. Print(DeviceExtension, DBG_UART_INFO, ("BaudRate: %d\n", BaudRate));
  330. }
  331. return status;
  332. }
  333. NTSTATUS
  334. SerialMouseReadChar(
  335. PDEVICE_EXTENSION DeviceExtension,
  336. PUCHAR Value
  337. )
  338. /*++
  339. Routine Description:
  340. Read a character from the serial port. Waits until a character has
  341. been read or the timeout value is reached.
  342. Arguments:
  343. Port - Pointer to the serial port.
  344. Value - The character read from the serial port input buffer.
  345. Timeout - The timeout value in milliseconds for the read.
  346. Return Value:
  347. TRUE if a character has been read, FALSE if a timeout occured.
  348. --*/
  349. {
  350. NTSTATUS status;
  351. USHORT actual;
  352. Print(DeviceExtension, DBG_UART_TRACE, ("ReadChar enter\n"));
  353. status =
  354. SerialMouseReadSerialPort(DeviceExtension, Value, 1, &actual);
  355. if (!NT_SUCCESS(status)) {
  356. Print(DeviceExtension, DBG_UART_ERROR,
  357. ("ReadChar failed! (%x)\n", status));
  358. }
  359. else if (actual != 1) {
  360. status = STATUS_UNSUCCESSFUL;
  361. }
  362. else {
  363. Print(DeviceExtension, DBG_UART_NOISE,
  364. ("ReadChar read %x (actual = %d)\n", (ULONG) *Value, actual));
  365. }
  366. Print(DeviceExtension, DBG_UART_TRACE, ("ReadChar exit (%x)\n", status));
  367. return status;
  368. }
  369. NTSTATUS
  370. SerialMouseFlushReadBuffer(
  371. PDEVICE_EXTENSION DeviceExtension
  372. )
  373. /*++
  374. Routine Description:
  375. Flush the serial port input buffer.
  376. Arguments:
  377. Port - Pointer to the serial port.
  378. Return Value:
  379. TRUE.
  380. --*/
  381. {
  382. ULONG bits = SERIAL_PURGE_RXCLEAR;
  383. NTSTATUS status;
  384. KEVENT event;
  385. IO_STATUS_BLOCK iosb;
  386. Print(DeviceExtension, DBG_UART_TRACE, ("FlushReadBuffer enter\n"));
  387. KeInitializeEvent(&event,
  388. NotificationEvent,
  389. FALSE);
  390. status = SerialMouseIoSyncIoctlEx(
  391. IOCTL_SERIAL_PURGE,
  392. DeviceExtension->TopOfStack,
  393. &event,
  394. &iosb,
  395. &bits,
  396. sizeof(ULONG),
  397. NULL,
  398. 0);
  399. if (!NT_SUCCESS(iosb.Status)) {
  400. status = iosb.Status;
  401. }
  402. if (!NT_SUCCESS(status)) {
  403. Print(DeviceExtension, DBG_UART_ERROR,
  404. ("FlushReadBuffer failed! (%x)\n", status));
  405. }
  406. Print(DeviceExtension, DBG_UART_TRACE,
  407. ("FlushReadBuffer exit (%x)\n", status));
  408. return status;
  409. }
  410. NTSTATUS
  411. SerialMouseWriteChar(
  412. PDEVICE_EXTENSION DeviceExtension,
  413. UCHAR Value
  414. )
  415. /*++
  416. Routine Description:
  417. Write a character to a serial port. Make sure the transmit buffer
  418. is empty before we write there.
  419. Arguments:
  420. Port - Pointer to the serial port.
  421. Value - Value to write to the serial port.
  422. Return Value:
  423. TRUE.
  424. --*/
  425. {
  426. IO_STATUS_BLOCK iosb;
  427. NTSTATUS status;
  428. Print(DeviceExtension, DBG_UART_TRACE, ("WriteChar enter\n"));
  429. status = SerialMouseWriteSerialPort(
  430. DeviceExtension,
  431. &Value,
  432. 1,
  433. &iosb);
  434. if (!NT_SUCCESS(iosb.Status)) {
  435. status = iosb.Status;
  436. }
  437. if (!NT_SUCCESS(status)) {
  438. Print(DeviceExtension, DBG_UART_ERROR,
  439. ("WriteChar failed! (%x)\n", status));
  440. }
  441. Print(DeviceExtension, DBG_UART_TRACE, ("WriteChar exit\n"));
  442. return status;
  443. }
  444. NTSTATUS
  445. SerialMouseWriteString(
  446. PDEVICE_EXTENSION DeviceExtension,
  447. PSZ Buffer
  448. )
  449. /*++
  450. Routine Description:
  451. Write a zero-terminated string to the serial port.
  452. Arguments:
  453. Port - Pointer to the serial port.
  454. Buffer - Pointer to a zero terminated string to write to
  455. the serial port.
  456. Return Value:
  457. TRUE.
  458. --*/
  459. {
  460. IO_STATUS_BLOCK iosb;
  461. NTSTATUS status;
  462. Print(DeviceExtension, DBG_UART_TRACE, ("WriteString enter\n"));
  463. status = SerialMouseWriteSerialPort(
  464. DeviceExtension,
  465. Buffer,
  466. strlen(Buffer),
  467. &iosb);
  468. if (!NT_SUCCESS(iosb.Status)) {
  469. status = iosb.Status;
  470. }
  471. if (!NT_SUCCESS(status)) {
  472. Print(DeviceExtension, DBG_UART_ERROR,
  473. ("WriteString failed! (%x)\n", status));
  474. }
  475. Print(DeviceExtension, DBG_UART_TRACE,
  476. ("WriteString exit (%x)\n", status));
  477. return status;
  478. }