Skip to content

Commit

Permalink
[usb] Use non-zero language ID to retrieve strings
Browse files Browse the repository at this point in the history
We currently use a zero language ID to retrieve strings such as the
ECM/NCM MAC address.  This works on most hardware devices, but is
known to fail on some software emulated CDC-NCM devices.

Fix by using the first supported language ID, falling back to English
(0x0409) if any error occurs when fetching the list of supported
languages.  This matches the behaviour of the Linux kernel.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
  • Loading branch information
mcb30 committed Jul 3, 2017
1 parent 1e5c5a2 commit 8e48d0d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
37 changes: 36 additions & 1 deletion src/drivers/bus/usb.c
Expand Up @@ -843,12 +843,40 @@ int usb_control ( struct usb_device *usb, unsigned int request,
return rc;
}

/**
* Get default language ID
*
* @v usb USB device
* @ret language Language ID
*/
static unsigned int usb_get_default_language ( struct usb_device *usb ) {
struct {
struct usb_descriptor_header header;
uint16_t language[1];
} __attribute__ (( packed )) desc;
unsigned int language;
int rc;

/* Get descriptor */
if ( ( rc = usb_get_descriptor ( usb, 0, USB_STRING_DESCRIPTOR, 0, 0,
&desc.header, sizeof ( desc ) ) ) !=0){
DBGC ( usb, "USB %s has no default language: %s\n",
usb->name, strerror ( rc ) );
return USB_LANG_ENGLISH;
}

/* Use first language ID */
language = le16_to_cpu ( desc.language[0] );
DBGC2 ( usb, "USB %s default language %#04x\n", usb->name, language );
return language;
}

/**
* Get USB string descriptor
*
* @v usb USB device
* @v index String index
* @v language Language ID
* @v language Language ID, or 0 to use default
* @v buf Data buffer
* @v len Length of buffer
* @ret len String length (excluding NUL), or negative error
Expand All @@ -864,6 +892,13 @@ int usb_get_string_descriptor ( struct usb_device *usb, unsigned int index,
unsigned int i;
int rc;

/* Use default language ID, if applicable */
if ( ( language == 0 ) && ( index != 0 ) ) {
if ( ! usb->language )
usb->language = usb_get_default_language ( usb );
language = usb->language;
}

/* Allocate buffer for string */
desc = malloc ( sizeof ( *desc ) );
if ( ! desc ) {
Expand Down
6 changes: 6 additions & 0 deletions src/include/ipxe/usb.h
Expand Up @@ -223,6 +223,9 @@ struct usb_string_descriptor {
/** A USB string descriptor */
#define USB_STRING_DESCRIPTOR 3

/** Language ID for English */
#define USB_LANG_ENGLISH 0x0409

/** A USB interface descriptor */
struct usb_interface_descriptor {
/** Descriptor header */
Expand Down Expand Up @@ -728,6 +731,9 @@ struct usb_device {
struct usb_endpoint control;
/** Completed control transfers */
struct list_head complete;

/** Default language ID (if known) */
unsigned int language;
};

/** USB device host controller operations */
Expand Down

0 comments on commit 8e48d0d

Please sign in to comment.