I got handle of my USB disk using following instruction.
handle = CreateFile(dev_name, 0, 0, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL);
I am getting this handle successfully. The problem I was facing previously
was that my ISR was not getting invoked on attaching USB disk. What I did, I
checked the registry values and found out that I was passing wrong GUID in
notification filter. Just to tell you I was passing {0xc5e4c602, 0xa413,
0x4caa, {0x9b, 0xbd, 0x2e, 0x35, 0x4c, 0x1b, 0xdd, 0xfc}} which was not
working.
I then had to look in registery that when I plug in my device it is
registered at some other Interface GUID which is {0xa5dcbf10, 0x6530, 0x11d2,
{0x90, 0x1f, 0x00, 0xc0, 0x4f, 0xb9, 0x51, 0xed}}.
Now when I set this value in notification filter and then pass this
notification filter to RegisterDeviceNotification I get ISR invoked
successfully.
But now the problem is that when I call DeviceIoControl method as shown
below it returns error. I've checked the error code and it is returning error
number 87 which means invalid parameter. code snippet is below
unsigned int usb_get_last_status(libusb_usb_dev_handle *dev)
{
usb_context_t ctx;
unsigned int ret = 0;
DWORD err = 0;
unsigned int status = 0xfffffffe;
memset(&ctx, 0, sizeof(usb_context_t));
ctx.dev = dev;
ctx.control_code = LIBUSB_IOCTL_USBD_STATUS_READ;
ctx.ol.Offset = 0;
ctx.ol.OffsetHigh = 0;
ctx.ol.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if(ctx.ol.hEvent)
{
ResetEvent(ctx.ol.hEvent);
if(!DeviceIoControl(ctx.dev->impl_info,
ctx.control_code,&ctx.req,sizeof(ctx.req),
&status,
sizeof(status),
&ret,
&ctx.ol))
{
err = GetLastError();
}
..........................
Can anyone tell me the reason of it and any solution to the problem?
> I got handle of my USB disk using following instruction.
> handle = CreateFile(dev_name, 0, 0, NULL, OPEN_EXISTING,
> FILE_ATTRIBUTE_NORMAL, NULL);
>
> I am getting this handle successfully. The problem I was facing previously
> was that my ISR was not getting invoked on attaching USB disk. What I did, I
> checked the registry values and found out that I was passing wrong GUID in
> notification filter. Just to tell you I was passing {0xc5e4c602, 0xa413,
> 0x4caa, {0x9b, 0xbd, 0x2e, 0x35, 0x4c, 0x1b, 0xdd, 0xfc}} which was not
> working.
I have no idea what that GUID is. Is it some sort of libusb device
interface? If so, maybe that explains why your IOCTL is failing --
libusb isn't really loaded, because you're not getting the device
interface arrival.
> I then had to look in registery that when I plug in my device it is
> registered at some other Interface GUID which is {0xa5dcbf10, 0x6530, 0x11d2,
> {0x90, 0x1f, 0x00, 0xc0, 0x4f, 0xb9, 0x51, 0xed}}.
This is registered by the USB stack for all USB devices. It's not
native to USB disk drives.
> But now the problem is that when I call DeviceIoControl method as shown
> below it returns error. I've checked the error code and it is returning error
> number 87 which means invalid parameter. code snippet is below
>
> ctx.control_code = LIBUSB_IOCTL_USBD_STATUS_READ;
I'm not familiar with how libusb works, but something tells me you
might not be able to send a libusb IOCTL to a USB drive which already
has a driver loaded for it (usbstor).
Thanks for your reply. Please see my comments
"chris.a...@gmail.com" wrote:
> On Apr 7, 2:07 am, mooni <mo...@discussions.microsoft.com> wrote:
>
> > I got handle of my USB disk using following instruction.
> > handle = CreateFile(dev_name, 0, 0, NULL, OPEN_EXISTING,
> > FILE_ATTRIBUTE_NORMAL, NULL);
> >
> > I am getting this handle successfully. The problem I was facing previously
> > was that my ISR was not getting invoked on attaching USB disk. What I did, I
> > checked the registry values and found out that I was passing wrong GUID in
> > notification filter. Just to tell you I was passing {0xc5e4c602, 0xa413,
> > 0x4caa, {0x9b, 0xbd, 0x2e, 0x35, 0x4c, 0x1b, 0xdd, 0xfc}} which was not
> > working.
>
> I have no idea what that GUID is. Is it some sort of libusb device
> interface? If so, maybe that explains why your IOCTL is failing --
> libusb isn't really loaded, because you're not getting the device
> interface arrival.
>
No actually I am successfully getting device interface arrival. Actually on
arrival I call this createFile and after this I called DeviceIoControl. I've
written a test code, just to make things look abit simple. Please see below
#include <windows.h>
#include <SetupAPI.h>
#include<Dbt.h>
#include "usb.h" // come with libusb
#include "driver_api.h" // come with libusb
#define LIBUSB_DEVICE_NAME "\\\\.\\libusb0-"
const char g_szClassName[] = "myWindowClass";
static HDEVNOTIFY hdevnotify;
//static GUID GUID_CLASS_VHCTRL = {0xc5e4c602, 0xa413, 0x4caa, {0x9b, 0xbd,
0x2e, 0x35, 0x4c, 0x1b, 0xdd, 0xfc}};
static GUID GUID_CLASS_VHCTRL = {0xa5dcbf10, 0x6530, 0x11d2, {0x90, 0x1f,
0x00, 0xc0, 0x4f, 0xb9, 0x51, 0xed}};
void checkit(WPARAM);
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_LBUTTONDOWN:
{
char szFileName[MAX_PATH];
HINSTANCE hInstance = GetModuleHandle(NULL);
printf("Hello");
GetModuleFileName(hInstance, szFileName, MAX_PATH);
MessageBox(hwnd, szFileName, "This program is:", MB_OK |
MB_ICONINFORMATION);
}
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DEVICECHANGE:
MessageBox(hwnd,"Hello","Welcome",MB_OK);
printf("Hello I am Taimoor");
checkit(wParam);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
void main()
{
HDEVINFO temp;
initDev1();
while(1);
}
DWORD WINAPI ThreadFunc (void* param)
{
MSG Msg;
WNDCLASSEX wc;
HWND hwnd;
DEV_BROADCAST_DEVICEINTERFACE NotificationFilter;
DWORD ret;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = NULL;//hInstance;
wc.hIcon = NULL;//LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = NULL;//LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm = NULL;//LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
"The title of my window",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
NULL, NULL, NULL/*hInstance*/, NULL);
if(hwnd == NULL)
{
MessageBox(NULL, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
if(hwnd)
{
ZeroMemory( &NotificationFilter, sizeof(NotificationFilter) );
NotificationFilter.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
NotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
NotificationFilter.dbcc_classguid = GUID_CLASS_VHCTRL;
hdevnotify = RegisterDeviceNotification(
hwnd,
&NotificationFilter,
DEVICE_NOTIFY_WINDOW_HANDLE);
}
if(!hdevnotify)
{
ret = GetLastError();
}
while(GetMessage(&Msg, hwnd, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
printf("I am out of loop");
}
int initDev1()
{
DWORD dwError;
DWORD threadId;
HANDLE hThread;
hThread = CreateThread(NULL, 0, ThreadFunc, NULL,
0, &threadId);
return 0;
}
void checkit(WPARAM wp)
{
HANDLE hand;
char dev_name[512];
int i;
DWORD derr;
libusb_request req;
OVERLAPPED ol;
unsigned int ret = 0;
unsigned int status = 0xfffffffe;
HDEVINFO dev_info;
if(wp == DBT_DEVICEARRIVAL)
printf("Device arrived");
else
printf("Device Detached");
for(i = 1; i < 256; i++)
{
_snprintf(dev_name, sizeof(dev_name) - 1,"%s%04d",
LIBUSB_DEVICE_NAME, i);
hand = CreateFile(dev_name, 0, 0, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL);
if(hand != INVALID_HANDLE_VALUE)
printf("Got the handle");
else
derr = GetLastError();
ol.Offset = 0;
ol.OffsetHigh = 0;
ol.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if(!DeviceIoControl(hand,LIBUSB_IOCTL_USBD_STATUS_READ,&req,
sizeof(req),&status,sizeof(status),&ret, &ol))
printf("Error in DeviceIoControl");
derr = GetLastError();
}
}
You fetch GetLastError, but you don't print it, nor do you stop trying to
go further.
> ol.Offset = 0;
> ol.OffsetHigh = 0;
> ol.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
>
> if(!DeviceIoControl(hand,LIBUSB_IOCTL_USBD_STATUS_READ,&req,
>sizeof(req),&status,sizeof(status),&ret, &ol))
> printf("Error in DeviceIoControl");
> derr = GetLastError();
LIBUSB_IOCTL_USBD_STATUS_READ is not defined in libusb-win32 0.1.12.1.
Where did you find this?
You haven't set up the request at all. You are passing random memory for
"req". That's probably what causes the ERROR_INVALID_PARAMETER.
If you are using the libusb-win32 driver, why not just use the libusb-win32
library, so that you don't have to worry about getting these details
correct?
--
Tim Roberts, ti...@probo.com
Providenza & Boekelheide, Inc.
Thanks alot for your reply. Please see my comments and guide me.
"Tim Roberts" wrote:
Well you can see this #include at start of program
#include "driver_api.h" // come with libusb
It is actually including driver_api.h which contains definition of this
LIBUSB_IOCTL_USBD_STATUS_READ. This driver_api.h is present in "driver"
folder of source.
> You haven't set up the request at all. You are passing random memory for
> "req". That's probably what causes the ERROR_INVALID_PARAMETER.
I've placed this statement in code
memset(&req,0,sizeof(req));
But it is still giving problem.
> If you are using the libusb-win32 driver, why not just use the libusb-win32
> library, so that you don't have to worry about getting these details
> correct?
> --
I can't because I've made some changes in libusb source. Not much just added
a method that calls this DeviceIoControl which is not working.
Do I need to make some change in request for LIBUSB_IOCTL_GET_VERSION. From
where I can know that how to prepare requests.
Also I've a previous version of libusb device driver(libusb0.sys). This
LIBUSB_IOCTL_USBD_STATUS_READ works fine with it but not with latest device
driver.
> Sorry, You were right LIBUSB_IOCTL_USBD_STATUS_READ is not defined in
> default driver_api.h. It was actually added by me.
> But no other operation is also working. I mean I've tried passing
> LIBUSB_IOCTL_GET_VERSION, but it also gives Invalid parameter error.
Look, why are you trying to use libusb to talk to a USB drive anyway?
What are you trying to do?
If you are modifying the driver source code, surely you know how to read
the source code to figure out what parameters are needed.
LIBUSB_IOCTL_GET_VERSION does not use an input buffer, and the output
buffer must be a libusb_request structure.
--