xref: /dragonfly/sys/bus/u4b/template/usb_template.c (revision 2b3f93ea)
1 /* $FreeBSD: head/sys/dev/usb/template/usb_template.c 259218 2013-12-11 13:20:32Z hselasky $ */
2 /*-
3  * Copyright (c) 2007 Hans Petter Selasky. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 /*
28  * This file contains sub-routines to build up USB descriptors from
29  * USB templates.
30  */
31 
32 #ifdef USB_GLOBAL_INCLUDE_FILE
33 #include USB_GLOBAL_INCLUDE_FILE
34 #else
35 #include <sys/stdint.h>
36 #include <sys/param.h>
37 #include <sys/queue.h>
38 #include <sys/types.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/bus.h>
42 #include <sys/module.h>
43 #include <sys/lock.h>
44 #include <sys/condvar.h>
45 #include <sys/sysctl.h>
46 #include <sys/unistd.h>
47 #include <sys/callout.h>
48 #include <sys/malloc.h>
49 #include <sys/caps.h>
50 
51 #include <bus/u4b/usb.h>
52 #include <bus/u4b/usb_ioctl.h>
53 #include <bus/u4b/usbdi.h>
54 #include <bus/u4b/usbdi_util.h>
55 #include "usbdevs.h"
56 
57 #include <bus/u4b/usb_cdc.h>
58 #include <bus/u4b/usb_core.h>
59 #include <bus/u4b/usb_dynamic.h>
60 #include <bus/u4b/usb_busdma.h>
61 #include <bus/u4b/usb_process.h>
62 #include <bus/u4b/usb_device.h>
63 
64 #define	USB_DEBUG_VAR usb_debug
65 #include <bus/u4b/usb_debug.h>
66 
67 #include <bus/u4b/usb_controller.h>
68 #include <bus/u4b/usb_bus.h>
69 #include <bus/u4b/usb_request.h>
70 #include <bus/u4b/template/usb_template.h>
71 #endif			/* USB_GLOBAL_INCLUDE_FILE */
72 
73 MODULE_DEPEND(usb_template, usb, 1, 1, 1);
74 MODULE_VERSION(usb_template, 1);
75 
76 /* function prototypes */
77 
78 static void	usb_make_raw_desc(struct usb_temp_setup *, const uint8_t *);
79 static void	usb_make_endpoint_desc(struct usb_temp_setup *,
80 		    const struct usb_temp_endpoint_desc *);
81 static void	usb_make_interface_desc(struct usb_temp_setup *,
82 		    const struct usb_temp_interface_desc *);
83 static void	usb_make_config_desc(struct usb_temp_setup *,
84 		    const struct usb_temp_config_desc *);
85 static void	usb_make_device_desc(struct usb_temp_setup *,
86 		    const struct usb_temp_device_desc *);
87 static uint8_t	usb_hw_ep_match(const struct usb_hw_ep_profile *, uint8_t,
88 		    uint8_t);
89 static uint8_t	usb_hw_ep_find_match(struct usb_hw_ep_scratch *,
90 		    struct usb_hw_ep_scratch_sub *, uint8_t);
91 static uint8_t	usb_hw_ep_get_needs(struct usb_hw_ep_scratch *, uint8_t,
92 		    uint8_t);
93 static usb_error_t usb_hw_ep_resolve(struct usb_device *,
94 		    struct usb_descriptor *);
95 static const struct usb_temp_device_desc *usb_temp_get_tdd(struct usb_device *);
96 static void	*usb_temp_get_device_desc(struct usb_device *);
97 static void	*usb_temp_get_qualifier_desc(struct usb_device *);
98 static void	*usb_temp_get_config_desc(struct usb_device *, uint16_t *,
99 		    uint8_t);
100 static const void *usb_temp_get_string_desc(struct usb_device *, uint16_t,
101 		    uint8_t);
102 static const void *usb_temp_get_vendor_desc(struct usb_device *,
103 		    const struct usb_device_request *, uint16_t *plen);
104 static const void *usb_temp_get_hub_desc(struct usb_device *);
105 static usb_error_t usb_temp_get_desc(struct usb_device *,
106 		    struct usb_device_request *, const void **, uint16_t *);
107 static usb_error_t usb_temp_setup_by_index(struct usb_device *,
108 		    uint16_t index);
109 static void	usb_temp_init(void *);
110 
111 /*------------------------------------------------------------------------*
112  *	usb_make_raw_desc
113  *
114  * This function will insert a raw USB descriptor into the generated
115  * USB configuration.
116  *------------------------------------------------------------------------*/
117 static void
usb_make_raw_desc(struct usb_temp_setup * temp,const uint8_t * raw)118 usb_make_raw_desc(struct usb_temp_setup *temp,
119     const uint8_t *raw)
120 {
121 	void *dst;
122 	uint8_t len;
123 
124 	/*
125          * The first byte of any USB descriptor gives the length.
126          */
127 	if (raw) {
128 		len = raw[0];
129 		if (temp->buf) {
130 			dst = USB_ADD_BYTES(temp->buf, temp->size);
131 			memcpy(dst, raw, len);
132 
133 			/* check if we have got a CDC union descriptor */
134 
135 			if ((raw[0] >= sizeof(struct usb_cdc_union_descriptor)) &&
136 			    (raw[1] == UDESC_CS_INTERFACE) &&
137 			    (raw[2] == UDESCSUB_CDC_UNION)) {
138 				struct usb_cdc_union_descriptor *ud = (void *)dst;
139 
140 				/* update the interface numbers */
141 
142 				ud->bMasterInterface +=
143 				    temp->bInterfaceNumber;
144 				ud->bSlaveInterface[0] +=
145 				    temp->bInterfaceNumber;
146 			}
147 
148 			/* check if we have got an interface association descriptor */
149 
150 			if ((raw[0] >= sizeof(struct usb_interface_assoc_descriptor)) &&
151 			    (raw[1] == UDESC_IFACE_ASSOC)) {
152 				struct usb_interface_assoc_descriptor *iad = (void *)dst;
153 
154 				/* update the interface number */
155 
156 				iad->bFirstInterface +=
157 				    temp->bInterfaceNumber;
158 			}
159 
160 			/* check if we have got a call management descriptor */
161 
162 			if ((raw[0] >= sizeof(struct usb_cdc_cm_descriptor)) &&
163 			    (raw[1] == UDESC_CS_INTERFACE) &&
164 			    (raw[2] == UDESCSUB_CDC_CM)) {
165 				struct usb_cdc_cm_descriptor *ccd = (void *)dst;
166 
167 				/* update the interface number */
168 
169 				ccd->bDataInterface +=
170 				    temp->bInterfaceNumber;
171 			}
172 		}
173 		temp->size += len;
174 	}
175 }
176 
177 /*------------------------------------------------------------------------*
178  *	usb_make_endpoint_desc
179  *
180  * This function will generate an USB endpoint descriptor from the
181  * given USB template endpoint descriptor, which will be inserted into
182  * the USB configuration.
183  *------------------------------------------------------------------------*/
184 static void
usb_make_endpoint_desc(struct usb_temp_setup * temp,const struct usb_temp_endpoint_desc * ted)185 usb_make_endpoint_desc(struct usb_temp_setup *temp,
186     const struct usb_temp_endpoint_desc *ted)
187 {
188 	struct usb_endpoint_descriptor *ed;
189 	const void **rd;
190 	uint16_t old_size;
191 	uint16_t mps;
192 	uint8_t ea;			/* Endpoint Address */
193 	uint8_t et;			/* Endpiont Type */
194 
195 	/* Reserve memory */
196 	old_size = temp->size;
197 
198 	ea = (ted->bEndpointAddress & (UE_ADDR | UE_DIR_IN | UE_DIR_OUT));
199 	et = (ted->bmAttributes & UE_XFERTYPE);
200 
201 	if (et == UE_ISOCHRONOUS) {
202 		/* account for extra byte fields */
203 		temp->size += sizeof(*ed) + 2;
204 	} else {
205 		temp->size += sizeof(*ed);
206 	}
207 
208 	/* Scan all Raw Descriptors first */
209 	rd = ted->ppRawDesc;
210 	if (rd) {
211 		while (*rd) {
212 			usb_make_raw_desc(temp, *rd);
213 			rd++;
214 		}
215 	}
216 	if (ted->pPacketSize == NULL) {
217 		/* not initialized */
218 		temp->err = USB_ERR_INVAL;
219 		return;
220 	}
221 	mps = ted->pPacketSize->mps[temp->usb_speed];
222 	if (mps == 0) {
223 		/* not initialized */
224 		temp->err = USB_ERR_INVAL;
225 		return;
226 	} else if (mps == UE_ZERO_MPS) {
227 		/* escape for Zero Max Packet Size */
228 		mps = 0;
229 	}
230 
231 	/*
232 	 * Fill out the real USB endpoint descriptor
233 	 * in case there is a buffer present:
234 	 */
235 	if (temp->buf) {
236 		ed = USB_ADD_BYTES(temp->buf, old_size);
237 		if (et == UE_ISOCHRONOUS)
238 			ed->bLength = sizeof(*ed) + 2;
239 		else
240 			ed->bLength = sizeof(*ed);
241 		ed->bDescriptorType = UDESC_ENDPOINT;
242 		ed->bEndpointAddress = ea;
243 		ed->bmAttributes = ted->bmAttributes;
244 		USETW(ed->wMaxPacketSize, mps);
245 
246 		/* setup bInterval parameter */
247 
248 		if (ted->pIntervals &&
249 		    ted->pIntervals->bInterval[temp->usb_speed]) {
250 			ed->bInterval =
251 			    ted->pIntervals->bInterval[temp->usb_speed];
252 		} else {
253 			switch (et) {
254 			case UE_BULK:
255 			case UE_CONTROL:
256 				ed->bInterval = 0;	/* not used */
257 				break;
258 			case UE_INTERRUPT:
259 				switch (temp->usb_speed) {
260 				case USB_SPEED_LOW:
261 				case USB_SPEED_FULL:
262 					ed->bInterval = 1;	/* 1 ms */
263 					break;
264 				default:
265 					ed->bInterval = 4;	/* 1 ms */
266 					break;
267 				}
268 				break;
269 			default:	/* UE_ISOCHRONOUS */
270 				switch (temp->usb_speed) {
271 				case USB_SPEED_LOW:
272 				case USB_SPEED_FULL:
273 					ed->bInterval = 1;	/* 1 ms */
274 					break;
275 				default:
276 					ed->bInterval = 1;	/* 125 us */
277 					break;
278 				}
279 				break;
280 			}
281 		}
282 	}
283 	temp->bNumEndpoints++;
284 }
285 
286 /*------------------------------------------------------------------------*
287  *	usb_make_interface_desc
288  *
289  * This function will generate an USB interface descriptor from the
290  * given USB template interface descriptor, which will be inserted
291  * into the USB configuration.
292  *------------------------------------------------------------------------*/
293 static void
usb_make_interface_desc(struct usb_temp_setup * temp,const struct usb_temp_interface_desc * tid)294 usb_make_interface_desc(struct usb_temp_setup *temp,
295     const struct usb_temp_interface_desc *tid)
296 {
297 	struct usb_interface_descriptor *id;
298 	const struct usb_temp_endpoint_desc **ted;
299 	const void **rd;
300 	uint16_t old_size;
301 
302 	/* Reserve memory */
303 
304 	old_size = temp->size;
305 	temp->size += sizeof(*id);
306 
307 	/* Update interface and alternate interface numbers */
308 
309 	if (tid->isAltInterface == 0) {
310 		temp->bAlternateSetting = 0;
311 		temp->bInterfaceNumber++;
312 	} else {
313 		temp->bAlternateSetting++;
314 	}
315 
316 	/* Scan all Raw Descriptors first */
317 
318 	rd = tid->ppRawDesc;
319 
320 	if (rd) {
321 		while (*rd) {
322 			usb_make_raw_desc(temp, *rd);
323 			rd++;
324 		}
325 	}
326 	/* Reset some counters */
327 
328 	temp->bNumEndpoints = 0;
329 
330 	/* Scan all Endpoint Descriptors second */
331 
332 	ted = tid->ppEndpoints;
333 	if (ted) {
334 		while (*ted) {
335 			usb_make_endpoint_desc(temp, *ted);
336 			ted++;
337 		}
338 	}
339 	/*
340 	 * Fill out the real USB interface descriptor
341 	 * in case there is a buffer present:
342 	 */
343 	if (temp->buf) {
344 		id = USB_ADD_BYTES(temp->buf, old_size);
345 		id->bLength = sizeof(*id);
346 		id->bDescriptorType = UDESC_INTERFACE;
347 		id->bInterfaceNumber = temp->bInterfaceNumber;
348 		id->bAlternateSetting = temp->bAlternateSetting;
349 		id->bNumEndpoints = temp->bNumEndpoints;
350 		id->bInterfaceClass = tid->bInterfaceClass;
351 		id->bInterfaceSubClass = tid->bInterfaceSubClass;
352 		id->bInterfaceProtocol = tid->bInterfaceProtocol;
353 		id->iInterface = tid->iInterface;
354 	}
355 }
356 
357 /*------------------------------------------------------------------------*
358  *	usb_make_config_desc
359  *
360  * This function will generate an USB config descriptor from the given
361  * USB template config descriptor, which will be inserted into the USB
362  * configuration.
363  *------------------------------------------------------------------------*/
364 static void
usb_make_config_desc(struct usb_temp_setup * temp,const struct usb_temp_config_desc * tcd)365 usb_make_config_desc(struct usb_temp_setup *temp,
366     const struct usb_temp_config_desc *tcd)
367 {
368 	struct usb_config_descriptor *cd;
369 	const struct usb_temp_interface_desc **tid;
370 	uint16_t old_size;
371 
372 	/* Reserve memory */
373 
374 	old_size = temp->size;
375 	temp->size += sizeof(*cd);
376 
377 	/* Reset some counters */
378 
379 	temp->bInterfaceNumber = 0xFF;
380 	temp->bAlternateSetting = 0;
381 
382 	/* Scan all the USB interfaces */
383 
384 	tid = tcd->ppIfaceDesc;
385 	if (tid) {
386 		while (*tid) {
387 			usb_make_interface_desc(temp, *tid);
388 			tid++;
389 		}
390 	}
391 	/*
392 	 * Fill out the real USB config descriptor
393 	 * in case there is a buffer present:
394 	 */
395 	if (temp->buf) {
396 		cd = USB_ADD_BYTES(temp->buf, old_size);
397 
398 		/* compute total size */
399 		old_size = temp->size - old_size;
400 
401 		cd->bLength = sizeof(*cd);
402 		cd->bDescriptorType = UDESC_CONFIG;
403 		USETW(cd->wTotalLength, old_size);
404 		cd->bNumInterface = temp->bInterfaceNumber + 1;
405 		cd->bConfigurationValue = temp->bConfigurationValue;
406 		cd->iConfiguration = tcd->iConfiguration;
407 		cd->bmAttributes = tcd->bmAttributes;
408 		cd->bMaxPower = tcd->bMaxPower;
409 		cd->bmAttributes |= (UC_REMOTE_WAKEUP | UC_BUS_POWERED);
410 
411 		if (temp->self_powered) {
412 			cd->bmAttributes |= UC_SELF_POWERED;
413 		} else {
414 			cd->bmAttributes &= ~UC_SELF_POWERED;
415 		}
416 	}
417 }
418 
419 /*------------------------------------------------------------------------*
420  *	usb_make_device_desc
421  *
422  * This function will generate an USB device descriptor from the
423  * given USB template device descriptor.
424  *------------------------------------------------------------------------*/
425 static void
usb_make_device_desc(struct usb_temp_setup * temp,const struct usb_temp_device_desc * tdd)426 usb_make_device_desc(struct usb_temp_setup *temp,
427     const struct usb_temp_device_desc *tdd)
428 {
429 	struct usb_temp_data *utd;
430 	const struct usb_temp_config_desc **tcd;
431 	uint16_t old_size;
432 
433 	/* Reserve memory */
434 
435 	old_size = temp->size;
436 	temp->size += sizeof(*utd);
437 
438 	/* Scan all the USB configs */
439 
440 	temp->bConfigurationValue = 1;
441 	tcd = tdd->ppConfigDesc;
442 	if (tcd) {
443 		while (*tcd) {
444 			usb_make_config_desc(temp, *tcd);
445 			temp->bConfigurationValue++;
446 			tcd++;
447 		}
448 	}
449 	/*
450 	 * Fill out the real USB device descriptor
451 	 * in case there is a buffer present:
452 	 */
453 
454 	if (temp->buf) {
455 		utd = USB_ADD_BYTES(temp->buf, old_size);
456 
457 		/* Store a pointer to our template device descriptor */
458 		utd->tdd = tdd;
459 
460 		/* Fill out USB device descriptor */
461 		utd->udd.bLength = sizeof(utd->udd);
462 		utd->udd.bDescriptorType = UDESC_DEVICE;
463 		utd->udd.bDeviceClass = tdd->bDeviceClass;
464 		utd->udd.bDeviceSubClass = tdd->bDeviceSubClass;
465 		utd->udd.bDeviceProtocol = tdd->bDeviceProtocol;
466 		USETW(utd->udd.idVendor, tdd->idVendor);
467 		USETW(utd->udd.idProduct, tdd->idProduct);
468 		USETW(utd->udd.bcdDevice, tdd->bcdDevice);
469 		utd->udd.iManufacturer = tdd->iManufacturer;
470 		utd->udd.iProduct = tdd->iProduct;
471 		utd->udd.iSerialNumber = tdd->iSerialNumber;
472 		utd->udd.bNumConfigurations = temp->bConfigurationValue - 1;
473 
474 		/*
475 		 * Fill out the USB device qualifier. Pretend that we
476 		 * don't support any other speeds by setting
477 		 * "bNumConfigurations" equal to zero. That saves us
478 		 * generating an extra set of configuration
479 		 * descriptors.
480 		 */
481 		utd->udq.bLength = sizeof(utd->udq);
482 		utd->udq.bDescriptorType = UDESC_DEVICE_QUALIFIER;
483 		utd->udq.bDeviceClass = tdd->bDeviceClass;
484 		utd->udq.bDeviceSubClass = tdd->bDeviceSubClass;
485 		utd->udq.bDeviceProtocol = tdd->bDeviceProtocol;
486 		utd->udq.bNumConfigurations = 0;
487 		USETW(utd->udq.bcdUSB, 0x0200);
488 		utd->udq.bMaxPacketSize0 = 0;
489 
490 		switch (temp->usb_speed) {
491 		case USB_SPEED_LOW:
492 			USETW(utd->udd.bcdUSB, 0x0110);
493 			utd->udd.bMaxPacketSize = 8;
494 			break;
495 		case USB_SPEED_FULL:
496 			USETW(utd->udd.bcdUSB, 0x0110);
497 			utd->udd.bMaxPacketSize = 32;
498 			break;
499 		case USB_SPEED_HIGH:
500 			USETW(utd->udd.bcdUSB, 0x0200);
501 			utd->udd.bMaxPacketSize = 64;
502 			break;
503 		case USB_SPEED_VARIABLE:
504 			USETW(utd->udd.bcdUSB, 0x0250);
505 			utd->udd.bMaxPacketSize = 255;	/* 512 bytes */
506 			break;
507 		case USB_SPEED_SUPER:
508 			USETW(utd->udd.bcdUSB, 0x0300);
509 			utd->udd.bMaxPacketSize = 9;	/* 2**9 = 512 bytes */
510 			break;
511 		default:
512 			temp->err = USB_ERR_INVAL;
513 			break;
514 		}
515 	}
516 }
517 
518 /*------------------------------------------------------------------------*
519  *	usb_hw_ep_match
520  *
521  * Return values:
522  *    0: The endpoint profile does not match the criterias
523  * Else: The endpoint profile matches the criterias
524  *------------------------------------------------------------------------*/
525 static uint8_t
usb_hw_ep_match(const struct usb_hw_ep_profile * pf,uint8_t ep_type,uint8_t ep_dir_in)526 usb_hw_ep_match(const struct usb_hw_ep_profile *pf,
527     uint8_t ep_type, uint8_t ep_dir_in)
528 {
529 	if (ep_type == UE_CONTROL) {
530 		/* special */
531 		return (pf->support_control);
532 	}
533 	if ((pf->support_in && ep_dir_in) ||
534 	    (pf->support_out && !ep_dir_in)) {
535 		if ((pf->support_interrupt && (ep_type == UE_INTERRUPT)) ||
536 		    (pf->support_isochronous && (ep_type == UE_ISOCHRONOUS)) ||
537 		    (pf->support_bulk && (ep_type == UE_BULK))) {
538 			return (1);
539 		}
540 	}
541 	return (0);
542 }
543 
544 /*------------------------------------------------------------------------*
545  *	usb_hw_ep_find_match
546  *
547  * This function is used to find the best matching endpoint profile
548  * for and endpoint belonging to an USB descriptor.
549  *
550  * Return values:
551  *    0: Success. Got a match.
552  * Else: Failure. No match.
553  *------------------------------------------------------------------------*/
554 static uint8_t
usb_hw_ep_find_match(struct usb_hw_ep_scratch * ues,struct usb_hw_ep_scratch_sub * ep,uint8_t is_simplex)555 usb_hw_ep_find_match(struct usb_hw_ep_scratch *ues,
556     struct usb_hw_ep_scratch_sub *ep, uint8_t is_simplex)
557 {
558 	const struct usb_hw_ep_profile *pf;
559 	uint16_t distance;
560 	uint16_t temp;
561 	uint16_t max_frame_size;
562 	uint8_t n;
563 	uint8_t best_n;
564 	uint8_t dir_in;
565 	uint8_t dir_out;
566 
567 	distance = 0xFFFF;
568 	best_n = 0;
569 
570 	if ((!ep->needs_in) && (!ep->needs_out)) {
571 		return (0);		/* we are done */
572 	}
573 	if (ep->needs_ep_type == UE_CONTROL) {
574 		dir_in = 1;
575 		dir_out = 1;
576 	} else {
577 		if (ep->needs_in) {
578 			dir_in = 1;
579 			dir_out = 0;
580 		} else {
581 			dir_in = 0;
582 			dir_out = 1;
583 		}
584 	}
585 
586 	for (n = 1; n != (USB_EP_MAX / 2); n++) {
587 
588 		/* get HW endpoint profile */
589 		(ues->methods->get_hw_ep_profile) (ues->udev, &pf, n);
590 		if (pf == NULL) {
591 			/* end of profiles */
592 			break;
593 		}
594 		/* check if IN-endpoint is reserved */
595 		if (dir_in || pf->is_simplex) {
596 			if (isset(ues->bmInAlloc, n)) {
597 				/* mismatch */
598 				continue;
599 			}
600 		}
601 		/* check if OUT-endpoint is reserved */
602 		if (dir_out || pf->is_simplex) {
603 			if (isset(ues->bmOutAlloc, n)) {
604 				/* mismatch */
605 				continue;
606 			}
607 		}
608 		/* check simplex */
609 		if (pf->is_simplex == is_simplex) {
610 			/* mismatch */
611 			continue;
612 		}
613 		/* check if HW endpoint matches */
614 		if (!usb_hw_ep_match(pf, ep->needs_ep_type, dir_in)) {
615 			/* mismatch */
616 			continue;
617 		}
618 		/* get maximum frame size */
619 		if (dir_in)
620 			max_frame_size = pf->max_in_frame_size;
621 		else
622 			max_frame_size = pf->max_out_frame_size;
623 
624 		/* check if we have a matching profile */
625 		if (max_frame_size >= ep->max_frame_size) {
626 			temp = (max_frame_size - ep->max_frame_size);
627 			if (distance > temp) {
628 				distance = temp;
629 				best_n = n;
630 				ep->pf = pf;
631 			}
632 		}
633 	}
634 
635 	/* see if we got a match */
636 	if (best_n != 0) {
637 		/* get the correct profile */
638 		pf = ep->pf;
639 
640 		/* reserve IN-endpoint */
641 		if (dir_in) {
642 			ues->bmInAlloc[best_n / 8] |=
643 			    (1 << (best_n % 8));
644 			ep->hw_endpoint_in = best_n | UE_DIR_IN;
645 			ep->needs_in = 0;
646 		}
647 		/* reserve OUT-endpoint */
648 		if (dir_out) {
649 			ues->bmOutAlloc[best_n / 8] |=
650 			    (1 << (best_n % 8));
651 			ep->hw_endpoint_out = best_n | UE_DIR_OUT;
652 			ep->needs_out = 0;
653 		}
654 		return (0);		/* got a match */
655 	}
656 	return (1);			/* failure */
657 }
658 
659 /*------------------------------------------------------------------------*
660  *	usb_hw_ep_get_needs
661  *
662  * This function will figure out the type and number of endpoints
663  * which are needed for an USB configuration.
664  *
665  * Return values:
666  *    0: Success.
667  * Else: Failure.
668  *------------------------------------------------------------------------*/
669 static uint8_t
usb_hw_ep_get_needs(struct usb_hw_ep_scratch * ues,uint8_t ep_type,uint8_t is_complete)670 usb_hw_ep_get_needs(struct usb_hw_ep_scratch *ues,
671     uint8_t ep_type, uint8_t is_complete)
672 {
673 	const struct usb_hw_ep_profile *pf;
674 	struct usb_hw_ep_scratch_sub *ep_iface;
675 	struct usb_hw_ep_scratch_sub *ep_curr;
676 	struct usb_hw_ep_scratch_sub *ep_max;
677 	struct usb_hw_ep_scratch_sub *ep_end;
678 	struct usb_descriptor *desc;
679 	struct usb_interface_descriptor *id;
680 	struct usb_endpoint_descriptor *ed;
681 	enum usb_dev_speed speed;
682 	uint16_t wMaxPacketSize;
683 	uint16_t temp;
684 	uint8_t ep_no;
685 
686 	ep_iface = ues->ep_max;
687 	ep_curr = ues->ep_max;
688 	ep_end = ues->ep + USB_EP_MAX;
689 	ep_max = ues->ep_max;
690 	desc = NULL;
691 	speed = usbd_get_speed(ues->udev);
692 
693 repeat:
694 
695 	while ((desc = usb_desc_foreach(ues->cd, desc))) {
696 
697 		if ((desc->bDescriptorType == UDESC_INTERFACE) &&
698 		    (desc->bLength >= sizeof(*id))) {
699 
700 			id = (void *)desc;
701 
702 			if (id->bAlternateSetting == 0) {
703 				/* going forward */
704 				ep_iface = ep_max;
705 			} else {
706 				/* reset */
707 				ep_curr = ep_iface;
708 			}
709 		}
710 		if ((desc->bDescriptorType == UDESC_ENDPOINT) &&
711 		    (desc->bLength >= sizeof(*ed))) {
712 
713 			ed = (void *)desc;
714 
715 			goto handle_endpoint_desc;
716 		}
717 	}
718 	ues->ep_max = ep_max;
719 	return (0);
720 
721 handle_endpoint_desc:
722 	temp = (ed->bmAttributes & UE_XFERTYPE);
723 
724 	if (temp == ep_type) {
725 
726 		if (ep_curr == ep_end) {
727 			/* too many endpoints */
728 			return (1);	/* failure */
729 		}
730 		wMaxPacketSize = UGETW(ed->wMaxPacketSize);
731 		if ((wMaxPacketSize & 0xF800) &&
732 		    (speed == USB_SPEED_HIGH)) {
733 			/* handle packet multiplier */
734 			temp = (wMaxPacketSize >> 11) & 3;
735 			wMaxPacketSize &= 0x7FF;
736 			if (temp == 1) {
737 				wMaxPacketSize *= 2;
738 			} else {
739 				wMaxPacketSize *= 3;
740 			}
741 		}
742 		/*
743 		 * Check if we have a fixed endpoint number, else the
744 		 * endpoint number is allocated dynamically:
745 		 */
746 		ep_no = (ed->bEndpointAddress & UE_ADDR);
747 		if (ep_no != 0) {
748 
749 			/* get HW endpoint profile */
750 			(ues->methods->get_hw_ep_profile)
751 			    (ues->udev, &pf, ep_no);
752 			if (pf == NULL) {
753 				/* HW profile does not exist - failure */
754 				DPRINTFN(0, "Endpoint profile %u "
755 				    "does not exist\n", ep_no);
756 				return (1);
757 			}
758 			/* reserve fixed endpoint number */
759 			if (ep_type == UE_CONTROL) {
760 				ues->bmInAlloc[ep_no / 8] |=
761 				    (1 << (ep_no % 8));
762 				ues->bmOutAlloc[ep_no / 8] |=
763 				    (1 << (ep_no % 8));
764 				if ((pf->max_in_frame_size < wMaxPacketSize) ||
765 				    (pf->max_out_frame_size < wMaxPacketSize)) {
766 					DPRINTFN(0, "Endpoint profile %u "
767 					    "has too small buffer\n", ep_no);
768 					return (1);
769 				}
770 			} else if (ed->bEndpointAddress & UE_DIR_IN) {
771 				ues->bmInAlloc[ep_no / 8] |=
772 				    (1 << (ep_no % 8));
773 				if (pf->max_in_frame_size < wMaxPacketSize) {
774 					DPRINTFN(0, "Endpoint profile %u "
775 					    "has too small buffer\n", ep_no);
776 					return (1);
777 				}
778 			} else {
779 				ues->bmOutAlloc[ep_no / 8] |=
780 				    (1 << (ep_no % 8));
781 				if (pf->max_out_frame_size < wMaxPacketSize) {
782 					DPRINTFN(0, "Endpoint profile %u "
783 					    "has too small buffer\n", ep_no);
784 					return (1);
785 				}
786 			}
787 		} else if (is_complete) {
788 
789 			/* check if we have enough buffer space */
790 			if (wMaxPacketSize >
791 			    ep_curr->max_frame_size) {
792 				return (1);	/* failure */
793 			}
794 			if (ed->bEndpointAddress & UE_DIR_IN) {
795 				ed->bEndpointAddress =
796 				    ep_curr->hw_endpoint_in;
797 			} else {
798 				ed->bEndpointAddress =
799 				    ep_curr->hw_endpoint_out;
800 			}
801 
802 		} else {
803 
804 			/* compute the maximum frame size */
805 			if (ep_curr->max_frame_size < wMaxPacketSize) {
806 				ep_curr->max_frame_size = wMaxPacketSize;
807 			}
808 			if (temp == UE_CONTROL) {
809 				ep_curr->needs_in = 1;
810 				ep_curr->needs_out = 1;
811 			} else {
812 				if (ed->bEndpointAddress & UE_DIR_IN) {
813 					ep_curr->needs_in = 1;
814 				} else {
815 					ep_curr->needs_out = 1;
816 				}
817 			}
818 			ep_curr->needs_ep_type = ep_type;
819 		}
820 
821 		ep_curr++;
822 		if (ep_max < ep_curr) {
823 			ep_max = ep_curr;
824 		}
825 	}
826 	goto repeat;
827 }
828 
829 /*------------------------------------------------------------------------*
830  *	usb_hw_ep_resolve
831  *
832  * This function will try to resolve endpoint requirements by the
833  * given endpoint profiles that the USB hardware reports.
834  *
835  * Return values:
836  *    0: Success
837  * Else: Failure
838  *------------------------------------------------------------------------*/
839 static usb_error_t
usb_hw_ep_resolve(struct usb_device * udev,struct usb_descriptor * desc)840 usb_hw_ep_resolve(struct usb_device *udev,
841     struct usb_descriptor *desc)
842 {
843 	struct usb_hw_ep_scratch *ues;
844 	struct usb_hw_ep_scratch_sub *ep;
845 	const struct usb_hw_ep_profile *pf;
846 	const struct usb_bus_methods *methods;
847 	struct usb_device_descriptor *dd;
848 	uint16_t mps;
849 
850 	if (desc == NULL)
851 		return (USB_ERR_INVAL);
852 
853 	/* get bus methods */
854 	methods = udev->bus->methods;
855 
856 	if (methods->get_hw_ep_profile == NULL)
857 		return (USB_ERR_INVAL);
858 
859 	if (desc->bDescriptorType == UDESC_DEVICE) {
860 
861 		if (desc->bLength < sizeof(*dd))
862 			return (USB_ERR_INVAL);
863 
864 		dd = (void *)desc;
865 
866 		/* get HW control endpoint 0 profile */
867 		(methods->get_hw_ep_profile) (udev, &pf, 0);
868 		if (pf == NULL) {
869 			return (USB_ERR_INVAL);
870 		}
871 		if (!usb_hw_ep_match(pf, UE_CONTROL, 0)) {
872 			DPRINTFN(0, "Endpoint 0 does not "
873 			    "support control\n");
874 			return (USB_ERR_INVAL);
875 		}
876 		mps = dd->bMaxPacketSize;
877 
878 		if (udev->speed == USB_SPEED_FULL) {
879 			/*
880 			 * We can optionally choose another packet size !
881 			 */
882 			while (1) {
883 				/* check if "mps" is ok */
884 				if (pf->max_in_frame_size >= mps) {
885 					break;
886 				}
887 				/* reduce maximum packet size */
888 				mps /= 2;
889 
890 				/* check if "mps" is too small */
891 				if (mps < 8) {
892 					return (USB_ERR_INVAL);
893 				}
894 			}
895 
896 			dd->bMaxPacketSize = mps;
897 
898 		} else {
899 			/* We only have one choice */
900 			if (mps == 255) {
901 				mps = 512;
902 			}
903 			/* Check if we support the specified wMaxPacketSize */
904 			if (pf->max_in_frame_size < mps) {
905 				return (USB_ERR_INVAL);
906 			}
907 		}
908 		return (0);		/* success */
909 	}
910 	if (desc->bDescriptorType != UDESC_CONFIG)
911 		return (USB_ERR_INVAL);
912 	if (desc->bLength < sizeof(*(ues->cd)))
913 		return (USB_ERR_INVAL);
914 
915 	ues = udev->scratch.hw_ep_scratch;
916 
917 	memset(ues, 0, sizeof(*ues));
918 
919 	ues->ep_max = ues->ep;
920 	ues->cd = (void *)desc;
921 	ues->methods = methods;
922 	ues->udev = udev;
923 
924 	/* Get all the endpoints we need */
925 
926 	if (usb_hw_ep_get_needs(ues, UE_ISOCHRONOUS, 0) ||
927 	    usb_hw_ep_get_needs(ues, UE_INTERRUPT, 0) ||
928 	    usb_hw_ep_get_needs(ues, UE_CONTROL, 0) ||
929 	    usb_hw_ep_get_needs(ues, UE_BULK, 0)) {
930 		DPRINTFN(0, "Could not get needs\n");
931 		return (USB_ERR_INVAL);
932 	}
933 	for (ep = ues->ep; ep != ues->ep_max; ep++) {
934 
935 		while (ep->needs_in || ep->needs_out) {
936 
937 			/*
938 		         * First try to use a simplex endpoint.
939 		         * Then try to use a duplex endpoint.
940 		         */
941 			if (usb_hw_ep_find_match(ues, ep, 1) &&
942 			    usb_hw_ep_find_match(ues, ep, 0)) {
943 				DPRINTFN(0, "Could not find match\n");
944 				return (USB_ERR_INVAL);
945 			}
946 		}
947 	}
948 
949 	ues->ep_max = ues->ep;
950 
951 	/* Update all endpoint addresses */
952 
953 	if (usb_hw_ep_get_needs(ues, UE_ISOCHRONOUS, 1) ||
954 	    usb_hw_ep_get_needs(ues, UE_INTERRUPT, 1) ||
955 	    usb_hw_ep_get_needs(ues, UE_CONTROL, 1) ||
956 	    usb_hw_ep_get_needs(ues, UE_BULK, 1)) {
957 		DPRINTFN(0, "Could not update endpoint address\n");
958 		return (USB_ERR_INVAL);
959 	}
960 	return (0);			/* success */
961 }
962 
963 /*------------------------------------------------------------------------*
964  *	usb_temp_get_tdd
965  *
966  * Returns:
967  *  NULL: No USB template device descriptor found.
968  *  Else: Pointer to the USB template device descriptor.
969  *------------------------------------------------------------------------*/
970 static const struct usb_temp_device_desc *
usb_temp_get_tdd(struct usb_device * udev)971 usb_temp_get_tdd(struct usb_device *udev)
972 {
973 	if (udev->usb_template_ptr == NULL) {
974 		return (NULL);
975 	}
976 	return (udev->usb_template_ptr->tdd);
977 }
978 
979 /*------------------------------------------------------------------------*
980  *	usb_temp_get_device_desc
981  *
982  * Returns:
983  *  NULL: No USB device descriptor found.
984  *  Else: Pointer to USB device descriptor.
985  *------------------------------------------------------------------------*/
986 static void *
usb_temp_get_device_desc(struct usb_device * udev)987 usb_temp_get_device_desc(struct usb_device *udev)
988 {
989 	struct usb_device_descriptor *dd;
990 
991 	if (udev->usb_template_ptr == NULL) {
992 		return (NULL);
993 	}
994 	dd = &udev->usb_template_ptr->udd;
995 	if (dd->bDescriptorType != UDESC_DEVICE) {
996 		/* sanity check failed */
997 		return (NULL);
998 	}
999 	return (dd);
1000 }
1001 
1002 /*------------------------------------------------------------------------*
1003  *	usb_temp_get_qualifier_desc
1004  *
1005  * Returns:
1006  *  NULL: No USB device_qualifier descriptor found.
1007  *  Else: Pointer to USB device_qualifier descriptor.
1008  *------------------------------------------------------------------------*/
1009 static void *
usb_temp_get_qualifier_desc(struct usb_device * udev)1010 usb_temp_get_qualifier_desc(struct usb_device *udev)
1011 {
1012 	struct usb_device_qualifier *dq;
1013 
1014 	if (udev->usb_template_ptr == NULL) {
1015 		return (NULL);
1016 	}
1017 	dq = &udev->usb_template_ptr->udq;
1018 	if (dq->bDescriptorType != UDESC_DEVICE_QUALIFIER) {
1019 		/* sanity check failed */
1020 		return (NULL);
1021 	}
1022 	return (dq);
1023 }
1024 
1025 /*------------------------------------------------------------------------*
1026  *	usb_temp_get_config_desc
1027  *
1028  * Returns:
1029  *  NULL: No USB config descriptor found.
1030  *  Else: Pointer to USB config descriptor having index "index".
1031  *------------------------------------------------------------------------*/
1032 static void *
usb_temp_get_config_desc(struct usb_device * udev,uint16_t * pLength,uint8_t index)1033 usb_temp_get_config_desc(struct usb_device *udev,
1034     uint16_t *pLength, uint8_t index)
1035 {
1036 	struct usb_device_descriptor *dd;
1037 	struct usb_config_descriptor *cd;
1038 	uint16_t temp;
1039 
1040 	if (udev->usb_template_ptr == NULL) {
1041 		return (NULL);
1042 	}
1043 	dd = &udev->usb_template_ptr->udd;
1044 	cd = (void *)(udev->usb_template_ptr + 1);
1045 
1046 	if (index >= dd->bNumConfigurations) {
1047 		/* out of range */
1048 		return (NULL);
1049 	}
1050 	while (index--) {
1051 		if (cd->bDescriptorType != UDESC_CONFIG) {
1052 			/* sanity check failed */
1053 			return (NULL);
1054 		}
1055 		temp = UGETW(cd->wTotalLength);
1056 		cd = USB_ADD_BYTES(cd, temp);
1057 	}
1058 
1059 	if (pLength) {
1060 		*pLength = UGETW(cd->wTotalLength);
1061 	}
1062 	return (cd);
1063 }
1064 
1065 /*------------------------------------------------------------------------*
1066  *	usb_temp_get_vendor_desc
1067  *
1068  * Returns:
1069  *  NULL: No vendor descriptor found.
1070  *  Else: Pointer to a vendor descriptor.
1071  *------------------------------------------------------------------------*/
1072 static const void *
usb_temp_get_vendor_desc(struct usb_device * udev,const struct usb_device_request * req,uint16_t * plen)1073 usb_temp_get_vendor_desc(struct usb_device *udev,
1074     const struct usb_device_request *req, uint16_t *plen)
1075 {
1076 	const struct usb_temp_device_desc *tdd;
1077 
1078 	tdd = usb_temp_get_tdd(udev);
1079 	if (tdd == NULL) {
1080 		return (NULL);
1081 	}
1082 	if (tdd->getVendorDesc == NULL) {
1083 		return (NULL);
1084 	}
1085 	return ((tdd->getVendorDesc) (req, plen));
1086 }
1087 
1088 /*------------------------------------------------------------------------*
1089  *	usb_temp_get_string_desc
1090  *
1091  * Returns:
1092  *  NULL: No string descriptor found.
1093  *  Else: Pointer to a string descriptor.
1094  *------------------------------------------------------------------------*/
1095 static const void *
usb_temp_get_string_desc(struct usb_device * udev,uint16_t lang_id,uint8_t string_index)1096 usb_temp_get_string_desc(struct usb_device *udev,
1097     uint16_t lang_id, uint8_t string_index)
1098 {
1099 	const struct usb_temp_device_desc *tdd;
1100 
1101 	tdd = usb_temp_get_tdd(udev);
1102 	if (tdd == NULL) {
1103 		return (NULL);
1104 	}
1105 	if (tdd->getStringDesc == NULL) {
1106 		return (NULL);
1107 	}
1108 	return ((tdd->getStringDesc) (lang_id, string_index));
1109 }
1110 
1111 /*------------------------------------------------------------------------*
1112  *	usb_temp_get_hub_desc
1113  *
1114  * Returns:
1115  *  NULL: No USB HUB descriptor found.
1116  *  Else: Pointer to a USB HUB descriptor.
1117  *------------------------------------------------------------------------*/
1118 static const void *
usb_temp_get_hub_desc(struct usb_device * udev)1119 usb_temp_get_hub_desc(struct usb_device *udev)
1120 {
1121 	return (NULL);			/* needs to be implemented */
1122 }
1123 
1124 /*------------------------------------------------------------------------*
1125  *	usb_temp_get_desc
1126  *
1127  * This function is a demultiplexer for local USB device side control
1128  * endpoint requests.
1129  *------------------------------------------------------------------------*/
1130 static usb_error_t
usb_temp_get_desc(struct usb_device * udev,struct usb_device_request * req,const void ** pPtr,uint16_t * pLength)1131 usb_temp_get_desc(struct usb_device *udev, struct usb_device_request *req,
1132     const void **pPtr, uint16_t *pLength)
1133 {
1134 	const uint8_t *buf;
1135 	uint16_t len;
1136 
1137 	buf = NULL;
1138 	len = 0;
1139 
1140 	switch (req->bmRequestType) {
1141 	case UT_READ_DEVICE:
1142 		switch (req->bRequest) {
1143 		case UR_GET_DESCRIPTOR:
1144 			goto tr_handle_get_descriptor;
1145 		default:
1146 			goto tr_stalled;
1147 		}
1148 	case UT_READ_CLASS_DEVICE:
1149 		switch (req->bRequest) {
1150 		case UR_GET_DESCRIPTOR:
1151 			goto tr_handle_get_class_descriptor;
1152 		default:
1153 			goto tr_stalled;
1154 		}
1155 	default:
1156 		goto tr_stalled;
1157 	}
1158 
1159 tr_handle_get_descriptor:
1160 	switch (req->wValue[1]) {
1161 	case UDESC_DEVICE:
1162 		if (req->wValue[0]) {
1163 			goto tr_stalled;
1164 		}
1165 		buf = usb_temp_get_device_desc(udev);
1166 		goto tr_valid;
1167 	case UDESC_DEVICE_QUALIFIER:
1168 		if (udev->speed != USB_SPEED_HIGH) {
1169 			goto tr_stalled;
1170 		}
1171 		if (req->wValue[0]) {
1172 			goto tr_stalled;
1173 		}
1174 		buf = usb_temp_get_qualifier_desc(udev);
1175 		goto tr_valid;
1176 	case UDESC_OTHER_SPEED_CONFIGURATION:
1177 		if (udev->speed != USB_SPEED_HIGH) {
1178 			goto tr_stalled;
1179 		}
1180 	case UDESC_CONFIG:
1181 		buf = usb_temp_get_config_desc(udev,
1182 		    &len, req->wValue[0]);
1183 		goto tr_valid;
1184 	case UDESC_STRING:
1185 		buf = usb_temp_get_string_desc(udev,
1186 		    UGETW(req->wIndex), req->wValue[0]);
1187 		goto tr_valid;
1188 	default:
1189 		goto tr_stalled;
1190 	}
1191 
1192 tr_handle_get_class_descriptor:
1193 	if (req->wValue[0]) {
1194 		goto tr_stalled;
1195 	}
1196 	buf = usb_temp_get_hub_desc(udev);
1197 	goto tr_valid;
1198 
1199 tr_valid:
1200 	if (buf == NULL)
1201 		goto tr_stalled;
1202 	if (len == 0)
1203 		len = buf[0];
1204 	*pPtr = buf;
1205 	*pLength = len;
1206 	return (0);	/* success */
1207 
1208 tr_stalled:
1209 	/* try to get a vendor specific descriptor */
1210 	len = 0;
1211 	buf = usb_temp_get_vendor_desc(udev, req, &len);
1212 	if (buf != NULL)
1213 		goto tr_valid;
1214 	*pPtr = NULL;
1215 	*pLength = 0;
1216 	return (0);	/* we ignore failures */
1217 }
1218 
1219 /*------------------------------------------------------------------------*
1220  *	usb_temp_setup
1221  *
1222  * This function generates USB descriptors according to the given USB
1223  * template device descriptor. It will also try to figure out the best
1224  * matching endpoint addresses using the hardware endpoint profiles.
1225  *
1226  * Returns:
1227  *    0: Success
1228  * Else: Failure
1229  *------------------------------------------------------------------------*/
1230 usb_error_t
usb_temp_setup(struct usb_device * udev,const struct usb_temp_device_desc * tdd)1231 usb_temp_setup(struct usb_device *udev,
1232     const struct usb_temp_device_desc *tdd)
1233 {
1234 	struct usb_temp_setup *uts;
1235 	void *buf;
1236 	usb_error_t error;
1237 	uint8_t n;
1238 	uint8_t do_unlock;
1239 
1240 	/* be NULL safe */
1241 	if (tdd == NULL)
1242 		return (0);
1243 
1244 	/* Protect scratch area */
1245 	do_unlock = usbd_enum_lock(udev);
1246 
1247 	uts = udev->scratch.temp_setup;
1248 
1249 	memset(uts, 0, sizeof(*uts));
1250 
1251 	uts->usb_speed = udev->speed;
1252 	uts->self_powered = udev->flags.self_powered;
1253 
1254 	/* first pass */
1255 
1256 	usb_make_device_desc(uts, tdd);
1257 
1258 	if (uts->err) {
1259 		/* some error happened */
1260 		goto done;
1261 	}
1262 	/* sanity check */
1263 	if (uts->size == 0) {
1264 		uts->err = USB_ERR_INVAL;
1265 		goto done;
1266 	}
1267 	/* allocate zeroed memory */
1268 	uts->buf = usbd_alloc_config_desc(udev, uts->size);
1269 	/*
1270 	 * Allow malloc() to return NULL regardless of M_WAITOK flag.
1271 	 * This helps when porting the software to non-FreeBSD
1272 	 * systems.
1273 	 */
1274 	if (uts->buf == NULL) {
1275 		/* could not allocate memory */
1276 		uts->err = USB_ERR_NOMEM;
1277 		goto done;
1278 	}
1279 	/* second pass */
1280 
1281 	uts->size = 0;
1282 
1283 	usb_make_device_desc(uts, tdd);
1284 
1285 	/*
1286 	 * Store a pointer to our descriptors:
1287 	 */
1288 	udev->usb_template_ptr = uts->buf;
1289 
1290 	if (uts->err) {
1291 		/* some error happened during second pass */
1292 		goto done;
1293 	}
1294 	/*
1295 	 * Resolve all endpoint addresses !
1296 	 */
1297 	buf = usb_temp_get_device_desc(udev);
1298 	uts->err = usb_hw_ep_resolve(udev, buf);
1299 	if (uts->err) {
1300 		DPRINTFN(0, "Could not resolve endpoints for "
1301 		    "Device Descriptor, error = %s\n",
1302 		    usbd_errstr(uts->err));
1303 		goto done;
1304 	}
1305 	for (n = 0;; n++) {
1306 
1307 		buf = usb_temp_get_config_desc(udev, NULL, n);
1308 		if (buf == NULL) {
1309 			break;
1310 		}
1311 		uts->err = usb_hw_ep_resolve(udev, buf);
1312 		if (uts->err) {
1313 			DPRINTFN(0, "Could not resolve endpoints for "
1314 			    "Config Descriptor %u, error = %s\n", n,
1315 			    usbd_errstr(uts->err));
1316 			goto done;
1317 		}
1318 	}
1319 done:
1320 	error = uts->err;
1321 	if (error)
1322 		usb_temp_unsetup(udev);
1323 	if (do_unlock)
1324 		usbd_enum_unlock(udev);
1325 	return (error);
1326 }
1327 
1328 /*------------------------------------------------------------------------*
1329  *	usb_temp_unsetup
1330  *
1331  * This function frees any memory associated with the currently
1332  * setup template, if any.
1333  *------------------------------------------------------------------------*/
1334 void
usb_temp_unsetup(struct usb_device * udev)1335 usb_temp_unsetup(struct usb_device *udev)
1336 {
1337 	usbd_free_config_desc(udev, udev->usb_template_ptr);
1338 	udev->usb_template_ptr = NULL;
1339 }
1340 
1341 static usb_error_t
usb_temp_setup_by_index(struct usb_device * udev,uint16_t index)1342 usb_temp_setup_by_index(struct usb_device *udev, uint16_t index)
1343 {
1344 	usb_error_t err;
1345 
1346 	switch (index) {
1347 	case USB_TEMP_MSC:
1348 		err = usb_temp_setup(udev, &usb_template_msc);
1349 		break;
1350 	case USB_TEMP_CDCE:
1351 		err = usb_temp_setup(udev, &usb_template_cdce);
1352 		break;
1353 	case USB_TEMP_MTP:
1354 		err = usb_temp_setup(udev, &usb_template_mtp);
1355 		break;
1356 	case USB_TEMP_MODEM:
1357 		err = usb_temp_setup(udev, &usb_template_modem);
1358 		break;
1359 	case USB_TEMP_AUDIO:
1360 		err = usb_temp_setup(udev, &usb_template_audio);
1361 		break;
1362 	case USB_TEMP_KBD:
1363 		err = usb_temp_setup(udev, &usb_template_kbd);
1364 		break;
1365 	case USB_TEMP_MOUSE:
1366 		err = usb_temp_setup(udev, &usb_template_mouse);
1367 		break;
1368 	case USB_TEMP_SERIALNET:
1369 		err = usb_temp_setup(udev, &usb_template_serialnet);
1370 		break;
1371 	default:
1372 		return (USB_ERR_INVAL);
1373 	}
1374 
1375 	return (err);
1376 }
1377 
1378 static void
usb_temp_init(void * arg)1379 usb_temp_init(void *arg)
1380 {
1381 	/* register our functions */
1382 	usb_temp_get_desc_p = &usb_temp_get_desc;
1383 	usb_temp_setup_by_index_p = &usb_temp_setup_by_index;
1384 	usb_temp_unsetup_p = &usb_temp_unsetup;
1385 }
1386 
1387 SYSINIT(usb_temp_init, SI_SUB_DRIVERS, SI_ORDER_FIRST, usb_temp_init, NULL);
1388 SYSUNINIT(usb_temp_unload, SI_SUB_DRIVERS, SI_ORDER_ANY, usb_temp_unload, NULL);
1389