Linux Audio

Check our new training course

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