102ac6454SAndrew Thompson /* $FreeBSD$ */ 202ac6454SAndrew Thompson /*- 3718cf2ccSPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 4718cf2ccSPedro F. Giffuni * 502ac6454SAndrew Thompson * Copyright (c) 2008 Hans Petter Selasky. All rights reserved. 602ac6454SAndrew Thompson * 702ac6454SAndrew Thompson * Redistribution and use in source and binary forms, with or without 802ac6454SAndrew Thompson * modification, are permitted provided that the following conditions 902ac6454SAndrew Thompson * are met: 1002ac6454SAndrew Thompson * 1. Redistributions of source code must retain the above copyright 1102ac6454SAndrew Thompson * notice, this list of conditions and the following disclaimer. 1202ac6454SAndrew Thompson * 2. Redistributions in binary form must reproduce the above copyright 1302ac6454SAndrew Thompson * notice, this list of conditions and the following disclaimer in the 1402ac6454SAndrew Thompson * documentation and/or other materials provided with the distribution. 1502ac6454SAndrew Thompson * 1602ac6454SAndrew Thompson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1702ac6454SAndrew Thompson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1802ac6454SAndrew Thompson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1902ac6454SAndrew Thompson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2002ac6454SAndrew Thompson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2102ac6454SAndrew Thompson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2202ac6454SAndrew Thompson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2302ac6454SAndrew Thompson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2402ac6454SAndrew Thompson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2502ac6454SAndrew Thompson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2602ac6454SAndrew Thompson * SUCH DAMAGE. 2702ac6454SAndrew Thompson */ 2802ac6454SAndrew Thompson 29d2b99310SHans Petter Selasky #ifdef USB_GLOBAL_INCLUDE_FILE 30d2b99310SHans Petter Selasky #include USB_GLOBAL_INCLUDE_FILE 31d2b99310SHans Petter Selasky #else 32ed6d949aSAndrew Thompson #include <sys/stdint.h> 33ed6d949aSAndrew Thompson #include <sys/stddef.h> 34ed6d949aSAndrew Thompson #include <sys/param.h> 35ed6d949aSAndrew Thompson #include <sys/queue.h> 36ed6d949aSAndrew Thompson #include <sys/types.h> 37ed6d949aSAndrew Thompson #include <sys/systm.h> 38ed6d949aSAndrew Thompson #include <sys/kernel.h> 39ed6d949aSAndrew Thompson #include <sys/bus.h> 40ed6d949aSAndrew Thompson #include <sys/module.h> 41ed6d949aSAndrew Thompson #include <sys/lock.h> 42ed6d949aSAndrew Thompson #include <sys/mutex.h> 43ed6d949aSAndrew Thompson #include <sys/condvar.h> 44ed6d949aSAndrew Thompson #include <sys/sysctl.h> 45ed6d949aSAndrew Thompson #include <sys/sx.h> 46ed6d949aSAndrew Thompson #include <sys/unistd.h> 47ed6d949aSAndrew Thompson #include <sys/callout.h> 48ed6d949aSAndrew Thompson #include <sys/malloc.h> 49ed6d949aSAndrew Thompson #include <sys/priv.h> 50ed6d949aSAndrew Thompson #include <sys/conf.h> 51ed6d949aSAndrew Thompson #include <sys/fcntl.h> 52ed6d949aSAndrew Thompson 5302ac6454SAndrew Thompson #include <dev/usb/usb.h> 54ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h> 55ed6d949aSAndrew Thompson #include <dev/usb/usbdi_util.h> 5602ac6454SAndrew Thompson #include <dev/usb/usb_ioctl.h> 57963169b4SHans Petter Selasky 58963169b4SHans Petter Selasky #if USB_HAVE_UGEN 59963169b4SHans Petter Selasky #include <sys/sbuf.h> 60963169b4SHans Petter Selasky #endif 61963169b4SHans Petter Selasky 6202ac6454SAndrew Thompson #include "usbdevs.h" 6302ac6454SAndrew Thompson 64a593f6b8SAndrew Thompson #define USB_DEBUG_VAR usb_debug 6502ac6454SAndrew Thompson 6602ac6454SAndrew Thompson #include <dev/usb/usb_core.h> 6702ac6454SAndrew Thompson #include <dev/usb/usb_debug.h> 6802ac6454SAndrew Thompson #include <dev/usb/usb_process.h> 6902ac6454SAndrew Thompson #include <dev/usb/usb_device.h> 7002ac6454SAndrew Thompson #include <dev/usb/usb_busdma.h> 7102ac6454SAndrew Thompson #include <dev/usb/usb_transfer.h> 7202ac6454SAndrew Thompson #include <dev/usb/usb_request.h> 7302ac6454SAndrew Thompson #include <dev/usb/usb_dynamic.h> 7402ac6454SAndrew Thompson #include <dev/usb/usb_hub.h> 7502ac6454SAndrew Thompson #include <dev/usb/usb_util.h> 7602ac6454SAndrew Thompson #include <dev/usb/usb_msctest.h> 77bdc081c6SAndrew Thompson #if USB_HAVE_UGEN 78bdc081c6SAndrew Thompson #include <dev/usb/usb_dev.h> 7902ac6454SAndrew Thompson #include <dev/usb/usb_generic.h> 80bdc081c6SAndrew Thompson #endif 8102ac6454SAndrew Thompson 8202ac6454SAndrew Thompson #include <dev/usb/quirk/usb_quirk.h> 8302ac6454SAndrew Thompson 8402ac6454SAndrew Thompson #include <dev/usb/usb_controller.h> 8502ac6454SAndrew Thompson #include <dev/usb/usb_bus.h> 86d2b99310SHans Petter Selasky #endif /* USB_GLOBAL_INCLUDE_FILE */ 8702ac6454SAndrew Thompson 8802ac6454SAndrew Thompson /* function prototypes */ 8902ac6454SAndrew Thompson 90a593f6b8SAndrew Thompson static void usb_init_endpoint(struct usb_device *, uint8_t, 91963169b4SHans Petter Selasky struct usb_endpoint_descriptor *, 92963169b4SHans Petter Selasky struct usb_endpoint_ss_comp_descriptor *, 93963169b4SHans Petter Selasky struct usb_endpoint *); 94a593f6b8SAndrew Thompson static void usb_unconfigure(struct usb_device *, uint8_t); 95a593f6b8SAndrew Thompson static void usb_detach_device_sub(struct usb_device *, device_t *, 968427ed84SHans Petter Selasky char **, uint8_t); 97a593f6b8SAndrew Thompson static uint8_t usb_probe_and_attach_sub(struct usb_device *, 98760bc48eSAndrew Thompson struct usb_attach_arg *); 99a593f6b8SAndrew Thompson static void usb_init_attach_arg(struct usb_device *, 100760bc48eSAndrew Thompson struct usb_attach_arg *); 101a593f6b8SAndrew Thompson static void usb_suspend_resume_sub(struct usb_device *, device_t, 10202ac6454SAndrew Thompson uint8_t); 103a0d53e0bSHans Petter Selasky static usb_proc_callback_t usbd_clear_stall_proc; 104963169b4SHans Petter Selasky static usb_error_t usb_config_parse(struct usb_device *, uint8_t, uint8_t); 105a593f6b8SAndrew Thompson static void usbd_set_device_strings(struct usb_device *); 1068bb77249SHans Petter Selasky #if USB_HAVE_DEVCTL 107a593f6b8SAndrew Thompson static void usb_notify_addq(const char *type, struct usb_device *); 1088bb77249SHans Petter Selasky #endif 1098bb77249SHans Petter Selasky #if USB_HAVE_UGEN 110a593f6b8SAndrew Thompson static void usb_fifo_free_wrap(struct usb_device *, uint8_t, uint8_t); 111a593f6b8SAndrew Thompson static void usb_cdev_create(struct usb_device *); 112a593f6b8SAndrew Thompson static void usb_cdev_free(struct usb_device *); 1130f6e8f93SAndrew Thompson #endif 11402ac6454SAndrew Thompson 11502ac6454SAndrew Thompson /* This variable is global to allow easy access to it: */ 11602ac6454SAndrew Thompson 117fa49bce5SHans Petter Selasky #ifdef USB_TEMPLATE 118fa49bce5SHans Petter Selasky int usb_template = USB_TEMPLATE; 119fa49bce5SHans Petter Selasky #else 120fa49bce5SHans Petter Selasky int usb_template; 121fa49bce5SHans Petter Selasky #endif 12202ac6454SAndrew Thompson 123af3b2549SHans Petter Selasky SYSCTL_INT(_hw_usb, OID_AUTO, template, CTLFLAG_RWTUN, 124a593f6b8SAndrew Thompson &usb_template, 0, "Selected USB device side template"); 12502ac6454SAndrew Thompson 1267a918edfSAndrew Thompson /* English is default language */ 1277a918edfSAndrew Thompson 1287a918edfSAndrew Thompson static int usb_lang_id = 0x0009; 1297a918edfSAndrew Thompson static int usb_lang_mask = 0x00FF; 1307a918edfSAndrew Thompson 131af3b2549SHans Petter Selasky SYSCTL_INT(_hw_usb, OID_AUTO, usb_lang_id, CTLFLAG_RWTUN, 1327a918edfSAndrew Thompson &usb_lang_id, 0, "Preferred USB language ID"); 1337a918edfSAndrew Thompson 134af3b2549SHans Petter Selasky SYSCTL_INT(_hw_usb, OID_AUTO, usb_lang_mask, CTLFLAG_RWTUN, 1357a918edfSAndrew Thompson &usb_lang_mask, 0, "Preferred USB language mask"); 1367a918edfSAndrew Thompson 1374d4fa6edSAndrew Thompson static const char* statestr[USB_STATE_MAX] = { 1384d4fa6edSAndrew Thompson [USB_STATE_DETACHED] = "DETACHED", 1394d4fa6edSAndrew Thompson [USB_STATE_ATTACHED] = "ATTACHED", 1404d4fa6edSAndrew Thompson [USB_STATE_POWERED] = "POWERED", 1414d4fa6edSAndrew Thompson [USB_STATE_ADDRESSED] = "ADDRESSED", 1424d4fa6edSAndrew Thompson [USB_STATE_CONFIGURED] = "CONFIGURED", 1434d4fa6edSAndrew Thompson }; 1444d4fa6edSAndrew Thompson 1454d4fa6edSAndrew Thompson const char * 146a593f6b8SAndrew Thompson usb_statestr(enum usb_dev_state state) 1474d4fa6edSAndrew Thompson { 148ec8f3127SAndrew Thompson return ((state < USB_STATE_MAX) ? statestr[state] : "UNKNOWN"); 1494d4fa6edSAndrew Thompson } 15002ac6454SAndrew Thompson 151ae538d85SAndrew Thompson const char * 152ae538d85SAndrew Thompson usb_get_manufacturer(struct usb_device *udev) 153ae538d85SAndrew Thompson { 154ae538d85SAndrew Thompson return (udev->manufacturer ? udev->manufacturer : "Unknown"); 155ae538d85SAndrew Thompson } 156ae538d85SAndrew Thompson 157ae538d85SAndrew Thompson const char * 158ae538d85SAndrew Thompson usb_get_product(struct usb_device *udev) 159ae538d85SAndrew Thompson { 160ae538d85SAndrew Thompson return (udev->product ? udev->product : ""); 161ae538d85SAndrew Thompson } 162ae538d85SAndrew Thompson 163ae538d85SAndrew Thompson const char * 164ae538d85SAndrew Thompson usb_get_serial(struct usb_device *udev) 165ae538d85SAndrew Thompson { 166ae538d85SAndrew Thompson return (udev->serial ? udev->serial : ""); 167ae538d85SAndrew Thompson } 168ae538d85SAndrew Thompson 16902ac6454SAndrew Thompson /*------------------------------------------------------------------------* 170a593f6b8SAndrew Thompson * usbd_get_ep_by_addr 17102ac6454SAndrew Thompson * 172ae60fdfbSAndrew Thompson * This function searches for an USB ep by endpoint address and 17302ac6454SAndrew Thompson * direction. 17402ac6454SAndrew Thompson * 17502ac6454SAndrew Thompson * Returns: 17602ac6454SAndrew Thompson * NULL: Failure 17702ac6454SAndrew Thompson * Else: Success 17802ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 179ae60fdfbSAndrew Thompson struct usb_endpoint * 180a593f6b8SAndrew Thompson usbd_get_ep_by_addr(struct usb_device *udev, uint8_t ea_val) 18102ac6454SAndrew Thompson { 182ae60fdfbSAndrew Thompson struct usb_endpoint *ep = udev->endpoints; 183ae60fdfbSAndrew Thompson struct usb_endpoint *ep_end = udev->endpoints + udev->endpoints_max; 18402ac6454SAndrew Thompson enum { 18502ac6454SAndrew Thompson EA_MASK = (UE_DIR_IN | UE_DIR_OUT | UE_ADDR), 18602ac6454SAndrew Thompson }; 18702ac6454SAndrew Thompson 18802ac6454SAndrew Thompson /* 18902ac6454SAndrew Thompson * According to the USB specification not all bits are used 19002ac6454SAndrew Thompson * for the endpoint address. Keep defined bits only: 19102ac6454SAndrew Thompson */ 19202ac6454SAndrew Thompson ea_val &= EA_MASK; 19302ac6454SAndrew Thompson 19402ac6454SAndrew Thompson /* 19520733245SPedro F. Giffuni * Iterate across all the USB endpoints searching for a match 19602ac6454SAndrew Thompson * based on the endpoint address: 19702ac6454SAndrew Thompson */ 198ae60fdfbSAndrew Thompson for (; ep != ep_end; ep++) { 19902ac6454SAndrew Thompson 200ae60fdfbSAndrew Thompson if (ep->edesc == NULL) { 20102ac6454SAndrew Thompson continue; 20202ac6454SAndrew Thompson } 20302ac6454SAndrew Thompson /* do the mask and check the value */ 204ae60fdfbSAndrew Thompson if ((ep->edesc->bEndpointAddress & EA_MASK) == ea_val) { 20502ac6454SAndrew Thompson goto found; 20602ac6454SAndrew Thompson } 20702ac6454SAndrew Thompson } 20802ac6454SAndrew Thompson 20902ac6454SAndrew Thompson /* 210ae60fdfbSAndrew Thompson * The default endpoint is always present and is checked separately: 21102ac6454SAndrew Thompson */ 21222039494SHans Petter Selasky if ((udev->ctrl_ep.edesc != NULL) && 2135b3bb704SAndrew Thompson ((udev->ctrl_ep.edesc->bEndpointAddress & EA_MASK) == ea_val)) { 2145b3bb704SAndrew Thompson ep = &udev->ctrl_ep; 21502ac6454SAndrew Thompson goto found; 21602ac6454SAndrew Thompson } 21702ac6454SAndrew Thompson return (NULL); 21802ac6454SAndrew Thompson 21902ac6454SAndrew Thompson found: 220ae60fdfbSAndrew Thompson return (ep); 22102ac6454SAndrew Thompson } 22202ac6454SAndrew Thompson 22302ac6454SAndrew Thompson /*------------------------------------------------------------------------* 224a593f6b8SAndrew Thompson * usbd_get_endpoint 22502ac6454SAndrew Thompson * 226ae60fdfbSAndrew Thompson * This function searches for an USB endpoint based on the information 227760bc48eSAndrew Thompson * given by the passed "struct usb_config" pointer. 22802ac6454SAndrew Thompson * 22902ac6454SAndrew Thompson * Return values: 23002ac6454SAndrew Thompson * NULL: No match. 231ae60fdfbSAndrew Thompson * Else: Pointer to "struct usb_endpoint". 23202ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 233ae60fdfbSAndrew Thompson struct usb_endpoint * 234a593f6b8SAndrew Thompson usbd_get_endpoint(struct usb_device *udev, uint8_t iface_index, 235760bc48eSAndrew Thompson const struct usb_config *setup) 23602ac6454SAndrew Thompson { 237ae60fdfbSAndrew Thompson struct usb_endpoint *ep = udev->endpoints; 238ae60fdfbSAndrew Thompson struct usb_endpoint *ep_end = udev->endpoints + udev->endpoints_max; 23902ac6454SAndrew Thompson uint8_t index = setup->ep_index; 24002ac6454SAndrew Thompson uint8_t ea_mask; 24102ac6454SAndrew Thompson uint8_t ea_val; 24202ac6454SAndrew Thompson uint8_t type_mask; 24302ac6454SAndrew Thompson uint8_t type_val; 24402ac6454SAndrew Thompson 24502ac6454SAndrew Thompson DPRINTFN(10, "udev=%p iface_index=%d address=0x%x " 24602ac6454SAndrew Thompson "type=0x%x dir=0x%x index=%d\n", 24702ac6454SAndrew Thompson udev, iface_index, setup->endpoint, 24802ac6454SAndrew Thompson setup->type, setup->direction, setup->ep_index); 24902ac6454SAndrew Thompson 2504eae601eSAndrew Thompson /* check USB mode */ 2514eae601eSAndrew Thompson 252f29a0724SAndrew Thompson if (setup->usb_mode != USB_MODE_DUAL && 253f29a0724SAndrew Thompson udev->flags.usb_mode != setup->usb_mode) { 254ae60fdfbSAndrew Thompson /* wrong mode - no endpoint */ 2554eae601eSAndrew Thompson return (NULL); 2564eae601eSAndrew Thompson } 2574eae601eSAndrew Thompson 25802ac6454SAndrew Thompson /* setup expected endpoint direction mask and value */ 25902ac6454SAndrew Thompson 26051ec1603SAndrew Thompson if (setup->direction == UE_DIR_RX) { 26151ec1603SAndrew Thompson ea_mask = (UE_DIR_IN | UE_DIR_OUT); 262f29a0724SAndrew Thompson ea_val = (udev->flags.usb_mode == USB_MODE_DEVICE) ? 26351ec1603SAndrew Thompson UE_DIR_OUT : UE_DIR_IN; 26451ec1603SAndrew Thompson } else if (setup->direction == UE_DIR_TX) { 26551ec1603SAndrew Thompson ea_mask = (UE_DIR_IN | UE_DIR_OUT); 266f29a0724SAndrew Thompson ea_val = (udev->flags.usb_mode == USB_MODE_DEVICE) ? 26751ec1603SAndrew Thompson UE_DIR_IN : UE_DIR_OUT; 26851ec1603SAndrew Thompson } else if (setup->direction == UE_DIR_ANY) { 26902ac6454SAndrew Thompson /* match any endpoint direction */ 27002ac6454SAndrew Thompson ea_mask = 0; 27102ac6454SAndrew Thompson ea_val = 0; 27202ac6454SAndrew Thompson } else { 27302ac6454SAndrew Thompson /* match the given endpoint direction */ 27402ac6454SAndrew Thompson ea_mask = (UE_DIR_IN | UE_DIR_OUT); 27502ac6454SAndrew Thompson ea_val = (setup->direction & (UE_DIR_IN | UE_DIR_OUT)); 27602ac6454SAndrew Thompson } 27702ac6454SAndrew Thompson 27802ac6454SAndrew Thompson /* setup expected endpoint address */ 27902ac6454SAndrew Thompson 28002ac6454SAndrew Thompson if (setup->endpoint == UE_ADDR_ANY) { 28102ac6454SAndrew Thompson /* match any endpoint address */ 28202ac6454SAndrew Thompson } else { 28302ac6454SAndrew Thompson /* match the given endpoint address */ 28402ac6454SAndrew Thompson ea_mask |= UE_ADDR; 28502ac6454SAndrew Thompson ea_val |= (setup->endpoint & UE_ADDR); 28602ac6454SAndrew Thompson } 28702ac6454SAndrew Thompson 28802ac6454SAndrew Thompson /* setup expected endpoint type */ 28902ac6454SAndrew Thompson 29002ac6454SAndrew Thompson if (setup->type == UE_BULK_INTR) { 29102ac6454SAndrew Thompson /* this will match BULK and INTERRUPT endpoints */ 29202ac6454SAndrew Thompson type_mask = 2; 29302ac6454SAndrew Thompson type_val = 2; 29402ac6454SAndrew Thompson } else if (setup->type == UE_TYPE_ANY) { 29502ac6454SAndrew Thompson /* match any endpoint type */ 29602ac6454SAndrew Thompson type_mask = 0; 29702ac6454SAndrew Thompson type_val = 0; 29802ac6454SAndrew Thompson } else { 29902ac6454SAndrew Thompson /* match the given endpoint type */ 30002ac6454SAndrew Thompson type_mask = UE_XFERTYPE; 30102ac6454SAndrew Thompson type_val = (setup->type & UE_XFERTYPE); 30202ac6454SAndrew Thompson } 30302ac6454SAndrew Thompson 30402ac6454SAndrew Thompson /* 30520733245SPedro F. Giffuni * Iterate across all the USB endpoints searching for a match 30602ac6454SAndrew Thompson * based on the endpoint address. Note that we are searching 307ae60fdfbSAndrew Thompson * the endpoints from the beginning of the "udev->endpoints" array. 30802ac6454SAndrew Thompson */ 309ae60fdfbSAndrew Thompson for (; ep != ep_end; ep++) { 31002ac6454SAndrew Thompson 311ae60fdfbSAndrew Thompson if ((ep->edesc == NULL) || 312ae60fdfbSAndrew Thompson (ep->iface_index != iface_index)) { 31302ac6454SAndrew Thompson continue; 31402ac6454SAndrew Thompson } 31502ac6454SAndrew Thompson /* do the masks and check the values */ 31602ac6454SAndrew Thompson 317ae60fdfbSAndrew Thompson if (((ep->edesc->bEndpointAddress & ea_mask) == ea_val) && 318ae60fdfbSAndrew Thompson ((ep->edesc->bmAttributes & type_mask) == type_val)) { 31902ac6454SAndrew Thompson if (!index--) { 32002ac6454SAndrew Thompson goto found; 32102ac6454SAndrew Thompson } 32202ac6454SAndrew Thompson } 32302ac6454SAndrew Thompson } 32402ac6454SAndrew Thompson 32502ac6454SAndrew Thompson /* 326ae60fdfbSAndrew Thompson * Match against default endpoint last, so that "any endpoint", "any 327ae60fdfbSAndrew Thompson * address" and "any direction" returns the first endpoint of the 32802ac6454SAndrew Thompson * interface. "iface_index" and "direction" is ignored: 32902ac6454SAndrew Thompson */ 33022039494SHans Petter Selasky if ((udev->ctrl_ep.edesc != NULL) && 3315b3bb704SAndrew Thompson ((udev->ctrl_ep.edesc->bEndpointAddress & ea_mask) == ea_val) && 3325b3bb704SAndrew Thompson ((udev->ctrl_ep.edesc->bmAttributes & type_mask) == type_val) && 33302ac6454SAndrew Thompson (!index)) { 3345b3bb704SAndrew Thompson ep = &udev->ctrl_ep; 33502ac6454SAndrew Thompson goto found; 33602ac6454SAndrew Thompson } 33702ac6454SAndrew Thompson return (NULL); 33802ac6454SAndrew Thompson 33902ac6454SAndrew Thompson found: 340ae60fdfbSAndrew Thompson return (ep); 34102ac6454SAndrew Thompson } 34202ac6454SAndrew Thompson 34302ac6454SAndrew Thompson /*------------------------------------------------------------------------* 344ed6d949aSAndrew Thompson * usbd_interface_count 34502ac6454SAndrew Thompson * 34602ac6454SAndrew Thompson * This function stores the number of USB interfaces excluding 34702ac6454SAndrew Thompson * alternate settings, which the USB config descriptor reports into 34802ac6454SAndrew Thompson * the unsigned 8-bit integer pointed to by "count". 34902ac6454SAndrew Thompson * 35002ac6454SAndrew Thompson * Returns: 35102ac6454SAndrew Thompson * 0: Success 35202ac6454SAndrew Thompson * Else: Failure 35302ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 354e0a69b51SAndrew Thompson usb_error_t 355ed6d949aSAndrew Thompson usbd_interface_count(struct usb_device *udev, uint8_t *count) 35602ac6454SAndrew Thompson { 35702ac6454SAndrew Thompson if (udev->cdesc == NULL) { 35802ac6454SAndrew Thompson *count = 0; 35902ac6454SAndrew Thompson return (USB_ERR_NOT_CONFIGURED); 36002ac6454SAndrew Thompson } 361bdd41206SAndrew Thompson *count = udev->ifaces_max; 36202ac6454SAndrew Thompson return (USB_ERR_NORMAL_COMPLETION); 36302ac6454SAndrew Thompson } 36402ac6454SAndrew Thompson 36502ac6454SAndrew Thompson /*------------------------------------------------------------------------* 366a593f6b8SAndrew Thompson * usb_init_endpoint 36702ac6454SAndrew Thompson * 368ae60fdfbSAndrew Thompson * This function will initialise the USB endpoint structure pointed to by 369ae60fdfbSAndrew Thompson * the "endpoint" argument. The structure pointed to by "endpoint" must be 370bdd41206SAndrew Thompson * zeroed before calling this function. 37102ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 37202ac6454SAndrew Thompson static void 373a593f6b8SAndrew Thompson usb_init_endpoint(struct usb_device *udev, uint8_t iface_index, 374963169b4SHans Petter Selasky struct usb_endpoint_descriptor *edesc, 375963169b4SHans Petter Selasky struct usb_endpoint_ss_comp_descriptor *ecomp, 376963169b4SHans Petter Selasky struct usb_endpoint *ep) 37702ac6454SAndrew Thompson { 378e892b3feSHans Petter Selasky const struct usb_bus_methods *methods; 379a5cf1aaaSHans Petter Selasky usb_stream_t x; 380ee3e3ff5SAndrew Thompson 381bdd41206SAndrew Thompson methods = udev->bus->methods; 38202ac6454SAndrew Thompson 383ae60fdfbSAndrew Thompson (methods->endpoint_init) (udev, edesc, ep); 38402ac6454SAndrew Thompson 385ae60fdfbSAndrew Thompson /* initialise USB endpoint structure */ 386ae60fdfbSAndrew Thompson ep->edesc = edesc; 387963169b4SHans Petter Selasky ep->ecomp = ecomp; 388ae60fdfbSAndrew Thompson ep->iface_index = iface_index; 389a5cf1aaaSHans Petter Selasky 390a5cf1aaaSHans Petter Selasky /* setup USB stream queues */ 391a5cf1aaaSHans Petter Selasky for (x = 0; x != USB_MAX_EP_STREAMS; x++) { 392a5cf1aaaSHans Petter Selasky TAILQ_INIT(&ep->endpoint_q[x].head); 393a5cf1aaaSHans Petter Selasky ep->endpoint_q[x].command = &usbd_pipe_start; 394a5cf1aaaSHans Petter Selasky } 39502ac6454SAndrew Thompson 39639307315SAndrew Thompson /* the pipe is not supported by the hardware */ 397ae60fdfbSAndrew Thompson if (ep->methods == NULL) 39839307315SAndrew Thompson return; 39939307315SAndrew Thompson 400a5cf1aaaSHans Petter Selasky /* check for SUPER-speed streams mode endpoint */ 401a5cf1aaaSHans Petter Selasky if (udev->speed == USB_SPEED_SUPER && ecomp != NULL && 402a5cf1aaaSHans Petter Selasky (edesc->bmAttributes & UE_XFERTYPE) == UE_BULK && 403a5cf1aaaSHans Petter Selasky (UE_GET_BULK_STREAMS(ecomp->bmAttributes) != 0)) { 404a5cf1aaaSHans Petter Selasky usbd_set_endpoint_mode(udev, ep, USB_EP_MODE_STREAMS); 405a5cf1aaaSHans Petter Selasky } else { 406a5cf1aaaSHans Petter Selasky usbd_set_endpoint_mode(udev, ep, USB_EP_MODE_DEFAULT); 407a5cf1aaaSHans Petter Selasky } 408a5cf1aaaSHans Petter Selasky 40902ac6454SAndrew Thompson /* clear stall, if any */ 410bdd41206SAndrew Thompson if (methods->clear_stall != NULL) { 41102ac6454SAndrew Thompson USB_BUS_LOCK(udev->bus); 412ae60fdfbSAndrew Thompson (methods->clear_stall) (udev, ep); 41302ac6454SAndrew Thompson USB_BUS_UNLOCK(udev->bus); 41402ac6454SAndrew Thompson } 41502ac6454SAndrew Thompson } 41602ac6454SAndrew Thompson 417bdd41206SAndrew Thompson /*-----------------------------------------------------------------------* 418a593f6b8SAndrew Thompson * usb_endpoint_foreach 41916589beaSAndrew Thompson * 42016589beaSAndrew Thompson * This function will iterate all the USB endpoints except the control 42116589beaSAndrew Thompson * endpoint. This function is NULL safe. 42216589beaSAndrew Thompson * 42316589beaSAndrew Thompson * Return values: 424ae60fdfbSAndrew Thompson * NULL: End of USB endpoints 425ae60fdfbSAndrew Thompson * Else: Pointer to next USB endpoint 42616589beaSAndrew Thompson *------------------------------------------------------------------------*/ 427ae60fdfbSAndrew Thompson struct usb_endpoint * 428a593f6b8SAndrew Thompson usb_endpoint_foreach(struct usb_device *udev, struct usb_endpoint *ep) 42916589beaSAndrew Thompson { 430a9e26757SAndrew Thompson struct usb_endpoint *ep_end; 43116589beaSAndrew Thompson 43216589beaSAndrew Thompson /* be NULL safe */ 43316589beaSAndrew Thompson if (udev == NULL) 43416589beaSAndrew Thompson return (NULL); 43516589beaSAndrew Thompson 436a9e26757SAndrew Thompson ep_end = udev->endpoints + udev->endpoints_max; 437a9e26757SAndrew Thompson 438ae60fdfbSAndrew Thompson /* get next endpoint */ 439ae60fdfbSAndrew Thompson if (ep == NULL) 440ae60fdfbSAndrew Thompson ep = udev->endpoints; 44116589beaSAndrew Thompson else 442ae60fdfbSAndrew Thompson ep++; 44316589beaSAndrew Thompson 444ae60fdfbSAndrew Thompson /* find next allocated ep */ 445ae60fdfbSAndrew Thompson while (ep != ep_end) { 446ae60fdfbSAndrew Thompson if (ep->edesc != NULL) 447ae60fdfbSAndrew Thompson return (ep); 448ae60fdfbSAndrew Thompson ep++; 44916589beaSAndrew Thompson } 45016589beaSAndrew Thompson return (NULL); 45116589beaSAndrew Thompson } 45216589beaSAndrew Thompson 45316589beaSAndrew Thompson /*------------------------------------------------------------------------* 454b78e84d1SHans Petter Selasky * usb_wait_pending_refs 455d008478eSHans Petter Selasky * 456d008478eSHans Petter Selasky * This function will wait for any USB references to go away before 457b78e84d1SHans Petter Selasky * returning. This function is used before freeing a USB device. 458d008478eSHans Petter Selasky *------------------------------------------------------------------------*/ 459d008478eSHans Petter Selasky static void 460b78e84d1SHans Petter Selasky usb_wait_pending_refs(struct usb_device *udev) 461d008478eSHans Petter Selasky { 462d008478eSHans Petter Selasky #if USB_HAVE_UGEN 463b78e84d1SHans Petter Selasky DPRINTF("Refcount = %d\n", (int)udev->refcount); 464d008478eSHans Petter Selasky 465b78e84d1SHans Petter Selasky mtx_lock(&usb_ref_lock); 466b78e84d1SHans Petter Selasky udev->refcount--; 467d008478eSHans Petter Selasky while (1) { 468d008478eSHans Petter Selasky /* wait for any pending references to go away */ 469b78e84d1SHans Petter Selasky if (udev->refcount == 0) { 470b78e84d1SHans Petter Selasky /* prevent further refs being taken, if any */ 471d008478eSHans Petter Selasky udev->refcount = USB_DEV_REF_MAX; 472d008478eSHans Petter Selasky break; 473d008478eSHans Petter Selasky } 474d008478eSHans Petter Selasky cv_wait(&udev->ref_cv, &usb_ref_lock); 475d008478eSHans Petter Selasky } 476d008478eSHans Petter Selasky mtx_unlock(&usb_ref_lock); 477d008478eSHans Petter Selasky #endif 478d008478eSHans Petter Selasky } 479d008478eSHans Petter Selasky 480d008478eSHans Petter Selasky /*------------------------------------------------------------------------* 481a593f6b8SAndrew Thompson * usb_unconfigure 48202ac6454SAndrew Thompson * 483ae60fdfbSAndrew Thompson * This function will free all USB interfaces and USB endpoints belonging 48402ac6454SAndrew Thompson * to an USB device. 485bdd41206SAndrew Thompson * 486bdd41206SAndrew Thompson * Flag values, see "USB_UNCFG_FLAG_XXX". 48702ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 48802ac6454SAndrew Thompson static void 489a593f6b8SAndrew Thompson usb_unconfigure(struct usb_device *udev, uint8_t flag) 49002ac6454SAndrew Thompson { 491bdd41206SAndrew Thompson uint8_t do_unlock; 49202ac6454SAndrew Thompson 4936950c75fSHans Petter Selasky /* Prevent re-enumeration */ 4946950c75fSHans Petter Selasky do_unlock = usbd_enum_lock(udev); 495bdd41206SAndrew Thompson 496bdd41206SAndrew Thompson /* detach all interface drivers */ 497a593f6b8SAndrew Thompson usb_detach_device(udev, USB_IFACE_INDEX_ANY, flag); 498bdd41206SAndrew Thompson 499bdd41206SAndrew Thompson #if USB_HAVE_UGEN 500bdd41206SAndrew Thompson /* free all FIFOs except control endpoint FIFOs */ 501a593f6b8SAndrew Thompson usb_fifo_free_wrap(udev, USB_IFACE_INDEX_ANY, flag); 502bdd41206SAndrew Thompson 503bdd41206SAndrew Thompson /* 504bdd41206SAndrew Thompson * Free all cdev's, if any. 505bdd41206SAndrew Thompson */ 506a593f6b8SAndrew Thompson usb_cdev_free(udev); 507bdd41206SAndrew Thompson #endif 50802ac6454SAndrew Thompson 509bdc081c6SAndrew Thompson #if USB_HAVE_COMPAT_LINUX 51002ac6454SAndrew Thompson /* free Linux compat device, if any */ 5118d59ecb2SHans Petter Selasky if (udev->linux_endpoint_start != NULL) { 5128d59ecb2SHans Petter Selasky usb_linux_free_device_p(udev); 513760bc48eSAndrew Thompson udev->linux_endpoint_start = NULL; 51402ac6454SAndrew Thompson } 515bdc081c6SAndrew Thompson #endif 51602ac6454SAndrew Thompson 517a593f6b8SAndrew Thompson usb_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_FREE); 51802ac6454SAndrew Thompson 519ae60fdfbSAndrew Thompson /* free "cdesc" after "ifaces" and "endpoints", if any */ 5207efaaa9aSAndrew Thompson if (udev->cdesc != NULL) { 521f29a0724SAndrew Thompson if (udev->flags.usb_mode != USB_MODE_DEVICE) 5222c79a775SHans Petter Selasky usbd_free_config_desc(udev, udev->cdesc); 52302ac6454SAndrew Thompson udev->cdesc = NULL; 52402ac6454SAndrew Thompson } 52502ac6454SAndrew Thompson /* set unconfigured state */ 52602ac6454SAndrew Thompson udev->curr_config_no = USB_UNCONFIG_NO; 52702ac6454SAndrew Thompson udev->curr_config_index = USB_UNCONFIG_INDEX; 528bdd41206SAndrew Thompson 529cb18f7d1SAlfred Perlstein if (do_unlock) 530cb18f7d1SAlfred Perlstein usbd_enum_unlock(udev); 53102ac6454SAndrew Thompson } 53202ac6454SAndrew Thompson 53302ac6454SAndrew Thompson /*------------------------------------------------------------------------* 534a593f6b8SAndrew Thompson * usbd_set_config_index 53502ac6454SAndrew Thompson * 53602ac6454SAndrew Thompson * This function selects configuration by index, independent of the 53702ac6454SAndrew Thompson * actual configuration number. This function should not be used by 53802ac6454SAndrew Thompson * USB drivers. 53902ac6454SAndrew Thompson * 54002ac6454SAndrew Thompson * Returns: 54102ac6454SAndrew Thompson * 0: Success 54202ac6454SAndrew Thompson * Else: Failure 54302ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 544e0a69b51SAndrew Thompson usb_error_t 545a593f6b8SAndrew Thompson usbd_set_config_index(struct usb_device *udev, uint8_t index) 54602ac6454SAndrew Thompson { 547760bc48eSAndrew Thompson struct usb_status ds; 548760bc48eSAndrew Thompson struct usb_config_descriptor *cdp; 54902ac6454SAndrew Thompson uint16_t power; 55002ac6454SAndrew Thompson uint16_t max_power; 55102ac6454SAndrew Thompson uint8_t selfpowered; 55202ac6454SAndrew Thompson uint8_t do_unlock; 553e0a69b51SAndrew Thompson usb_error_t err; 55402ac6454SAndrew Thompson 55502ac6454SAndrew Thompson DPRINTFN(6, "udev=%p index=%d\n", udev, index); 55602ac6454SAndrew Thompson 5576950c75fSHans Petter Selasky /* Prevent re-enumeration */ 5586950c75fSHans Petter Selasky do_unlock = usbd_enum_lock(udev); 55902ac6454SAndrew Thompson 560d88688c7SAndrew Thompson usb_unconfigure(udev, 0); 56102ac6454SAndrew Thompson 56202ac6454SAndrew Thompson if (index == USB_UNCONFIG_INDEX) { 56302ac6454SAndrew Thompson /* 56402ac6454SAndrew Thompson * Leave unallocated when unconfiguring the 565a593f6b8SAndrew Thompson * device. "usb_unconfigure()" will also reset 56602ac6454SAndrew Thompson * the current config number and index. 56702ac6454SAndrew Thompson */ 568a593f6b8SAndrew Thompson err = usbd_req_set_config(udev, NULL, USB_UNCONFIG_NO); 569bd216778SAndrew Thompson if (udev->state == USB_STATE_CONFIGURED) 570a593f6b8SAndrew Thompson usb_set_device_state(udev, USB_STATE_ADDRESSED); 57102ac6454SAndrew Thompson goto done; 57202ac6454SAndrew Thompson } 57302ac6454SAndrew Thompson /* get the full config descriptor */ 574f29a0724SAndrew Thompson if (udev->flags.usb_mode == USB_MODE_DEVICE) { 5757efaaa9aSAndrew Thompson /* save some memory */ 576a593f6b8SAndrew Thompson err = usbd_req_get_descriptor_ptr(udev, &cdp, 577459d369eSAndrew Thompson (UDESC_CONFIG << 8) | index); 5787efaaa9aSAndrew Thompson } else { 5797efaaa9aSAndrew Thompson /* normal request */ 580a593f6b8SAndrew Thompson err = usbd_req_get_config_desc_full(udev, 5812c79a775SHans Petter Selasky NULL, &cdp, index); 5827efaaa9aSAndrew Thompson } 58302ac6454SAndrew Thompson if (err) { 58402ac6454SAndrew Thompson goto done; 58502ac6454SAndrew Thompson } 58602ac6454SAndrew Thompson /* set the new config descriptor */ 58702ac6454SAndrew Thompson 58802ac6454SAndrew Thompson udev->cdesc = cdp; 58902ac6454SAndrew Thompson 59002ac6454SAndrew Thompson /* Figure out if the device is self or bus powered. */ 59102ac6454SAndrew Thompson selfpowered = 0; 59202ac6454SAndrew Thompson if ((!udev->flags.uq_bus_powered) && 59302ac6454SAndrew Thompson (cdp->bmAttributes & UC_SELF_POWERED) && 594f29a0724SAndrew Thompson (udev->flags.usb_mode == USB_MODE_HOST)) { 59502ac6454SAndrew Thompson /* May be self powered. */ 59602ac6454SAndrew Thompson if (cdp->bmAttributes & UC_BUS_POWERED) { 59702ac6454SAndrew Thompson /* Must ask device. */ 598a593f6b8SAndrew Thompson err = usbd_req_get_device_status(udev, NULL, &ds); 59902ac6454SAndrew Thompson if (err) { 60002ac6454SAndrew Thompson DPRINTFN(0, "could not read " 60102ac6454SAndrew Thompson "device status: %s\n", 602a593f6b8SAndrew Thompson usbd_errstr(err)); 60302ac6454SAndrew Thompson } else if (UGETW(ds.wStatus) & UDS_SELF_POWERED) { 60402ac6454SAndrew Thompson selfpowered = 1; 60502ac6454SAndrew Thompson } 60602ac6454SAndrew Thompson DPRINTF("status=0x%04x \n", 60702ac6454SAndrew Thompson UGETW(ds.wStatus)); 60802ac6454SAndrew Thompson } else 60902ac6454SAndrew Thompson selfpowered = 1; 61002ac6454SAndrew Thompson } 61102ac6454SAndrew Thompson DPRINTF("udev=%p cdesc=%p (addr %d) cno=%d attr=0x%02x, " 61202ac6454SAndrew Thompson "selfpowered=%d, power=%d\n", 61302ac6454SAndrew Thompson udev, cdp, 61491f72cedSAndrew Thompson udev->address, cdp->bConfigurationValue, cdp->bmAttributes, 61502ac6454SAndrew Thompson selfpowered, cdp->bMaxPower * 2); 61602ac6454SAndrew Thompson 61702ac6454SAndrew Thompson /* Check if we have enough power. */ 61802ac6454SAndrew Thompson power = cdp->bMaxPower * 2; 61902ac6454SAndrew Thompson 62002ac6454SAndrew Thompson if (udev->parent_hub) { 62102ac6454SAndrew Thompson max_power = udev->parent_hub->hub->portpower; 62202ac6454SAndrew Thompson } else { 62302ac6454SAndrew Thompson max_power = USB_MAX_POWER; 62402ac6454SAndrew Thompson } 62502ac6454SAndrew Thompson 62602ac6454SAndrew Thompson if (power > max_power) { 62702ac6454SAndrew Thompson DPRINTFN(0, "power exceeded %d > %d\n", power, max_power); 62802ac6454SAndrew Thompson err = USB_ERR_NO_POWER; 62902ac6454SAndrew Thompson goto done; 63002ac6454SAndrew Thompson } 63102ac6454SAndrew Thompson /* Only update "self_powered" in USB Host Mode */ 632f29a0724SAndrew Thompson if (udev->flags.usb_mode == USB_MODE_HOST) { 63302ac6454SAndrew Thompson udev->flags.self_powered = selfpowered; 63402ac6454SAndrew Thompson } 63502ac6454SAndrew Thompson udev->power = power; 63602ac6454SAndrew Thompson udev->curr_config_no = cdp->bConfigurationValue; 63702ac6454SAndrew Thompson udev->curr_config_index = index; 638a593f6b8SAndrew Thompson usb_set_device_state(udev, USB_STATE_CONFIGURED); 63902ac6454SAndrew Thompson 64002ac6454SAndrew Thompson /* Set the actual configuration value. */ 641a593f6b8SAndrew Thompson err = usbd_req_set_config(udev, NULL, cdp->bConfigurationValue); 64202ac6454SAndrew Thompson if (err) { 64302ac6454SAndrew Thompson goto done; 64402ac6454SAndrew Thompson } 645bdd41206SAndrew Thompson 646a593f6b8SAndrew Thompson err = usb_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_ALLOC); 64702ac6454SAndrew Thompson if (err) { 64802ac6454SAndrew Thompson goto done; 64902ac6454SAndrew Thompson } 650bdd41206SAndrew Thompson 651a593f6b8SAndrew Thompson err = usb_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_INIT); 652bdd41206SAndrew Thompson if (err) { 653bdd41206SAndrew Thompson goto done; 65402ac6454SAndrew Thompson } 655bdd41206SAndrew Thompson 6560f6e8f93SAndrew Thompson #if USB_HAVE_UGEN 657ee3e3ff5SAndrew Thompson /* create device nodes for each endpoint */ 658a593f6b8SAndrew Thompson usb_cdev_create(udev); 6590f6e8f93SAndrew Thompson #endif 66002ac6454SAndrew Thompson 66102ac6454SAndrew Thompson done: 662a593f6b8SAndrew Thompson DPRINTF("error=%s\n", usbd_errstr(err)); 66302ac6454SAndrew Thompson if (err) { 664d88688c7SAndrew Thompson usb_unconfigure(udev, 0); 66502ac6454SAndrew Thompson } 666cb18f7d1SAlfred Perlstein if (do_unlock) 667cb18f7d1SAlfred Perlstein usbd_enum_unlock(udev); 66802ac6454SAndrew Thompson return (err); 66902ac6454SAndrew Thompson } 67002ac6454SAndrew Thompson 67102ac6454SAndrew Thompson /*------------------------------------------------------------------------* 672a593f6b8SAndrew Thompson * usb_config_parse 673bdd41206SAndrew Thompson * 674ae60fdfbSAndrew Thompson * This function will allocate and free USB interfaces and USB endpoints, 675ae60fdfbSAndrew Thompson * parse the USB configuration structure and initialise the USB endpoints 676bdd41206SAndrew Thompson * and interfaces. If "iface_index" is not equal to 677bdd41206SAndrew Thompson * "USB_IFACE_INDEX_ANY" then the "cmd" parameter is the 678bdd41206SAndrew Thompson * alternate_setting to be selected for the given interface. Else the 679bdd41206SAndrew Thompson * "cmd" parameter is defined by "USB_CFG_XXX". "iface_index" can be 680bdd41206SAndrew Thompson * "USB_IFACE_INDEX_ANY" or a valid USB interface index. This function 681bdd41206SAndrew Thompson * is typically called when setting the configuration or when setting 682bdd41206SAndrew Thompson * an alternate interface. 683bdd41206SAndrew Thompson * 684bdd41206SAndrew Thompson * Returns: 685bdd41206SAndrew Thompson * 0: Success 686bdd41206SAndrew Thompson * Else: Failure 687bdd41206SAndrew Thompson *------------------------------------------------------------------------*/ 688963169b4SHans Petter Selasky static usb_error_t 689a593f6b8SAndrew Thompson usb_config_parse(struct usb_device *udev, uint8_t iface_index, uint8_t cmd) 690bdd41206SAndrew Thompson { 691760bc48eSAndrew Thompson struct usb_idesc_parse_state ips; 692760bc48eSAndrew Thompson struct usb_interface_descriptor *id; 693760bc48eSAndrew Thompson struct usb_endpoint_descriptor *ed; 694760bc48eSAndrew Thompson struct usb_interface *iface; 695ae60fdfbSAndrew Thompson struct usb_endpoint *ep; 696e0a69b51SAndrew Thompson usb_error_t err; 697bdd41206SAndrew Thompson uint8_t ep_curr; 698bdd41206SAndrew Thompson uint8_t ep_max; 699bdd41206SAndrew Thompson uint8_t temp; 700bdd41206SAndrew Thompson uint8_t do_init; 701bdd41206SAndrew Thompson uint8_t alt_index; 702bdd41206SAndrew Thompson 703bdd41206SAndrew Thompson if (iface_index != USB_IFACE_INDEX_ANY) { 704bdd41206SAndrew Thompson /* parameter overload */ 705bdd41206SAndrew Thompson alt_index = cmd; 706bdd41206SAndrew Thompson cmd = USB_CFG_INIT; 707bdd41206SAndrew Thompson } else { 708bdd41206SAndrew Thompson /* not used */ 709bdd41206SAndrew Thompson alt_index = 0; 710bdd41206SAndrew Thompson } 711bdd41206SAndrew Thompson 712bdd41206SAndrew Thompson err = 0; 713bdd41206SAndrew Thompson 714bdd41206SAndrew Thompson DPRINTFN(5, "iface_index=%d cmd=%d\n", 715bdd41206SAndrew Thompson iface_index, cmd); 716bdd41206SAndrew Thompson 717bdd41206SAndrew Thompson if (cmd == USB_CFG_FREE) 718bdd41206SAndrew Thompson goto cleanup; 719bdd41206SAndrew Thompson 720bdd41206SAndrew Thompson if (cmd == USB_CFG_INIT) { 72191cd9240SAndrew Thompson sx_assert(&udev->enum_sx, SA_LOCKED); 722bdd41206SAndrew Thompson 723ae60fdfbSAndrew Thompson /* check for in-use endpoints */ 724bdd41206SAndrew Thompson 725ae60fdfbSAndrew Thompson ep = udev->endpoints; 726ae60fdfbSAndrew Thompson ep_max = udev->endpoints_max; 727bdd41206SAndrew Thompson while (ep_max--) { 728ae60fdfbSAndrew Thompson /* look for matching endpoints */ 729bdd41206SAndrew Thompson if ((iface_index == USB_IFACE_INDEX_ANY) || 730ae60fdfbSAndrew Thompson (iface_index == ep->iface_index)) { 731f12c6c29SAndrew Thompson if (ep->refcount_alloc != 0) { 732bdd41206SAndrew Thompson /* 733bdd41206SAndrew Thompson * This typically indicates a 734bdd41206SAndrew Thompson * more serious error. 735bdd41206SAndrew Thompson */ 736bdd41206SAndrew Thompson err = USB_ERR_IN_USE; 737bdd41206SAndrew Thompson } else { 738ae60fdfbSAndrew Thompson /* reset endpoint */ 739ae60fdfbSAndrew Thompson memset(ep, 0, sizeof(*ep)); 740ae60fdfbSAndrew Thompson /* make sure we don't zero the endpoint again */ 741ae60fdfbSAndrew Thompson ep->iface_index = USB_IFACE_INDEX_ANY; 742bdd41206SAndrew Thompson } 743bdd41206SAndrew Thompson } 744ae60fdfbSAndrew Thompson ep++; 745bdd41206SAndrew Thompson } 746bdd41206SAndrew Thompson 747bdd41206SAndrew Thompson if (err) 748bdd41206SAndrew Thompson return (err); 749bdd41206SAndrew Thompson } 750bdd41206SAndrew Thompson 751bdd41206SAndrew Thompson memset(&ips, 0, sizeof(ips)); 752bdd41206SAndrew Thompson 753bdd41206SAndrew Thompson ep_curr = 0; 754bdd41206SAndrew Thompson ep_max = 0; 755bdd41206SAndrew Thompson 756a593f6b8SAndrew Thompson while ((id = usb_idesc_foreach(udev->cdesc, &ips))) { 757bdd41206SAndrew Thompson 758bdd41206SAndrew Thompson iface = udev->ifaces + ips.iface_index; 759bdd41206SAndrew Thompson 760bdd41206SAndrew Thompson /* check for specific interface match */ 761bdd41206SAndrew Thompson 762bdd41206SAndrew Thompson if (cmd == USB_CFG_INIT) { 763bdd41206SAndrew Thompson if ((iface_index != USB_IFACE_INDEX_ANY) && 764bdd41206SAndrew Thompson (iface_index != ips.iface_index)) { 765bdd41206SAndrew Thompson /* wrong interface */ 766bdd41206SAndrew Thompson do_init = 0; 767bdd41206SAndrew Thompson } else if (alt_index != ips.iface_index_alt) { 768bdd41206SAndrew Thompson /* wrong alternate setting */ 769bdd41206SAndrew Thompson do_init = 0; 770bdd41206SAndrew Thompson } else { 771bdd41206SAndrew Thompson /* initialise interface */ 772bdd41206SAndrew Thompson do_init = 1; 773bdd41206SAndrew Thompson } 774bdd41206SAndrew Thompson } else 775bdd41206SAndrew Thompson do_init = 0; 776bdd41206SAndrew Thompson 777bdd41206SAndrew Thompson /* check for new interface */ 778bdd41206SAndrew Thompson if (ips.iface_index_alt == 0) { 779bdd41206SAndrew Thompson /* update current number of endpoints */ 780bdd41206SAndrew Thompson ep_curr = ep_max; 781bdd41206SAndrew Thompson } 782bdd41206SAndrew Thompson /* check for init */ 783bdd41206SAndrew Thompson if (do_init) { 784bdd41206SAndrew Thompson /* setup the USB interface structure */ 785bdd41206SAndrew Thompson iface->idesc = id; 786bdd41206SAndrew Thompson /* set alternate index */ 787bdd41206SAndrew Thompson iface->alt_index = alt_index; 788d9073c1eSHans Petter Selasky /* set default interface parent */ 789d9073c1eSHans Petter Selasky if (iface_index == USB_IFACE_INDEX_ANY) { 790d9073c1eSHans Petter Selasky iface->parent_iface_index = 791d9073c1eSHans Petter Selasky USB_IFACE_INDEX_ANY; 792d9073c1eSHans Petter Selasky } 793bdd41206SAndrew Thompson } 794bdd41206SAndrew Thompson 795bdd41206SAndrew Thompson DPRINTFN(5, "found idesc nendpt=%d\n", id->bNumEndpoints); 796bdd41206SAndrew Thompson 797760bc48eSAndrew Thompson ed = (struct usb_endpoint_descriptor *)id; 798bdd41206SAndrew Thompson 799bdd41206SAndrew Thompson temp = ep_curr; 800bdd41206SAndrew Thompson 801bdd41206SAndrew Thompson /* iterate all the endpoint descriptors */ 802a593f6b8SAndrew Thompson while ((ed = usb_edesc_foreach(udev->cdesc, ed))) { 803bdd41206SAndrew Thompson 8045b0752bbSHans Petter Selasky /* check if endpoint limit has been reached */ 8055b0752bbSHans Petter Selasky if (temp >= USB_MAX_EP_UNITS) { 8065b0752bbSHans Petter Selasky DPRINTF("Endpoint limit reached\n"); 8075b0752bbSHans Petter Selasky break; 8085b0752bbSHans Petter Selasky } 809bdd41206SAndrew Thompson 810ae60fdfbSAndrew Thompson ep = udev->endpoints + temp; 811bdd41206SAndrew Thompson 812bdd41206SAndrew Thompson if (do_init) { 813963169b4SHans Petter Selasky void *ecomp; 814963169b4SHans Petter Selasky 815963169b4SHans Petter Selasky ecomp = usb_ed_comp_foreach(udev->cdesc, (void *)ed); 816963169b4SHans Petter Selasky if (ecomp != NULL) 817963169b4SHans Petter Selasky DPRINTFN(5, "Found endpoint companion descriptor\n"); 818963169b4SHans Petter Selasky 819a593f6b8SAndrew Thompson usb_init_endpoint(udev, 820963169b4SHans Petter Selasky ips.iface_index, ed, ecomp, ep); 821bdd41206SAndrew Thompson } 822bdd41206SAndrew Thompson 823bdd41206SAndrew Thompson temp ++; 824bdd41206SAndrew Thompson 825bdd41206SAndrew Thompson /* find maximum number of endpoints */ 826bdd41206SAndrew Thompson if (ep_max < temp) 827bdd41206SAndrew Thompson ep_max = temp; 828bdd41206SAndrew Thompson } 829bdd41206SAndrew Thompson } 830bdd41206SAndrew Thompson 831bdd41206SAndrew Thompson /* NOTE: It is valid to have no interfaces and no endpoints! */ 832bdd41206SAndrew Thompson 833bdd41206SAndrew Thompson if (cmd == USB_CFG_ALLOC) { 834bdd41206SAndrew Thompson udev->ifaces_max = ips.iface_index; 8355b0752bbSHans Petter Selasky #if (USB_HAVE_FIXED_IFACE == 0) 836bdd41206SAndrew Thompson udev->ifaces = NULL; 837bdd41206SAndrew Thompson if (udev->ifaces_max != 0) { 838bdd41206SAndrew Thompson udev->ifaces = malloc(sizeof(*iface) * udev->ifaces_max, 839bdd41206SAndrew Thompson M_USB, M_WAITOK | M_ZERO); 840bdd41206SAndrew Thompson if (udev->ifaces == NULL) { 841bdd41206SAndrew Thompson err = USB_ERR_NOMEM; 842bdd41206SAndrew Thompson goto done; 843bdd41206SAndrew Thompson } 844bdd41206SAndrew Thompson } 8455b0752bbSHans Petter Selasky #endif 8465b0752bbSHans Petter Selasky #if (USB_HAVE_FIXED_ENDPOINT == 0) 8479469c929SAndrew Thompson if (ep_max != 0) { 848ae60fdfbSAndrew Thompson udev->endpoints = malloc(sizeof(*ep) * ep_max, 849bdd41206SAndrew Thompson M_USB, M_WAITOK | M_ZERO); 850ae60fdfbSAndrew Thompson if (udev->endpoints == NULL) { 851bdd41206SAndrew Thompson err = USB_ERR_NOMEM; 852bdd41206SAndrew Thompson goto done; 853bdd41206SAndrew Thompson } 8549469c929SAndrew Thompson } else { 855ae60fdfbSAndrew Thompson udev->endpoints = NULL; 856bdd41206SAndrew Thompson } 8575b0752bbSHans Petter Selasky #endif 8589469c929SAndrew Thompson USB_BUS_LOCK(udev->bus); 859ae60fdfbSAndrew Thompson udev->endpoints_max = ep_max; 8609469c929SAndrew Thompson /* reset any ongoing clear-stall */ 861ae60fdfbSAndrew Thompson udev->ep_curr = NULL; 8629469c929SAndrew Thompson USB_BUS_UNLOCK(udev->bus); 863bdd41206SAndrew Thompson } 8646c218933SHans Petter Selasky #if (USB_HAVE_FIXED_IFACE == 0) || (USB_HAVE_FIXED_ENDPOINT == 0) 865bdd41206SAndrew Thompson done: 8666c218933SHans Petter Selasky #endif 867bdd41206SAndrew Thompson if (err) { 868bdd41206SAndrew Thompson if (cmd == USB_CFG_ALLOC) { 869bdd41206SAndrew Thompson cleanup: 8709469c929SAndrew Thompson USB_BUS_LOCK(udev->bus); 871ae60fdfbSAndrew Thompson udev->endpoints_max = 0; 8729469c929SAndrew Thompson /* reset any ongoing clear-stall */ 873ae60fdfbSAndrew Thompson udev->ep_curr = NULL; 8749469c929SAndrew Thompson USB_BUS_UNLOCK(udev->bus); 8759469c929SAndrew Thompson 8765b0752bbSHans Petter Selasky #if (USB_HAVE_FIXED_IFACE == 0) 877bdd41206SAndrew Thompson free(udev->ifaces, M_USB); 878bdd41206SAndrew Thompson udev->ifaces = NULL; 8795b0752bbSHans Petter Selasky #endif 8805b0752bbSHans Petter Selasky #if (USB_HAVE_FIXED_ENDPOINT == 0) 8815b0752bbSHans Petter Selasky free(udev->endpoints, M_USB); 882ae60fdfbSAndrew Thompson udev->endpoints = NULL; 8835b0752bbSHans Petter Selasky #endif 884bdd41206SAndrew Thompson udev->ifaces_max = 0; 885bdd41206SAndrew Thompson } 886bdd41206SAndrew Thompson } 887bdd41206SAndrew Thompson return (err); 888bdd41206SAndrew Thompson } 889bdd41206SAndrew Thompson 890bdd41206SAndrew Thompson /*------------------------------------------------------------------------* 891a593f6b8SAndrew Thompson * usbd_set_alt_interface_index 89202ac6454SAndrew Thompson * 89302ac6454SAndrew Thompson * This function will select an alternate interface index for the 89402ac6454SAndrew Thompson * given interface index. The interface should not be in use when this 895e2805033SAndrew Thompson * function is called. That means there should not be any open USB 896e2805033SAndrew Thompson * transfers. Else an error is returned. If the alternate setting is 897e2805033SAndrew Thompson * already set this function will simply return success. This function 898e2805033SAndrew Thompson * is called in Host mode and Device mode! 89902ac6454SAndrew Thompson * 90002ac6454SAndrew Thompson * Returns: 90102ac6454SAndrew Thompson * 0: Success 90202ac6454SAndrew Thompson * Else: Failure 90302ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 904e0a69b51SAndrew Thompson usb_error_t 905a593f6b8SAndrew Thompson usbd_set_alt_interface_index(struct usb_device *udev, 90602ac6454SAndrew Thompson uint8_t iface_index, uint8_t alt_index) 90702ac6454SAndrew Thompson { 908a593f6b8SAndrew Thompson struct usb_interface *iface = usbd_get_iface(udev, iface_index); 909e0a69b51SAndrew Thompson usb_error_t err; 91002ac6454SAndrew Thompson uint8_t do_unlock; 91102ac6454SAndrew Thompson 9126950c75fSHans Petter Selasky /* Prevent re-enumeration */ 9136950c75fSHans Petter Selasky do_unlock = usbd_enum_lock(udev); 9146950c75fSHans Petter Selasky 91502ac6454SAndrew Thompson if (iface == NULL) { 91602ac6454SAndrew Thompson err = USB_ERR_INVAL; 91702ac6454SAndrew Thompson goto done; 91802ac6454SAndrew Thompson } 919e2805033SAndrew Thompson if (iface->alt_index == alt_index) { 920e2805033SAndrew Thompson /* 921e2805033SAndrew Thompson * Optimise away duplicate setting of 922e2805033SAndrew Thompson * alternate setting in USB Host Mode! 923e2805033SAndrew Thompson */ 924e2805033SAndrew Thompson err = 0; 925e2805033SAndrew Thompson goto done; 926e2805033SAndrew Thompson } 9270f6e8f93SAndrew Thompson #if USB_HAVE_UGEN 92802ac6454SAndrew Thompson /* 92902ac6454SAndrew Thompson * Free all generic FIFOs for this interface, except control 93002ac6454SAndrew Thompson * endpoint FIFOs: 93102ac6454SAndrew Thompson */ 932a593f6b8SAndrew Thompson usb_fifo_free_wrap(udev, iface_index, 0); 9330f6e8f93SAndrew Thompson #endif 934bdd41206SAndrew Thompson 935a593f6b8SAndrew Thompson err = usb_config_parse(udev, iface_index, alt_index); 93602ac6454SAndrew Thompson if (err) { 93702ac6454SAndrew Thompson goto done; 93802ac6454SAndrew Thompson } 939bd73b187SAlfred Perlstein if (iface->alt_index != alt_index) { 940bd73b187SAlfred Perlstein /* the alternate setting does not exist */ 941bd73b187SAlfred Perlstein err = USB_ERR_INVAL; 942bd73b187SAlfred Perlstein goto done; 943bd73b187SAlfred Perlstein } 944bd73b187SAlfred Perlstein 945a593f6b8SAndrew Thompson err = usbd_req_set_alt_interface_no(udev, NULL, iface_index, 94602ac6454SAndrew Thompson iface->idesc->bAlternateSetting); 94702ac6454SAndrew Thompson 94802ac6454SAndrew Thompson done: 949cb18f7d1SAlfred Perlstein if (do_unlock) 950cb18f7d1SAlfred Perlstein usbd_enum_unlock(udev); 95102ac6454SAndrew Thompson return (err); 95202ac6454SAndrew Thompson } 95302ac6454SAndrew Thompson 95402ac6454SAndrew Thompson /*------------------------------------------------------------------------* 955a593f6b8SAndrew Thompson * usbd_set_endpoint_stall 95602ac6454SAndrew Thompson * 957963169b4SHans Petter Selasky * This function is used to make a BULK or INTERRUPT endpoint send 958963169b4SHans Petter Selasky * STALL tokens in USB device mode. 95902ac6454SAndrew Thompson * 96002ac6454SAndrew Thompson * Returns: 96102ac6454SAndrew Thompson * 0: Success 96202ac6454SAndrew Thompson * Else: Failure 96302ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 964e0a69b51SAndrew Thompson usb_error_t 965a593f6b8SAndrew Thompson usbd_set_endpoint_stall(struct usb_device *udev, struct usb_endpoint *ep, 96602ac6454SAndrew Thompson uint8_t do_stall) 96702ac6454SAndrew Thompson { 968760bc48eSAndrew Thompson struct usb_xfer *xfer; 969a5cf1aaaSHans Petter Selasky usb_stream_t x; 97002ac6454SAndrew Thompson uint8_t et; 97102ac6454SAndrew Thompson uint8_t was_stalled; 97202ac6454SAndrew Thompson 973ae60fdfbSAndrew Thompson if (ep == NULL) { 97402ac6454SAndrew Thompson /* nothing to do */ 97502ac6454SAndrew Thompson DPRINTF("Cannot find endpoint\n"); 97602ac6454SAndrew Thompson /* 97702ac6454SAndrew Thompson * Pretend that the clear or set stall request is 97802ac6454SAndrew Thompson * successful else some USB host stacks can do 97902ac6454SAndrew Thompson * strange things, especially when a control endpoint 98002ac6454SAndrew Thompson * stalls. 98102ac6454SAndrew Thompson */ 98202ac6454SAndrew Thompson return (0); 98302ac6454SAndrew Thompson } 984ae60fdfbSAndrew Thompson et = (ep->edesc->bmAttributes & UE_XFERTYPE); 98502ac6454SAndrew Thompson 98602ac6454SAndrew Thompson if ((et != UE_BULK) && 98702ac6454SAndrew Thompson (et != UE_INTERRUPT)) { 98802ac6454SAndrew Thompson /* 98902ac6454SAndrew Thompson * Should not stall control 99002ac6454SAndrew Thompson * nor isochronous endpoints. 99102ac6454SAndrew Thompson */ 99202ac6454SAndrew Thompson DPRINTF("Invalid endpoint\n"); 99302ac6454SAndrew Thompson return (0); 99402ac6454SAndrew Thompson } 99502ac6454SAndrew Thompson USB_BUS_LOCK(udev->bus); 99602ac6454SAndrew Thompson 99702ac6454SAndrew Thompson /* store current stall state */ 998ae60fdfbSAndrew Thompson was_stalled = ep->is_stalled; 99902ac6454SAndrew Thompson 100002ac6454SAndrew Thompson /* check for no change */ 100102ac6454SAndrew Thompson if (was_stalled && do_stall) { 1002ae60fdfbSAndrew Thompson /* if the endpoint is already stalled do nothing */ 100302ac6454SAndrew Thompson USB_BUS_UNLOCK(udev->bus); 100402ac6454SAndrew Thompson DPRINTF("No change\n"); 100502ac6454SAndrew Thompson return (0); 100602ac6454SAndrew Thompson } 100702ac6454SAndrew Thompson /* set stalled state */ 1008ae60fdfbSAndrew Thompson ep->is_stalled = 1; 100902ac6454SAndrew Thompson 101002ac6454SAndrew Thompson if (do_stall || (!was_stalled)) { 101102ac6454SAndrew Thompson if (!was_stalled) { 1012a5cf1aaaSHans Petter Selasky for (x = 0; x != USB_MAX_EP_STREAMS; x++) { 101302ac6454SAndrew Thompson /* lookup the current USB transfer, if any */ 1014a5cf1aaaSHans Petter Selasky xfer = ep->endpoint_q[x].curr; 1015a5cf1aaaSHans Petter Selasky if (xfer != NULL) { 101602ac6454SAndrew Thompson /* 1017a5cf1aaaSHans Petter Selasky * The "xfer_stall" method 1018a5cf1aaaSHans Petter Selasky * will complete the USB 1019a5cf1aaaSHans Petter Selasky * transfer like in case of a 1020a5cf1aaaSHans Petter Selasky * timeout setting the error 1021a5cf1aaaSHans Petter Selasky * code "USB_ERR_STALLED". 102202ac6454SAndrew Thompson */ 1023a5cf1aaaSHans Petter Selasky (udev->bus->methods->xfer_stall) (xfer); 1024a5cf1aaaSHans Petter Selasky } 1025a5cf1aaaSHans Petter Selasky } 1026a5cf1aaaSHans Petter Selasky } 1027a5cf1aaaSHans Petter Selasky (udev->bus->methods->set_stall) (udev, ep, &do_stall); 102802ac6454SAndrew Thompson } 102902ac6454SAndrew Thompson if (!do_stall) { 1030ae60fdfbSAndrew Thompson ep->toggle_next = 0; /* reset data toggle */ 1031ae60fdfbSAndrew Thompson ep->is_stalled = 0; /* clear stalled state */ 103202ac6454SAndrew Thompson 1033ae60fdfbSAndrew Thompson (udev->bus->methods->clear_stall) (udev, ep); 103402ac6454SAndrew Thompson 1035a5cf1aaaSHans Petter Selasky /* start the current or next transfer, if any */ 1036a5cf1aaaSHans Petter Selasky for (x = 0; x != USB_MAX_EP_STREAMS; x++) { 1037a5cf1aaaSHans Petter Selasky usb_command_wrapper(&ep->endpoint_q[x], 1038a5cf1aaaSHans Petter Selasky ep->endpoint_q[x].curr); 1039a5cf1aaaSHans Petter Selasky } 104002ac6454SAndrew Thompson } 104102ac6454SAndrew Thompson USB_BUS_UNLOCK(udev->bus); 104202ac6454SAndrew Thompson return (0); 104302ac6454SAndrew Thompson } 104402ac6454SAndrew Thompson 104502ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1046a593f6b8SAndrew Thompson * usb_reset_iface_endpoints - used in USB device side mode 104702ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 1048e0a69b51SAndrew Thompson usb_error_t 1049a593f6b8SAndrew Thompson usb_reset_iface_endpoints(struct usb_device *udev, uint8_t iface_index) 105002ac6454SAndrew Thompson { 1051ae60fdfbSAndrew Thompson struct usb_endpoint *ep; 1052ae60fdfbSAndrew Thompson struct usb_endpoint *ep_end; 105302ac6454SAndrew Thompson 1054ae60fdfbSAndrew Thompson ep = udev->endpoints; 1055ae60fdfbSAndrew Thompson ep_end = udev->endpoints + udev->endpoints_max; 105602ac6454SAndrew Thompson 1057ae60fdfbSAndrew Thompson for (; ep != ep_end; ep++) { 105802ac6454SAndrew Thompson 1059ae60fdfbSAndrew Thompson if ((ep->edesc == NULL) || 1060ae60fdfbSAndrew Thompson (ep->iface_index != iface_index)) { 106102ac6454SAndrew Thompson continue; 106202ac6454SAndrew Thompson } 106302ac6454SAndrew Thompson /* simulate a clear stall from the peer */ 1064bd73b187SAlfred Perlstein usbd_set_endpoint_stall(udev, ep, 0); 106502ac6454SAndrew Thompson } 106602ac6454SAndrew Thompson return (0); 106702ac6454SAndrew Thompson } 106802ac6454SAndrew Thompson 106902ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1070a593f6b8SAndrew Thompson * usb_detach_device_sub 107102ac6454SAndrew Thompson * 107202ac6454SAndrew Thompson * This function will try to detach an USB device. If it fails a panic 107302ac6454SAndrew Thompson * will result. 1074bdd41206SAndrew Thompson * 1075bdd41206SAndrew Thompson * Flag values, see "USB_UNCFG_FLAG_XXX". 107602ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 107702ac6454SAndrew Thompson static void 1078a593f6b8SAndrew Thompson usb_detach_device_sub(struct usb_device *udev, device_t *ppdev, 10798427ed84SHans Petter Selasky char **ppnpinfo, uint8_t flag) 108002ac6454SAndrew Thompson { 108102ac6454SAndrew Thompson device_t dev; 10828427ed84SHans Petter Selasky char *pnpinfo; 108302ac6454SAndrew Thompson int err; 108402ac6454SAndrew Thompson 1085d88688c7SAndrew Thompson dev = *ppdev; 1086d88688c7SAndrew Thompson if (dev) { 108702ac6454SAndrew Thompson /* 108802ac6454SAndrew Thompson * NOTE: It is important to clear "*ppdev" before deleting 108902ac6454SAndrew Thompson * the child due to some device methods being called late 109002ac6454SAndrew Thompson * during the delete process ! 109102ac6454SAndrew Thompson */ 109202ac6454SAndrew Thompson *ppdev = NULL; 109302ac6454SAndrew Thompson 1094cabe79d9SMarius Strobl if (!rebooting) { 109502ac6454SAndrew Thompson device_printf(dev, "at %s, port %d, addr %d " 109602ac6454SAndrew Thompson "(disconnected)\n", 109702ac6454SAndrew Thompson device_get_nameunit(udev->parent_dev), 109802ac6454SAndrew Thompson udev->port_no, udev->address); 1099cabe79d9SMarius Strobl } 110002ac6454SAndrew Thompson 110102ac6454SAndrew Thompson if (device_is_attached(dev)) { 1102ec8f3127SAndrew Thompson if (udev->flags.peer_suspended) { 110302ac6454SAndrew Thompson err = DEVICE_RESUME(dev); 110402ac6454SAndrew Thompson if (err) { 1105767cb2e2SAndrew Thompson device_printf(dev, "Resume failed\n"); 110602ac6454SAndrew Thompson } 110702ac6454SAndrew Thompson } 110802ac6454SAndrew Thompson } 1109d3bf5efcSHans Petter Selasky /* detach and delete child */ 111002ac6454SAndrew Thompson if (device_delete_child(udev->parent_dev, dev)) { 111102ac6454SAndrew Thompson goto error; 111202ac6454SAndrew Thompson } 111302ac6454SAndrew Thompson } 11148427ed84SHans Petter Selasky 11158427ed84SHans Petter Selasky pnpinfo = *ppnpinfo; 11168427ed84SHans Petter Selasky if (pnpinfo != NULL) { 11178427ed84SHans Petter Selasky *ppnpinfo = NULL; 11188427ed84SHans Petter Selasky free(pnpinfo, M_USBDEV); 11198427ed84SHans Petter Selasky } 112002ac6454SAndrew Thompson return; 112102ac6454SAndrew Thompson 112202ac6454SAndrew Thompson error: 112302ac6454SAndrew Thompson /* Detach is not allowed to fail in the USB world */ 11248427ed84SHans Petter Selasky panic("usb_detach_device_sub: A USB driver would not detach\n"); 112502ac6454SAndrew Thompson } 112602ac6454SAndrew Thompson 112702ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1128a593f6b8SAndrew Thompson * usb_detach_device 112902ac6454SAndrew Thompson * 113002ac6454SAndrew Thompson * The following function will detach the matching interfaces. 113102ac6454SAndrew Thompson * This function is NULL safe. 1132bdd41206SAndrew Thompson * 1133bdd41206SAndrew Thompson * Flag values, see "USB_UNCFG_FLAG_XXX". 113402ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 113502ac6454SAndrew Thompson void 1136a593f6b8SAndrew Thompson usb_detach_device(struct usb_device *udev, uint8_t iface_index, 1137bdd41206SAndrew Thompson uint8_t flag) 113802ac6454SAndrew Thompson { 1139760bc48eSAndrew Thompson struct usb_interface *iface; 114002ac6454SAndrew Thompson uint8_t i; 114102ac6454SAndrew Thompson 114202ac6454SAndrew Thompson if (udev == NULL) { 114302ac6454SAndrew Thompson /* nothing to do */ 114402ac6454SAndrew Thompson return; 114502ac6454SAndrew Thompson } 114602ac6454SAndrew Thompson DPRINTFN(4, "udev=%p\n", udev); 114702ac6454SAndrew Thompson 114891cd9240SAndrew Thompson sx_assert(&udev->enum_sx, SA_LOCKED); 114902ac6454SAndrew Thompson 115002ac6454SAndrew Thompson /* 115102ac6454SAndrew Thompson * First detach the child to give the child's detach routine a 115202ac6454SAndrew Thompson * chance to detach the sub-devices in the correct order. 115302ac6454SAndrew Thompson * Then delete the child using "device_delete_child()" which 115402ac6454SAndrew Thompson * will detach all sub-devices from the bottom and upwards! 115502ac6454SAndrew Thompson */ 115602ac6454SAndrew Thompson if (iface_index != USB_IFACE_INDEX_ANY) { 115702ac6454SAndrew Thompson i = iface_index; 115802ac6454SAndrew Thompson iface_index = i + 1; 115902ac6454SAndrew Thompson } else { 116002ac6454SAndrew Thompson i = 0; 116102ac6454SAndrew Thompson iface_index = USB_IFACE_MAX; 116202ac6454SAndrew Thompson } 116302ac6454SAndrew Thompson 116402ac6454SAndrew Thompson /* do the detach */ 116502ac6454SAndrew Thompson 116602ac6454SAndrew Thompson for (; i != iface_index; i++) { 116702ac6454SAndrew Thompson 1168a593f6b8SAndrew Thompson iface = usbd_get_iface(udev, i); 116902ac6454SAndrew Thompson if (iface == NULL) { 117002ac6454SAndrew Thompson /* looks like the end of the USB interfaces */ 117102ac6454SAndrew Thompson break; 117202ac6454SAndrew Thompson } 11738427ed84SHans Petter Selasky usb_detach_device_sub(udev, &iface->subdev, 11748427ed84SHans Petter Selasky &iface->pnpinfo, flag); 117502ac6454SAndrew Thompson } 117602ac6454SAndrew Thompson } 117702ac6454SAndrew Thompson 117802ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1179a593f6b8SAndrew Thompson * usb_probe_and_attach_sub 118002ac6454SAndrew Thompson * 118102ac6454SAndrew Thompson * Returns: 118202ac6454SAndrew Thompson * 0: Success 118302ac6454SAndrew Thompson * Else: Failure 118402ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 118502ac6454SAndrew Thompson static uint8_t 1186a593f6b8SAndrew Thompson usb_probe_and_attach_sub(struct usb_device *udev, 1187760bc48eSAndrew Thompson struct usb_attach_arg *uaa) 118802ac6454SAndrew Thompson { 1189760bc48eSAndrew Thompson struct usb_interface *iface; 119002ac6454SAndrew Thompson device_t dev; 119102ac6454SAndrew Thompson int err; 119202ac6454SAndrew Thompson 119302ac6454SAndrew Thompson iface = uaa->iface; 119402ac6454SAndrew Thompson if (iface->parent_iface_index != USB_IFACE_INDEX_ANY) { 119502ac6454SAndrew Thompson /* leave interface alone */ 119602ac6454SAndrew Thompson return (0); 119702ac6454SAndrew Thompson } 119802ac6454SAndrew Thompson dev = iface->subdev; 119902ac6454SAndrew Thompson if (dev) { 120002ac6454SAndrew Thompson 120102ac6454SAndrew Thompson /* clean up after module unload */ 120202ac6454SAndrew Thompson 120302ac6454SAndrew Thompson if (device_is_attached(dev)) { 120402ac6454SAndrew Thompson /* already a device there */ 120502ac6454SAndrew Thompson return (0); 120602ac6454SAndrew Thompson } 120702ac6454SAndrew Thompson /* clear "iface->subdev" as early as possible */ 120802ac6454SAndrew Thompson 120902ac6454SAndrew Thompson iface->subdev = NULL; 121002ac6454SAndrew Thompson 121102ac6454SAndrew Thompson if (device_delete_child(udev->parent_dev, dev)) { 121202ac6454SAndrew Thompson 121302ac6454SAndrew Thompson /* 121402ac6454SAndrew Thompson * Panic here, else one can get a double call 121502ac6454SAndrew Thompson * to device_detach(). USB devices should 121602ac6454SAndrew Thompson * never fail on detach! 121702ac6454SAndrew Thompson */ 1218767cb2e2SAndrew Thompson panic("device_delete_child() failed\n"); 121902ac6454SAndrew Thompson } 122002ac6454SAndrew Thompson } 122102ac6454SAndrew Thompson if (uaa->temp_dev == NULL) { 122202ac6454SAndrew Thompson 122302ac6454SAndrew Thompson /* create a new child */ 122402ac6454SAndrew Thompson uaa->temp_dev = device_add_child(udev->parent_dev, NULL, -1); 122502ac6454SAndrew Thompson if (uaa->temp_dev == NULL) { 122602ac6454SAndrew Thompson device_printf(udev->parent_dev, 1227767cb2e2SAndrew Thompson "Device creation failed\n"); 122802ac6454SAndrew Thompson return (1); /* failure */ 122902ac6454SAndrew Thompson } 123002ac6454SAndrew Thompson device_set_ivars(uaa->temp_dev, uaa); 123102ac6454SAndrew Thompson device_quiet(uaa->temp_dev); 123202ac6454SAndrew Thompson } 123302ac6454SAndrew Thompson /* 123402ac6454SAndrew Thompson * Set "subdev" before probe and attach so that "devd" gets 123502ac6454SAndrew Thompson * the information it needs. 123602ac6454SAndrew Thompson */ 123702ac6454SAndrew Thompson iface->subdev = uaa->temp_dev; 123802ac6454SAndrew Thompson 123902ac6454SAndrew Thompson if (device_probe_and_attach(iface->subdev) == 0) { 124002ac6454SAndrew Thompson /* 124102ac6454SAndrew Thompson * The USB attach arguments are only available during probe 124202ac6454SAndrew Thompson * and attach ! 124302ac6454SAndrew Thompson */ 124402ac6454SAndrew Thompson uaa->temp_dev = NULL; 124502ac6454SAndrew Thompson device_set_ivars(iface->subdev, NULL); 124602ac6454SAndrew Thompson 1247ec8f3127SAndrew Thompson if (udev->flags.peer_suspended) { 124802ac6454SAndrew Thompson err = DEVICE_SUSPEND(iface->subdev); 124988b4e0abSAndrew Thompson if (err) 125002ac6454SAndrew Thompson device_printf(iface->subdev, "Suspend failed\n"); 125102ac6454SAndrew Thompson } 125202ac6454SAndrew Thompson return (0); /* success */ 125302ac6454SAndrew Thompson } else { 125402ac6454SAndrew Thompson /* No USB driver found */ 125502ac6454SAndrew Thompson iface->subdev = NULL; 125602ac6454SAndrew Thompson } 125702ac6454SAndrew Thompson return (1); /* failure */ 125802ac6454SAndrew Thompson } 125902ac6454SAndrew Thompson 126002ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1261a593f6b8SAndrew Thompson * usbd_set_parent_iface 126202ac6454SAndrew Thompson * 126302ac6454SAndrew Thompson * Using this function will lock the alternate interface setting on an 126402ac6454SAndrew Thompson * interface. It is typically used for multi interface drivers. In USB 126502ac6454SAndrew Thompson * device side mode it is assumed that the alternate interfaces all 126602ac6454SAndrew Thompson * have the same endpoint descriptors. The default parent index value 126702ac6454SAndrew Thompson * is "USB_IFACE_INDEX_ANY". Then the alternate setting value is not 126802ac6454SAndrew Thompson * locked. 126902ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 127002ac6454SAndrew Thompson void 1271a593f6b8SAndrew Thompson usbd_set_parent_iface(struct usb_device *udev, uint8_t iface_index, 127202ac6454SAndrew Thompson uint8_t parent_index) 127302ac6454SAndrew Thompson { 1274760bc48eSAndrew Thompson struct usb_interface *iface; 127502ac6454SAndrew Thompson 1276d9073c1eSHans Petter Selasky if (udev == NULL) { 1277d9073c1eSHans Petter Selasky /* nothing to do */ 1278d9073c1eSHans Petter Selasky return; 127902ac6454SAndrew Thompson } 1280d9073c1eSHans Petter Selasky iface = usbd_get_iface(udev, iface_index); 1281d9073c1eSHans Petter Selasky if (iface != NULL) 1282d9073c1eSHans Petter Selasky iface->parent_iface_index = parent_index; 128302ac6454SAndrew Thompson } 128402ac6454SAndrew Thompson 128502ac6454SAndrew Thompson static void 1286a593f6b8SAndrew Thompson usb_init_attach_arg(struct usb_device *udev, 1287760bc48eSAndrew Thompson struct usb_attach_arg *uaa) 128802ac6454SAndrew Thompson { 1289d46dc4adSHans Petter Selasky memset(uaa, 0, sizeof(*uaa)); 129002ac6454SAndrew Thompson 129102ac6454SAndrew Thompson uaa->device = udev; 1292f29a0724SAndrew Thompson uaa->usb_mode = udev->flags.usb_mode; 129302ac6454SAndrew Thompson uaa->port = udev->port_no; 12942a4c6157SAndrew Thompson uaa->dev_state = UAA_DEV_READY; 129502ac6454SAndrew Thompson 129602ac6454SAndrew Thompson uaa->info.idVendor = UGETW(udev->ddesc.idVendor); 129702ac6454SAndrew Thompson uaa->info.idProduct = UGETW(udev->ddesc.idProduct); 129802ac6454SAndrew Thompson uaa->info.bcdDevice = UGETW(udev->ddesc.bcdDevice); 129902ac6454SAndrew Thompson uaa->info.bDeviceClass = udev->ddesc.bDeviceClass; 130002ac6454SAndrew Thompson uaa->info.bDeviceSubClass = udev->ddesc.bDeviceSubClass; 130102ac6454SAndrew Thompson uaa->info.bDeviceProtocol = udev->ddesc.bDeviceProtocol; 130202ac6454SAndrew Thompson uaa->info.bConfigIndex = udev->curr_config_index; 130302ac6454SAndrew Thompson uaa->info.bConfigNum = udev->curr_config_no; 130402ac6454SAndrew Thompson } 130502ac6454SAndrew Thompson 130602ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1307a593f6b8SAndrew Thompson * usb_probe_and_attach 130802ac6454SAndrew Thompson * 130902ac6454SAndrew Thompson * This function is called from "uhub_explore_sub()", 1310a593f6b8SAndrew Thompson * "usb_handle_set_config()" and "usb_handle_request()". 131102ac6454SAndrew Thompson * 131202ac6454SAndrew Thompson * Returns: 131302ac6454SAndrew Thompson * 0: Success 131402ac6454SAndrew Thompson * Else: A control transfer failed 131502ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 1316e0a69b51SAndrew Thompson usb_error_t 1317a593f6b8SAndrew Thompson usb_probe_and_attach(struct usb_device *udev, uint8_t iface_index) 131802ac6454SAndrew Thompson { 1319760bc48eSAndrew Thompson struct usb_attach_arg uaa; 1320760bc48eSAndrew Thompson struct usb_interface *iface; 132102ac6454SAndrew Thompson uint8_t i; 132202ac6454SAndrew Thompson uint8_t j; 132302ac6454SAndrew Thompson uint8_t do_unlock; 132402ac6454SAndrew Thompson 132502ac6454SAndrew Thompson if (udev == NULL) { 132602ac6454SAndrew Thompson DPRINTF("udev == NULL\n"); 132702ac6454SAndrew Thompson return (USB_ERR_INVAL); 132802ac6454SAndrew Thompson } 13296950c75fSHans Petter Selasky /* Prevent re-enumeration */ 13306950c75fSHans Petter Selasky do_unlock = usbd_enum_lock(udev); 133102ac6454SAndrew Thompson 133202ac6454SAndrew Thompson if (udev->curr_config_index == USB_UNCONFIG_INDEX) { 133302ac6454SAndrew Thompson /* do nothing - no configuration has been set */ 133402ac6454SAndrew Thompson goto done; 133502ac6454SAndrew Thompson } 133602ac6454SAndrew Thompson /* setup USB attach arguments */ 133702ac6454SAndrew Thompson 1338a593f6b8SAndrew Thompson usb_init_attach_arg(udev, &uaa); 133902ac6454SAndrew Thompson 1340a6b60150SHans Petter Selasky /* 1341a6b60150SHans Petter Selasky * If the whole USB device is targeted, invoke the USB event 1342a6b60150SHans Petter Selasky * handler(s): 1343a6b60150SHans Petter Selasky */ 1344a6b60150SHans Petter Selasky if (iface_index == USB_IFACE_INDEX_ANY) { 1345a6b60150SHans Petter Selasky 1346415bcd89SHans Petter Selasky if (usb_test_quirk(&uaa, UQ_MSC_DYMO_EJECT) != 0 && 1347415bcd89SHans Petter Selasky usb_dymo_eject(udev, 0) == 0) { 1348415bcd89SHans Petter Selasky /* success, mark the udev as disappearing */ 1349415bcd89SHans Petter Selasky uaa.dev_state = UAA_DEV_EJECTING; 1350415bcd89SHans Petter Selasky } 1351415bcd89SHans Petter Selasky 1352a6b60150SHans Petter Selasky EVENTHANDLER_INVOKE(usb_dev_configured, udev, &uaa); 1353a6b60150SHans Petter Selasky 1354a6b60150SHans Petter Selasky if (uaa.dev_state != UAA_DEV_READY) { 1355a6b60150SHans Petter Selasky /* leave device unconfigured */ 1356a6b60150SHans Petter Selasky usb_unconfigure(udev, 0); 1357a6b60150SHans Petter Selasky goto done; 1358a6b60150SHans Petter Selasky } 1359a6b60150SHans Petter Selasky } 1360a6b60150SHans Petter Selasky 136102ac6454SAndrew Thompson /* Check if only one interface should be probed: */ 136202ac6454SAndrew Thompson if (iface_index != USB_IFACE_INDEX_ANY) { 136302ac6454SAndrew Thompson i = iface_index; 136402ac6454SAndrew Thompson j = i + 1; 136502ac6454SAndrew Thompson } else { 136602ac6454SAndrew Thompson i = 0; 136702ac6454SAndrew Thompson j = USB_IFACE_MAX; 136802ac6454SAndrew Thompson } 136902ac6454SAndrew Thompson 137002ac6454SAndrew Thompson /* Do the probe and attach */ 137102ac6454SAndrew Thompson for (; i != j; i++) { 137202ac6454SAndrew Thompson 1373a593f6b8SAndrew Thompson iface = usbd_get_iface(udev, i); 137402ac6454SAndrew Thompson if (iface == NULL) { 137502ac6454SAndrew Thompson /* 137602ac6454SAndrew Thompson * Looks like the end of the USB 137702ac6454SAndrew Thompson * interfaces ! 137802ac6454SAndrew Thompson */ 137902ac6454SAndrew Thompson DPRINTFN(2, "end of interfaces " 138002ac6454SAndrew Thompson "at %u\n", i); 138102ac6454SAndrew Thompson break; 138202ac6454SAndrew Thompson } 138302ac6454SAndrew Thompson if (iface->idesc == NULL) { 138402ac6454SAndrew Thompson /* no interface descriptor */ 138502ac6454SAndrew Thompson continue; 138602ac6454SAndrew Thompson } 138702ac6454SAndrew Thompson uaa.iface = iface; 138802ac6454SAndrew Thompson 138902ac6454SAndrew Thompson uaa.info.bInterfaceClass = 139002ac6454SAndrew Thompson iface->idesc->bInterfaceClass; 139102ac6454SAndrew Thompson uaa.info.bInterfaceSubClass = 139202ac6454SAndrew Thompson iface->idesc->bInterfaceSubClass; 139302ac6454SAndrew Thompson uaa.info.bInterfaceProtocol = 139402ac6454SAndrew Thompson iface->idesc->bInterfaceProtocol; 139502ac6454SAndrew Thompson uaa.info.bIfaceIndex = i; 139602ac6454SAndrew Thompson uaa.info.bIfaceNum = 139702ac6454SAndrew Thompson iface->idesc->bInterfaceNumber; 1398bd73b187SAlfred Perlstein uaa.driver_info = 0; /* reset driver_info */ 139902ac6454SAndrew Thompson 140002ac6454SAndrew Thompson DPRINTFN(2, "iclass=%u/%u/%u iindex=%u/%u\n", 140102ac6454SAndrew Thompson uaa.info.bInterfaceClass, 140202ac6454SAndrew Thompson uaa.info.bInterfaceSubClass, 140302ac6454SAndrew Thompson uaa.info.bInterfaceProtocol, 140402ac6454SAndrew Thompson uaa.info.bIfaceIndex, 140502ac6454SAndrew Thompson uaa.info.bIfaceNum); 140602ac6454SAndrew Thompson 1407d43ffe94SHans Petter Selasky usb_probe_and_attach_sub(udev, &uaa); 140802ac6454SAndrew Thompson 1409d43ffe94SHans Petter Selasky /* 1410d43ffe94SHans Petter Selasky * Remove the leftover child, if any, to enforce that 1411d43ffe94SHans Petter Selasky * a new nomatch devd event is generated for the next 1412d43ffe94SHans Petter Selasky * interface if no driver is found: 1413d43ffe94SHans Petter Selasky */ 1414d43ffe94SHans Petter Selasky if (uaa.temp_dev == NULL) 1415d43ffe94SHans Petter Selasky continue; 1416d43ffe94SHans Petter Selasky if (device_delete_child(udev->parent_dev, uaa.temp_dev)) 1417767cb2e2SAndrew Thompson DPRINTFN(0, "device delete child failed\n"); 1418d43ffe94SHans Petter Selasky uaa.temp_dev = NULL; 141902ac6454SAndrew Thompson } 142002ac6454SAndrew Thompson done: 1421cb18f7d1SAlfred Perlstein if (do_unlock) 1422cb18f7d1SAlfred Perlstein usbd_enum_unlock(udev); 142302ac6454SAndrew Thompson return (0); 142402ac6454SAndrew Thompson } 142502ac6454SAndrew Thompson 142602ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1427a593f6b8SAndrew Thompson * usb_suspend_resume_sub 142802ac6454SAndrew Thompson * 142902ac6454SAndrew Thompson * This function is called when the suspend or resume methods should 143002ac6454SAndrew Thompson * be executed on an USB device. 143102ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 143202ac6454SAndrew Thompson static void 1433a593f6b8SAndrew Thompson usb_suspend_resume_sub(struct usb_device *udev, device_t dev, uint8_t do_suspend) 143402ac6454SAndrew Thompson { 143502ac6454SAndrew Thompson int err; 143602ac6454SAndrew Thompson 143702ac6454SAndrew Thompson if (dev == NULL) { 143802ac6454SAndrew Thompson return; 143902ac6454SAndrew Thompson } 144002ac6454SAndrew Thompson if (!device_is_attached(dev)) { 144102ac6454SAndrew Thompson return; 144202ac6454SAndrew Thompson } 144302ac6454SAndrew Thompson if (do_suspend) { 144402ac6454SAndrew Thompson err = DEVICE_SUSPEND(dev); 144502ac6454SAndrew Thompson } else { 144602ac6454SAndrew Thompson err = DEVICE_RESUME(dev); 144702ac6454SAndrew Thompson } 144802ac6454SAndrew Thompson if (err) { 1449767cb2e2SAndrew Thompson device_printf(dev, "%s failed\n", 145002ac6454SAndrew Thompson do_suspend ? "Suspend" : "Resume"); 145102ac6454SAndrew Thompson } 145202ac6454SAndrew Thompson } 145302ac6454SAndrew Thompson 145402ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1455a593f6b8SAndrew Thompson * usb_suspend_resume 145602ac6454SAndrew Thompson * 145702ac6454SAndrew Thompson * The following function will suspend or resume the USB device. 145802ac6454SAndrew Thompson * 145902ac6454SAndrew Thompson * Returns: 146002ac6454SAndrew Thompson * 0: Success 146102ac6454SAndrew Thompson * Else: Failure 146202ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 1463e0a69b51SAndrew Thompson usb_error_t 1464a593f6b8SAndrew Thompson usb_suspend_resume(struct usb_device *udev, uint8_t do_suspend) 146502ac6454SAndrew Thompson { 1466760bc48eSAndrew Thompson struct usb_interface *iface; 146702ac6454SAndrew Thompson uint8_t i; 146802ac6454SAndrew Thompson 146902ac6454SAndrew Thompson if (udev == NULL) { 147002ac6454SAndrew Thompson /* nothing to do */ 147102ac6454SAndrew Thompson return (0); 147202ac6454SAndrew Thompson } 147302ac6454SAndrew Thompson DPRINTFN(4, "udev=%p do_suspend=%d\n", udev, do_suspend); 147402ac6454SAndrew Thompson 14752df1e9a6SAndrew Thompson sx_assert(&udev->sr_sx, SA_LOCKED); 147602ac6454SAndrew Thompson 147702ac6454SAndrew Thompson USB_BUS_LOCK(udev->bus); 147802ac6454SAndrew Thompson /* filter the suspend events */ 1479ec8f3127SAndrew Thompson if (udev->flags.peer_suspended == do_suspend) { 148002ac6454SAndrew Thompson USB_BUS_UNLOCK(udev->bus); 148102ac6454SAndrew Thompson /* nothing to do */ 148202ac6454SAndrew Thompson return (0); 148302ac6454SAndrew Thompson } 1484ec8f3127SAndrew Thompson udev->flags.peer_suspended = do_suspend; 148502ac6454SAndrew Thompson USB_BUS_UNLOCK(udev->bus); 148602ac6454SAndrew Thompson 148702ac6454SAndrew Thompson /* do the suspend or resume */ 148802ac6454SAndrew Thompson 148902ac6454SAndrew Thompson for (i = 0; i != USB_IFACE_MAX; i++) { 149002ac6454SAndrew Thompson 1491a593f6b8SAndrew Thompson iface = usbd_get_iface(udev, i); 149202ac6454SAndrew Thompson if (iface == NULL) { 149302ac6454SAndrew Thompson /* looks like the end of the USB interfaces */ 149402ac6454SAndrew Thompson break; 149502ac6454SAndrew Thompson } 1496a593f6b8SAndrew Thompson usb_suspend_resume_sub(udev, iface->subdev, do_suspend); 149702ac6454SAndrew Thompson } 149802ac6454SAndrew Thompson return (0); 149902ac6454SAndrew Thompson } 150002ac6454SAndrew Thompson 150102ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1502a593f6b8SAndrew Thompson * usbd_clear_stall_proc 150302ac6454SAndrew Thompson * 150402ac6454SAndrew Thompson * This function performs generic USB clear stall operations. 150502ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 150602ac6454SAndrew Thompson static void 1507a593f6b8SAndrew Thompson usbd_clear_stall_proc(struct usb_proc_msg *_pm) 150802ac6454SAndrew Thompson { 1509a0d53e0bSHans Petter Selasky struct usb_udev_msg *pm = (void *)_pm; 1510760bc48eSAndrew Thompson struct usb_device *udev = pm->udev; 151102ac6454SAndrew Thompson 151202ac6454SAndrew Thompson /* Change lock */ 151302ac6454SAndrew Thompson USB_BUS_UNLOCK(udev->bus); 15140eb8d462SHans Petter Selasky USB_MTX_LOCK(&udev->device_mtx); 151502ac6454SAndrew Thompson 151602ac6454SAndrew Thompson /* Start clear stall callback */ 15175b3bb704SAndrew Thompson usbd_transfer_start(udev->ctrl_xfer[1]); 151802ac6454SAndrew Thompson 151902ac6454SAndrew Thompson /* Change lock */ 15200eb8d462SHans Petter Selasky USB_MTX_UNLOCK(&udev->device_mtx); 152102ac6454SAndrew Thompson USB_BUS_LOCK(udev->bus); 152202ac6454SAndrew Thompson } 152302ac6454SAndrew Thompson 152402ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1525a593f6b8SAndrew Thompson * usb_alloc_device 152602ac6454SAndrew Thompson * 152702ac6454SAndrew Thompson * This function allocates a new USB device. This function is called 152802ac6454SAndrew Thompson * when a new device has been put in the powered state, but not yet in 152902ac6454SAndrew Thompson * the addressed state. Get initial descriptor, set the address, get 153002ac6454SAndrew Thompson * full descriptor and get strings. 153102ac6454SAndrew Thompson * 153202ac6454SAndrew Thompson * Return values: 153302ac6454SAndrew Thompson * 0: Failure 153402ac6454SAndrew Thompson * Else: Success 153502ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 1536760bc48eSAndrew Thompson struct usb_device * 1537a593f6b8SAndrew Thompson usb_alloc_device(device_t parent_dev, struct usb_bus *bus, 1538760bc48eSAndrew Thompson struct usb_device *parent_hub, uint8_t depth, uint8_t port_index, 15398d2dd5ddSAndrew Thompson uint8_t port_no, enum usb_dev_speed speed, enum usb_hc_mode mode) 154002ac6454SAndrew Thompson { 1541760bc48eSAndrew Thompson struct usb_attach_arg uaa; 1542760bc48eSAndrew Thompson struct usb_device *udev; 1543760bc48eSAndrew Thompson struct usb_device *adev; 1544760bc48eSAndrew Thompson struct usb_device *hub; 154502ac6454SAndrew Thompson uint8_t *scratch_ptr; 1546e0a69b51SAndrew Thompson usb_error_t err; 154702ac6454SAndrew Thompson uint8_t device_index; 15482a4c6157SAndrew Thompson uint8_t config_index; 15492a4c6157SAndrew Thompson uint8_t config_quirk; 15502a4c6157SAndrew Thompson uint8_t set_config_failed; 15516950c75fSHans Petter Selasky uint8_t do_unlock; 155202ac6454SAndrew Thompson 155302ac6454SAndrew Thompson DPRINTF("parent_dev=%p, bus=%p, parent_hub=%p, depth=%u, " 1554f29a0724SAndrew Thompson "port_index=%u, port_no=%u, speed=%u, usb_mode=%u\n", 155502ac6454SAndrew Thompson parent_dev, bus, parent_hub, depth, port_index, port_no, 1556f29a0724SAndrew Thompson speed, mode); 155702ac6454SAndrew Thompson 155802ac6454SAndrew Thompson /* 155902ac6454SAndrew Thompson * Find an unused device index. In USB Host mode this is the 156002ac6454SAndrew Thompson * same as the device address. 156102ac6454SAndrew Thompson * 156202ac6454SAndrew Thompson * Device index zero is not used and device index 1 should 156302ac6454SAndrew Thompson * always be the root hub. 156402ac6454SAndrew Thompson */ 156502ac6454SAndrew Thompson for (device_index = USB_ROOT_HUB_ADDR; 156602ac6454SAndrew Thompson (device_index != bus->devices_max) && 156702ac6454SAndrew Thompson (bus->devices[device_index] != NULL); 156802ac6454SAndrew Thompson device_index++) /* nop */; 156902ac6454SAndrew Thompson 157002ac6454SAndrew Thompson if (device_index == bus->devices_max) { 157102ac6454SAndrew Thompson device_printf(bus->bdev, 1572767cb2e2SAndrew Thompson "No free USB device index for new device\n"); 157302ac6454SAndrew Thompson return (NULL); 157402ac6454SAndrew Thompson } 157502ac6454SAndrew Thompson 157602ac6454SAndrew Thompson if (depth > 0x10) { 157702ac6454SAndrew Thompson device_printf(bus->bdev, 1578767cb2e2SAndrew Thompson "Invalid device depth\n"); 157902ac6454SAndrew Thompson return (NULL); 158002ac6454SAndrew Thompson } 158102ac6454SAndrew Thompson udev = malloc(sizeof(*udev), M_USB, M_WAITOK | M_ZERO); 158202ac6454SAndrew Thompson if (udev == NULL) { 158302ac6454SAndrew Thompson return (NULL); 158402ac6454SAndrew Thompson } 158502ac6454SAndrew Thompson /* initialise our SX-lock */ 158691cd9240SAndrew Thompson sx_init_flags(&udev->enum_sx, "USB config SX lock", SX_DUPOK); 1587a6b60150SHans Petter Selasky sx_init_flags(&udev->sr_sx, "USB suspend and resume SX lock", SX_NOWITNESS); 158864cb5e2aSHans Petter Selasky sx_init_flags(&udev->ctrl_sx, "USB control transfer SX lock", SX_DUPOK); 158902ac6454SAndrew Thompson 159091cd9240SAndrew Thompson cv_init(&udev->ctrlreq_cv, "WCTRL"); 159191cd9240SAndrew Thompson cv_init(&udev->ref_cv, "UGONE"); 159202ac6454SAndrew Thompson 159302ac6454SAndrew Thompson /* initialise our mutex */ 159491cd9240SAndrew Thompson mtx_init(&udev->device_mtx, "USB device mutex", NULL, MTX_DEF); 159502ac6454SAndrew Thompson 159602ac6454SAndrew Thompson /* initialise generic clear stall */ 1597a593f6b8SAndrew Thompson udev->cs_msg[0].hdr.pm_callback = &usbd_clear_stall_proc; 159802ac6454SAndrew Thompson udev->cs_msg[0].udev = udev; 1599a593f6b8SAndrew Thompson udev->cs_msg[1].hdr.pm_callback = &usbd_clear_stall_proc; 160002ac6454SAndrew Thompson udev->cs_msg[1].udev = udev; 160102ac6454SAndrew Thompson 160202ac6454SAndrew Thompson /* initialise some USB device fields */ 160302ac6454SAndrew Thompson udev->parent_hub = parent_hub; 160402ac6454SAndrew Thompson udev->parent_dev = parent_dev; 160502ac6454SAndrew Thompson udev->port_index = port_index; 160602ac6454SAndrew Thompson udev->port_no = port_no; 160702ac6454SAndrew Thompson udev->depth = depth; 160802ac6454SAndrew Thompson udev->bus = bus; 160902ac6454SAndrew Thompson udev->address = USB_START_ADDR; /* default value */ 1610e0a69b51SAndrew Thompson udev->plugtime = (usb_ticks_t)ticks; 161102ac6454SAndrew Thompson /* 161202ac6454SAndrew Thompson * We need to force the power mode to "on" because there are plenty 161302ac6454SAndrew Thompson * of USB devices out there that do not work very well with 161402ac6454SAndrew Thompson * automatic suspend and resume! 161502ac6454SAndrew Thompson */ 1616d2d71ce7SAndrew Thompson udev->power_mode = usbd_filter_power_mode(udev, USB_POWER_MODE_ON); 161702ac6454SAndrew Thompson udev->pwr_save.last_xfer_time = ticks; 161802ac6454SAndrew Thompson /* we are not ready yet */ 161902ac6454SAndrew Thompson udev->refcount = 1; 162002ac6454SAndrew Thompson 162102ac6454SAndrew Thompson /* set up default endpoint descriptor */ 16225b3bb704SAndrew Thompson udev->ctrl_ep_desc.bLength = sizeof(udev->ctrl_ep_desc); 16235b3bb704SAndrew Thompson udev->ctrl_ep_desc.bDescriptorType = UDESC_ENDPOINT; 16245b3bb704SAndrew Thompson udev->ctrl_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT; 16255b3bb704SAndrew Thompson udev->ctrl_ep_desc.bmAttributes = UE_CONTROL; 16265b3bb704SAndrew Thompson udev->ctrl_ep_desc.wMaxPacketSize[0] = USB_MAX_IPACKET; 16275b3bb704SAndrew Thompson udev->ctrl_ep_desc.wMaxPacketSize[1] = 0; 16285b3bb704SAndrew Thompson udev->ctrl_ep_desc.bInterval = 0; 1629963169b4SHans Petter Selasky 1630963169b4SHans Petter Selasky /* set up default endpoint companion descriptor */ 1631963169b4SHans Petter Selasky udev->ctrl_ep_comp_desc.bLength = sizeof(udev->ctrl_ep_comp_desc); 1632963169b4SHans Petter Selasky udev->ctrl_ep_comp_desc.bDescriptorType = UDESC_ENDPOINT_SS_COMP; 1633963169b4SHans Petter Selasky 163402ac6454SAndrew Thompson udev->ddesc.bMaxPacketSize = USB_MAX_IPACKET; 163502ac6454SAndrew Thompson 163602ac6454SAndrew Thompson udev->speed = speed; 1637f29a0724SAndrew Thompson udev->flags.usb_mode = mode; 163802ac6454SAndrew Thompson 163902ac6454SAndrew Thompson /* search for our High Speed USB HUB, if any */ 164002ac6454SAndrew Thompson 164102ac6454SAndrew Thompson adev = udev; 164202ac6454SAndrew Thompson hub = udev->parent_hub; 164302ac6454SAndrew Thompson 164402ac6454SAndrew Thompson while (hub) { 164502ac6454SAndrew Thompson if (hub->speed == USB_SPEED_HIGH) { 164602ac6454SAndrew Thompson udev->hs_hub_addr = hub->address; 1647495f25ceSAndrew Thompson udev->parent_hs_hub = hub; 164802ac6454SAndrew Thompson udev->hs_port_no = adev->port_no; 164902ac6454SAndrew Thompson break; 165002ac6454SAndrew Thompson } 165102ac6454SAndrew Thompson adev = hub; 165202ac6454SAndrew Thompson hub = hub->parent_hub; 165302ac6454SAndrew Thompson } 165402ac6454SAndrew Thompson 1655ae60fdfbSAndrew Thompson /* init the default endpoint */ 1656a593f6b8SAndrew Thompson usb_init_endpoint(udev, 0, 16575b3bb704SAndrew Thompson &udev->ctrl_ep_desc, 1658963169b4SHans Petter Selasky &udev->ctrl_ep_comp_desc, 16595b3bb704SAndrew Thompson &udev->ctrl_ep); 166002ac6454SAndrew Thompson 166102ac6454SAndrew Thompson /* set device index */ 166202ac6454SAndrew Thompson udev->device_index = device_index; 166302ac6454SAndrew Thompson 16648755859aSAndrew Thompson #if USB_HAVE_UGEN 16650f6e8f93SAndrew Thompson /* Create ugen name */ 1666ee3e3ff5SAndrew Thompson snprintf(udev->ugen_name, sizeof(udev->ugen_name), 1667ee3e3ff5SAndrew Thompson USB_GENERIC_NAME "%u.%u", device_get_unit(bus->bdev), 1668ee3e3ff5SAndrew Thompson device_index); 16698755859aSAndrew Thompson LIST_INIT(&udev->pd_list); 16708755859aSAndrew Thompson 16710f6e8f93SAndrew Thompson /* Create the control endpoint device */ 16722ffd5fdcSHans Petter Selasky udev->ctrl_dev = usb_make_dev(udev, NULL, 0, 0, 16732ffd5fdcSHans Petter Selasky FREAD|FWRITE, UID_ROOT, GID_OPERATOR, 0600); 16740f6e8f93SAndrew Thompson 16750f6e8f93SAndrew Thompson /* Create a link from /dev/ugenX.X to the default endpoint */ 16762ffd5fdcSHans Petter Selasky if (udev->ctrl_dev != NULL) 16772ffd5fdcSHans Petter Selasky make_dev_alias(udev->ctrl_dev->cdev, "%s", udev->ugen_name); 16780f6e8f93SAndrew Thompson #endif 1679963169b4SHans Petter Selasky /* Initialise device */ 1680963169b4SHans Petter Selasky if (bus->methods->device_init != NULL) { 1681963169b4SHans Petter Selasky err = (bus->methods->device_init) (udev); 1682963169b4SHans Petter Selasky if (err != 0) { 1683963169b4SHans Petter Selasky DPRINTFN(0, "device init %d failed " 1684963169b4SHans Petter Selasky "(%s, ignored)\n", device_index, 1685963169b4SHans Petter Selasky usbd_errstr(err)); 1686963169b4SHans Petter Selasky goto done; 1687963169b4SHans Petter Selasky } 1688963169b4SHans Petter Selasky } 1689963169b4SHans Petter Selasky /* set powered device state after device init is complete */ 1690963169b4SHans Petter Selasky usb_set_device_state(udev, USB_STATE_POWERED); 1691963169b4SHans Petter Selasky 1692f29a0724SAndrew Thompson if (udev->flags.usb_mode == USB_MODE_HOST) { 169302ac6454SAndrew Thompson 1694a593f6b8SAndrew Thompson err = usbd_req_set_address(udev, NULL, device_index); 169502ac6454SAndrew Thompson 1696963169b4SHans Petter Selasky /* 1697963169b4SHans Petter Selasky * This is the new USB device address from now on, if 1698963169b4SHans Petter Selasky * the set address request didn't set it already. 1699963169b4SHans Petter Selasky */ 1700963169b4SHans Petter Selasky if (udev->address == USB_START_ADDR) 170102ac6454SAndrew Thompson udev->address = device_index; 170202ac6454SAndrew Thompson 170302ac6454SAndrew Thompson /* 170402ac6454SAndrew Thompson * We ignore any set-address errors, hence there are 170502ac6454SAndrew Thompson * buggy USB devices out there that actually receive 170602ac6454SAndrew Thompson * the SETUP PID, but manage to set the address before 170702ac6454SAndrew Thompson * the STATUS stage is ACK'ed. If the device responds 170802ac6454SAndrew Thompson * to the subsequent get-descriptor at the new 170902ac6454SAndrew Thompson * address, then we know that the set-address command 171002ac6454SAndrew Thompson * was successful. 171102ac6454SAndrew Thompson */ 171202ac6454SAndrew Thompson if (err) { 171302ac6454SAndrew Thompson DPRINTFN(0, "set address %d failed " 171403797f33SAndrew Thompson "(%s, ignored)\n", udev->address, 1715a593f6b8SAndrew Thompson usbd_errstr(err)); 171602ac6454SAndrew Thompson } 171702ac6454SAndrew Thompson } else { 171802ac6454SAndrew Thompson /* We are not self powered */ 171902ac6454SAndrew Thompson udev->flags.self_powered = 0; 172002ac6454SAndrew Thompson 172102ac6454SAndrew Thompson /* Set unconfigured state */ 172202ac6454SAndrew Thompson udev->curr_config_no = USB_UNCONFIG_NO; 172302ac6454SAndrew Thompson udev->curr_config_index = USB_UNCONFIG_INDEX; 172402ac6454SAndrew Thompson 172502ac6454SAndrew Thompson /* Setup USB descriptors */ 1726a593f6b8SAndrew Thompson err = (usb_temp_setup_by_index_p) (udev, usb_template); 172702ac6454SAndrew Thompson if (err) { 17286be75992SEdward Tomasz Napierala DPRINTFN(0, "setting up USB template failed - " 17296be75992SEdward Tomasz Napierala "usb_template(4) not loaded?\n"); 173002ac6454SAndrew Thompson goto done; 173102ac6454SAndrew Thompson } 173202ac6454SAndrew Thompson } 1733a593f6b8SAndrew Thompson usb_set_device_state(udev, USB_STATE_ADDRESSED); 173402ac6454SAndrew Thompson 1735963169b4SHans Petter Selasky /* setup the device descriptor and the initial "wMaxPacketSize" */ 1736963169b4SHans Petter Selasky err = usbd_setup_device_desc(udev, NULL); 1737963169b4SHans Petter Selasky 1738963169b4SHans Petter Selasky if (err != 0) { 173923de050bSHans Petter Selasky /* try to enumerate two more times */ 1740a593f6b8SAndrew Thompson err = usbd_req_re_enumerate(udev, NULL); 174123de050bSHans Petter Selasky if (err != 0) { 174223de050bSHans Petter Selasky err = usbd_req_re_enumerate(udev, NULL); 174323de050bSHans Petter Selasky if (err != 0) { 174402ac6454SAndrew Thompson goto done; 174502ac6454SAndrew Thompson } 174623de050bSHans Petter Selasky } 174723de050bSHans Petter Selasky } 174802ac6454SAndrew Thompson 174902ac6454SAndrew Thompson /* 175002ac6454SAndrew Thompson * Setup temporary USB attach args so that we can figure out some 175102ac6454SAndrew Thompson * basic quirks for this device. 175202ac6454SAndrew Thompson */ 1753a593f6b8SAndrew Thompson usb_init_attach_arg(udev, &uaa); 175402ac6454SAndrew Thompson 1755a593f6b8SAndrew Thompson if (usb_test_quirk(&uaa, UQ_BUS_POWERED)) { 175602ac6454SAndrew Thompson udev->flags.uq_bus_powered = 1; 175702ac6454SAndrew Thompson } 1758a593f6b8SAndrew Thompson if (usb_test_quirk(&uaa, UQ_NO_STRINGS)) { 175902ac6454SAndrew Thompson udev->flags.no_strings = 1; 176002ac6454SAndrew Thompson } 176102ac6454SAndrew Thompson /* 176202ac6454SAndrew Thompson * Workaround for buggy USB devices. 176302ac6454SAndrew Thompson * 176402ac6454SAndrew Thompson * It appears that some string-less USB chips will crash and 176502ac6454SAndrew Thompson * disappear if any attempts are made to read any string 176602ac6454SAndrew Thompson * descriptors. 176702ac6454SAndrew Thompson * 176802ac6454SAndrew Thompson * Try to detect such chips by checking the strings in the USB 176902ac6454SAndrew Thompson * device descriptor. If no strings are present there we 177002ac6454SAndrew Thompson * simply disable all USB strings. 177102ac6454SAndrew Thompson */ 17726950c75fSHans Petter Selasky 17736950c75fSHans Petter Selasky /* Protect scratch area */ 177464cb5e2aSHans Petter Selasky do_unlock = usbd_ctrl_lock(udev); 17756950c75fSHans Petter Selasky 17766950c75fSHans Petter Selasky scratch_ptr = udev->scratch.data; 177702ac6454SAndrew Thompson 1778869df733SHans Petter Selasky if (udev->flags.no_strings) { 1779869df733SHans Petter Selasky err = USB_ERR_INVAL; 1780869df733SHans Petter Selasky } else if (udev->ddesc.iManufacturer || 178102ac6454SAndrew Thompson udev->ddesc.iProduct || 178202ac6454SAndrew Thompson udev->ddesc.iSerialNumber) { 178302ac6454SAndrew Thompson /* read out the language ID string */ 1784a593f6b8SAndrew Thompson err = usbd_req_get_string_desc(udev, NULL, 1785c10e1453SAndrew Thompson (char *)scratch_ptr, 4, 0, USB_LANGUAGE_TABLE); 178602ac6454SAndrew Thompson } else { 178702ac6454SAndrew Thompson err = USB_ERR_INVAL; 178802ac6454SAndrew Thompson } 178902ac6454SAndrew Thompson 179002ac6454SAndrew Thompson if (err || (scratch_ptr[0] < 4)) { 179102ac6454SAndrew Thompson udev->flags.no_strings = 1; 179202ac6454SAndrew Thompson } else { 17937a918edfSAndrew Thompson uint16_t langid; 17947a918edfSAndrew Thompson uint16_t pref; 17957a918edfSAndrew Thompson uint16_t mask; 17967a918edfSAndrew Thompson uint8_t x; 17977a918edfSAndrew Thompson 17987a918edfSAndrew Thompson /* load preferred value and mask */ 17997a918edfSAndrew Thompson pref = usb_lang_id; 18007a918edfSAndrew Thompson mask = usb_lang_mask; 18017a918edfSAndrew Thompson 18027a918edfSAndrew Thompson /* align length correctly */ 18036950c75fSHans Petter Selasky scratch_ptr[0] &= ~1U; 18047a918edfSAndrew Thompson 18057a918edfSAndrew Thompson /* fix compiler warning */ 18067a918edfSAndrew Thompson langid = 0; 18077a918edfSAndrew Thompson 18087a918edfSAndrew Thompson /* search for preferred language */ 18097a918edfSAndrew Thompson for (x = 2; (x < scratch_ptr[0]); x += 2) { 18107a918edfSAndrew Thompson langid = UGETW(scratch_ptr + x); 18117a918edfSAndrew Thompson if ((langid & mask) == pref) 18127a918edfSAndrew Thompson break; 18137a918edfSAndrew Thompson } 18147a918edfSAndrew Thompson if (x >= scratch_ptr[0]) { 181502ac6454SAndrew Thompson /* pick the first language as the default */ 18167a918edfSAndrew Thompson DPRINTFN(1, "Using first language\n"); 18177a918edfSAndrew Thompson langid = UGETW(scratch_ptr + 2); 18187a918edfSAndrew Thompson } 18197a918edfSAndrew Thompson 18207a918edfSAndrew Thompson DPRINTFN(1, "Language selected: 0x%04x\n", langid); 18217a918edfSAndrew Thompson udev->langid = langid; 182202ac6454SAndrew Thompson } 182302ac6454SAndrew Thompson 18246950c75fSHans Petter Selasky if (do_unlock) 182564cb5e2aSHans Petter Selasky usbd_ctrl_unlock(udev); 18266950c75fSHans Petter Selasky 182702ac6454SAndrew Thompson /* assume 100mA bus powered for now. Changed when configured. */ 182802ac6454SAndrew Thompson udev->power = USB_MIN_POWER; 1829c86aead6SAndrew Thompson /* fetch the vendor and product strings from the device */ 1830a593f6b8SAndrew Thompson usbd_set_device_strings(udev); 183102ac6454SAndrew Thompson 18322a4c6157SAndrew Thompson if (udev->flags.usb_mode == USB_MODE_DEVICE) { 18332a4c6157SAndrew Thompson /* USB device mode setup is complete */ 18342a4c6157SAndrew Thompson err = 0; 18352a4c6157SAndrew Thompson goto config_done; 18362a4c6157SAndrew Thompson } 183702ac6454SAndrew Thompson 183802ac6454SAndrew Thompson /* 183902ac6454SAndrew Thompson * Most USB devices should attach to config index 0 by 184002ac6454SAndrew Thompson * default 184102ac6454SAndrew Thompson */ 1842a593f6b8SAndrew Thompson if (usb_test_quirk(&uaa, UQ_CFG_INDEX_0)) { 184302ac6454SAndrew Thompson config_index = 0; 184402ac6454SAndrew Thompson config_quirk = 1; 1845a593f6b8SAndrew Thompson } else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_1)) { 184602ac6454SAndrew Thompson config_index = 1; 184702ac6454SAndrew Thompson config_quirk = 1; 1848a593f6b8SAndrew Thompson } else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_2)) { 184902ac6454SAndrew Thompson config_index = 2; 185002ac6454SAndrew Thompson config_quirk = 1; 1851a593f6b8SAndrew Thompson } else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_3)) { 185202ac6454SAndrew Thompson config_index = 3; 185302ac6454SAndrew Thompson config_quirk = 1; 1854a593f6b8SAndrew Thompson } else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_4)) { 185502ac6454SAndrew Thompson config_index = 4; 185602ac6454SAndrew Thompson config_quirk = 1; 185702ac6454SAndrew Thompson } else { 185802ac6454SAndrew Thompson config_index = 0; 185902ac6454SAndrew Thompson config_quirk = 0; 186002ac6454SAndrew Thompson } 186102ac6454SAndrew Thompson 18622a4c6157SAndrew Thompson set_config_failed = 0; 186302ac6454SAndrew Thompson repeat_set_config: 186402ac6454SAndrew Thompson 186502ac6454SAndrew Thompson DPRINTF("setting config %u\n", config_index); 186602ac6454SAndrew Thompson 186702ac6454SAndrew Thompson /* get the USB device configured */ 1868a593f6b8SAndrew Thompson err = usbd_set_config_index(udev, config_index); 186902ac6454SAndrew Thompson if (err) { 187002ac6454SAndrew Thompson if (udev->ddesc.bNumConfigurations != 0) { 187102ac6454SAndrew Thompson if (!set_config_failed) { 187202ac6454SAndrew Thompson set_config_failed = 1; 187302ac6454SAndrew Thompson /* XXX try to re-enumerate the device */ 18742a4c6157SAndrew Thompson err = usbd_req_re_enumerate(udev, NULL); 187502ac6454SAndrew Thompson if (err == 0) 187602ac6454SAndrew Thompson goto repeat_set_config; 187702ac6454SAndrew Thompson } 18782a4c6157SAndrew Thompson DPRINTFN(0, "Failure selecting configuration index %u:" 18792a4c6157SAndrew Thompson "%s, port %u, addr %u (ignored)\n", 1880a593f6b8SAndrew Thompson config_index, usbd_errstr(err), udev->port_no, 188102ac6454SAndrew Thompson udev->address); 188202ac6454SAndrew Thompson } 188302ac6454SAndrew Thompson /* 18842a4c6157SAndrew Thompson * Some USB devices do not have any configurations. Ignore any 18852a4c6157SAndrew Thompson * set config failures! 188602ac6454SAndrew Thompson */ 188702ac6454SAndrew Thompson err = 0; 18882a4c6157SAndrew Thompson goto config_done; 18892a4c6157SAndrew Thompson } 18902a4c6157SAndrew Thompson if (!config_quirk && config_index + 1 < udev->ddesc.bNumConfigurations) { 189102ac6454SAndrew Thompson if ((udev->cdesc->bNumInterface < 2) && 18922a4c6157SAndrew Thompson usbd_get_no_descriptors(udev->cdesc, UDESC_ENDPOINT) == 0) { 18932a4c6157SAndrew Thompson DPRINTFN(0, "Found no endpoints, trying next config\n"); 189402ac6454SAndrew Thompson config_index++; 189502ac6454SAndrew Thompson goto repeat_set_config; 189602ac6454SAndrew Thompson } 189715d4edd3SHans Petter Selasky #if USB_HAVE_MSCTEST 189802ac6454SAndrew Thompson if (config_index == 0) { 189902ac6454SAndrew Thompson /* 190002ac6454SAndrew Thompson * Try to figure out if we have an 190102ac6454SAndrew Thompson * auto-install disk there: 190202ac6454SAndrew Thompson */ 1903d84a79e7SAndrew Thompson if (usb_iface_is_cdrom(udev, 0)) { 190402ac6454SAndrew Thompson DPRINTFN(0, "Found possible auto-install " 190502ac6454SAndrew Thompson "disk (trying next config)\n"); 190602ac6454SAndrew Thompson config_index++; 190702ac6454SAndrew Thompson goto repeat_set_config; 190802ac6454SAndrew Thompson } 190902ac6454SAndrew Thompson } 191015d4edd3SHans Petter Selasky #endif 19112a4c6157SAndrew Thompson } 191215d4edd3SHans Petter Selasky #if USB_HAVE_MSCTEST 1913d46dc4adSHans Petter Selasky if (set_config_failed == 0 && config_index == 0 && 19149157ad4bSHans Petter Selasky usb_test_quirk(&uaa, UQ_MSC_NO_SYNC_CACHE) == 0 && 19159157ad4bSHans Petter Selasky usb_test_quirk(&uaa, UQ_MSC_NO_GETMAXLUN) == 0) { 1916d46dc4adSHans Petter Selasky 1917d46dc4adSHans Petter Selasky /* 1918d46dc4adSHans Petter Selasky * Try to figure out if there are any MSC quirks we 1919d46dc4adSHans Petter Selasky * should apply automatically: 1920d46dc4adSHans Petter Selasky */ 1921d46dc4adSHans Petter Selasky err = usb_msc_auto_quirk(udev, 0); 1922d46dc4adSHans Petter Selasky 1923d46dc4adSHans Petter Selasky if (err != 0) { 1924d46dc4adSHans Petter Selasky set_config_failed = 1; 1925d46dc4adSHans Petter Selasky goto repeat_set_config; 1926d46dc4adSHans Petter Selasky } 1927d46dc4adSHans Petter Selasky } 192815d4edd3SHans Petter Selasky #endif 192902ac6454SAndrew Thompson 19302a4c6157SAndrew Thompson config_done: 193102ac6454SAndrew Thompson DPRINTF("new dev (addr %d), udev=%p, parent_hub=%p\n", 193202ac6454SAndrew Thompson udev->address, udev, udev->parent_hub); 193302ac6454SAndrew Thompson 193402ac6454SAndrew Thompson /* register our device - we are ready */ 1935a593f6b8SAndrew Thompson usb_bus_port_set_device(bus, parent_hub ? 193602ac6454SAndrew Thompson parent_hub->hub->ports + port_index : NULL, udev, device_index); 193702ac6454SAndrew Thompson 19380f6e8f93SAndrew Thompson #if USB_HAVE_UGEN 19398755859aSAndrew Thompson /* Symlink the ugen device name */ 1940a593f6b8SAndrew Thompson udev->ugen_symlink = usb_alloc_symlink(udev->ugen_name); 19418755859aSAndrew Thompson 19428755859aSAndrew Thompson /* Announce device */ 194388f9a4ffSEdward Tomasz Napierala printf("%s: <%s %s> at %s\n", udev->ugen_name, 194488f9a4ffSEdward Tomasz Napierala usb_get_manufacturer(udev), usb_get_product(udev), 194502ac6454SAndrew Thompson device_get_nameunit(udev->bus->bdev)); 19468bb77249SHans Petter Selasky #endif 19477efaaa9aSAndrew Thompson 19488bb77249SHans Petter Selasky #if USB_HAVE_DEVCTL 19493acd904dSAndrew Thompson usb_notify_addq("ATTACH", udev); 19508755859aSAndrew Thompson #endif 195102ac6454SAndrew Thompson done: 195202ac6454SAndrew Thompson if (err) { 1953d88688c7SAndrew Thompson /* 1954d88688c7SAndrew Thompson * Free USB device and all subdevices, if any. 1955d88688c7SAndrew Thompson */ 1956d88688c7SAndrew Thompson usb_free_device(udev, 0); 195702ac6454SAndrew Thompson udev = NULL; 195802ac6454SAndrew Thompson } 195902ac6454SAndrew Thompson return (udev); 196002ac6454SAndrew Thompson } 196102ac6454SAndrew Thompson 19620f6e8f93SAndrew Thompson #if USB_HAVE_UGEN 19632ffd5fdcSHans Petter Selasky struct usb_fs_privdata * 19642ffd5fdcSHans Petter Selasky usb_make_dev(struct usb_device *udev, const char *devname, int ep, 19652ffd5fdcSHans Petter Selasky int fi, int rwmode, uid_t uid, gid_t gid, int mode) 1966ee3e3ff5SAndrew Thompson { 1967760bc48eSAndrew Thompson struct usb_fs_privdata* pd; 1968ac3490fdSHans Petter Selasky struct make_dev_args args; 19692ffd5fdcSHans Petter Selasky char buffer[32]; 1970ee3e3ff5SAndrew Thompson 1971ee3e3ff5SAndrew Thompson /* Store information to locate ourselves again later */ 1972760bc48eSAndrew Thompson pd = malloc(sizeof(struct usb_fs_privdata), M_USBDEV, 1973ee3e3ff5SAndrew Thompson M_WAITOK | M_ZERO); 1974ee3e3ff5SAndrew Thompson pd->bus_index = device_get_unit(udev->bus->bdev); 1975ee3e3ff5SAndrew Thompson pd->dev_index = udev->device_index; 1976ee3e3ff5SAndrew Thompson pd->ep_addr = ep; 19772ffd5fdcSHans Petter Selasky pd->fifo_index = fi; 19782ffd5fdcSHans Petter Selasky pd->mode = rwmode; 1979ee3e3ff5SAndrew Thompson 1980ee3e3ff5SAndrew Thompson /* Now, create the device itself */ 19812ffd5fdcSHans Petter Selasky if (devname == NULL) { 19822ffd5fdcSHans Petter Selasky devname = buffer; 19832ffd5fdcSHans Petter Selasky snprintf(buffer, sizeof(buffer), USB_DEVICE_DIR "/%u.%u.%u", 1984f35aaff0SAndrew Thompson pd->bus_index, pd->dev_index, pd->ep_addr); 19852ffd5fdcSHans Petter Selasky } 19862ffd5fdcSHans Petter Selasky 1987ac3490fdSHans Petter Selasky /* Setup arguments for make_dev_s() */ 1988ac3490fdSHans Petter Selasky make_dev_args_init(&args); 1989ac3490fdSHans Petter Selasky args.mda_devsw = &usb_devsw; 1990ac3490fdSHans Petter Selasky args.mda_uid = uid; 1991ac3490fdSHans Petter Selasky args.mda_gid = gid; 1992ac3490fdSHans Petter Selasky args.mda_mode = mode; 1993ac3490fdSHans Petter Selasky args.mda_si_drv1 = pd; 19942ffd5fdcSHans Petter Selasky 1995ac3490fdSHans Petter Selasky if (make_dev_s(&args, &pd->cdev, "%s", devname) != 0) { 19962ffd5fdcSHans Petter Selasky DPRINTFN(0, "Failed to create device %s\n", devname); 19972ffd5fdcSHans Petter Selasky free(pd, M_USBDEV); 19982ffd5fdcSHans Petter Selasky return (NULL); 19992ffd5fdcSHans Petter Selasky } 20002ffd5fdcSHans Petter Selasky return (pd); 20012ffd5fdcSHans Petter Selasky } 20022ffd5fdcSHans Petter Selasky 20032ffd5fdcSHans Petter Selasky void 2004b78e84d1SHans Petter Selasky usb_destroy_dev_sync(struct usb_fs_privdata *pd) 20052ffd5fdcSHans Petter Selasky { 2006b78e84d1SHans Petter Selasky DPRINTFN(1, "Destroying device at ugen%d.%d\n", 2007b78e84d1SHans Petter Selasky pd->bus_index, pd->dev_index); 20082ffd5fdcSHans Petter Selasky 2009b78e84d1SHans Petter Selasky /* 2010b78e84d1SHans Petter Selasky * Destroy character device synchronously. After this 2011b78e84d1SHans Petter Selasky * all system calls are returned. Can block. 2012b78e84d1SHans Petter Selasky */ 20132ffd5fdcSHans Petter Selasky destroy_dev(pd->cdev); 20142ffd5fdcSHans Petter Selasky 20152ffd5fdcSHans Petter Selasky free(pd, M_USBDEV); 2016ee3e3ff5SAndrew Thompson } 2017ee3e3ff5SAndrew Thompson 2018b78e84d1SHans Petter Selasky void 2019b78e84d1SHans Petter Selasky usb_destroy_dev(struct usb_fs_privdata *pd) 2020b78e84d1SHans Petter Selasky { 2021b78e84d1SHans Petter Selasky struct usb_bus *bus; 2022b78e84d1SHans Petter Selasky 2023b78e84d1SHans Petter Selasky if (pd == NULL) 2024b78e84d1SHans Petter Selasky return; 2025b78e84d1SHans Petter Selasky 2026b78e84d1SHans Petter Selasky mtx_lock(&usb_ref_lock); 2027b78e84d1SHans Petter Selasky bus = devclass_get_softc(usb_devclass_ptr, pd->bus_index); 2028b78e84d1SHans Petter Selasky mtx_unlock(&usb_ref_lock); 2029b78e84d1SHans Petter Selasky 2030b78e84d1SHans Petter Selasky if (bus == NULL) { 2031b78e84d1SHans Petter Selasky usb_destroy_dev_sync(pd); 2032b78e84d1SHans Petter Selasky return; 2033b78e84d1SHans Petter Selasky } 2034b78e84d1SHans Petter Selasky 203507dbde67SHans Petter Selasky /* make sure we can re-use the device name */ 203607dbde67SHans Petter Selasky delist_dev(pd->cdev); 203707dbde67SHans Petter Selasky 2038b78e84d1SHans Petter Selasky USB_BUS_LOCK(bus); 2039b78e84d1SHans Petter Selasky LIST_INSERT_HEAD(&bus->pd_cleanup_list, pd, pd_next); 2040b78e84d1SHans Petter Selasky /* get cleanup going */ 2041b78e84d1SHans Petter Selasky usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus), 2042b78e84d1SHans Petter Selasky &bus->cleanup_msg[0], &bus->cleanup_msg[1]); 2043b78e84d1SHans Petter Selasky USB_BUS_UNLOCK(bus); 2044b78e84d1SHans Petter Selasky } 2045b78e84d1SHans Petter Selasky 2046ee3e3ff5SAndrew Thompson static void 2047a593f6b8SAndrew Thompson usb_cdev_create(struct usb_device *udev) 2048ee3e3ff5SAndrew Thompson { 2049760bc48eSAndrew Thompson struct usb_config_descriptor *cd; 2050760bc48eSAndrew Thompson struct usb_endpoint_descriptor *ed; 2051760bc48eSAndrew Thompson struct usb_descriptor *desc; 2052760bc48eSAndrew Thompson struct usb_fs_privdata* pd; 2053f5f145baSAndrew Thompson int inmode, outmode, inmask, outmask, mode; 2054f5f145baSAndrew Thompson uint8_t ep; 2055ee3e3ff5SAndrew Thompson 2056ee3e3ff5SAndrew Thompson KASSERT(LIST_FIRST(&udev->pd_list) == NULL, ("stale cdev entries")); 2057ee3e3ff5SAndrew Thompson 2058ee3e3ff5SAndrew Thompson DPRINTFN(2, "Creating device nodes\n"); 2059ee3e3ff5SAndrew Thompson 2060a593f6b8SAndrew Thompson if (usbd_get_mode(udev) == USB_MODE_DEVICE) { 2061ee3e3ff5SAndrew Thompson inmode = FWRITE; 2062ee3e3ff5SAndrew Thompson outmode = FREAD; 2063ee3e3ff5SAndrew Thompson } else { /* USB_MODE_HOST */ 2064ee3e3ff5SAndrew Thompson inmode = FREAD; 2065ee3e3ff5SAndrew Thompson outmode = FWRITE; 2066ee3e3ff5SAndrew Thompson } 2067ee3e3ff5SAndrew Thompson 2068f5f145baSAndrew Thompson inmask = 0; 2069f5f145baSAndrew Thompson outmask = 0; 2070f5f145baSAndrew Thompson desc = NULL; 2071f5f145baSAndrew Thompson 2072f5f145baSAndrew Thompson /* 2073f5f145baSAndrew Thompson * Collect all used endpoint numbers instead of just 2074f5f145baSAndrew Thompson * generating 16 static endpoints. 2075f5f145baSAndrew Thompson */ 2076a593f6b8SAndrew Thompson cd = usbd_get_config_descriptor(udev); 2077a593f6b8SAndrew Thompson while ((desc = usb_desc_foreach(cd, desc))) { 2078f5f145baSAndrew Thompson /* filter out all endpoint descriptors */ 2079f5f145baSAndrew Thompson if ((desc->bDescriptorType == UDESC_ENDPOINT) && 2080f5f145baSAndrew Thompson (desc->bLength >= sizeof(*ed))) { 2081760bc48eSAndrew Thompson ed = (struct usb_endpoint_descriptor *)desc; 2082f5f145baSAndrew Thompson 2083f5f145baSAndrew Thompson /* update masks */ 2084f5f145baSAndrew Thompson ep = ed->bEndpointAddress; 2085f5f145baSAndrew Thompson if (UE_GET_DIR(ep) == UE_DIR_OUT) 2086f5f145baSAndrew Thompson outmask |= 1 << UE_GET_ADDR(ep); 2087f5f145baSAndrew Thompson else 2088f5f145baSAndrew Thompson inmask |= 1 << UE_GET_ADDR(ep); 2089f5f145baSAndrew Thompson } 2090f5f145baSAndrew Thompson } 2091ee3e3ff5SAndrew Thompson 2092ee3e3ff5SAndrew Thompson /* Create all available endpoints except EP0 */ 2093ee3e3ff5SAndrew Thompson for (ep = 1; ep < 16; ep++) { 20942ffd5fdcSHans Petter Selasky mode = (inmask & (1 << ep)) ? inmode : 0; 20952ffd5fdcSHans Petter Selasky mode |= (outmask & (1 << ep)) ? outmode : 0; 2096ee3e3ff5SAndrew Thompson if (mode == 0) 2097ee3e3ff5SAndrew Thompson continue; /* no IN or OUT endpoint */ 2098ee3e3ff5SAndrew Thompson 20992ffd5fdcSHans Petter Selasky pd = usb_make_dev(udev, NULL, ep, 0, 21002ffd5fdcSHans Petter Selasky mode, UID_ROOT, GID_OPERATOR, 0600); 21012ffd5fdcSHans Petter Selasky 21022ffd5fdcSHans Petter Selasky if (pd != NULL) 2103ee3e3ff5SAndrew Thompson LIST_INSERT_HEAD(&udev->pd_list, pd, pd_next); 2104ee3e3ff5SAndrew Thompson } 2105ee3e3ff5SAndrew Thompson } 2106ee3e3ff5SAndrew Thompson 2107ee3e3ff5SAndrew Thompson static void 2108a593f6b8SAndrew Thompson usb_cdev_free(struct usb_device *udev) 2109ee3e3ff5SAndrew Thompson { 2110760bc48eSAndrew Thompson struct usb_fs_privdata* pd; 2111ee3e3ff5SAndrew Thompson 2112ee3e3ff5SAndrew Thompson DPRINTFN(2, "Freeing device nodes\n"); 2113ee3e3ff5SAndrew Thompson 2114ee3e3ff5SAndrew Thompson while ((pd = LIST_FIRST(&udev->pd_list)) != NULL) { 2115ee3e3ff5SAndrew Thompson KASSERT(pd->cdev->si_drv1 == pd, ("privdata corrupt")); 2116ee3e3ff5SAndrew Thompson 2117ee3e3ff5SAndrew Thompson LIST_REMOVE(pd, pd_next); 2118ee3e3ff5SAndrew Thompson 21192ffd5fdcSHans Petter Selasky usb_destroy_dev(pd); 21202ffd5fdcSHans Petter Selasky } 2121ee3e3ff5SAndrew Thompson } 21220f6e8f93SAndrew Thompson #endif 2123ee3e3ff5SAndrew Thompson 212402ac6454SAndrew Thompson /*------------------------------------------------------------------------* 2125a593f6b8SAndrew Thompson * usb_free_device 212602ac6454SAndrew Thompson * 2127d88688c7SAndrew Thompson * This function is NULL safe and will free an USB device and its 2128d88688c7SAndrew Thompson * children devices, if any. 2129bdd41206SAndrew Thompson * 2130d88688c7SAndrew Thompson * Flag values: Reserved, set to zero. 213102ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 213202ac6454SAndrew Thompson void 2133a593f6b8SAndrew Thompson usb_free_device(struct usb_device *udev, uint8_t flag) 213402ac6454SAndrew Thompson { 2135760bc48eSAndrew Thompson struct usb_bus *bus; 2136bdd41206SAndrew Thompson 2137bdd41206SAndrew Thompson if (udev == NULL) 2138bdd41206SAndrew Thompson return; /* already freed */ 213902ac6454SAndrew Thompson 214002ac6454SAndrew Thompson DPRINTFN(4, "udev=%p port=%d\n", udev, udev->port_no); 214102ac6454SAndrew Thompson 2142bd216778SAndrew Thompson bus = udev->bus; 2143f97da128SHans Petter Selasky 2144f97da128SHans Petter Selasky /* set DETACHED state to prevent any further references */ 2145a593f6b8SAndrew Thompson usb_set_device_state(udev, USB_STATE_DETACHED); 2146bdd41206SAndrew Thompson 21478bb77249SHans Petter Selasky #if USB_HAVE_DEVCTL 21483acd904dSAndrew Thompson usb_notify_addq("DETACH", udev); 21498bb77249SHans Petter Selasky #endif 215002ac6454SAndrew Thompson 21518bb77249SHans Petter Selasky #if USB_HAVE_UGEN 2152cabe79d9SMarius Strobl if (!rebooting) { 215388f9a4ffSEdward Tomasz Napierala printf("%s: <%s %s> at %s (disconnected)\n", udev->ugen_name, 215488f9a4ffSEdward Tomasz Napierala usb_get_manufacturer(udev), usb_get_product(udev), 215588f9a4ffSEdward Tomasz Napierala device_get_nameunit(bus->bdev)); 2156cabe79d9SMarius Strobl } 215702ac6454SAndrew Thompson 2158ee3e3ff5SAndrew Thompson /* Destroy UGEN symlink, if any */ 215902ac6454SAndrew Thompson if (udev->ugen_symlink) { 2160a593f6b8SAndrew Thompson usb_free_symlink(udev->ugen_symlink); 216102ac6454SAndrew Thompson udev->ugen_symlink = NULL; 216202ac6454SAndrew Thompson } 216302ac6454SAndrew Thompson 21642ffd5fdcSHans Petter Selasky usb_destroy_dev(udev->ctrl_dev); 21650f6e8f93SAndrew Thompson #endif 216602ac6454SAndrew Thompson 2167f29a0724SAndrew Thompson if (udev->flags.usb_mode == USB_MODE_DEVICE) { 216802ac6454SAndrew Thompson /* stop receiving any control transfers (Device Side Mode) */ 21695b3bb704SAndrew Thompson usbd_transfer_unsetup(udev->ctrl_xfer, USB_CTRL_XFER_MAX); 217002ac6454SAndrew Thompson } 217102ac6454SAndrew Thompson 2172bdd41206SAndrew Thompson /* the following will get the device unconfigured in software */ 2173d88688c7SAndrew Thompson usb_unconfigure(udev, USB_UNCFG_FLAG_FREE_EP0); 217402ac6454SAndrew Thompson 2175f97da128SHans Petter Selasky /* final device unregister after all character devices are closed */ 2176f97da128SHans Petter Selasky usb_bus_port_set_device(bus, udev->parent_hub ? 2177f97da128SHans Petter Selasky udev->parent_hub->hub->ports + udev->port_index : NULL, 2178f97da128SHans Petter Selasky NULL, USB_ROOT_HUB_ADDR); 2179f97da128SHans Petter Selasky 218002ac6454SAndrew Thompson /* unsetup any leftover default USB transfers */ 21815b3bb704SAndrew Thompson usbd_transfer_unsetup(udev->ctrl_xfer, USB_CTRL_XFER_MAX); 218202ac6454SAndrew Thompson 218302ac6454SAndrew Thompson /* template unsetup, if any */ 2184a593f6b8SAndrew Thompson (usb_temp_unsetup_p) (udev); 218502ac6454SAndrew Thompson 218602ac6454SAndrew Thompson /* 218702ac6454SAndrew Thompson * Make sure that our clear-stall messages are not queued 218802ac6454SAndrew Thompson * anywhere: 218902ac6454SAndrew Thompson */ 219002ac6454SAndrew Thompson USB_BUS_LOCK(udev->bus); 219143ea03d7SHans Petter Selasky usb_proc_mwait(USB_BUS_CS_PROC(udev->bus), 219202ac6454SAndrew Thompson &udev->cs_msg[0], &udev->cs_msg[1]); 219302ac6454SAndrew Thompson USB_BUS_UNLOCK(udev->bus); 219402ac6454SAndrew Thompson 2195b78e84d1SHans Petter Selasky /* wait for all references to go away */ 2196b78e84d1SHans Petter Selasky usb_wait_pending_refs(udev); 2197b78e84d1SHans Petter Selasky 219891cd9240SAndrew Thompson sx_destroy(&udev->enum_sx); 21992df1e9a6SAndrew Thompson sx_destroy(&udev->sr_sx); 220064cb5e2aSHans Petter Selasky sx_destroy(&udev->ctrl_sx); 220102ac6454SAndrew Thompson 220291cd9240SAndrew Thompson cv_destroy(&udev->ctrlreq_cv); 220391cd9240SAndrew Thompson cv_destroy(&udev->ref_cv); 220402ac6454SAndrew Thompson 220591cd9240SAndrew Thompson mtx_destroy(&udev->device_mtx); 22068755859aSAndrew Thompson #if USB_HAVE_UGEN 2207ee3e3ff5SAndrew Thompson KASSERT(LIST_FIRST(&udev->pd_list) == NULL, ("leaked cdev entries")); 22088755859aSAndrew Thompson #endif 220902ac6454SAndrew Thompson 2210963169b4SHans Petter Selasky /* Uninitialise device */ 2211963169b4SHans Petter Selasky if (bus->methods->device_uninit != NULL) 2212963169b4SHans Petter Selasky (bus->methods->device_uninit) (udev); 2213963169b4SHans Petter Selasky 221402ac6454SAndrew Thompson /* free device */ 221528212de2SAndrew Thompson free(udev->serial, M_USB); 221628212de2SAndrew Thompson free(udev->manufacturer, M_USB); 221728212de2SAndrew Thompson free(udev->product, M_USB); 221802ac6454SAndrew Thompson free(udev, M_USB); 221902ac6454SAndrew Thompson } 222002ac6454SAndrew Thompson 222102ac6454SAndrew Thompson /*------------------------------------------------------------------------* 2222a593f6b8SAndrew Thompson * usbd_get_iface 222302ac6454SAndrew Thompson * 222402ac6454SAndrew Thompson * This function is the safe way to get the USB interface structure 222502ac6454SAndrew Thompson * pointer by interface index. 222602ac6454SAndrew Thompson * 222702ac6454SAndrew Thompson * Return values: 222802ac6454SAndrew Thompson * NULL: Interface not present. 222902ac6454SAndrew Thompson * Else: Pointer to USB interface structure. 223002ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 2231760bc48eSAndrew Thompson struct usb_interface * 2232a593f6b8SAndrew Thompson usbd_get_iface(struct usb_device *udev, uint8_t iface_index) 223302ac6454SAndrew Thompson { 2234760bc48eSAndrew Thompson struct usb_interface *iface = udev->ifaces + iface_index; 223502ac6454SAndrew Thompson 2236bdd41206SAndrew Thompson if (iface_index >= udev->ifaces_max) 223702ac6454SAndrew Thompson return (NULL); 223802ac6454SAndrew Thompson return (iface); 223902ac6454SAndrew Thompson } 224002ac6454SAndrew Thompson 224102ac6454SAndrew Thompson /*------------------------------------------------------------------------* 22423c8d24f4SAndrew Thompson * usbd_find_descriptor 224302ac6454SAndrew Thompson * 224402ac6454SAndrew Thompson * This function will lookup the first descriptor that matches the 224502ac6454SAndrew Thompson * criteria given by the arguments "type" and "subtype". Descriptors 224602ac6454SAndrew Thompson * will only be searched within the interface having the index 224702ac6454SAndrew Thompson * "iface_index". If the "id" argument points to an USB descriptor, 224802ac6454SAndrew Thompson * it will be skipped before the search is started. This allows 224902ac6454SAndrew Thompson * searching for multiple descriptors using the same criteria. Else 225002ac6454SAndrew Thompson * the search is started after the interface descriptor. 225102ac6454SAndrew Thompson * 225202ac6454SAndrew Thompson * Return values: 225302ac6454SAndrew Thompson * NULL: End of descriptors 225402ac6454SAndrew Thompson * Else: A descriptor matching the criteria 225502ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 225602ac6454SAndrew Thompson void * 22573c8d24f4SAndrew Thompson usbd_find_descriptor(struct usb_device *udev, void *id, uint8_t iface_index, 225802ac6454SAndrew Thompson uint8_t type, uint8_t type_mask, 225902ac6454SAndrew Thompson uint8_t subtype, uint8_t subtype_mask) 226002ac6454SAndrew Thompson { 2261760bc48eSAndrew Thompson struct usb_descriptor *desc; 2262760bc48eSAndrew Thompson struct usb_config_descriptor *cd; 2263760bc48eSAndrew Thompson struct usb_interface *iface; 226402ac6454SAndrew Thompson 2265a593f6b8SAndrew Thompson cd = usbd_get_config_descriptor(udev); 226602ac6454SAndrew Thompson if (cd == NULL) { 226702ac6454SAndrew Thompson return (NULL); 226802ac6454SAndrew Thompson } 226902ac6454SAndrew Thompson if (id == NULL) { 2270a593f6b8SAndrew Thompson iface = usbd_get_iface(udev, iface_index); 227102ac6454SAndrew Thompson if (iface == NULL) { 227202ac6454SAndrew Thompson return (NULL); 227302ac6454SAndrew Thompson } 2274a593f6b8SAndrew Thompson id = usbd_get_interface_descriptor(iface); 227502ac6454SAndrew Thompson if (id == NULL) { 227602ac6454SAndrew Thompson return (NULL); 227702ac6454SAndrew Thompson } 227802ac6454SAndrew Thompson } 227902ac6454SAndrew Thompson desc = (void *)id; 228002ac6454SAndrew Thompson 2281a593f6b8SAndrew Thompson while ((desc = usb_desc_foreach(cd, desc))) { 228202ac6454SAndrew Thompson 228302ac6454SAndrew Thompson if (desc->bDescriptorType == UDESC_INTERFACE) { 228402ac6454SAndrew Thompson break; 228502ac6454SAndrew Thompson } 228602ac6454SAndrew Thompson if (((desc->bDescriptorType & type_mask) == type) && 228702ac6454SAndrew Thompson ((desc->bDescriptorSubtype & subtype_mask) == subtype)) { 228802ac6454SAndrew Thompson return (desc); 228902ac6454SAndrew Thompson } 229002ac6454SAndrew Thompson } 229102ac6454SAndrew Thompson return (NULL); 229202ac6454SAndrew Thompson } 229302ac6454SAndrew Thompson 229402ac6454SAndrew Thompson /*------------------------------------------------------------------------* 2295a593f6b8SAndrew Thompson * usb_devinfo 229602ac6454SAndrew Thompson * 229702ac6454SAndrew Thompson * This function will dump information from the device descriptor 229802ac6454SAndrew Thompson * belonging to the USB device pointed to by "udev", to the string 229902ac6454SAndrew Thompson * pointed to by "dst_ptr" having a maximum length of "dst_len" bytes 230002ac6454SAndrew Thompson * including the terminating zero. 230102ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 230202ac6454SAndrew Thompson void 2303a593f6b8SAndrew Thompson usb_devinfo(struct usb_device *udev, char *dst_ptr, uint16_t dst_len) 230402ac6454SAndrew Thompson { 2305760bc48eSAndrew Thompson struct usb_device_descriptor *udd = &udev->ddesc; 230602ac6454SAndrew Thompson uint16_t bcdDevice; 230702ac6454SAndrew Thompson uint16_t bcdUSB; 230802ac6454SAndrew Thompson 230902ac6454SAndrew Thompson bcdUSB = UGETW(udd->bcdUSB); 231002ac6454SAndrew Thompson bcdDevice = UGETW(udd->bcdDevice); 231102ac6454SAndrew Thompson 231202ac6454SAndrew Thompson if (udd->bDeviceClass != 0xFF) { 231302ac6454SAndrew Thompson snprintf(dst_ptr, dst_len, "%s %s, class %d/%d, rev %x.%02x/" 23148755859aSAndrew Thompson "%x.%02x, addr %d", 2315ae538d85SAndrew Thompson usb_get_manufacturer(udev), 2316ae538d85SAndrew Thompson usb_get_product(udev), 231702ac6454SAndrew Thompson udd->bDeviceClass, udd->bDeviceSubClass, 231802ac6454SAndrew Thompson (bcdUSB >> 8), bcdUSB & 0xFF, 231902ac6454SAndrew Thompson (bcdDevice >> 8), bcdDevice & 0xFF, 232002ac6454SAndrew Thompson udev->address); 232102ac6454SAndrew Thompson } else { 232202ac6454SAndrew Thompson snprintf(dst_ptr, dst_len, "%s %s, rev %x.%02x/" 23238755859aSAndrew Thompson "%x.%02x, addr %d", 2324ae538d85SAndrew Thompson usb_get_manufacturer(udev), 2325ae538d85SAndrew Thompson usb_get_product(udev), 232602ac6454SAndrew Thompson (bcdUSB >> 8), bcdUSB & 0xFF, 232702ac6454SAndrew Thompson (bcdDevice >> 8), bcdDevice & 0xFF, 232802ac6454SAndrew Thompson udev->address); 232902ac6454SAndrew Thompson } 233002ac6454SAndrew Thompson } 233102ac6454SAndrew Thompson 2332ed6d949aSAndrew Thompson #ifdef USB_VERBOSE 233302ac6454SAndrew Thompson /* 233402ac6454SAndrew Thompson * Descriptions of of known vendors and devices ("products"). 233502ac6454SAndrew Thompson */ 233602ac6454SAndrew Thompson struct usb_knowndev { 233702ac6454SAndrew Thompson uint16_t vendor; 233802ac6454SAndrew Thompson uint16_t product; 233902ac6454SAndrew Thompson uint32_t flags; 234002ac6454SAndrew Thompson const char *vendorname; 234102ac6454SAndrew Thompson const char *productname; 234202ac6454SAndrew Thompson }; 234302ac6454SAndrew Thompson 234402ac6454SAndrew Thompson #define USB_KNOWNDEV_NOPROD 0x01 /* match on vendor only */ 234502ac6454SAndrew Thompson 234602ac6454SAndrew Thompson #include "usbdevs.h" 234702ac6454SAndrew Thompson #include "usbdevs_data.h" 234802ac6454SAndrew Thompson #endif /* USB_VERBOSE */ 234902ac6454SAndrew Thompson 235002ac6454SAndrew Thompson static void 2351a593f6b8SAndrew Thompson usbd_set_device_strings(struct usb_device *udev) 235202ac6454SAndrew Thompson { 2353760bc48eSAndrew Thompson struct usb_device_descriptor *udd = &udev->ddesc; 2354ed6d949aSAndrew Thompson #ifdef USB_VERBOSE 235502ac6454SAndrew Thompson const struct usb_knowndev *kdp; 235602ac6454SAndrew Thompson #endif 23572df1e9a6SAndrew Thompson char *temp_ptr; 23587a918edfSAndrew Thompson size_t temp_size; 235902ac6454SAndrew Thompson uint16_t vendor_id; 236002ac6454SAndrew Thompson uint16_t product_id; 23616950c75fSHans Petter Selasky uint8_t do_unlock; 236202ac6454SAndrew Thompson 23636950c75fSHans Petter Selasky /* Protect scratch area */ 236464cb5e2aSHans Petter Selasky do_unlock = usbd_ctrl_lock(udev); 23656950c75fSHans Petter Selasky 23666950c75fSHans Petter Selasky temp_ptr = (char *)udev->scratch.data; 23676950c75fSHans Petter Selasky temp_size = sizeof(udev->scratch.data); 23687a918edfSAndrew Thompson 236902ac6454SAndrew Thompson vendor_id = UGETW(udd->idVendor); 237002ac6454SAndrew Thompson product_id = UGETW(udd->idProduct); 237102ac6454SAndrew Thompson 2372c86aead6SAndrew Thompson /* get serial number string */ 23737a918edfSAndrew Thompson usbd_req_get_string_any(udev, NULL, temp_ptr, temp_size, 2374c86aead6SAndrew Thompson udev->ddesc.iSerialNumber); 23757a918edfSAndrew Thompson udev->serial = strdup(temp_ptr, M_USB); 237602ac6454SAndrew Thompson 2377c86aead6SAndrew Thompson /* get manufacturer string */ 23787a918edfSAndrew Thompson usbd_req_get_string_any(udev, NULL, temp_ptr, temp_size, 2379c86aead6SAndrew Thompson udev->ddesc.iManufacturer); 23807a918edfSAndrew Thompson usb_trim_spaces(temp_ptr); 23817a918edfSAndrew Thompson if (temp_ptr[0] != '\0') 23827a918edfSAndrew Thompson udev->manufacturer = strdup(temp_ptr, M_USB); 2383c86aead6SAndrew Thompson 2384c86aead6SAndrew Thompson /* get product string */ 23857a918edfSAndrew Thompson usbd_req_get_string_any(udev, NULL, temp_ptr, temp_size, 2386c86aead6SAndrew Thompson udev->ddesc.iProduct); 23877a918edfSAndrew Thompson usb_trim_spaces(temp_ptr); 23887a918edfSAndrew Thompson if (temp_ptr[0] != '\0') 23897a918edfSAndrew Thompson udev->product = strdup(temp_ptr, M_USB); 2390c86aead6SAndrew Thompson 2391ed6d949aSAndrew Thompson #ifdef USB_VERBOSE 2392c86aead6SAndrew Thompson if (udev->manufacturer == NULL || udev->product == NULL) { 2393c86aead6SAndrew Thompson for (kdp = usb_knowndevs; kdp->vendorname != NULL; kdp++) { 239402ac6454SAndrew Thompson if (kdp->vendor == vendor_id && 239502ac6454SAndrew Thompson (kdp->product == product_id || 239602ac6454SAndrew Thompson (kdp->flags & USB_KNOWNDEV_NOPROD) != 0)) 239702ac6454SAndrew Thompson break; 239802ac6454SAndrew Thompson } 239902ac6454SAndrew Thompson if (kdp->vendorname != NULL) { 2400c86aead6SAndrew Thompson /* XXX should use pointer to knowndevs string */ 2401c86aead6SAndrew Thompson if (udev->manufacturer == NULL) { 2402c86aead6SAndrew Thompson udev->manufacturer = strdup(kdp->vendorname, 2403c86aead6SAndrew Thompson M_USB); 2404c86aead6SAndrew Thompson } 2405c86aead6SAndrew Thompson if (udev->product == NULL && 2406c86aead6SAndrew Thompson (kdp->flags & USB_KNOWNDEV_NOPROD) == 0) { 2407c86aead6SAndrew Thompson udev->product = strdup(kdp->productname, 2408c86aead6SAndrew Thompson M_USB); 2409c86aead6SAndrew Thompson } 241002ac6454SAndrew Thompson } 241102ac6454SAndrew Thompson } 241202ac6454SAndrew Thompson #endif 2413c86aead6SAndrew Thompson /* Provide default strings if none were found */ 2414c86aead6SAndrew Thompson if (udev->manufacturer == NULL) { 24157a918edfSAndrew Thompson snprintf(temp_ptr, temp_size, "vendor 0x%04x", vendor_id); 24167a918edfSAndrew Thompson udev->manufacturer = strdup(temp_ptr, M_USB); 241702ac6454SAndrew Thompson } 2418c86aead6SAndrew Thompson if (udev->product == NULL) { 24197a918edfSAndrew Thompson snprintf(temp_ptr, temp_size, "product 0x%04x", product_id); 24207a918edfSAndrew Thompson udev->product = strdup(temp_ptr, M_USB); 242102ac6454SAndrew Thompson } 24226950c75fSHans Petter Selasky 24236950c75fSHans Petter Selasky if (do_unlock) 242464cb5e2aSHans Petter Selasky usbd_ctrl_unlock(udev); 242502ac6454SAndrew Thompson } 242602ac6454SAndrew Thompson 242702ac6454SAndrew Thompson /* 242802ac6454SAndrew Thompson * Returns: 242902ac6454SAndrew Thompson * See: USB_MODE_XXX 243002ac6454SAndrew Thompson */ 2431f29a0724SAndrew Thompson enum usb_hc_mode 2432a593f6b8SAndrew Thompson usbd_get_mode(struct usb_device *udev) 243302ac6454SAndrew Thompson { 2434f29a0724SAndrew Thompson return (udev->flags.usb_mode); 243502ac6454SAndrew Thompson } 243602ac6454SAndrew Thompson 243702ac6454SAndrew Thompson /* 243802ac6454SAndrew Thompson * Returns: 243902ac6454SAndrew Thompson * See: USB_SPEED_XXX 244002ac6454SAndrew Thompson */ 24418d2dd5ddSAndrew Thompson enum usb_dev_speed 2442a593f6b8SAndrew Thompson usbd_get_speed(struct usb_device *udev) 244302ac6454SAndrew Thompson { 244402ac6454SAndrew Thompson return (udev->speed); 244502ac6454SAndrew Thompson } 244602ac6454SAndrew Thompson 244702ac6454SAndrew Thompson uint32_t 2448a593f6b8SAndrew Thompson usbd_get_isoc_fps(struct usb_device *udev) 244902ac6454SAndrew Thompson { 245002ac6454SAndrew Thompson ; /* indent fix */ 245102ac6454SAndrew Thompson switch (udev->speed) { 245202ac6454SAndrew Thompson case USB_SPEED_LOW: 245302ac6454SAndrew Thompson case USB_SPEED_FULL: 245402ac6454SAndrew Thompson return (1000); 245502ac6454SAndrew Thompson default: 245602ac6454SAndrew Thompson return (8000); 245702ac6454SAndrew Thompson } 245802ac6454SAndrew Thompson } 245902ac6454SAndrew Thompson 2460760bc48eSAndrew Thompson struct usb_device_descriptor * 2461a593f6b8SAndrew Thompson usbd_get_device_descriptor(struct usb_device *udev) 246202ac6454SAndrew Thompson { 246302ac6454SAndrew Thompson if (udev == NULL) 246402ac6454SAndrew Thompson return (NULL); /* be NULL safe */ 246502ac6454SAndrew Thompson return (&udev->ddesc); 246602ac6454SAndrew Thompson } 246702ac6454SAndrew Thompson 2468760bc48eSAndrew Thompson struct usb_config_descriptor * 2469a593f6b8SAndrew Thompson usbd_get_config_descriptor(struct usb_device *udev) 247002ac6454SAndrew Thompson { 247102ac6454SAndrew Thompson if (udev == NULL) 247202ac6454SAndrew Thompson return (NULL); /* be NULL safe */ 247302ac6454SAndrew Thompson return (udev->cdesc); 247402ac6454SAndrew Thompson } 247502ac6454SAndrew Thompson 247602ac6454SAndrew Thompson /*------------------------------------------------------------------------* 2477a593f6b8SAndrew Thompson * usb_test_quirk - test a device for a given quirk 247802ac6454SAndrew Thompson * 247902ac6454SAndrew Thompson * Return values: 248002ac6454SAndrew Thompson * 0: The USB device does not have the given quirk. 248102ac6454SAndrew Thompson * Else: The USB device has the given quirk. 248202ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 248302ac6454SAndrew Thompson uint8_t 2484a593f6b8SAndrew Thompson usb_test_quirk(const struct usb_attach_arg *uaa, uint16_t quirk) 248502ac6454SAndrew Thompson { 248602ac6454SAndrew Thompson uint8_t found; 24873f37fb62SHans Petter Selasky uint8_t x; 24883f37fb62SHans Petter Selasky 24893f37fb62SHans Petter Selasky if (quirk == UQ_NONE) 24903f37fb62SHans Petter Selasky return (0); 24913f37fb62SHans Petter Selasky 24923f37fb62SHans Petter Selasky /* search the automatic per device quirks first */ 24933f37fb62SHans Petter Selasky 24943f37fb62SHans Petter Selasky for (x = 0; x != USB_MAX_AUTO_QUIRK; x++) { 24953f37fb62SHans Petter Selasky if (uaa->device->autoQuirk[x] == quirk) 24963f37fb62SHans Petter Selasky return (1); 24973f37fb62SHans Petter Selasky } 24983f37fb62SHans Petter Selasky 24993f37fb62SHans Petter Selasky /* search global quirk table, if any */ 250002ac6454SAndrew Thompson 2501a593f6b8SAndrew Thompson found = (usb_test_quirk_p) (&uaa->info, quirk); 25023f37fb62SHans Petter Selasky 250302ac6454SAndrew Thompson return (found); 250402ac6454SAndrew Thompson } 250502ac6454SAndrew Thompson 2506760bc48eSAndrew Thompson struct usb_interface_descriptor * 2507a593f6b8SAndrew Thompson usbd_get_interface_descriptor(struct usb_interface *iface) 250802ac6454SAndrew Thompson { 250902ac6454SAndrew Thompson if (iface == NULL) 251002ac6454SAndrew Thompson return (NULL); /* be NULL safe */ 251102ac6454SAndrew Thompson return (iface->idesc); 251202ac6454SAndrew Thompson } 251302ac6454SAndrew Thompson 251402ac6454SAndrew Thompson uint8_t 2515a593f6b8SAndrew Thompson usbd_get_interface_altindex(struct usb_interface *iface) 251602ac6454SAndrew Thompson { 251702ac6454SAndrew Thompson return (iface->alt_index); 251802ac6454SAndrew Thompson } 251902ac6454SAndrew Thompson 252002ac6454SAndrew Thompson uint8_t 2521a593f6b8SAndrew Thompson usbd_get_bus_index(struct usb_device *udev) 252202ac6454SAndrew Thompson { 252302ac6454SAndrew Thompson return ((uint8_t)device_get_unit(udev->bus->bdev)); 252402ac6454SAndrew Thompson } 252502ac6454SAndrew Thompson 252602ac6454SAndrew Thompson uint8_t 2527a593f6b8SAndrew Thompson usbd_get_device_index(struct usb_device *udev) 252802ac6454SAndrew Thompson { 252902ac6454SAndrew Thompson return (udev->device_index); 253002ac6454SAndrew Thompson } 253102ac6454SAndrew Thompson 25328bb77249SHans Petter Selasky #if USB_HAVE_DEVCTL 25333acd904dSAndrew Thompson static void 25343acd904dSAndrew Thompson usb_notify_addq(const char *type, struct usb_device *udev) 25353acd904dSAndrew Thompson { 25363acd904dSAndrew Thompson struct usb_interface *iface; 25373acd904dSAndrew Thompson struct sbuf *sb; 25383acd904dSAndrew Thompson int i; 25393acd904dSAndrew Thompson 25403acd904dSAndrew Thompson /* announce the device */ 25413acd904dSAndrew Thompson sb = sbuf_new_auto(); 25423acd904dSAndrew Thompson sbuf_printf(sb, 25438bb77249SHans Petter Selasky #if USB_HAVE_UGEN 25448bb77249SHans Petter Selasky "ugen=%s " 254502c40241SHans Petter Selasky "cdev=%s " 25468bb77249SHans Petter Selasky #endif 25473acd904dSAndrew Thompson "vendor=0x%04x " 25483acd904dSAndrew Thompson "product=0x%04x " 25493acd904dSAndrew Thompson "devclass=0x%02x " 25503acd904dSAndrew Thompson "devsubclass=0x%02x " 25513acd904dSAndrew Thompson "sernum=\"%s\" " 25523acd904dSAndrew Thompson "release=0x%04x " 255366390216SAndrew Thompson "mode=%s " 25543acd904dSAndrew Thompson "port=%u " 25558bb77249SHans Petter Selasky #if USB_HAVE_UGEN 25566df3eebbSNick Hibma "parent=%s" 25578bb77249SHans Petter Selasky #endif 25588bb77249SHans Petter Selasky "", 25598bb77249SHans Petter Selasky #if USB_HAVE_UGEN 25603acd904dSAndrew Thompson udev->ugen_name, 256102c40241SHans Petter Selasky udev->ugen_name, 25628bb77249SHans Petter Selasky #endif 25633acd904dSAndrew Thompson UGETW(udev->ddesc.idVendor), 25643acd904dSAndrew Thompson UGETW(udev->ddesc.idProduct), 25653acd904dSAndrew Thompson udev->ddesc.bDeviceClass, 25663acd904dSAndrew Thompson udev->ddesc.bDeviceSubClass, 2567ae538d85SAndrew Thompson usb_get_serial(udev), 25683acd904dSAndrew Thompson UGETW(udev->ddesc.bcdDevice), 256966390216SAndrew Thompson (udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device", 25708bb77249SHans Petter Selasky udev->port_no 25718bb77249SHans Petter Selasky #if USB_HAVE_UGEN 25728bb77249SHans Petter Selasky , udev->parent_hub != NULL ? 25733acd904dSAndrew Thompson udev->parent_hub->ugen_name : 25748bb77249SHans Petter Selasky device_get_nameunit(device_get_parent(udev->bus->bdev)) 25758bb77249SHans Petter Selasky #endif 25768bb77249SHans Petter Selasky ); 25773acd904dSAndrew Thompson sbuf_finish(sb); 25783acd904dSAndrew Thompson devctl_notify("USB", "DEVICE", type, sbuf_data(sb)); 25793acd904dSAndrew Thompson sbuf_delete(sb); 25803acd904dSAndrew Thompson 25813acd904dSAndrew Thompson /* announce each interface */ 25823acd904dSAndrew Thompson for (i = 0; i < USB_IFACE_MAX; i++) { 25833acd904dSAndrew Thompson iface = usbd_get_iface(udev, i); 25843acd904dSAndrew Thompson if (iface == NULL) 25853acd904dSAndrew Thompson break; /* end of interfaces */ 25863acd904dSAndrew Thompson if (iface->idesc == NULL) 25873acd904dSAndrew Thompson continue; /* no interface descriptor */ 25883acd904dSAndrew Thompson 25893acd904dSAndrew Thompson sb = sbuf_new_auto(); 25903acd904dSAndrew Thompson sbuf_printf(sb, 25918bb77249SHans Petter Selasky #if USB_HAVE_UGEN 25928bb77249SHans Petter Selasky "ugen=%s " 259302c40241SHans Petter Selasky "cdev=%s " 25948bb77249SHans Petter Selasky #endif 25953acd904dSAndrew Thompson "vendor=0x%04x " 25963acd904dSAndrew Thompson "product=0x%04x " 25973acd904dSAndrew Thompson "devclass=0x%02x " 25983acd904dSAndrew Thompson "devsubclass=0x%02x " 25993acd904dSAndrew Thompson "sernum=\"%s\" " 26003acd904dSAndrew Thompson "release=0x%04x " 260166390216SAndrew Thompson "mode=%s " 26023acd904dSAndrew Thompson "interface=%d " 26033acd904dSAndrew Thompson "endpoints=%d " 26043acd904dSAndrew Thompson "intclass=0x%02x " 26053acd904dSAndrew Thompson "intsubclass=0x%02x " 26066df3eebbSNick Hibma "intprotocol=0x%02x", 26078bb77249SHans Petter Selasky #if USB_HAVE_UGEN 26083acd904dSAndrew Thompson udev->ugen_name, 260902c40241SHans Petter Selasky udev->ugen_name, 26108bb77249SHans Petter Selasky #endif 26113acd904dSAndrew Thompson UGETW(udev->ddesc.idVendor), 26123acd904dSAndrew Thompson UGETW(udev->ddesc.idProduct), 26133acd904dSAndrew Thompson udev->ddesc.bDeviceClass, 26143acd904dSAndrew Thompson udev->ddesc.bDeviceSubClass, 2615ae538d85SAndrew Thompson usb_get_serial(udev), 26163acd904dSAndrew Thompson UGETW(udev->ddesc.bcdDevice), 261766390216SAndrew Thompson (udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device", 26183acd904dSAndrew Thompson iface->idesc->bInterfaceNumber, 26193acd904dSAndrew Thompson iface->idesc->bNumEndpoints, 26203acd904dSAndrew Thompson iface->idesc->bInterfaceClass, 26213acd904dSAndrew Thompson iface->idesc->bInterfaceSubClass, 26223acd904dSAndrew Thompson iface->idesc->bInterfaceProtocol); 26233acd904dSAndrew Thompson sbuf_finish(sb); 26243acd904dSAndrew Thompson devctl_notify("USB", "INTERFACE", type, sbuf_data(sb)); 26253acd904dSAndrew Thompson sbuf_delete(sb); 26263acd904dSAndrew Thompson } 26273acd904dSAndrew Thompson } 26288bb77249SHans Petter Selasky #endif 262902ac6454SAndrew Thompson 26308bb77249SHans Petter Selasky #if USB_HAVE_UGEN 263102ac6454SAndrew Thompson /*------------------------------------------------------------------------* 2632a593f6b8SAndrew Thompson * usb_fifo_free_wrap 263302ac6454SAndrew Thompson * 263402ac6454SAndrew Thompson * This function will free the FIFOs. 263502ac6454SAndrew Thompson * 2636bdd41206SAndrew Thompson * Description of "flag" argument: If the USB_UNCFG_FLAG_FREE_EP0 flag 2637bdd41206SAndrew Thompson * is set and "iface_index" is set to "USB_IFACE_INDEX_ANY", we free 2638bdd41206SAndrew Thompson * all FIFOs. If the USB_UNCFG_FLAG_FREE_EP0 flag is not set and 2639bdd41206SAndrew Thompson * "iface_index" is set to "USB_IFACE_INDEX_ANY", we free all non 2640bdd41206SAndrew Thompson * control endpoint FIFOs. If "iface_index" is not set to 2641bdd41206SAndrew Thompson * "USB_IFACE_INDEX_ANY" the flag has no effect. 264202ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 264302ac6454SAndrew Thompson static void 2644a593f6b8SAndrew Thompson usb_fifo_free_wrap(struct usb_device *udev, 264502ac6454SAndrew Thompson uint8_t iface_index, uint8_t flag) 264602ac6454SAndrew Thompson { 2647760bc48eSAndrew Thompson struct usb_fifo *f; 264802ac6454SAndrew Thompson uint16_t i; 264902ac6454SAndrew Thompson 265002ac6454SAndrew Thompson /* 265102ac6454SAndrew Thompson * Free any USB FIFOs on the given interface: 265202ac6454SAndrew Thompson */ 265302ac6454SAndrew Thompson for (i = 0; i != USB_FIFO_MAX; i++) { 265402ac6454SAndrew Thompson f = udev->fifo[i]; 265502ac6454SAndrew Thompson if (f == NULL) { 265602ac6454SAndrew Thompson continue; 265702ac6454SAndrew Thompson } 265802ac6454SAndrew Thompson /* Check if the interface index matches */ 265902ac6454SAndrew Thompson if (iface_index == f->iface_index) { 2660a593f6b8SAndrew Thompson if (f->methods != &usb_ugen_methods) { 266102ac6454SAndrew Thompson /* 266202ac6454SAndrew Thompson * Don't free any non-generic FIFOs in 266302ac6454SAndrew Thompson * this case. 266402ac6454SAndrew Thompson */ 266502ac6454SAndrew Thompson continue; 266602ac6454SAndrew Thompson } 266702ac6454SAndrew Thompson if ((f->dev_ep_index == 0) && 266802ac6454SAndrew Thompson (f->fs_xfer == NULL)) { 266902ac6454SAndrew Thompson /* no need to free this FIFO */ 267002ac6454SAndrew Thompson continue; 267102ac6454SAndrew Thompson } 267202ac6454SAndrew Thompson } else if (iface_index == USB_IFACE_INDEX_ANY) { 2673a593f6b8SAndrew Thompson if ((f->methods == &usb_ugen_methods) && 2674bdd41206SAndrew Thompson (f->dev_ep_index == 0) && 2675bdd41206SAndrew Thompson (!(flag & USB_UNCFG_FLAG_FREE_EP0)) && 267602ac6454SAndrew Thompson (f->fs_xfer == NULL)) { 267702ac6454SAndrew Thompson /* no need to free this FIFO */ 267802ac6454SAndrew Thompson continue; 267902ac6454SAndrew Thompson } 268002ac6454SAndrew Thompson } else { 268102ac6454SAndrew Thompson /* no need to free this FIFO */ 268202ac6454SAndrew Thompson continue; 268302ac6454SAndrew Thompson } 268402ac6454SAndrew Thompson /* free this FIFO */ 2685a593f6b8SAndrew Thompson usb_fifo_free(f); 268602ac6454SAndrew Thompson } 268702ac6454SAndrew Thompson } 26880f6e8f93SAndrew Thompson #endif 268902ac6454SAndrew Thompson 269002ac6454SAndrew Thompson /*------------------------------------------------------------------------* 2691a593f6b8SAndrew Thompson * usb_peer_can_wakeup 269202ac6454SAndrew Thompson * 269302ac6454SAndrew Thompson * Return values: 269402ac6454SAndrew Thompson * 0: Peer cannot do resume signalling. 269502ac6454SAndrew Thompson * Else: Peer can do resume signalling. 269602ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 269702ac6454SAndrew Thompson uint8_t 2698a593f6b8SAndrew Thompson usb_peer_can_wakeup(struct usb_device *udev) 269902ac6454SAndrew Thompson { 2700760bc48eSAndrew Thompson const struct usb_config_descriptor *cdp; 270102ac6454SAndrew Thompson 270202ac6454SAndrew Thompson cdp = udev->cdesc; 2703f29a0724SAndrew Thompson if ((cdp != NULL) && (udev->flags.usb_mode == USB_MODE_HOST)) { 270402ac6454SAndrew Thompson return (cdp->bmAttributes & UC_REMOTE_WAKEUP); 270502ac6454SAndrew Thompson } 270602ac6454SAndrew Thompson return (0); /* not supported */ 270702ac6454SAndrew Thompson } 2708bd216778SAndrew Thompson 2709bd216778SAndrew Thompson void 2710a593f6b8SAndrew Thompson usb_set_device_state(struct usb_device *udev, enum usb_dev_state state) 2711bd216778SAndrew Thompson { 2712bd216778SAndrew Thompson 2713bd216778SAndrew Thompson KASSERT(state < USB_STATE_MAX, ("invalid udev state")); 2714bd216778SAndrew Thompson 2715bd216778SAndrew Thompson DPRINTF("udev %p state %s -> %s\n", udev, 2716a593f6b8SAndrew Thompson usb_statestr(udev->state), usb_statestr(state)); 2717963169b4SHans Petter Selasky 2718f97da128SHans Petter Selasky #if USB_HAVE_UGEN 2719f97da128SHans Petter Selasky mtx_lock(&usb_ref_lock); 2720f97da128SHans Petter Selasky #endif 2721f97da128SHans Petter Selasky udev->state = state; 2722f97da128SHans Petter Selasky #if USB_HAVE_UGEN 2723f97da128SHans Petter Selasky mtx_unlock(&usb_ref_lock); 2724f97da128SHans Petter Selasky #endif 2725963169b4SHans Petter Selasky if (udev->bus->methods->device_state_change != NULL) 2726963169b4SHans Petter Selasky (udev->bus->methods->device_state_change) (udev); 2727963169b4SHans Petter Selasky } 2728963169b4SHans Petter Selasky 2729963169b4SHans Petter Selasky enum usb_dev_state 2730963169b4SHans Petter Selasky usb_get_device_state(struct usb_device *udev) 2731963169b4SHans Petter Selasky { 2732963169b4SHans Petter Selasky if (udev == NULL) 2733963169b4SHans Petter Selasky return (USB_STATE_DETACHED); 2734963169b4SHans Petter Selasky return (udev->state); 2735bd216778SAndrew Thompson } 2736bd216778SAndrew Thompson 2737ec8f3127SAndrew Thompson uint8_t 2738a593f6b8SAndrew Thompson usbd_device_attached(struct usb_device *udev) 2739bd216778SAndrew Thompson { 2740bd216778SAndrew Thompson return (udev->state > USB_STATE_DETACHED); 2741bd216778SAndrew Thompson } 2742cb18f7d1SAlfred Perlstein 27436950c75fSHans Petter Selasky /* 27446950c75fSHans Petter Selasky * The following function locks enumerating the given USB device. If 27456950c75fSHans Petter Selasky * the lock is already grabbed this function returns zero. Else a 274610aab8b6SHans Petter Selasky * a value of one is returned. 27476950c75fSHans Petter Selasky */ 27486950c75fSHans Petter Selasky uint8_t 2749cb18f7d1SAlfred Perlstein usbd_enum_lock(struct usb_device *udev) 2750cb18f7d1SAlfred Perlstein { 27516950c75fSHans Petter Selasky if (sx_xlocked(&udev->enum_sx)) 27526950c75fSHans Petter Selasky return (0); 27536950c75fSHans Petter Selasky 275491cd9240SAndrew Thompson sx_xlock(&udev->enum_sx); 27552df1e9a6SAndrew Thompson sx_xlock(&udev->sr_sx); 2756cb18f7d1SAlfred Perlstein /* 2757cb18f7d1SAlfred Perlstein * NEWBUS LOCK NOTE: We should check if any parent SX locks 2758cb18f7d1SAlfred Perlstein * are locked before locking Giant. Else the lock can be 2759cb18f7d1SAlfred Perlstein * locked multiple times. 2760cb18f7d1SAlfred Perlstein */ 2761cb18f7d1SAlfred Perlstein mtx_lock(&Giant); 27626950c75fSHans Petter Selasky return (1); 2763cb18f7d1SAlfred Perlstein } 2764cb18f7d1SAlfred Perlstein 276510aab8b6SHans Petter Selasky #if USB_HAVE_UGEN 276610aab8b6SHans Petter Selasky /* 276710aab8b6SHans Petter Selasky * This function is the same like usbd_enum_lock() except a value of 276810aab8b6SHans Petter Selasky * 255 is returned when a signal is pending: 276910aab8b6SHans Petter Selasky */ 277010aab8b6SHans Petter Selasky uint8_t 277110aab8b6SHans Petter Selasky usbd_enum_lock_sig(struct usb_device *udev) 277210aab8b6SHans Petter Selasky { 277310aab8b6SHans Petter Selasky if (sx_xlocked(&udev->enum_sx)) 277410aab8b6SHans Petter Selasky return (0); 277510aab8b6SHans Petter Selasky if (sx_xlock_sig(&udev->enum_sx)) 277610aab8b6SHans Petter Selasky return (255); 277710aab8b6SHans Petter Selasky if (sx_xlock_sig(&udev->sr_sx)) { 277810aab8b6SHans Petter Selasky sx_xunlock(&udev->enum_sx); 277910aab8b6SHans Petter Selasky return (255); 278010aab8b6SHans Petter Selasky } 278110aab8b6SHans Petter Selasky mtx_lock(&Giant); 278210aab8b6SHans Petter Selasky return (1); 278310aab8b6SHans Petter Selasky } 278410aab8b6SHans Petter Selasky #endif 278510aab8b6SHans Petter Selasky 2786cb18f7d1SAlfred Perlstein /* The following function unlocks enumerating the given USB device. */ 2787cb18f7d1SAlfred Perlstein 2788cb18f7d1SAlfred Perlstein void 2789cb18f7d1SAlfred Perlstein usbd_enum_unlock(struct usb_device *udev) 2790cb18f7d1SAlfred Perlstein { 2791cb18f7d1SAlfred Perlstein mtx_unlock(&Giant); 279291cd9240SAndrew Thompson sx_xunlock(&udev->enum_sx); 27932df1e9a6SAndrew Thompson sx_xunlock(&udev->sr_sx); 27942df1e9a6SAndrew Thompson } 27952df1e9a6SAndrew Thompson 27962df1e9a6SAndrew Thompson /* The following function locks suspend and resume. */ 27972df1e9a6SAndrew Thompson 27982df1e9a6SAndrew Thompson void 27992df1e9a6SAndrew Thompson usbd_sr_lock(struct usb_device *udev) 28002df1e9a6SAndrew Thompson { 28012df1e9a6SAndrew Thompson sx_xlock(&udev->sr_sx); 28022df1e9a6SAndrew Thompson /* 28032df1e9a6SAndrew Thompson * NEWBUS LOCK NOTE: We should check if any parent SX locks 28042df1e9a6SAndrew Thompson * are locked before locking Giant. Else the lock can be 28052df1e9a6SAndrew Thompson * locked multiple times. 28062df1e9a6SAndrew Thompson */ 28072df1e9a6SAndrew Thompson mtx_lock(&Giant); 28082df1e9a6SAndrew Thompson } 28092df1e9a6SAndrew Thompson 28102df1e9a6SAndrew Thompson /* The following function unlocks suspend and resume. */ 28112df1e9a6SAndrew Thompson 28122df1e9a6SAndrew Thompson void 28132df1e9a6SAndrew Thompson usbd_sr_unlock(struct usb_device *udev) 28142df1e9a6SAndrew Thompson { 28152df1e9a6SAndrew Thompson mtx_unlock(&Giant); 28162df1e9a6SAndrew Thompson sx_xunlock(&udev->sr_sx); 2817cb18f7d1SAlfred Perlstein } 2818cb18f7d1SAlfred Perlstein 2819cb18f7d1SAlfred Perlstein /* 2820cb18f7d1SAlfred Perlstein * The following function checks the enumerating lock for the given 2821cb18f7d1SAlfred Perlstein * USB device. 2822cb18f7d1SAlfred Perlstein */ 2823cb18f7d1SAlfred Perlstein 2824cb18f7d1SAlfred Perlstein uint8_t 2825cb18f7d1SAlfred Perlstein usbd_enum_is_locked(struct usb_device *udev) 2826cb18f7d1SAlfred Perlstein { 282791cd9240SAndrew Thompson return (sx_xlocked(&udev->enum_sx)); 2828cb18f7d1SAlfred Perlstein } 28298427ed84SHans Petter Selasky 28308427ed84SHans Petter Selasky /* 283164cb5e2aSHans Petter Selasky * The following function is used to serialize access to USB control 283264cb5e2aSHans Petter Selasky * transfers and the USB scratch area. If the lock is already grabbed 283364cb5e2aSHans Petter Selasky * this function returns zero. Else a value of one is returned. 283464cb5e2aSHans Petter Selasky */ 283564cb5e2aSHans Petter Selasky uint8_t 283664cb5e2aSHans Petter Selasky usbd_ctrl_lock(struct usb_device *udev) 283764cb5e2aSHans Petter Selasky { 283864cb5e2aSHans Petter Selasky if (sx_xlocked(&udev->ctrl_sx)) 283964cb5e2aSHans Petter Selasky return (0); 284064cb5e2aSHans Petter Selasky sx_xlock(&udev->ctrl_sx); 284164cb5e2aSHans Petter Selasky 284264cb5e2aSHans Petter Selasky /* 284364cb5e2aSHans Petter Selasky * We need to allow suspend and resume at this point, else the 284464cb5e2aSHans Petter Selasky * control transfer will timeout if the device is suspended! 284564cb5e2aSHans Petter Selasky */ 284664cb5e2aSHans Petter Selasky if (usbd_enum_is_locked(udev)) 284764cb5e2aSHans Petter Selasky usbd_sr_unlock(udev); 284864cb5e2aSHans Petter Selasky return (1); 284964cb5e2aSHans Petter Selasky } 285064cb5e2aSHans Petter Selasky 285164cb5e2aSHans Petter Selasky void 285264cb5e2aSHans Petter Selasky usbd_ctrl_unlock(struct usb_device *udev) 285364cb5e2aSHans Petter Selasky { 285464cb5e2aSHans Petter Selasky sx_xunlock(&udev->ctrl_sx); 285564cb5e2aSHans Petter Selasky 285664cb5e2aSHans Petter Selasky /* 285764cb5e2aSHans Petter Selasky * Restore the suspend and resume lock after we have unlocked 285864cb5e2aSHans Petter Selasky * the USB control transfer lock to avoid LOR: 285964cb5e2aSHans Petter Selasky */ 286064cb5e2aSHans Petter Selasky if (usbd_enum_is_locked(udev)) 286164cb5e2aSHans Petter Selasky usbd_sr_lock(udev); 286264cb5e2aSHans Petter Selasky } 286364cb5e2aSHans Petter Selasky 286464cb5e2aSHans Petter Selasky /* 28658427ed84SHans Petter Selasky * The following function is used to set the per-interface specific 28668427ed84SHans Petter Selasky * plug and play information. The string referred to by the pnpinfo 28678427ed84SHans Petter Selasky * argument can safely be freed after calling this function. The 28688427ed84SHans Petter Selasky * pnpinfo of an interface will be reset at device detach or when 28698427ed84SHans Petter Selasky * passing a NULL argument to this function. This function 28708427ed84SHans Petter Selasky * returns zero on success, else a USB_ERR_XXX failure code. 28718427ed84SHans Petter Selasky */ 28728427ed84SHans Petter Selasky 28738427ed84SHans Petter Selasky usb_error_t 28748427ed84SHans Petter Selasky usbd_set_pnpinfo(struct usb_device *udev, uint8_t iface_index, const char *pnpinfo) 28758427ed84SHans Petter Selasky { 28768427ed84SHans Petter Selasky struct usb_interface *iface; 28778427ed84SHans Petter Selasky 28788427ed84SHans Petter Selasky iface = usbd_get_iface(udev, iface_index); 28798427ed84SHans Petter Selasky if (iface == NULL) 28808427ed84SHans Petter Selasky return (USB_ERR_INVAL); 28818427ed84SHans Petter Selasky 28828427ed84SHans Petter Selasky if (iface->pnpinfo != NULL) { 28838427ed84SHans Petter Selasky free(iface->pnpinfo, M_USBDEV); 28848427ed84SHans Petter Selasky iface->pnpinfo = NULL; 28858427ed84SHans Petter Selasky } 28868427ed84SHans Petter Selasky 28878427ed84SHans Petter Selasky if (pnpinfo == NULL || pnpinfo[0] == 0) 28888427ed84SHans Petter Selasky return (0); /* success */ 28898427ed84SHans Petter Selasky 28908427ed84SHans Petter Selasky iface->pnpinfo = strdup(pnpinfo, M_USBDEV); 28918427ed84SHans Petter Selasky if (iface->pnpinfo == NULL) 28928427ed84SHans Petter Selasky return (USB_ERR_NOMEM); 28938427ed84SHans Petter Selasky 28948427ed84SHans Petter Selasky return (0); /* success */ 28958427ed84SHans Petter Selasky } 28968427ed84SHans Petter Selasky 2897d46dc4adSHans Petter Selasky usb_error_t 2898d46dc4adSHans Petter Selasky usbd_add_dynamic_quirk(struct usb_device *udev, uint16_t quirk) 2899d46dc4adSHans Petter Selasky { 2900d46dc4adSHans Petter Selasky uint8_t x; 2901d46dc4adSHans Petter Selasky 2902d46dc4adSHans Petter Selasky for (x = 0; x != USB_MAX_AUTO_QUIRK; x++) { 29033f37fb62SHans Petter Selasky if (udev->autoQuirk[x] == 0 || 29043f37fb62SHans Petter Selasky udev->autoQuirk[x] == quirk) { 2905d46dc4adSHans Petter Selasky udev->autoQuirk[x] = quirk; 2906d46dc4adSHans Petter Selasky return (0); /* success */ 2907d46dc4adSHans Petter Selasky } 2908d46dc4adSHans Petter Selasky } 2909d46dc4adSHans Petter Selasky return (USB_ERR_NOMEM); 2910d46dc4adSHans Petter Selasky } 2911a5cf1aaaSHans Petter Selasky 2912a5cf1aaaSHans Petter Selasky /* 2913a5cf1aaaSHans Petter Selasky * The following function is used to select the endpoint mode. It 2914a5cf1aaaSHans Petter Selasky * should not be called outside enumeration context. 2915a5cf1aaaSHans Petter Selasky */ 2916a5cf1aaaSHans Petter Selasky 2917a5cf1aaaSHans Petter Selasky usb_error_t 2918a5cf1aaaSHans Petter Selasky usbd_set_endpoint_mode(struct usb_device *udev, struct usb_endpoint *ep, 2919a5cf1aaaSHans Petter Selasky uint8_t ep_mode) 2920a5cf1aaaSHans Petter Selasky { 2921a5cf1aaaSHans Petter Selasky usb_error_t error; 2922f61fc4bdSHans Petter Selasky uint8_t do_unlock; 2923a5cf1aaaSHans Petter Selasky 29246950c75fSHans Petter Selasky /* Prevent re-enumeration */ 29256950c75fSHans Petter Selasky do_unlock = usbd_enum_lock(udev); 2926a5cf1aaaSHans Petter Selasky 2927a5cf1aaaSHans Petter Selasky if (udev->bus->methods->set_endpoint_mode != NULL) { 2928a5cf1aaaSHans Petter Selasky error = (udev->bus->methods->set_endpoint_mode) ( 2929a5cf1aaaSHans Petter Selasky udev, ep, ep_mode); 2930a5cf1aaaSHans Petter Selasky } else if (ep_mode != USB_EP_MODE_DEFAULT) { 2931a5cf1aaaSHans Petter Selasky error = USB_ERR_INVAL; 2932a5cf1aaaSHans Petter Selasky } else { 2933a5cf1aaaSHans Petter Selasky error = 0; 2934a5cf1aaaSHans Petter Selasky } 2935a5cf1aaaSHans Petter Selasky 2936a5cf1aaaSHans Petter Selasky /* only set new mode regardless of error */ 2937a5cf1aaaSHans Petter Selasky ep->ep_mode = ep_mode; 2938a5cf1aaaSHans Petter Selasky 2939f61fc4bdSHans Petter Selasky if (do_unlock) 2940f61fc4bdSHans Petter Selasky usbd_enum_unlock(udev); 2941a5cf1aaaSHans Petter Selasky return (error); 2942a5cf1aaaSHans Petter Selasky } 2943a5cf1aaaSHans Petter Selasky 2944a5cf1aaaSHans Petter Selasky uint8_t 2945a5cf1aaaSHans Petter Selasky usbd_get_endpoint_mode(struct usb_device *udev, struct usb_endpoint *ep) 2946a5cf1aaaSHans Petter Selasky { 2947a5cf1aaaSHans Petter Selasky return (ep->ep_mode); 2948a5cf1aaaSHans Petter Selasky } 2949