1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * usb/gadget/config.c -- simplify building config descriptors
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Copyright (C) 2003 David Brownell
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * SPDX-License-Identifier: GPL-2.0+
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * Ported to U-Boot by: Thomas Smits <ts.smits@gmail.com> and
9*4882a593Smuzhiyun * Remy Bohmer <linux@bohmer.net>
10*4882a593Smuzhiyun */
11*4882a593Smuzhiyun
12*4882a593Smuzhiyun #include <common.h>
13*4882a593Smuzhiyun #include <asm/unaligned.h>
14*4882a593Smuzhiyun #include <linux/errno.h>
15*4882a593Smuzhiyun #include <linux/list.h>
16*4882a593Smuzhiyun #include <linux/string.h>
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun #include <linux/usb/ch9.h>
19*4882a593Smuzhiyun #include <linux/usb/gadget.h>
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun /**
23*4882a593Smuzhiyun * usb_descriptor_fillbuf - fill buffer with descriptors
24*4882a593Smuzhiyun * @buf: Buffer to be filled
25*4882a593Smuzhiyun * @buflen: Size of buf
26*4882a593Smuzhiyun * @src: Array of descriptor pointers, terminated by null pointer.
27*4882a593Smuzhiyun *
28*4882a593Smuzhiyun * Copies descriptors into the buffer, returning the length or a
29*4882a593Smuzhiyun * negative error code if they can't all be copied. Useful when
30*4882a593Smuzhiyun * assembling descriptors for an associated set of interfaces used
31*4882a593Smuzhiyun * as part of configuring a composite device; or in other cases where
32*4882a593Smuzhiyun * sets of descriptors need to be marshaled.
33*4882a593Smuzhiyun */
34*4882a593Smuzhiyun int
usb_descriptor_fillbuf(void * buf,unsigned buflen,const struct usb_descriptor_header ** src)35*4882a593Smuzhiyun usb_descriptor_fillbuf(void *buf, unsigned buflen,
36*4882a593Smuzhiyun const struct usb_descriptor_header **src)
37*4882a593Smuzhiyun {
38*4882a593Smuzhiyun u8 *dest = buf;
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun if (!src)
41*4882a593Smuzhiyun return -EINVAL;
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun /* fill buffer from src[] until null descriptor ptr */
44*4882a593Smuzhiyun for (; NULL != *src; src++) {
45*4882a593Smuzhiyun unsigned len = (*src)->bLength;
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun if (len > buflen)
48*4882a593Smuzhiyun return -EINVAL;
49*4882a593Smuzhiyun memcpy(dest, *src, len);
50*4882a593Smuzhiyun buflen -= len;
51*4882a593Smuzhiyun dest += len;
52*4882a593Smuzhiyun }
53*4882a593Smuzhiyun return dest - (u8 *)buf;
54*4882a593Smuzhiyun }
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun /**
58*4882a593Smuzhiyun * usb_gadget_config_buf - builts a complete configuration descriptor
59*4882a593Smuzhiyun * @config: Header for the descriptor, including characteristics such
60*4882a593Smuzhiyun * as power requirements and number of interfaces.
61*4882a593Smuzhiyun * @desc: Null-terminated vector of pointers to the descriptors (interface,
62*4882a593Smuzhiyun * endpoint, etc) defining all functions in this device configuration.
63*4882a593Smuzhiyun * @buf: Buffer for the resulting configuration descriptor.
64*4882a593Smuzhiyun * @length: Length of buffer. If this is not big enough to hold the
65*4882a593Smuzhiyun * entire configuration descriptor, an error code will be returned.
66*4882a593Smuzhiyun *
67*4882a593Smuzhiyun * This copies descriptors into the response buffer, building a descriptor
68*4882a593Smuzhiyun * for that configuration. It returns the buffer length or a negative
69*4882a593Smuzhiyun * status code. The config.wTotalLength field is set to match the length
70*4882a593Smuzhiyun * of the result, but other descriptor fields (including power usage and
71*4882a593Smuzhiyun * interface count) must be set by the caller.
72*4882a593Smuzhiyun *
73*4882a593Smuzhiyun * Gadget drivers could use this when constructing a config descriptor
74*4882a593Smuzhiyun * in response to USB_REQ_GET_DESCRIPTOR. They will need to patch the
75*4882a593Smuzhiyun * resulting bDescriptorType value if USB_DT_OTHER_SPEED_CONFIG is needed.
76*4882a593Smuzhiyun */
usb_gadget_config_buf(const struct usb_config_descriptor * config,void * buf,unsigned length,const struct usb_descriptor_header ** desc)77*4882a593Smuzhiyun int usb_gadget_config_buf(
78*4882a593Smuzhiyun const struct usb_config_descriptor *config,
79*4882a593Smuzhiyun void *buf,
80*4882a593Smuzhiyun unsigned length,
81*4882a593Smuzhiyun const struct usb_descriptor_header **desc
82*4882a593Smuzhiyun )
83*4882a593Smuzhiyun {
84*4882a593Smuzhiyun struct usb_config_descriptor *cp = buf;
85*4882a593Smuzhiyun int len;
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun /* config descriptor first */
88*4882a593Smuzhiyun if (length < USB_DT_CONFIG_SIZE || !desc)
89*4882a593Smuzhiyun return -EINVAL;
90*4882a593Smuzhiyun /* config need not be aligned */
91*4882a593Smuzhiyun memcpy(cp, config, sizeof(*cp));
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun /* then interface/endpoint/class/vendor/... */
94*4882a593Smuzhiyun len = usb_descriptor_fillbuf(USB_DT_CONFIG_SIZE + (u8 *)buf,
95*4882a593Smuzhiyun length - USB_DT_CONFIG_SIZE, desc);
96*4882a593Smuzhiyun if (len < 0)
97*4882a593Smuzhiyun return len;
98*4882a593Smuzhiyun len += USB_DT_CONFIG_SIZE;
99*4882a593Smuzhiyun if (len > 0xffff)
100*4882a593Smuzhiyun return -EINVAL;
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun /* patch up the config descriptor */
103*4882a593Smuzhiyun cp->bLength = USB_DT_CONFIG_SIZE;
104*4882a593Smuzhiyun cp->bDescriptorType = USB_DT_CONFIG;
105*4882a593Smuzhiyun put_unaligned_le16(len, &cp->wTotalLength);
106*4882a593Smuzhiyun cp->bmAttributes |= USB_CONFIG_ATT_ONE;
107*4882a593Smuzhiyun return len;
108*4882a593Smuzhiyun }
109