15 #include <linux/kernel.h>
16 #include <linux/slab.h>
17 #include <linux/module.h>
18 #include <linux/device.h>
19 #include <linux/utsname.h>
22 #include <asm/unaligned.h>
56 #define for_each_ep_desc(start, ep_desc) \
57 for (ep_desc = next_ep_desc(start); \
58 ep_desc; ep_desc = next_ep_desc(ep_desc+1))
87 int want_comp_desc = 0;
97 if (gadget_is_superspeed(g)) {
104 if (gadget_is_dualspeed(g)) {
122 _ep->
maxpacket = usb_endpoint_maxp(chosen_desc);
123 _ep->
desc = chosen_desc;
140 switch (usb_endpoint_type(_ep->
desc)) {
150 ERROR(cdev,
"ep0 bMaxBurst must be 0\n");
178 DBG(config->
cdev,
"adding '%s'/%p to config '%s'/%p\n",
180 config->
label, config);
185 function->config =
config;
190 value =
function->bind(config,
function);
193 function->config =
NULL;
212 DBG(config->
cdev,
"adding '%s'/%p --> %d\n",
246 status = usb_gadget_disconnect(cdev->
gadget);
250 spin_unlock_irqrestore(&cdev->
lock, flags);
278 status = usb_gadget_connect(cdev->
gadget);
281 spin_unlock_irqrestore(&cdev->
lock, flags);
388 u8 type = w_value >> 8;
392 speed = gadget->
speed;
393 else if (gadget_is_dualspeed(gadget)) {
423 return config_buf(c, speed, cdev->
req->buf, type);
437 if (gadget_is_dualspeed(gadget)) {
514 if (cdev->
gadget->ops->get_config_params)
515 cdev->
gadget->ops->get_config_params(&dcd_config_params);
518 dcd_config_params.bU2DevExitLat =
550 DBG(cdev,
"reset config\n");
556 bitmap_zero(f->endpoints, 32);
567 unsigned power = gadget_is_otg(gadget) ? 8 : 100;
592 INFO(cdev,
"%s config #%d: %s\n",
594 number, c ? c->
label :
"unconfigured");
615 switch (gadget->
speed) {
639 result = f->
set_alt(f, tmp, 0);
641 DBG(cdev,
"interface %d (%s/%p) alt 0 --> %d\n",
642 tmp, f->
name, f, result);
650 "%s: interface %d (%s) requested delayed status\n",
651 __func__, tmp, f->
name);
653 DBG(cdev,
"delayed_status count %d\n",
661 usb_gadget_vbus_draw(gadget, power);
689 DBG(cdev,
"adding config #%u '%s'/%p\n",
691 config->
label, config);
711 status = bind(config);
713 while (!list_empty(&config->
functions)) {
720 DBG(cdev,
"unbind function '%s'/%p\n",
731 DBG(cdev,
"cfg %d/%p speeds:%s%s%s\n",
736 ? (gadget_is_dualspeed(cdev->
gadget)
746 DBG(cdev,
" interface %d = %s/%p\n",
758 DBG(cdev,
"added config '%s'/%u --> %d\n", config->
label,
767 while (!list_empty(&config->
functions)) {
774 DBG(cdev,
"unbind function '%s'/%p\n", f->
name, f);
781 DBG(cdev,
"unbind config '%s'/%p\n", config->
label, config);
803 if (cdev->
config == config)
806 spin_unlock_irqrestore(&cdev->
lock, flags);
808 remove_config(cdev, config);
828 for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) {
829 if (*tmp == language)
838 static int lookup_string(
860 void *buf,
u16 language,
int id)
882 collect_langs(sp, s->
wData);
887 collect_langs(sp, s->
wData);
892 collect_langs(sp, s->
wData);
896 for (len = 0; len <= 126 && s->
wData[len]; len++)
910 len = lookup_string(composite->
strings, buf, language,
id);
916 len = lookup_string(c->
strings, buf, language,
id);
923 len = lookup_string(f->
strings, buf, language,
id);
978 for (; str->
s; ++
str) {
1012 if (
unlikely(n > 254 || (
unsigned)next + n > 254))
1025 "setup complete --> %d, %d/%d\n",
1044 u8 intf = w_index & 0xFF;
1055 req->
complete = composite_setup_complete;
1057 gadget->
ep0->driver_data =
cdev;
1065 switch (w_value >> 8) {
1068 cdev->
desc.bNumConfigurations =
1070 cdev->
desc.bMaxPacketSize0 =
1071 cdev->
gadget->ep0->maxpacket;
1072 if (gadget_is_superspeed(gadget)) {
1075 cdev->
desc.bMaxPacketSize0 = 9;
1081 value =
min(w_length, (
u16)
sizeof cdev->
desc);
1085 if (!gadget_is_dualspeed(gadget) ||
1089 value =
min_t(
int, w_length,
1093 if (!gadget_is_dualspeed(gadget) ||
1098 value = config_desc(cdev, w_value);
1100 value =
min(w_length, (
u16) value);
1103 value = get_string(cdev, req->
buf,
1104 w_index, w_value & 0xff);
1106 value =
min(w_length, (
u16) value);
1109 if (gadget_is_superspeed(gadget)) {
1110 value = bos_desc(cdev);
1111 value =
min(w_length, (
u16) value);
1121 if (gadget_is_otg(gadget)) {
1123 DBG(cdev,
"HNP available\n");
1125 DBG(cdev,
"HNP on another port\n");
1127 VDBG(cdev,
"HNP inactive\n");
1129 spin_lock(&cdev->
lock);
1130 value = set_config(cdev, ctrl, w_value);
1131 spin_unlock(&cdev->
lock);
1137 *(
u8 *)req->
buf = cdev->
config->bConfigurationValue;
1139 *(
u8 *)req->
buf = 0;
1140 value =
min(w_length, (
u16) 1);
1149 if (!cdev->
config || intf >= MAX_CONFIG_INTERFACES)
1156 value = f->
set_alt(f, w_index, w_value);
1159 "%s: interface %d (%s) requested delayed status\n",
1160 __func__, intf, f->
name);
1162 DBG(cdev,
"delayed_status count %d\n",
1169 if (!cdev->
config || intf >= MAX_CONFIG_INTERFACES)
1179 value =
min(w_length, (
u16) 1);
1190 if (!gadget_is_superspeed(gadget))
1195 put_unaligned_le16(0, req->
buf);
1196 if (!cdev->
config || intf >= MAX_CONFIG_INTERFACES)
1204 put_unaligned_le16(status & 0x0000ffff, req->
buf);
1213 if (!gadget_is_superspeed(gadget))
1219 if (!cdev->
config || intf >= MAX_CONFIG_INTERFACES)
1229 "func_suspend() returned error %d\n",
1239 "non-core control req%02x.%02x v%04x i%04x l%d\n",
1241 w_value, w_index, w_length);
1253 if (!cdev->
config || intf >= MAX_CONFIG_INTERFACES)
1259 endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
1270 value = f->
setup(f, ctrl);
1276 value = c->
setup(c, ctrl);
1288 DBG(cdev,
"ep_queue --> %d\n", value);
1290 composite_setup_complete(gadget->
ep0, req);
1294 "%s: Delayed status not supported for w_length != 0",
1303 static void composite_disconnect(
struct usb_gadget *gadget)
1306 unsigned long flags;
1314 if (cdev->
driver->disconnect)
1315 cdev->
driver->disconnect(cdev);
1316 spin_unlock_irqrestore(&cdev->
lock, flags);
1325 struct usb_gadget *gadget = dev_to_usb_gadget(dev);
1345 while (!list_empty(&cdev->
configs)) {
1349 remove_config(cdev, c);
1351 if (cdev->
driver->unbind)
1352 cdev->
driver->unbind(cdev);
1356 usb_ep_free_request(gadget->
ep0, cdev->
req);
1361 set_gadget_data(gadget,
NULL);
1378 idVendor =
new->idVendor;
1379 idProduct =
new->idProduct;
1380 bcdDevice =
new->bcdDevice;
1381 iSerialNumber =
new->iSerialNumber;
1382 iManufacturer =
new->iManufacturer;
1383 iProduct =
new->iProduct;
1389 new->idProduct = idProduct;
1391 new->bcdDevice = bcdDevice;
1393 new->bcdDevice =
cpu_to_le16(get_default_bcdDevice());
1395 new->iSerialNumber = iSerialNumber;
1397 new->iManufacturer = iManufacturer;
1399 new->iProduct = iProduct;
1407 static int composite_bind(
struct usb_gadget *gadget,
1420 set_gadget_data(gadget, cdev);
1421 INIT_LIST_HEAD(&cdev->
configs);
1428 if (!cdev->
req->buf)
1430 cdev->
req->complete = composite_setup_complete;
1431 gadget->
ep0->driver_data =
cdev;
1433 cdev->
driver = composite;
1441 usb_gadget_set_selfpowered(gadget);
1453 status = composite->
bind(cdev);
1457 update_unchanged_dev_desc(&cdev->
desc, composite->
dev);
1461 WARNING(cdev,
"userspace failed to provide iSerialNumber\n");
1468 INFO(cdev,
"%s ready\n", composite->
name);
1472 composite_unbind(gadget);
1487 DBG(cdev,
"suspend\n");
1494 if (cdev->
driver->suspend)
1495 cdev->
driver->suspend(cdev);
1499 usb_gadget_vbus_draw(gadget, 2);
1512 DBG(cdev,
"resume\n");
1513 if (cdev->
driver->resume)
1514 cdev->
driver->resume(cdev);
1521 maxpower = cdev->
config->bMaxPower;
1523 usb_gadget_vbus_draw(gadget, maxpower ?
1524 (2 * maxpower) : CONFIG_USB_GADGET_VBUS_DRAW);
1533 .bind = composite_bind,
1534 .unbind = composite_unbind,
1536 .setup = composite_setup,
1537 .disconnect = composite_disconnect,
1539 .suspend = composite_suspend,
1540 .resume = composite_resume,
1570 if (!driver || !driver->
dev || !driver->
bind)
1574 driver->
name =
"composite";
1614 unsigned long flags;
1616 DBG(cdev,
"%s\n", __func__);
1620 WARN(cdev,
"%s: Unexpected call\n", __func__);
1623 DBG(cdev,
"%s: Completing delayed status\n", __func__);
1627 DBG(cdev,
"ep_queue --> %d\n", value);
1629 composite_setup_complete(cdev->
gadget->ep0, req);
1633 spin_unlock_irqrestore(&cdev->
lock, flags);
1637 static char *composite_default_mfr(
struct usb_gadget *gadget)
1642 len =
snprintf(
NULL, 0,
"%s %s with %s", init_utsname()->sysname,
1648 snprintf(mfr, len,
"%s %s with %s", init_utsname()->sysname,