Moved sndblst in sound
[reactos.git] / reactos / drivers / base / mpu401 / 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 <ntddk.h>
13 #include "mpu401.h"
14
15
16 BOOLEAN WaitToSend(ULONG BasePort)
17 {
18 int TimeOut;
19
20 DbgPrint("WaitToSend ");
21
22 // Check if it's OK to send
23 for (TimeOut = MPU401_TIMEOUT;
24 ! MPU401_READY_TO_SEND(BasePort) && TimeOut > 0;
25 TimeOut --);
26
27 // If a time-out occurs, we report failure
28 if (! TimeOut)
29 {
30 DbgPrint("FAILED\n");
31 return FALSE;
32 }
33
34 DbgPrint("SUCCEEDED\n");
35
36 return TRUE;
37 }
38
39
40 BOOLEAN WaitToReceive(ULONG BasePort)
41 {
42 int TimeOut;
43
44 DbgPrint("WaitToSend ");
45
46 // Check if it's OK to receive
47 for (TimeOut = MPU401_TIMEOUT;
48 ! MPU401_READY_TO_RECEIVE(BasePort) && TimeOut > 0;
49 TimeOut --);
50
51 // If a time-out occurs, we report failure
52 if (! TimeOut)
53 {
54 DbgPrint("FAILED\n");
55 return FALSE;
56 }
57
58 DbgPrint("SUCCEEDED\n");
59
60 return TRUE;
61 }
62
63
64 BOOLEAN InitUARTMode(ULONG BasePort)
65 {
66 ULONG TimeOut;
67 UCHAR Status = 0;
68
69 DbgPrint("InitUARTMode() called\n");
70
71 // Check if it's OK to send
72 if (! WaitToSend(BasePort))
73 return FALSE;
74
75 DbgPrint("Resetting MPU401\n");
76
77 // Send an MPU reset:
78 MPU401_WRITE_COMMAND(BasePort, 0xff);
79
80 // Check if it's OK to receive (some cards will ignore the above reset
81 // command and so will not issue an ACK, so time out is NOT an error)
82 DbgPrint("Waiting for an ACK\n");
83 if (WaitToReceive(BasePort))
84 {
85 // Check to make sure the reset was acknowledged:
86 for (TimeOut = MPU401_TIMEOUT;
87 (Status = (MPU401_READ_DATA(BasePort) & 0xfe) && TimeOut > 0);
88 TimeOut --);
89 }
90
91 DbgPrint("Entering UART mode\n");
92 // Now we kick the MPU401 into UART ("dumb") mode
93 MPU401_WRITE_COMMAND(BasePort, 0x3f);
94
95 return TRUE;
96 }