3 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 * Copyright (c) 2007 Hans Petter Selasky. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * This file contains sub-routines to build up USB descriptors from
34 #ifdef USB_GLOBAL_INCLUDE_FILE
35 #include USB_GLOBAL_INCLUDE_FILE
37 #include <sys/stdint.h>
38 #include <sys/stddef.h>
39 #include <sys/param.h>
40 #include <sys/queue.h>
41 #include <sys/types.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
45 #include <sys/module.h>
47 #include <sys/mutex.h>
48 #include <sys/condvar.h>
49 #include <sys/sysctl.h>
51 #include <sys/unistd.h>
52 #include <sys/callout.h>
53 #include <sys/malloc.h>
56 #include <dev/usb/usb.h>
57 #include <dev/usb/usb_ioctl.h>
58 #include <dev/usb/usbdi.h>
59 #include <dev/usb/usbdi_util.h>
62 #include <dev/usb/usb_cdc.h>
63 #include <dev/usb/usb_core.h>
64 #include <dev/usb/usb_dynamic.h>
65 #include <dev/usb/usb_busdma.h>
66 #include <dev/usb/usb_process.h>
67 #include <dev/usb/usb_device.h>
68 #include <dev/usb/usb_util.h>
70 #define USB_DEBUG_VAR usb_debug
71 #include <dev/usb/usb_debug.h>
73 #include <dev/usb/usb_controller.h>
74 #include <dev/usb/usb_bus.h>
75 #include <dev/usb/usb_request.h>
76 #include <dev/usb/template/usb_template.h>
77 #endif /* USB_GLOBAL_INCLUDE_FILE */
79 MODULE_DEPEND(usb_template, usb, 1, 1, 1);
80 MODULE_VERSION(usb_template, 1);
82 /* function prototypes */
84 static int sysctl_hw_usb_template_power(SYSCTL_HANDLER_ARGS);
85 static void usb_make_raw_desc(struct usb_temp_setup *, const uint8_t *);
86 static void usb_make_endpoint_desc(struct usb_temp_setup *,
87 const struct usb_temp_endpoint_desc *);
88 static void usb_make_interface_desc(struct usb_temp_setup *,
89 const struct usb_temp_interface_desc *);
90 static void usb_make_config_desc(struct usb_temp_setup *,
91 const struct usb_temp_config_desc *);
92 static void usb_make_device_desc(struct usb_temp_setup *,
93 const struct usb_temp_device_desc *);
94 static uint8_t usb_hw_ep_match(const struct usb_hw_ep_profile *, uint8_t,
96 static uint8_t usb_hw_ep_find_match(struct usb_hw_ep_scratch *,
97 struct usb_hw_ep_scratch_sub *, uint8_t);
98 static uint8_t usb_hw_ep_get_needs(struct usb_hw_ep_scratch *, uint8_t,
100 static usb_error_t usb_hw_ep_resolve(struct usb_device *,
101 struct usb_descriptor *);
102 static const struct usb_temp_device_desc *usb_temp_get_tdd(struct usb_device *);
103 static void *usb_temp_get_device_desc(struct usb_device *);
104 static void *usb_temp_get_qualifier_desc(struct usb_device *);
105 static void *usb_temp_get_config_desc(struct usb_device *, uint16_t *,
107 static const void *usb_temp_get_string_desc(struct usb_device *, uint16_t,
109 static const void *usb_temp_get_vendor_desc(struct usb_device *,
110 const struct usb_device_request *, uint16_t *plen);
111 static const void *usb_temp_get_hub_desc(struct usb_device *);
112 static usb_error_t usb_temp_get_desc(struct usb_device *,
113 struct usb_device_request *, const void **, uint16_t *);
114 static usb_error_t usb_temp_setup_by_index(struct usb_device *,
116 static void usb_temp_init(void *);
118 SYSCTL_NODE(_hw_usb, OID_AUTO, templates, CTLFLAG_RW, 0,
119 "USB device side templates");
120 SYSCTL_PROC(_hw_usb, OID_AUTO, template_power,
121 CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
122 NULL, 0, sysctl_hw_usb_template_power,
123 "I", "USB bus power consumption in mA at 5V");
125 static int usb_template_power = 500; /* 500mA */
128 sysctl_hw_usb_template_power(SYSCTL_HANDLER_ARGS)
132 val = usb_template_power;
133 error = sysctl_handle_int(oidp, &val, 0, req);
134 if (error != 0 || req->newptr == NULL)
137 if (val < 0 || val > 500)
140 usb_template_power = val;
145 /*------------------------------------------------------------------------*
146 * usb_decode_str_desc
148 * Helper function to decode string descriptors into a C string.
149 *------------------------------------------------------------------------*/
151 usb_decode_str_desc(struct usb_string_descriptor *sd, char *buf, size_t buflen)
155 if (sd->bLength < 2) {
160 for (i = 0; i < buflen - 1 && i < (sd->bLength / 2) - 1; i++)
161 buf[i] = UGETW(sd->bString[i]);
166 /*------------------------------------------------------------------------*
169 * Callback for SYSCTL_PROC(9), to set and retrieve template string
171 *------------------------------------------------------------------------*/
173 usb_temp_sysctl(SYSCTL_HANDLER_ARGS)
176 struct usb_string_descriptor *sd = arg1;
177 size_t len, sdlen = arg2;
180 usb_decode_str_desc(sd, buf, sizeof(buf));
182 error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
183 if (error != 0 || req->newptr == NULL)
186 len = usb_make_str_desc(sd, sdlen, buf);
194 /*------------------------------------------------------------------------*
197 * This function will insert a raw USB descriptor into the generated
199 *------------------------------------------------------------------------*/
201 usb_make_raw_desc(struct usb_temp_setup *temp,
208 * The first byte of any USB descriptor gives the length.
213 dst = USB_ADD_BYTES(temp->buf, temp->size);
214 memcpy(dst, raw, len);
216 /* check if we have got a CDC union descriptor */
218 if ((raw[0] == sizeof(struct usb_cdc_union_descriptor)) &&
219 (raw[1] == UDESC_CS_INTERFACE) &&
220 (raw[2] == UDESCSUB_CDC_UNION)) {
221 struct usb_cdc_union_descriptor *ud = (void *)dst;
223 /* update the interface numbers */
225 ud->bMasterInterface +=
226 temp->bInterfaceNumber;
227 ud->bSlaveInterface[0] +=
228 temp->bInterfaceNumber;
231 /* check if we have got an interface association descriptor */
233 if ((raw[0] == sizeof(struct usb_interface_assoc_descriptor)) &&
234 (raw[1] == UDESC_IFACE_ASSOC)) {
235 struct usb_interface_assoc_descriptor *iad = (void *)dst;
237 /* update the interface number */
239 iad->bFirstInterface +=
240 temp->bInterfaceNumber;
243 /* check if we have got a call management descriptor */
245 if ((raw[0] == sizeof(struct usb_cdc_cm_descriptor)) &&
246 (raw[1] == UDESC_CS_INTERFACE) &&
247 (raw[2] == UDESCSUB_CDC_CM)) {
248 struct usb_cdc_cm_descriptor *ccd = (void *)dst;
250 /* update the interface number */
252 ccd->bDataInterface +=
253 temp->bInterfaceNumber;
260 /*------------------------------------------------------------------------*
261 * usb_make_endpoint_desc
263 * This function will generate an USB endpoint descriptor from the
264 * given USB template endpoint descriptor, which will be inserted into
265 * the USB configuration.
266 *------------------------------------------------------------------------*/
268 usb_make_endpoint_desc(struct usb_temp_setup *temp,
269 const struct usb_temp_endpoint_desc *ted)
271 struct usb_endpoint_descriptor *ed;
275 uint8_t ea; /* Endpoint Address */
276 uint8_t et; /* Endpiont Type */
279 old_size = temp->size;
281 ea = (ted->bEndpointAddress & (UE_ADDR | UE_DIR_IN | UE_DIR_OUT));
282 et = (ted->bmAttributes & UE_XFERTYPE);
284 if (et == UE_ISOCHRONOUS) {
285 /* account for extra byte fields */
286 temp->size += sizeof(*ed) + 2;
288 temp->size += sizeof(*ed);
291 /* Scan all Raw Descriptors first */
295 usb_make_raw_desc(temp, *rd);
299 if (ted->pPacketSize == NULL) {
300 /* not initialized */
301 temp->err = USB_ERR_INVAL;
304 mps = ted->pPacketSize->mps[temp->usb_speed];
306 /* not initialized */
307 temp->err = USB_ERR_INVAL;
309 } else if (mps == UE_ZERO_MPS) {
310 /* escape for Zero Max Packet Size */
315 * Fill out the real USB endpoint descriptor
316 * in case there is a buffer present:
319 ed = USB_ADD_BYTES(temp->buf, old_size);
320 if (et == UE_ISOCHRONOUS)
321 ed->bLength = sizeof(*ed) + 2;
323 ed->bLength = sizeof(*ed);
324 ed->bDescriptorType = UDESC_ENDPOINT;
325 ed->bEndpointAddress = ea;
326 ed->bmAttributes = ted->bmAttributes;
327 USETW(ed->wMaxPacketSize, mps);
329 /* setup bInterval parameter */
331 if (ted->pIntervals &&
332 ted->pIntervals->bInterval[temp->usb_speed]) {
334 ted->pIntervals->bInterval[temp->usb_speed];
339 ed->bInterval = 0; /* not used */
342 switch (temp->usb_speed) {
345 ed->bInterval = 1; /* 1 ms */
348 ed->bInterval = 4; /* 1 ms */
352 default: /* UE_ISOCHRONOUS */
353 switch (temp->usb_speed) {
356 ed->bInterval = 1; /* 1 ms */
359 ed->bInterval = 1; /* 125 us */
366 temp->bNumEndpoints++;
369 /*------------------------------------------------------------------------*
370 * usb_make_interface_desc
372 * This function will generate an USB interface descriptor from the
373 * given USB template interface descriptor, which will be inserted
374 * into the USB configuration.
375 *------------------------------------------------------------------------*/
377 usb_make_interface_desc(struct usb_temp_setup *temp,
378 const struct usb_temp_interface_desc *tid)
380 struct usb_interface_descriptor *id;
381 const struct usb_temp_endpoint_desc **ted;
387 old_size = temp->size;
388 temp->size += sizeof(*id);
390 /* Update interface and alternate interface numbers */
392 if (tid->isAltInterface == 0) {
393 temp->bAlternateSetting = 0;
394 temp->bInterfaceNumber++;
396 temp->bAlternateSetting++;
399 /* Scan all Raw Descriptors first */
405 usb_make_raw_desc(temp, *rd);
409 /* Reset some counters */
411 temp->bNumEndpoints = 0;
413 /* Scan all Endpoint Descriptors second */
415 ted = tid->ppEndpoints;
418 usb_make_endpoint_desc(temp, *ted);
423 * Fill out the real USB interface descriptor
424 * in case there is a buffer present:
427 id = USB_ADD_BYTES(temp->buf, old_size);
428 id->bLength = sizeof(*id);
429 id->bDescriptorType = UDESC_INTERFACE;
430 id->bInterfaceNumber = temp->bInterfaceNumber;
431 id->bAlternateSetting = temp->bAlternateSetting;
432 id->bNumEndpoints = temp->bNumEndpoints;
433 id->bInterfaceClass = tid->bInterfaceClass;
434 id->bInterfaceSubClass = tid->bInterfaceSubClass;
435 id->bInterfaceProtocol = tid->bInterfaceProtocol;
436 id->iInterface = tid->iInterface;
440 /*------------------------------------------------------------------------*
441 * usb_make_config_desc
443 * This function will generate an USB config descriptor from the given
444 * USB template config descriptor, which will be inserted into the USB
446 *------------------------------------------------------------------------*/
448 usb_make_config_desc(struct usb_temp_setup *temp,
449 const struct usb_temp_config_desc *tcd)
451 struct usb_config_descriptor *cd;
452 const struct usb_temp_interface_desc **tid;
458 old_size = temp->size;
459 temp->size += sizeof(*cd);
461 /* Reset some counters */
463 temp->bInterfaceNumber = 0xFF;
464 temp->bAlternateSetting = 0;
466 /* Scan all the USB interfaces */
468 tid = tcd->ppIfaceDesc;
471 usb_make_interface_desc(temp, *tid);
476 * Fill out the real USB config descriptor
477 * in case there is a buffer present:
480 cd = USB_ADD_BYTES(temp->buf, old_size);
482 /* compute total size */
483 old_size = temp->size - old_size;
485 cd->bLength = sizeof(*cd);
486 cd->bDescriptorType = UDESC_CONFIG;
487 USETW(cd->wTotalLength, old_size);
488 cd->bNumInterface = temp->bInterfaceNumber + 1;
489 cd->bConfigurationValue = temp->bConfigurationValue;
490 cd->iConfiguration = tcd->iConfiguration;
491 cd->bmAttributes = tcd->bmAttributes;
493 power = usb_template_power;
494 cd->bMaxPower = power / 2; /* 2 mA units */
495 cd->bmAttributes |= UC_REMOTE_WAKEUP;
497 cd->bmAttributes |= UC_BUS_POWERED;
498 cd->bmAttributes &= ~UC_SELF_POWERED;
500 cd->bmAttributes &= ~UC_BUS_POWERED;
501 cd->bmAttributes |= UC_SELF_POWERED;
506 /*------------------------------------------------------------------------*
507 * usb_make_device_desc
509 * This function will generate an USB device descriptor from the
510 * given USB template device descriptor.
511 *------------------------------------------------------------------------*/
513 usb_make_device_desc(struct usb_temp_setup *temp,
514 const struct usb_temp_device_desc *tdd)
516 struct usb_temp_data *utd;
517 const struct usb_temp_config_desc **tcd;
522 old_size = temp->size;
523 temp->size += sizeof(*utd);
525 /* Scan all the USB configs */
527 temp->bConfigurationValue = 1;
528 tcd = tdd->ppConfigDesc;
531 usb_make_config_desc(temp, *tcd);
532 temp->bConfigurationValue++;
537 * Fill out the real USB device descriptor
538 * in case there is a buffer present:
542 utd = USB_ADD_BYTES(temp->buf, old_size);
544 /* Store a pointer to our template device descriptor */
547 /* Fill out USB device descriptor */
548 utd->udd.bLength = sizeof(utd->udd);
549 utd->udd.bDescriptorType = UDESC_DEVICE;
550 utd->udd.bDeviceClass = tdd->bDeviceClass;
551 utd->udd.bDeviceSubClass = tdd->bDeviceSubClass;
552 utd->udd.bDeviceProtocol = tdd->bDeviceProtocol;
553 USETW(utd->udd.idVendor, tdd->idVendor);
554 USETW(utd->udd.idProduct, tdd->idProduct);
555 USETW(utd->udd.bcdDevice, tdd->bcdDevice);
556 utd->udd.iManufacturer = tdd->iManufacturer;
557 utd->udd.iProduct = tdd->iProduct;
558 utd->udd.iSerialNumber = tdd->iSerialNumber;
559 utd->udd.bNumConfigurations = temp->bConfigurationValue - 1;
562 * Fill out the USB device qualifier. Pretend that we
563 * don't support any other speeds by setting
564 * "bNumConfigurations" equal to zero. That saves us
565 * generating an extra set of configuration
568 utd->udq.bLength = sizeof(utd->udq);
569 utd->udq.bDescriptorType = UDESC_DEVICE_QUALIFIER;
570 utd->udq.bDeviceClass = tdd->bDeviceClass;
571 utd->udq.bDeviceSubClass = tdd->bDeviceSubClass;
572 utd->udq.bDeviceProtocol = tdd->bDeviceProtocol;
573 utd->udq.bNumConfigurations = 0;
574 USETW(utd->udq.bcdUSB, 0x0200);
575 utd->udq.bMaxPacketSize0 = 0;
577 switch (temp->usb_speed) {
579 USETW(utd->udd.bcdUSB, 0x0110);
580 utd->udd.bMaxPacketSize = 8;
583 USETW(utd->udd.bcdUSB, 0x0110);
584 utd->udd.bMaxPacketSize = 32;
587 USETW(utd->udd.bcdUSB, 0x0200);
588 utd->udd.bMaxPacketSize = 64;
590 case USB_SPEED_VARIABLE:
591 USETW(utd->udd.bcdUSB, 0x0250);
592 utd->udd.bMaxPacketSize = 255; /* 512 bytes */
594 case USB_SPEED_SUPER:
595 USETW(utd->udd.bcdUSB, 0x0300);
596 utd->udd.bMaxPacketSize = 9; /* 2**9 = 512 bytes */
599 temp->err = USB_ERR_INVAL;
605 /*------------------------------------------------------------------------*
609 * 0: The endpoint profile does not match the criteria
610 * Else: The endpoint profile matches the criteria
611 *------------------------------------------------------------------------*/
613 usb_hw_ep_match(const struct usb_hw_ep_profile *pf,
614 uint8_t ep_type, uint8_t ep_dir_in)
616 if (ep_type == UE_CONTROL) {
618 return (pf->support_control);
620 if ((pf->support_in && ep_dir_in) ||
621 (pf->support_out && !ep_dir_in)) {
622 if ((pf->support_interrupt && (ep_type == UE_INTERRUPT)) ||
623 (pf->support_isochronous && (ep_type == UE_ISOCHRONOUS)) ||
624 (pf->support_bulk && (ep_type == UE_BULK))) {
631 /*------------------------------------------------------------------------*
632 * usb_hw_ep_find_match
634 * This function is used to find the best matching endpoint profile
635 * for and endpoint belonging to an USB descriptor.
638 * 0: Success. Got a match.
639 * Else: Failure. No match.
640 *------------------------------------------------------------------------*/
642 usb_hw_ep_find_match(struct usb_hw_ep_scratch *ues,
643 struct usb_hw_ep_scratch_sub *ep, uint8_t is_simplex)
645 const struct usb_hw_ep_profile *pf;
648 uint16_t max_frame_size;
657 if ((!ep->needs_in) && (!ep->needs_out)) {
658 return (0); /* we are done */
660 if (ep->needs_ep_type == UE_CONTROL) {
673 for (n = 1; n != (USB_EP_MAX / 2); n++) {
675 /* get HW endpoint profile */
676 (ues->methods->get_hw_ep_profile) (ues->udev, &pf, n);
678 /* end of profiles */
681 /* check if IN-endpoint is reserved */
682 if (dir_in || pf->is_simplex) {
683 if (ues->bmInAlloc[n / 8] & (1 << (n % 8))) {
688 /* check if OUT-endpoint is reserved */
689 if (dir_out || pf->is_simplex) {
690 if (ues->bmOutAlloc[n / 8] & (1 << (n % 8))) {
696 if (pf->is_simplex == is_simplex) {
700 /* check if HW endpoint matches */
701 if (!usb_hw_ep_match(pf, ep->needs_ep_type, dir_in)) {
705 /* get maximum frame size */
707 max_frame_size = pf->max_in_frame_size;
709 max_frame_size = pf->max_out_frame_size;
711 /* check if we have a matching profile */
712 if (max_frame_size >= ep->max_frame_size) {
713 temp = (max_frame_size - ep->max_frame_size);
714 if (distance > temp) {
722 /* see if we got a match */
724 /* get the correct profile */
727 /* reserve IN-endpoint */
729 ues->bmInAlloc[best_n / 8] |=
731 ep->hw_endpoint_in = best_n | UE_DIR_IN;
734 /* reserve OUT-endpoint */
736 ues->bmOutAlloc[best_n / 8] |=
738 ep->hw_endpoint_out = best_n | UE_DIR_OUT;
741 return (0); /* got a match */
743 return (1); /* failure */
746 /*------------------------------------------------------------------------*
747 * usb_hw_ep_get_needs
749 * This function will figure out the type and number of endpoints
750 * which are needed for an USB configuration.
755 *------------------------------------------------------------------------*/
757 usb_hw_ep_get_needs(struct usb_hw_ep_scratch *ues,
758 uint8_t ep_type, uint8_t is_complete)
760 const struct usb_hw_ep_profile *pf;
761 struct usb_hw_ep_scratch_sub *ep_iface;
762 struct usb_hw_ep_scratch_sub *ep_curr;
763 struct usb_hw_ep_scratch_sub *ep_max;
764 struct usb_hw_ep_scratch_sub *ep_end;
765 struct usb_descriptor *desc;
766 struct usb_interface_descriptor *id;
767 struct usb_endpoint_descriptor *ed;
768 enum usb_dev_speed speed;
769 uint16_t wMaxPacketSize;
773 ep_iface = ues->ep_max;
774 ep_curr = ues->ep_max;
775 ep_end = ues->ep + USB_EP_MAX;
776 ep_max = ues->ep_max;
778 speed = usbd_get_speed(ues->udev);
782 while ((desc = usb_desc_foreach(ues->cd, desc))) {
784 if ((desc->bDescriptorType == UDESC_INTERFACE) &&
785 (desc->bLength >= sizeof(*id))) {
789 if (id->bAlternateSetting == 0) {
797 if ((desc->bDescriptorType == UDESC_ENDPOINT) &&
798 (desc->bLength >= sizeof(*ed))) {
802 goto handle_endpoint_desc;
805 ues->ep_max = ep_max;
808 handle_endpoint_desc:
809 temp = (ed->bmAttributes & UE_XFERTYPE);
811 if (temp == ep_type) {
813 if (ep_curr == ep_end) {
814 /* too many endpoints */
815 return (1); /* failure */
817 wMaxPacketSize = UGETW(ed->wMaxPacketSize);
818 if ((wMaxPacketSize & 0xF800) &&
819 (speed == USB_SPEED_HIGH)) {
820 /* handle packet multiplier */
821 temp = (wMaxPacketSize >> 11) & 3;
822 wMaxPacketSize &= 0x7FF;
830 * Check if we have a fixed endpoint number, else the
831 * endpoint number is allocated dynamically:
833 ep_no = (ed->bEndpointAddress & UE_ADDR);
836 /* get HW endpoint profile */
837 (ues->methods->get_hw_ep_profile)
838 (ues->udev, &pf, ep_no);
840 /* HW profile does not exist - failure */
841 DPRINTFN(0, "Endpoint profile %u "
842 "does not exist\n", ep_no);
845 /* reserve fixed endpoint number */
846 if (ep_type == UE_CONTROL) {
847 ues->bmInAlloc[ep_no / 8] |=
849 ues->bmOutAlloc[ep_no / 8] |=
851 if ((pf->max_in_frame_size < wMaxPacketSize) ||
852 (pf->max_out_frame_size < wMaxPacketSize)) {
853 DPRINTFN(0, "Endpoint profile %u "
854 "has too small buffer\n", ep_no);
857 } else if (ed->bEndpointAddress & UE_DIR_IN) {
858 ues->bmInAlloc[ep_no / 8] |=
860 if (pf->max_in_frame_size < wMaxPacketSize) {
861 DPRINTFN(0, "Endpoint profile %u "
862 "has too small buffer\n", ep_no);
866 ues->bmOutAlloc[ep_no / 8] |=
868 if (pf->max_out_frame_size < wMaxPacketSize) {
869 DPRINTFN(0, "Endpoint profile %u "
870 "has too small buffer\n", ep_no);
874 } else if (is_complete) {
876 /* check if we have enough buffer space */
878 ep_curr->max_frame_size) {
879 return (1); /* failure */
881 if (ed->bEndpointAddress & UE_DIR_IN) {
882 ed->bEndpointAddress =
883 ep_curr->hw_endpoint_in;
885 ed->bEndpointAddress =
886 ep_curr->hw_endpoint_out;
891 /* compute the maximum frame size */
892 if (ep_curr->max_frame_size < wMaxPacketSize) {
893 ep_curr->max_frame_size = wMaxPacketSize;
895 if (temp == UE_CONTROL) {
896 ep_curr->needs_in = 1;
897 ep_curr->needs_out = 1;
899 if (ed->bEndpointAddress & UE_DIR_IN) {
900 ep_curr->needs_in = 1;
902 ep_curr->needs_out = 1;
905 ep_curr->needs_ep_type = ep_type;
909 if (ep_max < ep_curr) {
916 /*------------------------------------------------------------------------*
919 * This function will try to resolve endpoint requirements by the
920 * given endpoint profiles that the USB hardware reports.
925 *------------------------------------------------------------------------*/
927 usb_hw_ep_resolve(struct usb_device *udev,
928 struct usb_descriptor *desc)
930 struct usb_hw_ep_scratch *ues;
931 struct usb_hw_ep_scratch_sub *ep;
932 const struct usb_hw_ep_profile *pf;
933 const struct usb_bus_methods *methods;
934 struct usb_device_descriptor *dd;
938 return (USB_ERR_INVAL);
940 /* get bus methods */
941 methods = udev->bus->methods;
943 if (methods->get_hw_ep_profile == NULL)
944 return (USB_ERR_INVAL);
946 if (desc->bDescriptorType == UDESC_DEVICE) {
948 if (desc->bLength < sizeof(*dd))
949 return (USB_ERR_INVAL);
953 /* get HW control endpoint 0 profile */
954 (methods->get_hw_ep_profile) (udev, &pf, 0);
956 return (USB_ERR_INVAL);
958 if (!usb_hw_ep_match(pf, UE_CONTROL, 0)) {
959 DPRINTFN(0, "Endpoint 0 does not "
960 "support control\n");
961 return (USB_ERR_INVAL);
963 mps = dd->bMaxPacketSize;
965 if (udev->speed == USB_SPEED_FULL) {
967 * We can optionally choose another packet size !
970 /* check if "mps" is ok */
971 if (pf->max_in_frame_size >= mps) {
974 /* reduce maximum packet size */
977 /* check if "mps" is too small */
979 return (USB_ERR_INVAL);
983 dd->bMaxPacketSize = mps;
986 /* We only have one choice */
990 /* Check if we support the specified wMaxPacketSize */
991 if (pf->max_in_frame_size < mps) {
992 return (USB_ERR_INVAL);
995 return (0); /* success */
997 if (desc->bDescriptorType != UDESC_CONFIG)
998 return (USB_ERR_INVAL);
999 if (desc->bLength < sizeof(*(ues->cd)))
1000 return (USB_ERR_INVAL);
1002 ues = udev->scratch.hw_ep_scratch;
1004 memset(ues, 0, sizeof(*ues));
1006 ues->ep_max = ues->ep;
1007 ues->cd = (void *)desc;
1008 ues->methods = methods;
1011 /* Get all the endpoints we need */
1013 if (usb_hw_ep_get_needs(ues, UE_ISOCHRONOUS, 0) ||
1014 usb_hw_ep_get_needs(ues, UE_INTERRUPT, 0) ||
1015 usb_hw_ep_get_needs(ues, UE_CONTROL, 0) ||
1016 usb_hw_ep_get_needs(ues, UE_BULK, 0)) {
1017 DPRINTFN(0, "Could not get needs\n");
1018 return (USB_ERR_INVAL);
1020 for (ep = ues->ep; ep != ues->ep_max; ep++) {
1022 while (ep->needs_in || ep->needs_out) {
1025 * First try to use a simplex endpoint.
1026 * Then try to use a duplex endpoint.
1028 if (usb_hw_ep_find_match(ues, ep, 1) &&
1029 usb_hw_ep_find_match(ues, ep, 0)) {
1030 DPRINTFN(0, "Could not find match\n");
1031 return (USB_ERR_INVAL);
1036 ues->ep_max = ues->ep;
1038 /* Update all endpoint addresses */
1040 if (usb_hw_ep_get_needs(ues, UE_ISOCHRONOUS, 1) ||
1041 usb_hw_ep_get_needs(ues, UE_INTERRUPT, 1) ||
1042 usb_hw_ep_get_needs(ues, UE_CONTROL, 1) ||
1043 usb_hw_ep_get_needs(ues, UE_BULK, 1)) {
1044 DPRINTFN(0, "Could not update endpoint address\n");
1045 return (USB_ERR_INVAL);
1047 return (0); /* success */
1050 /*------------------------------------------------------------------------*
1054 * NULL: No USB template device descriptor found.
1055 * Else: Pointer to the USB template device descriptor.
1056 *------------------------------------------------------------------------*/
1057 static const struct usb_temp_device_desc *
1058 usb_temp_get_tdd(struct usb_device *udev)
1060 if (udev->usb_template_ptr == NULL) {
1063 return (udev->usb_template_ptr->tdd);
1066 /*------------------------------------------------------------------------*
1067 * usb_temp_get_device_desc
1070 * NULL: No USB device descriptor found.
1071 * Else: Pointer to USB device descriptor.
1072 *------------------------------------------------------------------------*/
1074 usb_temp_get_device_desc(struct usb_device *udev)
1076 struct usb_device_descriptor *dd;
1078 if (udev->usb_template_ptr == NULL) {
1081 dd = &udev->usb_template_ptr->udd;
1082 if (dd->bDescriptorType != UDESC_DEVICE) {
1083 /* sanity check failed */
1089 /*------------------------------------------------------------------------*
1090 * usb_temp_get_qualifier_desc
1093 * NULL: No USB device_qualifier descriptor found.
1094 * Else: Pointer to USB device_qualifier descriptor.
1095 *------------------------------------------------------------------------*/
1097 usb_temp_get_qualifier_desc(struct usb_device *udev)
1099 struct usb_device_qualifier *dq;
1101 if (udev->usb_template_ptr == NULL) {
1104 dq = &udev->usb_template_ptr->udq;
1105 if (dq->bDescriptorType != UDESC_DEVICE_QUALIFIER) {
1106 /* sanity check failed */
1112 /*------------------------------------------------------------------------*
1113 * usb_temp_get_config_desc
1116 * NULL: No USB config descriptor found.
1117 * Else: Pointer to USB config descriptor having index "index".
1118 *------------------------------------------------------------------------*/
1120 usb_temp_get_config_desc(struct usb_device *udev,
1121 uint16_t *pLength, uint8_t index)
1123 struct usb_device_descriptor *dd;
1124 struct usb_config_descriptor *cd;
1127 if (udev->usb_template_ptr == NULL) {
1130 dd = &udev->usb_template_ptr->udd;
1131 cd = (void *)(udev->usb_template_ptr + 1);
1133 if (index >= dd->bNumConfigurations) {
1138 if (cd->bDescriptorType != UDESC_CONFIG) {
1139 /* sanity check failed */
1142 temp = UGETW(cd->wTotalLength);
1143 cd = USB_ADD_BYTES(cd, temp);
1147 *pLength = UGETW(cd->wTotalLength);
1152 /*------------------------------------------------------------------------*
1153 * usb_temp_get_vendor_desc
1156 * NULL: No vendor descriptor found.
1157 * Else: Pointer to a vendor descriptor.
1158 *------------------------------------------------------------------------*/
1160 usb_temp_get_vendor_desc(struct usb_device *udev,
1161 const struct usb_device_request *req, uint16_t *plen)
1163 const struct usb_temp_device_desc *tdd;
1165 tdd = usb_temp_get_tdd(udev);
1169 if (tdd->getVendorDesc == NULL) {
1172 return ((tdd->getVendorDesc) (req, plen));
1175 /*------------------------------------------------------------------------*
1176 * usb_temp_get_string_desc
1179 * NULL: No string descriptor found.
1180 * Else: Pointer to a string descriptor.
1181 *------------------------------------------------------------------------*/
1183 usb_temp_get_string_desc(struct usb_device *udev,
1184 uint16_t lang_id, uint8_t string_index)
1186 const struct usb_temp_device_desc *tdd;
1188 tdd = usb_temp_get_tdd(udev);
1192 if (tdd->getStringDesc == NULL) {
1195 return ((tdd->getStringDesc) (lang_id, string_index));
1198 /*------------------------------------------------------------------------*
1199 * usb_temp_get_hub_desc
1202 * NULL: No USB HUB descriptor found.
1203 * Else: Pointer to a USB HUB descriptor.
1204 *------------------------------------------------------------------------*/
1206 usb_temp_get_hub_desc(struct usb_device *udev)
1208 return (NULL); /* needs to be implemented */
1211 /*------------------------------------------------------------------------*
1214 * This function is a demultiplexer for local USB device side control
1215 * endpoint requests.
1216 *------------------------------------------------------------------------*/
1218 usb_temp_get_desc(struct usb_device *udev, struct usb_device_request *req,
1219 const void **pPtr, uint16_t *pLength)
1227 switch (req->bmRequestType) {
1228 case UT_READ_DEVICE:
1229 switch (req->bRequest) {
1230 case UR_GET_DESCRIPTOR:
1231 goto tr_handle_get_descriptor;
1235 case UT_READ_CLASS_DEVICE:
1236 switch (req->bRequest) {
1237 case UR_GET_DESCRIPTOR:
1238 goto tr_handle_get_class_descriptor;
1246 tr_handle_get_descriptor:
1247 switch (req->wValue[1]) {
1249 if (req->wValue[0]) {
1252 buf = usb_temp_get_device_desc(udev);
1254 case UDESC_DEVICE_QUALIFIER:
1255 if (udev->speed != USB_SPEED_HIGH) {
1258 if (req->wValue[0]) {
1261 buf = usb_temp_get_qualifier_desc(udev);
1263 case UDESC_OTHER_SPEED_CONFIGURATION:
1264 if (udev->speed != USB_SPEED_HIGH) {
1268 buf = usb_temp_get_config_desc(udev,
1269 &len, req->wValue[0]);
1272 buf = usb_temp_get_string_desc(udev,
1273 UGETW(req->wIndex), req->wValue[0]);
1279 tr_handle_get_class_descriptor:
1280 if (req->wValue[0]) {
1283 buf = usb_temp_get_hub_desc(udev);
1293 return (0); /* success */
1296 /* try to get a vendor specific descriptor */
1298 buf = usb_temp_get_vendor_desc(udev, req, &len);
1303 return (0); /* we ignore failures */
1306 /*------------------------------------------------------------------------*
1309 * This function generates USB descriptors according to the given USB
1310 * template device descriptor. It will also try to figure out the best
1311 * matching endpoint addresses using the hardware endpoint profiles.
1316 *------------------------------------------------------------------------*/
1318 usb_temp_setup(struct usb_device *udev,
1319 const struct usb_temp_device_desc *tdd)
1321 struct usb_temp_setup *uts;
1331 /* Protect scratch area */
1332 do_unlock = usbd_ctrl_lock(udev);
1334 uts = udev->scratch.temp_setup;
1336 memset(uts, 0, sizeof(*uts));
1338 uts->usb_speed = udev->speed;
1339 uts->self_powered = udev->flags.self_powered;
1343 usb_make_device_desc(uts, tdd);
1346 /* some error happened */
1350 if (uts->size == 0) {
1351 uts->err = USB_ERR_INVAL;
1354 /* allocate zeroed memory */
1355 uts->buf = usbd_alloc_config_desc(udev, uts->size);
1357 * Allow malloc() to return NULL regardless of M_WAITOK flag.
1358 * This helps when porting the software to non-FreeBSD
1361 if (uts->buf == NULL) {
1362 /* could not allocate memory */
1363 uts->err = USB_ERR_NOMEM;
1370 usb_make_device_desc(uts, tdd);
1373 * Store a pointer to our descriptors:
1375 udev->usb_template_ptr = uts->buf;
1378 /* some error happened during second pass */
1382 * Resolve all endpoint addresses !
1384 buf = usb_temp_get_device_desc(udev);
1385 uts->err = usb_hw_ep_resolve(udev, buf);
1387 DPRINTFN(0, "Could not resolve endpoints for "
1388 "Device Descriptor, error = %s\n",
1389 usbd_errstr(uts->err));
1394 buf = usb_temp_get_config_desc(udev, NULL, n);
1398 uts->err = usb_hw_ep_resolve(udev, buf);
1400 DPRINTFN(0, "Could not resolve endpoints for "
1401 "Config Descriptor %u, error = %s\n", n,
1402 usbd_errstr(uts->err));
1409 usb_temp_unsetup(udev);
1411 usbd_ctrl_unlock(udev);
1415 /*------------------------------------------------------------------------*
1418 * This function frees any memory associated with the currently
1419 * setup template, if any.
1420 *------------------------------------------------------------------------*/
1422 usb_temp_unsetup(struct usb_device *udev)
1424 usbd_free_config_desc(udev, udev->usb_template_ptr);
1425 udev->usb_template_ptr = NULL;
1429 usb_temp_setup_by_index(struct usb_device *udev, uint16_t index)
1435 err = usb_temp_setup(udev, &usb_template_msc);
1438 err = usb_temp_setup(udev, &usb_template_cdce);
1441 err = usb_temp_setup(udev, &usb_template_mtp);
1443 case USB_TEMP_MODEM:
1444 err = usb_temp_setup(udev, &usb_template_modem);
1446 case USB_TEMP_AUDIO:
1447 err = usb_temp_setup(udev, &usb_template_audio);
1450 err = usb_temp_setup(udev, &usb_template_kbd);
1452 case USB_TEMP_MOUSE:
1453 err = usb_temp_setup(udev, &usb_template_mouse);
1455 case USB_TEMP_PHONE:
1456 err = usb_temp_setup(udev, &usb_template_phone);
1458 case USB_TEMP_SERIALNET:
1459 err = usb_temp_setup(udev, &usb_template_serialnet);
1462 err = usb_temp_setup(udev, &usb_template_midi);
1464 case USB_TEMP_MULTI:
1465 err = usb_temp_setup(udev, &usb_template_multi);
1467 case USB_TEMP_CDCEEM:
1468 err = usb_temp_setup(udev, &usb_template_cdceem);
1471 return (USB_ERR_INVAL);
1478 usb_temp_init(void *arg)
1480 /* register our functions */
1481 usb_temp_get_desc_p = &usb_temp_get_desc;
1482 usb_temp_setup_by_index_p = &usb_temp_setup_by_index;
1483 usb_temp_unsetup_p = &usb_temp_unsetup;
1486 SYSINIT(usb_temp_init, SI_SUB_LOCK, SI_ORDER_FIRST, usb_temp_init, NULL);
1487 SYSUNINIT(usb_temp_unload, SI_SUB_LOCK, SI_ORDER_ANY, usb_temp_unload, NULL);