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/module.h>
42 #include <sys/mutex.h>
43 #include <sys/condvar.h>
44 #include <sys/sysctl.h>
46 #include <sys/unistd.h>
47 #include <sys/callout.h>
48 #include <sys/malloc.h>
51 #include <dev/usb/usb.h>
52 #include <dev/usb/usb_ioctl.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 memcpy(dst, raw, 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;
146 /* check if we have got an interface association descriptor */
148 if ((raw[0] >= sizeof(struct usb_interface_assoc_descriptor)) &&
149 (raw[1] == UDESC_IFACE_ASSOC)) {
150 struct usb_interface_assoc_descriptor *iad = (void *)dst;
152 /* update the interface number */
154 iad->bFirstInterface +=
155 temp->bInterfaceNumber;
158 /* check if we have got a call management descriptor */
160 if ((raw[0] >= sizeof(struct usb_cdc_cm_descriptor)) &&
161 (raw[1] == UDESC_CS_INTERFACE) &&
162 (raw[2] == UDESCSUB_CDC_CM)) {
163 struct usb_cdc_cm_descriptor *ccd = (void *)dst;
165 /* update the interface number */
167 ccd->bDataInterface +=
168 temp->bInterfaceNumber;
175 /*------------------------------------------------------------------------*
176 * usb_make_endpoint_desc
178 * This function will generate an USB endpoint descriptor from the
179 * given USB template endpoint descriptor, which will be inserted into
180 * the USB configuration.
181 *------------------------------------------------------------------------*/
183 usb_make_endpoint_desc(struct usb_temp_setup *temp,
184 const struct usb_temp_endpoint_desc *ted)
186 struct usb_endpoint_descriptor *ed;
190 uint8_t ea; /* Endpoint Address */
191 uint8_t et; /* Endpiont Type */
194 old_size = temp->size;
196 ea = (ted->bEndpointAddress & (UE_ADDR | UE_DIR_IN | UE_DIR_OUT));
197 et = (ted->bmAttributes & UE_XFERTYPE);
199 if (et == UE_ISOCHRONOUS) {
200 /* account for extra byte fields */
201 temp->size += sizeof(*ed) + 2;
203 temp->size += sizeof(*ed);
206 /* Scan all Raw Descriptors first */
210 usb_make_raw_desc(temp, *rd);
214 if (ted->pPacketSize == NULL) {
215 /* not initialized */
216 temp->err = USB_ERR_INVAL;
219 mps = ted->pPacketSize->mps[temp->usb_speed];
221 /* not initialized */
222 temp->err = USB_ERR_INVAL;
224 } else if (mps == UE_ZERO_MPS) {
225 /* escape for Zero Max Packet Size */
230 * Fill out the real USB endpoint descriptor
231 * in case there is a buffer present:
234 ed = USB_ADD_BYTES(temp->buf, old_size);
235 if (et == UE_ISOCHRONOUS)
236 ed->bLength = sizeof(*ed) + 2;
238 ed->bLength = sizeof(*ed);
239 ed->bDescriptorType = UDESC_ENDPOINT;
240 ed->bEndpointAddress = ea;
241 ed->bmAttributes = ted->bmAttributes;
242 USETW(ed->wMaxPacketSize, mps);
244 /* setup bInterval parameter */
246 if (ted->pIntervals &&
247 ted->pIntervals->bInterval[temp->usb_speed]) {
249 ted->pIntervals->bInterval[temp->usb_speed];
254 ed->bInterval = 0; /* not used */
257 switch (temp->usb_speed) {
260 ed->bInterval = 1; /* 1 ms */
263 ed->bInterval = 8; /* 8*125 us */
267 default: /* UE_ISOCHRONOUS */
268 switch (temp->usb_speed) {
271 ed->bInterval = 1; /* 1 ms */
274 ed->bInterval = 1; /* 125 us */
281 temp->bNumEndpoints++;
284 /*------------------------------------------------------------------------*
285 * usb_make_interface_desc
287 * This function will generate an USB interface descriptor from the
288 * given USB template interface descriptor, which will be inserted
289 * into the USB configuration.
290 *------------------------------------------------------------------------*/
292 usb_make_interface_desc(struct usb_temp_setup *temp,
293 const struct usb_temp_interface_desc *tid)
295 struct usb_interface_descriptor *id;
296 const struct usb_temp_endpoint_desc **ted;
302 old_size = temp->size;
303 temp->size += sizeof(*id);
305 /* Update interface and alternate interface numbers */
307 if (tid->isAltInterface == 0) {
308 temp->bAlternateSetting = 0;
309 temp->bInterfaceNumber++;
311 temp->bAlternateSetting++;
314 /* Scan all Raw Descriptors first */
320 usb_make_raw_desc(temp, *rd);
324 /* Reset some counters */
326 temp->bNumEndpoints = 0;
328 /* Scan all Endpoint Descriptors second */
330 ted = tid->ppEndpoints;
333 usb_make_endpoint_desc(temp, *ted);
338 * Fill out the real USB interface descriptor
339 * in case there is a buffer present:
342 id = USB_ADD_BYTES(temp->buf, old_size);
343 id->bLength = sizeof(*id);
344 id->bDescriptorType = UDESC_INTERFACE;
345 id->bInterfaceNumber = temp->bInterfaceNumber;
346 id->bAlternateSetting = temp->bAlternateSetting;
347 id->bNumEndpoints = temp->bNumEndpoints;
348 id->bInterfaceClass = tid->bInterfaceClass;
349 id->bInterfaceSubClass = tid->bInterfaceSubClass;
350 id->bInterfaceProtocol = tid->bInterfaceProtocol;
351 id->iInterface = tid->iInterface;
355 /*------------------------------------------------------------------------*
356 * usb_make_config_desc
358 * This function will generate an USB config descriptor from the given
359 * USB template config descriptor, which will be inserted into the USB
361 *------------------------------------------------------------------------*/
363 usb_make_config_desc(struct usb_temp_setup *temp,
364 const struct usb_temp_config_desc *tcd)
366 struct usb_config_descriptor *cd;
367 const struct usb_temp_interface_desc **tid;
372 old_size = temp->size;
373 temp->size += sizeof(*cd);
375 /* Reset some counters */
377 temp->bInterfaceNumber = 0 - 1;
378 temp->bAlternateSetting = 0;
380 /* Scan all the USB interfaces */
382 tid = tcd->ppIfaceDesc;
385 usb_make_interface_desc(temp, *tid);
390 * Fill out the real USB config descriptor
391 * in case there is a buffer present:
394 cd = USB_ADD_BYTES(temp->buf, old_size);
396 /* compute total size */
397 old_size = temp->size - old_size;
399 cd->bLength = sizeof(*cd);
400 cd->bDescriptorType = UDESC_CONFIG;
401 USETW(cd->wTotalLength, old_size);
402 cd->bNumInterface = temp->bInterfaceNumber + 1;
403 cd->bConfigurationValue = temp->bConfigurationValue;
404 cd->iConfiguration = tcd->iConfiguration;
405 cd->bmAttributes = tcd->bmAttributes;
406 cd->bMaxPower = tcd->bMaxPower;
407 cd->bmAttributes |= (UC_REMOTE_WAKEUP | UC_BUS_POWERED);
409 if (temp->self_powered) {
410 cd->bmAttributes |= UC_SELF_POWERED;
412 cd->bmAttributes &= ~UC_SELF_POWERED;
417 /*------------------------------------------------------------------------*
418 * usb_make_device_desc
420 * This function will generate an USB device descriptor from the
421 * given USB template device descriptor.
422 *------------------------------------------------------------------------*/
424 usb_make_device_desc(struct usb_temp_setup *temp,
425 const struct usb_temp_device_desc *tdd)
427 struct usb_temp_data *utd;
428 const struct usb_temp_config_desc **tcd;
433 old_size = temp->size;
434 temp->size += sizeof(*utd);
436 /* Scan all the USB configs */
438 temp->bConfigurationValue = 1;
439 tcd = tdd->ppConfigDesc;
442 usb_make_config_desc(temp, *tcd);
443 temp->bConfigurationValue++;
448 * Fill out the real USB device descriptor
449 * in case there is a buffer present:
453 utd = USB_ADD_BYTES(temp->buf, old_size);
455 /* Store a pointer to our template device descriptor */
458 /* Fill out USB device descriptor */
459 utd->udd.bLength = sizeof(utd->udd);
460 utd->udd.bDescriptorType = UDESC_DEVICE;
461 utd->udd.bDeviceClass = tdd->bDeviceClass;
462 utd->udd.bDeviceSubClass = tdd->bDeviceSubClass;
463 utd->udd.bDeviceProtocol = tdd->bDeviceProtocol;
464 USETW(utd->udd.idVendor, tdd->idVendor);
465 USETW(utd->udd.idProduct, tdd->idProduct);
466 USETW(utd->udd.bcdDevice, tdd->bcdDevice);
467 utd->udd.iManufacturer = tdd->iManufacturer;
468 utd->udd.iProduct = tdd->iProduct;
469 utd->udd.iSerialNumber = tdd->iSerialNumber;
470 utd->udd.bNumConfigurations = temp->bConfigurationValue - 1;
473 * Fill out the USB device qualifier. Pretend that we
474 * don't support any other speeds by setting
475 * "bNumConfigurations" equal to zero. That saves us
476 * generating an extra set of configuration
479 utd->udq.bLength = sizeof(utd->udq);
480 utd->udq.bDescriptorType = UDESC_DEVICE_QUALIFIER;
481 utd->udq.bDeviceClass = tdd->bDeviceClass;
482 utd->udq.bDeviceSubClass = tdd->bDeviceSubClass;
483 utd->udq.bDeviceProtocol = tdd->bDeviceProtocol;
484 utd->udq.bNumConfigurations = 0;
485 USETW(utd->udq.bcdUSB, 0x0200);
486 utd->udq.bMaxPacketSize0 = 0;
488 switch (temp->usb_speed) {
490 USETW(utd->udd.bcdUSB, 0x0110);
491 utd->udd.bMaxPacketSize = 8;
494 USETW(utd->udd.bcdUSB, 0x0110);
495 utd->udd.bMaxPacketSize = 32;
498 USETW(utd->udd.bcdUSB, 0x0200);
499 utd->udd.bMaxPacketSize = 64;
501 case USB_SPEED_VARIABLE:
502 USETW(utd->udd.bcdUSB, 0x0250);
503 utd->udd.bMaxPacketSize = 255; /* 512 bytes */
505 case USB_SPEED_SUPER:
506 USETW(utd->udd.bcdUSB, 0x0300);
507 utd->udd.bMaxPacketSize = 9; /* 2**9 = 512 bytes */
510 temp->err = USB_ERR_INVAL;
516 /*------------------------------------------------------------------------*
520 * 0: The endpoint profile does not match the criterias
521 * Else: The endpoint profile matches the criterias
522 *------------------------------------------------------------------------*/
524 usb_hw_ep_match(const struct usb_hw_ep_profile *pf,
525 uint8_t ep_type, uint8_t ep_dir_in)
527 if (ep_type == UE_CONTROL) {
529 return (pf->support_control);
531 if ((pf->support_in && ep_dir_in) ||
532 (pf->support_out && !ep_dir_in)) {
533 if ((pf->support_interrupt && (ep_type == UE_INTERRUPT)) ||
534 (pf->support_isochronous && (ep_type == UE_ISOCHRONOUS)) ||
535 (pf->support_bulk && (ep_type == UE_BULK))) {
542 /*------------------------------------------------------------------------*
543 * usb_hw_ep_find_match
545 * This function is used to find the best matching endpoint profile
546 * for and endpoint belonging to an USB descriptor.
549 * 0: Success. Got a match.
550 * Else: Failure. No match.
551 *------------------------------------------------------------------------*/
553 usb_hw_ep_find_match(struct usb_hw_ep_scratch *ues,
554 struct usb_hw_ep_scratch_sub *ep, uint8_t is_simplex)
556 const struct usb_hw_ep_profile *pf;
559 uint16_t max_frame_size;
568 if ((!ep->needs_in) && (!ep->needs_out)) {
569 return (0); /* we are done */
571 if (ep->needs_ep_type == UE_CONTROL) {
584 for (n = 1; n != (USB_EP_MAX / 2); n++) {
586 /* get HW endpoint profile */
587 (ues->methods->get_hw_ep_profile) (ues->udev, &pf, n);
589 /* end of profiles */
592 /* check if IN-endpoint is reserved */
593 if (dir_in || pf->is_simplex) {
594 if (ues->bmInAlloc[n / 8] & (1 << (n % 8))) {
599 /* check if OUT-endpoint is reserved */
600 if (dir_out || pf->is_simplex) {
601 if (ues->bmOutAlloc[n / 8] & (1 << (n % 8))) {
607 if (pf->is_simplex == is_simplex) {
611 /* check if HW endpoint matches */
612 if (!usb_hw_ep_match(pf, ep->needs_ep_type, dir_in)) {
616 /* get maximum frame size */
618 max_frame_size = pf->max_in_frame_size;
620 max_frame_size = pf->max_out_frame_size;
622 /* check if we have a matching profile */
623 if (max_frame_size >= ep->max_frame_size) {
624 temp = (max_frame_size - ep->max_frame_size);
625 if (distance > temp) {
633 /* see if we got a match */
635 /* get the correct profile */
638 /* reserve IN-endpoint */
640 ues->bmInAlloc[best_n / 8] |=
642 ep->hw_endpoint_in = best_n | UE_DIR_IN;
645 /* reserve OUT-endpoint */
647 ues->bmOutAlloc[best_n / 8] |=
649 ep->hw_endpoint_out = best_n | UE_DIR_OUT;
652 return (0); /* got a match */
654 return (1); /* failure */
657 /*------------------------------------------------------------------------*
658 * usb_hw_ep_get_needs
660 * This function will figure out the type and number of endpoints
661 * which are needed for an USB configuration.
666 *------------------------------------------------------------------------*/
668 usb_hw_ep_get_needs(struct usb_hw_ep_scratch *ues,
669 uint8_t ep_type, uint8_t is_complete)
671 const struct usb_hw_ep_profile *pf;
672 struct usb_hw_ep_scratch_sub *ep_iface;
673 struct usb_hw_ep_scratch_sub *ep_curr;
674 struct usb_hw_ep_scratch_sub *ep_max;
675 struct usb_hw_ep_scratch_sub *ep_end;
676 struct usb_descriptor *desc;
677 struct usb_interface_descriptor *id;
678 struct usb_endpoint_descriptor *ed;
679 enum usb_dev_speed speed;
680 uint16_t wMaxPacketSize;
684 ep_iface = ues->ep_max;
685 ep_curr = ues->ep_max;
686 ep_end = ues->ep + USB_EP_MAX;
687 ep_max = ues->ep_max;
689 speed = usbd_get_speed(ues->udev);
693 while ((desc = usb_desc_foreach(ues->cd, desc))) {
695 if ((desc->bDescriptorType == UDESC_INTERFACE) &&
696 (desc->bLength >= sizeof(*id))) {
700 if (id->bAlternateSetting == 0) {
708 if ((desc->bDescriptorType == UDESC_ENDPOINT) &&
709 (desc->bLength >= sizeof(*ed))) {
713 goto handle_endpoint_desc;
716 ues->ep_max = ep_max;
719 handle_endpoint_desc:
720 temp = (ed->bmAttributes & UE_XFERTYPE);
722 if (temp == ep_type) {
724 if (ep_curr == ep_end) {
725 /* too many endpoints */
726 return (1); /* failure */
728 wMaxPacketSize = UGETW(ed->wMaxPacketSize);
729 if ((wMaxPacketSize & 0xF800) &&
730 (speed == USB_SPEED_HIGH)) {
731 /* handle packet multiplier */
732 temp = (wMaxPacketSize >> 11) & 3;
733 wMaxPacketSize &= 0x7FF;
741 * Check if we have a fixed endpoint number, else the
742 * endpoint number is allocated dynamically:
744 ep_no = (ed->bEndpointAddress & UE_ADDR);
747 /* get HW endpoint profile */
748 (ues->methods->get_hw_ep_profile)
749 (ues->udev, &pf, ep_no);
751 /* HW profile does not exist - failure */
752 DPRINTFN(0, "Endpoint profile %u "
753 "does not exist\n", ep_no);
756 /* reserve fixed endpoint number */
757 if (ep_type == UE_CONTROL) {
758 ues->bmInAlloc[ep_no / 8] |=
760 ues->bmOutAlloc[ep_no / 8] |=
762 if ((pf->max_in_frame_size < wMaxPacketSize) ||
763 (pf->max_out_frame_size < wMaxPacketSize)) {
764 DPRINTFN(0, "Endpoint profile %u "
765 "has too small buffer\n", ep_no);
768 } else if (ed->bEndpointAddress & UE_DIR_IN) {
769 ues->bmInAlloc[ep_no / 8] |=
771 if (pf->max_in_frame_size < wMaxPacketSize) {
772 DPRINTFN(0, "Endpoint profile %u "
773 "has too small buffer\n", ep_no);
777 ues->bmOutAlloc[ep_no / 8] |=
779 if (pf->max_out_frame_size < wMaxPacketSize) {
780 DPRINTFN(0, "Endpoint profile %u "
781 "has too small buffer\n", ep_no);
785 } else if (is_complete) {
787 /* check if we have enough buffer space */
789 ep_curr->max_frame_size) {
790 return (1); /* failure */
792 if (ed->bEndpointAddress & UE_DIR_IN) {
793 ed->bEndpointAddress =
794 ep_curr->hw_endpoint_in;
796 ed->bEndpointAddress =
797 ep_curr->hw_endpoint_out;
802 /* compute the maximum frame size */
803 if (ep_curr->max_frame_size < wMaxPacketSize) {
804 ep_curr->max_frame_size = wMaxPacketSize;
806 if (temp == UE_CONTROL) {
807 ep_curr->needs_in = 1;
808 ep_curr->needs_out = 1;
810 if (ed->bEndpointAddress & UE_DIR_IN) {
811 ep_curr->needs_in = 1;
813 ep_curr->needs_out = 1;
816 ep_curr->needs_ep_type = ep_type;
820 if (ep_max < ep_curr) {
827 /*------------------------------------------------------------------------*
830 * This function will try to resolve endpoint requirements by the
831 * given endpoint profiles that the USB hardware reports.
836 *------------------------------------------------------------------------*/
838 usb_hw_ep_resolve(struct usb_device *udev,
839 struct usb_descriptor *desc)
841 struct usb_hw_ep_scratch *ues;
842 struct usb_hw_ep_scratch_sub *ep;
843 const struct usb_hw_ep_profile *pf;
844 struct usb_bus_methods *methods;
845 struct usb_device_descriptor *dd;
849 return (USB_ERR_INVAL);
851 /* get bus methods */
852 methods = udev->bus->methods;
854 if (methods->get_hw_ep_profile == NULL) {
855 return (USB_ERR_INVAL);
857 if (desc->bDescriptorType == UDESC_DEVICE) {
859 if (desc->bLength < sizeof(*dd)) {
860 return (USB_ERR_INVAL);
864 /* get HW control endpoint 0 profile */
865 (methods->get_hw_ep_profile) (udev, &pf, 0);
867 return (USB_ERR_INVAL);
869 if (!usb_hw_ep_match(pf, UE_CONTROL, 0)) {
870 DPRINTFN(0, "Endpoint 0 does not "
871 "support control\n");
872 return (USB_ERR_INVAL);
874 mps = dd->bMaxPacketSize;
876 if (udev->speed == USB_SPEED_FULL) {
878 * We can optionally choose another packet size !
881 /* check if "mps" is ok */
882 if (pf->max_in_frame_size >= mps) {
885 /* reduce maximum packet size */
888 /* check if "mps" is too small */
890 return (USB_ERR_INVAL);
894 dd->bMaxPacketSize = mps;
897 /* We only have one choice */
901 /* Check if we support the specified wMaxPacketSize */
902 if (pf->max_in_frame_size < mps) {
903 return (USB_ERR_INVAL);
906 return (0); /* success */
908 if (desc->bDescriptorType != UDESC_CONFIG) {
909 return (USB_ERR_INVAL);
911 if (desc->bLength < sizeof(*(ues->cd))) {
912 return (USB_ERR_INVAL);
914 ues = udev->bus->scratch[0].hw_ep_scratch;
916 bzero(ues, sizeof(*ues));
918 ues->ep_max = ues->ep;
919 ues->cd = (void *)desc;
920 ues->methods = methods;
923 /* Get all the endpoints we need */
925 if (usb_hw_ep_get_needs(ues, UE_ISOCHRONOUS, 0) ||
926 usb_hw_ep_get_needs(ues, UE_INTERRUPT, 0) ||
927 usb_hw_ep_get_needs(ues, UE_CONTROL, 0) ||
928 usb_hw_ep_get_needs(ues, UE_BULK, 0)) {
929 DPRINTFN(0, "Could not get needs\n");
930 return (USB_ERR_INVAL);
932 for (ep = ues->ep; ep != ues->ep_max; ep++) {
934 while (ep->needs_in || ep->needs_out) {
937 * First try to use a simplex endpoint.
938 * Then try to use a duplex endpoint.
940 if (usb_hw_ep_find_match(ues, ep, 1) &&
941 usb_hw_ep_find_match(ues, ep, 0)) {
942 DPRINTFN(0, "Could not find match\n");
943 return (USB_ERR_INVAL);
948 ues->ep_max = ues->ep;
950 /* Update all endpoint addresses */
952 if (usb_hw_ep_get_needs(ues, UE_ISOCHRONOUS, 1) ||
953 usb_hw_ep_get_needs(ues, UE_INTERRUPT, 1) ||
954 usb_hw_ep_get_needs(ues, UE_CONTROL, 1) ||
955 usb_hw_ep_get_needs(ues, UE_BULK, 1)) {
956 DPRINTFN(0, "Could not update endpoint address\n");
957 return (USB_ERR_INVAL);
959 return (0); /* success */
962 /*------------------------------------------------------------------------*
966 * NULL: No USB template device descriptor found.
967 * Else: Pointer to the USB template device descriptor.
968 *------------------------------------------------------------------------*/
969 static const struct usb_temp_device_desc *
970 usb_temp_get_tdd(struct usb_device *udev)
972 if (udev->usb_template_ptr == NULL) {
975 return (udev->usb_template_ptr->tdd);
978 /*------------------------------------------------------------------------*
979 * usb_temp_get_device_desc
982 * NULL: No USB device descriptor found.
983 * Else: Pointer to USB device descriptor.
984 *------------------------------------------------------------------------*/
986 usb_temp_get_device_desc(struct usb_device *udev)
988 struct usb_device_descriptor *dd;
990 if (udev->usb_template_ptr == NULL) {
993 dd = &udev->usb_template_ptr->udd;
994 if (dd->bDescriptorType != UDESC_DEVICE) {
995 /* sanity check failed */
1001 /*------------------------------------------------------------------------*
1002 * usb_temp_get_qualifier_desc
1005 * NULL: No USB device_qualifier descriptor found.
1006 * Else: Pointer to USB device_qualifier descriptor.
1007 *------------------------------------------------------------------------*/
1009 usb_temp_get_qualifier_desc(struct usb_device *udev)
1011 struct usb_device_qualifier *dq;
1013 if (udev->usb_template_ptr == NULL) {
1016 dq = &udev->usb_template_ptr->udq;
1017 if (dq->bDescriptorType != UDESC_DEVICE_QUALIFIER) {
1018 /* sanity check failed */
1024 /*------------------------------------------------------------------------*
1025 * usb_temp_get_config_desc
1028 * NULL: No USB config descriptor found.
1029 * Else: Pointer to USB config descriptor having index "index".
1030 *------------------------------------------------------------------------*/
1032 usb_temp_get_config_desc(struct usb_device *udev,
1033 uint16_t *pLength, uint8_t index)
1035 struct usb_device_descriptor *dd;
1036 struct usb_config_descriptor *cd;
1039 if (udev->usb_template_ptr == NULL) {
1042 dd = &udev->usb_template_ptr->udd;
1043 cd = (void *)(udev->usb_template_ptr + 1);
1045 if (index >= dd->bNumConfigurations) {
1050 if (cd->bDescriptorType != UDESC_CONFIG) {
1051 /* sanity check failed */
1054 temp = UGETW(cd->wTotalLength);
1055 cd = USB_ADD_BYTES(cd, temp);
1059 *pLength = UGETW(cd->wTotalLength);
1064 /*------------------------------------------------------------------------*
1065 * usb_temp_get_vendor_desc
1068 * NULL: No vendor descriptor found.
1069 * Else: Pointer to a vendor descriptor.
1070 *------------------------------------------------------------------------*/
1072 usb_temp_get_vendor_desc(struct usb_device *udev,
1073 const struct usb_device_request *req, uint16_t *plen)
1075 const struct usb_temp_device_desc *tdd;
1077 tdd = usb_temp_get_tdd(udev);
1081 if (tdd->getVendorDesc == NULL) {
1084 return ((tdd->getVendorDesc) (req, plen));
1087 /*------------------------------------------------------------------------*
1088 * usb_temp_get_string_desc
1091 * NULL: No string descriptor found.
1092 * Else: Pointer to a string descriptor.
1093 *------------------------------------------------------------------------*/
1095 usb_temp_get_string_desc(struct usb_device *udev,
1096 uint16_t lang_id, uint8_t string_index)
1098 const struct usb_temp_device_desc *tdd;
1100 tdd = usb_temp_get_tdd(udev);
1104 if (tdd->getStringDesc == NULL) {
1107 return ((tdd->getStringDesc) (lang_id, string_index));
1110 /*------------------------------------------------------------------------*
1111 * usb_temp_get_hub_desc
1114 * NULL: No USB HUB descriptor found.
1115 * Else: Pointer to a USB HUB descriptor.
1116 *------------------------------------------------------------------------*/
1118 usb_temp_get_hub_desc(struct usb_device *udev)
1120 return (NULL); /* needs to be implemented */
1123 /*------------------------------------------------------------------------*
1126 * This function is a demultiplexer for local USB device side control
1127 * endpoint requests.
1128 *------------------------------------------------------------------------*/
1130 usb_temp_get_desc(struct usb_device *udev, struct usb_device_request *req,
1131 const void **pPtr, uint16_t *pLength)
1139 switch (req->bmRequestType) {
1140 case UT_READ_DEVICE:
1141 switch (req->bRequest) {
1142 case UR_GET_DESCRIPTOR:
1143 goto tr_handle_get_descriptor;
1147 case UT_READ_CLASS_DEVICE:
1148 switch (req->bRequest) {
1149 case UR_GET_DESCRIPTOR:
1150 goto tr_handle_get_class_descriptor;
1158 tr_handle_get_descriptor:
1159 switch (req->wValue[1]) {
1161 if (req->wValue[0]) {
1164 buf = usb_temp_get_device_desc(udev);
1166 case UDESC_DEVICE_QUALIFIER:
1167 if (udev->speed != USB_SPEED_HIGH) {
1170 if (req->wValue[0]) {
1173 buf = usb_temp_get_qualifier_desc(udev);
1175 case UDESC_OTHER_SPEED_CONFIGURATION:
1176 if (udev->speed != USB_SPEED_HIGH) {
1180 buf = usb_temp_get_config_desc(udev,
1181 &len, req->wValue[0]);
1184 buf = usb_temp_get_string_desc(udev,
1185 UGETW(req->wIndex), req->wValue[0]);
1191 tr_handle_get_class_descriptor:
1192 if (req->wValue[0]) {
1195 buf = usb_temp_get_hub_desc(udev);
1205 return (0); /* success */
1208 /* try to get a vendor specific descriptor */
1210 buf = usb_temp_get_vendor_desc(udev, req, &len);
1215 return (0); /* we ignore failures */
1218 /*------------------------------------------------------------------------*
1221 * This function generates USB descriptors according to the given USB
1222 * template device descriptor. It will also try to figure out the best
1223 * matching endpoint addresses using the hardware endpoint profiles.
1228 *------------------------------------------------------------------------*/
1230 usb_temp_setup(struct usb_device *udev,
1231 const struct usb_temp_device_desc *tdd)
1233 struct usb_temp_setup *uts;
1241 uts = udev->bus->scratch[0].temp_setup;
1243 bzero(uts, sizeof(*uts));
1245 uts->usb_speed = udev->speed;
1246 uts->self_powered = udev->flags.self_powered;
1250 usb_make_device_desc(uts, tdd);
1253 /* some error happened */
1257 if (uts->size == 0) {
1258 return (USB_ERR_INVAL);
1260 /* allocate zeroed memory */
1261 uts->buf = malloc(uts->size, M_USB, M_WAITOK | M_ZERO);
1262 if (uts->buf == NULL) {
1263 /* could not allocate memory */
1264 return (USB_ERR_NOMEM);
1270 usb_make_device_desc(uts, tdd);
1273 * Store a pointer to our descriptors:
1275 udev->usb_template_ptr = uts->buf;
1278 /* some error happened during second pass */
1282 * Resolve all endpoint addresses !
1284 buf = usb_temp_get_device_desc(udev);
1285 uts->err = usb_hw_ep_resolve(udev, buf);
1287 DPRINTFN(0, "Could not resolve endpoints for "
1288 "Device Descriptor, error = %s\n",
1289 usbd_errstr(uts->err));
1294 buf = usb_temp_get_config_desc(udev, NULL, n);
1298 uts->err = usb_hw_ep_resolve(udev, buf);
1300 DPRINTFN(0, "Could not resolve endpoints for "
1301 "Config Descriptor %u, error = %s\n", n,
1302 usbd_errstr(uts->err));
1309 usb_temp_unsetup(udev);
1313 /*------------------------------------------------------------------------*
1316 * This function frees any memory associated with the currently
1317 * setup template, if any.
1318 *------------------------------------------------------------------------*/
1320 usb_temp_unsetup(struct usb_device *udev)
1322 if (udev->usb_template_ptr) {
1324 free(udev->usb_template_ptr, M_USB);
1326 udev->usb_template_ptr = NULL;
1331 usb_temp_setup_by_index(struct usb_device *udev, uint16_t index)
1337 err = usb_temp_setup(udev, &usb_template_msc);
1340 err = usb_temp_setup(udev, &usb_template_cdce);
1343 err = usb_temp_setup(udev, &usb_template_mtp);
1345 case USB_TEMP_MODEM:
1346 err = usb_temp_setup(udev, &usb_template_modem);
1348 case USB_TEMP_AUDIO:
1349 err = usb_temp_setup(udev, &usb_template_audio);
1352 err = usb_temp_setup(udev, &usb_template_kbd);
1354 case USB_TEMP_MOUSE:
1355 err = usb_temp_setup(udev, &usb_template_mouse);
1358 return (USB_ERR_INVAL);
1365 usb_temp_init(void *arg)
1367 /* register our functions */
1368 usb_temp_get_desc_p = &usb_temp_get_desc;
1369 usb_temp_setup_by_index_p = &usb_temp_setup_by_index;
1370 usb_temp_unsetup_p = &usb_temp_unsetup;
1373 SYSINIT(usb_temp_init, SI_SUB_LOCK, SI_ORDER_FIRST, usb_temp_init, NULL);
1374 SYSUNINIT(usb_temp_unload, SI_SUB_LOCK, SI_ORDER_ANY, usb_temp_unload, NULL);