17010f5b9SLukasz Majewski /* 27010f5b9SLukasz Majewski * composite.c - infrastructure for Composite USB Gadgets 37010f5b9SLukasz Majewski * 47010f5b9SLukasz Majewski * Copyright (C) 2006-2008 David Brownell 5a187559eSBin Meng * U-Boot porting: Lukasz Majewski <l.majewski@samsung.com> 67010f5b9SLukasz Majewski * 71a459660SWolfgang Denk * SPDX-License-Identifier: GPL-2.0+ 87010f5b9SLukasz Majewski */ 97010f5b9SLukasz Majewski #undef DEBUG 107010f5b9SLukasz Majewski 117010f5b9SLukasz Majewski #include <linux/bitops.h> 127010f5b9SLukasz Majewski #include <linux/usb/composite.h> 137010f5b9SLukasz Majewski 147010f5b9SLukasz Majewski #define USB_BUFSIZ 4096 157010f5b9SLukasz Majewski 16*bafc57b2SSimon Goldschmidt /* Helper type for accessing packed u16 pointers */ 17*bafc57b2SSimon Goldschmidt typedef struct { __le16 val; } __packed __le16_packed; 18*bafc57b2SSimon Goldschmidt 197010f5b9SLukasz Majewski static struct usb_composite_driver *composite; 207010f5b9SLukasz Majewski 21*bafc57b2SSimon Goldschmidt static inline void le16_add_cpu_packed(__le16_packed *var, u16 val) 22*bafc57b2SSimon Goldschmidt { 23*bafc57b2SSimon Goldschmidt var->val = cpu_to_le16(le16_to_cpu(var->val) + val); 24*bafc57b2SSimon Goldschmidt } 25*bafc57b2SSimon Goldschmidt 267010f5b9SLukasz Majewski /** 277010f5b9SLukasz Majewski * usb_add_function() - add a function to a configuration 287010f5b9SLukasz Majewski * @config: the configuration 297010f5b9SLukasz Majewski * @function: the function being added 307010f5b9SLukasz Majewski * Context: single threaded during gadget setup 317010f5b9SLukasz Majewski * 327010f5b9SLukasz Majewski * After initialization, each configuration must have one or more 337010f5b9SLukasz Majewski * functions added to it. Adding a function involves calling its @bind() 347010f5b9SLukasz Majewski * method to allocate resources such as interface and string identifiers 357010f5b9SLukasz Majewski * and endpoints. 367010f5b9SLukasz Majewski * 377010f5b9SLukasz Majewski * This function returns the value of the function's bind(), which is 387010f5b9SLukasz Majewski * zero for success else a negative errno value. 397010f5b9SLukasz Majewski */ 407010f5b9SLukasz Majewski int usb_add_function(struct usb_configuration *config, 417010f5b9SLukasz Majewski struct usb_function *function) 427010f5b9SLukasz Majewski { 437010f5b9SLukasz Majewski int value = -EINVAL; 447010f5b9SLukasz Majewski 457010f5b9SLukasz Majewski debug("adding '%s'/%p to config '%s'/%p\n", 467010f5b9SLukasz Majewski function->name, function, 477010f5b9SLukasz Majewski config->label, config); 487010f5b9SLukasz Majewski 497010f5b9SLukasz Majewski if (!function->set_alt || !function->disable) 507010f5b9SLukasz Majewski goto done; 517010f5b9SLukasz Majewski 527010f5b9SLukasz Majewski function->config = config; 537010f5b9SLukasz Majewski list_add_tail(&function->list, &config->functions); 547010f5b9SLukasz Majewski 557010f5b9SLukasz Majewski if (function->bind) { 567010f5b9SLukasz Majewski value = function->bind(config, function); 577010f5b9SLukasz Majewski if (value < 0) { 587010f5b9SLukasz Majewski list_del(&function->list); 597010f5b9SLukasz Majewski function->config = NULL; 607010f5b9SLukasz Majewski } 617010f5b9SLukasz Majewski } else 627010f5b9SLukasz Majewski value = 0; 637010f5b9SLukasz Majewski 647010f5b9SLukasz Majewski if (!config->fullspeed && function->descriptors) 657010f5b9SLukasz Majewski config->fullspeed = 1; 667010f5b9SLukasz Majewski if (!config->highspeed && function->hs_descriptors) 677010f5b9SLukasz Majewski config->highspeed = 1; 6826dd3474SWilliam Wu if (!config->superspeed && function->ss_descriptors) 6926dd3474SWilliam Wu config->superspeed = 1; 707010f5b9SLukasz Majewski 717010f5b9SLukasz Majewski done: 727010f5b9SLukasz Majewski if (value) 737010f5b9SLukasz Majewski debug("adding '%s'/%p --> %d\n", 747010f5b9SLukasz Majewski function->name, function, value); 757010f5b9SLukasz Majewski return value; 767010f5b9SLukasz Majewski } 777010f5b9SLukasz Majewski 787010f5b9SLukasz Majewski /** 797010f5b9SLukasz Majewski * usb_function_deactivate - prevent function and gadget enumeration 807010f5b9SLukasz Majewski * @function: the function that isn't yet ready to respond 817010f5b9SLukasz Majewski * 827010f5b9SLukasz Majewski * Blocks response of the gadget driver to host enumeration by 837010f5b9SLukasz Majewski * preventing the data line pullup from being activated. This is 847010f5b9SLukasz Majewski * normally called during @bind() processing to change from the 857010f5b9SLukasz Majewski * initial "ready to respond" state, or when a required resource 867010f5b9SLukasz Majewski * becomes available. 877010f5b9SLukasz Majewski * 887010f5b9SLukasz Majewski * For example, drivers that serve as a passthrough to a userspace 897010f5b9SLukasz Majewski * daemon can block enumeration unless that daemon (such as an OBEX, 907010f5b9SLukasz Majewski * MTP, or print server) is ready to handle host requests. 917010f5b9SLukasz Majewski * 927010f5b9SLukasz Majewski * Not all systems support software control of their USB peripheral 937010f5b9SLukasz Majewski * data pullups. 947010f5b9SLukasz Majewski * 957010f5b9SLukasz Majewski * Returns zero on success, else negative errno. 967010f5b9SLukasz Majewski */ 977010f5b9SLukasz Majewski int usb_function_deactivate(struct usb_function *function) 987010f5b9SLukasz Majewski { 997010f5b9SLukasz Majewski struct usb_composite_dev *cdev = function->config->cdev; 1007010f5b9SLukasz Majewski int status = 0; 1017010f5b9SLukasz Majewski 1027010f5b9SLukasz Majewski if (cdev->deactivations == 0) 1037010f5b9SLukasz Majewski status = usb_gadget_disconnect(cdev->gadget); 1047010f5b9SLukasz Majewski if (status == 0) 1057010f5b9SLukasz Majewski cdev->deactivations++; 1067010f5b9SLukasz Majewski 1077010f5b9SLukasz Majewski return status; 1087010f5b9SLukasz Majewski } 1097010f5b9SLukasz Majewski 1107010f5b9SLukasz Majewski /** 1117010f5b9SLukasz Majewski * usb_function_activate - allow function and gadget enumeration 1127010f5b9SLukasz Majewski * @function: function on which usb_function_activate() was called 1137010f5b9SLukasz Majewski * 1147010f5b9SLukasz Majewski * Reverses effect of usb_function_deactivate(). If no more functions 1157010f5b9SLukasz Majewski * are delaying their activation, the gadget driver will respond to 1167010f5b9SLukasz Majewski * host enumeration procedures. 1177010f5b9SLukasz Majewski * 1187010f5b9SLukasz Majewski * Returns zero on success, else negative errno. 1197010f5b9SLukasz Majewski */ 1207010f5b9SLukasz Majewski int usb_function_activate(struct usb_function *function) 1217010f5b9SLukasz Majewski { 1227010f5b9SLukasz Majewski struct usb_composite_dev *cdev = function->config->cdev; 1237010f5b9SLukasz Majewski int status = 0; 1247010f5b9SLukasz Majewski 1257010f5b9SLukasz Majewski if (cdev->deactivations == 0) 1267010f5b9SLukasz Majewski status = -EINVAL; 1277010f5b9SLukasz Majewski else { 1287010f5b9SLukasz Majewski cdev->deactivations--; 1297010f5b9SLukasz Majewski if (cdev->deactivations == 0) 1307010f5b9SLukasz Majewski status = usb_gadget_connect(cdev->gadget); 1317010f5b9SLukasz Majewski } 1327010f5b9SLukasz Majewski 1337010f5b9SLukasz Majewski return status; 1347010f5b9SLukasz Majewski } 1357010f5b9SLukasz Majewski 1367010f5b9SLukasz Majewski /** 1377010f5b9SLukasz Majewski * usb_interface_id() - allocate an unused interface ID 1387010f5b9SLukasz Majewski * @config: configuration associated with the interface 1397010f5b9SLukasz Majewski * @function: function handling the interface 1407010f5b9SLukasz Majewski * Context: single threaded during gadget setup 1417010f5b9SLukasz Majewski * 1427010f5b9SLukasz Majewski * usb_interface_id() is called from usb_function.bind() callbacks to 1437010f5b9SLukasz Majewski * allocate new interface IDs. The function driver will then store that 1447010f5b9SLukasz Majewski * ID in interface, association, CDC union, and other descriptors. It 1457010f5b9SLukasz Majewski * will also handle any control requests targetted at that interface, 1467010f5b9SLukasz Majewski * particularly changing its altsetting via set_alt(). There may 1477010f5b9SLukasz Majewski * also be class-specific or vendor-specific requests to handle. 1487010f5b9SLukasz Majewski * 1497010f5b9SLukasz Majewski * All interface identifier should be allocated using this routine, to 1507010f5b9SLukasz Majewski * ensure that for example different functions don't wrongly assign 1517010f5b9SLukasz Majewski * different meanings to the same identifier. Note that since interface 1527010f5b9SLukasz Majewski * identifers are configuration-specific, functions used in more than 1537010f5b9SLukasz Majewski * one configuration (or more than once in a given configuration) need 1547010f5b9SLukasz Majewski * multiple versions of the relevant descriptors. 1557010f5b9SLukasz Majewski * 1567010f5b9SLukasz Majewski * Returns the interface ID which was allocated; or -ENODEV if no 1577010f5b9SLukasz Majewski * more interface IDs can be allocated. 1587010f5b9SLukasz Majewski */ 1597010f5b9SLukasz Majewski int usb_interface_id(struct usb_configuration *config, 1607010f5b9SLukasz Majewski struct usb_function *function) 1617010f5b9SLukasz Majewski { 1627010f5b9SLukasz Majewski unsigned char id = config->next_interface_id; 1637010f5b9SLukasz Majewski 1647010f5b9SLukasz Majewski if (id < MAX_CONFIG_INTERFACES) { 1657010f5b9SLukasz Majewski config->interface[id] = function; 1667010f5b9SLukasz Majewski config->next_interface_id = id + 1; 1677010f5b9SLukasz Majewski return id; 1687010f5b9SLukasz Majewski } 1697010f5b9SLukasz Majewski return -ENODEV; 1707010f5b9SLukasz Majewski } 1717010f5b9SLukasz Majewski 1727010f5b9SLukasz Majewski static int config_buf(struct usb_configuration *config, 1737010f5b9SLukasz Majewski enum usb_device_speed speed, void *buf, u8 type) 1747010f5b9SLukasz Majewski { 1757010f5b9SLukasz Majewski int len = USB_BUFSIZ - USB_DT_CONFIG_SIZE; 1767010f5b9SLukasz Majewski void *next = buf + USB_DT_CONFIG_SIZE; 1777010f5b9SLukasz Majewski struct usb_descriptor_header **descriptors; 178dd2dbc26SHeinrich Schuchardt struct usb_config_descriptor *c; 1797010f5b9SLukasz Majewski int status; 1807010f5b9SLukasz Majewski struct usb_function *f; 1817010f5b9SLukasz Majewski 1827010f5b9SLukasz Majewski /* write the config descriptor */ 1837010f5b9SLukasz Majewski c = buf; 1847010f5b9SLukasz Majewski c->bLength = USB_DT_CONFIG_SIZE; 1857010f5b9SLukasz Majewski c->bDescriptorType = type; 1867010f5b9SLukasz Majewski 1877010f5b9SLukasz Majewski c->bNumInterfaces = config->next_interface_id; 1887010f5b9SLukasz Majewski c->bConfigurationValue = config->bConfigurationValue; 1897010f5b9SLukasz Majewski c->iConfiguration = config->iConfiguration; 1907010f5b9SLukasz Majewski c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes; 1917010f5b9SLukasz Majewski c->bMaxPower = config->bMaxPower ? : (CONFIG_USB_GADGET_VBUS_DRAW / 2); 1927010f5b9SLukasz Majewski 1937010f5b9SLukasz Majewski /* There may be e.g. OTG descriptors */ 1947010f5b9SLukasz Majewski if (config->descriptors) { 1957010f5b9SLukasz Majewski status = usb_descriptor_fillbuf(next, len, 1967010f5b9SLukasz Majewski config->descriptors); 1977010f5b9SLukasz Majewski if (status < 0) 1987010f5b9SLukasz Majewski return status; 1997010f5b9SLukasz Majewski len -= status; 2007010f5b9SLukasz Majewski next += status; 2017010f5b9SLukasz Majewski } 2027010f5b9SLukasz Majewski 2037010f5b9SLukasz Majewski /* add each function's descriptors */ 2047010f5b9SLukasz Majewski list_for_each_entry(f, &config->functions, list) { 20526dd3474SWilliam Wu switch (speed) { 20626dd3474SWilliam Wu case USB_SPEED_SUPER: 20726dd3474SWilliam Wu descriptors = f->ss_descriptors; 20826dd3474SWilliam Wu break; 20926dd3474SWilliam Wu case USB_SPEED_HIGH: 2107010f5b9SLukasz Majewski descriptors = f->hs_descriptors; 21126dd3474SWilliam Wu break; 21226dd3474SWilliam Wu default: 2137010f5b9SLukasz Majewski descriptors = f->descriptors; 21426dd3474SWilliam Wu } 21526dd3474SWilliam Wu 2167010f5b9SLukasz Majewski if (!descriptors) 2177010f5b9SLukasz Majewski continue; 2187010f5b9SLukasz Majewski status = usb_descriptor_fillbuf(next, len, 2197010f5b9SLukasz Majewski (const struct usb_descriptor_header **) descriptors); 2207010f5b9SLukasz Majewski if (status < 0) 2217010f5b9SLukasz Majewski return status; 2227010f5b9SLukasz Majewski len -= status; 2237010f5b9SLukasz Majewski next += status; 2247010f5b9SLukasz Majewski } 2257010f5b9SLukasz Majewski 2267010f5b9SLukasz Majewski len = next - buf; 2277010f5b9SLukasz Majewski c->wTotalLength = cpu_to_le16(len); 2287010f5b9SLukasz Majewski return len; 2297010f5b9SLukasz Majewski } 2307010f5b9SLukasz Majewski 2317010f5b9SLukasz Majewski static int config_desc(struct usb_composite_dev *cdev, unsigned w_value) 2327010f5b9SLukasz Majewski { 2337010f5b9SLukasz Majewski enum usb_device_speed speed = USB_SPEED_UNKNOWN; 2347010f5b9SLukasz Majewski struct usb_gadget *gadget = cdev->gadget; 2357010f5b9SLukasz Majewski u8 type = w_value >> 8; 2367010f5b9SLukasz Majewski int hs = 0; 2377010f5b9SLukasz Majewski struct usb_configuration *c; 2387010f5b9SLukasz Majewski 23926dd3474SWilliam Wu if (gadget->speed == USB_SPEED_SUPER) 24026dd3474SWilliam Wu speed = gadget->speed; 24126dd3474SWilliam Wu else if (gadget_is_dualspeed(gadget)) { 2427010f5b9SLukasz Majewski if (gadget->speed == USB_SPEED_HIGH) 2437010f5b9SLukasz Majewski hs = 1; 2447010f5b9SLukasz Majewski if (type == USB_DT_OTHER_SPEED_CONFIG) 2457010f5b9SLukasz Majewski hs = !hs; 2467010f5b9SLukasz Majewski if (hs) 2477010f5b9SLukasz Majewski speed = USB_SPEED_HIGH; 2487010f5b9SLukasz Majewski } 2497010f5b9SLukasz Majewski 2507010f5b9SLukasz Majewski w_value &= 0xff; 2517010f5b9SLukasz Majewski list_for_each_entry(c, &cdev->configs, list) { 25226dd3474SWilliam Wu switch (speed) { 25326dd3474SWilliam Wu case USB_SPEED_SUPER: 25426dd3474SWilliam Wu if (!c->superspeed) 25526dd3474SWilliam Wu continue; 25626dd3474SWilliam Wu break; 25726dd3474SWilliam Wu case USB_SPEED_HIGH: 2587010f5b9SLukasz Majewski if (!c->highspeed) 2597010f5b9SLukasz Majewski continue; 26026dd3474SWilliam Wu break; 26126dd3474SWilliam Wu default: 2627010f5b9SLukasz Majewski if (!c->fullspeed) 2637010f5b9SLukasz Majewski continue; 2647010f5b9SLukasz Majewski } 26526dd3474SWilliam Wu 2667010f5b9SLukasz Majewski if (w_value == 0) 2677010f5b9SLukasz Majewski return config_buf(c, speed, cdev->req->buf, type); 2687010f5b9SLukasz Majewski w_value--; 2697010f5b9SLukasz Majewski } 2707010f5b9SLukasz Majewski return -EINVAL; 2717010f5b9SLukasz Majewski } 2727010f5b9SLukasz Majewski 2737010f5b9SLukasz Majewski static int count_configs(struct usb_composite_dev *cdev, unsigned type) 2747010f5b9SLukasz Majewski { 2757010f5b9SLukasz Majewski struct usb_gadget *gadget = cdev->gadget; 2767010f5b9SLukasz Majewski unsigned count = 0; 2777010f5b9SLukasz Majewski int hs = 0; 2787010f5b9SLukasz Majewski struct usb_configuration *c; 2797010f5b9SLukasz Majewski 2807010f5b9SLukasz Majewski if (gadget_is_dualspeed(gadget)) { 2817010f5b9SLukasz Majewski if (gadget->speed == USB_SPEED_HIGH) 2827010f5b9SLukasz Majewski hs = 1; 2837010f5b9SLukasz Majewski if (type == USB_DT_DEVICE_QUALIFIER) 2847010f5b9SLukasz Majewski hs = !hs; 2857010f5b9SLukasz Majewski } 2867010f5b9SLukasz Majewski list_for_each_entry(c, &cdev->configs, list) { 2877010f5b9SLukasz Majewski /* ignore configs that won't work at this speed */ 2887010f5b9SLukasz Majewski if (hs) { 2897010f5b9SLukasz Majewski if (!c->highspeed) 2907010f5b9SLukasz Majewski continue; 2917010f5b9SLukasz Majewski } else { 2927010f5b9SLukasz Majewski if (!c->fullspeed) 2937010f5b9SLukasz Majewski continue; 2947010f5b9SLukasz Majewski } 2957010f5b9SLukasz Majewski count++; 2967010f5b9SLukasz Majewski } 2977010f5b9SLukasz Majewski return count; 2987010f5b9SLukasz Majewski } 2997010f5b9SLukasz Majewski 3007010f5b9SLukasz Majewski static void device_qual(struct usb_composite_dev *cdev) 3017010f5b9SLukasz Majewski { 3027010f5b9SLukasz Majewski struct usb_qualifier_descriptor *qual = cdev->req->buf; 3037010f5b9SLukasz Majewski 3047010f5b9SLukasz Majewski qual->bLength = sizeof(*qual); 3057010f5b9SLukasz Majewski qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER; 3067010f5b9SLukasz Majewski /* POLICY: same bcdUSB and device type info at both speeds */ 3077010f5b9SLukasz Majewski qual->bcdUSB = cdev->desc.bcdUSB; 3087010f5b9SLukasz Majewski qual->bDeviceClass = cdev->desc.bDeviceClass; 3097010f5b9SLukasz Majewski qual->bDeviceSubClass = cdev->desc.bDeviceSubClass; 3107010f5b9SLukasz Majewski qual->bDeviceProtocol = cdev->desc.bDeviceProtocol; 3117010f5b9SLukasz Majewski /* ASSUME same EP0 fifo size at both speeds */ 31204afd5b5SKishon Vijay Abraham I qual->bMaxPacketSize0 = cdev->gadget->ep0->maxpacket; 3137010f5b9SLukasz Majewski qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER); 3147010f5b9SLukasz Majewski qual->bRESERVED = 0; 3157010f5b9SLukasz Majewski } 3167010f5b9SLukasz Majewski 3177010f5b9SLukasz Majewski static void reset_config(struct usb_composite_dev *cdev) 3187010f5b9SLukasz Majewski { 3197010f5b9SLukasz Majewski struct usb_function *f; 3207010f5b9SLukasz Majewski 3217010f5b9SLukasz Majewski debug("%s:\n", __func__); 3227010f5b9SLukasz Majewski 3237010f5b9SLukasz Majewski list_for_each_entry(f, &cdev->config->functions, list) { 3247010f5b9SLukasz Majewski if (f->disable) 3257010f5b9SLukasz Majewski f->disable(f); 3267010f5b9SLukasz Majewski 3277010f5b9SLukasz Majewski bitmap_zero(f->endpoints, 32); 3287010f5b9SLukasz Majewski } 3297010f5b9SLukasz Majewski cdev->config = NULL; 3307010f5b9SLukasz Majewski } 3317010f5b9SLukasz Majewski 3327010f5b9SLukasz Majewski static int set_config(struct usb_composite_dev *cdev, 3337010f5b9SLukasz Majewski const struct usb_ctrlrequest *ctrl, unsigned number) 3347010f5b9SLukasz Majewski { 3357010f5b9SLukasz Majewski struct usb_gadget *gadget = cdev->gadget; 3367010f5b9SLukasz Majewski unsigned power = gadget_is_otg(gadget) ? 8 : 100; 3377010f5b9SLukasz Majewski struct usb_descriptor_header **descriptors; 3387010f5b9SLukasz Majewski int result = -EINVAL; 3397010f5b9SLukasz Majewski struct usb_endpoint_descriptor *ep; 3407010f5b9SLukasz Majewski struct usb_configuration *c = NULL; 3417010f5b9SLukasz Majewski int addr; 3427010f5b9SLukasz Majewski int tmp; 3437010f5b9SLukasz Majewski struct usb_function *f; 3447010f5b9SLukasz Majewski 3457010f5b9SLukasz Majewski if (cdev->config) 3467010f5b9SLukasz Majewski reset_config(cdev); 3477010f5b9SLukasz Majewski 3487010f5b9SLukasz Majewski if (number) { 3497010f5b9SLukasz Majewski list_for_each_entry(c, &cdev->configs, list) { 3507010f5b9SLukasz Majewski if (c->bConfigurationValue == number) { 3517010f5b9SLukasz Majewski result = 0; 3527010f5b9SLukasz Majewski break; 3537010f5b9SLukasz Majewski } 3547010f5b9SLukasz Majewski } 3557010f5b9SLukasz Majewski if (result < 0) 3567010f5b9SLukasz Majewski goto done; 3577010f5b9SLukasz Majewski } else 3587010f5b9SLukasz Majewski result = 0; 3597010f5b9SLukasz Majewski 3607010f5b9SLukasz Majewski debug("%s: %s speed config #%d: %s\n", __func__, 3617010f5b9SLukasz Majewski ({ char *speed; 3627010f5b9SLukasz Majewski switch (gadget->speed) { 3637010f5b9SLukasz Majewski case USB_SPEED_LOW: 3647010f5b9SLukasz Majewski speed = "low"; 3657010f5b9SLukasz Majewski break; 3667010f5b9SLukasz Majewski case USB_SPEED_FULL: 3677010f5b9SLukasz Majewski speed = "full"; 3687010f5b9SLukasz Majewski break; 3697010f5b9SLukasz Majewski case USB_SPEED_HIGH: 3707010f5b9SLukasz Majewski speed = "high"; 3717010f5b9SLukasz Majewski break; 37226dd3474SWilliam Wu case USB_SPEED_SUPER: 37326dd3474SWilliam Wu speed = "super"; 37426dd3474SWilliam Wu break; 3757010f5b9SLukasz Majewski default: 3767010f5b9SLukasz Majewski speed = "?"; 3777010f5b9SLukasz Majewski break; 3787010f5b9SLukasz Majewski }; 3797010f5b9SLukasz Majewski speed; 3807010f5b9SLukasz Majewski }), number, c ? c->label : "unconfigured"); 3817010f5b9SLukasz Majewski 3827010f5b9SLukasz Majewski if (!c) 3837010f5b9SLukasz Majewski goto done; 3847010f5b9SLukasz Majewski 3857010f5b9SLukasz Majewski cdev->config = c; 3867010f5b9SLukasz Majewski 3877010f5b9SLukasz Majewski /* Initialize all interfaces by setting them to altsetting zero. */ 3887010f5b9SLukasz Majewski for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) { 3897010f5b9SLukasz Majewski f = c->interface[tmp]; 3907010f5b9SLukasz Majewski if (!f) 3917010f5b9SLukasz Majewski break; 3927010f5b9SLukasz Majewski 3937010f5b9SLukasz Majewski /* 3947010f5b9SLukasz Majewski * Record which endpoints are used by the function. This is used 3957010f5b9SLukasz Majewski * to dispatch control requests targeted at that endpoint to the 3967010f5b9SLukasz Majewski * function's setup callback instead of the current 3977010f5b9SLukasz Majewski * configuration's setup callback. 3987010f5b9SLukasz Majewski */ 39926dd3474SWilliam Wu switch (gadget->speed) { 40026dd3474SWilliam Wu case USB_SPEED_SUPER: 40126dd3474SWilliam Wu descriptors = f->ss_descriptors; 40226dd3474SWilliam Wu break; 40326dd3474SWilliam Wu case USB_SPEED_HIGH: 4047010f5b9SLukasz Majewski descriptors = f->hs_descriptors; 40526dd3474SWilliam Wu break; 40626dd3474SWilliam Wu default: 4077010f5b9SLukasz Majewski descriptors = f->descriptors; 40826dd3474SWilliam Wu } 4097010f5b9SLukasz Majewski 4107010f5b9SLukasz Majewski for (; *descriptors; ++descriptors) { 4117010f5b9SLukasz Majewski if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT) 4127010f5b9SLukasz Majewski continue; 4137010f5b9SLukasz Majewski 4147010f5b9SLukasz Majewski ep = (struct usb_endpoint_descriptor *)*descriptors; 4157010f5b9SLukasz Majewski addr = ((ep->bEndpointAddress & 0x80) >> 3) 4167010f5b9SLukasz Majewski | (ep->bEndpointAddress & 0x0f); 41743156a85SBryan O'Donoghue generic_set_bit(addr, f->endpoints); 4187010f5b9SLukasz Majewski } 4197010f5b9SLukasz Majewski 4207010f5b9SLukasz Majewski result = f->set_alt(f, tmp, 0); 4217010f5b9SLukasz Majewski if (result < 0) { 4227010f5b9SLukasz Majewski debug("interface %d (%s/%p) alt 0 --> %d\n", 4237010f5b9SLukasz Majewski tmp, f->name, f, result); 4247010f5b9SLukasz Majewski 4257010f5b9SLukasz Majewski reset_config(cdev); 4267010f5b9SLukasz Majewski goto done; 4277010f5b9SLukasz Majewski } 4287010f5b9SLukasz Majewski } 4297010f5b9SLukasz Majewski 4307010f5b9SLukasz Majewski /* when we return, be sure our power usage is valid */ 4317010f5b9SLukasz Majewski power = c->bMaxPower ? (2 * c->bMaxPower) : CONFIG_USB_GADGET_VBUS_DRAW; 4327010f5b9SLukasz Majewski done: 4337010f5b9SLukasz Majewski usb_gadget_vbus_draw(gadget, power); 4347010f5b9SLukasz Majewski return result; 4357010f5b9SLukasz Majewski } 4367010f5b9SLukasz Majewski 4377010f5b9SLukasz Majewski /** 4387010f5b9SLukasz Majewski * usb_add_config() - add a configuration to a device. 4397010f5b9SLukasz Majewski * @cdev: wraps the USB gadget 4407010f5b9SLukasz Majewski * @config: the configuration, with bConfigurationValue assigned 4417010f5b9SLukasz Majewski * Context: single threaded during gadget setup 4427010f5b9SLukasz Majewski * 4437010f5b9SLukasz Majewski * One of the main tasks of a composite driver's bind() routine is to 4447010f5b9SLukasz Majewski * add each of the configurations it supports, using this routine. 4457010f5b9SLukasz Majewski * 4467010f5b9SLukasz Majewski * This function returns the value of the configuration's bind(), which 4477010f5b9SLukasz Majewski * is zero for success else a negative errno value. Binding configurations 4487010f5b9SLukasz Majewski * assigns global resources including string IDs, and per-configuration 4497010f5b9SLukasz Majewski * resources such as interface IDs and endpoints. 4507010f5b9SLukasz Majewski */ 4517010f5b9SLukasz Majewski int usb_add_config(struct usb_composite_dev *cdev, 4527010f5b9SLukasz Majewski struct usb_configuration *config) 4537010f5b9SLukasz Majewski { 4547010f5b9SLukasz Majewski int status = -EINVAL; 4557010f5b9SLukasz Majewski struct usb_configuration *c; 4567010f5b9SLukasz Majewski struct usb_function *f; 4577010f5b9SLukasz Majewski unsigned int i; 4587010f5b9SLukasz Majewski 4597010f5b9SLukasz Majewski debug("%s: adding config #%u '%s'/%p\n", __func__, 4607010f5b9SLukasz Majewski config->bConfigurationValue, 4617010f5b9SLukasz Majewski config->label, config); 4627010f5b9SLukasz Majewski 4637010f5b9SLukasz Majewski if (!config->bConfigurationValue || !config->bind) 4647010f5b9SLukasz Majewski goto done; 4657010f5b9SLukasz Majewski 4667010f5b9SLukasz Majewski /* Prevent duplicate configuration identifiers */ 4677010f5b9SLukasz Majewski list_for_each_entry(c, &cdev->configs, list) { 4687010f5b9SLukasz Majewski if (c->bConfigurationValue == config->bConfigurationValue) { 4697010f5b9SLukasz Majewski status = -EBUSY; 4707010f5b9SLukasz Majewski goto done; 4717010f5b9SLukasz Majewski } 4727010f5b9SLukasz Majewski } 4737010f5b9SLukasz Majewski 4747010f5b9SLukasz Majewski config->cdev = cdev; 4757010f5b9SLukasz Majewski list_add_tail(&config->list, &cdev->configs); 4767010f5b9SLukasz Majewski 4777010f5b9SLukasz Majewski INIT_LIST_HEAD(&config->functions); 4787010f5b9SLukasz Majewski config->next_interface_id = 0; 4797010f5b9SLukasz Majewski 4807010f5b9SLukasz Majewski status = config->bind(config); 4817010f5b9SLukasz Majewski if (status < 0) { 4827010f5b9SLukasz Majewski list_del(&config->list); 4837010f5b9SLukasz Majewski config->cdev = NULL; 4847010f5b9SLukasz Majewski } else { 48526dd3474SWilliam Wu debug("cfg %d/%p speeds:%s%s%s\n", 4867010f5b9SLukasz Majewski config->bConfigurationValue, config, 48726dd3474SWilliam Wu config->superspeed ? " super" : "", 4887010f5b9SLukasz Majewski config->highspeed ? " high" : "", 48926dd3474SWilliam Wu config->fullspeed ? 49026dd3474SWilliam Wu (gadget_is_dualspeed(cdev->gadget) ? 49126dd3474SWilliam Wu " full" : " full/low") : ""); 4927010f5b9SLukasz Majewski 4937010f5b9SLukasz Majewski for (i = 0; i < MAX_CONFIG_INTERFACES; i++) { 4947010f5b9SLukasz Majewski f = config->interface[i]; 4957010f5b9SLukasz Majewski if (!f) 4967010f5b9SLukasz Majewski continue; 4977010f5b9SLukasz Majewski debug("%s: interface %d = %s/%p\n", 4987010f5b9SLukasz Majewski __func__, i, f->name, f); 4997010f5b9SLukasz Majewski } 5007010f5b9SLukasz Majewski } 5017010f5b9SLukasz Majewski 5027010f5b9SLukasz Majewski usb_ep_autoconfig_reset(cdev->gadget); 5037010f5b9SLukasz Majewski 5047010f5b9SLukasz Majewski done: 5057010f5b9SLukasz Majewski if (status) 5067010f5b9SLukasz Majewski debug("added config '%s'/%u --> %d\n", config->label, 5077010f5b9SLukasz Majewski config->bConfigurationValue, status); 5087010f5b9SLukasz Majewski return status; 5097010f5b9SLukasz Majewski } 5107010f5b9SLukasz Majewski 5117010f5b9SLukasz Majewski /* 5127010f5b9SLukasz Majewski * We support strings in multiple languages ... string descriptor zero 5137010f5b9SLukasz Majewski * says which languages are supported. The typical case will be that 5147010f5b9SLukasz Majewski * only one language (probably English) is used, with I18N handled on 5157010f5b9SLukasz Majewski * the host side. 5167010f5b9SLukasz Majewski */ 5177010f5b9SLukasz Majewski 518*bafc57b2SSimon Goldschmidt static void collect_langs(struct usb_gadget_strings **sp, void *buf) 5197010f5b9SLukasz Majewski { 5207010f5b9SLukasz Majewski const struct usb_gadget_strings *s; 5217010f5b9SLukasz Majewski u16 language; 522*bafc57b2SSimon Goldschmidt __le16_packed *tmp; 523*bafc57b2SSimon Goldschmidt __le16_packed *end = (buf + 252); 5247010f5b9SLukasz Majewski 5257010f5b9SLukasz Majewski while (*sp) { 5267010f5b9SLukasz Majewski s = *sp; 5277010f5b9SLukasz Majewski language = cpu_to_le16(s->language); 528*bafc57b2SSimon Goldschmidt for (tmp = buf; tmp->val && tmp < end; tmp++) { 529*bafc57b2SSimon Goldschmidt if (tmp->val == language) 5307010f5b9SLukasz Majewski goto repeat; 5317010f5b9SLukasz Majewski } 532*bafc57b2SSimon Goldschmidt tmp->val = language; 5337010f5b9SLukasz Majewski repeat: 5347010f5b9SLukasz Majewski sp++; 5357010f5b9SLukasz Majewski } 5367010f5b9SLukasz Majewski } 5377010f5b9SLukasz Majewski 5387010f5b9SLukasz Majewski static int lookup_string( 5397010f5b9SLukasz Majewski struct usb_gadget_strings **sp, 5407010f5b9SLukasz Majewski void *buf, 5417010f5b9SLukasz Majewski u16 language, 5427010f5b9SLukasz Majewski int id 5437010f5b9SLukasz Majewski ) 5447010f5b9SLukasz Majewski { 5457010f5b9SLukasz Majewski int value; 5467010f5b9SLukasz Majewski struct usb_gadget_strings *s; 5477010f5b9SLukasz Majewski 5487010f5b9SLukasz Majewski while (*sp) { 5497010f5b9SLukasz Majewski s = *sp++; 5507010f5b9SLukasz Majewski if (s->language != language) 5517010f5b9SLukasz Majewski continue; 5527010f5b9SLukasz Majewski value = usb_gadget_get_string(s, id, buf); 5537010f5b9SLukasz Majewski if (value > 0) 5547010f5b9SLukasz Majewski return value; 5557010f5b9SLukasz Majewski } 5567010f5b9SLukasz Majewski return -EINVAL; 5577010f5b9SLukasz Majewski } 5587010f5b9SLukasz Majewski 5597010f5b9SLukasz Majewski static int get_string(struct usb_composite_dev *cdev, 5607010f5b9SLukasz Majewski void *buf, u16 language, int id) 5617010f5b9SLukasz Majewski { 5627010f5b9SLukasz Majewski struct usb_string_descriptor *s = buf; 5637010f5b9SLukasz Majewski struct usb_gadget_strings **sp; 5647010f5b9SLukasz Majewski int len; 5657010f5b9SLukasz Majewski struct usb_configuration *c; 5667010f5b9SLukasz Majewski struct usb_function *f; 5677010f5b9SLukasz Majewski 5687010f5b9SLukasz Majewski /* 5697010f5b9SLukasz Majewski * Yes, not only is USB's I18N support probably more than most 5707010f5b9SLukasz Majewski * folk will ever care about ... also, it's all supported here. 5717010f5b9SLukasz Majewski * (Except for UTF8 support for Unicode's "Astral Planes".) 5727010f5b9SLukasz Majewski */ 5737010f5b9SLukasz Majewski 5747010f5b9SLukasz Majewski /* 0 == report all available language codes */ 5757010f5b9SLukasz Majewski if (id == 0) { 5767010f5b9SLukasz Majewski memset(s, 0, 256); 5777010f5b9SLukasz Majewski s->bDescriptorType = USB_DT_STRING; 5787010f5b9SLukasz Majewski 5797010f5b9SLukasz Majewski sp = composite->strings; 5807010f5b9SLukasz Majewski if (sp) 5817010f5b9SLukasz Majewski collect_langs(sp, s->wData); 5827010f5b9SLukasz Majewski 5837010f5b9SLukasz Majewski list_for_each_entry(c, &cdev->configs, list) { 5847010f5b9SLukasz Majewski sp = c->strings; 5857010f5b9SLukasz Majewski if (sp) 5867010f5b9SLukasz Majewski collect_langs(sp, s->wData); 5877010f5b9SLukasz Majewski 5887010f5b9SLukasz Majewski list_for_each_entry(f, &c->functions, list) { 5897010f5b9SLukasz Majewski sp = f->strings; 5907010f5b9SLukasz Majewski if (sp) 5917010f5b9SLukasz Majewski collect_langs(sp, s->wData); 5927010f5b9SLukasz Majewski } 5937010f5b9SLukasz Majewski } 5947010f5b9SLukasz Majewski 5957010f5b9SLukasz Majewski for (len = 0; len <= 126 && s->wData[len]; len++) 5967010f5b9SLukasz Majewski continue; 5977010f5b9SLukasz Majewski if (!len) 5987010f5b9SLukasz Majewski return -EINVAL; 5997010f5b9SLukasz Majewski 6007010f5b9SLukasz Majewski s->bLength = 2 * (len + 1); 6017010f5b9SLukasz Majewski return s->bLength; 6027010f5b9SLukasz Majewski } 6037010f5b9SLukasz Majewski 6047010f5b9SLukasz Majewski /* 6057010f5b9SLukasz Majewski * Otherwise, look up and return a specified string. String IDs 6067010f5b9SLukasz Majewski * are device-scoped, so we look up each string table we're told 6077010f5b9SLukasz Majewski * about. These lookups are infrequent; simpler-is-better here. 6087010f5b9SLukasz Majewski */ 6097010f5b9SLukasz Majewski if (composite->strings) { 6107010f5b9SLukasz Majewski len = lookup_string(composite->strings, buf, language, id); 6117010f5b9SLukasz Majewski if (len > 0) 6127010f5b9SLukasz Majewski return len; 6137010f5b9SLukasz Majewski } 6147010f5b9SLukasz Majewski list_for_each_entry(c, &cdev->configs, list) { 6157010f5b9SLukasz Majewski if (c->strings) { 6167010f5b9SLukasz Majewski len = lookup_string(c->strings, buf, language, id); 6177010f5b9SLukasz Majewski if (len > 0) 6187010f5b9SLukasz Majewski return len; 6197010f5b9SLukasz Majewski } 6207010f5b9SLukasz Majewski list_for_each_entry(f, &c->functions, list) { 6217010f5b9SLukasz Majewski if (!f->strings) 6227010f5b9SLukasz Majewski continue; 6237010f5b9SLukasz Majewski len = lookup_string(f->strings, buf, language, id); 6247010f5b9SLukasz Majewski if (len > 0) 6257010f5b9SLukasz Majewski return len; 6267010f5b9SLukasz Majewski } 6277010f5b9SLukasz Majewski } 6287010f5b9SLukasz Majewski return -EINVAL; 6297010f5b9SLukasz Majewski } 6307010f5b9SLukasz Majewski 6317010f5b9SLukasz Majewski /** 6327010f5b9SLukasz Majewski * usb_string_id() - allocate an unused string ID 6337010f5b9SLukasz Majewski * @cdev: the device whose string descriptor IDs are being allocated 6347010f5b9SLukasz Majewski * Context: single threaded during gadget setup 6357010f5b9SLukasz Majewski * 6367010f5b9SLukasz Majewski * @usb_string_id() is called from bind() callbacks to allocate 6377010f5b9SLukasz Majewski * string IDs. Drivers for functions, configurations, or gadgets will 6387010f5b9SLukasz Majewski * then store that ID in the appropriate descriptors and string table. 6397010f5b9SLukasz Majewski * 6407010f5b9SLukasz Majewski * All string identifier should be allocated using this, 6417010f5b9SLukasz Majewski * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure 6427010f5b9SLukasz Majewski * that for example different functions don't wrongly assign different 6437010f5b9SLukasz Majewski * meanings to the same identifier. 6447010f5b9SLukasz Majewski */ 6457010f5b9SLukasz Majewski int usb_string_id(struct usb_composite_dev *cdev) 6467010f5b9SLukasz Majewski { 6477010f5b9SLukasz Majewski if (cdev->next_string_id < 254) { 6487010f5b9SLukasz Majewski /* 6497010f5b9SLukasz Majewski * string id 0 is reserved by USB spec for list of 6507010f5b9SLukasz Majewski * supported languages 6517010f5b9SLukasz Majewski * 255 reserved as well? -- mina86 6527010f5b9SLukasz Majewski */ 6537010f5b9SLukasz Majewski cdev->next_string_id++; 6547010f5b9SLukasz Majewski return cdev->next_string_id; 6557010f5b9SLukasz Majewski } 6567010f5b9SLukasz Majewski return -ENODEV; 6577010f5b9SLukasz Majewski } 6587010f5b9SLukasz Majewski 6597010f5b9SLukasz Majewski /** 6607010f5b9SLukasz Majewski * usb_string_ids() - allocate unused string IDs in batch 6617010f5b9SLukasz Majewski * @cdev: the device whose string descriptor IDs are being allocated 6627010f5b9SLukasz Majewski * @str: an array of usb_string objects to assign numbers to 6637010f5b9SLukasz Majewski * Context: single threaded during gadget setup 6647010f5b9SLukasz Majewski * 6657010f5b9SLukasz Majewski * @usb_string_ids() is called from bind() callbacks to allocate 6667010f5b9SLukasz Majewski * string IDs. Drivers for functions, configurations, or gadgets will 6677010f5b9SLukasz Majewski * then copy IDs from the string table to the appropriate descriptors 6687010f5b9SLukasz Majewski * and string table for other languages. 6697010f5b9SLukasz Majewski * 6707010f5b9SLukasz Majewski * All string identifier should be allocated using this, 6717010f5b9SLukasz Majewski * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for 6727010f5b9SLukasz Majewski * example different functions don't wrongly assign different meanings 6737010f5b9SLukasz Majewski * to the same identifier. 6747010f5b9SLukasz Majewski */ 6757010f5b9SLukasz Majewski int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str) 6767010f5b9SLukasz Majewski { 6777010f5b9SLukasz Majewski u8 next = cdev->next_string_id; 6787010f5b9SLukasz Majewski 6797010f5b9SLukasz Majewski for (; str->s; ++str) { 6807010f5b9SLukasz Majewski if (next >= 254) 6817010f5b9SLukasz Majewski return -ENODEV; 6827010f5b9SLukasz Majewski str->id = ++next; 6837010f5b9SLukasz Majewski } 6847010f5b9SLukasz Majewski 6857010f5b9SLukasz Majewski cdev->next_string_id = next; 6867010f5b9SLukasz Majewski 6877010f5b9SLukasz Majewski return 0; 6887010f5b9SLukasz Majewski } 6897010f5b9SLukasz Majewski 6907010f5b9SLukasz Majewski /** 6917010f5b9SLukasz Majewski * usb_string_ids_n() - allocate unused string IDs in batch 6927010f5b9SLukasz Majewski * @c: the device whose string descriptor IDs are being allocated 6937010f5b9SLukasz Majewski * @n: number of string IDs to allocate 6947010f5b9SLukasz Majewski * Context: single threaded during gadget setup 6957010f5b9SLukasz Majewski * 6967010f5b9SLukasz Majewski * Returns the first requested ID. This ID and next @n-1 IDs are now 6977010f5b9SLukasz Majewski * valid IDs. At least provided that @n is non-zero because if it 6987010f5b9SLukasz Majewski * is, returns last requested ID which is now very useful information. 6997010f5b9SLukasz Majewski * 7007010f5b9SLukasz Majewski * @usb_string_ids_n() is called from bind() callbacks to allocate 7017010f5b9SLukasz Majewski * string IDs. Drivers for functions, configurations, or gadgets will 7027010f5b9SLukasz Majewski * then store that ID in the appropriate descriptors and string table. 7037010f5b9SLukasz Majewski * 7047010f5b9SLukasz Majewski * All string identifier should be allocated using this, 7057010f5b9SLukasz Majewski * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for 7067010f5b9SLukasz Majewski * example different functions don't wrongly assign different meanings 7077010f5b9SLukasz Majewski * to the same identifier. 7087010f5b9SLukasz Majewski */ 7097010f5b9SLukasz Majewski int usb_string_ids_n(struct usb_composite_dev *c, unsigned n) 7107010f5b9SLukasz Majewski { 7117010f5b9SLukasz Majewski u8 next = c->next_string_id; 7127010f5b9SLukasz Majewski 7137010f5b9SLukasz Majewski if (n > 254 || next + n > 254) 7147010f5b9SLukasz Majewski return -ENODEV; 7157010f5b9SLukasz Majewski 7167010f5b9SLukasz Majewski c->next_string_id += n; 7177010f5b9SLukasz Majewski return next + 1; 7187010f5b9SLukasz Majewski } 7197010f5b9SLukasz Majewski 7207010f5b9SLukasz Majewski static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req) 7217010f5b9SLukasz Majewski { 7227010f5b9SLukasz Majewski if (req->status || req->actual != req->length) 7237010f5b9SLukasz Majewski debug("%s: setup complete --> %d, %d/%d\n", __func__, 7247010f5b9SLukasz Majewski req->status, req->actual, req->length); 7257010f5b9SLukasz Majewski } 7267010f5b9SLukasz Majewski 727*bafc57b2SSimon Goldschmidt static int bos_desc(struct usb_composite_dev *cdev) 728*bafc57b2SSimon Goldschmidt { 729*bafc57b2SSimon Goldschmidt struct usb_dev_cap_header *cap; 730*bafc57b2SSimon Goldschmidt struct usb_ext_cap_descriptor *usb_ext; 731*bafc57b2SSimon Goldschmidt struct usb_ss_cap_descriptor *ss_cap; 732*bafc57b2SSimon Goldschmidt struct usb_bos_descriptor *bos = cdev->req->buf; 733*bafc57b2SSimon Goldschmidt 734*bafc57b2SSimon Goldschmidt bos->bLength = USB_DT_BOS_SIZE; 735*bafc57b2SSimon Goldschmidt bos->bDescriptorType = USB_DT_BOS; 736*bafc57b2SSimon Goldschmidt bos->wTotalLength = cpu_to_le16(USB_DT_BOS_SIZE); 737*bafc57b2SSimon Goldschmidt bos->bNumDeviceCaps = 0; 738*bafc57b2SSimon Goldschmidt 739*bafc57b2SSimon Goldschmidt if (cdev->gadget->speed < USB_SPEED_SUPER) { 740*bafc57b2SSimon Goldschmidt /* For rockusb with bcdUSB (0x0201) */ 741*bafc57b2SSimon Goldschmidt cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength); 742*bafc57b2SSimon Goldschmidt bos->bNumDeviceCaps++; 743*bafc57b2SSimon Goldschmidt bos->wTotalLength = cpu_to_le16(bos->wTotalLength + 744*bafc57b2SSimon Goldschmidt sizeof(*cap)); 745*bafc57b2SSimon Goldschmidt cap->bLength = sizeof(*cap); 746*bafc57b2SSimon Goldschmidt cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY; 747*bafc57b2SSimon Goldschmidt cap->bDevCapabilityType = 0; 748*bafc57b2SSimon Goldschmidt } else { 749*bafc57b2SSimon Goldschmidt /* 750*bafc57b2SSimon Goldschmidt * A SuperSpeed device shall include the USB2.0 751*bafc57b2SSimon Goldschmidt * extension descriptor and shall support LPM when 752*bafc57b2SSimon Goldschmidt * operating in USB2.0 HS mode. 753*bafc57b2SSimon Goldschmidt */ 754*bafc57b2SSimon Goldschmidt usb_ext = cdev->req->buf + le16_to_cpu(bos->wTotalLength); 755*bafc57b2SSimon Goldschmidt bos->bNumDeviceCaps++; 756*bafc57b2SSimon Goldschmidt le16_add_cpu_packed((__le16_packed *)&bos->wTotalLength, 757*bafc57b2SSimon Goldschmidt USB_DT_USB_EXT_CAP_SIZE); 758*bafc57b2SSimon Goldschmidt usb_ext->bLength = USB_DT_USB_EXT_CAP_SIZE; 759*bafc57b2SSimon Goldschmidt usb_ext->bDescriptorType = USB_DT_DEVICE_CAPABILITY; 760*bafc57b2SSimon Goldschmidt usb_ext->bDevCapabilityType = USB_CAP_TYPE_EXT; 761*bafc57b2SSimon Goldschmidt usb_ext->bmAttributes = USB_LPM_SUPPORT; 762*bafc57b2SSimon Goldschmidt 763*bafc57b2SSimon Goldschmidt /* 764*bafc57b2SSimon Goldschmidt * The Superspeed USB Capability descriptor shall be 765*bafc57b2SSimon Goldschmidt * implemented by all SuperSpeed devices. 766*bafc57b2SSimon Goldschmidt */ 767*bafc57b2SSimon Goldschmidt ss_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength); 768*bafc57b2SSimon Goldschmidt bos->bNumDeviceCaps++; 769*bafc57b2SSimon Goldschmidt le16_add_cpu_packed((__le16_packed *)&bos->wTotalLength, 770*bafc57b2SSimon Goldschmidt USB_DT_USB_SS_CAP_SIZE); 771*bafc57b2SSimon Goldschmidt ss_cap->bLength = USB_DT_USB_SS_CAP_SIZE; 772*bafc57b2SSimon Goldschmidt ss_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY; 773*bafc57b2SSimon Goldschmidt ss_cap->bDevCapabilityType = USB_SS_CAP_TYPE; 774*bafc57b2SSimon Goldschmidt ss_cap->bmAttributes = 0; /* LTM is not supported yet */ 775*bafc57b2SSimon Goldschmidt ss_cap->wSpeedSupported = cpu_to_le16(USB_FULL_SPEED_OPERATION | 776*bafc57b2SSimon Goldschmidt USB_HIGH_SPEED_OPERATION | 777*bafc57b2SSimon Goldschmidt USB_5GBPS_OPERATION); 778*bafc57b2SSimon Goldschmidt ss_cap->bFunctionalitySupport = USB_FULL_SPEED_OPERATION; 779*bafc57b2SSimon Goldschmidt ss_cap->bU1devExitLat = USB_DEFAULT_U1_DEV_EXIT_LAT; 780*bafc57b2SSimon Goldschmidt ss_cap->bU2DevExitLat = cpu_to_le16(USB_DEFAULT_U2_DEV_EXIT_LAT); 781*bafc57b2SSimon Goldschmidt } 782*bafc57b2SSimon Goldschmidt 783*bafc57b2SSimon Goldschmidt return le16_to_cpu(bos->wTotalLength); 784*bafc57b2SSimon Goldschmidt } 785*bafc57b2SSimon Goldschmidt 7867010f5b9SLukasz Majewski /* 7877010f5b9SLukasz Majewski * The setup() callback implements all the ep0 functionality that's 7887010f5b9SLukasz Majewski * not handled lower down, in hardware or the hardware driver(like 7897010f5b9SLukasz Majewski * device and endpoint feature flags, and their status). It's all 7907010f5b9SLukasz Majewski * housekeeping for the gadget function we're implementing. Most of 7917010f5b9SLukasz Majewski * the work is in config and function specific setup. 7927010f5b9SLukasz Majewski */ 7937010f5b9SLukasz Majewski static int 7947010f5b9SLukasz Majewski composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl) 7957010f5b9SLukasz Majewski { 7967010f5b9SLukasz Majewski u16 w_length = le16_to_cpu(ctrl->wLength); 7977010f5b9SLukasz Majewski u16 w_index = le16_to_cpu(ctrl->wIndex); 7987010f5b9SLukasz Majewski u16 w_value = le16_to_cpu(ctrl->wValue); 7997010f5b9SLukasz Majewski struct usb_composite_dev *cdev = get_gadget_data(gadget); 8007010f5b9SLukasz Majewski u8 intf = w_index & 0xFF; 8017010f5b9SLukasz Majewski int value = -EOPNOTSUPP; 8027010f5b9SLukasz Majewski struct usb_request *req = cdev->req; 8037010f5b9SLukasz Majewski struct usb_function *f = NULL; 8047010f5b9SLukasz Majewski int standard; 8057010f5b9SLukasz Majewski u8 endp; 8067010f5b9SLukasz Majewski struct usb_configuration *c; 8077010f5b9SLukasz Majewski 8087010f5b9SLukasz Majewski /* 8097010f5b9SLukasz Majewski * partial re-init of the response message; the function or the 8107010f5b9SLukasz Majewski * gadget might need to intercept e.g. a control-OUT completion 8117010f5b9SLukasz Majewski * when we delegate to it. 8127010f5b9SLukasz Majewski */ 8137010f5b9SLukasz Majewski req->zero = 0; 8147010f5b9SLukasz Majewski req->complete = composite_setup_complete; 8157010f5b9SLukasz Majewski req->length = USB_BUFSIZ; 8167010f5b9SLukasz Majewski gadget->ep0->driver_data = cdev; 8177010f5b9SLukasz Majewski standard = (ctrl->bRequestType & USB_TYPE_MASK) 8187010f5b9SLukasz Majewski == USB_TYPE_STANDARD; 81926dd3474SWilliam Wu 8207010f5b9SLukasz Majewski if (!standard) 8217010f5b9SLukasz Majewski goto unknown; 8227010f5b9SLukasz Majewski 8237010f5b9SLukasz Majewski switch (ctrl->bRequest) { 8247010f5b9SLukasz Majewski 8257010f5b9SLukasz Majewski /* we handle all standard USB descriptors */ 8267010f5b9SLukasz Majewski case USB_REQ_GET_DESCRIPTOR: 8277010f5b9SLukasz Majewski if (ctrl->bRequestType != USB_DIR_IN) 8287010f5b9SLukasz Majewski goto unknown; 8297010f5b9SLukasz Majewski switch (w_value >> 8) { 8307010f5b9SLukasz Majewski 8317010f5b9SLukasz Majewski case USB_DT_DEVICE: 8327010f5b9SLukasz Majewski cdev->desc.bNumConfigurations = 8337010f5b9SLukasz Majewski count_configs(cdev, USB_DT_DEVICE); 834204f1379SSiva Durga Prasad Paladugu 83526dd3474SWilliam Wu if (gadget_is_superspeed(gadget) && 83626dd3474SWilliam Wu gadget->speed >= USB_SPEED_SUPER) { 83726dd3474SWilliam Wu /* 83826dd3474SWilliam Wu * bcdUSB should be 0x0300 for superspeed, 83926dd3474SWilliam Wu * but we change it to 0x0301 for rockusb. 84026dd3474SWilliam Wu */ 84126dd3474SWilliam Wu if (!strncmp(cdev->driver->name, 84226dd3474SWilliam Wu "rkusb_ums_dnl", 13)) 84326dd3474SWilliam Wu cdev->desc.bcdUSB = cpu_to_le16(0x0301); 84426dd3474SWilliam Wu else 84526dd3474SWilliam Wu cdev->desc.bcdUSB = cpu_to_le16(0x0300); 84626dd3474SWilliam Wu cdev->desc.bMaxPacketSize0 = 9; 847204f1379SSiva Durga Prasad Paladugu } else { 848204f1379SSiva Durga Prasad Paladugu cdev->desc.bMaxPacketSize0 = 849204f1379SSiva Durga Prasad Paladugu cdev->gadget->ep0->maxpacket; 85026dd3474SWilliam Wu } 85126dd3474SWilliam Wu 8527010f5b9SLukasz Majewski value = min(w_length, (u16) sizeof cdev->desc); 8537010f5b9SLukasz Majewski memcpy(req->buf, &cdev->desc, value); 8547010f5b9SLukasz Majewski break; 8557010f5b9SLukasz Majewski case USB_DT_DEVICE_QUALIFIER: 8567010f5b9SLukasz Majewski if (!gadget_is_dualspeed(gadget)) 8577010f5b9SLukasz Majewski break; 8587010f5b9SLukasz Majewski device_qual(cdev); 859b4141195SMasahiro Yamada value = min_t(int, w_length, 8607010f5b9SLukasz Majewski sizeof(struct usb_qualifier_descriptor)); 8617010f5b9SLukasz Majewski break; 8627010f5b9SLukasz Majewski case USB_DT_OTHER_SPEED_CONFIG: 8637010f5b9SLukasz Majewski if (!gadget_is_dualspeed(gadget)) 8647010f5b9SLukasz Majewski break; 8657010f5b9SLukasz Majewski 8667010f5b9SLukasz Majewski case USB_DT_CONFIG: 8677010f5b9SLukasz Majewski value = config_desc(cdev, w_value); 8687010f5b9SLukasz Majewski if (value >= 0) 8697010f5b9SLukasz Majewski value = min(w_length, (u16) value); 8707010f5b9SLukasz Majewski break; 8717010f5b9SLukasz Majewski case USB_DT_STRING: 8727010f5b9SLukasz Majewski value = get_string(cdev, req->buf, 8737010f5b9SLukasz Majewski w_index, w_value & 0xff); 8747010f5b9SLukasz Majewski if (value >= 0) 8757010f5b9SLukasz Majewski value = min(w_length, (u16) value); 8767010f5b9SLukasz Majewski break; 87787ed6b10SStefan Roese case USB_DT_BOS: 8788ddd5824SFrank Wang /* HACK: only for rockusb command. 8798ddd5824SFrank Wang * Rockchip upgrade tool use bcdUSB (0x0201) field 8808ddd5824SFrank Wang * distinguishing maskrom or loader device at present. 8818ddd5824SFrank Wang * Unfortunately, it conflict with Windows 8 and beyond 8828ddd5824SFrank Wang * which request BOS descriptor in this case that bcdUSB 8838ddd5824SFrank Wang * is set to 0x0201. 8848ddd5824SFrank Wang */ 88526dd3474SWilliam Wu if (gadget_is_superspeed(gadget) || 88626dd3474SWilliam Wu !strncmp(cdev->driver->name, "rkusb_ums_dnl", 13)) { 8878ddd5824SFrank Wang value = bos_desc(cdev); 8888ddd5824SFrank Wang value = min(w_length, (u16) value); 8898ddd5824SFrank Wang } 8908ddd5824SFrank Wang 89187ed6b10SStefan Roese /* 89287ed6b10SStefan Roese * The USB compliance test (USB 2.0 Command Verifier) 89387ed6b10SStefan Roese * issues this request. We should not run into the 89487ed6b10SStefan Roese * default path here. But return for now until 89587ed6b10SStefan Roese * the superspeed support is added. 89687ed6b10SStefan Roese */ 89787ed6b10SStefan Roese break; 8987010f5b9SLukasz Majewski default: 8997010f5b9SLukasz Majewski goto unknown; 9007010f5b9SLukasz Majewski } 9017010f5b9SLukasz Majewski break; 9027010f5b9SLukasz Majewski 9037010f5b9SLukasz Majewski /* any number of configs can work */ 9047010f5b9SLukasz Majewski case USB_REQ_SET_CONFIGURATION: 9057010f5b9SLukasz Majewski if (ctrl->bRequestType != 0) 9067010f5b9SLukasz Majewski goto unknown; 9077010f5b9SLukasz Majewski if (gadget_is_otg(gadget)) { 9087010f5b9SLukasz Majewski if (gadget->a_hnp_support) 9097010f5b9SLukasz Majewski debug("HNP available\n"); 9107010f5b9SLukasz Majewski else if (gadget->a_alt_hnp_support) 9117010f5b9SLukasz Majewski debug("HNP on another port\n"); 9127010f5b9SLukasz Majewski else 9137010f5b9SLukasz Majewski debug("HNP inactive\n"); 9147010f5b9SLukasz Majewski } 9157010f5b9SLukasz Majewski 9167010f5b9SLukasz Majewski value = set_config(cdev, ctrl, w_value); 9177010f5b9SLukasz Majewski break; 9187010f5b9SLukasz Majewski case USB_REQ_GET_CONFIGURATION: 9197010f5b9SLukasz Majewski if (ctrl->bRequestType != USB_DIR_IN) 9207010f5b9SLukasz Majewski goto unknown; 9217010f5b9SLukasz Majewski if (cdev->config) 9227010f5b9SLukasz Majewski *(u8 *)req->buf = cdev->config->bConfigurationValue; 9237010f5b9SLukasz Majewski else 9247010f5b9SLukasz Majewski *(u8 *)req->buf = 0; 9257010f5b9SLukasz Majewski value = min(w_length, (u16) 1); 9267010f5b9SLukasz Majewski break; 9277010f5b9SLukasz Majewski 9287010f5b9SLukasz Majewski /* 9297010f5b9SLukasz Majewski * function drivers must handle get/set altsetting; if there's 9307010f5b9SLukasz Majewski * no get() method, we know only altsetting zero works. 9317010f5b9SLukasz Majewski */ 9327010f5b9SLukasz Majewski case USB_REQ_SET_INTERFACE: 9337010f5b9SLukasz Majewski if (ctrl->bRequestType != USB_RECIP_INTERFACE) 9347010f5b9SLukasz Majewski goto unknown; 9357010f5b9SLukasz Majewski if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES) 9367010f5b9SLukasz Majewski break; 9377010f5b9SLukasz Majewski f = cdev->config->interface[intf]; 9387010f5b9SLukasz Majewski if (!f) 9397010f5b9SLukasz Majewski break; 9407010f5b9SLukasz Majewski if (w_value && !f->set_alt) 9417010f5b9SLukasz Majewski break; 9427010f5b9SLukasz Majewski value = f->set_alt(f, w_index, w_value); 9437010f5b9SLukasz Majewski break; 9447010f5b9SLukasz Majewski case USB_REQ_GET_INTERFACE: 9457010f5b9SLukasz Majewski if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)) 9467010f5b9SLukasz Majewski goto unknown; 9477010f5b9SLukasz Majewski if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES) 9487010f5b9SLukasz Majewski break; 9497010f5b9SLukasz Majewski f = cdev->config->interface[intf]; 9507010f5b9SLukasz Majewski if (!f) 9517010f5b9SLukasz Majewski break; 9527010f5b9SLukasz Majewski /* lots of interfaces only need altsetting zero... */ 9537010f5b9SLukasz Majewski value = f->get_alt ? f->get_alt(f, w_index) : 0; 9547010f5b9SLukasz Majewski if (value < 0) 9557010f5b9SLukasz Majewski break; 9567010f5b9SLukasz Majewski *((u8 *)req->buf) = value; 9577010f5b9SLukasz Majewski value = min(w_length, (u16) 1); 9587010f5b9SLukasz Majewski break; 9597010f5b9SLukasz Majewski default: 9607010f5b9SLukasz Majewski unknown: 9617010f5b9SLukasz Majewski debug("non-core control req%02x.%02x v%04x i%04x l%d\n", 9627010f5b9SLukasz Majewski ctrl->bRequestType, ctrl->bRequest, 9637010f5b9SLukasz Majewski w_value, w_index, w_length); 9647010f5b9SLukasz Majewski 965088d8eb9SChristophe Kerello if (!cdev->config) 966088d8eb9SChristophe Kerello goto done; 967088d8eb9SChristophe Kerello 9687010f5b9SLukasz Majewski /* 9697010f5b9SLukasz Majewski * functions always handle their interfaces and endpoints... 9707010f5b9SLukasz Majewski * punt other recipients (other, WUSB, ...) to the current 9717010f5b9SLukasz Majewski * configuration code. 9727010f5b9SLukasz Majewski */ 9737010f5b9SLukasz Majewski switch (ctrl->bRequestType & USB_RECIP_MASK) { 9747010f5b9SLukasz Majewski case USB_RECIP_INTERFACE: 9751bca07a1SFrank Wang if (!cdev->config) 9761bca07a1SFrank Wang break; 9777010f5b9SLukasz Majewski f = cdev->config->interface[intf]; 9787010f5b9SLukasz Majewski break; 9797010f5b9SLukasz Majewski 9807010f5b9SLukasz Majewski case USB_RECIP_ENDPOINT: 9811bca07a1SFrank Wang if (!cdev->config) 9821bca07a1SFrank Wang break; 9837010f5b9SLukasz Majewski endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f); 9847010f5b9SLukasz Majewski list_for_each_entry(f, &cdev->config->functions, list) { 9857010f5b9SLukasz Majewski if (test_bit(endp, f->endpoints)) 9867010f5b9SLukasz Majewski break; 9877010f5b9SLukasz Majewski } 9887010f5b9SLukasz Majewski if (&f->list == &cdev->config->functions) 9897010f5b9SLukasz Majewski f = NULL; 9907010f5b9SLukasz Majewski break; 991f7b4162eSLukasz Majewski /* 992f7b4162eSLukasz Majewski * dfu-util (version 0.5) sets bmRequestType.Receipent = Device 993f7b4162eSLukasz Majewski * for non-standard request (w_value = 0x21, 994f7b4162eSLukasz Majewski * bRequest = GET_DESCRIPTOR in this case). 995f7b4162eSLukasz Majewski * When only one interface is registered (as it is done now), 996f7b4162eSLukasz Majewski * then this request shall be handled as it was requested for 997f7b4162eSLukasz Majewski * interface. 998f7b4162eSLukasz Majewski * 999f7b4162eSLukasz Majewski * In the below code it is checked if only one interface is 1000f7b4162eSLukasz Majewski * present and proper function for it is extracted. Due to that 1001f7b4162eSLukasz Majewski * function's setup (f->setup) is called to handle this 1002f7b4162eSLukasz Majewski * special non-standard request. 1003f7b4162eSLukasz Majewski */ 1004f7b4162eSLukasz Majewski case USB_RECIP_DEVICE: 1005e290ced6SFrank Wang if (cdev->config) { 1006f7b4162eSLukasz Majewski debug("cdev->config->next_interface_id: %d intf: %d\n", 1007f7b4162eSLukasz Majewski cdev->config->next_interface_id, intf); 1008f7b4162eSLukasz Majewski if (cdev->config->next_interface_id == 1) 1009f7b4162eSLukasz Majewski f = cdev->config->interface[intf]; 1010e290ced6SFrank Wang } 1011f7b4162eSLukasz Majewski break; 10127010f5b9SLukasz Majewski } 10137010f5b9SLukasz Majewski 10147010f5b9SLukasz Majewski if (f && f->setup) 10157010f5b9SLukasz Majewski value = f->setup(f, ctrl); 10167010f5b9SLukasz Majewski else { 10177010f5b9SLukasz Majewski c = cdev->config; 1018088d8eb9SChristophe Kerello if (c->setup) 10197010f5b9SLukasz Majewski value = c->setup(c, ctrl); 10207010f5b9SLukasz Majewski } 10217010f5b9SLukasz Majewski 10227010f5b9SLukasz Majewski goto done; 10237010f5b9SLukasz Majewski } 10247010f5b9SLukasz Majewski 10257010f5b9SLukasz Majewski /* respond with data transfer before status phase? */ 10267010f5b9SLukasz Majewski if (value >= 0) { 10277010f5b9SLukasz Majewski req->length = value; 10287010f5b9SLukasz Majewski req->zero = value < w_length; 10297010f5b9SLukasz Majewski value = usb_ep_queue(gadget->ep0, req, GFP_KERNEL); 10307010f5b9SLukasz Majewski if (value < 0) { 10317010f5b9SLukasz Majewski debug("ep_queue --> %d\n", value); 10327010f5b9SLukasz Majewski req->status = 0; 10337010f5b9SLukasz Majewski composite_setup_complete(gadget->ep0, req); 10347010f5b9SLukasz Majewski } 10357010f5b9SLukasz Majewski } 10367010f5b9SLukasz Majewski 10377010f5b9SLukasz Majewski done: 10387010f5b9SLukasz Majewski /* device either stalls (value < 0) or reports success */ 10397010f5b9SLukasz Majewski return value; 10407010f5b9SLukasz Majewski } 10417010f5b9SLukasz Majewski 10427010f5b9SLukasz Majewski static void composite_disconnect(struct usb_gadget *gadget) 10437010f5b9SLukasz Majewski { 10447010f5b9SLukasz Majewski struct usb_composite_dev *cdev = get_gadget_data(gadget); 10457010f5b9SLukasz Majewski 10467010f5b9SLukasz Majewski if (cdev->config) 10477010f5b9SLukasz Majewski reset_config(cdev); 10487010f5b9SLukasz Majewski if (composite->disconnect) 10497010f5b9SLukasz Majewski composite->disconnect(cdev); 10507010f5b9SLukasz Majewski } 10517010f5b9SLukasz Majewski 10527010f5b9SLukasz Majewski static void composite_unbind(struct usb_gadget *gadget) 10537010f5b9SLukasz Majewski { 10547010f5b9SLukasz Majewski struct usb_composite_dev *cdev = get_gadget_data(gadget); 10557010f5b9SLukasz Majewski struct usb_configuration *c; 10567010f5b9SLukasz Majewski struct usb_function *f; 10577010f5b9SLukasz Majewski 10587010f5b9SLukasz Majewski /* 10597010f5b9SLukasz Majewski * composite_disconnect() must already have been called 10607010f5b9SLukasz Majewski * by the underlying peripheral controller driver! 10617010f5b9SLukasz Majewski * so there's no i/o concurrency that could affect the 10627010f5b9SLukasz Majewski * state protected by cdev->lock. 10637010f5b9SLukasz Majewski */ 10647010f5b9SLukasz Majewski BUG_ON(cdev->config); 10657010f5b9SLukasz Majewski 10667010f5b9SLukasz Majewski while (!list_empty(&cdev->configs)) { 10677010f5b9SLukasz Majewski c = list_first_entry(&cdev->configs, 10687010f5b9SLukasz Majewski struct usb_configuration, list); 10697010f5b9SLukasz Majewski while (!list_empty(&c->functions)) { 10707010f5b9SLukasz Majewski f = list_first_entry(&c->functions, 10717010f5b9SLukasz Majewski struct usb_function, list); 10727010f5b9SLukasz Majewski list_del(&f->list); 10737010f5b9SLukasz Majewski if (f->unbind) { 10747010f5b9SLukasz Majewski debug("unbind function '%s'/%p\n", 10757010f5b9SLukasz Majewski f->name, f); 10767010f5b9SLukasz Majewski f->unbind(c, f); 10777010f5b9SLukasz Majewski } 10787010f5b9SLukasz Majewski } 10797010f5b9SLukasz Majewski list_del(&c->list); 10807010f5b9SLukasz Majewski if (c->unbind) { 10817010f5b9SLukasz Majewski debug("unbind config '%s'/%p\n", c->label, c); 10827010f5b9SLukasz Majewski c->unbind(c); 10837010f5b9SLukasz Majewski } 108444bfb43fSStephen Warren free(c); 10857010f5b9SLukasz Majewski } 10867010f5b9SLukasz Majewski if (composite->unbind) 10877010f5b9SLukasz Majewski composite->unbind(cdev); 10887010f5b9SLukasz Majewski 10897010f5b9SLukasz Majewski if (cdev->req) { 10907010f5b9SLukasz Majewski kfree(cdev->req->buf); 10917010f5b9SLukasz Majewski usb_ep_free_request(gadget->ep0, cdev->req); 10927010f5b9SLukasz Majewski } 10937010f5b9SLukasz Majewski kfree(cdev); 10947010f5b9SLukasz Majewski set_gadget_data(gadget, NULL); 10957010f5b9SLukasz Majewski 10967010f5b9SLukasz Majewski composite = NULL; 10977010f5b9SLukasz Majewski } 10987010f5b9SLukasz Majewski 10997010f5b9SLukasz Majewski static int composite_bind(struct usb_gadget *gadget) 11007010f5b9SLukasz Majewski { 11017010f5b9SLukasz Majewski int status = -ENOMEM; 11027010f5b9SLukasz Majewski struct usb_composite_dev *cdev; 11037010f5b9SLukasz Majewski 11047010f5b9SLukasz Majewski cdev = calloc(sizeof *cdev, 1); 11057010f5b9SLukasz Majewski if (!cdev) 11067010f5b9SLukasz Majewski return status; 11077010f5b9SLukasz Majewski 11087010f5b9SLukasz Majewski cdev->gadget = gadget; 11097010f5b9SLukasz Majewski set_gadget_data(gadget, cdev); 11107010f5b9SLukasz Majewski INIT_LIST_HEAD(&cdev->configs); 11117010f5b9SLukasz Majewski 11127010f5b9SLukasz Majewski /* preallocate control response and buffer */ 11137010f5b9SLukasz Majewski cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL); 11147010f5b9SLukasz Majewski if (!cdev->req) 11157010f5b9SLukasz Majewski goto fail; 11167010f5b9SLukasz Majewski cdev->req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, USB_BUFSIZ); 11177010f5b9SLukasz Majewski if (!cdev->req->buf) 11187010f5b9SLukasz Majewski goto fail; 11197010f5b9SLukasz Majewski cdev->req->complete = composite_setup_complete; 11207010f5b9SLukasz Majewski gadget->ep0->driver_data = cdev; 11217010f5b9SLukasz Majewski 11227010f5b9SLukasz Majewski cdev->bufsiz = USB_BUFSIZ; 11237010f5b9SLukasz Majewski cdev->driver = composite; 11247010f5b9SLukasz Majewski 11257010f5b9SLukasz Majewski usb_gadget_set_selfpowered(gadget); 11267010f5b9SLukasz Majewski usb_ep_autoconfig_reset(cdev->gadget); 11277010f5b9SLukasz Majewski 11287010f5b9SLukasz Majewski status = composite->bind(cdev); 11297010f5b9SLukasz Majewski if (status < 0) 11307010f5b9SLukasz Majewski goto fail; 11317010f5b9SLukasz Majewski 113212595e99SPiotr Wilczek memcpy(&cdev->desc, composite->dev, 113312595e99SPiotr Wilczek sizeof(struct usb_device_descriptor)); 11347010f5b9SLukasz Majewski cdev->desc.bMaxPacketSize0 = gadget->ep0->maxpacket; 11357010f5b9SLukasz Majewski 11367010f5b9SLukasz Majewski debug("%s: ready\n", composite->name); 11377010f5b9SLukasz Majewski return 0; 11387010f5b9SLukasz Majewski 11397010f5b9SLukasz Majewski fail: 11407010f5b9SLukasz Majewski composite_unbind(gadget); 11417010f5b9SLukasz Majewski return status; 11427010f5b9SLukasz Majewski } 11437010f5b9SLukasz Majewski 11447010f5b9SLukasz Majewski static void 11457010f5b9SLukasz Majewski composite_suspend(struct usb_gadget *gadget) 11467010f5b9SLukasz Majewski { 11477010f5b9SLukasz Majewski struct usb_composite_dev *cdev = get_gadget_data(gadget); 11487010f5b9SLukasz Majewski struct usb_function *f; 11497010f5b9SLukasz Majewski 11507010f5b9SLukasz Majewski debug("%s: suspend\n", __func__); 11517010f5b9SLukasz Majewski if (cdev->config) { 11527010f5b9SLukasz Majewski list_for_each_entry(f, &cdev->config->functions, list) { 11537010f5b9SLukasz Majewski if (f->suspend) 11547010f5b9SLukasz Majewski f->suspend(f); 11557010f5b9SLukasz Majewski } 11567010f5b9SLukasz Majewski } 11577010f5b9SLukasz Majewski if (composite->suspend) 11587010f5b9SLukasz Majewski composite->suspend(cdev); 11597010f5b9SLukasz Majewski 11607010f5b9SLukasz Majewski cdev->suspended = 1; 11617010f5b9SLukasz Majewski } 11627010f5b9SLukasz Majewski 11637010f5b9SLukasz Majewski static void 11647010f5b9SLukasz Majewski composite_resume(struct usb_gadget *gadget) 11657010f5b9SLukasz Majewski { 11667010f5b9SLukasz Majewski struct usb_composite_dev *cdev = get_gadget_data(gadget); 11677010f5b9SLukasz Majewski struct usb_function *f; 11687010f5b9SLukasz Majewski 11697010f5b9SLukasz Majewski debug("%s: resume\n", __func__); 11707010f5b9SLukasz Majewski if (composite->resume) 11717010f5b9SLukasz Majewski composite->resume(cdev); 11727010f5b9SLukasz Majewski if (cdev->config) { 11737010f5b9SLukasz Majewski list_for_each_entry(f, &cdev->config->functions, list) { 11747010f5b9SLukasz Majewski if (f->resume) 11757010f5b9SLukasz Majewski f->resume(f); 11767010f5b9SLukasz Majewski } 11777010f5b9SLukasz Majewski } 11787010f5b9SLukasz Majewski 11797010f5b9SLukasz Majewski cdev->suspended = 0; 11807010f5b9SLukasz Majewski } 11817010f5b9SLukasz Majewski 11827010f5b9SLukasz Majewski static struct usb_gadget_driver composite_driver = { 11837010f5b9SLukasz Majewski .speed = USB_SPEED_HIGH, 11847010f5b9SLukasz Majewski 11857010f5b9SLukasz Majewski .bind = composite_bind, 11867010f5b9SLukasz Majewski .unbind = composite_unbind, 11877010f5b9SLukasz Majewski 11887010f5b9SLukasz Majewski .setup = composite_setup, 11896d691732SLukasz Majewski .reset = composite_disconnect, 11907010f5b9SLukasz Majewski .disconnect = composite_disconnect, 11917010f5b9SLukasz Majewski 11927010f5b9SLukasz Majewski .suspend = composite_suspend, 11937010f5b9SLukasz Majewski .resume = composite_resume, 11947010f5b9SLukasz Majewski }; 11957010f5b9SLukasz Majewski 11967010f5b9SLukasz Majewski /** 11977010f5b9SLukasz Majewski * usb_composite_register() - register a composite driver 11987010f5b9SLukasz Majewski * @driver: the driver to register 11997010f5b9SLukasz Majewski * Context: single threaded during gadget setup 12007010f5b9SLukasz Majewski * 12017010f5b9SLukasz Majewski * This function is used to register drivers using the composite driver 12027010f5b9SLukasz Majewski * framework. The return value is zero, or a negative errno value. 12037010f5b9SLukasz Majewski * Those values normally come from the driver's @bind method, which does 12047010f5b9SLukasz Majewski * all the work of setting up the driver to match the hardware. 12057010f5b9SLukasz Majewski * 12067010f5b9SLukasz Majewski * On successful return, the gadget is ready to respond to requests from 12077010f5b9SLukasz Majewski * the host, unless one of its components invokes usb_gadget_disconnect() 12087010f5b9SLukasz Majewski * while it was binding. That would usually be done in order to wait for 12097010f5b9SLukasz Majewski * some userspace participation. 12107010f5b9SLukasz Majewski */ 12117010f5b9SLukasz Majewski int usb_composite_register(struct usb_composite_driver *driver) 12127010f5b9SLukasz Majewski { 12138038f6d2SSam Protsenko int res; 12148038f6d2SSam Protsenko 12157010f5b9SLukasz Majewski if (!driver || !driver->dev || !driver->bind || composite) 12167010f5b9SLukasz Majewski return -EINVAL; 12177010f5b9SLukasz Majewski 12187010f5b9SLukasz Majewski if (!driver->name) 12197010f5b9SLukasz Majewski driver->name = "composite"; 12207010f5b9SLukasz Majewski composite = driver; 12217010f5b9SLukasz Majewski 12228038f6d2SSam Protsenko res = usb_gadget_register_driver(&composite_driver); 12238038f6d2SSam Protsenko if (res != 0) 12248038f6d2SSam Protsenko composite = NULL; 12258038f6d2SSam Protsenko 12268038f6d2SSam Protsenko return res; 12277010f5b9SLukasz Majewski } 12287010f5b9SLukasz Majewski 12297010f5b9SLukasz Majewski /** 12307010f5b9SLukasz Majewski * usb_composite_unregister() - unregister a composite driver 12317010f5b9SLukasz Majewski * @driver: the driver to unregister 12327010f5b9SLukasz Majewski * 12337010f5b9SLukasz Majewski * This function is used to unregister drivers using the composite 12347010f5b9SLukasz Majewski * driver framework. 12357010f5b9SLukasz Majewski */ 12367010f5b9SLukasz Majewski void usb_composite_unregister(struct usb_composite_driver *driver) 12377010f5b9SLukasz Majewski { 12387010f5b9SLukasz Majewski if (composite != driver) 12397010f5b9SLukasz Majewski return; 12407010f5b9SLukasz Majewski usb_gadget_unregister_driver(&composite_driver); 1241c67b0e42SHeiko Schocher composite = NULL; 12427010f5b9SLukasz Majewski } 1243