^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * usb/gadget/config.c -- simplify building config descriptors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2003 David Brownell
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/usb/ch9.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/usb/gadget.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/usb/composite.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/usb/otg.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * usb_descriptor_fillbuf - fill buffer with descriptors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * @buf: Buffer to be filled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * @buflen: Size of buf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * @src: Array of descriptor pointers, terminated by null pointer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * Copies descriptors into the buffer, returning the length or a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * negative error code if they can't all be copied. Useful when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * assembling descriptors for an associated set of interfaces used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * as part of configuring a composite device; or in other cases where
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * sets of descriptors need to be marshaled.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) usb_descriptor_fillbuf(void *buf, unsigned buflen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) const struct usb_descriptor_header **src)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) u8 *dest = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) if (!src)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) /* fill buffer from src[] until null descriptor ptr */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) for (; NULL != *src; src++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) unsigned len = (*src)->bLength;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) if (len > buflen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) memcpy(dest, *src, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) buflen -= len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) dest += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) return dest - (u8 *)buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) EXPORT_SYMBOL_GPL(usb_descriptor_fillbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) * usb_gadget_config_buf - builts a complete configuration descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) * @config: Header for the descriptor, including characteristics such
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) * as power requirements and number of interfaces.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) * @desc: Null-terminated vector of pointers to the descriptors (interface,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) * endpoint, etc) defining all functions in this device configuration.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) * @buf: Buffer for the resulting configuration descriptor.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) * @length: Length of buffer. If this is not big enough to hold the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) * entire configuration descriptor, an error code will be returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * This copies descriptors into the response buffer, building a descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) * for that configuration. It returns the buffer length or a negative
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) * status code. The config.wTotalLength field is set to match the length
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) * of the result, but other descriptor fields (including power usage and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) * interface count) must be set by the caller.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) * Gadget drivers could use this when constructing a config descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) * in response to USB_REQ_GET_DESCRIPTOR. They will need to patch the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) * resulting bDescriptorType value if USB_DT_OTHER_SPEED_CONFIG is needed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) int usb_gadget_config_buf(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) const struct usb_config_descriptor *config,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) void *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) unsigned length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) const struct usb_descriptor_header **desc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) struct usb_config_descriptor *cp = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) /* config descriptor first */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) if (length < USB_DT_CONFIG_SIZE || !desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) *cp = *config;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) /* then interface/endpoint/class/vendor/... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) len = usb_descriptor_fillbuf(USB_DT_CONFIG_SIZE + (u8 *)buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) length - USB_DT_CONFIG_SIZE, desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) if (len < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) len += USB_DT_CONFIG_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) if (len > 0xffff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) /* patch up the config descriptor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) cp->bLength = USB_DT_CONFIG_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) cp->bDescriptorType = USB_DT_CONFIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) cp->wTotalLength = cpu_to_le16(len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) cp->bmAttributes |= USB_CONFIG_ATT_ONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) EXPORT_SYMBOL_GPL(usb_gadget_config_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) * usb_copy_descriptors - copy a vector of USB descriptors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) * @src: null-terminated vector to copy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) * Context: initialization code, which may sleep
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) * This makes a copy of a vector of USB descriptors. Its primary use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) * is to support usb_function objects which can have multiple copies,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) * each needing different descriptors. Functions may have static
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) * tables of descriptors, which are used as templates and customized
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) * with identifiers (for interfaces, strings, endpoints, and more)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) * as needed by a given function instance.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) struct usb_descriptor_header **
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) usb_copy_descriptors(struct usb_descriptor_header **src)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) struct usb_descriptor_header **tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) unsigned bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) unsigned n_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) void *mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) struct usb_descriptor_header **ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) /* count descriptors and their sizes; then add vector size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) for (bytes = 0, n_desc = 0, tmp = src; *tmp; tmp++, n_desc++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) bytes += (*tmp)->bLength;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) bytes += (n_desc + 1) * sizeof(*tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) mem = kmalloc(bytes, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) if (!mem)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) /* fill in pointers starting at "tmp",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) * to descriptors copied starting at "mem";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) * and return "ret"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) tmp = mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) ret = mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) mem += (n_desc + 1) * sizeof(*tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) while (*src) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) memcpy(mem, *src, (*src)->bLength);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) *tmp = mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) tmp++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) mem += (*src)->bLength;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) src++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) *tmp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) EXPORT_SYMBOL_GPL(usb_copy_descriptors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) int usb_assign_descriptors(struct usb_function *f,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) struct usb_descriptor_header **fs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) struct usb_descriptor_header **hs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) struct usb_descriptor_header **ss,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) struct usb_descriptor_header **ssp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) struct usb_gadget *g = f->config->cdev->gadget;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) /* super-speed-plus descriptor falls back to super-speed one,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) * if such a descriptor was provided, thus avoiding a NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) * pointer dereference if a 5gbps capable gadget is used with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) * a 10gbps capable config (device port + cable + host port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) if (!ssp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) ssp = ss;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) if (fs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) f->fs_descriptors = usb_copy_descriptors(fs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) if (!f->fs_descriptors)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) if (hs && gadget_is_dualspeed(g)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) f->hs_descriptors = usb_copy_descriptors(hs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) if (!f->hs_descriptors)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) if (ss && gadget_is_superspeed(g)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) f->ss_descriptors = usb_copy_descriptors(ss);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) if (!f->ss_descriptors)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) if (ssp && gadget_is_superspeed_plus(g)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) f->ssp_descriptors = usb_copy_descriptors(ssp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) if (!f->ssp_descriptors)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) usb_free_all_descriptors(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) EXPORT_SYMBOL_GPL(usb_assign_descriptors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) void usb_free_all_descriptors(struct usb_function *f)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) usb_free_descriptors(f->fs_descriptors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) f->fs_descriptors = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) usb_free_descriptors(f->hs_descriptors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) f->hs_descriptors = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) usb_free_descriptors(f->ss_descriptors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) f->ss_descriptors = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) usb_free_descriptors(f->ssp_descriptors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) f->ssp_descriptors = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) EXPORT_SYMBOL_GPL(usb_free_all_descriptors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) struct usb_descriptor_header *usb_otg_descriptor_alloc(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) struct usb_gadget *gadget)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) struct usb_descriptor_header *otg_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) unsigned length = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) if (gadget->otg_caps && (gadget->otg_caps->otg_rev >= 0x0200))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) length = sizeof(struct usb_otg20_descriptor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) length = sizeof(struct usb_otg_descriptor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) otg_desc = kzalloc(length, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) return otg_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) EXPORT_SYMBOL_GPL(usb_otg_descriptor_alloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) int usb_otg_descriptor_init(struct usb_gadget *gadget,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) struct usb_descriptor_header *otg_desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) struct usb_otg_descriptor *otg1x_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) struct usb_otg20_descriptor *otg20_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) struct usb_otg_caps *otg_caps = gadget->otg_caps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) u8 otg_attributes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) if (!otg_desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) if (otg_caps && otg_caps->otg_rev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) if (otg_caps->hnp_support)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) otg_attributes |= USB_OTG_HNP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) if (otg_caps->srp_support)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) otg_attributes |= USB_OTG_SRP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) if (otg_caps->adp_support && (otg_caps->otg_rev >= 0x0200))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) otg_attributes |= USB_OTG_ADP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) otg_attributes = USB_OTG_SRP | USB_OTG_HNP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) if (otg_caps && (otg_caps->otg_rev >= 0x0200)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) otg20_desc = (struct usb_otg20_descriptor *)otg_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) otg20_desc->bLength = sizeof(struct usb_otg20_descriptor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) otg20_desc->bDescriptorType = USB_DT_OTG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) otg20_desc->bmAttributes = otg_attributes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) otg20_desc->bcdOTG = cpu_to_le16(otg_caps->otg_rev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) otg1x_desc = (struct usb_otg_descriptor *)otg_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) otg1x_desc->bLength = sizeof(struct usb_otg_descriptor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) otg1x_desc->bDescriptorType = USB_DT_OTG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) otg1x_desc->bmAttributes = otg_attributes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) EXPORT_SYMBOL_GPL(usb_otg_descriptor_init);