/*++
Copyright (c) 1998 Microsoft Corporation
Module Name:
1394main.c
Abstract
Authors:
Peter Binder (pbinder) 4/22/98
Christopher Baker (cbaker) 10/01
Revision History:
Date Who What
-------- --------- ------------------------------------------------------------
4/22/98 pbinder moved from 1394api.c
10/11/01 cbaker stripped out the automagic stuff that was causing problems, modified GetDeviceList
10/20/01 cbaker added [Get/Reset\CmdrState to assist in recovery from program crashes
10/24/01 cbaker added ReadRegister/WriteRegister Get[Model/Vendor]Name
10/29/01 cbaker added WriteRegisterUL
--*/
#define _1394MAIN_C
#define INITGUID
#include "pch.h"
#undef _1394MAIN_C
/*
* DllMain
*
* Dll Entry Function
*
* Under 1394diag, this function did a bunch of things with shared memory and process management
*
* Now, it does nothing interesting at all
*/
HINSTANCE g_hInstDLL;
BOOL
WINAPI
DllMain(
HINSTANCE hInstance,
ULONG ulReason,
LPVOID pvReserved
)
{
HKEY myKey = NULL;
DWORD dwDisp,dwRet,dwTraceLevel = DLL_TRACE_WARNING,dwType,dwSize = sizeof(DWORD);
g_hInstDLL = hInstance;
// now that we don't start the extra process nor muck around with shared
// memory and device change signals and bus resets, DllMain is really simple
if(ulReason == DLL_PROCESS_ATTACH)
{
// load the tracelevel from the registry
DllTrace(DLL_TRACE_ALWAYS,"DllMain: DLL_PROCESS_ATTACH to 1394Camera.dll version 6.00.00.0000\n");
dwRet = RegCreateKeyEx(HKEY_LOCAL_MACHINE,"Software\\CMU\\1394Camera",0,NULL,REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,NULL,&myKey,&dwDisp);
if(dwRet != ERROR_SUCCESS)
{
DllTrace(DLL_TRACE_ERROR,"DllMain: Failed to open the key (%d)\n",dwRet);
myKey = NULL;
} else {
if(dwDisp == REG_CREATED_NEW_KEY)
{
DllTrace(DLL_TRACE_CHECK,"DllMain: New key was created, setting TraceLevel to DLL_TRACE_WARNING\n");
dwTraceLevel = DLL_TRACE_WARNING;
dwRet = RegSetValueEx(myKey,"DllTraceLevel",0,REG_DWORD,(LPBYTE)&dwTraceLevel,dwSize);
if(dwRet != ERROR_SUCCESS)
DllTrace(DLL_TRACE_ERROR,"DllMain: error setting TraceLevel registry key (%d)\n",dwRet);
} else {
DllTrace(DLL_TRACE_CHECK,"DllMain: Old Key was opened, getting TraceLevel\n");
dwRet = RegQueryValueEx(myKey,"DllTraceLevel",NULL,&dwType,(LPBYTE)&dwTraceLevel,&dwSize);
if(dwRet != ERROR_SUCCESS)
{
DllTrace(DLL_TRACE_ERROR,"DllMain: error getting TraceLevel registry key (%d)\n",dwRet);
// default to TRACE_WARNING (that is, errors and warnings only
dwTraceLevel = DLL_TRACE_WARNING;
} else {
DllTrace(DLL_TRACE_CHECK,"DllMain: Read Tracelevel as %d\n",dwTraceLevel);
}
}
RegCloseKey(myKey);
}
SetDllTraceLevel(dwTraceLevel);
}
return TRUE;
}
/*
* GetCmdrState
*
* Currently just a stub that doesn't do anything noteworthy
* May eventually be used to report statistics, or more
*/
DWORD
CAMAPI
GetCmdrState( PSTR szDeviceName)
{
DWORD state;
HANDLE hDevice = OpenDevice(szDeviceName,FALSE);
if(hDevice != INVALID_HANDLE_VALUE)
{
if(DeviceIoControl(hDevice,IOCTL_GET_CMDR_STATE,NULL,0,&state,sizeof(DWORD),NULL,NULL))
{
return state;
}
}
return -1;
}
/*
* SetCmdrTraceLevel
*
* This is another stub in case I ever decide to
* reorganize the trace output from the driver.
*/
DWORD
CAMAPI
SetCmdrTraceLevel( PSTR szDeviceName, DWORD nlevel)
{
DWORD level = nlevel;
HANDLE hDevice = OpenDevice(szDeviceName,FALSE);
if(hDevice != INVALID_HANDLE_VALUE)
{
if(DeviceIoControl(hDevice,IOCTL_GET_CMDR_STATE,&level,sizeof(DWORD),NULL,0,NULL,NULL))
{
return 0;
}
}
return -1;
}
/*
* GetDeviceList
*
* Checks the GUID_1394CAMERA interface for all 1394 Digital cameras in the system
* and returns their device names in DeviceData
*
* args:
* DeviceData: a pointer to an externally allocated DEVICE_DATA structure;
*
* returns:
* the number of cameras discovered, or
* -1 on error
*/
DWORD
CAMAPI
GetDeviceList(
PDEVICE_DATA DeviceData
)
{
HDEVINFO hDevInfo;
ULONG i = 0;
SP_DEVICE_INTERFACE_DATA deviceInterfaceData;
PSP_DEVICE_INTERFACE_DETAIL_DATA DeviceInterfaceDetailData;
ULONG requiredSize;
GUID t1394CmdrGUID;
ULONG dwError;
HWND hWnd = NULL;
BOOL bReturn;
DllTrace(DLL_TRACE_ENTER, "Enter GetDeviceList(0x%08x)\r\n",DeviceData);
/******************************/
/* for this to work, you MUST */
/* define INITGUID before */
/* including pch.h */
/* otherwise you get an */
/* unresolved external symbol */
/* for t1394CmdrGUID */
/******************************/
t1394CmdrGUID = GUID_1394CMDR;
memset(DeviceData,0,sizeof(DEVICE_DATA));
// get a handle to 1394 test class using guid.
hDevInfo = SetupDiGetClassDevs( &t1394CmdrGUID,
NULL,
NULL,
(DIGCF_PRESENT | DIGCF_INTERFACEDEVICE)
);
if (!hDevInfo) {
dwError = GetLastError();
DllTrace(DLL_TRACE_ERROR, "GetDeviceList: SetupDiGetClassDevs failed = 0x%x\r\n", dwError);
return -1;
}
// here we're going to loop and find all of the
// 1394 camera interfaces available.
while (TRUE) {
DllTrace(DLL_TRACE_VERBOSE,"GetDeviceList: Enumerating Device %d\r\n",i);
deviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
if (!SetupDiEnumDeviceInterfaces(hDevInfo, NULL, &t1394CmdrGUID, i, &deviceInterfaceData))
{
dwError = GetLastError();
if(dwError != ERROR_NO_MORE_ITEMS)
{
DllTrace(DLL_TRACE_ERROR,"GetDeviceList: SetupDiEnumDeviceInterfaces failed -> 0x%x\r\n", dwError);
return -1;
} else {
DllTrace(DLL_TRACE_CHECK,"GetDeviceList: No More Items\r\n");
break;
}
}
// figure out the size...
bReturn = SetupDiGetDeviceInterfaceDetail( hDevInfo,
&deviceInterfaceData,
NULL,
0,
&requiredSize,
NULL
);
if (!bReturn) {
dwError = GetLastError();
if(dwError != ERROR_INSUFFICIENT_BUFFER)
{
DllTrace(DLL_TRACE_ERROR, "GetDeviceList: SetupDiGetDeviceInterfaceDetail Failed (1) -> 0x%x\r\n", dwError);
SetupDiDestroyDeviceInfoList(hDevInfo);
return -1;
}
} else {
DllTrace(DLL_TRACE_ERROR,"GetDeviceList: I really shouldn't be here (%s:%d)\r\n",__FILE__,__LINE__);
SetupDiDestroyDeviceInfoList(hDevInfo);
return -1;
}
DeviceInterfaceDetailData = LocalAlloc(LPTR, requiredSize);
if(DeviceInterfaceDetailData == NULL)
{
DllTrace(DLL_TRACE_ERROR,"GetDeviceList: LocalAlloc Failed\r\n");
SetupDiDestroyDeviceInfoList(hDevInfo);
return -1;
}
memset(DeviceInterfaceDetailData,0,requiredSize);
DeviceInterfaceDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
bReturn = SetupDiGetDeviceInterfaceDetail( hDevInfo,
&deviceInterfaceData,
DeviceInterfaceDetailData,
requiredSize,
NULL,
NULL
);
if (!bReturn)
{
dwError = GetLastError();
DllTrace(DLL_TRACE_ERROR, "GetDeviceList: SetupDiGetDeviceInterfaceDetail Failed (2) -> 0x%x\r\n", dwError);
SetupDiDestroyDeviceInfoList(hDevInfo);
LocalFree(DeviceInterfaceDetailData);
return -1;
} else {
// we have a device, let's put it in the list.
DeviceData->numDevices++;
strcp