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