2 * usb/gadget/config.c -- simplify building config descriptors
4 * Copyright (C) 2003 David Brownell
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <asm/errno.h>
26 #include <linux/list.h>
27 #include <linux/string.h>
29 #include <linux/usb/ch9.h>
30 #include <linux/usb/gadget.h>
34 * usb_descriptor_fillbuf - fill buffer with descriptors
35 * @buf: Buffer to be filled
36 * @buflen: Size of buf
37 * @src: Array of descriptor pointers, terminated by null pointer.
39 * Copies descriptors into the buffer, returning the length or a
40 * negative error code if they can't all be copied. Useful when
41 * assembling descriptors for an associated set of interfaces used
42 * as part of configuring a composite device; or in other cases where
43 * sets of descriptors need to be marshaled.
46 usb_descriptor_fillbuf(void *buf, unsigned buflen,
47 const struct usb_descriptor_header **src)
54 /* fill buffer from src[] until null descriptor ptr */
55 for (; NULL != *src; src++) {
56 unsigned len = (*src)->bLength;
60 memcpy(dest, *src, len);
64 return dest - (u8 *)buf;
69 * usb_gadget_config_buf - builts a complete configuration descriptor
70 * @config: Header for the descriptor, including characteristics such
71 * as power requirements and number of interfaces.
72 * @desc: Null-terminated vector of pointers to the descriptors (interface,
73 * endpoint, etc) defining all functions in this device configuration.
74 * @buf: Buffer for the resulting configuration descriptor.
75 * @length: Length of buffer. If this is not big enough to hold the
76 * entire configuration descriptor, an error code will be returned.
78 * This copies descriptors into the response buffer, building a descriptor
79 * for that configuration. It returns the buffer length or a negative
80 * status code. The config.wTotalLength field is set to match the length
81 * of the result, but other descriptor fields (including power usage and
82 * interface count) must be set by the caller.
84 * Gadget drivers could use this when constructing a config descriptor
85 * in response to USB_REQ_GET_DESCRIPTOR. They will need to patch the
86 * resulting bDescriptorType value if USB_DT_OTHER_SPEED_CONFIG is needed.
88 int usb_gadget_config_buf(
89 const struct usb_config_descriptor *config,
92 const struct usb_descriptor_header **desc
95 struct usb_config_descriptor *cp = buf;
98 /* config descriptor first */
99 if (length < USB_DT_CONFIG_SIZE || !desc)
103 /* then interface/endpoint/class/vendor/... */
104 len = usb_descriptor_fillbuf(USB_DT_CONFIG_SIZE + (u8 *)buf,
105 length - USB_DT_CONFIG_SIZE, desc);
108 len += USB_DT_CONFIG_SIZE;
112 /* patch up the config descriptor */
113 cp->bLength = USB_DT_CONFIG_SIZE;
114 cp->bDescriptorType = USB_DT_CONFIG;
115 cp->wTotalLength = cpu_to_le16(len);
116 cp->bmAttributes |= USB_CONFIG_ATT_ONE;