xref: /freebsd/sys/dev/usb/template/usb_template.c (revision 8833442863c16647c9245827a4df3e16c8209063)
102ac6454SAndrew Thompson /* $FreeBSD$ */
202ac6454SAndrew Thompson /*-
302ac6454SAndrew Thompson  * Copyright (c) 2007 Hans Petter Selasky. All rights reserved.
402ac6454SAndrew Thompson  *
502ac6454SAndrew Thompson  * Redistribution and use in source and binary forms, with or without
602ac6454SAndrew Thompson  * modification, are permitted provided that the following conditions
702ac6454SAndrew Thompson  * are met:
802ac6454SAndrew Thompson  * 1. Redistributions of source code must retain the above copyright
902ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer.
1002ac6454SAndrew Thompson  * 2. Redistributions in binary form must reproduce the above copyright
1102ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer in the
1202ac6454SAndrew Thompson  *    documentation and/or other materials provided with the distribution.
1302ac6454SAndrew Thompson  *
1402ac6454SAndrew Thompson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1502ac6454SAndrew Thompson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1602ac6454SAndrew Thompson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1702ac6454SAndrew Thompson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1802ac6454SAndrew Thompson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1902ac6454SAndrew Thompson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2002ac6454SAndrew Thompson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2102ac6454SAndrew Thompson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2202ac6454SAndrew Thompson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2302ac6454SAndrew Thompson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2402ac6454SAndrew Thompson  * SUCH DAMAGE.
2502ac6454SAndrew Thompson  */
2602ac6454SAndrew Thompson 
2702ac6454SAndrew Thompson /*
2802ac6454SAndrew Thompson  * This file contains sub-routines to build up USB descriptors from
2902ac6454SAndrew Thompson  * USB templates.
3002ac6454SAndrew Thompson  */
3102ac6454SAndrew Thompson 
32ed6d949aSAndrew Thompson #include <sys/stdint.h>
33ed6d949aSAndrew Thompson #include <sys/stddef.h>
34ed6d949aSAndrew Thompson #include <sys/param.h>
35ed6d949aSAndrew Thompson #include <sys/queue.h>
36ed6d949aSAndrew Thompson #include <sys/types.h>
37ed6d949aSAndrew Thompson #include <sys/systm.h>
38ed6d949aSAndrew Thompson #include <sys/kernel.h>
39ed6d949aSAndrew Thompson #include <sys/bus.h>
40ed6d949aSAndrew Thompson #include <sys/module.h>
41ed6d949aSAndrew Thompson #include <sys/lock.h>
42ed6d949aSAndrew Thompson #include <sys/mutex.h>
43ed6d949aSAndrew Thompson #include <sys/condvar.h>
44ed6d949aSAndrew Thompson #include <sys/sysctl.h>
45ed6d949aSAndrew Thompson #include <sys/sx.h>
46ed6d949aSAndrew Thompson #include <sys/unistd.h>
47ed6d949aSAndrew Thompson #include <sys/callout.h>
48ed6d949aSAndrew Thompson #include <sys/malloc.h>
49ed6d949aSAndrew Thompson #include <sys/priv.h>
50ed6d949aSAndrew Thompson 
5102ac6454SAndrew Thompson #include <dev/usb/usb.h>
52ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h>
53ed6d949aSAndrew Thompson #include <dev/usb/usbdi_util.h>
54ed6d949aSAndrew Thompson #include "usbdevs.h"
55ed6d949aSAndrew Thompson 
5602ac6454SAndrew Thompson #include <dev/usb/usb_cdc.h>
5702ac6454SAndrew Thompson #include <dev/usb/usb_core.h>
58ed6d949aSAndrew Thompson #include <dev/usb/usb_dynamic.h>
5902ac6454SAndrew Thompson #include <dev/usb/usb_busdma.h>
6002ac6454SAndrew Thompson #include <dev/usb/usb_process.h>
6102ac6454SAndrew Thompson #include <dev/usb/usb_device.h>
62ed6d949aSAndrew Thompson 
63ed6d949aSAndrew Thompson #define	USB_DEBUG_VAR usb_debug
64ed6d949aSAndrew Thompson #include <dev/usb/usb_debug.h>
6502ac6454SAndrew Thompson 
6602ac6454SAndrew Thompson #include <dev/usb/usb_controller.h>
6702ac6454SAndrew Thompson #include <dev/usb/usb_bus.h>
6802ac6454SAndrew Thompson #include <dev/usb/template/usb_template.h>
6902ac6454SAndrew Thompson 
7002ac6454SAndrew Thompson MODULE_DEPEND(usb_template, usb, 1, 1, 1);
7102ac6454SAndrew Thompson MODULE_VERSION(usb_template, 1);
7202ac6454SAndrew Thompson 
7302ac6454SAndrew Thompson /* function prototypes */
7402ac6454SAndrew Thompson 
75a593f6b8SAndrew Thompson static void	usb_make_raw_desc(struct usb_temp_setup *, const uint8_t *);
76a593f6b8SAndrew Thompson static void	usb_make_endpoint_desc(struct usb_temp_setup *,
77760bc48eSAndrew Thompson 		    const struct usb_temp_endpoint_desc *);
78a593f6b8SAndrew Thompson static void	usb_make_interface_desc(struct usb_temp_setup *,
79760bc48eSAndrew Thompson 		    const struct usb_temp_interface_desc *);
80a593f6b8SAndrew Thompson static void	usb_make_config_desc(struct usb_temp_setup *,
81760bc48eSAndrew Thompson 		    const struct usb_temp_config_desc *);
82a593f6b8SAndrew Thompson static void	usb_make_device_desc(struct usb_temp_setup *,
83760bc48eSAndrew Thompson 		    const struct usb_temp_device_desc *);
84a593f6b8SAndrew Thompson static uint8_t	usb_hw_ep_match(const struct usb_hw_ep_profile *, uint8_t,
8502ac6454SAndrew Thompson 		    uint8_t);
86a593f6b8SAndrew Thompson static uint8_t	usb_hw_ep_find_match(struct usb_hw_ep_scratch *,
87760bc48eSAndrew Thompson 		    struct usb_hw_ep_scratch_sub *, uint8_t);
88a593f6b8SAndrew Thompson static uint8_t	usb_hw_ep_get_needs(struct usb_hw_ep_scratch *, uint8_t,
8902ac6454SAndrew Thompson 		    uint8_t);
90a593f6b8SAndrew Thompson static usb_error_t usb_hw_ep_resolve(struct usb_device *,
91760bc48eSAndrew Thompson 		    struct usb_descriptor *);
92a593f6b8SAndrew Thompson static const struct usb_temp_device_desc *usb_temp_get_tdd(struct usb_device *);
93a593f6b8SAndrew Thompson static void	*usb_temp_get_device_desc(struct usb_device *);
94a593f6b8SAndrew Thompson static void	*usb_temp_get_qualifier_desc(struct usb_device *);
95a593f6b8SAndrew Thompson static void	*usb_temp_get_config_desc(struct usb_device *, uint16_t *,
9602ac6454SAndrew Thompson 		    uint8_t);
97a593f6b8SAndrew Thompson static const void *usb_temp_get_string_desc(struct usb_device *, uint16_t,
9802ac6454SAndrew Thompson 		    uint8_t);
99a593f6b8SAndrew Thompson static const void *usb_temp_get_vendor_desc(struct usb_device *,
1009a8e0122SAndrew Thompson 		    const struct usb_device_request *, uint16_t *plen);
101a593f6b8SAndrew Thompson static const void *usb_temp_get_hub_desc(struct usb_device *);
102a593f6b8SAndrew Thompson static usb_error_t usb_temp_get_desc(struct usb_device *,
103760bc48eSAndrew Thompson 		    struct usb_device_request *, const void **, uint16_t *);
104a593f6b8SAndrew Thompson static usb_error_t usb_temp_setup_by_index(struct usb_device *,
10502ac6454SAndrew Thompson 		    uint16_t index);
106a593f6b8SAndrew Thompson static void	usb_temp_init(void *);
10702ac6454SAndrew Thompson 
10802ac6454SAndrew Thompson /*------------------------------------------------------------------------*
109a593f6b8SAndrew Thompson  *	usb_make_raw_desc
11002ac6454SAndrew Thompson  *
11102ac6454SAndrew Thompson  * This function will insert a raw USB descriptor into the generated
11202ac6454SAndrew Thompson  * USB configuration.
11302ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
11402ac6454SAndrew Thompson static void
115a593f6b8SAndrew Thompson usb_make_raw_desc(struct usb_temp_setup *temp,
11602ac6454SAndrew Thompson     const uint8_t *raw)
11702ac6454SAndrew Thompson {
11802ac6454SAndrew Thompson 	void *dst;
11902ac6454SAndrew Thompson 	uint8_t len;
12002ac6454SAndrew Thompson 
12102ac6454SAndrew Thompson 	/*
12202ac6454SAndrew Thompson          * The first byte of any USB descriptor gives the length.
12302ac6454SAndrew Thompson          */
12402ac6454SAndrew Thompson 	if (raw) {
12502ac6454SAndrew Thompson 		len = raw[0];
12602ac6454SAndrew Thompson 		if (temp->buf) {
12702ac6454SAndrew Thompson 			dst = USB_ADD_BYTES(temp->buf, temp->size);
128*88334428SHans Petter Selasky 			memcpy(dst, raw, len);
12902ac6454SAndrew Thompson 
13002ac6454SAndrew Thompson 			/* check if we have got a CDC union descriptor */
13102ac6454SAndrew Thompson 
132760bc48eSAndrew Thompson 			if ((raw[0] >= sizeof(struct usb_cdc_union_descriptor)) &&
13302ac6454SAndrew Thompson 			    (raw[1] == UDESC_CS_INTERFACE) &&
13402ac6454SAndrew Thompson 			    (raw[2] == UDESCSUB_CDC_UNION)) {
135760bc48eSAndrew Thompson 				struct usb_cdc_union_descriptor *ud = (void *)dst;
13602ac6454SAndrew Thompson 
13702ac6454SAndrew Thompson 				/* update the interface numbers */
13802ac6454SAndrew Thompson 
13902ac6454SAndrew Thompson 				ud->bMasterInterface +=
14002ac6454SAndrew Thompson 				    temp->bInterfaceNumber;
14102ac6454SAndrew Thompson 				ud->bSlaveInterface[0] +=
14202ac6454SAndrew Thompson 				    temp->bInterfaceNumber;
14302ac6454SAndrew Thompson 			}
14402ac6454SAndrew Thompson 		}
14502ac6454SAndrew Thompson 		temp->size += len;
14602ac6454SAndrew Thompson 	}
14702ac6454SAndrew Thompson }
14802ac6454SAndrew Thompson 
14902ac6454SAndrew Thompson /*------------------------------------------------------------------------*
150a593f6b8SAndrew Thompson  *	usb_make_endpoint_desc
15102ac6454SAndrew Thompson  *
15202ac6454SAndrew Thompson  * This function will generate an USB endpoint descriptor from the
15302ac6454SAndrew Thompson  * given USB template endpoint descriptor, which will be inserted into
15402ac6454SAndrew Thompson  * the USB configuration.
15502ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
15602ac6454SAndrew Thompson static void
157a593f6b8SAndrew Thompson usb_make_endpoint_desc(struct usb_temp_setup *temp,
158760bc48eSAndrew Thompson     const struct usb_temp_endpoint_desc *ted)
15902ac6454SAndrew Thompson {
160760bc48eSAndrew Thompson 	struct usb_endpoint_descriptor *ed;
16102ac6454SAndrew Thompson 	const void **rd;
16202ac6454SAndrew Thompson 	uint16_t old_size;
16302ac6454SAndrew Thompson 	uint16_t mps;
1646a268418SAndrew Thompson 	uint8_t ea;			/* Endpoint Address */
1656a268418SAndrew Thompson 	uint8_t et;			/* Endpiont Type */
16602ac6454SAndrew Thompson 
16702ac6454SAndrew Thompson 	/* Reserve memory */
16802ac6454SAndrew Thompson 	old_size = temp->size;
1696a268418SAndrew Thompson 
1706a268418SAndrew Thompson 	ea = (ted->bEndpointAddress & (UE_ADDR | UE_DIR_IN | UE_DIR_OUT));
1716a268418SAndrew Thompson 	et = (ted->bmAttributes & UE_XFERTYPE);
1726a268418SAndrew Thompson 
1736a268418SAndrew Thompson 	if (et == UE_ISOCHRONOUS) {
1746a268418SAndrew Thompson 		/* account for extra byte fields */
1756a268418SAndrew Thompson 		temp->size += sizeof(*ed) + 2;
1766a268418SAndrew Thompson 	} else {
17702ac6454SAndrew Thompson 		temp->size += sizeof(*ed);
1786a268418SAndrew Thompson 	}
17902ac6454SAndrew Thompson 
18002ac6454SAndrew Thompson 	/* Scan all Raw Descriptors first */
18102ac6454SAndrew Thompson 	rd = ted->ppRawDesc;
18202ac6454SAndrew Thompson 	if (rd) {
18302ac6454SAndrew Thompson 		while (*rd) {
184a593f6b8SAndrew Thompson 			usb_make_raw_desc(temp, *rd);
18502ac6454SAndrew Thompson 			rd++;
18602ac6454SAndrew Thompson 		}
18702ac6454SAndrew Thompson 	}
18802ac6454SAndrew Thompson 	if (ted->pPacketSize == NULL) {
18902ac6454SAndrew Thompson 		/* not initialized */
19002ac6454SAndrew Thompson 		temp->err = USB_ERR_INVAL;
19102ac6454SAndrew Thompson 		return;
19202ac6454SAndrew Thompson 	}
1938d2dd5ddSAndrew Thompson 	mps = ted->pPacketSize->mps[temp->usb_speed];
19402ac6454SAndrew Thompson 	if (mps == 0) {
19502ac6454SAndrew Thompson 		/* not initialized */
19602ac6454SAndrew Thompson 		temp->err = USB_ERR_INVAL;
19702ac6454SAndrew Thompson 		return;
19802ac6454SAndrew Thompson 	} else if (mps == UE_ZERO_MPS) {
19902ac6454SAndrew Thompson 		/* escape for Zero Max Packet Size */
20002ac6454SAndrew Thompson 		mps = 0;
20102ac6454SAndrew Thompson 	}
20202ac6454SAndrew Thompson 
20302ac6454SAndrew Thompson 	/*
20402ac6454SAndrew Thompson 	 * Fill out the real USB endpoint descriptor
20502ac6454SAndrew Thompson 	 * in case there is a buffer present:
20602ac6454SAndrew Thompson 	 */
20702ac6454SAndrew Thompson 	if (temp->buf) {
20802ac6454SAndrew Thompson 		ed = USB_ADD_BYTES(temp->buf, old_size);
2096a268418SAndrew Thompson 		if (et == UE_ISOCHRONOUS)
2106a268418SAndrew Thompson 			ed->bLength = sizeof(*ed) + 2;
2116a268418SAndrew Thompson 		else
21202ac6454SAndrew Thompson 			ed->bLength = sizeof(*ed);
21302ac6454SAndrew Thompson 		ed->bDescriptorType = UDESC_ENDPOINT;
21402ac6454SAndrew Thompson 		ed->bEndpointAddress = ea;
21502ac6454SAndrew Thompson 		ed->bmAttributes = ted->bmAttributes;
21602ac6454SAndrew Thompson 		USETW(ed->wMaxPacketSize, mps);
21702ac6454SAndrew Thompson 
21802ac6454SAndrew Thompson 		/* setup bInterval parameter */
21902ac6454SAndrew Thompson 
22002ac6454SAndrew Thompson 		if (ted->pIntervals &&
2218d2dd5ddSAndrew Thompson 		    ted->pIntervals->bInterval[temp->usb_speed]) {
22202ac6454SAndrew Thompson 			ed->bInterval =
2238d2dd5ddSAndrew Thompson 			    ted->pIntervals->bInterval[temp->usb_speed];
22402ac6454SAndrew Thompson 		} else {
22502ac6454SAndrew Thompson 			switch (et) {
22602ac6454SAndrew Thompson 			case UE_BULK:
22702ac6454SAndrew Thompson 			case UE_CONTROL:
22802ac6454SAndrew Thompson 				ed->bInterval = 0;	/* not used */
22902ac6454SAndrew Thompson 				break;
23002ac6454SAndrew Thompson 			case UE_INTERRUPT:
2318d2dd5ddSAndrew Thompson 				switch (temp->usb_speed) {
23202ac6454SAndrew Thompson 				case USB_SPEED_LOW:
23302ac6454SAndrew Thompson 				case USB_SPEED_FULL:
23402ac6454SAndrew Thompson 					ed->bInterval = 1;	/* 1 ms */
23502ac6454SAndrew Thompson 					break;
23602ac6454SAndrew Thompson 				default:
23702ac6454SAndrew Thompson 					ed->bInterval = 8;	/* 8*125 us */
23802ac6454SAndrew Thompson 					break;
23902ac6454SAndrew Thompson 				}
24002ac6454SAndrew Thompson 				break;
24102ac6454SAndrew Thompson 			default:	/* UE_ISOCHRONOUS */
2428d2dd5ddSAndrew Thompson 				switch (temp->usb_speed) {
24302ac6454SAndrew Thompson 				case USB_SPEED_LOW:
24402ac6454SAndrew Thompson 				case USB_SPEED_FULL:
24502ac6454SAndrew Thompson 					ed->bInterval = 1;	/* 1 ms */
24602ac6454SAndrew Thompson 					break;
24702ac6454SAndrew Thompson 				default:
24802ac6454SAndrew Thompson 					ed->bInterval = 1;	/* 125 us */
24902ac6454SAndrew Thompson 					break;
25002ac6454SAndrew Thompson 				}
25102ac6454SAndrew Thompson 				break;
25202ac6454SAndrew Thompson 			}
25302ac6454SAndrew Thompson 		}
25402ac6454SAndrew Thompson 	}
25502ac6454SAndrew Thompson 	temp->bNumEndpoints++;
25602ac6454SAndrew Thompson }
25702ac6454SAndrew Thompson 
25802ac6454SAndrew Thompson /*------------------------------------------------------------------------*
259a593f6b8SAndrew Thompson  *	usb_make_interface_desc
26002ac6454SAndrew Thompson  *
26102ac6454SAndrew Thompson  * This function will generate an USB interface descriptor from the
26202ac6454SAndrew Thompson  * given USB template interface descriptor, which will be inserted
26302ac6454SAndrew Thompson  * into the USB configuration.
26402ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
26502ac6454SAndrew Thompson static void
266a593f6b8SAndrew Thompson usb_make_interface_desc(struct usb_temp_setup *temp,
267760bc48eSAndrew Thompson     const struct usb_temp_interface_desc *tid)
26802ac6454SAndrew Thompson {
269760bc48eSAndrew Thompson 	struct usb_interface_descriptor *id;
270760bc48eSAndrew Thompson 	const struct usb_temp_endpoint_desc **ted;
27102ac6454SAndrew Thompson 	const void **rd;
27202ac6454SAndrew Thompson 	uint16_t old_size;
27302ac6454SAndrew Thompson 
27402ac6454SAndrew Thompson 	/* Reserve memory */
27502ac6454SAndrew Thompson 
27602ac6454SAndrew Thompson 	old_size = temp->size;
27702ac6454SAndrew Thompson 	temp->size += sizeof(*id);
27802ac6454SAndrew Thompson 
27902ac6454SAndrew Thompson 	/* Update interface and alternate interface numbers */
28002ac6454SAndrew Thompson 
28102ac6454SAndrew Thompson 	if (tid->isAltInterface == 0) {
28202ac6454SAndrew Thompson 		temp->bAlternateSetting = 0;
28302ac6454SAndrew Thompson 		temp->bInterfaceNumber++;
28402ac6454SAndrew Thompson 	} else {
28502ac6454SAndrew Thompson 		temp->bAlternateSetting++;
28602ac6454SAndrew Thompson 	}
28702ac6454SAndrew Thompson 
28802ac6454SAndrew Thompson 	/* Scan all Raw Descriptors first */
28902ac6454SAndrew Thompson 
29002ac6454SAndrew Thompson 	rd = tid->ppRawDesc;
29102ac6454SAndrew Thompson 
29202ac6454SAndrew Thompson 	if (rd) {
29302ac6454SAndrew Thompson 		while (*rd) {
294a593f6b8SAndrew Thompson 			usb_make_raw_desc(temp, *rd);
29502ac6454SAndrew Thompson 			rd++;
29602ac6454SAndrew Thompson 		}
29702ac6454SAndrew Thompson 	}
29802ac6454SAndrew Thompson 	/* Reset some counters */
29902ac6454SAndrew Thompson 
30002ac6454SAndrew Thompson 	temp->bNumEndpoints = 0;
30102ac6454SAndrew Thompson 
30202ac6454SAndrew Thompson 	/* Scan all Endpoint Descriptors second */
30302ac6454SAndrew Thompson 
30402ac6454SAndrew Thompson 	ted = tid->ppEndpoints;
30502ac6454SAndrew Thompson 	if (ted) {
30602ac6454SAndrew Thompson 		while (*ted) {
307a593f6b8SAndrew Thompson 			usb_make_endpoint_desc(temp, *ted);
30802ac6454SAndrew Thompson 			ted++;
30902ac6454SAndrew Thompson 		}
31002ac6454SAndrew Thompson 	}
31102ac6454SAndrew Thompson 	/*
31202ac6454SAndrew Thompson 	 * Fill out the real USB interface descriptor
31302ac6454SAndrew Thompson 	 * in case there is a buffer present:
31402ac6454SAndrew Thompson 	 */
31502ac6454SAndrew Thompson 	if (temp->buf) {
31602ac6454SAndrew Thompson 		id = USB_ADD_BYTES(temp->buf, old_size);
31702ac6454SAndrew Thompson 		id->bLength = sizeof(*id);
31802ac6454SAndrew Thompson 		id->bDescriptorType = UDESC_INTERFACE;
31902ac6454SAndrew Thompson 		id->bInterfaceNumber = temp->bInterfaceNumber;
32002ac6454SAndrew Thompson 		id->bAlternateSetting = temp->bAlternateSetting;
32102ac6454SAndrew Thompson 		id->bNumEndpoints = temp->bNumEndpoints;
32202ac6454SAndrew Thompson 		id->bInterfaceClass = tid->bInterfaceClass;
32302ac6454SAndrew Thompson 		id->bInterfaceSubClass = tid->bInterfaceSubClass;
32402ac6454SAndrew Thompson 		id->bInterfaceProtocol = tid->bInterfaceProtocol;
32502ac6454SAndrew Thompson 		id->iInterface = tid->iInterface;
32602ac6454SAndrew Thompson 	}
32702ac6454SAndrew Thompson }
32802ac6454SAndrew Thompson 
32902ac6454SAndrew Thompson /*------------------------------------------------------------------------*
330a593f6b8SAndrew Thompson  *	usb_make_config_desc
33102ac6454SAndrew Thompson  *
33202ac6454SAndrew Thompson  * This function will generate an USB config descriptor from the given
33302ac6454SAndrew Thompson  * USB template config descriptor, which will be inserted into the USB
33402ac6454SAndrew Thompson  * configuration.
33502ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
33602ac6454SAndrew Thompson static void
337a593f6b8SAndrew Thompson usb_make_config_desc(struct usb_temp_setup *temp,
338760bc48eSAndrew Thompson     const struct usb_temp_config_desc *tcd)
33902ac6454SAndrew Thompson {
340760bc48eSAndrew Thompson 	struct usb_config_descriptor *cd;
341760bc48eSAndrew Thompson 	const struct usb_temp_interface_desc **tid;
34202ac6454SAndrew Thompson 	uint16_t old_size;
34302ac6454SAndrew Thompson 
34402ac6454SAndrew Thompson 	/* Reserve memory */
34502ac6454SAndrew Thompson 
34602ac6454SAndrew Thompson 	old_size = temp->size;
34702ac6454SAndrew Thompson 	temp->size += sizeof(*cd);
34802ac6454SAndrew Thompson 
34902ac6454SAndrew Thompson 	/* Reset some counters */
35002ac6454SAndrew Thompson 
35102ac6454SAndrew Thompson 	temp->bInterfaceNumber = 0 - 1;
35202ac6454SAndrew Thompson 	temp->bAlternateSetting = 0;
35302ac6454SAndrew Thompson 
35402ac6454SAndrew Thompson 	/* Scan all the USB interfaces */
35502ac6454SAndrew Thompson 
35602ac6454SAndrew Thompson 	tid = tcd->ppIfaceDesc;
35702ac6454SAndrew Thompson 	if (tid) {
35802ac6454SAndrew Thompson 		while (*tid) {
359a593f6b8SAndrew Thompson 			usb_make_interface_desc(temp, *tid);
36002ac6454SAndrew Thompson 			tid++;
36102ac6454SAndrew Thompson 		}
36202ac6454SAndrew Thompson 	}
36302ac6454SAndrew Thompson 	/*
36402ac6454SAndrew Thompson 	 * Fill out the real USB config descriptor
36502ac6454SAndrew Thompson 	 * in case there is a buffer present:
36602ac6454SAndrew Thompson 	 */
36702ac6454SAndrew Thompson 	if (temp->buf) {
36802ac6454SAndrew Thompson 		cd = USB_ADD_BYTES(temp->buf, old_size);
36902ac6454SAndrew Thompson 
37002ac6454SAndrew Thompson 		/* compute total size */
37102ac6454SAndrew Thompson 		old_size = temp->size - old_size;
37202ac6454SAndrew Thompson 
37302ac6454SAndrew Thompson 		cd->bLength = sizeof(*cd);
37402ac6454SAndrew Thompson 		cd->bDescriptorType = UDESC_CONFIG;
37502ac6454SAndrew Thompson 		USETW(cd->wTotalLength, old_size);
37602ac6454SAndrew Thompson 		cd->bNumInterface = temp->bInterfaceNumber + 1;
37702ac6454SAndrew Thompson 		cd->bConfigurationValue = temp->bConfigurationValue;
37802ac6454SAndrew Thompson 		cd->iConfiguration = tcd->iConfiguration;
37902ac6454SAndrew Thompson 		cd->bmAttributes = tcd->bmAttributes;
38002ac6454SAndrew Thompson 		cd->bMaxPower = tcd->bMaxPower;
38102ac6454SAndrew Thompson 		cd->bmAttributes |= (UC_REMOTE_WAKEUP | UC_BUS_POWERED);
38202ac6454SAndrew Thompson 
38302ac6454SAndrew Thompson 		if (temp->self_powered) {
38402ac6454SAndrew Thompson 			cd->bmAttributes |= UC_SELF_POWERED;
38502ac6454SAndrew Thompson 		} else {
38602ac6454SAndrew Thompson 			cd->bmAttributes &= ~UC_SELF_POWERED;
38702ac6454SAndrew Thompson 		}
38802ac6454SAndrew Thompson 	}
38902ac6454SAndrew Thompson }
39002ac6454SAndrew Thompson 
39102ac6454SAndrew Thompson /*------------------------------------------------------------------------*
392a593f6b8SAndrew Thompson  *	usb_make_device_desc
39302ac6454SAndrew Thompson  *
39402ac6454SAndrew Thompson  * This function will generate an USB device descriptor from the
39502ac6454SAndrew Thompson  * given USB template device descriptor.
39602ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
39702ac6454SAndrew Thompson static void
398a593f6b8SAndrew Thompson usb_make_device_desc(struct usb_temp_setup *temp,
399760bc48eSAndrew Thompson     const struct usb_temp_device_desc *tdd)
40002ac6454SAndrew Thompson {
401760bc48eSAndrew Thompson 	struct usb_temp_data *utd;
402760bc48eSAndrew Thompson 	const struct usb_temp_config_desc **tcd;
40302ac6454SAndrew Thompson 	uint16_t old_size;
40402ac6454SAndrew Thompson 
40502ac6454SAndrew Thompson 	/* Reserve memory */
40602ac6454SAndrew Thompson 
40702ac6454SAndrew Thompson 	old_size = temp->size;
40802ac6454SAndrew Thompson 	temp->size += sizeof(*utd);
40902ac6454SAndrew Thompson 
41002ac6454SAndrew Thompson 	/* Scan all the USB configs */
41102ac6454SAndrew Thompson 
41202ac6454SAndrew Thompson 	temp->bConfigurationValue = 1;
41302ac6454SAndrew Thompson 	tcd = tdd->ppConfigDesc;
41402ac6454SAndrew Thompson 	if (tcd) {
41502ac6454SAndrew Thompson 		while (*tcd) {
416a593f6b8SAndrew Thompson 			usb_make_config_desc(temp, *tcd);
41702ac6454SAndrew Thompson 			temp->bConfigurationValue++;
41802ac6454SAndrew Thompson 			tcd++;
41902ac6454SAndrew Thompson 		}
42002ac6454SAndrew Thompson 	}
42102ac6454SAndrew Thompson 	/*
42202ac6454SAndrew Thompson 	 * Fill out the real USB device descriptor
42302ac6454SAndrew Thompson 	 * in case there is a buffer present:
42402ac6454SAndrew Thompson 	 */
42502ac6454SAndrew Thompson 
42602ac6454SAndrew Thompson 	if (temp->buf) {
42702ac6454SAndrew Thompson 		utd = USB_ADD_BYTES(temp->buf, old_size);
42802ac6454SAndrew Thompson 
42902ac6454SAndrew Thompson 		/* Store a pointer to our template device descriptor */
43002ac6454SAndrew Thompson 		utd->tdd = tdd;
43102ac6454SAndrew Thompson 
43202ac6454SAndrew Thompson 		/* Fill out USB device descriptor */
43302ac6454SAndrew Thompson 		utd->udd.bLength = sizeof(utd->udd);
43402ac6454SAndrew Thompson 		utd->udd.bDescriptorType = UDESC_DEVICE;
43502ac6454SAndrew Thompson 		utd->udd.bDeviceClass = tdd->bDeviceClass;
43602ac6454SAndrew Thompson 		utd->udd.bDeviceSubClass = tdd->bDeviceSubClass;
43702ac6454SAndrew Thompson 		utd->udd.bDeviceProtocol = tdd->bDeviceProtocol;
43802ac6454SAndrew Thompson 		USETW(utd->udd.idVendor, tdd->idVendor);
43902ac6454SAndrew Thompson 		USETW(utd->udd.idProduct, tdd->idProduct);
44002ac6454SAndrew Thompson 		USETW(utd->udd.bcdDevice, tdd->bcdDevice);
44102ac6454SAndrew Thompson 		utd->udd.iManufacturer = tdd->iManufacturer;
44202ac6454SAndrew Thompson 		utd->udd.iProduct = tdd->iProduct;
44302ac6454SAndrew Thompson 		utd->udd.iSerialNumber = tdd->iSerialNumber;
44402ac6454SAndrew Thompson 		utd->udd.bNumConfigurations = temp->bConfigurationValue - 1;
44502ac6454SAndrew Thompson 
44602ac6454SAndrew Thompson 		/*
44702ac6454SAndrew Thompson 		 * Fill out the USB device qualifier. Pretend that we
44802ac6454SAndrew Thompson 		 * don't support any other speeds by setting
44902ac6454SAndrew Thompson 		 * "bNumConfigurations" equal to zero. That saves us
45002ac6454SAndrew Thompson 		 * generating an extra set of configuration
45102ac6454SAndrew Thompson 		 * descriptors.
45202ac6454SAndrew Thompson 		 */
45302ac6454SAndrew Thompson 		utd->udq.bLength = sizeof(utd->udq);
45402ac6454SAndrew Thompson 		utd->udq.bDescriptorType = UDESC_DEVICE_QUALIFIER;
45502ac6454SAndrew Thompson 		utd->udq.bDeviceClass = tdd->bDeviceClass;
45602ac6454SAndrew Thompson 		utd->udq.bDeviceSubClass = tdd->bDeviceSubClass;
45702ac6454SAndrew Thompson 		utd->udq.bDeviceProtocol = tdd->bDeviceProtocol;
45802ac6454SAndrew Thompson 		utd->udq.bNumConfigurations = 0;
45902ac6454SAndrew Thompson 		USETW(utd->udq.bcdUSB, 0x0200);
46002ac6454SAndrew Thompson 		utd->udq.bMaxPacketSize0 = 0;
46102ac6454SAndrew Thompson 
4628d2dd5ddSAndrew Thompson 		switch (temp->usb_speed) {
46302ac6454SAndrew Thompson 		case USB_SPEED_LOW:
46402ac6454SAndrew Thompson 			USETW(utd->udd.bcdUSB, 0x0110);
46502ac6454SAndrew Thompson 			utd->udd.bMaxPacketSize = 8;
46602ac6454SAndrew Thompson 			break;
46702ac6454SAndrew Thompson 		case USB_SPEED_FULL:
46802ac6454SAndrew Thompson 			USETW(utd->udd.bcdUSB, 0x0110);
46902ac6454SAndrew Thompson 			utd->udd.bMaxPacketSize = 32;
47002ac6454SAndrew Thompson 			break;
47102ac6454SAndrew Thompson 		case USB_SPEED_HIGH:
47202ac6454SAndrew Thompson 			USETW(utd->udd.bcdUSB, 0x0200);
47302ac6454SAndrew Thompson 			utd->udd.bMaxPacketSize = 64;
47402ac6454SAndrew Thompson 			break;
47502ac6454SAndrew Thompson 		case USB_SPEED_VARIABLE:
47602ac6454SAndrew Thompson 			USETW(utd->udd.bcdUSB, 0x0250);
47702ac6454SAndrew Thompson 			utd->udd.bMaxPacketSize = 255;	/* 512 bytes */
47802ac6454SAndrew Thompson 			break;
47902ac6454SAndrew Thompson 		default:
48002ac6454SAndrew Thompson 			temp->err = USB_ERR_INVAL;
48102ac6454SAndrew Thompson 			break;
48202ac6454SAndrew Thompson 		}
48302ac6454SAndrew Thompson 	}
48402ac6454SAndrew Thompson }
48502ac6454SAndrew Thompson 
48602ac6454SAndrew Thompson /*------------------------------------------------------------------------*
487a593f6b8SAndrew Thompson  *	usb_hw_ep_match
48802ac6454SAndrew Thompson  *
48902ac6454SAndrew Thompson  * Return values:
49002ac6454SAndrew Thompson  *    0: The endpoint profile does not match the criterias
49102ac6454SAndrew Thompson  * Else: The endpoint profile matches the criterias
49202ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
49302ac6454SAndrew Thompson static uint8_t
494a593f6b8SAndrew Thompson usb_hw_ep_match(const struct usb_hw_ep_profile *pf,
49502ac6454SAndrew Thompson     uint8_t ep_type, uint8_t ep_dir_in)
49602ac6454SAndrew Thompson {
49702ac6454SAndrew Thompson 	if (ep_type == UE_CONTROL) {
49802ac6454SAndrew Thompson 		/* special */
49902ac6454SAndrew Thompson 		return (pf->support_control);
50002ac6454SAndrew Thompson 	}
50102ac6454SAndrew Thompson 	if ((pf->support_in && ep_dir_in) ||
50202ac6454SAndrew Thompson 	    (pf->support_out && !ep_dir_in)) {
50302ac6454SAndrew Thompson 		if ((pf->support_interrupt && (ep_type == UE_INTERRUPT)) ||
50402ac6454SAndrew Thompson 		    (pf->support_isochronous && (ep_type == UE_ISOCHRONOUS)) ||
50502ac6454SAndrew Thompson 		    (pf->support_bulk && (ep_type == UE_BULK))) {
50602ac6454SAndrew Thompson 			return (1);
50702ac6454SAndrew Thompson 		}
50802ac6454SAndrew Thompson 	}
50902ac6454SAndrew Thompson 	return (0);
51002ac6454SAndrew Thompson }
51102ac6454SAndrew Thompson 
51202ac6454SAndrew Thompson /*------------------------------------------------------------------------*
513a593f6b8SAndrew Thompson  *	usb_hw_ep_find_match
51402ac6454SAndrew Thompson  *
51502ac6454SAndrew Thompson  * This function is used to find the best matching endpoint profile
51602ac6454SAndrew Thompson  * for and endpoint belonging to an USB descriptor.
51702ac6454SAndrew Thompson  *
51802ac6454SAndrew Thompson  * Return values:
51902ac6454SAndrew Thompson  *    0: Success. Got a match.
52002ac6454SAndrew Thompson  * Else: Failure. No match.
52102ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
52202ac6454SAndrew Thompson static uint8_t
523a593f6b8SAndrew Thompson usb_hw_ep_find_match(struct usb_hw_ep_scratch *ues,
524760bc48eSAndrew Thompson     struct usb_hw_ep_scratch_sub *ep, uint8_t is_simplex)
52502ac6454SAndrew Thompson {
526760bc48eSAndrew Thompson 	const struct usb_hw_ep_profile *pf;
52702ac6454SAndrew Thompson 	uint16_t distance;
52802ac6454SAndrew Thompson 	uint16_t temp;
52902ac6454SAndrew Thompson 	uint16_t max_frame_size;
53002ac6454SAndrew Thompson 	uint8_t n;
53102ac6454SAndrew Thompson 	uint8_t best_n;
53202ac6454SAndrew Thompson 	uint8_t dir_in;
53302ac6454SAndrew Thompson 	uint8_t dir_out;
53402ac6454SAndrew Thompson 
53502ac6454SAndrew Thompson 	distance = 0xFFFF;
53602ac6454SAndrew Thompson 	best_n = 0;
53702ac6454SAndrew Thompson 
53802ac6454SAndrew Thompson 	if ((!ep->needs_in) && (!ep->needs_out)) {
53902ac6454SAndrew Thompson 		return (0);		/* we are done */
54002ac6454SAndrew Thompson 	}
54102ac6454SAndrew Thompson 	if (ep->needs_ep_type == UE_CONTROL) {
54202ac6454SAndrew Thompson 		dir_in = 1;
54302ac6454SAndrew Thompson 		dir_out = 1;
54402ac6454SAndrew Thompson 	} else {
54502ac6454SAndrew Thompson 		if (ep->needs_in) {
54602ac6454SAndrew Thompson 			dir_in = 1;
54702ac6454SAndrew Thompson 			dir_out = 0;
54802ac6454SAndrew Thompson 		} else {
54902ac6454SAndrew Thompson 			dir_in = 0;
55002ac6454SAndrew Thompson 			dir_out = 1;
55102ac6454SAndrew Thompson 		}
55202ac6454SAndrew Thompson 	}
55302ac6454SAndrew Thompson 
55402ac6454SAndrew Thompson 	for (n = 1; n != (USB_EP_MAX / 2); n++) {
55502ac6454SAndrew Thompson 
55602ac6454SAndrew Thompson 		/* get HW endpoint profile */
55702ac6454SAndrew Thompson 		(ues->methods->get_hw_ep_profile) (ues->udev, &pf, n);
55802ac6454SAndrew Thompson 		if (pf == NULL) {
55902ac6454SAndrew Thompson 			/* end of profiles */
56002ac6454SAndrew Thompson 			break;
56102ac6454SAndrew Thompson 		}
56202ac6454SAndrew Thompson 		/* check if IN-endpoint is reserved */
56302ac6454SAndrew Thompson 		if (dir_in || pf->is_simplex) {
56402ac6454SAndrew Thompson 			if (ues->bmInAlloc[n / 8] & (1 << (n % 8))) {
56502ac6454SAndrew Thompson 				/* mismatch */
56602ac6454SAndrew Thompson 				continue;
56702ac6454SAndrew Thompson 			}
56802ac6454SAndrew Thompson 		}
56902ac6454SAndrew Thompson 		/* check if OUT-endpoint is reserved */
57002ac6454SAndrew Thompson 		if (dir_out || pf->is_simplex) {
57102ac6454SAndrew Thompson 			if (ues->bmOutAlloc[n / 8] & (1 << (n % 8))) {
57202ac6454SAndrew Thompson 				/* mismatch */
57302ac6454SAndrew Thompson 				continue;
57402ac6454SAndrew Thompson 			}
57502ac6454SAndrew Thompson 		}
57602ac6454SAndrew Thompson 		/* check simplex */
57702ac6454SAndrew Thompson 		if (pf->is_simplex == is_simplex) {
57802ac6454SAndrew Thompson 			/* mismatch */
57902ac6454SAndrew Thompson 			continue;
58002ac6454SAndrew Thompson 		}
58102ac6454SAndrew Thompson 		/* check if HW endpoint matches */
582a593f6b8SAndrew Thompson 		if (!usb_hw_ep_match(pf, ep->needs_ep_type, dir_in)) {
58302ac6454SAndrew Thompson 			/* mismatch */
58402ac6454SAndrew Thompson 			continue;
58502ac6454SAndrew Thompson 		}
58602ac6454SAndrew Thompson 		/* get maximum frame size */
58702ac6454SAndrew Thompson 		if (dir_in)
58802ac6454SAndrew Thompson 			max_frame_size = pf->max_in_frame_size;
58902ac6454SAndrew Thompson 		else
59002ac6454SAndrew Thompson 			max_frame_size = pf->max_out_frame_size;
59102ac6454SAndrew Thompson 
59202ac6454SAndrew Thompson 		/* check if we have a matching profile */
59302ac6454SAndrew Thompson 		if (max_frame_size >= ep->max_frame_size) {
59402ac6454SAndrew Thompson 			temp = (max_frame_size - ep->max_frame_size);
59502ac6454SAndrew Thompson 			if (distance > temp) {
59602ac6454SAndrew Thompson 				distance = temp;
59702ac6454SAndrew Thompson 				best_n = n;
59802ac6454SAndrew Thompson 				ep->pf = pf;
59902ac6454SAndrew Thompson 			}
60002ac6454SAndrew Thompson 		}
60102ac6454SAndrew Thompson 	}
60202ac6454SAndrew Thompson 
60302ac6454SAndrew Thompson 	/* see if we got a match */
60402ac6454SAndrew Thompson 	if (best_n != 0) {
60502ac6454SAndrew Thompson 		/* get the correct profile */
60602ac6454SAndrew Thompson 		pf = ep->pf;
60702ac6454SAndrew Thompson 
60802ac6454SAndrew Thompson 		/* reserve IN-endpoint */
60902ac6454SAndrew Thompson 		if (dir_in) {
61002ac6454SAndrew Thompson 			ues->bmInAlloc[best_n / 8] |=
61102ac6454SAndrew Thompson 			    (1 << (best_n % 8));
61202ac6454SAndrew Thompson 			ep->hw_endpoint_in = best_n | UE_DIR_IN;
61302ac6454SAndrew Thompson 			ep->needs_in = 0;
61402ac6454SAndrew Thompson 		}
61502ac6454SAndrew Thompson 		/* reserve OUT-endpoint */
61602ac6454SAndrew Thompson 		if (dir_out) {
61702ac6454SAndrew Thompson 			ues->bmOutAlloc[best_n / 8] |=
61802ac6454SAndrew Thompson 			    (1 << (best_n % 8));
61902ac6454SAndrew Thompson 			ep->hw_endpoint_out = best_n | UE_DIR_OUT;
62002ac6454SAndrew Thompson 			ep->needs_out = 0;
62102ac6454SAndrew Thompson 		}
62202ac6454SAndrew Thompson 		return (0);		/* got a match */
62302ac6454SAndrew Thompson 	}
62402ac6454SAndrew Thompson 	return (1);			/* failure */
62502ac6454SAndrew Thompson }
62602ac6454SAndrew Thompson 
62702ac6454SAndrew Thompson /*------------------------------------------------------------------------*
628a593f6b8SAndrew Thompson  *	usb_hw_ep_get_needs
62902ac6454SAndrew Thompson  *
63002ac6454SAndrew Thompson  * This function will figure out the type and number of endpoints
63102ac6454SAndrew Thompson  * which are needed for an USB configuration.
63202ac6454SAndrew Thompson  *
63302ac6454SAndrew Thompson  * Return values:
63402ac6454SAndrew Thompson  *    0: Success.
63502ac6454SAndrew Thompson  * Else: Failure.
63602ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
63702ac6454SAndrew Thompson static uint8_t
638a593f6b8SAndrew Thompson usb_hw_ep_get_needs(struct usb_hw_ep_scratch *ues,
63902ac6454SAndrew Thompson     uint8_t ep_type, uint8_t is_complete)
64002ac6454SAndrew Thompson {
641760bc48eSAndrew Thompson 	const struct usb_hw_ep_profile *pf;
642760bc48eSAndrew Thompson 	struct usb_hw_ep_scratch_sub *ep_iface;
643760bc48eSAndrew Thompson 	struct usb_hw_ep_scratch_sub *ep_curr;
644760bc48eSAndrew Thompson 	struct usb_hw_ep_scratch_sub *ep_max;
645760bc48eSAndrew Thompson 	struct usb_hw_ep_scratch_sub *ep_end;
646760bc48eSAndrew Thompson 	struct usb_descriptor *desc;
647760bc48eSAndrew Thompson 	struct usb_interface_descriptor *id;
648760bc48eSAndrew Thompson 	struct usb_endpoint_descriptor *ed;
6498d2dd5ddSAndrew Thompson 	enum usb_dev_speed speed;
65002ac6454SAndrew Thompson 	uint16_t wMaxPacketSize;
65102ac6454SAndrew Thompson 	uint16_t temp;
65202ac6454SAndrew Thompson 	uint8_t ep_no;
65302ac6454SAndrew Thompson 
65402ac6454SAndrew Thompson 	ep_iface = ues->ep_max;
65502ac6454SAndrew Thompson 	ep_curr = ues->ep_max;
65602ac6454SAndrew Thompson 	ep_end = ues->ep + USB_EP_MAX;
65702ac6454SAndrew Thompson 	ep_max = ues->ep_max;
65802ac6454SAndrew Thompson 	desc = NULL;
659a593f6b8SAndrew Thompson 	speed = usbd_get_speed(ues->udev);
66002ac6454SAndrew Thompson 
66102ac6454SAndrew Thompson repeat:
66202ac6454SAndrew Thompson 
663a593f6b8SAndrew Thompson 	while ((desc = usb_desc_foreach(ues->cd, desc))) {
66402ac6454SAndrew Thompson 
66502ac6454SAndrew Thompson 		if ((desc->bDescriptorType == UDESC_INTERFACE) &&
66602ac6454SAndrew Thompson 		    (desc->bLength >= sizeof(*id))) {
66702ac6454SAndrew Thompson 
66802ac6454SAndrew Thompson 			id = (void *)desc;
66902ac6454SAndrew Thompson 
67002ac6454SAndrew Thompson 			if (id->bAlternateSetting == 0) {
67102ac6454SAndrew Thompson 				/* going forward */
67202ac6454SAndrew Thompson 				ep_iface = ep_max;
67302ac6454SAndrew Thompson 			} else {
67402ac6454SAndrew Thompson 				/* reset */
67502ac6454SAndrew Thompson 				ep_curr = ep_iface;
67602ac6454SAndrew Thompson 			}
67702ac6454SAndrew Thompson 		}
67802ac6454SAndrew Thompson 		if ((desc->bDescriptorType == UDESC_ENDPOINT) &&
67902ac6454SAndrew Thompson 		    (desc->bLength >= sizeof(*ed))) {
68002ac6454SAndrew Thompson 
68102ac6454SAndrew Thompson 			ed = (void *)desc;
68202ac6454SAndrew Thompson 
68302ac6454SAndrew Thompson 			goto handle_endpoint_desc;
68402ac6454SAndrew Thompson 		}
68502ac6454SAndrew Thompson 	}
68602ac6454SAndrew Thompson 	ues->ep_max = ep_max;
68702ac6454SAndrew Thompson 	return (0);
68802ac6454SAndrew Thompson 
68902ac6454SAndrew Thompson handle_endpoint_desc:
69002ac6454SAndrew Thompson 	temp = (ed->bmAttributes & UE_XFERTYPE);
69102ac6454SAndrew Thompson 
69202ac6454SAndrew Thompson 	if (temp == ep_type) {
69302ac6454SAndrew Thompson 
69402ac6454SAndrew Thompson 		if (ep_curr == ep_end) {
69502ac6454SAndrew Thompson 			/* too many endpoints */
69602ac6454SAndrew Thompson 			return (1);	/* failure */
69702ac6454SAndrew Thompson 		}
69802ac6454SAndrew Thompson 		wMaxPacketSize = UGETW(ed->wMaxPacketSize);
69902ac6454SAndrew Thompson 		if ((wMaxPacketSize & 0xF800) &&
70002ac6454SAndrew Thompson 		    (speed == USB_SPEED_HIGH)) {
70102ac6454SAndrew Thompson 			/* handle packet multiplier */
70202ac6454SAndrew Thompson 			temp = (wMaxPacketSize >> 11) & 3;
70302ac6454SAndrew Thompson 			wMaxPacketSize &= 0x7FF;
70402ac6454SAndrew Thompson 			if (temp == 1) {
70502ac6454SAndrew Thompson 				wMaxPacketSize *= 2;
70602ac6454SAndrew Thompson 			} else {
70702ac6454SAndrew Thompson 				wMaxPacketSize *= 3;
70802ac6454SAndrew Thompson 			}
70902ac6454SAndrew Thompson 		}
71002ac6454SAndrew Thompson 		/*
71102ac6454SAndrew Thompson 		 * Check if we have a fixed endpoint number, else the
71202ac6454SAndrew Thompson 		 * endpoint number is allocated dynamically:
71302ac6454SAndrew Thompson 		 */
71402ac6454SAndrew Thompson 		ep_no = (ed->bEndpointAddress & UE_ADDR);
71502ac6454SAndrew Thompson 		if (ep_no != 0) {
71602ac6454SAndrew Thompson 
71702ac6454SAndrew Thompson 			/* get HW endpoint profile */
71802ac6454SAndrew Thompson 			(ues->methods->get_hw_ep_profile)
71902ac6454SAndrew Thompson 			    (ues->udev, &pf, ep_no);
72002ac6454SAndrew Thompson 			if (pf == NULL) {
72102ac6454SAndrew Thompson 				/* HW profile does not exist - failure */
72202ac6454SAndrew Thompson 				DPRINTFN(0, "Endpoint profile %u "
72302ac6454SAndrew Thompson 				    "does not exist\n", ep_no);
72402ac6454SAndrew Thompson 				return (1);
72502ac6454SAndrew Thompson 			}
72602ac6454SAndrew Thompson 			/* reserve fixed endpoint number */
72702ac6454SAndrew Thompson 			if (ep_type == UE_CONTROL) {
72802ac6454SAndrew Thompson 				ues->bmInAlloc[ep_no / 8] |=
72902ac6454SAndrew Thompson 				    (1 << (ep_no % 8));
73002ac6454SAndrew Thompson 				ues->bmOutAlloc[ep_no / 8] |=
73102ac6454SAndrew Thompson 				    (1 << (ep_no % 8));
73202ac6454SAndrew Thompson 				if ((pf->max_in_frame_size < wMaxPacketSize) ||
73302ac6454SAndrew Thompson 				    (pf->max_out_frame_size < wMaxPacketSize)) {
73402ac6454SAndrew Thompson 					DPRINTFN(0, "Endpoint profile %u "
735767cb2e2SAndrew Thompson 					    "has too small buffer\n", ep_no);
73602ac6454SAndrew Thompson 					return (1);
73702ac6454SAndrew Thompson 				}
73802ac6454SAndrew Thompson 			} else if (ed->bEndpointAddress & UE_DIR_IN) {
73902ac6454SAndrew Thompson 				ues->bmInAlloc[ep_no / 8] |=
74002ac6454SAndrew Thompson 				    (1 << (ep_no % 8));
74102ac6454SAndrew Thompson 				if (pf->max_in_frame_size < wMaxPacketSize) {
74202ac6454SAndrew Thompson 					DPRINTFN(0, "Endpoint profile %u "
743767cb2e2SAndrew Thompson 					    "has too small buffer\n", ep_no);
74402ac6454SAndrew Thompson 					return (1);
74502ac6454SAndrew Thompson 				}
74602ac6454SAndrew Thompson 			} else {
74702ac6454SAndrew Thompson 				ues->bmOutAlloc[ep_no / 8] |=
74802ac6454SAndrew Thompson 				    (1 << (ep_no % 8));
74902ac6454SAndrew Thompson 				if (pf->max_out_frame_size < wMaxPacketSize) {
75002ac6454SAndrew Thompson 					DPRINTFN(0, "Endpoint profile %u "
751767cb2e2SAndrew Thompson 					    "has too small buffer\n", ep_no);
75202ac6454SAndrew Thompson 					return (1);
75302ac6454SAndrew Thompson 				}
75402ac6454SAndrew Thompson 			}
75502ac6454SAndrew Thompson 		} else if (is_complete) {
75602ac6454SAndrew Thompson 
75702ac6454SAndrew Thompson 			/* check if we have enough buffer space */
75802ac6454SAndrew Thompson 			if (wMaxPacketSize >
75902ac6454SAndrew Thompson 			    ep_curr->max_frame_size) {
76002ac6454SAndrew Thompson 				return (1);	/* failure */
76102ac6454SAndrew Thompson 			}
76202ac6454SAndrew Thompson 			if (ed->bEndpointAddress & UE_DIR_IN) {
76302ac6454SAndrew Thompson 				ed->bEndpointAddress =
76402ac6454SAndrew Thompson 				    ep_curr->hw_endpoint_in;
76502ac6454SAndrew Thompson 			} else {
76602ac6454SAndrew Thompson 				ed->bEndpointAddress =
76702ac6454SAndrew Thompson 				    ep_curr->hw_endpoint_out;
76802ac6454SAndrew Thompson 			}
76902ac6454SAndrew Thompson 
77002ac6454SAndrew Thompson 		} else {
77102ac6454SAndrew Thompson 
77202ac6454SAndrew Thompson 			/* compute the maximum frame size */
77302ac6454SAndrew Thompson 			if (ep_curr->max_frame_size < wMaxPacketSize) {
77402ac6454SAndrew Thompson 				ep_curr->max_frame_size = wMaxPacketSize;
77502ac6454SAndrew Thompson 			}
77602ac6454SAndrew Thompson 			if (temp == UE_CONTROL) {
77702ac6454SAndrew Thompson 				ep_curr->needs_in = 1;
77802ac6454SAndrew Thompson 				ep_curr->needs_out = 1;
77902ac6454SAndrew Thompson 			} else {
78002ac6454SAndrew Thompson 				if (ed->bEndpointAddress & UE_DIR_IN) {
78102ac6454SAndrew Thompson 					ep_curr->needs_in = 1;
78202ac6454SAndrew Thompson 				} else {
78302ac6454SAndrew Thompson 					ep_curr->needs_out = 1;
78402ac6454SAndrew Thompson 				}
78502ac6454SAndrew Thompson 			}
78602ac6454SAndrew Thompson 			ep_curr->needs_ep_type = ep_type;
78702ac6454SAndrew Thompson 		}
78802ac6454SAndrew Thompson 
78902ac6454SAndrew Thompson 		ep_curr++;
79002ac6454SAndrew Thompson 		if (ep_max < ep_curr) {
79102ac6454SAndrew Thompson 			ep_max = ep_curr;
79202ac6454SAndrew Thompson 		}
79302ac6454SAndrew Thompson 	}
79402ac6454SAndrew Thompson 	goto repeat;
79502ac6454SAndrew Thompson }
79602ac6454SAndrew Thompson 
79702ac6454SAndrew Thompson /*------------------------------------------------------------------------*
798a593f6b8SAndrew Thompson  *	usb_hw_ep_resolve
79902ac6454SAndrew Thompson  *
80002ac6454SAndrew Thompson  * This function will try to resolve endpoint requirements by the
80102ac6454SAndrew Thompson  * given endpoint profiles that the USB hardware reports.
80202ac6454SAndrew Thompson  *
80302ac6454SAndrew Thompson  * Return values:
80402ac6454SAndrew Thompson  *    0: Success
80502ac6454SAndrew Thompson  * Else: Failure
80602ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
807e0a69b51SAndrew Thompson static usb_error_t
808a593f6b8SAndrew Thompson usb_hw_ep_resolve(struct usb_device *udev,
809760bc48eSAndrew Thompson     struct usb_descriptor *desc)
81002ac6454SAndrew Thompson {
811760bc48eSAndrew Thompson 	struct usb_hw_ep_scratch *ues;
812760bc48eSAndrew Thompson 	struct usb_hw_ep_scratch_sub *ep;
813760bc48eSAndrew Thompson 	const struct usb_hw_ep_profile *pf;
814760bc48eSAndrew Thompson 	struct usb_bus_methods *methods;
815760bc48eSAndrew Thompson 	struct usb_device_descriptor *dd;
81602ac6454SAndrew Thompson 	uint16_t mps;
81702ac6454SAndrew Thompson 
81802ac6454SAndrew Thompson 	if (desc == NULL) {
81902ac6454SAndrew Thompson 		return (USB_ERR_INVAL);
82002ac6454SAndrew Thompson 	}
82102ac6454SAndrew Thompson 	/* get bus methods */
82202ac6454SAndrew Thompson 	methods = udev->bus->methods;
82302ac6454SAndrew Thompson 
82402ac6454SAndrew Thompson 	if (methods->get_hw_ep_profile == NULL) {
82502ac6454SAndrew Thompson 		return (USB_ERR_INVAL);
82602ac6454SAndrew Thompson 	}
82702ac6454SAndrew Thompson 	if (desc->bDescriptorType == UDESC_DEVICE) {
82802ac6454SAndrew Thompson 
82902ac6454SAndrew Thompson 		if (desc->bLength < sizeof(*dd)) {
83002ac6454SAndrew Thompson 			return (USB_ERR_INVAL);
83102ac6454SAndrew Thompson 		}
83202ac6454SAndrew Thompson 		dd = (void *)desc;
83302ac6454SAndrew Thompson 
83402ac6454SAndrew Thompson 		/* get HW control endpoint 0 profile */
83502ac6454SAndrew Thompson 		(methods->get_hw_ep_profile) (udev, &pf, 0);
83602ac6454SAndrew Thompson 		if (pf == NULL) {
83702ac6454SAndrew Thompson 			return (USB_ERR_INVAL);
83802ac6454SAndrew Thompson 		}
839a593f6b8SAndrew Thompson 		if (!usb_hw_ep_match(pf, UE_CONTROL, 0)) {
84002ac6454SAndrew Thompson 			DPRINTFN(0, "Endpoint 0 does not "
84102ac6454SAndrew Thompson 			    "support control\n");
84202ac6454SAndrew Thompson 			return (USB_ERR_INVAL);
84302ac6454SAndrew Thompson 		}
84402ac6454SAndrew Thompson 		mps = dd->bMaxPacketSize;
84502ac6454SAndrew Thompson 
84602ac6454SAndrew Thompson 		if (udev->speed == USB_SPEED_FULL) {
84702ac6454SAndrew Thompson 			/*
84802ac6454SAndrew Thompson 			 * We can optionally choose another packet size !
84902ac6454SAndrew Thompson 			 */
85002ac6454SAndrew Thompson 			while (1) {
85102ac6454SAndrew Thompson 				/* check if "mps" is ok */
85202ac6454SAndrew Thompson 				if (pf->max_in_frame_size >= mps) {
85302ac6454SAndrew Thompson 					break;
85402ac6454SAndrew Thompson 				}
85502ac6454SAndrew Thompson 				/* reduce maximum packet size */
85602ac6454SAndrew Thompson 				mps /= 2;
85702ac6454SAndrew Thompson 
85802ac6454SAndrew Thompson 				/* check if "mps" is too small */
85902ac6454SAndrew Thompson 				if (mps < 8) {
86002ac6454SAndrew Thompson 					return (USB_ERR_INVAL);
86102ac6454SAndrew Thompson 				}
86202ac6454SAndrew Thompson 			}
86302ac6454SAndrew Thompson 
86402ac6454SAndrew Thompson 			dd->bMaxPacketSize = mps;
86502ac6454SAndrew Thompson 
86602ac6454SAndrew Thompson 		} else {
86702ac6454SAndrew Thompson 			/* We only have one choice */
86802ac6454SAndrew Thompson 			if (mps == 255) {
86902ac6454SAndrew Thompson 				mps = 512;
87002ac6454SAndrew Thompson 			}
87102ac6454SAndrew Thompson 			/* Check if we support the specified wMaxPacketSize */
87202ac6454SAndrew Thompson 			if (pf->max_in_frame_size < mps) {
87302ac6454SAndrew Thompson 				return (USB_ERR_INVAL);
87402ac6454SAndrew Thompson 			}
87502ac6454SAndrew Thompson 		}
87602ac6454SAndrew Thompson 		return (0);		/* success */
87702ac6454SAndrew Thompson 	}
87802ac6454SAndrew Thompson 	if (desc->bDescriptorType != UDESC_CONFIG) {
87902ac6454SAndrew Thompson 		return (USB_ERR_INVAL);
88002ac6454SAndrew Thompson 	}
88102ac6454SAndrew Thompson 	if (desc->bLength < sizeof(*(ues->cd))) {
88202ac6454SAndrew Thompson 		return (USB_ERR_INVAL);
88302ac6454SAndrew Thompson 	}
88402ac6454SAndrew Thompson 	ues = udev->bus->scratch[0].hw_ep_scratch;
88502ac6454SAndrew Thompson 
88602ac6454SAndrew Thompson 	bzero(ues, sizeof(*ues));
88702ac6454SAndrew Thompson 
88802ac6454SAndrew Thompson 	ues->ep_max = ues->ep;
88902ac6454SAndrew Thompson 	ues->cd = (void *)desc;
89002ac6454SAndrew Thompson 	ues->methods = methods;
89102ac6454SAndrew Thompson 	ues->udev = udev;
89202ac6454SAndrew Thompson 
89302ac6454SAndrew Thompson 	/* Get all the endpoints we need */
89402ac6454SAndrew Thompson 
895a593f6b8SAndrew Thompson 	if (usb_hw_ep_get_needs(ues, UE_ISOCHRONOUS, 0) ||
896a593f6b8SAndrew Thompson 	    usb_hw_ep_get_needs(ues, UE_INTERRUPT, 0) ||
897a593f6b8SAndrew Thompson 	    usb_hw_ep_get_needs(ues, UE_CONTROL, 0) ||
898a593f6b8SAndrew Thompson 	    usb_hw_ep_get_needs(ues, UE_BULK, 0)) {
89902ac6454SAndrew Thompson 		DPRINTFN(0, "Could not get needs\n");
90002ac6454SAndrew Thompson 		return (USB_ERR_INVAL);
90102ac6454SAndrew Thompson 	}
90202ac6454SAndrew Thompson 	for (ep = ues->ep; ep != ues->ep_max; ep++) {
90302ac6454SAndrew Thompson 
90402ac6454SAndrew Thompson 		while (ep->needs_in || ep->needs_out) {
90502ac6454SAndrew Thompson 
90602ac6454SAndrew Thompson 			/*
90702ac6454SAndrew Thompson 		         * First try to use a simplex endpoint.
90802ac6454SAndrew Thompson 		         * Then try to use a duplex endpoint.
90902ac6454SAndrew Thompson 		         */
910a593f6b8SAndrew Thompson 			if (usb_hw_ep_find_match(ues, ep, 1) &&
911a593f6b8SAndrew Thompson 			    usb_hw_ep_find_match(ues, ep, 0)) {
91202ac6454SAndrew Thompson 				DPRINTFN(0, "Could not find match\n");
91302ac6454SAndrew Thompson 				return (USB_ERR_INVAL);
91402ac6454SAndrew Thompson 			}
91502ac6454SAndrew Thompson 		}
91602ac6454SAndrew Thompson 	}
91702ac6454SAndrew Thompson 
91802ac6454SAndrew Thompson 	ues->ep_max = ues->ep;
91902ac6454SAndrew Thompson 
92002ac6454SAndrew Thompson 	/* Update all endpoint addresses */
92102ac6454SAndrew Thompson 
922a593f6b8SAndrew Thompson 	if (usb_hw_ep_get_needs(ues, UE_ISOCHRONOUS, 1) ||
923a593f6b8SAndrew Thompson 	    usb_hw_ep_get_needs(ues, UE_INTERRUPT, 1) ||
924a593f6b8SAndrew Thompson 	    usb_hw_ep_get_needs(ues, UE_CONTROL, 1) ||
925a593f6b8SAndrew Thompson 	    usb_hw_ep_get_needs(ues, UE_BULK, 1)) {
92602ac6454SAndrew Thompson 		DPRINTFN(0, "Could not update endpoint address\n");
92702ac6454SAndrew Thompson 		return (USB_ERR_INVAL);
92802ac6454SAndrew Thompson 	}
92902ac6454SAndrew Thompson 	return (0);			/* success */
93002ac6454SAndrew Thompson }
93102ac6454SAndrew Thompson 
93202ac6454SAndrew Thompson /*------------------------------------------------------------------------*
933a593f6b8SAndrew Thompson  *	usb_temp_get_tdd
93402ac6454SAndrew Thompson  *
93502ac6454SAndrew Thompson  * Returns:
93602ac6454SAndrew Thompson  *  NULL: No USB template device descriptor found.
93702ac6454SAndrew Thompson  *  Else: Pointer to the USB template device descriptor.
93802ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
939760bc48eSAndrew Thompson static const struct usb_temp_device_desc *
940a593f6b8SAndrew Thompson usb_temp_get_tdd(struct usb_device *udev)
94102ac6454SAndrew Thompson {
942a593f6b8SAndrew Thompson 	if (udev->usb_template_ptr == NULL) {
94302ac6454SAndrew Thompson 		return (NULL);
94402ac6454SAndrew Thompson 	}
945a593f6b8SAndrew Thompson 	return (udev->usb_template_ptr->tdd);
94602ac6454SAndrew Thompson }
94702ac6454SAndrew Thompson 
94802ac6454SAndrew Thompson /*------------------------------------------------------------------------*
949a593f6b8SAndrew Thompson  *	usb_temp_get_device_desc
95002ac6454SAndrew Thompson  *
95102ac6454SAndrew Thompson  * Returns:
95202ac6454SAndrew Thompson  *  NULL: No USB device descriptor found.
95302ac6454SAndrew Thompson  *  Else: Pointer to USB device descriptor.
95402ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
95502ac6454SAndrew Thompson static void *
956a593f6b8SAndrew Thompson usb_temp_get_device_desc(struct usb_device *udev)
95702ac6454SAndrew Thompson {
958760bc48eSAndrew Thompson 	struct usb_device_descriptor *dd;
95902ac6454SAndrew Thompson 
960a593f6b8SAndrew Thompson 	if (udev->usb_template_ptr == NULL) {
96102ac6454SAndrew Thompson 		return (NULL);
96202ac6454SAndrew Thompson 	}
963a593f6b8SAndrew Thompson 	dd = &udev->usb_template_ptr->udd;
96402ac6454SAndrew Thompson 	if (dd->bDescriptorType != UDESC_DEVICE) {
96502ac6454SAndrew Thompson 		/* sanity check failed */
96602ac6454SAndrew Thompson 		return (NULL);
96702ac6454SAndrew Thompson 	}
96802ac6454SAndrew Thompson 	return (dd);
96902ac6454SAndrew Thompson }
97002ac6454SAndrew Thompson 
97102ac6454SAndrew Thompson /*------------------------------------------------------------------------*
972a593f6b8SAndrew Thompson  *	usb_temp_get_qualifier_desc
97302ac6454SAndrew Thompson  *
97402ac6454SAndrew Thompson  * Returns:
97502ac6454SAndrew Thompson  *  NULL: No USB device_qualifier descriptor found.
97602ac6454SAndrew Thompson  *  Else: Pointer to USB device_qualifier descriptor.
97702ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
97802ac6454SAndrew Thompson static void *
979a593f6b8SAndrew Thompson usb_temp_get_qualifier_desc(struct usb_device *udev)
98002ac6454SAndrew Thompson {
981760bc48eSAndrew Thompson 	struct usb_device_qualifier *dq;
98202ac6454SAndrew Thompson 
983a593f6b8SAndrew Thompson 	if (udev->usb_template_ptr == NULL) {
98402ac6454SAndrew Thompson 		return (NULL);
98502ac6454SAndrew Thompson 	}
986a593f6b8SAndrew Thompson 	dq = &udev->usb_template_ptr->udq;
98702ac6454SAndrew Thompson 	if (dq->bDescriptorType != UDESC_DEVICE_QUALIFIER) {
98802ac6454SAndrew Thompson 		/* sanity check failed */
98902ac6454SAndrew Thompson 		return (NULL);
99002ac6454SAndrew Thompson 	}
99102ac6454SAndrew Thompson 	return (dq);
99202ac6454SAndrew Thompson }
99302ac6454SAndrew Thompson 
99402ac6454SAndrew Thompson /*------------------------------------------------------------------------*
995a593f6b8SAndrew Thompson  *	usb_temp_get_config_desc
99602ac6454SAndrew Thompson  *
99702ac6454SAndrew Thompson  * Returns:
99802ac6454SAndrew Thompson  *  NULL: No USB config descriptor found.
99902ac6454SAndrew Thompson  *  Else: Pointer to USB config descriptor having index "index".
100002ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
100102ac6454SAndrew Thompson static void *
1002a593f6b8SAndrew Thompson usb_temp_get_config_desc(struct usb_device *udev,
100302ac6454SAndrew Thompson     uint16_t *pLength, uint8_t index)
100402ac6454SAndrew Thompson {
1005760bc48eSAndrew Thompson 	struct usb_device_descriptor *dd;
1006760bc48eSAndrew Thompson 	struct usb_config_descriptor *cd;
100702ac6454SAndrew Thompson 	uint16_t temp;
100802ac6454SAndrew Thompson 
1009a593f6b8SAndrew Thompson 	if (udev->usb_template_ptr == NULL) {
101002ac6454SAndrew Thompson 		return (NULL);
101102ac6454SAndrew Thompson 	}
1012a593f6b8SAndrew Thompson 	dd = &udev->usb_template_ptr->udd;
1013a593f6b8SAndrew Thompson 	cd = (void *)(udev->usb_template_ptr + 1);
101402ac6454SAndrew Thompson 
101502ac6454SAndrew Thompson 	if (index >= dd->bNumConfigurations) {
101602ac6454SAndrew Thompson 		/* out of range */
101702ac6454SAndrew Thompson 		return (NULL);
101802ac6454SAndrew Thompson 	}
101902ac6454SAndrew Thompson 	while (index--) {
102002ac6454SAndrew Thompson 		if (cd->bDescriptorType != UDESC_CONFIG) {
102102ac6454SAndrew Thompson 			/* sanity check failed */
102202ac6454SAndrew Thompson 			return (NULL);
102302ac6454SAndrew Thompson 		}
102402ac6454SAndrew Thompson 		temp = UGETW(cd->wTotalLength);
102502ac6454SAndrew Thompson 		cd = USB_ADD_BYTES(cd, temp);
102602ac6454SAndrew Thompson 	}
102702ac6454SAndrew Thompson 
102802ac6454SAndrew Thompson 	if (pLength) {
102902ac6454SAndrew Thompson 		*pLength = UGETW(cd->wTotalLength);
103002ac6454SAndrew Thompson 	}
103102ac6454SAndrew Thompson 	return (cd);
103202ac6454SAndrew Thompson }
103302ac6454SAndrew Thompson 
103402ac6454SAndrew Thompson /*------------------------------------------------------------------------*
1035a593f6b8SAndrew Thompson  *	usb_temp_get_vendor_desc
103602ac6454SAndrew Thompson  *
103702ac6454SAndrew Thompson  * Returns:
103802ac6454SAndrew Thompson  *  NULL: No vendor descriptor found.
103902ac6454SAndrew Thompson  *  Else: Pointer to a vendor descriptor.
104002ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
104102ac6454SAndrew Thompson static const void *
1042a593f6b8SAndrew Thompson usb_temp_get_vendor_desc(struct usb_device *udev,
10439a8e0122SAndrew Thompson     const struct usb_device_request *req, uint16_t *plen)
104402ac6454SAndrew Thompson {
1045760bc48eSAndrew Thompson 	const struct usb_temp_device_desc *tdd;
104602ac6454SAndrew Thompson 
1047a593f6b8SAndrew Thompson 	tdd = usb_temp_get_tdd(udev);
104802ac6454SAndrew Thompson 	if (tdd == NULL) {
104902ac6454SAndrew Thompson 		return (NULL);
105002ac6454SAndrew Thompson 	}
105102ac6454SAndrew Thompson 	if (tdd->getVendorDesc == NULL) {
105202ac6454SAndrew Thompson 		return (NULL);
105302ac6454SAndrew Thompson 	}
10549a8e0122SAndrew Thompson 	return ((tdd->getVendorDesc) (req, plen));
105502ac6454SAndrew Thompson }
105602ac6454SAndrew Thompson 
105702ac6454SAndrew Thompson /*------------------------------------------------------------------------*
1058a593f6b8SAndrew Thompson  *	usb_temp_get_string_desc
105902ac6454SAndrew Thompson  *
106002ac6454SAndrew Thompson  * Returns:
106102ac6454SAndrew Thompson  *  NULL: No string descriptor found.
106202ac6454SAndrew Thompson  *  Else: Pointer to a string descriptor.
106302ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
106402ac6454SAndrew Thompson static const void *
1065a593f6b8SAndrew Thompson usb_temp_get_string_desc(struct usb_device *udev,
106602ac6454SAndrew Thompson     uint16_t lang_id, uint8_t string_index)
106702ac6454SAndrew Thompson {
1068760bc48eSAndrew Thompson 	const struct usb_temp_device_desc *tdd;
106902ac6454SAndrew Thompson 
1070a593f6b8SAndrew Thompson 	tdd = usb_temp_get_tdd(udev);
107102ac6454SAndrew Thompson 	if (tdd == NULL) {
107202ac6454SAndrew Thompson 		return (NULL);
107302ac6454SAndrew Thompson 	}
107402ac6454SAndrew Thompson 	if (tdd->getStringDesc == NULL) {
107502ac6454SAndrew Thompson 		return (NULL);
107602ac6454SAndrew Thompson 	}
107702ac6454SAndrew Thompson 	return ((tdd->getStringDesc) (lang_id, string_index));
107802ac6454SAndrew Thompson }
107902ac6454SAndrew Thompson 
108002ac6454SAndrew Thompson /*------------------------------------------------------------------------*
1081a593f6b8SAndrew Thompson  *	usb_temp_get_hub_desc
108202ac6454SAndrew Thompson  *
108302ac6454SAndrew Thompson  * Returns:
108402ac6454SAndrew Thompson  *  NULL: No USB HUB descriptor found.
108502ac6454SAndrew Thompson  *  Else: Pointer to a USB HUB descriptor.
108602ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
108702ac6454SAndrew Thompson static const void *
1088a593f6b8SAndrew Thompson usb_temp_get_hub_desc(struct usb_device *udev)
108902ac6454SAndrew Thompson {
109002ac6454SAndrew Thompson 	return (NULL);			/* needs to be implemented */
109102ac6454SAndrew Thompson }
109202ac6454SAndrew Thompson 
109302ac6454SAndrew Thompson /*------------------------------------------------------------------------*
1094a593f6b8SAndrew Thompson  *	usb_temp_get_desc
109502ac6454SAndrew Thompson  *
109602ac6454SAndrew Thompson  * This function is a demultiplexer for local USB device side control
109702ac6454SAndrew Thompson  * endpoint requests.
109802ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
1099e0a69b51SAndrew Thompson static usb_error_t
1100a593f6b8SAndrew Thompson usb_temp_get_desc(struct usb_device *udev, struct usb_device_request *req,
110102ac6454SAndrew Thompson     const void **pPtr, uint16_t *pLength)
110202ac6454SAndrew Thompson {
110302ac6454SAndrew Thompson 	const uint8_t *buf;
110402ac6454SAndrew Thompson 	uint16_t len;
110502ac6454SAndrew Thompson 
110602ac6454SAndrew Thompson 	buf = NULL;
110702ac6454SAndrew Thompson 	len = 0;
110802ac6454SAndrew Thompson 
110902ac6454SAndrew Thompson 	switch (req->bmRequestType) {
111002ac6454SAndrew Thompson 	case UT_READ_DEVICE:
111102ac6454SAndrew Thompson 		switch (req->bRequest) {
111202ac6454SAndrew Thompson 		case UR_GET_DESCRIPTOR:
111302ac6454SAndrew Thompson 			goto tr_handle_get_descriptor;
111402ac6454SAndrew Thompson 		default:
111502ac6454SAndrew Thompson 			goto tr_stalled;
111602ac6454SAndrew Thompson 		}
111702ac6454SAndrew Thompson 	case UT_READ_CLASS_DEVICE:
111802ac6454SAndrew Thompson 		switch (req->bRequest) {
111902ac6454SAndrew Thompson 		case UR_GET_DESCRIPTOR:
112002ac6454SAndrew Thompson 			goto tr_handle_get_class_descriptor;
112102ac6454SAndrew Thompson 		default:
112202ac6454SAndrew Thompson 			goto tr_stalled;
112302ac6454SAndrew Thompson 		}
112402ac6454SAndrew Thompson 	default:
112502ac6454SAndrew Thompson 		goto tr_stalled;
112602ac6454SAndrew Thompson 	}
112702ac6454SAndrew Thompson 
112802ac6454SAndrew Thompson tr_handle_get_descriptor:
112902ac6454SAndrew Thompson 	switch (req->wValue[1]) {
113002ac6454SAndrew Thompson 	case UDESC_DEVICE:
113102ac6454SAndrew Thompson 		if (req->wValue[0]) {
113202ac6454SAndrew Thompson 			goto tr_stalled;
113302ac6454SAndrew Thompson 		}
1134a593f6b8SAndrew Thompson 		buf = usb_temp_get_device_desc(udev);
113502ac6454SAndrew Thompson 		goto tr_valid;
113602ac6454SAndrew Thompson 	case UDESC_DEVICE_QUALIFIER:
113702ac6454SAndrew Thompson 		if (udev->speed != USB_SPEED_HIGH) {
113802ac6454SAndrew Thompson 			goto tr_stalled;
113902ac6454SAndrew Thompson 		}
114002ac6454SAndrew Thompson 		if (req->wValue[0]) {
114102ac6454SAndrew Thompson 			goto tr_stalled;
114202ac6454SAndrew Thompson 		}
1143a593f6b8SAndrew Thompson 		buf = usb_temp_get_qualifier_desc(udev);
114402ac6454SAndrew Thompson 		goto tr_valid;
114502ac6454SAndrew Thompson 	case UDESC_OTHER_SPEED_CONFIGURATION:
114602ac6454SAndrew Thompson 		if (udev->speed != USB_SPEED_HIGH) {
114702ac6454SAndrew Thompson 			goto tr_stalled;
114802ac6454SAndrew Thompson 		}
114902ac6454SAndrew Thompson 	case UDESC_CONFIG:
1150a593f6b8SAndrew Thompson 		buf = usb_temp_get_config_desc(udev,
115102ac6454SAndrew Thompson 		    &len, req->wValue[0]);
115202ac6454SAndrew Thompson 		goto tr_valid;
115302ac6454SAndrew Thompson 	case UDESC_STRING:
1154a593f6b8SAndrew Thompson 		buf = usb_temp_get_string_desc(udev,
115502ac6454SAndrew Thompson 		    UGETW(req->wIndex), req->wValue[0]);
115602ac6454SAndrew Thompson 		goto tr_valid;
115702ac6454SAndrew Thompson 	default:
115802ac6454SAndrew Thompson 		goto tr_stalled;
115902ac6454SAndrew Thompson 	}
116002ac6454SAndrew Thompson 
116102ac6454SAndrew Thompson tr_handle_get_class_descriptor:
116202ac6454SAndrew Thompson 	if (req->wValue[0]) {
116302ac6454SAndrew Thompson 		goto tr_stalled;
116402ac6454SAndrew Thompson 	}
1165a593f6b8SAndrew Thompson 	buf = usb_temp_get_hub_desc(udev);
116602ac6454SAndrew Thompson 	goto tr_valid;
116702ac6454SAndrew Thompson 
116802ac6454SAndrew Thompson tr_valid:
11699a8e0122SAndrew Thompson 	if (buf == NULL)
117002ac6454SAndrew Thompson 		goto tr_stalled;
11719a8e0122SAndrew Thompson 	if (len == 0)
117202ac6454SAndrew Thompson 		len = buf[0];
117302ac6454SAndrew Thompson 	*pPtr = buf;
117402ac6454SAndrew Thompson 	*pLength = len;
1175459d369eSAndrew Thompson 	return (0);	/* success */
117602ac6454SAndrew Thompson 
117702ac6454SAndrew Thompson tr_stalled:
11789a8e0122SAndrew Thompson 	/* try to get a vendor specific descriptor */
11799a8e0122SAndrew Thompson 	len = 0;
11809a8e0122SAndrew Thompson 	buf = usb_temp_get_vendor_desc(udev, req, &len);
11819a8e0122SAndrew Thompson 	if (buf != NULL)
11829a8e0122SAndrew Thompson 		goto tr_valid;
118302ac6454SAndrew Thompson 	*pPtr = NULL;
118402ac6454SAndrew Thompson 	*pLength = 0;
1185459d369eSAndrew Thompson 	return (0);	/* we ignore failures */
118602ac6454SAndrew Thompson }
118702ac6454SAndrew Thompson 
118802ac6454SAndrew Thompson /*------------------------------------------------------------------------*
1189760bc48eSAndrew Thompson  *	usb_temp_setup
119002ac6454SAndrew Thompson  *
119102ac6454SAndrew Thompson  * This function generates USB descriptors according to the given USB
119202ac6454SAndrew Thompson  * template device descriptor. It will also try to figure out the best
119302ac6454SAndrew Thompson  * matching endpoint addresses using the hardware endpoint profiles.
119402ac6454SAndrew Thompson  *
119502ac6454SAndrew Thompson  * Returns:
119602ac6454SAndrew Thompson  *    0: Success
119702ac6454SAndrew Thompson  * Else: Failure
119802ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
11999a8e0122SAndrew Thompson usb_error_t
1200760bc48eSAndrew Thompson usb_temp_setup(struct usb_device *udev,
1201760bc48eSAndrew Thompson     const struct usb_temp_device_desc *tdd)
120202ac6454SAndrew Thompson {
1203760bc48eSAndrew Thompson 	struct usb_temp_setup *uts;
120402ac6454SAndrew Thompson 	void *buf;
120502ac6454SAndrew Thompson 	uint8_t n;
120602ac6454SAndrew Thompson 
120702ac6454SAndrew Thompson 	if (tdd == NULL) {
120802ac6454SAndrew Thompson 		/* be NULL safe */
120902ac6454SAndrew Thompson 		return (0);
121002ac6454SAndrew Thompson 	}
121102ac6454SAndrew Thompson 	uts = udev->bus->scratch[0].temp_setup;
121202ac6454SAndrew Thompson 
121302ac6454SAndrew Thompson 	bzero(uts, sizeof(*uts));
121402ac6454SAndrew Thompson 
12158d2dd5ddSAndrew Thompson 	uts->usb_speed = udev->speed;
121602ac6454SAndrew Thompson 	uts->self_powered = udev->flags.self_powered;
121702ac6454SAndrew Thompson 
121802ac6454SAndrew Thompson 	/* first pass */
121902ac6454SAndrew Thompson 
1220a593f6b8SAndrew Thompson 	usb_make_device_desc(uts, tdd);
122102ac6454SAndrew Thompson 
122202ac6454SAndrew Thompson 	if (uts->err) {
122302ac6454SAndrew Thompson 		/* some error happened */
122402ac6454SAndrew Thompson 		return (uts->err);
122502ac6454SAndrew Thompson 	}
122602ac6454SAndrew Thompson 	/* sanity check */
122702ac6454SAndrew Thompson 	if (uts->size == 0) {
122802ac6454SAndrew Thompson 		return (USB_ERR_INVAL);
122902ac6454SAndrew Thompson 	}
123002ac6454SAndrew Thompson 	/* allocate zeroed memory */
123102ac6454SAndrew Thompson 	uts->buf = malloc(uts->size, M_USB, M_WAITOK | M_ZERO);
123202ac6454SAndrew Thompson 	if (uts->buf == NULL) {
123302ac6454SAndrew Thompson 		/* could not allocate memory */
123402ac6454SAndrew Thompson 		return (USB_ERR_NOMEM);
123502ac6454SAndrew Thompson 	}
123602ac6454SAndrew Thompson 	/* second pass */
123702ac6454SAndrew Thompson 
123802ac6454SAndrew Thompson 	uts->size = 0;
123902ac6454SAndrew Thompson 
1240a593f6b8SAndrew Thompson 	usb_make_device_desc(uts, tdd);
124102ac6454SAndrew Thompson 
124202ac6454SAndrew Thompson 	/*
124302ac6454SAndrew Thompson 	 * Store a pointer to our descriptors:
124402ac6454SAndrew Thompson 	 */
1245a593f6b8SAndrew Thompson 	udev->usb_template_ptr = uts->buf;
124602ac6454SAndrew Thompson 
124702ac6454SAndrew Thompson 	if (uts->err) {
124802ac6454SAndrew Thompson 		/* some error happened during second pass */
124902ac6454SAndrew Thompson 		goto error;
125002ac6454SAndrew Thompson 	}
125102ac6454SAndrew Thompson 	/*
125202ac6454SAndrew Thompson 	 * Resolve all endpoint addresses !
125302ac6454SAndrew Thompson 	 */
1254a593f6b8SAndrew Thompson 	buf = usb_temp_get_device_desc(udev);
1255a593f6b8SAndrew Thompson 	uts->err = usb_hw_ep_resolve(udev, buf);
125602ac6454SAndrew Thompson 	if (uts->err) {
125702ac6454SAndrew Thompson 		DPRINTFN(0, "Could not resolve endpoints for "
125802ac6454SAndrew Thompson 		    "Device Descriptor, error = %s\n",
1259a593f6b8SAndrew Thompson 		    usbd_errstr(uts->err));
126002ac6454SAndrew Thompson 		goto error;
126102ac6454SAndrew Thompson 	}
126202ac6454SAndrew Thompson 	for (n = 0;; n++) {
126302ac6454SAndrew Thompson 
1264a593f6b8SAndrew Thompson 		buf = usb_temp_get_config_desc(udev, NULL, n);
126502ac6454SAndrew Thompson 		if (buf == NULL) {
126602ac6454SAndrew Thompson 			break;
126702ac6454SAndrew Thompson 		}
1268a593f6b8SAndrew Thompson 		uts->err = usb_hw_ep_resolve(udev, buf);
126902ac6454SAndrew Thompson 		if (uts->err) {
127002ac6454SAndrew Thompson 			DPRINTFN(0, "Could not resolve endpoints for "
127102ac6454SAndrew Thompson 			    "Config Descriptor %u, error = %s\n", n,
1272a593f6b8SAndrew Thompson 			    usbd_errstr(uts->err));
127302ac6454SAndrew Thompson 			goto error;
127402ac6454SAndrew Thompson 		}
127502ac6454SAndrew Thompson 	}
127602ac6454SAndrew Thompson 	return (uts->err);
127702ac6454SAndrew Thompson 
127802ac6454SAndrew Thompson error:
1279a593f6b8SAndrew Thompson 	usb_temp_unsetup(udev);
128002ac6454SAndrew Thompson 	return (uts->err);
128102ac6454SAndrew Thompson }
128202ac6454SAndrew Thompson 
128302ac6454SAndrew Thompson /*------------------------------------------------------------------------*
1284a593f6b8SAndrew Thompson  *	usb_temp_unsetup
128502ac6454SAndrew Thompson  *
128602ac6454SAndrew Thompson  * This function frees any memory associated with the currently
128702ac6454SAndrew Thompson  * setup template, if any.
128802ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
12899a8e0122SAndrew Thompson void
1290a593f6b8SAndrew Thompson usb_temp_unsetup(struct usb_device *udev)
129102ac6454SAndrew Thompson {
1292a593f6b8SAndrew Thompson 	if (udev->usb_template_ptr) {
129302ac6454SAndrew Thompson 
1294a593f6b8SAndrew Thompson 		free(udev->usb_template_ptr, M_USB);
129502ac6454SAndrew Thompson 
1296a593f6b8SAndrew Thompson 		udev->usb_template_ptr = NULL;
129702ac6454SAndrew Thompson 	}
129802ac6454SAndrew Thompson }
129902ac6454SAndrew Thompson 
1300e0a69b51SAndrew Thompson static usb_error_t
1301a593f6b8SAndrew Thompson usb_temp_setup_by_index(struct usb_device *udev, uint16_t index)
130202ac6454SAndrew Thompson {
1303e0a69b51SAndrew Thompson 	usb_error_t err;
130402ac6454SAndrew Thompson 
130502ac6454SAndrew Thompson 	switch (index) {
130602ac6454SAndrew Thompson 	case 0:
1307a593f6b8SAndrew Thompson 		err = usb_temp_setup(udev, &usb_template_msc);
130802ac6454SAndrew Thompson 		break;
130902ac6454SAndrew Thompson 	case 1:
1310a593f6b8SAndrew Thompson 		err = usb_temp_setup(udev, &usb_template_cdce);
131102ac6454SAndrew Thompson 		break;
131202ac6454SAndrew Thompson 	case 2:
1313a593f6b8SAndrew Thompson 		err = usb_temp_setup(udev, &usb_template_mtp);
131402ac6454SAndrew Thompson 		break;
131502ac6454SAndrew Thompson 	default:
131602ac6454SAndrew Thompson 		return (USB_ERR_INVAL);
131702ac6454SAndrew Thompson 	}
131802ac6454SAndrew Thompson 
131902ac6454SAndrew Thompson 	return (err);
132002ac6454SAndrew Thompson }
132102ac6454SAndrew Thompson 
132202ac6454SAndrew Thompson static void
1323a593f6b8SAndrew Thompson usb_temp_init(void *arg)
132402ac6454SAndrew Thompson {
132502ac6454SAndrew Thompson 	/* register our functions */
1326a593f6b8SAndrew Thompson 	usb_temp_get_desc_p = &usb_temp_get_desc;
1327a593f6b8SAndrew Thompson 	usb_temp_setup_by_index_p = &usb_temp_setup_by_index;
1328a593f6b8SAndrew Thompson 	usb_temp_unsetup_p = &usb_temp_unsetup;
132902ac6454SAndrew Thompson }
133002ac6454SAndrew Thompson 
1331a593f6b8SAndrew Thompson SYSINIT(usb_temp_init, SI_SUB_LOCK, SI_ORDER_FIRST, usb_temp_init, NULL);
1332a593f6b8SAndrew Thompson SYSUNINIT(usb_temp_unload, SI_SUB_LOCK, SI_ORDER_ANY, usb_temp_unload, NULL);
1333