ReactOS  0.4.14-dev-1007-g90d795b
kernel.c
Go to the documentation of this file.
1 /*
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
5  *
6  * PURPOSE: Routines assisting with device I/O between user-mode and
7  * kernel-mode.
8  *
9  * PROGRAMMERS: Andrew Greenwood (silverblade@reactos.org)
10 */
11 
12 #include "precomp.h"
13 
14 /*
15  Wraps around CreateFile in order to provide a simpler interface tailored
16  towards sound driver support code. This simply takes a device path and
17  opens the device in either read-only mode, or read/write mode (depending on
18  the ReadOnly parameter).
19 
20  If the device is opened in read/write mode, it is opened for overlapped I/O.
21 */
24  IN PWSTR DevicePath,
27 {
28  DWORD AccessRights;
29 
30  VALIDATE_MMSYS_PARAMETER( DevicePath );
32 
33  AccessRights = ReadOnly ? GENERIC_READ : GENERIC_READ | GENERIC_WRITE;
34 
35  SND_TRACE(L"OpenKernelSoundDeviceByName: %wS\n", DevicePath);
36  *Handle = CreateFile(DevicePath,
37  AccessRights,
38  FILE_SHARE_WRITE, /* FIXME? Should be read also? */
39  NULL,
42  NULL);
43 
44  if ( *Handle == INVALID_HANDLE_VALUE )
45  {
46  SND_ERR(L"CreateFile filed - winerror %d\n", GetLastError());
48  }
49 
50  return MMSYSERR_NOERROR;
51 }
52 
53 
54 /*
55  Just a wrapped around CloseHandle.
56 */
60 {
62 
64 
65  return MMSYSERR_NOERROR;
66 }
67 
68 /*
69  This is a wrapper around DeviceIoControl which provides control over
70  instantiated sound devices. It waits for I/O to complete (since an
71  instantiated sound device is opened in overlapped mode, this is necessary).
72 */
77  IN LPVOID InBuffer,
79  OUT LPVOID OutBuffer,
81  OUT LPDWORD BytesTransferred OPTIONAL)
82 {
83  OVERLAPPED Overlapped;
84  BOOLEAN IoResult;
85  DWORD Transferred;
86 
87  /* Overlapped I/O is done here - this is used for waiting for completion */
88  ZeroMemory(&Overlapped, sizeof(OVERLAPPED));
89  Overlapped.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
90 
91  if ( ! Overlapped.hEvent )
93 
94  /* Talk to the device */
95  IoResult = DeviceIoControl(Handle,
97  InBuffer,
99  OutBuffer,
101  NULL,
102  &Overlapped);
103 
104  /* If failure occurs, make sure it's not just due to the overlapped I/O */
105  if ( ! IoResult )
106  {
107  if ( GetLastError() != ERROR_IO_PENDING )
108  {
109  CloseHandle(Overlapped.hEvent);
111  }
112  }
113 
114  /* Wait for the I/O to complete */
115  IoResult = GetOverlappedResult(Handle,
116  &Overlapped,
117  &Transferred,
118  TRUE);
119 
120  /* Don't need this any more */
121  CloseHandle(Overlapped.hEvent);
122 
123  if ( ! IoResult )
125 
126  SND_TRACE(L"Transferred %d bytes in Sync overlapped I/O\n", Transferred);
127 
128  if ( BytesTransferred )
129  *BytesTransferred = Transferred;
130 
131  return MMSYSERR_NOERROR;
132 }
#define CreateEvent
Definition: winbase.h:3582
#define IN
Definition: typedefs.h:39
MMRESULT OpenKernelSoundDeviceByName(IN PWSTR DevicePath, IN BOOLEAN ReadOnly, OUT PHANDLE Handle)
Definition: kernel.c:23
#define TRUE
Definition: types.h:120
#define CloseHandle
Definition: compat.h:406
_Must_inspect_result_ _In_ PFILE_OBJECT _In_ ULONG IoControlCode
Definition: fltkernel.h:1383
MMRESULT Win32ErrorToMmResult(IN UINT ErrorCode)
Definition: utility.c:87
BOOL WINAPI GetOverlappedResult(IN HANDLE hFile, IN LPOVERLAPPED lpOverlapped, OUT LPDWORD lpNumberOfBytesTransferred, IN BOOL bWait)
Definition: iocompl.c:204
uint16_t * PWSTR
Definition: typedefs.h:55
Definition: arc.h:80
_In_ PIRP _In_ ULONG _In_ ULONG _In_ ULONG _In_ ULONG OutBufferSize
Definition: classpnp.h:429
#define INVALID_HANDLE_VALUE
Definition: compat.h:399
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
#define ZeroMemory
Definition: winbase.h:1642
UINT MMRESULT
Definition: mmsystem.h:962
#define FILE_SHARE_WRITE
Definition: nt_native.h:681
#define ERROR_IO_PENDING
Definition: dderror.h:15
#define VALIDATE_MMSYS_PARAMETER(parameter_condition)
Definition: mmebuddy.h:71
#define GENERIC_WRITE
Definition: nt_native.h:90
HANDLE hEvent
Definition: winbase.h:792
unsigned char BOOLEAN
smooth NULL
Definition: ftsmooth.c:416
#define OPEN_EXISTING
Definition: compat.h:434
MMRESULT CloseKernelSoundDevice(IN HANDLE Handle)
Definition: kernel.c:58
_In_ HANDLE Handle
Definition: extypes.h:390
_In_ PIRP _In_ ULONG _In_ ULONG _In_ ULONG InBufferSize
Definition: classpnp.h:429
#define MMSYSERR_NOERROR
Definition: mmsystem.h:96
#define SND_ERR(...)
unsigned long DWORD
Definition: ntddk_ex.h:95
static const WCHAR L[]
Definition: oid.c:1250
PVOID *typedef PHANDLE
Definition: ntsecpkg.h:414
#define GENERIC_READ
Definition: compat.h:124
BOOL WINAPI DeviceIoControl(IN HANDLE hDevice, IN DWORD dwIoControlCode, IN LPVOID lpInBuffer OPTIONAL, IN DWORD nInBufferSize OPTIONAL, OUT LPVOID lpOutBuffer OPTIONAL, IN DWORD nOutBufferSize OPTIONAL, OUT LPDWORD lpBytesReturned OPTIONAL, IN LPOVERLAPPED lpOverlapped OPTIONAL)
Definition: deviceio.c:136
IN OUT PVCB OUT PDIRENT OUT PBCB IN BOOLEAN CreateFile
Definition: fatprocs.h:904
#define OUT
Definition: typedefs.h:40
MMRESULT SyncOverlappedDeviceIoControl(IN HANDLE Handle, IN DWORD IoControlCode, IN LPVOID InBuffer, IN DWORD InBufferSize, OUT LPVOID OutBuffer, IN DWORD OutBufferSize, OUT LPDWORD BytesTransferred OPTIONAL)
Definition: kernel.c:74
uint32_t * LPDWORD
Definition: typedefs.h:58
#define FILE_FLAG_OVERLAPPED
Definition: disk.h:46
#define SND_TRACE(...)
PULONG MinorVersion OPTIONAL
Definition: CrossNt.h:68