Linux Audio

Check our new training course

Loading...
v3.1
 
  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/slab.h>
 23#include <linux/kernel.h>
 
 24#include <linux/list.h>
 25#include <linux/string.h>
 26#include <linux/device.h>
 27
 28#include <linux/usb/ch9.h>
 29#include <linux/usb/gadget.h>
 30
 
 31
 32/**
 33 * usb_descriptor_fillbuf - fill buffer with descriptors
 34 * @buf: Buffer to be filled
 35 * @buflen: Size of buf
 36 * @src: Array of descriptor pointers, terminated by null pointer.
 37 *
 38 * Copies descriptors into the buffer, returning the length or a
 39 * negative error code if they can't all be copied.  Useful when
 40 * assembling descriptors for an associated set of interfaces used
 41 * as part of configuring a composite device; or in other cases where
 42 * sets of descriptors need to be marshaled.
 43 */
 44int
 45usb_descriptor_fillbuf(void *buf, unsigned buflen,
 46		const struct usb_descriptor_header **src)
 47{
 48	u8	*dest = buf;
 49
 50	if (!src)
 51		return -EINVAL;
 52
 53	/* fill buffer from src[] until null descriptor ptr */
 54	for (; NULL != *src; src++) {
 55		unsigned		len = (*src)->bLength;
 56
 57		if (len > buflen)
 58			return -EINVAL;
 59		memcpy(dest, *src, len);
 60		buflen -= len;
 61		dest += len;
 62	}
 63	return dest - (u8 *)buf;
 64}
 65
 66
 67/**
 68 * usb_gadget_config_buf - builts a complete configuration descriptor
 69 * @config: Header for the descriptor, including characteristics such
 70 *	as power requirements and number of interfaces.
 71 * @desc: Null-terminated vector of pointers to the descriptors (interface,
 72 *	endpoint, etc) defining all functions in this device configuration.
 73 * @buf: Buffer for the resulting configuration descriptor.
 74 * @length: Length of buffer.  If this is not big enough to hold the
 75 *	entire configuration descriptor, an error code will be returned.
 76 *
 77 * This copies descriptors into the response buffer, building a descriptor
 78 * for that configuration.  It returns the buffer length or a negative
 79 * status code.  The config.wTotalLength field is set to match the length
 80 * of the result, but other descriptor fields (including power usage and
 81 * interface count) must be set by the caller.
 82 *
 83 * Gadget drivers could use this when constructing a config descriptor
 84 * in response to USB_REQ_GET_DESCRIPTOR.  They will need to patch the
 85 * resulting bDescriptorType value if USB_DT_OTHER_SPEED_CONFIG is needed.
 86 */
 87int usb_gadget_config_buf(
 88	const struct usb_config_descriptor	*config,
 89	void					*buf,
 90	unsigned				length,
 91	const struct usb_descriptor_header	**desc
 92)
 93{
 94	struct usb_config_descriptor		*cp = buf;
 95	int					len;
 96
 97	/* config descriptor first */
 98	if (length < USB_DT_CONFIG_SIZE || !desc)
 99		return -EINVAL;
100	*cp = *config;
101
102	/* then interface/endpoint/class/vendor/... */
103	len = usb_descriptor_fillbuf(USB_DT_CONFIG_SIZE + (u8*)buf,
104			length - USB_DT_CONFIG_SIZE, desc);
105	if (len < 0)
106		return len;
107	len += USB_DT_CONFIG_SIZE;
108	if (len > 0xffff)
109		return -EINVAL;
110
111	/* patch up the config descriptor */
112	cp->bLength = USB_DT_CONFIG_SIZE;
113	cp->bDescriptorType = USB_DT_CONFIG;
114	cp->wTotalLength = cpu_to_le16(len);
115	cp->bmAttributes |= USB_CONFIG_ATT_ONE;
116	return len;
117}
 
118
119/**
120 * usb_copy_descriptors - copy a vector of USB descriptors
121 * @src: null-terminated vector to copy
122 * Context: initialization code, which may sleep
123 *
124 * This makes a copy of a vector of USB descriptors.  Its primary use
125 * is to support usb_function objects which can have multiple copies,
126 * each needing different descriptors.  Functions may have static
127 * tables of descriptors, which are used as templates and customized
128 * with identifiers (for interfaces, strings, endpoints, and more)
129 * as needed by a given function instance.
130 */
131struct usb_descriptor_header **
132usb_copy_descriptors(struct usb_descriptor_header **src)
133{
134	struct usb_descriptor_header **tmp;
135	unsigned bytes;
136	unsigned n_desc;
137	void *mem;
138	struct usb_descriptor_header **ret;
139
140	/* count descriptors and their sizes; then add vector size */
141	for (bytes = 0, n_desc = 0, tmp = src; *tmp; tmp++, n_desc++)
142		bytes += (*tmp)->bLength;
143	bytes += (n_desc + 1) * sizeof(*tmp);
144
145	mem = kmalloc(bytes, GFP_KERNEL);
146	if (!mem)
147		return NULL;
148
149	/* fill in pointers starting at "tmp",
150	 * to descriptors copied starting at "mem";
151	 * and return "ret"
152	 */
153	tmp = mem;
154	ret = mem;
155	mem += (n_desc + 1) * sizeof(*tmp);
156	while (*src) {
157		memcpy(mem, *src, (*src)->bLength);
158		*tmp = mem;
159		tmp++;
160		mem += (*src)->bLength;
161		src++;
162	}
163	*tmp = NULL;
164
165	return ret;
166}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
167
v6.8
  1// SPDX-License-Identifier: GPL-2.0+
  2/*
  3 * usb/gadget/config.c -- simplify building config descriptors
  4 *
  5 * Copyright (C) 2003 David Brownell
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  6 */
  7
  8#include <linux/errno.h>
  9#include <linux/slab.h>
 10#include <linux/kernel.h>
 11#include <linux/module.h>
 12#include <linux/list.h>
 13#include <linux/string.h>
 14#include <linux/device.h>
 15
 16#include <linux/usb/ch9.h>
 17#include <linux/usb/gadget.h>
 18#include <linux/usb/composite.h>
 19#include <linux/usb/otg.h>
 20
 21/**
 22 * usb_descriptor_fillbuf - fill buffer with descriptors
 23 * @buf: Buffer to be filled
 24 * @buflen: Size of buf
 25 * @src: Array of descriptor pointers, terminated by null pointer.
 26 *
 27 * Copies descriptors into the buffer, returning the length or a
 28 * negative error code if they can't all be copied.  Useful when
 29 * assembling descriptors for an associated set of interfaces used
 30 * as part of configuring a composite device; or in other cases where
 31 * sets of descriptors need to be marshaled.
 32 */
 33int
 34usb_descriptor_fillbuf(void *buf, unsigned buflen,
 35		const struct usb_descriptor_header **src)
 36{
 37	u8	*dest = buf;
 38
 39	if (!src)
 40		return -EINVAL;
 41
 42	/* fill buffer from src[] until null descriptor ptr */
 43	for (; NULL != *src; src++) {
 44		unsigned		len = (*src)->bLength;
 45
 46		if (len > buflen)
 47			return -EINVAL;
 48		memcpy(dest, *src, len);
 49		buflen -= len;
 50		dest += len;
 51	}
 52	return dest - (u8 *)buf;
 53}
 54EXPORT_SYMBOL_GPL(usb_descriptor_fillbuf);
 55
 56/**
 57 * usb_gadget_config_buf - builts a complete configuration descriptor
 58 * @config: Header for the descriptor, including characteristics such
 59 *	as power requirements and number of interfaces.
 60 * @desc: Null-terminated vector of pointers to the descriptors (interface,
 61 *	endpoint, etc) defining all functions in this device configuration.
 62 * @buf: Buffer for the resulting configuration descriptor.
 63 * @length: Length of buffer.  If this is not big enough to hold the
 64 *	entire configuration descriptor, an error code will be returned.
 65 *
 66 * This copies descriptors into the response buffer, building a descriptor
 67 * for that configuration.  It returns the buffer length or a negative
 68 * status code.  The config.wTotalLength field is set to match the length
 69 * of the result, but other descriptor fields (including power usage and
 70 * interface count) must be set by the caller.
 71 *
 72 * Gadget drivers could use this when constructing a config descriptor
 73 * in response to USB_REQ_GET_DESCRIPTOR.  They will need to patch the
 74 * resulting bDescriptorType value if USB_DT_OTHER_SPEED_CONFIG is needed.
 75 */
 76int usb_gadget_config_buf(
 77	const struct usb_config_descriptor	*config,
 78	void					*buf,
 79	unsigned				length,
 80	const struct usb_descriptor_header	**desc
 81)
 82{
 83	struct usb_config_descriptor		*cp = buf;
 84	int					len;
 85
 86	/* config descriptor first */
 87	if (length < USB_DT_CONFIG_SIZE || !desc)
 88		return -EINVAL;
 89	*cp = *config;
 90
 91	/* then interface/endpoint/class/vendor/... */
 92	len = usb_descriptor_fillbuf(USB_DT_CONFIG_SIZE + (u8 *)buf,
 93			length - USB_DT_CONFIG_SIZE, desc);
 94	if (len < 0)
 95		return len;
 96	len += USB_DT_CONFIG_SIZE;
 97	if (len > 0xffff)
 98		return -EINVAL;
 99
100	/* patch up the config descriptor */
101	cp->bLength = USB_DT_CONFIG_SIZE;
102	cp->bDescriptorType = USB_DT_CONFIG;
103	cp->wTotalLength = cpu_to_le16(len);
104	cp->bmAttributes |= USB_CONFIG_ATT_ONE;
105	return len;
106}
107EXPORT_SYMBOL_GPL(usb_gadget_config_buf);
108
109/**
110 * usb_copy_descriptors - copy a vector of USB descriptors
111 * @src: null-terminated vector to copy
112 * Context: initialization code, which may sleep
113 *
114 * This makes a copy of a vector of USB descriptors.  Its primary use
115 * is to support usb_function objects which can have multiple copies,
116 * each needing different descriptors.  Functions may have static
117 * tables of descriptors, which are used as templates and customized
118 * with identifiers (for interfaces, strings, endpoints, and more)
119 * as needed by a given function instance.
120 */
121struct usb_descriptor_header **
122usb_copy_descriptors(struct usb_descriptor_header **src)
123{
124	struct usb_descriptor_header **tmp;
125	unsigned bytes;
126	unsigned n_desc;
127	void *mem;
128	struct usb_descriptor_header **ret;
129
130	/* count descriptors and their sizes; then add vector size */
131	for (bytes = 0, n_desc = 0, tmp = src; *tmp; tmp++, n_desc++)
132		bytes += (*tmp)->bLength;
133	bytes += (n_desc + 1) * sizeof(*tmp);
134
135	mem = kmalloc(bytes, GFP_KERNEL);
136	if (!mem)
137		return NULL;
138
139	/* fill in pointers starting at "tmp",
140	 * to descriptors copied starting at "mem";
141	 * and return "ret"
142	 */
143	tmp = mem;
144	ret = mem;
145	mem += (n_desc + 1) * sizeof(*tmp);
146	while (*src) {
147		memcpy(mem, *src, (*src)->bLength);
148		*tmp = mem;
149		tmp++;
150		mem += (*src)->bLength;
151		src++;
152	}
153	*tmp = NULL;
154
155	return ret;
156}
157EXPORT_SYMBOL_GPL(usb_copy_descriptors);
158
159int usb_assign_descriptors(struct usb_function *f,
160		struct usb_descriptor_header **fs,
161		struct usb_descriptor_header **hs,
162		struct usb_descriptor_header **ss,
163		struct usb_descriptor_header **ssp)
164{
165	/* super-speed-plus descriptor falls back to super-speed one,
166	 * if such a descriptor was provided, thus avoiding a NULL
167	 * pointer dereference if a 5gbps capable gadget is used with
168	 * a 10gbps capable config (device port + cable + host port)
169	 */
170	if (!ssp)
171		ssp = ss;
172
173	if (fs) {
174		f->fs_descriptors = usb_copy_descriptors(fs);
175		if (!f->fs_descriptors)
176			goto err;
177	}
178	if (hs) {
179		f->hs_descriptors = usb_copy_descriptors(hs);
180		if (!f->hs_descriptors)
181			goto err;
182	}
183	if (ss) {
184		f->ss_descriptors = usb_copy_descriptors(ss);
185		if (!f->ss_descriptors)
186			goto err;
187	}
188	if (ssp) {
189		f->ssp_descriptors = usb_copy_descriptors(ssp);
190		if (!f->ssp_descriptors)
191			goto err;
192	}
193	return 0;
194err:
195	usb_free_all_descriptors(f);
196	return -ENOMEM;
197}
198EXPORT_SYMBOL_GPL(usb_assign_descriptors);
199
200void usb_free_all_descriptors(struct usb_function *f)
201{
202	usb_free_descriptors(f->fs_descriptors);
203	f->fs_descriptors = NULL;
204	usb_free_descriptors(f->hs_descriptors);
205	f->hs_descriptors = NULL;
206	usb_free_descriptors(f->ss_descriptors);
207	f->ss_descriptors = NULL;
208	usb_free_descriptors(f->ssp_descriptors);
209	f->ssp_descriptors = NULL;
210}
211EXPORT_SYMBOL_GPL(usb_free_all_descriptors);
212
213struct usb_descriptor_header *usb_otg_descriptor_alloc(
214				struct usb_gadget *gadget)
215{
216	struct usb_descriptor_header *otg_desc;
217	unsigned length = 0;
218
219	if (gadget->otg_caps && (gadget->otg_caps->otg_rev >= 0x0200))
220		length = sizeof(struct usb_otg20_descriptor);
221	else
222		length = sizeof(struct usb_otg_descriptor);
223
224	otg_desc = kzalloc(length, GFP_KERNEL);
225	return otg_desc;
226}
227EXPORT_SYMBOL_GPL(usb_otg_descriptor_alloc);
228
229int usb_otg_descriptor_init(struct usb_gadget *gadget,
230		struct usb_descriptor_header *otg_desc)
231{
232	struct usb_otg_descriptor *otg1x_desc;
233	struct usb_otg20_descriptor *otg20_desc;
234	struct usb_otg_caps *otg_caps = gadget->otg_caps;
235	u8 otg_attributes = 0;
236
237	if (!otg_desc)
238		return -EINVAL;
239
240	if (otg_caps && otg_caps->otg_rev) {
241		if (otg_caps->hnp_support)
242			otg_attributes |= USB_OTG_HNP;
243		if (otg_caps->srp_support)
244			otg_attributes |= USB_OTG_SRP;
245		if (otg_caps->adp_support && (otg_caps->otg_rev >= 0x0200))
246			otg_attributes |= USB_OTG_ADP;
247	} else {
248		otg_attributes = USB_OTG_SRP | USB_OTG_HNP;
249	}
250
251	if (otg_caps && (otg_caps->otg_rev >= 0x0200)) {
252		otg20_desc = (struct usb_otg20_descriptor *)otg_desc;
253		otg20_desc->bLength = sizeof(struct usb_otg20_descriptor);
254		otg20_desc->bDescriptorType = USB_DT_OTG;
255		otg20_desc->bmAttributes = otg_attributes;
256		otg20_desc->bcdOTG = cpu_to_le16(otg_caps->otg_rev);
257	} else {
258		otg1x_desc = (struct usb_otg_descriptor *)otg_desc;
259		otg1x_desc->bLength = sizeof(struct usb_otg_descriptor);
260		otg1x_desc->bDescriptorType = USB_DT_OTG;
261		otg1x_desc->bmAttributes = otg_attributes;
262	}
263
264	return 0;
265}
266EXPORT_SYMBOL_GPL(usb_otg_descriptor_init);