aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb/gadget/composite.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/usb/gadget/composite.c')
-rw-r--r--drivers/usb/gadget/composite.c139
1 files changed, 93 insertions, 46 deletions
diff --git a/drivers/usb/gadget/composite.c b/drivers/usb/gadget/composite.c
index 1160c55de7f2..7b5cc16e4a0b 100644
--- a/drivers/usb/gadget/composite.c
+++ b/drivers/usb/gadget/composite.c
@@ -24,6 +24,7 @@
24#include <linux/kernel.h> 24#include <linux/kernel.h>
25#include <linux/slab.h> 25#include <linux/slab.h>
26#include <linux/device.h> 26#include <linux/device.h>
27#include <linux/utsname.h>
27 28
28#include <linux/usb/composite.h> 29#include <linux/usb/composite.h>
29 30
@@ -39,6 +40,7 @@
39#define USB_BUFSIZ 1024 40#define USB_BUFSIZ 1024
40 41
41static struct usb_composite_driver *composite; 42static struct usb_composite_driver *composite;
43static int (*composite_gadget_bind)(struct usb_composite_dev *cdev);
42 44
43/* Some systems will need runtime overrides for the product identifers 45/* Some systems will need runtime overrides for the product identifers
44 * published in the device descriptor, either numbers or strings or both. 46 * published in the device descriptor, either numbers or strings or both.
@@ -69,6 +71,8 @@ static char *iSerialNumber;
69module_param(iSerialNumber, charp, 0); 71module_param(iSerialNumber, charp, 0);
70MODULE_PARM_DESC(iSerialNumber, "SerialNumber string"); 72MODULE_PARM_DESC(iSerialNumber, "SerialNumber string");
71 73
74static char composite_manufacturer[50];
75
72/*-------------------------------------------------------------------------*/ 76/*-------------------------------------------------------------------------*/
73 77
74/** 78/**
@@ -470,18 +474,20 @@ done:
470 * usb_add_config() - add a configuration to a device. 474 * usb_add_config() - add a configuration to a device.
471 * @cdev: wraps the USB gadget 475 * @cdev: wraps the USB gadget
472 * @config: the configuration, with bConfigurationValue assigned 476 * @config: the configuration, with bConfigurationValue assigned
477 * @bind: the configuration's bind function
473 * Context: single threaded during gadget setup 478 * Context: single threaded during gadget setup
474 * 479 *
475 * One of the main tasks of a composite driver's bind() routine is to 480 * One of the main tasks of a composite @bind() routine is to
476 * add each of the configurations it supports, using this routine. 481 * add each of the configurations it supports, using this routine.
477 * 482 *
478 * This function returns the value of the configuration's bind(), which 483 * This function returns the value of the configuration's @bind(), which
479 * is zero for success else a negative errno value. Binding configurations 484 * is zero for success else a negative errno value. Binding configurations
480 * assigns global resources including string IDs, and per-configuration 485 * assigns global resources including string IDs, and per-configuration
481 * resources such as interface IDs and endpoints. 486 * resources such as interface IDs and endpoints.
482 */ 487 */
483int usb_add_config(struct usb_composite_dev *cdev, 488int usb_add_config(struct usb_composite_dev *cdev,
484 struct usb_configuration *config) 489 struct usb_configuration *config,
490 int (*bind)(struct usb_configuration *))
485{ 491{
486 int status = -EINVAL; 492 int status = -EINVAL;
487 struct usb_configuration *c; 493 struct usb_configuration *c;
@@ -490,7 +496,7 @@ int usb_add_config(struct usb_composite_dev *cdev,
490 config->bConfigurationValue, 496 config->bConfigurationValue,
491 config->label, config); 497 config->label, config);
492 498
493 if (!config->bConfigurationValue || !config->bind) 499 if (!config->bConfigurationValue || !bind)
494 goto done; 500 goto done;
495 501
496 /* Prevent duplicate configuration identifiers */ 502 /* Prevent duplicate configuration identifiers */
@@ -507,7 +513,7 @@ int usb_add_config(struct usb_composite_dev *cdev,
507 INIT_LIST_HEAD(&config->functions); 513 INIT_LIST_HEAD(&config->functions);
508 config->next_interface_id = 0; 514 config->next_interface_id = 0;
509 515
510 status = config->bind(config); 516 status = bind(config);
511 if (status < 0) { 517 if (status < 0) {
512 list_del(&config->list); 518 list_del(&config->list);
513 config->cdev = NULL; 519 config->cdev = NULL;
@@ -533,7 +539,7 @@ int usb_add_config(struct usb_composite_dev *cdev,
533 } 539 }
534 } 540 }
535 541
536 /* set_alt(), or next config->bind(), sets up 542 /* set_alt(), or next bind(), sets up
537 * ep->driver_data as needed. 543 * ep->driver_data as needed.
538 */ 544 */
539 usb_ep_autoconfig_reset(cdev->gadget); 545 usb_ep_autoconfig_reset(cdev->gadget);
@@ -599,6 +605,7 @@ static int get_string(struct usb_composite_dev *cdev,
599 struct usb_configuration *c; 605 struct usb_configuration *c;
600 struct usb_function *f; 606 struct usb_function *f;
601 int len; 607 int len;
608 const char *str;
602 609
603 /* Yes, not only is USB's I18N support probably more than most 610 /* Yes, not only is USB's I18N support probably more than most
604 * folk will ever care about ... also, it's all supported here. 611 * folk will ever care about ... also, it's all supported here.
@@ -638,9 +645,29 @@ static int get_string(struct usb_composite_dev *cdev,
638 return s->bLength; 645 return s->bLength;
639 } 646 }
640 647
641 /* Otherwise, look up and return a specified string. String IDs 648 /* Otherwise, look up and return a specified string. First
642 * are device-scoped, so we look up each string table we're told 649 * check if the string has not been overridden.
643 * about. These lookups are infrequent; simpler-is-better here. 650 */
651 if (cdev->manufacturer_override == id)
652 str = iManufacturer ?: composite->iManufacturer ?:
653 composite_manufacturer;
654 else if (cdev->product_override == id)
655 str = iProduct ?: composite->iProduct;
656 else if (cdev->serial_override == id)
657 str = iSerialNumber;
658 else
659 str = NULL;
660 if (str) {
661 struct usb_gadget_strings strings = {
662 .language = language,
663 .strings = &(struct usb_string) { 0xff, str }
664 };
665 return usb_gadget_get_string(&strings, 0xff, buf);
666 }
667
668 /* String IDs are device-scoped, so we look up each string
669 * table we're told about. These lookups are infrequent;
670 * simpler-is-better here.
644 */ 671 */
645 if (composite->strings) { 672 if (composite->strings) {
646 len = lookup_string(composite->strings, buf, language, id); 673 len = lookup_string(composite->strings, buf, language, id);
@@ -901,7 +928,8 @@ unknown:
901 */ 928 */
902 switch (ctrl->bRequestType & USB_RECIP_MASK) { 929 switch (ctrl->bRequestType & USB_RECIP_MASK) {
903 case USB_RECIP_INTERFACE: 930 case USB_RECIP_INTERFACE:
904 f = cdev->config->interface[intf]; 931 if (cdev->config)
932 f = cdev->config->interface[intf];
905 break; 933 break;
906 934
907 case USB_RECIP_ENDPOINT: 935 case USB_RECIP_ENDPOINT:
@@ -1025,26 +1053,17 @@ composite_unbind(struct usb_gadget *gadget)
1025 composite = NULL; 1053 composite = NULL;
1026} 1054}
1027 1055
1028static void 1056static u8 override_id(struct usb_composite_dev *cdev, u8 *desc)
1029string_override_one(struct usb_gadget_strings *tab, u8 id, const char *s)
1030{ 1057{
1031 struct usb_string *str = tab->strings; 1058 if (!*desc) {
1032 1059 int ret = usb_string_id(cdev);
1033 for (str = tab->strings; str->s; str++) { 1060 if (unlikely(ret < 0))
1034 if (str->id == id) { 1061 WARNING(cdev, "failed to override string ID\n");
1035 str->s = s; 1062 else
1036 return; 1063 *desc = ret;
1037 }
1038 } 1064 }
1039}
1040 1065
1041static void 1066 return *desc;
1042string_override(struct usb_gadget_strings **tab, u8 id, const char *s)
1043{
1044 while (*tab) {
1045 string_override_one(*tab, id, s);
1046 tab++;
1047 }
1048} 1067}
1049 1068
1050static int composite_bind(struct usb_gadget *gadget) 1069static int composite_bind(struct usb_gadget *gadget)
@@ -1074,7 +1093,13 @@ static int composite_bind(struct usb_gadget *gadget)
1074 cdev->bufsiz = USB_BUFSIZ; 1093 cdev->bufsiz = USB_BUFSIZ;
1075 cdev->driver = composite; 1094 cdev->driver = composite;
1076 1095
1077 usb_gadget_set_selfpowered(gadget); 1096 /*
1097 * As per USB compliance update, a device that is actively drawing
1098 * more than 100mA from USB must report itself as bus-powered in
1099 * the GetStatus(DEVICE) call.
1100 */
1101 if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW)
1102 usb_gadget_set_selfpowered(gadget);
1078 1103
1079 /* interface and string IDs start at zero via kzalloc. 1104 /* interface and string IDs start at zero via kzalloc.
1080 * we force endpoints to start unassigned; few controller 1105 * we force endpoints to start unassigned; few controller
@@ -1094,26 +1119,41 @@ static int composite_bind(struct usb_gadget *gadget)
1094 * serial number), register function drivers, potentially update 1119 * serial number), register function drivers, potentially update
1095 * power state and consumption, etc 1120 * power state and consumption, etc
1096 */ 1121 */
1097 status = composite->bind(cdev); 1122 status = composite_gadget_bind(cdev);
1098 if (status < 0) 1123 if (status < 0)
1099 goto fail; 1124 goto fail;
1100 1125
1101 cdev->desc = *composite->dev; 1126 cdev->desc = *composite->dev;
1102 cdev->desc.bMaxPacketSize0 = gadget->ep0->maxpacket; 1127 cdev->desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1103 1128
1104 /* strings can't be assigned before bind() allocates the 1129 /* stirng overrides */
1105 * releavnt identifiers 1130 if (iManufacturer || !cdev->desc.iManufacturer) {
1106 */ 1131 if (!iManufacturer && !composite->iManufacturer &&
1107 if (cdev->desc.iManufacturer && iManufacturer) 1132 !*composite_manufacturer)
1108 string_override(composite->strings, 1133 snprintf(composite_manufacturer,
1109 cdev->desc.iManufacturer, iManufacturer); 1134 sizeof composite_manufacturer,
1110 if (cdev->desc.iProduct && iProduct) 1135 "%s %s with %s",
1111 string_override(composite->strings, 1136 init_utsname()->sysname,
1112 cdev->desc.iProduct, iProduct); 1137 init_utsname()->release,
1113 if (cdev->desc.iSerialNumber && iSerialNumber) 1138 gadget->name);
1114 string_override(composite->strings, 1139
1115 cdev->desc.iSerialNumber, iSerialNumber); 1140 cdev->manufacturer_override =
1141 override_id(cdev, &cdev->desc.iManufacturer);
1142 }
1143
1144 if (iProduct || (!cdev->desc.iProduct && composite->iProduct))
1145 cdev->product_override =
1146 override_id(cdev, &cdev->desc.iProduct);
1147
1148 if (iSerialNumber)
1149 cdev->serial_override =
1150 override_id(cdev, &cdev->desc.iSerialNumber);
1151
1152 /* has userspace failed to provide a serial number? */
1153 if (composite->needs_serial && !cdev->desc.iSerialNumber)
1154 WARNING(cdev, "userspace failed to provide iSerialNumber\n");
1116 1155
1156 /* finish up */
1117 status = device_create_file(&gadget->dev, &dev_attr_suspended); 1157 status = device_create_file(&gadget->dev, &dev_attr_suspended);
1118 if (status) 1158 if (status)
1119 goto fail; 1159 goto fail;
@@ -1177,7 +1217,6 @@ composite_resume(struct usb_gadget *gadget)
1177static struct usb_gadget_driver composite_driver = { 1217static struct usb_gadget_driver composite_driver = {
1178 .speed = USB_SPEED_HIGH, 1218 .speed = USB_SPEED_HIGH,
1179 1219
1180 .bind = composite_bind,
1181 .unbind = composite_unbind, 1220 .unbind = composite_unbind,
1182 1221
1183 .setup = composite_setup, 1222 .setup = composite_setup,
@@ -1192,8 +1231,12 @@ static struct usb_gadget_driver composite_driver = {
1192}; 1231};
1193 1232
1194/** 1233/**
1195 * usb_composite_register() - register a composite driver 1234 * usb_composite_probe() - register a composite driver
1196 * @driver: the driver to register 1235 * @driver: the driver to register
1236 * @bind: the callback used to allocate resources that are shared across the
1237 * whole device, such as string IDs, and add its configurations using
1238 * @usb_add_config(). This may fail by returning a negative errno
1239 * value; it should return zero on successful initialization.
1197 * Context: single threaded during gadget setup 1240 * Context: single threaded during gadget setup
1198 * 1241 *
1199 * This function is used to register drivers using the composite driver 1242 * This function is used to register drivers using the composite driver
@@ -1206,18 +1249,22 @@ static struct usb_gadget_driver composite_driver = {
1206 * while it was binding. That would usually be done in order to wait for 1249 * while it was binding. That would usually be done in order to wait for
1207 * some userspace participation. 1250 * some userspace participation.
1208 */ 1251 */
1209int usb_composite_register(struct usb_composite_driver *driver) 1252extern int usb_composite_probe(struct usb_composite_driver *driver,
1253 int (*bind)(struct usb_composite_dev *cdev))
1210{ 1254{
1211 if (!driver || !driver->dev || !driver->bind || composite) 1255 if (!driver || !driver->dev || !bind || composite)
1212 return -EINVAL; 1256 return -EINVAL;
1213 1257
1258 if (!driver->iProduct)
1259 driver->iProduct = driver->name;
1214 if (!driver->name) 1260 if (!driver->name)
1215 driver->name = "composite"; 1261 driver->name = "composite";
1216 composite_driver.function = (char *) driver->name; 1262 composite_driver.function = (char *) driver->name;
1217 composite_driver.driver.name = driver->name; 1263 composite_driver.driver.name = driver->name;
1218 composite = driver; 1264 composite = driver;
1265 composite_gadget_bind = bind;
1219 1266
1220 return usb_gadget_register_driver(&composite_driver); 1267 return usb_gadget_probe_driver(&composite_driver, composite_bind);
1221} 1268}
1222 1269
1223/** 1270/**