102ac6454SAndrew Thompson /*- 24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause 3718cf2ccSPedro F. Giffuni * 49b077d72SHans Petter Selasky * Copyright (c) 2008-2023 Hans Petter Selasky 502ac6454SAndrew Thompson * 602ac6454SAndrew Thompson * Redistribution and use in source and binary forms, with or without 702ac6454SAndrew Thompson * modification, are permitted provided that the following conditions 802ac6454SAndrew Thompson * are met: 902ac6454SAndrew Thompson * 1. Redistributions of source code must retain the above copyright 1002ac6454SAndrew Thompson * notice, this list of conditions and the following disclaimer. 1102ac6454SAndrew Thompson * 2. Redistributions in binary form must reproduce the above copyright 1202ac6454SAndrew Thompson * notice, this list of conditions and the following disclaimer in the 1302ac6454SAndrew Thompson * documentation and/or other materials provided with the distribution. 1402ac6454SAndrew Thompson * 1502ac6454SAndrew Thompson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1602ac6454SAndrew Thompson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1702ac6454SAndrew Thompson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1802ac6454SAndrew Thompson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1902ac6454SAndrew Thompson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2002ac6454SAndrew Thompson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2102ac6454SAndrew Thompson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2202ac6454SAndrew Thompson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2302ac6454SAndrew Thompson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2402ac6454SAndrew Thompson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2502ac6454SAndrew Thompson * SUCH DAMAGE. 2602ac6454SAndrew Thompson */ 2702ac6454SAndrew Thompson 28d2b99310SHans Petter Selasky #ifdef USB_GLOBAL_INCLUDE_FILE 29d2b99310SHans Petter Selasky #include USB_GLOBAL_INCLUDE_FILE 30d2b99310SHans Petter Selasky #else 31ed6d949aSAndrew Thompson #include <sys/stdint.h> 32ed6d949aSAndrew Thompson #include <sys/stddef.h> 33ed6d949aSAndrew Thompson #include <sys/param.h> 34e2e050c8SConrad Meyer #include <sys/eventhandler.h> 35ed6d949aSAndrew Thompson #include <sys/queue.h> 36ed6d949aSAndrew Thompson #include <sys/systm.h> 37ed6d949aSAndrew Thompson #include <sys/kernel.h> 38ed6d949aSAndrew Thompson #include <sys/bus.h> 39ed6d949aSAndrew Thompson #include <sys/module.h> 40ed6d949aSAndrew Thompson #include <sys/lock.h> 41ed6d949aSAndrew Thompson #include <sys/mutex.h> 42ed6d949aSAndrew Thompson #include <sys/condvar.h> 43ed6d949aSAndrew Thompson #include <sys/sysctl.h> 44ed6d949aSAndrew Thompson #include <sys/sx.h> 45ed6d949aSAndrew Thompson #include <sys/unistd.h> 46ed6d949aSAndrew Thompson #include <sys/callout.h> 47ed6d949aSAndrew Thompson #include <sys/malloc.h> 48ed6d949aSAndrew Thompson #include <sys/priv.h> 49ed6d949aSAndrew Thompson #include <sys/conf.h> 50ed6d949aSAndrew Thompson #include <sys/fcntl.h> 51ed6d949aSAndrew Thompson 5202ac6454SAndrew Thompson #include <dev/usb/usb.h> 53ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h> 54ed6d949aSAndrew Thompson #include <dev/usb/usbdi_util.h> 5502ac6454SAndrew Thompson #include <dev/usb/usb_ioctl.h> 56963169b4SHans Petter Selasky 57963169b4SHans Petter Selasky #if USB_HAVE_UGEN 58963169b4SHans Petter Selasky #include <sys/sbuf.h> 59963169b4SHans Petter Selasky #endif 60963169b4SHans Petter Selasky 6102ac6454SAndrew Thompson #include "usbdevs.h" 6202ac6454SAndrew Thompson 63a593f6b8SAndrew Thompson #define USB_DEBUG_VAR usb_debug 6402ac6454SAndrew Thompson 6502ac6454SAndrew Thompson #include <dev/usb/usb_core.h> 6602ac6454SAndrew Thompson #include <dev/usb/usb_debug.h> 6702ac6454SAndrew Thompson #include <dev/usb/usb_process.h> 6802ac6454SAndrew Thompson #include <dev/usb/usb_device.h> 6902ac6454SAndrew Thompson #include <dev/usb/usb_busdma.h> 7002ac6454SAndrew Thompson #include <dev/usb/usb_transfer.h> 7102ac6454SAndrew Thompson #include <dev/usb/usb_request.h> 7202ac6454SAndrew Thompson #include <dev/usb/usb_dynamic.h> 7302ac6454SAndrew Thompson #include <dev/usb/usb_hub.h> 7402ac6454SAndrew Thompson #include <dev/usb/usb_util.h> 7502ac6454SAndrew Thompson #include <dev/usb/usb_msctest.h> 76bdc081c6SAndrew Thompson #if USB_HAVE_UGEN 77bdc081c6SAndrew Thompson #include <dev/usb/usb_dev.h> 7802ac6454SAndrew Thompson #include <dev/usb/usb_generic.h> 79bdc081c6SAndrew Thompson #endif 8002ac6454SAndrew Thompson 8102ac6454SAndrew Thompson #include <dev/usb/quirk/usb_quirk.h> 8202ac6454SAndrew Thompson 8302ac6454SAndrew Thompson #include <dev/usb/usb_controller.h> 8402ac6454SAndrew Thompson #include <dev/usb/usb_bus.h> 85d2b99310SHans Petter Selasky #endif /* USB_GLOBAL_INCLUDE_FILE */ 8602ac6454SAndrew Thompson 8702ac6454SAndrew Thompson /* function prototypes */ 8802ac6454SAndrew Thompson 89f66c3cfcSEdward Tomasz Napierala static int sysctl_hw_usb_template(SYSCTL_HANDLER_ARGS); 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); 1058bb77249SHans Petter Selasky #if USB_HAVE_DEVCTL 106a593f6b8SAndrew Thompson static void usb_notify_addq(const char *type, struct usb_device *); 1078bb77249SHans Petter Selasky #endif 1088bb77249SHans Petter Selasky #if USB_HAVE_UGEN 109a593f6b8SAndrew Thompson static void usb_fifo_free_wrap(struct usb_device *, uint8_t, uint8_t); 110a593f6b8SAndrew Thompson static void usb_cdev_create(struct usb_device *); 111a593f6b8SAndrew Thompson static void usb_cdev_free(struct usb_device *); 1120f6e8f93SAndrew Thompson #endif 11302ac6454SAndrew Thompson 11402ac6454SAndrew Thompson /* This variable is global to allow easy access to it: */ 11502ac6454SAndrew Thompson 116fa49bce5SHans Petter Selasky #ifdef USB_TEMPLATE 117fa49bce5SHans Petter Selasky int usb_template = USB_TEMPLATE; 118fa49bce5SHans Petter Selasky #else 119c140287aSEdward Tomasz Napierala int usb_template = -1; 120fa49bce5SHans Petter Selasky #endif 12102ac6454SAndrew Thompson 122f66c3cfcSEdward Tomasz Napierala SYSCTL_PROC(_hw_usb, OID_AUTO, template, 123f66c3cfcSEdward Tomasz Napierala CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, 124f66c3cfcSEdward Tomasz Napierala NULL, 0, sysctl_hw_usb_template, 125f66c3cfcSEdward Tomasz Napierala "I", "Selected USB device side template"); 126f66c3cfcSEdward Tomasz Napierala 127f66c3cfcSEdward Tomasz Napierala /*------------------------------------------------------------------------* 128f66c3cfcSEdward Tomasz Napierala * usb_trigger_reprobe_on_off 129f66c3cfcSEdward Tomasz Napierala * 130f66c3cfcSEdward Tomasz Napierala * This function sets the pull up resistors for all ports currently 131f66c3cfcSEdward Tomasz Napierala * operating in device mode either on (when on_not_off is 1), or off 132f66c3cfcSEdward Tomasz Napierala * (when it's 0). 133f66c3cfcSEdward Tomasz Napierala *------------------------------------------------------------------------*/ 134f66c3cfcSEdward Tomasz Napierala static void 135f66c3cfcSEdward Tomasz Napierala usb_trigger_reprobe_on_off(int on_not_off) 136f66c3cfcSEdward Tomasz Napierala { 137f66c3cfcSEdward Tomasz Napierala struct usb_port_status ps; 138f66c3cfcSEdward Tomasz Napierala struct usb_bus *bus; 139f66c3cfcSEdward Tomasz Napierala struct usb_device *udev; 140f66c3cfcSEdward Tomasz Napierala usb_error_t err; 141f66c3cfcSEdward Tomasz Napierala int do_unlock, max; 142f66c3cfcSEdward Tomasz Napierala 143f66c3cfcSEdward Tomasz Napierala max = devclass_get_maxunit(usb_devclass_ptr); 144f66c3cfcSEdward Tomasz Napierala while (max >= 0) { 145f66c3cfcSEdward Tomasz Napierala mtx_lock(&usb_ref_lock); 146f66c3cfcSEdward Tomasz Napierala bus = devclass_get_softc(usb_devclass_ptr, max); 147f66c3cfcSEdward Tomasz Napierala max--; 148f66c3cfcSEdward Tomasz Napierala 149f66c3cfcSEdward Tomasz Napierala if (bus == NULL || bus->devices == NULL || 150f66c3cfcSEdward Tomasz Napierala bus->devices[USB_ROOT_HUB_ADDR] == NULL) { 151f66c3cfcSEdward Tomasz Napierala mtx_unlock(&usb_ref_lock); 152f66c3cfcSEdward Tomasz Napierala continue; 153f66c3cfcSEdward Tomasz Napierala } 154f66c3cfcSEdward Tomasz Napierala 155f66c3cfcSEdward Tomasz Napierala udev = bus->devices[USB_ROOT_HUB_ADDR]; 156f66c3cfcSEdward Tomasz Napierala 157f66c3cfcSEdward Tomasz Napierala if (udev->refcount == USB_DEV_REF_MAX) { 158f66c3cfcSEdward Tomasz Napierala mtx_unlock(&usb_ref_lock); 159f66c3cfcSEdward Tomasz Napierala continue; 160f66c3cfcSEdward Tomasz Napierala } 161f66c3cfcSEdward Tomasz Napierala 162f66c3cfcSEdward Tomasz Napierala udev->refcount++; 163f66c3cfcSEdward Tomasz Napierala mtx_unlock(&usb_ref_lock); 164f66c3cfcSEdward Tomasz Napierala 165f66c3cfcSEdward Tomasz Napierala do_unlock = usbd_enum_lock(udev); 166f66c3cfcSEdward Tomasz Napierala if (do_unlock > 1) { 167f66c3cfcSEdward Tomasz Napierala do_unlock = 0; 168f66c3cfcSEdward Tomasz Napierala goto next; 169f66c3cfcSEdward Tomasz Napierala } 170f66c3cfcSEdward Tomasz Napierala 171f66c3cfcSEdward Tomasz Napierala err = usbd_req_get_port_status(udev, NULL, &ps, 1); 172f66c3cfcSEdward Tomasz Napierala if (err != 0) { 173f66c3cfcSEdward Tomasz Napierala DPRINTF("usbd_req_get_port_status() " 174f66c3cfcSEdward Tomasz Napierala "failed: %s\n", usbd_errstr(err)); 175f66c3cfcSEdward Tomasz Napierala goto next; 176f66c3cfcSEdward Tomasz Napierala } 177f66c3cfcSEdward Tomasz Napierala 178f66c3cfcSEdward Tomasz Napierala if ((UGETW(ps.wPortStatus) & UPS_PORT_MODE_DEVICE) == 0) 179f66c3cfcSEdward Tomasz Napierala goto next; 180f66c3cfcSEdward Tomasz Napierala 181f66c3cfcSEdward Tomasz Napierala if (on_not_off) { 182f66c3cfcSEdward Tomasz Napierala err = usbd_req_set_port_feature(udev, NULL, 1, 183f66c3cfcSEdward Tomasz Napierala UHF_PORT_POWER); 184f66c3cfcSEdward Tomasz Napierala if (err != 0) { 185f66c3cfcSEdward Tomasz Napierala DPRINTF("usbd_req_set_port_feature() " 186f66c3cfcSEdward Tomasz Napierala "failed: %s\n", usbd_errstr(err)); 187f66c3cfcSEdward Tomasz Napierala } 188f66c3cfcSEdward Tomasz Napierala } else { 189f66c3cfcSEdward Tomasz Napierala err = usbd_req_clear_port_feature(udev, NULL, 1, 190f66c3cfcSEdward Tomasz Napierala UHF_PORT_POWER); 191f66c3cfcSEdward Tomasz Napierala if (err != 0) { 192f66c3cfcSEdward Tomasz Napierala DPRINTF("usbd_req_clear_port_feature() " 193f66c3cfcSEdward Tomasz Napierala "failed: %s\n", usbd_errstr(err)); 194f66c3cfcSEdward Tomasz Napierala } 195f66c3cfcSEdward Tomasz Napierala } 196f66c3cfcSEdward Tomasz Napierala 197f66c3cfcSEdward Tomasz Napierala next: 198f66c3cfcSEdward Tomasz Napierala mtx_lock(&usb_ref_lock); 199f66c3cfcSEdward Tomasz Napierala if (do_unlock) 200f66c3cfcSEdward Tomasz Napierala usbd_enum_unlock(udev); 201f66c3cfcSEdward Tomasz Napierala if (--(udev->refcount) == 0) 202f66c3cfcSEdward Tomasz Napierala cv_broadcast(&udev->ref_cv); 203f66c3cfcSEdward Tomasz Napierala mtx_unlock(&usb_ref_lock); 204f66c3cfcSEdward Tomasz Napierala } 205f66c3cfcSEdward Tomasz Napierala } 206f66c3cfcSEdward Tomasz Napierala 207f66c3cfcSEdward Tomasz Napierala /*------------------------------------------------------------------------* 208f66c3cfcSEdward Tomasz Napierala * usb_trigger_reprobe_all 209f66c3cfcSEdward Tomasz Napierala * 210f66c3cfcSEdward Tomasz Napierala * This function toggles the pull up resistors for all ports currently 211f66c3cfcSEdward Tomasz Napierala * operating in device mode, causing the host machine to reenumerate them. 212f66c3cfcSEdward Tomasz Napierala *------------------------------------------------------------------------*/ 213f66c3cfcSEdward Tomasz Napierala static void 214f66c3cfcSEdward Tomasz Napierala usb_trigger_reprobe_all(void) 215f66c3cfcSEdward Tomasz Napierala { 216f66c3cfcSEdward Tomasz Napierala 217f66c3cfcSEdward Tomasz Napierala /* 218f66c3cfcSEdward Tomasz Napierala * Set the pull up resistors off for all ports in device mode. 219f66c3cfcSEdward Tomasz Napierala */ 220f66c3cfcSEdward Tomasz Napierala usb_trigger_reprobe_on_off(0); 221f66c3cfcSEdward Tomasz Napierala 222f66c3cfcSEdward Tomasz Napierala /* 223f66c3cfcSEdward Tomasz Napierala * According to the DWC OTG spec this must be at least 3ms. 224f66c3cfcSEdward Tomasz Napierala */ 225f66c3cfcSEdward Tomasz Napierala usb_pause_mtx(NULL, USB_MS_TO_TICKS(USB_POWER_DOWN_TIME)); 226f66c3cfcSEdward Tomasz Napierala 227f66c3cfcSEdward Tomasz Napierala /* 228f66c3cfcSEdward Tomasz Napierala * Set the pull up resistors back on. 229f66c3cfcSEdward Tomasz Napierala */ 230f66c3cfcSEdward Tomasz Napierala usb_trigger_reprobe_on_off(1); 231f66c3cfcSEdward Tomasz Napierala } 232f66c3cfcSEdward Tomasz Napierala 233f66c3cfcSEdward Tomasz Napierala static int 234f66c3cfcSEdward Tomasz Napierala sysctl_hw_usb_template(SYSCTL_HANDLER_ARGS) 235f66c3cfcSEdward Tomasz Napierala { 236f66c3cfcSEdward Tomasz Napierala int error, val; 237f66c3cfcSEdward Tomasz Napierala 238f66c3cfcSEdward Tomasz Napierala val = usb_template; 239f66c3cfcSEdward Tomasz Napierala error = sysctl_handle_int(oidp, &val, 0, req); 240f66c3cfcSEdward Tomasz Napierala if (error != 0 || req->newptr == NULL || usb_template == val) 241f66c3cfcSEdward Tomasz Napierala return (error); 242f66c3cfcSEdward Tomasz Napierala 243f66c3cfcSEdward Tomasz Napierala usb_template = val; 244f66c3cfcSEdward Tomasz Napierala 245f66c3cfcSEdward Tomasz Napierala if (usb_template < 0) { 246f66c3cfcSEdward Tomasz Napierala usb_trigger_reprobe_on_off(0); 247f66c3cfcSEdward Tomasz Napierala } else { 248f66c3cfcSEdward Tomasz Napierala usb_trigger_reprobe_all(); 249f66c3cfcSEdward Tomasz Napierala } 250f66c3cfcSEdward Tomasz Napierala 251f66c3cfcSEdward Tomasz Napierala return (0); 252f66c3cfcSEdward Tomasz Napierala } 25302ac6454SAndrew Thompson 2547a918edfSAndrew Thompson /* English is default language */ 2557a918edfSAndrew Thompson 2567a918edfSAndrew Thompson static int usb_lang_id = 0x0009; 2577a918edfSAndrew Thompson static int usb_lang_mask = 0x00FF; 2587a918edfSAndrew Thompson 259af3b2549SHans Petter Selasky SYSCTL_INT(_hw_usb, OID_AUTO, usb_lang_id, CTLFLAG_RWTUN, 2607a918edfSAndrew Thompson &usb_lang_id, 0, "Preferred USB language ID"); 2617a918edfSAndrew Thompson 262af3b2549SHans Petter Selasky SYSCTL_INT(_hw_usb, OID_AUTO, usb_lang_mask, CTLFLAG_RWTUN, 2637a918edfSAndrew Thompson &usb_lang_mask, 0, "Preferred USB language mask"); 2647a918edfSAndrew Thompson 2654d4fa6edSAndrew Thompson static const char* statestr[USB_STATE_MAX] = { 2664d4fa6edSAndrew Thompson [USB_STATE_DETACHED] = "DETACHED", 2674d4fa6edSAndrew Thompson [USB_STATE_ATTACHED] = "ATTACHED", 2684d4fa6edSAndrew Thompson [USB_STATE_POWERED] = "POWERED", 2694d4fa6edSAndrew Thompson [USB_STATE_ADDRESSED] = "ADDRESSED", 2704d4fa6edSAndrew Thompson [USB_STATE_CONFIGURED] = "CONFIGURED", 2714d4fa6edSAndrew Thompson }; 2724d4fa6edSAndrew Thompson 2734d4fa6edSAndrew Thompson const char * 274a593f6b8SAndrew Thompson usb_statestr(enum usb_dev_state state) 2754d4fa6edSAndrew Thompson { 276ec8f3127SAndrew Thompson return ((state < USB_STATE_MAX) ? statestr[state] : "UNKNOWN"); 2774d4fa6edSAndrew Thompson } 27802ac6454SAndrew Thompson 279ae538d85SAndrew Thompson const char * 280ae538d85SAndrew Thompson usb_get_manufacturer(struct usb_device *udev) 281ae538d85SAndrew Thompson { 282ae538d85SAndrew Thompson return (udev->manufacturer ? udev->manufacturer : "Unknown"); 283ae538d85SAndrew Thompson } 284ae538d85SAndrew Thompson 285ae538d85SAndrew Thompson const char * 286ae538d85SAndrew Thompson usb_get_product(struct usb_device *udev) 287ae538d85SAndrew Thompson { 288ae538d85SAndrew Thompson return (udev->product ? udev->product : ""); 289ae538d85SAndrew Thompson } 290ae538d85SAndrew Thompson 291ae538d85SAndrew Thompson const char * 292ae538d85SAndrew Thompson usb_get_serial(struct usb_device *udev) 293ae538d85SAndrew Thompson { 294ae538d85SAndrew Thompson return (udev->serial ? udev->serial : ""); 295ae538d85SAndrew Thompson } 296ae538d85SAndrew Thompson 29702ac6454SAndrew Thompson /*------------------------------------------------------------------------* 298a593f6b8SAndrew Thompson * usbd_get_ep_by_addr 29902ac6454SAndrew Thompson * 300ae60fdfbSAndrew Thompson * This function searches for an USB ep by endpoint address and 30102ac6454SAndrew Thompson * direction. 30202ac6454SAndrew Thompson * 30302ac6454SAndrew Thompson * Returns: 30402ac6454SAndrew Thompson * NULL: Failure 30502ac6454SAndrew Thompson * Else: Success 30602ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 307ae60fdfbSAndrew Thompson struct usb_endpoint * 308a593f6b8SAndrew Thompson usbd_get_ep_by_addr(struct usb_device *udev, uint8_t ea_val) 30902ac6454SAndrew Thompson { 310ae60fdfbSAndrew Thompson struct usb_endpoint *ep = udev->endpoints; 311ae60fdfbSAndrew Thompson struct usb_endpoint *ep_end = udev->endpoints + udev->endpoints_max; 31202ac6454SAndrew Thompson enum { 31302ac6454SAndrew Thompson EA_MASK = (UE_DIR_IN | UE_DIR_OUT | UE_ADDR), 31402ac6454SAndrew Thompson }; 31502ac6454SAndrew Thompson 31602ac6454SAndrew Thompson /* 31702ac6454SAndrew Thompson * According to the USB specification not all bits are used 31802ac6454SAndrew Thompson * for the endpoint address. Keep defined bits only: 31902ac6454SAndrew Thompson */ 32002ac6454SAndrew Thompson ea_val &= EA_MASK; 32102ac6454SAndrew Thompson 32202ac6454SAndrew Thompson /* 32320733245SPedro F. Giffuni * Iterate across all the USB endpoints searching for a match 32402ac6454SAndrew Thompson * based on the endpoint address: 32502ac6454SAndrew Thompson */ 326ae60fdfbSAndrew Thompson for (; ep != ep_end; ep++) { 327ae60fdfbSAndrew Thompson if (ep->edesc == NULL) { 32802ac6454SAndrew Thompson continue; 32902ac6454SAndrew Thompson } 33002ac6454SAndrew Thompson /* do the mask and check the value */ 331ae60fdfbSAndrew Thompson if ((ep->edesc->bEndpointAddress & EA_MASK) == ea_val) { 33202ac6454SAndrew Thompson goto found; 33302ac6454SAndrew Thompson } 33402ac6454SAndrew Thompson } 33502ac6454SAndrew Thompson 33602ac6454SAndrew Thompson /* 337ae60fdfbSAndrew Thompson * The default endpoint is always present and is checked separately: 33802ac6454SAndrew Thompson */ 33922039494SHans Petter Selasky if ((udev->ctrl_ep.edesc != NULL) && 3405b3bb704SAndrew Thompson ((udev->ctrl_ep.edesc->bEndpointAddress & EA_MASK) == ea_val)) { 3415b3bb704SAndrew Thompson ep = &udev->ctrl_ep; 34202ac6454SAndrew Thompson goto found; 34302ac6454SAndrew Thompson } 34402ac6454SAndrew Thompson return (NULL); 34502ac6454SAndrew Thompson 34602ac6454SAndrew Thompson found: 347ae60fdfbSAndrew Thompson return (ep); 34802ac6454SAndrew Thompson } 34902ac6454SAndrew Thompson 35002ac6454SAndrew Thompson /*------------------------------------------------------------------------* 351a593f6b8SAndrew Thompson * usbd_get_endpoint 35202ac6454SAndrew Thompson * 353ae60fdfbSAndrew Thompson * This function searches for an USB endpoint based on the information 354760bc48eSAndrew Thompson * given by the passed "struct usb_config" pointer. 35502ac6454SAndrew Thompson * 35602ac6454SAndrew Thompson * Return values: 35702ac6454SAndrew Thompson * NULL: No match. 358ae60fdfbSAndrew Thompson * Else: Pointer to "struct usb_endpoint". 35902ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 360ae60fdfbSAndrew Thompson struct usb_endpoint * 361a593f6b8SAndrew Thompson usbd_get_endpoint(struct usb_device *udev, uint8_t iface_index, 362760bc48eSAndrew Thompson const struct usb_config *setup) 36302ac6454SAndrew Thompson { 364ae60fdfbSAndrew Thompson struct usb_endpoint *ep = udev->endpoints; 365ae60fdfbSAndrew Thompson struct usb_endpoint *ep_end = udev->endpoints + udev->endpoints_max; 36602ac6454SAndrew Thompson uint8_t index = setup->ep_index; 36702ac6454SAndrew Thompson uint8_t ea_mask; 36802ac6454SAndrew Thompson uint8_t ea_val; 36902ac6454SAndrew Thompson uint8_t type_mask; 37002ac6454SAndrew Thompson uint8_t type_val; 37102ac6454SAndrew Thompson 37202ac6454SAndrew Thompson DPRINTFN(10, "udev=%p iface_index=%d address=0x%x " 37302ac6454SAndrew Thompson "type=0x%x dir=0x%x index=%d\n", 37402ac6454SAndrew Thompson udev, iface_index, setup->endpoint, 37502ac6454SAndrew Thompson setup->type, setup->direction, setup->ep_index); 37602ac6454SAndrew Thompson 3774eae601eSAndrew Thompson /* check USB mode */ 3784eae601eSAndrew Thompson 379f29a0724SAndrew Thompson if (setup->usb_mode != USB_MODE_DUAL && 380f29a0724SAndrew Thompson udev->flags.usb_mode != setup->usb_mode) { 381ae60fdfbSAndrew Thompson /* wrong mode - no endpoint */ 3824eae601eSAndrew Thompson return (NULL); 3834eae601eSAndrew Thompson } 3844eae601eSAndrew Thompson 38502ac6454SAndrew Thompson /* setup expected endpoint direction mask and value */ 38602ac6454SAndrew Thompson 38751ec1603SAndrew Thompson if (setup->direction == UE_DIR_RX) { 38851ec1603SAndrew Thompson ea_mask = (UE_DIR_IN | UE_DIR_OUT); 389f29a0724SAndrew Thompson ea_val = (udev->flags.usb_mode == USB_MODE_DEVICE) ? 39051ec1603SAndrew Thompson UE_DIR_OUT : UE_DIR_IN; 39151ec1603SAndrew Thompson } else if (setup->direction == UE_DIR_TX) { 39251ec1603SAndrew Thompson ea_mask = (UE_DIR_IN | UE_DIR_OUT); 393f29a0724SAndrew Thompson ea_val = (udev->flags.usb_mode == USB_MODE_DEVICE) ? 39451ec1603SAndrew Thompson UE_DIR_IN : UE_DIR_OUT; 39551ec1603SAndrew Thompson } else if (setup->direction == UE_DIR_ANY) { 39602ac6454SAndrew Thompson /* match any endpoint direction */ 39702ac6454SAndrew Thompson ea_mask = 0; 39802ac6454SAndrew Thompson ea_val = 0; 39902ac6454SAndrew Thompson } else { 40002ac6454SAndrew Thompson /* match the given endpoint direction */ 40102ac6454SAndrew Thompson ea_mask = (UE_DIR_IN | UE_DIR_OUT); 40202ac6454SAndrew Thompson ea_val = (setup->direction & (UE_DIR_IN | UE_DIR_OUT)); 40302ac6454SAndrew Thompson } 40402ac6454SAndrew Thompson 40502ac6454SAndrew Thompson /* setup expected endpoint address */ 40602ac6454SAndrew Thompson 40702ac6454SAndrew Thompson if (setup->endpoint == UE_ADDR_ANY) { 40802ac6454SAndrew Thompson /* match any endpoint address */ 40902ac6454SAndrew Thompson } else { 41002ac6454SAndrew Thompson /* match the given endpoint address */ 41102ac6454SAndrew Thompson ea_mask |= UE_ADDR; 41202ac6454SAndrew Thompson ea_val |= (setup->endpoint & UE_ADDR); 41302ac6454SAndrew Thompson } 41402ac6454SAndrew Thompson 41502ac6454SAndrew Thompson /* setup expected endpoint type */ 41602ac6454SAndrew Thompson 41702ac6454SAndrew Thompson if (setup->type == UE_BULK_INTR) { 41802ac6454SAndrew Thompson /* this will match BULK and INTERRUPT endpoints */ 41902ac6454SAndrew Thompson type_mask = 2; 42002ac6454SAndrew Thompson type_val = 2; 42102ac6454SAndrew Thompson } else if (setup->type == UE_TYPE_ANY) { 42202ac6454SAndrew Thompson /* match any endpoint type */ 42302ac6454SAndrew Thompson type_mask = 0; 42402ac6454SAndrew Thompson type_val = 0; 42502ac6454SAndrew Thompson } else { 42602ac6454SAndrew Thompson /* match the given endpoint type */ 42702ac6454SAndrew Thompson type_mask = UE_XFERTYPE; 42802ac6454SAndrew Thompson type_val = (setup->type & UE_XFERTYPE); 42902ac6454SAndrew Thompson } 43002ac6454SAndrew Thompson 43102ac6454SAndrew Thompson /* 43220733245SPedro F. Giffuni * Iterate across all the USB endpoints searching for a match 43302ac6454SAndrew Thompson * based on the endpoint address. Note that we are searching 434ae60fdfbSAndrew Thompson * the endpoints from the beginning of the "udev->endpoints" array. 43502ac6454SAndrew Thompson */ 436ae60fdfbSAndrew Thompson for (; ep != ep_end; ep++) { 437ae60fdfbSAndrew Thompson if ((ep->edesc == NULL) || 438ae60fdfbSAndrew Thompson (ep->iface_index != iface_index)) { 43902ac6454SAndrew Thompson continue; 44002ac6454SAndrew Thompson } 44102ac6454SAndrew Thompson /* do the masks and check the values */ 44202ac6454SAndrew Thompson 443ae60fdfbSAndrew Thompson if (((ep->edesc->bEndpointAddress & ea_mask) == ea_val) && 444ae60fdfbSAndrew Thompson ((ep->edesc->bmAttributes & type_mask) == type_val)) { 44502ac6454SAndrew Thompson if (!index--) { 44602ac6454SAndrew Thompson goto found; 44702ac6454SAndrew Thompson } 44802ac6454SAndrew Thompson } 44902ac6454SAndrew Thompson } 45002ac6454SAndrew Thompson 45102ac6454SAndrew Thompson /* 452ae60fdfbSAndrew Thompson * Match against default endpoint last, so that "any endpoint", "any 453ae60fdfbSAndrew Thompson * address" and "any direction" returns the first endpoint of the 45402ac6454SAndrew Thompson * interface. "iface_index" and "direction" is ignored: 45502ac6454SAndrew Thompson */ 45622039494SHans Petter Selasky if ((udev->ctrl_ep.edesc != NULL) && 4575b3bb704SAndrew Thompson ((udev->ctrl_ep.edesc->bEndpointAddress & ea_mask) == ea_val) && 4585b3bb704SAndrew Thompson ((udev->ctrl_ep.edesc->bmAttributes & type_mask) == type_val) && 45902ac6454SAndrew Thompson (!index)) { 4605b3bb704SAndrew Thompson ep = &udev->ctrl_ep; 46102ac6454SAndrew Thompson goto found; 46202ac6454SAndrew Thompson } 46302ac6454SAndrew Thompson return (NULL); 46402ac6454SAndrew Thompson 46502ac6454SAndrew Thompson found: 466ae60fdfbSAndrew Thompson return (ep); 46702ac6454SAndrew Thompson } 46802ac6454SAndrew Thompson 46902ac6454SAndrew Thompson /*------------------------------------------------------------------------* 470ed6d949aSAndrew Thompson * usbd_interface_count 47102ac6454SAndrew Thompson * 47202ac6454SAndrew Thompson * This function stores the number of USB interfaces excluding 47302ac6454SAndrew Thompson * alternate settings, which the USB config descriptor reports into 47402ac6454SAndrew Thompson * the unsigned 8-bit integer pointed to by "count". 47502ac6454SAndrew Thompson * 47602ac6454SAndrew Thompson * Returns: 47702ac6454SAndrew Thompson * 0: Success 47802ac6454SAndrew Thompson * Else: Failure 47902ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 480e0a69b51SAndrew Thompson usb_error_t 481ed6d949aSAndrew Thompson usbd_interface_count(struct usb_device *udev, uint8_t *count) 48202ac6454SAndrew Thompson { 48302ac6454SAndrew Thompson if (udev->cdesc == NULL) { 48402ac6454SAndrew Thompson *count = 0; 48502ac6454SAndrew Thompson return (USB_ERR_NOT_CONFIGURED); 48602ac6454SAndrew Thompson } 487bdd41206SAndrew Thompson *count = udev->ifaces_max; 48802ac6454SAndrew Thompson return (USB_ERR_NORMAL_COMPLETION); 48902ac6454SAndrew Thompson } 49002ac6454SAndrew Thompson 49102ac6454SAndrew Thompson /*------------------------------------------------------------------------* 492a593f6b8SAndrew Thompson * usb_init_endpoint 49302ac6454SAndrew Thompson * 494ae60fdfbSAndrew Thompson * This function will initialise the USB endpoint structure pointed to by 495ae60fdfbSAndrew Thompson * the "endpoint" argument. The structure pointed to by "endpoint" must be 496bdd41206SAndrew Thompson * zeroed before calling this function. 49702ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 49802ac6454SAndrew Thompson static void 499a593f6b8SAndrew Thompson usb_init_endpoint(struct usb_device *udev, uint8_t iface_index, 500963169b4SHans Petter Selasky struct usb_endpoint_descriptor *edesc, 501963169b4SHans Petter Selasky struct usb_endpoint_ss_comp_descriptor *ecomp, 502963169b4SHans Petter Selasky struct usb_endpoint *ep) 50302ac6454SAndrew Thompson { 504e892b3feSHans Petter Selasky const struct usb_bus_methods *methods; 505a5cf1aaaSHans Petter Selasky usb_stream_t x; 506ee3e3ff5SAndrew Thompson 507bdd41206SAndrew Thompson methods = udev->bus->methods; 50802ac6454SAndrew Thompson 509ae60fdfbSAndrew Thompson (methods->endpoint_init) (udev, edesc, ep); 51002ac6454SAndrew Thompson 511ae60fdfbSAndrew Thompson /* initialise USB endpoint structure */ 512ae60fdfbSAndrew Thompson ep->edesc = edesc; 513963169b4SHans Petter Selasky ep->ecomp = ecomp; 514ae60fdfbSAndrew Thompson ep->iface_index = iface_index; 515a5cf1aaaSHans Petter Selasky 516a5cf1aaaSHans Petter Selasky /* setup USB stream queues */ 517a5cf1aaaSHans Petter Selasky for (x = 0; x != USB_MAX_EP_STREAMS; x++) { 518a5cf1aaaSHans Petter Selasky TAILQ_INIT(&ep->endpoint_q[x].head); 519a5cf1aaaSHans Petter Selasky ep->endpoint_q[x].command = &usbd_pipe_start; 520a5cf1aaaSHans Petter Selasky } 52102ac6454SAndrew Thompson 52239307315SAndrew Thompson /* the pipe is not supported by the hardware */ 523ae60fdfbSAndrew Thompson if (ep->methods == NULL) 52439307315SAndrew Thompson return; 52539307315SAndrew Thompson 526a5cf1aaaSHans Petter Selasky /* check for SUPER-speed streams mode endpoint */ 527a5cf1aaaSHans Petter Selasky if (udev->speed == USB_SPEED_SUPER && ecomp != NULL && 528a5cf1aaaSHans Petter Selasky (edesc->bmAttributes & UE_XFERTYPE) == UE_BULK && 529a5cf1aaaSHans Petter Selasky (UE_GET_BULK_STREAMS(ecomp->bmAttributes) != 0)) { 530a5cf1aaaSHans Petter Selasky usbd_set_endpoint_mode(udev, ep, USB_EP_MODE_STREAMS); 531a5cf1aaaSHans Petter Selasky } else { 532a5cf1aaaSHans Petter Selasky usbd_set_endpoint_mode(udev, ep, USB_EP_MODE_DEFAULT); 533a5cf1aaaSHans Petter Selasky } 534a5cf1aaaSHans Petter Selasky 53502ac6454SAndrew Thompson /* clear stall, if any */ 536bdd41206SAndrew Thompson if (methods->clear_stall != NULL) { 53702ac6454SAndrew Thompson USB_BUS_LOCK(udev->bus); 538ae60fdfbSAndrew Thompson (methods->clear_stall) (udev, ep); 53902ac6454SAndrew Thompson USB_BUS_UNLOCK(udev->bus); 54002ac6454SAndrew Thompson } 54102ac6454SAndrew Thompson } 54202ac6454SAndrew Thompson 543bdd41206SAndrew Thompson /*-----------------------------------------------------------------------* 544a593f6b8SAndrew Thompson * usb_endpoint_foreach 54516589beaSAndrew Thompson * 54616589beaSAndrew Thompson * This function will iterate all the USB endpoints except the control 54716589beaSAndrew Thompson * endpoint. This function is NULL safe. 54816589beaSAndrew Thompson * 54916589beaSAndrew Thompson * Return values: 550ae60fdfbSAndrew Thompson * NULL: End of USB endpoints 551ae60fdfbSAndrew Thompson * Else: Pointer to next USB endpoint 55216589beaSAndrew Thompson *------------------------------------------------------------------------*/ 553ae60fdfbSAndrew Thompson struct usb_endpoint * 554a593f6b8SAndrew Thompson usb_endpoint_foreach(struct usb_device *udev, struct usb_endpoint *ep) 55516589beaSAndrew Thompson { 556a9e26757SAndrew Thompson struct usb_endpoint *ep_end; 55716589beaSAndrew Thompson 55816589beaSAndrew Thompson /* be NULL safe */ 55916589beaSAndrew Thompson if (udev == NULL) 56016589beaSAndrew Thompson return (NULL); 56116589beaSAndrew Thompson 562a9e26757SAndrew Thompson ep_end = udev->endpoints + udev->endpoints_max; 563a9e26757SAndrew Thompson 564ae60fdfbSAndrew Thompson /* get next endpoint */ 565ae60fdfbSAndrew Thompson if (ep == NULL) 566ae60fdfbSAndrew Thompson ep = udev->endpoints; 56716589beaSAndrew Thompson else 568ae60fdfbSAndrew Thompson ep++; 56916589beaSAndrew Thompson 570ae60fdfbSAndrew Thompson /* find next allocated ep */ 571ae60fdfbSAndrew Thompson while (ep != ep_end) { 572ae60fdfbSAndrew Thompson if (ep->edesc != NULL) 573ae60fdfbSAndrew Thompson return (ep); 574ae60fdfbSAndrew Thompson ep++; 57516589beaSAndrew Thompson } 57616589beaSAndrew Thompson return (NULL); 57716589beaSAndrew Thompson } 57816589beaSAndrew Thompson 57916589beaSAndrew Thompson /*------------------------------------------------------------------------* 580b78e84d1SHans Petter Selasky * usb_wait_pending_refs 581d008478eSHans Petter Selasky * 582d008478eSHans Petter Selasky * This function will wait for any USB references to go away before 583b78e84d1SHans Petter Selasky * returning. This function is used before freeing a USB device. 584d008478eSHans Petter Selasky *------------------------------------------------------------------------*/ 585d008478eSHans Petter Selasky static void 586b78e84d1SHans Petter Selasky usb_wait_pending_refs(struct usb_device *udev) 587d008478eSHans Petter Selasky { 588d008478eSHans Petter Selasky #if USB_HAVE_UGEN 589b78e84d1SHans Petter Selasky DPRINTF("Refcount = %d\n", (int)udev->refcount); 590d008478eSHans Petter Selasky 591b78e84d1SHans Petter Selasky mtx_lock(&usb_ref_lock); 592b78e84d1SHans Petter Selasky udev->refcount--; 593d008478eSHans Petter Selasky while (1) { 594d008478eSHans Petter Selasky /* wait for any pending references to go away */ 595b78e84d1SHans Petter Selasky if (udev->refcount == 0) { 596b78e84d1SHans Petter Selasky /* prevent further refs being taken, if any */ 597d008478eSHans Petter Selasky udev->refcount = USB_DEV_REF_MAX; 598d008478eSHans Petter Selasky break; 599d008478eSHans Petter Selasky } 600d008478eSHans Petter Selasky cv_wait(&udev->ref_cv, &usb_ref_lock); 601d008478eSHans Petter Selasky } 602d008478eSHans Petter Selasky mtx_unlock(&usb_ref_lock); 603d008478eSHans Petter Selasky #endif 604d008478eSHans Petter Selasky } 605d008478eSHans Petter Selasky 606d008478eSHans Petter Selasky /*------------------------------------------------------------------------* 607a593f6b8SAndrew Thompson * usb_unconfigure 60802ac6454SAndrew Thompson * 609ae60fdfbSAndrew Thompson * This function will free all USB interfaces and USB endpoints belonging 61002ac6454SAndrew Thompson * to an USB device. 611bdd41206SAndrew Thompson * 612bdd41206SAndrew Thompson * Flag values, see "USB_UNCFG_FLAG_XXX". 61302ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 61402ac6454SAndrew Thompson static void 615a593f6b8SAndrew Thompson usb_unconfigure(struct usb_device *udev, uint8_t flag) 61602ac6454SAndrew Thompson { 617bdd41206SAndrew Thompson uint8_t do_unlock; 61802ac6454SAndrew Thompson 6196950c75fSHans Petter Selasky /* Prevent re-enumeration */ 6206950c75fSHans Petter Selasky do_unlock = usbd_enum_lock(udev); 621bdd41206SAndrew Thompson 622bdd41206SAndrew Thompson /* detach all interface drivers */ 623a593f6b8SAndrew Thompson usb_detach_device(udev, USB_IFACE_INDEX_ANY, flag); 624bdd41206SAndrew Thompson 625bdd41206SAndrew Thompson #if USB_HAVE_UGEN 626bdd41206SAndrew Thompson /* free all FIFOs except control endpoint FIFOs */ 627a593f6b8SAndrew Thompson usb_fifo_free_wrap(udev, USB_IFACE_INDEX_ANY, flag); 628bdd41206SAndrew Thompson 629bdd41206SAndrew Thompson /* 630bdd41206SAndrew Thompson * Free all cdev's, if any. 631bdd41206SAndrew Thompson */ 632a593f6b8SAndrew Thompson usb_cdev_free(udev); 633bdd41206SAndrew Thompson #endif 63402ac6454SAndrew Thompson 635bdc081c6SAndrew Thompson #if USB_HAVE_COMPAT_LINUX 63602ac6454SAndrew Thompson /* free Linux compat device, if any */ 6378d59ecb2SHans Petter Selasky if (udev->linux_endpoint_start != NULL) { 6388d59ecb2SHans Petter Selasky usb_linux_free_device_p(udev); 639760bc48eSAndrew Thompson udev->linux_endpoint_start = NULL; 64002ac6454SAndrew Thompson } 641bdc081c6SAndrew Thompson #endif 64202ac6454SAndrew Thompson 643a593f6b8SAndrew Thompson usb_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_FREE); 64402ac6454SAndrew Thompson 645ae60fdfbSAndrew Thompson /* free "cdesc" after "ifaces" and "endpoints", if any */ 6467efaaa9aSAndrew Thompson if (udev->cdesc != NULL) { 647f29a0724SAndrew Thompson if (udev->flags.usb_mode != USB_MODE_DEVICE) 6482c79a775SHans Petter Selasky usbd_free_config_desc(udev, udev->cdesc); 64902ac6454SAndrew Thompson udev->cdesc = NULL; 65002ac6454SAndrew Thompson } 65102ac6454SAndrew Thompson /* set unconfigured state */ 65202ac6454SAndrew Thompson udev->curr_config_no = USB_UNCONFIG_NO; 65302ac6454SAndrew Thompson udev->curr_config_index = USB_UNCONFIG_INDEX; 654bdd41206SAndrew Thompson 655cb18f7d1SAlfred Perlstein if (do_unlock) 656cb18f7d1SAlfred Perlstein usbd_enum_unlock(udev); 65702ac6454SAndrew Thompson } 65802ac6454SAndrew Thompson 65902ac6454SAndrew Thompson /*------------------------------------------------------------------------* 660a593f6b8SAndrew Thompson * usbd_set_config_index 66102ac6454SAndrew Thompson * 66202ac6454SAndrew Thompson * This function selects configuration by index, independent of the 66302ac6454SAndrew Thompson * actual configuration number. This function should not be used by 66402ac6454SAndrew Thompson * USB drivers. 66502ac6454SAndrew Thompson * 66602ac6454SAndrew Thompson * Returns: 66702ac6454SAndrew Thompson * 0: Success 66802ac6454SAndrew Thompson * Else: Failure 66902ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 670e0a69b51SAndrew Thompson usb_error_t 671a593f6b8SAndrew Thompson usbd_set_config_index(struct usb_device *udev, uint8_t index) 67202ac6454SAndrew Thompson { 673760bc48eSAndrew Thompson struct usb_status ds; 674760bc48eSAndrew Thompson struct usb_config_descriptor *cdp; 67502ac6454SAndrew Thompson uint16_t power; 67602ac6454SAndrew Thompson uint16_t max_power; 67702ac6454SAndrew Thompson uint8_t selfpowered; 67802ac6454SAndrew Thompson uint8_t do_unlock; 679e0a69b51SAndrew Thompson usb_error_t err; 68002ac6454SAndrew Thompson 68102ac6454SAndrew Thompson DPRINTFN(6, "udev=%p index=%d\n", udev, index); 68202ac6454SAndrew Thompson 6836950c75fSHans Petter Selasky /* Prevent re-enumeration */ 6846950c75fSHans Petter Selasky do_unlock = usbd_enum_lock(udev); 68502ac6454SAndrew Thompson 686d88688c7SAndrew Thompson usb_unconfigure(udev, 0); 68702ac6454SAndrew Thompson 68802ac6454SAndrew Thompson if (index == USB_UNCONFIG_INDEX) { 68902ac6454SAndrew Thompson /* 69002ac6454SAndrew Thompson * Leave unallocated when unconfiguring the 691a593f6b8SAndrew Thompson * device. "usb_unconfigure()" will also reset 69202ac6454SAndrew Thompson * the current config number and index. 69302ac6454SAndrew Thompson */ 694a593f6b8SAndrew Thompson err = usbd_req_set_config(udev, NULL, USB_UNCONFIG_NO); 695bd216778SAndrew Thompson if (udev->state == USB_STATE_CONFIGURED) 696a593f6b8SAndrew Thompson usb_set_device_state(udev, USB_STATE_ADDRESSED); 69702ac6454SAndrew Thompson goto done; 69802ac6454SAndrew Thompson } 69902ac6454SAndrew Thompson /* get the full config descriptor */ 700f29a0724SAndrew Thompson if (udev->flags.usb_mode == USB_MODE_DEVICE) { 7017efaaa9aSAndrew Thompson /* save some memory */ 702a593f6b8SAndrew Thompson err = usbd_req_get_descriptor_ptr(udev, &cdp, 703459d369eSAndrew Thompson (UDESC_CONFIG << 8) | index); 7047efaaa9aSAndrew Thompson } else { 7057efaaa9aSAndrew Thompson /* normal request */ 706a593f6b8SAndrew Thompson err = usbd_req_get_config_desc_full(udev, 7072c79a775SHans Petter Selasky NULL, &cdp, index); 7087efaaa9aSAndrew Thompson } 70902ac6454SAndrew Thompson if (err) { 71002ac6454SAndrew Thompson goto done; 71102ac6454SAndrew Thompson } 71202ac6454SAndrew Thompson /* set the new config descriptor */ 71302ac6454SAndrew Thompson 71402ac6454SAndrew Thompson udev->cdesc = cdp; 71502ac6454SAndrew Thompson 71602ac6454SAndrew Thompson /* Figure out if the device is self or bus powered. */ 71702ac6454SAndrew Thompson selfpowered = 0; 71802ac6454SAndrew Thompson if ((!udev->flags.uq_bus_powered) && 71902ac6454SAndrew Thompson (cdp->bmAttributes & UC_SELF_POWERED) && 720f29a0724SAndrew Thompson (udev->flags.usb_mode == USB_MODE_HOST)) { 72102ac6454SAndrew Thompson /* May be self powered. */ 72202ac6454SAndrew Thompson if (cdp->bmAttributes & UC_BUS_POWERED) { 72302ac6454SAndrew Thompson /* Must ask device. */ 724a593f6b8SAndrew Thompson err = usbd_req_get_device_status(udev, NULL, &ds); 72502ac6454SAndrew Thompson if (err) { 72602ac6454SAndrew Thompson DPRINTFN(0, "could not read " 72702ac6454SAndrew Thompson "device status: %s\n", 728a593f6b8SAndrew Thompson usbd_errstr(err)); 72902ac6454SAndrew Thompson } else if (UGETW(ds.wStatus) & UDS_SELF_POWERED) { 73002ac6454SAndrew Thompson selfpowered = 1; 73102ac6454SAndrew Thompson } 73202ac6454SAndrew Thompson DPRINTF("status=0x%04x \n", 73302ac6454SAndrew Thompson UGETW(ds.wStatus)); 73402ac6454SAndrew Thompson } else 73502ac6454SAndrew Thompson selfpowered = 1; 73602ac6454SAndrew Thompson } 73702ac6454SAndrew Thompson DPRINTF("udev=%p cdesc=%p (addr %d) cno=%d attr=0x%02x, " 73802ac6454SAndrew Thompson "selfpowered=%d, power=%d\n", 73902ac6454SAndrew Thompson udev, cdp, 74091f72cedSAndrew Thompson udev->address, cdp->bConfigurationValue, cdp->bmAttributes, 74102ac6454SAndrew Thompson selfpowered, cdp->bMaxPower * 2); 74202ac6454SAndrew Thompson 74302ac6454SAndrew Thompson /* Check if we have enough power. */ 74402ac6454SAndrew Thompson power = cdp->bMaxPower * 2; 74502ac6454SAndrew Thompson 74602ac6454SAndrew Thompson if (udev->parent_hub) { 74702ac6454SAndrew Thompson max_power = udev->parent_hub->hub->portpower; 74802ac6454SAndrew Thompson } else { 74902ac6454SAndrew Thompson max_power = USB_MAX_POWER; 75002ac6454SAndrew Thompson } 75102ac6454SAndrew Thompson 75202ac6454SAndrew Thompson if (power > max_power) { 75302ac6454SAndrew Thompson DPRINTFN(0, "power exceeded %d > %d\n", power, max_power); 75402ac6454SAndrew Thompson err = USB_ERR_NO_POWER; 75502ac6454SAndrew Thompson goto done; 75602ac6454SAndrew Thompson } 75702ac6454SAndrew Thompson /* Only update "self_powered" in USB Host Mode */ 758f29a0724SAndrew Thompson if (udev->flags.usb_mode == USB_MODE_HOST) { 75902ac6454SAndrew Thompson udev->flags.self_powered = selfpowered; 76002ac6454SAndrew Thompson } 76102ac6454SAndrew Thompson udev->power = power; 76202ac6454SAndrew Thompson udev->curr_config_no = cdp->bConfigurationValue; 76302ac6454SAndrew Thompson udev->curr_config_index = index; 764a593f6b8SAndrew Thompson usb_set_device_state(udev, USB_STATE_CONFIGURED); 76502ac6454SAndrew Thompson 76602ac6454SAndrew Thompson /* Set the actual configuration value. */ 767a593f6b8SAndrew Thompson err = usbd_req_set_config(udev, NULL, cdp->bConfigurationValue); 76802ac6454SAndrew Thompson if (err) { 76902ac6454SAndrew Thompson goto done; 77002ac6454SAndrew Thompson } 771bdd41206SAndrew Thompson 772a593f6b8SAndrew Thompson err = usb_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_ALLOC); 77302ac6454SAndrew Thompson if (err) { 77402ac6454SAndrew Thompson goto done; 77502ac6454SAndrew Thompson } 776bdd41206SAndrew Thompson 777a593f6b8SAndrew Thompson err = usb_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_INIT); 778bdd41206SAndrew Thompson if (err) { 779bdd41206SAndrew Thompson goto done; 78002ac6454SAndrew Thompson } 781bdd41206SAndrew Thompson 7820f6e8f93SAndrew Thompson #if USB_HAVE_UGEN 783ee3e3ff5SAndrew Thompson /* create device nodes for each endpoint */ 784a593f6b8SAndrew Thompson usb_cdev_create(udev); 7850f6e8f93SAndrew Thompson #endif 78602ac6454SAndrew Thompson 78702ac6454SAndrew Thompson done: 788a593f6b8SAndrew Thompson DPRINTF("error=%s\n", usbd_errstr(err)); 78902ac6454SAndrew Thompson if (err) { 790d88688c7SAndrew Thompson usb_unconfigure(udev, 0); 79102ac6454SAndrew Thompson } 792cb18f7d1SAlfred Perlstein if (do_unlock) 793cb18f7d1SAlfred Perlstein usbd_enum_unlock(udev); 79402ac6454SAndrew Thompson return (err); 79502ac6454SAndrew Thompson } 79602ac6454SAndrew Thompson 79702ac6454SAndrew Thompson /*------------------------------------------------------------------------* 798a593f6b8SAndrew Thompson * usb_config_parse 799bdd41206SAndrew Thompson * 800ae60fdfbSAndrew Thompson * This function will allocate and free USB interfaces and USB endpoints, 801ae60fdfbSAndrew Thompson * parse the USB configuration structure and initialise the USB endpoints 802bdd41206SAndrew Thompson * and interfaces. If "iface_index" is not equal to 803bdd41206SAndrew Thompson * "USB_IFACE_INDEX_ANY" then the "cmd" parameter is the 804bdd41206SAndrew Thompson * alternate_setting to be selected for the given interface. Else the 805bdd41206SAndrew Thompson * "cmd" parameter is defined by "USB_CFG_XXX". "iface_index" can be 806bdd41206SAndrew Thompson * "USB_IFACE_INDEX_ANY" or a valid USB interface index. This function 807bdd41206SAndrew Thompson * is typically called when setting the configuration or when setting 808bdd41206SAndrew Thompson * an alternate interface. 809bdd41206SAndrew Thompson * 810bdd41206SAndrew Thompson * Returns: 811bdd41206SAndrew Thompson * 0: Success 812bdd41206SAndrew Thompson * Else: Failure 813bdd41206SAndrew Thompson *------------------------------------------------------------------------*/ 814963169b4SHans Petter Selasky static usb_error_t 815a593f6b8SAndrew Thompson usb_config_parse(struct usb_device *udev, uint8_t iface_index, uint8_t cmd) 816bdd41206SAndrew Thompson { 817760bc48eSAndrew Thompson struct usb_idesc_parse_state ips; 818760bc48eSAndrew Thompson struct usb_interface_descriptor *id; 819760bc48eSAndrew Thompson struct usb_endpoint_descriptor *ed; 820760bc48eSAndrew Thompson struct usb_interface *iface; 821ae60fdfbSAndrew Thompson struct usb_endpoint *ep; 822e0a69b51SAndrew Thompson usb_error_t err; 823bdd41206SAndrew Thompson uint8_t ep_curr; 824bdd41206SAndrew Thompson uint8_t ep_max; 825bdd41206SAndrew Thompson uint8_t temp; 826bdd41206SAndrew Thompson uint8_t do_init; 827bdd41206SAndrew Thompson uint8_t alt_index; 828bdd41206SAndrew Thompson 829bdd41206SAndrew Thompson if (iface_index != USB_IFACE_INDEX_ANY) { 830bdd41206SAndrew Thompson /* parameter overload */ 831bdd41206SAndrew Thompson alt_index = cmd; 832bdd41206SAndrew Thompson cmd = USB_CFG_INIT; 833bdd41206SAndrew Thompson } else { 834bdd41206SAndrew Thompson /* not used */ 835bdd41206SAndrew Thompson alt_index = 0; 836bdd41206SAndrew Thompson } 837bdd41206SAndrew Thompson 838bdd41206SAndrew Thompson err = 0; 839bdd41206SAndrew Thompson 840bdd41206SAndrew Thompson DPRINTFN(5, "iface_index=%d cmd=%d\n", 841bdd41206SAndrew Thompson iface_index, cmd); 842bdd41206SAndrew Thompson 843e4611d26SHans Petter Selasky if (cmd == USB_CFG_INIT || cmd == USB_CFG_FREE) { 84491cd9240SAndrew Thompson sx_assert(&udev->enum_sx, SA_LOCKED); 845bdd41206SAndrew Thompson 846ae60fdfbSAndrew Thompson /* check for in-use endpoints */ 847bdd41206SAndrew Thompson 848e4611d26SHans Petter Selasky if (cmd == USB_CFG_INIT) { 849e4611d26SHans Petter Selasky ep = udev->endpoints; 850e4611d26SHans Petter Selasky ep_max = udev->endpoints_max; 851e4611d26SHans Petter Selasky while (ep_max--) { 852e4611d26SHans Petter Selasky /* look for matching endpoints */ 853e4611d26SHans Petter Selasky if (iface_index == USB_IFACE_INDEX_ANY || 854e4611d26SHans Petter Selasky iface_index == ep->iface_index) { 855e4611d26SHans Petter Selasky if (ep->refcount_alloc != 0) 856e4611d26SHans Petter Selasky return (USB_ERR_IN_USE); 857e4611d26SHans Petter Selasky } 858e4611d26SHans Petter Selasky } 859e4611d26SHans Petter Selasky } 860e4611d26SHans Petter Selasky 861ae60fdfbSAndrew Thompson ep = udev->endpoints; 862ae60fdfbSAndrew Thompson ep_max = udev->endpoints_max; 863bdd41206SAndrew Thompson while (ep_max--) { 864ae60fdfbSAndrew Thompson /* look for matching endpoints */ 865e4611d26SHans Petter Selasky if (iface_index == USB_IFACE_INDEX_ANY || 866e4611d26SHans Petter Selasky iface_index == ep->iface_index) { 867bdd41206SAndrew Thompson /* 868e4611d26SHans Petter Selasky * Check if hardware needs a callback 869e4611d26SHans Petter Selasky * to unconfigure the endpoint. This 870e4611d26SHans Petter Selasky * may happen multiple times, 871e4611d26SHans Petter Selasky * because the requested alternate 872e4611d26SHans Petter Selasky * setting may fail. The callback 873e4611d26SHans Petter Selasky * implementation should be aware of 874e4611d26SHans Petter Selasky * and handle that. 875bdd41206SAndrew Thompson */ 876e4611d26SHans Petter Selasky if (ep->edesc != NULL && 877e4611d26SHans Petter Selasky udev->bus->methods->endpoint_uninit != NULL) 878e4611d26SHans Petter Selasky udev->bus->methods->endpoint_uninit(udev, ep); 879e4611d26SHans Petter Selasky 880ae60fdfbSAndrew Thompson /* reset endpoint */ 881ae60fdfbSAndrew Thompson memset(ep, 0, sizeof(*ep)); 882ae60fdfbSAndrew Thompson /* make sure we don't zero the endpoint again */ 883ae60fdfbSAndrew Thompson ep->iface_index = USB_IFACE_INDEX_ANY; 884bdd41206SAndrew Thompson } 885ae60fdfbSAndrew Thompson ep++; 886bdd41206SAndrew Thompson } 887bdd41206SAndrew Thompson 888e4611d26SHans Petter Selasky if (cmd == USB_CFG_FREE) 889e4611d26SHans Petter Selasky goto cleanup; 890bdd41206SAndrew Thompson } 891bdd41206SAndrew Thompson 892bdd41206SAndrew Thompson memset(&ips, 0, sizeof(ips)); 893bdd41206SAndrew Thompson 894bdd41206SAndrew Thompson ep_curr = 0; 895bdd41206SAndrew Thompson ep_max = 0; 896bdd41206SAndrew Thompson 897a593f6b8SAndrew Thompson while ((id = usb_idesc_foreach(udev->cdesc, &ips))) { 898bdd41206SAndrew Thompson iface = udev->ifaces + ips.iface_index; 899bdd41206SAndrew Thompson 900bdd41206SAndrew Thompson /* check for specific interface match */ 901bdd41206SAndrew Thompson 902bdd41206SAndrew Thompson if (cmd == USB_CFG_INIT) { 903bdd41206SAndrew Thompson if ((iface_index != USB_IFACE_INDEX_ANY) && 904bdd41206SAndrew Thompson (iface_index != ips.iface_index)) { 905bdd41206SAndrew Thompson /* wrong interface */ 906bdd41206SAndrew Thompson do_init = 0; 907bdd41206SAndrew Thompson } else if (alt_index != ips.iface_index_alt) { 908bdd41206SAndrew Thompson /* wrong alternate setting */ 909bdd41206SAndrew Thompson do_init = 0; 910bdd41206SAndrew Thompson } else { 911bdd41206SAndrew Thompson /* initialise interface */ 912bdd41206SAndrew Thompson do_init = 1; 913bdd41206SAndrew Thompson } 914b1f99f9cSHans Petter Selasky /* update number of alternate settings, if any */ 915b1f99f9cSHans Petter Selasky if (iface_index == USB_IFACE_INDEX_ANY) 916b1f99f9cSHans Petter Selasky iface->num_altsetting = ips.iface_index_alt + 1; 917bdd41206SAndrew Thompson } else 918bdd41206SAndrew Thompson do_init = 0; 919bdd41206SAndrew Thompson 920bdd41206SAndrew Thompson /* check for new interface */ 921bdd41206SAndrew Thompson if (ips.iface_index_alt == 0) { 922bdd41206SAndrew Thompson /* update current number of endpoints */ 923bdd41206SAndrew Thompson ep_curr = ep_max; 924bdd41206SAndrew Thompson } 925b8b3f4fdSHans Petter Selasky 926bdd41206SAndrew Thompson /* check for init */ 927bdd41206SAndrew Thompson if (do_init) { 928bdd41206SAndrew Thompson /* setup the USB interface structure */ 929bdd41206SAndrew Thompson iface->idesc = id; 930bdd41206SAndrew Thompson /* set alternate index */ 931bdd41206SAndrew Thompson iface->alt_index = alt_index; 932d9073c1eSHans Petter Selasky /* set default interface parent */ 933d9073c1eSHans Petter Selasky if (iface_index == USB_IFACE_INDEX_ANY) { 934d9073c1eSHans Petter Selasky iface->parent_iface_index = 935d9073c1eSHans Petter Selasky USB_IFACE_INDEX_ANY; 936d9073c1eSHans Petter Selasky } 937bdd41206SAndrew Thompson } 938bdd41206SAndrew Thompson 939bdd41206SAndrew Thompson DPRINTFN(5, "found idesc nendpt=%d\n", id->bNumEndpoints); 940bdd41206SAndrew Thompson 941760bc48eSAndrew Thompson ed = (struct usb_endpoint_descriptor *)id; 942bdd41206SAndrew Thompson 943bdd41206SAndrew Thompson temp = ep_curr; 944bdd41206SAndrew Thompson 945bdd41206SAndrew Thompson /* iterate all the endpoint descriptors */ 946a593f6b8SAndrew Thompson while ((ed = usb_edesc_foreach(udev->cdesc, ed))) { 9475b0752bbSHans Petter Selasky /* check if endpoint limit has been reached */ 9485b0752bbSHans Petter Selasky if (temp >= USB_MAX_EP_UNITS) { 9495b0752bbSHans Petter Selasky DPRINTF("Endpoint limit reached\n"); 9505b0752bbSHans Petter Selasky break; 9515b0752bbSHans Petter Selasky } 952bdd41206SAndrew Thompson 953ae60fdfbSAndrew Thompson ep = udev->endpoints + temp; 954bdd41206SAndrew Thompson 955bdd41206SAndrew Thompson if (do_init) { 956963169b4SHans Petter Selasky void *ecomp; 957963169b4SHans Petter Selasky 958963169b4SHans Petter Selasky ecomp = usb_ed_comp_foreach(udev->cdesc, (void *)ed); 959963169b4SHans Petter Selasky if (ecomp != NULL) 960963169b4SHans Petter Selasky DPRINTFN(5, "Found endpoint companion descriptor\n"); 961963169b4SHans Petter Selasky 962a593f6b8SAndrew Thompson usb_init_endpoint(udev, 963963169b4SHans Petter Selasky ips.iface_index, ed, ecomp, ep); 964bdd41206SAndrew Thompson } 965bdd41206SAndrew Thompson 966bdd41206SAndrew Thompson temp ++; 967bdd41206SAndrew Thompson 968bdd41206SAndrew Thompson /* find maximum number of endpoints */ 969bdd41206SAndrew Thompson if (ep_max < temp) 970bdd41206SAndrew Thompson ep_max = temp; 971bdd41206SAndrew Thompson } 972bdd41206SAndrew Thompson } 973bdd41206SAndrew Thompson 974bdd41206SAndrew Thompson /* NOTE: It is valid to have no interfaces and no endpoints! */ 975bdd41206SAndrew Thompson 976bdd41206SAndrew Thompson if (cmd == USB_CFG_ALLOC) { 977bdd41206SAndrew Thompson udev->ifaces_max = ips.iface_index; 9785b0752bbSHans Petter Selasky #if (USB_HAVE_FIXED_IFACE == 0) 979bdd41206SAndrew Thompson udev->ifaces = NULL; 980bdd41206SAndrew Thompson if (udev->ifaces_max != 0) { 981bdd41206SAndrew Thompson udev->ifaces = malloc(sizeof(*iface) * udev->ifaces_max, 982bdd41206SAndrew Thompson M_USB, M_WAITOK | M_ZERO); 983bdd41206SAndrew Thompson if (udev->ifaces == NULL) { 984bdd41206SAndrew Thompson err = USB_ERR_NOMEM; 985bdd41206SAndrew Thompson goto done; 986bdd41206SAndrew Thompson } 987bdd41206SAndrew Thompson } 9885b0752bbSHans Petter Selasky #endif 9895b0752bbSHans Petter Selasky #if (USB_HAVE_FIXED_ENDPOINT == 0) 9909469c929SAndrew Thompson if (ep_max != 0) { 991ae60fdfbSAndrew Thompson udev->endpoints = malloc(sizeof(*ep) * ep_max, 992bdd41206SAndrew Thompson M_USB, M_WAITOK | M_ZERO); 993ae60fdfbSAndrew Thompson if (udev->endpoints == NULL) { 994bdd41206SAndrew Thompson err = USB_ERR_NOMEM; 995bdd41206SAndrew Thompson goto done; 996bdd41206SAndrew Thompson } 9979469c929SAndrew Thompson } else { 998ae60fdfbSAndrew Thompson udev->endpoints = NULL; 999bdd41206SAndrew Thompson } 10005b0752bbSHans Petter Selasky #endif 10019469c929SAndrew Thompson USB_BUS_LOCK(udev->bus); 1002ae60fdfbSAndrew Thompson udev->endpoints_max = ep_max; 10039469c929SAndrew Thompson /* reset any ongoing clear-stall */ 1004ae60fdfbSAndrew Thompson udev->ep_curr = NULL; 10059469c929SAndrew Thompson USB_BUS_UNLOCK(udev->bus); 1006bdd41206SAndrew Thompson } 10076c218933SHans Petter Selasky #if (USB_HAVE_FIXED_IFACE == 0) || (USB_HAVE_FIXED_ENDPOINT == 0) 1008bdd41206SAndrew Thompson done: 10096c218933SHans Petter Selasky #endif 1010bdd41206SAndrew Thompson if (err) { 1011bdd41206SAndrew Thompson if (cmd == USB_CFG_ALLOC) { 1012bdd41206SAndrew Thompson cleanup: 10139469c929SAndrew Thompson USB_BUS_LOCK(udev->bus); 1014ae60fdfbSAndrew Thompson udev->endpoints_max = 0; 10159469c929SAndrew Thompson /* reset any ongoing clear-stall */ 1016ae60fdfbSAndrew Thompson udev->ep_curr = NULL; 10179469c929SAndrew Thompson USB_BUS_UNLOCK(udev->bus); 10189469c929SAndrew Thompson 10195b0752bbSHans Petter Selasky #if (USB_HAVE_FIXED_IFACE == 0) 1020bdd41206SAndrew Thompson free(udev->ifaces, M_USB); 1021bdd41206SAndrew Thompson udev->ifaces = NULL; 10225b0752bbSHans Petter Selasky #endif 10235b0752bbSHans Petter Selasky #if (USB_HAVE_FIXED_ENDPOINT == 0) 10245b0752bbSHans Petter Selasky free(udev->endpoints, M_USB); 1025ae60fdfbSAndrew Thompson udev->endpoints = NULL; 10265b0752bbSHans Petter Selasky #endif 1027bdd41206SAndrew Thompson udev->ifaces_max = 0; 1028bdd41206SAndrew Thompson } 1029bdd41206SAndrew Thompson } 1030bdd41206SAndrew Thompson return (err); 1031bdd41206SAndrew Thompson } 1032bdd41206SAndrew Thompson 1033bdd41206SAndrew Thompson /*------------------------------------------------------------------------* 1034a593f6b8SAndrew Thompson * usbd_set_alt_interface_index 103502ac6454SAndrew Thompson * 103602ac6454SAndrew Thompson * This function will select an alternate interface index for the 103702ac6454SAndrew Thompson * given interface index. The interface should not be in use when this 1038e2805033SAndrew Thompson * function is called. That means there should not be any open USB 1039e2805033SAndrew Thompson * transfers. Else an error is returned. If the alternate setting is 1040e2805033SAndrew Thompson * already set this function will simply return success. This function 1041e2805033SAndrew Thompson * is called in Host mode and Device mode! 104202ac6454SAndrew Thompson * 104302ac6454SAndrew Thompson * Returns: 104402ac6454SAndrew Thompson * 0: Success 104502ac6454SAndrew Thompson * Else: Failure 104602ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 1047e0a69b51SAndrew Thompson usb_error_t 1048a593f6b8SAndrew Thompson usbd_set_alt_interface_index(struct usb_device *udev, 104902ac6454SAndrew Thompson uint8_t iface_index, uint8_t alt_index) 105002ac6454SAndrew Thompson { 1051a593f6b8SAndrew Thompson struct usb_interface *iface = usbd_get_iface(udev, iface_index); 1052e0a69b51SAndrew Thompson usb_error_t err; 105302ac6454SAndrew Thompson uint8_t do_unlock; 105402ac6454SAndrew Thompson 10556950c75fSHans Petter Selasky /* Prevent re-enumeration */ 10566950c75fSHans Petter Selasky do_unlock = usbd_enum_lock(udev); 10576950c75fSHans Petter Selasky 105802ac6454SAndrew Thompson if (iface == NULL) { 105902ac6454SAndrew Thompson err = USB_ERR_INVAL; 106002ac6454SAndrew Thompson goto done; 106102ac6454SAndrew Thompson } 1062e2805033SAndrew Thompson if (iface->alt_index == alt_index) { 1063e2805033SAndrew Thompson /* 1064e2805033SAndrew Thompson * Optimise away duplicate setting of 1065e2805033SAndrew Thompson * alternate setting in USB Host Mode! 1066e2805033SAndrew Thompson */ 1067e2805033SAndrew Thompson err = 0; 1068e2805033SAndrew Thompson goto done; 1069e2805033SAndrew Thompson } 10700f6e8f93SAndrew Thompson #if USB_HAVE_UGEN 107102ac6454SAndrew Thompson /* 107202ac6454SAndrew Thompson * Free all generic FIFOs for this interface, except control 107302ac6454SAndrew Thompson * endpoint FIFOs: 107402ac6454SAndrew Thompson */ 1075a593f6b8SAndrew Thompson usb_fifo_free_wrap(udev, iface_index, 0); 10760f6e8f93SAndrew Thompson #endif 1077bdd41206SAndrew Thompson 1078a593f6b8SAndrew Thompson err = usb_config_parse(udev, iface_index, alt_index); 107902ac6454SAndrew Thompson if (err) { 108002ac6454SAndrew Thompson goto done; 108102ac6454SAndrew Thompson } 1082bd73b187SAlfred Perlstein if (iface->alt_index != alt_index) { 1083bd73b187SAlfred Perlstein /* the alternate setting does not exist */ 1084bd73b187SAlfred Perlstein err = USB_ERR_INVAL; 1085bd73b187SAlfred Perlstein goto done; 1086bd73b187SAlfred Perlstein } 1087bd73b187SAlfred Perlstein 1088a593f6b8SAndrew Thompson err = usbd_req_set_alt_interface_no(udev, NULL, iface_index, 108902ac6454SAndrew Thompson iface->idesc->bAlternateSetting); 109002ac6454SAndrew Thompson 109102ac6454SAndrew Thompson done: 1092cb18f7d1SAlfred Perlstein if (do_unlock) 1093cb18f7d1SAlfred Perlstein usbd_enum_unlock(udev); 109402ac6454SAndrew Thompson return (err); 109502ac6454SAndrew Thompson } 109602ac6454SAndrew Thompson 109702ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1098a593f6b8SAndrew Thompson * usbd_set_endpoint_stall 109902ac6454SAndrew Thompson * 1100963169b4SHans Petter Selasky * This function is used to make a BULK or INTERRUPT endpoint send 1101963169b4SHans Petter Selasky * STALL tokens in USB device mode. 110202ac6454SAndrew Thompson * 110302ac6454SAndrew Thompson * Returns: 110402ac6454SAndrew Thompson * 0: Success 110502ac6454SAndrew Thompson * Else: Failure 110602ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 1107e0a69b51SAndrew Thompson usb_error_t 1108a593f6b8SAndrew Thompson usbd_set_endpoint_stall(struct usb_device *udev, struct usb_endpoint *ep, 110902ac6454SAndrew Thompson uint8_t do_stall) 111002ac6454SAndrew Thompson { 1111760bc48eSAndrew Thompson struct usb_xfer *xfer; 1112a5cf1aaaSHans Petter Selasky usb_stream_t x; 111302ac6454SAndrew Thompson uint8_t et; 111402ac6454SAndrew Thompson uint8_t was_stalled; 111502ac6454SAndrew Thompson 1116ae60fdfbSAndrew Thompson if (ep == NULL) { 111702ac6454SAndrew Thompson /* nothing to do */ 111802ac6454SAndrew Thompson DPRINTF("Cannot find endpoint\n"); 111902ac6454SAndrew Thompson /* 112002ac6454SAndrew Thompson * Pretend that the clear or set stall request is 112102ac6454SAndrew Thompson * successful else some USB host stacks can do 112202ac6454SAndrew Thompson * strange things, especially when a control endpoint 112302ac6454SAndrew Thompson * stalls. 112402ac6454SAndrew Thompson */ 112502ac6454SAndrew Thompson return (0); 112602ac6454SAndrew Thompson } 1127ae60fdfbSAndrew Thompson et = (ep->edesc->bmAttributes & UE_XFERTYPE); 112802ac6454SAndrew Thompson 112902ac6454SAndrew Thompson if ((et != UE_BULK) && 113002ac6454SAndrew Thompson (et != UE_INTERRUPT)) { 113102ac6454SAndrew Thompson /* 113202ac6454SAndrew Thompson * Should not stall control 113302ac6454SAndrew Thompson * nor isochronous endpoints. 113402ac6454SAndrew Thompson */ 113502ac6454SAndrew Thompson DPRINTF("Invalid endpoint\n"); 113602ac6454SAndrew Thompson return (0); 113702ac6454SAndrew Thompson } 113802ac6454SAndrew Thompson USB_BUS_LOCK(udev->bus); 113902ac6454SAndrew Thompson 114002ac6454SAndrew Thompson /* store current stall state */ 1141ae60fdfbSAndrew Thompson was_stalled = ep->is_stalled; 114202ac6454SAndrew Thompson 114302ac6454SAndrew Thompson /* check for no change */ 114402ac6454SAndrew Thompson if (was_stalled && do_stall) { 1145ae60fdfbSAndrew Thompson /* if the endpoint is already stalled do nothing */ 114602ac6454SAndrew Thompson USB_BUS_UNLOCK(udev->bus); 114702ac6454SAndrew Thompson DPRINTF("No change\n"); 114802ac6454SAndrew Thompson return (0); 114902ac6454SAndrew Thompson } 115002ac6454SAndrew Thompson /* set stalled state */ 1151ae60fdfbSAndrew Thompson ep->is_stalled = 1; 115202ac6454SAndrew Thompson 115302ac6454SAndrew Thompson if (do_stall || (!was_stalled)) { 115402ac6454SAndrew Thompson if (!was_stalled) { 1155a5cf1aaaSHans Petter Selasky for (x = 0; x != USB_MAX_EP_STREAMS; x++) { 115602ac6454SAndrew Thompson /* lookup the current USB transfer, if any */ 1157a5cf1aaaSHans Petter Selasky xfer = ep->endpoint_q[x].curr; 1158a5cf1aaaSHans Petter Selasky if (xfer != NULL) { 115902ac6454SAndrew Thompson /* 1160a5cf1aaaSHans Petter Selasky * The "xfer_stall" method 1161a5cf1aaaSHans Petter Selasky * will complete the USB 1162a5cf1aaaSHans Petter Selasky * transfer like in case of a 1163a5cf1aaaSHans Petter Selasky * timeout setting the error 1164a5cf1aaaSHans Petter Selasky * code "USB_ERR_STALLED". 116502ac6454SAndrew Thompson */ 1166a5cf1aaaSHans Petter Selasky (udev->bus->methods->xfer_stall) (xfer); 1167a5cf1aaaSHans Petter Selasky } 1168a5cf1aaaSHans Petter Selasky } 1169a5cf1aaaSHans Petter Selasky } 1170a5cf1aaaSHans Petter Selasky (udev->bus->methods->set_stall) (udev, ep, &do_stall); 117102ac6454SAndrew Thompson } 117202ac6454SAndrew Thompson if (!do_stall) { 1173ae60fdfbSAndrew Thompson ep->toggle_next = 0; /* reset data toggle */ 1174ae60fdfbSAndrew Thompson ep->is_stalled = 0; /* clear stalled state */ 117502ac6454SAndrew Thompson 1176ae60fdfbSAndrew Thompson (udev->bus->methods->clear_stall) (udev, ep); 117702ac6454SAndrew Thompson 1178a5cf1aaaSHans Petter Selasky /* start the current or next transfer, if any */ 1179a5cf1aaaSHans Petter Selasky for (x = 0; x != USB_MAX_EP_STREAMS; x++) { 1180a5cf1aaaSHans Petter Selasky usb_command_wrapper(&ep->endpoint_q[x], 1181a5cf1aaaSHans Petter Selasky ep->endpoint_q[x].curr); 1182a5cf1aaaSHans Petter Selasky } 118302ac6454SAndrew Thompson } 118402ac6454SAndrew Thompson USB_BUS_UNLOCK(udev->bus); 118502ac6454SAndrew Thompson return (0); 118602ac6454SAndrew Thompson } 118702ac6454SAndrew Thompson 118802ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1189a593f6b8SAndrew Thompson * usb_reset_iface_endpoints - used in USB device side mode 119002ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 1191e0a69b51SAndrew Thompson usb_error_t 1192a593f6b8SAndrew Thompson usb_reset_iface_endpoints(struct usb_device *udev, uint8_t iface_index) 119302ac6454SAndrew Thompson { 1194ae60fdfbSAndrew Thompson struct usb_endpoint *ep; 1195ae60fdfbSAndrew Thompson struct usb_endpoint *ep_end; 119602ac6454SAndrew Thompson 1197ae60fdfbSAndrew Thompson ep = udev->endpoints; 1198ae60fdfbSAndrew Thompson ep_end = udev->endpoints + udev->endpoints_max; 119902ac6454SAndrew Thompson 1200ae60fdfbSAndrew Thompson for (; ep != ep_end; ep++) { 1201ae60fdfbSAndrew Thompson if ((ep->edesc == NULL) || 1202ae60fdfbSAndrew Thompson (ep->iface_index != iface_index)) { 120302ac6454SAndrew Thompson continue; 120402ac6454SAndrew Thompson } 120502ac6454SAndrew Thompson /* simulate a clear stall from the peer */ 1206bd73b187SAlfred Perlstein usbd_set_endpoint_stall(udev, ep, 0); 120702ac6454SAndrew Thompson } 120802ac6454SAndrew Thompson return (0); 120902ac6454SAndrew Thompson } 121002ac6454SAndrew Thompson 121102ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1212a593f6b8SAndrew Thompson * usb_detach_device_sub 121302ac6454SAndrew Thompson * 121402ac6454SAndrew Thompson * This function will try to detach an USB device. If it fails a panic 121502ac6454SAndrew Thompson * will result. 1216bdd41206SAndrew Thompson * 1217bdd41206SAndrew Thompson * Flag values, see "USB_UNCFG_FLAG_XXX". 121802ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 121902ac6454SAndrew Thompson static void 1220a593f6b8SAndrew Thompson usb_detach_device_sub(struct usb_device *udev, device_t *ppdev, 12218427ed84SHans Petter Selasky char **ppnpinfo, uint8_t flag) 122202ac6454SAndrew Thompson { 122302ac6454SAndrew Thompson device_t dev; 12248427ed84SHans Petter Selasky char *pnpinfo; 122502ac6454SAndrew Thompson int err; 122602ac6454SAndrew Thompson 1227d88688c7SAndrew Thompson dev = *ppdev; 1228d88688c7SAndrew Thompson if (dev) { 122902ac6454SAndrew Thompson /* 123002ac6454SAndrew Thompson * NOTE: It is important to clear "*ppdev" before deleting 123102ac6454SAndrew Thompson * the child due to some device methods being called late 123202ac6454SAndrew Thompson * during the delete process ! 123302ac6454SAndrew Thompson */ 123402ac6454SAndrew Thompson *ppdev = NULL; 123502ac6454SAndrew Thompson 1236cabe79d9SMarius Strobl if (!rebooting) { 123702ac6454SAndrew Thompson device_printf(dev, "at %s, port %d, addr %d " 123802ac6454SAndrew Thompson "(disconnected)\n", 123902ac6454SAndrew Thompson device_get_nameunit(udev->parent_dev), 124002ac6454SAndrew Thompson udev->port_no, udev->address); 1241cabe79d9SMarius Strobl } 124202ac6454SAndrew Thompson 124302ac6454SAndrew Thompson if (device_is_attached(dev)) { 1244ec8f3127SAndrew Thompson if (udev->flags.peer_suspended) { 124502ac6454SAndrew Thompson err = DEVICE_RESUME(dev); 124602ac6454SAndrew Thompson if (err) { 1247767cb2e2SAndrew Thompson device_printf(dev, "Resume failed\n"); 124802ac6454SAndrew Thompson } 124902ac6454SAndrew Thompson } 125002ac6454SAndrew Thompson } 1251d3bf5efcSHans Petter Selasky /* detach and delete child */ 125202ac6454SAndrew Thompson if (device_delete_child(udev->parent_dev, dev)) { 125302ac6454SAndrew Thompson goto error; 125402ac6454SAndrew Thompson } 125502ac6454SAndrew Thompson } 12568427ed84SHans Petter Selasky 12578427ed84SHans Petter Selasky pnpinfo = *ppnpinfo; 12588427ed84SHans Petter Selasky if (pnpinfo != NULL) { 12598427ed84SHans Petter Selasky *ppnpinfo = NULL; 12608427ed84SHans Petter Selasky free(pnpinfo, M_USBDEV); 12618427ed84SHans Petter Selasky } 126202ac6454SAndrew Thompson return; 126302ac6454SAndrew Thompson 126402ac6454SAndrew Thompson error: 126502ac6454SAndrew Thompson /* Detach is not allowed to fail in the USB world */ 12668427ed84SHans Petter Selasky panic("usb_detach_device_sub: A USB driver would not detach\n"); 126702ac6454SAndrew Thompson } 126802ac6454SAndrew Thompson 126902ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1270a593f6b8SAndrew Thompson * usb_detach_device 127102ac6454SAndrew Thompson * 127202ac6454SAndrew Thompson * The following function will detach the matching interfaces. 127302ac6454SAndrew Thompson * This function is NULL safe. 1274bdd41206SAndrew Thompson * 1275bdd41206SAndrew Thompson * Flag values, see "USB_UNCFG_FLAG_XXX". 127602ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 127702ac6454SAndrew Thompson void 1278a593f6b8SAndrew Thompson usb_detach_device(struct usb_device *udev, uint8_t iface_index, 1279bdd41206SAndrew Thompson uint8_t flag) 128002ac6454SAndrew Thompson { 1281760bc48eSAndrew Thompson struct usb_interface *iface; 128202ac6454SAndrew Thompson uint8_t i; 128302ac6454SAndrew Thompson 128402ac6454SAndrew Thompson if (udev == NULL) { 128502ac6454SAndrew Thompson /* nothing to do */ 128602ac6454SAndrew Thompson return; 128702ac6454SAndrew Thompson } 128802ac6454SAndrew Thompson DPRINTFN(4, "udev=%p\n", udev); 128902ac6454SAndrew Thompson 129091cd9240SAndrew Thompson sx_assert(&udev->enum_sx, SA_LOCKED); 129102ac6454SAndrew Thompson 129202ac6454SAndrew Thompson /* 129302ac6454SAndrew Thompson * First detach the child to give the child's detach routine a 129402ac6454SAndrew Thompson * chance to detach the sub-devices in the correct order. 129502ac6454SAndrew Thompson * Then delete the child using "device_delete_child()" which 129602ac6454SAndrew Thompson * will detach all sub-devices from the bottom and upwards! 129702ac6454SAndrew Thompson */ 129802ac6454SAndrew Thompson if (iface_index != USB_IFACE_INDEX_ANY) { 129902ac6454SAndrew Thompson i = iface_index; 130002ac6454SAndrew Thompson iface_index = i + 1; 130102ac6454SAndrew Thompson } else { 130202ac6454SAndrew Thompson i = 0; 130302ac6454SAndrew Thompson iface_index = USB_IFACE_MAX; 130402ac6454SAndrew Thompson } 130502ac6454SAndrew Thompson 130602ac6454SAndrew Thompson /* do the detach */ 130702ac6454SAndrew Thompson 130802ac6454SAndrew Thompson for (; i != iface_index; i++) { 1309a593f6b8SAndrew Thompson iface = usbd_get_iface(udev, i); 131002ac6454SAndrew Thompson if (iface == NULL) { 131102ac6454SAndrew Thompson /* looks like the end of the USB interfaces */ 131202ac6454SAndrew Thompson break; 131302ac6454SAndrew Thompson } 13148427ed84SHans Petter Selasky usb_detach_device_sub(udev, &iface->subdev, 13158427ed84SHans Petter Selasky &iface->pnpinfo, flag); 131602ac6454SAndrew Thompson } 131702ac6454SAndrew Thompson } 131802ac6454SAndrew Thompson 131902ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1320a593f6b8SAndrew Thompson * usb_probe_and_attach_sub 132102ac6454SAndrew Thompson * 132202ac6454SAndrew Thompson * Returns: 132302ac6454SAndrew Thompson * 0: Success 132402ac6454SAndrew Thompson * Else: Failure 132502ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 132602ac6454SAndrew Thompson static uint8_t 1327a593f6b8SAndrew Thompson usb_probe_and_attach_sub(struct usb_device *udev, 1328760bc48eSAndrew Thompson struct usb_attach_arg *uaa) 132902ac6454SAndrew Thompson { 1330760bc48eSAndrew Thompson struct usb_interface *iface; 133102ac6454SAndrew Thompson device_t dev; 133202ac6454SAndrew Thompson int err; 133302ac6454SAndrew Thompson 133402ac6454SAndrew Thompson iface = uaa->iface; 133502ac6454SAndrew Thompson if (iface->parent_iface_index != USB_IFACE_INDEX_ANY) { 133602ac6454SAndrew Thompson /* leave interface alone */ 133702ac6454SAndrew Thompson return (0); 133802ac6454SAndrew Thompson } 133902ac6454SAndrew Thompson dev = iface->subdev; 134002ac6454SAndrew Thompson if (dev) { 134102ac6454SAndrew Thompson /* clean up after module unload */ 134202ac6454SAndrew Thompson 134302ac6454SAndrew Thompson if (device_is_attached(dev)) { 134402ac6454SAndrew Thompson /* already a device there */ 134502ac6454SAndrew Thompson return (0); 134602ac6454SAndrew Thompson } 134702ac6454SAndrew Thompson /* clear "iface->subdev" as early as possible */ 134802ac6454SAndrew Thompson 134902ac6454SAndrew Thompson iface->subdev = NULL; 135002ac6454SAndrew Thompson 135102ac6454SAndrew Thompson if (device_delete_child(udev->parent_dev, dev)) { 135202ac6454SAndrew Thompson /* 135302ac6454SAndrew Thompson * Panic here, else one can get a double call 135402ac6454SAndrew Thompson * to device_detach(). USB devices should 135502ac6454SAndrew Thompson * never fail on detach! 135602ac6454SAndrew Thompson */ 1357767cb2e2SAndrew Thompson panic("device_delete_child() failed\n"); 135802ac6454SAndrew Thompson } 135902ac6454SAndrew Thompson } 136002ac6454SAndrew Thompson if (uaa->temp_dev == NULL) { 136102ac6454SAndrew Thompson /* create a new child */ 13625b56413dSWarner Losh uaa->temp_dev = device_add_child(udev->parent_dev, NULL, DEVICE_UNIT_ANY); 136302ac6454SAndrew Thompson if (uaa->temp_dev == NULL) { 136402ac6454SAndrew Thompson device_printf(udev->parent_dev, 1365767cb2e2SAndrew Thompson "Device creation failed\n"); 136602ac6454SAndrew Thompson return (1); /* failure */ 136702ac6454SAndrew Thompson } 136802ac6454SAndrew Thompson device_set_ivars(uaa->temp_dev, uaa); 136902ac6454SAndrew Thompson device_quiet(uaa->temp_dev); 137002ac6454SAndrew Thompson } 137102ac6454SAndrew Thompson /* 137202ac6454SAndrew Thompson * Set "subdev" before probe and attach so that "devd" gets 137302ac6454SAndrew Thompson * the information it needs. 137402ac6454SAndrew Thompson */ 137502ac6454SAndrew Thompson iface->subdev = uaa->temp_dev; 137602ac6454SAndrew Thompson 137702ac6454SAndrew Thompson if (device_probe_and_attach(iface->subdev) == 0) { 137802ac6454SAndrew Thompson /* 137902ac6454SAndrew Thompson * The USB attach arguments are only available during probe 138002ac6454SAndrew Thompson * and attach ! 138102ac6454SAndrew Thompson */ 138202ac6454SAndrew Thompson uaa->temp_dev = NULL; 138302ac6454SAndrew Thompson device_set_ivars(iface->subdev, NULL); 138402ac6454SAndrew Thompson 1385ec8f3127SAndrew Thompson if (udev->flags.peer_suspended) { 138602ac6454SAndrew Thompson err = DEVICE_SUSPEND(iface->subdev); 138788b4e0abSAndrew Thompson if (err) 138802ac6454SAndrew Thompson device_printf(iface->subdev, "Suspend failed\n"); 138902ac6454SAndrew Thompson } 139002ac6454SAndrew Thompson return (0); /* success */ 139102ac6454SAndrew Thompson } else { 139202ac6454SAndrew Thompson /* No USB driver found */ 139302ac6454SAndrew Thompson iface->subdev = NULL; 139402ac6454SAndrew Thompson } 139502ac6454SAndrew Thompson return (1); /* failure */ 139602ac6454SAndrew Thompson } 139702ac6454SAndrew Thompson 139802ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1399a593f6b8SAndrew Thompson * usbd_set_parent_iface 140002ac6454SAndrew Thompson * 140102ac6454SAndrew Thompson * Using this function will lock the alternate interface setting on an 140202ac6454SAndrew Thompson * interface. It is typically used for multi interface drivers. In USB 140302ac6454SAndrew Thompson * device side mode it is assumed that the alternate interfaces all 140402ac6454SAndrew Thompson * have the same endpoint descriptors. The default parent index value 140502ac6454SAndrew Thompson * is "USB_IFACE_INDEX_ANY". Then the alternate setting value is not 140602ac6454SAndrew Thompson * locked. 140702ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 140802ac6454SAndrew Thompson void 1409a593f6b8SAndrew Thompson usbd_set_parent_iface(struct usb_device *udev, uint8_t iface_index, 141002ac6454SAndrew Thompson uint8_t parent_index) 141102ac6454SAndrew Thompson { 1412760bc48eSAndrew Thompson struct usb_interface *iface; 141302ac6454SAndrew Thompson 14145e055201SHans Petter Selasky if (udev == NULL || iface_index == parent_index) { 1415d9073c1eSHans Petter Selasky /* nothing to do */ 1416d9073c1eSHans Petter Selasky return; 141702ac6454SAndrew Thompson } 1418d9073c1eSHans Petter Selasky iface = usbd_get_iface(udev, iface_index); 1419d9073c1eSHans Petter Selasky if (iface != NULL) 1420d9073c1eSHans Petter Selasky iface->parent_iface_index = parent_index; 142102ac6454SAndrew Thompson } 142202ac6454SAndrew Thompson 142302ac6454SAndrew Thompson static void 1424a593f6b8SAndrew Thompson usb_init_attach_arg(struct usb_device *udev, 1425760bc48eSAndrew Thompson struct usb_attach_arg *uaa) 142602ac6454SAndrew Thompson { 1427d46dc4adSHans Petter Selasky memset(uaa, 0, sizeof(*uaa)); 142802ac6454SAndrew Thompson 142902ac6454SAndrew Thompson uaa->device = udev; 1430f29a0724SAndrew Thompson uaa->usb_mode = udev->flags.usb_mode; 143102ac6454SAndrew Thompson uaa->port = udev->port_no; 14322a4c6157SAndrew Thompson uaa->dev_state = UAA_DEV_READY; 143302ac6454SAndrew Thompson 143402ac6454SAndrew Thompson uaa->info.idVendor = UGETW(udev->ddesc.idVendor); 143502ac6454SAndrew Thompson uaa->info.idProduct = UGETW(udev->ddesc.idProduct); 143602ac6454SAndrew Thompson uaa->info.bcdDevice = UGETW(udev->ddesc.bcdDevice); 143702ac6454SAndrew Thompson uaa->info.bDeviceClass = udev->ddesc.bDeviceClass; 143802ac6454SAndrew Thompson uaa->info.bDeviceSubClass = udev->ddesc.bDeviceSubClass; 143902ac6454SAndrew Thompson uaa->info.bDeviceProtocol = udev->ddesc.bDeviceProtocol; 144002ac6454SAndrew Thompson uaa->info.bConfigIndex = udev->curr_config_index; 144102ac6454SAndrew Thompson uaa->info.bConfigNum = udev->curr_config_no; 144202ac6454SAndrew Thompson } 144302ac6454SAndrew Thompson 144402ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1445a593f6b8SAndrew Thompson * usb_probe_and_attach 144602ac6454SAndrew Thompson * 144702ac6454SAndrew Thompson * This function is called from "uhub_explore_sub()", 1448a593f6b8SAndrew Thompson * "usb_handle_set_config()" and "usb_handle_request()". 144902ac6454SAndrew Thompson * 145002ac6454SAndrew Thompson * Returns: 145102ac6454SAndrew Thompson * 0: Success 145202ac6454SAndrew Thompson * Else: A control transfer failed 145302ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 1454e0a69b51SAndrew Thompson usb_error_t 1455a593f6b8SAndrew Thompson usb_probe_and_attach(struct usb_device *udev, uint8_t iface_index) 145602ac6454SAndrew Thompson { 1457760bc48eSAndrew Thompson struct usb_attach_arg uaa; 1458760bc48eSAndrew Thompson struct usb_interface *iface; 145902ac6454SAndrew Thompson uint8_t i; 146002ac6454SAndrew Thompson uint8_t j; 146102ac6454SAndrew Thompson uint8_t do_unlock; 146202ac6454SAndrew Thompson 146302ac6454SAndrew Thompson if (udev == NULL) { 146402ac6454SAndrew Thompson DPRINTF("udev == NULL\n"); 146502ac6454SAndrew Thompson return (USB_ERR_INVAL); 146602ac6454SAndrew Thompson } 14676950c75fSHans Petter Selasky /* Prevent re-enumeration */ 14686950c75fSHans Petter Selasky do_unlock = usbd_enum_lock(udev); 146902ac6454SAndrew Thompson 147002ac6454SAndrew Thompson if (udev->curr_config_index == USB_UNCONFIG_INDEX) { 147102ac6454SAndrew Thompson /* do nothing - no configuration has been set */ 147202ac6454SAndrew Thompson goto done; 147302ac6454SAndrew Thompson } 147402ac6454SAndrew Thompson /* setup USB attach arguments */ 147502ac6454SAndrew Thompson 1476a593f6b8SAndrew Thompson usb_init_attach_arg(udev, &uaa); 147702ac6454SAndrew Thompson 1478a6b60150SHans Petter Selasky /* 1479a6b60150SHans Petter Selasky * If the whole USB device is targeted, invoke the USB event 1480a6b60150SHans Petter Selasky * handler(s): 1481a6b60150SHans Petter Selasky */ 1482a6b60150SHans Petter Selasky if (iface_index == USB_IFACE_INDEX_ANY) { 1483415bcd89SHans Petter Selasky if (usb_test_quirk(&uaa, UQ_MSC_DYMO_EJECT) != 0 && 1484415bcd89SHans Petter Selasky usb_dymo_eject(udev, 0) == 0) { 1485415bcd89SHans Petter Selasky /* success, mark the udev as disappearing */ 1486415bcd89SHans Petter Selasky uaa.dev_state = UAA_DEV_EJECTING; 1487415bcd89SHans Petter Selasky } 1488415bcd89SHans Petter Selasky 1489a6b60150SHans Petter Selasky EVENTHANDLER_INVOKE(usb_dev_configured, udev, &uaa); 1490a6b60150SHans Petter Selasky 1491a6b60150SHans Petter Selasky if (uaa.dev_state != UAA_DEV_READY) { 1492a6b60150SHans Petter Selasky /* leave device unconfigured */ 1493a6b60150SHans Petter Selasky usb_unconfigure(udev, 0); 1494a6b60150SHans Petter Selasky goto done; 1495a6b60150SHans Petter Selasky } 1496a6b60150SHans Petter Selasky } 1497a6b60150SHans Petter Selasky 149802ac6454SAndrew Thompson /* Check if only one interface should be probed: */ 149902ac6454SAndrew Thompson if (iface_index != USB_IFACE_INDEX_ANY) { 150002ac6454SAndrew Thompson i = iface_index; 150102ac6454SAndrew Thompson j = i + 1; 150202ac6454SAndrew Thompson } else { 150302ac6454SAndrew Thompson i = 0; 150402ac6454SAndrew Thompson j = USB_IFACE_MAX; 150502ac6454SAndrew Thompson } 150602ac6454SAndrew Thompson 150702ac6454SAndrew Thompson /* Do the probe and attach */ 150802ac6454SAndrew Thompson for (; i != j; i++) { 1509a593f6b8SAndrew Thompson iface = usbd_get_iface(udev, i); 151002ac6454SAndrew Thompson if (iface == NULL) { 151102ac6454SAndrew Thompson /* 151202ac6454SAndrew Thompson * Looks like the end of the USB 151302ac6454SAndrew Thompson * interfaces ! 151402ac6454SAndrew Thompson */ 151502ac6454SAndrew Thompson DPRINTFN(2, "end of interfaces " 151602ac6454SAndrew Thompson "at %u\n", i); 151702ac6454SAndrew Thompson break; 151802ac6454SAndrew Thompson } 151902ac6454SAndrew Thompson if (iface->idesc == NULL) { 152002ac6454SAndrew Thompson /* no interface descriptor */ 152102ac6454SAndrew Thompson continue; 152202ac6454SAndrew Thompson } 152302ac6454SAndrew Thompson uaa.iface = iface; 152402ac6454SAndrew Thompson 152502ac6454SAndrew Thompson uaa.info.bInterfaceClass = 152602ac6454SAndrew Thompson iface->idesc->bInterfaceClass; 152702ac6454SAndrew Thompson uaa.info.bInterfaceSubClass = 152802ac6454SAndrew Thompson iface->idesc->bInterfaceSubClass; 152902ac6454SAndrew Thompson uaa.info.bInterfaceProtocol = 153002ac6454SAndrew Thompson iface->idesc->bInterfaceProtocol; 153102ac6454SAndrew Thompson uaa.info.bIfaceIndex = i; 153202ac6454SAndrew Thompson uaa.info.bIfaceNum = 153302ac6454SAndrew Thompson iface->idesc->bInterfaceNumber; 1534bd73b187SAlfred Perlstein uaa.driver_info = 0; /* reset driver_info */ 153502ac6454SAndrew Thompson 153602ac6454SAndrew Thompson DPRINTFN(2, "iclass=%u/%u/%u iindex=%u/%u\n", 153702ac6454SAndrew Thompson uaa.info.bInterfaceClass, 153802ac6454SAndrew Thompson uaa.info.bInterfaceSubClass, 153902ac6454SAndrew Thompson uaa.info.bInterfaceProtocol, 154002ac6454SAndrew Thompson uaa.info.bIfaceIndex, 154102ac6454SAndrew Thompson uaa.info.bIfaceNum); 154202ac6454SAndrew Thompson 1543d43ffe94SHans Petter Selasky usb_probe_and_attach_sub(udev, &uaa); 154402ac6454SAndrew Thompson 1545d43ffe94SHans Petter Selasky /* 1546d43ffe94SHans Petter Selasky * Remove the leftover child, if any, to enforce that 1547d43ffe94SHans Petter Selasky * a new nomatch devd event is generated for the next 1548d43ffe94SHans Petter Selasky * interface if no driver is found: 1549d43ffe94SHans Petter Selasky */ 1550d43ffe94SHans Petter Selasky if (uaa.temp_dev == NULL) 1551d43ffe94SHans Petter Selasky continue; 1552d43ffe94SHans Petter Selasky if (device_delete_child(udev->parent_dev, uaa.temp_dev)) 1553767cb2e2SAndrew Thompson DPRINTFN(0, "device delete child failed\n"); 1554d43ffe94SHans Petter Selasky uaa.temp_dev = NULL; 155502ac6454SAndrew Thompson } 155602ac6454SAndrew Thompson done: 1557cb18f7d1SAlfred Perlstein if (do_unlock) 1558cb18f7d1SAlfred Perlstein usbd_enum_unlock(udev); 155902ac6454SAndrew Thompson return (0); 156002ac6454SAndrew Thompson } 156102ac6454SAndrew Thompson 156202ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1563a593f6b8SAndrew Thompson * usb_suspend_resume_sub 156402ac6454SAndrew Thompson * 156502ac6454SAndrew Thompson * This function is called when the suspend or resume methods should 156602ac6454SAndrew Thompson * be executed on an USB device. 156702ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 156802ac6454SAndrew Thompson static void 1569a593f6b8SAndrew Thompson usb_suspend_resume_sub(struct usb_device *udev, device_t dev, uint8_t do_suspend) 157002ac6454SAndrew Thompson { 157102ac6454SAndrew Thompson int err; 157202ac6454SAndrew Thompson 157302ac6454SAndrew Thompson if (dev == NULL) { 157402ac6454SAndrew Thompson return; 157502ac6454SAndrew Thompson } 157602ac6454SAndrew Thompson if (!device_is_attached(dev)) { 157702ac6454SAndrew Thompson return; 157802ac6454SAndrew Thompson } 157902ac6454SAndrew Thompson if (do_suspend) { 158002ac6454SAndrew Thompson err = DEVICE_SUSPEND(dev); 158102ac6454SAndrew Thompson } else { 158202ac6454SAndrew Thompson err = DEVICE_RESUME(dev); 158302ac6454SAndrew Thompson } 158402ac6454SAndrew Thompson if (err) { 1585767cb2e2SAndrew Thompson device_printf(dev, "%s failed\n", 158602ac6454SAndrew Thompson do_suspend ? "Suspend" : "Resume"); 158702ac6454SAndrew Thompson } 158802ac6454SAndrew Thompson } 158902ac6454SAndrew Thompson 159002ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1591a593f6b8SAndrew Thompson * usb_suspend_resume 159202ac6454SAndrew Thompson * 159302ac6454SAndrew Thompson * The following function will suspend or resume the USB device. 159402ac6454SAndrew Thompson * 159502ac6454SAndrew Thompson * Returns: 159602ac6454SAndrew Thompson * 0: Success 159702ac6454SAndrew Thompson * Else: Failure 159802ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 1599e0a69b51SAndrew Thompson usb_error_t 1600a593f6b8SAndrew Thompson usb_suspend_resume(struct usb_device *udev, uint8_t do_suspend) 160102ac6454SAndrew Thompson { 1602760bc48eSAndrew Thompson struct usb_interface *iface; 160302ac6454SAndrew Thompson uint8_t i; 160402ac6454SAndrew Thompson 160502ac6454SAndrew Thompson if (udev == NULL) { 160602ac6454SAndrew Thompson /* nothing to do */ 160702ac6454SAndrew Thompson return (0); 160802ac6454SAndrew Thompson } 160902ac6454SAndrew Thompson DPRINTFN(4, "udev=%p do_suspend=%d\n", udev, do_suspend); 161002ac6454SAndrew Thompson 16112df1e9a6SAndrew Thompson sx_assert(&udev->sr_sx, SA_LOCKED); 161202ac6454SAndrew Thompson 161302ac6454SAndrew Thompson USB_BUS_LOCK(udev->bus); 161402ac6454SAndrew Thompson /* filter the suspend events */ 1615ec8f3127SAndrew Thompson if (udev->flags.peer_suspended == do_suspend) { 161602ac6454SAndrew Thompson USB_BUS_UNLOCK(udev->bus); 161702ac6454SAndrew Thompson /* nothing to do */ 161802ac6454SAndrew Thompson return (0); 161902ac6454SAndrew Thompson } 1620ec8f3127SAndrew Thompson udev->flags.peer_suspended = do_suspend; 162102ac6454SAndrew Thompson USB_BUS_UNLOCK(udev->bus); 162202ac6454SAndrew Thompson 162302ac6454SAndrew Thompson /* do the suspend or resume */ 162402ac6454SAndrew Thompson 162502ac6454SAndrew Thompson for (i = 0; i != USB_IFACE_MAX; i++) { 1626a593f6b8SAndrew Thompson iface = usbd_get_iface(udev, i); 162702ac6454SAndrew Thompson if (iface == NULL) { 162802ac6454SAndrew Thompson /* looks like the end of the USB interfaces */ 162902ac6454SAndrew Thompson break; 163002ac6454SAndrew Thompson } 1631a593f6b8SAndrew Thompson usb_suspend_resume_sub(udev, iface->subdev, do_suspend); 163202ac6454SAndrew Thompson } 163302ac6454SAndrew Thompson return (0); 163402ac6454SAndrew Thompson } 163502ac6454SAndrew Thompson 163602ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1637a593f6b8SAndrew Thompson * usbd_clear_stall_proc 163802ac6454SAndrew Thompson * 163902ac6454SAndrew Thompson * This function performs generic USB clear stall operations. 164002ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 164102ac6454SAndrew Thompson static void 1642a593f6b8SAndrew Thompson usbd_clear_stall_proc(struct usb_proc_msg *_pm) 164302ac6454SAndrew Thompson { 1644a0d53e0bSHans Petter Selasky struct usb_udev_msg *pm = (void *)_pm; 1645760bc48eSAndrew Thompson struct usb_device *udev = pm->udev; 164602ac6454SAndrew Thompson 164702ac6454SAndrew Thompson /* Change lock */ 164802ac6454SAndrew Thompson USB_BUS_UNLOCK(udev->bus); 16490eb8d462SHans Petter Selasky USB_MTX_LOCK(&udev->device_mtx); 165002ac6454SAndrew Thompson 165102ac6454SAndrew Thompson /* Start clear stall callback */ 16525b3bb704SAndrew Thompson usbd_transfer_start(udev->ctrl_xfer[1]); 165302ac6454SAndrew Thompson 165402ac6454SAndrew Thompson /* Change lock */ 16550eb8d462SHans Petter Selasky USB_MTX_UNLOCK(&udev->device_mtx); 165602ac6454SAndrew Thompson USB_BUS_LOCK(udev->bus); 165702ac6454SAndrew Thompson } 165802ac6454SAndrew Thompson 165902ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1660f54ab96dSHans Petter Selasky * usb_get_langid 1661f54ab96dSHans Petter Selasky * 1662f54ab96dSHans Petter Selasky * This function tries to figure out the USB string language to use. 1663f54ab96dSHans Petter Selasky *------------------------------------------------------------------------*/ 1664f54ab96dSHans Petter Selasky void 1665f54ab96dSHans Petter Selasky usb_get_langid(struct usb_device *udev) 1666f54ab96dSHans Petter Selasky { 1667f54ab96dSHans Petter Selasky uint8_t *scratch_ptr; 1668f54ab96dSHans Petter Selasky uint8_t do_unlock; 1669f54ab96dSHans Petter Selasky int err; 1670f54ab96dSHans Petter Selasky 1671f54ab96dSHans Petter Selasky /* 1672f54ab96dSHans Petter Selasky * Workaround for buggy USB devices. 1673f54ab96dSHans Petter Selasky * 1674f54ab96dSHans Petter Selasky * It appears that some string-less USB chips will crash and 1675f54ab96dSHans Petter Selasky * disappear if any attempts are made to read any string 1676f54ab96dSHans Petter Selasky * descriptors. 1677f54ab96dSHans Petter Selasky * 1678f54ab96dSHans Petter Selasky * Try to detect such chips by checking the strings in the USB 1679f54ab96dSHans Petter Selasky * device descriptor. If no strings are present there we 1680f54ab96dSHans Petter Selasky * simply disable all USB strings. 1681f54ab96dSHans Petter Selasky */ 1682f54ab96dSHans Petter Selasky 1683f54ab96dSHans Petter Selasky /* Protect scratch area */ 1684f54ab96dSHans Petter Selasky do_unlock = usbd_ctrl_lock(udev); 1685f54ab96dSHans Petter Selasky 1686f54ab96dSHans Petter Selasky scratch_ptr = udev->scratch.data; 1687f54ab96dSHans Petter Selasky 1688f54ab96dSHans Petter Selasky if (udev->flags.no_strings) { 1689f54ab96dSHans Petter Selasky err = USB_ERR_INVAL; 1690f54ab96dSHans Petter Selasky } else if (udev->ddesc.iManufacturer || 1691f54ab96dSHans Petter Selasky udev->ddesc.iProduct || 1692f54ab96dSHans Petter Selasky udev->ddesc.iSerialNumber) { 1693f54ab96dSHans Petter Selasky /* read out the language ID string */ 1694f54ab96dSHans Petter Selasky err = usbd_req_get_string_desc(udev, NULL, 1695f54ab96dSHans Petter Selasky (char *)scratch_ptr, 4, 0, USB_LANGUAGE_TABLE); 1696f54ab96dSHans Petter Selasky } else { 1697f54ab96dSHans Petter Selasky err = USB_ERR_INVAL; 1698f54ab96dSHans Petter Selasky } 1699f54ab96dSHans Petter Selasky 1700f54ab96dSHans Petter Selasky if (err || (scratch_ptr[0] < 4)) { 1701f54ab96dSHans Petter Selasky udev->flags.no_strings = 1; 1702f54ab96dSHans Petter Selasky } else { 1703f54ab96dSHans Petter Selasky uint16_t langid; 1704f54ab96dSHans Petter Selasky uint16_t pref; 1705f54ab96dSHans Petter Selasky uint16_t mask; 1706f54ab96dSHans Petter Selasky uint8_t x; 1707f54ab96dSHans Petter Selasky 1708f54ab96dSHans Petter Selasky /* load preferred value and mask */ 1709f54ab96dSHans Petter Selasky pref = usb_lang_id; 1710f54ab96dSHans Petter Selasky mask = usb_lang_mask; 1711f54ab96dSHans Petter Selasky 1712f54ab96dSHans Petter Selasky /* align length correctly */ 1713f54ab96dSHans Petter Selasky scratch_ptr[0] &= ~1U; 1714f54ab96dSHans Petter Selasky 1715f54ab96dSHans Petter Selasky /* fix compiler warning */ 1716f54ab96dSHans Petter Selasky langid = 0; 1717f54ab96dSHans Petter Selasky 1718f54ab96dSHans Petter Selasky /* search for preferred language */ 1719f54ab96dSHans Petter Selasky for (x = 2; x < scratch_ptr[0]; x += 2) { 1720f54ab96dSHans Petter Selasky langid = UGETW(scratch_ptr + x); 1721f54ab96dSHans Petter Selasky if ((langid & mask) == pref) 1722f54ab96dSHans Petter Selasky break; 1723f54ab96dSHans Petter Selasky } 1724f54ab96dSHans Petter Selasky if (x >= scratch_ptr[0]) { 1725f54ab96dSHans Petter Selasky /* pick the first language as the default */ 1726f54ab96dSHans Petter Selasky DPRINTFN(1, "Using first language\n"); 1727f54ab96dSHans Petter Selasky langid = UGETW(scratch_ptr + 2); 1728f54ab96dSHans Petter Selasky } 1729f54ab96dSHans Petter Selasky 1730f54ab96dSHans Petter Selasky DPRINTFN(1, "Language selected: 0x%04x\n", langid); 1731f54ab96dSHans Petter Selasky udev->langid = langid; 1732f54ab96dSHans Petter Selasky } 1733f54ab96dSHans Petter Selasky 1734f54ab96dSHans Petter Selasky if (do_unlock) 1735f54ab96dSHans Petter Selasky usbd_ctrl_unlock(udev); 1736f54ab96dSHans Petter Selasky } 1737f54ab96dSHans Petter Selasky 1738f54ab96dSHans Petter Selasky /*------------------------------------------------------------------------* 1739a593f6b8SAndrew Thompson * usb_alloc_device 174002ac6454SAndrew Thompson * 174102ac6454SAndrew Thompson * This function allocates a new USB device. This function is called 174202ac6454SAndrew Thompson * when a new device has been put in the powered state, but not yet in 174302ac6454SAndrew Thompson * the addressed state. Get initial descriptor, set the address, get 174402ac6454SAndrew Thompson * full descriptor and get strings. 174502ac6454SAndrew Thompson * 174602ac6454SAndrew Thompson * Return values: 174702ac6454SAndrew Thompson * 0: Failure 174802ac6454SAndrew Thompson * Else: Success 174902ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 1750760bc48eSAndrew Thompson struct usb_device * 1751a593f6b8SAndrew Thompson usb_alloc_device(device_t parent_dev, struct usb_bus *bus, 1752760bc48eSAndrew Thompson struct usb_device *parent_hub, uint8_t depth, uint8_t port_index, 17538d2dd5ddSAndrew Thompson uint8_t port_no, enum usb_dev_speed speed, enum usb_hc_mode mode) 175402ac6454SAndrew Thompson { 1755760bc48eSAndrew Thompson struct usb_attach_arg uaa; 1756760bc48eSAndrew Thompson struct usb_device *udev; 1757760bc48eSAndrew Thompson struct usb_device *adev; 1758760bc48eSAndrew Thompson struct usb_device *hub; 1759e0a69b51SAndrew Thompson usb_error_t err; 176002ac6454SAndrew Thompson uint8_t device_index; 17612a4c6157SAndrew Thompson uint8_t config_index; 17622a4c6157SAndrew Thompson uint8_t config_quirk; 17632a4c6157SAndrew Thompson uint8_t set_config_failed; 176402ac6454SAndrew Thompson 176502ac6454SAndrew Thompson DPRINTF("parent_dev=%p, bus=%p, parent_hub=%p, depth=%u, " 1766f29a0724SAndrew Thompson "port_index=%u, port_no=%u, speed=%u, usb_mode=%u\n", 176702ac6454SAndrew Thompson parent_dev, bus, parent_hub, depth, port_index, port_no, 1768f29a0724SAndrew Thompson speed, mode); 176902ac6454SAndrew Thompson 177002ac6454SAndrew Thompson /* 177102ac6454SAndrew Thompson * Find an unused device index. In USB Host mode this is the 177202ac6454SAndrew Thompson * same as the device address. 177302ac6454SAndrew Thompson * 177402ac6454SAndrew Thompson * Device index zero is not used and device index 1 should 177502ac6454SAndrew Thompson * always be the root hub. 177602ac6454SAndrew Thompson */ 177702ac6454SAndrew Thompson for (device_index = USB_ROOT_HUB_ADDR; 177802ac6454SAndrew Thompson (device_index != bus->devices_max) && 177902ac6454SAndrew Thompson (bus->devices[device_index] != NULL); 178002ac6454SAndrew Thompson device_index++) /* nop */; 178102ac6454SAndrew Thompson 178202ac6454SAndrew Thompson if (device_index == bus->devices_max) { 178302ac6454SAndrew Thompson device_printf(bus->bdev, 1784767cb2e2SAndrew Thompson "No free USB device index for new device\n"); 178502ac6454SAndrew Thompson return (NULL); 178602ac6454SAndrew Thompson } 178702ac6454SAndrew Thompson 178802ac6454SAndrew Thompson if (depth > 0x10) { 178902ac6454SAndrew Thompson device_printf(bus->bdev, 1790767cb2e2SAndrew Thompson "Invalid device depth\n"); 179102ac6454SAndrew Thompson return (NULL); 179202ac6454SAndrew Thompson } 179302ac6454SAndrew Thompson udev = malloc(sizeof(*udev), M_USB, M_WAITOK | M_ZERO); 179494140f47SMark Johnston #if (USB_HAVE_MALLOC_WAITOK == 0) 179502ac6454SAndrew Thompson if (udev == NULL) { 179602ac6454SAndrew Thompson return (NULL); 179702ac6454SAndrew Thompson } 179894140f47SMark Johnston #endif 179902ac6454SAndrew Thompson /* initialise our SX-lock */ 180091cd9240SAndrew Thompson sx_init_flags(&udev->enum_sx, "USB config SX lock", SX_DUPOK); 1801a6b60150SHans Petter Selasky sx_init_flags(&udev->sr_sx, "USB suspend and resume SX lock", SX_NOWITNESS); 180264cb5e2aSHans Petter Selasky sx_init_flags(&udev->ctrl_sx, "USB control transfer SX lock", SX_DUPOK); 180302ac6454SAndrew Thompson 180491cd9240SAndrew Thompson cv_init(&udev->ctrlreq_cv, "WCTRL"); 180591cd9240SAndrew Thompson cv_init(&udev->ref_cv, "UGONE"); 180602ac6454SAndrew Thompson 180702ac6454SAndrew Thompson /* initialise our mutex */ 180891cd9240SAndrew Thompson mtx_init(&udev->device_mtx, "USB device mutex", NULL, MTX_DEF); 180902ac6454SAndrew Thompson 181002ac6454SAndrew Thompson /* initialise generic clear stall */ 1811a593f6b8SAndrew Thompson udev->cs_msg[0].hdr.pm_callback = &usbd_clear_stall_proc; 181202ac6454SAndrew Thompson udev->cs_msg[0].udev = udev; 1813a593f6b8SAndrew Thompson udev->cs_msg[1].hdr.pm_callback = &usbd_clear_stall_proc; 181402ac6454SAndrew Thompson udev->cs_msg[1].udev = udev; 181502ac6454SAndrew Thompson 181602ac6454SAndrew Thompson /* initialise some USB device fields */ 181702ac6454SAndrew Thompson udev->parent_hub = parent_hub; 181802ac6454SAndrew Thompson udev->parent_dev = parent_dev; 181902ac6454SAndrew Thompson udev->port_index = port_index; 182002ac6454SAndrew Thompson udev->port_no = port_no; 182102ac6454SAndrew Thompson udev->depth = depth; 182202ac6454SAndrew Thompson udev->bus = bus; 182302ac6454SAndrew Thompson udev->address = USB_START_ADDR; /* default value */ 1824e0a69b51SAndrew Thompson udev->plugtime = (usb_ticks_t)ticks; 182502ac6454SAndrew Thompson /* 182602ac6454SAndrew Thompson * We need to force the power mode to "on" because there are plenty 182702ac6454SAndrew Thompson * of USB devices out there that do not work very well with 182802ac6454SAndrew Thompson * automatic suspend and resume! 182902ac6454SAndrew Thompson */ 1830d2d71ce7SAndrew Thompson udev->power_mode = usbd_filter_power_mode(udev, USB_POWER_MODE_ON); 183102ac6454SAndrew Thompson udev->pwr_save.last_xfer_time = ticks; 183202ac6454SAndrew Thompson /* we are not ready yet */ 183302ac6454SAndrew Thompson udev->refcount = 1; 183402ac6454SAndrew Thompson 183502ac6454SAndrew Thompson /* set up default endpoint descriptor */ 18365b3bb704SAndrew Thompson udev->ctrl_ep_desc.bLength = sizeof(udev->ctrl_ep_desc); 18375b3bb704SAndrew Thompson udev->ctrl_ep_desc.bDescriptorType = UDESC_ENDPOINT; 18385b3bb704SAndrew Thompson udev->ctrl_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT; 18395b3bb704SAndrew Thompson udev->ctrl_ep_desc.bmAttributes = UE_CONTROL; 18405b3bb704SAndrew Thompson udev->ctrl_ep_desc.wMaxPacketSize[0] = USB_MAX_IPACKET; 18415b3bb704SAndrew Thompson udev->ctrl_ep_desc.wMaxPacketSize[1] = 0; 18425b3bb704SAndrew Thompson udev->ctrl_ep_desc.bInterval = 0; 1843963169b4SHans Petter Selasky 1844963169b4SHans Petter Selasky /* set up default endpoint companion descriptor */ 1845963169b4SHans Petter Selasky udev->ctrl_ep_comp_desc.bLength = sizeof(udev->ctrl_ep_comp_desc); 1846963169b4SHans Petter Selasky udev->ctrl_ep_comp_desc.bDescriptorType = UDESC_ENDPOINT_SS_COMP; 1847963169b4SHans Petter Selasky 184802ac6454SAndrew Thompson udev->ddesc.bMaxPacketSize = USB_MAX_IPACKET; 184902ac6454SAndrew Thompson 185002ac6454SAndrew Thompson udev->speed = speed; 1851f29a0724SAndrew Thompson udev->flags.usb_mode = mode; 185202ac6454SAndrew Thompson 185302ac6454SAndrew Thompson /* search for our High Speed USB HUB, if any */ 185402ac6454SAndrew Thompson 185502ac6454SAndrew Thompson adev = udev; 185602ac6454SAndrew Thompson hub = udev->parent_hub; 185702ac6454SAndrew Thompson 185802ac6454SAndrew Thompson while (hub) { 185902ac6454SAndrew Thompson if (hub->speed == USB_SPEED_HIGH) { 186002ac6454SAndrew Thompson udev->hs_hub_addr = hub->address; 1861495f25ceSAndrew Thompson udev->parent_hs_hub = hub; 186202ac6454SAndrew Thompson udev->hs_port_no = adev->port_no; 186302ac6454SAndrew Thompson break; 186402ac6454SAndrew Thompson } 186502ac6454SAndrew Thompson adev = hub; 186602ac6454SAndrew Thompson hub = hub->parent_hub; 186702ac6454SAndrew Thompson } 186802ac6454SAndrew Thompson 1869ae60fdfbSAndrew Thompson /* init the default endpoint */ 1870a593f6b8SAndrew Thompson usb_init_endpoint(udev, 0, 18715b3bb704SAndrew Thompson &udev->ctrl_ep_desc, 1872963169b4SHans Petter Selasky &udev->ctrl_ep_comp_desc, 18735b3bb704SAndrew Thompson &udev->ctrl_ep); 187402ac6454SAndrew Thompson 187502ac6454SAndrew Thompson /* set device index */ 187602ac6454SAndrew Thompson udev->device_index = device_index; 187702ac6454SAndrew Thompson 18788755859aSAndrew Thompson #if USB_HAVE_UGEN 18790f6e8f93SAndrew Thompson /* Create ugen name */ 1880ee3e3ff5SAndrew Thompson snprintf(udev->ugen_name, sizeof(udev->ugen_name), 1881ee3e3ff5SAndrew Thompson USB_GENERIC_NAME "%u.%u", device_get_unit(bus->bdev), 1882ee3e3ff5SAndrew Thompson device_index); 1883*5b8f97d8SBjoern A. Zeeb SLIST_INIT(&udev->pd_list); 18848755859aSAndrew Thompson 18850f6e8f93SAndrew Thompson /* Create the control endpoint device */ 18862ffd5fdcSHans Petter Selasky udev->ctrl_dev = usb_make_dev(udev, NULL, 0, 0, 18872ffd5fdcSHans Petter Selasky FREAD|FWRITE, UID_ROOT, GID_OPERATOR, 0600); 18880f6e8f93SAndrew Thompson 18890f6e8f93SAndrew Thompson /* Create a link from /dev/ugenX.X to the default endpoint */ 18902ffd5fdcSHans Petter Selasky if (udev->ctrl_dev != NULL) 18912ffd5fdcSHans Petter Selasky make_dev_alias(udev->ctrl_dev->cdev, "%s", udev->ugen_name); 18920f6e8f93SAndrew Thompson #endif 1893963169b4SHans Petter Selasky /* Initialise device */ 1894963169b4SHans Petter Selasky if (bus->methods->device_init != NULL) { 1895963169b4SHans Petter Selasky err = (bus->methods->device_init) (udev); 1896963169b4SHans Petter Selasky if (err != 0) { 1897963169b4SHans Petter Selasky DPRINTFN(0, "device init %d failed " 1898963169b4SHans Petter Selasky "(%s, ignored)\n", device_index, 1899963169b4SHans Petter Selasky usbd_errstr(err)); 1900963169b4SHans Petter Selasky goto done; 1901963169b4SHans Petter Selasky } 1902963169b4SHans Petter Selasky } 1903963169b4SHans Petter Selasky /* set powered device state after device init is complete */ 1904963169b4SHans Petter Selasky usb_set_device_state(udev, USB_STATE_POWERED); 1905963169b4SHans Petter Selasky 1906f29a0724SAndrew Thompson if (udev->flags.usb_mode == USB_MODE_HOST) { 1907a593f6b8SAndrew Thompson err = usbd_req_set_address(udev, NULL, device_index); 190802ac6454SAndrew Thompson 1909963169b4SHans Petter Selasky /* 1910963169b4SHans Petter Selasky * This is the new USB device address from now on, if 1911963169b4SHans Petter Selasky * the set address request didn't set it already. 1912963169b4SHans Petter Selasky */ 1913963169b4SHans Petter Selasky if (udev->address == USB_START_ADDR) 191402ac6454SAndrew Thompson udev->address = device_index; 191502ac6454SAndrew Thompson 191602ac6454SAndrew Thompson /* 191702ac6454SAndrew Thompson * We ignore any set-address errors, hence there are 191802ac6454SAndrew Thompson * buggy USB devices out there that actually receive 191902ac6454SAndrew Thompson * the SETUP PID, but manage to set the address before 192002ac6454SAndrew Thompson * the STATUS stage is ACK'ed. If the device responds 192102ac6454SAndrew Thompson * to the subsequent get-descriptor at the new 192202ac6454SAndrew Thompson * address, then we know that the set-address command 192302ac6454SAndrew Thompson * was successful. 192402ac6454SAndrew Thompson */ 192502ac6454SAndrew Thompson if (err) { 192602ac6454SAndrew Thompson DPRINTFN(0, "set address %d failed " 192703797f33SAndrew Thompson "(%s, ignored)\n", udev->address, 1928a593f6b8SAndrew Thompson usbd_errstr(err)); 192902ac6454SAndrew Thompson } 193002ac6454SAndrew Thompson } else { 193102ac6454SAndrew Thompson /* We are not self powered */ 193202ac6454SAndrew Thompson udev->flags.self_powered = 0; 193302ac6454SAndrew Thompson 193402ac6454SAndrew Thompson /* Set unconfigured state */ 193502ac6454SAndrew Thompson udev->curr_config_no = USB_UNCONFIG_NO; 193602ac6454SAndrew Thompson udev->curr_config_index = USB_UNCONFIG_INDEX; 193702ac6454SAndrew Thompson 193802ac6454SAndrew Thompson /* Setup USB descriptors */ 1939a593f6b8SAndrew Thompson err = (usb_temp_setup_by_index_p) (udev, usb_template); 194002ac6454SAndrew Thompson if (err) { 19416be75992SEdward Tomasz Napierala DPRINTFN(0, "setting up USB template failed - " 19426be75992SEdward Tomasz Napierala "usb_template(4) not loaded?\n"); 194302ac6454SAndrew Thompson goto done; 194402ac6454SAndrew Thompson } 194502ac6454SAndrew Thompson } 1946a593f6b8SAndrew Thompson usb_set_device_state(udev, USB_STATE_ADDRESSED); 194702ac6454SAndrew Thompson 1948963169b4SHans Petter Selasky /* setup the device descriptor and the initial "wMaxPacketSize" */ 1949963169b4SHans Petter Selasky err = usbd_setup_device_desc(udev, NULL); 1950963169b4SHans Petter Selasky 1951963169b4SHans Petter Selasky if (err != 0) { 195223de050bSHans Petter Selasky /* try to enumerate two more times */ 1953a593f6b8SAndrew Thompson err = usbd_req_re_enumerate(udev, NULL); 195423de050bSHans Petter Selasky if (err != 0) { 195523de050bSHans Petter Selasky err = usbd_req_re_enumerate(udev, NULL); 195623de050bSHans Petter Selasky if (err != 0) { 195702ac6454SAndrew Thompson goto done; 195802ac6454SAndrew Thompson } 195923de050bSHans Petter Selasky } 196023de050bSHans Petter Selasky } 196102ac6454SAndrew Thompson 196202ac6454SAndrew Thompson /* 196302ac6454SAndrew Thompson * Setup temporary USB attach args so that we can figure out some 196402ac6454SAndrew Thompson * basic quirks for this device. 196502ac6454SAndrew Thompson */ 1966a593f6b8SAndrew Thompson usb_init_attach_arg(udev, &uaa); 196702ac6454SAndrew Thompson 1968a593f6b8SAndrew Thompson if (usb_test_quirk(&uaa, UQ_BUS_POWERED)) { 196902ac6454SAndrew Thompson udev->flags.uq_bus_powered = 1; 197002ac6454SAndrew Thompson } 1971a593f6b8SAndrew Thompson if (usb_test_quirk(&uaa, UQ_NO_STRINGS)) { 197202ac6454SAndrew Thompson udev->flags.no_strings = 1; 197302ac6454SAndrew Thompson } 19746950c75fSHans Petter Selasky 1975f54ab96dSHans Petter Selasky usb_get_langid(udev); 19766950c75fSHans Petter Selasky 197702ac6454SAndrew Thompson /* assume 100mA bus powered for now. Changed when configured. */ 197802ac6454SAndrew Thompson udev->power = USB_MIN_POWER; 1979c86aead6SAndrew Thompson /* fetch the vendor and product strings from the device */ 1980f54ab96dSHans Petter Selasky usb_set_device_strings(udev); 198102ac6454SAndrew Thompson 19822a4c6157SAndrew Thompson if (udev->flags.usb_mode == USB_MODE_DEVICE) { 19832a4c6157SAndrew Thompson /* USB device mode setup is complete */ 19842a4c6157SAndrew Thompson err = 0; 19852a4c6157SAndrew Thompson goto config_done; 19862a4c6157SAndrew Thompson } 198702ac6454SAndrew Thompson 198802ac6454SAndrew Thompson /* 198902ac6454SAndrew Thompson * Most USB devices should attach to config index 0 by 199002ac6454SAndrew Thompson * default 199102ac6454SAndrew Thompson */ 1992a593f6b8SAndrew Thompson if (usb_test_quirk(&uaa, UQ_CFG_INDEX_0)) { 199302ac6454SAndrew Thompson config_index = 0; 199402ac6454SAndrew Thompson config_quirk = 1; 1995a593f6b8SAndrew Thompson } else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_1)) { 199602ac6454SAndrew Thompson config_index = 1; 199702ac6454SAndrew Thompson config_quirk = 1; 1998a593f6b8SAndrew Thompson } else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_2)) { 199902ac6454SAndrew Thompson config_index = 2; 200002ac6454SAndrew Thompson config_quirk = 1; 2001a593f6b8SAndrew Thompson } else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_3)) { 200202ac6454SAndrew Thompson config_index = 3; 200302ac6454SAndrew Thompson config_quirk = 1; 2004a593f6b8SAndrew Thompson } else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_4)) { 200502ac6454SAndrew Thompson config_index = 4; 200602ac6454SAndrew Thompson config_quirk = 1; 200702ac6454SAndrew Thompson } else { 200802ac6454SAndrew Thompson config_index = 0; 200902ac6454SAndrew Thompson config_quirk = 0; 201002ac6454SAndrew Thompson } 201102ac6454SAndrew Thompson 20122a4c6157SAndrew Thompson set_config_failed = 0; 201302ac6454SAndrew Thompson repeat_set_config: 201402ac6454SAndrew Thompson 201502ac6454SAndrew Thompson DPRINTF("setting config %u\n", config_index); 201602ac6454SAndrew Thompson 201702ac6454SAndrew Thompson /* get the USB device configured */ 2018a593f6b8SAndrew Thompson err = usbd_set_config_index(udev, config_index); 201902ac6454SAndrew Thompson if (err) { 202002ac6454SAndrew Thompson if (udev->ddesc.bNumConfigurations != 0) { 202102ac6454SAndrew Thompson if (!set_config_failed) { 202202ac6454SAndrew Thompson set_config_failed = 1; 202302ac6454SAndrew Thompson /* XXX try to re-enumerate the device */ 20242a4c6157SAndrew Thompson err = usbd_req_re_enumerate(udev, NULL); 202502ac6454SAndrew Thompson if (err == 0) 202602ac6454SAndrew Thompson goto repeat_set_config; 202702ac6454SAndrew Thompson } 20282a4c6157SAndrew Thompson DPRINTFN(0, "Failure selecting configuration index %u:" 20292a4c6157SAndrew Thompson "%s, port %u, addr %u (ignored)\n", 2030a593f6b8SAndrew Thompson config_index, usbd_errstr(err), udev->port_no, 203102ac6454SAndrew Thompson udev->address); 203202ac6454SAndrew Thompson } 203302ac6454SAndrew Thompson /* 20342a4c6157SAndrew Thompson * Some USB devices do not have any configurations. Ignore any 20352a4c6157SAndrew Thompson * set config failures! 203602ac6454SAndrew Thompson */ 203702ac6454SAndrew Thompson err = 0; 20382a4c6157SAndrew Thompson goto config_done; 20392a4c6157SAndrew Thompson } 20402a4c6157SAndrew Thompson if (!config_quirk && config_index + 1 < udev->ddesc.bNumConfigurations) { 204102ac6454SAndrew Thompson if ((udev->cdesc->bNumInterface < 2) && 20422a4c6157SAndrew Thompson usbd_get_no_descriptors(udev->cdesc, UDESC_ENDPOINT) == 0) { 20432a4c6157SAndrew Thompson DPRINTFN(0, "Found no endpoints, trying next config\n"); 204402ac6454SAndrew Thompson config_index++; 204502ac6454SAndrew Thompson goto repeat_set_config; 204602ac6454SAndrew Thompson } 204715d4edd3SHans Petter Selasky #if USB_HAVE_MSCTEST 20483ee94386SKornel Duleba if (config_index == 0 && 20493ee94386SKornel Duleba usb_test_quirk(&uaa, UQ_MSC_NO_INQUIRY) == 0) { 205002ac6454SAndrew Thompson /* 205102ac6454SAndrew Thompson * Try to figure out if we have an 205202ac6454SAndrew Thompson * auto-install disk there: 205302ac6454SAndrew Thompson */ 2054d84a79e7SAndrew Thompson if (usb_iface_is_cdrom(udev, 0)) { 205502ac6454SAndrew Thompson DPRINTFN(0, "Found possible auto-install " 205602ac6454SAndrew Thompson "disk (trying next config)\n"); 205702ac6454SAndrew Thompson config_index++; 205802ac6454SAndrew Thompson goto repeat_set_config; 205902ac6454SAndrew Thompson } 206002ac6454SAndrew Thompson } 206115d4edd3SHans Petter Selasky #endif 20622a4c6157SAndrew Thompson } 206315d4edd3SHans Petter Selasky #if USB_HAVE_MSCTEST 2064d46dc4adSHans Petter Selasky if (set_config_failed == 0 && config_index == 0 && 20657520b888SHans Petter Selasky usb_test_quirk(&uaa, UQ_MSC_NO_START_STOP) == 0 && 20667520b888SHans Petter Selasky usb_test_quirk(&uaa, UQ_MSC_NO_PREVENT_ALLOW) == 0 && 20679157ad4bSHans Petter Selasky usb_test_quirk(&uaa, UQ_MSC_NO_SYNC_CACHE) == 0 && 20687520b888SHans Petter Selasky usb_test_quirk(&uaa, UQ_MSC_NO_TEST_UNIT_READY) == 0 && 20693ee94386SKornel Duleba usb_test_quirk(&uaa, UQ_MSC_NO_GETMAXLUN) == 0 && 20703ee94386SKornel Duleba usb_test_quirk(&uaa, UQ_MSC_NO_INQUIRY) == 0) { 2071d46dc4adSHans Petter Selasky /* 2072d46dc4adSHans Petter Selasky * Try to figure out if there are any MSC quirks we 2073d46dc4adSHans Petter Selasky * should apply automatically: 2074d46dc4adSHans Petter Selasky */ 20757520b888SHans Petter Selasky err = usb_msc_auto_quirk(udev, 0, &uaa); 2076d46dc4adSHans Petter Selasky 2077d46dc4adSHans Petter Selasky if (err != 0) { 2078d46dc4adSHans Petter Selasky set_config_failed = 1; 2079d46dc4adSHans Petter Selasky goto repeat_set_config; 2080d46dc4adSHans Petter Selasky } 2081d46dc4adSHans Petter Selasky } 208215d4edd3SHans Petter Selasky #endif 208302ac6454SAndrew Thompson 20842a4c6157SAndrew Thompson config_done: 208502ac6454SAndrew Thompson DPRINTF("new dev (addr %d), udev=%p, parent_hub=%p\n", 208602ac6454SAndrew Thompson udev->address, udev, udev->parent_hub); 208702ac6454SAndrew Thompson 208802ac6454SAndrew Thompson /* register our device - we are ready */ 2089a593f6b8SAndrew Thompson usb_bus_port_set_device(bus, parent_hub ? 209002ac6454SAndrew Thompson parent_hub->hub->ports + port_index : NULL, udev, device_index); 209102ac6454SAndrew Thompson 20920f6e8f93SAndrew Thompson #if USB_HAVE_UGEN 20938755859aSAndrew Thompson /* Symlink the ugen device name */ 2094a593f6b8SAndrew Thompson udev->ugen_symlink = usb_alloc_symlink(udev->ugen_name); 20958755859aSAndrew Thompson 20968755859aSAndrew Thompson /* Announce device */ 209788f9a4ffSEdward Tomasz Napierala printf("%s: <%s %s> at %s\n", udev->ugen_name, 209888f9a4ffSEdward Tomasz Napierala usb_get_manufacturer(udev), usb_get_product(udev), 209902ac6454SAndrew Thompson device_get_nameunit(udev->bus->bdev)); 21008bb77249SHans Petter Selasky #endif 21017efaaa9aSAndrew Thompson 21028bb77249SHans Petter Selasky #if USB_HAVE_DEVCTL 21033acd904dSAndrew Thompson usb_notify_addq("ATTACH", udev); 21048755859aSAndrew Thompson #endif 210502ac6454SAndrew Thompson done: 210602ac6454SAndrew Thompson if (err) { 2107d88688c7SAndrew Thompson /* 2108d88688c7SAndrew Thompson * Free USB device and all subdevices, if any. 2109d88688c7SAndrew Thompson */ 2110d88688c7SAndrew Thompson usb_free_device(udev, 0); 211102ac6454SAndrew Thompson udev = NULL; 211202ac6454SAndrew Thompson } 211302ac6454SAndrew Thompson return (udev); 211402ac6454SAndrew Thompson } 211502ac6454SAndrew Thompson 21160f6e8f93SAndrew Thompson #if USB_HAVE_UGEN 21172ffd5fdcSHans Petter Selasky struct usb_fs_privdata * 21182ffd5fdcSHans Petter Selasky usb_make_dev(struct usb_device *udev, const char *devname, int ep, 21192ffd5fdcSHans Petter Selasky int fi, int rwmode, uid_t uid, gid_t gid, int mode) 2120ee3e3ff5SAndrew Thompson { 2121760bc48eSAndrew Thompson struct usb_fs_privdata* pd; 2122ac3490fdSHans Petter Selasky struct make_dev_args args; 21232ffd5fdcSHans Petter Selasky char buffer[32]; 2124ee3e3ff5SAndrew Thompson 2125ee3e3ff5SAndrew Thompson /* Store information to locate ourselves again later */ 2126760bc48eSAndrew Thompson pd = malloc(sizeof(struct usb_fs_privdata), M_USBDEV, 2127ee3e3ff5SAndrew Thompson M_WAITOK | M_ZERO); 2128ee3e3ff5SAndrew Thompson pd->bus_index = device_get_unit(udev->bus->bdev); 2129ee3e3ff5SAndrew Thompson pd->dev_index = udev->device_index; 2130ee3e3ff5SAndrew Thompson pd->ep_addr = ep; 21312ffd5fdcSHans Petter Selasky pd->fifo_index = fi; 21322ffd5fdcSHans Petter Selasky pd->mode = rwmode; 2133ee3e3ff5SAndrew Thompson 2134ee3e3ff5SAndrew Thompson /* Now, create the device itself */ 21352ffd5fdcSHans Petter Selasky if (devname == NULL) { 21362ffd5fdcSHans Petter Selasky devname = buffer; 21372ffd5fdcSHans Petter Selasky snprintf(buffer, sizeof(buffer), USB_DEVICE_DIR "/%u.%u.%u", 2138f35aaff0SAndrew Thompson pd->bus_index, pd->dev_index, pd->ep_addr); 21392ffd5fdcSHans Petter Selasky } 21402ffd5fdcSHans Petter Selasky 2141ac3490fdSHans Petter Selasky /* Setup arguments for make_dev_s() */ 2142ac3490fdSHans Petter Selasky make_dev_args_init(&args); 2143ac3490fdSHans Petter Selasky args.mda_devsw = &usb_devsw; 2144ac3490fdSHans Petter Selasky args.mda_uid = uid; 2145ac3490fdSHans Petter Selasky args.mda_gid = gid; 2146ac3490fdSHans Petter Selasky args.mda_mode = mode; 2147ac3490fdSHans Petter Selasky args.mda_si_drv1 = pd; 21482ffd5fdcSHans Petter Selasky 2149ac3490fdSHans Petter Selasky if (make_dev_s(&args, &pd->cdev, "%s", devname) != 0) { 21502ffd5fdcSHans Petter Selasky DPRINTFN(0, "Failed to create device %s\n", devname); 21512ffd5fdcSHans Petter Selasky free(pd, M_USBDEV); 21522ffd5fdcSHans Petter Selasky return (NULL); 21532ffd5fdcSHans Petter Selasky } 21542ffd5fdcSHans Petter Selasky return (pd); 21552ffd5fdcSHans Petter Selasky } 21562ffd5fdcSHans Petter Selasky 21572ffd5fdcSHans Petter Selasky void 2158b78e84d1SHans Petter Selasky usb_destroy_dev_sync(struct usb_fs_privdata *pd) 21592ffd5fdcSHans Petter Selasky { 2160b78e84d1SHans Petter Selasky DPRINTFN(1, "Destroying device at ugen%d.%d\n", 2161b78e84d1SHans Petter Selasky pd->bus_index, pd->dev_index); 21622ffd5fdcSHans Petter Selasky 2163b78e84d1SHans Petter Selasky /* 2164b78e84d1SHans Petter Selasky * Destroy character device synchronously. After this 2165b78e84d1SHans Petter Selasky * all system calls are returned. Can block. 2166b78e84d1SHans Petter Selasky */ 21672ffd5fdcSHans Petter Selasky destroy_dev(pd->cdev); 21682ffd5fdcSHans Petter Selasky 21692ffd5fdcSHans Petter Selasky free(pd, M_USBDEV); 2170ee3e3ff5SAndrew Thompson } 2171ee3e3ff5SAndrew Thompson 2172b78e84d1SHans Petter Selasky void 2173b78e84d1SHans Petter Selasky usb_destroy_dev(struct usb_fs_privdata *pd) 2174b78e84d1SHans Petter Selasky { 2175b78e84d1SHans Petter Selasky struct usb_bus *bus; 2176b78e84d1SHans Petter Selasky 2177b78e84d1SHans Petter Selasky if (pd == NULL) 2178b78e84d1SHans Petter Selasky return; 2179b78e84d1SHans Petter Selasky 2180b78e84d1SHans Petter Selasky mtx_lock(&usb_ref_lock); 2181b78e84d1SHans Petter Selasky bus = devclass_get_softc(usb_devclass_ptr, pd->bus_index); 2182b78e84d1SHans Petter Selasky mtx_unlock(&usb_ref_lock); 2183b78e84d1SHans Petter Selasky 2184b78e84d1SHans Petter Selasky if (bus == NULL) { 2185b78e84d1SHans Petter Selasky usb_destroy_dev_sync(pd); 2186b78e84d1SHans Petter Selasky return; 2187b78e84d1SHans Petter Selasky } 2188b78e84d1SHans Petter Selasky 218907dbde67SHans Petter Selasky /* make sure we can re-use the device name */ 219007dbde67SHans Petter Selasky delist_dev(pd->cdev); 219107dbde67SHans Petter Selasky 2192b78e84d1SHans Petter Selasky USB_BUS_LOCK(bus); 2193*5b8f97d8SBjoern A. Zeeb SLIST_INSERT_HEAD(&bus->pd_cleanup_list, pd, pd_next); 2194b78e84d1SHans Petter Selasky /* get cleanup going */ 2195b78e84d1SHans Petter Selasky usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus), 2196b78e84d1SHans Petter Selasky &bus->cleanup_msg[0], &bus->cleanup_msg[1]); 2197b78e84d1SHans Petter Selasky USB_BUS_UNLOCK(bus); 2198b78e84d1SHans Petter Selasky } 2199b78e84d1SHans Petter Selasky 2200ee3e3ff5SAndrew Thompson static void 2201a593f6b8SAndrew Thompson usb_cdev_create(struct usb_device *udev) 2202ee3e3ff5SAndrew Thompson { 2203760bc48eSAndrew Thompson struct usb_config_descriptor *cd; 2204760bc48eSAndrew Thompson struct usb_endpoint_descriptor *ed; 2205760bc48eSAndrew Thompson struct usb_descriptor *desc; 2206760bc48eSAndrew Thompson struct usb_fs_privdata* pd; 2207f5f145baSAndrew Thompson int inmode, outmode, inmask, outmask, mode; 2208f5f145baSAndrew Thompson uint8_t ep; 2209ee3e3ff5SAndrew Thompson 2210*5b8f97d8SBjoern A. Zeeb KASSERT(SLIST_FIRST(&udev->pd_list) == NULL, ("stale cdev entries")); 2211ee3e3ff5SAndrew Thompson 2212ee3e3ff5SAndrew Thompson DPRINTFN(2, "Creating device nodes\n"); 2213ee3e3ff5SAndrew Thompson 2214a593f6b8SAndrew Thompson if (usbd_get_mode(udev) == USB_MODE_DEVICE) { 2215ee3e3ff5SAndrew Thompson inmode = FWRITE; 2216ee3e3ff5SAndrew Thompson outmode = FREAD; 2217ee3e3ff5SAndrew Thompson } else { /* USB_MODE_HOST */ 2218ee3e3ff5SAndrew Thompson inmode = FREAD; 2219ee3e3ff5SAndrew Thompson outmode = FWRITE; 2220ee3e3ff5SAndrew Thompson } 2221ee3e3ff5SAndrew Thompson 2222f5f145baSAndrew Thompson inmask = 0; 2223f5f145baSAndrew Thompson outmask = 0; 2224f5f145baSAndrew Thompson desc = NULL; 2225f5f145baSAndrew Thompson 2226f5f145baSAndrew Thompson /* 2227f5f145baSAndrew Thompson * Collect all used endpoint numbers instead of just 2228f5f145baSAndrew Thompson * generating 16 static endpoints. 2229f5f145baSAndrew Thompson */ 2230a593f6b8SAndrew Thompson cd = usbd_get_config_descriptor(udev); 2231a593f6b8SAndrew Thompson while ((desc = usb_desc_foreach(cd, desc))) { 2232f5f145baSAndrew Thompson /* filter out all endpoint descriptors */ 2233f5f145baSAndrew Thompson if ((desc->bDescriptorType == UDESC_ENDPOINT) && 2234f5f145baSAndrew Thompson (desc->bLength >= sizeof(*ed))) { 2235760bc48eSAndrew Thompson ed = (struct usb_endpoint_descriptor *)desc; 2236f5f145baSAndrew Thompson 2237f5f145baSAndrew Thompson /* update masks */ 2238f5f145baSAndrew Thompson ep = ed->bEndpointAddress; 2239f5f145baSAndrew Thompson if (UE_GET_DIR(ep) == UE_DIR_OUT) 2240f5f145baSAndrew Thompson outmask |= 1 << UE_GET_ADDR(ep); 2241f5f145baSAndrew Thompson else 2242f5f145baSAndrew Thompson inmask |= 1 << UE_GET_ADDR(ep); 2243f5f145baSAndrew Thompson } 2244f5f145baSAndrew Thompson } 2245ee3e3ff5SAndrew Thompson 2246ee3e3ff5SAndrew Thompson /* Create all available endpoints except EP0 */ 2247ee3e3ff5SAndrew Thompson for (ep = 1; ep < 16; ep++) { 22482ffd5fdcSHans Petter Selasky mode = (inmask & (1 << ep)) ? inmode : 0; 22492ffd5fdcSHans Petter Selasky mode |= (outmask & (1 << ep)) ? outmode : 0; 2250ee3e3ff5SAndrew Thompson if (mode == 0) 2251ee3e3ff5SAndrew Thompson continue; /* no IN or OUT endpoint */ 2252ee3e3ff5SAndrew Thompson 22532ffd5fdcSHans Petter Selasky pd = usb_make_dev(udev, NULL, ep, 0, 22542ffd5fdcSHans Petter Selasky mode, UID_ROOT, GID_OPERATOR, 0600); 22552ffd5fdcSHans Petter Selasky 22562ffd5fdcSHans Petter Selasky if (pd != NULL) 2257*5b8f97d8SBjoern A. Zeeb SLIST_INSERT_HEAD(&udev->pd_list, pd, pd_next); 2258ee3e3ff5SAndrew Thompson } 2259ee3e3ff5SAndrew Thompson } 2260ee3e3ff5SAndrew Thompson 2261ee3e3ff5SAndrew Thompson static void 2262a593f6b8SAndrew Thompson usb_cdev_free(struct usb_device *udev) 2263ee3e3ff5SAndrew Thompson { 2264760bc48eSAndrew Thompson struct usb_fs_privdata* pd; 2265ee3e3ff5SAndrew Thompson 2266ee3e3ff5SAndrew Thompson DPRINTFN(2, "Freeing device nodes\n"); 2267ee3e3ff5SAndrew Thompson 2268*5b8f97d8SBjoern A. Zeeb while ((pd = SLIST_FIRST(&udev->pd_list)) != NULL) { 2269ee3e3ff5SAndrew Thompson KASSERT(pd->cdev->si_drv1 == pd, ("privdata corrupt")); 2270ee3e3ff5SAndrew Thompson 2271*5b8f97d8SBjoern A. Zeeb SLIST_REMOVE(&udev->pd_list, pd, usb_fs_privdata, pd_next); 2272ee3e3ff5SAndrew Thompson 22732ffd5fdcSHans Petter Selasky usb_destroy_dev(pd); 22742ffd5fdcSHans Petter Selasky } 2275ee3e3ff5SAndrew Thompson } 22760f6e8f93SAndrew Thompson #endif 2277ee3e3ff5SAndrew Thompson 227802ac6454SAndrew Thompson /*------------------------------------------------------------------------* 2279a593f6b8SAndrew Thompson * usb_free_device 228002ac6454SAndrew Thompson * 2281d88688c7SAndrew Thompson * This function is NULL safe and will free an USB device and its 2282d88688c7SAndrew Thompson * children devices, if any. 2283bdd41206SAndrew Thompson * 2284d88688c7SAndrew Thompson * Flag values: Reserved, set to zero. 228502ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 228602ac6454SAndrew Thompson void 2287a593f6b8SAndrew Thompson usb_free_device(struct usb_device *udev, uint8_t flag) 228802ac6454SAndrew Thompson { 2289760bc48eSAndrew Thompson struct usb_bus *bus; 2290bdd41206SAndrew Thompson 2291bdd41206SAndrew Thompson if (udev == NULL) 2292bdd41206SAndrew Thompson return; /* already freed */ 229302ac6454SAndrew Thompson 229402ac6454SAndrew Thompson DPRINTFN(4, "udev=%p port=%d\n", udev, udev->port_no); 229502ac6454SAndrew Thompson 2296bd216778SAndrew Thompson bus = udev->bus; 2297f97da128SHans Petter Selasky 2298f97da128SHans Petter Selasky /* set DETACHED state to prevent any further references */ 2299a593f6b8SAndrew Thompson usb_set_device_state(udev, USB_STATE_DETACHED); 2300bdd41206SAndrew Thompson 23018bb77249SHans Petter Selasky #if USB_HAVE_DEVCTL 23023acd904dSAndrew Thompson usb_notify_addq("DETACH", udev); 23038bb77249SHans Petter Selasky #endif 230402ac6454SAndrew Thompson 23058bb77249SHans Petter Selasky #if USB_HAVE_UGEN 2306cabe79d9SMarius Strobl if (!rebooting) { 230788f9a4ffSEdward Tomasz Napierala printf("%s: <%s %s> at %s (disconnected)\n", udev->ugen_name, 230888f9a4ffSEdward Tomasz Napierala usb_get_manufacturer(udev), usb_get_product(udev), 230988f9a4ffSEdward Tomasz Napierala device_get_nameunit(bus->bdev)); 2310cabe79d9SMarius Strobl } 231102ac6454SAndrew Thompson 2312ee3e3ff5SAndrew Thompson /* Destroy UGEN symlink, if any */ 231302ac6454SAndrew Thompson if (udev->ugen_symlink) { 2314a593f6b8SAndrew Thompson usb_free_symlink(udev->ugen_symlink); 231502ac6454SAndrew Thompson udev->ugen_symlink = NULL; 231602ac6454SAndrew Thompson } 231702ac6454SAndrew Thompson 23182ffd5fdcSHans Petter Selasky usb_destroy_dev(udev->ctrl_dev); 23190f6e8f93SAndrew Thompson #endif 232002ac6454SAndrew Thompson 2321f29a0724SAndrew Thompson if (udev->flags.usb_mode == USB_MODE_DEVICE) { 232202ac6454SAndrew Thompson /* stop receiving any control transfers (Device Side Mode) */ 23235b3bb704SAndrew Thompson usbd_transfer_unsetup(udev->ctrl_xfer, USB_CTRL_XFER_MAX); 232402ac6454SAndrew Thompson } 232502ac6454SAndrew Thompson 2326bdd41206SAndrew Thompson /* the following will get the device unconfigured in software */ 2327d88688c7SAndrew Thompson usb_unconfigure(udev, USB_UNCFG_FLAG_FREE_EP0); 232802ac6454SAndrew Thompson 2329f97da128SHans Petter Selasky /* final device unregister after all character devices are closed */ 2330f97da128SHans Petter Selasky usb_bus_port_set_device(bus, udev->parent_hub ? 2331f97da128SHans Petter Selasky udev->parent_hub->hub->ports + udev->port_index : NULL, 2332f97da128SHans Petter Selasky NULL, USB_ROOT_HUB_ADDR); 2333f97da128SHans Petter Selasky 233402ac6454SAndrew Thompson /* unsetup any leftover default USB transfers */ 23355b3bb704SAndrew Thompson usbd_transfer_unsetup(udev->ctrl_xfer, USB_CTRL_XFER_MAX); 233602ac6454SAndrew Thompson 233702ac6454SAndrew Thompson /* template unsetup, if any */ 2338a593f6b8SAndrew Thompson (usb_temp_unsetup_p) (udev); 233902ac6454SAndrew Thompson 234002ac6454SAndrew Thompson /* 234102ac6454SAndrew Thompson * Make sure that our clear-stall messages are not queued 234202ac6454SAndrew Thompson * anywhere: 234302ac6454SAndrew Thompson */ 234402ac6454SAndrew Thompson USB_BUS_LOCK(udev->bus); 234543ea03d7SHans Petter Selasky usb_proc_mwait(USB_BUS_CS_PROC(udev->bus), 234602ac6454SAndrew Thompson &udev->cs_msg[0], &udev->cs_msg[1]); 234702ac6454SAndrew Thompson USB_BUS_UNLOCK(udev->bus); 234802ac6454SAndrew Thompson 2349b78e84d1SHans Petter Selasky /* wait for all references to go away */ 2350b78e84d1SHans Petter Selasky usb_wait_pending_refs(udev); 2351b78e84d1SHans Petter Selasky 235291cd9240SAndrew Thompson sx_destroy(&udev->enum_sx); 23532df1e9a6SAndrew Thompson sx_destroy(&udev->sr_sx); 235464cb5e2aSHans Petter Selasky sx_destroy(&udev->ctrl_sx); 235502ac6454SAndrew Thompson 235691cd9240SAndrew Thompson cv_destroy(&udev->ctrlreq_cv); 235791cd9240SAndrew Thompson cv_destroy(&udev->ref_cv); 235802ac6454SAndrew Thompson 235991cd9240SAndrew Thompson mtx_destroy(&udev->device_mtx); 23608755859aSAndrew Thompson #if USB_HAVE_UGEN 2361*5b8f97d8SBjoern A. Zeeb KASSERT(SLIST_FIRST(&udev->pd_list) == NULL, ("leaked cdev entries")); 23628755859aSAndrew Thompson #endif 236302ac6454SAndrew Thompson 2364963169b4SHans Petter Selasky /* Uninitialise device */ 2365963169b4SHans Petter Selasky if (bus->methods->device_uninit != NULL) 2366963169b4SHans Petter Selasky (bus->methods->device_uninit) (udev); 2367963169b4SHans Petter Selasky 236802ac6454SAndrew Thompson /* free device */ 236928212de2SAndrew Thompson free(udev->serial, M_USB); 237028212de2SAndrew Thompson free(udev->manufacturer, M_USB); 237128212de2SAndrew Thompson free(udev->product, M_USB); 237202ac6454SAndrew Thompson free(udev, M_USB); 237302ac6454SAndrew Thompson } 237402ac6454SAndrew Thompson 237502ac6454SAndrew Thompson /*------------------------------------------------------------------------* 2376a593f6b8SAndrew Thompson * usbd_get_iface 237702ac6454SAndrew Thompson * 237802ac6454SAndrew Thompson * This function is the safe way to get the USB interface structure 237902ac6454SAndrew Thompson * pointer by interface index. 238002ac6454SAndrew Thompson * 238102ac6454SAndrew Thompson * Return values: 238202ac6454SAndrew Thompson * NULL: Interface not present. 238302ac6454SAndrew Thompson * Else: Pointer to USB interface structure. 238402ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 2385760bc48eSAndrew Thompson struct usb_interface * 2386a593f6b8SAndrew Thompson usbd_get_iface(struct usb_device *udev, uint8_t iface_index) 238702ac6454SAndrew Thompson { 2388760bc48eSAndrew Thompson struct usb_interface *iface = udev->ifaces + iface_index; 238902ac6454SAndrew Thompson 2390bdd41206SAndrew Thompson if (iface_index >= udev->ifaces_max) 239102ac6454SAndrew Thompson return (NULL); 239202ac6454SAndrew Thompson return (iface); 239302ac6454SAndrew Thompson } 239402ac6454SAndrew Thompson 239502ac6454SAndrew Thompson /*------------------------------------------------------------------------* 23963c8d24f4SAndrew Thompson * usbd_find_descriptor 239702ac6454SAndrew Thompson * 239802ac6454SAndrew Thompson * This function will lookup the first descriptor that matches the 239902ac6454SAndrew Thompson * criteria given by the arguments "type" and "subtype". Descriptors 240002ac6454SAndrew Thompson * will only be searched within the interface having the index 240102ac6454SAndrew Thompson * "iface_index". If the "id" argument points to an USB descriptor, 240202ac6454SAndrew Thompson * it will be skipped before the search is started. This allows 240302ac6454SAndrew Thompson * searching for multiple descriptors using the same criteria. Else 240402ac6454SAndrew Thompson * the search is started after the interface descriptor. 240502ac6454SAndrew Thompson * 240602ac6454SAndrew Thompson * Return values: 240702ac6454SAndrew Thompson * NULL: End of descriptors 240802ac6454SAndrew Thompson * Else: A descriptor matching the criteria 240902ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 241002ac6454SAndrew Thompson void * 24113c8d24f4SAndrew Thompson usbd_find_descriptor(struct usb_device *udev, void *id, uint8_t iface_index, 241202ac6454SAndrew Thompson uint8_t type, uint8_t type_mask, 241302ac6454SAndrew Thompson uint8_t subtype, uint8_t subtype_mask) 241402ac6454SAndrew Thompson { 2415760bc48eSAndrew Thompson struct usb_descriptor *desc; 2416760bc48eSAndrew Thompson struct usb_config_descriptor *cd; 2417760bc48eSAndrew Thompson struct usb_interface *iface; 241802ac6454SAndrew Thompson 2419a593f6b8SAndrew Thompson cd = usbd_get_config_descriptor(udev); 242002ac6454SAndrew Thompson if (cd == NULL) { 242102ac6454SAndrew Thompson return (NULL); 242202ac6454SAndrew Thompson } 242302ac6454SAndrew Thompson if (id == NULL) { 2424a593f6b8SAndrew Thompson iface = usbd_get_iface(udev, iface_index); 242502ac6454SAndrew Thompson if (iface == NULL) { 242602ac6454SAndrew Thompson return (NULL); 242702ac6454SAndrew Thompson } 2428a593f6b8SAndrew Thompson id = usbd_get_interface_descriptor(iface); 242902ac6454SAndrew Thompson if (id == NULL) { 243002ac6454SAndrew Thompson return (NULL); 243102ac6454SAndrew Thompson } 243202ac6454SAndrew Thompson } 243302ac6454SAndrew Thompson desc = (void *)id; 243402ac6454SAndrew Thompson 2435a593f6b8SAndrew Thompson while ((desc = usb_desc_foreach(cd, desc))) { 243602ac6454SAndrew Thompson if (desc->bDescriptorType == UDESC_INTERFACE) { 243702ac6454SAndrew Thompson break; 243802ac6454SAndrew Thompson } 243902ac6454SAndrew Thompson if (((desc->bDescriptorType & type_mask) == type) && 244002ac6454SAndrew Thompson ((desc->bDescriptorSubtype & subtype_mask) == subtype)) { 244102ac6454SAndrew Thompson return (desc); 244202ac6454SAndrew Thompson } 244302ac6454SAndrew Thompson } 244402ac6454SAndrew Thompson return (NULL); 244502ac6454SAndrew Thompson } 244602ac6454SAndrew Thompson 244702ac6454SAndrew Thompson /*------------------------------------------------------------------------* 2448a593f6b8SAndrew Thompson * usb_devinfo 244902ac6454SAndrew Thompson * 245002ac6454SAndrew Thompson * This function will dump information from the device descriptor 245102ac6454SAndrew Thompson * belonging to the USB device pointed to by "udev", to the string 245202ac6454SAndrew Thompson * pointed to by "dst_ptr" having a maximum length of "dst_len" bytes 245302ac6454SAndrew Thompson * including the terminating zero. 245402ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 245502ac6454SAndrew Thompson void 2456a593f6b8SAndrew Thompson usb_devinfo(struct usb_device *udev, char *dst_ptr, uint16_t dst_len) 245702ac6454SAndrew Thompson { 2458760bc48eSAndrew Thompson struct usb_device_descriptor *udd = &udev->ddesc; 245902ac6454SAndrew Thompson uint16_t bcdDevice; 246002ac6454SAndrew Thompson uint16_t bcdUSB; 246102ac6454SAndrew Thompson 246202ac6454SAndrew Thompson bcdUSB = UGETW(udd->bcdUSB); 246302ac6454SAndrew Thompson bcdDevice = UGETW(udd->bcdDevice); 246402ac6454SAndrew Thompson 246502ac6454SAndrew Thompson if (udd->bDeviceClass != 0xFF) { 246602ac6454SAndrew Thompson snprintf(dst_ptr, dst_len, "%s %s, class %d/%d, rev %x.%02x/" 24678755859aSAndrew Thompson "%x.%02x, addr %d", 2468ae538d85SAndrew Thompson usb_get_manufacturer(udev), 2469ae538d85SAndrew Thompson usb_get_product(udev), 247002ac6454SAndrew Thompson udd->bDeviceClass, udd->bDeviceSubClass, 247102ac6454SAndrew Thompson (bcdUSB >> 8), bcdUSB & 0xFF, 247202ac6454SAndrew Thompson (bcdDevice >> 8), bcdDevice & 0xFF, 247302ac6454SAndrew Thompson udev->address); 247402ac6454SAndrew Thompson } else { 247502ac6454SAndrew Thompson snprintf(dst_ptr, dst_len, "%s %s, rev %x.%02x/" 24768755859aSAndrew Thompson "%x.%02x, addr %d", 2477ae538d85SAndrew Thompson usb_get_manufacturer(udev), 2478ae538d85SAndrew Thompson usb_get_product(udev), 247902ac6454SAndrew Thompson (bcdUSB >> 8), bcdUSB & 0xFF, 248002ac6454SAndrew Thompson (bcdDevice >> 8), bcdDevice & 0xFF, 248102ac6454SAndrew Thompson udev->address); 248202ac6454SAndrew Thompson } 248302ac6454SAndrew Thompson } 248402ac6454SAndrew Thompson 2485ed6d949aSAndrew Thompson #ifdef USB_VERBOSE 248602ac6454SAndrew Thompson /* 2487604511f8SGordon Bergling * Descriptions of known vendors and devices ("products"). 248802ac6454SAndrew Thompson */ 248902ac6454SAndrew Thompson struct usb_knowndev { 249002ac6454SAndrew Thompson uint16_t vendor; 249102ac6454SAndrew Thompson uint16_t product; 249202ac6454SAndrew Thompson uint32_t flags; 249302ac6454SAndrew Thompson const char *vendorname; 249402ac6454SAndrew Thompson const char *productname; 249502ac6454SAndrew Thompson }; 249602ac6454SAndrew Thompson 249702ac6454SAndrew Thompson #define USB_KNOWNDEV_NOPROD 0x01 /* match on vendor only */ 249802ac6454SAndrew Thompson 249902ac6454SAndrew Thompson #include "usbdevs.h" 250002ac6454SAndrew Thompson #include "usbdevs_data.h" 250102ac6454SAndrew Thompson #endif /* USB_VERBOSE */ 250202ac6454SAndrew Thompson 2503f54ab96dSHans Petter Selasky void 2504f54ab96dSHans Petter Selasky usb_set_device_strings(struct usb_device *udev) 250502ac6454SAndrew Thompson { 2506760bc48eSAndrew Thompson struct usb_device_descriptor *udd = &udev->ddesc; 2507ed6d949aSAndrew Thompson #ifdef USB_VERBOSE 250802ac6454SAndrew Thompson const struct usb_knowndev *kdp; 250902ac6454SAndrew Thompson #endif 25102df1e9a6SAndrew Thompson char *temp_ptr; 25117a918edfSAndrew Thompson size_t temp_size; 251202ac6454SAndrew Thompson uint16_t vendor_id; 251302ac6454SAndrew Thompson uint16_t product_id; 25146950c75fSHans Petter Selasky uint8_t do_unlock; 251502ac6454SAndrew Thompson 25166950c75fSHans Petter Selasky /* Protect scratch area */ 251764cb5e2aSHans Petter Selasky do_unlock = usbd_ctrl_lock(udev); 25186950c75fSHans Petter Selasky 25196950c75fSHans Petter Selasky temp_ptr = (char *)udev->scratch.data; 25206950c75fSHans Petter Selasky temp_size = sizeof(udev->scratch.data); 25217a918edfSAndrew Thompson 252202ac6454SAndrew Thompson vendor_id = UGETW(udd->idVendor); 252302ac6454SAndrew Thompson product_id = UGETW(udd->idProduct); 252402ac6454SAndrew Thompson 2525f54ab96dSHans Petter Selasky /* cleanup old strings, if any */ 2526f54ab96dSHans Petter Selasky free(udev->serial, M_USB); 2527f54ab96dSHans Petter Selasky free(udev->manufacturer, M_USB); 2528f54ab96dSHans Petter Selasky free(udev->product, M_USB); 2529f54ab96dSHans Petter Selasky 2530f54ab96dSHans Petter Selasky /* zero the string pointers */ 2531f54ab96dSHans Petter Selasky udev->serial = NULL; 2532f54ab96dSHans Petter Selasky udev->manufacturer = NULL; 2533f54ab96dSHans Petter Selasky udev->product = NULL; 2534f54ab96dSHans Petter Selasky 2535c86aead6SAndrew Thompson /* get serial number string */ 25367a918edfSAndrew Thompson usbd_req_get_string_any(udev, NULL, temp_ptr, temp_size, 2537c86aead6SAndrew Thompson udev->ddesc.iSerialNumber); 25387a918edfSAndrew Thompson udev->serial = strdup(temp_ptr, M_USB); 253902ac6454SAndrew Thompson 2540c86aead6SAndrew Thompson /* get manufacturer string */ 25417a918edfSAndrew Thompson usbd_req_get_string_any(udev, NULL, temp_ptr, temp_size, 2542c86aead6SAndrew Thompson udev->ddesc.iManufacturer); 25437a918edfSAndrew Thompson usb_trim_spaces(temp_ptr); 25447a918edfSAndrew Thompson if (temp_ptr[0] != '\0') 25457a918edfSAndrew Thompson udev->manufacturer = strdup(temp_ptr, M_USB); 2546c86aead6SAndrew Thompson 2547c86aead6SAndrew Thompson /* get product string */ 25487a918edfSAndrew Thompson usbd_req_get_string_any(udev, NULL, temp_ptr, temp_size, 2549c86aead6SAndrew Thompson udev->ddesc.iProduct); 25507a918edfSAndrew Thompson usb_trim_spaces(temp_ptr); 25517a918edfSAndrew Thompson if (temp_ptr[0] != '\0') 25527a918edfSAndrew Thompson udev->product = strdup(temp_ptr, M_USB); 2553c86aead6SAndrew Thompson 2554ed6d949aSAndrew Thompson #ifdef USB_VERBOSE 2555c86aead6SAndrew Thompson if (udev->manufacturer == NULL || udev->product == NULL) { 2556c86aead6SAndrew Thompson for (kdp = usb_knowndevs; kdp->vendorname != NULL; kdp++) { 255702ac6454SAndrew Thompson if (kdp->vendor == vendor_id && 255802ac6454SAndrew Thompson (kdp->product == product_id || 255902ac6454SAndrew Thompson (kdp->flags & USB_KNOWNDEV_NOPROD) != 0)) 256002ac6454SAndrew Thompson break; 256102ac6454SAndrew Thompson } 256202ac6454SAndrew Thompson if (kdp->vendorname != NULL) { 2563c86aead6SAndrew Thompson /* XXX should use pointer to knowndevs string */ 2564c86aead6SAndrew Thompson if (udev->manufacturer == NULL) { 2565c86aead6SAndrew Thompson udev->manufacturer = strdup(kdp->vendorname, 2566c86aead6SAndrew Thompson M_USB); 2567c86aead6SAndrew Thompson } 2568c86aead6SAndrew Thompson if (udev->product == NULL && 2569c86aead6SAndrew Thompson (kdp->flags & USB_KNOWNDEV_NOPROD) == 0) { 2570c86aead6SAndrew Thompson udev->product = strdup(kdp->productname, 2571c86aead6SAndrew Thompson M_USB); 2572c86aead6SAndrew Thompson } 257302ac6454SAndrew Thompson } 257402ac6454SAndrew Thompson } 257502ac6454SAndrew Thompson #endif 2576c86aead6SAndrew Thompson /* Provide default strings if none were found */ 2577c86aead6SAndrew Thompson if (udev->manufacturer == NULL) { 25787a918edfSAndrew Thompson snprintf(temp_ptr, temp_size, "vendor 0x%04x", vendor_id); 25797a918edfSAndrew Thompson udev->manufacturer = strdup(temp_ptr, M_USB); 258002ac6454SAndrew Thompson } 2581c86aead6SAndrew Thompson if (udev->product == NULL) { 25827a918edfSAndrew Thompson snprintf(temp_ptr, temp_size, "product 0x%04x", product_id); 25837a918edfSAndrew Thompson udev->product = strdup(temp_ptr, M_USB); 258402ac6454SAndrew Thompson } 25856950c75fSHans Petter Selasky 25866950c75fSHans Petter Selasky if (do_unlock) 258764cb5e2aSHans Petter Selasky usbd_ctrl_unlock(udev); 258802ac6454SAndrew Thompson } 258902ac6454SAndrew Thompson 259002ac6454SAndrew Thompson /* 259102ac6454SAndrew Thompson * Returns: 259202ac6454SAndrew Thompson * See: USB_MODE_XXX 259302ac6454SAndrew Thompson */ 2594f29a0724SAndrew Thompson enum usb_hc_mode 2595a593f6b8SAndrew Thompson usbd_get_mode(struct usb_device *udev) 259602ac6454SAndrew Thompson { 2597f29a0724SAndrew Thompson return (udev->flags.usb_mode); 259802ac6454SAndrew Thompson } 259902ac6454SAndrew Thompson 260002ac6454SAndrew Thompson /* 260102ac6454SAndrew Thompson * Returns: 260202ac6454SAndrew Thompson * See: USB_SPEED_XXX 260302ac6454SAndrew Thompson */ 26048d2dd5ddSAndrew Thompson enum usb_dev_speed 2605a593f6b8SAndrew Thompson usbd_get_speed(struct usb_device *udev) 260602ac6454SAndrew Thompson { 260702ac6454SAndrew Thompson return (udev->speed); 260802ac6454SAndrew Thompson } 260902ac6454SAndrew Thompson 261002ac6454SAndrew Thompson uint32_t 2611a593f6b8SAndrew Thompson usbd_get_isoc_fps(struct usb_device *udev) 261202ac6454SAndrew Thompson { 261302ac6454SAndrew Thompson ; /* indent fix */ 261402ac6454SAndrew Thompson switch (udev->speed) { 261502ac6454SAndrew Thompson case USB_SPEED_LOW: 261602ac6454SAndrew Thompson case USB_SPEED_FULL: 261702ac6454SAndrew Thompson return (1000); 261802ac6454SAndrew Thompson default: 261902ac6454SAndrew Thompson return (8000); 262002ac6454SAndrew Thompson } 262102ac6454SAndrew Thompson } 262202ac6454SAndrew Thompson 2623760bc48eSAndrew Thompson struct usb_device_descriptor * 2624a593f6b8SAndrew Thompson usbd_get_device_descriptor(struct usb_device *udev) 262502ac6454SAndrew Thompson { 262602ac6454SAndrew Thompson if (udev == NULL) 262702ac6454SAndrew Thompson return (NULL); /* be NULL safe */ 262802ac6454SAndrew Thompson return (&udev->ddesc); 262902ac6454SAndrew Thompson } 263002ac6454SAndrew Thompson 2631760bc48eSAndrew Thompson struct usb_config_descriptor * 2632a593f6b8SAndrew Thompson usbd_get_config_descriptor(struct usb_device *udev) 263302ac6454SAndrew Thompson { 263402ac6454SAndrew Thompson if (udev == NULL) 263502ac6454SAndrew Thompson return (NULL); /* be NULL safe */ 263602ac6454SAndrew Thompson return (udev->cdesc); 263702ac6454SAndrew Thompson } 263802ac6454SAndrew Thompson 263902ac6454SAndrew Thompson /*------------------------------------------------------------------------* 2640a593f6b8SAndrew Thompson * usb_test_quirk - test a device for a given quirk 264102ac6454SAndrew Thompson * 264202ac6454SAndrew Thompson * Return values: 264302ac6454SAndrew Thompson * 0: The USB device does not have the given quirk. 264402ac6454SAndrew Thompson * Else: The USB device has the given quirk. 264502ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 264602ac6454SAndrew Thompson uint8_t 2647a593f6b8SAndrew Thompson usb_test_quirk(const struct usb_attach_arg *uaa, uint16_t quirk) 264802ac6454SAndrew Thompson { 264902ac6454SAndrew Thompson uint8_t found; 26503f37fb62SHans Petter Selasky uint8_t x; 26513f37fb62SHans Petter Selasky 26523f37fb62SHans Petter Selasky if (quirk == UQ_NONE) 26533f37fb62SHans Petter Selasky return (0); 26543f37fb62SHans Petter Selasky 26553f37fb62SHans Petter Selasky /* search the automatic per device quirks first */ 26563f37fb62SHans Petter Selasky 26573f37fb62SHans Petter Selasky for (x = 0; x != USB_MAX_AUTO_QUIRK; x++) { 26583f37fb62SHans Petter Selasky if (uaa->device->autoQuirk[x] == quirk) 26593f37fb62SHans Petter Selasky return (1); 26603f37fb62SHans Petter Selasky } 26613f37fb62SHans Petter Selasky 26623f37fb62SHans Petter Selasky /* search global quirk table, if any */ 266302ac6454SAndrew Thompson 2664a593f6b8SAndrew Thompson found = (usb_test_quirk_p) (&uaa->info, quirk); 26653f37fb62SHans Petter Selasky 266602ac6454SAndrew Thompson return (found); 266702ac6454SAndrew Thompson } 266802ac6454SAndrew Thompson 2669760bc48eSAndrew Thompson struct usb_interface_descriptor * 2670a593f6b8SAndrew Thompson usbd_get_interface_descriptor(struct usb_interface *iface) 267102ac6454SAndrew Thompson { 267202ac6454SAndrew Thompson if (iface == NULL) 267302ac6454SAndrew Thompson return (NULL); /* be NULL safe */ 267402ac6454SAndrew Thompson return (iface->idesc); 267502ac6454SAndrew Thompson } 267602ac6454SAndrew Thompson 267702ac6454SAndrew Thompson uint8_t 2678a593f6b8SAndrew Thompson usbd_get_interface_altindex(struct usb_interface *iface) 267902ac6454SAndrew Thompson { 268002ac6454SAndrew Thompson return (iface->alt_index); 268102ac6454SAndrew Thompson } 268202ac6454SAndrew Thompson 268302ac6454SAndrew Thompson uint8_t 2684a593f6b8SAndrew Thompson usbd_get_bus_index(struct usb_device *udev) 268502ac6454SAndrew Thompson { 268602ac6454SAndrew Thompson return ((uint8_t)device_get_unit(udev->bus->bdev)); 268702ac6454SAndrew Thompson } 268802ac6454SAndrew Thompson 268902ac6454SAndrew Thompson uint8_t 2690a593f6b8SAndrew Thompson usbd_get_device_index(struct usb_device *udev) 269102ac6454SAndrew Thompson { 269202ac6454SAndrew Thompson return (udev->device_index); 269302ac6454SAndrew Thompson } 269402ac6454SAndrew Thompson 26958bb77249SHans Petter Selasky #if USB_HAVE_DEVCTL 26963acd904dSAndrew Thompson static void 26973acd904dSAndrew Thompson usb_notify_addq(const char *type, struct usb_device *udev) 26983acd904dSAndrew Thompson { 26993acd904dSAndrew Thompson struct usb_interface *iface; 27003acd904dSAndrew Thompson struct sbuf *sb; 27013acd904dSAndrew Thompson int i; 27023acd904dSAndrew Thompson 27033acd904dSAndrew Thompson /* announce the device */ 27043acd904dSAndrew Thompson sb = sbuf_new_auto(); 27053acd904dSAndrew Thompson sbuf_printf(sb, 27068bb77249SHans Petter Selasky #if USB_HAVE_UGEN 27078bb77249SHans Petter Selasky "ugen=%s " 270802c40241SHans Petter Selasky "cdev=%s " 27098bb77249SHans Petter Selasky #endif 27103acd904dSAndrew Thompson "vendor=0x%04x " 27113acd904dSAndrew Thompson "product=0x%04x " 27123acd904dSAndrew Thompson "devclass=0x%02x " 27133acd904dSAndrew Thompson "devsubclass=0x%02x " 27143acd904dSAndrew Thompson "sernum=\"%s\" " 27153acd904dSAndrew Thompson "release=0x%04x " 271666390216SAndrew Thompson "mode=%s " 27173acd904dSAndrew Thompson "port=%u " 27188bb77249SHans Petter Selasky #if USB_HAVE_UGEN 27196df3eebbSNick Hibma "parent=%s" 27208bb77249SHans Petter Selasky #endif 27218bb77249SHans Petter Selasky "", 27228bb77249SHans Petter Selasky #if USB_HAVE_UGEN 27233acd904dSAndrew Thompson udev->ugen_name, 272402c40241SHans Petter Selasky udev->ugen_name, 27258bb77249SHans Petter Selasky #endif 27263acd904dSAndrew Thompson UGETW(udev->ddesc.idVendor), 27273acd904dSAndrew Thompson UGETW(udev->ddesc.idProduct), 27283acd904dSAndrew Thompson udev->ddesc.bDeviceClass, 27293acd904dSAndrew Thompson udev->ddesc.bDeviceSubClass, 2730ae538d85SAndrew Thompson usb_get_serial(udev), 27313acd904dSAndrew Thompson UGETW(udev->ddesc.bcdDevice), 273266390216SAndrew Thompson (udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device", 27338bb77249SHans Petter Selasky udev->port_no 27348bb77249SHans Petter Selasky #if USB_HAVE_UGEN 27358bb77249SHans Petter Selasky , udev->parent_hub != NULL ? 27363acd904dSAndrew Thompson udev->parent_hub->ugen_name : 27378bb77249SHans Petter Selasky device_get_nameunit(device_get_parent(udev->bus->bdev)) 27388bb77249SHans Petter Selasky #endif 27398bb77249SHans Petter Selasky ); 27403acd904dSAndrew Thompson sbuf_finish(sb); 27413acd904dSAndrew Thompson devctl_notify("USB", "DEVICE", type, sbuf_data(sb)); 27423acd904dSAndrew Thompson sbuf_delete(sb); 27433acd904dSAndrew Thompson 27443acd904dSAndrew Thompson /* announce each interface */ 27453acd904dSAndrew Thompson for (i = 0; i < USB_IFACE_MAX; i++) { 27463acd904dSAndrew Thompson iface = usbd_get_iface(udev, i); 27473acd904dSAndrew Thompson if (iface == NULL) 27483acd904dSAndrew Thompson break; /* end of interfaces */ 27493acd904dSAndrew Thompson if (iface->idesc == NULL) 27503acd904dSAndrew Thompson continue; /* no interface descriptor */ 27513acd904dSAndrew Thompson 27523acd904dSAndrew Thompson sb = sbuf_new_auto(); 27533acd904dSAndrew Thompson sbuf_printf(sb, 27548bb77249SHans Petter Selasky #if USB_HAVE_UGEN 27558bb77249SHans Petter Selasky "ugen=%s " 275602c40241SHans Petter Selasky "cdev=%s " 27578bb77249SHans Petter Selasky #endif 27583acd904dSAndrew Thompson "vendor=0x%04x " 27593acd904dSAndrew Thompson "product=0x%04x " 27603acd904dSAndrew Thompson "devclass=0x%02x " 27613acd904dSAndrew Thompson "devsubclass=0x%02x " 27623acd904dSAndrew Thompson "sernum=\"%s\" " 27633acd904dSAndrew Thompson "release=0x%04x " 276466390216SAndrew Thompson "mode=%s " 27653acd904dSAndrew Thompson "interface=%d " 27663acd904dSAndrew Thompson "endpoints=%d " 27673acd904dSAndrew Thompson "intclass=0x%02x " 27683acd904dSAndrew Thompson "intsubclass=0x%02x " 27696df3eebbSNick Hibma "intprotocol=0x%02x", 27708bb77249SHans Petter Selasky #if USB_HAVE_UGEN 27713acd904dSAndrew Thompson udev->ugen_name, 277202c40241SHans Petter Selasky udev->ugen_name, 27738bb77249SHans Petter Selasky #endif 27743acd904dSAndrew Thompson UGETW(udev->ddesc.idVendor), 27753acd904dSAndrew Thompson UGETW(udev->ddesc.idProduct), 27763acd904dSAndrew Thompson udev->ddesc.bDeviceClass, 27773acd904dSAndrew Thompson udev->ddesc.bDeviceSubClass, 2778ae538d85SAndrew Thompson usb_get_serial(udev), 27793acd904dSAndrew Thompson UGETW(udev->ddesc.bcdDevice), 278066390216SAndrew Thompson (udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device", 27813acd904dSAndrew Thompson iface->idesc->bInterfaceNumber, 27823acd904dSAndrew Thompson iface->idesc->bNumEndpoints, 27833acd904dSAndrew Thompson iface->idesc->bInterfaceClass, 27843acd904dSAndrew Thompson iface->idesc->bInterfaceSubClass, 27853acd904dSAndrew Thompson iface->idesc->bInterfaceProtocol); 27863acd904dSAndrew Thompson sbuf_finish(sb); 27873acd904dSAndrew Thompson devctl_notify("USB", "INTERFACE", type, sbuf_data(sb)); 27883acd904dSAndrew Thompson sbuf_delete(sb); 27893acd904dSAndrew Thompson } 27903acd904dSAndrew Thompson } 27918bb77249SHans Petter Selasky #endif 279202ac6454SAndrew Thompson 27938bb77249SHans Petter Selasky #if USB_HAVE_UGEN 279402ac6454SAndrew Thompson /*------------------------------------------------------------------------* 2795a593f6b8SAndrew Thompson * usb_fifo_free_wrap 279602ac6454SAndrew Thompson * 279702ac6454SAndrew Thompson * This function will free the FIFOs. 279802ac6454SAndrew Thompson * 2799bdd41206SAndrew Thompson * Description of "flag" argument: If the USB_UNCFG_FLAG_FREE_EP0 flag 2800bdd41206SAndrew Thompson * is set and "iface_index" is set to "USB_IFACE_INDEX_ANY", we free 2801bdd41206SAndrew Thompson * all FIFOs. If the USB_UNCFG_FLAG_FREE_EP0 flag is not set and 2802bdd41206SAndrew Thompson * "iface_index" is set to "USB_IFACE_INDEX_ANY", we free all non 2803bdd41206SAndrew Thompson * control endpoint FIFOs. If "iface_index" is not set to 2804bdd41206SAndrew Thompson * "USB_IFACE_INDEX_ANY" the flag has no effect. 280502ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 280602ac6454SAndrew Thompson static void 2807a593f6b8SAndrew Thompson usb_fifo_free_wrap(struct usb_device *udev, 280802ac6454SAndrew Thompson uint8_t iface_index, uint8_t flag) 280902ac6454SAndrew Thompson { 2810760bc48eSAndrew Thompson struct usb_fifo *f; 281102ac6454SAndrew Thompson uint16_t i; 281202ac6454SAndrew Thompson 281302ac6454SAndrew Thompson /* 281402ac6454SAndrew Thompson * Free any USB FIFOs on the given interface: 281502ac6454SAndrew Thompson */ 281602ac6454SAndrew Thompson for (i = 0; i != USB_FIFO_MAX; i++) { 281702ac6454SAndrew Thompson f = udev->fifo[i]; 281802ac6454SAndrew Thompson if (f == NULL) { 281902ac6454SAndrew Thompson continue; 282002ac6454SAndrew Thompson } 282102ac6454SAndrew Thompson /* Check if the interface index matches */ 282202ac6454SAndrew Thompson if (iface_index == f->iface_index) { 2823a593f6b8SAndrew Thompson if (f->methods != &usb_ugen_methods) { 282402ac6454SAndrew Thompson /* 282502ac6454SAndrew Thompson * Don't free any non-generic FIFOs in 282602ac6454SAndrew Thompson * this case. 282702ac6454SAndrew Thompson */ 282802ac6454SAndrew Thompson continue; 282902ac6454SAndrew Thompson } 283002ac6454SAndrew Thompson if ((f->dev_ep_index == 0) && 28319b077d72SHans Petter Selasky (f->fs_ep_max == 0)) { 283202ac6454SAndrew Thompson /* no need to free this FIFO */ 283302ac6454SAndrew Thompson continue; 283402ac6454SAndrew Thompson } 283502ac6454SAndrew Thompson } else if (iface_index == USB_IFACE_INDEX_ANY) { 2836a593f6b8SAndrew Thompson if ((f->methods == &usb_ugen_methods) && 2837bdd41206SAndrew Thompson (f->dev_ep_index == 0) && 2838bdd41206SAndrew Thompson (!(flag & USB_UNCFG_FLAG_FREE_EP0)) && 28399b077d72SHans Petter Selasky (f->fs_ep_max == 0)) { 284002ac6454SAndrew Thompson /* no need to free this FIFO */ 284102ac6454SAndrew Thompson continue; 284202ac6454SAndrew Thompson } 284302ac6454SAndrew Thompson } else { 284402ac6454SAndrew Thompson /* no need to free this FIFO */ 284502ac6454SAndrew Thompson continue; 284602ac6454SAndrew Thompson } 284702ac6454SAndrew Thompson /* free this FIFO */ 2848a593f6b8SAndrew Thompson usb_fifo_free(f); 284902ac6454SAndrew Thompson } 285002ac6454SAndrew Thompson } 28510f6e8f93SAndrew Thompson #endif 285202ac6454SAndrew Thompson 285302ac6454SAndrew Thompson /*------------------------------------------------------------------------* 2854a593f6b8SAndrew Thompson * usb_peer_can_wakeup 285502ac6454SAndrew Thompson * 285602ac6454SAndrew Thompson * Return values: 285702ac6454SAndrew Thompson * 0: Peer cannot do resume signalling. 285802ac6454SAndrew Thompson * Else: Peer can do resume signalling. 285902ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 286002ac6454SAndrew Thompson uint8_t 2861a593f6b8SAndrew Thompson usb_peer_can_wakeup(struct usb_device *udev) 286202ac6454SAndrew Thompson { 2863760bc48eSAndrew Thompson const struct usb_config_descriptor *cdp; 286402ac6454SAndrew Thompson 286502ac6454SAndrew Thompson cdp = udev->cdesc; 2866f29a0724SAndrew Thompson if ((cdp != NULL) && (udev->flags.usb_mode == USB_MODE_HOST)) { 286702ac6454SAndrew Thompson return (cdp->bmAttributes & UC_REMOTE_WAKEUP); 286802ac6454SAndrew Thompson } 286902ac6454SAndrew Thompson return (0); /* not supported */ 287002ac6454SAndrew Thompson } 2871bd216778SAndrew Thompson 2872bd216778SAndrew Thompson void 2873a593f6b8SAndrew Thompson usb_set_device_state(struct usb_device *udev, enum usb_dev_state state) 2874bd216778SAndrew Thompson { 2875bd216778SAndrew Thompson 2876bd216778SAndrew Thompson KASSERT(state < USB_STATE_MAX, ("invalid udev state")); 2877bd216778SAndrew Thompson 2878bd216778SAndrew Thompson DPRINTF("udev %p state %s -> %s\n", udev, 2879a593f6b8SAndrew Thompson usb_statestr(udev->state), usb_statestr(state)); 2880963169b4SHans Petter Selasky 2881f97da128SHans Petter Selasky #if USB_HAVE_UGEN 2882f97da128SHans Petter Selasky mtx_lock(&usb_ref_lock); 2883f97da128SHans Petter Selasky #endif 2884f97da128SHans Petter Selasky udev->state = state; 2885f97da128SHans Petter Selasky #if USB_HAVE_UGEN 2886f97da128SHans Petter Selasky mtx_unlock(&usb_ref_lock); 2887f97da128SHans Petter Selasky #endif 2888963169b4SHans Petter Selasky if (udev->bus->methods->device_state_change != NULL) 2889963169b4SHans Petter Selasky (udev->bus->methods->device_state_change) (udev); 2890963169b4SHans Petter Selasky } 2891963169b4SHans Petter Selasky 2892963169b4SHans Petter Selasky enum usb_dev_state 2893963169b4SHans Petter Selasky usb_get_device_state(struct usb_device *udev) 2894963169b4SHans Petter Selasky { 2895963169b4SHans Petter Selasky if (udev == NULL) 2896963169b4SHans Petter Selasky return (USB_STATE_DETACHED); 2897963169b4SHans Petter Selasky return (udev->state); 2898bd216778SAndrew Thompson } 2899bd216778SAndrew Thompson 2900ec8f3127SAndrew Thompson uint8_t 2901a593f6b8SAndrew Thompson usbd_device_attached(struct usb_device *udev) 2902bd216778SAndrew Thompson { 2903bd216778SAndrew Thompson return (udev->state > USB_STATE_DETACHED); 2904bd216778SAndrew Thompson } 2905cb18f7d1SAlfred Perlstein 29066950c75fSHans Petter Selasky /* 29076950c75fSHans Petter Selasky * The following function locks enumerating the given USB device. If 29086950c75fSHans Petter Selasky * the lock is already grabbed this function returns zero. Else a 290910aab8b6SHans Petter Selasky * a value of one is returned. 29106950c75fSHans Petter Selasky */ 29116950c75fSHans Petter Selasky uint8_t 2912cb18f7d1SAlfred Perlstein usbd_enum_lock(struct usb_device *udev) 2913cb18f7d1SAlfred Perlstein { 29146950c75fSHans Petter Selasky if (sx_xlocked(&udev->enum_sx)) 29156950c75fSHans Petter Selasky return (0); 29166950c75fSHans Petter Selasky 291791cd9240SAndrew Thompson sx_xlock(&udev->enum_sx); 29182df1e9a6SAndrew Thompson sx_xlock(&udev->sr_sx); 2919cb18f7d1SAlfred Perlstein /* 2920cb18f7d1SAlfred Perlstein * NEWBUS LOCK NOTE: We should check if any parent SX locks 2921cb18f7d1SAlfred Perlstein * are locked before locking Giant. Else the lock can be 2922cb18f7d1SAlfred Perlstein * locked multiple times. 2923cb18f7d1SAlfred Perlstein */ 2924c6df6f53SWarner Losh bus_topo_lock(); 29256950c75fSHans Petter Selasky return (1); 2926cb18f7d1SAlfred Perlstein } 2927cb18f7d1SAlfred Perlstein 292810aab8b6SHans Petter Selasky #if USB_HAVE_UGEN 292910aab8b6SHans Petter Selasky /* 293010aab8b6SHans Petter Selasky * This function is the same like usbd_enum_lock() except a value of 293110aab8b6SHans Petter Selasky * 255 is returned when a signal is pending: 293210aab8b6SHans Petter Selasky */ 293310aab8b6SHans Petter Selasky uint8_t 293410aab8b6SHans Petter Selasky usbd_enum_lock_sig(struct usb_device *udev) 293510aab8b6SHans Petter Selasky { 293610aab8b6SHans Petter Selasky if (sx_xlocked(&udev->enum_sx)) 293710aab8b6SHans Petter Selasky return (0); 293810aab8b6SHans Petter Selasky if (sx_xlock_sig(&udev->enum_sx)) 293910aab8b6SHans Petter Selasky return (255); 294010aab8b6SHans Petter Selasky if (sx_xlock_sig(&udev->sr_sx)) { 294110aab8b6SHans Petter Selasky sx_xunlock(&udev->enum_sx); 294210aab8b6SHans Petter Selasky return (255); 294310aab8b6SHans Petter Selasky } 2944c6df6f53SWarner Losh bus_topo_lock(); 294510aab8b6SHans Petter Selasky return (1); 294610aab8b6SHans Petter Selasky } 294710aab8b6SHans Petter Selasky #endif 294810aab8b6SHans Petter Selasky 2949cb18f7d1SAlfred Perlstein /* The following function unlocks enumerating the given USB device. */ 2950cb18f7d1SAlfred Perlstein 2951cb18f7d1SAlfred Perlstein void 2952cb18f7d1SAlfred Perlstein usbd_enum_unlock(struct usb_device *udev) 2953cb18f7d1SAlfred Perlstein { 2954c6df6f53SWarner Losh bus_topo_unlock(); 295591cd9240SAndrew Thompson sx_xunlock(&udev->enum_sx); 29562df1e9a6SAndrew Thompson sx_xunlock(&udev->sr_sx); 29572df1e9a6SAndrew Thompson } 29582df1e9a6SAndrew Thompson 29592df1e9a6SAndrew Thompson /* The following function locks suspend and resume. */ 29602df1e9a6SAndrew Thompson 29612df1e9a6SAndrew Thompson void 29622df1e9a6SAndrew Thompson usbd_sr_lock(struct usb_device *udev) 29632df1e9a6SAndrew Thompson { 29642df1e9a6SAndrew Thompson sx_xlock(&udev->sr_sx); 29652df1e9a6SAndrew Thompson /* 29662df1e9a6SAndrew Thompson * NEWBUS LOCK NOTE: We should check if any parent SX locks 29672df1e9a6SAndrew Thompson * are locked before locking Giant. Else the lock can be 29682df1e9a6SAndrew Thompson * locked multiple times. 29692df1e9a6SAndrew Thompson */ 2970c6df6f53SWarner Losh bus_topo_lock(); 29712df1e9a6SAndrew Thompson } 29722df1e9a6SAndrew Thompson 29732df1e9a6SAndrew Thompson /* The following function unlocks suspend and resume. */ 29742df1e9a6SAndrew Thompson 29752df1e9a6SAndrew Thompson void 29762df1e9a6SAndrew Thompson usbd_sr_unlock(struct usb_device *udev) 29772df1e9a6SAndrew Thompson { 2978c6df6f53SWarner Losh bus_topo_unlock(); 29792df1e9a6SAndrew Thompson sx_xunlock(&udev->sr_sx); 2980cb18f7d1SAlfred Perlstein } 2981cb18f7d1SAlfred Perlstein 2982cb18f7d1SAlfred Perlstein /* 2983cb18f7d1SAlfred Perlstein * The following function checks the enumerating lock for the given 2984cb18f7d1SAlfred Perlstein * USB device. 2985cb18f7d1SAlfred Perlstein */ 2986cb18f7d1SAlfred Perlstein 2987cb18f7d1SAlfred Perlstein uint8_t 2988cb18f7d1SAlfred Perlstein usbd_enum_is_locked(struct usb_device *udev) 2989cb18f7d1SAlfred Perlstein { 299091cd9240SAndrew Thompson return (sx_xlocked(&udev->enum_sx)); 2991cb18f7d1SAlfred Perlstein } 29928427ed84SHans Petter Selasky 29938427ed84SHans Petter Selasky /* 299464cb5e2aSHans Petter Selasky * The following function is used to serialize access to USB control 299564cb5e2aSHans Petter Selasky * transfers and the USB scratch area. If the lock is already grabbed 299664cb5e2aSHans Petter Selasky * this function returns zero. Else a value of one is returned. 299764cb5e2aSHans Petter Selasky */ 299864cb5e2aSHans Petter Selasky uint8_t 299964cb5e2aSHans Petter Selasky usbd_ctrl_lock(struct usb_device *udev) 300064cb5e2aSHans Petter Selasky { 300164cb5e2aSHans Petter Selasky if (sx_xlocked(&udev->ctrl_sx)) 300264cb5e2aSHans Petter Selasky return (0); 300364cb5e2aSHans Petter Selasky sx_xlock(&udev->ctrl_sx); 300464cb5e2aSHans Petter Selasky 300564cb5e2aSHans Petter Selasky /* 300664cb5e2aSHans Petter Selasky * We need to allow suspend and resume at this point, else the 300764cb5e2aSHans Petter Selasky * control transfer will timeout if the device is suspended! 300864cb5e2aSHans Petter Selasky */ 300964cb5e2aSHans Petter Selasky if (usbd_enum_is_locked(udev)) 301064cb5e2aSHans Petter Selasky usbd_sr_unlock(udev); 301164cb5e2aSHans Petter Selasky return (1); 301264cb5e2aSHans Petter Selasky } 301364cb5e2aSHans Petter Selasky 301464cb5e2aSHans Petter Selasky void 301564cb5e2aSHans Petter Selasky usbd_ctrl_unlock(struct usb_device *udev) 301664cb5e2aSHans Petter Selasky { 301764cb5e2aSHans Petter Selasky sx_xunlock(&udev->ctrl_sx); 301864cb5e2aSHans Petter Selasky 301964cb5e2aSHans Petter Selasky /* 302064cb5e2aSHans Petter Selasky * Restore the suspend and resume lock after we have unlocked 302164cb5e2aSHans Petter Selasky * the USB control transfer lock to avoid LOR: 302264cb5e2aSHans Petter Selasky */ 302364cb5e2aSHans Petter Selasky if (usbd_enum_is_locked(udev)) 302464cb5e2aSHans Petter Selasky usbd_sr_lock(udev); 302564cb5e2aSHans Petter Selasky } 302664cb5e2aSHans Petter Selasky 302764cb5e2aSHans Petter Selasky /* 30288427ed84SHans Petter Selasky * The following function is used to set the per-interface specific 30298427ed84SHans Petter Selasky * plug and play information. The string referred to by the pnpinfo 30308427ed84SHans Petter Selasky * argument can safely be freed after calling this function. The 30318427ed84SHans Petter Selasky * pnpinfo of an interface will be reset at device detach or when 30328427ed84SHans Petter Selasky * passing a NULL argument to this function. This function 30338427ed84SHans Petter Selasky * returns zero on success, else a USB_ERR_XXX failure code. 30348427ed84SHans Petter Selasky */ 30358427ed84SHans Petter Selasky 30368427ed84SHans Petter Selasky usb_error_t 30378427ed84SHans Petter Selasky usbd_set_pnpinfo(struct usb_device *udev, uint8_t iface_index, const char *pnpinfo) 30388427ed84SHans Petter Selasky { 30398427ed84SHans Petter Selasky struct usb_interface *iface; 30408427ed84SHans Petter Selasky 30418427ed84SHans Petter Selasky iface = usbd_get_iface(udev, iface_index); 30428427ed84SHans Petter Selasky if (iface == NULL) 30438427ed84SHans Petter Selasky return (USB_ERR_INVAL); 30448427ed84SHans Petter Selasky 30458427ed84SHans Petter Selasky if (iface->pnpinfo != NULL) { 30468427ed84SHans Petter Selasky free(iface->pnpinfo, M_USBDEV); 30478427ed84SHans Petter Selasky iface->pnpinfo = NULL; 30488427ed84SHans Petter Selasky } 30498427ed84SHans Petter Selasky 30508427ed84SHans Petter Selasky if (pnpinfo == NULL || pnpinfo[0] == 0) 30518427ed84SHans Petter Selasky return (0); /* success */ 30528427ed84SHans Petter Selasky 30538427ed84SHans Petter Selasky iface->pnpinfo = strdup(pnpinfo, M_USBDEV); 30548427ed84SHans Petter Selasky if (iface->pnpinfo == NULL) 30558427ed84SHans Petter Selasky return (USB_ERR_NOMEM); 30568427ed84SHans Petter Selasky 30578427ed84SHans Petter Selasky return (0); /* success */ 30588427ed84SHans Petter Selasky } 30598427ed84SHans Petter Selasky 3060d46dc4adSHans Petter Selasky usb_error_t 3061d46dc4adSHans Petter Selasky usbd_add_dynamic_quirk(struct usb_device *udev, uint16_t quirk) 3062d46dc4adSHans Petter Selasky { 3063d46dc4adSHans Petter Selasky uint8_t x; 3064d46dc4adSHans Petter Selasky 3065d46dc4adSHans Petter Selasky for (x = 0; x != USB_MAX_AUTO_QUIRK; x++) { 30663f37fb62SHans Petter Selasky if (udev->autoQuirk[x] == 0 || 30673f37fb62SHans Petter Selasky udev->autoQuirk[x] == quirk) { 3068d46dc4adSHans Petter Selasky udev->autoQuirk[x] = quirk; 3069d46dc4adSHans Petter Selasky return (0); /* success */ 3070d46dc4adSHans Petter Selasky } 3071d46dc4adSHans Petter Selasky } 3072d46dc4adSHans Petter Selasky return (USB_ERR_NOMEM); 3073d46dc4adSHans Petter Selasky } 3074a5cf1aaaSHans Petter Selasky 3075a5cf1aaaSHans Petter Selasky /* 3076a5cf1aaaSHans Petter Selasky * The following function is used to select the endpoint mode. It 3077a5cf1aaaSHans Petter Selasky * should not be called outside enumeration context. 3078a5cf1aaaSHans Petter Selasky */ 3079a5cf1aaaSHans Petter Selasky 3080a5cf1aaaSHans Petter Selasky usb_error_t 3081a5cf1aaaSHans Petter Selasky usbd_set_endpoint_mode(struct usb_device *udev, struct usb_endpoint *ep, 3082a5cf1aaaSHans Petter Selasky uint8_t ep_mode) 3083a5cf1aaaSHans Petter Selasky { 3084a5cf1aaaSHans Petter Selasky usb_error_t error; 3085f61fc4bdSHans Petter Selasky uint8_t do_unlock; 3086a5cf1aaaSHans Petter Selasky 30876950c75fSHans Petter Selasky /* Prevent re-enumeration */ 30886950c75fSHans Petter Selasky do_unlock = usbd_enum_lock(udev); 3089a5cf1aaaSHans Petter Selasky 3090a5cf1aaaSHans Petter Selasky if (udev->bus->methods->set_endpoint_mode != NULL) { 3091a5cf1aaaSHans Petter Selasky error = (udev->bus->methods->set_endpoint_mode) ( 3092a5cf1aaaSHans Petter Selasky udev, ep, ep_mode); 3093a5cf1aaaSHans Petter Selasky } else if (ep_mode != USB_EP_MODE_DEFAULT) { 3094a5cf1aaaSHans Petter Selasky error = USB_ERR_INVAL; 3095a5cf1aaaSHans Petter Selasky } else { 3096a5cf1aaaSHans Petter Selasky error = 0; 3097a5cf1aaaSHans Petter Selasky } 3098a5cf1aaaSHans Petter Selasky 3099a5cf1aaaSHans Petter Selasky /* only set new mode regardless of error */ 3100a5cf1aaaSHans Petter Selasky ep->ep_mode = ep_mode; 3101a5cf1aaaSHans Petter Selasky 3102f61fc4bdSHans Petter Selasky if (do_unlock) 3103f61fc4bdSHans Petter Selasky usbd_enum_unlock(udev); 3104a5cf1aaaSHans Petter Selasky return (error); 3105a5cf1aaaSHans Petter Selasky } 3106a5cf1aaaSHans Petter Selasky 3107a5cf1aaaSHans Petter Selasky uint8_t 3108a5cf1aaaSHans Petter Selasky usbd_get_endpoint_mode(struct usb_device *udev, struct usb_endpoint *ep) 3109a5cf1aaaSHans Petter Selasky { 3110a5cf1aaaSHans Petter Selasky return (ep->ep_mode); 3111a5cf1aaaSHans Petter Selasky } 3112