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