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 = 4; /* 1 ms */
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 = 0xFF;
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);
910 if (desc->bLength < sizeof(*(ues->cd)))
911 return (USB_ERR_INVAL);
913 ues = udev->scratch.hw_ep_scratch;
915 memset(ues, 0, sizeof(*ues));
917 ues->ep_max = ues->ep;
918 ues->cd = (void *)desc;
919 ues->methods = methods;
922 /* Get all the endpoints we need */
924 if (usb_hw_ep_get_needs(ues, UE_ISOCHRONOUS, 0) ||
925 usb_hw_ep_get_needs(ues, UE_INTERRUPT, 0) ||
926 usb_hw_ep_get_needs(ues, UE_CONTROL, 0) ||
927 usb_hw_ep_get_needs(ues, UE_BULK, 0)) {
928 DPRINTFN(0, "Could not get needs\n");
929 return (USB_ERR_INVAL);
931 for (ep = ues->ep; ep != ues->ep_max; ep++) {
933 while (ep->needs_in || ep->needs_out) {
936 * First try to use a simplex endpoint.
937 * Then try to use a duplex endpoint.
939 if (usb_hw_ep_find_match(ues, ep, 1) &&
940 usb_hw_ep_find_match(ues, ep, 0)) {
941 DPRINTFN(0, "Could not find match\n");
942 return (USB_ERR_INVAL);
947 ues->ep_max = ues->ep;
949 /* Update all endpoint addresses */
951 if (usb_hw_ep_get_needs(ues, UE_ISOCHRONOUS, 1) ||
952 usb_hw_ep_get_needs(ues, UE_INTERRUPT, 1) ||
953 usb_hw_ep_get_needs(ues, UE_CONTROL, 1) ||
954 usb_hw_ep_get_needs(ues, UE_BULK, 1)) {
955 DPRINTFN(0, "Could not update endpoint address\n");
956 return (USB_ERR_INVAL);
958 return (0); /* success */
961 /*------------------------------------------------------------------------*
965 * NULL: No USB template device descriptor found.
966 * Else: Pointer to the USB template device descriptor.
967 *------------------------------------------------------------------------*/
968 static const struct usb_temp_device_desc *
969 usb_temp_get_tdd(struct usb_device *udev)
971 if (udev->usb_template_ptr == NULL) {
974 return (udev->usb_template_ptr->tdd);
977 /*------------------------------------------------------------------------*
978 * usb_temp_get_device_desc
981 * NULL: No USB device descriptor found.
982 * Else: Pointer to USB device descriptor.
983 *------------------------------------------------------------------------*/
985 usb_temp_get_device_desc(struct usb_device *udev)
987 struct usb_device_descriptor *dd;
989 if (udev->usb_template_ptr == NULL) {
992 dd = &udev->usb_template_ptr->udd;
993 if (dd->bDescriptorType != UDESC_DEVICE) {
994 /* sanity check failed */
1000 /*------------------------------------------------------------------------*
1001 * usb_temp_get_qualifier_desc
1004 * NULL: No USB device_qualifier descriptor found.
1005 * Else: Pointer to USB device_qualifier descriptor.
1006 *------------------------------------------------------------------------*/
1008 usb_temp_get_qualifier_desc(struct usb_device *udev)
1010 struct usb_device_qualifier *dq;
1012 if (udev->usb_template_ptr == NULL) {
1015 dq = &udev->usb_template_ptr->udq;
1016 if (dq->bDescriptorType != UDESC_DEVICE_QUALIFIER) {
1017 /* sanity check failed */
1023 /*------------------------------------------------------------------------*
1024 * usb_temp_get_config_desc
1027 * NULL: No USB config descriptor found.
1028 * Else: Pointer to USB config descriptor having index "index".
1029 *------------------------------------------------------------------------*/
1031 usb_temp_get_config_desc(struct usb_device *udev,
1032 uint16_t *pLength, uint8_t index)
1034 struct usb_device_descriptor *dd;
1035 struct usb_config_descriptor *cd;
1038 if (udev->usb_template_ptr == NULL) {
1041 dd = &udev->usb_template_ptr->udd;
1042 cd = (void *)(udev->usb_template_ptr + 1);
1044 if (index >= dd->bNumConfigurations) {
1049 if (cd->bDescriptorType != UDESC_CONFIG) {
1050 /* sanity check failed */
1053 temp = UGETW(cd->wTotalLength);
1054 cd = USB_ADD_BYTES(cd, temp);
1058 *pLength = UGETW(cd->wTotalLength);
1063 /*------------------------------------------------------------------------*
1064 * usb_temp_get_vendor_desc
1067 * NULL: No vendor descriptor found.
1068 * Else: Pointer to a vendor descriptor.
1069 *------------------------------------------------------------------------*/
1071 usb_temp_get_vendor_desc(struct usb_device *udev,
1072 const struct usb_device_request *req, uint16_t *plen)
1074 const struct usb_temp_device_desc *tdd;
1076 tdd = usb_temp_get_tdd(udev);
1080 if (tdd->getVendorDesc == NULL) {
1083 return ((tdd->getVendorDesc) (req, plen));
1086 /*------------------------------------------------------------------------*
1087 * usb_temp_get_string_desc
1090 * NULL: No string descriptor found.
1091 * Else: Pointer to a string descriptor.
1092 *------------------------------------------------------------------------*/
1094 usb_temp_get_string_desc(struct usb_device *udev,
1095 uint16_t lang_id, uint8_t string_index)
1097 const struct usb_temp_device_desc *tdd;
1099 tdd = usb_temp_get_tdd(udev);
1103 if (tdd->getStringDesc == NULL) {
1106 return ((tdd->getStringDesc) (lang_id, string_index));
1109 /*------------------------------------------------------------------------*
1110 * usb_temp_get_hub_desc
1113 * NULL: No USB HUB descriptor found.
1114 * Else: Pointer to a USB HUB descriptor.
1115 *------------------------------------------------------------------------*/
1117 usb_temp_get_hub_desc(struct usb_device *udev)
1119 return (NULL); /* needs to be implemented */
1122 /*------------------------------------------------------------------------*
1125 * This function is a demultiplexer for local USB device side control
1126 * endpoint requests.
1127 *------------------------------------------------------------------------*/
1129 usb_temp_get_desc(struct usb_device *udev, struct usb_device_request *req,
1130 const void **pPtr, uint16_t *pLength)
1138 switch (req->bmRequestType) {
1139 case UT_READ_DEVICE:
1140 switch (req->bRequest) {
1141 case UR_GET_DESCRIPTOR:
1142 goto tr_handle_get_descriptor;
1146 case UT_READ_CLASS_DEVICE:
1147 switch (req->bRequest) {
1148 case UR_GET_DESCRIPTOR:
1149 goto tr_handle_get_class_descriptor;
1157 tr_handle_get_descriptor:
1158 switch (req->wValue[1]) {
1160 if (req->wValue[0]) {
1163 buf = usb_temp_get_device_desc(udev);
1165 case UDESC_DEVICE_QUALIFIER:
1166 if (udev->speed != USB_SPEED_HIGH) {
1169 if (req->wValue[0]) {
1172 buf = usb_temp_get_qualifier_desc(udev);
1174 case UDESC_OTHER_SPEED_CONFIGURATION:
1175 if (udev->speed != USB_SPEED_HIGH) {
1179 buf = usb_temp_get_config_desc(udev,
1180 &len, req->wValue[0]);
1183 buf = usb_temp_get_string_desc(udev,
1184 UGETW(req->wIndex), req->wValue[0]);
1190 tr_handle_get_class_descriptor:
1191 if (req->wValue[0]) {
1194 buf = usb_temp_get_hub_desc(udev);
1204 return (0); /* success */
1207 /* try to get a vendor specific descriptor */
1209 buf = usb_temp_get_vendor_desc(udev, req, &len);
1214 return (0); /* we ignore failures */
1217 /*------------------------------------------------------------------------*
1220 * This function generates USB descriptors according to the given USB
1221 * template device descriptor. It will also try to figure out the best
1222 * matching endpoint addresses using the hardware endpoint profiles.
1227 *------------------------------------------------------------------------*/
1229 usb_temp_setup(struct usb_device *udev,
1230 const struct usb_temp_device_desc *tdd)
1232 struct usb_temp_setup *uts;
1242 /* Protect scratch area */
1243 do_unlock = usbd_ctrl_lock(udev);
1245 uts = udev->scratch.temp_setup;
1247 memset(uts, 0, sizeof(*uts));
1249 uts->usb_speed = udev->speed;
1250 uts->self_powered = udev->flags.self_powered;
1254 usb_make_device_desc(uts, tdd);
1257 /* some error happened */
1261 if (uts->size == 0) {
1262 uts->err = USB_ERR_INVAL;
1265 /* allocate zeroed memory */
1266 uts->buf = malloc(uts->size, M_USB, M_WAITOK | M_ZERO);
1268 * Allow malloc() to return NULL regardless of M_WAITOK flag.
1269 * This helps when porting the software to non-FreeBSD
1272 if (uts->buf == NULL) {
1273 /* could not allocate memory */
1274 uts->err = USB_ERR_NOMEM;
1281 usb_make_device_desc(uts, tdd);
1284 * Store a pointer to our descriptors:
1286 udev->usb_template_ptr = uts->buf;
1289 /* some error happened during second pass */
1293 * Resolve all endpoint addresses !
1295 buf = usb_temp_get_device_desc(udev);
1296 uts->err = usb_hw_ep_resolve(udev, buf);
1298 DPRINTFN(0, "Could not resolve endpoints for "
1299 "Device Descriptor, error = %s\n",
1300 usbd_errstr(uts->err));
1305 buf = usb_temp_get_config_desc(udev, NULL, n);
1309 uts->err = usb_hw_ep_resolve(udev, buf);
1311 DPRINTFN(0, "Could not resolve endpoints for "
1312 "Config Descriptor %u, error = %s\n", n,
1313 usbd_errstr(uts->err));
1320 usb_temp_unsetup(udev);
1322 usbd_ctrl_unlock(udev);
1326 /*------------------------------------------------------------------------*
1329 * This function frees any memory associated with the currently
1330 * setup template, if any.
1331 *------------------------------------------------------------------------*/
1333 usb_temp_unsetup(struct usb_device *udev)
1335 if (udev->usb_template_ptr) {
1337 free(udev->usb_template_ptr, M_USB);
1339 udev->usb_template_ptr = NULL;
1344 usb_temp_setup_by_index(struct usb_device *udev, uint16_t index)
1350 err = usb_temp_setup(udev, &usb_template_msc);
1353 err = usb_temp_setup(udev, &usb_template_cdce);
1356 err = usb_temp_setup(udev, &usb_template_mtp);
1358 case USB_TEMP_MODEM:
1359 err = usb_temp_setup(udev, &usb_template_modem);
1361 case USB_TEMP_AUDIO:
1362 err = usb_temp_setup(udev, &usb_template_audio);
1365 err = usb_temp_setup(udev, &usb_template_kbd);
1367 case USB_TEMP_MOUSE:
1368 err = usb_temp_setup(udev, &usb_template_mouse);
1371 return (USB_ERR_INVAL);
1378 usb_temp_init(void *arg)
1380 /* register our functions */
1381 usb_temp_get_desc_p = &usb_temp_get_desc;
1382 usb_temp_setup_by_index_p = &usb_temp_setup_by_index;
1383 usb_temp_unsetup_p = &usb_temp_unsetup;
1386 SYSINIT(usb_temp_init, SI_SUB_LOCK, SI_ORDER_FIRST, usb_temp_init, NULL);
1387 SYSUNINIT(usb_temp_unload, SI_SUB_LOCK, SI_ORDER_ANY, usb_temp_unload, NULL);