libusb10_desc.c revision 203775
1/* $FreeBSD: head/lib/libusb/libusb10_desc.c 203775 2010-02-11 08:34:41Z wkoszek $ */
2/*-
3 * Copyright (c) 2009 Sylvestre Gallon. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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
24 * SUCH DAMAGE.
25 */
26
27#include <stdlib.h>
28#include <stdio.h>
29#include <sys/queue.h>
30
31#include "libusb20.h"
32#include "libusb20_desc.h"
33#include "libusb20_int.h"
34#include "libusb.h"
35#include "libusb10.h"
36
37#define	N_ALIGN(n) (-((-(n)) & (-8UL)))
38
39/* USB descriptors */
40
41int
42libusb_get_device_descriptor(libusb_device *dev,
43    struct libusb_device_descriptor *desc)
44{
45	struct LIBUSB20_DEVICE_DESC_DECODED *pdesc;
46	struct libusb20_device *pdev;
47
48	if ((dev == NULL) || (desc == NULL))
49		return (LIBUSB_ERROR_INVALID_PARAM);
50
51	pdev = dev->os_priv;
52	pdesc = libusb20_dev_get_device_desc(pdev);
53
54	desc->bLength = pdesc->bLength;
55	desc->bDescriptorType = pdesc->bDescriptorType;
56	desc->bcdUSB = pdesc->bcdUSB;
57	desc->bDeviceClass = pdesc->bDeviceClass;
58	desc->bDeviceSubClass = pdesc->bDeviceSubClass;
59	desc->bDeviceProtocol = pdesc->bDeviceProtocol;
60	desc->bMaxPacketSize0 = pdesc->bMaxPacketSize0;
61	desc->idVendor = pdesc->idVendor;
62	desc->idProduct = pdesc->idProduct;
63	desc->bcdDevice = pdesc->bcdDevice;
64	desc->iManufacturer = pdesc->iManufacturer;
65	desc->iProduct = pdesc->iProduct;
66	desc->iSerialNumber = pdesc->iSerialNumber;
67	desc->bNumConfigurations = pdesc->bNumConfigurations;
68
69	return (0);
70}
71
72int
73libusb_get_active_config_descriptor(libusb_device *dev,
74    struct libusb_config_descriptor **config)
75{
76	struct libusb20_device *pdev;
77	uint8_t config_index;
78
79	pdev = dev->os_priv;
80	config_index = libusb20_dev_get_config_index(pdev);
81
82	return (libusb_get_config_descriptor(dev, config_index, config));
83}
84
85int
86libusb_get_config_descriptor(libusb_device *dev, uint8_t config_index,
87    struct libusb_config_descriptor **config)
88{
89	struct libusb20_device *pdev;
90	struct libusb20_config *pconf;
91	struct libusb20_interface *pinf;
92	struct libusb20_endpoint *pend;
93	struct libusb_config_descriptor *pconfd;
94	struct libusb_interface_descriptor *ifd;
95	struct libusb_endpoint_descriptor *endd;
96	uint8_t *pextra;
97	uint16_t nextra;
98	uint8_t nif;
99	uint8_t nep;
100	uint8_t nalt;
101	uint8_t i;
102	uint8_t j;
103	uint8_t k;
104
105	if (dev == NULL || config == NULL)
106		return (LIBUSB_ERROR_INVALID_PARAM);
107
108	*config = NULL;
109
110	pdev = dev->os_priv;
111	pconf = libusb20_dev_alloc_config(pdev, config_index);
112
113	if (pconf == NULL)
114		return (LIBUSB_ERROR_NOT_FOUND);
115
116	nalt = nif = pconf->num_interface;
117	nep = 0;
118	nextra = N_ALIGN(pconf->extra.len);
119
120	for (i = 0; i < nif; i++) {
121
122		pinf = pconf->interface + i;
123		nextra += N_ALIGN(pinf->extra.len);
124		nep += pinf->num_endpoints;
125		k = pinf->num_endpoints;
126		pend = pinf->endpoints;
127		while (k--) {
128			nextra += N_ALIGN(pend->extra.len);
129			pend++;
130		}
131
132		j = pinf->num_altsetting;
133		nalt += pinf->num_altsetting;
134		pinf = pinf->altsetting;
135		while (j--) {
136			nextra += N_ALIGN(pinf->extra.len);
137			nep += pinf->num_endpoints;
138			k = pinf->num_endpoints;
139			pend = pinf->endpoints;
140			while (k--) {
141				nextra += N_ALIGN(pend->extra.len);
142				pend++;
143			}
144			pinf++;
145		}
146	}
147
148	nextra = nextra +
149	    (1 * sizeof(libusb_config_descriptor)) +
150	    (nif * sizeof(libusb_interface)) +
151	    (nalt * sizeof(libusb_interface_descriptor)) +
152	    (nep * sizeof(libusb_endpoint_descriptor));
153
154	nextra = N_ALIGN(nextra);
155
156	pconfd = malloc(nextra);
157
158	if (pconfd == NULL) {
159		free(pconf);
160		return (LIBUSB_ERROR_NO_MEM);
161	}
162	/* make sure memory is initialised */
163	memset(pconfd, 0, nextra);
164
165	pconfd->interface = (libusb_interface *) (pconfd + 1);
166
167	ifd = (libusb_interface_descriptor *) (pconfd->interface + nif);
168	endd = (libusb_endpoint_descriptor *) (ifd + nalt);
169	pextra = (uint8_t *)(endd + nep);
170
171	/* fill in config descriptor */
172
173	pconfd->bLength = pconf->desc.bLength;
174	pconfd->bDescriptorType = pconf->desc.bDescriptorType;
175	pconfd->wTotalLength = pconf->desc.wTotalLength;
176	pconfd->bNumInterfaces = pconf->desc.bNumInterfaces;
177	pconfd->bConfigurationValue = pconf->desc.bConfigurationValue;
178	pconfd->iConfiguration = pconf->desc.iConfiguration;
179	pconfd->bmAttributes = pconf->desc.bmAttributes;
180	pconfd->MaxPower = pconf->desc.bMaxPower;
181
182	if (pconf->extra.len != 0) {
183		pconfd->extra_length = pconf->extra.len;
184		pconfd->extra = pextra;
185		memcpy(pextra, pconf->extra.ptr, pconfd->extra_length);
186		pextra += N_ALIGN(pconfd->extra_length);
187	}
188	/* setup all interface and endpoint pointers */
189
190	for (i = 0; i < nif; i++) {
191
192		pconfd->interface[i].altsetting = ifd;
193		ifd->endpoint = endd;
194		endd += pconf->interface[i].num_endpoints;
195		ifd++;
196
197		for (j = 0; j < pconf->interface[i].num_altsetting; j++) {
198			ifd->endpoint = endd;
199			endd += pconf->interface[i].altsetting[j].num_endpoints;
200			ifd++;
201		}
202	}
203
204	/* fill in all interface and endpoint data */
205
206	for (i = 0; i < nif; i++) {
207		pinf = &pconf->interface[i];
208		pconfd->interface[i].num_altsetting = pinf->num_altsetting + 1;
209		for (j = 0; j < pconfd->interface[i].num_altsetting; j++) {
210			if (j != 0)
211				pinf = &pconf->interface[i].altsetting[j - 1];
212			ifd = &pconfd->interface[i].altsetting[j];
213			ifd->bLength = pinf->desc.bLength;
214			ifd->bDescriptorType = pinf->desc.bDescriptorType;
215			ifd->bInterfaceNumber = pinf->desc.bInterfaceNumber;
216			ifd->bAlternateSetting = pinf->desc.bAlternateSetting;
217			ifd->bNumEndpoints = pinf->desc.bNumEndpoints;
218			ifd->bInterfaceClass = pinf->desc.bInterfaceClass;
219			ifd->bInterfaceSubClass = pinf->desc.bInterfaceSubClass;
220			ifd->bInterfaceProtocol = pinf->desc.bInterfaceProtocol;
221			ifd->iInterface = pinf->desc.iInterface;
222			if (pinf->extra.len != 0) {
223				ifd->extra_length = pinf->extra.len;
224				ifd->extra = pextra;
225				memcpy(pextra, pinf->extra.ptr, pinf->extra.len);
226				pextra += N_ALIGN(pinf->extra.len);
227			}
228			for (k = 0; k < pinf->num_endpoints; k++) {
229				pend = &pinf->endpoints[k];
230				endd = &ifd->endpoint[k];
231				endd->bLength = pend->desc.bLength;
232				endd->bDescriptorType = pend->desc.bDescriptorType;
233				endd->bEndpointAddress = pend->desc.bEndpointAddress;
234				endd->bmAttributes = pend->desc.bmAttributes;
235				endd->wMaxPacketSize = pend->desc.wMaxPacketSize;
236				endd->bInterval = pend->desc.bInterval;
237				endd->bRefresh = pend->desc.bRefresh;
238				endd->bSynchAddress = pend->desc.bSynchAddress;
239				if (pend->extra.len != 0) {
240					endd->extra_length = pend->extra.len;
241					endd->extra = pextra;
242					memcpy(pextra, pend->extra.ptr, pend->extra.len);
243					pextra += N_ALIGN(pend->extra.len);
244				}
245			}
246		}
247	}
248
249	free(pconf);
250
251	*config = pconfd;
252
253	return (0);			/* success */
254}
255
256int
257libusb_get_config_descriptor_by_value(libusb_device *dev,
258    uint8_t bConfigurationValue, struct libusb_config_descriptor **config)
259{
260	struct LIBUSB20_DEVICE_DESC_DECODED *pdesc;
261	struct libusb20_device *pdev;
262	int i;
263	int err;
264
265	if (dev == NULL || config == NULL)
266		return (LIBUSB_ERROR_INVALID_PARAM);
267
268	pdev = dev->os_priv;
269	pdesc = libusb20_dev_get_device_desc(pdev);
270
271	for (i = 0; i < pdesc->bNumConfigurations; i++) {
272		err = libusb_get_config_descriptor(dev, i, config);
273		if (err)
274			return (err);
275
276		if ((*config)->bConfigurationValue == bConfigurationValue)
277			return (0);	/* success */
278
279		libusb_free_config_descriptor(*config);
280	}
281
282	*config = NULL;
283
284	return (LIBUSB_ERROR_NOT_FOUND);
285}
286
287void
288libusb_free_config_descriptor(struct libusb_config_descriptor *config)
289{
290	free(config);
291}
292
293int
294libusb_get_string_descriptor_ascii(libusb_device_handle *pdev,
295    uint8_t desc_index, unsigned char *data, int length)
296{
297	if (pdev == NULL || data == NULL || length < 1)
298		return (LIBUSB20_ERROR_INVALID_PARAM);
299
300	/* put some default data into the destination buffer */
301	data[0] = 0;
302
303	if (libusb20_dev_req_string_simple_sync(pdev, desc_index,
304	    data, length) == 0)
305		return (strlen(data));
306
307	return (LIBUSB_ERROR_OTHER);
308}
309
310int
311libusb_get_descriptor(libusb_device_handle * devh, uint8_t desc_type,
312    uint8_t desc_index, uint8_t *data, int length)
313{
314	return (libusb_control_transfer(devh, LIBUSB_ENDPOINT_IN,
315	    LIBUSB_REQUEST_GET_DESCRIPTOR, (desc_type << 8) | desc_index, 0, data,
316	    length, 1000));
317}
318