blob: c52d8b62a0312442ee9fb94e4d0dcf98fc952ee6 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Vijayavardhan Vennapusa95b650a2012-01-18 12:54:01 +053022/**
23 * usb_find_descriptor_fillbuf - fill buffer with the requested descriptor
24 * @buf: Buffer to be filled
25 * @buflen: Size of buf
26 * @src: Array of descriptor pointers, terminated by null pointer.
27 * @desc_type: bDescriptorType field of the requested descriptor.
28 *
29 * Copies the requested descriptor into the buffer, returning the length
30 * or a negative error code if it is not found or can't be copied. Useful
31 * when DT_OTG descriptor is requested.
32 */
33int
34usb_find_descriptor_fillbuf(void *buf, unsigned buflen,
35 const struct usb_descriptor_header **src, u8 desc_type)
36{
37 if (!src)
38 return -EINVAL;
39
40 for (; NULL != *src; src++) {
41 unsigned len;
42
43 if ((*src)->bDescriptorType != desc_type)
44 continue;
45
46 len = (*src)->bLength;
47 if (len > buflen)
48 return -EINVAL;
49
50 memcpy(buf, *src, len);
51 return len;
52 }
53
54 return -ENOENT;
55}
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57/**
58 * usb_descriptor_fillbuf - fill buffer with descriptors
59 * @buf: Buffer to be filled
60 * @buflen: Size of buf
61 * @src: Array of descriptor pointers, terminated by null pointer.
62 *
63 * Copies descriptors into the buffer, returning the length or a
64 * negative error code if they can't all be copied. Useful when
65 * assembling descriptors for an associated set of interfaces used
66 * as part of configuring a composite device; or in other cases where
67 * sets of descriptors need to be marshaled.
68 */
69int
70usb_descriptor_fillbuf(void *buf, unsigned buflen,
71 const struct usb_descriptor_header **src)
72{
73 u8 *dest = buf;
74
75 if (!src)
76 return -EINVAL;
77
78 /* fill buffer from src[] until null descriptor ptr */
David Brownella9475222007-07-30 12:31:07 -070079 for (; NULL != *src; src++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 unsigned len = (*src)->bLength;
81
82 if (len > buflen)
83 return -EINVAL;
84 memcpy(dest, *src, len);
85 buflen -= len;
86 dest += len;
87 }
88 return dest - (u8 *)buf;
89}
90
91
92/**
93 * usb_gadget_config_buf - builts a complete configuration descriptor
94 * @config: Header for the descriptor, including characteristics such
95 * as power requirements and number of interfaces.
96 * @desc: Null-terminated vector of pointers to the descriptors (interface,
97 * endpoint, etc) defining all functions in this device configuration.
98 * @buf: Buffer for the resulting configuration descriptor.
99 * @length: Length of buffer. If this is not big enough to hold the
100 * entire configuration descriptor, an error code will be returned.
101 *
102 * This copies descriptors into the response buffer, building a descriptor
103 * for that configuration. It returns the buffer length or a negative
104 * status code. The config.wTotalLength field is set to match the length
105 * of the result, but other descriptor fields (including power usage and
106 * interface count) must be set by the caller.
107 *
108 * Gadget drivers could use this when constructing a config descriptor
109 * in response to USB_REQ_GET_DESCRIPTOR. They will need to patch the
110 * resulting bDescriptorType value if USB_DT_OTHER_SPEED_CONFIG is needed.
111 */
112int usb_gadget_config_buf(
113 const struct usb_config_descriptor *config,
114 void *buf,
115 unsigned length,
116 const struct usb_descriptor_header **desc
117)
118{
119 struct usb_config_descriptor *cp = buf;
120 int len;
121
122 /* config descriptor first */
123 if (length < USB_DT_CONFIG_SIZE || !desc)
124 return -EINVAL;
David Brownella4c39c42008-06-19 17:52:25 -0700125 *cp = *config;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127 /* then interface/endpoint/class/vendor/... */
128 len = usb_descriptor_fillbuf(USB_DT_CONFIG_SIZE + (u8*)buf,
129 length - USB_DT_CONFIG_SIZE, desc);
130 if (len < 0)
131 return len;
132 len += USB_DT_CONFIG_SIZE;
133 if (len > 0xffff)
134 return -EINVAL;
135
136 /* patch up the config descriptor */
137 cp->bLength = USB_DT_CONFIG_SIZE;
138 cp->bDescriptorType = USB_DT_CONFIG;
139 cp->wTotalLength = cpu_to_le16(len);
140 cp->bmAttributes |= USB_CONFIG_ATT_ONE;
141 return len;
142}
143
David Brownella4c39c42008-06-19 17:52:25 -0700144/**
145 * usb_copy_descriptors - copy a vector of USB descriptors
146 * @src: null-terminated vector to copy
147 * Context: initialization code, which may sleep
148 *
149 * This makes a copy of a vector of USB descriptors. Its primary use
150 * is to support usb_function objects which can have multiple copies,
151 * each needing different descriptors. Functions may have static
152 * tables of descriptors, which are used as templates and customized
153 * with identifiers (for interfaces, strings, endpoints, and more)
154 * as needed by a given function instance.
155 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200156struct usb_descriptor_header **
David Brownella4c39c42008-06-19 17:52:25 -0700157usb_copy_descriptors(struct usb_descriptor_header **src)
158{
159 struct usb_descriptor_header **tmp;
160 unsigned bytes;
161 unsigned n_desc;
162 void *mem;
163 struct usb_descriptor_header **ret;
164
165 /* count descriptors and their sizes; then add vector size */
166 for (bytes = 0, n_desc = 0, tmp = src; *tmp; tmp++, n_desc++)
167 bytes += (*tmp)->bLength;
168 bytes += (n_desc + 1) * sizeof(*tmp);
169
170 mem = kmalloc(bytes, GFP_KERNEL);
171 if (!mem)
172 return NULL;
173
174 /* fill in pointers starting at "tmp",
175 * to descriptors copied starting at "mem";
176 * and return "ret"
177 */
178 tmp = mem;
179 ret = mem;
180 mem += (n_desc + 1) * sizeof(*tmp);
181 while (*src) {
182 memcpy(mem, *src, (*src)->bLength);
183 *tmp = mem;
184 tmp++;
185 mem += (*src)->bLength;
186 src++;
187 }
188 *tmp = NULL;
189
190 return ret;
191}
192