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