blob: 384332ca16d22c0e9dbbf921e46a993e6a84bee5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/kernel.h>
24#include <linux/list.h>
25#include <linux/string.h>
26#include <linux/device.h>
27
David Brownell5f848132006-12-16 15:34:53 -080028#include <linux/usb/ch9.h>
David Brownell9454a572007-10-04 18:05:17 -070029#include <linux/usb/gadget.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Vijayavardhan Vennapusa95b650a2012-01-18 12:54:01 +053031/**
32 * usb_find_descriptor_fillbuf - fill buffer with the requested descriptor
33 * @buf: Buffer to be filled
34 * @buflen: Size of buf
35 * @src: Array of descriptor pointers, terminated by null pointer.
36 * @desc_type: bDescriptorType field of the requested descriptor.
37 *
38 * Copies the requested descriptor into the buffer, returning the length
39 * or a negative error code if it is not found or can't be copied. Useful
40 * when DT_OTG descriptor is requested.
41 */
42int
43usb_find_descriptor_fillbuf(void *buf, unsigned buflen,
44 const struct usb_descriptor_header **src, u8 desc_type)
45{
46 if (!src)
47 return -EINVAL;
48
49 for (; NULL != *src; src++) {
50 unsigned len;
51
52 if ((*src)->bDescriptorType != desc_type)
53 continue;
54
55 len = (*src)->bLength;
56 if (len > buflen)
57 return -EINVAL;
58
59 memcpy(buf, *src, len);
60 return len;
61 }
62
63 return -ENOENT;
64}
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66/**
67 * usb_descriptor_fillbuf - fill buffer with descriptors
68 * @buf: Buffer to be filled
69 * @buflen: Size of buf
70 * @src: Array of descriptor pointers, terminated by null pointer.
71 *
72 * Copies descriptors into the buffer, returning the length or a
73 * negative error code if they can't all be copied. Useful when
74 * assembling descriptors for an associated set of interfaces used
75 * as part of configuring a composite device; or in other cases where
76 * sets of descriptors need to be marshaled.
77 */
78int
79usb_descriptor_fillbuf(void *buf, unsigned buflen,
80 const struct usb_descriptor_header **src)
81{
82 u8 *dest = buf;
83
84 if (!src)
85 return -EINVAL;
86
87 /* fill buffer from src[] until null descriptor ptr */
David Brownella9475222007-07-30 12:31:07 -070088 for (; NULL != *src; src++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 unsigned len = (*src)->bLength;
90
91 if (len > buflen)
92 return -EINVAL;
93 memcpy(dest, *src, len);
94 buflen -= len;
95 dest += len;
96 }
97 return dest - (u8 *)buf;
98}
99
100
101/**
102 * usb_gadget_config_buf - builts a complete configuration descriptor
103 * @config: Header for the descriptor, including characteristics such
104 * as power requirements and number of interfaces.
105 * @desc: Null-terminated vector of pointers to the descriptors (interface,
106 * endpoint, etc) defining all functions in this device configuration.
107 * @buf: Buffer for the resulting configuration descriptor.
108 * @length: Length of buffer. If this is not big enough to hold the
109 * entire configuration descriptor, an error code will be returned.
110 *
111 * This copies descriptors into the response buffer, building a descriptor
112 * for that configuration. It returns the buffer length or a negative
113 * status code. The config.wTotalLength field is set to match the length
114 * of the result, but other descriptor fields (including power usage and
115 * interface count) must be set by the caller.
116 *
117 * Gadget drivers could use this when constructing a config descriptor
118 * in response to USB_REQ_GET_DESCRIPTOR. They will need to patch the
119 * resulting bDescriptorType value if USB_DT_OTHER_SPEED_CONFIG is needed.
120 */
121int usb_gadget_config_buf(
122 const struct usb_config_descriptor *config,
123 void *buf,
124 unsigned length,
125 const struct usb_descriptor_header **desc
126)
127{
128 struct usb_config_descriptor *cp = buf;
129 int len;
130
131 /* config descriptor first */
132 if (length < USB_DT_CONFIG_SIZE || !desc)
133 return -EINVAL;
David Brownella4c39c42008-06-19 17:52:25 -0700134 *cp = *config;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136 /* then interface/endpoint/class/vendor/... */
137 len = usb_descriptor_fillbuf(USB_DT_CONFIG_SIZE + (u8*)buf,
138 length - USB_DT_CONFIG_SIZE, desc);
139 if (len < 0)
140 return len;
141 len += USB_DT_CONFIG_SIZE;
142 if (len > 0xffff)
143 return -EINVAL;
144
145 /* patch up the config descriptor */
146 cp->bLength = USB_DT_CONFIG_SIZE;
147 cp->bDescriptorType = USB_DT_CONFIG;
148 cp->wTotalLength = cpu_to_le16(len);
149 cp->bmAttributes |= USB_CONFIG_ATT_ONE;
150 return len;
151}
152
David Brownella4c39c42008-06-19 17:52:25 -0700153/**
154 * usb_copy_descriptors - copy a vector of USB descriptors
155 * @src: null-terminated vector to copy
156 * Context: initialization code, which may sleep
157 *
158 * This makes a copy of a vector of USB descriptors. Its primary use
159 * is to support usb_function objects which can have multiple copies,
160 * each needing different descriptors. Functions may have static
161 * tables of descriptors, which are used as templates and customized
162 * with identifiers (for interfaces, strings, endpoints, and more)
163 * as needed by a given function instance.
164 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200165struct usb_descriptor_header **
David Brownella4c39c42008-06-19 17:52:25 -0700166usb_copy_descriptors(struct usb_descriptor_header **src)
167{
168 struct usb_descriptor_header **tmp;
169 unsigned bytes;
170 unsigned n_desc;
171 void *mem;
172 struct usb_descriptor_header **ret;
173
174 /* count descriptors and their sizes; then add vector size */
175 for (bytes = 0, n_desc = 0, tmp = src; *tmp; tmp++, n_desc++)
176 bytes += (*tmp)->bLength;
177 bytes += (n_desc + 1) * sizeof(*tmp);
178
179 mem = kmalloc(bytes, GFP_KERNEL);
180 if (!mem)
181 return NULL;
182
183 /* fill in pointers starting at "tmp",
184 * to descriptors copied starting at "mem";
185 * and return "ret"
186 */
187 tmp = mem;
188 ret = mem;
189 mem += (n_desc + 1) * sizeof(*tmp);
190 while (*src) {
191 memcpy(mem, *src, (*src)->bLength);
192 *tmp = mem;
193 tmp++;
194 mem += (*src)->bLength;
195 src++;
196 }
197 *tmp = NULL;
198
199 return ret;
200}
David Brownac5d1542012-02-06 10:37:22 -0800201
202/**
203 * usb_find_endpoint - find a copy of an endpoint descriptor
204 * @src: original vector of descriptors
205 * @copy: copy of @src
206 * @match: endpoint descriptor found in @src
207 *
208 * This returns the copy of the @match descriptor made for @copy. Its
209 * intended use is to help remembering the endpoint descriptor to use
210 * when enabling a given endpoint.
211 */
212struct usb_endpoint_descriptor *
213usb_find_endpoint(
214 struct usb_descriptor_header **src,
215 struct usb_descriptor_header **copy,
216 struct usb_endpoint_descriptor *match
217)
218{
219 while (*src) {
220 if (*src == (void *) match)
221 return (void *)*copy;
222 src++;
223 copy++;
224 }
225 return NULL;
226}