Commit ad1a8102 authored by Michal Nazarewicz's avatar Michal Nazarewicz Committed by Greg Kroah-Hartman

USB: gadget: composite: Better string override handling

The iManufatcurer, iProduct and iSerialNumber composite module
parameters were only used when the gadget driver registers
strings for manufacturer, product and serial number.  If the
gadget never bothered to set corresponding fields in USB device
descriptors those module parameters are ignored.

This commit makes the parameters work even if the strings ID
have not been assigned.  It also changes the way IDs are
overridden -- what IDs are overridden is now saved in
usb_composite_dev structure -- which makes it unnecessary to
modify the string tables the way previous code did.

The commit also adds a iProduct and iManufatcurer fields to the
usb_composite_device structure.  If they are set, appropriate
strings are reserved and added to device descriptor.  This makes
it unnecessary for gadget drivers to maintain code for setting
those.  If iProduct is not set it defaults to
usb_composite_device::name; if iManufatcurer is not set
a default "<system> <release> with <gadget-name>" is used.

The last thing is that if needs_serial field of
usb_composite_device is set and user failed to provided
iSerialNumber parameter a warning is issued.
Signed-off-by: default avatarMichal Nazarewicz <m.nazarewicz@samsung.com>
Signed-off-by: default avatarKyungmin Park <kyungmin.park@samsung.com>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@suse.de>
parent fc19de61
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
#include <linux/kernel.h> #include <linux/kernel.h>
#include <linux/slab.h> #include <linux/slab.h>
#include <linux/device.h> #include <linux/device.h>
#include <linux/utsname.h>
#include <linux/usb/composite.h> #include <linux/usb/composite.h>
...@@ -69,6 +70,8 @@ static char *iSerialNumber; ...@@ -69,6 +70,8 @@ static char *iSerialNumber;
module_param(iSerialNumber, charp, 0); module_param(iSerialNumber, charp, 0);
MODULE_PARM_DESC(iSerialNumber, "SerialNumber string"); MODULE_PARM_DESC(iSerialNumber, "SerialNumber string");
static char composite_manufacturer[50];
/*-------------------------------------------------------------------------*/ /*-------------------------------------------------------------------------*/
/** /**
...@@ -599,6 +602,7 @@ static int get_string(struct usb_composite_dev *cdev, ...@@ -599,6 +602,7 @@ static int get_string(struct usb_composite_dev *cdev,
struct usb_configuration *c; struct usb_configuration *c;
struct usb_function *f; struct usb_function *f;
int len; int len;
const char *str;
/* Yes, not only is USB's I18N support probably more than most /* Yes, not only is USB's I18N support probably more than most
* folk will ever care about ... also, it's all supported here. * folk will ever care about ... also, it's all supported here.
...@@ -638,9 +642,29 @@ static int get_string(struct usb_composite_dev *cdev, ...@@ -638,9 +642,29 @@ static int get_string(struct usb_composite_dev *cdev,
return s->bLength; return s->bLength;
} }
/* Otherwise, look up and return a specified string. String IDs /* Otherwise, look up and return a specified string. First
* are device-scoped, so we look up each string table we're told * check if the string has not been overridden.
* about. These lookups are infrequent; simpler-is-better here. */
if (cdev->manufacturer_override == id)
str = iManufacturer ?: composite->iManufacturer ?:
composite_manufacturer;
else if (cdev->product_override == id)
str = iProduct ?: composite->iProduct;
else if (cdev->serial_override == id)
str = iSerialNumber;
else
str = NULL;
if (str) {
struct usb_gadget_strings strings = {
.language = language,
.strings = &(struct usb_string) { 0xff, str }
};
return usb_gadget_get_string(&strings, 0xff, buf);
}
/* String IDs are device-scoped, so we look up each string
* table we're told about. These lookups are infrequent;
* simpler-is-better here.
*/ */
if (composite->strings) { if (composite->strings) {
len = lookup_string(composite->strings, buf, language, id); len = lookup_string(composite->strings, buf, language, id);
...@@ -1025,26 +1049,17 @@ composite_unbind(struct usb_gadget *gadget) ...@@ -1025,26 +1049,17 @@ composite_unbind(struct usb_gadget *gadget)
composite = NULL; composite = NULL;
} }
static void static u8 override_id(struct usb_composite_dev *cdev, u8 *desc)
string_override_one(struct usb_gadget_strings *tab, u8 id, const char *s)
{ {
struct usb_string *str = tab->strings; if (!*desc) {
int ret = usb_string_id(cdev);
for (str = tab->strings; str->s; str++) { if (unlikely(ret < 0))
if (str->id == id) { WARNING(cdev, "failed to override string ID\n");
str->s = s; else
return; *desc = ret;
}
} }
}
static void return *desc;
string_override(struct usb_gadget_strings **tab, u8 id, const char *s)
{
while (*tab) {
string_override_one(*tab, id, s);
tab++;
}
} }
static int composite_bind(struct usb_gadget *gadget) static int composite_bind(struct usb_gadget *gadget)
...@@ -1107,19 +1122,34 @@ static int composite_bind(struct usb_gadget *gadget) ...@@ -1107,19 +1122,34 @@ static int composite_bind(struct usb_gadget *gadget)
cdev->desc = *composite->dev; cdev->desc = *composite->dev;
cdev->desc.bMaxPacketSize0 = gadget->ep0->maxpacket; cdev->desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
/* strings can't be assigned before bind() allocates the /* stirng overrides */
* releavnt identifiers if (iManufacturer || !cdev->desc.iManufacturer) {
*/ if (!iManufacturer && !composite->iManufacturer &&
if (cdev->desc.iManufacturer && iManufacturer) !*composite_manufacturer)
string_override(composite->strings, snprintf(composite_manufacturer,
cdev->desc.iManufacturer, iManufacturer); sizeof composite_manufacturer,
if (cdev->desc.iProduct && iProduct) "%s %s with %s",
string_override(composite->strings, init_utsname()->sysname,
cdev->desc.iProduct, iProduct); init_utsname()->release,
if (cdev->desc.iSerialNumber && iSerialNumber) gadget->name);
string_override(composite->strings,
cdev->desc.iSerialNumber, iSerialNumber); cdev->manufacturer_override =
override_id(cdev, &cdev->desc.iManufacturer);
}
if (iProduct || (!cdev->desc.iProduct && composite->iProduct))
cdev->product_override =
override_id(cdev, &cdev->desc.iProduct);
if (iSerialNumber)
cdev->serial_override =
override_id(cdev, &cdev->desc.iSerialNumber);
/* has userspace failed to provide a serial number? */
if (composite->needs_serial && !cdev->desc.iSerialNumber)
WARNING(cdev, "userspace failed to provide iSerialNumber\n");
/* finish up */
status = device_create_file(&gadget->dev, &dev_attr_suspended); status = device_create_file(&gadget->dev, &dev_attr_suspended);
if (status) if (status)
goto fail; goto fail;
...@@ -1217,6 +1247,8 @@ int usb_composite_register(struct usb_composite_driver *driver) ...@@ -1217,6 +1247,8 @@ int usb_composite_register(struct usb_composite_driver *driver)
if (!driver || !driver->dev || !driver->bind || composite) if (!driver || !driver->dev || !driver->bind || composite)
return -EINVAL; return -EINVAL;
if (!driver->iProduct)
driver->iProduct = driver->name;
if (!driver->name) if (!driver->name)
driver->name = "composite"; driver->name = "composite";
composite_driver.function = (char *) driver->name; composite_driver.function = (char *) driver->name;
......
...@@ -237,10 +237,17 @@ int usb_add_config(struct usb_composite_dev *, ...@@ -237,10 +237,17 @@ int usb_add_config(struct usb_composite_dev *,
/** /**
* struct usb_composite_driver - groups configurations into a gadget * struct usb_composite_driver - groups configurations into a gadget
* @name: For diagnostics, identifies the driver. * @name: For diagnostics, identifies the driver.
* @iProduct: Used as iProduct override if @dev->iProduct is not set.
* If NULL value of @name is taken.
* @iManufacturer: Used as iManufacturer override if @dev->iManufacturer is
* not set. If NULL a default "<system> <release> with <udc>" value
* will be used.
* @dev: Template descriptor for the device, including default device * @dev: Template descriptor for the device, including default device
* identifiers. * identifiers.
* @strings: tables of strings, keyed by identifiers assigned during bind() * @strings: tables of strings, keyed by identifiers assigned during bind()
* and language IDs provided in control requests * and language IDs provided in control requests
* @needs_serial: set to 1 if the gadget needs userspace to provide
* a serial number. If one is not provided, warning will be printed.
* @bind: (REQUIRED) Used to allocate resources that are shared across the * @bind: (REQUIRED) Used to allocate resources that are shared across the
* whole device, such as string IDs, and add its configurations using * whole device, such as string IDs, and add its configurations using
* @usb_add_config(). This may fail by returning a negative errno * @usb_add_config(). This may fail by returning a negative errno
...@@ -266,8 +273,11 @@ int usb_add_config(struct usb_composite_dev *, ...@@ -266,8 +273,11 @@ int usb_add_config(struct usb_composite_dev *,
*/ */
struct usb_composite_driver { struct usb_composite_driver {
const char *name; const char *name;
const char *iProduct;
const char *iManufacturer;
const struct usb_device_descriptor *dev; const struct usb_device_descriptor *dev;
struct usb_gadget_strings **strings; struct usb_gadget_strings **strings;
unsigned needs_serial:1;
/* REVISIT: bind() functions can be marked __init, which /* REVISIT: bind() functions can be marked __init, which
* makes trouble for section mismatch analysis. See if * makes trouble for section mismatch analysis. See if
...@@ -334,6 +344,9 @@ struct usb_composite_dev { ...@@ -334,6 +344,9 @@ struct usb_composite_dev {
struct list_head configs; struct list_head configs;
struct usb_composite_driver *driver; struct usb_composite_driver *driver;
u8 next_string_id; u8 next_string_id;
u8 manufacturer_override;
u8 product_override;
u8 serial_override;
/* the gadget driver won't enable the data pullup /* the gadget driver won't enable the data pullup
* while the deactivation count is nonzero. * while the deactivation count is nonzero.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment