xref: /freebsd/sys/dev/usb/template/usb_template.c (revision 8e06491a4e5709cf821dc010f349062f7b34c2f4)
102ac6454SAndrew Thompson /* $FreeBSD$ */
202ac6454SAndrew Thompson /*-
3718cf2ccSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4718cf2ccSPedro F. Giffuni  *
502ac6454SAndrew Thompson  * Copyright (c) 2007 Hans Petter Selasky. All rights reserved.
602ac6454SAndrew Thompson  *
702ac6454SAndrew Thompson  * Redistribution and use in source and binary forms, with or without
802ac6454SAndrew Thompson  * modification, are permitted provided that the following conditions
902ac6454SAndrew Thompson  * are met:
1002ac6454SAndrew Thompson  * 1. Redistributions of source code must retain the above copyright
1102ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer.
1202ac6454SAndrew Thompson  * 2. Redistributions in binary form must reproduce the above copyright
1302ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer in the
1402ac6454SAndrew Thompson  *    documentation and/or other materials provided with the distribution.
1502ac6454SAndrew Thompson  *
1602ac6454SAndrew Thompson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1702ac6454SAndrew Thompson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1802ac6454SAndrew Thompson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1902ac6454SAndrew Thompson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2002ac6454SAndrew Thompson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2102ac6454SAndrew Thompson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2202ac6454SAndrew Thompson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2302ac6454SAndrew Thompson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2402ac6454SAndrew Thompson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2502ac6454SAndrew Thompson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2602ac6454SAndrew Thompson  * SUCH DAMAGE.
2702ac6454SAndrew Thompson  */
2802ac6454SAndrew Thompson 
2902ac6454SAndrew Thompson /*
3002ac6454SAndrew Thompson  * This file contains sub-routines to build up USB descriptors from
3102ac6454SAndrew Thompson  * USB templates.
3202ac6454SAndrew Thompson  */
3302ac6454SAndrew Thompson 
34d2b99310SHans Petter Selasky #ifdef USB_GLOBAL_INCLUDE_FILE
35d2b99310SHans Petter Selasky #include USB_GLOBAL_INCLUDE_FILE
36d2b99310SHans Petter Selasky #else
37ed6d949aSAndrew Thompson #include <sys/stdint.h>
38ed6d949aSAndrew Thompson #include <sys/stddef.h>
39ed6d949aSAndrew Thompson #include <sys/param.h>
40ed6d949aSAndrew Thompson #include <sys/queue.h>
41ed6d949aSAndrew Thompson #include <sys/types.h>
42ed6d949aSAndrew Thompson #include <sys/systm.h>
43ed6d949aSAndrew Thompson #include <sys/kernel.h>
44ed6d949aSAndrew Thompson #include <sys/bus.h>
45ed6d949aSAndrew Thompson #include <sys/module.h>
46ed6d949aSAndrew Thompson #include <sys/lock.h>
47ed6d949aSAndrew Thompson #include <sys/mutex.h>
48ed6d949aSAndrew Thompson #include <sys/condvar.h>
49ed6d949aSAndrew Thompson #include <sys/sysctl.h>
50ed6d949aSAndrew Thompson #include <sys/sx.h>
51ed6d949aSAndrew Thompson #include <sys/unistd.h>
52ed6d949aSAndrew Thompson #include <sys/callout.h>
53ed6d949aSAndrew Thompson #include <sys/malloc.h>
54ed6d949aSAndrew Thompson #include <sys/priv.h>
55ed6d949aSAndrew Thompson 
5602ac6454SAndrew Thompson #include <dev/usb/usb.h>
57399e6543SHans Petter Selasky #include <dev/usb/usb_ioctl.h>
58ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h>
59ed6d949aSAndrew Thompson #include <dev/usb/usbdi_util.h>
60ed6d949aSAndrew Thompson #include "usbdevs.h"
61ed6d949aSAndrew Thompson 
6202ac6454SAndrew Thompson #include <dev/usb/usb_cdc.h>
6302ac6454SAndrew Thompson #include <dev/usb/usb_core.h>
64ed6d949aSAndrew Thompson #include <dev/usb/usb_dynamic.h>
6502ac6454SAndrew Thompson #include <dev/usb/usb_busdma.h>
6602ac6454SAndrew Thompson #include <dev/usb/usb_process.h>
6702ac6454SAndrew Thompson #include <dev/usb/usb_device.h>
68*8e06491aSEdward Tomasz Napierala #include <dev/usb/usb_util.h>
69ed6d949aSAndrew Thompson 
70ed6d949aSAndrew Thompson #define	USB_DEBUG_VAR usb_debug
71ed6d949aSAndrew Thompson #include <dev/usb/usb_debug.h>
7202ac6454SAndrew Thompson 
7302ac6454SAndrew Thompson #include <dev/usb/usb_controller.h>
7402ac6454SAndrew Thompson #include <dev/usb/usb_bus.h>
752c79a775SHans Petter Selasky #include <dev/usb/usb_request.h>
7602ac6454SAndrew Thompson #include <dev/usb/template/usb_template.h>
77d2b99310SHans Petter Selasky #endif			/* USB_GLOBAL_INCLUDE_FILE */
7802ac6454SAndrew Thompson 
79*8e06491aSEdward Tomasz Napierala SYSCTL_NODE(_hw_usb, OID_AUTO, templates, CTLFLAG_RW, 0,
80*8e06491aSEdward Tomasz Napierala     "USB device side templates");
81*8e06491aSEdward Tomasz Napierala 
8202ac6454SAndrew Thompson MODULE_DEPEND(usb_template, usb, 1, 1, 1);
8302ac6454SAndrew Thompson MODULE_VERSION(usb_template, 1);
8402ac6454SAndrew Thompson 
8502ac6454SAndrew Thompson /* function prototypes */
8602ac6454SAndrew Thompson 
87a593f6b8SAndrew Thompson static void	usb_make_raw_desc(struct usb_temp_setup *, const uint8_t *);
88a593f6b8SAndrew Thompson static void	usb_make_endpoint_desc(struct usb_temp_setup *,
89760bc48eSAndrew Thompson 		    const struct usb_temp_endpoint_desc *);
90a593f6b8SAndrew Thompson static void	usb_make_interface_desc(struct usb_temp_setup *,
91760bc48eSAndrew Thompson 		    const struct usb_temp_interface_desc *);
92a593f6b8SAndrew Thompson static void	usb_make_config_desc(struct usb_temp_setup *,
93760bc48eSAndrew Thompson 		    const struct usb_temp_config_desc *);
94a593f6b8SAndrew Thompson static void	usb_make_device_desc(struct usb_temp_setup *,
95760bc48eSAndrew Thompson 		    const struct usb_temp_device_desc *);
96a593f6b8SAndrew Thompson static uint8_t	usb_hw_ep_match(const struct usb_hw_ep_profile *, uint8_t,
9702ac6454SAndrew Thompson 		    uint8_t);
98a593f6b8SAndrew Thompson static uint8_t	usb_hw_ep_find_match(struct usb_hw_ep_scratch *,
99760bc48eSAndrew Thompson 		    struct usb_hw_ep_scratch_sub *, uint8_t);
100a593f6b8SAndrew Thompson static uint8_t	usb_hw_ep_get_needs(struct usb_hw_ep_scratch *, uint8_t,
10102ac6454SAndrew Thompson 		    uint8_t);
102a593f6b8SAndrew Thompson static usb_error_t usb_hw_ep_resolve(struct usb_device *,
103760bc48eSAndrew Thompson 		    struct usb_descriptor *);
104a593f6b8SAndrew Thompson static const struct usb_temp_device_desc *usb_temp_get_tdd(struct usb_device *);
105a593f6b8SAndrew Thompson static void	*usb_temp_get_device_desc(struct usb_device *);
106a593f6b8SAndrew Thompson static void	*usb_temp_get_qualifier_desc(struct usb_device *);
107a593f6b8SAndrew Thompson static void	*usb_temp_get_config_desc(struct usb_device *, uint16_t *,
10802ac6454SAndrew Thompson 		    uint8_t);
109a593f6b8SAndrew Thompson static const void *usb_temp_get_string_desc(struct usb_device *, uint16_t,
11002ac6454SAndrew Thompson 		    uint8_t);
111a593f6b8SAndrew Thompson static const void *usb_temp_get_vendor_desc(struct usb_device *,
1129a8e0122SAndrew Thompson 		    const struct usb_device_request *, uint16_t *plen);
113a593f6b8SAndrew Thompson static const void *usb_temp_get_hub_desc(struct usb_device *);
114a593f6b8SAndrew Thompson static usb_error_t usb_temp_get_desc(struct usb_device *,
115760bc48eSAndrew Thompson 		    struct usb_device_request *, const void **, uint16_t *);
116a593f6b8SAndrew Thompson static usb_error_t usb_temp_setup_by_index(struct usb_device *,
11702ac6454SAndrew Thompson 		    uint16_t index);
118a593f6b8SAndrew Thompson static void	usb_temp_init(void *);
11902ac6454SAndrew Thompson 
12002ac6454SAndrew Thompson /*------------------------------------------------------------------------*
121*8e06491aSEdward Tomasz Napierala  *	usb_decode_str_desc
122*8e06491aSEdward Tomasz Napierala  *
123*8e06491aSEdward Tomasz Napierala  * Helper function to decode string descriptors into a C string.
124*8e06491aSEdward Tomasz Napierala  *------------------------------------------------------------------------*/
125*8e06491aSEdward Tomasz Napierala void
126*8e06491aSEdward Tomasz Napierala usb_decode_str_desc(struct usb_string_descriptor *sd, char *buf, size_t buflen)
127*8e06491aSEdward Tomasz Napierala {
128*8e06491aSEdward Tomasz Napierala 	size_t i;
129*8e06491aSEdward Tomasz Napierala 
130*8e06491aSEdward Tomasz Napierala 	for (i = 0; i < buflen - 1 && i < sd->bLength / 2; i++)
131*8e06491aSEdward Tomasz Napierala 		buf[i] = UGETW(sd->bString[i]);
132*8e06491aSEdward Tomasz Napierala 
133*8e06491aSEdward Tomasz Napierala 	buf[i] = '\0';
134*8e06491aSEdward Tomasz Napierala }
135*8e06491aSEdward Tomasz Napierala 
136*8e06491aSEdward Tomasz Napierala /*------------------------------------------------------------------------*
137*8e06491aSEdward Tomasz Napierala  *	usb_temp_sysctl
138*8e06491aSEdward Tomasz Napierala  *
139*8e06491aSEdward Tomasz Napierala  * Callback for SYSCTL_PROC(9), to set and retrieve template string
140*8e06491aSEdward Tomasz Napierala  * descriptors.
141*8e06491aSEdward Tomasz Napierala  *------------------------------------------------------------------------*/
142*8e06491aSEdward Tomasz Napierala int
143*8e06491aSEdward Tomasz Napierala usb_temp_sysctl(SYSCTL_HANDLER_ARGS)
144*8e06491aSEdward Tomasz Napierala {
145*8e06491aSEdward Tomasz Napierala 	char buf[128];
146*8e06491aSEdward Tomasz Napierala 	struct usb_string_descriptor *sd = arg1;
147*8e06491aSEdward Tomasz Napierala 	size_t len, sdlen = arg2;
148*8e06491aSEdward Tomasz Napierala 	int error;
149*8e06491aSEdward Tomasz Napierala 
150*8e06491aSEdward Tomasz Napierala 	usb_decode_str_desc(sd, buf, sizeof(buf));
151*8e06491aSEdward Tomasz Napierala 
152*8e06491aSEdward Tomasz Napierala 	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
153*8e06491aSEdward Tomasz Napierala 	if (error != 0 || req->newptr == NULL)
154*8e06491aSEdward Tomasz Napierala 		return (error);
155*8e06491aSEdward Tomasz Napierala 
156*8e06491aSEdward Tomasz Napierala 	len = usb_make_str_desc(sd, sdlen, buf);
157*8e06491aSEdward Tomasz Napierala 	if (len == 0)
158*8e06491aSEdward Tomasz Napierala 		return (EINVAL);
159*8e06491aSEdward Tomasz Napierala 
160*8e06491aSEdward Tomasz Napierala 	return (0);
161*8e06491aSEdward Tomasz Napierala }
162*8e06491aSEdward Tomasz Napierala 
163*8e06491aSEdward Tomasz Napierala 
164*8e06491aSEdward Tomasz Napierala /*------------------------------------------------------------------------*
165a593f6b8SAndrew Thompson  *	usb_make_raw_desc
16602ac6454SAndrew Thompson  *
16702ac6454SAndrew Thompson  * This function will insert a raw USB descriptor into the generated
16802ac6454SAndrew Thompson  * USB configuration.
16902ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
17002ac6454SAndrew Thompson static void
171a593f6b8SAndrew Thompson usb_make_raw_desc(struct usb_temp_setup *temp,
17202ac6454SAndrew Thompson     const uint8_t *raw)
17302ac6454SAndrew Thompson {
17402ac6454SAndrew Thompson 	void *dst;
17502ac6454SAndrew Thompson 	uint8_t len;
17602ac6454SAndrew Thompson 
17702ac6454SAndrew Thompson 	/*
17802ac6454SAndrew Thompson          * The first byte of any USB descriptor gives the length.
17902ac6454SAndrew Thompson          */
18002ac6454SAndrew Thompson 	if (raw) {
18102ac6454SAndrew Thompson 		len = raw[0];
18202ac6454SAndrew Thompson 		if (temp->buf) {
18302ac6454SAndrew Thompson 			dst = USB_ADD_BYTES(temp->buf, temp->size);
18488334428SHans Petter Selasky 			memcpy(dst, raw, len);
18502ac6454SAndrew Thompson 
18602ac6454SAndrew Thompson 			/* check if we have got a CDC union descriptor */
18702ac6454SAndrew Thompson 
188f9478f91SHans Petter Selasky 			if ((raw[0] == sizeof(struct usb_cdc_union_descriptor)) &&
18902ac6454SAndrew Thompson 			    (raw[1] == UDESC_CS_INTERFACE) &&
19002ac6454SAndrew Thompson 			    (raw[2] == UDESCSUB_CDC_UNION)) {
191760bc48eSAndrew Thompson 				struct usb_cdc_union_descriptor *ud = (void *)dst;
19202ac6454SAndrew Thompson 
19302ac6454SAndrew Thompson 				/* update the interface numbers */
19402ac6454SAndrew Thompson 
19502ac6454SAndrew Thompson 				ud->bMasterInterface +=
19602ac6454SAndrew Thompson 				    temp->bInterfaceNumber;
19702ac6454SAndrew Thompson 				ud->bSlaveInterface[0] +=
19802ac6454SAndrew Thompson 				    temp->bInterfaceNumber;
19902ac6454SAndrew Thompson 			}
200399e6543SHans Petter Selasky 
201399e6543SHans Petter Selasky 			/* check if we have got an interface association descriptor */
202399e6543SHans Petter Selasky 
203f9478f91SHans Petter Selasky 			if ((raw[0] == sizeof(struct usb_interface_assoc_descriptor)) &&
204399e6543SHans Petter Selasky 			    (raw[1] == UDESC_IFACE_ASSOC)) {
205399e6543SHans Petter Selasky 				struct usb_interface_assoc_descriptor *iad = (void *)dst;
206399e6543SHans Petter Selasky 
207399e6543SHans Petter Selasky 				/* update the interface number */
208399e6543SHans Petter Selasky 
209399e6543SHans Petter Selasky 				iad->bFirstInterface +=
210399e6543SHans Petter Selasky 				    temp->bInterfaceNumber;
211399e6543SHans Petter Selasky 			}
212399e6543SHans Petter Selasky 
213399e6543SHans Petter Selasky 			/* check if we have got a call management descriptor */
214399e6543SHans Petter Selasky 
215f9478f91SHans Petter Selasky 			if ((raw[0] == sizeof(struct usb_cdc_cm_descriptor)) &&
216399e6543SHans Petter Selasky 			    (raw[1] == UDESC_CS_INTERFACE) &&
217399e6543SHans Petter Selasky 			    (raw[2] == UDESCSUB_CDC_CM)) {
218399e6543SHans Petter Selasky 				struct usb_cdc_cm_descriptor *ccd = (void *)dst;
219399e6543SHans Petter Selasky 
220399e6543SHans Petter Selasky 				/* update the interface number */
221399e6543SHans Petter Selasky 
222399e6543SHans Petter Selasky 				ccd->bDataInterface +=
223399e6543SHans Petter Selasky 				    temp->bInterfaceNumber;
224399e6543SHans Petter Selasky 			}
22502ac6454SAndrew Thompson 		}
22602ac6454SAndrew Thompson 		temp->size += len;
22702ac6454SAndrew Thompson 	}
22802ac6454SAndrew Thompson }
22902ac6454SAndrew Thompson 
23002ac6454SAndrew Thompson /*------------------------------------------------------------------------*
231a593f6b8SAndrew Thompson  *	usb_make_endpoint_desc
23202ac6454SAndrew Thompson  *
23302ac6454SAndrew Thompson  * This function will generate an USB endpoint descriptor from the
23402ac6454SAndrew Thompson  * given USB template endpoint descriptor, which will be inserted into
23502ac6454SAndrew Thompson  * the USB configuration.
23602ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
23702ac6454SAndrew Thompson static void
238a593f6b8SAndrew Thompson usb_make_endpoint_desc(struct usb_temp_setup *temp,
239760bc48eSAndrew Thompson     const struct usb_temp_endpoint_desc *ted)
24002ac6454SAndrew Thompson {
241760bc48eSAndrew Thompson 	struct usb_endpoint_descriptor *ed;
24202ac6454SAndrew Thompson 	const void **rd;
24302ac6454SAndrew Thompson 	uint16_t old_size;
24402ac6454SAndrew Thompson 	uint16_t mps;
2456a268418SAndrew Thompson 	uint8_t ea;			/* Endpoint Address */
2466a268418SAndrew Thompson 	uint8_t et;			/* Endpiont Type */
24702ac6454SAndrew Thompson 
24802ac6454SAndrew Thompson 	/* Reserve memory */
24902ac6454SAndrew Thompson 	old_size = temp->size;
2506a268418SAndrew Thompson 
2516a268418SAndrew Thompson 	ea = (ted->bEndpointAddress & (UE_ADDR | UE_DIR_IN | UE_DIR_OUT));
2526a268418SAndrew Thompson 	et = (ted->bmAttributes & UE_XFERTYPE);
2536a268418SAndrew Thompson 
2546a268418SAndrew Thompson 	if (et == UE_ISOCHRONOUS) {
2556a268418SAndrew Thompson 		/* account for extra byte fields */
2566a268418SAndrew Thompson 		temp->size += sizeof(*ed) + 2;
2576a268418SAndrew Thompson 	} else {
25802ac6454SAndrew Thompson 		temp->size += sizeof(*ed);
2596a268418SAndrew Thompson 	}
26002ac6454SAndrew Thompson 
26102ac6454SAndrew Thompson 	/* Scan all Raw Descriptors first */
26202ac6454SAndrew Thompson 	rd = ted->ppRawDesc;
26302ac6454SAndrew Thompson 	if (rd) {
26402ac6454SAndrew Thompson 		while (*rd) {
265a593f6b8SAndrew Thompson 			usb_make_raw_desc(temp, *rd);
26602ac6454SAndrew Thompson 			rd++;
26702ac6454SAndrew Thompson 		}
26802ac6454SAndrew Thompson 	}
26902ac6454SAndrew Thompson 	if (ted->pPacketSize == NULL) {
27002ac6454SAndrew Thompson 		/* not initialized */
27102ac6454SAndrew Thompson 		temp->err = USB_ERR_INVAL;
27202ac6454SAndrew Thompson 		return;
27302ac6454SAndrew Thompson 	}
2748d2dd5ddSAndrew Thompson 	mps = ted->pPacketSize->mps[temp->usb_speed];
27502ac6454SAndrew Thompson 	if (mps == 0) {
27602ac6454SAndrew Thompson 		/* not initialized */
27702ac6454SAndrew Thompson 		temp->err = USB_ERR_INVAL;
27802ac6454SAndrew Thompson 		return;
27902ac6454SAndrew Thompson 	} else if (mps == UE_ZERO_MPS) {
28002ac6454SAndrew Thompson 		/* escape for Zero Max Packet Size */
28102ac6454SAndrew Thompson 		mps = 0;
28202ac6454SAndrew Thompson 	}
28302ac6454SAndrew Thompson 
28402ac6454SAndrew Thompson 	/*
28502ac6454SAndrew Thompson 	 * Fill out the real USB endpoint descriptor
28602ac6454SAndrew Thompson 	 * in case there is a buffer present:
28702ac6454SAndrew Thompson 	 */
28802ac6454SAndrew Thompson 	if (temp->buf) {
28902ac6454SAndrew Thompson 		ed = USB_ADD_BYTES(temp->buf, old_size);
2906a268418SAndrew Thompson 		if (et == UE_ISOCHRONOUS)
2916a268418SAndrew Thompson 			ed->bLength = sizeof(*ed) + 2;
2926a268418SAndrew Thompson 		else
29302ac6454SAndrew Thompson 			ed->bLength = sizeof(*ed);
29402ac6454SAndrew Thompson 		ed->bDescriptorType = UDESC_ENDPOINT;
29502ac6454SAndrew Thompson 		ed->bEndpointAddress = ea;
29602ac6454SAndrew Thompson 		ed->bmAttributes = ted->bmAttributes;
29702ac6454SAndrew Thompson 		USETW(ed->wMaxPacketSize, mps);
29802ac6454SAndrew Thompson 
29902ac6454SAndrew Thompson 		/* setup bInterval parameter */
30002ac6454SAndrew Thompson 
30102ac6454SAndrew Thompson 		if (ted->pIntervals &&
3028d2dd5ddSAndrew Thompson 		    ted->pIntervals->bInterval[temp->usb_speed]) {
30302ac6454SAndrew Thompson 			ed->bInterval =
3048d2dd5ddSAndrew Thompson 			    ted->pIntervals->bInterval[temp->usb_speed];
30502ac6454SAndrew Thompson 		} else {
30602ac6454SAndrew Thompson 			switch (et) {
30702ac6454SAndrew Thompson 			case UE_BULK:
30802ac6454SAndrew Thompson 			case UE_CONTROL:
30902ac6454SAndrew Thompson 				ed->bInterval = 0;	/* not used */
31002ac6454SAndrew Thompson 				break;
31102ac6454SAndrew Thompson 			case UE_INTERRUPT:
3128d2dd5ddSAndrew Thompson 				switch (temp->usb_speed) {
31302ac6454SAndrew Thompson 				case USB_SPEED_LOW:
31402ac6454SAndrew Thompson 				case USB_SPEED_FULL:
31502ac6454SAndrew Thompson 					ed->bInterval = 1;	/* 1 ms */
31602ac6454SAndrew Thompson 					break;
31702ac6454SAndrew Thompson 				default:
318e6ee4f7dSHans Petter Selasky 					ed->bInterval = 4;	/* 1 ms */
31902ac6454SAndrew Thompson 					break;
32002ac6454SAndrew Thompson 				}
32102ac6454SAndrew Thompson 				break;
32202ac6454SAndrew Thompson 			default:	/* UE_ISOCHRONOUS */
3238d2dd5ddSAndrew Thompson 				switch (temp->usb_speed) {
32402ac6454SAndrew Thompson 				case USB_SPEED_LOW:
32502ac6454SAndrew Thompson 				case USB_SPEED_FULL:
32602ac6454SAndrew Thompson 					ed->bInterval = 1;	/* 1 ms */
32702ac6454SAndrew Thompson 					break;
32802ac6454SAndrew Thompson 				default:
32902ac6454SAndrew Thompson 					ed->bInterval = 1;	/* 125 us */
33002ac6454SAndrew Thompson 					break;
33102ac6454SAndrew Thompson 				}
33202ac6454SAndrew Thompson 				break;
33302ac6454SAndrew Thompson 			}
33402ac6454SAndrew Thompson 		}
33502ac6454SAndrew Thompson 	}
33602ac6454SAndrew Thompson 	temp->bNumEndpoints++;
33702ac6454SAndrew Thompson }
33802ac6454SAndrew Thompson 
33902ac6454SAndrew Thompson /*------------------------------------------------------------------------*
340a593f6b8SAndrew Thompson  *	usb_make_interface_desc
34102ac6454SAndrew Thompson  *
34202ac6454SAndrew Thompson  * This function will generate an USB interface descriptor from the
34302ac6454SAndrew Thompson  * given USB template interface descriptor, which will be inserted
34402ac6454SAndrew Thompson  * into the USB configuration.
34502ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
34602ac6454SAndrew Thompson static void
347a593f6b8SAndrew Thompson usb_make_interface_desc(struct usb_temp_setup *temp,
348760bc48eSAndrew Thompson     const struct usb_temp_interface_desc *tid)
34902ac6454SAndrew Thompson {
350760bc48eSAndrew Thompson 	struct usb_interface_descriptor *id;
351760bc48eSAndrew Thompson 	const struct usb_temp_endpoint_desc **ted;
35202ac6454SAndrew Thompson 	const void **rd;
35302ac6454SAndrew Thompson 	uint16_t old_size;
35402ac6454SAndrew Thompson 
35502ac6454SAndrew Thompson 	/* Reserve memory */
35602ac6454SAndrew Thompson 
35702ac6454SAndrew Thompson 	old_size = temp->size;
35802ac6454SAndrew Thompson 	temp->size += sizeof(*id);
35902ac6454SAndrew Thompson 
36002ac6454SAndrew Thompson 	/* Update interface and alternate interface numbers */
36102ac6454SAndrew Thompson 
36202ac6454SAndrew Thompson 	if (tid->isAltInterface == 0) {
36302ac6454SAndrew Thompson 		temp->bAlternateSetting = 0;
36402ac6454SAndrew Thompson 		temp->bInterfaceNumber++;
36502ac6454SAndrew Thompson 	} else {
36602ac6454SAndrew Thompson 		temp->bAlternateSetting++;
36702ac6454SAndrew Thompson 	}
36802ac6454SAndrew Thompson 
36902ac6454SAndrew Thompson 	/* Scan all Raw Descriptors first */
37002ac6454SAndrew Thompson 
37102ac6454SAndrew Thompson 	rd = tid->ppRawDesc;
37202ac6454SAndrew Thompson 
37302ac6454SAndrew Thompson 	if (rd) {
37402ac6454SAndrew Thompson 		while (*rd) {
375a593f6b8SAndrew Thompson 			usb_make_raw_desc(temp, *rd);
37602ac6454SAndrew Thompson 			rd++;
37702ac6454SAndrew Thompson 		}
37802ac6454SAndrew Thompson 	}
37902ac6454SAndrew Thompson 	/* Reset some counters */
38002ac6454SAndrew Thompson 
38102ac6454SAndrew Thompson 	temp->bNumEndpoints = 0;
38202ac6454SAndrew Thompson 
38302ac6454SAndrew Thompson 	/* Scan all Endpoint Descriptors second */
38402ac6454SAndrew Thompson 
38502ac6454SAndrew Thompson 	ted = tid->ppEndpoints;
38602ac6454SAndrew Thompson 	if (ted) {
38702ac6454SAndrew Thompson 		while (*ted) {
388a593f6b8SAndrew Thompson 			usb_make_endpoint_desc(temp, *ted);
38902ac6454SAndrew Thompson 			ted++;
39002ac6454SAndrew Thompson 		}
39102ac6454SAndrew Thompson 	}
39202ac6454SAndrew Thompson 	/*
39302ac6454SAndrew Thompson 	 * Fill out the real USB interface descriptor
39402ac6454SAndrew Thompson 	 * in case there is a buffer present:
39502ac6454SAndrew Thompson 	 */
39602ac6454SAndrew Thompson 	if (temp->buf) {
39702ac6454SAndrew Thompson 		id = USB_ADD_BYTES(temp->buf, old_size);
39802ac6454SAndrew Thompson 		id->bLength = sizeof(*id);
39902ac6454SAndrew Thompson 		id->bDescriptorType = UDESC_INTERFACE;
40002ac6454SAndrew Thompson 		id->bInterfaceNumber = temp->bInterfaceNumber;
40102ac6454SAndrew Thompson 		id->bAlternateSetting = temp->bAlternateSetting;
40202ac6454SAndrew Thompson 		id->bNumEndpoints = temp->bNumEndpoints;
40302ac6454SAndrew Thompson 		id->bInterfaceClass = tid->bInterfaceClass;
40402ac6454SAndrew Thompson 		id->bInterfaceSubClass = tid->bInterfaceSubClass;
40502ac6454SAndrew Thompson 		id->bInterfaceProtocol = tid->bInterfaceProtocol;
40602ac6454SAndrew Thompson 		id->iInterface = tid->iInterface;
40702ac6454SAndrew Thompson 	}
40802ac6454SAndrew Thompson }
40902ac6454SAndrew Thompson 
41002ac6454SAndrew Thompson /*------------------------------------------------------------------------*
411a593f6b8SAndrew Thompson  *	usb_make_config_desc
41202ac6454SAndrew Thompson  *
41302ac6454SAndrew Thompson  * This function will generate an USB config descriptor from the given
41402ac6454SAndrew Thompson  * USB template config descriptor, which will be inserted into the USB
41502ac6454SAndrew Thompson  * configuration.
41602ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
41702ac6454SAndrew Thompson static void
418a593f6b8SAndrew Thompson usb_make_config_desc(struct usb_temp_setup *temp,
419760bc48eSAndrew Thompson     const struct usb_temp_config_desc *tcd)
42002ac6454SAndrew Thompson {
421760bc48eSAndrew Thompson 	struct usb_config_descriptor *cd;
422760bc48eSAndrew Thompson 	const struct usb_temp_interface_desc **tid;
42302ac6454SAndrew Thompson 	uint16_t old_size;
42402ac6454SAndrew Thompson 
42502ac6454SAndrew Thompson 	/* Reserve memory */
42602ac6454SAndrew Thompson 
42702ac6454SAndrew Thompson 	old_size = temp->size;
42802ac6454SAndrew Thompson 	temp->size += sizeof(*cd);
42902ac6454SAndrew Thompson 
43002ac6454SAndrew Thompson 	/* Reset some counters */
43102ac6454SAndrew Thompson 
4326d917491SHans Petter Selasky 	temp->bInterfaceNumber = 0xFF;
43302ac6454SAndrew Thompson 	temp->bAlternateSetting = 0;
43402ac6454SAndrew Thompson 
43502ac6454SAndrew Thompson 	/* Scan all the USB interfaces */
43602ac6454SAndrew Thompson 
43702ac6454SAndrew Thompson 	tid = tcd->ppIfaceDesc;
43802ac6454SAndrew Thompson 	if (tid) {
43902ac6454SAndrew Thompson 		while (*tid) {
440a593f6b8SAndrew Thompson 			usb_make_interface_desc(temp, *tid);
44102ac6454SAndrew Thompson 			tid++;
44202ac6454SAndrew Thompson 		}
44302ac6454SAndrew Thompson 	}
44402ac6454SAndrew Thompson 	/*
44502ac6454SAndrew Thompson 	 * Fill out the real USB config descriptor
44602ac6454SAndrew Thompson 	 * in case there is a buffer present:
44702ac6454SAndrew Thompson 	 */
44802ac6454SAndrew Thompson 	if (temp->buf) {
44902ac6454SAndrew Thompson 		cd = USB_ADD_BYTES(temp->buf, old_size);
45002ac6454SAndrew Thompson 
45102ac6454SAndrew Thompson 		/* compute total size */
45202ac6454SAndrew Thompson 		old_size = temp->size - old_size;
45302ac6454SAndrew Thompson 
45402ac6454SAndrew Thompson 		cd->bLength = sizeof(*cd);
45502ac6454SAndrew Thompson 		cd->bDescriptorType = UDESC_CONFIG;
45602ac6454SAndrew Thompson 		USETW(cd->wTotalLength, old_size);
45702ac6454SAndrew Thompson 		cd->bNumInterface = temp->bInterfaceNumber + 1;
45802ac6454SAndrew Thompson 		cd->bConfigurationValue = temp->bConfigurationValue;
45902ac6454SAndrew Thompson 		cd->iConfiguration = tcd->iConfiguration;
46002ac6454SAndrew Thompson 		cd->bmAttributes = tcd->bmAttributes;
46102ac6454SAndrew Thompson 		cd->bMaxPower = tcd->bMaxPower;
46202ac6454SAndrew Thompson 		cd->bmAttributes |= (UC_REMOTE_WAKEUP | UC_BUS_POWERED);
46302ac6454SAndrew Thompson 
46402ac6454SAndrew Thompson 		if (temp->self_powered) {
46502ac6454SAndrew Thompson 			cd->bmAttributes |= UC_SELF_POWERED;
46602ac6454SAndrew Thompson 		} else {
46702ac6454SAndrew Thompson 			cd->bmAttributes &= ~UC_SELF_POWERED;
46802ac6454SAndrew Thompson 		}
46902ac6454SAndrew Thompson 	}
47002ac6454SAndrew Thompson }
47102ac6454SAndrew Thompson 
47202ac6454SAndrew Thompson /*------------------------------------------------------------------------*
473a593f6b8SAndrew Thompson  *	usb_make_device_desc
47402ac6454SAndrew Thompson  *
47502ac6454SAndrew Thompson  * This function will generate an USB device descriptor from the
47602ac6454SAndrew Thompson  * given USB template device descriptor.
47702ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
47802ac6454SAndrew Thompson static void
479a593f6b8SAndrew Thompson usb_make_device_desc(struct usb_temp_setup *temp,
480760bc48eSAndrew Thompson     const struct usb_temp_device_desc *tdd)
48102ac6454SAndrew Thompson {
482760bc48eSAndrew Thompson 	struct usb_temp_data *utd;
483760bc48eSAndrew Thompson 	const struct usb_temp_config_desc **tcd;
48402ac6454SAndrew Thompson 	uint16_t old_size;
48502ac6454SAndrew Thompson 
48602ac6454SAndrew Thompson 	/* Reserve memory */
48702ac6454SAndrew Thompson 
48802ac6454SAndrew Thompson 	old_size = temp->size;
48902ac6454SAndrew Thompson 	temp->size += sizeof(*utd);
49002ac6454SAndrew Thompson 
49102ac6454SAndrew Thompson 	/* Scan all the USB configs */
49202ac6454SAndrew Thompson 
49302ac6454SAndrew Thompson 	temp->bConfigurationValue = 1;
49402ac6454SAndrew Thompson 	tcd = tdd->ppConfigDesc;
49502ac6454SAndrew Thompson 	if (tcd) {
49602ac6454SAndrew Thompson 		while (*tcd) {
497a593f6b8SAndrew Thompson 			usb_make_config_desc(temp, *tcd);
49802ac6454SAndrew Thompson 			temp->bConfigurationValue++;
49902ac6454SAndrew Thompson 			tcd++;
50002ac6454SAndrew Thompson 		}
50102ac6454SAndrew Thompson 	}
50202ac6454SAndrew Thompson 	/*
50302ac6454SAndrew Thompson 	 * Fill out the real USB device descriptor
50402ac6454SAndrew Thompson 	 * in case there is a buffer present:
50502ac6454SAndrew Thompson 	 */
50602ac6454SAndrew Thompson 
50702ac6454SAndrew Thompson 	if (temp->buf) {
50802ac6454SAndrew Thompson 		utd = USB_ADD_BYTES(temp->buf, old_size);
50902ac6454SAndrew Thompson 
51002ac6454SAndrew Thompson 		/* Store a pointer to our template device descriptor */
51102ac6454SAndrew Thompson 		utd->tdd = tdd;
51202ac6454SAndrew Thompson 
51302ac6454SAndrew Thompson 		/* Fill out USB device descriptor */
51402ac6454SAndrew Thompson 		utd->udd.bLength = sizeof(utd->udd);
51502ac6454SAndrew Thompson 		utd->udd.bDescriptorType = UDESC_DEVICE;
51602ac6454SAndrew Thompson 		utd->udd.bDeviceClass = tdd->bDeviceClass;
51702ac6454SAndrew Thompson 		utd->udd.bDeviceSubClass = tdd->bDeviceSubClass;
51802ac6454SAndrew Thompson 		utd->udd.bDeviceProtocol = tdd->bDeviceProtocol;
51902ac6454SAndrew Thompson 		USETW(utd->udd.idVendor, tdd->idVendor);
52002ac6454SAndrew Thompson 		USETW(utd->udd.idProduct, tdd->idProduct);
52102ac6454SAndrew Thompson 		USETW(utd->udd.bcdDevice, tdd->bcdDevice);
52202ac6454SAndrew Thompson 		utd->udd.iManufacturer = tdd->iManufacturer;
52302ac6454SAndrew Thompson 		utd->udd.iProduct = tdd->iProduct;
52402ac6454SAndrew Thompson 		utd->udd.iSerialNumber = tdd->iSerialNumber;
52502ac6454SAndrew Thompson 		utd->udd.bNumConfigurations = temp->bConfigurationValue - 1;
52602ac6454SAndrew Thompson 
52702ac6454SAndrew Thompson 		/*
52802ac6454SAndrew Thompson 		 * Fill out the USB device qualifier. Pretend that we
52902ac6454SAndrew Thompson 		 * don't support any other speeds by setting
53002ac6454SAndrew Thompson 		 * "bNumConfigurations" equal to zero. That saves us
53102ac6454SAndrew Thompson 		 * generating an extra set of configuration
53202ac6454SAndrew Thompson 		 * descriptors.
53302ac6454SAndrew Thompson 		 */
53402ac6454SAndrew Thompson 		utd->udq.bLength = sizeof(utd->udq);
53502ac6454SAndrew Thompson 		utd->udq.bDescriptorType = UDESC_DEVICE_QUALIFIER;
53602ac6454SAndrew Thompson 		utd->udq.bDeviceClass = tdd->bDeviceClass;
53702ac6454SAndrew Thompson 		utd->udq.bDeviceSubClass = tdd->bDeviceSubClass;
53802ac6454SAndrew Thompson 		utd->udq.bDeviceProtocol = tdd->bDeviceProtocol;
53902ac6454SAndrew Thompson 		utd->udq.bNumConfigurations = 0;
54002ac6454SAndrew Thompson 		USETW(utd->udq.bcdUSB, 0x0200);
54102ac6454SAndrew Thompson 		utd->udq.bMaxPacketSize0 = 0;
54202ac6454SAndrew Thompson 
5438d2dd5ddSAndrew Thompson 		switch (temp->usb_speed) {
54402ac6454SAndrew Thompson 		case USB_SPEED_LOW:
54502ac6454SAndrew Thompson 			USETW(utd->udd.bcdUSB, 0x0110);
54602ac6454SAndrew Thompson 			utd->udd.bMaxPacketSize = 8;
54702ac6454SAndrew Thompson 			break;
54802ac6454SAndrew Thompson 		case USB_SPEED_FULL:
54902ac6454SAndrew Thompson 			USETW(utd->udd.bcdUSB, 0x0110);
55002ac6454SAndrew Thompson 			utd->udd.bMaxPacketSize = 32;
55102ac6454SAndrew Thompson 			break;
55202ac6454SAndrew Thompson 		case USB_SPEED_HIGH:
55302ac6454SAndrew Thompson 			USETW(utd->udd.bcdUSB, 0x0200);
55402ac6454SAndrew Thompson 			utd->udd.bMaxPacketSize = 64;
55502ac6454SAndrew Thompson 			break;
55602ac6454SAndrew Thompson 		case USB_SPEED_VARIABLE:
55702ac6454SAndrew Thompson 			USETW(utd->udd.bcdUSB, 0x0250);
55802ac6454SAndrew Thompson 			utd->udd.bMaxPacketSize = 255;	/* 512 bytes */
55902ac6454SAndrew Thompson 			break;
560399e6543SHans Petter Selasky 		case USB_SPEED_SUPER:
561399e6543SHans Petter Selasky 			USETW(utd->udd.bcdUSB, 0x0300);
562399e6543SHans Petter Selasky 			utd->udd.bMaxPacketSize = 9;	/* 2**9 = 512 bytes */
563399e6543SHans Petter Selasky 			break;
56402ac6454SAndrew Thompson 		default:
56502ac6454SAndrew Thompson 			temp->err = USB_ERR_INVAL;
56602ac6454SAndrew Thompson 			break;
56702ac6454SAndrew Thompson 		}
56802ac6454SAndrew Thompson 	}
56902ac6454SAndrew Thompson }
57002ac6454SAndrew Thompson 
57102ac6454SAndrew Thompson /*------------------------------------------------------------------------*
572a593f6b8SAndrew Thompson  *	usb_hw_ep_match
57302ac6454SAndrew Thompson  *
57402ac6454SAndrew Thompson  * Return values:
57520733245SPedro F. Giffuni  *    0: The endpoint profile does not match the criteria
57620733245SPedro F. Giffuni  * Else: The endpoint profile matches the criteria
57702ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
57802ac6454SAndrew Thompson static uint8_t
579a593f6b8SAndrew Thompson usb_hw_ep_match(const struct usb_hw_ep_profile *pf,
58002ac6454SAndrew Thompson     uint8_t ep_type, uint8_t ep_dir_in)
58102ac6454SAndrew Thompson {
58202ac6454SAndrew Thompson 	if (ep_type == UE_CONTROL) {
58302ac6454SAndrew Thompson 		/* special */
58402ac6454SAndrew Thompson 		return (pf->support_control);
58502ac6454SAndrew Thompson 	}
58602ac6454SAndrew Thompson 	if ((pf->support_in && ep_dir_in) ||
58702ac6454SAndrew Thompson 	    (pf->support_out && !ep_dir_in)) {
58802ac6454SAndrew Thompson 		if ((pf->support_interrupt && (ep_type == UE_INTERRUPT)) ||
58902ac6454SAndrew Thompson 		    (pf->support_isochronous && (ep_type == UE_ISOCHRONOUS)) ||
59002ac6454SAndrew Thompson 		    (pf->support_bulk && (ep_type == UE_BULK))) {
59102ac6454SAndrew Thompson 			return (1);
59202ac6454SAndrew Thompson 		}
59302ac6454SAndrew Thompson 	}
59402ac6454SAndrew Thompson 	return (0);
59502ac6454SAndrew Thompson }
59602ac6454SAndrew Thompson 
59702ac6454SAndrew Thompson /*------------------------------------------------------------------------*
598a593f6b8SAndrew Thompson  *	usb_hw_ep_find_match
59902ac6454SAndrew Thompson  *
60002ac6454SAndrew Thompson  * This function is used to find the best matching endpoint profile
60102ac6454SAndrew Thompson  * for and endpoint belonging to an USB descriptor.
60202ac6454SAndrew Thompson  *
60302ac6454SAndrew Thompson  * Return values:
60402ac6454SAndrew Thompson  *    0: Success. Got a match.
60502ac6454SAndrew Thompson  * Else: Failure. No match.
60602ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
60702ac6454SAndrew Thompson static uint8_t
608a593f6b8SAndrew Thompson usb_hw_ep_find_match(struct usb_hw_ep_scratch *ues,
609760bc48eSAndrew Thompson     struct usb_hw_ep_scratch_sub *ep, uint8_t is_simplex)
61002ac6454SAndrew Thompson {
611760bc48eSAndrew Thompson 	const struct usb_hw_ep_profile *pf;
61202ac6454SAndrew Thompson 	uint16_t distance;
61302ac6454SAndrew Thompson 	uint16_t temp;
61402ac6454SAndrew Thompson 	uint16_t max_frame_size;
61502ac6454SAndrew Thompson 	uint8_t n;
61602ac6454SAndrew Thompson 	uint8_t best_n;
61702ac6454SAndrew Thompson 	uint8_t dir_in;
61802ac6454SAndrew Thompson 	uint8_t dir_out;
61902ac6454SAndrew Thompson 
62002ac6454SAndrew Thompson 	distance = 0xFFFF;
62102ac6454SAndrew Thompson 	best_n = 0;
62202ac6454SAndrew Thompson 
62302ac6454SAndrew Thompson 	if ((!ep->needs_in) && (!ep->needs_out)) {
62402ac6454SAndrew Thompson 		return (0);		/* we are done */
62502ac6454SAndrew Thompson 	}
62602ac6454SAndrew Thompson 	if (ep->needs_ep_type == UE_CONTROL) {
62702ac6454SAndrew Thompson 		dir_in = 1;
62802ac6454SAndrew Thompson 		dir_out = 1;
62902ac6454SAndrew Thompson 	} else {
63002ac6454SAndrew Thompson 		if (ep->needs_in) {
63102ac6454SAndrew Thompson 			dir_in = 1;
63202ac6454SAndrew Thompson 			dir_out = 0;
63302ac6454SAndrew Thompson 		} else {
63402ac6454SAndrew Thompson 			dir_in = 0;
63502ac6454SAndrew Thompson 			dir_out = 1;
63602ac6454SAndrew Thompson 		}
63702ac6454SAndrew Thompson 	}
63802ac6454SAndrew Thompson 
63902ac6454SAndrew Thompson 	for (n = 1; n != (USB_EP_MAX / 2); n++) {
64002ac6454SAndrew Thompson 
64102ac6454SAndrew Thompson 		/* get HW endpoint profile */
64202ac6454SAndrew Thompson 		(ues->methods->get_hw_ep_profile) (ues->udev, &pf, n);
64302ac6454SAndrew Thompson 		if (pf == NULL) {
64402ac6454SAndrew Thompson 			/* end of profiles */
64502ac6454SAndrew Thompson 			break;
64602ac6454SAndrew Thompson 		}
64702ac6454SAndrew Thompson 		/* check if IN-endpoint is reserved */
64802ac6454SAndrew Thompson 		if (dir_in || pf->is_simplex) {
64902ac6454SAndrew Thompson 			if (ues->bmInAlloc[n / 8] & (1 << (n % 8))) {
65002ac6454SAndrew Thompson 				/* mismatch */
65102ac6454SAndrew Thompson 				continue;
65202ac6454SAndrew Thompson 			}
65302ac6454SAndrew Thompson 		}
65402ac6454SAndrew Thompson 		/* check if OUT-endpoint is reserved */
65502ac6454SAndrew Thompson 		if (dir_out || pf->is_simplex) {
65602ac6454SAndrew Thompson 			if (ues->bmOutAlloc[n / 8] & (1 << (n % 8))) {
65702ac6454SAndrew Thompson 				/* mismatch */
65802ac6454SAndrew Thompson 				continue;
65902ac6454SAndrew Thompson 			}
66002ac6454SAndrew Thompson 		}
66102ac6454SAndrew Thompson 		/* check simplex */
66202ac6454SAndrew Thompson 		if (pf->is_simplex == is_simplex) {
66302ac6454SAndrew Thompson 			/* mismatch */
66402ac6454SAndrew Thompson 			continue;
66502ac6454SAndrew Thompson 		}
66602ac6454SAndrew Thompson 		/* check if HW endpoint matches */
667a593f6b8SAndrew Thompson 		if (!usb_hw_ep_match(pf, ep->needs_ep_type, dir_in)) {
66802ac6454SAndrew Thompson 			/* mismatch */
66902ac6454SAndrew Thompson 			continue;
67002ac6454SAndrew Thompson 		}
67102ac6454SAndrew Thompson 		/* get maximum frame size */
67202ac6454SAndrew Thompson 		if (dir_in)
67302ac6454SAndrew Thompson 			max_frame_size = pf->max_in_frame_size;
67402ac6454SAndrew Thompson 		else
67502ac6454SAndrew Thompson 			max_frame_size = pf->max_out_frame_size;
67602ac6454SAndrew Thompson 
67702ac6454SAndrew Thompson 		/* check if we have a matching profile */
67802ac6454SAndrew Thompson 		if (max_frame_size >= ep->max_frame_size) {
67902ac6454SAndrew Thompson 			temp = (max_frame_size - ep->max_frame_size);
68002ac6454SAndrew Thompson 			if (distance > temp) {
68102ac6454SAndrew Thompson 				distance = temp;
68202ac6454SAndrew Thompson 				best_n = n;
68302ac6454SAndrew Thompson 				ep->pf = pf;
68402ac6454SAndrew Thompson 			}
68502ac6454SAndrew Thompson 		}
68602ac6454SAndrew Thompson 	}
68702ac6454SAndrew Thompson 
68802ac6454SAndrew Thompson 	/* see if we got a match */
68902ac6454SAndrew Thompson 	if (best_n != 0) {
69002ac6454SAndrew Thompson 		/* get the correct profile */
69102ac6454SAndrew Thompson 		pf = ep->pf;
69202ac6454SAndrew Thompson 
69302ac6454SAndrew Thompson 		/* reserve IN-endpoint */
69402ac6454SAndrew Thompson 		if (dir_in) {
69502ac6454SAndrew Thompson 			ues->bmInAlloc[best_n / 8] |=
69602ac6454SAndrew Thompson 			    (1 << (best_n % 8));
69702ac6454SAndrew Thompson 			ep->hw_endpoint_in = best_n | UE_DIR_IN;
69802ac6454SAndrew Thompson 			ep->needs_in = 0;
69902ac6454SAndrew Thompson 		}
70002ac6454SAndrew Thompson 		/* reserve OUT-endpoint */
70102ac6454SAndrew Thompson 		if (dir_out) {
70202ac6454SAndrew Thompson 			ues->bmOutAlloc[best_n / 8] |=
70302ac6454SAndrew Thompson 			    (1 << (best_n % 8));
70402ac6454SAndrew Thompson 			ep->hw_endpoint_out = best_n | UE_DIR_OUT;
70502ac6454SAndrew Thompson 			ep->needs_out = 0;
70602ac6454SAndrew Thompson 		}
70702ac6454SAndrew Thompson 		return (0);		/* got a match */
70802ac6454SAndrew Thompson 	}
70902ac6454SAndrew Thompson 	return (1);			/* failure */
71002ac6454SAndrew Thompson }
71102ac6454SAndrew Thompson 
71202ac6454SAndrew Thompson /*------------------------------------------------------------------------*
713a593f6b8SAndrew Thompson  *	usb_hw_ep_get_needs
71402ac6454SAndrew Thompson  *
71502ac6454SAndrew Thompson  * This function will figure out the type and number of endpoints
71602ac6454SAndrew Thompson  * which are needed for an USB configuration.
71702ac6454SAndrew Thompson  *
71802ac6454SAndrew Thompson  * Return values:
71902ac6454SAndrew Thompson  *    0: Success.
72002ac6454SAndrew Thompson  * Else: Failure.
72102ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
72202ac6454SAndrew Thompson static uint8_t
723a593f6b8SAndrew Thompson usb_hw_ep_get_needs(struct usb_hw_ep_scratch *ues,
72402ac6454SAndrew Thompson     uint8_t ep_type, uint8_t is_complete)
72502ac6454SAndrew Thompson {
726760bc48eSAndrew Thompson 	const struct usb_hw_ep_profile *pf;
727760bc48eSAndrew Thompson 	struct usb_hw_ep_scratch_sub *ep_iface;
728760bc48eSAndrew Thompson 	struct usb_hw_ep_scratch_sub *ep_curr;
729760bc48eSAndrew Thompson 	struct usb_hw_ep_scratch_sub *ep_max;
730760bc48eSAndrew Thompson 	struct usb_hw_ep_scratch_sub *ep_end;
731760bc48eSAndrew Thompson 	struct usb_descriptor *desc;
732760bc48eSAndrew Thompson 	struct usb_interface_descriptor *id;
733760bc48eSAndrew Thompson 	struct usb_endpoint_descriptor *ed;
7348d2dd5ddSAndrew Thompson 	enum usb_dev_speed speed;
73502ac6454SAndrew Thompson 	uint16_t wMaxPacketSize;
73602ac6454SAndrew Thompson 	uint16_t temp;
73702ac6454SAndrew Thompson 	uint8_t ep_no;
73802ac6454SAndrew Thompson 
73902ac6454SAndrew Thompson 	ep_iface = ues->ep_max;
74002ac6454SAndrew Thompson 	ep_curr = ues->ep_max;
74102ac6454SAndrew Thompson 	ep_end = ues->ep + USB_EP_MAX;
74202ac6454SAndrew Thompson 	ep_max = ues->ep_max;
74302ac6454SAndrew Thompson 	desc = NULL;
744a593f6b8SAndrew Thompson 	speed = usbd_get_speed(ues->udev);
74502ac6454SAndrew Thompson 
74602ac6454SAndrew Thompson repeat:
74702ac6454SAndrew Thompson 
748a593f6b8SAndrew Thompson 	while ((desc = usb_desc_foreach(ues->cd, desc))) {
74902ac6454SAndrew Thompson 
75002ac6454SAndrew Thompson 		if ((desc->bDescriptorType == UDESC_INTERFACE) &&
75102ac6454SAndrew Thompson 		    (desc->bLength >= sizeof(*id))) {
75202ac6454SAndrew Thompson 
75302ac6454SAndrew Thompson 			id = (void *)desc;
75402ac6454SAndrew Thompson 
75502ac6454SAndrew Thompson 			if (id->bAlternateSetting == 0) {
75602ac6454SAndrew Thompson 				/* going forward */
75702ac6454SAndrew Thompson 				ep_iface = ep_max;
75802ac6454SAndrew Thompson 			} else {
75902ac6454SAndrew Thompson 				/* reset */
76002ac6454SAndrew Thompson 				ep_curr = ep_iface;
76102ac6454SAndrew Thompson 			}
76202ac6454SAndrew Thompson 		}
76302ac6454SAndrew Thompson 		if ((desc->bDescriptorType == UDESC_ENDPOINT) &&
76402ac6454SAndrew Thompson 		    (desc->bLength >= sizeof(*ed))) {
76502ac6454SAndrew Thompson 
76602ac6454SAndrew Thompson 			ed = (void *)desc;
76702ac6454SAndrew Thompson 
76802ac6454SAndrew Thompson 			goto handle_endpoint_desc;
76902ac6454SAndrew Thompson 		}
77002ac6454SAndrew Thompson 	}
77102ac6454SAndrew Thompson 	ues->ep_max = ep_max;
77202ac6454SAndrew Thompson 	return (0);
77302ac6454SAndrew Thompson 
77402ac6454SAndrew Thompson handle_endpoint_desc:
77502ac6454SAndrew Thompson 	temp = (ed->bmAttributes & UE_XFERTYPE);
77602ac6454SAndrew Thompson 
77702ac6454SAndrew Thompson 	if (temp == ep_type) {
77802ac6454SAndrew Thompson 
77902ac6454SAndrew Thompson 		if (ep_curr == ep_end) {
78002ac6454SAndrew Thompson 			/* too many endpoints */
78102ac6454SAndrew Thompson 			return (1);	/* failure */
78202ac6454SAndrew Thompson 		}
78302ac6454SAndrew Thompson 		wMaxPacketSize = UGETW(ed->wMaxPacketSize);
78402ac6454SAndrew Thompson 		if ((wMaxPacketSize & 0xF800) &&
78502ac6454SAndrew Thompson 		    (speed == USB_SPEED_HIGH)) {
78602ac6454SAndrew Thompson 			/* handle packet multiplier */
78702ac6454SAndrew Thompson 			temp = (wMaxPacketSize >> 11) & 3;
78802ac6454SAndrew Thompson 			wMaxPacketSize &= 0x7FF;
78902ac6454SAndrew Thompson 			if (temp == 1) {
79002ac6454SAndrew Thompson 				wMaxPacketSize *= 2;
79102ac6454SAndrew Thompson 			} else {
79202ac6454SAndrew Thompson 				wMaxPacketSize *= 3;
79302ac6454SAndrew Thompson 			}
79402ac6454SAndrew Thompson 		}
79502ac6454SAndrew Thompson 		/*
79602ac6454SAndrew Thompson 		 * Check if we have a fixed endpoint number, else the
79702ac6454SAndrew Thompson 		 * endpoint number is allocated dynamically:
79802ac6454SAndrew Thompson 		 */
79902ac6454SAndrew Thompson 		ep_no = (ed->bEndpointAddress & UE_ADDR);
80002ac6454SAndrew Thompson 		if (ep_no != 0) {
80102ac6454SAndrew Thompson 
80202ac6454SAndrew Thompson 			/* get HW endpoint profile */
80302ac6454SAndrew Thompson 			(ues->methods->get_hw_ep_profile)
80402ac6454SAndrew Thompson 			    (ues->udev, &pf, ep_no);
80502ac6454SAndrew Thompson 			if (pf == NULL) {
80602ac6454SAndrew Thompson 				/* HW profile does not exist - failure */
80702ac6454SAndrew Thompson 				DPRINTFN(0, "Endpoint profile %u "
80802ac6454SAndrew Thompson 				    "does not exist\n", ep_no);
80902ac6454SAndrew Thompson 				return (1);
81002ac6454SAndrew Thompson 			}
81102ac6454SAndrew Thompson 			/* reserve fixed endpoint number */
81202ac6454SAndrew Thompson 			if (ep_type == UE_CONTROL) {
81302ac6454SAndrew Thompson 				ues->bmInAlloc[ep_no / 8] |=
81402ac6454SAndrew Thompson 				    (1 << (ep_no % 8));
81502ac6454SAndrew Thompson 				ues->bmOutAlloc[ep_no / 8] |=
81602ac6454SAndrew Thompson 				    (1 << (ep_no % 8));
81702ac6454SAndrew Thompson 				if ((pf->max_in_frame_size < wMaxPacketSize) ||
81802ac6454SAndrew Thompson 				    (pf->max_out_frame_size < wMaxPacketSize)) {
81902ac6454SAndrew Thompson 					DPRINTFN(0, "Endpoint profile %u "
820767cb2e2SAndrew Thompson 					    "has too small buffer\n", ep_no);
82102ac6454SAndrew Thompson 					return (1);
82202ac6454SAndrew Thompson 				}
82302ac6454SAndrew Thompson 			} else if (ed->bEndpointAddress & UE_DIR_IN) {
82402ac6454SAndrew Thompson 				ues->bmInAlloc[ep_no / 8] |=
82502ac6454SAndrew Thompson 				    (1 << (ep_no % 8));
82602ac6454SAndrew Thompson 				if (pf->max_in_frame_size < wMaxPacketSize) {
82702ac6454SAndrew Thompson 					DPRINTFN(0, "Endpoint profile %u "
828767cb2e2SAndrew Thompson 					    "has too small buffer\n", ep_no);
82902ac6454SAndrew Thompson 					return (1);
83002ac6454SAndrew Thompson 				}
83102ac6454SAndrew Thompson 			} else {
83202ac6454SAndrew Thompson 				ues->bmOutAlloc[ep_no / 8] |=
83302ac6454SAndrew Thompson 				    (1 << (ep_no % 8));
83402ac6454SAndrew Thompson 				if (pf->max_out_frame_size < wMaxPacketSize) {
83502ac6454SAndrew Thompson 					DPRINTFN(0, "Endpoint profile %u "
836767cb2e2SAndrew Thompson 					    "has too small buffer\n", ep_no);
83702ac6454SAndrew Thompson 					return (1);
83802ac6454SAndrew Thompson 				}
83902ac6454SAndrew Thompson 			}
84002ac6454SAndrew Thompson 		} else if (is_complete) {
84102ac6454SAndrew Thompson 
84202ac6454SAndrew Thompson 			/* check if we have enough buffer space */
84302ac6454SAndrew Thompson 			if (wMaxPacketSize >
84402ac6454SAndrew Thompson 			    ep_curr->max_frame_size) {
84502ac6454SAndrew Thompson 				return (1);	/* failure */
84602ac6454SAndrew Thompson 			}
84702ac6454SAndrew Thompson 			if (ed->bEndpointAddress & UE_DIR_IN) {
84802ac6454SAndrew Thompson 				ed->bEndpointAddress =
84902ac6454SAndrew Thompson 				    ep_curr->hw_endpoint_in;
85002ac6454SAndrew Thompson 			} else {
85102ac6454SAndrew Thompson 				ed->bEndpointAddress =
85202ac6454SAndrew Thompson 				    ep_curr->hw_endpoint_out;
85302ac6454SAndrew Thompson 			}
85402ac6454SAndrew Thompson 
85502ac6454SAndrew Thompson 		} else {
85602ac6454SAndrew Thompson 
85702ac6454SAndrew Thompson 			/* compute the maximum frame size */
85802ac6454SAndrew Thompson 			if (ep_curr->max_frame_size < wMaxPacketSize) {
85902ac6454SAndrew Thompson 				ep_curr->max_frame_size = wMaxPacketSize;
86002ac6454SAndrew Thompson 			}
86102ac6454SAndrew Thompson 			if (temp == UE_CONTROL) {
86202ac6454SAndrew Thompson 				ep_curr->needs_in = 1;
86302ac6454SAndrew Thompson 				ep_curr->needs_out = 1;
86402ac6454SAndrew Thompson 			} else {
86502ac6454SAndrew Thompson 				if (ed->bEndpointAddress & UE_DIR_IN) {
86602ac6454SAndrew Thompson 					ep_curr->needs_in = 1;
86702ac6454SAndrew Thompson 				} else {
86802ac6454SAndrew Thompson 					ep_curr->needs_out = 1;
86902ac6454SAndrew Thompson 				}
87002ac6454SAndrew Thompson 			}
87102ac6454SAndrew Thompson 			ep_curr->needs_ep_type = ep_type;
87202ac6454SAndrew Thompson 		}
87302ac6454SAndrew Thompson 
87402ac6454SAndrew Thompson 		ep_curr++;
87502ac6454SAndrew Thompson 		if (ep_max < ep_curr) {
87602ac6454SAndrew Thompson 			ep_max = ep_curr;
87702ac6454SAndrew Thompson 		}
87802ac6454SAndrew Thompson 	}
87902ac6454SAndrew Thompson 	goto repeat;
88002ac6454SAndrew Thompson }
88102ac6454SAndrew Thompson 
88202ac6454SAndrew Thompson /*------------------------------------------------------------------------*
883a593f6b8SAndrew Thompson  *	usb_hw_ep_resolve
88402ac6454SAndrew Thompson  *
88502ac6454SAndrew Thompson  * This function will try to resolve endpoint requirements by the
88602ac6454SAndrew Thompson  * given endpoint profiles that the USB hardware reports.
88702ac6454SAndrew Thompson  *
88802ac6454SAndrew Thompson  * Return values:
88902ac6454SAndrew Thompson  *    0: Success
89002ac6454SAndrew Thompson  * Else: Failure
89102ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
892e0a69b51SAndrew Thompson static usb_error_t
893a593f6b8SAndrew Thompson usb_hw_ep_resolve(struct usb_device *udev,
894760bc48eSAndrew Thompson     struct usb_descriptor *desc)
89502ac6454SAndrew Thompson {
896760bc48eSAndrew Thompson 	struct usb_hw_ep_scratch *ues;
897760bc48eSAndrew Thompson 	struct usb_hw_ep_scratch_sub *ep;
898760bc48eSAndrew Thompson 	const struct usb_hw_ep_profile *pf;
899e892b3feSHans Petter Selasky 	const struct usb_bus_methods *methods;
900760bc48eSAndrew Thompson 	struct usb_device_descriptor *dd;
90102ac6454SAndrew Thompson 	uint16_t mps;
90202ac6454SAndrew Thompson 
9036950c75fSHans Petter Selasky 	if (desc == NULL)
90402ac6454SAndrew Thompson 		return (USB_ERR_INVAL);
9056950c75fSHans Petter Selasky 
90602ac6454SAndrew Thompson 	/* get bus methods */
90702ac6454SAndrew Thompson 	methods = udev->bus->methods;
90802ac6454SAndrew Thompson 
9096950c75fSHans Petter Selasky 	if (methods->get_hw_ep_profile == NULL)
91002ac6454SAndrew Thompson 		return (USB_ERR_INVAL);
9116950c75fSHans Petter Selasky 
91202ac6454SAndrew Thompson 	if (desc->bDescriptorType == UDESC_DEVICE) {
91302ac6454SAndrew Thompson 
9146950c75fSHans Petter Selasky 		if (desc->bLength < sizeof(*dd))
91502ac6454SAndrew Thompson 			return (USB_ERR_INVAL);
9166950c75fSHans Petter Selasky 
91702ac6454SAndrew Thompson 		dd = (void *)desc;
91802ac6454SAndrew Thompson 
91902ac6454SAndrew Thompson 		/* get HW control endpoint 0 profile */
92002ac6454SAndrew Thompson 		(methods->get_hw_ep_profile) (udev, &pf, 0);
92102ac6454SAndrew Thompson 		if (pf == NULL) {
92202ac6454SAndrew Thompson 			return (USB_ERR_INVAL);
92302ac6454SAndrew Thompson 		}
924a593f6b8SAndrew Thompson 		if (!usb_hw_ep_match(pf, UE_CONTROL, 0)) {
92502ac6454SAndrew Thompson 			DPRINTFN(0, "Endpoint 0 does not "
92602ac6454SAndrew Thompson 			    "support control\n");
92702ac6454SAndrew Thompson 			return (USB_ERR_INVAL);
92802ac6454SAndrew Thompson 		}
92902ac6454SAndrew Thompson 		mps = dd->bMaxPacketSize;
93002ac6454SAndrew Thompson 
93102ac6454SAndrew Thompson 		if (udev->speed == USB_SPEED_FULL) {
93202ac6454SAndrew Thompson 			/*
93302ac6454SAndrew Thompson 			 * We can optionally choose another packet size !
93402ac6454SAndrew Thompson 			 */
93502ac6454SAndrew Thompson 			while (1) {
93602ac6454SAndrew Thompson 				/* check if "mps" is ok */
93702ac6454SAndrew Thompson 				if (pf->max_in_frame_size >= mps) {
93802ac6454SAndrew Thompson 					break;
93902ac6454SAndrew Thompson 				}
94002ac6454SAndrew Thompson 				/* reduce maximum packet size */
94102ac6454SAndrew Thompson 				mps /= 2;
94202ac6454SAndrew Thompson 
94302ac6454SAndrew Thompson 				/* check if "mps" is too small */
94402ac6454SAndrew Thompson 				if (mps < 8) {
94502ac6454SAndrew Thompson 					return (USB_ERR_INVAL);
94602ac6454SAndrew Thompson 				}
94702ac6454SAndrew Thompson 			}
94802ac6454SAndrew Thompson 
94902ac6454SAndrew Thompson 			dd->bMaxPacketSize = mps;
95002ac6454SAndrew Thompson 
95102ac6454SAndrew Thompson 		} else {
95202ac6454SAndrew Thompson 			/* We only have one choice */
95302ac6454SAndrew Thompson 			if (mps == 255) {
95402ac6454SAndrew Thompson 				mps = 512;
95502ac6454SAndrew Thompson 			}
95602ac6454SAndrew Thompson 			/* Check if we support the specified wMaxPacketSize */
95702ac6454SAndrew Thompson 			if (pf->max_in_frame_size < mps) {
95802ac6454SAndrew Thompson 				return (USB_ERR_INVAL);
95902ac6454SAndrew Thompson 			}
96002ac6454SAndrew Thompson 		}
96102ac6454SAndrew Thompson 		return (0);		/* success */
96202ac6454SAndrew Thompson 	}
9636950c75fSHans Petter Selasky 	if (desc->bDescriptorType != UDESC_CONFIG)
96402ac6454SAndrew Thompson 		return (USB_ERR_INVAL);
9656950c75fSHans Petter Selasky 	if (desc->bLength < sizeof(*(ues->cd)))
96602ac6454SAndrew Thompson 		return (USB_ERR_INVAL);
9676950c75fSHans Petter Selasky 
9686950c75fSHans Petter Selasky 	ues = udev->scratch.hw_ep_scratch;
96902ac6454SAndrew Thompson 
970271ae033SHans Petter Selasky 	memset(ues, 0, sizeof(*ues));
97102ac6454SAndrew Thompson 
97202ac6454SAndrew Thompson 	ues->ep_max = ues->ep;
97302ac6454SAndrew Thompson 	ues->cd = (void *)desc;
97402ac6454SAndrew Thompson 	ues->methods = methods;
97502ac6454SAndrew Thompson 	ues->udev = udev;
97602ac6454SAndrew Thompson 
97702ac6454SAndrew Thompson 	/* Get all the endpoints we need */
97802ac6454SAndrew Thompson 
979a593f6b8SAndrew Thompson 	if (usb_hw_ep_get_needs(ues, UE_ISOCHRONOUS, 0) ||
980a593f6b8SAndrew Thompson 	    usb_hw_ep_get_needs(ues, UE_INTERRUPT, 0) ||
981a593f6b8SAndrew Thompson 	    usb_hw_ep_get_needs(ues, UE_CONTROL, 0) ||
982a593f6b8SAndrew Thompson 	    usb_hw_ep_get_needs(ues, UE_BULK, 0)) {
98302ac6454SAndrew Thompson 		DPRINTFN(0, "Could not get needs\n");
98402ac6454SAndrew Thompson 		return (USB_ERR_INVAL);
98502ac6454SAndrew Thompson 	}
98602ac6454SAndrew Thompson 	for (ep = ues->ep; ep != ues->ep_max; ep++) {
98702ac6454SAndrew Thompson 
98802ac6454SAndrew Thompson 		while (ep->needs_in || ep->needs_out) {
98902ac6454SAndrew Thompson 
99002ac6454SAndrew Thompson 			/*
99102ac6454SAndrew Thompson 		         * First try to use a simplex endpoint.
99202ac6454SAndrew Thompson 		         * Then try to use a duplex endpoint.
99302ac6454SAndrew Thompson 		         */
994a593f6b8SAndrew Thompson 			if (usb_hw_ep_find_match(ues, ep, 1) &&
995a593f6b8SAndrew Thompson 			    usb_hw_ep_find_match(ues, ep, 0)) {
99602ac6454SAndrew Thompson 				DPRINTFN(0, "Could not find match\n");
99702ac6454SAndrew Thompson 				return (USB_ERR_INVAL);
99802ac6454SAndrew Thompson 			}
99902ac6454SAndrew Thompson 		}
100002ac6454SAndrew Thompson 	}
100102ac6454SAndrew Thompson 
100202ac6454SAndrew Thompson 	ues->ep_max = ues->ep;
100302ac6454SAndrew Thompson 
100402ac6454SAndrew Thompson 	/* Update all endpoint addresses */
100502ac6454SAndrew Thompson 
1006a593f6b8SAndrew Thompson 	if (usb_hw_ep_get_needs(ues, UE_ISOCHRONOUS, 1) ||
1007a593f6b8SAndrew Thompson 	    usb_hw_ep_get_needs(ues, UE_INTERRUPT, 1) ||
1008a593f6b8SAndrew Thompson 	    usb_hw_ep_get_needs(ues, UE_CONTROL, 1) ||
1009a593f6b8SAndrew Thompson 	    usb_hw_ep_get_needs(ues, UE_BULK, 1)) {
101002ac6454SAndrew Thompson 		DPRINTFN(0, "Could not update endpoint address\n");
101102ac6454SAndrew Thompson 		return (USB_ERR_INVAL);
101202ac6454SAndrew Thompson 	}
101302ac6454SAndrew Thompson 	return (0);			/* success */
101402ac6454SAndrew Thompson }
101502ac6454SAndrew Thompson 
101602ac6454SAndrew Thompson /*------------------------------------------------------------------------*
1017a593f6b8SAndrew Thompson  *	usb_temp_get_tdd
101802ac6454SAndrew Thompson  *
101902ac6454SAndrew Thompson  * Returns:
102002ac6454SAndrew Thompson  *  NULL: No USB template device descriptor found.
102102ac6454SAndrew Thompson  *  Else: Pointer to the USB template device descriptor.
102202ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
1023760bc48eSAndrew Thompson static const struct usb_temp_device_desc *
1024a593f6b8SAndrew Thompson usb_temp_get_tdd(struct usb_device *udev)
102502ac6454SAndrew Thompson {
1026a593f6b8SAndrew Thompson 	if (udev->usb_template_ptr == NULL) {
102702ac6454SAndrew Thompson 		return (NULL);
102802ac6454SAndrew Thompson 	}
1029a593f6b8SAndrew Thompson 	return (udev->usb_template_ptr->tdd);
103002ac6454SAndrew Thompson }
103102ac6454SAndrew Thompson 
103202ac6454SAndrew Thompson /*------------------------------------------------------------------------*
1033a593f6b8SAndrew Thompson  *	usb_temp_get_device_desc
103402ac6454SAndrew Thompson  *
103502ac6454SAndrew Thompson  * Returns:
103602ac6454SAndrew Thompson  *  NULL: No USB device descriptor found.
103702ac6454SAndrew Thompson  *  Else: Pointer to USB device descriptor.
103802ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
103902ac6454SAndrew Thompson static void *
1040a593f6b8SAndrew Thompson usb_temp_get_device_desc(struct usb_device *udev)
104102ac6454SAndrew Thompson {
1042760bc48eSAndrew Thompson 	struct usb_device_descriptor *dd;
104302ac6454SAndrew Thompson 
1044a593f6b8SAndrew Thompson 	if (udev->usb_template_ptr == NULL) {
104502ac6454SAndrew Thompson 		return (NULL);
104602ac6454SAndrew Thompson 	}
1047a593f6b8SAndrew Thompson 	dd = &udev->usb_template_ptr->udd;
104802ac6454SAndrew Thompson 	if (dd->bDescriptorType != UDESC_DEVICE) {
104902ac6454SAndrew Thompson 		/* sanity check failed */
105002ac6454SAndrew Thompson 		return (NULL);
105102ac6454SAndrew Thompson 	}
105202ac6454SAndrew Thompson 	return (dd);
105302ac6454SAndrew Thompson }
105402ac6454SAndrew Thompson 
105502ac6454SAndrew Thompson /*------------------------------------------------------------------------*
1056a593f6b8SAndrew Thompson  *	usb_temp_get_qualifier_desc
105702ac6454SAndrew Thompson  *
105802ac6454SAndrew Thompson  * Returns:
105902ac6454SAndrew Thompson  *  NULL: No USB device_qualifier descriptor found.
106002ac6454SAndrew Thompson  *  Else: Pointer to USB device_qualifier descriptor.
106102ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
106202ac6454SAndrew Thompson static void *
1063a593f6b8SAndrew Thompson usb_temp_get_qualifier_desc(struct usb_device *udev)
106402ac6454SAndrew Thompson {
1065760bc48eSAndrew Thompson 	struct usb_device_qualifier *dq;
106602ac6454SAndrew Thompson 
1067a593f6b8SAndrew Thompson 	if (udev->usb_template_ptr == NULL) {
106802ac6454SAndrew Thompson 		return (NULL);
106902ac6454SAndrew Thompson 	}
1070a593f6b8SAndrew Thompson 	dq = &udev->usb_template_ptr->udq;
107102ac6454SAndrew Thompson 	if (dq->bDescriptorType != UDESC_DEVICE_QUALIFIER) {
107202ac6454SAndrew Thompson 		/* sanity check failed */
107302ac6454SAndrew Thompson 		return (NULL);
107402ac6454SAndrew Thompson 	}
107502ac6454SAndrew Thompson 	return (dq);
107602ac6454SAndrew Thompson }
107702ac6454SAndrew Thompson 
107802ac6454SAndrew Thompson /*------------------------------------------------------------------------*
1079a593f6b8SAndrew Thompson  *	usb_temp_get_config_desc
108002ac6454SAndrew Thompson  *
108102ac6454SAndrew Thompson  * Returns:
108202ac6454SAndrew Thompson  *  NULL: No USB config descriptor found.
108302ac6454SAndrew Thompson  *  Else: Pointer to USB config descriptor having index "index".
108402ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
108502ac6454SAndrew Thompson static void *
1086a593f6b8SAndrew Thompson usb_temp_get_config_desc(struct usb_device *udev,
108702ac6454SAndrew Thompson     uint16_t *pLength, uint8_t index)
108802ac6454SAndrew Thompson {
1089760bc48eSAndrew Thompson 	struct usb_device_descriptor *dd;
1090760bc48eSAndrew Thompson 	struct usb_config_descriptor *cd;
109102ac6454SAndrew Thompson 	uint16_t temp;
109202ac6454SAndrew Thompson 
1093a593f6b8SAndrew Thompson 	if (udev->usb_template_ptr == NULL) {
109402ac6454SAndrew Thompson 		return (NULL);
109502ac6454SAndrew Thompson 	}
1096a593f6b8SAndrew Thompson 	dd = &udev->usb_template_ptr->udd;
1097a593f6b8SAndrew Thompson 	cd = (void *)(udev->usb_template_ptr + 1);
109802ac6454SAndrew Thompson 
109902ac6454SAndrew Thompson 	if (index >= dd->bNumConfigurations) {
110002ac6454SAndrew Thompson 		/* out of range */
110102ac6454SAndrew Thompson 		return (NULL);
110202ac6454SAndrew Thompson 	}
110302ac6454SAndrew Thompson 	while (index--) {
110402ac6454SAndrew Thompson 		if (cd->bDescriptorType != UDESC_CONFIG) {
110502ac6454SAndrew Thompson 			/* sanity check failed */
110602ac6454SAndrew Thompson 			return (NULL);
110702ac6454SAndrew Thompson 		}
110802ac6454SAndrew Thompson 		temp = UGETW(cd->wTotalLength);
110902ac6454SAndrew Thompson 		cd = USB_ADD_BYTES(cd, temp);
111002ac6454SAndrew Thompson 	}
111102ac6454SAndrew Thompson 
111202ac6454SAndrew Thompson 	if (pLength) {
111302ac6454SAndrew Thompson 		*pLength = UGETW(cd->wTotalLength);
111402ac6454SAndrew Thompson 	}
111502ac6454SAndrew Thompson 	return (cd);
111602ac6454SAndrew Thompson }
111702ac6454SAndrew Thompson 
111802ac6454SAndrew Thompson /*------------------------------------------------------------------------*
1119a593f6b8SAndrew Thompson  *	usb_temp_get_vendor_desc
112002ac6454SAndrew Thompson  *
112102ac6454SAndrew Thompson  * Returns:
112202ac6454SAndrew Thompson  *  NULL: No vendor descriptor found.
112302ac6454SAndrew Thompson  *  Else: Pointer to a vendor descriptor.
112402ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
112502ac6454SAndrew Thompson static const void *
1126a593f6b8SAndrew Thompson usb_temp_get_vendor_desc(struct usb_device *udev,
11279a8e0122SAndrew Thompson     const struct usb_device_request *req, uint16_t *plen)
112802ac6454SAndrew Thompson {
1129760bc48eSAndrew Thompson 	const struct usb_temp_device_desc *tdd;
113002ac6454SAndrew Thompson 
1131a593f6b8SAndrew Thompson 	tdd = usb_temp_get_tdd(udev);
113202ac6454SAndrew Thompson 	if (tdd == NULL) {
113302ac6454SAndrew Thompson 		return (NULL);
113402ac6454SAndrew Thompson 	}
113502ac6454SAndrew Thompson 	if (tdd->getVendorDesc == NULL) {
113602ac6454SAndrew Thompson 		return (NULL);
113702ac6454SAndrew Thompson 	}
11389a8e0122SAndrew Thompson 	return ((tdd->getVendorDesc) (req, plen));
113902ac6454SAndrew Thompson }
114002ac6454SAndrew Thompson 
114102ac6454SAndrew Thompson /*------------------------------------------------------------------------*
1142a593f6b8SAndrew Thompson  *	usb_temp_get_string_desc
114302ac6454SAndrew Thompson  *
114402ac6454SAndrew Thompson  * Returns:
114502ac6454SAndrew Thompson  *  NULL: No string descriptor found.
114602ac6454SAndrew Thompson  *  Else: Pointer to a string descriptor.
114702ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
114802ac6454SAndrew Thompson static const void *
1149a593f6b8SAndrew Thompson usb_temp_get_string_desc(struct usb_device *udev,
115002ac6454SAndrew Thompson     uint16_t lang_id, uint8_t string_index)
115102ac6454SAndrew Thompson {
1152760bc48eSAndrew Thompson 	const struct usb_temp_device_desc *tdd;
115302ac6454SAndrew Thompson 
1154a593f6b8SAndrew Thompson 	tdd = usb_temp_get_tdd(udev);
115502ac6454SAndrew Thompson 	if (tdd == NULL) {
115602ac6454SAndrew Thompson 		return (NULL);
115702ac6454SAndrew Thompson 	}
115802ac6454SAndrew Thompson 	if (tdd->getStringDesc == NULL) {
115902ac6454SAndrew Thompson 		return (NULL);
116002ac6454SAndrew Thompson 	}
116102ac6454SAndrew Thompson 	return ((tdd->getStringDesc) (lang_id, string_index));
116202ac6454SAndrew Thompson }
116302ac6454SAndrew Thompson 
116402ac6454SAndrew Thompson /*------------------------------------------------------------------------*
1165a593f6b8SAndrew Thompson  *	usb_temp_get_hub_desc
116602ac6454SAndrew Thompson  *
116702ac6454SAndrew Thompson  * Returns:
116802ac6454SAndrew Thompson  *  NULL: No USB HUB descriptor found.
116902ac6454SAndrew Thompson  *  Else: Pointer to a USB HUB descriptor.
117002ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
117102ac6454SAndrew Thompson static const void *
1172a593f6b8SAndrew Thompson usb_temp_get_hub_desc(struct usb_device *udev)
117302ac6454SAndrew Thompson {
117402ac6454SAndrew Thompson 	return (NULL);			/* needs to be implemented */
117502ac6454SAndrew Thompson }
117602ac6454SAndrew Thompson 
117702ac6454SAndrew Thompson /*------------------------------------------------------------------------*
1178a593f6b8SAndrew Thompson  *	usb_temp_get_desc
117902ac6454SAndrew Thompson  *
118002ac6454SAndrew Thompson  * This function is a demultiplexer for local USB device side control
118102ac6454SAndrew Thompson  * endpoint requests.
118202ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
1183e0a69b51SAndrew Thompson static usb_error_t
1184a593f6b8SAndrew Thompson usb_temp_get_desc(struct usb_device *udev, struct usb_device_request *req,
118502ac6454SAndrew Thompson     const void **pPtr, uint16_t *pLength)
118602ac6454SAndrew Thompson {
118702ac6454SAndrew Thompson 	const uint8_t *buf;
118802ac6454SAndrew Thompson 	uint16_t len;
118902ac6454SAndrew Thompson 
119002ac6454SAndrew Thompson 	buf = NULL;
119102ac6454SAndrew Thompson 	len = 0;
119202ac6454SAndrew Thompson 
119302ac6454SAndrew Thompson 	switch (req->bmRequestType) {
119402ac6454SAndrew Thompson 	case UT_READ_DEVICE:
119502ac6454SAndrew Thompson 		switch (req->bRequest) {
119602ac6454SAndrew Thompson 		case UR_GET_DESCRIPTOR:
119702ac6454SAndrew Thompson 			goto tr_handle_get_descriptor;
119802ac6454SAndrew Thompson 		default:
119902ac6454SAndrew Thompson 			goto tr_stalled;
120002ac6454SAndrew Thompson 		}
120102ac6454SAndrew Thompson 	case UT_READ_CLASS_DEVICE:
120202ac6454SAndrew Thompson 		switch (req->bRequest) {
120302ac6454SAndrew Thompson 		case UR_GET_DESCRIPTOR:
120402ac6454SAndrew Thompson 			goto tr_handle_get_class_descriptor;
120502ac6454SAndrew Thompson 		default:
120602ac6454SAndrew Thompson 			goto tr_stalled;
120702ac6454SAndrew Thompson 		}
120802ac6454SAndrew Thompson 	default:
120902ac6454SAndrew Thompson 		goto tr_stalled;
121002ac6454SAndrew Thompson 	}
121102ac6454SAndrew Thompson 
121202ac6454SAndrew Thompson tr_handle_get_descriptor:
121302ac6454SAndrew Thompson 	switch (req->wValue[1]) {
121402ac6454SAndrew Thompson 	case UDESC_DEVICE:
121502ac6454SAndrew Thompson 		if (req->wValue[0]) {
121602ac6454SAndrew Thompson 			goto tr_stalled;
121702ac6454SAndrew Thompson 		}
1218a593f6b8SAndrew Thompson 		buf = usb_temp_get_device_desc(udev);
121902ac6454SAndrew Thompson 		goto tr_valid;
122002ac6454SAndrew Thompson 	case UDESC_DEVICE_QUALIFIER:
122102ac6454SAndrew Thompson 		if (udev->speed != USB_SPEED_HIGH) {
122202ac6454SAndrew Thompson 			goto tr_stalled;
122302ac6454SAndrew Thompson 		}
122402ac6454SAndrew Thompson 		if (req->wValue[0]) {
122502ac6454SAndrew Thompson 			goto tr_stalled;
122602ac6454SAndrew Thompson 		}
1227a593f6b8SAndrew Thompson 		buf = usb_temp_get_qualifier_desc(udev);
122802ac6454SAndrew Thompson 		goto tr_valid;
122902ac6454SAndrew Thompson 	case UDESC_OTHER_SPEED_CONFIGURATION:
123002ac6454SAndrew Thompson 		if (udev->speed != USB_SPEED_HIGH) {
123102ac6454SAndrew Thompson 			goto tr_stalled;
123202ac6454SAndrew Thompson 		}
123302ac6454SAndrew Thompson 	case UDESC_CONFIG:
1234a593f6b8SAndrew Thompson 		buf = usb_temp_get_config_desc(udev,
123502ac6454SAndrew Thompson 		    &len, req->wValue[0]);
123602ac6454SAndrew Thompson 		goto tr_valid;
123702ac6454SAndrew Thompson 	case UDESC_STRING:
1238a593f6b8SAndrew Thompson 		buf = usb_temp_get_string_desc(udev,
123902ac6454SAndrew Thompson 		    UGETW(req->wIndex), req->wValue[0]);
124002ac6454SAndrew Thompson 		goto tr_valid;
124102ac6454SAndrew Thompson 	default:
124202ac6454SAndrew Thompson 		goto tr_stalled;
124302ac6454SAndrew Thompson 	}
124402ac6454SAndrew Thompson 
124502ac6454SAndrew Thompson tr_handle_get_class_descriptor:
124602ac6454SAndrew Thompson 	if (req->wValue[0]) {
124702ac6454SAndrew Thompson 		goto tr_stalled;
124802ac6454SAndrew Thompson 	}
1249a593f6b8SAndrew Thompson 	buf = usb_temp_get_hub_desc(udev);
125002ac6454SAndrew Thompson 	goto tr_valid;
125102ac6454SAndrew Thompson 
125202ac6454SAndrew Thompson tr_valid:
12539a8e0122SAndrew Thompson 	if (buf == NULL)
125402ac6454SAndrew Thompson 		goto tr_stalled;
12559a8e0122SAndrew Thompson 	if (len == 0)
125602ac6454SAndrew Thompson 		len = buf[0];
125702ac6454SAndrew Thompson 	*pPtr = buf;
125802ac6454SAndrew Thompson 	*pLength = len;
1259459d369eSAndrew Thompson 	return (0);	/* success */
126002ac6454SAndrew Thompson 
126102ac6454SAndrew Thompson tr_stalled:
12629a8e0122SAndrew Thompson 	/* try to get a vendor specific descriptor */
12639a8e0122SAndrew Thompson 	len = 0;
12649a8e0122SAndrew Thompson 	buf = usb_temp_get_vendor_desc(udev, req, &len);
12659a8e0122SAndrew Thompson 	if (buf != NULL)
12669a8e0122SAndrew Thompson 		goto tr_valid;
126702ac6454SAndrew Thompson 	*pPtr = NULL;
126802ac6454SAndrew Thompson 	*pLength = 0;
1269459d369eSAndrew Thompson 	return (0);	/* we ignore failures */
127002ac6454SAndrew Thompson }
127102ac6454SAndrew Thompson 
127202ac6454SAndrew Thompson /*------------------------------------------------------------------------*
1273760bc48eSAndrew Thompson  *	usb_temp_setup
127402ac6454SAndrew Thompson  *
127502ac6454SAndrew Thompson  * This function generates USB descriptors according to the given USB
127602ac6454SAndrew Thompson  * template device descriptor. It will also try to figure out the best
127702ac6454SAndrew Thompson  * matching endpoint addresses using the hardware endpoint profiles.
127802ac6454SAndrew Thompson  *
127902ac6454SAndrew Thompson  * Returns:
128002ac6454SAndrew Thompson  *    0: Success
128102ac6454SAndrew Thompson  * Else: Failure
128202ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
12839a8e0122SAndrew Thompson usb_error_t
1284760bc48eSAndrew Thompson usb_temp_setup(struct usb_device *udev,
1285760bc48eSAndrew Thompson     const struct usb_temp_device_desc *tdd)
128602ac6454SAndrew Thompson {
1287760bc48eSAndrew Thompson 	struct usb_temp_setup *uts;
128802ac6454SAndrew Thompson 	void *buf;
12896950c75fSHans Petter Selasky 	usb_error_t error;
129002ac6454SAndrew Thompson 	uint8_t n;
12916950c75fSHans Petter Selasky 	uint8_t do_unlock;
129202ac6454SAndrew Thompson 
129302ac6454SAndrew Thompson 	/* be NULL safe */
12946950c75fSHans Petter Selasky 	if (tdd == NULL)
129502ac6454SAndrew Thompson 		return (0);
12966950c75fSHans Petter Selasky 
12976950c75fSHans Petter Selasky 	/* Protect scratch area */
129864cb5e2aSHans Petter Selasky 	do_unlock = usbd_ctrl_lock(udev);
12996950c75fSHans Petter Selasky 
13006950c75fSHans Petter Selasky 	uts = udev->scratch.temp_setup;
130102ac6454SAndrew Thompson 
1302271ae033SHans Petter Selasky 	memset(uts, 0, sizeof(*uts));
130302ac6454SAndrew Thompson 
13048d2dd5ddSAndrew Thompson 	uts->usb_speed = udev->speed;
130502ac6454SAndrew Thompson 	uts->self_powered = udev->flags.self_powered;
130602ac6454SAndrew Thompson 
130702ac6454SAndrew Thompson 	/* first pass */
130802ac6454SAndrew Thompson 
1309a593f6b8SAndrew Thompson 	usb_make_device_desc(uts, tdd);
131002ac6454SAndrew Thompson 
131102ac6454SAndrew Thompson 	if (uts->err) {
131202ac6454SAndrew Thompson 		/* some error happened */
13136950c75fSHans Petter Selasky 		goto done;
131402ac6454SAndrew Thompson 	}
131502ac6454SAndrew Thompson 	/* sanity check */
131602ac6454SAndrew Thompson 	if (uts->size == 0) {
13176950c75fSHans Petter Selasky 		uts->err = USB_ERR_INVAL;
13186950c75fSHans Petter Selasky 		goto done;
131902ac6454SAndrew Thompson 	}
132002ac6454SAndrew Thompson 	/* allocate zeroed memory */
13212c79a775SHans Petter Selasky 	uts->buf = usbd_alloc_config_desc(udev, uts->size);
13226950c75fSHans Petter Selasky 	/*
13236950c75fSHans Petter Selasky 	 * Allow malloc() to return NULL regardless of M_WAITOK flag.
13246950c75fSHans Petter Selasky 	 * This helps when porting the software to non-FreeBSD
13256950c75fSHans Petter Selasky 	 * systems.
13266950c75fSHans Petter Selasky 	 */
132702ac6454SAndrew Thompson 	if (uts->buf == NULL) {
132802ac6454SAndrew Thompson 		/* could not allocate memory */
13296950c75fSHans Petter Selasky 		uts->err = USB_ERR_NOMEM;
13306950c75fSHans Petter Selasky 		goto done;
133102ac6454SAndrew Thompson 	}
133202ac6454SAndrew Thompson 	/* second pass */
133302ac6454SAndrew Thompson 
133402ac6454SAndrew Thompson 	uts->size = 0;
133502ac6454SAndrew Thompson 
1336a593f6b8SAndrew Thompson 	usb_make_device_desc(uts, tdd);
133702ac6454SAndrew Thompson 
133802ac6454SAndrew Thompson 	/*
133902ac6454SAndrew Thompson 	 * Store a pointer to our descriptors:
134002ac6454SAndrew Thompson 	 */
1341a593f6b8SAndrew Thompson 	udev->usb_template_ptr = uts->buf;
134202ac6454SAndrew Thompson 
134302ac6454SAndrew Thompson 	if (uts->err) {
134402ac6454SAndrew Thompson 		/* some error happened during second pass */
13456950c75fSHans Petter Selasky 		goto done;
134602ac6454SAndrew Thompson 	}
134702ac6454SAndrew Thompson 	/*
134802ac6454SAndrew Thompson 	 * Resolve all endpoint addresses !
134902ac6454SAndrew Thompson 	 */
1350a593f6b8SAndrew Thompson 	buf = usb_temp_get_device_desc(udev);
1351a593f6b8SAndrew Thompson 	uts->err = usb_hw_ep_resolve(udev, buf);
135202ac6454SAndrew Thompson 	if (uts->err) {
135302ac6454SAndrew Thompson 		DPRINTFN(0, "Could not resolve endpoints for "
135402ac6454SAndrew Thompson 		    "Device Descriptor, error = %s\n",
1355a593f6b8SAndrew Thompson 		    usbd_errstr(uts->err));
13566950c75fSHans Petter Selasky 		goto done;
135702ac6454SAndrew Thompson 	}
135802ac6454SAndrew Thompson 	for (n = 0;; n++) {
135902ac6454SAndrew Thompson 
1360a593f6b8SAndrew Thompson 		buf = usb_temp_get_config_desc(udev, NULL, n);
136102ac6454SAndrew Thompson 		if (buf == NULL) {
136202ac6454SAndrew Thompson 			break;
136302ac6454SAndrew Thompson 		}
1364a593f6b8SAndrew Thompson 		uts->err = usb_hw_ep_resolve(udev, buf);
136502ac6454SAndrew Thompson 		if (uts->err) {
136602ac6454SAndrew Thompson 			DPRINTFN(0, "Could not resolve endpoints for "
136702ac6454SAndrew Thompson 			    "Config Descriptor %u, error = %s\n", n,
1368a593f6b8SAndrew Thompson 			    usbd_errstr(uts->err));
13696950c75fSHans Petter Selasky 			goto done;
137002ac6454SAndrew Thompson 		}
137102ac6454SAndrew Thompson 	}
13726950c75fSHans Petter Selasky done:
13736950c75fSHans Petter Selasky 	error = uts->err;
13746950c75fSHans Petter Selasky 	if (error)
1375a593f6b8SAndrew Thompson 		usb_temp_unsetup(udev);
13766950c75fSHans Petter Selasky 	if (do_unlock)
137764cb5e2aSHans Petter Selasky 		usbd_ctrl_unlock(udev);
13786950c75fSHans Petter Selasky 	return (error);
137902ac6454SAndrew Thompson }
138002ac6454SAndrew Thompson 
138102ac6454SAndrew Thompson /*------------------------------------------------------------------------*
1382a593f6b8SAndrew Thompson  *	usb_temp_unsetup
138302ac6454SAndrew Thompson  *
138402ac6454SAndrew Thompson  * This function frees any memory associated with the currently
138502ac6454SAndrew Thompson  * setup template, if any.
138602ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
13879a8e0122SAndrew Thompson void
1388a593f6b8SAndrew Thompson usb_temp_unsetup(struct usb_device *udev)
138902ac6454SAndrew Thompson {
13902c79a775SHans Petter Selasky 	usbd_free_config_desc(udev, udev->usb_template_ptr);
1391a593f6b8SAndrew Thompson 	udev->usb_template_ptr = NULL;
139202ac6454SAndrew Thompson }
139302ac6454SAndrew Thompson 
1394e0a69b51SAndrew Thompson static usb_error_t
1395a593f6b8SAndrew Thompson usb_temp_setup_by_index(struct usb_device *udev, uint16_t index)
139602ac6454SAndrew Thompson {
1397e0a69b51SAndrew Thompson 	usb_error_t err;
139802ac6454SAndrew Thompson 
139902ac6454SAndrew Thompson 	switch (index) {
1400399e6543SHans Petter Selasky 	case USB_TEMP_MSC:
1401a593f6b8SAndrew Thompson 		err = usb_temp_setup(udev, &usb_template_msc);
140202ac6454SAndrew Thompson 		break;
1403399e6543SHans Petter Selasky 	case USB_TEMP_CDCE:
1404a593f6b8SAndrew Thompson 		err = usb_temp_setup(udev, &usb_template_cdce);
140502ac6454SAndrew Thompson 		break;
1406399e6543SHans Petter Selasky 	case USB_TEMP_MTP:
1407a593f6b8SAndrew Thompson 		err = usb_temp_setup(udev, &usb_template_mtp);
140802ac6454SAndrew Thompson 		break;
1409399e6543SHans Petter Selasky 	case USB_TEMP_MODEM:
1410399e6543SHans Petter Selasky 		err = usb_temp_setup(udev, &usb_template_modem);
1411399e6543SHans Petter Selasky 		break;
1412399e6543SHans Petter Selasky 	case USB_TEMP_AUDIO:
1413399e6543SHans Petter Selasky 		err = usb_temp_setup(udev, &usb_template_audio);
1414399e6543SHans Petter Selasky 		break;
1415399e6543SHans Petter Selasky 	case USB_TEMP_KBD:
1416399e6543SHans Petter Selasky 		err = usb_temp_setup(udev, &usb_template_kbd);
1417399e6543SHans Petter Selasky 		break;
1418399e6543SHans Petter Selasky 	case USB_TEMP_MOUSE:
1419399e6543SHans Petter Selasky 		err = usb_temp_setup(udev, &usb_template_mouse);
1420399e6543SHans Petter Selasky 		break;
1421f9478f91SHans Petter Selasky 	case USB_TEMP_PHONE:
1422f9478f91SHans Petter Selasky 		err = usb_temp_setup(udev, &usb_template_phone);
1423f9478f91SHans Petter Selasky 		break;
14243e420a3eSRuslan Bukin 	case USB_TEMP_SERIALNET:
14253e420a3eSRuslan Bukin 		err = usb_temp_setup(udev, &usb_template_serialnet);
14263e420a3eSRuslan Bukin 		break;
14270a76fe7cSHans Petter Selasky 	case USB_TEMP_MIDI:
14280a76fe7cSHans Petter Selasky 		err = usb_temp_setup(udev, &usb_template_midi);
14290a76fe7cSHans Petter Selasky 		break;
143002ac6454SAndrew Thompson 	default:
143102ac6454SAndrew Thompson 		return (USB_ERR_INVAL);
143202ac6454SAndrew Thompson 	}
143302ac6454SAndrew Thompson 
143402ac6454SAndrew Thompson 	return (err);
143502ac6454SAndrew Thompson }
143602ac6454SAndrew Thompson 
143702ac6454SAndrew Thompson static void
1438a593f6b8SAndrew Thompson usb_temp_init(void *arg)
143902ac6454SAndrew Thompson {
144002ac6454SAndrew Thompson 	/* register our functions */
1441a593f6b8SAndrew Thompson 	usb_temp_get_desc_p = &usb_temp_get_desc;
1442a593f6b8SAndrew Thompson 	usb_temp_setup_by_index_p = &usb_temp_setup_by_index;
1443a593f6b8SAndrew Thompson 	usb_temp_unsetup_p = &usb_temp_unsetup;
144402ac6454SAndrew Thompson }
144502ac6454SAndrew Thompson 
1446a593f6b8SAndrew Thompson SYSINIT(usb_temp_init, SI_SUB_LOCK, SI_ORDER_FIRST, usb_temp_init, NULL);
1447a593f6b8SAndrew Thompson SYSUNINIT(usb_temp_unload, SI_SUB_LOCK, SI_ORDER_ANY, usb_temp_unload, NULL);
1448