3 * Copyright (c) 2007 Hans Petter Selasky. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * This file contains sub-routines to build up USB descriptors from
32 #include <sys/stdint.h>
33 #include <sys/stddef.h>
34 #include <sys/param.h>
35 #include <sys/queue.h>
36 #include <sys/types.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
40 #include <sys/linker_set.h>
41 #include <sys/module.h>
43 #include <sys/mutex.h>
44 #include <sys/condvar.h>
45 #include <sys/sysctl.h>
47 #include <sys/unistd.h>
48 #include <sys/callout.h>
49 #include <sys/malloc.h>
52 #include <dev/usb/usb.h>
53 #include <dev/usb/usbdi.h>
54 #include <dev/usb/usbdi_util.h>
57 #include <dev/usb/usb_cdc.h>
58 #include <dev/usb/usb_core.h>
59 #include <dev/usb/usb_dynamic.h>
60 #include <dev/usb/usb_busdma.h>
61 #include <dev/usb/usb_process.h>
62 #include <dev/usb/usb_device.h>
64 #define USB_DEBUG_VAR usb_debug
65 #include <dev/usb/usb_debug.h>
67 #include <dev/usb/usb_controller.h>
68 #include <dev/usb/usb_bus.h>
69 #include <dev/usb/template/usb_template.h>
71 MODULE_DEPEND(usb_template, usb, 1, 1, 1);
72 MODULE_VERSION(usb_template, 1);
74 /* function prototypes */
76 static void usb_make_raw_desc(struct usb_temp_setup *, const uint8_t *);
77 static void usb_make_endpoint_desc(struct usb_temp_setup *,
78 const struct usb_temp_endpoint_desc *);
79 static void usb_make_interface_desc(struct usb_temp_setup *,
80 const struct usb_temp_interface_desc *);
81 static void usb_make_config_desc(struct usb_temp_setup *,
82 const struct usb_temp_config_desc *);
83 static void usb_make_device_desc(struct usb_temp_setup *,
84 const struct usb_temp_device_desc *);
85 static uint8_t usb_hw_ep_match(const struct usb_hw_ep_profile *, uint8_t,
87 static uint8_t usb_hw_ep_find_match(struct usb_hw_ep_scratch *,
88 struct usb_hw_ep_scratch_sub *, uint8_t);
89 static uint8_t usb_hw_ep_get_needs(struct usb_hw_ep_scratch *, uint8_t,
91 static usb_error_t usb_hw_ep_resolve(struct usb_device *,
92 struct usb_descriptor *);
93 static const struct usb_temp_device_desc *usb_temp_get_tdd(struct usb_device *);
94 static void *usb_temp_get_device_desc(struct usb_device *);
95 static void *usb_temp_get_qualifier_desc(struct usb_device *);
96 static void *usb_temp_get_config_desc(struct usb_device *, uint16_t *,
98 static const void *usb_temp_get_string_desc(struct usb_device *, uint16_t,
100 static const void *usb_temp_get_vendor_desc(struct usb_device *,
101 const struct usb_device_request *, uint16_t *plen);
102 static const void *usb_temp_get_hub_desc(struct usb_device *);
103 static usb_error_t usb_temp_get_desc(struct usb_device *,
104 struct usb_device_request *, const void **, uint16_t *);
105 static usb_error_t usb_temp_setup_by_index(struct usb_device *,
107 static void usb_temp_init(void *);
109 /*------------------------------------------------------------------------*
112 * This function will insert a raw USB descriptor into the generated
114 *------------------------------------------------------------------------*/
116 usb_make_raw_desc(struct usb_temp_setup *temp,
123 * The first byte of any USB descriptor gives the length.
128 dst = USB_ADD_BYTES(temp->buf, temp->size);
129 bcopy(raw, dst, len);
131 /* check if we have got a CDC union descriptor */
133 if ((raw[0] >= sizeof(struct usb_cdc_union_descriptor)) &&
134 (raw[1] == UDESC_CS_INTERFACE) &&
135 (raw[2] == UDESCSUB_CDC_UNION)) {
136 struct usb_cdc_union_descriptor *ud = (void *)dst;
138 /* update the interface numbers */
140 ud->bMasterInterface +=
141 temp->bInterfaceNumber;
142 ud->bSlaveInterface[0] +=
143 temp->bInterfaceNumber;
150 /*------------------------------------------------------------------------*
151 * usb_make_endpoint_desc
153 * This function will generate an USB endpoint descriptor from the
154 * given USB template endpoint descriptor, which will be inserted into
155 * the USB configuration.
156 *------------------------------------------------------------------------*/
158 usb_make_endpoint_desc(struct usb_temp_setup *temp,
159 const struct usb_temp_endpoint_desc *ted)
161 struct usb_endpoint_descriptor *ed;
165 uint8_t ea = 0; /* Endpoint Address */
166 uint8_t et = 0; /* Endpiont Type */
169 old_size = temp->size;
170 temp->size += sizeof(*ed);
172 /* Scan all Raw Descriptors first */
177 usb_make_raw_desc(temp, *rd);
181 if (ted->pPacketSize == NULL) {
182 /* not initialized */
183 temp->err = USB_ERR_INVAL;
186 mps = ted->pPacketSize->mps[temp->usb_speed];
188 /* not initialized */
189 temp->err = USB_ERR_INVAL;
191 } else if (mps == UE_ZERO_MPS) {
192 /* escape for Zero Max Packet Size */
195 ea = (ted->bEndpointAddress & (UE_ADDR | UE_DIR_IN | UE_DIR_OUT));
196 et = (ted->bmAttributes & UE_XFERTYPE);
199 * Fill out the real USB endpoint descriptor
200 * in case there is a buffer present:
203 ed = USB_ADD_BYTES(temp->buf, old_size);
204 ed->bLength = sizeof(*ed);
205 ed->bDescriptorType = UDESC_ENDPOINT;
206 ed->bEndpointAddress = ea;
207 ed->bmAttributes = ted->bmAttributes;
208 USETW(ed->wMaxPacketSize, mps);
210 /* setup bInterval parameter */
212 if (ted->pIntervals &&
213 ted->pIntervals->bInterval[temp->usb_speed]) {
215 ted->pIntervals->bInterval[temp->usb_speed];
220 ed->bInterval = 0; /* not used */
223 switch (temp->usb_speed) {
226 ed->bInterval = 1; /* 1 ms */
229 ed->bInterval = 8; /* 8*125 us */
233 default: /* UE_ISOCHRONOUS */
234 switch (temp->usb_speed) {
237 ed->bInterval = 1; /* 1 ms */
240 ed->bInterval = 1; /* 125 us */
247 temp->bNumEndpoints++;
250 /*------------------------------------------------------------------------*
251 * usb_make_interface_desc
253 * This function will generate an USB interface descriptor from the
254 * given USB template interface descriptor, which will be inserted
255 * into the USB configuration.
256 *------------------------------------------------------------------------*/
258 usb_make_interface_desc(struct usb_temp_setup *temp,
259 const struct usb_temp_interface_desc *tid)
261 struct usb_interface_descriptor *id;
262 const struct usb_temp_endpoint_desc **ted;
268 old_size = temp->size;
269 temp->size += sizeof(*id);
271 /* Update interface and alternate interface numbers */
273 if (tid->isAltInterface == 0) {
274 temp->bAlternateSetting = 0;
275 temp->bInterfaceNumber++;
277 temp->bAlternateSetting++;
280 /* Scan all Raw Descriptors first */
286 usb_make_raw_desc(temp, *rd);
290 /* Reset some counters */
292 temp->bNumEndpoints = 0;
294 /* Scan all Endpoint Descriptors second */
296 ted = tid->ppEndpoints;
299 usb_make_endpoint_desc(temp, *ted);
304 * Fill out the real USB interface descriptor
305 * in case there is a buffer present:
308 id = USB_ADD_BYTES(temp->buf, old_size);
309 id->bLength = sizeof(*id);
310 id->bDescriptorType = UDESC_INTERFACE;
311 id->bInterfaceNumber = temp->bInterfaceNumber;
312 id->bAlternateSetting = temp->bAlternateSetting;
313 id->bNumEndpoints = temp->bNumEndpoints;
314 id->bInterfaceClass = tid->bInterfaceClass;
315 id->bInterfaceSubClass = tid->bInterfaceSubClass;
316 id->bInterfaceProtocol = tid->bInterfaceProtocol;
317 id->iInterface = tid->iInterface;
321 /*------------------------------------------------------------------------*
322 * usb_make_config_desc
324 * This function will generate an USB config descriptor from the given
325 * USB template config descriptor, which will be inserted into the USB
327 *------------------------------------------------------------------------*/
329 usb_make_config_desc(struct usb_temp_setup *temp,
330 const struct usb_temp_config_desc *tcd)
332 struct usb_config_descriptor *cd;
333 const struct usb_temp_interface_desc **tid;
338 old_size = temp->size;
339 temp->size += sizeof(*cd);
341 /* Reset some counters */
343 temp->bInterfaceNumber = 0 - 1;
344 temp->bAlternateSetting = 0;
346 /* Scan all the USB interfaces */
348 tid = tcd->ppIfaceDesc;
351 usb_make_interface_desc(temp, *tid);
356 * Fill out the real USB config descriptor
357 * in case there is a buffer present:
360 cd = USB_ADD_BYTES(temp->buf, old_size);
362 /* compute total size */
363 old_size = temp->size - old_size;
365 cd->bLength = sizeof(*cd);
366 cd->bDescriptorType = UDESC_CONFIG;
367 USETW(cd->wTotalLength, old_size);
368 cd->bNumInterface = temp->bInterfaceNumber + 1;
369 cd->bConfigurationValue = temp->bConfigurationValue;
370 cd->iConfiguration = tcd->iConfiguration;
371 cd->bmAttributes = tcd->bmAttributes;
372 cd->bMaxPower = tcd->bMaxPower;
373 cd->bmAttributes |= (UC_REMOTE_WAKEUP | UC_BUS_POWERED);
375 if (temp->self_powered) {
376 cd->bmAttributes |= UC_SELF_POWERED;
378 cd->bmAttributes &= ~UC_SELF_POWERED;
383 /*------------------------------------------------------------------------*
384 * usb_make_device_desc
386 * This function will generate an USB device descriptor from the
387 * given USB template device descriptor.
388 *------------------------------------------------------------------------*/
390 usb_make_device_desc(struct usb_temp_setup *temp,
391 const struct usb_temp_device_desc *tdd)
393 struct usb_temp_data *utd;
394 const struct usb_temp_config_desc **tcd;
399 old_size = temp->size;
400 temp->size += sizeof(*utd);
402 /* Scan all the USB configs */
404 temp->bConfigurationValue = 1;
405 tcd = tdd->ppConfigDesc;
408 usb_make_config_desc(temp, *tcd);
409 temp->bConfigurationValue++;
414 * Fill out the real USB device descriptor
415 * in case there is a buffer present:
419 utd = USB_ADD_BYTES(temp->buf, old_size);
421 /* Store a pointer to our template device descriptor */
424 /* Fill out USB device descriptor */
425 utd->udd.bLength = sizeof(utd->udd);
426 utd->udd.bDescriptorType = UDESC_DEVICE;
427 utd->udd.bDeviceClass = tdd->bDeviceClass;
428 utd->udd.bDeviceSubClass = tdd->bDeviceSubClass;
429 utd->udd.bDeviceProtocol = tdd->bDeviceProtocol;
430 USETW(utd->udd.idVendor, tdd->idVendor);
431 USETW(utd->udd.idProduct, tdd->idProduct);
432 USETW(utd->udd.bcdDevice, tdd->bcdDevice);
433 utd->udd.iManufacturer = tdd->iManufacturer;
434 utd->udd.iProduct = tdd->iProduct;
435 utd->udd.iSerialNumber = tdd->iSerialNumber;
436 utd->udd.bNumConfigurations = temp->bConfigurationValue - 1;
439 * Fill out the USB device qualifier. Pretend that we
440 * don't support any other speeds by setting
441 * "bNumConfigurations" equal to zero. That saves us
442 * generating an extra set of configuration
445 utd->udq.bLength = sizeof(utd->udq);
446 utd->udq.bDescriptorType = UDESC_DEVICE_QUALIFIER;
447 utd->udq.bDeviceClass = tdd->bDeviceClass;
448 utd->udq.bDeviceSubClass = tdd->bDeviceSubClass;
449 utd->udq.bDeviceProtocol = tdd->bDeviceProtocol;
450 utd->udq.bNumConfigurations = 0;
451 USETW(utd->udq.bcdUSB, 0x0200);
452 utd->udq.bMaxPacketSize0 = 0;
454 switch (temp->usb_speed) {
456 USETW(utd->udd.bcdUSB, 0x0110);
457 utd->udd.bMaxPacketSize = 8;
460 USETW(utd->udd.bcdUSB, 0x0110);
461 utd->udd.bMaxPacketSize = 32;
464 USETW(utd->udd.bcdUSB, 0x0200);
465 utd->udd.bMaxPacketSize = 64;
467 case USB_SPEED_VARIABLE:
468 USETW(utd->udd.bcdUSB, 0x0250);
469 utd->udd.bMaxPacketSize = 255; /* 512 bytes */
472 temp->err = USB_ERR_INVAL;
478 /*------------------------------------------------------------------------*
482 * 0: The endpoint profile does not match the criterias
483 * Else: The endpoint profile matches the criterias
484 *------------------------------------------------------------------------*/
486 usb_hw_ep_match(const struct usb_hw_ep_profile *pf,
487 uint8_t ep_type, uint8_t ep_dir_in)
489 if (ep_type == UE_CONTROL) {
491 return (pf->support_control);
493 if ((pf->support_in && ep_dir_in) ||
494 (pf->support_out && !ep_dir_in)) {
495 if ((pf->support_interrupt && (ep_type == UE_INTERRUPT)) ||
496 (pf->support_isochronous && (ep_type == UE_ISOCHRONOUS)) ||
497 (pf->support_bulk && (ep_type == UE_BULK))) {
504 /*------------------------------------------------------------------------*
505 * usb_hw_ep_find_match
507 * This function is used to find the best matching endpoint profile
508 * for and endpoint belonging to an USB descriptor.
511 * 0: Success. Got a match.
512 * Else: Failure. No match.
513 *------------------------------------------------------------------------*/
515 usb_hw_ep_find_match(struct usb_hw_ep_scratch *ues,
516 struct usb_hw_ep_scratch_sub *ep, uint8_t is_simplex)
518 const struct usb_hw_ep_profile *pf;
521 uint16_t max_frame_size;
530 if ((!ep->needs_in) && (!ep->needs_out)) {
531 return (0); /* we are done */
533 if (ep->needs_ep_type == UE_CONTROL) {
546 for (n = 1; n != (USB_EP_MAX / 2); n++) {
548 /* get HW endpoint profile */
549 (ues->methods->get_hw_ep_profile) (ues->udev, &pf, n);
551 /* end of profiles */
554 /* check if IN-endpoint is reserved */
555 if (dir_in || pf->is_simplex) {
556 if (ues->bmInAlloc[n / 8] & (1 << (n % 8))) {
561 /* check if OUT-endpoint is reserved */
562 if (dir_out || pf->is_simplex) {
563 if (ues->bmOutAlloc[n / 8] & (1 << (n % 8))) {
569 if (pf->is_simplex == is_simplex) {
573 /* check if HW endpoint matches */
574 if (!usb_hw_ep_match(pf, ep->needs_ep_type, dir_in)) {
578 /* get maximum frame size */
580 max_frame_size = pf->max_in_frame_size;
582 max_frame_size = pf->max_out_frame_size;
584 /* check if we have a matching profile */
585 if (max_frame_size >= ep->max_frame_size) {
586 temp = (max_frame_size - ep->max_frame_size);
587 if (distance > temp) {
595 /* see if we got a match */
597 /* get the correct profile */
600 /* reserve IN-endpoint */
602 ues->bmInAlloc[best_n / 8] |=
604 ep->hw_endpoint_in = best_n | UE_DIR_IN;
607 /* reserve OUT-endpoint */
609 ues->bmOutAlloc[best_n / 8] |=
611 ep->hw_endpoint_out = best_n | UE_DIR_OUT;
614 return (0); /* got a match */
616 return (1); /* failure */
619 /*------------------------------------------------------------------------*
620 * usb_hw_ep_get_needs
622 * This function will figure out the type and number of endpoints
623 * which are needed for an USB configuration.
628 *------------------------------------------------------------------------*/
630 usb_hw_ep_get_needs(struct usb_hw_ep_scratch *ues,
631 uint8_t ep_type, uint8_t is_complete)
633 const struct usb_hw_ep_profile *pf;
634 struct usb_hw_ep_scratch_sub *ep_iface;
635 struct usb_hw_ep_scratch_sub *ep_curr;
636 struct usb_hw_ep_scratch_sub *ep_max;
637 struct usb_hw_ep_scratch_sub *ep_end;
638 struct usb_descriptor *desc;
639 struct usb_interface_descriptor *id;
640 struct usb_endpoint_descriptor *ed;
641 enum usb_dev_speed speed;
642 uint16_t wMaxPacketSize;
646 ep_iface = ues->ep_max;
647 ep_curr = ues->ep_max;
648 ep_end = ues->ep + USB_EP_MAX;
649 ep_max = ues->ep_max;
651 speed = usbd_get_speed(ues->udev);
655 while ((desc = usb_desc_foreach(ues->cd, desc))) {
657 if ((desc->bDescriptorType == UDESC_INTERFACE) &&
658 (desc->bLength >= sizeof(*id))) {
662 if (id->bAlternateSetting == 0) {
670 if ((desc->bDescriptorType == UDESC_ENDPOINT) &&
671 (desc->bLength >= sizeof(*ed))) {
675 goto handle_endpoint_desc;
678 ues->ep_max = ep_max;
681 handle_endpoint_desc:
682 temp = (ed->bmAttributes & UE_XFERTYPE);
684 if (temp == ep_type) {
686 if (ep_curr == ep_end) {
687 /* too many endpoints */
688 return (1); /* failure */
690 wMaxPacketSize = UGETW(ed->wMaxPacketSize);
691 if ((wMaxPacketSize & 0xF800) &&
692 (speed == USB_SPEED_HIGH)) {
693 /* handle packet multiplier */
694 temp = (wMaxPacketSize >> 11) & 3;
695 wMaxPacketSize &= 0x7FF;
703 * Check if we have a fixed endpoint number, else the
704 * endpoint number is allocated dynamically:
706 ep_no = (ed->bEndpointAddress & UE_ADDR);
709 /* get HW endpoint profile */
710 (ues->methods->get_hw_ep_profile)
711 (ues->udev, &pf, ep_no);
713 /* HW profile does not exist - failure */
714 DPRINTFN(0, "Endpoint profile %u "
715 "does not exist\n", ep_no);
718 /* reserve fixed endpoint number */
719 if (ep_type == UE_CONTROL) {
720 ues->bmInAlloc[ep_no / 8] |=
722 ues->bmOutAlloc[ep_no / 8] |=
724 if ((pf->max_in_frame_size < wMaxPacketSize) ||
725 (pf->max_out_frame_size < wMaxPacketSize)) {
726 DPRINTFN(0, "Endpoint profile %u "
727 "has too small buffer\n", ep_no);
730 } else if (ed->bEndpointAddress & UE_DIR_IN) {
731 ues->bmInAlloc[ep_no / 8] |=
733 if (pf->max_in_frame_size < wMaxPacketSize) {
734 DPRINTFN(0, "Endpoint profile %u "
735 "has too small buffer\n", ep_no);
739 ues->bmOutAlloc[ep_no / 8] |=
741 if (pf->max_out_frame_size < wMaxPacketSize) {
742 DPRINTFN(0, "Endpoint profile %u "
743 "has too small buffer\n", ep_no);
747 } else if (is_complete) {
749 /* check if we have enough buffer space */
751 ep_curr->max_frame_size) {
752 return (1); /* failure */
754 if (ed->bEndpointAddress & UE_DIR_IN) {
755 ed->bEndpointAddress =
756 ep_curr->hw_endpoint_in;
758 ed->bEndpointAddress =
759 ep_curr->hw_endpoint_out;
764 /* compute the maximum frame size */
765 if (ep_curr->max_frame_size < wMaxPacketSize) {
766 ep_curr->max_frame_size = wMaxPacketSize;
768 if (temp == UE_CONTROL) {
769 ep_curr->needs_in = 1;
770 ep_curr->needs_out = 1;
772 if (ed->bEndpointAddress & UE_DIR_IN) {
773 ep_curr->needs_in = 1;
775 ep_curr->needs_out = 1;
778 ep_curr->needs_ep_type = ep_type;
782 if (ep_max < ep_curr) {
789 /*------------------------------------------------------------------------*
792 * This function will try to resolve endpoint requirements by the
793 * given endpoint profiles that the USB hardware reports.
798 *------------------------------------------------------------------------*/
800 usb_hw_ep_resolve(struct usb_device *udev,
801 struct usb_descriptor *desc)
803 struct usb_hw_ep_scratch *ues;
804 struct usb_hw_ep_scratch_sub *ep;
805 const struct usb_hw_ep_profile *pf;
806 struct usb_bus_methods *methods;
807 struct usb_device_descriptor *dd;
811 return (USB_ERR_INVAL);
813 /* get bus methods */
814 methods = udev->bus->methods;
816 if (methods->get_hw_ep_profile == NULL) {
817 return (USB_ERR_INVAL);
819 if (desc->bDescriptorType == UDESC_DEVICE) {
821 if (desc->bLength < sizeof(*dd)) {
822 return (USB_ERR_INVAL);
826 /* get HW control endpoint 0 profile */
827 (methods->get_hw_ep_profile) (udev, &pf, 0);
829 return (USB_ERR_INVAL);
831 if (!usb_hw_ep_match(pf, UE_CONTROL, 0)) {
832 DPRINTFN(0, "Endpoint 0 does not "
833 "support control\n");
834 return (USB_ERR_INVAL);
836 mps = dd->bMaxPacketSize;
838 if (udev->speed == USB_SPEED_FULL) {
840 * We can optionally choose another packet size !
843 /* check if "mps" is ok */
844 if (pf->max_in_frame_size >= mps) {
847 /* reduce maximum packet size */
850 /* check if "mps" is too small */
852 return (USB_ERR_INVAL);
856 dd->bMaxPacketSize = mps;
859 /* We only have one choice */
863 /* Check if we support the specified wMaxPacketSize */
864 if (pf->max_in_frame_size < mps) {
865 return (USB_ERR_INVAL);
868 return (0); /* success */
870 if (desc->bDescriptorType != UDESC_CONFIG) {
871 return (USB_ERR_INVAL);
873 if (desc->bLength < sizeof(*(ues->cd))) {
874 return (USB_ERR_INVAL);
876 ues = udev->bus->scratch[0].hw_ep_scratch;
878 bzero(ues, sizeof(*ues));
880 ues->ep_max = ues->ep;
881 ues->cd = (void *)desc;
882 ues->methods = methods;
885 /* Get all the endpoints we need */
887 if (usb_hw_ep_get_needs(ues, UE_ISOCHRONOUS, 0) ||
888 usb_hw_ep_get_needs(ues, UE_INTERRUPT, 0) ||
889 usb_hw_ep_get_needs(ues, UE_CONTROL, 0) ||
890 usb_hw_ep_get_needs(ues, UE_BULK, 0)) {
891 DPRINTFN(0, "Could not get needs\n");
892 return (USB_ERR_INVAL);
894 for (ep = ues->ep; ep != ues->ep_max; ep++) {
896 while (ep->needs_in || ep->needs_out) {
899 * First try to use a simplex endpoint.
900 * Then try to use a duplex endpoint.
902 if (usb_hw_ep_find_match(ues, ep, 1) &&
903 usb_hw_ep_find_match(ues, ep, 0)) {
904 DPRINTFN(0, "Could not find match\n");
905 return (USB_ERR_INVAL);
910 ues->ep_max = ues->ep;
912 /* Update all endpoint addresses */
914 if (usb_hw_ep_get_needs(ues, UE_ISOCHRONOUS, 1) ||
915 usb_hw_ep_get_needs(ues, UE_INTERRUPT, 1) ||
916 usb_hw_ep_get_needs(ues, UE_CONTROL, 1) ||
917 usb_hw_ep_get_needs(ues, UE_BULK, 1)) {
918 DPRINTFN(0, "Could not update endpoint address\n");
919 return (USB_ERR_INVAL);
921 return (0); /* success */
924 /*------------------------------------------------------------------------*
928 * NULL: No USB template device descriptor found.
929 * Else: Pointer to the USB template device descriptor.
930 *------------------------------------------------------------------------*/
931 static const struct usb_temp_device_desc *
932 usb_temp_get_tdd(struct usb_device *udev)
934 if (udev->usb_template_ptr == NULL) {
937 return (udev->usb_template_ptr->tdd);
940 /*------------------------------------------------------------------------*
941 * usb_temp_get_device_desc
944 * NULL: No USB device descriptor found.
945 * Else: Pointer to USB device descriptor.
946 *------------------------------------------------------------------------*/
948 usb_temp_get_device_desc(struct usb_device *udev)
950 struct usb_device_descriptor *dd;
952 if (udev->usb_template_ptr == NULL) {
955 dd = &udev->usb_template_ptr->udd;
956 if (dd->bDescriptorType != UDESC_DEVICE) {
957 /* sanity check failed */
963 /*------------------------------------------------------------------------*
964 * usb_temp_get_qualifier_desc
967 * NULL: No USB device_qualifier descriptor found.
968 * Else: Pointer to USB device_qualifier descriptor.
969 *------------------------------------------------------------------------*/
971 usb_temp_get_qualifier_desc(struct usb_device *udev)
973 struct usb_device_qualifier *dq;
975 if (udev->usb_template_ptr == NULL) {
978 dq = &udev->usb_template_ptr->udq;
979 if (dq->bDescriptorType != UDESC_DEVICE_QUALIFIER) {
980 /* sanity check failed */
986 /*------------------------------------------------------------------------*
987 * usb_temp_get_config_desc
990 * NULL: No USB config descriptor found.
991 * Else: Pointer to USB config descriptor having index "index".
992 *------------------------------------------------------------------------*/
994 usb_temp_get_config_desc(struct usb_device *udev,
995 uint16_t *pLength, uint8_t index)
997 struct usb_device_descriptor *dd;
998 struct usb_config_descriptor *cd;
1001 if (udev->usb_template_ptr == NULL) {
1004 dd = &udev->usb_template_ptr->udd;
1005 cd = (void *)(udev->usb_template_ptr + 1);
1007 if (index >= dd->bNumConfigurations) {
1012 if (cd->bDescriptorType != UDESC_CONFIG) {
1013 /* sanity check failed */
1016 temp = UGETW(cd->wTotalLength);
1017 cd = USB_ADD_BYTES(cd, temp);
1021 *pLength = UGETW(cd->wTotalLength);
1026 /*------------------------------------------------------------------------*
1027 * usb_temp_get_vendor_desc
1030 * NULL: No vendor descriptor found.
1031 * Else: Pointer to a vendor descriptor.
1032 *------------------------------------------------------------------------*/
1034 usb_temp_get_vendor_desc(struct usb_device *udev,
1035 const struct usb_device_request *req, uint16_t *plen)
1037 const struct usb_temp_device_desc *tdd;
1039 tdd = usb_temp_get_tdd(udev);
1043 if (tdd->getVendorDesc == NULL) {
1046 return ((tdd->getVendorDesc) (req, plen));
1049 /*------------------------------------------------------------------------*
1050 * usb_temp_get_string_desc
1053 * NULL: No string descriptor found.
1054 * Else: Pointer to a string descriptor.
1055 *------------------------------------------------------------------------*/
1057 usb_temp_get_string_desc(struct usb_device *udev,
1058 uint16_t lang_id, uint8_t string_index)
1060 const struct usb_temp_device_desc *tdd;
1062 tdd = usb_temp_get_tdd(udev);
1066 if (tdd->getStringDesc == NULL) {
1069 return ((tdd->getStringDesc) (lang_id, string_index));
1072 /*------------------------------------------------------------------------*
1073 * usb_temp_get_hub_desc
1076 * NULL: No USB HUB descriptor found.
1077 * Else: Pointer to a USB HUB descriptor.
1078 *------------------------------------------------------------------------*/
1080 usb_temp_get_hub_desc(struct usb_device *udev)
1082 return (NULL); /* needs to be implemented */
1085 /*------------------------------------------------------------------------*
1088 * This function is a demultiplexer for local USB device side control
1089 * endpoint requests.
1090 *------------------------------------------------------------------------*/
1092 usb_temp_get_desc(struct usb_device *udev, struct usb_device_request *req,
1093 const void **pPtr, uint16_t *pLength)
1101 switch (req->bmRequestType) {
1102 case UT_READ_DEVICE:
1103 switch (req->bRequest) {
1104 case UR_GET_DESCRIPTOR:
1105 goto tr_handle_get_descriptor;
1109 case UT_READ_CLASS_DEVICE:
1110 switch (req->bRequest) {
1111 case UR_GET_DESCRIPTOR:
1112 goto tr_handle_get_class_descriptor;
1120 tr_handle_get_descriptor:
1121 switch (req->wValue[1]) {
1123 if (req->wValue[0]) {
1126 buf = usb_temp_get_device_desc(udev);
1128 case UDESC_DEVICE_QUALIFIER:
1129 if (udev->speed != USB_SPEED_HIGH) {
1132 if (req->wValue[0]) {
1135 buf = usb_temp_get_qualifier_desc(udev);
1137 case UDESC_OTHER_SPEED_CONFIGURATION:
1138 if (udev->speed != USB_SPEED_HIGH) {
1142 buf = usb_temp_get_config_desc(udev,
1143 &len, req->wValue[0]);
1146 buf = usb_temp_get_string_desc(udev,
1147 UGETW(req->wIndex), req->wValue[0]);
1153 tr_handle_get_class_descriptor:
1154 if (req->wValue[0]) {
1157 buf = usb_temp_get_hub_desc(udev);
1167 return (0); /* success */
1170 /* try to get a vendor specific descriptor */
1172 buf = usb_temp_get_vendor_desc(udev, req, &len);
1177 return (0); /* we ignore failures */
1180 /*------------------------------------------------------------------------*
1183 * This function generates USB descriptors according to the given USB
1184 * template device descriptor. It will also try to figure out the best
1185 * matching endpoint addresses using the hardware endpoint profiles.
1190 *------------------------------------------------------------------------*/
1192 usb_temp_setup(struct usb_device *udev,
1193 const struct usb_temp_device_desc *tdd)
1195 struct usb_temp_setup *uts;
1203 uts = udev->bus->scratch[0].temp_setup;
1205 bzero(uts, sizeof(*uts));
1207 uts->usb_speed = udev->speed;
1208 uts->self_powered = udev->flags.self_powered;
1212 usb_make_device_desc(uts, tdd);
1215 /* some error happened */
1219 if (uts->size == 0) {
1220 return (USB_ERR_INVAL);
1222 /* allocate zeroed memory */
1223 uts->buf = malloc(uts->size, M_USB, M_WAITOK | M_ZERO);
1224 if (uts->buf == NULL) {
1225 /* could not allocate memory */
1226 return (USB_ERR_NOMEM);
1232 usb_make_device_desc(uts, tdd);
1235 * Store a pointer to our descriptors:
1237 udev->usb_template_ptr = uts->buf;
1240 /* some error happened during second pass */
1244 * Resolve all endpoint addresses !
1246 buf = usb_temp_get_device_desc(udev);
1247 uts->err = usb_hw_ep_resolve(udev, buf);
1249 DPRINTFN(0, "Could not resolve endpoints for "
1250 "Device Descriptor, error = %s\n",
1251 usbd_errstr(uts->err));
1256 buf = usb_temp_get_config_desc(udev, NULL, n);
1260 uts->err = usb_hw_ep_resolve(udev, buf);
1262 DPRINTFN(0, "Could not resolve endpoints for "
1263 "Config Descriptor %u, error = %s\n", n,
1264 usbd_errstr(uts->err));
1271 usb_temp_unsetup(udev);
1275 /*------------------------------------------------------------------------*
1278 * This function frees any memory associated with the currently
1279 * setup template, if any.
1280 *------------------------------------------------------------------------*/
1282 usb_temp_unsetup(struct usb_device *udev)
1284 if (udev->usb_template_ptr) {
1286 free(udev->usb_template_ptr, M_USB);
1288 udev->usb_template_ptr = NULL;
1293 usb_temp_setup_by_index(struct usb_device *udev, uint16_t index)
1299 err = usb_temp_setup(udev, &usb_template_msc);
1302 err = usb_temp_setup(udev, &usb_template_cdce);
1305 err = usb_temp_setup(udev, &usb_template_mtp);
1308 return (USB_ERR_INVAL);
1315 usb_temp_init(void *arg)
1317 /* register our functions */
1318 usb_temp_get_desc_p = &usb_temp_get_desc;
1319 usb_temp_setup_by_index_p = &usb_temp_setup_by_index;
1320 usb_temp_unsetup_p = &usb_temp_unsetup;
1323 SYSINIT(usb_temp_init, SI_SUB_LOCK, SI_ORDER_FIRST, usb_temp_init, NULL);
1324 SYSUNINIT(usb_temp_unload, SI_SUB_LOCK, SI_ORDER_ANY, usb_temp_unload, NULL);