9e04b8a445fc246f7c550ad897028639b0298f96
[reactos.git] / reactos / drivers / dd / blue / portio.c
1 /*
2 *
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: services/dd/mpu401/portio.c (see also mpu401.h)
6 * PURPOSE: MPU-401 MIDI port I/O helper
7 * PROGRAMMER: Andrew Greenwood
8 * UPDATE HISTORY:
9 * Sept 26, 2003: Created
10 */
11
12 #include <windows.h>
13 #include <ddk/ntddk.h>
14 #include "mpu401.h"
15
16
17 BOOLEAN WaitToSend(UINT BasePort)
18 {
19 int TimeOut;
20
21 DbgPrint("WaitToSend ");
22
23 // Check if it's OK to send
24 for (TimeOut = MPU401_TIMEOUT;
25 ! MPU401_READY_TO_SEND(BasePort) && TimeOut > 0;
26 TimeOut --);
27
28 // If a time-out occurs, we report failure
29 if (! TimeOut)
30 {
31 DbgPrint("FAILED\n");
32 return FALSE;
33 }
34
35 DbgPrint("SUCCEEDED\n");
36
37 return TRUE;
38 }
39
40
41 BOOLEAN WaitToReceive(UINT BasePort)
42 {
43 int TimeOut;
44
45 DbgPrint("WaitToSend ");
46
47 // Check if it's OK to receive
48 for (TimeOut = MPU401_TIMEOUT;
49 ! MPU401_READY_TO_RECEIVE(BasePort) && TimeOut > 0;
50 TimeOut --);
51
52 // If a time-out occurs, we report failure
53 if (! TimeOut)
54 {
55 DbgPrint("FAILED\n");
56 return FALSE;
57 }
58
59 DbgPrint("SUCCEEDED\n");
60
61 return TRUE;
62 }
63
64
65 BOOLEAN InitUARTMode(UINT BasePort)
66 {
67 UINT TimeOut;
68 UCHAR Status = 0;
69
70 DbgPrint("InitUARTMode() called\n");
71
72 // Check if it's OK to send
73 if (! WaitToSend(BasePort))
74 return FALSE;
75
76 DbgPrint("Resetting MPU401\n");
77
78 // Send an MPU reset:
79 MPU401_WRITE_COMMAND(BasePort, 0xff);
80
81 // Check if it's OK to receive (some cards will ignore the above reset
82 // command and so will not issue an ACK, so time out is NOT an error)
83 DbgPrint("Waiting for an ACK\n");
84 if (WaitToReceive(BasePort))
85 {
86 // Check to make sure the reset was acknowledged:
87 for (TimeOut = MPU401_TIMEOUT;
88 Status = (MPU401_READ_DATA(BasePort) & 0xfe) && TimeOut > 0;
89 TimeOut --);
90 }
91
92 DbgPrint("Entering UART mode\n");
93 // Now we kick the MPU401 into UART ("dumb") mode
94 MPU401_WRITE_COMMAND(BasePort, 0x3f);
95
96 return TRUE;
97 }