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