2 * PROJECT: ReactOS Sound System "MME Buddy" Library
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: lib/drivers/sound/mmebuddy/kernel.c
6 * PURPOSE: Routines assisting with device I/O between user-mode and
9 * PROGRAMMERS: Andrew Greenwood (silverblade@reactos.org)
19 Wraps around CreateFile in order to provide a simpler interface tailored
20 towards sound driver support code. This simply takes a device path and
21 opens the device in either read-only mode, or read/write mode (depending on
22 the ReadOnly parameter).
24 If the device is opened in read/write mode, it is opened for overlapped I/O.
27 OpenKernelSoundDeviceByName(
34 VALIDATE_MMSYS_PARAMETER( DevicePath
);
35 VALIDATE_MMSYS_PARAMETER( Handle
);
37 AccessRights
= ReadOnly
? GENERIC_READ
: GENERIC_READ
| GENERIC_WRITE
;
39 SND_TRACE(L
"OpenKernelSoundDeviceByName: %wS\n", DevicePath
);
40 *Handle
= CreateFile(DevicePath
,
42 FILE_SHARE_WRITE
, /* FIXME? Should be read also? */
45 ReadOnly
? 0 : FILE_FLAG_OVERLAPPED
,
48 if ( *Handle
== INVALID_HANDLE_VALUE
)
50 SND_ERR(L
"CreateFile filed - winerror %d\n", GetLastError());
51 return Win32ErrorToMmResult(GetLastError());
54 return MMSYSERR_NOERROR
;
59 Just a wrapped around CloseHandle.
62 CloseKernelSoundDevice(
65 VALIDATE_MMSYS_PARAMETER( Handle
);
69 return MMSYSERR_NOERROR
;
73 This is a wrapper around DeviceIoControl which provides control over
74 instantiated sound devices. It waits for I/O to complete (since an
75 instantiated sound device is opened in overlapped mode, this is necessary).
78 SyncOverlappedDeviceIoControl(
80 IN DWORD IoControlCode
,
82 IN DWORD InBufferSize
,
84 IN DWORD OutBufferSize
,
85 OUT LPDWORD BytesTransferred OPTIONAL
)
87 OVERLAPPED Overlapped
;
91 /* Overlapped I/O is done here - this is used for waiting for completion */
92 ZeroMemory(&Overlapped
, sizeof(OVERLAPPED
));
93 Overlapped
.hEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
95 if ( ! Overlapped
.hEvent
)
96 return Win32ErrorToMmResult(GetLastError());
98 /* Talk to the device */
99 IoResult
= DeviceIoControl(Handle
,
108 /* If failure occurs, make sure it's not just due to the overlapped I/O */
111 if ( GetLastError() != ERROR_IO_PENDING
)
113 CloseHandle(Overlapped
.hEvent
);
114 return Win32ErrorToMmResult(GetLastError());
118 /* Wait for the I/O to complete */
119 IoResult
= GetOverlappedResult(Handle
,
124 /* Don't need this any more */
125 CloseHandle(Overlapped
.hEvent
);
128 return Win32ErrorToMmResult(GetLastError());
130 if ( BytesTransferred
)
131 *BytesTransferred
= Transferred
;
133 return MMSYSERR_NOERROR
;