3 * Copyright (c) 2009 Sylvestre Gallon. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/queue.h>
33 #include "libusb20_desc.h"
34 #include "libusb20_int.h"
41 libusb_get_device_descriptor(libusb_device *dev,
42 struct libusb_device_descriptor *desc)
44 struct LIBUSB20_DEVICE_DESC_DECODED *pdesc;
45 struct libusb20_device *pdev;
47 if ((dev == NULL) || (desc == NULL))
48 return (LIBUSB_ERROR_INVALID_PARAM);
51 pdesc = libusb20_dev_get_device_desc(pdev);
53 desc->bLength = pdesc->bLength;
54 desc->bDescriptorType = pdesc->bDescriptorType;
55 desc->bcdUSB = pdesc->bcdUSB;
56 desc->bDeviceClass = pdesc->bDeviceClass;
57 desc->bDeviceSubClass = pdesc->bDeviceSubClass;
58 desc->bDeviceProtocol = pdesc->bDeviceProtocol;
59 desc->bMaxPacketSize0 = pdesc->bMaxPacketSize0;
60 desc->idVendor = pdesc->idVendor;
61 desc->idProduct = pdesc->idProduct;
62 desc->bcdDevice = pdesc->bcdDevice;
63 desc->iManufacturer = pdesc->iManufacturer;
64 desc->iProduct = pdesc->iProduct;
65 desc->iSerialNumber = pdesc->iSerialNumber;
66 desc->bNumConfigurations = pdesc->bNumConfigurations;
72 libusb_get_active_config_descriptor(libusb_device *dev,
73 struct libusb_config_descriptor **config)
75 struct libusb20_device *pdev;
79 config_index = libusb20_dev_get_config_index(pdev);
81 return (libusb_get_config_descriptor(dev, config_index, config));
85 libusb_get_config_descriptor(libusb_device *dev, uint8_t config_index,
86 struct libusb_config_descriptor **config)
88 struct libusb20_device *pdev;
89 struct libusb20_config *pconf;
90 struct libusb20_interface *pinf;
91 struct libusb20_endpoint *pend;
92 struct libusb_config_descriptor *pconfd;
93 struct libusb_interface_descriptor *ifd;
94 struct libusb_endpoint_descriptor *endd;
104 if (dev == NULL || config == NULL)
105 return (LIBUSB_ERROR_INVALID_PARAM);
110 pconf = libusb20_dev_alloc_config(pdev, config_index);
113 return (LIBUSB_ERROR_NOT_FOUND);
115 nalt = nif = pconf->num_interface;
117 nextra = pconf->extra.len;
119 for (i = 0; i < nif; i++) {
121 pinf = pconf->interface + i;
122 nextra += pinf->extra.len;
123 nep += pinf->num_endpoints;
124 k = pinf->num_endpoints;
125 pend = pinf->endpoints;
127 nextra += pend->extra.len;
131 j = pinf->num_altsetting;
132 nalt += pinf->num_altsetting;
133 pinf = pinf->altsetting;
135 nextra += pinf->extra.len;
136 nep += pinf->num_endpoints;
137 k = pinf->num_endpoints;
138 pend = pinf->endpoints;
140 nextra += pend->extra.len;
148 (1 * sizeof(libusb_config_descriptor)) +
149 (nif * sizeof(libusb_interface)) +
150 (nalt * sizeof(libusb_interface_descriptor)) +
151 (nep * sizeof(libusb_endpoint_descriptor));
153 pconfd = malloc(nextra);
155 if (pconfd == NULL) {
157 return (LIBUSB_ERROR_NO_MEM);
159 /* make sure memory is clean */
160 memset(pconfd, 0, nextra);
162 pconfd->interface = (libusb_interface *) (pconfd +
163 sizeof(libusb_config_descriptor));
165 ifd = (libusb_interface_descriptor *) (pconfd->interface + nif);
166 endd = (libusb_endpoint_descriptor *) (ifd + nalt);
167 pextra = (uint8_t *)(endd + nep);
169 /* fill in config descriptor */
171 pconfd->bLength = pconf->desc.bLength;
172 pconfd->bDescriptorType = pconf->desc.bDescriptorType;
173 pconfd->wTotalLength = pconf->desc.wTotalLength;
174 pconfd->bNumInterfaces = pconf->desc.bNumInterfaces;
175 pconfd->bConfigurationValue = pconf->desc.bConfigurationValue;
176 pconfd->iConfiguration = pconf->desc.iConfiguration;
177 pconfd->bmAttributes = pconf->desc.bmAttributes;
178 pconfd->MaxPower = pconf->desc.bMaxPower;
180 if (pconf->extra.len != 0) {
181 pconfd->extra_length = pconf->extra.len;
182 pconfd->extra = pextra;
183 memcpy(pextra, pconf->extra.ptr, pconfd->extra_length);
184 pextra += pconfd->extra_length;
186 /* setup all interface and endpoint pointers */
188 for (i = 0; i < nif; i++) {
190 pconfd->interface[i].altsetting = ifd;
191 ifd->endpoint = endd;
192 endd += pconf->interface[i].num_endpoints;
195 for (j = 0; j < pconf->interface[i].num_altsetting; j++) {
196 ifd->endpoint = endd;
197 endd += pconf->interface[i].altsetting[j].num_endpoints;
202 /* fill in all interface and endpoint data */
204 for (i = 0; i < nif; i++) {
205 pinf = &pconf->interface[i];
206 pconfd->interface[i].num_altsetting = pinf->num_altsetting + 1;
207 for (j = 0; j < pconfd->interface[i].num_altsetting; j++) {
209 pinf = &pconf->interface[i].altsetting[j - 1];
210 ifd = &pconfd->interface[i].altsetting[j];
211 ifd->bLength = pinf->desc.bLength;
212 ifd->bDescriptorType = pinf->desc.bDescriptorType;
213 ifd->bInterfaceNumber = pinf->desc.bInterfaceNumber;
214 ifd->bAlternateSetting = pinf->desc.bAlternateSetting;
215 ifd->bNumEndpoints = pinf->desc.bNumEndpoints;
216 ifd->bInterfaceClass = pinf->desc.bInterfaceClass;
217 ifd->bInterfaceSubClass = pinf->desc.bInterfaceSubClass;
218 ifd->bInterfaceProtocol = pinf->desc.bInterfaceProtocol;
219 ifd->iInterface = pinf->desc.iInterface;
220 if (pinf->extra.len != 0) {
221 ifd->extra_length = pinf->extra.len;
223 memcpy(pextra, pinf->extra.ptr, pinf->extra.len);
224 pextra += pinf->extra.len;
226 for (k = 0; k < pinf->num_endpoints; k++) {
227 pend = &pinf->endpoints[k];
228 endd = &ifd->endpoint[k];
229 endd->bLength = pend->desc.bLength;
230 endd->bDescriptorType = pend->desc.bDescriptorType;
231 endd->bEndpointAddress = pend->desc.bEndpointAddress;
232 endd->bmAttributes = pend->desc.bmAttributes;
233 endd->wMaxPacketSize = pend->desc.wMaxPacketSize;
234 endd->bInterval = pend->desc.bInterval;
235 endd->bRefresh = pend->desc.bRefresh;
236 endd->bSynchAddress = pend->desc.bSynchAddress;
237 if (pend->extra.len != 0) {
238 endd->extra_length = pend->extra.len;
239 endd->extra = pextra;
240 memcpy(pextra, pend->extra.ptr, pend->extra.len);
241 pextra += pend->extra.len;
251 return (0); /* success */
255 libusb_get_config_descriptor_by_value(libusb_device *dev,
256 uint8_t bConfigurationValue, struct libusb_config_descriptor **config)
258 struct LIBUSB20_DEVICE_DESC_DECODED *pdesc;
259 struct libusb20_device *pdev;
263 if (dev == NULL || config == NULL)
264 return (LIBUSB_ERROR_INVALID_PARAM);
267 pdesc = libusb20_dev_get_device_desc(pdev);
269 for (i = 0; i < pdesc->bNumConfigurations; i++) {
270 err = libusb_get_config_descriptor(dev, i, config);
274 if ((*config)->bConfigurationValue == bConfigurationValue)
275 return (0); /* success */
277 libusb_free_config_descriptor(*config);
282 return (LIBUSB_ERROR_NOT_FOUND);
286 libusb_free_config_descriptor(struct libusb_config_descriptor *config)
292 libusb_get_string_descriptor_ascii(libusb_device_handle *pdev,
293 uint8_t desc_index, unsigned char *data, int length)
295 if (pdev == NULL || data == NULL || length < 1)
296 return (LIBUSB20_ERROR_INVALID_PARAM);
298 /* put some default data into the destination buffer */
301 if (libusb20_dev_req_string_simple_sync(pdev, desc_index,
303 return (strlen(data));
305 return (LIBUSB_ERROR_OTHER);