Failed to allocate memory for the device's Configuration Descriptor

Hi,

I’m developing a usb device driver and want to
allocate memory for the device’s configuration Descriptor, but
if (!pCurrInst->pUsbConfigDesc)
always returns false!

here is my code, hope anyone can help me:

for (ii = 1; ii <= MAX_DEVICES; ii++)
{
pCurrInst = &DeviceList[ii];
pCurrInst->DeviceNumber = 0;
pCurrInst->DeviceType = 0;
memset(pCurrInst->FWVersion, 0, sizeof(pCurrInst->FWVersion));
memset(&(pCurrInst->Features), 0, sizeof(pCurrInst->Features));
pCurrInst->Protocol2 = FALSE;

if (pCurrInst->pUsbConfigDesc) ExFreePool(pCurrInst->pUsbConfigDesc);
pCurrInst->pUsbConfigDesc = (PUSB_CONFIGURATION_DESCRIPTOR) ExAllocatePoolWithTag(NonPagedPool, CONFIGDESCSIZE,‘1GAT’);

if (!pCurrInst->pUsbConfigDesc)
return FALSE;
}

You know that arrays’ indexing starts with 0 (not 1) in C/C++. So your code will most likely crash at some point (unless your DeviceList array size is at least MAX_DEVICES + 1). Make sure that CONFIGDESCSIZE has some sensible value (e.g. not 0 and it’s not too big).

Krzysztof Uchronski

Typically you
A) only have one device’s config desc to retrieve
B) you query for the config desc size first and do not use a hard coded value

What kind of driver is this that you have an array of config descriptors?

d

tiny phone keyboard + fat thumbs = you do the muth

-----Original Message-----
From: xxxxx@wolfvision.net
Sent: Tuesday, January 19, 2010 1:26 AM
To: Windows System Software Devs Interest List
Subject: [ntdev] Failed to allocate memory for the device’s Configuration Descriptor

Hi,

I’m developing a usb device driver and want to
allocate memory for the device’s configuration Descriptor, but
if (!pCurrInst->pUsbConfigDesc)
always returns false!

here is my code, hope anyone can help me:

for (ii = 1; ii <= MAX_DEVICES; ii++)
{
pCurrInst = &DeviceList[ii];
pCurrInst->DeviceNumber = 0;
pCurrInst->DeviceType = 0;
memset(pCurrInst->FWVersion, 0, sizeof(pCurrInst->FWVersion));
memset(&(pCurrInst->Features), 0, sizeof(pCurrInst->Features));
pCurrInst->Protocol2 = FALSE;

if (pCurrInst->pUsbConfigDesc) ExFreePool(pCurrInst->pUsbConfigDesc);
pCurrInst->pUsbConfigDesc = (PUSB_CONFIGURATION_DESCRIPTOR) ExAllocatePoolWithTag(NonPagedPool, CONFIGDESCSIZE,‘1GAT’);

if (!pCurrInst->pUsbConfigDesc)
return FALSE;
}


NTDEV is sponsored by OSR

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer

There can be multiple devices connected on different usb ports. The devices are visualizers.

It’s not a hard coded value. The CONFIGDESCSIZE is defined following:
sizeof(USB_CONFIGURATION_DESCRIPTOR) + sizeof(USBD_INTERFACE_INFORMATION) + (sizeof(USB_DEVICE_DESCRIPTOR) * MAX_EPCOUNT)

That would be a hard coded value and even if there are multiple devices plugged in, you typically have a per device storage area in your device extension of wdfdevice context area. Having global data like this is typically not a good idea.

d

tiny phone keyboard + fat thumbs = you do the muth

-----Original Message-----
From: xxxxx@wolfvision.net
Sent: Tuesday, January 19, 2010 7:52 AM
To: Windows System Software Devs Interest List
Subject: RE:[ntdev] Failed to allocate memory for the device’s Configuration Descriptor

There can be multiple devices connected on different usb ports. The devices are visualizers.

It’s not a hard coded value. The CONFIGDESCSIZE is defined following:
sizeof(USB_CONFIGURATION_DESCRIPTOR) + sizeof(USBD_INTERFACE_INFORMATION) + (sizeof(USB_DEVICE_DESCRIPTOR) * MAX_EPCOUNT)


NTDEV is sponsored by OSR

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer

Unless MAX_EPCOUNT is some huge number the code you have provided
should not always fail to allocate a buffer. I’m guessing that you
have edited the code and we are not looking at what really is going
on, or you have left out some vital information regarding when this
code fails.

Mark Roddy

On Tue, Jan 19, 2010 at 10:29 AM, Doron Holan wrote:
> Typically you
> A) only have one device’s config desc to retrieve
> B) you query for the config desc size first and do not use a hard coded value
>
> What kind of driver is this that you have an array of config descriptors?
>
> d
>
> tiny phone keyboard + fat thumbs = you do the muth
>
>
>
> -----Original Message-----
> From: xxxxx@wolfvision.net
> Sent: Tuesday, January 19, 2010 1:26 AM
> To: Windows System Software Devs Interest List
> Subject: [ntdev] Failed to allocate memory for the device’s Configuration Descriptor
>
>
> Hi,
>
> I’m developing a usb device driver and want to
> allocate memory for the device’s configuration Descriptor, but
> if (!pCurrInst->pUsbConfigDesc)
> always returns false!
>
> here is my code, hope anyone can help me:
>
> ? ? ? ?for (ii = 1; ii <= MAX_DEVICES; ii++)
> ? ? ? ?{
> ? ? ? ? ? ? ? ?pCurrInst = &DeviceList[ii];
> ? ? ? ? ? ? ? ?pCurrInst->DeviceNumber = 0;
> ? ? ? ? ? ? ? ?pCurrInst->DeviceType = 0;
> ? ? ? ? ? ? ? ?memset(pCurrInst->FWVersion, 0, sizeof(pCurrInst->FWVersion));
> ? ? ? ? ? ? ? ?memset(&(pCurrInst->Features), 0, sizeof(pCurrInst->Features));
> ? ? ? ? ? ? ? ?pCurrInst->Protocol2 = FALSE;
>
> ? ? ? ? ? ? ? ?if (pCurrInst->pUsbConfigDesc) ExFreePool(pCurrInst->pUsbConfigDesc);
> ? ? ? ? ? ? ? ?pCurrInst->pUsbConfigDesc = (PUSB_CONFIGURATION_DESCRIPTOR) ExAllocatePoolWithTag(NonPagedPool, CONFIGDESCSIZE,‘1GAT’);
>
> ? ? ? ? ? ? ? ?if (!pCurrInst->pUsbConfigDesc)
> ? ? ? ? ? ? ? ? ? ? ? ?return FALSE;
> ? ? ? ?}
>
> —
> NTDEV is sponsored by OSR
>
> For our schedule of WDF, WDM, debugging and other seminars visit:
> http://www.osr.com/seminars
>
> To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer
>
>
> —
> NTDEV is sponsored by OSR
>
> For our schedule of WDF, WDM, debugging and other seminars visit:
> http://www.osr.com/seminars
>
> To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer
>

xxxxx@wolfvision.net wrote:

There can be multiple devices connected on different usb ports.

Maybe, but they won’t be handled by the same driver instance. Is that
array a (gasp) global?

The devices are visualizers.

It’s not a hard coded value. The CONFIGDESCSIZE is defined following:
sizeof(USB_CONFIGURATION_DESCRIPTOR) + sizeof(USBD_INTERFACE_INFORMATION) + (sizeof(USB_DEVICE_DESCRIPTOR) * MAX_EPCOUNT)

I’m not sure how you define the term, but in my world, that’s a
hard-coded value. The proper way to fetch a configuration descriptor is
to fetch the first sizeof(USB_CONFIGURATION_DESCRIPTOR) bytes first.
That part of the structure contains the total actual length of the
configuration descriptor. You then allocate that much space and read it
again.


Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.