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