On Mon, 21 Jun 2004, sting sting wrote:

> Hello,
> 
> My question is regarding 2.4.20 kernel , but as I looked of 2.6 kernel code 
> , it's
> seems also relevant.
> 
> the descriptor struct size is 18 bytes (it is the descriptor member is 
> usb_device struct (struct usb_device_descriptor) ;
> 
> I see tha in usb.c , in usb_new_device() , there's a call to
> 
> usb_get_descriptor(dev, USB_DT_DEVICE, 0, &dev->descriptor, 8)
> 
> (Note : the last paramter, 8 , means that we read only 8 bytes instead of 18 
> , the real size)
> and afterwards
> 
> dev->epmaxpacketin [0] = dev->descriptor.bMaxPacketSize0;
> dev->epmaxpacketout[0] = dev->descriptor.bMaxPacketSize0;
> 
> err = usb_get_device_descriptor(dev);
> 
> Why is the first call with 8 bytes, than setting of epmaxpacketin 
> [0]/epmaxpacketout [0] ,
> and then again a call to usb_get_device_descriptor() , wheras this second 
> call in fact calls
> usb_get_descriptor() with size 18 ?
> 
> What is wrong with reading 18 bytes , the full structure, at first try ?
> 
> I saw some posting in this forum (from quite a long time ago); They say 
> there that you should not
> try to read length higher than length in fact (like 128); I agree; But why 
> not to read tha actual length , 18?
> 
> regards
> Sting

It's because of the way USB works.  Data is transferred in packets, and 
each endpoint (like endpoint 0, used for transferring descriptors) has a 
maximum packet size.  If the amount of data to be transferred is larger 
than the maximum packet size, it gets divided up into multiple packets.  
Each packet has to be exactly the maximum size except for the last one, 
which is allowed to be smaller.

Now you can see the problem.  Initially we _do not know_ the maximum
packet size for endpoint 0.  It could be any power of 2 between 8 and 64.  
So if we wanted to read an 18-byte descriptor, should we expect the device
to transfer two 8-byte packets followed by a 2-byte packet, or a 16-byte
packet followed by a 2-byte packet, or an 18-byte packet?  If we guess
wrong the transfer will fail.

The answer is to read just the first eight bytes of the descriptor.  That
can always be done with a single packet transfer.  Contained within those
eight bytes is the actual maxpacket size for endpoint 0.  Once we know
that, we can go ahead and transfer the entire descriptor.

Alan Stern



-------------------------------------------------------
This SF.Net email is sponsored by The 2004 JavaOne(SM) Conference
Learn from the experts at JavaOne(SM), Sun's Worldwide Java Developer
Conference, June 28 - July 1 at the Moscone Center in San Francisco, CA
REGISTER AND SAVE! http://java.sun.com/javaone/sf Priority Code NWMGYKND
_______________________________________________
[EMAIL PROTECTED]
To unsubscribe, use the last form field at:
https://lists.sourceforge.net/lists/listinfo/linux-usb-devel

Reply via email to